Staging: hv: Rename ringbuffer_read
[deliverable/linux.git] / drivers / staging / 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 "hv_api.h"
30 #include "logging.h"
31 #include "vmbus_private.h"
32
33 #define NUM_PAGES_SPANNED(addr, len) \
34 ((PAGE_ALIGN(addr + len) >> PAGE_SHIFT) - (addr >> PAGE_SHIFT))
35
36 /* Internal routines */
37 static int create_gpadl_header(
38 void *kbuffer, /* must be phys and virt contiguous */
39 u32 size, /* page-size multiple */
40 struct vmbus_channel_msginfo **msginfo,
41 u32 *messagecount);
42 static void dump_vmbus_channel(struct vmbus_channel *channel);
43 static void vmbus_setevent(struct vmbus_channel *channel);
44
45
46 #if 0
47 static void DumpMonitorPage(struct hv_monitor_page *MonitorPage)
48 {
49 int i = 0;
50 int j = 0;
51
52 DPRINT_DBG(VMBUS, "monitorPage - %p, trigger state - %d",
53 MonitorPage, MonitorPage->trigger_state);
54
55 for (i = 0; i < 4; i++)
56 DPRINT_DBG(VMBUS, "trigger group (%d) - %llx", i,
57 MonitorPage->trigger_group[i].as_uint64);
58
59 for (i = 0; i < 4; i++) {
60 for (j = 0; j < 32; j++) {
61 DPRINT_DBG(VMBUS, "latency (%d)(%d) - %llx", i, j,
62 MonitorPage->latency[i][j]);
63 }
64 }
65 for (i = 0; i < 4; i++) {
66 for (j = 0; j < 32; j++) {
67 DPRINT_DBG(VMBUS, "param-conn id (%d)(%d) - %d", i, j,
68 MonitorPage->parameter[i][j].connectionid.asu32);
69 DPRINT_DBG(VMBUS, "param-flag (%d)(%d) - %d", i, j,
70 MonitorPage->parameter[i][j].flag_number);
71 }
72 }
73 }
74 #endif
75
76 /*
77 * vmbus_setevent- Trigger an event notification on the specified
78 * channel.
79 */
80 static void vmbus_setevent(struct vmbus_channel *channel)
81 {
82 struct hv_monitor_page *monitorpage;
83
84 if (channel->offermsg.monitor_allocated) {
85 /* Each u32 represents 32 channels */
86 sync_set_bit(channel->offermsg.child_relid & 31,
87 (unsigned long *) vmbus_connection.send_int_page +
88 (channel->offermsg.child_relid >> 5));
89
90 monitorpage = vmbus_connection.monitor_pages;
91 monitorpage++; /* Get the child to parent monitor page */
92
93 sync_set_bit(channel->monitor_bit,
94 (unsigned long *)&monitorpage->trigger_group
95 [channel->monitor_grp].pending);
96
97 } else {
98 vmbus_set_event(channel->offermsg.child_relid);
99 }
100 }
101
102 #if 0
103 static void VmbusChannelClearEvent(struct vmbus_channel *channel)
104 {
105 struct hv_monitor_page *monitorPage;
106
107 if (Channel->offermsg.monitor_allocated) {
108 /* Each u32 represents 32 channels */
109 sync_clear_bit(Channel->offermsg.child_relid & 31,
110 (unsigned long *)vmbus_connection.send_int_page +
111 (Channel->offermsg.child_relid >> 5));
112
113 monitorPage = (struct hv_monitor_page *)
114 vmbus_connection.monitor_pages;
115 monitorPage++; /* Get the child to parent monitor page */
116
117 sync_clear_bit(Channel->monitor_bit,
118 (unsigned long *)&monitorPage->trigger_group
119 [Channel->monitor_grp].Pending);
120 }
121 }
122
123 #endif
124 /*
125 * vmbus_get_debug_info -Retrieve various channel debug info
126 */
127 void vmbus_get_debug_info(struct vmbus_channel *channel,
128 struct vmbus_channel_debug_info *debuginfo)
129 {
130 struct hv_monitor_page *monitorpage;
131 u8 monitor_group = (u8)channel->offermsg.monitorid / 32;
132 u8 monitor_offset = (u8)channel->offermsg.monitorid % 32;
133 /* u32 monitorBit = 1 << monitorOffset; */
134
135 debuginfo->relid = channel->offermsg.child_relid;
136 debuginfo->state = channel->state;
137 memcpy(&debuginfo->interfacetype,
138 &channel->offermsg.offer.if_type, sizeof(struct hv_guid));
139 memcpy(&debuginfo->interface_instance,
140 &channel->offermsg.offer.if_instance,
141 sizeof(struct hv_guid));
142
143 monitorpage = (struct hv_monitor_page *)vmbus_connection.monitor_pages;
144
145 debuginfo->monitorid = channel->offermsg.monitorid;
146
147 debuginfo->servermonitor_pending =
148 monitorpage->trigger_group[monitor_group].pending;
149 debuginfo->servermonitor_latency =
150 monitorpage->latency[monitor_group][monitor_offset];
151 debuginfo->servermonitor_connectionid =
152 monitorpage->parameter[monitor_group]
153 [monitor_offset].connectionid.u.id;
154
155 monitorpage++;
156
157 debuginfo->clientmonitor_pending =
158 monitorpage->trigger_group[monitor_group].pending;
159 debuginfo->clientmonitor_latency =
160 monitorpage->latency[monitor_group][monitor_offset];
161 debuginfo->clientmonitor_connectionid =
162 monitorpage->parameter[monitor_group]
163 [monitor_offset].connectionid.u.id;
164
165 ringbuffer_get_debuginfo(&channel->inbound, &debuginfo->inbound);
166 ringbuffer_get_debuginfo(&channel->outbound, &debuginfo->outbound);
167 }
168
169 /*
170 * vmbus_open - Open the specified channel.
171 */
172 int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size,
173 u32 recv_ringbuffer_size, void *userdata, u32 userdatalen,
174 void (*onchannelcallback)(void *context), void *context)
175 {
176 struct vmbus_channel_open_channel *openMsg;
177 struct vmbus_channel_msginfo *openInfo = NULL;
178 void *in, *out;
179 unsigned long flags;
180 int ret, err = 0;
181
182 /* Aligned to page size */
183 /* ASSERT(!(SendRingBufferSize & (PAGE_SIZE - 1))); */
184 /* ASSERT(!(RecvRingBufferSize & (PAGE_SIZE - 1))); */
185
186 newchannel->onchannel_callback = onchannelcallback;
187 newchannel->channel_callback_context = context;
188
189 /* Allocate the ring buffer */
190 out = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO,
191 get_order(send_ringbuffer_size + recv_ringbuffer_size));
192
193 if (!out)
194 return -ENOMEM;
195
196 /* ASSERT(((unsigned long)out & (PAGE_SIZE-1)) == 0); */
197
198 in = (void *)((unsigned long)out + send_ringbuffer_size);
199
200 newchannel->ringbuffer_pages = out;
201 newchannel->ringbuffer_pagecount = (send_ringbuffer_size +
202 recv_ringbuffer_size) >> PAGE_SHIFT;
203
204 ret = hv_ringbuffer_init(
205 &newchannel->outbound, out, send_ringbuffer_size);
206
207 if (ret != 0) {
208 err = ret;
209 goto errorout;
210 }
211
212 ret = hv_ringbuffer_init(
213 &newchannel->inbound, in, recv_ringbuffer_size);
214 if (ret != 0) {
215 err = ret;
216 goto errorout;
217 }
218
219
220 /* Establish the gpadl for the ring buffer */
221 newchannel->ringbuffer_gpadlhandle = 0;
222
223 ret = vmbus_establish_gpadl(newchannel,
224 newchannel->outbound.ring_buffer,
225 send_ringbuffer_size +
226 recv_ringbuffer_size,
227 &newchannel->ringbuffer_gpadlhandle);
228
229 if (ret != 0) {
230 err = ret;
231 goto errorout;
232 }
233
234 /* Create and init the channel open message */
235 openInfo = kmalloc(sizeof(*openInfo) +
236 sizeof(struct vmbus_channel_open_channel),
237 GFP_KERNEL);
238 if (!openInfo) {
239 err = -ENOMEM;
240 goto errorout;
241 }
242
243 init_waitqueue_head(&openInfo->waitevent);
244
245 openMsg = (struct vmbus_channel_open_channel *)openInfo->msg;
246 openMsg->header.msgtype = CHANNELMSG_OPENCHANNEL;
247 openMsg->openid = newchannel->offermsg.child_relid; /* FIXME */
248 openMsg->child_relid = newchannel->offermsg.child_relid;
249 openMsg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle;
250 openMsg->downstream_ringbuffer_pageoffset = send_ringbuffer_size >>
251 PAGE_SHIFT;
252 openMsg->server_contextarea_gpadlhandle = 0; /* TODO */
253
254 if (userdatalen > MAX_USER_DEFINED_BYTES) {
255 err = -EINVAL;
256 goto errorout;
257 }
258
259 if (userdatalen)
260 memcpy(openMsg->userdata, userdata, userdatalen);
261
262 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
263 list_add_tail(&openInfo->msglistentry,
264 &vmbus_connection.chn_msg_list);
265 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
266
267 ret = vmbus_post_msg(openMsg,
268 sizeof(struct vmbus_channel_open_channel));
269
270 if (ret != 0)
271 goto Cleanup;
272
273 openInfo->wait_condition = 0;
274 wait_event_timeout(openInfo->waitevent,
275 openInfo->wait_condition,
276 msecs_to_jiffies(1000));
277 if (openInfo->wait_condition == 0) {
278 err = -ETIMEDOUT;
279 goto errorout;
280 }
281
282
283 if (openInfo->response.open_result.status)
284 err = openInfo->response.open_result.status;
285
286 Cleanup:
287 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
288 list_del(&openInfo->msglistentry);
289 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
290
291 kfree(openInfo);
292 return err;
293
294 errorout:
295 hv_ringbuffer_cleanup(&newchannel->outbound);
296 hv_ringbuffer_cleanup(&newchannel->inbound);
297 free_pages((unsigned long)out,
298 get_order(send_ringbuffer_size + recv_ringbuffer_size));
299 kfree(openInfo);
300 return err;
301 }
302 EXPORT_SYMBOL_GPL(vmbus_open);
303
304 /*
305 * dump_gpadl_body - Dump the gpadl body message to the console for
306 * debugging purposes.
307 */
308 static void dump_gpadl_body(struct vmbus_channel_gpadl_body *gpadl, u32 len)
309 {
310 int i;
311 int pfncount;
312
313 pfncount = (len - sizeof(struct vmbus_channel_gpadl_body)) /
314 sizeof(u64);
315
316 DPRINT_DBG(VMBUS, "gpadl body - len %d pfn count %d", len, pfncount);
317
318 for (i = 0; i < pfncount; i++)
319 DPRINT_DBG(VMBUS, "gpadl body - %d) pfn %llu",
320 i, gpadl->pfn[i]);
321 }
322
323 /*
324 * dump_gpadl_header - Dump the gpadl header message to the console for
325 * debugging purposes.
326 */
327 static void dump_gpadl_header(struct vmbus_channel_gpadl_header *gpadl)
328 {
329 int i, j;
330 int pagecount;
331
332 DPRINT_DBG(VMBUS,
333 "gpadl header - relid %d, range count %d, range buflen %d",
334 gpadl->child_relid, gpadl->rangecount, gpadl->range_buflen);
335 for (i = 0; i < gpadl->rangecount; i++) {
336 pagecount = gpadl->range[i].byte_count >> PAGE_SHIFT;
337 pagecount = (pagecount > 26) ? 26 : pagecount;
338
339 DPRINT_DBG(VMBUS, "gpadl range %d - len %d offset %d "
340 "page count %d", i, gpadl->range[i].byte_count,
341 gpadl->range[i].byte_offset, pagecount);
342
343 for (j = 0; j < pagecount; j++)
344 DPRINT_DBG(VMBUS, "%d) pfn %llu", j,
345 gpadl->range[i].pfn_array[j]);
346 }
347 }
348
349 /*
350 * create_gpadl_header - Creates a gpadl for the specified buffer
351 */
352 static int create_gpadl_header(void *kbuffer, u32 size,
353 struct vmbus_channel_msginfo **msginfo,
354 u32 *messagecount)
355 {
356 int i;
357 int pagecount;
358 unsigned long long pfn;
359 struct vmbus_channel_gpadl_header *gpadl_header;
360 struct vmbus_channel_gpadl_body *gpadl_body;
361 struct vmbus_channel_msginfo *msgheader;
362 struct vmbus_channel_msginfo *msgbody = NULL;
363 u32 msgsize;
364
365 int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
366
367 /* ASSERT((kbuffer & (PAGE_SIZE-1)) == 0); */
368 /* ASSERT((Size & (PAGE_SIZE-1)) == 0); */
369
370 pagecount = size >> PAGE_SHIFT;
371 pfn = virt_to_phys(kbuffer) >> PAGE_SHIFT;
372
373 /* do we need a gpadl body msg */
374 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
375 sizeof(struct vmbus_channel_gpadl_header) -
376 sizeof(struct gpa_range);
377 pfncount = pfnsize / sizeof(u64);
378
379 if (pagecount > pfncount) {
380 /* we need a gpadl body */
381 /* fill in the header */
382 msgsize = sizeof(struct vmbus_channel_msginfo) +
383 sizeof(struct vmbus_channel_gpadl_header) +
384 sizeof(struct gpa_range) + pfncount * sizeof(u64);
385 msgheader = kzalloc(msgsize, GFP_KERNEL);
386 if (!msgheader)
387 goto nomem;
388
389 INIT_LIST_HEAD(&msgheader->submsglist);
390 msgheader->msgsize = msgsize;
391
392 gpadl_header = (struct vmbus_channel_gpadl_header *)
393 msgheader->msg;
394 gpadl_header->rangecount = 1;
395 gpadl_header->range_buflen = sizeof(struct gpa_range) +
396 pagecount * sizeof(u64);
397 gpadl_header->range[0].byte_offset = 0;
398 gpadl_header->range[0].byte_count = size;
399 for (i = 0; i < pfncount; i++)
400 gpadl_header->range[0].pfn_array[i] = pfn+i;
401 *msginfo = msgheader;
402 *messagecount = 1;
403
404 pfnsum = pfncount;
405 pfnleft = pagecount - pfncount;
406
407 /* how many pfns can we fit */
408 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
409 sizeof(struct vmbus_channel_gpadl_body);
410 pfncount = pfnsize / sizeof(u64);
411
412 /* fill in the body */
413 while (pfnleft) {
414 if (pfnleft > pfncount)
415 pfncurr = pfncount;
416 else
417 pfncurr = pfnleft;
418
419 msgsize = sizeof(struct vmbus_channel_msginfo) +
420 sizeof(struct vmbus_channel_gpadl_body) +
421 pfncurr * sizeof(u64);
422 msgbody = kzalloc(msgsize, GFP_KERNEL);
423 /* FIXME: we probably need to more if this fails */
424 if (!msgbody)
425 goto nomem;
426 msgbody->msgsize = msgsize;
427 (*messagecount)++;
428 gpadl_body =
429 (struct vmbus_channel_gpadl_body *)msgbody->msg;
430
431 /*
432 * FIXME:
433 * Gpadl is u32 and we are using a pointer which could
434 * be 64-bit
435 */
436 /* gpadl_body->Gpadl = kbuffer; */
437 for (i = 0; i < pfncurr; i++)
438 gpadl_body->pfn[i] = pfn + pfnsum + i;
439
440 /* add to msg header */
441 list_add_tail(&msgbody->msglistentry,
442 &msgheader->submsglist);
443 pfnsum += pfncurr;
444 pfnleft -= pfncurr;
445 }
446 } else {
447 /* everything fits in a header */
448 msgsize = sizeof(struct vmbus_channel_msginfo) +
449 sizeof(struct vmbus_channel_gpadl_header) +
450 sizeof(struct gpa_range) + pagecount * sizeof(u64);
451 msgheader = kzalloc(msgsize, GFP_KERNEL);
452 if (msgheader == NULL)
453 goto nomem;
454 msgheader->msgsize = msgsize;
455
456 gpadl_header = (struct vmbus_channel_gpadl_header *)
457 msgheader->msg;
458 gpadl_header->rangecount = 1;
459 gpadl_header->range_buflen = sizeof(struct gpa_range) +
460 pagecount * sizeof(u64);
461 gpadl_header->range[0].byte_offset = 0;
462 gpadl_header->range[0].byte_count = size;
463 for (i = 0; i < pagecount; i++)
464 gpadl_header->range[0].pfn_array[i] = pfn+i;
465
466 *msginfo = msgheader;
467 *messagecount = 1;
468 }
469
470 return 0;
471 nomem:
472 kfree(msgheader);
473 kfree(msgbody);
474 return -ENOMEM;
475 }
476
477 /*
478 * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
479 *
480 * @channel: a channel
481 * @kbuffer: from kmalloc
482 * @size: page-size multiple
483 * @gpadl_handle: some funky thing
484 */
485 int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
486 u32 size, u32 *gpadl_handle)
487 {
488 struct vmbus_channel_gpadl_header *gpadlmsg;
489 struct vmbus_channel_gpadl_body *gpadl_body;
490 /* struct vmbus_channel_gpadl_created *gpadlCreated; */
491 struct vmbus_channel_msginfo *msginfo = NULL;
492 struct vmbus_channel_msginfo *submsginfo;
493 u32 msgcount;
494 struct list_head *curr;
495 u32 next_gpadl_handle;
496 unsigned long flags;
497 int ret = 0;
498
499 next_gpadl_handle = atomic_read(&vmbus_connection.next_gpadl_handle);
500 atomic_inc(&vmbus_connection.next_gpadl_handle);
501
502 ret = create_gpadl_header(kbuffer, size, &msginfo, &msgcount);
503 if (ret)
504 return ret;
505
506 init_waitqueue_head(&msginfo->waitevent);
507
508 gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
509 gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
510 gpadlmsg->child_relid = channel->offermsg.child_relid;
511 gpadlmsg->gpadl = next_gpadl_handle;
512
513 dump_gpadl_header(gpadlmsg);
514
515 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
516 list_add_tail(&msginfo->msglistentry,
517 &vmbus_connection.chn_msg_list);
518
519 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
520
521 msginfo->wait_condition = 0;
522 ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
523 sizeof(*msginfo));
524 if (ret != 0)
525 goto Cleanup;
526
527 if (msgcount > 1) {
528 list_for_each(curr, &msginfo->submsglist) {
529
530 /* FIXME: should this use list_entry() instead ? */
531 submsginfo = (struct vmbus_channel_msginfo *)curr;
532 gpadl_body =
533 (struct vmbus_channel_gpadl_body *)submsginfo->msg;
534
535 gpadl_body->header.msgtype =
536 CHANNELMSG_GPADL_BODY;
537 gpadl_body->gpadl = next_gpadl_handle;
538
539 dump_gpadl_body(gpadl_body, submsginfo->msgsize -
540 sizeof(*submsginfo));
541 ret = vmbus_post_msg(gpadl_body,
542 submsginfo->msgsize -
543 sizeof(*submsginfo));
544 if (ret != 0)
545 goto Cleanup;
546
547 }
548 }
549 wait_event_timeout(msginfo->waitevent,
550 msginfo->wait_condition,
551 msecs_to_jiffies(1000));
552 BUG_ON(msginfo->wait_condition == 0);
553
554
555 /* At this point, we received the gpadl created msg */
556 *gpadl_handle = gpadlmsg->gpadl;
557
558 Cleanup:
559 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
560 list_del(&msginfo->msglistentry);
561 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
562
563 kfree(msginfo);
564 return ret;
565 }
566 EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
567
568 /*
569 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
570 */
571 int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
572 {
573 struct vmbus_channel_gpadl_teardown *msg;
574 struct vmbus_channel_msginfo *info;
575 unsigned long flags;
576 int ret;
577
578 /* ASSERT(gpadl_handle != 0); */
579
580 info = kmalloc(sizeof(*info) +
581 sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
582 if (!info)
583 return -ENOMEM;
584
585 init_waitqueue_head(&info->waitevent);
586
587 msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
588
589 msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
590 msg->child_relid = channel->offermsg.child_relid;
591 msg->gpadl = gpadl_handle;
592
593 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
594 list_add_tail(&info->msglistentry,
595 &vmbus_connection.chn_msg_list);
596 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
597 info->wait_condition = 0;
598 ret = vmbus_post_msg(msg,
599 sizeof(struct vmbus_channel_gpadl_teardown));
600
601 BUG_ON(ret != 0);
602 wait_event_timeout(info->waitevent,
603 info->wait_condition, msecs_to_jiffies(1000));
604 BUG_ON(info->wait_condition == 0);
605
606 /* Received a torndown response */
607 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
608 list_del(&info->msglistentry);
609 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
610
611 kfree(info);
612 return ret;
613 }
614 EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
615
616 /*
617 * vmbus_close - Close the specified channel
618 */
619 void vmbus_close(struct vmbus_channel *channel)
620 {
621 struct vmbus_channel_close_channel *msg;
622 struct vmbus_channel_msginfo *info;
623 unsigned long flags;
624 int ret;
625
626 /* Stop callback and cancel the timer asap */
627 channel->onchannel_callback = NULL;
628 del_timer_sync(&channel->poll_timer);
629
630 /* Send a closing message */
631 info = kmalloc(sizeof(*info) +
632 sizeof(struct vmbus_channel_close_channel), GFP_KERNEL);
633 /* FIXME: can't do anything other than return here because the
634 * function is void */
635 if (!info)
636 return;
637
638
639 msg = (struct vmbus_channel_close_channel *)info->msg;
640 msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
641 msg->child_relid = channel->offermsg.child_relid;
642
643 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel));
644
645 BUG_ON(ret != 0);
646 /* Tear down the gpadl for the channel's ring buffer */
647 if (channel->ringbuffer_gpadlhandle)
648 vmbus_teardown_gpadl(channel,
649 channel->ringbuffer_gpadlhandle);
650
651 /* TODO: Send a msg to release the childRelId */
652
653 /* Cleanup the ring buffers for this channel */
654 hv_ringbuffer_cleanup(&channel->outbound);
655 hv_ringbuffer_cleanup(&channel->inbound);
656
657 free_pages((unsigned long)channel->ringbuffer_pages,
658 get_order(channel->ringbuffer_pagecount * PAGE_SIZE));
659
660 kfree(info);
661
662 /*
663 * If we are closing the channel during an error path in
664 * opening the channel, don't free the channel since the
665 * caller will free the channel
666 */
667
668 if (channel->state == CHANNEL_OPEN_STATE) {
669 spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
670 list_del(&channel->listentry);
671 spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
672
673 free_channel(channel);
674 }
675 }
676 EXPORT_SYMBOL_GPL(vmbus_close);
677
678 /**
679 * vmbus_sendpacket() - Send the specified buffer on the given channel
680 * @channel: Pointer to vmbus_channel structure.
681 * @buffer: Pointer to the buffer you want to receive the data into.
682 * @bufferlen: Maximum size of what the the buffer will hold
683 * @requestid: Identifier of the request
684 * @type: Type of packet that is being send e.g. negotiate, time
685 * packet etc.
686 *
687 * Sends data in @buffer directly to hyper-v via the vmbus
688 * This will send the data unparsed to hyper-v.
689 *
690 * Mainly used by Hyper-V drivers.
691 */
692 int vmbus_sendpacket(struct vmbus_channel *channel, const void *buffer,
693 u32 bufferlen, u64 requestid,
694 enum vmbus_packet_type type, u32 flags)
695 {
696 struct vmpacket_descriptor desc;
697 u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
698 u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
699 struct scatterlist bufferlist[3];
700 u64 aligned_data = 0;
701 int ret;
702
703 dump_vmbus_channel(channel);
704
705 /* ASSERT((packetLenAligned - packetLen) < sizeof(u64)); */
706
707 /* Setup the descriptor */
708 desc.type = type; /* VmbusPacketTypeDataInBand; */
709 desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
710 /* in 8-bytes granularity */
711 desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
712 desc.len8 = (u16)(packetlen_aligned >> 3);
713 desc.trans_id = requestid;
714
715 sg_init_table(bufferlist, 3);
716 sg_set_buf(&bufferlist[0], &desc, sizeof(struct vmpacket_descriptor));
717 sg_set_buf(&bufferlist[1], buffer, bufferlen);
718 sg_set_buf(&bufferlist[2], &aligned_data,
719 packetlen_aligned - packetlen);
720
721 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3);
722
723 /* TODO: We should determine if this is optional */
724 if (ret == 0 && !get_ringbuffer_interrupt_mask(&channel->outbound))
725 vmbus_setevent(channel);
726
727 return ret;
728 }
729 EXPORT_SYMBOL(vmbus_sendpacket);
730
731 /*
732 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
733 * packets using a GPADL Direct packet type.
734 */
735 int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
736 struct hv_page_buffer pagebuffers[],
737 u32 pagecount, void *buffer, u32 bufferlen,
738 u64 requestid)
739 {
740 int ret;
741 int i;
742 struct vmbus_channel_packet_page_buffer desc;
743 u32 descsize;
744 u32 packetlen;
745 u32 packetlen_aligned;
746 struct scatterlist bufferlist[3];
747 u64 aligned_data = 0;
748
749 if (pagecount > MAX_PAGE_BUFFER_COUNT)
750 return -EINVAL;
751
752 dump_vmbus_channel(channel);
753
754 /*
755 * Adjust the size down since vmbus_channel_packet_page_buffer is the
756 * largest size we support
757 */
758 descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
759 ((MAX_PAGE_BUFFER_COUNT - pagecount) *
760 sizeof(struct hv_page_buffer));
761 packetlen = descsize + bufferlen;
762 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
763
764 /* ASSERT((packetLenAligned - packetLen) < sizeof(u64)); */
765
766 /* Setup the descriptor */
767 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
768 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
769 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
770 desc.length8 = (u16)(packetlen_aligned >> 3);
771 desc.transactionid = requestid;
772 desc.rangecount = pagecount;
773
774 for (i = 0; i < pagecount; i++) {
775 desc.range[i].len = pagebuffers[i].len;
776 desc.range[i].offset = pagebuffers[i].offset;
777 desc.range[i].pfn = pagebuffers[i].pfn;
778 }
779
780 sg_init_table(bufferlist, 3);
781 sg_set_buf(&bufferlist[0], &desc, descsize);
782 sg_set_buf(&bufferlist[1], buffer, bufferlen);
783 sg_set_buf(&bufferlist[2], &aligned_data,
784 packetlen_aligned - packetlen);
785
786 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3);
787
788 /* TODO: We should determine if this is optional */
789 if (ret == 0 && !get_ringbuffer_interrupt_mask(&channel->outbound))
790 vmbus_setevent(channel);
791
792 return ret;
793 }
794 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
795
796 /*
797 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
798 * using a GPADL Direct packet type.
799 */
800 int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
801 struct hv_multipage_buffer *multi_pagebuffer,
802 void *buffer, u32 bufferlen, u64 requestid)
803 {
804 int ret;
805 struct vmbus_channel_packet_multipage_buffer desc;
806 u32 descsize;
807 u32 packetlen;
808 u32 packetlen_aligned;
809 struct scatterlist bufferlist[3];
810 u64 aligned_data = 0;
811 u32 pfncount = NUM_PAGES_SPANNED(multi_pagebuffer->offset,
812 multi_pagebuffer->len);
813
814 dump_vmbus_channel(channel);
815
816 if ((pfncount < 0) || (pfncount > MAX_MULTIPAGE_BUFFER_COUNT))
817 return -EINVAL;
818
819 /*
820 * Adjust the size down since vmbus_channel_packet_multipage_buffer is
821 * the largest size we support
822 */
823 descsize = sizeof(struct vmbus_channel_packet_multipage_buffer) -
824 ((MAX_MULTIPAGE_BUFFER_COUNT - pfncount) *
825 sizeof(u64));
826 packetlen = descsize + bufferlen;
827 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
828
829 /* ASSERT((packetLenAligned - packetLen) < sizeof(u64)); */
830
831 /* Setup the descriptor */
832 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
833 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
834 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
835 desc.length8 = (u16)(packetlen_aligned >> 3);
836 desc.transactionid = requestid;
837 desc.rangecount = 1;
838
839 desc.range.len = multi_pagebuffer->len;
840 desc.range.offset = multi_pagebuffer->offset;
841
842 memcpy(desc.range.pfn_array, multi_pagebuffer->pfn_array,
843 pfncount * sizeof(u64));
844
845 sg_init_table(bufferlist, 3);
846 sg_set_buf(&bufferlist[0], &desc, descsize);
847 sg_set_buf(&bufferlist[1], buffer, bufferlen);
848 sg_set_buf(&bufferlist[2], &aligned_data,
849 packetlen_aligned - packetlen);
850
851 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3);
852
853 /* TODO: We should determine if this is optional */
854 if (ret == 0 && !get_ringbuffer_interrupt_mask(&channel->outbound))
855 vmbus_setevent(channel);
856
857 return ret;
858 }
859 EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer);
860
861 /**
862 * vmbus_recvpacket() - Retrieve the user packet on the specified channel
863 * @channel: Pointer to vmbus_channel structure.
864 * @buffer: Pointer to the buffer you want to receive the data into.
865 * @bufferlen: Maximum size of what the the buffer will hold
866 * @buffer_actual_len: The actual size of the data after it was received
867 * @requestid: Identifier of the request
868 *
869 * Receives directly from the hyper-v vmbus and puts the data it received
870 * into Buffer. This will receive the data unparsed from hyper-v.
871 *
872 * Mainly used by Hyper-V drivers.
873 */
874 int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
875 u32 bufferlen, u32 *buffer_actual_len, u64 *requestid)
876 {
877 struct vmpacket_descriptor desc;
878 u32 packetlen;
879 u32 userlen;
880 int ret;
881 unsigned long flags;
882
883 *buffer_actual_len = 0;
884 *requestid = 0;
885
886 spin_lock_irqsave(&channel->inbound_lock, flags);
887
888 ret = hv_ringbuffer_peek(&channel->inbound, &desc,
889 sizeof(struct vmpacket_descriptor));
890 if (ret != 0) {
891 spin_unlock_irqrestore(&channel->inbound_lock, flags);
892 return 0;
893 }
894
895 /* VmbusChannelClearEvent(Channel); */
896
897 packetlen = desc.len8 << 3;
898 userlen = packetlen - (desc.offset8 << 3);
899 /* ASSERT(userLen > 0); */
900
901 *buffer_actual_len = userlen;
902
903 if (userlen > bufferlen) {
904 spin_unlock_irqrestore(&channel->inbound_lock, flags);
905
906 pr_err("Buffer too small - got %d needs %d\n",
907 bufferlen, userlen);
908 return -1;
909 }
910
911 *requestid = desc.trans_id;
912
913 /* Copy over the packet to the user buffer */
914 ret = hv_ringbuffer_read(&channel->inbound, buffer, userlen,
915 (desc.offset8 << 3));
916
917 spin_unlock_irqrestore(&channel->inbound_lock, flags);
918
919 return 0;
920 }
921 EXPORT_SYMBOL(vmbus_recvpacket);
922
923 /*
924 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
925 */
926 int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
927 u32 bufferlen, u32 *buffer_actual_len,
928 u64 *requestid)
929 {
930 struct vmpacket_descriptor desc;
931 u32 packetlen;
932 u32 userlen;
933 int ret;
934 unsigned long flags;
935
936 *buffer_actual_len = 0;
937 *requestid = 0;
938
939 spin_lock_irqsave(&channel->inbound_lock, flags);
940
941 ret = hv_ringbuffer_peek(&channel->inbound, &desc,
942 sizeof(struct vmpacket_descriptor));
943 if (ret != 0) {
944 spin_unlock_irqrestore(&channel->inbound_lock, flags);
945 return 0;
946 }
947
948 /* VmbusChannelClearEvent(Channel); */
949
950 packetlen = desc.len8 << 3;
951 userlen = packetlen - (desc.offset8 << 3);
952
953 *buffer_actual_len = packetlen;
954
955 if (packetlen > bufferlen) {
956 spin_unlock_irqrestore(&channel->inbound_lock, flags);
957
958 pr_err("Buffer too small - needed %d bytes but "
959 "got space for only %d bytes\n",
960 packetlen, bufferlen);
961 return -2;
962 }
963
964 *requestid = desc.trans_id;
965
966 /* Copy over the entire packet to the user buffer */
967 ret = hv_ringbuffer_read(&channel->inbound, buffer, packetlen, 0);
968
969 spin_unlock_irqrestore(&channel->inbound_lock, flags);
970 return 0;
971 }
972 EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);
973
974 /*
975 * vmbus_onchannel_event - Channel event callback
976 */
977 void vmbus_onchannel_event(struct vmbus_channel *channel)
978 {
979 dump_vmbus_channel(channel);
980 /* ASSERT(Channel->OnChannelCallback); */
981
982 channel->onchannel_callback(channel->channel_callback_context);
983
984 mod_timer(&channel->poll_timer, jiffies + usecs_to_jiffies(100));
985 }
986
987 /*
988 * vmbus_ontimer - Timer event callback
989 */
990 void vmbus_ontimer(unsigned long data)
991 {
992 struct vmbus_channel *channel = (struct vmbus_channel *)data;
993
994 if (channel->onchannel_callback)
995 channel->onchannel_callback(channel->channel_callback_context);
996 }
997
998 /*
999 * dump_vmbus_channel- Dump vmbus channel info to the console
1000 */
1001 static void dump_vmbus_channel(struct vmbus_channel *channel)
1002 {
1003 DPRINT_DBG(VMBUS, "Channel (%d)", channel->offermsg.child_relid);
1004 dump_ring_info(&channel->outbound, "Outbound ");
1005 dump_ring_info(&channel->inbound, "Inbound ");
1006 }
This page took 0.053438 seconds and 5 git commands to generate.