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> |
Dexuan Cui | 638fea3 | 2016-06-09 18:47:24 -0700 | [diff] [blame^] | 24 | #include <linux/interrupt.h> |
K. Y. Srinivasan | 0c3b7b2 | 2011-02-11 09:59:43 -0800 | [diff] [blame] | 25 | #include <linux/sched.h> |
| 26 | #include <linux/wait.h> |
Greg Kroah-Hartman | a0086dc | 2009-08-17 17:22:08 -0700 | [diff] [blame] | 27 | #include <linux/mm.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 28 | #include <linux/slab.h> |
Bill Pemberton | 53af545 | 2009-09-11 21:46:44 -0400 | [diff] [blame] | 29 | #include <linux/list.h> |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 30 | #include <linux/module.h> |
Haiyang Zhang | 8b5d6d3 | 2010-05-28 23:22:44 +0000 | [diff] [blame] | 31 | #include <linux/completion.h> |
Vitaly Kuznetsov | 4157191 | 2016-01-27 22:29:35 -0800 | [diff] [blame] | 32 | #include <linux/delay.h> |
Greg Kroah-Hartman | 46a9719 | 2011-10-04 12:29:52 -0700 | [diff] [blame] | 33 | #include <linux/hyperv.h> |
K. Y. Srinivasan | 3f335ea | 2011-05-12 19:34:15 -0700 | [diff] [blame] | 34 | |
K. Y. Srinivasan | 0f2a661 | 2011-05-12 19:34:28 -0700 | [diff] [blame] | 35 | #include "hyperv_vmbus.h" |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 36 | |
K. Y. Srinivasan | 7047f17 | 2015-12-25 20:00:30 -0800 | [diff] [blame] | 37 | static void init_vp_index(struct vmbus_channel *channel, u16 dev_type); |
| 38 | |
| 39 | static const struct vmbus_device vmbus_devs[] = { |
| 40 | /* IDE */ |
| 41 | { .dev_type = HV_IDE, |
| 42 | HV_IDE_GUID, |
| 43 | .perf_device = true, |
| 44 | }, |
| 45 | |
| 46 | /* SCSI */ |
| 47 | { .dev_type = HV_SCSI, |
| 48 | HV_SCSI_GUID, |
| 49 | .perf_device = true, |
| 50 | }, |
| 51 | |
| 52 | /* Fibre Channel */ |
| 53 | { .dev_type = HV_FC, |
| 54 | HV_SYNTHFC_GUID, |
| 55 | .perf_device = true, |
| 56 | }, |
| 57 | |
| 58 | /* Synthetic NIC */ |
| 59 | { .dev_type = HV_NIC, |
| 60 | HV_NIC_GUID, |
| 61 | .perf_device = true, |
| 62 | }, |
| 63 | |
| 64 | /* Network Direct */ |
| 65 | { .dev_type = HV_ND, |
| 66 | HV_ND_GUID, |
| 67 | .perf_device = true, |
| 68 | }, |
| 69 | |
| 70 | /* PCIE */ |
| 71 | { .dev_type = HV_PCIE, |
| 72 | HV_PCIE_GUID, |
| 73 | .perf_device = true, |
| 74 | }, |
| 75 | |
| 76 | /* Synthetic Frame Buffer */ |
| 77 | { .dev_type = HV_FB, |
| 78 | HV_SYNTHVID_GUID, |
| 79 | .perf_device = false, |
| 80 | }, |
| 81 | |
| 82 | /* Synthetic Keyboard */ |
| 83 | { .dev_type = HV_KBD, |
| 84 | HV_KBD_GUID, |
| 85 | .perf_device = false, |
| 86 | }, |
| 87 | |
| 88 | /* Synthetic MOUSE */ |
| 89 | { .dev_type = HV_MOUSE, |
| 90 | HV_MOUSE_GUID, |
| 91 | .perf_device = false, |
| 92 | }, |
| 93 | |
| 94 | /* KVP */ |
| 95 | { .dev_type = HV_KVP, |
| 96 | HV_KVP_GUID, |
| 97 | .perf_device = false, |
| 98 | }, |
| 99 | |
| 100 | /* Time Synch */ |
| 101 | { .dev_type = HV_TS, |
| 102 | HV_TS_GUID, |
| 103 | .perf_device = false, |
| 104 | }, |
| 105 | |
| 106 | /* Heartbeat */ |
| 107 | { .dev_type = HV_HB, |
| 108 | HV_HEART_BEAT_GUID, |
| 109 | .perf_device = false, |
| 110 | }, |
| 111 | |
| 112 | /* Shutdown */ |
| 113 | { .dev_type = HV_SHUTDOWN, |
| 114 | HV_SHUTDOWN_GUID, |
| 115 | .perf_device = false, |
| 116 | }, |
| 117 | |
| 118 | /* File copy */ |
| 119 | { .dev_type = HV_FCOPY, |
| 120 | HV_FCOPY_GUID, |
| 121 | .perf_device = false, |
| 122 | }, |
| 123 | |
| 124 | /* Backup */ |
| 125 | { .dev_type = HV_BACKUP, |
| 126 | HV_VSS_GUID, |
| 127 | .perf_device = false, |
| 128 | }, |
| 129 | |
| 130 | /* Dynamic Memory */ |
| 131 | { .dev_type = HV_DM, |
| 132 | HV_DM_GUID, |
| 133 | .perf_device = false, |
| 134 | }, |
| 135 | |
| 136 | /* Unknown GUID */ |
| 137 | { .dev_type = HV_UNKOWN, |
| 138 | .perf_device = false, |
| 139 | }, |
| 140 | }; |
| 141 | |
| 142 | static u16 hv_get_dev_type(const uuid_le *guid) |
| 143 | { |
| 144 | u16 i; |
| 145 | |
| 146 | for (i = HV_IDE; i < HV_UNKOWN; i++) { |
| 147 | if (!uuid_le_cmp(*guid, vmbus_devs[i].guid)) |
| 148 | return i; |
| 149 | } |
| 150 | pr_info("Unknown GUID: %pUl\n", guid); |
| 151 | return i; |
| 152 | } |
Vitaly Kuznetsov | f38e7dd | 2015-05-06 17:47:45 -0700 | [diff] [blame] | 153 | |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 154 | /** |
Greg Kroah-Hartman | da0e963 | 2011-10-11 08:42:28 -0600 | [diff] [blame] | 155 | * vmbus_prep_negotiate_resp() - Create default response for Hyper-V Negotiate message |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 156 | * @icmsghdrp: Pointer to msg header structure |
| 157 | * @icmsg_negotiate: Pointer to negotiate message structure |
| 158 | * @buf: Raw buffer channel data |
| 159 | * |
| 160 | * @icmsghdrp is of type &struct icmsg_hdr. |
| 161 | * @negop is of type &struct icmsg_negotiate. |
K. Y. Srinivasan | c836d0a | 2012-05-12 13:44:58 -0700 | [diff] [blame] | 162 | * Set up and fill in default negotiate response message. |
| 163 | * |
K. Y. Srinivasan | 6741335 | 2013-07-02 10:31:30 -0700 | [diff] [blame] | 164 | * The fw_version specifies the framework version that |
| 165 | * we can support and srv_version specifies the service |
| 166 | * version we can support. |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 167 | * |
| 168 | * Mainly used by Hyper-V drivers. |
| 169 | */ |
K. Y. Srinivasan | 6741335 | 2013-07-02 10:31:30 -0700 | [diff] [blame] | 170 | bool vmbus_prep_negotiate_resp(struct icmsg_hdr *icmsghdrp, |
K. Y. Srinivasan | c836d0a | 2012-05-12 13:44:58 -0700 | [diff] [blame] | 171 | struct icmsg_negotiate *negop, u8 *buf, |
K. Y. Srinivasan | 6741335 | 2013-07-02 10:31:30 -0700 | [diff] [blame] | 172 | int fw_version, int srv_version) |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 173 | { |
K. Y. Srinivasan | 6741335 | 2013-07-02 10:31:30 -0700 | [diff] [blame] | 174 | int icframe_major, icframe_minor; |
| 175 | int icmsg_major, icmsg_minor; |
| 176 | int fw_major, fw_minor; |
| 177 | int srv_major, srv_minor; |
K. Y. Srinivasan | c836d0a | 2012-05-12 13:44:58 -0700 | [diff] [blame] | 178 | int i; |
K. Y. Srinivasan | 6741335 | 2013-07-02 10:31:30 -0700 | [diff] [blame] | 179 | bool found_match = false; |
K. Y. Srinivasan | c836d0a | 2012-05-12 13:44:58 -0700 | [diff] [blame] | 180 | |
K. Y. Srinivasan | a360530 | 2012-05-12 13:44:57 -0700 | [diff] [blame] | 181 | icmsghdrp->icmsgsize = 0x10; |
K. Y. Srinivasan | 6741335 | 2013-07-02 10:31:30 -0700 | [diff] [blame] | 182 | fw_major = (fw_version >> 16); |
| 183 | fw_minor = (fw_version & 0xFFFF); |
| 184 | |
| 185 | srv_major = (srv_version >> 16); |
| 186 | srv_minor = (srv_version & 0xFFFF); |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 187 | |
K. Y. Srinivasan | a360530 | 2012-05-12 13:44:57 -0700 | [diff] [blame] | 188 | negop = (struct icmsg_negotiate *)&buf[ |
| 189 | sizeof(struct vmbuspipe_hdr) + |
| 190 | sizeof(struct icmsg_hdr)]; |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 191 | |
K. Y. Srinivasan | 6741335 | 2013-07-02 10:31:30 -0700 | [diff] [blame] | 192 | icframe_major = negop->icframe_vercnt; |
| 193 | icframe_minor = 0; |
| 194 | |
| 195 | icmsg_major = negop->icmsg_vercnt; |
| 196 | icmsg_minor = 0; |
K. Y. Srinivasan | c836d0a | 2012-05-12 13:44:58 -0700 | [diff] [blame] | 197 | |
| 198 | /* |
| 199 | * Select the framework version number we will |
| 200 | * support. |
| 201 | */ |
| 202 | |
| 203 | for (i = 0; i < negop->icframe_vercnt; i++) { |
K. Y. Srinivasan | 6741335 | 2013-07-02 10:31:30 -0700 | [diff] [blame] | 204 | if ((negop->icversion_data[i].major == fw_major) && |
| 205 | (negop->icversion_data[i].minor == fw_minor)) { |
| 206 | icframe_major = negop->icversion_data[i].major; |
| 207 | icframe_minor = negop->icversion_data[i].minor; |
| 208 | found_match = true; |
| 209 | } |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 210 | } |
K. Y. Srinivasan | a360530 | 2012-05-12 13:44:57 -0700 | [diff] [blame] | 211 | |
K. Y. Srinivasan | 6741335 | 2013-07-02 10:31:30 -0700 | [diff] [blame] | 212 | if (!found_match) |
| 213 | goto fw_error; |
| 214 | |
| 215 | found_match = false; |
| 216 | |
K. Y. Srinivasan | c836d0a | 2012-05-12 13:44:58 -0700 | [diff] [blame] | 217 | for (i = negop->icframe_vercnt; |
| 218 | (i < negop->icframe_vercnt + negop->icmsg_vercnt); i++) { |
K. Y. Srinivasan | 6741335 | 2013-07-02 10:31:30 -0700 | [diff] [blame] | 219 | if ((negop->icversion_data[i].major == srv_major) && |
| 220 | (negop->icversion_data[i].minor == srv_minor)) { |
| 221 | icmsg_major = negop->icversion_data[i].major; |
| 222 | icmsg_minor = negop->icversion_data[i].minor; |
| 223 | found_match = true; |
| 224 | } |
K. Y. Srinivasan | c836d0a | 2012-05-12 13:44:58 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | /* |
K. Y. Srinivasan | 6741335 | 2013-07-02 10:31:30 -0700 | [diff] [blame] | 228 | * Respond with the framework and service |
K. Y. Srinivasan | c836d0a | 2012-05-12 13:44:58 -0700 | [diff] [blame] | 229 | * version numbers we can support. |
| 230 | */ |
K. Y. Srinivasan | 6741335 | 2013-07-02 10:31:30 -0700 | [diff] [blame] | 231 | |
| 232 | fw_error: |
| 233 | if (!found_match) { |
| 234 | negop->icframe_vercnt = 0; |
| 235 | negop->icmsg_vercnt = 0; |
| 236 | } else { |
| 237 | negop->icframe_vercnt = 1; |
| 238 | negop->icmsg_vercnt = 1; |
| 239 | } |
| 240 | |
| 241 | negop->icversion_data[0].major = icframe_major; |
| 242 | negop->icversion_data[0].minor = icframe_minor; |
| 243 | negop->icversion_data[1].major = icmsg_major; |
| 244 | negop->icversion_data[1].minor = icmsg_minor; |
| 245 | return found_match; |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 246 | } |
K. Y. Srinivasan | a360530 | 2012-05-12 13:44:57 -0700 | [diff] [blame] | 247 | |
Greg Kroah-Hartman | da0e963 | 2011-10-11 08:42:28 -0600 | [diff] [blame] | 248 | EXPORT_SYMBOL_GPL(vmbus_prep_negotiate_resp); |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 249 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 250 | /* |
Haiyang Zhang | e98cb27 | 2010-10-15 10:14:07 -0700 | [diff] [blame] | 251 | * alloc_channel - Allocate and initialize a vmbus channel object |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 252 | */ |
Greg Kroah-Hartman | 50fe56d | 2010-10-20 15:51:57 -0700 | [diff] [blame] | 253 | static struct vmbus_channel *alloc_channel(void) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 254 | { |
Vitaly Kuznetsov | bc63b6f | 2015-02-27 11:25:52 -0800 | [diff] [blame] | 255 | static atomic_t chan_num = ATOMIC_INIT(0); |
Greg Kroah-Hartman | aded716 | 2009-08-18 15:21:19 -0700 | [diff] [blame] | 256 | struct vmbus_channel *channel; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 257 | |
Greg Kroah-Hartman | aded716 | 2009-08-18 15:21:19 -0700 | [diff] [blame] | 258 | channel = kzalloc(sizeof(*channel), GFP_ATOMIC); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 259 | if (!channel) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 260 | return NULL; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 261 | |
Vitaly Kuznetsov | bc63b6f | 2015-02-27 11:25:52 -0800 | [diff] [blame] | 262 | channel->id = atomic_inc_return(&chan_num); |
K. Y. Srinivasan | fe760e4 | 2016-01-27 22:29:45 -0800 | [diff] [blame] | 263 | channel->acquire_ring_lock = true; |
Greg Kroah-Hartman | 54411c4 | 2009-07-15 14:48:32 -0700 | [diff] [blame] | 264 | spin_lock_init(&channel->inbound_lock); |
Vitaly Kuznetsov | 67fae05 | 2015-01-20 16:45:05 +0100 | [diff] [blame] | 265 | spin_lock_init(&channel->lock); |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 266 | |
| 267 | INIT_LIST_HEAD(&channel->sc_list); |
K. Y. Srinivasan | 3a28fa3 | 2014-04-08 18:45:54 -0700 | [diff] [blame] | 268 | INIT_LIST_HEAD(&channel->percpu_list); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 269 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 270 | return channel; |
| 271 | } |
| 272 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 273 | /* |
Haiyang Zhang | e98cb27 | 2010-10-15 10:14:07 -0700 | [diff] [blame] | 274 | * free_channel - Release the resources used by the vmbus channel object |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 275 | */ |
Greg Kroah-Hartman | 9f3e28e | 2011-10-11 09:40:01 -0600 | [diff] [blame] | 276 | static void free_channel(struct vmbus_channel *channel) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 277 | { |
Dexuan Cui | aadc378 | 2015-03-27 09:10:10 -0700 | [diff] [blame] | 278 | kfree(channel); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 279 | } |
| 280 | |
K. Y. Srinivasan | 3a28fa3 | 2014-04-08 18:45:54 -0700 | [diff] [blame] | 281 | static void percpu_channel_enq(void *arg) |
| 282 | { |
| 283 | struct vmbus_channel *channel = arg; |
| 284 | int cpu = smp_processor_id(); |
Haiyang Zhang | 8b5d6d3 | 2010-05-28 23:22:44 +0000 | [diff] [blame] | 285 | |
K. Y. Srinivasan | 3a28fa3 | 2014-04-08 18:45:54 -0700 | [diff] [blame] | 286 | list_add_tail(&channel->percpu_list, &hv_context.percpu_list[cpu]); |
| 287 | } |
| 288 | |
| 289 | static void percpu_channel_deq(void *arg) |
| 290 | { |
| 291 | struct vmbus_channel *channel = arg; |
| 292 | |
| 293 | list_del(&channel->percpu_list); |
| 294 | } |
Haiyang Zhang | 8b5d6d3 | 2010-05-28 23:22:44 +0000 | [diff] [blame] | 295 | |
K. Y. Srinivasan | ed6cfcc | 2015-02-28 11:18:17 -0800 | [diff] [blame] | 296 | |
Dexuan Cui | f52078c | 2015-12-14 16:01:50 -0800 | [diff] [blame] | 297 | static void vmbus_release_relid(u32 relid) |
Timo Teräs | 4b2f9ab | 2010-12-15 20:48:09 +0200 | [diff] [blame] | 298 | { |
K. Y. Srinivasan | ed6cfcc | 2015-02-28 11:18:17 -0800 | [diff] [blame] | 299 | struct vmbus_channel_relid_released msg; |
Vitaly Kuznetsov | 04a258c | 2014-11-04 13:40:11 +0100 | [diff] [blame] | 300 | |
K. Y. Srinivasan | c870597 | 2013-03-15 12:25:44 -0700 | [diff] [blame] | 301 | memset(&msg, 0, sizeof(struct vmbus_channel_relid_released)); |
K. Y. Srinivasan | ed6cfcc | 2015-02-28 11:18:17 -0800 | [diff] [blame] | 302 | msg.child_relid = relid; |
K. Y. Srinivasan | c870597 | 2013-03-15 12:25:44 -0700 | [diff] [blame] | 303 | msg.header.msgtype = CHANNELMSG_RELID_RELEASED; |
| 304 | vmbus_post_msg(&msg, sizeof(struct vmbus_channel_relid_released)); |
Dexuan Cui | f52078c | 2015-12-14 16:01:50 -0800 | [diff] [blame] | 305 | } |
K. Y. Srinivasan | c870597 | 2013-03-15 12:25:44 -0700 | [diff] [blame] | 306 | |
Dexuan Cui | 638fea3 | 2016-06-09 18:47:24 -0700 | [diff] [blame^] | 307 | void hv_event_tasklet_disable(struct vmbus_channel *channel) |
| 308 | { |
| 309 | struct tasklet_struct *tasklet; |
| 310 | tasklet = hv_context.event_dpc[channel->target_cpu]; |
| 311 | tasklet_disable(tasklet); |
| 312 | } |
| 313 | |
| 314 | void hv_event_tasklet_enable(struct vmbus_channel *channel) |
| 315 | { |
| 316 | struct tasklet_struct *tasklet; |
| 317 | tasklet = hv_context.event_dpc[channel->target_cpu]; |
| 318 | tasklet_enable(tasklet); |
| 319 | |
| 320 | /* In case there is any pending event */ |
| 321 | tasklet_schedule(tasklet); |
| 322 | } |
| 323 | |
Dexuan Cui | f52078c | 2015-12-14 16:01:50 -0800 | [diff] [blame] | 324 | void hv_process_channel_removal(struct vmbus_channel *channel, u32 relid) |
| 325 | { |
| 326 | unsigned long flags; |
| 327 | struct vmbus_channel *primary_channel; |
| 328 | |
Dexuan Cui | 34c6801 | 2015-12-14 16:01:49 -0800 | [diff] [blame] | 329 | BUG_ON(!channel->rescind); |
Dexuan Cui | 85d9aa7 | 2016-01-27 22:29:43 -0800 | [diff] [blame] | 330 | BUG_ON(!mutex_is_locked(&vmbus_connection.channel_mutex)); |
Dexuan Cui | 34c6801 | 2015-12-14 16:01:49 -0800 | [diff] [blame] | 331 | |
Dexuan Cui | 638fea3 | 2016-06-09 18:47:24 -0700 | [diff] [blame^] | 332 | hv_event_tasklet_disable(channel); |
K. Y. Srinivasan | 2115b56 | 2014-08-28 18:29:53 -0700 | [diff] [blame] | 333 | if (channel->target_cpu != get_cpu()) { |
| 334 | put_cpu(); |
K. Y. Srinivasan | 3a28fa3 | 2014-04-08 18:45:54 -0700 | [diff] [blame] | 335 | smp_call_function_single(channel->target_cpu, |
| 336 | percpu_channel_deq, channel, true); |
K. Y. Srinivasan | 2115b56 | 2014-08-28 18:29:53 -0700 | [diff] [blame] | 337 | } else { |
K. Y. Srinivasan | 3a28fa3 | 2014-04-08 18:45:54 -0700 | [diff] [blame] | 338 | percpu_channel_deq(channel); |
K. Y. Srinivasan | 2115b56 | 2014-08-28 18:29:53 -0700 | [diff] [blame] | 339 | put_cpu(); |
| 340 | } |
Dexuan Cui | 638fea3 | 2016-06-09 18:47:24 -0700 | [diff] [blame^] | 341 | hv_event_tasklet_enable(channel); |
K. Y. Srinivasan | 3a28fa3 | 2014-04-08 18:45:54 -0700 | [diff] [blame] | 342 | |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 343 | if (channel->primary_channel == NULL) { |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 344 | list_del(&channel->listentry); |
Dexuan Cui | ca1c4b7 | 2015-08-13 17:07:03 -0700 | [diff] [blame] | 345 | |
| 346 | primary_channel = channel; |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 347 | } else { |
| 348 | primary_channel = channel->primary_channel; |
Vitaly Kuznetsov | 67fae05 | 2015-01-20 16:45:05 +0100 | [diff] [blame] | 349 | spin_lock_irqsave(&primary_channel->lock, flags); |
K. Y. Srinivasan | 565ce64 | 2013-10-16 19:27:19 -0700 | [diff] [blame] | 350 | list_del(&channel->sc_list); |
Vitaly Kuznetsov | 357e836 | 2015-05-06 17:47:44 -0700 | [diff] [blame] | 351 | primary_channel->num_sc--; |
Vitaly Kuznetsov | 67fae05 | 2015-01-20 16:45:05 +0100 | [diff] [blame] | 352 | spin_unlock_irqrestore(&primary_channel->lock, flags); |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 353 | } |
Dexuan Cui | ca1c4b7 | 2015-08-13 17:07:03 -0700 | [diff] [blame] | 354 | |
| 355 | /* |
| 356 | * We need to free the bit for init_vp_index() to work in the case |
| 357 | * of sub-channel, when we reload drivers like hv_netvsc. |
| 358 | */ |
| 359 | cpumask_clear_cpu(channel->target_cpu, |
| 360 | &primary_channel->alloced_cpus_in_node); |
| 361 | |
Dexuan Cui | 638fea3 | 2016-06-09 18:47:24 -0700 | [diff] [blame^] | 362 | vmbus_release_relid(relid); |
| 363 | |
K. Y. Srinivasan | c870597 | 2013-03-15 12:25:44 -0700 | [diff] [blame] | 364 | free_channel(channel); |
Timo Teräs | 4b2f9ab | 2010-12-15 20:48:09 +0200 | [diff] [blame] | 365 | } |
Haiyang Zhang | 8b5d6d3 | 2010-05-28 23:22:44 +0000 | [diff] [blame] | 366 | |
K. Y. Srinivasan | 93e5bd0 | 2011-12-12 09:29:17 -0800 | [diff] [blame] | 367 | void vmbus_free_channels(void) |
| 368 | { |
Dexuan Cui | 813c5b7 | 2015-04-22 21:31:30 -0700 | [diff] [blame] | 369 | struct vmbus_channel *channel, *tmp; |
K. Y. Srinivasan | 93e5bd0 | 2011-12-12 09:29:17 -0800 | [diff] [blame] | 370 | |
Dexuan Cui | 813c5b7 | 2015-04-22 21:31:30 -0700 | [diff] [blame] | 371 | list_for_each_entry_safe(channel, tmp, &vmbus_connection.chn_list, |
| 372 | listentry) { |
Dexuan Cui | 34c6801 | 2015-12-14 16:01:49 -0800 | [diff] [blame] | 373 | /* hv_process_channel_removal() needs this */ |
Dexuan Cui | 813c5b7 | 2015-04-22 21:31:30 -0700 | [diff] [blame] | 374 | channel->rescind = true; |
| 375 | |
K. Y. Srinivasan | 93e5bd0 | 2011-12-12 09:29:17 -0800 | [diff] [blame] | 376 | vmbus_device_unregister(channel->device_obj); |
K. Y. Srinivasan | 93e5bd0 | 2011-12-12 09:29:17 -0800 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 380 | /* |
Haiyang Zhang | e98cb27 | 2010-10-15 10:14:07 -0700 | [diff] [blame] | 381 | * vmbus_process_offer - Process the offer by creating a channel/device |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 382 | * associated with this offer |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 383 | */ |
K. Y. Srinivasan | 2dd37cb | 2015-02-28 11:18:18 -0800 | [diff] [blame] | 384 | static void vmbus_process_offer(struct vmbus_channel *newchannel) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 385 | { |
Greg Kroah-Hartman | aded716 | 2009-08-18 15:21:19 -0700 | [diff] [blame] | 386 | struct vmbus_channel *channel; |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 387 | bool fnew = true; |
Greg Kroah-Hartman | 0f5e44c | 2009-07-15 14:57:16 -0700 | [diff] [blame] | 388 | unsigned long flags; |
K. Y. Srinivasan | 7047f17 | 2015-12-25 20:00:30 -0800 | [diff] [blame] | 389 | u16 dev_type; |
Dexuan Cui | 85d9aa7 | 2016-01-27 22:29:43 -0800 | [diff] [blame] | 390 | int ret; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 391 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 392 | /* Make sure this is a new offer */ |
Dexuan Cui | d6f591e | 2015-12-14 16:01:51 -0800 | [diff] [blame] | 393 | mutex_lock(&vmbus_connection.channel_mutex); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 394 | |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 395 | list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) { |
K. Y. Srinivasan | 358d2ee | 2011-08-25 09:48:28 -0700 | [diff] [blame] | 396 | if (!uuid_le_cmp(channel->offermsg.offer.if_type, |
| 397 | newchannel->offermsg.offer.if_type) && |
| 398 | !uuid_le_cmp(channel->offermsg.offer.if_instance, |
| 399 | newchannel->offermsg.offer.if_instance)) { |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 400 | fnew = false; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 401 | break; |
| 402 | } |
| 403 | } |
| 404 | |
Vitaly Kuznetsov | 8dfd332 | 2015-05-06 17:47:42 -0700 | [diff] [blame] | 405 | if (fnew) |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 406 | list_add_tail(&newchannel->listentry, |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 407 | &vmbus_connection.chn_list); |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 408 | |
Dexuan Cui | d6f591e | 2015-12-14 16:01:51 -0800 | [diff] [blame] | 409 | mutex_unlock(&vmbus_connection.channel_mutex); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 410 | |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 411 | if (!fnew) { |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 412 | /* |
| 413 | * Check to see if this is a sub-channel. |
| 414 | */ |
| 415 | if (newchannel->offermsg.offer.sub_channel_index != 0) { |
| 416 | /* |
| 417 | * Process the sub-channel. |
| 418 | */ |
| 419 | newchannel->primary_channel = channel; |
Vitaly Kuznetsov | 67fae05 | 2015-01-20 16:45:05 +0100 | [diff] [blame] | 420 | spin_lock_irqsave(&channel->lock, flags); |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 421 | list_add_tail(&newchannel->sc_list, &channel->sc_list); |
K. Y. Srinivasan | a13e8bb | 2015-02-28 11:39:02 -0800 | [diff] [blame] | 422 | channel->num_sc++; |
Vitaly Kuznetsov | 357e836 | 2015-05-06 17:47:44 -0700 | [diff] [blame] | 423 | spin_unlock_irqrestore(&channel->lock, flags); |
Vitaly Kuznetsov | 8dfd332 | 2015-05-06 17:47:42 -0700 | [diff] [blame] | 424 | } else |
| 425 | goto err_free_chan; |
| 426 | } |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 427 | |
K. Y. Srinivasan | 7047f17 | 2015-12-25 20:00:30 -0800 | [diff] [blame] | 428 | dev_type = hv_get_dev_type(&newchannel->offermsg.offer.if_type); |
| 429 | |
| 430 | init_vp_index(newchannel, dev_type); |
Vitaly Kuznetsov | f38e7dd | 2015-05-06 17:47:45 -0700 | [diff] [blame] | 431 | |
Dexuan Cui | 638fea3 | 2016-06-09 18:47:24 -0700 | [diff] [blame^] | 432 | hv_event_tasklet_disable(newchannel); |
Vitaly Kuznetsov | 8dfd332 | 2015-05-06 17:47:42 -0700 | [diff] [blame] | 433 | if (newchannel->target_cpu != get_cpu()) { |
| 434 | put_cpu(); |
| 435 | smp_call_function_single(newchannel->target_cpu, |
| 436 | percpu_channel_enq, |
| 437 | newchannel, true); |
| 438 | } else { |
| 439 | percpu_channel_enq(newchannel); |
| 440 | put_cpu(); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 441 | } |
Dexuan Cui | 638fea3 | 2016-06-09 18:47:24 -0700 | [diff] [blame^] | 442 | hv_event_tasklet_enable(newchannel); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 443 | |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 444 | /* |
K. Y. Srinivasan | 42dceeb | 2013-08-26 14:08:58 -0700 | [diff] [blame] | 445 | * This state is used to indicate a successful open |
| 446 | * so that when we do close the channel normally, we |
| 447 | * can cleanup properly |
| 448 | */ |
| 449 | newchannel->state = CHANNEL_OPEN_STATE; |
| 450 | |
Vitaly Kuznetsov | 8dfd332 | 2015-05-06 17:47:42 -0700 | [diff] [blame] | 451 | if (!fnew) { |
| 452 | if (channel->sc_creation_callback != NULL) |
| 453 | channel->sc_creation_callback(newchannel); |
| 454 | return; |
| 455 | } |
| 456 | |
K. Y. Srinivasan | 42dceeb | 2013-08-26 14:08:58 -0700 | [diff] [blame] | 457 | /* |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 458 | * Start the process of binding this offer to the driver |
| 459 | * We need to set the DeviceObject field before calling |
Haiyang Zhang | 646f1ea | 2011-01-26 12:12:10 -0800 | [diff] [blame] | 460 | * vmbus_child_dev_add() |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 461 | */ |
K. Y. Srinivasan | f2c7301 | 2011-09-08 07:24:12 -0700 | [diff] [blame] | 462 | newchannel->device_obj = vmbus_device_create( |
Haiyang Zhang | 767dff6 | 2011-01-26 12:12:12 -0800 | [diff] [blame] | 463 | &newchannel->offermsg.offer.if_type, |
| 464 | &newchannel->offermsg.offer.if_instance, |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 465 | newchannel); |
Vitaly Kuznetsov | 9c3a6f7 | 2015-01-20 16:45:04 +0100 | [diff] [blame] | 466 | if (!newchannel->device_obj) |
K. Y. Srinivasan | 5b1e5b5 | 2015-02-28 11:18:19 -0800 | [diff] [blame] | 467 | goto err_deq_chan; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 468 | |
K. Y. Srinivasan | 7047f17 | 2015-12-25 20:00:30 -0800 | [diff] [blame] | 469 | newchannel->device_obj->device_id = dev_type; |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 470 | /* |
| 471 | * Add the new device to the bus. This will kick off device-driver |
| 472 | * binding which eventually invokes the device driver's AddDevice() |
| 473 | * method. |
| 474 | */ |
Dexuan Cui | 85d9aa7 | 2016-01-27 22:29:43 -0800 | [diff] [blame] | 475 | mutex_lock(&vmbus_connection.channel_mutex); |
| 476 | ret = vmbus_device_register(newchannel->device_obj); |
| 477 | mutex_unlock(&vmbus_connection.channel_mutex); |
| 478 | |
| 479 | if (ret != 0) { |
Dexuan Cui | d43e2fe | 2015-03-27 09:10:09 -0700 | [diff] [blame] | 480 | pr_err("unable to add child device object (relid %d)\n", |
| 481 | newchannel->offermsg.child_relid); |
| 482 | kfree(newchannel->device_obj); |
| 483 | goto err_deq_chan; |
| 484 | } |
Vitaly Kuznetsov | 9c3a6f7 | 2015-01-20 16:45:04 +0100 | [diff] [blame] | 485 | return; |
K. Y. Srinivasan | 2dd37cb | 2015-02-28 11:18:18 -0800 | [diff] [blame] | 486 | |
K. Y. Srinivasan | 5b1e5b5 | 2015-02-28 11:18:19 -0800 | [diff] [blame] | 487 | err_deq_chan: |
Dexuan Cui | d6f591e | 2015-12-14 16:01:51 -0800 | [diff] [blame] | 488 | mutex_lock(&vmbus_connection.channel_mutex); |
K. Y. Srinivasan | 5b1e5b5 | 2015-02-28 11:18:19 -0800 | [diff] [blame] | 489 | list_del(&newchannel->listentry); |
Dexuan Cui | d6f591e | 2015-12-14 16:01:51 -0800 | [diff] [blame] | 490 | mutex_unlock(&vmbus_connection.channel_mutex); |
K. Y. Srinivasan | 5b1e5b5 | 2015-02-28 11:18:19 -0800 | [diff] [blame] | 491 | |
Dexuan Cui | 638fea3 | 2016-06-09 18:47:24 -0700 | [diff] [blame^] | 492 | hv_event_tasklet_disable(newchannel); |
K. Y. Srinivasan | 5b1e5b5 | 2015-02-28 11:18:19 -0800 | [diff] [blame] | 493 | if (newchannel->target_cpu != get_cpu()) { |
| 494 | put_cpu(); |
| 495 | smp_call_function_single(newchannel->target_cpu, |
| 496 | percpu_channel_deq, newchannel, true); |
| 497 | } else { |
| 498 | percpu_channel_deq(newchannel); |
| 499 | put_cpu(); |
| 500 | } |
Dexuan Cui | 638fea3 | 2016-06-09 18:47:24 -0700 | [diff] [blame^] | 501 | hv_event_tasklet_enable(newchannel); |
| 502 | |
| 503 | vmbus_release_relid(newchannel->offermsg.child_relid); |
K. Y. Srinivasan | 5b1e5b5 | 2015-02-28 11:18:19 -0800 | [diff] [blame] | 504 | |
Vitaly Kuznetsov | 9c3a6f7 | 2015-01-20 16:45:04 +0100 | [diff] [blame] | 505 | err_free_chan: |
| 506 | free_channel(newchannel); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 507 | } |
| 508 | |
K. Y. Srinivasan | a119845 | 2012-12-01 06:46:50 -0800 | [diff] [blame] | 509 | /* |
| 510 | * We use this state to statically distribute the channel interrupt load. |
| 511 | */ |
K. Y. Srinivasan | 1f656ff | 2015-05-30 23:37:48 -0700 | [diff] [blame] | 512 | static int next_numa_node_id; |
K. Y. Srinivasan | a119845 | 2012-12-01 06:46:50 -0800 | [diff] [blame] | 513 | |
| 514 | /* |
| 515 | * Starting with Win8, we can statically distribute the incoming |
K. Y. Srinivasan | 1f656ff | 2015-05-30 23:37:48 -0700 | [diff] [blame] | 516 | * channel interrupt load by binding a channel to VCPU. |
| 517 | * We do this in a hierarchical fashion: |
| 518 | * First distribute the primary channels across available NUMA nodes |
| 519 | * and then distribute the subchannels amongst the CPUs in the NUMA |
| 520 | * node assigned to the primary channel. |
| 521 | * |
| 522 | * For pre-win8 hosts or non-performance critical channels we assign the |
| 523 | * first CPU in the first NUMA node. |
K. Y. Srinivasan | a119845 | 2012-12-01 06:46:50 -0800 | [diff] [blame] | 524 | */ |
K. Y. Srinivasan | 7047f17 | 2015-12-25 20:00:30 -0800 | [diff] [blame] | 525 | 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] | 526 | { |
| 527 | u32 cur_cpu; |
K. Y. Srinivasan | 7047f17 | 2015-12-25 20:00:30 -0800 | [diff] [blame] | 528 | bool perf_chn = vmbus_devs[dev_type].perf_device; |
K. Y. Srinivasan | 1f656ff | 2015-05-30 23:37:48 -0700 | [diff] [blame] | 529 | struct vmbus_channel *primary = channel->primary_channel; |
| 530 | int next_node; |
| 531 | struct cpumask available_mask; |
K. Y. Srinivasan | 9f01ec5 | 2015-08-05 00:52:38 -0700 | [diff] [blame] | 532 | struct cpumask *alloced_mask; |
K. Y. Srinivasan | a119845 | 2012-12-01 06:46:50 -0800 | [diff] [blame] | 533 | |
K. Y. Srinivasan | a119845 | 2012-12-01 06:46:50 -0800 | [diff] [blame] | 534 | if ((vmbus_proto_version == VERSION_WS2008) || |
| 535 | (vmbus_proto_version == VERSION_WIN7) || (!perf_chn)) { |
| 536 | /* |
| 537 | * Prior to win8, all channel interrupts are |
| 538 | * delivered on cpu 0. |
| 539 | * Also if the channel is not a performance critical |
| 540 | * channel, bind it to cpu 0. |
| 541 | */ |
K. Y. Srinivasan | 1f656ff | 2015-05-30 23:37:48 -0700 | [diff] [blame] | 542 | channel->numa_node = 0; |
K. Y. Srinivasan | d3ba720 | 2014-04-08 18:45:53 -0700 | [diff] [blame] | 543 | channel->target_cpu = 0; |
K. Y. Srinivasan | 9c6e64a | 2015-05-30 23:37:47 -0700 | [diff] [blame] | 544 | channel->target_vp = hv_context.vp_index[0]; |
K. Y. Srinivasan | d3ba720 | 2014-04-08 18:45:53 -0700 | [diff] [blame] | 545 | return; |
K. Y. Srinivasan | a119845 | 2012-12-01 06:46:50 -0800 | [diff] [blame] | 546 | } |
Vitaly Kuznetsov | ce59fec | 2015-05-06 17:47:46 -0700 | [diff] [blame] | 547 | |
| 548 | /* |
K. Y. Srinivasan | 1f656ff | 2015-05-30 23:37:48 -0700 | [diff] [blame] | 549 | * We distribute primary channels evenly across all the available |
| 550 | * NUMA nodes and within the assigned NUMA node we will assign the |
| 551 | * first available CPU to the primary channel. |
| 552 | * The sub-channels will be assigned to the CPUs available in the |
| 553 | * NUMA node evenly. |
Vitaly Kuznetsov | ce59fec | 2015-05-06 17:47:46 -0700 | [diff] [blame] | 554 | */ |
K. Y. Srinivasan | 1f656ff | 2015-05-30 23:37:48 -0700 | [diff] [blame] | 555 | if (!primary) { |
| 556 | while (true) { |
| 557 | next_node = next_numa_node_id++; |
| 558 | if (next_node == nr_node_ids) |
| 559 | next_node = next_numa_node_id = 0; |
| 560 | if (cpumask_empty(cpumask_of_node(next_node))) |
| 561 | continue; |
| 562 | break; |
Vitaly Kuznetsov | ce59fec | 2015-05-06 17:47:46 -0700 | [diff] [blame] | 563 | } |
K. Y. Srinivasan | 1f656ff | 2015-05-30 23:37:48 -0700 | [diff] [blame] | 564 | channel->numa_node = next_node; |
| 565 | primary = channel; |
Vitaly Kuznetsov | ce59fec | 2015-05-06 17:47:46 -0700 | [diff] [blame] | 566 | } |
K. Y. Srinivasan | 9f01ec5 | 2015-08-05 00:52:38 -0700 | [diff] [blame] | 567 | alloced_mask = &hv_context.hv_numa_map[primary->numa_node]; |
Vitaly Kuznetsov | ce59fec | 2015-05-06 17:47:46 -0700 | [diff] [blame] | 568 | |
K. Y. Srinivasan | 9f01ec5 | 2015-08-05 00:52:38 -0700 | [diff] [blame] | 569 | if (cpumask_weight(alloced_mask) == |
K. Y. Srinivasan | 1f656ff | 2015-05-30 23:37:48 -0700 | [diff] [blame] | 570 | cpumask_weight(cpumask_of_node(primary->numa_node))) { |
| 571 | /* |
| 572 | * We have cycled through all the CPUs in the node; |
| 573 | * reset the alloced map. |
| 574 | */ |
K. Y. Srinivasan | 9f01ec5 | 2015-08-05 00:52:38 -0700 | [diff] [blame] | 575 | cpumask_clear(alloced_mask); |
K. Y. Srinivasan | 1f656ff | 2015-05-30 23:37:48 -0700 | [diff] [blame] | 576 | } |
| 577 | |
K. Y. Srinivasan | 9f01ec5 | 2015-08-05 00:52:38 -0700 | [diff] [blame] | 578 | cpumask_xor(&available_mask, alloced_mask, |
K. Y. Srinivasan | 1f656ff | 2015-05-30 23:37:48 -0700 | [diff] [blame] | 579 | cpumask_of_node(primary->numa_node)); |
| 580 | |
Dexuan Cui | 3b71107 | 2015-08-05 00:52:39 -0700 | [diff] [blame] | 581 | cur_cpu = -1; |
Vitaly Kuznetsov | 79fd8e7 | 2016-01-27 22:29:34 -0800 | [diff] [blame] | 582 | |
| 583 | /* |
| 584 | * Normally Hyper-V host doesn't create more subchannels than there |
| 585 | * are VCPUs on the node but it is possible when not all present VCPUs |
| 586 | * on the node are initialized by guest. Clear the alloced_cpus_in_node |
| 587 | * to start over. |
| 588 | */ |
| 589 | if (cpumask_equal(&primary->alloced_cpus_in_node, |
| 590 | cpumask_of_node(primary->numa_node))) |
| 591 | cpumask_clear(&primary->alloced_cpus_in_node); |
| 592 | |
Dexuan Cui | 3b71107 | 2015-08-05 00:52:39 -0700 | [diff] [blame] | 593 | while (true) { |
| 594 | cur_cpu = cpumask_next(cur_cpu, &available_mask); |
| 595 | if (cur_cpu >= nr_cpu_ids) { |
| 596 | cur_cpu = -1; |
| 597 | cpumask_copy(&available_mask, |
| 598 | cpumask_of_node(primary->numa_node)); |
| 599 | continue; |
| 600 | } |
| 601 | |
Dexuan Cui | ca1c4b7 | 2015-08-13 17:07:03 -0700 | [diff] [blame] | 602 | /* |
| 603 | * NOTE: in the case of sub-channel, we clear the sub-channel |
| 604 | * related bit(s) in primary->alloced_cpus_in_node in |
| 605 | * hv_process_channel_removal(), so when we reload drivers |
| 606 | * like hv_netvsc in SMP guest, here we're able to re-allocate |
| 607 | * bit from primary->alloced_cpus_in_node. |
| 608 | */ |
Dexuan Cui | 3b71107 | 2015-08-05 00:52:39 -0700 | [diff] [blame] | 609 | if (!cpumask_test_cpu(cur_cpu, |
| 610 | &primary->alloced_cpus_in_node)) { |
| 611 | cpumask_set_cpu(cur_cpu, |
| 612 | &primary->alloced_cpus_in_node); |
| 613 | cpumask_set_cpu(cur_cpu, alloced_mask); |
| 614 | break; |
| 615 | } |
| 616 | } |
K. Y. Srinivasan | 1f656ff | 2015-05-30 23:37:48 -0700 | [diff] [blame] | 617 | |
K. Y. Srinivasan | d3ba720 | 2014-04-08 18:45:53 -0700 | [diff] [blame] | 618 | channel->target_cpu = cur_cpu; |
| 619 | channel->target_vp = hv_context.vp_index[cur_cpu]; |
K. Y. Srinivasan | a119845 | 2012-12-01 06:46:50 -0800 | [diff] [blame] | 620 | } |
| 621 | |
Vitaly Kuznetsov | 4157191 | 2016-01-27 22:29:35 -0800 | [diff] [blame] | 622 | static void vmbus_wait_for_unload(void) |
| 623 | { |
Vitaly Kuznetsov | cd95aad | 2016-04-30 19:21:34 -0700 | [diff] [blame] | 624 | int cpu; |
| 625 | void *page_addr; |
| 626 | struct hv_message *msg; |
Vitaly Kuznetsov | 4157191 | 2016-01-27 22:29:35 -0800 | [diff] [blame] | 627 | struct vmbus_channel_message_header *hdr; |
Vitaly Kuznetsov | cd95aad | 2016-04-30 19:21:34 -0700 | [diff] [blame] | 628 | u32 message_type; |
Vitaly Kuznetsov | 4157191 | 2016-01-27 22:29:35 -0800 | [diff] [blame] | 629 | |
Vitaly Kuznetsov | cd95aad | 2016-04-30 19:21:34 -0700 | [diff] [blame] | 630 | /* |
| 631 | * CHANNELMSG_UNLOAD_RESPONSE is always delivered to the CPU which was |
| 632 | * used for initial contact or to CPU0 depending on host version. When |
| 633 | * we're crashing on a different CPU let's hope that IRQ handler on |
| 634 | * the cpu which receives CHANNELMSG_UNLOAD_RESPONSE is still |
| 635 | * functional and vmbus_unload_response() will complete |
| 636 | * vmbus_connection.unload_event. If not, the last thing we can do is |
| 637 | * read message pages for all CPUs directly. |
| 638 | */ |
Vitaly Kuznetsov | 4157191 | 2016-01-27 22:29:35 -0800 | [diff] [blame] | 639 | while (1) { |
Vitaly Kuznetsov | cd95aad | 2016-04-30 19:21:34 -0700 | [diff] [blame] | 640 | if (completion_done(&vmbus_connection.unload_event)) |
| 641 | break; |
| 642 | |
| 643 | for_each_online_cpu(cpu) { |
| 644 | page_addr = hv_context.synic_message_page[cpu]; |
| 645 | msg = (struct hv_message *)page_addr + |
| 646 | VMBUS_MESSAGE_SINT; |
| 647 | |
| 648 | message_type = READ_ONCE(msg->header.message_type); |
| 649 | if (message_type == HVMSG_NONE) |
| 650 | continue; |
| 651 | |
| 652 | hdr = (struct vmbus_channel_message_header *) |
| 653 | msg->u.payload; |
| 654 | |
| 655 | if (hdr->msgtype == CHANNELMSG_UNLOAD_RESPONSE) |
| 656 | complete(&vmbus_connection.unload_event); |
| 657 | |
| 658 | vmbus_signal_eom(msg, message_type); |
Vitaly Kuznetsov | 4157191 | 2016-01-27 22:29:35 -0800 | [diff] [blame] | 659 | } |
| 660 | |
Vitaly Kuznetsov | cd95aad | 2016-04-30 19:21:34 -0700 | [diff] [blame] | 661 | mdelay(10); |
| 662 | } |
Vitaly Kuznetsov | 4157191 | 2016-01-27 22:29:35 -0800 | [diff] [blame] | 663 | |
Vitaly Kuznetsov | cd95aad | 2016-04-30 19:21:34 -0700 | [diff] [blame] | 664 | /* |
| 665 | * We're crashing and already got the UNLOAD_RESPONSE, cleanup all |
| 666 | * maybe-pending messages on all CPUs to be able to receive new |
| 667 | * messages after we reconnect. |
| 668 | */ |
| 669 | for_each_online_cpu(cpu) { |
| 670 | page_addr = hv_context.synic_message_page[cpu]; |
| 671 | msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT; |
| 672 | msg->header.message_type = HVMSG_NONE; |
Vitaly Kuznetsov | 4157191 | 2016-01-27 22:29:35 -0800 | [diff] [blame] | 673 | } |
| 674 | } |
| 675 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 676 | /* |
K. Y. Srinivasan | 2db84ef | 2015-04-22 21:31:32 -0700 | [diff] [blame] | 677 | * vmbus_unload_response - Handler for the unload response. |
| 678 | */ |
| 679 | static void vmbus_unload_response(struct vmbus_channel_message_header *hdr) |
| 680 | { |
| 681 | /* |
| 682 | * This is a global event; just wakeup the waiting thread. |
| 683 | * Once we successfully unload, we can cleanup the monitor state. |
| 684 | */ |
| 685 | complete(&vmbus_connection.unload_event); |
| 686 | } |
| 687 | |
Vitaly Kuznetsov | 75ff3a8 | 2016-02-26 15:13:16 -0800 | [diff] [blame] | 688 | void vmbus_initiate_unload(bool crash) |
K. Y. Srinivasan | 2db84ef | 2015-04-22 21:31:32 -0700 | [diff] [blame] | 689 | { |
| 690 | struct vmbus_channel_message_header hdr; |
| 691 | |
Vitaly Kuznetsov | 4a54243 | 2015-08-01 16:08:19 -0700 | [diff] [blame] | 692 | /* Pre-Win2012R2 hosts don't support reconnect */ |
| 693 | if (vmbus_proto_version < VERSION_WIN8_1) |
| 694 | return; |
| 695 | |
K. Y. Srinivasan | 2db84ef | 2015-04-22 21:31:32 -0700 | [diff] [blame] | 696 | init_completion(&vmbus_connection.unload_event); |
| 697 | memset(&hdr, 0, sizeof(struct vmbus_channel_message_header)); |
| 698 | hdr.msgtype = CHANNELMSG_UNLOAD; |
| 699 | vmbus_post_msg(&hdr, sizeof(struct vmbus_channel_message_header)); |
| 700 | |
Vitaly Kuznetsov | 4157191 | 2016-01-27 22:29:35 -0800 | [diff] [blame] | 701 | /* |
| 702 | * vmbus_initiate_unload() is also called on crash and the crash can be |
| 703 | * happening in an interrupt context, where scheduling is impossible. |
| 704 | */ |
Vitaly Kuznetsov | 75ff3a8 | 2016-02-26 15:13:16 -0800 | [diff] [blame] | 705 | if (!crash) |
Vitaly Kuznetsov | 4157191 | 2016-01-27 22:29:35 -0800 | [diff] [blame] | 706 | wait_for_completion(&vmbus_connection.unload_event); |
| 707 | else |
| 708 | vmbus_wait_for_unload(); |
K. Y. Srinivasan | 2db84ef | 2015-04-22 21:31:32 -0700 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | /* |
Haiyang Zhang | e98cb27 | 2010-10-15 10:14:07 -0700 | [diff] [blame] | 712 | * vmbus_onoffer - Handler for channel offers from vmbus in parent partition. |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 713 | * |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 714 | */ |
Haiyang Zhang | e98cb27 | 2010-10-15 10:14:07 -0700 | [diff] [blame] | 715 | static void vmbus_onoffer(struct vmbus_channel_message_header *hdr) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 716 | { |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 717 | struct vmbus_channel_offer_channel *offer; |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 718 | struct vmbus_channel *newchannel; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 719 | |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 720 | offer = (struct vmbus_channel_offer_channel *)hdr; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 721 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 722 | /* Allocate the channel object and save this offer. */ |
Haiyang Zhang | e98cb27 | 2010-10-15 10:14:07 -0700 | [diff] [blame] | 723 | newchannel = alloc_channel(); |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 724 | if (!newchannel) { |
Hank Janssen | 0a46618 | 2011-03-29 13:58:47 -0700 | [diff] [blame] | 725 | pr_err("Unable to allocate channel object\n"); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 726 | return; |
| 727 | } |
| 728 | |
K. Y. Srinivasan | 132368b | 2012-12-01 06:46:33 -0800 | [diff] [blame] | 729 | /* |
| 730 | * By default we setup state to enable batched |
| 731 | * reading. A specific service can choose to |
| 732 | * disable this prior to opening the channel. |
| 733 | */ |
| 734 | newchannel->batched_reading = true; |
| 735 | |
K. Y. Srinivasan | b3bf60c | 2012-12-01 06:46:45 -0800 | [diff] [blame] | 736 | /* |
| 737 | * Setup state for signalling the host. |
| 738 | */ |
| 739 | newchannel->sig_event = (struct hv_input_signal_event *) |
| 740 | (ALIGN((unsigned long) |
| 741 | &newchannel->sig_buf, |
| 742 | HV_HYPERCALL_PARAM_ALIGN)); |
| 743 | |
| 744 | newchannel->sig_event->connectionid.asu32 = 0; |
| 745 | newchannel->sig_event->connectionid.u.id = VMBUS_EVENT_CONNECTION_ID; |
| 746 | newchannel->sig_event->flag_number = 0; |
| 747 | newchannel->sig_event->rsvdz = 0; |
| 748 | |
| 749 | if (vmbus_proto_version != VERSION_WS2008) { |
| 750 | newchannel->is_dedicated_interrupt = |
| 751 | (offer->is_dedicated_interrupt != 0); |
| 752 | newchannel->sig_event->connectionid.u.id = |
| 753 | offer->connection_id; |
| 754 | } |
| 755 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 756 | memcpy(&newchannel->offermsg, offer, |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 757 | sizeof(struct vmbus_channel_offer_channel)); |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 758 | newchannel->monitor_grp = (u8)offer->monitorid / 32; |
| 759 | newchannel->monitor_bit = (u8)offer->monitorid % 32; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 760 | |
K. Y. Srinivasan | 2dd37cb | 2015-02-28 11:18:18 -0800 | [diff] [blame] | 761 | vmbus_process_offer(newchannel); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 762 | } |
| 763 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 764 | /* |
Haiyang Zhang | e98cb27 | 2010-10-15 10:14:07 -0700 | [diff] [blame] | 765 | * vmbus_onoffer_rescind - Rescind offer handler. |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 766 | * |
| 767 | * We queue a work item to process this offer synchronously |
| 768 | */ |
Haiyang Zhang | e98cb27 | 2010-10-15 10:14:07 -0700 | [diff] [blame] | 769 | static void vmbus_onoffer_rescind(struct vmbus_channel_message_header *hdr) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 770 | { |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 771 | struct vmbus_channel_rescind_offer *rescind; |
Greg Kroah-Hartman | aded716 | 2009-08-18 15:21:19 -0700 | [diff] [blame] | 772 | struct vmbus_channel *channel; |
Dexuan Cui | d43e2fe | 2015-03-27 09:10:09 -0700 | [diff] [blame] | 773 | unsigned long flags; |
| 774 | struct device *dev; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 775 | |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 776 | rescind = (struct vmbus_channel_rescind_offer *)hdr; |
Dexuan Cui | 85d9aa7 | 2016-01-27 22:29:43 -0800 | [diff] [blame] | 777 | |
| 778 | mutex_lock(&vmbus_connection.channel_mutex); |
Dexuan Cui | d43e2fe | 2015-03-27 09:10:09 -0700 | [diff] [blame] | 779 | channel = relid2channel(rescind->child_relid); |
Hank Janssen | 98e0870 | 2011-03-29 13:58:44 -0700 | [diff] [blame] | 780 | |
K. Y. Srinivasan | 2dd37cb | 2015-02-28 11:18:18 -0800 | [diff] [blame] | 781 | if (channel == NULL) { |
Dexuan Cui | f52078c | 2015-12-14 16:01:50 -0800 | [diff] [blame] | 782 | /* |
| 783 | * This is very impossible, because in |
| 784 | * vmbus_process_offer(), we have already invoked |
| 785 | * vmbus_release_relid() on error. |
| 786 | */ |
Dexuan Cui | 85d9aa7 | 2016-01-27 22:29:43 -0800 | [diff] [blame] | 787 | goto out; |
K. Y. Srinivasan | 2dd37cb | 2015-02-28 11:18:18 -0800 | [diff] [blame] | 788 | } |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 789 | |
Dexuan Cui | d43e2fe | 2015-03-27 09:10:09 -0700 | [diff] [blame] | 790 | spin_lock_irqsave(&channel->lock, flags); |
| 791 | channel->rescind = true; |
| 792 | spin_unlock_irqrestore(&channel->lock, flags); |
| 793 | |
| 794 | if (channel->device_obj) { |
Dexuan Cui | 499e840 | 2016-01-27 22:29:42 -0800 | [diff] [blame] | 795 | if (channel->chn_rescind_callback) { |
| 796 | channel->chn_rescind_callback(channel); |
Dexuan Cui | 85d9aa7 | 2016-01-27 22:29:43 -0800 | [diff] [blame] | 797 | goto out; |
Dexuan Cui | 499e840 | 2016-01-27 22:29:42 -0800 | [diff] [blame] | 798 | } |
Dexuan Cui | d43e2fe | 2015-03-27 09:10:09 -0700 | [diff] [blame] | 799 | /* |
| 800 | * We will have to unregister this device from the |
| 801 | * driver core. |
| 802 | */ |
| 803 | dev = get_device(&channel->device_obj->device); |
| 804 | if (dev) { |
| 805 | vmbus_device_unregister(channel->device_obj); |
| 806 | put_device(dev); |
| 807 | } |
| 808 | } else { |
| 809 | hv_process_channel_removal(channel, |
| 810 | channel->offermsg.child_relid); |
K. Y. Srinivasan | 2dd37cb | 2015-02-28 11:18:18 -0800 | [diff] [blame] | 811 | } |
Dexuan Cui | 85d9aa7 | 2016-01-27 22:29:43 -0800 | [diff] [blame] | 812 | |
| 813 | out: |
| 814 | mutex_unlock(&vmbus_connection.channel_mutex); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 815 | } |
| 816 | |
Dexuan Cui | 85d9aa7 | 2016-01-27 22:29:43 -0800 | [diff] [blame] | 817 | void vmbus_hvsock_device_unregister(struct vmbus_channel *channel) |
| 818 | { |
| 819 | mutex_lock(&vmbus_connection.channel_mutex); |
| 820 | |
| 821 | BUG_ON(!is_hvsock_channel(channel)); |
| 822 | |
| 823 | channel->rescind = true; |
| 824 | vmbus_device_unregister(channel->device_obj); |
| 825 | |
| 826 | mutex_unlock(&vmbus_connection.channel_mutex); |
| 827 | } |
| 828 | EXPORT_SYMBOL_GPL(vmbus_hvsock_device_unregister); |
| 829 | |
| 830 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 831 | /* |
Haiyang Zhang | e98cb27 | 2010-10-15 10:14:07 -0700 | [diff] [blame] | 832 | * vmbus_onoffers_delivered - |
| 833 | * This is invoked when all offers have been delivered. |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 834 | * |
| 835 | * Nothing to do here. |
| 836 | */ |
Haiyang Zhang | e98cb27 | 2010-10-15 10:14:07 -0700 | [diff] [blame] | 837 | static void vmbus_onoffers_delivered( |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 838 | struct vmbus_channel_message_header *hdr) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 839 | { |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 840 | } |
| 841 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 842 | /* |
Haiyang Zhang | e98cb27 | 2010-10-15 10:14:07 -0700 | [diff] [blame] | 843 | * vmbus_onopen_result - Open result handler. |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 844 | * |
| 845 | * This is invoked when we received a response to our channel open request. |
| 846 | * Find the matching request, copy the response and signal the requesting |
| 847 | * thread. |
| 848 | */ |
Haiyang Zhang | e98cb27 | 2010-10-15 10:14:07 -0700 | [diff] [blame] | 849 | static void vmbus_onopen_result(struct vmbus_channel_message_header *hdr) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 850 | { |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 851 | struct vmbus_channel_open_result *result; |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 852 | struct vmbus_channel_msginfo *msginfo; |
| 853 | struct vmbus_channel_message_header *requestheader; |
| 854 | struct vmbus_channel_open_channel *openmsg; |
Greg Kroah-Hartman | dd0813b | 2009-07-15 14:56:45 -0700 | [diff] [blame] | 855 | unsigned long flags; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 856 | |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 857 | result = (struct vmbus_channel_open_result *)hdr; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 858 | |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 859 | /* |
| 860 | * Find the open msg, copy the result and signal/unblock the wait event |
| 861 | */ |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 862 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 863 | |
Hank Janssen | ebb61e5 | 2011-02-18 12:39:57 -0800 | [diff] [blame] | 864 | list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list, |
| 865 | msglistentry) { |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 866 | requestheader = |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 867 | (struct vmbus_channel_message_header *)msginfo->msg; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 868 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 869 | if (requestheader->msgtype == CHANNELMSG_OPENCHANNEL) { |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 870 | openmsg = |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 871 | (struct vmbus_channel_open_channel *)msginfo->msg; |
| 872 | if (openmsg->child_relid == result->child_relid && |
| 873 | openmsg->openid == result->openid) { |
| 874 | memcpy(&msginfo->response.open_result, |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 875 | result, |
K. Y. Srinivasan | 9568a19 | 2011-05-10 07:55:39 -0700 | [diff] [blame] | 876 | sizeof( |
| 877 | struct vmbus_channel_open_result)); |
| 878 | complete(&msginfo->waitevent); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 879 | break; |
| 880 | } |
| 881 | } |
| 882 | } |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 883 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 884 | } |
| 885 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 886 | /* |
Haiyang Zhang | e98cb27 | 2010-10-15 10:14:07 -0700 | [diff] [blame] | 887 | * vmbus_ongpadl_created - GPADL created handler. |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 888 | * |
| 889 | * This is invoked when we received a response to our gpadl create request. |
| 890 | * Find the matching request, copy the response and signal the requesting |
| 891 | * thread. |
| 892 | */ |
Haiyang Zhang | e98cb27 | 2010-10-15 10:14:07 -0700 | [diff] [blame] | 893 | static void vmbus_ongpadl_created(struct vmbus_channel_message_header *hdr) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 894 | { |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 895 | struct vmbus_channel_gpadl_created *gpadlcreated; |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 896 | struct vmbus_channel_msginfo *msginfo; |
| 897 | struct vmbus_channel_message_header *requestheader; |
| 898 | struct vmbus_channel_gpadl_header *gpadlheader; |
Greg Kroah-Hartman | dd0813b | 2009-07-15 14:56:45 -0700 | [diff] [blame] | 899 | unsigned long flags; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 900 | |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 901 | gpadlcreated = (struct vmbus_channel_gpadl_created *)hdr; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 902 | |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 903 | /* |
| 904 | * Find the establish msg, copy the result and signal/unblock the wait |
| 905 | * event |
| 906 | */ |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 907 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 908 | |
Hank Janssen | ebb61e5 | 2011-02-18 12:39:57 -0800 | [diff] [blame] | 909 | list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list, |
| 910 | msglistentry) { |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 911 | requestheader = |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 912 | (struct vmbus_channel_message_header *)msginfo->msg; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 913 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 914 | if (requestheader->msgtype == CHANNELMSG_GPADL_HEADER) { |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 915 | gpadlheader = |
| 916 | (struct vmbus_channel_gpadl_header *)requestheader; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 917 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 918 | if ((gpadlcreated->child_relid == |
| 919 | gpadlheader->child_relid) && |
| 920 | (gpadlcreated->gpadl == gpadlheader->gpadl)) { |
| 921 | memcpy(&msginfo->response.gpadl_created, |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 922 | gpadlcreated, |
K. Y. Srinivasan | 9568a19 | 2011-05-10 07:55:39 -0700 | [diff] [blame] | 923 | sizeof( |
| 924 | struct vmbus_channel_gpadl_created)); |
| 925 | complete(&msginfo->waitevent); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 926 | break; |
| 927 | } |
| 928 | } |
| 929 | } |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 930 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 931 | } |
| 932 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 933 | /* |
Haiyang Zhang | e98cb27 | 2010-10-15 10:14:07 -0700 | [diff] [blame] | 934 | * vmbus_ongpadl_torndown - GPADL torndown handler. |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 935 | * |
| 936 | * This is invoked when we received a response to our gpadl teardown request. |
| 937 | * Find the matching request, copy the response and signal the requesting |
| 938 | * thread. |
| 939 | */ |
Haiyang Zhang | e98cb27 | 2010-10-15 10:14:07 -0700 | [diff] [blame] | 940 | static void vmbus_ongpadl_torndown( |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 941 | struct vmbus_channel_message_header *hdr) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 942 | { |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 943 | struct vmbus_channel_gpadl_torndown *gpadl_torndown; |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 944 | struct vmbus_channel_msginfo *msginfo; |
| 945 | struct vmbus_channel_message_header *requestheader; |
| 946 | struct vmbus_channel_gpadl_teardown *gpadl_teardown; |
Greg Kroah-Hartman | dd0813b | 2009-07-15 14:56:45 -0700 | [diff] [blame] | 947 | unsigned long flags; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 948 | |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 949 | gpadl_torndown = (struct vmbus_channel_gpadl_torndown *)hdr; |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 950 | |
| 951 | /* |
| 952 | * Find the open msg, copy the result and signal/unblock the wait event |
| 953 | */ |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 954 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 955 | |
Hank Janssen | ebb61e5 | 2011-02-18 12:39:57 -0800 | [diff] [blame] | 956 | list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list, |
| 957 | msglistentry) { |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 958 | requestheader = |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 959 | (struct vmbus_channel_message_header *)msginfo->msg; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 960 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 961 | if (requestheader->msgtype == CHANNELMSG_GPADL_TEARDOWN) { |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 962 | gpadl_teardown = |
| 963 | (struct vmbus_channel_gpadl_teardown *)requestheader; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 964 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 965 | if (gpadl_torndown->gpadl == gpadl_teardown->gpadl) { |
| 966 | memcpy(&msginfo->response.gpadl_torndown, |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 967 | gpadl_torndown, |
K. Y. Srinivasan | 9568a19 | 2011-05-10 07:55:39 -0700 | [diff] [blame] | 968 | sizeof( |
| 969 | struct vmbus_channel_gpadl_torndown)); |
| 970 | complete(&msginfo->waitevent); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 971 | break; |
| 972 | } |
| 973 | } |
| 974 | } |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 975 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 976 | } |
| 977 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 978 | /* |
Haiyang Zhang | e98cb27 | 2010-10-15 10:14:07 -0700 | [diff] [blame] | 979 | * vmbus_onversion_response - Version response handler |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 980 | * |
| 981 | * This is invoked when we received a response to our initiate contact request. |
| 982 | * Find the matching request, copy the response and signal the requesting |
| 983 | * thread. |
| 984 | */ |
Haiyang Zhang | e98cb27 | 2010-10-15 10:14:07 -0700 | [diff] [blame] | 985 | static void vmbus_onversion_response( |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 986 | struct vmbus_channel_message_header *hdr) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 987 | { |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 988 | struct vmbus_channel_msginfo *msginfo; |
| 989 | struct vmbus_channel_message_header *requestheader; |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 990 | struct vmbus_channel_version_response *version_response; |
Greg Kroah-Hartman | dd0813b | 2009-07-15 14:56:45 -0700 | [diff] [blame] | 991 | unsigned long flags; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 992 | |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 993 | version_response = (struct vmbus_channel_version_response *)hdr; |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 994 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 995 | |
Hank Janssen | ebb61e5 | 2011-02-18 12:39:57 -0800 | [diff] [blame] | 996 | list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list, |
| 997 | msglistentry) { |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 998 | requestheader = |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 999 | (struct vmbus_channel_message_header *)msginfo->msg; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1000 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 1001 | if (requestheader->msgtype == |
| 1002 | CHANNELMSG_INITIATE_CONTACT) { |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 1003 | memcpy(&msginfo->response.version_response, |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 1004 | version_response, |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 1005 | sizeof(struct vmbus_channel_version_response)); |
K. Y. Srinivasan | 9568a19 | 2011-05-10 07:55:39 -0700 | [diff] [blame] | 1006 | complete(&msginfo->waitevent); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1007 | } |
| 1008 | } |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 1009 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1010 | } |
| 1011 | |
Greg Kroah-Hartman | c8212f0 | 2009-08-31 21:51:50 -0700 | [diff] [blame] | 1012 | /* Channel message dispatch table */ |
Dexuan Cui | 652594c | 2015-03-27 09:10:08 -0700 | [diff] [blame] | 1013 | struct vmbus_channel_message_table_entry |
K. Y. Srinivasan | b7c6b02 | 2011-05-10 07:55:38 -0700 | [diff] [blame] | 1014 | channel_message_table[CHANNELMSG_COUNT] = { |
Dexuan Cui | 652594c | 2015-03-27 09:10:08 -0700 | [diff] [blame] | 1015 | {CHANNELMSG_INVALID, 0, NULL}, |
| 1016 | {CHANNELMSG_OFFERCHANNEL, 0, vmbus_onoffer}, |
| 1017 | {CHANNELMSG_RESCIND_CHANNELOFFER, 0, vmbus_onoffer_rescind}, |
| 1018 | {CHANNELMSG_REQUESTOFFERS, 0, NULL}, |
| 1019 | {CHANNELMSG_ALLOFFERS_DELIVERED, 1, vmbus_onoffers_delivered}, |
| 1020 | {CHANNELMSG_OPENCHANNEL, 0, NULL}, |
| 1021 | {CHANNELMSG_OPENCHANNEL_RESULT, 1, vmbus_onopen_result}, |
| 1022 | {CHANNELMSG_CLOSECHANNEL, 0, NULL}, |
| 1023 | {CHANNELMSG_GPADL_HEADER, 0, NULL}, |
| 1024 | {CHANNELMSG_GPADL_BODY, 0, NULL}, |
| 1025 | {CHANNELMSG_GPADL_CREATED, 1, vmbus_ongpadl_created}, |
| 1026 | {CHANNELMSG_GPADL_TEARDOWN, 0, NULL}, |
| 1027 | {CHANNELMSG_GPADL_TORNDOWN, 1, vmbus_ongpadl_torndown}, |
| 1028 | {CHANNELMSG_RELID_RELEASED, 0, NULL}, |
| 1029 | {CHANNELMSG_INITIATE_CONTACT, 0, NULL}, |
| 1030 | {CHANNELMSG_VERSION_RESPONSE, 1, vmbus_onversion_response}, |
| 1031 | {CHANNELMSG_UNLOAD, 0, NULL}, |
K. Y. Srinivasan | 2db84ef | 2015-04-22 21:31:32 -0700 | [diff] [blame] | 1032 | {CHANNELMSG_UNLOAD_RESPONSE, 1, vmbus_unload_response}, |
Dexuan Cui | 5c23a1a | 2016-01-27 22:29:40 -0800 | [diff] [blame] | 1033 | {CHANNELMSG_18, 0, NULL}, |
| 1034 | {CHANNELMSG_19, 0, NULL}, |
| 1035 | {CHANNELMSG_20, 0, NULL}, |
| 1036 | {CHANNELMSG_TL_CONNECT_REQUEST, 0, NULL}, |
Greg Kroah-Hartman | c8212f0 | 2009-08-31 21:51:50 -0700 | [diff] [blame] | 1037 | }; |
| 1038 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 1039 | /* |
Haiyang Zhang | e98cb27 | 2010-10-15 10:14:07 -0700 | [diff] [blame] | 1040 | * vmbus_onmessage - Handler for channel protocol messages. |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 1041 | * |
| 1042 | * This is invoked in the vmbus worker thread context. |
| 1043 | */ |
Haiyang Zhang | e98cb27 | 2010-10-15 10:14:07 -0700 | [diff] [blame] | 1044 | void vmbus_onmessage(void *context) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1045 | { |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 1046 | struct hv_message *msg = context; |
Greg Kroah-Hartman | 8225021 | 2009-08-26 15:16:04 -0700 | [diff] [blame] | 1047 | struct vmbus_channel_message_header *hdr; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1048 | int size; |
| 1049 | |
Haiyang Zhang | f6feebe | 2010-11-08 14:04:39 -0800 | [diff] [blame] | 1050 | hdr = (struct vmbus_channel_message_header *)msg->u.payload; |
| 1051 | size = msg->header.payload_size; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1052 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 1053 | if (hdr->msgtype >= CHANNELMSG_COUNT) { |
Hank Janssen | 0a46618 | 2011-03-29 13:58:47 -0700 | [diff] [blame] | 1054 | pr_err("Received invalid channel message type %d size %d\n", |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 1055 | hdr->msgtype, size); |
Greg Kroah-Hartman | 04f50c4 | 2009-07-16 12:35:37 -0700 | [diff] [blame] | 1056 | print_hex_dump_bytes("", DUMP_PREFIX_NONE, |
Haiyang Zhang | f6feebe | 2010-11-08 14:04:39 -0800 | [diff] [blame] | 1057 | (unsigned char *)msg->u.payload, size); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1058 | return; |
| 1059 | } |
| 1060 | |
K. Y. Srinivasan | b7c6b02 | 2011-05-10 07:55:38 -0700 | [diff] [blame] | 1061 | if (channel_message_table[hdr->msgtype].message_handler) |
| 1062 | channel_message_table[hdr->msgtype].message_handler(hdr); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1063 | else |
Hank Janssen | 0a46618 | 2011-03-29 13:58:47 -0700 | [diff] [blame] | 1064 | pr_err("Unhandled channel message type %d\n", hdr->msgtype); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1065 | } |
| 1066 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 1067 | /* |
Haiyang Zhang | e98cb27 | 2010-10-15 10:14:07 -0700 | [diff] [blame] | 1068 | * 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] | 1069 | */ |
Haiyang Zhang | e98cb27 | 2010-10-15 10:14:07 -0700 | [diff] [blame] | 1070 | int vmbus_request_offers(void) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1071 | { |
Greg Kroah-Hartman | 8225021 | 2009-08-26 15:16:04 -0700 | [diff] [blame] | 1072 | struct vmbus_channel_message_header *msg; |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 1073 | struct vmbus_channel_msginfo *msginfo; |
Nicholas Mc Guire | 51e5181 | 2015-02-27 11:26:02 -0800 | [diff] [blame] | 1074 | int ret; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1075 | |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 1076 | msginfo = kmalloc(sizeof(*msginfo) + |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 1077 | sizeof(struct vmbus_channel_message_header), |
| 1078 | GFP_KERNEL); |
Haiyang Zhang | 188963e | 2010-10-15 10:14:06 -0700 | [diff] [blame] | 1079 | if (!msginfo) |
Bill Pemberton | 75910f2 | 2010-05-05 15:27:31 -0400 | [diff] [blame] | 1080 | return -ENOMEM; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1081 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 1082 | msg = (struct vmbus_channel_message_header *)msginfo->msg; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1083 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 1084 | msg->msgtype = CHANNELMSG_REQUESTOFFERS; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1085 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1086 | |
Haiyang Zhang | c697767 | 2011-01-26 12:12:08 -0800 | [diff] [blame] | 1087 | ret = vmbus_post_msg(msg, |
Greg Kroah-Hartman | bd60c33 | 2009-08-31 21:47:21 -0700 | [diff] [blame] | 1088 | sizeof(struct vmbus_channel_message_header)); |
| 1089 | if (ret != 0) { |
Hank Janssen | 0a46618 | 2011-03-29 13:58:47 -0700 | [diff] [blame] | 1090 | pr_err("Unable to request offers - %d\n", ret); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1091 | |
K. Y. Srinivasan | 0c3b7b2 | 2011-02-11 09:59:43 -0800 | [diff] [blame] | 1092 | goto cleanup; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1093 | } |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1094 | |
K. Y. Srinivasan | 0c3b7b2 | 2011-02-11 09:59:43 -0800 | [diff] [blame] | 1095 | cleanup: |
Ilia Mirkin | dd9b15d | 2011-03-13 00:29:00 -0500 | [diff] [blame] | 1096 | kfree(msginfo); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1097 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1098 | return ret; |
| 1099 | } |
| 1100 | |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 1101 | /* |
| 1102 | * Retrieve the (sub) channel on which to send an outgoing request. |
K. Y. Srinivasan | a13e8bb | 2015-02-28 11:39:02 -0800 | [diff] [blame] | 1103 | * When a primary channel has multiple sub-channels, we try to |
| 1104 | * distribute the load equally amongst all available channels. |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 1105 | */ |
| 1106 | struct vmbus_channel *vmbus_get_outgoing_channel(struct vmbus_channel *primary) |
| 1107 | { |
| 1108 | struct list_head *cur, *tmp; |
K. Y. Srinivasan | 87712bf8 | 2014-12-14 23:34:51 -0800 | [diff] [blame] | 1109 | int cur_cpu; |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 1110 | struct vmbus_channel *cur_channel; |
| 1111 | struct vmbus_channel *outgoing_channel = primary; |
K. Y. Srinivasan | a13e8bb | 2015-02-28 11:39:02 -0800 | [diff] [blame] | 1112 | int next_channel; |
| 1113 | int i = 1; |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 1114 | |
| 1115 | if (list_empty(&primary->sc_list)) |
| 1116 | return outgoing_channel; |
| 1117 | |
K. Y. Srinivasan | a13e8bb | 2015-02-28 11:39:02 -0800 | [diff] [blame] | 1118 | next_channel = primary->next_oc++; |
| 1119 | |
| 1120 | if (next_channel > (primary->num_sc)) { |
| 1121 | primary->next_oc = 0; |
| 1122 | return outgoing_channel; |
| 1123 | } |
| 1124 | |
K. Y. Srinivasan | 87712bf8 | 2014-12-14 23:34:51 -0800 | [diff] [blame] | 1125 | cur_cpu = hv_context.vp_index[get_cpu()]; |
| 1126 | put_cpu(); |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 1127 | list_for_each_safe(cur, tmp, &primary->sc_list) { |
| 1128 | cur_channel = list_entry(cur, struct vmbus_channel, sc_list); |
| 1129 | if (cur_channel->state != CHANNEL_OPENED_STATE) |
| 1130 | continue; |
| 1131 | |
| 1132 | if (cur_channel->target_vp == cur_cpu) |
| 1133 | return cur_channel; |
| 1134 | |
K. Y. Srinivasan | a13e8bb | 2015-02-28 11:39:02 -0800 | [diff] [blame] | 1135 | if (i == next_channel) |
| 1136 | return cur_channel; |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 1137 | |
K. Y. Srinivasan | a13e8bb | 2015-02-28 11:39:02 -0800 | [diff] [blame] | 1138 | i++; |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | return outgoing_channel; |
| 1142 | } |
| 1143 | EXPORT_SYMBOL_GPL(vmbus_get_outgoing_channel); |
| 1144 | |
| 1145 | static void invoke_sc_cb(struct vmbus_channel *primary_channel) |
| 1146 | { |
| 1147 | struct list_head *cur, *tmp; |
| 1148 | struct vmbus_channel *cur_channel; |
| 1149 | |
| 1150 | if (primary_channel->sc_creation_callback == NULL) |
| 1151 | return; |
| 1152 | |
| 1153 | list_for_each_safe(cur, tmp, &primary_channel->sc_list) { |
| 1154 | cur_channel = list_entry(cur, struct vmbus_channel, sc_list); |
| 1155 | |
| 1156 | primary_channel->sc_creation_callback(cur_channel); |
| 1157 | } |
| 1158 | } |
| 1159 | |
| 1160 | void vmbus_set_sc_create_callback(struct vmbus_channel *primary_channel, |
| 1161 | void (*sc_cr_cb)(struct vmbus_channel *new_sc)) |
| 1162 | { |
| 1163 | primary_channel->sc_creation_callback = sc_cr_cb; |
| 1164 | } |
| 1165 | EXPORT_SYMBOL_GPL(vmbus_set_sc_create_callback); |
| 1166 | |
| 1167 | bool vmbus_are_subchannels_present(struct vmbus_channel *primary) |
| 1168 | { |
| 1169 | bool ret; |
| 1170 | |
| 1171 | ret = !list_empty(&primary->sc_list); |
| 1172 | |
| 1173 | if (ret) { |
| 1174 | /* |
| 1175 | * Invoke the callback on sub-channel creation. |
| 1176 | * This will present a uniform interface to the |
| 1177 | * clients. |
| 1178 | */ |
| 1179 | invoke_sc_cb(primary); |
| 1180 | } |
| 1181 | |
| 1182 | return ret; |
| 1183 | } |
| 1184 | EXPORT_SYMBOL_GPL(vmbus_are_subchannels_present); |
Dexuan Cui | 499e840 | 2016-01-27 22:29:42 -0800 | [diff] [blame] | 1185 | |
| 1186 | void vmbus_set_chn_rescind_callback(struct vmbus_channel *channel, |
| 1187 | void (*chn_rescind_cb)(struct vmbus_channel *)) |
| 1188 | { |
| 1189 | channel->chn_rescind_callback = chn_rescind_cb; |
| 1190 | } |
| 1191 | EXPORT_SYMBOL_GPL(vmbus_set_chn_rescind_callback); |