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