Linux 4.19.133
[linux/fpc-iii.git] / drivers / hv / channel.c
blob5e515533e9cdbccdb73860eb92517a572c86f7dd
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 phys_addr_t 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);
83 * vmbus_open - Open the specified channel.
85 int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size,
86 u32 recv_ringbuffer_size, void *userdata, u32 userdatalen,
87 void (*onchannelcallback)(void *context), void *context)
89 struct vmbus_channel_open_channel *open_msg;
90 struct vmbus_channel_msginfo *open_info = NULL;
91 unsigned long flags;
92 int ret, err = 0;
93 struct page *page;
94 unsigned int order;
96 if (send_ringbuffer_size % PAGE_SIZE ||
97 recv_ringbuffer_size % PAGE_SIZE)
98 return -EINVAL;
100 order = get_order(send_ringbuffer_size + recv_ringbuffer_size);
102 spin_lock_irqsave(&newchannel->lock, flags);
103 if (newchannel->state == CHANNEL_OPEN_STATE) {
104 newchannel->state = CHANNEL_OPENING_STATE;
105 } else {
106 spin_unlock_irqrestore(&newchannel->lock, flags);
107 return -EINVAL;
109 spin_unlock_irqrestore(&newchannel->lock, flags);
111 newchannel->onchannel_callback = onchannelcallback;
112 newchannel->channel_callback_context = context;
114 /* Allocate the ring buffer */
115 page = alloc_pages_node(cpu_to_node(newchannel->target_cpu),
116 GFP_KERNEL|__GFP_ZERO, order);
118 if (!page)
119 page = alloc_pages(GFP_KERNEL|__GFP_ZERO, order);
121 if (!page) {
122 err = -ENOMEM;
123 goto error_set_chnstate;
126 newchannel->ringbuffer_page = page;
127 newchannel->ringbuffer_pagecount = (send_ringbuffer_size +
128 recv_ringbuffer_size) >> PAGE_SHIFT;
130 ret = hv_ringbuffer_init(&newchannel->outbound, page,
131 send_ringbuffer_size >> PAGE_SHIFT);
133 if (ret != 0) {
134 err = ret;
135 goto error_free_pages;
138 ret = hv_ringbuffer_init(&newchannel->inbound,
139 &page[send_ringbuffer_size >> PAGE_SHIFT],
140 recv_ringbuffer_size >> PAGE_SHIFT);
141 if (ret != 0) {
142 err = ret;
143 goto error_free_pages;
147 /* Establish the gpadl for the ring buffer */
148 newchannel->ringbuffer_gpadlhandle = 0;
150 ret = vmbus_establish_gpadl(newchannel,
151 page_address(page),
152 send_ringbuffer_size +
153 recv_ringbuffer_size,
154 &newchannel->ringbuffer_gpadlhandle);
156 if (ret != 0) {
157 err = ret;
158 goto error_free_pages;
161 /* Create and init the channel open message */
162 open_info = kmalloc(sizeof(*open_info) +
163 sizeof(struct vmbus_channel_open_channel),
164 GFP_KERNEL);
165 if (!open_info) {
166 err = -ENOMEM;
167 goto error_free_gpadl;
170 init_completion(&open_info->waitevent);
171 open_info->waiting_channel = newchannel;
173 open_msg = (struct vmbus_channel_open_channel *)open_info->msg;
174 open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL;
175 open_msg->openid = newchannel->offermsg.child_relid;
176 open_msg->child_relid = newchannel->offermsg.child_relid;
177 open_msg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle;
178 open_msg->downstream_ringbuffer_pageoffset = send_ringbuffer_size >>
179 PAGE_SHIFT;
180 open_msg->target_vp = newchannel->target_vp;
182 if (userdatalen > MAX_USER_DEFINED_BYTES) {
183 err = -EINVAL;
184 goto error_free_gpadl;
187 if (userdatalen)
188 memcpy(open_msg->userdata, userdata, userdatalen);
190 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
191 list_add_tail(&open_info->msglistentry,
192 &vmbus_connection.chn_msg_list);
193 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
195 if (newchannel->rescind) {
196 err = -ENODEV;
197 goto error_free_gpadl;
200 ret = vmbus_post_msg(open_msg,
201 sizeof(struct vmbus_channel_open_channel), true);
203 trace_vmbus_open(open_msg, ret);
205 if (ret != 0) {
206 err = ret;
207 goto error_clean_msglist;
210 wait_for_completion(&open_info->waitevent);
212 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
213 list_del(&open_info->msglistentry);
214 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
216 if (newchannel->rescind) {
217 err = -ENODEV;
218 goto error_free_gpadl;
221 if (open_info->response.open_result.status) {
222 err = -EAGAIN;
223 goto error_free_gpadl;
226 newchannel->state = CHANNEL_OPENED_STATE;
227 kfree(open_info);
228 return 0;
230 error_clean_msglist:
231 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
232 list_del(&open_info->msglistentry);
233 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
235 error_free_gpadl:
236 vmbus_teardown_gpadl(newchannel, newchannel->ringbuffer_gpadlhandle);
237 kfree(open_info);
238 error_free_pages:
239 hv_ringbuffer_cleanup(&newchannel->outbound);
240 hv_ringbuffer_cleanup(&newchannel->inbound);
241 __free_pages(page, order);
242 error_set_chnstate:
243 newchannel->state = CHANNEL_OPEN_STATE;
244 return err;
246 EXPORT_SYMBOL_GPL(vmbus_open);
248 /* Used for Hyper-V Socket: a guest client's connect() to the host */
249 int vmbus_send_tl_connect_request(const uuid_le *shv_guest_servie_id,
250 const uuid_le *shv_host_servie_id)
252 struct vmbus_channel_tl_connect_request conn_msg;
253 int ret;
255 memset(&conn_msg, 0, sizeof(conn_msg));
256 conn_msg.header.msgtype = CHANNELMSG_TL_CONNECT_REQUEST;
257 conn_msg.guest_endpoint_id = *shv_guest_servie_id;
258 conn_msg.host_service_id = *shv_host_servie_id;
260 ret = vmbus_post_msg(&conn_msg, sizeof(conn_msg), true);
262 trace_vmbus_send_tl_connect_request(&conn_msg, ret);
264 return ret;
266 EXPORT_SYMBOL_GPL(vmbus_send_tl_connect_request);
269 * create_gpadl_header - Creates a gpadl for the specified buffer
271 static int create_gpadl_header(void *kbuffer, u32 size,
272 struct vmbus_channel_msginfo **msginfo)
274 int i;
275 int pagecount;
276 struct vmbus_channel_gpadl_header *gpadl_header;
277 struct vmbus_channel_gpadl_body *gpadl_body;
278 struct vmbus_channel_msginfo *msgheader;
279 struct vmbus_channel_msginfo *msgbody = NULL;
280 u32 msgsize;
282 int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
284 pagecount = size >> PAGE_SHIFT;
286 /* do we need a gpadl body msg */
287 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
288 sizeof(struct vmbus_channel_gpadl_header) -
289 sizeof(struct gpa_range);
290 pfncount = pfnsize / sizeof(u64);
292 if (pagecount > pfncount) {
293 /* we need a gpadl body */
294 /* fill in the header */
295 msgsize = sizeof(struct vmbus_channel_msginfo) +
296 sizeof(struct vmbus_channel_gpadl_header) +
297 sizeof(struct gpa_range) + pfncount * sizeof(u64);
298 msgheader = kzalloc(msgsize, GFP_KERNEL);
299 if (!msgheader)
300 goto nomem;
302 INIT_LIST_HEAD(&msgheader->submsglist);
303 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 < pfncount; i++)
313 gpadl_header->range[0].pfn_array[i] = virt_to_hvpfn(
314 kbuffer + PAGE_SIZE * i);
315 *msginfo = msgheader;
317 pfnsum = pfncount;
318 pfnleft = pagecount - pfncount;
320 /* how many pfns can we fit */
321 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
322 sizeof(struct vmbus_channel_gpadl_body);
323 pfncount = pfnsize / sizeof(u64);
325 /* fill in the body */
326 while (pfnleft) {
327 if (pfnleft > pfncount)
328 pfncurr = pfncount;
329 else
330 pfncurr = pfnleft;
332 msgsize = sizeof(struct vmbus_channel_msginfo) +
333 sizeof(struct vmbus_channel_gpadl_body) +
334 pfncurr * sizeof(u64);
335 msgbody = kzalloc(msgsize, GFP_KERNEL);
337 if (!msgbody) {
338 struct vmbus_channel_msginfo *pos = NULL;
339 struct vmbus_channel_msginfo *tmp = NULL;
341 * Free up all the allocated messages.
343 list_for_each_entry_safe(pos, tmp,
344 &msgheader->submsglist,
345 msglistentry) {
347 list_del(&pos->msglistentry);
348 kfree(pos);
351 goto nomem;
354 msgbody->msgsize = msgsize;
355 gpadl_body =
356 (struct vmbus_channel_gpadl_body *)msgbody->msg;
359 * Gpadl is u32 and we are using a pointer which could
360 * be 64-bit
361 * This is governed by the guest/host protocol and
362 * so the hypervisor guarantees that this is ok.
364 for (i = 0; i < pfncurr; i++)
365 gpadl_body->pfn[i] = virt_to_hvpfn(
366 kbuffer + PAGE_SIZE * (pfnsum + i));
368 /* add to msg header */
369 list_add_tail(&msgbody->msglistentry,
370 &msgheader->submsglist);
371 pfnsum += pfncurr;
372 pfnleft -= pfncurr;
374 } else {
375 /* everything fits in a header */
376 msgsize = sizeof(struct vmbus_channel_msginfo) +
377 sizeof(struct vmbus_channel_gpadl_header) +
378 sizeof(struct gpa_range) + pagecount * sizeof(u64);
379 msgheader = kzalloc(msgsize, GFP_KERNEL);
380 if (msgheader == NULL)
381 goto nomem;
383 INIT_LIST_HEAD(&msgheader->submsglist);
384 msgheader->msgsize = msgsize;
386 gpadl_header = (struct vmbus_channel_gpadl_header *)
387 msgheader->msg;
388 gpadl_header->rangecount = 1;
389 gpadl_header->range_buflen = sizeof(struct gpa_range) +
390 pagecount * sizeof(u64);
391 gpadl_header->range[0].byte_offset = 0;
392 gpadl_header->range[0].byte_count = size;
393 for (i = 0; i < pagecount; i++)
394 gpadl_header->range[0].pfn_array[i] = virt_to_hvpfn(
395 kbuffer + PAGE_SIZE * i);
397 *msginfo = msgheader;
400 return 0;
401 nomem:
402 kfree(msgheader);
403 kfree(msgbody);
404 return -ENOMEM;
408 * vmbus_establish_gpadl - Establish a GPADL for the specified buffer
410 * @channel: a channel
411 * @kbuffer: from kmalloc or vmalloc
412 * @size: page-size multiple
413 * @gpadl_handle: some funky thing
415 int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
416 u32 size, u32 *gpadl_handle)
418 struct vmbus_channel_gpadl_header *gpadlmsg;
419 struct vmbus_channel_gpadl_body *gpadl_body;
420 struct vmbus_channel_msginfo *msginfo = NULL;
421 struct vmbus_channel_msginfo *submsginfo, *tmp;
422 struct list_head *curr;
423 u32 next_gpadl_handle;
424 unsigned long flags;
425 int ret = 0;
427 next_gpadl_handle =
428 (atomic_inc_return(&vmbus_connection.next_gpadl_handle) - 1);
430 ret = create_gpadl_header(kbuffer, size, &msginfo);
431 if (ret)
432 return ret;
434 init_completion(&msginfo->waitevent);
435 msginfo->waiting_channel = channel;
437 gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
438 gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
439 gpadlmsg->child_relid = channel->offermsg.child_relid;
440 gpadlmsg->gpadl = next_gpadl_handle;
443 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
444 list_add_tail(&msginfo->msglistentry,
445 &vmbus_connection.chn_msg_list);
447 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
449 if (channel->rescind) {
450 ret = -ENODEV;
451 goto cleanup;
454 ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
455 sizeof(*msginfo), true);
457 trace_vmbus_establish_gpadl_header(gpadlmsg, ret);
459 if (ret != 0)
460 goto cleanup;
462 list_for_each(curr, &msginfo->submsglist) {
463 submsginfo = (struct vmbus_channel_msginfo *)curr;
464 gpadl_body =
465 (struct vmbus_channel_gpadl_body *)submsginfo->msg;
467 gpadl_body->header.msgtype =
468 CHANNELMSG_GPADL_BODY;
469 gpadl_body->gpadl = next_gpadl_handle;
471 ret = vmbus_post_msg(gpadl_body,
472 submsginfo->msgsize - sizeof(*submsginfo),
473 true);
475 trace_vmbus_establish_gpadl_body(gpadl_body, ret);
477 if (ret != 0)
478 goto cleanup;
481 wait_for_completion(&msginfo->waitevent);
483 if (msginfo->response.gpadl_created.creation_status != 0) {
484 pr_err("Failed to establish GPADL: err = 0x%x\n",
485 msginfo->response.gpadl_created.creation_status);
487 ret = -EDQUOT;
488 goto cleanup;
491 if (channel->rescind) {
492 ret = -ENODEV;
493 goto cleanup;
496 /* At this point, we received the gpadl created msg */
497 *gpadl_handle = gpadlmsg->gpadl;
499 cleanup:
500 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
501 list_del(&msginfo->msglistentry);
502 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
503 list_for_each_entry_safe(submsginfo, tmp, &msginfo->submsglist,
504 msglistentry) {
505 kfree(submsginfo);
508 kfree(msginfo);
509 return ret;
511 EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
514 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
516 int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
518 struct vmbus_channel_gpadl_teardown *msg;
519 struct vmbus_channel_msginfo *info;
520 unsigned long flags;
521 int ret;
523 info = kmalloc(sizeof(*info) +
524 sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
525 if (!info)
526 return -ENOMEM;
528 init_completion(&info->waitevent);
529 info->waiting_channel = channel;
531 msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
533 msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
534 msg->child_relid = channel->offermsg.child_relid;
535 msg->gpadl = gpadl_handle;
537 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
538 list_add_tail(&info->msglistentry,
539 &vmbus_connection.chn_msg_list);
540 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
542 if (channel->rescind)
543 goto post_msg_err;
545 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_gpadl_teardown),
546 true);
548 trace_vmbus_teardown_gpadl(msg, ret);
550 if (ret)
551 goto post_msg_err;
553 wait_for_completion(&info->waitevent);
555 post_msg_err:
557 * If the channel has been rescinded;
558 * we will be awakened by the rescind
559 * handler; set the error code to zero so we don't leak memory.
561 if (channel->rescind)
562 ret = 0;
564 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
565 list_del(&info->msglistentry);
566 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
568 kfree(info);
569 return ret;
571 EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
573 static void reset_channel_cb(void *arg)
575 struct vmbus_channel *channel = arg;
577 channel->onchannel_callback = NULL;
580 void vmbus_reset_channel_cb(struct vmbus_channel *channel)
583 * vmbus_on_event(), running in the per-channel tasklet, can race
584 * with vmbus_close_internal() in the case of SMP guest, e.g., when
585 * the former is accessing channel->inbound.ring_buffer, the latter
586 * could be freeing the ring_buffer pages, so here we must stop it
587 * first.
589 tasklet_disable(&channel->callback_event);
591 channel->sc_creation_callback = NULL;
593 /* Stop the callback asap */
594 if (channel->target_cpu != get_cpu()) {
595 put_cpu();
596 smp_call_function_single(channel->target_cpu, reset_channel_cb,
597 channel, true);
598 } else {
599 reset_channel_cb(channel);
600 put_cpu();
603 /* Re-enable tasklet for use on re-open */
604 tasklet_enable(&channel->callback_event);
607 static int vmbus_close_internal(struct vmbus_channel *channel)
609 struct vmbus_channel_close_channel *msg;
610 int ret;
612 vmbus_reset_channel_cb(channel);
615 * In case a device driver's probe() fails (e.g.,
616 * util_probe() -> vmbus_open() returns -ENOMEM) and the device is
617 * rescinded later (e.g., we dynamically disable an Integrated Service
618 * in Hyper-V Manager), the driver's remove() invokes vmbus_close():
619 * here we should skip most of the below cleanup work.
621 if (channel->state != CHANNEL_OPENED_STATE) {
622 ret = -EINVAL;
623 goto out;
626 channel->state = CHANNEL_OPEN_STATE;
628 /* Send a closing message */
630 msg = &channel->close_msg.msg;
632 msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
633 msg->child_relid = channel->offermsg.child_relid;
635 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel),
636 true);
638 trace_vmbus_close_internal(msg, ret);
640 if (ret) {
641 pr_err("Close failed: close post msg return is %d\n", ret);
643 * If we failed to post the close msg,
644 * it is perhaps better to leak memory.
646 goto out;
649 /* Tear down the gpadl for the channel's ring buffer */
650 if (channel->ringbuffer_gpadlhandle) {
651 ret = vmbus_teardown_gpadl(channel,
652 channel->ringbuffer_gpadlhandle);
653 if (ret) {
654 pr_err("Close failed: teardown gpadl return %d\n", ret);
656 * If we failed to teardown gpadl,
657 * it is perhaps better to leak memory.
659 goto out;
663 /* Cleanup the ring buffers for this channel */
664 hv_ringbuffer_cleanup(&channel->outbound);
665 hv_ringbuffer_cleanup(&channel->inbound);
667 __free_pages(channel->ringbuffer_page,
668 get_order(channel->ringbuffer_pagecount << PAGE_SHIFT));
670 out:
671 return ret;
675 * vmbus_close - Close the specified channel
677 void vmbus_close(struct vmbus_channel *channel)
679 struct list_head *cur, *tmp;
680 struct vmbus_channel *cur_channel;
682 if (channel->primary_channel != NULL) {
684 * We will only close sub-channels when
685 * the primary is closed.
687 return;
690 * Close all the sub-channels first and then close the
691 * primary channel.
693 list_for_each_safe(cur, tmp, &channel->sc_list) {
694 cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
695 if (cur_channel->rescind) {
696 wait_for_completion(&cur_channel->rescind_event);
697 mutex_lock(&vmbus_connection.channel_mutex);
698 vmbus_close_internal(cur_channel);
699 hv_process_channel_removal(
700 cur_channel->offermsg.child_relid);
701 } else {
702 mutex_lock(&vmbus_connection.channel_mutex);
703 vmbus_close_internal(cur_channel);
705 mutex_unlock(&vmbus_connection.channel_mutex);
708 * Now close the primary.
710 mutex_lock(&vmbus_connection.channel_mutex);
711 vmbus_close_internal(channel);
712 mutex_unlock(&vmbus_connection.channel_mutex);
714 EXPORT_SYMBOL_GPL(vmbus_close);
717 * vmbus_sendpacket() - Send the specified buffer on the given channel
718 * @channel: Pointer to vmbus_channel structure.
719 * @buffer: Pointer to the buffer you want to receive the data into.
720 * @bufferlen: Maximum size of what the the buffer will hold
721 * @requestid: Identifier of the request
722 * @type: Type of packet that is being send e.g. negotiate, time
723 * packet etc.
725 * Sends data in @buffer directly to hyper-v via the vmbus
726 * This will send the data unparsed to hyper-v.
728 * Mainly used by Hyper-V drivers.
730 int vmbus_sendpacket(struct vmbus_channel *channel, void *buffer,
731 u32 bufferlen, u64 requestid,
732 enum vmbus_packet_type type, u32 flags)
734 struct vmpacket_descriptor desc;
735 u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
736 u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
737 struct kvec bufferlist[3];
738 u64 aligned_data = 0;
739 int num_vecs = ((bufferlen != 0) ? 3 : 1);
742 /* Setup the descriptor */
743 desc.type = type; /* VmbusPacketTypeDataInBand; */
744 desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
745 /* in 8-bytes granularity */
746 desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
747 desc.len8 = (u16)(packetlen_aligned >> 3);
748 desc.trans_id = requestid;
750 bufferlist[0].iov_base = &desc;
751 bufferlist[0].iov_len = sizeof(struct vmpacket_descriptor);
752 bufferlist[1].iov_base = buffer;
753 bufferlist[1].iov_len = bufferlen;
754 bufferlist[2].iov_base = &aligned_data;
755 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
757 return hv_ringbuffer_write(channel, bufferlist, num_vecs);
759 EXPORT_SYMBOL(vmbus_sendpacket);
762 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
763 * packets using a GPADL Direct packet type. This interface allows you
764 * to control notifying the host. This will be useful for sending
765 * batched data. Also the sender can control the send flags
766 * explicitly.
768 int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
769 struct hv_page_buffer pagebuffers[],
770 u32 pagecount, void *buffer, u32 bufferlen,
771 u64 requestid)
773 int i;
774 struct vmbus_channel_packet_page_buffer desc;
775 u32 descsize;
776 u32 packetlen;
777 u32 packetlen_aligned;
778 struct kvec bufferlist[3];
779 u64 aligned_data = 0;
781 if (pagecount > MAX_PAGE_BUFFER_COUNT)
782 return -EINVAL;
785 * Adjust the size down since vmbus_channel_packet_page_buffer is the
786 * largest size we support
788 descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
789 ((MAX_PAGE_BUFFER_COUNT - pagecount) *
790 sizeof(struct hv_page_buffer));
791 packetlen = descsize + bufferlen;
792 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
794 /* Setup the descriptor */
795 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
796 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
797 desc.dataoffset8 = descsize >> 3; /* in 8-bytes granularity */
798 desc.length8 = (u16)(packetlen_aligned >> 3);
799 desc.transactionid = requestid;
800 desc.reserved = 0;
801 desc.rangecount = pagecount;
803 for (i = 0; i < pagecount; i++) {
804 desc.range[i].len = pagebuffers[i].len;
805 desc.range[i].offset = pagebuffers[i].offset;
806 desc.range[i].pfn = pagebuffers[i].pfn;
809 bufferlist[0].iov_base = &desc;
810 bufferlist[0].iov_len = descsize;
811 bufferlist[1].iov_base = buffer;
812 bufferlist[1].iov_len = bufferlen;
813 bufferlist[2].iov_base = &aligned_data;
814 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
816 return hv_ringbuffer_write(channel, bufferlist, 3);
818 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
821 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
822 * using a GPADL Direct packet type.
823 * The buffer includes the vmbus descriptor.
825 int vmbus_sendpacket_mpb_desc(struct vmbus_channel *channel,
826 struct vmbus_packet_mpb_array *desc,
827 u32 desc_size,
828 void *buffer, u32 bufferlen, u64 requestid)
830 u32 packetlen;
831 u32 packetlen_aligned;
832 struct kvec bufferlist[3];
833 u64 aligned_data = 0;
835 packetlen = desc_size + bufferlen;
836 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
838 /* Setup the descriptor */
839 desc->type = VM_PKT_DATA_USING_GPA_DIRECT;
840 desc->flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
841 desc->dataoffset8 = desc_size >> 3; /* in 8-bytes granularity */
842 desc->length8 = (u16)(packetlen_aligned >> 3);
843 desc->transactionid = requestid;
844 desc->reserved = 0;
845 desc->rangecount = 1;
847 bufferlist[0].iov_base = desc;
848 bufferlist[0].iov_len = desc_size;
849 bufferlist[1].iov_base = buffer;
850 bufferlist[1].iov_len = bufferlen;
851 bufferlist[2].iov_base = &aligned_data;
852 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
854 return hv_ringbuffer_write(channel, bufferlist, 3);
856 EXPORT_SYMBOL_GPL(vmbus_sendpacket_mpb_desc);
859 * vmbus_recvpacket() - Retrieve the user packet on the specified channel
860 * @channel: Pointer to vmbus_channel structure.
861 * @buffer: Pointer to the buffer you want to receive the data into.
862 * @bufferlen: Maximum size of what the the buffer will hold
863 * @buffer_actual_len: The actual size of the data after it was received
864 * @requestid: Identifier of the request
866 * Receives directly from the hyper-v vmbus and puts the data it received
867 * into Buffer. This will receive the data unparsed from hyper-v.
869 * Mainly used by Hyper-V drivers.
871 static inline int
872 __vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
873 u32 bufferlen, u32 *buffer_actual_len, u64 *requestid,
874 bool raw)
876 return hv_ringbuffer_read(channel, buffer, bufferlen,
877 buffer_actual_len, requestid, raw);
881 int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
882 u32 bufferlen, u32 *buffer_actual_len,
883 u64 *requestid)
885 return __vmbus_recvpacket(channel, buffer, bufferlen,
886 buffer_actual_len, requestid, false);
888 EXPORT_SYMBOL(vmbus_recvpacket);
891 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
893 int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
894 u32 bufferlen, u32 *buffer_actual_len,
895 u64 *requestid)
897 return __vmbus_recvpacket(channel, buffer, bufferlen,
898 buffer_actual_len, requestid, true);
900 EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);