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