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