blob: 107d72f9834da4a8c6e35418fd73470529c356ab [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>
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -080024#include <linux/sched.h>
25#include <linux/wait.h>
Greg Kroah-Hartmana0086dc2009-08-17 17:22:08 -070026#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Bill Pemberton53af5452009-09-11 21:46:44 -040028#include <linux/list.h>
Hank Janssenc88c4e42010-05-04 15:55:05 -070029#include <linux/module.h>
Haiyang Zhang8b5d6d32010-05-28 23:22:44 +000030#include <linux/completion.h>
Greg Kroah-Hartman46a97192011-10-04 12:29:52 -070031#include <linux/hyperv.h>
K. Y. Srinivasan3f335ea2011-05-12 19:34:15 -070032
K. Y. Srinivasan0f2a6612011-05-12 19:34:28 -070033#include "hyperv_vmbus.h"
Hank Janssen3e7ee492009-07-13 16:02:34 -070034
K. Y. Srinivasan7047f172015-12-25 20:00:30 -080035static void init_vp_index(struct vmbus_channel *channel, u16 dev_type);
36
37static const struct vmbus_device vmbus_devs[] = {
38 /* IDE */
39 { .dev_type = HV_IDE,
40 HV_IDE_GUID,
41 .perf_device = true,
42 },
43
44 /* SCSI */
45 { .dev_type = HV_SCSI,
46 HV_SCSI_GUID,
47 .perf_device = true,
48 },
49
50 /* Fibre Channel */
51 { .dev_type = HV_FC,
52 HV_SYNTHFC_GUID,
53 .perf_device = true,
54 },
55
56 /* Synthetic NIC */
57 { .dev_type = HV_NIC,
58 HV_NIC_GUID,
59 .perf_device = true,
60 },
61
62 /* Network Direct */
63 { .dev_type = HV_ND,
64 HV_ND_GUID,
65 .perf_device = true,
66 },
67
68 /* PCIE */
69 { .dev_type = HV_PCIE,
70 HV_PCIE_GUID,
71 .perf_device = true,
72 },
73
74 /* Synthetic Frame Buffer */
75 { .dev_type = HV_FB,
76 HV_SYNTHVID_GUID,
77 .perf_device = false,
78 },
79
80 /* Synthetic Keyboard */
81 { .dev_type = HV_KBD,
82 HV_KBD_GUID,
83 .perf_device = false,
84 },
85
86 /* Synthetic MOUSE */
87 { .dev_type = HV_MOUSE,
88 HV_MOUSE_GUID,
89 .perf_device = false,
90 },
91
92 /* KVP */
93 { .dev_type = HV_KVP,
94 HV_KVP_GUID,
95 .perf_device = false,
96 },
97
98 /* Time Synch */
99 { .dev_type = HV_TS,
100 HV_TS_GUID,
101 .perf_device = false,
102 },
103
104 /* Heartbeat */
105 { .dev_type = HV_HB,
106 HV_HEART_BEAT_GUID,
107 .perf_device = false,
108 },
109
110 /* Shutdown */
111 { .dev_type = HV_SHUTDOWN,
112 HV_SHUTDOWN_GUID,
113 .perf_device = false,
114 },
115
116 /* File copy */
117 { .dev_type = HV_FCOPY,
118 HV_FCOPY_GUID,
119 .perf_device = false,
120 },
121
122 /* Backup */
123 { .dev_type = HV_BACKUP,
124 HV_VSS_GUID,
125 .perf_device = false,
126 },
127
128 /* Dynamic Memory */
129 { .dev_type = HV_DM,
130 HV_DM_GUID,
131 .perf_device = false,
132 },
133
134 /* Unknown GUID */
135 { .dev_type = HV_UNKOWN,
136 .perf_device = false,
137 },
138};
139
140static u16 hv_get_dev_type(const uuid_le *guid)
141{
142 u16 i;
143
144 for (i = HV_IDE; i < HV_UNKOWN; i++) {
145 if (!uuid_le_cmp(*guid, vmbus_devs[i].guid))
146 return i;
147 }
148 pr_info("Unknown GUID: %pUl\n", guid);
149 return i;
150}
Vitaly Kuznetsovf38e7dd2015-05-06 17:47:45 -0700151
Hank Janssenc88c4e42010-05-04 15:55:05 -0700152/**
Greg Kroah-Hartmanda0e9632011-10-11 08:42:28 -0600153 * vmbus_prep_negotiate_resp() - Create default response for Hyper-V Negotiate message
Hank Janssenc88c4e42010-05-04 15:55:05 -0700154 * @icmsghdrp: Pointer to msg header structure
155 * @icmsg_negotiate: Pointer to negotiate message structure
156 * @buf: Raw buffer channel data
157 *
158 * @icmsghdrp is of type &struct icmsg_hdr.
159 * @negop is of type &struct icmsg_negotiate.
K. Y. Srinivasanc836d0a2012-05-12 13:44:58 -0700160 * Set up and fill in default negotiate response message.
161 *
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700162 * The fw_version specifies the framework version that
163 * we can support and srv_version specifies the service
164 * version we can support.
Hank Janssenc88c4e42010-05-04 15:55:05 -0700165 *
166 * Mainly used by Hyper-V drivers.
167 */
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700168bool vmbus_prep_negotiate_resp(struct icmsg_hdr *icmsghdrp,
K. Y. Srinivasanc836d0a2012-05-12 13:44:58 -0700169 struct icmsg_negotiate *negop, u8 *buf,
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700170 int fw_version, int srv_version)
Hank Janssenc88c4e42010-05-04 15:55:05 -0700171{
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700172 int icframe_major, icframe_minor;
173 int icmsg_major, icmsg_minor;
174 int fw_major, fw_minor;
175 int srv_major, srv_minor;
K. Y. Srinivasanc836d0a2012-05-12 13:44:58 -0700176 int i;
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700177 bool found_match = false;
K. Y. Srinivasanc836d0a2012-05-12 13:44:58 -0700178
K. Y. Srinivasana3605302012-05-12 13:44:57 -0700179 icmsghdrp->icmsgsize = 0x10;
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700180 fw_major = (fw_version >> 16);
181 fw_minor = (fw_version & 0xFFFF);
182
183 srv_major = (srv_version >> 16);
184 srv_minor = (srv_version & 0xFFFF);
Hank Janssenc88c4e42010-05-04 15:55:05 -0700185
K. Y. Srinivasana3605302012-05-12 13:44:57 -0700186 negop = (struct icmsg_negotiate *)&buf[
187 sizeof(struct vmbuspipe_hdr) +
188 sizeof(struct icmsg_hdr)];
Hank Janssenc88c4e42010-05-04 15:55:05 -0700189
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700190 icframe_major = negop->icframe_vercnt;
191 icframe_minor = 0;
192
193 icmsg_major = negop->icmsg_vercnt;
194 icmsg_minor = 0;
K. Y. Srinivasanc836d0a2012-05-12 13:44:58 -0700195
196 /*
197 * Select the framework version number we will
198 * support.
199 */
200
201 for (i = 0; i < negop->icframe_vercnt; i++) {
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700202 if ((negop->icversion_data[i].major == fw_major) &&
203 (negop->icversion_data[i].minor == fw_minor)) {
204 icframe_major = negop->icversion_data[i].major;
205 icframe_minor = negop->icversion_data[i].minor;
206 found_match = true;
207 }
Hank Janssenc88c4e42010-05-04 15:55:05 -0700208 }
K. Y. Srinivasana3605302012-05-12 13:44:57 -0700209
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700210 if (!found_match)
211 goto fw_error;
212
213 found_match = false;
214
K. Y. Srinivasanc836d0a2012-05-12 13:44:58 -0700215 for (i = negop->icframe_vercnt;
216 (i < negop->icframe_vercnt + negop->icmsg_vercnt); i++) {
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700217 if ((negop->icversion_data[i].major == srv_major) &&
218 (negop->icversion_data[i].minor == srv_minor)) {
219 icmsg_major = negop->icversion_data[i].major;
220 icmsg_minor = negop->icversion_data[i].minor;
221 found_match = true;
222 }
K. Y. Srinivasanc836d0a2012-05-12 13:44:58 -0700223 }
224
225 /*
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700226 * Respond with the framework and service
K. Y. Srinivasanc836d0a2012-05-12 13:44:58 -0700227 * version numbers we can support.
228 */
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700229
230fw_error:
231 if (!found_match) {
232 negop->icframe_vercnt = 0;
233 negop->icmsg_vercnt = 0;
234 } else {
235 negop->icframe_vercnt = 1;
236 negop->icmsg_vercnt = 1;
237 }
238
239 negop->icversion_data[0].major = icframe_major;
240 negop->icversion_data[0].minor = icframe_minor;
241 negop->icversion_data[1].major = icmsg_major;
242 negop->icversion_data[1].minor = icmsg_minor;
243 return found_match;
Hank Janssenc88c4e42010-05-04 15:55:05 -0700244}
K. Y. Srinivasana3605302012-05-12 13:44:57 -0700245
Greg Kroah-Hartmanda0e9632011-10-11 08:42:28 -0600246EXPORT_SYMBOL_GPL(vmbus_prep_negotiate_resp);
Hank Janssenc88c4e42010-05-04 15:55:05 -0700247
Hank Janssen3e189512010-03-04 22:11:00 +0000248/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700249 * alloc_channel - Allocate and initialize a vmbus channel object
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700250 */
Greg Kroah-Hartman50fe56d2010-10-20 15:51:57 -0700251static struct vmbus_channel *alloc_channel(void)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700252{
Vitaly Kuznetsovbc63b6f2015-02-27 11:25:52 -0800253 static atomic_t chan_num = ATOMIC_INIT(0);
Greg Kroah-Hartmanaded7162009-08-18 15:21:19 -0700254 struct vmbus_channel *channel;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700255
Greg Kroah-Hartmanaded7162009-08-18 15:21:19 -0700256 channel = kzalloc(sizeof(*channel), GFP_ATOMIC);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700257 if (!channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700258 return NULL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700259
Vitaly Kuznetsovbc63b6f2015-02-27 11:25:52 -0800260 channel->id = atomic_inc_return(&chan_num);
Greg Kroah-Hartman54411c42009-07-15 14:48:32 -0700261 spin_lock_init(&channel->inbound_lock);
Vitaly Kuznetsov67fae052015-01-20 16:45:05 +0100262 spin_lock_init(&channel->lock);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700263
264 INIT_LIST_HEAD(&channel->sc_list);
K. Y. Srinivasan3a28fa32014-04-08 18:45:54 -0700265 INIT_LIST_HEAD(&channel->percpu_list);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700266
Hank Janssen3e7ee492009-07-13 16:02:34 -0700267 return channel;
268}
269
Hank Janssen3e189512010-03-04 22:11:00 +0000270/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700271 * free_channel - Release the resources used by the vmbus channel object
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700272 */
Greg Kroah-Hartman9f3e28e2011-10-11 09:40:01 -0600273static void free_channel(struct vmbus_channel *channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700274{
Dexuan Cuiaadc3782015-03-27 09:10:10 -0700275 kfree(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700276}
277
K. Y. Srinivasan3a28fa32014-04-08 18:45:54 -0700278static void percpu_channel_enq(void *arg)
279{
280 struct vmbus_channel *channel = arg;
281 int cpu = smp_processor_id();
Haiyang Zhang8b5d6d32010-05-28 23:22:44 +0000282
K. Y. Srinivasan3a28fa32014-04-08 18:45:54 -0700283 list_add_tail(&channel->percpu_list, &hv_context.percpu_list[cpu]);
284}
285
286static void percpu_channel_deq(void *arg)
287{
288 struct vmbus_channel *channel = arg;
289
290 list_del(&channel->percpu_list);
291}
Haiyang Zhang8b5d6d32010-05-28 23:22:44 +0000292
K. Y. Srinivasaned6cfcc2015-02-28 11:18:17 -0800293
Dexuan Cuif52078c2015-12-14 16:01:50 -0800294static void vmbus_release_relid(u32 relid)
Timo Teräs4b2f9ab2010-12-15 20:48:09 +0200295{
K. Y. Srinivasaned6cfcc2015-02-28 11:18:17 -0800296 struct vmbus_channel_relid_released msg;
Vitaly Kuznetsov04a258c2014-11-04 13:40:11 +0100297
K. Y. Srinivasanc8705972013-03-15 12:25:44 -0700298 memset(&msg, 0, sizeof(struct vmbus_channel_relid_released));
K. Y. Srinivasaned6cfcc2015-02-28 11:18:17 -0800299 msg.child_relid = relid;
K. Y. Srinivasanc8705972013-03-15 12:25:44 -0700300 msg.header.msgtype = CHANNELMSG_RELID_RELEASED;
301 vmbus_post_msg(&msg, sizeof(struct vmbus_channel_relid_released));
Dexuan Cuif52078c2015-12-14 16:01:50 -0800302}
K. Y. Srinivasanc8705972013-03-15 12:25:44 -0700303
Dexuan Cuif52078c2015-12-14 16:01:50 -0800304void hv_process_channel_removal(struct vmbus_channel *channel, u32 relid)
305{
306 unsigned long flags;
307 struct vmbus_channel *primary_channel;
308
309 vmbus_release_relid(relid);
K. Y. Srinivasaned6cfcc2015-02-28 11:18:17 -0800310
Dexuan Cui34c68012015-12-14 16:01:49 -0800311 BUG_ON(!channel->rescind);
312
K. Y. Srinivasan2115b562014-08-28 18:29:53 -0700313 if (channel->target_cpu != get_cpu()) {
314 put_cpu();
K. Y. Srinivasan3a28fa32014-04-08 18:45:54 -0700315 smp_call_function_single(channel->target_cpu,
316 percpu_channel_deq, channel, true);
K. Y. Srinivasan2115b562014-08-28 18:29:53 -0700317 } else {
K. Y. Srinivasan3a28fa32014-04-08 18:45:54 -0700318 percpu_channel_deq(channel);
K. Y. Srinivasan2115b562014-08-28 18:29:53 -0700319 put_cpu();
320 }
K. Y. Srinivasan3a28fa32014-04-08 18:45:54 -0700321
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700322 if (channel->primary_channel == NULL) {
Dexuan Cuid6f591e2015-12-14 16:01:51 -0800323 mutex_lock(&vmbus_connection.channel_mutex);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700324 list_del(&channel->listentry);
Dexuan Cuid6f591e2015-12-14 16:01:51 -0800325 mutex_unlock(&vmbus_connection.channel_mutex);
Dexuan Cuica1c4b72015-08-13 17:07:03 -0700326
327 primary_channel = channel;
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700328 } else {
329 primary_channel = channel->primary_channel;
Vitaly Kuznetsov67fae052015-01-20 16:45:05 +0100330 spin_lock_irqsave(&primary_channel->lock, flags);
K. Y. Srinivasan565ce642013-10-16 19:27:19 -0700331 list_del(&channel->sc_list);
Vitaly Kuznetsov357e8362015-05-06 17:47:44 -0700332 primary_channel->num_sc--;
Vitaly Kuznetsov67fae052015-01-20 16:45:05 +0100333 spin_unlock_irqrestore(&primary_channel->lock, flags);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700334 }
Dexuan Cuica1c4b72015-08-13 17:07:03 -0700335
336 /*
337 * We need to free the bit for init_vp_index() to work in the case
338 * of sub-channel, when we reload drivers like hv_netvsc.
339 */
340 cpumask_clear_cpu(channel->target_cpu,
341 &primary_channel->alloced_cpus_in_node);
342
K. Y. Srinivasanc8705972013-03-15 12:25:44 -0700343 free_channel(channel);
Timo Teräs4b2f9ab2010-12-15 20:48:09 +0200344}
Haiyang Zhang8b5d6d32010-05-28 23:22:44 +0000345
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -0800346void vmbus_free_channels(void)
347{
Dexuan Cui813c5b72015-04-22 21:31:30 -0700348 struct vmbus_channel *channel, *tmp;
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -0800349
Dexuan Cui813c5b72015-04-22 21:31:30 -0700350 list_for_each_entry_safe(channel, tmp, &vmbus_connection.chn_list,
351 listentry) {
Dexuan Cui34c68012015-12-14 16:01:49 -0800352 /* hv_process_channel_removal() needs this */
Dexuan Cui813c5b72015-04-22 21:31:30 -0700353 channel->rescind = true;
354
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -0800355 vmbus_device_unregister(channel->device_obj);
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -0800356 }
357}
358
Hank Janssen3e189512010-03-04 22:11:00 +0000359/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700360 * vmbus_process_offer - Process the offer by creating a channel/device
Hank Janssenc88c4e42010-05-04 15:55:05 -0700361 * associated with this offer
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700362 */
K. Y. Srinivasan2dd37cb2015-02-28 11:18:18 -0800363static void vmbus_process_offer(struct vmbus_channel *newchannel)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700364{
Greg Kroah-Hartmanaded7162009-08-18 15:21:19 -0700365 struct vmbus_channel *channel;
Haiyang Zhang188963e2010-10-15 10:14:06 -0700366 bool fnew = true;
Greg Kroah-Hartman0f5e44c2009-07-15 14:57:16 -0700367 unsigned long flags;
K. Y. Srinivasan7047f172015-12-25 20:00:30 -0800368 u16 dev_type;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700369
Bill Pemberton454f18a2009-07-27 16:47:24 -0400370 /* Make sure this is a new offer */
Dexuan Cuid6f591e2015-12-14 16:01:51 -0800371 mutex_lock(&vmbus_connection.channel_mutex);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700372
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800373 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
K. Y. Srinivasan358d2ee2011-08-25 09:48:28 -0700374 if (!uuid_le_cmp(channel->offermsg.offer.if_type,
375 newchannel->offermsg.offer.if_type) &&
376 !uuid_le_cmp(channel->offermsg.offer.if_instance,
377 newchannel->offermsg.offer.if_instance)) {
Haiyang Zhang188963e2010-10-15 10:14:06 -0700378 fnew = false;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700379 break;
380 }
381 }
382
Vitaly Kuznetsov8dfd3322015-05-06 17:47:42 -0700383 if (fnew)
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800384 list_add_tail(&newchannel->listentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800385 &vmbus_connection.chn_list);
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700386
Dexuan Cuid6f591e2015-12-14 16:01:51 -0800387 mutex_unlock(&vmbus_connection.channel_mutex);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700388
Haiyang Zhang188963e2010-10-15 10:14:06 -0700389 if (!fnew) {
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700390 /*
391 * Check to see if this is a sub-channel.
392 */
393 if (newchannel->offermsg.offer.sub_channel_index != 0) {
394 /*
395 * Process the sub-channel.
396 */
397 newchannel->primary_channel = channel;
Vitaly Kuznetsov67fae052015-01-20 16:45:05 +0100398 spin_lock_irqsave(&channel->lock, flags);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700399 list_add_tail(&newchannel->sc_list, &channel->sc_list);
K. Y. Srinivasana13e8bb2015-02-28 11:39:02 -0800400 channel->num_sc++;
Vitaly Kuznetsov357e8362015-05-06 17:47:44 -0700401 spin_unlock_irqrestore(&channel->lock, flags);
Vitaly Kuznetsov8dfd3322015-05-06 17:47:42 -0700402 } else
403 goto err_free_chan;
404 }
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700405
K. Y. Srinivasan7047f172015-12-25 20:00:30 -0800406 dev_type = hv_get_dev_type(&newchannel->offermsg.offer.if_type);
407
408 init_vp_index(newchannel, dev_type);
Vitaly Kuznetsovf38e7dd2015-05-06 17:47:45 -0700409
Vitaly Kuznetsov8dfd3322015-05-06 17:47:42 -0700410 if (newchannel->target_cpu != get_cpu()) {
411 put_cpu();
412 smp_call_function_single(newchannel->target_cpu,
413 percpu_channel_enq,
414 newchannel, true);
415 } else {
416 percpu_channel_enq(newchannel);
417 put_cpu();
Hank Janssen3e7ee492009-07-13 16:02:34 -0700418 }
419
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700420 /*
K. Y. Srinivasan42dceeb2013-08-26 14:08:58 -0700421 * This state is used to indicate a successful open
422 * so that when we do close the channel normally, we
423 * can cleanup properly
424 */
425 newchannel->state = CHANNEL_OPEN_STATE;
426
Vitaly Kuznetsov8dfd3322015-05-06 17:47:42 -0700427 if (!fnew) {
428 if (channel->sc_creation_callback != NULL)
429 channel->sc_creation_callback(newchannel);
430 return;
431 }
432
K. Y. Srinivasan42dceeb2013-08-26 14:08:58 -0700433 /*
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700434 * Start the process of binding this offer to the driver
435 * We need to set the DeviceObject field before calling
Haiyang Zhang646f1ea2011-01-26 12:12:10 -0800436 * vmbus_child_dev_add()
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700437 */
K. Y. Srinivasanf2c73012011-09-08 07:24:12 -0700438 newchannel->device_obj = vmbus_device_create(
Haiyang Zhang767dff62011-01-26 12:12:12 -0800439 &newchannel->offermsg.offer.if_type,
440 &newchannel->offermsg.offer.if_instance,
Haiyang Zhang188963e2010-10-15 10:14:06 -0700441 newchannel);
Vitaly Kuznetsov9c3a6f72015-01-20 16:45:04 +0100442 if (!newchannel->device_obj)
K. Y. Srinivasan5b1e5b52015-02-28 11:18:19 -0800443 goto err_deq_chan;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700444
K. Y. Srinivasan7047f172015-12-25 20:00:30 -0800445 newchannel->device_obj->device_id = dev_type;
Bill Pemberton454f18a2009-07-27 16:47:24 -0400446 /*
447 * Add the new device to the bus. This will kick off device-driver
448 * binding which eventually invokes the device driver's AddDevice()
449 * method.
450 */
Dexuan Cuid43e2fe2015-03-27 09:10:09 -0700451 if (vmbus_device_register(newchannel->device_obj) != 0) {
452 pr_err("unable to add child device object (relid %d)\n",
453 newchannel->offermsg.child_relid);
454 kfree(newchannel->device_obj);
455 goto err_deq_chan;
456 }
Vitaly Kuznetsov9c3a6f72015-01-20 16:45:04 +0100457 return;
K. Y. Srinivasan2dd37cb2015-02-28 11:18:18 -0800458
K. Y. Srinivasan5b1e5b52015-02-28 11:18:19 -0800459err_deq_chan:
Dexuan Cuif52078c2015-12-14 16:01:50 -0800460 vmbus_release_relid(newchannel->offermsg.child_relid);
461
Dexuan Cuid6f591e2015-12-14 16:01:51 -0800462 mutex_lock(&vmbus_connection.channel_mutex);
K. Y. Srinivasan5b1e5b52015-02-28 11:18:19 -0800463 list_del(&newchannel->listentry);
Dexuan Cuid6f591e2015-12-14 16:01:51 -0800464 mutex_unlock(&vmbus_connection.channel_mutex);
K. Y. Srinivasan5b1e5b52015-02-28 11:18:19 -0800465
466 if (newchannel->target_cpu != get_cpu()) {
467 put_cpu();
468 smp_call_function_single(newchannel->target_cpu,
469 percpu_channel_deq, newchannel, true);
470 } else {
471 percpu_channel_deq(newchannel);
472 put_cpu();
473 }
474
Vitaly Kuznetsov9c3a6f72015-01-20 16:45:04 +0100475err_free_chan:
476 free_channel(newchannel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700477}
478
K. Y. Srinivasana1198452012-12-01 06:46:50 -0800479/*
480 * We use this state to statically distribute the channel interrupt load.
481 */
K. Y. Srinivasan1f656ff2015-05-30 23:37:48 -0700482static int next_numa_node_id;
K. Y. Srinivasana1198452012-12-01 06:46:50 -0800483
484/*
485 * Starting with Win8, we can statically distribute the incoming
K. Y. Srinivasan1f656ff2015-05-30 23:37:48 -0700486 * channel interrupt load by binding a channel to VCPU.
487 * We do this in a hierarchical fashion:
488 * First distribute the primary channels across available NUMA nodes
489 * and then distribute the subchannels amongst the CPUs in the NUMA
490 * node assigned to the primary channel.
491 *
492 * For pre-win8 hosts or non-performance critical channels we assign the
493 * first CPU in the first NUMA node.
K. Y. Srinivasana1198452012-12-01 06:46:50 -0800494 */
K. Y. Srinivasan7047f172015-12-25 20:00:30 -0800495static void init_vp_index(struct vmbus_channel *channel, u16 dev_type)
K. Y. Srinivasana1198452012-12-01 06:46:50 -0800496{
497 u32 cur_cpu;
K. Y. Srinivasan7047f172015-12-25 20:00:30 -0800498 bool perf_chn = vmbus_devs[dev_type].perf_device;
K. Y. Srinivasan1f656ff2015-05-30 23:37:48 -0700499 struct vmbus_channel *primary = channel->primary_channel;
500 int next_node;
501 struct cpumask available_mask;
K. Y. Srinivasan9f01ec52015-08-05 00:52:38 -0700502 struct cpumask *alloced_mask;
K. Y. Srinivasana1198452012-12-01 06:46:50 -0800503
K. Y. Srinivasana1198452012-12-01 06:46:50 -0800504 if ((vmbus_proto_version == VERSION_WS2008) ||
505 (vmbus_proto_version == VERSION_WIN7) || (!perf_chn)) {
506 /*
507 * Prior to win8, all channel interrupts are
508 * delivered on cpu 0.
509 * Also if the channel is not a performance critical
510 * channel, bind it to cpu 0.
511 */
K. Y. Srinivasan1f656ff2015-05-30 23:37:48 -0700512 channel->numa_node = 0;
K. Y. Srinivasand3ba7202014-04-08 18:45:53 -0700513 channel->target_cpu = 0;
K. Y. Srinivasan9c6e64a2015-05-30 23:37:47 -0700514 channel->target_vp = hv_context.vp_index[0];
K. Y. Srinivasand3ba7202014-04-08 18:45:53 -0700515 return;
K. Y. Srinivasana1198452012-12-01 06:46:50 -0800516 }
Vitaly Kuznetsovce59fec2015-05-06 17:47:46 -0700517
518 /*
K. Y. Srinivasan1f656ff2015-05-30 23:37:48 -0700519 * We distribute primary channels evenly across all the available
520 * NUMA nodes and within the assigned NUMA node we will assign the
521 * first available CPU to the primary channel.
522 * The sub-channels will be assigned to the CPUs available in the
523 * NUMA node evenly.
Vitaly Kuznetsovce59fec2015-05-06 17:47:46 -0700524 */
K. Y. Srinivasan1f656ff2015-05-30 23:37:48 -0700525 if (!primary) {
526 while (true) {
527 next_node = next_numa_node_id++;
528 if (next_node == nr_node_ids)
529 next_node = next_numa_node_id = 0;
530 if (cpumask_empty(cpumask_of_node(next_node)))
531 continue;
532 break;
Vitaly Kuznetsovce59fec2015-05-06 17:47:46 -0700533 }
K. Y. Srinivasan1f656ff2015-05-30 23:37:48 -0700534 channel->numa_node = next_node;
535 primary = channel;
Vitaly Kuznetsovce59fec2015-05-06 17:47:46 -0700536 }
K. Y. Srinivasan9f01ec52015-08-05 00:52:38 -0700537 alloced_mask = &hv_context.hv_numa_map[primary->numa_node];
Vitaly Kuznetsovce59fec2015-05-06 17:47:46 -0700538
K. Y. Srinivasan9f01ec52015-08-05 00:52:38 -0700539 if (cpumask_weight(alloced_mask) ==
K. Y. Srinivasan1f656ff2015-05-30 23:37:48 -0700540 cpumask_weight(cpumask_of_node(primary->numa_node))) {
541 /*
542 * We have cycled through all the CPUs in the node;
543 * reset the alloced map.
544 */
K. Y. Srinivasan9f01ec52015-08-05 00:52:38 -0700545 cpumask_clear(alloced_mask);
K. Y. Srinivasan1f656ff2015-05-30 23:37:48 -0700546 }
547
K. Y. Srinivasan9f01ec52015-08-05 00:52:38 -0700548 cpumask_xor(&available_mask, alloced_mask,
K. Y. Srinivasan1f656ff2015-05-30 23:37:48 -0700549 cpumask_of_node(primary->numa_node));
550
Dexuan Cui3b711072015-08-05 00:52:39 -0700551 cur_cpu = -1;
552 while (true) {
553 cur_cpu = cpumask_next(cur_cpu, &available_mask);
554 if (cur_cpu >= nr_cpu_ids) {
555 cur_cpu = -1;
556 cpumask_copy(&available_mask,
557 cpumask_of_node(primary->numa_node));
558 continue;
559 }
560
Dexuan Cuica1c4b72015-08-13 17:07:03 -0700561 /*
562 * NOTE: in the case of sub-channel, we clear the sub-channel
563 * related bit(s) in primary->alloced_cpus_in_node in
564 * hv_process_channel_removal(), so when we reload drivers
565 * like hv_netvsc in SMP guest, here we're able to re-allocate
566 * bit from primary->alloced_cpus_in_node.
567 */
Dexuan Cui3b711072015-08-05 00:52:39 -0700568 if (!cpumask_test_cpu(cur_cpu,
569 &primary->alloced_cpus_in_node)) {
570 cpumask_set_cpu(cur_cpu,
571 &primary->alloced_cpus_in_node);
572 cpumask_set_cpu(cur_cpu, alloced_mask);
573 break;
574 }
575 }
K. Y. Srinivasan1f656ff2015-05-30 23:37:48 -0700576
K. Y. Srinivasand3ba7202014-04-08 18:45:53 -0700577 channel->target_cpu = cur_cpu;
578 channel->target_vp = hv_context.vp_index[cur_cpu];
K. Y. Srinivasana1198452012-12-01 06:46:50 -0800579}
580
Hank Janssen3e189512010-03-04 22:11:00 +0000581/*
K. Y. Srinivasan2db84ef2015-04-22 21:31:32 -0700582 * vmbus_unload_response - Handler for the unload response.
583 */
584static void vmbus_unload_response(struct vmbus_channel_message_header *hdr)
585{
586 /*
587 * This is a global event; just wakeup the waiting thread.
588 * Once we successfully unload, we can cleanup the monitor state.
589 */
590 complete(&vmbus_connection.unload_event);
591}
592
593void vmbus_initiate_unload(void)
594{
595 struct vmbus_channel_message_header hdr;
596
Vitaly Kuznetsov4a542432015-08-01 16:08:19 -0700597 /* Pre-Win2012R2 hosts don't support reconnect */
598 if (vmbus_proto_version < VERSION_WIN8_1)
599 return;
600
K. Y. Srinivasan2db84ef2015-04-22 21:31:32 -0700601 init_completion(&vmbus_connection.unload_event);
602 memset(&hdr, 0, sizeof(struct vmbus_channel_message_header));
603 hdr.msgtype = CHANNELMSG_UNLOAD;
604 vmbus_post_msg(&hdr, sizeof(struct vmbus_channel_message_header));
605
606 wait_for_completion(&vmbus_connection.unload_event);
607}
608
609/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700610 * vmbus_onoffer - Handler for channel offers from vmbus in parent partition.
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700611 *
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700612 */
Haiyang Zhange98cb272010-10-15 10:14:07 -0700613static void vmbus_onoffer(struct vmbus_channel_message_header *hdr)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700614{
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700615 struct vmbus_channel_offer_channel *offer;
Haiyang Zhang188963e2010-10-15 10:14:06 -0700616 struct vmbus_channel *newchannel;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700617
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700618 offer = (struct vmbus_channel_offer_channel *)hdr;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700619
Bill Pemberton454f18a2009-07-27 16:47:24 -0400620 /* Allocate the channel object and save this offer. */
Haiyang Zhange98cb272010-10-15 10:14:07 -0700621 newchannel = alloc_channel();
Haiyang Zhang188963e2010-10-15 10:14:06 -0700622 if (!newchannel) {
Hank Janssen0a466182011-03-29 13:58:47 -0700623 pr_err("Unable to allocate channel object\n");
Hank Janssen3e7ee492009-07-13 16:02:34 -0700624 return;
625 }
626
K. Y. Srinivasan132368b2012-12-01 06:46:33 -0800627 /*
628 * By default we setup state to enable batched
629 * reading. A specific service can choose to
630 * disable this prior to opening the channel.
631 */
632 newchannel->batched_reading = true;
633
K. Y. Srinivasanb3bf60c2012-12-01 06:46:45 -0800634 /*
635 * Setup state for signalling the host.
636 */
637 newchannel->sig_event = (struct hv_input_signal_event *)
638 (ALIGN((unsigned long)
639 &newchannel->sig_buf,
640 HV_HYPERCALL_PARAM_ALIGN));
641
642 newchannel->sig_event->connectionid.asu32 = 0;
643 newchannel->sig_event->connectionid.u.id = VMBUS_EVENT_CONNECTION_ID;
644 newchannel->sig_event->flag_number = 0;
645 newchannel->sig_event->rsvdz = 0;
646
647 if (vmbus_proto_version != VERSION_WS2008) {
648 newchannel->is_dedicated_interrupt =
649 (offer->is_dedicated_interrupt != 0);
650 newchannel->sig_event->connectionid.u.id =
651 offer->connection_id;
652 }
653
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800654 memcpy(&newchannel->offermsg, offer,
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700655 sizeof(struct vmbus_channel_offer_channel));
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800656 newchannel->monitor_grp = (u8)offer->monitorid / 32;
657 newchannel->monitor_bit = (u8)offer->monitorid % 32;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700658
K. Y. Srinivasan2dd37cb2015-02-28 11:18:18 -0800659 vmbus_process_offer(newchannel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700660}
661
Hank Janssen3e189512010-03-04 22:11:00 +0000662/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700663 * vmbus_onoffer_rescind - Rescind offer handler.
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700664 *
665 * We queue a work item to process this offer synchronously
666 */
Haiyang Zhange98cb272010-10-15 10:14:07 -0700667static void vmbus_onoffer_rescind(struct vmbus_channel_message_header *hdr)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700668{
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700669 struct vmbus_channel_rescind_offer *rescind;
Greg Kroah-Hartmanaded7162009-08-18 15:21:19 -0700670 struct vmbus_channel *channel;
Dexuan Cuid43e2fe2015-03-27 09:10:09 -0700671 unsigned long flags;
672 struct device *dev;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700673
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700674 rescind = (struct vmbus_channel_rescind_offer *)hdr;
Dexuan Cuid43e2fe2015-03-27 09:10:09 -0700675 channel = relid2channel(rescind->child_relid);
Hank Janssen98e08702011-03-29 13:58:44 -0700676
K. Y. Srinivasan2dd37cb2015-02-28 11:18:18 -0800677 if (channel == NULL) {
Dexuan Cuif52078c2015-12-14 16:01:50 -0800678 /*
679 * This is very impossible, because in
680 * vmbus_process_offer(), we have already invoked
681 * vmbus_release_relid() on error.
682 */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700683 return;
K. Y. Srinivasan2dd37cb2015-02-28 11:18:18 -0800684 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700685
Dexuan Cuid43e2fe2015-03-27 09:10:09 -0700686 spin_lock_irqsave(&channel->lock, flags);
687 channel->rescind = true;
688 spin_unlock_irqrestore(&channel->lock, flags);
689
690 if (channel->device_obj) {
691 /*
692 * We will have to unregister this device from the
693 * driver core.
694 */
695 dev = get_device(&channel->device_obj->device);
696 if (dev) {
697 vmbus_device_unregister(channel->device_obj);
698 put_device(dev);
699 }
700 } else {
701 hv_process_channel_removal(channel,
702 channel->offermsg.child_relid);
K. Y. Srinivasan2dd37cb2015-02-28 11:18:18 -0800703 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700704}
705
Hank Janssen3e189512010-03-04 22:11:00 +0000706/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700707 * vmbus_onoffers_delivered -
708 * This is invoked when all offers have been delivered.
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700709 *
710 * Nothing to do here.
711 */
Haiyang Zhange98cb272010-10-15 10:14:07 -0700712static void vmbus_onoffers_delivered(
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700713 struct vmbus_channel_message_header *hdr)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700714{
Hank Janssen3e7ee492009-07-13 16:02:34 -0700715}
716
Hank Janssen3e189512010-03-04 22:11:00 +0000717/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700718 * vmbus_onopen_result - Open result handler.
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700719 *
720 * This is invoked when we received a response to our channel open request.
721 * Find the matching request, copy the response and signal the requesting
722 * thread.
723 */
Haiyang Zhange98cb272010-10-15 10:14:07 -0700724static void vmbus_onopen_result(struct vmbus_channel_message_header *hdr)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700725{
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700726 struct vmbus_channel_open_result *result;
Haiyang Zhang188963e2010-10-15 10:14:06 -0700727 struct vmbus_channel_msginfo *msginfo;
728 struct vmbus_channel_message_header *requestheader;
729 struct vmbus_channel_open_channel *openmsg;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700730 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700731
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700732 result = (struct vmbus_channel_open_result *)hdr;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700733
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700734 /*
735 * Find the open msg, copy the result and signal/unblock the wait event
736 */
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800737 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700738
Hank Janssenebb61e52011-02-18 12:39:57 -0800739 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
740 msglistentry) {
Haiyang Zhang188963e2010-10-15 10:14:06 -0700741 requestheader =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800742 (struct vmbus_channel_message_header *)msginfo->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700743
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800744 if (requestheader->msgtype == CHANNELMSG_OPENCHANNEL) {
Haiyang Zhang188963e2010-10-15 10:14:06 -0700745 openmsg =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800746 (struct vmbus_channel_open_channel *)msginfo->msg;
747 if (openmsg->child_relid == result->child_relid &&
748 openmsg->openid == result->openid) {
749 memcpy(&msginfo->response.open_result,
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700750 result,
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700751 sizeof(
752 struct vmbus_channel_open_result));
753 complete(&msginfo->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700754 break;
755 }
756 }
757 }
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800758 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700759}
760
Hank Janssen3e189512010-03-04 22:11:00 +0000761/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700762 * vmbus_ongpadl_created - GPADL created handler.
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700763 *
764 * This is invoked when we received a response to our gpadl create request.
765 * Find the matching request, copy the response and signal the requesting
766 * thread.
767 */
Haiyang Zhange98cb272010-10-15 10:14:07 -0700768static void vmbus_ongpadl_created(struct vmbus_channel_message_header *hdr)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700769{
Haiyang Zhang188963e2010-10-15 10:14:06 -0700770 struct vmbus_channel_gpadl_created *gpadlcreated;
Haiyang Zhang188963e2010-10-15 10:14:06 -0700771 struct vmbus_channel_msginfo *msginfo;
772 struct vmbus_channel_message_header *requestheader;
773 struct vmbus_channel_gpadl_header *gpadlheader;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700774 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700775
Haiyang Zhang188963e2010-10-15 10:14:06 -0700776 gpadlcreated = (struct vmbus_channel_gpadl_created *)hdr;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700777
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700778 /*
779 * Find the establish msg, copy the result and signal/unblock the wait
780 * event
781 */
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800782 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700783
Hank Janssenebb61e52011-02-18 12:39:57 -0800784 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
785 msglistentry) {
Haiyang Zhang188963e2010-10-15 10:14:06 -0700786 requestheader =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800787 (struct vmbus_channel_message_header *)msginfo->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700788
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800789 if (requestheader->msgtype == CHANNELMSG_GPADL_HEADER) {
Haiyang Zhang188963e2010-10-15 10:14:06 -0700790 gpadlheader =
791 (struct vmbus_channel_gpadl_header *)requestheader;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700792
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800793 if ((gpadlcreated->child_relid ==
794 gpadlheader->child_relid) &&
795 (gpadlcreated->gpadl == gpadlheader->gpadl)) {
796 memcpy(&msginfo->response.gpadl_created,
Haiyang Zhang188963e2010-10-15 10:14:06 -0700797 gpadlcreated,
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700798 sizeof(
799 struct vmbus_channel_gpadl_created));
800 complete(&msginfo->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700801 break;
802 }
803 }
804 }
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800805 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700806}
807
Hank Janssen3e189512010-03-04 22:11:00 +0000808/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700809 * vmbus_ongpadl_torndown - GPADL torndown handler.
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700810 *
811 * This is invoked when we received a response to our gpadl teardown request.
812 * Find the matching request, copy the response and signal the requesting
813 * thread.
814 */
Haiyang Zhange98cb272010-10-15 10:14:07 -0700815static void vmbus_ongpadl_torndown(
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700816 struct vmbus_channel_message_header *hdr)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700817{
Haiyang Zhang188963e2010-10-15 10:14:06 -0700818 struct vmbus_channel_gpadl_torndown *gpadl_torndown;
Haiyang Zhang188963e2010-10-15 10:14:06 -0700819 struct vmbus_channel_msginfo *msginfo;
820 struct vmbus_channel_message_header *requestheader;
821 struct vmbus_channel_gpadl_teardown *gpadl_teardown;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700822 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700823
Haiyang Zhang188963e2010-10-15 10:14:06 -0700824 gpadl_torndown = (struct vmbus_channel_gpadl_torndown *)hdr;
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700825
826 /*
827 * Find the open msg, copy the result and signal/unblock the wait event
828 */
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800829 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700830
Hank Janssenebb61e52011-02-18 12:39:57 -0800831 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
832 msglistentry) {
Haiyang Zhang188963e2010-10-15 10:14:06 -0700833 requestheader =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800834 (struct vmbus_channel_message_header *)msginfo->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700835
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800836 if (requestheader->msgtype == CHANNELMSG_GPADL_TEARDOWN) {
Haiyang Zhang188963e2010-10-15 10:14:06 -0700837 gpadl_teardown =
838 (struct vmbus_channel_gpadl_teardown *)requestheader;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700839
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800840 if (gpadl_torndown->gpadl == gpadl_teardown->gpadl) {
841 memcpy(&msginfo->response.gpadl_torndown,
Haiyang Zhang188963e2010-10-15 10:14:06 -0700842 gpadl_torndown,
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700843 sizeof(
844 struct vmbus_channel_gpadl_torndown));
845 complete(&msginfo->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700846 break;
847 }
848 }
849 }
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800850 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700851}
852
Hank Janssen3e189512010-03-04 22:11:00 +0000853/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700854 * vmbus_onversion_response - Version response handler
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700855 *
856 * This is invoked when we received a response to our initiate contact request.
857 * Find the matching request, copy the response and signal the requesting
858 * thread.
859 */
Haiyang Zhange98cb272010-10-15 10:14:07 -0700860static void vmbus_onversion_response(
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700861 struct vmbus_channel_message_header *hdr)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700862{
Haiyang Zhang188963e2010-10-15 10:14:06 -0700863 struct vmbus_channel_msginfo *msginfo;
864 struct vmbus_channel_message_header *requestheader;
Haiyang Zhang188963e2010-10-15 10:14:06 -0700865 struct vmbus_channel_version_response *version_response;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700866 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700867
Haiyang Zhang188963e2010-10-15 10:14:06 -0700868 version_response = (struct vmbus_channel_version_response *)hdr;
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800869 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700870
Hank Janssenebb61e52011-02-18 12:39:57 -0800871 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
872 msglistentry) {
Haiyang Zhang188963e2010-10-15 10:14:06 -0700873 requestheader =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800874 (struct vmbus_channel_message_header *)msginfo->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700875
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800876 if (requestheader->msgtype ==
877 CHANNELMSG_INITIATE_CONTACT) {
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800878 memcpy(&msginfo->response.version_response,
Haiyang Zhang188963e2010-10-15 10:14:06 -0700879 version_response,
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700880 sizeof(struct vmbus_channel_version_response));
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700881 complete(&msginfo->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700882 }
883 }
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800884 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700885}
886
Greg Kroah-Hartmanc8212f02009-08-31 21:51:50 -0700887/* Channel message dispatch table */
Dexuan Cui652594c2015-03-27 09:10:08 -0700888struct vmbus_channel_message_table_entry
K. Y. Srinivasanb7c6b022011-05-10 07:55:38 -0700889 channel_message_table[CHANNELMSG_COUNT] = {
Dexuan Cui652594c2015-03-27 09:10:08 -0700890 {CHANNELMSG_INVALID, 0, NULL},
891 {CHANNELMSG_OFFERCHANNEL, 0, vmbus_onoffer},
892 {CHANNELMSG_RESCIND_CHANNELOFFER, 0, vmbus_onoffer_rescind},
893 {CHANNELMSG_REQUESTOFFERS, 0, NULL},
894 {CHANNELMSG_ALLOFFERS_DELIVERED, 1, vmbus_onoffers_delivered},
895 {CHANNELMSG_OPENCHANNEL, 0, NULL},
896 {CHANNELMSG_OPENCHANNEL_RESULT, 1, vmbus_onopen_result},
897 {CHANNELMSG_CLOSECHANNEL, 0, NULL},
898 {CHANNELMSG_GPADL_HEADER, 0, NULL},
899 {CHANNELMSG_GPADL_BODY, 0, NULL},
900 {CHANNELMSG_GPADL_CREATED, 1, vmbus_ongpadl_created},
901 {CHANNELMSG_GPADL_TEARDOWN, 0, NULL},
902 {CHANNELMSG_GPADL_TORNDOWN, 1, vmbus_ongpadl_torndown},
903 {CHANNELMSG_RELID_RELEASED, 0, NULL},
904 {CHANNELMSG_INITIATE_CONTACT, 0, NULL},
905 {CHANNELMSG_VERSION_RESPONSE, 1, vmbus_onversion_response},
906 {CHANNELMSG_UNLOAD, 0, NULL},
K. Y. Srinivasan2db84ef2015-04-22 21:31:32 -0700907 {CHANNELMSG_UNLOAD_RESPONSE, 1, vmbus_unload_response},
Greg Kroah-Hartmanc8212f02009-08-31 21:51:50 -0700908};
909
Hank Janssen3e189512010-03-04 22:11:00 +0000910/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700911 * vmbus_onmessage - Handler for channel protocol messages.
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700912 *
913 * This is invoked in the vmbus worker thread context.
914 */
Haiyang Zhange98cb272010-10-15 10:14:07 -0700915void vmbus_onmessage(void *context)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700916{
Haiyang Zhang188963e2010-10-15 10:14:06 -0700917 struct hv_message *msg = context;
Greg Kroah-Hartman82250212009-08-26 15:16:04 -0700918 struct vmbus_channel_message_header *hdr;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700919 int size;
920
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800921 hdr = (struct vmbus_channel_message_header *)msg->u.payload;
922 size = msg->header.payload_size;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700923
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800924 if (hdr->msgtype >= CHANNELMSG_COUNT) {
Hank Janssen0a466182011-03-29 13:58:47 -0700925 pr_err("Received invalid channel message type %d size %d\n",
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800926 hdr->msgtype, size);
Greg Kroah-Hartman04f50c42009-07-16 12:35:37 -0700927 print_hex_dump_bytes("", DUMP_PREFIX_NONE,
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800928 (unsigned char *)msg->u.payload, size);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700929 return;
930 }
931
K. Y. Srinivasanb7c6b022011-05-10 07:55:38 -0700932 if (channel_message_table[hdr->msgtype].message_handler)
933 channel_message_table[hdr->msgtype].message_handler(hdr);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700934 else
Hank Janssen0a466182011-03-29 13:58:47 -0700935 pr_err("Unhandled channel message type %d\n", hdr->msgtype);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700936}
937
Hank Janssen3e189512010-03-04 22:11:00 +0000938/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700939 * vmbus_request_offers - Send a request to get all our pending offers.
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700940 */
Haiyang Zhange98cb272010-10-15 10:14:07 -0700941int vmbus_request_offers(void)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700942{
Greg Kroah-Hartman82250212009-08-26 15:16:04 -0700943 struct vmbus_channel_message_header *msg;
Haiyang Zhang188963e2010-10-15 10:14:06 -0700944 struct vmbus_channel_msginfo *msginfo;
Nicholas Mc Guire51e51812015-02-27 11:26:02 -0800945 int ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700946
Haiyang Zhang188963e2010-10-15 10:14:06 -0700947 msginfo = kmalloc(sizeof(*msginfo) +
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700948 sizeof(struct vmbus_channel_message_header),
949 GFP_KERNEL);
Haiyang Zhang188963e2010-10-15 10:14:06 -0700950 if (!msginfo)
Bill Pemberton75910f22010-05-05 15:27:31 -0400951 return -ENOMEM;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700952
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800953 msg = (struct vmbus_channel_message_header *)msginfo->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700954
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800955 msg->msgtype = CHANNELMSG_REQUESTOFFERS;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700956
Hank Janssen3e7ee492009-07-13 16:02:34 -0700957
Haiyang Zhangc6977672011-01-26 12:12:08 -0800958 ret = vmbus_post_msg(msg,
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700959 sizeof(struct vmbus_channel_message_header));
960 if (ret != 0) {
Hank Janssen0a466182011-03-29 13:58:47 -0700961 pr_err("Unable to request offers - %d\n", ret);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700962
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800963 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700964 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700965
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800966cleanup:
Ilia Mirkindd9b15d2011-03-13 00:29:00 -0500967 kfree(msginfo);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700968
Hank Janssen3e7ee492009-07-13 16:02:34 -0700969 return ret;
970}
971
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700972/*
973 * Retrieve the (sub) channel on which to send an outgoing request.
K. Y. Srinivasana13e8bb2015-02-28 11:39:02 -0800974 * When a primary channel has multiple sub-channels, we try to
975 * distribute the load equally amongst all available channels.
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700976 */
977struct vmbus_channel *vmbus_get_outgoing_channel(struct vmbus_channel *primary)
978{
979 struct list_head *cur, *tmp;
K. Y. Srinivasan87712bf82014-12-14 23:34:51 -0800980 int cur_cpu;
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700981 struct vmbus_channel *cur_channel;
982 struct vmbus_channel *outgoing_channel = primary;
K. Y. Srinivasana13e8bb2015-02-28 11:39:02 -0800983 int next_channel;
984 int i = 1;
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700985
986 if (list_empty(&primary->sc_list))
987 return outgoing_channel;
988
K. Y. Srinivasana13e8bb2015-02-28 11:39:02 -0800989 next_channel = primary->next_oc++;
990
991 if (next_channel > (primary->num_sc)) {
992 primary->next_oc = 0;
993 return outgoing_channel;
994 }
995
K. Y. Srinivasan87712bf82014-12-14 23:34:51 -0800996 cur_cpu = hv_context.vp_index[get_cpu()];
997 put_cpu();
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700998 list_for_each_safe(cur, tmp, &primary->sc_list) {
999 cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
1000 if (cur_channel->state != CHANNEL_OPENED_STATE)
1001 continue;
1002
1003 if (cur_channel->target_vp == cur_cpu)
1004 return cur_channel;
1005
K. Y. Srinivasana13e8bb2015-02-28 11:39:02 -08001006 if (i == next_channel)
1007 return cur_channel;
K. Y. Srinivasane68d2972013-05-23 12:02:32 -07001008
K. Y. Srinivasana13e8bb2015-02-28 11:39:02 -08001009 i++;
K. Y. Srinivasane68d2972013-05-23 12:02:32 -07001010 }
1011
1012 return outgoing_channel;
1013}
1014EXPORT_SYMBOL_GPL(vmbus_get_outgoing_channel);
1015
1016static void invoke_sc_cb(struct vmbus_channel *primary_channel)
1017{
1018 struct list_head *cur, *tmp;
1019 struct vmbus_channel *cur_channel;
1020
1021 if (primary_channel->sc_creation_callback == NULL)
1022 return;
1023
1024 list_for_each_safe(cur, tmp, &primary_channel->sc_list) {
1025 cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
1026
1027 primary_channel->sc_creation_callback(cur_channel);
1028 }
1029}
1030
1031void vmbus_set_sc_create_callback(struct vmbus_channel *primary_channel,
1032 void (*sc_cr_cb)(struct vmbus_channel *new_sc))
1033{
1034 primary_channel->sc_creation_callback = sc_cr_cb;
1035}
1036EXPORT_SYMBOL_GPL(vmbus_set_sc_create_callback);
1037
1038bool vmbus_are_subchannels_present(struct vmbus_channel *primary)
1039{
1040 bool ret;
1041
1042 ret = !list_empty(&primary->sc_list);
1043
1044 if (ret) {
1045 /*
1046 * Invoke the callback on sub-channel creation.
1047 * This will present a uniform interface to the
1048 * clients.
1049 */
1050 invoke_sc_cb(primary);
1051 }
1052
1053 return ret;
1054}
1055EXPORT_SYMBOL_GPL(vmbus_are_subchannels_present);