channel.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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 "hyperv_vmbus.h"
  30. #define NUM_PAGES_SPANNED(addr, len) \
  31. ((PAGE_ALIGN(addr + len) >> PAGE_SHIFT) - (addr >> PAGE_SHIFT))
  32. /* Internal routines */
  33. static int create_gpadl_header(
  34. void *kbuffer, /* must be phys and virt contiguous */
  35. u32 size, /* page-size multiple */
  36. struct vmbus_channel_msginfo **msginfo,
  37. u32 *messagecount);
  38. static void vmbus_setevent(struct vmbus_channel *channel);
  39. /*
  40. * vmbus_setevent- Trigger an event notification on the specified
  41. * channel.
  42. */
  43. static void vmbus_setevent(struct vmbus_channel *channel)
  44. {
  45. struct hv_monitor_page *monitorpage;
  46. if (channel->offermsg.monitor_allocated) {
  47. /* Each u32 represents 32 channels */
  48. sync_set_bit(channel->offermsg.child_relid & 31,
  49. (unsigned long *) vmbus_connection.send_int_page +
  50. (channel->offermsg.child_relid >> 5));
  51. monitorpage = vmbus_connection.monitor_pages;
  52. monitorpage++; /* Get the child to parent monitor page */
  53. sync_set_bit(channel->monitor_bit,
  54. (unsigned long *)&monitorpage->trigger_group
  55. [channel->monitor_grp].pending);
  56. } else {
  57. vmbus_set_event(channel->offermsg.child_relid);
  58. }
  59. }
  60. /*
  61. * vmbus_get_debug_info -Retrieve various channel debug info
  62. */
  63. void vmbus_get_debug_info(struct vmbus_channel *channel,
  64. struct vmbus_channel_debug_info *debuginfo)
  65. {
  66. struct hv_monitor_page *monitorpage;
  67. u8 monitor_group = (u8)channel->offermsg.monitorid / 32;
  68. u8 monitor_offset = (u8)channel->offermsg.monitorid % 32;
  69. debuginfo->relid = channel->offermsg.child_relid;
  70. debuginfo->state = channel->state;
  71. memcpy(&debuginfo->interfacetype,
  72. &channel->offermsg.offer.if_type, sizeof(uuid_le));
  73. memcpy(&debuginfo->interface_instance,
  74. &channel->offermsg.offer.if_instance,
  75. sizeof(uuid_le));
  76. monitorpage = (struct hv_monitor_page *)vmbus_connection.monitor_pages;
  77. debuginfo->monitorid = channel->offermsg.monitorid;
  78. debuginfo->servermonitor_pending =
  79. monitorpage->trigger_group[monitor_group].pending;
  80. debuginfo->servermonitor_latency =
  81. monitorpage->latency[monitor_group][monitor_offset];
  82. debuginfo->servermonitor_connectionid =
  83. monitorpage->parameter[monitor_group]
  84. [monitor_offset].connectionid.u.id;
  85. monitorpage++;
  86. debuginfo->clientmonitor_pending =
  87. monitorpage->trigger_group[monitor_group].pending;
  88. debuginfo->clientmonitor_latency =
  89. monitorpage->latency[monitor_group][monitor_offset];
  90. debuginfo->clientmonitor_connectionid =
  91. monitorpage->parameter[monitor_group]
  92. [monitor_offset].connectionid.u.id;
  93. hv_ringbuffer_get_debuginfo(&channel->inbound, &debuginfo->inbound);
  94. hv_ringbuffer_get_debuginfo(&channel->outbound, &debuginfo->outbound);
  95. }
  96. /*
  97. * vmbus_open - Open the specified channel.
  98. */
  99. int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size,
  100. u32 recv_ringbuffer_size, void *userdata, u32 userdatalen,
  101. void (*onchannelcallback)(void *context), void *context)
  102. {
  103. struct vmbus_channel_open_channel *open_msg;
  104. struct vmbus_channel_msginfo *open_info = NULL;
  105. void *in, *out;
  106. unsigned long flags;
  107. int ret, t, err = 0;
  108. newchannel->onchannel_callback = onchannelcallback;
  109. newchannel->channel_callback_context = context;
  110. /* Allocate the ring buffer */
  111. out = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO,
  112. get_order(send_ringbuffer_size + recv_ringbuffer_size));
  113. if (!out)
  114. return -ENOMEM;
  115. in = (void *)((unsigned long)out + send_ringbuffer_size);
  116. newchannel->ringbuffer_pages = out;
  117. newchannel->ringbuffer_pagecount = (send_ringbuffer_size +
  118. recv_ringbuffer_size) >> PAGE_SHIFT;
  119. ret = hv_ringbuffer_init(
  120. &newchannel->outbound, out, send_ringbuffer_size);
  121. if (ret != 0) {
  122. err = ret;
  123. goto error0;
  124. }
  125. ret = hv_ringbuffer_init(
  126. &newchannel->inbound, in, recv_ringbuffer_size);
  127. if (ret != 0) {
  128. err = ret;
  129. goto error0;
  130. }
  131. /* Establish the gpadl for the ring buffer */
  132. newchannel->ringbuffer_gpadlhandle = 0;
  133. ret = vmbus_establish_gpadl(newchannel,
  134. newchannel->outbound.ring_buffer,
  135. send_ringbuffer_size +
  136. recv_ringbuffer_size,
  137. &newchannel->ringbuffer_gpadlhandle);
  138. if (ret != 0) {
  139. err = ret;
  140. goto error0;
  141. }
  142. /* Create and init the channel open message */
  143. open_info = kmalloc(sizeof(*open_info) +
  144. sizeof(struct vmbus_channel_open_channel),
  145. GFP_KERNEL);
  146. if (!open_info) {
  147. err = -ENOMEM;
  148. goto error_gpadl;
  149. }
  150. init_completion(&open_info->waitevent);
  151. open_msg = (struct vmbus_channel_open_channel *)open_info->msg;
  152. open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL;
  153. open_msg->openid = newchannel->offermsg.child_relid;
  154. open_msg->child_relid = newchannel->offermsg.child_relid;
  155. open_msg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle;
  156. open_msg->downstream_ringbuffer_pageoffset = send_ringbuffer_size >>
  157. PAGE_SHIFT;
  158. open_msg->server_contextarea_gpadlhandle = 0;
  159. if (userdatalen > MAX_USER_DEFINED_BYTES) {
  160. err = -EINVAL;
  161. goto error_gpadl;
  162. }
  163. if (userdatalen)
  164. memcpy(open_msg->userdata, userdata, userdatalen);
  165. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  166. list_add_tail(&open_info->msglistentry,
  167. &vmbus_connection.chn_msg_list);
  168. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  169. ret = vmbus_post_msg(open_msg,
  170. sizeof(struct vmbus_channel_open_channel));
  171. if (ret != 0) {
  172. err = ret;
  173. goto error1;
  174. }
  175. t = wait_for_completion_timeout(&open_info->waitevent, 5*HZ);
  176. if (t == 0) {
  177. err = -ETIMEDOUT;
  178. goto error1;
  179. }
  180. if (open_info->response.open_result.status)
  181. err = open_info->response.open_result.status;
  182. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  183. list_del(&open_info->msglistentry);
  184. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  185. kfree(open_info);
  186. return err;
  187. error1:
  188. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  189. list_del(&open_info->msglistentry);
  190. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  191. error_gpadl:
  192. vmbus_teardown_gpadl(newchannel, newchannel->ringbuffer_gpadlhandle);
  193. error0:
  194. free_pages((unsigned long)out,
  195. get_order(send_ringbuffer_size + recv_ringbuffer_size));
  196. kfree(open_info);
  197. return err;
  198. }
  199. EXPORT_SYMBOL_GPL(vmbus_open);
  200. /*
  201. * create_gpadl_header - Creates a gpadl for the specified buffer
  202. */
  203. static int create_gpadl_header(void *kbuffer, u32 size,
  204. struct vmbus_channel_msginfo **msginfo,
  205. u32 *messagecount)
  206. {
  207. int i;
  208. int pagecount;
  209. unsigned long long pfn;
  210. struct vmbus_channel_gpadl_header *gpadl_header;
  211. struct vmbus_channel_gpadl_body *gpadl_body;
  212. struct vmbus_channel_msginfo *msgheader;
  213. struct vmbus_channel_msginfo *msgbody = NULL;
  214. u32 msgsize;
  215. int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
  216. pagecount = size >> PAGE_SHIFT;
  217. pfn = virt_to_phys(kbuffer) >> PAGE_SHIFT;
  218. /* do we need a gpadl body msg */
  219. pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
  220. sizeof(struct vmbus_channel_gpadl_header) -
  221. sizeof(struct gpa_range);
  222. pfncount = pfnsize / sizeof(u64);
  223. if (pagecount > pfncount) {
  224. /* we need a gpadl body */
  225. /* fill in the header */
  226. msgsize = sizeof(struct vmbus_channel_msginfo) +
  227. sizeof(struct vmbus_channel_gpadl_header) +
  228. sizeof(struct gpa_range) + pfncount * sizeof(u64);
  229. msgheader = kzalloc(msgsize, GFP_KERNEL);
  230. if (!msgheader)
  231. goto nomem;
  232. INIT_LIST_HEAD(&msgheader->submsglist);
  233. msgheader->msgsize = msgsize;
  234. gpadl_header = (struct vmbus_channel_gpadl_header *)
  235. msgheader->msg;
  236. gpadl_header->rangecount = 1;
  237. gpadl_header->range_buflen = sizeof(struct gpa_range) +
  238. pagecount * sizeof(u64);
  239. gpadl_header->range[0].byte_offset = 0;
  240. gpadl_header->range[0].byte_count = size;
  241. for (i = 0; i < pfncount; i++)
  242. gpadl_header->range[0].pfn_array[i] = pfn+i;
  243. *msginfo = msgheader;
  244. *messagecount = 1;
  245. pfnsum = pfncount;
  246. pfnleft = pagecount - pfncount;
  247. /* how many pfns can we fit */
  248. pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
  249. sizeof(struct vmbus_channel_gpadl_body);
  250. pfncount = pfnsize / sizeof(u64);
  251. /* fill in the body */
  252. while (pfnleft) {
  253. if (pfnleft > pfncount)
  254. pfncurr = pfncount;
  255. else
  256. pfncurr = pfnleft;
  257. msgsize = sizeof(struct vmbus_channel_msginfo) +
  258. sizeof(struct vmbus_channel_gpadl_body) +
  259. pfncurr * sizeof(u64);
  260. msgbody = kzalloc(msgsize, GFP_KERNEL);
  261. if (!msgbody) {
  262. struct vmbus_channel_msginfo *pos = NULL;
  263. struct vmbus_channel_msginfo *tmp = NULL;
  264. /*
  265. * Free up all the allocated messages.
  266. */
  267. list_for_each_entry_safe(pos, tmp,
  268. &msgheader->submsglist,
  269. msglistentry) {
  270. list_del(&pos->msglistentry);
  271. kfree(pos);
  272. }
  273. goto nomem;
  274. }
  275. msgbody->msgsize = msgsize;
  276. (*messagecount)++;
  277. gpadl_body =
  278. (struct vmbus_channel_gpadl_body *)msgbody->msg;
  279. /*
  280. * Gpadl is u32 and we are using a pointer which could
  281. * be 64-bit
  282. * This is governed by the guest/host protocol and
  283. * so the hypervisor gurantees that this is ok.
  284. */
  285. for (i = 0; i < pfncurr; i++)
  286. gpadl_body->pfn[i] = pfn + pfnsum + i;
  287. /* add to msg header */
  288. list_add_tail(&msgbody->msglistentry,
  289. &msgheader->submsglist);
  290. pfnsum += pfncurr;
  291. pfnleft -= pfncurr;
  292. }
  293. } else {
  294. /* everything fits in a header */
  295. msgsize = sizeof(struct vmbus_channel_msginfo) +
  296. sizeof(struct vmbus_channel_gpadl_header) +
  297. sizeof(struct gpa_range) + pagecount * sizeof(u64);
  298. msgheader = kzalloc(msgsize, GFP_KERNEL);
  299. if (msgheader == NULL)
  300. goto nomem;
  301. msgheader->msgsize = msgsize;
  302. gpadl_header = (struct vmbus_channel_gpadl_header *)
  303. msgheader->msg;
  304. gpadl_header->rangecount = 1;
  305. gpadl_header->range_buflen = sizeof(struct gpa_range) +
  306. pagecount * sizeof(u64);
  307. gpadl_header->range[0].byte_offset = 0;
  308. gpadl_header->range[0].byte_count = size;
  309. for (i = 0; i < pagecount; i++)
  310. gpadl_header->range[0].pfn_array[i] = pfn+i;
  311. *msginfo = msgheader;
  312. *messagecount = 1;
  313. }
  314. return 0;
  315. nomem:
  316. kfree(msgheader);
  317. kfree(msgbody);
  318. return -ENOMEM;
  319. }
  320. /*
  321. * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
  322. *
  323. * @channel: a channel
  324. * @kbuffer: from kmalloc
  325. * @size: page-size multiple
  326. * @gpadl_handle: some funky thing
  327. */
  328. int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
  329. u32 size, u32 *gpadl_handle)
  330. {
  331. struct vmbus_channel_gpadl_header *gpadlmsg;
  332. struct vmbus_channel_gpadl_body *gpadl_body;
  333. struct vmbus_channel_msginfo *msginfo = NULL;
  334. struct vmbus_channel_msginfo *submsginfo;
  335. u32 msgcount;
  336. struct list_head *curr;
  337. u32 next_gpadl_handle;
  338. unsigned long flags;
  339. int ret = 0;
  340. next_gpadl_handle = atomic_read(&vmbus_connection.next_gpadl_handle);
  341. atomic_inc(&vmbus_connection.next_gpadl_handle);
  342. ret = create_gpadl_header(kbuffer, size, &msginfo, &msgcount);
  343. if (ret)
  344. return ret;
  345. init_completion(&msginfo->waitevent);
  346. gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
  347. gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
  348. gpadlmsg->child_relid = channel->offermsg.child_relid;
  349. gpadlmsg->gpadl = next_gpadl_handle;
  350. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  351. list_add_tail(&msginfo->msglistentry,
  352. &vmbus_connection.chn_msg_list);
  353. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  354. ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
  355. sizeof(*msginfo));
  356. if (ret != 0)
  357. goto cleanup;
  358. if (msgcount > 1) {
  359. list_for_each(curr, &msginfo->submsglist) {
  360. submsginfo = (struct vmbus_channel_msginfo *)curr;
  361. gpadl_body =
  362. (struct vmbus_channel_gpadl_body *)submsginfo->msg;
  363. gpadl_body->header.msgtype =
  364. CHANNELMSG_GPADL_BODY;
  365. gpadl_body->gpadl = next_gpadl_handle;
  366. ret = vmbus_post_msg(gpadl_body,
  367. submsginfo->msgsize -
  368. sizeof(*submsginfo));
  369. if (ret != 0)
  370. goto cleanup;
  371. }
  372. }
  373. wait_for_completion(&msginfo->waitevent);
  374. /* At this point, we received the gpadl created msg */
  375. *gpadl_handle = gpadlmsg->gpadl;
  376. cleanup:
  377. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  378. list_del(&msginfo->msglistentry);
  379. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  380. kfree(msginfo);
  381. return ret;
  382. }
  383. EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
  384. /*
  385. * vmbus_teardown_gpadl -Teardown the specified GPADL handle
  386. */
  387. int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
  388. {
  389. struct vmbus_channel_gpadl_teardown *msg;
  390. struct vmbus_channel_msginfo *info;
  391. unsigned long flags;
  392. int ret;
  393. info = kmalloc(sizeof(*info) +
  394. sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
  395. if (!info)
  396. return -ENOMEM;
  397. init_completion(&info->waitevent);
  398. msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
  399. msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
  400. msg->child_relid = channel->offermsg.child_relid;
  401. msg->gpadl = gpadl_handle;
  402. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  403. list_add_tail(&info->msglistentry,
  404. &vmbus_connection.chn_msg_list);
  405. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  406. ret = vmbus_post_msg(msg,
  407. sizeof(struct vmbus_channel_gpadl_teardown));
  408. if (ret)
  409. goto post_msg_err;
  410. wait_for_completion(&info->waitevent);
  411. post_msg_err:
  412. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  413. list_del(&info->msglistentry);
  414. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  415. kfree(info);
  416. return ret;
  417. }
  418. EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
  419. /*
  420. * vmbus_close - Close the specified channel
  421. */
  422. void vmbus_close(struct vmbus_channel *channel)
  423. {
  424. struct vmbus_channel_close_channel *msg;
  425. int ret;
  426. unsigned long flags;
  427. /* Stop callback and cancel the timer asap */
  428. spin_lock_irqsave(&channel->inbound_lock, flags);
  429. channel->onchannel_callback = NULL;
  430. spin_unlock_irqrestore(&channel->inbound_lock, flags);
  431. /* Send a closing message */
  432. msg = &channel->close_msg.msg;
  433. msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
  434. msg->child_relid = channel->offermsg.child_relid;
  435. ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel));
  436. if (ret) {
  437. pr_err("Close failed: close post msg return is %d\n", ret);
  438. /*
  439. * If we failed to post the close msg,
  440. * it is perhaps better to leak memory.
  441. */
  442. return;
  443. }
  444. /* Tear down the gpadl for the channel's ring buffer */
  445. if (channel->ringbuffer_gpadlhandle) {
  446. ret = vmbus_teardown_gpadl(channel,
  447. channel->ringbuffer_gpadlhandle);
  448. if (ret) {
  449. pr_err("Close failed: teardown gpadl return %d\n", ret);
  450. /*
  451. * If we failed to teardown gpadl,
  452. * it is perhaps better to leak memory.
  453. */
  454. return;
  455. }
  456. }
  457. /* Cleanup the ring buffers for this channel */
  458. hv_ringbuffer_cleanup(&channel->outbound);
  459. hv_ringbuffer_cleanup(&channel->inbound);
  460. free_pages((unsigned long)channel->ringbuffer_pages,
  461. get_order(channel->ringbuffer_pagecount * PAGE_SIZE));
  462. }
  463. EXPORT_SYMBOL_GPL(vmbus_close);
  464. /**
  465. * vmbus_sendpacket() - Send the specified buffer on the given channel
  466. * @channel: Pointer to vmbus_channel structure.
  467. * @buffer: Pointer to the buffer you want to receive the data into.
  468. * @bufferlen: Maximum size of what the the buffer will hold
  469. * @requestid: Identifier of the request
  470. * @type: Type of packet that is being send e.g. negotiate, time
  471. * packet etc.
  472. *
  473. * Sends data in @buffer directly to hyper-v via the vmbus
  474. * This will send the data unparsed to hyper-v.
  475. *
  476. * Mainly used by Hyper-V drivers.
  477. */
  478. int vmbus_sendpacket(struct vmbus_channel *channel, const void *buffer,
  479. u32 bufferlen, u64 requestid,
  480. enum vmbus_packet_type type, u32 flags)
  481. {
  482. struct vmpacket_descriptor desc;
  483. u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
  484. u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
  485. struct scatterlist bufferlist[3];
  486. u64 aligned_data = 0;
  487. int ret;
  488. /* Setup the descriptor */
  489. desc.type = type; /* VmbusPacketTypeDataInBand; */
  490. desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
  491. /* in 8-bytes granularity */
  492. desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
  493. desc.len8 = (u16)(packetlen_aligned >> 3);
  494. desc.trans_id = requestid;
  495. sg_init_table(bufferlist, 3);
  496. sg_set_buf(&bufferlist[0], &desc, sizeof(struct vmpacket_descriptor));
  497. sg_set_buf(&bufferlist[1], buffer, bufferlen);
  498. sg_set_buf(&bufferlist[2], &aligned_data,
  499. packetlen_aligned - packetlen);
  500. ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3);
  501. if (ret == 0 && !hv_get_ringbuffer_interrupt_mask(&channel->outbound))
  502. vmbus_setevent(channel);
  503. return ret;
  504. }
  505. EXPORT_SYMBOL(vmbus_sendpacket);
  506. /*
  507. * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
  508. * packets using a GPADL Direct packet type.
  509. */
  510. int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
  511. struct hv_page_buffer pagebuffers[],
  512. u32 pagecount, void *buffer, u32 bufferlen,
  513. u64 requestid)
  514. {
  515. int ret;
  516. int i;
  517. struct vmbus_channel_packet_page_buffer desc;
  518. u32 descsize;
  519. u32 packetlen;
  520. u32 packetlen_aligned;
  521. struct scatterlist bufferlist[3];
  522. u64 aligned_data = 0;
  523. if (pagecount > MAX_PAGE_BUFFER_COUNT)
  524. return -EINVAL;
  525. /*
  526. * Adjust the size down since vmbus_channel_packet_page_buffer is the
  527. * largest size we support
  528. */
  529. descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
  530. ((MAX_PAGE_BUFFER_COUNT - pagecount) *
  531. sizeof(struct hv_page_buffer));
  532. packetlen = descsize + bufferlen;
  533. packetlen_aligned = ALIGN(packetlen, sizeof(u64));
  534. /* Setup the descriptor */
  535. desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
  536. desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
  537. desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
  538. desc.length8 = (u16)(packetlen_aligned >> 3);
  539. desc.transactionid = requestid;
  540. desc.rangecount = pagecount;
  541. for (i = 0; i < pagecount; i++) {
  542. desc.range[i].len = pagebuffers[i].len;
  543. desc.range[i].offset = pagebuffers[i].offset;
  544. desc.range[i].pfn = pagebuffers[i].pfn;
  545. }
  546. sg_init_table(bufferlist, 3);
  547. sg_set_buf(&bufferlist[0], &desc, descsize);
  548. sg_set_buf(&bufferlist[1], buffer, bufferlen);
  549. sg_set_buf(&bufferlist[2], &aligned_data,
  550. packetlen_aligned - packetlen);
  551. ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3);
  552. if (ret == 0 && !hv_get_ringbuffer_interrupt_mask(&channel->outbound))
  553. vmbus_setevent(channel);
  554. return ret;
  555. }
  556. EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
  557. /*
  558. * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
  559. * using a GPADL Direct packet type.
  560. */
  561. int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
  562. struct hv_multipage_buffer *multi_pagebuffer,
  563. void *buffer, u32 bufferlen, u64 requestid)
  564. {
  565. int ret;
  566. struct vmbus_channel_packet_multipage_buffer desc;
  567. u32 descsize;
  568. u32 packetlen;
  569. u32 packetlen_aligned;
  570. struct scatterlist bufferlist[3];
  571. u64 aligned_data = 0;
  572. u32 pfncount = NUM_PAGES_SPANNED(multi_pagebuffer->offset,
  573. multi_pagebuffer->len);
  574. if ((pfncount < 0) || (pfncount > MAX_MULTIPAGE_BUFFER_COUNT))
  575. return -EINVAL;
  576. /*
  577. * Adjust the size down since vmbus_channel_packet_multipage_buffer is
  578. * the largest size we support
  579. */
  580. descsize = sizeof(struct vmbus_channel_packet_multipage_buffer) -
  581. ((MAX_MULTIPAGE_BUFFER_COUNT - pfncount) *
  582. sizeof(u64));
  583. packetlen = descsize + bufferlen;
  584. packetlen_aligned = ALIGN(packetlen, sizeof(u64));
  585. /* Setup the descriptor */
  586. desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
  587. desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
  588. desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
  589. desc.length8 = (u16)(packetlen_aligned >> 3);
  590. desc.transactionid = requestid;
  591. desc.rangecount = 1;
  592. desc.range.len = multi_pagebuffer->len;
  593. desc.range.offset = multi_pagebuffer->offset;
  594. memcpy(desc.range.pfn_array, multi_pagebuffer->pfn_array,
  595. pfncount * sizeof(u64));
  596. sg_init_table(bufferlist, 3);
  597. sg_set_buf(&bufferlist[0], &desc, descsize);
  598. sg_set_buf(&bufferlist[1], buffer, bufferlen);
  599. sg_set_buf(&bufferlist[2], &aligned_data,
  600. packetlen_aligned - packetlen);
  601. ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3);
  602. if (ret == 0 && !hv_get_ringbuffer_interrupt_mask(&channel->outbound))
  603. vmbus_setevent(channel);
  604. return ret;
  605. }
  606. EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer);
  607. /**
  608. * vmbus_recvpacket() - Retrieve the user packet on the specified channel
  609. * @channel: Pointer to vmbus_channel structure.
  610. * @buffer: Pointer to the buffer you want to receive the data into.
  611. * @bufferlen: Maximum size of what the the buffer will hold
  612. * @buffer_actual_len: The actual size of the data after it was received
  613. * @requestid: Identifier of the request
  614. *
  615. * Receives directly from the hyper-v vmbus and puts the data it received
  616. * into Buffer. This will receive the data unparsed from hyper-v.
  617. *
  618. * Mainly used by Hyper-V drivers.
  619. */
  620. int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
  621. u32 bufferlen, u32 *buffer_actual_len, u64 *requestid)
  622. {
  623. struct vmpacket_descriptor desc;
  624. u32 packetlen;
  625. u32 userlen;
  626. int ret;
  627. *buffer_actual_len = 0;
  628. *requestid = 0;
  629. ret = hv_ringbuffer_peek(&channel->inbound, &desc,
  630. sizeof(struct vmpacket_descriptor));
  631. if (ret != 0)
  632. return 0;
  633. packetlen = desc.len8 << 3;
  634. userlen = packetlen - (desc.offset8 << 3);
  635. *buffer_actual_len = userlen;
  636. if (userlen > bufferlen) {
  637. pr_err("Buffer too small - got %d needs %d\n",
  638. bufferlen, userlen);
  639. return -ETOOSMALL;
  640. }
  641. *requestid = desc.trans_id;
  642. /* Copy over the packet to the user buffer */
  643. ret = hv_ringbuffer_read(&channel->inbound, buffer, userlen,
  644. (desc.offset8 << 3));
  645. return 0;
  646. }
  647. EXPORT_SYMBOL(vmbus_recvpacket);
  648. /*
  649. * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
  650. */
  651. int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
  652. u32 bufferlen, u32 *buffer_actual_len,
  653. u64 *requestid)
  654. {
  655. struct vmpacket_descriptor desc;
  656. u32 packetlen;
  657. u32 userlen;
  658. int ret;
  659. *buffer_actual_len = 0;
  660. *requestid = 0;
  661. ret = hv_ringbuffer_peek(&channel->inbound, &desc,
  662. sizeof(struct vmpacket_descriptor));
  663. if (ret != 0)
  664. return 0;
  665. packetlen = desc.len8 << 3;
  666. userlen = packetlen - (desc.offset8 << 3);
  667. *buffer_actual_len = packetlen;
  668. if (packetlen > bufferlen) {
  669. pr_err("Buffer too small - needed %d bytes but "
  670. "got space for only %d bytes\n",
  671. packetlen, bufferlen);
  672. return -ENOBUFS;
  673. }
  674. *requestid = desc.trans_id;
  675. /* Copy over the entire packet to the user buffer */
  676. ret = hv_ringbuffer_read(&channel->inbound, buffer, packetlen, 0);
  677. return 0;
  678. }
  679. EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);