channel.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2009, Microsoft Corporation.
  4. *
  5. * Authors:
  6. * Haiyang Zhang <[email protected]>
  7. * Hank Janssen <[email protected]>
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/kernel.h>
  11. #include <linux/sched.h>
  12. #include <linux/wait.h>
  13. #include <linux/mm.h>
  14. #include <linux/slab.h>
  15. #include <linux/module.h>
  16. #include <linux/hyperv.h>
  17. #include <linux/uio.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/set_memory.h>
  20. #include <asm/page.h>
  21. #include <asm/mem_encrypt.h>
  22. #include <asm/mshyperv.h>
  23. #include "hyperv_vmbus.h"
  24. /*
  25. * hv_gpadl_size - Return the real size of a gpadl, the size that Hyper-V uses
  26. *
  27. * For BUFFER gpadl, Hyper-V uses the exact same size as the guest does.
  28. *
  29. * For RING gpadl, in each ring, the guest uses one PAGE_SIZE as the header
  30. * (because of the alignment requirement), however, the hypervisor only
  31. * uses the first HV_HYP_PAGE_SIZE as the header, therefore leaving a
  32. * (PAGE_SIZE - HV_HYP_PAGE_SIZE) gap. And since there are two rings in a
  33. * ringbuffer, the total size for a RING gpadl that Hyper-V uses is the
  34. * total size that the guest uses minus twice of the gap size.
  35. */
  36. static inline u32 hv_gpadl_size(enum hv_gpadl_type type, u32 size)
  37. {
  38. switch (type) {
  39. case HV_GPADL_BUFFER:
  40. return size;
  41. case HV_GPADL_RING:
  42. /* The size of a ringbuffer must be page-aligned */
  43. BUG_ON(size % PAGE_SIZE);
  44. /*
  45. * Two things to notice here:
  46. * 1) We're processing two ring buffers as a unit
  47. * 2) We're skipping any space larger than HV_HYP_PAGE_SIZE in
  48. * the first guest-size page of each of the two ring buffers.
  49. * So we effectively subtract out two guest-size pages, and add
  50. * back two Hyper-V size pages.
  51. */
  52. return size - 2 * (PAGE_SIZE - HV_HYP_PAGE_SIZE);
  53. }
  54. BUG();
  55. return 0;
  56. }
  57. /*
  58. * hv_ring_gpadl_send_hvpgoffset - Calculate the send offset (in unit of
  59. * HV_HYP_PAGE) in a ring gpadl based on the
  60. * offset in the guest
  61. *
  62. * @offset: the offset (in bytes) where the send ringbuffer starts in the
  63. * virtual address space of the guest
  64. */
  65. static inline u32 hv_ring_gpadl_send_hvpgoffset(u32 offset)
  66. {
  67. /*
  68. * For RING gpadl, in each ring, the guest uses one PAGE_SIZE as the
  69. * header (because of the alignment requirement), however, the
  70. * hypervisor only uses the first HV_HYP_PAGE_SIZE as the header,
  71. * therefore leaving a (PAGE_SIZE - HV_HYP_PAGE_SIZE) gap.
  72. *
  73. * And to calculate the effective send offset in gpadl, we need to
  74. * substract this gap.
  75. */
  76. return (offset - (PAGE_SIZE - HV_HYP_PAGE_SIZE)) >> HV_HYP_PAGE_SHIFT;
  77. }
  78. /*
  79. * hv_gpadl_hvpfn - Return the Hyper-V page PFN of the @i th Hyper-V page in
  80. * the gpadl
  81. *
  82. * @type: the type of the gpadl
  83. * @kbuffer: the pointer to the gpadl in the guest
  84. * @size: the total size (in bytes) of the gpadl
  85. * @send_offset: the offset (in bytes) where the send ringbuffer starts in the
  86. * virtual address space of the guest
  87. * @i: the index
  88. */
  89. static inline u64 hv_gpadl_hvpfn(enum hv_gpadl_type type, void *kbuffer,
  90. u32 size, u32 send_offset, int i)
  91. {
  92. int send_idx = hv_ring_gpadl_send_hvpgoffset(send_offset);
  93. unsigned long delta = 0UL;
  94. switch (type) {
  95. case HV_GPADL_BUFFER:
  96. break;
  97. case HV_GPADL_RING:
  98. if (i == 0)
  99. delta = 0;
  100. else if (i <= send_idx)
  101. delta = PAGE_SIZE - HV_HYP_PAGE_SIZE;
  102. else
  103. delta = 2 * (PAGE_SIZE - HV_HYP_PAGE_SIZE);
  104. break;
  105. default:
  106. BUG();
  107. break;
  108. }
  109. return virt_to_hvpfn(kbuffer + delta + (HV_HYP_PAGE_SIZE * i));
  110. }
  111. /*
  112. * vmbus_setevent- Trigger an event notification on the specified
  113. * channel.
  114. */
  115. void vmbus_setevent(struct vmbus_channel *channel)
  116. {
  117. struct hv_monitor_page *monitorpage;
  118. trace_vmbus_setevent(channel);
  119. /*
  120. * For channels marked as in "low latency" mode
  121. * bypass the monitor page mechanism.
  122. */
  123. if (channel->offermsg.monitor_allocated && !channel->low_latency) {
  124. vmbus_send_interrupt(channel->offermsg.child_relid);
  125. /* Get the child to parent monitor page */
  126. monitorpage = vmbus_connection.monitor_pages[1];
  127. sync_set_bit(channel->monitor_bit,
  128. (unsigned long *)&monitorpage->trigger_group
  129. [channel->monitor_grp].pending);
  130. } else {
  131. vmbus_set_event(channel);
  132. }
  133. }
  134. EXPORT_SYMBOL_GPL(vmbus_setevent);
  135. /* vmbus_free_ring - drop mapping of ring buffer */
  136. void vmbus_free_ring(struct vmbus_channel *channel)
  137. {
  138. hv_ringbuffer_cleanup(&channel->outbound);
  139. hv_ringbuffer_cleanup(&channel->inbound);
  140. if (channel->ringbuffer_page) {
  141. __free_pages(channel->ringbuffer_page,
  142. get_order(channel->ringbuffer_pagecount
  143. << PAGE_SHIFT));
  144. channel->ringbuffer_page = NULL;
  145. }
  146. }
  147. EXPORT_SYMBOL_GPL(vmbus_free_ring);
  148. /* vmbus_alloc_ring - allocate and map pages for ring buffer */
  149. int vmbus_alloc_ring(struct vmbus_channel *newchannel,
  150. u32 send_size, u32 recv_size)
  151. {
  152. struct page *page;
  153. int order;
  154. if (send_size % PAGE_SIZE || recv_size % PAGE_SIZE)
  155. return -EINVAL;
  156. /* Allocate the ring buffer */
  157. order = get_order(send_size + recv_size);
  158. page = alloc_pages_node(cpu_to_node(newchannel->target_cpu),
  159. GFP_KERNEL|__GFP_ZERO, order);
  160. if (!page)
  161. page = alloc_pages(GFP_KERNEL|__GFP_ZERO, order);
  162. if (!page)
  163. return -ENOMEM;
  164. newchannel->ringbuffer_page = page;
  165. newchannel->ringbuffer_pagecount = (send_size + recv_size) >> PAGE_SHIFT;
  166. newchannel->ringbuffer_send_offset = send_size >> PAGE_SHIFT;
  167. return 0;
  168. }
  169. EXPORT_SYMBOL_GPL(vmbus_alloc_ring);
  170. /* Used for Hyper-V Socket: a guest client's connect() to the host */
  171. int vmbus_send_tl_connect_request(const guid_t *shv_guest_servie_id,
  172. const guid_t *shv_host_servie_id)
  173. {
  174. struct vmbus_channel_tl_connect_request conn_msg;
  175. int ret;
  176. memset(&conn_msg, 0, sizeof(conn_msg));
  177. conn_msg.header.msgtype = CHANNELMSG_TL_CONNECT_REQUEST;
  178. conn_msg.guest_endpoint_id = *shv_guest_servie_id;
  179. conn_msg.host_service_id = *shv_host_servie_id;
  180. ret = vmbus_post_msg(&conn_msg, sizeof(conn_msg), true);
  181. trace_vmbus_send_tl_connect_request(&conn_msg, ret);
  182. return ret;
  183. }
  184. EXPORT_SYMBOL_GPL(vmbus_send_tl_connect_request);
  185. static int send_modifychannel_without_ack(struct vmbus_channel *channel, u32 target_vp)
  186. {
  187. struct vmbus_channel_modifychannel msg;
  188. int ret;
  189. memset(&msg, 0, sizeof(msg));
  190. msg.header.msgtype = CHANNELMSG_MODIFYCHANNEL;
  191. msg.child_relid = channel->offermsg.child_relid;
  192. msg.target_vp = target_vp;
  193. ret = vmbus_post_msg(&msg, sizeof(msg), true);
  194. trace_vmbus_send_modifychannel(&msg, ret);
  195. return ret;
  196. }
  197. static int send_modifychannel_with_ack(struct vmbus_channel *channel, u32 target_vp)
  198. {
  199. struct vmbus_channel_modifychannel *msg;
  200. struct vmbus_channel_msginfo *info;
  201. unsigned long flags;
  202. int ret;
  203. info = kzalloc(sizeof(struct vmbus_channel_msginfo) +
  204. sizeof(struct vmbus_channel_modifychannel),
  205. GFP_KERNEL);
  206. if (!info)
  207. return -ENOMEM;
  208. init_completion(&info->waitevent);
  209. info->waiting_channel = channel;
  210. msg = (struct vmbus_channel_modifychannel *)info->msg;
  211. msg->header.msgtype = CHANNELMSG_MODIFYCHANNEL;
  212. msg->child_relid = channel->offermsg.child_relid;
  213. msg->target_vp = target_vp;
  214. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  215. list_add_tail(&info->msglistentry, &vmbus_connection.chn_msg_list);
  216. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  217. ret = vmbus_post_msg(msg, sizeof(*msg), true);
  218. trace_vmbus_send_modifychannel(msg, ret);
  219. if (ret != 0) {
  220. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  221. list_del(&info->msglistentry);
  222. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  223. goto free_info;
  224. }
  225. /*
  226. * Release channel_mutex; otherwise, vmbus_onoffer_rescind() could block on
  227. * the mutex and be unable to signal the completion.
  228. *
  229. * See the caller target_cpu_store() for information about the usage of the
  230. * mutex.
  231. */
  232. mutex_unlock(&vmbus_connection.channel_mutex);
  233. wait_for_completion(&info->waitevent);
  234. mutex_lock(&vmbus_connection.channel_mutex);
  235. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  236. list_del(&info->msglistentry);
  237. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  238. if (info->response.modify_response.status)
  239. ret = -EAGAIN;
  240. free_info:
  241. kfree(info);
  242. return ret;
  243. }
  244. /*
  245. * Set/change the vCPU (@target_vp) the channel (@child_relid) will interrupt.
  246. *
  247. * CHANNELMSG_MODIFYCHANNEL messages are aynchronous. When VMbus version 5.3
  248. * or later is negotiated, Hyper-V always sends an ACK in response to such a
  249. * message. For VMbus version 5.2 and earlier, it never sends an ACK. With-
  250. * out an ACK, we can not know when the host will stop interrupting the "old"
  251. * vCPU and start interrupting the "new" vCPU for the given channel.
  252. *
  253. * The CHANNELMSG_MODIFYCHANNEL message type is supported since VMBus version
  254. * VERSION_WIN10_V4_1.
  255. */
  256. int vmbus_send_modifychannel(struct vmbus_channel *channel, u32 target_vp)
  257. {
  258. if (vmbus_proto_version >= VERSION_WIN10_V5_3)
  259. return send_modifychannel_with_ack(channel, target_vp);
  260. return send_modifychannel_without_ack(channel, target_vp);
  261. }
  262. EXPORT_SYMBOL_GPL(vmbus_send_modifychannel);
  263. /*
  264. * create_gpadl_header - Creates a gpadl for the specified buffer
  265. */
  266. static int create_gpadl_header(enum hv_gpadl_type type, void *kbuffer,
  267. u32 size, u32 send_offset,
  268. struct vmbus_channel_msginfo **msginfo)
  269. {
  270. int i;
  271. int pagecount;
  272. struct vmbus_channel_gpadl_header *gpadl_header;
  273. struct vmbus_channel_gpadl_body *gpadl_body;
  274. struct vmbus_channel_msginfo *msgheader;
  275. struct vmbus_channel_msginfo *msgbody = NULL;
  276. u32 msgsize;
  277. int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
  278. pagecount = hv_gpadl_size(type, size) >> HV_HYP_PAGE_SHIFT;
  279. /* do we need a gpadl body msg */
  280. pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
  281. sizeof(struct vmbus_channel_gpadl_header) -
  282. sizeof(struct gpa_range);
  283. pfncount = pfnsize / sizeof(u64);
  284. if (pagecount > pfncount) {
  285. /* we need a gpadl body */
  286. /* fill in the header */
  287. msgsize = sizeof(struct vmbus_channel_msginfo) +
  288. sizeof(struct vmbus_channel_gpadl_header) +
  289. sizeof(struct gpa_range) + pfncount * sizeof(u64);
  290. msgheader = kzalloc(msgsize, GFP_KERNEL);
  291. if (!msgheader)
  292. goto nomem;
  293. INIT_LIST_HEAD(&msgheader->submsglist);
  294. msgheader->msgsize = msgsize;
  295. gpadl_header = (struct vmbus_channel_gpadl_header *)
  296. msgheader->msg;
  297. gpadl_header->rangecount = 1;
  298. gpadl_header->range_buflen = sizeof(struct gpa_range) +
  299. pagecount * sizeof(u64);
  300. gpadl_header->range[0].byte_offset = 0;
  301. gpadl_header->range[0].byte_count = hv_gpadl_size(type, size);
  302. for (i = 0; i < pfncount; i++)
  303. gpadl_header->range[0].pfn_array[i] = hv_gpadl_hvpfn(
  304. type, kbuffer, size, send_offset, i);
  305. *msginfo = msgheader;
  306. pfnsum = pfncount;
  307. pfnleft = pagecount - pfncount;
  308. /* how many pfns can we fit */
  309. pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
  310. sizeof(struct vmbus_channel_gpadl_body);
  311. pfncount = pfnsize / sizeof(u64);
  312. /* fill in the body */
  313. while (pfnleft) {
  314. if (pfnleft > pfncount)
  315. pfncurr = pfncount;
  316. else
  317. pfncurr = pfnleft;
  318. msgsize = sizeof(struct vmbus_channel_msginfo) +
  319. sizeof(struct vmbus_channel_gpadl_body) +
  320. pfncurr * sizeof(u64);
  321. msgbody = kzalloc(msgsize, GFP_KERNEL);
  322. if (!msgbody) {
  323. struct vmbus_channel_msginfo *pos = NULL;
  324. struct vmbus_channel_msginfo *tmp = NULL;
  325. /*
  326. * Free up all the allocated messages.
  327. */
  328. list_for_each_entry_safe(pos, tmp,
  329. &msgheader->submsglist,
  330. msglistentry) {
  331. list_del(&pos->msglistentry);
  332. kfree(pos);
  333. }
  334. goto nomem;
  335. }
  336. msgbody->msgsize = msgsize;
  337. gpadl_body =
  338. (struct vmbus_channel_gpadl_body *)msgbody->msg;
  339. /*
  340. * Gpadl is u32 and we are using a pointer which could
  341. * be 64-bit
  342. * This is governed by the guest/host protocol and
  343. * so the hypervisor guarantees that this is ok.
  344. */
  345. for (i = 0; i < pfncurr; i++)
  346. gpadl_body->pfn[i] = hv_gpadl_hvpfn(type,
  347. kbuffer, size, send_offset, pfnsum + i);
  348. /* add to msg header */
  349. list_add_tail(&msgbody->msglistentry,
  350. &msgheader->submsglist);
  351. pfnsum += pfncurr;
  352. pfnleft -= pfncurr;
  353. }
  354. } else {
  355. /* everything fits in a header */
  356. msgsize = sizeof(struct vmbus_channel_msginfo) +
  357. sizeof(struct vmbus_channel_gpadl_header) +
  358. sizeof(struct gpa_range) + pagecount * sizeof(u64);
  359. msgheader = kzalloc(msgsize, GFP_KERNEL);
  360. if (msgheader == NULL)
  361. goto nomem;
  362. INIT_LIST_HEAD(&msgheader->submsglist);
  363. msgheader->msgsize = msgsize;
  364. gpadl_header = (struct vmbus_channel_gpadl_header *)
  365. msgheader->msg;
  366. gpadl_header->rangecount = 1;
  367. gpadl_header->range_buflen = sizeof(struct gpa_range) +
  368. pagecount * sizeof(u64);
  369. gpadl_header->range[0].byte_offset = 0;
  370. gpadl_header->range[0].byte_count = hv_gpadl_size(type, size);
  371. for (i = 0; i < pagecount; i++)
  372. gpadl_header->range[0].pfn_array[i] = hv_gpadl_hvpfn(
  373. type, kbuffer, size, send_offset, i);
  374. *msginfo = msgheader;
  375. }
  376. return 0;
  377. nomem:
  378. kfree(msgheader);
  379. kfree(msgbody);
  380. return -ENOMEM;
  381. }
  382. /*
  383. * __vmbus_establish_gpadl - Establish a GPADL for a buffer or ringbuffer
  384. *
  385. * @channel: a channel
  386. * @type: the type of the corresponding GPADL, only meaningful for the guest.
  387. * @kbuffer: from kmalloc or vmalloc
  388. * @size: page-size multiple
  389. * @send_offset: the offset (in bytes) where the send ring buffer starts,
  390. * should be 0 for BUFFER type gpadl
  391. * @gpadl_handle: some funky thing
  392. */
  393. static int __vmbus_establish_gpadl(struct vmbus_channel *channel,
  394. enum hv_gpadl_type type, void *kbuffer,
  395. u32 size, u32 send_offset,
  396. struct vmbus_gpadl *gpadl)
  397. {
  398. struct vmbus_channel_gpadl_header *gpadlmsg;
  399. struct vmbus_channel_gpadl_body *gpadl_body;
  400. struct vmbus_channel_msginfo *msginfo = NULL;
  401. struct vmbus_channel_msginfo *submsginfo, *tmp;
  402. struct list_head *curr;
  403. u32 next_gpadl_handle;
  404. unsigned long flags;
  405. int ret = 0;
  406. next_gpadl_handle =
  407. (atomic_inc_return(&vmbus_connection.next_gpadl_handle) - 1);
  408. ret = create_gpadl_header(type, kbuffer, size, send_offset, &msginfo);
  409. if (ret)
  410. return ret;
  411. ret = set_memory_decrypted((unsigned long)kbuffer,
  412. PFN_UP(size));
  413. if (ret) {
  414. dev_warn(&channel->device_obj->device,
  415. "Failed to set host visibility for new GPADL %d.\n",
  416. ret);
  417. return ret;
  418. }
  419. init_completion(&msginfo->waitevent);
  420. msginfo->waiting_channel = channel;
  421. gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
  422. gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
  423. gpadlmsg->child_relid = channel->offermsg.child_relid;
  424. gpadlmsg->gpadl = next_gpadl_handle;
  425. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  426. list_add_tail(&msginfo->msglistentry,
  427. &vmbus_connection.chn_msg_list);
  428. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  429. if (channel->rescind) {
  430. ret = -ENODEV;
  431. goto cleanup;
  432. }
  433. ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
  434. sizeof(*msginfo), true);
  435. trace_vmbus_establish_gpadl_header(gpadlmsg, ret);
  436. if (ret != 0)
  437. goto cleanup;
  438. list_for_each(curr, &msginfo->submsglist) {
  439. submsginfo = (struct vmbus_channel_msginfo *)curr;
  440. gpadl_body =
  441. (struct vmbus_channel_gpadl_body *)submsginfo->msg;
  442. gpadl_body->header.msgtype =
  443. CHANNELMSG_GPADL_BODY;
  444. gpadl_body->gpadl = next_gpadl_handle;
  445. ret = vmbus_post_msg(gpadl_body,
  446. submsginfo->msgsize - sizeof(*submsginfo),
  447. true);
  448. trace_vmbus_establish_gpadl_body(gpadl_body, ret);
  449. if (ret != 0)
  450. goto cleanup;
  451. }
  452. wait_for_completion(&msginfo->waitevent);
  453. if (msginfo->response.gpadl_created.creation_status != 0) {
  454. pr_err("Failed to establish GPADL: err = 0x%x\n",
  455. msginfo->response.gpadl_created.creation_status);
  456. ret = -EDQUOT;
  457. goto cleanup;
  458. }
  459. if (channel->rescind) {
  460. ret = -ENODEV;
  461. goto cleanup;
  462. }
  463. /* At this point, we received the gpadl created msg */
  464. gpadl->gpadl_handle = gpadlmsg->gpadl;
  465. gpadl->buffer = kbuffer;
  466. gpadl->size = size;
  467. cleanup:
  468. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  469. list_del(&msginfo->msglistentry);
  470. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  471. list_for_each_entry_safe(submsginfo, tmp, &msginfo->submsglist,
  472. msglistentry) {
  473. kfree(submsginfo);
  474. }
  475. kfree(msginfo);
  476. if (ret)
  477. set_memory_encrypted((unsigned long)kbuffer,
  478. PFN_UP(size));
  479. return ret;
  480. }
  481. /*
  482. * vmbus_establish_gpadl - Establish a GPADL for the specified buffer
  483. *
  484. * @channel: a channel
  485. * @kbuffer: from kmalloc or vmalloc
  486. * @size: page-size multiple
  487. * @gpadl_handle: some funky thing
  488. */
  489. int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
  490. u32 size, struct vmbus_gpadl *gpadl)
  491. {
  492. return __vmbus_establish_gpadl(channel, HV_GPADL_BUFFER, kbuffer, size,
  493. 0U, gpadl);
  494. }
  495. EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
  496. /**
  497. * request_arr_init - Allocates memory for the requestor array. Each slot
  498. * keeps track of the next available slot in the array. Initially, each
  499. * slot points to the next one (as in a Linked List). The last slot
  500. * does not point to anything, so its value is U64_MAX by default.
  501. * @size The size of the array
  502. */
  503. static u64 *request_arr_init(u32 size)
  504. {
  505. int i;
  506. u64 *req_arr;
  507. req_arr = kcalloc(size, sizeof(u64), GFP_KERNEL);
  508. if (!req_arr)
  509. return NULL;
  510. for (i = 0; i < size - 1; i++)
  511. req_arr[i] = i + 1;
  512. /* Last slot (no more available slots) */
  513. req_arr[i] = U64_MAX;
  514. return req_arr;
  515. }
  516. /*
  517. * vmbus_alloc_requestor - Initializes @rqstor's fields.
  518. * Index 0 is the first free slot
  519. * @size: Size of the requestor array
  520. */
  521. static int vmbus_alloc_requestor(struct vmbus_requestor *rqstor, u32 size)
  522. {
  523. u64 *rqst_arr;
  524. unsigned long *bitmap;
  525. rqst_arr = request_arr_init(size);
  526. if (!rqst_arr)
  527. return -ENOMEM;
  528. bitmap = bitmap_zalloc(size, GFP_KERNEL);
  529. if (!bitmap) {
  530. kfree(rqst_arr);
  531. return -ENOMEM;
  532. }
  533. rqstor->req_arr = rqst_arr;
  534. rqstor->req_bitmap = bitmap;
  535. rqstor->size = size;
  536. rqstor->next_request_id = 0;
  537. spin_lock_init(&rqstor->req_lock);
  538. return 0;
  539. }
  540. /*
  541. * vmbus_free_requestor - Frees memory allocated for @rqstor
  542. * @rqstor: Pointer to the requestor struct
  543. */
  544. static void vmbus_free_requestor(struct vmbus_requestor *rqstor)
  545. {
  546. kfree(rqstor->req_arr);
  547. bitmap_free(rqstor->req_bitmap);
  548. }
  549. static int __vmbus_open(struct vmbus_channel *newchannel,
  550. void *userdata, u32 userdatalen,
  551. void (*onchannelcallback)(void *context), void *context)
  552. {
  553. struct vmbus_channel_open_channel *open_msg;
  554. struct vmbus_channel_msginfo *open_info = NULL;
  555. struct page *page = newchannel->ringbuffer_page;
  556. u32 send_pages, recv_pages;
  557. unsigned long flags;
  558. int err;
  559. if (userdatalen > MAX_USER_DEFINED_BYTES)
  560. return -EINVAL;
  561. send_pages = newchannel->ringbuffer_send_offset;
  562. recv_pages = newchannel->ringbuffer_pagecount - send_pages;
  563. if (newchannel->state != CHANNEL_OPEN_STATE)
  564. return -EINVAL;
  565. /* Create and init requestor */
  566. if (newchannel->rqstor_size) {
  567. if (vmbus_alloc_requestor(&newchannel->requestor, newchannel->rqstor_size))
  568. return -ENOMEM;
  569. }
  570. newchannel->state = CHANNEL_OPENING_STATE;
  571. newchannel->onchannel_callback = onchannelcallback;
  572. newchannel->channel_callback_context = context;
  573. if (!newchannel->max_pkt_size)
  574. newchannel->max_pkt_size = VMBUS_DEFAULT_MAX_PKT_SIZE;
  575. /* Establish the gpadl for the ring buffer */
  576. newchannel->ringbuffer_gpadlhandle.gpadl_handle = 0;
  577. err = __vmbus_establish_gpadl(newchannel, HV_GPADL_RING,
  578. page_address(newchannel->ringbuffer_page),
  579. (send_pages + recv_pages) << PAGE_SHIFT,
  580. newchannel->ringbuffer_send_offset << PAGE_SHIFT,
  581. &newchannel->ringbuffer_gpadlhandle);
  582. if (err)
  583. goto error_clean_ring;
  584. err = hv_ringbuffer_init(&newchannel->outbound,
  585. page, send_pages, 0);
  586. if (err)
  587. goto error_free_gpadl;
  588. err = hv_ringbuffer_init(&newchannel->inbound, &page[send_pages],
  589. recv_pages, newchannel->max_pkt_size);
  590. if (err)
  591. goto error_free_gpadl;
  592. /* Create and init the channel open message */
  593. open_info = kzalloc(sizeof(*open_info) +
  594. sizeof(struct vmbus_channel_open_channel),
  595. GFP_KERNEL);
  596. if (!open_info) {
  597. err = -ENOMEM;
  598. goto error_free_gpadl;
  599. }
  600. init_completion(&open_info->waitevent);
  601. open_info->waiting_channel = newchannel;
  602. open_msg = (struct vmbus_channel_open_channel *)open_info->msg;
  603. open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL;
  604. open_msg->openid = newchannel->offermsg.child_relid;
  605. open_msg->child_relid = newchannel->offermsg.child_relid;
  606. open_msg->ringbuffer_gpadlhandle
  607. = newchannel->ringbuffer_gpadlhandle.gpadl_handle;
  608. /*
  609. * The unit of ->downstream_ringbuffer_pageoffset is HV_HYP_PAGE and
  610. * the unit of ->ringbuffer_send_offset (i.e. send_pages) is PAGE, so
  611. * here we calculate it into HV_HYP_PAGE.
  612. */
  613. open_msg->downstream_ringbuffer_pageoffset =
  614. hv_ring_gpadl_send_hvpgoffset(send_pages << PAGE_SHIFT);
  615. open_msg->target_vp = hv_cpu_number_to_vp_number(newchannel->target_cpu);
  616. if (userdatalen)
  617. memcpy(open_msg->userdata, userdata, userdatalen);
  618. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  619. list_add_tail(&open_info->msglistentry,
  620. &vmbus_connection.chn_msg_list);
  621. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  622. if (newchannel->rescind) {
  623. err = -ENODEV;
  624. goto error_clean_msglist;
  625. }
  626. err = vmbus_post_msg(open_msg,
  627. sizeof(struct vmbus_channel_open_channel), true);
  628. trace_vmbus_open(open_msg, err);
  629. if (err != 0)
  630. goto error_clean_msglist;
  631. wait_for_completion(&open_info->waitevent);
  632. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  633. list_del(&open_info->msglistentry);
  634. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  635. if (newchannel->rescind) {
  636. err = -ENODEV;
  637. goto error_free_info;
  638. }
  639. if (open_info->response.open_result.status) {
  640. err = -EAGAIN;
  641. goto error_free_info;
  642. }
  643. newchannel->state = CHANNEL_OPENED_STATE;
  644. kfree(open_info);
  645. return 0;
  646. error_clean_msglist:
  647. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  648. list_del(&open_info->msglistentry);
  649. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  650. error_free_info:
  651. kfree(open_info);
  652. error_free_gpadl:
  653. vmbus_teardown_gpadl(newchannel, &newchannel->ringbuffer_gpadlhandle);
  654. error_clean_ring:
  655. hv_ringbuffer_cleanup(&newchannel->outbound);
  656. hv_ringbuffer_cleanup(&newchannel->inbound);
  657. vmbus_free_requestor(&newchannel->requestor);
  658. newchannel->state = CHANNEL_OPEN_STATE;
  659. return err;
  660. }
  661. /*
  662. * vmbus_connect_ring - Open the channel but reuse ring buffer
  663. */
  664. int vmbus_connect_ring(struct vmbus_channel *newchannel,
  665. void (*onchannelcallback)(void *context), void *context)
  666. {
  667. return __vmbus_open(newchannel, NULL, 0, onchannelcallback, context);
  668. }
  669. EXPORT_SYMBOL_GPL(vmbus_connect_ring);
  670. /*
  671. * vmbus_open - Open the specified channel.
  672. */
  673. int vmbus_open(struct vmbus_channel *newchannel,
  674. u32 send_ringbuffer_size, u32 recv_ringbuffer_size,
  675. void *userdata, u32 userdatalen,
  676. void (*onchannelcallback)(void *context), void *context)
  677. {
  678. int err;
  679. err = vmbus_alloc_ring(newchannel, send_ringbuffer_size,
  680. recv_ringbuffer_size);
  681. if (err)
  682. return err;
  683. err = __vmbus_open(newchannel, userdata, userdatalen,
  684. onchannelcallback, context);
  685. if (err)
  686. vmbus_free_ring(newchannel);
  687. return err;
  688. }
  689. EXPORT_SYMBOL_GPL(vmbus_open);
  690. /*
  691. * vmbus_teardown_gpadl -Teardown the specified GPADL handle
  692. */
  693. int vmbus_teardown_gpadl(struct vmbus_channel *channel, struct vmbus_gpadl *gpadl)
  694. {
  695. struct vmbus_channel_gpadl_teardown *msg;
  696. struct vmbus_channel_msginfo *info;
  697. unsigned long flags;
  698. int ret;
  699. info = kzalloc(sizeof(*info) +
  700. sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
  701. if (!info)
  702. return -ENOMEM;
  703. init_completion(&info->waitevent);
  704. info->waiting_channel = channel;
  705. msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
  706. msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
  707. msg->child_relid = channel->offermsg.child_relid;
  708. msg->gpadl = gpadl->gpadl_handle;
  709. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  710. list_add_tail(&info->msglistentry,
  711. &vmbus_connection.chn_msg_list);
  712. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  713. if (channel->rescind)
  714. goto post_msg_err;
  715. ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_gpadl_teardown),
  716. true);
  717. trace_vmbus_teardown_gpadl(msg, ret);
  718. if (ret)
  719. goto post_msg_err;
  720. wait_for_completion(&info->waitevent);
  721. gpadl->gpadl_handle = 0;
  722. post_msg_err:
  723. /*
  724. * If the channel has been rescinded;
  725. * we will be awakened by the rescind
  726. * handler; set the error code to zero so we don't leak memory.
  727. */
  728. if (channel->rescind)
  729. ret = 0;
  730. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  731. list_del(&info->msglistentry);
  732. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  733. kfree(info);
  734. ret = set_memory_encrypted((unsigned long)gpadl->buffer,
  735. PFN_UP(gpadl->size));
  736. if (ret)
  737. pr_warn("Fail to set mem host visibility in GPADL teardown %d.\n", ret);
  738. return ret;
  739. }
  740. EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
  741. void vmbus_reset_channel_cb(struct vmbus_channel *channel)
  742. {
  743. unsigned long flags;
  744. /*
  745. * vmbus_on_event(), running in the per-channel tasklet, can race
  746. * with vmbus_close_internal() in the case of SMP guest, e.g., when
  747. * the former is accessing channel->inbound.ring_buffer, the latter
  748. * could be freeing the ring_buffer pages, so here we must stop it
  749. * first.
  750. *
  751. * vmbus_chan_sched() might call the netvsc driver callback function
  752. * that ends up scheduling NAPI work that accesses the ring buffer.
  753. * At this point, we have to ensure that any such work is completed
  754. * and that the channel ring buffer is no longer being accessed, cf.
  755. * the calls to napi_disable() in netvsc_device_remove().
  756. */
  757. tasklet_disable(&channel->callback_event);
  758. /* See the inline comments in vmbus_chan_sched(). */
  759. spin_lock_irqsave(&channel->sched_lock, flags);
  760. channel->onchannel_callback = NULL;
  761. spin_unlock_irqrestore(&channel->sched_lock, flags);
  762. channel->sc_creation_callback = NULL;
  763. /* Re-enable tasklet for use on re-open */
  764. tasklet_enable(&channel->callback_event);
  765. }
  766. static int vmbus_close_internal(struct vmbus_channel *channel)
  767. {
  768. struct vmbus_channel_close_channel *msg;
  769. int ret;
  770. vmbus_reset_channel_cb(channel);
  771. /*
  772. * In case a device driver's probe() fails (e.g.,
  773. * util_probe() -> vmbus_open() returns -ENOMEM) and the device is
  774. * rescinded later (e.g., we dynamically disable an Integrated Service
  775. * in Hyper-V Manager), the driver's remove() invokes vmbus_close():
  776. * here we should skip most of the below cleanup work.
  777. */
  778. if (channel->state != CHANNEL_OPENED_STATE)
  779. return -EINVAL;
  780. channel->state = CHANNEL_OPEN_STATE;
  781. /* Send a closing message */
  782. msg = &channel->close_msg.msg;
  783. msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
  784. msg->child_relid = channel->offermsg.child_relid;
  785. ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel),
  786. true);
  787. trace_vmbus_close_internal(msg, ret);
  788. if (ret) {
  789. pr_err("Close failed: close post msg return is %d\n", ret);
  790. /*
  791. * If we failed to post the close msg,
  792. * it is perhaps better to leak memory.
  793. */
  794. }
  795. /* Tear down the gpadl for the channel's ring buffer */
  796. else if (channel->ringbuffer_gpadlhandle.gpadl_handle) {
  797. ret = vmbus_teardown_gpadl(channel, &channel->ringbuffer_gpadlhandle);
  798. if (ret) {
  799. pr_err("Close failed: teardown gpadl return %d\n", ret);
  800. /*
  801. * If we failed to teardown gpadl,
  802. * it is perhaps better to leak memory.
  803. */
  804. }
  805. }
  806. if (!ret)
  807. vmbus_free_requestor(&channel->requestor);
  808. return ret;
  809. }
  810. /* disconnect ring - close all channels */
  811. int vmbus_disconnect_ring(struct vmbus_channel *channel)
  812. {
  813. struct vmbus_channel *cur_channel, *tmp;
  814. int ret;
  815. if (channel->primary_channel != NULL)
  816. return -EINVAL;
  817. list_for_each_entry_safe(cur_channel, tmp, &channel->sc_list, sc_list) {
  818. if (cur_channel->rescind)
  819. wait_for_completion(&cur_channel->rescind_event);
  820. mutex_lock(&vmbus_connection.channel_mutex);
  821. if (vmbus_close_internal(cur_channel) == 0) {
  822. vmbus_free_ring(cur_channel);
  823. if (cur_channel->rescind)
  824. hv_process_channel_removal(cur_channel);
  825. }
  826. mutex_unlock(&vmbus_connection.channel_mutex);
  827. }
  828. /*
  829. * Now close the primary.
  830. */
  831. mutex_lock(&vmbus_connection.channel_mutex);
  832. ret = vmbus_close_internal(channel);
  833. mutex_unlock(&vmbus_connection.channel_mutex);
  834. return ret;
  835. }
  836. EXPORT_SYMBOL_GPL(vmbus_disconnect_ring);
  837. /*
  838. * vmbus_close - Close the specified channel
  839. */
  840. void vmbus_close(struct vmbus_channel *channel)
  841. {
  842. if (vmbus_disconnect_ring(channel) == 0)
  843. vmbus_free_ring(channel);
  844. }
  845. EXPORT_SYMBOL_GPL(vmbus_close);
  846. /**
  847. * vmbus_sendpacket_getid() - Send the specified buffer on the given channel
  848. * @channel: Pointer to vmbus_channel structure
  849. * @buffer: Pointer to the buffer you want to send the data from.
  850. * @bufferlen: Maximum size of what the buffer holds.
  851. * @requestid: Identifier of the request
  852. * @trans_id: Identifier of the transaction associated to this request, if
  853. * the send is successful; undefined, otherwise.
  854. * @type: Type of packet that is being sent e.g. negotiate, time
  855. * packet etc.
  856. * @flags: 0 or VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
  857. *
  858. * Sends data in @buffer directly to Hyper-V via the vmbus.
  859. * This will send the data unparsed to Hyper-V.
  860. *
  861. * Mainly used by Hyper-V drivers.
  862. */
  863. int vmbus_sendpacket_getid(struct vmbus_channel *channel, void *buffer,
  864. u32 bufferlen, u64 requestid, u64 *trans_id,
  865. enum vmbus_packet_type type, u32 flags)
  866. {
  867. struct vmpacket_descriptor desc;
  868. u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
  869. u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
  870. struct kvec bufferlist[3];
  871. u64 aligned_data = 0;
  872. int num_vecs = ((bufferlen != 0) ? 3 : 1);
  873. /* Setup the descriptor */
  874. desc.type = type; /* VmbusPacketTypeDataInBand; */
  875. desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
  876. /* in 8-bytes granularity */
  877. desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
  878. desc.len8 = (u16)(packetlen_aligned >> 3);
  879. desc.trans_id = VMBUS_RQST_ERROR; /* will be updated in hv_ringbuffer_write() */
  880. bufferlist[0].iov_base = &desc;
  881. bufferlist[0].iov_len = sizeof(struct vmpacket_descriptor);
  882. bufferlist[1].iov_base = buffer;
  883. bufferlist[1].iov_len = bufferlen;
  884. bufferlist[2].iov_base = &aligned_data;
  885. bufferlist[2].iov_len = (packetlen_aligned - packetlen);
  886. return hv_ringbuffer_write(channel, bufferlist, num_vecs, requestid, trans_id);
  887. }
  888. EXPORT_SYMBOL(vmbus_sendpacket_getid);
  889. /**
  890. * vmbus_sendpacket() - Send the specified buffer on the given channel
  891. * @channel: Pointer to vmbus_channel structure
  892. * @buffer: Pointer to the buffer you want to send the data from.
  893. * @bufferlen: Maximum size of what the buffer holds.
  894. * @requestid: Identifier of the request
  895. * @type: Type of packet that is being sent e.g. negotiate, time
  896. * packet etc.
  897. * @flags: 0 or VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
  898. *
  899. * Sends data in @buffer directly to Hyper-V via the vmbus.
  900. * This will send the data unparsed to Hyper-V.
  901. *
  902. * Mainly used by Hyper-V drivers.
  903. */
  904. int vmbus_sendpacket(struct vmbus_channel *channel, void *buffer,
  905. u32 bufferlen, u64 requestid,
  906. enum vmbus_packet_type type, u32 flags)
  907. {
  908. return vmbus_sendpacket_getid(channel, buffer, bufferlen,
  909. requestid, NULL, type, flags);
  910. }
  911. EXPORT_SYMBOL(vmbus_sendpacket);
  912. /*
  913. * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
  914. * packets using a GPADL Direct packet type. This interface allows you
  915. * to control notifying the host. This will be useful for sending
  916. * batched data. Also the sender can control the send flags
  917. * explicitly.
  918. */
  919. int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
  920. struct hv_page_buffer pagebuffers[],
  921. u32 pagecount, void *buffer, u32 bufferlen,
  922. u64 requestid)
  923. {
  924. int i;
  925. struct vmbus_channel_packet_page_buffer desc;
  926. u32 descsize;
  927. u32 packetlen;
  928. u32 packetlen_aligned;
  929. struct kvec bufferlist[3];
  930. u64 aligned_data = 0;
  931. if (pagecount > MAX_PAGE_BUFFER_COUNT)
  932. return -EINVAL;
  933. /*
  934. * Adjust the size down since vmbus_channel_packet_page_buffer is the
  935. * largest size we support
  936. */
  937. descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
  938. ((MAX_PAGE_BUFFER_COUNT - pagecount) *
  939. sizeof(struct hv_page_buffer));
  940. packetlen = descsize + bufferlen;
  941. packetlen_aligned = ALIGN(packetlen, sizeof(u64));
  942. /* Setup the descriptor */
  943. desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
  944. desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
  945. desc.dataoffset8 = descsize >> 3; /* in 8-bytes granularity */
  946. desc.length8 = (u16)(packetlen_aligned >> 3);
  947. desc.transactionid = VMBUS_RQST_ERROR; /* will be updated in hv_ringbuffer_write() */
  948. desc.reserved = 0;
  949. desc.rangecount = pagecount;
  950. for (i = 0; i < pagecount; i++) {
  951. desc.range[i].len = pagebuffers[i].len;
  952. desc.range[i].offset = pagebuffers[i].offset;
  953. desc.range[i].pfn = pagebuffers[i].pfn;
  954. }
  955. bufferlist[0].iov_base = &desc;
  956. bufferlist[0].iov_len = descsize;
  957. bufferlist[1].iov_base = buffer;
  958. bufferlist[1].iov_len = bufferlen;
  959. bufferlist[2].iov_base = &aligned_data;
  960. bufferlist[2].iov_len = (packetlen_aligned - packetlen);
  961. return hv_ringbuffer_write(channel, bufferlist, 3, requestid, NULL);
  962. }
  963. EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
  964. /*
  965. * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
  966. * using a GPADL Direct packet type.
  967. * The buffer includes the vmbus descriptor.
  968. */
  969. int vmbus_sendpacket_mpb_desc(struct vmbus_channel *channel,
  970. struct vmbus_packet_mpb_array *desc,
  971. u32 desc_size,
  972. void *buffer, u32 bufferlen, u64 requestid)
  973. {
  974. u32 packetlen;
  975. u32 packetlen_aligned;
  976. struct kvec bufferlist[3];
  977. u64 aligned_data = 0;
  978. packetlen = desc_size + bufferlen;
  979. packetlen_aligned = ALIGN(packetlen, sizeof(u64));
  980. /* Setup the descriptor */
  981. desc->type = VM_PKT_DATA_USING_GPA_DIRECT;
  982. desc->flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
  983. desc->dataoffset8 = desc_size >> 3; /* in 8-bytes granularity */
  984. desc->length8 = (u16)(packetlen_aligned >> 3);
  985. desc->transactionid = VMBUS_RQST_ERROR; /* will be updated in hv_ringbuffer_write() */
  986. desc->reserved = 0;
  987. desc->rangecount = 1;
  988. bufferlist[0].iov_base = desc;
  989. bufferlist[0].iov_len = desc_size;
  990. bufferlist[1].iov_base = buffer;
  991. bufferlist[1].iov_len = bufferlen;
  992. bufferlist[2].iov_base = &aligned_data;
  993. bufferlist[2].iov_len = (packetlen_aligned - packetlen);
  994. return hv_ringbuffer_write(channel, bufferlist, 3, requestid, NULL);
  995. }
  996. EXPORT_SYMBOL_GPL(vmbus_sendpacket_mpb_desc);
  997. /**
  998. * __vmbus_recvpacket() - Retrieve the user packet on the specified channel
  999. * @channel: Pointer to vmbus_channel structure
  1000. * @buffer: Pointer to the buffer you want to receive the data into.
  1001. * @bufferlen: Maximum size of what the buffer can hold.
  1002. * @buffer_actual_len: The actual size of the data after it was received.
  1003. * @requestid: Identifier of the request
  1004. * @raw: true means keep the vmpacket_descriptor header in the received data.
  1005. *
  1006. * Receives directly from the hyper-v vmbus and puts the data it received
  1007. * into Buffer. This will receive the data unparsed from hyper-v.
  1008. *
  1009. * Mainly used by Hyper-V drivers.
  1010. */
  1011. static inline int
  1012. __vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
  1013. u32 bufferlen, u32 *buffer_actual_len, u64 *requestid,
  1014. bool raw)
  1015. {
  1016. return hv_ringbuffer_read(channel, buffer, bufferlen,
  1017. buffer_actual_len, requestid, raw);
  1018. }
  1019. int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
  1020. u32 bufferlen, u32 *buffer_actual_len,
  1021. u64 *requestid)
  1022. {
  1023. return __vmbus_recvpacket(channel, buffer, bufferlen,
  1024. buffer_actual_len, requestid, false);
  1025. }
  1026. EXPORT_SYMBOL(vmbus_recvpacket);
  1027. /*
  1028. * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
  1029. */
  1030. int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
  1031. u32 bufferlen, u32 *buffer_actual_len,
  1032. u64 *requestid)
  1033. {
  1034. return __vmbus_recvpacket(channel, buffer, bufferlen,
  1035. buffer_actual_len, requestid, true);
  1036. }
  1037. EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);
  1038. /*
  1039. * vmbus_next_request_id - Returns a new request id. It is also
  1040. * the index at which the guest memory address is stored.
  1041. * Uses a spin lock to avoid race conditions.
  1042. * @channel: Pointer to the VMbus channel struct
  1043. * @rqst_add: Guest memory address to be stored in the array
  1044. */
  1045. u64 vmbus_next_request_id(struct vmbus_channel *channel, u64 rqst_addr)
  1046. {
  1047. struct vmbus_requestor *rqstor = &channel->requestor;
  1048. unsigned long flags;
  1049. u64 current_id;
  1050. /* Check rqstor has been initialized */
  1051. if (!channel->rqstor_size)
  1052. return VMBUS_NO_RQSTOR;
  1053. lock_requestor(channel, flags);
  1054. current_id = rqstor->next_request_id;
  1055. /* Requestor array is full */
  1056. if (current_id >= rqstor->size) {
  1057. unlock_requestor(channel, flags);
  1058. return VMBUS_RQST_ERROR;
  1059. }
  1060. rqstor->next_request_id = rqstor->req_arr[current_id];
  1061. rqstor->req_arr[current_id] = rqst_addr;
  1062. /* The already held spin lock provides atomicity */
  1063. bitmap_set(rqstor->req_bitmap, current_id, 1);
  1064. unlock_requestor(channel, flags);
  1065. /*
  1066. * Cannot return an ID of 0, which is reserved for an unsolicited
  1067. * message from Hyper-V; Hyper-V does not acknowledge (respond to)
  1068. * VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED requests with ID of
  1069. * 0 sent by the guest.
  1070. */
  1071. return current_id + 1;
  1072. }
  1073. EXPORT_SYMBOL_GPL(vmbus_next_request_id);
  1074. /* As in vmbus_request_addr_match() but without the requestor lock */
  1075. u64 __vmbus_request_addr_match(struct vmbus_channel *channel, u64 trans_id,
  1076. u64 rqst_addr)
  1077. {
  1078. struct vmbus_requestor *rqstor = &channel->requestor;
  1079. u64 req_addr;
  1080. /* Check rqstor has been initialized */
  1081. if (!channel->rqstor_size)
  1082. return VMBUS_NO_RQSTOR;
  1083. /* Hyper-V can send an unsolicited message with ID of 0 */
  1084. if (!trans_id)
  1085. return VMBUS_RQST_ERROR;
  1086. /* Data corresponding to trans_id is stored at trans_id - 1 */
  1087. trans_id--;
  1088. /* Invalid trans_id */
  1089. if (trans_id >= rqstor->size || !test_bit(trans_id, rqstor->req_bitmap))
  1090. return VMBUS_RQST_ERROR;
  1091. req_addr = rqstor->req_arr[trans_id];
  1092. if (rqst_addr == VMBUS_RQST_ADDR_ANY || req_addr == rqst_addr) {
  1093. rqstor->req_arr[trans_id] = rqstor->next_request_id;
  1094. rqstor->next_request_id = trans_id;
  1095. /* The already held spin lock provides atomicity */
  1096. bitmap_clear(rqstor->req_bitmap, trans_id, 1);
  1097. }
  1098. return req_addr;
  1099. }
  1100. EXPORT_SYMBOL_GPL(__vmbus_request_addr_match);
  1101. /*
  1102. * vmbus_request_addr_match - Clears/removes @trans_id from the @channel's
  1103. * requestor, provided the memory address stored at @trans_id equals @rqst_addr
  1104. * (or provided @rqst_addr matches the sentinel value VMBUS_RQST_ADDR_ANY).
  1105. *
  1106. * Returns the memory address stored at @trans_id, or VMBUS_RQST_ERROR if
  1107. * @trans_id is not contained in the requestor.
  1108. *
  1109. * Acquires and releases the requestor spin lock.
  1110. */
  1111. u64 vmbus_request_addr_match(struct vmbus_channel *channel, u64 trans_id,
  1112. u64 rqst_addr)
  1113. {
  1114. unsigned long flags;
  1115. u64 req_addr;
  1116. lock_requestor(channel, flags);
  1117. req_addr = __vmbus_request_addr_match(channel, trans_id, rqst_addr);
  1118. unlock_requestor(channel, flags);
  1119. return req_addr;
  1120. }
  1121. EXPORT_SYMBOL_GPL(vmbus_request_addr_match);
  1122. /*
  1123. * vmbus_request_addr - Returns the memory address stored at @trans_id
  1124. * in @rqstor. Uses a spin lock to avoid race conditions.
  1125. * @channel: Pointer to the VMbus channel struct
  1126. * @trans_id: Request id sent back from Hyper-V. Becomes the requestor's
  1127. * next request id.
  1128. */
  1129. u64 vmbus_request_addr(struct vmbus_channel *channel, u64 trans_id)
  1130. {
  1131. return vmbus_request_addr_match(channel, trans_id, VMBUS_RQST_ADDR_ANY);
  1132. }
  1133. EXPORT_SYMBOL_GPL(vmbus_request_addr);