1 /* 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> 20 */ 21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 22 23 #include <linux/kernel.h> 24 #include <linux/sched.h> 25 #include <linux/wait.h> 26 #include <linux/mm.h> 27 #include <linux/slab.h> 28 #include <linux/list.h> 29 #include <linux/module.h> 30 #include <linux/completion.h> 31 #include <linux/hyperv.h> 32 33 #include "hyperv_vmbus.h" 34 35 struct vmbus_channel_message_table_entry { 36 enum vmbus_channel_message_type message_type; 37 void (*message_handler)(struct vmbus_channel_message_header *msg); 38 }; 39 40 41 /** 42 * vmbus_prep_negotiate_resp() - Create default response for Hyper-V Negotiate message 43 * @icmsghdrp: Pointer to msg header structure 44 * @icmsg_negotiate: Pointer to negotiate message structure 45 * @buf: Raw buffer channel data 46 * 47 * @icmsghdrp is of type &struct icmsg_hdr. 48 * @negop is of type &struct icmsg_negotiate. 49 * Set up and fill in default negotiate response message. 50 * 51 * The fw_version specifies the framework version that 52 * we can support and srv_version specifies the service 53 * version we can support. 54 * 55 * Mainly used by Hyper-V drivers. 56 */ 57 bool vmbus_prep_negotiate_resp(struct icmsg_hdr *icmsghdrp, 58 struct icmsg_negotiate *negop, u8 *buf, 59 int fw_version, int srv_version) 60 { 61 int icframe_major, icframe_minor; 62 int icmsg_major, icmsg_minor; 63 int fw_major, fw_minor; 64 int srv_major, srv_minor; 65 int i; 66 bool found_match = false; 67 68 icmsghdrp->icmsgsize = 0x10; 69 fw_major = (fw_version >> 16); 70 fw_minor = (fw_version & 0xFFFF); 71 72 srv_major = (srv_version >> 16); 73 srv_minor = (srv_version & 0xFFFF); 74 75 negop = (struct icmsg_negotiate *)&buf[ 76 sizeof(struct vmbuspipe_hdr) + 77 sizeof(struct icmsg_hdr)]; 78 79 icframe_major = negop->icframe_vercnt; 80 icframe_minor = 0; 81 82 icmsg_major = negop->icmsg_vercnt; 83 icmsg_minor = 0; 84 85 /* 86 * Select the framework version number we will 87 * support. 88 */ 89 90 for (i = 0; i < negop->icframe_vercnt; i++) { 91 if ((negop->icversion_data[i].major == fw_major) && 92 (negop->icversion_data[i].minor == fw_minor)) { 93 icframe_major = negop->icversion_data[i].major; 94 icframe_minor = negop->icversion_data[i].minor; 95 found_match = true; 96 } 97 } 98 99 if (!found_match) 100 goto fw_error; 101 102 found_match = false; 103 104 for (i = negop->icframe_vercnt; 105 (i < negop->icframe_vercnt + negop->icmsg_vercnt); i++) { 106 if ((negop->icversion_data[i].major == srv_major) && 107 (negop->icversion_data[i].minor == srv_minor)) { 108 icmsg_major = negop->icversion_data[i].major; 109 icmsg_minor = negop->icversion_data[i].minor; 110 found_match = true; 111 } 112 } 113 114 /* 115 * Respond with the framework and service 116 * version numbers we can support. 117 */ 118 119 fw_error: 120 if (!found_match) { 121 negop->icframe_vercnt = 0; 122 negop->icmsg_vercnt = 0; 123 } else { 124 negop->icframe_vercnt = 1; 125 negop->icmsg_vercnt = 1; 126 } 127 128 negop->icversion_data[0].major = icframe_major; 129 negop->icversion_data[0].minor = icframe_minor; 130 negop->icversion_data[1].major = icmsg_major; 131 negop->icversion_data[1].minor = icmsg_minor; 132 return found_match; 133 } 134 135 EXPORT_SYMBOL_GPL(vmbus_prep_negotiate_resp); 136 137 /* 138 * alloc_channel - Allocate and initialize a vmbus channel object 139 */ 140 static struct vmbus_channel *alloc_channel(void) 141 { 142 static atomic_t chan_num = ATOMIC_INIT(0); 143 struct vmbus_channel *channel; 144 145 channel = kzalloc(sizeof(*channel), GFP_ATOMIC); 146 if (!channel) 147 return NULL; 148 149 channel->id = atomic_inc_return(&chan_num); 150 spin_lock_init(&channel->inbound_lock); 151 spin_lock_init(&channel->lock); 152 153 INIT_LIST_HEAD(&channel->sc_list); 154 INIT_LIST_HEAD(&channel->percpu_list); 155 156 channel->controlwq = alloc_workqueue("hv_vmbus_ctl/%d", WQ_MEM_RECLAIM, 157 1, channel->id); 158 if (!channel->controlwq) { 159 kfree(channel); 160 return NULL; 161 } 162 163 return channel; 164 } 165 166 /* 167 * release_hannel - Release the vmbus channel object itself 168 */ 169 static void release_channel(struct work_struct *work) 170 { 171 struct vmbus_channel *channel = container_of(work, 172 struct vmbus_channel, 173 work); 174 175 destroy_workqueue(channel->controlwq); 176 177 kfree(channel); 178 } 179 180 /* 181 * free_channel - Release the resources used by the vmbus channel object 182 */ 183 static void free_channel(struct vmbus_channel *channel) 184 { 185 186 /* 187 * We have to release the channel's workqueue/thread in the vmbus's 188 * workqueue/thread context 189 * ie we can't destroy ourselves. 190 */ 191 INIT_WORK(&channel->work, release_channel); 192 queue_work(vmbus_connection.work_queue, &channel->work); 193 } 194 195 static void percpu_channel_enq(void *arg) 196 { 197 struct vmbus_channel *channel = arg; 198 int cpu = smp_processor_id(); 199 200 list_add_tail(&channel->percpu_list, &hv_context.percpu_list[cpu]); 201 } 202 203 static void percpu_channel_deq(void *arg) 204 { 205 struct vmbus_channel *channel = arg; 206 207 list_del(&channel->percpu_list); 208 } 209 210 211 void hv_process_channel_removal(struct vmbus_channel *channel, u32 relid) 212 { 213 struct vmbus_channel_relid_released msg; 214 unsigned long flags; 215 struct vmbus_channel *primary_channel; 216 217 memset(&msg, 0, sizeof(struct vmbus_channel_relid_released)); 218 msg.child_relid = relid; 219 msg.header.msgtype = CHANNELMSG_RELID_RELEASED; 220 vmbus_post_msg(&msg, sizeof(struct vmbus_channel_relid_released)); 221 222 if (channel == NULL) 223 return; 224 225 if (channel->target_cpu != get_cpu()) { 226 put_cpu(); 227 smp_call_function_single(channel->target_cpu, 228 percpu_channel_deq, channel, true); 229 } else { 230 percpu_channel_deq(channel); 231 put_cpu(); 232 } 233 234 if (channel->primary_channel == NULL) { 235 spin_lock_irqsave(&vmbus_connection.channel_lock, flags); 236 list_del(&channel->listentry); 237 spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags); 238 } else { 239 primary_channel = channel->primary_channel; 240 spin_lock_irqsave(&primary_channel->lock, flags); 241 list_del(&channel->sc_list); 242 spin_unlock_irqrestore(&primary_channel->lock, flags); 243 } 244 free_channel(channel); 245 } 246 247 /* 248 * vmbus_process_rescind_offer - 249 * Rescind the offer by initiating a device removal 250 */ 251 static void vmbus_process_rescind_offer(struct work_struct *work) 252 { 253 struct vmbus_channel *channel = container_of(work, 254 struct vmbus_channel, 255 work); 256 struct device *dev; 257 258 if (channel->device_obj) { 259 dev = get_device(&channel->device_obj->device); 260 if (dev) { 261 vmbus_device_unregister(channel->device_obj); 262 put_device(dev); 263 } 264 } else { 265 hv_process_channel_removal(channel, 266 channel->offermsg.child_relid); 267 } 268 } 269 270 void vmbus_free_channels(void) 271 { 272 struct vmbus_channel *channel; 273 274 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) { 275 vmbus_device_unregister(channel->device_obj); 276 free_channel(channel); 277 } 278 } 279 280 /* 281 * vmbus_process_offer - Process the offer by creating a channel/device 282 * associated with this offer 283 */ 284 static void vmbus_process_offer(struct work_struct *work) 285 { 286 struct vmbus_channel *newchannel = container_of(work, 287 struct vmbus_channel, 288 work); 289 struct vmbus_channel *channel; 290 bool fnew = true; 291 bool enq = false; 292 int ret; 293 unsigned long flags; 294 295 /* Make sure this is a new offer */ 296 spin_lock_irqsave(&vmbus_connection.channel_lock, flags); 297 298 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) { 299 if (!uuid_le_cmp(channel->offermsg.offer.if_type, 300 newchannel->offermsg.offer.if_type) && 301 !uuid_le_cmp(channel->offermsg.offer.if_instance, 302 newchannel->offermsg.offer.if_instance)) { 303 fnew = false; 304 break; 305 } 306 } 307 308 if (fnew) { 309 list_add_tail(&newchannel->listentry, 310 &vmbus_connection.chn_list); 311 enq = true; 312 } 313 314 spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags); 315 316 if (enq) { 317 if (newchannel->target_cpu != get_cpu()) { 318 put_cpu(); 319 smp_call_function_single(newchannel->target_cpu, 320 percpu_channel_enq, 321 newchannel, true); 322 } else { 323 percpu_channel_enq(newchannel); 324 put_cpu(); 325 } 326 } 327 if (!fnew) { 328 /* 329 * Check to see if this is a sub-channel. 330 */ 331 if (newchannel->offermsg.offer.sub_channel_index != 0) { 332 /* 333 * Process the sub-channel. 334 */ 335 newchannel->primary_channel = channel; 336 spin_lock_irqsave(&channel->lock, flags); 337 list_add_tail(&newchannel->sc_list, &channel->sc_list); 338 spin_unlock_irqrestore(&channel->lock, flags); 339 340 if (newchannel->target_cpu != get_cpu()) { 341 put_cpu(); 342 smp_call_function_single(newchannel->target_cpu, 343 percpu_channel_enq, 344 newchannel, true); 345 } else { 346 percpu_channel_enq(newchannel); 347 put_cpu(); 348 } 349 350 newchannel->state = CHANNEL_OPEN_STATE; 351 if (channel->sc_creation_callback != NULL) 352 channel->sc_creation_callback(newchannel); 353 354 goto done_init_rescind; 355 } 356 357 goto err_free_chan; 358 } 359 360 /* 361 * This state is used to indicate a successful open 362 * so that when we do close the channel normally, we 363 * can cleanup properly 364 */ 365 newchannel->state = CHANNEL_OPEN_STATE; 366 367 /* 368 * Start the process of binding this offer to the driver 369 * We need to set the DeviceObject field before calling 370 * vmbus_child_dev_add() 371 */ 372 newchannel->device_obj = vmbus_device_create( 373 &newchannel->offermsg.offer.if_type, 374 &newchannel->offermsg.offer.if_instance, 375 newchannel); 376 if (!newchannel->device_obj) 377 goto err_free_chan; 378 379 /* 380 * Add the new device to the bus. This will kick off device-driver 381 * binding which eventually invokes the device driver's AddDevice() 382 * method. 383 */ 384 ret = vmbus_device_register(newchannel->device_obj); 385 if (ret != 0) { 386 pr_err("unable to add child device object (relid %d)\n", 387 newchannel->offermsg.child_relid); 388 389 spin_lock_irqsave(&vmbus_connection.channel_lock, flags); 390 list_del(&newchannel->listentry); 391 spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags); 392 kfree(newchannel->device_obj); 393 goto err_free_chan; 394 } 395 done_init_rescind: 396 spin_lock_irqsave(&newchannel->lock, flags); 397 /* The next possible work is rescind handling */ 398 INIT_WORK(&newchannel->work, vmbus_process_rescind_offer); 399 /* Check if rescind offer was already received */ 400 if (newchannel->rescind) 401 queue_work(newchannel->controlwq, &newchannel->work); 402 spin_unlock_irqrestore(&newchannel->lock, flags); 403 return; 404 err_free_chan: 405 free_channel(newchannel); 406 } 407 408 enum { 409 IDE = 0, 410 SCSI, 411 NIC, 412 MAX_PERF_CHN, 413 }; 414 415 /* 416 * This is an array of device_ids (device types) that are performance critical. 417 * We attempt to distribute the interrupt load for these devices across 418 * all available CPUs. 419 */ 420 static const struct hv_vmbus_device_id hp_devs[] = { 421 /* IDE */ 422 { HV_IDE_GUID, }, 423 /* Storage - SCSI */ 424 { HV_SCSI_GUID, }, 425 /* Network */ 426 { HV_NIC_GUID, }, 427 /* NetworkDirect Guest RDMA */ 428 { HV_ND_GUID, }, 429 }; 430 431 432 /* 433 * We use this state to statically distribute the channel interrupt load. 434 */ 435 static u32 next_vp; 436 437 /* 438 * Starting with Win8, we can statically distribute the incoming 439 * channel interrupt load by binding a channel to VCPU. We 440 * implement here a simple round robin scheme for distributing 441 * the interrupt load. 442 * We will bind channels that are not performance critical to cpu 0 and 443 * performance critical channels (IDE, SCSI and Network) will be uniformly 444 * distributed across all available CPUs. 445 */ 446 static void init_vp_index(struct vmbus_channel *channel, const uuid_le *type_guid) 447 { 448 u32 cur_cpu; 449 int i; 450 bool perf_chn = false; 451 u32 max_cpus = num_online_cpus(); 452 453 for (i = IDE; i < MAX_PERF_CHN; i++) { 454 if (!memcmp(type_guid->b, hp_devs[i].guid, 455 sizeof(uuid_le))) { 456 perf_chn = true; 457 break; 458 } 459 } 460 if ((vmbus_proto_version == VERSION_WS2008) || 461 (vmbus_proto_version == VERSION_WIN7) || (!perf_chn)) { 462 /* 463 * Prior to win8, all channel interrupts are 464 * delivered on cpu 0. 465 * Also if the channel is not a performance critical 466 * channel, bind it to cpu 0. 467 */ 468 channel->target_cpu = 0; 469 channel->target_vp = 0; 470 return; 471 } 472 cur_cpu = (++next_vp % max_cpus); 473 channel->target_cpu = cur_cpu; 474 channel->target_vp = hv_context.vp_index[cur_cpu]; 475 } 476 477 /* 478 * vmbus_onoffer - Handler for channel offers from vmbus in parent partition. 479 * 480 */ 481 static void vmbus_onoffer(struct vmbus_channel_message_header *hdr) 482 { 483 struct vmbus_channel_offer_channel *offer; 484 struct vmbus_channel *newchannel; 485 486 offer = (struct vmbus_channel_offer_channel *)hdr; 487 488 /* Allocate the channel object and save this offer. */ 489 newchannel = alloc_channel(); 490 if (!newchannel) { 491 pr_err("Unable to allocate channel object\n"); 492 return; 493 } 494 495 /* 496 * By default we setup state to enable batched 497 * reading. A specific service can choose to 498 * disable this prior to opening the channel. 499 */ 500 newchannel->batched_reading = true; 501 502 /* 503 * Setup state for signalling the host. 504 */ 505 newchannel->sig_event = (struct hv_input_signal_event *) 506 (ALIGN((unsigned long) 507 &newchannel->sig_buf, 508 HV_HYPERCALL_PARAM_ALIGN)); 509 510 newchannel->sig_event->connectionid.asu32 = 0; 511 newchannel->sig_event->connectionid.u.id = VMBUS_EVENT_CONNECTION_ID; 512 newchannel->sig_event->flag_number = 0; 513 newchannel->sig_event->rsvdz = 0; 514 515 if (vmbus_proto_version != VERSION_WS2008) { 516 newchannel->is_dedicated_interrupt = 517 (offer->is_dedicated_interrupt != 0); 518 newchannel->sig_event->connectionid.u.id = 519 offer->connection_id; 520 } 521 522 init_vp_index(newchannel, &offer->offer.if_type); 523 524 memcpy(&newchannel->offermsg, offer, 525 sizeof(struct vmbus_channel_offer_channel)); 526 newchannel->monitor_grp = (u8)offer->monitorid / 32; 527 newchannel->monitor_bit = (u8)offer->monitorid % 32; 528 529 INIT_WORK(&newchannel->work, vmbus_process_offer); 530 queue_work(newchannel->controlwq, &newchannel->work); 531 } 532 533 /* 534 * vmbus_onoffer_rescind - Rescind offer handler. 535 * 536 * We queue a work item to process this offer synchronously 537 */ 538 static void vmbus_onoffer_rescind(struct vmbus_channel_message_header *hdr) 539 { 540 struct vmbus_channel_rescind_offer *rescind; 541 struct vmbus_channel *channel; 542 unsigned long flags; 543 544 rescind = (struct vmbus_channel_rescind_offer *)hdr; 545 channel = relid2channel(rescind->child_relid); 546 547 if (channel == NULL) 548 /* Just return here, no channel found */ 549 return; 550 551 spin_lock_irqsave(&channel->lock, flags); 552 channel->rescind = true; 553 /* 554 * channel->work.func != vmbus_process_rescind_offer means we are still 555 * processing offer request and the rescind offer processing should be 556 * postponed. It will be done at the very end of vmbus_process_offer() 557 * as rescind flag is being checked there. 558 */ 559 if (channel->work.func == vmbus_process_rescind_offer) 560 /* work is initialized for vmbus_process_rescind_offer() from 561 * vmbus_process_offer() where the channel got created */ 562 queue_work(channel->controlwq, &channel->work); 563 564 spin_unlock_irqrestore(&channel->lock, flags); 565 } 566 567 /* 568 * vmbus_onoffers_delivered - 569 * This is invoked when all offers have been delivered. 570 * 571 * Nothing to do here. 572 */ 573 static void vmbus_onoffers_delivered( 574 struct vmbus_channel_message_header *hdr) 575 { 576 } 577 578 /* 579 * vmbus_onopen_result - Open result handler. 580 * 581 * This is invoked when we received a response to our channel open request. 582 * Find the matching request, copy the response and signal the requesting 583 * thread. 584 */ 585 static void vmbus_onopen_result(struct vmbus_channel_message_header *hdr) 586 { 587 struct vmbus_channel_open_result *result; 588 struct vmbus_channel_msginfo *msginfo; 589 struct vmbus_channel_message_header *requestheader; 590 struct vmbus_channel_open_channel *openmsg; 591 unsigned long flags; 592 593 result = (struct vmbus_channel_open_result *)hdr; 594 595 /* 596 * Find the open msg, copy the result and signal/unblock the wait event 597 */ 598 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); 599 600 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list, 601 msglistentry) { 602 requestheader = 603 (struct vmbus_channel_message_header *)msginfo->msg; 604 605 if (requestheader->msgtype == CHANNELMSG_OPENCHANNEL) { 606 openmsg = 607 (struct vmbus_channel_open_channel *)msginfo->msg; 608 if (openmsg->child_relid == result->child_relid && 609 openmsg->openid == result->openid) { 610 memcpy(&msginfo->response.open_result, 611 result, 612 sizeof( 613 struct vmbus_channel_open_result)); 614 complete(&msginfo->waitevent); 615 break; 616 } 617 } 618 } 619 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); 620 } 621 622 /* 623 * vmbus_ongpadl_created - GPADL created handler. 624 * 625 * This is invoked when we received a response to our gpadl create request. 626 * Find the matching request, copy the response and signal the requesting 627 * thread. 628 */ 629 static void vmbus_ongpadl_created(struct vmbus_channel_message_header *hdr) 630 { 631 struct vmbus_channel_gpadl_created *gpadlcreated; 632 struct vmbus_channel_msginfo *msginfo; 633 struct vmbus_channel_message_header *requestheader; 634 struct vmbus_channel_gpadl_header *gpadlheader; 635 unsigned long flags; 636 637 gpadlcreated = (struct vmbus_channel_gpadl_created *)hdr; 638 639 /* 640 * Find the establish msg, copy the result and signal/unblock the wait 641 * event 642 */ 643 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); 644 645 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list, 646 msglistentry) { 647 requestheader = 648 (struct vmbus_channel_message_header *)msginfo->msg; 649 650 if (requestheader->msgtype == CHANNELMSG_GPADL_HEADER) { 651 gpadlheader = 652 (struct vmbus_channel_gpadl_header *)requestheader; 653 654 if ((gpadlcreated->child_relid == 655 gpadlheader->child_relid) && 656 (gpadlcreated->gpadl == gpadlheader->gpadl)) { 657 memcpy(&msginfo->response.gpadl_created, 658 gpadlcreated, 659 sizeof( 660 struct vmbus_channel_gpadl_created)); 661 complete(&msginfo->waitevent); 662 break; 663 } 664 } 665 } 666 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); 667 } 668 669 /* 670 * vmbus_ongpadl_torndown - GPADL torndown handler. 671 * 672 * This is invoked when we received a response to our gpadl teardown request. 673 * Find the matching request, copy the response and signal the requesting 674 * thread. 675 */ 676 static void vmbus_ongpadl_torndown( 677 struct vmbus_channel_message_header *hdr) 678 { 679 struct vmbus_channel_gpadl_torndown *gpadl_torndown; 680 struct vmbus_channel_msginfo *msginfo; 681 struct vmbus_channel_message_header *requestheader; 682 struct vmbus_channel_gpadl_teardown *gpadl_teardown; 683 unsigned long flags; 684 685 gpadl_torndown = (struct vmbus_channel_gpadl_torndown *)hdr; 686 687 /* 688 * Find the open msg, copy the result and signal/unblock the wait event 689 */ 690 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); 691 692 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list, 693 msglistentry) { 694 requestheader = 695 (struct vmbus_channel_message_header *)msginfo->msg; 696 697 if (requestheader->msgtype == CHANNELMSG_GPADL_TEARDOWN) { 698 gpadl_teardown = 699 (struct vmbus_channel_gpadl_teardown *)requestheader; 700 701 if (gpadl_torndown->gpadl == gpadl_teardown->gpadl) { 702 memcpy(&msginfo->response.gpadl_torndown, 703 gpadl_torndown, 704 sizeof( 705 struct vmbus_channel_gpadl_torndown)); 706 complete(&msginfo->waitevent); 707 break; 708 } 709 } 710 } 711 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); 712 } 713 714 /* 715 * vmbus_onversion_response - Version response handler 716 * 717 * This is invoked when we received a response to our initiate contact request. 718 * Find the matching request, copy the response and signal the requesting 719 * thread. 720 */ 721 static void vmbus_onversion_response( 722 struct vmbus_channel_message_header *hdr) 723 { 724 struct vmbus_channel_msginfo *msginfo; 725 struct vmbus_channel_message_header *requestheader; 726 struct vmbus_channel_version_response *version_response; 727 unsigned long flags; 728 729 version_response = (struct vmbus_channel_version_response *)hdr; 730 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); 731 732 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list, 733 msglistentry) { 734 requestheader = 735 (struct vmbus_channel_message_header *)msginfo->msg; 736 737 if (requestheader->msgtype == 738 CHANNELMSG_INITIATE_CONTACT) { 739 memcpy(&msginfo->response.version_response, 740 version_response, 741 sizeof(struct vmbus_channel_version_response)); 742 complete(&msginfo->waitevent); 743 } 744 } 745 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); 746 } 747 748 /* Channel message dispatch table */ 749 static struct vmbus_channel_message_table_entry 750 channel_message_table[CHANNELMSG_COUNT] = { 751 {CHANNELMSG_INVALID, NULL}, 752 {CHANNELMSG_OFFERCHANNEL, vmbus_onoffer}, 753 {CHANNELMSG_RESCIND_CHANNELOFFER, vmbus_onoffer_rescind}, 754 {CHANNELMSG_REQUESTOFFERS, NULL}, 755 {CHANNELMSG_ALLOFFERS_DELIVERED, vmbus_onoffers_delivered}, 756 {CHANNELMSG_OPENCHANNEL, NULL}, 757 {CHANNELMSG_OPENCHANNEL_RESULT, vmbus_onopen_result}, 758 {CHANNELMSG_CLOSECHANNEL, NULL}, 759 {CHANNELMSG_GPADL_HEADER, NULL}, 760 {CHANNELMSG_GPADL_BODY, NULL}, 761 {CHANNELMSG_GPADL_CREATED, vmbus_ongpadl_created}, 762 {CHANNELMSG_GPADL_TEARDOWN, NULL}, 763 {CHANNELMSG_GPADL_TORNDOWN, vmbus_ongpadl_torndown}, 764 {CHANNELMSG_RELID_RELEASED, NULL}, 765 {CHANNELMSG_INITIATE_CONTACT, NULL}, 766 {CHANNELMSG_VERSION_RESPONSE, vmbus_onversion_response}, 767 {CHANNELMSG_UNLOAD, NULL}, 768 }; 769 770 /* 771 * vmbus_onmessage - Handler for channel protocol messages. 772 * 773 * This is invoked in the vmbus worker thread context. 774 */ 775 void vmbus_onmessage(void *context) 776 { 777 struct hv_message *msg = context; 778 struct vmbus_channel_message_header *hdr; 779 int size; 780 781 hdr = (struct vmbus_channel_message_header *)msg->u.payload; 782 size = msg->header.payload_size; 783 784 if (hdr->msgtype >= CHANNELMSG_COUNT) { 785 pr_err("Received invalid channel message type %d size %d\n", 786 hdr->msgtype, size); 787 print_hex_dump_bytes("", DUMP_PREFIX_NONE, 788 (unsigned char *)msg->u.payload, size); 789 return; 790 } 791 792 if (channel_message_table[hdr->msgtype].message_handler) 793 channel_message_table[hdr->msgtype].message_handler(hdr); 794 else 795 pr_err("Unhandled channel message type %d\n", hdr->msgtype); 796 } 797 798 /* 799 * vmbus_request_offers - Send a request to get all our pending offers. 800 */ 801 int vmbus_request_offers(void) 802 { 803 struct vmbus_channel_message_header *msg; 804 struct vmbus_channel_msginfo *msginfo; 805 int ret; 806 unsigned long t; 807 808 msginfo = kmalloc(sizeof(*msginfo) + 809 sizeof(struct vmbus_channel_message_header), 810 GFP_KERNEL); 811 if (!msginfo) 812 return -ENOMEM; 813 814 init_completion(&msginfo->waitevent); 815 816 msg = (struct vmbus_channel_message_header *)msginfo->msg; 817 818 msg->msgtype = CHANNELMSG_REQUESTOFFERS; 819 820 821 ret = vmbus_post_msg(msg, 822 sizeof(struct vmbus_channel_message_header)); 823 if (ret != 0) { 824 pr_err("Unable to request offers - %d\n", ret); 825 826 goto cleanup; 827 } 828 829 t = wait_for_completion_timeout(&msginfo->waitevent, 5*HZ); 830 if (t == 0) { 831 ret = -ETIMEDOUT; 832 goto cleanup; 833 } 834 835 836 837 cleanup: 838 kfree(msginfo); 839 840 return ret; 841 } 842 843 /* 844 * Retrieve the (sub) channel on which to send an outgoing request. 845 * When a primary channel has multiple sub-channels, we choose a 846 * channel whose VCPU binding is closest to the VCPU on which 847 * this call is being made. 848 */ 849 struct vmbus_channel *vmbus_get_outgoing_channel(struct vmbus_channel *primary) 850 { 851 struct list_head *cur, *tmp; 852 int cur_cpu; 853 struct vmbus_channel *cur_channel; 854 struct vmbus_channel *outgoing_channel = primary; 855 int cpu_distance, new_cpu_distance; 856 857 if (list_empty(&primary->sc_list)) 858 return outgoing_channel; 859 860 cur_cpu = hv_context.vp_index[get_cpu()]; 861 put_cpu(); 862 list_for_each_safe(cur, tmp, &primary->sc_list) { 863 cur_channel = list_entry(cur, struct vmbus_channel, sc_list); 864 if (cur_channel->state != CHANNEL_OPENED_STATE) 865 continue; 866 867 if (cur_channel->target_vp == cur_cpu) 868 return cur_channel; 869 870 cpu_distance = ((outgoing_channel->target_vp > cur_cpu) ? 871 (outgoing_channel->target_vp - cur_cpu) : 872 (cur_cpu - outgoing_channel->target_vp)); 873 874 new_cpu_distance = ((cur_channel->target_vp > cur_cpu) ? 875 (cur_channel->target_vp - cur_cpu) : 876 (cur_cpu - cur_channel->target_vp)); 877 878 if (cpu_distance < new_cpu_distance) 879 continue; 880 881 outgoing_channel = cur_channel; 882 } 883 884 return outgoing_channel; 885 } 886 EXPORT_SYMBOL_GPL(vmbus_get_outgoing_channel); 887 888 static void invoke_sc_cb(struct vmbus_channel *primary_channel) 889 { 890 struct list_head *cur, *tmp; 891 struct vmbus_channel *cur_channel; 892 893 if (primary_channel->sc_creation_callback == NULL) 894 return; 895 896 list_for_each_safe(cur, tmp, &primary_channel->sc_list) { 897 cur_channel = list_entry(cur, struct vmbus_channel, sc_list); 898 899 primary_channel->sc_creation_callback(cur_channel); 900 } 901 } 902 903 void vmbus_set_sc_create_callback(struct vmbus_channel *primary_channel, 904 void (*sc_cr_cb)(struct vmbus_channel *new_sc)) 905 { 906 primary_channel->sc_creation_callback = sc_cr_cb; 907 } 908 EXPORT_SYMBOL_GPL(vmbus_set_sc_create_callback); 909 910 bool vmbus_are_subchannels_present(struct vmbus_channel *primary) 911 { 912 bool ret; 913 914 ret = !list_empty(&primary->sc_list); 915 916 if (ret) { 917 /* 918 * Invoke the callback on sub-channel creation. 919 * This will present a uniform interface to the 920 * clients. 921 */ 922 invoke_sc_cb(primary); 923 } 924 925 return ret; 926 } 927 EXPORT_SYMBOL_GPL(vmbus_are_subchannels_present); 928