1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) Microsoft Corporation. 4 * 5 * Author: 6 * Jake Oshins <jakeo@microsoft.com> 7 * 8 * This driver acts as a paravirtual front-end for PCI Express root buses. 9 * When a PCI Express function (either an entire device or an SR-IOV 10 * Virtual Function) is being passed through to the VM, this driver exposes 11 * a new bus to the guest VM. This is modeled as a root PCI bus because 12 * no bridges are being exposed to the VM. In fact, with a "Generation 2" 13 * VM within Hyper-V, there may seem to be no PCI bus at all in the VM 14 * until a device as been exposed using this driver. 15 * 16 * Each root PCI bus has its own PCI domain, which is called "Segment" in 17 * the PCI Firmware Specifications. Thus while each device passed through 18 * to the VM using this front-end will appear at "device 0", the domain will 19 * be unique. Typically, each bus will have one PCI function on it, though 20 * this driver does support more than one. 21 * 22 * In order to map the interrupts from the device through to the guest VM, 23 * this driver also implements an IRQ Domain, which handles interrupts (either 24 * MSI or MSI-X) associated with the functions on the bus. As interrupts are 25 * set up, torn down, or reaffined, this driver communicates with the 26 * underlying hypervisor to adjust the mappings in the I/O MMU so that each 27 * interrupt will be delivered to the correct virtual processor at the right 28 * vector. This driver does not support level-triggered (line-based) 29 * interrupts, and will report that the Interrupt Line register in the 30 * function's configuration space is zero. 31 * 32 * The rest of this driver mostly maps PCI concepts onto underlying Hyper-V 33 * facilities. For instance, the configuration space of a function exposed 34 * by Hyper-V is mapped into a single page of memory space, and the 35 * read and write handlers for config space must be aware of this mechanism. 36 * Similarly, device setup and teardown involves messages sent to and from 37 * the PCI back-end driver in Hyper-V. 38 */ 39 40 #include <linux/kernel.h> 41 #include <linux/module.h> 42 #include <linux/pci.h> 43 #include <linux/pci-ecam.h> 44 #include <linux/delay.h> 45 #include <linux/semaphore.h> 46 #include <linux/irqdomain.h> 47 #include <asm/irqdomain.h> 48 #include <asm/apic.h> 49 #include <linux/irq.h> 50 #include <linux/msi.h> 51 #include <linux/hyperv.h> 52 #include <linux/refcount.h> 53 #include <asm/mshyperv.h> 54 55 /* 56 * Protocol versions. The low word is the minor version, the high word the 57 * major version. 58 */ 59 60 #define PCI_MAKE_VERSION(major, minor) ((u32)(((major) << 16) | (minor))) 61 #define PCI_MAJOR_VERSION(version) ((u32)(version) >> 16) 62 #define PCI_MINOR_VERSION(version) ((u32)(version) & 0xff) 63 64 enum pci_protocol_version_t { 65 PCI_PROTOCOL_VERSION_1_1 = PCI_MAKE_VERSION(1, 1), /* Win10 */ 66 PCI_PROTOCOL_VERSION_1_2 = PCI_MAKE_VERSION(1, 2), /* RS1 */ 67 PCI_PROTOCOL_VERSION_1_3 = PCI_MAKE_VERSION(1, 3), /* Vibranium */ 68 PCI_PROTOCOL_VERSION_1_4 = PCI_MAKE_VERSION(1, 4), /* WS2022 */ 69 }; 70 71 #define CPU_AFFINITY_ALL -1ULL 72 73 /* 74 * Supported protocol versions in the order of probing - highest go 75 * first. 76 */ 77 static enum pci_protocol_version_t pci_protocol_versions[] = { 78 PCI_PROTOCOL_VERSION_1_4, 79 PCI_PROTOCOL_VERSION_1_3, 80 PCI_PROTOCOL_VERSION_1_2, 81 PCI_PROTOCOL_VERSION_1_1, 82 }; 83 84 #define PCI_CONFIG_MMIO_LENGTH 0x2000 85 #define CFG_PAGE_OFFSET 0x1000 86 #define CFG_PAGE_SIZE (PCI_CONFIG_MMIO_LENGTH - CFG_PAGE_OFFSET) 87 88 #define MAX_SUPPORTED_MSI_MESSAGES 0x400 89 90 #define STATUS_REVISION_MISMATCH 0xC0000059 91 92 /* space for 32bit serial number as string */ 93 #define SLOT_NAME_SIZE 11 94 95 /* 96 * Message Types 97 */ 98 99 enum pci_message_type { 100 /* 101 * Version 1.1 102 */ 103 PCI_MESSAGE_BASE = 0x42490000, 104 PCI_BUS_RELATIONS = PCI_MESSAGE_BASE + 0, 105 PCI_QUERY_BUS_RELATIONS = PCI_MESSAGE_BASE + 1, 106 PCI_POWER_STATE_CHANGE = PCI_MESSAGE_BASE + 4, 107 PCI_QUERY_RESOURCE_REQUIREMENTS = PCI_MESSAGE_BASE + 5, 108 PCI_QUERY_RESOURCE_RESOURCES = PCI_MESSAGE_BASE + 6, 109 PCI_BUS_D0ENTRY = PCI_MESSAGE_BASE + 7, 110 PCI_BUS_D0EXIT = PCI_MESSAGE_BASE + 8, 111 PCI_READ_BLOCK = PCI_MESSAGE_BASE + 9, 112 PCI_WRITE_BLOCK = PCI_MESSAGE_BASE + 0xA, 113 PCI_EJECT = PCI_MESSAGE_BASE + 0xB, 114 PCI_QUERY_STOP = PCI_MESSAGE_BASE + 0xC, 115 PCI_REENABLE = PCI_MESSAGE_BASE + 0xD, 116 PCI_QUERY_STOP_FAILED = PCI_MESSAGE_BASE + 0xE, 117 PCI_EJECTION_COMPLETE = PCI_MESSAGE_BASE + 0xF, 118 PCI_RESOURCES_ASSIGNED = PCI_MESSAGE_BASE + 0x10, 119 PCI_RESOURCES_RELEASED = PCI_MESSAGE_BASE + 0x11, 120 PCI_INVALIDATE_BLOCK = PCI_MESSAGE_BASE + 0x12, 121 PCI_QUERY_PROTOCOL_VERSION = PCI_MESSAGE_BASE + 0x13, 122 PCI_CREATE_INTERRUPT_MESSAGE = PCI_MESSAGE_BASE + 0x14, 123 PCI_DELETE_INTERRUPT_MESSAGE = PCI_MESSAGE_BASE + 0x15, 124 PCI_RESOURCES_ASSIGNED2 = PCI_MESSAGE_BASE + 0x16, 125 PCI_CREATE_INTERRUPT_MESSAGE2 = PCI_MESSAGE_BASE + 0x17, 126 PCI_DELETE_INTERRUPT_MESSAGE2 = PCI_MESSAGE_BASE + 0x18, /* unused */ 127 PCI_BUS_RELATIONS2 = PCI_MESSAGE_BASE + 0x19, 128 PCI_RESOURCES_ASSIGNED3 = PCI_MESSAGE_BASE + 0x1A, 129 PCI_CREATE_INTERRUPT_MESSAGE3 = PCI_MESSAGE_BASE + 0x1B, 130 PCI_MESSAGE_MAXIMUM 131 }; 132 133 /* 134 * Structures defining the virtual PCI Express protocol. 135 */ 136 137 union pci_version { 138 struct { 139 u16 minor_version; 140 u16 major_version; 141 } parts; 142 u32 version; 143 } __packed; 144 145 /* 146 * Function numbers are 8-bits wide on Express, as interpreted through ARI, 147 * which is all this driver does. This representation is the one used in 148 * Windows, which is what is expected when sending this back and forth with 149 * the Hyper-V parent partition. 150 */ 151 union win_slot_encoding { 152 struct { 153 u32 dev:5; 154 u32 func:3; 155 u32 reserved:24; 156 } bits; 157 u32 slot; 158 } __packed; 159 160 /* 161 * Pretty much as defined in the PCI Specifications. 162 */ 163 struct pci_function_description { 164 u16 v_id; /* vendor ID */ 165 u16 d_id; /* device ID */ 166 u8 rev; 167 u8 prog_intf; 168 u8 subclass; 169 u8 base_class; 170 u32 subsystem_id; 171 union win_slot_encoding win_slot; 172 u32 ser; /* serial number */ 173 } __packed; 174 175 enum pci_device_description_flags { 176 HV_PCI_DEVICE_FLAG_NONE = 0x0, 177 HV_PCI_DEVICE_FLAG_NUMA_AFFINITY = 0x1, 178 }; 179 180 struct pci_function_description2 { 181 u16 v_id; /* vendor ID */ 182 u16 d_id; /* device ID */ 183 u8 rev; 184 u8 prog_intf; 185 u8 subclass; 186 u8 base_class; 187 u32 subsystem_id; 188 union win_slot_encoding win_slot; 189 u32 ser; /* serial number */ 190 u32 flags; 191 u16 virtual_numa_node; 192 u16 reserved; 193 } __packed; 194 195 /** 196 * struct hv_msi_desc 197 * @vector: IDT entry 198 * @delivery_mode: As defined in Intel's Programmer's 199 * Reference Manual, Volume 3, Chapter 8. 200 * @vector_count: Number of contiguous entries in the 201 * Interrupt Descriptor Table that are 202 * occupied by this Message-Signaled 203 * Interrupt. For "MSI", as first defined 204 * in PCI 2.2, this can be between 1 and 205 * 32. For "MSI-X," as first defined in PCI 206 * 3.0, this must be 1, as each MSI-X table 207 * entry would have its own descriptor. 208 * @reserved: Empty space 209 * @cpu_mask: All the target virtual processors. 210 */ 211 struct hv_msi_desc { 212 u8 vector; 213 u8 delivery_mode; 214 u16 vector_count; 215 u32 reserved; 216 u64 cpu_mask; 217 } __packed; 218 219 /** 220 * struct hv_msi_desc2 - 1.2 version of hv_msi_desc 221 * @vector: IDT entry 222 * @delivery_mode: As defined in Intel's Programmer's 223 * Reference Manual, Volume 3, Chapter 8. 224 * @vector_count: Number of contiguous entries in the 225 * Interrupt Descriptor Table that are 226 * occupied by this Message-Signaled 227 * Interrupt. For "MSI", as first defined 228 * in PCI 2.2, this can be between 1 and 229 * 32. For "MSI-X," as first defined in PCI 230 * 3.0, this must be 1, as each MSI-X table 231 * entry would have its own descriptor. 232 * @processor_count: number of bits enabled in array. 233 * @processor_array: All the target virtual processors. 234 */ 235 struct hv_msi_desc2 { 236 u8 vector; 237 u8 delivery_mode; 238 u16 vector_count; 239 u16 processor_count; 240 u16 processor_array[32]; 241 } __packed; 242 243 /* 244 * struct hv_msi_desc3 - 1.3 version of hv_msi_desc 245 * Everything is the same as in 'hv_msi_desc2' except that the size of the 246 * 'vector' field is larger to support bigger vector values. For ex: LPI 247 * vectors on ARM. 248 */ 249 struct hv_msi_desc3 { 250 u32 vector; 251 u8 delivery_mode; 252 u8 reserved; 253 u16 vector_count; 254 u16 processor_count; 255 u16 processor_array[32]; 256 } __packed; 257 258 /** 259 * struct tran_int_desc 260 * @reserved: unused, padding 261 * @vector_count: same as in hv_msi_desc 262 * @data: This is the "data payload" value that is 263 * written by the device when it generates 264 * a message-signaled interrupt, either MSI 265 * or MSI-X. 266 * @address: This is the address to which the data 267 * payload is written on interrupt 268 * generation. 269 */ 270 struct tran_int_desc { 271 u16 reserved; 272 u16 vector_count; 273 u32 data; 274 u64 address; 275 } __packed; 276 277 /* 278 * A generic message format for virtual PCI. 279 * Specific message formats are defined later in the file. 280 */ 281 282 struct pci_message { 283 u32 type; 284 } __packed; 285 286 struct pci_child_message { 287 struct pci_message message_type; 288 union win_slot_encoding wslot; 289 } __packed; 290 291 struct pci_incoming_message { 292 struct vmpacket_descriptor hdr; 293 struct pci_message message_type; 294 } __packed; 295 296 struct pci_response { 297 struct vmpacket_descriptor hdr; 298 s32 status; /* negative values are failures */ 299 } __packed; 300 301 struct pci_packet { 302 void (*completion_func)(void *context, struct pci_response *resp, 303 int resp_packet_size); 304 void *compl_ctxt; 305 306 struct pci_message message[]; 307 }; 308 309 /* 310 * Specific message types supporting the PCI protocol. 311 */ 312 313 /* 314 * Version negotiation message. Sent from the guest to the host. 315 * The guest is free to try different versions until the host 316 * accepts the version. 317 * 318 * pci_version: The protocol version requested. 319 * is_last_attempt: If TRUE, this is the last version guest will request. 320 * reservedz: Reserved field, set to zero. 321 */ 322 323 struct pci_version_request { 324 struct pci_message message_type; 325 u32 protocol_version; 326 } __packed; 327 328 /* 329 * Bus D0 Entry. This is sent from the guest to the host when the virtual 330 * bus (PCI Express port) is ready for action. 331 */ 332 333 struct pci_bus_d0_entry { 334 struct pci_message message_type; 335 u32 reserved; 336 u64 mmio_base; 337 } __packed; 338 339 struct pci_bus_relations { 340 struct pci_incoming_message incoming; 341 u32 device_count; 342 struct pci_function_description func[]; 343 } __packed; 344 345 struct pci_bus_relations2 { 346 struct pci_incoming_message incoming; 347 u32 device_count; 348 struct pci_function_description2 func[]; 349 } __packed; 350 351 struct pci_q_res_req_response { 352 struct vmpacket_descriptor hdr; 353 s32 status; /* negative values are failures */ 354 u32 probed_bar[PCI_STD_NUM_BARS]; 355 } __packed; 356 357 struct pci_set_power { 358 struct pci_message message_type; 359 union win_slot_encoding wslot; 360 u32 power_state; /* In Windows terms */ 361 u32 reserved; 362 } __packed; 363 364 struct pci_set_power_response { 365 struct vmpacket_descriptor hdr; 366 s32 status; /* negative values are failures */ 367 union win_slot_encoding wslot; 368 u32 resultant_state; /* In Windows terms */ 369 u32 reserved; 370 } __packed; 371 372 struct pci_resources_assigned { 373 struct pci_message message_type; 374 union win_slot_encoding wslot; 375 u8 memory_range[0x14][6]; /* not used here */ 376 u32 msi_descriptors; 377 u32 reserved[4]; 378 } __packed; 379 380 struct pci_resources_assigned2 { 381 struct pci_message message_type; 382 union win_slot_encoding wslot; 383 u8 memory_range[0x14][6]; /* not used here */ 384 u32 msi_descriptor_count; 385 u8 reserved[70]; 386 } __packed; 387 388 struct pci_create_interrupt { 389 struct pci_message message_type; 390 union win_slot_encoding wslot; 391 struct hv_msi_desc int_desc; 392 } __packed; 393 394 struct pci_create_int_response { 395 struct pci_response response; 396 u32 reserved; 397 struct tran_int_desc int_desc; 398 } __packed; 399 400 struct pci_create_interrupt2 { 401 struct pci_message message_type; 402 union win_slot_encoding wslot; 403 struct hv_msi_desc2 int_desc; 404 } __packed; 405 406 struct pci_create_interrupt3 { 407 struct pci_message message_type; 408 union win_slot_encoding wslot; 409 struct hv_msi_desc3 int_desc; 410 } __packed; 411 412 struct pci_delete_interrupt { 413 struct pci_message message_type; 414 union win_slot_encoding wslot; 415 struct tran_int_desc int_desc; 416 } __packed; 417 418 /* 419 * Note: the VM must pass a valid block id, wslot and bytes_requested. 420 */ 421 struct pci_read_block { 422 struct pci_message message_type; 423 u32 block_id; 424 union win_slot_encoding wslot; 425 u32 bytes_requested; 426 } __packed; 427 428 struct pci_read_block_response { 429 struct vmpacket_descriptor hdr; 430 u32 status; 431 u8 bytes[HV_CONFIG_BLOCK_SIZE_MAX]; 432 } __packed; 433 434 /* 435 * Note: the VM must pass a valid block id, wslot and byte_count. 436 */ 437 struct pci_write_block { 438 struct pci_message message_type; 439 u32 block_id; 440 union win_slot_encoding wslot; 441 u32 byte_count; 442 u8 bytes[HV_CONFIG_BLOCK_SIZE_MAX]; 443 } __packed; 444 445 struct pci_dev_inval_block { 446 struct pci_incoming_message incoming; 447 union win_slot_encoding wslot; 448 u64 block_mask; 449 } __packed; 450 451 struct pci_dev_incoming { 452 struct pci_incoming_message incoming; 453 union win_slot_encoding wslot; 454 } __packed; 455 456 struct pci_eject_response { 457 struct pci_message message_type; 458 union win_slot_encoding wslot; 459 u32 status; 460 } __packed; 461 462 static int pci_ring_size = (4 * PAGE_SIZE); 463 464 /* 465 * Driver specific state. 466 */ 467 468 enum hv_pcibus_state { 469 hv_pcibus_init = 0, 470 hv_pcibus_probed, 471 hv_pcibus_installed, 472 hv_pcibus_removing, 473 hv_pcibus_maximum 474 }; 475 476 struct hv_pcibus_device { 477 #ifdef CONFIG_X86 478 struct pci_sysdata sysdata; 479 #elif defined(CONFIG_ARM64) 480 struct pci_config_window sysdata; 481 #endif 482 struct pci_host_bridge *bridge; 483 struct fwnode_handle *fwnode; 484 /* Protocol version negotiated with the host */ 485 enum pci_protocol_version_t protocol_version; 486 enum hv_pcibus_state state; 487 struct hv_device *hdev; 488 resource_size_t low_mmio_space; 489 resource_size_t high_mmio_space; 490 struct resource *mem_config; 491 struct resource *low_mmio_res; 492 struct resource *high_mmio_res; 493 struct completion *survey_event; 494 struct pci_bus *pci_bus; 495 spinlock_t config_lock; /* Avoid two threads writing index page */ 496 spinlock_t device_list_lock; /* Protect lists below */ 497 void __iomem *cfg_addr; 498 499 struct list_head children; 500 struct list_head dr_list; 501 502 struct msi_domain_info msi_info; 503 struct irq_domain *irq_domain; 504 505 spinlock_t retarget_msi_interrupt_lock; 506 507 struct workqueue_struct *wq; 508 509 /* Highest slot of child device with resources allocated */ 510 int wslot_res_allocated; 511 512 /* hypercall arg, must not cross page boundary */ 513 struct hv_retarget_device_interrupt retarget_msi_interrupt_params; 514 515 /* 516 * Don't put anything here: retarget_msi_interrupt_params must be last 517 */ 518 }; 519 520 /* 521 * Tracks "Device Relations" messages from the host, which must be both 522 * processed in order and deferred so that they don't run in the context 523 * of the incoming packet callback. 524 */ 525 struct hv_dr_work { 526 struct work_struct wrk; 527 struct hv_pcibus_device *bus; 528 }; 529 530 struct hv_pcidev_description { 531 u16 v_id; /* vendor ID */ 532 u16 d_id; /* device ID */ 533 u8 rev; 534 u8 prog_intf; 535 u8 subclass; 536 u8 base_class; 537 u32 subsystem_id; 538 union win_slot_encoding win_slot; 539 u32 ser; /* serial number */ 540 u32 flags; 541 u16 virtual_numa_node; 542 }; 543 544 struct hv_dr_state { 545 struct list_head list_entry; 546 u32 device_count; 547 struct hv_pcidev_description func[]; 548 }; 549 550 enum hv_pcichild_state { 551 hv_pcichild_init = 0, 552 hv_pcichild_requirements, 553 hv_pcichild_resourced, 554 hv_pcichild_ejecting, 555 hv_pcichild_maximum 556 }; 557 558 struct hv_pci_dev { 559 /* List protected by pci_rescan_remove_lock */ 560 struct list_head list_entry; 561 refcount_t refs; 562 enum hv_pcichild_state state; 563 struct pci_slot *pci_slot; 564 struct hv_pcidev_description desc; 565 bool reported_missing; 566 struct hv_pcibus_device *hbus; 567 struct work_struct wrk; 568 569 void (*block_invalidate)(void *context, u64 block_mask); 570 void *invalidate_context; 571 572 /* 573 * What would be observed if one wrote 0xFFFFFFFF to a BAR and then 574 * read it back, for each of the BAR offsets within config space. 575 */ 576 u32 probed_bar[PCI_STD_NUM_BARS]; 577 }; 578 579 struct hv_pci_compl { 580 struct completion host_event; 581 s32 completion_status; 582 }; 583 584 static void hv_pci_onchannelcallback(void *context); 585 586 /** 587 * hv_pci_generic_compl() - Invoked for a completion packet 588 * @context: Set up by the sender of the packet. 589 * @resp: The response packet 590 * @resp_packet_size: Size in bytes of the packet 591 * 592 * This function is used to trigger an event and report status 593 * for any message for which the completion packet contains a 594 * status and nothing else. 595 */ 596 static void hv_pci_generic_compl(void *context, struct pci_response *resp, 597 int resp_packet_size) 598 { 599 struct hv_pci_compl *comp_pkt = context; 600 601 if (resp_packet_size >= offsetofend(struct pci_response, status)) 602 comp_pkt->completion_status = resp->status; 603 else 604 comp_pkt->completion_status = -1; 605 606 complete(&comp_pkt->host_event); 607 } 608 609 static struct hv_pci_dev *get_pcichild_wslot(struct hv_pcibus_device *hbus, 610 u32 wslot); 611 612 static void get_pcichild(struct hv_pci_dev *hpdev) 613 { 614 refcount_inc(&hpdev->refs); 615 } 616 617 static void put_pcichild(struct hv_pci_dev *hpdev) 618 { 619 if (refcount_dec_and_test(&hpdev->refs)) 620 kfree(hpdev); 621 } 622 623 /* 624 * There is no good way to get notified from vmbus_onoffer_rescind(), 625 * so let's use polling here, since this is not a hot path. 626 */ 627 static int wait_for_response(struct hv_device *hdev, 628 struct completion *comp) 629 { 630 while (true) { 631 if (hdev->channel->rescind) { 632 dev_warn_once(&hdev->device, "The device is gone.\n"); 633 return -ENODEV; 634 } 635 636 if (wait_for_completion_timeout(comp, HZ / 10)) 637 break; 638 } 639 640 return 0; 641 } 642 643 /** 644 * devfn_to_wslot() - Convert from Linux PCI slot to Windows 645 * @devfn: The Linux representation of PCI slot 646 * 647 * Windows uses a slightly different representation of PCI slot. 648 * 649 * Return: The Windows representation 650 */ 651 static u32 devfn_to_wslot(int devfn) 652 { 653 union win_slot_encoding wslot; 654 655 wslot.slot = 0; 656 wslot.bits.dev = PCI_SLOT(devfn); 657 wslot.bits.func = PCI_FUNC(devfn); 658 659 return wslot.slot; 660 } 661 662 /** 663 * wslot_to_devfn() - Convert from Windows PCI slot to Linux 664 * @wslot: The Windows representation of PCI slot 665 * 666 * Windows uses a slightly different representation of PCI slot. 667 * 668 * Return: The Linux representation 669 */ 670 static int wslot_to_devfn(u32 wslot) 671 { 672 union win_slot_encoding slot_no; 673 674 slot_no.slot = wslot; 675 return PCI_DEVFN(slot_no.bits.dev, slot_no.bits.func); 676 } 677 678 /* 679 * PCI Configuration Space for these root PCI buses is implemented as a pair 680 * of pages in memory-mapped I/O space. Writing to the first page chooses 681 * the PCI function being written or read. Once the first page has been 682 * written to, the following page maps in the entire configuration space of 683 * the function. 684 */ 685 686 /** 687 * _hv_pcifront_read_config() - Internal PCI config read 688 * @hpdev: The PCI driver's representation of the device 689 * @where: Offset within config space 690 * @size: Size of the transfer 691 * @val: Pointer to the buffer receiving the data 692 */ 693 static void _hv_pcifront_read_config(struct hv_pci_dev *hpdev, int where, 694 int size, u32 *val) 695 { 696 unsigned long flags; 697 void __iomem *addr = hpdev->hbus->cfg_addr + CFG_PAGE_OFFSET + where; 698 699 /* 700 * If the attempt is to read the IDs or the ROM BAR, simulate that. 701 */ 702 if (where + size <= PCI_COMMAND) { 703 memcpy(val, ((u8 *)&hpdev->desc.v_id) + where, size); 704 } else if (where >= PCI_CLASS_REVISION && where + size <= 705 PCI_CACHE_LINE_SIZE) { 706 memcpy(val, ((u8 *)&hpdev->desc.rev) + where - 707 PCI_CLASS_REVISION, size); 708 } else if (where >= PCI_SUBSYSTEM_VENDOR_ID && where + size <= 709 PCI_ROM_ADDRESS) { 710 memcpy(val, (u8 *)&hpdev->desc.subsystem_id + where - 711 PCI_SUBSYSTEM_VENDOR_ID, size); 712 } else if (where >= PCI_ROM_ADDRESS && where + size <= 713 PCI_CAPABILITY_LIST) { 714 /* ROM BARs are unimplemented */ 715 *val = 0; 716 } else if (where >= PCI_INTERRUPT_LINE && where + size <= 717 PCI_INTERRUPT_PIN) { 718 /* 719 * Interrupt Line and Interrupt PIN are hard-wired to zero 720 * because this front-end only supports message-signaled 721 * interrupts. 722 */ 723 *val = 0; 724 } else if (where + size <= CFG_PAGE_SIZE) { 725 spin_lock_irqsave(&hpdev->hbus->config_lock, flags); 726 /* Choose the function to be read. (See comment above) */ 727 writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr); 728 /* Make sure the function was chosen before we start reading. */ 729 mb(); 730 /* Read from that function's config space. */ 731 switch (size) { 732 case 1: 733 *val = readb(addr); 734 break; 735 case 2: 736 *val = readw(addr); 737 break; 738 default: 739 *val = readl(addr); 740 break; 741 } 742 /* 743 * Make sure the read was done before we release the spinlock 744 * allowing consecutive reads/writes. 745 */ 746 mb(); 747 spin_unlock_irqrestore(&hpdev->hbus->config_lock, flags); 748 } else { 749 dev_err(&hpdev->hbus->hdev->device, 750 "Attempt to read beyond a function's config space.\n"); 751 } 752 } 753 754 static u16 hv_pcifront_get_vendor_id(struct hv_pci_dev *hpdev) 755 { 756 u16 ret; 757 unsigned long flags; 758 void __iomem *addr = hpdev->hbus->cfg_addr + CFG_PAGE_OFFSET + 759 PCI_VENDOR_ID; 760 761 spin_lock_irqsave(&hpdev->hbus->config_lock, flags); 762 763 /* Choose the function to be read. (See comment above) */ 764 writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr); 765 /* Make sure the function was chosen before we start reading. */ 766 mb(); 767 /* Read from that function's config space. */ 768 ret = readw(addr); 769 /* 770 * mb() is not required here, because the spin_unlock_irqrestore() 771 * is a barrier. 772 */ 773 774 spin_unlock_irqrestore(&hpdev->hbus->config_lock, flags); 775 776 return ret; 777 } 778 779 /** 780 * _hv_pcifront_write_config() - Internal PCI config write 781 * @hpdev: The PCI driver's representation of the device 782 * @where: Offset within config space 783 * @size: Size of the transfer 784 * @val: The data being transferred 785 */ 786 static void _hv_pcifront_write_config(struct hv_pci_dev *hpdev, int where, 787 int size, u32 val) 788 { 789 unsigned long flags; 790 void __iomem *addr = hpdev->hbus->cfg_addr + CFG_PAGE_OFFSET + where; 791 792 if (where >= PCI_SUBSYSTEM_VENDOR_ID && 793 where + size <= PCI_CAPABILITY_LIST) { 794 /* SSIDs and ROM BARs are read-only */ 795 } else if (where >= PCI_COMMAND && where + size <= CFG_PAGE_SIZE) { 796 spin_lock_irqsave(&hpdev->hbus->config_lock, flags); 797 /* Choose the function to be written. (See comment above) */ 798 writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr); 799 /* Make sure the function was chosen before we start writing. */ 800 wmb(); 801 /* Write to that function's config space. */ 802 switch (size) { 803 case 1: 804 writeb(val, addr); 805 break; 806 case 2: 807 writew(val, addr); 808 break; 809 default: 810 writel(val, addr); 811 break; 812 } 813 /* 814 * Make sure the write was done before we release the spinlock 815 * allowing consecutive reads/writes. 816 */ 817 mb(); 818 spin_unlock_irqrestore(&hpdev->hbus->config_lock, flags); 819 } else { 820 dev_err(&hpdev->hbus->hdev->device, 821 "Attempt to write beyond a function's config space.\n"); 822 } 823 } 824 825 /** 826 * hv_pcifront_read_config() - Read configuration space 827 * @bus: PCI Bus structure 828 * @devfn: Device/function 829 * @where: Offset from base 830 * @size: Byte/word/dword 831 * @val: Value to be read 832 * 833 * Return: PCIBIOS_SUCCESSFUL on success 834 * PCIBIOS_DEVICE_NOT_FOUND on failure 835 */ 836 static int hv_pcifront_read_config(struct pci_bus *bus, unsigned int devfn, 837 int where, int size, u32 *val) 838 { 839 struct hv_pcibus_device *hbus = 840 container_of(bus->sysdata, struct hv_pcibus_device, sysdata); 841 struct hv_pci_dev *hpdev; 842 843 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(devfn)); 844 if (!hpdev) 845 return PCIBIOS_DEVICE_NOT_FOUND; 846 847 _hv_pcifront_read_config(hpdev, where, size, val); 848 849 put_pcichild(hpdev); 850 return PCIBIOS_SUCCESSFUL; 851 } 852 853 /** 854 * hv_pcifront_write_config() - Write configuration space 855 * @bus: PCI Bus structure 856 * @devfn: Device/function 857 * @where: Offset from base 858 * @size: Byte/word/dword 859 * @val: Value to be written to device 860 * 861 * Return: PCIBIOS_SUCCESSFUL on success 862 * PCIBIOS_DEVICE_NOT_FOUND on failure 863 */ 864 static int hv_pcifront_write_config(struct pci_bus *bus, unsigned int devfn, 865 int where, int size, u32 val) 866 { 867 struct hv_pcibus_device *hbus = 868 container_of(bus->sysdata, struct hv_pcibus_device, sysdata); 869 struct hv_pci_dev *hpdev; 870 871 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(devfn)); 872 if (!hpdev) 873 return PCIBIOS_DEVICE_NOT_FOUND; 874 875 _hv_pcifront_write_config(hpdev, where, size, val); 876 877 put_pcichild(hpdev); 878 return PCIBIOS_SUCCESSFUL; 879 } 880 881 /* PCIe operations */ 882 static struct pci_ops hv_pcifront_ops = { 883 .read = hv_pcifront_read_config, 884 .write = hv_pcifront_write_config, 885 }; 886 887 /* 888 * Paravirtual backchannel 889 * 890 * Hyper-V SR-IOV provides a backchannel mechanism in software for 891 * communication between a VF driver and a PF driver. These 892 * "configuration blocks" are similar in concept to PCI configuration space, 893 * but instead of doing reads and writes in 32-bit chunks through a very slow 894 * path, packets of up to 128 bytes can be sent or received asynchronously. 895 * 896 * Nearly every SR-IOV device contains just such a communications channel in 897 * hardware, so using this one in software is usually optional. Using the 898 * software channel, however, allows driver implementers to leverage software 899 * tools that fuzz the communications channel looking for vulnerabilities. 900 * 901 * The usage model for these packets puts the responsibility for reading or 902 * writing on the VF driver. The VF driver sends a read or a write packet, 903 * indicating which "block" is being referred to by number. 904 * 905 * If the PF driver wishes to initiate communication, it can "invalidate" one or 906 * more of the first 64 blocks. This invalidation is delivered via a callback 907 * supplied by the VF driver by this driver. 908 * 909 * No protocol is implied, except that supplied by the PF and VF drivers. 910 */ 911 912 struct hv_read_config_compl { 913 struct hv_pci_compl comp_pkt; 914 void *buf; 915 unsigned int len; 916 unsigned int bytes_returned; 917 }; 918 919 /** 920 * hv_pci_read_config_compl() - Invoked when a response packet 921 * for a read config block operation arrives. 922 * @context: Identifies the read config operation 923 * @resp: The response packet itself 924 * @resp_packet_size: Size in bytes of the response packet 925 */ 926 static void hv_pci_read_config_compl(void *context, struct pci_response *resp, 927 int resp_packet_size) 928 { 929 struct hv_read_config_compl *comp = context; 930 struct pci_read_block_response *read_resp = 931 (struct pci_read_block_response *)resp; 932 unsigned int data_len, hdr_len; 933 934 hdr_len = offsetof(struct pci_read_block_response, bytes); 935 if (resp_packet_size < hdr_len) { 936 comp->comp_pkt.completion_status = -1; 937 goto out; 938 } 939 940 data_len = resp_packet_size - hdr_len; 941 if (data_len > 0 && read_resp->status == 0) { 942 comp->bytes_returned = min(comp->len, data_len); 943 memcpy(comp->buf, read_resp->bytes, comp->bytes_returned); 944 } else { 945 comp->bytes_returned = 0; 946 } 947 948 comp->comp_pkt.completion_status = read_resp->status; 949 out: 950 complete(&comp->comp_pkt.host_event); 951 } 952 953 /** 954 * hv_read_config_block() - Sends a read config block request to 955 * the back-end driver running in the Hyper-V parent partition. 956 * @pdev: The PCI driver's representation for this device. 957 * @buf: Buffer into which the config block will be copied. 958 * @len: Size in bytes of buf. 959 * @block_id: Identifies the config block which has been requested. 960 * @bytes_returned: Size which came back from the back-end driver. 961 * 962 * Return: 0 on success, -errno on failure 963 */ 964 static int hv_read_config_block(struct pci_dev *pdev, void *buf, 965 unsigned int len, unsigned int block_id, 966 unsigned int *bytes_returned) 967 { 968 struct hv_pcibus_device *hbus = 969 container_of(pdev->bus->sysdata, struct hv_pcibus_device, 970 sysdata); 971 struct { 972 struct pci_packet pkt; 973 char buf[sizeof(struct pci_read_block)]; 974 } pkt; 975 struct hv_read_config_compl comp_pkt; 976 struct pci_read_block *read_blk; 977 int ret; 978 979 if (len == 0 || len > HV_CONFIG_BLOCK_SIZE_MAX) 980 return -EINVAL; 981 982 init_completion(&comp_pkt.comp_pkt.host_event); 983 comp_pkt.buf = buf; 984 comp_pkt.len = len; 985 986 memset(&pkt, 0, sizeof(pkt)); 987 pkt.pkt.completion_func = hv_pci_read_config_compl; 988 pkt.pkt.compl_ctxt = &comp_pkt; 989 read_blk = (struct pci_read_block *)&pkt.pkt.message; 990 read_blk->message_type.type = PCI_READ_BLOCK; 991 read_blk->wslot.slot = devfn_to_wslot(pdev->devfn); 992 read_blk->block_id = block_id; 993 read_blk->bytes_requested = len; 994 995 ret = vmbus_sendpacket(hbus->hdev->channel, read_blk, 996 sizeof(*read_blk), (unsigned long)&pkt.pkt, 997 VM_PKT_DATA_INBAND, 998 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); 999 if (ret) 1000 return ret; 1001 1002 ret = wait_for_response(hbus->hdev, &comp_pkt.comp_pkt.host_event); 1003 if (ret) 1004 return ret; 1005 1006 if (comp_pkt.comp_pkt.completion_status != 0 || 1007 comp_pkt.bytes_returned == 0) { 1008 dev_err(&hbus->hdev->device, 1009 "Read Config Block failed: 0x%x, bytes_returned=%d\n", 1010 comp_pkt.comp_pkt.completion_status, 1011 comp_pkt.bytes_returned); 1012 return -EIO; 1013 } 1014 1015 *bytes_returned = comp_pkt.bytes_returned; 1016 return 0; 1017 } 1018 1019 /** 1020 * hv_pci_write_config_compl() - Invoked when a response packet for a write 1021 * config block operation arrives. 1022 * @context: Identifies the write config operation 1023 * @resp: The response packet itself 1024 * @resp_packet_size: Size in bytes of the response packet 1025 */ 1026 static void hv_pci_write_config_compl(void *context, struct pci_response *resp, 1027 int resp_packet_size) 1028 { 1029 struct hv_pci_compl *comp_pkt = context; 1030 1031 comp_pkt->completion_status = resp->status; 1032 complete(&comp_pkt->host_event); 1033 } 1034 1035 /** 1036 * hv_write_config_block() - Sends a write config block request to the 1037 * back-end driver running in the Hyper-V parent partition. 1038 * @pdev: The PCI driver's representation for this device. 1039 * @buf: Buffer from which the config block will be copied. 1040 * @len: Size in bytes of buf. 1041 * @block_id: Identifies the config block which is being written. 1042 * 1043 * Return: 0 on success, -errno on failure 1044 */ 1045 static int hv_write_config_block(struct pci_dev *pdev, void *buf, 1046 unsigned int len, unsigned int block_id) 1047 { 1048 struct hv_pcibus_device *hbus = 1049 container_of(pdev->bus->sysdata, struct hv_pcibus_device, 1050 sysdata); 1051 struct { 1052 struct pci_packet pkt; 1053 char buf[sizeof(struct pci_write_block)]; 1054 u32 reserved; 1055 } pkt; 1056 struct hv_pci_compl comp_pkt; 1057 struct pci_write_block *write_blk; 1058 u32 pkt_size; 1059 int ret; 1060 1061 if (len == 0 || len > HV_CONFIG_BLOCK_SIZE_MAX) 1062 return -EINVAL; 1063 1064 init_completion(&comp_pkt.host_event); 1065 1066 memset(&pkt, 0, sizeof(pkt)); 1067 pkt.pkt.completion_func = hv_pci_write_config_compl; 1068 pkt.pkt.compl_ctxt = &comp_pkt; 1069 write_blk = (struct pci_write_block *)&pkt.pkt.message; 1070 write_blk->message_type.type = PCI_WRITE_BLOCK; 1071 write_blk->wslot.slot = devfn_to_wslot(pdev->devfn); 1072 write_blk->block_id = block_id; 1073 write_blk->byte_count = len; 1074 memcpy(write_blk->bytes, buf, len); 1075 pkt_size = offsetof(struct pci_write_block, bytes) + len; 1076 /* 1077 * This quirk is required on some hosts shipped around 2018, because 1078 * these hosts don't check the pkt_size correctly (new hosts have been 1079 * fixed since early 2019). The quirk is also safe on very old hosts 1080 * and new hosts, because, on them, what really matters is the length 1081 * specified in write_blk->byte_count. 1082 */ 1083 pkt_size += sizeof(pkt.reserved); 1084 1085 ret = vmbus_sendpacket(hbus->hdev->channel, write_blk, pkt_size, 1086 (unsigned long)&pkt.pkt, VM_PKT_DATA_INBAND, 1087 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); 1088 if (ret) 1089 return ret; 1090 1091 ret = wait_for_response(hbus->hdev, &comp_pkt.host_event); 1092 if (ret) 1093 return ret; 1094 1095 if (comp_pkt.completion_status != 0) { 1096 dev_err(&hbus->hdev->device, 1097 "Write Config Block failed: 0x%x\n", 1098 comp_pkt.completion_status); 1099 return -EIO; 1100 } 1101 1102 return 0; 1103 } 1104 1105 /** 1106 * hv_register_block_invalidate() - Invoked when a config block invalidation 1107 * arrives from the back-end driver. 1108 * @pdev: The PCI driver's representation for this device. 1109 * @context: Identifies the device. 1110 * @block_invalidate: Identifies all of the blocks being invalidated. 1111 * 1112 * Return: 0 on success, -errno on failure 1113 */ 1114 static int hv_register_block_invalidate(struct pci_dev *pdev, void *context, 1115 void (*block_invalidate)(void *context, 1116 u64 block_mask)) 1117 { 1118 struct hv_pcibus_device *hbus = 1119 container_of(pdev->bus->sysdata, struct hv_pcibus_device, 1120 sysdata); 1121 struct hv_pci_dev *hpdev; 1122 1123 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn)); 1124 if (!hpdev) 1125 return -ENODEV; 1126 1127 hpdev->block_invalidate = block_invalidate; 1128 hpdev->invalidate_context = context; 1129 1130 put_pcichild(hpdev); 1131 return 0; 1132 1133 } 1134 1135 /* Interrupt management hooks */ 1136 static void hv_int_desc_free(struct hv_pci_dev *hpdev, 1137 struct tran_int_desc *int_desc) 1138 { 1139 struct pci_delete_interrupt *int_pkt; 1140 struct { 1141 struct pci_packet pkt; 1142 u8 buffer[sizeof(struct pci_delete_interrupt)]; 1143 } ctxt; 1144 1145 memset(&ctxt, 0, sizeof(ctxt)); 1146 int_pkt = (struct pci_delete_interrupt *)&ctxt.pkt.message; 1147 int_pkt->message_type.type = 1148 PCI_DELETE_INTERRUPT_MESSAGE; 1149 int_pkt->wslot.slot = hpdev->desc.win_slot.slot; 1150 int_pkt->int_desc = *int_desc; 1151 vmbus_sendpacket(hpdev->hbus->hdev->channel, int_pkt, sizeof(*int_pkt), 1152 (unsigned long)&ctxt.pkt, VM_PKT_DATA_INBAND, 0); 1153 kfree(int_desc); 1154 } 1155 1156 /** 1157 * hv_msi_free() - Free the MSI. 1158 * @domain: The interrupt domain pointer 1159 * @info: Extra MSI-related context 1160 * @irq: Identifies the IRQ. 1161 * 1162 * The Hyper-V parent partition and hypervisor are tracking the 1163 * messages that are in use, keeping the interrupt redirection 1164 * table up to date. This callback sends a message that frees 1165 * the IRT entry and related tracking nonsense. 1166 */ 1167 static void hv_msi_free(struct irq_domain *domain, struct msi_domain_info *info, 1168 unsigned int irq) 1169 { 1170 struct hv_pcibus_device *hbus; 1171 struct hv_pci_dev *hpdev; 1172 struct pci_dev *pdev; 1173 struct tran_int_desc *int_desc; 1174 struct irq_data *irq_data = irq_domain_get_irq_data(domain, irq); 1175 struct msi_desc *msi = irq_data_get_msi_desc(irq_data); 1176 1177 pdev = msi_desc_to_pci_dev(msi); 1178 hbus = info->data; 1179 int_desc = irq_data_get_irq_chip_data(irq_data); 1180 if (!int_desc) 1181 return; 1182 1183 irq_data->chip_data = NULL; 1184 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn)); 1185 if (!hpdev) { 1186 kfree(int_desc); 1187 return; 1188 } 1189 1190 hv_int_desc_free(hpdev, int_desc); 1191 put_pcichild(hpdev); 1192 } 1193 1194 static int hv_set_affinity(struct irq_data *data, const struct cpumask *dest, 1195 bool force) 1196 { 1197 struct irq_data *parent = data->parent_data; 1198 1199 return parent->chip->irq_set_affinity(parent, dest, force); 1200 } 1201 1202 static void hv_irq_mask(struct irq_data *data) 1203 { 1204 pci_msi_mask_irq(data); 1205 } 1206 1207 /** 1208 * hv_irq_unmask() - "Unmask" the IRQ by setting its current 1209 * affinity. 1210 * @data: Describes the IRQ 1211 * 1212 * Build new a destination for the MSI and make a hypercall to 1213 * update the Interrupt Redirection Table. "Device Logical ID" 1214 * is built out of this PCI bus's instance GUID and the function 1215 * number of the device. 1216 */ 1217 static void hv_irq_unmask(struct irq_data *data) 1218 { 1219 struct msi_desc *msi_desc = irq_data_get_msi_desc(data); 1220 struct irq_cfg *cfg = irqd_cfg(data); 1221 struct hv_retarget_device_interrupt *params; 1222 struct hv_pcibus_device *hbus; 1223 struct cpumask *dest; 1224 cpumask_var_t tmp; 1225 struct pci_bus *pbus; 1226 struct pci_dev *pdev; 1227 unsigned long flags; 1228 u32 var_size = 0; 1229 int cpu, nr_bank; 1230 u64 res; 1231 1232 dest = irq_data_get_effective_affinity_mask(data); 1233 pdev = msi_desc_to_pci_dev(msi_desc); 1234 pbus = pdev->bus; 1235 hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata); 1236 1237 spin_lock_irqsave(&hbus->retarget_msi_interrupt_lock, flags); 1238 1239 params = &hbus->retarget_msi_interrupt_params; 1240 memset(params, 0, sizeof(*params)); 1241 params->partition_id = HV_PARTITION_ID_SELF; 1242 params->int_entry.source = HV_INTERRUPT_SOURCE_MSI; 1243 hv_set_msi_entry_from_desc(¶ms->int_entry.msi_entry, msi_desc); 1244 params->device_id = (hbus->hdev->dev_instance.b[5] << 24) | 1245 (hbus->hdev->dev_instance.b[4] << 16) | 1246 (hbus->hdev->dev_instance.b[7] << 8) | 1247 (hbus->hdev->dev_instance.b[6] & 0xf8) | 1248 PCI_FUNC(pdev->devfn); 1249 params->int_target.vector = cfg->vector; 1250 1251 /* 1252 * Honoring apic->delivery_mode set to APIC_DELIVERY_MODE_FIXED by 1253 * setting the HV_DEVICE_INTERRUPT_TARGET_MULTICAST flag results in a 1254 * spurious interrupt storm. Not doing so does not seem to have a 1255 * negative effect (yet?). 1256 */ 1257 1258 if (hbus->protocol_version >= PCI_PROTOCOL_VERSION_1_2) { 1259 /* 1260 * PCI_PROTOCOL_VERSION_1_2 supports the VP_SET version of the 1261 * HVCALL_RETARGET_INTERRUPT hypercall, which also coincides 1262 * with >64 VP support. 1263 * ms_hyperv.hints & HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED 1264 * is not sufficient for this hypercall. 1265 */ 1266 params->int_target.flags |= 1267 HV_DEVICE_INTERRUPT_TARGET_PROCESSOR_SET; 1268 1269 if (!alloc_cpumask_var(&tmp, GFP_ATOMIC)) { 1270 res = 1; 1271 goto exit_unlock; 1272 } 1273 1274 cpumask_and(tmp, dest, cpu_online_mask); 1275 nr_bank = cpumask_to_vpset(¶ms->int_target.vp_set, tmp); 1276 free_cpumask_var(tmp); 1277 1278 if (nr_bank <= 0) { 1279 res = 1; 1280 goto exit_unlock; 1281 } 1282 1283 /* 1284 * var-sized hypercall, var-size starts after vp_mask (thus 1285 * vp_set.format does not count, but vp_set.valid_bank_mask 1286 * does). 1287 */ 1288 var_size = 1 + nr_bank; 1289 } else { 1290 for_each_cpu_and(cpu, dest, cpu_online_mask) { 1291 params->int_target.vp_mask |= 1292 (1ULL << hv_cpu_number_to_vp_number(cpu)); 1293 } 1294 } 1295 1296 res = hv_do_hypercall(HVCALL_RETARGET_INTERRUPT | (var_size << 17), 1297 params, NULL); 1298 1299 exit_unlock: 1300 spin_unlock_irqrestore(&hbus->retarget_msi_interrupt_lock, flags); 1301 1302 /* 1303 * During hibernation, when a CPU is offlined, the kernel tries 1304 * to move the interrupt to the remaining CPUs that haven't 1305 * been offlined yet. In this case, the below hv_do_hypercall() 1306 * always fails since the vmbus channel has been closed: 1307 * refer to cpu_disable_common() -> fixup_irqs() -> 1308 * irq_migrate_all_off_this_cpu() -> migrate_one_irq(). 1309 * 1310 * Suppress the error message for hibernation because the failure 1311 * during hibernation does not matter (at this time all the devices 1312 * have been frozen). Note: the correct affinity info is still updated 1313 * into the irqdata data structure in migrate_one_irq() -> 1314 * irq_do_set_affinity() -> hv_set_affinity(), so later when the VM 1315 * resumes, hv_pci_restore_msi_state() is able to correctly restore 1316 * the interrupt with the correct affinity. 1317 */ 1318 if (!hv_result_success(res) && hbus->state != hv_pcibus_removing) 1319 dev_err(&hbus->hdev->device, 1320 "%s() failed: %#llx", __func__, res); 1321 1322 pci_msi_unmask_irq(data); 1323 } 1324 1325 struct compose_comp_ctxt { 1326 struct hv_pci_compl comp_pkt; 1327 struct tran_int_desc int_desc; 1328 }; 1329 1330 static void hv_pci_compose_compl(void *context, struct pci_response *resp, 1331 int resp_packet_size) 1332 { 1333 struct compose_comp_ctxt *comp_pkt = context; 1334 struct pci_create_int_response *int_resp = 1335 (struct pci_create_int_response *)resp; 1336 1337 comp_pkt->comp_pkt.completion_status = resp->status; 1338 comp_pkt->int_desc = int_resp->int_desc; 1339 complete(&comp_pkt->comp_pkt.host_event); 1340 } 1341 1342 static u32 hv_compose_msi_req_v1( 1343 struct pci_create_interrupt *int_pkt, struct cpumask *affinity, 1344 u32 slot, u8 vector) 1345 { 1346 int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE; 1347 int_pkt->wslot.slot = slot; 1348 int_pkt->int_desc.vector = vector; 1349 int_pkt->int_desc.vector_count = 1; 1350 int_pkt->int_desc.delivery_mode = APIC_DELIVERY_MODE_FIXED; 1351 1352 /* 1353 * Create MSI w/ dummy vCPU set, overwritten by subsequent retarget in 1354 * hv_irq_unmask(). 1355 */ 1356 int_pkt->int_desc.cpu_mask = CPU_AFFINITY_ALL; 1357 1358 return sizeof(*int_pkt); 1359 } 1360 1361 /* 1362 * Create MSI w/ dummy vCPU set targeting just one vCPU, overwritten 1363 * by subsequent retarget in hv_irq_unmask(). 1364 */ 1365 static int hv_compose_msi_req_get_cpu(struct cpumask *affinity) 1366 { 1367 return cpumask_first_and(affinity, cpu_online_mask); 1368 } 1369 1370 static u32 hv_compose_msi_req_v2( 1371 struct pci_create_interrupt2 *int_pkt, struct cpumask *affinity, 1372 u32 slot, u8 vector) 1373 { 1374 int cpu; 1375 1376 int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE2; 1377 int_pkt->wslot.slot = slot; 1378 int_pkt->int_desc.vector = vector; 1379 int_pkt->int_desc.vector_count = 1; 1380 int_pkt->int_desc.delivery_mode = APIC_DELIVERY_MODE_FIXED; 1381 cpu = hv_compose_msi_req_get_cpu(affinity); 1382 int_pkt->int_desc.processor_array[0] = 1383 hv_cpu_number_to_vp_number(cpu); 1384 int_pkt->int_desc.processor_count = 1; 1385 1386 return sizeof(*int_pkt); 1387 } 1388 1389 static u32 hv_compose_msi_req_v3( 1390 struct pci_create_interrupt3 *int_pkt, struct cpumask *affinity, 1391 u32 slot, u32 vector) 1392 { 1393 int cpu; 1394 1395 int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE3; 1396 int_pkt->wslot.slot = slot; 1397 int_pkt->int_desc.vector = vector; 1398 int_pkt->int_desc.reserved = 0; 1399 int_pkt->int_desc.vector_count = 1; 1400 int_pkt->int_desc.delivery_mode = APIC_DELIVERY_MODE_FIXED; 1401 cpu = hv_compose_msi_req_get_cpu(affinity); 1402 int_pkt->int_desc.processor_array[0] = 1403 hv_cpu_number_to_vp_number(cpu); 1404 int_pkt->int_desc.processor_count = 1; 1405 1406 return sizeof(*int_pkt); 1407 } 1408 1409 /** 1410 * hv_compose_msi_msg() - Supplies a valid MSI address/data 1411 * @data: Everything about this MSI 1412 * @msg: Buffer that is filled in by this function 1413 * 1414 * This function unpacks the IRQ looking for target CPU set, IDT 1415 * vector and mode and sends a message to the parent partition 1416 * asking for a mapping for that tuple in this partition. The 1417 * response supplies a data value and address to which that data 1418 * should be written to trigger that interrupt. 1419 */ 1420 static void hv_compose_msi_msg(struct irq_data *data, struct msi_msg *msg) 1421 { 1422 struct irq_cfg *cfg = irqd_cfg(data); 1423 struct hv_pcibus_device *hbus; 1424 struct vmbus_channel *channel; 1425 struct hv_pci_dev *hpdev; 1426 struct pci_bus *pbus; 1427 struct pci_dev *pdev; 1428 struct cpumask *dest; 1429 struct compose_comp_ctxt comp; 1430 struct tran_int_desc *int_desc; 1431 struct { 1432 struct pci_packet pci_pkt; 1433 union { 1434 struct pci_create_interrupt v1; 1435 struct pci_create_interrupt2 v2; 1436 struct pci_create_interrupt3 v3; 1437 } int_pkts; 1438 } __packed ctxt; 1439 1440 u32 size; 1441 int ret; 1442 1443 pdev = msi_desc_to_pci_dev(irq_data_get_msi_desc(data)); 1444 dest = irq_data_get_effective_affinity_mask(data); 1445 pbus = pdev->bus; 1446 hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata); 1447 channel = hbus->hdev->channel; 1448 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn)); 1449 if (!hpdev) 1450 goto return_null_message; 1451 1452 /* Free any previous message that might have already been composed. */ 1453 if (data->chip_data) { 1454 int_desc = data->chip_data; 1455 data->chip_data = NULL; 1456 hv_int_desc_free(hpdev, int_desc); 1457 } 1458 1459 int_desc = kzalloc(sizeof(*int_desc), GFP_ATOMIC); 1460 if (!int_desc) 1461 goto drop_reference; 1462 1463 memset(&ctxt, 0, sizeof(ctxt)); 1464 init_completion(&comp.comp_pkt.host_event); 1465 ctxt.pci_pkt.completion_func = hv_pci_compose_compl; 1466 ctxt.pci_pkt.compl_ctxt = ∁ 1467 1468 switch (hbus->protocol_version) { 1469 case PCI_PROTOCOL_VERSION_1_1: 1470 size = hv_compose_msi_req_v1(&ctxt.int_pkts.v1, 1471 dest, 1472 hpdev->desc.win_slot.slot, 1473 cfg->vector); 1474 break; 1475 1476 case PCI_PROTOCOL_VERSION_1_2: 1477 case PCI_PROTOCOL_VERSION_1_3: 1478 size = hv_compose_msi_req_v2(&ctxt.int_pkts.v2, 1479 dest, 1480 hpdev->desc.win_slot.slot, 1481 cfg->vector); 1482 break; 1483 1484 case PCI_PROTOCOL_VERSION_1_4: 1485 size = hv_compose_msi_req_v3(&ctxt.int_pkts.v3, 1486 dest, 1487 hpdev->desc.win_slot.slot, 1488 cfg->vector); 1489 break; 1490 1491 default: 1492 /* As we only negotiate protocol versions known to this driver, 1493 * this path should never hit. However, this is it not a hot 1494 * path so we print a message to aid future updates. 1495 */ 1496 dev_err(&hbus->hdev->device, 1497 "Unexpected vPCI protocol, update driver."); 1498 goto free_int_desc; 1499 } 1500 1501 ret = vmbus_sendpacket(hpdev->hbus->hdev->channel, &ctxt.int_pkts, 1502 size, (unsigned long)&ctxt.pci_pkt, 1503 VM_PKT_DATA_INBAND, 1504 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); 1505 if (ret) { 1506 dev_err(&hbus->hdev->device, 1507 "Sending request for interrupt failed: 0x%x", 1508 comp.comp_pkt.completion_status); 1509 goto free_int_desc; 1510 } 1511 1512 /* 1513 * Prevents hv_pci_onchannelcallback() from running concurrently 1514 * in the tasklet. 1515 */ 1516 tasklet_disable_in_atomic(&channel->callback_event); 1517 1518 /* 1519 * Since this function is called with IRQ locks held, can't 1520 * do normal wait for completion; instead poll. 1521 */ 1522 while (!try_wait_for_completion(&comp.comp_pkt.host_event)) { 1523 unsigned long flags; 1524 1525 /* 0xFFFF means an invalid PCI VENDOR ID. */ 1526 if (hv_pcifront_get_vendor_id(hpdev) == 0xFFFF) { 1527 dev_err_once(&hbus->hdev->device, 1528 "the device has gone\n"); 1529 goto enable_tasklet; 1530 } 1531 1532 /* 1533 * Make sure that the ring buffer data structure doesn't get 1534 * freed while we dereference the ring buffer pointer. Test 1535 * for the channel's onchannel_callback being NULL within a 1536 * sched_lock critical section. See also the inline comments 1537 * in vmbus_reset_channel_cb(). 1538 */ 1539 spin_lock_irqsave(&channel->sched_lock, flags); 1540 if (unlikely(channel->onchannel_callback == NULL)) { 1541 spin_unlock_irqrestore(&channel->sched_lock, flags); 1542 goto enable_tasklet; 1543 } 1544 hv_pci_onchannelcallback(hbus); 1545 spin_unlock_irqrestore(&channel->sched_lock, flags); 1546 1547 if (hpdev->state == hv_pcichild_ejecting) { 1548 dev_err_once(&hbus->hdev->device, 1549 "the device is being ejected\n"); 1550 goto enable_tasklet; 1551 } 1552 1553 udelay(100); 1554 } 1555 1556 tasklet_enable(&channel->callback_event); 1557 1558 if (comp.comp_pkt.completion_status < 0) { 1559 dev_err(&hbus->hdev->device, 1560 "Request for interrupt failed: 0x%x", 1561 comp.comp_pkt.completion_status); 1562 goto free_int_desc; 1563 } 1564 1565 /* 1566 * Record the assignment so that this can be unwound later. Using 1567 * irq_set_chip_data() here would be appropriate, but the lock it takes 1568 * is already held. 1569 */ 1570 *int_desc = comp.int_desc; 1571 data->chip_data = int_desc; 1572 1573 /* Pass up the result. */ 1574 msg->address_hi = comp.int_desc.address >> 32; 1575 msg->address_lo = comp.int_desc.address & 0xffffffff; 1576 msg->data = comp.int_desc.data; 1577 1578 put_pcichild(hpdev); 1579 return; 1580 1581 enable_tasklet: 1582 tasklet_enable(&channel->callback_event); 1583 free_int_desc: 1584 kfree(int_desc); 1585 drop_reference: 1586 put_pcichild(hpdev); 1587 return_null_message: 1588 msg->address_hi = 0; 1589 msg->address_lo = 0; 1590 msg->data = 0; 1591 } 1592 1593 /* HW Interrupt Chip Descriptor */ 1594 static struct irq_chip hv_msi_irq_chip = { 1595 .name = "Hyper-V PCIe MSI", 1596 .irq_compose_msi_msg = hv_compose_msi_msg, 1597 .irq_set_affinity = hv_set_affinity, 1598 .irq_ack = irq_chip_ack_parent, 1599 .irq_mask = hv_irq_mask, 1600 .irq_unmask = hv_irq_unmask, 1601 }; 1602 1603 static struct msi_domain_ops hv_msi_ops = { 1604 .msi_prepare = pci_msi_prepare, 1605 .msi_free = hv_msi_free, 1606 }; 1607 1608 /** 1609 * hv_pcie_init_irq_domain() - Initialize IRQ domain 1610 * @hbus: The root PCI bus 1611 * 1612 * This function creates an IRQ domain which will be used for 1613 * interrupts from devices that have been passed through. These 1614 * devices only support MSI and MSI-X, not line-based interrupts 1615 * or simulations of line-based interrupts through PCIe's 1616 * fabric-layer messages. Because interrupts are remapped, we 1617 * can support multi-message MSI here. 1618 * 1619 * Return: '0' on success and error value on failure 1620 */ 1621 static int hv_pcie_init_irq_domain(struct hv_pcibus_device *hbus) 1622 { 1623 hbus->msi_info.chip = &hv_msi_irq_chip; 1624 hbus->msi_info.ops = &hv_msi_ops; 1625 hbus->msi_info.flags = (MSI_FLAG_USE_DEF_DOM_OPS | 1626 MSI_FLAG_USE_DEF_CHIP_OPS | MSI_FLAG_MULTI_PCI_MSI | 1627 MSI_FLAG_PCI_MSIX); 1628 hbus->msi_info.handler = handle_edge_irq; 1629 hbus->msi_info.handler_name = "edge"; 1630 hbus->msi_info.data = hbus; 1631 hbus->irq_domain = pci_msi_create_irq_domain(hbus->fwnode, 1632 &hbus->msi_info, 1633 x86_vector_domain); 1634 if (!hbus->irq_domain) { 1635 dev_err(&hbus->hdev->device, 1636 "Failed to build an MSI IRQ domain\n"); 1637 return -ENODEV; 1638 } 1639 1640 dev_set_msi_domain(&hbus->bridge->dev, hbus->irq_domain); 1641 1642 return 0; 1643 } 1644 1645 /** 1646 * get_bar_size() - Get the address space consumed by a BAR 1647 * @bar_val: Value that a BAR returned after -1 was written 1648 * to it. 1649 * 1650 * This function returns the size of the BAR, rounded up to 1 1651 * page. It has to be rounded up because the hypervisor's page 1652 * table entry that maps the BAR into the VM can't specify an 1653 * offset within a page. The invariant is that the hypervisor 1654 * must place any BARs of smaller than page length at the 1655 * beginning of a page. 1656 * 1657 * Return: Size in bytes of the consumed MMIO space. 1658 */ 1659 static u64 get_bar_size(u64 bar_val) 1660 { 1661 return round_up((1 + ~(bar_val & PCI_BASE_ADDRESS_MEM_MASK)), 1662 PAGE_SIZE); 1663 } 1664 1665 /** 1666 * survey_child_resources() - Total all MMIO requirements 1667 * @hbus: Root PCI bus, as understood by this driver 1668 */ 1669 static void survey_child_resources(struct hv_pcibus_device *hbus) 1670 { 1671 struct hv_pci_dev *hpdev; 1672 resource_size_t bar_size = 0; 1673 unsigned long flags; 1674 struct completion *event; 1675 u64 bar_val; 1676 int i; 1677 1678 /* If nobody is waiting on the answer, don't compute it. */ 1679 event = xchg(&hbus->survey_event, NULL); 1680 if (!event) 1681 return; 1682 1683 /* If the answer has already been computed, go with it. */ 1684 if (hbus->low_mmio_space || hbus->high_mmio_space) { 1685 complete(event); 1686 return; 1687 } 1688 1689 spin_lock_irqsave(&hbus->device_list_lock, flags); 1690 1691 /* 1692 * Due to an interesting quirk of the PCI spec, all memory regions 1693 * for a child device are a power of 2 in size and aligned in memory, 1694 * so it's sufficient to just add them up without tracking alignment. 1695 */ 1696 list_for_each_entry(hpdev, &hbus->children, list_entry) { 1697 for (i = 0; i < PCI_STD_NUM_BARS; i++) { 1698 if (hpdev->probed_bar[i] & PCI_BASE_ADDRESS_SPACE_IO) 1699 dev_err(&hbus->hdev->device, 1700 "There's an I/O BAR in this list!\n"); 1701 1702 if (hpdev->probed_bar[i] != 0) { 1703 /* 1704 * A probed BAR has all the upper bits set that 1705 * can be changed. 1706 */ 1707 1708 bar_val = hpdev->probed_bar[i]; 1709 if (bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64) 1710 bar_val |= 1711 ((u64)hpdev->probed_bar[++i] << 32); 1712 else 1713 bar_val |= 0xffffffff00000000ULL; 1714 1715 bar_size = get_bar_size(bar_val); 1716 1717 if (bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64) 1718 hbus->high_mmio_space += bar_size; 1719 else 1720 hbus->low_mmio_space += bar_size; 1721 } 1722 } 1723 } 1724 1725 spin_unlock_irqrestore(&hbus->device_list_lock, flags); 1726 complete(event); 1727 } 1728 1729 /** 1730 * prepopulate_bars() - Fill in BARs with defaults 1731 * @hbus: Root PCI bus, as understood by this driver 1732 * 1733 * The core PCI driver code seems much, much happier if the BARs 1734 * for a device have values upon first scan. So fill them in. 1735 * The algorithm below works down from large sizes to small, 1736 * attempting to pack the assignments optimally. The assumption, 1737 * enforced in other parts of the code, is that the beginning of 1738 * the memory-mapped I/O space will be aligned on the largest 1739 * BAR size. 1740 */ 1741 static void prepopulate_bars(struct hv_pcibus_device *hbus) 1742 { 1743 resource_size_t high_size = 0; 1744 resource_size_t low_size = 0; 1745 resource_size_t high_base = 0; 1746 resource_size_t low_base = 0; 1747 resource_size_t bar_size; 1748 struct hv_pci_dev *hpdev; 1749 unsigned long flags; 1750 u64 bar_val; 1751 u32 command; 1752 bool high; 1753 int i; 1754 1755 if (hbus->low_mmio_space) { 1756 low_size = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space)); 1757 low_base = hbus->low_mmio_res->start; 1758 } 1759 1760 if (hbus->high_mmio_space) { 1761 high_size = 1ULL << 1762 (63 - __builtin_clzll(hbus->high_mmio_space)); 1763 high_base = hbus->high_mmio_res->start; 1764 } 1765 1766 spin_lock_irqsave(&hbus->device_list_lock, flags); 1767 1768 /* 1769 * Clear the memory enable bit, in case it's already set. This occurs 1770 * in the suspend path of hibernation, where the device is suspended, 1771 * resumed and suspended again: see hibernation_snapshot() and 1772 * hibernation_platform_enter(). 1773 * 1774 * If the memory enable bit is already set, Hyper-V silently ignores 1775 * the below BAR updates, and the related PCI device driver can not 1776 * work, because reading from the device register(s) always returns 1777 * 0xFFFFFFFF. 1778 */ 1779 list_for_each_entry(hpdev, &hbus->children, list_entry) { 1780 _hv_pcifront_read_config(hpdev, PCI_COMMAND, 2, &command); 1781 command &= ~PCI_COMMAND_MEMORY; 1782 _hv_pcifront_write_config(hpdev, PCI_COMMAND, 2, command); 1783 } 1784 1785 /* Pick addresses for the BARs. */ 1786 do { 1787 list_for_each_entry(hpdev, &hbus->children, list_entry) { 1788 for (i = 0; i < PCI_STD_NUM_BARS; i++) { 1789 bar_val = hpdev->probed_bar[i]; 1790 if (bar_val == 0) 1791 continue; 1792 high = bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64; 1793 if (high) { 1794 bar_val |= 1795 ((u64)hpdev->probed_bar[i + 1] 1796 << 32); 1797 } else { 1798 bar_val |= 0xffffffffULL << 32; 1799 } 1800 bar_size = get_bar_size(bar_val); 1801 if (high) { 1802 if (high_size != bar_size) { 1803 i++; 1804 continue; 1805 } 1806 _hv_pcifront_write_config(hpdev, 1807 PCI_BASE_ADDRESS_0 + (4 * i), 1808 4, 1809 (u32)(high_base & 0xffffff00)); 1810 i++; 1811 _hv_pcifront_write_config(hpdev, 1812 PCI_BASE_ADDRESS_0 + (4 * i), 1813 4, (u32)(high_base >> 32)); 1814 high_base += bar_size; 1815 } else { 1816 if (low_size != bar_size) 1817 continue; 1818 _hv_pcifront_write_config(hpdev, 1819 PCI_BASE_ADDRESS_0 + (4 * i), 1820 4, 1821 (u32)(low_base & 0xffffff00)); 1822 low_base += bar_size; 1823 } 1824 } 1825 if (high_size <= 1 && low_size <= 1) { 1826 /* Set the memory enable bit. */ 1827 _hv_pcifront_read_config(hpdev, PCI_COMMAND, 2, 1828 &command); 1829 command |= PCI_COMMAND_MEMORY; 1830 _hv_pcifront_write_config(hpdev, PCI_COMMAND, 2, 1831 command); 1832 break; 1833 } 1834 } 1835 1836 high_size >>= 1; 1837 low_size >>= 1; 1838 } while (high_size || low_size); 1839 1840 spin_unlock_irqrestore(&hbus->device_list_lock, flags); 1841 } 1842 1843 /* 1844 * Assign entries in sysfs pci slot directory. 1845 * 1846 * Note that this function does not need to lock the children list 1847 * because it is called from pci_devices_present_work which 1848 * is serialized with hv_eject_device_work because they are on the 1849 * same ordered workqueue. Therefore hbus->children list will not change 1850 * even when pci_create_slot sleeps. 1851 */ 1852 static void hv_pci_assign_slots(struct hv_pcibus_device *hbus) 1853 { 1854 struct hv_pci_dev *hpdev; 1855 char name[SLOT_NAME_SIZE]; 1856 int slot_nr; 1857 1858 list_for_each_entry(hpdev, &hbus->children, list_entry) { 1859 if (hpdev->pci_slot) 1860 continue; 1861 1862 slot_nr = PCI_SLOT(wslot_to_devfn(hpdev->desc.win_slot.slot)); 1863 snprintf(name, SLOT_NAME_SIZE, "%u", hpdev->desc.ser); 1864 hpdev->pci_slot = pci_create_slot(hbus->bridge->bus, slot_nr, 1865 name, NULL); 1866 if (IS_ERR(hpdev->pci_slot)) { 1867 pr_warn("pci_create slot %s failed\n", name); 1868 hpdev->pci_slot = NULL; 1869 } 1870 } 1871 } 1872 1873 /* 1874 * Remove entries in sysfs pci slot directory. 1875 */ 1876 static void hv_pci_remove_slots(struct hv_pcibus_device *hbus) 1877 { 1878 struct hv_pci_dev *hpdev; 1879 1880 list_for_each_entry(hpdev, &hbus->children, list_entry) { 1881 if (!hpdev->pci_slot) 1882 continue; 1883 pci_destroy_slot(hpdev->pci_slot); 1884 hpdev->pci_slot = NULL; 1885 } 1886 } 1887 1888 /* 1889 * Set NUMA node for the devices on the bus 1890 */ 1891 static void hv_pci_assign_numa_node(struct hv_pcibus_device *hbus) 1892 { 1893 struct pci_dev *dev; 1894 struct pci_bus *bus = hbus->bridge->bus; 1895 struct hv_pci_dev *hv_dev; 1896 1897 list_for_each_entry(dev, &bus->devices, bus_list) { 1898 hv_dev = get_pcichild_wslot(hbus, devfn_to_wslot(dev->devfn)); 1899 if (!hv_dev) 1900 continue; 1901 1902 if (hv_dev->desc.flags & HV_PCI_DEVICE_FLAG_NUMA_AFFINITY) 1903 set_dev_node(&dev->dev, hv_dev->desc.virtual_numa_node); 1904 1905 put_pcichild(hv_dev); 1906 } 1907 } 1908 1909 /** 1910 * create_root_hv_pci_bus() - Expose a new root PCI bus 1911 * @hbus: Root PCI bus, as understood by this driver 1912 * 1913 * Return: 0 on success, -errno on failure 1914 */ 1915 static int create_root_hv_pci_bus(struct hv_pcibus_device *hbus) 1916 { 1917 int error; 1918 struct pci_host_bridge *bridge = hbus->bridge; 1919 1920 bridge->dev.parent = &hbus->hdev->device; 1921 bridge->sysdata = &hbus->sysdata; 1922 bridge->ops = &hv_pcifront_ops; 1923 1924 error = pci_scan_root_bus_bridge(bridge); 1925 if (error) 1926 return error; 1927 1928 pci_lock_rescan_remove(); 1929 hv_pci_assign_numa_node(hbus); 1930 pci_bus_assign_resources(bridge->bus); 1931 hv_pci_assign_slots(hbus); 1932 pci_bus_add_devices(bridge->bus); 1933 pci_unlock_rescan_remove(); 1934 hbus->state = hv_pcibus_installed; 1935 return 0; 1936 } 1937 1938 struct q_res_req_compl { 1939 struct completion host_event; 1940 struct hv_pci_dev *hpdev; 1941 }; 1942 1943 /** 1944 * q_resource_requirements() - Query Resource Requirements 1945 * @context: The completion context. 1946 * @resp: The response that came from the host. 1947 * @resp_packet_size: The size in bytes of resp. 1948 * 1949 * This function is invoked on completion of a Query Resource 1950 * Requirements packet. 1951 */ 1952 static void q_resource_requirements(void *context, struct pci_response *resp, 1953 int resp_packet_size) 1954 { 1955 struct q_res_req_compl *completion = context; 1956 struct pci_q_res_req_response *q_res_req = 1957 (struct pci_q_res_req_response *)resp; 1958 int i; 1959 1960 if (resp->status < 0) { 1961 dev_err(&completion->hpdev->hbus->hdev->device, 1962 "query resource requirements failed: %x\n", 1963 resp->status); 1964 } else { 1965 for (i = 0; i < PCI_STD_NUM_BARS; i++) { 1966 completion->hpdev->probed_bar[i] = 1967 q_res_req->probed_bar[i]; 1968 } 1969 } 1970 1971 complete(&completion->host_event); 1972 } 1973 1974 /** 1975 * new_pcichild_device() - Create a new child device 1976 * @hbus: The internal struct tracking this root PCI bus. 1977 * @desc: The information supplied so far from the host 1978 * about the device. 1979 * 1980 * This function creates the tracking structure for a new child 1981 * device and kicks off the process of figuring out what it is. 1982 * 1983 * Return: Pointer to the new tracking struct 1984 */ 1985 static struct hv_pci_dev *new_pcichild_device(struct hv_pcibus_device *hbus, 1986 struct hv_pcidev_description *desc) 1987 { 1988 struct hv_pci_dev *hpdev; 1989 struct pci_child_message *res_req; 1990 struct q_res_req_compl comp_pkt; 1991 struct { 1992 struct pci_packet init_packet; 1993 u8 buffer[sizeof(struct pci_child_message)]; 1994 } pkt; 1995 unsigned long flags; 1996 int ret; 1997 1998 hpdev = kzalloc(sizeof(*hpdev), GFP_KERNEL); 1999 if (!hpdev) 2000 return NULL; 2001 2002 hpdev->hbus = hbus; 2003 2004 memset(&pkt, 0, sizeof(pkt)); 2005 init_completion(&comp_pkt.host_event); 2006 comp_pkt.hpdev = hpdev; 2007 pkt.init_packet.compl_ctxt = &comp_pkt; 2008 pkt.init_packet.completion_func = q_resource_requirements; 2009 res_req = (struct pci_child_message *)&pkt.init_packet.message; 2010 res_req->message_type.type = PCI_QUERY_RESOURCE_REQUIREMENTS; 2011 res_req->wslot.slot = desc->win_slot.slot; 2012 2013 ret = vmbus_sendpacket(hbus->hdev->channel, res_req, 2014 sizeof(struct pci_child_message), 2015 (unsigned long)&pkt.init_packet, 2016 VM_PKT_DATA_INBAND, 2017 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); 2018 if (ret) 2019 goto error; 2020 2021 if (wait_for_response(hbus->hdev, &comp_pkt.host_event)) 2022 goto error; 2023 2024 hpdev->desc = *desc; 2025 refcount_set(&hpdev->refs, 1); 2026 get_pcichild(hpdev); 2027 spin_lock_irqsave(&hbus->device_list_lock, flags); 2028 2029 list_add_tail(&hpdev->list_entry, &hbus->children); 2030 spin_unlock_irqrestore(&hbus->device_list_lock, flags); 2031 return hpdev; 2032 2033 error: 2034 kfree(hpdev); 2035 return NULL; 2036 } 2037 2038 /** 2039 * get_pcichild_wslot() - Find device from slot 2040 * @hbus: Root PCI bus, as understood by this driver 2041 * @wslot: Location on the bus 2042 * 2043 * This function looks up a PCI device and returns the internal 2044 * representation of it. It acquires a reference on it, so that 2045 * the device won't be deleted while somebody is using it. The 2046 * caller is responsible for calling put_pcichild() to release 2047 * this reference. 2048 * 2049 * Return: Internal representation of a PCI device 2050 */ 2051 static struct hv_pci_dev *get_pcichild_wslot(struct hv_pcibus_device *hbus, 2052 u32 wslot) 2053 { 2054 unsigned long flags; 2055 struct hv_pci_dev *iter, *hpdev = NULL; 2056 2057 spin_lock_irqsave(&hbus->device_list_lock, flags); 2058 list_for_each_entry(iter, &hbus->children, list_entry) { 2059 if (iter->desc.win_slot.slot == wslot) { 2060 hpdev = iter; 2061 get_pcichild(hpdev); 2062 break; 2063 } 2064 } 2065 spin_unlock_irqrestore(&hbus->device_list_lock, flags); 2066 2067 return hpdev; 2068 } 2069 2070 /** 2071 * pci_devices_present_work() - Handle new list of child devices 2072 * @work: Work struct embedded in struct hv_dr_work 2073 * 2074 * "Bus Relations" is the Windows term for "children of this 2075 * bus." The terminology is preserved here for people trying to 2076 * debug the interaction between Hyper-V and Linux. This 2077 * function is called when the parent partition reports a list 2078 * of functions that should be observed under this PCI Express 2079 * port (bus). 2080 * 2081 * This function updates the list, and must tolerate being 2082 * called multiple times with the same information. The typical 2083 * number of child devices is one, with very atypical cases 2084 * involving three or four, so the algorithms used here can be 2085 * simple and inefficient. 2086 * 2087 * It must also treat the omission of a previously observed device as 2088 * notification that the device no longer exists. 2089 * 2090 * Note that this function is serialized with hv_eject_device_work(), 2091 * because both are pushed to the ordered workqueue hbus->wq. 2092 */ 2093 static void pci_devices_present_work(struct work_struct *work) 2094 { 2095 u32 child_no; 2096 bool found; 2097 struct hv_pcidev_description *new_desc; 2098 struct hv_pci_dev *hpdev; 2099 struct hv_pcibus_device *hbus; 2100 struct list_head removed; 2101 struct hv_dr_work *dr_wrk; 2102 struct hv_dr_state *dr = NULL; 2103 unsigned long flags; 2104 2105 dr_wrk = container_of(work, struct hv_dr_work, wrk); 2106 hbus = dr_wrk->bus; 2107 kfree(dr_wrk); 2108 2109 INIT_LIST_HEAD(&removed); 2110 2111 /* Pull this off the queue and process it if it was the last one. */ 2112 spin_lock_irqsave(&hbus->device_list_lock, flags); 2113 while (!list_empty(&hbus->dr_list)) { 2114 dr = list_first_entry(&hbus->dr_list, struct hv_dr_state, 2115 list_entry); 2116 list_del(&dr->list_entry); 2117 2118 /* Throw this away if the list still has stuff in it. */ 2119 if (!list_empty(&hbus->dr_list)) { 2120 kfree(dr); 2121 continue; 2122 } 2123 } 2124 spin_unlock_irqrestore(&hbus->device_list_lock, flags); 2125 2126 if (!dr) 2127 return; 2128 2129 /* First, mark all existing children as reported missing. */ 2130 spin_lock_irqsave(&hbus->device_list_lock, flags); 2131 list_for_each_entry(hpdev, &hbus->children, list_entry) { 2132 hpdev->reported_missing = true; 2133 } 2134 spin_unlock_irqrestore(&hbus->device_list_lock, flags); 2135 2136 /* Next, add back any reported devices. */ 2137 for (child_no = 0; child_no < dr->device_count; child_no++) { 2138 found = false; 2139 new_desc = &dr->func[child_no]; 2140 2141 spin_lock_irqsave(&hbus->device_list_lock, flags); 2142 list_for_each_entry(hpdev, &hbus->children, list_entry) { 2143 if ((hpdev->desc.win_slot.slot == new_desc->win_slot.slot) && 2144 (hpdev->desc.v_id == new_desc->v_id) && 2145 (hpdev->desc.d_id == new_desc->d_id) && 2146 (hpdev->desc.ser == new_desc->ser)) { 2147 hpdev->reported_missing = false; 2148 found = true; 2149 } 2150 } 2151 spin_unlock_irqrestore(&hbus->device_list_lock, flags); 2152 2153 if (!found) { 2154 hpdev = new_pcichild_device(hbus, new_desc); 2155 if (!hpdev) 2156 dev_err(&hbus->hdev->device, 2157 "couldn't record a child device.\n"); 2158 } 2159 } 2160 2161 /* Move missing children to a list on the stack. */ 2162 spin_lock_irqsave(&hbus->device_list_lock, flags); 2163 do { 2164 found = false; 2165 list_for_each_entry(hpdev, &hbus->children, list_entry) { 2166 if (hpdev->reported_missing) { 2167 found = true; 2168 put_pcichild(hpdev); 2169 list_move_tail(&hpdev->list_entry, &removed); 2170 break; 2171 } 2172 } 2173 } while (found); 2174 spin_unlock_irqrestore(&hbus->device_list_lock, flags); 2175 2176 /* Delete everything that should no longer exist. */ 2177 while (!list_empty(&removed)) { 2178 hpdev = list_first_entry(&removed, struct hv_pci_dev, 2179 list_entry); 2180 list_del(&hpdev->list_entry); 2181 2182 if (hpdev->pci_slot) 2183 pci_destroy_slot(hpdev->pci_slot); 2184 2185 put_pcichild(hpdev); 2186 } 2187 2188 switch (hbus->state) { 2189 case hv_pcibus_installed: 2190 /* 2191 * Tell the core to rescan bus 2192 * because there may have been changes. 2193 */ 2194 pci_lock_rescan_remove(); 2195 pci_scan_child_bus(hbus->bridge->bus); 2196 hv_pci_assign_numa_node(hbus); 2197 hv_pci_assign_slots(hbus); 2198 pci_unlock_rescan_remove(); 2199 break; 2200 2201 case hv_pcibus_init: 2202 case hv_pcibus_probed: 2203 survey_child_resources(hbus); 2204 break; 2205 2206 default: 2207 break; 2208 } 2209 2210 kfree(dr); 2211 } 2212 2213 /** 2214 * hv_pci_start_relations_work() - Queue work to start device discovery 2215 * @hbus: Root PCI bus, as understood by this driver 2216 * @dr: The list of children returned from host 2217 * 2218 * Return: 0 on success, -errno on failure 2219 */ 2220 static int hv_pci_start_relations_work(struct hv_pcibus_device *hbus, 2221 struct hv_dr_state *dr) 2222 { 2223 struct hv_dr_work *dr_wrk; 2224 unsigned long flags; 2225 bool pending_dr; 2226 2227 if (hbus->state == hv_pcibus_removing) { 2228 dev_info(&hbus->hdev->device, 2229 "PCI VMBus BUS_RELATIONS: ignored\n"); 2230 return -ENOENT; 2231 } 2232 2233 dr_wrk = kzalloc(sizeof(*dr_wrk), GFP_NOWAIT); 2234 if (!dr_wrk) 2235 return -ENOMEM; 2236 2237 INIT_WORK(&dr_wrk->wrk, pci_devices_present_work); 2238 dr_wrk->bus = hbus; 2239 2240 spin_lock_irqsave(&hbus->device_list_lock, flags); 2241 /* 2242 * If pending_dr is true, we have already queued a work, 2243 * which will see the new dr. Otherwise, we need to 2244 * queue a new work. 2245 */ 2246 pending_dr = !list_empty(&hbus->dr_list); 2247 list_add_tail(&dr->list_entry, &hbus->dr_list); 2248 spin_unlock_irqrestore(&hbus->device_list_lock, flags); 2249 2250 if (pending_dr) 2251 kfree(dr_wrk); 2252 else 2253 queue_work(hbus->wq, &dr_wrk->wrk); 2254 2255 return 0; 2256 } 2257 2258 /** 2259 * hv_pci_devices_present() - Handle list of new children 2260 * @hbus: Root PCI bus, as understood by this driver 2261 * @relations: Packet from host listing children 2262 * 2263 * Process a new list of devices on the bus. The list of devices is 2264 * discovered by VSP and sent to us via VSP message PCI_BUS_RELATIONS, 2265 * whenever a new list of devices for this bus appears. 2266 */ 2267 static void hv_pci_devices_present(struct hv_pcibus_device *hbus, 2268 struct pci_bus_relations *relations) 2269 { 2270 struct hv_dr_state *dr; 2271 int i; 2272 2273 dr = kzalloc(struct_size(dr, func, relations->device_count), 2274 GFP_NOWAIT); 2275 if (!dr) 2276 return; 2277 2278 dr->device_count = relations->device_count; 2279 for (i = 0; i < dr->device_count; i++) { 2280 dr->func[i].v_id = relations->func[i].v_id; 2281 dr->func[i].d_id = relations->func[i].d_id; 2282 dr->func[i].rev = relations->func[i].rev; 2283 dr->func[i].prog_intf = relations->func[i].prog_intf; 2284 dr->func[i].subclass = relations->func[i].subclass; 2285 dr->func[i].base_class = relations->func[i].base_class; 2286 dr->func[i].subsystem_id = relations->func[i].subsystem_id; 2287 dr->func[i].win_slot = relations->func[i].win_slot; 2288 dr->func[i].ser = relations->func[i].ser; 2289 } 2290 2291 if (hv_pci_start_relations_work(hbus, dr)) 2292 kfree(dr); 2293 } 2294 2295 /** 2296 * hv_pci_devices_present2() - Handle list of new children 2297 * @hbus: Root PCI bus, as understood by this driver 2298 * @relations: Packet from host listing children 2299 * 2300 * This function is the v2 version of hv_pci_devices_present() 2301 */ 2302 static void hv_pci_devices_present2(struct hv_pcibus_device *hbus, 2303 struct pci_bus_relations2 *relations) 2304 { 2305 struct hv_dr_state *dr; 2306 int i; 2307 2308 dr = kzalloc(struct_size(dr, func, relations->device_count), 2309 GFP_NOWAIT); 2310 if (!dr) 2311 return; 2312 2313 dr->device_count = relations->device_count; 2314 for (i = 0; i < dr->device_count; i++) { 2315 dr->func[i].v_id = relations->func[i].v_id; 2316 dr->func[i].d_id = relations->func[i].d_id; 2317 dr->func[i].rev = relations->func[i].rev; 2318 dr->func[i].prog_intf = relations->func[i].prog_intf; 2319 dr->func[i].subclass = relations->func[i].subclass; 2320 dr->func[i].base_class = relations->func[i].base_class; 2321 dr->func[i].subsystem_id = relations->func[i].subsystem_id; 2322 dr->func[i].win_slot = relations->func[i].win_slot; 2323 dr->func[i].ser = relations->func[i].ser; 2324 dr->func[i].flags = relations->func[i].flags; 2325 dr->func[i].virtual_numa_node = 2326 relations->func[i].virtual_numa_node; 2327 } 2328 2329 if (hv_pci_start_relations_work(hbus, dr)) 2330 kfree(dr); 2331 } 2332 2333 /** 2334 * hv_eject_device_work() - Asynchronously handles ejection 2335 * @work: Work struct embedded in internal device struct 2336 * 2337 * This function handles ejecting a device. Windows will 2338 * attempt to gracefully eject a device, waiting 60 seconds to 2339 * hear back from the guest OS that this completed successfully. 2340 * If this timer expires, the device will be forcibly removed. 2341 */ 2342 static void hv_eject_device_work(struct work_struct *work) 2343 { 2344 struct pci_eject_response *ejct_pkt; 2345 struct hv_pcibus_device *hbus; 2346 struct hv_pci_dev *hpdev; 2347 struct pci_dev *pdev; 2348 unsigned long flags; 2349 int wslot; 2350 struct { 2351 struct pci_packet pkt; 2352 u8 buffer[sizeof(struct pci_eject_response)]; 2353 } ctxt; 2354 2355 hpdev = container_of(work, struct hv_pci_dev, wrk); 2356 hbus = hpdev->hbus; 2357 2358 WARN_ON(hpdev->state != hv_pcichild_ejecting); 2359 2360 /* 2361 * Ejection can come before or after the PCI bus has been set up, so 2362 * attempt to find it and tear down the bus state, if it exists. This 2363 * must be done without constructs like pci_domain_nr(hbus->bridge->bus) 2364 * because hbus->bridge->bus may not exist yet. 2365 */ 2366 wslot = wslot_to_devfn(hpdev->desc.win_slot.slot); 2367 pdev = pci_get_domain_bus_and_slot(hbus->bridge->domain_nr, 0, wslot); 2368 if (pdev) { 2369 pci_lock_rescan_remove(); 2370 pci_stop_and_remove_bus_device(pdev); 2371 pci_dev_put(pdev); 2372 pci_unlock_rescan_remove(); 2373 } 2374 2375 spin_lock_irqsave(&hbus->device_list_lock, flags); 2376 list_del(&hpdev->list_entry); 2377 spin_unlock_irqrestore(&hbus->device_list_lock, flags); 2378 2379 if (hpdev->pci_slot) 2380 pci_destroy_slot(hpdev->pci_slot); 2381 2382 memset(&ctxt, 0, sizeof(ctxt)); 2383 ejct_pkt = (struct pci_eject_response *)&ctxt.pkt.message; 2384 ejct_pkt->message_type.type = PCI_EJECTION_COMPLETE; 2385 ejct_pkt->wslot.slot = hpdev->desc.win_slot.slot; 2386 vmbus_sendpacket(hbus->hdev->channel, ejct_pkt, 2387 sizeof(*ejct_pkt), (unsigned long)&ctxt.pkt, 2388 VM_PKT_DATA_INBAND, 0); 2389 2390 /* For the get_pcichild() in hv_pci_eject_device() */ 2391 put_pcichild(hpdev); 2392 /* For the two refs got in new_pcichild_device() */ 2393 put_pcichild(hpdev); 2394 put_pcichild(hpdev); 2395 /* hpdev has been freed. Do not use it any more. */ 2396 } 2397 2398 /** 2399 * hv_pci_eject_device() - Handles device ejection 2400 * @hpdev: Internal device tracking struct 2401 * 2402 * This function is invoked when an ejection packet arrives. It 2403 * just schedules work so that we don't re-enter the packet 2404 * delivery code handling the ejection. 2405 */ 2406 static void hv_pci_eject_device(struct hv_pci_dev *hpdev) 2407 { 2408 struct hv_pcibus_device *hbus = hpdev->hbus; 2409 struct hv_device *hdev = hbus->hdev; 2410 2411 if (hbus->state == hv_pcibus_removing) { 2412 dev_info(&hdev->device, "PCI VMBus EJECT: ignored\n"); 2413 return; 2414 } 2415 2416 hpdev->state = hv_pcichild_ejecting; 2417 get_pcichild(hpdev); 2418 INIT_WORK(&hpdev->wrk, hv_eject_device_work); 2419 queue_work(hbus->wq, &hpdev->wrk); 2420 } 2421 2422 /** 2423 * hv_pci_onchannelcallback() - Handles incoming packets 2424 * @context: Internal bus tracking struct 2425 * 2426 * This function is invoked whenever the host sends a packet to 2427 * this channel (which is private to this root PCI bus). 2428 */ 2429 static void hv_pci_onchannelcallback(void *context) 2430 { 2431 const int packet_size = 0x100; 2432 int ret; 2433 struct hv_pcibus_device *hbus = context; 2434 u32 bytes_recvd; 2435 u64 req_id; 2436 struct vmpacket_descriptor *desc; 2437 unsigned char *buffer; 2438 int bufferlen = packet_size; 2439 struct pci_packet *comp_packet; 2440 struct pci_response *response; 2441 struct pci_incoming_message *new_message; 2442 struct pci_bus_relations *bus_rel; 2443 struct pci_bus_relations2 *bus_rel2; 2444 struct pci_dev_inval_block *inval; 2445 struct pci_dev_incoming *dev_message; 2446 struct hv_pci_dev *hpdev; 2447 2448 buffer = kmalloc(bufferlen, GFP_ATOMIC); 2449 if (!buffer) 2450 return; 2451 2452 while (1) { 2453 ret = vmbus_recvpacket_raw(hbus->hdev->channel, buffer, 2454 bufferlen, &bytes_recvd, &req_id); 2455 2456 if (ret == -ENOBUFS) { 2457 kfree(buffer); 2458 /* Handle large packet */ 2459 bufferlen = bytes_recvd; 2460 buffer = kmalloc(bytes_recvd, GFP_ATOMIC); 2461 if (!buffer) 2462 return; 2463 continue; 2464 } 2465 2466 /* Zero length indicates there are no more packets. */ 2467 if (ret || !bytes_recvd) 2468 break; 2469 2470 /* 2471 * All incoming packets must be at least as large as a 2472 * response. 2473 */ 2474 if (bytes_recvd <= sizeof(struct pci_response)) 2475 continue; 2476 desc = (struct vmpacket_descriptor *)buffer; 2477 2478 switch (desc->type) { 2479 case VM_PKT_COMP: 2480 2481 /* 2482 * The host is trusted, and thus it's safe to interpret 2483 * this transaction ID as a pointer. 2484 */ 2485 comp_packet = (struct pci_packet *)req_id; 2486 response = (struct pci_response *)buffer; 2487 comp_packet->completion_func(comp_packet->compl_ctxt, 2488 response, 2489 bytes_recvd); 2490 break; 2491 2492 case VM_PKT_DATA_INBAND: 2493 2494 new_message = (struct pci_incoming_message *)buffer; 2495 switch (new_message->message_type.type) { 2496 case PCI_BUS_RELATIONS: 2497 2498 bus_rel = (struct pci_bus_relations *)buffer; 2499 if (bytes_recvd < 2500 struct_size(bus_rel, func, 2501 bus_rel->device_count)) { 2502 dev_err(&hbus->hdev->device, 2503 "bus relations too small\n"); 2504 break; 2505 } 2506 2507 hv_pci_devices_present(hbus, bus_rel); 2508 break; 2509 2510 case PCI_BUS_RELATIONS2: 2511 2512 bus_rel2 = (struct pci_bus_relations2 *)buffer; 2513 if (bytes_recvd < 2514 struct_size(bus_rel2, func, 2515 bus_rel2->device_count)) { 2516 dev_err(&hbus->hdev->device, 2517 "bus relations v2 too small\n"); 2518 break; 2519 } 2520 2521 hv_pci_devices_present2(hbus, bus_rel2); 2522 break; 2523 2524 case PCI_EJECT: 2525 2526 dev_message = (struct pci_dev_incoming *)buffer; 2527 hpdev = get_pcichild_wslot(hbus, 2528 dev_message->wslot.slot); 2529 if (hpdev) { 2530 hv_pci_eject_device(hpdev); 2531 put_pcichild(hpdev); 2532 } 2533 break; 2534 2535 case PCI_INVALIDATE_BLOCK: 2536 2537 inval = (struct pci_dev_inval_block *)buffer; 2538 hpdev = get_pcichild_wslot(hbus, 2539 inval->wslot.slot); 2540 if (hpdev) { 2541 if (hpdev->block_invalidate) { 2542 hpdev->block_invalidate( 2543 hpdev->invalidate_context, 2544 inval->block_mask); 2545 } 2546 put_pcichild(hpdev); 2547 } 2548 break; 2549 2550 default: 2551 dev_warn(&hbus->hdev->device, 2552 "Unimplemented protocol message %x\n", 2553 new_message->message_type.type); 2554 break; 2555 } 2556 break; 2557 2558 default: 2559 dev_err(&hbus->hdev->device, 2560 "unhandled packet type %d, tid %llx len %d\n", 2561 desc->type, req_id, bytes_recvd); 2562 break; 2563 } 2564 } 2565 2566 kfree(buffer); 2567 } 2568 2569 /** 2570 * hv_pci_protocol_negotiation() - Set up protocol 2571 * @hdev: VMBus's tracking struct for this root PCI bus. 2572 * @version: Array of supported channel protocol versions in 2573 * the order of probing - highest go first. 2574 * @num_version: Number of elements in the version array. 2575 * 2576 * This driver is intended to support running on Windows 10 2577 * (server) and later versions. It will not run on earlier 2578 * versions, as they assume that many of the operations which 2579 * Linux needs accomplished with a spinlock held were done via 2580 * asynchronous messaging via VMBus. Windows 10 increases the 2581 * surface area of PCI emulation so that these actions can take 2582 * place by suspending a virtual processor for their duration. 2583 * 2584 * This function negotiates the channel protocol version, 2585 * failing if the host doesn't support the necessary protocol 2586 * level. 2587 */ 2588 static int hv_pci_protocol_negotiation(struct hv_device *hdev, 2589 enum pci_protocol_version_t version[], 2590 int num_version) 2591 { 2592 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev); 2593 struct pci_version_request *version_req; 2594 struct hv_pci_compl comp_pkt; 2595 struct pci_packet *pkt; 2596 int ret; 2597 int i; 2598 2599 /* 2600 * Initiate the handshake with the host and negotiate 2601 * a version that the host can support. We start with the 2602 * highest version number and go down if the host cannot 2603 * support it. 2604 */ 2605 pkt = kzalloc(sizeof(*pkt) + sizeof(*version_req), GFP_KERNEL); 2606 if (!pkt) 2607 return -ENOMEM; 2608 2609 init_completion(&comp_pkt.host_event); 2610 pkt->completion_func = hv_pci_generic_compl; 2611 pkt->compl_ctxt = &comp_pkt; 2612 version_req = (struct pci_version_request *)&pkt->message; 2613 version_req->message_type.type = PCI_QUERY_PROTOCOL_VERSION; 2614 2615 for (i = 0; i < num_version; i++) { 2616 version_req->protocol_version = version[i]; 2617 ret = vmbus_sendpacket(hdev->channel, version_req, 2618 sizeof(struct pci_version_request), 2619 (unsigned long)pkt, VM_PKT_DATA_INBAND, 2620 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); 2621 if (!ret) 2622 ret = wait_for_response(hdev, &comp_pkt.host_event); 2623 2624 if (ret) { 2625 dev_err(&hdev->device, 2626 "PCI Pass-through VSP failed to request version: %d", 2627 ret); 2628 goto exit; 2629 } 2630 2631 if (comp_pkt.completion_status >= 0) { 2632 hbus->protocol_version = version[i]; 2633 dev_info(&hdev->device, 2634 "PCI VMBus probing: Using version %#x\n", 2635 hbus->protocol_version); 2636 goto exit; 2637 } 2638 2639 if (comp_pkt.completion_status != STATUS_REVISION_MISMATCH) { 2640 dev_err(&hdev->device, 2641 "PCI Pass-through VSP failed version request: %#x", 2642 comp_pkt.completion_status); 2643 ret = -EPROTO; 2644 goto exit; 2645 } 2646 2647 reinit_completion(&comp_pkt.host_event); 2648 } 2649 2650 dev_err(&hdev->device, 2651 "PCI pass-through VSP failed to find supported version"); 2652 ret = -EPROTO; 2653 2654 exit: 2655 kfree(pkt); 2656 return ret; 2657 } 2658 2659 /** 2660 * hv_pci_free_bridge_windows() - Release memory regions for the 2661 * bus 2662 * @hbus: Root PCI bus, as understood by this driver 2663 */ 2664 static void hv_pci_free_bridge_windows(struct hv_pcibus_device *hbus) 2665 { 2666 /* 2667 * Set the resources back to the way they looked when they 2668 * were allocated by setting IORESOURCE_BUSY again. 2669 */ 2670 2671 if (hbus->low_mmio_space && hbus->low_mmio_res) { 2672 hbus->low_mmio_res->flags |= IORESOURCE_BUSY; 2673 vmbus_free_mmio(hbus->low_mmio_res->start, 2674 resource_size(hbus->low_mmio_res)); 2675 } 2676 2677 if (hbus->high_mmio_space && hbus->high_mmio_res) { 2678 hbus->high_mmio_res->flags |= IORESOURCE_BUSY; 2679 vmbus_free_mmio(hbus->high_mmio_res->start, 2680 resource_size(hbus->high_mmio_res)); 2681 } 2682 } 2683 2684 /** 2685 * hv_pci_allocate_bridge_windows() - Allocate memory regions 2686 * for the bus 2687 * @hbus: Root PCI bus, as understood by this driver 2688 * 2689 * This function calls vmbus_allocate_mmio(), which is itself a 2690 * bit of a compromise. Ideally, we might change the pnp layer 2691 * in the kernel such that it comprehends either PCI devices 2692 * which are "grandchildren of ACPI," with some intermediate bus 2693 * node (in this case, VMBus) or change it such that it 2694 * understands VMBus. The pnp layer, however, has been declared 2695 * deprecated, and not subject to change. 2696 * 2697 * The workaround, implemented here, is to ask VMBus to allocate 2698 * MMIO space for this bus. VMBus itself knows which ranges are 2699 * appropriate by looking at its own ACPI objects. Then, after 2700 * these ranges are claimed, they're modified to look like they 2701 * would have looked if the ACPI and pnp code had allocated 2702 * bridge windows. These descriptors have to exist in this form 2703 * in order to satisfy the code which will get invoked when the 2704 * endpoint PCI function driver calls request_mem_region() or 2705 * request_mem_region_exclusive(). 2706 * 2707 * Return: 0 on success, -errno on failure 2708 */ 2709 static int hv_pci_allocate_bridge_windows(struct hv_pcibus_device *hbus) 2710 { 2711 resource_size_t align; 2712 int ret; 2713 2714 if (hbus->low_mmio_space) { 2715 align = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space)); 2716 ret = vmbus_allocate_mmio(&hbus->low_mmio_res, hbus->hdev, 0, 2717 (u64)(u32)0xffffffff, 2718 hbus->low_mmio_space, 2719 align, false); 2720 if (ret) { 2721 dev_err(&hbus->hdev->device, 2722 "Need %#llx of low MMIO space. Consider reconfiguring the VM.\n", 2723 hbus->low_mmio_space); 2724 return ret; 2725 } 2726 2727 /* Modify this resource to become a bridge window. */ 2728 hbus->low_mmio_res->flags |= IORESOURCE_WINDOW; 2729 hbus->low_mmio_res->flags &= ~IORESOURCE_BUSY; 2730 pci_add_resource(&hbus->bridge->windows, hbus->low_mmio_res); 2731 } 2732 2733 if (hbus->high_mmio_space) { 2734 align = 1ULL << (63 - __builtin_clzll(hbus->high_mmio_space)); 2735 ret = vmbus_allocate_mmio(&hbus->high_mmio_res, hbus->hdev, 2736 0x100000000, -1, 2737 hbus->high_mmio_space, align, 2738 false); 2739 if (ret) { 2740 dev_err(&hbus->hdev->device, 2741 "Need %#llx of high MMIO space. Consider reconfiguring the VM.\n", 2742 hbus->high_mmio_space); 2743 goto release_low_mmio; 2744 } 2745 2746 /* Modify this resource to become a bridge window. */ 2747 hbus->high_mmio_res->flags |= IORESOURCE_WINDOW; 2748 hbus->high_mmio_res->flags &= ~IORESOURCE_BUSY; 2749 pci_add_resource(&hbus->bridge->windows, hbus->high_mmio_res); 2750 } 2751 2752 return 0; 2753 2754 release_low_mmio: 2755 if (hbus->low_mmio_res) { 2756 vmbus_free_mmio(hbus->low_mmio_res->start, 2757 resource_size(hbus->low_mmio_res)); 2758 } 2759 2760 return ret; 2761 } 2762 2763 /** 2764 * hv_allocate_config_window() - Find MMIO space for PCI Config 2765 * @hbus: Root PCI bus, as understood by this driver 2766 * 2767 * This function claims memory-mapped I/O space for accessing 2768 * configuration space for the functions on this bus. 2769 * 2770 * Return: 0 on success, -errno on failure 2771 */ 2772 static int hv_allocate_config_window(struct hv_pcibus_device *hbus) 2773 { 2774 int ret; 2775 2776 /* 2777 * Set up a region of MMIO space to use for accessing configuration 2778 * space. 2779 */ 2780 ret = vmbus_allocate_mmio(&hbus->mem_config, hbus->hdev, 0, -1, 2781 PCI_CONFIG_MMIO_LENGTH, 0x1000, false); 2782 if (ret) 2783 return ret; 2784 2785 /* 2786 * vmbus_allocate_mmio() gets used for allocating both device endpoint 2787 * resource claims (those which cannot be overlapped) and the ranges 2788 * which are valid for the children of this bus, which are intended 2789 * to be overlapped by those children. Set the flag on this claim 2790 * meaning that this region can't be overlapped. 2791 */ 2792 2793 hbus->mem_config->flags |= IORESOURCE_BUSY; 2794 2795 return 0; 2796 } 2797 2798 static void hv_free_config_window(struct hv_pcibus_device *hbus) 2799 { 2800 vmbus_free_mmio(hbus->mem_config->start, PCI_CONFIG_MMIO_LENGTH); 2801 } 2802 2803 static int hv_pci_bus_exit(struct hv_device *hdev, bool keep_devs); 2804 2805 /** 2806 * hv_pci_enter_d0() - Bring the "bus" into the D0 power state 2807 * @hdev: VMBus's tracking struct for this root PCI bus 2808 * 2809 * Return: 0 on success, -errno on failure 2810 */ 2811 static int hv_pci_enter_d0(struct hv_device *hdev) 2812 { 2813 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev); 2814 struct pci_bus_d0_entry *d0_entry; 2815 struct hv_pci_compl comp_pkt; 2816 struct pci_packet *pkt; 2817 int ret; 2818 2819 /* 2820 * Tell the host that the bus is ready to use, and moved into the 2821 * powered-on state. This includes telling the host which region 2822 * of memory-mapped I/O space has been chosen for configuration space 2823 * access. 2824 */ 2825 pkt = kzalloc(sizeof(*pkt) + sizeof(*d0_entry), GFP_KERNEL); 2826 if (!pkt) 2827 return -ENOMEM; 2828 2829 init_completion(&comp_pkt.host_event); 2830 pkt->completion_func = hv_pci_generic_compl; 2831 pkt->compl_ctxt = &comp_pkt; 2832 d0_entry = (struct pci_bus_d0_entry *)&pkt->message; 2833 d0_entry->message_type.type = PCI_BUS_D0ENTRY; 2834 d0_entry->mmio_base = hbus->mem_config->start; 2835 2836 ret = vmbus_sendpacket(hdev->channel, d0_entry, sizeof(*d0_entry), 2837 (unsigned long)pkt, VM_PKT_DATA_INBAND, 2838 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); 2839 if (!ret) 2840 ret = wait_for_response(hdev, &comp_pkt.host_event); 2841 2842 if (ret) 2843 goto exit; 2844 2845 if (comp_pkt.completion_status < 0) { 2846 dev_err(&hdev->device, 2847 "PCI Pass-through VSP failed D0 Entry with status %x\n", 2848 comp_pkt.completion_status); 2849 ret = -EPROTO; 2850 goto exit; 2851 } 2852 2853 ret = 0; 2854 2855 exit: 2856 kfree(pkt); 2857 return ret; 2858 } 2859 2860 /** 2861 * hv_pci_query_relations() - Ask host to send list of child 2862 * devices 2863 * @hdev: VMBus's tracking struct for this root PCI bus 2864 * 2865 * Return: 0 on success, -errno on failure 2866 */ 2867 static int hv_pci_query_relations(struct hv_device *hdev) 2868 { 2869 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev); 2870 struct pci_message message; 2871 struct completion comp; 2872 int ret; 2873 2874 /* Ask the host to send along the list of child devices */ 2875 init_completion(&comp); 2876 if (cmpxchg(&hbus->survey_event, NULL, &comp)) 2877 return -ENOTEMPTY; 2878 2879 memset(&message, 0, sizeof(message)); 2880 message.type = PCI_QUERY_BUS_RELATIONS; 2881 2882 ret = vmbus_sendpacket(hdev->channel, &message, sizeof(message), 2883 0, VM_PKT_DATA_INBAND, 0); 2884 if (!ret) 2885 ret = wait_for_response(hdev, &comp); 2886 2887 return ret; 2888 } 2889 2890 /** 2891 * hv_send_resources_allocated() - Report local resource choices 2892 * @hdev: VMBus's tracking struct for this root PCI bus 2893 * 2894 * The host OS is expecting to be sent a request as a message 2895 * which contains all the resources that the device will use. 2896 * The response contains those same resources, "translated" 2897 * which is to say, the values which should be used by the 2898 * hardware, when it delivers an interrupt. (MMIO resources are 2899 * used in local terms.) This is nice for Windows, and lines up 2900 * with the FDO/PDO split, which doesn't exist in Linux. Linux 2901 * is deeply expecting to scan an emulated PCI configuration 2902 * space. So this message is sent here only to drive the state 2903 * machine on the host forward. 2904 * 2905 * Return: 0 on success, -errno on failure 2906 */ 2907 static int hv_send_resources_allocated(struct hv_device *hdev) 2908 { 2909 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev); 2910 struct pci_resources_assigned *res_assigned; 2911 struct pci_resources_assigned2 *res_assigned2; 2912 struct hv_pci_compl comp_pkt; 2913 struct hv_pci_dev *hpdev; 2914 struct pci_packet *pkt; 2915 size_t size_res; 2916 int wslot; 2917 int ret; 2918 2919 size_res = (hbus->protocol_version < PCI_PROTOCOL_VERSION_1_2) 2920 ? sizeof(*res_assigned) : sizeof(*res_assigned2); 2921 2922 pkt = kmalloc(sizeof(*pkt) + size_res, GFP_KERNEL); 2923 if (!pkt) 2924 return -ENOMEM; 2925 2926 ret = 0; 2927 2928 for (wslot = 0; wslot < 256; wslot++) { 2929 hpdev = get_pcichild_wslot(hbus, wslot); 2930 if (!hpdev) 2931 continue; 2932 2933 memset(pkt, 0, sizeof(*pkt) + size_res); 2934 init_completion(&comp_pkt.host_event); 2935 pkt->completion_func = hv_pci_generic_compl; 2936 pkt->compl_ctxt = &comp_pkt; 2937 2938 if (hbus->protocol_version < PCI_PROTOCOL_VERSION_1_2) { 2939 res_assigned = 2940 (struct pci_resources_assigned *)&pkt->message; 2941 res_assigned->message_type.type = 2942 PCI_RESOURCES_ASSIGNED; 2943 res_assigned->wslot.slot = hpdev->desc.win_slot.slot; 2944 } else { 2945 res_assigned2 = 2946 (struct pci_resources_assigned2 *)&pkt->message; 2947 res_assigned2->message_type.type = 2948 PCI_RESOURCES_ASSIGNED2; 2949 res_assigned2->wslot.slot = hpdev->desc.win_slot.slot; 2950 } 2951 put_pcichild(hpdev); 2952 2953 ret = vmbus_sendpacket(hdev->channel, &pkt->message, 2954 size_res, (unsigned long)pkt, 2955 VM_PKT_DATA_INBAND, 2956 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); 2957 if (!ret) 2958 ret = wait_for_response(hdev, &comp_pkt.host_event); 2959 if (ret) 2960 break; 2961 2962 if (comp_pkt.completion_status < 0) { 2963 ret = -EPROTO; 2964 dev_err(&hdev->device, 2965 "resource allocated returned 0x%x", 2966 comp_pkt.completion_status); 2967 break; 2968 } 2969 2970 hbus->wslot_res_allocated = wslot; 2971 } 2972 2973 kfree(pkt); 2974 return ret; 2975 } 2976 2977 /** 2978 * hv_send_resources_released() - Report local resources 2979 * released 2980 * @hdev: VMBus's tracking struct for this root PCI bus 2981 * 2982 * Return: 0 on success, -errno on failure 2983 */ 2984 static int hv_send_resources_released(struct hv_device *hdev) 2985 { 2986 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev); 2987 struct pci_child_message pkt; 2988 struct hv_pci_dev *hpdev; 2989 int wslot; 2990 int ret; 2991 2992 for (wslot = hbus->wslot_res_allocated; wslot >= 0; wslot--) { 2993 hpdev = get_pcichild_wslot(hbus, wslot); 2994 if (!hpdev) 2995 continue; 2996 2997 memset(&pkt, 0, sizeof(pkt)); 2998 pkt.message_type.type = PCI_RESOURCES_RELEASED; 2999 pkt.wslot.slot = hpdev->desc.win_slot.slot; 3000 3001 put_pcichild(hpdev); 3002 3003 ret = vmbus_sendpacket(hdev->channel, &pkt, sizeof(pkt), 0, 3004 VM_PKT_DATA_INBAND, 0); 3005 if (ret) 3006 return ret; 3007 3008 hbus->wslot_res_allocated = wslot - 1; 3009 } 3010 3011 hbus->wslot_res_allocated = -1; 3012 3013 return 0; 3014 } 3015 3016 #define HVPCI_DOM_MAP_SIZE (64 * 1024) 3017 static DECLARE_BITMAP(hvpci_dom_map, HVPCI_DOM_MAP_SIZE); 3018 3019 /* 3020 * PCI domain number 0 is used by emulated devices on Gen1 VMs, so define 0 3021 * as invalid for passthrough PCI devices of this driver. 3022 */ 3023 #define HVPCI_DOM_INVALID 0 3024 3025 /** 3026 * hv_get_dom_num() - Get a valid PCI domain number 3027 * Check if the PCI domain number is in use, and return another number if 3028 * it is in use. 3029 * 3030 * @dom: Requested domain number 3031 * 3032 * return: domain number on success, HVPCI_DOM_INVALID on failure 3033 */ 3034 static u16 hv_get_dom_num(u16 dom) 3035 { 3036 unsigned int i; 3037 3038 if (test_and_set_bit(dom, hvpci_dom_map) == 0) 3039 return dom; 3040 3041 for_each_clear_bit(i, hvpci_dom_map, HVPCI_DOM_MAP_SIZE) { 3042 if (test_and_set_bit(i, hvpci_dom_map) == 0) 3043 return i; 3044 } 3045 3046 return HVPCI_DOM_INVALID; 3047 } 3048 3049 /** 3050 * hv_put_dom_num() - Mark the PCI domain number as free 3051 * @dom: Domain number to be freed 3052 */ 3053 static void hv_put_dom_num(u16 dom) 3054 { 3055 clear_bit(dom, hvpci_dom_map); 3056 } 3057 3058 /** 3059 * hv_pci_probe() - New VMBus channel probe, for a root PCI bus 3060 * @hdev: VMBus's tracking struct for this root PCI bus 3061 * @dev_id: Identifies the device itself 3062 * 3063 * Return: 0 on success, -errno on failure 3064 */ 3065 static int hv_pci_probe(struct hv_device *hdev, 3066 const struct hv_vmbus_device_id *dev_id) 3067 { 3068 struct pci_host_bridge *bridge; 3069 struct hv_pcibus_device *hbus; 3070 u16 dom_req, dom; 3071 char *name; 3072 bool enter_d0_retry = true; 3073 int ret; 3074 3075 /* 3076 * hv_pcibus_device contains the hypercall arguments for retargeting in 3077 * hv_irq_unmask(). Those must not cross a page boundary. 3078 */ 3079 BUILD_BUG_ON(sizeof(*hbus) > HV_HYP_PAGE_SIZE); 3080 3081 bridge = devm_pci_alloc_host_bridge(&hdev->device, 0); 3082 if (!bridge) 3083 return -ENOMEM; 3084 3085 /* 3086 * With the recent 59bb47985c1d ("mm, sl[aou]b: guarantee natural 3087 * alignment for kmalloc(power-of-two)"), kzalloc() is able to allocate 3088 * a 4KB buffer that is guaranteed to be 4KB-aligned. Here the size and 3089 * alignment of hbus is important because hbus's field 3090 * retarget_msi_interrupt_params must not cross a 4KB page boundary. 3091 * 3092 * Here we prefer kzalloc to get_zeroed_page(), because a buffer 3093 * allocated by the latter is not tracked and scanned by kmemleak, and 3094 * hence kmemleak reports the pointer contained in the hbus buffer 3095 * (i.e. the hpdev struct, which is created in new_pcichild_device() and 3096 * is tracked by hbus->children) as memory leak (false positive). 3097 * 3098 * If the kernel doesn't have 59bb47985c1d, get_zeroed_page() *must* be 3099 * used to allocate the hbus buffer and we can avoid the kmemleak false 3100 * positive by using kmemleak_alloc() and kmemleak_free() to ask 3101 * kmemleak to track and scan the hbus buffer. 3102 */ 3103 hbus = kzalloc(HV_HYP_PAGE_SIZE, GFP_KERNEL); 3104 if (!hbus) 3105 return -ENOMEM; 3106 3107 hbus->bridge = bridge; 3108 hbus->state = hv_pcibus_init; 3109 hbus->wslot_res_allocated = -1; 3110 3111 /* 3112 * The PCI bus "domain" is what is called "segment" in ACPI and other 3113 * specs. Pull it from the instance ID, to get something usually 3114 * unique. In rare cases of collision, we will find out another number 3115 * not in use. 3116 * 3117 * Note that, since this code only runs in a Hyper-V VM, Hyper-V 3118 * together with this guest driver can guarantee that (1) The only 3119 * domain used by Gen1 VMs for something that looks like a physical 3120 * PCI bus (which is actually emulated by the hypervisor) is domain 0. 3121 * (2) There will be no overlap between domains (after fixing possible 3122 * collisions) in the same VM. 3123 */ 3124 dom_req = hdev->dev_instance.b[5] << 8 | hdev->dev_instance.b[4]; 3125 dom = hv_get_dom_num(dom_req); 3126 3127 if (dom == HVPCI_DOM_INVALID) { 3128 dev_err(&hdev->device, 3129 "Unable to use dom# 0x%x or other numbers", dom_req); 3130 ret = -EINVAL; 3131 goto free_bus; 3132 } 3133 3134 if (dom != dom_req) 3135 dev_info(&hdev->device, 3136 "PCI dom# 0x%x has collision, using 0x%x", 3137 dom_req, dom); 3138 3139 hbus->bridge->domain_nr = dom; 3140 #ifdef CONFIG_X86 3141 hbus->sysdata.domain = dom; 3142 #endif 3143 3144 hbus->hdev = hdev; 3145 INIT_LIST_HEAD(&hbus->children); 3146 INIT_LIST_HEAD(&hbus->dr_list); 3147 spin_lock_init(&hbus->config_lock); 3148 spin_lock_init(&hbus->device_list_lock); 3149 spin_lock_init(&hbus->retarget_msi_interrupt_lock); 3150 hbus->wq = alloc_ordered_workqueue("hv_pci_%x", 0, 3151 hbus->bridge->domain_nr); 3152 if (!hbus->wq) { 3153 ret = -ENOMEM; 3154 goto free_dom; 3155 } 3156 3157 ret = vmbus_open(hdev->channel, pci_ring_size, pci_ring_size, NULL, 0, 3158 hv_pci_onchannelcallback, hbus); 3159 if (ret) 3160 goto destroy_wq; 3161 3162 hv_set_drvdata(hdev, hbus); 3163 3164 ret = hv_pci_protocol_negotiation(hdev, pci_protocol_versions, 3165 ARRAY_SIZE(pci_protocol_versions)); 3166 if (ret) 3167 goto close; 3168 3169 ret = hv_allocate_config_window(hbus); 3170 if (ret) 3171 goto close; 3172 3173 hbus->cfg_addr = ioremap(hbus->mem_config->start, 3174 PCI_CONFIG_MMIO_LENGTH); 3175 if (!hbus->cfg_addr) { 3176 dev_err(&hdev->device, 3177 "Unable to map a virtual address for config space\n"); 3178 ret = -ENOMEM; 3179 goto free_config; 3180 } 3181 3182 name = kasprintf(GFP_KERNEL, "%pUL", &hdev->dev_instance); 3183 if (!name) { 3184 ret = -ENOMEM; 3185 goto unmap; 3186 } 3187 3188 hbus->fwnode = irq_domain_alloc_named_fwnode(name); 3189 kfree(name); 3190 if (!hbus->fwnode) { 3191 ret = -ENOMEM; 3192 goto unmap; 3193 } 3194 3195 ret = hv_pcie_init_irq_domain(hbus); 3196 if (ret) 3197 goto free_fwnode; 3198 3199 retry: 3200 ret = hv_pci_query_relations(hdev); 3201 if (ret) 3202 goto free_irq_domain; 3203 3204 ret = hv_pci_enter_d0(hdev); 3205 /* 3206 * In certain case (Kdump) the pci device of interest was 3207 * not cleanly shut down and resource is still held on host 3208 * side, the host could return invalid device status. 3209 * We need to explicitly request host to release the resource 3210 * and try to enter D0 again. 3211 * Since the hv_pci_bus_exit() call releases structures 3212 * of all its child devices, we need to start the retry from 3213 * hv_pci_query_relations() call, requesting host to send 3214 * the synchronous child device relations message before this 3215 * information is needed in hv_send_resources_allocated() 3216 * call later. 3217 */ 3218 if (ret == -EPROTO && enter_d0_retry) { 3219 enter_d0_retry = false; 3220 3221 dev_err(&hdev->device, "Retrying D0 Entry\n"); 3222 3223 /* 3224 * Hv_pci_bus_exit() calls hv_send_resources_released() 3225 * to free up resources of its child devices. 3226 * In the kdump kernel we need to set the 3227 * wslot_res_allocated to 255 so it scans all child 3228 * devices to release resources allocated in the 3229 * normal kernel before panic happened. 3230 */ 3231 hbus->wslot_res_allocated = 255; 3232 ret = hv_pci_bus_exit(hdev, true); 3233 3234 if (ret == 0) 3235 goto retry; 3236 3237 dev_err(&hdev->device, 3238 "Retrying D0 failed with ret %d\n", ret); 3239 } 3240 if (ret) 3241 goto free_irq_domain; 3242 3243 ret = hv_pci_allocate_bridge_windows(hbus); 3244 if (ret) 3245 goto exit_d0; 3246 3247 ret = hv_send_resources_allocated(hdev); 3248 if (ret) 3249 goto free_windows; 3250 3251 prepopulate_bars(hbus); 3252 3253 hbus->state = hv_pcibus_probed; 3254 3255 ret = create_root_hv_pci_bus(hbus); 3256 if (ret) 3257 goto free_windows; 3258 3259 return 0; 3260 3261 free_windows: 3262 hv_pci_free_bridge_windows(hbus); 3263 exit_d0: 3264 (void) hv_pci_bus_exit(hdev, true); 3265 free_irq_domain: 3266 irq_domain_remove(hbus->irq_domain); 3267 free_fwnode: 3268 irq_domain_free_fwnode(hbus->fwnode); 3269 unmap: 3270 iounmap(hbus->cfg_addr); 3271 free_config: 3272 hv_free_config_window(hbus); 3273 close: 3274 vmbus_close(hdev->channel); 3275 destroy_wq: 3276 destroy_workqueue(hbus->wq); 3277 free_dom: 3278 hv_put_dom_num(hbus->bridge->domain_nr); 3279 free_bus: 3280 kfree(hbus); 3281 return ret; 3282 } 3283 3284 static int hv_pci_bus_exit(struct hv_device *hdev, bool keep_devs) 3285 { 3286 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev); 3287 struct { 3288 struct pci_packet teardown_packet; 3289 u8 buffer[sizeof(struct pci_message)]; 3290 } pkt; 3291 struct hv_pci_compl comp_pkt; 3292 struct hv_pci_dev *hpdev, *tmp; 3293 unsigned long flags; 3294 int ret; 3295 3296 /* 3297 * After the host sends the RESCIND_CHANNEL message, it doesn't 3298 * access the per-channel ringbuffer any longer. 3299 */ 3300 if (hdev->channel->rescind) 3301 return 0; 3302 3303 if (!keep_devs) { 3304 struct list_head removed; 3305 3306 /* Move all present children to the list on stack */ 3307 INIT_LIST_HEAD(&removed); 3308 spin_lock_irqsave(&hbus->device_list_lock, flags); 3309 list_for_each_entry_safe(hpdev, tmp, &hbus->children, list_entry) 3310 list_move_tail(&hpdev->list_entry, &removed); 3311 spin_unlock_irqrestore(&hbus->device_list_lock, flags); 3312 3313 /* Remove all children in the list */ 3314 list_for_each_entry_safe(hpdev, tmp, &removed, list_entry) { 3315 list_del(&hpdev->list_entry); 3316 if (hpdev->pci_slot) 3317 pci_destroy_slot(hpdev->pci_slot); 3318 /* For the two refs got in new_pcichild_device() */ 3319 put_pcichild(hpdev); 3320 put_pcichild(hpdev); 3321 } 3322 } 3323 3324 ret = hv_send_resources_released(hdev); 3325 if (ret) { 3326 dev_err(&hdev->device, 3327 "Couldn't send resources released packet(s)\n"); 3328 return ret; 3329 } 3330 3331 memset(&pkt.teardown_packet, 0, sizeof(pkt.teardown_packet)); 3332 init_completion(&comp_pkt.host_event); 3333 pkt.teardown_packet.completion_func = hv_pci_generic_compl; 3334 pkt.teardown_packet.compl_ctxt = &comp_pkt; 3335 pkt.teardown_packet.message[0].type = PCI_BUS_D0EXIT; 3336 3337 ret = vmbus_sendpacket(hdev->channel, &pkt.teardown_packet.message, 3338 sizeof(struct pci_message), 3339 (unsigned long)&pkt.teardown_packet, 3340 VM_PKT_DATA_INBAND, 3341 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); 3342 if (ret) 3343 return ret; 3344 3345 if (wait_for_completion_timeout(&comp_pkt.host_event, 10 * HZ) == 0) 3346 return -ETIMEDOUT; 3347 3348 return 0; 3349 } 3350 3351 /** 3352 * hv_pci_remove() - Remove routine for this VMBus channel 3353 * @hdev: VMBus's tracking struct for this root PCI bus 3354 * 3355 * Return: 0 on success, -errno on failure 3356 */ 3357 static int hv_pci_remove(struct hv_device *hdev) 3358 { 3359 struct hv_pcibus_device *hbus; 3360 int ret; 3361 3362 hbus = hv_get_drvdata(hdev); 3363 if (hbus->state == hv_pcibus_installed) { 3364 tasklet_disable(&hdev->channel->callback_event); 3365 hbus->state = hv_pcibus_removing; 3366 tasklet_enable(&hdev->channel->callback_event); 3367 destroy_workqueue(hbus->wq); 3368 hbus->wq = NULL; 3369 /* 3370 * At this point, no work is running or can be scheduled 3371 * on hbus-wq. We can't race with hv_pci_devices_present() 3372 * or hv_pci_eject_device(), it's safe to proceed. 3373 */ 3374 3375 /* Remove the bus from PCI's point of view. */ 3376 pci_lock_rescan_remove(); 3377 pci_stop_root_bus(hbus->bridge->bus); 3378 hv_pci_remove_slots(hbus); 3379 pci_remove_root_bus(hbus->bridge->bus); 3380 pci_unlock_rescan_remove(); 3381 } 3382 3383 ret = hv_pci_bus_exit(hdev, false); 3384 3385 vmbus_close(hdev->channel); 3386 3387 iounmap(hbus->cfg_addr); 3388 hv_free_config_window(hbus); 3389 hv_pci_free_bridge_windows(hbus); 3390 irq_domain_remove(hbus->irq_domain); 3391 irq_domain_free_fwnode(hbus->fwnode); 3392 3393 hv_put_dom_num(hbus->bridge->domain_nr); 3394 3395 kfree(hbus); 3396 return ret; 3397 } 3398 3399 static int hv_pci_suspend(struct hv_device *hdev) 3400 { 3401 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev); 3402 enum hv_pcibus_state old_state; 3403 int ret; 3404 3405 /* 3406 * hv_pci_suspend() must make sure there are no pending work items 3407 * before calling vmbus_close(), since it runs in a process context 3408 * as a callback in dpm_suspend(). When it starts to run, the channel 3409 * callback hv_pci_onchannelcallback(), which runs in a tasklet 3410 * context, can be still running concurrently and scheduling new work 3411 * items onto hbus->wq in hv_pci_devices_present() and 3412 * hv_pci_eject_device(), and the work item handlers can access the 3413 * vmbus channel, which can be being closed by hv_pci_suspend(), e.g. 3414 * the work item handler pci_devices_present_work() -> 3415 * new_pcichild_device() writes to the vmbus channel. 3416 * 3417 * To eliminate the race, hv_pci_suspend() disables the channel 3418 * callback tasklet, sets hbus->state to hv_pcibus_removing, and 3419 * re-enables the tasklet. This way, when hv_pci_suspend() proceeds, 3420 * it knows that no new work item can be scheduled, and then it flushes 3421 * hbus->wq and safely closes the vmbus channel. 3422 */ 3423 tasklet_disable(&hdev->channel->callback_event); 3424 3425 /* Change the hbus state to prevent new work items. */ 3426 old_state = hbus->state; 3427 if (hbus->state == hv_pcibus_installed) 3428 hbus->state = hv_pcibus_removing; 3429 3430 tasklet_enable(&hdev->channel->callback_event); 3431 3432 if (old_state != hv_pcibus_installed) 3433 return -EINVAL; 3434 3435 flush_workqueue(hbus->wq); 3436 3437 ret = hv_pci_bus_exit(hdev, true); 3438 if (ret) 3439 return ret; 3440 3441 vmbus_close(hdev->channel); 3442 3443 return 0; 3444 } 3445 3446 static int hv_pci_restore_msi_msg(struct pci_dev *pdev, void *arg) 3447 { 3448 struct msi_desc *entry; 3449 struct irq_data *irq_data; 3450 3451 for_each_pci_msi_entry(entry, pdev) { 3452 irq_data = irq_get_irq_data(entry->irq); 3453 if (WARN_ON_ONCE(!irq_data)) 3454 return -EINVAL; 3455 3456 hv_compose_msi_msg(irq_data, &entry->msg); 3457 } 3458 3459 return 0; 3460 } 3461 3462 /* 3463 * Upon resume, pci_restore_msi_state() -> ... -> __pci_write_msi_msg() 3464 * directly writes the MSI/MSI-X registers via MMIO, but since Hyper-V 3465 * doesn't trap and emulate the MMIO accesses, here hv_compose_msi_msg() 3466 * must be used to ask Hyper-V to re-create the IOMMU Interrupt Remapping 3467 * Table entries. 3468 */ 3469 static void hv_pci_restore_msi_state(struct hv_pcibus_device *hbus) 3470 { 3471 pci_walk_bus(hbus->bridge->bus, hv_pci_restore_msi_msg, NULL); 3472 } 3473 3474 static int hv_pci_resume(struct hv_device *hdev) 3475 { 3476 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev); 3477 enum pci_protocol_version_t version[1]; 3478 int ret; 3479 3480 hbus->state = hv_pcibus_init; 3481 3482 ret = vmbus_open(hdev->channel, pci_ring_size, pci_ring_size, NULL, 0, 3483 hv_pci_onchannelcallback, hbus); 3484 if (ret) 3485 return ret; 3486 3487 /* Only use the version that was in use before hibernation. */ 3488 version[0] = hbus->protocol_version; 3489 ret = hv_pci_protocol_negotiation(hdev, version, 1); 3490 if (ret) 3491 goto out; 3492 3493 ret = hv_pci_query_relations(hdev); 3494 if (ret) 3495 goto out; 3496 3497 ret = hv_pci_enter_d0(hdev); 3498 if (ret) 3499 goto out; 3500 3501 ret = hv_send_resources_allocated(hdev); 3502 if (ret) 3503 goto out; 3504 3505 prepopulate_bars(hbus); 3506 3507 hv_pci_restore_msi_state(hbus); 3508 3509 hbus->state = hv_pcibus_installed; 3510 return 0; 3511 out: 3512 vmbus_close(hdev->channel); 3513 return ret; 3514 } 3515 3516 static const struct hv_vmbus_device_id hv_pci_id_table[] = { 3517 /* PCI Pass-through Class ID */ 3518 /* 44C4F61D-4444-4400-9D52-802E27EDE19F */ 3519 { HV_PCIE_GUID, }, 3520 { }, 3521 }; 3522 3523 MODULE_DEVICE_TABLE(vmbus, hv_pci_id_table); 3524 3525 static struct hv_driver hv_pci_drv = { 3526 .name = "hv_pci", 3527 .id_table = hv_pci_id_table, 3528 .probe = hv_pci_probe, 3529 .remove = hv_pci_remove, 3530 .suspend = hv_pci_suspend, 3531 .resume = hv_pci_resume, 3532 }; 3533 3534 static void __exit exit_hv_pci_drv(void) 3535 { 3536 vmbus_driver_unregister(&hv_pci_drv); 3537 3538 hvpci_block_ops.read_block = NULL; 3539 hvpci_block_ops.write_block = NULL; 3540 hvpci_block_ops.reg_blk_invalidate = NULL; 3541 } 3542 3543 static int __init init_hv_pci_drv(void) 3544 { 3545 if (!hv_is_hyperv_initialized()) 3546 return -ENODEV; 3547 3548 /* Set the invalid domain number's bit, so it will not be used */ 3549 set_bit(HVPCI_DOM_INVALID, hvpci_dom_map); 3550 3551 /* Initialize PCI block r/w interface */ 3552 hvpci_block_ops.read_block = hv_read_config_block; 3553 hvpci_block_ops.write_block = hv_write_config_block; 3554 hvpci_block_ops.reg_blk_invalidate = hv_register_block_invalidate; 3555 3556 return vmbus_driver_register(&hv_pci_drv); 3557 } 3558 3559 module_init(init_hv_pci_drv); 3560 module_exit(exit_hv_pci_drv); 3561 3562 MODULE_DESCRIPTION("Hyper-V PCI"); 3563 MODULE_LICENSE("GPL v2"); 3564