blob: 20ea2ee330b8776a2c22e8a113b3bec0404427d1 [file] [log] [blame]
Bjorn Helgaas8cfab3c2018-01-26 12:50:27 -06001// SPDX-License-Identifier: GPL-2.0
Jake Oshins4daace02016-02-16 21:56:23 +00002/*
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 Oshins4daace02016-02-16 21:56:23 +000038 */
39
40#include <linux/kernel.h>
41#include <linux/module.h>
42#include <linux/pci.h>
Boqun Feng88f94c72021-07-27 02:06:57 +080043#include <linux/pci-ecam.h>
Stephen Hemminger80bfeeb2017-07-31 16:48:29 -070044#include <linux/delay.h>
Jake Oshins4daace02016-02-16 21:56:23 +000045#include <linux/semaphore.h>
Nicolai Stange447ae312018-07-29 12:15:33 +020046#include <linux/irq.h>
Jake Oshins4daace02016-02-16 21:56:23 +000047#include <linux/msi.h>
48#include <linux/hyperv.h>
Elena Reshetova24196f02017-04-18 09:02:48 -050049#include <linux/refcount.h>
Sunil Muthuswamyd9932b42022-01-05 11:32:36 -080050#include <linux/irqdomain.h>
51#include <linux/acpi.h>
Jake Oshins4daace02016-02-16 21:56:23 +000052#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 Loeserb1db7e72017-05-24 13:41:27 -070059#define PCI_MAKE_VERSION(major, minor) ((u32)(((major) << 16) | (minor)))
Jake Oshins4daace02016-02-16 21:56:23 +000060#define PCI_MAJOR_VERSION(version) ((u32)(version) >> 16)
61#define PCI_MINOR_VERSION(version) ((u32)(version) & 0xff)
62
Jork Loeserb1db7e72017-05-24 13:41:27 -070063enum pci_protocol_version_t {
64 PCI_PROTOCOL_VERSION_1_1 = PCI_MAKE_VERSION(1, 1), /* Win10 */
Jork Loeser7dcf90e2017-05-24 13:41:28 -070065 PCI_PROTOCOL_VERSION_1_2 = PCI_MAKE_VERSION(1, 2), /* RS1 */
Long Li999dd952020-02-25 21:06:08 -080066 PCI_PROTOCOL_VERSION_1_3 = PCI_MAKE_VERSION(1, 3), /* Vibranium */
Sunil Muthuswamy8f6a6b32021-07-12 21:58:18 +000067 PCI_PROTOCOL_VERSION_1_4 = PCI_MAKE_VERSION(1, 4), /* WS2022 */
Jake Oshins4daace02016-02-16 21:56:23 +000068};
69
K. Y. Srinivasan433fcf62017-03-24 11:07:21 -070070#define CPU_AFFINITY_ALL -1ULL
Jork Loeserb1db7e72017-05-24 13:41:27 -070071
72/*
73 * Supported protocol versions in the order of probing - highest go
74 * first.
75 */
76static enum pci_protocol_version_t pci_protocol_versions[] = {
Sunil Muthuswamy8f6a6b32021-07-12 21:58:18 +000077 PCI_PROTOCOL_VERSION_1_4,
Long Li999dd952020-02-25 21:06:08 -080078 PCI_PROTOCOL_VERSION_1_3,
Jork Loeser7dcf90e2017-05-24 13:41:28 -070079 PCI_PROTOCOL_VERSION_1_2,
Jork Loeserb1db7e72017-05-24 13:41:27 -070080 PCI_PROTOCOL_VERSION_1_1,
81};
82
Jake Oshins4daace02016-02-16 21:56:23 +000083#define PCI_CONFIG_MMIO_LENGTH 0x2000
84#define CFG_PAGE_OFFSET 0x1000
85#define CFG_PAGE_SIZE (PCI_CONFIG_MMIO_LENGTH - CFG_PAGE_OFFSET)
86
87#define MAX_SUPPORTED_MSI_MESSAGES 0x400
88
Jork Loeserb1db7e72017-05-24 13:41:27 -070089#define STATUS_REVISION_MISMATCH 0xC0000059
90
Stephen Hemmingera15f2c082018-09-14 12:54:56 -070091/* space for 32bit serial number as string */
92#define SLOT_NAME_SIZE 11
93
Jake Oshins4daace02016-02-16 21:56:23 +000094/*
95 * Message Types
96 */
97
98enum pci_message_type {
99 /*
100 * Version 1.1
101 */
102 PCI_MESSAGE_BASE = 0x42490000,
103 PCI_BUS_RELATIONS = PCI_MESSAGE_BASE + 0,
104 PCI_QUERY_BUS_RELATIONS = PCI_MESSAGE_BASE + 1,
105 PCI_POWER_STATE_CHANGE = PCI_MESSAGE_BASE + 4,
106 PCI_QUERY_RESOURCE_REQUIREMENTS = PCI_MESSAGE_BASE + 5,
107 PCI_QUERY_RESOURCE_RESOURCES = PCI_MESSAGE_BASE + 6,
108 PCI_BUS_D0ENTRY = PCI_MESSAGE_BASE + 7,
109 PCI_BUS_D0EXIT = PCI_MESSAGE_BASE + 8,
110 PCI_READ_BLOCK = PCI_MESSAGE_BASE + 9,
111 PCI_WRITE_BLOCK = PCI_MESSAGE_BASE + 0xA,
112 PCI_EJECT = PCI_MESSAGE_BASE + 0xB,
113 PCI_QUERY_STOP = PCI_MESSAGE_BASE + 0xC,
114 PCI_REENABLE = PCI_MESSAGE_BASE + 0xD,
115 PCI_QUERY_STOP_FAILED = PCI_MESSAGE_BASE + 0xE,
116 PCI_EJECTION_COMPLETE = PCI_MESSAGE_BASE + 0xF,
117 PCI_RESOURCES_ASSIGNED = PCI_MESSAGE_BASE + 0x10,
118 PCI_RESOURCES_RELEASED = PCI_MESSAGE_BASE + 0x11,
119 PCI_INVALIDATE_BLOCK = PCI_MESSAGE_BASE + 0x12,
120 PCI_QUERY_PROTOCOL_VERSION = PCI_MESSAGE_BASE + 0x13,
121 PCI_CREATE_INTERRUPT_MESSAGE = PCI_MESSAGE_BASE + 0x14,
122 PCI_DELETE_INTERRUPT_MESSAGE = PCI_MESSAGE_BASE + 0x15,
Jork Loeser7dcf90e2017-05-24 13:41:28 -0700123 PCI_RESOURCES_ASSIGNED2 = PCI_MESSAGE_BASE + 0x16,
124 PCI_CREATE_INTERRUPT_MESSAGE2 = PCI_MESSAGE_BASE + 0x17,
125 PCI_DELETE_INTERRUPT_MESSAGE2 = PCI_MESSAGE_BASE + 0x18, /* unused */
Long Li999dd952020-02-25 21:06:08 -0800126 PCI_BUS_RELATIONS2 = PCI_MESSAGE_BASE + 0x19,
Sunil Muthuswamy8f6a6b32021-07-12 21:58:18 +0000127 PCI_RESOURCES_ASSIGNED3 = PCI_MESSAGE_BASE + 0x1A,
128 PCI_CREATE_INTERRUPT_MESSAGE3 = PCI_MESSAGE_BASE + 0x1B,
Jake Oshins4daace02016-02-16 21:56:23 +0000129 PCI_MESSAGE_MAXIMUM
130};
131
132/*
133 * Structures defining the virtual PCI Express protocol.
134 */
135
136union pci_version {
137 struct {
138 u16 minor_version;
139 u16 major_version;
140 } parts;
141 u32 version;
142} __packed;
143
144/*
145 * Function numbers are 8-bits wide on Express, as interpreted through ARI,
146 * which is all this driver does. This representation is the one used in
147 * Windows, which is what is expected when sending this back and forth with
148 * the Hyper-V parent partition.
149 */
150union win_slot_encoding {
151 struct {
Dexuan Cui60e2e2f2017-02-10 15:18:46 -0600152 u32 dev:5;
153 u32 func:3;
Jake Oshins4daace02016-02-16 21:56:23 +0000154 u32 reserved:24;
155 } bits;
156 u32 slot;
157} __packed;
158
159/*
160 * Pretty much as defined in the PCI Specifications.
161 */
162struct pci_function_description {
163 u16 v_id; /* vendor ID */
164 u16 d_id; /* device ID */
165 u8 rev;
166 u8 prog_intf;
167 u8 subclass;
168 u8 base_class;
169 u32 subsystem_id;
170 union win_slot_encoding win_slot;
171 u32 ser; /* serial number */
172} __packed;
173
Long Li999dd952020-02-25 21:06:08 -0800174enum pci_device_description_flags {
175 HV_PCI_DEVICE_FLAG_NONE = 0x0,
176 HV_PCI_DEVICE_FLAG_NUMA_AFFINITY = 0x1,
177};
178
179struct pci_function_description2 {
180 u16 v_id; /* vendor ID */
181 u16 d_id; /* device ID */
182 u8 rev;
183 u8 prog_intf;
184 u8 subclass;
185 u8 base_class;
186 u32 subsystem_id;
187 union win_slot_encoding win_slot;
188 u32 ser; /* serial number */
189 u32 flags;
190 u16 virtual_numa_node;
191 u16 reserved;
192} __packed;
193
Jake Oshins4daace02016-02-16 21:56:23 +0000194/**
195 * struct hv_msi_desc
196 * @vector: IDT entry
197 * @delivery_mode: As defined in Intel's Programmer's
198 * Reference Manual, Volume 3, Chapter 8.
199 * @vector_count: Number of contiguous entries in the
200 * Interrupt Descriptor Table that are
201 * occupied by this Message-Signaled
202 * Interrupt. For "MSI", as first defined
203 * in PCI 2.2, this can be between 1 and
204 * 32. For "MSI-X," as first defined in PCI
205 * 3.0, this must be 1, as each MSI-X table
206 * entry would have its own descriptor.
207 * @reserved: Empty space
208 * @cpu_mask: All the target virtual processors.
209 */
210struct hv_msi_desc {
211 u8 vector;
212 u8 delivery_mode;
213 u16 vector_count;
214 u32 reserved;
215 u64 cpu_mask;
216} __packed;
217
218/**
Jork Loeser7dcf90e2017-05-24 13:41:28 -0700219 * struct hv_msi_desc2 - 1.2 version of hv_msi_desc
220 * @vector: IDT entry
221 * @delivery_mode: As defined in Intel's Programmer's
222 * Reference Manual, Volume 3, Chapter 8.
223 * @vector_count: Number of contiguous entries in the
224 * Interrupt Descriptor Table that are
225 * occupied by this Message-Signaled
226 * Interrupt. For "MSI", as first defined
227 * in PCI 2.2, this can be between 1 and
228 * 32. For "MSI-X," as first defined in PCI
229 * 3.0, this must be 1, as each MSI-X table
230 * entry would have its own descriptor.
231 * @processor_count: number of bits enabled in array.
232 * @processor_array: All the target virtual processors.
233 */
234struct hv_msi_desc2 {
235 u8 vector;
236 u8 delivery_mode;
237 u16 vector_count;
238 u16 processor_count;
239 u16 processor_array[32];
240} __packed;
241
Sunil Muthuswamy8f6a6b32021-07-12 21:58:18 +0000242/*
243 * struct hv_msi_desc3 - 1.3 version of hv_msi_desc
244 * Everything is the same as in 'hv_msi_desc2' except that the size of the
245 * 'vector' field is larger to support bigger vector values. For ex: LPI
246 * vectors on ARM.
247 */
248struct hv_msi_desc3 {
249 u32 vector;
250 u8 delivery_mode;
251 u8 reserved;
252 u16 vector_count;
253 u16 processor_count;
254 u16 processor_array[32];
255} __packed;
256
Jork Loeser7dcf90e2017-05-24 13:41:28 -0700257/**
Jake Oshins4daace02016-02-16 21:56:23 +0000258 * struct tran_int_desc
259 * @reserved: unused, padding
260 * @vector_count: same as in hv_msi_desc
261 * @data: This is the "data payload" value that is
262 * written by the device when it generates
263 * a message-signaled interrupt, either MSI
264 * or MSI-X.
265 * @address: This is the address to which the data
266 * payload is written on interrupt
267 * generation.
268 */
269struct tran_int_desc {
270 u16 reserved;
271 u16 vector_count;
272 u32 data;
273 u64 address;
274} __packed;
275
276/*
277 * A generic message format for virtual PCI.
278 * Specific message formats are defined later in the file.
279 */
280
281struct pci_message {
Dexuan Cui0c6045d2016-08-23 04:45:51 +0000282 u32 type;
Jake Oshins4daace02016-02-16 21:56:23 +0000283} __packed;
284
285struct pci_child_message {
Dexuan Cui0c6045d2016-08-23 04:45:51 +0000286 struct pci_message message_type;
Jake Oshins4daace02016-02-16 21:56:23 +0000287 union win_slot_encoding wslot;
288} __packed;
289
290struct pci_incoming_message {
291 struct vmpacket_descriptor hdr;
292 struct pci_message message_type;
293} __packed;
294
295struct pci_response {
296 struct vmpacket_descriptor hdr;
297 s32 status; /* negative values are failures */
298} __packed;
299
300struct pci_packet {
301 void (*completion_func)(void *context, struct pci_response *resp,
302 int resp_packet_size);
303 void *compl_ctxt;
Dexuan Cui0c6045d2016-08-23 04:45:51 +0000304
Gustavo A. R. Silva067fb6c2020-02-12 18:50:48 -0600305 struct pci_message message[];
Jake Oshins4daace02016-02-16 21:56:23 +0000306};
307
308/*
309 * Specific message types supporting the PCI protocol.
310 */
311
312/*
313 * Version negotiation message. Sent from the guest to the host.
314 * The guest is free to try different versions until the host
315 * accepts the version.
316 *
317 * pci_version: The protocol version requested.
318 * is_last_attempt: If TRUE, this is the last version guest will request.
319 * reservedz: Reserved field, set to zero.
320 */
321
322struct pci_version_request {
323 struct pci_message message_type;
Jork Loeser691ac1d2017-05-24 13:41:24 -0700324 u32 protocol_version;
Jake Oshins4daace02016-02-16 21:56:23 +0000325} __packed;
326
327/*
328 * Bus D0 Entry. This is sent from the guest to the host when the virtual
329 * bus (PCI Express port) is ready for action.
330 */
331
332struct pci_bus_d0_entry {
333 struct pci_message message_type;
334 u32 reserved;
335 u64 mmio_base;
336} __packed;
337
338struct pci_bus_relations {
339 struct pci_incoming_message incoming;
340 u32 device_count;
Gustavo A. R. Silva067fb6c2020-02-12 18:50:48 -0600341 struct pci_function_description func[];
Jake Oshins4daace02016-02-16 21:56:23 +0000342} __packed;
343
Long Li999dd952020-02-25 21:06:08 -0800344struct pci_bus_relations2 {
345 struct pci_incoming_message incoming;
346 u32 device_count;
Gustavo A. R. Silva067fb6c2020-02-12 18:50:48 -0600347 struct pci_function_description2 func[];
Long Li999dd952020-02-25 21:06:08 -0800348} __packed;
349
Jake Oshins4daace02016-02-16 21:56:23 +0000350struct pci_q_res_req_response {
351 struct vmpacket_descriptor hdr;
352 s32 status; /* negative values are failures */
Denis Efremovc9c13ba2019-09-28 02:43:08 +0300353 u32 probed_bar[PCI_STD_NUM_BARS];
Jake Oshins4daace02016-02-16 21:56:23 +0000354} __packed;
355
356struct pci_set_power {
357 struct pci_message message_type;
358 union win_slot_encoding wslot;
359 u32 power_state; /* In Windows terms */
360 u32 reserved;
361} __packed;
362
363struct pci_set_power_response {
364 struct vmpacket_descriptor hdr;
365 s32 status; /* negative values are failures */
366 union win_slot_encoding wslot;
367 u32 resultant_state; /* In Windows terms */
368 u32 reserved;
369} __packed;
370
371struct pci_resources_assigned {
372 struct pci_message message_type;
373 union win_slot_encoding wslot;
374 u8 memory_range[0x14][6]; /* not used here */
375 u32 msi_descriptors;
376 u32 reserved[4];
377} __packed;
378
Jork Loeser7dcf90e2017-05-24 13:41:28 -0700379struct pci_resources_assigned2 {
380 struct pci_message message_type;
381 union win_slot_encoding wslot;
382 u8 memory_range[0x14][6]; /* not used here */
383 u32 msi_descriptor_count;
384 u8 reserved[70];
385} __packed;
386
Jake Oshins4daace02016-02-16 21:56:23 +0000387struct pci_create_interrupt {
388 struct pci_message message_type;
389 union win_slot_encoding wslot;
390 struct hv_msi_desc int_desc;
391} __packed;
392
393struct pci_create_int_response {
394 struct pci_response response;
395 u32 reserved;
396 struct tran_int_desc int_desc;
397} __packed;
398
Jork Loeser7dcf90e2017-05-24 13:41:28 -0700399struct pci_create_interrupt2 {
400 struct pci_message message_type;
401 union win_slot_encoding wslot;
402 struct hv_msi_desc2 int_desc;
403} __packed;
404
Sunil Muthuswamy8f6a6b32021-07-12 21:58:18 +0000405struct pci_create_interrupt3 {
406 struct pci_message message_type;
407 union win_slot_encoding wslot;
408 struct hv_msi_desc3 int_desc;
409} __packed;
410
Jake Oshins4daace02016-02-16 21:56:23 +0000411struct pci_delete_interrupt {
412 struct pci_message message_type;
413 union win_slot_encoding wslot;
414 struct tran_int_desc int_desc;
415} __packed;
416
Dexuan Cuie5d2f912019-08-22 05:05:37 +0000417/*
418 * Note: the VM must pass a valid block id, wslot and bytes_requested.
419 */
420struct pci_read_block {
421 struct pci_message message_type;
422 u32 block_id;
423 union win_slot_encoding wslot;
424 u32 bytes_requested;
425} __packed;
426
427struct pci_read_block_response {
428 struct vmpacket_descriptor hdr;
429 u32 status;
430 u8 bytes[HV_CONFIG_BLOCK_SIZE_MAX];
431} __packed;
432
433/*
434 * Note: the VM must pass a valid block id, wslot and byte_count.
435 */
436struct pci_write_block {
437 struct pci_message message_type;
438 u32 block_id;
439 union win_slot_encoding wslot;
440 u32 byte_count;
441 u8 bytes[HV_CONFIG_BLOCK_SIZE_MAX];
442} __packed;
443
444struct pci_dev_inval_block {
445 struct pci_incoming_message incoming;
446 union win_slot_encoding wslot;
447 u64 block_mask;
448} __packed;
449
Jake Oshins4daace02016-02-16 21:56:23 +0000450struct pci_dev_incoming {
451 struct pci_incoming_message incoming;
452 union win_slot_encoding wslot;
453} __packed;
454
455struct pci_eject_response {
Dexuan Cui0c6045d2016-08-23 04:45:51 +0000456 struct pci_message message_type;
Jake Oshins4daace02016-02-16 21:56:23 +0000457 union win_slot_encoding wslot;
458 u32 status;
459} __packed;
460
461static int pci_ring_size = (4 * PAGE_SIZE);
462
Jake Oshins4daace02016-02-16 21:56:23 +0000463/*
464 * Driver specific state.
465 */
466
467enum hv_pcibus_state {
468 hv_pcibus_init = 0,
469 hv_pcibus_probed,
470 hv_pcibus_installed,
Dexuan Cuiac82fc82019-11-24 21:33:52 -0800471 hv_pcibus_removing,
Jake Oshins4daace02016-02-16 21:56:23 +0000472 hv_pcibus_maximum
473};
474
475struct hv_pcibus_device {
Boqun Feng88f94c72021-07-27 02:06:57 +0800476#ifdef CONFIG_X86
Jake Oshins4daace02016-02-16 21:56:23 +0000477 struct pci_sysdata sysdata;
Boqun Feng88f94c72021-07-27 02:06:57 +0800478#elif defined(CONFIG_ARM64)
479 struct pci_config_window sysdata;
480#endif
Arnd Bergmann418cb6c2021-07-27 02:06:54 +0800481 struct pci_host_bridge *bridge;
Boqun Feng9e7f9172021-07-27 02:06:56 +0800482 struct fwnode_handle *fwnode;
Dexuan Cui14ef39f2019-11-24 21:33:53 -0800483 /* Protocol version negotiated with the host */
484 enum pci_protocol_version_t protocol_version;
Jake Oshins4daace02016-02-16 21:56:23 +0000485 enum hv_pcibus_state state;
Jake Oshins4daace02016-02-16 21:56:23 +0000486 struct hv_device *hdev;
487 resource_size_t low_mmio_space;
488 resource_size_t high_mmio_space;
489 struct resource *mem_config;
490 struct resource *low_mmio_res;
491 struct resource *high_mmio_res;
492 struct completion *survey_event;
Jake Oshins4daace02016-02-16 21:56:23 +0000493 struct pci_bus *pci_bus;
494 spinlock_t config_lock; /* Avoid two threads writing index page */
495 spinlock_t device_list_lock; /* Protect lists below */
496 void __iomem *cfg_addr;
497
Jake Oshins4daace02016-02-16 21:56:23 +0000498 struct list_head children;
499 struct list_head dr_list;
Jake Oshins4daace02016-02-16 21:56:23 +0000500
501 struct msi_domain_info msi_info;
Jake Oshins4daace02016-02-16 21:56:23 +0000502 struct irq_domain *irq_domain;
Jork Loeserbe66b672017-05-24 13:41:25 -0700503
Long Li0de8ce32016-11-08 14:04:38 -0800504 spinlock_t retarget_msi_interrupt_lock;
Dexuan Cui021ad272018-03-15 14:20:53 +0000505
506 struct workqueue_struct *wq;
Maya Nakamura9bc11742019-03-01 06:59:02 +0000507
Wei Hu83cc3502020-05-07 13:02:11 +0800508 /* Highest slot of child device with resources allocated */
509 int wslot_res_allocated;
510
Maya Nakamura9bc11742019-03-01 06:59:02 +0000511 /* hypercall arg, must not cross page boundary */
Boqun Feng61bfd922020-02-10 11:39:52 +0800512 struct hv_retarget_device_interrupt retarget_msi_interrupt_params;
Maya Nakamura9bc11742019-03-01 06:59:02 +0000513
514 /*
515 * Don't put anything here: retarget_msi_interrupt_params must be last
516 */
Jake Oshins4daace02016-02-16 21:56:23 +0000517};
518
519/*
520 * Tracks "Device Relations" messages from the host, which must be both
521 * processed in order and deferred so that they don't run in the context
522 * of the incoming packet callback.
523 */
524struct hv_dr_work {
525 struct work_struct wrk;
526 struct hv_pcibus_device *bus;
527};
528
Long Lif9ad0f32020-02-25 21:06:07 -0800529struct hv_pcidev_description {
530 u16 v_id; /* vendor ID */
531 u16 d_id; /* device ID */
532 u8 rev;
533 u8 prog_intf;
534 u8 subclass;
535 u8 base_class;
536 u32 subsystem_id;
537 union win_slot_encoding win_slot;
538 u32 ser; /* serial number */
539 u32 flags;
540 u16 virtual_numa_node;
541};
542
Jake Oshins4daace02016-02-16 21:56:23 +0000543struct hv_dr_state {
544 struct list_head list_entry;
545 u32 device_count;
Gustavo A. R. Silva067fb6c2020-02-12 18:50:48 -0600546 struct hv_pcidev_description func[];
Jake Oshins4daace02016-02-16 21:56:23 +0000547};
548
549enum hv_pcichild_state {
550 hv_pcichild_init = 0,
551 hv_pcichild_requirements,
552 hv_pcichild_resourced,
553 hv_pcichild_ejecting,
554 hv_pcichild_maximum
555};
556
Jake Oshins4daace02016-02-16 21:56:23 +0000557struct hv_pci_dev {
558 /* List protected by pci_rescan_remove_lock */
559 struct list_head list_entry;
Elena Reshetova24196f02017-04-18 09:02:48 -0500560 refcount_t refs;
Jake Oshins4daace02016-02-16 21:56:23 +0000561 enum hv_pcichild_state state;
Stephen Hemmingera15f2c082018-09-14 12:54:56 -0700562 struct pci_slot *pci_slot;
Long Lif9ad0f32020-02-25 21:06:07 -0800563 struct hv_pcidev_description desc;
Jake Oshins4daace02016-02-16 21:56:23 +0000564 bool reported_missing;
565 struct hv_pcibus_device *hbus;
566 struct work_struct wrk;
567
Dexuan Cuie5d2f912019-08-22 05:05:37 +0000568 void (*block_invalidate)(void *context, u64 block_mask);
569 void *invalidate_context;
570
Jake Oshins4daace02016-02-16 21:56:23 +0000571 /*
572 * What would be observed if one wrote 0xFFFFFFFF to a BAR and then
573 * read it back, for each of the BAR offsets within config space.
574 */
Denis Efremovc9c13ba2019-09-28 02:43:08 +0300575 u32 probed_bar[PCI_STD_NUM_BARS];
Jake Oshins4daace02016-02-16 21:56:23 +0000576};
577
578struct hv_pci_compl {
579 struct completion host_event;
580 s32 completion_status;
581};
582
Dexuan Cuide0aa7b2018-03-15 14:21:08 +0000583static void hv_pci_onchannelcallback(void *context);
584
Sunil Muthuswamy831c1ae2022-01-05 11:32:35 -0800585#ifdef CONFIG_X86
586#define DELIVERY_MODE APIC_DELIVERY_MODE_FIXED
587#define FLOW_HANDLER handle_edge_irq
588#define FLOW_NAME "edge"
589
590static int hv_pci_irqchip_init(void)
591{
592 return 0;
593}
594
595static struct irq_domain *hv_pci_get_root_domain(void)
596{
597 return x86_vector_domain;
598}
599
600static unsigned int hv_msi_get_int_vector(struct irq_data *data)
601{
602 struct irq_cfg *cfg = irqd_cfg(data);
603
604 return cfg->vector;
605}
606
607static void hv_set_msi_entry_from_desc(union hv_msi_entry *msi_entry,
608 struct msi_desc *msi_desc)
609{
610 msi_entry->address.as_uint32 = msi_desc->msg.address_lo;
611 msi_entry->data.as_uint32 = msi_desc->msg.data;
612}
613
614static int hv_msi_prepare(struct irq_domain *domain, struct device *dev,
615 int nvec, msi_alloc_info_t *info)
616{
617 return pci_msi_prepare(domain, dev, nvec, info);
618}
Sunil Muthuswamyd9932b42022-01-05 11:32:36 -0800619#elif defined(CONFIG_ARM64)
620/*
621 * SPI vectors to use for vPCI; arch SPIs range is [32, 1019], but leaving a bit
622 * of room at the start to allow for SPIs to be specified through ACPI and
623 * starting with a power of two to satisfy power of 2 multi-MSI requirement.
624 */
625#define HV_PCI_MSI_SPI_START 64
626#define HV_PCI_MSI_SPI_NR (1020 - HV_PCI_MSI_SPI_START)
627#define DELIVERY_MODE 0
628#define FLOW_HANDLER NULL
629#define FLOW_NAME NULL
630#define hv_msi_prepare NULL
631
632struct hv_pci_chip_data {
633 DECLARE_BITMAP(spi_map, HV_PCI_MSI_SPI_NR);
634 struct mutex map_lock;
635};
636
637/* Hyper-V vPCI MSI GIC IRQ domain */
638static struct irq_domain *hv_msi_gic_irq_domain;
639
640/* Hyper-V PCI MSI IRQ chip */
641static struct irq_chip hv_arm64_msi_irq_chip = {
642 .name = "MSI",
643 .irq_set_affinity = irq_chip_set_affinity_parent,
644 .irq_eoi = irq_chip_eoi_parent,
645 .irq_mask = irq_chip_mask_parent,
646 .irq_unmask = irq_chip_unmask_parent
647};
648
649static unsigned int hv_msi_get_int_vector(struct irq_data *irqd)
650{
651 return irqd->parent_data->hwirq;
652}
653
654static void hv_set_msi_entry_from_desc(union hv_msi_entry *msi_entry,
655 struct msi_desc *msi_desc)
656{
657 msi_entry->address = ((u64)msi_desc->msg.address_hi << 32) |
658 msi_desc->msg.address_lo;
659 msi_entry->data = msi_desc->msg.data;
660}
661
662/*
663 * @nr_bm_irqs: Indicates the number of IRQs that were allocated from
664 * the bitmap.
665 * @nr_dom_irqs: Indicates the number of IRQs that were allocated from
666 * the parent domain.
667 */
668static void hv_pci_vec_irq_free(struct irq_domain *domain,
669 unsigned int virq,
670 unsigned int nr_bm_irqs,
671 unsigned int nr_dom_irqs)
672{
673 struct hv_pci_chip_data *chip_data = domain->host_data;
674 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
675 int first = d->hwirq - HV_PCI_MSI_SPI_START;
676 int i;
677
678 mutex_lock(&chip_data->map_lock);
679 bitmap_release_region(chip_data->spi_map,
680 first,
681 get_count_order(nr_bm_irqs));
682 mutex_unlock(&chip_data->map_lock);
683 for (i = 0; i < nr_dom_irqs; i++) {
684 if (i)
685 d = irq_domain_get_irq_data(domain, virq + i);
686 irq_domain_reset_irq_data(d);
687 }
688
689 irq_domain_free_irqs_parent(domain, virq, nr_dom_irqs);
690}
691
692static void hv_pci_vec_irq_domain_free(struct irq_domain *domain,
693 unsigned int virq,
694 unsigned int nr_irqs)
695{
696 hv_pci_vec_irq_free(domain, virq, nr_irqs, nr_irqs);
697}
698
699static int hv_pci_vec_alloc_device_irq(struct irq_domain *domain,
700 unsigned int nr_irqs,
701 irq_hw_number_t *hwirq)
702{
703 struct hv_pci_chip_data *chip_data = domain->host_data;
704 int index;
705
706 /* Find and allocate region from the SPI bitmap */
707 mutex_lock(&chip_data->map_lock);
708 index = bitmap_find_free_region(chip_data->spi_map,
709 HV_PCI_MSI_SPI_NR,
710 get_count_order(nr_irqs));
711 mutex_unlock(&chip_data->map_lock);
712 if (index < 0)
713 return -ENOSPC;
714
715 *hwirq = index + HV_PCI_MSI_SPI_START;
716
717 return 0;
718}
719
720static int hv_pci_vec_irq_gic_domain_alloc(struct irq_domain *domain,
721 unsigned int virq,
722 irq_hw_number_t hwirq)
723{
724 struct irq_fwspec fwspec;
725 struct irq_data *d;
726 int ret;
727
728 fwspec.fwnode = domain->parent->fwnode;
729 fwspec.param_count = 2;
730 fwspec.param[0] = hwirq;
731 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
732
733 ret = irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
734 if (ret)
735 return ret;
736
737 /*
738 * Since the interrupt specifier is not coming from ACPI or DT, the
739 * trigger type will need to be set explicitly. Otherwise, it will be
740 * set to whatever is in the GIC configuration.
741 */
742 d = irq_domain_get_irq_data(domain->parent, virq);
743
744 return d->chip->irq_set_type(d, IRQ_TYPE_EDGE_RISING);
745}
746
747static int hv_pci_vec_irq_domain_alloc(struct irq_domain *domain,
748 unsigned int virq, unsigned int nr_irqs,
749 void *args)
750{
751 irq_hw_number_t hwirq;
752 unsigned int i;
753 int ret;
754
755 ret = hv_pci_vec_alloc_device_irq(domain, nr_irqs, &hwirq);
756 if (ret)
757 return ret;
758
759 for (i = 0; i < nr_irqs; i++) {
760 ret = hv_pci_vec_irq_gic_domain_alloc(domain, virq + i,
761 hwirq + i);
762 if (ret) {
763 hv_pci_vec_irq_free(domain, virq, nr_irqs, i);
764 return ret;
765 }
766
767 irq_domain_set_hwirq_and_chip(domain, virq + i,
768 hwirq + i,
769 &hv_arm64_msi_irq_chip,
770 domain->host_data);
771 pr_debug("pID:%d vID:%u\n", (int)(hwirq + i), virq + i);
772 }
773
774 return 0;
775}
776
777/*
778 * Pick the first cpu as the irq affinity that can be temporarily used for
779 * composing MSI from the hypervisor. GIC will eventually set the right
780 * affinity for the irq and the 'unmask' will retarget the interrupt to that
781 * cpu.
782 */
783static int hv_pci_vec_irq_domain_activate(struct irq_domain *domain,
784 struct irq_data *irqd, bool reserve)
785{
786 int cpu = cpumask_first(cpu_present_mask);
787
788 irq_data_update_effective_affinity(irqd, cpumask_of(cpu));
789
790 return 0;
791}
792
793static const struct irq_domain_ops hv_pci_domain_ops = {
794 .alloc = hv_pci_vec_irq_domain_alloc,
795 .free = hv_pci_vec_irq_domain_free,
796 .activate = hv_pci_vec_irq_domain_activate,
797};
798
799static int hv_pci_irqchip_init(void)
800{
801 static struct hv_pci_chip_data *chip_data;
802 struct fwnode_handle *fn = NULL;
803 int ret = -ENOMEM;
804
805 chip_data = kzalloc(sizeof(*chip_data), GFP_KERNEL);
806 if (!chip_data)
807 return ret;
808
809 mutex_init(&chip_data->map_lock);
810 fn = irq_domain_alloc_named_fwnode("hv_vpci_arm64");
811 if (!fn)
812 goto free_chip;
813
814 /*
815 * IRQ domain once enabled, should not be removed since there is no
816 * way to ensure that all the corresponding devices are also gone and
817 * no interrupts will be generated.
818 */
819 hv_msi_gic_irq_domain = acpi_irq_create_hierarchy(0, HV_PCI_MSI_SPI_NR,
820 fn, &hv_pci_domain_ops,
821 chip_data);
822
823 if (!hv_msi_gic_irq_domain) {
824 pr_err("Failed to create Hyper-V arm64 vPCI MSI IRQ domain\n");
825 goto free_chip;
826 }
827
828 return 0;
829
830free_chip:
831 kfree(chip_data);
832 if (fn)
833 irq_domain_free_fwnode(fn);
834
835 return ret;
836}
837
838static struct irq_domain *hv_pci_get_root_domain(void)
839{
840 return hv_msi_gic_irq_domain;
841}
842#endif /* CONFIG_ARM64 */
Sunil Muthuswamy831c1ae2022-01-05 11:32:35 -0800843
Jake Oshins4daace02016-02-16 21:56:23 +0000844/**
845 * hv_pci_generic_compl() - Invoked for a completion packet
846 * @context: Set up by the sender of the packet.
847 * @resp: The response packet
848 * @resp_packet_size: Size in bytes of the packet
849 *
850 * This function is used to trigger an event and report status
851 * for any message for which the completion packet contains a
852 * status and nothing else.
853 */
Dexuan Cuia5b45b72016-08-23 04:49:22 +0000854static void hv_pci_generic_compl(void *context, struct pci_response *resp,
855 int resp_packet_size)
Jake Oshins4daace02016-02-16 21:56:23 +0000856{
857 struct hv_pci_compl *comp_pkt = context;
858
859 if (resp_packet_size >= offsetofend(struct pci_response, status))
860 comp_pkt->completion_status = resp->status;
Dexuan Cuia5b45b72016-08-23 04:49:22 +0000861 else
862 comp_pkt->completion_status = -1;
863
Jake Oshins4daace02016-02-16 21:56:23 +0000864 complete(&comp_pkt->host_event);
865}
866
867static struct hv_pci_dev *get_pcichild_wslot(struct hv_pcibus_device *hbus,
868 u32 wslot);
Stephen Hemminger8c99e122018-05-23 10:11:12 -0700869
870static void get_pcichild(struct hv_pci_dev *hpdev)
871{
872 refcount_inc(&hpdev->refs);
873}
874
875static void put_pcichild(struct hv_pci_dev *hpdev)
876{
877 if (refcount_dec_and_test(&hpdev->refs))
878 kfree(hpdev);
879}
Jake Oshins4daace02016-02-16 21:56:23 +0000880
Dexuan Cuic3635da2018-05-23 21:12:01 +0000881/*
882 * There is no good way to get notified from vmbus_onoffer_rescind(),
883 * so let's use polling here, since this is not a hot path.
884 */
885static int wait_for_response(struct hv_device *hdev,
886 struct completion *comp)
887{
888 while (true) {
889 if (hdev->channel->rescind) {
890 dev_warn_once(&hdev->device, "The device is gone.\n");
891 return -ENODEV;
892 }
893
894 if (wait_for_completion_timeout(comp, HZ / 10))
895 break;
896 }
897
898 return 0;
899}
900
Jake Oshins4daace02016-02-16 21:56:23 +0000901/**
902 * devfn_to_wslot() - Convert from Linux PCI slot to Windows
903 * @devfn: The Linux representation of PCI slot
904 *
905 * Windows uses a slightly different representation of PCI slot.
906 *
907 * Return: The Windows representation
908 */
909static u32 devfn_to_wslot(int devfn)
910{
911 union win_slot_encoding wslot;
912
913 wslot.slot = 0;
Dexuan Cui60e2e2f2017-02-10 15:18:46 -0600914 wslot.bits.dev = PCI_SLOT(devfn);
915 wslot.bits.func = PCI_FUNC(devfn);
Jake Oshins4daace02016-02-16 21:56:23 +0000916
917 return wslot.slot;
918}
919
920/**
921 * wslot_to_devfn() - Convert from Windows PCI slot to Linux
922 * @wslot: The Windows representation of PCI slot
923 *
924 * Windows uses a slightly different representation of PCI slot.
925 *
926 * Return: The Linux representation
927 */
928static int wslot_to_devfn(u32 wslot)
929{
930 union win_slot_encoding slot_no;
931
932 slot_no.slot = wslot;
Dexuan Cui60e2e2f2017-02-10 15:18:46 -0600933 return PCI_DEVFN(slot_no.bits.dev, slot_no.bits.func);
Jake Oshins4daace02016-02-16 21:56:23 +0000934}
935
936/*
937 * PCI Configuration Space for these root PCI buses is implemented as a pair
938 * of pages in memory-mapped I/O space. Writing to the first page chooses
939 * the PCI function being written or read. Once the first page has been
940 * written to, the following page maps in the entire configuration space of
941 * the function.
942 */
943
944/**
945 * _hv_pcifront_read_config() - Internal PCI config read
946 * @hpdev: The PCI driver's representation of the device
947 * @where: Offset within config space
948 * @size: Size of the transfer
949 * @val: Pointer to the buffer receiving the data
950 */
951static void _hv_pcifront_read_config(struct hv_pci_dev *hpdev, int where,
952 int size, u32 *val)
953{
954 unsigned long flags;
955 void __iomem *addr = hpdev->hbus->cfg_addr + CFG_PAGE_OFFSET + where;
956
957 /*
958 * If the attempt is to read the IDs or the ROM BAR, simulate that.
959 */
960 if (where + size <= PCI_COMMAND) {
961 memcpy(val, ((u8 *)&hpdev->desc.v_id) + where, size);
962 } else if (where >= PCI_CLASS_REVISION && where + size <=
963 PCI_CACHE_LINE_SIZE) {
964 memcpy(val, ((u8 *)&hpdev->desc.rev) + where -
965 PCI_CLASS_REVISION, size);
966 } else if (where >= PCI_SUBSYSTEM_VENDOR_ID && where + size <=
967 PCI_ROM_ADDRESS) {
968 memcpy(val, (u8 *)&hpdev->desc.subsystem_id + where -
969 PCI_SUBSYSTEM_VENDOR_ID, size);
970 } else if (where >= PCI_ROM_ADDRESS && where + size <=
971 PCI_CAPABILITY_LIST) {
972 /* ROM BARs are unimplemented */
973 *val = 0;
974 } else if (where >= PCI_INTERRUPT_LINE && where + size <=
975 PCI_INTERRUPT_PIN) {
976 /*
977 * Interrupt Line and Interrupt PIN are hard-wired to zero
978 * because this front-end only supports message-signaled
979 * interrupts.
980 */
981 *val = 0;
982 } else if (where + size <= CFG_PAGE_SIZE) {
983 spin_lock_irqsave(&hpdev->hbus->config_lock, flags);
984 /* Choose the function to be read. (See comment above) */
985 writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr);
Vitaly Kuznetsovbdd74442016-05-03 14:22:00 +0200986 /* Make sure the function was chosen before we start reading. */
987 mb();
Jake Oshins4daace02016-02-16 21:56:23 +0000988 /* Read from that function's config space. */
989 switch (size) {
990 case 1:
991 *val = readb(addr);
992 break;
993 case 2:
994 *val = readw(addr);
995 break;
996 default:
997 *val = readl(addr);
998 break;
999 }
Vitaly Kuznetsovbdd74442016-05-03 14:22:00 +02001000 /*
Dexuan Cuidf3f2152018-03-15 14:21:35 +00001001 * Make sure the read was done before we release the spinlock
Vitaly Kuznetsovbdd74442016-05-03 14:22:00 +02001002 * allowing consecutive reads/writes.
1003 */
1004 mb();
Jake Oshins4daace02016-02-16 21:56:23 +00001005 spin_unlock_irqrestore(&hpdev->hbus->config_lock, flags);
1006 } else {
1007 dev_err(&hpdev->hbus->hdev->device,
1008 "Attempt to read beyond a function's config space.\n");
1009 }
1010}
1011
Dexuan Cuide0aa7b2018-03-15 14:21:08 +00001012static u16 hv_pcifront_get_vendor_id(struct hv_pci_dev *hpdev)
1013{
1014 u16 ret;
1015 unsigned long flags;
1016 void __iomem *addr = hpdev->hbus->cfg_addr + CFG_PAGE_OFFSET +
1017 PCI_VENDOR_ID;
1018
1019 spin_lock_irqsave(&hpdev->hbus->config_lock, flags);
1020
1021 /* Choose the function to be read. (See comment above) */
1022 writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr);
1023 /* Make sure the function was chosen before we start reading. */
1024 mb();
1025 /* Read from that function's config space. */
1026 ret = readw(addr);
1027 /*
1028 * mb() is not required here, because the spin_unlock_irqrestore()
1029 * is a barrier.
1030 */
1031
1032 spin_unlock_irqrestore(&hpdev->hbus->config_lock, flags);
1033
1034 return ret;
1035}
1036
Jake Oshins4daace02016-02-16 21:56:23 +00001037/**
1038 * _hv_pcifront_write_config() - Internal PCI config write
1039 * @hpdev: The PCI driver's representation of the device
1040 * @where: Offset within config space
1041 * @size: Size of the transfer
1042 * @val: The data being transferred
1043 */
1044static void _hv_pcifront_write_config(struct hv_pci_dev *hpdev, int where,
1045 int size, u32 val)
1046{
1047 unsigned long flags;
1048 void __iomem *addr = hpdev->hbus->cfg_addr + CFG_PAGE_OFFSET + where;
1049
1050 if (where >= PCI_SUBSYSTEM_VENDOR_ID &&
1051 where + size <= PCI_CAPABILITY_LIST) {
1052 /* SSIDs and ROM BARs are read-only */
1053 } else if (where >= PCI_COMMAND && where + size <= CFG_PAGE_SIZE) {
1054 spin_lock_irqsave(&hpdev->hbus->config_lock, flags);
1055 /* Choose the function to be written. (See comment above) */
1056 writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr);
Vitaly Kuznetsovbdd74442016-05-03 14:22:00 +02001057 /* Make sure the function was chosen before we start writing. */
1058 wmb();
Jake Oshins4daace02016-02-16 21:56:23 +00001059 /* Write to that function's config space. */
1060 switch (size) {
1061 case 1:
1062 writeb(val, addr);
1063 break;
1064 case 2:
1065 writew(val, addr);
1066 break;
1067 default:
1068 writel(val, addr);
1069 break;
1070 }
Vitaly Kuznetsovbdd74442016-05-03 14:22:00 +02001071 /*
1072 * Make sure the write was done before we release the spinlock
1073 * allowing consecutive reads/writes.
1074 */
1075 mb();
Jake Oshins4daace02016-02-16 21:56:23 +00001076 spin_unlock_irqrestore(&hpdev->hbus->config_lock, flags);
1077 } else {
1078 dev_err(&hpdev->hbus->hdev->device,
1079 "Attempt to write beyond a function's config space.\n");
1080 }
1081}
1082
1083/**
1084 * hv_pcifront_read_config() - Read configuration space
1085 * @bus: PCI Bus structure
1086 * @devfn: Device/function
1087 * @where: Offset from base
1088 * @size: Byte/word/dword
1089 * @val: Value to be read
1090 *
1091 * Return: PCIBIOS_SUCCESSFUL on success
1092 * PCIBIOS_DEVICE_NOT_FOUND on failure
1093 */
1094static int hv_pcifront_read_config(struct pci_bus *bus, unsigned int devfn,
1095 int where, int size, u32 *val)
1096{
1097 struct hv_pcibus_device *hbus =
1098 container_of(bus->sysdata, struct hv_pcibus_device, sysdata);
1099 struct hv_pci_dev *hpdev;
1100
1101 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(devfn));
1102 if (!hpdev)
1103 return PCIBIOS_DEVICE_NOT_FOUND;
1104
1105 _hv_pcifront_read_config(hpdev, where, size, val);
1106
Stephen Hemminger8c99e122018-05-23 10:11:12 -07001107 put_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +00001108 return PCIBIOS_SUCCESSFUL;
1109}
1110
1111/**
1112 * hv_pcifront_write_config() - Write configuration space
1113 * @bus: PCI Bus structure
1114 * @devfn: Device/function
1115 * @where: Offset from base
1116 * @size: Byte/word/dword
1117 * @val: Value to be written to device
1118 *
1119 * Return: PCIBIOS_SUCCESSFUL on success
1120 * PCIBIOS_DEVICE_NOT_FOUND on failure
1121 */
1122static int hv_pcifront_write_config(struct pci_bus *bus, unsigned int devfn,
1123 int where, int size, u32 val)
1124{
1125 struct hv_pcibus_device *hbus =
1126 container_of(bus->sysdata, struct hv_pcibus_device, sysdata);
1127 struct hv_pci_dev *hpdev;
1128
1129 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(devfn));
1130 if (!hpdev)
1131 return PCIBIOS_DEVICE_NOT_FOUND;
1132
1133 _hv_pcifront_write_config(hpdev, where, size, val);
1134
Stephen Hemminger8c99e122018-05-23 10:11:12 -07001135 put_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +00001136 return PCIBIOS_SUCCESSFUL;
1137}
1138
1139/* PCIe operations */
1140static struct pci_ops hv_pcifront_ops = {
1141 .read = hv_pcifront_read_config,
1142 .write = hv_pcifront_write_config,
1143};
1144
Dexuan Cuie5d2f912019-08-22 05:05:37 +00001145/*
1146 * Paravirtual backchannel
1147 *
1148 * Hyper-V SR-IOV provides a backchannel mechanism in software for
1149 * communication between a VF driver and a PF driver. These
1150 * "configuration blocks" are similar in concept to PCI configuration space,
1151 * but instead of doing reads and writes in 32-bit chunks through a very slow
1152 * path, packets of up to 128 bytes can be sent or received asynchronously.
1153 *
1154 * Nearly every SR-IOV device contains just such a communications channel in
1155 * hardware, so using this one in software is usually optional. Using the
1156 * software channel, however, allows driver implementers to leverage software
1157 * tools that fuzz the communications channel looking for vulnerabilities.
1158 *
1159 * The usage model for these packets puts the responsibility for reading or
1160 * writing on the VF driver. The VF driver sends a read or a write packet,
1161 * indicating which "block" is being referred to by number.
1162 *
1163 * If the PF driver wishes to initiate communication, it can "invalidate" one or
1164 * more of the first 64 blocks. This invalidation is delivered via a callback
1165 * supplied by the VF driver by this driver.
1166 *
1167 * No protocol is implied, except that supplied by the PF and VF drivers.
1168 */
1169
1170struct hv_read_config_compl {
1171 struct hv_pci_compl comp_pkt;
1172 void *buf;
1173 unsigned int len;
1174 unsigned int bytes_returned;
1175};
1176
1177/**
1178 * hv_pci_read_config_compl() - Invoked when a response packet
1179 * for a read config block operation arrives.
1180 * @context: Identifies the read config operation
1181 * @resp: The response packet itself
1182 * @resp_packet_size: Size in bytes of the response packet
1183 */
1184static void hv_pci_read_config_compl(void *context, struct pci_response *resp,
1185 int resp_packet_size)
1186{
1187 struct hv_read_config_compl *comp = context;
1188 struct pci_read_block_response *read_resp =
1189 (struct pci_read_block_response *)resp;
1190 unsigned int data_len, hdr_len;
1191
1192 hdr_len = offsetof(struct pci_read_block_response, bytes);
1193 if (resp_packet_size < hdr_len) {
1194 comp->comp_pkt.completion_status = -1;
1195 goto out;
1196 }
1197
1198 data_len = resp_packet_size - hdr_len;
1199 if (data_len > 0 && read_resp->status == 0) {
1200 comp->bytes_returned = min(comp->len, data_len);
1201 memcpy(comp->buf, read_resp->bytes, comp->bytes_returned);
1202 } else {
1203 comp->bytes_returned = 0;
1204 }
1205
1206 comp->comp_pkt.completion_status = read_resp->status;
1207out:
1208 complete(&comp->comp_pkt.host_event);
1209}
1210
1211/**
1212 * hv_read_config_block() - Sends a read config block request to
1213 * the back-end driver running in the Hyper-V parent partition.
1214 * @pdev: The PCI driver's representation for this device.
1215 * @buf: Buffer into which the config block will be copied.
1216 * @len: Size in bytes of buf.
1217 * @block_id: Identifies the config block which has been requested.
1218 * @bytes_returned: Size which came back from the back-end driver.
1219 *
1220 * Return: 0 on success, -errno on failure
1221 */
Wei Yongjuna459d9e2020-07-06 21:52:34 +08001222static int hv_read_config_block(struct pci_dev *pdev, void *buf,
1223 unsigned int len, unsigned int block_id,
1224 unsigned int *bytes_returned)
Dexuan Cuie5d2f912019-08-22 05:05:37 +00001225{
1226 struct hv_pcibus_device *hbus =
1227 container_of(pdev->bus->sysdata, struct hv_pcibus_device,
1228 sysdata);
1229 struct {
1230 struct pci_packet pkt;
1231 char buf[sizeof(struct pci_read_block)];
1232 } pkt;
1233 struct hv_read_config_compl comp_pkt;
1234 struct pci_read_block *read_blk;
1235 int ret;
1236
1237 if (len == 0 || len > HV_CONFIG_BLOCK_SIZE_MAX)
1238 return -EINVAL;
1239
1240 init_completion(&comp_pkt.comp_pkt.host_event);
1241 comp_pkt.buf = buf;
1242 comp_pkt.len = len;
1243
1244 memset(&pkt, 0, sizeof(pkt));
1245 pkt.pkt.completion_func = hv_pci_read_config_compl;
1246 pkt.pkt.compl_ctxt = &comp_pkt;
1247 read_blk = (struct pci_read_block *)&pkt.pkt.message;
1248 read_blk->message_type.type = PCI_READ_BLOCK;
1249 read_blk->wslot.slot = devfn_to_wslot(pdev->devfn);
1250 read_blk->block_id = block_id;
1251 read_blk->bytes_requested = len;
1252
1253 ret = vmbus_sendpacket(hbus->hdev->channel, read_blk,
1254 sizeof(*read_blk), (unsigned long)&pkt.pkt,
1255 VM_PKT_DATA_INBAND,
1256 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1257 if (ret)
1258 return ret;
1259
1260 ret = wait_for_response(hbus->hdev, &comp_pkt.comp_pkt.host_event);
1261 if (ret)
1262 return ret;
1263
1264 if (comp_pkt.comp_pkt.completion_status != 0 ||
1265 comp_pkt.bytes_returned == 0) {
1266 dev_err(&hbus->hdev->device,
1267 "Read Config Block failed: 0x%x, bytes_returned=%d\n",
1268 comp_pkt.comp_pkt.completion_status,
1269 comp_pkt.bytes_returned);
1270 return -EIO;
1271 }
1272
1273 *bytes_returned = comp_pkt.bytes_returned;
1274 return 0;
1275}
Dexuan Cuie5d2f912019-08-22 05:05:37 +00001276
1277/**
1278 * hv_pci_write_config_compl() - Invoked when a response packet for a write
1279 * config block operation arrives.
1280 * @context: Identifies the write config operation
1281 * @resp: The response packet itself
1282 * @resp_packet_size: Size in bytes of the response packet
1283 */
1284static void hv_pci_write_config_compl(void *context, struct pci_response *resp,
1285 int resp_packet_size)
1286{
1287 struct hv_pci_compl *comp_pkt = context;
1288
1289 comp_pkt->completion_status = resp->status;
1290 complete(&comp_pkt->host_event);
1291}
1292
1293/**
1294 * hv_write_config_block() - Sends a write config block request to the
1295 * back-end driver running in the Hyper-V parent partition.
1296 * @pdev: The PCI driver's representation for this device.
1297 * @buf: Buffer from which the config block will be copied.
1298 * @len: Size in bytes of buf.
1299 * @block_id: Identifies the config block which is being written.
1300 *
1301 * Return: 0 on success, -errno on failure
1302 */
Wei Yongjuna459d9e2020-07-06 21:52:34 +08001303static int hv_write_config_block(struct pci_dev *pdev, void *buf,
1304 unsigned int len, unsigned int block_id)
Dexuan Cuie5d2f912019-08-22 05:05:37 +00001305{
1306 struct hv_pcibus_device *hbus =
1307 container_of(pdev->bus->sysdata, struct hv_pcibus_device,
1308 sysdata);
1309 struct {
1310 struct pci_packet pkt;
1311 char buf[sizeof(struct pci_write_block)];
1312 u32 reserved;
1313 } pkt;
1314 struct hv_pci_compl comp_pkt;
1315 struct pci_write_block *write_blk;
1316 u32 pkt_size;
1317 int ret;
1318
1319 if (len == 0 || len > HV_CONFIG_BLOCK_SIZE_MAX)
1320 return -EINVAL;
1321
1322 init_completion(&comp_pkt.host_event);
1323
1324 memset(&pkt, 0, sizeof(pkt));
1325 pkt.pkt.completion_func = hv_pci_write_config_compl;
1326 pkt.pkt.compl_ctxt = &comp_pkt;
1327 write_blk = (struct pci_write_block *)&pkt.pkt.message;
1328 write_blk->message_type.type = PCI_WRITE_BLOCK;
1329 write_blk->wslot.slot = devfn_to_wslot(pdev->devfn);
1330 write_blk->block_id = block_id;
1331 write_blk->byte_count = len;
1332 memcpy(write_blk->bytes, buf, len);
1333 pkt_size = offsetof(struct pci_write_block, bytes) + len;
1334 /*
1335 * This quirk is required on some hosts shipped around 2018, because
1336 * these hosts don't check the pkt_size correctly (new hosts have been
1337 * fixed since early 2019). The quirk is also safe on very old hosts
1338 * and new hosts, because, on them, what really matters is the length
1339 * specified in write_blk->byte_count.
1340 */
1341 pkt_size += sizeof(pkt.reserved);
1342
1343 ret = vmbus_sendpacket(hbus->hdev->channel, write_blk, pkt_size,
1344 (unsigned long)&pkt.pkt, VM_PKT_DATA_INBAND,
1345 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1346 if (ret)
1347 return ret;
1348
1349 ret = wait_for_response(hbus->hdev, &comp_pkt.host_event);
1350 if (ret)
1351 return ret;
1352
1353 if (comp_pkt.completion_status != 0) {
1354 dev_err(&hbus->hdev->device,
1355 "Write Config Block failed: 0x%x\n",
1356 comp_pkt.completion_status);
1357 return -EIO;
1358 }
1359
1360 return 0;
1361}
Dexuan Cuie5d2f912019-08-22 05:05:37 +00001362
1363/**
1364 * hv_register_block_invalidate() - Invoked when a config block invalidation
1365 * arrives from the back-end driver.
1366 * @pdev: The PCI driver's representation for this device.
1367 * @context: Identifies the device.
1368 * @block_invalidate: Identifies all of the blocks being invalidated.
1369 *
1370 * Return: 0 on success, -errno on failure
1371 */
Wei Yongjuna459d9e2020-07-06 21:52:34 +08001372static int hv_register_block_invalidate(struct pci_dev *pdev, void *context,
1373 void (*block_invalidate)(void *context,
1374 u64 block_mask))
Dexuan Cuie5d2f912019-08-22 05:05:37 +00001375{
1376 struct hv_pcibus_device *hbus =
1377 container_of(pdev->bus->sysdata, struct hv_pcibus_device,
1378 sysdata);
1379 struct hv_pci_dev *hpdev;
1380
1381 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
1382 if (!hpdev)
1383 return -ENODEV;
1384
1385 hpdev->block_invalidate = block_invalidate;
1386 hpdev->invalidate_context = context;
1387
1388 put_pcichild(hpdev);
1389 return 0;
1390
1391}
Dexuan Cuie5d2f912019-08-22 05:05:37 +00001392
Jake Oshins4daace02016-02-16 21:56:23 +00001393/* Interrupt management hooks */
1394static void hv_int_desc_free(struct hv_pci_dev *hpdev,
1395 struct tran_int_desc *int_desc)
1396{
1397 struct pci_delete_interrupt *int_pkt;
1398 struct {
1399 struct pci_packet pkt;
Dexuan Cui0c6045d2016-08-23 04:45:51 +00001400 u8 buffer[sizeof(struct pci_delete_interrupt)];
Jake Oshins4daace02016-02-16 21:56:23 +00001401 } ctxt;
1402
1403 memset(&ctxt, 0, sizeof(ctxt));
1404 int_pkt = (struct pci_delete_interrupt *)&ctxt.pkt.message;
Dexuan Cui0c6045d2016-08-23 04:45:51 +00001405 int_pkt->message_type.type =
Jake Oshins4daace02016-02-16 21:56:23 +00001406 PCI_DELETE_INTERRUPT_MESSAGE;
1407 int_pkt->wslot.slot = hpdev->desc.win_slot.slot;
1408 int_pkt->int_desc = *int_desc;
1409 vmbus_sendpacket(hpdev->hbus->hdev->channel, int_pkt, sizeof(*int_pkt),
1410 (unsigned long)&ctxt.pkt, VM_PKT_DATA_INBAND, 0);
1411 kfree(int_desc);
1412}
1413
1414/**
1415 * hv_msi_free() - Free the MSI.
1416 * @domain: The interrupt domain pointer
1417 * @info: Extra MSI-related context
1418 * @irq: Identifies the IRQ.
1419 *
1420 * The Hyper-V parent partition and hypervisor are tracking the
1421 * messages that are in use, keeping the interrupt redirection
1422 * table up to date. This callback sends a message that frees
1423 * the IRT entry and related tracking nonsense.
1424 */
1425static void hv_msi_free(struct irq_domain *domain, struct msi_domain_info *info,
1426 unsigned int irq)
1427{
1428 struct hv_pcibus_device *hbus;
1429 struct hv_pci_dev *hpdev;
1430 struct pci_dev *pdev;
1431 struct tran_int_desc *int_desc;
1432 struct irq_data *irq_data = irq_domain_get_irq_data(domain, irq);
1433 struct msi_desc *msi = irq_data_get_msi_desc(irq_data);
1434
1435 pdev = msi_desc_to_pci_dev(msi);
1436 hbus = info->data;
Cathy Avery0c6e6172016-07-12 11:31:24 -04001437 int_desc = irq_data_get_irq_chip_data(irq_data);
1438 if (!int_desc)
Jake Oshins4daace02016-02-16 21:56:23 +00001439 return;
1440
Cathy Avery0c6e6172016-07-12 11:31:24 -04001441 irq_data->chip_data = NULL;
1442 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
1443 if (!hpdev) {
1444 kfree(int_desc);
1445 return;
Jake Oshins4daace02016-02-16 21:56:23 +00001446 }
1447
Cathy Avery0c6e6172016-07-12 11:31:24 -04001448 hv_int_desc_free(hpdev, int_desc);
Stephen Hemminger8c99e122018-05-23 10:11:12 -07001449 put_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +00001450}
1451
Tobias Klauser542ccf42016-10-31 12:04:09 +01001452static void hv_irq_mask(struct irq_data *data)
Jake Oshins4daace02016-02-16 21:56:23 +00001453{
1454 pci_msi_mask_irq(data);
Sunil Muthuswamyd9932b42022-01-05 11:32:36 -08001455 if (data->parent_data->chip->irq_mask)
1456 irq_chip_mask_parent(data);
Jake Oshins4daace02016-02-16 21:56:23 +00001457}
1458
1459/**
1460 * hv_irq_unmask() - "Unmask" the IRQ by setting its current
1461 * affinity.
1462 * @data: Describes the IRQ
1463 *
1464 * Build new a destination for the MSI and make a hypercall to
1465 * update the Interrupt Redirection Table. "Device Logical ID"
1466 * is built out of this PCI bus's instance GUID and the function
1467 * number of the device.
1468 */
Tobias Klauser542ccf42016-10-31 12:04:09 +01001469static void hv_irq_unmask(struct irq_data *data)
Jake Oshins4daace02016-02-16 21:56:23 +00001470{
1471 struct msi_desc *msi_desc = irq_data_get_msi_desc(data);
Boqun Feng61bfd922020-02-10 11:39:52 +08001472 struct hv_retarget_device_interrupt *params;
Jake Oshins4daace02016-02-16 21:56:23 +00001473 struct hv_pcibus_device *hbus;
1474 struct cpumask *dest;
Maya Nakamurac8ccf752019-03-01 07:04:17 +00001475 cpumask_var_t tmp;
Jake Oshins4daace02016-02-16 21:56:23 +00001476 struct pci_bus *pbus;
1477 struct pci_dev *pdev;
Long Li0de8ce32016-11-08 14:04:38 -08001478 unsigned long flags;
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001479 u32 var_size = 0;
Maya Nakamurac8ccf752019-03-01 07:04:17 +00001480 int cpu, nr_bank;
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001481 u64 res;
Jake Oshins4daace02016-02-16 21:56:23 +00001482
Dexuan Cui79aa8012017-11-01 20:30:53 +00001483 dest = irq_data_get_effective_affinity_mask(data);
Jake Oshins4daace02016-02-16 21:56:23 +00001484 pdev = msi_desc_to_pci_dev(msi_desc);
1485 pbus = pdev->bus;
1486 hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata);
1487
Long Li0de8ce32016-11-08 14:04:38 -08001488 spin_lock_irqsave(&hbus->retarget_msi_interrupt_lock, flags);
1489
1490 params = &hbus->retarget_msi_interrupt_params;
1491 memset(params, 0, sizeof(*params));
1492 params->partition_id = HV_PARTITION_ID_SELF;
Wei Liub59fb7b2021-02-03 15:04:31 +00001493 params->int_entry.source = HV_INTERRUPT_SOURCE_MSI;
Boqun Feng1cf106d2020-02-10 11:39:53 +08001494 hv_set_msi_entry_from_desc(&params->int_entry.msi_entry, msi_desc);
Long Li0de8ce32016-11-08 14:04:38 -08001495 params->device_id = (hbus->hdev->dev_instance.b[5] << 24) |
Jake Oshins4daace02016-02-16 21:56:23 +00001496 (hbus->hdev->dev_instance.b[4] << 16) |
1497 (hbus->hdev->dev_instance.b[7] << 8) |
1498 (hbus->hdev->dev_instance.b[6] & 0xf8) |
1499 PCI_FUNC(pdev->devfn);
Sunil Muthuswamy831c1ae2022-01-05 11:32:35 -08001500 params->int_target.vector = hv_msi_get_int_vector(data);
Jake Oshins4daace02016-02-16 21:56:23 +00001501
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001502 /*
Thomas Gleixner72161292020-10-24 22:35:05 +01001503 * Honoring apic->delivery_mode set to APIC_DELIVERY_MODE_FIXED by
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001504 * setting the HV_DEVICE_INTERRUPT_TARGET_MULTICAST flag results in a
1505 * spurious interrupt storm. Not doing so does not seem to have a
1506 * negative effect (yet?).
1507 */
Jake Oshins4daace02016-02-16 21:56:23 +00001508
Dexuan Cui14ef39f2019-11-24 21:33:53 -08001509 if (hbus->protocol_version >= PCI_PROTOCOL_VERSION_1_2) {
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001510 /*
1511 * PCI_PROTOCOL_VERSION_1_2 supports the VP_SET version of the
1512 * HVCALL_RETARGET_INTERRUPT hypercall, which also coincides
1513 * with >64 VP support.
1514 * ms_hyperv.hints & HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED
1515 * is not sufficient for this hypercall.
1516 */
1517 params->int_target.flags |=
1518 HV_DEVICE_INTERRUPT_TARGET_PROCESSOR_SET;
Maya Nakamurac8ccf752019-03-01 07:04:17 +00001519
1520 if (!alloc_cpumask_var(&tmp, GFP_ATOMIC)) {
1521 res = 1;
1522 goto exit_unlock;
1523 }
1524
1525 cpumask_and(tmp, dest, cpu_online_mask);
1526 nr_bank = cpumask_to_vpset(&params->int_target.vp_set, tmp);
1527 free_cpumask_var(tmp);
1528
1529 if (nr_bank <= 0) {
1530 res = 1;
1531 goto exit_unlock;
1532 }
Long Li0de8ce32016-11-08 14:04:38 -08001533
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001534 /*
1535 * var-sized hypercall, var-size starts after vp_mask (thus
Maya Nakamura9bc11742019-03-01 06:59:02 +00001536 * vp_set.format does not count, but vp_set.valid_bank_mask
1537 * does).
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001538 */
Maya Nakamurac8ccf752019-03-01 07:04:17 +00001539 var_size = 1 + nr_bank;
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001540 } else {
1541 for_each_cpu_and(cpu, dest, cpu_online_mask) {
1542 params->int_target.vp_mask |=
Vitaly Kuznetsov7415aea2017-08-02 18:09:18 +02001543 (1ULL << hv_cpu_number_to_vp_number(cpu));
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001544 }
1545 }
1546
1547 res = hv_do_hypercall(HVCALL_RETARGET_INTERRUPT | (var_size << 17),
1548 params, NULL);
1549
1550exit_unlock:
Long Li0de8ce32016-11-08 14:04:38 -08001551 spin_unlock_irqrestore(&hbus->retarget_msi_interrupt_lock, flags);
Jake Oshins4daace02016-02-16 21:56:23 +00001552
Dexuan Cui915cff72020-10-02 01:51:58 -07001553 /*
1554 * During hibernation, when a CPU is offlined, the kernel tries
1555 * to move the interrupt to the remaining CPUs that haven't
1556 * been offlined yet. In this case, the below hv_do_hypercall()
1557 * always fails since the vmbus channel has been closed:
1558 * refer to cpu_disable_common() -> fixup_irqs() ->
1559 * irq_migrate_all_off_this_cpu() -> migrate_one_irq().
1560 *
1561 * Suppress the error message for hibernation because the failure
1562 * during hibernation does not matter (at this time all the devices
1563 * have been frozen). Note: the correct affinity info is still updated
1564 * into the irqdata data structure in migrate_one_irq() ->
1565 * irq_do_set_affinity() -> hv_set_affinity(), so later when the VM
1566 * resumes, hv_pci_restore_msi_state() is able to correctly restore
1567 * the interrupt with the correct affinity.
1568 */
Joseph Salisbury753ed9c2021-04-16 17:43:03 -07001569 if (!hv_result_success(res) && hbus->state != hv_pcibus_removing)
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001570 dev_err(&hbus->hdev->device,
1571 "%s() failed: %#llx", __func__, res);
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001572
Sunil Muthuswamyd9932b42022-01-05 11:32:36 -08001573 if (data->parent_data->chip->irq_unmask)
1574 irq_chip_unmask_parent(data);
Jake Oshins4daace02016-02-16 21:56:23 +00001575 pci_msi_unmask_irq(data);
1576}
1577
1578struct compose_comp_ctxt {
1579 struct hv_pci_compl comp_pkt;
1580 struct tran_int_desc int_desc;
1581};
1582
1583static void hv_pci_compose_compl(void *context, struct pci_response *resp,
1584 int resp_packet_size)
1585{
1586 struct compose_comp_ctxt *comp_pkt = context;
1587 struct pci_create_int_response *int_resp =
1588 (struct pci_create_int_response *)resp;
1589
1590 comp_pkt->comp_pkt.completion_status = resp->status;
1591 comp_pkt->int_desc = int_resp->int_desc;
1592 complete(&comp_pkt->comp_pkt.host_event);
1593}
1594
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001595static u32 hv_compose_msi_req_v1(
1596 struct pci_create_interrupt *int_pkt, struct cpumask *affinity,
1597 u32 slot, u8 vector)
1598{
1599 int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE;
1600 int_pkt->wslot.slot = slot;
1601 int_pkt->int_desc.vector = vector;
1602 int_pkt->int_desc.vector_count = 1;
Sunil Muthuswamy831c1ae2022-01-05 11:32:35 -08001603 int_pkt->int_desc.delivery_mode = DELIVERY_MODE;
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001604
1605 /*
1606 * Create MSI w/ dummy vCPU set, overwritten by subsequent retarget in
1607 * hv_irq_unmask().
1608 */
1609 int_pkt->int_desc.cpu_mask = CPU_AFFINITY_ALL;
1610
1611 return sizeof(*int_pkt);
1612}
1613
Sunil Muthuswamy8f6a6b32021-07-12 21:58:18 +00001614/*
1615 * Create MSI w/ dummy vCPU set targeting just one vCPU, overwritten
1616 * by subsequent retarget in hv_irq_unmask().
1617 */
1618static int hv_compose_msi_req_get_cpu(struct cpumask *affinity)
1619{
1620 return cpumask_first_and(affinity, cpu_online_mask);
1621}
1622
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001623static u32 hv_compose_msi_req_v2(
1624 struct pci_create_interrupt2 *int_pkt, struct cpumask *affinity,
1625 u32 slot, u8 vector)
1626{
1627 int cpu;
1628
1629 int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE2;
1630 int_pkt->wslot.slot = slot;
1631 int_pkt->int_desc.vector = vector;
1632 int_pkt->int_desc.vector_count = 1;
Sunil Muthuswamy831c1ae2022-01-05 11:32:35 -08001633 int_pkt->int_desc.delivery_mode = DELIVERY_MODE;
Sunil Muthuswamy8f6a6b32021-07-12 21:58:18 +00001634 cpu = hv_compose_msi_req_get_cpu(affinity);
1635 int_pkt->int_desc.processor_array[0] =
1636 hv_cpu_number_to_vp_number(cpu);
1637 int_pkt->int_desc.processor_count = 1;
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001638
Sunil Muthuswamy8f6a6b32021-07-12 21:58:18 +00001639 return sizeof(*int_pkt);
1640}
1641
1642static u32 hv_compose_msi_req_v3(
1643 struct pci_create_interrupt3 *int_pkt, struct cpumask *affinity,
1644 u32 slot, u32 vector)
1645{
1646 int cpu;
1647
1648 int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE3;
1649 int_pkt->wslot.slot = slot;
1650 int_pkt->int_desc.vector = vector;
1651 int_pkt->int_desc.reserved = 0;
1652 int_pkt->int_desc.vector_count = 1;
Sunil Muthuswamy831c1ae2022-01-05 11:32:35 -08001653 int_pkt->int_desc.delivery_mode = DELIVERY_MODE;
Sunil Muthuswamy8f6a6b32021-07-12 21:58:18 +00001654 cpu = hv_compose_msi_req_get_cpu(affinity);
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001655 int_pkt->int_desc.processor_array[0] =
Vitaly Kuznetsov7415aea2017-08-02 18:09:18 +02001656 hv_cpu_number_to_vp_number(cpu);
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001657 int_pkt->int_desc.processor_count = 1;
1658
1659 return sizeof(*int_pkt);
1660}
1661
Jake Oshins4daace02016-02-16 21:56:23 +00001662/**
1663 * hv_compose_msi_msg() - Supplies a valid MSI address/data
1664 * @data: Everything about this MSI
1665 * @msg: Buffer that is filled in by this function
1666 *
1667 * This function unpacks the IRQ looking for target CPU set, IDT
1668 * vector and mode and sends a message to the parent partition
1669 * asking for a mapping for that tuple in this partition. The
1670 * response supplies a data value and address to which that data
1671 * should be written to trigger that interrupt.
1672 */
1673static void hv_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
1674{
Jake Oshins4daace02016-02-16 21:56:23 +00001675 struct hv_pcibus_device *hbus;
Andrea Parri (Microsoft)240ad772020-04-06 02:15:10 +02001676 struct vmbus_channel *channel;
Jake Oshins4daace02016-02-16 21:56:23 +00001677 struct hv_pci_dev *hpdev;
1678 struct pci_bus *pbus;
1679 struct pci_dev *pdev;
Dexuan Cui79aa8012017-11-01 20:30:53 +00001680 struct cpumask *dest;
Jake Oshins4daace02016-02-16 21:56:23 +00001681 struct compose_comp_ctxt comp;
1682 struct tran_int_desc *int_desc;
Jake Oshins4daace02016-02-16 21:56:23 +00001683 struct {
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001684 struct pci_packet pci_pkt;
1685 union {
1686 struct pci_create_interrupt v1;
1687 struct pci_create_interrupt2 v2;
Sunil Muthuswamy8f6a6b32021-07-12 21:58:18 +00001688 struct pci_create_interrupt3 v3;
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001689 } int_pkts;
1690 } __packed ctxt;
1691
1692 u32 size;
Jake Oshins4daace02016-02-16 21:56:23 +00001693 int ret;
1694
1695 pdev = msi_desc_to_pci_dev(irq_data_get_msi_desc(data));
Dexuan Cui79aa8012017-11-01 20:30:53 +00001696 dest = irq_data_get_effective_affinity_mask(data);
Jake Oshins4daace02016-02-16 21:56:23 +00001697 pbus = pdev->bus;
1698 hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata);
Andrea Parri (Microsoft)240ad772020-04-06 02:15:10 +02001699 channel = hbus->hdev->channel;
Jake Oshins4daace02016-02-16 21:56:23 +00001700 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
1701 if (!hpdev)
1702 goto return_null_message;
1703
1704 /* Free any previous message that might have already been composed. */
1705 if (data->chip_data) {
1706 int_desc = data->chip_data;
1707 data->chip_data = NULL;
1708 hv_int_desc_free(hpdev, int_desc);
1709 }
1710
K. Y. Srinivasan59c58cee2017-03-24 11:07:22 -07001711 int_desc = kzalloc(sizeof(*int_desc), GFP_ATOMIC);
Jake Oshins4daace02016-02-16 21:56:23 +00001712 if (!int_desc)
1713 goto drop_reference;
1714
1715 memset(&ctxt, 0, sizeof(ctxt));
1716 init_completion(&comp.comp_pkt.host_event);
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001717 ctxt.pci_pkt.completion_func = hv_pci_compose_compl;
1718 ctxt.pci_pkt.compl_ctxt = &comp;
Jake Oshins4daace02016-02-16 21:56:23 +00001719
Dexuan Cui14ef39f2019-11-24 21:33:53 -08001720 switch (hbus->protocol_version) {
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001721 case PCI_PROTOCOL_VERSION_1_1:
1722 size = hv_compose_msi_req_v1(&ctxt.int_pkts.v1,
Dexuan Cui79aa8012017-11-01 20:30:53 +00001723 dest,
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001724 hpdev->desc.win_slot.slot,
Sunil Muthuswamy831c1ae2022-01-05 11:32:35 -08001725 hv_msi_get_int_vector(data));
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001726 break;
1727
1728 case PCI_PROTOCOL_VERSION_1_2:
Long Li999dd952020-02-25 21:06:08 -08001729 case PCI_PROTOCOL_VERSION_1_3:
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001730 size = hv_compose_msi_req_v2(&ctxt.int_pkts.v2,
Dexuan Cui79aa8012017-11-01 20:30:53 +00001731 dest,
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001732 hpdev->desc.win_slot.slot,
Sunil Muthuswamy831c1ae2022-01-05 11:32:35 -08001733 hv_msi_get_int_vector(data));
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001734 break;
1735
Sunil Muthuswamy8f6a6b32021-07-12 21:58:18 +00001736 case PCI_PROTOCOL_VERSION_1_4:
1737 size = hv_compose_msi_req_v3(&ctxt.int_pkts.v3,
1738 dest,
1739 hpdev->desc.win_slot.slot,
Sunil Muthuswamy831c1ae2022-01-05 11:32:35 -08001740 hv_msi_get_int_vector(data));
Sunil Muthuswamy8f6a6b32021-07-12 21:58:18 +00001741 break;
1742
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001743 default:
1744 /* As we only negotiate protocol versions known to this driver,
1745 * this path should never hit. However, this is it not a hot
1746 * path so we print a message to aid future updates.
1747 */
1748 dev_err(&hbus->hdev->device,
1749 "Unexpected vPCI protocol, update driver.");
1750 goto free_int_desc;
Jake Oshins4daace02016-02-16 21:56:23 +00001751 }
1752
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001753 ret = vmbus_sendpacket(hpdev->hbus->hdev->channel, &ctxt.int_pkts,
1754 size, (unsigned long)&ctxt.pci_pkt,
Jake Oshins4daace02016-02-16 21:56:23 +00001755 VM_PKT_DATA_INBAND,
1756 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001757 if (ret) {
1758 dev_err(&hbus->hdev->device,
1759 "Sending request for interrupt failed: 0x%x",
1760 comp.comp_pkt.completion_status);
Dexuan Cui665e2242016-08-23 04:48:11 +00001761 goto free_int_desc;
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001762 }
Dexuan Cui665e2242016-08-23 04:48:11 +00001763
Stephen Hemminger80bfeeb2017-07-31 16:48:29 -07001764 /*
Andrea Parri (Microsoft)240ad772020-04-06 02:15:10 +02001765 * Prevents hv_pci_onchannelcallback() from running concurrently
1766 * in the tasklet.
1767 */
Sebastian Andrzej Siewiorbe4017c2021-03-09 09:42:15 +01001768 tasklet_disable_in_atomic(&channel->callback_event);
Andrea Parri (Microsoft)240ad772020-04-06 02:15:10 +02001769
1770 /*
Stephen Hemminger80bfeeb2017-07-31 16:48:29 -07001771 * Since this function is called with IRQ locks held, can't
1772 * do normal wait for completion; instead poll.
1773 */
Dexuan Cuide0aa7b2018-03-15 14:21:08 +00001774 while (!try_wait_for_completion(&comp.comp_pkt.host_event)) {
Andrea Parri (Microsoft)240ad772020-04-06 02:15:10 +02001775 unsigned long flags;
1776
Dexuan Cuide0aa7b2018-03-15 14:21:08 +00001777 /* 0xFFFF means an invalid PCI VENDOR ID. */
1778 if (hv_pcifront_get_vendor_id(hpdev) == 0xFFFF) {
1779 dev_err_once(&hbus->hdev->device,
1780 "the device has gone\n");
Andrea Parri (Microsoft)240ad772020-04-06 02:15:10 +02001781 goto enable_tasklet;
Dexuan Cuide0aa7b2018-03-15 14:21:08 +00001782 }
1783
1784 /*
Andrea Parri (Microsoft)240ad772020-04-06 02:15:10 +02001785 * Make sure that the ring buffer data structure doesn't get
1786 * freed while we dereference the ring buffer pointer. Test
1787 * for the channel's onchannel_callback being NULL within a
1788 * sched_lock critical section. See also the inline comments
1789 * in vmbus_reset_channel_cb().
Dexuan Cuide0aa7b2018-03-15 14:21:08 +00001790 */
Andrea Parri (Microsoft)240ad772020-04-06 02:15:10 +02001791 spin_lock_irqsave(&channel->sched_lock, flags);
1792 if (unlikely(channel->onchannel_callback == NULL)) {
1793 spin_unlock_irqrestore(&channel->sched_lock, flags);
1794 goto enable_tasklet;
1795 }
1796 hv_pci_onchannelcallback(hbus);
1797 spin_unlock_irqrestore(&channel->sched_lock, flags);
Dexuan Cuide0aa7b2018-03-15 14:21:08 +00001798
1799 if (hpdev->state == hv_pcichild_ejecting) {
1800 dev_err_once(&hbus->hdev->device,
1801 "the device is being ejected\n");
Andrea Parri (Microsoft)240ad772020-04-06 02:15:10 +02001802 goto enable_tasklet;
Dexuan Cuide0aa7b2018-03-15 14:21:08 +00001803 }
1804
Stephen Hemminger80bfeeb2017-07-31 16:48:29 -07001805 udelay(100);
Dexuan Cuide0aa7b2018-03-15 14:21:08 +00001806 }
Jake Oshins4daace02016-02-16 21:56:23 +00001807
Andrea Parri (Microsoft)240ad772020-04-06 02:15:10 +02001808 tasklet_enable(&channel->callback_event);
1809
Jake Oshins4daace02016-02-16 21:56:23 +00001810 if (comp.comp_pkt.completion_status < 0) {
1811 dev_err(&hbus->hdev->device,
1812 "Request for interrupt failed: 0x%x",
1813 comp.comp_pkt.completion_status);
1814 goto free_int_desc;
1815 }
1816
1817 /*
1818 * Record the assignment so that this can be unwound later. Using
1819 * irq_set_chip_data() here would be appropriate, but the lock it takes
1820 * is already held.
1821 */
1822 *int_desc = comp.int_desc;
1823 data->chip_data = int_desc;
1824
1825 /* Pass up the result. */
1826 msg->address_hi = comp.int_desc.address >> 32;
1827 msg->address_lo = comp.int_desc.address & 0xffffffff;
1828 msg->data = comp.int_desc.data;
1829
Stephen Hemminger8c99e122018-05-23 10:11:12 -07001830 put_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +00001831 return;
1832
Andrea Parri (Microsoft)240ad772020-04-06 02:15:10 +02001833enable_tasklet:
1834 tasklet_enable(&channel->callback_event);
Jake Oshins4daace02016-02-16 21:56:23 +00001835free_int_desc:
1836 kfree(int_desc);
1837drop_reference:
Stephen Hemminger8c99e122018-05-23 10:11:12 -07001838 put_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +00001839return_null_message:
1840 msg->address_hi = 0;
1841 msg->address_lo = 0;
1842 msg->data = 0;
1843}
1844
1845/* HW Interrupt Chip Descriptor */
1846static struct irq_chip hv_msi_irq_chip = {
1847 .name = "Hyper-V PCIe MSI",
1848 .irq_compose_msi_msg = hv_compose_msi_msg,
Sunil Muthuswamy831c1ae2022-01-05 11:32:35 -08001849 .irq_set_affinity = irq_chip_set_affinity_parent,
Sunil Muthuswamyd9932b42022-01-05 11:32:36 -08001850#ifdef CONFIG_X86
Jake Oshins4daace02016-02-16 21:56:23 +00001851 .irq_ack = irq_chip_ack_parent,
Sunil Muthuswamyd9932b42022-01-05 11:32:36 -08001852#elif defined(CONFIG_ARM64)
1853 .irq_eoi = irq_chip_eoi_parent,
1854#endif
Jake Oshins4daace02016-02-16 21:56:23 +00001855 .irq_mask = hv_irq_mask,
1856 .irq_unmask = hv_irq_unmask,
1857};
1858
Jake Oshins4daace02016-02-16 21:56:23 +00001859static struct msi_domain_ops hv_msi_ops = {
Sunil Muthuswamy831c1ae2022-01-05 11:32:35 -08001860 .msi_prepare = hv_msi_prepare,
Jake Oshins4daace02016-02-16 21:56:23 +00001861 .msi_free = hv_msi_free,
1862};
1863
1864/**
1865 * hv_pcie_init_irq_domain() - Initialize IRQ domain
1866 * @hbus: The root PCI bus
1867 *
1868 * This function creates an IRQ domain which will be used for
1869 * interrupts from devices that have been passed through. These
1870 * devices only support MSI and MSI-X, not line-based interrupts
1871 * or simulations of line-based interrupts through PCIe's
1872 * fabric-layer messages. Because interrupts are remapped, we
1873 * can support multi-message MSI here.
1874 *
1875 * Return: '0' on success and error value on failure
1876 */
1877static int hv_pcie_init_irq_domain(struct hv_pcibus_device *hbus)
1878{
1879 hbus->msi_info.chip = &hv_msi_irq_chip;
1880 hbus->msi_info.ops = &hv_msi_ops;
1881 hbus->msi_info.flags = (MSI_FLAG_USE_DEF_DOM_OPS |
1882 MSI_FLAG_USE_DEF_CHIP_OPS | MSI_FLAG_MULTI_PCI_MSI |
1883 MSI_FLAG_PCI_MSIX);
Sunil Muthuswamy831c1ae2022-01-05 11:32:35 -08001884 hbus->msi_info.handler = FLOW_HANDLER;
1885 hbus->msi_info.handler_name = FLOW_NAME;
Jake Oshins4daace02016-02-16 21:56:23 +00001886 hbus->msi_info.data = hbus;
Boqun Feng9e7f9172021-07-27 02:06:56 +08001887 hbus->irq_domain = pci_msi_create_irq_domain(hbus->fwnode,
Jake Oshins4daace02016-02-16 21:56:23 +00001888 &hbus->msi_info,
Sunil Muthuswamy831c1ae2022-01-05 11:32:35 -08001889 hv_pci_get_root_domain());
Jake Oshins4daace02016-02-16 21:56:23 +00001890 if (!hbus->irq_domain) {
1891 dev_err(&hbus->hdev->device,
1892 "Failed to build an MSI IRQ domain\n");
1893 return -ENODEV;
1894 }
1895
Boqun Feng9e7f9172021-07-27 02:06:56 +08001896 dev_set_msi_domain(&hbus->bridge->dev, hbus->irq_domain);
1897
Jake Oshins4daace02016-02-16 21:56:23 +00001898 return 0;
1899}
1900
1901/**
1902 * get_bar_size() - Get the address space consumed by a BAR
1903 * @bar_val: Value that a BAR returned after -1 was written
1904 * to it.
1905 *
1906 * This function returns the size of the BAR, rounded up to 1
1907 * page. It has to be rounded up because the hypervisor's page
1908 * table entry that maps the BAR into the VM can't specify an
1909 * offset within a page. The invariant is that the hypervisor
1910 * must place any BARs of smaller than page length at the
1911 * beginning of a page.
1912 *
1913 * Return: Size in bytes of the consumed MMIO space.
1914 */
1915static u64 get_bar_size(u64 bar_val)
1916{
1917 return round_up((1 + ~(bar_val & PCI_BASE_ADDRESS_MEM_MASK)),
1918 PAGE_SIZE);
1919}
1920
1921/**
1922 * survey_child_resources() - Total all MMIO requirements
1923 * @hbus: Root PCI bus, as understood by this driver
1924 */
1925static void survey_child_resources(struct hv_pcibus_device *hbus)
1926{
Jake Oshins4daace02016-02-16 21:56:23 +00001927 struct hv_pci_dev *hpdev;
1928 resource_size_t bar_size = 0;
1929 unsigned long flags;
1930 struct completion *event;
1931 u64 bar_val;
1932 int i;
1933
1934 /* If nobody is waiting on the answer, don't compute it. */
1935 event = xchg(&hbus->survey_event, NULL);
1936 if (!event)
1937 return;
1938
1939 /* If the answer has already been computed, go with it. */
1940 if (hbus->low_mmio_space || hbus->high_mmio_space) {
1941 complete(event);
1942 return;
1943 }
1944
1945 spin_lock_irqsave(&hbus->device_list_lock, flags);
1946
1947 /*
1948 * Due to an interesting quirk of the PCI spec, all memory regions
1949 * for a child device are a power of 2 in size and aligned in memory,
1950 * so it's sufficient to just add them up without tracking alignment.
1951 */
Stephen Hemminger5b8db8f2018-05-23 10:11:14 -07001952 list_for_each_entry(hpdev, &hbus->children, list_entry) {
Denis Efremovc9c13ba2019-09-28 02:43:08 +03001953 for (i = 0; i < PCI_STD_NUM_BARS; i++) {
Jake Oshins4daace02016-02-16 21:56:23 +00001954 if (hpdev->probed_bar[i] & PCI_BASE_ADDRESS_SPACE_IO)
1955 dev_err(&hbus->hdev->device,
1956 "There's an I/O BAR in this list!\n");
1957
1958 if (hpdev->probed_bar[i] != 0) {
1959 /*
1960 * A probed BAR has all the upper bits set that
1961 * can be changed.
1962 */
1963
1964 bar_val = hpdev->probed_bar[i];
1965 if (bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64)
1966 bar_val |=
1967 ((u64)hpdev->probed_bar[++i] << 32);
1968 else
1969 bar_val |= 0xffffffff00000000ULL;
1970
1971 bar_size = get_bar_size(bar_val);
1972
1973 if (bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64)
1974 hbus->high_mmio_space += bar_size;
1975 else
1976 hbus->low_mmio_space += bar_size;
1977 }
1978 }
1979 }
1980
1981 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1982 complete(event);
1983}
1984
1985/**
1986 * prepopulate_bars() - Fill in BARs with defaults
1987 * @hbus: Root PCI bus, as understood by this driver
1988 *
1989 * The core PCI driver code seems much, much happier if the BARs
1990 * for a device have values upon first scan. So fill them in.
1991 * The algorithm below works down from large sizes to small,
1992 * attempting to pack the assignments optimally. The assumption,
1993 * enforced in other parts of the code, is that the beginning of
1994 * the memory-mapped I/O space will be aligned on the largest
1995 * BAR size.
1996 */
1997static void prepopulate_bars(struct hv_pcibus_device *hbus)
1998{
1999 resource_size_t high_size = 0;
2000 resource_size_t low_size = 0;
2001 resource_size_t high_base = 0;
2002 resource_size_t low_base = 0;
2003 resource_size_t bar_size;
2004 struct hv_pci_dev *hpdev;
Jake Oshins4daace02016-02-16 21:56:23 +00002005 unsigned long flags;
2006 u64 bar_val;
2007 u32 command;
2008 bool high;
2009 int i;
2010
2011 if (hbus->low_mmio_space) {
2012 low_size = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space));
2013 low_base = hbus->low_mmio_res->start;
2014 }
2015
2016 if (hbus->high_mmio_space) {
2017 high_size = 1ULL <<
2018 (63 - __builtin_clzll(hbus->high_mmio_space));
2019 high_base = hbus->high_mmio_res->start;
2020 }
2021
2022 spin_lock_irqsave(&hbus->device_list_lock, flags);
2023
Dexuan Cuiac82fc82019-11-24 21:33:52 -08002024 /*
2025 * Clear the memory enable bit, in case it's already set. This occurs
2026 * in the suspend path of hibernation, where the device is suspended,
2027 * resumed and suspended again: see hibernation_snapshot() and
2028 * hibernation_platform_enter().
2029 *
Bjorn Helgaasc77bfb52021-01-26 15:38:55 -06002030 * If the memory enable bit is already set, Hyper-V silently ignores
Dexuan Cuiac82fc82019-11-24 21:33:52 -08002031 * the below BAR updates, and the related PCI device driver can not
2032 * work, because reading from the device register(s) always returns
Naveen Naidu14e04d02021-11-18 19:33:34 +05302033 * 0xFFFFFFFF (PCI_ERROR_RESPONSE).
Dexuan Cuiac82fc82019-11-24 21:33:52 -08002034 */
2035 list_for_each_entry(hpdev, &hbus->children, list_entry) {
2036 _hv_pcifront_read_config(hpdev, PCI_COMMAND, 2, &command);
2037 command &= ~PCI_COMMAND_MEMORY;
2038 _hv_pcifront_write_config(hpdev, PCI_COMMAND, 2, command);
2039 }
2040
Jake Oshins4daace02016-02-16 21:56:23 +00002041 /* Pick addresses for the BARs. */
2042 do {
Stephen Hemminger5b8db8f2018-05-23 10:11:14 -07002043 list_for_each_entry(hpdev, &hbus->children, list_entry) {
Denis Efremovc9c13ba2019-09-28 02:43:08 +03002044 for (i = 0; i < PCI_STD_NUM_BARS; i++) {
Jake Oshins4daace02016-02-16 21:56:23 +00002045 bar_val = hpdev->probed_bar[i];
2046 if (bar_val == 0)
2047 continue;
2048 high = bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64;
2049 if (high) {
2050 bar_val |=
2051 ((u64)hpdev->probed_bar[i + 1]
2052 << 32);
2053 } else {
2054 bar_val |= 0xffffffffULL << 32;
2055 }
2056 bar_size = get_bar_size(bar_val);
2057 if (high) {
2058 if (high_size != bar_size) {
2059 i++;
2060 continue;
2061 }
2062 _hv_pcifront_write_config(hpdev,
2063 PCI_BASE_ADDRESS_0 + (4 * i),
2064 4,
2065 (u32)(high_base & 0xffffff00));
2066 i++;
2067 _hv_pcifront_write_config(hpdev,
2068 PCI_BASE_ADDRESS_0 + (4 * i),
2069 4, (u32)(high_base >> 32));
2070 high_base += bar_size;
2071 } else {
2072 if (low_size != bar_size)
2073 continue;
2074 _hv_pcifront_write_config(hpdev,
2075 PCI_BASE_ADDRESS_0 + (4 * i),
2076 4,
2077 (u32)(low_base & 0xffffff00));
2078 low_base += bar_size;
2079 }
2080 }
2081 if (high_size <= 1 && low_size <= 1) {
2082 /* Set the memory enable bit. */
2083 _hv_pcifront_read_config(hpdev, PCI_COMMAND, 2,
2084 &command);
2085 command |= PCI_COMMAND_MEMORY;
2086 _hv_pcifront_write_config(hpdev, PCI_COMMAND, 2,
2087 command);
2088 break;
2089 }
2090 }
2091
2092 high_size >>= 1;
2093 low_size >>= 1;
2094 } while (high_size || low_size);
2095
2096 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
2097}
2098
Stephen Hemmingera15f2c082018-09-14 12:54:56 -07002099/*
2100 * Assign entries in sysfs pci slot directory.
2101 *
2102 * Note that this function does not need to lock the children list
2103 * because it is called from pci_devices_present_work which
2104 * is serialized with hv_eject_device_work because they are on the
2105 * same ordered workqueue. Therefore hbus->children list will not change
2106 * even when pci_create_slot sleeps.
2107 */
2108static void hv_pci_assign_slots(struct hv_pcibus_device *hbus)
2109{
2110 struct hv_pci_dev *hpdev;
2111 char name[SLOT_NAME_SIZE];
2112 int slot_nr;
2113
2114 list_for_each_entry(hpdev, &hbus->children, list_entry) {
2115 if (hpdev->pci_slot)
2116 continue;
2117
2118 slot_nr = PCI_SLOT(wslot_to_devfn(hpdev->desc.win_slot.slot));
2119 snprintf(name, SLOT_NAME_SIZE, "%u", hpdev->desc.ser);
Arnd Bergmann418cb6c2021-07-27 02:06:54 +08002120 hpdev->pci_slot = pci_create_slot(hbus->bridge->bus, slot_nr,
Stephen Hemmingera15f2c082018-09-14 12:54:56 -07002121 name, NULL);
Wei Yongjun54be5b82018-09-21 02:53:17 +00002122 if (IS_ERR(hpdev->pci_slot)) {
Stephen Hemmingera15f2c082018-09-14 12:54:56 -07002123 pr_warn("pci_create slot %s failed\n", name);
Wei Yongjun54be5b82018-09-21 02:53:17 +00002124 hpdev->pci_slot = NULL;
2125 }
Stephen Hemmingera15f2c082018-09-14 12:54:56 -07002126 }
2127}
2128
Dexuan Cui15becc22019-03-04 21:34:48 +00002129/*
2130 * Remove entries in sysfs pci slot directory.
2131 */
2132static void hv_pci_remove_slots(struct hv_pcibus_device *hbus)
2133{
2134 struct hv_pci_dev *hpdev;
2135
2136 list_for_each_entry(hpdev, &hbus->children, list_entry) {
2137 if (!hpdev->pci_slot)
2138 continue;
2139 pci_destroy_slot(hpdev->pci_slot);
2140 hpdev->pci_slot = NULL;
2141 }
2142}
2143
Long Li999dd952020-02-25 21:06:08 -08002144/*
2145 * Set NUMA node for the devices on the bus
2146 */
2147static void hv_pci_assign_numa_node(struct hv_pcibus_device *hbus)
2148{
2149 struct pci_dev *dev;
Arnd Bergmann418cb6c2021-07-27 02:06:54 +08002150 struct pci_bus *bus = hbus->bridge->bus;
Long Li999dd952020-02-25 21:06:08 -08002151 struct hv_pci_dev *hv_dev;
2152
2153 list_for_each_entry(dev, &bus->devices, bus_list) {
2154 hv_dev = get_pcichild_wslot(hbus, devfn_to_wslot(dev->devfn));
2155 if (!hv_dev)
2156 continue;
2157
2158 if (hv_dev->desc.flags & HV_PCI_DEVICE_FLAG_NUMA_AFFINITY)
2159 set_dev_node(&dev->dev, hv_dev->desc.virtual_numa_node);
2160
2161 put_pcichild(hv_dev);
2162 }
2163}
2164
Jake Oshins4daace02016-02-16 21:56:23 +00002165/**
2166 * create_root_hv_pci_bus() - Expose a new root PCI bus
2167 * @hbus: Root PCI bus, as understood by this driver
2168 *
2169 * Return: 0 on success, -errno on failure
2170 */
2171static int create_root_hv_pci_bus(struct hv_pcibus_device *hbus)
2172{
Arnd Bergmann418cb6c2021-07-27 02:06:54 +08002173 int error;
2174 struct pci_host_bridge *bridge = hbus->bridge;
2175
2176 bridge->dev.parent = &hbus->hdev->device;
2177 bridge->sysdata = &hbus->sysdata;
2178 bridge->ops = &hv_pcifront_ops;
2179
2180 error = pci_scan_root_bus_bridge(bridge);
2181 if (error)
2182 return error;
Jake Oshins4daace02016-02-16 21:56:23 +00002183
Long Li414428c2017-03-23 14:58:32 -07002184 pci_lock_rescan_remove();
Long Li999dd952020-02-25 21:06:08 -08002185 hv_pci_assign_numa_node(hbus);
Arnd Bergmann418cb6c2021-07-27 02:06:54 +08002186 pci_bus_assign_resources(bridge->bus);
Stephen Hemmingera15f2c082018-09-14 12:54:56 -07002187 hv_pci_assign_slots(hbus);
Arnd Bergmann418cb6c2021-07-27 02:06:54 +08002188 pci_bus_add_devices(bridge->bus);
Long Li414428c2017-03-23 14:58:32 -07002189 pci_unlock_rescan_remove();
Jake Oshins4daace02016-02-16 21:56:23 +00002190 hbus->state = hv_pcibus_installed;
2191 return 0;
2192}
2193
2194struct q_res_req_compl {
2195 struct completion host_event;
2196 struct hv_pci_dev *hpdev;
2197};
2198
2199/**
2200 * q_resource_requirements() - Query Resource Requirements
2201 * @context: The completion context.
2202 * @resp: The response that came from the host.
2203 * @resp_packet_size: The size in bytes of resp.
2204 *
2205 * This function is invoked on completion of a Query Resource
2206 * Requirements packet.
2207 */
2208static void q_resource_requirements(void *context, struct pci_response *resp,
2209 int resp_packet_size)
2210{
2211 struct q_res_req_compl *completion = context;
2212 struct pci_q_res_req_response *q_res_req =
2213 (struct pci_q_res_req_response *)resp;
2214 int i;
2215
2216 if (resp->status < 0) {
2217 dev_err(&completion->hpdev->hbus->hdev->device,
2218 "query resource requirements failed: %x\n",
2219 resp->status);
2220 } else {
Denis Efremovc9c13ba2019-09-28 02:43:08 +03002221 for (i = 0; i < PCI_STD_NUM_BARS; i++) {
Jake Oshins4daace02016-02-16 21:56:23 +00002222 completion->hpdev->probed_bar[i] =
2223 q_res_req->probed_bar[i];
2224 }
2225 }
2226
2227 complete(&completion->host_event);
2228}
2229
Jake Oshins4daace02016-02-16 21:56:23 +00002230/**
2231 * new_pcichild_device() - Create a new child device
2232 * @hbus: The internal struct tracking this root PCI bus.
2233 * @desc: The information supplied so far from the host
2234 * about the device.
2235 *
2236 * This function creates the tracking structure for a new child
2237 * device and kicks off the process of figuring out what it is.
2238 *
2239 * Return: Pointer to the new tracking struct
2240 */
2241static struct hv_pci_dev *new_pcichild_device(struct hv_pcibus_device *hbus,
Long Lif9ad0f32020-02-25 21:06:07 -08002242 struct hv_pcidev_description *desc)
Jake Oshins4daace02016-02-16 21:56:23 +00002243{
2244 struct hv_pci_dev *hpdev;
2245 struct pci_child_message *res_req;
2246 struct q_res_req_compl comp_pkt;
Dexuan Cui8286e962016-11-10 07:17:48 +00002247 struct {
2248 struct pci_packet init_packet;
2249 u8 buffer[sizeof(struct pci_child_message)];
Jake Oshins4daace02016-02-16 21:56:23 +00002250 } pkt;
2251 unsigned long flags;
2252 int ret;
2253
Jia-Ju Bai7403bd12018-03-18 22:53:28 +08002254 hpdev = kzalloc(sizeof(*hpdev), GFP_KERNEL);
Jake Oshins4daace02016-02-16 21:56:23 +00002255 if (!hpdev)
2256 return NULL;
2257
2258 hpdev->hbus = hbus;
2259
2260 memset(&pkt, 0, sizeof(pkt));
2261 init_completion(&comp_pkt.host_event);
2262 comp_pkt.hpdev = hpdev;
2263 pkt.init_packet.compl_ctxt = &comp_pkt;
2264 pkt.init_packet.completion_func = q_resource_requirements;
2265 res_req = (struct pci_child_message *)&pkt.init_packet.message;
Dexuan Cui0c6045d2016-08-23 04:45:51 +00002266 res_req->message_type.type = PCI_QUERY_RESOURCE_REQUIREMENTS;
Jake Oshins4daace02016-02-16 21:56:23 +00002267 res_req->wslot.slot = desc->win_slot.slot;
2268
2269 ret = vmbus_sendpacket(hbus->hdev->channel, res_req,
2270 sizeof(struct pci_child_message),
2271 (unsigned long)&pkt.init_packet,
2272 VM_PKT_DATA_INBAND,
2273 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
2274 if (ret)
2275 goto error;
2276
Dexuan Cuic3635da2018-05-23 21:12:01 +00002277 if (wait_for_response(hbus->hdev, &comp_pkt.host_event))
2278 goto error;
Jake Oshins4daace02016-02-16 21:56:23 +00002279
2280 hpdev->desc = *desc;
Elena Reshetova24196f02017-04-18 09:02:48 -05002281 refcount_set(&hpdev->refs, 1);
Stephen Hemminger8c99e122018-05-23 10:11:12 -07002282 get_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +00002283 spin_lock_irqsave(&hbus->device_list_lock, flags);
Haiyang Zhang4a9b0932017-02-13 18:10:11 +00002284
Jake Oshins4daace02016-02-16 21:56:23 +00002285 list_add_tail(&hpdev->list_entry, &hbus->children);
2286 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
2287 return hpdev;
2288
2289error:
2290 kfree(hpdev);
2291 return NULL;
2292}
2293
2294/**
2295 * get_pcichild_wslot() - Find device from slot
2296 * @hbus: Root PCI bus, as understood by this driver
2297 * @wslot: Location on the bus
2298 *
2299 * This function looks up a PCI device and returns the internal
2300 * representation of it. It acquires a reference on it, so that
2301 * the device won't be deleted while somebody is using it. The
2302 * caller is responsible for calling put_pcichild() to release
2303 * this reference.
2304 *
2305 * Return: Internal representation of a PCI device
2306 */
2307static struct hv_pci_dev *get_pcichild_wslot(struct hv_pcibus_device *hbus,
2308 u32 wslot)
2309{
2310 unsigned long flags;
2311 struct hv_pci_dev *iter, *hpdev = NULL;
2312
2313 spin_lock_irqsave(&hbus->device_list_lock, flags);
2314 list_for_each_entry(iter, &hbus->children, list_entry) {
2315 if (iter->desc.win_slot.slot == wslot) {
2316 hpdev = iter;
Stephen Hemminger8c99e122018-05-23 10:11:12 -07002317 get_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +00002318 break;
2319 }
2320 }
2321 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
2322
2323 return hpdev;
2324}
2325
2326/**
2327 * pci_devices_present_work() - Handle new list of child devices
2328 * @work: Work struct embedded in struct hv_dr_work
2329 *
2330 * "Bus Relations" is the Windows term for "children of this
2331 * bus." The terminology is preserved here for people trying to
2332 * debug the interaction between Hyper-V and Linux. This
2333 * function is called when the parent partition reports a list
2334 * of functions that should be observed under this PCI Express
2335 * port (bus).
2336 *
2337 * This function updates the list, and must tolerate being
2338 * called multiple times with the same information. The typical
2339 * number of child devices is one, with very atypical cases
2340 * involving three or four, so the algorithms used here can be
2341 * simple and inefficient.
2342 *
2343 * It must also treat the omission of a previously observed device as
2344 * notification that the device no longer exists.
2345 *
Dexuan Cui021ad272018-03-15 14:20:53 +00002346 * Note that this function is serialized with hv_eject_device_work(),
2347 * because both are pushed to the ordered workqueue hbus->wq.
Jake Oshins4daace02016-02-16 21:56:23 +00002348 */
2349static void pci_devices_present_work(struct work_struct *work)
2350{
2351 u32 child_no;
2352 bool found;
Long Lif9ad0f32020-02-25 21:06:07 -08002353 struct hv_pcidev_description *new_desc;
Jake Oshins4daace02016-02-16 21:56:23 +00002354 struct hv_pci_dev *hpdev;
2355 struct hv_pcibus_device *hbus;
2356 struct list_head removed;
2357 struct hv_dr_work *dr_wrk;
2358 struct hv_dr_state *dr = NULL;
2359 unsigned long flags;
2360
2361 dr_wrk = container_of(work, struct hv_dr_work, wrk);
2362 hbus = dr_wrk->bus;
2363 kfree(dr_wrk);
2364
2365 INIT_LIST_HEAD(&removed);
2366
Jake Oshins4daace02016-02-16 21:56:23 +00002367 /* Pull this off the queue and process it if it was the last one. */
2368 spin_lock_irqsave(&hbus->device_list_lock, flags);
2369 while (!list_empty(&hbus->dr_list)) {
2370 dr = list_first_entry(&hbus->dr_list, struct hv_dr_state,
2371 list_entry);
2372 list_del(&dr->list_entry);
2373
2374 /* Throw this away if the list still has stuff in it. */
2375 if (!list_empty(&hbus->dr_list)) {
2376 kfree(dr);
2377 continue;
2378 }
2379 }
2380 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
2381
Long Li326dc2e2021-05-12 01:06:49 -07002382 if (!dr)
Jake Oshins4daace02016-02-16 21:56:23 +00002383 return;
Jake Oshins4daace02016-02-16 21:56:23 +00002384
2385 /* First, mark all existing children as reported missing. */
2386 spin_lock_irqsave(&hbus->device_list_lock, flags);
Stephen Hemminger5b8db8f2018-05-23 10:11:14 -07002387 list_for_each_entry(hpdev, &hbus->children, list_entry) {
2388 hpdev->reported_missing = true;
Jake Oshins4daace02016-02-16 21:56:23 +00002389 }
2390 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
2391
2392 /* Next, add back any reported devices. */
2393 for (child_no = 0; child_no < dr->device_count; child_no++) {
2394 found = false;
2395 new_desc = &dr->func[child_no];
2396
2397 spin_lock_irqsave(&hbus->device_list_lock, flags);
Stephen Hemminger5b8db8f2018-05-23 10:11:14 -07002398 list_for_each_entry(hpdev, &hbus->children, list_entry) {
2399 if ((hpdev->desc.win_slot.slot == new_desc->win_slot.slot) &&
Jake Oshins4daace02016-02-16 21:56:23 +00002400 (hpdev->desc.v_id == new_desc->v_id) &&
2401 (hpdev->desc.d_id == new_desc->d_id) &&
2402 (hpdev->desc.ser == new_desc->ser)) {
2403 hpdev->reported_missing = false;
2404 found = true;
2405 }
2406 }
2407 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
2408
2409 if (!found) {
2410 hpdev = new_pcichild_device(hbus, new_desc);
2411 if (!hpdev)
2412 dev_err(&hbus->hdev->device,
2413 "couldn't record a child device.\n");
2414 }
2415 }
2416
2417 /* Move missing children to a list on the stack. */
2418 spin_lock_irqsave(&hbus->device_list_lock, flags);
2419 do {
2420 found = false;
Stephen Hemminger5b8db8f2018-05-23 10:11:14 -07002421 list_for_each_entry(hpdev, &hbus->children, list_entry) {
Jake Oshins4daace02016-02-16 21:56:23 +00002422 if (hpdev->reported_missing) {
2423 found = true;
Stephen Hemminger8c99e122018-05-23 10:11:12 -07002424 put_pcichild(hpdev);
Wei Yongjun4f1cb012016-07-28 16:16:48 +00002425 list_move_tail(&hpdev->list_entry, &removed);
Jake Oshins4daace02016-02-16 21:56:23 +00002426 break;
2427 }
2428 }
2429 } while (found);
2430 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
2431
2432 /* Delete everything that should no longer exist. */
2433 while (!list_empty(&removed)) {
2434 hpdev = list_first_entry(&removed, struct hv_pci_dev,
2435 list_entry);
2436 list_del(&hpdev->list_entry);
Dexuan Cui340d4552019-03-04 21:34:49 +00002437
2438 if (hpdev->pci_slot)
2439 pci_destroy_slot(hpdev->pci_slot);
2440
Stephen Hemminger8c99e122018-05-23 10:11:12 -07002441 put_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +00002442 }
2443
Jork Loeser691ac1d2017-05-24 13:41:24 -07002444 switch (hbus->state) {
Long Lid3a78d82017-03-23 14:58:10 -07002445 case hv_pcibus_installed:
2446 /*
Jork Loeser691ac1d2017-05-24 13:41:24 -07002447 * Tell the core to rescan bus
2448 * because there may have been changes.
2449 */
Jake Oshins4daace02016-02-16 21:56:23 +00002450 pci_lock_rescan_remove();
Arnd Bergmann418cb6c2021-07-27 02:06:54 +08002451 pci_scan_child_bus(hbus->bridge->bus);
Long Li999dd952020-02-25 21:06:08 -08002452 hv_pci_assign_numa_node(hbus);
Stephen Hemmingera15f2c082018-09-14 12:54:56 -07002453 hv_pci_assign_slots(hbus);
Jake Oshins4daace02016-02-16 21:56:23 +00002454 pci_unlock_rescan_remove();
Long Lid3a78d82017-03-23 14:58:10 -07002455 break;
2456
2457 case hv_pcibus_init:
2458 case hv_pcibus_probed:
Jake Oshins4daace02016-02-16 21:56:23 +00002459 survey_child_resources(hbus);
Long Lid3a78d82017-03-23 14:58:10 -07002460 break;
2461
2462 default:
2463 break;
Jake Oshins4daace02016-02-16 21:56:23 +00002464 }
2465
Jake Oshins4daace02016-02-16 21:56:23 +00002466 kfree(dr);
2467}
2468
2469/**
Long Lif9ad0f32020-02-25 21:06:07 -08002470 * hv_pci_start_relations_work() - Queue work to start device discovery
Jake Oshins4daace02016-02-16 21:56:23 +00002471 * @hbus: Root PCI bus, as understood by this driver
Long Lif9ad0f32020-02-25 21:06:07 -08002472 * @dr: The list of children returned from host
Jake Oshins4daace02016-02-16 21:56:23 +00002473 *
Long Lif9ad0f32020-02-25 21:06:07 -08002474 * Return: 0 on success, -errno on failure
Jake Oshins4daace02016-02-16 21:56:23 +00002475 */
Long Lif9ad0f32020-02-25 21:06:07 -08002476static int hv_pci_start_relations_work(struct hv_pcibus_device *hbus,
2477 struct hv_dr_state *dr)
Jake Oshins4daace02016-02-16 21:56:23 +00002478{
Jake Oshins4daace02016-02-16 21:56:23 +00002479 struct hv_dr_work *dr_wrk;
2480 unsigned long flags;
Dexuan Cui948373b2018-03-15 14:22:00 +00002481 bool pending_dr;
Jake Oshins4daace02016-02-16 21:56:23 +00002482
Dexuan Cuiac82fc82019-11-24 21:33:52 -08002483 if (hbus->state == hv_pcibus_removing) {
2484 dev_info(&hbus->hdev->device,
2485 "PCI VMBus BUS_RELATIONS: ignored\n");
Long Lif9ad0f32020-02-25 21:06:07 -08002486 return -ENOENT;
Dexuan Cuiac82fc82019-11-24 21:33:52 -08002487 }
2488
Jake Oshins4daace02016-02-16 21:56:23 +00002489 dr_wrk = kzalloc(sizeof(*dr_wrk), GFP_NOWAIT);
2490 if (!dr_wrk)
Long Lif9ad0f32020-02-25 21:06:07 -08002491 return -ENOMEM;
Jake Oshins4daace02016-02-16 21:56:23 +00002492
2493 INIT_WORK(&dr_wrk->wrk, pci_devices_present_work);
2494 dr_wrk->bus = hbus;
Jake Oshins4daace02016-02-16 21:56:23 +00002495
2496 spin_lock_irqsave(&hbus->device_list_lock, flags);
Dexuan Cui948373b2018-03-15 14:22:00 +00002497 /*
2498 * If pending_dr is true, we have already queued a work,
2499 * which will see the new dr. Otherwise, we need to
2500 * queue a new work.
2501 */
2502 pending_dr = !list_empty(&hbus->dr_list);
Jake Oshins4daace02016-02-16 21:56:23 +00002503 list_add_tail(&dr->list_entry, &hbus->dr_list);
2504 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
2505
Long Li326dc2e2021-05-12 01:06:49 -07002506 if (pending_dr)
Dexuan Cui948373b2018-03-15 14:22:00 +00002507 kfree(dr_wrk);
Long Li326dc2e2021-05-12 01:06:49 -07002508 else
Dexuan Cui948373b2018-03-15 14:22:00 +00002509 queue_work(hbus->wq, &dr_wrk->wrk);
Long Lif9ad0f32020-02-25 21:06:07 -08002510
2511 return 0;
2512}
2513
2514/**
2515 * hv_pci_devices_present() - Handle list of new children
2516 * @hbus: Root PCI bus, as understood by this driver
2517 * @relations: Packet from host listing children
2518 *
2519 * Process a new list of devices on the bus. The list of devices is
2520 * discovered by VSP and sent to us via VSP message PCI_BUS_RELATIONS,
2521 * whenever a new list of devices for this bus appears.
2522 */
2523static void hv_pci_devices_present(struct hv_pcibus_device *hbus,
2524 struct pci_bus_relations *relations)
2525{
2526 struct hv_dr_state *dr;
2527 int i;
2528
Gustavo A. R. Silvad0684fd02020-05-25 11:43:19 -05002529 dr = kzalloc(struct_size(dr, func, relations->device_count),
2530 GFP_NOWAIT);
Long Lif9ad0f32020-02-25 21:06:07 -08002531 if (!dr)
2532 return;
2533
2534 dr->device_count = relations->device_count;
2535 for (i = 0; i < dr->device_count; i++) {
2536 dr->func[i].v_id = relations->func[i].v_id;
2537 dr->func[i].d_id = relations->func[i].d_id;
2538 dr->func[i].rev = relations->func[i].rev;
2539 dr->func[i].prog_intf = relations->func[i].prog_intf;
2540 dr->func[i].subclass = relations->func[i].subclass;
2541 dr->func[i].base_class = relations->func[i].base_class;
2542 dr->func[i].subsystem_id = relations->func[i].subsystem_id;
2543 dr->func[i].win_slot = relations->func[i].win_slot;
2544 dr->func[i].ser = relations->func[i].ser;
2545 }
2546
2547 if (hv_pci_start_relations_work(hbus, dr))
2548 kfree(dr);
Jake Oshins4daace02016-02-16 21:56:23 +00002549}
2550
2551/**
Long Li999dd952020-02-25 21:06:08 -08002552 * hv_pci_devices_present2() - Handle list of new children
2553 * @hbus: Root PCI bus, as understood by this driver
2554 * @relations: Packet from host listing children
2555 *
2556 * This function is the v2 version of hv_pci_devices_present()
2557 */
2558static void hv_pci_devices_present2(struct hv_pcibus_device *hbus,
2559 struct pci_bus_relations2 *relations)
2560{
2561 struct hv_dr_state *dr;
2562 int i;
2563
Gustavo A. R. Silvad0684fd02020-05-25 11:43:19 -05002564 dr = kzalloc(struct_size(dr, func, relations->device_count),
2565 GFP_NOWAIT);
Long Li999dd952020-02-25 21:06:08 -08002566 if (!dr)
2567 return;
2568
2569 dr->device_count = relations->device_count;
2570 for (i = 0; i < dr->device_count; i++) {
2571 dr->func[i].v_id = relations->func[i].v_id;
2572 dr->func[i].d_id = relations->func[i].d_id;
2573 dr->func[i].rev = relations->func[i].rev;
2574 dr->func[i].prog_intf = relations->func[i].prog_intf;
2575 dr->func[i].subclass = relations->func[i].subclass;
2576 dr->func[i].base_class = relations->func[i].base_class;
2577 dr->func[i].subsystem_id = relations->func[i].subsystem_id;
2578 dr->func[i].win_slot = relations->func[i].win_slot;
2579 dr->func[i].ser = relations->func[i].ser;
2580 dr->func[i].flags = relations->func[i].flags;
2581 dr->func[i].virtual_numa_node =
2582 relations->func[i].virtual_numa_node;
2583 }
2584
2585 if (hv_pci_start_relations_work(hbus, dr))
2586 kfree(dr);
2587}
2588
2589/**
Jake Oshins4daace02016-02-16 21:56:23 +00002590 * hv_eject_device_work() - Asynchronously handles ejection
2591 * @work: Work struct embedded in internal device struct
2592 *
2593 * This function handles ejecting a device. Windows will
2594 * attempt to gracefully eject a device, waiting 60 seconds to
2595 * hear back from the guest OS that this completed successfully.
2596 * If this timer expires, the device will be forcibly removed.
2597 */
2598static void hv_eject_device_work(struct work_struct *work)
2599{
2600 struct pci_eject_response *ejct_pkt;
Dexuan Cui4df591b22019-06-21 23:45:23 +00002601 struct hv_pcibus_device *hbus;
Jake Oshins4daace02016-02-16 21:56:23 +00002602 struct hv_pci_dev *hpdev;
2603 struct pci_dev *pdev;
2604 unsigned long flags;
2605 int wslot;
2606 struct {
2607 struct pci_packet pkt;
Dexuan Cui0c6045d2016-08-23 04:45:51 +00002608 u8 buffer[sizeof(struct pci_eject_response)];
Jake Oshins4daace02016-02-16 21:56:23 +00002609 } ctxt;
2610
2611 hpdev = container_of(work, struct hv_pci_dev, wrk);
Dexuan Cui4df591b22019-06-21 23:45:23 +00002612 hbus = hpdev->hbus;
Jake Oshins4daace02016-02-16 21:56:23 +00002613
Dexuan Cuifca288c2018-03-15 14:21:43 +00002614 WARN_ON(hpdev->state != hv_pcichild_ejecting);
Jake Oshins4daace02016-02-16 21:56:23 +00002615
2616 /*
2617 * Ejection can come before or after the PCI bus has been set up, so
2618 * attempt to find it and tear down the bus state, if it exists. This
Arnd Bergmann418cb6c2021-07-27 02:06:54 +08002619 * must be done without constructs like pci_domain_nr(hbus->bridge->bus)
2620 * because hbus->bridge->bus may not exist yet.
Jake Oshins4daace02016-02-16 21:56:23 +00002621 */
2622 wslot = wslot_to_devfn(hpdev->desc.win_slot.slot);
Boqun Feng38c0d262021-07-27 02:06:55 +08002623 pdev = pci_get_domain_bus_and_slot(hbus->bridge->domain_nr, 0, wslot);
Jake Oshins4daace02016-02-16 21:56:23 +00002624 if (pdev) {
Long Li414428c2017-03-23 14:58:32 -07002625 pci_lock_rescan_remove();
Jake Oshins4daace02016-02-16 21:56:23 +00002626 pci_stop_and_remove_bus_device(pdev);
2627 pci_dev_put(pdev);
Long Li414428c2017-03-23 14:58:32 -07002628 pci_unlock_rescan_remove();
Jake Oshins4daace02016-02-16 21:56:23 +00002629 }
2630
Dexuan Cui4df591b22019-06-21 23:45:23 +00002631 spin_lock_irqsave(&hbus->device_list_lock, flags);
Dexuan Cuie74d2eb2016-11-10 07:19:52 +00002632 list_del(&hpdev->list_entry);
Dexuan Cui4df591b22019-06-21 23:45:23 +00002633 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
Dexuan Cuie74d2eb2016-11-10 07:19:52 +00002634
Stephen Hemmingera15f2c082018-09-14 12:54:56 -07002635 if (hpdev->pci_slot)
2636 pci_destroy_slot(hpdev->pci_slot);
2637
Jake Oshins4daace02016-02-16 21:56:23 +00002638 memset(&ctxt, 0, sizeof(ctxt));
2639 ejct_pkt = (struct pci_eject_response *)&ctxt.pkt.message;
Dexuan Cui0c6045d2016-08-23 04:45:51 +00002640 ejct_pkt->message_type.type = PCI_EJECTION_COMPLETE;
Jake Oshins4daace02016-02-16 21:56:23 +00002641 ejct_pkt->wslot.slot = hpdev->desc.win_slot.slot;
Dexuan Cui4df591b22019-06-21 23:45:23 +00002642 vmbus_sendpacket(hbus->hdev->channel, ejct_pkt,
Jake Oshins4daace02016-02-16 21:56:23 +00002643 sizeof(*ejct_pkt), (unsigned long)&ctxt.pkt,
2644 VM_PKT_DATA_INBAND, 0);
2645
Dexuan Cui05f151a2019-03-04 21:34:48 +00002646 /* For the get_pcichild() in hv_pci_eject_device() */
2647 put_pcichild(hpdev);
2648 /* For the two refs got in new_pcichild_device() */
Stephen Hemminger8c99e122018-05-23 10:11:12 -07002649 put_pcichild(hpdev);
2650 put_pcichild(hpdev);
Dexuan Cui4df591b22019-06-21 23:45:23 +00002651 /* hpdev has been freed. Do not use it any more. */
Jake Oshins4daace02016-02-16 21:56:23 +00002652}
2653
2654/**
2655 * hv_pci_eject_device() - Handles device ejection
2656 * @hpdev: Internal device tracking struct
2657 *
2658 * This function is invoked when an ejection packet arrives. It
2659 * just schedules work so that we don't re-enter the packet
2660 * delivery code handling the ejection.
2661 */
2662static void hv_pci_eject_device(struct hv_pci_dev *hpdev)
2663{
Dexuan Cuiac82fc82019-11-24 21:33:52 -08002664 struct hv_pcibus_device *hbus = hpdev->hbus;
2665 struct hv_device *hdev = hbus->hdev;
2666
2667 if (hbus->state == hv_pcibus_removing) {
2668 dev_info(&hdev->device, "PCI VMBus EJECT: ignored\n");
2669 return;
2670 }
2671
Jake Oshins4daace02016-02-16 21:56:23 +00002672 hpdev->state = hv_pcichild_ejecting;
Stephen Hemminger8c99e122018-05-23 10:11:12 -07002673 get_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +00002674 INIT_WORK(&hpdev->wrk, hv_eject_device_work);
Dexuan Cuiac82fc82019-11-24 21:33:52 -08002675 queue_work(hbus->wq, &hpdev->wrk);
Jake Oshins4daace02016-02-16 21:56:23 +00002676}
2677
2678/**
2679 * hv_pci_onchannelcallback() - Handles incoming packets
2680 * @context: Internal bus tracking struct
2681 *
2682 * This function is invoked whenever the host sends a packet to
2683 * this channel (which is private to this root PCI bus).
2684 */
2685static void hv_pci_onchannelcallback(void *context)
2686{
2687 const int packet_size = 0x100;
2688 int ret;
2689 struct hv_pcibus_device *hbus = context;
2690 u32 bytes_recvd;
2691 u64 req_id;
2692 struct vmpacket_descriptor *desc;
2693 unsigned char *buffer;
2694 int bufferlen = packet_size;
2695 struct pci_packet *comp_packet;
2696 struct pci_response *response;
2697 struct pci_incoming_message *new_message;
2698 struct pci_bus_relations *bus_rel;
Long Li999dd952020-02-25 21:06:08 -08002699 struct pci_bus_relations2 *bus_rel2;
Dexuan Cuie5d2f912019-08-22 05:05:37 +00002700 struct pci_dev_inval_block *inval;
Jake Oshins4daace02016-02-16 21:56:23 +00002701 struct pci_dev_incoming *dev_message;
2702 struct hv_pci_dev *hpdev;
2703
2704 buffer = kmalloc(bufferlen, GFP_ATOMIC);
2705 if (!buffer)
2706 return;
2707
2708 while (1) {
2709 ret = vmbus_recvpacket_raw(hbus->hdev->channel, buffer,
2710 bufferlen, &bytes_recvd, &req_id);
2711
2712 if (ret == -ENOBUFS) {
2713 kfree(buffer);
2714 /* Handle large packet */
2715 bufferlen = bytes_recvd;
2716 buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
2717 if (!buffer)
2718 return;
2719 continue;
2720 }
2721
Vitaly Kuznetsov837d7412016-06-17 12:45:30 -05002722 /* Zero length indicates there are no more packets. */
2723 if (ret || !bytes_recvd)
2724 break;
2725
Jake Oshins4daace02016-02-16 21:56:23 +00002726 /*
2727 * All incoming packets must be at least as large as a
2728 * response.
2729 */
Vitaly Kuznetsov60fcdac2016-05-30 16:17:58 +02002730 if (bytes_recvd <= sizeof(struct pci_response))
Vitaly Kuznetsov837d7412016-06-17 12:45:30 -05002731 continue;
Jake Oshins4daace02016-02-16 21:56:23 +00002732 desc = (struct vmpacket_descriptor *)buffer;
2733
2734 switch (desc->type) {
2735 case VM_PKT_COMP:
2736
2737 /*
2738 * The host is trusted, and thus it's safe to interpret
2739 * this transaction ID as a pointer.
2740 */
2741 comp_packet = (struct pci_packet *)req_id;
2742 response = (struct pci_response *)buffer;
2743 comp_packet->completion_func(comp_packet->compl_ctxt,
2744 response,
2745 bytes_recvd);
Vitaly Kuznetsov60fcdac2016-05-30 16:17:58 +02002746 break;
Jake Oshins4daace02016-02-16 21:56:23 +00002747
2748 case VM_PKT_DATA_INBAND:
2749
2750 new_message = (struct pci_incoming_message *)buffer;
Dexuan Cui0c6045d2016-08-23 04:45:51 +00002751 switch (new_message->message_type.type) {
Jake Oshins4daace02016-02-16 21:56:23 +00002752 case PCI_BUS_RELATIONS:
2753
2754 bus_rel = (struct pci_bus_relations *)buffer;
2755 if (bytes_recvd <
Gustavo A. R. Silvad0684fd02020-05-25 11:43:19 -05002756 struct_size(bus_rel, func,
2757 bus_rel->device_count)) {
Jake Oshins4daace02016-02-16 21:56:23 +00002758 dev_err(&hbus->hdev->device,
2759 "bus relations too small\n");
2760 break;
2761 }
2762
2763 hv_pci_devices_present(hbus, bus_rel);
2764 break;
2765
Long Li999dd952020-02-25 21:06:08 -08002766 case PCI_BUS_RELATIONS2:
2767
2768 bus_rel2 = (struct pci_bus_relations2 *)buffer;
2769 if (bytes_recvd <
Gustavo A. R. Silvad0684fd02020-05-25 11:43:19 -05002770 struct_size(bus_rel2, func,
2771 bus_rel2->device_count)) {
Long Li999dd952020-02-25 21:06:08 -08002772 dev_err(&hbus->hdev->device,
2773 "bus relations v2 too small\n");
2774 break;
2775 }
2776
2777 hv_pci_devices_present2(hbus, bus_rel2);
2778 break;
2779
Jake Oshins4daace02016-02-16 21:56:23 +00002780 case PCI_EJECT:
2781
2782 dev_message = (struct pci_dev_incoming *)buffer;
2783 hpdev = get_pcichild_wslot(hbus,
2784 dev_message->wslot.slot);
2785 if (hpdev) {
2786 hv_pci_eject_device(hpdev);
Stephen Hemminger8c99e122018-05-23 10:11:12 -07002787 put_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +00002788 }
2789 break;
2790
Dexuan Cuie5d2f912019-08-22 05:05:37 +00002791 case PCI_INVALIDATE_BLOCK:
2792
2793 inval = (struct pci_dev_inval_block *)buffer;
2794 hpdev = get_pcichild_wslot(hbus,
2795 inval->wslot.slot);
2796 if (hpdev) {
2797 if (hpdev->block_invalidate) {
2798 hpdev->block_invalidate(
2799 hpdev->invalidate_context,
2800 inval->block_mask);
2801 }
2802 put_pcichild(hpdev);
2803 }
2804 break;
2805
Jake Oshins4daace02016-02-16 21:56:23 +00002806 default:
2807 dev_warn(&hbus->hdev->device,
2808 "Unimplemented protocol message %x\n",
Dexuan Cui0c6045d2016-08-23 04:45:51 +00002809 new_message->message_type.type);
Jake Oshins4daace02016-02-16 21:56:23 +00002810 break;
2811 }
2812 break;
2813
2814 default:
2815 dev_err(&hbus->hdev->device,
2816 "unhandled packet type %d, tid %llx len %d\n",
2817 desc->type, req_id, bytes_recvd);
2818 break;
2819 }
Jake Oshins4daace02016-02-16 21:56:23 +00002820 }
Vitaly Kuznetsov60fcdac2016-05-30 16:17:58 +02002821
2822 kfree(buffer);
Jake Oshins4daace02016-02-16 21:56:23 +00002823}
2824
2825/**
2826 * hv_pci_protocol_negotiation() - Set up protocol
Krzysztof Wilczyński6d2730c2020-09-25 23:47:53 +00002827 * @hdev: VMBus's tracking struct for this root PCI bus.
2828 * @version: Array of supported channel protocol versions in
2829 * the order of probing - highest go first.
2830 * @num_version: Number of elements in the version array.
Jake Oshins4daace02016-02-16 21:56:23 +00002831 *
2832 * This driver is intended to support running on Windows 10
2833 * (server) and later versions. It will not run on earlier
2834 * versions, as they assume that many of the operations which
2835 * Linux needs accomplished with a spinlock held were done via
2836 * asynchronous messaging via VMBus. Windows 10 increases the
2837 * surface area of PCI emulation so that these actions can take
2838 * place by suspending a virtual processor for their duration.
2839 *
2840 * This function negotiates the channel protocol version,
2841 * failing if the host doesn't support the necessary protocol
2842 * level.
2843 */
Dexuan Cuia8e37502019-11-24 21:33:51 -08002844static int hv_pci_protocol_negotiation(struct hv_device *hdev,
2845 enum pci_protocol_version_t version[],
2846 int num_version)
Jake Oshins4daace02016-02-16 21:56:23 +00002847{
Dexuan Cui14ef39f2019-11-24 21:33:53 -08002848 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
Jake Oshins4daace02016-02-16 21:56:23 +00002849 struct pci_version_request *version_req;
2850 struct hv_pci_compl comp_pkt;
2851 struct pci_packet *pkt;
2852 int ret;
Jork Loeserb1db7e72017-05-24 13:41:27 -07002853 int i;
Jake Oshins4daace02016-02-16 21:56:23 +00002854
2855 /*
2856 * Initiate the handshake with the host and negotiate
2857 * a version that the host can support. We start with the
2858 * highest version number and go down if the host cannot
2859 * support it.
2860 */
2861 pkt = kzalloc(sizeof(*pkt) + sizeof(*version_req), GFP_KERNEL);
2862 if (!pkt)
2863 return -ENOMEM;
2864
2865 init_completion(&comp_pkt.host_event);
2866 pkt->completion_func = hv_pci_generic_compl;
2867 pkt->compl_ctxt = &comp_pkt;
2868 version_req = (struct pci_version_request *)&pkt->message;
Dexuan Cui0c6045d2016-08-23 04:45:51 +00002869 version_req->message_type.type = PCI_QUERY_PROTOCOL_VERSION;
Jake Oshins4daace02016-02-16 21:56:23 +00002870
Dexuan Cuia8e37502019-11-24 21:33:51 -08002871 for (i = 0; i < num_version; i++) {
2872 version_req->protocol_version = version[i];
Jork Loeserb1db7e72017-05-24 13:41:27 -07002873 ret = vmbus_sendpacket(hdev->channel, version_req,
2874 sizeof(struct pci_version_request),
2875 (unsigned long)pkt, VM_PKT_DATA_INBAND,
2876 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
Dexuan Cuic3635da2018-05-23 21:12:01 +00002877 if (!ret)
2878 ret = wait_for_response(hdev, &comp_pkt.host_event);
2879
Jork Loeserb1db7e72017-05-24 13:41:27 -07002880 if (ret) {
2881 dev_err(&hdev->device,
Dexuan Cuic3635da2018-05-23 21:12:01 +00002882 "PCI Pass-through VSP failed to request version: %d",
Jork Loeserb1db7e72017-05-24 13:41:27 -07002883 ret);
2884 goto exit;
2885 }
Jake Oshins4daace02016-02-16 21:56:23 +00002886
Jork Loeserb1db7e72017-05-24 13:41:27 -07002887 if (comp_pkt.completion_status >= 0) {
Dexuan Cui14ef39f2019-11-24 21:33:53 -08002888 hbus->protocol_version = version[i];
Jork Loeserb1db7e72017-05-24 13:41:27 -07002889 dev_info(&hdev->device,
2890 "PCI VMBus probing: Using version %#x\n",
Dexuan Cui14ef39f2019-11-24 21:33:53 -08002891 hbus->protocol_version);
Jork Loeserb1db7e72017-05-24 13:41:27 -07002892 goto exit;
2893 }
2894
2895 if (comp_pkt.completion_status != STATUS_REVISION_MISMATCH) {
2896 dev_err(&hdev->device,
2897 "PCI Pass-through VSP failed version request: %#x",
2898 comp_pkt.completion_status);
2899 ret = -EPROTO;
2900 goto exit;
2901 }
2902
2903 reinit_completion(&comp_pkt.host_event);
Jake Oshins4daace02016-02-16 21:56:23 +00002904 }
2905
Jork Loeserb1db7e72017-05-24 13:41:27 -07002906 dev_err(&hdev->device,
2907 "PCI pass-through VSP failed to find supported version");
2908 ret = -EPROTO;
Jake Oshins4daace02016-02-16 21:56:23 +00002909
2910exit:
2911 kfree(pkt);
2912 return ret;
2913}
2914
2915/**
2916 * hv_pci_free_bridge_windows() - Release memory regions for the
2917 * bus
2918 * @hbus: Root PCI bus, as understood by this driver
2919 */
2920static void hv_pci_free_bridge_windows(struct hv_pcibus_device *hbus)
2921{
2922 /*
2923 * Set the resources back to the way they looked when they
2924 * were allocated by setting IORESOURCE_BUSY again.
2925 */
2926
2927 if (hbus->low_mmio_space && hbus->low_mmio_res) {
2928 hbus->low_mmio_res->flags |= IORESOURCE_BUSY;
Jake Oshins696ca5e2016-04-05 10:22:52 -07002929 vmbus_free_mmio(hbus->low_mmio_res->start,
2930 resource_size(hbus->low_mmio_res));
Jake Oshins4daace02016-02-16 21:56:23 +00002931 }
2932
2933 if (hbus->high_mmio_space && hbus->high_mmio_res) {
2934 hbus->high_mmio_res->flags |= IORESOURCE_BUSY;
Jake Oshins696ca5e2016-04-05 10:22:52 -07002935 vmbus_free_mmio(hbus->high_mmio_res->start,
2936 resource_size(hbus->high_mmio_res));
Jake Oshins4daace02016-02-16 21:56:23 +00002937 }
2938}
2939
2940/**
2941 * hv_pci_allocate_bridge_windows() - Allocate memory regions
2942 * for the bus
2943 * @hbus: Root PCI bus, as understood by this driver
2944 *
2945 * This function calls vmbus_allocate_mmio(), which is itself a
2946 * bit of a compromise. Ideally, we might change the pnp layer
2947 * in the kernel such that it comprehends either PCI devices
2948 * which are "grandchildren of ACPI," with some intermediate bus
2949 * node (in this case, VMBus) or change it such that it
2950 * understands VMBus. The pnp layer, however, has been declared
2951 * deprecated, and not subject to change.
2952 *
2953 * The workaround, implemented here, is to ask VMBus to allocate
2954 * MMIO space for this bus. VMBus itself knows which ranges are
2955 * appropriate by looking at its own ACPI objects. Then, after
2956 * these ranges are claimed, they're modified to look like they
2957 * would have looked if the ACPI and pnp code had allocated
2958 * bridge windows. These descriptors have to exist in this form
2959 * in order to satisfy the code which will get invoked when the
2960 * endpoint PCI function driver calls request_mem_region() or
2961 * request_mem_region_exclusive().
2962 *
2963 * Return: 0 on success, -errno on failure
2964 */
2965static int hv_pci_allocate_bridge_windows(struct hv_pcibus_device *hbus)
2966{
2967 resource_size_t align;
2968 int ret;
2969
2970 if (hbus->low_mmio_space) {
2971 align = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space));
2972 ret = vmbus_allocate_mmio(&hbus->low_mmio_res, hbus->hdev, 0,
2973 (u64)(u32)0xffffffff,
2974 hbus->low_mmio_space,
2975 align, false);
2976 if (ret) {
2977 dev_err(&hbus->hdev->device,
2978 "Need %#llx of low MMIO space. Consider reconfiguring the VM.\n",
2979 hbus->low_mmio_space);
2980 return ret;
2981 }
2982
2983 /* Modify this resource to become a bridge window. */
2984 hbus->low_mmio_res->flags |= IORESOURCE_WINDOW;
2985 hbus->low_mmio_res->flags &= ~IORESOURCE_BUSY;
Arnd Bergmann418cb6c2021-07-27 02:06:54 +08002986 pci_add_resource(&hbus->bridge->windows, hbus->low_mmio_res);
Jake Oshins4daace02016-02-16 21:56:23 +00002987 }
2988
2989 if (hbus->high_mmio_space) {
2990 align = 1ULL << (63 - __builtin_clzll(hbus->high_mmio_space));
2991 ret = vmbus_allocate_mmio(&hbus->high_mmio_res, hbus->hdev,
2992 0x100000000, -1,
2993 hbus->high_mmio_space, align,
2994 false);
2995 if (ret) {
2996 dev_err(&hbus->hdev->device,
2997 "Need %#llx of high MMIO space. Consider reconfiguring the VM.\n",
2998 hbus->high_mmio_space);
2999 goto release_low_mmio;
3000 }
3001
3002 /* Modify this resource to become a bridge window. */
3003 hbus->high_mmio_res->flags |= IORESOURCE_WINDOW;
3004 hbus->high_mmio_res->flags &= ~IORESOURCE_BUSY;
Arnd Bergmann418cb6c2021-07-27 02:06:54 +08003005 pci_add_resource(&hbus->bridge->windows, hbus->high_mmio_res);
Jake Oshins4daace02016-02-16 21:56:23 +00003006 }
3007
3008 return 0;
3009
3010release_low_mmio:
3011 if (hbus->low_mmio_res) {
Jake Oshins696ca5e2016-04-05 10:22:52 -07003012 vmbus_free_mmio(hbus->low_mmio_res->start,
3013 resource_size(hbus->low_mmio_res));
Jake Oshins4daace02016-02-16 21:56:23 +00003014 }
3015
3016 return ret;
3017}
3018
3019/**
3020 * hv_allocate_config_window() - Find MMIO space for PCI Config
3021 * @hbus: Root PCI bus, as understood by this driver
3022 *
3023 * This function claims memory-mapped I/O space for accessing
3024 * configuration space for the functions on this bus.
3025 *
3026 * Return: 0 on success, -errno on failure
3027 */
3028static int hv_allocate_config_window(struct hv_pcibus_device *hbus)
3029{
3030 int ret;
3031
3032 /*
3033 * Set up a region of MMIO space to use for accessing configuration
3034 * space.
3035 */
3036 ret = vmbus_allocate_mmio(&hbus->mem_config, hbus->hdev, 0, -1,
3037 PCI_CONFIG_MMIO_LENGTH, 0x1000, false);
3038 if (ret)
3039 return ret;
3040
3041 /*
3042 * vmbus_allocate_mmio() gets used for allocating both device endpoint
3043 * resource claims (those which cannot be overlapped) and the ranges
3044 * which are valid for the children of this bus, which are intended
3045 * to be overlapped by those children. Set the flag on this claim
3046 * meaning that this region can't be overlapped.
3047 */
3048
3049 hbus->mem_config->flags |= IORESOURCE_BUSY;
3050
3051 return 0;
3052}
3053
3054static void hv_free_config_window(struct hv_pcibus_device *hbus)
3055{
Jake Oshins696ca5e2016-04-05 10:22:52 -07003056 vmbus_free_mmio(hbus->mem_config->start, PCI_CONFIG_MMIO_LENGTH);
Jake Oshins4daace02016-02-16 21:56:23 +00003057}
3058
Wei Huc81992e2020-05-07 13:03:00 +08003059static int hv_pci_bus_exit(struct hv_device *hdev, bool keep_devs);
3060
Jake Oshins4daace02016-02-16 21:56:23 +00003061/**
3062 * hv_pci_enter_d0() - Bring the "bus" into the D0 power state
3063 * @hdev: VMBus's tracking struct for this root PCI bus
3064 *
3065 * Return: 0 on success, -errno on failure
3066 */
3067static int hv_pci_enter_d0(struct hv_device *hdev)
3068{
3069 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
3070 struct pci_bus_d0_entry *d0_entry;
3071 struct hv_pci_compl comp_pkt;
3072 struct pci_packet *pkt;
3073 int ret;
3074
3075 /*
3076 * Tell the host that the bus is ready to use, and moved into the
3077 * powered-on state. This includes telling the host which region
3078 * of memory-mapped I/O space has been chosen for configuration space
3079 * access.
3080 */
3081 pkt = kzalloc(sizeof(*pkt) + sizeof(*d0_entry), GFP_KERNEL);
3082 if (!pkt)
3083 return -ENOMEM;
3084
3085 init_completion(&comp_pkt.host_event);
3086 pkt->completion_func = hv_pci_generic_compl;
3087 pkt->compl_ctxt = &comp_pkt;
3088 d0_entry = (struct pci_bus_d0_entry *)&pkt->message;
Dexuan Cui0c6045d2016-08-23 04:45:51 +00003089 d0_entry->message_type.type = PCI_BUS_D0ENTRY;
Jake Oshins4daace02016-02-16 21:56:23 +00003090 d0_entry->mmio_base = hbus->mem_config->start;
3091
3092 ret = vmbus_sendpacket(hdev->channel, d0_entry, sizeof(*d0_entry),
3093 (unsigned long)pkt, VM_PKT_DATA_INBAND,
3094 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
Dexuan Cuic3635da2018-05-23 21:12:01 +00003095 if (!ret)
3096 ret = wait_for_response(hdev, &comp_pkt.host_event);
3097
Jake Oshins4daace02016-02-16 21:56:23 +00003098 if (ret)
3099 goto exit;
3100
Jake Oshins4daace02016-02-16 21:56:23 +00003101 if (comp_pkt.completion_status < 0) {
3102 dev_err(&hdev->device,
3103 "PCI Pass-through VSP failed D0 Entry with status %x\n",
3104 comp_pkt.completion_status);
3105 ret = -EPROTO;
3106 goto exit;
3107 }
3108
3109 ret = 0;
3110
3111exit:
3112 kfree(pkt);
3113 return ret;
3114}
3115
3116/**
3117 * hv_pci_query_relations() - Ask host to send list of child
3118 * devices
3119 * @hdev: VMBus's tracking struct for this root PCI bus
3120 *
3121 * Return: 0 on success, -errno on failure
3122 */
3123static int hv_pci_query_relations(struct hv_device *hdev)
3124{
3125 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
3126 struct pci_message message;
3127 struct completion comp;
3128 int ret;
3129
3130 /* Ask the host to send along the list of child devices */
3131 init_completion(&comp);
3132 if (cmpxchg(&hbus->survey_event, NULL, &comp))
3133 return -ENOTEMPTY;
3134
3135 memset(&message, 0, sizeof(message));
Dexuan Cui0c6045d2016-08-23 04:45:51 +00003136 message.type = PCI_QUERY_BUS_RELATIONS;
Jake Oshins4daace02016-02-16 21:56:23 +00003137
3138 ret = vmbus_sendpacket(hdev->channel, &message, sizeof(message),
3139 0, VM_PKT_DATA_INBAND, 0);
Dexuan Cuic3635da2018-05-23 21:12:01 +00003140 if (!ret)
3141 ret = wait_for_response(hdev, &comp);
Jake Oshins4daace02016-02-16 21:56:23 +00003142
Dexuan Cuic3635da2018-05-23 21:12:01 +00003143 return ret;
Jake Oshins4daace02016-02-16 21:56:23 +00003144}
3145
3146/**
3147 * hv_send_resources_allocated() - Report local resource choices
3148 * @hdev: VMBus's tracking struct for this root PCI bus
3149 *
3150 * The host OS is expecting to be sent a request as a message
3151 * which contains all the resources that the device will use.
3152 * The response contains those same resources, "translated"
3153 * which is to say, the values which should be used by the
3154 * hardware, when it delivers an interrupt. (MMIO resources are
3155 * used in local terms.) This is nice for Windows, and lines up
3156 * with the FDO/PDO split, which doesn't exist in Linux. Linux
3157 * is deeply expecting to scan an emulated PCI configuration
3158 * space. So this message is sent here only to drive the state
3159 * machine on the host forward.
3160 *
3161 * Return: 0 on success, -errno on failure
3162 */
3163static int hv_send_resources_allocated(struct hv_device *hdev)
3164{
3165 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
3166 struct pci_resources_assigned *res_assigned;
Jork Loeser7dcf90e2017-05-24 13:41:28 -07003167 struct pci_resources_assigned2 *res_assigned2;
Jake Oshins4daace02016-02-16 21:56:23 +00003168 struct hv_pci_compl comp_pkt;
3169 struct hv_pci_dev *hpdev;
3170 struct pci_packet *pkt;
Jork Loeser7dcf90e2017-05-24 13:41:28 -07003171 size_t size_res;
Wei Hu83cc3502020-05-07 13:02:11 +08003172 int wslot;
Jake Oshins4daace02016-02-16 21:56:23 +00003173 int ret;
3174
Dexuan Cui14ef39f2019-11-24 21:33:53 -08003175 size_res = (hbus->protocol_version < PCI_PROTOCOL_VERSION_1_2)
Jork Loeser7dcf90e2017-05-24 13:41:28 -07003176 ? sizeof(*res_assigned) : sizeof(*res_assigned2);
3177
3178 pkt = kmalloc(sizeof(*pkt) + size_res, GFP_KERNEL);
Jake Oshins4daace02016-02-16 21:56:23 +00003179 if (!pkt)
3180 return -ENOMEM;
3181
3182 ret = 0;
3183
3184 for (wslot = 0; wslot < 256; wslot++) {
3185 hpdev = get_pcichild_wslot(hbus, wslot);
3186 if (!hpdev)
3187 continue;
3188
Jork Loeser7dcf90e2017-05-24 13:41:28 -07003189 memset(pkt, 0, sizeof(*pkt) + size_res);
Jake Oshins4daace02016-02-16 21:56:23 +00003190 init_completion(&comp_pkt.host_event);
3191 pkt->completion_func = hv_pci_generic_compl;
3192 pkt->compl_ctxt = &comp_pkt;
Jake Oshins4daace02016-02-16 21:56:23 +00003193
Dexuan Cui14ef39f2019-11-24 21:33:53 -08003194 if (hbus->protocol_version < PCI_PROTOCOL_VERSION_1_2) {
Jork Loeser7dcf90e2017-05-24 13:41:28 -07003195 res_assigned =
3196 (struct pci_resources_assigned *)&pkt->message;
3197 res_assigned->message_type.type =
3198 PCI_RESOURCES_ASSIGNED;
3199 res_assigned->wslot.slot = hpdev->desc.win_slot.slot;
3200 } else {
3201 res_assigned2 =
3202 (struct pci_resources_assigned2 *)&pkt->message;
3203 res_assigned2->message_type.type =
3204 PCI_RESOURCES_ASSIGNED2;
3205 res_assigned2->wslot.slot = hpdev->desc.win_slot.slot;
3206 }
Stephen Hemminger8c99e122018-05-23 10:11:12 -07003207 put_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +00003208
Jork Loeser7dcf90e2017-05-24 13:41:28 -07003209 ret = vmbus_sendpacket(hdev->channel, &pkt->message,
3210 size_res, (unsigned long)pkt,
3211 VM_PKT_DATA_INBAND,
3212 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
Dexuan Cuic3635da2018-05-23 21:12:01 +00003213 if (!ret)
3214 ret = wait_for_response(hdev, &comp_pkt.host_event);
Jake Oshins4daace02016-02-16 21:56:23 +00003215 if (ret)
3216 break;
3217
Jake Oshins4daace02016-02-16 21:56:23 +00003218 if (comp_pkt.completion_status < 0) {
3219 ret = -EPROTO;
3220 dev_err(&hdev->device,
3221 "resource allocated returned 0x%x",
3222 comp_pkt.completion_status);
3223 break;
3224 }
Wei Hu83cc3502020-05-07 13:02:11 +08003225
3226 hbus->wslot_res_allocated = wslot;
Jake Oshins4daace02016-02-16 21:56:23 +00003227 }
3228
3229 kfree(pkt);
3230 return ret;
3231}
3232
3233/**
3234 * hv_send_resources_released() - Report local resources
3235 * released
3236 * @hdev: VMBus's tracking struct for this root PCI bus
3237 *
3238 * Return: 0 on success, -errno on failure
3239 */
3240static int hv_send_resources_released(struct hv_device *hdev)
3241{
3242 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
3243 struct pci_child_message pkt;
3244 struct hv_pci_dev *hpdev;
Wei Hu83cc3502020-05-07 13:02:11 +08003245 int wslot;
Jake Oshins4daace02016-02-16 21:56:23 +00003246 int ret;
3247
Wei Hu83cc3502020-05-07 13:02:11 +08003248 for (wslot = hbus->wslot_res_allocated; wslot >= 0; wslot--) {
Jake Oshins4daace02016-02-16 21:56:23 +00003249 hpdev = get_pcichild_wslot(hbus, wslot);
3250 if (!hpdev)
3251 continue;
3252
3253 memset(&pkt, 0, sizeof(pkt));
Dexuan Cui0c6045d2016-08-23 04:45:51 +00003254 pkt.message_type.type = PCI_RESOURCES_RELEASED;
Jake Oshins4daace02016-02-16 21:56:23 +00003255 pkt.wslot.slot = hpdev->desc.win_slot.slot;
3256
Stephen Hemminger8c99e122018-05-23 10:11:12 -07003257 put_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +00003258
3259 ret = vmbus_sendpacket(hdev->channel, &pkt, sizeof(pkt), 0,
3260 VM_PKT_DATA_INBAND, 0);
3261 if (ret)
3262 return ret;
Wei Hu83cc3502020-05-07 13:02:11 +08003263
3264 hbus->wslot_res_allocated = wslot - 1;
Jake Oshins4daace02016-02-16 21:56:23 +00003265 }
3266
Wei Hu83cc3502020-05-07 13:02:11 +08003267 hbus->wslot_res_allocated = -1;
3268
Jake Oshins4daace02016-02-16 21:56:23 +00003269 return 0;
3270}
3271
Haiyang Zhangbe700102019-08-15 17:01:37 +00003272#define HVPCI_DOM_MAP_SIZE (64 * 1024)
3273static DECLARE_BITMAP(hvpci_dom_map, HVPCI_DOM_MAP_SIZE);
3274
3275/*
3276 * PCI domain number 0 is used by emulated devices on Gen1 VMs, so define 0
3277 * as invalid for passthrough PCI devices of this driver.
3278 */
3279#define HVPCI_DOM_INVALID 0
3280
3281/**
3282 * hv_get_dom_num() - Get a valid PCI domain number
3283 * Check if the PCI domain number is in use, and return another number if
3284 * it is in use.
3285 *
3286 * @dom: Requested domain number
3287 *
3288 * return: domain number on success, HVPCI_DOM_INVALID on failure
3289 */
3290static u16 hv_get_dom_num(u16 dom)
3291{
3292 unsigned int i;
3293
3294 if (test_and_set_bit(dom, hvpci_dom_map) == 0)
3295 return dom;
3296
3297 for_each_clear_bit(i, hvpci_dom_map, HVPCI_DOM_MAP_SIZE) {
3298 if (test_and_set_bit(i, hvpci_dom_map) == 0)
3299 return i;
3300 }
3301
3302 return HVPCI_DOM_INVALID;
3303}
3304
3305/**
3306 * hv_put_dom_num() - Mark the PCI domain number as free
3307 * @dom: Domain number to be freed
3308 */
3309static void hv_put_dom_num(u16 dom)
3310{
3311 clear_bit(dom, hvpci_dom_map);
3312}
3313
Jake Oshins4daace02016-02-16 21:56:23 +00003314/**
3315 * hv_pci_probe() - New VMBus channel probe, for a root PCI bus
3316 * @hdev: VMBus's tracking struct for this root PCI bus
3317 * @dev_id: Identifies the device itself
3318 *
3319 * Return: 0 on success, -errno on failure
3320 */
3321static int hv_pci_probe(struct hv_device *hdev,
3322 const struct hv_vmbus_device_id *dev_id)
3323{
Arnd Bergmann418cb6c2021-07-27 02:06:54 +08003324 struct pci_host_bridge *bridge;
Jake Oshins4daace02016-02-16 21:56:23 +00003325 struct hv_pcibus_device *hbus;
Haiyang Zhangbe700102019-08-15 17:01:37 +00003326 u16 dom_req, dom;
Marc Zyngier467a3bb2019-08-06 15:23:33 +01003327 char *name;
Wei Hud6af2ed2020-07-27 15:17:31 +08003328 bool enter_d0_retry = true;
Jake Oshins4daace02016-02-16 21:56:23 +00003329 int ret;
3330
Jork Loeserbe66b672017-05-24 13:41:25 -07003331 /*
3332 * hv_pcibus_device contains the hypercall arguments for retargeting in
3333 * hv_irq_unmask(). Those must not cross a page boundary.
3334 */
Dexuan Cui877b9112019-11-24 21:33:54 -08003335 BUILD_BUG_ON(sizeof(*hbus) > HV_HYP_PAGE_SIZE);
Jork Loeserbe66b672017-05-24 13:41:25 -07003336
Arnd Bergmann418cb6c2021-07-27 02:06:54 +08003337 bridge = devm_pci_alloc_host_bridge(&hdev->device, 0);
3338 if (!bridge)
3339 return -ENOMEM;
3340
Dexuan Cui877b9112019-11-24 21:33:54 -08003341 /*
3342 * With the recent 59bb47985c1d ("mm, sl[aou]b: guarantee natural
3343 * alignment for kmalloc(power-of-two)"), kzalloc() is able to allocate
3344 * a 4KB buffer that is guaranteed to be 4KB-aligned. Here the size and
3345 * alignment of hbus is important because hbus's field
3346 * retarget_msi_interrupt_params must not cross a 4KB page boundary.
3347 *
3348 * Here we prefer kzalloc to get_zeroed_page(), because a buffer
3349 * allocated by the latter is not tracked and scanned by kmemleak, and
3350 * hence kmemleak reports the pointer contained in the hbus buffer
3351 * (i.e. the hpdev struct, which is created in new_pcichild_device() and
3352 * is tracked by hbus->children) as memory leak (false positive).
3353 *
3354 * If the kernel doesn't have 59bb47985c1d, get_zeroed_page() *must* be
3355 * used to allocate the hbus buffer and we can avoid the kmemleak false
3356 * positive by using kmemleak_alloc() and kmemleak_free() to ask
3357 * kmemleak to track and scan the hbus buffer.
3358 */
Dexuan Cuie658a4f2020-02-21 21:59:56 -08003359 hbus = kzalloc(HV_HYP_PAGE_SIZE, GFP_KERNEL);
Jake Oshins4daace02016-02-16 21:56:23 +00003360 if (!hbus)
3361 return -ENOMEM;
Arnd Bergmann418cb6c2021-07-27 02:06:54 +08003362
3363 hbus->bridge = bridge;
Long Lid3a78d82017-03-23 14:58:10 -07003364 hbus->state = hv_pcibus_init;
Wei Hu83cc3502020-05-07 13:02:11 +08003365 hbus->wslot_res_allocated = -1;
Jake Oshins4daace02016-02-16 21:56:23 +00003366
3367 /*
Haiyang Zhangbe700102019-08-15 17:01:37 +00003368 * The PCI bus "domain" is what is called "segment" in ACPI and other
3369 * specs. Pull it from the instance ID, to get something usually
3370 * unique. In rare cases of collision, we will find out another number
3371 * not in use.
3372 *
3373 * Note that, since this code only runs in a Hyper-V VM, Hyper-V
3374 * together with this guest driver can guarantee that (1) The only
3375 * domain used by Gen1 VMs for something that looks like a physical
3376 * PCI bus (which is actually emulated by the hypervisor) is domain 0.
3377 * (2) There will be no overlap between domains (after fixing possible
3378 * collisions) in the same VM.
Jake Oshins4daace02016-02-16 21:56:23 +00003379 */
Haiyang Zhangf73f8a52019-08-15 17:01:45 +00003380 dom_req = hdev->dev_instance.b[5] << 8 | hdev->dev_instance.b[4];
Haiyang Zhangbe700102019-08-15 17:01:37 +00003381 dom = hv_get_dom_num(dom_req);
3382
3383 if (dom == HVPCI_DOM_INVALID) {
3384 dev_err(&hdev->device,
Krzysztof Wilczyńskif1831202021-10-08 22:27:30 +00003385 "Unable to use dom# 0x%x or other numbers", dom_req);
Haiyang Zhangbe700102019-08-15 17:01:37 +00003386 ret = -EINVAL;
3387 goto free_bus;
3388 }
3389
3390 if (dom != dom_req)
3391 dev_info(&hdev->device,
Krzysztof Wilczyńskif1831202021-10-08 22:27:30 +00003392 "PCI dom# 0x%x has collision, using 0x%x",
Haiyang Zhangbe700102019-08-15 17:01:37 +00003393 dom_req, dom);
3394
Boqun Feng38c0d262021-07-27 02:06:55 +08003395 hbus->bridge->domain_nr = dom;
Boqun Feng88f94c72021-07-27 02:06:57 +08003396#ifdef CONFIG_X86
Haiyang Zhangbe700102019-08-15 17:01:37 +00003397 hbus->sysdata.domain = dom;
Boqun Feng88f94c72021-07-27 02:06:57 +08003398#endif
Jake Oshins4daace02016-02-16 21:56:23 +00003399
3400 hbus->hdev = hdev;
Jake Oshins4daace02016-02-16 21:56:23 +00003401 INIT_LIST_HEAD(&hbus->children);
3402 INIT_LIST_HEAD(&hbus->dr_list);
Jake Oshins4daace02016-02-16 21:56:23 +00003403 spin_lock_init(&hbus->config_lock);
3404 spin_lock_init(&hbus->device_list_lock);
Long Li0de8ce32016-11-08 14:04:38 -08003405 spin_lock_init(&hbus->retarget_msi_interrupt_lock);
Dexuan Cui021ad272018-03-15 14:20:53 +00003406 hbus->wq = alloc_ordered_workqueue("hv_pci_%x", 0,
Boqun Feng38c0d262021-07-27 02:06:55 +08003407 hbus->bridge->domain_nr);
Dexuan Cui021ad272018-03-15 14:20:53 +00003408 if (!hbus->wq) {
3409 ret = -ENOMEM;
Haiyang Zhangbe700102019-08-15 17:01:37 +00003410 goto free_dom;
Dexuan Cui021ad272018-03-15 14:20:53 +00003411 }
Jake Oshins4daace02016-02-16 21:56:23 +00003412
3413 ret = vmbus_open(hdev->channel, pci_ring_size, pci_ring_size, NULL, 0,
3414 hv_pci_onchannelcallback, hbus);
3415 if (ret)
Dexuan Cui021ad272018-03-15 14:20:53 +00003416 goto destroy_wq;
Jake Oshins4daace02016-02-16 21:56:23 +00003417
3418 hv_set_drvdata(hdev, hbus);
3419
Dexuan Cuia8e37502019-11-24 21:33:51 -08003420 ret = hv_pci_protocol_negotiation(hdev, pci_protocol_versions,
3421 ARRAY_SIZE(pci_protocol_versions));
Jake Oshins4daace02016-02-16 21:56:23 +00003422 if (ret)
3423 goto close;
3424
3425 ret = hv_allocate_config_window(hbus);
3426 if (ret)
3427 goto close;
3428
3429 hbus->cfg_addr = ioremap(hbus->mem_config->start,
3430 PCI_CONFIG_MMIO_LENGTH);
3431 if (!hbus->cfg_addr) {
3432 dev_err(&hdev->device,
3433 "Unable to map a virtual address for config space\n");
3434 ret = -ENOMEM;
3435 goto free_config;
3436 }
3437
Marc Zyngier467a3bb2019-08-06 15:23:33 +01003438 name = kasprintf(GFP_KERNEL, "%pUL", &hdev->dev_instance);
3439 if (!name) {
3440 ret = -ENOMEM;
3441 goto unmap;
3442 }
3443
Boqun Feng9e7f9172021-07-27 02:06:56 +08003444 hbus->fwnode = irq_domain_alloc_named_fwnode(name);
Marc Zyngier467a3bb2019-08-06 15:23:33 +01003445 kfree(name);
Boqun Feng9e7f9172021-07-27 02:06:56 +08003446 if (!hbus->fwnode) {
Jake Oshins4daace02016-02-16 21:56:23 +00003447 ret = -ENOMEM;
3448 goto unmap;
3449 }
3450
3451 ret = hv_pcie_init_irq_domain(hbus);
3452 if (ret)
3453 goto free_fwnode;
3454
Wei Hud6af2ed2020-07-27 15:17:31 +08003455retry:
Jake Oshins4daace02016-02-16 21:56:23 +00003456 ret = hv_pci_query_relations(hdev);
3457 if (ret)
3458 goto free_irq_domain;
3459
3460 ret = hv_pci_enter_d0(hdev);
Wei Hud6af2ed2020-07-27 15:17:31 +08003461 /*
3462 * In certain case (Kdump) the pci device of interest was
3463 * not cleanly shut down and resource is still held on host
3464 * side, the host could return invalid device status.
3465 * We need to explicitly request host to release the resource
3466 * and try to enter D0 again.
3467 * Since the hv_pci_bus_exit() call releases structures
3468 * of all its child devices, we need to start the retry from
3469 * hv_pci_query_relations() call, requesting host to send
3470 * the synchronous child device relations message before this
3471 * information is needed in hv_send_resources_allocated()
3472 * call later.
3473 */
3474 if (ret == -EPROTO && enter_d0_retry) {
3475 enter_d0_retry = false;
3476
3477 dev_err(&hdev->device, "Retrying D0 Entry\n");
3478
3479 /*
3480 * Hv_pci_bus_exit() calls hv_send_resources_released()
3481 * to free up resources of its child devices.
3482 * In the kdump kernel we need to set the
3483 * wslot_res_allocated to 255 so it scans all child
3484 * devices to release resources allocated in the
3485 * normal kernel before panic happened.
3486 */
3487 hbus->wslot_res_allocated = 255;
3488 ret = hv_pci_bus_exit(hdev, true);
3489
3490 if (ret == 0)
3491 goto retry;
3492
3493 dev_err(&hdev->device,
3494 "Retrying D0 failed with ret %d\n", ret);
3495 }
Jake Oshins4daace02016-02-16 21:56:23 +00003496 if (ret)
3497 goto free_irq_domain;
3498
3499 ret = hv_pci_allocate_bridge_windows(hbus);
3500 if (ret)
Wei Hu83cc3502020-05-07 13:02:11 +08003501 goto exit_d0;
Jake Oshins4daace02016-02-16 21:56:23 +00003502
3503 ret = hv_send_resources_allocated(hdev);
3504 if (ret)
3505 goto free_windows;
3506
3507 prepopulate_bars(hbus);
3508
3509 hbus->state = hv_pcibus_probed;
3510
3511 ret = create_root_hv_pci_bus(hbus);
3512 if (ret)
3513 goto free_windows;
3514
3515 return 0;
3516
3517free_windows:
3518 hv_pci_free_bridge_windows(hbus);
Wei Hu83cc3502020-05-07 13:02:11 +08003519exit_d0:
3520 (void) hv_pci_bus_exit(hdev, true);
Jake Oshins4daace02016-02-16 21:56:23 +00003521free_irq_domain:
3522 irq_domain_remove(hbus->irq_domain);
3523free_fwnode:
Boqun Feng9e7f9172021-07-27 02:06:56 +08003524 irq_domain_free_fwnode(hbus->fwnode);
Jake Oshins4daace02016-02-16 21:56:23 +00003525unmap:
3526 iounmap(hbus->cfg_addr);
3527free_config:
3528 hv_free_config_window(hbus);
3529close:
3530 vmbus_close(hdev->channel);
Dexuan Cui021ad272018-03-15 14:20:53 +00003531destroy_wq:
3532 destroy_workqueue(hbus->wq);
Haiyang Zhangbe700102019-08-15 17:01:37 +00003533free_dom:
Boqun Feng38c0d262021-07-27 02:06:55 +08003534 hv_put_dom_num(hbus->bridge->domain_nr);
Jake Oshins4daace02016-02-16 21:56:23 +00003535free_bus:
Dexuan Cui42c3d412020-02-21 21:59:57 -08003536 kfree(hbus);
Jake Oshins4daace02016-02-16 21:56:23 +00003537 return ret;
3538}
3539
Wei Huc81992e2020-05-07 13:03:00 +08003540static int hv_pci_bus_exit(struct hv_device *hdev, bool keep_devs)
Jake Oshins4daace02016-02-16 21:56:23 +00003541{
Dexuan Cui179785242016-11-10 07:18:47 +00003542 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
3543 struct {
Jake Oshins4daace02016-02-16 21:56:23 +00003544 struct pci_packet teardown_packet;
Dexuan Cui179785242016-11-10 07:18:47 +00003545 u8 buffer[sizeof(struct pci_message)];
Jake Oshins4daace02016-02-16 21:56:23 +00003546 } pkt;
Jake Oshins4daace02016-02-16 21:56:23 +00003547 struct hv_pci_compl comp_pkt;
Long Li94d22762021-05-12 01:06:40 -07003548 struct hv_pci_dev *hpdev, *tmp;
3549 unsigned long flags;
Dexuan Cui179785242016-11-10 07:18:47 +00003550 int ret;
Jake Oshins4daace02016-02-16 21:56:23 +00003551
Dexuan Cui179785242016-11-10 07:18:47 +00003552 /*
3553 * After the host sends the RESCIND_CHANNEL message, it doesn't
3554 * access the per-channel ringbuffer any longer.
3555 */
3556 if (hdev->channel->rescind)
Dexuan Cuia8e37502019-11-24 21:33:51 -08003557 return 0;
Dexuan Cui179785242016-11-10 07:18:47 +00003558
Wei Huc81992e2020-05-07 13:03:00 +08003559 if (!keep_devs) {
Long Li41608b62021-08-30 16:13:27 -07003560 struct list_head removed;
3561
3562 /* Move all present children to the list on stack */
3563 INIT_LIST_HEAD(&removed);
Long Li94d22762021-05-12 01:06:40 -07003564 spin_lock_irqsave(&hbus->device_list_lock, flags);
Long Li41608b62021-08-30 16:13:27 -07003565 list_for_each_entry_safe(hpdev, tmp, &hbus->children, list_entry)
3566 list_move_tail(&hpdev->list_entry, &removed);
3567 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
3568
3569 /* Remove all children in the list */
3570 list_for_each_entry_safe(hpdev, tmp, &removed, list_entry) {
Long Li94d22762021-05-12 01:06:40 -07003571 list_del(&hpdev->list_entry);
3572 if (hpdev->pci_slot)
3573 pci_destroy_slot(hpdev->pci_slot);
3574 /* For the two refs got in new_pcichild_device() */
3575 put_pcichild(hpdev);
3576 put_pcichild(hpdev);
3577 }
Dexuan Cuia8e37502019-11-24 21:33:51 -08003578 }
Dexuan Cui179785242016-11-10 07:18:47 +00003579
3580 ret = hv_send_resources_released(hdev);
Dexuan Cuia8e37502019-11-24 21:33:51 -08003581 if (ret) {
Dexuan Cui179785242016-11-10 07:18:47 +00003582 dev_err(&hdev->device,
3583 "Couldn't send resources released packet(s)\n");
Dexuan Cuia8e37502019-11-24 21:33:51 -08003584 return ret;
3585 }
Jake Oshins4daace02016-02-16 21:56:23 +00003586
Jake Oshins4daace02016-02-16 21:56:23 +00003587 memset(&pkt.teardown_packet, 0, sizeof(pkt.teardown_packet));
3588 init_completion(&comp_pkt.host_event);
3589 pkt.teardown_packet.completion_func = hv_pci_generic_compl;
3590 pkt.teardown_packet.compl_ctxt = &comp_pkt;
Dexuan Cui0c6045d2016-08-23 04:45:51 +00003591 pkt.teardown_packet.message[0].type = PCI_BUS_D0EXIT;
Jake Oshins4daace02016-02-16 21:56:23 +00003592
3593 ret = vmbus_sendpacket(hdev->channel, &pkt.teardown_packet.message,
3594 sizeof(struct pci_message),
3595 (unsigned long)&pkt.teardown_packet,
3596 VM_PKT_DATA_INBAND,
3597 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
Dexuan Cuia8e37502019-11-24 21:33:51 -08003598 if (ret)
3599 return ret;
3600
3601 if (wait_for_completion_timeout(&comp_pkt.host_event, 10 * HZ) == 0)
3602 return -ETIMEDOUT;
3603
3604 return 0;
Dexuan Cui179785242016-11-10 07:18:47 +00003605}
Jake Oshins4daace02016-02-16 21:56:23 +00003606
Dexuan Cui179785242016-11-10 07:18:47 +00003607/**
3608 * hv_pci_remove() - Remove routine for this VMBus channel
3609 * @hdev: VMBus's tracking struct for this root PCI bus
3610 *
3611 * Return: 0 on success, -errno on failure
3612 */
3613static int hv_pci_remove(struct hv_device *hdev)
3614{
3615 struct hv_pcibus_device *hbus;
Dexuan Cuia8e37502019-11-24 21:33:51 -08003616 int ret;
Dexuan Cui179785242016-11-10 07:18:47 +00003617
3618 hbus = hv_get_drvdata(hdev);
Jake Oshins4daace02016-02-16 21:56:23 +00003619 if (hbus->state == hv_pcibus_installed) {
Long Li94d22762021-05-12 01:06:40 -07003620 tasklet_disable(&hdev->channel->callback_event);
3621 hbus->state = hv_pcibus_removing;
3622 tasklet_enable(&hdev->channel->callback_event);
3623 destroy_workqueue(hbus->wq);
3624 hbus->wq = NULL;
3625 /*
3626 * At this point, no work is running or can be scheduled
3627 * on hbus-wq. We can't race with hv_pci_devices_present()
3628 * or hv_pci_eject_device(), it's safe to proceed.
3629 */
3630
Jake Oshins4daace02016-02-16 21:56:23 +00003631 /* Remove the bus from PCI's point of view. */
3632 pci_lock_rescan_remove();
Arnd Bergmann418cb6c2021-07-27 02:06:54 +08003633 pci_stop_root_bus(hbus->bridge->bus);
Dexuan Cui15becc22019-03-04 21:34:48 +00003634 hv_pci_remove_slots(hbus);
Arnd Bergmann418cb6c2021-07-27 02:06:54 +08003635 pci_remove_root_bus(hbus->bridge->bus);
Jake Oshins4daace02016-02-16 21:56:23 +00003636 pci_unlock_rescan_remove();
3637 }
3638
Dexuan Cuia8e37502019-11-24 21:33:51 -08003639 ret = hv_pci_bus_exit(hdev, false);
Vitaly Kuznetsovdeb22e52016-04-29 11:39:10 +02003640
Jake Oshins4daace02016-02-16 21:56:23 +00003641 vmbus_close(hdev->channel);
3642
Jake Oshins4daace02016-02-16 21:56:23 +00003643 iounmap(hbus->cfg_addr);
3644 hv_free_config_window(hbus);
Jake Oshins4daace02016-02-16 21:56:23 +00003645 hv_pci_free_bridge_windows(hbus);
3646 irq_domain_remove(hbus->irq_domain);
Boqun Feng9e7f9172021-07-27 02:06:56 +08003647 irq_domain_free_fwnode(hbus->fwnode);
Haiyang Zhangbe700102019-08-15 17:01:37 +00003648
Boqun Feng38c0d262021-07-27 02:06:55 +08003649 hv_put_dom_num(hbus->bridge->domain_nr);
Haiyang Zhangbe700102019-08-15 17:01:37 +00003650
Dexuan Cui877b9112019-11-24 21:33:54 -08003651 kfree(hbus);
Dexuan Cuia8e37502019-11-24 21:33:51 -08003652 return ret;
Jake Oshins4daace02016-02-16 21:56:23 +00003653}
3654
Dexuan Cuiac82fc82019-11-24 21:33:52 -08003655static int hv_pci_suspend(struct hv_device *hdev)
3656{
3657 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
3658 enum hv_pcibus_state old_state;
3659 int ret;
3660
3661 /*
3662 * hv_pci_suspend() must make sure there are no pending work items
3663 * before calling vmbus_close(), since it runs in a process context
3664 * as a callback in dpm_suspend(). When it starts to run, the channel
3665 * callback hv_pci_onchannelcallback(), which runs in a tasklet
3666 * context, can be still running concurrently and scheduling new work
3667 * items onto hbus->wq in hv_pci_devices_present() and
3668 * hv_pci_eject_device(), and the work item handlers can access the
3669 * vmbus channel, which can be being closed by hv_pci_suspend(), e.g.
3670 * the work item handler pci_devices_present_work() ->
3671 * new_pcichild_device() writes to the vmbus channel.
3672 *
3673 * To eliminate the race, hv_pci_suspend() disables the channel
3674 * callback tasklet, sets hbus->state to hv_pcibus_removing, and
3675 * re-enables the tasklet. This way, when hv_pci_suspend() proceeds,
3676 * it knows that no new work item can be scheduled, and then it flushes
3677 * hbus->wq and safely closes the vmbus channel.
3678 */
3679 tasklet_disable(&hdev->channel->callback_event);
3680
3681 /* Change the hbus state to prevent new work items. */
3682 old_state = hbus->state;
3683 if (hbus->state == hv_pcibus_installed)
3684 hbus->state = hv_pcibus_removing;
3685
3686 tasklet_enable(&hdev->channel->callback_event);
3687
3688 if (old_state != hv_pcibus_installed)
3689 return -EINVAL;
3690
3691 flush_workqueue(hbus->wq);
3692
3693 ret = hv_pci_bus_exit(hdev, true);
3694 if (ret)
3695 return ret;
3696
3697 vmbus_close(hdev->channel);
3698
Jake Oshins4daace02016-02-16 21:56:23 +00003699 return 0;
3700}
3701
Dexuan Cui915cff72020-10-02 01:51:58 -07003702static int hv_pci_restore_msi_msg(struct pci_dev *pdev, void *arg)
3703{
Dexuan Cui915cff72020-10-02 01:51:58 -07003704 struct irq_data *irq_data;
Thomas Gleixnerdc2b4532021-12-06 23:51:33 +01003705 struct msi_desc *entry;
3706 int ret = 0;
Dexuan Cui915cff72020-10-02 01:51:58 -07003707
Thomas Gleixnerdc2b4532021-12-06 23:51:33 +01003708 msi_lock_descs(&pdev->dev);
3709 msi_for_each_desc(entry, &pdev->dev, MSI_DESC_ASSOCIATED) {
Dexuan Cui915cff72020-10-02 01:51:58 -07003710 irq_data = irq_get_irq_data(entry->irq);
Thomas Gleixnerdc2b4532021-12-06 23:51:33 +01003711 if (WARN_ON_ONCE(!irq_data)) {
3712 ret = -EINVAL;
3713 break;
3714 }
Dexuan Cui915cff72020-10-02 01:51:58 -07003715
3716 hv_compose_msi_msg(irq_data, &entry->msg);
3717 }
Thomas Gleixnerdc2b4532021-12-06 23:51:33 +01003718 msi_unlock_descs(&pdev->dev);
Dexuan Cui915cff72020-10-02 01:51:58 -07003719
Thomas Gleixnerdc2b4532021-12-06 23:51:33 +01003720 return ret;
Dexuan Cui915cff72020-10-02 01:51:58 -07003721}
3722
3723/*
3724 * Upon resume, pci_restore_msi_state() -> ... -> __pci_write_msi_msg()
3725 * directly writes the MSI/MSI-X registers via MMIO, but since Hyper-V
3726 * doesn't trap and emulate the MMIO accesses, here hv_compose_msi_msg()
3727 * must be used to ask Hyper-V to re-create the IOMMU Interrupt Remapping
3728 * Table entries.
3729 */
3730static void hv_pci_restore_msi_state(struct hv_pcibus_device *hbus)
3731{
Arnd Bergmann418cb6c2021-07-27 02:06:54 +08003732 pci_walk_bus(hbus->bridge->bus, hv_pci_restore_msi_msg, NULL);
Dexuan Cui915cff72020-10-02 01:51:58 -07003733}
3734
Dexuan Cuiac82fc82019-11-24 21:33:52 -08003735static int hv_pci_resume(struct hv_device *hdev)
3736{
3737 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
3738 enum pci_protocol_version_t version[1];
3739 int ret;
3740
3741 hbus->state = hv_pcibus_init;
3742
3743 ret = vmbus_open(hdev->channel, pci_ring_size, pci_ring_size, NULL, 0,
3744 hv_pci_onchannelcallback, hbus);
3745 if (ret)
3746 return ret;
3747
3748 /* Only use the version that was in use before hibernation. */
Dexuan Cui14ef39f2019-11-24 21:33:53 -08003749 version[0] = hbus->protocol_version;
Dexuan Cuiac82fc82019-11-24 21:33:52 -08003750 ret = hv_pci_protocol_negotiation(hdev, version, 1);
3751 if (ret)
3752 goto out;
3753
3754 ret = hv_pci_query_relations(hdev);
3755 if (ret)
3756 goto out;
3757
3758 ret = hv_pci_enter_d0(hdev);
3759 if (ret)
3760 goto out;
3761
3762 ret = hv_send_resources_allocated(hdev);
3763 if (ret)
3764 goto out;
3765
3766 prepopulate_bars(hbus);
3767
Dexuan Cui915cff72020-10-02 01:51:58 -07003768 hv_pci_restore_msi_state(hbus);
3769
Dexuan Cuiac82fc82019-11-24 21:33:52 -08003770 hbus->state = hv_pcibus_installed;
3771 return 0;
3772out:
3773 vmbus_close(hdev->channel);
3774 return ret;
3775}
3776
Jake Oshins4daace02016-02-16 21:56:23 +00003777static const struct hv_vmbus_device_id hv_pci_id_table[] = {
3778 /* PCI Pass-through Class ID */
3779 /* 44C4F61D-4444-4400-9D52-802E27EDE19F */
3780 { HV_PCIE_GUID, },
3781 { },
3782};
3783
3784MODULE_DEVICE_TABLE(vmbus, hv_pci_id_table);
3785
3786static struct hv_driver hv_pci_drv = {
3787 .name = "hv_pci",
3788 .id_table = hv_pci_id_table,
3789 .probe = hv_pci_probe,
3790 .remove = hv_pci_remove,
Dexuan Cuiac82fc82019-11-24 21:33:52 -08003791 .suspend = hv_pci_suspend,
3792 .resume = hv_pci_resume,
Jake Oshins4daace02016-02-16 21:56:23 +00003793};
3794
3795static void __exit exit_hv_pci_drv(void)
3796{
3797 vmbus_driver_unregister(&hv_pci_drv);
Haiyang Zhang348dd932019-08-22 05:05:41 +00003798
3799 hvpci_block_ops.read_block = NULL;
3800 hvpci_block_ops.write_block = NULL;
3801 hvpci_block_ops.reg_blk_invalidate = NULL;
Jake Oshins4daace02016-02-16 21:56:23 +00003802}
3803
3804static int __init init_hv_pci_drv(void)
3805{
Sunil Muthuswamy831c1ae2022-01-05 11:32:35 -08003806 int ret;
3807
Haiyang Zhang7d815f42021-05-25 16:17:33 -07003808 if (!hv_is_hyperv_initialized())
3809 return -ENODEV;
3810
Sunil Muthuswamy831c1ae2022-01-05 11:32:35 -08003811 ret = hv_pci_irqchip_init();
3812 if (ret)
3813 return ret;
3814
Haiyang Zhangbe700102019-08-15 17:01:37 +00003815 /* Set the invalid domain number's bit, so it will not be used */
3816 set_bit(HVPCI_DOM_INVALID, hvpci_dom_map);
3817
Haiyang Zhang348dd932019-08-22 05:05:41 +00003818 /* Initialize PCI block r/w interface */
3819 hvpci_block_ops.read_block = hv_read_config_block;
3820 hvpci_block_ops.write_block = hv_write_config_block;
3821 hvpci_block_ops.reg_blk_invalidate = hv_register_block_invalidate;
3822
Jake Oshins4daace02016-02-16 21:56:23 +00003823 return vmbus_driver_register(&hv_pci_drv);
3824}
3825
3826module_init(init_hv_pci_drv);
3827module_exit(exit_hv_pci_drv);
3828
3829MODULE_DESCRIPTION("Hyper-V PCI");
3830MODULE_LICENSE("GPL v2");