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