]>
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> |
638fea33 | 24 | #include <linux/interrupt.h> |
0c3b7b2f S |
25 | #include <linux/sched.h> |
26 | #include <linux/wait.h> | |
a0086dc5 | 27 | #include <linux/mm.h> |
5a0e3ad6 | 28 | #include <linux/slab.h> |
53af545b | 29 | #include <linux/list.h> |
c88c4e4c | 30 | #include <linux/module.h> |
8b5d6d3b | 31 | #include <linux/completion.h> |
41571916 | 32 | #include <linux/delay.h> |
46a97191 | 33 | #include <linux/hyperv.h> |
3f335ea2 | 34 | |
0f2a6619 | 35 | #include "hyperv_vmbus.h" |
3e7ee490 | 36 | |
7047f17d S |
37 | static void init_vp_index(struct vmbus_channel *channel, u16 dev_type); |
38 | ||
39 | static const struct vmbus_device vmbus_devs[] = { | |
40 | /* IDE */ | |
41 | { .dev_type = HV_IDE, | |
42 | HV_IDE_GUID, | |
43 | .perf_device = true, | |
44 | }, | |
45 | ||
46 | /* SCSI */ | |
47 | { .dev_type = HV_SCSI, | |
48 | HV_SCSI_GUID, | |
49 | .perf_device = true, | |
50 | }, | |
51 | ||
52 | /* Fibre Channel */ | |
53 | { .dev_type = HV_FC, | |
54 | HV_SYNTHFC_GUID, | |
55 | .perf_device = true, | |
56 | }, | |
57 | ||
58 | /* Synthetic NIC */ | |
59 | { .dev_type = HV_NIC, | |
60 | HV_NIC_GUID, | |
61 | .perf_device = true, | |
62 | }, | |
63 | ||
64 | /* Network Direct */ | |
65 | { .dev_type = HV_ND, | |
66 | HV_ND_GUID, | |
67 | .perf_device = true, | |
68 | }, | |
69 | ||
70 | /* PCIE */ | |
71 | { .dev_type = HV_PCIE, | |
72 | HV_PCIE_GUID, | |
73 | .perf_device = true, | |
74 | }, | |
75 | ||
76 | /* Synthetic Frame Buffer */ | |
77 | { .dev_type = HV_FB, | |
78 | HV_SYNTHVID_GUID, | |
79 | .perf_device = false, | |
80 | }, | |
81 | ||
82 | /* Synthetic Keyboard */ | |
83 | { .dev_type = HV_KBD, | |
84 | HV_KBD_GUID, | |
85 | .perf_device = false, | |
86 | }, | |
87 | ||
88 | /* Synthetic MOUSE */ | |
89 | { .dev_type = HV_MOUSE, | |
90 | HV_MOUSE_GUID, | |
91 | .perf_device = false, | |
92 | }, | |
93 | ||
94 | /* KVP */ | |
95 | { .dev_type = HV_KVP, | |
96 | HV_KVP_GUID, | |
97 | .perf_device = false, | |
98 | }, | |
99 | ||
100 | /* Time Synch */ | |
101 | { .dev_type = HV_TS, | |
102 | HV_TS_GUID, | |
103 | .perf_device = false, | |
104 | }, | |
105 | ||
106 | /* Heartbeat */ | |
107 | { .dev_type = HV_HB, | |
108 | HV_HEART_BEAT_GUID, | |
109 | .perf_device = false, | |
110 | }, | |
111 | ||
112 | /* Shutdown */ | |
113 | { .dev_type = HV_SHUTDOWN, | |
114 | HV_SHUTDOWN_GUID, | |
115 | .perf_device = false, | |
116 | }, | |
117 | ||
118 | /* File copy */ | |
119 | { .dev_type = HV_FCOPY, | |
120 | HV_FCOPY_GUID, | |
121 | .perf_device = false, | |
122 | }, | |
123 | ||
124 | /* Backup */ | |
125 | { .dev_type = HV_BACKUP, | |
126 | HV_VSS_GUID, | |
127 | .perf_device = false, | |
128 | }, | |
129 | ||
130 | /* Dynamic Memory */ | |
131 | { .dev_type = HV_DM, | |
132 | HV_DM_GUID, | |
133 | .perf_device = false, | |
134 | }, | |
135 | ||
136 | /* Unknown GUID */ | |
137 | { .dev_type = HV_UNKOWN, | |
138 | .perf_device = false, | |
139 | }, | |
140 | }; | |
141 | ||
142 | static u16 hv_get_dev_type(const uuid_le *guid) | |
143 | { | |
144 | u16 i; | |
145 | ||
146 | for (i = HV_IDE; i < HV_UNKOWN; i++) { | |
147 | if (!uuid_le_cmp(*guid, vmbus_devs[i].guid)) | |
148 | return i; | |
149 | } | |
150 | pr_info("Unknown GUID: %pUl\n", guid); | |
151 | return i; | |
152 | } | |
f38e7dd7 | 153 | |
c88c4e4c | 154 | /** |
da0e9631 | 155 | * vmbus_prep_negotiate_resp() - Create default response for Hyper-V Negotiate message |
c88c4e4c HJ |
156 | * @icmsghdrp: Pointer to msg header structure |
157 | * @icmsg_negotiate: Pointer to negotiate message structure | |
158 | * @buf: Raw buffer channel data | |
159 | * | |
160 | * @icmsghdrp is of type &struct icmsg_hdr. | |
161 | * @negop is of type &struct icmsg_negotiate. | |
c836d0ab S |
162 | * Set up and fill in default negotiate response message. |
163 | * | |
6741335b S |
164 | * The fw_version specifies the framework version that |
165 | * we can support and srv_version specifies the service | |
166 | * version we can support. | |
c88c4e4c HJ |
167 | * |
168 | * Mainly used by Hyper-V drivers. | |
169 | */ | |
6741335b | 170 | bool vmbus_prep_negotiate_resp(struct icmsg_hdr *icmsghdrp, |
c836d0ab | 171 | struct icmsg_negotiate *negop, u8 *buf, |
6741335b | 172 | int fw_version, int srv_version) |
c88c4e4c | 173 | { |
6741335b S |
174 | int icframe_major, icframe_minor; |
175 | int icmsg_major, icmsg_minor; | |
176 | int fw_major, fw_minor; | |
177 | int srv_major, srv_minor; | |
c836d0ab | 178 | int i; |
6741335b | 179 | bool found_match = false; |
c836d0ab | 180 | |
a3605300 | 181 | icmsghdrp->icmsgsize = 0x10; |
6741335b S |
182 | fw_major = (fw_version >> 16); |
183 | fw_minor = (fw_version & 0xFFFF); | |
184 | ||
185 | srv_major = (srv_version >> 16); | |
186 | srv_minor = (srv_version & 0xFFFF); | |
c88c4e4c | 187 | |
a3605300 S |
188 | negop = (struct icmsg_negotiate *)&buf[ |
189 | sizeof(struct vmbuspipe_hdr) + | |
190 | sizeof(struct icmsg_hdr)]; | |
c88c4e4c | 191 | |
6741335b S |
192 | icframe_major = negop->icframe_vercnt; |
193 | icframe_minor = 0; | |
194 | ||
195 | icmsg_major = negop->icmsg_vercnt; | |
196 | icmsg_minor = 0; | |
c836d0ab S |
197 | |
198 | /* | |
199 | * Select the framework version number we will | |
200 | * support. | |
201 | */ | |
202 | ||
203 | for (i = 0; i < negop->icframe_vercnt; i++) { | |
6741335b S |
204 | if ((negop->icversion_data[i].major == fw_major) && |
205 | (negop->icversion_data[i].minor == fw_minor)) { | |
206 | icframe_major = negop->icversion_data[i].major; | |
207 | icframe_minor = negop->icversion_data[i].minor; | |
208 | found_match = true; | |
209 | } | |
c836d0ab S |
210 | } |
211 | ||
6741335b S |
212 | if (!found_match) |
213 | goto fw_error; | |
214 | ||
215 | found_match = false; | |
216 | ||
c836d0ab S |
217 | for (i = negop->icframe_vercnt; |
218 | (i < negop->icframe_vercnt + negop->icmsg_vercnt); i++) { | |
6741335b S |
219 | if ((negop->icversion_data[i].major == srv_major) && |
220 | (negop->icversion_data[i].minor == srv_minor)) { | |
221 | icmsg_major = negop->icversion_data[i].major; | |
222 | icmsg_minor = negop->icversion_data[i].minor; | |
223 | found_match = true; | |
224 | } | |
c88c4e4c | 225 | } |
a3605300 | 226 | |
c836d0ab | 227 | /* |
6741335b | 228 | * Respond with the framework and service |
c836d0ab S |
229 | * version numbers we can support. |
230 | */ | |
6741335b S |
231 | |
232 | fw_error: | |
233 | if (!found_match) { | |
234 | negop->icframe_vercnt = 0; | |
235 | negop->icmsg_vercnt = 0; | |
236 | } else { | |
237 | negop->icframe_vercnt = 1; | |
238 | negop->icmsg_vercnt = 1; | |
239 | } | |
240 | ||
241 | negop->icversion_data[0].major = icframe_major; | |
242 | negop->icversion_data[0].minor = icframe_minor; | |
243 | negop->icversion_data[1].major = icmsg_major; | |
244 | negop->icversion_data[1].minor = icmsg_minor; | |
245 | return found_match; | |
c88c4e4c | 246 | } |
a3605300 | 247 | |
da0e9631 | 248 | EXPORT_SYMBOL_GPL(vmbus_prep_negotiate_resp); |
c88c4e4c | 249 | |
3e189519 | 250 | /* |
e98cb276 | 251 | * alloc_channel - Allocate and initialize a vmbus channel object |
bd60c33e | 252 | */ |
50fe56d2 | 253 | static struct vmbus_channel *alloc_channel(void) |
3e7ee490 | 254 | { |
bc63b6f6 | 255 | static atomic_t chan_num = ATOMIC_INIT(0); |
aded7165 | 256 | struct vmbus_channel *channel; |
3e7ee490 | 257 | |
aded7165 | 258 | channel = kzalloc(sizeof(*channel), GFP_ATOMIC); |
3e7ee490 | 259 | if (!channel) |
3e7ee490 | 260 | return NULL; |
3e7ee490 | 261 | |
bc63b6f6 | 262 | channel->id = atomic_inc_return(&chan_num); |
fe760e4d | 263 | channel->acquire_ring_lock = true; |
54411c42 | 264 | spin_lock_init(&channel->inbound_lock); |
67fae053 | 265 | spin_lock_init(&channel->lock); |
e68d2971 S |
266 | |
267 | INIT_LIST_HEAD(&channel->sc_list); | |
3a28fa35 | 268 | INIT_LIST_HEAD(&channel->percpu_list); |
3e7ee490 | 269 | |
3e7ee490 HJ |
270 | return channel; |
271 | } | |
272 | ||
3e189519 | 273 | /* |
e98cb276 | 274 | * free_channel - Release the resources used by the vmbus channel object |
bd60c33e | 275 | */ |
9f3e28e3 | 276 | static void free_channel(struct vmbus_channel *channel) |
3e7ee490 | 277 | { |
aadc3780 | 278 | kfree(channel); |
3e7ee490 HJ |
279 | } |
280 | ||
3a28fa35 S |
281 | static void percpu_channel_enq(void *arg) |
282 | { | |
283 | struct vmbus_channel *channel = arg; | |
284 | int cpu = smp_processor_id(); | |
285 | ||
286 | list_add_tail(&channel->percpu_list, &hv_context.percpu_list[cpu]); | |
287 | } | |
8b5d6d3b | 288 | |
3a28fa35 S |
289 | static void percpu_channel_deq(void *arg) |
290 | { | |
291 | struct vmbus_channel *channel = arg; | |
292 | ||
293 | list_del(&channel->percpu_list); | |
294 | } | |
8b5d6d3b | 295 | |
ed6cfcc5 | 296 | |
f52078cf | 297 | static void vmbus_release_relid(u32 relid) |
4b2f9abe | 298 | { |
ed6cfcc5 | 299 | struct vmbus_channel_relid_released msg; |
4b2f9abe | 300 | |
c8705979 | 301 | memset(&msg, 0, sizeof(struct vmbus_channel_relid_released)); |
ed6cfcc5 | 302 | msg.child_relid = relid; |
c8705979 S |
303 | msg.header.msgtype = CHANNELMSG_RELID_RELEASED; |
304 | vmbus_post_msg(&msg, sizeof(struct vmbus_channel_relid_released)); | |
f52078cf | 305 | } |
c8705979 | 306 | |
638fea33 DC |
307 | void hv_event_tasklet_disable(struct vmbus_channel *channel) |
308 | { | |
309 | struct tasklet_struct *tasklet; | |
310 | tasklet = hv_context.event_dpc[channel->target_cpu]; | |
311 | tasklet_disable(tasklet); | |
312 | } | |
313 | ||
314 | void hv_event_tasklet_enable(struct vmbus_channel *channel) | |
315 | { | |
316 | struct tasklet_struct *tasklet; | |
317 | tasklet = hv_context.event_dpc[channel->target_cpu]; | |
318 | tasklet_enable(tasklet); | |
319 | ||
320 | /* In case there is any pending event */ | |
321 | tasklet_schedule(tasklet); | |
322 | } | |
323 | ||
f52078cf DC |
324 | void hv_process_channel_removal(struct vmbus_channel *channel, u32 relid) |
325 | { | |
326 | unsigned long flags; | |
327 | struct vmbus_channel *primary_channel; | |
328 | ||
34c6801e | 329 | BUG_ON(!channel->rescind); |
85d9aa70 | 330 | BUG_ON(!mutex_is_locked(&vmbus_connection.channel_mutex)); |
34c6801e | 331 | |
638fea33 | 332 | hv_event_tasklet_disable(channel); |
2115b561 S |
333 | if (channel->target_cpu != get_cpu()) { |
334 | put_cpu(); | |
3a28fa35 S |
335 | smp_call_function_single(channel->target_cpu, |
336 | percpu_channel_deq, channel, true); | |
2115b561 | 337 | } else { |
3a28fa35 | 338 | percpu_channel_deq(channel); |
2115b561 S |
339 | put_cpu(); |
340 | } | |
638fea33 | 341 | hv_event_tasklet_enable(channel); |
3a28fa35 | 342 | |
e68d2971 | 343 | if (channel->primary_channel == NULL) { |
e68d2971 | 344 | list_del(&channel->listentry); |
ca1c4b74 DC |
345 | |
346 | primary_channel = channel; | |
e68d2971 S |
347 | } else { |
348 | primary_channel = channel->primary_channel; | |
67fae053 | 349 | spin_lock_irqsave(&primary_channel->lock, flags); |
565ce642 | 350 | list_del(&channel->sc_list); |
357e836a | 351 | primary_channel->num_sc--; |
67fae053 | 352 | spin_unlock_irqrestore(&primary_channel->lock, flags); |
e68d2971 | 353 | } |
ca1c4b74 DC |
354 | |
355 | /* | |
356 | * We need to free the bit for init_vp_index() to work in the case | |
357 | * of sub-channel, when we reload drivers like hv_netvsc. | |
358 | */ | |
359 | cpumask_clear_cpu(channel->target_cpu, | |
360 | &primary_channel->alloced_cpus_in_node); | |
361 | ||
638fea33 DC |
362 | vmbus_release_relid(relid); |
363 | ||
c8705979 | 364 | free_channel(channel); |
4b2f9abe | 365 | } |
8b5d6d3b | 366 | |
93e5bd06 S |
367 | void vmbus_free_channels(void) |
368 | { | |
813c5b79 DC |
369 | struct vmbus_channel *channel, *tmp; |
370 | ||
371 | list_for_each_entry_safe(channel, tmp, &vmbus_connection.chn_list, | |
372 | listentry) { | |
34c6801e | 373 | /* hv_process_channel_removal() needs this */ |
813c5b79 | 374 | channel->rescind = true; |
93e5bd06 | 375 | |
93e5bd06 | 376 | vmbus_device_unregister(channel->device_obj); |
93e5bd06 S |
377 | } |
378 | } | |
379 | ||
3e189519 | 380 | /* |
e98cb276 | 381 | * vmbus_process_offer - Process the offer by creating a channel/device |
c88c4e4c | 382 | * associated with this offer |
bd60c33e | 383 | */ |
2dd37cb8 | 384 | static void vmbus_process_offer(struct vmbus_channel *newchannel) |
3e7ee490 | 385 | { |
aded7165 | 386 | struct vmbus_channel *channel; |
188963ec | 387 | bool fnew = true; |
0f5e44ca | 388 | unsigned long flags; |
7047f17d | 389 | u16 dev_type; |
85d9aa70 | 390 | int ret; |
3e7ee490 | 391 | |
454f18a9 | 392 | /* Make sure this is a new offer */ |
d6f591e3 | 393 | mutex_lock(&vmbus_connection.channel_mutex); |
3e7ee490 | 394 | |
da9fcb72 | 395 | list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) { |
358d2ee2 S |
396 | if (!uuid_le_cmp(channel->offermsg.offer.if_type, |
397 | newchannel->offermsg.offer.if_type) && | |
398 | !uuid_le_cmp(channel->offermsg.offer.if_instance, | |
399 | newchannel->offermsg.offer.if_instance)) { | |
188963ec | 400 | fnew = false; |
3e7ee490 HJ |
401 | break; |
402 | } | |
403 | } | |
404 | ||
8dfd3326 | 405 | if (fnew) |
c50f7fb2 | 406 | list_add_tail(&newchannel->listentry, |
da9fcb72 | 407 | &vmbus_connection.chn_list); |
bd60c33e | 408 | |
d6f591e3 | 409 | mutex_unlock(&vmbus_connection.channel_mutex); |
3e7ee490 | 410 | |
188963ec | 411 | if (!fnew) { |
e68d2971 S |
412 | /* |
413 | * Check to see if this is a sub-channel. | |
414 | */ | |
415 | if (newchannel->offermsg.offer.sub_channel_index != 0) { | |
416 | /* | |
417 | * Process the sub-channel. | |
418 | */ | |
419 | newchannel->primary_channel = channel; | |
67fae053 | 420 | spin_lock_irqsave(&channel->lock, flags); |
e68d2971 | 421 | list_add_tail(&newchannel->sc_list, &channel->sc_list); |
a13e8bbe | 422 | channel->num_sc++; |
357e836a | 423 | spin_unlock_irqrestore(&channel->lock, flags); |
8dfd3326 VK |
424 | } else |
425 | goto err_free_chan; | |
426 | } | |
e68d2971 | 427 | |
7047f17d | 428 | dev_type = hv_get_dev_type(&newchannel->offermsg.offer.if_type); |
ccef9bcc S |
429 | if (dev_type == HV_NIC) |
430 | set_channel_signal_state(newchannel, HV_SIGNAL_POLICY_EXPLICIT); | |
7047f17d S |
431 | |
432 | init_vp_index(newchannel, dev_type); | |
f38e7dd7 | 433 | |
638fea33 | 434 | hv_event_tasklet_disable(newchannel); |
8dfd3326 VK |
435 | if (newchannel->target_cpu != get_cpu()) { |
436 | put_cpu(); | |
437 | smp_call_function_single(newchannel->target_cpu, | |
438 | percpu_channel_enq, | |
439 | newchannel, true); | |
440 | } else { | |
441 | percpu_channel_enq(newchannel); | |
442 | put_cpu(); | |
3e7ee490 | 443 | } |
638fea33 | 444 | hv_event_tasklet_enable(newchannel); |
3e7ee490 | 445 | |
42dceebe S |
446 | /* |
447 | * This state is used to indicate a successful open | |
448 | * so that when we do close the channel normally, we | |
449 | * can cleanup properly | |
450 | */ | |
451 | newchannel->state = CHANNEL_OPEN_STATE; | |
452 | ||
8dfd3326 VK |
453 | if (!fnew) { |
454 | if (channel->sc_creation_callback != NULL) | |
455 | channel->sc_creation_callback(newchannel); | |
456 | return; | |
457 | } | |
458 | ||
bd60c33e GKH |
459 | /* |
460 | * Start the process of binding this offer to the driver | |
461 | * We need to set the DeviceObject field before calling | |
646f1ea3 | 462 | * vmbus_child_dev_add() |
bd60c33e | 463 | */ |
f2c73011 | 464 | newchannel->device_obj = vmbus_device_create( |
767dff68 HZ |
465 | &newchannel->offermsg.offer.if_type, |
466 | &newchannel->offermsg.offer.if_instance, | |
188963ec | 467 | newchannel); |
9c3a6f7e | 468 | if (!newchannel->device_obj) |
5b1e5b53 | 469 | goto err_deq_chan; |
3e7ee490 | 470 | |
7047f17d | 471 | newchannel->device_obj->device_id = dev_type; |
454f18a9 BP |
472 | /* |
473 | * Add the new device to the bus. This will kick off device-driver | |
474 | * binding which eventually invokes the device driver's AddDevice() | |
475 | * method. | |
476 | */ | |
85d9aa70 DC |
477 | mutex_lock(&vmbus_connection.channel_mutex); |
478 | ret = vmbus_device_register(newchannel->device_obj); | |
479 | mutex_unlock(&vmbus_connection.channel_mutex); | |
480 | ||
481 | if (ret != 0) { | |
d43e2fe7 DC |
482 | pr_err("unable to add child device object (relid %d)\n", |
483 | newchannel->offermsg.child_relid); | |
484 | kfree(newchannel->device_obj); | |
485 | goto err_deq_chan; | |
486 | } | |
9c3a6f7e | 487 | return; |
2dd37cb8 | 488 | |
5b1e5b53 | 489 | err_deq_chan: |
d6f591e3 | 490 | mutex_lock(&vmbus_connection.channel_mutex); |
5b1e5b53 | 491 | list_del(&newchannel->listentry); |
d6f591e3 | 492 | mutex_unlock(&vmbus_connection.channel_mutex); |
5b1e5b53 | 493 | |
638fea33 | 494 | hv_event_tasklet_disable(newchannel); |
5b1e5b53 S |
495 | if (newchannel->target_cpu != get_cpu()) { |
496 | put_cpu(); | |
497 | smp_call_function_single(newchannel->target_cpu, | |
498 | percpu_channel_deq, newchannel, true); | |
499 | } else { | |
500 | percpu_channel_deq(newchannel); | |
501 | put_cpu(); | |
502 | } | |
638fea33 DC |
503 | hv_event_tasklet_enable(newchannel); |
504 | ||
505 | vmbus_release_relid(newchannel->offermsg.child_relid); | |
5b1e5b53 | 506 | |
9c3a6f7e VK |
507 | err_free_chan: |
508 | free_channel(newchannel); | |
3e7ee490 HJ |
509 | } |
510 | ||
a119845f S |
511 | /* |
512 | * We use this state to statically distribute the channel interrupt load. | |
513 | */ | |
1f656ff3 | 514 | static int next_numa_node_id; |
a119845f S |
515 | |
516 | /* | |
517 | * Starting with Win8, we can statically distribute the incoming | |
1f656ff3 S |
518 | * channel interrupt load by binding a channel to VCPU. |
519 | * We do this in a hierarchical fashion: | |
520 | * First distribute the primary channels across available NUMA nodes | |
521 | * and then distribute the subchannels amongst the CPUs in the NUMA | |
522 | * node assigned to the primary channel. | |
523 | * | |
524 | * For pre-win8 hosts or non-performance critical channels we assign the | |
525 | * first CPU in the first NUMA node. | |
a119845f | 526 | */ |
7047f17d | 527 | static void init_vp_index(struct vmbus_channel *channel, u16 dev_type) |
a119845f S |
528 | { |
529 | u32 cur_cpu; | |
7047f17d | 530 | bool perf_chn = vmbus_devs[dev_type].perf_device; |
1f656ff3 S |
531 | struct vmbus_channel *primary = channel->primary_channel; |
532 | int next_node; | |
533 | struct cpumask available_mask; | |
9f01ec53 | 534 | struct cpumask *alloced_mask; |
a119845f | 535 | |
a119845f S |
536 | if ((vmbus_proto_version == VERSION_WS2008) || |
537 | (vmbus_proto_version == VERSION_WIN7) || (!perf_chn)) { | |
538 | /* | |
539 | * Prior to win8, all channel interrupts are | |
540 | * delivered on cpu 0. | |
541 | * Also if the channel is not a performance critical | |
542 | * channel, bind it to cpu 0. | |
543 | */ | |
1f656ff3 | 544 | channel->numa_node = 0; |
d3ba720d | 545 | channel->target_cpu = 0; |
9c6e64ad | 546 | channel->target_vp = hv_context.vp_index[0]; |
d3ba720d | 547 | return; |
a119845f | 548 | } |
ce59fec8 VK |
549 | |
550 | /* | |
1f656ff3 S |
551 | * We distribute primary channels evenly across all the available |
552 | * NUMA nodes and within the assigned NUMA node we will assign the | |
553 | * first available CPU to the primary channel. | |
554 | * The sub-channels will be assigned to the CPUs available in the | |
555 | * NUMA node evenly. | |
ce59fec8 | 556 | */ |
1f656ff3 S |
557 | if (!primary) { |
558 | while (true) { | |
559 | next_node = next_numa_node_id++; | |
560 | if (next_node == nr_node_ids) | |
561 | next_node = next_numa_node_id = 0; | |
562 | if (cpumask_empty(cpumask_of_node(next_node))) | |
563 | continue; | |
564 | break; | |
565 | } | |
566 | channel->numa_node = next_node; | |
567 | primary = channel; | |
568 | } | |
9f01ec53 | 569 | alloced_mask = &hv_context.hv_numa_map[primary->numa_node]; |
1f656ff3 | 570 | |
9f01ec53 | 571 | if (cpumask_weight(alloced_mask) == |
1f656ff3 | 572 | cpumask_weight(cpumask_of_node(primary->numa_node))) { |
ce59fec8 | 573 | /* |
1f656ff3 S |
574 | * We have cycled through all the CPUs in the node; |
575 | * reset the alloced map. | |
ce59fec8 | 576 | */ |
9f01ec53 | 577 | cpumask_clear(alloced_mask); |
ce59fec8 VK |
578 | } |
579 | ||
9f01ec53 | 580 | cpumask_xor(&available_mask, alloced_mask, |
1f656ff3 S |
581 | cpumask_of_node(primary->numa_node)); |
582 | ||
3b71107d | 583 | cur_cpu = -1; |
79fd8e70 VK |
584 | |
585 | /* | |
586 | * Normally Hyper-V host doesn't create more subchannels than there | |
587 | * are VCPUs on the node but it is possible when not all present VCPUs | |
588 | * on the node are initialized by guest. Clear the alloced_cpus_in_node | |
589 | * to start over. | |
590 | */ | |
591 | if (cpumask_equal(&primary->alloced_cpus_in_node, | |
592 | cpumask_of_node(primary->numa_node))) | |
593 | cpumask_clear(&primary->alloced_cpus_in_node); | |
594 | ||
3b71107d DC |
595 | while (true) { |
596 | cur_cpu = cpumask_next(cur_cpu, &available_mask); | |
597 | if (cur_cpu >= nr_cpu_ids) { | |
598 | cur_cpu = -1; | |
599 | cpumask_copy(&available_mask, | |
600 | cpumask_of_node(primary->numa_node)); | |
601 | continue; | |
602 | } | |
603 | ||
ca1c4b74 DC |
604 | /* |
605 | * NOTE: in the case of sub-channel, we clear the sub-channel | |
606 | * related bit(s) in primary->alloced_cpus_in_node in | |
607 | * hv_process_channel_removal(), so when we reload drivers | |
608 | * like hv_netvsc in SMP guest, here we're able to re-allocate | |
609 | * bit from primary->alloced_cpus_in_node. | |
610 | */ | |
3b71107d DC |
611 | if (!cpumask_test_cpu(cur_cpu, |
612 | &primary->alloced_cpus_in_node)) { | |
613 | cpumask_set_cpu(cur_cpu, | |
614 | &primary->alloced_cpus_in_node); | |
615 | cpumask_set_cpu(cur_cpu, alloced_mask); | |
616 | break; | |
617 | } | |
618 | } | |
1f656ff3 | 619 | |
d3ba720d S |
620 | channel->target_cpu = cur_cpu; |
621 | channel->target_vp = hv_context.vp_index[cur_cpu]; | |
a119845f S |
622 | } |
623 | ||
41571916 VK |
624 | static void vmbus_wait_for_unload(void) |
625 | { | |
cd95aad5 VK |
626 | int cpu; |
627 | void *page_addr; | |
628 | struct hv_message *msg; | |
41571916 | 629 | struct vmbus_channel_message_header *hdr; |
cd95aad5 | 630 | u32 message_type; |
41571916 | 631 | |
cd95aad5 VK |
632 | /* |
633 | * CHANNELMSG_UNLOAD_RESPONSE is always delivered to the CPU which was | |
634 | * used for initial contact or to CPU0 depending on host version. When | |
635 | * we're crashing on a different CPU let's hope that IRQ handler on | |
636 | * the cpu which receives CHANNELMSG_UNLOAD_RESPONSE is still | |
637 | * functional and vmbus_unload_response() will complete | |
638 | * vmbus_connection.unload_event. If not, the last thing we can do is | |
639 | * read message pages for all CPUs directly. | |
640 | */ | |
41571916 | 641 | while (1) { |
cd95aad5 VK |
642 | if (completion_done(&vmbus_connection.unload_event)) |
643 | break; | |
41571916 | 644 | |
cd95aad5 VK |
645 | for_each_online_cpu(cpu) { |
646 | page_addr = hv_context.synic_message_page[cpu]; | |
647 | msg = (struct hv_message *)page_addr + | |
648 | VMBUS_MESSAGE_SINT; | |
41571916 | 649 | |
cd95aad5 VK |
650 | message_type = READ_ONCE(msg->header.message_type); |
651 | if (message_type == HVMSG_NONE) | |
652 | continue; | |
41571916 | 653 | |
cd95aad5 VK |
654 | hdr = (struct vmbus_channel_message_header *) |
655 | msg->u.payload; | |
656 | ||
657 | if (hdr->msgtype == CHANNELMSG_UNLOAD_RESPONSE) | |
658 | complete(&vmbus_connection.unload_event); | |
659 | ||
660 | vmbus_signal_eom(msg, message_type); | |
661 | } | |
662 | ||
663 | mdelay(10); | |
664 | } | |
665 | ||
666 | /* | |
667 | * We're crashing and already got the UNLOAD_RESPONSE, cleanup all | |
668 | * maybe-pending messages on all CPUs to be able to receive new | |
669 | * messages after we reconnect. | |
670 | */ | |
671 | for_each_online_cpu(cpu) { | |
672 | page_addr = hv_context.synic_message_page[cpu]; | |
673 | msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT; | |
674 | msg->header.message_type = HVMSG_NONE; | |
41571916 VK |
675 | } |
676 | } | |
677 | ||
2db84eff S |
678 | /* |
679 | * vmbus_unload_response - Handler for the unload response. | |
680 | */ | |
681 | static void vmbus_unload_response(struct vmbus_channel_message_header *hdr) | |
682 | { | |
683 | /* | |
684 | * This is a global event; just wakeup the waiting thread. | |
685 | * Once we successfully unload, we can cleanup the monitor state. | |
686 | */ | |
687 | complete(&vmbus_connection.unload_event); | |
688 | } | |
689 | ||
75ff3a8a | 690 | void vmbus_initiate_unload(bool crash) |
2db84eff S |
691 | { |
692 | struct vmbus_channel_message_header hdr; | |
693 | ||
4a54243f VK |
694 | /* Pre-Win2012R2 hosts don't support reconnect */ |
695 | if (vmbus_proto_version < VERSION_WIN8_1) | |
696 | return; | |
697 | ||
2db84eff S |
698 | init_completion(&vmbus_connection.unload_event); |
699 | memset(&hdr, 0, sizeof(struct vmbus_channel_message_header)); | |
700 | hdr.msgtype = CHANNELMSG_UNLOAD; | |
701 | vmbus_post_msg(&hdr, sizeof(struct vmbus_channel_message_header)); | |
702 | ||
41571916 VK |
703 | /* |
704 | * vmbus_initiate_unload() is also called on crash and the crash can be | |
705 | * happening in an interrupt context, where scheduling is impossible. | |
706 | */ | |
75ff3a8a | 707 | if (!crash) |
41571916 VK |
708 | wait_for_completion(&vmbus_connection.unload_event); |
709 | else | |
710 | vmbus_wait_for_unload(); | |
2db84eff S |
711 | } |
712 | ||
3e189519 | 713 | /* |
e98cb276 | 714 | * vmbus_onoffer - Handler for channel offers from vmbus in parent partition. |
bd60c33e | 715 | * |
bd60c33e | 716 | */ |
e98cb276 | 717 | static void vmbus_onoffer(struct vmbus_channel_message_header *hdr) |
3e7ee490 | 718 | { |
bd60c33e | 719 | struct vmbus_channel_offer_channel *offer; |
188963ec | 720 | struct vmbus_channel *newchannel; |
3e7ee490 | 721 | |
bd60c33e | 722 | offer = (struct vmbus_channel_offer_channel *)hdr; |
3e7ee490 | 723 | |
454f18a9 | 724 | /* Allocate the channel object and save this offer. */ |
e98cb276 | 725 | newchannel = alloc_channel(); |
188963ec | 726 | if (!newchannel) { |
0a46618d | 727 | pr_err("Unable to allocate channel object\n"); |
3e7ee490 HJ |
728 | return; |
729 | } | |
730 | ||
132368bd S |
731 | /* |
732 | * By default we setup state to enable batched | |
733 | * reading. A specific service can choose to | |
734 | * disable this prior to opening the channel. | |
735 | */ | |
736 | newchannel->batched_reading = true; | |
737 | ||
b3bf60c7 S |
738 | /* |
739 | * Setup state for signalling the host. | |
740 | */ | |
741 | newchannel->sig_event = (struct hv_input_signal_event *) | |
742 | (ALIGN((unsigned long) | |
743 | &newchannel->sig_buf, | |
744 | HV_HYPERCALL_PARAM_ALIGN)); | |
745 | ||
746 | newchannel->sig_event->connectionid.asu32 = 0; | |
747 | newchannel->sig_event->connectionid.u.id = VMBUS_EVENT_CONNECTION_ID; | |
748 | newchannel->sig_event->flag_number = 0; | |
749 | newchannel->sig_event->rsvdz = 0; | |
750 | ||
751 | if (vmbus_proto_version != VERSION_WS2008) { | |
752 | newchannel->is_dedicated_interrupt = | |
753 | (offer->is_dedicated_interrupt != 0); | |
754 | newchannel->sig_event->connectionid.u.id = | |
755 | offer->connection_id; | |
756 | } | |
757 | ||
c50f7fb2 | 758 | memcpy(&newchannel->offermsg, offer, |
bd60c33e | 759 | sizeof(struct vmbus_channel_offer_channel)); |
c50f7fb2 HZ |
760 | newchannel->monitor_grp = (u8)offer->monitorid / 32; |
761 | newchannel->monitor_bit = (u8)offer->monitorid % 32; | |
3e7ee490 | 762 | |
2dd37cb8 | 763 | vmbus_process_offer(newchannel); |
3e7ee490 HJ |
764 | } |
765 | ||
3e189519 | 766 | /* |
e98cb276 | 767 | * vmbus_onoffer_rescind - Rescind offer handler. |
bd60c33e GKH |
768 | * |
769 | * We queue a work item to process this offer synchronously | |
770 | */ | |
e98cb276 | 771 | static void vmbus_onoffer_rescind(struct vmbus_channel_message_header *hdr) |
3e7ee490 | 772 | { |
bd60c33e | 773 | struct vmbus_channel_rescind_offer *rescind; |
aded7165 | 774 | struct vmbus_channel *channel; |
d43e2fe7 DC |
775 | unsigned long flags; |
776 | struct device *dev; | |
3e7ee490 | 777 | |
bd60c33e | 778 | rescind = (struct vmbus_channel_rescind_offer *)hdr; |
85d9aa70 DC |
779 | |
780 | mutex_lock(&vmbus_connection.channel_mutex); | |
d43e2fe7 | 781 | channel = relid2channel(rescind->child_relid); |
98e08702 | 782 | |
2dd37cb8 | 783 | if (channel == NULL) { |
f52078cf DC |
784 | /* |
785 | * This is very impossible, because in | |
786 | * vmbus_process_offer(), we have already invoked | |
787 | * vmbus_release_relid() on error. | |
788 | */ | |
85d9aa70 | 789 | goto out; |
2dd37cb8 | 790 | } |
3e7ee490 | 791 | |
d43e2fe7 DC |
792 | spin_lock_irqsave(&channel->lock, flags); |
793 | channel->rescind = true; | |
794 | spin_unlock_irqrestore(&channel->lock, flags); | |
795 | ||
796 | if (channel->device_obj) { | |
499e8401 DC |
797 | if (channel->chn_rescind_callback) { |
798 | channel->chn_rescind_callback(channel); | |
85d9aa70 | 799 | goto out; |
499e8401 | 800 | } |
d43e2fe7 DC |
801 | /* |
802 | * We will have to unregister this device from the | |
803 | * driver core. | |
804 | */ | |
805 | dev = get_device(&channel->device_obj->device); | |
806 | if (dev) { | |
807 | vmbus_device_unregister(channel->device_obj); | |
808 | put_device(dev); | |
809 | } | |
810 | } else { | |
811 | hv_process_channel_removal(channel, | |
812 | channel->offermsg.child_relid); | |
2dd37cb8 | 813 | } |
85d9aa70 DC |
814 | |
815 | out: | |
816 | mutex_unlock(&vmbus_connection.channel_mutex); | |
817 | } | |
818 | ||
819 | void vmbus_hvsock_device_unregister(struct vmbus_channel *channel) | |
820 | { | |
821 | mutex_lock(&vmbus_connection.channel_mutex); | |
822 | ||
823 | BUG_ON(!is_hvsock_channel(channel)); | |
824 | ||
825 | channel->rescind = true; | |
826 | vmbus_device_unregister(channel->device_obj); | |
827 | ||
828 | mutex_unlock(&vmbus_connection.channel_mutex); | |
3e7ee490 | 829 | } |
85d9aa70 DC |
830 | EXPORT_SYMBOL_GPL(vmbus_hvsock_device_unregister); |
831 | ||
3e7ee490 | 832 | |
3e189519 | 833 | /* |
e98cb276 HZ |
834 | * vmbus_onoffers_delivered - |
835 | * This is invoked when all offers have been delivered. | |
bd60c33e GKH |
836 | * |
837 | * Nothing to do here. | |
838 | */ | |
e98cb276 | 839 | static void vmbus_onoffers_delivered( |
bd60c33e | 840 | struct vmbus_channel_message_header *hdr) |
3e7ee490 | 841 | { |
3e7ee490 HJ |
842 | } |
843 | ||
3e189519 | 844 | /* |
e98cb276 | 845 | * vmbus_onopen_result - Open result handler. |
bd60c33e GKH |
846 | * |
847 | * This is invoked when we received a response to our channel open request. | |
848 | * Find the matching request, copy the response and signal the requesting | |
849 | * thread. | |
850 | */ | |
e98cb276 | 851 | static void vmbus_onopen_result(struct vmbus_channel_message_header *hdr) |
3e7ee490 | 852 | { |
bd60c33e | 853 | struct vmbus_channel_open_result *result; |
188963ec HZ |
854 | struct vmbus_channel_msginfo *msginfo; |
855 | struct vmbus_channel_message_header *requestheader; | |
856 | struct vmbus_channel_open_channel *openmsg; | |
dd0813b6 | 857 | unsigned long flags; |
3e7ee490 | 858 | |
bd60c33e | 859 | result = (struct vmbus_channel_open_result *)hdr; |
3e7ee490 | 860 | |
bd60c33e GKH |
861 | /* |
862 | * Find the open msg, copy the result and signal/unblock the wait event | |
863 | */ | |
15b2f647 | 864 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
3e7ee490 | 865 | |
ebb61e5f HJ |
866 | list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list, |
867 | msglistentry) { | |
188963ec | 868 | requestheader = |
c50f7fb2 | 869 | (struct vmbus_channel_message_header *)msginfo->msg; |
188963ec | 870 | |
c50f7fb2 | 871 | if (requestheader->msgtype == CHANNELMSG_OPENCHANNEL) { |
188963ec | 872 | openmsg = |
c50f7fb2 HZ |
873 | (struct vmbus_channel_open_channel *)msginfo->msg; |
874 | if (openmsg->child_relid == result->child_relid && | |
875 | openmsg->openid == result->openid) { | |
876 | memcpy(&msginfo->response.open_result, | |
bd60c33e | 877 | result, |
9568a193 S |
878 | sizeof( |
879 | struct vmbus_channel_open_result)); | |
880 | complete(&msginfo->waitevent); | |
3e7ee490 HJ |
881 | break; |
882 | } | |
883 | } | |
884 | } | |
15b2f647 | 885 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
3e7ee490 HJ |
886 | } |
887 | ||
3e189519 | 888 | /* |
e98cb276 | 889 | * vmbus_ongpadl_created - GPADL created handler. |
bd60c33e GKH |
890 | * |
891 | * This is invoked when we received a response to our gpadl create request. | |
892 | * Find the matching request, copy the response and signal the requesting | |
893 | * thread. | |
894 | */ | |
e98cb276 | 895 | static void vmbus_ongpadl_created(struct vmbus_channel_message_header *hdr) |
3e7ee490 | 896 | { |
188963ec | 897 | struct vmbus_channel_gpadl_created *gpadlcreated; |
188963ec HZ |
898 | struct vmbus_channel_msginfo *msginfo; |
899 | struct vmbus_channel_message_header *requestheader; | |
900 | struct vmbus_channel_gpadl_header *gpadlheader; | |
dd0813b6 | 901 | unsigned long flags; |
3e7ee490 | 902 | |
188963ec | 903 | gpadlcreated = (struct vmbus_channel_gpadl_created *)hdr; |
3e7ee490 | 904 | |
bd60c33e GKH |
905 | /* |
906 | * Find the establish msg, copy the result and signal/unblock the wait | |
907 | * event | |
908 | */ | |
15b2f647 | 909 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
3e7ee490 | 910 | |
ebb61e5f HJ |
911 | list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list, |
912 | msglistentry) { | |
188963ec | 913 | requestheader = |
c50f7fb2 | 914 | (struct vmbus_channel_message_header *)msginfo->msg; |
188963ec | 915 | |
c50f7fb2 | 916 | if (requestheader->msgtype == CHANNELMSG_GPADL_HEADER) { |
188963ec HZ |
917 | gpadlheader = |
918 | (struct vmbus_channel_gpadl_header *)requestheader; | |
919 | ||
c50f7fb2 HZ |
920 | if ((gpadlcreated->child_relid == |
921 | gpadlheader->child_relid) && | |
922 | (gpadlcreated->gpadl == gpadlheader->gpadl)) { | |
923 | memcpy(&msginfo->response.gpadl_created, | |
188963ec | 924 | gpadlcreated, |
9568a193 S |
925 | sizeof( |
926 | struct vmbus_channel_gpadl_created)); | |
927 | complete(&msginfo->waitevent); | |
3e7ee490 HJ |
928 | break; |
929 | } | |
930 | } | |
931 | } | |
15b2f647 | 932 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
3e7ee490 HJ |
933 | } |
934 | ||
3e189519 | 935 | /* |
e98cb276 | 936 | * vmbus_ongpadl_torndown - GPADL torndown handler. |
bd60c33e GKH |
937 | * |
938 | * This is invoked when we received a response to our gpadl teardown request. | |
939 | * Find the matching request, copy the response and signal the requesting | |
940 | * thread. | |
941 | */ | |
e98cb276 | 942 | static void vmbus_ongpadl_torndown( |
bd60c33e | 943 | struct vmbus_channel_message_header *hdr) |
3e7ee490 | 944 | { |
188963ec | 945 | struct vmbus_channel_gpadl_torndown *gpadl_torndown; |
188963ec HZ |
946 | struct vmbus_channel_msginfo *msginfo; |
947 | struct vmbus_channel_message_header *requestheader; | |
948 | struct vmbus_channel_gpadl_teardown *gpadl_teardown; | |
dd0813b6 | 949 | unsigned long flags; |
3e7ee490 | 950 | |
188963ec | 951 | gpadl_torndown = (struct vmbus_channel_gpadl_torndown *)hdr; |
bd60c33e GKH |
952 | |
953 | /* | |
954 | * Find the open msg, copy the result and signal/unblock the wait event | |
955 | */ | |
15b2f647 | 956 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
3e7ee490 | 957 | |
ebb61e5f HJ |
958 | list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list, |
959 | msglistentry) { | |
188963ec | 960 | requestheader = |
c50f7fb2 | 961 | (struct vmbus_channel_message_header *)msginfo->msg; |
3e7ee490 | 962 | |
c50f7fb2 | 963 | if (requestheader->msgtype == CHANNELMSG_GPADL_TEARDOWN) { |
188963ec HZ |
964 | gpadl_teardown = |
965 | (struct vmbus_channel_gpadl_teardown *)requestheader; | |
3e7ee490 | 966 | |
c50f7fb2 HZ |
967 | if (gpadl_torndown->gpadl == gpadl_teardown->gpadl) { |
968 | memcpy(&msginfo->response.gpadl_torndown, | |
188963ec | 969 | gpadl_torndown, |
9568a193 S |
970 | sizeof( |
971 | struct vmbus_channel_gpadl_torndown)); | |
972 | complete(&msginfo->waitevent); | |
3e7ee490 HJ |
973 | break; |
974 | } | |
975 | } | |
976 | } | |
15b2f647 | 977 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
3e7ee490 HJ |
978 | } |
979 | ||
3e189519 | 980 | /* |
e98cb276 | 981 | * vmbus_onversion_response - Version response handler |
bd60c33e GKH |
982 | * |
983 | * This is invoked when we received a response to our initiate contact request. | |
984 | * Find the matching request, copy the response and signal the requesting | |
985 | * thread. | |
986 | */ | |
e98cb276 | 987 | static void vmbus_onversion_response( |
bd60c33e | 988 | struct vmbus_channel_message_header *hdr) |
3e7ee490 | 989 | { |
188963ec HZ |
990 | struct vmbus_channel_msginfo *msginfo; |
991 | struct vmbus_channel_message_header *requestheader; | |
188963ec | 992 | struct vmbus_channel_version_response *version_response; |
dd0813b6 | 993 | unsigned long flags; |
3e7ee490 | 994 | |
188963ec | 995 | version_response = (struct vmbus_channel_version_response *)hdr; |
15b2f647 | 996 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
3e7ee490 | 997 | |
ebb61e5f HJ |
998 | list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list, |
999 | msglistentry) { | |
188963ec | 1000 | requestheader = |
c50f7fb2 | 1001 | (struct vmbus_channel_message_header *)msginfo->msg; |
3e7ee490 | 1002 | |
c50f7fb2 HZ |
1003 | if (requestheader->msgtype == |
1004 | CHANNELMSG_INITIATE_CONTACT) { | |
c50f7fb2 | 1005 | memcpy(&msginfo->response.version_response, |
188963ec | 1006 | version_response, |
bd60c33e | 1007 | sizeof(struct vmbus_channel_version_response)); |
9568a193 | 1008 | complete(&msginfo->waitevent); |
3e7ee490 HJ |
1009 | } |
1010 | } | |
15b2f647 | 1011 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
3e7ee490 HJ |
1012 | } |
1013 | ||
c8212f04 | 1014 | /* Channel message dispatch table */ |
652594c7 | 1015 | struct vmbus_channel_message_table_entry |
b7c6b02f | 1016 | channel_message_table[CHANNELMSG_COUNT] = { |
652594c7 DC |
1017 | {CHANNELMSG_INVALID, 0, NULL}, |
1018 | {CHANNELMSG_OFFERCHANNEL, 0, vmbus_onoffer}, | |
1019 | {CHANNELMSG_RESCIND_CHANNELOFFER, 0, vmbus_onoffer_rescind}, | |
1020 | {CHANNELMSG_REQUESTOFFERS, 0, NULL}, | |
1021 | {CHANNELMSG_ALLOFFERS_DELIVERED, 1, vmbus_onoffers_delivered}, | |
1022 | {CHANNELMSG_OPENCHANNEL, 0, NULL}, | |
1023 | {CHANNELMSG_OPENCHANNEL_RESULT, 1, vmbus_onopen_result}, | |
1024 | {CHANNELMSG_CLOSECHANNEL, 0, NULL}, | |
1025 | {CHANNELMSG_GPADL_HEADER, 0, NULL}, | |
1026 | {CHANNELMSG_GPADL_BODY, 0, NULL}, | |
1027 | {CHANNELMSG_GPADL_CREATED, 1, vmbus_ongpadl_created}, | |
1028 | {CHANNELMSG_GPADL_TEARDOWN, 0, NULL}, | |
1029 | {CHANNELMSG_GPADL_TORNDOWN, 1, vmbus_ongpadl_torndown}, | |
1030 | {CHANNELMSG_RELID_RELEASED, 0, NULL}, | |
1031 | {CHANNELMSG_INITIATE_CONTACT, 0, NULL}, | |
1032 | {CHANNELMSG_VERSION_RESPONSE, 1, vmbus_onversion_response}, | |
1033 | {CHANNELMSG_UNLOAD, 0, NULL}, | |
2db84eff | 1034 | {CHANNELMSG_UNLOAD_RESPONSE, 1, vmbus_unload_response}, |
5c23a1a5 DC |
1035 | {CHANNELMSG_18, 0, NULL}, |
1036 | {CHANNELMSG_19, 0, NULL}, | |
1037 | {CHANNELMSG_20, 0, NULL}, | |
1038 | {CHANNELMSG_TL_CONNECT_REQUEST, 0, NULL}, | |
c8212f04 GKH |
1039 | }; |
1040 | ||
3e189519 | 1041 | /* |
e98cb276 | 1042 | * vmbus_onmessage - Handler for channel protocol messages. |
bd60c33e GKH |
1043 | * |
1044 | * This is invoked in the vmbus worker thread context. | |
1045 | */ | |
e98cb276 | 1046 | void vmbus_onmessage(void *context) |
3e7ee490 | 1047 | { |
188963ec | 1048 | struct hv_message *msg = context; |
82250213 | 1049 | struct vmbus_channel_message_header *hdr; |
3e7ee490 HJ |
1050 | int size; |
1051 | ||
f6feebe0 HZ |
1052 | hdr = (struct vmbus_channel_message_header *)msg->u.payload; |
1053 | size = msg->header.payload_size; | |
3e7ee490 | 1054 | |
c50f7fb2 | 1055 | if (hdr->msgtype >= CHANNELMSG_COUNT) { |
0a46618d | 1056 | pr_err("Received invalid channel message type %d size %d\n", |
c50f7fb2 | 1057 | hdr->msgtype, size); |
04f50c4d | 1058 | print_hex_dump_bytes("", DUMP_PREFIX_NONE, |
f6feebe0 | 1059 | (unsigned char *)msg->u.payload, size); |
3e7ee490 HJ |
1060 | return; |
1061 | } | |
1062 | ||
b7c6b02f S |
1063 | if (channel_message_table[hdr->msgtype].message_handler) |
1064 | channel_message_table[hdr->msgtype].message_handler(hdr); | |
3e7ee490 | 1065 | else |
0a46618d | 1066 | pr_err("Unhandled channel message type %d\n", hdr->msgtype); |
3e7ee490 HJ |
1067 | } |
1068 | ||
3e189519 | 1069 | /* |
e98cb276 | 1070 | * vmbus_request_offers - Send a request to get all our pending offers. |
bd60c33e | 1071 | */ |
e98cb276 | 1072 | int vmbus_request_offers(void) |
3e7ee490 | 1073 | { |
82250213 | 1074 | struct vmbus_channel_message_header *msg; |
188963ec | 1075 | struct vmbus_channel_msginfo *msginfo; |
51e5181d | 1076 | int ret; |
3e7ee490 | 1077 | |
188963ec | 1078 | msginfo = kmalloc(sizeof(*msginfo) + |
bd60c33e GKH |
1079 | sizeof(struct vmbus_channel_message_header), |
1080 | GFP_KERNEL); | |
188963ec | 1081 | if (!msginfo) |
75910f23 | 1082 | return -ENOMEM; |
3e7ee490 | 1083 | |
c50f7fb2 | 1084 | msg = (struct vmbus_channel_message_header *)msginfo->msg; |
3e7ee490 | 1085 | |
c50f7fb2 | 1086 | msg->msgtype = CHANNELMSG_REQUESTOFFERS; |
3e7ee490 | 1087 | |
3e7ee490 | 1088 | |
c6977677 | 1089 | ret = vmbus_post_msg(msg, |
bd60c33e GKH |
1090 | sizeof(struct vmbus_channel_message_header)); |
1091 | if (ret != 0) { | |
0a46618d | 1092 | pr_err("Unable to request offers - %d\n", ret); |
3e7ee490 | 1093 | |
0c3b7b2f S |
1094 | goto cleanup; |
1095 | } | |
3e7ee490 | 1096 | |
0c3b7b2f | 1097 | cleanup: |
dd9b15dc | 1098 | kfree(msginfo); |
3e7ee490 | 1099 | |
3e7ee490 HJ |
1100 | return ret; |
1101 | } | |
1102 | ||
e68d2971 S |
1103 | /* |
1104 | * Retrieve the (sub) channel on which to send an outgoing request. | |
a13e8bbe S |
1105 | * When a primary channel has multiple sub-channels, we try to |
1106 | * distribute the load equally amongst all available channels. | |
e68d2971 S |
1107 | */ |
1108 | struct vmbus_channel *vmbus_get_outgoing_channel(struct vmbus_channel *primary) | |
1109 | { | |
1110 | struct list_head *cur, *tmp; | |
87712bf8 | 1111 | int cur_cpu; |
e68d2971 S |
1112 | struct vmbus_channel *cur_channel; |
1113 | struct vmbus_channel *outgoing_channel = primary; | |
a13e8bbe S |
1114 | int next_channel; |
1115 | int i = 1; | |
e68d2971 S |
1116 | |
1117 | if (list_empty(&primary->sc_list)) | |
1118 | return outgoing_channel; | |
1119 | ||
a13e8bbe S |
1120 | next_channel = primary->next_oc++; |
1121 | ||
1122 | if (next_channel > (primary->num_sc)) { | |
1123 | primary->next_oc = 0; | |
1124 | return outgoing_channel; | |
1125 | } | |
1126 | ||
87712bf8 S |
1127 | cur_cpu = hv_context.vp_index[get_cpu()]; |
1128 | put_cpu(); | |
e68d2971 S |
1129 | list_for_each_safe(cur, tmp, &primary->sc_list) { |
1130 | cur_channel = list_entry(cur, struct vmbus_channel, sc_list); | |
1131 | if (cur_channel->state != CHANNEL_OPENED_STATE) | |
1132 | continue; | |
1133 | ||
1134 | if (cur_channel->target_vp == cur_cpu) | |
1135 | return cur_channel; | |
1136 | ||
a13e8bbe S |
1137 | if (i == next_channel) |
1138 | return cur_channel; | |
e68d2971 | 1139 | |
a13e8bbe | 1140 | i++; |
e68d2971 S |
1141 | } |
1142 | ||
1143 | return outgoing_channel; | |
1144 | } | |
1145 | EXPORT_SYMBOL_GPL(vmbus_get_outgoing_channel); | |
1146 | ||
1147 | static void invoke_sc_cb(struct vmbus_channel *primary_channel) | |
1148 | { | |
1149 | struct list_head *cur, *tmp; | |
1150 | struct vmbus_channel *cur_channel; | |
1151 | ||
1152 | if (primary_channel->sc_creation_callback == NULL) | |
1153 | return; | |
1154 | ||
1155 | list_for_each_safe(cur, tmp, &primary_channel->sc_list) { | |
1156 | cur_channel = list_entry(cur, struct vmbus_channel, sc_list); | |
1157 | ||
1158 | primary_channel->sc_creation_callback(cur_channel); | |
1159 | } | |
1160 | } | |
1161 | ||
1162 | void vmbus_set_sc_create_callback(struct vmbus_channel *primary_channel, | |
1163 | void (*sc_cr_cb)(struct vmbus_channel *new_sc)) | |
1164 | { | |
1165 | primary_channel->sc_creation_callback = sc_cr_cb; | |
1166 | } | |
1167 | EXPORT_SYMBOL_GPL(vmbus_set_sc_create_callback); | |
1168 | ||
1169 | bool vmbus_are_subchannels_present(struct vmbus_channel *primary) | |
1170 | { | |
1171 | bool ret; | |
1172 | ||
1173 | ret = !list_empty(&primary->sc_list); | |
1174 | ||
1175 | if (ret) { | |
1176 | /* | |
1177 | * Invoke the callback on sub-channel creation. | |
1178 | * This will present a uniform interface to the | |
1179 | * clients. | |
1180 | */ | |
1181 | invoke_sc_cb(primary); | |
1182 | } | |
1183 | ||
1184 | return ret; | |
1185 | } | |
1186 | EXPORT_SYMBOL_GPL(vmbus_are_subchannels_present); | |
499e8401 DC |
1187 | |
1188 | void vmbus_set_chn_rescind_callback(struct vmbus_channel *channel, | |
1189 | void (*chn_rescind_cb)(struct vmbus_channel *)) | |
1190 | { | |
1191 | channel->chn_rescind_callback = chn_rescind_cb; | |
1192 | } | |
1193 | EXPORT_SYMBOL_GPL(vmbus_set_chn_rescind_callback); |