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