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