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