xref: /linux/drivers/pci/controller/pci-hyperv.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
16e0832faSShawn Lin // SPDX-License-Identifier: GPL-2.0
26e0832faSShawn Lin /*
36e0832faSShawn Lin  * Copyright (c) Microsoft Corporation.
46e0832faSShawn Lin  *
56e0832faSShawn Lin  * Author:
66e0832faSShawn Lin  *   Jake Oshins <jakeo@microsoft.com>
76e0832faSShawn Lin  *
86e0832faSShawn Lin  * This driver acts as a paravirtual front-end for PCI Express root buses.
96e0832faSShawn Lin  * When a PCI Express function (either an entire device or an SR-IOV
106e0832faSShawn Lin  * Virtual Function) is being passed through to the VM, this driver exposes
116e0832faSShawn Lin  * a new bus to the guest VM.  This is modeled as a root PCI bus because
126e0832faSShawn Lin  * no bridges are being exposed to the VM.  In fact, with a "Generation 2"
136e0832faSShawn Lin  * VM within Hyper-V, there may seem to be no PCI bus at all in the VM
146e0832faSShawn Lin  * until a device as been exposed using this driver.
156e0832faSShawn Lin  *
166e0832faSShawn Lin  * Each root PCI bus has its own PCI domain, which is called "Segment" in
176e0832faSShawn Lin  * the PCI Firmware Specifications.  Thus while each device passed through
186e0832faSShawn Lin  * to the VM using this front-end will appear at "device 0", the domain will
196e0832faSShawn Lin  * be unique.  Typically, each bus will have one PCI function on it, though
206e0832faSShawn Lin  * this driver does support more than one.
216e0832faSShawn Lin  *
226e0832faSShawn Lin  * In order to map the interrupts from the device through to the guest VM,
236e0832faSShawn Lin  * this driver also implements an IRQ Domain, which handles interrupts (either
246e0832faSShawn Lin  * MSI or MSI-X) associated with the functions on the bus.  As interrupts are
256e0832faSShawn Lin  * set up, torn down, or reaffined, this driver communicates with the
266e0832faSShawn Lin  * underlying hypervisor to adjust the mappings in the I/O MMU so that each
276e0832faSShawn Lin  * interrupt will be delivered to the correct virtual processor at the right
286e0832faSShawn Lin  * vector.  This driver does not support level-triggered (line-based)
296e0832faSShawn Lin  * interrupts, and will report that the Interrupt Line register in the
306e0832faSShawn Lin  * function's configuration space is zero.
316e0832faSShawn Lin  *
326e0832faSShawn Lin  * The rest of this driver mostly maps PCI concepts onto underlying Hyper-V
336e0832faSShawn Lin  * facilities.  For instance, the configuration space of a function exposed
346e0832faSShawn Lin  * by Hyper-V is mapped into a single page of memory space, and the
356e0832faSShawn Lin  * read and write handlers for config space must be aware of this mechanism.
366e0832faSShawn Lin  * Similarly, device setup and teardown involves messages sent to and from
376e0832faSShawn Lin  * the PCI back-end driver in Hyper-V.
386e0832faSShawn Lin  */
396e0832faSShawn Lin 
406e0832faSShawn Lin #include <linux/kernel.h>
416e0832faSShawn Lin #include <linux/module.h>
426e0832faSShawn Lin #include <linux/pci.h>
4388f94c7fSBoqun Feng #include <linux/pci-ecam.h>
446e0832faSShawn Lin #include <linux/delay.h>
456e0832faSShawn Lin #include <linux/semaphore.h>
46447ae316SNicolai Stange #include <linux/irq.h>
476e0832faSShawn Lin #include <linux/msi.h>
486e0832faSShawn Lin #include <linux/hyperv.h>
496e0832faSShawn Lin #include <linux/refcount.h>
50d9932b46SSunil Muthuswamy #include <linux/irqdomain.h>
51d9932b46SSunil Muthuswamy #include <linux/acpi.h>
52b5ff74c1SMichael Kelley #include <linux/sizes.h>
536e0832faSShawn Lin #include <asm/mshyperv.h>
546e0832faSShawn Lin 
556e0832faSShawn Lin /*
566e0832faSShawn Lin  * Protocol versions. The low word is the minor version, the high word the
576e0832faSShawn Lin  * major version.
586e0832faSShawn Lin  */
596e0832faSShawn Lin 
606e0832faSShawn Lin #define PCI_MAKE_VERSION(major, minor) ((u32)(((major) << 16) | (minor)))
616e0832faSShawn Lin #define PCI_MAJOR_VERSION(version) ((u32)(version) >> 16)
626e0832faSShawn Lin #define PCI_MINOR_VERSION(version) ((u32)(version) & 0xff)
636e0832faSShawn Lin 
646e0832faSShawn Lin enum pci_protocol_version_t {
656e0832faSShawn Lin 	PCI_PROTOCOL_VERSION_1_1 = PCI_MAKE_VERSION(1, 1),	/* Win10 */
666e0832faSShawn Lin 	PCI_PROTOCOL_VERSION_1_2 = PCI_MAKE_VERSION(1, 2),	/* RS1 */
67999dd956SLong Li 	PCI_PROTOCOL_VERSION_1_3 = PCI_MAKE_VERSION(1, 3),	/* Vibranium */
688f6a6b3cSSunil Muthuswamy 	PCI_PROTOCOL_VERSION_1_4 = PCI_MAKE_VERSION(1, 4),	/* WS2022 */
696e0832faSShawn Lin };
706e0832faSShawn Lin 
716e0832faSShawn Lin #define CPU_AFFINITY_ALL	-1ULL
726e0832faSShawn Lin 
736e0832faSShawn Lin /*
746e0832faSShawn Lin  * Supported protocol versions in the order of probing - highest go
756e0832faSShawn Lin  * first.
766e0832faSShawn Lin  */
776e0832faSShawn Lin static enum pci_protocol_version_t pci_protocol_versions[] = {
788f6a6b3cSSunil Muthuswamy 	PCI_PROTOCOL_VERSION_1_4,
79999dd956SLong Li 	PCI_PROTOCOL_VERSION_1_3,
806e0832faSShawn Lin 	PCI_PROTOCOL_VERSION_1_2,
816e0832faSShawn Lin 	PCI_PROTOCOL_VERSION_1_1,
826e0832faSShawn Lin };
836e0832faSShawn Lin 
846e0832faSShawn Lin #define PCI_CONFIG_MMIO_LENGTH	0x2000
856e0832faSShawn Lin #define CFG_PAGE_OFFSET 0x1000
866e0832faSShawn Lin #define CFG_PAGE_SIZE (PCI_CONFIG_MMIO_LENGTH - CFG_PAGE_OFFSET)
876e0832faSShawn Lin 
886e0832faSShawn Lin #define MAX_SUPPORTED_MSI_MESSAGES 0x400
896e0832faSShawn Lin 
906e0832faSShawn Lin #define STATUS_REVISION_MISMATCH 0xC0000059
916e0832faSShawn Lin 
92a15f2c08SStephen Hemminger /* space for 32bit serial number as string */
93a15f2c08SStephen Hemminger #define SLOT_NAME_SIZE 11
94a15f2c08SStephen Hemminger 
956e0832faSShawn Lin /*
96de5ddb7dSAndrea Parri (Microsoft)  * Size of requestor for VMbus; the value is based on the observation
97de5ddb7dSAndrea Parri (Microsoft)  * that having more than one request outstanding is 'rare', and so 64
98de5ddb7dSAndrea Parri (Microsoft)  * should be generous in ensuring that we don't ever run out.
99de5ddb7dSAndrea Parri (Microsoft)  */
100de5ddb7dSAndrea Parri (Microsoft) #define HV_PCI_RQSTOR_SIZE 64
101de5ddb7dSAndrea Parri (Microsoft) 
102de5ddb7dSAndrea Parri (Microsoft) /*
1036e0832faSShawn Lin  * Message Types
1046e0832faSShawn Lin  */
1056e0832faSShawn Lin 
1066e0832faSShawn Lin enum pci_message_type {
1076e0832faSShawn Lin 	/*
1086e0832faSShawn Lin 	 * Version 1.1
1096e0832faSShawn Lin 	 */
1106e0832faSShawn Lin 	PCI_MESSAGE_BASE                = 0x42490000,
1116e0832faSShawn Lin 	PCI_BUS_RELATIONS               = PCI_MESSAGE_BASE + 0,
1126e0832faSShawn Lin 	PCI_QUERY_BUS_RELATIONS         = PCI_MESSAGE_BASE + 1,
1136e0832faSShawn Lin 	PCI_POWER_STATE_CHANGE          = PCI_MESSAGE_BASE + 4,
1146e0832faSShawn Lin 	PCI_QUERY_RESOURCE_REQUIREMENTS = PCI_MESSAGE_BASE + 5,
1156e0832faSShawn Lin 	PCI_QUERY_RESOURCE_RESOURCES    = PCI_MESSAGE_BASE + 6,
1166e0832faSShawn Lin 	PCI_BUS_D0ENTRY                 = PCI_MESSAGE_BASE + 7,
1176e0832faSShawn Lin 	PCI_BUS_D0EXIT                  = PCI_MESSAGE_BASE + 8,
1186e0832faSShawn Lin 	PCI_READ_BLOCK                  = PCI_MESSAGE_BASE + 9,
1196e0832faSShawn Lin 	PCI_WRITE_BLOCK                 = PCI_MESSAGE_BASE + 0xA,
1206e0832faSShawn Lin 	PCI_EJECT                       = PCI_MESSAGE_BASE + 0xB,
1216e0832faSShawn Lin 	PCI_QUERY_STOP                  = PCI_MESSAGE_BASE + 0xC,
1226e0832faSShawn Lin 	PCI_REENABLE                    = PCI_MESSAGE_BASE + 0xD,
1236e0832faSShawn Lin 	PCI_QUERY_STOP_FAILED           = PCI_MESSAGE_BASE + 0xE,
1246e0832faSShawn Lin 	PCI_EJECTION_COMPLETE           = PCI_MESSAGE_BASE + 0xF,
1256e0832faSShawn Lin 	PCI_RESOURCES_ASSIGNED          = PCI_MESSAGE_BASE + 0x10,
1266e0832faSShawn Lin 	PCI_RESOURCES_RELEASED          = PCI_MESSAGE_BASE + 0x11,
1276e0832faSShawn Lin 	PCI_INVALIDATE_BLOCK            = PCI_MESSAGE_BASE + 0x12,
1286e0832faSShawn Lin 	PCI_QUERY_PROTOCOL_VERSION      = PCI_MESSAGE_BASE + 0x13,
1296e0832faSShawn Lin 	PCI_CREATE_INTERRUPT_MESSAGE    = PCI_MESSAGE_BASE + 0x14,
1306e0832faSShawn Lin 	PCI_DELETE_INTERRUPT_MESSAGE    = PCI_MESSAGE_BASE + 0x15,
1316e0832faSShawn Lin 	PCI_RESOURCES_ASSIGNED2		= PCI_MESSAGE_BASE + 0x16,
1326e0832faSShawn Lin 	PCI_CREATE_INTERRUPT_MESSAGE2	= PCI_MESSAGE_BASE + 0x17,
1336e0832faSShawn Lin 	PCI_DELETE_INTERRUPT_MESSAGE2	= PCI_MESSAGE_BASE + 0x18, /* unused */
134999dd956SLong Li 	PCI_BUS_RELATIONS2		= PCI_MESSAGE_BASE + 0x19,
1358f6a6b3cSSunil Muthuswamy 	PCI_RESOURCES_ASSIGNED3         = PCI_MESSAGE_BASE + 0x1A,
1368f6a6b3cSSunil Muthuswamy 	PCI_CREATE_INTERRUPT_MESSAGE3   = PCI_MESSAGE_BASE + 0x1B,
1376e0832faSShawn Lin 	PCI_MESSAGE_MAXIMUM
1386e0832faSShawn Lin };
1396e0832faSShawn Lin 
1406e0832faSShawn Lin /*
1416e0832faSShawn Lin  * Structures defining the virtual PCI Express protocol.
1426e0832faSShawn Lin  */
1436e0832faSShawn Lin 
1446e0832faSShawn Lin union pci_version {
1456e0832faSShawn Lin 	struct {
1466e0832faSShawn Lin 		u16 minor_version;
1476e0832faSShawn Lin 		u16 major_version;
1486e0832faSShawn Lin 	} parts;
1496e0832faSShawn Lin 	u32 version;
1506e0832faSShawn Lin } __packed;
1516e0832faSShawn Lin 
1526e0832faSShawn Lin /*
1536e0832faSShawn Lin  * Function numbers are 8-bits wide on Express, as interpreted through ARI,
1546e0832faSShawn Lin  * which is all this driver does.  This representation is the one used in
1556e0832faSShawn Lin  * Windows, which is what is expected when sending this back and forth with
1566e0832faSShawn Lin  * the Hyper-V parent partition.
1576e0832faSShawn Lin  */
1586e0832faSShawn Lin union win_slot_encoding {
1596e0832faSShawn Lin 	struct {
1606e0832faSShawn Lin 		u32	dev:5;
1616e0832faSShawn Lin 		u32	func:3;
1626e0832faSShawn Lin 		u32	reserved:24;
1636e0832faSShawn Lin 	} bits;
1646e0832faSShawn Lin 	u32 slot;
1656e0832faSShawn Lin } __packed;
1666e0832faSShawn Lin 
1676e0832faSShawn Lin /*
1686e0832faSShawn Lin  * Pretty much as defined in the PCI Specifications.
1696e0832faSShawn Lin  */
1706e0832faSShawn Lin struct pci_function_description {
1716e0832faSShawn Lin 	u16	v_id;	/* vendor ID */
1726e0832faSShawn Lin 	u16	d_id;	/* device ID */
1736e0832faSShawn Lin 	u8	rev;
1746e0832faSShawn Lin 	u8	prog_intf;
1756e0832faSShawn Lin 	u8	subclass;
1766e0832faSShawn Lin 	u8	base_class;
1776e0832faSShawn Lin 	u32	subsystem_id;
1786e0832faSShawn Lin 	union win_slot_encoding win_slot;
1796e0832faSShawn Lin 	u32	ser;	/* serial number */
1806e0832faSShawn Lin } __packed;
1816e0832faSShawn Lin 
182999dd956SLong Li enum pci_device_description_flags {
183999dd956SLong Li 	HV_PCI_DEVICE_FLAG_NONE			= 0x0,
184999dd956SLong Li 	HV_PCI_DEVICE_FLAG_NUMA_AFFINITY	= 0x1,
185999dd956SLong Li };
186999dd956SLong Li 
187999dd956SLong Li struct pci_function_description2 {
188999dd956SLong Li 	u16	v_id;	/* vendor ID */
189999dd956SLong Li 	u16	d_id;	/* device ID */
190999dd956SLong Li 	u8	rev;
191999dd956SLong Li 	u8	prog_intf;
192999dd956SLong Li 	u8	subclass;
193999dd956SLong Li 	u8	base_class;
194999dd956SLong Li 	u32	subsystem_id;
195999dd956SLong Li 	union	win_slot_encoding win_slot;
196999dd956SLong Li 	u32	ser;	/* serial number */
197999dd956SLong Li 	u32	flags;
198999dd956SLong Li 	u16	virtual_numa_node;
199999dd956SLong Li 	u16	reserved;
200999dd956SLong Li } __packed;
201999dd956SLong Li 
2026e0832faSShawn Lin /**
2036e0832faSShawn Lin  * struct hv_msi_desc
2046e0832faSShawn Lin  * @vector:		IDT entry
2056e0832faSShawn Lin  * @delivery_mode:	As defined in Intel's Programmer's
2066e0832faSShawn Lin  *			Reference Manual, Volume 3, Chapter 8.
2076e0832faSShawn Lin  * @vector_count:	Number of contiguous entries in the
2086e0832faSShawn Lin  *			Interrupt Descriptor Table that are
2096e0832faSShawn Lin  *			occupied by this Message-Signaled
2106e0832faSShawn Lin  *			Interrupt. For "MSI", as first defined
2116e0832faSShawn Lin  *			in PCI 2.2, this can be between 1 and
2126e0832faSShawn Lin  *			32. For "MSI-X," as first defined in PCI
2136e0832faSShawn Lin  *			3.0, this must be 1, as each MSI-X table
2146e0832faSShawn Lin  *			entry would have its own descriptor.
2156e0832faSShawn Lin  * @reserved:		Empty space
2166e0832faSShawn Lin  * @cpu_mask:		All the target virtual processors.
2176e0832faSShawn Lin  */
2186e0832faSShawn Lin struct hv_msi_desc {
2196e0832faSShawn Lin 	u8	vector;
2206e0832faSShawn Lin 	u8	delivery_mode;
2216e0832faSShawn Lin 	u16	vector_count;
2226e0832faSShawn Lin 	u32	reserved;
2236e0832faSShawn Lin 	u64	cpu_mask;
2246e0832faSShawn Lin } __packed;
2256e0832faSShawn Lin 
2266e0832faSShawn Lin /**
2276e0832faSShawn Lin  * struct hv_msi_desc2 - 1.2 version of hv_msi_desc
2286e0832faSShawn Lin  * @vector:		IDT entry
2296e0832faSShawn Lin  * @delivery_mode:	As defined in Intel's Programmer's
2306e0832faSShawn Lin  *			Reference Manual, Volume 3, Chapter 8.
2316e0832faSShawn Lin  * @vector_count:	Number of contiguous entries in the
2326e0832faSShawn Lin  *			Interrupt Descriptor Table that are
2336e0832faSShawn Lin  *			occupied by this Message-Signaled
2346e0832faSShawn Lin  *			Interrupt. For "MSI", as first defined
2356e0832faSShawn Lin  *			in PCI 2.2, this can be between 1 and
2366e0832faSShawn Lin  *			32. For "MSI-X," as first defined in PCI
2376e0832faSShawn Lin  *			3.0, this must be 1, as each MSI-X table
2386e0832faSShawn Lin  *			entry would have its own descriptor.
2396e0832faSShawn Lin  * @processor_count:	number of bits enabled in array.
2406e0832faSShawn Lin  * @processor_array:	All the target virtual processors.
2416e0832faSShawn Lin  */
2426e0832faSShawn Lin struct hv_msi_desc2 {
2436e0832faSShawn Lin 	u8	vector;
2446e0832faSShawn Lin 	u8	delivery_mode;
2456e0832faSShawn Lin 	u16	vector_count;
2466e0832faSShawn Lin 	u16	processor_count;
2476e0832faSShawn Lin 	u16	processor_array[32];
2486e0832faSShawn Lin } __packed;
2496e0832faSShawn Lin 
2508f6a6b3cSSunil Muthuswamy /*
2518f6a6b3cSSunil Muthuswamy  * struct hv_msi_desc3 - 1.3 version of hv_msi_desc
2528f6a6b3cSSunil Muthuswamy  *	Everything is the same as in 'hv_msi_desc2' except that the size of the
2538f6a6b3cSSunil Muthuswamy  *	'vector' field is larger to support bigger vector values. For ex: LPI
2548f6a6b3cSSunil Muthuswamy  *	vectors on ARM.
2558f6a6b3cSSunil Muthuswamy  */
2568f6a6b3cSSunil Muthuswamy struct hv_msi_desc3 {
2578f6a6b3cSSunil Muthuswamy 	u32	vector;
2588f6a6b3cSSunil Muthuswamy 	u8	delivery_mode;
2598f6a6b3cSSunil Muthuswamy 	u8	reserved;
2608f6a6b3cSSunil Muthuswamy 	u16	vector_count;
2618f6a6b3cSSunil Muthuswamy 	u16	processor_count;
2628f6a6b3cSSunil Muthuswamy 	u16	processor_array[32];
2638f6a6b3cSSunil Muthuswamy } __packed;
2648f6a6b3cSSunil Muthuswamy 
2656e0832faSShawn Lin /**
2666e0832faSShawn Lin  * struct tran_int_desc
2676e0832faSShawn Lin  * @reserved:		unused, padding
2686e0832faSShawn Lin  * @vector_count:	same as in hv_msi_desc
2696e0832faSShawn Lin  * @data:		This is the "data payload" value that is
2706e0832faSShawn Lin  *			written by the device when it generates
2716e0832faSShawn Lin  *			a message-signaled interrupt, either MSI
2726e0832faSShawn Lin  *			or MSI-X.
2736e0832faSShawn Lin  * @address:		This is the address to which the data
2746e0832faSShawn Lin  *			payload is written on interrupt
2756e0832faSShawn Lin  *			generation.
2766e0832faSShawn Lin  */
2776e0832faSShawn Lin struct tran_int_desc {
2786e0832faSShawn Lin 	u16	reserved;
2796e0832faSShawn Lin 	u16	vector_count;
2806e0832faSShawn Lin 	u32	data;
2816e0832faSShawn Lin 	u64	address;
2826e0832faSShawn Lin } __packed;
2836e0832faSShawn Lin 
2846e0832faSShawn Lin /*
2856e0832faSShawn Lin  * A generic message format for virtual PCI.
2866e0832faSShawn Lin  * Specific message formats are defined later in the file.
2876e0832faSShawn Lin  */
2886e0832faSShawn Lin 
2896e0832faSShawn Lin struct pci_message {
2906e0832faSShawn Lin 	u32 type;
2916e0832faSShawn Lin } __packed;
2926e0832faSShawn Lin 
2936e0832faSShawn Lin struct pci_child_message {
2946e0832faSShawn Lin 	struct pci_message message_type;
2956e0832faSShawn Lin 	union win_slot_encoding wslot;
2966e0832faSShawn Lin } __packed;
2976e0832faSShawn Lin 
2986e0832faSShawn Lin struct pci_incoming_message {
2996e0832faSShawn Lin 	struct vmpacket_descriptor hdr;
3006e0832faSShawn Lin 	struct pci_message message_type;
3016e0832faSShawn Lin } __packed;
3026e0832faSShawn Lin 
3036e0832faSShawn Lin struct pci_response {
3046e0832faSShawn Lin 	struct vmpacket_descriptor hdr;
3056e0832faSShawn Lin 	s32 status;			/* negative values are failures */
3066e0832faSShawn Lin } __packed;
3076e0832faSShawn Lin 
3086e0832faSShawn Lin struct pci_packet {
3096e0832faSShawn Lin 	void (*completion_func)(void *context, struct pci_response *resp,
3106e0832faSShawn Lin 				int resp_packet_size);
3116e0832faSShawn Lin 	void *compl_ctxt;
3126e0832faSShawn Lin 
313067fb6c9SGustavo A. R. Silva 	struct pci_message message[];
3146e0832faSShawn Lin };
3156e0832faSShawn Lin 
3166e0832faSShawn Lin /*
3176e0832faSShawn Lin  * Specific message types supporting the PCI protocol.
3186e0832faSShawn Lin  */
3196e0832faSShawn Lin 
3206e0832faSShawn Lin /*
3216e0832faSShawn Lin  * Version negotiation message. Sent from the guest to the host.
3226e0832faSShawn Lin  * The guest is free to try different versions until the host
3236e0832faSShawn Lin  * accepts the version.
3246e0832faSShawn Lin  *
3256e0832faSShawn Lin  * pci_version: The protocol version requested.
3266e0832faSShawn Lin  * is_last_attempt: If TRUE, this is the last version guest will request.
3276e0832faSShawn Lin  * reservedz: Reserved field, set to zero.
3286e0832faSShawn Lin  */
3296e0832faSShawn Lin 
3306e0832faSShawn Lin struct pci_version_request {
3316e0832faSShawn Lin 	struct pci_message message_type;
3326e0832faSShawn Lin 	u32 protocol_version;
3336e0832faSShawn Lin } __packed;
3346e0832faSShawn Lin 
3356e0832faSShawn Lin /*
3366e0832faSShawn Lin  * Bus D0 Entry.  This is sent from the guest to the host when the virtual
3376e0832faSShawn Lin  * bus (PCI Express port) is ready for action.
3386e0832faSShawn Lin  */
3396e0832faSShawn Lin 
3406e0832faSShawn Lin struct pci_bus_d0_entry {
3416e0832faSShawn Lin 	struct pci_message message_type;
3426e0832faSShawn Lin 	u32 reserved;
3436e0832faSShawn Lin 	u64 mmio_base;
3446e0832faSShawn Lin } __packed;
3456e0832faSShawn Lin 
3466e0832faSShawn Lin struct pci_bus_relations {
3476e0832faSShawn Lin 	struct pci_incoming_message incoming;
3486e0832faSShawn Lin 	u32 device_count;
349067fb6c9SGustavo A. R. Silva 	struct pci_function_description func[];
3506e0832faSShawn Lin } __packed;
3516e0832faSShawn Lin 
352999dd956SLong Li struct pci_bus_relations2 {
353999dd956SLong Li 	struct pci_incoming_message incoming;
354999dd956SLong Li 	u32 device_count;
355067fb6c9SGustavo A. R. Silva 	struct pci_function_description2 func[];
356999dd956SLong Li } __packed;
357999dd956SLong Li 
3586e0832faSShawn Lin struct pci_q_res_req_response {
3596e0832faSShawn Lin 	struct vmpacket_descriptor hdr;
3606e0832faSShawn Lin 	s32 status;			/* negative values are failures */
361c9c13ba4SDenis Efremov 	u32 probed_bar[PCI_STD_NUM_BARS];
3626e0832faSShawn Lin } __packed;
3636e0832faSShawn Lin 
3646e0832faSShawn Lin struct pci_set_power {
3656e0832faSShawn Lin 	struct pci_message message_type;
3666e0832faSShawn Lin 	union win_slot_encoding wslot;
3676e0832faSShawn Lin 	u32 power_state;		/* In Windows terms */
3686e0832faSShawn Lin 	u32 reserved;
3696e0832faSShawn Lin } __packed;
3706e0832faSShawn Lin 
3716e0832faSShawn Lin struct pci_set_power_response {
3726e0832faSShawn Lin 	struct vmpacket_descriptor hdr;
3736e0832faSShawn Lin 	s32 status;			/* negative values are failures */
3746e0832faSShawn Lin 	union win_slot_encoding wslot;
3756e0832faSShawn Lin 	u32 resultant_state;		/* In Windows terms */
3766e0832faSShawn Lin 	u32 reserved;
3776e0832faSShawn Lin } __packed;
3786e0832faSShawn Lin 
3796e0832faSShawn Lin struct pci_resources_assigned {
3806e0832faSShawn Lin 	struct pci_message message_type;
3816e0832faSShawn Lin 	union win_slot_encoding wslot;
3826e0832faSShawn Lin 	u8 memory_range[0x14][6];	/* not used here */
3836e0832faSShawn Lin 	u32 msi_descriptors;
3846e0832faSShawn Lin 	u32 reserved[4];
3856e0832faSShawn Lin } __packed;
3866e0832faSShawn Lin 
3876e0832faSShawn Lin struct pci_resources_assigned2 {
3886e0832faSShawn Lin 	struct pci_message message_type;
3896e0832faSShawn Lin 	union win_slot_encoding wslot;
3906e0832faSShawn Lin 	u8 memory_range[0x14][6];	/* not used here */
3916e0832faSShawn Lin 	u32 msi_descriptor_count;
3926e0832faSShawn Lin 	u8 reserved[70];
3936e0832faSShawn Lin } __packed;
3946e0832faSShawn Lin 
3956e0832faSShawn Lin struct pci_create_interrupt {
3966e0832faSShawn Lin 	struct pci_message message_type;
3976e0832faSShawn Lin 	union win_slot_encoding wslot;
3986e0832faSShawn Lin 	struct hv_msi_desc int_desc;
3996e0832faSShawn Lin } __packed;
4006e0832faSShawn Lin 
4016e0832faSShawn Lin struct pci_create_int_response {
4026e0832faSShawn Lin 	struct pci_response response;
4036e0832faSShawn Lin 	u32 reserved;
4046e0832faSShawn Lin 	struct tran_int_desc int_desc;
4056e0832faSShawn Lin } __packed;
4066e0832faSShawn Lin 
4076e0832faSShawn Lin struct pci_create_interrupt2 {
4086e0832faSShawn Lin 	struct pci_message message_type;
4096e0832faSShawn Lin 	union win_slot_encoding wslot;
4106e0832faSShawn Lin 	struct hv_msi_desc2 int_desc;
4116e0832faSShawn Lin } __packed;
4126e0832faSShawn Lin 
4138f6a6b3cSSunil Muthuswamy struct pci_create_interrupt3 {
4148f6a6b3cSSunil Muthuswamy 	struct pci_message message_type;
4158f6a6b3cSSunil Muthuswamy 	union win_slot_encoding wslot;
4168f6a6b3cSSunil Muthuswamy 	struct hv_msi_desc3 int_desc;
4178f6a6b3cSSunil Muthuswamy } __packed;
4188f6a6b3cSSunil Muthuswamy 
4196e0832faSShawn Lin struct pci_delete_interrupt {
4206e0832faSShawn Lin 	struct pci_message message_type;
4216e0832faSShawn Lin 	union win_slot_encoding wslot;
4226e0832faSShawn Lin 	struct tran_int_desc int_desc;
4236e0832faSShawn Lin } __packed;
4246e0832faSShawn Lin 
425e5d2f910SDexuan Cui /*
426e5d2f910SDexuan Cui  * Note: the VM must pass a valid block id, wslot and bytes_requested.
427e5d2f910SDexuan Cui  */
428e5d2f910SDexuan Cui struct pci_read_block {
429e5d2f910SDexuan Cui 	struct pci_message message_type;
430e5d2f910SDexuan Cui 	u32 block_id;
431e5d2f910SDexuan Cui 	union win_slot_encoding wslot;
432e5d2f910SDexuan Cui 	u32 bytes_requested;
433e5d2f910SDexuan Cui } __packed;
434e5d2f910SDexuan Cui 
435e5d2f910SDexuan Cui struct pci_read_block_response {
436e5d2f910SDexuan Cui 	struct vmpacket_descriptor hdr;
437e5d2f910SDexuan Cui 	u32 status;
438e5d2f910SDexuan Cui 	u8 bytes[HV_CONFIG_BLOCK_SIZE_MAX];
439e5d2f910SDexuan Cui } __packed;
440e5d2f910SDexuan Cui 
441e5d2f910SDexuan Cui /*
442e5d2f910SDexuan Cui  * Note: the VM must pass a valid block id, wslot and byte_count.
443e5d2f910SDexuan Cui  */
444e5d2f910SDexuan Cui struct pci_write_block {
445e5d2f910SDexuan Cui 	struct pci_message message_type;
446e5d2f910SDexuan Cui 	u32 block_id;
447e5d2f910SDexuan Cui 	union win_slot_encoding wslot;
448e5d2f910SDexuan Cui 	u32 byte_count;
449e5d2f910SDexuan Cui 	u8 bytes[HV_CONFIG_BLOCK_SIZE_MAX];
450e5d2f910SDexuan Cui } __packed;
451e5d2f910SDexuan Cui 
452e5d2f910SDexuan Cui struct pci_dev_inval_block {
453e5d2f910SDexuan Cui 	struct pci_incoming_message incoming;
454e5d2f910SDexuan Cui 	union win_slot_encoding wslot;
455e5d2f910SDexuan Cui 	u64 block_mask;
456e5d2f910SDexuan Cui } __packed;
457e5d2f910SDexuan Cui 
4586e0832faSShawn Lin struct pci_dev_incoming {
4596e0832faSShawn Lin 	struct pci_incoming_message incoming;
4606e0832faSShawn Lin 	union win_slot_encoding wslot;
4616e0832faSShawn Lin } __packed;
4626e0832faSShawn Lin 
4636e0832faSShawn Lin struct pci_eject_response {
4646e0832faSShawn Lin 	struct pci_message message_type;
4656e0832faSShawn Lin 	union win_slot_encoding wslot;
4666e0832faSShawn Lin 	u32 status;
4676e0832faSShawn Lin } __packed;
4686e0832faSShawn Lin 
469b5ff74c1SMichael Kelley static int pci_ring_size = VMBUS_RING_SIZE(SZ_16K);
4706e0832faSShawn Lin 
4716e0832faSShawn Lin /*
4726e0832faSShawn Lin  * Driver specific state.
4736e0832faSShawn Lin  */
4746e0832faSShawn Lin 
4756e0832faSShawn Lin enum hv_pcibus_state {
4766e0832faSShawn Lin 	hv_pcibus_init = 0,
4776e0832faSShawn Lin 	hv_pcibus_probed,
4786e0832faSShawn Lin 	hv_pcibus_installed,
479ac82fc83SDexuan Cui 	hv_pcibus_removing,
4806e0832faSShawn Lin 	hv_pcibus_maximum
4816e0832faSShawn Lin };
4826e0832faSShawn Lin 
4836e0832faSShawn Lin struct hv_pcibus_device {
48488f94c7fSBoqun Feng #ifdef CONFIG_X86
4856e0832faSShawn Lin 	struct pci_sysdata sysdata;
48688f94c7fSBoqun Feng #elif defined(CONFIG_ARM64)
48788f94c7fSBoqun Feng 	struct pci_config_window sysdata;
48888f94c7fSBoqun Feng #endif
489418cb6c8SArnd Bergmann 	struct pci_host_bridge *bridge;
4909e7f9178SBoqun Feng 	struct fwnode_handle *fwnode;
49114ef39fdSDexuan Cui 	/* Protocol version negotiated with the host */
49214ef39fdSDexuan Cui 	enum pci_protocol_version_t protocol_version;
493067d6ec7SDexuan Cui 
494067d6ec7SDexuan Cui 	struct mutex state_lock;
4956e0832faSShawn Lin 	enum hv_pcibus_state state;
496067d6ec7SDexuan Cui 
4976e0832faSShawn Lin 	struct hv_device *hdev;
4986e0832faSShawn Lin 	resource_size_t low_mmio_space;
4996e0832faSShawn Lin 	resource_size_t high_mmio_space;
5006e0832faSShawn Lin 	struct resource *mem_config;
5016e0832faSShawn Lin 	struct resource *low_mmio_res;
5026e0832faSShawn Lin 	struct resource *high_mmio_res;
5036e0832faSShawn Lin 	struct completion *survey_event;
5046e0832faSShawn Lin 	struct pci_bus *pci_bus;
5056e0832faSShawn Lin 	spinlock_t config_lock;	/* Avoid two threads writing index page */
5066e0832faSShawn Lin 	spinlock_t device_list_lock;	/* Protect lists below */
5076e0832faSShawn Lin 	void __iomem *cfg_addr;
5086e0832faSShawn Lin 
5096e0832faSShawn Lin 	struct list_head children;
5106e0832faSShawn Lin 	struct list_head dr_list;
5116e0832faSShawn Lin 
5126e0832faSShawn Lin 	struct msi_domain_info msi_info;
5136e0832faSShawn Lin 	struct irq_domain *irq_domain;
5146e0832faSShawn Lin 
5156e0832faSShawn Lin 	struct workqueue_struct *wq;
5169bc11742SMaya Nakamura 
51783cc3508SWei Hu 	/* Highest slot of child device with resources allocated */
51883cc3508SWei Hu 	int wslot_res_allocated;
5192c6ba421SMichael Kelley 	bool use_calls; /* Use hypercalls to access mmio cfg space */
5206e0832faSShawn Lin };
5216e0832faSShawn Lin 
5226e0832faSShawn Lin /*
5236e0832faSShawn Lin  * Tracks "Device Relations" messages from the host, which must be both
5246e0832faSShawn Lin  * processed in order and deferred so that they don't run in the context
5256e0832faSShawn Lin  * of the incoming packet callback.
5266e0832faSShawn Lin  */
5276e0832faSShawn Lin struct hv_dr_work {
5286e0832faSShawn Lin 	struct work_struct wrk;
5296e0832faSShawn Lin 	struct hv_pcibus_device *bus;
5306e0832faSShawn Lin };
5316e0832faSShawn Lin 
532f9ad0f36SLong Li struct hv_pcidev_description {
533f9ad0f36SLong Li 	u16	v_id;	/* vendor ID */
534f9ad0f36SLong Li 	u16	d_id;	/* device ID */
535f9ad0f36SLong Li 	u8	rev;
536f9ad0f36SLong Li 	u8	prog_intf;
537f9ad0f36SLong Li 	u8	subclass;
538f9ad0f36SLong Li 	u8	base_class;
539f9ad0f36SLong Li 	u32	subsystem_id;
540f9ad0f36SLong Li 	union	win_slot_encoding win_slot;
541f9ad0f36SLong Li 	u32	ser;	/* serial number */
542f9ad0f36SLong Li 	u32	flags;
543f9ad0f36SLong Li 	u16	virtual_numa_node;
544f9ad0f36SLong Li };
545f9ad0f36SLong Li 
5466e0832faSShawn Lin struct hv_dr_state {
5476e0832faSShawn Lin 	struct list_head list_entry;
5486e0832faSShawn Lin 	u32 device_count;
549f741bcadSKees Cook 	struct hv_pcidev_description func[] __counted_by(device_count);
5506e0832faSShawn Lin };
5516e0832faSShawn Lin 
5526e0832faSShawn Lin struct hv_pci_dev {
5536e0832faSShawn Lin 	/* List protected by pci_rescan_remove_lock */
5546e0832faSShawn Lin 	struct list_head list_entry;
5556e0832faSShawn Lin 	refcount_t refs;
556a15f2c08SStephen Hemminger 	struct pci_slot *pci_slot;
557f9ad0f36SLong Li 	struct hv_pcidev_description desc;
5586e0832faSShawn Lin 	bool reported_missing;
5596e0832faSShawn Lin 	struct hv_pcibus_device *hbus;
5606e0832faSShawn Lin 	struct work_struct wrk;
5616e0832faSShawn Lin 
562e5d2f910SDexuan Cui 	void (*block_invalidate)(void *context, u64 block_mask);
563e5d2f910SDexuan Cui 	void *invalidate_context;
564e5d2f910SDexuan Cui 
5656e0832faSShawn Lin 	/*
5666e0832faSShawn Lin 	 * What would be observed if one wrote 0xFFFFFFFF to a BAR and then
5676e0832faSShawn Lin 	 * read it back, for each of the BAR offsets within config space.
5686e0832faSShawn Lin 	 */
569c9c13ba4SDenis Efremov 	u32 probed_bar[PCI_STD_NUM_BARS];
5706e0832faSShawn Lin };
5716e0832faSShawn Lin 
5726e0832faSShawn Lin struct hv_pci_compl {
5736e0832faSShawn Lin 	struct completion host_event;
5746e0832faSShawn Lin 	s32 completion_status;
5756e0832faSShawn Lin };
5766e0832faSShawn Lin 
5776e0832faSShawn Lin static void hv_pci_onchannelcallback(void *context);
5786e0832faSShawn Lin 
579831c1ae7SSunil Muthuswamy #ifdef CONFIG_X86
580831c1ae7SSunil Muthuswamy #define DELIVERY_MODE	APIC_DELIVERY_MODE_FIXED
581831c1ae7SSunil Muthuswamy #define FLOW_HANDLER	handle_edge_irq
582831c1ae7SSunil Muthuswamy #define FLOW_NAME	"edge"
583831c1ae7SSunil Muthuswamy 
hv_pci_irqchip_init(void)584831c1ae7SSunil Muthuswamy static int hv_pci_irqchip_init(void)
585831c1ae7SSunil Muthuswamy {
586831c1ae7SSunil Muthuswamy 	return 0;
587831c1ae7SSunil Muthuswamy }
588831c1ae7SSunil Muthuswamy 
hv_pci_get_root_domain(void)589831c1ae7SSunil Muthuswamy static struct irq_domain *hv_pci_get_root_domain(void)
590831c1ae7SSunil Muthuswamy {
591831c1ae7SSunil Muthuswamy 	return x86_vector_domain;
592831c1ae7SSunil Muthuswamy }
593831c1ae7SSunil Muthuswamy 
hv_msi_get_int_vector(struct irq_data * data)594831c1ae7SSunil Muthuswamy static unsigned int hv_msi_get_int_vector(struct irq_data *data)
595831c1ae7SSunil Muthuswamy {
596831c1ae7SSunil Muthuswamy 	struct irq_cfg *cfg = irqd_cfg(data);
597831c1ae7SSunil Muthuswamy 
598831c1ae7SSunil Muthuswamy 	return cfg->vector;
599831c1ae7SSunil Muthuswamy }
600831c1ae7SSunil Muthuswamy 
601d474d92dSThomas Gleixner #define hv_msi_prepare		pci_msi_prepare
602d06957d7SBoqun Feng 
603d06957d7SBoqun Feng /**
604d06957d7SBoqun Feng  * hv_arch_irq_unmask() - "Unmask" the IRQ by setting its current
605d06957d7SBoqun Feng  * affinity.
606d06957d7SBoqun Feng  * @data:	Describes the IRQ
607d06957d7SBoqun Feng  *
608d06957d7SBoqun Feng  * Build new a destination for the MSI and make a hypercall to
609d06957d7SBoqun Feng  * update the Interrupt Redirection Table. "Device Logical ID"
610d06957d7SBoqun Feng  * is built out of this PCI bus's instance GUID and the function
611d06957d7SBoqun Feng  * number of the device.
612d06957d7SBoqun Feng  */
hv_arch_irq_unmask(struct irq_data * data)613d06957d7SBoqun Feng static void hv_arch_irq_unmask(struct irq_data *data)
614d06957d7SBoqun Feng {
615d06957d7SBoqun Feng 	struct msi_desc *msi_desc = irq_data_get_msi_desc(data);
616d06957d7SBoqun Feng 	struct hv_retarget_device_interrupt *params;
617455880dfSJeffrey Hugo 	struct tran_int_desc *int_desc;
618d06957d7SBoqun Feng 	struct hv_pcibus_device *hbus;
6194d0b8298SSamuel Holland 	const struct cpumask *dest;
620d06957d7SBoqun Feng 	cpumask_var_t tmp;
621d06957d7SBoqun Feng 	struct pci_bus *pbus;
622d06957d7SBoqun Feng 	struct pci_dev *pdev;
623d06957d7SBoqun Feng 	unsigned long flags;
624d06957d7SBoqun Feng 	u32 var_size = 0;
625d06957d7SBoqun Feng 	int cpu, nr_bank;
626d06957d7SBoqun Feng 	u64 res;
627d06957d7SBoqun Feng 
628d06957d7SBoqun Feng 	dest = irq_data_get_effective_affinity_mask(data);
629d06957d7SBoqun Feng 	pdev = msi_desc_to_pci_dev(msi_desc);
630d06957d7SBoqun Feng 	pbus = pdev->bus;
631d06957d7SBoqun Feng 	hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata);
632455880dfSJeffrey Hugo 	int_desc = data->chip_data;
6332738d5abSDexuan Cui 	if (!int_desc) {
6342738d5abSDexuan Cui 		dev_warn(&hbus->hdev->device, "%s() can not unmask irq %u\n",
6352738d5abSDexuan Cui 			 __func__, data->irq);
6362738d5abSDexuan Cui 		return;
6372738d5abSDexuan Cui 	}
638d06957d7SBoqun Feng 
639a494aef2SDexuan Cui 	local_irq_save(flags);
640d06957d7SBoqun Feng 
641a494aef2SDexuan Cui 	params = *this_cpu_ptr(hyperv_pcpu_input_arg);
642d06957d7SBoqun Feng 	memset(params, 0, sizeof(*params));
643d06957d7SBoqun Feng 	params->partition_id = HV_PARTITION_ID_SELF;
644d06957d7SBoqun Feng 	params->int_entry.source = HV_INTERRUPT_SOURCE_MSI;
645455880dfSJeffrey Hugo 	params->int_entry.msi_entry.address.as_uint32 = int_desc->address & 0xffffffff;
646455880dfSJeffrey Hugo 	params->int_entry.msi_entry.data.as_uint32 = int_desc->data;
647d06957d7SBoqun Feng 	params->device_id = (hbus->hdev->dev_instance.b[5] << 24) |
648d06957d7SBoqun Feng 			   (hbus->hdev->dev_instance.b[4] << 16) |
649d06957d7SBoqun Feng 			   (hbus->hdev->dev_instance.b[7] << 8) |
650d06957d7SBoqun Feng 			   (hbus->hdev->dev_instance.b[6] & 0xf8) |
651d06957d7SBoqun Feng 			   PCI_FUNC(pdev->devfn);
652d06957d7SBoqun Feng 	params->int_target.vector = hv_msi_get_int_vector(data);
653d06957d7SBoqun Feng 
654d06957d7SBoqun Feng 	if (hbus->protocol_version >= PCI_PROTOCOL_VERSION_1_2) {
655d06957d7SBoqun Feng 		/*
656d06957d7SBoqun Feng 		 * PCI_PROTOCOL_VERSION_1_2 supports the VP_SET version of the
657d06957d7SBoqun Feng 		 * HVCALL_RETARGET_INTERRUPT hypercall, which also coincides
658d06957d7SBoqun Feng 		 * with >64 VP support.
659d06957d7SBoqun Feng 		 * ms_hyperv.hints & HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED
660d06957d7SBoqun Feng 		 * is not sufficient for this hypercall.
661d06957d7SBoqun Feng 		 */
662d06957d7SBoqun Feng 		params->int_target.flags |=
663d06957d7SBoqun Feng 			HV_DEVICE_INTERRUPT_TARGET_PROCESSOR_SET;
664d06957d7SBoqun Feng 
665d06957d7SBoqun Feng 		if (!alloc_cpumask_var(&tmp, GFP_ATOMIC)) {
666d06957d7SBoqun Feng 			res = 1;
667a494aef2SDexuan Cui 			goto out;
668d06957d7SBoqun Feng 		}
669d06957d7SBoqun Feng 
670d06957d7SBoqun Feng 		cpumask_and(tmp, dest, cpu_online_mask);
671d06957d7SBoqun Feng 		nr_bank = cpumask_to_vpset(&params->int_target.vp_set, tmp);
672d06957d7SBoqun Feng 		free_cpumask_var(tmp);
673d06957d7SBoqun Feng 
674d06957d7SBoqun Feng 		if (nr_bank <= 0) {
675d06957d7SBoqun Feng 			res = 1;
676a494aef2SDexuan Cui 			goto out;
677d06957d7SBoqun Feng 		}
678d06957d7SBoqun Feng 
679d06957d7SBoqun Feng 		/*
680d06957d7SBoqun Feng 		 * var-sized hypercall, var-size starts after vp_mask (thus
681d06957d7SBoqun Feng 		 * vp_set.format does not count, but vp_set.valid_bank_mask
682d06957d7SBoqun Feng 		 * does).
683d06957d7SBoqun Feng 		 */
684d06957d7SBoqun Feng 		var_size = 1 + nr_bank;
685d06957d7SBoqun Feng 	} else {
686d06957d7SBoqun Feng 		for_each_cpu_and(cpu, dest, cpu_online_mask) {
687d06957d7SBoqun Feng 			params->int_target.vp_mask |=
688d06957d7SBoqun Feng 				(1ULL << hv_cpu_number_to_vp_number(cpu));
689d06957d7SBoqun Feng 		}
690d06957d7SBoqun Feng 	}
691d06957d7SBoqun Feng 
692d06957d7SBoqun Feng 	res = hv_do_hypercall(HVCALL_RETARGET_INTERRUPT | (var_size << 17),
693d06957d7SBoqun Feng 			      params, NULL);
694d06957d7SBoqun Feng 
695a494aef2SDexuan Cui out:
696a494aef2SDexuan Cui 	local_irq_restore(flags);
697d06957d7SBoqun Feng 
698d06957d7SBoqun Feng 	/*
699d06957d7SBoqun Feng 	 * During hibernation, when a CPU is offlined, the kernel tries
700d06957d7SBoqun Feng 	 * to move the interrupt to the remaining CPUs that haven't
701d06957d7SBoqun Feng 	 * been offlined yet. In this case, the below hv_do_hypercall()
702d06957d7SBoqun Feng 	 * always fails since the vmbus channel has been closed:
703d06957d7SBoqun Feng 	 * refer to cpu_disable_common() -> fixup_irqs() ->
704d06957d7SBoqun Feng 	 * irq_migrate_all_off_this_cpu() -> migrate_one_irq().
705d06957d7SBoqun Feng 	 *
706d06957d7SBoqun Feng 	 * Suppress the error message for hibernation because the failure
707d06957d7SBoqun Feng 	 * during hibernation does not matter (at this time all the devices
708d06957d7SBoqun Feng 	 * have been frozen). Note: the correct affinity info is still updated
709d06957d7SBoqun Feng 	 * into the irqdata data structure in migrate_one_irq() ->
710503112f4SOlaf Hering 	 * irq_do_set_affinity(), so later when the VM resumes,
711503112f4SOlaf Hering 	 * hv_pci_restore_msi_state() is able to correctly restore the
712503112f4SOlaf Hering 	 * interrupt with the correct affinity.
713d06957d7SBoqun Feng 	 */
714d06957d7SBoqun Feng 	if (!hv_result_success(res) && hbus->state != hv_pcibus_removing)
715d06957d7SBoqun Feng 		dev_err(&hbus->hdev->device,
716d06957d7SBoqun Feng 			"%s() failed: %#llx", __func__, res);
717d06957d7SBoqun Feng }
718d9932b46SSunil Muthuswamy #elif defined(CONFIG_ARM64)
719d9932b46SSunil Muthuswamy /*
720d9932b46SSunil Muthuswamy  * SPI vectors to use for vPCI; arch SPIs range is [32, 1019], but leaving a bit
721d9932b46SSunil Muthuswamy  * of room at the start to allow for SPIs to be specified through ACPI and
722d9932b46SSunil Muthuswamy  * starting with a power of two to satisfy power of 2 multi-MSI requirement.
723d9932b46SSunil Muthuswamy  */
724d9932b46SSunil Muthuswamy #define HV_PCI_MSI_SPI_START	64
725d9932b46SSunil Muthuswamy #define HV_PCI_MSI_SPI_NR	(1020 - HV_PCI_MSI_SPI_START)
726d9932b46SSunil Muthuswamy #define DELIVERY_MODE		0
727d9932b46SSunil Muthuswamy #define FLOW_HANDLER		NULL
728d9932b46SSunil Muthuswamy #define FLOW_NAME		NULL
729d9932b46SSunil Muthuswamy #define hv_msi_prepare		NULL
730d9932b46SSunil Muthuswamy 
731d9932b46SSunil Muthuswamy struct hv_pci_chip_data {
732d9932b46SSunil Muthuswamy 	DECLARE_BITMAP(spi_map, HV_PCI_MSI_SPI_NR);
733d9932b46SSunil Muthuswamy 	struct mutex	map_lock;
734d9932b46SSunil Muthuswamy };
735d9932b46SSunil Muthuswamy 
736d9932b46SSunil Muthuswamy /* Hyper-V vPCI MSI GIC IRQ domain */
737d9932b46SSunil Muthuswamy static struct irq_domain *hv_msi_gic_irq_domain;
738d9932b46SSunil Muthuswamy 
739d9932b46SSunil Muthuswamy /* Hyper-V PCI MSI IRQ chip */
740d9932b46SSunil Muthuswamy static struct irq_chip hv_arm64_msi_irq_chip = {
741d9932b46SSunil Muthuswamy 	.name = "MSI",
742d9932b46SSunil Muthuswamy 	.irq_set_affinity = irq_chip_set_affinity_parent,
743d9932b46SSunil Muthuswamy 	.irq_eoi = irq_chip_eoi_parent,
744d9932b46SSunil Muthuswamy 	.irq_mask = irq_chip_mask_parent,
745d9932b46SSunil Muthuswamy 	.irq_unmask = irq_chip_unmask_parent
746d9932b46SSunil Muthuswamy };
747d9932b46SSunil Muthuswamy 
hv_msi_get_int_vector(struct irq_data * irqd)748d9932b46SSunil Muthuswamy static unsigned int hv_msi_get_int_vector(struct irq_data *irqd)
749d9932b46SSunil Muthuswamy {
750d9932b46SSunil Muthuswamy 	return irqd->parent_data->hwirq;
751d9932b46SSunil Muthuswamy }
752d9932b46SSunil Muthuswamy 
753d9932b46SSunil Muthuswamy /*
754d9932b46SSunil Muthuswamy  * @nr_bm_irqs:		Indicates the number of IRQs that were allocated from
755d9932b46SSunil Muthuswamy  *			the bitmap.
756d9932b46SSunil Muthuswamy  * @nr_dom_irqs:	Indicates the number of IRQs that were allocated from
757d9932b46SSunil Muthuswamy  *			the parent domain.
758d9932b46SSunil Muthuswamy  */
hv_pci_vec_irq_free(struct irq_domain * domain,unsigned int virq,unsigned int nr_bm_irqs,unsigned int nr_dom_irqs)759d9932b46SSunil Muthuswamy static void hv_pci_vec_irq_free(struct irq_domain *domain,
760d9932b46SSunil Muthuswamy 				unsigned int virq,
761d9932b46SSunil Muthuswamy 				unsigned int nr_bm_irqs,
762d9932b46SSunil Muthuswamy 				unsigned int nr_dom_irqs)
763d9932b46SSunil Muthuswamy {
764d9932b46SSunil Muthuswamy 	struct hv_pci_chip_data *chip_data = domain->host_data;
765d9932b46SSunil Muthuswamy 	struct irq_data *d = irq_domain_get_irq_data(domain, virq);
766d9932b46SSunil Muthuswamy 	int first = d->hwirq - HV_PCI_MSI_SPI_START;
767d9932b46SSunil Muthuswamy 	int i;
768d9932b46SSunil Muthuswamy 
769d9932b46SSunil Muthuswamy 	mutex_lock(&chip_data->map_lock);
770d9932b46SSunil Muthuswamy 	bitmap_release_region(chip_data->spi_map,
771d9932b46SSunil Muthuswamy 			      first,
772d9932b46SSunil Muthuswamy 			      get_count_order(nr_bm_irqs));
773d9932b46SSunil Muthuswamy 	mutex_unlock(&chip_data->map_lock);
774d9932b46SSunil Muthuswamy 	for (i = 0; i < nr_dom_irqs; i++) {
775d9932b46SSunil Muthuswamy 		if (i)
776d9932b46SSunil Muthuswamy 			d = irq_domain_get_irq_data(domain, virq + i);
777d9932b46SSunil Muthuswamy 		irq_domain_reset_irq_data(d);
778d9932b46SSunil Muthuswamy 	}
779d9932b46SSunil Muthuswamy 
780d9932b46SSunil Muthuswamy 	irq_domain_free_irqs_parent(domain, virq, nr_dom_irqs);
781d9932b46SSunil Muthuswamy }
782d9932b46SSunil Muthuswamy 
hv_pci_vec_irq_domain_free(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs)783d9932b46SSunil Muthuswamy static void hv_pci_vec_irq_domain_free(struct irq_domain *domain,
784d9932b46SSunil Muthuswamy 				       unsigned int virq,
785d9932b46SSunil Muthuswamy 				       unsigned int nr_irqs)
786d9932b46SSunil Muthuswamy {
787d9932b46SSunil Muthuswamy 	hv_pci_vec_irq_free(domain, virq, nr_irqs, nr_irqs);
788d9932b46SSunil Muthuswamy }
789d9932b46SSunil Muthuswamy 
hv_pci_vec_alloc_device_irq(struct irq_domain * domain,unsigned int nr_irqs,irq_hw_number_t * hwirq)790d9932b46SSunil Muthuswamy static int hv_pci_vec_alloc_device_irq(struct irq_domain *domain,
791d9932b46SSunil Muthuswamy 				       unsigned int nr_irqs,
792d9932b46SSunil Muthuswamy 				       irq_hw_number_t *hwirq)
793d9932b46SSunil Muthuswamy {
794d9932b46SSunil Muthuswamy 	struct hv_pci_chip_data *chip_data = domain->host_data;
795d9932b46SSunil Muthuswamy 	int index;
796d9932b46SSunil Muthuswamy 
797d9932b46SSunil Muthuswamy 	/* Find and allocate region from the SPI bitmap */
798d9932b46SSunil Muthuswamy 	mutex_lock(&chip_data->map_lock);
799d9932b46SSunil Muthuswamy 	index = bitmap_find_free_region(chip_data->spi_map,
800d9932b46SSunil Muthuswamy 					HV_PCI_MSI_SPI_NR,
801d9932b46SSunil Muthuswamy 					get_count_order(nr_irqs));
802d9932b46SSunil Muthuswamy 	mutex_unlock(&chip_data->map_lock);
803d9932b46SSunil Muthuswamy 	if (index < 0)
804d9932b46SSunil Muthuswamy 		return -ENOSPC;
805d9932b46SSunil Muthuswamy 
806d9932b46SSunil Muthuswamy 	*hwirq = index + HV_PCI_MSI_SPI_START;
807d9932b46SSunil Muthuswamy 
808d9932b46SSunil Muthuswamy 	return 0;
809d9932b46SSunil Muthuswamy }
810d9932b46SSunil Muthuswamy 
hv_pci_vec_irq_gic_domain_alloc(struct irq_domain * domain,unsigned int virq,irq_hw_number_t hwirq)811d9932b46SSunil Muthuswamy static int hv_pci_vec_irq_gic_domain_alloc(struct irq_domain *domain,
812d9932b46SSunil Muthuswamy 					   unsigned int virq,
813d9932b46SSunil Muthuswamy 					   irq_hw_number_t hwirq)
814d9932b46SSunil Muthuswamy {
815d9932b46SSunil Muthuswamy 	struct irq_fwspec fwspec;
816d9932b46SSunil Muthuswamy 	struct irq_data *d;
817d9932b46SSunil Muthuswamy 	int ret;
818d9932b46SSunil Muthuswamy 
819d9932b46SSunil Muthuswamy 	fwspec.fwnode = domain->parent->fwnode;
820d9932b46SSunil Muthuswamy 	fwspec.param_count = 2;
821d9932b46SSunil Muthuswamy 	fwspec.param[0] = hwirq;
822d9932b46SSunil Muthuswamy 	fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
823d9932b46SSunil Muthuswamy 
824d9932b46SSunil Muthuswamy 	ret = irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
825d9932b46SSunil Muthuswamy 	if (ret)
826d9932b46SSunil Muthuswamy 		return ret;
827d9932b46SSunil Muthuswamy 
828d9932b46SSunil Muthuswamy 	/*
829d9932b46SSunil Muthuswamy 	 * Since the interrupt specifier is not coming from ACPI or DT, the
830d9932b46SSunil Muthuswamy 	 * trigger type will need to be set explicitly. Otherwise, it will be
831d9932b46SSunil Muthuswamy 	 * set to whatever is in the GIC configuration.
832d9932b46SSunil Muthuswamy 	 */
833d9932b46SSunil Muthuswamy 	d = irq_domain_get_irq_data(domain->parent, virq);
834d9932b46SSunil Muthuswamy 
835d9932b46SSunil Muthuswamy 	return d->chip->irq_set_type(d, IRQ_TYPE_EDGE_RISING);
836d9932b46SSunil Muthuswamy }
837d9932b46SSunil Muthuswamy 
hv_pci_vec_irq_domain_alloc(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs,void * args)838d9932b46SSunil Muthuswamy static int hv_pci_vec_irq_domain_alloc(struct irq_domain *domain,
839d9932b46SSunil Muthuswamy 				       unsigned int virq, unsigned int nr_irqs,
840d9932b46SSunil Muthuswamy 				       void *args)
841d9932b46SSunil Muthuswamy {
842d9932b46SSunil Muthuswamy 	irq_hw_number_t hwirq;
843d9932b46SSunil Muthuswamy 	unsigned int i;
844d9932b46SSunil Muthuswamy 	int ret;
845d9932b46SSunil Muthuswamy 
846d9932b46SSunil Muthuswamy 	ret = hv_pci_vec_alloc_device_irq(domain, nr_irqs, &hwirq);
847d9932b46SSunil Muthuswamy 	if (ret)
848d9932b46SSunil Muthuswamy 		return ret;
849d9932b46SSunil Muthuswamy 
850d9932b46SSunil Muthuswamy 	for (i = 0; i < nr_irqs; i++) {
851d9932b46SSunil Muthuswamy 		ret = hv_pci_vec_irq_gic_domain_alloc(domain, virq + i,
852d9932b46SSunil Muthuswamy 						      hwirq + i);
853d9932b46SSunil Muthuswamy 		if (ret) {
854d9932b46SSunil Muthuswamy 			hv_pci_vec_irq_free(domain, virq, nr_irqs, i);
855d9932b46SSunil Muthuswamy 			return ret;
856d9932b46SSunil Muthuswamy 		}
857d9932b46SSunil Muthuswamy 
858d9932b46SSunil Muthuswamy 		irq_domain_set_hwirq_and_chip(domain, virq + i,
859d9932b46SSunil Muthuswamy 					      hwirq + i,
860d9932b46SSunil Muthuswamy 					      &hv_arm64_msi_irq_chip,
861d9932b46SSunil Muthuswamy 					      domain->host_data);
862d9932b46SSunil Muthuswamy 		pr_debug("pID:%d vID:%u\n", (int)(hwirq + i), virq + i);
863d9932b46SSunil Muthuswamy 	}
864d9932b46SSunil Muthuswamy 
865d9932b46SSunil Muthuswamy 	return 0;
866d9932b46SSunil Muthuswamy }
867d9932b46SSunil Muthuswamy 
868d9932b46SSunil Muthuswamy /*
869d9932b46SSunil Muthuswamy  * Pick the first cpu as the irq affinity that can be temporarily used for
870d9932b46SSunil Muthuswamy  * composing MSI from the hypervisor. GIC will eventually set the right
871d9932b46SSunil Muthuswamy  * affinity for the irq and the 'unmask' will retarget the interrupt to that
872d9932b46SSunil Muthuswamy  * cpu.
873d9932b46SSunil Muthuswamy  */
hv_pci_vec_irq_domain_activate(struct irq_domain * domain,struct irq_data * irqd,bool reserve)874d9932b46SSunil Muthuswamy static int hv_pci_vec_irq_domain_activate(struct irq_domain *domain,
875d9932b46SSunil Muthuswamy 					  struct irq_data *irqd, bool reserve)
876d9932b46SSunil Muthuswamy {
877d9932b46SSunil Muthuswamy 	int cpu = cpumask_first(cpu_present_mask);
878d9932b46SSunil Muthuswamy 
879d9932b46SSunil Muthuswamy 	irq_data_update_effective_affinity(irqd, cpumask_of(cpu));
880d9932b46SSunil Muthuswamy 
881d9932b46SSunil Muthuswamy 	return 0;
882d9932b46SSunil Muthuswamy }
883d9932b46SSunil Muthuswamy 
884d9932b46SSunil Muthuswamy static const struct irq_domain_ops hv_pci_domain_ops = {
885d9932b46SSunil Muthuswamy 	.alloc	= hv_pci_vec_irq_domain_alloc,
886d9932b46SSunil Muthuswamy 	.free	= hv_pci_vec_irq_domain_free,
887d9932b46SSunil Muthuswamy 	.activate = hv_pci_vec_irq_domain_activate,
888d9932b46SSunil Muthuswamy };
889d9932b46SSunil Muthuswamy 
hv_pci_irqchip_init(void)890d9932b46SSunil Muthuswamy static int hv_pci_irqchip_init(void)
891d9932b46SSunil Muthuswamy {
892d9932b46SSunil Muthuswamy 	static struct hv_pci_chip_data *chip_data;
893d9932b46SSunil Muthuswamy 	struct fwnode_handle *fn = NULL;
894d9932b46SSunil Muthuswamy 	int ret = -ENOMEM;
895d9932b46SSunil Muthuswamy 
896d9932b46SSunil Muthuswamy 	chip_data = kzalloc(sizeof(*chip_data), GFP_KERNEL);
897d9932b46SSunil Muthuswamy 	if (!chip_data)
898d9932b46SSunil Muthuswamy 		return ret;
899d9932b46SSunil Muthuswamy 
900d9932b46SSunil Muthuswamy 	mutex_init(&chip_data->map_lock);
901d9932b46SSunil Muthuswamy 	fn = irq_domain_alloc_named_fwnode("hv_vpci_arm64");
902d9932b46SSunil Muthuswamy 	if (!fn)
903d9932b46SSunil Muthuswamy 		goto free_chip;
904d9932b46SSunil Muthuswamy 
905d9932b46SSunil Muthuswamy 	/*
906d9932b46SSunil Muthuswamy 	 * IRQ domain once enabled, should not be removed since there is no
907d9932b46SSunil Muthuswamy 	 * way to ensure that all the corresponding devices are also gone and
908d9932b46SSunil Muthuswamy 	 * no interrupts will be generated.
909d9932b46SSunil Muthuswamy 	 */
910d9932b46SSunil Muthuswamy 	hv_msi_gic_irq_domain = acpi_irq_create_hierarchy(0, HV_PCI_MSI_SPI_NR,
911d9932b46SSunil Muthuswamy 							  fn, &hv_pci_domain_ops,
912d9932b46SSunil Muthuswamy 							  chip_data);
913d9932b46SSunil Muthuswamy 
914d9932b46SSunil Muthuswamy 	if (!hv_msi_gic_irq_domain) {
915d9932b46SSunil Muthuswamy 		pr_err("Failed to create Hyper-V arm64 vPCI MSI IRQ domain\n");
916d9932b46SSunil Muthuswamy 		goto free_chip;
917d9932b46SSunil Muthuswamy 	}
918d9932b46SSunil Muthuswamy 
919d9932b46SSunil Muthuswamy 	return 0;
920d9932b46SSunil Muthuswamy 
921d9932b46SSunil Muthuswamy free_chip:
922d9932b46SSunil Muthuswamy 	kfree(chip_data);
923d9932b46SSunil Muthuswamy 	if (fn)
924d9932b46SSunil Muthuswamy 		irq_domain_free_fwnode(fn);
925d9932b46SSunil Muthuswamy 
926d9932b46SSunil Muthuswamy 	return ret;
927d9932b46SSunil Muthuswamy }
928d9932b46SSunil Muthuswamy 
hv_pci_get_root_domain(void)929d9932b46SSunil Muthuswamy static struct irq_domain *hv_pci_get_root_domain(void)
930d9932b46SSunil Muthuswamy {
931d9932b46SSunil Muthuswamy 	return hv_msi_gic_irq_domain;
932d9932b46SSunil Muthuswamy }
933d06957d7SBoqun Feng 
934d06957d7SBoqun Feng /*
935d06957d7SBoqun Feng  * SPIs are used for interrupts of PCI devices and SPIs is managed via GICD
936d06957d7SBoqun Feng  * registers which Hyper-V already supports, so no hypercall needed.
937d06957d7SBoqun Feng  */
hv_arch_irq_unmask(struct irq_data * data)938d06957d7SBoqun Feng static void hv_arch_irq_unmask(struct irq_data *data) { }
939d9932b46SSunil Muthuswamy #endif /* CONFIG_ARM64 */
940831c1ae7SSunil Muthuswamy 
9416e0832faSShawn Lin /**
9426e0832faSShawn Lin  * hv_pci_generic_compl() - Invoked for a completion packet
9436e0832faSShawn Lin  * @context:		Set up by the sender of the packet.
9446e0832faSShawn Lin  * @resp:		The response packet
9456e0832faSShawn Lin  * @resp_packet_size:	Size in bytes of the packet
9466e0832faSShawn Lin  *
9476e0832faSShawn Lin  * This function is used to trigger an event and report status
9486e0832faSShawn Lin  * for any message for which the completion packet contains a
9496e0832faSShawn Lin  * status and nothing else.
9506e0832faSShawn Lin  */
hv_pci_generic_compl(void * context,struct pci_response * resp,int resp_packet_size)9516e0832faSShawn Lin static void hv_pci_generic_compl(void *context, struct pci_response *resp,
9526e0832faSShawn Lin 				 int resp_packet_size)
9536e0832faSShawn Lin {
9546e0832faSShawn Lin 	struct hv_pci_compl *comp_pkt = context;
9556e0832faSShawn Lin 
9566e0832faSShawn Lin 	comp_pkt->completion_status = resp->status;
9576e0832faSShawn Lin 	complete(&comp_pkt->host_event);
9586e0832faSShawn Lin }
9596e0832faSShawn Lin 
9606e0832faSShawn Lin static struct hv_pci_dev *get_pcichild_wslot(struct hv_pcibus_device *hbus,
9616e0832faSShawn Lin 						u32 wslot);
9626e0832faSShawn Lin 
get_pcichild(struct hv_pci_dev * hpdev)9636e0832faSShawn Lin static void get_pcichild(struct hv_pci_dev *hpdev)
9646e0832faSShawn Lin {
9656e0832faSShawn Lin 	refcount_inc(&hpdev->refs);
9666e0832faSShawn Lin }
9676e0832faSShawn Lin 
put_pcichild(struct hv_pci_dev * hpdev)9686e0832faSShawn Lin static void put_pcichild(struct hv_pci_dev *hpdev)
9696e0832faSShawn Lin {
9706e0832faSShawn Lin 	if (refcount_dec_and_test(&hpdev->refs))
9716e0832faSShawn Lin 		kfree(hpdev);
9726e0832faSShawn Lin }
9736e0832faSShawn Lin 
9746e0832faSShawn Lin /*
9756e0832faSShawn Lin  * There is no good way to get notified from vmbus_onoffer_rescind(),
9766e0832faSShawn Lin  * so let's use polling here, since this is not a hot path.
9776e0832faSShawn Lin  */
wait_for_response(struct hv_device * hdev,struct completion * comp)9786e0832faSShawn Lin static int wait_for_response(struct hv_device *hdev,
9796e0832faSShawn Lin 			     struct completion *comp)
9806e0832faSShawn Lin {
9816e0832faSShawn Lin 	while (true) {
9826e0832faSShawn Lin 		if (hdev->channel->rescind) {
9836e0832faSShawn Lin 			dev_warn_once(&hdev->device, "The device is gone.\n");
9846e0832faSShawn Lin 			return -ENODEV;
9856e0832faSShawn Lin 		}
9866e0832faSShawn Lin 
9876e0832faSShawn Lin 		if (wait_for_completion_timeout(comp, HZ / 10))
9886e0832faSShawn Lin 			break;
9896e0832faSShawn Lin 	}
9906e0832faSShawn Lin 
9916e0832faSShawn Lin 	return 0;
9926e0832faSShawn Lin }
9936e0832faSShawn Lin 
9946e0832faSShawn Lin /**
9956e0832faSShawn Lin  * devfn_to_wslot() - Convert from Linux PCI slot to Windows
9966e0832faSShawn Lin  * @devfn:	The Linux representation of PCI slot
9976e0832faSShawn Lin  *
9986e0832faSShawn Lin  * Windows uses a slightly different representation of PCI slot.
9996e0832faSShawn Lin  *
10006e0832faSShawn Lin  * Return: The Windows representation
10016e0832faSShawn Lin  */
devfn_to_wslot(int devfn)10026e0832faSShawn Lin static u32 devfn_to_wslot(int devfn)
10036e0832faSShawn Lin {
10046e0832faSShawn Lin 	union win_slot_encoding wslot;
10056e0832faSShawn Lin 
10066e0832faSShawn Lin 	wslot.slot = 0;
10076e0832faSShawn Lin 	wslot.bits.dev = PCI_SLOT(devfn);
10086e0832faSShawn Lin 	wslot.bits.func = PCI_FUNC(devfn);
10096e0832faSShawn Lin 
10106e0832faSShawn Lin 	return wslot.slot;
10116e0832faSShawn Lin }
10126e0832faSShawn Lin 
10136e0832faSShawn Lin /**
10146e0832faSShawn Lin  * wslot_to_devfn() - Convert from Windows PCI slot to Linux
10156e0832faSShawn Lin  * @wslot:	The Windows representation of PCI slot
10166e0832faSShawn Lin  *
10176e0832faSShawn Lin  * Windows uses a slightly different representation of PCI slot.
10186e0832faSShawn Lin  *
10196e0832faSShawn Lin  * Return: The Linux representation
10206e0832faSShawn Lin  */
wslot_to_devfn(u32 wslot)10216e0832faSShawn Lin static int wslot_to_devfn(u32 wslot)
10226e0832faSShawn Lin {
10236e0832faSShawn Lin 	union win_slot_encoding slot_no;
10246e0832faSShawn Lin 
10256e0832faSShawn Lin 	slot_no.slot = wslot;
10266e0832faSShawn Lin 	return PCI_DEVFN(slot_no.bits.dev, slot_no.bits.func);
10276e0832faSShawn Lin }
10286e0832faSShawn Lin 
hv_pci_read_mmio(struct device * dev,phys_addr_t gpa,int size,u32 * val)10292c6ba421SMichael Kelley static void hv_pci_read_mmio(struct device *dev, phys_addr_t gpa, int size, u32 *val)
10302c6ba421SMichael Kelley {
10312c6ba421SMichael Kelley 	struct hv_mmio_read_input *in;
10322c6ba421SMichael Kelley 	struct hv_mmio_read_output *out;
10332c6ba421SMichael Kelley 	u64 ret;
10342c6ba421SMichael Kelley 
10352c6ba421SMichael Kelley 	/*
10362c6ba421SMichael Kelley 	 * Must be called with interrupts disabled so it is safe
10372c6ba421SMichael Kelley 	 * to use the per-cpu input argument page.  Use it for
10382c6ba421SMichael Kelley 	 * both input and output.
10392c6ba421SMichael Kelley 	 */
10402c6ba421SMichael Kelley 	in = *this_cpu_ptr(hyperv_pcpu_input_arg);
10412c6ba421SMichael Kelley 	out = *this_cpu_ptr(hyperv_pcpu_input_arg) + sizeof(*in);
10422c6ba421SMichael Kelley 	in->gpa = gpa;
10432c6ba421SMichael Kelley 	in->size = size;
10442c6ba421SMichael Kelley 
10452c6ba421SMichael Kelley 	ret = hv_do_hypercall(HVCALL_MMIO_READ, in, out);
10462c6ba421SMichael Kelley 	if (hv_result_success(ret)) {
10472c6ba421SMichael Kelley 		switch (size) {
10482c6ba421SMichael Kelley 		case 1:
10492c6ba421SMichael Kelley 			*val = *(u8 *)(out->data);
10502c6ba421SMichael Kelley 			break;
10512c6ba421SMichael Kelley 		case 2:
10522c6ba421SMichael Kelley 			*val = *(u16 *)(out->data);
10532c6ba421SMichael Kelley 			break;
10542c6ba421SMichael Kelley 		default:
10552c6ba421SMichael Kelley 			*val = *(u32 *)(out->data);
10562c6ba421SMichael Kelley 			break;
10572c6ba421SMichael Kelley 		}
10582c6ba421SMichael Kelley 	} else
10592c6ba421SMichael Kelley 		dev_err(dev, "MMIO read hypercall error %llx addr %llx size %d\n",
10602c6ba421SMichael Kelley 				ret, gpa, size);
10612c6ba421SMichael Kelley }
10622c6ba421SMichael Kelley 
hv_pci_write_mmio(struct device * dev,phys_addr_t gpa,int size,u32 val)10632c6ba421SMichael Kelley static void hv_pci_write_mmio(struct device *dev, phys_addr_t gpa, int size, u32 val)
10642c6ba421SMichael Kelley {
10652c6ba421SMichael Kelley 	struct hv_mmio_write_input *in;
10662c6ba421SMichael Kelley 	u64 ret;
10672c6ba421SMichael Kelley 
10682c6ba421SMichael Kelley 	/*
10692c6ba421SMichael Kelley 	 * Must be called with interrupts disabled so it is safe
10702c6ba421SMichael Kelley 	 * to use the per-cpu input argument memory.
10712c6ba421SMichael Kelley 	 */
10722c6ba421SMichael Kelley 	in = *this_cpu_ptr(hyperv_pcpu_input_arg);
10732c6ba421SMichael Kelley 	in->gpa = gpa;
10742c6ba421SMichael Kelley 	in->size = size;
10752c6ba421SMichael Kelley 	switch (size) {
10762c6ba421SMichael Kelley 	case 1:
10772c6ba421SMichael Kelley 		*(u8 *)(in->data) = val;
10782c6ba421SMichael Kelley 		break;
10792c6ba421SMichael Kelley 	case 2:
10802c6ba421SMichael Kelley 		*(u16 *)(in->data) = val;
10812c6ba421SMichael Kelley 		break;
10822c6ba421SMichael Kelley 	default:
10832c6ba421SMichael Kelley 		*(u32 *)(in->data) = val;
10842c6ba421SMichael Kelley 		break;
10852c6ba421SMichael Kelley 	}
10862c6ba421SMichael Kelley 
10872c6ba421SMichael Kelley 	ret = hv_do_hypercall(HVCALL_MMIO_WRITE, in, NULL);
10882c6ba421SMichael Kelley 	if (!hv_result_success(ret))
10892c6ba421SMichael Kelley 		dev_err(dev, "MMIO write hypercall error %llx addr %llx size %d\n",
10902c6ba421SMichael Kelley 				ret, gpa, size);
10912c6ba421SMichael Kelley }
10922c6ba421SMichael Kelley 
10936e0832faSShawn Lin /*
10946e0832faSShawn Lin  * PCI Configuration Space for these root PCI buses is implemented as a pair
10956e0832faSShawn Lin  * of pages in memory-mapped I/O space.  Writing to the first page chooses
10966e0832faSShawn Lin  * the PCI function being written or read.  Once the first page has been
10976e0832faSShawn Lin  * written to, the following page maps in the entire configuration space of
10986e0832faSShawn Lin  * the function.
10996e0832faSShawn Lin  */
11006e0832faSShawn Lin 
11016e0832faSShawn Lin /**
11026e0832faSShawn Lin  * _hv_pcifront_read_config() - Internal PCI config read
11036e0832faSShawn Lin  * @hpdev:	The PCI driver's representation of the device
11046e0832faSShawn Lin  * @where:	Offset within config space
11056e0832faSShawn Lin  * @size:	Size of the transfer
11066e0832faSShawn Lin  * @val:	Pointer to the buffer receiving the data
11076e0832faSShawn Lin  */
_hv_pcifront_read_config(struct hv_pci_dev * hpdev,int where,int size,u32 * val)11086e0832faSShawn Lin static void _hv_pcifront_read_config(struct hv_pci_dev *hpdev, int where,
11096e0832faSShawn Lin 				     int size, u32 *val)
11106e0832faSShawn Lin {
11112c6ba421SMichael Kelley 	struct hv_pcibus_device *hbus = hpdev->hbus;
11122c6ba421SMichael Kelley 	struct device *dev = &hbus->hdev->device;
11132c6ba421SMichael Kelley 	int offset = where + CFG_PAGE_OFFSET;
11146e0832faSShawn Lin 	unsigned long flags;
11156e0832faSShawn Lin 
11166e0832faSShawn Lin 	/*
11176e0832faSShawn Lin 	 * If the attempt is to read the IDs or the ROM BAR, simulate that.
11186e0832faSShawn Lin 	 */
11196e0832faSShawn Lin 	if (where + size <= PCI_COMMAND) {
11206e0832faSShawn Lin 		memcpy(val, ((u8 *)&hpdev->desc.v_id) + where, size);
11216e0832faSShawn Lin 	} else if (where >= PCI_CLASS_REVISION && where + size <=
11226e0832faSShawn Lin 		   PCI_CACHE_LINE_SIZE) {
11236e0832faSShawn Lin 		memcpy(val, ((u8 *)&hpdev->desc.rev) + where -
11246e0832faSShawn Lin 		       PCI_CLASS_REVISION, size);
11256e0832faSShawn Lin 	} else if (where >= PCI_SUBSYSTEM_VENDOR_ID && where + size <=
11266e0832faSShawn Lin 		   PCI_ROM_ADDRESS) {
11276e0832faSShawn Lin 		memcpy(val, (u8 *)&hpdev->desc.subsystem_id + where -
11286e0832faSShawn Lin 		       PCI_SUBSYSTEM_VENDOR_ID, size);
11296e0832faSShawn Lin 	} else if (where >= PCI_ROM_ADDRESS && where + size <=
11306e0832faSShawn Lin 		   PCI_CAPABILITY_LIST) {
11316e0832faSShawn Lin 		/* ROM BARs are unimplemented */
11326e0832faSShawn Lin 		*val = 0;
1133*fea93a3eSWei Liu 	} else if ((where >= PCI_INTERRUPT_LINE && where + size <= PCI_INTERRUPT_PIN) ||
1134*fea93a3eSWei Liu 		   (where >= PCI_INTERRUPT_PIN && where + size <= PCI_MIN_GNT)) {
11356e0832faSShawn Lin 		/*
11366e0832faSShawn Lin 		 * Interrupt Line and Interrupt PIN are hard-wired to zero
11376e0832faSShawn Lin 		 * because this front-end only supports message-signaled
11386e0832faSShawn Lin 		 * interrupts.
11396e0832faSShawn Lin 		 */
11406e0832faSShawn Lin 		*val = 0;
11416e0832faSShawn Lin 	} else if (where + size <= CFG_PAGE_SIZE) {
11422c6ba421SMichael Kelley 
11432c6ba421SMichael Kelley 		spin_lock_irqsave(&hbus->config_lock, flags);
11442c6ba421SMichael Kelley 		if (hbus->use_calls) {
11452c6ba421SMichael Kelley 			phys_addr_t addr = hbus->mem_config->start + offset;
11462c6ba421SMichael Kelley 
11472c6ba421SMichael Kelley 			hv_pci_write_mmio(dev, hbus->mem_config->start, 4,
11482c6ba421SMichael Kelley 						hpdev->desc.win_slot.slot);
11492c6ba421SMichael Kelley 			hv_pci_read_mmio(dev, addr, size, val);
11502c6ba421SMichael Kelley 		} else {
11512c6ba421SMichael Kelley 			void __iomem *addr = hbus->cfg_addr + offset;
11522c6ba421SMichael Kelley 
11536e0832faSShawn Lin 			/* Choose the function to be read. (See comment above) */
11542c6ba421SMichael Kelley 			writel(hpdev->desc.win_slot.slot, hbus->cfg_addr);
11552c6ba421SMichael Kelley 			/* Make sure the function was chosen before reading. */
11566e0832faSShawn Lin 			mb();
11576e0832faSShawn Lin 			/* Read from that function's config space. */
11586e0832faSShawn Lin 			switch (size) {
11596e0832faSShawn Lin 			case 1:
11606e0832faSShawn Lin 				*val = readb(addr);
11616e0832faSShawn Lin 				break;
11626e0832faSShawn Lin 			case 2:
11636e0832faSShawn Lin 				*val = readw(addr);
11646e0832faSShawn Lin 				break;
11656e0832faSShawn Lin 			default:
11666e0832faSShawn Lin 				*val = readl(addr);
11676e0832faSShawn Lin 				break;
11686e0832faSShawn Lin 			}
11696e0832faSShawn Lin 			/*
11702c6ba421SMichael Kelley 			 * Make sure the read was done before we release the
11712c6ba421SMichael Kelley 			 * spinlock allowing consecutive reads/writes.
11726e0832faSShawn Lin 			 */
11736e0832faSShawn Lin 			mb();
11742c6ba421SMichael Kelley 		}
11752c6ba421SMichael Kelley 		spin_unlock_irqrestore(&hbus->config_lock, flags);
11766e0832faSShawn Lin 	} else {
11772c6ba421SMichael Kelley 		dev_err(dev, "Attempt to read beyond a function's config space.\n");
11786e0832faSShawn Lin 	}
11796e0832faSShawn Lin }
11806e0832faSShawn Lin 
hv_pcifront_get_vendor_id(struct hv_pci_dev * hpdev)11816e0832faSShawn Lin static u16 hv_pcifront_get_vendor_id(struct hv_pci_dev *hpdev)
11826e0832faSShawn Lin {
11832c6ba421SMichael Kelley 	struct hv_pcibus_device *hbus = hpdev->hbus;
11842c6ba421SMichael Kelley 	struct device *dev = &hbus->hdev->device;
11852c6ba421SMichael Kelley 	u32 val;
11866e0832faSShawn Lin 	u16 ret;
11876e0832faSShawn Lin 	unsigned long flags;
11882c6ba421SMichael Kelley 
11892c6ba421SMichael Kelley 	spin_lock_irqsave(&hbus->config_lock, flags);
11902c6ba421SMichael Kelley 
11912c6ba421SMichael Kelley 	if (hbus->use_calls) {
11922c6ba421SMichael Kelley 		phys_addr_t addr = hbus->mem_config->start +
11932c6ba421SMichael Kelley 					 CFG_PAGE_OFFSET + PCI_VENDOR_ID;
11942c6ba421SMichael Kelley 
11952c6ba421SMichael Kelley 		hv_pci_write_mmio(dev, hbus->mem_config->start, 4,
11962c6ba421SMichael Kelley 					hpdev->desc.win_slot.slot);
11972c6ba421SMichael Kelley 		hv_pci_read_mmio(dev, addr, 2, &val);
11982c6ba421SMichael Kelley 		ret = val;  /* Truncates to 16 bits */
11992c6ba421SMichael Kelley 	} else {
12002c6ba421SMichael Kelley 		void __iomem *addr = hbus->cfg_addr + CFG_PAGE_OFFSET +
12016e0832faSShawn Lin 					     PCI_VENDOR_ID;
12026e0832faSShawn Lin 		/* Choose the function to be read. (See comment above) */
12032c6ba421SMichael Kelley 		writel(hpdev->desc.win_slot.slot, hbus->cfg_addr);
12046e0832faSShawn Lin 		/* Make sure the function was chosen before we start reading. */
12056e0832faSShawn Lin 		mb();
12066e0832faSShawn Lin 		/* Read from that function's config space. */
12076e0832faSShawn Lin 		ret = readw(addr);
12086e0832faSShawn Lin 		/*
12092c6ba421SMichael Kelley 		 * mb() is not required here, because the
12102c6ba421SMichael Kelley 		 * spin_unlock_irqrestore() is a barrier.
12116e0832faSShawn Lin 		 */
12122c6ba421SMichael Kelley 	}
12136e0832faSShawn Lin 
12142c6ba421SMichael Kelley 	spin_unlock_irqrestore(&hbus->config_lock, flags);
12156e0832faSShawn Lin 
12166e0832faSShawn Lin 	return ret;
12176e0832faSShawn Lin }
12186e0832faSShawn Lin 
12196e0832faSShawn Lin /**
12206e0832faSShawn Lin  * _hv_pcifront_write_config() - Internal PCI config write
12216e0832faSShawn Lin  * @hpdev:	The PCI driver's representation of the device
12226e0832faSShawn Lin  * @where:	Offset within config space
12236e0832faSShawn Lin  * @size:	Size of the transfer
12246e0832faSShawn Lin  * @val:	The data being transferred
12256e0832faSShawn Lin  */
_hv_pcifront_write_config(struct hv_pci_dev * hpdev,int where,int size,u32 val)12266e0832faSShawn Lin static void _hv_pcifront_write_config(struct hv_pci_dev *hpdev, int where,
12276e0832faSShawn Lin 				      int size, u32 val)
12286e0832faSShawn Lin {
12292c6ba421SMichael Kelley 	struct hv_pcibus_device *hbus = hpdev->hbus;
12302c6ba421SMichael Kelley 	struct device *dev = &hbus->hdev->device;
12312c6ba421SMichael Kelley 	int offset = where + CFG_PAGE_OFFSET;
12326e0832faSShawn Lin 	unsigned long flags;
12336e0832faSShawn Lin 
12346e0832faSShawn Lin 	if (where >= PCI_SUBSYSTEM_VENDOR_ID &&
12356e0832faSShawn Lin 	    where + size <= PCI_CAPABILITY_LIST) {
12366e0832faSShawn Lin 		/* SSIDs and ROM BARs are read-only */
12376e0832faSShawn Lin 	} else if (where >= PCI_COMMAND && where + size <= CFG_PAGE_SIZE) {
12382c6ba421SMichael Kelley 		spin_lock_irqsave(&hbus->config_lock, flags);
12392c6ba421SMichael Kelley 
12402c6ba421SMichael Kelley 		if (hbus->use_calls) {
12412c6ba421SMichael Kelley 			phys_addr_t addr = hbus->mem_config->start + offset;
12422c6ba421SMichael Kelley 
12432c6ba421SMichael Kelley 			hv_pci_write_mmio(dev, hbus->mem_config->start, 4,
12442c6ba421SMichael Kelley 						hpdev->desc.win_slot.slot);
12452c6ba421SMichael Kelley 			hv_pci_write_mmio(dev, addr, size, val);
12462c6ba421SMichael Kelley 		} else {
12472c6ba421SMichael Kelley 			void __iomem *addr = hbus->cfg_addr + offset;
12482c6ba421SMichael Kelley 
12492c6ba421SMichael Kelley 			/* Choose the function to write. (See comment above) */
12502c6ba421SMichael Kelley 			writel(hpdev->desc.win_slot.slot, hbus->cfg_addr);
12512c6ba421SMichael Kelley 			/* Make sure the function was chosen before writing. */
12526e0832faSShawn Lin 			wmb();
12536e0832faSShawn Lin 			/* Write to that function's config space. */
12546e0832faSShawn Lin 			switch (size) {
12556e0832faSShawn Lin 			case 1:
12566e0832faSShawn Lin 				writeb(val, addr);
12576e0832faSShawn Lin 				break;
12586e0832faSShawn Lin 			case 2:
12596e0832faSShawn Lin 				writew(val, addr);
12606e0832faSShawn Lin 				break;
12616e0832faSShawn Lin 			default:
12626e0832faSShawn Lin 				writel(val, addr);
12636e0832faSShawn Lin 				break;
12646e0832faSShawn Lin 			}
12656e0832faSShawn Lin 			/*
12662c6ba421SMichael Kelley 			 * Make sure the write was done before we release the
12672c6ba421SMichael Kelley 			 * spinlock allowing consecutive reads/writes.
12686e0832faSShawn Lin 			 */
12696e0832faSShawn Lin 			mb();
12702c6ba421SMichael Kelley 		}
12712c6ba421SMichael Kelley 		spin_unlock_irqrestore(&hbus->config_lock, flags);
12726e0832faSShawn Lin 	} else {
12732c6ba421SMichael Kelley 		dev_err(dev, "Attempt to write beyond a function's config space.\n");
12746e0832faSShawn Lin 	}
12756e0832faSShawn Lin }
12766e0832faSShawn Lin 
12776e0832faSShawn Lin /**
12786e0832faSShawn Lin  * hv_pcifront_read_config() - Read configuration space
12796e0832faSShawn Lin  * @bus: PCI Bus structure
12806e0832faSShawn Lin  * @devfn: Device/function
12816e0832faSShawn Lin  * @where: Offset from base
12826e0832faSShawn Lin  * @size: Byte/word/dword
12836e0832faSShawn Lin  * @val: Value to be read
12846e0832faSShawn Lin  *
12856e0832faSShawn Lin  * Return: PCIBIOS_SUCCESSFUL on success
12866e0832faSShawn Lin  *	   PCIBIOS_DEVICE_NOT_FOUND on failure
12876e0832faSShawn Lin  */
hv_pcifront_read_config(struct pci_bus * bus,unsigned int devfn,int where,int size,u32 * val)12886e0832faSShawn Lin static int hv_pcifront_read_config(struct pci_bus *bus, unsigned int devfn,
12896e0832faSShawn Lin 				   int where, int size, u32 *val)
12906e0832faSShawn Lin {
12916e0832faSShawn Lin 	struct hv_pcibus_device *hbus =
12926e0832faSShawn Lin 		container_of(bus->sysdata, struct hv_pcibus_device, sysdata);
12936e0832faSShawn Lin 	struct hv_pci_dev *hpdev;
12946e0832faSShawn Lin 
12956e0832faSShawn Lin 	hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(devfn));
12966e0832faSShawn Lin 	if (!hpdev)
12976e0832faSShawn Lin 		return PCIBIOS_DEVICE_NOT_FOUND;
12986e0832faSShawn Lin 
12996e0832faSShawn Lin 	_hv_pcifront_read_config(hpdev, where, size, val);
13006e0832faSShawn Lin 
13016e0832faSShawn Lin 	put_pcichild(hpdev);
13026e0832faSShawn Lin 	return PCIBIOS_SUCCESSFUL;
13036e0832faSShawn Lin }
13046e0832faSShawn Lin 
13056e0832faSShawn Lin /**
13066e0832faSShawn Lin  * hv_pcifront_write_config() - Write configuration space
13076e0832faSShawn Lin  * @bus: PCI Bus structure
13086e0832faSShawn Lin  * @devfn: Device/function
13096e0832faSShawn Lin  * @where: Offset from base
13106e0832faSShawn Lin  * @size: Byte/word/dword
13116e0832faSShawn Lin  * @val: Value to be written to device
13126e0832faSShawn Lin  *
13136e0832faSShawn Lin  * Return: PCIBIOS_SUCCESSFUL on success
13146e0832faSShawn Lin  *	   PCIBIOS_DEVICE_NOT_FOUND on failure
13156e0832faSShawn Lin  */
hv_pcifront_write_config(struct pci_bus * bus,unsigned int devfn,int where,int size,u32 val)13166e0832faSShawn Lin static int hv_pcifront_write_config(struct pci_bus *bus, unsigned int devfn,
13176e0832faSShawn Lin 				    int where, int size, u32 val)
13186e0832faSShawn Lin {
13196e0832faSShawn Lin 	struct hv_pcibus_device *hbus =
13206e0832faSShawn Lin 	    container_of(bus->sysdata, struct hv_pcibus_device, sysdata);
13216e0832faSShawn Lin 	struct hv_pci_dev *hpdev;
13226e0832faSShawn Lin 
13236e0832faSShawn Lin 	hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(devfn));
13246e0832faSShawn Lin 	if (!hpdev)
13256e0832faSShawn Lin 		return PCIBIOS_DEVICE_NOT_FOUND;
13266e0832faSShawn Lin 
13276e0832faSShawn Lin 	_hv_pcifront_write_config(hpdev, where, size, val);
13286e0832faSShawn Lin 
13296e0832faSShawn Lin 	put_pcichild(hpdev);
13306e0832faSShawn Lin 	return PCIBIOS_SUCCESSFUL;
13316e0832faSShawn Lin }
13326e0832faSShawn Lin 
13336e0832faSShawn Lin /* PCIe operations */
13346e0832faSShawn Lin static struct pci_ops hv_pcifront_ops = {
13356e0832faSShawn Lin 	.read  = hv_pcifront_read_config,
13366e0832faSShawn Lin 	.write = hv_pcifront_write_config,
13376e0832faSShawn Lin };
13386e0832faSShawn Lin 
1339e5d2f910SDexuan Cui /*
1340e5d2f910SDexuan Cui  * Paravirtual backchannel
1341e5d2f910SDexuan Cui  *
1342e5d2f910SDexuan Cui  * Hyper-V SR-IOV provides a backchannel mechanism in software for
1343e5d2f910SDexuan Cui  * communication between a VF driver and a PF driver.  These
1344e5d2f910SDexuan Cui  * "configuration blocks" are similar in concept to PCI configuration space,
1345e5d2f910SDexuan Cui  * but instead of doing reads and writes in 32-bit chunks through a very slow
1346e5d2f910SDexuan Cui  * path, packets of up to 128 bytes can be sent or received asynchronously.
1347e5d2f910SDexuan Cui  *
1348e5d2f910SDexuan Cui  * Nearly every SR-IOV device contains just such a communications channel in
1349e5d2f910SDexuan Cui  * hardware, so using this one in software is usually optional.  Using the
1350e5d2f910SDexuan Cui  * software channel, however, allows driver implementers to leverage software
1351e5d2f910SDexuan Cui  * tools that fuzz the communications channel looking for vulnerabilities.
1352e5d2f910SDexuan Cui  *
1353e5d2f910SDexuan Cui  * The usage model for these packets puts the responsibility for reading or
1354e5d2f910SDexuan Cui  * writing on the VF driver.  The VF driver sends a read or a write packet,
1355e5d2f910SDexuan Cui  * indicating which "block" is being referred to by number.
1356e5d2f910SDexuan Cui  *
1357e5d2f910SDexuan Cui  * If the PF driver wishes to initiate communication, it can "invalidate" one or
1358e5d2f910SDexuan Cui  * more of the first 64 blocks.  This invalidation is delivered via a callback
1359e5d2f910SDexuan Cui  * supplied by the VF driver by this driver.
1360e5d2f910SDexuan Cui  *
1361e5d2f910SDexuan Cui  * No protocol is implied, except that supplied by the PF and VF drivers.
1362e5d2f910SDexuan Cui  */
1363e5d2f910SDexuan Cui 
1364e5d2f910SDexuan Cui struct hv_read_config_compl {
1365e5d2f910SDexuan Cui 	struct hv_pci_compl comp_pkt;
1366e5d2f910SDexuan Cui 	void *buf;
1367e5d2f910SDexuan Cui 	unsigned int len;
1368e5d2f910SDexuan Cui 	unsigned int bytes_returned;
1369e5d2f910SDexuan Cui };
1370e5d2f910SDexuan Cui 
1371e5d2f910SDexuan Cui /**
1372e5d2f910SDexuan Cui  * hv_pci_read_config_compl() - Invoked when a response packet
1373e5d2f910SDexuan Cui  * for a read config block operation arrives.
1374e5d2f910SDexuan Cui  * @context:		Identifies the read config operation
1375e5d2f910SDexuan Cui  * @resp:		The response packet itself
1376e5d2f910SDexuan Cui  * @resp_packet_size:	Size in bytes of the response packet
1377e5d2f910SDexuan Cui  */
hv_pci_read_config_compl(void * context,struct pci_response * resp,int resp_packet_size)1378e5d2f910SDexuan Cui static void hv_pci_read_config_compl(void *context, struct pci_response *resp,
1379e5d2f910SDexuan Cui 				     int resp_packet_size)
1380e5d2f910SDexuan Cui {
1381e5d2f910SDexuan Cui 	struct hv_read_config_compl *comp = context;
1382e5d2f910SDexuan Cui 	struct pci_read_block_response *read_resp =
1383e5d2f910SDexuan Cui 		(struct pci_read_block_response *)resp;
1384e5d2f910SDexuan Cui 	unsigned int data_len, hdr_len;
1385e5d2f910SDexuan Cui 
1386e5d2f910SDexuan Cui 	hdr_len = offsetof(struct pci_read_block_response, bytes);
1387e5d2f910SDexuan Cui 	if (resp_packet_size < hdr_len) {
1388e5d2f910SDexuan Cui 		comp->comp_pkt.completion_status = -1;
1389e5d2f910SDexuan Cui 		goto out;
1390e5d2f910SDexuan Cui 	}
1391e5d2f910SDexuan Cui 
1392e5d2f910SDexuan Cui 	data_len = resp_packet_size - hdr_len;
1393e5d2f910SDexuan Cui 	if (data_len > 0 && read_resp->status == 0) {
1394e5d2f910SDexuan Cui 		comp->bytes_returned = min(comp->len, data_len);
1395e5d2f910SDexuan Cui 		memcpy(comp->buf, read_resp->bytes, comp->bytes_returned);
1396e5d2f910SDexuan Cui 	} else {
1397e5d2f910SDexuan Cui 		comp->bytes_returned = 0;
1398e5d2f910SDexuan Cui 	}
1399e5d2f910SDexuan Cui 
1400e5d2f910SDexuan Cui 	comp->comp_pkt.completion_status = read_resp->status;
1401e5d2f910SDexuan Cui out:
1402e5d2f910SDexuan Cui 	complete(&comp->comp_pkt.host_event);
1403e5d2f910SDexuan Cui }
1404e5d2f910SDexuan Cui 
1405e5d2f910SDexuan Cui /**
1406e5d2f910SDexuan Cui  * hv_read_config_block() - Sends a read config block request to
1407e5d2f910SDexuan Cui  * the back-end driver running in the Hyper-V parent partition.
1408e5d2f910SDexuan Cui  * @pdev:		The PCI driver's representation for this device.
1409e5d2f910SDexuan Cui  * @buf:		Buffer into which the config block will be copied.
1410e5d2f910SDexuan Cui  * @len:		Size in bytes of buf.
1411e5d2f910SDexuan Cui  * @block_id:		Identifies the config block which has been requested.
1412e5d2f910SDexuan Cui  * @bytes_returned:	Size which came back from the back-end driver.
1413e5d2f910SDexuan Cui  *
1414e5d2f910SDexuan Cui  * Return: 0 on success, -errno on failure
1415e5d2f910SDexuan Cui  */
hv_read_config_block(struct pci_dev * pdev,void * buf,unsigned int len,unsigned int block_id,unsigned int * bytes_returned)1416a459d9e1SWei Yongjun static int hv_read_config_block(struct pci_dev *pdev, void *buf,
1417a459d9e1SWei Yongjun 				unsigned int len, unsigned int block_id,
1418a459d9e1SWei Yongjun 				unsigned int *bytes_returned)
1419e5d2f910SDexuan Cui {
1420e5d2f910SDexuan Cui 	struct hv_pcibus_device *hbus =
1421e5d2f910SDexuan Cui 		container_of(pdev->bus->sysdata, struct hv_pcibus_device,
1422e5d2f910SDexuan Cui 			     sysdata);
1423e5d2f910SDexuan Cui 	struct {
1424e5d2f910SDexuan Cui 		struct pci_packet pkt;
1425e5d2f910SDexuan Cui 		char buf[sizeof(struct pci_read_block)];
1426e5d2f910SDexuan Cui 	} pkt;
1427e5d2f910SDexuan Cui 	struct hv_read_config_compl comp_pkt;
1428e5d2f910SDexuan Cui 	struct pci_read_block *read_blk;
1429e5d2f910SDexuan Cui 	int ret;
1430e5d2f910SDexuan Cui 
1431e5d2f910SDexuan Cui 	if (len == 0 || len > HV_CONFIG_BLOCK_SIZE_MAX)
1432e5d2f910SDexuan Cui 		return -EINVAL;
1433e5d2f910SDexuan Cui 
1434e5d2f910SDexuan Cui 	init_completion(&comp_pkt.comp_pkt.host_event);
1435e5d2f910SDexuan Cui 	comp_pkt.buf = buf;
1436e5d2f910SDexuan Cui 	comp_pkt.len = len;
1437e5d2f910SDexuan Cui 
1438e5d2f910SDexuan Cui 	memset(&pkt, 0, sizeof(pkt));
1439e5d2f910SDexuan Cui 	pkt.pkt.completion_func = hv_pci_read_config_compl;
1440e5d2f910SDexuan Cui 	pkt.pkt.compl_ctxt = &comp_pkt;
1441e5d2f910SDexuan Cui 	read_blk = (struct pci_read_block *)&pkt.pkt.message;
1442e5d2f910SDexuan Cui 	read_blk->message_type.type = PCI_READ_BLOCK;
1443e5d2f910SDexuan Cui 	read_blk->wslot.slot = devfn_to_wslot(pdev->devfn);
1444e5d2f910SDexuan Cui 	read_blk->block_id = block_id;
1445e5d2f910SDexuan Cui 	read_blk->bytes_requested = len;
1446e5d2f910SDexuan Cui 
1447e5d2f910SDexuan Cui 	ret = vmbus_sendpacket(hbus->hdev->channel, read_blk,
1448e5d2f910SDexuan Cui 			       sizeof(*read_blk), (unsigned long)&pkt.pkt,
1449e5d2f910SDexuan Cui 			       VM_PKT_DATA_INBAND,
1450e5d2f910SDexuan Cui 			       VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1451e5d2f910SDexuan Cui 	if (ret)
1452e5d2f910SDexuan Cui 		return ret;
1453e5d2f910SDexuan Cui 
1454e5d2f910SDexuan Cui 	ret = wait_for_response(hbus->hdev, &comp_pkt.comp_pkt.host_event);
1455e5d2f910SDexuan Cui 	if (ret)
1456e5d2f910SDexuan Cui 		return ret;
1457e5d2f910SDexuan Cui 
1458e5d2f910SDexuan Cui 	if (comp_pkt.comp_pkt.completion_status != 0 ||
1459e5d2f910SDexuan Cui 	    comp_pkt.bytes_returned == 0) {
1460e5d2f910SDexuan Cui 		dev_err(&hbus->hdev->device,
1461e5d2f910SDexuan Cui 			"Read Config Block failed: 0x%x, bytes_returned=%d\n",
1462e5d2f910SDexuan Cui 			comp_pkt.comp_pkt.completion_status,
1463e5d2f910SDexuan Cui 			comp_pkt.bytes_returned);
1464e5d2f910SDexuan Cui 		return -EIO;
1465e5d2f910SDexuan Cui 	}
1466e5d2f910SDexuan Cui 
1467e5d2f910SDexuan Cui 	*bytes_returned = comp_pkt.bytes_returned;
1468e5d2f910SDexuan Cui 	return 0;
1469e5d2f910SDexuan Cui }
1470e5d2f910SDexuan Cui 
1471e5d2f910SDexuan Cui /**
1472e5d2f910SDexuan Cui  * hv_pci_write_config_compl() - Invoked when a response packet for a write
1473e5d2f910SDexuan Cui  * config block operation arrives.
1474e5d2f910SDexuan Cui  * @context:		Identifies the write config operation
1475e5d2f910SDexuan Cui  * @resp:		The response packet itself
1476e5d2f910SDexuan Cui  * @resp_packet_size:	Size in bytes of the response packet
1477e5d2f910SDexuan Cui  */
hv_pci_write_config_compl(void * context,struct pci_response * resp,int resp_packet_size)1478e5d2f910SDexuan Cui static void hv_pci_write_config_compl(void *context, struct pci_response *resp,
1479e5d2f910SDexuan Cui 				      int resp_packet_size)
1480e5d2f910SDexuan Cui {
1481e5d2f910SDexuan Cui 	struct hv_pci_compl *comp_pkt = context;
1482e5d2f910SDexuan Cui 
1483e5d2f910SDexuan Cui 	comp_pkt->completion_status = resp->status;
1484e5d2f910SDexuan Cui 	complete(&comp_pkt->host_event);
1485e5d2f910SDexuan Cui }
1486e5d2f910SDexuan Cui 
1487e5d2f910SDexuan Cui /**
1488e5d2f910SDexuan Cui  * hv_write_config_block() - Sends a write config block request to the
1489e5d2f910SDexuan Cui  * back-end driver running in the Hyper-V parent partition.
1490e5d2f910SDexuan Cui  * @pdev:		The PCI driver's representation for this device.
1491e5d2f910SDexuan Cui  * @buf:		Buffer from which the config block will	be copied.
1492e5d2f910SDexuan Cui  * @len:		Size in bytes of buf.
1493e5d2f910SDexuan Cui  * @block_id:		Identifies the config block which is being written.
1494e5d2f910SDexuan Cui  *
1495e5d2f910SDexuan Cui  * Return: 0 on success, -errno on failure
1496e5d2f910SDexuan Cui  */
hv_write_config_block(struct pci_dev * pdev,void * buf,unsigned int len,unsigned int block_id)1497a459d9e1SWei Yongjun static int hv_write_config_block(struct pci_dev *pdev, void *buf,
1498a459d9e1SWei Yongjun 				unsigned int len, unsigned int block_id)
1499e5d2f910SDexuan Cui {
1500e5d2f910SDexuan Cui 	struct hv_pcibus_device *hbus =
1501e5d2f910SDexuan Cui 		container_of(pdev->bus->sysdata, struct hv_pcibus_device,
1502e5d2f910SDexuan Cui 			     sysdata);
1503e5d2f910SDexuan Cui 	struct {
1504e5d2f910SDexuan Cui 		struct pci_packet pkt;
1505e5d2f910SDexuan Cui 		char buf[sizeof(struct pci_write_block)];
1506e5d2f910SDexuan Cui 		u32 reserved;
1507e5d2f910SDexuan Cui 	} pkt;
1508e5d2f910SDexuan Cui 	struct hv_pci_compl comp_pkt;
1509e5d2f910SDexuan Cui 	struct pci_write_block *write_blk;
1510e5d2f910SDexuan Cui 	u32 pkt_size;
1511e5d2f910SDexuan Cui 	int ret;
1512e5d2f910SDexuan Cui 
1513e5d2f910SDexuan Cui 	if (len == 0 || len > HV_CONFIG_BLOCK_SIZE_MAX)
1514e5d2f910SDexuan Cui 		return -EINVAL;
1515e5d2f910SDexuan Cui 
1516e5d2f910SDexuan Cui 	init_completion(&comp_pkt.host_event);
1517e5d2f910SDexuan Cui 
1518e5d2f910SDexuan Cui 	memset(&pkt, 0, sizeof(pkt));
1519e5d2f910SDexuan Cui 	pkt.pkt.completion_func = hv_pci_write_config_compl;
1520e5d2f910SDexuan Cui 	pkt.pkt.compl_ctxt = &comp_pkt;
1521e5d2f910SDexuan Cui 	write_blk = (struct pci_write_block *)&pkt.pkt.message;
1522e5d2f910SDexuan Cui 	write_blk->message_type.type = PCI_WRITE_BLOCK;
1523e5d2f910SDexuan Cui 	write_blk->wslot.slot = devfn_to_wslot(pdev->devfn);
1524e5d2f910SDexuan Cui 	write_blk->block_id = block_id;
1525e5d2f910SDexuan Cui 	write_blk->byte_count = len;
1526e5d2f910SDexuan Cui 	memcpy(write_blk->bytes, buf, len);
1527e5d2f910SDexuan Cui 	pkt_size = offsetof(struct pci_write_block, bytes) + len;
1528e5d2f910SDexuan Cui 	/*
1529e5d2f910SDexuan Cui 	 * This quirk is required on some hosts shipped around 2018, because
1530e5d2f910SDexuan Cui 	 * these hosts don't check the pkt_size correctly (new hosts have been
1531e5d2f910SDexuan Cui 	 * fixed since early 2019). The quirk is also safe on very old hosts
1532e5d2f910SDexuan Cui 	 * and new hosts, because, on them, what really matters is the length
1533e5d2f910SDexuan Cui 	 * specified in write_blk->byte_count.
1534e5d2f910SDexuan Cui 	 */
1535e5d2f910SDexuan Cui 	pkt_size += sizeof(pkt.reserved);
1536e5d2f910SDexuan Cui 
1537e5d2f910SDexuan Cui 	ret = vmbus_sendpacket(hbus->hdev->channel, write_blk, pkt_size,
1538e5d2f910SDexuan Cui 			       (unsigned long)&pkt.pkt, VM_PKT_DATA_INBAND,
1539e5d2f910SDexuan Cui 			       VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1540e5d2f910SDexuan Cui 	if (ret)
1541e5d2f910SDexuan Cui 		return ret;
1542e5d2f910SDexuan Cui 
1543e5d2f910SDexuan Cui 	ret = wait_for_response(hbus->hdev, &comp_pkt.host_event);
1544e5d2f910SDexuan Cui 	if (ret)
1545e5d2f910SDexuan Cui 		return ret;
1546e5d2f910SDexuan Cui 
1547e5d2f910SDexuan Cui 	if (comp_pkt.completion_status != 0) {
1548e5d2f910SDexuan Cui 		dev_err(&hbus->hdev->device,
1549e5d2f910SDexuan Cui 			"Write Config Block failed: 0x%x\n",
1550e5d2f910SDexuan Cui 			comp_pkt.completion_status);
1551e5d2f910SDexuan Cui 		return -EIO;
1552e5d2f910SDexuan Cui 	}
1553e5d2f910SDexuan Cui 
1554e5d2f910SDexuan Cui 	return 0;
1555e5d2f910SDexuan Cui }
1556e5d2f910SDexuan Cui 
1557e5d2f910SDexuan Cui /**
1558e5d2f910SDexuan Cui  * hv_register_block_invalidate() - Invoked when a config block invalidation
1559e5d2f910SDexuan Cui  * arrives from the back-end driver.
1560e5d2f910SDexuan Cui  * @pdev:		The PCI driver's representation for this device.
1561e5d2f910SDexuan Cui  * @context:		Identifies the device.
1562e5d2f910SDexuan Cui  * @block_invalidate:	Identifies all of the blocks being invalidated.
1563e5d2f910SDexuan Cui  *
1564e5d2f910SDexuan Cui  * Return: 0 on success, -errno on failure
1565e5d2f910SDexuan Cui  */
hv_register_block_invalidate(struct pci_dev * pdev,void * context,void (* block_invalidate)(void * context,u64 block_mask))1566a459d9e1SWei Yongjun static int hv_register_block_invalidate(struct pci_dev *pdev, void *context,
1567e5d2f910SDexuan Cui 					void (*block_invalidate)(void *context,
1568e5d2f910SDexuan Cui 								 u64 block_mask))
1569e5d2f910SDexuan Cui {
1570e5d2f910SDexuan Cui 	struct hv_pcibus_device *hbus =
1571e5d2f910SDexuan Cui 		container_of(pdev->bus->sysdata, struct hv_pcibus_device,
1572e5d2f910SDexuan Cui 			     sysdata);
1573e5d2f910SDexuan Cui 	struct hv_pci_dev *hpdev;
1574e5d2f910SDexuan Cui 
1575e5d2f910SDexuan Cui 	hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
1576e5d2f910SDexuan Cui 	if (!hpdev)
1577e5d2f910SDexuan Cui 		return -ENODEV;
1578e5d2f910SDexuan Cui 
1579e5d2f910SDexuan Cui 	hpdev->block_invalidate = block_invalidate;
1580e5d2f910SDexuan Cui 	hpdev->invalidate_context = context;
1581e5d2f910SDexuan Cui 
1582e5d2f910SDexuan Cui 	put_pcichild(hpdev);
1583e5d2f910SDexuan Cui 	return 0;
1584e5d2f910SDexuan Cui 
1585e5d2f910SDexuan Cui }
1586e5d2f910SDexuan Cui 
15876e0832faSShawn Lin /* Interrupt management hooks */
hv_int_desc_free(struct hv_pci_dev * hpdev,struct tran_int_desc * int_desc)15886e0832faSShawn Lin static void hv_int_desc_free(struct hv_pci_dev *hpdev,
15896e0832faSShawn Lin 			     struct tran_int_desc *int_desc)
15906e0832faSShawn Lin {
15916e0832faSShawn Lin 	struct pci_delete_interrupt *int_pkt;
15926e0832faSShawn Lin 	struct {
15936e0832faSShawn Lin 		struct pci_packet pkt;
15946e0832faSShawn Lin 		u8 buffer[sizeof(struct pci_delete_interrupt)];
15956e0832faSShawn Lin 	} ctxt;
15966e0832faSShawn Lin 
1597a2bad844SJeffrey Hugo 	if (!int_desc->vector_count) {
1598a2bad844SJeffrey Hugo 		kfree(int_desc);
1599a2bad844SJeffrey Hugo 		return;
1600a2bad844SJeffrey Hugo 	}
16016e0832faSShawn Lin 	memset(&ctxt, 0, sizeof(ctxt));
16026e0832faSShawn Lin 	int_pkt = (struct pci_delete_interrupt *)&ctxt.pkt.message;
16036e0832faSShawn Lin 	int_pkt->message_type.type =
16046e0832faSShawn Lin 		PCI_DELETE_INTERRUPT_MESSAGE;
16056e0832faSShawn Lin 	int_pkt->wslot.slot = hpdev->desc.win_slot.slot;
16066e0832faSShawn Lin 	int_pkt->int_desc = *int_desc;
16076e0832faSShawn Lin 	vmbus_sendpacket(hpdev->hbus->hdev->channel, int_pkt, sizeof(*int_pkt),
1608de5ddb7dSAndrea Parri (Microsoft) 			 0, VM_PKT_DATA_INBAND, 0);
16096e0832faSShawn Lin 	kfree(int_desc);
16106e0832faSShawn Lin }
16116e0832faSShawn Lin 
16126e0832faSShawn Lin /**
16136e0832faSShawn Lin  * hv_msi_free() - Free the MSI.
16146e0832faSShawn Lin  * @domain:	The interrupt domain pointer
16156e0832faSShawn Lin  * @info:	Extra MSI-related context
16166e0832faSShawn Lin  * @irq:	Identifies the IRQ.
16176e0832faSShawn Lin  *
16186e0832faSShawn Lin  * The Hyper-V parent partition and hypervisor are tracking the
16196e0832faSShawn Lin  * messages that are in use, keeping the interrupt redirection
16206e0832faSShawn Lin  * table up to date.  This callback sends a message that frees
16216e0832faSShawn Lin  * the IRT entry and related tracking nonsense.
16226e0832faSShawn Lin  */
hv_msi_free(struct irq_domain * domain,struct msi_domain_info * info,unsigned int irq)16236e0832faSShawn Lin static void hv_msi_free(struct irq_domain *domain, struct msi_domain_info *info,
16246e0832faSShawn Lin 			unsigned int irq)
16256e0832faSShawn Lin {
16266e0832faSShawn Lin 	struct hv_pcibus_device *hbus;
16276e0832faSShawn Lin 	struct hv_pci_dev *hpdev;
16286e0832faSShawn Lin 	struct pci_dev *pdev;
16296e0832faSShawn Lin 	struct tran_int_desc *int_desc;
16306e0832faSShawn Lin 	struct irq_data *irq_data = irq_domain_get_irq_data(domain, irq);
16316e0832faSShawn Lin 	struct msi_desc *msi = irq_data_get_msi_desc(irq_data);
16326e0832faSShawn Lin 
16336e0832faSShawn Lin 	pdev = msi_desc_to_pci_dev(msi);
16346e0832faSShawn Lin 	hbus = info->data;
16356e0832faSShawn Lin 	int_desc = irq_data_get_irq_chip_data(irq_data);
16366e0832faSShawn Lin 	if (!int_desc)
16376e0832faSShawn Lin 		return;
16386e0832faSShawn Lin 
16396e0832faSShawn Lin 	irq_data->chip_data = NULL;
16406e0832faSShawn Lin 	hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
16416e0832faSShawn Lin 	if (!hpdev) {
16426e0832faSShawn Lin 		kfree(int_desc);
16436e0832faSShawn Lin 		return;
16446e0832faSShawn Lin 	}
16456e0832faSShawn Lin 
16466e0832faSShawn Lin 	hv_int_desc_free(hpdev, int_desc);
16476e0832faSShawn Lin 	put_pcichild(hpdev);
16486e0832faSShawn Lin }
16496e0832faSShawn Lin 
hv_irq_mask(struct irq_data * data)16506e0832faSShawn Lin static void hv_irq_mask(struct irq_data *data)
16516e0832faSShawn Lin {
16526e0832faSShawn Lin 	pci_msi_mask_irq(data);
1653d9932b46SSunil Muthuswamy 	if (data->parent_data->chip->irq_mask)
1654d9932b46SSunil Muthuswamy 		irq_chip_mask_parent(data);
16556e0832faSShawn Lin }
16566e0832faSShawn Lin 
hv_irq_unmask(struct irq_data * data)16576e0832faSShawn Lin static void hv_irq_unmask(struct irq_data *data)
16586e0832faSShawn Lin {
1659d06957d7SBoqun Feng 	hv_arch_irq_unmask(data);
16606e0832faSShawn Lin 
1661d9932b46SSunil Muthuswamy 	if (data->parent_data->chip->irq_unmask)
1662d9932b46SSunil Muthuswamy 		irq_chip_unmask_parent(data);
16636e0832faSShawn Lin 	pci_msi_unmask_irq(data);
16646e0832faSShawn Lin }
16656e0832faSShawn Lin 
16666e0832faSShawn Lin struct compose_comp_ctxt {
16676e0832faSShawn Lin 	struct hv_pci_compl comp_pkt;
16686e0832faSShawn Lin 	struct tran_int_desc int_desc;
16696e0832faSShawn Lin };
16706e0832faSShawn Lin 
hv_pci_compose_compl(void * context,struct pci_response * resp,int resp_packet_size)16716e0832faSShawn Lin static void hv_pci_compose_compl(void *context, struct pci_response *resp,
16726e0832faSShawn Lin 				 int resp_packet_size)
16736e0832faSShawn Lin {
16746e0832faSShawn Lin 	struct compose_comp_ctxt *comp_pkt = context;
16756e0832faSShawn Lin 	struct pci_create_int_response *int_resp =
16766e0832faSShawn Lin 		(struct pci_create_int_response *)resp;
16776e0832faSShawn Lin 
16789937fa6dSAndrea Parri (Microsoft) 	if (resp_packet_size < sizeof(*int_resp)) {
16799937fa6dSAndrea Parri (Microsoft) 		comp_pkt->comp_pkt.completion_status = -1;
16809937fa6dSAndrea Parri (Microsoft) 		goto out;
16819937fa6dSAndrea Parri (Microsoft) 	}
16826e0832faSShawn Lin 	comp_pkt->comp_pkt.completion_status = resp->status;
16836e0832faSShawn Lin 	comp_pkt->int_desc = int_resp->int_desc;
16849937fa6dSAndrea Parri (Microsoft) out:
16856e0832faSShawn Lin 	complete(&comp_pkt->comp_pkt.host_event);
16866e0832faSShawn Lin }
16876e0832faSShawn Lin 
hv_compose_msi_req_v1(struct pci_create_interrupt * int_pkt,u32 slot,u8 vector,u16 vector_count)16886e0832faSShawn Lin static u32 hv_compose_msi_req_v1(
1689c234ba80SDexuan Cui 	struct pci_create_interrupt *int_pkt,
1690e70af8d0SDexuan Cui 	u32 slot, u8 vector, u16 vector_count)
16916e0832faSShawn Lin {
16926e0832faSShawn Lin 	int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE;
16936e0832faSShawn Lin 	int_pkt->wslot.slot = slot;
16946e0832faSShawn Lin 	int_pkt->int_desc.vector = vector;
1695a2bad844SJeffrey Hugo 	int_pkt->int_desc.vector_count = vector_count;
1696831c1ae7SSunil Muthuswamy 	int_pkt->int_desc.delivery_mode = DELIVERY_MODE;
16976e0832faSShawn Lin 
16986e0832faSShawn Lin 	/*
16996e0832faSShawn Lin 	 * Create MSI w/ dummy vCPU set, overwritten by subsequent retarget in
17006e0832faSShawn Lin 	 * hv_irq_unmask().
17016e0832faSShawn Lin 	 */
17026e0832faSShawn Lin 	int_pkt->int_desc.cpu_mask = CPU_AFFINITY_ALL;
17036e0832faSShawn Lin 
17046e0832faSShawn Lin 	return sizeof(*int_pkt);
17056e0832faSShawn Lin }
17066e0832faSShawn Lin 
17078f6a6b3cSSunil Muthuswamy /*
1708c234ba80SDexuan Cui  * The vCPU selected by hv_compose_multi_msi_req_get_cpu() and
1709c234ba80SDexuan Cui  * hv_compose_msi_req_get_cpu() is a "dummy" vCPU because the final vCPU to be
1710c234ba80SDexuan Cui  * interrupted is specified later in hv_irq_unmask() and communicated to Hyper-V
1711c234ba80SDexuan Cui  * via the HVCALL_RETARGET_INTERRUPT hypercall. But the choice of dummy vCPU is
1712c234ba80SDexuan Cui  * not irrelevant because Hyper-V chooses the physical CPU to handle the
1713c234ba80SDexuan Cui  * interrupts based on the vCPU specified in message sent to the vPCI VSP in
1714c234ba80SDexuan Cui  * hv_compose_msi_msg(). Hyper-V's choice of pCPU is not visible to the guest,
1715c234ba80SDexuan Cui  * but assigning too many vPCI device interrupts to the same pCPU can cause a
1716c234ba80SDexuan Cui  * performance bottleneck. So we spread out the dummy vCPUs to influence Hyper-V
1717c234ba80SDexuan Cui  * to spread out the pCPUs that it selects.
1718c234ba80SDexuan Cui  *
1719c234ba80SDexuan Cui  * For the single-MSI and MSI-X cases, it's OK for hv_compose_msi_req_get_cpu()
1720c234ba80SDexuan Cui  * to always return the same dummy vCPU, because a second call to
1721c234ba80SDexuan Cui  * hv_compose_msi_msg() contains the "real" vCPU, causing Hyper-V to choose a
1722c234ba80SDexuan Cui  * new pCPU for the interrupt. But for the multi-MSI case, the second call to
1723c234ba80SDexuan Cui  * hv_compose_msi_msg() exits without sending a message to the vPCI VSP, so the
1724c234ba80SDexuan Cui  * original dummy vCPU is used. This dummy vCPU must be round-robin'ed so that
1725c234ba80SDexuan Cui  * the pCPUs are spread out. All interrupts for a multi-MSI device end up using
1726c234ba80SDexuan Cui  * the same pCPU, even though the vCPUs will be spread out by later calls
1727c234ba80SDexuan Cui  * to hv_irq_unmask(), but that is the best we can do now.
1728c234ba80SDexuan Cui  *
1729c234ba80SDexuan Cui  * With Hyper-V in Nov 2022, the HVCALL_RETARGET_INTERRUPT hypercall does *not*
1730c234ba80SDexuan Cui  * cause Hyper-V to reselect the pCPU based on the specified vCPU. Such an
1731c234ba80SDexuan Cui  * enhancement is planned for a future version. With that enhancement, the
1732c234ba80SDexuan Cui  * dummy vCPU selection won't matter, and interrupts for the same multi-MSI
1733c234ba80SDexuan Cui  * device will be spread across multiple pCPUs.
1734c234ba80SDexuan Cui  */
1735c234ba80SDexuan Cui 
1736c234ba80SDexuan Cui /*
17378f6a6b3cSSunil Muthuswamy  * Create MSI w/ dummy vCPU set targeting just one vCPU, overwritten
17388f6a6b3cSSunil Muthuswamy  * by subsequent retarget in hv_irq_unmask().
17398f6a6b3cSSunil Muthuswamy  */
hv_compose_msi_req_get_cpu(const struct cpumask * affinity)17409167fd5dSSamuel Holland static int hv_compose_msi_req_get_cpu(const struct cpumask *affinity)
17418f6a6b3cSSunil Muthuswamy {
17428f6a6b3cSSunil Muthuswamy 	return cpumask_first_and(affinity, cpu_online_mask);
17438f6a6b3cSSunil Muthuswamy }
17448f6a6b3cSSunil Muthuswamy 
1745c234ba80SDexuan Cui /*
1746c234ba80SDexuan Cui  * Make sure the dummy vCPU values for multi-MSI don't all point to vCPU0.
1747c234ba80SDexuan Cui  */
hv_compose_multi_msi_req_get_cpu(void)1748c234ba80SDexuan Cui static int hv_compose_multi_msi_req_get_cpu(void)
17496e0832faSShawn Lin {
1750c234ba80SDexuan Cui 	static DEFINE_SPINLOCK(multi_msi_cpu_lock);
1751c234ba80SDexuan Cui 
1752c234ba80SDexuan Cui 	/* -1 means starting with CPU 0 */
1753c234ba80SDexuan Cui 	static int cpu_next = -1;
1754c234ba80SDexuan Cui 
1755c234ba80SDexuan Cui 	unsigned long flags;
17566e0832faSShawn Lin 	int cpu;
17576e0832faSShawn Lin 
1758c234ba80SDexuan Cui 	spin_lock_irqsave(&multi_msi_cpu_lock, flags);
1759c234ba80SDexuan Cui 
1760c234ba80SDexuan Cui 	cpu_next = cpumask_next_wrap(cpu_next, cpu_online_mask, nr_cpu_ids,
1761c234ba80SDexuan Cui 				     false);
1762c234ba80SDexuan Cui 	cpu = cpu_next;
1763c234ba80SDexuan Cui 
1764c234ba80SDexuan Cui 	spin_unlock_irqrestore(&multi_msi_cpu_lock, flags);
1765c234ba80SDexuan Cui 
1766c234ba80SDexuan Cui 	return cpu;
1767c234ba80SDexuan Cui }
1768c234ba80SDexuan Cui 
hv_compose_msi_req_v2(struct pci_create_interrupt2 * int_pkt,int cpu,u32 slot,u8 vector,u16 vector_count)1769c234ba80SDexuan Cui static u32 hv_compose_msi_req_v2(
1770c234ba80SDexuan Cui 	struct pci_create_interrupt2 *int_pkt, int cpu,
1771c234ba80SDexuan Cui 	u32 slot, u8 vector, u16 vector_count)
1772c234ba80SDexuan Cui {
17736e0832faSShawn Lin 	int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE2;
17746e0832faSShawn Lin 	int_pkt->wslot.slot = slot;
17756e0832faSShawn Lin 	int_pkt->int_desc.vector = vector;
1776a2bad844SJeffrey Hugo 	int_pkt->int_desc.vector_count = vector_count;
1777831c1ae7SSunil Muthuswamy 	int_pkt->int_desc.delivery_mode = DELIVERY_MODE;
17788f6a6b3cSSunil Muthuswamy 	int_pkt->int_desc.processor_array[0] =
17798f6a6b3cSSunil Muthuswamy 		hv_cpu_number_to_vp_number(cpu);
17808f6a6b3cSSunil Muthuswamy 	int_pkt->int_desc.processor_count = 1;
17816e0832faSShawn Lin 
17828f6a6b3cSSunil Muthuswamy 	return sizeof(*int_pkt);
17838f6a6b3cSSunil Muthuswamy }
17848f6a6b3cSSunil Muthuswamy 
hv_compose_msi_req_v3(struct pci_create_interrupt3 * int_pkt,int cpu,u32 slot,u32 vector,u16 vector_count)17858f6a6b3cSSunil Muthuswamy static u32 hv_compose_msi_req_v3(
1786c234ba80SDexuan Cui 	struct pci_create_interrupt3 *int_pkt, int cpu,
1787e70af8d0SDexuan Cui 	u32 slot, u32 vector, u16 vector_count)
17888f6a6b3cSSunil Muthuswamy {
17898f6a6b3cSSunil Muthuswamy 	int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE3;
17908f6a6b3cSSunil Muthuswamy 	int_pkt->wslot.slot = slot;
17918f6a6b3cSSunil Muthuswamy 	int_pkt->int_desc.vector = vector;
17928f6a6b3cSSunil Muthuswamy 	int_pkt->int_desc.reserved = 0;
1793a2bad844SJeffrey Hugo 	int_pkt->int_desc.vector_count = vector_count;
1794831c1ae7SSunil Muthuswamy 	int_pkt->int_desc.delivery_mode = DELIVERY_MODE;
17956e0832faSShawn Lin 	int_pkt->int_desc.processor_array[0] =
17966e0832faSShawn Lin 		hv_cpu_number_to_vp_number(cpu);
17976e0832faSShawn Lin 	int_pkt->int_desc.processor_count = 1;
17986e0832faSShawn Lin 
17996e0832faSShawn Lin 	return sizeof(*int_pkt);
18006e0832faSShawn Lin }
18016e0832faSShawn Lin 
18026e0832faSShawn Lin /**
18036e0832faSShawn Lin  * hv_compose_msi_msg() - Supplies a valid MSI address/data
18046e0832faSShawn Lin  * @data:	Everything about this MSI
18056e0832faSShawn Lin  * @msg:	Buffer that is filled in by this function
18066e0832faSShawn Lin  *
18076e0832faSShawn Lin  * This function unpacks the IRQ looking for target CPU set, IDT
18086e0832faSShawn Lin  * vector and mode and sends a message to the parent partition
18096e0832faSShawn Lin  * asking for a mapping for that tuple in this partition.  The
18106e0832faSShawn Lin  * response supplies a data value and address to which that data
18116e0832faSShawn Lin  * should be written to trigger that interrupt.
18126e0832faSShawn Lin  */
hv_compose_msi_msg(struct irq_data * data,struct msi_msg * msg)18136e0832faSShawn Lin static void hv_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
18146e0832faSShawn Lin {
18156e0832faSShawn Lin 	struct hv_pcibus_device *hbus;
1816240ad77cSAndrea Parri (Microsoft) 	struct vmbus_channel *channel;
18176e0832faSShawn Lin 	struct hv_pci_dev *hpdev;
18186e0832faSShawn Lin 	struct pci_bus *pbus;
18196e0832faSShawn Lin 	struct pci_dev *pdev;
18204d0b8298SSamuel Holland 	const struct cpumask *dest;
18216e0832faSShawn Lin 	struct compose_comp_ctxt comp;
18226e0832faSShawn Lin 	struct tran_int_desc *int_desc;
1823a2bad844SJeffrey Hugo 	struct msi_desc *msi_desc;
1824e70af8d0SDexuan Cui 	/*
1825e70af8d0SDexuan Cui 	 * vector_count should be u16: see hv_msi_desc, hv_msi_desc2
1826e70af8d0SDexuan Cui 	 * and hv_msi_desc3. vector must be u32: see hv_msi_desc3.
1827e70af8d0SDexuan Cui 	 */
1828e70af8d0SDexuan Cui 	u16 vector_count;
1829e70af8d0SDexuan Cui 	u32 vector;
18306e0832faSShawn Lin 	struct {
18316e0832faSShawn Lin 		struct pci_packet pci_pkt;
18326e0832faSShawn Lin 		union {
18336e0832faSShawn Lin 			struct pci_create_interrupt v1;
18346e0832faSShawn Lin 			struct pci_create_interrupt2 v2;
18358f6a6b3cSSunil Muthuswamy 			struct pci_create_interrupt3 v3;
18366e0832faSShawn Lin 		} int_pkts;
18376e0832faSShawn Lin 	} __packed ctxt;
1838c234ba80SDexuan Cui 	bool multi_msi;
1839a765ed47SAndrea Parri (Microsoft) 	u64 trans_id;
18406e0832faSShawn Lin 	u32 size;
18416e0832faSShawn Lin 	int ret;
1842c234ba80SDexuan Cui 	int cpu;
1843c234ba80SDexuan Cui 
1844c234ba80SDexuan Cui 	msi_desc  = irq_data_get_msi_desc(data);
1845c234ba80SDexuan Cui 	multi_msi = !msi_desc->pci.msi_attrib.is_msix &&
1846c234ba80SDexuan Cui 		    msi_desc->nvec_used > 1;
18476e0832faSShawn Lin 
1848b4b77778SJeffrey Hugo 	/* Reuse the previous allocation */
1849c234ba80SDexuan Cui 	if (data->chip_data && multi_msi) {
1850b4b77778SJeffrey Hugo 		int_desc = data->chip_data;
1851b4b77778SJeffrey Hugo 		msg->address_hi = int_desc->address >> 32;
1852b4b77778SJeffrey Hugo 		msg->address_lo = int_desc->address & 0xffffffff;
1853b4b77778SJeffrey Hugo 		msg->data = int_desc->data;
1854b4b77778SJeffrey Hugo 		return;
1855b4b77778SJeffrey Hugo 	}
1856b4b77778SJeffrey Hugo 
1857a2bad844SJeffrey Hugo 	pdev = msi_desc_to_pci_dev(msi_desc);
18586e0832faSShawn Lin 	dest = irq_data_get_effective_affinity_mask(data);
18596e0832faSShawn Lin 	pbus = pdev->bus;
18606e0832faSShawn Lin 	hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata);
1861240ad77cSAndrea Parri (Microsoft) 	channel = hbus->hdev->channel;
18626e0832faSShawn Lin 	hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
18636e0832faSShawn Lin 	if (!hpdev)
18646e0832faSShawn Lin 		goto return_null_message;
18656e0832faSShawn Lin 
1866c234ba80SDexuan Cui 	/* Free any previous message that might have already been composed. */
1867c234ba80SDexuan Cui 	if (data->chip_data && !multi_msi) {
1868c234ba80SDexuan Cui 		int_desc = data->chip_data;
1869c234ba80SDexuan Cui 		data->chip_data = NULL;
1870c234ba80SDexuan Cui 		hv_int_desc_free(hpdev, int_desc);
1871c234ba80SDexuan Cui 	}
1872c234ba80SDexuan Cui 
18736e0832faSShawn Lin 	int_desc = kzalloc(sizeof(*int_desc), GFP_ATOMIC);
18746e0832faSShawn Lin 	if (!int_desc)
18756e0832faSShawn Lin 		goto drop_reference;
18766e0832faSShawn Lin 
1877c234ba80SDexuan Cui 	if (multi_msi) {
1878a2bad844SJeffrey Hugo 		/*
1879a2bad844SJeffrey Hugo 		 * If this is not the first MSI of Multi MSI, we already have
1880a2bad844SJeffrey Hugo 		 * a mapping.  Can exit early.
1881a2bad844SJeffrey Hugo 		 */
1882a2bad844SJeffrey Hugo 		if (msi_desc->irq != data->irq) {
1883a2bad844SJeffrey Hugo 			data->chip_data = int_desc;
1884a2bad844SJeffrey Hugo 			int_desc->address = msi_desc->msg.address_lo |
1885a2bad844SJeffrey Hugo 					    (u64)msi_desc->msg.address_hi << 32;
1886a2bad844SJeffrey Hugo 			int_desc->data = msi_desc->msg.data +
1887a2bad844SJeffrey Hugo 					 (data->irq - msi_desc->irq);
1888a2bad844SJeffrey Hugo 			msg->address_hi = msi_desc->msg.address_hi;
1889a2bad844SJeffrey Hugo 			msg->address_lo = msi_desc->msg.address_lo;
1890a2bad844SJeffrey Hugo 			msg->data = int_desc->data;
1891a2bad844SJeffrey Hugo 			put_pcichild(hpdev);
1892a2bad844SJeffrey Hugo 			return;
1893a2bad844SJeffrey Hugo 		}
1894a2bad844SJeffrey Hugo 		/*
1895a2bad844SJeffrey Hugo 		 * The vector we select here is a dummy value.  The correct
1896a2bad844SJeffrey Hugo 		 * value gets sent to the hypervisor in unmask().  This needs
1897a2bad844SJeffrey Hugo 		 * to be aligned with the count, and also not zero.  Multi-msi
1898a2bad844SJeffrey Hugo 		 * is powers of 2 up to 32, so 32 will always work here.
1899a2bad844SJeffrey Hugo 		 */
1900a2bad844SJeffrey Hugo 		vector = 32;
1901a2bad844SJeffrey Hugo 		vector_count = msi_desc->nvec_used;
1902c234ba80SDexuan Cui 		cpu = hv_compose_multi_msi_req_get_cpu();
1903a2bad844SJeffrey Hugo 	} else {
1904a2bad844SJeffrey Hugo 		vector = hv_msi_get_int_vector(data);
1905a2bad844SJeffrey Hugo 		vector_count = 1;
1906c234ba80SDexuan Cui 		cpu = hv_compose_msi_req_get_cpu(dest);
1907a2bad844SJeffrey Hugo 	}
1908a2bad844SJeffrey Hugo 
1909e70af8d0SDexuan Cui 	/*
1910e70af8d0SDexuan Cui 	 * hv_compose_msi_req_v1 and v2 are for x86 only, meaning 'vector'
1911e70af8d0SDexuan Cui 	 * can't exceed u8. Cast 'vector' down to u8 for v1/v2 explicitly
1912e70af8d0SDexuan Cui 	 * for better readability.
1913e70af8d0SDexuan Cui 	 */
19146e0832faSShawn Lin 	memset(&ctxt, 0, sizeof(ctxt));
19156e0832faSShawn Lin 	init_completion(&comp.comp_pkt.host_event);
19166e0832faSShawn Lin 	ctxt.pci_pkt.completion_func = hv_pci_compose_compl;
19176e0832faSShawn Lin 	ctxt.pci_pkt.compl_ctxt = &comp;
19186e0832faSShawn Lin 
191914ef39fdSDexuan Cui 	switch (hbus->protocol_version) {
19206e0832faSShawn Lin 	case PCI_PROTOCOL_VERSION_1_1:
19216e0832faSShawn Lin 		size = hv_compose_msi_req_v1(&ctxt.int_pkts.v1,
19226e0832faSShawn Lin 					hpdev->desc.win_slot.slot,
1923e70af8d0SDexuan Cui 					(u8)vector,
1924a2bad844SJeffrey Hugo 					vector_count);
19256e0832faSShawn Lin 		break;
19266e0832faSShawn Lin 
19276e0832faSShawn Lin 	case PCI_PROTOCOL_VERSION_1_2:
1928999dd956SLong Li 	case PCI_PROTOCOL_VERSION_1_3:
19296e0832faSShawn Lin 		size = hv_compose_msi_req_v2(&ctxt.int_pkts.v2,
1930c234ba80SDexuan Cui 					cpu,
19316e0832faSShawn Lin 					hpdev->desc.win_slot.slot,
1932e70af8d0SDexuan Cui 					(u8)vector,
1933a2bad844SJeffrey Hugo 					vector_count);
19346e0832faSShawn Lin 		break;
19356e0832faSShawn Lin 
19368f6a6b3cSSunil Muthuswamy 	case PCI_PROTOCOL_VERSION_1_4:
19378f6a6b3cSSunil Muthuswamy 		size = hv_compose_msi_req_v3(&ctxt.int_pkts.v3,
1938c234ba80SDexuan Cui 					cpu,
19398f6a6b3cSSunil Muthuswamy 					hpdev->desc.win_slot.slot,
1940a2bad844SJeffrey Hugo 					vector,
1941a2bad844SJeffrey Hugo 					vector_count);
19428f6a6b3cSSunil Muthuswamy 		break;
19438f6a6b3cSSunil Muthuswamy 
19446e0832faSShawn Lin 	default:
19456e0832faSShawn Lin 		/* As we only negotiate protocol versions known to this driver,
19466e0832faSShawn Lin 		 * this path should never hit. However, this is it not a hot
19476e0832faSShawn Lin 		 * path so we print a message to aid future updates.
19486e0832faSShawn Lin 		 */
19496e0832faSShawn Lin 		dev_err(&hbus->hdev->device,
19506e0832faSShawn Lin 			"Unexpected vPCI protocol, update driver.");
19516e0832faSShawn Lin 		goto free_int_desc;
19526e0832faSShawn Lin 	}
19536e0832faSShawn Lin 
1954a765ed47SAndrea Parri (Microsoft) 	ret = vmbus_sendpacket_getid(hpdev->hbus->hdev->channel, &ctxt.int_pkts,
19556e0832faSShawn Lin 				     size, (unsigned long)&ctxt.pci_pkt,
1956a765ed47SAndrea Parri (Microsoft) 				     &trans_id, VM_PKT_DATA_INBAND,
19576e0832faSShawn Lin 				     VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
19586e0832faSShawn Lin 	if (ret) {
19596e0832faSShawn Lin 		dev_err(&hbus->hdev->device,
19606e0832faSShawn Lin 			"Sending request for interrupt failed: 0x%x",
19616e0832faSShawn Lin 			comp.comp_pkt.completion_status);
19626e0832faSShawn Lin 		goto free_int_desc;
19636e0832faSShawn Lin 	}
19646e0832faSShawn Lin 
19656e0832faSShawn Lin 	/*
1966240ad77cSAndrea Parri (Microsoft) 	 * Prevents hv_pci_onchannelcallback() from running concurrently
1967240ad77cSAndrea Parri (Microsoft) 	 * in the tasklet.
1968240ad77cSAndrea Parri (Microsoft) 	 */
1969be4017ceSSebastian Andrzej Siewior 	tasklet_disable_in_atomic(&channel->callback_event);
1970240ad77cSAndrea Parri (Microsoft) 
1971240ad77cSAndrea Parri (Microsoft) 	/*
19726e0832faSShawn Lin 	 * Since this function is called with IRQ locks held, can't
19736e0832faSShawn Lin 	 * do normal wait for completion; instead poll.
19746e0832faSShawn Lin 	 */
19756e0832faSShawn Lin 	while (!try_wait_for_completion(&comp.comp_pkt.host_event)) {
1976240ad77cSAndrea Parri (Microsoft) 		unsigned long flags;
1977240ad77cSAndrea Parri (Microsoft) 
19786e0832faSShawn Lin 		/* 0xFFFF means an invalid PCI VENDOR ID. */
19796e0832faSShawn Lin 		if (hv_pcifront_get_vendor_id(hpdev) == 0xFFFF) {
19806e0832faSShawn Lin 			dev_err_once(&hbus->hdev->device,
19816e0832faSShawn Lin 				     "the device has gone\n");
1982240ad77cSAndrea Parri (Microsoft) 			goto enable_tasklet;
19836e0832faSShawn Lin 		}
19846e0832faSShawn Lin 
19856e0832faSShawn Lin 		/*
1986240ad77cSAndrea Parri (Microsoft) 		 * Make sure that the ring buffer data structure doesn't get
1987240ad77cSAndrea Parri (Microsoft) 		 * freed while we dereference the ring buffer pointer.  Test
1988240ad77cSAndrea Parri (Microsoft) 		 * for the channel's onchannel_callback being NULL within a
1989240ad77cSAndrea Parri (Microsoft) 		 * sched_lock critical section.  See also the inline comments
1990240ad77cSAndrea Parri (Microsoft) 		 * in vmbus_reset_channel_cb().
19916e0832faSShawn Lin 		 */
1992240ad77cSAndrea Parri (Microsoft) 		spin_lock_irqsave(&channel->sched_lock, flags);
1993240ad77cSAndrea Parri (Microsoft) 		if (unlikely(channel->onchannel_callback == NULL)) {
1994240ad77cSAndrea Parri (Microsoft) 			spin_unlock_irqrestore(&channel->sched_lock, flags);
1995240ad77cSAndrea Parri (Microsoft) 			goto enable_tasklet;
1996240ad77cSAndrea Parri (Microsoft) 		}
19976e0832faSShawn Lin 		hv_pci_onchannelcallback(hbus);
1998240ad77cSAndrea Parri (Microsoft) 		spin_unlock_irqrestore(&channel->sched_lock, flags);
19996e0832faSShawn Lin 
20006e0832faSShawn Lin 		udelay(100);
20016e0832faSShawn Lin 	}
20026e0832faSShawn Lin 
2003240ad77cSAndrea Parri (Microsoft) 	tasklet_enable(&channel->callback_event);
2004240ad77cSAndrea Parri (Microsoft) 
20056e0832faSShawn Lin 	if (comp.comp_pkt.completion_status < 0) {
20066e0832faSShawn Lin 		dev_err(&hbus->hdev->device,
20076e0832faSShawn Lin 			"Request for interrupt failed: 0x%x",
20086e0832faSShawn Lin 			comp.comp_pkt.completion_status);
20096e0832faSShawn Lin 		goto free_int_desc;
20106e0832faSShawn Lin 	}
20116e0832faSShawn Lin 
20126e0832faSShawn Lin 	/*
20136e0832faSShawn Lin 	 * Record the assignment so that this can be unwound later. Using
20146e0832faSShawn Lin 	 * irq_set_chip_data() here would be appropriate, but the lock it takes
20156e0832faSShawn Lin 	 * is already held.
20166e0832faSShawn Lin 	 */
20176e0832faSShawn Lin 	*int_desc = comp.int_desc;
20186e0832faSShawn Lin 	data->chip_data = int_desc;
20196e0832faSShawn Lin 
20206e0832faSShawn Lin 	/* Pass up the result. */
20216e0832faSShawn Lin 	msg->address_hi = comp.int_desc.address >> 32;
20226e0832faSShawn Lin 	msg->address_lo = comp.int_desc.address & 0xffffffff;
20236e0832faSShawn Lin 	msg->data = comp.int_desc.data;
20246e0832faSShawn Lin 
20256e0832faSShawn Lin 	put_pcichild(hpdev);
20266e0832faSShawn Lin 	return;
20276e0832faSShawn Lin 
2028240ad77cSAndrea Parri (Microsoft) enable_tasklet:
2029240ad77cSAndrea Parri (Microsoft) 	tasklet_enable(&channel->callback_event);
2030a765ed47SAndrea Parri (Microsoft) 	/*
2031a765ed47SAndrea Parri (Microsoft) 	 * The completion packet on the stack becomes invalid after 'return';
2032a765ed47SAndrea Parri (Microsoft) 	 * remove the ID from the VMbus requestor if the identifier is still
2033a765ed47SAndrea Parri (Microsoft) 	 * mapped to/associated with the packet.  (The identifier could have
2034a765ed47SAndrea Parri (Microsoft) 	 * been 're-used', i.e., already removed and (re-)mapped.)
2035a765ed47SAndrea Parri (Microsoft) 	 *
2036a765ed47SAndrea Parri (Microsoft) 	 * Cf. hv_pci_onchannelcallback().
2037a765ed47SAndrea Parri (Microsoft) 	 */
2038a765ed47SAndrea Parri (Microsoft) 	vmbus_request_addr_match(channel, trans_id, (unsigned long)&ctxt.pci_pkt);
20396e0832faSShawn Lin free_int_desc:
20406e0832faSShawn Lin 	kfree(int_desc);
20416e0832faSShawn Lin drop_reference:
20426e0832faSShawn Lin 	put_pcichild(hpdev);
20436e0832faSShawn Lin return_null_message:
20446e0832faSShawn Lin 	msg->address_hi = 0;
20456e0832faSShawn Lin 	msg->address_lo = 0;
20466e0832faSShawn Lin 	msg->data = 0;
20476e0832faSShawn Lin }
20486e0832faSShawn Lin 
20496e0832faSShawn Lin /* HW Interrupt Chip Descriptor */
20506e0832faSShawn Lin static struct irq_chip hv_msi_irq_chip = {
20516e0832faSShawn Lin 	.name			= "Hyper-V PCIe MSI",
20526e0832faSShawn Lin 	.irq_compose_msi_msg	= hv_compose_msi_msg,
2053831c1ae7SSunil Muthuswamy 	.irq_set_affinity	= irq_chip_set_affinity_parent,
2054d9932b46SSunil Muthuswamy #ifdef CONFIG_X86
20556e0832faSShawn Lin 	.irq_ack		= irq_chip_ack_parent,
2056d9932b46SSunil Muthuswamy #elif defined(CONFIG_ARM64)
2057d9932b46SSunil Muthuswamy 	.irq_eoi		= irq_chip_eoi_parent,
2058d9932b46SSunil Muthuswamy #endif
20596e0832faSShawn Lin 	.irq_mask		= hv_irq_mask,
20606e0832faSShawn Lin 	.irq_unmask		= hv_irq_unmask,
20616e0832faSShawn Lin };
20626e0832faSShawn Lin 
20636e0832faSShawn Lin static struct msi_domain_ops hv_msi_ops = {
2064831c1ae7SSunil Muthuswamy 	.msi_prepare	= hv_msi_prepare,
20656e0832faSShawn Lin 	.msi_free	= hv_msi_free,
20666e0832faSShawn Lin };
20676e0832faSShawn Lin 
20686e0832faSShawn Lin /**
20696e0832faSShawn Lin  * hv_pcie_init_irq_domain() - Initialize IRQ domain
20706e0832faSShawn Lin  * @hbus:	The root PCI bus
20716e0832faSShawn Lin  *
20726e0832faSShawn Lin  * This function creates an IRQ domain which will be used for
20736e0832faSShawn Lin  * interrupts from devices that have been passed through.  These
20746e0832faSShawn Lin  * devices only support MSI and MSI-X, not line-based interrupts
20756e0832faSShawn Lin  * or simulations of line-based interrupts through PCIe's
20766e0832faSShawn Lin  * fabric-layer messages.  Because interrupts are remapped, we
20776e0832faSShawn Lin  * can support multi-message MSI here.
20786e0832faSShawn Lin  *
20796e0832faSShawn Lin  * Return: '0' on success and error value on failure
20806e0832faSShawn Lin  */
hv_pcie_init_irq_domain(struct hv_pcibus_device * hbus)20816e0832faSShawn Lin static int hv_pcie_init_irq_domain(struct hv_pcibus_device *hbus)
20826e0832faSShawn Lin {
20836e0832faSShawn Lin 	hbus->msi_info.chip = &hv_msi_irq_chip;
20846e0832faSShawn Lin 	hbus->msi_info.ops = &hv_msi_ops;
20856e0832faSShawn Lin 	hbus->msi_info.flags = (MSI_FLAG_USE_DEF_DOM_OPS |
20866e0832faSShawn Lin 		MSI_FLAG_USE_DEF_CHIP_OPS | MSI_FLAG_MULTI_PCI_MSI |
20876e0832faSShawn Lin 		MSI_FLAG_PCI_MSIX);
2088831c1ae7SSunil Muthuswamy 	hbus->msi_info.handler = FLOW_HANDLER;
2089831c1ae7SSunil Muthuswamy 	hbus->msi_info.handler_name = FLOW_NAME;
20906e0832faSShawn Lin 	hbus->msi_info.data = hbus;
20919e7f9178SBoqun Feng 	hbus->irq_domain = pci_msi_create_irq_domain(hbus->fwnode,
20926e0832faSShawn Lin 						     &hbus->msi_info,
2093831c1ae7SSunil Muthuswamy 						     hv_pci_get_root_domain());
20946e0832faSShawn Lin 	if (!hbus->irq_domain) {
20956e0832faSShawn Lin 		dev_err(&hbus->hdev->device,
20966e0832faSShawn Lin 			"Failed to build an MSI IRQ domain\n");
20976e0832faSShawn Lin 		return -ENODEV;
20986e0832faSShawn Lin 	}
20996e0832faSShawn Lin 
21009e7f9178SBoqun Feng 	dev_set_msi_domain(&hbus->bridge->dev, hbus->irq_domain);
21019e7f9178SBoqun Feng 
21026e0832faSShawn Lin 	return 0;
21036e0832faSShawn Lin }
21046e0832faSShawn Lin 
21056e0832faSShawn Lin /**
21066e0832faSShawn Lin  * get_bar_size() - Get the address space consumed by a BAR
21076e0832faSShawn Lin  * @bar_val:	Value that a BAR returned after -1 was written
21086e0832faSShawn Lin  *              to it.
21096e0832faSShawn Lin  *
21106e0832faSShawn Lin  * This function returns the size of the BAR, rounded up to 1
21116e0832faSShawn Lin  * page.  It has to be rounded up because the hypervisor's page
21126e0832faSShawn Lin  * table entry that maps the BAR into the VM can't specify an
21136e0832faSShawn Lin  * offset within a page.  The invariant is that the hypervisor
21146e0832faSShawn Lin  * must place any BARs of smaller than page length at the
21156e0832faSShawn Lin  * beginning of a page.
21166e0832faSShawn Lin  *
21176e0832faSShawn Lin  * Return:	Size in bytes of the consumed MMIO space.
21186e0832faSShawn Lin  */
get_bar_size(u64 bar_val)21196e0832faSShawn Lin static u64 get_bar_size(u64 bar_val)
21206e0832faSShawn Lin {
21216e0832faSShawn Lin 	return round_up((1 + ~(bar_val & PCI_BASE_ADDRESS_MEM_MASK)),
21226e0832faSShawn Lin 			PAGE_SIZE);
21236e0832faSShawn Lin }
21246e0832faSShawn Lin 
21256e0832faSShawn Lin /**
21266e0832faSShawn Lin  * survey_child_resources() - Total all MMIO requirements
21276e0832faSShawn Lin  * @hbus:	Root PCI bus, as understood by this driver
21286e0832faSShawn Lin  */
survey_child_resources(struct hv_pcibus_device * hbus)21296e0832faSShawn Lin static void survey_child_resources(struct hv_pcibus_device *hbus)
21306e0832faSShawn Lin {
21316e0832faSShawn Lin 	struct hv_pci_dev *hpdev;
21326e0832faSShawn Lin 	resource_size_t bar_size = 0;
21336e0832faSShawn Lin 	unsigned long flags;
21346e0832faSShawn Lin 	struct completion *event;
21356e0832faSShawn Lin 	u64 bar_val;
21366e0832faSShawn Lin 	int i;
21376e0832faSShawn Lin 
21386e0832faSShawn Lin 	/* If nobody is waiting on the answer, don't compute it. */
21396e0832faSShawn Lin 	event = xchg(&hbus->survey_event, NULL);
21406e0832faSShawn Lin 	if (!event)
21416e0832faSShawn Lin 		return;
21426e0832faSShawn Lin 
21436e0832faSShawn Lin 	/* If the answer has already been computed, go with it. */
21446e0832faSShawn Lin 	if (hbus->low_mmio_space || hbus->high_mmio_space) {
21456e0832faSShawn Lin 		complete(event);
21466e0832faSShawn Lin 		return;
21476e0832faSShawn Lin 	}
21486e0832faSShawn Lin 
21496e0832faSShawn Lin 	spin_lock_irqsave(&hbus->device_list_lock, flags);
21506e0832faSShawn Lin 
21516e0832faSShawn Lin 	/*
21526e0832faSShawn Lin 	 * Due to an interesting quirk of the PCI spec, all memory regions
21536e0832faSShawn Lin 	 * for a child device are a power of 2 in size and aligned in memory,
21546e0832faSShawn Lin 	 * so it's sufficient to just add them up without tracking alignment.
21556e0832faSShawn Lin 	 */
21566e0832faSShawn Lin 	list_for_each_entry(hpdev, &hbus->children, list_entry) {
2157c9c13ba4SDenis Efremov 		for (i = 0; i < PCI_STD_NUM_BARS; i++) {
21586e0832faSShawn Lin 			if (hpdev->probed_bar[i] & PCI_BASE_ADDRESS_SPACE_IO)
21596e0832faSShawn Lin 				dev_err(&hbus->hdev->device,
21606e0832faSShawn Lin 					"There's an I/O BAR in this list!\n");
21616e0832faSShawn Lin 
21626e0832faSShawn Lin 			if (hpdev->probed_bar[i] != 0) {
21636e0832faSShawn Lin 				/*
21646e0832faSShawn Lin 				 * A probed BAR has all the upper bits set that
21656e0832faSShawn Lin 				 * can be changed.
21666e0832faSShawn Lin 				 */
21676e0832faSShawn Lin 
21686e0832faSShawn Lin 				bar_val = hpdev->probed_bar[i];
21696e0832faSShawn Lin 				if (bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64)
21706e0832faSShawn Lin 					bar_val |=
21716e0832faSShawn Lin 					((u64)hpdev->probed_bar[++i] << 32);
21726e0832faSShawn Lin 				else
21736e0832faSShawn Lin 					bar_val |= 0xffffffff00000000ULL;
21746e0832faSShawn Lin 
21756e0832faSShawn Lin 				bar_size = get_bar_size(bar_val);
21766e0832faSShawn Lin 
21776e0832faSShawn Lin 				if (bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64)
21786e0832faSShawn Lin 					hbus->high_mmio_space += bar_size;
21796e0832faSShawn Lin 				else
21806e0832faSShawn Lin 					hbus->low_mmio_space += bar_size;
21816e0832faSShawn Lin 			}
21826e0832faSShawn Lin 		}
21836e0832faSShawn Lin 	}
21846e0832faSShawn Lin 
21856e0832faSShawn Lin 	spin_unlock_irqrestore(&hbus->device_list_lock, flags);
21866e0832faSShawn Lin 	complete(event);
21876e0832faSShawn Lin }
21886e0832faSShawn Lin 
21896e0832faSShawn Lin /**
21906e0832faSShawn Lin  * prepopulate_bars() - Fill in BARs with defaults
21916e0832faSShawn Lin  * @hbus:	Root PCI bus, as understood by this driver
21926e0832faSShawn Lin  *
21936e0832faSShawn Lin  * The core PCI driver code seems much, much happier if the BARs
21946e0832faSShawn Lin  * for a device have values upon first scan. So fill them in.
21956e0832faSShawn Lin  * The algorithm below works down from large sizes to small,
21966e0832faSShawn Lin  * attempting to pack the assignments optimally. The assumption,
21976e0832faSShawn Lin  * enforced in other parts of the code, is that the beginning of
21986e0832faSShawn Lin  * the memory-mapped I/O space will be aligned on the largest
21996e0832faSShawn Lin  * BAR size.
22006e0832faSShawn Lin  */
prepopulate_bars(struct hv_pcibus_device * hbus)22016e0832faSShawn Lin static void prepopulate_bars(struct hv_pcibus_device *hbus)
22026e0832faSShawn Lin {
22036e0832faSShawn Lin 	resource_size_t high_size = 0;
22046e0832faSShawn Lin 	resource_size_t low_size = 0;
22056e0832faSShawn Lin 	resource_size_t high_base = 0;
22066e0832faSShawn Lin 	resource_size_t low_base = 0;
22076e0832faSShawn Lin 	resource_size_t bar_size;
22086e0832faSShawn Lin 	struct hv_pci_dev *hpdev;
22096e0832faSShawn Lin 	unsigned long flags;
22106e0832faSShawn Lin 	u64 bar_val;
22116e0832faSShawn Lin 	u32 command;
22126e0832faSShawn Lin 	bool high;
22136e0832faSShawn Lin 	int i;
22146e0832faSShawn Lin 
22156e0832faSShawn Lin 	if (hbus->low_mmio_space) {
22166e0832faSShawn Lin 		low_size = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space));
22176e0832faSShawn Lin 		low_base = hbus->low_mmio_res->start;
22186e0832faSShawn Lin 	}
22196e0832faSShawn Lin 
22206e0832faSShawn Lin 	if (hbus->high_mmio_space) {
22216e0832faSShawn Lin 		high_size = 1ULL <<
22226e0832faSShawn Lin 			(63 - __builtin_clzll(hbus->high_mmio_space));
22236e0832faSShawn Lin 		high_base = hbus->high_mmio_res->start;
22246e0832faSShawn Lin 	}
22256e0832faSShawn Lin 
22266e0832faSShawn Lin 	spin_lock_irqsave(&hbus->device_list_lock, flags);
22276e0832faSShawn Lin 
2228ac82fc83SDexuan Cui 	/*
2229ac82fc83SDexuan Cui 	 * Clear the memory enable bit, in case it's already set. This occurs
2230ac82fc83SDexuan Cui 	 * in the suspend path of hibernation, where the device is suspended,
2231ac82fc83SDexuan Cui 	 * resumed and suspended again: see hibernation_snapshot() and
2232ac82fc83SDexuan Cui 	 * hibernation_platform_enter().
2233ac82fc83SDexuan Cui 	 *
2234c77bfb54SBjorn Helgaas 	 * If the memory enable bit is already set, Hyper-V silently ignores
2235ac82fc83SDexuan Cui 	 * the below BAR updates, and the related PCI device driver can not
2236ac82fc83SDexuan Cui 	 * work, because reading from the device register(s) always returns
223714e04d0dSNaveen Naidu 	 * 0xFFFFFFFF (PCI_ERROR_RESPONSE).
2238ac82fc83SDexuan Cui 	 */
2239ac82fc83SDexuan Cui 	list_for_each_entry(hpdev, &hbus->children, list_entry) {
2240ac82fc83SDexuan Cui 		_hv_pcifront_read_config(hpdev, PCI_COMMAND, 2, &command);
2241ac82fc83SDexuan Cui 		command &= ~PCI_COMMAND_MEMORY;
2242ac82fc83SDexuan Cui 		_hv_pcifront_write_config(hpdev, PCI_COMMAND, 2, command);
2243ac82fc83SDexuan Cui 	}
2244ac82fc83SDexuan Cui 
22456e0832faSShawn Lin 	/* Pick addresses for the BARs. */
22466e0832faSShawn Lin 	do {
22476e0832faSShawn Lin 		list_for_each_entry(hpdev, &hbus->children, list_entry) {
2248c9c13ba4SDenis Efremov 			for (i = 0; i < PCI_STD_NUM_BARS; i++) {
22496e0832faSShawn Lin 				bar_val = hpdev->probed_bar[i];
22506e0832faSShawn Lin 				if (bar_val == 0)
22516e0832faSShawn Lin 					continue;
22526e0832faSShawn Lin 				high = bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64;
22536e0832faSShawn Lin 				if (high) {
22546e0832faSShawn Lin 					bar_val |=
22556e0832faSShawn Lin 						((u64)hpdev->probed_bar[i + 1]
22566e0832faSShawn Lin 						 << 32);
22576e0832faSShawn Lin 				} else {
22586e0832faSShawn Lin 					bar_val |= 0xffffffffULL << 32;
22596e0832faSShawn Lin 				}
22606e0832faSShawn Lin 				bar_size = get_bar_size(bar_val);
22616e0832faSShawn Lin 				if (high) {
22626e0832faSShawn Lin 					if (high_size != bar_size) {
22636e0832faSShawn Lin 						i++;
22646e0832faSShawn Lin 						continue;
22656e0832faSShawn Lin 					}
22666e0832faSShawn Lin 					_hv_pcifront_write_config(hpdev,
22676e0832faSShawn Lin 						PCI_BASE_ADDRESS_0 + (4 * i),
22686e0832faSShawn Lin 						4,
22696e0832faSShawn Lin 						(u32)(high_base & 0xffffff00));
22706e0832faSShawn Lin 					i++;
22716e0832faSShawn Lin 					_hv_pcifront_write_config(hpdev,
22726e0832faSShawn Lin 						PCI_BASE_ADDRESS_0 + (4 * i),
22736e0832faSShawn Lin 						4, (u32)(high_base >> 32));
22746e0832faSShawn Lin 					high_base += bar_size;
22756e0832faSShawn Lin 				} else {
22766e0832faSShawn Lin 					if (low_size != bar_size)
22776e0832faSShawn Lin 						continue;
22786e0832faSShawn Lin 					_hv_pcifront_write_config(hpdev,
22796e0832faSShawn Lin 						PCI_BASE_ADDRESS_0 + (4 * i),
22806e0832faSShawn Lin 						4,
22816e0832faSShawn Lin 						(u32)(low_base & 0xffffff00));
22826e0832faSShawn Lin 					low_base += bar_size;
22836e0832faSShawn Lin 				}
22846e0832faSShawn Lin 			}
22856e0832faSShawn Lin 			if (high_size <= 1 && low_size <= 1) {
228623e118a4SDexuan Cui 				/*
228723e118a4SDexuan Cui 				 * No need to set the PCI_COMMAND_MEMORY bit as
228823e118a4SDexuan Cui 				 * the core PCI driver doesn't require the bit
228923e118a4SDexuan Cui 				 * to be pre-set. Actually here we intentionally
229023e118a4SDexuan Cui 				 * keep the bit off so that the PCI BAR probing
229123e118a4SDexuan Cui 				 * in the core PCI driver doesn't cause Hyper-V
229223e118a4SDexuan Cui 				 * to unnecessarily unmap/map the virtual BARs
229323e118a4SDexuan Cui 				 * from/to the physical BARs multiple times.
229423e118a4SDexuan Cui 				 * This reduces the VM boot time significantly
229523e118a4SDexuan Cui 				 * if the BAR sizes are huge.
229623e118a4SDexuan Cui 				 */
22976e0832faSShawn Lin 				break;
22986e0832faSShawn Lin 			}
22996e0832faSShawn Lin 		}
23006e0832faSShawn Lin 
23016e0832faSShawn Lin 		high_size >>= 1;
23026e0832faSShawn Lin 		low_size >>= 1;
23036e0832faSShawn Lin 	}  while (high_size || low_size);
23046e0832faSShawn Lin 
23056e0832faSShawn Lin 	spin_unlock_irqrestore(&hbus->device_list_lock, flags);
23066e0832faSShawn Lin }
23076e0832faSShawn Lin 
2308a15f2c08SStephen Hemminger /*
2309a15f2c08SStephen Hemminger  * Assign entries in sysfs pci slot directory.
2310a15f2c08SStephen Hemminger  *
2311a15f2c08SStephen Hemminger  * Note that this function does not need to lock the children list
2312a15f2c08SStephen Hemminger  * because it is called from pci_devices_present_work which
2313a15f2c08SStephen Hemminger  * is serialized with hv_eject_device_work because they are on the
2314a15f2c08SStephen Hemminger  * same ordered workqueue. Therefore hbus->children list will not change
2315a15f2c08SStephen Hemminger  * even when pci_create_slot sleeps.
2316a15f2c08SStephen Hemminger  */
hv_pci_assign_slots(struct hv_pcibus_device * hbus)2317a15f2c08SStephen Hemminger static void hv_pci_assign_slots(struct hv_pcibus_device *hbus)
2318a15f2c08SStephen Hemminger {
2319a15f2c08SStephen Hemminger 	struct hv_pci_dev *hpdev;
2320a15f2c08SStephen Hemminger 	char name[SLOT_NAME_SIZE];
2321a15f2c08SStephen Hemminger 	int slot_nr;
2322a15f2c08SStephen Hemminger 
2323a15f2c08SStephen Hemminger 	list_for_each_entry(hpdev, &hbus->children, list_entry) {
2324a15f2c08SStephen Hemminger 		if (hpdev->pci_slot)
2325a15f2c08SStephen Hemminger 			continue;
2326a15f2c08SStephen Hemminger 
2327a15f2c08SStephen Hemminger 		slot_nr = PCI_SLOT(wslot_to_devfn(hpdev->desc.win_slot.slot));
2328a15f2c08SStephen Hemminger 		snprintf(name, SLOT_NAME_SIZE, "%u", hpdev->desc.ser);
2329418cb6c8SArnd Bergmann 		hpdev->pci_slot = pci_create_slot(hbus->bridge->bus, slot_nr,
2330a15f2c08SStephen Hemminger 					  name, NULL);
233154be5b8cSWei Yongjun 		if (IS_ERR(hpdev->pci_slot)) {
2332a15f2c08SStephen Hemminger 			pr_warn("pci_create slot %s failed\n", name);
233354be5b8cSWei Yongjun 			hpdev->pci_slot = NULL;
233454be5b8cSWei Yongjun 		}
2335a15f2c08SStephen Hemminger 	}
2336a15f2c08SStephen Hemminger }
2337a15f2c08SStephen Hemminger 
233815becc2bSDexuan Cui /*
233915becc2bSDexuan Cui  * Remove entries in sysfs pci slot directory.
234015becc2bSDexuan Cui  */
hv_pci_remove_slots(struct hv_pcibus_device * hbus)234115becc2bSDexuan Cui static void hv_pci_remove_slots(struct hv_pcibus_device *hbus)
234215becc2bSDexuan Cui {
234315becc2bSDexuan Cui 	struct hv_pci_dev *hpdev;
234415becc2bSDexuan Cui 
234515becc2bSDexuan Cui 	list_for_each_entry(hpdev, &hbus->children, list_entry) {
234615becc2bSDexuan Cui 		if (!hpdev->pci_slot)
234715becc2bSDexuan Cui 			continue;
234815becc2bSDexuan Cui 		pci_destroy_slot(hpdev->pci_slot);
234915becc2bSDexuan Cui 		hpdev->pci_slot = NULL;
235015becc2bSDexuan Cui 	}
235115becc2bSDexuan Cui }
235215becc2bSDexuan Cui 
2353999dd956SLong Li /*
2354999dd956SLong Li  * Set NUMA node for the devices on the bus
2355999dd956SLong Li  */
hv_pci_assign_numa_node(struct hv_pcibus_device * hbus)2356999dd956SLong Li static void hv_pci_assign_numa_node(struct hv_pcibus_device *hbus)
2357999dd956SLong Li {
2358999dd956SLong Li 	struct pci_dev *dev;
2359418cb6c8SArnd Bergmann 	struct pci_bus *bus = hbus->bridge->bus;
2360999dd956SLong Li 	struct hv_pci_dev *hv_dev;
2361999dd956SLong Li 
2362999dd956SLong Li 	list_for_each_entry(dev, &bus->devices, bus_list) {
2363999dd956SLong Li 		hv_dev = get_pcichild_wslot(hbus, devfn_to_wslot(dev->devfn));
2364999dd956SLong Li 		if (!hv_dev)
2365999dd956SLong Li 			continue;
2366999dd956SLong Li 
23673149efcdSLong Li 		if (hv_dev->desc.flags & HV_PCI_DEVICE_FLAG_NUMA_AFFINITY &&
23683149efcdSLong Li 		    hv_dev->desc.virtual_numa_node < num_possible_nodes())
23693149efcdSLong Li 			/*
23703149efcdSLong Li 			 * The kernel may boot with some NUMA nodes offline
23713149efcdSLong Li 			 * (e.g. in a KDUMP kernel) or with NUMA disabled via
23723149efcdSLong Li 			 * "numa=off". In those cases, adjust the host provided
23733149efcdSLong Li 			 * NUMA node to a valid NUMA node used by the kernel.
23743149efcdSLong Li 			 */
23753149efcdSLong Li 			set_dev_node(&dev->dev,
23763149efcdSLong Li 				     numa_map_to_online_node(
23773149efcdSLong Li 					     hv_dev->desc.virtual_numa_node));
2378999dd956SLong Li 
2379999dd956SLong Li 		put_pcichild(hv_dev);
2380999dd956SLong Li 	}
2381999dd956SLong Li }
2382999dd956SLong Li 
23836e0832faSShawn Lin /**
23846e0832faSShawn Lin  * create_root_hv_pci_bus() - Expose a new root PCI bus
23856e0832faSShawn Lin  * @hbus:	Root PCI bus, as understood by this driver
23866e0832faSShawn Lin  *
23876e0832faSShawn Lin  * Return: 0 on success, -errno on failure
23886e0832faSShawn Lin  */
create_root_hv_pci_bus(struct hv_pcibus_device * hbus)23896e0832faSShawn Lin static int create_root_hv_pci_bus(struct hv_pcibus_device *hbus)
23906e0832faSShawn Lin {
2391418cb6c8SArnd Bergmann 	int error;
2392418cb6c8SArnd Bergmann 	struct pci_host_bridge *bridge = hbus->bridge;
2393418cb6c8SArnd Bergmann 
2394418cb6c8SArnd Bergmann 	bridge->dev.parent = &hbus->hdev->device;
2395418cb6c8SArnd Bergmann 	bridge->sysdata = &hbus->sysdata;
2396418cb6c8SArnd Bergmann 	bridge->ops = &hv_pcifront_ops;
2397418cb6c8SArnd Bergmann 
2398418cb6c8SArnd Bergmann 	error = pci_scan_root_bus_bridge(bridge);
2399418cb6c8SArnd Bergmann 	if (error)
2400418cb6c8SArnd Bergmann 		return error;
24016e0832faSShawn Lin 
24026e0832faSShawn Lin 	pci_lock_rescan_remove();
2403999dd956SLong Li 	hv_pci_assign_numa_node(hbus);
2404418cb6c8SArnd Bergmann 	pci_bus_assign_resources(bridge->bus);
2405a15f2c08SStephen Hemminger 	hv_pci_assign_slots(hbus);
2406418cb6c8SArnd Bergmann 	pci_bus_add_devices(bridge->bus);
24076e0832faSShawn Lin 	pci_unlock_rescan_remove();
24086e0832faSShawn Lin 	hbus->state = hv_pcibus_installed;
24096e0832faSShawn Lin 	return 0;
24106e0832faSShawn Lin }
24116e0832faSShawn Lin 
24126e0832faSShawn Lin struct q_res_req_compl {
24136e0832faSShawn Lin 	struct completion host_event;
24146e0832faSShawn Lin 	struct hv_pci_dev *hpdev;
24156e0832faSShawn Lin };
24166e0832faSShawn Lin 
24176e0832faSShawn Lin /**
24186e0832faSShawn Lin  * q_resource_requirements() - Query Resource Requirements
24196e0832faSShawn Lin  * @context:		The completion context.
24206e0832faSShawn Lin  * @resp:		The response that came from the host.
24216e0832faSShawn Lin  * @resp_packet_size:	The size in bytes of resp.
24226e0832faSShawn Lin  *
24236e0832faSShawn Lin  * This function is invoked on completion of a Query Resource
24246e0832faSShawn Lin  * Requirements packet.
24256e0832faSShawn Lin  */
q_resource_requirements(void * context,struct pci_response * resp,int resp_packet_size)24266e0832faSShawn Lin static void q_resource_requirements(void *context, struct pci_response *resp,
24276e0832faSShawn Lin 				    int resp_packet_size)
24286e0832faSShawn Lin {
24296e0832faSShawn Lin 	struct q_res_req_compl *completion = context;
24306e0832faSShawn Lin 	struct pci_q_res_req_response *q_res_req =
24316e0832faSShawn Lin 		(struct pci_q_res_req_response *)resp;
24329937fa6dSAndrea Parri (Microsoft) 	s32 status;
24336e0832faSShawn Lin 	int i;
24346e0832faSShawn Lin 
24359937fa6dSAndrea Parri (Microsoft) 	status = (resp_packet_size < sizeof(*q_res_req)) ? -1 : resp->status;
24369937fa6dSAndrea Parri (Microsoft) 	if (status < 0) {
24376e0832faSShawn Lin 		dev_err(&completion->hpdev->hbus->hdev->device,
24386e0832faSShawn Lin 			"query resource requirements failed: %x\n",
24399937fa6dSAndrea Parri (Microsoft) 			status);
24406e0832faSShawn Lin 	} else {
2441c9c13ba4SDenis Efremov 		for (i = 0; i < PCI_STD_NUM_BARS; i++) {
24426e0832faSShawn Lin 			completion->hpdev->probed_bar[i] =
24436e0832faSShawn Lin 				q_res_req->probed_bar[i];
24446e0832faSShawn Lin 		}
24456e0832faSShawn Lin 	}
24466e0832faSShawn Lin 
24476e0832faSShawn Lin 	complete(&completion->host_event);
24486e0832faSShawn Lin }
24496e0832faSShawn Lin 
24506e0832faSShawn Lin /**
24516e0832faSShawn Lin  * new_pcichild_device() - Create a new child device
24526e0832faSShawn Lin  * @hbus:	The internal struct tracking this root PCI bus.
24536e0832faSShawn Lin  * @desc:	The information supplied so far from the host
24546e0832faSShawn Lin  *              about the device.
24556e0832faSShawn Lin  *
24566e0832faSShawn Lin  * This function creates the tracking structure for a new child
24576e0832faSShawn Lin  * device and kicks off the process of figuring out what it is.
24586e0832faSShawn Lin  *
24596e0832faSShawn Lin  * Return: Pointer to the new tracking struct
24606e0832faSShawn Lin  */
new_pcichild_device(struct hv_pcibus_device * hbus,struct hv_pcidev_description * desc)24616e0832faSShawn Lin static struct hv_pci_dev *new_pcichild_device(struct hv_pcibus_device *hbus,
2462f9ad0f36SLong Li 		struct hv_pcidev_description *desc)
24636e0832faSShawn Lin {
24646e0832faSShawn Lin 	struct hv_pci_dev *hpdev;
24656e0832faSShawn Lin 	struct pci_child_message *res_req;
24666e0832faSShawn Lin 	struct q_res_req_compl comp_pkt;
24676e0832faSShawn Lin 	struct {
24686e0832faSShawn Lin 		struct pci_packet init_packet;
24696e0832faSShawn Lin 		u8 buffer[sizeof(struct pci_child_message)];
24706e0832faSShawn Lin 	} pkt;
24716e0832faSShawn Lin 	unsigned long flags;
24726e0832faSShawn Lin 	int ret;
24736e0832faSShawn Lin 
24747403bd14SJia-Ju Bai 	hpdev = kzalloc(sizeof(*hpdev), GFP_KERNEL);
24756e0832faSShawn Lin 	if (!hpdev)
24766e0832faSShawn Lin 		return NULL;
24776e0832faSShawn Lin 
24786e0832faSShawn Lin 	hpdev->hbus = hbus;
24796e0832faSShawn Lin 
24806e0832faSShawn Lin 	memset(&pkt, 0, sizeof(pkt));
24816e0832faSShawn Lin 	init_completion(&comp_pkt.host_event);
24826e0832faSShawn Lin 	comp_pkt.hpdev = hpdev;
24836e0832faSShawn Lin 	pkt.init_packet.compl_ctxt = &comp_pkt;
24846e0832faSShawn Lin 	pkt.init_packet.completion_func = q_resource_requirements;
24856e0832faSShawn Lin 	res_req = (struct pci_child_message *)&pkt.init_packet.message;
24866e0832faSShawn Lin 	res_req->message_type.type = PCI_QUERY_RESOURCE_REQUIREMENTS;
24876e0832faSShawn Lin 	res_req->wslot.slot = desc->win_slot.slot;
24886e0832faSShawn Lin 
24896e0832faSShawn Lin 	ret = vmbus_sendpacket(hbus->hdev->channel, res_req,
24906e0832faSShawn Lin 			       sizeof(struct pci_child_message),
24916e0832faSShawn Lin 			       (unsigned long)&pkt.init_packet,
24926e0832faSShawn Lin 			       VM_PKT_DATA_INBAND,
24936e0832faSShawn Lin 			       VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
24946e0832faSShawn Lin 	if (ret)
24956e0832faSShawn Lin 		goto error;
24966e0832faSShawn Lin 
24976e0832faSShawn Lin 	if (wait_for_response(hbus->hdev, &comp_pkt.host_event))
24986e0832faSShawn Lin 		goto error;
24996e0832faSShawn Lin 
25006e0832faSShawn Lin 	hpdev->desc = *desc;
25016e0832faSShawn Lin 	refcount_set(&hpdev->refs, 1);
25026e0832faSShawn Lin 	get_pcichild(hpdev);
25036e0832faSShawn Lin 	spin_lock_irqsave(&hbus->device_list_lock, flags);
25046e0832faSShawn Lin 
25056e0832faSShawn Lin 	list_add_tail(&hpdev->list_entry, &hbus->children);
25066e0832faSShawn Lin 	spin_unlock_irqrestore(&hbus->device_list_lock, flags);
25076e0832faSShawn Lin 	return hpdev;
25086e0832faSShawn Lin 
25096e0832faSShawn Lin error:
25106e0832faSShawn Lin 	kfree(hpdev);
25116e0832faSShawn Lin 	return NULL;
25126e0832faSShawn Lin }
25136e0832faSShawn Lin 
25146e0832faSShawn Lin /**
25156e0832faSShawn Lin  * get_pcichild_wslot() - Find device from slot
25166e0832faSShawn Lin  * @hbus:	Root PCI bus, as understood by this driver
25176e0832faSShawn Lin  * @wslot:	Location on the bus
25186e0832faSShawn Lin  *
25196e0832faSShawn Lin  * This function looks up a PCI device and returns the internal
25206e0832faSShawn Lin  * representation of it.  It acquires a reference on it, so that
25216e0832faSShawn Lin  * the device won't be deleted while somebody is using it.  The
25226e0832faSShawn Lin  * caller is responsible for calling put_pcichild() to release
25236e0832faSShawn Lin  * this reference.
25246e0832faSShawn Lin  *
25256e0832faSShawn Lin  * Return:	Internal representation of a PCI device
25266e0832faSShawn Lin  */
get_pcichild_wslot(struct hv_pcibus_device * hbus,u32 wslot)25276e0832faSShawn Lin static struct hv_pci_dev *get_pcichild_wslot(struct hv_pcibus_device *hbus,
25286e0832faSShawn Lin 					     u32 wslot)
25296e0832faSShawn Lin {
25306e0832faSShawn Lin 	unsigned long flags;
25316e0832faSShawn Lin 	struct hv_pci_dev *iter, *hpdev = NULL;
25326e0832faSShawn Lin 
25336e0832faSShawn Lin 	spin_lock_irqsave(&hbus->device_list_lock, flags);
25346e0832faSShawn Lin 	list_for_each_entry(iter, &hbus->children, list_entry) {
25356e0832faSShawn Lin 		if (iter->desc.win_slot.slot == wslot) {
25366e0832faSShawn Lin 			hpdev = iter;
25376e0832faSShawn Lin 			get_pcichild(hpdev);
25386e0832faSShawn Lin 			break;
25396e0832faSShawn Lin 		}
25406e0832faSShawn Lin 	}
25416e0832faSShawn Lin 	spin_unlock_irqrestore(&hbus->device_list_lock, flags);
25426e0832faSShawn Lin 
25436e0832faSShawn Lin 	return hpdev;
25446e0832faSShawn Lin }
25456e0832faSShawn Lin 
25466e0832faSShawn Lin /**
25476e0832faSShawn Lin  * pci_devices_present_work() - Handle new list of child devices
25486e0832faSShawn Lin  * @work:	Work struct embedded in struct hv_dr_work
25496e0832faSShawn Lin  *
25506e0832faSShawn Lin  * "Bus Relations" is the Windows term for "children of this
25516e0832faSShawn Lin  * bus."  The terminology is preserved here for people trying to
25526e0832faSShawn Lin  * debug the interaction between Hyper-V and Linux.  This
25536e0832faSShawn Lin  * function is called when the parent partition reports a list
25546e0832faSShawn Lin  * of functions that should be observed under this PCI Express
25556e0832faSShawn Lin  * port (bus).
25566e0832faSShawn Lin  *
25576e0832faSShawn Lin  * This function updates the list, and must tolerate being
25586e0832faSShawn Lin  * called multiple times with the same information.  The typical
25596e0832faSShawn Lin  * number of child devices is one, with very atypical cases
25606e0832faSShawn Lin  * involving three or four, so the algorithms used here can be
25616e0832faSShawn Lin  * simple and inefficient.
25626e0832faSShawn Lin  *
25636e0832faSShawn Lin  * It must also treat the omission of a previously observed device as
25646e0832faSShawn Lin  * notification that the device no longer exists.
25656e0832faSShawn Lin  *
25666e0832faSShawn Lin  * Note that this function is serialized with hv_eject_device_work(),
25676e0832faSShawn Lin  * because both are pushed to the ordered workqueue hbus->wq.
25686e0832faSShawn Lin  */
pci_devices_present_work(struct work_struct * work)25696e0832faSShawn Lin static void pci_devices_present_work(struct work_struct *work)
25706e0832faSShawn Lin {
25716e0832faSShawn Lin 	u32 child_no;
25726e0832faSShawn Lin 	bool found;
2573f9ad0f36SLong Li 	struct hv_pcidev_description *new_desc;
25746e0832faSShawn Lin 	struct hv_pci_dev *hpdev;
25756e0832faSShawn Lin 	struct hv_pcibus_device *hbus;
25766e0832faSShawn Lin 	struct list_head removed;
25776e0832faSShawn Lin 	struct hv_dr_work *dr_wrk;
25786e0832faSShawn Lin 	struct hv_dr_state *dr = NULL;
25796e0832faSShawn Lin 	unsigned long flags;
25806e0832faSShawn Lin 
25816e0832faSShawn Lin 	dr_wrk = container_of(work, struct hv_dr_work, wrk);
25826e0832faSShawn Lin 	hbus = dr_wrk->bus;
25836e0832faSShawn Lin 	kfree(dr_wrk);
25846e0832faSShawn Lin 
25856e0832faSShawn Lin 	INIT_LIST_HEAD(&removed);
25866e0832faSShawn Lin 
25876e0832faSShawn Lin 	/* Pull this off the queue and process it if it was the last one. */
25886e0832faSShawn Lin 	spin_lock_irqsave(&hbus->device_list_lock, flags);
25896e0832faSShawn Lin 	while (!list_empty(&hbus->dr_list)) {
25906e0832faSShawn Lin 		dr = list_first_entry(&hbus->dr_list, struct hv_dr_state,
25916e0832faSShawn Lin 				      list_entry);
25926e0832faSShawn Lin 		list_del(&dr->list_entry);
25936e0832faSShawn Lin 
25946e0832faSShawn Lin 		/* Throw this away if the list still has stuff in it. */
25956e0832faSShawn Lin 		if (!list_empty(&hbus->dr_list)) {
25966e0832faSShawn Lin 			kfree(dr);
25976e0832faSShawn Lin 			continue;
25986e0832faSShawn Lin 		}
25996e0832faSShawn Lin 	}
26006e0832faSShawn Lin 	spin_unlock_irqrestore(&hbus->device_list_lock, flags);
26016e0832faSShawn Lin 
2602326dc2e1SLong Li 	if (!dr)
26036e0832faSShawn Lin 		return;
26046e0832faSShawn Lin 
2605067d6ec7SDexuan Cui 	mutex_lock(&hbus->state_lock);
2606067d6ec7SDexuan Cui 
26076e0832faSShawn Lin 	/* First, mark all existing children as reported missing. */
26086e0832faSShawn Lin 	spin_lock_irqsave(&hbus->device_list_lock, flags);
26096e0832faSShawn Lin 	list_for_each_entry(hpdev, &hbus->children, list_entry) {
26106e0832faSShawn Lin 		hpdev->reported_missing = true;
26116e0832faSShawn Lin 	}
26126e0832faSShawn Lin 	spin_unlock_irqrestore(&hbus->device_list_lock, flags);
26136e0832faSShawn Lin 
26146e0832faSShawn Lin 	/* Next, add back any reported devices. */
26156e0832faSShawn Lin 	for (child_no = 0; child_no < dr->device_count; child_no++) {
26166e0832faSShawn Lin 		found = false;
26176e0832faSShawn Lin 		new_desc = &dr->func[child_no];
26186e0832faSShawn Lin 
26196e0832faSShawn Lin 		spin_lock_irqsave(&hbus->device_list_lock, flags);
26206e0832faSShawn Lin 		list_for_each_entry(hpdev, &hbus->children, list_entry) {
26216e0832faSShawn Lin 			if ((hpdev->desc.win_slot.slot == new_desc->win_slot.slot) &&
26226e0832faSShawn Lin 			    (hpdev->desc.v_id == new_desc->v_id) &&
26236e0832faSShawn Lin 			    (hpdev->desc.d_id == new_desc->d_id) &&
26246e0832faSShawn Lin 			    (hpdev->desc.ser == new_desc->ser)) {
26256e0832faSShawn Lin 				hpdev->reported_missing = false;
26266e0832faSShawn Lin 				found = true;
26276e0832faSShawn Lin 			}
26286e0832faSShawn Lin 		}
26296e0832faSShawn Lin 		spin_unlock_irqrestore(&hbus->device_list_lock, flags);
26306e0832faSShawn Lin 
26316e0832faSShawn Lin 		if (!found) {
26326e0832faSShawn Lin 			hpdev = new_pcichild_device(hbus, new_desc);
26336e0832faSShawn Lin 			if (!hpdev)
26346e0832faSShawn Lin 				dev_err(&hbus->hdev->device,
26356e0832faSShawn Lin 					"couldn't record a child device.\n");
26366e0832faSShawn Lin 		}
26376e0832faSShawn Lin 	}
26386e0832faSShawn Lin 
26396e0832faSShawn Lin 	/* Move missing children to a list on the stack. */
26406e0832faSShawn Lin 	spin_lock_irqsave(&hbus->device_list_lock, flags);
26416e0832faSShawn Lin 	do {
26426e0832faSShawn Lin 		found = false;
26436e0832faSShawn Lin 		list_for_each_entry(hpdev, &hbus->children, list_entry) {
26446e0832faSShawn Lin 			if (hpdev->reported_missing) {
26456e0832faSShawn Lin 				found = true;
26466e0832faSShawn Lin 				put_pcichild(hpdev);
26476e0832faSShawn Lin 				list_move_tail(&hpdev->list_entry, &removed);
26486e0832faSShawn Lin 				break;
26496e0832faSShawn Lin 			}
26506e0832faSShawn Lin 		}
26516e0832faSShawn Lin 	} while (found);
26526e0832faSShawn Lin 	spin_unlock_irqrestore(&hbus->device_list_lock, flags);
26536e0832faSShawn Lin 
26546e0832faSShawn Lin 	/* Delete everything that should no longer exist. */
26556e0832faSShawn Lin 	while (!list_empty(&removed)) {
26566e0832faSShawn Lin 		hpdev = list_first_entry(&removed, struct hv_pci_dev,
26576e0832faSShawn Lin 					 list_entry);
26586e0832faSShawn Lin 		list_del(&hpdev->list_entry);
2659340d4556SDexuan Cui 
2660340d4556SDexuan Cui 		if (hpdev->pci_slot)
2661340d4556SDexuan Cui 			pci_destroy_slot(hpdev->pci_slot);
2662340d4556SDexuan Cui 
26636e0832faSShawn Lin 		put_pcichild(hpdev);
26646e0832faSShawn Lin 	}
26656e0832faSShawn Lin 
26666e0832faSShawn Lin 	switch (hbus->state) {
26676e0832faSShawn Lin 	case hv_pcibus_installed:
26686e0832faSShawn Lin 		/*
26696e0832faSShawn Lin 		 * Tell the core to rescan bus
26706e0832faSShawn Lin 		 * because there may have been changes.
26716e0832faSShawn Lin 		 */
26726e0832faSShawn Lin 		pci_lock_rescan_remove();
2673418cb6c8SArnd Bergmann 		pci_scan_child_bus(hbus->bridge->bus);
2674999dd956SLong Li 		hv_pci_assign_numa_node(hbus);
2675a15f2c08SStephen Hemminger 		hv_pci_assign_slots(hbus);
26766e0832faSShawn Lin 		pci_unlock_rescan_remove();
26776e0832faSShawn Lin 		break;
26786e0832faSShawn Lin 
26796e0832faSShawn Lin 	case hv_pcibus_init:
26806e0832faSShawn Lin 	case hv_pcibus_probed:
26816e0832faSShawn Lin 		survey_child_resources(hbus);
26826e0832faSShawn Lin 		break;
26836e0832faSShawn Lin 
26846e0832faSShawn Lin 	default:
26856e0832faSShawn Lin 		break;
26866e0832faSShawn Lin 	}
26876e0832faSShawn Lin 
2688067d6ec7SDexuan Cui 	mutex_unlock(&hbus->state_lock);
2689067d6ec7SDexuan Cui 
26906e0832faSShawn Lin 	kfree(dr);
26916e0832faSShawn Lin }
26926e0832faSShawn Lin 
26936e0832faSShawn Lin /**
2694f9ad0f36SLong Li  * hv_pci_start_relations_work() - Queue work to start device discovery
26956e0832faSShawn Lin  * @hbus:	Root PCI bus, as understood by this driver
2696f9ad0f36SLong Li  * @dr:		The list of children returned from host
26976e0832faSShawn Lin  *
2698f9ad0f36SLong Li  * Return:  0 on success, -errno on failure
26996e0832faSShawn Lin  */
hv_pci_start_relations_work(struct hv_pcibus_device * hbus,struct hv_dr_state * dr)2700f9ad0f36SLong Li static int hv_pci_start_relations_work(struct hv_pcibus_device *hbus,
2701f9ad0f36SLong Li 				       struct hv_dr_state *dr)
27026e0832faSShawn Lin {
27036e0832faSShawn Lin 	struct hv_dr_work *dr_wrk;
27046e0832faSShawn Lin 	unsigned long flags;
27056e0832faSShawn Lin 	bool pending_dr;
27066e0832faSShawn Lin 
2707ac82fc83SDexuan Cui 	if (hbus->state == hv_pcibus_removing) {
2708ac82fc83SDexuan Cui 		dev_info(&hbus->hdev->device,
2709ac82fc83SDexuan Cui 			 "PCI VMBus BUS_RELATIONS: ignored\n");
2710f9ad0f36SLong Li 		return -ENOENT;
2711ac82fc83SDexuan Cui 	}
2712ac82fc83SDexuan Cui 
27136e0832faSShawn Lin 	dr_wrk = kzalloc(sizeof(*dr_wrk), GFP_NOWAIT);
27146e0832faSShawn Lin 	if (!dr_wrk)
2715f9ad0f36SLong Li 		return -ENOMEM;
27166e0832faSShawn Lin 
27176e0832faSShawn Lin 	INIT_WORK(&dr_wrk->wrk, pci_devices_present_work);
27186e0832faSShawn Lin 	dr_wrk->bus = hbus;
27196e0832faSShawn Lin 
27206e0832faSShawn Lin 	spin_lock_irqsave(&hbus->device_list_lock, flags);
27216e0832faSShawn Lin 	/*
27226e0832faSShawn Lin 	 * If pending_dr is true, we have already queued a work,
27236e0832faSShawn Lin 	 * which will see the new dr. Otherwise, we need to
27246e0832faSShawn Lin 	 * queue a new work.
27256e0832faSShawn Lin 	 */
27266e0832faSShawn Lin 	pending_dr = !list_empty(&hbus->dr_list);
27276e0832faSShawn Lin 	list_add_tail(&dr->list_entry, &hbus->dr_list);
27286e0832faSShawn Lin 	spin_unlock_irqrestore(&hbus->device_list_lock, flags);
27296e0832faSShawn Lin 
2730326dc2e1SLong Li 	if (pending_dr)
27316e0832faSShawn Lin 		kfree(dr_wrk);
2732326dc2e1SLong Li 	else
27336e0832faSShawn Lin 		queue_work(hbus->wq, &dr_wrk->wrk);
2734f9ad0f36SLong Li 
2735f9ad0f36SLong Li 	return 0;
2736f9ad0f36SLong Li }
2737f9ad0f36SLong Li 
2738f9ad0f36SLong Li /**
2739f9ad0f36SLong Li  * hv_pci_devices_present() - Handle list of new children
2740f9ad0f36SLong Li  * @hbus:      Root PCI bus, as understood by this driver
2741f9ad0f36SLong Li  * @relations: Packet from host listing children
2742f9ad0f36SLong Li  *
2743f9ad0f36SLong Li  * Process a new list of devices on the bus. The list of devices is
2744f9ad0f36SLong Li  * discovered by VSP and sent to us via VSP message PCI_BUS_RELATIONS,
2745f9ad0f36SLong Li  * whenever a new list of devices for this bus appears.
2746f9ad0f36SLong Li  */
hv_pci_devices_present(struct hv_pcibus_device * hbus,struct pci_bus_relations * relations)2747f9ad0f36SLong Li static void hv_pci_devices_present(struct hv_pcibus_device *hbus,
2748f9ad0f36SLong Li 				   struct pci_bus_relations *relations)
2749f9ad0f36SLong Li {
2750f9ad0f36SLong Li 	struct hv_dr_state *dr;
2751f9ad0f36SLong Li 	int i;
2752f9ad0f36SLong Li 
2753d0684fd0SGustavo A. R. Silva 	dr = kzalloc(struct_size(dr, func, relations->device_count),
2754d0684fd0SGustavo A. R. Silva 		     GFP_NOWAIT);
2755f9ad0f36SLong Li 	if (!dr)
2756f9ad0f36SLong Li 		return;
2757f9ad0f36SLong Li 
2758f9ad0f36SLong Li 	dr->device_count = relations->device_count;
2759f9ad0f36SLong Li 	for (i = 0; i < dr->device_count; i++) {
2760f9ad0f36SLong Li 		dr->func[i].v_id = relations->func[i].v_id;
2761f9ad0f36SLong Li 		dr->func[i].d_id = relations->func[i].d_id;
2762f9ad0f36SLong Li 		dr->func[i].rev = relations->func[i].rev;
2763f9ad0f36SLong Li 		dr->func[i].prog_intf = relations->func[i].prog_intf;
2764f9ad0f36SLong Li 		dr->func[i].subclass = relations->func[i].subclass;
2765f9ad0f36SLong Li 		dr->func[i].base_class = relations->func[i].base_class;
2766f9ad0f36SLong Li 		dr->func[i].subsystem_id = relations->func[i].subsystem_id;
2767f9ad0f36SLong Li 		dr->func[i].win_slot = relations->func[i].win_slot;
2768f9ad0f36SLong Li 		dr->func[i].ser = relations->func[i].ser;
2769f9ad0f36SLong Li 	}
2770f9ad0f36SLong Li 
2771f9ad0f36SLong Li 	if (hv_pci_start_relations_work(hbus, dr))
2772f9ad0f36SLong Li 		kfree(dr);
27736e0832faSShawn Lin }
27746e0832faSShawn Lin 
27756e0832faSShawn Lin /**
2776999dd956SLong Li  * hv_pci_devices_present2() - Handle list of new children
2777999dd956SLong Li  * @hbus:	Root PCI bus, as understood by this driver
2778999dd956SLong Li  * @relations:	Packet from host listing children
2779999dd956SLong Li  *
2780999dd956SLong Li  * This function is the v2 version of hv_pci_devices_present()
2781999dd956SLong Li  */
hv_pci_devices_present2(struct hv_pcibus_device * hbus,struct pci_bus_relations2 * relations)2782999dd956SLong Li static void hv_pci_devices_present2(struct hv_pcibus_device *hbus,
2783999dd956SLong Li 				    struct pci_bus_relations2 *relations)
2784999dd956SLong Li {
2785999dd956SLong Li 	struct hv_dr_state *dr;
2786999dd956SLong Li 	int i;
2787999dd956SLong Li 
2788d0684fd0SGustavo A. R. Silva 	dr = kzalloc(struct_size(dr, func, relations->device_count),
2789d0684fd0SGustavo A. R. Silva 		     GFP_NOWAIT);
2790999dd956SLong Li 	if (!dr)
2791999dd956SLong Li 		return;
2792999dd956SLong Li 
2793999dd956SLong Li 	dr->device_count = relations->device_count;
2794999dd956SLong Li 	for (i = 0; i < dr->device_count; i++) {
2795999dd956SLong Li 		dr->func[i].v_id = relations->func[i].v_id;
2796999dd956SLong Li 		dr->func[i].d_id = relations->func[i].d_id;
2797999dd956SLong Li 		dr->func[i].rev = relations->func[i].rev;
2798999dd956SLong Li 		dr->func[i].prog_intf = relations->func[i].prog_intf;
2799999dd956SLong Li 		dr->func[i].subclass = relations->func[i].subclass;
2800999dd956SLong Li 		dr->func[i].base_class = relations->func[i].base_class;
2801999dd956SLong Li 		dr->func[i].subsystem_id = relations->func[i].subsystem_id;
2802999dd956SLong Li 		dr->func[i].win_slot = relations->func[i].win_slot;
2803999dd956SLong Li 		dr->func[i].ser = relations->func[i].ser;
2804999dd956SLong Li 		dr->func[i].flags = relations->func[i].flags;
2805999dd956SLong Li 		dr->func[i].virtual_numa_node =
2806999dd956SLong Li 			relations->func[i].virtual_numa_node;
2807999dd956SLong Li 	}
2808999dd956SLong Li 
2809999dd956SLong Li 	if (hv_pci_start_relations_work(hbus, dr))
2810999dd956SLong Li 		kfree(dr);
2811999dd956SLong Li }
2812999dd956SLong Li 
2813999dd956SLong Li /**
28146e0832faSShawn Lin  * hv_eject_device_work() - Asynchronously handles ejection
28156e0832faSShawn Lin  * @work:	Work struct embedded in internal device struct
28166e0832faSShawn Lin  *
28176e0832faSShawn Lin  * This function handles ejecting a device.  Windows will
28186e0832faSShawn Lin  * attempt to gracefully eject a device, waiting 60 seconds to
28196e0832faSShawn Lin  * hear back from the guest OS that this completed successfully.
28206e0832faSShawn Lin  * If this timer expires, the device will be forcibly removed.
28216e0832faSShawn Lin  */
hv_eject_device_work(struct work_struct * work)28226e0832faSShawn Lin static void hv_eject_device_work(struct work_struct *work)
28236e0832faSShawn Lin {
28246e0832faSShawn Lin 	struct pci_eject_response *ejct_pkt;
28254df591b2SDexuan Cui 	struct hv_pcibus_device *hbus;
28266e0832faSShawn Lin 	struct hv_pci_dev *hpdev;
28276e0832faSShawn Lin 	struct pci_dev *pdev;
28286e0832faSShawn Lin 	unsigned long flags;
28296e0832faSShawn Lin 	int wslot;
28306e0832faSShawn Lin 	struct {
28316e0832faSShawn Lin 		struct pci_packet pkt;
28326e0832faSShawn Lin 		u8 buffer[sizeof(struct pci_eject_response)];
28336e0832faSShawn Lin 	} ctxt;
28346e0832faSShawn Lin 
28356e0832faSShawn Lin 	hpdev = container_of(work, struct hv_pci_dev, wrk);
28364df591b2SDexuan Cui 	hbus = hpdev->hbus;
28376e0832faSShawn Lin 
2838067d6ec7SDexuan Cui 	mutex_lock(&hbus->state_lock);
2839067d6ec7SDexuan Cui 
28406e0832faSShawn Lin 	/*
28416e0832faSShawn Lin 	 * Ejection can come before or after the PCI bus has been set up, so
28426e0832faSShawn Lin 	 * attempt to find it and tear down the bus state, if it exists.  This
2843418cb6c8SArnd Bergmann 	 * must be done without constructs like pci_domain_nr(hbus->bridge->bus)
2844418cb6c8SArnd Bergmann 	 * because hbus->bridge->bus may not exist yet.
28456e0832faSShawn Lin 	 */
28466e0832faSShawn Lin 	wslot = wslot_to_devfn(hpdev->desc.win_slot.slot);
284738c0d266SBoqun Feng 	pdev = pci_get_domain_bus_and_slot(hbus->bridge->domain_nr, 0, wslot);
28486e0832faSShawn Lin 	if (pdev) {
28496e0832faSShawn Lin 		pci_lock_rescan_remove();
28506e0832faSShawn Lin 		pci_stop_and_remove_bus_device(pdev);
28516e0832faSShawn Lin 		pci_dev_put(pdev);
28526e0832faSShawn Lin 		pci_unlock_rescan_remove();
28536e0832faSShawn Lin 	}
28546e0832faSShawn Lin 
28554df591b2SDexuan Cui 	spin_lock_irqsave(&hbus->device_list_lock, flags);
28566e0832faSShawn Lin 	list_del(&hpdev->list_entry);
28574df591b2SDexuan Cui 	spin_unlock_irqrestore(&hbus->device_list_lock, flags);
28586e0832faSShawn Lin 
2859a15f2c08SStephen Hemminger 	if (hpdev->pci_slot)
2860a15f2c08SStephen Hemminger 		pci_destroy_slot(hpdev->pci_slot);
2861a15f2c08SStephen Hemminger 
28626e0832faSShawn Lin 	memset(&ctxt, 0, sizeof(ctxt));
28636e0832faSShawn Lin 	ejct_pkt = (struct pci_eject_response *)&ctxt.pkt.message;
28646e0832faSShawn Lin 	ejct_pkt->message_type.type = PCI_EJECTION_COMPLETE;
28656e0832faSShawn Lin 	ejct_pkt->wslot.slot = hpdev->desc.win_slot.slot;
28664df591b2SDexuan Cui 	vmbus_sendpacket(hbus->hdev->channel, ejct_pkt,
2867de5ddb7dSAndrea Parri (Microsoft) 			 sizeof(*ejct_pkt), 0,
28686e0832faSShawn Lin 			 VM_PKT_DATA_INBAND, 0);
28696e0832faSShawn Lin 
287005f151a7SDexuan Cui 	/* For the get_pcichild() in hv_pci_eject_device() */
287105f151a7SDexuan Cui 	put_pcichild(hpdev);
287205f151a7SDexuan Cui 	/* For the two refs got in new_pcichild_device() */
28736e0832faSShawn Lin 	put_pcichild(hpdev);
28746e0832faSShawn Lin 	put_pcichild(hpdev);
28754df591b2SDexuan Cui 	/* hpdev has been freed. Do not use it any more. */
2876067d6ec7SDexuan Cui 
2877067d6ec7SDexuan Cui 	mutex_unlock(&hbus->state_lock);
28786e0832faSShawn Lin }
28796e0832faSShawn Lin 
28806e0832faSShawn Lin /**
28816e0832faSShawn Lin  * hv_pci_eject_device() - Handles device ejection
28826e0832faSShawn Lin  * @hpdev:	Internal device tracking struct
28836e0832faSShawn Lin  *
28846e0832faSShawn Lin  * This function is invoked when an ejection packet arrives.  It
28856e0832faSShawn Lin  * just schedules work so that we don't re-enter the packet
28866e0832faSShawn Lin  * delivery code handling the ejection.
28876e0832faSShawn Lin  */
hv_pci_eject_device(struct hv_pci_dev * hpdev)28886e0832faSShawn Lin static void hv_pci_eject_device(struct hv_pci_dev *hpdev)
28896e0832faSShawn Lin {
2890ac82fc83SDexuan Cui 	struct hv_pcibus_device *hbus = hpdev->hbus;
2891ac82fc83SDexuan Cui 	struct hv_device *hdev = hbus->hdev;
2892ac82fc83SDexuan Cui 
2893ac82fc83SDexuan Cui 	if (hbus->state == hv_pcibus_removing) {
2894ac82fc83SDexuan Cui 		dev_info(&hdev->device, "PCI VMBus EJECT: ignored\n");
2895ac82fc83SDexuan Cui 		return;
2896ac82fc83SDexuan Cui 	}
2897ac82fc83SDexuan Cui 
28986e0832faSShawn Lin 	get_pcichild(hpdev);
28996e0832faSShawn Lin 	INIT_WORK(&hpdev->wrk, hv_eject_device_work);
2900ac82fc83SDexuan Cui 	queue_work(hbus->wq, &hpdev->wrk);
29016e0832faSShawn Lin }
29026e0832faSShawn Lin 
29036e0832faSShawn Lin /**
29046e0832faSShawn Lin  * hv_pci_onchannelcallback() - Handles incoming packets
29056e0832faSShawn Lin  * @context:	Internal bus tracking struct
29066e0832faSShawn Lin  *
29076e0832faSShawn Lin  * This function is invoked whenever the host sends a packet to
29086e0832faSShawn Lin  * this channel (which is private to this root PCI bus).
29096e0832faSShawn Lin  */
hv_pci_onchannelcallback(void * context)29106e0832faSShawn Lin static void hv_pci_onchannelcallback(void *context)
29116e0832faSShawn Lin {
29126e0832faSShawn Lin 	const int packet_size = 0x100;
29136e0832faSShawn Lin 	int ret;
29146e0832faSShawn Lin 	struct hv_pcibus_device *hbus = context;
2915de5ddb7dSAndrea Parri (Microsoft) 	struct vmbus_channel *chan = hbus->hdev->channel;
29166e0832faSShawn Lin 	u32 bytes_recvd;
2917de5ddb7dSAndrea Parri (Microsoft) 	u64 req_id, req_addr;
29186e0832faSShawn Lin 	struct vmpacket_descriptor *desc;
29196e0832faSShawn Lin 	unsigned char *buffer;
29206e0832faSShawn Lin 	int bufferlen = packet_size;
29216e0832faSShawn Lin 	struct pci_packet *comp_packet;
29226e0832faSShawn Lin 	struct pci_response *response;
29236e0832faSShawn Lin 	struct pci_incoming_message *new_message;
29246e0832faSShawn Lin 	struct pci_bus_relations *bus_rel;
2925999dd956SLong Li 	struct pci_bus_relations2 *bus_rel2;
2926e5d2f910SDexuan Cui 	struct pci_dev_inval_block *inval;
29276e0832faSShawn Lin 	struct pci_dev_incoming *dev_message;
29286e0832faSShawn Lin 	struct hv_pci_dev *hpdev;
2929a765ed47SAndrea Parri (Microsoft) 	unsigned long flags;
29306e0832faSShawn Lin 
29316e0832faSShawn Lin 	buffer = kmalloc(bufferlen, GFP_ATOMIC);
29326e0832faSShawn Lin 	if (!buffer)
29336e0832faSShawn Lin 		return;
29346e0832faSShawn Lin 
29356e0832faSShawn Lin 	while (1) {
2936de5ddb7dSAndrea Parri (Microsoft) 		ret = vmbus_recvpacket_raw(chan, buffer, bufferlen,
2937de5ddb7dSAndrea Parri (Microsoft) 					   &bytes_recvd, &req_id);
29386e0832faSShawn Lin 
29396e0832faSShawn Lin 		if (ret == -ENOBUFS) {
29406e0832faSShawn Lin 			kfree(buffer);
29416e0832faSShawn Lin 			/* Handle large packet */
29426e0832faSShawn Lin 			bufferlen = bytes_recvd;
29436e0832faSShawn Lin 			buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
29446e0832faSShawn Lin 			if (!buffer)
29456e0832faSShawn Lin 				return;
29466e0832faSShawn Lin 			continue;
29476e0832faSShawn Lin 		}
29486e0832faSShawn Lin 
29496e0832faSShawn Lin 		/* Zero length indicates there are no more packets. */
29506e0832faSShawn Lin 		if (ret || !bytes_recvd)
29516e0832faSShawn Lin 			break;
29526e0832faSShawn Lin 
29536e0832faSShawn Lin 		/*
29546e0832faSShawn Lin 		 * All incoming packets must be at least as large as a
29556e0832faSShawn Lin 		 * response.
29566e0832faSShawn Lin 		 */
29576e0832faSShawn Lin 		if (bytes_recvd <= sizeof(struct pci_response))
29586e0832faSShawn Lin 			continue;
29596e0832faSShawn Lin 		desc = (struct vmpacket_descriptor *)buffer;
29606e0832faSShawn Lin 
29616e0832faSShawn Lin 		switch (desc->type) {
29626e0832faSShawn Lin 		case VM_PKT_COMP:
29636e0832faSShawn Lin 
2964a765ed47SAndrea Parri (Microsoft) 			lock_requestor(chan, flags);
2965a765ed47SAndrea Parri (Microsoft) 			req_addr = __vmbus_request_addr_match(chan, req_id,
2966a765ed47SAndrea Parri (Microsoft) 							      VMBUS_RQST_ADDR_ANY);
2967de5ddb7dSAndrea Parri (Microsoft) 			if (req_addr == VMBUS_RQST_ERROR) {
2968a765ed47SAndrea Parri (Microsoft) 				unlock_requestor(chan, flags);
2969de5ddb7dSAndrea Parri (Microsoft) 				dev_err(&hbus->hdev->device,
2970de5ddb7dSAndrea Parri (Microsoft) 					"Invalid transaction ID %llx\n",
2971de5ddb7dSAndrea Parri (Microsoft) 					req_id);
2972de5ddb7dSAndrea Parri (Microsoft) 				break;
2973de5ddb7dSAndrea Parri (Microsoft) 			}
2974de5ddb7dSAndrea Parri (Microsoft) 			comp_packet = (struct pci_packet *)req_addr;
29756e0832faSShawn Lin 			response = (struct pci_response *)buffer;
2976a765ed47SAndrea Parri (Microsoft) 			/*
2977a765ed47SAndrea Parri (Microsoft) 			 * Call ->completion_func() within the critical section to make
2978a765ed47SAndrea Parri (Microsoft) 			 * sure that the packet pointer is still valid during the call:
2979a765ed47SAndrea Parri (Microsoft) 			 * here 'valid' means that there's a task still waiting for the
2980a765ed47SAndrea Parri (Microsoft) 			 * completion, and that the packet data is still on the waiting
2981a765ed47SAndrea Parri (Microsoft) 			 * task's stack.  Cf. hv_compose_msi_msg().
2982a765ed47SAndrea Parri (Microsoft) 			 */
29836e0832faSShawn Lin 			comp_packet->completion_func(comp_packet->compl_ctxt,
29846e0832faSShawn Lin 						     response,
29856e0832faSShawn Lin 						     bytes_recvd);
2986a765ed47SAndrea Parri (Microsoft) 			unlock_requestor(chan, flags);
29876e0832faSShawn Lin 			break;
29886e0832faSShawn Lin 
29896e0832faSShawn Lin 		case VM_PKT_DATA_INBAND:
29906e0832faSShawn Lin 
29916e0832faSShawn Lin 			new_message = (struct pci_incoming_message *)buffer;
29926e0832faSShawn Lin 			switch (new_message->message_type.type) {
29936e0832faSShawn Lin 			case PCI_BUS_RELATIONS:
29946e0832faSShawn Lin 
29956e0832faSShawn Lin 				bus_rel = (struct pci_bus_relations *)buffer;
29969937fa6dSAndrea Parri (Microsoft) 				if (bytes_recvd < sizeof(*bus_rel) ||
29979937fa6dSAndrea Parri (Microsoft) 				    bytes_recvd <
2998d0684fd0SGustavo A. R. Silva 					struct_size(bus_rel, func,
2999d0684fd0SGustavo A. R. Silva 						    bus_rel->device_count)) {
30006e0832faSShawn Lin 					dev_err(&hbus->hdev->device,
30016e0832faSShawn Lin 						"bus relations too small\n");
30026e0832faSShawn Lin 					break;
30036e0832faSShawn Lin 				}
30046e0832faSShawn Lin 
30056e0832faSShawn Lin 				hv_pci_devices_present(hbus, bus_rel);
30066e0832faSShawn Lin 				break;
30076e0832faSShawn Lin 
3008999dd956SLong Li 			case PCI_BUS_RELATIONS2:
3009999dd956SLong Li 
3010999dd956SLong Li 				bus_rel2 = (struct pci_bus_relations2 *)buffer;
30119937fa6dSAndrea Parri (Microsoft) 				if (bytes_recvd < sizeof(*bus_rel2) ||
30129937fa6dSAndrea Parri (Microsoft) 				    bytes_recvd <
3013d0684fd0SGustavo A. R. Silva 					struct_size(bus_rel2, func,
3014d0684fd0SGustavo A. R. Silva 						    bus_rel2->device_count)) {
3015999dd956SLong Li 					dev_err(&hbus->hdev->device,
3016999dd956SLong Li 						"bus relations v2 too small\n");
3017999dd956SLong Li 					break;
3018999dd956SLong Li 				}
3019999dd956SLong Li 
3020999dd956SLong Li 				hv_pci_devices_present2(hbus, bus_rel2);
3021999dd956SLong Li 				break;
3022999dd956SLong Li 
30236e0832faSShawn Lin 			case PCI_EJECT:
30246e0832faSShawn Lin 
30256e0832faSShawn Lin 				dev_message = (struct pci_dev_incoming *)buffer;
30269937fa6dSAndrea Parri (Microsoft) 				if (bytes_recvd < sizeof(*dev_message)) {
30279937fa6dSAndrea Parri (Microsoft) 					dev_err(&hbus->hdev->device,
30289937fa6dSAndrea Parri (Microsoft) 						"eject message too small\n");
30299937fa6dSAndrea Parri (Microsoft) 					break;
30309937fa6dSAndrea Parri (Microsoft) 				}
30316e0832faSShawn Lin 				hpdev = get_pcichild_wslot(hbus,
30326e0832faSShawn Lin 						      dev_message->wslot.slot);
30336e0832faSShawn Lin 				if (hpdev) {
30346e0832faSShawn Lin 					hv_pci_eject_device(hpdev);
30356e0832faSShawn Lin 					put_pcichild(hpdev);
30366e0832faSShawn Lin 				}
30376e0832faSShawn Lin 				break;
30386e0832faSShawn Lin 
3039e5d2f910SDexuan Cui 			case PCI_INVALIDATE_BLOCK:
3040e5d2f910SDexuan Cui 
3041e5d2f910SDexuan Cui 				inval = (struct pci_dev_inval_block *)buffer;
30429937fa6dSAndrea Parri (Microsoft) 				if (bytes_recvd < sizeof(*inval)) {
30439937fa6dSAndrea Parri (Microsoft) 					dev_err(&hbus->hdev->device,
30449937fa6dSAndrea Parri (Microsoft) 						"invalidate message too small\n");
30459937fa6dSAndrea Parri (Microsoft) 					break;
30469937fa6dSAndrea Parri (Microsoft) 				}
3047e5d2f910SDexuan Cui 				hpdev = get_pcichild_wslot(hbus,
3048e5d2f910SDexuan Cui 							   inval->wslot.slot);
3049e5d2f910SDexuan Cui 				if (hpdev) {
3050e5d2f910SDexuan Cui 					if (hpdev->block_invalidate) {
3051e5d2f910SDexuan Cui 						hpdev->block_invalidate(
3052e5d2f910SDexuan Cui 						    hpdev->invalidate_context,
3053e5d2f910SDexuan Cui 						    inval->block_mask);
3054e5d2f910SDexuan Cui 					}
3055e5d2f910SDexuan Cui 					put_pcichild(hpdev);
3056e5d2f910SDexuan Cui 				}
3057e5d2f910SDexuan Cui 				break;
3058e5d2f910SDexuan Cui 
30596e0832faSShawn Lin 			default:
30606e0832faSShawn Lin 				dev_warn(&hbus->hdev->device,
30616e0832faSShawn Lin 					"Unimplemented protocol message %x\n",
30626e0832faSShawn Lin 					new_message->message_type.type);
30636e0832faSShawn Lin 				break;
30646e0832faSShawn Lin 			}
30656e0832faSShawn Lin 			break;
30666e0832faSShawn Lin 
30676e0832faSShawn Lin 		default:
30686e0832faSShawn Lin 			dev_err(&hbus->hdev->device,
30696e0832faSShawn Lin 				"unhandled packet type %d, tid %llx len %d\n",
30706e0832faSShawn Lin 				desc->type, req_id, bytes_recvd);
30716e0832faSShawn Lin 			break;
30726e0832faSShawn Lin 		}
30736e0832faSShawn Lin 	}
30746e0832faSShawn Lin 
30756e0832faSShawn Lin 	kfree(buffer);
30766e0832faSShawn Lin }
30776e0832faSShawn Lin 
30786e0832faSShawn Lin /**
30796e0832faSShawn Lin  * hv_pci_protocol_negotiation() - Set up protocol
30806d2730cbSKrzysztof Wilczyński  * @hdev:		VMBus's tracking struct for this root PCI bus.
30816d2730cbSKrzysztof Wilczyński  * @version:		Array of supported channel protocol versions in
30826d2730cbSKrzysztof Wilczyński  *			the order of probing - highest go first.
30836d2730cbSKrzysztof Wilczyński  * @num_version:	Number of elements in the version array.
30846e0832faSShawn Lin  *
30856e0832faSShawn Lin  * This driver is intended to support running on Windows 10
30866e0832faSShawn Lin  * (server) and later versions. It will not run on earlier
30876e0832faSShawn Lin  * versions, as they assume that many of the operations which
30886e0832faSShawn Lin  * Linux needs accomplished with a spinlock held were done via
30896e0832faSShawn Lin  * asynchronous messaging via VMBus.  Windows 10 increases the
30906e0832faSShawn Lin  * surface area of PCI emulation so that these actions can take
30916e0832faSShawn Lin  * place by suspending a virtual processor for their duration.
30926e0832faSShawn Lin  *
30936e0832faSShawn Lin  * This function negotiates the channel protocol version,
30946e0832faSShawn Lin  * failing if the host doesn't support the necessary protocol
30956e0832faSShawn Lin  * level.
30966e0832faSShawn Lin  */
hv_pci_protocol_negotiation(struct hv_device * hdev,enum pci_protocol_version_t version[],int num_version)3097a8e37506SDexuan Cui static int hv_pci_protocol_negotiation(struct hv_device *hdev,
3098a8e37506SDexuan Cui 				       enum pci_protocol_version_t version[],
3099a8e37506SDexuan Cui 				       int num_version)
31006e0832faSShawn Lin {
310114ef39fdSDexuan Cui 	struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
31026e0832faSShawn Lin 	struct pci_version_request *version_req;
31036e0832faSShawn Lin 	struct hv_pci_compl comp_pkt;
31046e0832faSShawn Lin 	struct pci_packet *pkt;
31056e0832faSShawn Lin 	int ret;
31066e0832faSShawn Lin 	int i;
31076e0832faSShawn Lin 
31086e0832faSShawn Lin 	/*
31096e0832faSShawn Lin 	 * Initiate the handshake with the host and negotiate
31106e0832faSShawn Lin 	 * a version that the host can support. We start with the
31116e0832faSShawn Lin 	 * highest version number and go down if the host cannot
31126e0832faSShawn Lin 	 * support it.
31136e0832faSShawn Lin 	 */
31146e0832faSShawn Lin 	pkt = kzalloc(sizeof(*pkt) + sizeof(*version_req), GFP_KERNEL);
31156e0832faSShawn Lin 	if (!pkt)
31166e0832faSShawn Lin 		return -ENOMEM;
31176e0832faSShawn Lin 
31186e0832faSShawn Lin 	init_completion(&comp_pkt.host_event);
31196e0832faSShawn Lin 	pkt->completion_func = hv_pci_generic_compl;
31206e0832faSShawn Lin 	pkt->compl_ctxt = &comp_pkt;
31216e0832faSShawn Lin 	version_req = (struct pci_version_request *)&pkt->message;
31226e0832faSShawn Lin 	version_req->message_type.type = PCI_QUERY_PROTOCOL_VERSION;
31236e0832faSShawn Lin 
3124a8e37506SDexuan Cui 	for (i = 0; i < num_version; i++) {
3125a8e37506SDexuan Cui 		version_req->protocol_version = version[i];
31266e0832faSShawn Lin 		ret = vmbus_sendpacket(hdev->channel, version_req,
31276e0832faSShawn Lin 				sizeof(struct pci_version_request),
31286e0832faSShawn Lin 				(unsigned long)pkt, VM_PKT_DATA_INBAND,
31296e0832faSShawn Lin 				VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
31306e0832faSShawn Lin 		if (!ret)
31316e0832faSShawn Lin 			ret = wait_for_response(hdev, &comp_pkt.host_event);
31326e0832faSShawn Lin 
31336e0832faSShawn Lin 		if (ret) {
31346e0832faSShawn Lin 			dev_err(&hdev->device,
31356e0832faSShawn Lin 				"PCI Pass-through VSP failed to request version: %d",
31366e0832faSShawn Lin 				ret);
31376e0832faSShawn Lin 			goto exit;
31386e0832faSShawn Lin 		}
31396e0832faSShawn Lin 
31406e0832faSShawn Lin 		if (comp_pkt.completion_status >= 0) {
314114ef39fdSDexuan Cui 			hbus->protocol_version = version[i];
31426e0832faSShawn Lin 			dev_info(&hdev->device,
31436e0832faSShawn Lin 				"PCI VMBus probing: Using version %#x\n",
314414ef39fdSDexuan Cui 				hbus->protocol_version);
31456e0832faSShawn Lin 			goto exit;
31466e0832faSShawn Lin 		}
31476e0832faSShawn Lin 
31486e0832faSShawn Lin 		if (comp_pkt.completion_status != STATUS_REVISION_MISMATCH) {
31496e0832faSShawn Lin 			dev_err(&hdev->device,
31506e0832faSShawn Lin 				"PCI Pass-through VSP failed version request: %#x",
31516e0832faSShawn Lin 				comp_pkt.completion_status);
31526e0832faSShawn Lin 			ret = -EPROTO;
31536e0832faSShawn Lin 			goto exit;
31546e0832faSShawn Lin 		}
31556e0832faSShawn Lin 
31566e0832faSShawn Lin 		reinit_completion(&comp_pkt.host_event);
31576e0832faSShawn Lin 	}
31586e0832faSShawn Lin 
31596e0832faSShawn Lin 	dev_err(&hdev->device,
31606e0832faSShawn Lin 		"PCI pass-through VSP failed to find supported version");
31616e0832faSShawn Lin 	ret = -EPROTO;
31626e0832faSShawn Lin 
31636e0832faSShawn Lin exit:
31646e0832faSShawn Lin 	kfree(pkt);
31656e0832faSShawn Lin 	return ret;
31666e0832faSShawn Lin }
31676e0832faSShawn Lin 
31686e0832faSShawn Lin /**
31696e0832faSShawn Lin  * hv_pci_free_bridge_windows() - Release memory regions for the
31706e0832faSShawn Lin  * bus
31716e0832faSShawn Lin  * @hbus:	Root PCI bus, as understood by this driver
31726e0832faSShawn Lin  */
hv_pci_free_bridge_windows(struct hv_pcibus_device * hbus)31736e0832faSShawn Lin static void hv_pci_free_bridge_windows(struct hv_pcibus_device *hbus)
31746e0832faSShawn Lin {
31756e0832faSShawn Lin 	/*
31766e0832faSShawn Lin 	 * Set the resources back to the way they looked when they
31776e0832faSShawn Lin 	 * were allocated by setting IORESOURCE_BUSY again.
31786e0832faSShawn Lin 	 */
31796e0832faSShawn Lin 
31806e0832faSShawn Lin 	if (hbus->low_mmio_space && hbus->low_mmio_res) {
31816e0832faSShawn Lin 		hbus->low_mmio_res->flags |= IORESOURCE_BUSY;
31826e0832faSShawn Lin 		vmbus_free_mmio(hbus->low_mmio_res->start,
31836e0832faSShawn Lin 				resource_size(hbus->low_mmio_res));
31846e0832faSShawn Lin 	}
31856e0832faSShawn Lin 
31866e0832faSShawn Lin 	if (hbus->high_mmio_space && hbus->high_mmio_res) {
31876e0832faSShawn Lin 		hbus->high_mmio_res->flags |= IORESOURCE_BUSY;
31886e0832faSShawn Lin 		vmbus_free_mmio(hbus->high_mmio_res->start,
31896e0832faSShawn Lin 				resource_size(hbus->high_mmio_res));
31906e0832faSShawn Lin 	}
31916e0832faSShawn Lin }
31926e0832faSShawn Lin 
31936e0832faSShawn Lin /**
31946e0832faSShawn Lin  * hv_pci_allocate_bridge_windows() - Allocate memory regions
31956e0832faSShawn Lin  * for the bus
31966e0832faSShawn Lin  * @hbus:	Root PCI bus, as understood by this driver
31976e0832faSShawn Lin  *
31986e0832faSShawn Lin  * This function calls vmbus_allocate_mmio(), which is itself a
31996e0832faSShawn Lin  * bit of a compromise.  Ideally, we might change the pnp layer
32006e0832faSShawn Lin  * in the kernel such that it comprehends either PCI devices
32016e0832faSShawn Lin  * which are "grandchildren of ACPI," with some intermediate bus
32026e0832faSShawn Lin  * node (in this case, VMBus) or change it such that it
32036e0832faSShawn Lin  * understands VMBus.  The pnp layer, however, has been declared
32046e0832faSShawn Lin  * deprecated, and not subject to change.
32056e0832faSShawn Lin  *
32066e0832faSShawn Lin  * The workaround, implemented here, is to ask VMBus to allocate
32076e0832faSShawn Lin  * MMIO space for this bus.  VMBus itself knows which ranges are
32086e0832faSShawn Lin  * appropriate by looking at its own ACPI objects.  Then, after
32096e0832faSShawn Lin  * these ranges are claimed, they're modified to look like they
32106e0832faSShawn Lin  * would have looked if the ACPI and pnp code had allocated
32116e0832faSShawn Lin  * bridge windows.  These descriptors have to exist in this form
32126e0832faSShawn Lin  * in order to satisfy the code which will get invoked when the
32136e0832faSShawn Lin  * endpoint PCI function driver calls request_mem_region() or
32146e0832faSShawn Lin  * request_mem_region_exclusive().
32156e0832faSShawn Lin  *
32166e0832faSShawn Lin  * Return: 0 on success, -errno on failure
32176e0832faSShawn Lin  */
hv_pci_allocate_bridge_windows(struct hv_pcibus_device * hbus)32186e0832faSShawn Lin static int hv_pci_allocate_bridge_windows(struct hv_pcibus_device *hbus)
32196e0832faSShawn Lin {
32206e0832faSShawn Lin 	resource_size_t align;
32216e0832faSShawn Lin 	int ret;
32226e0832faSShawn Lin 
32236e0832faSShawn Lin 	if (hbus->low_mmio_space) {
32246e0832faSShawn Lin 		align = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space));
32256e0832faSShawn Lin 		ret = vmbus_allocate_mmio(&hbus->low_mmio_res, hbus->hdev, 0,
32266e0832faSShawn Lin 					  (u64)(u32)0xffffffff,
32276e0832faSShawn Lin 					  hbus->low_mmio_space,
32286e0832faSShawn Lin 					  align, false);
32296e0832faSShawn Lin 		if (ret) {
32306e0832faSShawn Lin 			dev_err(&hbus->hdev->device,
32316e0832faSShawn Lin 				"Need %#llx of low MMIO space. Consider reconfiguring the VM.\n",
32326e0832faSShawn Lin 				hbus->low_mmio_space);
32336e0832faSShawn Lin 			return ret;
32346e0832faSShawn Lin 		}
32356e0832faSShawn Lin 
32366e0832faSShawn Lin 		/* Modify this resource to become a bridge window. */
32376e0832faSShawn Lin 		hbus->low_mmio_res->flags |= IORESOURCE_WINDOW;
32386e0832faSShawn Lin 		hbus->low_mmio_res->flags &= ~IORESOURCE_BUSY;
3239418cb6c8SArnd Bergmann 		pci_add_resource(&hbus->bridge->windows, hbus->low_mmio_res);
32406e0832faSShawn Lin 	}
32416e0832faSShawn Lin 
32426e0832faSShawn Lin 	if (hbus->high_mmio_space) {
32436e0832faSShawn Lin 		align = 1ULL << (63 - __builtin_clzll(hbus->high_mmio_space));
32446e0832faSShawn Lin 		ret = vmbus_allocate_mmio(&hbus->high_mmio_res, hbus->hdev,
32456e0832faSShawn Lin 					  0x100000000, -1,
32466e0832faSShawn Lin 					  hbus->high_mmio_space, align,
32476e0832faSShawn Lin 					  false);
32486e0832faSShawn Lin 		if (ret) {
32496e0832faSShawn Lin 			dev_err(&hbus->hdev->device,
32506e0832faSShawn Lin 				"Need %#llx of high MMIO space. Consider reconfiguring the VM.\n",
32516e0832faSShawn Lin 				hbus->high_mmio_space);
32526e0832faSShawn Lin 			goto release_low_mmio;
32536e0832faSShawn Lin 		}
32546e0832faSShawn Lin 
32556e0832faSShawn Lin 		/* Modify this resource to become a bridge window. */
32566e0832faSShawn Lin 		hbus->high_mmio_res->flags |= IORESOURCE_WINDOW;
32576e0832faSShawn Lin 		hbus->high_mmio_res->flags &= ~IORESOURCE_BUSY;
3258418cb6c8SArnd Bergmann 		pci_add_resource(&hbus->bridge->windows, hbus->high_mmio_res);
32596e0832faSShawn Lin 	}
32606e0832faSShawn Lin 
32616e0832faSShawn Lin 	return 0;
32626e0832faSShawn Lin 
32636e0832faSShawn Lin release_low_mmio:
32646e0832faSShawn Lin 	if (hbus->low_mmio_res) {
32656e0832faSShawn Lin 		vmbus_free_mmio(hbus->low_mmio_res->start,
32666e0832faSShawn Lin 				resource_size(hbus->low_mmio_res));
32676e0832faSShawn Lin 	}
32686e0832faSShawn Lin 
32696e0832faSShawn Lin 	return ret;
32706e0832faSShawn Lin }
32716e0832faSShawn Lin 
32726e0832faSShawn Lin /**
32736e0832faSShawn Lin  * hv_allocate_config_window() - Find MMIO space for PCI Config
32746e0832faSShawn Lin  * @hbus:	Root PCI bus, as understood by this driver
32756e0832faSShawn Lin  *
32766e0832faSShawn Lin  * This function claims memory-mapped I/O space for accessing
32776e0832faSShawn Lin  * configuration space for the functions on this bus.
32786e0832faSShawn Lin  *
32796e0832faSShawn Lin  * Return: 0 on success, -errno on failure
32806e0832faSShawn Lin  */
hv_allocate_config_window(struct hv_pcibus_device * hbus)32816e0832faSShawn Lin static int hv_allocate_config_window(struct hv_pcibus_device *hbus)
32826e0832faSShawn Lin {
32836e0832faSShawn Lin 	int ret;
32846e0832faSShawn Lin 
32856e0832faSShawn Lin 	/*
32866e0832faSShawn Lin 	 * Set up a region of MMIO space to use for accessing configuration
32876e0832faSShawn Lin 	 * space.
32886e0832faSShawn Lin 	 */
32896e0832faSShawn Lin 	ret = vmbus_allocate_mmio(&hbus->mem_config, hbus->hdev, 0, -1,
32906e0832faSShawn Lin 				  PCI_CONFIG_MMIO_LENGTH, 0x1000, false);
32916e0832faSShawn Lin 	if (ret)
32926e0832faSShawn Lin 		return ret;
32936e0832faSShawn Lin 
32946e0832faSShawn Lin 	/*
32956e0832faSShawn Lin 	 * vmbus_allocate_mmio() gets used for allocating both device endpoint
32966e0832faSShawn Lin 	 * resource claims (those which cannot be overlapped) and the ranges
32976e0832faSShawn Lin 	 * which are valid for the children of this bus, which are intended
32986e0832faSShawn Lin 	 * to be overlapped by those children.  Set the flag on this claim
32996e0832faSShawn Lin 	 * meaning that this region can't be overlapped.
33006e0832faSShawn Lin 	 */
33016e0832faSShawn Lin 
33026e0832faSShawn Lin 	hbus->mem_config->flags |= IORESOURCE_BUSY;
33036e0832faSShawn Lin 
33046e0832faSShawn Lin 	return 0;
33056e0832faSShawn Lin }
33066e0832faSShawn Lin 
hv_free_config_window(struct hv_pcibus_device * hbus)33076e0832faSShawn Lin static void hv_free_config_window(struct hv_pcibus_device *hbus)
33086e0832faSShawn Lin {
33096e0832faSShawn Lin 	vmbus_free_mmio(hbus->mem_config->start, PCI_CONFIG_MMIO_LENGTH);
33106e0832faSShawn Lin }
33116e0832faSShawn Lin 
3312c81992e7SWei Hu static int hv_pci_bus_exit(struct hv_device *hdev, bool keep_devs);
3313c81992e7SWei Hu 
33146e0832faSShawn Lin /**
33156e0832faSShawn Lin  * hv_pci_enter_d0() - Bring the "bus" into the D0 power state
33166e0832faSShawn Lin  * @hdev:	VMBus's tracking struct for this root PCI bus
33176e0832faSShawn Lin  *
33186e0832faSShawn Lin  * Return: 0 on success, -errno on failure
33196e0832faSShawn Lin  */
hv_pci_enter_d0(struct hv_device * hdev)33206e0832faSShawn Lin static int hv_pci_enter_d0(struct hv_device *hdev)
33216e0832faSShawn Lin {
33226e0832faSShawn Lin 	struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
33236e0832faSShawn Lin 	struct pci_bus_d0_entry *d0_entry;
33246e0832faSShawn Lin 	struct hv_pci_compl comp_pkt;
33256e0832faSShawn Lin 	struct pci_packet *pkt;
3326a847234eSDexuan Cui 	bool retry = true;
33276e0832faSShawn Lin 	int ret;
33286e0832faSShawn Lin 
3329a847234eSDexuan Cui enter_d0_retry:
33306e0832faSShawn Lin 	/*
33316e0832faSShawn Lin 	 * Tell the host that the bus is ready to use, and moved into the
33326e0832faSShawn Lin 	 * powered-on state.  This includes telling the host which region
33336e0832faSShawn Lin 	 * of memory-mapped I/O space has been chosen for configuration space
33346e0832faSShawn Lin 	 * access.
33356e0832faSShawn Lin 	 */
33366e0832faSShawn Lin 	pkt = kzalloc(sizeof(*pkt) + sizeof(*d0_entry), GFP_KERNEL);
33376e0832faSShawn Lin 	if (!pkt)
33386e0832faSShawn Lin 		return -ENOMEM;
33396e0832faSShawn Lin 
33406e0832faSShawn Lin 	init_completion(&comp_pkt.host_event);
33416e0832faSShawn Lin 	pkt->completion_func = hv_pci_generic_compl;
33426e0832faSShawn Lin 	pkt->compl_ctxt = &comp_pkt;
33436e0832faSShawn Lin 	d0_entry = (struct pci_bus_d0_entry *)&pkt->message;
33446e0832faSShawn Lin 	d0_entry->message_type.type = PCI_BUS_D0ENTRY;
33456e0832faSShawn Lin 	d0_entry->mmio_base = hbus->mem_config->start;
33466e0832faSShawn Lin 
33476e0832faSShawn Lin 	ret = vmbus_sendpacket(hdev->channel, d0_entry, sizeof(*d0_entry),
33486e0832faSShawn Lin 			       (unsigned long)pkt, VM_PKT_DATA_INBAND,
33496e0832faSShawn Lin 			       VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
33506e0832faSShawn Lin 	if (!ret)
33516e0832faSShawn Lin 		ret = wait_for_response(hdev, &comp_pkt.host_event);
33526e0832faSShawn Lin 
33536e0832faSShawn Lin 	if (ret)
33546e0832faSShawn Lin 		goto exit;
33556e0832faSShawn Lin 
3356a847234eSDexuan Cui 	/*
3357a847234eSDexuan Cui 	 * In certain case (Kdump) the pci device of interest was
3358a847234eSDexuan Cui 	 * not cleanly shut down and resource is still held on host
3359a847234eSDexuan Cui 	 * side, the host could return invalid device status.
3360a847234eSDexuan Cui 	 * We need to explicitly request host to release the resource
3361a847234eSDexuan Cui 	 * and try to enter D0 again.
3362a847234eSDexuan Cui 	 */
3363a847234eSDexuan Cui 	if (comp_pkt.completion_status < 0 && retry) {
3364a847234eSDexuan Cui 		retry = false;
3365a847234eSDexuan Cui 
3366a847234eSDexuan Cui 		dev_err(&hdev->device, "Retrying D0 Entry\n");
3367a847234eSDexuan Cui 
3368a847234eSDexuan Cui 		/*
3369a847234eSDexuan Cui 		 * Hv_pci_bus_exit() calls hv_send_resource_released()
3370a847234eSDexuan Cui 		 * to free up resources of its child devices.
3371a847234eSDexuan Cui 		 * In the kdump kernel we need to set the
3372a847234eSDexuan Cui 		 * wslot_res_allocated to 255 so it scans all child
3373a847234eSDexuan Cui 		 * devices to release resources allocated in the
3374a847234eSDexuan Cui 		 * normal kernel before panic happened.
3375a847234eSDexuan Cui 		 */
3376a847234eSDexuan Cui 		hbus->wslot_res_allocated = 255;
3377a847234eSDexuan Cui 
3378a847234eSDexuan Cui 		ret = hv_pci_bus_exit(hdev, true);
3379a847234eSDexuan Cui 
3380a847234eSDexuan Cui 		if (ret == 0) {
3381a847234eSDexuan Cui 			kfree(pkt);
3382a847234eSDexuan Cui 			goto enter_d0_retry;
3383a847234eSDexuan Cui 		}
3384a847234eSDexuan Cui 		dev_err(&hdev->device,
3385a847234eSDexuan Cui 			"Retrying D0 failed with ret %d\n", ret);
3386a847234eSDexuan Cui 	}
3387a847234eSDexuan Cui 
33886e0832faSShawn Lin 	if (comp_pkt.completion_status < 0) {
33896e0832faSShawn Lin 		dev_err(&hdev->device,
33906e0832faSShawn Lin 			"PCI Pass-through VSP failed D0 Entry with status %x\n",
33916e0832faSShawn Lin 			comp_pkt.completion_status);
33926e0832faSShawn Lin 		ret = -EPROTO;
33936e0832faSShawn Lin 		goto exit;
33946e0832faSShawn Lin 	}
33956e0832faSShawn Lin 
33966e0832faSShawn Lin 	ret = 0;
33976e0832faSShawn Lin 
33986e0832faSShawn Lin exit:
33996e0832faSShawn Lin 	kfree(pkt);
34006e0832faSShawn Lin 	return ret;
34016e0832faSShawn Lin }
34026e0832faSShawn Lin 
34036e0832faSShawn Lin /**
34046e0832faSShawn Lin  * hv_pci_query_relations() - Ask host to send list of child
34056e0832faSShawn Lin  * devices
34066e0832faSShawn Lin  * @hdev:	VMBus's tracking struct for this root PCI bus
34076e0832faSShawn Lin  *
34086e0832faSShawn Lin  * Return: 0 on success, -errno on failure
34096e0832faSShawn Lin  */
hv_pci_query_relations(struct hv_device * hdev)34106e0832faSShawn Lin static int hv_pci_query_relations(struct hv_device *hdev)
34116e0832faSShawn Lin {
34126e0832faSShawn Lin 	struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
34136e0832faSShawn Lin 	struct pci_message message;
34146e0832faSShawn Lin 	struct completion comp;
34156e0832faSShawn Lin 	int ret;
34166e0832faSShawn Lin 
34176e0832faSShawn Lin 	/* Ask the host to send along the list of child devices */
34186e0832faSShawn Lin 	init_completion(&comp);
34196e0832faSShawn Lin 	if (cmpxchg(&hbus->survey_event, NULL, &comp))
34206e0832faSShawn Lin 		return -ENOTEMPTY;
34216e0832faSShawn Lin 
34226e0832faSShawn Lin 	memset(&message, 0, sizeof(message));
34236e0832faSShawn Lin 	message.type = PCI_QUERY_BUS_RELATIONS;
34246e0832faSShawn Lin 
34256e0832faSShawn Lin 	ret = vmbus_sendpacket(hdev->channel, &message, sizeof(message),
34266e0832faSShawn Lin 			       0, VM_PKT_DATA_INBAND, 0);
34276e0832faSShawn Lin 	if (!ret)
34286e0832faSShawn Lin 		ret = wait_for_response(hdev, &comp);
34296e0832faSShawn Lin 
3430440b5e36SDexuan Cui 	/*
3431440b5e36SDexuan Cui 	 * In the case of fast device addition/removal, it's possible that
3432440b5e36SDexuan Cui 	 * vmbus_sendpacket() or wait_for_response() returns -ENODEV but we
3433440b5e36SDexuan Cui 	 * already got a PCI_BUS_RELATIONS* message from the host and the
3434440b5e36SDexuan Cui 	 * channel callback already scheduled a work to hbus->wq, which can be
3435440b5e36SDexuan Cui 	 * running pci_devices_present_work() -> survey_child_resources() ->
3436440b5e36SDexuan Cui 	 * complete(&hbus->survey_event), even after hv_pci_query_relations()
3437440b5e36SDexuan Cui 	 * exits and the stack variable 'comp' is no longer valid; as a result,
3438440b5e36SDexuan Cui 	 * a hang or a page fault may happen when the complete() calls
3439440b5e36SDexuan Cui 	 * raw_spin_lock_irqsave(). Flush hbus->wq before we exit from
3440440b5e36SDexuan Cui 	 * hv_pci_query_relations() to avoid the issues. Note: if 'ret' is
3441440b5e36SDexuan Cui 	 * -ENODEV, there can't be any more work item scheduled to hbus->wq
3442440b5e36SDexuan Cui 	 * after the flush_workqueue(): see vmbus_onoffer_rescind() ->
3443440b5e36SDexuan Cui 	 * vmbus_reset_channel_cb(), vmbus_rescind_cleanup() ->
3444440b5e36SDexuan Cui 	 * channel->rescind = true.
3445440b5e36SDexuan Cui 	 */
3446440b5e36SDexuan Cui 	flush_workqueue(hbus->wq);
3447440b5e36SDexuan Cui 
34486e0832faSShawn Lin 	return ret;
34496e0832faSShawn Lin }
34506e0832faSShawn Lin 
34516e0832faSShawn Lin /**
34526e0832faSShawn Lin  * hv_send_resources_allocated() - Report local resource choices
34536e0832faSShawn Lin  * @hdev:	VMBus's tracking struct for this root PCI bus
34546e0832faSShawn Lin  *
34556e0832faSShawn Lin  * The host OS is expecting to be sent a request as a message
34566e0832faSShawn Lin  * which contains all the resources that the device will use.
34576e0832faSShawn Lin  * The response contains those same resources, "translated"
34586e0832faSShawn Lin  * which is to say, the values which should be used by the
34596e0832faSShawn Lin  * hardware, when it delivers an interrupt.  (MMIO resources are
34606e0832faSShawn Lin  * used in local terms.)  This is nice for Windows, and lines up
34616e0832faSShawn Lin  * with the FDO/PDO split, which doesn't exist in Linux.  Linux
34626e0832faSShawn Lin  * is deeply expecting to scan an emulated PCI configuration
34636e0832faSShawn Lin  * space.  So this message is sent here only to drive the state
34646e0832faSShawn Lin  * machine on the host forward.
34656e0832faSShawn Lin  *
34666e0832faSShawn Lin  * Return: 0 on success, -errno on failure
34676e0832faSShawn Lin  */
hv_send_resources_allocated(struct hv_device * hdev)34686e0832faSShawn Lin static int hv_send_resources_allocated(struct hv_device *hdev)
34696e0832faSShawn Lin {
34706e0832faSShawn Lin 	struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
34716e0832faSShawn Lin 	struct pci_resources_assigned *res_assigned;
34726e0832faSShawn Lin 	struct pci_resources_assigned2 *res_assigned2;
34736e0832faSShawn Lin 	struct hv_pci_compl comp_pkt;
34746e0832faSShawn Lin 	struct hv_pci_dev *hpdev;
34756e0832faSShawn Lin 	struct pci_packet *pkt;
34766e0832faSShawn Lin 	size_t size_res;
347783cc3508SWei Hu 	int wslot;
34786e0832faSShawn Lin 	int ret;
34796e0832faSShawn Lin 
348014ef39fdSDexuan Cui 	size_res = (hbus->protocol_version < PCI_PROTOCOL_VERSION_1_2)
34816e0832faSShawn Lin 			? sizeof(*res_assigned) : sizeof(*res_assigned2);
34826e0832faSShawn Lin 
34836e0832faSShawn Lin 	pkt = kmalloc(sizeof(*pkt) + size_res, GFP_KERNEL);
34846e0832faSShawn Lin 	if (!pkt)
34856e0832faSShawn Lin 		return -ENOMEM;
34866e0832faSShawn Lin 
34876e0832faSShawn Lin 	ret = 0;
34886e0832faSShawn Lin 
34896e0832faSShawn Lin 	for (wslot = 0; wslot < 256; wslot++) {
34906e0832faSShawn Lin 		hpdev = get_pcichild_wslot(hbus, wslot);
34916e0832faSShawn Lin 		if (!hpdev)
34926e0832faSShawn Lin 			continue;
34936e0832faSShawn Lin 
34946e0832faSShawn Lin 		memset(pkt, 0, sizeof(*pkt) + size_res);
34956e0832faSShawn Lin 		init_completion(&comp_pkt.host_event);
34966e0832faSShawn Lin 		pkt->completion_func = hv_pci_generic_compl;
34976e0832faSShawn Lin 		pkt->compl_ctxt = &comp_pkt;
34986e0832faSShawn Lin 
349914ef39fdSDexuan Cui 		if (hbus->protocol_version < PCI_PROTOCOL_VERSION_1_2) {
35006e0832faSShawn Lin 			res_assigned =
35016e0832faSShawn Lin 				(struct pci_resources_assigned *)&pkt->message;
35026e0832faSShawn Lin 			res_assigned->message_type.type =
35036e0832faSShawn Lin 				PCI_RESOURCES_ASSIGNED;
35046e0832faSShawn Lin 			res_assigned->wslot.slot = hpdev->desc.win_slot.slot;
35056e0832faSShawn Lin 		} else {
35066e0832faSShawn Lin 			res_assigned2 =
35076e0832faSShawn Lin 				(struct pci_resources_assigned2 *)&pkt->message;
35086e0832faSShawn Lin 			res_assigned2->message_type.type =
35096e0832faSShawn Lin 				PCI_RESOURCES_ASSIGNED2;
35106e0832faSShawn Lin 			res_assigned2->wslot.slot = hpdev->desc.win_slot.slot;
35116e0832faSShawn Lin 		}
35126e0832faSShawn Lin 		put_pcichild(hpdev);
35136e0832faSShawn Lin 
35146e0832faSShawn Lin 		ret = vmbus_sendpacket(hdev->channel, &pkt->message,
35156e0832faSShawn Lin 				size_res, (unsigned long)pkt,
35166e0832faSShawn Lin 				VM_PKT_DATA_INBAND,
35176e0832faSShawn Lin 				VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
35186e0832faSShawn Lin 		if (!ret)
35196e0832faSShawn Lin 			ret = wait_for_response(hdev, &comp_pkt.host_event);
35206e0832faSShawn Lin 		if (ret)
35216e0832faSShawn Lin 			break;
35226e0832faSShawn Lin 
35236e0832faSShawn Lin 		if (comp_pkt.completion_status < 0) {
35246e0832faSShawn Lin 			ret = -EPROTO;
35256e0832faSShawn Lin 			dev_err(&hdev->device,
35266e0832faSShawn Lin 				"resource allocated returned 0x%x",
35276e0832faSShawn Lin 				comp_pkt.completion_status);
35286e0832faSShawn Lin 			break;
35296e0832faSShawn Lin 		}
353083cc3508SWei Hu 
353183cc3508SWei Hu 		hbus->wslot_res_allocated = wslot;
35326e0832faSShawn Lin 	}
35336e0832faSShawn Lin 
35346e0832faSShawn Lin 	kfree(pkt);
35356e0832faSShawn Lin 	return ret;
35366e0832faSShawn Lin }
35376e0832faSShawn Lin 
35386e0832faSShawn Lin /**
35396e0832faSShawn Lin  * hv_send_resources_released() - Report local resources
35406e0832faSShawn Lin  * released
35416e0832faSShawn Lin  * @hdev:	VMBus's tracking struct for this root PCI bus
35426e0832faSShawn Lin  *
35436e0832faSShawn Lin  * Return: 0 on success, -errno on failure
35446e0832faSShawn Lin  */
hv_send_resources_released(struct hv_device * hdev)35456e0832faSShawn Lin static int hv_send_resources_released(struct hv_device *hdev)
35466e0832faSShawn Lin {
35476e0832faSShawn Lin 	struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
35486e0832faSShawn Lin 	struct pci_child_message pkt;
35496e0832faSShawn Lin 	struct hv_pci_dev *hpdev;
355083cc3508SWei Hu 	int wslot;
35516e0832faSShawn Lin 	int ret;
35526e0832faSShawn Lin 
355383cc3508SWei Hu 	for (wslot = hbus->wslot_res_allocated; wslot >= 0; wslot--) {
35546e0832faSShawn Lin 		hpdev = get_pcichild_wslot(hbus, wslot);
35556e0832faSShawn Lin 		if (!hpdev)
35566e0832faSShawn Lin 			continue;
35576e0832faSShawn Lin 
35586e0832faSShawn Lin 		memset(&pkt, 0, sizeof(pkt));
35596e0832faSShawn Lin 		pkt.message_type.type = PCI_RESOURCES_RELEASED;
35606e0832faSShawn Lin 		pkt.wslot.slot = hpdev->desc.win_slot.slot;
35616e0832faSShawn Lin 
35626e0832faSShawn Lin 		put_pcichild(hpdev);
35636e0832faSShawn Lin 
35646e0832faSShawn Lin 		ret = vmbus_sendpacket(hdev->channel, &pkt, sizeof(pkt), 0,
35656e0832faSShawn Lin 				       VM_PKT_DATA_INBAND, 0);
35666e0832faSShawn Lin 		if (ret)
35676e0832faSShawn Lin 			return ret;
356883cc3508SWei Hu 
356983cc3508SWei Hu 		hbus->wslot_res_allocated = wslot - 1;
35706e0832faSShawn Lin 	}
35716e0832faSShawn Lin 
357283cc3508SWei Hu 	hbus->wslot_res_allocated = -1;
357383cc3508SWei Hu 
35746e0832faSShawn Lin 	return 0;
35756e0832faSShawn Lin }
35766e0832faSShawn Lin 
3577be700103SHaiyang Zhang #define HVPCI_DOM_MAP_SIZE (64 * 1024)
3578be700103SHaiyang Zhang static DECLARE_BITMAP(hvpci_dom_map, HVPCI_DOM_MAP_SIZE);
3579be700103SHaiyang Zhang 
3580be700103SHaiyang Zhang /*
3581be700103SHaiyang Zhang  * PCI domain number 0 is used by emulated devices on Gen1 VMs, so define 0
3582be700103SHaiyang Zhang  * as invalid for passthrough PCI devices of this driver.
3583be700103SHaiyang Zhang  */
3584be700103SHaiyang Zhang #define HVPCI_DOM_INVALID 0
3585be700103SHaiyang Zhang 
3586be700103SHaiyang Zhang /**
3587be700103SHaiyang Zhang  * hv_get_dom_num() - Get a valid PCI domain number
3588be700103SHaiyang Zhang  * Check if the PCI domain number is in use, and return another number if
3589be700103SHaiyang Zhang  * it is in use.
3590be700103SHaiyang Zhang  *
3591be700103SHaiyang Zhang  * @dom: Requested domain number
3592be700103SHaiyang Zhang  *
3593be700103SHaiyang Zhang  * return: domain number on success, HVPCI_DOM_INVALID on failure
3594be700103SHaiyang Zhang  */
hv_get_dom_num(u16 dom)3595be700103SHaiyang Zhang static u16 hv_get_dom_num(u16 dom)
3596be700103SHaiyang Zhang {
3597be700103SHaiyang Zhang 	unsigned int i;
3598be700103SHaiyang Zhang 
3599be700103SHaiyang Zhang 	if (test_and_set_bit(dom, hvpci_dom_map) == 0)
3600be700103SHaiyang Zhang 		return dom;
3601be700103SHaiyang Zhang 
3602be700103SHaiyang Zhang 	for_each_clear_bit(i, hvpci_dom_map, HVPCI_DOM_MAP_SIZE) {
3603be700103SHaiyang Zhang 		if (test_and_set_bit(i, hvpci_dom_map) == 0)
3604be700103SHaiyang Zhang 			return i;
3605be700103SHaiyang Zhang 	}
3606be700103SHaiyang Zhang 
3607be700103SHaiyang Zhang 	return HVPCI_DOM_INVALID;
3608be700103SHaiyang Zhang }
3609be700103SHaiyang Zhang 
3610be700103SHaiyang Zhang /**
3611be700103SHaiyang Zhang  * hv_put_dom_num() - Mark the PCI domain number as free
3612be700103SHaiyang Zhang  * @dom: Domain number to be freed
3613be700103SHaiyang Zhang  */
hv_put_dom_num(u16 dom)3614be700103SHaiyang Zhang static void hv_put_dom_num(u16 dom)
3615be700103SHaiyang Zhang {
3616be700103SHaiyang Zhang 	clear_bit(dom, hvpci_dom_map);
3617be700103SHaiyang Zhang }
3618be700103SHaiyang Zhang 
36196e0832faSShawn Lin /**
36206e0832faSShawn Lin  * hv_pci_probe() - New VMBus channel probe, for a root PCI bus
36216e0832faSShawn Lin  * @hdev:	VMBus's tracking struct for this root PCI bus
36226e0832faSShawn Lin  * @dev_id:	Identifies the device itself
36236e0832faSShawn Lin  *
36246e0832faSShawn Lin  * Return: 0 on success, -errno on failure
36256e0832faSShawn Lin  */
hv_pci_probe(struct hv_device * hdev,const struct hv_vmbus_device_id * dev_id)36266e0832faSShawn Lin static int hv_pci_probe(struct hv_device *hdev,
36276e0832faSShawn Lin 			const struct hv_vmbus_device_id *dev_id)
36286e0832faSShawn Lin {
3629418cb6c8SArnd Bergmann 	struct pci_host_bridge *bridge;
36306e0832faSShawn Lin 	struct hv_pcibus_device *hbus;
3631be700103SHaiyang Zhang 	u16 dom_req, dom;
3632467a3bb9SMarc Zyngier 	char *name;
36336e0832faSShawn Lin 	int ret;
36346e0832faSShawn Lin 
3635418cb6c8SArnd Bergmann 	bridge = devm_pci_alloc_host_bridge(&hdev->device, 0);
3636418cb6c8SArnd Bergmann 	if (!bridge)
3637418cb6c8SArnd Bergmann 		return -ENOMEM;
3638418cb6c8SArnd Bergmann 
3639a494aef2SDexuan Cui 	hbus = kzalloc(sizeof(*hbus), GFP_KERNEL);
36406e0832faSShawn Lin 	if (!hbus)
36416e0832faSShawn Lin 		return -ENOMEM;
3642418cb6c8SArnd Bergmann 
3643418cb6c8SArnd Bergmann 	hbus->bridge = bridge;
3644067d6ec7SDexuan Cui 	mutex_init(&hbus->state_lock);
36456e0832faSShawn Lin 	hbus->state = hv_pcibus_init;
364683cc3508SWei Hu 	hbus->wslot_res_allocated = -1;
36476e0832faSShawn Lin 
36486e0832faSShawn Lin 	/*
3649be700103SHaiyang Zhang 	 * The PCI bus "domain" is what is called "segment" in ACPI and other
3650be700103SHaiyang Zhang 	 * specs. Pull it from the instance ID, to get something usually
3651be700103SHaiyang Zhang 	 * unique. In rare cases of collision, we will find out another number
3652be700103SHaiyang Zhang 	 * not in use.
3653be700103SHaiyang Zhang 	 *
3654be700103SHaiyang Zhang 	 * Note that, since this code only runs in a Hyper-V VM, Hyper-V
3655be700103SHaiyang Zhang 	 * together with this guest driver can guarantee that (1) The only
3656be700103SHaiyang Zhang 	 * domain used by Gen1 VMs for something that looks like a physical
3657be700103SHaiyang Zhang 	 * PCI bus (which is actually emulated by the hypervisor) is domain 0.
3658be700103SHaiyang Zhang 	 * (2) There will be no overlap between domains (after fixing possible
3659be700103SHaiyang Zhang 	 * collisions) in the same VM.
36606e0832faSShawn Lin 	 */
3661f73f8a50SHaiyang Zhang 	dom_req = hdev->dev_instance.b[5] << 8 | hdev->dev_instance.b[4];
3662be700103SHaiyang Zhang 	dom = hv_get_dom_num(dom_req);
3663be700103SHaiyang Zhang 
3664be700103SHaiyang Zhang 	if (dom == HVPCI_DOM_INVALID) {
3665be700103SHaiyang Zhang 		dev_err(&hdev->device,
3666f1831208SKrzysztof Wilczyński 			"Unable to use dom# 0x%x or other numbers", dom_req);
3667be700103SHaiyang Zhang 		ret = -EINVAL;
3668be700103SHaiyang Zhang 		goto free_bus;
3669be700103SHaiyang Zhang 	}
3670be700103SHaiyang Zhang 
3671be700103SHaiyang Zhang 	if (dom != dom_req)
3672be700103SHaiyang Zhang 		dev_info(&hdev->device,
3673f1831208SKrzysztof Wilczyński 			 "PCI dom# 0x%x has collision, using 0x%x",
3674be700103SHaiyang Zhang 			 dom_req, dom);
3675be700103SHaiyang Zhang 
367638c0d266SBoqun Feng 	hbus->bridge->domain_nr = dom;
367788f94c7fSBoqun Feng #ifdef CONFIG_X86
3678be700103SHaiyang Zhang 	hbus->sysdata.domain = dom;
36792c6ba421SMichael Kelley 	hbus->use_calls = !!(ms_hyperv.hints & HV_X64_USE_MMIO_HYPERCALLS);
36808d217324SMichael Kelley #elif defined(CONFIG_ARM64)
36818d217324SMichael Kelley 	/*
36828d217324SMichael Kelley 	 * Set the PCI bus parent to be the corresponding VMbus
36838d217324SMichael Kelley 	 * device. Then the VMbus device will be assigned as the
36848d217324SMichael Kelley 	 * ACPI companion in pcibios_root_bridge_prepare() and
36858d217324SMichael Kelley 	 * pci_dma_configure() will propagate device coherence
36868d217324SMichael Kelley 	 * information to devices created on the bus.
36878d217324SMichael Kelley 	 */
36888d217324SMichael Kelley 	hbus->sysdata.parent = hdev->device.parent;
36892c6ba421SMichael Kelley 	hbus->use_calls = false;
369088f94c7fSBoqun Feng #endif
36916e0832faSShawn Lin 
36926e0832faSShawn Lin 	hbus->hdev = hdev;
36936e0832faSShawn Lin 	INIT_LIST_HEAD(&hbus->children);
36946e0832faSShawn Lin 	INIT_LIST_HEAD(&hbus->dr_list);
36956e0832faSShawn Lin 	spin_lock_init(&hbus->config_lock);
36966e0832faSShawn Lin 	spin_lock_init(&hbus->device_list_lock);
36976e0832faSShawn Lin 	hbus->wq = alloc_ordered_workqueue("hv_pci_%x", 0,
369838c0d266SBoqun Feng 					   hbus->bridge->domain_nr);
36996e0832faSShawn Lin 	if (!hbus->wq) {
37006e0832faSShawn Lin 		ret = -ENOMEM;
3701be700103SHaiyang Zhang 		goto free_dom;
37026e0832faSShawn Lin 	}
37036e0832faSShawn Lin 
3704de5ddb7dSAndrea Parri (Microsoft) 	hdev->channel->next_request_id_callback = vmbus_next_request_id;
3705de5ddb7dSAndrea Parri (Microsoft) 	hdev->channel->request_addr_callback = vmbus_request_addr;
3706de5ddb7dSAndrea Parri (Microsoft) 	hdev->channel->rqstor_size = HV_PCI_RQSTOR_SIZE;
3707de5ddb7dSAndrea Parri (Microsoft) 
37086e0832faSShawn Lin 	ret = vmbus_open(hdev->channel, pci_ring_size, pci_ring_size, NULL, 0,
37096e0832faSShawn Lin 			 hv_pci_onchannelcallback, hbus);
37106e0832faSShawn Lin 	if (ret)
37116e0832faSShawn Lin 		goto destroy_wq;
37126e0832faSShawn Lin 
37136e0832faSShawn Lin 	hv_set_drvdata(hdev, hbus);
37146e0832faSShawn Lin 
3715a8e37506SDexuan Cui 	ret = hv_pci_protocol_negotiation(hdev, pci_protocol_versions,
3716a8e37506SDexuan Cui 					  ARRAY_SIZE(pci_protocol_versions));
37176e0832faSShawn Lin 	if (ret)
37186e0832faSShawn Lin 		goto close;
37196e0832faSShawn Lin 
37206e0832faSShawn Lin 	ret = hv_allocate_config_window(hbus);
37216e0832faSShawn Lin 	if (ret)
37226e0832faSShawn Lin 		goto close;
37236e0832faSShawn Lin 
37246e0832faSShawn Lin 	hbus->cfg_addr = ioremap(hbus->mem_config->start,
37256e0832faSShawn Lin 				 PCI_CONFIG_MMIO_LENGTH);
37266e0832faSShawn Lin 	if (!hbus->cfg_addr) {
37276e0832faSShawn Lin 		dev_err(&hdev->device,
37286e0832faSShawn Lin 			"Unable to map a virtual address for config space\n");
37296e0832faSShawn Lin 		ret = -ENOMEM;
37306e0832faSShawn Lin 		goto free_config;
37316e0832faSShawn Lin 	}
37326e0832faSShawn Lin 
3733467a3bb9SMarc Zyngier 	name = kasprintf(GFP_KERNEL, "%pUL", &hdev->dev_instance);
3734467a3bb9SMarc Zyngier 	if (!name) {
3735467a3bb9SMarc Zyngier 		ret = -ENOMEM;
3736467a3bb9SMarc Zyngier 		goto unmap;
3737467a3bb9SMarc Zyngier 	}
3738467a3bb9SMarc Zyngier 
37399e7f9178SBoqun Feng 	hbus->fwnode = irq_domain_alloc_named_fwnode(name);
3740467a3bb9SMarc Zyngier 	kfree(name);
37419e7f9178SBoqun Feng 	if (!hbus->fwnode) {
37426e0832faSShawn Lin 		ret = -ENOMEM;
37436e0832faSShawn Lin 		goto unmap;
37446e0832faSShawn Lin 	}
37456e0832faSShawn Lin 
37466e0832faSShawn Lin 	ret = hv_pcie_init_irq_domain(hbus);
37476e0832faSShawn Lin 	if (ret)
37486e0832faSShawn Lin 		goto free_fwnode;
37496e0832faSShawn Lin 
37506e0832faSShawn Lin 	ret = hv_pci_query_relations(hdev);
37516e0832faSShawn Lin 	if (ret)
37526e0832faSShawn Lin 		goto free_irq_domain;
37536e0832faSShawn Lin 
3754067d6ec7SDexuan Cui 	mutex_lock(&hbus->state_lock);
3755067d6ec7SDexuan Cui 
37566e0832faSShawn Lin 	ret = hv_pci_enter_d0(hdev);
37576e0832faSShawn Lin 	if (ret)
3758067d6ec7SDexuan Cui 		goto release_state_lock;
37596e0832faSShawn Lin 
37606e0832faSShawn Lin 	ret = hv_pci_allocate_bridge_windows(hbus);
37616e0832faSShawn Lin 	if (ret)
376283cc3508SWei Hu 		goto exit_d0;
37636e0832faSShawn Lin 
37646e0832faSShawn Lin 	ret = hv_send_resources_allocated(hdev);
37656e0832faSShawn Lin 	if (ret)
37666e0832faSShawn Lin 		goto free_windows;
37676e0832faSShawn Lin 
37686e0832faSShawn Lin 	prepopulate_bars(hbus);
37696e0832faSShawn Lin 
37706e0832faSShawn Lin 	hbus->state = hv_pcibus_probed;
37716e0832faSShawn Lin 
37726e0832faSShawn Lin 	ret = create_root_hv_pci_bus(hbus);
37736e0832faSShawn Lin 	if (ret)
37746e0832faSShawn Lin 		goto free_windows;
37756e0832faSShawn Lin 
3776067d6ec7SDexuan Cui 	mutex_unlock(&hbus->state_lock);
37776e0832faSShawn Lin 	return 0;
37786e0832faSShawn Lin 
37796e0832faSShawn Lin free_windows:
37806e0832faSShawn Lin 	hv_pci_free_bridge_windows(hbus);
378183cc3508SWei Hu exit_d0:
378283cc3508SWei Hu 	(void) hv_pci_bus_exit(hdev, true);
3783067d6ec7SDexuan Cui release_state_lock:
3784067d6ec7SDexuan Cui 	mutex_unlock(&hbus->state_lock);
37856e0832faSShawn Lin free_irq_domain:
37866e0832faSShawn Lin 	irq_domain_remove(hbus->irq_domain);
37876e0832faSShawn Lin free_fwnode:
37889e7f9178SBoqun Feng 	irq_domain_free_fwnode(hbus->fwnode);
37896e0832faSShawn Lin unmap:
37906e0832faSShawn Lin 	iounmap(hbus->cfg_addr);
37916e0832faSShawn Lin free_config:
37926e0832faSShawn Lin 	hv_free_config_window(hbus);
37936e0832faSShawn Lin close:
37946e0832faSShawn Lin 	vmbus_close(hdev->channel);
37956e0832faSShawn Lin destroy_wq:
37966e0832faSShawn Lin 	destroy_workqueue(hbus->wq);
3797be700103SHaiyang Zhang free_dom:
379838c0d266SBoqun Feng 	hv_put_dom_num(hbus->bridge->domain_nr);
37996e0832faSShawn Lin free_bus:
380042c3d418SDexuan Cui 	kfree(hbus);
38016e0832faSShawn Lin 	return ret;
38026e0832faSShawn Lin }
38036e0832faSShawn Lin 
hv_pci_bus_exit(struct hv_device * hdev,bool keep_devs)3804c81992e7SWei Hu static int hv_pci_bus_exit(struct hv_device *hdev, bool keep_devs)
38056e0832faSShawn Lin {
38066e0832faSShawn Lin 	struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
3807b4927bd2SAndrea Parri (Microsoft) 	struct vmbus_channel *chan = hdev->channel;
38086e0832faSShawn Lin 	struct {
38096e0832faSShawn Lin 		struct pci_packet teardown_packet;
38106e0832faSShawn Lin 		u8 buffer[sizeof(struct pci_message)];
38116e0832faSShawn Lin 	} pkt;
38126e0832faSShawn Lin 	struct hv_pci_compl comp_pkt;
381394d22763SLong Li 	struct hv_pci_dev *hpdev, *tmp;
381494d22763SLong Li 	unsigned long flags;
3815b4927bd2SAndrea Parri (Microsoft) 	u64 trans_id;
38166e0832faSShawn Lin 	int ret;
38176e0832faSShawn Lin 
38186e0832faSShawn Lin 	/*
38196e0832faSShawn Lin 	 * After the host sends the RESCIND_CHANNEL message, it doesn't
38206e0832faSShawn Lin 	 * access the per-channel ringbuffer any longer.
38216e0832faSShawn Lin 	 */
3822b4927bd2SAndrea Parri (Microsoft) 	if (chan->rescind)
3823a8e37506SDexuan Cui 		return 0;
38246e0832faSShawn Lin 
3825c81992e7SWei Hu 	if (!keep_devs) {
382641608b64SLong Li 		struct list_head removed;
382741608b64SLong Li 
382841608b64SLong Li 		/* Move all present children to the list on stack */
382941608b64SLong Li 		INIT_LIST_HEAD(&removed);
383094d22763SLong Li 		spin_lock_irqsave(&hbus->device_list_lock, flags);
383141608b64SLong Li 		list_for_each_entry_safe(hpdev, tmp, &hbus->children, list_entry)
383241608b64SLong Li 			list_move_tail(&hpdev->list_entry, &removed);
383341608b64SLong Li 		spin_unlock_irqrestore(&hbus->device_list_lock, flags);
383441608b64SLong Li 
383541608b64SLong Li 		/* Remove all children in the list */
383641608b64SLong Li 		list_for_each_entry_safe(hpdev, tmp, &removed, list_entry) {
383794d22763SLong Li 			list_del(&hpdev->list_entry);
383894d22763SLong Li 			if (hpdev->pci_slot)
383994d22763SLong Li 				pci_destroy_slot(hpdev->pci_slot);
384094d22763SLong Li 			/* For the two refs got in new_pcichild_device() */
384194d22763SLong Li 			put_pcichild(hpdev);
384294d22763SLong Li 			put_pcichild(hpdev);
384394d22763SLong Li 		}
3844a8e37506SDexuan Cui 	}
38456e0832faSShawn Lin 
38466e0832faSShawn Lin 	ret = hv_send_resources_released(hdev);
3847a8e37506SDexuan Cui 	if (ret) {
38486e0832faSShawn Lin 		dev_err(&hdev->device,
38496e0832faSShawn Lin 			"Couldn't send resources released packet(s)\n");
3850a8e37506SDexuan Cui 		return ret;
3851a8e37506SDexuan Cui 	}
38526e0832faSShawn Lin 
38536e0832faSShawn Lin 	memset(&pkt.teardown_packet, 0, sizeof(pkt.teardown_packet));
38546e0832faSShawn Lin 	init_completion(&comp_pkt.host_event);
38556e0832faSShawn Lin 	pkt.teardown_packet.completion_func = hv_pci_generic_compl;
38566e0832faSShawn Lin 	pkt.teardown_packet.compl_ctxt = &comp_pkt;
38576e0832faSShawn Lin 	pkt.teardown_packet.message[0].type = PCI_BUS_D0EXIT;
38586e0832faSShawn Lin 
3859b4927bd2SAndrea Parri (Microsoft) 	ret = vmbus_sendpacket_getid(chan, &pkt.teardown_packet.message,
38606e0832faSShawn Lin 				     sizeof(struct pci_message),
38616e0832faSShawn Lin 				     (unsigned long)&pkt.teardown_packet,
3862b4927bd2SAndrea Parri (Microsoft) 				     &trans_id, VM_PKT_DATA_INBAND,
38636e0832faSShawn Lin 				     VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
3864a8e37506SDexuan Cui 	if (ret)
3865a8e37506SDexuan Cui 		return ret;
3866a8e37506SDexuan Cui 
3867b4927bd2SAndrea Parri (Microsoft) 	if (wait_for_completion_timeout(&comp_pkt.host_event, 10 * HZ) == 0) {
3868b4927bd2SAndrea Parri (Microsoft) 		/*
3869b4927bd2SAndrea Parri (Microsoft) 		 * The completion packet on the stack becomes invalid after
3870b4927bd2SAndrea Parri (Microsoft) 		 * 'return'; remove the ID from the VMbus requestor if the
3871b4927bd2SAndrea Parri (Microsoft) 		 * identifier is still mapped to/associated with the packet.
3872b4927bd2SAndrea Parri (Microsoft) 		 *
3873b4927bd2SAndrea Parri (Microsoft) 		 * Cf. hv_pci_onchannelcallback().
3874b4927bd2SAndrea Parri (Microsoft) 		 */
3875b4927bd2SAndrea Parri (Microsoft) 		vmbus_request_addr_match(chan, trans_id,
3876b4927bd2SAndrea Parri (Microsoft) 					 (unsigned long)&pkt.teardown_packet);
3877a8e37506SDexuan Cui 		return -ETIMEDOUT;
3878b4927bd2SAndrea Parri (Microsoft) 	}
3879a8e37506SDexuan Cui 
3880a8e37506SDexuan Cui 	return 0;
38816e0832faSShawn Lin }
38826e0832faSShawn Lin 
38836e0832faSShawn Lin /**
38846e0832faSShawn Lin  * hv_pci_remove() - Remove routine for this VMBus channel
38856e0832faSShawn Lin  * @hdev:	VMBus's tracking struct for this root PCI bus
38866e0832faSShawn Lin  */
hv_pci_remove(struct hv_device * hdev)388796ec2939SDawei Li static void hv_pci_remove(struct hv_device *hdev)
38886e0832faSShawn Lin {
38896e0832faSShawn Lin 	struct hv_pcibus_device *hbus;
38906e0832faSShawn Lin 
38916e0832faSShawn Lin 	hbus = hv_get_drvdata(hdev);
38926e0832faSShawn Lin 	if (hbus->state == hv_pcibus_installed) {
389394d22763SLong Li 		tasklet_disable(&hdev->channel->callback_event);
389494d22763SLong Li 		hbus->state = hv_pcibus_removing;
389594d22763SLong Li 		tasklet_enable(&hdev->channel->callback_event);
389694d22763SLong Li 		destroy_workqueue(hbus->wq);
389794d22763SLong Li 		hbus->wq = NULL;
389894d22763SLong Li 		/*
389994d22763SLong Li 		 * At this point, no work is running or can be scheduled
390094d22763SLong Li 		 * on hbus-wq. We can't race with hv_pci_devices_present()
390194d22763SLong Li 		 * or hv_pci_eject_device(), it's safe to proceed.
390294d22763SLong Li 		 */
390394d22763SLong Li 
39046e0832faSShawn Lin 		/* Remove the bus from PCI's point of view. */
39056e0832faSShawn Lin 		pci_lock_rescan_remove();
3906418cb6c8SArnd Bergmann 		pci_stop_root_bus(hbus->bridge->bus);
390715becc2bSDexuan Cui 		hv_pci_remove_slots(hbus);
3908418cb6c8SArnd Bergmann 		pci_remove_root_bus(hbus->bridge->bus);
39096e0832faSShawn Lin 		pci_unlock_rescan_remove();
39106e0832faSShawn Lin 	}
39116e0832faSShawn Lin 
391296ec2939SDawei Li 	hv_pci_bus_exit(hdev, false);
39136e0832faSShawn Lin 
39146e0832faSShawn Lin 	vmbus_close(hdev->channel);
39156e0832faSShawn Lin 
39166e0832faSShawn Lin 	iounmap(hbus->cfg_addr);
39176e0832faSShawn Lin 	hv_free_config_window(hbus);
39186e0832faSShawn Lin 	hv_pci_free_bridge_windows(hbus);
39196e0832faSShawn Lin 	irq_domain_remove(hbus->irq_domain);
39209e7f9178SBoqun Feng 	irq_domain_free_fwnode(hbus->fwnode);
3921be700103SHaiyang Zhang 
392238c0d266SBoqun Feng 	hv_put_dom_num(hbus->bridge->domain_nr);
3923be700103SHaiyang Zhang 
3924877b911aSDexuan Cui 	kfree(hbus);
39256e0832faSShawn Lin }
39266e0832faSShawn Lin 
hv_pci_suspend(struct hv_device * hdev)3927ac82fc83SDexuan Cui static int hv_pci_suspend(struct hv_device *hdev)
3928ac82fc83SDexuan Cui {
3929ac82fc83SDexuan Cui 	struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
3930ac82fc83SDexuan Cui 	enum hv_pcibus_state old_state;
3931ac82fc83SDexuan Cui 	int ret;
3932ac82fc83SDexuan Cui 
3933ac82fc83SDexuan Cui 	/*
3934ac82fc83SDexuan Cui 	 * hv_pci_suspend() must make sure there are no pending work items
3935ac82fc83SDexuan Cui 	 * before calling vmbus_close(), since it runs in a process context
3936ac82fc83SDexuan Cui 	 * as a callback in dpm_suspend().  When it starts to run, the channel
3937ac82fc83SDexuan Cui 	 * callback hv_pci_onchannelcallback(), which runs in a tasklet
3938ac82fc83SDexuan Cui 	 * context, can be still running concurrently and scheduling new work
3939ac82fc83SDexuan Cui 	 * items onto hbus->wq in hv_pci_devices_present() and
3940ac82fc83SDexuan Cui 	 * hv_pci_eject_device(), and the work item handlers can access the
3941ac82fc83SDexuan Cui 	 * vmbus channel, which can be being closed by hv_pci_suspend(), e.g.
3942ac82fc83SDexuan Cui 	 * the work item handler pci_devices_present_work() ->
3943ac82fc83SDexuan Cui 	 * new_pcichild_device() writes to the vmbus channel.
3944ac82fc83SDexuan Cui 	 *
3945ac82fc83SDexuan Cui 	 * To eliminate the race, hv_pci_suspend() disables the channel
3946ac82fc83SDexuan Cui 	 * callback tasklet, sets hbus->state to hv_pcibus_removing, and
3947ac82fc83SDexuan Cui 	 * re-enables the tasklet. This way, when hv_pci_suspend() proceeds,
3948ac82fc83SDexuan Cui 	 * it knows that no new work item can be scheduled, and then it flushes
3949ac82fc83SDexuan Cui 	 * hbus->wq and safely closes the vmbus channel.
3950ac82fc83SDexuan Cui 	 */
3951ac82fc83SDexuan Cui 	tasklet_disable(&hdev->channel->callback_event);
3952ac82fc83SDexuan Cui 
3953ac82fc83SDexuan Cui 	/* Change the hbus state to prevent new work items. */
3954ac82fc83SDexuan Cui 	old_state = hbus->state;
3955ac82fc83SDexuan Cui 	if (hbus->state == hv_pcibus_installed)
3956ac82fc83SDexuan Cui 		hbus->state = hv_pcibus_removing;
3957ac82fc83SDexuan Cui 
3958ac82fc83SDexuan Cui 	tasklet_enable(&hdev->channel->callback_event);
3959ac82fc83SDexuan Cui 
3960ac82fc83SDexuan Cui 	if (old_state != hv_pcibus_installed)
3961ac82fc83SDexuan Cui 		return -EINVAL;
3962ac82fc83SDexuan Cui 
3963ac82fc83SDexuan Cui 	flush_workqueue(hbus->wq);
3964ac82fc83SDexuan Cui 
3965ac82fc83SDexuan Cui 	ret = hv_pci_bus_exit(hdev, true);
3966ac82fc83SDexuan Cui 	if (ret)
3967ac82fc83SDexuan Cui 		return ret;
3968ac82fc83SDexuan Cui 
3969ac82fc83SDexuan Cui 	vmbus_close(hdev->channel);
3970ac82fc83SDexuan Cui 
39716e0832faSShawn Lin 	return 0;
39726e0832faSShawn Lin }
39736e0832faSShawn Lin 
hv_pci_restore_msi_msg(struct pci_dev * pdev,void * arg)3974915cff7fSDexuan Cui static int hv_pci_restore_msi_msg(struct pci_dev *pdev, void *arg)
3975915cff7fSDexuan Cui {
3976915cff7fSDexuan Cui 	struct irq_data *irq_data;
3977dc2b4532SThomas Gleixner 	struct msi_desc *entry;
3978dc2b4532SThomas Gleixner 	int ret = 0;
3979915cff7fSDexuan Cui 
398004bbe863SDexuan Cui 	if (!pdev->msi_enabled && !pdev->msix_enabled)
398104bbe863SDexuan Cui 		return 0;
398204bbe863SDexuan Cui 
3983dc2b4532SThomas Gleixner 	msi_lock_descs(&pdev->dev);
3984dc2b4532SThomas Gleixner 	msi_for_each_desc(entry, &pdev->dev, MSI_DESC_ASSOCIATED) {
3985915cff7fSDexuan Cui 		irq_data = irq_get_irq_data(entry->irq);
3986dc2b4532SThomas Gleixner 		if (WARN_ON_ONCE(!irq_data)) {
3987dc2b4532SThomas Gleixner 			ret = -EINVAL;
3988dc2b4532SThomas Gleixner 			break;
3989dc2b4532SThomas Gleixner 		}
3990915cff7fSDexuan Cui 
3991915cff7fSDexuan Cui 		hv_compose_msi_msg(irq_data, &entry->msg);
3992915cff7fSDexuan Cui 	}
3993dc2b4532SThomas Gleixner 	msi_unlock_descs(&pdev->dev);
3994915cff7fSDexuan Cui 
3995dc2b4532SThomas Gleixner 	return ret;
3996915cff7fSDexuan Cui }
3997915cff7fSDexuan Cui 
3998915cff7fSDexuan Cui /*
3999915cff7fSDexuan Cui  * Upon resume, pci_restore_msi_state() -> ... ->  __pci_write_msi_msg()
4000915cff7fSDexuan Cui  * directly writes the MSI/MSI-X registers via MMIO, but since Hyper-V
4001915cff7fSDexuan Cui  * doesn't trap and emulate the MMIO accesses, here hv_compose_msi_msg()
4002915cff7fSDexuan Cui  * must be used to ask Hyper-V to re-create the IOMMU Interrupt Remapping
4003915cff7fSDexuan Cui  * Table entries.
4004915cff7fSDexuan Cui  */
hv_pci_restore_msi_state(struct hv_pcibus_device * hbus)4005915cff7fSDexuan Cui static void hv_pci_restore_msi_state(struct hv_pcibus_device *hbus)
4006915cff7fSDexuan Cui {
4007418cb6c8SArnd Bergmann 	pci_walk_bus(hbus->bridge->bus, hv_pci_restore_msi_msg, NULL);
4008915cff7fSDexuan Cui }
4009915cff7fSDexuan Cui 
hv_pci_resume(struct hv_device * hdev)4010ac82fc83SDexuan Cui static int hv_pci_resume(struct hv_device *hdev)
4011ac82fc83SDexuan Cui {
4012ac82fc83SDexuan Cui 	struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
4013ac82fc83SDexuan Cui 	enum pci_protocol_version_t version[1];
4014ac82fc83SDexuan Cui 	int ret;
4015ac82fc83SDexuan Cui 
4016ac82fc83SDexuan Cui 	hbus->state = hv_pcibus_init;
4017ac82fc83SDexuan Cui 
4018de5ddb7dSAndrea Parri (Microsoft) 	hdev->channel->next_request_id_callback = vmbus_next_request_id;
4019de5ddb7dSAndrea Parri (Microsoft) 	hdev->channel->request_addr_callback = vmbus_request_addr;
4020de5ddb7dSAndrea Parri (Microsoft) 	hdev->channel->rqstor_size = HV_PCI_RQSTOR_SIZE;
4021de5ddb7dSAndrea Parri (Microsoft) 
4022ac82fc83SDexuan Cui 	ret = vmbus_open(hdev->channel, pci_ring_size, pci_ring_size, NULL, 0,
4023ac82fc83SDexuan Cui 			 hv_pci_onchannelcallback, hbus);
4024ac82fc83SDexuan Cui 	if (ret)
4025ac82fc83SDexuan Cui 		return ret;
4026ac82fc83SDexuan Cui 
4027ac82fc83SDexuan Cui 	/* Only use the version that was in use before hibernation. */
402814ef39fdSDexuan Cui 	version[0] = hbus->protocol_version;
4029ac82fc83SDexuan Cui 	ret = hv_pci_protocol_negotiation(hdev, version, 1);
4030ac82fc83SDexuan Cui 	if (ret)
4031ac82fc83SDexuan Cui 		goto out;
4032ac82fc83SDexuan Cui 
4033ac82fc83SDexuan Cui 	ret = hv_pci_query_relations(hdev);
4034ac82fc83SDexuan Cui 	if (ret)
4035ac82fc83SDexuan Cui 		goto out;
4036ac82fc83SDexuan Cui 
4037067d6ec7SDexuan Cui 	mutex_lock(&hbus->state_lock);
4038067d6ec7SDexuan Cui 
4039ac82fc83SDexuan Cui 	ret = hv_pci_enter_d0(hdev);
4040ac82fc83SDexuan Cui 	if (ret)
4041067d6ec7SDexuan Cui 		goto release_state_lock;
4042ac82fc83SDexuan Cui 
4043ac82fc83SDexuan Cui 	ret = hv_send_resources_allocated(hdev);
4044ac82fc83SDexuan Cui 	if (ret)
4045067d6ec7SDexuan Cui 		goto release_state_lock;
4046ac82fc83SDexuan Cui 
4047ac82fc83SDexuan Cui 	prepopulate_bars(hbus);
4048ac82fc83SDexuan Cui 
4049915cff7fSDexuan Cui 	hv_pci_restore_msi_state(hbus);
4050915cff7fSDexuan Cui 
4051ac82fc83SDexuan Cui 	hbus->state = hv_pcibus_installed;
4052067d6ec7SDexuan Cui 	mutex_unlock(&hbus->state_lock);
4053ac82fc83SDexuan Cui 	return 0;
4054067d6ec7SDexuan Cui 
4055067d6ec7SDexuan Cui release_state_lock:
4056067d6ec7SDexuan Cui 	mutex_unlock(&hbus->state_lock);
4057ac82fc83SDexuan Cui out:
4058ac82fc83SDexuan Cui 	vmbus_close(hdev->channel);
4059ac82fc83SDexuan Cui 	return ret;
4060ac82fc83SDexuan Cui }
4061ac82fc83SDexuan Cui 
40626e0832faSShawn Lin static const struct hv_vmbus_device_id hv_pci_id_table[] = {
40636e0832faSShawn Lin 	/* PCI Pass-through Class ID */
40646e0832faSShawn Lin 	/* 44C4F61D-4444-4400-9D52-802E27EDE19F */
40656e0832faSShawn Lin 	{ HV_PCIE_GUID, },
40666e0832faSShawn Lin 	{ },
40676e0832faSShawn Lin };
40686e0832faSShawn Lin 
40696e0832faSShawn Lin MODULE_DEVICE_TABLE(vmbus, hv_pci_id_table);
40706e0832faSShawn Lin 
40716e0832faSShawn Lin static struct hv_driver hv_pci_drv = {
40726e0832faSShawn Lin 	.name		= "hv_pci",
40736e0832faSShawn Lin 	.id_table	= hv_pci_id_table,
40746e0832faSShawn Lin 	.probe		= hv_pci_probe,
40756e0832faSShawn Lin 	.remove		= hv_pci_remove,
4076ac82fc83SDexuan Cui 	.suspend	= hv_pci_suspend,
4077ac82fc83SDexuan Cui 	.resume		= hv_pci_resume,
40786e0832faSShawn Lin };
40796e0832faSShawn Lin 
exit_hv_pci_drv(void)40806e0832faSShawn Lin static void __exit exit_hv_pci_drv(void)
40816e0832faSShawn Lin {
40826e0832faSShawn Lin 	vmbus_driver_unregister(&hv_pci_drv);
4083348dd93eSHaiyang Zhang 
4084348dd93eSHaiyang Zhang 	hvpci_block_ops.read_block = NULL;
4085348dd93eSHaiyang Zhang 	hvpci_block_ops.write_block = NULL;
4086348dd93eSHaiyang Zhang 	hvpci_block_ops.reg_blk_invalidate = NULL;
40876e0832faSShawn Lin }
40886e0832faSShawn Lin 
init_hv_pci_drv(void)40896e0832faSShawn Lin static int __init init_hv_pci_drv(void)
40906e0832faSShawn Lin {
4091831c1ae7SSunil Muthuswamy 	int ret;
4092831c1ae7SSunil Muthuswamy 
40937d815f4aSHaiyang Zhang 	if (!hv_is_hyperv_initialized())
40947d815f4aSHaiyang Zhang 		return -ENODEV;
40957d815f4aSHaiyang Zhang 
4096831c1ae7SSunil Muthuswamy 	ret = hv_pci_irqchip_init();
4097831c1ae7SSunil Muthuswamy 	if (ret)
4098831c1ae7SSunil Muthuswamy 		return ret;
4099831c1ae7SSunil Muthuswamy 
4100be700103SHaiyang Zhang 	/* Set the invalid domain number's bit, so it will not be used */
4101be700103SHaiyang Zhang 	set_bit(HVPCI_DOM_INVALID, hvpci_dom_map);
4102be700103SHaiyang Zhang 
4103348dd93eSHaiyang Zhang 	/* Initialize PCI block r/w interface */
4104348dd93eSHaiyang Zhang 	hvpci_block_ops.read_block = hv_read_config_block;
4105348dd93eSHaiyang Zhang 	hvpci_block_ops.write_block = hv_write_config_block;
4106348dd93eSHaiyang Zhang 	hvpci_block_ops.reg_blk_invalidate = hv_register_block_invalidate;
4107348dd93eSHaiyang Zhang 
41086e0832faSShawn Lin 	return vmbus_driver_register(&hv_pci_drv);
41096e0832faSShawn Lin }
41106e0832faSShawn Lin 
41116e0832faSShawn Lin module_init(init_hv_pci_drv);
41126e0832faSShawn Lin module_exit(exit_hv_pci_drv);
41136e0832faSShawn Lin 
41146e0832faSShawn Lin MODULE_DESCRIPTION("Hyper-V PCI");
41156e0832faSShawn Lin MODULE_LICENSE("GPL v2");
4116