blob: 272c63ac49f918156158844e1a5fbd5f575d0e18 [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>
Stephen Hemminger80bfeeb2017-07-31 16:48:29 -070043#include <linux/delay.h>
Jake Oshins4daace02016-02-16 21:56:23 +000044#include <linux/semaphore.h>
45#include <linux/irqdomain.h>
46#include <asm/irqdomain.h>
47#include <asm/apic.h>
Nicolai Stange447ae312018-07-29 12:15:33 +020048#include <linux/irq.h>
Jake Oshins4daace02016-02-16 21:56:23 +000049#include <linux/msi.h>
50#include <linux/hyperv.h>
Elena Reshetova24196f02017-04-18 09:02:48 -050051#include <linux/refcount.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 */
Jake Oshins4daace02016-02-16 21:56:23 +000067};
68
K. Y. Srinivasan433fcf62017-03-24 11:07:21 -070069#define CPU_AFFINITY_ALL -1ULL
Jork Loeserb1db7e72017-05-24 13:41:27 -070070
71/*
72 * Supported protocol versions in the order of probing - highest go
73 * first.
74 */
75static enum pci_protocol_version_t pci_protocol_versions[] = {
Long Li999dd952020-02-25 21:06:08 -080076 PCI_PROTOCOL_VERSION_1_3,
Jork Loeser7dcf90e2017-05-24 13:41:28 -070077 PCI_PROTOCOL_VERSION_1_2,
Jork Loeserb1db7e72017-05-24 13:41:27 -070078 PCI_PROTOCOL_VERSION_1_1,
79};
80
Jake Oshins4daace02016-02-16 21:56:23 +000081#define PCI_CONFIG_MMIO_LENGTH 0x2000
82#define CFG_PAGE_OFFSET 0x1000
83#define CFG_PAGE_SIZE (PCI_CONFIG_MMIO_LENGTH - CFG_PAGE_OFFSET)
84
85#define MAX_SUPPORTED_MSI_MESSAGES 0x400
86
Jork Loeserb1db7e72017-05-24 13:41:27 -070087#define STATUS_REVISION_MISMATCH 0xC0000059
88
Stephen Hemmingera15f2c082018-09-14 12:54:56 -070089/* space for 32bit serial number as string */
90#define SLOT_NAME_SIZE 11
91
Jake Oshins4daace02016-02-16 21:56:23 +000092/*
93 * Message Types
94 */
95
96enum pci_message_type {
97 /*
98 * Version 1.1
99 */
100 PCI_MESSAGE_BASE = 0x42490000,
101 PCI_BUS_RELATIONS = PCI_MESSAGE_BASE + 0,
102 PCI_QUERY_BUS_RELATIONS = PCI_MESSAGE_BASE + 1,
103 PCI_POWER_STATE_CHANGE = PCI_MESSAGE_BASE + 4,
104 PCI_QUERY_RESOURCE_REQUIREMENTS = PCI_MESSAGE_BASE + 5,
105 PCI_QUERY_RESOURCE_RESOURCES = PCI_MESSAGE_BASE + 6,
106 PCI_BUS_D0ENTRY = PCI_MESSAGE_BASE + 7,
107 PCI_BUS_D0EXIT = PCI_MESSAGE_BASE + 8,
108 PCI_READ_BLOCK = PCI_MESSAGE_BASE + 9,
109 PCI_WRITE_BLOCK = PCI_MESSAGE_BASE + 0xA,
110 PCI_EJECT = PCI_MESSAGE_BASE + 0xB,
111 PCI_QUERY_STOP = PCI_MESSAGE_BASE + 0xC,
112 PCI_REENABLE = PCI_MESSAGE_BASE + 0xD,
113 PCI_QUERY_STOP_FAILED = PCI_MESSAGE_BASE + 0xE,
114 PCI_EJECTION_COMPLETE = PCI_MESSAGE_BASE + 0xF,
115 PCI_RESOURCES_ASSIGNED = PCI_MESSAGE_BASE + 0x10,
116 PCI_RESOURCES_RELEASED = PCI_MESSAGE_BASE + 0x11,
117 PCI_INVALIDATE_BLOCK = PCI_MESSAGE_BASE + 0x12,
118 PCI_QUERY_PROTOCOL_VERSION = PCI_MESSAGE_BASE + 0x13,
119 PCI_CREATE_INTERRUPT_MESSAGE = PCI_MESSAGE_BASE + 0x14,
120 PCI_DELETE_INTERRUPT_MESSAGE = PCI_MESSAGE_BASE + 0x15,
Jork Loeser7dcf90e2017-05-24 13:41:28 -0700121 PCI_RESOURCES_ASSIGNED2 = PCI_MESSAGE_BASE + 0x16,
122 PCI_CREATE_INTERRUPT_MESSAGE2 = PCI_MESSAGE_BASE + 0x17,
123 PCI_DELETE_INTERRUPT_MESSAGE2 = PCI_MESSAGE_BASE + 0x18, /* unused */
Long Li999dd952020-02-25 21:06:08 -0800124 PCI_BUS_RELATIONS2 = PCI_MESSAGE_BASE + 0x19,
Jake Oshins4daace02016-02-16 21:56:23 +0000125 PCI_MESSAGE_MAXIMUM
126};
127
128/*
129 * Structures defining the virtual PCI Express protocol.
130 */
131
132union pci_version {
133 struct {
134 u16 minor_version;
135 u16 major_version;
136 } parts;
137 u32 version;
138} __packed;
139
140/*
141 * Function numbers are 8-bits wide on Express, as interpreted through ARI,
142 * which is all this driver does. This representation is the one used in
143 * Windows, which is what is expected when sending this back and forth with
144 * the Hyper-V parent partition.
145 */
146union win_slot_encoding {
147 struct {
Dexuan Cui60e2e2f2017-02-10 15:18:46 -0600148 u32 dev:5;
149 u32 func:3;
Jake Oshins4daace02016-02-16 21:56:23 +0000150 u32 reserved:24;
151 } bits;
152 u32 slot;
153} __packed;
154
155/*
156 * Pretty much as defined in the PCI Specifications.
157 */
158struct pci_function_description {
159 u16 v_id; /* vendor ID */
160 u16 d_id; /* device ID */
161 u8 rev;
162 u8 prog_intf;
163 u8 subclass;
164 u8 base_class;
165 u32 subsystem_id;
166 union win_slot_encoding win_slot;
167 u32 ser; /* serial number */
168} __packed;
169
Long Li999dd952020-02-25 21:06:08 -0800170enum pci_device_description_flags {
171 HV_PCI_DEVICE_FLAG_NONE = 0x0,
172 HV_PCI_DEVICE_FLAG_NUMA_AFFINITY = 0x1,
173};
174
175struct pci_function_description2 {
176 u16 v_id; /* vendor ID */
177 u16 d_id; /* device ID */
178 u8 rev;
179 u8 prog_intf;
180 u8 subclass;
181 u8 base_class;
182 u32 subsystem_id;
183 union win_slot_encoding win_slot;
184 u32 ser; /* serial number */
185 u32 flags;
186 u16 virtual_numa_node;
187 u16 reserved;
188} __packed;
189
Jake Oshins4daace02016-02-16 21:56:23 +0000190/**
191 * struct hv_msi_desc
192 * @vector: IDT entry
193 * @delivery_mode: As defined in Intel's Programmer's
194 * Reference Manual, Volume 3, Chapter 8.
195 * @vector_count: Number of contiguous entries in the
196 * Interrupt Descriptor Table that are
197 * occupied by this Message-Signaled
198 * Interrupt. For "MSI", as first defined
199 * in PCI 2.2, this can be between 1 and
200 * 32. For "MSI-X," as first defined in PCI
201 * 3.0, this must be 1, as each MSI-X table
202 * entry would have its own descriptor.
203 * @reserved: Empty space
204 * @cpu_mask: All the target virtual processors.
205 */
206struct hv_msi_desc {
207 u8 vector;
208 u8 delivery_mode;
209 u16 vector_count;
210 u32 reserved;
211 u64 cpu_mask;
212} __packed;
213
214/**
Jork Loeser7dcf90e2017-05-24 13:41:28 -0700215 * struct hv_msi_desc2 - 1.2 version of hv_msi_desc
216 * @vector: IDT entry
217 * @delivery_mode: As defined in Intel's Programmer's
218 * Reference Manual, Volume 3, Chapter 8.
219 * @vector_count: Number of contiguous entries in the
220 * Interrupt Descriptor Table that are
221 * occupied by this Message-Signaled
222 * Interrupt. For "MSI", as first defined
223 * in PCI 2.2, this can be between 1 and
224 * 32. For "MSI-X," as first defined in PCI
225 * 3.0, this must be 1, as each MSI-X table
226 * entry would have its own descriptor.
227 * @processor_count: number of bits enabled in array.
228 * @processor_array: All the target virtual processors.
229 */
230struct hv_msi_desc2 {
231 u8 vector;
232 u8 delivery_mode;
233 u16 vector_count;
234 u16 processor_count;
235 u16 processor_array[32];
236} __packed;
237
238/**
Jake Oshins4daace02016-02-16 21:56:23 +0000239 * struct tran_int_desc
240 * @reserved: unused, padding
241 * @vector_count: same as in hv_msi_desc
242 * @data: This is the "data payload" value that is
243 * written by the device when it generates
244 * a message-signaled interrupt, either MSI
245 * or MSI-X.
246 * @address: This is the address to which the data
247 * payload is written on interrupt
248 * generation.
249 */
250struct tran_int_desc {
251 u16 reserved;
252 u16 vector_count;
253 u32 data;
254 u64 address;
255} __packed;
256
257/*
258 * A generic message format for virtual PCI.
259 * Specific message formats are defined later in the file.
260 */
261
262struct pci_message {
Dexuan Cui0c6045d2016-08-23 04:45:51 +0000263 u32 type;
Jake Oshins4daace02016-02-16 21:56:23 +0000264} __packed;
265
266struct pci_child_message {
Dexuan Cui0c6045d2016-08-23 04:45:51 +0000267 struct pci_message message_type;
Jake Oshins4daace02016-02-16 21:56:23 +0000268 union win_slot_encoding wslot;
269} __packed;
270
271struct pci_incoming_message {
272 struct vmpacket_descriptor hdr;
273 struct pci_message message_type;
274} __packed;
275
276struct pci_response {
277 struct vmpacket_descriptor hdr;
278 s32 status; /* negative values are failures */
279} __packed;
280
281struct pci_packet {
282 void (*completion_func)(void *context, struct pci_response *resp,
283 int resp_packet_size);
284 void *compl_ctxt;
Dexuan Cui0c6045d2016-08-23 04:45:51 +0000285
Gustavo A. R. Silva067fb6c2020-02-12 18:50:48 -0600286 struct pci_message message[];
Jake Oshins4daace02016-02-16 21:56:23 +0000287};
288
289/*
290 * Specific message types supporting the PCI protocol.
291 */
292
293/*
294 * Version negotiation message. Sent from the guest to the host.
295 * The guest is free to try different versions until the host
296 * accepts the version.
297 *
298 * pci_version: The protocol version requested.
299 * is_last_attempt: If TRUE, this is the last version guest will request.
300 * reservedz: Reserved field, set to zero.
301 */
302
303struct pci_version_request {
304 struct pci_message message_type;
Jork Loeser691ac1d2017-05-24 13:41:24 -0700305 u32 protocol_version;
Jake Oshins4daace02016-02-16 21:56:23 +0000306} __packed;
307
308/*
309 * Bus D0 Entry. This is sent from the guest to the host when the virtual
310 * bus (PCI Express port) is ready for action.
311 */
312
313struct pci_bus_d0_entry {
314 struct pci_message message_type;
315 u32 reserved;
316 u64 mmio_base;
317} __packed;
318
319struct pci_bus_relations {
320 struct pci_incoming_message incoming;
321 u32 device_count;
Gustavo A. R. Silva067fb6c2020-02-12 18:50:48 -0600322 struct pci_function_description func[];
Jake Oshins4daace02016-02-16 21:56:23 +0000323} __packed;
324
Long Li999dd952020-02-25 21:06:08 -0800325struct pci_bus_relations2 {
326 struct pci_incoming_message incoming;
327 u32 device_count;
Gustavo A. R. Silva067fb6c2020-02-12 18:50:48 -0600328 struct pci_function_description2 func[];
Long Li999dd952020-02-25 21:06:08 -0800329} __packed;
330
Jake Oshins4daace02016-02-16 21:56:23 +0000331struct pci_q_res_req_response {
332 struct vmpacket_descriptor hdr;
333 s32 status; /* negative values are failures */
Denis Efremovc9c13ba2019-09-28 02:43:08 +0300334 u32 probed_bar[PCI_STD_NUM_BARS];
Jake Oshins4daace02016-02-16 21:56:23 +0000335} __packed;
336
337struct pci_set_power {
338 struct pci_message message_type;
339 union win_slot_encoding wslot;
340 u32 power_state; /* In Windows terms */
341 u32 reserved;
342} __packed;
343
344struct pci_set_power_response {
345 struct vmpacket_descriptor hdr;
346 s32 status; /* negative values are failures */
347 union win_slot_encoding wslot;
348 u32 resultant_state; /* In Windows terms */
349 u32 reserved;
350} __packed;
351
352struct pci_resources_assigned {
353 struct pci_message message_type;
354 union win_slot_encoding wslot;
355 u8 memory_range[0x14][6]; /* not used here */
356 u32 msi_descriptors;
357 u32 reserved[4];
358} __packed;
359
Jork Loeser7dcf90e2017-05-24 13:41:28 -0700360struct pci_resources_assigned2 {
361 struct pci_message message_type;
362 union win_slot_encoding wslot;
363 u8 memory_range[0x14][6]; /* not used here */
364 u32 msi_descriptor_count;
365 u8 reserved[70];
366} __packed;
367
Jake Oshins4daace02016-02-16 21:56:23 +0000368struct pci_create_interrupt {
369 struct pci_message message_type;
370 union win_slot_encoding wslot;
371 struct hv_msi_desc int_desc;
372} __packed;
373
374struct pci_create_int_response {
375 struct pci_response response;
376 u32 reserved;
377 struct tran_int_desc int_desc;
378} __packed;
379
Jork Loeser7dcf90e2017-05-24 13:41:28 -0700380struct pci_create_interrupt2 {
381 struct pci_message message_type;
382 union win_slot_encoding wslot;
383 struct hv_msi_desc2 int_desc;
384} __packed;
385
Jake Oshins4daace02016-02-16 21:56:23 +0000386struct pci_delete_interrupt {
387 struct pci_message message_type;
388 union win_slot_encoding wslot;
389 struct tran_int_desc int_desc;
390} __packed;
391
Dexuan Cuie5d2f912019-08-22 05:05:37 +0000392/*
393 * Note: the VM must pass a valid block id, wslot and bytes_requested.
394 */
395struct pci_read_block {
396 struct pci_message message_type;
397 u32 block_id;
398 union win_slot_encoding wslot;
399 u32 bytes_requested;
400} __packed;
401
402struct pci_read_block_response {
403 struct vmpacket_descriptor hdr;
404 u32 status;
405 u8 bytes[HV_CONFIG_BLOCK_SIZE_MAX];
406} __packed;
407
408/*
409 * Note: the VM must pass a valid block id, wslot and byte_count.
410 */
411struct pci_write_block {
412 struct pci_message message_type;
413 u32 block_id;
414 union win_slot_encoding wslot;
415 u32 byte_count;
416 u8 bytes[HV_CONFIG_BLOCK_SIZE_MAX];
417} __packed;
418
419struct pci_dev_inval_block {
420 struct pci_incoming_message incoming;
421 union win_slot_encoding wslot;
422 u64 block_mask;
423} __packed;
424
Jake Oshins4daace02016-02-16 21:56:23 +0000425struct pci_dev_incoming {
426 struct pci_incoming_message incoming;
427 union win_slot_encoding wslot;
428} __packed;
429
430struct pci_eject_response {
Dexuan Cui0c6045d2016-08-23 04:45:51 +0000431 struct pci_message message_type;
Jake Oshins4daace02016-02-16 21:56:23 +0000432 union win_slot_encoding wslot;
433 u32 status;
434} __packed;
435
436static int pci_ring_size = (4 * PAGE_SIZE);
437
Jake Oshins4daace02016-02-16 21:56:23 +0000438/*
439 * Driver specific state.
440 */
441
442enum hv_pcibus_state {
443 hv_pcibus_init = 0,
444 hv_pcibus_probed,
445 hv_pcibus_installed,
Dexuan Cuiac82fc82019-11-24 21:33:52 -0800446 hv_pcibus_removing,
Jake Oshins4daace02016-02-16 21:56:23 +0000447 hv_pcibus_maximum
448};
449
450struct hv_pcibus_device {
451 struct pci_sysdata sysdata;
Dexuan Cui14ef39f2019-11-24 21:33:53 -0800452 /* Protocol version negotiated with the host */
453 enum pci_protocol_version_t protocol_version;
Jake Oshins4daace02016-02-16 21:56:23 +0000454 enum hv_pcibus_state state;
Stephen Hemminger6708be92018-05-23 10:11:13 -0700455 refcount_t remove_lock;
Jake Oshins4daace02016-02-16 21:56:23 +0000456 struct hv_device *hdev;
457 resource_size_t low_mmio_space;
458 resource_size_t high_mmio_space;
459 struct resource *mem_config;
460 struct resource *low_mmio_res;
461 struct resource *high_mmio_res;
462 struct completion *survey_event;
463 struct completion remove_event;
464 struct pci_bus *pci_bus;
465 spinlock_t config_lock; /* Avoid two threads writing index page */
466 spinlock_t device_list_lock; /* Protect lists below */
467 void __iomem *cfg_addr;
468
Jake Oshins4daace02016-02-16 21:56:23 +0000469 struct list_head resources_for_children;
470
471 struct list_head children;
472 struct list_head dr_list;
Jake Oshins4daace02016-02-16 21:56:23 +0000473
474 struct msi_domain_info msi_info;
Jake Oshins4daace02016-02-16 21:56:23 +0000475 struct irq_domain *irq_domain;
Jork Loeserbe66b672017-05-24 13:41:25 -0700476
Long Li0de8ce32016-11-08 14:04:38 -0800477 spinlock_t retarget_msi_interrupt_lock;
Dexuan Cui021ad272018-03-15 14:20:53 +0000478
479 struct workqueue_struct *wq;
Maya Nakamura9bc11742019-03-01 06:59:02 +0000480
Wei Hu83cc3502020-05-07 13:02:11 +0800481 /* Highest slot of child device with resources allocated */
482 int wslot_res_allocated;
483
Maya Nakamura9bc11742019-03-01 06:59:02 +0000484 /* hypercall arg, must not cross page boundary */
Boqun Feng61bfd922020-02-10 11:39:52 +0800485 struct hv_retarget_device_interrupt retarget_msi_interrupt_params;
Maya Nakamura9bc11742019-03-01 06:59:02 +0000486
487 /*
488 * Don't put anything here: retarget_msi_interrupt_params must be last
489 */
Jake Oshins4daace02016-02-16 21:56:23 +0000490};
491
492/*
493 * Tracks "Device Relations" messages from the host, which must be both
494 * processed in order and deferred so that they don't run in the context
495 * of the incoming packet callback.
496 */
497struct hv_dr_work {
498 struct work_struct wrk;
499 struct hv_pcibus_device *bus;
500};
501
Long Lif9ad0f32020-02-25 21:06:07 -0800502struct hv_pcidev_description {
503 u16 v_id; /* vendor ID */
504 u16 d_id; /* device ID */
505 u8 rev;
506 u8 prog_intf;
507 u8 subclass;
508 u8 base_class;
509 u32 subsystem_id;
510 union win_slot_encoding win_slot;
511 u32 ser; /* serial number */
512 u32 flags;
513 u16 virtual_numa_node;
514};
515
Jake Oshins4daace02016-02-16 21:56:23 +0000516struct hv_dr_state {
517 struct list_head list_entry;
518 u32 device_count;
Gustavo A. R. Silva067fb6c2020-02-12 18:50:48 -0600519 struct hv_pcidev_description func[];
Jake Oshins4daace02016-02-16 21:56:23 +0000520};
521
522enum hv_pcichild_state {
523 hv_pcichild_init = 0,
524 hv_pcichild_requirements,
525 hv_pcichild_resourced,
526 hv_pcichild_ejecting,
527 hv_pcichild_maximum
528};
529
Jake Oshins4daace02016-02-16 21:56:23 +0000530struct hv_pci_dev {
531 /* List protected by pci_rescan_remove_lock */
532 struct list_head list_entry;
Elena Reshetova24196f02017-04-18 09:02:48 -0500533 refcount_t refs;
Jake Oshins4daace02016-02-16 21:56:23 +0000534 enum hv_pcichild_state state;
Stephen Hemmingera15f2c082018-09-14 12:54:56 -0700535 struct pci_slot *pci_slot;
Long Lif9ad0f32020-02-25 21:06:07 -0800536 struct hv_pcidev_description desc;
Jake Oshins4daace02016-02-16 21:56:23 +0000537 bool reported_missing;
538 struct hv_pcibus_device *hbus;
539 struct work_struct wrk;
540
Dexuan Cuie5d2f912019-08-22 05:05:37 +0000541 void (*block_invalidate)(void *context, u64 block_mask);
542 void *invalidate_context;
543
Jake Oshins4daace02016-02-16 21:56:23 +0000544 /*
545 * What would be observed if one wrote 0xFFFFFFFF to a BAR and then
546 * read it back, for each of the BAR offsets within config space.
547 */
Denis Efremovc9c13ba2019-09-28 02:43:08 +0300548 u32 probed_bar[PCI_STD_NUM_BARS];
Jake Oshins4daace02016-02-16 21:56:23 +0000549};
550
551struct hv_pci_compl {
552 struct completion host_event;
553 s32 completion_status;
554};
555
Dexuan Cuide0aa7b2018-03-15 14:21:08 +0000556static void hv_pci_onchannelcallback(void *context);
557
Jake Oshins4daace02016-02-16 21:56:23 +0000558/**
559 * hv_pci_generic_compl() - Invoked for a completion packet
560 * @context: Set up by the sender of the packet.
561 * @resp: The response packet
562 * @resp_packet_size: Size in bytes of the packet
563 *
564 * This function is used to trigger an event and report status
565 * for any message for which the completion packet contains a
566 * status and nothing else.
567 */
Dexuan Cuia5b45b72016-08-23 04:49:22 +0000568static void hv_pci_generic_compl(void *context, struct pci_response *resp,
569 int resp_packet_size)
Jake Oshins4daace02016-02-16 21:56:23 +0000570{
571 struct hv_pci_compl *comp_pkt = context;
572
573 if (resp_packet_size >= offsetofend(struct pci_response, status))
574 comp_pkt->completion_status = resp->status;
Dexuan Cuia5b45b72016-08-23 04:49:22 +0000575 else
576 comp_pkt->completion_status = -1;
577
Jake Oshins4daace02016-02-16 21:56:23 +0000578 complete(&comp_pkt->host_event);
579}
580
581static struct hv_pci_dev *get_pcichild_wslot(struct hv_pcibus_device *hbus,
582 u32 wslot);
Stephen Hemminger8c99e122018-05-23 10:11:12 -0700583
584static void get_pcichild(struct hv_pci_dev *hpdev)
585{
586 refcount_inc(&hpdev->refs);
587}
588
589static void put_pcichild(struct hv_pci_dev *hpdev)
590{
591 if (refcount_dec_and_test(&hpdev->refs))
592 kfree(hpdev);
593}
Jake Oshins4daace02016-02-16 21:56:23 +0000594
595static void get_hvpcibus(struct hv_pcibus_device *hv_pcibus);
596static void put_hvpcibus(struct hv_pcibus_device *hv_pcibus);
597
Dexuan Cuic3635da2018-05-23 21:12:01 +0000598/*
599 * There is no good way to get notified from vmbus_onoffer_rescind(),
600 * so let's use polling here, since this is not a hot path.
601 */
602static int wait_for_response(struct hv_device *hdev,
603 struct completion *comp)
604{
605 while (true) {
606 if (hdev->channel->rescind) {
607 dev_warn_once(&hdev->device, "The device is gone.\n");
608 return -ENODEV;
609 }
610
611 if (wait_for_completion_timeout(comp, HZ / 10))
612 break;
613 }
614
615 return 0;
616}
617
Jake Oshins4daace02016-02-16 21:56:23 +0000618/**
619 * devfn_to_wslot() - Convert from Linux PCI slot to Windows
620 * @devfn: The Linux representation of PCI slot
621 *
622 * Windows uses a slightly different representation of PCI slot.
623 *
624 * Return: The Windows representation
625 */
626static u32 devfn_to_wslot(int devfn)
627{
628 union win_slot_encoding wslot;
629
630 wslot.slot = 0;
Dexuan Cui60e2e2f2017-02-10 15:18:46 -0600631 wslot.bits.dev = PCI_SLOT(devfn);
632 wslot.bits.func = PCI_FUNC(devfn);
Jake Oshins4daace02016-02-16 21:56:23 +0000633
634 return wslot.slot;
635}
636
637/**
638 * wslot_to_devfn() - Convert from Windows PCI slot to Linux
639 * @wslot: The Windows representation of PCI slot
640 *
641 * Windows uses a slightly different representation of PCI slot.
642 *
643 * Return: The Linux representation
644 */
645static int wslot_to_devfn(u32 wslot)
646{
647 union win_slot_encoding slot_no;
648
649 slot_no.slot = wslot;
Dexuan Cui60e2e2f2017-02-10 15:18:46 -0600650 return PCI_DEVFN(slot_no.bits.dev, slot_no.bits.func);
Jake Oshins4daace02016-02-16 21:56:23 +0000651}
652
653/*
654 * PCI Configuration Space for these root PCI buses is implemented as a pair
655 * of pages in memory-mapped I/O space. Writing to the first page chooses
656 * the PCI function being written or read. Once the first page has been
657 * written to, the following page maps in the entire configuration space of
658 * the function.
659 */
660
661/**
662 * _hv_pcifront_read_config() - Internal PCI config read
663 * @hpdev: The PCI driver's representation of the device
664 * @where: Offset within config space
665 * @size: Size of the transfer
666 * @val: Pointer to the buffer receiving the data
667 */
668static void _hv_pcifront_read_config(struct hv_pci_dev *hpdev, int where,
669 int size, u32 *val)
670{
671 unsigned long flags;
672 void __iomem *addr = hpdev->hbus->cfg_addr + CFG_PAGE_OFFSET + where;
673
674 /*
675 * If the attempt is to read the IDs or the ROM BAR, simulate that.
676 */
677 if (where + size <= PCI_COMMAND) {
678 memcpy(val, ((u8 *)&hpdev->desc.v_id) + where, size);
679 } else if (where >= PCI_CLASS_REVISION && where + size <=
680 PCI_CACHE_LINE_SIZE) {
681 memcpy(val, ((u8 *)&hpdev->desc.rev) + where -
682 PCI_CLASS_REVISION, size);
683 } else if (where >= PCI_SUBSYSTEM_VENDOR_ID && where + size <=
684 PCI_ROM_ADDRESS) {
685 memcpy(val, (u8 *)&hpdev->desc.subsystem_id + where -
686 PCI_SUBSYSTEM_VENDOR_ID, size);
687 } else if (where >= PCI_ROM_ADDRESS && where + size <=
688 PCI_CAPABILITY_LIST) {
689 /* ROM BARs are unimplemented */
690 *val = 0;
691 } else if (where >= PCI_INTERRUPT_LINE && where + size <=
692 PCI_INTERRUPT_PIN) {
693 /*
694 * Interrupt Line and Interrupt PIN are hard-wired to zero
695 * because this front-end only supports message-signaled
696 * interrupts.
697 */
698 *val = 0;
699 } else if (where + size <= CFG_PAGE_SIZE) {
700 spin_lock_irqsave(&hpdev->hbus->config_lock, flags);
701 /* Choose the function to be read. (See comment above) */
702 writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr);
Vitaly Kuznetsovbdd74442016-05-03 14:22:00 +0200703 /* Make sure the function was chosen before we start reading. */
704 mb();
Jake Oshins4daace02016-02-16 21:56:23 +0000705 /* Read from that function's config space. */
706 switch (size) {
707 case 1:
708 *val = readb(addr);
709 break;
710 case 2:
711 *val = readw(addr);
712 break;
713 default:
714 *val = readl(addr);
715 break;
716 }
Vitaly Kuznetsovbdd74442016-05-03 14:22:00 +0200717 /*
Dexuan Cuidf3f2152018-03-15 14:21:35 +0000718 * Make sure the read was done before we release the spinlock
Vitaly Kuznetsovbdd74442016-05-03 14:22:00 +0200719 * allowing consecutive reads/writes.
720 */
721 mb();
Jake Oshins4daace02016-02-16 21:56:23 +0000722 spin_unlock_irqrestore(&hpdev->hbus->config_lock, flags);
723 } else {
724 dev_err(&hpdev->hbus->hdev->device,
725 "Attempt to read beyond a function's config space.\n");
726 }
727}
728
Dexuan Cuide0aa7b2018-03-15 14:21:08 +0000729static u16 hv_pcifront_get_vendor_id(struct hv_pci_dev *hpdev)
730{
731 u16 ret;
732 unsigned long flags;
733 void __iomem *addr = hpdev->hbus->cfg_addr + CFG_PAGE_OFFSET +
734 PCI_VENDOR_ID;
735
736 spin_lock_irqsave(&hpdev->hbus->config_lock, flags);
737
738 /* Choose the function to be read. (See comment above) */
739 writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr);
740 /* Make sure the function was chosen before we start reading. */
741 mb();
742 /* Read from that function's config space. */
743 ret = readw(addr);
744 /*
745 * mb() is not required here, because the spin_unlock_irqrestore()
746 * is a barrier.
747 */
748
749 spin_unlock_irqrestore(&hpdev->hbus->config_lock, flags);
750
751 return ret;
752}
753
Jake Oshins4daace02016-02-16 21:56:23 +0000754/**
755 * _hv_pcifront_write_config() - Internal PCI config write
756 * @hpdev: The PCI driver's representation of the device
757 * @where: Offset within config space
758 * @size: Size of the transfer
759 * @val: The data being transferred
760 */
761static void _hv_pcifront_write_config(struct hv_pci_dev *hpdev, int where,
762 int size, u32 val)
763{
764 unsigned long flags;
765 void __iomem *addr = hpdev->hbus->cfg_addr + CFG_PAGE_OFFSET + where;
766
767 if (where >= PCI_SUBSYSTEM_VENDOR_ID &&
768 where + size <= PCI_CAPABILITY_LIST) {
769 /* SSIDs and ROM BARs are read-only */
770 } else if (where >= PCI_COMMAND && where + size <= CFG_PAGE_SIZE) {
771 spin_lock_irqsave(&hpdev->hbus->config_lock, flags);
772 /* Choose the function to be written. (See comment above) */
773 writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr);
Vitaly Kuznetsovbdd74442016-05-03 14:22:00 +0200774 /* Make sure the function was chosen before we start writing. */
775 wmb();
Jake Oshins4daace02016-02-16 21:56:23 +0000776 /* Write to that function's config space. */
777 switch (size) {
778 case 1:
779 writeb(val, addr);
780 break;
781 case 2:
782 writew(val, addr);
783 break;
784 default:
785 writel(val, addr);
786 break;
787 }
Vitaly Kuznetsovbdd74442016-05-03 14:22:00 +0200788 /*
789 * Make sure the write was done before we release the spinlock
790 * allowing consecutive reads/writes.
791 */
792 mb();
Jake Oshins4daace02016-02-16 21:56:23 +0000793 spin_unlock_irqrestore(&hpdev->hbus->config_lock, flags);
794 } else {
795 dev_err(&hpdev->hbus->hdev->device,
796 "Attempt to write beyond a function's config space.\n");
797 }
798}
799
800/**
801 * hv_pcifront_read_config() - Read configuration space
802 * @bus: PCI Bus structure
803 * @devfn: Device/function
804 * @where: Offset from base
805 * @size: Byte/word/dword
806 * @val: Value to be read
807 *
808 * Return: PCIBIOS_SUCCESSFUL on success
809 * PCIBIOS_DEVICE_NOT_FOUND on failure
810 */
811static int hv_pcifront_read_config(struct pci_bus *bus, unsigned int devfn,
812 int where, int size, u32 *val)
813{
814 struct hv_pcibus_device *hbus =
815 container_of(bus->sysdata, struct hv_pcibus_device, sysdata);
816 struct hv_pci_dev *hpdev;
817
818 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(devfn));
819 if (!hpdev)
820 return PCIBIOS_DEVICE_NOT_FOUND;
821
822 _hv_pcifront_read_config(hpdev, where, size, val);
823
Stephen Hemminger8c99e122018-05-23 10:11:12 -0700824 put_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +0000825 return PCIBIOS_SUCCESSFUL;
826}
827
828/**
829 * hv_pcifront_write_config() - Write configuration space
830 * @bus: PCI Bus structure
831 * @devfn: Device/function
832 * @where: Offset from base
833 * @size: Byte/word/dword
834 * @val: Value to be written to device
835 *
836 * Return: PCIBIOS_SUCCESSFUL on success
837 * PCIBIOS_DEVICE_NOT_FOUND on failure
838 */
839static int hv_pcifront_write_config(struct pci_bus *bus, unsigned int devfn,
840 int where, int size, u32 val)
841{
842 struct hv_pcibus_device *hbus =
843 container_of(bus->sysdata, struct hv_pcibus_device, sysdata);
844 struct hv_pci_dev *hpdev;
845
846 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(devfn));
847 if (!hpdev)
848 return PCIBIOS_DEVICE_NOT_FOUND;
849
850 _hv_pcifront_write_config(hpdev, where, size, val);
851
Stephen Hemminger8c99e122018-05-23 10:11:12 -0700852 put_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +0000853 return PCIBIOS_SUCCESSFUL;
854}
855
856/* PCIe operations */
857static struct pci_ops hv_pcifront_ops = {
858 .read = hv_pcifront_read_config,
859 .write = hv_pcifront_write_config,
860};
861
Dexuan Cuie5d2f912019-08-22 05:05:37 +0000862/*
863 * Paravirtual backchannel
864 *
865 * Hyper-V SR-IOV provides a backchannel mechanism in software for
866 * communication between a VF driver and a PF driver. These
867 * "configuration blocks" are similar in concept to PCI configuration space,
868 * but instead of doing reads and writes in 32-bit chunks through a very slow
869 * path, packets of up to 128 bytes can be sent or received asynchronously.
870 *
871 * Nearly every SR-IOV device contains just such a communications channel in
872 * hardware, so using this one in software is usually optional. Using the
873 * software channel, however, allows driver implementers to leverage software
874 * tools that fuzz the communications channel looking for vulnerabilities.
875 *
876 * The usage model for these packets puts the responsibility for reading or
877 * writing on the VF driver. The VF driver sends a read or a write packet,
878 * indicating which "block" is being referred to by number.
879 *
880 * If the PF driver wishes to initiate communication, it can "invalidate" one or
881 * more of the first 64 blocks. This invalidation is delivered via a callback
882 * supplied by the VF driver by this driver.
883 *
884 * No protocol is implied, except that supplied by the PF and VF drivers.
885 */
886
887struct hv_read_config_compl {
888 struct hv_pci_compl comp_pkt;
889 void *buf;
890 unsigned int len;
891 unsigned int bytes_returned;
892};
893
894/**
895 * hv_pci_read_config_compl() - Invoked when a response packet
896 * for a read config block operation arrives.
897 * @context: Identifies the read config operation
898 * @resp: The response packet itself
899 * @resp_packet_size: Size in bytes of the response packet
900 */
901static void hv_pci_read_config_compl(void *context, struct pci_response *resp,
902 int resp_packet_size)
903{
904 struct hv_read_config_compl *comp = context;
905 struct pci_read_block_response *read_resp =
906 (struct pci_read_block_response *)resp;
907 unsigned int data_len, hdr_len;
908
909 hdr_len = offsetof(struct pci_read_block_response, bytes);
910 if (resp_packet_size < hdr_len) {
911 comp->comp_pkt.completion_status = -1;
912 goto out;
913 }
914
915 data_len = resp_packet_size - hdr_len;
916 if (data_len > 0 && read_resp->status == 0) {
917 comp->bytes_returned = min(comp->len, data_len);
918 memcpy(comp->buf, read_resp->bytes, comp->bytes_returned);
919 } else {
920 comp->bytes_returned = 0;
921 }
922
923 comp->comp_pkt.completion_status = read_resp->status;
924out:
925 complete(&comp->comp_pkt.host_event);
926}
927
928/**
929 * hv_read_config_block() - Sends a read config block request to
930 * the back-end driver running in the Hyper-V parent partition.
931 * @pdev: The PCI driver's representation for this device.
932 * @buf: Buffer into which the config block will be copied.
933 * @len: Size in bytes of buf.
934 * @block_id: Identifies the config block which has been requested.
935 * @bytes_returned: Size which came back from the back-end driver.
936 *
937 * Return: 0 on success, -errno on failure
938 */
Wei Yongjuna459d9e2020-07-06 21:52:34 +0800939static int hv_read_config_block(struct pci_dev *pdev, void *buf,
940 unsigned int len, unsigned int block_id,
941 unsigned int *bytes_returned)
Dexuan Cuie5d2f912019-08-22 05:05:37 +0000942{
943 struct hv_pcibus_device *hbus =
944 container_of(pdev->bus->sysdata, struct hv_pcibus_device,
945 sysdata);
946 struct {
947 struct pci_packet pkt;
948 char buf[sizeof(struct pci_read_block)];
949 } pkt;
950 struct hv_read_config_compl comp_pkt;
951 struct pci_read_block *read_blk;
952 int ret;
953
954 if (len == 0 || len > HV_CONFIG_BLOCK_SIZE_MAX)
955 return -EINVAL;
956
957 init_completion(&comp_pkt.comp_pkt.host_event);
958 comp_pkt.buf = buf;
959 comp_pkt.len = len;
960
961 memset(&pkt, 0, sizeof(pkt));
962 pkt.pkt.completion_func = hv_pci_read_config_compl;
963 pkt.pkt.compl_ctxt = &comp_pkt;
964 read_blk = (struct pci_read_block *)&pkt.pkt.message;
965 read_blk->message_type.type = PCI_READ_BLOCK;
966 read_blk->wslot.slot = devfn_to_wslot(pdev->devfn);
967 read_blk->block_id = block_id;
968 read_blk->bytes_requested = len;
969
970 ret = vmbus_sendpacket(hbus->hdev->channel, read_blk,
971 sizeof(*read_blk), (unsigned long)&pkt.pkt,
972 VM_PKT_DATA_INBAND,
973 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
974 if (ret)
975 return ret;
976
977 ret = wait_for_response(hbus->hdev, &comp_pkt.comp_pkt.host_event);
978 if (ret)
979 return ret;
980
981 if (comp_pkt.comp_pkt.completion_status != 0 ||
982 comp_pkt.bytes_returned == 0) {
983 dev_err(&hbus->hdev->device,
984 "Read Config Block failed: 0x%x, bytes_returned=%d\n",
985 comp_pkt.comp_pkt.completion_status,
986 comp_pkt.bytes_returned);
987 return -EIO;
988 }
989
990 *bytes_returned = comp_pkt.bytes_returned;
991 return 0;
992}
Dexuan Cuie5d2f912019-08-22 05:05:37 +0000993
994/**
995 * hv_pci_write_config_compl() - Invoked when a response packet for a write
996 * config block operation arrives.
997 * @context: Identifies the write config operation
998 * @resp: The response packet itself
999 * @resp_packet_size: Size in bytes of the response packet
1000 */
1001static void hv_pci_write_config_compl(void *context, struct pci_response *resp,
1002 int resp_packet_size)
1003{
1004 struct hv_pci_compl *comp_pkt = context;
1005
1006 comp_pkt->completion_status = resp->status;
1007 complete(&comp_pkt->host_event);
1008}
1009
1010/**
1011 * hv_write_config_block() - Sends a write config block request to the
1012 * back-end driver running in the Hyper-V parent partition.
1013 * @pdev: The PCI driver's representation for this device.
1014 * @buf: Buffer from which the config block will be copied.
1015 * @len: Size in bytes of buf.
1016 * @block_id: Identifies the config block which is being written.
1017 *
1018 * Return: 0 on success, -errno on failure
1019 */
Wei Yongjuna459d9e2020-07-06 21:52:34 +08001020static int hv_write_config_block(struct pci_dev *pdev, void *buf,
1021 unsigned int len, unsigned int block_id)
Dexuan Cuie5d2f912019-08-22 05:05:37 +00001022{
1023 struct hv_pcibus_device *hbus =
1024 container_of(pdev->bus->sysdata, struct hv_pcibus_device,
1025 sysdata);
1026 struct {
1027 struct pci_packet pkt;
1028 char buf[sizeof(struct pci_write_block)];
1029 u32 reserved;
1030 } pkt;
1031 struct hv_pci_compl comp_pkt;
1032 struct pci_write_block *write_blk;
1033 u32 pkt_size;
1034 int ret;
1035
1036 if (len == 0 || len > HV_CONFIG_BLOCK_SIZE_MAX)
1037 return -EINVAL;
1038
1039 init_completion(&comp_pkt.host_event);
1040
1041 memset(&pkt, 0, sizeof(pkt));
1042 pkt.pkt.completion_func = hv_pci_write_config_compl;
1043 pkt.pkt.compl_ctxt = &comp_pkt;
1044 write_blk = (struct pci_write_block *)&pkt.pkt.message;
1045 write_blk->message_type.type = PCI_WRITE_BLOCK;
1046 write_blk->wslot.slot = devfn_to_wslot(pdev->devfn);
1047 write_blk->block_id = block_id;
1048 write_blk->byte_count = len;
1049 memcpy(write_blk->bytes, buf, len);
1050 pkt_size = offsetof(struct pci_write_block, bytes) + len;
1051 /*
1052 * This quirk is required on some hosts shipped around 2018, because
1053 * these hosts don't check the pkt_size correctly (new hosts have been
1054 * fixed since early 2019). The quirk is also safe on very old hosts
1055 * and new hosts, because, on them, what really matters is the length
1056 * specified in write_blk->byte_count.
1057 */
1058 pkt_size += sizeof(pkt.reserved);
1059
1060 ret = vmbus_sendpacket(hbus->hdev->channel, write_blk, pkt_size,
1061 (unsigned long)&pkt.pkt, VM_PKT_DATA_INBAND,
1062 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1063 if (ret)
1064 return ret;
1065
1066 ret = wait_for_response(hbus->hdev, &comp_pkt.host_event);
1067 if (ret)
1068 return ret;
1069
1070 if (comp_pkt.completion_status != 0) {
1071 dev_err(&hbus->hdev->device,
1072 "Write Config Block failed: 0x%x\n",
1073 comp_pkt.completion_status);
1074 return -EIO;
1075 }
1076
1077 return 0;
1078}
Dexuan Cuie5d2f912019-08-22 05:05:37 +00001079
1080/**
1081 * hv_register_block_invalidate() - Invoked when a config block invalidation
1082 * arrives from the back-end driver.
1083 * @pdev: The PCI driver's representation for this device.
1084 * @context: Identifies the device.
1085 * @block_invalidate: Identifies all of the blocks being invalidated.
1086 *
1087 * Return: 0 on success, -errno on failure
1088 */
Wei Yongjuna459d9e2020-07-06 21:52:34 +08001089static int hv_register_block_invalidate(struct pci_dev *pdev, void *context,
1090 void (*block_invalidate)(void *context,
1091 u64 block_mask))
Dexuan Cuie5d2f912019-08-22 05:05:37 +00001092{
1093 struct hv_pcibus_device *hbus =
1094 container_of(pdev->bus->sysdata, struct hv_pcibus_device,
1095 sysdata);
1096 struct hv_pci_dev *hpdev;
1097
1098 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
1099 if (!hpdev)
1100 return -ENODEV;
1101
1102 hpdev->block_invalidate = block_invalidate;
1103 hpdev->invalidate_context = context;
1104
1105 put_pcichild(hpdev);
1106 return 0;
1107
1108}
Dexuan Cuie5d2f912019-08-22 05:05:37 +00001109
Jake Oshins4daace02016-02-16 21:56:23 +00001110/* Interrupt management hooks */
1111static void hv_int_desc_free(struct hv_pci_dev *hpdev,
1112 struct tran_int_desc *int_desc)
1113{
1114 struct pci_delete_interrupt *int_pkt;
1115 struct {
1116 struct pci_packet pkt;
Dexuan Cui0c6045d2016-08-23 04:45:51 +00001117 u8 buffer[sizeof(struct pci_delete_interrupt)];
Jake Oshins4daace02016-02-16 21:56:23 +00001118 } ctxt;
1119
1120 memset(&ctxt, 0, sizeof(ctxt));
1121 int_pkt = (struct pci_delete_interrupt *)&ctxt.pkt.message;
Dexuan Cui0c6045d2016-08-23 04:45:51 +00001122 int_pkt->message_type.type =
Jake Oshins4daace02016-02-16 21:56:23 +00001123 PCI_DELETE_INTERRUPT_MESSAGE;
1124 int_pkt->wslot.slot = hpdev->desc.win_slot.slot;
1125 int_pkt->int_desc = *int_desc;
1126 vmbus_sendpacket(hpdev->hbus->hdev->channel, int_pkt, sizeof(*int_pkt),
1127 (unsigned long)&ctxt.pkt, VM_PKT_DATA_INBAND, 0);
1128 kfree(int_desc);
1129}
1130
1131/**
1132 * hv_msi_free() - Free the MSI.
1133 * @domain: The interrupt domain pointer
1134 * @info: Extra MSI-related context
1135 * @irq: Identifies the IRQ.
1136 *
1137 * The Hyper-V parent partition and hypervisor are tracking the
1138 * messages that are in use, keeping the interrupt redirection
1139 * table up to date. This callback sends a message that frees
1140 * the IRT entry and related tracking nonsense.
1141 */
1142static void hv_msi_free(struct irq_domain *domain, struct msi_domain_info *info,
1143 unsigned int irq)
1144{
1145 struct hv_pcibus_device *hbus;
1146 struct hv_pci_dev *hpdev;
1147 struct pci_dev *pdev;
1148 struct tran_int_desc *int_desc;
1149 struct irq_data *irq_data = irq_domain_get_irq_data(domain, irq);
1150 struct msi_desc *msi = irq_data_get_msi_desc(irq_data);
1151
1152 pdev = msi_desc_to_pci_dev(msi);
1153 hbus = info->data;
Cathy Avery0c6e6172016-07-12 11:31:24 -04001154 int_desc = irq_data_get_irq_chip_data(irq_data);
1155 if (!int_desc)
Jake Oshins4daace02016-02-16 21:56:23 +00001156 return;
1157
Cathy Avery0c6e6172016-07-12 11:31:24 -04001158 irq_data->chip_data = NULL;
1159 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
1160 if (!hpdev) {
1161 kfree(int_desc);
1162 return;
Jake Oshins4daace02016-02-16 21:56:23 +00001163 }
1164
Cathy Avery0c6e6172016-07-12 11:31:24 -04001165 hv_int_desc_free(hpdev, int_desc);
Stephen Hemminger8c99e122018-05-23 10:11:12 -07001166 put_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +00001167}
1168
1169static int hv_set_affinity(struct irq_data *data, const struct cpumask *dest,
1170 bool force)
1171{
1172 struct irq_data *parent = data->parent_data;
1173
1174 return parent->chip->irq_set_affinity(parent, dest, force);
1175}
1176
Tobias Klauser542ccf42016-10-31 12:04:09 +01001177static void hv_irq_mask(struct irq_data *data)
Jake Oshins4daace02016-02-16 21:56:23 +00001178{
1179 pci_msi_mask_irq(data);
1180}
1181
1182/**
1183 * hv_irq_unmask() - "Unmask" the IRQ by setting its current
1184 * affinity.
1185 * @data: Describes the IRQ
1186 *
1187 * Build new a destination for the MSI and make a hypercall to
1188 * update the Interrupt Redirection Table. "Device Logical ID"
1189 * is built out of this PCI bus's instance GUID and the function
1190 * number of the device.
1191 */
Tobias Klauser542ccf42016-10-31 12:04:09 +01001192static void hv_irq_unmask(struct irq_data *data)
Jake Oshins4daace02016-02-16 21:56:23 +00001193{
1194 struct msi_desc *msi_desc = irq_data_get_msi_desc(data);
1195 struct irq_cfg *cfg = irqd_cfg(data);
Boqun Feng61bfd922020-02-10 11:39:52 +08001196 struct hv_retarget_device_interrupt *params;
Jake Oshins4daace02016-02-16 21:56:23 +00001197 struct hv_pcibus_device *hbus;
1198 struct cpumask *dest;
Maya Nakamurac8ccf752019-03-01 07:04:17 +00001199 cpumask_var_t tmp;
Jake Oshins4daace02016-02-16 21:56:23 +00001200 struct pci_bus *pbus;
1201 struct pci_dev *pdev;
Long Li0de8ce32016-11-08 14:04:38 -08001202 unsigned long flags;
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001203 u32 var_size = 0;
Maya Nakamurac8ccf752019-03-01 07:04:17 +00001204 int cpu, nr_bank;
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001205 u64 res;
Jake Oshins4daace02016-02-16 21:56:23 +00001206
Dexuan Cui79aa8012017-11-01 20:30:53 +00001207 dest = irq_data_get_effective_affinity_mask(data);
Jake Oshins4daace02016-02-16 21:56:23 +00001208 pdev = msi_desc_to_pci_dev(msi_desc);
1209 pbus = pdev->bus;
1210 hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata);
1211
Long Li0de8ce32016-11-08 14:04:38 -08001212 spin_lock_irqsave(&hbus->retarget_msi_interrupt_lock, flags);
1213
1214 params = &hbus->retarget_msi_interrupt_params;
1215 memset(params, 0, sizeof(*params));
1216 params->partition_id = HV_PARTITION_ID_SELF;
Wei Liub59fb7b2021-02-03 15:04:31 +00001217 params->int_entry.source = HV_INTERRUPT_SOURCE_MSI;
Boqun Feng1cf106d2020-02-10 11:39:53 +08001218 hv_set_msi_entry_from_desc(&params->int_entry.msi_entry, msi_desc);
Long Li0de8ce32016-11-08 14:04:38 -08001219 params->device_id = (hbus->hdev->dev_instance.b[5] << 24) |
Jake Oshins4daace02016-02-16 21:56:23 +00001220 (hbus->hdev->dev_instance.b[4] << 16) |
1221 (hbus->hdev->dev_instance.b[7] << 8) |
1222 (hbus->hdev->dev_instance.b[6] & 0xf8) |
1223 PCI_FUNC(pdev->devfn);
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001224 params->int_target.vector = cfg->vector;
Jake Oshins4daace02016-02-16 21:56:23 +00001225
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001226 /*
Thomas Gleixner72161292020-10-24 22:35:05 +01001227 * Honoring apic->delivery_mode set to APIC_DELIVERY_MODE_FIXED by
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001228 * setting the HV_DEVICE_INTERRUPT_TARGET_MULTICAST flag results in a
1229 * spurious interrupt storm. Not doing so does not seem to have a
1230 * negative effect (yet?).
1231 */
Jake Oshins4daace02016-02-16 21:56:23 +00001232
Dexuan Cui14ef39f2019-11-24 21:33:53 -08001233 if (hbus->protocol_version >= PCI_PROTOCOL_VERSION_1_2) {
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001234 /*
1235 * PCI_PROTOCOL_VERSION_1_2 supports the VP_SET version of the
1236 * HVCALL_RETARGET_INTERRUPT hypercall, which also coincides
1237 * with >64 VP support.
1238 * ms_hyperv.hints & HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED
1239 * is not sufficient for this hypercall.
1240 */
1241 params->int_target.flags |=
1242 HV_DEVICE_INTERRUPT_TARGET_PROCESSOR_SET;
Maya Nakamurac8ccf752019-03-01 07:04:17 +00001243
1244 if (!alloc_cpumask_var(&tmp, GFP_ATOMIC)) {
1245 res = 1;
1246 goto exit_unlock;
1247 }
1248
1249 cpumask_and(tmp, dest, cpu_online_mask);
1250 nr_bank = cpumask_to_vpset(&params->int_target.vp_set, tmp);
1251 free_cpumask_var(tmp);
1252
1253 if (nr_bank <= 0) {
1254 res = 1;
1255 goto exit_unlock;
1256 }
Long Li0de8ce32016-11-08 14:04:38 -08001257
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001258 /*
1259 * var-sized hypercall, var-size starts after vp_mask (thus
Maya Nakamura9bc11742019-03-01 06:59:02 +00001260 * vp_set.format does not count, but vp_set.valid_bank_mask
1261 * does).
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001262 */
Maya Nakamurac8ccf752019-03-01 07:04:17 +00001263 var_size = 1 + nr_bank;
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001264 } else {
1265 for_each_cpu_and(cpu, dest, cpu_online_mask) {
1266 params->int_target.vp_mask |=
Vitaly Kuznetsov7415aea2017-08-02 18:09:18 +02001267 (1ULL << hv_cpu_number_to_vp_number(cpu));
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001268 }
1269 }
1270
1271 res = hv_do_hypercall(HVCALL_RETARGET_INTERRUPT | (var_size << 17),
1272 params, NULL);
1273
1274exit_unlock:
Long Li0de8ce32016-11-08 14:04:38 -08001275 spin_unlock_irqrestore(&hbus->retarget_msi_interrupt_lock, flags);
Jake Oshins4daace02016-02-16 21:56:23 +00001276
Dexuan Cui915cff72020-10-02 01:51:58 -07001277 /*
1278 * During hibernation, when a CPU is offlined, the kernel tries
1279 * to move the interrupt to the remaining CPUs that haven't
1280 * been offlined yet. In this case, the below hv_do_hypercall()
1281 * always fails since the vmbus channel has been closed:
1282 * refer to cpu_disable_common() -> fixup_irqs() ->
1283 * irq_migrate_all_off_this_cpu() -> migrate_one_irq().
1284 *
1285 * Suppress the error message for hibernation because the failure
1286 * during hibernation does not matter (at this time all the devices
1287 * have been frozen). Note: the correct affinity info is still updated
1288 * into the irqdata data structure in migrate_one_irq() ->
1289 * irq_do_set_affinity() -> hv_set_affinity(), so later when the VM
1290 * resumes, hv_pci_restore_msi_state() is able to correctly restore
1291 * the interrupt with the correct affinity.
1292 */
Joseph Salisbury753ed9c2021-04-16 17:43:03 -07001293 if (!hv_result_success(res) && hbus->state != hv_pcibus_removing)
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001294 dev_err(&hbus->hdev->device,
1295 "%s() failed: %#llx", __func__, res);
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001296
Jake Oshins4daace02016-02-16 21:56:23 +00001297 pci_msi_unmask_irq(data);
1298}
1299
1300struct compose_comp_ctxt {
1301 struct hv_pci_compl comp_pkt;
1302 struct tran_int_desc int_desc;
1303};
1304
1305static void hv_pci_compose_compl(void *context, struct pci_response *resp,
1306 int resp_packet_size)
1307{
1308 struct compose_comp_ctxt *comp_pkt = context;
1309 struct pci_create_int_response *int_resp =
1310 (struct pci_create_int_response *)resp;
1311
1312 comp_pkt->comp_pkt.completion_status = resp->status;
1313 comp_pkt->int_desc = int_resp->int_desc;
1314 complete(&comp_pkt->comp_pkt.host_event);
1315}
1316
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001317static u32 hv_compose_msi_req_v1(
1318 struct pci_create_interrupt *int_pkt, struct cpumask *affinity,
1319 u32 slot, u8 vector)
1320{
1321 int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE;
1322 int_pkt->wslot.slot = slot;
1323 int_pkt->int_desc.vector = vector;
1324 int_pkt->int_desc.vector_count = 1;
Thomas Gleixner72161292020-10-24 22:35:05 +01001325 int_pkt->int_desc.delivery_mode = APIC_DELIVERY_MODE_FIXED;
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001326
1327 /*
1328 * Create MSI w/ dummy vCPU set, overwritten by subsequent retarget in
1329 * hv_irq_unmask().
1330 */
1331 int_pkt->int_desc.cpu_mask = CPU_AFFINITY_ALL;
1332
1333 return sizeof(*int_pkt);
1334}
1335
1336static u32 hv_compose_msi_req_v2(
1337 struct pci_create_interrupt2 *int_pkt, struct cpumask *affinity,
1338 u32 slot, u8 vector)
1339{
1340 int cpu;
1341
1342 int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE2;
1343 int_pkt->wslot.slot = slot;
1344 int_pkt->int_desc.vector = vector;
1345 int_pkt->int_desc.vector_count = 1;
Thomas Gleixner72161292020-10-24 22:35:05 +01001346 int_pkt->int_desc.delivery_mode = APIC_DELIVERY_MODE_FIXED;
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001347
1348 /*
1349 * Create MSI w/ dummy vCPU set targeting just one vCPU, overwritten
1350 * by subsequent retarget in hv_irq_unmask().
1351 */
1352 cpu = cpumask_first_and(affinity, cpu_online_mask);
1353 int_pkt->int_desc.processor_array[0] =
Vitaly Kuznetsov7415aea2017-08-02 18:09:18 +02001354 hv_cpu_number_to_vp_number(cpu);
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001355 int_pkt->int_desc.processor_count = 1;
1356
1357 return sizeof(*int_pkt);
1358}
1359
Jake Oshins4daace02016-02-16 21:56:23 +00001360/**
1361 * hv_compose_msi_msg() - Supplies a valid MSI address/data
1362 * @data: Everything about this MSI
1363 * @msg: Buffer that is filled in by this function
1364 *
1365 * This function unpacks the IRQ looking for target CPU set, IDT
1366 * vector and mode and sends a message to the parent partition
1367 * asking for a mapping for that tuple in this partition. The
1368 * response supplies a data value and address to which that data
1369 * should be written to trigger that interrupt.
1370 */
1371static void hv_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
1372{
1373 struct irq_cfg *cfg = irqd_cfg(data);
1374 struct hv_pcibus_device *hbus;
Andrea Parri (Microsoft)240ad772020-04-06 02:15:10 +02001375 struct vmbus_channel *channel;
Jake Oshins4daace02016-02-16 21:56:23 +00001376 struct hv_pci_dev *hpdev;
1377 struct pci_bus *pbus;
1378 struct pci_dev *pdev;
Dexuan Cui79aa8012017-11-01 20:30:53 +00001379 struct cpumask *dest;
Jake Oshins4daace02016-02-16 21:56:23 +00001380 struct compose_comp_ctxt comp;
1381 struct tran_int_desc *int_desc;
Jake Oshins4daace02016-02-16 21:56:23 +00001382 struct {
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001383 struct pci_packet pci_pkt;
1384 union {
1385 struct pci_create_interrupt v1;
1386 struct pci_create_interrupt2 v2;
1387 } int_pkts;
1388 } __packed ctxt;
1389
1390 u32 size;
Jake Oshins4daace02016-02-16 21:56:23 +00001391 int ret;
1392
1393 pdev = msi_desc_to_pci_dev(irq_data_get_msi_desc(data));
Dexuan Cui79aa8012017-11-01 20:30:53 +00001394 dest = irq_data_get_effective_affinity_mask(data);
Jake Oshins4daace02016-02-16 21:56:23 +00001395 pbus = pdev->bus;
1396 hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata);
Andrea Parri (Microsoft)240ad772020-04-06 02:15:10 +02001397 channel = hbus->hdev->channel;
Jake Oshins4daace02016-02-16 21:56:23 +00001398 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
1399 if (!hpdev)
1400 goto return_null_message;
1401
1402 /* Free any previous message that might have already been composed. */
1403 if (data->chip_data) {
1404 int_desc = data->chip_data;
1405 data->chip_data = NULL;
1406 hv_int_desc_free(hpdev, int_desc);
1407 }
1408
K. Y. Srinivasan59c58cee2017-03-24 11:07:22 -07001409 int_desc = kzalloc(sizeof(*int_desc), GFP_ATOMIC);
Jake Oshins4daace02016-02-16 21:56:23 +00001410 if (!int_desc)
1411 goto drop_reference;
1412
1413 memset(&ctxt, 0, sizeof(ctxt));
1414 init_completion(&comp.comp_pkt.host_event);
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001415 ctxt.pci_pkt.completion_func = hv_pci_compose_compl;
1416 ctxt.pci_pkt.compl_ctxt = &comp;
Jake Oshins4daace02016-02-16 21:56:23 +00001417
Dexuan Cui14ef39f2019-11-24 21:33:53 -08001418 switch (hbus->protocol_version) {
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001419 case PCI_PROTOCOL_VERSION_1_1:
1420 size = hv_compose_msi_req_v1(&ctxt.int_pkts.v1,
Dexuan Cui79aa8012017-11-01 20:30:53 +00001421 dest,
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001422 hpdev->desc.win_slot.slot,
1423 cfg->vector);
1424 break;
1425
1426 case PCI_PROTOCOL_VERSION_1_2:
Long Li999dd952020-02-25 21:06:08 -08001427 case PCI_PROTOCOL_VERSION_1_3:
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001428 size = hv_compose_msi_req_v2(&ctxt.int_pkts.v2,
Dexuan Cui79aa8012017-11-01 20:30:53 +00001429 dest,
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001430 hpdev->desc.win_slot.slot,
1431 cfg->vector);
1432 break;
1433
1434 default:
1435 /* As we only negotiate protocol versions known to this driver,
1436 * this path should never hit. However, this is it not a hot
1437 * path so we print a message to aid future updates.
1438 */
1439 dev_err(&hbus->hdev->device,
1440 "Unexpected vPCI protocol, update driver.");
1441 goto free_int_desc;
Jake Oshins4daace02016-02-16 21:56:23 +00001442 }
1443
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001444 ret = vmbus_sendpacket(hpdev->hbus->hdev->channel, &ctxt.int_pkts,
1445 size, (unsigned long)&ctxt.pci_pkt,
Jake Oshins4daace02016-02-16 21:56:23 +00001446 VM_PKT_DATA_INBAND,
1447 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001448 if (ret) {
1449 dev_err(&hbus->hdev->device,
1450 "Sending request for interrupt failed: 0x%x",
1451 comp.comp_pkt.completion_status);
Dexuan Cui665e2242016-08-23 04:48:11 +00001452 goto free_int_desc;
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001453 }
Dexuan Cui665e2242016-08-23 04:48:11 +00001454
Stephen Hemminger80bfeeb2017-07-31 16:48:29 -07001455 /*
Andrea Parri (Microsoft)240ad772020-04-06 02:15:10 +02001456 * Prevents hv_pci_onchannelcallback() from running concurrently
1457 * in the tasklet.
1458 */
Sebastian Andrzej Siewiorbe4017c2021-03-09 09:42:15 +01001459 tasklet_disable_in_atomic(&channel->callback_event);
Andrea Parri (Microsoft)240ad772020-04-06 02:15:10 +02001460
1461 /*
Stephen Hemminger80bfeeb2017-07-31 16:48:29 -07001462 * Since this function is called with IRQ locks held, can't
1463 * do normal wait for completion; instead poll.
1464 */
Dexuan Cuide0aa7b2018-03-15 14:21:08 +00001465 while (!try_wait_for_completion(&comp.comp_pkt.host_event)) {
Andrea Parri (Microsoft)240ad772020-04-06 02:15:10 +02001466 unsigned long flags;
1467
Dexuan Cuide0aa7b2018-03-15 14:21:08 +00001468 /* 0xFFFF means an invalid PCI VENDOR ID. */
1469 if (hv_pcifront_get_vendor_id(hpdev) == 0xFFFF) {
1470 dev_err_once(&hbus->hdev->device,
1471 "the device has gone\n");
Andrea Parri (Microsoft)240ad772020-04-06 02:15:10 +02001472 goto enable_tasklet;
Dexuan Cuide0aa7b2018-03-15 14:21:08 +00001473 }
1474
1475 /*
Andrea Parri (Microsoft)240ad772020-04-06 02:15:10 +02001476 * Make sure that the ring buffer data structure doesn't get
1477 * freed while we dereference the ring buffer pointer. Test
1478 * for the channel's onchannel_callback being NULL within a
1479 * sched_lock critical section. See also the inline comments
1480 * in vmbus_reset_channel_cb().
Dexuan Cuide0aa7b2018-03-15 14:21:08 +00001481 */
Andrea Parri (Microsoft)240ad772020-04-06 02:15:10 +02001482 spin_lock_irqsave(&channel->sched_lock, flags);
1483 if (unlikely(channel->onchannel_callback == NULL)) {
1484 spin_unlock_irqrestore(&channel->sched_lock, flags);
1485 goto enable_tasklet;
1486 }
1487 hv_pci_onchannelcallback(hbus);
1488 spin_unlock_irqrestore(&channel->sched_lock, flags);
Dexuan Cuide0aa7b2018-03-15 14:21:08 +00001489
1490 if (hpdev->state == hv_pcichild_ejecting) {
1491 dev_err_once(&hbus->hdev->device,
1492 "the device is being ejected\n");
Andrea Parri (Microsoft)240ad772020-04-06 02:15:10 +02001493 goto enable_tasklet;
Dexuan Cuide0aa7b2018-03-15 14:21:08 +00001494 }
1495
Stephen Hemminger80bfeeb2017-07-31 16:48:29 -07001496 udelay(100);
Dexuan Cuide0aa7b2018-03-15 14:21:08 +00001497 }
Jake Oshins4daace02016-02-16 21:56:23 +00001498
Andrea Parri (Microsoft)240ad772020-04-06 02:15:10 +02001499 tasklet_enable(&channel->callback_event);
1500
Jake Oshins4daace02016-02-16 21:56:23 +00001501 if (comp.comp_pkt.completion_status < 0) {
1502 dev_err(&hbus->hdev->device,
1503 "Request for interrupt failed: 0x%x",
1504 comp.comp_pkt.completion_status);
1505 goto free_int_desc;
1506 }
1507
1508 /*
1509 * Record the assignment so that this can be unwound later. Using
1510 * irq_set_chip_data() here would be appropriate, but the lock it takes
1511 * is already held.
1512 */
1513 *int_desc = comp.int_desc;
1514 data->chip_data = int_desc;
1515
1516 /* Pass up the result. */
1517 msg->address_hi = comp.int_desc.address >> 32;
1518 msg->address_lo = comp.int_desc.address & 0xffffffff;
1519 msg->data = comp.int_desc.data;
1520
Stephen Hemminger8c99e122018-05-23 10:11:12 -07001521 put_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +00001522 return;
1523
Andrea Parri (Microsoft)240ad772020-04-06 02:15:10 +02001524enable_tasklet:
1525 tasklet_enable(&channel->callback_event);
Jake Oshins4daace02016-02-16 21:56:23 +00001526free_int_desc:
1527 kfree(int_desc);
1528drop_reference:
Stephen Hemminger8c99e122018-05-23 10:11:12 -07001529 put_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +00001530return_null_message:
1531 msg->address_hi = 0;
1532 msg->address_lo = 0;
1533 msg->data = 0;
1534}
1535
1536/* HW Interrupt Chip Descriptor */
1537static struct irq_chip hv_msi_irq_chip = {
1538 .name = "Hyper-V PCIe MSI",
1539 .irq_compose_msi_msg = hv_compose_msi_msg,
1540 .irq_set_affinity = hv_set_affinity,
1541 .irq_ack = irq_chip_ack_parent,
1542 .irq_mask = hv_irq_mask,
1543 .irq_unmask = hv_irq_unmask,
1544};
1545
Jake Oshins4daace02016-02-16 21:56:23 +00001546static struct msi_domain_ops hv_msi_ops = {
Jake Oshins4daace02016-02-16 21:56:23 +00001547 .msi_prepare = pci_msi_prepare,
Jake Oshins4daace02016-02-16 21:56:23 +00001548 .msi_free = hv_msi_free,
1549};
1550
1551/**
1552 * hv_pcie_init_irq_domain() - Initialize IRQ domain
1553 * @hbus: The root PCI bus
1554 *
1555 * This function creates an IRQ domain which will be used for
1556 * interrupts from devices that have been passed through. These
1557 * devices only support MSI and MSI-X, not line-based interrupts
1558 * or simulations of line-based interrupts through PCIe's
1559 * fabric-layer messages. Because interrupts are remapped, we
1560 * can support multi-message MSI here.
1561 *
1562 * Return: '0' on success and error value on failure
1563 */
1564static int hv_pcie_init_irq_domain(struct hv_pcibus_device *hbus)
1565{
1566 hbus->msi_info.chip = &hv_msi_irq_chip;
1567 hbus->msi_info.ops = &hv_msi_ops;
1568 hbus->msi_info.flags = (MSI_FLAG_USE_DEF_DOM_OPS |
1569 MSI_FLAG_USE_DEF_CHIP_OPS | MSI_FLAG_MULTI_PCI_MSI |
1570 MSI_FLAG_PCI_MSIX);
1571 hbus->msi_info.handler = handle_edge_irq;
1572 hbus->msi_info.handler_name = "edge";
1573 hbus->msi_info.data = hbus;
1574 hbus->irq_domain = pci_msi_create_irq_domain(hbus->sysdata.fwnode,
1575 &hbus->msi_info,
1576 x86_vector_domain);
1577 if (!hbus->irq_domain) {
1578 dev_err(&hbus->hdev->device,
1579 "Failed to build an MSI IRQ domain\n");
1580 return -ENODEV;
1581 }
1582
1583 return 0;
1584}
1585
1586/**
1587 * get_bar_size() - Get the address space consumed by a BAR
1588 * @bar_val: Value that a BAR returned after -1 was written
1589 * to it.
1590 *
1591 * This function returns the size of the BAR, rounded up to 1
1592 * page. It has to be rounded up because the hypervisor's page
1593 * table entry that maps the BAR into the VM can't specify an
1594 * offset within a page. The invariant is that the hypervisor
1595 * must place any BARs of smaller than page length at the
1596 * beginning of a page.
1597 *
1598 * Return: Size in bytes of the consumed MMIO space.
1599 */
1600static u64 get_bar_size(u64 bar_val)
1601{
1602 return round_up((1 + ~(bar_val & PCI_BASE_ADDRESS_MEM_MASK)),
1603 PAGE_SIZE);
1604}
1605
1606/**
1607 * survey_child_resources() - Total all MMIO requirements
1608 * @hbus: Root PCI bus, as understood by this driver
1609 */
1610static void survey_child_resources(struct hv_pcibus_device *hbus)
1611{
Jake Oshins4daace02016-02-16 21:56:23 +00001612 struct hv_pci_dev *hpdev;
1613 resource_size_t bar_size = 0;
1614 unsigned long flags;
1615 struct completion *event;
1616 u64 bar_val;
1617 int i;
1618
1619 /* If nobody is waiting on the answer, don't compute it. */
1620 event = xchg(&hbus->survey_event, NULL);
1621 if (!event)
1622 return;
1623
1624 /* If the answer has already been computed, go with it. */
1625 if (hbus->low_mmio_space || hbus->high_mmio_space) {
1626 complete(event);
1627 return;
1628 }
1629
1630 spin_lock_irqsave(&hbus->device_list_lock, flags);
1631
1632 /*
1633 * Due to an interesting quirk of the PCI spec, all memory regions
1634 * for a child device are a power of 2 in size and aligned in memory,
1635 * so it's sufficient to just add them up without tracking alignment.
1636 */
Stephen Hemminger5b8db8f2018-05-23 10:11:14 -07001637 list_for_each_entry(hpdev, &hbus->children, list_entry) {
Denis Efremovc9c13ba2019-09-28 02:43:08 +03001638 for (i = 0; i < PCI_STD_NUM_BARS; i++) {
Jake Oshins4daace02016-02-16 21:56:23 +00001639 if (hpdev->probed_bar[i] & PCI_BASE_ADDRESS_SPACE_IO)
1640 dev_err(&hbus->hdev->device,
1641 "There's an I/O BAR in this list!\n");
1642
1643 if (hpdev->probed_bar[i] != 0) {
1644 /*
1645 * A probed BAR has all the upper bits set that
1646 * can be changed.
1647 */
1648
1649 bar_val = hpdev->probed_bar[i];
1650 if (bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64)
1651 bar_val |=
1652 ((u64)hpdev->probed_bar[++i] << 32);
1653 else
1654 bar_val |= 0xffffffff00000000ULL;
1655
1656 bar_size = get_bar_size(bar_val);
1657
1658 if (bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64)
1659 hbus->high_mmio_space += bar_size;
1660 else
1661 hbus->low_mmio_space += bar_size;
1662 }
1663 }
1664 }
1665
1666 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1667 complete(event);
1668}
1669
1670/**
1671 * prepopulate_bars() - Fill in BARs with defaults
1672 * @hbus: Root PCI bus, as understood by this driver
1673 *
1674 * The core PCI driver code seems much, much happier if the BARs
1675 * for a device have values upon first scan. So fill them in.
1676 * The algorithm below works down from large sizes to small,
1677 * attempting to pack the assignments optimally. The assumption,
1678 * enforced in other parts of the code, is that the beginning of
1679 * the memory-mapped I/O space will be aligned on the largest
1680 * BAR size.
1681 */
1682static void prepopulate_bars(struct hv_pcibus_device *hbus)
1683{
1684 resource_size_t high_size = 0;
1685 resource_size_t low_size = 0;
1686 resource_size_t high_base = 0;
1687 resource_size_t low_base = 0;
1688 resource_size_t bar_size;
1689 struct hv_pci_dev *hpdev;
Jake Oshins4daace02016-02-16 21:56:23 +00001690 unsigned long flags;
1691 u64 bar_val;
1692 u32 command;
1693 bool high;
1694 int i;
1695
1696 if (hbus->low_mmio_space) {
1697 low_size = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space));
1698 low_base = hbus->low_mmio_res->start;
1699 }
1700
1701 if (hbus->high_mmio_space) {
1702 high_size = 1ULL <<
1703 (63 - __builtin_clzll(hbus->high_mmio_space));
1704 high_base = hbus->high_mmio_res->start;
1705 }
1706
1707 spin_lock_irqsave(&hbus->device_list_lock, flags);
1708
Dexuan Cuiac82fc82019-11-24 21:33:52 -08001709 /*
1710 * Clear the memory enable bit, in case it's already set. This occurs
1711 * in the suspend path of hibernation, where the device is suspended,
1712 * resumed and suspended again: see hibernation_snapshot() and
1713 * hibernation_platform_enter().
1714 *
Bjorn Helgaasc77bfb52021-01-26 15:38:55 -06001715 * If the memory enable bit is already set, Hyper-V silently ignores
Dexuan Cuiac82fc82019-11-24 21:33:52 -08001716 * the below BAR updates, and the related PCI device driver can not
1717 * work, because reading from the device register(s) always returns
1718 * 0xFFFFFFFF.
1719 */
1720 list_for_each_entry(hpdev, &hbus->children, list_entry) {
1721 _hv_pcifront_read_config(hpdev, PCI_COMMAND, 2, &command);
1722 command &= ~PCI_COMMAND_MEMORY;
1723 _hv_pcifront_write_config(hpdev, PCI_COMMAND, 2, command);
1724 }
1725
Jake Oshins4daace02016-02-16 21:56:23 +00001726 /* Pick addresses for the BARs. */
1727 do {
Stephen Hemminger5b8db8f2018-05-23 10:11:14 -07001728 list_for_each_entry(hpdev, &hbus->children, list_entry) {
Denis Efremovc9c13ba2019-09-28 02:43:08 +03001729 for (i = 0; i < PCI_STD_NUM_BARS; i++) {
Jake Oshins4daace02016-02-16 21:56:23 +00001730 bar_val = hpdev->probed_bar[i];
1731 if (bar_val == 0)
1732 continue;
1733 high = bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64;
1734 if (high) {
1735 bar_val |=
1736 ((u64)hpdev->probed_bar[i + 1]
1737 << 32);
1738 } else {
1739 bar_val |= 0xffffffffULL << 32;
1740 }
1741 bar_size = get_bar_size(bar_val);
1742 if (high) {
1743 if (high_size != bar_size) {
1744 i++;
1745 continue;
1746 }
1747 _hv_pcifront_write_config(hpdev,
1748 PCI_BASE_ADDRESS_0 + (4 * i),
1749 4,
1750 (u32)(high_base & 0xffffff00));
1751 i++;
1752 _hv_pcifront_write_config(hpdev,
1753 PCI_BASE_ADDRESS_0 + (4 * i),
1754 4, (u32)(high_base >> 32));
1755 high_base += bar_size;
1756 } else {
1757 if (low_size != bar_size)
1758 continue;
1759 _hv_pcifront_write_config(hpdev,
1760 PCI_BASE_ADDRESS_0 + (4 * i),
1761 4,
1762 (u32)(low_base & 0xffffff00));
1763 low_base += bar_size;
1764 }
1765 }
1766 if (high_size <= 1 && low_size <= 1) {
1767 /* Set the memory enable bit. */
1768 _hv_pcifront_read_config(hpdev, PCI_COMMAND, 2,
1769 &command);
1770 command |= PCI_COMMAND_MEMORY;
1771 _hv_pcifront_write_config(hpdev, PCI_COMMAND, 2,
1772 command);
1773 break;
1774 }
1775 }
1776
1777 high_size >>= 1;
1778 low_size >>= 1;
1779 } while (high_size || low_size);
1780
1781 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1782}
1783
Stephen Hemmingera15f2c082018-09-14 12:54:56 -07001784/*
1785 * Assign entries in sysfs pci slot directory.
1786 *
1787 * Note that this function does not need to lock the children list
1788 * because it is called from pci_devices_present_work which
1789 * is serialized with hv_eject_device_work because they are on the
1790 * same ordered workqueue. Therefore hbus->children list will not change
1791 * even when pci_create_slot sleeps.
1792 */
1793static void hv_pci_assign_slots(struct hv_pcibus_device *hbus)
1794{
1795 struct hv_pci_dev *hpdev;
1796 char name[SLOT_NAME_SIZE];
1797 int slot_nr;
1798
1799 list_for_each_entry(hpdev, &hbus->children, list_entry) {
1800 if (hpdev->pci_slot)
1801 continue;
1802
1803 slot_nr = PCI_SLOT(wslot_to_devfn(hpdev->desc.win_slot.slot));
1804 snprintf(name, SLOT_NAME_SIZE, "%u", hpdev->desc.ser);
1805 hpdev->pci_slot = pci_create_slot(hbus->pci_bus, slot_nr,
1806 name, NULL);
Wei Yongjun54be5b82018-09-21 02:53:17 +00001807 if (IS_ERR(hpdev->pci_slot)) {
Stephen Hemmingera15f2c082018-09-14 12:54:56 -07001808 pr_warn("pci_create slot %s failed\n", name);
Wei Yongjun54be5b82018-09-21 02:53:17 +00001809 hpdev->pci_slot = NULL;
1810 }
Stephen Hemmingera15f2c082018-09-14 12:54:56 -07001811 }
1812}
1813
Dexuan Cui15becc22019-03-04 21:34:48 +00001814/*
1815 * Remove entries in sysfs pci slot directory.
1816 */
1817static void hv_pci_remove_slots(struct hv_pcibus_device *hbus)
1818{
1819 struct hv_pci_dev *hpdev;
1820
1821 list_for_each_entry(hpdev, &hbus->children, list_entry) {
1822 if (!hpdev->pci_slot)
1823 continue;
1824 pci_destroy_slot(hpdev->pci_slot);
1825 hpdev->pci_slot = NULL;
1826 }
1827}
1828
Long Li999dd952020-02-25 21:06:08 -08001829/*
1830 * Set NUMA node for the devices on the bus
1831 */
1832static void hv_pci_assign_numa_node(struct hv_pcibus_device *hbus)
1833{
1834 struct pci_dev *dev;
1835 struct pci_bus *bus = hbus->pci_bus;
1836 struct hv_pci_dev *hv_dev;
1837
1838 list_for_each_entry(dev, &bus->devices, bus_list) {
1839 hv_dev = get_pcichild_wslot(hbus, devfn_to_wslot(dev->devfn));
1840 if (!hv_dev)
1841 continue;
1842
1843 if (hv_dev->desc.flags & HV_PCI_DEVICE_FLAG_NUMA_AFFINITY)
1844 set_dev_node(&dev->dev, hv_dev->desc.virtual_numa_node);
1845
1846 put_pcichild(hv_dev);
1847 }
1848}
1849
Jake Oshins4daace02016-02-16 21:56:23 +00001850/**
1851 * create_root_hv_pci_bus() - Expose a new root PCI bus
1852 * @hbus: Root PCI bus, as understood by this driver
1853 *
1854 * Return: 0 on success, -errno on failure
1855 */
1856static int create_root_hv_pci_bus(struct hv_pcibus_device *hbus)
1857{
1858 /* Register the device */
1859 hbus->pci_bus = pci_create_root_bus(&hbus->hdev->device,
1860 0, /* bus number is always zero */
1861 &hv_pcifront_ops,
1862 &hbus->sysdata,
1863 &hbus->resources_for_children);
1864 if (!hbus->pci_bus)
1865 return -ENODEV;
1866
Long Li414428c2017-03-23 14:58:32 -07001867 pci_lock_rescan_remove();
Jake Oshins4daace02016-02-16 21:56:23 +00001868 pci_scan_child_bus(hbus->pci_bus);
Long Li999dd952020-02-25 21:06:08 -08001869 hv_pci_assign_numa_node(hbus);
Jake Oshins4daace02016-02-16 21:56:23 +00001870 pci_bus_assign_resources(hbus->pci_bus);
Stephen Hemmingera15f2c082018-09-14 12:54:56 -07001871 hv_pci_assign_slots(hbus);
Jake Oshins4daace02016-02-16 21:56:23 +00001872 pci_bus_add_devices(hbus->pci_bus);
Long Li414428c2017-03-23 14:58:32 -07001873 pci_unlock_rescan_remove();
Jake Oshins4daace02016-02-16 21:56:23 +00001874 hbus->state = hv_pcibus_installed;
1875 return 0;
1876}
1877
1878struct q_res_req_compl {
1879 struct completion host_event;
1880 struct hv_pci_dev *hpdev;
1881};
1882
1883/**
1884 * q_resource_requirements() - Query Resource Requirements
1885 * @context: The completion context.
1886 * @resp: The response that came from the host.
1887 * @resp_packet_size: The size in bytes of resp.
1888 *
1889 * This function is invoked on completion of a Query Resource
1890 * Requirements packet.
1891 */
1892static void q_resource_requirements(void *context, struct pci_response *resp,
1893 int resp_packet_size)
1894{
1895 struct q_res_req_compl *completion = context;
1896 struct pci_q_res_req_response *q_res_req =
1897 (struct pci_q_res_req_response *)resp;
1898 int i;
1899
1900 if (resp->status < 0) {
1901 dev_err(&completion->hpdev->hbus->hdev->device,
1902 "query resource requirements failed: %x\n",
1903 resp->status);
1904 } else {
Denis Efremovc9c13ba2019-09-28 02:43:08 +03001905 for (i = 0; i < PCI_STD_NUM_BARS; i++) {
Jake Oshins4daace02016-02-16 21:56:23 +00001906 completion->hpdev->probed_bar[i] =
1907 q_res_req->probed_bar[i];
1908 }
1909 }
1910
1911 complete(&completion->host_event);
1912}
1913
Jake Oshins4daace02016-02-16 21:56:23 +00001914/**
1915 * new_pcichild_device() - Create a new child device
1916 * @hbus: The internal struct tracking this root PCI bus.
1917 * @desc: The information supplied so far from the host
1918 * about the device.
1919 *
1920 * This function creates the tracking structure for a new child
1921 * device and kicks off the process of figuring out what it is.
1922 *
1923 * Return: Pointer to the new tracking struct
1924 */
1925static struct hv_pci_dev *new_pcichild_device(struct hv_pcibus_device *hbus,
Long Lif9ad0f32020-02-25 21:06:07 -08001926 struct hv_pcidev_description *desc)
Jake Oshins4daace02016-02-16 21:56:23 +00001927{
1928 struct hv_pci_dev *hpdev;
1929 struct pci_child_message *res_req;
1930 struct q_res_req_compl comp_pkt;
Dexuan Cui8286e962016-11-10 07:17:48 +00001931 struct {
1932 struct pci_packet init_packet;
1933 u8 buffer[sizeof(struct pci_child_message)];
Jake Oshins4daace02016-02-16 21:56:23 +00001934 } pkt;
1935 unsigned long flags;
1936 int ret;
1937
Jia-Ju Bai7403bd12018-03-18 22:53:28 +08001938 hpdev = kzalloc(sizeof(*hpdev), GFP_KERNEL);
Jake Oshins4daace02016-02-16 21:56:23 +00001939 if (!hpdev)
1940 return NULL;
1941
1942 hpdev->hbus = hbus;
1943
1944 memset(&pkt, 0, sizeof(pkt));
1945 init_completion(&comp_pkt.host_event);
1946 comp_pkt.hpdev = hpdev;
1947 pkt.init_packet.compl_ctxt = &comp_pkt;
1948 pkt.init_packet.completion_func = q_resource_requirements;
1949 res_req = (struct pci_child_message *)&pkt.init_packet.message;
Dexuan Cui0c6045d2016-08-23 04:45:51 +00001950 res_req->message_type.type = PCI_QUERY_RESOURCE_REQUIREMENTS;
Jake Oshins4daace02016-02-16 21:56:23 +00001951 res_req->wslot.slot = desc->win_slot.slot;
1952
1953 ret = vmbus_sendpacket(hbus->hdev->channel, res_req,
1954 sizeof(struct pci_child_message),
1955 (unsigned long)&pkt.init_packet,
1956 VM_PKT_DATA_INBAND,
1957 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1958 if (ret)
1959 goto error;
1960
Dexuan Cuic3635da2018-05-23 21:12:01 +00001961 if (wait_for_response(hbus->hdev, &comp_pkt.host_event))
1962 goto error;
Jake Oshins4daace02016-02-16 21:56:23 +00001963
1964 hpdev->desc = *desc;
Elena Reshetova24196f02017-04-18 09:02:48 -05001965 refcount_set(&hpdev->refs, 1);
Stephen Hemminger8c99e122018-05-23 10:11:12 -07001966 get_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +00001967 spin_lock_irqsave(&hbus->device_list_lock, flags);
Haiyang Zhang4a9b0932017-02-13 18:10:11 +00001968
Jake Oshins4daace02016-02-16 21:56:23 +00001969 list_add_tail(&hpdev->list_entry, &hbus->children);
1970 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1971 return hpdev;
1972
1973error:
1974 kfree(hpdev);
1975 return NULL;
1976}
1977
1978/**
1979 * get_pcichild_wslot() - Find device from slot
1980 * @hbus: Root PCI bus, as understood by this driver
1981 * @wslot: Location on the bus
1982 *
1983 * This function looks up a PCI device and returns the internal
1984 * representation of it. It acquires a reference on it, so that
1985 * the device won't be deleted while somebody is using it. The
1986 * caller is responsible for calling put_pcichild() to release
1987 * this reference.
1988 *
1989 * Return: Internal representation of a PCI device
1990 */
1991static struct hv_pci_dev *get_pcichild_wslot(struct hv_pcibus_device *hbus,
1992 u32 wslot)
1993{
1994 unsigned long flags;
1995 struct hv_pci_dev *iter, *hpdev = NULL;
1996
1997 spin_lock_irqsave(&hbus->device_list_lock, flags);
1998 list_for_each_entry(iter, &hbus->children, list_entry) {
1999 if (iter->desc.win_slot.slot == wslot) {
2000 hpdev = iter;
Stephen Hemminger8c99e122018-05-23 10:11:12 -07002001 get_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +00002002 break;
2003 }
2004 }
2005 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
2006
2007 return hpdev;
2008}
2009
2010/**
2011 * pci_devices_present_work() - Handle new list of child devices
2012 * @work: Work struct embedded in struct hv_dr_work
2013 *
2014 * "Bus Relations" is the Windows term for "children of this
2015 * bus." The terminology is preserved here for people trying to
2016 * debug the interaction between Hyper-V and Linux. This
2017 * function is called when the parent partition reports a list
2018 * of functions that should be observed under this PCI Express
2019 * port (bus).
2020 *
2021 * This function updates the list, and must tolerate being
2022 * called multiple times with the same information. The typical
2023 * number of child devices is one, with very atypical cases
2024 * involving three or four, so the algorithms used here can be
2025 * simple and inefficient.
2026 *
2027 * It must also treat the omission of a previously observed device as
2028 * notification that the device no longer exists.
2029 *
Dexuan Cui021ad272018-03-15 14:20:53 +00002030 * Note that this function is serialized with hv_eject_device_work(),
2031 * because both are pushed to the ordered workqueue hbus->wq.
Jake Oshins4daace02016-02-16 21:56:23 +00002032 */
2033static void pci_devices_present_work(struct work_struct *work)
2034{
2035 u32 child_no;
2036 bool found;
Long Lif9ad0f32020-02-25 21:06:07 -08002037 struct hv_pcidev_description *new_desc;
Jake Oshins4daace02016-02-16 21:56:23 +00002038 struct hv_pci_dev *hpdev;
2039 struct hv_pcibus_device *hbus;
2040 struct list_head removed;
2041 struct hv_dr_work *dr_wrk;
2042 struct hv_dr_state *dr = NULL;
2043 unsigned long flags;
2044
2045 dr_wrk = container_of(work, struct hv_dr_work, wrk);
2046 hbus = dr_wrk->bus;
2047 kfree(dr_wrk);
2048
2049 INIT_LIST_HEAD(&removed);
2050
Jake Oshins4daace02016-02-16 21:56:23 +00002051 /* Pull this off the queue and process it if it was the last one. */
2052 spin_lock_irqsave(&hbus->device_list_lock, flags);
2053 while (!list_empty(&hbus->dr_list)) {
2054 dr = list_first_entry(&hbus->dr_list, struct hv_dr_state,
2055 list_entry);
2056 list_del(&dr->list_entry);
2057
2058 /* Throw this away if the list still has stuff in it. */
2059 if (!list_empty(&hbus->dr_list)) {
2060 kfree(dr);
2061 continue;
2062 }
2063 }
2064 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
2065
2066 if (!dr) {
Jake Oshins4daace02016-02-16 21:56:23 +00002067 put_hvpcibus(hbus);
2068 return;
2069 }
2070
2071 /* First, mark all existing children as reported missing. */
2072 spin_lock_irqsave(&hbus->device_list_lock, flags);
Stephen Hemminger5b8db8f2018-05-23 10:11:14 -07002073 list_for_each_entry(hpdev, &hbus->children, list_entry) {
2074 hpdev->reported_missing = true;
Jake Oshins4daace02016-02-16 21:56:23 +00002075 }
2076 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
2077
2078 /* Next, add back any reported devices. */
2079 for (child_no = 0; child_no < dr->device_count; child_no++) {
2080 found = false;
2081 new_desc = &dr->func[child_no];
2082
2083 spin_lock_irqsave(&hbus->device_list_lock, flags);
Stephen Hemminger5b8db8f2018-05-23 10:11:14 -07002084 list_for_each_entry(hpdev, &hbus->children, list_entry) {
2085 if ((hpdev->desc.win_slot.slot == new_desc->win_slot.slot) &&
Jake Oshins4daace02016-02-16 21:56:23 +00002086 (hpdev->desc.v_id == new_desc->v_id) &&
2087 (hpdev->desc.d_id == new_desc->d_id) &&
2088 (hpdev->desc.ser == new_desc->ser)) {
2089 hpdev->reported_missing = false;
2090 found = true;
2091 }
2092 }
2093 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
2094
2095 if (!found) {
2096 hpdev = new_pcichild_device(hbus, new_desc);
2097 if (!hpdev)
2098 dev_err(&hbus->hdev->device,
2099 "couldn't record a child device.\n");
2100 }
2101 }
2102
2103 /* Move missing children to a list on the stack. */
2104 spin_lock_irqsave(&hbus->device_list_lock, flags);
2105 do {
2106 found = false;
Stephen Hemminger5b8db8f2018-05-23 10:11:14 -07002107 list_for_each_entry(hpdev, &hbus->children, list_entry) {
Jake Oshins4daace02016-02-16 21:56:23 +00002108 if (hpdev->reported_missing) {
2109 found = true;
Stephen Hemminger8c99e122018-05-23 10:11:12 -07002110 put_pcichild(hpdev);
Wei Yongjun4f1cb012016-07-28 16:16:48 +00002111 list_move_tail(&hpdev->list_entry, &removed);
Jake Oshins4daace02016-02-16 21:56:23 +00002112 break;
2113 }
2114 }
2115 } while (found);
2116 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
2117
2118 /* Delete everything that should no longer exist. */
2119 while (!list_empty(&removed)) {
2120 hpdev = list_first_entry(&removed, struct hv_pci_dev,
2121 list_entry);
2122 list_del(&hpdev->list_entry);
Dexuan Cui340d4552019-03-04 21:34:49 +00002123
2124 if (hpdev->pci_slot)
2125 pci_destroy_slot(hpdev->pci_slot);
2126
Stephen Hemminger8c99e122018-05-23 10:11:12 -07002127 put_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +00002128 }
2129
Jork Loeser691ac1d2017-05-24 13:41:24 -07002130 switch (hbus->state) {
Long Lid3a78d82017-03-23 14:58:10 -07002131 case hv_pcibus_installed:
2132 /*
Jork Loeser691ac1d2017-05-24 13:41:24 -07002133 * Tell the core to rescan bus
2134 * because there may have been changes.
2135 */
Jake Oshins4daace02016-02-16 21:56:23 +00002136 pci_lock_rescan_remove();
2137 pci_scan_child_bus(hbus->pci_bus);
Long Li999dd952020-02-25 21:06:08 -08002138 hv_pci_assign_numa_node(hbus);
Stephen Hemmingera15f2c082018-09-14 12:54:56 -07002139 hv_pci_assign_slots(hbus);
Jake Oshins4daace02016-02-16 21:56:23 +00002140 pci_unlock_rescan_remove();
Long Lid3a78d82017-03-23 14:58:10 -07002141 break;
2142
2143 case hv_pcibus_init:
2144 case hv_pcibus_probed:
Jake Oshins4daace02016-02-16 21:56:23 +00002145 survey_child_resources(hbus);
Long Lid3a78d82017-03-23 14:58:10 -07002146 break;
2147
2148 default:
2149 break;
Jake Oshins4daace02016-02-16 21:56:23 +00002150 }
2151
Jake Oshins4daace02016-02-16 21:56:23 +00002152 put_hvpcibus(hbus);
2153 kfree(dr);
2154}
2155
2156/**
Long Lif9ad0f32020-02-25 21:06:07 -08002157 * hv_pci_start_relations_work() - Queue work to start device discovery
Jake Oshins4daace02016-02-16 21:56:23 +00002158 * @hbus: Root PCI bus, as understood by this driver
Long Lif9ad0f32020-02-25 21:06:07 -08002159 * @dr: The list of children returned from host
Jake Oshins4daace02016-02-16 21:56:23 +00002160 *
Long Lif9ad0f32020-02-25 21:06:07 -08002161 * Return: 0 on success, -errno on failure
Jake Oshins4daace02016-02-16 21:56:23 +00002162 */
Long Lif9ad0f32020-02-25 21:06:07 -08002163static int hv_pci_start_relations_work(struct hv_pcibus_device *hbus,
2164 struct hv_dr_state *dr)
Jake Oshins4daace02016-02-16 21:56:23 +00002165{
Jake Oshins4daace02016-02-16 21:56:23 +00002166 struct hv_dr_work *dr_wrk;
2167 unsigned long flags;
Dexuan Cui948373b2018-03-15 14:22:00 +00002168 bool pending_dr;
Jake Oshins4daace02016-02-16 21:56:23 +00002169
Dexuan Cuiac82fc82019-11-24 21:33:52 -08002170 if (hbus->state == hv_pcibus_removing) {
2171 dev_info(&hbus->hdev->device,
2172 "PCI VMBus BUS_RELATIONS: ignored\n");
Long Lif9ad0f32020-02-25 21:06:07 -08002173 return -ENOENT;
Dexuan Cuiac82fc82019-11-24 21:33:52 -08002174 }
2175
Jake Oshins4daace02016-02-16 21:56:23 +00002176 dr_wrk = kzalloc(sizeof(*dr_wrk), GFP_NOWAIT);
2177 if (!dr_wrk)
Long Lif9ad0f32020-02-25 21:06:07 -08002178 return -ENOMEM;
Jake Oshins4daace02016-02-16 21:56:23 +00002179
2180 INIT_WORK(&dr_wrk->wrk, pci_devices_present_work);
2181 dr_wrk->bus = hbus;
Jake Oshins4daace02016-02-16 21:56:23 +00002182
2183 spin_lock_irqsave(&hbus->device_list_lock, flags);
Dexuan Cui948373b2018-03-15 14:22:00 +00002184 /*
2185 * If pending_dr is true, we have already queued a work,
2186 * which will see the new dr. Otherwise, we need to
2187 * queue a new work.
2188 */
2189 pending_dr = !list_empty(&hbus->dr_list);
Jake Oshins4daace02016-02-16 21:56:23 +00002190 list_add_tail(&dr->list_entry, &hbus->dr_list);
2191 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
2192
Dexuan Cui948373b2018-03-15 14:22:00 +00002193 if (pending_dr) {
2194 kfree(dr_wrk);
2195 } else {
2196 get_hvpcibus(hbus);
2197 queue_work(hbus->wq, &dr_wrk->wrk);
2198 }
Long Lif9ad0f32020-02-25 21:06:07 -08002199
2200 return 0;
2201}
2202
2203/**
2204 * hv_pci_devices_present() - Handle list of new children
2205 * @hbus: Root PCI bus, as understood by this driver
2206 * @relations: Packet from host listing children
2207 *
2208 * Process a new list of devices on the bus. The list of devices is
2209 * discovered by VSP and sent to us via VSP message PCI_BUS_RELATIONS,
2210 * whenever a new list of devices for this bus appears.
2211 */
2212static void hv_pci_devices_present(struct hv_pcibus_device *hbus,
2213 struct pci_bus_relations *relations)
2214{
2215 struct hv_dr_state *dr;
2216 int i;
2217
Gustavo A. R. Silvad0684fd02020-05-25 11:43:19 -05002218 dr = kzalloc(struct_size(dr, func, relations->device_count),
2219 GFP_NOWAIT);
Long Lif9ad0f32020-02-25 21:06:07 -08002220 if (!dr)
2221 return;
2222
2223 dr->device_count = relations->device_count;
2224 for (i = 0; i < dr->device_count; i++) {
2225 dr->func[i].v_id = relations->func[i].v_id;
2226 dr->func[i].d_id = relations->func[i].d_id;
2227 dr->func[i].rev = relations->func[i].rev;
2228 dr->func[i].prog_intf = relations->func[i].prog_intf;
2229 dr->func[i].subclass = relations->func[i].subclass;
2230 dr->func[i].base_class = relations->func[i].base_class;
2231 dr->func[i].subsystem_id = relations->func[i].subsystem_id;
2232 dr->func[i].win_slot = relations->func[i].win_slot;
2233 dr->func[i].ser = relations->func[i].ser;
2234 }
2235
2236 if (hv_pci_start_relations_work(hbus, dr))
2237 kfree(dr);
Jake Oshins4daace02016-02-16 21:56:23 +00002238}
2239
2240/**
Long Li999dd952020-02-25 21:06:08 -08002241 * hv_pci_devices_present2() - Handle list of new children
2242 * @hbus: Root PCI bus, as understood by this driver
2243 * @relations: Packet from host listing children
2244 *
2245 * This function is the v2 version of hv_pci_devices_present()
2246 */
2247static void hv_pci_devices_present2(struct hv_pcibus_device *hbus,
2248 struct pci_bus_relations2 *relations)
2249{
2250 struct hv_dr_state *dr;
2251 int i;
2252
Gustavo A. R. Silvad0684fd02020-05-25 11:43:19 -05002253 dr = kzalloc(struct_size(dr, func, relations->device_count),
2254 GFP_NOWAIT);
Long Li999dd952020-02-25 21:06:08 -08002255 if (!dr)
2256 return;
2257
2258 dr->device_count = relations->device_count;
2259 for (i = 0; i < dr->device_count; i++) {
2260 dr->func[i].v_id = relations->func[i].v_id;
2261 dr->func[i].d_id = relations->func[i].d_id;
2262 dr->func[i].rev = relations->func[i].rev;
2263 dr->func[i].prog_intf = relations->func[i].prog_intf;
2264 dr->func[i].subclass = relations->func[i].subclass;
2265 dr->func[i].base_class = relations->func[i].base_class;
2266 dr->func[i].subsystem_id = relations->func[i].subsystem_id;
2267 dr->func[i].win_slot = relations->func[i].win_slot;
2268 dr->func[i].ser = relations->func[i].ser;
2269 dr->func[i].flags = relations->func[i].flags;
2270 dr->func[i].virtual_numa_node =
2271 relations->func[i].virtual_numa_node;
2272 }
2273
2274 if (hv_pci_start_relations_work(hbus, dr))
2275 kfree(dr);
2276}
2277
2278/**
Jake Oshins4daace02016-02-16 21:56:23 +00002279 * hv_eject_device_work() - Asynchronously handles ejection
2280 * @work: Work struct embedded in internal device struct
2281 *
2282 * This function handles ejecting a device. Windows will
2283 * attempt to gracefully eject a device, waiting 60 seconds to
2284 * hear back from the guest OS that this completed successfully.
2285 * If this timer expires, the device will be forcibly removed.
2286 */
2287static void hv_eject_device_work(struct work_struct *work)
2288{
2289 struct pci_eject_response *ejct_pkt;
Dexuan Cui4df591b22019-06-21 23:45:23 +00002290 struct hv_pcibus_device *hbus;
Jake Oshins4daace02016-02-16 21:56:23 +00002291 struct hv_pci_dev *hpdev;
2292 struct pci_dev *pdev;
2293 unsigned long flags;
2294 int wslot;
2295 struct {
2296 struct pci_packet pkt;
Dexuan Cui0c6045d2016-08-23 04:45:51 +00002297 u8 buffer[sizeof(struct pci_eject_response)];
Jake Oshins4daace02016-02-16 21:56:23 +00002298 } ctxt;
2299
2300 hpdev = container_of(work, struct hv_pci_dev, wrk);
Dexuan Cui4df591b22019-06-21 23:45:23 +00002301 hbus = hpdev->hbus;
Jake Oshins4daace02016-02-16 21:56:23 +00002302
Dexuan Cuifca288c2018-03-15 14:21:43 +00002303 WARN_ON(hpdev->state != hv_pcichild_ejecting);
Jake Oshins4daace02016-02-16 21:56:23 +00002304
2305 /*
2306 * Ejection can come before or after the PCI bus has been set up, so
2307 * attempt to find it and tear down the bus state, if it exists. This
2308 * must be done without constructs like pci_domain_nr(hbus->pci_bus)
2309 * because hbus->pci_bus may not exist yet.
2310 */
2311 wslot = wslot_to_devfn(hpdev->desc.win_slot.slot);
Dexuan Cui4df591b22019-06-21 23:45:23 +00002312 pdev = pci_get_domain_bus_and_slot(hbus->sysdata.domain, 0, wslot);
Jake Oshins4daace02016-02-16 21:56:23 +00002313 if (pdev) {
Long Li414428c2017-03-23 14:58:32 -07002314 pci_lock_rescan_remove();
Jake Oshins4daace02016-02-16 21:56:23 +00002315 pci_stop_and_remove_bus_device(pdev);
2316 pci_dev_put(pdev);
Long Li414428c2017-03-23 14:58:32 -07002317 pci_unlock_rescan_remove();
Jake Oshins4daace02016-02-16 21:56:23 +00002318 }
2319
Dexuan Cui4df591b22019-06-21 23:45:23 +00002320 spin_lock_irqsave(&hbus->device_list_lock, flags);
Dexuan Cuie74d2eb2016-11-10 07:19:52 +00002321 list_del(&hpdev->list_entry);
Dexuan Cui4df591b22019-06-21 23:45:23 +00002322 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
Dexuan Cuie74d2eb2016-11-10 07:19:52 +00002323
Stephen Hemmingera15f2c082018-09-14 12:54:56 -07002324 if (hpdev->pci_slot)
2325 pci_destroy_slot(hpdev->pci_slot);
2326
Jake Oshins4daace02016-02-16 21:56:23 +00002327 memset(&ctxt, 0, sizeof(ctxt));
2328 ejct_pkt = (struct pci_eject_response *)&ctxt.pkt.message;
Dexuan Cui0c6045d2016-08-23 04:45:51 +00002329 ejct_pkt->message_type.type = PCI_EJECTION_COMPLETE;
Jake Oshins4daace02016-02-16 21:56:23 +00002330 ejct_pkt->wslot.slot = hpdev->desc.win_slot.slot;
Dexuan Cui4df591b22019-06-21 23:45:23 +00002331 vmbus_sendpacket(hbus->hdev->channel, ejct_pkt,
Jake Oshins4daace02016-02-16 21:56:23 +00002332 sizeof(*ejct_pkt), (unsigned long)&ctxt.pkt,
2333 VM_PKT_DATA_INBAND, 0);
2334
Dexuan Cui05f151a2019-03-04 21:34:48 +00002335 /* For the get_pcichild() in hv_pci_eject_device() */
2336 put_pcichild(hpdev);
2337 /* For the two refs got in new_pcichild_device() */
Stephen Hemminger8c99e122018-05-23 10:11:12 -07002338 put_pcichild(hpdev);
2339 put_pcichild(hpdev);
Dexuan Cui4df591b22019-06-21 23:45:23 +00002340 /* hpdev has been freed. Do not use it any more. */
2341
2342 put_hvpcibus(hbus);
Jake Oshins4daace02016-02-16 21:56:23 +00002343}
2344
2345/**
2346 * hv_pci_eject_device() - Handles device ejection
2347 * @hpdev: Internal device tracking struct
2348 *
2349 * This function is invoked when an ejection packet arrives. It
2350 * just schedules work so that we don't re-enter the packet
2351 * delivery code handling the ejection.
2352 */
2353static void hv_pci_eject_device(struct hv_pci_dev *hpdev)
2354{
Dexuan Cuiac82fc82019-11-24 21:33:52 -08002355 struct hv_pcibus_device *hbus = hpdev->hbus;
2356 struct hv_device *hdev = hbus->hdev;
2357
2358 if (hbus->state == hv_pcibus_removing) {
2359 dev_info(&hdev->device, "PCI VMBus EJECT: ignored\n");
2360 return;
2361 }
2362
Jake Oshins4daace02016-02-16 21:56:23 +00002363 hpdev->state = hv_pcichild_ejecting;
Stephen Hemminger8c99e122018-05-23 10:11:12 -07002364 get_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +00002365 INIT_WORK(&hpdev->wrk, hv_eject_device_work);
Dexuan Cuiac82fc82019-11-24 21:33:52 -08002366 get_hvpcibus(hbus);
2367 queue_work(hbus->wq, &hpdev->wrk);
Jake Oshins4daace02016-02-16 21:56:23 +00002368}
2369
2370/**
2371 * hv_pci_onchannelcallback() - Handles incoming packets
2372 * @context: Internal bus tracking struct
2373 *
2374 * This function is invoked whenever the host sends a packet to
2375 * this channel (which is private to this root PCI bus).
2376 */
2377static void hv_pci_onchannelcallback(void *context)
2378{
2379 const int packet_size = 0x100;
2380 int ret;
2381 struct hv_pcibus_device *hbus = context;
2382 u32 bytes_recvd;
2383 u64 req_id;
2384 struct vmpacket_descriptor *desc;
2385 unsigned char *buffer;
2386 int bufferlen = packet_size;
2387 struct pci_packet *comp_packet;
2388 struct pci_response *response;
2389 struct pci_incoming_message *new_message;
2390 struct pci_bus_relations *bus_rel;
Long Li999dd952020-02-25 21:06:08 -08002391 struct pci_bus_relations2 *bus_rel2;
Dexuan Cuie5d2f912019-08-22 05:05:37 +00002392 struct pci_dev_inval_block *inval;
Jake Oshins4daace02016-02-16 21:56:23 +00002393 struct pci_dev_incoming *dev_message;
2394 struct hv_pci_dev *hpdev;
2395
2396 buffer = kmalloc(bufferlen, GFP_ATOMIC);
2397 if (!buffer)
2398 return;
2399
2400 while (1) {
2401 ret = vmbus_recvpacket_raw(hbus->hdev->channel, buffer,
2402 bufferlen, &bytes_recvd, &req_id);
2403
2404 if (ret == -ENOBUFS) {
2405 kfree(buffer);
2406 /* Handle large packet */
2407 bufferlen = bytes_recvd;
2408 buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
2409 if (!buffer)
2410 return;
2411 continue;
2412 }
2413
Vitaly Kuznetsov837d7412016-06-17 12:45:30 -05002414 /* Zero length indicates there are no more packets. */
2415 if (ret || !bytes_recvd)
2416 break;
2417
Jake Oshins4daace02016-02-16 21:56:23 +00002418 /*
2419 * All incoming packets must be at least as large as a
2420 * response.
2421 */
Vitaly Kuznetsov60fcdac2016-05-30 16:17:58 +02002422 if (bytes_recvd <= sizeof(struct pci_response))
Vitaly Kuznetsov837d7412016-06-17 12:45:30 -05002423 continue;
Jake Oshins4daace02016-02-16 21:56:23 +00002424 desc = (struct vmpacket_descriptor *)buffer;
2425
2426 switch (desc->type) {
2427 case VM_PKT_COMP:
2428
2429 /*
2430 * The host is trusted, and thus it's safe to interpret
2431 * this transaction ID as a pointer.
2432 */
2433 comp_packet = (struct pci_packet *)req_id;
2434 response = (struct pci_response *)buffer;
2435 comp_packet->completion_func(comp_packet->compl_ctxt,
2436 response,
2437 bytes_recvd);
Vitaly Kuznetsov60fcdac2016-05-30 16:17:58 +02002438 break;
Jake Oshins4daace02016-02-16 21:56:23 +00002439
2440 case VM_PKT_DATA_INBAND:
2441
2442 new_message = (struct pci_incoming_message *)buffer;
Dexuan Cui0c6045d2016-08-23 04:45:51 +00002443 switch (new_message->message_type.type) {
Jake Oshins4daace02016-02-16 21:56:23 +00002444 case PCI_BUS_RELATIONS:
2445
2446 bus_rel = (struct pci_bus_relations *)buffer;
2447 if (bytes_recvd <
Gustavo A. R. Silvad0684fd02020-05-25 11:43:19 -05002448 struct_size(bus_rel, func,
2449 bus_rel->device_count)) {
Jake Oshins4daace02016-02-16 21:56:23 +00002450 dev_err(&hbus->hdev->device,
2451 "bus relations too small\n");
2452 break;
2453 }
2454
2455 hv_pci_devices_present(hbus, bus_rel);
2456 break;
2457
Long Li999dd952020-02-25 21:06:08 -08002458 case PCI_BUS_RELATIONS2:
2459
2460 bus_rel2 = (struct pci_bus_relations2 *)buffer;
2461 if (bytes_recvd <
Gustavo A. R. Silvad0684fd02020-05-25 11:43:19 -05002462 struct_size(bus_rel2, func,
2463 bus_rel2->device_count)) {
Long Li999dd952020-02-25 21:06:08 -08002464 dev_err(&hbus->hdev->device,
2465 "bus relations v2 too small\n");
2466 break;
2467 }
2468
2469 hv_pci_devices_present2(hbus, bus_rel2);
2470 break;
2471
Jake Oshins4daace02016-02-16 21:56:23 +00002472 case PCI_EJECT:
2473
2474 dev_message = (struct pci_dev_incoming *)buffer;
2475 hpdev = get_pcichild_wslot(hbus,
2476 dev_message->wslot.slot);
2477 if (hpdev) {
2478 hv_pci_eject_device(hpdev);
Stephen Hemminger8c99e122018-05-23 10:11:12 -07002479 put_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +00002480 }
2481 break;
2482
Dexuan Cuie5d2f912019-08-22 05:05:37 +00002483 case PCI_INVALIDATE_BLOCK:
2484
2485 inval = (struct pci_dev_inval_block *)buffer;
2486 hpdev = get_pcichild_wslot(hbus,
2487 inval->wslot.slot);
2488 if (hpdev) {
2489 if (hpdev->block_invalidate) {
2490 hpdev->block_invalidate(
2491 hpdev->invalidate_context,
2492 inval->block_mask);
2493 }
2494 put_pcichild(hpdev);
2495 }
2496 break;
2497
Jake Oshins4daace02016-02-16 21:56:23 +00002498 default:
2499 dev_warn(&hbus->hdev->device,
2500 "Unimplemented protocol message %x\n",
Dexuan Cui0c6045d2016-08-23 04:45:51 +00002501 new_message->message_type.type);
Jake Oshins4daace02016-02-16 21:56:23 +00002502 break;
2503 }
2504 break;
2505
2506 default:
2507 dev_err(&hbus->hdev->device,
2508 "unhandled packet type %d, tid %llx len %d\n",
2509 desc->type, req_id, bytes_recvd);
2510 break;
2511 }
Jake Oshins4daace02016-02-16 21:56:23 +00002512 }
Vitaly Kuznetsov60fcdac2016-05-30 16:17:58 +02002513
2514 kfree(buffer);
Jake Oshins4daace02016-02-16 21:56:23 +00002515}
2516
2517/**
2518 * hv_pci_protocol_negotiation() - Set up protocol
Krzysztof Wilczyński6d2730c2020-09-25 23:47:53 +00002519 * @hdev: VMBus's tracking struct for this root PCI bus.
2520 * @version: Array of supported channel protocol versions in
2521 * the order of probing - highest go first.
2522 * @num_version: Number of elements in the version array.
Jake Oshins4daace02016-02-16 21:56:23 +00002523 *
2524 * This driver is intended to support running on Windows 10
2525 * (server) and later versions. It will not run on earlier
2526 * versions, as they assume that many of the operations which
2527 * Linux needs accomplished with a spinlock held were done via
2528 * asynchronous messaging via VMBus. Windows 10 increases the
2529 * surface area of PCI emulation so that these actions can take
2530 * place by suspending a virtual processor for their duration.
2531 *
2532 * This function negotiates the channel protocol version,
2533 * failing if the host doesn't support the necessary protocol
2534 * level.
2535 */
Dexuan Cuia8e37502019-11-24 21:33:51 -08002536static int hv_pci_protocol_negotiation(struct hv_device *hdev,
2537 enum pci_protocol_version_t version[],
2538 int num_version)
Jake Oshins4daace02016-02-16 21:56:23 +00002539{
Dexuan Cui14ef39f2019-11-24 21:33:53 -08002540 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
Jake Oshins4daace02016-02-16 21:56:23 +00002541 struct pci_version_request *version_req;
2542 struct hv_pci_compl comp_pkt;
2543 struct pci_packet *pkt;
2544 int ret;
Jork Loeserb1db7e72017-05-24 13:41:27 -07002545 int i;
Jake Oshins4daace02016-02-16 21:56:23 +00002546
2547 /*
2548 * Initiate the handshake with the host and negotiate
2549 * a version that the host can support. We start with the
2550 * highest version number and go down if the host cannot
2551 * support it.
2552 */
2553 pkt = kzalloc(sizeof(*pkt) + sizeof(*version_req), GFP_KERNEL);
2554 if (!pkt)
2555 return -ENOMEM;
2556
2557 init_completion(&comp_pkt.host_event);
2558 pkt->completion_func = hv_pci_generic_compl;
2559 pkt->compl_ctxt = &comp_pkt;
2560 version_req = (struct pci_version_request *)&pkt->message;
Dexuan Cui0c6045d2016-08-23 04:45:51 +00002561 version_req->message_type.type = PCI_QUERY_PROTOCOL_VERSION;
Jake Oshins4daace02016-02-16 21:56:23 +00002562
Dexuan Cuia8e37502019-11-24 21:33:51 -08002563 for (i = 0; i < num_version; i++) {
2564 version_req->protocol_version = version[i];
Jork Loeserb1db7e72017-05-24 13:41:27 -07002565 ret = vmbus_sendpacket(hdev->channel, version_req,
2566 sizeof(struct pci_version_request),
2567 (unsigned long)pkt, VM_PKT_DATA_INBAND,
2568 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
Dexuan Cuic3635da2018-05-23 21:12:01 +00002569 if (!ret)
2570 ret = wait_for_response(hdev, &comp_pkt.host_event);
2571
Jork Loeserb1db7e72017-05-24 13:41:27 -07002572 if (ret) {
2573 dev_err(&hdev->device,
Dexuan Cuic3635da2018-05-23 21:12:01 +00002574 "PCI Pass-through VSP failed to request version: %d",
Jork Loeserb1db7e72017-05-24 13:41:27 -07002575 ret);
2576 goto exit;
2577 }
Jake Oshins4daace02016-02-16 21:56:23 +00002578
Jork Loeserb1db7e72017-05-24 13:41:27 -07002579 if (comp_pkt.completion_status >= 0) {
Dexuan Cui14ef39f2019-11-24 21:33:53 -08002580 hbus->protocol_version = version[i];
Jork Loeserb1db7e72017-05-24 13:41:27 -07002581 dev_info(&hdev->device,
2582 "PCI VMBus probing: Using version %#x\n",
Dexuan Cui14ef39f2019-11-24 21:33:53 -08002583 hbus->protocol_version);
Jork Loeserb1db7e72017-05-24 13:41:27 -07002584 goto exit;
2585 }
2586
2587 if (comp_pkt.completion_status != STATUS_REVISION_MISMATCH) {
2588 dev_err(&hdev->device,
2589 "PCI Pass-through VSP failed version request: %#x",
2590 comp_pkt.completion_status);
2591 ret = -EPROTO;
2592 goto exit;
2593 }
2594
2595 reinit_completion(&comp_pkt.host_event);
Jake Oshins4daace02016-02-16 21:56:23 +00002596 }
2597
Jork Loeserb1db7e72017-05-24 13:41:27 -07002598 dev_err(&hdev->device,
2599 "PCI pass-through VSP failed to find supported version");
2600 ret = -EPROTO;
Jake Oshins4daace02016-02-16 21:56:23 +00002601
2602exit:
2603 kfree(pkt);
2604 return ret;
2605}
2606
2607/**
2608 * hv_pci_free_bridge_windows() - Release memory regions for the
2609 * bus
2610 * @hbus: Root PCI bus, as understood by this driver
2611 */
2612static void hv_pci_free_bridge_windows(struct hv_pcibus_device *hbus)
2613{
2614 /*
2615 * Set the resources back to the way they looked when they
2616 * were allocated by setting IORESOURCE_BUSY again.
2617 */
2618
2619 if (hbus->low_mmio_space && hbus->low_mmio_res) {
2620 hbus->low_mmio_res->flags |= IORESOURCE_BUSY;
Jake Oshins696ca5e2016-04-05 10:22:52 -07002621 vmbus_free_mmio(hbus->low_mmio_res->start,
2622 resource_size(hbus->low_mmio_res));
Jake Oshins4daace02016-02-16 21:56:23 +00002623 }
2624
2625 if (hbus->high_mmio_space && hbus->high_mmio_res) {
2626 hbus->high_mmio_res->flags |= IORESOURCE_BUSY;
Jake Oshins696ca5e2016-04-05 10:22:52 -07002627 vmbus_free_mmio(hbus->high_mmio_res->start,
2628 resource_size(hbus->high_mmio_res));
Jake Oshins4daace02016-02-16 21:56:23 +00002629 }
2630}
2631
2632/**
2633 * hv_pci_allocate_bridge_windows() - Allocate memory regions
2634 * for the bus
2635 * @hbus: Root PCI bus, as understood by this driver
2636 *
2637 * This function calls vmbus_allocate_mmio(), which is itself a
2638 * bit of a compromise. Ideally, we might change the pnp layer
2639 * in the kernel such that it comprehends either PCI devices
2640 * which are "grandchildren of ACPI," with some intermediate bus
2641 * node (in this case, VMBus) or change it such that it
2642 * understands VMBus. The pnp layer, however, has been declared
2643 * deprecated, and not subject to change.
2644 *
2645 * The workaround, implemented here, is to ask VMBus to allocate
2646 * MMIO space for this bus. VMBus itself knows which ranges are
2647 * appropriate by looking at its own ACPI objects. Then, after
2648 * these ranges are claimed, they're modified to look like they
2649 * would have looked if the ACPI and pnp code had allocated
2650 * bridge windows. These descriptors have to exist in this form
2651 * in order to satisfy the code which will get invoked when the
2652 * endpoint PCI function driver calls request_mem_region() or
2653 * request_mem_region_exclusive().
2654 *
2655 * Return: 0 on success, -errno on failure
2656 */
2657static int hv_pci_allocate_bridge_windows(struct hv_pcibus_device *hbus)
2658{
2659 resource_size_t align;
2660 int ret;
2661
2662 if (hbus->low_mmio_space) {
2663 align = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space));
2664 ret = vmbus_allocate_mmio(&hbus->low_mmio_res, hbus->hdev, 0,
2665 (u64)(u32)0xffffffff,
2666 hbus->low_mmio_space,
2667 align, false);
2668 if (ret) {
2669 dev_err(&hbus->hdev->device,
2670 "Need %#llx of low MMIO space. Consider reconfiguring the VM.\n",
2671 hbus->low_mmio_space);
2672 return ret;
2673 }
2674
2675 /* Modify this resource to become a bridge window. */
2676 hbus->low_mmio_res->flags |= IORESOURCE_WINDOW;
2677 hbus->low_mmio_res->flags &= ~IORESOURCE_BUSY;
2678 pci_add_resource(&hbus->resources_for_children,
2679 hbus->low_mmio_res);
2680 }
2681
2682 if (hbus->high_mmio_space) {
2683 align = 1ULL << (63 - __builtin_clzll(hbus->high_mmio_space));
2684 ret = vmbus_allocate_mmio(&hbus->high_mmio_res, hbus->hdev,
2685 0x100000000, -1,
2686 hbus->high_mmio_space, align,
2687 false);
2688 if (ret) {
2689 dev_err(&hbus->hdev->device,
2690 "Need %#llx of high MMIO space. Consider reconfiguring the VM.\n",
2691 hbus->high_mmio_space);
2692 goto release_low_mmio;
2693 }
2694
2695 /* Modify this resource to become a bridge window. */
2696 hbus->high_mmio_res->flags |= IORESOURCE_WINDOW;
2697 hbus->high_mmio_res->flags &= ~IORESOURCE_BUSY;
2698 pci_add_resource(&hbus->resources_for_children,
2699 hbus->high_mmio_res);
2700 }
2701
2702 return 0;
2703
2704release_low_mmio:
2705 if (hbus->low_mmio_res) {
Jake Oshins696ca5e2016-04-05 10:22:52 -07002706 vmbus_free_mmio(hbus->low_mmio_res->start,
2707 resource_size(hbus->low_mmio_res));
Jake Oshins4daace02016-02-16 21:56:23 +00002708 }
2709
2710 return ret;
2711}
2712
2713/**
2714 * hv_allocate_config_window() - Find MMIO space for PCI Config
2715 * @hbus: Root PCI bus, as understood by this driver
2716 *
2717 * This function claims memory-mapped I/O space for accessing
2718 * configuration space for the functions on this bus.
2719 *
2720 * Return: 0 on success, -errno on failure
2721 */
2722static int hv_allocate_config_window(struct hv_pcibus_device *hbus)
2723{
2724 int ret;
2725
2726 /*
2727 * Set up a region of MMIO space to use for accessing configuration
2728 * space.
2729 */
2730 ret = vmbus_allocate_mmio(&hbus->mem_config, hbus->hdev, 0, -1,
2731 PCI_CONFIG_MMIO_LENGTH, 0x1000, false);
2732 if (ret)
2733 return ret;
2734
2735 /*
2736 * vmbus_allocate_mmio() gets used for allocating both device endpoint
2737 * resource claims (those which cannot be overlapped) and the ranges
2738 * which are valid for the children of this bus, which are intended
2739 * to be overlapped by those children. Set the flag on this claim
2740 * meaning that this region can't be overlapped.
2741 */
2742
2743 hbus->mem_config->flags |= IORESOURCE_BUSY;
2744
2745 return 0;
2746}
2747
2748static void hv_free_config_window(struct hv_pcibus_device *hbus)
2749{
Jake Oshins696ca5e2016-04-05 10:22:52 -07002750 vmbus_free_mmio(hbus->mem_config->start, PCI_CONFIG_MMIO_LENGTH);
Jake Oshins4daace02016-02-16 21:56:23 +00002751}
2752
Wei Huc81992e2020-05-07 13:03:00 +08002753static int hv_pci_bus_exit(struct hv_device *hdev, bool keep_devs);
2754
Jake Oshins4daace02016-02-16 21:56:23 +00002755/**
2756 * hv_pci_enter_d0() - Bring the "bus" into the D0 power state
2757 * @hdev: VMBus's tracking struct for this root PCI bus
2758 *
2759 * Return: 0 on success, -errno on failure
2760 */
2761static int hv_pci_enter_d0(struct hv_device *hdev)
2762{
2763 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
2764 struct pci_bus_d0_entry *d0_entry;
2765 struct hv_pci_compl comp_pkt;
2766 struct pci_packet *pkt;
2767 int ret;
2768
2769 /*
2770 * Tell the host that the bus is ready to use, and moved into the
2771 * powered-on state. This includes telling the host which region
2772 * of memory-mapped I/O space has been chosen for configuration space
2773 * access.
2774 */
2775 pkt = kzalloc(sizeof(*pkt) + sizeof(*d0_entry), GFP_KERNEL);
2776 if (!pkt)
2777 return -ENOMEM;
2778
2779 init_completion(&comp_pkt.host_event);
2780 pkt->completion_func = hv_pci_generic_compl;
2781 pkt->compl_ctxt = &comp_pkt;
2782 d0_entry = (struct pci_bus_d0_entry *)&pkt->message;
Dexuan Cui0c6045d2016-08-23 04:45:51 +00002783 d0_entry->message_type.type = PCI_BUS_D0ENTRY;
Jake Oshins4daace02016-02-16 21:56:23 +00002784 d0_entry->mmio_base = hbus->mem_config->start;
2785
2786 ret = vmbus_sendpacket(hdev->channel, d0_entry, sizeof(*d0_entry),
2787 (unsigned long)pkt, VM_PKT_DATA_INBAND,
2788 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
Dexuan Cuic3635da2018-05-23 21:12:01 +00002789 if (!ret)
2790 ret = wait_for_response(hdev, &comp_pkt.host_event);
2791
Jake Oshins4daace02016-02-16 21:56:23 +00002792 if (ret)
2793 goto exit;
2794
Jake Oshins4daace02016-02-16 21:56:23 +00002795 if (comp_pkt.completion_status < 0) {
2796 dev_err(&hdev->device,
2797 "PCI Pass-through VSP failed D0 Entry with status %x\n",
2798 comp_pkt.completion_status);
2799 ret = -EPROTO;
2800 goto exit;
2801 }
2802
2803 ret = 0;
2804
2805exit:
2806 kfree(pkt);
2807 return ret;
2808}
2809
2810/**
2811 * hv_pci_query_relations() - Ask host to send list of child
2812 * devices
2813 * @hdev: VMBus's tracking struct for this root PCI bus
2814 *
2815 * Return: 0 on success, -errno on failure
2816 */
2817static int hv_pci_query_relations(struct hv_device *hdev)
2818{
2819 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
2820 struct pci_message message;
2821 struct completion comp;
2822 int ret;
2823
2824 /* Ask the host to send along the list of child devices */
2825 init_completion(&comp);
2826 if (cmpxchg(&hbus->survey_event, NULL, &comp))
2827 return -ENOTEMPTY;
2828
2829 memset(&message, 0, sizeof(message));
Dexuan Cui0c6045d2016-08-23 04:45:51 +00002830 message.type = PCI_QUERY_BUS_RELATIONS;
Jake Oshins4daace02016-02-16 21:56:23 +00002831
2832 ret = vmbus_sendpacket(hdev->channel, &message, sizeof(message),
2833 0, VM_PKT_DATA_INBAND, 0);
Dexuan Cuic3635da2018-05-23 21:12:01 +00002834 if (!ret)
2835 ret = wait_for_response(hdev, &comp);
Jake Oshins4daace02016-02-16 21:56:23 +00002836
Dexuan Cuic3635da2018-05-23 21:12:01 +00002837 return ret;
Jake Oshins4daace02016-02-16 21:56:23 +00002838}
2839
2840/**
2841 * hv_send_resources_allocated() - Report local resource choices
2842 * @hdev: VMBus's tracking struct for this root PCI bus
2843 *
2844 * The host OS is expecting to be sent a request as a message
2845 * which contains all the resources that the device will use.
2846 * The response contains those same resources, "translated"
2847 * which is to say, the values which should be used by the
2848 * hardware, when it delivers an interrupt. (MMIO resources are
2849 * used in local terms.) This is nice for Windows, and lines up
2850 * with the FDO/PDO split, which doesn't exist in Linux. Linux
2851 * is deeply expecting to scan an emulated PCI configuration
2852 * space. So this message is sent here only to drive the state
2853 * machine on the host forward.
2854 *
2855 * Return: 0 on success, -errno on failure
2856 */
2857static int hv_send_resources_allocated(struct hv_device *hdev)
2858{
2859 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
2860 struct pci_resources_assigned *res_assigned;
Jork Loeser7dcf90e2017-05-24 13:41:28 -07002861 struct pci_resources_assigned2 *res_assigned2;
Jake Oshins4daace02016-02-16 21:56:23 +00002862 struct hv_pci_compl comp_pkt;
2863 struct hv_pci_dev *hpdev;
2864 struct pci_packet *pkt;
Jork Loeser7dcf90e2017-05-24 13:41:28 -07002865 size_t size_res;
Wei Hu83cc3502020-05-07 13:02:11 +08002866 int wslot;
Jake Oshins4daace02016-02-16 21:56:23 +00002867 int ret;
2868
Dexuan Cui14ef39f2019-11-24 21:33:53 -08002869 size_res = (hbus->protocol_version < PCI_PROTOCOL_VERSION_1_2)
Jork Loeser7dcf90e2017-05-24 13:41:28 -07002870 ? sizeof(*res_assigned) : sizeof(*res_assigned2);
2871
2872 pkt = kmalloc(sizeof(*pkt) + size_res, GFP_KERNEL);
Jake Oshins4daace02016-02-16 21:56:23 +00002873 if (!pkt)
2874 return -ENOMEM;
2875
2876 ret = 0;
2877
2878 for (wslot = 0; wslot < 256; wslot++) {
2879 hpdev = get_pcichild_wslot(hbus, wslot);
2880 if (!hpdev)
2881 continue;
2882
Jork Loeser7dcf90e2017-05-24 13:41:28 -07002883 memset(pkt, 0, sizeof(*pkt) + size_res);
Jake Oshins4daace02016-02-16 21:56:23 +00002884 init_completion(&comp_pkt.host_event);
2885 pkt->completion_func = hv_pci_generic_compl;
2886 pkt->compl_ctxt = &comp_pkt;
Jake Oshins4daace02016-02-16 21:56:23 +00002887
Dexuan Cui14ef39f2019-11-24 21:33:53 -08002888 if (hbus->protocol_version < PCI_PROTOCOL_VERSION_1_2) {
Jork Loeser7dcf90e2017-05-24 13:41:28 -07002889 res_assigned =
2890 (struct pci_resources_assigned *)&pkt->message;
2891 res_assigned->message_type.type =
2892 PCI_RESOURCES_ASSIGNED;
2893 res_assigned->wslot.slot = hpdev->desc.win_slot.slot;
2894 } else {
2895 res_assigned2 =
2896 (struct pci_resources_assigned2 *)&pkt->message;
2897 res_assigned2->message_type.type =
2898 PCI_RESOURCES_ASSIGNED2;
2899 res_assigned2->wslot.slot = hpdev->desc.win_slot.slot;
2900 }
Stephen Hemminger8c99e122018-05-23 10:11:12 -07002901 put_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +00002902
Jork Loeser7dcf90e2017-05-24 13:41:28 -07002903 ret = vmbus_sendpacket(hdev->channel, &pkt->message,
2904 size_res, (unsigned long)pkt,
2905 VM_PKT_DATA_INBAND,
2906 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
Dexuan Cuic3635da2018-05-23 21:12:01 +00002907 if (!ret)
2908 ret = wait_for_response(hdev, &comp_pkt.host_event);
Jake Oshins4daace02016-02-16 21:56:23 +00002909 if (ret)
2910 break;
2911
Jake Oshins4daace02016-02-16 21:56:23 +00002912 if (comp_pkt.completion_status < 0) {
2913 ret = -EPROTO;
2914 dev_err(&hdev->device,
2915 "resource allocated returned 0x%x",
2916 comp_pkt.completion_status);
2917 break;
2918 }
Wei Hu83cc3502020-05-07 13:02:11 +08002919
2920 hbus->wslot_res_allocated = wslot;
Jake Oshins4daace02016-02-16 21:56:23 +00002921 }
2922
2923 kfree(pkt);
2924 return ret;
2925}
2926
2927/**
2928 * hv_send_resources_released() - Report local resources
2929 * released
2930 * @hdev: VMBus's tracking struct for this root PCI bus
2931 *
2932 * Return: 0 on success, -errno on failure
2933 */
2934static int hv_send_resources_released(struct hv_device *hdev)
2935{
2936 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
2937 struct pci_child_message pkt;
2938 struct hv_pci_dev *hpdev;
Wei Hu83cc3502020-05-07 13:02:11 +08002939 int wslot;
Jake Oshins4daace02016-02-16 21:56:23 +00002940 int ret;
2941
Wei Hu83cc3502020-05-07 13:02:11 +08002942 for (wslot = hbus->wslot_res_allocated; wslot >= 0; wslot--) {
Jake Oshins4daace02016-02-16 21:56:23 +00002943 hpdev = get_pcichild_wslot(hbus, wslot);
2944 if (!hpdev)
2945 continue;
2946
2947 memset(&pkt, 0, sizeof(pkt));
Dexuan Cui0c6045d2016-08-23 04:45:51 +00002948 pkt.message_type.type = PCI_RESOURCES_RELEASED;
Jake Oshins4daace02016-02-16 21:56:23 +00002949 pkt.wslot.slot = hpdev->desc.win_slot.slot;
2950
Stephen Hemminger8c99e122018-05-23 10:11:12 -07002951 put_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +00002952
2953 ret = vmbus_sendpacket(hdev->channel, &pkt, sizeof(pkt), 0,
2954 VM_PKT_DATA_INBAND, 0);
2955 if (ret)
2956 return ret;
Wei Hu83cc3502020-05-07 13:02:11 +08002957
2958 hbus->wslot_res_allocated = wslot - 1;
Jake Oshins4daace02016-02-16 21:56:23 +00002959 }
2960
Wei Hu83cc3502020-05-07 13:02:11 +08002961 hbus->wslot_res_allocated = -1;
2962
Jake Oshins4daace02016-02-16 21:56:23 +00002963 return 0;
2964}
2965
2966static void get_hvpcibus(struct hv_pcibus_device *hbus)
2967{
Stephen Hemminger6708be92018-05-23 10:11:13 -07002968 refcount_inc(&hbus->remove_lock);
Jake Oshins4daace02016-02-16 21:56:23 +00002969}
2970
2971static void put_hvpcibus(struct hv_pcibus_device *hbus)
2972{
Stephen Hemminger6708be92018-05-23 10:11:13 -07002973 if (refcount_dec_and_test(&hbus->remove_lock))
Jake Oshins4daace02016-02-16 21:56:23 +00002974 complete(&hbus->remove_event);
2975}
2976
Haiyang Zhangbe700102019-08-15 17:01:37 +00002977#define HVPCI_DOM_MAP_SIZE (64 * 1024)
2978static DECLARE_BITMAP(hvpci_dom_map, HVPCI_DOM_MAP_SIZE);
2979
2980/*
2981 * PCI domain number 0 is used by emulated devices on Gen1 VMs, so define 0
2982 * as invalid for passthrough PCI devices of this driver.
2983 */
2984#define HVPCI_DOM_INVALID 0
2985
2986/**
2987 * hv_get_dom_num() - Get a valid PCI domain number
2988 * Check if the PCI domain number is in use, and return another number if
2989 * it is in use.
2990 *
2991 * @dom: Requested domain number
2992 *
2993 * return: domain number on success, HVPCI_DOM_INVALID on failure
2994 */
2995static u16 hv_get_dom_num(u16 dom)
2996{
2997 unsigned int i;
2998
2999 if (test_and_set_bit(dom, hvpci_dom_map) == 0)
3000 return dom;
3001
3002 for_each_clear_bit(i, hvpci_dom_map, HVPCI_DOM_MAP_SIZE) {
3003 if (test_and_set_bit(i, hvpci_dom_map) == 0)
3004 return i;
3005 }
3006
3007 return HVPCI_DOM_INVALID;
3008}
3009
3010/**
3011 * hv_put_dom_num() - Mark the PCI domain number as free
3012 * @dom: Domain number to be freed
3013 */
3014static void hv_put_dom_num(u16 dom)
3015{
3016 clear_bit(dom, hvpci_dom_map);
3017}
3018
Jake Oshins4daace02016-02-16 21:56:23 +00003019/**
3020 * hv_pci_probe() - New VMBus channel probe, for a root PCI bus
3021 * @hdev: VMBus's tracking struct for this root PCI bus
3022 * @dev_id: Identifies the device itself
3023 *
3024 * Return: 0 on success, -errno on failure
3025 */
3026static int hv_pci_probe(struct hv_device *hdev,
3027 const struct hv_vmbus_device_id *dev_id)
3028{
3029 struct hv_pcibus_device *hbus;
Haiyang Zhangbe700102019-08-15 17:01:37 +00003030 u16 dom_req, dom;
Marc Zyngier467a3bb2019-08-06 15:23:33 +01003031 char *name;
Wei Hud6af2ed2020-07-27 15:17:31 +08003032 bool enter_d0_retry = true;
Jake Oshins4daace02016-02-16 21:56:23 +00003033 int ret;
3034
Jork Loeserbe66b672017-05-24 13:41:25 -07003035 /*
3036 * hv_pcibus_device contains the hypercall arguments for retargeting in
3037 * hv_irq_unmask(). Those must not cross a page boundary.
3038 */
Dexuan Cui877b9112019-11-24 21:33:54 -08003039 BUILD_BUG_ON(sizeof(*hbus) > HV_HYP_PAGE_SIZE);
Jork Loeserbe66b672017-05-24 13:41:25 -07003040
Dexuan Cui877b9112019-11-24 21:33:54 -08003041 /*
3042 * With the recent 59bb47985c1d ("mm, sl[aou]b: guarantee natural
3043 * alignment for kmalloc(power-of-two)"), kzalloc() is able to allocate
3044 * a 4KB buffer that is guaranteed to be 4KB-aligned. Here the size and
3045 * alignment of hbus is important because hbus's field
3046 * retarget_msi_interrupt_params must not cross a 4KB page boundary.
3047 *
3048 * Here we prefer kzalloc to get_zeroed_page(), because a buffer
3049 * allocated by the latter is not tracked and scanned by kmemleak, and
3050 * hence kmemleak reports the pointer contained in the hbus buffer
3051 * (i.e. the hpdev struct, which is created in new_pcichild_device() and
3052 * is tracked by hbus->children) as memory leak (false positive).
3053 *
3054 * If the kernel doesn't have 59bb47985c1d, get_zeroed_page() *must* be
3055 * used to allocate the hbus buffer and we can avoid the kmemleak false
3056 * positive by using kmemleak_alloc() and kmemleak_free() to ask
3057 * kmemleak to track and scan the hbus buffer.
3058 */
Dexuan Cuie658a4f2020-02-21 21:59:56 -08003059 hbus = kzalloc(HV_HYP_PAGE_SIZE, GFP_KERNEL);
Jake Oshins4daace02016-02-16 21:56:23 +00003060 if (!hbus)
3061 return -ENOMEM;
Long Lid3a78d82017-03-23 14:58:10 -07003062 hbus->state = hv_pcibus_init;
Wei Hu83cc3502020-05-07 13:02:11 +08003063 hbus->wslot_res_allocated = -1;
Jake Oshins4daace02016-02-16 21:56:23 +00003064
3065 /*
Haiyang Zhangbe700102019-08-15 17:01:37 +00003066 * The PCI bus "domain" is what is called "segment" in ACPI and other
3067 * specs. Pull it from the instance ID, to get something usually
3068 * unique. In rare cases of collision, we will find out another number
3069 * not in use.
3070 *
3071 * Note that, since this code only runs in a Hyper-V VM, Hyper-V
3072 * together with this guest driver can guarantee that (1) The only
3073 * domain used by Gen1 VMs for something that looks like a physical
3074 * PCI bus (which is actually emulated by the hypervisor) is domain 0.
3075 * (2) There will be no overlap between domains (after fixing possible
3076 * collisions) in the same VM.
Jake Oshins4daace02016-02-16 21:56:23 +00003077 */
Haiyang Zhangf73f8a52019-08-15 17:01:45 +00003078 dom_req = hdev->dev_instance.b[5] << 8 | hdev->dev_instance.b[4];
Haiyang Zhangbe700102019-08-15 17:01:37 +00003079 dom = hv_get_dom_num(dom_req);
3080
3081 if (dom == HVPCI_DOM_INVALID) {
3082 dev_err(&hdev->device,
3083 "Unable to use dom# 0x%hx or other numbers", dom_req);
3084 ret = -EINVAL;
3085 goto free_bus;
3086 }
3087
3088 if (dom != dom_req)
3089 dev_info(&hdev->device,
3090 "PCI dom# 0x%hx has collision, using 0x%hx",
3091 dom_req, dom);
3092
3093 hbus->sysdata.domain = dom;
Jake Oshins4daace02016-02-16 21:56:23 +00003094
3095 hbus->hdev = hdev;
Stephen Hemminger6708be92018-05-23 10:11:13 -07003096 refcount_set(&hbus->remove_lock, 1);
Jake Oshins4daace02016-02-16 21:56:23 +00003097 INIT_LIST_HEAD(&hbus->children);
3098 INIT_LIST_HEAD(&hbus->dr_list);
3099 INIT_LIST_HEAD(&hbus->resources_for_children);
3100 spin_lock_init(&hbus->config_lock);
3101 spin_lock_init(&hbus->device_list_lock);
Long Li0de8ce32016-11-08 14:04:38 -08003102 spin_lock_init(&hbus->retarget_msi_interrupt_lock);
Jake Oshins4daace02016-02-16 21:56:23 +00003103 init_completion(&hbus->remove_event);
Dexuan Cui021ad272018-03-15 14:20:53 +00003104 hbus->wq = alloc_ordered_workqueue("hv_pci_%x", 0,
3105 hbus->sysdata.domain);
3106 if (!hbus->wq) {
3107 ret = -ENOMEM;
Haiyang Zhangbe700102019-08-15 17:01:37 +00003108 goto free_dom;
Dexuan Cui021ad272018-03-15 14:20:53 +00003109 }
Jake Oshins4daace02016-02-16 21:56:23 +00003110
3111 ret = vmbus_open(hdev->channel, pci_ring_size, pci_ring_size, NULL, 0,
3112 hv_pci_onchannelcallback, hbus);
3113 if (ret)
Dexuan Cui021ad272018-03-15 14:20:53 +00003114 goto destroy_wq;
Jake Oshins4daace02016-02-16 21:56:23 +00003115
3116 hv_set_drvdata(hdev, hbus);
3117
Dexuan Cuia8e37502019-11-24 21:33:51 -08003118 ret = hv_pci_protocol_negotiation(hdev, pci_protocol_versions,
3119 ARRAY_SIZE(pci_protocol_versions));
Jake Oshins4daace02016-02-16 21:56:23 +00003120 if (ret)
3121 goto close;
3122
3123 ret = hv_allocate_config_window(hbus);
3124 if (ret)
3125 goto close;
3126
3127 hbus->cfg_addr = ioremap(hbus->mem_config->start,
3128 PCI_CONFIG_MMIO_LENGTH);
3129 if (!hbus->cfg_addr) {
3130 dev_err(&hdev->device,
3131 "Unable to map a virtual address for config space\n");
3132 ret = -ENOMEM;
3133 goto free_config;
3134 }
3135
Marc Zyngier467a3bb2019-08-06 15:23:33 +01003136 name = kasprintf(GFP_KERNEL, "%pUL", &hdev->dev_instance);
3137 if (!name) {
3138 ret = -ENOMEM;
3139 goto unmap;
3140 }
3141
3142 hbus->sysdata.fwnode = irq_domain_alloc_named_fwnode(name);
3143 kfree(name);
Jake Oshins4daace02016-02-16 21:56:23 +00003144 if (!hbus->sysdata.fwnode) {
3145 ret = -ENOMEM;
3146 goto unmap;
3147 }
3148
3149 ret = hv_pcie_init_irq_domain(hbus);
3150 if (ret)
3151 goto free_fwnode;
3152
Wei Hud6af2ed2020-07-27 15:17:31 +08003153retry:
Jake Oshins4daace02016-02-16 21:56:23 +00003154 ret = hv_pci_query_relations(hdev);
3155 if (ret)
3156 goto free_irq_domain;
3157
3158 ret = hv_pci_enter_d0(hdev);
Wei Hud6af2ed2020-07-27 15:17:31 +08003159 /*
3160 * In certain case (Kdump) the pci device of interest was
3161 * not cleanly shut down and resource is still held on host
3162 * side, the host could return invalid device status.
3163 * We need to explicitly request host to release the resource
3164 * and try to enter D0 again.
3165 * Since the hv_pci_bus_exit() call releases structures
3166 * of all its child devices, we need to start the retry from
3167 * hv_pci_query_relations() call, requesting host to send
3168 * the synchronous child device relations message before this
3169 * information is needed in hv_send_resources_allocated()
3170 * call later.
3171 */
3172 if (ret == -EPROTO && enter_d0_retry) {
3173 enter_d0_retry = false;
3174
3175 dev_err(&hdev->device, "Retrying D0 Entry\n");
3176
3177 /*
3178 * Hv_pci_bus_exit() calls hv_send_resources_released()
3179 * to free up resources of its child devices.
3180 * In the kdump kernel we need to set the
3181 * wslot_res_allocated to 255 so it scans all child
3182 * devices to release resources allocated in the
3183 * normal kernel before panic happened.
3184 */
3185 hbus->wslot_res_allocated = 255;
3186 ret = hv_pci_bus_exit(hdev, true);
3187
3188 if (ret == 0)
3189 goto retry;
3190
3191 dev_err(&hdev->device,
3192 "Retrying D0 failed with ret %d\n", ret);
3193 }
Jake Oshins4daace02016-02-16 21:56:23 +00003194 if (ret)
3195 goto free_irq_domain;
3196
3197 ret = hv_pci_allocate_bridge_windows(hbus);
3198 if (ret)
Wei Hu83cc3502020-05-07 13:02:11 +08003199 goto exit_d0;
Jake Oshins4daace02016-02-16 21:56:23 +00003200
3201 ret = hv_send_resources_allocated(hdev);
3202 if (ret)
3203 goto free_windows;
3204
3205 prepopulate_bars(hbus);
3206
3207 hbus->state = hv_pcibus_probed;
3208
3209 ret = create_root_hv_pci_bus(hbus);
3210 if (ret)
3211 goto free_windows;
3212
3213 return 0;
3214
3215free_windows:
3216 hv_pci_free_bridge_windows(hbus);
Wei Hu83cc3502020-05-07 13:02:11 +08003217exit_d0:
3218 (void) hv_pci_bus_exit(hdev, true);
Jake Oshins4daace02016-02-16 21:56:23 +00003219free_irq_domain:
3220 irq_domain_remove(hbus->irq_domain);
3221free_fwnode:
3222 irq_domain_free_fwnode(hbus->sysdata.fwnode);
3223unmap:
3224 iounmap(hbus->cfg_addr);
3225free_config:
3226 hv_free_config_window(hbus);
3227close:
3228 vmbus_close(hdev->channel);
Dexuan Cui021ad272018-03-15 14:20:53 +00003229destroy_wq:
3230 destroy_workqueue(hbus->wq);
Haiyang Zhangbe700102019-08-15 17:01:37 +00003231free_dom:
3232 hv_put_dom_num(hbus->sysdata.domain);
Jake Oshins4daace02016-02-16 21:56:23 +00003233free_bus:
Dexuan Cui42c3d412020-02-21 21:59:57 -08003234 kfree(hbus);
Jake Oshins4daace02016-02-16 21:56:23 +00003235 return ret;
3236}
3237
Wei Huc81992e2020-05-07 13:03:00 +08003238static int hv_pci_bus_exit(struct hv_device *hdev, bool keep_devs)
Jake Oshins4daace02016-02-16 21:56:23 +00003239{
Dexuan Cui179785242016-11-10 07:18:47 +00003240 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
3241 struct {
Jake Oshins4daace02016-02-16 21:56:23 +00003242 struct pci_packet teardown_packet;
Dexuan Cui179785242016-11-10 07:18:47 +00003243 u8 buffer[sizeof(struct pci_message)];
Jake Oshins4daace02016-02-16 21:56:23 +00003244 } pkt;
Jake Oshins4daace02016-02-16 21:56:23 +00003245 struct hv_pci_compl comp_pkt;
Long Li94d22762021-05-12 01:06:40 -07003246 struct hv_pci_dev *hpdev, *tmp;
3247 unsigned long flags;
Dexuan Cui179785242016-11-10 07:18:47 +00003248 int ret;
Jake Oshins4daace02016-02-16 21:56:23 +00003249
Dexuan Cui179785242016-11-10 07:18:47 +00003250 /*
3251 * After the host sends the RESCIND_CHANNEL message, it doesn't
3252 * access the per-channel ringbuffer any longer.
3253 */
3254 if (hdev->channel->rescind)
Dexuan Cuia8e37502019-11-24 21:33:51 -08003255 return 0;
Dexuan Cui179785242016-11-10 07:18:47 +00003256
Wei Huc81992e2020-05-07 13:03:00 +08003257 if (!keep_devs) {
Dexuan Cuia8e37502019-11-24 21:33:51 -08003258 /* Delete any children which might still exist. */
Long Li94d22762021-05-12 01:06:40 -07003259 spin_lock_irqsave(&hbus->device_list_lock, flags);
3260 list_for_each_entry_safe(hpdev, tmp, &hbus->children, list_entry) {
3261 list_del(&hpdev->list_entry);
3262 if (hpdev->pci_slot)
3263 pci_destroy_slot(hpdev->pci_slot);
3264 /* For the two refs got in new_pcichild_device() */
3265 put_pcichild(hpdev);
3266 put_pcichild(hpdev);
3267 }
3268 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
Dexuan Cuia8e37502019-11-24 21:33:51 -08003269 }
Dexuan Cui179785242016-11-10 07:18:47 +00003270
3271 ret = hv_send_resources_released(hdev);
Dexuan Cuia8e37502019-11-24 21:33:51 -08003272 if (ret) {
Dexuan Cui179785242016-11-10 07:18:47 +00003273 dev_err(&hdev->device,
3274 "Couldn't send resources released packet(s)\n");
Dexuan Cuia8e37502019-11-24 21:33:51 -08003275 return ret;
3276 }
Jake Oshins4daace02016-02-16 21:56:23 +00003277
Jake Oshins4daace02016-02-16 21:56:23 +00003278 memset(&pkt.teardown_packet, 0, sizeof(pkt.teardown_packet));
3279 init_completion(&comp_pkt.host_event);
3280 pkt.teardown_packet.completion_func = hv_pci_generic_compl;
3281 pkt.teardown_packet.compl_ctxt = &comp_pkt;
Dexuan Cui0c6045d2016-08-23 04:45:51 +00003282 pkt.teardown_packet.message[0].type = PCI_BUS_D0EXIT;
Jake Oshins4daace02016-02-16 21:56:23 +00003283
3284 ret = vmbus_sendpacket(hdev->channel, &pkt.teardown_packet.message,
3285 sizeof(struct pci_message),
3286 (unsigned long)&pkt.teardown_packet,
3287 VM_PKT_DATA_INBAND,
3288 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
Dexuan Cuia8e37502019-11-24 21:33:51 -08003289 if (ret)
3290 return ret;
3291
3292 if (wait_for_completion_timeout(&comp_pkt.host_event, 10 * HZ) == 0)
3293 return -ETIMEDOUT;
3294
3295 return 0;
Dexuan Cui179785242016-11-10 07:18:47 +00003296}
Jake Oshins4daace02016-02-16 21:56:23 +00003297
Dexuan Cui179785242016-11-10 07:18:47 +00003298/**
3299 * hv_pci_remove() - Remove routine for this VMBus channel
3300 * @hdev: VMBus's tracking struct for this root PCI bus
3301 *
3302 * Return: 0 on success, -errno on failure
3303 */
3304static int hv_pci_remove(struct hv_device *hdev)
3305{
3306 struct hv_pcibus_device *hbus;
Dexuan Cuia8e37502019-11-24 21:33:51 -08003307 int ret;
Dexuan Cui179785242016-11-10 07:18:47 +00003308
3309 hbus = hv_get_drvdata(hdev);
Jake Oshins4daace02016-02-16 21:56:23 +00003310 if (hbus->state == hv_pcibus_installed) {
Long Li94d22762021-05-12 01:06:40 -07003311 tasklet_disable(&hdev->channel->callback_event);
3312 hbus->state = hv_pcibus_removing;
3313 tasklet_enable(&hdev->channel->callback_event);
3314 destroy_workqueue(hbus->wq);
3315 hbus->wq = NULL;
3316 /*
3317 * At this point, no work is running or can be scheduled
3318 * on hbus-wq. We can't race with hv_pci_devices_present()
3319 * or hv_pci_eject_device(), it's safe to proceed.
3320 */
3321
Jake Oshins4daace02016-02-16 21:56:23 +00003322 /* Remove the bus from PCI's point of view. */
3323 pci_lock_rescan_remove();
3324 pci_stop_root_bus(hbus->pci_bus);
Dexuan Cui15becc22019-03-04 21:34:48 +00003325 hv_pci_remove_slots(hbus);
Dexuan Cui533ca1f2019-08-02 22:50:20 +00003326 pci_remove_root_bus(hbus->pci_bus);
Jake Oshins4daace02016-02-16 21:56:23 +00003327 pci_unlock_rescan_remove();
3328 }
3329
Dexuan Cuia8e37502019-11-24 21:33:51 -08003330 ret = hv_pci_bus_exit(hdev, false);
Vitaly Kuznetsovdeb22e52016-04-29 11:39:10 +02003331
Jake Oshins4daace02016-02-16 21:56:23 +00003332 vmbus_close(hdev->channel);
3333
Jake Oshins4daace02016-02-16 21:56:23 +00003334 iounmap(hbus->cfg_addr);
3335 hv_free_config_window(hbus);
3336 pci_free_resource_list(&hbus->resources_for_children);
3337 hv_pci_free_bridge_windows(hbus);
3338 irq_domain_remove(hbus->irq_domain);
3339 irq_domain_free_fwnode(hbus->sysdata.fwnode);
3340 put_hvpcibus(hbus);
3341 wait_for_completion(&hbus->remove_event);
Haiyang Zhangbe700102019-08-15 17:01:37 +00003342
3343 hv_put_dom_num(hbus->sysdata.domain);
3344
Dexuan Cui877b9112019-11-24 21:33:54 -08003345 kfree(hbus);
Dexuan Cuia8e37502019-11-24 21:33:51 -08003346 return ret;
Jake Oshins4daace02016-02-16 21:56:23 +00003347}
3348
Dexuan Cuiac82fc82019-11-24 21:33:52 -08003349static int hv_pci_suspend(struct hv_device *hdev)
3350{
3351 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
3352 enum hv_pcibus_state old_state;
3353 int ret;
3354
3355 /*
3356 * hv_pci_suspend() must make sure there are no pending work items
3357 * before calling vmbus_close(), since it runs in a process context
3358 * as a callback in dpm_suspend(). When it starts to run, the channel
3359 * callback hv_pci_onchannelcallback(), which runs in a tasklet
3360 * context, can be still running concurrently and scheduling new work
3361 * items onto hbus->wq in hv_pci_devices_present() and
3362 * hv_pci_eject_device(), and the work item handlers can access the
3363 * vmbus channel, which can be being closed by hv_pci_suspend(), e.g.
3364 * the work item handler pci_devices_present_work() ->
3365 * new_pcichild_device() writes to the vmbus channel.
3366 *
3367 * To eliminate the race, hv_pci_suspend() disables the channel
3368 * callback tasklet, sets hbus->state to hv_pcibus_removing, and
3369 * re-enables the tasklet. This way, when hv_pci_suspend() proceeds,
3370 * it knows that no new work item can be scheduled, and then it flushes
3371 * hbus->wq and safely closes the vmbus channel.
3372 */
3373 tasklet_disable(&hdev->channel->callback_event);
3374
3375 /* Change the hbus state to prevent new work items. */
3376 old_state = hbus->state;
3377 if (hbus->state == hv_pcibus_installed)
3378 hbus->state = hv_pcibus_removing;
3379
3380 tasklet_enable(&hdev->channel->callback_event);
3381
3382 if (old_state != hv_pcibus_installed)
3383 return -EINVAL;
3384
3385 flush_workqueue(hbus->wq);
3386
3387 ret = hv_pci_bus_exit(hdev, true);
3388 if (ret)
3389 return ret;
3390
3391 vmbus_close(hdev->channel);
3392
Jake Oshins4daace02016-02-16 21:56:23 +00003393 return 0;
3394}
3395
Dexuan Cui915cff72020-10-02 01:51:58 -07003396static int hv_pci_restore_msi_msg(struct pci_dev *pdev, void *arg)
3397{
3398 struct msi_desc *entry;
3399 struct irq_data *irq_data;
3400
3401 for_each_pci_msi_entry(entry, pdev) {
3402 irq_data = irq_get_irq_data(entry->irq);
3403 if (WARN_ON_ONCE(!irq_data))
3404 return -EINVAL;
3405
3406 hv_compose_msi_msg(irq_data, &entry->msg);
3407 }
3408
3409 return 0;
3410}
3411
3412/*
3413 * Upon resume, pci_restore_msi_state() -> ... -> __pci_write_msi_msg()
3414 * directly writes the MSI/MSI-X registers via MMIO, but since Hyper-V
3415 * doesn't trap and emulate the MMIO accesses, here hv_compose_msi_msg()
3416 * must be used to ask Hyper-V to re-create the IOMMU Interrupt Remapping
3417 * Table entries.
3418 */
3419static void hv_pci_restore_msi_state(struct hv_pcibus_device *hbus)
3420{
3421 pci_walk_bus(hbus->pci_bus, hv_pci_restore_msi_msg, NULL);
3422}
3423
Dexuan Cuiac82fc82019-11-24 21:33:52 -08003424static int hv_pci_resume(struct hv_device *hdev)
3425{
3426 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
3427 enum pci_protocol_version_t version[1];
3428 int ret;
3429
3430 hbus->state = hv_pcibus_init;
3431
3432 ret = vmbus_open(hdev->channel, pci_ring_size, pci_ring_size, NULL, 0,
3433 hv_pci_onchannelcallback, hbus);
3434 if (ret)
3435 return ret;
3436
3437 /* Only use the version that was in use before hibernation. */
Dexuan Cui14ef39f2019-11-24 21:33:53 -08003438 version[0] = hbus->protocol_version;
Dexuan Cuiac82fc82019-11-24 21:33:52 -08003439 ret = hv_pci_protocol_negotiation(hdev, version, 1);
3440 if (ret)
3441 goto out;
3442
3443 ret = hv_pci_query_relations(hdev);
3444 if (ret)
3445 goto out;
3446
3447 ret = hv_pci_enter_d0(hdev);
3448 if (ret)
3449 goto out;
3450
3451 ret = hv_send_resources_allocated(hdev);
3452 if (ret)
3453 goto out;
3454
3455 prepopulate_bars(hbus);
3456
Dexuan Cui915cff72020-10-02 01:51:58 -07003457 hv_pci_restore_msi_state(hbus);
3458
Dexuan Cuiac82fc82019-11-24 21:33:52 -08003459 hbus->state = hv_pcibus_installed;
3460 return 0;
3461out:
3462 vmbus_close(hdev->channel);
3463 return ret;
3464}
3465
Jake Oshins4daace02016-02-16 21:56:23 +00003466static const struct hv_vmbus_device_id hv_pci_id_table[] = {
3467 /* PCI Pass-through Class ID */
3468 /* 44C4F61D-4444-4400-9D52-802E27EDE19F */
3469 { HV_PCIE_GUID, },
3470 { },
3471};
3472
3473MODULE_DEVICE_TABLE(vmbus, hv_pci_id_table);
3474
3475static struct hv_driver hv_pci_drv = {
3476 .name = "hv_pci",
3477 .id_table = hv_pci_id_table,
3478 .probe = hv_pci_probe,
3479 .remove = hv_pci_remove,
Dexuan Cuiac82fc82019-11-24 21:33:52 -08003480 .suspend = hv_pci_suspend,
3481 .resume = hv_pci_resume,
Jake Oshins4daace02016-02-16 21:56:23 +00003482};
3483
3484static void __exit exit_hv_pci_drv(void)
3485{
3486 vmbus_driver_unregister(&hv_pci_drv);
Haiyang Zhang348dd932019-08-22 05:05:41 +00003487
3488 hvpci_block_ops.read_block = NULL;
3489 hvpci_block_ops.write_block = NULL;
3490 hvpci_block_ops.reg_blk_invalidate = NULL;
Jake Oshins4daace02016-02-16 21:56:23 +00003491}
3492
3493static int __init init_hv_pci_drv(void)
3494{
Haiyang Zhangbe700102019-08-15 17:01:37 +00003495 /* Set the invalid domain number's bit, so it will not be used */
3496 set_bit(HVPCI_DOM_INVALID, hvpci_dom_map);
3497
Haiyang Zhang348dd932019-08-22 05:05:41 +00003498 /* Initialize PCI block r/w interface */
3499 hvpci_block_ops.read_block = hv_read_config_block;
3500 hvpci_block_ops.write_block = hv_write_config_block;
3501 hvpci_block_ops.reg_blk_invalidate = hv_register_block_invalidate;
3502
Jake Oshins4daace02016-02-16 21:56:23 +00003503 return vmbus_driver_register(&hv_pci_drv);
3504}
3505
3506module_init(init_hv_pci_drv);
3507module_exit(exit_hv_pci_drv);
3508
3509MODULE_DESCRIPTION("Hyper-V PCI");
3510MODULE_LICENSE("GPL v2");