channel.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  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 && !channel->low_latency) {
  46. vmbus_send_interrupt(channel->offermsg.child_relid);
  47. /* Get the child to parent monitor page */
  48. monitorpage = vmbus_connection.monitor_pages[1];
  49. sync_set_bit(channel->monitor_bit,
  50. (unsigned long *)&monitorpage->trigger_group
  51. [channel->monitor_grp].pending);
  52. } else {
  53. vmbus_set_event(channel);
  54. }
  55. }
  56. EXPORT_SYMBOL_GPL(vmbus_setevent);
  57. /*
  58. * vmbus_open - Open the specified channel.
  59. */
  60. int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size,
  61. u32 recv_ringbuffer_size, void *userdata, u32 userdatalen,
  62. void (*onchannelcallback)(void *context), void *context)
  63. {
  64. struct vmbus_channel_open_channel *open_msg;
  65. struct vmbus_channel_msginfo *open_info = NULL;
  66. unsigned long flags;
  67. int ret, err = 0;
  68. struct page *page;
  69. if (send_ringbuffer_size % PAGE_SIZE ||
  70. recv_ringbuffer_size % PAGE_SIZE)
  71. return -EINVAL;
  72. spin_lock_irqsave(&newchannel->lock, flags);
  73. if (newchannel->state == CHANNEL_OPEN_STATE) {
  74. newchannel->state = CHANNEL_OPENING_STATE;
  75. } else {
  76. spin_unlock_irqrestore(&newchannel->lock, flags);
  77. return -EINVAL;
  78. }
  79. spin_unlock_irqrestore(&newchannel->lock, flags);
  80. newchannel->onchannel_callback = onchannelcallback;
  81. newchannel->channel_callback_context = context;
  82. /* Allocate the ring buffer */
  83. page = alloc_pages_node(cpu_to_node(newchannel->target_cpu),
  84. GFP_KERNEL|__GFP_ZERO,
  85. get_order(send_ringbuffer_size +
  86. recv_ringbuffer_size));
  87. if (!page)
  88. page = alloc_pages(GFP_KERNEL|__GFP_ZERO,
  89. get_order(send_ringbuffer_size +
  90. recv_ringbuffer_size));
  91. if (!page) {
  92. err = -ENOMEM;
  93. goto error_set_chnstate;
  94. }
  95. newchannel->ringbuffer_pages = page_address(page);
  96. newchannel->ringbuffer_pagecount = (send_ringbuffer_size +
  97. recv_ringbuffer_size) >> PAGE_SHIFT;
  98. ret = hv_ringbuffer_init(&newchannel->outbound, page,
  99. send_ringbuffer_size >> PAGE_SHIFT);
  100. if (ret != 0) {
  101. err = ret;
  102. goto error_free_pages;
  103. }
  104. ret = hv_ringbuffer_init(&newchannel->inbound,
  105. &page[send_ringbuffer_size >> PAGE_SHIFT],
  106. recv_ringbuffer_size >> PAGE_SHIFT);
  107. if (ret != 0) {
  108. err = ret;
  109. goto error_free_pages;
  110. }
  111. /* Establish the gpadl for the ring buffer */
  112. newchannel->ringbuffer_gpadlhandle = 0;
  113. ret = vmbus_establish_gpadl(newchannel,
  114. page_address(page),
  115. send_ringbuffer_size +
  116. recv_ringbuffer_size,
  117. &newchannel->ringbuffer_gpadlhandle);
  118. if (ret != 0) {
  119. err = ret;
  120. goto error_free_pages;
  121. }
  122. /* Create and init the channel open message */
  123. open_info = kmalloc(sizeof(*open_info) +
  124. sizeof(struct vmbus_channel_open_channel),
  125. GFP_KERNEL);
  126. if (!open_info) {
  127. err = -ENOMEM;
  128. goto error_free_gpadl;
  129. }
  130. init_completion(&open_info->waitevent);
  131. open_info->waiting_channel = newchannel;
  132. open_msg = (struct vmbus_channel_open_channel *)open_info->msg;
  133. open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL;
  134. open_msg->openid = newchannel->offermsg.child_relid;
  135. open_msg->child_relid = newchannel->offermsg.child_relid;
  136. open_msg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle;
  137. open_msg->downstream_ringbuffer_pageoffset = send_ringbuffer_size >>
  138. PAGE_SHIFT;
  139. open_msg->target_vp = newchannel->target_vp;
  140. if (userdatalen > MAX_USER_DEFINED_BYTES) {
  141. err = -EINVAL;
  142. goto error_free_gpadl;
  143. }
  144. if (userdatalen)
  145. memcpy(open_msg->userdata, userdata, userdatalen);
  146. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  147. list_add_tail(&open_info->msglistentry,
  148. &vmbus_connection.chn_msg_list);
  149. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  150. if (newchannel->rescind) {
  151. err = -ENODEV;
  152. goto error_free_gpadl;
  153. }
  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 guarantees 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 - Establish 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. if (channel->rescind) {
  358. ret = -ENODEV;
  359. goto cleanup;
  360. }
  361. ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
  362. sizeof(*msginfo), true);
  363. if (ret != 0)
  364. goto cleanup;
  365. list_for_each(curr, &msginfo->submsglist) {
  366. submsginfo = (struct vmbus_channel_msginfo *)curr;
  367. gpadl_body =
  368. (struct vmbus_channel_gpadl_body *)submsginfo->msg;
  369. gpadl_body->header.msgtype =
  370. CHANNELMSG_GPADL_BODY;
  371. gpadl_body->gpadl = next_gpadl_handle;
  372. ret = vmbus_post_msg(gpadl_body,
  373. submsginfo->msgsize - sizeof(*submsginfo),
  374. true);
  375. if (ret != 0)
  376. goto cleanup;
  377. }
  378. wait_for_completion(&msginfo->waitevent);
  379. if (msginfo->response.gpadl_created.creation_status != 0) {
  380. pr_err("Failed to establish GPADL: err = 0x%x\n",
  381. msginfo->response.gpadl_created.creation_status);
  382. ret = -EDQUOT;
  383. goto cleanup;
  384. }
  385. if (channel->rescind) {
  386. ret = -ENODEV;
  387. goto cleanup;
  388. }
  389. /* At this point, we received the gpadl created msg */
  390. *gpadl_handle = gpadlmsg->gpadl;
  391. cleanup:
  392. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  393. list_del(&msginfo->msglistentry);
  394. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  395. list_for_each_entry_safe(submsginfo, tmp, &msginfo->submsglist,
  396. msglistentry) {
  397. kfree(submsginfo);
  398. }
  399. kfree(msginfo);
  400. return ret;
  401. }
  402. EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
  403. /*
  404. * vmbus_teardown_gpadl -Teardown the specified GPADL handle
  405. */
  406. int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
  407. {
  408. struct vmbus_channel_gpadl_teardown *msg;
  409. struct vmbus_channel_msginfo *info;
  410. unsigned long flags;
  411. int ret;
  412. info = kmalloc(sizeof(*info) +
  413. sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
  414. if (!info)
  415. return -ENOMEM;
  416. init_completion(&info->waitevent);
  417. info->waiting_channel = channel;
  418. msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
  419. msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
  420. msg->child_relid = channel->offermsg.child_relid;
  421. msg->gpadl = gpadl_handle;
  422. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  423. list_add_tail(&info->msglistentry,
  424. &vmbus_connection.chn_msg_list);
  425. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  426. if (channel->rescind)
  427. goto post_msg_err;
  428. ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_gpadl_teardown),
  429. true);
  430. if (ret)
  431. goto post_msg_err;
  432. wait_for_completion(&info->waitevent);
  433. post_msg_err:
  434. /*
  435. * If the channel has been rescinded;
  436. * we will be awakened by the rescind
  437. * handler; set the error code to zero so we don't leak memory.
  438. */
  439. if (channel->rescind)
  440. ret = 0;
  441. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  442. list_del(&info->msglistentry);
  443. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  444. kfree(info);
  445. return ret;
  446. }
  447. EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
  448. static void reset_channel_cb(void *arg)
  449. {
  450. struct vmbus_channel *channel = arg;
  451. channel->onchannel_callback = NULL;
  452. }
  453. void vmbus_reset_channel_cb(struct vmbus_channel *channel)
  454. {
  455. /*
  456. * vmbus_on_event(), running in the per-channel tasklet, can race
  457. * with vmbus_close_internal() in the case of SMP guest, e.g., when
  458. * the former is accessing channel->inbound.ring_buffer, the latter
  459. * could be freeing the ring_buffer pages, so here we must stop it
  460. * first.
  461. */
  462. tasklet_disable(&channel->callback_event);
  463. channel->sc_creation_callback = NULL;
  464. /* Stop the callback asap */
  465. if (channel->target_cpu != get_cpu()) {
  466. put_cpu();
  467. smp_call_function_single(channel->target_cpu, reset_channel_cb,
  468. channel, true);
  469. } else {
  470. reset_channel_cb(channel);
  471. put_cpu();
  472. }
  473. /* Re-enable tasklet for use on re-open */
  474. tasklet_enable(&channel->callback_event);
  475. }
  476. static int vmbus_close_internal(struct vmbus_channel *channel)
  477. {
  478. struct vmbus_channel_close_channel *msg;
  479. int ret;
  480. vmbus_reset_channel_cb(channel);
  481. /*
  482. * In case a device driver's probe() fails (e.g.,
  483. * util_probe() -> vmbus_open() returns -ENOMEM) and the device is
  484. * rescinded later (e.g., we dynamically disable an Integrated Service
  485. * in Hyper-V Manager), the driver's remove() invokes vmbus_close():
  486. * here we should skip most of the below cleanup work.
  487. */
  488. if (channel->state != CHANNEL_OPENED_STATE) {
  489. ret = -EINVAL;
  490. goto out;
  491. }
  492. channel->state = CHANNEL_OPEN_STATE;
  493. /* Send a closing message */
  494. msg = &channel->close_msg.msg;
  495. msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
  496. msg->child_relid = channel->offermsg.child_relid;
  497. ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel),
  498. true);
  499. if (ret) {
  500. pr_err("Close failed: close post msg return is %d\n", ret);
  501. /*
  502. * If we failed to post the close msg,
  503. * it is perhaps better to leak memory.
  504. */
  505. goto out;
  506. }
  507. /* Tear down the gpadl for the channel's ring buffer */
  508. if (channel->ringbuffer_gpadlhandle) {
  509. ret = vmbus_teardown_gpadl(channel,
  510. channel->ringbuffer_gpadlhandle);
  511. if (ret) {
  512. pr_err("Close failed: teardown gpadl return %d\n", ret);
  513. /*
  514. * If we failed to teardown gpadl,
  515. * it is perhaps better to leak memory.
  516. */
  517. goto out;
  518. }
  519. }
  520. /* Cleanup the ring buffers for this channel */
  521. hv_ringbuffer_cleanup(&channel->outbound);
  522. hv_ringbuffer_cleanup(&channel->inbound);
  523. free_pages((unsigned long)channel->ringbuffer_pages,
  524. get_order(channel->ringbuffer_pagecount * PAGE_SIZE));
  525. out:
  526. return ret;
  527. }
  528. /*
  529. * vmbus_close - Close the specified channel
  530. */
  531. void vmbus_close(struct vmbus_channel *channel)
  532. {
  533. struct list_head *cur, *tmp;
  534. struct vmbus_channel *cur_channel;
  535. if (channel->primary_channel != NULL) {
  536. /*
  537. * We will only close sub-channels when
  538. * the primary is closed.
  539. */
  540. return;
  541. }
  542. /*
  543. * Close all the sub-channels first and then close the
  544. * primary channel.
  545. */
  546. list_for_each_safe(cur, tmp, &channel->sc_list) {
  547. cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
  548. if (cur_channel->rescind) {
  549. wait_for_completion(&cur_channel->rescind_event);
  550. mutex_lock(&vmbus_connection.channel_mutex);
  551. vmbus_close_internal(cur_channel);
  552. hv_process_channel_removal(
  553. cur_channel->offermsg.child_relid);
  554. } else {
  555. mutex_lock(&vmbus_connection.channel_mutex);
  556. vmbus_close_internal(cur_channel);
  557. }
  558. mutex_unlock(&vmbus_connection.channel_mutex);
  559. }
  560. /*
  561. * Now close the primary.
  562. */
  563. mutex_lock(&vmbus_connection.channel_mutex);
  564. vmbus_close_internal(channel);
  565. mutex_unlock(&vmbus_connection.channel_mutex);
  566. }
  567. EXPORT_SYMBOL_GPL(vmbus_close);
  568. /**
  569. * vmbus_sendpacket() - Send the specified buffer on the given channel
  570. * @channel: Pointer to vmbus_channel structure.
  571. * @buffer: Pointer to the buffer you want to receive the data into.
  572. * @bufferlen: Maximum size of what the the buffer will hold
  573. * @requestid: Identifier of the request
  574. * @type: Type of packet that is being send e.g. negotiate, time
  575. * packet etc.
  576. *
  577. * Sends data in @buffer directly to hyper-v via the vmbus
  578. * This will send the data unparsed to hyper-v.
  579. *
  580. * Mainly used by Hyper-V drivers.
  581. */
  582. int vmbus_sendpacket(struct vmbus_channel *channel, void *buffer,
  583. u32 bufferlen, u64 requestid,
  584. enum vmbus_packet_type type, u32 flags)
  585. {
  586. struct vmpacket_descriptor desc;
  587. u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
  588. u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
  589. struct kvec bufferlist[3];
  590. u64 aligned_data = 0;
  591. int num_vecs = ((bufferlen != 0) ? 3 : 1);
  592. /* Setup the descriptor */
  593. desc.type = type; /* VmbusPacketTypeDataInBand; */
  594. desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
  595. /* in 8-bytes granularity */
  596. desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
  597. desc.len8 = (u16)(packetlen_aligned >> 3);
  598. desc.trans_id = requestid;
  599. bufferlist[0].iov_base = &desc;
  600. bufferlist[0].iov_len = sizeof(struct vmpacket_descriptor);
  601. bufferlist[1].iov_base = buffer;
  602. bufferlist[1].iov_len = bufferlen;
  603. bufferlist[2].iov_base = &aligned_data;
  604. bufferlist[2].iov_len = (packetlen_aligned - packetlen);
  605. return hv_ringbuffer_write(channel, bufferlist, num_vecs);
  606. }
  607. EXPORT_SYMBOL(vmbus_sendpacket);
  608. /*
  609. * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
  610. * packets using a GPADL Direct packet type. This interface allows you
  611. * to control notifying the host. This will be useful for sending
  612. * batched data. Also the sender can control the send flags
  613. * explicitly.
  614. */
  615. int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
  616. struct hv_page_buffer pagebuffers[],
  617. u32 pagecount, void *buffer, u32 bufferlen,
  618. u64 requestid)
  619. {
  620. int i;
  621. struct vmbus_channel_packet_page_buffer desc;
  622. u32 descsize;
  623. u32 packetlen;
  624. u32 packetlen_aligned;
  625. struct kvec bufferlist[3];
  626. u64 aligned_data = 0;
  627. if (pagecount > MAX_PAGE_BUFFER_COUNT)
  628. return -EINVAL;
  629. /*
  630. * Adjust the size down since vmbus_channel_packet_page_buffer is the
  631. * largest size we support
  632. */
  633. descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
  634. ((MAX_PAGE_BUFFER_COUNT - pagecount) *
  635. sizeof(struct hv_page_buffer));
  636. packetlen = descsize + bufferlen;
  637. packetlen_aligned = ALIGN(packetlen, sizeof(u64));
  638. /* Setup the descriptor */
  639. desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
  640. desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
  641. desc.dataoffset8 = descsize >> 3; /* in 8-bytes granularity */
  642. desc.length8 = (u16)(packetlen_aligned >> 3);
  643. desc.transactionid = requestid;
  644. desc.rangecount = pagecount;
  645. for (i = 0; i < pagecount; i++) {
  646. desc.range[i].len = pagebuffers[i].len;
  647. desc.range[i].offset = pagebuffers[i].offset;
  648. desc.range[i].pfn = pagebuffers[i].pfn;
  649. }
  650. bufferlist[0].iov_base = &desc;
  651. bufferlist[0].iov_len = descsize;
  652. bufferlist[1].iov_base = buffer;
  653. bufferlist[1].iov_len = bufferlen;
  654. bufferlist[2].iov_base = &aligned_data;
  655. bufferlist[2].iov_len = (packetlen_aligned - packetlen);
  656. return hv_ringbuffer_write(channel, bufferlist, 3);
  657. }
  658. EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
  659. /*
  660. * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
  661. * using a GPADL Direct packet type.
  662. * The buffer includes the vmbus descriptor.
  663. */
  664. int vmbus_sendpacket_mpb_desc(struct vmbus_channel *channel,
  665. struct vmbus_packet_mpb_array *desc,
  666. u32 desc_size,
  667. void *buffer, u32 bufferlen, u64 requestid)
  668. {
  669. u32 packetlen;
  670. u32 packetlen_aligned;
  671. struct kvec bufferlist[3];
  672. u64 aligned_data = 0;
  673. packetlen = desc_size + bufferlen;
  674. packetlen_aligned = ALIGN(packetlen, sizeof(u64));
  675. /* Setup the descriptor */
  676. desc->type = VM_PKT_DATA_USING_GPA_DIRECT;
  677. desc->flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
  678. desc->dataoffset8 = desc_size >> 3; /* in 8-bytes granularity */
  679. desc->length8 = (u16)(packetlen_aligned >> 3);
  680. desc->transactionid = requestid;
  681. desc->rangecount = 1;
  682. bufferlist[0].iov_base = desc;
  683. bufferlist[0].iov_len = desc_size;
  684. bufferlist[1].iov_base = buffer;
  685. bufferlist[1].iov_len = bufferlen;
  686. bufferlist[2].iov_base = &aligned_data;
  687. bufferlist[2].iov_len = (packetlen_aligned - packetlen);
  688. return hv_ringbuffer_write(channel, bufferlist, 3);
  689. }
  690. EXPORT_SYMBOL_GPL(vmbus_sendpacket_mpb_desc);
  691. /**
  692. * vmbus_recvpacket() - Retrieve the user packet on the specified channel
  693. * @channel: Pointer to vmbus_channel structure.
  694. * @buffer: Pointer to the buffer you want to receive the data into.
  695. * @bufferlen: Maximum size of what the the buffer will hold
  696. * @buffer_actual_len: The actual size of the data after it was received
  697. * @requestid: Identifier of the request
  698. *
  699. * Receives directly from the hyper-v vmbus and puts the data it received
  700. * into Buffer. This will receive the data unparsed from hyper-v.
  701. *
  702. * Mainly used by Hyper-V drivers.
  703. */
  704. static inline int
  705. __vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
  706. u32 bufferlen, u32 *buffer_actual_len, u64 *requestid,
  707. bool raw)
  708. {
  709. return hv_ringbuffer_read(channel, buffer, bufferlen,
  710. buffer_actual_len, requestid, raw);
  711. }
  712. int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
  713. u32 bufferlen, u32 *buffer_actual_len,
  714. u64 *requestid)
  715. {
  716. return __vmbus_recvpacket(channel, buffer, bufferlen,
  717. buffer_actual_len, requestid, false);
  718. }
  719. EXPORT_SYMBOL(vmbus_recvpacket);
  720. /*
  721. * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
  722. */
  723. int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
  724. u32 bufferlen, u32 *buffer_actual_len,
  725. u64 *requestid)
  726. {
  727. return __vmbus_recvpacket(channel, buffer, bufferlen,
  728. buffer_actual_len, requestid, true);
  729. }
  730. EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);