Staging: hv: vmbus: Get rid of unnecessary comments in channel.c
[deliverable/linux.git] / drivers / staging / hv / channel.c
... / ...
CommitLineData
1/*
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
30#include "hyperv.h"
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 *openInfo = 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 errorout;
150 }
151
152 ret = hv_ringbuffer_init(
153 &newchannel->inbound, in, recv_ringbuffer_size);
154 if (ret != 0) {
155 err = ret;
156 goto errorout;
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 errorout;
172 }
173
174 /* Create and init the channel open message */
175 openInfo = kmalloc(sizeof(*openInfo) +
176 sizeof(struct vmbus_channel_open_channel),
177 GFP_KERNEL);
178 if (!openInfo) {
179 err = -ENOMEM;
180 goto errorout;
181 }
182
183 init_completion(&openInfo->waitevent);
184
185 open_msg = (struct vmbus_channel_open_channel *)openInfo->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 errorout;
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(&openInfo->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 goto cleanup;
212
213 t = wait_for_completion_timeout(&openInfo->waitevent, 5*HZ);
214 if (t == 0) {
215 err = -ETIMEDOUT;
216 goto errorout;
217 }
218
219
220 if (openInfo->response.open_result.status)
221 err = openInfo->response.open_result.status;
222
223cleanup:
224 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
225 list_del(&openInfo->msglistentry);
226 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
227
228 kfree(openInfo);
229 return err;
230
231errorout:
232 hv_ringbuffer_cleanup(&newchannel->outbound);
233 hv_ringbuffer_cleanup(&newchannel->inbound);
234 free_pages((unsigned long)out,
235 get_order(send_ringbuffer_size + recv_ringbuffer_size));
236 kfree(openInfo);
237 return err;
238}
239EXPORT_SYMBOL_GPL(vmbus_open);
240
241/*
242 * create_gpadl_header - Creates a gpadl for the specified buffer
243 */
244static int create_gpadl_header(void *kbuffer, u32 size,
245 struct vmbus_channel_msginfo **msginfo,
246 u32 *messagecount)
247{
248 int i;
249 int pagecount;
250 unsigned long long pfn;
251 struct vmbus_channel_gpadl_header *gpadl_header;
252 struct vmbus_channel_gpadl_body *gpadl_body;
253 struct vmbus_channel_msginfo *msgheader;
254 struct vmbus_channel_msginfo *msgbody = NULL;
255 u32 msgsize;
256
257 int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
258
259 pagecount = size >> PAGE_SHIFT;
260 pfn = virt_to_phys(kbuffer) >> PAGE_SHIFT;
261
262 /* do we need a gpadl body msg */
263 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
264 sizeof(struct vmbus_channel_gpadl_header) -
265 sizeof(struct gpa_range);
266 pfncount = pfnsize / sizeof(u64);
267
268 if (pagecount > pfncount) {
269 /* we need a gpadl body */
270 /* fill in the header */
271 msgsize = sizeof(struct vmbus_channel_msginfo) +
272 sizeof(struct vmbus_channel_gpadl_header) +
273 sizeof(struct gpa_range) + pfncount * sizeof(u64);
274 msgheader = kzalloc(msgsize, GFP_KERNEL);
275 if (!msgheader)
276 goto nomem;
277
278 INIT_LIST_HEAD(&msgheader->submsglist);
279 msgheader->msgsize = msgsize;
280
281 gpadl_header = (struct vmbus_channel_gpadl_header *)
282 msgheader->msg;
283 gpadl_header->rangecount = 1;
284 gpadl_header->range_buflen = sizeof(struct gpa_range) +
285 pagecount * sizeof(u64);
286 gpadl_header->range[0].byte_offset = 0;
287 gpadl_header->range[0].byte_count = size;
288 for (i = 0; i < pfncount; i++)
289 gpadl_header->range[0].pfn_array[i] = pfn+i;
290 *msginfo = msgheader;
291 *messagecount = 1;
292
293 pfnsum = pfncount;
294 pfnleft = pagecount - pfncount;
295
296 /* how many pfns can we fit */
297 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
298 sizeof(struct vmbus_channel_gpadl_body);
299 pfncount = pfnsize / sizeof(u64);
300
301 /* fill in the body */
302 while (pfnleft) {
303 if (pfnleft > pfncount)
304 pfncurr = pfncount;
305 else
306 pfncurr = pfnleft;
307
308 msgsize = sizeof(struct vmbus_channel_msginfo) +
309 sizeof(struct vmbus_channel_gpadl_body) +
310 pfncurr * sizeof(u64);
311 msgbody = kzalloc(msgsize, GFP_KERNEL);
312
313 if (!msgbody) {
314 struct vmbus_channel_msginfo *pos = NULL;
315 struct vmbus_channel_msginfo *tmp = NULL;
316 /*
317 * Free up all the allocated messages.
318 */
319 list_for_each_entry_safe(pos, tmp,
320 &msgheader->submsglist,
321 msglistentry) {
322
323 list_del(&pos->msglistentry);
324 kfree(pos);
325 }
326
327 goto nomem;
328 }
329
330 msgbody->msgsize = msgsize;
331 (*messagecount)++;
332 gpadl_body =
333 (struct vmbus_channel_gpadl_body *)msgbody->msg;
334
335 /*
336 * Gpadl is u32 and we are using a pointer which could
337 * be 64-bit
338 * This is governed by the guest/host protocol and
339 * so the hypervisor gurantees that this is ok.
340 */
341 for (i = 0; i < pfncurr; i++)
342 gpadl_body->pfn[i] = pfn + pfnsum + i;
343
344 /* add to msg header */
345 list_add_tail(&msgbody->msglistentry,
346 &msgheader->submsglist);
347 pfnsum += pfncurr;
348 pfnleft -= pfncurr;
349 }
350 } else {
351 /* everything fits in a header */
352 msgsize = sizeof(struct vmbus_channel_msginfo) +
353 sizeof(struct vmbus_channel_gpadl_header) +
354 sizeof(struct gpa_range) + pagecount * sizeof(u64);
355 msgheader = kzalloc(msgsize, GFP_KERNEL);
356 if (msgheader == NULL)
357 goto nomem;
358 msgheader->msgsize = msgsize;
359
360 gpadl_header = (struct vmbus_channel_gpadl_header *)
361 msgheader->msg;
362 gpadl_header->rangecount = 1;
363 gpadl_header->range_buflen = sizeof(struct gpa_range) +
364 pagecount * sizeof(u64);
365 gpadl_header->range[0].byte_offset = 0;
366 gpadl_header->range[0].byte_count = size;
367 for (i = 0; i < pagecount; i++)
368 gpadl_header->range[0].pfn_array[i] = pfn+i;
369
370 *msginfo = msgheader;
371 *messagecount = 1;
372 }
373
374 return 0;
375nomem:
376 kfree(msgheader);
377 kfree(msgbody);
378 return -ENOMEM;
379}
380
381/*
382 * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
383 *
384 * @channel: a channel
385 * @kbuffer: from kmalloc
386 * @size: page-size multiple
387 * @gpadl_handle: some funky thing
388 */
389int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
390 u32 size, u32 *gpadl_handle)
391{
392 struct vmbus_channel_gpadl_header *gpadlmsg;
393 struct vmbus_channel_gpadl_body *gpadl_body;
394 struct vmbus_channel_msginfo *msginfo = NULL;
395 struct vmbus_channel_msginfo *submsginfo;
396 u32 msgcount;
397 struct list_head *curr;
398 u32 next_gpadl_handle;
399 unsigned long flags;
400 int ret = 0;
401 int t;
402
403 next_gpadl_handle = atomic_read(&vmbus_connection.next_gpadl_handle);
404 atomic_inc(&vmbus_connection.next_gpadl_handle);
405
406 ret = create_gpadl_header(kbuffer, size, &msginfo, &msgcount);
407 if (ret)
408 return ret;
409
410 init_completion(&msginfo->waitevent);
411
412 gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
413 gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
414 gpadlmsg->child_relid = channel->offermsg.child_relid;
415 gpadlmsg->gpadl = next_gpadl_handle;
416
417
418 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
419 list_add_tail(&msginfo->msglistentry,
420 &vmbus_connection.chn_msg_list);
421
422 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
423
424 ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
425 sizeof(*msginfo));
426 if (ret != 0)
427 goto cleanup;
428
429 if (msgcount > 1) {
430 list_for_each(curr, &msginfo->submsglist) {
431
432 submsginfo = (struct vmbus_channel_msginfo *)curr;
433 gpadl_body =
434 (struct vmbus_channel_gpadl_body *)submsginfo->msg;
435
436 gpadl_body->header.msgtype =
437 CHANNELMSG_GPADL_BODY;
438 gpadl_body->gpadl = next_gpadl_handle;
439
440 ret = vmbus_post_msg(gpadl_body,
441 submsginfo->msgsize -
442 sizeof(*submsginfo));
443 if (ret != 0)
444 goto cleanup;
445
446 }
447 }
448 t = wait_for_completion_timeout(&msginfo->waitevent, 5*HZ);
449 BUG_ON(t == 0);
450
451
452 /* At this point, we received the gpadl created msg */
453 *gpadl_handle = gpadlmsg->gpadl;
454
455cleanup:
456 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
457 list_del(&msginfo->msglistentry);
458 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
459
460 kfree(msginfo);
461 return ret;
462}
463EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
464
465/*
466 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
467 */
468int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
469{
470 struct vmbus_channel_gpadl_teardown *msg;
471 struct vmbus_channel_msginfo *info;
472 unsigned long flags;
473 int ret, t;
474
475 info = kmalloc(sizeof(*info) +
476 sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
477 if (!info)
478 return -ENOMEM;
479
480 init_completion(&info->waitevent);
481
482 msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
483
484 msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
485 msg->child_relid = channel->offermsg.child_relid;
486 msg->gpadl = gpadl_handle;
487
488 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
489 list_add_tail(&info->msglistentry,
490 &vmbus_connection.chn_msg_list);
491 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
492 ret = vmbus_post_msg(msg,
493 sizeof(struct vmbus_channel_gpadl_teardown));
494
495 BUG_ON(ret != 0);
496 t = wait_for_completion_timeout(&info->waitevent, 5*HZ);
497 BUG_ON(t == 0);
498
499 /* Received a torndown response */
500 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
501 list_del(&info->msglistentry);
502 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
503
504 kfree(info);
505 return ret;
506}
507EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
508
509/*
510 * vmbus_close - Close the specified channel
511 */
512void vmbus_close(struct vmbus_channel *channel)
513{
514 struct vmbus_channel_close_channel *msg;
515 int ret;
516
517 /* Stop callback and cancel the timer asap */
518 channel->onchannel_callback = NULL;
519
520 /* Send a closing message */
521
522 msg = &channel->close_msg.msg;
523
524 msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
525 msg->child_relid = channel->offermsg.child_relid;
526
527 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel));
528
529 BUG_ON(ret != 0);
530 /* Tear down the gpadl for the channel's ring buffer */
531 if (channel->ringbuffer_gpadlhandle)
532 vmbus_teardown_gpadl(channel,
533 channel->ringbuffer_gpadlhandle);
534
535 /* Cleanup the ring buffers for this channel */
536 hv_ringbuffer_cleanup(&channel->outbound);
537 hv_ringbuffer_cleanup(&channel->inbound);
538
539 free_pages((unsigned long)channel->ringbuffer_pages,
540 get_order(channel->ringbuffer_pagecount * PAGE_SIZE));
541
542
543}
544EXPORT_SYMBOL_GPL(vmbus_close);
545
546/**
547 * vmbus_sendpacket() - Send the specified buffer on the given channel
548 * @channel: Pointer to vmbus_channel structure.
549 * @buffer: Pointer to the buffer you want to receive the data into.
550 * @bufferlen: Maximum size of what the the buffer will hold
551 * @requestid: Identifier of the request
552 * @type: Type of packet that is being send e.g. negotiate, time
553 * packet etc.
554 *
555 * Sends data in @buffer directly to hyper-v via the vmbus
556 * This will send the data unparsed to hyper-v.
557 *
558 * Mainly used by Hyper-V drivers.
559 */
560int vmbus_sendpacket(struct vmbus_channel *channel, const void *buffer,
561 u32 bufferlen, u64 requestid,
562 enum vmbus_packet_type type, u32 flags)
563{
564 struct vmpacket_descriptor desc;
565 u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
566 u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
567 struct scatterlist bufferlist[3];
568 u64 aligned_data = 0;
569 int ret;
570
571
572 /* Setup the descriptor */
573 desc.type = type; /* VmbusPacketTypeDataInBand; */
574 desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
575 /* in 8-bytes granularity */
576 desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
577 desc.len8 = (u16)(packetlen_aligned >> 3);
578 desc.trans_id = requestid;
579
580 sg_init_table(bufferlist, 3);
581 sg_set_buf(&bufferlist[0], &desc, sizeof(struct vmpacket_descriptor));
582 sg_set_buf(&bufferlist[1], buffer, bufferlen);
583 sg_set_buf(&bufferlist[2], &aligned_data,
584 packetlen_aligned - packetlen);
585
586 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3);
587
588 if (ret == 0 && !hv_get_ringbuffer_interrupt_mask(&channel->outbound))
589 vmbus_setevent(channel);
590
591 return ret;
592}
593EXPORT_SYMBOL(vmbus_sendpacket);
594
595/*
596 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
597 * packets using a GPADL Direct packet type.
598 */
599int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
600 struct hv_page_buffer pagebuffers[],
601 u32 pagecount, void *buffer, u32 bufferlen,
602 u64 requestid)
603{
604 int ret;
605 int i;
606 struct vmbus_channel_packet_page_buffer desc;
607 u32 descsize;
608 u32 packetlen;
609 u32 packetlen_aligned;
610 struct scatterlist bufferlist[3];
611 u64 aligned_data = 0;
612
613 if (pagecount > MAX_PAGE_BUFFER_COUNT)
614 return -EINVAL;
615
616
617 /*
618 * Adjust the size down since vmbus_channel_packet_page_buffer is the
619 * largest size we support
620 */
621 descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
622 ((MAX_PAGE_BUFFER_COUNT - pagecount) *
623 sizeof(struct hv_page_buffer));
624 packetlen = descsize + bufferlen;
625 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
626
627 /* Setup the descriptor */
628 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
629 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
630 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
631 desc.length8 = (u16)(packetlen_aligned >> 3);
632 desc.transactionid = requestid;
633 desc.rangecount = pagecount;
634
635 for (i = 0; i < pagecount; i++) {
636 desc.range[i].len = pagebuffers[i].len;
637 desc.range[i].offset = pagebuffers[i].offset;
638 desc.range[i].pfn = pagebuffers[i].pfn;
639 }
640
641 sg_init_table(bufferlist, 3);
642 sg_set_buf(&bufferlist[0], &desc, descsize);
643 sg_set_buf(&bufferlist[1], buffer, bufferlen);
644 sg_set_buf(&bufferlist[2], &aligned_data,
645 packetlen_aligned - packetlen);
646
647 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3);
648
649 if (ret == 0 && !hv_get_ringbuffer_interrupt_mask(&channel->outbound))
650 vmbus_setevent(channel);
651
652 return ret;
653}
654EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
655
656/*
657 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
658 * using a GPADL Direct packet type.
659 */
660int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
661 struct hv_multipage_buffer *multi_pagebuffer,
662 void *buffer, u32 bufferlen, u64 requestid)
663{
664 int ret;
665 struct vmbus_channel_packet_multipage_buffer desc;
666 u32 descsize;
667 u32 packetlen;
668 u32 packetlen_aligned;
669 struct scatterlist bufferlist[3];
670 u64 aligned_data = 0;
671 u32 pfncount = NUM_PAGES_SPANNED(multi_pagebuffer->offset,
672 multi_pagebuffer->len);
673
674
675 if ((pfncount < 0) || (pfncount > MAX_MULTIPAGE_BUFFER_COUNT))
676 return -EINVAL;
677
678 /*
679 * Adjust the size down since vmbus_channel_packet_multipage_buffer is
680 * the largest size we support
681 */
682 descsize = sizeof(struct vmbus_channel_packet_multipage_buffer) -
683 ((MAX_MULTIPAGE_BUFFER_COUNT - pfncount) *
684 sizeof(u64));
685 packetlen = descsize + bufferlen;
686 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
687
688
689 /* Setup the descriptor */
690 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
691 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
692 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
693 desc.length8 = (u16)(packetlen_aligned >> 3);
694 desc.transactionid = requestid;
695 desc.rangecount = 1;
696
697 desc.range.len = multi_pagebuffer->len;
698 desc.range.offset = multi_pagebuffer->offset;
699
700 memcpy(desc.range.pfn_array, multi_pagebuffer->pfn_array,
701 pfncount * sizeof(u64));
702
703 sg_init_table(bufferlist, 3);
704 sg_set_buf(&bufferlist[0], &desc, descsize);
705 sg_set_buf(&bufferlist[1], buffer, bufferlen);
706 sg_set_buf(&bufferlist[2], &aligned_data,
707 packetlen_aligned - packetlen);
708
709 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3);
710
711 if (ret == 0 && !hv_get_ringbuffer_interrupt_mask(&channel->outbound))
712 vmbus_setevent(channel);
713
714 return ret;
715}
716EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer);
717
718/**
719 * vmbus_recvpacket() - Retrieve the user packet on the specified channel
720 * @channel: Pointer to vmbus_channel structure.
721 * @buffer: Pointer to the buffer you want to receive the data into.
722 * @bufferlen: Maximum size of what the the buffer will hold
723 * @buffer_actual_len: The actual size of the data after it was received
724 * @requestid: Identifier of the request
725 *
726 * Receives directly from the hyper-v vmbus and puts the data it received
727 * into Buffer. This will receive the data unparsed from hyper-v.
728 *
729 * Mainly used by Hyper-V drivers.
730 */
731int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
732 u32 bufferlen, u32 *buffer_actual_len, u64 *requestid)
733{
734 struct vmpacket_descriptor desc;
735 u32 packetlen;
736 u32 userlen;
737 int ret;
738 unsigned long flags;
739
740 *buffer_actual_len = 0;
741 *requestid = 0;
742
743 spin_lock_irqsave(&channel->inbound_lock, flags);
744
745 ret = hv_ringbuffer_peek(&channel->inbound, &desc,
746 sizeof(struct vmpacket_descriptor));
747 if (ret != 0) {
748 spin_unlock_irqrestore(&channel->inbound_lock, flags);
749 return 0;
750 }
751
752 packetlen = desc.len8 << 3;
753 userlen = packetlen - (desc.offset8 << 3);
754
755 *buffer_actual_len = userlen;
756
757 if (userlen > bufferlen) {
758 spin_unlock_irqrestore(&channel->inbound_lock, flags);
759
760 pr_err("Buffer too small - got %d needs %d\n",
761 bufferlen, userlen);
762 return -ETOOSMALL;
763 }
764
765 *requestid = desc.trans_id;
766
767 /* Copy over the packet to the user buffer */
768 ret = hv_ringbuffer_read(&channel->inbound, buffer, userlen,
769 (desc.offset8 << 3));
770
771 spin_unlock_irqrestore(&channel->inbound_lock, flags);
772
773 return 0;
774}
775EXPORT_SYMBOL(vmbus_recvpacket);
776
777/*
778 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
779 */
780int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
781 u32 bufferlen, u32 *buffer_actual_len,
782 u64 *requestid)
783{
784 struct vmpacket_descriptor desc;
785 u32 packetlen;
786 u32 userlen;
787 int ret;
788 unsigned long flags;
789
790 *buffer_actual_len = 0;
791 *requestid = 0;
792
793 spin_lock_irqsave(&channel->inbound_lock, flags);
794
795 ret = hv_ringbuffer_peek(&channel->inbound, &desc,
796 sizeof(struct vmpacket_descriptor));
797 if (ret != 0) {
798 spin_unlock_irqrestore(&channel->inbound_lock, flags);
799 return 0;
800 }
801
802
803 packetlen = desc.len8 << 3;
804 userlen = packetlen - (desc.offset8 << 3);
805
806 *buffer_actual_len = packetlen;
807
808 if (packetlen > bufferlen) {
809 spin_unlock_irqrestore(&channel->inbound_lock, flags);
810
811 pr_err("Buffer too small - needed %d bytes but "
812 "got space for only %d bytes\n",
813 packetlen, bufferlen);
814 return -2;
815 }
816
817 *requestid = desc.trans_id;
818
819 /* Copy over the entire packet to the user buffer */
820 ret = hv_ringbuffer_read(&channel->inbound, buffer, packetlen, 0);
821
822 spin_unlock_irqrestore(&channel->inbound_lock, flags);
823 return 0;
824}
825EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);
This page took 0.02689 seconds and 5 git commands to generate.