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
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.
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>
27 #include <linux/slab.h>
28 #include <linux/module.h>
31 #include "vmbus_private.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,
42 static void dump_vmbus_channel(struct vmbus_channel *channel);
43 static void vmbus_setevent(struct vmbus_channel *channel);
47 static void DumpMonitorPage(struct hv_monitor_page *MonitorPage)
52 DPRINT_DBG(VMBUS, "monitorPage - %p, trigger state - %d",
53 MonitorPage, MonitorPage->trigger_state);
55 for (i = 0; i < 4; i++)
56 DPRINT_DBG(VMBUS, "trigger group (%d) - %llx", i,
57 MonitorPage->trigger_group[i].as_uint64);
59 for (i = 0; i < 4; i++) {
60 for (j = 0; j < 32; j++) {
61 DPRINT_DBG(VMBUS, "latency (%d)(%d) - %llx", i, j,
62 MonitorPage->latency[i][j]);
65 for (i = 0; i < 4; i++) {
66 for (j = 0; j < 32; j++) {
67 DPRINT_DBG(VMBUS, "param-conn id (%d)(%d) - %d", i, j,
68 MonitorPage->parameter[i][j].connectionid.asu32);
69 DPRINT_DBG(VMBUS, "param-flag (%d)(%d) - %d", i, j,
70 MonitorPage->parameter[i][j].flag_number);
77 * vmbus_setevent- Trigger an event notification on the specified
80 static void vmbus_setevent(struct vmbus_channel *channel)
82 struct hv_monitor_page *monitorpage;
84 if (channel->offermsg.monitor_allocated) {
85 /* Each u32 represents 32 channels */
86 sync_set_bit(channel->offermsg.child_relid & 31,
87 (unsigned long *) vmbus_connection.send_int_page +
88 (channel->offermsg.child_relid >> 5));
90 monitorpage = vmbus_connection.monitor_pages;
91 monitorpage++; /* Get the child to parent monitor page */
93 sync_set_bit(channel->monitor_bit,
94 (unsigned long *)&monitorpage->trigger_group
95 [channel->monitor_grp].pending);
98 vmbus_set_event(channel->offermsg.child_relid);
103 static void VmbusChannelClearEvent(struct vmbus_channel *channel)
105 struct hv_monitor_page *monitorPage;
107 if (Channel->offermsg.monitor_allocated) {
108 /* Each u32 represents 32 channels */
109 sync_clear_bit(Channel->offermsg.child_relid & 31,
110 (unsigned long *)vmbus_connection.send_int_page +
111 (Channel->offermsg.child_relid >> 5));
113 monitorPage = (struct hv_monitor_page *)
114 vmbus_connection.monitor_pages;
115 monitorPage++; /* Get the child to parent monitor page */
117 sync_clear_bit(Channel->monitor_bit,
118 (unsigned long *)&monitorPage->trigger_group
119 [Channel->monitor_grp].Pending);
125 * vmbus_get_debug_info -Retrieve various channel debug info
127 void vmbus_get_debug_info(struct vmbus_channel *channel,
128 struct vmbus_channel_debug_info *debuginfo)
130 struct hv_monitor_page *monitorpage;
131 u8 monitor_group = (u8)channel->offermsg.monitorid / 32;
132 u8 monitor_offset = (u8)channel->offermsg.monitorid % 32;
133 /* u32 monitorBit = 1 << monitorOffset; */
135 debuginfo->relid = channel->offermsg.child_relid;
136 debuginfo->state = channel->state;
137 memcpy(&debuginfo->interfacetype,
138 &channel->offermsg.offer.if_type, sizeof(struct hv_guid));
139 memcpy(&debuginfo->interface_instance,
140 &channel->offermsg.offer.if_instance,
141 sizeof(struct hv_guid));
143 monitorpage = (struct hv_monitor_page *)vmbus_connection.monitor_pages;
145 debuginfo->monitorid = channel->offermsg.monitorid;
147 debuginfo->servermonitor_pending =
148 monitorpage->trigger_group[monitor_group].pending;
149 debuginfo->servermonitor_latency =
150 monitorpage->latency[monitor_group][monitor_offset];
151 debuginfo->servermonitor_connectionid =
152 monitorpage->parameter[monitor_group]
153 [monitor_offset].connectionid.u.id;
157 debuginfo->clientmonitor_pending =
158 monitorpage->trigger_group[monitor_group].pending;
159 debuginfo->clientmonitor_latency =
160 monitorpage->latency[monitor_group][monitor_offset];
161 debuginfo->clientmonitor_connectionid =
162 monitorpage->parameter[monitor_group]
163 [monitor_offset].connectionid.u.id;
165 ringbuffer_get_debuginfo(&channel->inbound, &debuginfo->inbound);
166 ringbuffer_get_debuginfo(&channel->outbound, &debuginfo->outbound);
170 * vmbus_open - Open the specified channel.
172 int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size,
173 u32 recv_ringbuffer_size, void *userdata, u32 userdatalen,
174 void (*onchannelcallback)(void *context), void *context)
176 struct vmbus_channel_open_channel *openMsg;
177 struct vmbus_channel_msginfo *openInfo = NULL;
182 /* Aligned to page size */
183 /* ASSERT(!(SendRingBufferSize & (PAGE_SIZE - 1))); */
184 /* ASSERT(!(RecvRingBufferSize & (PAGE_SIZE - 1))); */
186 newchannel->onchannel_callback = onchannelcallback;
187 newchannel->channel_callback_context = context;
189 /* Allocate the ring buffer */
190 out = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO,
191 get_order(send_ringbuffer_size + recv_ringbuffer_size));
196 /* ASSERT(((unsigned long)out & (PAGE_SIZE-1)) == 0); */
198 in = (void *)((unsigned long)out + send_ringbuffer_size);
200 newchannel->ringbuffer_pages = out;
201 newchannel->ringbuffer_pagecount = (send_ringbuffer_size +
202 recv_ringbuffer_size) >> PAGE_SHIFT;
204 ret = hv_ringbuffer_init(
205 &newchannel->outbound, out, send_ringbuffer_size);
212 ret = hv_ringbuffer_init(
213 &newchannel->inbound, in, recv_ringbuffer_size);
220 /* Establish the gpadl for the ring buffer */
221 newchannel->ringbuffer_gpadlhandle = 0;
223 ret = vmbus_establish_gpadl(newchannel,
224 newchannel->outbound.ring_buffer,
225 send_ringbuffer_size +
226 recv_ringbuffer_size,
227 &newchannel->ringbuffer_gpadlhandle);
234 /* Create and init the channel open message */
235 openInfo = kmalloc(sizeof(*openInfo) +
236 sizeof(struct vmbus_channel_open_channel),
243 init_waitqueue_head(&openInfo->waitevent);
245 openMsg = (struct vmbus_channel_open_channel *)openInfo->msg;
246 openMsg->header.msgtype = CHANNELMSG_OPENCHANNEL;
247 openMsg->openid = newchannel->offermsg.child_relid; /* FIXME */
248 openMsg->child_relid = newchannel->offermsg.child_relid;
249 openMsg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle;
250 openMsg->downstream_ringbuffer_pageoffset = send_ringbuffer_size >>
252 openMsg->server_contextarea_gpadlhandle = 0; /* TODO */
254 if (userdatalen > MAX_USER_DEFINED_BYTES) {
260 memcpy(openMsg->userdata, userdata, userdatalen);
262 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
263 list_add_tail(&openInfo->msglistentry,
264 &vmbus_connection.chn_msg_list);
265 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
267 ret = vmbus_post_msg(openMsg,
268 sizeof(struct vmbus_channel_open_channel));
273 openInfo->wait_condition = 0;
274 wait_event_timeout(openInfo->waitevent,
275 openInfo->wait_condition,
276 msecs_to_jiffies(1000));
277 if (openInfo->wait_condition == 0) {
283 if (openInfo->response.open_result.status)
284 err = openInfo->response.open_result.status;
287 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
288 list_del(&openInfo->msglistentry);
289 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
295 hv_ringbuffer_cleanup(&newchannel->outbound);
296 hv_ringbuffer_cleanup(&newchannel->inbound);
297 free_pages((unsigned long)out,
298 get_order(send_ringbuffer_size + recv_ringbuffer_size));
302 EXPORT_SYMBOL_GPL(vmbus_open);
305 * dump_gpadl_body - Dump the gpadl body message to the console for
306 * debugging purposes.
308 static void dump_gpadl_body(struct vmbus_channel_gpadl_body *gpadl, u32 len)
313 pfncount = (len - sizeof(struct vmbus_channel_gpadl_body)) /
316 DPRINT_DBG(VMBUS, "gpadl body - len %d pfn count %d", len, pfncount);
318 for (i = 0; i < pfncount; i++)
319 DPRINT_DBG(VMBUS, "gpadl body - %d) pfn %llu",
324 * dump_gpadl_header - Dump the gpadl header message to the console for
325 * debugging purposes.
327 static void dump_gpadl_header(struct vmbus_channel_gpadl_header *gpadl)
333 "gpadl header - relid %d, range count %d, range buflen %d",
334 gpadl->child_relid, gpadl->rangecount, gpadl->range_buflen);
335 for (i = 0; i < gpadl->rangecount; i++) {
336 pagecount = gpadl->range[i].byte_count >> PAGE_SHIFT;
337 pagecount = (pagecount > 26) ? 26 : pagecount;
339 DPRINT_DBG(VMBUS, "gpadl range %d - len %d offset %d "
340 "page count %d", i, gpadl->range[i].byte_count,
341 gpadl->range[i].byte_offset, pagecount);
343 for (j = 0; j < pagecount; j++)
344 DPRINT_DBG(VMBUS, "%d) pfn %llu", j,
345 gpadl->range[i].pfn_array[j]);
350 * create_gpadl_header - Creates a gpadl for the specified buffer
352 static int create_gpadl_header(void *kbuffer, u32 size,
353 struct vmbus_channel_msginfo **msginfo,
358 unsigned long long pfn;
359 struct vmbus_channel_gpadl_header *gpadl_header;
360 struct vmbus_channel_gpadl_body *gpadl_body;
361 struct vmbus_channel_msginfo *msgheader;
362 struct vmbus_channel_msginfo *msgbody = NULL;
365 int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
367 /* ASSERT((kbuffer & (PAGE_SIZE-1)) == 0); */
368 /* ASSERT((Size & (PAGE_SIZE-1)) == 0); */
370 pagecount = size >> PAGE_SHIFT;
371 pfn = virt_to_phys(kbuffer) >> PAGE_SHIFT;
373 /* do we need a gpadl body msg */
374 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
375 sizeof(struct vmbus_channel_gpadl_header) -
376 sizeof(struct gpa_range);
377 pfncount = pfnsize / sizeof(u64);
379 if (pagecount > pfncount) {
380 /* we need a gpadl body */
381 /* fill in the header */
382 msgsize = sizeof(struct vmbus_channel_msginfo) +
383 sizeof(struct vmbus_channel_gpadl_header) +
384 sizeof(struct gpa_range) + pfncount * sizeof(u64);
385 msgheader = kzalloc(msgsize, GFP_KERNEL);
389 INIT_LIST_HEAD(&msgheader->submsglist);
390 msgheader->msgsize = msgsize;
392 gpadl_header = (struct vmbus_channel_gpadl_header *)
394 gpadl_header->rangecount = 1;
395 gpadl_header->range_buflen = sizeof(struct gpa_range) +
396 pagecount * sizeof(u64);
397 gpadl_header->range[0].byte_offset = 0;
398 gpadl_header->range[0].byte_count = size;
399 for (i = 0; i < pfncount; i++)
400 gpadl_header->range[0].pfn_array[i] = pfn+i;
401 *msginfo = msgheader;
405 pfnleft = pagecount - pfncount;
407 /* how many pfns can we fit */
408 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
409 sizeof(struct vmbus_channel_gpadl_body);
410 pfncount = pfnsize / sizeof(u64);
412 /* fill in the body */
414 if (pfnleft > pfncount)
419 msgsize = sizeof(struct vmbus_channel_msginfo) +
420 sizeof(struct vmbus_channel_gpadl_body) +
421 pfncurr * sizeof(u64);
422 msgbody = kzalloc(msgsize, GFP_KERNEL);
423 /* FIXME: we probably need to more if this fails */
426 msgbody->msgsize = msgsize;
429 (struct vmbus_channel_gpadl_body *)msgbody->msg;
433 * Gpadl is u32 and we are using a pointer which could
436 /* gpadl_body->Gpadl = kbuffer; */
437 for (i = 0; i < pfncurr; i++)
438 gpadl_body->pfn[i] = pfn + pfnsum + i;
440 /* add to msg header */
441 list_add_tail(&msgbody->msglistentry,
442 &msgheader->submsglist);
447 /* everything fits in a header */
448 msgsize = sizeof(struct vmbus_channel_msginfo) +
449 sizeof(struct vmbus_channel_gpadl_header) +
450 sizeof(struct gpa_range) + pagecount * sizeof(u64);
451 msgheader = kzalloc(msgsize, GFP_KERNEL);
452 if (msgheader == NULL)
454 msgheader->msgsize = msgsize;
456 gpadl_header = (struct vmbus_channel_gpadl_header *)
458 gpadl_header->rangecount = 1;
459 gpadl_header->range_buflen = sizeof(struct gpa_range) +
460 pagecount * sizeof(u64);
461 gpadl_header->range[0].byte_offset = 0;
462 gpadl_header->range[0].byte_count = size;
463 for (i = 0; i < pagecount; i++)
464 gpadl_header->range[0].pfn_array[i] = pfn+i;
466 *msginfo = msgheader;
478 * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
480 * @channel: a channel
481 * @kbuffer: from kmalloc
482 * @size: page-size multiple
483 * @gpadl_handle: some funky thing
485 int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
486 u32 size, u32 *gpadl_handle)
488 struct vmbus_channel_gpadl_header *gpadlmsg;
489 struct vmbus_channel_gpadl_body *gpadl_body;
490 /* struct vmbus_channel_gpadl_created *gpadlCreated; */
491 struct vmbus_channel_msginfo *msginfo = NULL;
492 struct vmbus_channel_msginfo *submsginfo;
494 struct list_head *curr;
495 u32 next_gpadl_handle;
499 next_gpadl_handle = atomic_read(&vmbus_connection.next_gpadl_handle);
500 atomic_inc(&vmbus_connection.next_gpadl_handle);
502 ret = create_gpadl_header(kbuffer, size, &msginfo, &msgcount);
506 init_waitqueue_head(&msginfo->waitevent);
508 gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
509 gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
510 gpadlmsg->child_relid = channel->offermsg.child_relid;
511 gpadlmsg->gpadl = next_gpadl_handle;
513 dump_gpadl_header(gpadlmsg);
515 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
516 list_add_tail(&msginfo->msglistentry,
517 &vmbus_connection.chn_msg_list);
519 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
521 msginfo->wait_condition = 0;
522 ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
528 list_for_each(curr, &msginfo->submsglist) {
530 /* FIXME: should this use list_entry() instead ? */
531 submsginfo = (struct vmbus_channel_msginfo *)curr;
533 (struct vmbus_channel_gpadl_body *)submsginfo->msg;
535 gpadl_body->header.msgtype =
536 CHANNELMSG_GPADL_BODY;
537 gpadl_body->gpadl = next_gpadl_handle;
539 dump_gpadl_body(gpadl_body, submsginfo->msgsize -
540 sizeof(*submsginfo));
541 ret = vmbus_post_msg(gpadl_body,
542 submsginfo->msgsize -
543 sizeof(*submsginfo));
549 wait_event_timeout(msginfo->waitevent,
550 msginfo->wait_condition,
551 msecs_to_jiffies(1000));
552 BUG_ON(msginfo->wait_condition == 0);
555 /* At this point, we received the gpadl created msg */
556 *gpadl_handle = gpadlmsg->gpadl;
559 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
560 list_del(&msginfo->msglistentry);
561 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
566 EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
569 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
571 int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
573 struct vmbus_channel_gpadl_teardown *msg;
574 struct vmbus_channel_msginfo *info;
578 /* ASSERT(gpadl_handle != 0); */
580 info = kmalloc(sizeof(*info) +
581 sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
585 init_waitqueue_head(&info->waitevent);
587 msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
589 msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
590 msg->child_relid = channel->offermsg.child_relid;
591 msg->gpadl = gpadl_handle;
593 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
594 list_add_tail(&info->msglistentry,
595 &vmbus_connection.chn_msg_list);
596 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
597 info->wait_condition = 0;
598 ret = vmbus_post_msg(msg,
599 sizeof(struct vmbus_channel_gpadl_teardown));
602 wait_event_timeout(info->waitevent,
603 info->wait_condition, msecs_to_jiffies(1000));
604 BUG_ON(info->wait_condition == 0);
606 /* Received a torndown response */
607 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
608 list_del(&info->msglistentry);
609 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
614 EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
617 * vmbus_close - Close the specified channel
619 void vmbus_close(struct vmbus_channel *channel)
621 struct vmbus_channel_close_channel *msg;
622 struct vmbus_channel_msginfo *info;
626 /* Stop callback and cancel the timer asap */
627 channel->onchannel_callback = NULL;
628 del_timer_sync(&channel->poll_timer);
630 /* Send a closing message */
631 info = kmalloc(sizeof(*info) +
632 sizeof(struct vmbus_channel_close_channel), GFP_KERNEL);
633 /* FIXME: can't do anything other than return here because the
634 * function is void */
639 msg = (struct vmbus_channel_close_channel *)info->msg;
640 msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
641 msg->child_relid = channel->offermsg.child_relid;
643 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel));
646 /* Tear down the gpadl for the channel's ring buffer */
647 if (channel->ringbuffer_gpadlhandle)
648 vmbus_teardown_gpadl(channel,
649 channel->ringbuffer_gpadlhandle);
651 /* TODO: Send a msg to release the childRelId */
653 /* Cleanup the ring buffers for this channel */
654 hv_ringbuffer_cleanup(&channel->outbound);
655 hv_ringbuffer_cleanup(&channel->inbound);
657 free_pages((unsigned long)channel->ringbuffer_pages,
658 get_order(channel->ringbuffer_pagecount * PAGE_SIZE));
663 * If we are closing the channel during an error path in
664 * opening the channel, don't free the channel since the
665 * caller will free the channel
668 if (channel->state == CHANNEL_OPEN_STATE) {
669 spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
670 list_del(&channel->listentry);
671 spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
673 free_channel(channel);
676 EXPORT_SYMBOL_GPL(vmbus_close);
679 * vmbus_sendpacket() - Send the specified buffer on the given channel
680 * @channel: Pointer to vmbus_channel structure.
681 * @buffer: Pointer to the buffer you want to receive the data into.
682 * @bufferlen: Maximum size of what the the buffer will hold
683 * @requestid: Identifier of the request
684 * @type: Type of packet that is being send e.g. negotiate, time
687 * Sends data in @buffer directly to hyper-v via the vmbus
688 * This will send the data unparsed to hyper-v.
690 * Mainly used by Hyper-V drivers.
692 int vmbus_sendpacket(struct vmbus_channel *channel, const void *buffer,
693 u32 bufferlen, u64 requestid,
694 enum vmbus_packet_type type, u32 flags)
696 struct vmpacket_descriptor desc;
697 u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
698 u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
699 struct scatterlist bufferlist[3];
700 u64 aligned_data = 0;
703 dump_vmbus_channel(channel);
705 /* ASSERT((packetLenAligned - packetLen) < sizeof(u64)); */
707 /* Setup the descriptor */
708 desc.type = type; /* VmbusPacketTypeDataInBand; */
709 desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
710 /* in 8-bytes granularity */
711 desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
712 desc.len8 = (u16)(packetlen_aligned >> 3);
713 desc.trans_id = requestid;
715 sg_init_table(bufferlist, 3);
716 sg_set_buf(&bufferlist[0], &desc, sizeof(struct vmpacket_descriptor));
717 sg_set_buf(&bufferlist[1], buffer, bufferlen);
718 sg_set_buf(&bufferlist[2], &aligned_data,
719 packetlen_aligned - packetlen);
721 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3);
723 /* TODO: We should determine if this is optional */
724 if (ret == 0 && !hv_get_ringbuffer_interrupt_mask(&channel->outbound))
725 vmbus_setevent(channel);
729 EXPORT_SYMBOL(vmbus_sendpacket);
732 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
733 * packets using a GPADL Direct packet type.
735 int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
736 struct hv_page_buffer pagebuffers[],
737 u32 pagecount, void *buffer, u32 bufferlen,
742 struct vmbus_channel_packet_page_buffer desc;
745 u32 packetlen_aligned;
746 struct scatterlist bufferlist[3];
747 u64 aligned_data = 0;
749 if (pagecount > MAX_PAGE_BUFFER_COUNT)
752 dump_vmbus_channel(channel);
755 * Adjust the size down since vmbus_channel_packet_page_buffer is the
756 * largest size we support
758 descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
759 ((MAX_PAGE_BUFFER_COUNT - pagecount) *
760 sizeof(struct hv_page_buffer));
761 packetlen = descsize + bufferlen;
762 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
764 /* ASSERT((packetLenAligned - packetLen) < sizeof(u64)); */
766 /* Setup the descriptor */
767 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
768 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
769 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
770 desc.length8 = (u16)(packetlen_aligned >> 3);
771 desc.transactionid = requestid;
772 desc.rangecount = pagecount;
774 for (i = 0; i < pagecount; i++) {
775 desc.range[i].len = pagebuffers[i].len;
776 desc.range[i].offset = pagebuffers[i].offset;
777 desc.range[i].pfn = pagebuffers[i].pfn;
780 sg_init_table(bufferlist, 3);
781 sg_set_buf(&bufferlist[0], &desc, descsize);
782 sg_set_buf(&bufferlist[1], buffer, bufferlen);
783 sg_set_buf(&bufferlist[2], &aligned_data,
784 packetlen_aligned - packetlen);
786 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3);
788 /* TODO: We should determine if this is optional */
789 if (ret == 0 && !hv_get_ringbuffer_interrupt_mask(&channel->outbound))
790 vmbus_setevent(channel);
794 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
797 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
798 * using a GPADL Direct packet type.
800 int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
801 struct hv_multipage_buffer *multi_pagebuffer,
802 void *buffer, u32 bufferlen, u64 requestid)
805 struct vmbus_channel_packet_multipage_buffer desc;
808 u32 packetlen_aligned;
809 struct scatterlist bufferlist[3];
810 u64 aligned_data = 0;
811 u32 pfncount = NUM_PAGES_SPANNED(multi_pagebuffer->offset,
812 multi_pagebuffer->len);
814 dump_vmbus_channel(channel);
816 if ((pfncount < 0) || (pfncount > MAX_MULTIPAGE_BUFFER_COUNT))
820 * Adjust the size down since vmbus_channel_packet_multipage_buffer is
821 * the largest size we support
823 descsize = sizeof(struct vmbus_channel_packet_multipage_buffer) -
824 ((MAX_MULTIPAGE_BUFFER_COUNT - pfncount) *
826 packetlen = descsize + bufferlen;
827 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
829 /* ASSERT((packetLenAligned - packetLen) < sizeof(u64)); */
831 /* Setup the descriptor */
832 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
833 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
834 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
835 desc.length8 = (u16)(packetlen_aligned >> 3);
836 desc.transactionid = requestid;
839 desc.range.len = multi_pagebuffer->len;
840 desc.range.offset = multi_pagebuffer->offset;
842 memcpy(desc.range.pfn_array, multi_pagebuffer->pfn_array,
843 pfncount * sizeof(u64));
845 sg_init_table(bufferlist, 3);
846 sg_set_buf(&bufferlist[0], &desc, descsize);
847 sg_set_buf(&bufferlist[1], buffer, bufferlen);
848 sg_set_buf(&bufferlist[2], &aligned_data,
849 packetlen_aligned - packetlen);
851 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3);
853 /* TODO: We should determine if this is optional */
854 if (ret == 0 && !hv_get_ringbuffer_interrupt_mask(&channel->outbound))
855 vmbus_setevent(channel);
859 EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer);
862 * vmbus_recvpacket() - Retrieve the user packet on the specified channel
863 * @channel: Pointer to vmbus_channel structure.
864 * @buffer: Pointer to the buffer you want to receive the data into.
865 * @bufferlen: Maximum size of what the the buffer will hold
866 * @buffer_actual_len: The actual size of the data after it was received
867 * @requestid: Identifier of the request
869 * Receives directly from the hyper-v vmbus and puts the data it received
870 * into Buffer. This will receive the data unparsed from hyper-v.
872 * Mainly used by Hyper-V drivers.
874 int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
875 u32 bufferlen, u32 *buffer_actual_len, u64 *requestid)
877 struct vmpacket_descriptor desc;
883 *buffer_actual_len = 0;
886 spin_lock_irqsave(&channel->inbound_lock, flags);
888 ret = hv_ringbuffer_peek(&channel->inbound, &desc,
889 sizeof(struct vmpacket_descriptor));
891 spin_unlock_irqrestore(&channel->inbound_lock, flags);
895 /* VmbusChannelClearEvent(Channel); */
897 packetlen = desc.len8 << 3;
898 userlen = packetlen - (desc.offset8 << 3);
899 /* ASSERT(userLen > 0); */
901 *buffer_actual_len = userlen;
903 if (userlen > bufferlen) {
904 spin_unlock_irqrestore(&channel->inbound_lock, flags);
906 pr_err("Buffer too small - got %d needs %d\n",
911 *requestid = desc.trans_id;
913 /* Copy over the packet to the user buffer */
914 ret = hv_ringbuffer_read(&channel->inbound, buffer, userlen,
915 (desc.offset8 << 3));
917 spin_unlock_irqrestore(&channel->inbound_lock, flags);
921 EXPORT_SYMBOL(vmbus_recvpacket);
924 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
926 int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
927 u32 bufferlen, u32 *buffer_actual_len,
930 struct vmpacket_descriptor desc;
936 *buffer_actual_len = 0;
939 spin_lock_irqsave(&channel->inbound_lock, flags);
941 ret = hv_ringbuffer_peek(&channel->inbound, &desc,
942 sizeof(struct vmpacket_descriptor));
944 spin_unlock_irqrestore(&channel->inbound_lock, flags);
948 /* VmbusChannelClearEvent(Channel); */
950 packetlen = desc.len8 << 3;
951 userlen = packetlen - (desc.offset8 << 3);
953 *buffer_actual_len = packetlen;
955 if (packetlen > bufferlen) {
956 spin_unlock_irqrestore(&channel->inbound_lock, flags);
958 pr_err("Buffer too small - needed %d bytes but "
959 "got space for only %d bytes\n",
960 packetlen, bufferlen);
964 *requestid = desc.trans_id;
966 /* Copy over the entire packet to the user buffer */
967 ret = hv_ringbuffer_read(&channel->inbound, buffer, packetlen, 0);
969 spin_unlock_irqrestore(&channel->inbound_lock, flags);
972 EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);
975 * vmbus_onchannel_event - Channel event callback
977 void vmbus_onchannel_event(struct vmbus_channel *channel)
979 dump_vmbus_channel(channel);
980 /* ASSERT(Channel->OnChannelCallback); */
982 channel->onchannel_callback(channel->channel_callback_context);
984 mod_timer(&channel->poll_timer, jiffies + usecs_to_jiffies(100));
988 * vmbus_ontimer - Timer event callback
990 void vmbus_ontimer(unsigned long data)
992 struct vmbus_channel *channel = (struct vmbus_channel *)data;
994 if (channel->onchannel_callback)
995 channel->onchannel_callback(channel->channel_callback_context);
999 * dump_vmbus_channel- Dump vmbus channel info to the console
1001 static void dump_vmbus_channel(struct vmbus_channel *channel)
1003 DPRINT_DBG(VMBUS, "Channel (%d)", channel->offermsg.child_relid);
1004 dump_ring_info(&channel->outbound, "Outbound ");
1005 dump_ring_info(&channel->inbound, "Inbound ");