blob: f3a8b52acb5163b57835fc02a0cb591e51e80a94 [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
Dexuan Cui0f988292016-09-07 05:39:34 -0700142static const struct {
143 uuid_le guid;
144} vmbus_unsupported_devs[] = {
145 { HV_AVMA1_GUID },
146 { HV_AVMA2_GUID },
147 { HV_RDV_GUID },
148};
149
150static bool is_unsupported_vmbus_devs(const uuid_le *guid)
K. Y. Srinivasan7047f172015-12-25 20:00:30 -0800151{
Dexuan Cui0f988292016-09-07 05:39:34 -0700152 int i;
153
154 for (i = 0; i < ARRAY_SIZE(vmbus_unsupported_devs); i++)
155 if (!uuid_le_cmp(*guid, vmbus_unsupported_devs[i].guid))
156 return true;
157 return false;
158}
159
160static u16 hv_get_dev_type(const struct vmbus_channel *channel)
161{
162 const uuid_le *guid = &channel->offermsg.offer.if_type;
K. Y. Srinivasan7047f172015-12-25 20:00:30 -0800163 u16 i;
164
Dexuan Cui0f988292016-09-07 05:39:34 -0700165 if (is_hvsock_channel(channel) || is_unsupported_vmbus_devs(guid))
166 return HV_UNKOWN;
167
K. Y. Srinivasan7047f172015-12-25 20:00:30 -0800168 for (i = HV_IDE; i < HV_UNKOWN; i++) {
169 if (!uuid_le_cmp(*guid, vmbus_devs[i].guid))
170 return i;
171 }
172 pr_info("Unknown GUID: %pUl\n", guid);
173 return i;
174}
Vitaly Kuznetsovf38e7dd2015-05-06 17:47:45 -0700175
Hank Janssenc88c4e42010-05-04 15:55:05 -0700176/**
Greg Kroah-Hartmanda0e9632011-10-11 08:42:28 -0600177 * vmbus_prep_negotiate_resp() - Create default response for Hyper-V Negotiate message
Hank Janssenc88c4e42010-05-04 15:55:05 -0700178 * @icmsghdrp: Pointer to msg header structure
179 * @icmsg_negotiate: Pointer to negotiate message structure
180 * @buf: Raw buffer channel data
181 *
182 * @icmsghdrp is of type &struct icmsg_hdr.
183 * @negop is of type &struct icmsg_negotiate.
K. Y. Srinivasanc836d0a2012-05-12 13:44:58 -0700184 * Set up and fill in default negotiate response message.
185 *
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700186 * The fw_version specifies the framework version that
187 * we can support and srv_version specifies the service
188 * version we can support.
Hank Janssenc88c4e42010-05-04 15:55:05 -0700189 *
190 * Mainly used by Hyper-V drivers.
191 */
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700192bool vmbus_prep_negotiate_resp(struct icmsg_hdr *icmsghdrp,
K. Y. Srinivasanc836d0a2012-05-12 13:44:58 -0700193 struct icmsg_negotiate *negop, u8 *buf,
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700194 int fw_version, int srv_version)
Hank Janssenc88c4e42010-05-04 15:55:05 -0700195{
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700196 int icframe_major, icframe_minor;
197 int icmsg_major, icmsg_minor;
198 int fw_major, fw_minor;
199 int srv_major, srv_minor;
K. Y. Srinivasanc836d0a2012-05-12 13:44:58 -0700200 int i;
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700201 bool found_match = false;
K. Y. Srinivasanc836d0a2012-05-12 13:44:58 -0700202
K. Y. Srinivasana3605302012-05-12 13:44:57 -0700203 icmsghdrp->icmsgsize = 0x10;
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700204 fw_major = (fw_version >> 16);
205 fw_minor = (fw_version & 0xFFFF);
206
207 srv_major = (srv_version >> 16);
208 srv_minor = (srv_version & 0xFFFF);
Hank Janssenc88c4e42010-05-04 15:55:05 -0700209
K. Y. Srinivasana3605302012-05-12 13:44:57 -0700210 negop = (struct icmsg_negotiate *)&buf[
211 sizeof(struct vmbuspipe_hdr) +
212 sizeof(struct icmsg_hdr)];
Hank Janssenc88c4e42010-05-04 15:55:05 -0700213
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700214 icframe_major = negop->icframe_vercnt;
215 icframe_minor = 0;
216
217 icmsg_major = negop->icmsg_vercnt;
218 icmsg_minor = 0;
K. Y. Srinivasanc836d0a2012-05-12 13:44:58 -0700219
220 /*
221 * Select the framework version number we will
222 * support.
223 */
224
225 for (i = 0; i < negop->icframe_vercnt; i++) {
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700226 if ((negop->icversion_data[i].major == fw_major) &&
227 (negop->icversion_data[i].minor == fw_minor)) {
228 icframe_major = negop->icversion_data[i].major;
229 icframe_minor = negop->icversion_data[i].minor;
230 found_match = true;
231 }
Hank Janssenc88c4e42010-05-04 15:55:05 -0700232 }
K. Y. Srinivasana3605302012-05-12 13:44:57 -0700233
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700234 if (!found_match)
235 goto fw_error;
236
237 found_match = false;
238
K. Y. Srinivasanc836d0a2012-05-12 13:44:58 -0700239 for (i = negop->icframe_vercnt;
240 (i < negop->icframe_vercnt + negop->icmsg_vercnt); i++) {
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700241 if ((negop->icversion_data[i].major == srv_major) &&
242 (negop->icversion_data[i].minor == srv_minor)) {
243 icmsg_major = negop->icversion_data[i].major;
244 icmsg_minor = negop->icversion_data[i].minor;
245 found_match = true;
246 }
K. Y. Srinivasanc836d0a2012-05-12 13:44:58 -0700247 }
248
249 /*
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700250 * Respond with the framework and service
K. Y. Srinivasanc836d0a2012-05-12 13:44:58 -0700251 * version numbers we can support.
252 */
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700253
254fw_error:
255 if (!found_match) {
256 negop->icframe_vercnt = 0;
257 negop->icmsg_vercnt = 0;
258 } else {
259 negop->icframe_vercnt = 1;
260 negop->icmsg_vercnt = 1;
261 }
262
263 negop->icversion_data[0].major = icframe_major;
264 negop->icversion_data[0].minor = icframe_minor;
265 negop->icversion_data[1].major = icmsg_major;
266 negop->icversion_data[1].minor = icmsg_minor;
267 return found_match;
Hank Janssenc88c4e42010-05-04 15:55:05 -0700268}
K. Y. Srinivasana3605302012-05-12 13:44:57 -0700269
Greg Kroah-Hartmanda0e9632011-10-11 08:42:28 -0600270EXPORT_SYMBOL_GPL(vmbus_prep_negotiate_resp);
Hank Janssenc88c4e42010-05-04 15:55:05 -0700271
Hank Janssen3e189512010-03-04 22:11:00 +0000272/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700273 * alloc_channel - Allocate and initialize a vmbus channel object
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700274 */
Greg Kroah-Hartman50fe56d2010-10-20 15:51:57 -0700275static struct vmbus_channel *alloc_channel(void)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700276{
Greg Kroah-Hartmanaded71652009-08-18 15:21:19 -0700277 struct vmbus_channel *channel;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700278
Greg Kroah-Hartmanaded71652009-08-18 15:21:19 -0700279 channel = kzalloc(sizeof(*channel), GFP_ATOMIC);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700280 if (!channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700281 return NULL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700282
K. Y. Srinivasanfe760e42016-01-27 22:29:45 -0800283 channel->acquire_ring_lock = true;
Greg Kroah-Hartman54411c42009-07-15 14:48:32 -0700284 spin_lock_init(&channel->inbound_lock);
Vitaly Kuznetsov67fae052015-01-20 16:45:05 +0100285 spin_lock_init(&channel->lock);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700286
287 INIT_LIST_HEAD(&channel->sc_list);
K. Y. Srinivasan3a28fa32014-04-08 18:45:54 -0700288 INIT_LIST_HEAD(&channel->percpu_list);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700289
Hank Janssen3e7ee492009-07-13 16:02:34 -0700290 return channel;
291}
292
Hank Janssen3e189512010-03-04 22:11:00 +0000293/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700294 * free_channel - Release the resources used by the vmbus channel object
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700295 */
Greg Kroah-Hartman9f3e28e2011-10-11 09:40:01 -0600296static void free_channel(struct vmbus_channel *channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700297{
Dexuan Cuiaadc3782015-03-27 09:10:10 -0700298 kfree(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700299}
300
K. Y. Srinivasan3a28fa32014-04-08 18:45:54 -0700301static void percpu_channel_enq(void *arg)
302{
303 struct vmbus_channel *channel = arg;
304 int cpu = smp_processor_id();
Haiyang Zhang8b5d6d32010-05-28 23:22:44 +0000305
K. Y. Srinivasan3a28fa32014-04-08 18:45:54 -0700306 list_add_tail(&channel->percpu_list, &hv_context.percpu_list[cpu]);
307}
308
309static void percpu_channel_deq(void *arg)
310{
311 struct vmbus_channel *channel = arg;
312
313 list_del(&channel->percpu_list);
314}
Haiyang Zhang8b5d6d32010-05-28 23:22:44 +0000315
K. Y. Srinivasaned6cfcc2015-02-28 11:18:17 -0800316
Dexuan Cuif52078c2015-12-14 16:01:50 -0800317static void vmbus_release_relid(u32 relid)
Timo Teräs4b2f9ab2010-12-15 20:48:09 +0200318{
K. Y. Srinivasaned6cfcc2015-02-28 11:18:17 -0800319 struct vmbus_channel_relid_released msg;
Vitaly Kuznetsov04a258c2014-11-04 13:40:11 +0100320
K. Y. Srinivasanc8705972013-03-15 12:25:44 -0700321 memset(&msg, 0, sizeof(struct vmbus_channel_relid_released));
K. Y. Srinivasaned6cfcc2015-02-28 11:18:17 -0800322 msg.child_relid = relid;
K. Y. Srinivasanc8705972013-03-15 12:25:44 -0700323 msg.header.msgtype = CHANNELMSG_RELID_RELEASED;
Vitaly Kuznetsov65013a92016-12-07 01:16:24 -0800324 vmbus_post_msg(&msg, sizeof(struct vmbus_channel_relid_released),
325 true);
Dexuan Cuif52078c2015-12-14 16:01:50 -0800326}
K. Y. Srinivasanc8705972013-03-15 12:25:44 -0700327
Dexuan Cui638fea32016-06-09 18:47:24 -0700328void hv_event_tasklet_disable(struct vmbus_channel *channel)
329{
330 struct tasklet_struct *tasklet;
331 tasklet = hv_context.event_dpc[channel->target_cpu];
332 tasklet_disable(tasklet);
333}
334
335void hv_event_tasklet_enable(struct vmbus_channel *channel)
336{
337 struct tasklet_struct *tasklet;
338 tasklet = hv_context.event_dpc[channel->target_cpu];
339 tasklet_enable(tasklet);
340
341 /* In case there is any pending event */
342 tasklet_schedule(tasklet);
343}
344
Dexuan Cuif52078c2015-12-14 16:01:50 -0800345void hv_process_channel_removal(struct vmbus_channel *channel, u32 relid)
346{
347 unsigned long flags;
348 struct vmbus_channel *primary_channel;
349
Dexuan Cui34c68012015-12-14 16:01:49 -0800350 BUG_ON(!channel->rescind);
Dexuan Cui85d9aa72016-01-27 22:29:43 -0800351 BUG_ON(!mutex_is_locked(&vmbus_connection.channel_mutex));
Dexuan Cui34c68012015-12-14 16:01:49 -0800352
Dexuan Cui638fea32016-06-09 18:47:24 -0700353 hv_event_tasklet_disable(channel);
K. Y. Srinivasan2115b562014-08-28 18:29:53 -0700354 if (channel->target_cpu != get_cpu()) {
355 put_cpu();
K. Y. Srinivasan3a28fa32014-04-08 18:45:54 -0700356 smp_call_function_single(channel->target_cpu,
357 percpu_channel_deq, channel, true);
K. Y. Srinivasan2115b562014-08-28 18:29:53 -0700358 } else {
K. Y. Srinivasan3a28fa32014-04-08 18:45:54 -0700359 percpu_channel_deq(channel);
K. Y. Srinivasan2115b562014-08-28 18:29:53 -0700360 put_cpu();
361 }
Dexuan Cui638fea32016-06-09 18:47:24 -0700362 hv_event_tasklet_enable(channel);
K. Y. Srinivasan3a28fa32014-04-08 18:45:54 -0700363
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700364 if (channel->primary_channel == NULL) {
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700365 list_del(&channel->listentry);
Dexuan Cuica1c4b72015-08-13 17:07:03 -0700366
367 primary_channel = channel;
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700368 } else {
369 primary_channel = channel->primary_channel;
Vitaly Kuznetsov67fae052015-01-20 16:45:05 +0100370 spin_lock_irqsave(&primary_channel->lock, flags);
K. Y. Srinivasan565ce642013-10-16 19:27:19 -0700371 list_del(&channel->sc_list);
Vitaly Kuznetsov357e8362015-05-06 17:47:44 -0700372 primary_channel->num_sc--;
Vitaly Kuznetsov67fae052015-01-20 16:45:05 +0100373 spin_unlock_irqrestore(&primary_channel->lock, flags);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700374 }
Dexuan Cuica1c4b72015-08-13 17:07:03 -0700375
376 /*
377 * We need to free the bit for init_vp_index() to work in the case
378 * of sub-channel, when we reload drivers like hv_netvsc.
379 */
K. Y. Srinivasan509879b2016-09-02 05:58:23 -0700380 if (channel->affinity_policy == HV_LOCALIZED)
381 cpumask_clear_cpu(channel->target_cpu,
382 &primary_channel->alloced_cpus_in_node);
Dexuan Cuica1c4b72015-08-13 17:07:03 -0700383
Dexuan Cui638fea32016-06-09 18:47:24 -0700384 vmbus_release_relid(relid);
385
K. Y. Srinivasanc8705972013-03-15 12:25:44 -0700386 free_channel(channel);
Timo Teräs4b2f9ab2010-12-15 20:48:09 +0200387}
Haiyang Zhang8b5d6d32010-05-28 23:22:44 +0000388
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -0800389void vmbus_free_channels(void)
390{
Dexuan Cui813c5b72015-04-22 21:31:30 -0700391 struct vmbus_channel *channel, *tmp;
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -0800392
Vitaly Kuznetsovbf6a9b32016-12-03 12:34:32 -0800393 mutex_lock(&vmbus_connection.channel_mutex);
Dexuan Cui813c5b72015-04-22 21:31:30 -0700394 list_for_each_entry_safe(channel, tmp, &vmbus_connection.chn_list,
395 listentry) {
Dexuan Cui34c68012015-12-14 16:01:49 -0800396 /* hv_process_channel_removal() needs this */
Dexuan Cui813c5b72015-04-22 21:31:30 -0700397 channel->rescind = true;
398
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -0800399 vmbus_device_unregister(channel->device_obj);
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -0800400 }
Vitaly Kuznetsovbf6a9b32016-12-03 12:34:32 -0800401 mutex_unlock(&vmbus_connection.channel_mutex);
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -0800402}
403
Hank Janssen3e189512010-03-04 22:11:00 +0000404/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700405 * vmbus_process_offer - Process the offer by creating a channel/device
Hank Janssenc88c4e42010-05-04 15:55:05 -0700406 * associated with this offer
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700407 */
K. Y. Srinivasan2dd37cb2015-02-28 11:18:18 -0800408static void vmbus_process_offer(struct vmbus_channel *newchannel)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700409{
Greg Kroah-Hartmanaded71652009-08-18 15:21:19 -0700410 struct vmbus_channel *channel;
Haiyang Zhang188963e2010-10-15 10:14:06 -0700411 bool fnew = true;
Greg Kroah-Hartman0f5e44c2009-07-15 14:57:16 -0700412 unsigned long flags;
K. Y. Srinivasan7047f172015-12-25 20:00:30 -0800413 u16 dev_type;
Dexuan Cui85d9aa72016-01-27 22:29:43 -0800414 int ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700415
Bill Pemberton454f18a2009-07-27 16:47:24 -0400416 /* Make sure this is a new offer */
Dexuan Cuid6f591e2015-12-14 16:01:51 -0800417 mutex_lock(&vmbus_connection.channel_mutex);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700418
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800419 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
K. Y. Srinivasan358d2ee2011-08-25 09:48:28 -0700420 if (!uuid_le_cmp(channel->offermsg.offer.if_type,
421 newchannel->offermsg.offer.if_type) &&
422 !uuid_le_cmp(channel->offermsg.offer.if_instance,
423 newchannel->offermsg.offer.if_instance)) {
Haiyang Zhang188963e2010-10-15 10:14:06 -0700424 fnew = false;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700425 break;
426 }
427 }
428
Vitaly Kuznetsov8dfd3322015-05-06 17:47:42 -0700429 if (fnew)
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800430 list_add_tail(&newchannel->listentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800431 &vmbus_connection.chn_list);
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700432
Dexuan Cuid6f591e2015-12-14 16:01:51 -0800433 mutex_unlock(&vmbus_connection.channel_mutex);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700434
Haiyang Zhang188963e2010-10-15 10:14:06 -0700435 if (!fnew) {
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700436 /*
437 * Check to see if this is a sub-channel.
438 */
439 if (newchannel->offermsg.offer.sub_channel_index != 0) {
440 /*
441 * Process the sub-channel.
442 */
443 newchannel->primary_channel = channel;
Vitaly Kuznetsov67fae052015-01-20 16:45:05 +0100444 spin_lock_irqsave(&channel->lock, flags);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700445 list_add_tail(&newchannel->sc_list, &channel->sc_list);
K. Y. Srinivasana13e8bb2015-02-28 11:39:02 -0800446 channel->num_sc++;
Vitaly Kuznetsov357e8362015-05-06 17:47:44 -0700447 spin_unlock_irqrestore(&channel->lock, flags);
Vitaly Kuznetsov8dfd3322015-05-06 17:47:42 -0700448 } else
449 goto err_free_chan;
450 }
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700451
Dexuan Cui0f988292016-09-07 05:39:34 -0700452 dev_type = hv_get_dev_type(newchannel);
K. Y. Srinivasan7047f172015-12-25 20:00:30 -0800453
454 init_vp_index(newchannel, dev_type);
Vitaly Kuznetsovf38e7dd2015-05-06 17:47:45 -0700455
Dexuan Cui638fea32016-06-09 18:47:24 -0700456 hv_event_tasklet_disable(newchannel);
Vitaly Kuznetsov8dfd3322015-05-06 17:47:42 -0700457 if (newchannel->target_cpu != get_cpu()) {
458 put_cpu();
459 smp_call_function_single(newchannel->target_cpu,
460 percpu_channel_enq,
461 newchannel, true);
462 } else {
463 percpu_channel_enq(newchannel);
464 put_cpu();
Hank Janssen3e7ee492009-07-13 16:02:34 -0700465 }
Dexuan Cui638fea32016-06-09 18:47:24 -0700466 hv_event_tasklet_enable(newchannel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700467
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700468 /*
K. Y. Srinivasan42dceeb2013-08-26 14:08:58 -0700469 * This state is used to indicate a successful open
470 * so that when we do close the channel normally, we
471 * can cleanup properly
472 */
473 newchannel->state = CHANNEL_OPEN_STATE;
474
Vitaly Kuznetsov8dfd3322015-05-06 17:47:42 -0700475 if (!fnew) {
476 if (channel->sc_creation_callback != NULL)
477 channel->sc_creation_callback(newchannel);
478 return;
479 }
480
K. Y. Srinivasan42dceeb2013-08-26 14:08:58 -0700481 /*
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700482 * Start the process of binding this offer to the driver
483 * We need to set the DeviceObject field before calling
Haiyang Zhang646f1ea2011-01-26 12:12:10 -0800484 * vmbus_child_dev_add()
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700485 */
K. Y. Srinivasanf2c73012011-09-08 07:24:12 -0700486 newchannel->device_obj = vmbus_device_create(
Haiyang Zhang767dff62011-01-26 12:12:12 -0800487 &newchannel->offermsg.offer.if_type,
488 &newchannel->offermsg.offer.if_instance,
Haiyang Zhang188963e2010-10-15 10:14:06 -0700489 newchannel);
Vitaly Kuznetsov9c3a6f72015-01-20 16:45:04 +0100490 if (!newchannel->device_obj)
K. Y. Srinivasan5b1e5b52015-02-28 11:18:19 -0800491 goto err_deq_chan;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700492
K. Y. Srinivasan7047f172015-12-25 20:00:30 -0800493 newchannel->device_obj->device_id = dev_type;
Bill Pemberton454f18a2009-07-27 16:47:24 -0400494 /*
495 * Add the new device to the bus. This will kick off device-driver
496 * binding which eventually invokes the device driver's AddDevice()
497 * method.
498 */
Dexuan Cui85d9aa72016-01-27 22:29:43 -0800499 mutex_lock(&vmbus_connection.channel_mutex);
500 ret = vmbus_device_register(newchannel->device_obj);
501 mutex_unlock(&vmbus_connection.channel_mutex);
502
503 if (ret != 0) {
Dexuan Cuid43e2fe2015-03-27 09:10:09 -0700504 pr_err("unable to add child device object (relid %d)\n",
505 newchannel->offermsg.child_relid);
506 kfree(newchannel->device_obj);
507 goto err_deq_chan;
508 }
Vitaly Kuznetsov9c3a6f72015-01-20 16:45:04 +0100509 return;
K. Y. Srinivasan2dd37cb2015-02-28 11:18:18 -0800510
K. Y. Srinivasan5b1e5b52015-02-28 11:18:19 -0800511err_deq_chan:
Dexuan Cuid6f591e2015-12-14 16:01:51 -0800512 mutex_lock(&vmbus_connection.channel_mutex);
K. Y. Srinivasan5b1e5b52015-02-28 11:18:19 -0800513 list_del(&newchannel->listentry);
Dexuan Cuid6f591e2015-12-14 16:01:51 -0800514 mutex_unlock(&vmbus_connection.channel_mutex);
K. Y. Srinivasan5b1e5b52015-02-28 11:18:19 -0800515
Dexuan Cui638fea32016-06-09 18:47:24 -0700516 hv_event_tasklet_disable(newchannel);
K. Y. Srinivasan5b1e5b52015-02-28 11:18:19 -0800517 if (newchannel->target_cpu != get_cpu()) {
518 put_cpu();
519 smp_call_function_single(newchannel->target_cpu,
520 percpu_channel_deq, newchannel, true);
521 } else {
522 percpu_channel_deq(newchannel);
523 put_cpu();
524 }
Dexuan Cui638fea32016-06-09 18:47:24 -0700525 hv_event_tasklet_enable(newchannel);
526
527 vmbus_release_relid(newchannel->offermsg.child_relid);
K. Y. Srinivasan5b1e5b52015-02-28 11:18:19 -0800528
Vitaly Kuznetsov9c3a6f72015-01-20 16:45:04 +0100529err_free_chan:
530 free_channel(newchannel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700531}
532
K. Y. Srinivasana1198452012-12-01 06:46:50 -0800533/*
534 * We use this state to statically distribute the channel interrupt load.
535 */
K. Y. Srinivasan1f656ff2015-05-30 23:37:48 -0700536static int next_numa_node_id;
K. Y. Srinivasana1198452012-12-01 06:46:50 -0800537
538/*
539 * Starting with Win8, we can statically distribute the incoming
K. Y. Srinivasan1f656ff2015-05-30 23:37:48 -0700540 * channel interrupt load by binding a channel to VCPU.
541 * We do this in a hierarchical fashion:
542 * First distribute the primary channels across available NUMA nodes
543 * and then distribute the subchannels amongst the CPUs in the NUMA
544 * node assigned to the primary channel.
545 *
546 * For pre-win8 hosts or non-performance critical channels we assign the
547 * first CPU in the first NUMA node.
K. Y. Srinivasana1198452012-12-01 06:46:50 -0800548 */
K. Y. Srinivasan7047f172015-12-25 20:00:30 -0800549static void init_vp_index(struct vmbus_channel *channel, u16 dev_type)
K. Y. Srinivasana1198452012-12-01 06:46:50 -0800550{
551 u32 cur_cpu;
K. Y. Srinivasan7047f172015-12-25 20:00:30 -0800552 bool perf_chn = vmbus_devs[dev_type].perf_device;
K. Y. Srinivasan1f656ff2015-05-30 23:37:48 -0700553 struct vmbus_channel *primary = channel->primary_channel;
554 int next_node;
555 struct cpumask available_mask;
K. Y. Srinivasan9f01ec532015-08-05 00:52:38 -0700556 struct cpumask *alloced_mask;
K. Y. Srinivasana1198452012-12-01 06:46:50 -0800557
K. Y. Srinivasana1198452012-12-01 06:46:50 -0800558 if ((vmbus_proto_version == VERSION_WS2008) ||
559 (vmbus_proto_version == VERSION_WIN7) || (!perf_chn)) {
560 /*
561 * Prior to win8, all channel interrupts are
562 * delivered on cpu 0.
563 * Also if the channel is not a performance critical
564 * channel, bind it to cpu 0.
565 */
K. Y. Srinivasan1f656ff2015-05-30 23:37:48 -0700566 channel->numa_node = 0;
K. Y. Srinivasand3ba7202014-04-08 18:45:53 -0700567 channel->target_cpu = 0;
K. Y. Srinivasan9c6e64a2015-05-30 23:37:47 -0700568 channel->target_vp = hv_context.vp_index[0];
K. Y. Srinivasand3ba7202014-04-08 18:45:53 -0700569 return;
K. Y. Srinivasana1198452012-12-01 06:46:50 -0800570 }
Vitaly Kuznetsovce59fec2015-05-06 17:47:46 -0700571
572 /*
K. Y. Srinivasan509879b2016-09-02 05:58:23 -0700573 * Based on the channel affinity policy, we will assign the NUMA
574 * nodes.
Vitaly Kuznetsovce59fec2015-05-06 17:47:46 -0700575 */
K. Y. Srinivasan509879b2016-09-02 05:58:23 -0700576
577 if ((channel->affinity_policy == HV_BALANCED) || (!primary)) {
K. Y. Srinivasan1f656ff2015-05-30 23:37:48 -0700578 while (true) {
579 next_node = next_numa_node_id++;
K. Y. Srinivasan509879b2016-09-02 05:58:23 -0700580 if (next_node == nr_node_ids) {
K. Y. Srinivasan1f656ff2015-05-30 23:37:48 -0700581 next_node = next_numa_node_id = 0;
K. Y. Srinivasan509879b2016-09-02 05:58:23 -0700582 continue;
583 }
K. Y. Srinivasan1f656ff2015-05-30 23:37:48 -0700584 if (cpumask_empty(cpumask_of_node(next_node)))
585 continue;
586 break;
Vitaly Kuznetsovce59fec2015-05-06 17:47:46 -0700587 }
K. Y. Srinivasan1f656ff2015-05-30 23:37:48 -0700588 channel->numa_node = next_node;
589 primary = channel;
Vitaly Kuznetsovce59fec2015-05-06 17:47:46 -0700590 }
K. Y. Srinivasan9f01ec532015-08-05 00:52:38 -0700591 alloced_mask = &hv_context.hv_numa_map[primary->numa_node];
Vitaly Kuznetsovce59fec2015-05-06 17:47:46 -0700592
K. Y. Srinivasan9f01ec532015-08-05 00:52:38 -0700593 if (cpumask_weight(alloced_mask) ==
K. Y. Srinivasan1f656ff2015-05-30 23:37:48 -0700594 cpumask_weight(cpumask_of_node(primary->numa_node))) {
595 /*
596 * We have cycled through all the CPUs in the node;
597 * reset the alloced map.
598 */
K. Y. Srinivasan9f01ec532015-08-05 00:52:38 -0700599 cpumask_clear(alloced_mask);
K. Y. Srinivasan1f656ff2015-05-30 23:37:48 -0700600 }
601
K. Y. Srinivasan9f01ec532015-08-05 00:52:38 -0700602 cpumask_xor(&available_mask, alloced_mask,
K. Y. Srinivasan1f656ff2015-05-30 23:37:48 -0700603 cpumask_of_node(primary->numa_node));
604
Dexuan Cui3b711072015-08-05 00:52:39 -0700605 cur_cpu = -1;
Vitaly Kuznetsov79fd8e72016-01-27 22:29:34 -0800606
K. Y. Srinivasan509879b2016-09-02 05:58:23 -0700607 if (primary->affinity_policy == HV_LOCALIZED) {
608 /*
609 * Normally Hyper-V host doesn't create more subchannels
610 * than there are VCPUs on the node but it is possible when not
611 * all present VCPUs on the node are initialized by guest.
612 * Clear the alloced_cpus_in_node to start over.
613 */
614 if (cpumask_equal(&primary->alloced_cpus_in_node,
615 cpumask_of_node(primary->numa_node)))
616 cpumask_clear(&primary->alloced_cpus_in_node);
617 }
Vitaly Kuznetsov79fd8e72016-01-27 22:29:34 -0800618
Dexuan Cui3b711072015-08-05 00:52:39 -0700619 while (true) {
620 cur_cpu = cpumask_next(cur_cpu, &available_mask);
621 if (cur_cpu >= nr_cpu_ids) {
622 cur_cpu = -1;
623 cpumask_copy(&available_mask,
624 cpumask_of_node(primary->numa_node));
625 continue;
626 }
627
K. Y. Srinivasan509879b2016-09-02 05:58:23 -0700628 if (primary->affinity_policy == HV_LOCALIZED) {
629 /*
630 * NOTE: in the case of sub-channel, we clear the
631 * sub-channel related bit(s) in
632 * primary->alloced_cpus_in_node in
633 * hv_process_channel_removal(), so when we
634 * reload drivers like hv_netvsc in SMP guest, here
635 * we're able to re-allocate
636 * bit from primary->alloced_cpus_in_node.
637 */
638 if (!cpumask_test_cpu(cur_cpu,
639 &primary->alloced_cpus_in_node)) {
640 cpumask_set_cpu(cur_cpu,
641 &primary->alloced_cpus_in_node);
642 cpumask_set_cpu(cur_cpu, alloced_mask);
643 break;
644 }
645 } else {
Dexuan Cui3b711072015-08-05 00:52:39 -0700646 cpumask_set_cpu(cur_cpu, alloced_mask);
647 break;
648 }
649 }
K. Y. Srinivasan1f656ff2015-05-30 23:37:48 -0700650
K. Y. Srinivasand3ba7202014-04-08 18:45:53 -0700651 channel->target_cpu = cur_cpu;
652 channel->target_vp = hv_context.vp_index[cur_cpu];
K. Y. Srinivasana1198452012-12-01 06:46:50 -0800653}
654
Vitaly Kuznetsov41571912016-01-27 22:29:35 -0800655static void vmbus_wait_for_unload(void)
656{
Vitaly Kuznetsovcd95aad2016-04-30 19:21:34 -0700657 int cpu;
658 void *page_addr;
659 struct hv_message *msg;
Vitaly Kuznetsov41571912016-01-27 22:29:35 -0800660 struct vmbus_channel_message_header *hdr;
Vitaly Kuznetsovcd95aad2016-04-30 19:21:34 -0700661 u32 message_type;
Vitaly Kuznetsov41571912016-01-27 22:29:35 -0800662
Vitaly Kuznetsovcd95aad2016-04-30 19:21:34 -0700663 /*
664 * CHANNELMSG_UNLOAD_RESPONSE is always delivered to the CPU which was
665 * used for initial contact or to CPU0 depending on host version. When
666 * we're crashing on a different CPU let's hope that IRQ handler on
667 * the cpu which receives CHANNELMSG_UNLOAD_RESPONSE is still
668 * functional and vmbus_unload_response() will complete
669 * vmbus_connection.unload_event. If not, the last thing we can do is
670 * read message pages for all CPUs directly.
671 */
Vitaly Kuznetsov41571912016-01-27 22:29:35 -0800672 while (1) {
Vitaly Kuznetsovcd95aad2016-04-30 19:21:34 -0700673 if (completion_done(&vmbus_connection.unload_event))
674 break;
675
676 for_each_online_cpu(cpu) {
677 page_addr = hv_context.synic_message_page[cpu];
678 msg = (struct hv_message *)page_addr +
679 VMBUS_MESSAGE_SINT;
680
681 message_type = READ_ONCE(msg->header.message_type);
682 if (message_type == HVMSG_NONE)
683 continue;
684
685 hdr = (struct vmbus_channel_message_header *)
686 msg->u.payload;
687
688 if (hdr->msgtype == CHANNELMSG_UNLOAD_RESPONSE)
689 complete(&vmbus_connection.unload_event);
690
691 vmbus_signal_eom(msg, message_type);
Vitaly Kuznetsov41571912016-01-27 22:29:35 -0800692 }
693
Vitaly Kuznetsovcd95aad2016-04-30 19:21:34 -0700694 mdelay(10);
695 }
Vitaly Kuznetsov41571912016-01-27 22:29:35 -0800696
Vitaly Kuznetsovcd95aad2016-04-30 19:21:34 -0700697 /*
698 * We're crashing and already got the UNLOAD_RESPONSE, cleanup all
699 * maybe-pending messages on all CPUs to be able to receive new
700 * messages after we reconnect.
701 */
702 for_each_online_cpu(cpu) {
703 page_addr = hv_context.synic_message_page[cpu];
704 msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
705 msg->header.message_type = HVMSG_NONE;
Vitaly Kuznetsov41571912016-01-27 22:29:35 -0800706 }
707}
708
Hank Janssen3e189512010-03-04 22:11:00 +0000709/*
K. Y. Srinivasan2db84ef2015-04-22 21:31:32 -0700710 * vmbus_unload_response - Handler for the unload response.
711 */
712static void vmbus_unload_response(struct vmbus_channel_message_header *hdr)
713{
714 /*
715 * This is a global event; just wakeup the waiting thread.
716 * Once we successfully unload, we can cleanup the monitor state.
717 */
718 complete(&vmbus_connection.unload_event);
719}
720
Vitaly Kuznetsov75ff3a82016-02-26 15:13:16 -0800721void vmbus_initiate_unload(bool crash)
K. Y. Srinivasan2db84ef2015-04-22 21:31:32 -0700722{
723 struct vmbus_channel_message_header hdr;
724
Vitaly Kuznetsov4a542432015-08-01 16:08:19 -0700725 /* Pre-Win2012R2 hosts don't support reconnect */
726 if (vmbus_proto_version < VERSION_WIN8_1)
727 return;
728
K. Y. Srinivasan2db84ef2015-04-22 21:31:32 -0700729 init_completion(&vmbus_connection.unload_event);
730 memset(&hdr, 0, sizeof(struct vmbus_channel_message_header));
731 hdr.msgtype = CHANNELMSG_UNLOAD;
Vitaly Kuznetsov65013a92016-12-07 01:16:24 -0800732 vmbus_post_msg(&hdr, sizeof(struct vmbus_channel_message_header),
733 !crash);
K. Y. Srinivasan2db84ef2015-04-22 21:31:32 -0700734
Vitaly Kuznetsov41571912016-01-27 22:29:35 -0800735 /*
736 * vmbus_initiate_unload() is also called on crash and the crash can be
737 * happening in an interrupt context, where scheduling is impossible.
738 */
Vitaly Kuznetsov75ff3a82016-02-26 15:13:16 -0800739 if (!crash)
Vitaly Kuznetsov41571912016-01-27 22:29:35 -0800740 wait_for_completion(&vmbus_connection.unload_event);
741 else
742 vmbus_wait_for_unload();
K. Y. Srinivasan2db84ef2015-04-22 21:31:32 -0700743}
744
745/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700746 * vmbus_onoffer - Handler for channel offers from vmbus in parent partition.
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700747 *
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700748 */
Haiyang Zhange98cb272010-10-15 10:14:07 -0700749static void vmbus_onoffer(struct vmbus_channel_message_header *hdr)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700750{
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700751 struct vmbus_channel_offer_channel *offer;
Haiyang Zhang188963e2010-10-15 10:14:06 -0700752 struct vmbus_channel *newchannel;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700753
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700754 offer = (struct vmbus_channel_offer_channel *)hdr;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700755
Bill Pemberton454f18a2009-07-27 16:47:24 -0400756 /* Allocate the channel object and save this offer. */
Haiyang Zhange98cb272010-10-15 10:14:07 -0700757 newchannel = alloc_channel();
Haiyang Zhang188963e2010-10-15 10:14:06 -0700758 if (!newchannel) {
Hank Janssen0a466182011-03-29 13:58:47 -0700759 pr_err("Unable to allocate channel object\n");
Hank Janssen3e7ee492009-07-13 16:02:34 -0700760 return;
761 }
762
K. Y. Srinivasan132368b2012-12-01 06:46:33 -0800763 /*
764 * By default we setup state to enable batched
765 * reading. A specific service can choose to
766 * disable this prior to opening the channel.
767 */
768 newchannel->batched_reading = true;
769
K. Y. Srinivasanb3bf60c2012-12-01 06:46:45 -0800770 /*
771 * Setup state for signalling the host.
772 */
773 newchannel->sig_event = (struct hv_input_signal_event *)
774 (ALIGN((unsigned long)
775 &newchannel->sig_buf,
776 HV_HYPERCALL_PARAM_ALIGN));
777
778 newchannel->sig_event->connectionid.asu32 = 0;
779 newchannel->sig_event->connectionid.u.id = VMBUS_EVENT_CONNECTION_ID;
780 newchannel->sig_event->flag_number = 0;
781 newchannel->sig_event->rsvdz = 0;
782
783 if (vmbus_proto_version != VERSION_WS2008) {
784 newchannel->is_dedicated_interrupt =
785 (offer->is_dedicated_interrupt != 0);
786 newchannel->sig_event->connectionid.u.id =
787 offer->connection_id;
788 }
789
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800790 memcpy(&newchannel->offermsg, offer,
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700791 sizeof(struct vmbus_channel_offer_channel));
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800792 newchannel->monitor_grp = (u8)offer->monitorid / 32;
793 newchannel->monitor_bit = (u8)offer->monitorid % 32;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700794
K. Y. Srinivasan2dd37cb2015-02-28 11:18:18 -0800795 vmbus_process_offer(newchannel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700796}
797
Hank Janssen3e189512010-03-04 22:11:00 +0000798/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700799 * vmbus_onoffer_rescind - Rescind offer handler.
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700800 *
801 * We queue a work item to process this offer synchronously
802 */
Haiyang Zhange98cb272010-10-15 10:14:07 -0700803static void vmbus_onoffer_rescind(struct vmbus_channel_message_header *hdr)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700804{
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700805 struct vmbus_channel_rescind_offer *rescind;
Greg Kroah-Hartmanaded71652009-08-18 15:21:19 -0700806 struct vmbus_channel *channel;
Dexuan Cuid43e2fe2015-03-27 09:10:09 -0700807 unsigned long flags;
808 struct device *dev;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700809
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700810 rescind = (struct vmbus_channel_rescind_offer *)hdr;
Dexuan Cui85d9aa72016-01-27 22:29:43 -0800811
812 mutex_lock(&vmbus_connection.channel_mutex);
Dexuan Cuid43e2fe2015-03-27 09:10:09 -0700813 channel = relid2channel(rescind->child_relid);
Hank Janssen98e08702011-03-29 13:58:44 -0700814
K. Y. Srinivasan2dd37cb2015-02-28 11:18:18 -0800815 if (channel == NULL) {
Dexuan Cuif52078c2015-12-14 16:01:50 -0800816 /*
817 * This is very impossible, because in
818 * vmbus_process_offer(), we have already invoked
819 * vmbus_release_relid() on error.
820 */
Dexuan Cui85d9aa72016-01-27 22:29:43 -0800821 goto out;
K. Y. Srinivasan2dd37cb2015-02-28 11:18:18 -0800822 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700823
Dexuan Cuid43e2fe2015-03-27 09:10:09 -0700824 spin_lock_irqsave(&channel->lock, flags);
825 channel->rescind = true;
826 spin_unlock_irqrestore(&channel->lock, flags);
827
828 if (channel->device_obj) {
Dexuan Cui499e8402016-01-27 22:29:42 -0800829 if (channel->chn_rescind_callback) {
830 channel->chn_rescind_callback(channel);
Dexuan Cui85d9aa72016-01-27 22:29:43 -0800831 goto out;
Dexuan Cui499e8402016-01-27 22:29:42 -0800832 }
Dexuan Cuid43e2fe2015-03-27 09:10:09 -0700833 /*
834 * We will have to unregister this device from the
835 * driver core.
836 */
837 dev = get_device(&channel->device_obj->device);
838 if (dev) {
839 vmbus_device_unregister(channel->device_obj);
840 put_device(dev);
841 }
842 } else {
843 hv_process_channel_removal(channel,
844 channel->offermsg.child_relid);
K. Y. Srinivasan2dd37cb2015-02-28 11:18:18 -0800845 }
Dexuan Cui85d9aa72016-01-27 22:29:43 -0800846
847out:
848 mutex_unlock(&vmbus_connection.channel_mutex);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700849}
850
Dexuan Cui85d9aa72016-01-27 22:29:43 -0800851void vmbus_hvsock_device_unregister(struct vmbus_channel *channel)
852{
853 mutex_lock(&vmbus_connection.channel_mutex);
854
855 BUG_ON(!is_hvsock_channel(channel));
856
857 channel->rescind = true;
858 vmbus_device_unregister(channel->device_obj);
859
860 mutex_unlock(&vmbus_connection.channel_mutex);
861}
862EXPORT_SYMBOL_GPL(vmbus_hvsock_device_unregister);
863
864
Hank Janssen3e189512010-03-04 22:11:00 +0000865/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700866 * vmbus_onoffers_delivered -
867 * This is invoked when all offers have been delivered.
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700868 *
869 * Nothing to do here.
870 */
Haiyang Zhange98cb272010-10-15 10:14:07 -0700871static void vmbus_onoffers_delivered(
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700872 struct vmbus_channel_message_header *hdr)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700873{
Hank Janssen3e7ee492009-07-13 16:02:34 -0700874}
875
Hank Janssen3e189512010-03-04 22:11:00 +0000876/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700877 * vmbus_onopen_result - Open result handler.
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700878 *
879 * This is invoked when we received a response to our channel open request.
880 * Find the matching request, copy the response and signal the requesting
881 * thread.
882 */
Haiyang Zhange98cb272010-10-15 10:14:07 -0700883static void vmbus_onopen_result(struct vmbus_channel_message_header *hdr)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700884{
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700885 struct vmbus_channel_open_result *result;
Haiyang Zhang188963e2010-10-15 10:14:06 -0700886 struct vmbus_channel_msginfo *msginfo;
887 struct vmbus_channel_message_header *requestheader;
888 struct vmbus_channel_open_channel *openmsg;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700889 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700890
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700891 result = (struct vmbus_channel_open_result *)hdr;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700892
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700893 /*
894 * Find the open msg, copy the result and signal/unblock the wait event
895 */
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800896 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700897
Hank Janssenebb61e52011-02-18 12:39:57 -0800898 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
899 msglistentry) {
Haiyang Zhang188963e2010-10-15 10:14:06 -0700900 requestheader =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800901 (struct vmbus_channel_message_header *)msginfo->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700902
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800903 if (requestheader->msgtype == CHANNELMSG_OPENCHANNEL) {
Haiyang Zhang188963e2010-10-15 10:14:06 -0700904 openmsg =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800905 (struct vmbus_channel_open_channel *)msginfo->msg;
906 if (openmsg->child_relid == result->child_relid &&
907 openmsg->openid == result->openid) {
908 memcpy(&msginfo->response.open_result,
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700909 result,
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700910 sizeof(
911 struct vmbus_channel_open_result));
912 complete(&msginfo->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700913 break;
914 }
915 }
916 }
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800917 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700918}
919
Hank Janssen3e189512010-03-04 22:11:00 +0000920/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700921 * vmbus_ongpadl_created - GPADL created handler.
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700922 *
923 * This is invoked when we received a response to our gpadl create request.
924 * Find the matching request, copy the response and signal the requesting
925 * thread.
926 */
Haiyang Zhange98cb272010-10-15 10:14:07 -0700927static void vmbus_ongpadl_created(struct vmbus_channel_message_header *hdr)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700928{
Haiyang Zhang188963e2010-10-15 10:14:06 -0700929 struct vmbus_channel_gpadl_created *gpadlcreated;
Haiyang Zhang188963e2010-10-15 10:14:06 -0700930 struct vmbus_channel_msginfo *msginfo;
931 struct vmbus_channel_message_header *requestheader;
932 struct vmbus_channel_gpadl_header *gpadlheader;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700933 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700934
Haiyang Zhang188963e2010-10-15 10:14:06 -0700935 gpadlcreated = (struct vmbus_channel_gpadl_created *)hdr;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700936
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700937 /*
938 * Find the establish msg, copy the result and signal/unblock the wait
939 * event
940 */
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800941 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700942
Hank Janssenebb61e52011-02-18 12:39:57 -0800943 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
944 msglistentry) {
Haiyang Zhang188963e2010-10-15 10:14:06 -0700945 requestheader =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800946 (struct vmbus_channel_message_header *)msginfo->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700947
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800948 if (requestheader->msgtype == CHANNELMSG_GPADL_HEADER) {
Haiyang Zhang188963e2010-10-15 10:14:06 -0700949 gpadlheader =
950 (struct vmbus_channel_gpadl_header *)requestheader;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700951
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800952 if ((gpadlcreated->child_relid ==
953 gpadlheader->child_relid) &&
954 (gpadlcreated->gpadl == gpadlheader->gpadl)) {
955 memcpy(&msginfo->response.gpadl_created,
Haiyang Zhang188963e2010-10-15 10:14:06 -0700956 gpadlcreated,
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700957 sizeof(
958 struct vmbus_channel_gpadl_created));
959 complete(&msginfo->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700960 break;
961 }
962 }
963 }
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800964 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700965}
966
Hank Janssen3e189512010-03-04 22:11:00 +0000967/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700968 * vmbus_ongpadl_torndown - GPADL torndown handler.
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700969 *
970 * This is invoked when we received a response to our gpadl teardown request.
971 * Find the matching request, copy the response and signal the requesting
972 * thread.
973 */
Haiyang Zhange98cb272010-10-15 10:14:07 -0700974static void vmbus_ongpadl_torndown(
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700975 struct vmbus_channel_message_header *hdr)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700976{
Haiyang Zhang188963e2010-10-15 10:14:06 -0700977 struct vmbus_channel_gpadl_torndown *gpadl_torndown;
Haiyang Zhang188963e2010-10-15 10:14:06 -0700978 struct vmbus_channel_msginfo *msginfo;
979 struct vmbus_channel_message_header *requestheader;
980 struct vmbus_channel_gpadl_teardown *gpadl_teardown;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700981 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700982
Haiyang Zhang188963e2010-10-15 10:14:06 -0700983 gpadl_torndown = (struct vmbus_channel_gpadl_torndown *)hdr;
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700984
985 /*
986 * Find the open msg, copy the result and signal/unblock the wait event
987 */
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800988 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700989
Hank Janssenebb61e52011-02-18 12:39:57 -0800990 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
991 msglistentry) {
Haiyang Zhang188963e2010-10-15 10:14:06 -0700992 requestheader =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800993 (struct vmbus_channel_message_header *)msginfo->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700994
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800995 if (requestheader->msgtype == CHANNELMSG_GPADL_TEARDOWN) {
Haiyang Zhang188963e2010-10-15 10:14:06 -0700996 gpadl_teardown =
997 (struct vmbus_channel_gpadl_teardown *)requestheader;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700998
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800999 if (gpadl_torndown->gpadl == gpadl_teardown->gpadl) {
1000 memcpy(&msginfo->response.gpadl_torndown,
Haiyang Zhang188963e2010-10-15 10:14:06 -07001001 gpadl_torndown,
K. Y. Srinivasan9568a192011-05-10 07:55:39 -07001002 sizeof(
1003 struct vmbus_channel_gpadl_torndown));
1004 complete(&msginfo->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001005 break;
1006 }
1007 }
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
Hank Janssen3e189512010-03-04 22:11:00 +00001012/*
Haiyang Zhange98cb272010-10-15 10:14:07 -07001013 * vmbus_onversion_response - Version response handler
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -07001014 *
1015 * This is invoked when we received a response to our initiate contact request.
1016 * Find the matching request, copy the response and signal the requesting
1017 * thread.
1018 */
Haiyang Zhange98cb272010-10-15 10:14:07 -07001019static void vmbus_onversion_response(
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -07001020 struct vmbus_channel_message_header *hdr)
Hank Janssen3e7ee492009-07-13 16:02:34 -07001021{
Haiyang Zhang188963e2010-10-15 10:14:06 -07001022 struct vmbus_channel_msginfo *msginfo;
1023 struct vmbus_channel_message_header *requestheader;
Haiyang Zhang188963e2010-10-15 10:14:06 -07001024 struct vmbus_channel_version_response *version_response;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -07001025 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001026
Haiyang Zhang188963e2010-10-15 10:14:06 -07001027 version_response = (struct vmbus_channel_version_response *)hdr;
Haiyang Zhang15b2f642011-01-26 12:12:07 -08001028 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001029
Hank Janssenebb61e52011-02-18 12:39:57 -08001030 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
1031 msglistentry) {
Haiyang Zhang188963e2010-10-15 10:14:06 -07001032 requestheader =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -08001033 (struct vmbus_channel_message_header *)msginfo->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001034
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -08001035 if (requestheader->msgtype ==
1036 CHANNELMSG_INITIATE_CONTACT) {
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -08001037 memcpy(&msginfo->response.version_response,
Haiyang Zhang188963e2010-10-15 10:14:06 -07001038 version_response,
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -07001039 sizeof(struct vmbus_channel_version_response));
K. Y. Srinivasan9568a192011-05-10 07:55:39 -07001040 complete(&msginfo->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001041 }
1042 }
Haiyang Zhang15b2f642011-01-26 12:12:07 -08001043 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001044}
1045
Greg Kroah-Hartmanc8212f02009-08-31 21:51:50 -07001046/* Channel message dispatch table */
Dexuan Cui652594c2015-03-27 09:10:08 -07001047struct vmbus_channel_message_table_entry
K. Y. Srinivasanb7c6b022011-05-10 07:55:38 -07001048 channel_message_table[CHANNELMSG_COUNT] = {
Dexuan Cui652594c2015-03-27 09:10:08 -07001049 {CHANNELMSG_INVALID, 0, NULL},
1050 {CHANNELMSG_OFFERCHANNEL, 0, vmbus_onoffer},
1051 {CHANNELMSG_RESCIND_CHANNELOFFER, 0, vmbus_onoffer_rescind},
1052 {CHANNELMSG_REQUESTOFFERS, 0, NULL},
1053 {CHANNELMSG_ALLOFFERS_DELIVERED, 1, vmbus_onoffers_delivered},
1054 {CHANNELMSG_OPENCHANNEL, 0, NULL},
1055 {CHANNELMSG_OPENCHANNEL_RESULT, 1, vmbus_onopen_result},
1056 {CHANNELMSG_CLOSECHANNEL, 0, NULL},
1057 {CHANNELMSG_GPADL_HEADER, 0, NULL},
1058 {CHANNELMSG_GPADL_BODY, 0, NULL},
1059 {CHANNELMSG_GPADL_CREATED, 1, vmbus_ongpadl_created},
1060 {CHANNELMSG_GPADL_TEARDOWN, 0, NULL},
1061 {CHANNELMSG_GPADL_TORNDOWN, 1, vmbus_ongpadl_torndown},
1062 {CHANNELMSG_RELID_RELEASED, 0, NULL},
1063 {CHANNELMSG_INITIATE_CONTACT, 0, NULL},
1064 {CHANNELMSG_VERSION_RESPONSE, 1, vmbus_onversion_response},
1065 {CHANNELMSG_UNLOAD, 0, NULL},
K. Y. Srinivasan2db84ef2015-04-22 21:31:32 -07001066 {CHANNELMSG_UNLOAD_RESPONSE, 1, vmbus_unload_response},
Dexuan Cui5c23a1a2016-01-27 22:29:40 -08001067 {CHANNELMSG_18, 0, NULL},
1068 {CHANNELMSG_19, 0, NULL},
1069 {CHANNELMSG_20, 0, NULL},
1070 {CHANNELMSG_TL_CONNECT_REQUEST, 0, NULL},
Greg Kroah-Hartmanc8212f02009-08-31 21:51:50 -07001071};
1072
Hank Janssen3e189512010-03-04 22:11:00 +00001073/*
Haiyang Zhange98cb272010-10-15 10:14:07 -07001074 * vmbus_onmessage - Handler for channel protocol messages.
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -07001075 *
1076 * This is invoked in the vmbus worker thread context.
1077 */
Haiyang Zhange98cb272010-10-15 10:14:07 -07001078void vmbus_onmessage(void *context)
Hank Janssen3e7ee492009-07-13 16:02:34 -07001079{
Haiyang Zhang188963e2010-10-15 10:14:06 -07001080 struct hv_message *msg = context;
Greg Kroah-Hartman82250212009-08-26 15:16:04 -07001081 struct vmbus_channel_message_header *hdr;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001082 int size;
1083
Haiyang Zhangf6feebe2010-11-08 14:04:39 -08001084 hdr = (struct vmbus_channel_message_header *)msg->u.payload;
1085 size = msg->header.payload_size;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001086
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -08001087 if (hdr->msgtype >= CHANNELMSG_COUNT) {
Hank Janssen0a466182011-03-29 13:58:47 -07001088 pr_err("Received invalid channel message type %d size %d\n",
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -08001089 hdr->msgtype, size);
Greg Kroah-Hartman04f50c42009-07-16 12:35:37 -07001090 print_hex_dump_bytes("", DUMP_PREFIX_NONE,
Haiyang Zhangf6feebe2010-11-08 14:04:39 -08001091 (unsigned char *)msg->u.payload, size);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001092 return;
1093 }
1094
K. Y. Srinivasanb7c6b022011-05-10 07:55:38 -07001095 if (channel_message_table[hdr->msgtype].message_handler)
1096 channel_message_table[hdr->msgtype].message_handler(hdr);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001097 else
Hank Janssen0a466182011-03-29 13:58:47 -07001098 pr_err("Unhandled channel message type %d\n", hdr->msgtype);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001099}
1100
Hank Janssen3e189512010-03-04 22:11:00 +00001101/*
Haiyang Zhange98cb272010-10-15 10:14:07 -07001102 * vmbus_request_offers - Send a request to get all our pending offers.
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -07001103 */
Haiyang Zhange98cb272010-10-15 10:14:07 -07001104int vmbus_request_offers(void)
Hank Janssen3e7ee492009-07-13 16:02:34 -07001105{
Greg Kroah-Hartman82250212009-08-26 15:16:04 -07001106 struct vmbus_channel_message_header *msg;
Haiyang Zhang188963e2010-10-15 10:14:06 -07001107 struct vmbus_channel_msginfo *msginfo;
Nicholas Mc Guire51e51812015-02-27 11:26:02 -08001108 int ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001109
Haiyang Zhang188963e2010-10-15 10:14:06 -07001110 msginfo = kmalloc(sizeof(*msginfo) +
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -07001111 sizeof(struct vmbus_channel_message_header),
1112 GFP_KERNEL);
Haiyang Zhang188963e2010-10-15 10:14:06 -07001113 if (!msginfo)
Bill Pemberton75910f22010-05-05 15:27:31 -04001114 return -ENOMEM;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001115
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -08001116 msg = (struct vmbus_channel_message_header *)msginfo->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001117
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -08001118 msg->msgtype = CHANNELMSG_REQUESTOFFERS;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001119
Hank Janssen3e7ee492009-07-13 16:02:34 -07001120
Vitaly Kuznetsov65013a92016-12-07 01:16:24 -08001121 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_message_header),
1122 true);
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -07001123 if (ret != 0) {
Hank Janssen0a466182011-03-29 13:58:47 -07001124 pr_err("Unable to request offers - %d\n", ret);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001125
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -08001126 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001127 }
Hank Janssen3e7ee492009-07-13 16:02:34 -07001128
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -08001129cleanup:
Ilia Mirkindd9b15d2011-03-13 00:29:00 -05001130 kfree(msginfo);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001131
Hank Janssen3e7ee492009-07-13 16:02:34 -07001132 return ret;
1133}
1134
K. Y. Srinivasane68d2972013-05-23 12:02:32 -07001135/*
1136 * Retrieve the (sub) channel on which to send an outgoing request.
K. Y. Srinivasana13e8bb2015-02-28 11:39:02 -08001137 * When a primary channel has multiple sub-channels, we try to
1138 * distribute the load equally amongst all available channels.
K. Y. Srinivasane68d2972013-05-23 12:02:32 -07001139 */
1140struct vmbus_channel *vmbus_get_outgoing_channel(struct vmbus_channel *primary)
1141{
1142 struct list_head *cur, *tmp;
K. Y. Srinivasan87712bf82014-12-14 23:34:51 -08001143 int cur_cpu;
K. Y. Srinivasane68d2972013-05-23 12:02:32 -07001144 struct vmbus_channel *cur_channel;
1145 struct vmbus_channel *outgoing_channel = primary;
K. Y. Srinivasana13e8bb2015-02-28 11:39:02 -08001146 int next_channel;
1147 int i = 1;
K. Y. Srinivasane68d2972013-05-23 12:02:32 -07001148
1149 if (list_empty(&primary->sc_list))
1150 return outgoing_channel;
1151
K. Y. Srinivasana13e8bb2015-02-28 11:39:02 -08001152 next_channel = primary->next_oc++;
1153
1154 if (next_channel > (primary->num_sc)) {
1155 primary->next_oc = 0;
1156 return outgoing_channel;
1157 }
1158
K. Y. Srinivasan87712bf82014-12-14 23:34:51 -08001159 cur_cpu = hv_context.vp_index[get_cpu()];
1160 put_cpu();
K. Y. Srinivasane68d2972013-05-23 12:02:32 -07001161 list_for_each_safe(cur, tmp, &primary->sc_list) {
1162 cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
1163 if (cur_channel->state != CHANNEL_OPENED_STATE)
1164 continue;
1165
1166 if (cur_channel->target_vp == cur_cpu)
1167 return cur_channel;
1168
K. Y. Srinivasana13e8bb2015-02-28 11:39:02 -08001169 if (i == next_channel)
1170 return cur_channel;
K. Y. Srinivasane68d2972013-05-23 12:02:32 -07001171
K. Y. Srinivasana13e8bb2015-02-28 11:39:02 -08001172 i++;
K. Y. Srinivasane68d2972013-05-23 12:02:32 -07001173 }
1174
1175 return outgoing_channel;
1176}
1177EXPORT_SYMBOL_GPL(vmbus_get_outgoing_channel);
1178
1179static void invoke_sc_cb(struct vmbus_channel *primary_channel)
1180{
1181 struct list_head *cur, *tmp;
1182 struct vmbus_channel *cur_channel;
1183
1184 if (primary_channel->sc_creation_callback == NULL)
1185 return;
1186
1187 list_for_each_safe(cur, tmp, &primary_channel->sc_list) {
1188 cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
1189
1190 primary_channel->sc_creation_callback(cur_channel);
1191 }
1192}
1193
1194void vmbus_set_sc_create_callback(struct vmbus_channel *primary_channel,
1195 void (*sc_cr_cb)(struct vmbus_channel *new_sc))
1196{
1197 primary_channel->sc_creation_callback = sc_cr_cb;
1198}
1199EXPORT_SYMBOL_GPL(vmbus_set_sc_create_callback);
1200
1201bool vmbus_are_subchannels_present(struct vmbus_channel *primary)
1202{
1203 bool ret;
1204
1205 ret = !list_empty(&primary->sc_list);
1206
1207 if (ret) {
1208 /*
1209 * Invoke the callback on sub-channel creation.
1210 * This will present a uniform interface to the
1211 * clients.
1212 */
1213 invoke_sc_cb(primary);
1214 }
1215
1216 return ret;
1217}
1218EXPORT_SYMBOL_GPL(vmbus_are_subchannels_present);
Dexuan Cui499e8402016-01-27 22:29:42 -08001219
1220void vmbus_set_chn_rescind_callback(struct vmbus_channel *channel,
1221 void (*chn_rescind_cb)(struct vmbus_channel *))
1222{
1223 channel->chn_rescind_callback = chn_rescind_cb;
1224}
1225EXPORT_SYMBOL_GPL(vmbus_set_chn_rescind_callback);