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