Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1 | /* |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 2 | * Copyright (c) 2009, Microsoft Corporation. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms and conditions of the GNU General Public License, |
| 6 | * version 2, as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | * more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along with |
| 14 | * this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
| 15 | * Place - Suite 330, Boston, MA 02111-1307 USA. |
| 16 | * |
| 17 | * Authors: |
| 18 | * Haiyang Zhang <haiyangz@microsoft.com> |
| 19 | * Hank Janssen <hjanssen@microsoft.com> |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 20 | */ |
Hank Janssen | 0a46618 | 2011-03-29 13:58:47 -0700 | [diff] [blame] | 21 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 22 | |
Greg Kroah-Hartman | a0086dc | 2009-08-17 17:22:08 -0700 | [diff] [blame] | 23 | #include <linux/kernel.h> |
K. Y. Srinivasan | 0c3b7b2 | 2011-02-11 09:59:43 -0800 | [diff] [blame] | 24 | #include <linux/sched.h> |
| 25 | #include <linux/wait.h> |
Greg Kroah-Hartman | a0086dc | 2009-08-17 17:22:08 -0700 | [diff] [blame] | 26 | #include <linux/mm.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 27 | #include <linux/slab.h> |
Bill Pemberton | 53af545 | 2009-09-11 21:46:44 -0400 | [diff] [blame] | 28 | #include <linux/list.h> |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 29 | #include <linux/module.h> |
Haiyang Zhang | 8b5d6d3 | 2010-05-28 23:22:44 +0000 | [diff] [blame] | 30 | #include <linux/completion.h> |
Greg Kroah-Hartman | 46a9719 | 2011-10-04 12:29:52 -0700 | [diff] [blame] | 31 | #include <linux/hyperv.h> |
K. Y. Srinivasan | 3f335ea | 2011-05-12 19:34:15 -0700 | [diff] [blame] | 32 | |
K. Y. Srinivasan | 0f2a661 | 2011-05-12 19:34:28 -0700 | [diff] [blame] | 33 | #include "hyperv_vmbus.h" |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 34 | |
K. Y. Srinivasan | 7047f17 | 2015-12-25 20:00:30 -0800 | [diff] [blame] | 35 | static void init_vp_index(struct vmbus_channel *channel, u16 dev_type); |
| 36 | |
| 37 | static 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 | |
| 140 | static 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 Kuznetsov | f38e7dd | 2015-05-06 17:47:45 -0700 | [diff] [blame] | 151 | |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 152 | /** |
Greg Kroah-Hartman | da0e963 | 2011-10-11 08:42:28 -0600 | [diff] [blame] | 153 | * vmbus_prep_negotiate_resp() - Create default response for Hyper-V Negotiate message |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 154 | * @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. Srinivasan | c836d0a | 2012-05-12 13:44:58 -0700 | [diff] [blame] | 160 | * Set up and fill in default negotiate response message. |
| 161 | * |
K. Y. Srinivasan | 6741335 | 2013-07-02 10:31:30 -0700 | [diff] [blame] | 162 | * The fw_version specifies the framework version that |
| 163 | * we can support and srv_version specifies the service |
| 164 | * version we can support. |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 165 | * |
| 166 | * Mainly used by Hyper-V drivers. |
| 167 | */ |
K. Y. Srinivasan | 6741335 | 2013-07-02 10:31:30 -0700 | [diff] [blame] | 168 | bool vmbus_prep_negotiate_resp(struct icmsg_hdr *icmsghdrp, |
K. Y. Srinivasan | c836d0a | 2012-05-12 13:44:58 -0700 | [diff] [blame] | 169 | struct icmsg_negotiate *negop, u8 *buf, |
K. Y. Srinivasan | 6741335 | 2013-07-02 10:31:30 -0700 | [diff] [blame] | 170 | int fw_version, int srv_version) |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 171 | { |
K. Y. Srinivasan | 6741335 | 2013-07-02 10:31:30 -0700 | [diff] [blame] | 172 | 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. Srinivasan | c836d0a | 2012-05-12 13:44:58 -0700 | [diff] [blame] | 176 | int i; |
K. Y. Srinivasan | 6741335 | 2013-07-02 10:31:30 -0700 | [diff] [blame] | 177 | bool found_match = false; |
K. Y. Srinivasan | c836d0a | 2012-05-12 13:44:58 -0700 | [diff] [blame] | 178 | |
K. Y. Srinivasan | a360530 | 2012-05-12 13:44:57 -0700 | [diff] [blame] | 179 | icmsghdrp->icmsgsize = 0x10; |
K. Y. Srinivasan | 6741335 | 2013-07-02 10:31:30 -0700 | [diff] [blame] | 180 | 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 Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 185 | |
K. Y. Srinivasan | a360530 | 2012-05-12 13:44:57 -0700 | [diff] [blame] | 186 | negop = (struct icmsg_negotiate *)&buf[ |
| 187 | sizeof(struct vmbuspipe_hdr) + |
| 188 | sizeof(struct icmsg_hdr)]; |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 189 | |
K. Y. Srinivasan | 6741335 | 2013-07-02 10:31:30 -0700 | [diff] [blame] | 190 | icframe_major = negop->icframe_vercnt; |
| 191 | icframe_minor = 0; |
| 192 | |
| 193 | icmsg_major = negop->icmsg_vercnt; |
| 194 | icmsg_minor = 0; |
K. Y. Srinivasan | c836d0a | 2012-05-12 13:44:58 -0700 | [diff] [blame] | 195 | |
| 196 | /* |
| 197 | * Select the framework version number we will |
| 198 | * support. |
| 199 | */ |
| 200 | |
| 201 | for (i = 0; i < negop->icframe_vercnt; i++) { |
K. Y. Srinivasan | 6741335 | 2013-07-02 10:31:30 -0700 | [diff] [blame] | 202 | 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 Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 208 | } |
K. Y. Srinivasan | a360530 | 2012-05-12 13:44:57 -0700 | [diff] [blame] | 209 | |
K. Y. Srinivasan | 6741335 | 2013-07-02 10:31:30 -0700 | [diff] [blame] | 210 | if (!found_match) |
| 211 | goto fw_error; |
| 212 | |
| 213 | found_match = false; |
| 214 | |
K. Y. Srinivasan | c836d0a | 2012-05-12 13:44:58 -0700 | [diff] [blame] | 215 | for (i = negop->icframe_vercnt; |
| 216 | (i < negop->icframe_vercnt + negop->icmsg_vercnt); i++) { |
K. Y. Srinivasan | 6741335 | 2013-07-02 10:31:30 -0700 | [diff] [blame] | 217 | 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. Srinivasan | c836d0a | 2012-05-12 13:44:58 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | /* |
K. Y. Srinivasan | 6741335 | 2013-07-02 10:31:30 -0700 | [diff] [blame] | 226 | * Respond with the framework and service |
K. Y. Srinivasan | c836d0a | 2012-05-12 13:44:58 -0700 | [diff] [blame] | 227 | * version numbers we can support. |
| 228 | */ |
K. Y. Srinivasan | 6741335 | 2013-07-02 10:31:30 -0700 | [diff] [blame] | 229 | |
| 230 | fw_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 Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 244 | } |
K. Y. Srinivasan | a360530 | 2012-05-12 13:44:57 -0700 | [diff] [blame] | 245 | |
Greg Kroah-Hartman | da0e963 | 2011-10-11 08:42:28 -0600 | [diff] [blame] | 246 | EXPORT_SYMBOL_GPL(vmbus_prep_negotiate_resp); |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 247 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 248 | /* |
Haiyang Zhang | e98cb27 | 2010-10-15 10:14:07 -0700 | [diff] [blame] | 249 | * alloc_channel - Allocate and initialize a vmbus channel object |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 250 | */ |
Greg Kroah-Hartman | 50fe56d | 2010-10-20 15:51:57 -0700 | [diff] [blame] | 251 | static struct vmbus_channel *alloc_channel(void) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 252 | { |
Vitaly Kuznetsov | bc63b6f | 2015-02-27 11:25:52 -0800 | [diff] [blame] | 253 | static atomic_t chan_num = ATOMIC_INIT(0); |
Greg Kroah-Hartman | aded716 | 2009-08-18 15:21:19 -0700 | [diff] [blame] | 254 | struct vmbus_channel *channel; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 255 | |
Greg Kroah-Hartman | aded716 | 2009-08-18 15:21:19 -0700 | [diff] [blame] | 256 | channel = kzalloc(sizeof(*channel), GFP_ATOMIC); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 257 | if (!channel) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 258 | return NULL; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 259 | |
Vitaly Kuznetsov | bc63b6f | 2015-02-27 11:25:52 -0800 | [diff] [blame] | 260 | channel->id = atomic_inc_return(&chan_num); |
Greg Kroah-Hartman | 54411c4 | 2009-07-15 14:48:32 -0700 | [diff] [blame] | 261 | spin_lock_init(&channel->inbound_lock); |
Vitaly Kuznetsov | 67fae05 | 2015-01-20 16:45:05 +0100 | [diff] [blame] | 262 | spin_lock_init(&channel->lock); |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 263 | |
| 264 | INIT_LIST_HEAD(&channel->sc_list); |
K. Y. Srinivasan | 3a28fa3 | 2014-04-08 18:45:54 -0700 | [diff] [blame] | 265 | INIT_LIST_HEAD(&channel->percpu_list); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 266 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 267 | return channel; |
| 268 | } |
| 269 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 270 | /* |
Haiyang Zhang | e98cb27 | 2010-10-15 10:14:07 -0700 | [diff] [blame] | 271 | * free_channel - Release the resources used by the vmbus channel object |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 272 | */ |
Greg Kroah-Hartman | 9f3e28e | 2011-10-11 09:40:01 -0600 | [diff] [blame] | 273 | static void free_channel(struct vmbus_channel *channel) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 274 | { |
Dexuan Cui | aadc378 | 2015-03-27 09:10:10 -0700 | [diff] [blame] | 275 | kfree(channel); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 276 | } |
| 277 | |
K. Y. Srinivasan | 3a28fa3 | 2014-04-08 18:45:54 -0700 | [diff] [blame] | 278 | static void percpu_channel_enq(void *arg) |
| 279 | { |
| 280 | struct vmbus_channel *channel = arg; |
| 281 | int cpu = smp_processor_id(); |
Haiyang Zhang | 8b5d6d3 | 2010-05-28 23:22:44 +0000 | [diff] [blame] | 282 | |
K. Y. Srinivasan | 3a28fa3 | 2014-04-08 18:45:54 -0700 | [diff] [blame] | 283 | list_add_tail(&channel->percpu_list, &hv_context.percpu_list[cpu]); |
| 284 | } |
| 285 | |
| 286 | static void percpu_channel_deq(void *arg) |
| 287 | { |
| 288 | struct vmbus_channel *channel = arg; |
| 289 | |
| 290 | list_del(&channel->percpu_list); |
| 291 | } |
Haiyang Zhang | 8b5d6d3 | 2010-05-28 23:22:44 +0000 | [diff] [blame] | 292 | |
K. Y. Srinivasan | ed6cfcc | 2015-02-28 11:18:17 -0800 | [diff] [blame] | 293 | |
Dexuan Cui | f52078c | 2015-12-14 16:01:50 -0800 | [diff] [blame] | 294 | static void vmbus_release_relid(u32 relid) |
Timo Teräs | 4b2f9ab | 2010-12-15 20:48:09 +0200 | [diff] [blame] | 295 | { |
K. Y. Srinivasan | ed6cfcc | 2015-02-28 11:18:17 -0800 | [diff] [blame] | 296 | struct vmbus_channel_relid_released msg; |
Vitaly Kuznetsov | 04a258c | 2014-11-04 13:40:11 +0100 | [diff] [blame] | 297 | |
K. Y. Srinivasan | c870597 | 2013-03-15 12:25:44 -0700 | [diff] [blame] | 298 | memset(&msg, 0, sizeof(struct vmbus_channel_relid_released)); |
K. Y. Srinivasan | ed6cfcc | 2015-02-28 11:18:17 -0800 | [diff] [blame] | 299 | msg.child_relid = relid; |
K. Y. Srinivasan | c870597 | 2013-03-15 12:25:44 -0700 | [diff] [blame] | 300 | msg.header.msgtype = CHANNELMSG_RELID_RELEASED; |
| 301 | vmbus_post_msg(&msg, sizeof(struct vmbus_channel_relid_released)); |
Dexuan Cui | f52078c | 2015-12-14 16:01:50 -0800 | [diff] [blame] | 302 | } |
K. Y. Srinivasan | c870597 | 2013-03-15 12:25:44 -0700 | [diff] [blame] | 303 | |
Dexuan Cui | f52078c | 2015-12-14 16:01:50 -0800 | [diff] [blame] | 304 | void 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. Srinivasan | ed6cfcc | 2015-02-28 11:18:17 -0800 | [diff] [blame] | 310 | |
Dexuan Cui | 34c6801 | 2015-12-14 16:01:49 -0800 | [diff] [blame] | 311 | BUG_ON(!channel->rescind); |
| 312 | |
K. Y. Srinivasan | 2115b56 | 2014-08-28 18:29:53 -0700 | [diff] [blame] | 313 | if (channel->target_cpu != get_cpu()) { |
| 314 | put_cpu(); |
K. Y. Srinivasan | 3a28fa3 | 2014-04-08 18:45:54 -0700 | [diff] [blame] | 315 | smp_call_function_single(channel->target_cpu, |
| 316 | percpu_channel_deq, channel, true); |
K. Y. Srinivasan | 2115b56 | 2014-08-28 18:29:53 -0700 | [diff] [blame] | 317 | } else { |
K. Y. Srinivasan | 3a28fa3 | 2014-04-08 18:45:54 -0700 | [diff] [blame] | 318 | percpu_channel_deq(channel); |
K. Y. Srinivasan | 2115b56 | 2014-08-28 18:29:53 -0700 | [diff] [blame] | 319 | put_cpu(); |
| 320 | } |
K. Y. Srinivasan | 3a28fa3 | 2014-04-08 18:45:54 -0700 | [diff] [blame] | 321 | |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 322 | if (channel->primary_channel == NULL) { |
Dexuan Cui | d6f591e | 2015-12-14 16:01:51 -0800 | [diff] [blame] | 323 | mutex_lock(&vmbus_connection.channel_mutex); |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 324 | list_del(&channel->listentry); |
Dexuan Cui | d6f591e | 2015-12-14 16:01:51 -0800 | [diff] [blame] | 325 | mutex_unlock(&vmbus_connection.channel_mutex); |
Dexuan Cui | ca1c4b7 | 2015-08-13 17:07:03 -0700 | [diff] [blame] | 326 | |
| 327 | primary_channel = channel; |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 328 | } else { |
| 329 | primary_channel = channel->primary_channel; |
Vitaly Kuznetsov | 67fae05 | 2015-01-20 16:45:05 +0100 | [diff] [blame] | 330 | spin_lock_irqsave(&primary_channel->lock, flags); |
K. Y. Srinivasan | 565ce64 | 2013-10-16 19:27:19 -0700 | [diff] [blame] | 331 | list_del(&channel->sc_list); |
Vitaly Kuznetsov | 357e836 | 2015-05-06 17:47:44 -0700 | [diff] [blame] | 332 | primary_channel->num_sc--; |
Vitaly Kuznetsov | 67fae05 | 2015-01-20 16:45:05 +0100 | [diff] [blame] | 333 | spin_unlock_irqrestore(&primary_channel->lock, flags); |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 334 | } |
Dexuan Cui | ca1c4b7 | 2015-08-13 17:07:03 -0700 | [diff] [blame] | 335 | |
| 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. Srinivasan | c870597 | 2013-03-15 12:25:44 -0700 | [diff] [blame] | 343 | free_channel(channel); |
Timo Teräs | 4b2f9ab | 2010-12-15 20:48:09 +0200 | [diff] [blame] | 344 | } |
Haiyang Zhang | 8b5d6d3 | 2010-05-28 23:22:44 +0000 | [diff] [blame] | 345 | |
K. Y. Srinivasan | 93e5bd0 | 2011-12-12 09:29:17 -0800 | [diff] [blame] | 346 | void vmbus_free_channels(void) |
| 347 | { |
Dexuan Cui | 813c5b7 | 2015-04-22 21:31:30 -0700 | [diff] [blame] | 348 | struct vmbus_channel *channel, *tmp; |
K. Y. Srinivasan | 93e5bd0 | 2011-12-12 09:29:17 -0800 | [diff] [blame] | 349 | |
Dexuan Cui | 813c5b7 | 2015-04-22 21:31:30 -0700 | [diff] [blame] | 350 | list_for_each_entry_safe(channel, tmp, &vmbus_connection.chn_list, |
| 351 | listentry) { |
Dexuan Cui | 34c6801 | 2015-12-14 16:01:49 -0800 | [diff] [blame] | 352 | /* hv_process_channel_removal() needs this */ |
Dexuan Cui | 813c5b7 | 2015-04-22 21:31:30 -0700 | [diff] [blame] | 353 | channel->rescind = true; |
| 354 | |
K. Y. Srinivasan | 93e5bd0 | 2011-12-12 09:29:17 -0800 | [diff] [blame] | 355 | vmbus_device_unregister(channel->device_obj); |
K. Y. Srinivasan | 93e5bd0 | 2011-12-12 09:29:17 -0800 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 359 | /* |
Haiyang Zhang | e98cb27 | 2010-10-15 10:14:07 -0700 | [diff] [blame] | 360 | * vmbus_process_offer - Process the offer by creating a channel/device |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 361 | * associated with this offer |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 362 | */ |
K. Y. Srinivasan | 2dd37cb | 2015-02-28 11:18:18 -0800 | [diff] [blame] | 363 | static void vmbus_process_offer(struct vmbus_channel *newchannel) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 364 | { |
Greg Kroah-Hartman | aded716 | 2009-08-18 15:21:19 -0700 | [diff] [blame] | 365 | struct vmbus_channel *channel; |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 366 | bool fnew = true; |
Greg Kroah-Hartman | 0f5e44c | 2009-07-15 14:57:16 -0700 | [diff] [blame] | 367 | unsigned long flags; |
K. Y. Srinivasan | 7047f17 | 2015-12-25 20:00:30 -0800 | [diff] [blame] | 368 | u16 dev_type; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 369 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 370 | /* Make sure this is a new offer */ |
Dexuan Cui | d6f591e | 2015-12-14 16:01:51 -0800 | [diff] [blame] | 371 | mutex_lock(&vmbus_connection.channel_mutex); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 372 | |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 373 | list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) { |
K. Y. Srinivasan | 358d2ee | 2011-08-25 09:48:28 -0700 | [diff] [blame] | 374 | 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 Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 378 | fnew = false; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 379 | break; |
| 380 | } |
| 381 | } |
| 382 | |
Vitaly Kuznetsov | 8dfd332 | 2015-05-06 17:47:42 -0700 | [diff] [blame] | 383 | if (fnew) |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 384 | list_add_tail(&newchannel->listentry, |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 385 | &vmbus_connection.chn_list); |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 386 | |
Dexuan Cui | d6f591e | 2015-12-14 16:01:51 -0800 | [diff] [blame] | 387 | mutex_unlock(&vmbus_connection.channel_mutex); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 388 | |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 389 | if (!fnew) { |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 390 | /* |
| 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 Kuznetsov | 67fae05 | 2015-01-20 16:45:05 +0100 | [diff] [blame] | 398 | spin_lock_irqsave(&channel->lock, flags); |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 399 | list_add_tail(&newchannel->sc_list, &channel->sc_list); |
K. Y. Srinivasan | a13e8bb | 2015-02-28 11:39:02 -0800 | [diff] [blame] | 400 | channel->num_sc++; |
Vitaly Kuznetsov | 357e836 | 2015-05-06 17:47:44 -0700 | [diff] [blame] | 401 | spin_unlock_irqrestore(&channel->lock, flags); |
Vitaly Kuznetsov | 8dfd332 | 2015-05-06 17:47:42 -0700 | [diff] [blame] | 402 | } else |
| 403 | goto err_free_chan; |
| 404 | } |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 405 | |
K. Y. Srinivasan | 7047f17 | 2015-12-25 20:00:30 -0800 | [diff] [blame] | 406 | dev_type = hv_get_dev_type(&newchannel->offermsg.offer.if_type); |
| 407 | |
| 408 | init_vp_index(newchannel, dev_type); |
Vitaly Kuznetsov | f38e7dd | 2015-05-06 17:47:45 -0700 | [diff] [blame] | 409 | |
Vitaly Kuznetsov | 8dfd332 | 2015-05-06 17:47:42 -0700 | [diff] [blame] | 410 | 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 Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 420 | /* |
K. Y. Srinivasan | 42dceeb | 2013-08-26 14:08:58 -0700 | [diff] [blame] | 421 | * 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 Kuznetsov | 8dfd332 | 2015-05-06 17:47:42 -0700 | [diff] [blame] | 427 | if (!fnew) { |
| 428 | if (channel->sc_creation_callback != NULL) |
| 429 | channel->sc_creation_callback(newchannel); |
| 430 | return; |
| 431 | } |
| 432 | |
K. Y. Srinivasan | 42dceeb | 2013-08-26 14:08:58 -0700 | [diff] [blame] | 433 | /* |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 434 | * Start the process of binding this offer to the driver |
| 435 | * We need to set the DeviceObject field before calling |
Haiyang Zhang | 646f1ea | 2011-01-26 12:12:10 -0800 | [diff] [blame] | 436 | * vmbus_child_dev_add() |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 437 | */ |
K. Y. Srinivasan | f2c7301 | 2011-09-08 07:24:12 -0700 | [diff] [blame] | 438 | newchannel->device_obj = vmbus_device_create( |
Haiyang Zhang | 767dff6 | 2011-01-26 12:12:12 -0800 | [diff] [blame] | 439 | &newchannel->offermsg.offer.if_type, |
| 440 | &newchannel->offermsg.offer.if_instance, |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 441 | newchannel); |
Vitaly Kuznetsov | 9c3a6f7 | 2015-01-20 16:45:04 +0100 | [diff] [blame] | 442 | if (!newchannel->device_obj) |
K. Y. Srinivasan | 5b1e5b5 | 2015-02-28 11:18:19 -0800 | [diff] [blame] | 443 | goto err_deq_chan; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 444 | |
K. Y. Srinivasan | 7047f17 | 2015-12-25 20:00:30 -0800 | [diff] [blame] | 445 | newchannel->device_obj->device_id = dev_type; |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 446 | /* |
| 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 Cui | d43e2fe | 2015-03-27 09:10:09 -0700 | [diff] [blame] | 451 | 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 Kuznetsov | 9c3a6f7 | 2015-01-20 16:45:04 +0100 | [diff] [blame] | 457 | return; |
K. Y. Srinivasan | 2dd37cb | 2015-02-28 11:18:18 -0800 | [diff] [blame] | 458 | |
K. Y. Srinivasan | 5b1e5b5 | 2015-02-28 11:18:19 -0800 | [diff] [blame] | 459 | err_deq_chan: |
Dexuan Cui | f52078c | 2015-12-14 16:01:50 -0800 | [diff] [blame] | 460 | vmbus_release_relid(newchannel->offermsg.child_relid); |
| 461 | |
Dexuan Cui | d6f591e | 2015-12-14 16:01:51 -0800 | [diff] [blame] | 462 | mutex_lock(&vmbus_connection.channel_mutex); |
K. Y. Srinivasan | 5b1e5b5 | 2015-02-28 11:18:19 -0800 | [diff] [blame] | 463 | list_del(&newchannel->listentry); |
Dexuan Cui | d6f591e | 2015-12-14 16:01:51 -0800 | [diff] [blame] | 464 | mutex_unlock(&vmbus_connection.channel_mutex); |
K. Y. Srinivasan | 5b1e5b5 | 2015-02-28 11:18:19 -0800 | [diff] [blame] | 465 | |
| 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 Kuznetsov | 9c3a6f7 | 2015-01-20 16:45:04 +0100 | [diff] [blame] | 475 | err_free_chan: |
| 476 | free_channel(newchannel); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 477 | } |
| 478 | |
K. Y. Srinivasan | a119845 | 2012-12-01 06:46:50 -0800 | [diff] [blame] | 479 | /* |
| 480 | * We use this state to statically distribute the channel interrupt load. |
| 481 | */ |
K. Y. Srinivasan | 1f656ff | 2015-05-30 23:37:48 -0700 | [diff] [blame] | 482 | static int next_numa_node_id; |
K. Y. Srinivasan | a119845 | 2012-12-01 06:46:50 -0800 | [diff] [blame] | 483 | |
| 484 | /* |
| 485 | * Starting with Win8, we can statically distribute the incoming |
K. Y. Srinivasan | 1f656ff | 2015-05-30 23:37:48 -0700 | [diff] [blame] | 486 | * 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. Srinivasan | a119845 | 2012-12-01 06:46:50 -0800 | [diff] [blame] | 494 | */ |
K. Y. Srinivasan | 7047f17 | 2015-12-25 20:00:30 -0800 | [diff] [blame] | 495 | static void init_vp_index(struct vmbus_channel *channel, u16 dev_type) |
K. Y. Srinivasan | a119845 | 2012-12-01 06:46:50 -0800 | [diff] [blame] | 496 | { |
| 497 | u32 cur_cpu; |
K. Y. Srinivasan | 7047f17 | 2015-12-25 20:00:30 -0800 | [diff] [blame] | 498 | bool perf_chn = vmbus_devs[dev_type].perf_device; |
K. Y. Srinivasan | 1f656ff | 2015-05-30 23:37:48 -0700 | [diff] [blame] | 499 | struct vmbus_channel *primary = channel->primary_channel; |
| 500 | int next_node; |
| 501 | struct cpumask available_mask; |
K. Y. Srinivasan | 9f01ec5 | 2015-08-05 00:52:38 -0700 | [diff] [blame] | 502 | struct cpumask *alloced_mask; |
K. Y. Srinivasan | a119845 | 2012-12-01 06:46:50 -0800 | [diff] [blame] | 503 | |
K. Y. Srinivasan | a119845 | 2012-12-01 06:46:50 -0800 | [diff] [blame] | 504 | 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. Srinivasan | 1f656ff | 2015-05-30 23:37:48 -0700 | [diff] [blame] | 512 | channel->numa_node = 0; |
K. Y. Srinivasan | d3ba720 | 2014-04-08 18:45:53 -0700 | [diff] [blame] | 513 | channel->target_cpu = 0; |
K. Y. Srinivasan | 9c6e64a | 2015-05-30 23:37:47 -0700 | [diff] [blame] | 514 | channel->target_vp = hv_context.vp_index[0]; |
K. Y. Srinivasan | d3ba720 | 2014-04-08 18:45:53 -0700 | [diff] [blame] | 515 | return; |
K. Y. Srinivasan | a119845 | 2012-12-01 06:46:50 -0800 | [diff] [blame] | 516 | } |
Vitaly Kuznetsov | ce59fec | 2015-05-06 17:47:46 -0700 | [diff] [blame] | 517 | |
| 518 | /* |
K. Y. Srinivasan | 1f656ff | 2015-05-30 23:37:48 -0700 | [diff] [blame] | 519 | * 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 Kuznetsov | ce59fec | 2015-05-06 17:47:46 -0700 | [diff] [blame] | 524 | */ |
K. Y. Srinivasan | 1f656ff | 2015-05-30 23:37:48 -0700 | [diff] [blame] | 525 | 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 Kuznetsov | ce59fec | 2015-05-06 17:47:46 -0700 | [diff] [blame] | 533 | } |
K. Y. Srinivasan | 1f656ff | 2015-05-30 23:37:48 -0700 | [diff] [blame] | 534 | channel->numa_node = next_node; |
| 535 | primary = channel; |
Vitaly Kuznetsov | ce59fec | 2015-05-06 17:47:46 -0700 | [diff] [blame] | 536 | } |
K. Y. Srinivasan | 9f01ec5 | 2015-08-05 00:52:38 -0700 | [diff] [blame] | 537 | alloced_mask = &hv_context.hv_numa_map[primary->numa_node]; |
Vitaly Kuznetsov | ce59fec | 2015-05-06 17:47:46 -0700 | [diff] [blame] | 538 | |
K. Y. Srinivasan | 9f01ec5 | 2015-08-05 00:52:38 -0700 | [diff] [blame] | 539 | if (cpumask_weight(alloced_mask) == |
K. Y. Srinivasan | 1f656ff | 2015-05-30 23:37:48 -0700 | [diff] [blame] | 540 | 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. Srinivasan | 9f01ec5 | 2015-08-05 00:52:38 -0700 | [diff] [blame] | 545 | cpumask_clear(alloced_mask); |
K. Y. Srinivasan | 1f656ff | 2015-05-30 23:37:48 -0700 | [diff] [blame] | 546 | } |
| 547 | |
K. Y. Srinivasan | 9f01ec5 | 2015-08-05 00:52:38 -0700 | [diff] [blame] | 548 | cpumask_xor(&available_mask, alloced_mask, |
K. Y. Srinivasan | 1f656ff | 2015-05-30 23:37:48 -0700 | [diff] [blame] | 549 | cpumask_of_node(primary->numa_node)); |
| 550 | |
Dexuan Cui | 3b71107 | 2015-08-05 00:52:39 -0700 | [diff] [blame] | 551 | cur_cpu = -1; |
Vitaly Kuznetsov | 79fd8e7 | 2016-01-27 22:29:34 -0800 | [diff] [blame^] | 552 | |
| 553 | /* |
| 554 | * Normally Hyper-V host doesn't create more subchannels than there |
| 555 | * are VCPUs on the node but it is possible when not all present VCPUs |
| 556 | * on the node are initialized by guest. Clear the alloced_cpus_in_node |
| 557 | * to start over. |
| 558 | */ |
| 559 | if (cpumask_equal(&primary->alloced_cpus_in_node, |
| 560 | cpumask_of_node(primary->numa_node))) |
| 561 | cpumask_clear(&primary->alloced_cpus_in_node); |
| 562 | |
Dexuan Cui | 3b71107 | 2015-08-05 00:52:39 -0700 | [diff] [blame] | 563 | while (true) { |
| 564 | cur_cpu = cpumask_next(cur_cpu, &available_mask); |
| 565 | if (cur_cpu >= nr_cpu_ids) { |
| 566 | cur_cpu = -1; |
| 567 | cpumask_copy(&available_mask, |
| 568 | cpumask_of_node(primary->numa_node)); |
| 569 | continue; |
| 570 | } |
| 571 | |
Dexuan Cui | ca1c4b7 | 2015-08-13 17:07:03 -0700 | [diff] [blame] | 572 | /* |
| 573 | * NOTE: in the case of sub-channel, we clear the sub-channel |
| 574 | * related bit(s) in primary->alloced_cpus_in_node in |
| 575 | * hv_process_channel_removal(), so when we reload drivers |
| 576 | * like hv_netvsc in SMP guest, here we're able to re-allocate |
| 577 | * bit from primary->alloced_cpus_in_node. |
| 578 | */ |
Dexuan Cui | 3b71107 | 2015-08-05 00:52:39 -0700 | [diff] [blame] | 579 | if (!cpumask_test_cpu(cur_cpu, |
| 580 | &primary->alloced_cpus_in_node)) { |
| 581 | cpumask_set_cpu(cur_cpu, |
| 582 | &primary->alloced_cpus_in_node); |
| 583 | cpumask_set_cpu(cur_cpu, alloced_mask); |
| 584 | break; |
| 585 | } |
| 586 | } |
K. Y. Srinivasan | 1f656ff | 2015-05-30 23:37:48 -0700 | [diff] [blame] | 587 | |
K. Y. Srinivasan | d3ba720 | 2014-04-08 18:45:53 -0700 | [diff] [blame] | 588 | channel->target_cpu = cur_cpu; |
| 589 | channel->target_vp = hv_context.vp_index[cur_cpu]; |
K. Y. Srinivasan | a119845 | 2012-12-01 06:46:50 -0800 | [diff] [blame] | 590 | } |
| 591 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 592 | /* |
K. Y. Srinivasan | 2db84ef | 2015-04-22 21:31:32 -0700 | [diff] [blame] | 593 | * vmbus_unload_response - Handler for the unload response. |
| 594 | */ |
| 595 | static void vmbus_unload_response(struct vmbus_channel_message_header *hdr) |
| 596 | { |
| 597 | /* |
| 598 | * This is a global event; just wakeup the waiting thread. |
| 599 | * Once we successfully unload, we can cleanup the monitor state. |
| 600 | */ |
| 601 | complete(&vmbus_connection.unload_event); |
| 602 | } |
| 603 | |
| 604 | void vmbus_initiate_unload(void) |
| 605 | { |
| 606 | struct vmbus_channel_message_header hdr; |
| 607 | |
Vitaly Kuznetsov | 4a54243 | 2015-08-01 16:08:19 -0700 | [diff] [blame] | 608 | /* Pre-Win2012R2 hosts don't support reconnect */ |
| 609 | if (vmbus_proto_version < VERSION_WIN8_1) |
| 610 | return; |
| 611 | |
K. Y. Srinivasan | 2db84ef | 2015-04-22 21:31:32 -0700 | [diff] [blame] | 612 | init_completion(&vmbus_connection.unload_event); |
| 613 | memset(&hdr, 0, sizeof(struct vmbus_channel_message_header)); |
| 614 | hdr.msgtype = CHANNELMSG_UNLOAD; |
| 615 | vmbus_post_msg(&hdr, sizeof(struct vmbus_channel_message_header)); |
| 616 | |
| 617 | wait_for_completion(&vmbus_connection.unload_event); |
| 618 | } |
| 619 | |
| 620 | /* |
Haiyang Zhang | e98cb27 | 2010-10-15 10:14:07 -0700 | [diff] [blame] | 621 | * vmbus_onoffer - Handler for channel offers from vmbus in parent partition. |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 622 | * |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 623 | */ |
Haiyang Zhang | e98cb27 | 2010-10-15 10:14:07 -0700 | [diff] [blame] | 624 | static void vmbus_onoffer(struct vmbus_channel_message_header *hdr) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 625 | { |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 626 | struct vmbus_channel_offer_channel *offer; |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 627 | struct vmbus_channel *newchannel; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 628 | |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 629 | offer = (struct vmbus_channel_offer_channel *)hdr; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 630 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 631 | /* Allocate the channel object and save this offer. */ |
Haiyang Zhang | e98cb27 | 2010-10-15 10:14:07 -0700 | [diff] [blame] | 632 | newchannel = alloc_channel(); |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 633 | if (!newchannel) { |
Hank Janssen | 0a46618 | 2011-03-29 13:58:47 -0700 | [diff] [blame] | 634 | pr_err("Unable to allocate channel object\n"); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 635 | return; |
| 636 | } |
| 637 | |
K. Y. Srinivasan | 132368b | 2012-12-01 06:46:33 -0800 | [diff] [blame] | 638 | /* |
| 639 | * By default we setup state to enable batched |
| 640 | * reading. A specific service can choose to |
| 641 | * disable this prior to opening the channel. |
| 642 | */ |
| 643 | newchannel->batched_reading = true; |
| 644 | |
K. Y. Srinivasan | b3bf60c | 2012-12-01 06:46:45 -0800 | [diff] [blame] | 645 | /* |
| 646 | * Setup state for signalling the host. |
| 647 | */ |
| 648 | newchannel->sig_event = (struct hv_input_signal_event *) |
| 649 | (ALIGN((unsigned long) |
| 650 | &newchannel->sig_buf, |
| 651 | HV_HYPERCALL_PARAM_ALIGN)); |
| 652 | |
| 653 | newchannel->sig_event->connectionid.asu32 = 0; |
| 654 | newchannel->sig_event->connectionid.u.id = VMBUS_EVENT_CONNECTION_ID; |
| 655 | newchannel->sig_event->flag_number = 0; |
| 656 | newchannel->sig_event->rsvdz = 0; |
| 657 | |
| 658 | if (vmbus_proto_version != VERSION_WS2008) { |
| 659 | newchannel->is_dedicated_interrupt = |
| 660 | (offer->is_dedicated_interrupt != 0); |
| 661 | newchannel->sig_event->connectionid.u.id = |
| 662 | offer->connection_id; |
| 663 | } |
| 664 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 665 | memcpy(&newchannel->offermsg, offer, |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 666 | sizeof(struct vmbus_channel_offer_channel)); |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 667 | newchannel->monitor_grp = (u8)offer->monitorid / 32; |
| 668 | newchannel->monitor_bit = (u8)offer->monitorid % 32; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 669 | |
K. Y. Srinivasan | 2dd37cb | 2015-02-28 11:18:18 -0800 | [diff] [blame] | 670 | vmbus_process_offer(newchannel); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 671 | } |
| 672 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 673 | /* |
Haiyang Zhang | e98cb27 | 2010-10-15 10:14:07 -0700 | [diff] [blame] | 674 | * vmbus_onoffer_rescind - Rescind offer handler. |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 675 | * |
| 676 | * We queue a work item to process this offer synchronously |
| 677 | */ |
Haiyang Zhang | e98cb27 | 2010-10-15 10:14:07 -0700 | [diff] [blame] | 678 | static void vmbus_onoffer_rescind(struct vmbus_channel_message_header *hdr) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 679 | { |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 680 | struct vmbus_channel_rescind_offer *rescind; |
Greg Kroah-Hartman | aded716 | 2009-08-18 15:21:19 -0700 | [diff] [blame] | 681 | struct vmbus_channel *channel; |
Dexuan Cui | d43e2fe | 2015-03-27 09:10:09 -0700 | [diff] [blame] | 682 | unsigned long flags; |
| 683 | struct device *dev; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 684 | |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 685 | rescind = (struct vmbus_channel_rescind_offer *)hdr; |
Dexuan Cui | d43e2fe | 2015-03-27 09:10:09 -0700 | [diff] [blame] | 686 | channel = relid2channel(rescind->child_relid); |
Hank Janssen | 98e0870 | 2011-03-29 13:58:44 -0700 | [diff] [blame] | 687 | |
K. Y. Srinivasan | 2dd37cb | 2015-02-28 11:18:18 -0800 | [diff] [blame] | 688 | if (channel == NULL) { |
Dexuan Cui | f52078c | 2015-12-14 16:01:50 -0800 | [diff] [blame] | 689 | /* |
| 690 | * This is very impossible, because in |
| 691 | * vmbus_process_offer(), we have already invoked |
| 692 | * vmbus_release_relid() on error. |
| 693 | */ |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 694 | return; |
K. Y. Srinivasan | 2dd37cb | 2015-02-28 11:18:18 -0800 | [diff] [blame] | 695 | } |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 696 | |
Dexuan Cui | d43e2fe | 2015-03-27 09:10:09 -0700 | [diff] [blame] | 697 | spin_lock_irqsave(&channel->lock, flags); |
| 698 | channel->rescind = true; |
| 699 | spin_unlock_irqrestore(&channel->lock, flags); |
| 700 | |
| 701 | if (channel->device_obj) { |
| 702 | /* |
| 703 | * We will have to unregister this device from the |
| 704 | * driver core. |
| 705 | */ |
| 706 | dev = get_device(&channel->device_obj->device); |
| 707 | if (dev) { |
| 708 | vmbus_device_unregister(channel->device_obj); |
| 709 | put_device(dev); |
| 710 | } |
| 711 | } else { |
| 712 | hv_process_channel_removal(channel, |
| 713 | channel->offermsg.child_relid); |
K. Y. Srinivasan | 2dd37cb | 2015-02-28 11:18:18 -0800 | [diff] [blame] | 714 | } |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 715 | } |
| 716 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 717 | /* |
Haiyang Zhang | e98cb27 | 2010-10-15 10:14:07 -0700 | [diff] [blame] | 718 | * vmbus_onoffers_delivered - |
| 719 | * This is invoked when all offers have been delivered. |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 720 | * |
| 721 | * Nothing to do here. |
| 722 | */ |
Haiyang Zhang | e98cb27 | 2010-10-15 10:14:07 -0700 | [diff] [blame] | 723 | static void vmbus_onoffers_delivered( |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 724 | struct vmbus_channel_message_header *hdr) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 725 | { |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 726 | } |
| 727 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 728 | /* |
Haiyang Zhang | e98cb27 | 2010-10-15 10:14:07 -0700 | [diff] [blame] | 729 | * vmbus_onopen_result - Open result handler. |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 730 | * |
| 731 | * This is invoked when we received a response to our channel open request. |
| 732 | * Find the matching request, copy the response and signal the requesting |
| 733 | * thread. |
| 734 | */ |
Haiyang Zhang | e98cb27 | 2010-10-15 10:14:07 -0700 | [diff] [blame] | 735 | static void vmbus_onopen_result(struct vmbus_channel_message_header *hdr) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 736 | { |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 737 | struct vmbus_channel_open_result *result; |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 738 | struct vmbus_channel_msginfo *msginfo; |
| 739 | struct vmbus_channel_message_header *requestheader; |
| 740 | struct vmbus_channel_open_channel *openmsg; |
Greg Kroah-Hartman | dd0813b | 2009-07-15 14:56:45 -0700 | [diff] [blame] | 741 | unsigned long flags; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 742 | |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 743 | result = (struct vmbus_channel_open_result *)hdr; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 744 | |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 745 | /* |
| 746 | * Find the open msg, copy the result and signal/unblock the wait event |
| 747 | */ |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 748 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 749 | |
Hank Janssen | ebb61e5 | 2011-02-18 12:39:57 -0800 | [diff] [blame] | 750 | list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list, |
| 751 | msglistentry) { |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 752 | requestheader = |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 753 | (struct vmbus_channel_message_header *)msginfo->msg; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 754 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 755 | if (requestheader->msgtype == CHANNELMSG_OPENCHANNEL) { |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 756 | openmsg = |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 757 | (struct vmbus_channel_open_channel *)msginfo->msg; |
| 758 | if (openmsg->child_relid == result->child_relid && |
| 759 | openmsg->openid == result->openid) { |
| 760 | memcpy(&msginfo->response.open_result, |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 761 | result, |
K. Y. Srinivasan | 9568a19 | 2011-05-10 07:55:39 -0700 | [diff] [blame] | 762 | sizeof( |
| 763 | struct vmbus_channel_open_result)); |
| 764 | complete(&msginfo->waitevent); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 765 | break; |
| 766 | } |
| 767 | } |
| 768 | } |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 769 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 770 | } |
| 771 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 772 | /* |
Haiyang Zhang | e98cb27 | 2010-10-15 10:14:07 -0700 | [diff] [blame] | 773 | * vmbus_ongpadl_created - GPADL created handler. |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 774 | * |
| 775 | * This is invoked when we received a response to our gpadl create request. |
| 776 | * Find the matching request, copy the response and signal the requesting |
| 777 | * thread. |
| 778 | */ |
Haiyang Zhang | e98cb27 | 2010-10-15 10:14:07 -0700 | [diff] [blame] | 779 | static void vmbus_ongpadl_created(struct vmbus_channel_message_header *hdr) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 780 | { |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 781 | struct vmbus_channel_gpadl_created *gpadlcreated; |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 782 | struct vmbus_channel_msginfo *msginfo; |
| 783 | struct vmbus_channel_message_header *requestheader; |
| 784 | struct vmbus_channel_gpadl_header *gpadlheader; |
Greg Kroah-Hartman | dd0813b | 2009-07-15 14:56:45 -0700 | [diff] [blame] | 785 | unsigned long flags; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 786 | |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 787 | gpadlcreated = (struct vmbus_channel_gpadl_created *)hdr; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 788 | |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 789 | /* |
| 790 | * Find the establish msg, copy the result and signal/unblock the wait |
| 791 | * event |
| 792 | */ |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 793 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 794 | |
Hank Janssen | ebb61e5 | 2011-02-18 12:39:57 -0800 | [diff] [blame] | 795 | list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list, |
| 796 | msglistentry) { |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 797 | requestheader = |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 798 | (struct vmbus_channel_message_header *)msginfo->msg; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 799 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 800 | if (requestheader->msgtype == CHANNELMSG_GPADL_HEADER) { |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 801 | gpadlheader = |
| 802 | (struct vmbus_channel_gpadl_header *)requestheader; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 803 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 804 | if ((gpadlcreated->child_relid == |
| 805 | gpadlheader->child_relid) && |
| 806 | (gpadlcreated->gpadl == gpadlheader->gpadl)) { |
| 807 | memcpy(&msginfo->response.gpadl_created, |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 808 | gpadlcreated, |
K. Y. Srinivasan | 9568a19 | 2011-05-10 07:55:39 -0700 | [diff] [blame] | 809 | sizeof( |
| 810 | struct vmbus_channel_gpadl_created)); |
| 811 | complete(&msginfo->waitevent); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 812 | break; |
| 813 | } |
| 814 | } |
| 815 | } |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 816 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 817 | } |
| 818 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 819 | /* |
Haiyang Zhang | e98cb27 | 2010-10-15 10:14:07 -0700 | [diff] [blame] | 820 | * vmbus_ongpadl_torndown - GPADL torndown handler. |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 821 | * |
| 822 | * This is invoked when we received a response to our gpadl teardown request. |
| 823 | * Find the matching request, copy the response and signal the requesting |
| 824 | * thread. |
| 825 | */ |
Haiyang Zhang | e98cb27 | 2010-10-15 10:14:07 -0700 | [diff] [blame] | 826 | static void vmbus_ongpadl_torndown( |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 827 | struct vmbus_channel_message_header *hdr) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 828 | { |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 829 | struct vmbus_channel_gpadl_torndown *gpadl_torndown; |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 830 | struct vmbus_channel_msginfo *msginfo; |
| 831 | struct vmbus_channel_message_header *requestheader; |
| 832 | struct vmbus_channel_gpadl_teardown *gpadl_teardown; |
Greg Kroah-Hartman | dd0813b | 2009-07-15 14:56:45 -0700 | [diff] [blame] | 833 | unsigned long flags; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 834 | |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 835 | gpadl_torndown = (struct vmbus_channel_gpadl_torndown *)hdr; |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 836 | |
| 837 | /* |
| 838 | * Find the open msg, copy the result and signal/unblock the wait event |
| 839 | */ |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 840 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 841 | |
Hank Janssen | ebb61e5 | 2011-02-18 12:39:57 -0800 | [diff] [blame] | 842 | list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list, |
| 843 | msglistentry) { |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 844 | requestheader = |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 845 | (struct vmbus_channel_message_header *)msginfo->msg; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 846 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 847 | if (requestheader->msgtype == CHANNELMSG_GPADL_TEARDOWN) { |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 848 | gpadl_teardown = |
| 849 | (struct vmbus_channel_gpadl_teardown *)requestheader; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 850 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 851 | if (gpadl_torndown->gpadl == gpadl_teardown->gpadl) { |
| 852 | memcpy(&msginfo->response.gpadl_torndown, |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 853 | gpadl_torndown, |
K. Y. Srinivasan | 9568a19 | 2011-05-10 07:55:39 -0700 | [diff] [blame] | 854 | sizeof( |
| 855 | struct vmbus_channel_gpadl_torndown)); |
| 856 | complete(&msginfo->waitevent); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 857 | break; |
| 858 | } |
| 859 | } |
| 860 | } |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 861 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 862 | } |
| 863 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 864 | /* |
Haiyang Zhang | e98cb27 | 2010-10-15 10:14:07 -0700 | [diff] [blame] | 865 | * vmbus_onversion_response - Version response handler |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 866 | * |
| 867 | * This is invoked when we received a response to our initiate contact request. |
| 868 | * Find the matching request, copy the response and signal the requesting |
| 869 | * thread. |
| 870 | */ |
Haiyang Zhang | e98cb27 | 2010-10-15 10:14:07 -0700 | [diff] [blame] | 871 | static void vmbus_onversion_response( |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 872 | struct vmbus_channel_message_header *hdr) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 873 | { |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 874 | struct vmbus_channel_msginfo *msginfo; |
| 875 | struct vmbus_channel_message_header *requestheader; |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 876 | struct vmbus_channel_version_response *version_response; |
Greg Kroah-Hartman | dd0813b | 2009-07-15 14:56:45 -0700 | [diff] [blame] | 877 | unsigned long flags; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 878 | |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 879 | version_response = (struct vmbus_channel_version_response *)hdr; |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 880 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 881 | |
Hank Janssen | ebb61e5 | 2011-02-18 12:39:57 -0800 | [diff] [blame] | 882 | list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list, |
| 883 | msglistentry) { |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 884 | requestheader = |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 885 | (struct vmbus_channel_message_header *)msginfo->msg; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 886 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 887 | if (requestheader->msgtype == |
| 888 | CHANNELMSG_INITIATE_CONTACT) { |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 889 | memcpy(&msginfo->response.version_response, |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 890 | version_response, |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 891 | sizeof(struct vmbus_channel_version_response)); |
K. Y. Srinivasan | 9568a19 | 2011-05-10 07:55:39 -0700 | [diff] [blame] | 892 | complete(&msginfo->waitevent); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 893 | } |
| 894 | } |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 895 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 896 | } |
| 897 | |
Greg Kroah-Hartman | c8212f0 | 2009-08-31 21:51:50 -0700 | [diff] [blame] | 898 | /* Channel message dispatch table */ |
Dexuan Cui | 652594c | 2015-03-27 09:10:08 -0700 | [diff] [blame] | 899 | struct vmbus_channel_message_table_entry |
K. Y. Srinivasan | b7c6b02 | 2011-05-10 07:55:38 -0700 | [diff] [blame] | 900 | channel_message_table[CHANNELMSG_COUNT] = { |
Dexuan Cui | 652594c | 2015-03-27 09:10:08 -0700 | [diff] [blame] | 901 | {CHANNELMSG_INVALID, 0, NULL}, |
| 902 | {CHANNELMSG_OFFERCHANNEL, 0, vmbus_onoffer}, |
| 903 | {CHANNELMSG_RESCIND_CHANNELOFFER, 0, vmbus_onoffer_rescind}, |
| 904 | {CHANNELMSG_REQUESTOFFERS, 0, NULL}, |
| 905 | {CHANNELMSG_ALLOFFERS_DELIVERED, 1, vmbus_onoffers_delivered}, |
| 906 | {CHANNELMSG_OPENCHANNEL, 0, NULL}, |
| 907 | {CHANNELMSG_OPENCHANNEL_RESULT, 1, vmbus_onopen_result}, |
| 908 | {CHANNELMSG_CLOSECHANNEL, 0, NULL}, |
| 909 | {CHANNELMSG_GPADL_HEADER, 0, NULL}, |
| 910 | {CHANNELMSG_GPADL_BODY, 0, NULL}, |
| 911 | {CHANNELMSG_GPADL_CREATED, 1, vmbus_ongpadl_created}, |
| 912 | {CHANNELMSG_GPADL_TEARDOWN, 0, NULL}, |
| 913 | {CHANNELMSG_GPADL_TORNDOWN, 1, vmbus_ongpadl_torndown}, |
| 914 | {CHANNELMSG_RELID_RELEASED, 0, NULL}, |
| 915 | {CHANNELMSG_INITIATE_CONTACT, 0, NULL}, |
| 916 | {CHANNELMSG_VERSION_RESPONSE, 1, vmbus_onversion_response}, |
| 917 | {CHANNELMSG_UNLOAD, 0, NULL}, |
K. Y. Srinivasan | 2db84ef | 2015-04-22 21:31:32 -0700 | [diff] [blame] | 918 | {CHANNELMSG_UNLOAD_RESPONSE, 1, vmbus_unload_response}, |
Greg Kroah-Hartman | c8212f0 | 2009-08-31 21:51:50 -0700 | [diff] [blame] | 919 | }; |
| 920 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 921 | /* |
Haiyang Zhang | e98cb27 | 2010-10-15 10:14:07 -0700 | [diff] [blame] | 922 | * vmbus_onmessage - Handler for channel protocol messages. |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 923 | * |
| 924 | * This is invoked in the vmbus worker thread context. |
| 925 | */ |
Haiyang Zhang | e98cb27 | 2010-10-15 10:14:07 -0700 | [diff] [blame] | 926 | void vmbus_onmessage(void *context) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 927 | { |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 928 | struct hv_message *msg = context; |
Greg Kroah-Hartman | 8225021 | 2009-08-26 15:16:04 -0700 | [diff] [blame] | 929 | struct vmbus_channel_message_header *hdr; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 930 | int size; |
| 931 | |
Haiyang Zhang | f6feebe | 2010-11-08 14:04:39 -0800 | [diff] [blame] | 932 | hdr = (struct vmbus_channel_message_header *)msg->u.payload; |
| 933 | size = msg->header.payload_size; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 934 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 935 | if (hdr->msgtype >= CHANNELMSG_COUNT) { |
Hank Janssen | 0a46618 | 2011-03-29 13:58:47 -0700 | [diff] [blame] | 936 | pr_err("Received invalid channel message type %d size %d\n", |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 937 | hdr->msgtype, size); |
Greg Kroah-Hartman | 04f50c4 | 2009-07-16 12:35:37 -0700 | [diff] [blame] | 938 | print_hex_dump_bytes("", DUMP_PREFIX_NONE, |
Haiyang Zhang | f6feebe | 2010-11-08 14:04:39 -0800 | [diff] [blame] | 939 | (unsigned char *)msg->u.payload, size); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 940 | return; |
| 941 | } |
| 942 | |
K. Y. Srinivasan | b7c6b02 | 2011-05-10 07:55:38 -0700 | [diff] [blame] | 943 | if (channel_message_table[hdr->msgtype].message_handler) |
| 944 | channel_message_table[hdr->msgtype].message_handler(hdr); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 945 | else |
Hank Janssen | 0a46618 | 2011-03-29 13:58:47 -0700 | [diff] [blame] | 946 | pr_err("Unhandled channel message type %d\n", hdr->msgtype); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 947 | } |
| 948 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 949 | /* |
Haiyang Zhang | e98cb27 | 2010-10-15 10:14:07 -0700 | [diff] [blame] | 950 | * vmbus_request_offers - Send a request to get all our pending offers. |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 951 | */ |
Haiyang Zhang | e98cb27 | 2010-10-15 10:14:07 -0700 | [diff] [blame] | 952 | int vmbus_request_offers(void) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 953 | { |
Greg Kroah-Hartman | 8225021 | 2009-08-26 15:16:04 -0700 | [diff] [blame] | 954 | struct vmbus_channel_message_header *msg; |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 955 | struct vmbus_channel_msginfo *msginfo; |
Nicholas Mc Guire | 51e5181 | 2015-02-27 11:26:02 -0800 | [diff] [blame] | 956 | int ret; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 957 | |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 958 | msginfo = kmalloc(sizeof(*msginfo) + |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 959 | sizeof(struct vmbus_channel_message_header), |
| 960 | GFP_KERNEL); |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 961 | if (!msginfo) |
Bill Pemberton | 75910f2 | 2010-05-05 15:27:31 -0400 | [diff] [blame] | 962 | return -ENOMEM; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 963 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 964 | msg = (struct vmbus_channel_message_header *)msginfo->msg; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 965 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 966 | msg->msgtype = CHANNELMSG_REQUESTOFFERS; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 967 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 968 | |
Haiyang Zhang | c697767 | 2011-01-26 12:12:08 -0800 | [diff] [blame] | 969 | ret = vmbus_post_msg(msg, |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 970 | sizeof(struct vmbus_channel_message_header)); |
| 971 | if (ret != 0) { |
Hank Janssen | 0a46618 | 2011-03-29 13:58:47 -0700 | [diff] [blame] | 972 | pr_err("Unable to request offers - %d\n", ret); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 973 | |
K. Y. Srinivasan | 0c3b7b2 | 2011-02-11 09:59:43 -0800 | [diff] [blame] | 974 | goto cleanup; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 975 | } |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 976 | |
K. Y. Srinivasan | 0c3b7b2 | 2011-02-11 09:59:43 -0800 | [diff] [blame] | 977 | cleanup: |
Ilia Mirkin | dd9b15d | 2011-03-13 00:29:00 -0500 | [diff] [blame] | 978 | kfree(msginfo); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 979 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 980 | return ret; |
| 981 | } |
| 982 | |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 983 | /* |
| 984 | * Retrieve the (sub) channel on which to send an outgoing request. |
K. Y. Srinivasan | a13e8bb | 2015-02-28 11:39:02 -0800 | [diff] [blame] | 985 | * When a primary channel has multiple sub-channels, we try to |
| 986 | * distribute the load equally amongst all available channels. |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 987 | */ |
| 988 | struct vmbus_channel *vmbus_get_outgoing_channel(struct vmbus_channel *primary) |
| 989 | { |
| 990 | struct list_head *cur, *tmp; |
K. Y. Srinivasan | 87712bf8 | 2014-12-14 23:34:51 -0800 | [diff] [blame] | 991 | int cur_cpu; |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 992 | struct vmbus_channel *cur_channel; |
| 993 | struct vmbus_channel *outgoing_channel = primary; |
K. Y. Srinivasan | a13e8bb | 2015-02-28 11:39:02 -0800 | [diff] [blame] | 994 | int next_channel; |
| 995 | int i = 1; |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 996 | |
| 997 | if (list_empty(&primary->sc_list)) |
| 998 | return outgoing_channel; |
| 999 | |
K. Y. Srinivasan | a13e8bb | 2015-02-28 11:39:02 -0800 | [diff] [blame] | 1000 | next_channel = primary->next_oc++; |
| 1001 | |
| 1002 | if (next_channel > (primary->num_sc)) { |
| 1003 | primary->next_oc = 0; |
| 1004 | return outgoing_channel; |
| 1005 | } |
| 1006 | |
K. Y. Srinivasan | 87712bf8 | 2014-12-14 23:34:51 -0800 | [diff] [blame] | 1007 | cur_cpu = hv_context.vp_index[get_cpu()]; |
| 1008 | put_cpu(); |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 1009 | list_for_each_safe(cur, tmp, &primary->sc_list) { |
| 1010 | cur_channel = list_entry(cur, struct vmbus_channel, sc_list); |
| 1011 | if (cur_channel->state != CHANNEL_OPENED_STATE) |
| 1012 | continue; |
| 1013 | |
| 1014 | if (cur_channel->target_vp == cur_cpu) |
| 1015 | return cur_channel; |
| 1016 | |
K. Y. Srinivasan | a13e8bb | 2015-02-28 11:39:02 -0800 | [diff] [blame] | 1017 | if (i == next_channel) |
| 1018 | return cur_channel; |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 1019 | |
K. Y. Srinivasan | a13e8bb | 2015-02-28 11:39:02 -0800 | [diff] [blame] | 1020 | i++; |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | return outgoing_channel; |
| 1024 | } |
| 1025 | EXPORT_SYMBOL_GPL(vmbus_get_outgoing_channel); |
| 1026 | |
| 1027 | static void invoke_sc_cb(struct vmbus_channel *primary_channel) |
| 1028 | { |
| 1029 | struct list_head *cur, *tmp; |
| 1030 | struct vmbus_channel *cur_channel; |
| 1031 | |
| 1032 | if (primary_channel->sc_creation_callback == NULL) |
| 1033 | return; |
| 1034 | |
| 1035 | list_for_each_safe(cur, tmp, &primary_channel->sc_list) { |
| 1036 | cur_channel = list_entry(cur, struct vmbus_channel, sc_list); |
| 1037 | |
| 1038 | primary_channel->sc_creation_callback(cur_channel); |
| 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | void vmbus_set_sc_create_callback(struct vmbus_channel *primary_channel, |
| 1043 | void (*sc_cr_cb)(struct vmbus_channel *new_sc)) |
| 1044 | { |
| 1045 | primary_channel->sc_creation_callback = sc_cr_cb; |
| 1046 | } |
| 1047 | EXPORT_SYMBOL_GPL(vmbus_set_sc_create_callback); |
| 1048 | |
| 1049 | bool vmbus_are_subchannels_present(struct vmbus_channel *primary) |
| 1050 | { |
| 1051 | bool ret; |
| 1052 | |
| 1053 | ret = !list_empty(&primary->sc_list); |
| 1054 | |
| 1055 | if (ret) { |
| 1056 | /* |
| 1057 | * Invoke the callback on sub-channel creation. |
| 1058 | * This will present a uniform interface to the |
| 1059 | * clients. |
| 1060 | */ |
| 1061 | invoke_sc_cb(primary); |
| 1062 | } |
| 1063 | |
| 1064 | return ret; |
| 1065 | } |
| 1066 | EXPORT_SYMBOL_GPL(vmbus_are_subchannels_present); |