channel.c 26 KB

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