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