blob: 980ef7e174fbac056f68df931a6ed31d0dd8cebe [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
2 * Copyright (c) 2009, Microsoft Corporation.
3 *
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.
7 *
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.
12 *
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.
16 *
17 * Authors:
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
20 */
21#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
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
31#include "hyperv_vmbus.h"
32
33#define NUM_PAGES_SPANNED(addr, len) \
34((PAGE_ALIGN(addr + len) >> PAGE_SHIFT) - (addr >> PAGE_SHIFT))
35
36/* Internal routines */
37static 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);
42static void vmbus_setevent(struct vmbus_channel *channel);
43
44/*
45 * vmbus_setevent- Trigger an event notification on the specified
46 * channel.
47 */
48static void vmbus_setevent(struct vmbus_channel *channel)
49{
50 struct hv_monitor_page *monitorpage;
51
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));
57
58 monitorpage = vmbus_connection.monitor_pages;
59 monitorpage++; /* Get the child to parent monitor page */
60
61 sync_set_bit(channel->monitor_bit,
62 (unsigned long *)&monitorpage->trigger_group
63 [channel->monitor_grp].pending);
64
65 } else {
66 vmbus_set_event(channel->offermsg.child_relid);
67 }
68}
69
70/*
71 * vmbus_get_debug_info -Retrieve various channel debug info
72 */
73void vmbus_get_debug_info(struct vmbus_channel *channel,
74 struct vmbus_channel_debug_info *debuginfo)
75{
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
80 debuginfo->relid = channel->offermsg.child_relid;
81 debuginfo->state = channel->state;
82 memcpy(&debuginfo->interfacetype,
83 &channel->offermsg.offer.if_type, sizeof(uuid_le));
84 memcpy(&debuginfo->interface_instance,
85 &channel->offermsg.offer.if_instance,
86 sizeof(uuid_le));
87
88 monitorpage = (struct hv_monitor_page *)vmbus_connection.monitor_pages;
89
90 debuginfo->monitorid = channel->offermsg.monitorid;
91
92 debuginfo->servermonitor_pending =
93 monitorpage->trigger_group[monitor_group].pending;
94 debuginfo->servermonitor_latency =
95 monitorpage->latency[monitor_group][monitor_offset];
96 debuginfo->servermonitor_connectionid =
97 monitorpage->parameter[monitor_group]
98 [monitor_offset].connectionid.u.id;
99
100 monitorpage++;
101
102 debuginfo->clientmonitor_pending =
103 monitorpage->trigger_group[monitor_group].pending;
104 debuginfo->clientmonitor_latency =
105 monitorpage->latency[monitor_group][monitor_offset];
106 debuginfo->clientmonitor_connectionid =
107 monitorpage->parameter[monitor_group]
108 [monitor_offset].connectionid.u.id;
109
110 hv_ringbuffer_get_debuginfo(&channel->inbound, &debuginfo->inbound);
111 hv_ringbuffer_get_debuginfo(&channel->outbound, &debuginfo->outbound);
112}
113
114/*
115 * vmbus_open - Open the specified channel.
116 */
117int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size,
118 u32 recv_ringbuffer_size, void *userdata, u32 userdatalen,
119 void (*onchannelcallback)(void *context), void *context)
120{
121 struct vmbus_channel_open_channel *open_msg;
122 struct vmbus_channel_msginfo *open_info = NULL;
123 void *in, *out;
124 unsigned long flags;
125 int ret, t, err = 0;
126
127 newchannel->onchannel_callback = onchannelcallback;
128 newchannel->channel_callback_context = context;
129
130 /* Allocate the ring buffer */
131 out = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO,
132 get_order(send_ringbuffer_size + recv_ringbuffer_size));
133
134 if (!out)
135 return -ENOMEM;
136
137
138 in = (void *)((unsigned long)out + send_ringbuffer_size);
139
140 newchannel->ringbuffer_pages = out;
141 newchannel->ringbuffer_pagecount = (send_ringbuffer_size +
142 recv_ringbuffer_size) >> PAGE_SHIFT;
143
144 ret = hv_ringbuffer_init(
145 &newchannel->outbound, out, send_ringbuffer_size);
146
147 if (ret != 0) {
148 err = ret;
149 goto error0;
150 }
151
152 ret = hv_ringbuffer_init(
153 &newchannel->inbound, in, recv_ringbuffer_size);
154 if (ret != 0) {
155 err = ret;
156 goto error0;
157 }
158
159
160 /* Establish the gpadl for the ring buffer */
161 newchannel->ringbuffer_gpadlhandle = 0;
162
163 ret = vmbus_establish_gpadl(newchannel,
164 newchannel->outbound.ring_buffer,
165 send_ringbuffer_size +
166 recv_ringbuffer_size,
167 &newchannel->ringbuffer_gpadlhandle);
168
169 if (ret != 0) {
170 err = ret;
171 goto error0;
172 }
173
174 /* Create and init the channel open message */
175 open_info = kmalloc(sizeof(*open_info) +
176 sizeof(struct vmbus_channel_open_channel),
177 GFP_KERNEL);
178 if (!open_info) {
179 err = -ENOMEM;
180 goto error_gpadl;
181 }
182
183 init_completion(&open_info->waitevent);
184
185 open_msg = (struct vmbus_channel_open_channel *)open_info->msg;
186 open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL;
187 open_msg->openid = newchannel->offermsg.child_relid;
188 open_msg->child_relid = newchannel->offermsg.child_relid;
189 open_msg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle;
190 open_msg->downstream_ringbuffer_pageoffset = send_ringbuffer_size >>
191 PAGE_SHIFT;
192 open_msg->server_contextarea_gpadlhandle = 0;
193
194 if (userdatalen > MAX_USER_DEFINED_BYTES) {
195 err = -EINVAL;
196 goto error_gpadl;
197 }
198
199 if (userdatalen)
200 memcpy(open_msg->userdata, userdata, userdatalen);
201
202 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
203 list_add_tail(&open_info->msglistentry,
204 &vmbus_connection.chn_msg_list);
205 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
206
207 ret = vmbus_post_msg(open_msg,
208 sizeof(struct vmbus_channel_open_channel));
209
210 if (ret != 0) {
211 err = ret;
212 goto error1;
213 }
214
215 t = wait_for_completion_timeout(&open_info->waitevent, 5*HZ);
216 if (t == 0) {
217 err = -ETIMEDOUT;
218 goto error1;
219 }
220
221
222 if (open_info->response.open_result.status)
223 err = open_info->response.open_result.status;
224
225 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
226 list_del(&open_info->msglistentry);
227 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
228
229 kfree(open_info);
230 return err;
231
232error1:
233 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
234 list_del(&open_info->msglistentry);
235 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
236
237error_gpadl:
238 vmbus_teardown_gpadl(newchannel, newchannel->ringbuffer_gpadlhandle);
239
240error0:
241 free_pages((unsigned long)out,
242 get_order(send_ringbuffer_size + recv_ringbuffer_size));
243 kfree(open_info);
244 return err;
245}
246EXPORT_SYMBOL_GPL(vmbus_open);
247
248/*
249 * create_gpadl_header - Creates a gpadl for the specified buffer
250 */
251static int create_gpadl_header(void *kbuffer, u32 size,
252 struct vmbus_channel_msginfo **msginfo,
253 u32 *messagecount)
254{
255 int i;
256 int pagecount;
257 unsigned long long pfn;
258 struct vmbus_channel_gpadl_header *gpadl_header;
259 struct vmbus_channel_gpadl_body *gpadl_body;
260 struct vmbus_channel_msginfo *msgheader;
261 struct vmbus_channel_msginfo *msgbody = NULL;
262 u32 msgsize;
263
264 int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
265
266 pagecount = size >> PAGE_SHIFT;
267 pfn = virt_to_phys(kbuffer) >> PAGE_SHIFT;
268
269 /* do we need a gpadl body msg */
270 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
271 sizeof(struct vmbus_channel_gpadl_header) -
272 sizeof(struct gpa_range);
273 pfncount = pfnsize / sizeof(u64);
274
275 if (pagecount > pfncount) {
276 /* we need a gpadl body */
277 /* fill in the header */
278 msgsize = sizeof(struct vmbus_channel_msginfo) +
279 sizeof(struct vmbus_channel_gpadl_header) +
280 sizeof(struct gpa_range) + pfncount * sizeof(u64);
281 msgheader = kzalloc(msgsize, GFP_KERNEL);
282 if (!msgheader)
283 goto nomem;
284
285 INIT_LIST_HEAD(&msgheader->submsglist);
286 msgheader->msgsize = msgsize;
287
288 gpadl_header = (struct vmbus_channel_gpadl_header *)
289 msgheader->msg;
290 gpadl_header->rangecount = 1;
291 gpadl_header->range_buflen = sizeof(struct gpa_range) +
292 pagecount * sizeof(u64);
293 gpadl_header->range[0].byte_offset = 0;
294 gpadl_header->range[0].byte_count = size;
295 for (i = 0; i < pfncount; i++)
296 gpadl_header->range[0].pfn_array[i] = pfn+i;
297 *msginfo = msgheader;
298 *messagecount = 1;
299
300 pfnsum = pfncount;
301 pfnleft = pagecount - pfncount;
302
303 /* how many pfns can we fit */
304 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
305 sizeof(struct vmbus_channel_gpadl_body);
306 pfncount = pfnsize / sizeof(u64);
307
308 /* fill in the body */
309 while (pfnleft) {
310 if (pfnleft > pfncount)
311 pfncurr = pfncount;
312 else
313 pfncurr = pfnleft;
314
315 msgsize = sizeof(struct vmbus_channel_msginfo) +
316 sizeof(struct vmbus_channel_gpadl_body) +
317 pfncurr * sizeof(u64);
318 msgbody = kzalloc(msgsize, GFP_KERNEL);
319
320 if (!msgbody) {
321 struct vmbus_channel_msginfo *pos = NULL;
322 struct vmbus_channel_msginfo *tmp = NULL;
323 /*
324 * Free up all the allocated messages.
325 */
326 list_for_each_entry_safe(pos, tmp,
327 &msgheader->submsglist,
328 msglistentry) {
329
330 list_del(&pos->msglistentry);
331 kfree(pos);
332 }
333
334 goto nomem;
335 }
336
337 msgbody->msgsize = msgsize;
338 (*messagecount)++;
339 gpadl_body =
340 (struct vmbus_channel_gpadl_body *)msgbody->msg;
341
342 /*
343 * Gpadl is u32 and we are using a pointer which could
344 * be 64-bit
345 * This is governed by the guest/host protocol and
346 * so the hypervisor gurantees that this is ok.
347 */
348 for (i = 0; i < pfncurr; i++)
349 gpadl_body->pfn[i] = pfn + pfnsum + i;
350
351 /* add to msg header */
352 list_add_tail(&msgbody->msglistentry,
353 &msgheader->submsglist);
354 pfnsum += pfncurr;
355 pfnleft -= pfncurr;
356 }
357 } else {
358 /* everything fits in a header */
359 msgsize = sizeof(struct vmbus_channel_msginfo) +
360 sizeof(struct vmbus_channel_gpadl_header) +
361 sizeof(struct gpa_range) + pagecount * sizeof(u64);
362 msgheader = kzalloc(msgsize, GFP_KERNEL);
363 if (msgheader == NULL)
364 goto nomem;
365 msgheader->msgsize = msgsize;
366
367 gpadl_header = (struct vmbus_channel_gpadl_header *)
368 msgheader->msg;
369 gpadl_header->rangecount = 1;
370 gpadl_header->range_buflen = sizeof(struct gpa_range) +
371 pagecount * sizeof(u64);
372 gpadl_header->range[0].byte_offset = 0;
373 gpadl_header->range[0].byte_count = size;
374 for (i = 0; i < pagecount; i++)
375 gpadl_header->range[0].pfn_array[i] = pfn+i;
376
377 *msginfo = msgheader;
378 *messagecount = 1;
379 }
380
381 return 0;
382nomem:
383 kfree(msgheader);
384 kfree(msgbody);
385 return -ENOMEM;
386}
387
388/*
389 * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
390 *
391 * @channel: a channel
392 * @kbuffer: from kmalloc
393 * @size: page-size multiple
394 * @gpadl_handle: some funky thing
395 */
396int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
397 u32 size, u32 *gpadl_handle)
398{
399 struct vmbus_channel_gpadl_header *gpadlmsg;
400 struct vmbus_channel_gpadl_body *gpadl_body;
401 struct vmbus_channel_msginfo *msginfo = NULL;
402 struct vmbus_channel_msginfo *submsginfo;
403 u32 msgcount;
404 struct list_head *curr;
405 u32 next_gpadl_handle;
406 unsigned long flags;
407 int ret = 0;
408
409 next_gpadl_handle = atomic_read(&vmbus_connection.next_gpadl_handle);
410 atomic_inc(&vmbus_connection.next_gpadl_handle);
411
412 ret = create_gpadl_header(kbuffer, size, &msginfo, &msgcount);
413 if (ret)
414 return ret;
415
416 init_completion(&msginfo->waitevent);
417
418 gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
419 gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
420 gpadlmsg->child_relid = channel->offermsg.child_relid;
421 gpadlmsg->gpadl = next_gpadl_handle;
422
423
424 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
425 list_add_tail(&msginfo->msglistentry,
426 &vmbus_connection.chn_msg_list);
427
428 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
429
430 ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
431 sizeof(*msginfo));
432 if (ret != 0)
433 goto cleanup;
434
435 if (msgcount > 1) {
436 list_for_each(curr, &msginfo->submsglist) {
437
438 submsginfo = (struct vmbus_channel_msginfo *)curr;
439 gpadl_body =
440 (struct vmbus_channel_gpadl_body *)submsginfo->msg;
441
442 gpadl_body->header.msgtype =
443 CHANNELMSG_GPADL_BODY;
444 gpadl_body->gpadl = next_gpadl_handle;
445
446 ret = vmbus_post_msg(gpadl_body,
447 submsginfo->msgsize -
448 sizeof(*submsginfo));
449 if (ret != 0)
450 goto cleanup;
451
452 }
453 }
454 wait_for_completion(&msginfo->waitevent);
455
456 /* At this point, we received the gpadl created msg */
457 *gpadl_handle = gpadlmsg->gpadl;
458
459cleanup:
460 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
461 list_del(&msginfo->msglistentry);
462 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
463
464 kfree(msginfo);
465 return ret;
466}
467EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
468
469/*
470 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
471 */
472int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
473{
474 struct vmbus_channel_gpadl_teardown *msg;
475 struct vmbus_channel_msginfo *info;
476 unsigned long flags;
477 int ret;
478
479 info = kmalloc(sizeof(*info) +
480 sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
481 if (!info)
482 return -ENOMEM;
483
484 init_completion(&info->waitevent);
485
486 msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
487
488 msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
489 msg->child_relid = channel->offermsg.child_relid;
490 msg->gpadl = gpadl_handle;
491
492 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
493 list_add_tail(&info->msglistentry,
494 &vmbus_connection.chn_msg_list);
495 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
496 ret = vmbus_post_msg(msg,
497 sizeof(struct vmbus_channel_gpadl_teardown));
498
499 if (ret)
500 goto post_msg_err;
501
502 wait_for_completion(&info->waitevent);
503
504post_msg_err:
505 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
506 list_del(&info->msglistentry);
507 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
508
509 kfree(info);
510 return ret;
511}
512EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
513
514/*
515 * vmbus_close - Close the specified channel
516 */
517void vmbus_close(struct vmbus_channel *channel)
518{
519 struct vmbus_channel_close_channel *msg;
520 int ret;
521 unsigned long flags;
522
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);
527
528 /* Send a closing message */
529
530 msg = &channel->close_msg.msg;
531
532 msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
533 msg->child_relid = channel->offermsg.child_relid;
534
535 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel));
536
537 if (ret) {
538 pr_err("Close failed: close post msg return is %d\n", ret);
539 /*
540 * If we failed to post the close msg,
541 * it is perhaps better to leak memory.
542 */
543 return;
544 }
545
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);
552 /*
553 * If we failed to teardown gpadl,
554 * it is perhaps better to leak memory.
555 */
556 return;
557 }
558 }
559
560 /* Cleanup the ring buffers for this channel */
561 hv_ringbuffer_cleanup(&channel->outbound);
562 hv_ringbuffer_cleanup(&channel->inbound);
563
564 free_pages((unsigned long)channel->ringbuffer_pages,
565 get_order(channel->ringbuffer_pagecount * PAGE_SIZE));
566}
567EXPORT_SYMBOL_GPL(vmbus_close);
568
569/**
570 * vmbus_sendpacket() - Send the specified buffer on the given channel
571 * @channel: Pointer to vmbus_channel structure.
572 * @buffer: Pointer to the buffer you want to receive the data into.
573 * @bufferlen: Maximum size of what the the buffer will hold
574 * @requestid: Identifier of the request
575 * @type: Type of packet that is being send e.g. negotiate, time
576 * packet etc.
577 *
578 * Sends data in @buffer directly to hyper-v via the vmbus
579 * This will send the data unparsed to hyper-v.
580 *
581 * Mainly used by Hyper-V drivers.
582 */
583int vmbus_sendpacket(struct vmbus_channel *channel, const void *buffer,
584 u32 bufferlen, u64 requestid,
585 enum vmbus_packet_type type, u32 flags)
586{
587 struct vmpacket_descriptor desc;
588 u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
589 u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
590 struct scatterlist bufferlist[3];
591 u64 aligned_data = 0;
592 int ret;
593
594
595 /* Setup the descriptor */
596 desc.type = type; /* VmbusPacketTypeDataInBand; */
597 desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
598 /* in 8-bytes granularity */
599 desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
600 desc.len8 = (u16)(packetlen_aligned >> 3);
601 desc.trans_id = requestid;
602
603 sg_init_table(bufferlist, 3);
604 sg_set_buf(&bufferlist[0], &desc, sizeof(struct vmpacket_descriptor));
605 sg_set_buf(&bufferlist[1], buffer, bufferlen);
606 sg_set_buf(&bufferlist[2], &aligned_data,
607 packetlen_aligned - packetlen);
608
609 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3);
610
611 if (ret == 0 && !hv_get_ringbuffer_interrupt_mask(&channel->outbound))
612 vmbus_setevent(channel);
613
614 return ret;
615}
616EXPORT_SYMBOL(vmbus_sendpacket);
617
618/*
619 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
620 * packets using a GPADL Direct packet type.
621 */
622int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
623 struct hv_page_buffer pagebuffers[],
624 u32 pagecount, void *buffer, u32 bufferlen,
625 u64 requestid)
626{
627 int ret;
628 int i;
629 struct vmbus_channel_packet_page_buffer desc;
630 u32 descsize;
631 u32 packetlen;
632 u32 packetlen_aligned;
633 struct scatterlist bufferlist[3];
634 u64 aligned_data = 0;
635
636 if (pagecount > MAX_PAGE_BUFFER_COUNT)
637 return -EINVAL;
638
639
640 /*
641 * Adjust the size down since vmbus_channel_packet_page_buffer is the
642 * largest size we support
643 */
644 descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
645 ((MAX_PAGE_BUFFER_COUNT - pagecount) *
646 sizeof(struct hv_page_buffer));
647 packetlen = descsize + bufferlen;
648 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
649
650 /* Setup the descriptor */
651 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
652 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
653 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
654 desc.length8 = (u16)(packetlen_aligned >> 3);
655 desc.transactionid = requestid;
656 desc.rangecount = pagecount;
657
658 for (i = 0; i < pagecount; i++) {
659 desc.range[i].len = pagebuffers[i].len;
660 desc.range[i].offset = pagebuffers[i].offset;
661 desc.range[i].pfn = pagebuffers[i].pfn;
662 }
663
664 sg_init_table(bufferlist, 3);
665 sg_set_buf(&bufferlist[0], &desc, descsize);
666 sg_set_buf(&bufferlist[1], buffer, bufferlen);
667 sg_set_buf(&bufferlist[2], &aligned_data,
668 packetlen_aligned - packetlen);
669
670 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3);
671
672 if (ret == 0 && !hv_get_ringbuffer_interrupt_mask(&channel->outbound))
673 vmbus_setevent(channel);
674
675 return ret;
676}
677EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
678
679/*
680 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
681 * using a GPADL Direct packet type.
682 */
683int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
684 struct hv_multipage_buffer *multi_pagebuffer,
685 void *buffer, u32 bufferlen, u64 requestid)
686{
687 int ret;
688 struct vmbus_channel_packet_multipage_buffer desc;
689 u32 descsize;
690 u32 packetlen;
691 u32 packetlen_aligned;
692 struct scatterlist bufferlist[3];
693 u64 aligned_data = 0;
694 u32 pfncount = NUM_PAGES_SPANNED(multi_pagebuffer->offset,
695 multi_pagebuffer->len);
696
697
698 if ((pfncount < 0) || (pfncount > MAX_MULTIPAGE_BUFFER_COUNT))
699 return -EINVAL;
700
701 /*
702 * Adjust the size down since vmbus_channel_packet_multipage_buffer is
703 * the largest size we support
704 */
705 descsize = sizeof(struct vmbus_channel_packet_multipage_buffer) -
706 ((MAX_MULTIPAGE_BUFFER_COUNT - pfncount) *
707 sizeof(u64));
708 packetlen = descsize + bufferlen;
709 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
710
711
712 /* Setup the descriptor */
713 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
714 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
715 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
716 desc.length8 = (u16)(packetlen_aligned >> 3);
717 desc.transactionid = requestid;
718 desc.rangecount = 1;
719
720 desc.range.len = multi_pagebuffer->len;
721 desc.range.offset = multi_pagebuffer->offset;
722
723 memcpy(desc.range.pfn_array, multi_pagebuffer->pfn_array,
724 pfncount * sizeof(u64));
725
726 sg_init_table(bufferlist, 3);
727 sg_set_buf(&bufferlist[0], &desc, descsize);
728 sg_set_buf(&bufferlist[1], buffer, bufferlen);
729 sg_set_buf(&bufferlist[2], &aligned_data,
730 packetlen_aligned - packetlen);
731
732 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3);
733
734 if (ret == 0 && !hv_get_ringbuffer_interrupt_mask(&channel->outbound))
735 vmbus_setevent(channel);
736
737 return ret;
738}
739EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer);
740
741/**
742 * vmbus_recvpacket() - Retrieve the user packet on the specified channel
743 * @channel: Pointer to vmbus_channel structure.
744 * @buffer: Pointer to the buffer you want to receive the data into.
745 * @bufferlen: Maximum size of what the the buffer will hold
746 * @buffer_actual_len: The actual size of the data after it was received
747 * @requestid: Identifier of the request
748 *
749 * Receives directly from the hyper-v vmbus and puts the data it received
750 * into Buffer. This will receive the data unparsed from hyper-v.
751 *
752 * Mainly used by Hyper-V drivers.
753 */
754int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
755 u32 bufferlen, u32 *buffer_actual_len, u64 *requestid)
756{
757 struct vmpacket_descriptor desc;
758 u32 packetlen;
759 u32 userlen;
760 int ret;
761
762 *buffer_actual_len = 0;
763 *requestid = 0;
764
765
766 ret = hv_ringbuffer_peek(&channel->inbound, &desc,
767 sizeof(struct vmpacket_descriptor));
768 if (ret != 0)
769 return 0;
770
771 packetlen = desc.len8 << 3;
772 userlen = packetlen - (desc.offset8 << 3);
773
774 *buffer_actual_len = userlen;
775
776 if (userlen > bufferlen) {
777
778 pr_err("Buffer too small - got %d needs %d\n",
779 bufferlen, userlen);
780 return -ETOOSMALL;
781 }
782
783 *requestid = desc.trans_id;
784
785 /* Copy over the packet to the user buffer */
786 ret = hv_ringbuffer_read(&channel->inbound, buffer, userlen,
787 (desc.offset8 << 3));
788
789
790 return 0;
791}
792EXPORT_SYMBOL(vmbus_recvpacket);
793
794/*
795 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
796 */
797int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
798 u32 bufferlen, u32 *buffer_actual_len,
799 u64 *requestid)
800{
801 struct vmpacket_descriptor desc;
802 u32 packetlen;
803 u32 userlen;
804 int ret;
805
806 *buffer_actual_len = 0;
807 *requestid = 0;
808
809
810 ret = hv_ringbuffer_peek(&channel->inbound, &desc,
811 sizeof(struct vmpacket_descriptor));
812 if (ret != 0)
813 return 0;
814
815
816 packetlen = desc.len8 << 3;
817 userlen = packetlen - (desc.offset8 << 3);
818
819 *buffer_actual_len = packetlen;
820
821 if (packetlen > bufferlen) {
822 pr_err("Buffer too small - needed %d bytes but "
823 "got space for only %d bytes\n",
824 packetlen, bufferlen);
825 return -ENOBUFS;
826 }
827
828 *requestid = desc.trans_id;
829
830 /* Copy over the entire packet to the user buffer */
831 ret = hv_ringbuffer_read(&channel->inbound, buffer, packetlen, 0);
832
833 return 0;
834}
835EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);