1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2009, Microsoft Corporation. 4 * 5 * Authors: 6 * Haiyang Zhang <haiyangz@microsoft.com> 7 * Hank Janssen <hjanssen@microsoft.com> 8 */ 9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 10 11 #include <linux/kernel.h> 12 #include <linux/interrupt.h> 13 #include <linux/sched.h> 14 #include <linux/wait.h> 15 #include <linux/mm.h> 16 #include <linux/slab.h> 17 #include <linux/list.h> 18 #include <linux/module.h> 19 #include <linux/completion.h> 20 #include <linux/delay.h> 21 #include <linux/cpu.h> 22 #include <linux/hyperv.h> 23 #include <asm/mshyperv.h> 24 25 #include "hyperv_vmbus.h" 26 27 static void init_vp_index(struct vmbus_channel *channel); 28 29 const struct vmbus_device vmbus_devs[] = { 30 /* IDE */ 31 { .dev_type = HV_IDE, 32 HV_IDE_GUID, 33 .perf_device = true, 34 }, 35 36 /* SCSI */ 37 { .dev_type = HV_SCSI, 38 HV_SCSI_GUID, 39 .perf_device = true, 40 }, 41 42 /* Fibre Channel */ 43 { .dev_type = HV_FC, 44 HV_SYNTHFC_GUID, 45 .perf_device = true, 46 }, 47 48 /* Synthetic NIC */ 49 { .dev_type = HV_NIC, 50 HV_NIC_GUID, 51 .perf_device = true, 52 }, 53 54 /* Network Direct */ 55 { .dev_type = HV_ND, 56 HV_ND_GUID, 57 .perf_device = true, 58 }, 59 60 /* PCIE */ 61 { .dev_type = HV_PCIE, 62 HV_PCIE_GUID, 63 .perf_device = false, 64 }, 65 66 /* Synthetic Frame Buffer */ 67 { .dev_type = HV_FB, 68 HV_SYNTHVID_GUID, 69 .perf_device = false, 70 }, 71 72 /* Synthetic Keyboard */ 73 { .dev_type = HV_KBD, 74 HV_KBD_GUID, 75 .perf_device = false, 76 }, 77 78 /* Synthetic MOUSE */ 79 { .dev_type = HV_MOUSE, 80 HV_MOUSE_GUID, 81 .perf_device = false, 82 }, 83 84 /* KVP */ 85 { .dev_type = HV_KVP, 86 HV_KVP_GUID, 87 .perf_device = false, 88 }, 89 90 /* Time Synch */ 91 { .dev_type = HV_TS, 92 HV_TS_GUID, 93 .perf_device = false, 94 }, 95 96 /* Heartbeat */ 97 { .dev_type = HV_HB, 98 HV_HEART_BEAT_GUID, 99 .perf_device = false, 100 }, 101 102 /* Shutdown */ 103 { .dev_type = HV_SHUTDOWN, 104 HV_SHUTDOWN_GUID, 105 .perf_device = false, 106 }, 107 108 /* File copy */ 109 { .dev_type = HV_FCOPY, 110 HV_FCOPY_GUID, 111 .perf_device = false, 112 }, 113 114 /* Backup */ 115 { .dev_type = HV_BACKUP, 116 HV_VSS_GUID, 117 .perf_device = false, 118 }, 119 120 /* Dynamic Memory */ 121 { .dev_type = HV_DM, 122 HV_DM_GUID, 123 .perf_device = false, 124 }, 125 126 /* Unknown GUID */ 127 { .dev_type = HV_UNKNOWN, 128 .perf_device = false, 129 }, 130 }; 131 132 static const struct { 133 guid_t guid; 134 } vmbus_unsupported_devs[] = { 135 { HV_AVMA1_GUID }, 136 { HV_AVMA2_GUID }, 137 { HV_RDV_GUID }, 138 }; 139 140 /* 141 * The rescinded channel may be blocked waiting for a response from the host; 142 * take care of that. 143 */ 144 static void vmbus_rescind_cleanup(struct vmbus_channel *channel) 145 { 146 struct vmbus_channel_msginfo *msginfo; 147 unsigned long flags; 148 149 150 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); 151 channel->rescind = true; 152 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list, 153 msglistentry) { 154 155 if (msginfo->waiting_channel == channel) { 156 complete(&msginfo->waitevent); 157 break; 158 } 159 } 160 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); 161 } 162 163 static bool is_unsupported_vmbus_devs(const guid_t *guid) 164 { 165 int i; 166 167 for (i = 0; i < ARRAY_SIZE(vmbus_unsupported_devs); i++) 168 if (guid_equal(guid, &vmbus_unsupported_devs[i].guid)) 169 return true; 170 return false; 171 } 172 173 static u16 hv_get_dev_type(const struct vmbus_channel *channel) 174 { 175 const guid_t *guid = &channel->offermsg.offer.if_type; 176 u16 i; 177 178 if (is_hvsock_channel(channel) || is_unsupported_vmbus_devs(guid)) 179 return HV_UNKNOWN; 180 181 for (i = HV_IDE; i < HV_UNKNOWN; i++) { 182 if (guid_equal(guid, &vmbus_devs[i].guid)) 183 return i; 184 } 185 pr_info("Unknown GUID: %pUl\n", guid); 186 return i; 187 } 188 189 /** 190 * vmbus_prep_negotiate_resp() - Create default response for Negotiate message 191 * @icmsghdrp: Pointer to msg header structure 192 * @buf: Raw buffer channel data 193 * @buflen: Length of the raw buffer channel data. 194 * @fw_version: The framework versions we can support. 195 * @fw_vercnt: The size of @fw_version. 196 * @srv_version: The service versions we can support. 197 * @srv_vercnt: The size of @srv_version. 198 * @nego_fw_version: The selected framework version. 199 * @nego_srv_version: The selected service version. 200 * 201 * Note: Versions are given in decreasing order. 202 * 203 * Set up and fill in default negotiate response message. 204 * Mainly used by Hyper-V drivers. 205 */ 206 bool vmbus_prep_negotiate_resp(struct icmsg_hdr *icmsghdrp, u8 *buf, 207 u32 buflen, const int *fw_version, int fw_vercnt, 208 const int *srv_version, int srv_vercnt, 209 int *nego_fw_version, int *nego_srv_version) 210 { 211 int icframe_major, icframe_minor; 212 int icmsg_major, icmsg_minor; 213 int fw_major, fw_minor; 214 int srv_major, srv_minor; 215 int i, j; 216 bool found_match = false; 217 struct icmsg_negotiate *negop; 218 219 /* Check that there's enough space for icframe_vercnt, icmsg_vercnt */ 220 if (buflen < ICMSG_HDR + offsetof(struct icmsg_negotiate, reserved)) { 221 pr_err_ratelimited("Invalid icmsg negotiate\n"); 222 return false; 223 } 224 225 icmsghdrp->icmsgsize = 0x10; 226 negop = (struct icmsg_negotiate *)&buf[ICMSG_HDR]; 227 228 icframe_major = negop->icframe_vercnt; 229 icframe_minor = 0; 230 231 icmsg_major = negop->icmsg_vercnt; 232 icmsg_minor = 0; 233 234 /* Validate negop packet */ 235 if (icframe_major > IC_VERSION_NEGOTIATION_MAX_VER_COUNT || 236 icmsg_major > IC_VERSION_NEGOTIATION_MAX_VER_COUNT || 237 ICMSG_NEGOTIATE_PKT_SIZE(icframe_major, icmsg_major) > buflen) { 238 pr_err_ratelimited("Invalid icmsg negotiate - icframe_major: %u, icmsg_major: %u\n", 239 icframe_major, icmsg_major); 240 goto fw_error; 241 } 242 243 /* 244 * Select the framework version number we will 245 * support. 246 */ 247 248 for (i = 0; i < fw_vercnt; i++) { 249 fw_major = (fw_version[i] >> 16); 250 fw_minor = (fw_version[i] & 0xFFFF); 251 252 for (j = 0; j < negop->icframe_vercnt; j++) { 253 if ((negop->icversion_data[j].major == fw_major) && 254 (negop->icversion_data[j].minor == fw_minor)) { 255 icframe_major = negop->icversion_data[j].major; 256 icframe_minor = negop->icversion_data[j].minor; 257 found_match = true; 258 break; 259 } 260 } 261 262 if (found_match) 263 break; 264 } 265 266 if (!found_match) 267 goto fw_error; 268 269 found_match = false; 270 271 for (i = 0; i < srv_vercnt; i++) { 272 srv_major = (srv_version[i] >> 16); 273 srv_minor = (srv_version[i] & 0xFFFF); 274 275 for (j = negop->icframe_vercnt; 276 (j < negop->icframe_vercnt + negop->icmsg_vercnt); 277 j++) { 278 279 if ((negop->icversion_data[j].major == srv_major) && 280 (negop->icversion_data[j].minor == srv_minor)) { 281 282 icmsg_major = negop->icversion_data[j].major; 283 icmsg_minor = negop->icversion_data[j].minor; 284 found_match = true; 285 break; 286 } 287 } 288 289 if (found_match) 290 break; 291 } 292 293 /* 294 * Respond with the framework and service 295 * version numbers we can support. 296 */ 297 298 fw_error: 299 if (!found_match) { 300 negop->icframe_vercnt = 0; 301 negop->icmsg_vercnt = 0; 302 } else { 303 negop->icframe_vercnt = 1; 304 negop->icmsg_vercnt = 1; 305 } 306 307 if (nego_fw_version) 308 *nego_fw_version = (icframe_major << 16) | icframe_minor; 309 310 if (nego_srv_version) 311 *nego_srv_version = (icmsg_major << 16) | icmsg_minor; 312 313 negop->icversion_data[0].major = icframe_major; 314 negop->icversion_data[0].minor = icframe_minor; 315 negop->icversion_data[1].major = icmsg_major; 316 negop->icversion_data[1].minor = icmsg_minor; 317 return found_match; 318 } 319 320 EXPORT_SYMBOL_GPL(vmbus_prep_negotiate_resp); 321 322 /* 323 * alloc_channel - Allocate and initialize a vmbus channel object 324 */ 325 static struct vmbus_channel *alloc_channel(void) 326 { 327 struct vmbus_channel *channel; 328 329 channel = kzalloc(sizeof(*channel), GFP_ATOMIC); 330 if (!channel) 331 return NULL; 332 333 spin_lock_init(&channel->sched_lock); 334 init_completion(&channel->rescind_event); 335 336 INIT_LIST_HEAD(&channel->sc_list); 337 338 tasklet_init(&channel->callback_event, 339 vmbus_on_event, (unsigned long)channel); 340 341 hv_ringbuffer_pre_init(channel); 342 343 return channel; 344 } 345 346 /* 347 * free_channel - Release the resources used by the vmbus channel object 348 */ 349 static void free_channel(struct vmbus_channel *channel) 350 { 351 tasklet_kill(&channel->callback_event); 352 vmbus_remove_channel_attr_group(channel); 353 354 kobject_put(&channel->kobj); 355 } 356 357 void vmbus_channel_map_relid(struct vmbus_channel *channel) 358 { 359 if (WARN_ON(channel->offermsg.child_relid >= MAX_CHANNEL_RELIDS)) 360 return; 361 /* 362 * The mapping of the channel's relid is visible from the CPUs that 363 * execute vmbus_chan_sched() by the time that vmbus_chan_sched() will 364 * execute: 365 * 366 * (a) In the "normal (i.e., not resuming from hibernation)" path, 367 * the full barrier in smp_store_mb() guarantees that the store 368 * is propagated to all CPUs before the add_channel_work work 369 * is queued. In turn, add_channel_work is queued before the 370 * channel's ring buffer is allocated/initialized and the 371 * OPENCHANNEL message for the channel is sent in vmbus_open(). 372 * Hyper-V won't start sending the interrupts for the channel 373 * before the OPENCHANNEL message is acked. The memory barrier 374 * in vmbus_chan_sched() -> sync_test_and_clear_bit() ensures 375 * that vmbus_chan_sched() must find the channel's relid in 376 * recv_int_page before retrieving the channel pointer from the 377 * array of channels. 378 * 379 * (b) In the "resuming from hibernation" path, the smp_store_mb() 380 * guarantees that the store is propagated to all CPUs before 381 * the VMBus connection is marked as ready for the resume event 382 * (cf. check_ready_for_resume_event()). The interrupt handler 383 * of the VMBus driver and vmbus_chan_sched() can not run before 384 * vmbus_bus_resume() has completed execution (cf. resume_noirq). 385 */ 386 smp_store_mb( 387 vmbus_connection.channels[channel->offermsg.child_relid], 388 channel); 389 } 390 391 void vmbus_channel_unmap_relid(struct vmbus_channel *channel) 392 { 393 if (WARN_ON(channel->offermsg.child_relid >= MAX_CHANNEL_RELIDS)) 394 return; 395 WRITE_ONCE( 396 vmbus_connection.channels[channel->offermsg.child_relid], 397 NULL); 398 } 399 400 static void vmbus_release_relid(u32 relid) 401 { 402 struct vmbus_channel_relid_released msg; 403 int ret; 404 405 memset(&msg, 0, sizeof(struct vmbus_channel_relid_released)); 406 msg.child_relid = relid; 407 msg.header.msgtype = CHANNELMSG_RELID_RELEASED; 408 ret = vmbus_post_msg(&msg, sizeof(struct vmbus_channel_relid_released), 409 true); 410 411 trace_vmbus_release_relid(&msg, ret); 412 } 413 414 void hv_process_channel_removal(struct vmbus_channel *channel) 415 { 416 lockdep_assert_held(&vmbus_connection.channel_mutex); 417 BUG_ON(!channel->rescind); 418 419 /* 420 * hv_process_channel_removal() could find INVALID_RELID only for 421 * hv_sock channels. See the inline comments in vmbus_onoffer(). 422 */ 423 WARN_ON(channel->offermsg.child_relid == INVALID_RELID && 424 !is_hvsock_channel(channel)); 425 426 /* 427 * Upon suspend, an in-use hv_sock channel is removed from the array of 428 * channels and the relid is invalidated. After hibernation, when the 429 * user-space appplication destroys the channel, it's unnecessary and 430 * unsafe to remove the channel from the array of channels. See also 431 * the inline comments before the call of vmbus_release_relid() below. 432 */ 433 if (channel->offermsg.child_relid != INVALID_RELID) 434 vmbus_channel_unmap_relid(channel); 435 436 if (channel->primary_channel == NULL) 437 list_del(&channel->listentry); 438 else 439 list_del(&channel->sc_list); 440 441 /* 442 * If this is a "perf" channel, updates the hv_numa_map[] masks so that 443 * init_vp_index() can (re-)use the CPU. 444 */ 445 if (hv_is_perf_channel(channel)) 446 hv_clear_alloced_cpu(channel->target_cpu); 447 448 /* 449 * Upon suspend, an in-use hv_sock channel is marked as "rescinded" and 450 * the relid is invalidated; after hibernation, when the user-space app 451 * destroys the channel, the relid is INVALID_RELID, and in this case 452 * it's unnecessary and unsafe to release the old relid, since the same 453 * relid can refer to a completely different channel now. 454 */ 455 if (channel->offermsg.child_relid != INVALID_RELID) 456 vmbus_release_relid(channel->offermsg.child_relid); 457 458 free_channel(channel); 459 } 460 461 void vmbus_free_channels(void) 462 { 463 struct vmbus_channel *channel, *tmp; 464 465 list_for_each_entry_safe(channel, tmp, &vmbus_connection.chn_list, 466 listentry) { 467 /* hv_process_channel_removal() needs this */ 468 channel->rescind = true; 469 470 vmbus_device_unregister(channel->device_obj); 471 } 472 } 473 474 /* Note: the function can run concurrently for primary/sub channels. */ 475 static void vmbus_add_channel_work(struct work_struct *work) 476 { 477 struct vmbus_channel *newchannel = 478 container_of(work, struct vmbus_channel, add_channel_work); 479 struct vmbus_channel *primary_channel = newchannel->primary_channel; 480 int ret; 481 482 /* 483 * This state is used to indicate a successful open 484 * so that when we do close the channel normally, we 485 * can cleanup properly. 486 */ 487 newchannel->state = CHANNEL_OPEN_STATE; 488 489 if (primary_channel != NULL) { 490 /* newchannel is a sub-channel. */ 491 struct hv_device *dev = primary_channel->device_obj; 492 493 if (vmbus_add_channel_kobj(dev, newchannel)) 494 goto err_deq_chan; 495 496 if (primary_channel->sc_creation_callback != NULL) 497 primary_channel->sc_creation_callback(newchannel); 498 499 newchannel->probe_done = true; 500 return; 501 } 502 503 /* 504 * Start the process of binding the primary channel to the driver 505 */ 506 newchannel->device_obj = vmbus_device_create( 507 &newchannel->offermsg.offer.if_type, 508 &newchannel->offermsg.offer.if_instance, 509 newchannel); 510 if (!newchannel->device_obj) 511 goto err_deq_chan; 512 513 newchannel->device_obj->device_id = newchannel->device_id; 514 /* 515 * Add the new device to the bus. This will kick off device-driver 516 * binding which eventually invokes the device driver's AddDevice() 517 * method. 518 */ 519 ret = vmbus_device_register(newchannel->device_obj); 520 521 if (ret != 0) { 522 pr_err("unable to add child device object (relid %d)\n", 523 newchannel->offermsg.child_relid); 524 kfree(newchannel->device_obj); 525 goto err_deq_chan; 526 } 527 528 newchannel->probe_done = true; 529 return; 530 531 err_deq_chan: 532 mutex_lock(&vmbus_connection.channel_mutex); 533 534 /* 535 * We need to set the flag, otherwise 536 * vmbus_onoffer_rescind() can be blocked. 537 */ 538 newchannel->probe_done = true; 539 540 if (primary_channel == NULL) 541 list_del(&newchannel->listentry); 542 else 543 list_del(&newchannel->sc_list); 544 545 /* vmbus_process_offer() has mapped the channel. */ 546 vmbus_channel_unmap_relid(newchannel); 547 548 mutex_unlock(&vmbus_connection.channel_mutex); 549 550 vmbus_release_relid(newchannel->offermsg.child_relid); 551 552 free_channel(newchannel); 553 } 554 555 /* 556 * vmbus_process_offer - Process the offer by creating a channel/device 557 * associated with this offer 558 */ 559 static void vmbus_process_offer(struct vmbus_channel *newchannel) 560 { 561 struct vmbus_channel *channel; 562 struct workqueue_struct *wq; 563 bool fnew = true; 564 565 /* 566 * Synchronize vmbus_process_offer() and CPU hotplugging: 567 * 568 * CPU1 CPU2 569 * 570 * [vmbus_process_offer()] [Hot removal of the CPU] 571 * 572 * CPU_READ_LOCK CPUS_WRITE_LOCK 573 * LOAD cpu_online_mask SEARCH chn_list 574 * STORE target_cpu LOAD target_cpu 575 * INSERT chn_list STORE cpu_online_mask 576 * CPUS_READ_UNLOCK CPUS_WRITE_UNLOCK 577 * 578 * Forbids: CPU1's LOAD from *not* seing CPU2's STORE && 579 * CPU2's SEARCH from *not* seeing CPU1's INSERT 580 * 581 * Forbids: CPU2's SEARCH from seeing CPU1's INSERT && 582 * CPU2's LOAD from *not* seing CPU1's STORE 583 */ 584 cpus_read_lock(); 585 586 /* 587 * Serializes the modifications of the chn_list list as well as 588 * the accesses to next_numa_node_id in init_vp_index(). 589 */ 590 mutex_lock(&vmbus_connection.channel_mutex); 591 592 init_vp_index(newchannel); 593 594 /* Remember the channels that should be cleaned up upon suspend. */ 595 if (is_hvsock_channel(newchannel) || is_sub_channel(newchannel)) 596 atomic_inc(&vmbus_connection.nr_chan_close_on_suspend); 597 598 /* 599 * Now that we have acquired the channel_mutex, 600 * we can release the potentially racing rescind thread. 601 */ 602 atomic_dec(&vmbus_connection.offer_in_progress); 603 604 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) { 605 if (guid_equal(&channel->offermsg.offer.if_type, 606 &newchannel->offermsg.offer.if_type) && 607 guid_equal(&channel->offermsg.offer.if_instance, 608 &newchannel->offermsg.offer.if_instance)) { 609 fnew = false; 610 break; 611 } 612 } 613 614 if (fnew) { 615 list_add_tail(&newchannel->listentry, 616 &vmbus_connection.chn_list); 617 } else { 618 /* 619 * Check to see if this is a valid sub-channel. 620 */ 621 if (newchannel->offermsg.offer.sub_channel_index == 0) { 622 mutex_unlock(&vmbus_connection.channel_mutex); 623 /* 624 * Don't call free_channel(), because newchannel->kobj 625 * is not initialized yet. 626 */ 627 kfree(newchannel); 628 WARN_ON_ONCE(1); 629 return; 630 } 631 /* 632 * Process the sub-channel. 633 */ 634 newchannel->primary_channel = channel; 635 list_add_tail(&newchannel->sc_list, &channel->sc_list); 636 } 637 638 vmbus_channel_map_relid(newchannel); 639 640 mutex_unlock(&vmbus_connection.channel_mutex); 641 cpus_read_unlock(); 642 643 /* 644 * vmbus_process_offer() mustn't call channel->sc_creation_callback() 645 * directly for sub-channels, because sc_creation_callback() -> 646 * vmbus_open() may never get the host's response to the 647 * OPEN_CHANNEL message (the host may rescind a channel at any time, 648 * e.g. in the case of hot removing a NIC), and vmbus_onoffer_rescind() 649 * may not wake up the vmbus_open() as it's blocked due to a non-zero 650 * vmbus_connection.offer_in_progress, and finally we have a deadlock. 651 * 652 * The above is also true for primary channels, if the related device 653 * drivers use sync probing mode by default. 654 * 655 * And, usually the handling of primary channels and sub-channels can 656 * depend on each other, so we should offload them to different 657 * workqueues to avoid possible deadlock, e.g. in sync-probing mode, 658 * NIC1's netvsc_subchan_work() can race with NIC2's netvsc_probe() -> 659 * rtnl_lock(), and causes deadlock: the former gets the rtnl_lock 660 * and waits for all the sub-channels to appear, but the latter 661 * can't get the rtnl_lock and this blocks the handling of 662 * sub-channels. 663 */ 664 INIT_WORK(&newchannel->add_channel_work, vmbus_add_channel_work); 665 wq = fnew ? vmbus_connection.handle_primary_chan_wq : 666 vmbus_connection.handle_sub_chan_wq; 667 queue_work(wq, &newchannel->add_channel_work); 668 } 669 670 /* 671 * We use this state to statically distribute the channel interrupt load. 672 */ 673 static int next_numa_node_id; 674 675 /* 676 * Starting with Win8, we can statically distribute the incoming 677 * channel interrupt load by binding a channel to VCPU. 678 * 679 * For pre-win8 hosts or non-performance critical channels we assign the 680 * VMBUS_CONNECT_CPU. 681 * 682 * Starting with win8, performance critical channels will be distributed 683 * evenly among all the available NUMA nodes. Once the node is assigned, 684 * we will assign the CPU based on a simple round robin scheme. 685 */ 686 static void init_vp_index(struct vmbus_channel *channel) 687 { 688 bool perf_chn = hv_is_perf_channel(channel); 689 cpumask_var_t available_mask; 690 struct cpumask *alloced_mask; 691 u32 target_cpu; 692 int numa_node; 693 694 if ((vmbus_proto_version == VERSION_WS2008) || 695 (vmbus_proto_version == VERSION_WIN7) || (!perf_chn) || 696 !alloc_cpumask_var(&available_mask, GFP_KERNEL)) { 697 /* 698 * Prior to win8, all channel interrupts are 699 * delivered on VMBUS_CONNECT_CPU. 700 * Also if the channel is not a performance critical 701 * channel, bind it to VMBUS_CONNECT_CPU. 702 * In case alloc_cpumask_var() fails, bind it to 703 * VMBUS_CONNECT_CPU. 704 */ 705 channel->target_cpu = VMBUS_CONNECT_CPU; 706 if (perf_chn) 707 hv_set_alloced_cpu(VMBUS_CONNECT_CPU); 708 return; 709 } 710 711 while (true) { 712 numa_node = next_numa_node_id++; 713 if (numa_node == nr_node_ids) { 714 next_numa_node_id = 0; 715 continue; 716 } 717 if (cpumask_empty(cpumask_of_node(numa_node))) 718 continue; 719 break; 720 } 721 alloced_mask = &hv_context.hv_numa_map[numa_node]; 722 723 if (cpumask_weight(alloced_mask) == 724 cpumask_weight(cpumask_of_node(numa_node))) { 725 /* 726 * We have cycled through all the CPUs in the node; 727 * reset the alloced map. 728 */ 729 cpumask_clear(alloced_mask); 730 } 731 732 cpumask_xor(available_mask, alloced_mask, cpumask_of_node(numa_node)); 733 734 target_cpu = cpumask_first(available_mask); 735 cpumask_set_cpu(target_cpu, alloced_mask); 736 737 channel->target_cpu = target_cpu; 738 739 free_cpumask_var(available_mask); 740 } 741 742 static void vmbus_wait_for_unload(void) 743 { 744 int cpu; 745 void *page_addr; 746 struct hv_message *msg; 747 struct vmbus_channel_message_header *hdr; 748 u32 message_type, i; 749 750 /* 751 * CHANNELMSG_UNLOAD_RESPONSE is always delivered to the CPU which was 752 * used for initial contact or to CPU0 depending on host version. When 753 * we're crashing on a different CPU let's hope that IRQ handler on 754 * the cpu which receives CHANNELMSG_UNLOAD_RESPONSE is still 755 * functional and vmbus_unload_response() will complete 756 * vmbus_connection.unload_event. If not, the last thing we can do is 757 * read message pages for all CPUs directly. 758 * 759 * Wait no more than 10 seconds so that the panic path can't get 760 * hung forever in case the response message isn't seen. 761 */ 762 for (i = 0; i < 1000; i++) { 763 if (completion_done(&vmbus_connection.unload_event)) 764 break; 765 766 for_each_online_cpu(cpu) { 767 struct hv_per_cpu_context *hv_cpu 768 = per_cpu_ptr(hv_context.cpu_context, cpu); 769 770 page_addr = hv_cpu->synic_message_page; 771 msg = (struct hv_message *)page_addr 772 + VMBUS_MESSAGE_SINT; 773 774 message_type = READ_ONCE(msg->header.message_type); 775 if (message_type == HVMSG_NONE) 776 continue; 777 778 hdr = (struct vmbus_channel_message_header *) 779 msg->u.payload; 780 781 if (hdr->msgtype == CHANNELMSG_UNLOAD_RESPONSE) 782 complete(&vmbus_connection.unload_event); 783 784 vmbus_signal_eom(msg, message_type); 785 } 786 787 mdelay(10); 788 } 789 790 /* 791 * We're crashing and already got the UNLOAD_RESPONSE, cleanup all 792 * maybe-pending messages on all CPUs to be able to receive new 793 * messages after we reconnect. 794 */ 795 for_each_online_cpu(cpu) { 796 struct hv_per_cpu_context *hv_cpu 797 = per_cpu_ptr(hv_context.cpu_context, cpu); 798 799 page_addr = hv_cpu->synic_message_page; 800 msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT; 801 msg->header.message_type = HVMSG_NONE; 802 } 803 } 804 805 /* 806 * vmbus_unload_response - Handler for the unload response. 807 */ 808 static void vmbus_unload_response(struct vmbus_channel_message_header *hdr) 809 { 810 /* 811 * This is a global event; just wakeup the waiting thread. 812 * Once we successfully unload, we can cleanup the monitor state. 813 */ 814 complete(&vmbus_connection.unload_event); 815 } 816 817 void vmbus_initiate_unload(bool crash) 818 { 819 struct vmbus_channel_message_header hdr; 820 821 if (xchg(&vmbus_connection.conn_state, DISCONNECTED) == DISCONNECTED) 822 return; 823 824 /* Pre-Win2012R2 hosts don't support reconnect */ 825 if (vmbus_proto_version < VERSION_WIN8_1) 826 return; 827 828 init_completion(&vmbus_connection.unload_event); 829 memset(&hdr, 0, sizeof(struct vmbus_channel_message_header)); 830 hdr.msgtype = CHANNELMSG_UNLOAD; 831 vmbus_post_msg(&hdr, sizeof(struct vmbus_channel_message_header), 832 !crash); 833 834 /* 835 * vmbus_initiate_unload() is also called on crash and the crash can be 836 * happening in an interrupt context, where scheduling is impossible. 837 */ 838 if (!crash) 839 wait_for_completion(&vmbus_connection.unload_event); 840 else 841 vmbus_wait_for_unload(); 842 } 843 844 static void check_ready_for_resume_event(void) 845 { 846 /* 847 * If all the old primary channels have been fixed up, then it's safe 848 * to resume. 849 */ 850 if (atomic_dec_and_test(&vmbus_connection.nr_chan_fixup_on_resume)) 851 complete(&vmbus_connection.ready_for_resume_event); 852 } 853 854 static void vmbus_setup_channel_state(struct vmbus_channel *channel, 855 struct vmbus_channel_offer_channel *offer) 856 { 857 /* 858 * Setup state for signalling the host. 859 */ 860 channel->sig_event = VMBUS_EVENT_CONNECTION_ID; 861 862 if (vmbus_proto_version != VERSION_WS2008) { 863 channel->is_dedicated_interrupt = 864 (offer->is_dedicated_interrupt != 0); 865 channel->sig_event = offer->connection_id; 866 } 867 868 memcpy(&channel->offermsg, offer, 869 sizeof(struct vmbus_channel_offer_channel)); 870 channel->monitor_grp = (u8)offer->monitorid / 32; 871 channel->monitor_bit = (u8)offer->monitorid % 32; 872 channel->device_id = hv_get_dev_type(channel); 873 } 874 875 /* 876 * find_primary_channel_by_offer - Get the channel object given the new offer. 877 * This is only used in the resume path of hibernation. 878 */ 879 static struct vmbus_channel * 880 find_primary_channel_by_offer(const struct vmbus_channel_offer_channel *offer) 881 { 882 struct vmbus_channel *channel = NULL, *iter; 883 const guid_t *inst1, *inst2; 884 885 /* Ignore sub-channel offers. */ 886 if (offer->offer.sub_channel_index != 0) 887 return NULL; 888 889 mutex_lock(&vmbus_connection.channel_mutex); 890 891 list_for_each_entry(iter, &vmbus_connection.chn_list, listentry) { 892 inst1 = &iter->offermsg.offer.if_instance; 893 inst2 = &offer->offer.if_instance; 894 895 if (guid_equal(inst1, inst2)) { 896 channel = iter; 897 break; 898 } 899 } 900 901 mutex_unlock(&vmbus_connection.channel_mutex); 902 903 return channel; 904 } 905 906 /* 907 * vmbus_onoffer - Handler for channel offers from vmbus in parent partition. 908 * 909 */ 910 static void vmbus_onoffer(struct vmbus_channel_message_header *hdr) 911 { 912 struct vmbus_channel_offer_channel *offer; 913 struct vmbus_channel *oldchannel, *newchannel; 914 size_t offer_sz; 915 916 offer = (struct vmbus_channel_offer_channel *)hdr; 917 918 trace_vmbus_onoffer(offer); 919 920 oldchannel = find_primary_channel_by_offer(offer); 921 922 if (oldchannel != NULL) { 923 /* 924 * We're resuming from hibernation: all the sub-channel and 925 * hv_sock channels we had before the hibernation should have 926 * been cleaned up, and now we must be seeing a re-offered 927 * primary channel that we had before the hibernation. 928 */ 929 930 /* 931 * { Initially: channel relid = INVALID_RELID, 932 * channels[valid_relid] = NULL } 933 * 934 * CPU1 CPU2 935 * 936 * [vmbus_onoffer()] [vmbus_device_release()] 937 * 938 * LOCK channel_mutex LOCK channel_mutex 939 * STORE channel relid = valid_relid LOAD r1 = channel relid 940 * MAP_RELID channel if (r1 != INVALID_RELID) 941 * UNLOCK channel_mutex UNMAP_RELID channel 942 * UNLOCK channel_mutex 943 * 944 * Forbids: r1 == valid_relid && 945 * channels[valid_relid] == channel 946 * 947 * Note. r1 can be INVALID_RELID only for an hv_sock channel. 948 * None of the hv_sock channels which were present before the 949 * suspend are re-offered upon the resume. See the WARN_ON() 950 * in hv_process_channel_removal(). 951 */ 952 mutex_lock(&vmbus_connection.channel_mutex); 953 954 atomic_dec(&vmbus_connection.offer_in_progress); 955 956 WARN_ON(oldchannel->offermsg.child_relid != INVALID_RELID); 957 /* Fix up the relid. */ 958 oldchannel->offermsg.child_relid = offer->child_relid; 959 960 offer_sz = sizeof(*offer); 961 if (memcmp(offer, &oldchannel->offermsg, offer_sz) != 0) { 962 /* 963 * This is not an error, since the host can also change 964 * the other field(s) of the offer, e.g. on WS RS5 965 * (Build 17763), the offer->connection_id of the 966 * Mellanox VF vmbus device can change when the host 967 * reoffers the device upon resume. 968 */ 969 pr_debug("vmbus offer changed: relid=%d\n", 970 offer->child_relid); 971 972 print_hex_dump_debug("Old vmbus offer: ", 973 DUMP_PREFIX_OFFSET, 16, 4, 974 &oldchannel->offermsg, offer_sz, 975 false); 976 print_hex_dump_debug("New vmbus offer: ", 977 DUMP_PREFIX_OFFSET, 16, 4, 978 offer, offer_sz, false); 979 980 /* Fix up the old channel. */ 981 vmbus_setup_channel_state(oldchannel, offer); 982 } 983 984 /* Add the channel back to the array of channels. */ 985 vmbus_channel_map_relid(oldchannel); 986 check_ready_for_resume_event(); 987 988 mutex_unlock(&vmbus_connection.channel_mutex); 989 return; 990 } 991 992 /* Allocate the channel object and save this offer. */ 993 newchannel = alloc_channel(); 994 if (!newchannel) { 995 vmbus_release_relid(offer->child_relid); 996 atomic_dec(&vmbus_connection.offer_in_progress); 997 pr_err("Unable to allocate channel object\n"); 998 return; 999 } 1000 1001 vmbus_setup_channel_state(newchannel, offer); 1002 1003 vmbus_process_offer(newchannel); 1004 } 1005 1006 static void check_ready_for_suspend_event(void) 1007 { 1008 /* 1009 * If all the sub-channels or hv_sock channels have been cleaned up, 1010 * then it's safe to suspend. 1011 */ 1012 if (atomic_dec_and_test(&vmbus_connection.nr_chan_close_on_suspend)) 1013 complete(&vmbus_connection.ready_for_suspend_event); 1014 } 1015 1016 /* 1017 * vmbus_onoffer_rescind - Rescind offer handler. 1018 * 1019 * We queue a work item to process this offer synchronously 1020 */ 1021 static void vmbus_onoffer_rescind(struct vmbus_channel_message_header *hdr) 1022 { 1023 struct vmbus_channel_rescind_offer *rescind; 1024 struct vmbus_channel *channel; 1025 struct device *dev; 1026 bool clean_up_chan_for_suspend; 1027 1028 rescind = (struct vmbus_channel_rescind_offer *)hdr; 1029 1030 trace_vmbus_onoffer_rescind(rescind); 1031 1032 /* 1033 * The offer msg and the corresponding rescind msg 1034 * from the host are guranteed to be ordered - 1035 * offer comes in first and then the rescind. 1036 * Since we process these events in work elements, 1037 * and with preemption, we may end up processing 1038 * the events out of order. We rely on the synchronization 1039 * provided by offer_in_progress and by channel_mutex for 1040 * ordering these events: 1041 * 1042 * { Initially: offer_in_progress = 1 } 1043 * 1044 * CPU1 CPU2 1045 * 1046 * [vmbus_onoffer()] [vmbus_onoffer_rescind()] 1047 * 1048 * LOCK channel_mutex WAIT_ON offer_in_progress == 0 1049 * DECREMENT offer_in_progress LOCK channel_mutex 1050 * STORE channels[] LOAD channels[] 1051 * UNLOCK channel_mutex UNLOCK channel_mutex 1052 * 1053 * Forbids: CPU2's LOAD from *not* seeing CPU1's STORE 1054 */ 1055 1056 while (atomic_read(&vmbus_connection.offer_in_progress) != 0) { 1057 /* 1058 * We wait here until any channel offer is currently 1059 * being processed. 1060 */ 1061 msleep(1); 1062 } 1063 1064 mutex_lock(&vmbus_connection.channel_mutex); 1065 channel = relid2channel(rescind->child_relid); 1066 if (channel != NULL) { 1067 /* 1068 * Guarantee that no other instance of vmbus_onoffer_rescind() 1069 * has got a reference to the channel object. Synchronize on 1070 * &vmbus_connection.channel_mutex. 1071 */ 1072 if (channel->rescind_ref) { 1073 mutex_unlock(&vmbus_connection.channel_mutex); 1074 return; 1075 } 1076 channel->rescind_ref = true; 1077 } 1078 mutex_unlock(&vmbus_connection.channel_mutex); 1079 1080 if (channel == NULL) { 1081 /* 1082 * We failed in processing the offer message; 1083 * we would have cleaned up the relid in that 1084 * failure path. 1085 */ 1086 return; 1087 } 1088 1089 clean_up_chan_for_suspend = is_hvsock_channel(channel) || 1090 is_sub_channel(channel); 1091 /* 1092 * Before setting channel->rescind in vmbus_rescind_cleanup(), we 1093 * should make sure the channel callback is not running any more. 1094 */ 1095 vmbus_reset_channel_cb(channel); 1096 1097 /* 1098 * Now wait for offer handling to complete. 1099 */ 1100 vmbus_rescind_cleanup(channel); 1101 while (READ_ONCE(channel->probe_done) == false) { 1102 /* 1103 * We wait here until any channel offer is currently 1104 * being processed. 1105 */ 1106 msleep(1); 1107 } 1108 1109 /* 1110 * At this point, the rescind handling can proceed safely. 1111 */ 1112 1113 if (channel->device_obj) { 1114 if (channel->chn_rescind_callback) { 1115 channel->chn_rescind_callback(channel); 1116 1117 if (clean_up_chan_for_suspend) 1118 check_ready_for_suspend_event(); 1119 1120 return; 1121 } 1122 /* 1123 * We will have to unregister this device from the 1124 * driver core. 1125 */ 1126 dev = get_device(&channel->device_obj->device); 1127 if (dev) { 1128 vmbus_device_unregister(channel->device_obj); 1129 put_device(dev); 1130 } 1131 } else if (channel->primary_channel != NULL) { 1132 /* 1133 * Sub-channel is being rescinded. Following is the channel 1134 * close sequence when initiated from the driveri (refer to 1135 * vmbus_close() for details): 1136 * 1. Close all sub-channels first 1137 * 2. Then close the primary channel. 1138 */ 1139 mutex_lock(&vmbus_connection.channel_mutex); 1140 if (channel->state == CHANNEL_OPEN_STATE) { 1141 /* 1142 * The channel is currently not open; 1143 * it is safe for us to cleanup the channel. 1144 */ 1145 hv_process_channel_removal(channel); 1146 } else { 1147 complete(&channel->rescind_event); 1148 } 1149 mutex_unlock(&vmbus_connection.channel_mutex); 1150 } 1151 1152 /* The "channel" may have been freed. Do not access it any longer. */ 1153 1154 if (clean_up_chan_for_suspend) 1155 check_ready_for_suspend_event(); 1156 } 1157 1158 void vmbus_hvsock_device_unregister(struct vmbus_channel *channel) 1159 { 1160 BUG_ON(!is_hvsock_channel(channel)); 1161 1162 /* We always get a rescind msg when a connection is closed. */ 1163 while (!READ_ONCE(channel->probe_done) || !READ_ONCE(channel->rescind)) 1164 msleep(1); 1165 1166 vmbus_device_unregister(channel->device_obj); 1167 } 1168 EXPORT_SYMBOL_GPL(vmbus_hvsock_device_unregister); 1169 1170 1171 /* 1172 * vmbus_onoffers_delivered - 1173 * This is invoked when all offers have been delivered. 1174 * 1175 * Nothing to do here. 1176 */ 1177 static void vmbus_onoffers_delivered( 1178 struct vmbus_channel_message_header *hdr) 1179 { 1180 } 1181 1182 /* 1183 * vmbus_onopen_result - Open result handler. 1184 * 1185 * This is invoked when we received a response to our channel open request. 1186 * Find the matching request, copy the response and signal the requesting 1187 * thread. 1188 */ 1189 static void vmbus_onopen_result(struct vmbus_channel_message_header *hdr) 1190 { 1191 struct vmbus_channel_open_result *result; 1192 struct vmbus_channel_msginfo *msginfo; 1193 struct vmbus_channel_message_header *requestheader; 1194 struct vmbus_channel_open_channel *openmsg; 1195 unsigned long flags; 1196 1197 result = (struct vmbus_channel_open_result *)hdr; 1198 1199 trace_vmbus_onopen_result(result); 1200 1201 /* 1202 * Find the open msg, copy the result and signal/unblock the wait event 1203 */ 1204 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); 1205 1206 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list, 1207 msglistentry) { 1208 requestheader = 1209 (struct vmbus_channel_message_header *)msginfo->msg; 1210 1211 if (requestheader->msgtype == CHANNELMSG_OPENCHANNEL) { 1212 openmsg = 1213 (struct vmbus_channel_open_channel *)msginfo->msg; 1214 if (openmsg->child_relid == result->child_relid && 1215 openmsg->openid == result->openid) { 1216 memcpy(&msginfo->response.open_result, 1217 result, 1218 sizeof( 1219 struct vmbus_channel_open_result)); 1220 complete(&msginfo->waitevent); 1221 break; 1222 } 1223 } 1224 } 1225 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); 1226 } 1227 1228 /* 1229 * vmbus_ongpadl_created - GPADL created handler. 1230 * 1231 * This is invoked when we received a response to our gpadl create request. 1232 * Find the matching request, copy the response and signal the requesting 1233 * thread. 1234 */ 1235 static void vmbus_ongpadl_created(struct vmbus_channel_message_header *hdr) 1236 { 1237 struct vmbus_channel_gpadl_created *gpadlcreated; 1238 struct vmbus_channel_msginfo *msginfo; 1239 struct vmbus_channel_message_header *requestheader; 1240 struct vmbus_channel_gpadl_header *gpadlheader; 1241 unsigned long flags; 1242 1243 gpadlcreated = (struct vmbus_channel_gpadl_created *)hdr; 1244 1245 trace_vmbus_ongpadl_created(gpadlcreated); 1246 1247 /* 1248 * Find the establish msg, copy the result and signal/unblock the wait 1249 * event 1250 */ 1251 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); 1252 1253 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list, 1254 msglistentry) { 1255 requestheader = 1256 (struct vmbus_channel_message_header *)msginfo->msg; 1257 1258 if (requestheader->msgtype == CHANNELMSG_GPADL_HEADER) { 1259 gpadlheader = 1260 (struct vmbus_channel_gpadl_header *)requestheader; 1261 1262 if ((gpadlcreated->child_relid == 1263 gpadlheader->child_relid) && 1264 (gpadlcreated->gpadl == gpadlheader->gpadl)) { 1265 memcpy(&msginfo->response.gpadl_created, 1266 gpadlcreated, 1267 sizeof( 1268 struct vmbus_channel_gpadl_created)); 1269 complete(&msginfo->waitevent); 1270 break; 1271 } 1272 } 1273 } 1274 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); 1275 } 1276 1277 /* 1278 * vmbus_ongpadl_torndown - GPADL torndown handler. 1279 * 1280 * This is invoked when we received a response to our gpadl teardown request. 1281 * Find the matching request, copy the response and signal the requesting 1282 * thread. 1283 */ 1284 static void vmbus_ongpadl_torndown( 1285 struct vmbus_channel_message_header *hdr) 1286 { 1287 struct vmbus_channel_gpadl_torndown *gpadl_torndown; 1288 struct vmbus_channel_msginfo *msginfo; 1289 struct vmbus_channel_message_header *requestheader; 1290 struct vmbus_channel_gpadl_teardown *gpadl_teardown; 1291 unsigned long flags; 1292 1293 gpadl_torndown = (struct vmbus_channel_gpadl_torndown *)hdr; 1294 1295 trace_vmbus_ongpadl_torndown(gpadl_torndown); 1296 1297 /* 1298 * Find the open msg, copy the result and signal/unblock the wait event 1299 */ 1300 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); 1301 1302 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list, 1303 msglistentry) { 1304 requestheader = 1305 (struct vmbus_channel_message_header *)msginfo->msg; 1306 1307 if (requestheader->msgtype == CHANNELMSG_GPADL_TEARDOWN) { 1308 gpadl_teardown = 1309 (struct vmbus_channel_gpadl_teardown *)requestheader; 1310 1311 if (gpadl_torndown->gpadl == gpadl_teardown->gpadl) { 1312 memcpy(&msginfo->response.gpadl_torndown, 1313 gpadl_torndown, 1314 sizeof( 1315 struct vmbus_channel_gpadl_torndown)); 1316 complete(&msginfo->waitevent); 1317 break; 1318 } 1319 } 1320 } 1321 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); 1322 } 1323 1324 /* 1325 * vmbus_onversion_response - Version response handler 1326 * 1327 * This is invoked when we received a response to our initiate contact request. 1328 * Find the matching request, copy the response and signal the requesting 1329 * thread. 1330 */ 1331 static void vmbus_onversion_response( 1332 struct vmbus_channel_message_header *hdr) 1333 { 1334 struct vmbus_channel_msginfo *msginfo; 1335 struct vmbus_channel_message_header *requestheader; 1336 struct vmbus_channel_version_response *version_response; 1337 unsigned long flags; 1338 1339 version_response = (struct vmbus_channel_version_response *)hdr; 1340 1341 trace_vmbus_onversion_response(version_response); 1342 1343 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); 1344 1345 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list, 1346 msglistentry) { 1347 requestheader = 1348 (struct vmbus_channel_message_header *)msginfo->msg; 1349 1350 if (requestheader->msgtype == 1351 CHANNELMSG_INITIATE_CONTACT) { 1352 memcpy(&msginfo->response.version_response, 1353 version_response, 1354 sizeof(struct vmbus_channel_version_response)); 1355 complete(&msginfo->waitevent); 1356 } 1357 } 1358 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); 1359 } 1360 1361 /* Channel message dispatch table */ 1362 const struct vmbus_channel_message_table_entry 1363 channel_message_table[CHANNELMSG_COUNT] = { 1364 { CHANNELMSG_INVALID, 0, NULL, 0}, 1365 { CHANNELMSG_OFFERCHANNEL, 0, vmbus_onoffer, 1366 sizeof(struct vmbus_channel_offer_channel)}, 1367 { CHANNELMSG_RESCIND_CHANNELOFFER, 0, vmbus_onoffer_rescind, 1368 sizeof(struct vmbus_channel_rescind_offer) }, 1369 { CHANNELMSG_REQUESTOFFERS, 0, NULL, 0}, 1370 { CHANNELMSG_ALLOFFERS_DELIVERED, 1, vmbus_onoffers_delivered, 0}, 1371 { CHANNELMSG_OPENCHANNEL, 0, NULL, 0}, 1372 { CHANNELMSG_OPENCHANNEL_RESULT, 1, vmbus_onopen_result, 1373 sizeof(struct vmbus_channel_open_result)}, 1374 { CHANNELMSG_CLOSECHANNEL, 0, NULL, 0}, 1375 { CHANNELMSG_GPADL_HEADER, 0, NULL, 0}, 1376 { CHANNELMSG_GPADL_BODY, 0, NULL, 0}, 1377 { CHANNELMSG_GPADL_CREATED, 1, vmbus_ongpadl_created, 1378 sizeof(struct vmbus_channel_gpadl_created)}, 1379 { CHANNELMSG_GPADL_TEARDOWN, 0, NULL, 0}, 1380 { CHANNELMSG_GPADL_TORNDOWN, 1, vmbus_ongpadl_torndown, 1381 sizeof(struct vmbus_channel_gpadl_torndown) }, 1382 { CHANNELMSG_RELID_RELEASED, 0, NULL, 0}, 1383 { CHANNELMSG_INITIATE_CONTACT, 0, NULL, 0}, 1384 { CHANNELMSG_VERSION_RESPONSE, 1, vmbus_onversion_response, 1385 sizeof(struct vmbus_channel_version_response)}, 1386 { CHANNELMSG_UNLOAD, 0, NULL, 0}, 1387 { CHANNELMSG_UNLOAD_RESPONSE, 1, vmbus_unload_response, 0}, 1388 { CHANNELMSG_18, 0, NULL, 0}, 1389 { CHANNELMSG_19, 0, NULL, 0}, 1390 { CHANNELMSG_20, 0, NULL, 0}, 1391 { CHANNELMSG_TL_CONNECT_REQUEST, 0, NULL, 0}, 1392 { CHANNELMSG_MODIFYCHANNEL, 0, NULL, 0}, 1393 { CHANNELMSG_TL_CONNECT_RESULT, 0, NULL, 0}, 1394 }; 1395 1396 /* 1397 * vmbus_onmessage - Handler for channel protocol messages. 1398 * 1399 * This is invoked in the vmbus worker thread context. 1400 */ 1401 void vmbus_onmessage(struct vmbus_channel_message_header *hdr) 1402 { 1403 trace_vmbus_on_message(hdr); 1404 1405 /* 1406 * vmbus_on_msg_dpc() makes sure the hdr->msgtype here can not go 1407 * out of bound and the message_handler pointer can not be NULL. 1408 */ 1409 channel_message_table[hdr->msgtype].message_handler(hdr); 1410 } 1411 1412 /* 1413 * vmbus_request_offers - Send a request to get all our pending offers. 1414 */ 1415 int vmbus_request_offers(void) 1416 { 1417 struct vmbus_channel_message_header *msg; 1418 struct vmbus_channel_msginfo *msginfo; 1419 int ret; 1420 1421 msginfo = kmalloc(sizeof(*msginfo) + 1422 sizeof(struct vmbus_channel_message_header), 1423 GFP_KERNEL); 1424 if (!msginfo) 1425 return -ENOMEM; 1426 1427 msg = (struct vmbus_channel_message_header *)msginfo->msg; 1428 1429 msg->msgtype = CHANNELMSG_REQUESTOFFERS; 1430 1431 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_message_header), 1432 true); 1433 1434 trace_vmbus_request_offers(ret); 1435 1436 if (ret != 0) { 1437 pr_err("Unable to request offers - %d\n", ret); 1438 1439 goto cleanup; 1440 } 1441 1442 cleanup: 1443 kfree(msginfo); 1444 1445 return ret; 1446 } 1447 1448 static void invoke_sc_cb(struct vmbus_channel *primary_channel) 1449 { 1450 struct list_head *cur, *tmp; 1451 struct vmbus_channel *cur_channel; 1452 1453 if (primary_channel->sc_creation_callback == NULL) 1454 return; 1455 1456 list_for_each_safe(cur, tmp, &primary_channel->sc_list) { 1457 cur_channel = list_entry(cur, struct vmbus_channel, sc_list); 1458 1459 primary_channel->sc_creation_callback(cur_channel); 1460 } 1461 } 1462 1463 void vmbus_set_sc_create_callback(struct vmbus_channel *primary_channel, 1464 void (*sc_cr_cb)(struct vmbus_channel *new_sc)) 1465 { 1466 primary_channel->sc_creation_callback = sc_cr_cb; 1467 } 1468 EXPORT_SYMBOL_GPL(vmbus_set_sc_create_callback); 1469 1470 bool vmbus_are_subchannels_present(struct vmbus_channel *primary) 1471 { 1472 bool ret; 1473 1474 ret = !list_empty(&primary->sc_list); 1475 1476 if (ret) { 1477 /* 1478 * Invoke the callback on sub-channel creation. 1479 * This will present a uniform interface to the 1480 * clients. 1481 */ 1482 invoke_sc_cb(primary); 1483 } 1484 1485 return ret; 1486 } 1487 EXPORT_SYMBOL_GPL(vmbus_are_subchannels_present); 1488 1489 void vmbus_set_chn_rescind_callback(struct vmbus_channel *channel, 1490 void (*chn_rescind_cb)(struct vmbus_channel *)) 1491 { 1492 channel->chn_rescind_callback = chn_rescind_cb; 1493 } 1494 EXPORT_SYMBOL_GPL(vmbus_set_chn_rescind_callback); 1495