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