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