channel.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  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 error0;
  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 error0;
  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. goto error1;
  173. t = wait_for_completion_timeout(&open_info->waitevent, 5*HZ);
  174. if (t == 0) {
  175. err = -ETIMEDOUT;
  176. goto error1;
  177. }
  178. if (open_info->response.open_result.status)
  179. err = open_info->response.open_result.status;
  180. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  181. list_del(&open_info->msglistentry);
  182. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  183. kfree(open_info);
  184. return err;
  185. error1:
  186. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  187. list_del(&open_info->msglistentry);
  188. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  189. error0:
  190. free_pages((unsigned long)out,
  191. get_order(send_ringbuffer_size + recv_ringbuffer_size));
  192. kfree(open_info);
  193. return err;
  194. }
  195. EXPORT_SYMBOL_GPL(vmbus_open);
  196. /*
  197. * create_gpadl_header - Creates a gpadl for the specified buffer
  198. */
  199. static int create_gpadl_header(void *kbuffer, u32 size,
  200. struct vmbus_channel_msginfo **msginfo,
  201. u32 *messagecount)
  202. {
  203. int i;
  204. int pagecount;
  205. unsigned long long pfn;
  206. struct vmbus_channel_gpadl_header *gpadl_header;
  207. struct vmbus_channel_gpadl_body *gpadl_body;
  208. struct vmbus_channel_msginfo *msgheader;
  209. struct vmbus_channel_msginfo *msgbody = NULL;
  210. u32 msgsize;
  211. int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
  212. pagecount = size >> PAGE_SHIFT;
  213. pfn = virt_to_phys(kbuffer) >> PAGE_SHIFT;
  214. /* do we need a gpadl body msg */
  215. pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
  216. sizeof(struct vmbus_channel_gpadl_header) -
  217. sizeof(struct gpa_range);
  218. pfncount = pfnsize / sizeof(u64);
  219. if (pagecount > pfncount) {
  220. /* we need a gpadl body */
  221. /* fill in the header */
  222. msgsize = sizeof(struct vmbus_channel_msginfo) +
  223. sizeof(struct vmbus_channel_gpadl_header) +
  224. sizeof(struct gpa_range) + pfncount * sizeof(u64);
  225. msgheader = kzalloc(msgsize, GFP_KERNEL);
  226. if (!msgheader)
  227. goto nomem;
  228. INIT_LIST_HEAD(&msgheader->submsglist);
  229. msgheader->msgsize = msgsize;
  230. gpadl_header = (struct vmbus_channel_gpadl_header *)
  231. msgheader->msg;
  232. gpadl_header->rangecount = 1;
  233. gpadl_header->range_buflen = sizeof(struct gpa_range) +
  234. pagecount * sizeof(u64);
  235. gpadl_header->range[0].byte_offset = 0;
  236. gpadl_header->range[0].byte_count = size;
  237. for (i = 0; i < pfncount; i++)
  238. gpadl_header->range[0].pfn_array[i] = pfn+i;
  239. *msginfo = msgheader;
  240. *messagecount = 1;
  241. pfnsum = pfncount;
  242. pfnleft = pagecount - pfncount;
  243. /* how many pfns can we fit */
  244. pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
  245. sizeof(struct vmbus_channel_gpadl_body);
  246. pfncount = pfnsize / sizeof(u64);
  247. /* fill in the body */
  248. while (pfnleft) {
  249. if (pfnleft > pfncount)
  250. pfncurr = pfncount;
  251. else
  252. pfncurr = pfnleft;
  253. msgsize = sizeof(struct vmbus_channel_msginfo) +
  254. sizeof(struct vmbus_channel_gpadl_body) +
  255. pfncurr * sizeof(u64);
  256. msgbody = kzalloc(msgsize, GFP_KERNEL);
  257. if (!msgbody) {
  258. struct vmbus_channel_msginfo *pos = NULL;
  259. struct vmbus_channel_msginfo *tmp = NULL;
  260. /*
  261. * Free up all the allocated messages.
  262. */
  263. list_for_each_entry_safe(pos, tmp,
  264. &msgheader->submsglist,
  265. msglistentry) {
  266. list_del(&pos->msglistentry);
  267. kfree(pos);
  268. }
  269. goto nomem;
  270. }
  271. msgbody->msgsize = msgsize;
  272. (*messagecount)++;
  273. gpadl_body =
  274. (struct vmbus_channel_gpadl_body *)msgbody->msg;
  275. /*
  276. * Gpadl is u32 and we are using a pointer which could
  277. * be 64-bit
  278. * This is governed by the guest/host protocol and
  279. * so the hypervisor gurantees that this is ok.
  280. */
  281. for (i = 0; i < pfncurr; i++)
  282. gpadl_body->pfn[i] = pfn + pfnsum + i;
  283. /* add to msg header */
  284. list_add_tail(&msgbody->msglistentry,
  285. &msgheader->submsglist);
  286. pfnsum += pfncurr;
  287. pfnleft -= pfncurr;
  288. }
  289. } else {
  290. /* everything fits in a header */
  291. msgsize = sizeof(struct vmbus_channel_msginfo) +
  292. sizeof(struct vmbus_channel_gpadl_header) +
  293. sizeof(struct gpa_range) + pagecount * sizeof(u64);
  294. msgheader = kzalloc(msgsize, GFP_KERNEL);
  295. if (msgheader == NULL)
  296. goto nomem;
  297. msgheader->msgsize = msgsize;
  298. gpadl_header = (struct vmbus_channel_gpadl_header *)
  299. msgheader->msg;
  300. gpadl_header->rangecount = 1;
  301. gpadl_header->range_buflen = sizeof(struct gpa_range) +
  302. pagecount * sizeof(u64);
  303. gpadl_header->range[0].byte_offset = 0;
  304. gpadl_header->range[0].byte_count = size;
  305. for (i = 0; i < pagecount; i++)
  306. gpadl_header->range[0].pfn_array[i] = pfn+i;
  307. *msginfo = msgheader;
  308. *messagecount = 1;
  309. }
  310. return 0;
  311. nomem:
  312. kfree(msgheader);
  313. kfree(msgbody);
  314. return -ENOMEM;
  315. }
  316. /*
  317. * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
  318. *
  319. * @channel: a channel
  320. * @kbuffer: from kmalloc
  321. * @size: page-size multiple
  322. * @gpadl_handle: some funky thing
  323. */
  324. int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
  325. u32 size, u32 *gpadl_handle)
  326. {
  327. struct vmbus_channel_gpadl_header *gpadlmsg;
  328. struct vmbus_channel_gpadl_body *gpadl_body;
  329. struct vmbus_channel_msginfo *msginfo = NULL;
  330. struct vmbus_channel_msginfo *submsginfo;
  331. u32 msgcount;
  332. struct list_head *curr;
  333. u32 next_gpadl_handle;
  334. unsigned long flags;
  335. int ret = 0;
  336. int t;
  337. next_gpadl_handle = atomic_read(&vmbus_connection.next_gpadl_handle);
  338. atomic_inc(&vmbus_connection.next_gpadl_handle);
  339. ret = create_gpadl_header(kbuffer, size, &msginfo, &msgcount);
  340. if (ret)
  341. return ret;
  342. init_completion(&msginfo->waitevent);
  343. gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
  344. gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
  345. gpadlmsg->child_relid = channel->offermsg.child_relid;
  346. gpadlmsg->gpadl = next_gpadl_handle;
  347. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  348. list_add_tail(&msginfo->msglistentry,
  349. &vmbus_connection.chn_msg_list);
  350. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  351. ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
  352. sizeof(*msginfo));
  353. if (ret != 0)
  354. goto cleanup;
  355. if (msgcount > 1) {
  356. list_for_each(curr, &msginfo->submsglist) {
  357. submsginfo = (struct vmbus_channel_msginfo *)curr;
  358. gpadl_body =
  359. (struct vmbus_channel_gpadl_body *)submsginfo->msg;
  360. gpadl_body->header.msgtype =
  361. CHANNELMSG_GPADL_BODY;
  362. gpadl_body->gpadl = next_gpadl_handle;
  363. ret = vmbus_post_msg(gpadl_body,
  364. submsginfo->msgsize -
  365. sizeof(*submsginfo));
  366. if (ret != 0)
  367. goto cleanup;
  368. }
  369. }
  370. t = wait_for_completion_timeout(&msginfo->waitevent, 5*HZ);
  371. BUG_ON(t == 0);
  372. /* At this point, we received the gpadl created msg */
  373. *gpadl_handle = gpadlmsg->gpadl;
  374. cleanup:
  375. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  376. list_del(&msginfo->msglistentry);
  377. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  378. kfree(msginfo);
  379. return ret;
  380. }
  381. EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
  382. /*
  383. * vmbus_teardown_gpadl -Teardown the specified GPADL handle
  384. */
  385. int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
  386. {
  387. struct vmbus_channel_gpadl_teardown *msg;
  388. struct vmbus_channel_msginfo *info;
  389. unsigned long flags;
  390. int ret, t;
  391. info = kmalloc(sizeof(*info) +
  392. sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
  393. if (!info)
  394. return -ENOMEM;
  395. init_completion(&info->waitevent);
  396. msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
  397. msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
  398. msg->child_relid = channel->offermsg.child_relid;
  399. msg->gpadl = gpadl_handle;
  400. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  401. list_add_tail(&info->msglistentry,
  402. &vmbus_connection.chn_msg_list);
  403. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  404. ret = vmbus_post_msg(msg,
  405. sizeof(struct vmbus_channel_gpadl_teardown));
  406. BUG_ON(ret != 0);
  407. t = wait_for_completion_timeout(&info->waitevent, 5*HZ);
  408. BUG_ON(t == 0);
  409. /* Received a torndown response */
  410. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  411. list_del(&info->msglistentry);
  412. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  413. kfree(info);
  414. return ret;
  415. }
  416. EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
  417. /*
  418. * vmbus_close - Close the specified channel
  419. */
  420. void vmbus_close(struct vmbus_channel *channel)
  421. {
  422. struct vmbus_channel_close_channel *msg;
  423. int ret;
  424. unsigned long flags;
  425. /* Stop callback and cancel the timer asap */
  426. spin_lock_irqsave(&channel->inbound_lock, flags);
  427. channel->onchannel_callback = NULL;
  428. spin_unlock_irqrestore(&channel->inbound_lock, flags);
  429. /* Send a closing message */
  430. msg = &channel->close_msg.msg;
  431. msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
  432. msg->child_relid = channel->offermsg.child_relid;
  433. ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel));
  434. BUG_ON(ret != 0);
  435. /* Tear down the gpadl for the channel's ring buffer */
  436. if (channel->ringbuffer_gpadlhandle)
  437. vmbus_teardown_gpadl(channel,
  438. channel->ringbuffer_gpadlhandle);
  439. /* Cleanup the ring buffers for this channel */
  440. hv_ringbuffer_cleanup(&channel->outbound);
  441. hv_ringbuffer_cleanup(&channel->inbound);
  442. free_pages((unsigned long)channel->ringbuffer_pages,
  443. get_order(channel->ringbuffer_pagecount * PAGE_SIZE));
  444. }
  445. EXPORT_SYMBOL_GPL(vmbus_close);
  446. /**
  447. * vmbus_sendpacket() - Send the specified buffer on the given channel
  448. * @channel: Pointer to vmbus_channel structure.
  449. * @buffer: Pointer to the buffer you want to receive the data into.
  450. * @bufferlen: Maximum size of what the the buffer will hold
  451. * @requestid: Identifier of the request
  452. * @type: Type of packet that is being send e.g. negotiate, time
  453. * packet etc.
  454. *
  455. * Sends data in @buffer directly to hyper-v via the vmbus
  456. * This will send the data unparsed to hyper-v.
  457. *
  458. * Mainly used by Hyper-V drivers.
  459. */
  460. int vmbus_sendpacket(struct vmbus_channel *channel, const void *buffer,
  461. u32 bufferlen, u64 requestid,
  462. enum vmbus_packet_type type, u32 flags)
  463. {
  464. struct vmpacket_descriptor desc;
  465. u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
  466. u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
  467. struct scatterlist bufferlist[3];
  468. u64 aligned_data = 0;
  469. int ret;
  470. /* Setup the descriptor */
  471. desc.type = type; /* VmbusPacketTypeDataInBand; */
  472. desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
  473. /* in 8-bytes granularity */
  474. desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
  475. desc.len8 = (u16)(packetlen_aligned >> 3);
  476. desc.trans_id = requestid;
  477. sg_init_table(bufferlist, 3);
  478. sg_set_buf(&bufferlist[0], &desc, sizeof(struct vmpacket_descriptor));
  479. sg_set_buf(&bufferlist[1], buffer, bufferlen);
  480. sg_set_buf(&bufferlist[2], &aligned_data,
  481. packetlen_aligned - packetlen);
  482. ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3);
  483. if (ret == 0 && !hv_get_ringbuffer_interrupt_mask(&channel->outbound))
  484. vmbus_setevent(channel);
  485. return ret;
  486. }
  487. EXPORT_SYMBOL(vmbus_sendpacket);
  488. /*
  489. * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
  490. * packets using a GPADL Direct packet type.
  491. */
  492. int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
  493. struct hv_page_buffer pagebuffers[],
  494. u32 pagecount, void *buffer, u32 bufferlen,
  495. u64 requestid)
  496. {
  497. int ret;
  498. int i;
  499. struct vmbus_channel_packet_page_buffer desc;
  500. u32 descsize;
  501. u32 packetlen;
  502. u32 packetlen_aligned;
  503. struct scatterlist bufferlist[3];
  504. u64 aligned_data = 0;
  505. if (pagecount > MAX_PAGE_BUFFER_COUNT)
  506. return -EINVAL;
  507. /*
  508. * Adjust the size down since vmbus_channel_packet_page_buffer is the
  509. * largest size we support
  510. */
  511. descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
  512. ((MAX_PAGE_BUFFER_COUNT - pagecount) *
  513. sizeof(struct hv_page_buffer));
  514. packetlen = descsize + bufferlen;
  515. packetlen_aligned = ALIGN(packetlen, sizeof(u64));
  516. /* Setup the descriptor */
  517. desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
  518. desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
  519. desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
  520. desc.length8 = (u16)(packetlen_aligned >> 3);
  521. desc.transactionid = requestid;
  522. desc.rangecount = pagecount;
  523. for (i = 0; i < pagecount; i++) {
  524. desc.range[i].len = pagebuffers[i].len;
  525. desc.range[i].offset = pagebuffers[i].offset;
  526. desc.range[i].pfn = pagebuffers[i].pfn;
  527. }
  528. sg_init_table(bufferlist, 3);
  529. sg_set_buf(&bufferlist[0], &desc, descsize);
  530. sg_set_buf(&bufferlist[1], buffer, bufferlen);
  531. sg_set_buf(&bufferlist[2], &aligned_data,
  532. packetlen_aligned - packetlen);
  533. ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3);
  534. if (ret == 0 && !hv_get_ringbuffer_interrupt_mask(&channel->outbound))
  535. vmbus_setevent(channel);
  536. return ret;
  537. }
  538. EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
  539. /*
  540. * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
  541. * using a GPADL Direct packet type.
  542. */
  543. int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
  544. struct hv_multipage_buffer *multi_pagebuffer,
  545. void *buffer, u32 bufferlen, u64 requestid)
  546. {
  547. int ret;
  548. struct vmbus_channel_packet_multipage_buffer desc;
  549. u32 descsize;
  550. u32 packetlen;
  551. u32 packetlen_aligned;
  552. struct scatterlist bufferlist[3];
  553. u64 aligned_data = 0;
  554. u32 pfncount = NUM_PAGES_SPANNED(multi_pagebuffer->offset,
  555. multi_pagebuffer->len);
  556. if ((pfncount < 0) || (pfncount > MAX_MULTIPAGE_BUFFER_COUNT))
  557. return -EINVAL;
  558. /*
  559. * Adjust the size down since vmbus_channel_packet_multipage_buffer is
  560. * the largest size we support
  561. */
  562. descsize = sizeof(struct vmbus_channel_packet_multipage_buffer) -
  563. ((MAX_MULTIPAGE_BUFFER_COUNT - pfncount) *
  564. sizeof(u64));
  565. packetlen = descsize + bufferlen;
  566. packetlen_aligned = ALIGN(packetlen, sizeof(u64));
  567. /* Setup the descriptor */
  568. desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
  569. desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
  570. desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
  571. desc.length8 = (u16)(packetlen_aligned >> 3);
  572. desc.transactionid = requestid;
  573. desc.rangecount = 1;
  574. desc.range.len = multi_pagebuffer->len;
  575. desc.range.offset = multi_pagebuffer->offset;
  576. memcpy(desc.range.pfn_array, multi_pagebuffer->pfn_array,
  577. pfncount * sizeof(u64));
  578. sg_init_table(bufferlist, 3);
  579. sg_set_buf(&bufferlist[0], &desc, descsize);
  580. sg_set_buf(&bufferlist[1], buffer, bufferlen);
  581. sg_set_buf(&bufferlist[2], &aligned_data,
  582. packetlen_aligned - packetlen);
  583. ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3);
  584. if (ret == 0 && !hv_get_ringbuffer_interrupt_mask(&channel->outbound))
  585. vmbus_setevent(channel);
  586. return ret;
  587. }
  588. EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer);
  589. /**
  590. * vmbus_recvpacket() - Retrieve the user packet on the specified channel
  591. * @channel: Pointer to vmbus_channel structure.
  592. * @buffer: Pointer to the buffer you want to receive the data into.
  593. * @bufferlen: Maximum size of what the the buffer will hold
  594. * @buffer_actual_len: The actual size of the data after it was received
  595. * @requestid: Identifier of the request
  596. *
  597. * Receives directly from the hyper-v vmbus and puts the data it received
  598. * into Buffer. This will receive the data unparsed from hyper-v.
  599. *
  600. * Mainly used by Hyper-V drivers.
  601. */
  602. int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
  603. u32 bufferlen, u32 *buffer_actual_len, u64 *requestid)
  604. {
  605. struct vmpacket_descriptor desc;
  606. u32 packetlen;
  607. u32 userlen;
  608. int ret;
  609. *buffer_actual_len = 0;
  610. *requestid = 0;
  611. ret = hv_ringbuffer_peek(&channel->inbound, &desc,
  612. sizeof(struct vmpacket_descriptor));
  613. if (ret != 0)
  614. return 0;
  615. packetlen = desc.len8 << 3;
  616. userlen = packetlen - (desc.offset8 << 3);
  617. *buffer_actual_len = userlen;
  618. if (userlen > bufferlen) {
  619. pr_err("Buffer too small - got %d needs %d\n",
  620. bufferlen, userlen);
  621. return -ETOOSMALL;
  622. }
  623. *requestid = desc.trans_id;
  624. /* Copy over the packet to the user buffer */
  625. ret = hv_ringbuffer_read(&channel->inbound, buffer, userlen,
  626. (desc.offset8 << 3));
  627. return 0;
  628. }
  629. EXPORT_SYMBOL(vmbus_recvpacket);
  630. /*
  631. * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
  632. */
  633. int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
  634. u32 bufferlen, u32 *buffer_actual_len,
  635. u64 *requestid)
  636. {
  637. struct vmpacket_descriptor desc;
  638. u32 packetlen;
  639. u32 userlen;
  640. int ret;
  641. *buffer_actual_len = 0;
  642. *requestid = 0;
  643. ret = hv_ringbuffer_peek(&channel->inbound, &desc,
  644. sizeof(struct vmpacket_descriptor));
  645. if (ret != 0)
  646. return 0;
  647. packetlen = desc.len8 << 3;
  648. userlen = packetlen - (desc.offset8 << 3);
  649. *buffer_actual_len = packetlen;
  650. if (packetlen > bufferlen) {
  651. pr_err("Buffer too small - needed %d bytes but "
  652. "got space for only %d bytes\n",
  653. packetlen, bufferlen);
  654. return -ENOBUFS;
  655. }
  656. *requestid = desc.trans_id;
  657. /* Copy over the entire packet to the user buffer */
  658. ret = hv_ringbuffer_read(&channel->inbound, buffer, packetlen, 0);
  659. return 0;
  660. }
  661. EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);