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