blob: fa920469bf104fda2e57d9c2bce2f15f90d5b2a4 [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
Greg Kroah-Hartman1d7e9072009-08-28 16:26:11 -070035struct vmbus_channel_message_table_entry {
K. Y. Srinivasancf9dcba2011-05-10 07:55:37 -070036 enum vmbus_channel_message_type message_type;
K. Y. Srinivasanfa90f1d2011-05-10 07:55:36 -070037 void (*message_handler)(struct vmbus_channel_message_header *msg);
Greg Kroah-Hartman1d7e9072009-08-28 16:26:11 -070038};
Hank Janssen3e7ee492009-07-13 16:02:34 -070039
Hank Janssenc88c4e42010-05-04 15:55:05 -070040
41/**
Greg Kroah-Hartmanda0e9632011-10-11 08:42:28 -060042 * vmbus_prep_negotiate_resp() - Create default response for Hyper-V Negotiate message
Hank Janssenc88c4e42010-05-04 15:55:05 -070043 * @icmsghdrp: Pointer to msg header structure
44 * @icmsg_negotiate: Pointer to negotiate message structure
45 * @buf: Raw buffer channel data
46 *
47 * @icmsghdrp is of type &struct icmsg_hdr.
48 * @negop is of type &struct icmsg_negotiate.
K. Y. Srinivasanc836d0a2012-05-12 13:44:58 -070049 * Set up and fill in default negotiate response message.
50 *
K. Y. Srinivasan67413352013-07-02 10:31:30 -070051 * The fw_version specifies the framework version that
52 * we can support and srv_version specifies the service
53 * version we can support.
Hank Janssenc88c4e42010-05-04 15:55:05 -070054 *
55 * Mainly used by Hyper-V drivers.
56 */
K. Y. Srinivasan67413352013-07-02 10:31:30 -070057bool vmbus_prep_negotiate_resp(struct icmsg_hdr *icmsghdrp,
K. Y. Srinivasanc836d0a2012-05-12 13:44:58 -070058 struct icmsg_negotiate *negop, u8 *buf,
K. Y. Srinivasan67413352013-07-02 10:31:30 -070059 int fw_version, int srv_version)
Hank Janssenc88c4e42010-05-04 15:55:05 -070060{
K. Y. Srinivasan67413352013-07-02 10:31:30 -070061 int icframe_major, icframe_minor;
62 int icmsg_major, icmsg_minor;
63 int fw_major, fw_minor;
64 int srv_major, srv_minor;
K. Y. Srinivasanc836d0a2012-05-12 13:44:58 -070065 int i;
K. Y. Srinivasan67413352013-07-02 10:31:30 -070066 bool found_match = false;
K. Y. Srinivasanc836d0a2012-05-12 13:44:58 -070067
K. Y. Srinivasana3605302012-05-12 13:44:57 -070068 icmsghdrp->icmsgsize = 0x10;
K. Y. Srinivasan67413352013-07-02 10:31:30 -070069 fw_major = (fw_version >> 16);
70 fw_minor = (fw_version & 0xFFFF);
71
72 srv_major = (srv_version >> 16);
73 srv_minor = (srv_version & 0xFFFF);
Hank Janssenc88c4e42010-05-04 15:55:05 -070074
K. Y. Srinivasana3605302012-05-12 13:44:57 -070075 negop = (struct icmsg_negotiate *)&buf[
76 sizeof(struct vmbuspipe_hdr) +
77 sizeof(struct icmsg_hdr)];
Hank Janssenc88c4e42010-05-04 15:55:05 -070078
K. Y. Srinivasan67413352013-07-02 10:31:30 -070079 icframe_major = negop->icframe_vercnt;
80 icframe_minor = 0;
81
82 icmsg_major = negop->icmsg_vercnt;
83 icmsg_minor = 0;
K. Y. Srinivasanc836d0a2012-05-12 13:44:58 -070084
85 /*
86 * Select the framework version number we will
87 * support.
88 */
89
90 for (i = 0; i < negop->icframe_vercnt; i++) {
K. Y. Srinivasan67413352013-07-02 10:31:30 -070091 if ((negop->icversion_data[i].major == fw_major) &&
92 (negop->icversion_data[i].minor == fw_minor)) {
93 icframe_major = negop->icversion_data[i].major;
94 icframe_minor = negop->icversion_data[i].minor;
95 found_match = true;
96 }
Hank Janssenc88c4e42010-05-04 15:55:05 -070097 }
K. Y. Srinivasana3605302012-05-12 13:44:57 -070098
K. Y. Srinivasan67413352013-07-02 10:31:30 -070099 if (!found_match)
100 goto fw_error;
101
102 found_match = false;
103
K. Y. Srinivasanc836d0a2012-05-12 13:44:58 -0700104 for (i = negop->icframe_vercnt;
105 (i < negop->icframe_vercnt + negop->icmsg_vercnt); i++) {
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700106 if ((negop->icversion_data[i].major == srv_major) &&
107 (negop->icversion_data[i].minor == srv_minor)) {
108 icmsg_major = negop->icversion_data[i].major;
109 icmsg_minor = negop->icversion_data[i].minor;
110 found_match = true;
111 }
K. Y. Srinivasanc836d0a2012-05-12 13:44:58 -0700112 }
113
114 /*
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700115 * Respond with the framework and service
K. Y. Srinivasanc836d0a2012-05-12 13:44:58 -0700116 * version numbers we can support.
117 */
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700118
119fw_error:
120 if (!found_match) {
121 negop->icframe_vercnt = 0;
122 negop->icmsg_vercnt = 0;
123 } else {
124 negop->icframe_vercnt = 1;
125 negop->icmsg_vercnt = 1;
126 }
127
128 negop->icversion_data[0].major = icframe_major;
129 negop->icversion_data[0].minor = icframe_minor;
130 negop->icversion_data[1].major = icmsg_major;
131 negop->icversion_data[1].minor = icmsg_minor;
132 return found_match;
Hank Janssenc88c4e42010-05-04 15:55:05 -0700133}
K. Y. Srinivasana3605302012-05-12 13:44:57 -0700134
Greg Kroah-Hartmanda0e9632011-10-11 08:42:28 -0600135EXPORT_SYMBOL_GPL(vmbus_prep_negotiate_resp);
Hank Janssenc88c4e42010-05-04 15:55:05 -0700136
Hank Janssen3e189512010-03-04 22:11:00 +0000137/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700138 * alloc_channel - Allocate and initialize a vmbus channel object
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700139 */
Greg Kroah-Hartman50fe56d2010-10-20 15:51:57 -0700140static struct vmbus_channel *alloc_channel(void)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700141{
Greg Kroah-Hartmanaded7162009-08-18 15:21:19 -0700142 struct vmbus_channel *channel;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700143
Greg Kroah-Hartmanaded7162009-08-18 15:21:19 -0700144 channel = kzalloc(sizeof(*channel), GFP_ATOMIC);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700145 if (!channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700146 return NULL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700147
Greg Kroah-Hartman54411c42009-07-15 14:48:32 -0700148 spin_lock_init(&channel->inbound_lock);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700149 spin_lock_init(&channel->sc_lock);
150
151 INIT_LIST_HEAD(&channel->sc_list);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700152
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800153 channel->controlwq = create_workqueue("hv_vmbus_ctl");
154 if (!channel->controlwq) {
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -0700155 kfree(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700156 return NULL;
157 }
158
159 return channel;
160}
161
Hank Janssen3e189512010-03-04 22:11:00 +0000162/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700163 * release_hannel - Release the vmbus channel object itself
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700164 */
Timo Teräs4b2f9ab2010-12-15 20:48:09 +0200165static void release_channel(struct work_struct *work)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700166{
Timo Teräs4b2f9ab2010-12-15 20:48:09 +0200167 struct vmbus_channel *channel = container_of(work,
168 struct vmbus_channel,
169 work);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700170
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800171 destroy_workqueue(channel->controlwq);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700172
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -0700173 kfree(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700174}
175
Hank Janssen3e189512010-03-04 22:11:00 +0000176/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700177 * free_channel - Release the resources used by the vmbus channel object
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700178 */
Greg Kroah-Hartman9f3e28e2011-10-11 09:40:01 -0600179static void free_channel(struct vmbus_channel *channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700180{
Hank Janssen3e7ee492009-07-13 16:02:34 -0700181
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700182 /*
183 * We have to release the channel's workqueue/thread in the vmbus's
184 * workqueue/thread context
185 * ie we can't destroy ourselves.
186 */
Timo Teräs4b2f9ab2010-12-15 20:48:09 +0200187 INIT_WORK(&channel->work, release_channel);
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800188 queue_work(vmbus_connection.work_queue, &channel->work);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700189}
190
Haiyang Zhang8b5d6d32010-05-28 23:22:44 +0000191
Haiyang Zhang8b5d6d32010-05-28 23:22:44 +0000192
Timo Teräs4b2f9ab2010-12-15 20:48:09 +0200193/*
194 * vmbus_process_rescind_offer -
195 * Rescind the offer by initiating a device removal
196 */
197static void vmbus_process_rescind_offer(struct work_struct *work)
198{
199 struct vmbus_channel *channel = container_of(work,
200 struct vmbus_channel,
201 work);
K. Y. Srinivasanc8705972013-03-15 12:25:44 -0700202 unsigned long flags;
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700203 struct vmbus_channel *primary_channel;
K. Y. Srinivasanc8705972013-03-15 12:25:44 -0700204 struct vmbus_channel_relid_released msg;
Timo Teräs4b2f9ab2010-12-15 20:48:09 +0200205
K. Y. Srinivasan565ce642013-10-16 19:27:19 -0700206 if (channel->device_obj)
207 vmbus_device_unregister(channel->device_obj);
K. Y. Srinivasanc8705972013-03-15 12:25:44 -0700208 memset(&msg, 0, sizeof(struct vmbus_channel_relid_released));
209 msg.child_relid = channel->offermsg.child_relid;
210 msg.header.msgtype = CHANNELMSG_RELID_RELEASED;
211 vmbus_post_msg(&msg, sizeof(struct vmbus_channel_relid_released));
212
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700213 if (channel->primary_channel == NULL) {
214 spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
215 list_del(&channel->listentry);
216 spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
217 } else {
218 primary_channel = channel->primary_channel;
219 spin_lock_irqsave(&primary_channel->sc_lock, flags);
K. Y. Srinivasan565ce642013-10-16 19:27:19 -0700220 list_del(&channel->sc_list);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700221 spin_unlock_irqrestore(&primary_channel->sc_lock, flags);
222 }
K. Y. Srinivasanc8705972013-03-15 12:25:44 -0700223 free_channel(channel);
Timo Teräs4b2f9ab2010-12-15 20:48:09 +0200224}
Haiyang Zhang8b5d6d32010-05-28 23:22:44 +0000225
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -0800226void vmbus_free_channels(void)
227{
228 struct vmbus_channel *channel;
229
230 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
231 vmbus_device_unregister(channel->device_obj);
232 kfree(channel->device_obj);
233 free_channel(channel);
234 }
235}
236
Hank Janssen3e189512010-03-04 22:11:00 +0000237/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700238 * vmbus_process_offer - Process the offer by creating a channel/device
Hank Janssenc88c4e42010-05-04 15:55:05 -0700239 * associated with this offer
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700240 */
Timo Teräs4b2f9ab2010-12-15 20:48:09 +0200241static void vmbus_process_offer(struct work_struct *work)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700242{
Timo Teräs4b2f9ab2010-12-15 20:48:09 +0200243 struct vmbus_channel *newchannel = container_of(work,
244 struct vmbus_channel,
245 work);
Greg Kroah-Hartmanaded7162009-08-18 15:21:19 -0700246 struct vmbus_channel *channel;
Haiyang Zhang188963e2010-10-15 10:14:06 -0700247 bool fnew = true;
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700248 int ret;
Greg Kroah-Hartman0f5e44c2009-07-15 14:57:16 -0700249 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700250
Timo Teräs4b2f9ab2010-12-15 20:48:09 +0200251 /* The next possible work is rescind handling */
252 INIT_WORK(&newchannel->work, vmbus_process_rescind_offer);
253
Bill Pemberton454f18a2009-07-27 16:47:24 -0400254 /* Make sure this is a new offer */
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800255 spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700256
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800257 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
K. Y. Srinivasan358d2ee2011-08-25 09:48:28 -0700258 if (!uuid_le_cmp(channel->offermsg.offer.if_type,
259 newchannel->offermsg.offer.if_type) &&
260 !uuid_le_cmp(channel->offermsg.offer.if_instance,
261 newchannel->offermsg.offer.if_instance)) {
Haiyang Zhang188963e2010-10-15 10:14:06 -0700262 fnew = false;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700263 break;
264 }
265 }
266
Haiyang Zhang188963e2010-10-15 10:14:06 -0700267 if (fnew)
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800268 list_add_tail(&newchannel->listentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800269 &vmbus_connection.chn_list);
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700270
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800271 spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700272
Haiyang Zhang188963e2010-10-15 10:14:06 -0700273 if (!fnew) {
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700274 /*
275 * Check to see if this is a sub-channel.
276 */
277 if (newchannel->offermsg.offer.sub_channel_index != 0) {
278 /*
279 * Process the sub-channel.
280 */
281 newchannel->primary_channel = channel;
282 spin_lock_irqsave(&channel->sc_lock, flags);
283 list_add_tail(&newchannel->sc_list, &channel->sc_list);
284 spin_unlock_irqrestore(&channel->sc_lock, flags);
285 newchannel->state = CHANNEL_OPEN_STATE;
286 if (channel->sc_creation_callback != NULL)
287 channel->sc_creation_callback(newchannel);
288
289 return;
290 }
291
Haiyang Zhange98cb272010-10-15 10:14:07 -0700292 free_channel(newchannel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700293 return;
294 }
295
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700296 /*
K. Y. Srinivasan42dceeb2013-08-26 14:08:58 -0700297 * This state is used to indicate a successful open
298 * so that when we do close the channel normally, we
299 * can cleanup properly
300 */
301 newchannel->state = CHANNEL_OPEN_STATE;
302
303 /*
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700304 * Start the process of binding this offer to the driver
305 * We need to set the DeviceObject field before calling
Haiyang Zhang646f1ea2011-01-26 12:12:10 -0800306 * vmbus_child_dev_add()
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700307 */
K. Y. Srinivasanf2c73012011-09-08 07:24:12 -0700308 newchannel->device_obj = vmbus_device_create(
Haiyang Zhang767dff62011-01-26 12:12:12 -0800309 &newchannel->offermsg.offer.if_type,
310 &newchannel->offermsg.offer.if_instance,
Haiyang Zhang188963e2010-10-15 10:14:06 -0700311 newchannel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700312
Bill Pemberton454f18a2009-07-27 16:47:24 -0400313 /*
314 * Add the new device to the bus. This will kick off device-driver
315 * binding which eventually invokes the device driver's AddDevice()
316 * method.
317 */
K. Y. Srinivasan227942812011-09-08 07:24:13 -0700318 ret = vmbus_device_register(newchannel->device_obj);
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700319 if (ret != 0) {
Hank Janssen0a466182011-03-29 13:58:47 -0700320 pr_err("unable to add child device object (relid %d)\n",
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800321 newchannel->offermsg.child_relid);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700322
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800323 spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800324 list_del(&newchannel->listentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800325 spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
K. Y. Srinivasan8b8ee672011-12-12 09:29:16 -0800326 kfree(newchannel->device_obj);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700327
Haiyang Zhange98cb272010-10-15 10:14:07 -0700328 free_channel(newchannel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700329 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700330}
331
K. Y. Srinivasana1198452012-12-01 06:46:50 -0800332enum {
333 IDE = 0,
334 SCSI,
335 NIC,
336 MAX_PERF_CHN,
337};
338
339/*
K. Y. Srinivasan7fb96562013-01-23 17:42:40 -0800340 * This is an array of device_ids (device types) that are performance critical.
K. Y. Srinivasana1198452012-12-01 06:46:50 -0800341 * We attempt to distribute the interrupt load for these devices across
342 * all available CPUs.
343 */
K. Y. Srinivasan7fb96562013-01-23 17:42:40 -0800344static const struct hv_vmbus_device_id hp_devs[] = {
K. Y. Srinivasana1198452012-12-01 06:46:50 -0800345 /* IDE */
K. Y. Srinivasan7fb96562013-01-23 17:42:40 -0800346 { HV_IDE_GUID, },
K. Y. Srinivasana1198452012-12-01 06:46:50 -0800347 /* Storage - SCSI */
K. Y. Srinivasan7fb96562013-01-23 17:42:40 -0800348 { HV_SCSI_GUID, },
K. Y. Srinivasana1198452012-12-01 06:46:50 -0800349 /* Network */
K. Y. Srinivasan7fb96562013-01-23 17:42:40 -0800350 { HV_NIC_GUID, },
K. Y. Srinivasana1198452012-12-01 06:46:50 -0800351};
352
353
354/*
355 * We use this state to statically distribute the channel interrupt load.
356 */
357static u32 next_vp;
358
359/*
360 * Starting with Win8, we can statically distribute the incoming
361 * channel interrupt load by binding a channel to VCPU. We
362 * implement here a simple round robin scheme for distributing
363 * the interrupt load.
364 * We will bind channels that are not performance critical to cpu 0 and
365 * performance critical channels (IDE, SCSI and Network) will be uniformly
366 * distributed across all available CPUs.
367 */
368static u32 get_vp_index(uuid_le *type_guid)
369{
370 u32 cur_cpu;
371 int i;
372 bool perf_chn = false;
373 u32 max_cpus = num_online_cpus();
374
375 for (i = IDE; i < MAX_PERF_CHN; i++) {
K. Y. Srinivasan7fb96562013-01-23 17:42:40 -0800376 if (!memcmp(type_guid->b, hp_devs[i].guid,
K. Y. Srinivasana1198452012-12-01 06:46:50 -0800377 sizeof(uuid_le))) {
378 perf_chn = true;
379 break;
380 }
381 }
382 if ((vmbus_proto_version == VERSION_WS2008) ||
383 (vmbus_proto_version == VERSION_WIN7) || (!perf_chn)) {
384 /*
385 * Prior to win8, all channel interrupts are
386 * delivered on cpu 0.
387 * Also if the channel is not a performance critical
388 * channel, bind it to cpu 0.
389 */
390 return 0;
391 }
392 cur_cpu = (++next_vp % max_cpus);
K. Y. Srinivasand2242a32013-05-17 09:30:35 -0700393 return hv_context.vp_index[cur_cpu];
K. Y. Srinivasana1198452012-12-01 06:46:50 -0800394}
395
Hank Janssen3e189512010-03-04 22:11:00 +0000396/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700397 * vmbus_onoffer - Handler for channel offers from vmbus in parent partition.
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700398 *
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700399 */
Haiyang Zhange98cb272010-10-15 10:14:07 -0700400static void vmbus_onoffer(struct vmbus_channel_message_header *hdr)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700401{
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700402 struct vmbus_channel_offer_channel *offer;
Haiyang Zhang188963e2010-10-15 10:14:06 -0700403 struct vmbus_channel *newchannel;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700404
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700405 offer = (struct vmbus_channel_offer_channel *)hdr;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700406
Bill Pemberton454f18a2009-07-27 16:47:24 -0400407 /* Allocate the channel object and save this offer. */
Haiyang Zhange98cb272010-10-15 10:14:07 -0700408 newchannel = alloc_channel();
Haiyang Zhang188963e2010-10-15 10:14:06 -0700409 if (!newchannel) {
Hank Janssen0a466182011-03-29 13:58:47 -0700410 pr_err("Unable to allocate channel object\n");
Hank Janssen3e7ee492009-07-13 16:02:34 -0700411 return;
412 }
413
K. Y. Srinivasan132368b2012-12-01 06:46:33 -0800414 /*
415 * By default we setup state to enable batched
416 * reading. A specific service can choose to
417 * disable this prior to opening the channel.
418 */
419 newchannel->batched_reading = true;
420
K. Y. Srinivasanb3bf60c2012-12-01 06:46:45 -0800421 /*
422 * Setup state for signalling the host.
423 */
424 newchannel->sig_event = (struct hv_input_signal_event *)
425 (ALIGN((unsigned long)
426 &newchannel->sig_buf,
427 HV_HYPERCALL_PARAM_ALIGN));
428
429 newchannel->sig_event->connectionid.asu32 = 0;
430 newchannel->sig_event->connectionid.u.id = VMBUS_EVENT_CONNECTION_ID;
431 newchannel->sig_event->flag_number = 0;
432 newchannel->sig_event->rsvdz = 0;
433
434 if (vmbus_proto_version != VERSION_WS2008) {
435 newchannel->is_dedicated_interrupt =
436 (offer->is_dedicated_interrupt != 0);
437 newchannel->sig_event->connectionid.u.id =
438 offer->connection_id;
439 }
440
K. Y. Srinivasana1198452012-12-01 06:46:50 -0800441 newchannel->target_vp = get_vp_index(&offer->offer.if_type);
K. Y. Srinivasanabbf3b22012-12-01 06:46:48 -0800442
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800443 memcpy(&newchannel->offermsg, offer,
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700444 sizeof(struct vmbus_channel_offer_channel));
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800445 newchannel->monitor_grp = (u8)offer->monitorid / 32;
446 newchannel->monitor_bit = (u8)offer->monitorid % 32;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700447
Timo Teräs4b2f9ab2010-12-15 20:48:09 +0200448 INIT_WORK(&newchannel->work, vmbus_process_offer);
449 queue_work(newchannel->controlwq, &newchannel->work);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700450}
451
Hank Janssen3e189512010-03-04 22:11:00 +0000452/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700453 * vmbus_onoffer_rescind - Rescind offer handler.
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700454 *
455 * We queue a work item to process this offer synchronously
456 */
Haiyang Zhange98cb272010-10-15 10:14:07 -0700457static void vmbus_onoffer_rescind(struct vmbus_channel_message_header *hdr)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700458{
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700459 struct vmbus_channel_rescind_offer *rescind;
Greg Kroah-Hartmanaded7162009-08-18 15:21:19 -0700460 struct vmbus_channel *channel;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700461
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700462 rescind = (struct vmbus_channel_rescind_offer *)hdr;
Haiyang Zhangc6977672011-01-26 12:12:08 -0800463 channel = relid2channel(rescind->child_relid);
Hank Janssen98e08702011-03-29 13:58:44 -0700464
465 if (channel == NULL)
466 /* Just return here, no channel found */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700467 return;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700468
Timo Teräs4b2f9ab2010-12-15 20:48:09 +0200469 /* work is initialized for vmbus_process_rescind_offer() from
470 * vmbus_process_offer() where the channel got created */
471 queue_work(channel->controlwq, &channel->work);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700472}
473
Hank Janssen3e189512010-03-04 22:11:00 +0000474/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700475 * vmbus_onoffers_delivered -
476 * This is invoked when all offers have been delivered.
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700477 *
478 * Nothing to do here.
479 */
Haiyang Zhange98cb272010-10-15 10:14:07 -0700480static void vmbus_onoffers_delivered(
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700481 struct vmbus_channel_message_header *hdr)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700482{
Hank Janssen3e7ee492009-07-13 16:02:34 -0700483}
484
Hank Janssen3e189512010-03-04 22:11:00 +0000485/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700486 * vmbus_onopen_result - Open result handler.
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700487 *
488 * This is invoked when we received a response to our channel open request.
489 * Find the matching request, copy the response and signal the requesting
490 * thread.
491 */
Haiyang Zhange98cb272010-10-15 10:14:07 -0700492static void vmbus_onopen_result(struct vmbus_channel_message_header *hdr)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700493{
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700494 struct vmbus_channel_open_result *result;
Haiyang Zhang188963e2010-10-15 10:14:06 -0700495 struct vmbus_channel_msginfo *msginfo;
496 struct vmbus_channel_message_header *requestheader;
497 struct vmbus_channel_open_channel *openmsg;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700498 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700499
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700500 result = (struct vmbus_channel_open_result *)hdr;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700501
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700502 /*
503 * Find the open msg, copy the result and signal/unblock the wait event
504 */
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800505 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700506
Hank Janssenebb61e52011-02-18 12:39:57 -0800507 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
508 msglistentry) {
Haiyang Zhang188963e2010-10-15 10:14:06 -0700509 requestheader =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800510 (struct vmbus_channel_message_header *)msginfo->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700511
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800512 if (requestheader->msgtype == CHANNELMSG_OPENCHANNEL) {
Haiyang Zhang188963e2010-10-15 10:14:06 -0700513 openmsg =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800514 (struct vmbus_channel_open_channel *)msginfo->msg;
515 if (openmsg->child_relid == result->child_relid &&
516 openmsg->openid == result->openid) {
517 memcpy(&msginfo->response.open_result,
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700518 result,
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700519 sizeof(
520 struct vmbus_channel_open_result));
521 complete(&msginfo->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700522 break;
523 }
524 }
525 }
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800526 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700527}
528
Hank Janssen3e189512010-03-04 22:11:00 +0000529/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700530 * vmbus_ongpadl_created - GPADL created handler.
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700531 *
532 * This is invoked when we received a response to our gpadl create request.
533 * Find the matching request, copy the response and signal the requesting
534 * thread.
535 */
Haiyang Zhange98cb272010-10-15 10:14:07 -0700536static void vmbus_ongpadl_created(struct vmbus_channel_message_header *hdr)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700537{
Haiyang Zhang188963e2010-10-15 10:14:06 -0700538 struct vmbus_channel_gpadl_created *gpadlcreated;
Haiyang Zhang188963e2010-10-15 10:14:06 -0700539 struct vmbus_channel_msginfo *msginfo;
540 struct vmbus_channel_message_header *requestheader;
541 struct vmbus_channel_gpadl_header *gpadlheader;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700542 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700543
Haiyang Zhang188963e2010-10-15 10:14:06 -0700544 gpadlcreated = (struct vmbus_channel_gpadl_created *)hdr;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700545
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700546 /*
547 * Find the establish msg, copy the result and signal/unblock the wait
548 * event
549 */
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800550 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700551
Hank Janssenebb61e52011-02-18 12:39:57 -0800552 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
553 msglistentry) {
Haiyang Zhang188963e2010-10-15 10:14:06 -0700554 requestheader =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800555 (struct vmbus_channel_message_header *)msginfo->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700556
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800557 if (requestheader->msgtype == CHANNELMSG_GPADL_HEADER) {
Haiyang Zhang188963e2010-10-15 10:14:06 -0700558 gpadlheader =
559 (struct vmbus_channel_gpadl_header *)requestheader;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700560
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800561 if ((gpadlcreated->child_relid ==
562 gpadlheader->child_relid) &&
563 (gpadlcreated->gpadl == gpadlheader->gpadl)) {
564 memcpy(&msginfo->response.gpadl_created,
Haiyang Zhang188963e2010-10-15 10:14:06 -0700565 gpadlcreated,
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700566 sizeof(
567 struct vmbus_channel_gpadl_created));
568 complete(&msginfo->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700569 break;
570 }
571 }
572 }
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800573 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700574}
575
Hank Janssen3e189512010-03-04 22:11:00 +0000576/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700577 * vmbus_ongpadl_torndown - GPADL torndown handler.
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700578 *
579 * This is invoked when we received a response to our gpadl teardown request.
580 * Find the matching request, copy the response and signal the requesting
581 * thread.
582 */
Haiyang Zhange98cb272010-10-15 10:14:07 -0700583static void vmbus_ongpadl_torndown(
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700584 struct vmbus_channel_message_header *hdr)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700585{
Haiyang Zhang188963e2010-10-15 10:14:06 -0700586 struct vmbus_channel_gpadl_torndown *gpadl_torndown;
Haiyang Zhang188963e2010-10-15 10:14:06 -0700587 struct vmbus_channel_msginfo *msginfo;
588 struct vmbus_channel_message_header *requestheader;
589 struct vmbus_channel_gpadl_teardown *gpadl_teardown;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700590 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700591
Haiyang Zhang188963e2010-10-15 10:14:06 -0700592 gpadl_torndown = (struct vmbus_channel_gpadl_torndown *)hdr;
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700593
594 /*
595 * Find the open msg, copy the result and signal/unblock the wait event
596 */
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800597 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700598
Hank Janssenebb61e52011-02-18 12:39:57 -0800599 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
600 msglistentry) {
Haiyang Zhang188963e2010-10-15 10:14:06 -0700601 requestheader =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800602 (struct vmbus_channel_message_header *)msginfo->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700603
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800604 if (requestheader->msgtype == CHANNELMSG_GPADL_TEARDOWN) {
Haiyang Zhang188963e2010-10-15 10:14:06 -0700605 gpadl_teardown =
606 (struct vmbus_channel_gpadl_teardown *)requestheader;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700607
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800608 if (gpadl_torndown->gpadl == gpadl_teardown->gpadl) {
609 memcpy(&msginfo->response.gpadl_torndown,
Haiyang Zhang188963e2010-10-15 10:14:06 -0700610 gpadl_torndown,
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700611 sizeof(
612 struct vmbus_channel_gpadl_torndown));
613 complete(&msginfo->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700614 break;
615 }
616 }
617 }
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800618 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700619}
620
Hank Janssen3e189512010-03-04 22:11:00 +0000621/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700622 * vmbus_onversion_response - Version response handler
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700623 *
624 * This is invoked when we received a response to our initiate contact request.
625 * Find the matching request, copy the response and signal the requesting
626 * thread.
627 */
Haiyang Zhange98cb272010-10-15 10:14:07 -0700628static void vmbus_onversion_response(
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700629 struct vmbus_channel_message_header *hdr)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700630{
Haiyang Zhang188963e2010-10-15 10:14:06 -0700631 struct vmbus_channel_msginfo *msginfo;
632 struct vmbus_channel_message_header *requestheader;
Haiyang Zhang188963e2010-10-15 10:14:06 -0700633 struct vmbus_channel_version_response *version_response;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700634 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700635
Haiyang Zhang188963e2010-10-15 10:14:06 -0700636 version_response = (struct vmbus_channel_version_response *)hdr;
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800637 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700638
Hank Janssenebb61e52011-02-18 12:39:57 -0800639 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
640 msglistentry) {
Haiyang Zhang188963e2010-10-15 10:14:06 -0700641 requestheader =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800642 (struct vmbus_channel_message_header *)msginfo->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700643
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800644 if (requestheader->msgtype ==
645 CHANNELMSG_INITIATE_CONTACT) {
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800646 memcpy(&msginfo->response.version_response,
Haiyang Zhang188963e2010-10-15 10:14:06 -0700647 version_response,
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700648 sizeof(struct vmbus_channel_version_response));
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700649 complete(&msginfo->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700650 }
651 }
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800652 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700653}
654
Greg Kroah-Hartmanc8212f02009-08-31 21:51:50 -0700655/* Channel message dispatch table */
656static struct vmbus_channel_message_table_entry
K. Y. Srinivasanb7c6b022011-05-10 07:55:38 -0700657 channel_message_table[CHANNELMSG_COUNT] = {
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800658 {CHANNELMSG_INVALID, NULL},
659 {CHANNELMSG_OFFERCHANNEL, vmbus_onoffer},
660 {CHANNELMSG_RESCIND_CHANNELOFFER, vmbus_onoffer_rescind},
661 {CHANNELMSG_REQUESTOFFERS, NULL},
662 {CHANNELMSG_ALLOFFERS_DELIVERED, vmbus_onoffers_delivered},
663 {CHANNELMSG_OPENCHANNEL, NULL},
664 {CHANNELMSG_OPENCHANNEL_RESULT, vmbus_onopen_result},
665 {CHANNELMSG_CLOSECHANNEL, NULL},
666 {CHANNELMSG_GPADL_HEADER, NULL},
667 {CHANNELMSG_GPADL_BODY, NULL},
668 {CHANNELMSG_GPADL_CREATED, vmbus_ongpadl_created},
669 {CHANNELMSG_GPADL_TEARDOWN, NULL},
670 {CHANNELMSG_GPADL_TORNDOWN, vmbus_ongpadl_torndown},
671 {CHANNELMSG_RELID_RELEASED, NULL},
672 {CHANNELMSG_INITIATE_CONTACT, NULL},
673 {CHANNELMSG_VERSION_RESPONSE, vmbus_onversion_response},
674 {CHANNELMSG_UNLOAD, NULL},
Greg Kroah-Hartmanc8212f02009-08-31 21:51:50 -0700675};
676
Hank Janssen3e189512010-03-04 22:11:00 +0000677/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700678 * vmbus_onmessage - Handler for channel protocol messages.
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700679 *
680 * This is invoked in the vmbus worker thread context.
681 */
Haiyang Zhange98cb272010-10-15 10:14:07 -0700682void vmbus_onmessage(void *context)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700683{
Haiyang Zhang188963e2010-10-15 10:14:06 -0700684 struct hv_message *msg = context;
Greg Kroah-Hartman82250212009-08-26 15:16:04 -0700685 struct vmbus_channel_message_header *hdr;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700686 int size;
687
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800688 hdr = (struct vmbus_channel_message_header *)msg->u.payload;
689 size = msg->header.payload_size;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700690
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800691 if (hdr->msgtype >= CHANNELMSG_COUNT) {
Hank Janssen0a466182011-03-29 13:58:47 -0700692 pr_err("Received invalid channel message type %d size %d\n",
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800693 hdr->msgtype, size);
Greg Kroah-Hartman04f50c42009-07-16 12:35:37 -0700694 print_hex_dump_bytes("", DUMP_PREFIX_NONE,
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800695 (unsigned char *)msg->u.payload, size);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700696 return;
697 }
698
K. Y. Srinivasanb7c6b022011-05-10 07:55:38 -0700699 if (channel_message_table[hdr->msgtype].message_handler)
700 channel_message_table[hdr->msgtype].message_handler(hdr);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700701 else
Hank Janssen0a466182011-03-29 13:58:47 -0700702 pr_err("Unhandled channel message type %d\n", hdr->msgtype);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700703}
704
Hank Janssen3e189512010-03-04 22:11:00 +0000705/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700706 * vmbus_request_offers - Send a request to get all our pending offers.
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700707 */
Haiyang Zhange98cb272010-10-15 10:14:07 -0700708int vmbus_request_offers(void)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700709{
Greg Kroah-Hartman82250212009-08-26 15:16:04 -0700710 struct vmbus_channel_message_header *msg;
Haiyang Zhang188963e2010-10-15 10:14:06 -0700711 struct vmbus_channel_msginfo *msginfo;
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700712 int ret, t;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700713
Haiyang Zhang188963e2010-10-15 10:14:06 -0700714 msginfo = kmalloc(sizeof(*msginfo) +
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700715 sizeof(struct vmbus_channel_message_header),
716 GFP_KERNEL);
Haiyang Zhang188963e2010-10-15 10:14:06 -0700717 if (!msginfo)
Bill Pemberton75910f22010-05-05 15:27:31 -0400718 return -ENOMEM;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700719
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700720 init_completion(&msginfo->waitevent);
Bill Pemberton75910f22010-05-05 15:27:31 -0400721
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800722 msg = (struct vmbus_channel_message_header *)msginfo->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700723
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800724 msg->msgtype = CHANNELMSG_REQUESTOFFERS;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700725
Hank Janssen3e7ee492009-07-13 16:02:34 -0700726
Haiyang Zhangc6977672011-01-26 12:12:08 -0800727 ret = vmbus_post_msg(msg,
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700728 sizeof(struct vmbus_channel_message_header));
729 if (ret != 0) {
Hank Janssen0a466182011-03-29 13:58:47 -0700730 pr_err("Unable to request offers - %d\n", ret);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700731
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800732 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700733 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700734
K. Y. Srinivasan2dfde962011-06-16 13:16:34 -0700735 t = wait_for_completion_timeout(&msginfo->waitevent, 5*HZ);
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700736 if (t == 0) {
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800737 ret = -ETIMEDOUT;
738 goto cleanup;
739 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700740
741
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800742
743cleanup:
Ilia Mirkindd9b15d2011-03-13 00:29:00 -0500744 kfree(msginfo);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700745
Hank Janssen3e7ee492009-07-13 16:02:34 -0700746 return ret;
747}
748
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700749/*
750 * Retrieve the (sub) channel on which to send an outgoing request.
751 * When a primary channel has multiple sub-channels, we choose a
752 * channel whose VCPU binding is closest to the VCPU on which
753 * this call is being made.
754 */
755struct vmbus_channel *vmbus_get_outgoing_channel(struct vmbus_channel *primary)
756{
757 struct list_head *cur, *tmp;
758 int cur_cpu = hv_context.vp_index[smp_processor_id()];
759 struct vmbus_channel *cur_channel;
760 struct vmbus_channel *outgoing_channel = primary;
761 int cpu_distance, new_cpu_distance;
762
763 if (list_empty(&primary->sc_list))
764 return outgoing_channel;
765
766 list_for_each_safe(cur, tmp, &primary->sc_list) {
767 cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
768 if (cur_channel->state != CHANNEL_OPENED_STATE)
769 continue;
770
771 if (cur_channel->target_vp == cur_cpu)
772 return cur_channel;
773
774 cpu_distance = ((outgoing_channel->target_vp > cur_cpu) ?
775 (outgoing_channel->target_vp - cur_cpu) :
776 (cur_cpu - outgoing_channel->target_vp));
777
778 new_cpu_distance = ((cur_channel->target_vp > cur_cpu) ?
779 (cur_channel->target_vp - cur_cpu) :
780 (cur_cpu - cur_channel->target_vp));
781
782 if (cpu_distance < new_cpu_distance)
783 continue;
784
785 outgoing_channel = cur_channel;
786 }
787
788 return outgoing_channel;
789}
790EXPORT_SYMBOL_GPL(vmbus_get_outgoing_channel);
791
792static void invoke_sc_cb(struct vmbus_channel *primary_channel)
793{
794 struct list_head *cur, *tmp;
795 struct vmbus_channel *cur_channel;
796
797 if (primary_channel->sc_creation_callback == NULL)
798 return;
799
800 list_for_each_safe(cur, tmp, &primary_channel->sc_list) {
801 cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
802
803 primary_channel->sc_creation_callback(cur_channel);
804 }
805}
806
807void vmbus_set_sc_create_callback(struct vmbus_channel *primary_channel,
808 void (*sc_cr_cb)(struct vmbus_channel *new_sc))
809{
810 primary_channel->sc_creation_callback = sc_cr_cb;
811}
812EXPORT_SYMBOL_GPL(vmbus_set_sc_create_callback);
813
814bool vmbus_are_subchannels_present(struct vmbus_channel *primary)
815{
816 bool ret;
817
818 ret = !list_empty(&primary->sc_list);
819
820 if (ret) {
821 /*
822 * Invoke the callback on sub-channel creation.
823 * This will present a uniform interface to the
824 * clients.
825 */
826 invoke_sc_cb(primary);
827 }
828
829 return ret;
830}
831EXPORT_SYMBOL_GPL(vmbus_are_subchannels_present);