Linux 3.12.39
[linux/fpc-iii.git] / drivers / hv / channel.c
blobdea6613313511f38d880c4b6830a80f7fd3d847d
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 monitorpage = vmbus_connection.monitor_pages;
51 monitorpage++; /* Get the child to parent monitor page */
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_get_debug_info -Retrieve various channel debug info
65 void vmbus_get_debug_info(struct vmbus_channel *channel,
66 struct vmbus_channel_debug_info *debuginfo)
68 struct hv_monitor_page *monitorpage;
69 u8 monitor_group = (u8)channel->offermsg.monitorid / 32;
70 u8 monitor_offset = (u8)channel->offermsg.monitorid % 32;
72 debuginfo->relid = channel->offermsg.child_relid;
73 debuginfo->state = channel->state;
74 memcpy(&debuginfo->interfacetype,
75 &channel->offermsg.offer.if_type, sizeof(uuid_le));
76 memcpy(&debuginfo->interface_instance,
77 &channel->offermsg.offer.if_instance,
78 sizeof(uuid_le));
80 monitorpage = (struct hv_monitor_page *)vmbus_connection.monitor_pages;
82 debuginfo->monitorid = channel->offermsg.monitorid;
84 debuginfo->servermonitor_pending =
85 monitorpage->trigger_group[monitor_group].pending;
86 debuginfo->servermonitor_latency =
87 monitorpage->latency[monitor_group][monitor_offset];
88 debuginfo->servermonitor_connectionid =
89 monitorpage->parameter[monitor_group]
90 [monitor_offset].connectionid.u.id;
92 monitorpage++;
94 debuginfo->clientmonitor_pending =
95 monitorpage->trigger_group[monitor_group].pending;
96 debuginfo->clientmonitor_latency =
97 monitorpage->latency[monitor_group][monitor_offset];
98 debuginfo->clientmonitor_connectionid =
99 monitorpage->parameter[monitor_group]
100 [monitor_offset].connectionid.u.id;
102 hv_ringbuffer_get_debuginfo(&channel->inbound, &debuginfo->inbound);
103 hv_ringbuffer_get_debuginfo(&channel->outbound, &debuginfo->outbound);
107 * vmbus_open - Open the specified channel.
109 int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size,
110 u32 recv_ringbuffer_size, void *userdata, u32 userdatalen,
111 void (*onchannelcallback)(void *context), void *context)
113 struct vmbus_channel_open_channel *open_msg;
114 struct vmbus_channel_msginfo *open_info = NULL;
115 void *in, *out;
116 unsigned long flags;
117 int ret, t, err = 0;
119 spin_lock_irqsave(&newchannel->sc_lock, flags);
120 if (newchannel->state == CHANNEL_OPEN_STATE) {
121 newchannel->state = CHANNEL_OPENING_STATE;
122 } else {
123 spin_unlock_irqrestore(&newchannel->sc_lock, flags);
124 return -EINVAL;
126 spin_unlock_irqrestore(&newchannel->sc_lock, flags);
128 newchannel->onchannel_callback = onchannelcallback;
129 newchannel->channel_callback_context = context;
131 /* Allocate the ring buffer */
132 out = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO,
133 get_order(send_ringbuffer_size + recv_ringbuffer_size));
135 if (!out)
136 return -ENOMEM;
139 in = (void *)((unsigned long)out + send_ringbuffer_size);
141 newchannel->ringbuffer_pages = out;
142 newchannel->ringbuffer_pagecount = (send_ringbuffer_size +
143 recv_ringbuffer_size) >> PAGE_SHIFT;
145 ret = hv_ringbuffer_init(
146 &newchannel->outbound, out, send_ringbuffer_size);
148 if (ret != 0) {
149 err = ret;
150 goto error0;
153 ret = hv_ringbuffer_init(
154 &newchannel->inbound, in, recv_ringbuffer_size);
155 if (ret != 0) {
156 err = ret;
157 goto error0;
161 /* Establish the gpadl for the ring buffer */
162 newchannel->ringbuffer_gpadlhandle = 0;
164 ret = vmbus_establish_gpadl(newchannel,
165 newchannel->outbound.ring_buffer,
166 send_ringbuffer_size +
167 recv_ringbuffer_size,
168 &newchannel->ringbuffer_gpadlhandle);
170 if (ret != 0) {
171 err = ret;
172 goto error0;
175 /* Create and init the channel open message */
176 open_info = kmalloc(sizeof(*open_info) +
177 sizeof(struct vmbus_channel_open_channel),
178 GFP_KERNEL);
179 if (!open_info) {
180 err = -ENOMEM;
181 goto error0;
184 init_completion(&open_info->waitevent);
186 open_msg = (struct vmbus_channel_open_channel *)open_info->msg;
187 open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL;
188 open_msg->openid = newchannel->offermsg.child_relid;
189 open_msg->child_relid = newchannel->offermsg.child_relid;
190 open_msg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle;
191 open_msg->downstream_ringbuffer_pageoffset = send_ringbuffer_size >>
192 PAGE_SHIFT;
193 open_msg->target_vp = newchannel->target_vp;
195 if (userdatalen > MAX_USER_DEFINED_BYTES) {
196 err = -EINVAL;
197 goto error0;
200 if (userdatalen)
201 memcpy(open_msg->userdata, userdata, userdatalen);
203 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
204 list_add_tail(&open_info->msglistentry,
205 &vmbus_connection.chn_msg_list);
206 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
208 ret = vmbus_post_msg(open_msg,
209 sizeof(struct vmbus_channel_open_channel));
211 if (ret != 0) {
212 err = ret;
213 goto error1;
216 t = wait_for_completion_timeout(&open_info->waitevent, 5*HZ);
217 if (t == 0) {
218 err = -ETIMEDOUT;
219 goto error1;
223 if (open_info->response.open_result.status)
224 err = open_info->response.open_result.status;
226 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
227 list_del(&open_info->msglistentry);
228 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
230 if (err == 0)
231 newchannel->state = CHANNEL_OPENED_STATE;
233 kfree(open_info);
234 return err;
236 error1:
237 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
238 list_del(&open_info->msglistentry);
239 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
241 error0:
242 free_pages((unsigned long)out,
243 get_order(send_ringbuffer_size + recv_ringbuffer_size));
244 kfree(open_info);
245 return err;
247 EXPORT_SYMBOL_GPL(vmbus_open);
250 * create_gpadl_header - Creates a gpadl for the specified buffer
252 static int create_gpadl_header(void *kbuffer, u32 size,
253 struct vmbus_channel_msginfo **msginfo,
254 u32 *messagecount)
256 int i;
257 int pagecount;
258 unsigned long long pfn;
259 struct vmbus_channel_gpadl_header *gpadl_header;
260 struct vmbus_channel_gpadl_body *gpadl_body;
261 struct vmbus_channel_msginfo *msgheader;
262 struct vmbus_channel_msginfo *msgbody = NULL;
263 u32 msgsize;
265 int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
267 pagecount = size >> PAGE_SHIFT;
268 pfn = virt_to_phys(kbuffer) >> PAGE_SHIFT;
270 /* do we need a gpadl body msg */
271 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
272 sizeof(struct vmbus_channel_gpadl_header) -
273 sizeof(struct gpa_range);
274 pfncount = pfnsize / sizeof(u64);
276 if (pagecount > pfncount) {
277 /* we need a gpadl body */
278 /* fill in the header */
279 msgsize = sizeof(struct vmbus_channel_msginfo) +
280 sizeof(struct vmbus_channel_gpadl_header) +
281 sizeof(struct gpa_range) + pfncount * sizeof(u64);
282 msgheader = kzalloc(msgsize, GFP_KERNEL);
283 if (!msgheader)
284 goto nomem;
286 INIT_LIST_HEAD(&msgheader->submsglist);
287 msgheader->msgsize = msgsize;
289 gpadl_header = (struct vmbus_channel_gpadl_header *)
290 msgheader->msg;
291 gpadl_header->rangecount = 1;
292 gpadl_header->range_buflen = sizeof(struct gpa_range) +
293 pagecount * sizeof(u64);
294 gpadl_header->range[0].byte_offset = 0;
295 gpadl_header->range[0].byte_count = size;
296 for (i = 0; i < pfncount; i++)
297 gpadl_header->range[0].pfn_array[i] = pfn+i;
298 *msginfo = msgheader;
299 *messagecount = 1;
301 pfnsum = pfncount;
302 pfnleft = pagecount - pfncount;
304 /* how many pfns can we fit */
305 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
306 sizeof(struct vmbus_channel_gpadl_body);
307 pfncount = pfnsize / sizeof(u64);
309 /* fill in the body */
310 while (pfnleft) {
311 if (pfnleft > pfncount)
312 pfncurr = pfncount;
313 else
314 pfncurr = pfnleft;
316 msgsize = sizeof(struct vmbus_channel_msginfo) +
317 sizeof(struct vmbus_channel_gpadl_body) +
318 pfncurr * sizeof(u64);
319 msgbody = kzalloc(msgsize, GFP_KERNEL);
321 if (!msgbody) {
322 struct vmbus_channel_msginfo *pos = NULL;
323 struct vmbus_channel_msginfo *tmp = NULL;
325 * Free up all the allocated messages.
327 list_for_each_entry_safe(pos, tmp,
328 &msgheader->submsglist,
329 msglistentry) {
331 list_del(&pos->msglistentry);
332 kfree(pos);
335 goto nomem;
338 msgbody->msgsize = msgsize;
339 (*messagecount)++;
340 gpadl_body =
341 (struct vmbus_channel_gpadl_body *)msgbody->msg;
344 * Gpadl is u32 and we are using a pointer which could
345 * be 64-bit
346 * This is governed by the guest/host protocol and
347 * so the hypervisor gurantees that this is ok.
349 for (i = 0; i < pfncurr; i++)
350 gpadl_body->pfn[i] = pfn + pfnsum + i;
352 /* add to msg header */
353 list_add_tail(&msgbody->msglistentry,
354 &msgheader->submsglist);
355 pfnsum += pfncurr;
356 pfnleft -= pfncurr;
358 } else {
359 /* everything fits in a header */
360 msgsize = sizeof(struct vmbus_channel_msginfo) +
361 sizeof(struct vmbus_channel_gpadl_header) +
362 sizeof(struct gpa_range) + pagecount * sizeof(u64);
363 msgheader = kzalloc(msgsize, GFP_KERNEL);
364 if (msgheader == NULL)
365 goto nomem;
366 msgheader->msgsize = msgsize;
368 gpadl_header = (struct vmbus_channel_gpadl_header *)
369 msgheader->msg;
370 gpadl_header->rangecount = 1;
371 gpadl_header->range_buflen = sizeof(struct gpa_range) +
372 pagecount * sizeof(u64);
373 gpadl_header->range[0].byte_offset = 0;
374 gpadl_header->range[0].byte_count = size;
375 for (i = 0; i < pagecount; i++)
376 gpadl_header->range[0].pfn_array[i] = pfn+i;
378 *msginfo = msgheader;
379 *messagecount = 1;
382 return 0;
383 nomem:
384 kfree(msgheader);
385 kfree(msgbody);
386 return -ENOMEM;
390 * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
392 * @channel: a channel
393 * @kbuffer: from kmalloc
394 * @size: page-size multiple
395 * @gpadl_handle: some funky thing
397 int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
398 u32 size, u32 *gpadl_handle)
400 struct vmbus_channel_gpadl_header *gpadlmsg;
401 struct vmbus_channel_gpadl_body *gpadl_body;
402 struct vmbus_channel_msginfo *msginfo = NULL;
403 struct vmbus_channel_msginfo *submsginfo;
404 u32 msgcount;
405 struct list_head *curr;
406 u32 next_gpadl_handle;
407 unsigned long flags;
408 int ret = 0;
410 next_gpadl_handle = atomic_read(&vmbus_connection.next_gpadl_handle);
411 atomic_inc(&vmbus_connection.next_gpadl_handle);
413 ret = create_gpadl_header(kbuffer, size, &msginfo, &msgcount);
414 if (ret)
415 return ret;
417 init_completion(&msginfo->waitevent);
419 gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
420 gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
421 gpadlmsg->child_relid = channel->offermsg.child_relid;
422 gpadlmsg->gpadl = next_gpadl_handle;
425 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
426 list_add_tail(&msginfo->msglistentry,
427 &vmbus_connection.chn_msg_list);
429 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
431 ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
432 sizeof(*msginfo));
433 if (ret != 0)
434 goto cleanup;
436 if (msgcount > 1) {
437 list_for_each(curr, &msginfo->submsglist) {
439 submsginfo = (struct vmbus_channel_msginfo *)curr;
440 gpadl_body =
441 (struct vmbus_channel_gpadl_body *)submsginfo->msg;
443 gpadl_body->header.msgtype =
444 CHANNELMSG_GPADL_BODY;
445 gpadl_body->gpadl = next_gpadl_handle;
447 ret = vmbus_post_msg(gpadl_body,
448 submsginfo->msgsize -
449 sizeof(*submsginfo));
450 if (ret != 0)
451 goto cleanup;
455 wait_for_completion(&msginfo->waitevent);
457 /* At this point, we received the gpadl created msg */
458 *gpadl_handle = gpadlmsg->gpadl;
460 cleanup:
461 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
462 list_del(&msginfo->msglistentry);
463 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
465 kfree(msginfo);
466 return ret;
468 EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
471 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
473 int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
475 struct vmbus_channel_gpadl_teardown *msg;
476 struct vmbus_channel_msginfo *info;
477 unsigned long flags;
478 int ret;
480 info = kmalloc(sizeof(*info) +
481 sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
482 if (!info)
483 return -ENOMEM;
485 init_completion(&info->waitevent);
487 msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
489 msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
490 msg->child_relid = channel->offermsg.child_relid;
491 msg->gpadl = gpadl_handle;
493 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
494 list_add_tail(&info->msglistentry,
495 &vmbus_connection.chn_msg_list);
496 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
497 ret = vmbus_post_msg(msg,
498 sizeof(struct vmbus_channel_gpadl_teardown));
500 if (ret)
501 goto post_msg_err;
503 wait_for_completion(&info->waitevent);
505 post_msg_err:
506 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
507 list_del(&info->msglistentry);
508 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
510 kfree(info);
511 return ret;
513 EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
515 static int vmbus_close_internal(struct vmbus_channel *channel)
517 struct vmbus_channel_close_channel *msg;
518 int ret;
519 unsigned long flags;
521 channel->state = CHANNEL_OPEN_STATE;
522 channel->sc_creation_callback = NULL;
523 /* Stop callback and cancel the timer asap */
524 spin_lock_irqsave(&channel->inbound_lock, flags);
525 channel->onchannel_callback = NULL;
526 spin_unlock_irqrestore(&channel->inbound_lock, flags);
528 /* Send a closing message */
530 msg = &channel->close_msg.msg;
532 msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
533 msg->child_relid = channel->offermsg.child_relid;
535 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel));
537 if (ret) {
538 pr_err("Close failed: close post msg return is %d\n", ret);
540 * If we failed to post the close msg,
541 * it is perhaps better to leak memory.
543 return ret;
546 /* Tear down the gpadl for the channel's ring buffer */
547 if (channel->ringbuffer_gpadlhandle) {
548 ret = vmbus_teardown_gpadl(channel,
549 channel->ringbuffer_gpadlhandle);
550 if (ret) {
551 pr_err("Close failed: teardown gpadl return %d\n", ret);
553 * If we failed to teardown gpadl,
554 * it is perhaps better to leak memory.
556 return ret;
560 /* Cleanup the ring buffers for this channel */
561 hv_ringbuffer_cleanup(&channel->outbound);
562 hv_ringbuffer_cleanup(&channel->inbound);
564 free_pages((unsigned long)channel->ringbuffer_pages,
565 get_order(channel->ringbuffer_pagecount * PAGE_SIZE));
567 return ret;
571 * vmbus_close - Close the specified channel
573 void vmbus_close(struct vmbus_channel *channel)
575 struct list_head *cur, *tmp;
576 struct vmbus_channel *cur_channel;
578 if (channel->primary_channel != NULL) {
580 * We will only close sub-channels when
581 * the primary is closed.
583 return;
586 * Close all the sub-channels first and then close the
587 * primary channel.
589 list_for_each_safe(cur, tmp, &channel->sc_list) {
590 cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
591 if (cur_channel->state != CHANNEL_OPENED_STATE)
592 continue;
593 vmbus_close_internal(cur_channel);
596 * Now close the primary.
598 vmbus_close_internal(channel);
600 EXPORT_SYMBOL_GPL(vmbus_close);
603 * vmbus_sendpacket() - Send the specified buffer on the given channel
604 * @channel: Pointer to vmbus_channel structure.
605 * @buffer: Pointer to the buffer you want to receive the data into.
606 * @bufferlen: Maximum size of what the the buffer will hold
607 * @requestid: Identifier of the request
608 * @type: Type of packet that is being send e.g. negotiate, time
609 * packet etc.
611 * Sends data in @buffer directly to hyper-v via the vmbus
612 * This will send the data unparsed to hyper-v.
614 * Mainly used by Hyper-V drivers.
616 int vmbus_sendpacket(struct vmbus_channel *channel, const void *buffer,
617 u32 bufferlen, u64 requestid,
618 enum vmbus_packet_type type, u32 flags)
620 struct vmpacket_descriptor desc;
621 u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
622 u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
623 struct scatterlist bufferlist[3];
624 u64 aligned_data = 0;
625 int ret;
626 bool signal = false;
629 /* Setup the descriptor */
630 desc.type = type; /* VmbusPacketTypeDataInBand; */
631 desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
632 /* in 8-bytes granularity */
633 desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
634 desc.len8 = (u16)(packetlen_aligned >> 3);
635 desc.trans_id = requestid;
637 sg_init_table(bufferlist, 3);
638 sg_set_buf(&bufferlist[0], &desc, sizeof(struct vmpacket_descriptor));
639 sg_set_buf(&bufferlist[1], buffer, bufferlen);
640 sg_set_buf(&bufferlist[2], &aligned_data,
641 packetlen_aligned - packetlen);
643 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal);
645 if (ret == 0 && signal)
646 vmbus_setevent(channel);
648 return ret;
650 EXPORT_SYMBOL(vmbus_sendpacket);
653 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
654 * packets using a GPADL Direct packet type.
656 int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
657 struct hv_page_buffer pagebuffers[],
658 u32 pagecount, void *buffer, u32 bufferlen,
659 u64 requestid)
661 int ret;
662 int i;
663 struct vmbus_channel_packet_page_buffer desc;
664 u32 descsize;
665 u32 packetlen;
666 u32 packetlen_aligned;
667 struct scatterlist bufferlist[3];
668 u64 aligned_data = 0;
669 bool signal = false;
671 if (pagecount > MAX_PAGE_BUFFER_COUNT)
672 return -EINVAL;
676 * Adjust the size down since vmbus_channel_packet_page_buffer is the
677 * largest size we support
679 descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
680 ((MAX_PAGE_BUFFER_COUNT - pagecount) *
681 sizeof(struct hv_page_buffer));
682 packetlen = descsize + bufferlen;
683 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
685 /* Setup the descriptor */
686 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
687 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
688 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
689 desc.length8 = (u16)(packetlen_aligned >> 3);
690 desc.transactionid = requestid;
691 desc.rangecount = pagecount;
693 for (i = 0; i < pagecount; i++) {
694 desc.range[i].len = pagebuffers[i].len;
695 desc.range[i].offset = pagebuffers[i].offset;
696 desc.range[i].pfn = pagebuffers[i].pfn;
699 sg_init_table(bufferlist, 3);
700 sg_set_buf(&bufferlist[0], &desc, descsize);
701 sg_set_buf(&bufferlist[1], buffer, bufferlen);
702 sg_set_buf(&bufferlist[2], &aligned_data,
703 packetlen_aligned - packetlen);
705 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal);
707 if (ret == 0 && signal)
708 vmbus_setevent(channel);
710 return ret;
712 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
715 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
716 * using a GPADL Direct packet type.
718 int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
719 struct hv_multipage_buffer *multi_pagebuffer,
720 void *buffer, u32 bufferlen, u64 requestid)
722 int ret;
723 struct vmbus_channel_packet_multipage_buffer desc;
724 u32 descsize;
725 u32 packetlen;
726 u32 packetlen_aligned;
727 struct scatterlist bufferlist[3];
728 u64 aligned_data = 0;
729 bool signal = false;
730 u32 pfncount = NUM_PAGES_SPANNED(multi_pagebuffer->offset,
731 multi_pagebuffer->len);
734 if ((pfncount < 0) || (pfncount > MAX_MULTIPAGE_BUFFER_COUNT))
735 return -EINVAL;
738 * Adjust the size down since vmbus_channel_packet_multipage_buffer is
739 * the largest size we support
741 descsize = sizeof(struct vmbus_channel_packet_multipage_buffer) -
742 ((MAX_MULTIPAGE_BUFFER_COUNT - pfncount) *
743 sizeof(u64));
744 packetlen = descsize + bufferlen;
745 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
748 /* Setup the descriptor */
749 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
750 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
751 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
752 desc.length8 = (u16)(packetlen_aligned >> 3);
753 desc.transactionid = requestid;
754 desc.rangecount = 1;
756 desc.range.len = multi_pagebuffer->len;
757 desc.range.offset = multi_pagebuffer->offset;
759 memcpy(desc.range.pfn_array, multi_pagebuffer->pfn_array,
760 pfncount * sizeof(u64));
762 sg_init_table(bufferlist, 3);
763 sg_set_buf(&bufferlist[0], &desc, descsize);
764 sg_set_buf(&bufferlist[1], buffer, bufferlen);
765 sg_set_buf(&bufferlist[2], &aligned_data,
766 packetlen_aligned - packetlen);
768 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal);
770 if (ret == 0 && signal)
771 vmbus_setevent(channel);
773 return ret;
775 EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer);
778 * vmbus_recvpacket() - Retrieve the user packet on the specified channel
779 * @channel: Pointer to vmbus_channel structure.
780 * @buffer: Pointer to the buffer you want to receive the data into.
781 * @bufferlen: Maximum size of what the the buffer will hold
782 * @buffer_actual_len: The actual size of the data after it was received
783 * @requestid: Identifier of the request
785 * Receives directly from the hyper-v vmbus and puts the data it received
786 * into Buffer. This will receive the data unparsed from hyper-v.
788 * Mainly used by Hyper-V drivers.
790 int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
791 u32 bufferlen, u32 *buffer_actual_len, u64 *requestid)
793 struct vmpacket_descriptor desc;
794 u32 packetlen;
795 u32 userlen;
796 int ret;
797 bool signal = false;
799 *buffer_actual_len = 0;
800 *requestid = 0;
803 ret = hv_ringbuffer_peek(&channel->inbound, &desc,
804 sizeof(struct vmpacket_descriptor));
805 if (ret != 0)
806 return 0;
808 packetlen = desc.len8 << 3;
809 userlen = packetlen - (desc.offset8 << 3);
811 *buffer_actual_len = userlen;
813 if (userlen > bufferlen) {
815 pr_err("Buffer too small - got %d needs %d\n",
816 bufferlen, userlen);
817 return -ETOOSMALL;
820 *requestid = desc.trans_id;
822 /* Copy over the packet to the user buffer */
823 ret = hv_ringbuffer_read(&channel->inbound, buffer, userlen,
824 (desc.offset8 << 3), &signal);
826 if (signal)
827 vmbus_setevent(channel);
829 return 0;
831 EXPORT_SYMBOL(vmbus_recvpacket);
834 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
836 int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
837 u32 bufferlen, u32 *buffer_actual_len,
838 u64 *requestid)
840 struct vmpacket_descriptor desc;
841 u32 packetlen;
842 int ret;
843 bool signal = false;
845 *buffer_actual_len = 0;
846 *requestid = 0;
849 ret = hv_ringbuffer_peek(&channel->inbound, &desc,
850 sizeof(struct vmpacket_descriptor));
851 if (ret != 0)
852 return 0;
855 packetlen = desc.len8 << 3;
857 *buffer_actual_len = packetlen;
859 if (packetlen > bufferlen) {
860 pr_err("Buffer too small - needed %d bytes but "
861 "got space for only %d bytes\n",
862 packetlen, bufferlen);
863 return -ENOBUFS;
866 *requestid = desc.trans_id;
868 /* Copy over the entire packet to the user buffer */
869 ret = hv_ringbuffer_read(&channel->inbound, buffer, packetlen, 0,
870 &signal);
872 if (signal)
873 vmbus_setevent(channel);
875 return 0;
877 EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);