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