blob: 8818b92e59127aee661743b331f03b1af4d0ee82 [file] [log] [blame]
Hank Janssen3e7ee492009-07-13 16:02:34 -07001/*
Hank Janssen3e7ee492009-07-13 16:02:34 -07002 * 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>
Hank Janssen3e7ee492009-07-13 16:02:34 -070020 */
Hank Janssen0a466182011-03-29 13:58:47 -070021#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
Greg Kroah-Hartmana0086dc2009-08-17 17:22:08 -070023#include <linux/kernel.h>
Dexuan Cui638fea32016-06-09 18:47:24 -070024#include <linux/interrupt.h>
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -080025#include <linux/sched.h>
26#include <linux/wait.h>
Greg Kroah-Hartmana0086dc2009-08-17 17:22:08 -070027#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Bill Pemberton53af5452009-09-11 21:46:44 -040029#include <linux/list.h>
Hank Janssenc88c4e42010-05-04 15:55:05 -070030#include <linux/module.h>
Haiyang Zhang8b5d6d32010-05-28 23:22:44 +000031#include <linux/completion.h>
Vitaly Kuznetsov41571912016-01-27 22:29:35 -080032#include <linux/delay.h>
Greg Kroah-Hartman46a97192011-10-04 12:29:52 -070033#include <linux/hyperv.h>
K. Y. Srinivasan3f335ea2011-05-12 19:34:15 -070034
K. Y. Srinivasan0f2a6612011-05-12 19:34:28 -070035#include "hyperv_vmbus.h"
Hank Janssen3e7ee492009-07-13 16:02:34 -070036
K. Y. Srinivasan7047f172015-12-25 20:00:30 -080037static 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 */
137 { .dev_type = HV_UNKOWN,
138 .perf_device = false,
139 },
140};
141
142static u16 hv_get_dev_type(const uuid_le *guid)
143{
144 u16 i;
145
146 for (i = HV_IDE; i < HV_UNKOWN; i++) {
147 if (!uuid_le_cmp(*guid, vmbus_devs[i].guid))
148 return i;
149 }
150 pr_info("Unknown GUID: %pUl\n", guid);
151 return i;
152}
Vitaly Kuznetsovf38e7dd2015-05-06 17:47:45 -0700153
Hank Janssenc88c4e42010-05-04 15:55:05 -0700154/**
Greg Kroah-Hartmanda0e9632011-10-11 08:42:28 -0600155 * vmbus_prep_negotiate_resp() - Create default response for Hyper-V Negotiate message
Hank Janssenc88c4e42010-05-04 15:55:05 -0700156 * @icmsghdrp: Pointer to msg header structure
157 * @icmsg_negotiate: Pointer to negotiate message structure
158 * @buf: Raw buffer channel data
159 *
160 * @icmsghdrp is of type &struct icmsg_hdr.
161 * @negop is of type &struct icmsg_negotiate.
K. Y. Srinivasanc836d0a2012-05-12 13:44:58 -0700162 * Set up and fill in default negotiate response message.
163 *
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700164 * The fw_version specifies the framework version that
165 * we can support and srv_version specifies the service
166 * version we can support.
Hank Janssenc88c4e42010-05-04 15:55:05 -0700167 *
168 * Mainly used by Hyper-V drivers.
169 */
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700170bool vmbus_prep_negotiate_resp(struct icmsg_hdr *icmsghdrp,
K. Y. Srinivasanc836d0a2012-05-12 13:44:58 -0700171 struct icmsg_negotiate *negop, u8 *buf,
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700172 int fw_version, int srv_version)
Hank Janssenc88c4e42010-05-04 15:55:05 -0700173{
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700174 int icframe_major, icframe_minor;
175 int icmsg_major, icmsg_minor;
176 int fw_major, fw_minor;
177 int srv_major, srv_minor;
K. Y. Srinivasanc836d0a2012-05-12 13:44:58 -0700178 int i;
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700179 bool found_match = false;
K. Y. Srinivasanc836d0a2012-05-12 13:44:58 -0700180
K. Y. Srinivasana3605302012-05-12 13:44:57 -0700181 icmsghdrp->icmsgsize = 0x10;
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700182 fw_major = (fw_version >> 16);
183 fw_minor = (fw_version & 0xFFFF);
184
185 srv_major = (srv_version >> 16);
186 srv_minor = (srv_version & 0xFFFF);
Hank Janssenc88c4e42010-05-04 15:55:05 -0700187
K. Y. Srinivasana3605302012-05-12 13:44:57 -0700188 negop = (struct icmsg_negotiate *)&buf[
189 sizeof(struct vmbuspipe_hdr) +
190 sizeof(struct icmsg_hdr)];
Hank Janssenc88c4e42010-05-04 15:55:05 -0700191
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700192 icframe_major = negop->icframe_vercnt;
193 icframe_minor = 0;
194
195 icmsg_major = negop->icmsg_vercnt;
196 icmsg_minor = 0;
K. Y. Srinivasanc836d0a2012-05-12 13:44:58 -0700197
198 /*
199 * Select the framework version number we will
200 * support.
201 */
202
203 for (i = 0; i < negop->icframe_vercnt; i++) {
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700204 if ((negop->icversion_data[i].major == fw_major) &&
205 (negop->icversion_data[i].minor == fw_minor)) {
206 icframe_major = negop->icversion_data[i].major;
207 icframe_minor = negop->icversion_data[i].minor;
208 found_match = true;
209 }
Hank Janssenc88c4e42010-05-04 15:55:05 -0700210 }
K. Y. Srinivasana3605302012-05-12 13:44:57 -0700211
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700212 if (!found_match)
213 goto fw_error;
214
215 found_match = false;
216
K. Y. Srinivasanc836d0a2012-05-12 13:44:58 -0700217 for (i = negop->icframe_vercnt;
218 (i < negop->icframe_vercnt + negop->icmsg_vercnt); i++) {
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700219 if ((negop->icversion_data[i].major == srv_major) &&
220 (negop->icversion_data[i].minor == srv_minor)) {
221 icmsg_major = negop->icversion_data[i].major;
222 icmsg_minor = negop->icversion_data[i].minor;
223 found_match = true;
224 }
K. Y. Srinivasanc836d0a2012-05-12 13:44:58 -0700225 }
226
227 /*
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700228 * Respond with the framework and service
K. Y. Srinivasanc836d0a2012-05-12 13:44:58 -0700229 * version numbers we can support.
230 */
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700231
232fw_error:
233 if (!found_match) {
234 negop->icframe_vercnt = 0;
235 negop->icmsg_vercnt = 0;
236 } else {
237 negop->icframe_vercnt = 1;
238 negop->icmsg_vercnt = 1;
239 }
240
241 negop->icversion_data[0].major = icframe_major;
242 negop->icversion_data[0].minor = icframe_minor;
243 negop->icversion_data[1].major = icmsg_major;
244 negop->icversion_data[1].minor = icmsg_minor;
245 return found_match;
Hank Janssenc88c4e42010-05-04 15:55:05 -0700246}
K. Y. Srinivasana3605302012-05-12 13:44:57 -0700247
Greg Kroah-Hartmanda0e9632011-10-11 08:42:28 -0600248EXPORT_SYMBOL_GPL(vmbus_prep_negotiate_resp);
Hank Janssenc88c4e42010-05-04 15:55:05 -0700249
Hank Janssen3e189512010-03-04 22:11:00 +0000250/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700251 * alloc_channel - Allocate and initialize a vmbus channel object
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700252 */
Greg Kroah-Hartman50fe56d2010-10-20 15:51:57 -0700253static struct vmbus_channel *alloc_channel(void)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700254{
Vitaly Kuznetsovbc63b6f2015-02-27 11:25:52 -0800255 static atomic_t chan_num = ATOMIC_INIT(0);
Greg Kroah-Hartmanaded7162009-08-18 15:21:19 -0700256 struct vmbus_channel *channel;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700257
Greg Kroah-Hartmanaded7162009-08-18 15:21:19 -0700258 channel = kzalloc(sizeof(*channel), GFP_ATOMIC);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700259 if (!channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700260 return NULL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700261
Vitaly Kuznetsovbc63b6f2015-02-27 11:25:52 -0800262 channel->id = atomic_inc_return(&chan_num);
K. Y. Srinivasanfe760e42016-01-27 22:29:45 -0800263 channel->acquire_ring_lock = true;
Greg Kroah-Hartman54411c42009-07-15 14:48:32 -0700264 spin_lock_init(&channel->inbound_lock);
Vitaly Kuznetsov67fae052015-01-20 16:45:05 +0100265 spin_lock_init(&channel->lock);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700266
267 INIT_LIST_HEAD(&channel->sc_list);
K. Y. Srinivasan3a28fa32014-04-08 18:45:54 -0700268 INIT_LIST_HEAD(&channel->percpu_list);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700269
Hank Janssen3e7ee492009-07-13 16:02:34 -0700270 return channel;
271}
272
Hank Janssen3e189512010-03-04 22:11:00 +0000273/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700274 * free_channel - Release the resources used by the vmbus channel object
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700275 */
Greg Kroah-Hartman9f3e28e2011-10-11 09:40:01 -0600276static void free_channel(struct vmbus_channel *channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700277{
Dexuan Cuiaadc3782015-03-27 09:10:10 -0700278 kfree(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700279}
280
K. Y. Srinivasan3a28fa32014-04-08 18:45:54 -0700281static void percpu_channel_enq(void *arg)
282{
283 struct vmbus_channel *channel = arg;
284 int cpu = smp_processor_id();
Haiyang Zhang8b5d6d32010-05-28 23:22:44 +0000285
K. Y. Srinivasan3a28fa32014-04-08 18:45:54 -0700286 list_add_tail(&channel->percpu_list, &hv_context.percpu_list[cpu]);
287}
288
289static void percpu_channel_deq(void *arg)
290{
291 struct vmbus_channel *channel = arg;
292
293 list_del(&channel->percpu_list);
294}
Haiyang Zhang8b5d6d32010-05-28 23:22:44 +0000295
K. Y. Srinivasaned6cfcc2015-02-28 11:18:17 -0800296
Dexuan Cuif52078c2015-12-14 16:01:50 -0800297static void vmbus_release_relid(u32 relid)
Timo Teräs4b2f9ab2010-12-15 20:48:09 +0200298{
K. Y. Srinivasaned6cfcc2015-02-28 11:18:17 -0800299 struct vmbus_channel_relid_released msg;
Vitaly Kuznetsov04a258c2014-11-04 13:40:11 +0100300
K. Y. Srinivasanc8705972013-03-15 12:25:44 -0700301 memset(&msg, 0, sizeof(struct vmbus_channel_relid_released));
K. Y. Srinivasaned6cfcc2015-02-28 11:18:17 -0800302 msg.child_relid = relid;
K. Y. Srinivasanc8705972013-03-15 12:25:44 -0700303 msg.header.msgtype = CHANNELMSG_RELID_RELEASED;
304 vmbus_post_msg(&msg, sizeof(struct vmbus_channel_relid_released));
Dexuan Cuif52078c2015-12-14 16:01:50 -0800305}
K. Y. Srinivasanc8705972013-03-15 12:25:44 -0700306
Dexuan Cui638fea32016-06-09 18:47:24 -0700307void hv_event_tasklet_disable(struct vmbus_channel *channel)
308{
309 struct tasklet_struct *tasklet;
310 tasklet = hv_context.event_dpc[channel->target_cpu];
311 tasklet_disable(tasklet);
312}
313
314void hv_event_tasklet_enable(struct vmbus_channel *channel)
315{
316 struct tasklet_struct *tasklet;
317 tasklet = hv_context.event_dpc[channel->target_cpu];
318 tasklet_enable(tasklet);
319
320 /* In case there is any pending event */
321 tasklet_schedule(tasklet);
322}
323
Dexuan Cuif52078c2015-12-14 16:01:50 -0800324void hv_process_channel_removal(struct vmbus_channel *channel, u32 relid)
325{
326 unsigned long flags;
327 struct vmbus_channel *primary_channel;
328
Dexuan Cui34c68012015-12-14 16:01:49 -0800329 BUG_ON(!channel->rescind);
Dexuan Cui85d9aa72016-01-27 22:29:43 -0800330 BUG_ON(!mutex_is_locked(&vmbus_connection.channel_mutex));
Dexuan Cui34c68012015-12-14 16:01:49 -0800331
Dexuan Cui638fea32016-06-09 18:47:24 -0700332 hv_event_tasklet_disable(channel);
K. Y. Srinivasan2115b562014-08-28 18:29:53 -0700333 if (channel->target_cpu != get_cpu()) {
334 put_cpu();
K. Y. Srinivasan3a28fa32014-04-08 18:45:54 -0700335 smp_call_function_single(channel->target_cpu,
336 percpu_channel_deq, channel, true);
K. Y. Srinivasan2115b562014-08-28 18:29:53 -0700337 } else {
K. Y. Srinivasan3a28fa32014-04-08 18:45:54 -0700338 percpu_channel_deq(channel);
K. Y. Srinivasan2115b562014-08-28 18:29:53 -0700339 put_cpu();
340 }
Dexuan Cui638fea32016-06-09 18:47:24 -0700341 hv_event_tasklet_enable(channel);
K. Y. Srinivasan3a28fa32014-04-08 18:45:54 -0700342
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700343 if (channel->primary_channel == NULL) {
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700344 list_del(&channel->listentry);
Dexuan Cuica1c4b72015-08-13 17:07:03 -0700345
346 primary_channel = channel;
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700347 } else {
348 primary_channel = channel->primary_channel;
Vitaly Kuznetsov67fae052015-01-20 16:45:05 +0100349 spin_lock_irqsave(&primary_channel->lock, flags);
K. Y. Srinivasan565ce642013-10-16 19:27:19 -0700350 list_del(&channel->sc_list);
Vitaly Kuznetsov357e8362015-05-06 17:47:44 -0700351 primary_channel->num_sc--;
Vitaly Kuznetsov67fae052015-01-20 16:45:05 +0100352 spin_unlock_irqrestore(&primary_channel->lock, flags);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700353 }
Dexuan Cuica1c4b72015-08-13 17:07:03 -0700354
355 /*
356 * We need to free the bit for init_vp_index() to work in the case
357 * of sub-channel, when we reload drivers like hv_netvsc.
358 */
359 cpumask_clear_cpu(channel->target_cpu,
360 &primary_channel->alloced_cpus_in_node);
361
Dexuan Cui638fea32016-06-09 18:47:24 -0700362 vmbus_release_relid(relid);
363
K. Y. Srinivasanc8705972013-03-15 12:25:44 -0700364 free_channel(channel);
Timo Teräs4b2f9ab2010-12-15 20:48:09 +0200365}
Haiyang Zhang8b5d6d32010-05-28 23:22:44 +0000366
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -0800367void vmbus_free_channels(void)
368{
Dexuan Cui813c5b72015-04-22 21:31:30 -0700369 struct vmbus_channel *channel, *tmp;
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -0800370
Dexuan Cui813c5b72015-04-22 21:31:30 -0700371 list_for_each_entry_safe(channel, tmp, &vmbus_connection.chn_list,
372 listentry) {
Dexuan Cui34c68012015-12-14 16:01:49 -0800373 /* hv_process_channel_removal() needs this */
Dexuan Cui813c5b72015-04-22 21:31:30 -0700374 channel->rescind = true;
375
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -0800376 vmbus_device_unregister(channel->device_obj);
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -0800377 }
378}
379
Hank Janssen3e189512010-03-04 22:11:00 +0000380/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700381 * vmbus_process_offer - Process the offer by creating a channel/device
Hank Janssenc88c4e42010-05-04 15:55:05 -0700382 * associated with this offer
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700383 */
K. Y. Srinivasan2dd37cb2015-02-28 11:18:18 -0800384static void vmbus_process_offer(struct vmbus_channel *newchannel)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700385{
Greg Kroah-Hartmanaded7162009-08-18 15:21:19 -0700386 struct vmbus_channel *channel;
Haiyang Zhang188963e2010-10-15 10:14:06 -0700387 bool fnew = true;
Greg Kroah-Hartman0f5e44c2009-07-15 14:57:16 -0700388 unsigned long flags;
K. Y. Srinivasan7047f172015-12-25 20:00:30 -0800389 u16 dev_type;
Dexuan Cui85d9aa72016-01-27 22:29:43 -0800390 int ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700391
Bill Pemberton454f18a2009-07-27 16:47:24 -0400392 /* Make sure this is a new offer */
Dexuan Cuid6f591e2015-12-14 16:01:51 -0800393 mutex_lock(&vmbus_connection.channel_mutex);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700394
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800395 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
K. Y. Srinivasan358d2ee2011-08-25 09:48:28 -0700396 if (!uuid_le_cmp(channel->offermsg.offer.if_type,
397 newchannel->offermsg.offer.if_type) &&
398 !uuid_le_cmp(channel->offermsg.offer.if_instance,
399 newchannel->offermsg.offer.if_instance)) {
Haiyang Zhang188963e2010-10-15 10:14:06 -0700400 fnew = false;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700401 break;
402 }
403 }
404
Vitaly Kuznetsov8dfd3322015-05-06 17:47:42 -0700405 if (fnew)
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800406 list_add_tail(&newchannel->listentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800407 &vmbus_connection.chn_list);
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700408
Dexuan Cuid6f591e2015-12-14 16:01:51 -0800409 mutex_unlock(&vmbus_connection.channel_mutex);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700410
Haiyang Zhang188963e2010-10-15 10:14:06 -0700411 if (!fnew) {
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700412 /*
413 * Check to see if this is a sub-channel.
414 */
415 if (newchannel->offermsg.offer.sub_channel_index != 0) {
416 /*
417 * Process the sub-channel.
418 */
419 newchannel->primary_channel = channel;
Vitaly Kuznetsov67fae052015-01-20 16:45:05 +0100420 spin_lock_irqsave(&channel->lock, flags);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700421 list_add_tail(&newchannel->sc_list, &channel->sc_list);
K. Y. Srinivasana13e8bb2015-02-28 11:39:02 -0800422 channel->num_sc++;
Vitaly Kuznetsov357e8362015-05-06 17:47:44 -0700423 spin_unlock_irqrestore(&channel->lock, flags);
Vitaly Kuznetsov8dfd3322015-05-06 17:47:42 -0700424 } else
425 goto err_free_chan;
426 }
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700427
K. Y. Srinivasan7047f172015-12-25 20:00:30 -0800428 dev_type = hv_get_dev_type(&newchannel->offermsg.offer.if_type);
429
430 init_vp_index(newchannel, dev_type);
Vitaly Kuznetsovf38e7dd2015-05-06 17:47:45 -0700431
Dexuan Cui638fea32016-06-09 18:47:24 -0700432 hv_event_tasklet_disable(newchannel);
Vitaly Kuznetsov8dfd3322015-05-06 17:47:42 -0700433 if (newchannel->target_cpu != get_cpu()) {
434 put_cpu();
435 smp_call_function_single(newchannel->target_cpu,
436 percpu_channel_enq,
437 newchannel, true);
438 } else {
439 percpu_channel_enq(newchannel);
440 put_cpu();
Hank Janssen3e7ee492009-07-13 16:02:34 -0700441 }
Dexuan Cui638fea32016-06-09 18:47:24 -0700442 hv_event_tasklet_enable(newchannel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700443
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700444 /*
K. Y. Srinivasan42dceeb2013-08-26 14:08:58 -0700445 * This state is used to indicate a successful open
446 * so that when we do close the channel normally, we
447 * can cleanup properly
448 */
449 newchannel->state = CHANNEL_OPEN_STATE;
450
Vitaly Kuznetsov8dfd3322015-05-06 17:47:42 -0700451 if (!fnew) {
452 if (channel->sc_creation_callback != NULL)
453 channel->sc_creation_callback(newchannel);
454 return;
455 }
456
K. Y. Srinivasan42dceeb2013-08-26 14:08:58 -0700457 /*
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700458 * Start the process of binding this offer to the driver
459 * We need to set the DeviceObject field before calling
Haiyang Zhang646f1ea2011-01-26 12:12:10 -0800460 * vmbus_child_dev_add()
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700461 */
K. Y. Srinivasanf2c73012011-09-08 07:24:12 -0700462 newchannel->device_obj = vmbus_device_create(
Haiyang Zhang767dff62011-01-26 12:12:12 -0800463 &newchannel->offermsg.offer.if_type,
464 &newchannel->offermsg.offer.if_instance,
Haiyang Zhang188963e2010-10-15 10:14:06 -0700465 newchannel);
Vitaly Kuznetsov9c3a6f72015-01-20 16:45:04 +0100466 if (!newchannel->device_obj)
K. Y. Srinivasan5b1e5b52015-02-28 11:18:19 -0800467 goto err_deq_chan;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700468
K. Y. Srinivasan7047f172015-12-25 20:00:30 -0800469 newchannel->device_obj->device_id = dev_type;
Bill Pemberton454f18a2009-07-27 16:47:24 -0400470 /*
471 * Add the new device to the bus. This will kick off device-driver
472 * binding which eventually invokes the device driver's AddDevice()
473 * method.
474 */
Dexuan Cui85d9aa72016-01-27 22:29:43 -0800475 mutex_lock(&vmbus_connection.channel_mutex);
476 ret = vmbus_device_register(newchannel->device_obj);
477 mutex_unlock(&vmbus_connection.channel_mutex);
478
479 if (ret != 0) {
Dexuan Cuid43e2fe2015-03-27 09:10:09 -0700480 pr_err("unable to add child device object (relid %d)\n",
481 newchannel->offermsg.child_relid);
482 kfree(newchannel->device_obj);
483 goto err_deq_chan;
484 }
Vitaly Kuznetsov9c3a6f72015-01-20 16:45:04 +0100485 return;
K. Y. Srinivasan2dd37cb2015-02-28 11:18:18 -0800486
K. Y. Srinivasan5b1e5b52015-02-28 11:18:19 -0800487err_deq_chan:
Dexuan Cuid6f591e2015-12-14 16:01:51 -0800488 mutex_lock(&vmbus_connection.channel_mutex);
K. Y. Srinivasan5b1e5b52015-02-28 11:18:19 -0800489 list_del(&newchannel->listentry);
Dexuan Cuid6f591e2015-12-14 16:01:51 -0800490 mutex_unlock(&vmbus_connection.channel_mutex);
K. Y. Srinivasan5b1e5b52015-02-28 11:18:19 -0800491
Dexuan Cui638fea32016-06-09 18:47:24 -0700492 hv_event_tasklet_disable(newchannel);
K. Y. Srinivasan5b1e5b52015-02-28 11:18:19 -0800493 if (newchannel->target_cpu != get_cpu()) {
494 put_cpu();
495 smp_call_function_single(newchannel->target_cpu,
496 percpu_channel_deq, newchannel, true);
497 } else {
498 percpu_channel_deq(newchannel);
499 put_cpu();
500 }
Dexuan Cui638fea32016-06-09 18:47:24 -0700501 hv_event_tasklet_enable(newchannel);
502
503 vmbus_release_relid(newchannel->offermsg.child_relid);
K. Y. Srinivasan5b1e5b52015-02-28 11:18:19 -0800504
Vitaly Kuznetsov9c3a6f72015-01-20 16:45:04 +0100505err_free_chan:
506 free_channel(newchannel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700507}
508
K. Y. Srinivasana1198452012-12-01 06:46:50 -0800509/*
510 * We use this state to statically distribute the channel interrupt load.
511 */
K. Y. Srinivasan1f656ff2015-05-30 23:37:48 -0700512static int next_numa_node_id;
K. Y. Srinivasana1198452012-12-01 06:46:50 -0800513
514/*
515 * Starting with Win8, we can statically distribute the incoming
K. Y. Srinivasan1f656ff2015-05-30 23:37:48 -0700516 * channel interrupt load by binding a channel to VCPU.
517 * We do this in a hierarchical fashion:
518 * First distribute the primary channels across available NUMA nodes
519 * and then distribute the subchannels amongst the CPUs in the NUMA
520 * node assigned to the primary channel.
521 *
522 * For pre-win8 hosts or non-performance critical channels we assign the
523 * first CPU in the first NUMA node.
K. Y. Srinivasana1198452012-12-01 06:46:50 -0800524 */
K. Y. Srinivasan7047f172015-12-25 20:00:30 -0800525static void init_vp_index(struct vmbus_channel *channel, u16 dev_type)
K. Y. Srinivasana1198452012-12-01 06:46:50 -0800526{
527 u32 cur_cpu;
K. Y. Srinivasan7047f172015-12-25 20:00:30 -0800528 bool perf_chn = vmbus_devs[dev_type].perf_device;
K. Y. Srinivasan1f656ff2015-05-30 23:37:48 -0700529 struct vmbus_channel *primary = channel->primary_channel;
530 int next_node;
531 struct cpumask available_mask;
K. Y. Srinivasan9f01ec52015-08-05 00:52:38 -0700532 struct cpumask *alloced_mask;
K. Y. Srinivasana1198452012-12-01 06:46:50 -0800533
K. Y. Srinivasana1198452012-12-01 06:46:50 -0800534 if ((vmbus_proto_version == VERSION_WS2008) ||
535 (vmbus_proto_version == VERSION_WIN7) || (!perf_chn)) {
536 /*
537 * Prior to win8, all channel interrupts are
538 * delivered on cpu 0.
539 * Also if the channel is not a performance critical
540 * channel, bind it to cpu 0.
541 */
K. Y. Srinivasan1f656ff2015-05-30 23:37:48 -0700542 channel->numa_node = 0;
K. Y. Srinivasand3ba7202014-04-08 18:45:53 -0700543 channel->target_cpu = 0;
K. Y. Srinivasan9c6e64a2015-05-30 23:37:47 -0700544 channel->target_vp = hv_context.vp_index[0];
K. Y. Srinivasand3ba7202014-04-08 18:45:53 -0700545 return;
K. Y. Srinivasana1198452012-12-01 06:46:50 -0800546 }
Vitaly Kuznetsovce59fec2015-05-06 17:47:46 -0700547
548 /*
K. Y. Srinivasan1f656ff2015-05-30 23:37:48 -0700549 * We distribute primary channels evenly across all the available
550 * NUMA nodes and within the assigned NUMA node we will assign the
551 * first available CPU to the primary channel.
552 * The sub-channels will be assigned to the CPUs available in the
553 * NUMA node evenly.
Vitaly Kuznetsovce59fec2015-05-06 17:47:46 -0700554 */
K. Y. Srinivasan1f656ff2015-05-30 23:37:48 -0700555 if (!primary) {
556 while (true) {
557 next_node = next_numa_node_id++;
558 if (next_node == nr_node_ids)
559 next_node = next_numa_node_id = 0;
560 if (cpumask_empty(cpumask_of_node(next_node)))
561 continue;
562 break;
Vitaly Kuznetsovce59fec2015-05-06 17:47:46 -0700563 }
K. Y. Srinivasan1f656ff2015-05-30 23:37:48 -0700564 channel->numa_node = next_node;
565 primary = channel;
Vitaly Kuznetsovce59fec2015-05-06 17:47:46 -0700566 }
K. Y. Srinivasan9f01ec52015-08-05 00:52:38 -0700567 alloced_mask = &hv_context.hv_numa_map[primary->numa_node];
Vitaly Kuznetsovce59fec2015-05-06 17:47:46 -0700568
K. Y. Srinivasan9f01ec52015-08-05 00:52:38 -0700569 if (cpumask_weight(alloced_mask) ==
K. Y. Srinivasan1f656ff2015-05-30 23:37:48 -0700570 cpumask_weight(cpumask_of_node(primary->numa_node))) {
571 /*
572 * We have cycled through all the CPUs in the node;
573 * reset the alloced map.
574 */
K. Y. Srinivasan9f01ec52015-08-05 00:52:38 -0700575 cpumask_clear(alloced_mask);
K. Y. Srinivasan1f656ff2015-05-30 23:37:48 -0700576 }
577
K. Y. Srinivasan9f01ec52015-08-05 00:52:38 -0700578 cpumask_xor(&available_mask, alloced_mask,
K. Y. Srinivasan1f656ff2015-05-30 23:37:48 -0700579 cpumask_of_node(primary->numa_node));
580
Dexuan Cui3b711072015-08-05 00:52:39 -0700581 cur_cpu = -1;
Vitaly Kuznetsov79fd8e72016-01-27 22:29:34 -0800582
583 /*
584 * Normally Hyper-V host doesn't create more subchannels than there
585 * are VCPUs on the node but it is possible when not all present VCPUs
586 * on the node are initialized by guest. Clear the alloced_cpus_in_node
587 * to start over.
588 */
589 if (cpumask_equal(&primary->alloced_cpus_in_node,
590 cpumask_of_node(primary->numa_node)))
591 cpumask_clear(&primary->alloced_cpus_in_node);
592
Dexuan Cui3b711072015-08-05 00:52:39 -0700593 while (true) {
594 cur_cpu = cpumask_next(cur_cpu, &available_mask);
595 if (cur_cpu >= nr_cpu_ids) {
596 cur_cpu = -1;
597 cpumask_copy(&available_mask,
598 cpumask_of_node(primary->numa_node));
599 continue;
600 }
601
Dexuan Cuica1c4b72015-08-13 17:07:03 -0700602 /*
603 * NOTE: in the case of sub-channel, we clear the sub-channel
604 * related bit(s) in primary->alloced_cpus_in_node in
605 * hv_process_channel_removal(), so when we reload drivers
606 * like hv_netvsc in SMP guest, here we're able to re-allocate
607 * bit from primary->alloced_cpus_in_node.
608 */
Dexuan Cui3b711072015-08-05 00:52:39 -0700609 if (!cpumask_test_cpu(cur_cpu,
610 &primary->alloced_cpus_in_node)) {
611 cpumask_set_cpu(cur_cpu,
612 &primary->alloced_cpus_in_node);
613 cpumask_set_cpu(cur_cpu, alloced_mask);
614 break;
615 }
616 }
K. Y. Srinivasan1f656ff2015-05-30 23:37:48 -0700617
K. Y. Srinivasand3ba7202014-04-08 18:45:53 -0700618 channel->target_cpu = cur_cpu;
619 channel->target_vp = hv_context.vp_index[cur_cpu];
K. Y. Srinivasana1198452012-12-01 06:46:50 -0800620}
621
Vitaly Kuznetsov41571912016-01-27 22:29:35 -0800622static void vmbus_wait_for_unload(void)
623{
Vitaly Kuznetsovcd95aad2016-04-30 19:21:34 -0700624 int cpu;
625 void *page_addr;
626 struct hv_message *msg;
Vitaly Kuznetsov41571912016-01-27 22:29:35 -0800627 struct vmbus_channel_message_header *hdr;
Vitaly Kuznetsovcd95aad2016-04-30 19:21:34 -0700628 u32 message_type;
Vitaly Kuznetsov41571912016-01-27 22:29:35 -0800629
Vitaly Kuznetsovcd95aad2016-04-30 19:21:34 -0700630 /*
631 * CHANNELMSG_UNLOAD_RESPONSE is always delivered to the CPU which was
632 * used for initial contact or to CPU0 depending on host version. When
633 * we're crashing on a different CPU let's hope that IRQ handler on
634 * the cpu which receives CHANNELMSG_UNLOAD_RESPONSE is still
635 * functional and vmbus_unload_response() will complete
636 * vmbus_connection.unload_event. If not, the last thing we can do is
637 * read message pages for all CPUs directly.
638 */
Vitaly Kuznetsov41571912016-01-27 22:29:35 -0800639 while (1) {
Vitaly Kuznetsovcd95aad2016-04-30 19:21:34 -0700640 if (completion_done(&vmbus_connection.unload_event))
641 break;
642
643 for_each_online_cpu(cpu) {
644 page_addr = hv_context.synic_message_page[cpu];
645 msg = (struct hv_message *)page_addr +
646 VMBUS_MESSAGE_SINT;
647
648 message_type = READ_ONCE(msg->header.message_type);
649 if (message_type == HVMSG_NONE)
650 continue;
651
652 hdr = (struct vmbus_channel_message_header *)
653 msg->u.payload;
654
655 if (hdr->msgtype == CHANNELMSG_UNLOAD_RESPONSE)
656 complete(&vmbus_connection.unload_event);
657
658 vmbus_signal_eom(msg, message_type);
Vitaly Kuznetsov41571912016-01-27 22:29:35 -0800659 }
660
Vitaly Kuznetsovcd95aad2016-04-30 19:21:34 -0700661 mdelay(10);
662 }
Vitaly Kuznetsov41571912016-01-27 22:29:35 -0800663
Vitaly Kuznetsovcd95aad2016-04-30 19:21:34 -0700664 /*
665 * We're crashing and already got the UNLOAD_RESPONSE, cleanup all
666 * maybe-pending messages on all CPUs to be able to receive new
667 * messages after we reconnect.
668 */
669 for_each_online_cpu(cpu) {
670 page_addr = hv_context.synic_message_page[cpu];
671 msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
672 msg->header.message_type = HVMSG_NONE;
Vitaly Kuznetsov41571912016-01-27 22:29:35 -0800673 }
674}
675
Hank Janssen3e189512010-03-04 22:11:00 +0000676/*
K. Y. Srinivasan2db84ef2015-04-22 21:31:32 -0700677 * vmbus_unload_response - Handler for the unload response.
678 */
679static void vmbus_unload_response(struct vmbus_channel_message_header *hdr)
680{
681 /*
682 * This is a global event; just wakeup the waiting thread.
683 * Once we successfully unload, we can cleanup the monitor state.
684 */
685 complete(&vmbus_connection.unload_event);
686}
687
Vitaly Kuznetsov75ff3a82016-02-26 15:13:16 -0800688void vmbus_initiate_unload(bool crash)
K. Y. Srinivasan2db84ef2015-04-22 21:31:32 -0700689{
690 struct vmbus_channel_message_header hdr;
691
Vitaly Kuznetsov4a542432015-08-01 16:08:19 -0700692 /* Pre-Win2012R2 hosts don't support reconnect */
693 if (vmbus_proto_version < VERSION_WIN8_1)
694 return;
695
K. Y. Srinivasan2db84ef2015-04-22 21:31:32 -0700696 init_completion(&vmbus_connection.unload_event);
697 memset(&hdr, 0, sizeof(struct vmbus_channel_message_header));
698 hdr.msgtype = CHANNELMSG_UNLOAD;
699 vmbus_post_msg(&hdr, sizeof(struct vmbus_channel_message_header));
700
Vitaly Kuznetsov41571912016-01-27 22:29:35 -0800701 /*
702 * vmbus_initiate_unload() is also called on crash and the crash can be
703 * happening in an interrupt context, where scheduling is impossible.
704 */
Vitaly Kuznetsov75ff3a82016-02-26 15:13:16 -0800705 if (!crash)
Vitaly Kuznetsov41571912016-01-27 22:29:35 -0800706 wait_for_completion(&vmbus_connection.unload_event);
707 else
708 vmbus_wait_for_unload();
K. Y. Srinivasan2db84ef2015-04-22 21:31:32 -0700709}
710
711/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700712 * vmbus_onoffer - Handler for channel offers from vmbus in parent partition.
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700713 *
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700714 */
Haiyang Zhange98cb272010-10-15 10:14:07 -0700715static void vmbus_onoffer(struct vmbus_channel_message_header *hdr)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700716{
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700717 struct vmbus_channel_offer_channel *offer;
Haiyang Zhang188963e2010-10-15 10:14:06 -0700718 struct vmbus_channel *newchannel;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700719
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700720 offer = (struct vmbus_channel_offer_channel *)hdr;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700721
Bill Pemberton454f18a2009-07-27 16:47:24 -0400722 /* Allocate the channel object and save this offer. */
Haiyang Zhange98cb272010-10-15 10:14:07 -0700723 newchannel = alloc_channel();
Haiyang Zhang188963e2010-10-15 10:14:06 -0700724 if (!newchannel) {
Hank Janssen0a466182011-03-29 13:58:47 -0700725 pr_err("Unable to allocate channel object\n");
Hank Janssen3e7ee492009-07-13 16:02:34 -0700726 return;
727 }
728
K. Y. Srinivasan132368b2012-12-01 06:46:33 -0800729 /*
730 * By default we setup state to enable batched
731 * reading. A specific service can choose to
732 * disable this prior to opening the channel.
733 */
734 newchannel->batched_reading = true;
735
K. Y. Srinivasanb3bf60c2012-12-01 06:46:45 -0800736 /*
737 * Setup state for signalling the host.
738 */
739 newchannel->sig_event = (struct hv_input_signal_event *)
740 (ALIGN((unsigned long)
741 &newchannel->sig_buf,
742 HV_HYPERCALL_PARAM_ALIGN));
743
744 newchannel->sig_event->connectionid.asu32 = 0;
745 newchannel->sig_event->connectionid.u.id = VMBUS_EVENT_CONNECTION_ID;
746 newchannel->sig_event->flag_number = 0;
747 newchannel->sig_event->rsvdz = 0;
748
749 if (vmbus_proto_version != VERSION_WS2008) {
750 newchannel->is_dedicated_interrupt =
751 (offer->is_dedicated_interrupt != 0);
752 newchannel->sig_event->connectionid.u.id =
753 offer->connection_id;
754 }
755
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800756 memcpy(&newchannel->offermsg, offer,
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700757 sizeof(struct vmbus_channel_offer_channel));
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800758 newchannel->monitor_grp = (u8)offer->monitorid / 32;
759 newchannel->monitor_bit = (u8)offer->monitorid % 32;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700760
K. Y. Srinivasan2dd37cb2015-02-28 11:18:18 -0800761 vmbus_process_offer(newchannel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700762}
763
Hank Janssen3e189512010-03-04 22:11:00 +0000764/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700765 * vmbus_onoffer_rescind - Rescind offer handler.
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700766 *
767 * We queue a work item to process this offer synchronously
768 */
Haiyang Zhange98cb272010-10-15 10:14:07 -0700769static void vmbus_onoffer_rescind(struct vmbus_channel_message_header *hdr)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700770{
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700771 struct vmbus_channel_rescind_offer *rescind;
Greg Kroah-Hartmanaded7162009-08-18 15:21:19 -0700772 struct vmbus_channel *channel;
Dexuan Cuid43e2fe2015-03-27 09:10:09 -0700773 unsigned long flags;
774 struct device *dev;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700775
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700776 rescind = (struct vmbus_channel_rescind_offer *)hdr;
Dexuan Cui85d9aa72016-01-27 22:29:43 -0800777
778 mutex_lock(&vmbus_connection.channel_mutex);
Dexuan Cuid43e2fe2015-03-27 09:10:09 -0700779 channel = relid2channel(rescind->child_relid);
Hank Janssen98e08702011-03-29 13:58:44 -0700780
K. Y. Srinivasan2dd37cb2015-02-28 11:18:18 -0800781 if (channel == NULL) {
Dexuan Cuif52078c2015-12-14 16:01:50 -0800782 /*
783 * This is very impossible, because in
784 * vmbus_process_offer(), we have already invoked
785 * vmbus_release_relid() on error.
786 */
Dexuan Cui85d9aa72016-01-27 22:29:43 -0800787 goto out;
K. Y. Srinivasan2dd37cb2015-02-28 11:18:18 -0800788 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700789
Dexuan Cuid43e2fe2015-03-27 09:10:09 -0700790 spin_lock_irqsave(&channel->lock, flags);
791 channel->rescind = true;
792 spin_unlock_irqrestore(&channel->lock, flags);
793
794 if (channel->device_obj) {
Dexuan Cui499e8402016-01-27 22:29:42 -0800795 if (channel->chn_rescind_callback) {
796 channel->chn_rescind_callback(channel);
Dexuan Cui85d9aa72016-01-27 22:29:43 -0800797 goto out;
Dexuan Cui499e8402016-01-27 22:29:42 -0800798 }
Dexuan Cuid43e2fe2015-03-27 09:10:09 -0700799 /*
800 * We will have to unregister this device from the
801 * driver core.
802 */
803 dev = get_device(&channel->device_obj->device);
804 if (dev) {
805 vmbus_device_unregister(channel->device_obj);
806 put_device(dev);
807 }
808 } else {
809 hv_process_channel_removal(channel,
810 channel->offermsg.child_relid);
K. Y. Srinivasan2dd37cb2015-02-28 11:18:18 -0800811 }
Dexuan Cui85d9aa72016-01-27 22:29:43 -0800812
813out:
814 mutex_unlock(&vmbus_connection.channel_mutex);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700815}
816
Dexuan Cui85d9aa72016-01-27 22:29:43 -0800817void vmbus_hvsock_device_unregister(struct vmbus_channel *channel)
818{
819 mutex_lock(&vmbus_connection.channel_mutex);
820
821 BUG_ON(!is_hvsock_channel(channel));
822
823 channel->rescind = true;
824 vmbus_device_unregister(channel->device_obj);
825
826 mutex_unlock(&vmbus_connection.channel_mutex);
827}
828EXPORT_SYMBOL_GPL(vmbus_hvsock_device_unregister);
829
830
Hank Janssen3e189512010-03-04 22:11:00 +0000831/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700832 * vmbus_onoffers_delivered -
833 * This is invoked when all offers have been delivered.
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700834 *
835 * Nothing to do here.
836 */
Haiyang Zhange98cb272010-10-15 10:14:07 -0700837static void vmbus_onoffers_delivered(
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700838 struct vmbus_channel_message_header *hdr)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700839{
Hank Janssen3e7ee492009-07-13 16:02:34 -0700840}
841
Hank Janssen3e189512010-03-04 22:11:00 +0000842/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700843 * vmbus_onopen_result - Open result handler.
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700844 *
845 * This is invoked when we received a response to our channel open request.
846 * Find the matching request, copy the response and signal the requesting
847 * thread.
848 */
Haiyang Zhange98cb272010-10-15 10:14:07 -0700849static void vmbus_onopen_result(struct vmbus_channel_message_header *hdr)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700850{
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700851 struct vmbus_channel_open_result *result;
Haiyang Zhang188963e2010-10-15 10:14:06 -0700852 struct vmbus_channel_msginfo *msginfo;
853 struct vmbus_channel_message_header *requestheader;
854 struct vmbus_channel_open_channel *openmsg;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700855 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700856
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700857 result = (struct vmbus_channel_open_result *)hdr;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700858
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700859 /*
860 * Find the open msg, copy the result and signal/unblock the wait event
861 */
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800862 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700863
Hank Janssenebb61e52011-02-18 12:39:57 -0800864 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
865 msglistentry) {
Haiyang Zhang188963e2010-10-15 10:14:06 -0700866 requestheader =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800867 (struct vmbus_channel_message_header *)msginfo->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700868
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800869 if (requestheader->msgtype == CHANNELMSG_OPENCHANNEL) {
Haiyang Zhang188963e2010-10-15 10:14:06 -0700870 openmsg =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800871 (struct vmbus_channel_open_channel *)msginfo->msg;
872 if (openmsg->child_relid == result->child_relid &&
873 openmsg->openid == result->openid) {
874 memcpy(&msginfo->response.open_result,
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700875 result,
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700876 sizeof(
877 struct vmbus_channel_open_result));
878 complete(&msginfo->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700879 break;
880 }
881 }
882 }
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800883 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700884}
885
Hank Janssen3e189512010-03-04 22:11:00 +0000886/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700887 * vmbus_ongpadl_created - GPADL created handler.
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700888 *
889 * This is invoked when we received a response to our gpadl create request.
890 * Find the matching request, copy the response and signal the requesting
891 * thread.
892 */
Haiyang Zhange98cb272010-10-15 10:14:07 -0700893static void vmbus_ongpadl_created(struct vmbus_channel_message_header *hdr)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700894{
Haiyang Zhang188963e2010-10-15 10:14:06 -0700895 struct vmbus_channel_gpadl_created *gpadlcreated;
Haiyang Zhang188963e2010-10-15 10:14:06 -0700896 struct vmbus_channel_msginfo *msginfo;
897 struct vmbus_channel_message_header *requestheader;
898 struct vmbus_channel_gpadl_header *gpadlheader;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700899 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700900
Haiyang Zhang188963e2010-10-15 10:14:06 -0700901 gpadlcreated = (struct vmbus_channel_gpadl_created *)hdr;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700902
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700903 /*
904 * Find the establish msg, copy the result and signal/unblock the wait
905 * event
906 */
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800907 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700908
Hank Janssenebb61e52011-02-18 12:39:57 -0800909 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
910 msglistentry) {
Haiyang Zhang188963e2010-10-15 10:14:06 -0700911 requestheader =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800912 (struct vmbus_channel_message_header *)msginfo->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700913
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800914 if (requestheader->msgtype == CHANNELMSG_GPADL_HEADER) {
Haiyang Zhang188963e2010-10-15 10:14:06 -0700915 gpadlheader =
916 (struct vmbus_channel_gpadl_header *)requestheader;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700917
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800918 if ((gpadlcreated->child_relid ==
919 gpadlheader->child_relid) &&
920 (gpadlcreated->gpadl == gpadlheader->gpadl)) {
921 memcpy(&msginfo->response.gpadl_created,
Haiyang Zhang188963e2010-10-15 10:14:06 -0700922 gpadlcreated,
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700923 sizeof(
924 struct vmbus_channel_gpadl_created));
925 complete(&msginfo->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700926 break;
927 }
928 }
929 }
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800930 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700931}
932
Hank Janssen3e189512010-03-04 22:11:00 +0000933/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700934 * vmbus_ongpadl_torndown - GPADL torndown handler.
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700935 *
936 * This is invoked when we received a response to our gpadl teardown request.
937 * Find the matching request, copy the response and signal the requesting
938 * thread.
939 */
Haiyang Zhange98cb272010-10-15 10:14:07 -0700940static void vmbus_ongpadl_torndown(
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700941 struct vmbus_channel_message_header *hdr)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700942{
Haiyang Zhang188963e2010-10-15 10:14:06 -0700943 struct vmbus_channel_gpadl_torndown *gpadl_torndown;
Haiyang Zhang188963e2010-10-15 10:14:06 -0700944 struct vmbus_channel_msginfo *msginfo;
945 struct vmbus_channel_message_header *requestheader;
946 struct vmbus_channel_gpadl_teardown *gpadl_teardown;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700947 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700948
Haiyang Zhang188963e2010-10-15 10:14:06 -0700949 gpadl_torndown = (struct vmbus_channel_gpadl_torndown *)hdr;
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700950
951 /*
952 * Find the open msg, copy the result and signal/unblock the wait event
953 */
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800954 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700955
Hank Janssenebb61e52011-02-18 12:39:57 -0800956 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
957 msglistentry) {
Haiyang Zhang188963e2010-10-15 10:14:06 -0700958 requestheader =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800959 (struct vmbus_channel_message_header *)msginfo->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700960
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800961 if (requestheader->msgtype == CHANNELMSG_GPADL_TEARDOWN) {
Haiyang Zhang188963e2010-10-15 10:14:06 -0700962 gpadl_teardown =
963 (struct vmbus_channel_gpadl_teardown *)requestheader;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700964
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800965 if (gpadl_torndown->gpadl == gpadl_teardown->gpadl) {
966 memcpy(&msginfo->response.gpadl_torndown,
Haiyang Zhang188963e2010-10-15 10:14:06 -0700967 gpadl_torndown,
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700968 sizeof(
969 struct vmbus_channel_gpadl_torndown));
970 complete(&msginfo->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700971 break;
972 }
973 }
974 }
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800975 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700976}
977
Hank Janssen3e189512010-03-04 22:11:00 +0000978/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700979 * vmbus_onversion_response - Version response handler
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700980 *
981 * This is invoked when we received a response to our initiate contact request.
982 * Find the matching request, copy the response and signal the requesting
983 * thread.
984 */
Haiyang Zhange98cb272010-10-15 10:14:07 -0700985static void vmbus_onversion_response(
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700986 struct vmbus_channel_message_header *hdr)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700987{
Haiyang Zhang188963e2010-10-15 10:14:06 -0700988 struct vmbus_channel_msginfo *msginfo;
989 struct vmbus_channel_message_header *requestheader;
Haiyang Zhang188963e2010-10-15 10:14:06 -0700990 struct vmbus_channel_version_response *version_response;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700991 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700992
Haiyang Zhang188963e2010-10-15 10:14:06 -0700993 version_response = (struct vmbus_channel_version_response *)hdr;
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800994 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700995
Hank Janssenebb61e52011-02-18 12:39:57 -0800996 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
997 msglistentry) {
Haiyang Zhang188963e2010-10-15 10:14:06 -0700998 requestheader =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800999 (struct vmbus_channel_message_header *)msginfo->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001000
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -08001001 if (requestheader->msgtype ==
1002 CHANNELMSG_INITIATE_CONTACT) {
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -08001003 memcpy(&msginfo->response.version_response,
Haiyang Zhang188963e2010-10-15 10:14:06 -07001004 version_response,
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -07001005 sizeof(struct vmbus_channel_version_response));
K. Y. Srinivasan9568a192011-05-10 07:55:39 -07001006 complete(&msginfo->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001007 }
1008 }
Haiyang Zhang15b2f642011-01-26 12:12:07 -08001009 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001010}
1011
Greg Kroah-Hartmanc8212f02009-08-31 21:51:50 -07001012/* Channel message dispatch table */
Dexuan Cui652594c2015-03-27 09:10:08 -07001013struct vmbus_channel_message_table_entry
K. Y. Srinivasanb7c6b022011-05-10 07:55:38 -07001014 channel_message_table[CHANNELMSG_COUNT] = {
Dexuan Cui652594c2015-03-27 09:10:08 -07001015 {CHANNELMSG_INVALID, 0, NULL},
1016 {CHANNELMSG_OFFERCHANNEL, 0, vmbus_onoffer},
1017 {CHANNELMSG_RESCIND_CHANNELOFFER, 0, vmbus_onoffer_rescind},
1018 {CHANNELMSG_REQUESTOFFERS, 0, NULL},
1019 {CHANNELMSG_ALLOFFERS_DELIVERED, 1, vmbus_onoffers_delivered},
1020 {CHANNELMSG_OPENCHANNEL, 0, NULL},
1021 {CHANNELMSG_OPENCHANNEL_RESULT, 1, vmbus_onopen_result},
1022 {CHANNELMSG_CLOSECHANNEL, 0, NULL},
1023 {CHANNELMSG_GPADL_HEADER, 0, NULL},
1024 {CHANNELMSG_GPADL_BODY, 0, NULL},
1025 {CHANNELMSG_GPADL_CREATED, 1, vmbus_ongpadl_created},
1026 {CHANNELMSG_GPADL_TEARDOWN, 0, NULL},
1027 {CHANNELMSG_GPADL_TORNDOWN, 1, vmbus_ongpadl_torndown},
1028 {CHANNELMSG_RELID_RELEASED, 0, NULL},
1029 {CHANNELMSG_INITIATE_CONTACT, 0, NULL},
1030 {CHANNELMSG_VERSION_RESPONSE, 1, vmbus_onversion_response},
1031 {CHANNELMSG_UNLOAD, 0, NULL},
K. Y. Srinivasan2db84ef2015-04-22 21:31:32 -07001032 {CHANNELMSG_UNLOAD_RESPONSE, 1, vmbus_unload_response},
Dexuan Cui5c23a1a2016-01-27 22:29:40 -08001033 {CHANNELMSG_18, 0, NULL},
1034 {CHANNELMSG_19, 0, NULL},
1035 {CHANNELMSG_20, 0, NULL},
1036 {CHANNELMSG_TL_CONNECT_REQUEST, 0, NULL},
Greg Kroah-Hartmanc8212f02009-08-31 21:51:50 -07001037};
1038
Hank Janssen3e189512010-03-04 22:11:00 +00001039/*
Haiyang Zhange98cb272010-10-15 10:14:07 -07001040 * vmbus_onmessage - Handler for channel protocol messages.
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -07001041 *
1042 * This is invoked in the vmbus worker thread context.
1043 */
Haiyang Zhange98cb272010-10-15 10:14:07 -07001044void vmbus_onmessage(void *context)
Hank Janssen3e7ee492009-07-13 16:02:34 -07001045{
Haiyang Zhang188963e2010-10-15 10:14:06 -07001046 struct hv_message *msg = context;
Greg Kroah-Hartman82250212009-08-26 15:16:04 -07001047 struct vmbus_channel_message_header *hdr;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001048 int size;
1049
Haiyang Zhangf6feebe2010-11-08 14:04:39 -08001050 hdr = (struct vmbus_channel_message_header *)msg->u.payload;
1051 size = msg->header.payload_size;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001052
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -08001053 if (hdr->msgtype >= CHANNELMSG_COUNT) {
Hank Janssen0a466182011-03-29 13:58:47 -07001054 pr_err("Received invalid channel message type %d size %d\n",
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -08001055 hdr->msgtype, size);
Greg Kroah-Hartman04f50c42009-07-16 12:35:37 -07001056 print_hex_dump_bytes("", DUMP_PREFIX_NONE,
Haiyang Zhangf6feebe2010-11-08 14:04:39 -08001057 (unsigned char *)msg->u.payload, size);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001058 return;
1059 }
1060
K. Y. Srinivasanb7c6b022011-05-10 07:55:38 -07001061 if (channel_message_table[hdr->msgtype].message_handler)
1062 channel_message_table[hdr->msgtype].message_handler(hdr);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001063 else
Hank Janssen0a466182011-03-29 13:58:47 -07001064 pr_err("Unhandled channel message type %d\n", hdr->msgtype);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001065}
1066
Hank Janssen3e189512010-03-04 22:11:00 +00001067/*
Haiyang Zhange98cb272010-10-15 10:14:07 -07001068 * vmbus_request_offers - Send a request to get all our pending offers.
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -07001069 */
Haiyang Zhange98cb272010-10-15 10:14:07 -07001070int vmbus_request_offers(void)
Hank Janssen3e7ee492009-07-13 16:02:34 -07001071{
Greg Kroah-Hartman82250212009-08-26 15:16:04 -07001072 struct vmbus_channel_message_header *msg;
Haiyang Zhang188963e2010-10-15 10:14:06 -07001073 struct vmbus_channel_msginfo *msginfo;
Nicholas Mc Guire51e51812015-02-27 11:26:02 -08001074 int ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001075
Haiyang Zhang188963e2010-10-15 10:14:06 -07001076 msginfo = kmalloc(sizeof(*msginfo) +
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -07001077 sizeof(struct vmbus_channel_message_header),
1078 GFP_KERNEL);
Haiyang Zhang188963e2010-10-15 10:14:06 -07001079 if (!msginfo)
Bill Pemberton75910f22010-05-05 15:27:31 -04001080 return -ENOMEM;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001081
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -08001082 msg = (struct vmbus_channel_message_header *)msginfo->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001083
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -08001084 msg->msgtype = CHANNELMSG_REQUESTOFFERS;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001085
Hank Janssen3e7ee492009-07-13 16:02:34 -07001086
Haiyang Zhangc6977672011-01-26 12:12:08 -08001087 ret = vmbus_post_msg(msg,
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -07001088 sizeof(struct vmbus_channel_message_header));
1089 if (ret != 0) {
Hank Janssen0a466182011-03-29 13:58:47 -07001090 pr_err("Unable to request offers - %d\n", ret);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001091
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -08001092 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001093 }
Hank Janssen3e7ee492009-07-13 16:02:34 -07001094
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -08001095cleanup:
Ilia Mirkindd9b15d2011-03-13 00:29:00 -05001096 kfree(msginfo);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001097
Hank Janssen3e7ee492009-07-13 16:02:34 -07001098 return ret;
1099}
1100
K. Y. Srinivasane68d2972013-05-23 12:02:32 -07001101/*
1102 * Retrieve the (sub) channel on which to send an outgoing request.
K. Y. Srinivasana13e8bb2015-02-28 11:39:02 -08001103 * When a primary channel has multiple sub-channels, we try to
1104 * distribute the load equally amongst all available channels.
K. Y. Srinivasane68d2972013-05-23 12:02:32 -07001105 */
1106struct vmbus_channel *vmbus_get_outgoing_channel(struct vmbus_channel *primary)
1107{
1108 struct list_head *cur, *tmp;
K. Y. Srinivasan87712bf82014-12-14 23:34:51 -08001109 int cur_cpu;
K. Y. Srinivasane68d2972013-05-23 12:02:32 -07001110 struct vmbus_channel *cur_channel;
1111 struct vmbus_channel *outgoing_channel = primary;
K. Y. Srinivasana13e8bb2015-02-28 11:39:02 -08001112 int next_channel;
1113 int i = 1;
K. Y. Srinivasane68d2972013-05-23 12:02:32 -07001114
1115 if (list_empty(&primary->sc_list))
1116 return outgoing_channel;
1117
K. Y. Srinivasana13e8bb2015-02-28 11:39:02 -08001118 next_channel = primary->next_oc++;
1119
1120 if (next_channel > (primary->num_sc)) {
1121 primary->next_oc = 0;
1122 return outgoing_channel;
1123 }
1124
K. Y. Srinivasan87712bf82014-12-14 23:34:51 -08001125 cur_cpu = hv_context.vp_index[get_cpu()];
1126 put_cpu();
K. Y. Srinivasane68d2972013-05-23 12:02:32 -07001127 list_for_each_safe(cur, tmp, &primary->sc_list) {
1128 cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
1129 if (cur_channel->state != CHANNEL_OPENED_STATE)
1130 continue;
1131
1132 if (cur_channel->target_vp == cur_cpu)
1133 return cur_channel;
1134
K. Y. Srinivasana13e8bb2015-02-28 11:39:02 -08001135 if (i == next_channel)
1136 return cur_channel;
K. Y. Srinivasane68d2972013-05-23 12:02:32 -07001137
K. Y. Srinivasana13e8bb2015-02-28 11:39:02 -08001138 i++;
K. Y. Srinivasane68d2972013-05-23 12:02:32 -07001139 }
1140
1141 return outgoing_channel;
1142}
1143EXPORT_SYMBOL_GPL(vmbus_get_outgoing_channel);
1144
1145static void invoke_sc_cb(struct vmbus_channel *primary_channel)
1146{
1147 struct list_head *cur, *tmp;
1148 struct vmbus_channel *cur_channel;
1149
1150 if (primary_channel->sc_creation_callback == NULL)
1151 return;
1152
1153 list_for_each_safe(cur, tmp, &primary_channel->sc_list) {
1154 cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
1155
1156 primary_channel->sc_creation_callback(cur_channel);
1157 }
1158}
1159
1160void vmbus_set_sc_create_callback(struct vmbus_channel *primary_channel,
1161 void (*sc_cr_cb)(struct vmbus_channel *new_sc))
1162{
1163 primary_channel->sc_creation_callback = sc_cr_cb;
1164}
1165EXPORT_SYMBOL_GPL(vmbus_set_sc_create_callback);
1166
1167bool vmbus_are_subchannels_present(struct vmbus_channel *primary)
1168{
1169 bool ret;
1170
1171 ret = !list_empty(&primary->sc_list);
1172
1173 if (ret) {
1174 /*
1175 * Invoke the callback on sub-channel creation.
1176 * This will present a uniform interface to the
1177 * clients.
1178 */
1179 invoke_sc_cb(primary);
1180 }
1181
1182 return ret;
1183}
1184EXPORT_SYMBOL_GPL(vmbus_are_subchannels_present);
Dexuan Cui499e8402016-01-27 22:29:42 -08001185
1186void vmbus_set_chn_rescind_callback(struct vmbus_channel *channel,
1187 void (*chn_rescind_cb)(struct vmbus_channel *))
1188{
1189 channel->chn_rescind_callback = chn_rescind_cb;
1190}
1191EXPORT_SYMBOL_GPL(vmbus_set_chn_rescind_callback);