sparc: properly conditionalize use of TIF_32BIT
[linux/fpc-iii.git] / drivers / hv / channel.c
blob284cf66489f428f1b00434d088da85222f00ea3a
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>
32 #include "hyperv_vmbus.h"
34 #define NUM_PAGES_SPANNED(addr, len) \
35 ((PAGE_ALIGN(addr + len) >> PAGE_SHIFT) - (addr >> PAGE_SHIFT))
38 * vmbus_setevent- Trigger an event notification on the specified
39 * channel.
41 static void vmbus_setevent(struct vmbus_channel *channel)
43 struct hv_monitor_page *monitorpage;
45 if (channel->offermsg.monitor_allocated) {
46 /* Each u32 represents 32 channels */
47 sync_set_bit(channel->offermsg.child_relid & 31,
48 (unsigned long *) vmbus_connection.send_int_page +
49 (channel->offermsg.child_relid >> 5));
51 /* Get the child to parent monitor page */
52 monitorpage = vmbus_connection.monitor_pages[1];
54 sync_set_bit(channel->monitor_bit,
55 (unsigned long *)&monitorpage->trigger_group
56 [channel->monitor_grp].pending);
58 } else {
59 vmbus_set_event(channel);
64 * vmbus_open - Open the specified channel.
66 int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size,
67 u32 recv_ringbuffer_size, void *userdata, u32 userdatalen,
68 void (*onchannelcallback)(void *context), void *context)
70 struct vmbus_channel_open_channel *open_msg;
71 struct vmbus_channel_msginfo *open_info = NULL;
72 void *in, *out;
73 unsigned long flags;
74 int ret, t, err = 0;
76 spin_lock_irqsave(&newchannel->sc_lock, flags);
77 if (newchannel->state == CHANNEL_OPEN_STATE) {
78 newchannel->state = CHANNEL_OPENING_STATE;
79 } else {
80 spin_unlock_irqrestore(&newchannel->sc_lock, flags);
81 return -EINVAL;
83 spin_unlock_irqrestore(&newchannel->sc_lock, flags);
85 newchannel->onchannel_callback = onchannelcallback;
86 newchannel->channel_callback_context = context;
88 /* Allocate the ring buffer */
89 out = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO,
90 get_order(send_ringbuffer_size + recv_ringbuffer_size));
92 if (!out)
93 return -ENOMEM;
96 in = (void *)((unsigned long)out + send_ringbuffer_size);
98 newchannel->ringbuffer_pages = out;
99 newchannel->ringbuffer_pagecount = (send_ringbuffer_size +
100 recv_ringbuffer_size) >> PAGE_SHIFT;
102 ret = hv_ringbuffer_init(
103 &newchannel->outbound, out, send_ringbuffer_size);
105 if (ret != 0) {
106 err = ret;
107 goto error0;
110 ret = hv_ringbuffer_init(
111 &newchannel->inbound, in, recv_ringbuffer_size);
112 if (ret != 0) {
113 err = ret;
114 goto error0;
118 /* Establish the gpadl for the ring buffer */
119 newchannel->ringbuffer_gpadlhandle = 0;
121 ret = vmbus_establish_gpadl(newchannel,
122 newchannel->outbound.ring_buffer,
123 send_ringbuffer_size +
124 recv_ringbuffer_size,
125 &newchannel->ringbuffer_gpadlhandle);
127 if (ret != 0) {
128 err = ret;
129 goto error0;
132 /* Create and init the channel open message */
133 open_info = kmalloc(sizeof(*open_info) +
134 sizeof(struct vmbus_channel_open_channel),
135 GFP_KERNEL);
136 if (!open_info) {
137 err = -ENOMEM;
138 goto error0;
141 init_completion(&open_info->waitevent);
143 open_msg = (struct vmbus_channel_open_channel *)open_info->msg;
144 open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL;
145 open_msg->openid = newchannel->offermsg.child_relid;
146 open_msg->child_relid = newchannel->offermsg.child_relid;
147 open_msg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle;
148 open_msg->downstream_ringbuffer_pageoffset = send_ringbuffer_size >>
149 PAGE_SHIFT;
150 open_msg->target_vp = newchannel->target_vp;
152 if (userdatalen > MAX_USER_DEFINED_BYTES) {
153 err = -EINVAL;
154 goto error0;
157 if (userdatalen)
158 memcpy(open_msg->userdata, userdata, userdatalen);
160 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
161 list_add_tail(&open_info->msglistentry,
162 &vmbus_connection.chn_msg_list);
163 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
165 ret = vmbus_post_msg(open_msg,
166 sizeof(struct vmbus_channel_open_channel));
168 if (ret != 0)
169 goto error1;
171 t = wait_for_completion_timeout(&open_info->waitevent, 5*HZ);
172 if (t == 0) {
173 err = -ETIMEDOUT;
174 goto error1;
178 if (open_info->response.open_result.status)
179 err = open_info->response.open_result.status;
181 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
182 list_del(&open_info->msglistentry);
183 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
185 if (err == 0)
186 newchannel->state = CHANNEL_OPENED_STATE;
188 kfree(open_info);
189 return err;
191 error1:
192 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
193 list_del(&open_info->msglistentry);
194 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
196 error0:
197 free_pages((unsigned long)out,
198 get_order(send_ringbuffer_size + recv_ringbuffer_size));
199 kfree(open_info);
200 return err;
202 EXPORT_SYMBOL_GPL(vmbus_open);
205 * create_gpadl_header - Creates a gpadl for the specified buffer
207 static int create_gpadl_header(void *kbuffer, u32 size,
208 struct vmbus_channel_msginfo **msginfo,
209 u32 *messagecount)
211 int i;
212 int pagecount;
213 struct vmbus_channel_gpadl_header *gpadl_header;
214 struct vmbus_channel_gpadl_body *gpadl_body;
215 struct vmbus_channel_msginfo *msgheader;
216 struct vmbus_channel_msginfo *msgbody = NULL;
217 u32 msgsize;
219 int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
221 pagecount = size >> PAGE_SHIFT;
223 /* do we need a gpadl body msg */
224 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
225 sizeof(struct vmbus_channel_gpadl_header) -
226 sizeof(struct gpa_range);
227 pfncount = pfnsize / sizeof(u64);
229 if (pagecount > pfncount) {
230 /* we need a gpadl body */
231 /* fill in the header */
232 msgsize = sizeof(struct vmbus_channel_msginfo) +
233 sizeof(struct vmbus_channel_gpadl_header) +
234 sizeof(struct gpa_range) + pfncount * sizeof(u64);
235 msgheader = kzalloc(msgsize, GFP_KERNEL);
236 if (!msgheader)
237 goto nomem;
239 INIT_LIST_HEAD(&msgheader->submsglist);
240 msgheader->msgsize = msgsize;
242 gpadl_header = (struct vmbus_channel_gpadl_header *)
243 msgheader->msg;
244 gpadl_header->rangecount = 1;
245 gpadl_header->range_buflen = sizeof(struct gpa_range) +
246 pagecount * sizeof(u64);
247 gpadl_header->range[0].byte_offset = 0;
248 gpadl_header->range[0].byte_count = size;
249 for (i = 0; i < pfncount; i++)
250 gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys(
251 kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT;
252 *msginfo = msgheader;
253 *messagecount = 1;
255 pfnsum = pfncount;
256 pfnleft = pagecount - pfncount;
258 /* how many pfns can we fit */
259 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
260 sizeof(struct vmbus_channel_gpadl_body);
261 pfncount = pfnsize / sizeof(u64);
263 /* fill in the body */
264 while (pfnleft) {
265 if (pfnleft > pfncount)
266 pfncurr = pfncount;
267 else
268 pfncurr = pfnleft;
270 msgsize = sizeof(struct vmbus_channel_msginfo) +
271 sizeof(struct vmbus_channel_gpadl_body) +
272 pfncurr * sizeof(u64);
273 msgbody = kzalloc(msgsize, GFP_KERNEL);
275 if (!msgbody) {
276 struct vmbus_channel_msginfo *pos = NULL;
277 struct vmbus_channel_msginfo *tmp = NULL;
279 * Free up all the allocated messages.
281 list_for_each_entry_safe(pos, tmp,
282 &msgheader->submsglist,
283 msglistentry) {
285 list_del(&pos->msglistentry);
286 kfree(pos);
289 goto nomem;
292 msgbody->msgsize = msgsize;
293 (*messagecount)++;
294 gpadl_body =
295 (struct vmbus_channel_gpadl_body *)msgbody->msg;
298 * Gpadl is u32 and we are using a pointer which could
299 * be 64-bit
300 * This is governed by the guest/host protocol and
301 * so the hypervisor gurantees that this is ok.
303 for (i = 0; i < pfncurr; i++)
304 gpadl_body->pfn[i] = slow_virt_to_phys(
305 kbuffer + PAGE_SIZE * (pfnsum + i)) >>
306 PAGE_SHIFT;
308 /* add to msg header */
309 list_add_tail(&msgbody->msglistentry,
310 &msgheader->submsglist);
311 pfnsum += pfncurr;
312 pfnleft -= pfncurr;
314 } else {
315 /* everything fits in a header */
316 msgsize = sizeof(struct vmbus_channel_msginfo) +
317 sizeof(struct vmbus_channel_gpadl_header) +
318 sizeof(struct gpa_range) + pagecount * sizeof(u64);
319 msgheader = kzalloc(msgsize, GFP_KERNEL);
320 if (msgheader == NULL)
321 goto nomem;
322 msgheader->msgsize = msgsize;
324 gpadl_header = (struct vmbus_channel_gpadl_header *)
325 msgheader->msg;
326 gpadl_header->rangecount = 1;
327 gpadl_header->range_buflen = sizeof(struct gpa_range) +
328 pagecount * sizeof(u64);
329 gpadl_header->range[0].byte_offset = 0;
330 gpadl_header->range[0].byte_count = size;
331 for (i = 0; i < pagecount; i++)
332 gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys(
333 kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT;
335 *msginfo = msgheader;
336 *messagecount = 1;
339 return 0;
340 nomem:
341 kfree(msgheader);
342 kfree(msgbody);
343 return -ENOMEM;
347 * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
349 * @channel: a channel
350 * @kbuffer: from kmalloc or vmalloc
351 * @size: page-size multiple
352 * @gpadl_handle: some funky thing
354 int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
355 u32 size, u32 *gpadl_handle)
357 struct vmbus_channel_gpadl_header *gpadlmsg;
358 struct vmbus_channel_gpadl_body *gpadl_body;
359 struct vmbus_channel_msginfo *msginfo = NULL;
360 struct vmbus_channel_msginfo *submsginfo;
361 u32 msgcount;
362 struct list_head *curr;
363 u32 next_gpadl_handle;
364 unsigned long flags;
365 int ret = 0;
366 int t;
368 next_gpadl_handle = atomic_read(&vmbus_connection.next_gpadl_handle);
369 atomic_inc(&vmbus_connection.next_gpadl_handle);
371 ret = create_gpadl_header(kbuffer, size, &msginfo, &msgcount);
372 if (ret)
373 return ret;
375 init_completion(&msginfo->waitevent);
377 gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
378 gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
379 gpadlmsg->child_relid = channel->offermsg.child_relid;
380 gpadlmsg->gpadl = next_gpadl_handle;
383 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
384 list_add_tail(&msginfo->msglistentry,
385 &vmbus_connection.chn_msg_list);
387 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
389 ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
390 sizeof(*msginfo));
391 if (ret != 0)
392 goto cleanup;
394 if (msgcount > 1) {
395 list_for_each(curr, &msginfo->submsglist) {
397 submsginfo = (struct vmbus_channel_msginfo *)curr;
398 gpadl_body =
399 (struct vmbus_channel_gpadl_body *)submsginfo->msg;
401 gpadl_body->header.msgtype =
402 CHANNELMSG_GPADL_BODY;
403 gpadl_body->gpadl = next_gpadl_handle;
405 ret = vmbus_post_msg(gpadl_body,
406 submsginfo->msgsize -
407 sizeof(*submsginfo));
408 if (ret != 0)
409 goto cleanup;
413 t = wait_for_completion_timeout(&msginfo->waitevent, 5*HZ);
414 BUG_ON(t == 0);
417 /* At this point, we received the gpadl created msg */
418 *gpadl_handle = gpadlmsg->gpadl;
420 cleanup:
421 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
422 list_del(&msginfo->msglistentry);
423 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
425 kfree(msginfo);
426 return ret;
428 EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
431 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
433 int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
435 struct vmbus_channel_gpadl_teardown *msg;
436 struct vmbus_channel_msginfo *info;
437 unsigned long flags;
438 int ret, t;
440 info = kmalloc(sizeof(*info) +
441 sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
442 if (!info)
443 return -ENOMEM;
445 init_completion(&info->waitevent);
447 msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
449 msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
450 msg->child_relid = channel->offermsg.child_relid;
451 msg->gpadl = gpadl_handle;
453 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
454 list_add_tail(&info->msglistentry,
455 &vmbus_connection.chn_msg_list);
456 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
457 ret = vmbus_post_msg(msg,
458 sizeof(struct vmbus_channel_gpadl_teardown));
460 BUG_ON(ret != 0);
461 t = wait_for_completion_timeout(&info->waitevent, 5*HZ);
462 BUG_ON(t == 0);
464 /* Received a torndown response */
465 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
466 list_del(&info->msglistentry);
467 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
469 kfree(info);
470 return ret;
472 EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
474 static void reset_channel_cb(void *arg)
476 struct vmbus_channel *channel = arg;
478 channel->onchannel_callback = NULL;
481 static void vmbus_close_internal(struct vmbus_channel *channel)
483 struct vmbus_channel_close_channel *msg;
484 int ret;
486 channel->state = CHANNEL_OPEN_STATE;
487 channel->sc_creation_callback = NULL;
488 /* Stop callback and cancel the timer asap */
489 if (channel->target_cpu != smp_processor_id())
490 smp_call_function_single(channel->target_cpu, reset_channel_cb,
491 channel, true);
492 else
493 reset_channel_cb(channel);
495 /* Send a closing message */
497 msg = &channel->close_msg.msg;
499 msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
500 msg->child_relid = channel->offermsg.child_relid;
502 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel));
504 BUG_ON(ret != 0);
505 /* Tear down the gpadl for the channel's ring buffer */
506 if (channel->ringbuffer_gpadlhandle)
507 vmbus_teardown_gpadl(channel,
508 channel->ringbuffer_gpadlhandle);
510 /* Cleanup the ring buffers for this channel */
511 hv_ringbuffer_cleanup(&channel->outbound);
512 hv_ringbuffer_cleanup(&channel->inbound);
514 free_pages((unsigned long)channel->ringbuffer_pages,
515 get_order(channel->ringbuffer_pagecount * PAGE_SIZE));
521 * vmbus_close - Close the specified channel
523 void vmbus_close(struct vmbus_channel *channel)
525 struct list_head *cur, *tmp;
526 struct vmbus_channel *cur_channel;
528 if (channel->primary_channel != NULL) {
530 * We will only close sub-channels when
531 * the primary is closed.
533 return;
536 * Close all the sub-channels first and then close the
537 * primary channel.
539 list_for_each_safe(cur, tmp, &channel->sc_list) {
540 cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
541 if (cur_channel->state != CHANNEL_OPENED_STATE)
542 continue;
543 vmbus_close_internal(cur_channel);
546 * Now close the primary.
548 vmbus_close_internal(channel);
550 EXPORT_SYMBOL_GPL(vmbus_close);
553 * vmbus_sendpacket() - Send the specified buffer on the given channel
554 * @channel: Pointer to vmbus_channel structure.
555 * @buffer: Pointer to the buffer you want to receive the data into.
556 * @bufferlen: Maximum size of what the the buffer will hold
557 * @requestid: Identifier of the request
558 * @type: Type of packet that is being send e.g. negotiate, time
559 * packet etc.
561 * Sends data in @buffer directly to hyper-v via the vmbus
562 * This will send the data unparsed to hyper-v.
564 * Mainly used by Hyper-V drivers.
566 int vmbus_sendpacket(struct vmbus_channel *channel, void *buffer,
567 u32 bufferlen, u64 requestid,
568 enum vmbus_packet_type type, u32 flags)
570 struct vmpacket_descriptor desc;
571 u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
572 u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
573 struct kvec bufferlist[3];
574 u64 aligned_data = 0;
575 int ret;
576 bool signal = false;
579 /* Setup the descriptor */
580 desc.type = type; /* VmbusPacketTypeDataInBand; */
581 desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
582 /* in 8-bytes granularity */
583 desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
584 desc.len8 = (u16)(packetlen_aligned >> 3);
585 desc.trans_id = requestid;
587 bufferlist[0].iov_base = &desc;
588 bufferlist[0].iov_len = sizeof(struct vmpacket_descriptor);
589 bufferlist[1].iov_base = buffer;
590 bufferlist[1].iov_len = bufferlen;
591 bufferlist[2].iov_base = &aligned_data;
592 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
594 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal);
596 if (ret == 0 && signal)
597 vmbus_setevent(channel);
599 return ret;
601 EXPORT_SYMBOL(vmbus_sendpacket);
604 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
605 * packets using a GPADL Direct packet type.
607 int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
608 struct hv_page_buffer pagebuffers[],
609 u32 pagecount, void *buffer, u32 bufferlen,
610 u64 requestid)
612 int ret;
613 int i;
614 struct vmbus_channel_packet_page_buffer desc;
615 u32 descsize;
616 u32 packetlen;
617 u32 packetlen_aligned;
618 struct kvec bufferlist[3];
619 u64 aligned_data = 0;
620 bool signal = false;
622 if (pagecount > MAX_PAGE_BUFFER_COUNT)
623 return -EINVAL;
627 * Adjust the size down since vmbus_channel_packet_page_buffer is the
628 * largest size we support
630 descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
631 ((MAX_PAGE_BUFFER_COUNT - pagecount) *
632 sizeof(struct hv_page_buffer));
633 packetlen = descsize + bufferlen;
634 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
636 /* Setup the descriptor */
637 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
638 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
639 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
640 desc.length8 = (u16)(packetlen_aligned >> 3);
641 desc.transactionid = requestid;
642 desc.rangecount = pagecount;
644 for (i = 0; i < pagecount; i++) {
645 desc.range[i].len = pagebuffers[i].len;
646 desc.range[i].offset = pagebuffers[i].offset;
647 desc.range[i].pfn = pagebuffers[i].pfn;
650 bufferlist[0].iov_base = &desc;
651 bufferlist[0].iov_len = descsize;
652 bufferlist[1].iov_base = buffer;
653 bufferlist[1].iov_len = bufferlen;
654 bufferlist[2].iov_base = &aligned_data;
655 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
657 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal);
659 if (ret == 0 && signal)
660 vmbus_setevent(channel);
662 return ret;
664 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
667 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
668 * using a GPADL Direct packet type.
670 int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
671 struct hv_multipage_buffer *multi_pagebuffer,
672 void *buffer, u32 bufferlen, u64 requestid)
674 int ret;
675 struct vmbus_channel_packet_multipage_buffer desc;
676 u32 descsize;
677 u32 packetlen;
678 u32 packetlen_aligned;
679 struct kvec bufferlist[3];
680 u64 aligned_data = 0;
681 bool signal = false;
682 u32 pfncount = NUM_PAGES_SPANNED(multi_pagebuffer->offset,
683 multi_pagebuffer->len);
685 if (pfncount > MAX_MULTIPAGE_BUFFER_COUNT)
686 return -EINVAL;
689 * Adjust the size down since vmbus_channel_packet_multipage_buffer is
690 * the largest size we support
692 descsize = sizeof(struct vmbus_channel_packet_multipage_buffer) -
693 ((MAX_MULTIPAGE_BUFFER_COUNT - pfncount) *
694 sizeof(u64));
695 packetlen = descsize + bufferlen;
696 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
699 /* Setup the descriptor */
700 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
701 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
702 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
703 desc.length8 = (u16)(packetlen_aligned >> 3);
704 desc.transactionid = requestid;
705 desc.rangecount = 1;
707 desc.range.len = multi_pagebuffer->len;
708 desc.range.offset = multi_pagebuffer->offset;
710 memcpy(desc.range.pfn_array, multi_pagebuffer->pfn_array,
711 pfncount * sizeof(u64));
713 bufferlist[0].iov_base = &desc;
714 bufferlist[0].iov_len = descsize;
715 bufferlist[1].iov_base = buffer;
716 bufferlist[1].iov_len = bufferlen;
717 bufferlist[2].iov_base = &aligned_data;
718 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
720 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal);
722 if (ret == 0 && signal)
723 vmbus_setevent(channel);
725 return ret;
727 EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer);
730 * vmbus_recvpacket() - Retrieve the user packet on the specified channel
731 * @channel: Pointer to vmbus_channel structure.
732 * @buffer: Pointer to the buffer you want to receive the data into.
733 * @bufferlen: Maximum size of what the the buffer will hold
734 * @buffer_actual_len: The actual size of the data after it was received
735 * @requestid: Identifier of the request
737 * Receives directly from the hyper-v vmbus and puts the data it received
738 * into Buffer. This will receive the data unparsed from hyper-v.
740 * Mainly used by Hyper-V drivers.
742 int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
743 u32 bufferlen, u32 *buffer_actual_len, u64 *requestid)
745 struct vmpacket_descriptor desc;
746 u32 packetlen;
747 u32 userlen;
748 int ret;
749 bool signal = false;
751 *buffer_actual_len = 0;
752 *requestid = 0;
755 ret = hv_ringbuffer_peek(&channel->inbound, &desc,
756 sizeof(struct vmpacket_descriptor));
757 if (ret != 0)
758 return 0;
760 packetlen = desc.len8 << 3;
761 userlen = packetlen - (desc.offset8 << 3);
763 *buffer_actual_len = userlen;
765 if (userlen > bufferlen) {
767 pr_err("Buffer too small - got %d needs %d\n",
768 bufferlen, userlen);
769 return -ETOOSMALL;
772 *requestid = desc.trans_id;
774 /* Copy over the packet to the user buffer */
775 ret = hv_ringbuffer_read(&channel->inbound, buffer, userlen,
776 (desc.offset8 << 3), &signal);
778 if (signal)
779 vmbus_setevent(channel);
781 return 0;
783 EXPORT_SYMBOL(vmbus_recvpacket);
786 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
788 int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
789 u32 bufferlen, u32 *buffer_actual_len,
790 u64 *requestid)
792 struct vmpacket_descriptor desc;
793 u32 packetlen;
794 int ret;
795 bool signal = false;
797 *buffer_actual_len = 0;
798 *requestid = 0;
801 ret = hv_ringbuffer_peek(&channel->inbound, &desc,
802 sizeof(struct vmpacket_descriptor));
803 if (ret != 0)
804 return 0;
807 packetlen = desc.len8 << 3;
809 *buffer_actual_len = packetlen;
811 if (packetlen > bufferlen) {
812 pr_err("Buffer too small - needed %d bytes but "
813 "got space for only %d bytes\n",
814 packetlen, bufferlen);
815 return -ENOBUFS;
818 *requestid = desc.trans_id;
820 /* Copy over the entire packet to the user buffer */
821 ret = hv_ringbuffer_read(&channel->inbound, buffer, packetlen, 0,
822 &signal);
824 if (signal)
825 vmbus_setevent(channel);
827 return ret;
829 EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);