Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) Microsoft Corporation. |
| 3 | * |
| 4 | * Author: |
| 5 | * Jake Oshins <jakeo@microsoft.com> |
| 6 | * |
| 7 | * This driver acts as a paravirtual front-end for PCI Express root buses. |
| 8 | * When a PCI Express function (either an entire device or an SR-IOV |
| 9 | * Virtual Function) is being passed through to the VM, this driver exposes |
| 10 | * a new bus to the guest VM. This is modeled as a root PCI bus because |
| 11 | * no bridges are being exposed to the VM. In fact, with a "Generation 2" |
| 12 | * VM within Hyper-V, there may seem to be no PCI bus at all in the VM |
| 13 | * until a device as been exposed using this driver. |
| 14 | * |
| 15 | * Each root PCI bus has its own PCI domain, which is called "Segment" in |
| 16 | * the PCI Firmware Specifications. Thus while each device passed through |
| 17 | * to the VM using this front-end will appear at "device 0", the domain will |
| 18 | * be unique. Typically, each bus will have one PCI function on it, though |
| 19 | * this driver does support more than one. |
| 20 | * |
| 21 | * In order to map the interrupts from the device through to the guest VM, |
| 22 | * this driver also implements an IRQ Domain, which handles interrupts (either |
| 23 | * MSI or MSI-X) associated with the functions on the bus. As interrupts are |
| 24 | * set up, torn down, or reaffined, this driver communicates with the |
| 25 | * underlying hypervisor to adjust the mappings in the I/O MMU so that each |
| 26 | * interrupt will be delivered to the correct virtual processor at the right |
| 27 | * vector. This driver does not support level-triggered (line-based) |
| 28 | * interrupts, and will report that the Interrupt Line register in the |
| 29 | * function's configuration space is zero. |
| 30 | * |
| 31 | * The rest of this driver mostly maps PCI concepts onto underlying Hyper-V |
| 32 | * facilities. For instance, the configuration space of a function exposed |
| 33 | * by Hyper-V is mapped into a single page of memory space, and the |
| 34 | * read and write handlers for config space must be aware of this mechanism. |
| 35 | * Similarly, device setup and teardown involves messages sent to and from |
| 36 | * the PCI back-end driver in Hyper-V. |
| 37 | * |
| 38 | * This program is free software; you can redistribute it and/or modify it |
| 39 | * under the terms of the GNU General Public License version 2 as published |
| 40 | * by the Free Software Foundation. |
| 41 | * |
| 42 | * This program is distributed in the hope that it will be useful, but |
| 43 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 44 | * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or |
| 45 | * NON INFRINGEMENT. See the GNU General Public License for more |
| 46 | * details. |
| 47 | * |
| 48 | */ |
| 49 | |
| 50 | #include <linux/kernel.h> |
| 51 | #include <linux/module.h> |
| 52 | #include <linux/pci.h> |
| 53 | #include <linux/semaphore.h> |
| 54 | #include <linux/irqdomain.h> |
| 55 | #include <asm/irqdomain.h> |
| 56 | #include <asm/apic.h> |
| 57 | #include <linux/msi.h> |
| 58 | #include <linux/hyperv.h> |
| 59 | #include <asm/mshyperv.h> |
| 60 | |
| 61 | /* |
| 62 | * Protocol versions. The low word is the minor version, the high word the |
| 63 | * major version. |
| 64 | */ |
| 65 | |
| 66 | #define PCI_MAKE_VERSION(major, minor) ((u32)(((major) << 16) | (major))) |
| 67 | #define PCI_MAJOR_VERSION(version) ((u32)(version) >> 16) |
| 68 | #define PCI_MINOR_VERSION(version) ((u32)(version) & 0xff) |
| 69 | |
| 70 | enum { |
| 71 | PCI_PROTOCOL_VERSION_1_1 = PCI_MAKE_VERSION(1, 1), |
| 72 | PCI_PROTOCOL_VERSION_CURRENT = PCI_PROTOCOL_VERSION_1_1 |
| 73 | }; |
| 74 | |
| 75 | #define PCI_CONFIG_MMIO_LENGTH 0x2000 |
| 76 | #define CFG_PAGE_OFFSET 0x1000 |
| 77 | #define CFG_PAGE_SIZE (PCI_CONFIG_MMIO_LENGTH - CFG_PAGE_OFFSET) |
| 78 | |
| 79 | #define MAX_SUPPORTED_MSI_MESSAGES 0x400 |
| 80 | |
| 81 | /* |
| 82 | * Message Types |
| 83 | */ |
| 84 | |
| 85 | enum pci_message_type { |
| 86 | /* |
| 87 | * Version 1.1 |
| 88 | */ |
| 89 | PCI_MESSAGE_BASE = 0x42490000, |
| 90 | PCI_BUS_RELATIONS = PCI_MESSAGE_BASE + 0, |
| 91 | PCI_QUERY_BUS_RELATIONS = PCI_MESSAGE_BASE + 1, |
| 92 | PCI_POWER_STATE_CHANGE = PCI_MESSAGE_BASE + 4, |
| 93 | PCI_QUERY_RESOURCE_REQUIREMENTS = PCI_MESSAGE_BASE + 5, |
| 94 | PCI_QUERY_RESOURCE_RESOURCES = PCI_MESSAGE_BASE + 6, |
| 95 | PCI_BUS_D0ENTRY = PCI_MESSAGE_BASE + 7, |
| 96 | PCI_BUS_D0EXIT = PCI_MESSAGE_BASE + 8, |
| 97 | PCI_READ_BLOCK = PCI_MESSAGE_BASE + 9, |
| 98 | PCI_WRITE_BLOCK = PCI_MESSAGE_BASE + 0xA, |
| 99 | PCI_EJECT = PCI_MESSAGE_BASE + 0xB, |
| 100 | PCI_QUERY_STOP = PCI_MESSAGE_BASE + 0xC, |
| 101 | PCI_REENABLE = PCI_MESSAGE_BASE + 0xD, |
| 102 | PCI_QUERY_STOP_FAILED = PCI_MESSAGE_BASE + 0xE, |
| 103 | PCI_EJECTION_COMPLETE = PCI_MESSAGE_BASE + 0xF, |
| 104 | PCI_RESOURCES_ASSIGNED = PCI_MESSAGE_BASE + 0x10, |
| 105 | PCI_RESOURCES_RELEASED = PCI_MESSAGE_BASE + 0x11, |
| 106 | PCI_INVALIDATE_BLOCK = PCI_MESSAGE_BASE + 0x12, |
| 107 | PCI_QUERY_PROTOCOL_VERSION = PCI_MESSAGE_BASE + 0x13, |
| 108 | PCI_CREATE_INTERRUPT_MESSAGE = PCI_MESSAGE_BASE + 0x14, |
| 109 | PCI_DELETE_INTERRUPT_MESSAGE = PCI_MESSAGE_BASE + 0x15, |
| 110 | PCI_MESSAGE_MAXIMUM |
| 111 | }; |
| 112 | |
| 113 | /* |
| 114 | * Structures defining the virtual PCI Express protocol. |
| 115 | */ |
| 116 | |
| 117 | union pci_version { |
| 118 | struct { |
| 119 | u16 minor_version; |
| 120 | u16 major_version; |
| 121 | } parts; |
| 122 | u32 version; |
| 123 | } __packed; |
| 124 | |
| 125 | /* |
| 126 | * Function numbers are 8-bits wide on Express, as interpreted through ARI, |
| 127 | * which is all this driver does. This representation is the one used in |
| 128 | * Windows, which is what is expected when sending this back and forth with |
| 129 | * the Hyper-V parent partition. |
| 130 | */ |
| 131 | union win_slot_encoding { |
| 132 | struct { |
Dexuan Cui | 60e2e2f | 2017-02-10 15:18:46 -0600 | [diff] [blame] | 133 | u32 dev:5; |
| 134 | u32 func:3; |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 135 | u32 reserved:24; |
| 136 | } bits; |
| 137 | u32 slot; |
| 138 | } __packed; |
| 139 | |
| 140 | /* |
| 141 | * Pretty much as defined in the PCI Specifications. |
| 142 | */ |
| 143 | struct pci_function_description { |
| 144 | u16 v_id; /* vendor ID */ |
| 145 | u16 d_id; /* device ID */ |
| 146 | u8 rev; |
| 147 | u8 prog_intf; |
| 148 | u8 subclass; |
| 149 | u8 base_class; |
| 150 | u32 subsystem_id; |
| 151 | union win_slot_encoding win_slot; |
| 152 | u32 ser; /* serial number */ |
| 153 | } __packed; |
| 154 | |
| 155 | /** |
| 156 | * struct hv_msi_desc |
| 157 | * @vector: IDT entry |
| 158 | * @delivery_mode: As defined in Intel's Programmer's |
| 159 | * Reference Manual, Volume 3, Chapter 8. |
| 160 | * @vector_count: Number of contiguous entries in the |
| 161 | * Interrupt Descriptor Table that are |
| 162 | * occupied by this Message-Signaled |
| 163 | * Interrupt. For "MSI", as first defined |
| 164 | * in PCI 2.2, this can be between 1 and |
| 165 | * 32. For "MSI-X," as first defined in PCI |
| 166 | * 3.0, this must be 1, as each MSI-X table |
| 167 | * entry would have its own descriptor. |
| 168 | * @reserved: Empty space |
| 169 | * @cpu_mask: All the target virtual processors. |
| 170 | */ |
| 171 | struct hv_msi_desc { |
| 172 | u8 vector; |
| 173 | u8 delivery_mode; |
| 174 | u16 vector_count; |
| 175 | u32 reserved; |
| 176 | u64 cpu_mask; |
| 177 | } __packed; |
| 178 | |
| 179 | /** |
| 180 | * struct tran_int_desc |
| 181 | * @reserved: unused, padding |
| 182 | * @vector_count: same as in hv_msi_desc |
| 183 | * @data: This is the "data payload" value that is |
| 184 | * written by the device when it generates |
| 185 | * a message-signaled interrupt, either MSI |
| 186 | * or MSI-X. |
| 187 | * @address: This is the address to which the data |
| 188 | * payload is written on interrupt |
| 189 | * generation. |
| 190 | */ |
| 191 | struct tran_int_desc { |
| 192 | u16 reserved; |
| 193 | u16 vector_count; |
| 194 | u32 data; |
| 195 | u64 address; |
| 196 | } __packed; |
| 197 | |
| 198 | /* |
| 199 | * A generic message format for virtual PCI. |
| 200 | * Specific message formats are defined later in the file. |
| 201 | */ |
| 202 | |
| 203 | struct pci_message { |
Dexuan Cui | 0c6045d | 2016-08-23 04:45:51 +0000 | [diff] [blame] | 204 | u32 type; |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 205 | } __packed; |
| 206 | |
| 207 | struct pci_child_message { |
Dexuan Cui | 0c6045d | 2016-08-23 04:45:51 +0000 | [diff] [blame] | 208 | struct pci_message message_type; |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 209 | union win_slot_encoding wslot; |
| 210 | } __packed; |
| 211 | |
| 212 | struct pci_incoming_message { |
| 213 | struct vmpacket_descriptor hdr; |
| 214 | struct pci_message message_type; |
| 215 | } __packed; |
| 216 | |
| 217 | struct pci_response { |
| 218 | struct vmpacket_descriptor hdr; |
| 219 | s32 status; /* negative values are failures */ |
| 220 | } __packed; |
| 221 | |
| 222 | struct pci_packet { |
| 223 | void (*completion_func)(void *context, struct pci_response *resp, |
| 224 | int resp_packet_size); |
| 225 | void *compl_ctxt; |
Dexuan Cui | 0c6045d | 2016-08-23 04:45:51 +0000 | [diff] [blame] | 226 | |
| 227 | struct pci_message message[0]; |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 228 | }; |
| 229 | |
| 230 | /* |
| 231 | * Specific message types supporting the PCI protocol. |
| 232 | */ |
| 233 | |
| 234 | /* |
| 235 | * Version negotiation message. Sent from the guest to the host. |
| 236 | * The guest is free to try different versions until the host |
| 237 | * accepts the version. |
| 238 | * |
| 239 | * pci_version: The protocol version requested. |
| 240 | * is_last_attempt: If TRUE, this is the last version guest will request. |
| 241 | * reservedz: Reserved field, set to zero. |
| 242 | */ |
| 243 | |
| 244 | struct pci_version_request { |
| 245 | struct pci_message message_type; |
| 246 | enum pci_message_type protocol_version; |
| 247 | } __packed; |
| 248 | |
| 249 | /* |
| 250 | * Bus D0 Entry. This is sent from the guest to the host when the virtual |
| 251 | * bus (PCI Express port) is ready for action. |
| 252 | */ |
| 253 | |
| 254 | struct pci_bus_d0_entry { |
| 255 | struct pci_message message_type; |
| 256 | u32 reserved; |
| 257 | u64 mmio_base; |
| 258 | } __packed; |
| 259 | |
| 260 | struct pci_bus_relations { |
| 261 | struct pci_incoming_message incoming; |
| 262 | u32 device_count; |
Dexuan Cui | 7d0f8ee | 2016-08-23 04:46:39 +0000 | [diff] [blame] | 263 | struct pci_function_description func[0]; |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 264 | } __packed; |
| 265 | |
| 266 | struct pci_q_res_req_response { |
| 267 | struct vmpacket_descriptor hdr; |
| 268 | s32 status; /* negative values are failures */ |
| 269 | u32 probed_bar[6]; |
| 270 | } __packed; |
| 271 | |
| 272 | struct pci_set_power { |
| 273 | struct pci_message message_type; |
| 274 | union win_slot_encoding wslot; |
| 275 | u32 power_state; /* In Windows terms */ |
| 276 | u32 reserved; |
| 277 | } __packed; |
| 278 | |
| 279 | struct pci_set_power_response { |
| 280 | struct vmpacket_descriptor hdr; |
| 281 | s32 status; /* negative values are failures */ |
| 282 | union win_slot_encoding wslot; |
| 283 | u32 resultant_state; /* In Windows terms */ |
| 284 | u32 reserved; |
| 285 | } __packed; |
| 286 | |
| 287 | struct pci_resources_assigned { |
| 288 | struct pci_message message_type; |
| 289 | union win_slot_encoding wslot; |
| 290 | u8 memory_range[0x14][6]; /* not used here */ |
| 291 | u32 msi_descriptors; |
| 292 | u32 reserved[4]; |
| 293 | } __packed; |
| 294 | |
| 295 | struct pci_create_interrupt { |
| 296 | struct pci_message message_type; |
| 297 | union win_slot_encoding wslot; |
| 298 | struct hv_msi_desc int_desc; |
| 299 | } __packed; |
| 300 | |
| 301 | struct pci_create_int_response { |
| 302 | struct pci_response response; |
| 303 | u32 reserved; |
| 304 | struct tran_int_desc int_desc; |
| 305 | } __packed; |
| 306 | |
| 307 | struct pci_delete_interrupt { |
| 308 | struct pci_message message_type; |
| 309 | union win_slot_encoding wslot; |
| 310 | struct tran_int_desc int_desc; |
| 311 | } __packed; |
| 312 | |
| 313 | struct pci_dev_incoming { |
| 314 | struct pci_incoming_message incoming; |
| 315 | union win_slot_encoding wslot; |
| 316 | } __packed; |
| 317 | |
| 318 | struct pci_eject_response { |
Dexuan Cui | 0c6045d | 2016-08-23 04:45:51 +0000 | [diff] [blame] | 319 | struct pci_message message_type; |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 320 | union win_slot_encoding wslot; |
| 321 | u32 status; |
| 322 | } __packed; |
| 323 | |
| 324 | static int pci_ring_size = (4 * PAGE_SIZE); |
| 325 | |
| 326 | /* |
| 327 | * Definitions or interrupt steering hypercall. |
| 328 | */ |
| 329 | #define HV_PARTITION_ID_SELF ((u64)-1) |
| 330 | #define HVCALL_RETARGET_INTERRUPT 0x7e |
| 331 | |
| 332 | struct retarget_msi_interrupt { |
| 333 | u64 partition_id; /* use "self" */ |
| 334 | u64 device_id; |
| 335 | u32 source; /* 1 for MSI(-X) */ |
| 336 | u32 reserved1; |
| 337 | u32 address; |
| 338 | u32 data; |
| 339 | u64 reserved2; |
| 340 | u32 vector; |
| 341 | u32 flags; |
| 342 | u64 vp_mask; |
| 343 | } __packed; |
| 344 | |
| 345 | /* |
| 346 | * Driver specific state. |
| 347 | */ |
| 348 | |
| 349 | enum hv_pcibus_state { |
| 350 | hv_pcibus_init = 0, |
| 351 | hv_pcibus_probed, |
| 352 | hv_pcibus_installed, |
Long Li | d3a78d8 | 2017-03-23 14:58:10 -0700 | [diff] [blame^] | 353 | hv_pcibus_removed, |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 354 | hv_pcibus_maximum |
| 355 | }; |
| 356 | |
| 357 | struct hv_pcibus_device { |
| 358 | struct pci_sysdata sysdata; |
| 359 | enum hv_pcibus_state state; |
| 360 | atomic_t remove_lock; |
| 361 | struct hv_device *hdev; |
| 362 | resource_size_t low_mmio_space; |
| 363 | resource_size_t high_mmio_space; |
| 364 | struct resource *mem_config; |
| 365 | struct resource *low_mmio_res; |
| 366 | struct resource *high_mmio_res; |
| 367 | struct completion *survey_event; |
| 368 | struct completion remove_event; |
| 369 | struct pci_bus *pci_bus; |
| 370 | spinlock_t config_lock; /* Avoid two threads writing index page */ |
| 371 | spinlock_t device_list_lock; /* Protect lists below */ |
| 372 | void __iomem *cfg_addr; |
| 373 | |
| 374 | struct semaphore enum_sem; |
| 375 | struct list_head resources_for_children; |
| 376 | |
| 377 | struct list_head children; |
| 378 | struct list_head dr_list; |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 379 | |
| 380 | struct msi_domain_info msi_info; |
| 381 | struct msi_controller msi_chip; |
| 382 | struct irq_domain *irq_domain; |
Long Li | 0de8ce3 | 2016-11-08 14:04:38 -0800 | [diff] [blame] | 383 | struct retarget_msi_interrupt retarget_msi_interrupt_params; |
| 384 | spinlock_t retarget_msi_interrupt_lock; |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 385 | }; |
| 386 | |
| 387 | /* |
| 388 | * Tracks "Device Relations" messages from the host, which must be both |
| 389 | * processed in order and deferred so that they don't run in the context |
| 390 | * of the incoming packet callback. |
| 391 | */ |
| 392 | struct hv_dr_work { |
| 393 | struct work_struct wrk; |
| 394 | struct hv_pcibus_device *bus; |
| 395 | }; |
| 396 | |
| 397 | struct hv_dr_state { |
| 398 | struct list_head list_entry; |
| 399 | u32 device_count; |
Dexuan Cui | 7d0f8ee | 2016-08-23 04:46:39 +0000 | [diff] [blame] | 400 | struct pci_function_description func[0]; |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 401 | }; |
| 402 | |
| 403 | enum hv_pcichild_state { |
| 404 | hv_pcichild_init = 0, |
| 405 | hv_pcichild_requirements, |
| 406 | hv_pcichild_resourced, |
| 407 | hv_pcichild_ejecting, |
| 408 | hv_pcichild_maximum |
| 409 | }; |
| 410 | |
| 411 | enum hv_pcidev_ref_reason { |
| 412 | hv_pcidev_ref_invalid = 0, |
| 413 | hv_pcidev_ref_initial, |
| 414 | hv_pcidev_ref_by_slot, |
| 415 | hv_pcidev_ref_packet, |
| 416 | hv_pcidev_ref_pnp, |
| 417 | hv_pcidev_ref_childlist, |
| 418 | hv_pcidev_irqdata, |
| 419 | hv_pcidev_ref_max |
| 420 | }; |
| 421 | |
| 422 | struct hv_pci_dev { |
| 423 | /* List protected by pci_rescan_remove_lock */ |
| 424 | struct list_head list_entry; |
| 425 | atomic_t refs; |
| 426 | enum hv_pcichild_state state; |
| 427 | struct pci_function_description desc; |
| 428 | bool reported_missing; |
| 429 | struct hv_pcibus_device *hbus; |
| 430 | struct work_struct wrk; |
| 431 | |
| 432 | /* |
| 433 | * What would be observed if one wrote 0xFFFFFFFF to a BAR and then |
| 434 | * read it back, for each of the BAR offsets within config space. |
| 435 | */ |
| 436 | u32 probed_bar[6]; |
| 437 | }; |
| 438 | |
| 439 | struct hv_pci_compl { |
| 440 | struct completion host_event; |
| 441 | s32 completion_status; |
| 442 | }; |
| 443 | |
| 444 | /** |
| 445 | * hv_pci_generic_compl() - Invoked for a completion packet |
| 446 | * @context: Set up by the sender of the packet. |
| 447 | * @resp: The response packet |
| 448 | * @resp_packet_size: Size in bytes of the packet |
| 449 | * |
| 450 | * This function is used to trigger an event and report status |
| 451 | * for any message for which the completion packet contains a |
| 452 | * status and nothing else. |
| 453 | */ |
Dexuan Cui | a5b45b7 | 2016-08-23 04:49:22 +0000 | [diff] [blame] | 454 | static void hv_pci_generic_compl(void *context, struct pci_response *resp, |
| 455 | int resp_packet_size) |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 456 | { |
| 457 | struct hv_pci_compl *comp_pkt = context; |
| 458 | |
| 459 | if (resp_packet_size >= offsetofend(struct pci_response, status)) |
| 460 | comp_pkt->completion_status = resp->status; |
Dexuan Cui | a5b45b7 | 2016-08-23 04:49:22 +0000 | [diff] [blame] | 461 | else |
| 462 | comp_pkt->completion_status = -1; |
| 463 | |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 464 | complete(&comp_pkt->host_event); |
| 465 | } |
| 466 | |
| 467 | static struct hv_pci_dev *get_pcichild_wslot(struct hv_pcibus_device *hbus, |
| 468 | u32 wslot); |
| 469 | static void get_pcichild(struct hv_pci_dev *hv_pcidev, |
| 470 | enum hv_pcidev_ref_reason reason); |
| 471 | static void put_pcichild(struct hv_pci_dev *hv_pcidev, |
| 472 | enum hv_pcidev_ref_reason reason); |
| 473 | |
| 474 | static void get_hvpcibus(struct hv_pcibus_device *hv_pcibus); |
| 475 | static void put_hvpcibus(struct hv_pcibus_device *hv_pcibus); |
| 476 | |
| 477 | /** |
| 478 | * devfn_to_wslot() - Convert from Linux PCI slot to Windows |
| 479 | * @devfn: The Linux representation of PCI slot |
| 480 | * |
| 481 | * Windows uses a slightly different representation of PCI slot. |
| 482 | * |
| 483 | * Return: The Windows representation |
| 484 | */ |
| 485 | static u32 devfn_to_wslot(int devfn) |
| 486 | { |
| 487 | union win_slot_encoding wslot; |
| 488 | |
| 489 | wslot.slot = 0; |
Dexuan Cui | 60e2e2f | 2017-02-10 15:18:46 -0600 | [diff] [blame] | 490 | wslot.bits.dev = PCI_SLOT(devfn); |
| 491 | wslot.bits.func = PCI_FUNC(devfn); |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 492 | |
| 493 | return wslot.slot; |
| 494 | } |
| 495 | |
| 496 | /** |
| 497 | * wslot_to_devfn() - Convert from Windows PCI slot to Linux |
| 498 | * @wslot: The Windows representation of PCI slot |
| 499 | * |
| 500 | * Windows uses a slightly different representation of PCI slot. |
| 501 | * |
| 502 | * Return: The Linux representation |
| 503 | */ |
| 504 | static int wslot_to_devfn(u32 wslot) |
| 505 | { |
| 506 | union win_slot_encoding slot_no; |
| 507 | |
| 508 | slot_no.slot = wslot; |
Dexuan Cui | 60e2e2f | 2017-02-10 15:18:46 -0600 | [diff] [blame] | 509 | return PCI_DEVFN(slot_no.bits.dev, slot_no.bits.func); |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | /* |
| 513 | * PCI Configuration Space for these root PCI buses is implemented as a pair |
| 514 | * of pages in memory-mapped I/O space. Writing to the first page chooses |
| 515 | * the PCI function being written or read. Once the first page has been |
| 516 | * written to, the following page maps in the entire configuration space of |
| 517 | * the function. |
| 518 | */ |
| 519 | |
| 520 | /** |
| 521 | * _hv_pcifront_read_config() - Internal PCI config read |
| 522 | * @hpdev: The PCI driver's representation of the device |
| 523 | * @where: Offset within config space |
| 524 | * @size: Size of the transfer |
| 525 | * @val: Pointer to the buffer receiving the data |
| 526 | */ |
| 527 | static void _hv_pcifront_read_config(struct hv_pci_dev *hpdev, int where, |
| 528 | int size, u32 *val) |
| 529 | { |
| 530 | unsigned long flags; |
| 531 | void __iomem *addr = hpdev->hbus->cfg_addr + CFG_PAGE_OFFSET + where; |
| 532 | |
| 533 | /* |
| 534 | * If the attempt is to read the IDs or the ROM BAR, simulate that. |
| 535 | */ |
| 536 | if (where + size <= PCI_COMMAND) { |
| 537 | memcpy(val, ((u8 *)&hpdev->desc.v_id) + where, size); |
| 538 | } else if (where >= PCI_CLASS_REVISION && where + size <= |
| 539 | PCI_CACHE_LINE_SIZE) { |
| 540 | memcpy(val, ((u8 *)&hpdev->desc.rev) + where - |
| 541 | PCI_CLASS_REVISION, size); |
| 542 | } else if (where >= PCI_SUBSYSTEM_VENDOR_ID && where + size <= |
| 543 | PCI_ROM_ADDRESS) { |
| 544 | memcpy(val, (u8 *)&hpdev->desc.subsystem_id + where - |
| 545 | PCI_SUBSYSTEM_VENDOR_ID, size); |
| 546 | } else if (where >= PCI_ROM_ADDRESS && where + size <= |
| 547 | PCI_CAPABILITY_LIST) { |
| 548 | /* ROM BARs are unimplemented */ |
| 549 | *val = 0; |
| 550 | } else if (where >= PCI_INTERRUPT_LINE && where + size <= |
| 551 | PCI_INTERRUPT_PIN) { |
| 552 | /* |
| 553 | * Interrupt Line and Interrupt PIN are hard-wired to zero |
| 554 | * because this front-end only supports message-signaled |
| 555 | * interrupts. |
| 556 | */ |
| 557 | *val = 0; |
| 558 | } else if (where + size <= CFG_PAGE_SIZE) { |
| 559 | spin_lock_irqsave(&hpdev->hbus->config_lock, flags); |
| 560 | /* Choose the function to be read. (See comment above) */ |
| 561 | writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr); |
Vitaly Kuznetsov | bdd7444 | 2016-05-03 14:22:00 +0200 | [diff] [blame] | 562 | /* Make sure the function was chosen before we start reading. */ |
| 563 | mb(); |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 564 | /* Read from that function's config space. */ |
| 565 | switch (size) { |
| 566 | case 1: |
| 567 | *val = readb(addr); |
| 568 | break; |
| 569 | case 2: |
| 570 | *val = readw(addr); |
| 571 | break; |
| 572 | default: |
| 573 | *val = readl(addr); |
| 574 | break; |
| 575 | } |
Vitaly Kuznetsov | bdd7444 | 2016-05-03 14:22:00 +0200 | [diff] [blame] | 576 | /* |
| 577 | * Make sure the write was done before we release the spinlock |
| 578 | * allowing consecutive reads/writes. |
| 579 | */ |
| 580 | mb(); |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 581 | spin_unlock_irqrestore(&hpdev->hbus->config_lock, flags); |
| 582 | } else { |
| 583 | dev_err(&hpdev->hbus->hdev->device, |
| 584 | "Attempt to read beyond a function's config space.\n"); |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | /** |
| 589 | * _hv_pcifront_write_config() - Internal PCI config write |
| 590 | * @hpdev: The PCI driver's representation of the device |
| 591 | * @where: Offset within config space |
| 592 | * @size: Size of the transfer |
| 593 | * @val: The data being transferred |
| 594 | */ |
| 595 | static void _hv_pcifront_write_config(struct hv_pci_dev *hpdev, int where, |
| 596 | int size, u32 val) |
| 597 | { |
| 598 | unsigned long flags; |
| 599 | void __iomem *addr = hpdev->hbus->cfg_addr + CFG_PAGE_OFFSET + where; |
| 600 | |
| 601 | if (where >= PCI_SUBSYSTEM_VENDOR_ID && |
| 602 | where + size <= PCI_CAPABILITY_LIST) { |
| 603 | /* SSIDs and ROM BARs are read-only */ |
| 604 | } else if (where >= PCI_COMMAND && where + size <= CFG_PAGE_SIZE) { |
| 605 | spin_lock_irqsave(&hpdev->hbus->config_lock, flags); |
| 606 | /* Choose the function to be written. (See comment above) */ |
| 607 | writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr); |
Vitaly Kuznetsov | bdd7444 | 2016-05-03 14:22:00 +0200 | [diff] [blame] | 608 | /* Make sure the function was chosen before we start writing. */ |
| 609 | wmb(); |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 610 | /* Write to that function's config space. */ |
| 611 | switch (size) { |
| 612 | case 1: |
| 613 | writeb(val, addr); |
| 614 | break; |
| 615 | case 2: |
| 616 | writew(val, addr); |
| 617 | break; |
| 618 | default: |
| 619 | writel(val, addr); |
| 620 | break; |
| 621 | } |
Vitaly Kuznetsov | bdd7444 | 2016-05-03 14:22:00 +0200 | [diff] [blame] | 622 | /* |
| 623 | * Make sure the write was done before we release the spinlock |
| 624 | * allowing consecutive reads/writes. |
| 625 | */ |
| 626 | mb(); |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 627 | spin_unlock_irqrestore(&hpdev->hbus->config_lock, flags); |
| 628 | } else { |
| 629 | dev_err(&hpdev->hbus->hdev->device, |
| 630 | "Attempt to write beyond a function's config space.\n"); |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | /** |
| 635 | * hv_pcifront_read_config() - Read configuration space |
| 636 | * @bus: PCI Bus structure |
| 637 | * @devfn: Device/function |
| 638 | * @where: Offset from base |
| 639 | * @size: Byte/word/dword |
| 640 | * @val: Value to be read |
| 641 | * |
| 642 | * Return: PCIBIOS_SUCCESSFUL on success |
| 643 | * PCIBIOS_DEVICE_NOT_FOUND on failure |
| 644 | */ |
| 645 | static int hv_pcifront_read_config(struct pci_bus *bus, unsigned int devfn, |
| 646 | int where, int size, u32 *val) |
| 647 | { |
| 648 | struct hv_pcibus_device *hbus = |
| 649 | container_of(bus->sysdata, struct hv_pcibus_device, sysdata); |
| 650 | struct hv_pci_dev *hpdev; |
| 651 | |
| 652 | hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(devfn)); |
| 653 | if (!hpdev) |
| 654 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 655 | |
| 656 | _hv_pcifront_read_config(hpdev, where, size, val); |
| 657 | |
| 658 | put_pcichild(hpdev, hv_pcidev_ref_by_slot); |
| 659 | return PCIBIOS_SUCCESSFUL; |
| 660 | } |
| 661 | |
| 662 | /** |
| 663 | * hv_pcifront_write_config() - Write configuration space |
| 664 | * @bus: PCI Bus structure |
| 665 | * @devfn: Device/function |
| 666 | * @where: Offset from base |
| 667 | * @size: Byte/word/dword |
| 668 | * @val: Value to be written to device |
| 669 | * |
| 670 | * Return: PCIBIOS_SUCCESSFUL on success |
| 671 | * PCIBIOS_DEVICE_NOT_FOUND on failure |
| 672 | */ |
| 673 | static int hv_pcifront_write_config(struct pci_bus *bus, unsigned int devfn, |
| 674 | int where, int size, u32 val) |
| 675 | { |
| 676 | struct hv_pcibus_device *hbus = |
| 677 | container_of(bus->sysdata, struct hv_pcibus_device, sysdata); |
| 678 | struct hv_pci_dev *hpdev; |
| 679 | |
| 680 | hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(devfn)); |
| 681 | if (!hpdev) |
| 682 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 683 | |
| 684 | _hv_pcifront_write_config(hpdev, where, size, val); |
| 685 | |
| 686 | put_pcichild(hpdev, hv_pcidev_ref_by_slot); |
| 687 | return PCIBIOS_SUCCESSFUL; |
| 688 | } |
| 689 | |
| 690 | /* PCIe operations */ |
| 691 | static struct pci_ops hv_pcifront_ops = { |
| 692 | .read = hv_pcifront_read_config, |
| 693 | .write = hv_pcifront_write_config, |
| 694 | }; |
| 695 | |
| 696 | /* Interrupt management hooks */ |
| 697 | static void hv_int_desc_free(struct hv_pci_dev *hpdev, |
| 698 | struct tran_int_desc *int_desc) |
| 699 | { |
| 700 | struct pci_delete_interrupt *int_pkt; |
| 701 | struct { |
| 702 | struct pci_packet pkt; |
Dexuan Cui | 0c6045d | 2016-08-23 04:45:51 +0000 | [diff] [blame] | 703 | u8 buffer[sizeof(struct pci_delete_interrupt)]; |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 704 | } ctxt; |
| 705 | |
| 706 | memset(&ctxt, 0, sizeof(ctxt)); |
| 707 | int_pkt = (struct pci_delete_interrupt *)&ctxt.pkt.message; |
Dexuan Cui | 0c6045d | 2016-08-23 04:45:51 +0000 | [diff] [blame] | 708 | int_pkt->message_type.type = |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 709 | PCI_DELETE_INTERRUPT_MESSAGE; |
| 710 | int_pkt->wslot.slot = hpdev->desc.win_slot.slot; |
| 711 | int_pkt->int_desc = *int_desc; |
| 712 | vmbus_sendpacket(hpdev->hbus->hdev->channel, int_pkt, sizeof(*int_pkt), |
| 713 | (unsigned long)&ctxt.pkt, VM_PKT_DATA_INBAND, 0); |
| 714 | kfree(int_desc); |
| 715 | } |
| 716 | |
| 717 | /** |
| 718 | * hv_msi_free() - Free the MSI. |
| 719 | * @domain: The interrupt domain pointer |
| 720 | * @info: Extra MSI-related context |
| 721 | * @irq: Identifies the IRQ. |
| 722 | * |
| 723 | * The Hyper-V parent partition and hypervisor are tracking the |
| 724 | * messages that are in use, keeping the interrupt redirection |
| 725 | * table up to date. This callback sends a message that frees |
| 726 | * the IRT entry and related tracking nonsense. |
| 727 | */ |
| 728 | static void hv_msi_free(struct irq_domain *domain, struct msi_domain_info *info, |
| 729 | unsigned int irq) |
| 730 | { |
| 731 | struct hv_pcibus_device *hbus; |
| 732 | struct hv_pci_dev *hpdev; |
| 733 | struct pci_dev *pdev; |
| 734 | struct tran_int_desc *int_desc; |
| 735 | struct irq_data *irq_data = irq_domain_get_irq_data(domain, irq); |
| 736 | struct msi_desc *msi = irq_data_get_msi_desc(irq_data); |
| 737 | |
| 738 | pdev = msi_desc_to_pci_dev(msi); |
| 739 | hbus = info->data; |
Cathy Avery | 0c6e617 | 2016-07-12 11:31:24 -0400 | [diff] [blame] | 740 | int_desc = irq_data_get_irq_chip_data(irq_data); |
| 741 | if (!int_desc) |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 742 | return; |
| 743 | |
Cathy Avery | 0c6e617 | 2016-07-12 11:31:24 -0400 | [diff] [blame] | 744 | irq_data->chip_data = NULL; |
| 745 | hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn)); |
| 746 | if (!hpdev) { |
| 747 | kfree(int_desc); |
| 748 | return; |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 749 | } |
| 750 | |
Cathy Avery | 0c6e617 | 2016-07-12 11:31:24 -0400 | [diff] [blame] | 751 | hv_int_desc_free(hpdev, int_desc); |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 752 | put_pcichild(hpdev, hv_pcidev_ref_by_slot); |
| 753 | } |
| 754 | |
| 755 | static int hv_set_affinity(struct irq_data *data, const struct cpumask *dest, |
| 756 | bool force) |
| 757 | { |
| 758 | struct irq_data *parent = data->parent_data; |
| 759 | |
| 760 | return parent->chip->irq_set_affinity(parent, dest, force); |
| 761 | } |
| 762 | |
Tobias Klauser | 542ccf4 | 2016-10-31 12:04:09 +0100 | [diff] [blame] | 763 | static void hv_irq_mask(struct irq_data *data) |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 764 | { |
| 765 | pci_msi_mask_irq(data); |
| 766 | } |
| 767 | |
| 768 | /** |
| 769 | * hv_irq_unmask() - "Unmask" the IRQ by setting its current |
| 770 | * affinity. |
| 771 | * @data: Describes the IRQ |
| 772 | * |
| 773 | * Build new a destination for the MSI and make a hypercall to |
| 774 | * update the Interrupt Redirection Table. "Device Logical ID" |
| 775 | * is built out of this PCI bus's instance GUID and the function |
| 776 | * number of the device. |
| 777 | */ |
Tobias Klauser | 542ccf4 | 2016-10-31 12:04:09 +0100 | [diff] [blame] | 778 | static void hv_irq_unmask(struct irq_data *data) |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 779 | { |
| 780 | struct msi_desc *msi_desc = irq_data_get_msi_desc(data); |
| 781 | struct irq_cfg *cfg = irqd_cfg(data); |
Long Li | 0de8ce3 | 2016-11-08 14:04:38 -0800 | [diff] [blame] | 782 | struct retarget_msi_interrupt *params; |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 783 | struct hv_pcibus_device *hbus; |
| 784 | struct cpumask *dest; |
| 785 | struct pci_bus *pbus; |
| 786 | struct pci_dev *pdev; |
| 787 | int cpu; |
Long Li | 0de8ce3 | 2016-11-08 14:04:38 -0800 | [diff] [blame] | 788 | unsigned long flags; |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 789 | |
| 790 | dest = irq_data_get_affinity_mask(data); |
| 791 | pdev = msi_desc_to_pci_dev(msi_desc); |
| 792 | pbus = pdev->bus; |
| 793 | hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata); |
| 794 | |
Long Li | 0de8ce3 | 2016-11-08 14:04:38 -0800 | [diff] [blame] | 795 | spin_lock_irqsave(&hbus->retarget_msi_interrupt_lock, flags); |
| 796 | |
| 797 | params = &hbus->retarget_msi_interrupt_params; |
| 798 | memset(params, 0, sizeof(*params)); |
| 799 | params->partition_id = HV_PARTITION_ID_SELF; |
| 800 | params->source = 1; /* MSI(-X) */ |
| 801 | params->address = msi_desc->msg.address_lo; |
| 802 | params->data = msi_desc->msg.data; |
| 803 | params->device_id = (hbus->hdev->dev_instance.b[5] << 24) | |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 804 | (hbus->hdev->dev_instance.b[4] << 16) | |
| 805 | (hbus->hdev->dev_instance.b[7] << 8) | |
| 806 | (hbus->hdev->dev_instance.b[6] & 0xf8) | |
| 807 | PCI_FUNC(pdev->devfn); |
Long Li | 0de8ce3 | 2016-11-08 14:04:38 -0800 | [diff] [blame] | 808 | params->vector = cfg->vector; |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 809 | |
| 810 | for_each_cpu_and(cpu, dest, cpu_online_mask) |
Long Li | 0de8ce3 | 2016-11-08 14:04:38 -0800 | [diff] [blame] | 811 | params->vp_mask |= (1ULL << vmbus_cpu_number_to_vp_number(cpu)); |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 812 | |
Long Li | 0de8ce3 | 2016-11-08 14:04:38 -0800 | [diff] [blame] | 813 | hv_do_hypercall(HVCALL_RETARGET_INTERRUPT, params, NULL); |
| 814 | |
| 815 | spin_unlock_irqrestore(&hbus->retarget_msi_interrupt_lock, flags); |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 816 | |
| 817 | pci_msi_unmask_irq(data); |
| 818 | } |
| 819 | |
| 820 | struct compose_comp_ctxt { |
| 821 | struct hv_pci_compl comp_pkt; |
| 822 | struct tran_int_desc int_desc; |
| 823 | }; |
| 824 | |
| 825 | static void hv_pci_compose_compl(void *context, struct pci_response *resp, |
| 826 | int resp_packet_size) |
| 827 | { |
| 828 | struct compose_comp_ctxt *comp_pkt = context; |
| 829 | struct pci_create_int_response *int_resp = |
| 830 | (struct pci_create_int_response *)resp; |
| 831 | |
| 832 | comp_pkt->comp_pkt.completion_status = resp->status; |
| 833 | comp_pkt->int_desc = int_resp->int_desc; |
| 834 | complete(&comp_pkt->comp_pkt.host_event); |
| 835 | } |
| 836 | |
| 837 | /** |
| 838 | * hv_compose_msi_msg() - Supplies a valid MSI address/data |
| 839 | * @data: Everything about this MSI |
| 840 | * @msg: Buffer that is filled in by this function |
| 841 | * |
| 842 | * This function unpacks the IRQ looking for target CPU set, IDT |
| 843 | * vector and mode and sends a message to the parent partition |
| 844 | * asking for a mapping for that tuple in this partition. The |
| 845 | * response supplies a data value and address to which that data |
| 846 | * should be written to trigger that interrupt. |
| 847 | */ |
| 848 | static void hv_compose_msi_msg(struct irq_data *data, struct msi_msg *msg) |
| 849 | { |
| 850 | struct irq_cfg *cfg = irqd_cfg(data); |
| 851 | struct hv_pcibus_device *hbus; |
| 852 | struct hv_pci_dev *hpdev; |
| 853 | struct pci_bus *pbus; |
| 854 | struct pci_dev *pdev; |
| 855 | struct pci_create_interrupt *int_pkt; |
| 856 | struct compose_comp_ctxt comp; |
| 857 | struct tran_int_desc *int_desc; |
| 858 | struct cpumask *affinity; |
| 859 | struct { |
| 860 | struct pci_packet pkt; |
Dexuan Cui | 0c6045d | 2016-08-23 04:45:51 +0000 | [diff] [blame] | 861 | u8 buffer[sizeof(struct pci_create_interrupt)]; |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 862 | } ctxt; |
| 863 | int cpu; |
| 864 | int ret; |
| 865 | |
| 866 | pdev = msi_desc_to_pci_dev(irq_data_get_msi_desc(data)); |
| 867 | pbus = pdev->bus; |
| 868 | hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata); |
| 869 | hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn)); |
| 870 | if (!hpdev) |
| 871 | goto return_null_message; |
| 872 | |
| 873 | /* Free any previous message that might have already been composed. */ |
| 874 | if (data->chip_data) { |
| 875 | int_desc = data->chip_data; |
| 876 | data->chip_data = NULL; |
| 877 | hv_int_desc_free(hpdev, int_desc); |
| 878 | } |
| 879 | |
| 880 | int_desc = kzalloc(sizeof(*int_desc), GFP_KERNEL); |
| 881 | if (!int_desc) |
| 882 | goto drop_reference; |
| 883 | |
| 884 | memset(&ctxt, 0, sizeof(ctxt)); |
| 885 | init_completion(&comp.comp_pkt.host_event); |
| 886 | ctxt.pkt.completion_func = hv_pci_compose_compl; |
| 887 | ctxt.pkt.compl_ctxt = ∁ |
| 888 | int_pkt = (struct pci_create_interrupt *)&ctxt.pkt.message; |
Dexuan Cui | 0c6045d | 2016-08-23 04:45:51 +0000 | [diff] [blame] | 889 | int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE; |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 890 | int_pkt->wslot.slot = hpdev->desc.win_slot.slot; |
| 891 | int_pkt->int_desc.vector = cfg->vector; |
| 892 | int_pkt->int_desc.vector_count = 1; |
| 893 | int_pkt->int_desc.delivery_mode = |
| 894 | (apic->irq_delivery_mode == dest_LowestPrio) ? 1 : 0; |
| 895 | |
| 896 | /* |
| 897 | * This bit doesn't have to work on machines with more than 64 |
| 898 | * processors because Hyper-V only supports 64 in a guest. |
| 899 | */ |
| 900 | affinity = irq_data_get_affinity_mask(data); |
| 901 | for_each_cpu_and(cpu, affinity, cpu_online_mask) { |
| 902 | int_pkt->int_desc.cpu_mask |= |
| 903 | (1ULL << vmbus_cpu_number_to_vp_number(cpu)); |
| 904 | } |
| 905 | |
| 906 | ret = vmbus_sendpacket(hpdev->hbus->hdev->channel, int_pkt, |
| 907 | sizeof(*int_pkt), (unsigned long)&ctxt.pkt, |
| 908 | VM_PKT_DATA_INBAND, |
| 909 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); |
Dexuan Cui | 665e224 | 2016-08-23 04:48:11 +0000 | [diff] [blame] | 910 | if (ret) |
| 911 | goto free_int_desc; |
| 912 | |
| 913 | wait_for_completion(&comp.comp_pkt.host_event); |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 914 | |
| 915 | if (comp.comp_pkt.completion_status < 0) { |
| 916 | dev_err(&hbus->hdev->device, |
| 917 | "Request for interrupt failed: 0x%x", |
| 918 | comp.comp_pkt.completion_status); |
| 919 | goto free_int_desc; |
| 920 | } |
| 921 | |
| 922 | /* |
| 923 | * Record the assignment so that this can be unwound later. Using |
| 924 | * irq_set_chip_data() here would be appropriate, but the lock it takes |
| 925 | * is already held. |
| 926 | */ |
| 927 | *int_desc = comp.int_desc; |
| 928 | data->chip_data = int_desc; |
| 929 | |
| 930 | /* Pass up the result. */ |
| 931 | msg->address_hi = comp.int_desc.address >> 32; |
| 932 | msg->address_lo = comp.int_desc.address & 0xffffffff; |
| 933 | msg->data = comp.int_desc.data; |
| 934 | |
| 935 | put_pcichild(hpdev, hv_pcidev_ref_by_slot); |
| 936 | return; |
| 937 | |
| 938 | free_int_desc: |
| 939 | kfree(int_desc); |
| 940 | drop_reference: |
| 941 | put_pcichild(hpdev, hv_pcidev_ref_by_slot); |
| 942 | return_null_message: |
| 943 | msg->address_hi = 0; |
| 944 | msg->address_lo = 0; |
| 945 | msg->data = 0; |
| 946 | } |
| 947 | |
| 948 | /* HW Interrupt Chip Descriptor */ |
| 949 | static struct irq_chip hv_msi_irq_chip = { |
| 950 | .name = "Hyper-V PCIe MSI", |
| 951 | .irq_compose_msi_msg = hv_compose_msi_msg, |
| 952 | .irq_set_affinity = hv_set_affinity, |
| 953 | .irq_ack = irq_chip_ack_parent, |
| 954 | .irq_mask = hv_irq_mask, |
| 955 | .irq_unmask = hv_irq_unmask, |
| 956 | }; |
| 957 | |
| 958 | static irq_hw_number_t hv_msi_domain_ops_get_hwirq(struct msi_domain_info *info, |
| 959 | msi_alloc_info_t *arg) |
| 960 | { |
| 961 | return arg->msi_hwirq; |
| 962 | } |
| 963 | |
| 964 | static struct msi_domain_ops hv_msi_ops = { |
| 965 | .get_hwirq = hv_msi_domain_ops_get_hwirq, |
| 966 | .msi_prepare = pci_msi_prepare, |
| 967 | .set_desc = pci_msi_set_desc, |
| 968 | .msi_free = hv_msi_free, |
| 969 | }; |
| 970 | |
| 971 | /** |
| 972 | * hv_pcie_init_irq_domain() - Initialize IRQ domain |
| 973 | * @hbus: The root PCI bus |
| 974 | * |
| 975 | * This function creates an IRQ domain which will be used for |
| 976 | * interrupts from devices that have been passed through. These |
| 977 | * devices only support MSI and MSI-X, not line-based interrupts |
| 978 | * or simulations of line-based interrupts through PCIe's |
| 979 | * fabric-layer messages. Because interrupts are remapped, we |
| 980 | * can support multi-message MSI here. |
| 981 | * |
| 982 | * Return: '0' on success and error value on failure |
| 983 | */ |
| 984 | static int hv_pcie_init_irq_domain(struct hv_pcibus_device *hbus) |
| 985 | { |
| 986 | hbus->msi_info.chip = &hv_msi_irq_chip; |
| 987 | hbus->msi_info.ops = &hv_msi_ops; |
| 988 | hbus->msi_info.flags = (MSI_FLAG_USE_DEF_DOM_OPS | |
| 989 | MSI_FLAG_USE_DEF_CHIP_OPS | MSI_FLAG_MULTI_PCI_MSI | |
| 990 | MSI_FLAG_PCI_MSIX); |
| 991 | hbus->msi_info.handler = handle_edge_irq; |
| 992 | hbus->msi_info.handler_name = "edge"; |
| 993 | hbus->msi_info.data = hbus; |
| 994 | hbus->irq_domain = pci_msi_create_irq_domain(hbus->sysdata.fwnode, |
| 995 | &hbus->msi_info, |
| 996 | x86_vector_domain); |
| 997 | if (!hbus->irq_domain) { |
| 998 | dev_err(&hbus->hdev->device, |
| 999 | "Failed to build an MSI IRQ domain\n"); |
| 1000 | return -ENODEV; |
| 1001 | } |
| 1002 | |
| 1003 | return 0; |
| 1004 | } |
| 1005 | |
| 1006 | /** |
| 1007 | * get_bar_size() - Get the address space consumed by a BAR |
| 1008 | * @bar_val: Value that a BAR returned after -1 was written |
| 1009 | * to it. |
| 1010 | * |
| 1011 | * This function returns the size of the BAR, rounded up to 1 |
| 1012 | * page. It has to be rounded up because the hypervisor's page |
| 1013 | * table entry that maps the BAR into the VM can't specify an |
| 1014 | * offset within a page. The invariant is that the hypervisor |
| 1015 | * must place any BARs of smaller than page length at the |
| 1016 | * beginning of a page. |
| 1017 | * |
| 1018 | * Return: Size in bytes of the consumed MMIO space. |
| 1019 | */ |
| 1020 | static u64 get_bar_size(u64 bar_val) |
| 1021 | { |
| 1022 | return round_up((1 + ~(bar_val & PCI_BASE_ADDRESS_MEM_MASK)), |
| 1023 | PAGE_SIZE); |
| 1024 | } |
| 1025 | |
| 1026 | /** |
| 1027 | * survey_child_resources() - Total all MMIO requirements |
| 1028 | * @hbus: Root PCI bus, as understood by this driver |
| 1029 | */ |
| 1030 | static void survey_child_resources(struct hv_pcibus_device *hbus) |
| 1031 | { |
| 1032 | struct list_head *iter; |
| 1033 | struct hv_pci_dev *hpdev; |
| 1034 | resource_size_t bar_size = 0; |
| 1035 | unsigned long flags; |
| 1036 | struct completion *event; |
| 1037 | u64 bar_val; |
| 1038 | int i; |
| 1039 | |
| 1040 | /* If nobody is waiting on the answer, don't compute it. */ |
| 1041 | event = xchg(&hbus->survey_event, NULL); |
| 1042 | if (!event) |
| 1043 | return; |
| 1044 | |
| 1045 | /* If the answer has already been computed, go with it. */ |
| 1046 | if (hbus->low_mmio_space || hbus->high_mmio_space) { |
| 1047 | complete(event); |
| 1048 | return; |
| 1049 | } |
| 1050 | |
| 1051 | spin_lock_irqsave(&hbus->device_list_lock, flags); |
| 1052 | |
| 1053 | /* |
| 1054 | * Due to an interesting quirk of the PCI spec, all memory regions |
| 1055 | * for a child device are a power of 2 in size and aligned in memory, |
| 1056 | * so it's sufficient to just add them up without tracking alignment. |
| 1057 | */ |
| 1058 | list_for_each(iter, &hbus->children) { |
| 1059 | hpdev = container_of(iter, struct hv_pci_dev, list_entry); |
| 1060 | for (i = 0; i < 6; i++) { |
| 1061 | if (hpdev->probed_bar[i] & PCI_BASE_ADDRESS_SPACE_IO) |
| 1062 | dev_err(&hbus->hdev->device, |
| 1063 | "There's an I/O BAR in this list!\n"); |
| 1064 | |
| 1065 | if (hpdev->probed_bar[i] != 0) { |
| 1066 | /* |
| 1067 | * A probed BAR has all the upper bits set that |
| 1068 | * can be changed. |
| 1069 | */ |
| 1070 | |
| 1071 | bar_val = hpdev->probed_bar[i]; |
| 1072 | if (bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64) |
| 1073 | bar_val |= |
| 1074 | ((u64)hpdev->probed_bar[++i] << 32); |
| 1075 | else |
| 1076 | bar_val |= 0xffffffff00000000ULL; |
| 1077 | |
| 1078 | bar_size = get_bar_size(bar_val); |
| 1079 | |
| 1080 | if (bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64) |
| 1081 | hbus->high_mmio_space += bar_size; |
| 1082 | else |
| 1083 | hbus->low_mmio_space += bar_size; |
| 1084 | } |
| 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | spin_unlock_irqrestore(&hbus->device_list_lock, flags); |
| 1089 | complete(event); |
| 1090 | } |
| 1091 | |
| 1092 | /** |
| 1093 | * prepopulate_bars() - Fill in BARs with defaults |
| 1094 | * @hbus: Root PCI bus, as understood by this driver |
| 1095 | * |
| 1096 | * The core PCI driver code seems much, much happier if the BARs |
| 1097 | * for a device have values upon first scan. So fill them in. |
| 1098 | * The algorithm below works down from large sizes to small, |
| 1099 | * attempting to pack the assignments optimally. The assumption, |
| 1100 | * enforced in other parts of the code, is that the beginning of |
| 1101 | * the memory-mapped I/O space will be aligned on the largest |
| 1102 | * BAR size. |
| 1103 | */ |
| 1104 | static void prepopulate_bars(struct hv_pcibus_device *hbus) |
| 1105 | { |
| 1106 | resource_size_t high_size = 0; |
| 1107 | resource_size_t low_size = 0; |
| 1108 | resource_size_t high_base = 0; |
| 1109 | resource_size_t low_base = 0; |
| 1110 | resource_size_t bar_size; |
| 1111 | struct hv_pci_dev *hpdev; |
| 1112 | struct list_head *iter; |
| 1113 | unsigned long flags; |
| 1114 | u64 bar_val; |
| 1115 | u32 command; |
| 1116 | bool high; |
| 1117 | int i; |
| 1118 | |
| 1119 | if (hbus->low_mmio_space) { |
| 1120 | low_size = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space)); |
| 1121 | low_base = hbus->low_mmio_res->start; |
| 1122 | } |
| 1123 | |
| 1124 | if (hbus->high_mmio_space) { |
| 1125 | high_size = 1ULL << |
| 1126 | (63 - __builtin_clzll(hbus->high_mmio_space)); |
| 1127 | high_base = hbus->high_mmio_res->start; |
| 1128 | } |
| 1129 | |
| 1130 | spin_lock_irqsave(&hbus->device_list_lock, flags); |
| 1131 | |
| 1132 | /* Pick addresses for the BARs. */ |
| 1133 | do { |
| 1134 | list_for_each(iter, &hbus->children) { |
| 1135 | hpdev = container_of(iter, struct hv_pci_dev, |
| 1136 | list_entry); |
| 1137 | for (i = 0; i < 6; i++) { |
| 1138 | bar_val = hpdev->probed_bar[i]; |
| 1139 | if (bar_val == 0) |
| 1140 | continue; |
| 1141 | high = bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64; |
| 1142 | if (high) { |
| 1143 | bar_val |= |
| 1144 | ((u64)hpdev->probed_bar[i + 1] |
| 1145 | << 32); |
| 1146 | } else { |
| 1147 | bar_val |= 0xffffffffULL << 32; |
| 1148 | } |
| 1149 | bar_size = get_bar_size(bar_val); |
| 1150 | if (high) { |
| 1151 | if (high_size != bar_size) { |
| 1152 | i++; |
| 1153 | continue; |
| 1154 | } |
| 1155 | _hv_pcifront_write_config(hpdev, |
| 1156 | PCI_BASE_ADDRESS_0 + (4 * i), |
| 1157 | 4, |
| 1158 | (u32)(high_base & 0xffffff00)); |
| 1159 | i++; |
| 1160 | _hv_pcifront_write_config(hpdev, |
| 1161 | PCI_BASE_ADDRESS_0 + (4 * i), |
| 1162 | 4, (u32)(high_base >> 32)); |
| 1163 | high_base += bar_size; |
| 1164 | } else { |
| 1165 | if (low_size != bar_size) |
| 1166 | continue; |
| 1167 | _hv_pcifront_write_config(hpdev, |
| 1168 | PCI_BASE_ADDRESS_0 + (4 * i), |
| 1169 | 4, |
| 1170 | (u32)(low_base & 0xffffff00)); |
| 1171 | low_base += bar_size; |
| 1172 | } |
| 1173 | } |
| 1174 | if (high_size <= 1 && low_size <= 1) { |
| 1175 | /* Set the memory enable bit. */ |
| 1176 | _hv_pcifront_read_config(hpdev, PCI_COMMAND, 2, |
| 1177 | &command); |
| 1178 | command |= PCI_COMMAND_MEMORY; |
| 1179 | _hv_pcifront_write_config(hpdev, PCI_COMMAND, 2, |
| 1180 | command); |
| 1181 | break; |
| 1182 | } |
| 1183 | } |
| 1184 | |
| 1185 | high_size >>= 1; |
| 1186 | low_size >>= 1; |
| 1187 | } while (high_size || low_size); |
| 1188 | |
| 1189 | spin_unlock_irqrestore(&hbus->device_list_lock, flags); |
| 1190 | } |
| 1191 | |
| 1192 | /** |
| 1193 | * create_root_hv_pci_bus() - Expose a new root PCI bus |
| 1194 | * @hbus: Root PCI bus, as understood by this driver |
| 1195 | * |
| 1196 | * Return: 0 on success, -errno on failure |
| 1197 | */ |
| 1198 | static int create_root_hv_pci_bus(struct hv_pcibus_device *hbus) |
| 1199 | { |
| 1200 | /* Register the device */ |
| 1201 | hbus->pci_bus = pci_create_root_bus(&hbus->hdev->device, |
| 1202 | 0, /* bus number is always zero */ |
| 1203 | &hv_pcifront_ops, |
| 1204 | &hbus->sysdata, |
| 1205 | &hbus->resources_for_children); |
| 1206 | if (!hbus->pci_bus) |
| 1207 | return -ENODEV; |
| 1208 | |
| 1209 | hbus->pci_bus->msi = &hbus->msi_chip; |
| 1210 | hbus->pci_bus->msi->dev = &hbus->hdev->device; |
| 1211 | |
| 1212 | pci_scan_child_bus(hbus->pci_bus); |
| 1213 | pci_bus_assign_resources(hbus->pci_bus); |
| 1214 | pci_bus_add_devices(hbus->pci_bus); |
| 1215 | hbus->state = hv_pcibus_installed; |
| 1216 | return 0; |
| 1217 | } |
| 1218 | |
| 1219 | struct q_res_req_compl { |
| 1220 | struct completion host_event; |
| 1221 | struct hv_pci_dev *hpdev; |
| 1222 | }; |
| 1223 | |
| 1224 | /** |
| 1225 | * q_resource_requirements() - Query Resource Requirements |
| 1226 | * @context: The completion context. |
| 1227 | * @resp: The response that came from the host. |
| 1228 | * @resp_packet_size: The size in bytes of resp. |
| 1229 | * |
| 1230 | * This function is invoked on completion of a Query Resource |
| 1231 | * Requirements packet. |
| 1232 | */ |
| 1233 | static void q_resource_requirements(void *context, struct pci_response *resp, |
| 1234 | int resp_packet_size) |
| 1235 | { |
| 1236 | struct q_res_req_compl *completion = context; |
| 1237 | struct pci_q_res_req_response *q_res_req = |
| 1238 | (struct pci_q_res_req_response *)resp; |
| 1239 | int i; |
| 1240 | |
| 1241 | if (resp->status < 0) { |
| 1242 | dev_err(&completion->hpdev->hbus->hdev->device, |
| 1243 | "query resource requirements failed: %x\n", |
| 1244 | resp->status); |
| 1245 | } else { |
| 1246 | for (i = 0; i < 6; i++) { |
| 1247 | completion->hpdev->probed_bar[i] = |
| 1248 | q_res_req->probed_bar[i]; |
| 1249 | } |
| 1250 | } |
| 1251 | |
| 1252 | complete(&completion->host_event); |
| 1253 | } |
| 1254 | |
| 1255 | static void get_pcichild(struct hv_pci_dev *hpdev, |
| 1256 | enum hv_pcidev_ref_reason reason) |
| 1257 | { |
| 1258 | atomic_inc(&hpdev->refs); |
| 1259 | } |
| 1260 | |
| 1261 | static void put_pcichild(struct hv_pci_dev *hpdev, |
| 1262 | enum hv_pcidev_ref_reason reason) |
| 1263 | { |
| 1264 | if (atomic_dec_and_test(&hpdev->refs)) |
| 1265 | kfree(hpdev); |
| 1266 | } |
| 1267 | |
| 1268 | /** |
| 1269 | * new_pcichild_device() - Create a new child device |
| 1270 | * @hbus: The internal struct tracking this root PCI bus. |
| 1271 | * @desc: The information supplied so far from the host |
| 1272 | * about the device. |
| 1273 | * |
| 1274 | * This function creates the tracking structure for a new child |
| 1275 | * device and kicks off the process of figuring out what it is. |
| 1276 | * |
| 1277 | * Return: Pointer to the new tracking struct |
| 1278 | */ |
| 1279 | static struct hv_pci_dev *new_pcichild_device(struct hv_pcibus_device *hbus, |
| 1280 | struct pci_function_description *desc) |
| 1281 | { |
| 1282 | struct hv_pci_dev *hpdev; |
| 1283 | struct pci_child_message *res_req; |
| 1284 | struct q_res_req_compl comp_pkt; |
Dexuan Cui | 8286e96 | 2016-11-10 07:17:48 +0000 | [diff] [blame] | 1285 | struct { |
| 1286 | struct pci_packet init_packet; |
| 1287 | u8 buffer[sizeof(struct pci_child_message)]; |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 1288 | } pkt; |
| 1289 | unsigned long flags; |
| 1290 | int ret; |
| 1291 | |
| 1292 | hpdev = kzalloc(sizeof(*hpdev), GFP_ATOMIC); |
| 1293 | if (!hpdev) |
| 1294 | return NULL; |
| 1295 | |
| 1296 | hpdev->hbus = hbus; |
| 1297 | |
| 1298 | memset(&pkt, 0, sizeof(pkt)); |
| 1299 | init_completion(&comp_pkt.host_event); |
| 1300 | comp_pkt.hpdev = hpdev; |
| 1301 | pkt.init_packet.compl_ctxt = &comp_pkt; |
| 1302 | pkt.init_packet.completion_func = q_resource_requirements; |
| 1303 | res_req = (struct pci_child_message *)&pkt.init_packet.message; |
Dexuan Cui | 0c6045d | 2016-08-23 04:45:51 +0000 | [diff] [blame] | 1304 | res_req->message_type.type = PCI_QUERY_RESOURCE_REQUIREMENTS; |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 1305 | res_req->wslot.slot = desc->win_slot.slot; |
| 1306 | |
| 1307 | ret = vmbus_sendpacket(hbus->hdev->channel, res_req, |
| 1308 | sizeof(struct pci_child_message), |
| 1309 | (unsigned long)&pkt.init_packet, |
| 1310 | VM_PKT_DATA_INBAND, |
| 1311 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); |
| 1312 | if (ret) |
| 1313 | goto error; |
| 1314 | |
| 1315 | wait_for_completion(&comp_pkt.host_event); |
| 1316 | |
| 1317 | hpdev->desc = *desc; |
| 1318 | get_pcichild(hpdev, hv_pcidev_ref_initial); |
| 1319 | get_pcichild(hpdev, hv_pcidev_ref_childlist); |
| 1320 | spin_lock_irqsave(&hbus->device_list_lock, flags); |
Haiyang Zhang | 4a9b093 | 2017-02-13 18:10:11 +0000 | [diff] [blame] | 1321 | |
| 1322 | /* |
| 1323 | * When a device is being added to the bus, we set the PCI domain |
| 1324 | * number to be the device serial number, which is non-zero and |
| 1325 | * unique on the same VM. The serial numbers start with 1, and |
| 1326 | * increase by 1 for each device. So device names including this |
| 1327 | * can have shorter names than based on the bus instance UUID. |
| 1328 | * Only the first device serial number is used for domain, so the |
| 1329 | * domain number will not change after the first device is added. |
| 1330 | */ |
| 1331 | if (list_empty(&hbus->children)) |
| 1332 | hbus->sysdata.domain = desc->ser; |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 1333 | list_add_tail(&hpdev->list_entry, &hbus->children); |
| 1334 | spin_unlock_irqrestore(&hbus->device_list_lock, flags); |
| 1335 | return hpdev; |
| 1336 | |
| 1337 | error: |
| 1338 | kfree(hpdev); |
| 1339 | return NULL; |
| 1340 | } |
| 1341 | |
| 1342 | /** |
| 1343 | * get_pcichild_wslot() - Find device from slot |
| 1344 | * @hbus: Root PCI bus, as understood by this driver |
| 1345 | * @wslot: Location on the bus |
| 1346 | * |
| 1347 | * This function looks up a PCI device and returns the internal |
| 1348 | * representation of it. It acquires a reference on it, so that |
| 1349 | * the device won't be deleted while somebody is using it. The |
| 1350 | * caller is responsible for calling put_pcichild() to release |
| 1351 | * this reference. |
| 1352 | * |
| 1353 | * Return: Internal representation of a PCI device |
| 1354 | */ |
| 1355 | static struct hv_pci_dev *get_pcichild_wslot(struct hv_pcibus_device *hbus, |
| 1356 | u32 wslot) |
| 1357 | { |
| 1358 | unsigned long flags; |
| 1359 | struct hv_pci_dev *iter, *hpdev = NULL; |
| 1360 | |
| 1361 | spin_lock_irqsave(&hbus->device_list_lock, flags); |
| 1362 | list_for_each_entry(iter, &hbus->children, list_entry) { |
| 1363 | if (iter->desc.win_slot.slot == wslot) { |
| 1364 | hpdev = iter; |
| 1365 | get_pcichild(hpdev, hv_pcidev_ref_by_slot); |
| 1366 | break; |
| 1367 | } |
| 1368 | } |
| 1369 | spin_unlock_irqrestore(&hbus->device_list_lock, flags); |
| 1370 | |
| 1371 | return hpdev; |
| 1372 | } |
| 1373 | |
| 1374 | /** |
| 1375 | * pci_devices_present_work() - Handle new list of child devices |
| 1376 | * @work: Work struct embedded in struct hv_dr_work |
| 1377 | * |
| 1378 | * "Bus Relations" is the Windows term for "children of this |
| 1379 | * bus." The terminology is preserved here for people trying to |
| 1380 | * debug the interaction between Hyper-V and Linux. This |
| 1381 | * function is called when the parent partition reports a list |
| 1382 | * of functions that should be observed under this PCI Express |
| 1383 | * port (bus). |
| 1384 | * |
| 1385 | * This function updates the list, and must tolerate being |
| 1386 | * called multiple times with the same information. The typical |
| 1387 | * number of child devices is one, with very atypical cases |
| 1388 | * involving three or four, so the algorithms used here can be |
| 1389 | * simple and inefficient. |
| 1390 | * |
| 1391 | * It must also treat the omission of a previously observed device as |
| 1392 | * notification that the device no longer exists. |
| 1393 | * |
| 1394 | * Note that this function is a work item, and it may not be |
| 1395 | * invoked in the order that it was queued. Back to back |
| 1396 | * updates of the list of present devices may involve queuing |
| 1397 | * multiple work items, and this one may run before ones that |
| 1398 | * were sent later. As such, this function only does something |
| 1399 | * if is the last one in the queue. |
| 1400 | */ |
| 1401 | static void pci_devices_present_work(struct work_struct *work) |
| 1402 | { |
| 1403 | u32 child_no; |
| 1404 | bool found; |
| 1405 | struct list_head *iter; |
| 1406 | struct pci_function_description *new_desc; |
| 1407 | struct hv_pci_dev *hpdev; |
| 1408 | struct hv_pcibus_device *hbus; |
| 1409 | struct list_head removed; |
| 1410 | struct hv_dr_work *dr_wrk; |
| 1411 | struct hv_dr_state *dr = NULL; |
| 1412 | unsigned long flags; |
| 1413 | |
| 1414 | dr_wrk = container_of(work, struct hv_dr_work, wrk); |
| 1415 | hbus = dr_wrk->bus; |
| 1416 | kfree(dr_wrk); |
| 1417 | |
| 1418 | INIT_LIST_HEAD(&removed); |
| 1419 | |
| 1420 | if (down_interruptible(&hbus->enum_sem)) { |
| 1421 | put_hvpcibus(hbus); |
| 1422 | return; |
| 1423 | } |
| 1424 | |
| 1425 | /* Pull this off the queue and process it if it was the last one. */ |
| 1426 | spin_lock_irqsave(&hbus->device_list_lock, flags); |
| 1427 | while (!list_empty(&hbus->dr_list)) { |
| 1428 | dr = list_first_entry(&hbus->dr_list, struct hv_dr_state, |
| 1429 | list_entry); |
| 1430 | list_del(&dr->list_entry); |
| 1431 | |
| 1432 | /* Throw this away if the list still has stuff in it. */ |
| 1433 | if (!list_empty(&hbus->dr_list)) { |
| 1434 | kfree(dr); |
| 1435 | continue; |
| 1436 | } |
| 1437 | } |
| 1438 | spin_unlock_irqrestore(&hbus->device_list_lock, flags); |
| 1439 | |
| 1440 | if (!dr) { |
| 1441 | up(&hbus->enum_sem); |
| 1442 | put_hvpcibus(hbus); |
| 1443 | return; |
| 1444 | } |
| 1445 | |
| 1446 | /* First, mark all existing children as reported missing. */ |
| 1447 | spin_lock_irqsave(&hbus->device_list_lock, flags); |
| 1448 | list_for_each(iter, &hbus->children) { |
| 1449 | hpdev = container_of(iter, struct hv_pci_dev, |
| 1450 | list_entry); |
| 1451 | hpdev->reported_missing = true; |
| 1452 | } |
| 1453 | spin_unlock_irqrestore(&hbus->device_list_lock, flags); |
| 1454 | |
| 1455 | /* Next, add back any reported devices. */ |
| 1456 | for (child_no = 0; child_no < dr->device_count; child_no++) { |
| 1457 | found = false; |
| 1458 | new_desc = &dr->func[child_no]; |
| 1459 | |
| 1460 | spin_lock_irqsave(&hbus->device_list_lock, flags); |
| 1461 | list_for_each(iter, &hbus->children) { |
| 1462 | hpdev = container_of(iter, struct hv_pci_dev, |
| 1463 | list_entry); |
| 1464 | if ((hpdev->desc.win_slot.slot == |
| 1465 | new_desc->win_slot.slot) && |
| 1466 | (hpdev->desc.v_id == new_desc->v_id) && |
| 1467 | (hpdev->desc.d_id == new_desc->d_id) && |
| 1468 | (hpdev->desc.ser == new_desc->ser)) { |
| 1469 | hpdev->reported_missing = false; |
| 1470 | found = true; |
| 1471 | } |
| 1472 | } |
| 1473 | spin_unlock_irqrestore(&hbus->device_list_lock, flags); |
| 1474 | |
| 1475 | if (!found) { |
| 1476 | hpdev = new_pcichild_device(hbus, new_desc); |
| 1477 | if (!hpdev) |
| 1478 | dev_err(&hbus->hdev->device, |
| 1479 | "couldn't record a child device.\n"); |
| 1480 | } |
| 1481 | } |
| 1482 | |
| 1483 | /* Move missing children to a list on the stack. */ |
| 1484 | spin_lock_irqsave(&hbus->device_list_lock, flags); |
| 1485 | do { |
| 1486 | found = false; |
| 1487 | list_for_each(iter, &hbus->children) { |
| 1488 | hpdev = container_of(iter, struct hv_pci_dev, |
| 1489 | list_entry); |
| 1490 | if (hpdev->reported_missing) { |
| 1491 | found = true; |
| 1492 | put_pcichild(hpdev, hv_pcidev_ref_childlist); |
Wei Yongjun | 4f1cb01 | 2016-07-28 16:16:48 +0000 | [diff] [blame] | 1493 | list_move_tail(&hpdev->list_entry, &removed); |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 1494 | break; |
| 1495 | } |
| 1496 | } |
| 1497 | } while (found); |
| 1498 | spin_unlock_irqrestore(&hbus->device_list_lock, flags); |
| 1499 | |
| 1500 | /* Delete everything that should no longer exist. */ |
| 1501 | while (!list_empty(&removed)) { |
| 1502 | hpdev = list_first_entry(&removed, struct hv_pci_dev, |
| 1503 | list_entry); |
| 1504 | list_del(&hpdev->list_entry); |
| 1505 | put_pcichild(hpdev, hv_pcidev_ref_initial); |
| 1506 | } |
| 1507 | |
Long Li | d3a78d8 | 2017-03-23 14:58:10 -0700 | [diff] [blame^] | 1508 | switch(hbus->state) { |
| 1509 | case hv_pcibus_installed: |
| 1510 | /* |
| 1511 | * Tell the core to rescan bus |
| 1512 | * because there may have been changes. |
| 1513 | */ |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 1514 | pci_lock_rescan_remove(); |
| 1515 | pci_scan_child_bus(hbus->pci_bus); |
| 1516 | pci_unlock_rescan_remove(); |
Long Li | d3a78d8 | 2017-03-23 14:58:10 -0700 | [diff] [blame^] | 1517 | break; |
| 1518 | |
| 1519 | case hv_pcibus_init: |
| 1520 | case hv_pcibus_probed: |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 1521 | survey_child_resources(hbus); |
Long Li | d3a78d8 | 2017-03-23 14:58:10 -0700 | [diff] [blame^] | 1522 | break; |
| 1523 | |
| 1524 | default: |
| 1525 | break; |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
| 1528 | up(&hbus->enum_sem); |
| 1529 | put_hvpcibus(hbus); |
| 1530 | kfree(dr); |
| 1531 | } |
| 1532 | |
| 1533 | /** |
| 1534 | * hv_pci_devices_present() - Handles list of new children |
| 1535 | * @hbus: Root PCI bus, as understood by this driver |
| 1536 | * @relations: Packet from host listing children |
| 1537 | * |
| 1538 | * This function is invoked whenever a new list of devices for |
| 1539 | * this bus appears. |
| 1540 | */ |
| 1541 | static void hv_pci_devices_present(struct hv_pcibus_device *hbus, |
| 1542 | struct pci_bus_relations *relations) |
| 1543 | { |
| 1544 | struct hv_dr_state *dr; |
| 1545 | struct hv_dr_work *dr_wrk; |
| 1546 | unsigned long flags; |
| 1547 | |
| 1548 | dr_wrk = kzalloc(sizeof(*dr_wrk), GFP_NOWAIT); |
| 1549 | if (!dr_wrk) |
| 1550 | return; |
| 1551 | |
| 1552 | dr = kzalloc(offsetof(struct hv_dr_state, func) + |
| 1553 | (sizeof(struct pci_function_description) * |
| 1554 | (relations->device_count)), GFP_NOWAIT); |
| 1555 | if (!dr) { |
| 1556 | kfree(dr_wrk); |
| 1557 | return; |
| 1558 | } |
| 1559 | |
| 1560 | INIT_WORK(&dr_wrk->wrk, pci_devices_present_work); |
| 1561 | dr_wrk->bus = hbus; |
| 1562 | dr->device_count = relations->device_count; |
| 1563 | if (dr->device_count != 0) { |
| 1564 | memcpy(dr->func, relations->func, |
| 1565 | sizeof(struct pci_function_description) * |
| 1566 | dr->device_count); |
| 1567 | } |
| 1568 | |
| 1569 | spin_lock_irqsave(&hbus->device_list_lock, flags); |
| 1570 | list_add_tail(&dr->list_entry, &hbus->dr_list); |
| 1571 | spin_unlock_irqrestore(&hbus->device_list_lock, flags); |
| 1572 | |
| 1573 | get_hvpcibus(hbus); |
| 1574 | schedule_work(&dr_wrk->wrk); |
| 1575 | } |
| 1576 | |
| 1577 | /** |
| 1578 | * hv_eject_device_work() - Asynchronously handles ejection |
| 1579 | * @work: Work struct embedded in internal device struct |
| 1580 | * |
| 1581 | * This function handles ejecting a device. Windows will |
| 1582 | * attempt to gracefully eject a device, waiting 60 seconds to |
| 1583 | * hear back from the guest OS that this completed successfully. |
| 1584 | * If this timer expires, the device will be forcibly removed. |
| 1585 | */ |
| 1586 | static void hv_eject_device_work(struct work_struct *work) |
| 1587 | { |
| 1588 | struct pci_eject_response *ejct_pkt; |
| 1589 | struct hv_pci_dev *hpdev; |
| 1590 | struct pci_dev *pdev; |
| 1591 | unsigned long flags; |
| 1592 | int wslot; |
| 1593 | struct { |
| 1594 | struct pci_packet pkt; |
Dexuan Cui | 0c6045d | 2016-08-23 04:45:51 +0000 | [diff] [blame] | 1595 | u8 buffer[sizeof(struct pci_eject_response)]; |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 1596 | } ctxt; |
| 1597 | |
| 1598 | hpdev = container_of(work, struct hv_pci_dev, wrk); |
| 1599 | |
| 1600 | if (hpdev->state != hv_pcichild_ejecting) { |
| 1601 | put_pcichild(hpdev, hv_pcidev_ref_pnp); |
| 1602 | return; |
| 1603 | } |
| 1604 | |
| 1605 | /* |
| 1606 | * Ejection can come before or after the PCI bus has been set up, so |
| 1607 | * attempt to find it and tear down the bus state, if it exists. This |
| 1608 | * must be done without constructs like pci_domain_nr(hbus->pci_bus) |
| 1609 | * because hbus->pci_bus may not exist yet. |
| 1610 | */ |
| 1611 | wslot = wslot_to_devfn(hpdev->desc.win_slot.slot); |
| 1612 | pdev = pci_get_domain_bus_and_slot(hpdev->hbus->sysdata.domain, 0, |
| 1613 | wslot); |
| 1614 | if (pdev) { |
| 1615 | pci_stop_and_remove_bus_device(pdev); |
| 1616 | pci_dev_put(pdev); |
| 1617 | } |
| 1618 | |
Dexuan Cui | e74d2eb | 2016-11-10 07:19:52 +0000 | [diff] [blame] | 1619 | spin_lock_irqsave(&hpdev->hbus->device_list_lock, flags); |
| 1620 | list_del(&hpdev->list_entry); |
| 1621 | spin_unlock_irqrestore(&hpdev->hbus->device_list_lock, flags); |
| 1622 | |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 1623 | memset(&ctxt, 0, sizeof(ctxt)); |
| 1624 | ejct_pkt = (struct pci_eject_response *)&ctxt.pkt.message; |
Dexuan Cui | 0c6045d | 2016-08-23 04:45:51 +0000 | [diff] [blame] | 1625 | ejct_pkt->message_type.type = PCI_EJECTION_COMPLETE; |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 1626 | ejct_pkt->wslot.slot = hpdev->desc.win_slot.slot; |
| 1627 | vmbus_sendpacket(hpdev->hbus->hdev->channel, ejct_pkt, |
| 1628 | sizeof(*ejct_pkt), (unsigned long)&ctxt.pkt, |
| 1629 | VM_PKT_DATA_INBAND, 0); |
| 1630 | |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 1631 | put_pcichild(hpdev, hv_pcidev_ref_childlist); |
| 1632 | put_pcichild(hpdev, hv_pcidev_ref_pnp); |
| 1633 | put_hvpcibus(hpdev->hbus); |
| 1634 | } |
| 1635 | |
| 1636 | /** |
| 1637 | * hv_pci_eject_device() - Handles device ejection |
| 1638 | * @hpdev: Internal device tracking struct |
| 1639 | * |
| 1640 | * This function is invoked when an ejection packet arrives. It |
| 1641 | * just schedules work so that we don't re-enter the packet |
| 1642 | * delivery code handling the ejection. |
| 1643 | */ |
| 1644 | static void hv_pci_eject_device(struct hv_pci_dev *hpdev) |
| 1645 | { |
| 1646 | hpdev->state = hv_pcichild_ejecting; |
| 1647 | get_pcichild(hpdev, hv_pcidev_ref_pnp); |
| 1648 | INIT_WORK(&hpdev->wrk, hv_eject_device_work); |
| 1649 | get_hvpcibus(hpdev->hbus); |
| 1650 | schedule_work(&hpdev->wrk); |
| 1651 | } |
| 1652 | |
| 1653 | /** |
| 1654 | * hv_pci_onchannelcallback() - Handles incoming packets |
| 1655 | * @context: Internal bus tracking struct |
| 1656 | * |
| 1657 | * This function is invoked whenever the host sends a packet to |
| 1658 | * this channel (which is private to this root PCI bus). |
| 1659 | */ |
| 1660 | static void hv_pci_onchannelcallback(void *context) |
| 1661 | { |
| 1662 | const int packet_size = 0x100; |
| 1663 | int ret; |
| 1664 | struct hv_pcibus_device *hbus = context; |
| 1665 | u32 bytes_recvd; |
| 1666 | u64 req_id; |
| 1667 | struct vmpacket_descriptor *desc; |
| 1668 | unsigned char *buffer; |
| 1669 | int bufferlen = packet_size; |
| 1670 | struct pci_packet *comp_packet; |
| 1671 | struct pci_response *response; |
| 1672 | struct pci_incoming_message *new_message; |
| 1673 | struct pci_bus_relations *bus_rel; |
| 1674 | struct pci_dev_incoming *dev_message; |
| 1675 | struct hv_pci_dev *hpdev; |
| 1676 | |
| 1677 | buffer = kmalloc(bufferlen, GFP_ATOMIC); |
| 1678 | if (!buffer) |
| 1679 | return; |
| 1680 | |
| 1681 | while (1) { |
| 1682 | ret = vmbus_recvpacket_raw(hbus->hdev->channel, buffer, |
| 1683 | bufferlen, &bytes_recvd, &req_id); |
| 1684 | |
| 1685 | if (ret == -ENOBUFS) { |
| 1686 | kfree(buffer); |
| 1687 | /* Handle large packet */ |
| 1688 | bufferlen = bytes_recvd; |
| 1689 | buffer = kmalloc(bytes_recvd, GFP_ATOMIC); |
| 1690 | if (!buffer) |
| 1691 | return; |
| 1692 | continue; |
| 1693 | } |
| 1694 | |
Vitaly Kuznetsov | 837d741 | 2016-06-17 12:45:30 -0500 | [diff] [blame] | 1695 | /* Zero length indicates there are no more packets. */ |
| 1696 | if (ret || !bytes_recvd) |
| 1697 | break; |
| 1698 | |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 1699 | /* |
| 1700 | * All incoming packets must be at least as large as a |
| 1701 | * response. |
| 1702 | */ |
Vitaly Kuznetsov | 60fcdac | 2016-05-30 16:17:58 +0200 | [diff] [blame] | 1703 | if (bytes_recvd <= sizeof(struct pci_response)) |
Vitaly Kuznetsov | 837d741 | 2016-06-17 12:45:30 -0500 | [diff] [blame] | 1704 | continue; |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 1705 | desc = (struct vmpacket_descriptor *)buffer; |
| 1706 | |
| 1707 | switch (desc->type) { |
| 1708 | case VM_PKT_COMP: |
| 1709 | |
| 1710 | /* |
| 1711 | * The host is trusted, and thus it's safe to interpret |
| 1712 | * this transaction ID as a pointer. |
| 1713 | */ |
| 1714 | comp_packet = (struct pci_packet *)req_id; |
| 1715 | response = (struct pci_response *)buffer; |
| 1716 | comp_packet->completion_func(comp_packet->compl_ctxt, |
| 1717 | response, |
| 1718 | bytes_recvd); |
Vitaly Kuznetsov | 60fcdac | 2016-05-30 16:17:58 +0200 | [diff] [blame] | 1719 | break; |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 1720 | |
| 1721 | case VM_PKT_DATA_INBAND: |
| 1722 | |
| 1723 | new_message = (struct pci_incoming_message *)buffer; |
Dexuan Cui | 0c6045d | 2016-08-23 04:45:51 +0000 | [diff] [blame] | 1724 | switch (new_message->message_type.type) { |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 1725 | case PCI_BUS_RELATIONS: |
| 1726 | |
| 1727 | bus_rel = (struct pci_bus_relations *)buffer; |
| 1728 | if (bytes_recvd < |
| 1729 | offsetof(struct pci_bus_relations, func) + |
| 1730 | (sizeof(struct pci_function_description) * |
| 1731 | (bus_rel->device_count))) { |
| 1732 | dev_err(&hbus->hdev->device, |
| 1733 | "bus relations too small\n"); |
| 1734 | break; |
| 1735 | } |
| 1736 | |
| 1737 | hv_pci_devices_present(hbus, bus_rel); |
| 1738 | break; |
| 1739 | |
| 1740 | case PCI_EJECT: |
| 1741 | |
| 1742 | dev_message = (struct pci_dev_incoming *)buffer; |
| 1743 | hpdev = get_pcichild_wslot(hbus, |
| 1744 | dev_message->wslot.slot); |
| 1745 | if (hpdev) { |
| 1746 | hv_pci_eject_device(hpdev); |
| 1747 | put_pcichild(hpdev, |
| 1748 | hv_pcidev_ref_by_slot); |
| 1749 | } |
| 1750 | break; |
| 1751 | |
| 1752 | default: |
| 1753 | dev_warn(&hbus->hdev->device, |
| 1754 | "Unimplemented protocol message %x\n", |
Dexuan Cui | 0c6045d | 2016-08-23 04:45:51 +0000 | [diff] [blame] | 1755 | new_message->message_type.type); |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 1756 | break; |
| 1757 | } |
| 1758 | break; |
| 1759 | |
| 1760 | default: |
| 1761 | dev_err(&hbus->hdev->device, |
| 1762 | "unhandled packet type %d, tid %llx len %d\n", |
| 1763 | desc->type, req_id, bytes_recvd); |
| 1764 | break; |
| 1765 | } |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 1766 | } |
Vitaly Kuznetsov | 60fcdac | 2016-05-30 16:17:58 +0200 | [diff] [blame] | 1767 | |
| 1768 | kfree(buffer); |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 1769 | } |
| 1770 | |
| 1771 | /** |
| 1772 | * hv_pci_protocol_negotiation() - Set up protocol |
| 1773 | * @hdev: VMBus's tracking struct for this root PCI bus |
| 1774 | * |
| 1775 | * This driver is intended to support running on Windows 10 |
| 1776 | * (server) and later versions. It will not run on earlier |
| 1777 | * versions, as they assume that many of the operations which |
| 1778 | * Linux needs accomplished with a spinlock held were done via |
| 1779 | * asynchronous messaging via VMBus. Windows 10 increases the |
| 1780 | * surface area of PCI emulation so that these actions can take |
| 1781 | * place by suspending a virtual processor for their duration. |
| 1782 | * |
| 1783 | * This function negotiates the channel protocol version, |
| 1784 | * failing if the host doesn't support the necessary protocol |
| 1785 | * level. |
| 1786 | */ |
| 1787 | static int hv_pci_protocol_negotiation(struct hv_device *hdev) |
| 1788 | { |
| 1789 | struct pci_version_request *version_req; |
| 1790 | struct hv_pci_compl comp_pkt; |
| 1791 | struct pci_packet *pkt; |
| 1792 | int ret; |
| 1793 | |
| 1794 | /* |
| 1795 | * Initiate the handshake with the host and negotiate |
| 1796 | * a version that the host can support. We start with the |
| 1797 | * highest version number and go down if the host cannot |
| 1798 | * support it. |
| 1799 | */ |
| 1800 | pkt = kzalloc(sizeof(*pkt) + sizeof(*version_req), GFP_KERNEL); |
| 1801 | if (!pkt) |
| 1802 | return -ENOMEM; |
| 1803 | |
| 1804 | init_completion(&comp_pkt.host_event); |
| 1805 | pkt->completion_func = hv_pci_generic_compl; |
| 1806 | pkt->compl_ctxt = &comp_pkt; |
| 1807 | version_req = (struct pci_version_request *)&pkt->message; |
Dexuan Cui | 0c6045d | 2016-08-23 04:45:51 +0000 | [diff] [blame] | 1808 | version_req->message_type.type = PCI_QUERY_PROTOCOL_VERSION; |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 1809 | version_req->protocol_version = PCI_PROTOCOL_VERSION_CURRENT; |
| 1810 | |
| 1811 | ret = vmbus_sendpacket(hdev->channel, version_req, |
| 1812 | sizeof(struct pci_version_request), |
| 1813 | (unsigned long)pkt, VM_PKT_DATA_INBAND, |
| 1814 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); |
| 1815 | if (ret) |
| 1816 | goto exit; |
| 1817 | |
| 1818 | wait_for_completion(&comp_pkt.host_event); |
| 1819 | |
| 1820 | if (comp_pkt.completion_status < 0) { |
| 1821 | dev_err(&hdev->device, |
| 1822 | "PCI Pass-through VSP failed version request %x\n", |
| 1823 | comp_pkt.completion_status); |
| 1824 | ret = -EPROTO; |
| 1825 | goto exit; |
| 1826 | } |
| 1827 | |
| 1828 | ret = 0; |
| 1829 | |
| 1830 | exit: |
| 1831 | kfree(pkt); |
| 1832 | return ret; |
| 1833 | } |
| 1834 | |
| 1835 | /** |
| 1836 | * hv_pci_free_bridge_windows() - Release memory regions for the |
| 1837 | * bus |
| 1838 | * @hbus: Root PCI bus, as understood by this driver |
| 1839 | */ |
| 1840 | static void hv_pci_free_bridge_windows(struct hv_pcibus_device *hbus) |
| 1841 | { |
| 1842 | /* |
| 1843 | * Set the resources back to the way they looked when they |
| 1844 | * were allocated by setting IORESOURCE_BUSY again. |
| 1845 | */ |
| 1846 | |
| 1847 | if (hbus->low_mmio_space && hbus->low_mmio_res) { |
| 1848 | hbus->low_mmio_res->flags |= IORESOURCE_BUSY; |
Jake Oshins | 696ca5e | 2016-04-05 10:22:52 -0700 | [diff] [blame] | 1849 | vmbus_free_mmio(hbus->low_mmio_res->start, |
| 1850 | resource_size(hbus->low_mmio_res)); |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 1851 | } |
| 1852 | |
| 1853 | if (hbus->high_mmio_space && hbus->high_mmio_res) { |
| 1854 | hbus->high_mmio_res->flags |= IORESOURCE_BUSY; |
Jake Oshins | 696ca5e | 2016-04-05 10:22:52 -0700 | [diff] [blame] | 1855 | vmbus_free_mmio(hbus->high_mmio_res->start, |
| 1856 | resource_size(hbus->high_mmio_res)); |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 1857 | } |
| 1858 | } |
| 1859 | |
| 1860 | /** |
| 1861 | * hv_pci_allocate_bridge_windows() - Allocate memory regions |
| 1862 | * for the bus |
| 1863 | * @hbus: Root PCI bus, as understood by this driver |
| 1864 | * |
| 1865 | * This function calls vmbus_allocate_mmio(), which is itself a |
| 1866 | * bit of a compromise. Ideally, we might change the pnp layer |
| 1867 | * in the kernel such that it comprehends either PCI devices |
| 1868 | * which are "grandchildren of ACPI," with some intermediate bus |
| 1869 | * node (in this case, VMBus) or change it such that it |
| 1870 | * understands VMBus. The pnp layer, however, has been declared |
| 1871 | * deprecated, and not subject to change. |
| 1872 | * |
| 1873 | * The workaround, implemented here, is to ask VMBus to allocate |
| 1874 | * MMIO space for this bus. VMBus itself knows which ranges are |
| 1875 | * appropriate by looking at its own ACPI objects. Then, after |
| 1876 | * these ranges are claimed, they're modified to look like they |
| 1877 | * would have looked if the ACPI and pnp code had allocated |
| 1878 | * bridge windows. These descriptors have to exist in this form |
| 1879 | * in order to satisfy the code which will get invoked when the |
| 1880 | * endpoint PCI function driver calls request_mem_region() or |
| 1881 | * request_mem_region_exclusive(). |
| 1882 | * |
| 1883 | * Return: 0 on success, -errno on failure |
| 1884 | */ |
| 1885 | static int hv_pci_allocate_bridge_windows(struct hv_pcibus_device *hbus) |
| 1886 | { |
| 1887 | resource_size_t align; |
| 1888 | int ret; |
| 1889 | |
| 1890 | if (hbus->low_mmio_space) { |
| 1891 | align = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space)); |
| 1892 | ret = vmbus_allocate_mmio(&hbus->low_mmio_res, hbus->hdev, 0, |
| 1893 | (u64)(u32)0xffffffff, |
| 1894 | hbus->low_mmio_space, |
| 1895 | align, false); |
| 1896 | if (ret) { |
| 1897 | dev_err(&hbus->hdev->device, |
| 1898 | "Need %#llx of low MMIO space. Consider reconfiguring the VM.\n", |
| 1899 | hbus->low_mmio_space); |
| 1900 | return ret; |
| 1901 | } |
| 1902 | |
| 1903 | /* Modify this resource to become a bridge window. */ |
| 1904 | hbus->low_mmio_res->flags |= IORESOURCE_WINDOW; |
| 1905 | hbus->low_mmio_res->flags &= ~IORESOURCE_BUSY; |
| 1906 | pci_add_resource(&hbus->resources_for_children, |
| 1907 | hbus->low_mmio_res); |
| 1908 | } |
| 1909 | |
| 1910 | if (hbus->high_mmio_space) { |
| 1911 | align = 1ULL << (63 - __builtin_clzll(hbus->high_mmio_space)); |
| 1912 | ret = vmbus_allocate_mmio(&hbus->high_mmio_res, hbus->hdev, |
| 1913 | 0x100000000, -1, |
| 1914 | hbus->high_mmio_space, align, |
| 1915 | false); |
| 1916 | if (ret) { |
| 1917 | dev_err(&hbus->hdev->device, |
| 1918 | "Need %#llx of high MMIO space. Consider reconfiguring the VM.\n", |
| 1919 | hbus->high_mmio_space); |
| 1920 | goto release_low_mmio; |
| 1921 | } |
| 1922 | |
| 1923 | /* Modify this resource to become a bridge window. */ |
| 1924 | hbus->high_mmio_res->flags |= IORESOURCE_WINDOW; |
| 1925 | hbus->high_mmio_res->flags &= ~IORESOURCE_BUSY; |
| 1926 | pci_add_resource(&hbus->resources_for_children, |
| 1927 | hbus->high_mmio_res); |
| 1928 | } |
| 1929 | |
| 1930 | return 0; |
| 1931 | |
| 1932 | release_low_mmio: |
| 1933 | if (hbus->low_mmio_res) { |
Jake Oshins | 696ca5e | 2016-04-05 10:22:52 -0700 | [diff] [blame] | 1934 | vmbus_free_mmio(hbus->low_mmio_res->start, |
| 1935 | resource_size(hbus->low_mmio_res)); |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 1936 | } |
| 1937 | |
| 1938 | return ret; |
| 1939 | } |
| 1940 | |
| 1941 | /** |
| 1942 | * hv_allocate_config_window() - Find MMIO space for PCI Config |
| 1943 | * @hbus: Root PCI bus, as understood by this driver |
| 1944 | * |
| 1945 | * This function claims memory-mapped I/O space for accessing |
| 1946 | * configuration space for the functions on this bus. |
| 1947 | * |
| 1948 | * Return: 0 on success, -errno on failure |
| 1949 | */ |
| 1950 | static int hv_allocate_config_window(struct hv_pcibus_device *hbus) |
| 1951 | { |
| 1952 | int ret; |
| 1953 | |
| 1954 | /* |
| 1955 | * Set up a region of MMIO space to use for accessing configuration |
| 1956 | * space. |
| 1957 | */ |
| 1958 | ret = vmbus_allocate_mmio(&hbus->mem_config, hbus->hdev, 0, -1, |
| 1959 | PCI_CONFIG_MMIO_LENGTH, 0x1000, false); |
| 1960 | if (ret) |
| 1961 | return ret; |
| 1962 | |
| 1963 | /* |
| 1964 | * vmbus_allocate_mmio() gets used for allocating both device endpoint |
| 1965 | * resource claims (those which cannot be overlapped) and the ranges |
| 1966 | * which are valid for the children of this bus, which are intended |
| 1967 | * to be overlapped by those children. Set the flag on this claim |
| 1968 | * meaning that this region can't be overlapped. |
| 1969 | */ |
| 1970 | |
| 1971 | hbus->mem_config->flags |= IORESOURCE_BUSY; |
| 1972 | |
| 1973 | return 0; |
| 1974 | } |
| 1975 | |
| 1976 | static void hv_free_config_window(struct hv_pcibus_device *hbus) |
| 1977 | { |
Jake Oshins | 696ca5e | 2016-04-05 10:22:52 -0700 | [diff] [blame] | 1978 | vmbus_free_mmio(hbus->mem_config->start, PCI_CONFIG_MMIO_LENGTH); |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 1979 | } |
| 1980 | |
| 1981 | /** |
| 1982 | * hv_pci_enter_d0() - Bring the "bus" into the D0 power state |
| 1983 | * @hdev: VMBus's tracking struct for this root PCI bus |
| 1984 | * |
| 1985 | * Return: 0 on success, -errno on failure |
| 1986 | */ |
| 1987 | static int hv_pci_enter_d0(struct hv_device *hdev) |
| 1988 | { |
| 1989 | struct hv_pcibus_device *hbus = hv_get_drvdata(hdev); |
| 1990 | struct pci_bus_d0_entry *d0_entry; |
| 1991 | struct hv_pci_compl comp_pkt; |
| 1992 | struct pci_packet *pkt; |
| 1993 | int ret; |
| 1994 | |
| 1995 | /* |
| 1996 | * Tell the host that the bus is ready to use, and moved into the |
| 1997 | * powered-on state. This includes telling the host which region |
| 1998 | * of memory-mapped I/O space has been chosen for configuration space |
| 1999 | * access. |
| 2000 | */ |
| 2001 | pkt = kzalloc(sizeof(*pkt) + sizeof(*d0_entry), GFP_KERNEL); |
| 2002 | if (!pkt) |
| 2003 | return -ENOMEM; |
| 2004 | |
| 2005 | init_completion(&comp_pkt.host_event); |
| 2006 | pkt->completion_func = hv_pci_generic_compl; |
| 2007 | pkt->compl_ctxt = &comp_pkt; |
| 2008 | d0_entry = (struct pci_bus_d0_entry *)&pkt->message; |
Dexuan Cui | 0c6045d | 2016-08-23 04:45:51 +0000 | [diff] [blame] | 2009 | d0_entry->message_type.type = PCI_BUS_D0ENTRY; |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 2010 | d0_entry->mmio_base = hbus->mem_config->start; |
| 2011 | |
| 2012 | ret = vmbus_sendpacket(hdev->channel, d0_entry, sizeof(*d0_entry), |
| 2013 | (unsigned long)pkt, VM_PKT_DATA_INBAND, |
| 2014 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); |
| 2015 | if (ret) |
| 2016 | goto exit; |
| 2017 | |
| 2018 | wait_for_completion(&comp_pkt.host_event); |
| 2019 | |
| 2020 | if (comp_pkt.completion_status < 0) { |
| 2021 | dev_err(&hdev->device, |
| 2022 | "PCI Pass-through VSP failed D0 Entry with status %x\n", |
| 2023 | comp_pkt.completion_status); |
| 2024 | ret = -EPROTO; |
| 2025 | goto exit; |
| 2026 | } |
| 2027 | |
| 2028 | ret = 0; |
| 2029 | |
| 2030 | exit: |
| 2031 | kfree(pkt); |
| 2032 | return ret; |
| 2033 | } |
| 2034 | |
| 2035 | /** |
| 2036 | * hv_pci_query_relations() - Ask host to send list of child |
| 2037 | * devices |
| 2038 | * @hdev: VMBus's tracking struct for this root PCI bus |
| 2039 | * |
| 2040 | * Return: 0 on success, -errno on failure |
| 2041 | */ |
| 2042 | static int hv_pci_query_relations(struct hv_device *hdev) |
| 2043 | { |
| 2044 | struct hv_pcibus_device *hbus = hv_get_drvdata(hdev); |
| 2045 | struct pci_message message; |
| 2046 | struct completion comp; |
| 2047 | int ret; |
| 2048 | |
| 2049 | /* Ask the host to send along the list of child devices */ |
| 2050 | init_completion(&comp); |
| 2051 | if (cmpxchg(&hbus->survey_event, NULL, &comp)) |
| 2052 | return -ENOTEMPTY; |
| 2053 | |
| 2054 | memset(&message, 0, sizeof(message)); |
Dexuan Cui | 0c6045d | 2016-08-23 04:45:51 +0000 | [diff] [blame] | 2055 | message.type = PCI_QUERY_BUS_RELATIONS; |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 2056 | |
| 2057 | ret = vmbus_sendpacket(hdev->channel, &message, sizeof(message), |
| 2058 | 0, VM_PKT_DATA_INBAND, 0); |
| 2059 | if (ret) |
| 2060 | return ret; |
| 2061 | |
| 2062 | wait_for_completion(&comp); |
| 2063 | return 0; |
| 2064 | } |
| 2065 | |
| 2066 | /** |
| 2067 | * hv_send_resources_allocated() - Report local resource choices |
| 2068 | * @hdev: VMBus's tracking struct for this root PCI bus |
| 2069 | * |
| 2070 | * The host OS is expecting to be sent a request as a message |
| 2071 | * which contains all the resources that the device will use. |
| 2072 | * The response contains those same resources, "translated" |
| 2073 | * which is to say, the values which should be used by the |
| 2074 | * hardware, when it delivers an interrupt. (MMIO resources are |
| 2075 | * used in local terms.) This is nice for Windows, and lines up |
| 2076 | * with the FDO/PDO split, which doesn't exist in Linux. Linux |
| 2077 | * is deeply expecting to scan an emulated PCI configuration |
| 2078 | * space. So this message is sent here only to drive the state |
| 2079 | * machine on the host forward. |
| 2080 | * |
| 2081 | * Return: 0 on success, -errno on failure |
| 2082 | */ |
| 2083 | static int hv_send_resources_allocated(struct hv_device *hdev) |
| 2084 | { |
| 2085 | struct hv_pcibus_device *hbus = hv_get_drvdata(hdev); |
| 2086 | struct pci_resources_assigned *res_assigned; |
| 2087 | struct hv_pci_compl comp_pkt; |
| 2088 | struct hv_pci_dev *hpdev; |
| 2089 | struct pci_packet *pkt; |
| 2090 | u32 wslot; |
| 2091 | int ret; |
| 2092 | |
| 2093 | pkt = kmalloc(sizeof(*pkt) + sizeof(*res_assigned), GFP_KERNEL); |
| 2094 | if (!pkt) |
| 2095 | return -ENOMEM; |
| 2096 | |
| 2097 | ret = 0; |
| 2098 | |
| 2099 | for (wslot = 0; wslot < 256; wslot++) { |
| 2100 | hpdev = get_pcichild_wslot(hbus, wslot); |
| 2101 | if (!hpdev) |
| 2102 | continue; |
| 2103 | |
| 2104 | memset(pkt, 0, sizeof(*pkt) + sizeof(*res_assigned)); |
| 2105 | init_completion(&comp_pkt.host_event); |
| 2106 | pkt->completion_func = hv_pci_generic_compl; |
| 2107 | pkt->compl_ctxt = &comp_pkt; |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 2108 | res_assigned = (struct pci_resources_assigned *)&pkt->message; |
Dexuan Cui | 0c6045d | 2016-08-23 04:45:51 +0000 | [diff] [blame] | 2109 | res_assigned->message_type.type = PCI_RESOURCES_ASSIGNED; |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 2110 | res_assigned->wslot.slot = hpdev->desc.win_slot.slot; |
| 2111 | |
| 2112 | put_pcichild(hpdev, hv_pcidev_ref_by_slot); |
| 2113 | |
| 2114 | ret = vmbus_sendpacket( |
| 2115 | hdev->channel, &pkt->message, |
| 2116 | sizeof(*res_assigned), |
| 2117 | (unsigned long)pkt, |
| 2118 | VM_PKT_DATA_INBAND, |
| 2119 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); |
| 2120 | if (ret) |
| 2121 | break; |
| 2122 | |
| 2123 | wait_for_completion(&comp_pkt.host_event); |
| 2124 | |
| 2125 | if (comp_pkt.completion_status < 0) { |
| 2126 | ret = -EPROTO; |
| 2127 | dev_err(&hdev->device, |
| 2128 | "resource allocated returned 0x%x", |
| 2129 | comp_pkt.completion_status); |
| 2130 | break; |
| 2131 | } |
| 2132 | } |
| 2133 | |
| 2134 | kfree(pkt); |
| 2135 | return ret; |
| 2136 | } |
| 2137 | |
| 2138 | /** |
| 2139 | * hv_send_resources_released() - Report local resources |
| 2140 | * released |
| 2141 | * @hdev: VMBus's tracking struct for this root PCI bus |
| 2142 | * |
| 2143 | * Return: 0 on success, -errno on failure |
| 2144 | */ |
| 2145 | static int hv_send_resources_released(struct hv_device *hdev) |
| 2146 | { |
| 2147 | struct hv_pcibus_device *hbus = hv_get_drvdata(hdev); |
| 2148 | struct pci_child_message pkt; |
| 2149 | struct hv_pci_dev *hpdev; |
| 2150 | u32 wslot; |
| 2151 | int ret; |
| 2152 | |
| 2153 | for (wslot = 0; wslot < 256; wslot++) { |
| 2154 | hpdev = get_pcichild_wslot(hbus, wslot); |
| 2155 | if (!hpdev) |
| 2156 | continue; |
| 2157 | |
| 2158 | memset(&pkt, 0, sizeof(pkt)); |
Dexuan Cui | 0c6045d | 2016-08-23 04:45:51 +0000 | [diff] [blame] | 2159 | pkt.message_type.type = PCI_RESOURCES_RELEASED; |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 2160 | pkt.wslot.slot = hpdev->desc.win_slot.slot; |
| 2161 | |
| 2162 | put_pcichild(hpdev, hv_pcidev_ref_by_slot); |
| 2163 | |
| 2164 | ret = vmbus_sendpacket(hdev->channel, &pkt, sizeof(pkt), 0, |
| 2165 | VM_PKT_DATA_INBAND, 0); |
| 2166 | if (ret) |
| 2167 | return ret; |
| 2168 | } |
| 2169 | |
| 2170 | return 0; |
| 2171 | } |
| 2172 | |
| 2173 | static void get_hvpcibus(struct hv_pcibus_device *hbus) |
| 2174 | { |
| 2175 | atomic_inc(&hbus->remove_lock); |
| 2176 | } |
| 2177 | |
| 2178 | static void put_hvpcibus(struct hv_pcibus_device *hbus) |
| 2179 | { |
| 2180 | if (atomic_dec_and_test(&hbus->remove_lock)) |
| 2181 | complete(&hbus->remove_event); |
| 2182 | } |
| 2183 | |
| 2184 | /** |
| 2185 | * hv_pci_probe() - New VMBus channel probe, for a root PCI bus |
| 2186 | * @hdev: VMBus's tracking struct for this root PCI bus |
| 2187 | * @dev_id: Identifies the device itself |
| 2188 | * |
| 2189 | * Return: 0 on success, -errno on failure |
| 2190 | */ |
| 2191 | static int hv_pci_probe(struct hv_device *hdev, |
| 2192 | const struct hv_vmbus_device_id *dev_id) |
| 2193 | { |
| 2194 | struct hv_pcibus_device *hbus; |
| 2195 | int ret; |
| 2196 | |
| 2197 | hbus = kzalloc(sizeof(*hbus), GFP_KERNEL); |
| 2198 | if (!hbus) |
| 2199 | return -ENOMEM; |
Long Li | d3a78d8 | 2017-03-23 14:58:10 -0700 | [diff] [blame^] | 2200 | hbus->state = hv_pcibus_init; |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 2201 | |
| 2202 | /* |
| 2203 | * The PCI bus "domain" is what is called "segment" in ACPI and |
| 2204 | * other specs. Pull it from the instance ID, to get something |
| 2205 | * unique. Bytes 8 and 9 are what is used in Windows guests, so |
| 2206 | * do the same thing for consistency. Note that, since this code |
| 2207 | * only runs in a Hyper-V VM, Hyper-V can (and does) guarantee |
| 2208 | * that (1) the only domain in use for something that looks like |
| 2209 | * a physical PCI bus (which is actually emulated by the |
| 2210 | * hypervisor) is domain 0 and (2) there will be no overlap |
| 2211 | * between domains derived from these instance IDs in the same |
| 2212 | * VM. |
| 2213 | */ |
| 2214 | hbus->sysdata.domain = hdev->dev_instance.b[9] | |
| 2215 | hdev->dev_instance.b[8] << 8; |
| 2216 | |
| 2217 | hbus->hdev = hdev; |
| 2218 | atomic_inc(&hbus->remove_lock); |
| 2219 | INIT_LIST_HEAD(&hbus->children); |
| 2220 | INIT_LIST_HEAD(&hbus->dr_list); |
| 2221 | INIT_LIST_HEAD(&hbus->resources_for_children); |
| 2222 | spin_lock_init(&hbus->config_lock); |
| 2223 | spin_lock_init(&hbus->device_list_lock); |
Long Li | 0de8ce3 | 2016-11-08 14:04:38 -0800 | [diff] [blame] | 2224 | spin_lock_init(&hbus->retarget_msi_interrupt_lock); |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 2225 | sema_init(&hbus->enum_sem, 1); |
| 2226 | init_completion(&hbus->remove_event); |
| 2227 | |
| 2228 | ret = vmbus_open(hdev->channel, pci_ring_size, pci_ring_size, NULL, 0, |
| 2229 | hv_pci_onchannelcallback, hbus); |
| 2230 | if (ret) |
| 2231 | goto free_bus; |
| 2232 | |
| 2233 | hv_set_drvdata(hdev, hbus); |
| 2234 | |
| 2235 | ret = hv_pci_protocol_negotiation(hdev); |
| 2236 | if (ret) |
| 2237 | goto close; |
| 2238 | |
| 2239 | ret = hv_allocate_config_window(hbus); |
| 2240 | if (ret) |
| 2241 | goto close; |
| 2242 | |
| 2243 | hbus->cfg_addr = ioremap(hbus->mem_config->start, |
| 2244 | PCI_CONFIG_MMIO_LENGTH); |
| 2245 | if (!hbus->cfg_addr) { |
| 2246 | dev_err(&hdev->device, |
| 2247 | "Unable to map a virtual address for config space\n"); |
| 2248 | ret = -ENOMEM; |
| 2249 | goto free_config; |
| 2250 | } |
| 2251 | |
| 2252 | hbus->sysdata.fwnode = irq_domain_alloc_fwnode(hbus); |
| 2253 | if (!hbus->sysdata.fwnode) { |
| 2254 | ret = -ENOMEM; |
| 2255 | goto unmap; |
| 2256 | } |
| 2257 | |
| 2258 | ret = hv_pcie_init_irq_domain(hbus); |
| 2259 | if (ret) |
| 2260 | goto free_fwnode; |
| 2261 | |
| 2262 | ret = hv_pci_query_relations(hdev); |
| 2263 | if (ret) |
| 2264 | goto free_irq_domain; |
| 2265 | |
| 2266 | ret = hv_pci_enter_d0(hdev); |
| 2267 | if (ret) |
| 2268 | goto free_irq_domain; |
| 2269 | |
| 2270 | ret = hv_pci_allocate_bridge_windows(hbus); |
| 2271 | if (ret) |
| 2272 | goto free_irq_domain; |
| 2273 | |
| 2274 | ret = hv_send_resources_allocated(hdev); |
| 2275 | if (ret) |
| 2276 | goto free_windows; |
| 2277 | |
| 2278 | prepopulate_bars(hbus); |
| 2279 | |
| 2280 | hbus->state = hv_pcibus_probed; |
| 2281 | |
| 2282 | ret = create_root_hv_pci_bus(hbus); |
| 2283 | if (ret) |
| 2284 | goto free_windows; |
| 2285 | |
| 2286 | return 0; |
| 2287 | |
| 2288 | free_windows: |
| 2289 | hv_pci_free_bridge_windows(hbus); |
| 2290 | free_irq_domain: |
| 2291 | irq_domain_remove(hbus->irq_domain); |
| 2292 | free_fwnode: |
| 2293 | irq_domain_free_fwnode(hbus->sysdata.fwnode); |
| 2294 | unmap: |
| 2295 | iounmap(hbus->cfg_addr); |
| 2296 | free_config: |
| 2297 | hv_free_config_window(hbus); |
| 2298 | close: |
| 2299 | vmbus_close(hdev->channel); |
| 2300 | free_bus: |
| 2301 | kfree(hbus); |
| 2302 | return ret; |
| 2303 | } |
| 2304 | |
Dexuan Cui | 17978524 | 2016-11-10 07:18:47 +0000 | [diff] [blame] | 2305 | static void hv_pci_bus_exit(struct hv_device *hdev) |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 2306 | { |
Dexuan Cui | 17978524 | 2016-11-10 07:18:47 +0000 | [diff] [blame] | 2307 | struct hv_pcibus_device *hbus = hv_get_drvdata(hdev); |
| 2308 | struct { |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 2309 | struct pci_packet teardown_packet; |
Dexuan Cui | 17978524 | 2016-11-10 07:18:47 +0000 | [diff] [blame] | 2310 | u8 buffer[sizeof(struct pci_message)]; |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 2311 | } pkt; |
| 2312 | struct pci_bus_relations relations; |
| 2313 | struct hv_pci_compl comp_pkt; |
Dexuan Cui | 17978524 | 2016-11-10 07:18:47 +0000 | [diff] [blame] | 2314 | int ret; |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 2315 | |
Dexuan Cui | 17978524 | 2016-11-10 07:18:47 +0000 | [diff] [blame] | 2316 | /* |
| 2317 | * After the host sends the RESCIND_CHANNEL message, it doesn't |
| 2318 | * access the per-channel ringbuffer any longer. |
| 2319 | */ |
| 2320 | if (hdev->channel->rescind) |
| 2321 | return; |
| 2322 | |
| 2323 | /* Delete any children which might still exist. */ |
| 2324 | memset(&relations, 0, sizeof(relations)); |
| 2325 | hv_pci_devices_present(hbus, &relations); |
| 2326 | |
| 2327 | ret = hv_send_resources_released(hdev); |
| 2328 | if (ret) |
| 2329 | dev_err(&hdev->device, |
| 2330 | "Couldn't send resources released packet(s)\n"); |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 2331 | |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 2332 | memset(&pkt.teardown_packet, 0, sizeof(pkt.teardown_packet)); |
| 2333 | init_completion(&comp_pkt.host_event); |
| 2334 | pkt.teardown_packet.completion_func = hv_pci_generic_compl; |
| 2335 | pkt.teardown_packet.compl_ctxt = &comp_pkt; |
Dexuan Cui | 0c6045d | 2016-08-23 04:45:51 +0000 | [diff] [blame] | 2336 | pkt.teardown_packet.message[0].type = PCI_BUS_D0EXIT; |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 2337 | |
| 2338 | ret = vmbus_sendpacket(hdev->channel, &pkt.teardown_packet.message, |
| 2339 | sizeof(struct pci_message), |
| 2340 | (unsigned long)&pkt.teardown_packet, |
| 2341 | VM_PKT_DATA_INBAND, |
| 2342 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); |
| 2343 | if (!ret) |
| 2344 | wait_for_completion_timeout(&comp_pkt.host_event, 10 * HZ); |
Dexuan Cui | 17978524 | 2016-11-10 07:18:47 +0000 | [diff] [blame] | 2345 | } |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 2346 | |
Dexuan Cui | 17978524 | 2016-11-10 07:18:47 +0000 | [diff] [blame] | 2347 | /** |
| 2348 | * hv_pci_remove() - Remove routine for this VMBus channel |
| 2349 | * @hdev: VMBus's tracking struct for this root PCI bus |
| 2350 | * |
| 2351 | * Return: 0 on success, -errno on failure |
| 2352 | */ |
| 2353 | static int hv_pci_remove(struct hv_device *hdev) |
| 2354 | { |
| 2355 | struct hv_pcibus_device *hbus; |
| 2356 | |
| 2357 | hbus = hv_get_drvdata(hdev); |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 2358 | if (hbus->state == hv_pcibus_installed) { |
| 2359 | /* Remove the bus from PCI's point of view. */ |
| 2360 | pci_lock_rescan_remove(); |
| 2361 | pci_stop_root_bus(hbus->pci_bus); |
| 2362 | pci_remove_root_bus(hbus->pci_bus); |
| 2363 | pci_unlock_rescan_remove(); |
Long Li | d3a78d8 | 2017-03-23 14:58:10 -0700 | [diff] [blame^] | 2364 | hbus->state = hv_pcibus_removed; |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 2365 | } |
| 2366 | |
Dexuan Cui | 17978524 | 2016-11-10 07:18:47 +0000 | [diff] [blame] | 2367 | hv_pci_bus_exit(hdev); |
Vitaly Kuznetsov | deb22e5 | 2016-04-29 11:39:10 +0200 | [diff] [blame] | 2368 | |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 2369 | vmbus_close(hdev->channel); |
| 2370 | |
Jake Oshins | 4daace0 | 2016-02-16 21:56:23 +0000 | [diff] [blame] | 2371 | iounmap(hbus->cfg_addr); |
| 2372 | hv_free_config_window(hbus); |
| 2373 | pci_free_resource_list(&hbus->resources_for_children); |
| 2374 | hv_pci_free_bridge_windows(hbus); |
| 2375 | irq_domain_remove(hbus->irq_domain); |
| 2376 | irq_domain_free_fwnode(hbus->sysdata.fwnode); |
| 2377 | put_hvpcibus(hbus); |
| 2378 | wait_for_completion(&hbus->remove_event); |
| 2379 | kfree(hbus); |
| 2380 | return 0; |
| 2381 | } |
| 2382 | |
| 2383 | static const struct hv_vmbus_device_id hv_pci_id_table[] = { |
| 2384 | /* PCI Pass-through Class ID */ |
| 2385 | /* 44C4F61D-4444-4400-9D52-802E27EDE19F */ |
| 2386 | { HV_PCIE_GUID, }, |
| 2387 | { }, |
| 2388 | }; |
| 2389 | |
| 2390 | MODULE_DEVICE_TABLE(vmbus, hv_pci_id_table); |
| 2391 | |
| 2392 | static struct hv_driver hv_pci_drv = { |
| 2393 | .name = "hv_pci", |
| 2394 | .id_table = hv_pci_id_table, |
| 2395 | .probe = hv_pci_probe, |
| 2396 | .remove = hv_pci_remove, |
| 2397 | }; |
| 2398 | |
| 2399 | static void __exit exit_hv_pci_drv(void) |
| 2400 | { |
| 2401 | vmbus_driver_unregister(&hv_pci_drv); |
| 2402 | } |
| 2403 | |
| 2404 | static int __init init_hv_pci_drv(void) |
| 2405 | { |
| 2406 | return vmbus_driver_register(&hv_pci_drv); |
| 2407 | } |
| 2408 | |
| 2409 | module_init(init_hv_pci_drv); |
| 2410 | module_exit(exit_hv_pci_drv); |
| 2411 | |
| 2412 | MODULE_DESCRIPTION("Hyper-V PCI"); |
| 2413 | MODULE_LICENSE("GPL v2"); |