channel.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  1. /*
  2. * Copyright (c) 2009, Microsoft Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  15. * Place - Suite 330, Boston, MA 02111-1307 USA.
  16. *
  17. * Authors:
  18. * Haiyang Zhang <haiyangz@microsoft.com>
  19. * Hank Janssen <hjanssen@microsoft.com>
  20. */
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #include <linux/kernel.h>
  23. #include <linux/sched.h>
  24. #include <linux/wait.h>
  25. #include <linux/mm.h>
  26. #include <linux/slab.h>
  27. #include <linux/module.h>
  28. #include <linux/hyperv.h>
  29. #include <linux/uio.h>
  30. #include <linux/interrupt.h>
  31. #include "hyperv_vmbus.h"
  32. #define NUM_PAGES_SPANNED(addr, len) \
  33. ((PAGE_ALIGN(addr + len) >> PAGE_SHIFT) - (addr >> PAGE_SHIFT))
  34. /*
  35. * vmbus_setevent- Trigger an event notification on the specified
  36. * channel.
  37. */
  38. static void vmbus_setevent(struct vmbus_channel *channel)
  39. {
  40. struct hv_monitor_page *monitorpage;
  41. if (channel->offermsg.monitor_allocated) {
  42. /* Each u32 represents 32 channels */
  43. sync_set_bit(channel->offermsg.child_relid & 31,
  44. (unsigned long *) vmbus_connection.send_int_page +
  45. (channel->offermsg.child_relid >> 5));
  46. /* Get the child to parent monitor page */
  47. monitorpage = vmbus_connection.monitor_pages[1];
  48. sync_set_bit(channel->monitor_bit,
  49. (unsigned long *)&monitorpage->trigger_group
  50. [channel->monitor_grp].pending);
  51. } else {
  52. vmbus_set_event(channel);
  53. }
  54. }
  55. /*
  56. * vmbus_open - Open the specified channel.
  57. */
  58. int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size,
  59. u32 recv_ringbuffer_size, void *userdata, u32 userdatalen,
  60. void (*onchannelcallback)(void *context), void *context)
  61. {
  62. struct vmbus_channel_open_channel *open_msg;
  63. struct vmbus_channel_msginfo *open_info = NULL;
  64. void *in, *out;
  65. unsigned long flags;
  66. int ret, err = 0;
  67. struct page *page;
  68. spin_lock_irqsave(&newchannel->lock, flags);
  69. if (newchannel->state == CHANNEL_OPEN_STATE) {
  70. newchannel->state = CHANNEL_OPENING_STATE;
  71. } else {
  72. spin_unlock_irqrestore(&newchannel->lock, flags);
  73. return -EINVAL;
  74. }
  75. spin_unlock_irqrestore(&newchannel->lock, flags);
  76. newchannel->onchannel_callback = onchannelcallback;
  77. newchannel->channel_callback_context = context;
  78. /* Allocate the ring buffer */
  79. page = alloc_pages_node(cpu_to_node(newchannel->target_cpu),
  80. GFP_KERNEL|__GFP_ZERO,
  81. get_order(send_ringbuffer_size +
  82. recv_ringbuffer_size));
  83. if (!page)
  84. out = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO,
  85. get_order(send_ringbuffer_size +
  86. recv_ringbuffer_size));
  87. else
  88. out = (void *)page_address(page);
  89. if (!out) {
  90. err = -ENOMEM;
  91. goto error0;
  92. }
  93. in = (void *)((unsigned long)out + send_ringbuffer_size);
  94. newchannel->ringbuffer_pages = out;
  95. newchannel->ringbuffer_pagecount = (send_ringbuffer_size +
  96. recv_ringbuffer_size) >> PAGE_SHIFT;
  97. ret = hv_ringbuffer_init(
  98. &newchannel->outbound, out, send_ringbuffer_size);
  99. if (ret != 0) {
  100. err = ret;
  101. goto error0;
  102. }
  103. ret = hv_ringbuffer_init(
  104. &newchannel->inbound, in, recv_ringbuffer_size);
  105. if (ret != 0) {
  106. err = ret;
  107. goto error0;
  108. }
  109. /* Establish the gpadl for the ring buffer */
  110. newchannel->ringbuffer_gpadlhandle = 0;
  111. ret = vmbus_establish_gpadl(newchannel,
  112. newchannel->outbound.ring_buffer,
  113. send_ringbuffer_size +
  114. recv_ringbuffer_size,
  115. &newchannel->ringbuffer_gpadlhandle);
  116. if (ret != 0) {
  117. err = ret;
  118. goto error0;
  119. }
  120. /* Create and init the channel open message */
  121. open_info = kmalloc(sizeof(*open_info) +
  122. sizeof(struct vmbus_channel_open_channel),
  123. GFP_KERNEL);
  124. if (!open_info) {
  125. err = -ENOMEM;
  126. goto error_gpadl;
  127. }
  128. init_completion(&open_info->waitevent);
  129. open_msg = (struct vmbus_channel_open_channel *)open_info->msg;
  130. open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL;
  131. open_msg->openid = newchannel->offermsg.child_relid;
  132. open_msg->child_relid = newchannel->offermsg.child_relid;
  133. open_msg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle;
  134. open_msg->downstream_ringbuffer_pageoffset = send_ringbuffer_size >>
  135. PAGE_SHIFT;
  136. open_msg->target_vp = newchannel->target_vp;
  137. if (userdatalen > MAX_USER_DEFINED_BYTES) {
  138. err = -EINVAL;
  139. goto error_gpadl;
  140. }
  141. if (userdatalen)
  142. memcpy(open_msg->userdata, userdata, userdatalen);
  143. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  144. list_add_tail(&open_info->msglistentry,
  145. &vmbus_connection.chn_msg_list);
  146. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  147. ret = vmbus_post_msg(open_msg,
  148. sizeof(struct vmbus_channel_open_channel));
  149. if (ret != 0) {
  150. err = ret;
  151. goto error1;
  152. }
  153. wait_for_completion(&open_info->waitevent);
  154. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  155. list_del(&open_info->msglistentry);
  156. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  157. if (open_info->response.open_result.status) {
  158. err = -EAGAIN;
  159. goto error_gpadl;
  160. }
  161. newchannel->state = CHANNEL_OPENED_STATE;
  162. kfree(open_info);
  163. return 0;
  164. error1:
  165. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  166. list_del(&open_info->msglistentry);
  167. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  168. error_gpadl:
  169. vmbus_teardown_gpadl(newchannel, newchannel->ringbuffer_gpadlhandle);
  170. error0:
  171. free_pages((unsigned long)out,
  172. get_order(send_ringbuffer_size + recv_ringbuffer_size));
  173. kfree(open_info);
  174. newchannel->state = CHANNEL_OPEN_STATE;
  175. return err;
  176. }
  177. EXPORT_SYMBOL_GPL(vmbus_open);
  178. /*
  179. * create_gpadl_header - Creates a gpadl for the specified buffer
  180. */
  181. static int create_gpadl_header(void *kbuffer, u32 size,
  182. struct vmbus_channel_msginfo **msginfo,
  183. u32 *messagecount)
  184. {
  185. int i;
  186. int pagecount;
  187. struct vmbus_channel_gpadl_header *gpadl_header;
  188. struct vmbus_channel_gpadl_body *gpadl_body;
  189. struct vmbus_channel_msginfo *msgheader;
  190. struct vmbus_channel_msginfo *msgbody = NULL;
  191. u32 msgsize;
  192. int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
  193. pagecount = size >> PAGE_SHIFT;
  194. /* do we need a gpadl body msg */
  195. pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
  196. sizeof(struct vmbus_channel_gpadl_header) -
  197. sizeof(struct gpa_range);
  198. pfncount = pfnsize / sizeof(u64);
  199. if (pagecount > pfncount) {
  200. /* we need a gpadl body */
  201. /* fill in the header */
  202. msgsize = sizeof(struct vmbus_channel_msginfo) +
  203. sizeof(struct vmbus_channel_gpadl_header) +
  204. sizeof(struct gpa_range) + pfncount * sizeof(u64);
  205. msgheader = kzalloc(msgsize, GFP_KERNEL);
  206. if (!msgheader)
  207. goto nomem;
  208. INIT_LIST_HEAD(&msgheader->submsglist);
  209. msgheader->msgsize = msgsize;
  210. gpadl_header = (struct vmbus_channel_gpadl_header *)
  211. msgheader->msg;
  212. gpadl_header->rangecount = 1;
  213. gpadl_header->range_buflen = sizeof(struct gpa_range) +
  214. pagecount * sizeof(u64);
  215. gpadl_header->range[0].byte_offset = 0;
  216. gpadl_header->range[0].byte_count = size;
  217. for (i = 0; i < pfncount; i++)
  218. gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys(
  219. kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT;
  220. *msginfo = msgheader;
  221. *messagecount = 1;
  222. pfnsum = pfncount;
  223. pfnleft = pagecount - pfncount;
  224. /* how many pfns can we fit */
  225. pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
  226. sizeof(struct vmbus_channel_gpadl_body);
  227. pfncount = pfnsize / sizeof(u64);
  228. /* fill in the body */
  229. while (pfnleft) {
  230. if (pfnleft > pfncount)
  231. pfncurr = pfncount;
  232. else
  233. pfncurr = pfnleft;
  234. msgsize = sizeof(struct vmbus_channel_msginfo) +
  235. sizeof(struct vmbus_channel_gpadl_body) +
  236. pfncurr * sizeof(u64);
  237. msgbody = kzalloc(msgsize, GFP_KERNEL);
  238. if (!msgbody) {
  239. struct vmbus_channel_msginfo *pos = NULL;
  240. struct vmbus_channel_msginfo *tmp = NULL;
  241. /*
  242. * Free up all the allocated messages.
  243. */
  244. list_for_each_entry_safe(pos, tmp,
  245. &msgheader->submsglist,
  246. msglistentry) {
  247. list_del(&pos->msglistentry);
  248. kfree(pos);
  249. }
  250. goto nomem;
  251. }
  252. msgbody->msgsize = msgsize;
  253. (*messagecount)++;
  254. gpadl_body =
  255. (struct vmbus_channel_gpadl_body *)msgbody->msg;
  256. /*
  257. * Gpadl is u32 and we are using a pointer which could
  258. * be 64-bit
  259. * This is governed by the guest/host protocol and
  260. * so the hypervisor gurantees that this is ok.
  261. */
  262. for (i = 0; i < pfncurr; i++)
  263. gpadl_body->pfn[i] = slow_virt_to_phys(
  264. kbuffer + PAGE_SIZE * (pfnsum + i)) >>
  265. PAGE_SHIFT;
  266. /* add to msg header */
  267. list_add_tail(&msgbody->msglistentry,
  268. &msgheader->submsglist);
  269. pfnsum += pfncurr;
  270. pfnleft -= pfncurr;
  271. }
  272. } else {
  273. /* everything fits in a header */
  274. msgsize = sizeof(struct vmbus_channel_msginfo) +
  275. sizeof(struct vmbus_channel_gpadl_header) +
  276. sizeof(struct gpa_range) + pagecount * sizeof(u64);
  277. msgheader = kzalloc(msgsize, GFP_KERNEL);
  278. if (msgheader == NULL)
  279. goto nomem;
  280. msgheader->msgsize = msgsize;
  281. gpadl_header = (struct vmbus_channel_gpadl_header *)
  282. msgheader->msg;
  283. gpadl_header->rangecount = 1;
  284. gpadl_header->range_buflen = sizeof(struct gpa_range) +
  285. pagecount * sizeof(u64);
  286. gpadl_header->range[0].byte_offset = 0;
  287. gpadl_header->range[0].byte_count = size;
  288. for (i = 0; i < pagecount; i++)
  289. gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys(
  290. kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT;
  291. *msginfo = msgheader;
  292. *messagecount = 1;
  293. }
  294. return 0;
  295. nomem:
  296. kfree(msgheader);
  297. kfree(msgbody);
  298. return -ENOMEM;
  299. }
  300. /*
  301. * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
  302. *
  303. * @channel: a channel
  304. * @kbuffer: from kmalloc or vmalloc
  305. * @size: page-size multiple
  306. * @gpadl_handle: some funky thing
  307. */
  308. int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
  309. u32 size, u32 *gpadl_handle)
  310. {
  311. struct vmbus_channel_gpadl_header *gpadlmsg;
  312. struct vmbus_channel_gpadl_body *gpadl_body;
  313. struct vmbus_channel_msginfo *msginfo = NULL;
  314. struct vmbus_channel_msginfo *submsginfo, *tmp;
  315. u32 msgcount;
  316. struct list_head *curr;
  317. u32 next_gpadl_handle;
  318. unsigned long flags;
  319. int ret = 0;
  320. next_gpadl_handle =
  321. (atomic_inc_return(&vmbus_connection.next_gpadl_handle) - 1);
  322. ret = create_gpadl_header(kbuffer, size, &msginfo, &msgcount);
  323. if (ret)
  324. return ret;
  325. init_completion(&msginfo->waitevent);
  326. gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
  327. gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
  328. gpadlmsg->child_relid = channel->offermsg.child_relid;
  329. gpadlmsg->gpadl = next_gpadl_handle;
  330. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  331. list_add_tail(&msginfo->msglistentry,
  332. &vmbus_connection.chn_msg_list);
  333. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  334. ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
  335. sizeof(*msginfo));
  336. if (ret != 0)
  337. goto cleanup;
  338. if (msgcount > 1) {
  339. list_for_each(curr, &msginfo->submsglist) {
  340. submsginfo = (struct vmbus_channel_msginfo *)curr;
  341. gpadl_body =
  342. (struct vmbus_channel_gpadl_body *)submsginfo->msg;
  343. gpadl_body->header.msgtype =
  344. CHANNELMSG_GPADL_BODY;
  345. gpadl_body->gpadl = next_gpadl_handle;
  346. ret = vmbus_post_msg(gpadl_body,
  347. submsginfo->msgsize -
  348. sizeof(*submsginfo));
  349. if (ret != 0)
  350. goto cleanup;
  351. }
  352. }
  353. wait_for_completion(&msginfo->waitevent);
  354. /* At this point, we received the gpadl created msg */
  355. *gpadl_handle = gpadlmsg->gpadl;
  356. cleanup:
  357. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  358. list_del(&msginfo->msglistentry);
  359. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  360. if (msgcount > 1) {
  361. list_for_each_entry_safe(submsginfo, tmp, &msginfo->submsglist,
  362. msglistentry) {
  363. kfree(submsginfo);
  364. }
  365. }
  366. kfree(msginfo);
  367. return ret;
  368. }
  369. EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
  370. /*
  371. * vmbus_teardown_gpadl -Teardown the specified GPADL handle
  372. */
  373. int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
  374. {
  375. struct vmbus_channel_gpadl_teardown *msg;
  376. struct vmbus_channel_msginfo *info;
  377. unsigned long flags;
  378. int ret;
  379. info = kmalloc(sizeof(*info) +
  380. sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
  381. if (!info)
  382. return -ENOMEM;
  383. init_completion(&info->waitevent);
  384. msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
  385. msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
  386. msg->child_relid = channel->offermsg.child_relid;
  387. msg->gpadl = gpadl_handle;
  388. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  389. list_add_tail(&info->msglistentry,
  390. &vmbus_connection.chn_msg_list);
  391. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  392. ret = vmbus_post_msg(msg,
  393. sizeof(struct vmbus_channel_gpadl_teardown));
  394. if (ret)
  395. goto post_msg_err;
  396. wait_for_completion(&info->waitevent);
  397. post_msg_err:
  398. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  399. list_del(&info->msglistentry);
  400. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  401. kfree(info);
  402. return ret;
  403. }
  404. EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
  405. static void reset_channel_cb(void *arg)
  406. {
  407. struct vmbus_channel *channel = arg;
  408. channel->onchannel_callback = NULL;
  409. }
  410. static int vmbus_close_internal(struct vmbus_channel *channel)
  411. {
  412. struct vmbus_channel_close_channel *msg;
  413. struct tasklet_struct *tasklet;
  414. int ret;
  415. /*
  416. * process_chn_event(), running in the tasklet, can race
  417. * with vmbus_close_internal() in the case of SMP guest, e.g., when
  418. * the former is accessing channel->inbound.ring_buffer, the latter
  419. * could be freeing the ring_buffer pages.
  420. *
  421. * To resolve the race, we can serialize them by disabling the
  422. * tasklet when the latter is running here.
  423. */
  424. tasklet = hv_context.event_dpc[channel->target_cpu];
  425. tasklet_disable(tasklet);
  426. channel->state = CHANNEL_OPEN_STATE;
  427. channel->sc_creation_callback = NULL;
  428. /* Stop callback and cancel the timer asap */
  429. if (channel->target_cpu != get_cpu()) {
  430. put_cpu();
  431. smp_call_function_single(channel->target_cpu, reset_channel_cb,
  432. channel, true);
  433. } else {
  434. reset_channel_cb(channel);
  435. put_cpu();
  436. }
  437. /* Send a closing message */
  438. msg = &channel->close_msg.msg;
  439. msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
  440. msg->child_relid = channel->offermsg.child_relid;
  441. ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel));
  442. if (ret) {
  443. pr_err("Close failed: close post msg return is %d\n", ret);
  444. /*
  445. * If we failed to post the close msg,
  446. * it is perhaps better to leak memory.
  447. */
  448. goto out;
  449. }
  450. /* Tear down the gpadl for the channel's ring buffer */
  451. if (channel->ringbuffer_gpadlhandle) {
  452. ret = vmbus_teardown_gpadl(channel,
  453. channel->ringbuffer_gpadlhandle);
  454. if (ret) {
  455. pr_err("Close failed: teardown gpadl return %d\n", ret);
  456. /*
  457. * If we failed to teardown gpadl,
  458. * it is perhaps better to leak memory.
  459. */
  460. goto out;
  461. }
  462. }
  463. /* Cleanup the ring buffers for this channel */
  464. hv_ringbuffer_cleanup(&channel->outbound);
  465. hv_ringbuffer_cleanup(&channel->inbound);
  466. free_pages((unsigned long)channel->ringbuffer_pages,
  467. get_order(channel->ringbuffer_pagecount * PAGE_SIZE));
  468. out:
  469. tasklet_enable(tasklet);
  470. return ret;
  471. }
  472. /*
  473. * vmbus_close - Close the specified channel
  474. */
  475. void vmbus_close(struct vmbus_channel *channel)
  476. {
  477. struct list_head *cur, *tmp;
  478. struct vmbus_channel *cur_channel;
  479. if (channel->primary_channel != NULL) {
  480. /*
  481. * We will only close sub-channels when
  482. * the primary is closed.
  483. */
  484. return;
  485. }
  486. /*
  487. * Close all the sub-channels first and then close the
  488. * primary channel.
  489. */
  490. list_for_each_safe(cur, tmp, &channel->sc_list) {
  491. cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
  492. if (cur_channel->state != CHANNEL_OPENED_STATE)
  493. continue;
  494. vmbus_close_internal(cur_channel);
  495. }
  496. /*
  497. * Now close the primary.
  498. */
  499. vmbus_close_internal(channel);
  500. }
  501. EXPORT_SYMBOL_GPL(vmbus_close);
  502. int vmbus_sendpacket_ctl(struct vmbus_channel *channel, void *buffer,
  503. u32 bufferlen, u64 requestid,
  504. enum vmbus_packet_type type, u32 flags, bool kick_q)
  505. {
  506. struct vmpacket_descriptor desc;
  507. u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
  508. u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
  509. struct kvec bufferlist[3];
  510. u64 aligned_data = 0;
  511. int ret;
  512. bool signal = false;
  513. int num_vecs = ((bufferlen != 0) ? 3 : 1);
  514. /* Setup the descriptor */
  515. desc.type = type; /* VmbusPacketTypeDataInBand; */
  516. desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
  517. /* in 8-bytes granularity */
  518. desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
  519. desc.len8 = (u16)(packetlen_aligned >> 3);
  520. desc.trans_id = requestid;
  521. bufferlist[0].iov_base = &desc;
  522. bufferlist[0].iov_len = sizeof(struct vmpacket_descriptor);
  523. bufferlist[1].iov_base = buffer;
  524. bufferlist[1].iov_len = bufferlen;
  525. bufferlist[2].iov_base = &aligned_data;
  526. bufferlist[2].iov_len = (packetlen_aligned - packetlen);
  527. ret = hv_ringbuffer_write(&channel->outbound, bufferlist, num_vecs,
  528. &signal);
  529. /*
  530. * Signalling the host is conditional on many factors:
  531. * 1. The ring state changed from being empty to non-empty.
  532. * This is tracked by the variable "signal".
  533. * 2. The variable kick_q tracks if more data will be placed
  534. * on the ring. We will not signal if more data is
  535. * to be placed.
  536. *
  537. * Based on the channel signal state, we will decide
  538. * which signaling policy will be applied.
  539. *
  540. * If we cannot write to the ring-buffer; signal the host
  541. * even if we may not have written anything. This is a rare
  542. * enough condition that it should not matter.
  543. */
  544. if (channel->signal_policy)
  545. signal = true;
  546. else
  547. kick_q = true;
  548. if (((ret == 0) && kick_q && signal) || (ret))
  549. vmbus_setevent(channel);
  550. return ret;
  551. }
  552. EXPORT_SYMBOL(vmbus_sendpacket_ctl);
  553. /**
  554. * vmbus_sendpacket() - Send the specified buffer on the given channel
  555. * @channel: Pointer to vmbus_channel structure.
  556. * @buffer: Pointer to the buffer you want to receive the data into.
  557. * @bufferlen: Maximum size of what the the buffer will hold
  558. * @requestid: Identifier of the request
  559. * @type: Type of packet that is being send e.g. negotiate, time
  560. * packet etc.
  561. *
  562. * Sends data in @buffer directly to hyper-v via the vmbus
  563. * This will send the data unparsed to hyper-v.
  564. *
  565. * Mainly used by Hyper-V drivers.
  566. */
  567. int vmbus_sendpacket(struct vmbus_channel *channel, void *buffer,
  568. u32 bufferlen, u64 requestid,
  569. enum vmbus_packet_type type, u32 flags)
  570. {
  571. return vmbus_sendpacket_ctl(channel, buffer, bufferlen, requestid,
  572. type, flags, true);
  573. }
  574. EXPORT_SYMBOL(vmbus_sendpacket);
  575. /*
  576. * vmbus_sendpacket_pagebuffer_ctl - Send a range of single-page buffer
  577. * packets using a GPADL Direct packet type. This interface allows you
  578. * to control notifying the host. This will be useful for sending
  579. * batched data. Also the sender can control the send flags
  580. * explicitly.
  581. */
  582. int vmbus_sendpacket_pagebuffer_ctl(struct vmbus_channel *channel,
  583. struct hv_page_buffer pagebuffers[],
  584. u32 pagecount, void *buffer, u32 bufferlen,
  585. u64 requestid,
  586. u32 flags,
  587. bool kick_q)
  588. {
  589. int ret;
  590. int i;
  591. struct vmbus_channel_packet_page_buffer desc;
  592. u32 descsize;
  593. u32 packetlen;
  594. u32 packetlen_aligned;
  595. struct kvec bufferlist[3];
  596. u64 aligned_data = 0;
  597. bool signal = false;
  598. if (pagecount > MAX_PAGE_BUFFER_COUNT)
  599. return -EINVAL;
  600. /*
  601. * Adjust the size down since vmbus_channel_packet_page_buffer is the
  602. * largest size we support
  603. */
  604. descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
  605. ((MAX_PAGE_BUFFER_COUNT - pagecount) *
  606. sizeof(struct hv_page_buffer));
  607. packetlen = descsize + bufferlen;
  608. packetlen_aligned = ALIGN(packetlen, sizeof(u64));
  609. /* Setup the descriptor */
  610. desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
  611. desc.flags = flags;
  612. desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
  613. desc.length8 = (u16)(packetlen_aligned >> 3);
  614. desc.transactionid = requestid;
  615. desc.rangecount = pagecount;
  616. for (i = 0; i < pagecount; i++) {
  617. desc.range[i].len = pagebuffers[i].len;
  618. desc.range[i].offset = pagebuffers[i].offset;
  619. desc.range[i].pfn = pagebuffers[i].pfn;
  620. }
  621. bufferlist[0].iov_base = &desc;
  622. bufferlist[0].iov_len = descsize;
  623. bufferlist[1].iov_base = buffer;
  624. bufferlist[1].iov_len = bufferlen;
  625. bufferlist[2].iov_base = &aligned_data;
  626. bufferlist[2].iov_len = (packetlen_aligned - packetlen);
  627. ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal);
  628. /*
  629. * Signalling the host is conditional on many factors:
  630. * 1. The ring state changed from being empty to non-empty.
  631. * This is tracked by the variable "signal".
  632. * 2. The variable kick_q tracks if more data will be placed
  633. * on the ring. We will not signal if more data is
  634. * to be placed.
  635. *
  636. * Based on the channel signal state, we will decide
  637. * which signaling policy will be applied.
  638. *
  639. * If we cannot write to the ring-buffer; signal the host
  640. * even if we may not have written anything. This is a rare
  641. * enough condition that it should not matter.
  642. */
  643. if (channel->signal_policy)
  644. signal = true;
  645. else
  646. kick_q = true;
  647. if (((ret == 0) && kick_q && signal) || (ret))
  648. vmbus_setevent(channel);
  649. return ret;
  650. }
  651. EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer_ctl);
  652. /*
  653. * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
  654. * packets using a GPADL Direct packet type.
  655. */
  656. int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
  657. struct hv_page_buffer pagebuffers[],
  658. u32 pagecount, void *buffer, u32 bufferlen,
  659. u64 requestid)
  660. {
  661. u32 flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
  662. return vmbus_sendpacket_pagebuffer_ctl(channel, pagebuffers, pagecount,
  663. buffer, bufferlen, requestid,
  664. flags, true);
  665. }
  666. EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
  667. /*
  668. * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
  669. * using a GPADL Direct packet type.
  670. * The buffer includes the vmbus descriptor.
  671. */
  672. int vmbus_sendpacket_mpb_desc(struct vmbus_channel *channel,
  673. struct vmbus_packet_mpb_array *desc,
  674. u32 desc_size,
  675. void *buffer, u32 bufferlen, u64 requestid)
  676. {
  677. int ret;
  678. u32 packetlen;
  679. u32 packetlen_aligned;
  680. struct kvec bufferlist[3];
  681. u64 aligned_data = 0;
  682. bool signal = false;
  683. packetlen = desc_size + bufferlen;
  684. packetlen_aligned = ALIGN(packetlen, sizeof(u64));
  685. /* Setup the descriptor */
  686. desc->type = VM_PKT_DATA_USING_GPA_DIRECT;
  687. desc->flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
  688. desc->dataoffset8 = desc_size >> 3; /* in 8-bytes grandularity */
  689. desc->length8 = (u16)(packetlen_aligned >> 3);
  690. desc->transactionid = requestid;
  691. desc->rangecount = 1;
  692. bufferlist[0].iov_base = desc;
  693. bufferlist[0].iov_len = desc_size;
  694. bufferlist[1].iov_base = buffer;
  695. bufferlist[1].iov_len = bufferlen;
  696. bufferlist[2].iov_base = &aligned_data;
  697. bufferlist[2].iov_len = (packetlen_aligned - packetlen);
  698. ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal);
  699. if (ret == 0 && signal)
  700. vmbus_setevent(channel);
  701. return ret;
  702. }
  703. EXPORT_SYMBOL_GPL(vmbus_sendpacket_mpb_desc);
  704. /*
  705. * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
  706. * using a GPADL Direct packet type.
  707. */
  708. int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
  709. struct hv_multipage_buffer *multi_pagebuffer,
  710. void *buffer, u32 bufferlen, u64 requestid)
  711. {
  712. int ret;
  713. struct vmbus_channel_packet_multipage_buffer desc;
  714. u32 descsize;
  715. u32 packetlen;
  716. u32 packetlen_aligned;
  717. struct kvec bufferlist[3];
  718. u64 aligned_data = 0;
  719. bool signal = false;
  720. u32 pfncount = NUM_PAGES_SPANNED(multi_pagebuffer->offset,
  721. multi_pagebuffer->len);
  722. if (pfncount > MAX_MULTIPAGE_BUFFER_COUNT)
  723. return -EINVAL;
  724. /*
  725. * Adjust the size down since vmbus_channel_packet_multipage_buffer is
  726. * the largest size we support
  727. */
  728. descsize = sizeof(struct vmbus_channel_packet_multipage_buffer) -
  729. ((MAX_MULTIPAGE_BUFFER_COUNT - pfncount) *
  730. sizeof(u64));
  731. packetlen = descsize + bufferlen;
  732. packetlen_aligned = ALIGN(packetlen, sizeof(u64));
  733. /* Setup the descriptor */
  734. desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
  735. desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
  736. desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
  737. desc.length8 = (u16)(packetlen_aligned >> 3);
  738. desc.transactionid = requestid;
  739. desc.rangecount = 1;
  740. desc.range.len = multi_pagebuffer->len;
  741. desc.range.offset = multi_pagebuffer->offset;
  742. memcpy(desc.range.pfn_array, multi_pagebuffer->pfn_array,
  743. pfncount * sizeof(u64));
  744. bufferlist[0].iov_base = &desc;
  745. bufferlist[0].iov_len = descsize;
  746. bufferlist[1].iov_base = buffer;
  747. bufferlist[1].iov_len = bufferlen;
  748. bufferlist[2].iov_base = &aligned_data;
  749. bufferlist[2].iov_len = (packetlen_aligned - packetlen);
  750. ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal);
  751. if (ret == 0 && signal)
  752. vmbus_setevent(channel);
  753. return ret;
  754. }
  755. EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer);
  756. /**
  757. * vmbus_recvpacket() - Retrieve the user packet on the specified channel
  758. * @channel: Pointer to vmbus_channel structure.
  759. * @buffer: Pointer to the buffer you want to receive the data into.
  760. * @bufferlen: Maximum size of what the the buffer will hold
  761. * @buffer_actual_len: The actual size of the data after it was received
  762. * @requestid: Identifier of the request
  763. *
  764. * Receives directly from the hyper-v vmbus and puts the data it received
  765. * into Buffer. This will receive the data unparsed from hyper-v.
  766. *
  767. * Mainly used by Hyper-V drivers.
  768. */
  769. int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
  770. u32 bufferlen, u32 *buffer_actual_len, u64 *requestid)
  771. {
  772. struct vmpacket_descriptor desc;
  773. u32 packetlen;
  774. u32 userlen;
  775. int ret;
  776. bool signal = false;
  777. *buffer_actual_len = 0;
  778. *requestid = 0;
  779. ret = hv_ringbuffer_peek(&channel->inbound, &desc,
  780. sizeof(struct vmpacket_descriptor));
  781. if (ret != 0)
  782. return 0;
  783. packetlen = desc.len8 << 3;
  784. userlen = packetlen - (desc.offset8 << 3);
  785. *buffer_actual_len = userlen;
  786. if (userlen > bufferlen) {
  787. pr_err("Buffer too small - got %d needs %d\n",
  788. bufferlen, userlen);
  789. return -ETOOSMALL;
  790. }
  791. *requestid = desc.trans_id;
  792. /* Copy over the packet to the user buffer */
  793. ret = hv_ringbuffer_read(&channel->inbound, buffer, userlen,
  794. (desc.offset8 << 3), &signal);
  795. if (signal)
  796. vmbus_setevent(channel);
  797. return 0;
  798. }
  799. EXPORT_SYMBOL(vmbus_recvpacket);
  800. /*
  801. * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
  802. */
  803. int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
  804. u32 bufferlen, u32 *buffer_actual_len,
  805. u64 *requestid)
  806. {
  807. struct vmpacket_descriptor desc;
  808. u32 packetlen;
  809. int ret;
  810. bool signal = false;
  811. *buffer_actual_len = 0;
  812. *requestid = 0;
  813. ret = hv_ringbuffer_peek(&channel->inbound, &desc,
  814. sizeof(struct vmpacket_descriptor));
  815. if (ret != 0)
  816. return 0;
  817. packetlen = desc.len8 << 3;
  818. *buffer_actual_len = packetlen;
  819. if (packetlen > bufferlen)
  820. return -ENOBUFS;
  821. *requestid = desc.trans_id;
  822. /* Copy over the entire packet to the user buffer */
  823. ret = hv_ringbuffer_read(&channel->inbound, buffer, packetlen, 0,
  824. &signal);
  825. if (signal)
  826. vmbus_setevent(channel);
  827. return ret;
  828. }
  829. EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);