media: vimc: free vimc_cap_device when the last user disappears
[linux/fpc-iii.git] / drivers / hv / channel.c
blob23381c41d087ae02be9ebf5d1a932b589e9a30e3
1 /*
2 * Copyright (c) 2009, Microsoft Corporation.
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.
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.
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.
17 * Authors:
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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 #include <asm/page.h>
34 #include "hyperv_vmbus.h"
36 #define NUM_PAGES_SPANNED(addr, len) \
37 ((PAGE_ALIGN(addr + len) >> PAGE_SHIFT) - (addr >> PAGE_SHIFT))
39 static unsigned long virt_to_hvpfn(void *addr)
41 unsigned long paddr;
43 if (is_vmalloc_addr(addr))
44 paddr = page_to_phys(vmalloc_to_page(addr)) +
45 offset_in_page(addr);
46 else
47 paddr = __pa(addr);
49 return paddr >> PAGE_SHIFT;
53 * vmbus_setevent- Trigger an event notification on the specified
54 * channel.
56 void vmbus_setevent(struct vmbus_channel *channel)
58 struct hv_monitor_page *monitorpage;
60 trace_vmbus_setevent(channel);
63 * For channels marked as in "low latency" mode
64 * bypass the monitor page mechanism.
66 if (channel->offermsg.monitor_allocated && !channel->low_latency) {
67 vmbus_send_interrupt(channel->offermsg.child_relid);
69 /* Get the child to parent monitor page */
70 monitorpage = vmbus_connection.monitor_pages[1];
72 sync_set_bit(channel->monitor_bit,
73 (unsigned long *)&monitorpage->trigger_group
74 [channel->monitor_grp].pending);
76 } else {
77 vmbus_set_event(channel);
80 EXPORT_SYMBOL_GPL(vmbus_setevent);
82 /* vmbus_free_ring - drop mapping of ring buffer */
83 void vmbus_free_ring(struct vmbus_channel *channel)
85 hv_ringbuffer_cleanup(&channel->outbound);
86 hv_ringbuffer_cleanup(&channel->inbound);
88 if (channel->ringbuffer_page) {
89 __free_pages(channel->ringbuffer_page,
90 get_order(channel->ringbuffer_pagecount
91 << PAGE_SHIFT));
92 channel->ringbuffer_page = NULL;
95 EXPORT_SYMBOL_GPL(vmbus_free_ring);
97 /* vmbus_alloc_ring - allocate and map pages for ring buffer */
98 int vmbus_alloc_ring(struct vmbus_channel *newchannel,
99 u32 send_size, u32 recv_size)
101 struct page *page;
102 int order;
104 if (send_size % PAGE_SIZE || recv_size % PAGE_SIZE)
105 return -EINVAL;
107 /* Allocate the ring buffer */
108 order = get_order(send_size + recv_size);
109 page = alloc_pages_node(cpu_to_node(newchannel->target_cpu),
110 GFP_KERNEL|__GFP_ZERO, order);
112 if (!page)
113 page = alloc_pages(GFP_KERNEL|__GFP_ZERO, order);
115 if (!page)
116 return -ENOMEM;
118 newchannel->ringbuffer_page = page;
119 newchannel->ringbuffer_pagecount = (send_size + recv_size) >> PAGE_SHIFT;
120 newchannel->ringbuffer_send_offset = send_size >> PAGE_SHIFT;
122 return 0;
124 EXPORT_SYMBOL_GPL(vmbus_alloc_ring);
126 static int __vmbus_open(struct vmbus_channel *newchannel,
127 void *userdata, u32 userdatalen,
128 void (*onchannelcallback)(void *context), void *context)
130 struct vmbus_channel_open_channel *open_msg;
131 struct vmbus_channel_msginfo *open_info = NULL;
132 struct page *page = newchannel->ringbuffer_page;
133 u32 send_pages, recv_pages;
134 unsigned long flags;
135 int err;
137 if (userdatalen > MAX_USER_DEFINED_BYTES)
138 return -EINVAL;
140 send_pages = newchannel->ringbuffer_send_offset;
141 recv_pages = newchannel->ringbuffer_pagecount - send_pages;
143 spin_lock_irqsave(&newchannel->lock, flags);
144 if (newchannel->state != CHANNEL_OPEN_STATE) {
145 spin_unlock_irqrestore(&newchannel->lock, flags);
146 return -EINVAL;
148 spin_unlock_irqrestore(&newchannel->lock, flags);
150 newchannel->state = CHANNEL_OPENING_STATE;
151 newchannel->onchannel_callback = onchannelcallback;
152 newchannel->channel_callback_context = context;
154 err = hv_ringbuffer_init(&newchannel->outbound, page, send_pages);
155 if (err)
156 goto error_clean_ring;
158 err = hv_ringbuffer_init(&newchannel->inbound,
159 &page[send_pages], recv_pages);
160 if (err)
161 goto error_clean_ring;
163 /* Establish the gpadl for the ring buffer */
164 newchannel->ringbuffer_gpadlhandle = 0;
166 err = vmbus_establish_gpadl(newchannel,
167 page_address(newchannel->ringbuffer_page),
168 (send_pages + recv_pages) << PAGE_SHIFT,
169 &newchannel->ringbuffer_gpadlhandle);
170 if (err)
171 goto error_clean_ring;
173 /* Create and init the channel open message */
174 open_info = kmalloc(sizeof(*open_info) +
175 sizeof(struct vmbus_channel_open_channel),
176 GFP_KERNEL);
177 if (!open_info) {
178 err = -ENOMEM;
179 goto error_free_gpadl;
182 init_completion(&open_info->waitevent);
183 open_info->waiting_channel = newchannel;
185 open_msg = (struct vmbus_channel_open_channel *)open_info->msg;
186 open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL;
187 open_msg->openid = newchannel->offermsg.child_relid;
188 open_msg->child_relid = newchannel->offermsg.child_relid;
189 open_msg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle;
190 open_msg->downstream_ringbuffer_pageoffset = newchannel->ringbuffer_send_offset;
191 open_msg->target_vp = newchannel->target_vp;
193 if (userdatalen)
194 memcpy(open_msg->userdata, userdata, userdatalen);
196 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
197 list_add_tail(&open_info->msglistentry,
198 &vmbus_connection.chn_msg_list);
199 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
201 if (newchannel->rescind) {
202 err = -ENODEV;
203 goto error_free_info;
206 err = vmbus_post_msg(open_msg,
207 sizeof(struct vmbus_channel_open_channel), true);
209 trace_vmbus_open(open_msg, err);
211 if (err != 0)
212 goto error_clean_msglist;
214 wait_for_completion(&open_info->waitevent);
216 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
217 list_del(&open_info->msglistentry);
218 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
220 if (newchannel->rescind) {
221 err = -ENODEV;
222 goto error_free_info;
225 if (open_info->response.open_result.status) {
226 err = -EAGAIN;
227 goto error_free_info;
230 newchannel->state = CHANNEL_OPENED_STATE;
231 kfree(open_info);
232 return 0;
234 error_clean_msglist:
235 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
236 list_del(&open_info->msglistentry);
237 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
238 error_free_info:
239 kfree(open_info);
240 error_free_gpadl:
241 vmbus_teardown_gpadl(newchannel, newchannel->ringbuffer_gpadlhandle);
242 newchannel->ringbuffer_gpadlhandle = 0;
243 error_clean_ring:
244 hv_ringbuffer_cleanup(&newchannel->outbound);
245 hv_ringbuffer_cleanup(&newchannel->inbound);
246 newchannel->state = CHANNEL_OPEN_STATE;
247 return err;
251 * vmbus_connect_ring - Open the channel but reuse ring buffer
253 int vmbus_connect_ring(struct vmbus_channel *newchannel,
254 void (*onchannelcallback)(void *context), void *context)
256 return __vmbus_open(newchannel, NULL, 0, onchannelcallback, context);
258 EXPORT_SYMBOL_GPL(vmbus_connect_ring);
261 * vmbus_open - Open the specified channel.
263 int vmbus_open(struct vmbus_channel *newchannel,
264 u32 send_ringbuffer_size, u32 recv_ringbuffer_size,
265 void *userdata, u32 userdatalen,
266 void (*onchannelcallback)(void *context), void *context)
268 int err;
270 err = vmbus_alloc_ring(newchannel, send_ringbuffer_size,
271 recv_ringbuffer_size);
272 if (err)
273 return err;
275 err = __vmbus_open(newchannel, userdata, userdatalen,
276 onchannelcallback, context);
277 if (err)
278 vmbus_free_ring(newchannel);
280 return err;
282 EXPORT_SYMBOL_GPL(vmbus_open);
284 /* Used for Hyper-V Socket: a guest client's connect() to the host */
285 int vmbus_send_tl_connect_request(const guid_t *shv_guest_servie_id,
286 const guid_t *shv_host_servie_id)
288 struct vmbus_channel_tl_connect_request conn_msg;
289 int ret;
291 memset(&conn_msg, 0, sizeof(conn_msg));
292 conn_msg.header.msgtype = CHANNELMSG_TL_CONNECT_REQUEST;
293 conn_msg.guest_endpoint_id = *shv_guest_servie_id;
294 conn_msg.host_service_id = *shv_host_servie_id;
296 ret = vmbus_post_msg(&conn_msg, sizeof(conn_msg), true);
298 trace_vmbus_send_tl_connect_request(&conn_msg, ret);
300 return ret;
302 EXPORT_SYMBOL_GPL(vmbus_send_tl_connect_request);
305 * create_gpadl_header - Creates a gpadl for the specified buffer
307 static int create_gpadl_header(void *kbuffer, u32 size,
308 struct vmbus_channel_msginfo **msginfo)
310 int i;
311 int pagecount;
312 struct vmbus_channel_gpadl_header *gpadl_header;
313 struct vmbus_channel_gpadl_body *gpadl_body;
314 struct vmbus_channel_msginfo *msgheader;
315 struct vmbus_channel_msginfo *msgbody = NULL;
316 u32 msgsize;
318 int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
320 pagecount = size >> PAGE_SHIFT;
322 /* do we need a gpadl body msg */
323 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
324 sizeof(struct vmbus_channel_gpadl_header) -
325 sizeof(struct gpa_range);
326 pfncount = pfnsize / sizeof(u64);
328 if (pagecount > pfncount) {
329 /* we need a gpadl body */
330 /* fill in the header */
331 msgsize = sizeof(struct vmbus_channel_msginfo) +
332 sizeof(struct vmbus_channel_gpadl_header) +
333 sizeof(struct gpa_range) + pfncount * sizeof(u64);
334 msgheader = kzalloc(msgsize, GFP_KERNEL);
335 if (!msgheader)
336 goto nomem;
338 INIT_LIST_HEAD(&msgheader->submsglist);
339 msgheader->msgsize = msgsize;
341 gpadl_header = (struct vmbus_channel_gpadl_header *)
342 msgheader->msg;
343 gpadl_header->rangecount = 1;
344 gpadl_header->range_buflen = sizeof(struct gpa_range) +
345 pagecount * sizeof(u64);
346 gpadl_header->range[0].byte_offset = 0;
347 gpadl_header->range[0].byte_count = size;
348 for (i = 0; i < pfncount; i++)
349 gpadl_header->range[0].pfn_array[i] = virt_to_hvpfn(
350 kbuffer + PAGE_SIZE * i);
351 *msginfo = msgheader;
353 pfnsum = pfncount;
354 pfnleft = pagecount - pfncount;
356 /* how many pfns can we fit */
357 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
358 sizeof(struct vmbus_channel_gpadl_body);
359 pfncount = pfnsize / sizeof(u64);
361 /* fill in the body */
362 while (pfnleft) {
363 if (pfnleft > pfncount)
364 pfncurr = pfncount;
365 else
366 pfncurr = pfnleft;
368 msgsize = sizeof(struct vmbus_channel_msginfo) +
369 sizeof(struct vmbus_channel_gpadl_body) +
370 pfncurr * sizeof(u64);
371 msgbody = kzalloc(msgsize, GFP_KERNEL);
373 if (!msgbody) {
374 struct vmbus_channel_msginfo *pos = NULL;
375 struct vmbus_channel_msginfo *tmp = NULL;
377 * Free up all the allocated messages.
379 list_for_each_entry_safe(pos, tmp,
380 &msgheader->submsglist,
381 msglistentry) {
383 list_del(&pos->msglistentry);
384 kfree(pos);
387 goto nomem;
390 msgbody->msgsize = msgsize;
391 gpadl_body =
392 (struct vmbus_channel_gpadl_body *)msgbody->msg;
395 * Gpadl is u32 and we are using a pointer which could
396 * be 64-bit
397 * This is governed by the guest/host protocol and
398 * so the hypervisor guarantees that this is ok.
400 for (i = 0; i < pfncurr; i++)
401 gpadl_body->pfn[i] = virt_to_hvpfn(
402 kbuffer + PAGE_SIZE * (pfnsum + i));
404 /* add to msg header */
405 list_add_tail(&msgbody->msglistentry,
406 &msgheader->submsglist);
407 pfnsum += pfncurr;
408 pfnleft -= pfncurr;
410 } else {
411 /* everything fits in a header */
412 msgsize = sizeof(struct vmbus_channel_msginfo) +
413 sizeof(struct vmbus_channel_gpadl_header) +
414 sizeof(struct gpa_range) + pagecount * sizeof(u64);
415 msgheader = kzalloc(msgsize, GFP_KERNEL);
416 if (msgheader == NULL)
417 goto nomem;
419 INIT_LIST_HEAD(&msgheader->submsglist);
420 msgheader->msgsize = msgsize;
422 gpadl_header = (struct vmbus_channel_gpadl_header *)
423 msgheader->msg;
424 gpadl_header->rangecount = 1;
425 gpadl_header->range_buflen = sizeof(struct gpa_range) +
426 pagecount * sizeof(u64);
427 gpadl_header->range[0].byte_offset = 0;
428 gpadl_header->range[0].byte_count = size;
429 for (i = 0; i < pagecount; i++)
430 gpadl_header->range[0].pfn_array[i] = virt_to_hvpfn(
431 kbuffer + PAGE_SIZE * i);
433 *msginfo = msgheader;
436 return 0;
437 nomem:
438 kfree(msgheader);
439 kfree(msgbody);
440 return -ENOMEM;
444 * vmbus_establish_gpadl - Establish a GPADL for the specified buffer
446 * @channel: a channel
447 * @kbuffer: from kmalloc or vmalloc
448 * @size: page-size multiple
449 * @gpadl_handle: some funky thing
451 int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
452 u32 size, u32 *gpadl_handle)
454 struct vmbus_channel_gpadl_header *gpadlmsg;
455 struct vmbus_channel_gpadl_body *gpadl_body;
456 struct vmbus_channel_msginfo *msginfo = NULL;
457 struct vmbus_channel_msginfo *submsginfo, *tmp;
458 struct list_head *curr;
459 u32 next_gpadl_handle;
460 unsigned long flags;
461 int ret = 0;
463 next_gpadl_handle =
464 (atomic_inc_return(&vmbus_connection.next_gpadl_handle) - 1);
466 ret = create_gpadl_header(kbuffer, size, &msginfo);
467 if (ret)
468 return ret;
470 init_completion(&msginfo->waitevent);
471 msginfo->waiting_channel = channel;
473 gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
474 gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
475 gpadlmsg->child_relid = channel->offermsg.child_relid;
476 gpadlmsg->gpadl = next_gpadl_handle;
479 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
480 list_add_tail(&msginfo->msglistentry,
481 &vmbus_connection.chn_msg_list);
483 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
485 if (channel->rescind) {
486 ret = -ENODEV;
487 goto cleanup;
490 ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
491 sizeof(*msginfo), true);
493 trace_vmbus_establish_gpadl_header(gpadlmsg, ret);
495 if (ret != 0)
496 goto cleanup;
498 list_for_each(curr, &msginfo->submsglist) {
499 submsginfo = (struct vmbus_channel_msginfo *)curr;
500 gpadl_body =
501 (struct vmbus_channel_gpadl_body *)submsginfo->msg;
503 gpadl_body->header.msgtype =
504 CHANNELMSG_GPADL_BODY;
505 gpadl_body->gpadl = next_gpadl_handle;
507 ret = vmbus_post_msg(gpadl_body,
508 submsginfo->msgsize - sizeof(*submsginfo),
509 true);
511 trace_vmbus_establish_gpadl_body(gpadl_body, ret);
513 if (ret != 0)
514 goto cleanup;
517 wait_for_completion(&msginfo->waitevent);
519 if (msginfo->response.gpadl_created.creation_status != 0) {
520 pr_err("Failed to establish GPADL: err = 0x%x\n",
521 msginfo->response.gpadl_created.creation_status);
523 ret = -EDQUOT;
524 goto cleanup;
527 if (channel->rescind) {
528 ret = -ENODEV;
529 goto cleanup;
532 /* At this point, we received the gpadl created msg */
533 *gpadl_handle = gpadlmsg->gpadl;
535 cleanup:
536 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
537 list_del(&msginfo->msglistentry);
538 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
539 list_for_each_entry_safe(submsginfo, tmp, &msginfo->submsglist,
540 msglistentry) {
541 kfree(submsginfo);
544 kfree(msginfo);
545 return ret;
547 EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
550 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
552 int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
554 struct vmbus_channel_gpadl_teardown *msg;
555 struct vmbus_channel_msginfo *info;
556 unsigned long flags;
557 int ret;
559 info = kmalloc(sizeof(*info) +
560 sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
561 if (!info)
562 return -ENOMEM;
564 init_completion(&info->waitevent);
565 info->waiting_channel = channel;
567 msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
569 msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
570 msg->child_relid = channel->offermsg.child_relid;
571 msg->gpadl = gpadl_handle;
573 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
574 list_add_tail(&info->msglistentry,
575 &vmbus_connection.chn_msg_list);
576 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
578 if (channel->rescind)
579 goto post_msg_err;
581 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_gpadl_teardown),
582 true);
584 trace_vmbus_teardown_gpadl(msg, ret);
586 if (ret)
587 goto post_msg_err;
589 wait_for_completion(&info->waitevent);
591 post_msg_err:
593 * If the channel has been rescinded;
594 * we will be awakened by the rescind
595 * handler; set the error code to zero so we don't leak memory.
597 if (channel->rescind)
598 ret = 0;
600 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
601 list_del(&info->msglistentry);
602 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
604 kfree(info);
605 return ret;
607 EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
609 static void reset_channel_cb(void *arg)
611 struct vmbus_channel *channel = arg;
613 channel->onchannel_callback = NULL;
616 void vmbus_reset_channel_cb(struct vmbus_channel *channel)
619 * vmbus_on_event(), running in the per-channel tasklet, can race
620 * with vmbus_close_internal() in the case of SMP guest, e.g., when
621 * the former is accessing channel->inbound.ring_buffer, the latter
622 * could be freeing the ring_buffer pages, so here we must stop it
623 * first.
625 tasklet_disable(&channel->callback_event);
627 channel->sc_creation_callback = NULL;
629 /* Stop the callback asap */
630 if (channel->target_cpu != get_cpu()) {
631 put_cpu();
632 smp_call_function_single(channel->target_cpu, reset_channel_cb,
633 channel, true);
634 } else {
635 reset_channel_cb(channel);
636 put_cpu();
639 /* Re-enable tasklet for use on re-open */
640 tasklet_enable(&channel->callback_event);
643 static int vmbus_close_internal(struct vmbus_channel *channel)
645 struct vmbus_channel_close_channel *msg;
646 int ret;
648 vmbus_reset_channel_cb(channel);
651 * In case a device driver's probe() fails (e.g.,
652 * util_probe() -> vmbus_open() returns -ENOMEM) and the device is
653 * rescinded later (e.g., we dynamically disable an Integrated Service
654 * in Hyper-V Manager), the driver's remove() invokes vmbus_close():
655 * here we should skip most of the below cleanup work.
657 if (channel->state != CHANNEL_OPENED_STATE)
658 return -EINVAL;
660 channel->state = CHANNEL_OPEN_STATE;
662 /* Send a closing message */
664 msg = &channel->close_msg.msg;
666 msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
667 msg->child_relid = channel->offermsg.child_relid;
669 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel),
670 true);
672 trace_vmbus_close_internal(msg, ret);
674 if (ret) {
675 pr_err("Close failed: close post msg return is %d\n", ret);
677 * If we failed to post the close msg,
678 * it is perhaps better to leak memory.
682 /* Tear down the gpadl for the channel's ring buffer */
683 else if (channel->ringbuffer_gpadlhandle) {
684 ret = vmbus_teardown_gpadl(channel,
685 channel->ringbuffer_gpadlhandle);
686 if (ret) {
687 pr_err("Close failed: teardown gpadl return %d\n", ret);
689 * If we failed to teardown gpadl,
690 * it is perhaps better to leak memory.
694 channel->ringbuffer_gpadlhandle = 0;
697 return ret;
700 /* disconnect ring - close all channels */
701 int vmbus_disconnect_ring(struct vmbus_channel *channel)
703 struct vmbus_channel *cur_channel, *tmp;
704 int ret;
706 if (channel->primary_channel != NULL)
707 return -EINVAL;
709 list_for_each_entry_safe(cur_channel, tmp, &channel->sc_list, sc_list) {
710 if (cur_channel->rescind)
711 wait_for_completion(&cur_channel->rescind_event);
713 mutex_lock(&vmbus_connection.channel_mutex);
714 if (vmbus_close_internal(cur_channel) == 0) {
715 vmbus_free_ring(cur_channel);
717 if (cur_channel->rescind)
718 hv_process_channel_removal(cur_channel);
720 mutex_unlock(&vmbus_connection.channel_mutex);
724 * Now close the primary.
726 mutex_lock(&vmbus_connection.channel_mutex);
727 ret = vmbus_close_internal(channel);
728 mutex_unlock(&vmbus_connection.channel_mutex);
730 return ret;
732 EXPORT_SYMBOL_GPL(vmbus_disconnect_ring);
735 * vmbus_close - Close the specified channel
737 void vmbus_close(struct vmbus_channel *channel)
739 if (vmbus_disconnect_ring(channel) == 0)
740 vmbus_free_ring(channel);
742 EXPORT_SYMBOL_GPL(vmbus_close);
745 * vmbus_sendpacket() - Send the specified buffer on the given channel
746 * @channel: Pointer to vmbus_channel structure
747 * @buffer: Pointer to the buffer you want to send the data from.
748 * @bufferlen: Maximum size of what the buffer holds.
749 * @requestid: Identifier of the request
750 * @type: Type of packet that is being sent e.g. negotiate, time
751 * packet etc.
752 * @flags: 0 or VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
754 * Sends data in @buffer directly to Hyper-V via the vmbus.
755 * This will send the data unparsed to Hyper-V.
757 * Mainly used by Hyper-V drivers.
759 int vmbus_sendpacket(struct vmbus_channel *channel, void *buffer,
760 u32 bufferlen, u64 requestid,
761 enum vmbus_packet_type type, u32 flags)
763 struct vmpacket_descriptor desc;
764 u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
765 u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
766 struct kvec bufferlist[3];
767 u64 aligned_data = 0;
768 int num_vecs = ((bufferlen != 0) ? 3 : 1);
771 /* Setup the descriptor */
772 desc.type = type; /* VmbusPacketTypeDataInBand; */
773 desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
774 /* in 8-bytes granularity */
775 desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
776 desc.len8 = (u16)(packetlen_aligned >> 3);
777 desc.trans_id = requestid;
779 bufferlist[0].iov_base = &desc;
780 bufferlist[0].iov_len = sizeof(struct vmpacket_descriptor);
781 bufferlist[1].iov_base = buffer;
782 bufferlist[1].iov_len = bufferlen;
783 bufferlist[2].iov_base = &aligned_data;
784 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
786 return hv_ringbuffer_write(channel, bufferlist, num_vecs);
788 EXPORT_SYMBOL(vmbus_sendpacket);
791 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
792 * packets using a GPADL Direct packet type. This interface allows you
793 * to control notifying the host. This will be useful for sending
794 * batched data. Also the sender can control the send flags
795 * explicitly.
797 int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
798 struct hv_page_buffer pagebuffers[],
799 u32 pagecount, void *buffer, u32 bufferlen,
800 u64 requestid)
802 int i;
803 struct vmbus_channel_packet_page_buffer desc;
804 u32 descsize;
805 u32 packetlen;
806 u32 packetlen_aligned;
807 struct kvec bufferlist[3];
808 u64 aligned_data = 0;
810 if (pagecount > MAX_PAGE_BUFFER_COUNT)
811 return -EINVAL;
814 * Adjust the size down since vmbus_channel_packet_page_buffer is the
815 * largest size we support
817 descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
818 ((MAX_PAGE_BUFFER_COUNT - pagecount) *
819 sizeof(struct hv_page_buffer));
820 packetlen = descsize + bufferlen;
821 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
823 /* Setup the descriptor */
824 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
825 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
826 desc.dataoffset8 = descsize >> 3; /* in 8-bytes granularity */
827 desc.length8 = (u16)(packetlen_aligned >> 3);
828 desc.transactionid = requestid;
829 desc.reserved = 0;
830 desc.rangecount = pagecount;
832 for (i = 0; i < pagecount; i++) {
833 desc.range[i].len = pagebuffers[i].len;
834 desc.range[i].offset = pagebuffers[i].offset;
835 desc.range[i].pfn = pagebuffers[i].pfn;
838 bufferlist[0].iov_base = &desc;
839 bufferlist[0].iov_len = descsize;
840 bufferlist[1].iov_base = buffer;
841 bufferlist[1].iov_len = bufferlen;
842 bufferlist[2].iov_base = &aligned_data;
843 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
845 return hv_ringbuffer_write(channel, bufferlist, 3);
847 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
850 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
851 * using a GPADL Direct packet type.
852 * The buffer includes the vmbus descriptor.
854 int vmbus_sendpacket_mpb_desc(struct vmbus_channel *channel,
855 struct vmbus_packet_mpb_array *desc,
856 u32 desc_size,
857 void *buffer, u32 bufferlen, u64 requestid)
859 u32 packetlen;
860 u32 packetlen_aligned;
861 struct kvec bufferlist[3];
862 u64 aligned_data = 0;
864 packetlen = desc_size + bufferlen;
865 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
867 /* Setup the descriptor */
868 desc->type = VM_PKT_DATA_USING_GPA_DIRECT;
869 desc->flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
870 desc->dataoffset8 = desc_size >> 3; /* in 8-bytes granularity */
871 desc->length8 = (u16)(packetlen_aligned >> 3);
872 desc->transactionid = requestid;
873 desc->reserved = 0;
874 desc->rangecount = 1;
876 bufferlist[0].iov_base = desc;
877 bufferlist[0].iov_len = desc_size;
878 bufferlist[1].iov_base = buffer;
879 bufferlist[1].iov_len = bufferlen;
880 bufferlist[2].iov_base = &aligned_data;
881 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
883 return hv_ringbuffer_write(channel, bufferlist, 3);
885 EXPORT_SYMBOL_GPL(vmbus_sendpacket_mpb_desc);
888 * __vmbus_recvpacket() - Retrieve the user packet on the specified channel
889 * @channel: Pointer to vmbus_channel structure
890 * @buffer: Pointer to the buffer you want to receive the data into.
891 * @bufferlen: Maximum size of what the buffer can hold.
892 * @buffer_actual_len: The actual size of the data after it was received.
893 * @requestid: Identifier of the request
894 * @raw: true means keep the vmpacket_descriptor header in the received data.
896 * Receives directly from the hyper-v vmbus and puts the data it received
897 * into Buffer. This will receive the data unparsed from hyper-v.
899 * Mainly used by Hyper-V drivers.
901 static inline int
902 __vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
903 u32 bufferlen, u32 *buffer_actual_len, u64 *requestid,
904 bool raw)
906 return hv_ringbuffer_read(channel, buffer, bufferlen,
907 buffer_actual_len, requestid, raw);
911 int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
912 u32 bufferlen, u32 *buffer_actual_len,
913 u64 *requestid)
915 return __vmbus_recvpacket(channel, buffer, bufferlen,
916 buffer_actual_len, requestid, false);
918 EXPORT_SYMBOL(vmbus_recvpacket);
921 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
923 int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
924 u32 bufferlen, u32 *buffer_actual_len,
925 u64 *requestid)
927 return __vmbus_recvpacket(channel, buffer, bufferlen,
928 buffer_actual_len, requestid, true);
930 EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);