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