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