channel.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  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. void vmbus_setevent(struct vmbus_channel *channel)
  39. {
  40. struct hv_monitor_page *monitorpage;
  41. /*
  42. * For channels marked as in "low latency" mode
  43. * bypass the monitor page mechanism.
  44. */
  45. if ((channel->offermsg.monitor_allocated) &&
  46. (!channel->low_latency)) {
  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. /* Get the child to parent monitor page */
  52. monitorpage = vmbus_connection.monitor_pages[1];
  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);
  58. }
  59. }
  60. EXPORT_SYMBOL_GPL(vmbus_setevent);
  61. /*
  62. * vmbus_open - Open the specified channel.
  63. */
  64. int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size,
  65. u32 recv_ringbuffer_size, void *userdata, u32 userdatalen,
  66. void (*onchannelcallback)(void *context), void *context)
  67. {
  68. struct vmbus_channel_open_channel *open_msg;
  69. struct vmbus_channel_msginfo *open_info = NULL;
  70. unsigned long flags;
  71. int ret, err = 0;
  72. struct page *page;
  73. if (send_ringbuffer_size % PAGE_SIZE ||
  74. recv_ringbuffer_size % PAGE_SIZE)
  75. return -EINVAL;
  76. spin_lock_irqsave(&newchannel->lock, flags);
  77. if (newchannel->state == CHANNEL_OPEN_STATE) {
  78. newchannel->state = CHANNEL_OPENING_STATE;
  79. } else {
  80. spin_unlock_irqrestore(&newchannel->lock, flags);
  81. return -EINVAL;
  82. }
  83. spin_unlock_irqrestore(&newchannel->lock, flags);
  84. newchannel->onchannel_callback = onchannelcallback;
  85. newchannel->channel_callback_context = context;
  86. /* Allocate the ring buffer */
  87. page = alloc_pages_node(cpu_to_node(newchannel->target_cpu),
  88. GFP_KERNEL|__GFP_ZERO,
  89. get_order(send_ringbuffer_size +
  90. recv_ringbuffer_size));
  91. if (!page)
  92. page = alloc_pages(GFP_KERNEL|__GFP_ZERO,
  93. get_order(send_ringbuffer_size +
  94. recv_ringbuffer_size));
  95. if (!page) {
  96. err = -ENOMEM;
  97. goto error_set_chnstate;
  98. }
  99. newchannel->ringbuffer_pages = page_address(page);
  100. newchannel->ringbuffer_pagecount = (send_ringbuffer_size +
  101. recv_ringbuffer_size) >> PAGE_SHIFT;
  102. ret = hv_ringbuffer_init(&newchannel->outbound, page,
  103. send_ringbuffer_size >> PAGE_SHIFT);
  104. if (ret != 0) {
  105. err = ret;
  106. goto error_free_pages;
  107. }
  108. ret = hv_ringbuffer_init(&newchannel->inbound,
  109. &page[send_ringbuffer_size >> PAGE_SHIFT],
  110. recv_ringbuffer_size >> PAGE_SHIFT);
  111. if (ret != 0) {
  112. err = ret;
  113. goto error_free_pages;
  114. }
  115. /* Establish the gpadl for the ring buffer */
  116. newchannel->ringbuffer_gpadlhandle = 0;
  117. ret = vmbus_establish_gpadl(newchannel,
  118. page_address(page),
  119. send_ringbuffer_size +
  120. recv_ringbuffer_size,
  121. &newchannel->ringbuffer_gpadlhandle);
  122. if (ret != 0) {
  123. err = ret;
  124. goto error_free_pages;
  125. }
  126. /* Create and init the channel open message */
  127. open_info = kmalloc(sizeof(*open_info) +
  128. sizeof(struct vmbus_channel_open_channel),
  129. GFP_KERNEL);
  130. if (!open_info) {
  131. err = -ENOMEM;
  132. goto error_free_gpadl;
  133. }
  134. init_completion(&open_info->waitevent);
  135. open_info->waiting_channel = newchannel;
  136. open_msg = (struct vmbus_channel_open_channel *)open_info->msg;
  137. open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL;
  138. open_msg->openid = newchannel->offermsg.child_relid;
  139. open_msg->child_relid = newchannel->offermsg.child_relid;
  140. open_msg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle;
  141. open_msg->downstream_ringbuffer_pageoffset = send_ringbuffer_size >>
  142. PAGE_SHIFT;
  143. open_msg->target_vp = newchannel->target_vp;
  144. if (userdatalen > MAX_USER_DEFINED_BYTES) {
  145. err = -EINVAL;
  146. goto error_free_gpadl;
  147. }
  148. if (userdatalen)
  149. memcpy(open_msg->userdata, userdata, userdatalen);
  150. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  151. list_add_tail(&open_info->msglistentry,
  152. &vmbus_connection.chn_msg_list);
  153. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  154. ret = vmbus_post_msg(open_msg,
  155. sizeof(struct vmbus_channel_open_channel), true);
  156. if (ret != 0) {
  157. err = ret;
  158. goto error_clean_msglist;
  159. }
  160. wait_for_completion(&open_info->waitevent);
  161. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  162. list_del(&open_info->msglistentry);
  163. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  164. if (newchannel->rescind) {
  165. err = -ENODEV;
  166. goto error_free_gpadl;
  167. }
  168. if (open_info->response.open_result.status) {
  169. err = -EAGAIN;
  170. goto error_free_gpadl;
  171. }
  172. newchannel->state = CHANNEL_OPENED_STATE;
  173. kfree(open_info);
  174. return 0;
  175. error_clean_msglist:
  176. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  177. list_del(&open_info->msglistentry);
  178. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  179. error_free_gpadl:
  180. vmbus_teardown_gpadl(newchannel, newchannel->ringbuffer_gpadlhandle);
  181. kfree(open_info);
  182. error_free_pages:
  183. hv_ringbuffer_cleanup(&newchannel->outbound);
  184. hv_ringbuffer_cleanup(&newchannel->inbound);
  185. __free_pages(page,
  186. get_order(send_ringbuffer_size + recv_ringbuffer_size));
  187. error_set_chnstate:
  188. newchannel->state = CHANNEL_OPEN_STATE;
  189. return err;
  190. }
  191. EXPORT_SYMBOL_GPL(vmbus_open);
  192. /* Used for Hyper-V Socket: a guest client's connect() to the host */
  193. int vmbus_send_tl_connect_request(const uuid_le *shv_guest_servie_id,
  194. const uuid_le *shv_host_servie_id)
  195. {
  196. struct vmbus_channel_tl_connect_request conn_msg;
  197. memset(&conn_msg, 0, sizeof(conn_msg));
  198. conn_msg.header.msgtype = CHANNELMSG_TL_CONNECT_REQUEST;
  199. conn_msg.guest_endpoint_id = *shv_guest_servie_id;
  200. conn_msg.host_service_id = *shv_host_servie_id;
  201. return vmbus_post_msg(&conn_msg, sizeof(conn_msg), true);
  202. }
  203. EXPORT_SYMBOL_GPL(vmbus_send_tl_connect_request);
  204. /*
  205. * create_gpadl_header - Creates a gpadl for the specified buffer
  206. */
  207. static int create_gpadl_header(void *kbuffer, u32 size,
  208. struct vmbus_channel_msginfo **msginfo)
  209. {
  210. int i;
  211. int pagecount;
  212. struct vmbus_channel_gpadl_header *gpadl_header;
  213. struct vmbus_channel_gpadl_body *gpadl_body;
  214. struct vmbus_channel_msginfo *msgheader;
  215. struct vmbus_channel_msginfo *msgbody = NULL;
  216. u32 msgsize;
  217. int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
  218. pagecount = size >> PAGE_SHIFT;
  219. /* do we need a gpadl body msg */
  220. pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
  221. sizeof(struct vmbus_channel_gpadl_header) -
  222. sizeof(struct gpa_range);
  223. pfncount = pfnsize / sizeof(u64);
  224. if (pagecount > pfncount) {
  225. /* we need a gpadl body */
  226. /* fill in the header */
  227. msgsize = sizeof(struct vmbus_channel_msginfo) +
  228. sizeof(struct vmbus_channel_gpadl_header) +
  229. sizeof(struct gpa_range) + pfncount * sizeof(u64);
  230. msgheader = kzalloc(msgsize, GFP_KERNEL);
  231. if (!msgheader)
  232. goto nomem;
  233. INIT_LIST_HEAD(&msgheader->submsglist);
  234. msgheader->msgsize = msgsize;
  235. gpadl_header = (struct vmbus_channel_gpadl_header *)
  236. msgheader->msg;
  237. gpadl_header->rangecount = 1;
  238. gpadl_header->range_buflen = sizeof(struct gpa_range) +
  239. pagecount * sizeof(u64);
  240. gpadl_header->range[0].byte_offset = 0;
  241. gpadl_header->range[0].byte_count = size;
  242. for (i = 0; i < pfncount; i++)
  243. gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys(
  244. kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT;
  245. *msginfo = msgheader;
  246. pfnsum = pfncount;
  247. pfnleft = pagecount - pfncount;
  248. /* how many pfns can we fit */
  249. pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
  250. sizeof(struct vmbus_channel_gpadl_body);
  251. pfncount = pfnsize / sizeof(u64);
  252. /* fill in the body */
  253. while (pfnleft) {
  254. if (pfnleft > pfncount)
  255. pfncurr = pfncount;
  256. else
  257. pfncurr = pfnleft;
  258. msgsize = sizeof(struct vmbus_channel_msginfo) +
  259. sizeof(struct vmbus_channel_gpadl_body) +
  260. pfncurr * sizeof(u64);
  261. msgbody = kzalloc(msgsize, GFP_KERNEL);
  262. if (!msgbody) {
  263. struct vmbus_channel_msginfo *pos = NULL;
  264. struct vmbus_channel_msginfo *tmp = NULL;
  265. /*
  266. * Free up all the allocated messages.
  267. */
  268. list_for_each_entry_safe(pos, tmp,
  269. &msgheader->submsglist,
  270. msglistentry) {
  271. list_del(&pos->msglistentry);
  272. kfree(pos);
  273. }
  274. goto nomem;
  275. }
  276. msgbody->msgsize = msgsize;
  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] = slow_virt_to_phys(
  287. kbuffer + PAGE_SIZE * (pfnsum + i)) >>
  288. PAGE_SHIFT;
  289. /* add to msg header */
  290. list_add_tail(&msgbody->msglistentry,
  291. &msgheader->submsglist);
  292. pfnsum += pfncurr;
  293. pfnleft -= pfncurr;
  294. }
  295. } else {
  296. /* everything fits in a header */
  297. msgsize = sizeof(struct vmbus_channel_msginfo) +
  298. sizeof(struct vmbus_channel_gpadl_header) +
  299. sizeof(struct gpa_range) + pagecount * sizeof(u64);
  300. msgheader = kzalloc(msgsize, GFP_KERNEL);
  301. if (msgheader == NULL)
  302. goto nomem;
  303. INIT_LIST_HEAD(&msgheader->submsglist);
  304. msgheader->msgsize = msgsize;
  305. gpadl_header = (struct vmbus_channel_gpadl_header *)
  306. msgheader->msg;
  307. gpadl_header->rangecount = 1;
  308. gpadl_header->range_buflen = sizeof(struct gpa_range) +
  309. pagecount * sizeof(u64);
  310. gpadl_header->range[0].byte_offset = 0;
  311. gpadl_header->range[0].byte_count = size;
  312. for (i = 0; i < pagecount; i++)
  313. gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys(
  314. kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT;
  315. *msginfo = msgheader;
  316. }
  317. return 0;
  318. nomem:
  319. kfree(msgheader);
  320. kfree(msgbody);
  321. return -ENOMEM;
  322. }
  323. /*
  324. * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
  325. *
  326. * @channel: a channel
  327. * @kbuffer: from kmalloc or vmalloc
  328. * @size: page-size multiple
  329. * @gpadl_handle: some funky thing
  330. */
  331. int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
  332. u32 size, u32 *gpadl_handle)
  333. {
  334. struct vmbus_channel_gpadl_header *gpadlmsg;
  335. struct vmbus_channel_gpadl_body *gpadl_body;
  336. struct vmbus_channel_msginfo *msginfo = NULL;
  337. struct vmbus_channel_msginfo *submsginfo, *tmp;
  338. struct list_head *curr;
  339. u32 next_gpadl_handle;
  340. unsigned long flags;
  341. int ret = 0;
  342. next_gpadl_handle =
  343. (atomic_inc_return(&vmbus_connection.next_gpadl_handle) - 1);
  344. ret = create_gpadl_header(kbuffer, size, &msginfo);
  345. if (ret)
  346. return ret;
  347. init_completion(&msginfo->waitevent);
  348. msginfo->waiting_channel = channel;
  349. gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
  350. gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
  351. gpadlmsg->child_relid = channel->offermsg.child_relid;
  352. gpadlmsg->gpadl = next_gpadl_handle;
  353. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  354. list_add_tail(&msginfo->msglistentry,
  355. &vmbus_connection.chn_msg_list);
  356. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  357. ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
  358. sizeof(*msginfo), true);
  359. if (ret != 0)
  360. goto cleanup;
  361. list_for_each(curr, &msginfo->submsglist) {
  362. submsginfo = (struct vmbus_channel_msginfo *)curr;
  363. gpadl_body =
  364. (struct vmbus_channel_gpadl_body *)submsginfo->msg;
  365. gpadl_body->header.msgtype =
  366. CHANNELMSG_GPADL_BODY;
  367. gpadl_body->gpadl = next_gpadl_handle;
  368. ret = vmbus_post_msg(gpadl_body,
  369. submsginfo->msgsize - sizeof(*submsginfo),
  370. true);
  371. if (ret != 0)
  372. goto cleanup;
  373. }
  374. wait_for_completion(&msginfo->waitevent);
  375. if (channel->rescind) {
  376. ret = -ENODEV;
  377. goto cleanup;
  378. }
  379. /* At this point, we received the gpadl created msg */
  380. *gpadl_handle = gpadlmsg->gpadl;
  381. cleanup:
  382. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  383. list_del(&msginfo->msglistentry);
  384. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  385. list_for_each_entry_safe(submsginfo, tmp, &msginfo->submsglist,
  386. msglistentry) {
  387. kfree(submsginfo);
  388. }
  389. kfree(msginfo);
  390. return ret;
  391. }
  392. EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
  393. /*
  394. * vmbus_teardown_gpadl -Teardown the specified GPADL handle
  395. */
  396. int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
  397. {
  398. struct vmbus_channel_gpadl_teardown *msg;
  399. struct vmbus_channel_msginfo *info;
  400. unsigned long flags;
  401. int ret;
  402. info = kmalloc(sizeof(*info) +
  403. sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
  404. if (!info)
  405. return -ENOMEM;
  406. init_completion(&info->waitevent);
  407. info->waiting_channel = channel;
  408. msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
  409. msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
  410. msg->child_relid = channel->offermsg.child_relid;
  411. msg->gpadl = gpadl_handle;
  412. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  413. list_add_tail(&info->msglistentry,
  414. &vmbus_connection.chn_msg_list);
  415. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  416. ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_gpadl_teardown),
  417. true);
  418. if (ret)
  419. goto post_msg_err;
  420. wait_for_completion(&info->waitevent);
  421. post_msg_err:
  422. /*
  423. * If the channel has been rescinded;
  424. * we will be awakened by the rescind
  425. * handler; set the error code to zero so we don't leak memory.
  426. */
  427. if (channel->rescind)
  428. ret = 0;
  429. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  430. list_del(&info->msglistentry);
  431. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  432. kfree(info);
  433. return ret;
  434. }
  435. EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
  436. static void reset_channel_cb(void *arg)
  437. {
  438. struct vmbus_channel *channel = arg;
  439. channel->onchannel_callback = NULL;
  440. }
  441. static int vmbus_close_internal(struct vmbus_channel *channel)
  442. {
  443. struct vmbus_channel_close_channel *msg;
  444. int ret;
  445. /*
  446. * process_chn_event(), running in the tasklet, can race
  447. * with vmbus_close_internal() in the case of SMP guest, e.g., when
  448. * the former is accessing channel->inbound.ring_buffer, the latter
  449. * could be freeing the ring_buffer pages.
  450. *
  451. * To resolve the race, we can serialize them by disabling the
  452. * tasklet when the latter is running here.
  453. */
  454. hv_event_tasklet_disable(channel);
  455. /*
  456. * In case a device driver's probe() fails (e.g.,
  457. * util_probe() -> vmbus_open() returns -ENOMEM) and the device is
  458. * rescinded later (e.g., we dynamically disble an Integrated Service
  459. * in Hyper-V Manager), the driver's remove() invokes vmbus_close():
  460. * here we should skip most of the below cleanup work.
  461. */
  462. if (channel->state != CHANNEL_OPENED_STATE) {
  463. ret = -EINVAL;
  464. goto out;
  465. }
  466. channel->state = CHANNEL_OPEN_STATE;
  467. channel->sc_creation_callback = NULL;
  468. /* Stop callback and cancel the timer asap */
  469. if (channel->target_cpu != get_cpu()) {
  470. put_cpu();
  471. smp_call_function_single(channel->target_cpu, reset_channel_cb,
  472. channel, true);
  473. } else {
  474. reset_channel_cb(channel);
  475. put_cpu();
  476. }
  477. /* Send a closing message */
  478. msg = &channel->close_msg.msg;
  479. msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
  480. msg->child_relid = channel->offermsg.child_relid;
  481. ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel),
  482. true);
  483. if (ret) {
  484. pr_err("Close failed: close post msg return is %d\n", ret);
  485. /*
  486. * If we failed to post the close msg,
  487. * it is perhaps better to leak memory.
  488. */
  489. goto out;
  490. }
  491. /* Tear down the gpadl for the channel's ring buffer */
  492. if (channel->ringbuffer_gpadlhandle) {
  493. ret = vmbus_teardown_gpadl(channel,
  494. channel->ringbuffer_gpadlhandle);
  495. if (ret) {
  496. pr_err("Close failed: teardown gpadl return %d\n", ret);
  497. /*
  498. * If we failed to teardown gpadl,
  499. * it is perhaps better to leak memory.
  500. */
  501. goto out;
  502. }
  503. }
  504. /* Cleanup the ring buffers for this channel */
  505. hv_ringbuffer_cleanup(&channel->outbound);
  506. hv_ringbuffer_cleanup(&channel->inbound);
  507. free_pages((unsigned long)channel->ringbuffer_pages,
  508. get_order(channel->ringbuffer_pagecount * PAGE_SIZE));
  509. out:
  510. hv_event_tasklet_enable(channel);
  511. return ret;
  512. }
  513. /*
  514. * vmbus_close - Close the specified channel
  515. */
  516. void vmbus_close(struct vmbus_channel *channel)
  517. {
  518. struct list_head *cur, *tmp;
  519. struct vmbus_channel *cur_channel;
  520. if (channel->primary_channel != NULL) {
  521. /*
  522. * We will only close sub-channels when
  523. * the primary is closed.
  524. */
  525. return;
  526. }
  527. /*
  528. * Close all the sub-channels first and then close the
  529. * primary channel.
  530. */
  531. list_for_each_safe(cur, tmp, &channel->sc_list) {
  532. cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
  533. if (cur_channel->state != CHANNEL_OPENED_STATE)
  534. continue;
  535. vmbus_close_internal(cur_channel);
  536. }
  537. /*
  538. * Now close the primary.
  539. */
  540. vmbus_close_internal(channel);
  541. }
  542. EXPORT_SYMBOL_GPL(vmbus_close);
  543. int vmbus_sendpacket_ctl(struct vmbus_channel *channel, void *buffer,
  544. u32 bufferlen, u64 requestid,
  545. enum vmbus_packet_type type, u32 flags, bool kick_q)
  546. {
  547. struct vmpacket_descriptor desc;
  548. u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
  549. u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
  550. struct kvec bufferlist[3];
  551. u64 aligned_data = 0;
  552. bool lock = channel->acquire_ring_lock;
  553. int num_vecs = ((bufferlen != 0) ? 3 : 1);
  554. /* Setup the descriptor */
  555. desc.type = type; /* VmbusPacketTypeDataInBand; */
  556. desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
  557. /* in 8-bytes granularity */
  558. desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
  559. desc.len8 = (u16)(packetlen_aligned >> 3);
  560. desc.trans_id = requestid;
  561. bufferlist[0].iov_base = &desc;
  562. bufferlist[0].iov_len = sizeof(struct vmpacket_descriptor);
  563. bufferlist[1].iov_base = buffer;
  564. bufferlist[1].iov_len = bufferlen;
  565. bufferlist[2].iov_base = &aligned_data;
  566. bufferlist[2].iov_len = (packetlen_aligned - packetlen);
  567. return hv_ringbuffer_write(channel, bufferlist, num_vecs,
  568. lock, kick_q);
  569. }
  570. EXPORT_SYMBOL(vmbus_sendpacket_ctl);
  571. /**
  572. * vmbus_sendpacket() - Send the specified buffer on the given channel
  573. * @channel: Pointer to vmbus_channel structure.
  574. * @buffer: Pointer to the buffer you want to receive the data into.
  575. * @bufferlen: Maximum size of what the the buffer will hold
  576. * @requestid: Identifier of the request
  577. * @type: Type of packet that is being send e.g. negotiate, time
  578. * packet etc.
  579. *
  580. * Sends data in @buffer directly to hyper-v via the vmbus
  581. * This will send the data unparsed to hyper-v.
  582. *
  583. * Mainly used by Hyper-V drivers.
  584. */
  585. int vmbus_sendpacket(struct vmbus_channel *channel, void *buffer,
  586. u32 bufferlen, u64 requestid,
  587. enum vmbus_packet_type type, u32 flags)
  588. {
  589. return vmbus_sendpacket_ctl(channel, buffer, bufferlen, requestid,
  590. type, flags, true);
  591. }
  592. EXPORT_SYMBOL(vmbus_sendpacket);
  593. /*
  594. * vmbus_sendpacket_pagebuffer_ctl - Send a range of single-page buffer
  595. * packets using a GPADL Direct packet type. This interface allows you
  596. * to control notifying the host. This will be useful for sending
  597. * batched data. Also the sender can control the send flags
  598. * explicitly.
  599. */
  600. int vmbus_sendpacket_pagebuffer_ctl(struct vmbus_channel *channel,
  601. struct hv_page_buffer pagebuffers[],
  602. u32 pagecount, void *buffer, u32 bufferlen,
  603. u64 requestid,
  604. u32 flags,
  605. bool kick_q)
  606. {
  607. int i;
  608. struct vmbus_channel_packet_page_buffer desc;
  609. u32 descsize;
  610. u32 packetlen;
  611. u32 packetlen_aligned;
  612. struct kvec bufferlist[3];
  613. u64 aligned_data = 0;
  614. bool lock = channel->acquire_ring_lock;
  615. if (pagecount > MAX_PAGE_BUFFER_COUNT)
  616. return -EINVAL;
  617. /*
  618. * Adjust the size down since vmbus_channel_packet_page_buffer is the
  619. * largest size we support
  620. */
  621. descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
  622. ((MAX_PAGE_BUFFER_COUNT - pagecount) *
  623. sizeof(struct hv_page_buffer));
  624. packetlen = descsize + bufferlen;
  625. packetlen_aligned = ALIGN(packetlen, sizeof(u64));
  626. /* Setup the descriptor */
  627. desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
  628. desc.flags = flags;
  629. desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
  630. desc.length8 = (u16)(packetlen_aligned >> 3);
  631. desc.transactionid = requestid;
  632. desc.rangecount = pagecount;
  633. for (i = 0; i < pagecount; i++) {
  634. desc.range[i].len = pagebuffers[i].len;
  635. desc.range[i].offset = pagebuffers[i].offset;
  636. desc.range[i].pfn = pagebuffers[i].pfn;
  637. }
  638. bufferlist[0].iov_base = &desc;
  639. bufferlist[0].iov_len = descsize;
  640. bufferlist[1].iov_base = buffer;
  641. bufferlist[1].iov_len = bufferlen;
  642. bufferlist[2].iov_base = &aligned_data;
  643. bufferlist[2].iov_len = (packetlen_aligned - packetlen);
  644. return hv_ringbuffer_write(channel, bufferlist, 3,
  645. lock, kick_q);
  646. }
  647. EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer_ctl);
  648. /*
  649. * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
  650. * packets using a GPADL Direct packet type.
  651. */
  652. int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
  653. struct hv_page_buffer pagebuffers[],
  654. u32 pagecount, void *buffer, u32 bufferlen,
  655. u64 requestid)
  656. {
  657. u32 flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
  658. return vmbus_sendpacket_pagebuffer_ctl(channel, pagebuffers, pagecount,
  659. buffer, bufferlen, requestid,
  660. flags, true);
  661. }
  662. EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
  663. /*
  664. * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
  665. * using a GPADL Direct packet type.
  666. * The buffer includes the vmbus descriptor.
  667. */
  668. int vmbus_sendpacket_mpb_desc(struct vmbus_channel *channel,
  669. struct vmbus_packet_mpb_array *desc,
  670. u32 desc_size,
  671. void *buffer, u32 bufferlen, u64 requestid)
  672. {
  673. u32 packetlen;
  674. u32 packetlen_aligned;
  675. struct kvec bufferlist[3];
  676. u64 aligned_data = 0;
  677. bool lock = channel->acquire_ring_lock;
  678. packetlen = desc_size + bufferlen;
  679. packetlen_aligned = ALIGN(packetlen, sizeof(u64));
  680. /* Setup the descriptor */
  681. desc->type = VM_PKT_DATA_USING_GPA_DIRECT;
  682. desc->flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
  683. desc->dataoffset8 = desc_size >> 3; /* in 8-bytes grandularity */
  684. desc->length8 = (u16)(packetlen_aligned >> 3);
  685. desc->transactionid = requestid;
  686. desc->rangecount = 1;
  687. bufferlist[0].iov_base = desc;
  688. bufferlist[0].iov_len = desc_size;
  689. bufferlist[1].iov_base = buffer;
  690. bufferlist[1].iov_len = bufferlen;
  691. bufferlist[2].iov_base = &aligned_data;
  692. bufferlist[2].iov_len = (packetlen_aligned - packetlen);
  693. return hv_ringbuffer_write(channel, bufferlist, 3,
  694. lock, true);
  695. }
  696. EXPORT_SYMBOL_GPL(vmbus_sendpacket_mpb_desc);
  697. /*
  698. * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
  699. * using a GPADL Direct packet type.
  700. */
  701. int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
  702. struct hv_multipage_buffer *multi_pagebuffer,
  703. void *buffer, u32 bufferlen, u64 requestid)
  704. {
  705. struct vmbus_channel_packet_multipage_buffer desc;
  706. u32 descsize;
  707. u32 packetlen;
  708. u32 packetlen_aligned;
  709. struct kvec bufferlist[3];
  710. u64 aligned_data = 0;
  711. bool lock = channel->acquire_ring_lock;
  712. u32 pfncount = NUM_PAGES_SPANNED(multi_pagebuffer->offset,
  713. multi_pagebuffer->len);
  714. if (pfncount > MAX_MULTIPAGE_BUFFER_COUNT)
  715. return -EINVAL;
  716. /*
  717. * Adjust the size down since vmbus_channel_packet_multipage_buffer is
  718. * the largest size we support
  719. */
  720. descsize = sizeof(struct vmbus_channel_packet_multipage_buffer) -
  721. ((MAX_MULTIPAGE_BUFFER_COUNT - pfncount) *
  722. sizeof(u64));
  723. packetlen = descsize + bufferlen;
  724. packetlen_aligned = ALIGN(packetlen, sizeof(u64));
  725. /* Setup the descriptor */
  726. desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
  727. desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
  728. desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
  729. desc.length8 = (u16)(packetlen_aligned >> 3);
  730. desc.transactionid = requestid;
  731. desc.rangecount = 1;
  732. desc.range.len = multi_pagebuffer->len;
  733. desc.range.offset = multi_pagebuffer->offset;
  734. memcpy(desc.range.pfn_array, multi_pagebuffer->pfn_array,
  735. pfncount * sizeof(u64));
  736. bufferlist[0].iov_base = &desc;
  737. bufferlist[0].iov_len = descsize;
  738. bufferlist[1].iov_base = buffer;
  739. bufferlist[1].iov_len = bufferlen;
  740. bufferlist[2].iov_base = &aligned_data;
  741. bufferlist[2].iov_len = (packetlen_aligned - packetlen);
  742. return hv_ringbuffer_write(channel, bufferlist, 3,
  743. lock, true);
  744. }
  745. EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer);
  746. /**
  747. * vmbus_recvpacket() - Retrieve the user packet on the specified channel
  748. * @channel: Pointer to vmbus_channel structure.
  749. * @buffer: Pointer to the buffer you want to receive the data into.
  750. * @bufferlen: Maximum size of what the the buffer will hold
  751. * @buffer_actual_len: The actual size of the data after it was received
  752. * @requestid: Identifier of the request
  753. *
  754. * Receives directly from the hyper-v vmbus and puts the data it received
  755. * into Buffer. This will receive the data unparsed from hyper-v.
  756. *
  757. * Mainly used by Hyper-V drivers.
  758. */
  759. static inline int
  760. __vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
  761. u32 bufferlen, u32 *buffer_actual_len, u64 *requestid,
  762. bool raw)
  763. {
  764. return hv_ringbuffer_read(channel, buffer, bufferlen,
  765. buffer_actual_len, requestid, raw);
  766. }
  767. int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
  768. u32 bufferlen, u32 *buffer_actual_len,
  769. u64 *requestid)
  770. {
  771. return __vmbus_recvpacket(channel, buffer, bufferlen,
  772. buffer_actual_len, requestid, false);
  773. }
  774. EXPORT_SYMBOL(vmbus_recvpacket);
  775. /*
  776. * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
  777. */
  778. int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
  779. u32 bufferlen, u32 *buffer_actual_len,
  780. u64 *requestid)
  781. {
  782. return __vmbus_recvpacket(channel, buffer, bufferlen,
  783. buffer_actual_len, requestid, true);
  784. }
  785. EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);