channel.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  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_mpb_desc - Send one or more multi-page buffer packets
  900. * using a GPADL Direct packet type.
  901. * The desc argument must include space for the VMBus descriptor. The
  902. * rangecount field must already be set.
  903. */
  904. int vmbus_sendpacket_mpb_desc(struct vmbus_channel *channel,
  905. struct vmbus_packet_mpb_array *desc,
  906. u32 desc_size,
  907. void *buffer, u32 bufferlen, u64 requestid)
  908. {
  909. u32 packetlen;
  910. u32 packetlen_aligned;
  911. struct kvec bufferlist[3];
  912. u64 aligned_data = 0;
  913. packetlen = desc_size + bufferlen;
  914. packetlen_aligned = ALIGN(packetlen, sizeof(u64));
  915. /* Setup the descriptor */
  916. desc->type = VM_PKT_DATA_USING_GPA_DIRECT;
  917. desc->flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
  918. desc->dataoffset8 = desc_size >> 3; /* in 8-bytes granularity */
  919. desc->length8 = (u16)(packetlen_aligned >> 3);
  920. desc->transactionid = VMBUS_RQST_ERROR; /* will be updated in hv_ringbuffer_write() */
  921. desc->reserved = 0;
  922. bufferlist[0].iov_base = desc;
  923. bufferlist[0].iov_len = desc_size;
  924. bufferlist[1].iov_base = buffer;
  925. bufferlist[1].iov_len = bufferlen;
  926. bufferlist[2].iov_base = &aligned_data;
  927. bufferlist[2].iov_len = (packetlen_aligned - packetlen);
  928. return hv_ringbuffer_write(channel, bufferlist, 3, requestid, NULL);
  929. }
  930. EXPORT_SYMBOL_GPL(vmbus_sendpacket_mpb_desc);
  931. /**
  932. * __vmbus_recvpacket() - Retrieve the user packet on the specified channel
  933. * @channel: Pointer to vmbus_channel structure
  934. * @buffer: Pointer to the buffer you want to receive the data into.
  935. * @bufferlen: Maximum size of what the buffer can hold.
  936. * @buffer_actual_len: The actual size of the data after it was received.
  937. * @requestid: Identifier of the request
  938. * @raw: true means keep the vmpacket_descriptor header in the received data.
  939. *
  940. * Receives directly from the hyper-v vmbus and puts the data it received
  941. * into Buffer. This will receive the data unparsed from hyper-v.
  942. *
  943. * Mainly used by Hyper-V drivers.
  944. */
  945. static inline int
  946. __vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
  947. u32 bufferlen, u32 *buffer_actual_len, u64 *requestid,
  948. bool raw)
  949. {
  950. return hv_ringbuffer_read(channel, buffer, bufferlen,
  951. buffer_actual_len, requestid, raw);
  952. }
  953. int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
  954. u32 bufferlen, u32 *buffer_actual_len,
  955. u64 *requestid)
  956. {
  957. return __vmbus_recvpacket(channel, buffer, bufferlen,
  958. buffer_actual_len, requestid, false);
  959. }
  960. EXPORT_SYMBOL(vmbus_recvpacket);
  961. /*
  962. * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
  963. */
  964. int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
  965. u32 bufferlen, u32 *buffer_actual_len,
  966. u64 *requestid)
  967. {
  968. return __vmbus_recvpacket(channel, buffer, bufferlen,
  969. buffer_actual_len, requestid, true);
  970. }
  971. EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);
  972. /*
  973. * vmbus_next_request_id - Returns a new request id. It is also
  974. * the index at which the guest memory address is stored.
  975. * Uses a spin lock to avoid race conditions.
  976. * @channel: Pointer to the VMbus channel struct
  977. * @rqst_add: Guest memory address to be stored in the array
  978. */
  979. u64 vmbus_next_request_id(struct vmbus_channel *channel, u64 rqst_addr)
  980. {
  981. struct vmbus_requestor *rqstor = &channel->requestor;
  982. unsigned long flags;
  983. u64 current_id;
  984. /* Check rqstor has been initialized */
  985. if (!channel->rqstor_size)
  986. return VMBUS_NO_RQSTOR;
  987. lock_requestor(channel, flags);
  988. current_id = rqstor->next_request_id;
  989. /* Requestor array is full */
  990. if (current_id >= rqstor->size) {
  991. unlock_requestor(channel, flags);
  992. return VMBUS_RQST_ERROR;
  993. }
  994. rqstor->next_request_id = rqstor->req_arr[current_id];
  995. rqstor->req_arr[current_id] = rqst_addr;
  996. /* The already held spin lock provides atomicity */
  997. bitmap_set(rqstor->req_bitmap, current_id, 1);
  998. unlock_requestor(channel, flags);
  999. /*
  1000. * Cannot return an ID of 0, which is reserved for an unsolicited
  1001. * message from Hyper-V; Hyper-V does not acknowledge (respond to)
  1002. * VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED requests with ID of
  1003. * 0 sent by the guest.
  1004. */
  1005. return current_id + 1;
  1006. }
  1007. EXPORT_SYMBOL_GPL(vmbus_next_request_id);
  1008. /* As in vmbus_request_addr_match() but without the requestor lock */
  1009. u64 __vmbus_request_addr_match(struct vmbus_channel *channel, u64 trans_id,
  1010. u64 rqst_addr)
  1011. {
  1012. struct vmbus_requestor *rqstor = &channel->requestor;
  1013. u64 req_addr;
  1014. /* Check rqstor has been initialized */
  1015. if (!channel->rqstor_size)
  1016. return VMBUS_NO_RQSTOR;
  1017. /* Hyper-V can send an unsolicited message with ID of 0 */
  1018. if (!trans_id)
  1019. return VMBUS_RQST_ERROR;
  1020. /* Data corresponding to trans_id is stored at trans_id - 1 */
  1021. trans_id--;
  1022. /* Invalid trans_id */
  1023. if (trans_id >= rqstor->size || !test_bit(trans_id, rqstor->req_bitmap))
  1024. return VMBUS_RQST_ERROR;
  1025. req_addr = rqstor->req_arr[trans_id];
  1026. if (rqst_addr == VMBUS_RQST_ADDR_ANY || req_addr == rqst_addr) {
  1027. rqstor->req_arr[trans_id] = rqstor->next_request_id;
  1028. rqstor->next_request_id = trans_id;
  1029. /* The already held spin lock provides atomicity */
  1030. bitmap_clear(rqstor->req_bitmap, trans_id, 1);
  1031. }
  1032. return req_addr;
  1033. }
  1034. EXPORT_SYMBOL_GPL(__vmbus_request_addr_match);
  1035. /*
  1036. * vmbus_request_addr_match - Clears/removes @trans_id from the @channel's
  1037. * requestor, provided the memory address stored at @trans_id equals @rqst_addr
  1038. * (or provided @rqst_addr matches the sentinel value VMBUS_RQST_ADDR_ANY).
  1039. *
  1040. * Returns the memory address stored at @trans_id, or VMBUS_RQST_ERROR if
  1041. * @trans_id is not contained in the requestor.
  1042. *
  1043. * Acquires and releases the requestor spin lock.
  1044. */
  1045. u64 vmbus_request_addr_match(struct vmbus_channel *channel, u64 trans_id,
  1046. u64 rqst_addr)
  1047. {
  1048. unsigned long flags;
  1049. u64 req_addr;
  1050. lock_requestor(channel, flags);
  1051. req_addr = __vmbus_request_addr_match(channel, trans_id, rqst_addr);
  1052. unlock_requestor(channel, flags);
  1053. return req_addr;
  1054. }
  1055. EXPORT_SYMBOL_GPL(vmbus_request_addr_match);
  1056. /*
  1057. * vmbus_request_addr - Returns the memory address stored at @trans_id
  1058. * in @rqstor. Uses a spin lock to avoid race conditions.
  1059. * @channel: Pointer to the VMbus channel struct
  1060. * @trans_id: Request id sent back from Hyper-V. Becomes the requestor's
  1061. * next request id.
  1062. */
  1063. u64 vmbus_request_addr(struct vmbus_channel *channel, u64 trans_id)
  1064. {
  1065. return vmbus_request_addr_match(channel, trans_id, VMBUS_RQST_ADDR_ANY);
  1066. }
  1067. EXPORT_SYMBOL_GPL(vmbus_request_addr);