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