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 | ||
a0086dc5 | 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> |
53af545b | 28 | #include <linux/list.h> |
c88c4e4c | 29 | #include <linux/module.h> |
8b5d6d3b | 30 | #include <linux/completion.h> |
46a97191 | 31 | #include <linux/hyperv.h> |
3f335ea2 | 32 | |
0f2a6619 | 33 | #include "hyperv_vmbus.h" |
3e7ee490 | 34 | |
1d7e907f | 35 | struct vmbus_channel_message_table_entry { |
cf9dcba7 | 36 | enum vmbus_channel_message_type message_type; |
fa90f1de | 37 | void (*message_handler)(struct vmbus_channel_message_header *msg); |
1d7e907f | 38 | }; |
3e7ee490 | 39 | |
c88c4e4c HJ |
40 | |
41 | /** | |
da0e9631 | 42 | * vmbus_prep_negotiate_resp() - Create default response for Hyper-V Negotiate message |
c88c4e4c HJ |
43 | * @icmsghdrp: Pointer to msg header structure |
44 | * @icmsg_negotiate: Pointer to negotiate message structure | |
45 | * @buf: Raw buffer channel data | |
46 | * | |
47 | * @icmsghdrp is of type &struct icmsg_hdr. | |
48 | * @negop is of type &struct icmsg_negotiate. | |
c836d0ab S |
49 | * Set up and fill in default negotiate response message. |
50 | * | |
6741335b S |
51 | * The fw_version specifies the framework version that |
52 | * we can support and srv_version specifies the service | |
53 | * version we can support. | |
c88c4e4c HJ |
54 | * |
55 | * Mainly used by Hyper-V drivers. | |
56 | */ | |
6741335b | 57 | bool vmbus_prep_negotiate_resp(struct icmsg_hdr *icmsghdrp, |
c836d0ab | 58 | struct icmsg_negotiate *negop, u8 *buf, |
6741335b | 59 | int fw_version, int srv_version) |
c88c4e4c | 60 | { |
6741335b S |
61 | int icframe_major, icframe_minor; |
62 | int icmsg_major, icmsg_minor; | |
63 | int fw_major, fw_minor; | |
64 | int srv_major, srv_minor; | |
c836d0ab | 65 | int i; |
6741335b | 66 | bool found_match = false; |
c836d0ab | 67 | |
a3605300 | 68 | icmsghdrp->icmsgsize = 0x10; |
6741335b S |
69 | fw_major = (fw_version >> 16); |
70 | fw_minor = (fw_version & 0xFFFF); | |
71 | ||
72 | srv_major = (srv_version >> 16); | |
73 | srv_minor = (srv_version & 0xFFFF); | |
c88c4e4c | 74 | |
a3605300 S |
75 | negop = (struct icmsg_negotiate *)&buf[ |
76 | sizeof(struct vmbuspipe_hdr) + | |
77 | sizeof(struct icmsg_hdr)]; | |
c88c4e4c | 78 | |
6741335b S |
79 | icframe_major = negop->icframe_vercnt; |
80 | icframe_minor = 0; | |
81 | ||
82 | icmsg_major = negop->icmsg_vercnt; | |
83 | icmsg_minor = 0; | |
c836d0ab S |
84 | |
85 | /* | |
86 | * Select the framework version number we will | |
87 | * support. | |
88 | */ | |
89 | ||
90 | for (i = 0; i < negop->icframe_vercnt; i++) { | |
6741335b S |
91 | if ((negop->icversion_data[i].major == fw_major) && |
92 | (negop->icversion_data[i].minor == fw_minor)) { | |
93 | icframe_major = negop->icversion_data[i].major; | |
94 | icframe_minor = negop->icversion_data[i].minor; | |
95 | found_match = true; | |
96 | } | |
c836d0ab S |
97 | } |
98 | ||
6741335b S |
99 | if (!found_match) |
100 | goto fw_error; | |
101 | ||
102 | found_match = false; | |
103 | ||
c836d0ab S |
104 | for (i = negop->icframe_vercnt; |
105 | (i < negop->icframe_vercnt + negop->icmsg_vercnt); i++) { | |
6741335b S |
106 | if ((negop->icversion_data[i].major == srv_major) && |
107 | (negop->icversion_data[i].minor == srv_minor)) { | |
108 | icmsg_major = negop->icversion_data[i].major; | |
109 | icmsg_minor = negop->icversion_data[i].minor; | |
110 | found_match = true; | |
111 | } | |
c88c4e4c | 112 | } |
a3605300 | 113 | |
c836d0ab | 114 | /* |
6741335b | 115 | * Respond with the framework and service |
c836d0ab S |
116 | * version numbers we can support. |
117 | */ | |
6741335b S |
118 | |
119 | fw_error: | |
120 | if (!found_match) { | |
121 | negop->icframe_vercnt = 0; | |
122 | negop->icmsg_vercnt = 0; | |
123 | } else { | |
124 | negop->icframe_vercnt = 1; | |
125 | negop->icmsg_vercnt = 1; | |
126 | } | |
127 | ||
128 | negop->icversion_data[0].major = icframe_major; | |
129 | negop->icversion_data[0].minor = icframe_minor; | |
130 | negop->icversion_data[1].major = icmsg_major; | |
131 | negop->icversion_data[1].minor = icmsg_minor; | |
132 | return found_match; | |
c88c4e4c | 133 | } |
a3605300 | 134 | |
da0e9631 | 135 | EXPORT_SYMBOL_GPL(vmbus_prep_negotiate_resp); |
c88c4e4c | 136 | |
3e189519 | 137 | /* |
e98cb276 | 138 | * alloc_channel - Allocate and initialize a vmbus channel object |
bd60c33e | 139 | */ |
50fe56d2 | 140 | static struct vmbus_channel *alloc_channel(void) |
3e7ee490 | 141 | { |
bc63b6f6 | 142 | static atomic_t chan_num = ATOMIC_INIT(0); |
aded7165 | 143 | struct vmbus_channel *channel; |
3e7ee490 | 144 | |
aded7165 | 145 | channel = kzalloc(sizeof(*channel), GFP_ATOMIC); |
3e7ee490 | 146 | if (!channel) |
3e7ee490 | 147 | return NULL; |
3e7ee490 | 148 | |
bc63b6f6 | 149 | channel->id = atomic_inc_return(&chan_num); |
54411c42 | 150 | spin_lock_init(&channel->inbound_lock); |
67fae053 | 151 | spin_lock_init(&channel->lock); |
e68d2971 S |
152 | |
153 | INIT_LIST_HEAD(&channel->sc_list); | |
3a28fa35 | 154 | INIT_LIST_HEAD(&channel->percpu_list); |
3e7ee490 | 155 | |
bc63b6f6 VK |
156 | channel->controlwq = alloc_workqueue("hv_vmbus_ctl/%d", WQ_MEM_RECLAIM, |
157 | 1, channel->id); | |
c50f7fb2 | 158 | if (!channel->controlwq) { |
8c69f52a | 159 | kfree(channel); |
3e7ee490 HJ |
160 | return NULL; |
161 | } | |
162 | ||
163 | return channel; | |
164 | } | |
165 | ||
3e189519 | 166 | /* |
e98cb276 | 167 | * release_hannel - Release the vmbus channel object itself |
bd60c33e | 168 | */ |
4b2f9abe | 169 | static void release_channel(struct work_struct *work) |
3e7ee490 | 170 | { |
4b2f9abe TT |
171 | struct vmbus_channel *channel = container_of(work, |
172 | struct vmbus_channel, | |
173 | work); | |
3e7ee490 | 174 | |
c50f7fb2 | 175 | destroy_workqueue(channel->controlwq); |
3e7ee490 | 176 | |
8c69f52a | 177 | kfree(channel); |
3e7ee490 HJ |
178 | } |
179 | ||
3e189519 | 180 | /* |
e98cb276 | 181 | * free_channel - Release the resources used by the vmbus channel object |
bd60c33e | 182 | */ |
9f3e28e3 | 183 | static void free_channel(struct vmbus_channel *channel) |
3e7ee490 | 184 | { |
3e7ee490 | 185 | |
bd60c33e GKH |
186 | /* |
187 | * We have to release the channel's workqueue/thread in the vmbus's | |
188 | * workqueue/thread context | |
189 | * ie we can't destroy ourselves. | |
190 | */ | |
4b2f9abe | 191 | INIT_WORK(&channel->work, release_channel); |
da9fcb72 | 192 | queue_work(vmbus_connection.work_queue, &channel->work); |
3e7ee490 HJ |
193 | } |
194 | ||
3a28fa35 S |
195 | static void percpu_channel_enq(void *arg) |
196 | { | |
197 | struct vmbus_channel *channel = arg; | |
198 | int cpu = smp_processor_id(); | |
199 | ||
200 | list_add_tail(&channel->percpu_list, &hv_context.percpu_list[cpu]); | |
201 | } | |
8b5d6d3b | 202 | |
3a28fa35 S |
203 | static void percpu_channel_deq(void *arg) |
204 | { | |
205 | struct vmbus_channel *channel = arg; | |
206 | ||
207 | list_del(&channel->percpu_list); | |
208 | } | |
8b5d6d3b | 209 | |
4b2f9abe TT |
210 | /* |
211 | * vmbus_process_rescind_offer - | |
212 | * Rescind the offer by initiating a device removal | |
213 | */ | |
214 | static void vmbus_process_rescind_offer(struct work_struct *work) | |
215 | { | |
216 | struct vmbus_channel *channel = container_of(work, | |
217 | struct vmbus_channel, | |
218 | work); | |
c8705979 | 219 | unsigned long flags; |
e68d2971 | 220 | struct vmbus_channel *primary_channel; |
c8705979 | 221 | struct vmbus_channel_relid_released msg; |
04a258c1 VK |
222 | struct device *dev; |
223 | ||
224 | if (channel->device_obj) { | |
225 | dev = get_device(&channel->device_obj->device); | |
226 | if (dev) { | |
227 | vmbus_device_unregister(channel->device_obj); | |
228 | put_device(dev); | |
229 | } | |
230 | } | |
4b2f9abe | 231 | |
c8705979 S |
232 | memset(&msg, 0, sizeof(struct vmbus_channel_relid_released)); |
233 | msg.child_relid = channel->offermsg.child_relid; | |
234 | msg.header.msgtype = CHANNELMSG_RELID_RELEASED; | |
235 | vmbus_post_msg(&msg, sizeof(struct vmbus_channel_relid_released)); | |
236 | ||
2115b561 S |
237 | if (channel->target_cpu != get_cpu()) { |
238 | put_cpu(); | |
3a28fa35 S |
239 | smp_call_function_single(channel->target_cpu, |
240 | percpu_channel_deq, channel, true); | |
2115b561 | 241 | } else { |
3a28fa35 | 242 | percpu_channel_deq(channel); |
2115b561 S |
243 | put_cpu(); |
244 | } | |
3a28fa35 | 245 | |
e68d2971 S |
246 | if (channel->primary_channel == NULL) { |
247 | spin_lock_irqsave(&vmbus_connection.channel_lock, flags); | |
248 | list_del(&channel->listentry); | |
249 | spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags); | |
250 | } else { | |
251 | primary_channel = channel->primary_channel; | |
67fae053 | 252 | spin_lock_irqsave(&primary_channel->lock, flags); |
565ce642 | 253 | list_del(&channel->sc_list); |
67fae053 | 254 | spin_unlock_irqrestore(&primary_channel->lock, flags); |
e68d2971 | 255 | } |
c8705979 | 256 | free_channel(channel); |
4b2f9abe | 257 | } |
8b5d6d3b | 258 | |
93e5bd06 S |
259 | void vmbus_free_channels(void) |
260 | { | |
261 | struct vmbus_channel *channel; | |
262 | ||
263 | list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) { | |
264 | vmbus_device_unregister(channel->device_obj); | |
93e5bd06 S |
265 | free_channel(channel); |
266 | } | |
267 | } | |
268 | ||
3e189519 | 269 | /* |
e98cb276 | 270 | * vmbus_process_offer - Process the offer by creating a channel/device |
c88c4e4c | 271 | * associated with this offer |
bd60c33e | 272 | */ |
4b2f9abe | 273 | static void vmbus_process_offer(struct work_struct *work) |
3e7ee490 | 274 | { |
4b2f9abe TT |
275 | struct vmbus_channel *newchannel = container_of(work, |
276 | struct vmbus_channel, | |
277 | work); | |
aded7165 | 278 | struct vmbus_channel *channel; |
188963ec | 279 | bool fnew = true; |
3a28fa35 | 280 | bool enq = false; |
bd60c33e | 281 | int ret; |
0f5e44ca | 282 | unsigned long flags; |
3e7ee490 | 283 | |
454f18a9 | 284 | /* Make sure this is a new offer */ |
15b2f647 | 285 | spin_lock_irqsave(&vmbus_connection.channel_lock, flags); |
3e7ee490 | 286 | |
da9fcb72 | 287 | list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) { |
358d2ee2 S |
288 | if (!uuid_le_cmp(channel->offermsg.offer.if_type, |
289 | newchannel->offermsg.offer.if_type) && | |
290 | !uuid_le_cmp(channel->offermsg.offer.if_instance, | |
291 | newchannel->offermsg.offer.if_instance)) { | |
188963ec | 292 | fnew = false; |
3e7ee490 HJ |
293 | break; |
294 | } | |
295 | } | |
296 | ||
3a28fa35 | 297 | if (fnew) { |
c50f7fb2 | 298 | list_add_tail(&newchannel->listentry, |
da9fcb72 | 299 | &vmbus_connection.chn_list); |
3a28fa35 S |
300 | enq = true; |
301 | } | |
bd60c33e | 302 | |
15b2f647 | 303 | spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags); |
3e7ee490 | 304 | |
3a28fa35 | 305 | if (enq) { |
2115b561 S |
306 | if (newchannel->target_cpu != get_cpu()) { |
307 | put_cpu(); | |
3a28fa35 S |
308 | smp_call_function_single(newchannel->target_cpu, |
309 | percpu_channel_enq, | |
310 | newchannel, true); | |
2115b561 | 311 | } else { |
3a28fa35 | 312 | percpu_channel_enq(newchannel); |
2115b561 S |
313 | put_cpu(); |
314 | } | |
3a28fa35 | 315 | } |
188963ec | 316 | if (!fnew) { |
e68d2971 S |
317 | /* |
318 | * Check to see if this is a sub-channel. | |
319 | */ | |
320 | if (newchannel->offermsg.offer.sub_channel_index != 0) { | |
321 | /* | |
322 | * Process the sub-channel. | |
323 | */ | |
324 | newchannel->primary_channel = channel; | |
67fae053 | 325 | spin_lock_irqsave(&channel->lock, flags); |
e68d2971 | 326 | list_add_tail(&newchannel->sc_list, &channel->sc_list); |
67fae053 | 327 | spin_unlock_irqrestore(&channel->lock, flags); |
3a28fa35 | 328 | |
2115b561 S |
329 | if (newchannel->target_cpu != get_cpu()) { |
330 | put_cpu(); | |
3a28fa35 S |
331 | smp_call_function_single(newchannel->target_cpu, |
332 | percpu_channel_enq, | |
333 | newchannel, true); | |
2115b561 | 334 | } else { |
3a28fa35 | 335 | percpu_channel_enq(newchannel); |
2115b561 S |
336 | put_cpu(); |
337 | } | |
3a28fa35 | 338 | |
e68d2971 S |
339 | newchannel->state = CHANNEL_OPEN_STATE; |
340 | if (channel->sc_creation_callback != NULL) | |
341 | channel->sc_creation_callback(newchannel); | |
342 | ||
d7f2fbaf | 343 | goto done_init_rescind; |
e68d2971 S |
344 | } |
345 | ||
9c3a6f7e | 346 | goto err_free_chan; |
3e7ee490 HJ |
347 | } |
348 | ||
42dceebe S |
349 | /* |
350 | * This state is used to indicate a successful open | |
351 | * so that when we do close the channel normally, we | |
352 | * can cleanup properly | |
353 | */ | |
354 | newchannel->state = CHANNEL_OPEN_STATE; | |
355 | ||
bd60c33e GKH |
356 | /* |
357 | * Start the process of binding this offer to the driver | |
358 | * We need to set the DeviceObject field before calling | |
646f1ea3 | 359 | * vmbus_child_dev_add() |
bd60c33e | 360 | */ |
f2c73011 | 361 | newchannel->device_obj = vmbus_device_create( |
767dff68 HZ |
362 | &newchannel->offermsg.offer.if_type, |
363 | &newchannel->offermsg.offer.if_instance, | |
188963ec | 364 | newchannel); |
9c3a6f7e VK |
365 | if (!newchannel->device_obj) |
366 | goto err_free_chan; | |
3e7ee490 | 367 | |
454f18a9 BP |
368 | /* |
369 | * Add the new device to the bus. This will kick off device-driver | |
370 | * binding which eventually invokes the device driver's AddDevice() | |
371 | * method. | |
372 | */ | |
22794281 | 373 | ret = vmbus_device_register(newchannel->device_obj); |
bd60c33e | 374 | if (ret != 0) { |
0a46618d | 375 | pr_err("unable to add child device object (relid %d)\n", |
c50f7fb2 | 376 | newchannel->offermsg.child_relid); |
3e7ee490 | 377 | |
15b2f647 | 378 | spin_lock_irqsave(&vmbus_connection.channel_lock, flags); |
c50f7fb2 | 379 | list_del(&newchannel->listentry); |
15b2f647 | 380 | spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags); |
8b8ee675 | 381 | kfree(newchannel->device_obj); |
9c3a6f7e | 382 | goto err_free_chan; |
3e7ee490 | 383 | } |
d7f2fbaf VK |
384 | done_init_rescind: |
385 | spin_lock_irqsave(&newchannel->lock, flags); | |
386 | /* The next possible work is rescind handling */ | |
387 | INIT_WORK(&newchannel->work, vmbus_process_rescind_offer); | |
388 | /* Check if rescind offer was already received */ | |
389 | if (newchannel->rescind) | |
390 | queue_work(newchannel->controlwq, &newchannel->work); | |
391 | spin_unlock_irqrestore(&newchannel->lock, flags); | |
9c3a6f7e VK |
392 | return; |
393 | err_free_chan: | |
394 | free_channel(newchannel); | |
3e7ee490 HJ |
395 | } |
396 | ||
a119845f S |
397 | enum { |
398 | IDE = 0, | |
399 | SCSI, | |
400 | NIC, | |
401 | MAX_PERF_CHN, | |
402 | }; | |
403 | ||
404 | /* | |
7fb96565 | 405 | * This is an array of device_ids (device types) that are performance critical. |
a119845f S |
406 | * We attempt to distribute the interrupt load for these devices across |
407 | * all available CPUs. | |
408 | */ | |
7fb96565 | 409 | static const struct hv_vmbus_device_id hp_devs[] = { |
a119845f | 410 | /* IDE */ |
7fb96565 | 411 | { HV_IDE_GUID, }, |
a119845f | 412 | /* Storage - SCSI */ |
7fb96565 | 413 | { HV_SCSI_GUID, }, |
a119845f | 414 | /* Network */ |
7fb96565 | 415 | { HV_NIC_GUID, }, |
04653a00 S |
416 | /* NetworkDirect Guest RDMA */ |
417 | { HV_ND_GUID, }, | |
a119845f S |
418 | }; |
419 | ||
420 | ||
421 | /* | |
422 | * We use this state to statically distribute the channel interrupt load. | |
423 | */ | |
424 | static u32 next_vp; | |
425 | ||
426 | /* | |
427 | * Starting with Win8, we can statically distribute the incoming | |
428 | * channel interrupt load by binding a channel to VCPU. We | |
429 | * implement here a simple round robin scheme for distributing | |
430 | * the interrupt load. | |
431 | * We will bind channels that are not performance critical to cpu 0 and | |
432 | * performance critical channels (IDE, SCSI and Network) will be uniformly | |
433 | * distributed across all available CPUs. | |
434 | */ | |
f9da455b | 435 | static void init_vp_index(struct vmbus_channel *channel, const uuid_le *type_guid) |
a119845f S |
436 | { |
437 | u32 cur_cpu; | |
438 | int i; | |
439 | bool perf_chn = false; | |
440 | u32 max_cpus = num_online_cpus(); | |
441 | ||
442 | for (i = IDE; i < MAX_PERF_CHN; i++) { | |
7fb96565 | 443 | if (!memcmp(type_guid->b, hp_devs[i].guid, |
a119845f S |
444 | sizeof(uuid_le))) { |
445 | perf_chn = true; | |
446 | break; | |
447 | } | |
448 | } | |
449 | if ((vmbus_proto_version == VERSION_WS2008) || | |
450 | (vmbus_proto_version == VERSION_WIN7) || (!perf_chn)) { | |
451 | /* | |
452 | * Prior to win8, all channel interrupts are | |
453 | * delivered on cpu 0. | |
454 | * Also if the channel is not a performance critical | |
455 | * channel, bind it to cpu 0. | |
456 | */ | |
d3ba720d S |
457 | channel->target_cpu = 0; |
458 | channel->target_vp = 0; | |
459 | return; | |
a119845f S |
460 | } |
461 | cur_cpu = (++next_vp % max_cpus); | |
d3ba720d S |
462 | channel->target_cpu = cur_cpu; |
463 | channel->target_vp = hv_context.vp_index[cur_cpu]; | |
a119845f S |
464 | } |
465 | ||
3e189519 | 466 | /* |
e98cb276 | 467 | * vmbus_onoffer - Handler for channel offers from vmbus in parent partition. |
bd60c33e | 468 | * |
bd60c33e | 469 | */ |
e98cb276 | 470 | static void vmbus_onoffer(struct vmbus_channel_message_header *hdr) |
3e7ee490 | 471 | { |
bd60c33e | 472 | struct vmbus_channel_offer_channel *offer; |
188963ec | 473 | struct vmbus_channel *newchannel; |
3e7ee490 | 474 | |
bd60c33e | 475 | offer = (struct vmbus_channel_offer_channel *)hdr; |
3e7ee490 | 476 | |
454f18a9 | 477 | /* Allocate the channel object and save this offer. */ |
e98cb276 | 478 | newchannel = alloc_channel(); |
188963ec | 479 | if (!newchannel) { |
0a46618d | 480 | pr_err("Unable to allocate channel object\n"); |
3e7ee490 HJ |
481 | return; |
482 | } | |
483 | ||
132368bd S |
484 | /* |
485 | * By default we setup state to enable batched | |
486 | * reading. A specific service can choose to | |
487 | * disable this prior to opening the channel. | |
488 | */ | |
489 | newchannel->batched_reading = true; | |
490 | ||
b3bf60c7 S |
491 | /* |
492 | * Setup state for signalling the host. | |
493 | */ | |
494 | newchannel->sig_event = (struct hv_input_signal_event *) | |
495 | (ALIGN((unsigned long) | |
496 | &newchannel->sig_buf, | |
497 | HV_HYPERCALL_PARAM_ALIGN)); | |
498 | ||
499 | newchannel->sig_event->connectionid.asu32 = 0; | |
500 | newchannel->sig_event->connectionid.u.id = VMBUS_EVENT_CONNECTION_ID; | |
501 | newchannel->sig_event->flag_number = 0; | |
502 | newchannel->sig_event->rsvdz = 0; | |
503 | ||
504 | if (vmbus_proto_version != VERSION_WS2008) { | |
505 | newchannel->is_dedicated_interrupt = | |
506 | (offer->is_dedicated_interrupt != 0); | |
507 | newchannel->sig_event->connectionid.u.id = | |
508 | offer->connection_id; | |
509 | } | |
510 | ||
d3ba720d | 511 | init_vp_index(newchannel, &offer->offer.if_type); |
abbf3b2a | 512 | |
c50f7fb2 | 513 | memcpy(&newchannel->offermsg, offer, |
bd60c33e | 514 | sizeof(struct vmbus_channel_offer_channel)); |
c50f7fb2 HZ |
515 | newchannel->monitor_grp = (u8)offer->monitorid / 32; |
516 | newchannel->monitor_bit = (u8)offer->monitorid % 32; | |
3e7ee490 | 517 | |
4b2f9abe TT |
518 | INIT_WORK(&newchannel->work, vmbus_process_offer); |
519 | queue_work(newchannel->controlwq, &newchannel->work); | |
3e7ee490 HJ |
520 | } |
521 | ||
3e189519 | 522 | /* |
e98cb276 | 523 | * vmbus_onoffer_rescind - Rescind offer handler. |
bd60c33e GKH |
524 | * |
525 | * We queue a work item to process this offer synchronously | |
526 | */ | |
e98cb276 | 527 | static void vmbus_onoffer_rescind(struct vmbus_channel_message_header *hdr) |
3e7ee490 | 528 | { |
bd60c33e | 529 | struct vmbus_channel_rescind_offer *rescind; |
aded7165 | 530 | struct vmbus_channel *channel; |
d7f2fbaf | 531 | unsigned long flags; |
3e7ee490 | 532 | |
bd60c33e | 533 | rescind = (struct vmbus_channel_rescind_offer *)hdr; |
c6977677 | 534 | channel = relid2channel(rescind->child_relid); |
98e08702 HJ |
535 | |
536 | if (channel == NULL) | |
537 | /* Just return here, no channel found */ | |
3e7ee490 | 538 | return; |
3e7ee490 | 539 | |
d7f2fbaf | 540 | spin_lock_irqsave(&channel->lock, flags); |
c3582a2c | 541 | channel->rescind = true; |
d7f2fbaf VK |
542 | /* |
543 | * channel->work.func != vmbus_process_rescind_offer means we are still | |
544 | * processing offer request and the rescind offer processing should be | |
545 | * postponed. It will be done at the very end of vmbus_process_offer() | |
546 | * as rescind flag is being checked there. | |
547 | */ | |
548 | if (channel->work.func == vmbus_process_rescind_offer) | |
549 | /* work is initialized for vmbus_process_rescind_offer() from | |
550 | * vmbus_process_offer() where the channel got created */ | |
551 | queue_work(channel->controlwq, &channel->work); | |
c3582a2c | 552 | |
d7f2fbaf | 553 | spin_unlock_irqrestore(&channel->lock, flags); |
3e7ee490 HJ |
554 | } |
555 | ||
3e189519 | 556 | /* |
e98cb276 HZ |
557 | * vmbus_onoffers_delivered - |
558 | * This is invoked when all offers have been delivered. | |
bd60c33e GKH |
559 | * |
560 | * Nothing to do here. | |
561 | */ | |
e98cb276 | 562 | static void vmbus_onoffers_delivered( |
bd60c33e | 563 | struct vmbus_channel_message_header *hdr) |
3e7ee490 | 564 | { |
3e7ee490 HJ |
565 | } |
566 | ||
3e189519 | 567 | /* |
e98cb276 | 568 | * vmbus_onopen_result - Open result handler. |
bd60c33e GKH |
569 | * |
570 | * This is invoked when we received a response to our channel open request. | |
571 | * Find the matching request, copy the response and signal the requesting | |
572 | * thread. | |
573 | */ | |
e98cb276 | 574 | static void vmbus_onopen_result(struct vmbus_channel_message_header *hdr) |
3e7ee490 | 575 | { |
bd60c33e | 576 | struct vmbus_channel_open_result *result; |
188963ec HZ |
577 | struct vmbus_channel_msginfo *msginfo; |
578 | struct vmbus_channel_message_header *requestheader; | |
579 | struct vmbus_channel_open_channel *openmsg; | |
dd0813b6 | 580 | unsigned long flags; |
3e7ee490 | 581 | |
bd60c33e | 582 | result = (struct vmbus_channel_open_result *)hdr; |
3e7ee490 | 583 | |
bd60c33e GKH |
584 | /* |
585 | * Find the open msg, copy the result and signal/unblock the wait event | |
586 | */ | |
15b2f647 | 587 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
3e7ee490 | 588 | |
ebb61e5f HJ |
589 | list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list, |
590 | msglistentry) { | |
188963ec | 591 | requestheader = |
c50f7fb2 | 592 | (struct vmbus_channel_message_header *)msginfo->msg; |
188963ec | 593 | |
c50f7fb2 | 594 | if (requestheader->msgtype == CHANNELMSG_OPENCHANNEL) { |
188963ec | 595 | openmsg = |
c50f7fb2 HZ |
596 | (struct vmbus_channel_open_channel *)msginfo->msg; |
597 | if (openmsg->child_relid == result->child_relid && | |
598 | openmsg->openid == result->openid) { | |
599 | memcpy(&msginfo->response.open_result, | |
bd60c33e | 600 | result, |
9568a193 S |
601 | sizeof( |
602 | struct vmbus_channel_open_result)); | |
603 | complete(&msginfo->waitevent); | |
3e7ee490 HJ |
604 | break; |
605 | } | |
606 | } | |
607 | } | |
15b2f647 | 608 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
3e7ee490 HJ |
609 | } |
610 | ||
3e189519 | 611 | /* |
e98cb276 | 612 | * vmbus_ongpadl_created - GPADL created handler. |
bd60c33e GKH |
613 | * |
614 | * This is invoked when we received a response to our gpadl create request. | |
615 | * Find the matching request, copy the response and signal the requesting | |
616 | * thread. | |
617 | */ | |
e98cb276 | 618 | static void vmbus_ongpadl_created(struct vmbus_channel_message_header *hdr) |
3e7ee490 | 619 | { |
188963ec | 620 | struct vmbus_channel_gpadl_created *gpadlcreated; |
188963ec HZ |
621 | struct vmbus_channel_msginfo *msginfo; |
622 | struct vmbus_channel_message_header *requestheader; | |
623 | struct vmbus_channel_gpadl_header *gpadlheader; | |
dd0813b6 | 624 | unsigned long flags; |
3e7ee490 | 625 | |
188963ec | 626 | gpadlcreated = (struct vmbus_channel_gpadl_created *)hdr; |
3e7ee490 | 627 | |
bd60c33e GKH |
628 | /* |
629 | * Find the establish msg, copy the result and signal/unblock the wait | |
630 | * event | |
631 | */ | |
15b2f647 | 632 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
3e7ee490 | 633 | |
ebb61e5f HJ |
634 | list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list, |
635 | msglistentry) { | |
188963ec | 636 | requestheader = |
c50f7fb2 | 637 | (struct vmbus_channel_message_header *)msginfo->msg; |
188963ec | 638 | |
c50f7fb2 | 639 | if (requestheader->msgtype == CHANNELMSG_GPADL_HEADER) { |
188963ec HZ |
640 | gpadlheader = |
641 | (struct vmbus_channel_gpadl_header *)requestheader; | |
642 | ||
c50f7fb2 HZ |
643 | if ((gpadlcreated->child_relid == |
644 | gpadlheader->child_relid) && | |
645 | (gpadlcreated->gpadl == gpadlheader->gpadl)) { | |
646 | memcpy(&msginfo->response.gpadl_created, | |
188963ec | 647 | gpadlcreated, |
9568a193 S |
648 | sizeof( |
649 | struct vmbus_channel_gpadl_created)); | |
650 | complete(&msginfo->waitevent); | |
3e7ee490 HJ |
651 | break; |
652 | } | |
653 | } | |
654 | } | |
15b2f647 | 655 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
3e7ee490 HJ |
656 | } |
657 | ||
3e189519 | 658 | /* |
e98cb276 | 659 | * vmbus_ongpadl_torndown - GPADL torndown handler. |
bd60c33e GKH |
660 | * |
661 | * This is invoked when we received a response to our gpadl teardown request. | |
662 | * Find the matching request, copy the response and signal the requesting | |
663 | * thread. | |
664 | */ | |
e98cb276 | 665 | static void vmbus_ongpadl_torndown( |
bd60c33e | 666 | struct vmbus_channel_message_header *hdr) |
3e7ee490 | 667 | { |
188963ec | 668 | struct vmbus_channel_gpadl_torndown *gpadl_torndown; |
188963ec HZ |
669 | struct vmbus_channel_msginfo *msginfo; |
670 | struct vmbus_channel_message_header *requestheader; | |
671 | struct vmbus_channel_gpadl_teardown *gpadl_teardown; | |
dd0813b6 | 672 | unsigned long flags; |
3e7ee490 | 673 | |
188963ec | 674 | gpadl_torndown = (struct vmbus_channel_gpadl_torndown *)hdr; |
bd60c33e GKH |
675 | |
676 | /* | |
677 | * Find the open msg, copy the result and signal/unblock the wait event | |
678 | */ | |
15b2f647 | 679 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
3e7ee490 | 680 | |
ebb61e5f HJ |
681 | list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list, |
682 | msglistentry) { | |
188963ec | 683 | requestheader = |
c50f7fb2 | 684 | (struct vmbus_channel_message_header *)msginfo->msg; |
3e7ee490 | 685 | |
c50f7fb2 | 686 | if (requestheader->msgtype == CHANNELMSG_GPADL_TEARDOWN) { |
188963ec HZ |
687 | gpadl_teardown = |
688 | (struct vmbus_channel_gpadl_teardown *)requestheader; | |
3e7ee490 | 689 | |
c50f7fb2 HZ |
690 | if (gpadl_torndown->gpadl == gpadl_teardown->gpadl) { |
691 | memcpy(&msginfo->response.gpadl_torndown, | |
188963ec | 692 | gpadl_torndown, |
9568a193 S |
693 | sizeof( |
694 | struct vmbus_channel_gpadl_torndown)); | |
695 | complete(&msginfo->waitevent); | |
3e7ee490 HJ |
696 | break; |
697 | } | |
698 | } | |
699 | } | |
15b2f647 | 700 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
3e7ee490 HJ |
701 | } |
702 | ||
3e189519 | 703 | /* |
e98cb276 | 704 | * vmbus_onversion_response - Version response handler |
bd60c33e GKH |
705 | * |
706 | * This is invoked when we received a response to our initiate contact request. | |
707 | * Find the matching request, copy the response and signal the requesting | |
708 | * thread. | |
709 | */ | |
e98cb276 | 710 | static void vmbus_onversion_response( |
bd60c33e | 711 | struct vmbus_channel_message_header *hdr) |
3e7ee490 | 712 | { |
188963ec HZ |
713 | struct vmbus_channel_msginfo *msginfo; |
714 | struct vmbus_channel_message_header *requestheader; | |
188963ec | 715 | struct vmbus_channel_version_response *version_response; |
dd0813b6 | 716 | unsigned long flags; |
3e7ee490 | 717 | |
188963ec | 718 | version_response = (struct vmbus_channel_version_response *)hdr; |
15b2f647 | 719 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
3e7ee490 | 720 | |
ebb61e5f HJ |
721 | list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list, |
722 | msglistentry) { | |
188963ec | 723 | requestheader = |
c50f7fb2 | 724 | (struct vmbus_channel_message_header *)msginfo->msg; |
3e7ee490 | 725 | |
c50f7fb2 HZ |
726 | if (requestheader->msgtype == |
727 | CHANNELMSG_INITIATE_CONTACT) { | |
c50f7fb2 | 728 | memcpy(&msginfo->response.version_response, |
188963ec | 729 | version_response, |
bd60c33e | 730 | sizeof(struct vmbus_channel_version_response)); |
9568a193 | 731 | complete(&msginfo->waitevent); |
3e7ee490 HJ |
732 | } |
733 | } | |
15b2f647 | 734 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
3e7ee490 HJ |
735 | } |
736 | ||
c8212f04 GKH |
737 | /* Channel message dispatch table */ |
738 | static struct vmbus_channel_message_table_entry | |
b7c6b02f | 739 | channel_message_table[CHANNELMSG_COUNT] = { |
c50f7fb2 HZ |
740 | {CHANNELMSG_INVALID, NULL}, |
741 | {CHANNELMSG_OFFERCHANNEL, vmbus_onoffer}, | |
742 | {CHANNELMSG_RESCIND_CHANNELOFFER, vmbus_onoffer_rescind}, | |
743 | {CHANNELMSG_REQUESTOFFERS, NULL}, | |
744 | {CHANNELMSG_ALLOFFERS_DELIVERED, vmbus_onoffers_delivered}, | |
745 | {CHANNELMSG_OPENCHANNEL, NULL}, | |
746 | {CHANNELMSG_OPENCHANNEL_RESULT, vmbus_onopen_result}, | |
747 | {CHANNELMSG_CLOSECHANNEL, NULL}, | |
748 | {CHANNELMSG_GPADL_HEADER, NULL}, | |
749 | {CHANNELMSG_GPADL_BODY, NULL}, | |
750 | {CHANNELMSG_GPADL_CREATED, vmbus_ongpadl_created}, | |
751 | {CHANNELMSG_GPADL_TEARDOWN, NULL}, | |
752 | {CHANNELMSG_GPADL_TORNDOWN, vmbus_ongpadl_torndown}, | |
753 | {CHANNELMSG_RELID_RELEASED, NULL}, | |
754 | {CHANNELMSG_INITIATE_CONTACT, NULL}, | |
755 | {CHANNELMSG_VERSION_RESPONSE, vmbus_onversion_response}, | |
756 | {CHANNELMSG_UNLOAD, NULL}, | |
c8212f04 GKH |
757 | }; |
758 | ||
3e189519 | 759 | /* |
e98cb276 | 760 | * vmbus_onmessage - Handler for channel protocol messages. |
bd60c33e GKH |
761 | * |
762 | * This is invoked in the vmbus worker thread context. | |
763 | */ | |
e98cb276 | 764 | void vmbus_onmessage(void *context) |
3e7ee490 | 765 | { |
188963ec | 766 | struct hv_message *msg = context; |
82250213 | 767 | struct vmbus_channel_message_header *hdr; |
3e7ee490 HJ |
768 | int size; |
769 | ||
f6feebe0 HZ |
770 | hdr = (struct vmbus_channel_message_header *)msg->u.payload; |
771 | size = msg->header.payload_size; | |
3e7ee490 | 772 | |
c50f7fb2 | 773 | if (hdr->msgtype >= CHANNELMSG_COUNT) { |
0a46618d | 774 | pr_err("Received invalid channel message type %d size %d\n", |
c50f7fb2 | 775 | hdr->msgtype, size); |
04f50c4d | 776 | print_hex_dump_bytes("", DUMP_PREFIX_NONE, |
f6feebe0 | 777 | (unsigned char *)msg->u.payload, size); |
3e7ee490 HJ |
778 | return; |
779 | } | |
780 | ||
b7c6b02f S |
781 | if (channel_message_table[hdr->msgtype].message_handler) |
782 | channel_message_table[hdr->msgtype].message_handler(hdr); | |
3e7ee490 | 783 | else |
0a46618d | 784 | pr_err("Unhandled channel message type %d\n", hdr->msgtype); |
3e7ee490 HJ |
785 | } |
786 | ||
3e189519 | 787 | /* |
e98cb276 | 788 | * vmbus_request_offers - Send a request to get all our pending offers. |
bd60c33e | 789 | */ |
e98cb276 | 790 | int vmbus_request_offers(void) |
3e7ee490 | 791 | { |
82250213 | 792 | struct vmbus_channel_message_header *msg; |
188963ec | 793 | struct vmbus_channel_msginfo *msginfo; |
51e5181d NMG |
794 | int ret; |
795 | unsigned long t; | |
3e7ee490 | 796 | |
188963ec | 797 | msginfo = kmalloc(sizeof(*msginfo) + |
bd60c33e GKH |
798 | sizeof(struct vmbus_channel_message_header), |
799 | GFP_KERNEL); | |
188963ec | 800 | if (!msginfo) |
75910f23 | 801 | return -ENOMEM; |
3e7ee490 | 802 | |
9568a193 | 803 | init_completion(&msginfo->waitevent); |
75910f23 | 804 | |
c50f7fb2 | 805 | msg = (struct vmbus_channel_message_header *)msginfo->msg; |
3e7ee490 | 806 | |
c50f7fb2 | 807 | msg->msgtype = CHANNELMSG_REQUESTOFFERS; |
3e7ee490 | 808 | |
3e7ee490 | 809 | |
c6977677 | 810 | ret = vmbus_post_msg(msg, |
bd60c33e GKH |
811 | sizeof(struct vmbus_channel_message_header)); |
812 | if (ret != 0) { | |
0a46618d | 813 | pr_err("Unable to request offers - %d\n", ret); |
3e7ee490 | 814 | |
0c3b7b2f S |
815 | goto cleanup; |
816 | } | |
3e7ee490 | 817 | |
2dfde964 | 818 | t = wait_for_completion_timeout(&msginfo->waitevent, 5*HZ); |
9568a193 | 819 | if (t == 0) { |
0c3b7b2f S |
820 | ret = -ETIMEDOUT; |
821 | goto cleanup; | |
3e7ee490 | 822 | } |
3e7ee490 | 823 | |
3e7ee490 HJ |
824 | |
825 | ||
0c3b7b2f | 826 | cleanup: |
dd9b15dc | 827 | kfree(msginfo); |
3e7ee490 | 828 | |
3e7ee490 HJ |
829 | return ret; |
830 | } | |
831 | ||
e68d2971 S |
832 | /* |
833 | * Retrieve the (sub) channel on which to send an outgoing request. | |
834 | * When a primary channel has multiple sub-channels, we choose a | |
835 | * channel whose VCPU binding is closest to the VCPU on which | |
836 | * this call is being made. | |
837 | */ | |
838 | struct vmbus_channel *vmbus_get_outgoing_channel(struct vmbus_channel *primary) | |
839 | { | |
840 | struct list_head *cur, *tmp; | |
87712bf8 | 841 | int cur_cpu; |
e68d2971 S |
842 | struct vmbus_channel *cur_channel; |
843 | struct vmbus_channel *outgoing_channel = primary; | |
844 | int cpu_distance, new_cpu_distance; | |
845 | ||
846 | if (list_empty(&primary->sc_list)) | |
847 | return outgoing_channel; | |
848 | ||
87712bf8 S |
849 | cur_cpu = hv_context.vp_index[get_cpu()]; |
850 | put_cpu(); | |
e68d2971 S |
851 | list_for_each_safe(cur, tmp, &primary->sc_list) { |
852 | cur_channel = list_entry(cur, struct vmbus_channel, sc_list); | |
853 | if (cur_channel->state != CHANNEL_OPENED_STATE) | |
854 | continue; | |
855 | ||
856 | if (cur_channel->target_vp == cur_cpu) | |
857 | return cur_channel; | |
858 | ||
859 | cpu_distance = ((outgoing_channel->target_vp > cur_cpu) ? | |
860 | (outgoing_channel->target_vp - cur_cpu) : | |
861 | (cur_cpu - outgoing_channel->target_vp)); | |
862 | ||
863 | new_cpu_distance = ((cur_channel->target_vp > cur_cpu) ? | |
864 | (cur_channel->target_vp - cur_cpu) : | |
865 | (cur_cpu - cur_channel->target_vp)); | |
866 | ||
867 | if (cpu_distance < new_cpu_distance) | |
868 | continue; | |
869 | ||
870 | outgoing_channel = cur_channel; | |
871 | } | |
872 | ||
873 | return outgoing_channel; | |
874 | } | |
875 | EXPORT_SYMBOL_GPL(vmbus_get_outgoing_channel); | |
876 | ||
877 | static void invoke_sc_cb(struct vmbus_channel *primary_channel) | |
878 | { | |
879 | struct list_head *cur, *tmp; | |
880 | struct vmbus_channel *cur_channel; | |
881 | ||
882 | if (primary_channel->sc_creation_callback == NULL) | |
883 | return; | |
884 | ||
885 | list_for_each_safe(cur, tmp, &primary_channel->sc_list) { | |
886 | cur_channel = list_entry(cur, struct vmbus_channel, sc_list); | |
887 | ||
888 | primary_channel->sc_creation_callback(cur_channel); | |
889 | } | |
890 | } | |
891 | ||
892 | void vmbus_set_sc_create_callback(struct vmbus_channel *primary_channel, | |
893 | void (*sc_cr_cb)(struct vmbus_channel *new_sc)) | |
894 | { | |
895 | primary_channel->sc_creation_callback = sc_cr_cb; | |
896 | } | |
897 | EXPORT_SYMBOL_GPL(vmbus_set_sc_create_callback); | |
898 | ||
899 | bool vmbus_are_subchannels_present(struct vmbus_channel *primary) | |
900 | { | |
901 | bool ret; | |
902 | ||
903 | ret = !list_empty(&primary->sc_list); | |
904 | ||
905 | if (ret) { | |
906 | /* | |
907 | * Invoke the callback on sub-channel creation. | |
908 | * This will present a uniform interface to the | |
909 | * clients. | |
910 | */ | |
911 | invoke_sc_cb(primary); | |
912 | } | |
913 | ||
914 | return ret; | |
915 | } | |
916 | EXPORT_SYMBOL_GPL(vmbus_are_subchannels_present); |