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