Lynx framebuffers multidomain implementation.
[linux/elbrus.git] / drivers / hv / channel.c
blob356f22f90a05fad4cab48a8a4bd72f1e7bf03158
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>
31 #include "hyperv_vmbus.h"
33 #define NUM_PAGES_SPANNED(addr, len) \
34 ((PAGE_ALIGN(addr + len) >> PAGE_SHIFT) - (addr >> PAGE_SHIFT))
37 * vmbus_setevent- Trigger an event notification on the specified
38 * channel.
40 static void vmbus_setevent(struct vmbus_channel *channel)
42 struct hv_monitor_page *monitorpage;
44 if (channel->offermsg.monitor_allocated) {
45 /* Each u32 represents 32 channels */
46 sync_set_bit(channel->offermsg.child_relid & 31,
47 (unsigned long *) vmbus_connection.send_int_page +
48 (channel->offermsg.child_relid >> 5));
50 /* Get the child to parent monitor page */
51 monitorpage = vmbus_connection.monitor_pages[1];
53 sync_set_bit(channel->monitor_bit,
54 (unsigned long *)&monitorpage->trigger_group
55 [channel->monitor_grp].pending);
57 } else {
58 vmbus_set_event(channel);
63 * vmbus_open - Open the specified channel.
65 int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size,
66 u32 recv_ringbuffer_size, void *userdata, u32 userdatalen,
67 void (*onchannelcallback)(void *context), void *context)
69 struct vmbus_channel_open_channel *open_msg;
70 struct vmbus_channel_msginfo *open_info = NULL;
71 void *in, *out;
72 unsigned long flags;
73 int ret, t, err = 0;
75 spin_lock_irqsave(&newchannel->sc_lock, flags);
76 if (newchannel->state == CHANNEL_OPEN_STATE) {
77 newchannel->state = CHANNEL_OPENING_STATE;
78 } else {
79 spin_unlock_irqrestore(&newchannel->sc_lock, flags);
80 return -EINVAL;
82 spin_unlock_irqrestore(&newchannel->sc_lock, flags);
84 newchannel->onchannel_callback = onchannelcallback;
85 newchannel->channel_callback_context = context;
87 /* Allocate the ring buffer */
88 out = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO,
89 get_order(send_ringbuffer_size + recv_ringbuffer_size));
91 if (!out)
92 return -ENOMEM;
95 in = (void *)((unsigned long)out + send_ringbuffer_size);
97 newchannel->ringbuffer_pages = out;
98 newchannel->ringbuffer_pagecount = (send_ringbuffer_size +
99 recv_ringbuffer_size) >> PAGE_SHIFT;
101 ret = hv_ringbuffer_init(
102 &newchannel->outbound, out, send_ringbuffer_size);
104 if (ret != 0) {
105 err = ret;
106 goto error0;
109 ret = hv_ringbuffer_init(
110 &newchannel->inbound, in, recv_ringbuffer_size);
111 if (ret != 0) {
112 err = ret;
113 goto error0;
117 /* Establish the gpadl for the ring buffer */
118 newchannel->ringbuffer_gpadlhandle = 0;
120 ret = vmbus_establish_gpadl(newchannel,
121 newchannel->outbound.ring_buffer,
122 send_ringbuffer_size +
123 recv_ringbuffer_size,
124 &newchannel->ringbuffer_gpadlhandle);
126 if (ret != 0) {
127 err = ret;
128 goto error0;
131 /* Create and init the channel open message */
132 open_info = kmalloc(sizeof(*open_info) +
133 sizeof(struct vmbus_channel_open_channel),
134 GFP_KERNEL);
135 if (!open_info) {
136 err = -ENOMEM;
137 goto error_gpadl;
140 init_completion(&open_info->waitevent);
142 open_msg = (struct vmbus_channel_open_channel *)open_info->msg;
143 open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL;
144 open_msg->openid = newchannel->offermsg.child_relid;
145 open_msg->child_relid = newchannel->offermsg.child_relid;
146 open_msg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle;
147 open_msg->downstream_ringbuffer_pageoffset = send_ringbuffer_size >>
148 PAGE_SHIFT;
149 open_msg->target_vp = newchannel->target_vp;
151 if (userdatalen > MAX_USER_DEFINED_BYTES) {
152 err = -EINVAL;
153 goto error_gpadl;
156 if (userdatalen)
157 memcpy(open_msg->userdata, userdata, userdatalen);
159 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
160 list_add_tail(&open_info->msglistentry,
161 &vmbus_connection.chn_msg_list);
162 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
164 ret = vmbus_post_msg(open_msg,
165 sizeof(struct vmbus_channel_open_channel));
167 if (ret != 0) {
168 err = ret;
169 goto error1;
172 t = wait_for_completion_timeout(&open_info->waitevent, 5*HZ);
173 if (t == 0) {
174 err = -ETIMEDOUT;
175 goto error1;
179 if (open_info->response.open_result.status)
180 err = open_info->response.open_result.status;
182 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
183 list_del(&open_info->msglistentry);
184 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
186 if (err == 0)
187 newchannel->state = CHANNEL_OPENED_STATE;
189 kfree(open_info);
190 return err;
192 error1:
193 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
194 list_del(&open_info->msglistentry);
195 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
197 error_gpadl:
198 vmbus_teardown_gpadl(newchannel, newchannel->ringbuffer_gpadlhandle);
200 error0:
201 free_pages((unsigned long)out,
202 get_order(send_ringbuffer_size + recv_ringbuffer_size));
203 kfree(open_info);
204 return err;
206 EXPORT_SYMBOL_GPL(vmbus_open);
209 * create_gpadl_header - Creates a gpadl for the specified buffer
211 static int create_gpadl_header(void *kbuffer, u32 size,
212 struct vmbus_channel_msginfo **msginfo,
213 u32 *messagecount)
215 int i;
216 int pagecount;
217 struct vmbus_channel_gpadl_header *gpadl_header;
218 struct vmbus_channel_gpadl_body *gpadl_body;
219 struct vmbus_channel_msginfo *msgheader;
220 struct vmbus_channel_msginfo *msgbody = NULL;
221 u32 msgsize;
223 int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
225 pagecount = size >> PAGE_SHIFT;
227 /* do we need a gpadl body msg */
228 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
229 sizeof(struct vmbus_channel_gpadl_header) -
230 sizeof(struct gpa_range);
231 pfncount = pfnsize / sizeof(u64);
233 if (pagecount > pfncount) {
234 /* we need a gpadl body */
235 /* fill in the header */
236 msgsize = sizeof(struct vmbus_channel_msginfo) +
237 sizeof(struct vmbus_channel_gpadl_header) +
238 sizeof(struct gpa_range) + pfncount * sizeof(u64);
239 msgheader = kzalloc(msgsize, GFP_KERNEL);
240 if (!msgheader)
241 goto nomem;
243 INIT_LIST_HEAD(&msgheader->submsglist);
244 msgheader->msgsize = msgsize;
246 gpadl_header = (struct vmbus_channel_gpadl_header *)
247 msgheader->msg;
248 gpadl_header->rangecount = 1;
249 gpadl_header->range_buflen = sizeof(struct gpa_range) +
250 pagecount * sizeof(u64);
251 gpadl_header->range[0].byte_offset = 0;
252 gpadl_header->range[0].byte_count = size;
253 for (i = 0; i < pfncount; i++)
254 gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys(
255 kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT;
256 *msginfo = msgheader;
257 *messagecount = 1;
259 pfnsum = pfncount;
260 pfnleft = pagecount - pfncount;
262 /* how many pfns can we fit */
263 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
264 sizeof(struct vmbus_channel_gpadl_body);
265 pfncount = pfnsize / sizeof(u64);
267 /* fill in the body */
268 while (pfnleft) {
269 if (pfnleft > pfncount)
270 pfncurr = pfncount;
271 else
272 pfncurr = pfnleft;
274 msgsize = sizeof(struct vmbus_channel_msginfo) +
275 sizeof(struct vmbus_channel_gpadl_body) +
276 pfncurr * sizeof(u64);
277 msgbody = kzalloc(msgsize, GFP_KERNEL);
279 if (!msgbody) {
280 struct vmbus_channel_msginfo *pos = NULL;
281 struct vmbus_channel_msginfo *tmp = NULL;
283 * Free up all the allocated messages.
285 list_for_each_entry_safe(pos, tmp,
286 &msgheader->submsglist,
287 msglistentry) {
289 list_del(&pos->msglistentry);
290 kfree(pos);
293 goto nomem;
296 msgbody->msgsize = msgsize;
297 (*messagecount)++;
298 gpadl_body =
299 (struct vmbus_channel_gpadl_body *)msgbody->msg;
302 * Gpadl is u32 and we are using a pointer which could
303 * be 64-bit
304 * This is governed by the guest/host protocol and
305 * so the hypervisor gurantees that this is ok.
307 for (i = 0; i < pfncurr; i++)
308 gpadl_body->pfn[i] = slow_virt_to_phys(
309 kbuffer + PAGE_SIZE * (pfnsum + i)) >>
310 PAGE_SHIFT;
312 /* add to msg header */
313 list_add_tail(&msgbody->msglistentry,
314 &msgheader->submsglist);
315 pfnsum += pfncurr;
316 pfnleft -= pfncurr;
318 } else {
319 /* everything fits in a header */
320 msgsize = sizeof(struct vmbus_channel_msginfo) +
321 sizeof(struct vmbus_channel_gpadl_header) +
322 sizeof(struct gpa_range) + pagecount * sizeof(u64);
323 msgheader = kzalloc(msgsize, GFP_KERNEL);
324 if (msgheader == NULL)
325 goto nomem;
326 msgheader->msgsize = msgsize;
328 gpadl_header = (struct vmbus_channel_gpadl_header *)
329 msgheader->msg;
330 gpadl_header->rangecount = 1;
331 gpadl_header->range_buflen = sizeof(struct gpa_range) +
332 pagecount * sizeof(u64);
333 gpadl_header->range[0].byte_offset = 0;
334 gpadl_header->range[0].byte_count = size;
335 for (i = 0; i < pagecount; i++)
336 gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys(
337 kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT;
339 *msginfo = msgheader;
340 *messagecount = 1;
343 return 0;
344 nomem:
345 kfree(msgheader);
346 kfree(msgbody);
347 return -ENOMEM;
351 * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
353 * @channel: a channel
354 * @kbuffer: from kmalloc or vmalloc
355 * @size: page-size multiple
356 * @gpadl_handle: some funky thing
358 int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
359 u32 size, u32 *gpadl_handle)
361 struct vmbus_channel_gpadl_header *gpadlmsg;
362 struct vmbus_channel_gpadl_body *gpadl_body;
363 struct vmbus_channel_msginfo *msginfo = NULL;
364 struct vmbus_channel_msginfo *submsginfo;
365 u32 msgcount;
366 struct list_head *curr;
367 u32 next_gpadl_handle;
368 unsigned long flags;
369 int ret = 0;
371 next_gpadl_handle = atomic_read(&vmbus_connection.next_gpadl_handle);
372 atomic_inc(&vmbus_connection.next_gpadl_handle);
374 ret = create_gpadl_header(kbuffer, size, &msginfo, &msgcount);
375 if (ret)
376 return ret;
378 init_completion(&msginfo->waitevent);
380 gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
381 gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
382 gpadlmsg->child_relid = channel->offermsg.child_relid;
383 gpadlmsg->gpadl = next_gpadl_handle;
386 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
387 list_add_tail(&msginfo->msglistentry,
388 &vmbus_connection.chn_msg_list);
390 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
392 ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
393 sizeof(*msginfo));
394 if (ret != 0)
395 goto cleanup;
397 if (msgcount > 1) {
398 list_for_each(curr, &msginfo->submsglist) {
400 submsginfo = (struct vmbus_channel_msginfo *)curr;
401 gpadl_body =
402 (struct vmbus_channel_gpadl_body *)submsginfo->msg;
404 gpadl_body->header.msgtype =
405 CHANNELMSG_GPADL_BODY;
406 gpadl_body->gpadl = next_gpadl_handle;
408 ret = vmbus_post_msg(gpadl_body,
409 submsginfo->msgsize -
410 sizeof(*submsginfo));
411 if (ret != 0)
412 goto cleanup;
416 wait_for_completion(&msginfo->waitevent);
418 /* At this point, we received the gpadl created msg */
419 *gpadl_handle = gpadlmsg->gpadl;
421 cleanup:
422 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
423 list_del(&msginfo->msglistentry);
424 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
426 kfree(msginfo);
427 return ret;
429 EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
432 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
434 int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
436 struct vmbus_channel_gpadl_teardown *msg;
437 struct vmbus_channel_msginfo *info;
438 unsigned long flags;
439 int ret;
441 info = kmalloc(sizeof(*info) +
442 sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
443 if (!info)
444 return -ENOMEM;
446 init_completion(&info->waitevent);
448 msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
450 msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
451 msg->child_relid = channel->offermsg.child_relid;
452 msg->gpadl = gpadl_handle;
454 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
455 list_add_tail(&info->msglistentry,
456 &vmbus_connection.chn_msg_list);
457 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
458 ret = vmbus_post_msg(msg,
459 sizeof(struct vmbus_channel_gpadl_teardown));
461 if (ret)
462 goto post_msg_err;
464 wait_for_completion(&info->waitevent);
466 post_msg_err:
467 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
468 list_del(&info->msglistentry);
469 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
471 kfree(info);
472 return ret;
474 EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
476 static int vmbus_close_internal(struct vmbus_channel *channel)
478 struct vmbus_channel_close_channel *msg;
479 int ret;
480 unsigned long flags;
482 channel->state = CHANNEL_OPEN_STATE;
483 channel->sc_creation_callback = NULL;
484 /* Stop callback and cancel the timer asap */
485 spin_lock_irqsave(&channel->inbound_lock, flags);
486 channel->onchannel_callback = NULL;
487 spin_unlock_irqrestore(&channel->inbound_lock, flags);
489 /* Send a closing message */
491 msg = &channel->close_msg.msg;
493 msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
494 msg->child_relid = channel->offermsg.child_relid;
496 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel));
498 if (ret) {
499 pr_err("Close failed: close post msg return is %d\n", ret);
501 * If we failed to post the close msg,
502 * it is perhaps better to leak memory.
504 return ret;
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);
514 * If we failed to teardown gpadl,
515 * it is perhaps better to leak memory.
517 return ret;
521 /* Cleanup the ring buffers for this channel */
522 hv_ringbuffer_cleanup(&channel->outbound);
523 hv_ringbuffer_cleanup(&channel->inbound);
525 free_pages((unsigned long)channel->ringbuffer_pages,
526 get_order(channel->ringbuffer_pagecount * PAGE_SIZE));
528 return ret;
532 * vmbus_close - Close the specified channel
534 void vmbus_close(struct vmbus_channel *channel)
536 struct list_head *cur, *tmp;
537 struct vmbus_channel *cur_channel;
539 if (channel->primary_channel != NULL) {
541 * We will only close sub-channels when
542 * the primary is closed.
544 return;
547 * Close all the sub-channels first and then close the
548 * primary channel.
550 list_for_each_safe(cur, tmp, &channel->sc_list) {
551 cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
552 if (cur_channel->state != CHANNEL_OPENED_STATE)
553 continue;
554 vmbus_close_internal(cur_channel);
557 * Now close the primary.
559 vmbus_close_internal(channel);
561 EXPORT_SYMBOL_GPL(vmbus_close);
564 * vmbus_sendpacket() - Send the specified buffer on the given channel
565 * @channel: Pointer to vmbus_channel structure.
566 * @buffer: Pointer to the buffer you want to receive the data into.
567 * @bufferlen: Maximum size of what the the buffer will hold
568 * @requestid: Identifier of the request
569 * @type: Type of packet that is being send e.g. negotiate, time
570 * packet etc.
572 * Sends data in @buffer directly to hyper-v via the vmbus
573 * This will send the data unparsed to hyper-v.
575 * Mainly used by Hyper-V drivers.
577 int vmbus_sendpacket(struct vmbus_channel *channel, const void *buffer,
578 u32 bufferlen, u64 requestid,
579 enum vmbus_packet_type type, u32 flags)
581 struct vmpacket_descriptor desc;
582 u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
583 u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
584 struct scatterlist bufferlist[3];
585 u64 aligned_data = 0;
586 int ret;
587 bool signal = false;
590 /* Setup the descriptor */
591 desc.type = type; /* VmbusPacketTypeDataInBand; */
592 desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
593 /* in 8-bytes granularity */
594 desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
595 desc.len8 = (u16)(packetlen_aligned >> 3);
596 desc.trans_id = requestid;
598 sg_init_table(bufferlist, 3);
599 sg_set_buf(&bufferlist[0], &desc, sizeof(struct vmpacket_descriptor));
600 sg_set_buf(&bufferlist[1], buffer, bufferlen);
601 sg_set_buf(&bufferlist[2], &aligned_data,
602 packetlen_aligned - packetlen);
604 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal);
606 if (ret == 0 && signal)
607 vmbus_setevent(channel);
609 return ret;
611 EXPORT_SYMBOL(vmbus_sendpacket);
614 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
615 * packets using a GPADL Direct packet type.
617 int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
618 struct hv_page_buffer pagebuffers[],
619 u32 pagecount, void *buffer, u32 bufferlen,
620 u64 requestid)
622 int ret;
623 int i;
624 struct vmbus_channel_packet_page_buffer desc;
625 u32 descsize;
626 u32 packetlen;
627 u32 packetlen_aligned;
628 struct scatterlist bufferlist[3];
629 u64 aligned_data = 0;
630 bool signal = false;
632 if (pagecount > MAX_PAGE_BUFFER_COUNT)
633 return -EINVAL;
637 * Adjust the size down since vmbus_channel_packet_page_buffer is the
638 * largest size we support
640 descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
641 ((MAX_PAGE_BUFFER_COUNT - pagecount) *
642 sizeof(struct hv_page_buffer));
643 packetlen = descsize + bufferlen;
644 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
646 /* Setup the descriptor */
647 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
648 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
649 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
650 desc.length8 = (u16)(packetlen_aligned >> 3);
651 desc.transactionid = requestid;
652 desc.rangecount = pagecount;
654 for (i = 0; i < pagecount; i++) {
655 desc.range[i].len = pagebuffers[i].len;
656 desc.range[i].offset = pagebuffers[i].offset;
657 desc.range[i].pfn = pagebuffers[i].pfn;
660 sg_init_table(bufferlist, 3);
661 sg_set_buf(&bufferlist[0], &desc, descsize);
662 sg_set_buf(&bufferlist[1], buffer, bufferlen);
663 sg_set_buf(&bufferlist[2], &aligned_data,
664 packetlen_aligned - packetlen);
666 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal);
668 if (ret == 0 && signal)
669 vmbus_setevent(channel);
671 return ret;
673 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
676 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
677 * using a GPADL Direct packet type.
679 int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
680 struct hv_multipage_buffer *multi_pagebuffer,
681 void *buffer, u32 bufferlen, u64 requestid)
683 int ret;
684 struct vmbus_channel_packet_multipage_buffer desc;
685 u32 descsize;
686 u32 packetlen;
687 u32 packetlen_aligned;
688 struct scatterlist bufferlist[3];
689 u64 aligned_data = 0;
690 bool signal = false;
691 u32 pfncount = NUM_PAGES_SPANNED(multi_pagebuffer->offset,
692 multi_pagebuffer->len);
695 if ((pfncount < 0) || (pfncount > MAX_MULTIPAGE_BUFFER_COUNT))
696 return -EINVAL;
699 * Adjust the size down since vmbus_channel_packet_multipage_buffer is
700 * the largest size we support
702 descsize = sizeof(struct vmbus_channel_packet_multipage_buffer) -
703 ((MAX_MULTIPAGE_BUFFER_COUNT - pfncount) *
704 sizeof(u64));
705 packetlen = descsize + bufferlen;
706 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
709 /* Setup the descriptor */
710 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
711 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
712 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
713 desc.length8 = (u16)(packetlen_aligned >> 3);
714 desc.transactionid = requestid;
715 desc.rangecount = 1;
717 desc.range.len = multi_pagebuffer->len;
718 desc.range.offset = multi_pagebuffer->offset;
720 memcpy(desc.range.pfn_array, multi_pagebuffer->pfn_array,
721 pfncount * sizeof(u64));
723 sg_init_table(bufferlist, 3);
724 sg_set_buf(&bufferlist[0], &desc, descsize);
725 sg_set_buf(&bufferlist[1], buffer, bufferlen);
726 sg_set_buf(&bufferlist[2], &aligned_data,
727 packetlen_aligned - packetlen);
729 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal);
731 if (ret == 0 && signal)
732 vmbus_setevent(channel);
734 return ret;
736 EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer);
739 * vmbus_recvpacket() - Retrieve the user packet on the specified channel
740 * @channel: Pointer to vmbus_channel structure.
741 * @buffer: Pointer to the buffer you want to receive the data into.
742 * @bufferlen: Maximum size of what the the buffer will hold
743 * @buffer_actual_len: The actual size of the data after it was received
744 * @requestid: Identifier of the request
746 * Receives directly from the hyper-v vmbus and puts the data it received
747 * into Buffer. This will receive the data unparsed from hyper-v.
749 * Mainly used by Hyper-V drivers.
751 int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
752 u32 bufferlen, u32 *buffer_actual_len, u64 *requestid)
754 struct vmpacket_descriptor desc;
755 u32 packetlen;
756 u32 userlen;
757 int ret;
758 bool signal = false;
760 *buffer_actual_len = 0;
761 *requestid = 0;
764 ret = hv_ringbuffer_peek(&channel->inbound, &desc,
765 sizeof(struct vmpacket_descriptor));
766 if (ret != 0)
767 return 0;
769 packetlen = desc.len8 << 3;
770 userlen = packetlen - (desc.offset8 << 3);
772 *buffer_actual_len = userlen;
774 if (userlen > bufferlen) {
776 pr_err("Buffer too small - got %d needs %d\n",
777 bufferlen, userlen);
778 return -ETOOSMALL;
781 *requestid = desc.trans_id;
783 /* Copy over the packet to the user buffer */
784 ret = hv_ringbuffer_read(&channel->inbound, buffer, userlen,
785 (desc.offset8 << 3), &signal);
787 if (signal)
788 vmbus_setevent(channel);
790 return 0;
792 EXPORT_SYMBOL(vmbus_recvpacket);
795 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
797 int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
798 u32 bufferlen, u32 *buffer_actual_len,
799 u64 *requestid)
801 struct vmpacket_descriptor desc;
802 u32 packetlen;
803 int ret;
804 bool signal = false;
806 *buffer_actual_len = 0;
807 *requestid = 0;
810 ret = hv_ringbuffer_peek(&channel->inbound, &desc,
811 sizeof(struct vmpacket_descriptor));
812 if (ret != 0)
813 return 0;
816 packetlen = desc.len8 << 3;
818 *buffer_actual_len = packetlen;
820 if (packetlen > bufferlen) {
821 pr_err("Buffer too small - needed %d bytes but "
822 "got space for only %d bytes\n",
823 packetlen, bufferlen);
824 return -ENOBUFS;
827 *requestid = desc.trans_id;
829 /* Copy over the entire packet to the user buffer */
830 ret = hv_ringbuffer_read(&channel->inbound, buffer, packetlen, 0,
831 &signal);
833 if (signal)
834 vmbus_setevent(channel);
836 return ret;
838 EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);