channel.c 39 KB

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