1 /* 2 * 3 * Copyright (c) 2009, Microsoft Corporation. 4 * 5 * This program is free software; you can redistribute it and/or modify it 6 * under the terms and conditions of the GNU General Public License, 7 * version 2, as published by the Free Software Foundation. 8 * 9 * This program is distributed in the hope it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 12 * more details. 13 * 14 * You should have received a copy of the GNU General Public License along with 15 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple 16 * Place - Suite 330, Boston, MA 02111-1307 USA. 17 * 18 * Authors: 19 * Haiyang Zhang <haiyangz@microsoft.com> 20 * Hank Janssen <hjanssen@microsoft.com> 21 * 22 */ 23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 24 25 #include <linux/kernel.h> 26 #include <linux/sched.h> 27 #include <linux/wait.h> 28 #include <linux/delay.h> 29 #include <linux/mm.h> 30 #include <linux/slab.h> 31 #include <linux/vmalloc.h> 32 #include <linux/hyperv.h> 33 #include <linux/export.h> 34 #include <asm/hyperv.h> 35 #include <asm/mshyperv.h> 36 37 #include "hyperv_vmbus.h" 38 39 40 struct vmbus_connection vmbus_connection = { 41 .conn_state = DISCONNECTED, 42 .next_gpadl_handle = ATOMIC_INIT(0xE1E10), 43 }; 44 EXPORT_SYMBOL_GPL(vmbus_connection); 45 46 /* 47 * Negotiated protocol version with the host. 48 */ 49 __u32 vmbus_proto_version; 50 EXPORT_SYMBOL_GPL(vmbus_proto_version); 51 52 static __u32 vmbus_get_next_version(__u32 current_version) 53 { 54 switch (current_version) { 55 case (VERSION_WIN7): 56 return VERSION_WS2008; 57 58 case (VERSION_WIN8): 59 return VERSION_WIN7; 60 61 case (VERSION_WIN8_1): 62 return VERSION_WIN8; 63 64 case (VERSION_WIN10): 65 return VERSION_WIN8_1; 66 67 case (VERSION_WS2008): 68 default: 69 return VERSION_INVAL; 70 } 71 } 72 73 static int vmbus_negotiate_version(struct vmbus_channel_msginfo *msginfo, 74 __u32 version) 75 { 76 int ret = 0; 77 struct vmbus_channel_initiate_contact *msg; 78 unsigned long flags; 79 80 init_completion(&msginfo->waitevent); 81 82 msg = (struct vmbus_channel_initiate_contact *)msginfo->msg; 83 84 msg->header.msgtype = CHANNELMSG_INITIATE_CONTACT; 85 msg->vmbus_version_requested = version; 86 msg->interrupt_page = virt_to_phys(vmbus_connection.int_page); 87 msg->monitor_page1 = virt_to_phys(vmbus_connection.monitor_pages[0]); 88 msg->monitor_page2 = virt_to_phys(vmbus_connection.monitor_pages[1]); 89 /* 90 * We want all channel messages to be delivered on CPU 0. 91 * This has been the behavior pre-win8. This is not 92 * perf issue and having all channel messages delivered on CPU 0 93 * would be ok. 94 * For post win8 hosts, we support receiving channel messagges on 95 * all the CPUs. This is needed for kexec to work correctly where 96 * the CPU attempting to connect may not be CPU 0. 97 */ 98 if (version >= VERSION_WIN8_1) { 99 msg->target_vcpu = hv_context.vp_index[smp_processor_id()]; 100 vmbus_connection.connect_cpu = smp_processor_id(); 101 } else { 102 msg->target_vcpu = 0; 103 vmbus_connection.connect_cpu = 0; 104 } 105 106 /* 107 * Add to list before we send the request since we may 108 * receive the response before returning from this routine 109 */ 110 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); 111 list_add_tail(&msginfo->msglistentry, 112 &vmbus_connection.chn_msg_list); 113 114 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); 115 116 ret = vmbus_post_msg(msg, 117 sizeof(struct vmbus_channel_initiate_contact), 118 true); 119 if (ret != 0) { 120 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); 121 list_del(&msginfo->msglistentry); 122 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, 123 flags); 124 return ret; 125 } 126 127 /* Wait for the connection response */ 128 wait_for_completion(&msginfo->waitevent); 129 130 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); 131 list_del(&msginfo->msglistentry); 132 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); 133 134 /* Check if successful */ 135 if (msginfo->response.version_response.version_supported) { 136 vmbus_connection.conn_state = CONNECTED; 137 } else { 138 return -ECONNREFUSED; 139 } 140 141 return ret; 142 } 143 144 /* 145 * vmbus_connect - Sends a connect request on the partition service connection 146 */ 147 int vmbus_connect(void) 148 { 149 int ret = 0; 150 struct vmbus_channel_msginfo *msginfo = NULL; 151 __u32 version; 152 153 /* Initialize the vmbus connection */ 154 vmbus_connection.conn_state = CONNECTING; 155 vmbus_connection.work_queue = create_workqueue("hv_vmbus_con"); 156 if (!vmbus_connection.work_queue) { 157 ret = -ENOMEM; 158 goto cleanup; 159 } 160 161 INIT_LIST_HEAD(&vmbus_connection.chn_msg_list); 162 spin_lock_init(&vmbus_connection.channelmsg_lock); 163 164 INIT_LIST_HEAD(&vmbus_connection.chn_list); 165 mutex_init(&vmbus_connection.channel_mutex); 166 167 /* 168 * Setup the vmbus event connection for channel interrupt 169 * abstraction stuff 170 */ 171 vmbus_connection.int_page = 172 (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, 0); 173 if (vmbus_connection.int_page == NULL) { 174 ret = -ENOMEM; 175 goto cleanup; 176 } 177 178 vmbus_connection.recv_int_page = vmbus_connection.int_page; 179 vmbus_connection.send_int_page = 180 (void *)((unsigned long)vmbus_connection.int_page + 181 (PAGE_SIZE >> 1)); 182 183 /* 184 * Setup the monitor notification facility. The 1st page for 185 * parent->child and the 2nd page for child->parent 186 */ 187 vmbus_connection.monitor_pages[0] = (void *)__get_free_pages((GFP_KERNEL|__GFP_ZERO), 0); 188 vmbus_connection.monitor_pages[1] = (void *)__get_free_pages((GFP_KERNEL|__GFP_ZERO), 0); 189 if ((vmbus_connection.monitor_pages[0] == NULL) || 190 (vmbus_connection.monitor_pages[1] == NULL)) { 191 ret = -ENOMEM; 192 goto cleanup; 193 } 194 195 msginfo = kzalloc(sizeof(*msginfo) + 196 sizeof(struct vmbus_channel_initiate_contact), 197 GFP_KERNEL); 198 if (msginfo == NULL) { 199 ret = -ENOMEM; 200 goto cleanup; 201 } 202 203 /* 204 * Negotiate a compatible VMBUS version number with the 205 * host. We start with the highest number we can support 206 * and work our way down until we negotiate a compatible 207 * version. 208 */ 209 210 version = VERSION_CURRENT; 211 212 do { 213 ret = vmbus_negotiate_version(msginfo, version); 214 if (ret == -ETIMEDOUT) 215 goto cleanup; 216 217 if (vmbus_connection.conn_state == CONNECTED) 218 break; 219 220 version = vmbus_get_next_version(version); 221 } while (version != VERSION_INVAL); 222 223 if (version == VERSION_INVAL) 224 goto cleanup; 225 226 vmbus_proto_version = version; 227 pr_info("Vmbus version:%d.%d\n", 228 version >> 16, version & 0xFFFF); 229 230 kfree(msginfo); 231 return 0; 232 233 cleanup: 234 pr_err("Unable to connect to host\n"); 235 236 vmbus_connection.conn_state = DISCONNECTED; 237 vmbus_disconnect(); 238 239 kfree(msginfo); 240 241 return ret; 242 } 243 244 void vmbus_disconnect(void) 245 { 246 /* 247 * First send the unload request to the host. 248 */ 249 vmbus_initiate_unload(false); 250 251 if (vmbus_connection.work_queue) { 252 drain_workqueue(vmbus_connection.work_queue); 253 destroy_workqueue(vmbus_connection.work_queue); 254 } 255 256 if (vmbus_connection.int_page) { 257 free_pages((unsigned long)vmbus_connection.int_page, 0); 258 vmbus_connection.int_page = NULL; 259 } 260 261 free_pages((unsigned long)vmbus_connection.monitor_pages[0], 0); 262 free_pages((unsigned long)vmbus_connection.monitor_pages[1], 0); 263 vmbus_connection.monitor_pages[0] = NULL; 264 vmbus_connection.monitor_pages[1] = NULL; 265 } 266 267 /* 268 * relid2channel - Get the channel object given its 269 * child relative id (ie channel id) 270 */ 271 struct vmbus_channel *relid2channel(u32 relid) 272 { 273 struct vmbus_channel *channel; 274 struct vmbus_channel *found_channel = NULL; 275 struct list_head *cur, *tmp; 276 struct vmbus_channel *cur_sc; 277 278 BUG_ON(!mutex_is_locked(&vmbus_connection.channel_mutex)); 279 280 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) { 281 if (channel->offermsg.child_relid == relid) { 282 found_channel = channel; 283 break; 284 } else if (!list_empty(&channel->sc_list)) { 285 /* 286 * Deal with sub-channels. 287 */ 288 list_for_each_safe(cur, tmp, &channel->sc_list) { 289 cur_sc = list_entry(cur, struct vmbus_channel, 290 sc_list); 291 if (cur_sc->offermsg.child_relid == relid) { 292 found_channel = cur_sc; 293 break; 294 } 295 } 296 } 297 } 298 299 return found_channel; 300 } 301 302 /* 303 * vmbus_on_event - Process a channel event notification 304 * 305 * For batched channels (default) optimize host to guest signaling 306 * by ensuring: 307 * 1. While reading the channel, we disable interrupts from host. 308 * 2. Ensure that we process all posted messages from the host 309 * before returning from this callback. 310 * 3. Once we return, enable signaling from the host. Once this 311 * state is set we check to see if additional packets are 312 * available to read. In this case we repeat the process. 313 * If this tasklet has been running for a long time 314 * then reschedule ourselves. 315 */ 316 void vmbus_on_event(unsigned long data) 317 { 318 struct vmbus_channel *channel = (void *) data; 319 unsigned long time_limit = jiffies + 2; 320 321 do { 322 void (*callback_fn)(void *); 323 324 /* A channel once created is persistent even when 325 * there is no driver handling the device. An 326 * unloading driver sets the onchannel_callback to NULL. 327 */ 328 callback_fn = READ_ONCE(channel->onchannel_callback); 329 if (unlikely(callback_fn == NULL)) 330 return; 331 332 (*callback_fn)(channel->channel_callback_context); 333 334 if (channel->callback_mode != HV_CALL_BATCHED) 335 return; 336 337 if (likely(hv_end_read(&channel->inbound) == 0)) 338 return; 339 340 hv_begin_read(&channel->inbound); 341 } while (likely(time_before(jiffies, time_limit))); 342 343 /* The time limit (2 jiffies) has been reached */ 344 tasklet_schedule(&channel->callback_event); 345 } 346 347 /* 348 * vmbus_post_msg - Send a msg on the vmbus's message connection 349 */ 350 int vmbus_post_msg(void *buffer, size_t buflen, bool can_sleep) 351 { 352 union hv_connection_id conn_id; 353 int ret = 0; 354 int retries = 0; 355 u32 usec = 1; 356 357 conn_id.asu32 = 0; 358 conn_id.u.id = VMBUS_MESSAGE_CONNECTION_ID; 359 360 /* 361 * hv_post_message() can have transient failures because of 362 * insufficient resources. Retry the operation a couple of 363 * times before giving up. 364 */ 365 while (retries < 100) { 366 ret = hv_post_message(conn_id, 1, buffer, buflen); 367 368 switch (ret) { 369 case HV_STATUS_INVALID_CONNECTION_ID: 370 /* 371 * We could get this if we send messages too 372 * frequently. 373 */ 374 ret = -EAGAIN; 375 break; 376 case HV_STATUS_INSUFFICIENT_MEMORY: 377 case HV_STATUS_INSUFFICIENT_BUFFERS: 378 ret = -ENOBUFS; 379 break; 380 case HV_STATUS_SUCCESS: 381 return ret; 382 default: 383 pr_err("hv_post_msg() failed; error code:%d\n", ret); 384 return -EINVAL; 385 } 386 387 retries++; 388 if (can_sleep && usec > 1000) 389 msleep(usec / 1000); 390 else if (usec < MAX_UDELAY_MS * 1000) 391 udelay(usec); 392 else 393 mdelay(usec / 1000); 394 395 if (retries < 22) 396 usec *= 2; 397 } 398 return ret; 399 } 400 401 /* 402 * vmbus_set_event - Send an event notification to the parent 403 */ 404 void vmbus_set_event(struct vmbus_channel *channel) 405 { 406 u32 child_relid = channel->offermsg.child_relid; 407 408 if (!channel->is_dedicated_interrupt) 409 vmbus_send_interrupt(child_relid); 410 411 hv_do_fast_hypercall8(HVCALL_SIGNAL_EVENT, channel->sig_event); 412 } 413 EXPORT_SYMBOL_GPL(vmbus_set_event); 414