Merge branch 'upstream' of git://git.linux-mips.org/pub/scm/upstream-linus
[linux-btrfs-devel.git] / drivers / staging / hv / channel.c
blob455f47a891f17980a71d13b7f82ec00f89f27ae4
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>
30 #include "hyperv.h"
31 #include "hyperv_vmbus.h"
33 #define NUM_PAGES_SPANNED(addr, len) \
34 ((PAGE_ALIGN(addr + len) >> PAGE_SHIFT) - (addr >> PAGE_SHIFT))
36 /* Internal routines */
37 static int create_gpadl_header(
38 void *kbuffer, /* must be phys and virt contiguous */
39 u32 size, /* page-size multiple */
40 struct vmbus_channel_msginfo **msginfo,
41 u32 *messagecount);
42 static void vmbus_setevent(struct vmbus_channel *channel);
45 * vmbus_setevent- Trigger an event notification on the specified
46 * channel.
48 static void vmbus_setevent(struct vmbus_channel *channel)
50 struct hv_monitor_page *monitorpage;
52 if (channel->offermsg.monitor_allocated) {
53 /* Each u32 represents 32 channels */
54 sync_set_bit(channel->offermsg.child_relid & 31,
55 (unsigned long *) vmbus_connection.send_int_page +
56 (channel->offermsg.child_relid >> 5));
58 monitorpage = vmbus_connection.monitor_pages;
59 monitorpage++; /* Get the child to parent monitor page */
61 sync_set_bit(channel->monitor_bit,
62 (unsigned long *)&monitorpage->trigger_group
63 [channel->monitor_grp].pending);
65 } else {
66 vmbus_set_event(channel->offermsg.child_relid);
71 * vmbus_get_debug_info -Retrieve various channel debug info
73 void vmbus_get_debug_info(struct vmbus_channel *channel,
74 struct vmbus_channel_debug_info *debuginfo)
76 struct hv_monitor_page *monitorpage;
77 u8 monitor_group = (u8)channel->offermsg.monitorid / 32;
78 u8 monitor_offset = (u8)channel->offermsg.monitorid % 32;
79 /* u32 monitorBit = 1 << monitorOffset; */
81 debuginfo->relid = channel->offermsg.child_relid;
82 debuginfo->state = channel->state;
83 memcpy(&debuginfo->interfacetype,
84 &channel->offermsg.offer.if_type, sizeof(struct hv_guid));
85 memcpy(&debuginfo->interface_instance,
86 &channel->offermsg.offer.if_instance,
87 sizeof(struct hv_guid));
89 monitorpage = (struct hv_monitor_page *)vmbus_connection.monitor_pages;
91 debuginfo->monitorid = channel->offermsg.monitorid;
93 debuginfo->servermonitor_pending =
94 monitorpage->trigger_group[monitor_group].pending;
95 debuginfo->servermonitor_latency =
96 monitorpage->latency[monitor_group][monitor_offset];
97 debuginfo->servermonitor_connectionid =
98 monitorpage->parameter[monitor_group]
99 [monitor_offset].connectionid.u.id;
101 monitorpage++;
103 debuginfo->clientmonitor_pending =
104 monitorpage->trigger_group[monitor_group].pending;
105 debuginfo->clientmonitor_latency =
106 monitorpage->latency[monitor_group][monitor_offset];
107 debuginfo->clientmonitor_connectionid =
108 monitorpage->parameter[monitor_group]
109 [monitor_offset].connectionid.u.id;
111 hv_ringbuffer_get_debuginfo(&channel->inbound, &debuginfo->inbound);
112 hv_ringbuffer_get_debuginfo(&channel->outbound, &debuginfo->outbound);
116 * vmbus_open - Open the specified channel.
118 int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size,
119 u32 recv_ringbuffer_size, void *userdata, u32 userdatalen,
120 void (*onchannelcallback)(void *context), void *context)
122 struct vmbus_channel_open_channel *openMsg;
123 struct vmbus_channel_msginfo *openInfo = NULL;
124 void *in, *out;
125 unsigned long flags;
126 int ret, t, err = 0;
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 errorout;
153 ret = hv_ringbuffer_init(
154 &newchannel->inbound, in, recv_ringbuffer_size);
155 if (ret != 0) {
156 err = ret;
157 goto errorout;
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 errorout;
175 /* Create and init the channel open message */
176 openInfo = kmalloc(sizeof(*openInfo) +
177 sizeof(struct vmbus_channel_open_channel),
178 GFP_KERNEL);
179 if (!openInfo) {
180 err = -ENOMEM;
181 goto errorout;
184 init_completion(&openInfo->waitevent);
186 openMsg = (struct vmbus_channel_open_channel *)openInfo->msg;
187 openMsg->header.msgtype = CHANNELMSG_OPENCHANNEL;
188 openMsg->openid = newchannel->offermsg.child_relid;
189 openMsg->child_relid = newchannel->offermsg.child_relid;
190 openMsg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle;
191 openMsg->downstream_ringbuffer_pageoffset = send_ringbuffer_size >>
192 PAGE_SHIFT;
193 openMsg->server_contextarea_gpadlhandle = 0;
195 if (userdatalen > MAX_USER_DEFINED_BYTES) {
196 err = -EINVAL;
197 goto errorout;
200 if (userdatalen)
201 memcpy(openMsg->userdata, userdata, userdatalen);
203 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
204 list_add_tail(&openInfo->msglistentry,
205 &vmbus_connection.chn_msg_list);
206 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
208 ret = vmbus_post_msg(openMsg,
209 sizeof(struct vmbus_channel_open_channel));
211 if (ret != 0)
212 goto cleanup;
214 t = wait_for_completion_timeout(&openInfo->waitevent, 5*HZ);
215 if (t == 0) {
216 err = -ETIMEDOUT;
217 goto errorout;
221 if (openInfo->response.open_result.status)
222 err = openInfo->response.open_result.status;
224 cleanup:
225 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
226 list_del(&openInfo->msglistentry);
227 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
229 kfree(openInfo);
230 return err;
232 errorout:
233 hv_ringbuffer_cleanup(&newchannel->outbound);
234 hv_ringbuffer_cleanup(&newchannel->inbound);
235 free_pages((unsigned long)out,
236 get_order(send_ringbuffer_size + recv_ringbuffer_size));
237 kfree(openInfo);
238 return err;
240 EXPORT_SYMBOL_GPL(vmbus_open);
243 * dump_gpadl_body - Dump the gpadl body message to the console for
244 * debugging purposes.
246 static void dump_gpadl_body(struct vmbus_channel_gpadl_body *gpadl, u32 len)
248 int i;
249 int pfncount;
251 pfncount = (len - sizeof(struct vmbus_channel_gpadl_body)) /
252 sizeof(u64);
254 DPRINT_DBG(VMBUS, "gpadl body - len %d pfn count %d", len, pfncount);
256 for (i = 0; i < pfncount; i++)
257 DPRINT_DBG(VMBUS, "gpadl body - %d) pfn %llu",
258 i, gpadl->pfn[i]);
262 * dump_gpadl_header - Dump the gpadl header message to the console for
263 * debugging purposes.
265 static void dump_gpadl_header(struct vmbus_channel_gpadl_header *gpadl)
267 int i, j;
268 int pagecount;
270 DPRINT_DBG(VMBUS,
271 "gpadl header - relid %d, range count %d, range buflen %d",
272 gpadl->child_relid, gpadl->rangecount, gpadl->range_buflen);
273 for (i = 0; i < gpadl->rangecount; i++) {
274 pagecount = gpadl->range[i].byte_count >> PAGE_SHIFT;
275 pagecount = (pagecount > 26) ? 26 : pagecount;
277 DPRINT_DBG(VMBUS, "gpadl range %d - len %d offset %d "
278 "page count %d", i, gpadl->range[i].byte_count,
279 gpadl->range[i].byte_offset, pagecount);
281 for (j = 0; j < pagecount; j++)
282 DPRINT_DBG(VMBUS, "%d) pfn %llu", j,
283 gpadl->range[i].pfn_array[j]);
288 * create_gpadl_header - Creates a gpadl for the specified buffer
290 static int create_gpadl_header(void *kbuffer, u32 size,
291 struct vmbus_channel_msginfo **msginfo,
292 u32 *messagecount)
294 int i;
295 int pagecount;
296 unsigned long long pfn;
297 struct vmbus_channel_gpadl_header *gpadl_header;
298 struct vmbus_channel_gpadl_body *gpadl_body;
299 struct vmbus_channel_msginfo *msgheader;
300 struct vmbus_channel_msginfo *msgbody = NULL;
301 u32 msgsize;
303 int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
305 pagecount = size >> PAGE_SHIFT;
306 pfn = virt_to_phys(kbuffer) >> PAGE_SHIFT;
308 /* do we need a gpadl body msg */
309 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
310 sizeof(struct vmbus_channel_gpadl_header) -
311 sizeof(struct gpa_range);
312 pfncount = pfnsize / sizeof(u64);
314 if (pagecount > pfncount) {
315 /* we need a gpadl body */
316 /* fill in the header */
317 msgsize = sizeof(struct vmbus_channel_msginfo) +
318 sizeof(struct vmbus_channel_gpadl_header) +
319 sizeof(struct gpa_range) + pfncount * sizeof(u64);
320 msgheader = kzalloc(msgsize, GFP_KERNEL);
321 if (!msgheader)
322 goto nomem;
324 INIT_LIST_HEAD(&msgheader->submsglist);
325 msgheader->msgsize = msgsize;
327 gpadl_header = (struct vmbus_channel_gpadl_header *)
328 msgheader->msg;
329 gpadl_header->rangecount = 1;
330 gpadl_header->range_buflen = sizeof(struct gpa_range) +
331 pagecount * sizeof(u64);
332 gpadl_header->range[0].byte_offset = 0;
333 gpadl_header->range[0].byte_count = size;
334 for (i = 0; i < pfncount; i++)
335 gpadl_header->range[0].pfn_array[i] = pfn+i;
336 *msginfo = msgheader;
337 *messagecount = 1;
339 pfnsum = pfncount;
340 pfnleft = pagecount - pfncount;
342 /* how many pfns can we fit */
343 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
344 sizeof(struct vmbus_channel_gpadl_body);
345 pfncount = pfnsize / sizeof(u64);
347 /* fill in the body */
348 while (pfnleft) {
349 if (pfnleft > pfncount)
350 pfncurr = pfncount;
351 else
352 pfncurr = pfnleft;
354 msgsize = sizeof(struct vmbus_channel_msginfo) +
355 sizeof(struct vmbus_channel_gpadl_body) +
356 pfncurr * sizeof(u64);
357 msgbody = kzalloc(msgsize, GFP_KERNEL);
359 if (!msgbody) {
360 struct vmbus_channel_msginfo *pos = NULL;
361 struct vmbus_channel_msginfo *tmp = NULL;
363 * Free up all the allocated messages.
365 list_for_each_entry_safe(pos, tmp,
366 &msgheader->submsglist,
367 msglistentry) {
369 list_del(&pos->msglistentry);
370 kfree(pos);
373 goto nomem;
376 msgbody->msgsize = msgsize;
377 (*messagecount)++;
378 gpadl_body =
379 (struct vmbus_channel_gpadl_body *)msgbody->msg;
382 * Gpadl is u32 and we are using a pointer which could
383 * be 64-bit
384 * This is governed by the guest/host protocol and
385 * so the hypervisor gurantees that this is ok.
387 for (i = 0; i < pfncurr; i++)
388 gpadl_body->pfn[i] = pfn + pfnsum + i;
390 /* add to msg header */
391 list_add_tail(&msgbody->msglistentry,
392 &msgheader->submsglist);
393 pfnsum += pfncurr;
394 pfnleft -= pfncurr;
396 } else {
397 /* everything fits in a header */
398 msgsize = sizeof(struct vmbus_channel_msginfo) +
399 sizeof(struct vmbus_channel_gpadl_header) +
400 sizeof(struct gpa_range) + pagecount * sizeof(u64);
401 msgheader = kzalloc(msgsize, GFP_KERNEL);
402 if (msgheader == NULL)
403 goto nomem;
404 msgheader->msgsize = msgsize;
406 gpadl_header = (struct vmbus_channel_gpadl_header *)
407 msgheader->msg;
408 gpadl_header->rangecount = 1;
409 gpadl_header->range_buflen = sizeof(struct gpa_range) +
410 pagecount * sizeof(u64);
411 gpadl_header->range[0].byte_offset = 0;
412 gpadl_header->range[0].byte_count = size;
413 for (i = 0; i < pagecount; i++)
414 gpadl_header->range[0].pfn_array[i] = pfn+i;
416 *msginfo = msgheader;
417 *messagecount = 1;
420 return 0;
421 nomem:
422 kfree(msgheader);
423 kfree(msgbody);
424 return -ENOMEM;
428 * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
430 * @channel: a channel
431 * @kbuffer: from kmalloc
432 * @size: page-size multiple
433 * @gpadl_handle: some funky thing
435 int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
436 u32 size, u32 *gpadl_handle)
438 struct vmbus_channel_gpadl_header *gpadlmsg;
439 struct vmbus_channel_gpadl_body *gpadl_body;
440 /* struct vmbus_channel_gpadl_created *gpadlCreated; */
441 struct vmbus_channel_msginfo *msginfo = NULL;
442 struct vmbus_channel_msginfo *submsginfo;
443 u32 msgcount;
444 struct list_head *curr;
445 u32 next_gpadl_handle;
446 unsigned long flags;
447 int ret = 0;
448 int t;
450 next_gpadl_handle = atomic_read(&vmbus_connection.next_gpadl_handle);
451 atomic_inc(&vmbus_connection.next_gpadl_handle);
453 ret = create_gpadl_header(kbuffer, size, &msginfo, &msgcount);
454 if (ret)
455 return ret;
457 init_completion(&msginfo->waitevent);
459 gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
460 gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
461 gpadlmsg->child_relid = channel->offermsg.child_relid;
462 gpadlmsg->gpadl = next_gpadl_handle;
464 dump_gpadl_header(gpadlmsg);
466 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
467 list_add_tail(&msginfo->msglistentry,
468 &vmbus_connection.chn_msg_list);
470 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
472 ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
473 sizeof(*msginfo));
474 if (ret != 0)
475 goto cleanup;
477 if (msgcount > 1) {
478 list_for_each(curr, &msginfo->submsglist) {
480 submsginfo = (struct vmbus_channel_msginfo *)curr;
481 gpadl_body =
482 (struct vmbus_channel_gpadl_body *)submsginfo->msg;
484 gpadl_body->header.msgtype =
485 CHANNELMSG_GPADL_BODY;
486 gpadl_body->gpadl = next_gpadl_handle;
488 dump_gpadl_body(gpadl_body, submsginfo->msgsize -
489 sizeof(*submsginfo));
490 ret = vmbus_post_msg(gpadl_body,
491 submsginfo->msgsize -
492 sizeof(*submsginfo));
493 if (ret != 0)
494 goto cleanup;
498 t = wait_for_completion_timeout(&msginfo->waitevent, 5*HZ);
499 BUG_ON(t == 0);
502 /* At this point, we received the gpadl created msg */
503 *gpadl_handle = gpadlmsg->gpadl;
505 cleanup:
506 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
507 list_del(&msginfo->msglistentry);
508 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
510 kfree(msginfo);
511 return ret;
513 EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
516 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
518 int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
520 struct vmbus_channel_gpadl_teardown *msg;
521 struct vmbus_channel_msginfo *info;
522 unsigned long flags;
523 int ret, t;
525 /* ASSERT(gpadl_handle != 0); */
527 info = kmalloc(sizeof(*info) +
528 sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
529 if (!info)
530 return -ENOMEM;
532 init_completion(&info->waitevent);
534 msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
536 msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
537 msg->child_relid = channel->offermsg.child_relid;
538 msg->gpadl = gpadl_handle;
540 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
541 list_add_tail(&info->msglistentry,
542 &vmbus_connection.chn_msg_list);
543 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
544 ret = vmbus_post_msg(msg,
545 sizeof(struct vmbus_channel_gpadl_teardown));
547 BUG_ON(ret != 0);
548 t = wait_for_completion_timeout(&info->waitevent, 5*HZ);
549 BUG_ON(t == 0);
551 /* Received a torndown response */
552 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
553 list_del(&info->msglistentry);
554 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
556 kfree(info);
557 return ret;
559 EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
562 * vmbus_close - Close the specified channel
564 void vmbus_close(struct vmbus_channel *channel)
566 struct vmbus_channel_close_channel *msg;
567 int ret;
569 /* Stop callback and cancel the timer asap */
570 channel->onchannel_callback = NULL;
572 /* Send a closing message */
574 msg = &channel->close_msg.msg;
576 msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
577 msg->child_relid = channel->offermsg.child_relid;
579 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel));
581 BUG_ON(ret != 0);
582 /* Tear down the gpadl for the channel's ring buffer */
583 if (channel->ringbuffer_gpadlhandle)
584 vmbus_teardown_gpadl(channel,
585 channel->ringbuffer_gpadlhandle);
587 /* Cleanup the ring buffers for this channel */
588 hv_ringbuffer_cleanup(&channel->outbound);
589 hv_ringbuffer_cleanup(&channel->inbound);
591 free_pages((unsigned long)channel->ringbuffer_pages,
592 get_order(channel->ringbuffer_pagecount * PAGE_SIZE));
596 EXPORT_SYMBOL_GPL(vmbus_close);
599 * vmbus_sendpacket() - Send the specified buffer on the given channel
600 * @channel: Pointer to vmbus_channel structure.
601 * @buffer: Pointer to the buffer you want to receive the data into.
602 * @bufferlen: Maximum size of what the the buffer will hold
603 * @requestid: Identifier of the request
604 * @type: Type of packet that is being send e.g. negotiate, time
605 * packet etc.
607 * Sends data in @buffer directly to hyper-v via the vmbus
608 * This will send the data unparsed to hyper-v.
610 * Mainly used by Hyper-V drivers.
612 int vmbus_sendpacket(struct vmbus_channel *channel, const void *buffer,
613 u32 bufferlen, u64 requestid,
614 enum vmbus_packet_type type, u32 flags)
616 struct vmpacket_descriptor desc;
617 u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
618 u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
619 struct scatterlist bufferlist[3];
620 u64 aligned_data = 0;
621 int ret;
624 /* Setup the descriptor */
625 desc.type = type; /* VmbusPacketTypeDataInBand; */
626 desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
627 /* in 8-bytes granularity */
628 desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
629 desc.len8 = (u16)(packetlen_aligned >> 3);
630 desc.trans_id = requestid;
632 sg_init_table(bufferlist, 3);
633 sg_set_buf(&bufferlist[0], &desc, sizeof(struct vmpacket_descriptor));
634 sg_set_buf(&bufferlist[1], buffer, bufferlen);
635 sg_set_buf(&bufferlist[2], &aligned_data,
636 packetlen_aligned - packetlen);
638 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3);
640 if (ret == 0 && !hv_get_ringbuffer_interrupt_mask(&channel->outbound))
641 vmbus_setevent(channel);
643 return ret;
645 EXPORT_SYMBOL(vmbus_sendpacket);
648 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
649 * packets using a GPADL Direct packet type.
651 int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
652 struct hv_page_buffer pagebuffers[],
653 u32 pagecount, void *buffer, u32 bufferlen,
654 u64 requestid)
656 int ret;
657 int i;
658 struct vmbus_channel_packet_page_buffer desc;
659 u32 descsize;
660 u32 packetlen;
661 u32 packetlen_aligned;
662 struct scatterlist bufferlist[3];
663 u64 aligned_data = 0;
665 if (pagecount > MAX_PAGE_BUFFER_COUNT)
666 return -EINVAL;
670 * Adjust the size down since vmbus_channel_packet_page_buffer is the
671 * largest size we support
673 descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
674 ((MAX_PAGE_BUFFER_COUNT - pagecount) *
675 sizeof(struct hv_page_buffer));
676 packetlen = descsize + bufferlen;
677 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
679 /* Setup the descriptor */
680 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
681 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
682 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
683 desc.length8 = (u16)(packetlen_aligned >> 3);
684 desc.transactionid = requestid;
685 desc.rangecount = pagecount;
687 for (i = 0; i < pagecount; i++) {
688 desc.range[i].len = pagebuffers[i].len;
689 desc.range[i].offset = pagebuffers[i].offset;
690 desc.range[i].pfn = pagebuffers[i].pfn;
693 sg_init_table(bufferlist, 3);
694 sg_set_buf(&bufferlist[0], &desc, descsize);
695 sg_set_buf(&bufferlist[1], buffer, bufferlen);
696 sg_set_buf(&bufferlist[2], &aligned_data,
697 packetlen_aligned - packetlen);
699 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3);
701 if (ret == 0 && !hv_get_ringbuffer_interrupt_mask(&channel->outbound))
702 vmbus_setevent(channel);
704 return ret;
706 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
709 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
710 * using a GPADL Direct packet type.
712 int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
713 struct hv_multipage_buffer *multi_pagebuffer,
714 void *buffer, u32 bufferlen, u64 requestid)
716 int ret;
717 struct vmbus_channel_packet_multipage_buffer desc;
718 u32 descsize;
719 u32 packetlen;
720 u32 packetlen_aligned;
721 struct scatterlist bufferlist[3];
722 u64 aligned_data = 0;
723 u32 pfncount = NUM_PAGES_SPANNED(multi_pagebuffer->offset,
724 multi_pagebuffer->len);
727 if ((pfncount < 0) || (pfncount > MAX_MULTIPAGE_BUFFER_COUNT))
728 return -EINVAL;
731 * Adjust the size down since vmbus_channel_packet_multipage_buffer is
732 * the largest size we support
734 descsize = sizeof(struct vmbus_channel_packet_multipage_buffer) -
735 ((MAX_MULTIPAGE_BUFFER_COUNT - pfncount) *
736 sizeof(u64));
737 packetlen = descsize + bufferlen;
738 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
741 /* Setup the descriptor */
742 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
743 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
744 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
745 desc.length8 = (u16)(packetlen_aligned >> 3);
746 desc.transactionid = requestid;
747 desc.rangecount = 1;
749 desc.range.len = multi_pagebuffer->len;
750 desc.range.offset = multi_pagebuffer->offset;
752 memcpy(desc.range.pfn_array, multi_pagebuffer->pfn_array,
753 pfncount * sizeof(u64));
755 sg_init_table(bufferlist, 3);
756 sg_set_buf(&bufferlist[0], &desc, descsize);
757 sg_set_buf(&bufferlist[1], buffer, bufferlen);
758 sg_set_buf(&bufferlist[2], &aligned_data,
759 packetlen_aligned - packetlen);
761 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3);
763 if (ret == 0 && !hv_get_ringbuffer_interrupt_mask(&channel->outbound))
764 vmbus_setevent(channel);
766 return ret;
768 EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer);
771 * vmbus_recvpacket() - Retrieve the user packet on the specified channel
772 * @channel: Pointer to vmbus_channel structure.
773 * @buffer: Pointer to the buffer you want to receive the data into.
774 * @bufferlen: Maximum size of what the the buffer will hold
775 * @buffer_actual_len: The actual size of the data after it was received
776 * @requestid: Identifier of the request
778 * Receives directly from the hyper-v vmbus and puts the data it received
779 * into Buffer. This will receive the data unparsed from hyper-v.
781 * Mainly used by Hyper-V drivers.
783 int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
784 u32 bufferlen, u32 *buffer_actual_len, u64 *requestid)
786 struct vmpacket_descriptor desc;
787 u32 packetlen;
788 u32 userlen;
789 int ret;
790 unsigned long flags;
792 *buffer_actual_len = 0;
793 *requestid = 0;
795 spin_lock_irqsave(&channel->inbound_lock, flags);
797 ret = hv_ringbuffer_peek(&channel->inbound, &desc,
798 sizeof(struct vmpacket_descriptor));
799 if (ret != 0) {
800 spin_unlock_irqrestore(&channel->inbound_lock, flags);
801 return 0;
804 packetlen = desc.len8 << 3;
805 userlen = packetlen - (desc.offset8 << 3);
807 *buffer_actual_len = userlen;
809 if (userlen > bufferlen) {
810 spin_unlock_irqrestore(&channel->inbound_lock, flags);
812 pr_err("Buffer too small - got %d needs %d\n",
813 bufferlen, userlen);
814 return -ETOOSMALL;
817 *requestid = desc.trans_id;
819 /* Copy over the packet to the user buffer */
820 ret = hv_ringbuffer_read(&channel->inbound, buffer, userlen,
821 (desc.offset8 << 3));
823 spin_unlock_irqrestore(&channel->inbound_lock, flags);
825 return 0;
827 EXPORT_SYMBOL(vmbus_recvpacket);
830 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
832 int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
833 u32 bufferlen, u32 *buffer_actual_len,
834 u64 *requestid)
836 struct vmpacket_descriptor desc;
837 u32 packetlen;
838 u32 userlen;
839 int ret;
840 unsigned long flags;
842 *buffer_actual_len = 0;
843 *requestid = 0;
845 spin_lock_irqsave(&channel->inbound_lock, flags);
847 ret = hv_ringbuffer_peek(&channel->inbound, &desc,
848 sizeof(struct vmpacket_descriptor));
849 if (ret != 0) {
850 spin_unlock_irqrestore(&channel->inbound_lock, flags);
851 return 0;
855 packetlen = desc.len8 << 3;
856 userlen = packetlen - (desc.offset8 << 3);
858 *buffer_actual_len = packetlen;
860 if (packetlen > bufferlen) {
861 spin_unlock_irqrestore(&channel->inbound_lock, flags);
863 pr_err("Buffer too small - needed %d bytes but "
864 "got space for only %d bytes\n",
865 packetlen, bufferlen);
866 return -2;
869 *requestid = desc.trans_id;
871 /* Copy over the entire packet to the user buffer */
872 ret = hv_ringbuffer_read(&channel->inbound, buffer, packetlen, 0);
874 spin_unlock_irqrestore(&channel->inbound_lock, flags);
875 return 0;
877 EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);