2018-01-26 18:50:27 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2016-02-16 21:56:23 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
*
|
|
|
|
* Author:
|
|
|
|
* Jake Oshins <jakeo@microsoft.com>
|
|
|
|
*
|
|
|
|
* This driver acts as a paravirtual front-end for PCI Express root buses.
|
|
|
|
* When a PCI Express function (either an entire device or an SR-IOV
|
|
|
|
* Virtual Function) is being passed through to the VM, this driver exposes
|
|
|
|
* a new bus to the guest VM. This is modeled as a root PCI bus because
|
|
|
|
* no bridges are being exposed to the VM. In fact, with a "Generation 2"
|
|
|
|
* VM within Hyper-V, there may seem to be no PCI bus at all in the VM
|
|
|
|
* until a device as been exposed using this driver.
|
|
|
|
*
|
|
|
|
* Each root PCI bus has its own PCI domain, which is called "Segment" in
|
|
|
|
* the PCI Firmware Specifications. Thus while each device passed through
|
|
|
|
* to the VM using this front-end will appear at "device 0", the domain will
|
|
|
|
* be unique. Typically, each bus will have one PCI function on it, though
|
|
|
|
* this driver does support more than one.
|
|
|
|
*
|
|
|
|
* In order to map the interrupts from the device through to the guest VM,
|
|
|
|
* this driver also implements an IRQ Domain, which handles interrupts (either
|
|
|
|
* MSI or MSI-X) associated with the functions on the bus. As interrupts are
|
|
|
|
* set up, torn down, or reaffined, this driver communicates with the
|
|
|
|
* underlying hypervisor to adjust the mappings in the I/O MMU so that each
|
|
|
|
* interrupt will be delivered to the correct virtual processor at the right
|
|
|
|
* vector. This driver does not support level-triggered (line-based)
|
|
|
|
* interrupts, and will report that the Interrupt Line register in the
|
|
|
|
* function's configuration space is zero.
|
|
|
|
*
|
|
|
|
* The rest of this driver mostly maps PCI concepts onto underlying Hyper-V
|
|
|
|
* facilities. For instance, the configuration space of a function exposed
|
|
|
|
* by Hyper-V is mapped into a single page of memory space, and the
|
|
|
|
* read and write handlers for config space must be aware of this mechanism.
|
|
|
|
* Similarly, device setup and teardown involves messages sent to and from
|
|
|
|
* the PCI back-end driver in Hyper-V.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/pci.h>
|
2021-07-26 18:06:57 +00:00
|
|
|
#include <linux/pci-ecam.h>
|
2017-07-31 23:48:29 +00:00
|
|
|
#include <linux/delay.h>
|
2016-02-16 21:56:23 +00:00
|
|
|
#include <linux/semaphore.h>
|
2018-07-29 10:15:33 +00:00
|
|
|
#include <linux/irq.h>
|
2016-02-16 21:56:23 +00:00
|
|
|
#include <linux/msi.h>
|
|
|
|
#include <linux/hyperv.h>
|
2017-04-18 14:02:48 +00:00
|
|
|
#include <linux/refcount.h>
|
2022-01-05 19:32:36 +00:00
|
|
|
#include <linux/irqdomain.h>
|
|
|
|
#include <linux/acpi.h>
|
2016-02-16 21:56:23 +00:00
|
|
|
#include <asm/mshyperv.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Protocol versions. The low word is the minor version, the high word the
|
|
|
|
* major version.
|
|
|
|
*/
|
|
|
|
|
2017-05-24 20:41:27 +00:00
|
|
|
#define PCI_MAKE_VERSION(major, minor) ((u32)(((major) << 16) | (minor)))
|
2016-02-16 21:56:23 +00:00
|
|
|
#define PCI_MAJOR_VERSION(version) ((u32)(version) >> 16)
|
|
|
|
#define PCI_MINOR_VERSION(version) ((u32)(version) & 0xff)
|
|
|
|
|
2017-05-24 20:41:27 +00:00
|
|
|
enum pci_protocol_version_t {
|
|
|
|
PCI_PROTOCOL_VERSION_1_1 = PCI_MAKE_VERSION(1, 1), /* Win10 */
|
2017-05-24 20:41:28 +00:00
|
|
|
PCI_PROTOCOL_VERSION_1_2 = PCI_MAKE_VERSION(1, 2), /* RS1 */
|
2020-02-26 05:06:08 +00:00
|
|
|
PCI_PROTOCOL_VERSION_1_3 = PCI_MAKE_VERSION(1, 3), /* Vibranium */
|
2021-07-12 21:58:18 +00:00
|
|
|
PCI_PROTOCOL_VERSION_1_4 = PCI_MAKE_VERSION(1, 4), /* WS2022 */
|
2016-02-16 21:56:23 +00:00
|
|
|
};
|
|
|
|
|
2017-03-24 18:07:21 +00:00
|
|
|
#define CPU_AFFINITY_ALL -1ULL
|
2017-05-24 20:41:27 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Supported protocol versions in the order of probing - highest go
|
|
|
|
* first.
|
|
|
|
*/
|
|
|
|
static enum pci_protocol_version_t pci_protocol_versions[] = {
|
2021-07-12 21:58:18 +00:00
|
|
|
PCI_PROTOCOL_VERSION_1_4,
|
2020-02-26 05:06:08 +00:00
|
|
|
PCI_PROTOCOL_VERSION_1_3,
|
2017-05-24 20:41:28 +00:00
|
|
|
PCI_PROTOCOL_VERSION_1_2,
|
2017-05-24 20:41:27 +00:00
|
|
|
PCI_PROTOCOL_VERSION_1_1,
|
|
|
|
};
|
|
|
|
|
2016-02-16 21:56:23 +00:00
|
|
|
#define PCI_CONFIG_MMIO_LENGTH 0x2000
|
|
|
|
#define CFG_PAGE_OFFSET 0x1000
|
|
|
|
#define CFG_PAGE_SIZE (PCI_CONFIG_MMIO_LENGTH - CFG_PAGE_OFFSET)
|
|
|
|
|
|
|
|
#define MAX_SUPPORTED_MSI_MESSAGES 0x400
|
|
|
|
|
2017-05-24 20:41:27 +00:00
|
|
|
#define STATUS_REVISION_MISMATCH 0xC0000059
|
|
|
|
|
2018-09-14 19:54:56 +00:00
|
|
|
/* space for 32bit serial number as string */
|
|
|
|
#define SLOT_NAME_SIZE 11
|
|
|
|
|
2022-04-19 12:23:21 +00:00
|
|
|
/*
|
|
|
|
* Size of requestor for VMbus; the value is based on the observation
|
|
|
|
* that having more than one request outstanding is 'rare', and so 64
|
|
|
|
* should be generous in ensuring that we don't ever run out.
|
|
|
|
*/
|
|
|
|
#define HV_PCI_RQSTOR_SIZE 64
|
|
|
|
|
2016-02-16 21:56:23 +00:00
|
|
|
/*
|
|
|
|
* Message Types
|
|
|
|
*/
|
|
|
|
|
|
|
|
enum pci_message_type {
|
|
|
|
/*
|
|
|
|
* Version 1.1
|
|
|
|
*/
|
|
|
|
PCI_MESSAGE_BASE = 0x42490000,
|
|
|
|
PCI_BUS_RELATIONS = PCI_MESSAGE_BASE + 0,
|
|
|
|
PCI_QUERY_BUS_RELATIONS = PCI_MESSAGE_BASE + 1,
|
|
|
|
PCI_POWER_STATE_CHANGE = PCI_MESSAGE_BASE + 4,
|
|
|
|
PCI_QUERY_RESOURCE_REQUIREMENTS = PCI_MESSAGE_BASE + 5,
|
|
|
|
PCI_QUERY_RESOURCE_RESOURCES = PCI_MESSAGE_BASE + 6,
|
|
|
|
PCI_BUS_D0ENTRY = PCI_MESSAGE_BASE + 7,
|
|
|
|
PCI_BUS_D0EXIT = PCI_MESSAGE_BASE + 8,
|
|
|
|
PCI_READ_BLOCK = PCI_MESSAGE_BASE + 9,
|
|
|
|
PCI_WRITE_BLOCK = PCI_MESSAGE_BASE + 0xA,
|
|
|
|
PCI_EJECT = PCI_MESSAGE_BASE + 0xB,
|
|
|
|
PCI_QUERY_STOP = PCI_MESSAGE_BASE + 0xC,
|
|
|
|
PCI_REENABLE = PCI_MESSAGE_BASE + 0xD,
|
|
|
|
PCI_QUERY_STOP_FAILED = PCI_MESSAGE_BASE + 0xE,
|
|
|
|
PCI_EJECTION_COMPLETE = PCI_MESSAGE_BASE + 0xF,
|
|
|
|
PCI_RESOURCES_ASSIGNED = PCI_MESSAGE_BASE + 0x10,
|
|
|
|
PCI_RESOURCES_RELEASED = PCI_MESSAGE_BASE + 0x11,
|
|
|
|
PCI_INVALIDATE_BLOCK = PCI_MESSAGE_BASE + 0x12,
|
|
|
|
PCI_QUERY_PROTOCOL_VERSION = PCI_MESSAGE_BASE + 0x13,
|
|
|
|
PCI_CREATE_INTERRUPT_MESSAGE = PCI_MESSAGE_BASE + 0x14,
|
|
|
|
PCI_DELETE_INTERRUPT_MESSAGE = PCI_MESSAGE_BASE + 0x15,
|
2017-05-24 20:41:28 +00:00
|
|
|
PCI_RESOURCES_ASSIGNED2 = PCI_MESSAGE_BASE + 0x16,
|
|
|
|
PCI_CREATE_INTERRUPT_MESSAGE2 = PCI_MESSAGE_BASE + 0x17,
|
|
|
|
PCI_DELETE_INTERRUPT_MESSAGE2 = PCI_MESSAGE_BASE + 0x18, /* unused */
|
2020-02-26 05:06:08 +00:00
|
|
|
PCI_BUS_RELATIONS2 = PCI_MESSAGE_BASE + 0x19,
|
2021-07-12 21:58:18 +00:00
|
|
|
PCI_RESOURCES_ASSIGNED3 = PCI_MESSAGE_BASE + 0x1A,
|
|
|
|
PCI_CREATE_INTERRUPT_MESSAGE3 = PCI_MESSAGE_BASE + 0x1B,
|
2016-02-16 21:56:23 +00:00
|
|
|
PCI_MESSAGE_MAXIMUM
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Structures defining the virtual PCI Express protocol.
|
|
|
|
*/
|
|
|
|
|
|
|
|
union pci_version {
|
|
|
|
struct {
|
|
|
|
u16 minor_version;
|
|
|
|
u16 major_version;
|
|
|
|
} parts;
|
|
|
|
u32 version;
|
|
|
|
} __packed;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Function numbers are 8-bits wide on Express, as interpreted through ARI,
|
|
|
|
* which is all this driver does. This representation is the one used in
|
|
|
|
* Windows, which is what is expected when sending this back and forth with
|
|
|
|
* the Hyper-V parent partition.
|
|
|
|
*/
|
|
|
|
union win_slot_encoding {
|
|
|
|
struct {
|
2017-02-10 21:18:46 +00:00
|
|
|
u32 dev:5;
|
|
|
|
u32 func:3;
|
2016-02-16 21:56:23 +00:00
|
|
|
u32 reserved:24;
|
|
|
|
} bits;
|
|
|
|
u32 slot;
|
|
|
|
} __packed;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Pretty much as defined in the PCI Specifications.
|
|
|
|
*/
|
|
|
|
struct pci_function_description {
|
|
|
|
u16 v_id; /* vendor ID */
|
|
|
|
u16 d_id; /* device ID */
|
|
|
|
u8 rev;
|
|
|
|
u8 prog_intf;
|
|
|
|
u8 subclass;
|
|
|
|
u8 base_class;
|
|
|
|
u32 subsystem_id;
|
|
|
|
union win_slot_encoding win_slot;
|
|
|
|
u32 ser; /* serial number */
|
|
|
|
} __packed;
|
|
|
|
|
2020-02-26 05:06:08 +00:00
|
|
|
enum pci_device_description_flags {
|
|
|
|
HV_PCI_DEVICE_FLAG_NONE = 0x0,
|
|
|
|
HV_PCI_DEVICE_FLAG_NUMA_AFFINITY = 0x1,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct pci_function_description2 {
|
|
|
|
u16 v_id; /* vendor ID */
|
|
|
|
u16 d_id; /* device ID */
|
|
|
|
u8 rev;
|
|
|
|
u8 prog_intf;
|
|
|
|
u8 subclass;
|
|
|
|
u8 base_class;
|
|
|
|
u32 subsystem_id;
|
|
|
|
union win_slot_encoding win_slot;
|
|
|
|
u32 ser; /* serial number */
|
|
|
|
u32 flags;
|
|
|
|
u16 virtual_numa_node;
|
|
|
|
u16 reserved;
|
|
|
|
} __packed;
|
|
|
|
|
2016-02-16 21:56:23 +00:00
|
|
|
/**
|
|
|
|
* struct hv_msi_desc
|
|
|
|
* @vector: IDT entry
|
|
|
|
* @delivery_mode: As defined in Intel's Programmer's
|
|
|
|
* Reference Manual, Volume 3, Chapter 8.
|
|
|
|
* @vector_count: Number of contiguous entries in the
|
|
|
|
* Interrupt Descriptor Table that are
|
|
|
|
* occupied by this Message-Signaled
|
|
|
|
* Interrupt. For "MSI", as first defined
|
|
|
|
* in PCI 2.2, this can be between 1 and
|
|
|
|
* 32. For "MSI-X," as first defined in PCI
|
|
|
|
* 3.0, this must be 1, as each MSI-X table
|
|
|
|
* entry would have its own descriptor.
|
|
|
|
* @reserved: Empty space
|
|
|
|
* @cpu_mask: All the target virtual processors.
|
|
|
|
*/
|
|
|
|
struct hv_msi_desc {
|
|
|
|
u8 vector;
|
|
|
|
u8 delivery_mode;
|
|
|
|
u16 vector_count;
|
|
|
|
u32 reserved;
|
|
|
|
u64 cpu_mask;
|
|
|
|
} __packed;
|
|
|
|
|
2017-05-24 20:41:28 +00:00
|
|
|
/**
|
|
|
|
* struct hv_msi_desc2 - 1.2 version of hv_msi_desc
|
|
|
|
* @vector: IDT entry
|
|
|
|
* @delivery_mode: As defined in Intel's Programmer's
|
|
|
|
* Reference Manual, Volume 3, Chapter 8.
|
|
|
|
* @vector_count: Number of contiguous entries in the
|
|
|
|
* Interrupt Descriptor Table that are
|
|
|
|
* occupied by this Message-Signaled
|
|
|
|
* Interrupt. For "MSI", as first defined
|
|
|
|
* in PCI 2.2, this can be between 1 and
|
|
|
|
* 32. For "MSI-X," as first defined in PCI
|
|
|
|
* 3.0, this must be 1, as each MSI-X table
|
|
|
|
* entry would have its own descriptor.
|
|
|
|
* @processor_count: number of bits enabled in array.
|
|
|
|
* @processor_array: All the target virtual processors.
|
|
|
|
*/
|
|
|
|
struct hv_msi_desc2 {
|
|
|
|
u8 vector;
|
|
|
|
u8 delivery_mode;
|
|
|
|
u16 vector_count;
|
|
|
|
u16 processor_count;
|
|
|
|
u16 processor_array[32];
|
|
|
|
} __packed;
|
|
|
|
|
2021-07-12 21:58:18 +00:00
|
|
|
/*
|
|
|
|
* struct hv_msi_desc3 - 1.3 version of hv_msi_desc
|
|
|
|
* Everything is the same as in 'hv_msi_desc2' except that the size of the
|
|
|
|
* 'vector' field is larger to support bigger vector values. For ex: LPI
|
|
|
|
* vectors on ARM.
|
|
|
|
*/
|
|
|
|
struct hv_msi_desc3 {
|
|
|
|
u32 vector;
|
|
|
|
u8 delivery_mode;
|
|
|
|
u8 reserved;
|
|
|
|
u16 vector_count;
|
|
|
|
u16 processor_count;
|
|
|
|
u16 processor_array[32];
|
|
|
|
} __packed;
|
|
|
|
|
2016-02-16 21:56:23 +00:00
|
|
|
/**
|
|
|
|
* struct tran_int_desc
|
|
|
|
* @reserved: unused, padding
|
|
|
|
* @vector_count: same as in hv_msi_desc
|
|
|
|
* @data: This is the "data payload" value that is
|
|
|
|
* written by the device when it generates
|
|
|
|
* a message-signaled interrupt, either MSI
|
|
|
|
* or MSI-X.
|
|
|
|
* @address: This is the address to which the data
|
|
|
|
* payload is written on interrupt
|
|
|
|
* generation.
|
|
|
|
*/
|
|
|
|
struct tran_int_desc {
|
|
|
|
u16 reserved;
|
|
|
|
u16 vector_count;
|
|
|
|
u32 data;
|
|
|
|
u64 address;
|
|
|
|
} __packed;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A generic message format for virtual PCI.
|
|
|
|
* Specific message formats are defined later in the file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct pci_message {
|
2016-08-23 04:45:51 +00:00
|
|
|
u32 type;
|
2016-02-16 21:56:23 +00:00
|
|
|
} __packed;
|
|
|
|
|
|
|
|
struct pci_child_message {
|
2016-08-23 04:45:51 +00:00
|
|
|
struct pci_message message_type;
|
2016-02-16 21:56:23 +00:00
|
|
|
union win_slot_encoding wslot;
|
|
|
|
} __packed;
|
|
|
|
|
|
|
|
struct pci_incoming_message {
|
|
|
|
struct vmpacket_descriptor hdr;
|
|
|
|
struct pci_message message_type;
|
|
|
|
} __packed;
|
|
|
|
|
|
|
|
struct pci_response {
|
|
|
|
struct vmpacket_descriptor hdr;
|
|
|
|
s32 status; /* negative values are failures */
|
|
|
|
} __packed;
|
|
|
|
|
|
|
|
struct pci_packet {
|
|
|
|
void (*completion_func)(void *context, struct pci_response *resp,
|
|
|
|
int resp_packet_size);
|
|
|
|
void *compl_ctxt;
|
2016-08-23 04:45:51 +00:00
|
|
|
|
2020-02-13 00:50:48 +00:00
|
|
|
struct pci_message message[];
|
2016-02-16 21:56:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Specific message types supporting the PCI protocol.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Version negotiation message. Sent from the guest to the host.
|
|
|
|
* The guest is free to try different versions until the host
|
|
|
|
* accepts the version.
|
|
|
|
*
|
|
|
|
* pci_version: The protocol version requested.
|
|
|
|
* is_last_attempt: If TRUE, this is the last version guest will request.
|
|
|
|
* reservedz: Reserved field, set to zero.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct pci_version_request {
|
|
|
|
struct pci_message message_type;
|
2017-05-24 20:41:24 +00:00
|
|
|
u32 protocol_version;
|
2016-02-16 21:56:23 +00:00
|
|
|
} __packed;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Bus D0 Entry. This is sent from the guest to the host when the virtual
|
|
|
|
* bus (PCI Express port) is ready for action.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct pci_bus_d0_entry {
|
|
|
|
struct pci_message message_type;
|
|
|
|
u32 reserved;
|
|
|
|
u64 mmio_base;
|
|
|
|
} __packed;
|
|
|
|
|
|
|
|
struct pci_bus_relations {
|
|
|
|
struct pci_incoming_message incoming;
|
|
|
|
u32 device_count;
|
2020-02-13 00:50:48 +00:00
|
|
|
struct pci_function_description func[];
|
2016-02-16 21:56:23 +00:00
|
|
|
} __packed;
|
|
|
|
|
2020-02-26 05:06:08 +00:00
|
|
|
struct pci_bus_relations2 {
|
|
|
|
struct pci_incoming_message incoming;
|
|
|
|
u32 device_count;
|
2020-02-13 00:50:48 +00:00
|
|
|
struct pci_function_description2 func[];
|
2020-02-26 05:06:08 +00:00
|
|
|
} __packed;
|
|
|
|
|
2016-02-16 21:56:23 +00:00
|
|
|
struct pci_q_res_req_response {
|
|
|
|
struct vmpacket_descriptor hdr;
|
|
|
|
s32 status; /* negative values are failures */
|
2019-09-27 23:43:08 +00:00
|
|
|
u32 probed_bar[PCI_STD_NUM_BARS];
|
2016-02-16 21:56:23 +00:00
|
|
|
} __packed;
|
|
|
|
|
|
|
|
struct pci_set_power {
|
|
|
|
struct pci_message message_type;
|
|
|
|
union win_slot_encoding wslot;
|
|
|
|
u32 power_state; /* In Windows terms */
|
|
|
|
u32 reserved;
|
|
|
|
} __packed;
|
|
|
|
|
|
|
|
struct pci_set_power_response {
|
|
|
|
struct vmpacket_descriptor hdr;
|
|
|
|
s32 status; /* negative values are failures */
|
|
|
|
union win_slot_encoding wslot;
|
|
|
|
u32 resultant_state; /* In Windows terms */
|
|
|
|
u32 reserved;
|
|
|
|
} __packed;
|
|
|
|
|
|
|
|
struct pci_resources_assigned {
|
|
|
|
struct pci_message message_type;
|
|
|
|
union win_slot_encoding wslot;
|
|
|
|
u8 memory_range[0x14][6]; /* not used here */
|
|
|
|
u32 msi_descriptors;
|
|
|
|
u32 reserved[4];
|
|
|
|
} __packed;
|
|
|
|
|
2017-05-24 20:41:28 +00:00
|
|
|
struct pci_resources_assigned2 {
|
|
|
|
struct pci_message message_type;
|
|
|
|
union win_slot_encoding wslot;
|
|
|
|
u8 memory_range[0x14][6]; /* not used here */
|
|
|
|
u32 msi_descriptor_count;
|
|
|
|
u8 reserved[70];
|
|
|
|
} __packed;
|
|
|
|
|
2016-02-16 21:56:23 +00:00
|
|
|
struct pci_create_interrupt {
|
|
|
|
struct pci_message message_type;
|
|
|
|
union win_slot_encoding wslot;
|
|
|
|
struct hv_msi_desc int_desc;
|
|
|
|
} __packed;
|
|
|
|
|
|
|
|
struct pci_create_int_response {
|
|
|
|
struct pci_response response;
|
|
|
|
u32 reserved;
|
|
|
|
struct tran_int_desc int_desc;
|
|
|
|
} __packed;
|
|
|
|
|
2017-05-24 20:41:28 +00:00
|
|
|
struct pci_create_interrupt2 {
|
|
|
|
struct pci_message message_type;
|
|
|
|
union win_slot_encoding wslot;
|
|
|
|
struct hv_msi_desc2 int_desc;
|
|
|
|
} __packed;
|
|
|
|
|
2021-07-12 21:58:18 +00:00
|
|
|
struct pci_create_interrupt3 {
|
|
|
|
struct pci_message message_type;
|
|
|
|
union win_slot_encoding wslot;
|
|
|
|
struct hv_msi_desc3 int_desc;
|
|
|
|
} __packed;
|
|
|
|
|
2016-02-16 21:56:23 +00:00
|
|
|
struct pci_delete_interrupt {
|
|
|
|
struct pci_message message_type;
|
|
|
|
union win_slot_encoding wslot;
|
|
|
|
struct tran_int_desc int_desc;
|
|
|
|
} __packed;
|
|
|
|
|
2019-08-22 05:05:37 +00:00
|
|
|
/*
|
|
|
|
* Note: the VM must pass a valid block id, wslot and bytes_requested.
|
|
|
|
*/
|
|
|
|
struct pci_read_block {
|
|
|
|
struct pci_message message_type;
|
|
|
|
u32 block_id;
|
|
|
|
union win_slot_encoding wslot;
|
|
|
|
u32 bytes_requested;
|
|
|
|
} __packed;
|
|
|
|
|
|
|
|
struct pci_read_block_response {
|
|
|
|
struct vmpacket_descriptor hdr;
|
|
|
|
u32 status;
|
|
|
|
u8 bytes[HV_CONFIG_BLOCK_SIZE_MAX];
|
|
|
|
} __packed;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note: the VM must pass a valid block id, wslot and byte_count.
|
|
|
|
*/
|
|
|
|
struct pci_write_block {
|
|
|
|
struct pci_message message_type;
|
|
|
|
u32 block_id;
|
|
|
|
union win_slot_encoding wslot;
|
|
|
|
u32 byte_count;
|
|
|
|
u8 bytes[HV_CONFIG_BLOCK_SIZE_MAX];
|
|
|
|
} __packed;
|
|
|
|
|
|
|
|
struct pci_dev_inval_block {
|
|
|
|
struct pci_incoming_message incoming;
|
|
|
|
union win_slot_encoding wslot;
|
|
|
|
u64 block_mask;
|
|
|
|
} __packed;
|
|
|
|
|
2016-02-16 21:56:23 +00:00
|
|
|
struct pci_dev_incoming {
|
|
|
|
struct pci_incoming_message incoming;
|
|
|
|
union win_slot_encoding wslot;
|
|
|
|
} __packed;
|
|
|
|
|
|
|
|
struct pci_eject_response {
|
2016-08-23 04:45:51 +00:00
|
|
|
struct pci_message message_type;
|
2016-02-16 21:56:23 +00:00
|
|
|
union win_slot_encoding wslot;
|
|
|
|
u32 status;
|
|
|
|
} __packed;
|
|
|
|
|
|
|
|
static int pci_ring_size = (4 * PAGE_SIZE);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Driver specific state.
|
|
|
|
*/
|
|
|
|
|
|
|
|
enum hv_pcibus_state {
|
|
|
|
hv_pcibus_init = 0,
|
|
|
|
hv_pcibus_probed,
|
|
|
|
hv_pcibus_installed,
|
PCI: hv: Add hibernation support
Add suspend() and resume() functions so that Hyper-V virtual PCI devices
are handled properly when the VM hibernates and resumes from
hibernation.
Note that the suspend() function must make sure there are no pending
work items before calling vmbus_close(), since it runs in a process
context as a callback in dpm_suspend(). When it starts to run, the
channel callback hv_pci_onchannelcallback(), which runs in a tasklet
context, can be still running concurrently and scheduling new work items
onto hbus->wq in hv_pci_devices_present() and hv_pci_eject_device(), and
the work item handlers can access the vmbus channel, which can be being
closed by hv_pci_suspend(), e.g. the work item handler
pci_devices_present_work() -> new_pcichild_device() writes to the vmbus
channel.
To eliminate the race, hv_pci_suspend() disables the channel callback
tasklet, sets hbus->state to hv_pcibus_removing, and re-enables the
tasklet. This way, when hv_pci_suspend() proceeds, it knows that no new
work item can be scheduled, and then it flushes hbus->wq and safely
closes the vmbus channel.
Signed-off-by: Dexuan Cui <decui@microsoft.com>
Signed-off-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Reviewed-by: Michael Kelley <mikelley@microsoft.com>
2019-11-25 05:33:52 +00:00
|
|
|
hv_pcibus_removing,
|
2016-02-16 21:56:23 +00:00
|
|
|
hv_pcibus_maximum
|
|
|
|
};
|
|
|
|
|
|
|
|
struct hv_pcibus_device {
|
2021-07-26 18:06:57 +00:00
|
|
|
#ifdef CONFIG_X86
|
2016-02-16 21:56:23 +00:00
|
|
|
struct pci_sysdata sysdata;
|
2021-07-26 18:06:57 +00:00
|
|
|
#elif defined(CONFIG_ARM64)
|
|
|
|
struct pci_config_window sysdata;
|
|
|
|
#endif
|
2021-07-26 18:06:54 +00:00
|
|
|
struct pci_host_bridge *bridge;
|
2021-07-26 18:06:56 +00:00
|
|
|
struct fwnode_handle *fwnode;
|
2019-11-25 05:33:53 +00:00
|
|
|
/* Protocol version negotiated with the host */
|
|
|
|
enum pci_protocol_version_t protocol_version;
|
2016-02-16 21:56:23 +00:00
|
|
|
enum hv_pcibus_state state;
|
|
|
|
struct hv_device *hdev;
|
|
|
|
resource_size_t low_mmio_space;
|
|
|
|
resource_size_t high_mmio_space;
|
|
|
|
struct resource *mem_config;
|
|
|
|
struct resource *low_mmio_res;
|
|
|
|
struct resource *high_mmio_res;
|
|
|
|
struct completion *survey_event;
|
|
|
|
struct pci_bus *pci_bus;
|
|
|
|
spinlock_t config_lock; /* Avoid two threads writing index page */
|
|
|
|
spinlock_t device_list_lock; /* Protect lists below */
|
|
|
|
void __iomem *cfg_addr;
|
|
|
|
|
|
|
|
struct list_head children;
|
|
|
|
struct list_head dr_list;
|
|
|
|
|
|
|
|
struct msi_domain_info msi_info;
|
|
|
|
struct irq_domain *irq_domain;
|
2017-05-24 20:41:25 +00:00
|
|
|
|
2016-11-08 22:04:38 +00:00
|
|
|
spinlock_t retarget_msi_interrupt_lock;
|
2018-03-15 14:20:53 +00:00
|
|
|
|
|
|
|
struct workqueue_struct *wq;
|
2019-03-01 06:59:02 +00:00
|
|
|
|
2020-05-07 05:02:11 +00:00
|
|
|
/* Highest slot of child device with resources allocated */
|
|
|
|
int wslot_res_allocated;
|
|
|
|
|
2019-03-01 06:59:02 +00:00
|
|
|
/* hypercall arg, must not cross page boundary */
|
2020-02-10 03:39:52 +00:00
|
|
|
struct hv_retarget_device_interrupt retarget_msi_interrupt_params;
|
2019-03-01 06:59:02 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't put anything here: retarget_msi_interrupt_params must be last
|
|
|
|
*/
|
2016-02-16 21:56:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tracks "Device Relations" messages from the host, which must be both
|
|
|
|
* processed in order and deferred so that they don't run in the context
|
|
|
|
* of the incoming packet callback.
|
|
|
|
*/
|
|
|
|
struct hv_dr_work {
|
|
|
|
struct work_struct wrk;
|
|
|
|
struct hv_pcibus_device *bus;
|
|
|
|
};
|
|
|
|
|
2020-02-26 05:06:07 +00:00
|
|
|
struct hv_pcidev_description {
|
|
|
|
u16 v_id; /* vendor ID */
|
|
|
|
u16 d_id; /* device ID */
|
|
|
|
u8 rev;
|
|
|
|
u8 prog_intf;
|
|
|
|
u8 subclass;
|
|
|
|
u8 base_class;
|
|
|
|
u32 subsystem_id;
|
|
|
|
union win_slot_encoding win_slot;
|
|
|
|
u32 ser; /* serial number */
|
|
|
|
u32 flags;
|
|
|
|
u16 virtual_numa_node;
|
|
|
|
};
|
|
|
|
|
2016-02-16 21:56:23 +00:00
|
|
|
struct hv_dr_state {
|
|
|
|
struct list_head list_entry;
|
|
|
|
u32 device_count;
|
2020-02-13 00:50:48 +00:00
|
|
|
struct hv_pcidev_description func[];
|
2016-02-16 21:56:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum hv_pcichild_state {
|
|
|
|
hv_pcichild_init = 0,
|
|
|
|
hv_pcichild_requirements,
|
|
|
|
hv_pcichild_resourced,
|
|
|
|
hv_pcichild_ejecting,
|
|
|
|
hv_pcichild_maximum
|
|
|
|
};
|
|
|
|
|
|
|
|
struct hv_pci_dev {
|
|
|
|
/* List protected by pci_rescan_remove_lock */
|
|
|
|
struct list_head list_entry;
|
2017-04-18 14:02:48 +00:00
|
|
|
refcount_t refs;
|
2016-02-16 21:56:23 +00:00
|
|
|
enum hv_pcichild_state state;
|
2018-09-14 19:54:56 +00:00
|
|
|
struct pci_slot *pci_slot;
|
2020-02-26 05:06:07 +00:00
|
|
|
struct hv_pcidev_description desc;
|
2016-02-16 21:56:23 +00:00
|
|
|
bool reported_missing;
|
|
|
|
struct hv_pcibus_device *hbus;
|
|
|
|
struct work_struct wrk;
|
|
|
|
|
2019-08-22 05:05:37 +00:00
|
|
|
void (*block_invalidate)(void *context, u64 block_mask);
|
|
|
|
void *invalidate_context;
|
|
|
|
|
2016-02-16 21:56:23 +00:00
|
|
|
/*
|
|
|
|
* What would be observed if one wrote 0xFFFFFFFF to a BAR and then
|
|
|
|
* read it back, for each of the BAR offsets within config space.
|
|
|
|
*/
|
2019-09-27 23:43:08 +00:00
|
|
|
u32 probed_bar[PCI_STD_NUM_BARS];
|
2016-02-16 21:56:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct hv_pci_compl {
|
|
|
|
struct completion host_event;
|
|
|
|
s32 completion_status;
|
|
|
|
};
|
|
|
|
|
PCI: hv: Fix 2 hang issues in hv_compose_msi_msg()
1. With the patch "x86/vector/msi: Switch to global reservation mode",
the recent v4.15 and newer kernels always hang for 1-vCPU Hyper-V VM
with SR-IOV. This is because when we reach hv_compose_msi_msg() by
request_irq() -> request_threaded_irq() ->__setup_irq()->irq_startup()
-> __irq_startup() -> irq_domain_activate_irq() -> ... ->
msi_domain_activate() -> ... -> hv_compose_msi_msg(), local irq is
disabled in __setup_irq().
Note: when we reach hv_compose_msi_msg() by another code path:
pci_enable_msix_range() -> ... -> irq_domain_activate_irq() -> ... ->
hv_compose_msi_msg(), local irq is not disabled.
hv_compose_msi_msg() depends on an interrupt from the host.
With interrupts disabled, a UP VM always hangs in the busy loop in
the function, because the interrupt callback hv_pci_onchannelcallback()
can not be called.
We can do nothing but work it around by polling the channel. This
is ugly, but we don't have any other choice.
2. If the host is ejecting the VF device before we reach
hv_compose_msi_msg(), in a UP VM, we can hang in hv_compose_msi_msg()
forever, because at this time the host doesn't respond to the
CREATE_INTERRUPT request. This issue exists the first day the
pci-hyperv driver appears in the kernel.
Luckily, this can also by worked around by polling the channel
for the PCI_EJECT message and hpdev->state, and by checking the
PCI vendor ID.
Note: actually the above 2 issues also happen to a SMP VM, if
"hbus->hdev->channel->target_cpu == smp_processor_id()" is true.
Fixes: 4900be83602b ("x86/vector/msi: Switch to global reservation mode")
Tested-by: Adrian Suhov <v-adsuho@microsoft.com>
Tested-by: Chris Valean <v-chvale@microsoft.com>
Signed-off-by: Dexuan Cui <decui@microsoft.com>
Signed-off-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Reviewed-by: Michael Kelley <mikelley@microsoft.com>
Acked-by: Haiyang Zhang <haiyangz@microsoft.com>
Cc: <stable@vger.kernel.org>
Cc: Stephen Hemminger <sthemmin@microsoft.com>
Cc: K. Y. Srinivasan <kys@microsoft.com>
Cc: Vitaly Kuznetsov <vkuznets@redhat.com>
Cc: Jack Morgenstein <jackm@mellanox.com>
2018-03-15 14:21:08 +00:00
|
|
|
static void hv_pci_onchannelcallback(void *context);
|
|
|
|
|
2022-01-05 19:32:35 +00:00
|
|
|
#ifdef CONFIG_X86
|
|
|
|
#define DELIVERY_MODE APIC_DELIVERY_MODE_FIXED
|
|
|
|
#define FLOW_HANDLER handle_edge_irq
|
|
|
|
#define FLOW_NAME "edge"
|
|
|
|
|
|
|
|
static int hv_pci_irqchip_init(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct irq_domain *hv_pci_get_root_domain(void)
|
|
|
|
{
|
|
|
|
return x86_vector_domain;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int hv_msi_get_int_vector(struct irq_data *data)
|
|
|
|
{
|
|
|
|
struct irq_cfg *cfg = irqd_cfg(data);
|
|
|
|
|
|
|
|
return cfg->vector;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hv_msi_prepare(struct irq_domain *domain, struct device *dev,
|
|
|
|
int nvec, msi_alloc_info_t *info)
|
|
|
|
{
|
2022-04-13 13:36:21 +00:00
|
|
|
int ret = pci_msi_prepare(domain, dev, nvec, info);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* By using the interrupt remapper in the hypervisor IOMMU, contiguous
|
|
|
|
* CPU vectors is not needed for multi-MSI
|
|
|
|
*/
|
|
|
|
if (info->type == X86_IRQ_ALLOC_TYPE_PCI_MSI)
|
|
|
|
info->flags &= ~X86_IRQ_ALLOC_CONTIGUOUS_VECTORS;
|
|
|
|
|
|
|
|
return ret;
|
2022-01-05 19:32:35 +00:00
|
|
|
}
|
2022-02-17 03:45:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* hv_arch_irq_unmask() - "Unmask" the IRQ by setting its current
|
|
|
|
* affinity.
|
|
|
|
* @data: Describes the IRQ
|
|
|
|
*
|
|
|
|
* Build new a destination for the MSI and make a hypercall to
|
|
|
|
* update the Interrupt Redirection Table. "Device Logical ID"
|
|
|
|
* is built out of this PCI bus's instance GUID and the function
|
|
|
|
* number of the device.
|
|
|
|
*/
|
|
|
|
static void hv_arch_irq_unmask(struct irq_data *data)
|
|
|
|
{
|
|
|
|
struct msi_desc *msi_desc = irq_data_get_msi_desc(data);
|
|
|
|
struct hv_retarget_device_interrupt *params;
|
2022-04-27 14:07:33 +00:00
|
|
|
struct tran_int_desc *int_desc;
|
2022-02-17 03:45:19 +00:00
|
|
|
struct hv_pcibus_device *hbus;
|
|
|
|
struct cpumask *dest;
|
|
|
|
cpumask_var_t tmp;
|
|
|
|
struct pci_bus *pbus;
|
|
|
|
struct pci_dev *pdev;
|
|
|
|
unsigned long flags;
|
|
|
|
u32 var_size = 0;
|
|
|
|
int cpu, nr_bank;
|
|
|
|
u64 res;
|
|
|
|
|
|
|
|
dest = irq_data_get_effective_affinity_mask(data);
|
|
|
|
pdev = msi_desc_to_pci_dev(msi_desc);
|
|
|
|
pbus = pdev->bus;
|
|
|
|
hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata);
|
2022-04-27 14:07:33 +00:00
|
|
|
int_desc = data->chip_data;
|
2022-02-17 03:45:19 +00:00
|
|
|
|
|
|
|
spin_lock_irqsave(&hbus->retarget_msi_interrupt_lock, flags);
|
|
|
|
|
|
|
|
params = &hbus->retarget_msi_interrupt_params;
|
|
|
|
memset(params, 0, sizeof(*params));
|
|
|
|
params->partition_id = HV_PARTITION_ID_SELF;
|
|
|
|
params->int_entry.source = HV_INTERRUPT_SOURCE_MSI;
|
2022-04-27 14:07:33 +00:00
|
|
|
params->int_entry.msi_entry.address.as_uint32 = int_desc->address & 0xffffffff;
|
|
|
|
params->int_entry.msi_entry.data.as_uint32 = int_desc->data;
|
2022-02-17 03:45:19 +00:00
|
|
|
params->device_id = (hbus->hdev->dev_instance.b[5] << 24) |
|
|
|
|
(hbus->hdev->dev_instance.b[4] << 16) |
|
|
|
|
(hbus->hdev->dev_instance.b[7] << 8) |
|
|
|
|
(hbus->hdev->dev_instance.b[6] & 0xf8) |
|
|
|
|
PCI_FUNC(pdev->devfn);
|
|
|
|
params->int_target.vector = hv_msi_get_int_vector(data);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Honoring apic->delivery_mode set to APIC_DELIVERY_MODE_FIXED by
|
|
|
|
* setting the HV_DEVICE_INTERRUPT_TARGET_MULTICAST flag results in a
|
|
|
|
* spurious interrupt storm. Not doing so does not seem to have a
|
|
|
|
* negative effect (yet?).
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (hbus->protocol_version >= PCI_PROTOCOL_VERSION_1_2) {
|
|
|
|
/*
|
|
|
|
* PCI_PROTOCOL_VERSION_1_2 supports the VP_SET version of the
|
|
|
|
* HVCALL_RETARGET_INTERRUPT hypercall, which also coincides
|
|
|
|
* with >64 VP support.
|
|
|
|
* ms_hyperv.hints & HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED
|
|
|
|
* is not sufficient for this hypercall.
|
|
|
|
*/
|
|
|
|
params->int_target.flags |=
|
|
|
|
HV_DEVICE_INTERRUPT_TARGET_PROCESSOR_SET;
|
|
|
|
|
|
|
|
if (!alloc_cpumask_var(&tmp, GFP_ATOMIC)) {
|
|
|
|
res = 1;
|
|
|
|
goto exit_unlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
cpumask_and(tmp, dest, cpu_online_mask);
|
|
|
|
nr_bank = cpumask_to_vpset(¶ms->int_target.vp_set, tmp);
|
|
|
|
free_cpumask_var(tmp);
|
|
|
|
|
|
|
|
if (nr_bank <= 0) {
|
|
|
|
res = 1;
|
|
|
|
goto exit_unlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* var-sized hypercall, var-size starts after vp_mask (thus
|
|
|
|
* vp_set.format does not count, but vp_set.valid_bank_mask
|
|
|
|
* does).
|
|
|
|
*/
|
|
|
|
var_size = 1 + nr_bank;
|
|
|
|
} else {
|
|
|
|
for_each_cpu_and(cpu, dest, cpu_online_mask) {
|
|
|
|
params->int_target.vp_mask |=
|
|
|
|
(1ULL << hv_cpu_number_to_vp_number(cpu));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res = hv_do_hypercall(HVCALL_RETARGET_INTERRUPT | (var_size << 17),
|
|
|
|
params, NULL);
|
|
|
|
|
|
|
|
exit_unlock:
|
|
|
|
spin_unlock_irqrestore(&hbus->retarget_msi_interrupt_lock, flags);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* During hibernation, when a CPU is offlined, the kernel tries
|
|
|
|
* to move the interrupt to the remaining CPUs that haven't
|
|
|
|
* been offlined yet. In this case, the below hv_do_hypercall()
|
|
|
|
* always fails since the vmbus channel has been closed:
|
|
|
|
* refer to cpu_disable_common() -> fixup_irqs() ->
|
|
|
|
* irq_migrate_all_off_this_cpu() -> migrate_one_irq().
|
|
|
|
*
|
|
|
|
* Suppress the error message for hibernation because the failure
|
|
|
|
* during hibernation does not matter (at this time all the devices
|
|
|
|
* have been frozen). Note: the correct affinity info is still updated
|
|
|
|
* into the irqdata data structure in migrate_one_irq() ->
|
|
|
|
* irq_do_set_affinity() -> hv_set_affinity(), so later when the VM
|
|
|
|
* resumes, hv_pci_restore_msi_state() is able to correctly restore
|
|
|
|
* the interrupt with the correct affinity.
|
|
|
|
*/
|
|
|
|
if (!hv_result_success(res) && hbus->state != hv_pcibus_removing)
|
|
|
|
dev_err(&hbus->hdev->device,
|
|
|
|
"%s() failed: %#llx", __func__, res);
|
|
|
|
}
|
2022-01-05 19:32:36 +00:00
|
|
|
#elif defined(CONFIG_ARM64)
|
|
|
|
/*
|
|
|
|
* SPI vectors to use for vPCI; arch SPIs range is [32, 1019], but leaving a bit
|
|
|
|
* of room at the start to allow for SPIs to be specified through ACPI and
|
|
|
|
* starting with a power of two to satisfy power of 2 multi-MSI requirement.
|
|
|
|
*/
|
|
|
|
#define HV_PCI_MSI_SPI_START 64
|
|
|
|
#define HV_PCI_MSI_SPI_NR (1020 - HV_PCI_MSI_SPI_START)
|
|
|
|
#define DELIVERY_MODE 0
|
|
|
|
#define FLOW_HANDLER NULL
|
|
|
|
#define FLOW_NAME NULL
|
|
|
|
#define hv_msi_prepare NULL
|
|
|
|
|
|
|
|
struct hv_pci_chip_data {
|
|
|
|
DECLARE_BITMAP(spi_map, HV_PCI_MSI_SPI_NR);
|
|
|
|
struct mutex map_lock;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Hyper-V vPCI MSI GIC IRQ domain */
|
|
|
|
static struct irq_domain *hv_msi_gic_irq_domain;
|
|
|
|
|
|
|
|
/* Hyper-V PCI MSI IRQ chip */
|
|
|
|
static struct irq_chip hv_arm64_msi_irq_chip = {
|
|
|
|
.name = "MSI",
|
|
|
|
.irq_set_affinity = irq_chip_set_affinity_parent,
|
|
|
|
.irq_eoi = irq_chip_eoi_parent,
|
|
|
|
.irq_mask = irq_chip_mask_parent,
|
|
|
|
.irq_unmask = irq_chip_unmask_parent
|
|
|
|
};
|
|
|
|
|
|
|
|
static unsigned int hv_msi_get_int_vector(struct irq_data *irqd)
|
|
|
|
{
|
|
|
|
return irqd->parent_data->hwirq;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @nr_bm_irqs: Indicates the number of IRQs that were allocated from
|
|
|
|
* the bitmap.
|
|
|
|
* @nr_dom_irqs: Indicates the number of IRQs that were allocated from
|
|
|
|
* the parent domain.
|
|
|
|
*/
|
|
|
|
static void hv_pci_vec_irq_free(struct irq_domain *domain,
|
|
|
|
unsigned int virq,
|
|
|
|
unsigned int nr_bm_irqs,
|
|
|
|
unsigned int nr_dom_irqs)
|
|
|
|
{
|
|
|
|
struct hv_pci_chip_data *chip_data = domain->host_data;
|
|
|
|
struct irq_data *d = irq_domain_get_irq_data(domain, virq);
|
|
|
|
int first = d->hwirq - HV_PCI_MSI_SPI_START;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
mutex_lock(&chip_data->map_lock);
|
|
|
|
bitmap_release_region(chip_data->spi_map,
|
|
|
|
first,
|
|
|
|
get_count_order(nr_bm_irqs));
|
|
|
|
mutex_unlock(&chip_data->map_lock);
|
|
|
|
for (i = 0; i < nr_dom_irqs; i++) {
|
|
|
|
if (i)
|
|
|
|
d = irq_domain_get_irq_data(domain, virq + i);
|
|
|
|
irq_domain_reset_irq_data(d);
|
|
|
|
}
|
|
|
|
|
|
|
|
irq_domain_free_irqs_parent(domain, virq, nr_dom_irqs);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void hv_pci_vec_irq_domain_free(struct irq_domain *domain,
|
|
|
|
unsigned int virq,
|
|
|
|
unsigned int nr_irqs)
|
|
|
|
{
|
|
|
|
hv_pci_vec_irq_free(domain, virq, nr_irqs, nr_irqs);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hv_pci_vec_alloc_device_irq(struct irq_domain *domain,
|
|
|
|
unsigned int nr_irqs,
|
|
|
|
irq_hw_number_t *hwirq)
|
|
|
|
{
|
|
|
|
struct hv_pci_chip_data *chip_data = domain->host_data;
|
|
|
|
int index;
|
|
|
|
|
|
|
|
/* Find and allocate region from the SPI bitmap */
|
|
|
|
mutex_lock(&chip_data->map_lock);
|
|
|
|
index = bitmap_find_free_region(chip_data->spi_map,
|
|
|
|
HV_PCI_MSI_SPI_NR,
|
|
|
|
get_count_order(nr_irqs));
|
|
|
|
mutex_unlock(&chip_data->map_lock);
|
|
|
|
if (index < 0)
|
|
|
|
return -ENOSPC;
|
|
|
|
|
|
|
|
*hwirq = index + HV_PCI_MSI_SPI_START;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hv_pci_vec_irq_gic_domain_alloc(struct irq_domain *domain,
|
|
|
|
unsigned int virq,
|
|
|
|
irq_hw_number_t hwirq)
|
|
|
|
{
|
|
|
|
struct irq_fwspec fwspec;
|
|
|
|
struct irq_data *d;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
fwspec.fwnode = domain->parent->fwnode;
|
|
|
|
fwspec.param_count = 2;
|
|
|
|
fwspec.param[0] = hwirq;
|
|
|
|
fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
|
|
|
|
|
|
|
|
ret = irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Since the interrupt specifier is not coming from ACPI or DT, the
|
|
|
|
* trigger type will need to be set explicitly. Otherwise, it will be
|
|
|
|
* set to whatever is in the GIC configuration.
|
|
|
|
*/
|
|
|
|
d = irq_domain_get_irq_data(domain->parent, virq);
|
|
|
|
|
|
|
|
return d->chip->irq_set_type(d, IRQ_TYPE_EDGE_RISING);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hv_pci_vec_irq_domain_alloc(struct irq_domain *domain,
|
|
|
|
unsigned int virq, unsigned int nr_irqs,
|
|
|
|
void *args)
|
|
|
|
{
|
|
|
|
irq_hw_number_t hwirq;
|
|
|
|
unsigned int i;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = hv_pci_vec_alloc_device_irq(domain, nr_irqs, &hwirq);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
for (i = 0; i < nr_irqs; i++) {
|
|
|
|
ret = hv_pci_vec_irq_gic_domain_alloc(domain, virq + i,
|
|
|
|
hwirq + i);
|
|
|
|
if (ret) {
|
|
|
|
hv_pci_vec_irq_free(domain, virq, nr_irqs, i);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
irq_domain_set_hwirq_and_chip(domain, virq + i,
|
|
|
|
hwirq + i,
|
|
|
|
&hv_arm64_msi_irq_chip,
|
|
|
|
domain->host_data);
|
|
|
|
pr_debug("pID:%d vID:%u\n", (int)(hwirq + i), virq + i);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Pick the first cpu as the irq affinity that can be temporarily used for
|
|
|
|
* composing MSI from the hypervisor. GIC will eventually set the right
|
|
|
|
* affinity for the irq and the 'unmask' will retarget the interrupt to that
|
|
|
|
* cpu.
|
|
|
|
*/
|
|
|
|
static int hv_pci_vec_irq_domain_activate(struct irq_domain *domain,
|
|
|
|
struct irq_data *irqd, bool reserve)
|
|
|
|
{
|
|
|
|
int cpu = cpumask_first(cpu_present_mask);
|
|
|
|
|
|
|
|
irq_data_update_effective_affinity(irqd, cpumask_of(cpu));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct irq_domain_ops hv_pci_domain_ops = {
|
|
|
|
.alloc = hv_pci_vec_irq_domain_alloc,
|
|
|
|
.free = hv_pci_vec_irq_domain_free,
|
|
|
|
.activate = hv_pci_vec_irq_domain_activate,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int hv_pci_irqchip_init(void)
|
|
|
|
{
|
|
|
|
static struct hv_pci_chip_data *chip_data;
|
|
|
|
struct fwnode_handle *fn = NULL;
|
|
|
|
int ret = -ENOMEM;
|
|
|
|
|
|
|
|
chip_data = kzalloc(sizeof(*chip_data), GFP_KERNEL);
|
|
|
|
if (!chip_data)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
mutex_init(&chip_data->map_lock);
|
|
|
|
fn = irq_domain_alloc_named_fwnode("hv_vpci_arm64");
|
|
|
|
if (!fn)
|
|
|
|
goto free_chip;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* IRQ domain once enabled, should not be removed since there is no
|
|
|
|
* way to ensure that all the corresponding devices are also gone and
|
|
|
|
* no interrupts will be generated.
|
|
|
|
*/
|
|
|
|
hv_msi_gic_irq_domain = acpi_irq_create_hierarchy(0, HV_PCI_MSI_SPI_NR,
|
|
|
|
fn, &hv_pci_domain_ops,
|
|
|
|
chip_data);
|
|
|
|
|
|
|
|
if (!hv_msi_gic_irq_domain) {
|
|
|
|
pr_err("Failed to create Hyper-V arm64 vPCI MSI IRQ domain\n");
|
|
|
|
goto free_chip;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
free_chip:
|
|
|
|
kfree(chip_data);
|
|
|
|
if (fn)
|
|
|
|
irq_domain_free_fwnode(fn);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct irq_domain *hv_pci_get_root_domain(void)
|
|
|
|
{
|
|
|
|
return hv_msi_gic_irq_domain;
|
|
|
|
}
|
2022-02-17 03:45:19 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* SPIs are used for interrupts of PCI devices and SPIs is managed via GICD
|
|
|
|
* registers which Hyper-V already supports, so no hypercall needed.
|
|
|
|
*/
|
|
|
|
static void hv_arch_irq_unmask(struct irq_data *data) { }
|
2022-01-05 19:32:36 +00:00
|
|
|
#endif /* CONFIG_ARM64 */
|
2022-01-05 19:32:35 +00:00
|
|
|
|
2016-02-16 21:56:23 +00:00
|
|
|
/**
|
|
|
|
* hv_pci_generic_compl() - Invoked for a completion packet
|
|
|
|
* @context: Set up by the sender of the packet.
|
|
|
|
* @resp: The response packet
|
|
|
|
* @resp_packet_size: Size in bytes of the packet
|
|
|
|
*
|
|
|
|
* This function is used to trigger an event and report status
|
|
|
|
* for any message for which the completion packet contains a
|
|
|
|
* status and nothing else.
|
|
|
|
*/
|
2016-08-23 04:49:22 +00:00
|
|
|
static void hv_pci_generic_compl(void *context, struct pci_response *resp,
|
|
|
|
int resp_packet_size)
|
2016-02-16 21:56:23 +00:00
|
|
|
{
|
|
|
|
struct hv_pci_compl *comp_pkt = context;
|
|
|
|
|
2022-05-11 22:32:06 +00:00
|
|
|
comp_pkt->completion_status = resp->status;
|
2016-02-16 21:56:23 +00:00
|
|
|
complete(&comp_pkt->host_event);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct hv_pci_dev *get_pcichild_wslot(struct hv_pcibus_device *hbus,
|
|
|
|
u32 wslot);
|
2018-05-23 17:11:12 +00:00
|
|
|
|
|
|
|
static void get_pcichild(struct hv_pci_dev *hpdev)
|
|
|
|
{
|
|
|
|
refcount_inc(&hpdev->refs);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void put_pcichild(struct hv_pci_dev *hpdev)
|
|
|
|
{
|
|
|
|
if (refcount_dec_and_test(&hpdev->refs))
|
|
|
|
kfree(hpdev);
|
|
|
|
}
|
2016-02-16 21:56:23 +00:00
|
|
|
|
2018-05-23 21:12:01 +00:00
|
|
|
/*
|
|
|
|
* There is no good way to get notified from vmbus_onoffer_rescind(),
|
|
|
|
* so let's use polling here, since this is not a hot path.
|
|
|
|
*/
|
|
|
|
static int wait_for_response(struct hv_device *hdev,
|
|
|
|
struct completion *comp)
|
|
|
|
{
|
|
|
|
while (true) {
|
|
|
|
if (hdev->channel->rescind) {
|
|
|
|
dev_warn_once(&hdev->device, "The device is gone.\n");
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wait_for_completion_timeout(comp, HZ / 10))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-16 21:56:23 +00:00
|
|
|
/**
|
|
|
|
* devfn_to_wslot() - Convert from Linux PCI slot to Windows
|
|
|
|
* @devfn: The Linux representation of PCI slot
|
|
|
|
*
|
|
|
|
* Windows uses a slightly different representation of PCI slot.
|
|
|
|
*
|
|
|
|
* Return: The Windows representation
|
|
|
|
*/
|
|
|
|
static u32 devfn_to_wslot(int devfn)
|
|
|
|
{
|
|
|
|
union win_slot_encoding wslot;
|
|
|
|
|
|
|
|
wslot.slot = 0;
|
2017-02-10 21:18:46 +00:00
|
|
|
wslot.bits.dev = PCI_SLOT(devfn);
|
|
|
|
wslot.bits.func = PCI_FUNC(devfn);
|
2016-02-16 21:56:23 +00:00
|
|
|
|
|
|
|
return wslot.slot;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* wslot_to_devfn() - Convert from Windows PCI slot to Linux
|
|
|
|
* @wslot: The Windows representation of PCI slot
|
|
|
|
*
|
|
|
|
* Windows uses a slightly different representation of PCI slot.
|
|
|
|
*
|
|
|
|
* Return: The Linux representation
|
|
|
|
*/
|
|
|
|
static int wslot_to_devfn(u32 wslot)
|
|
|
|
{
|
|
|
|
union win_slot_encoding slot_no;
|
|
|
|
|
|
|
|
slot_no.slot = wslot;
|
2017-02-10 21:18:46 +00:00
|
|
|
return PCI_DEVFN(slot_no.bits.dev, slot_no.bits.func);
|
2016-02-16 21:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* PCI Configuration Space for these root PCI buses is implemented as a pair
|
|
|
|
* of pages in memory-mapped I/O space. Writing to the first page chooses
|
|
|
|
* the PCI function being written or read. Once the first page has been
|
|
|
|
* written to, the following page maps in the entire configuration space of
|
|
|
|
* the function.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* _hv_pcifront_read_config() - Internal PCI config read
|
|
|
|
* @hpdev: The PCI driver's representation of the device
|
|
|
|
* @where: Offset within config space
|
|
|
|
* @size: Size of the transfer
|
|
|
|
* @val: Pointer to the buffer receiving the data
|
|
|
|
*/
|
|
|
|
static void _hv_pcifront_read_config(struct hv_pci_dev *hpdev, int where,
|
|
|
|
int size, u32 *val)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
void __iomem *addr = hpdev->hbus->cfg_addr + CFG_PAGE_OFFSET + where;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the attempt is to read the IDs or the ROM BAR, simulate that.
|
|
|
|
*/
|
|
|
|
if (where + size <= PCI_COMMAND) {
|
|
|
|
memcpy(val, ((u8 *)&hpdev->desc.v_id) + where, size);
|
|
|
|
} else if (where >= PCI_CLASS_REVISION && where + size <=
|
|
|
|
PCI_CACHE_LINE_SIZE) {
|
|
|
|
memcpy(val, ((u8 *)&hpdev->desc.rev) + where -
|
|
|
|
PCI_CLASS_REVISION, size);
|
|
|
|
} else if (where >= PCI_SUBSYSTEM_VENDOR_ID && where + size <=
|
|
|
|
PCI_ROM_ADDRESS) {
|
|
|
|
memcpy(val, (u8 *)&hpdev->desc.subsystem_id + where -
|
|
|
|
PCI_SUBSYSTEM_VENDOR_ID, size);
|
|
|
|
} else if (where >= PCI_ROM_ADDRESS && where + size <=
|
|
|
|
PCI_CAPABILITY_LIST) {
|
|
|
|
/* ROM BARs are unimplemented */
|
|
|
|
*val = 0;
|
|
|
|
} else if (where >= PCI_INTERRUPT_LINE && where + size <=
|
|
|
|
PCI_INTERRUPT_PIN) {
|
|
|
|
/*
|
|
|
|
* Interrupt Line and Interrupt PIN are hard-wired to zero
|
|
|
|
* because this front-end only supports message-signaled
|
|
|
|
* interrupts.
|
|
|
|
*/
|
|
|
|
*val = 0;
|
|
|
|
} else if (where + size <= CFG_PAGE_SIZE) {
|
|
|
|
spin_lock_irqsave(&hpdev->hbus->config_lock, flags);
|
|
|
|
/* Choose the function to be read. (See comment above) */
|
|
|
|
writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr);
|
2016-05-03 12:22:00 +00:00
|
|
|
/* Make sure the function was chosen before we start reading. */
|
|
|
|
mb();
|
2016-02-16 21:56:23 +00:00
|
|
|
/* Read from that function's config space. */
|
|
|
|
switch (size) {
|
|
|
|
case 1:
|
|
|
|
*val = readb(addr);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
*val = readw(addr);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*val = readl(addr);
|
|
|
|
break;
|
|
|
|
}
|
2016-05-03 12:22:00 +00:00
|
|
|
/*
|
2018-03-15 14:21:35 +00:00
|
|
|
* Make sure the read was done before we release the spinlock
|
2016-05-03 12:22:00 +00:00
|
|
|
* allowing consecutive reads/writes.
|
|
|
|
*/
|
|
|
|
mb();
|
2016-02-16 21:56:23 +00:00
|
|
|
spin_unlock_irqrestore(&hpdev->hbus->config_lock, flags);
|
|
|
|
} else {
|
|
|
|
dev_err(&hpdev->hbus->hdev->device,
|
|
|
|
"Attempt to read beyond a function's config space.\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
PCI: hv: Fix 2 hang issues in hv_compose_msi_msg()
1. With the patch "x86/vector/msi: Switch to global reservation mode",
the recent v4.15 and newer kernels always hang for 1-vCPU Hyper-V VM
with SR-IOV. This is because when we reach hv_compose_msi_msg() by
request_irq() -> request_threaded_irq() ->__setup_irq()->irq_startup()
-> __irq_startup() -> irq_domain_activate_irq() -> ... ->
msi_domain_activate() -> ... -> hv_compose_msi_msg(), local irq is
disabled in __setup_irq().
Note: when we reach hv_compose_msi_msg() by another code path:
pci_enable_msix_range() -> ... -> irq_domain_activate_irq() -> ... ->
hv_compose_msi_msg(), local irq is not disabled.
hv_compose_msi_msg() depends on an interrupt from the host.
With interrupts disabled, a UP VM always hangs in the busy loop in
the function, because the interrupt callback hv_pci_onchannelcallback()
can not be called.
We can do nothing but work it around by polling the channel. This
is ugly, but we don't have any other choice.
2. If the host is ejecting the VF device before we reach
hv_compose_msi_msg(), in a UP VM, we can hang in hv_compose_msi_msg()
forever, because at this time the host doesn't respond to the
CREATE_INTERRUPT request. This issue exists the first day the
pci-hyperv driver appears in the kernel.
Luckily, this can also by worked around by polling the channel
for the PCI_EJECT message and hpdev->state, and by checking the
PCI vendor ID.
Note: actually the above 2 issues also happen to a SMP VM, if
"hbus->hdev->channel->target_cpu == smp_processor_id()" is true.
Fixes: 4900be83602b ("x86/vector/msi: Switch to global reservation mode")
Tested-by: Adrian Suhov <v-adsuho@microsoft.com>
Tested-by: Chris Valean <v-chvale@microsoft.com>
Signed-off-by: Dexuan Cui <decui@microsoft.com>
Signed-off-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Reviewed-by: Michael Kelley <mikelley@microsoft.com>
Acked-by: Haiyang Zhang <haiyangz@microsoft.com>
Cc: <stable@vger.kernel.org>
Cc: Stephen Hemminger <sthemmin@microsoft.com>
Cc: K. Y. Srinivasan <kys@microsoft.com>
Cc: Vitaly Kuznetsov <vkuznets@redhat.com>
Cc: Jack Morgenstein <jackm@mellanox.com>
2018-03-15 14:21:08 +00:00
|
|
|
static u16 hv_pcifront_get_vendor_id(struct hv_pci_dev *hpdev)
|
|
|
|
{
|
|
|
|
u16 ret;
|
|
|
|
unsigned long flags;
|
|
|
|
void __iomem *addr = hpdev->hbus->cfg_addr + CFG_PAGE_OFFSET +
|
|
|
|
PCI_VENDOR_ID;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&hpdev->hbus->config_lock, flags);
|
|
|
|
|
|
|
|
/* Choose the function to be read. (See comment above) */
|
|
|
|
writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr);
|
|
|
|
/* Make sure the function was chosen before we start reading. */
|
|
|
|
mb();
|
|
|
|
/* Read from that function's config space. */
|
|
|
|
ret = readw(addr);
|
|
|
|
/*
|
|
|
|
* mb() is not required here, because the spin_unlock_irqrestore()
|
|
|
|
* is a barrier.
|
|
|
|
*/
|
|
|
|
|
|
|
|
spin_unlock_irqrestore(&hpdev->hbus->config_lock, flags);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-02-16 21:56:23 +00:00
|
|
|
/**
|
|
|
|
* _hv_pcifront_write_config() - Internal PCI config write
|
|
|
|
* @hpdev: The PCI driver's representation of the device
|
|
|
|
* @where: Offset within config space
|
|
|
|
* @size: Size of the transfer
|
|
|
|
* @val: The data being transferred
|
|
|
|
*/
|
|
|
|
static void _hv_pcifront_write_config(struct hv_pci_dev *hpdev, int where,
|
|
|
|
int size, u32 val)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
void __iomem *addr = hpdev->hbus->cfg_addr + CFG_PAGE_OFFSET + where;
|
|
|
|
|
|
|
|
if (where >= PCI_SUBSYSTEM_VENDOR_ID &&
|
|
|
|
where + size <= PCI_CAPABILITY_LIST) {
|
|
|
|
/* SSIDs and ROM BARs are read-only */
|
|
|
|
} else if (where >= PCI_COMMAND && where + size <= CFG_PAGE_SIZE) {
|
|
|
|
spin_lock_irqsave(&hpdev->hbus->config_lock, flags);
|
|
|
|
/* Choose the function to be written. (See comment above) */
|
|
|
|
writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr);
|
2016-05-03 12:22:00 +00:00
|
|
|
/* Make sure the function was chosen before we start writing. */
|
|
|
|
wmb();
|
2016-02-16 21:56:23 +00:00
|
|
|
/* Write to that function's config space. */
|
|
|
|
switch (size) {
|
|
|
|
case 1:
|
|
|
|
writeb(val, addr);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
writew(val, addr);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
writel(val, addr);
|
|
|
|
break;
|
|
|
|
}
|
2016-05-03 12:22:00 +00:00
|
|
|
/*
|
|
|
|
* Make sure the write was done before we release the spinlock
|
|
|
|
* allowing consecutive reads/writes.
|
|
|
|
*/
|
|
|
|
mb();
|
2016-02-16 21:56:23 +00:00
|
|
|
spin_unlock_irqrestore(&hpdev->hbus->config_lock, flags);
|
|
|
|
} else {
|
|
|
|
dev_err(&hpdev->hbus->hdev->device,
|
|
|
|
"Attempt to write beyond a function's config space.\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* hv_pcifront_read_config() - Read configuration space
|
|
|
|
* @bus: PCI Bus structure
|
|
|
|
* @devfn: Device/function
|
|
|
|
* @where: Offset from base
|
|
|
|
* @size: Byte/word/dword
|
|
|
|
* @val: Value to be read
|
|
|
|
*
|
|
|
|
* Return: PCIBIOS_SUCCESSFUL on success
|
|
|
|
* PCIBIOS_DEVICE_NOT_FOUND on failure
|
|
|
|
*/
|
|
|
|
static int hv_pcifront_read_config(struct pci_bus *bus, unsigned int devfn,
|
|
|
|
int where, int size, u32 *val)
|
|
|
|
{
|
|
|
|
struct hv_pcibus_device *hbus =
|
|
|
|
container_of(bus->sysdata, struct hv_pcibus_device, sysdata);
|
|
|
|
struct hv_pci_dev *hpdev;
|
|
|
|
|
|
|
|
hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(devfn));
|
|
|
|
if (!hpdev)
|
|
|
|
return PCIBIOS_DEVICE_NOT_FOUND;
|
|
|
|
|
|
|
|
_hv_pcifront_read_config(hpdev, where, size, val);
|
|
|
|
|
2018-05-23 17:11:12 +00:00
|
|
|
put_pcichild(hpdev);
|
2016-02-16 21:56:23 +00:00
|
|
|
return PCIBIOS_SUCCESSFUL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* hv_pcifront_write_config() - Write configuration space
|
|
|
|
* @bus: PCI Bus structure
|
|
|
|
* @devfn: Device/function
|
|
|
|
* @where: Offset from base
|
|
|
|
* @size: Byte/word/dword
|
|
|
|
* @val: Value to be written to device
|
|
|
|
*
|
|
|
|
* Return: PCIBIOS_SUCCESSFUL on success
|
|
|
|
* PCIBIOS_DEVICE_NOT_FOUND on failure
|
|
|
|
*/
|
|
|
|
static int hv_pcifront_write_config(struct pci_bus *bus, unsigned int devfn,
|
|
|
|
int where, int size, u32 val)
|
|
|
|
{
|
|
|
|
struct hv_pcibus_device *hbus =
|
|
|
|
container_of(bus->sysdata, struct hv_pcibus_device, sysdata);
|
|
|
|
struct hv_pci_dev *hpdev;
|
|
|
|
|
|
|
|
hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(devfn));
|
|
|
|
if (!hpdev)
|
|
|
|
return PCIBIOS_DEVICE_NOT_FOUND;
|
|
|
|
|
|
|
|
_hv_pcifront_write_config(hpdev, where, size, val);
|
|
|
|
|
2018-05-23 17:11:12 +00:00
|
|
|
put_pcichild(hpdev);
|
2016-02-16 21:56:23 +00:00
|
|
|
return PCIBIOS_SUCCESSFUL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* PCIe operations */
|
|
|
|
static struct pci_ops hv_pcifront_ops = {
|
|
|
|
.read = hv_pcifront_read_config,
|
|
|
|
.write = hv_pcifront_write_config,
|
|
|
|
};
|
|
|
|
|
2019-08-22 05:05:37 +00:00
|
|
|
/*
|
|
|
|
* Paravirtual backchannel
|
|
|
|
*
|
|
|
|
* Hyper-V SR-IOV provides a backchannel mechanism in software for
|
|
|
|
* communication between a VF driver and a PF driver. These
|
|
|
|
* "configuration blocks" are similar in concept to PCI configuration space,
|
|
|
|
* but instead of doing reads and writes in 32-bit chunks through a very slow
|
|
|
|
* path, packets of up to 128 bytes can be sent or received asynchronously.
|
|
|
|
*
|
|
|
|
* Nearly every SR-IOV device contains just such a communications channel in
|
|
|
|
* hardware, so using this one in software is usually optional. Using the
|
|
|
|
* software channel, however, allows driver implementers to leverage software
|
|
|
|
* tools that fuzz the communications channel looking for vulnerabilities.
|
|
|
|
*
|
|
|
|
* The usage model for these packets puts the responsibility for reading or
|
|
|
|
* writing on the VF driver. The VF driver sends a read or a write packet,
|
|
|
|
* indicating which "block" is being referred to by number.
|
|
|
|
*
|
|
|
|
* If the PF driver wishes to initiate communication, it can "invalidate" one or
|
|
|
|
* more of the first 64 blocks. This invalidation is delivered via a callback
|
|
|
|
* supplied by the VF driver by this driver.
|
|
|
|
*
|
|
|
|
* No protocol is implied, except that supplied by the PF and VF drivers.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct hv_read_config_compl {
|
|
|
|
struct hv_pci_compl comp_pkt;
|
|
|
|
void *buf;
|
|
|
|
unsigned int len;
|
|
|
|
unsigned int bytes_returned;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* hv_pci_read_config_compl() - Invoked when a response packet
|
|
|
|
* for a read config block operation arrives.
|
|
|
|
* @context: Identifies the read config operation
|
|
|
|
* @resp: The response packet itself
|
|
|
|
* @resp_packet_size: Size in bytes of the response packet
|
|
|
|
*/
|
|
|
|
static void hv_pci_read_config_compl(void *context, struct pci_response *resp,
|
|
|
|
int resp_packet_size)
|
|
|
|
{
|
|
|
|
struct hv_read_config_compl *comp = context;
|
|
|
|
struct pci_read_block_response *read_resp =
|
|
|
|
(struct pci_read_block_response *)resp;
|
|
|
|
unsigned int data_len, hdr_len;
|
|
|
|
|
|
|
|
hdr_len = offsetof(struct pci_read_block_response, bytes);
|
|
|
|
if (resp_packet_size < hdr_len) {
|
|
|
|
comp->comp_pkt.completion_status = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
data_len = resp_packet_size - hdr_len;
|
|
|
|
if (data_len > 0 && read_resp->status == 0) {
|
|
|
|
comp->bytes_returned = min(comp->len, data_len);
|
|
|
|
memcpy(comp->buf, read_resp->bytes, comp->bytes_returned);
|
|
|
|
} else {
|
|
|
|
comp->bytes_returned = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
comp->comp_pkt.completion_status = read_resp->status;
|
|
|
|
out:
|
|
|
|
complete(&comp->comp_pkt.host_event);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* hv_read_config_block() - Sends a read config block request to
|
|
|
|
* the back-end driver running in the Hyper-V parent partition.
|
|
|
|
* @pdev: The PCI driver's representation for this device.
|
|
|
|
* @buf: Buffer into which the config block will be copied.
|
|
|
|
* @len: Size in bytes of buf.
|
|
|
|
* @block_id: Identifies the config block which has been requested.
|
|
|
|
* @bytes_returned: Size which came back from the back-end driver.
|
|
|
|
*
|
|
|
|
* Return: 0 on success, -errno on failure
|
|
|
|
*/
|
2020-07-06 13:52:34 +00:00
|
|
|
static int hv_read_config_block(struct pci_dev *pdev, void *buf,
|
|
|
|
unsigned int len, unsigned int block_id,
|
|
|
|
unsigned int *bytes_returned)
|
2019-08-22 05:05:37 +00:00
|
|
|
{
|
|
|
|
struct hv_pcibus_device *hbus =
|
|
|
|
container_of(pdev->bus->sysdata, struct hv_pcibus_device,
|
|
|
|
sysdata);
|
|
|
|
struct {
|
|
|
|
struct pci_packet pkt;
|
|
|
|
char buf[sizeof(struct pci_read_block)];
|
|
|
|
} pkt;
|
|
|
|
struct hv_read_config_compl comp_pkt;
|
|
|
|
struct pci_read_block *read_blk;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (len == 0 || len > HV_CONFIG_BLOCK_SIZE_MAX)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
init_completion(&comp_pkt.comp_pkt.host_event);
|
|
|
|
comp_pkt.buf = buf;
|
|
|
|
comp_pkt.len = len;
|
|
|
|
|
|
|
|
memset(&pkt, 0, sizeof(pkt));
|
|
|
|
pkt.pkt.completion_func = hv_pci_read_config_compl;
|
|
|
|
pkt.pkt.compl_ctxt = &comp_pkt;
|
|
|
|
read_blk = (struct pci_read_block *)&pkt.pkt.message;
|
|
|
|
read_blk->message_type.type = PCI_READ_BLOCK;
|
|
|
|
read_blk->wslot.slot = devfn_to_wslot(pdev->devfn);
|
|
|
|
read_blk->block_id = block_id;
|
|
|
|
read_blk->bytes_requested = len;
|
|
|
|
|
|
|
|
ret = vmbus_sendpacket(hbus->hdev->channel, read_blk,
|
|
|
|
sizeof(*read_blk), (unsigned long)&pkt.pkt,
|
|
|
|
VM_PKT_DATA_INBAND,
|
|
|
|
VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = wait_for_response(hbus->hdev, &comp_pkt.comp_pkt.host_event);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (comp_pkt.comp_pkt.completion_status != 0 ||
|
|
|
|
comp_pkt.bytes_returned == 0) {
|
|
|
|
dev_err(&hbus->hdev->device,
|
|
|
|
"Read Config Block failed: 0x%x, bytes_returned=%d\n",
|
|
|
|
comp_pkt.comp_pkt.completion_status,
|
|
|
|
comp_pkt.bytes_returned);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
*bytes_returned = comp_pkt.bytes_returned;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* hv_pci_write_config_compl() - Invoked when a response packet for a write
|
|
|
|
* config block operation arrives.
|
|
|
|
* @context: Identifies the write config operation
|
|
|
|
* @resp: The response packet itself
|
|
|
|
* @resp_packet_size: Size in bytes of the response packet
|
|
|
|
*/
|
|
|
|
static void hv_pci_write_config_compl(void *context, struct pci_response *resp,
|
|
|
|
int resp_packet_size)
|
|
|
|
{
|
|
|
|
struct hv_pci_compl *comp_pkt = context;
|
|
|
|
|
|
|
|
comp_pkt->completion_status = resp->status;
|
|
|
|
complete(&comp_pkt->host_event);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* hv_write_config_block() - Sends a write config block request to the
|
|
|
|
* back-end driver running in the Hyper-V parent partition.
|
|
|
|
* @pdev: The PCI driver's representation for this device.
|
|
|
|
* @buf: Buffer from which the config block will be copied.
|
|
|
|
* @len: Size in bytes of buf.
|
|
|
|
* @block_id: Identifies the config block which is being written.
|
|
|
|
*
|
|
|
|
* Return: 0 on success, -errno on failure
|
|
|
|
*/
|
2020-07-06 13:52:34 +00:00
|
|
|
static int hv_write_config_block(struct pci_dev *pdev, void *buf,
|
|
|
|
unsigned int len, unsigned int block_id)
|
2019-08-22 05:05:37 +00:00
|
|
|
{
|
|
|
|
struct hv_pcibus_device *hbus =
|
|
|
|
container_of(pdev->bus->sysdata, struct hv_pcibus_device,
|
|
|
|
sysdata);
|
|
|
|
struct {
|
|
|
|
struct pci_packet pkt;
|
|
|
|
char buf[sizeof(struct pci_write_block)];
|
|
|
|
u32 reserved;
|
|
|
|
} pkt;
|
|
|
|
struct hv_pci_compl comp_pkt;
|
|
|
|
struct pci_write_block *write_blk;
|
|
|
|
u32 pkt_size;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (len == 0 || len > HV_CONFIG_BLOCK_SIZE_MAX)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
init_completion(&comp_pkt.host_event);
|
|
|
|
|
|
|
|
memset(&pkt, 0, sizeof(pkt));
|
|
|
|
pkt.pkt.completion_func = hv_pci_write_config_compl;
|
|
|
|
pkt.pkt.compl_ctxt = &comp_pkt;
|
|
|
|
write_blk = (struct pci_write_block *)&pkt.pkt.message;
|
|
|
|
write_blk->message_type.type = PCI_WRITE_BLOCK;
|
|
|
|
write_blk->wslot.slot = devfn_to_wslot(pdev->devfn);
|
|
|
|
write_blk->block_id = block_id;
|
|
|
|
write_blk->byte_count = len;
|
|
|
|
memcpy(write_blk->bytes, buf, len);
|
|
|
|
pkt_size = offsetof(struct pci_write_block, bytes) + len;
|
|
|
|
/*
|
|
|
|
* This quirk is required on some hosts shipped around 2018, because
|
|
|
|
* these hosts don't check the pkt_size correctly (new hosts have been
|
|
|
|
* fixed since early 2019). The quirk is also safe on very old hosts
|
|
|
|
* and new hosts, because, on them, what really matters is the length
|
|
|
|
* specified in write_blk->byte_count.
|
|
|
|
*/
|
|
|
|
pkt_size += sizeof(pkt.reserved);
|
|
|
|
|
|
|
|
ret = vmbus_sendpacket(hbus->hdev->channel, write_blk, pkt_size,
|
|
|
|
(unsigned long)&pkt.pkt, VM_PKT_DATA_INBAND,
|
|
|
|
VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = wait_for_response(hbus->hdev, &comp_pkt.host_event);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (comp_pkt.completion_status != 0) {
|
|
|
|
dev_err(&hbus->hdev->device,
|
|
|
|
"Write Config Block failed: 0x%x\n",
|
|
|
|
comp_pkt.completion_status);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* hv_register_block_invalidate() - Invoked when a config block invalidation
|
|
|
|
* arrives from the back-end driver.
|
|
|
|
* @pdev: The PCI driver's representation for this device.
|
|
|
|
* @context: Identifies the device.
|
|
|
|
* @block_invalidate: Identifies all of the blocks being invalidated.
|
|
|
|
*
|
|
|
|
* Return: 0 on success, -errno on failure
|
|
|
|
*/
|
2020-07-06 13:52:34 +00:00
|
|
|
static int hv_register_block_invalidate(struct pci_dev *pdev, void *context,
|
|
|
|
void (*block_invalidate)(void *context,
|
|
|
|
u64 block_mask))
|
2019-08-22 05:05:37 +00:00
|
|
|
{
|
|
|
|
struct hv_pcibus_device *hbus =
|
|
|
|
container_of(pdev->bus->sysdata, struct hv_pcibus_device,
|
|
|
|
sysdata);
|
|
|
|
struct hv_pci_dev *hpdev;
|
|
|
|
|
|
|
|
hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
|
|
|
|
if (!hpdev)
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
hpdev->block_invalidate = block_invalidate;
|
|
|
|
hpdev->invalidate_context = context;
|
|
|
|
|
|
|
|
put_pcichild(hpdev);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-02-16 21:56:23 +00:00
|
|
|
/* Interrupt management hooks */
|
|
|
|
static void hv_int_desc_free(struct hv_pci_dev *hpdev,
|
|
|
|
struct tran_int_desc *int_desc)
|
|
|
|
{
|
|
|
|
struct pci_delete_interrupt *int_pkt;
|
|
|
|
struct {
|
|
|
|
struct pci_packet pkt;
|
2016-08-23 04:45:51 +00:00
|
|
|
u8 buffer[sizeof(struct pci_delete_interrupt)];
|
2016-02-16 21:56:23 +00:00
|
|
|
} ctxt;
|
|
|
|
|
2022-05-11 15:23:19 +00:00
|
|
|
if (!int_desc->vector_count) {
|
|
|
|
kfree(int_desc);
|
|
|
|
return;
|
|
|
|
}
|
2016-02-16 21:56:23 +00:00
|
|
|
memset(&ctxt, 0, sizeof(ctxt));
|
|
|
|
int_pkt = (struct pci_delete_interrupt *)&ctxt.pkt.message;
|
2016-08-23 04:45:51 +00:00
|
|
|
int_pkt->message_type.type =
|
2016-02-16 21:56:23 +00:00
|
|
|
PCI_DELETE_INTERRUPT_MESSAGE;
|
|
|
|
int_pkt->wslot.slot = hpdev->desc.win_slot.slot;
|
|
|
|
int_pkt->int_desc = *int_desc;
|
|
|
|
vmbus_sendpacket(hpdev->hbus->hdev->channel, int_pkt, sizeof(*int_pkt),
|
2022-04-19 12:23:21 +00:00
|
|
|
0, VM_PKT_DATA_INBAND, 0);
|
2016-02-16 21:56:23 +00:00
|
|
|
kfree(int_desc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* hv_msi_free() - Free the MSI.
|
|
|
|
* @domain: The interrupt domain pointer
|
|
|
|
* @info: Extra MSI-related context
|
|
|
|
* @irq: Identifies the IRQ.
|
|
|
|
*
|
|
|
|
* The Hyper-V parent partition and hypervisor are tracking the
|
|
|
|
* messages that are in use, keeping the interrupt redirection
|
|
|
|
* table up to date. This callback sends a message that frees
|
|
|
|
* the IRT entry and related tracking nonsense.
|
|
|
|
*/
|
|
|
|
static void hv_msi_free(struct irq_domain *domain, struct msi_domain_info *info,
|
|
|
|
unsigned int irq)
|
|
|
|
{
|
|
|
|
struct hv_pcibus_device *hbus;
|
|
|
|
struct hv_pci_dev *hpdev;
|
|
|
|
struct pci_dev *pdev;
|
|
|
|
struct tran_int_desc *int_desc;
|
|
|
|
struct irq_data *irq_data = irq_domain_get_irq_data(domain, irq);
|
|
|
|
struct msi_desc *msi = irq_data_get_msi_desc(irq_data);
|
|
|
|
|
|
|
|
pdev = msi_desc_to_pci_dev(msi);
|
|
|
|
hbus = info->data;
|
2016-07-12 15:31:24 +00:00
|
|
|
int_desc = irq_data_get_irq_chip_data(irq_data);
|
|
|
|
if (!int_desc)
|
2016-02-16 21:56:23 +00:00
|
|
|
return;
|
|
|
|
|
2016-07-12 15:31:24 +00:00
|
|
|
irq_data->chip_data = NULL;
|
|
|
|
hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
|
|
|
|
if (!hpdev) {
|
|
|
|
kfree(int_desc);
|
|
|
|
return;
|
2016-02-16 21:56:23 +00:00
|
|
|
}
|
|
|
|
|
2016-07-12 15:31:24 +00:00
|
|
|
hv_int_desc_free(hpdev, int_desc);
|
2018-05-23 17:11:12 +00:00
|
|
|
put_pcichild(hpdev);
|
2016-02-16 21:56:23 +00:00
|
|
|
}
|
|
|
|
|
2016-10-31 11:04:09 +00:00
|
|
|
static void hv_irq_mask(struct irq_data *data)
|
2016-02-16 21:56:23 +00:00
|
|
|
{
|
|
|
|
pci_msi_mask_irq(data);
|
2022-01-05 19:32:36 +00:00
|
|
|
if (data->parent_data->chip->irq_mask)
|
|
|
|
irq_chip_mask_parent(data);
|
2016-02-16 21:56:23 +00:00
|
|
|
}
|
|
|
|
|
2016-10-31 11:04:09 +00:00
|
|
|
static void hv_irq_unmask(struct irq_data *data)
|
2016-02-16 21:56:23 +00:00
|
|
|
{
|
2022-02-17 03:45:19 +00:00
|
|
|
hv_arch_irq_unmask(data);
|
2017-05-24 20:41:28 +00:00
|
|
|
|
2022-01-05 19:32:36 +00:00
|
|
|
if (data->parent_data->chip->irq_unmask)
|
|
|
|
irq_chip_unmask_parent(data);
|
2016-02-16 21:56:23 +00:00
|
|
|
pci_msi_unmask_irq(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct compose_comp_ctxt {
|
|
|
|
struct hv_pci_compl comp_pkt;
|
|
|
|
struct tran_int_desc int_desc;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void hv_pci_compose_compl(void *context, struct pci_response *resp,
|
|
|
|
int resp_packet_size)
|
|
|
|
{
|
|
|
|
struct compose_comp_ctxt *comp_pkt = context;
|
|
|
|
struct pci_create_int_response *int_resp =
|
|
|
|
(struct pci_create_int_response *)resp;
|
|
|
|
|
2022-05-11 22:32:06 +00:00
|
|
|
if (resp_packet_size < sizeof(*int_resp)) {
|
|
|
|
comp_pkt->comp_pkt.completion_status = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
2016-02-16 21:56:23 +00:00
|
|
|
comp_pkt->comp_pkt.completion_status = resp->status;
|
|
|
|
comp_pkt->int_desc = int_resp->int_desc;
|
2022-05-11 22:32:06 +00:00
|
|
|
out:
|
2016-02-16 21:56:23 +00:00
|
|
|
complete(&comp_pkt->comp_pkt.host_event);
|
|
|
|
}
|
|
|
|
|
2017-05-24 20:41:28 +00:00
|
|
|
static u32 hv_compose_msi_req_v1(
|
|
|
|
struct pci_create_interrupt *int_pkt, struct cpumask *affinity,
|
2022-05-11 15:23:19 +00:00
|
|
|
u32 slot, u8 vector, u8 vector_count)
|
2017-05-24 20:41:28 +00:00
|
|
|
{
|
|
|
|
int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE;
|
|
|
|
int_pkt->wslot.slot = slot;
|
|
|
|
int_pkt->int_desc.vector = vector;
|
2022-05-11 15:23:19 +00:00
|
|
|
int_pkt->int_desc.vector_count = vector_count;
|
2022-01-05 19:32:35 +00:00
|
|
|
int_pkt->int_desc.delivery_mode = DELIVERY_MODE;
|
2017-05-24 20:41:28 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Create MSI w/ dummy vCPU set, overwritten by subsequent retarget in
|
|
|
|
* hv_irq_unmask().
|
|
|
|
*/
|
|
|
|
int_pkt->int_desc.cpu_mask = CPU_AFFINITY_ALL;
|
|
|
|
|
|
|
|
return sizeof(*int_pkt);
|
|
|
|
}
|
|
|
|
|
2021-07-12 21:58:18 +00:00
|
|
|
/*
|
|
|
|
* Create MSI w/ dummy vCPU set targeting just one vCPU, overwritten
|
|
|
|
* by subsequent retarget in hv_irq_unmask().
|
|
|
|
*/
|
|
|
|
static int hv_compose_msi_req_get_cpu(struct cpumask *affinity)
|
|
|
|
{
|
|
|
|
return cpumask_first_and(affinity, cpu_online_mask);
|
|
|
|
}
|
|
|
|
|
2017-05-24 20:41:28 +00:00
|
|
|
static u32 hv_compose_msi_req_v2(
|
|
|
|
struct pci_create_interrupt2 *int_pkt, struct cpumask *affinity,
|
2022-05-11 15:23:19 +00:00
|
|
|
u32 slot, u8 vector, u8 vector_count)
|
2017-05-24 20:41:28 +00:00
|
|
|
{
|
|
|
|
int cpu;
|
|
|
|
|
|
|
|
int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE2;
|
|
|
|
int_pkt->wslot.slot = slot;
|
|
|
|
int_pkt->int_desc.vector = vector;
|
2022-05-11 15:23:19 +00:00
|
|
|
int_pkt->int_desc.vector_count = vector_count;
|
2022-01-05 19:32:35 +00:00
|
|
|
int_pkt->int_desc.delivery_mode = DELIVERY_MODE;
|
2021-07-12 21:58:18 +00:00
|
|
|
cpu = hv_compose_msi_req_get_cpu(affinity);
|
|
|
|
int_pkt->int_desc.processor_array[0] =
|
|
|
|
hv_cpu_number_to_vp_number(cpu);
|
|
|
|
int_pkt->int_desc.processor_count = 1;
|
2017-05-24 20:41:28 +00:00
|
|
|
|
2021-07-12 21:58:18 +00:00
|
|
|
return sizeof(*int_pkt);
|
|
|
|
}
|
|
|
|
|
|
|
|
static u32 hv_compose_msi_req_v3(
|
|
|
|
struct pci_create_interrupt3 *int_pkt, struct cpumask *affinity,
|
2022-05-11 15:23:19 +00:00
|
|
|
u32 slot, u32 vector, u8 vector_count)
|
2021-07-12 21:58:18 +00:00
|
|
|
{
|
|
|
|
int cpu;
|
|
|
|
|
|
|
|
int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE3;
|
|
|
|
int_pkt->wslot.slot = slot;
|
|
|
|
int_pkt->int_desc.vector = vector;
|
|
|
|
int_pkt->int_desc.reserved = 0;
|
2022-05-11 15:23:19 +00:00
|
|
|
int_pkt->int_desc.vector_count = vector_count;
|
2022-01-05 19:32:35 +00:00
|
|
|
int_pkt->int_desc.delivery_mode = DELIVERY_MODE;
|
2021-07-12 21:58:18 +00:00
|
|
|
cpu = hv_compose_msi_req_get_cpu(affinity);
|
2017-05-24 20:41:28 +00:00
|
|
|
int_pkt->int_desc.processor_array[0] =
|
2017-08-02 16:09:18 +00:00
|
|
|
hv_cpu_number_to_vp_number(cpu);
|
2017-05-24 20:41:28 +00:00
|
|
|
int_pkt->int_desc.processor_count = 1;
|
|
|
|
|
|
|
|
return sizeof(*int_pkt);
|
|
|
|
}
|
|
|
|
|
2016-02-16 21:56:23 +00:00
|
|
|
/**
|
|
|
|
* hv_compose_msi_msg() - Supplies a valid MSI address/data
|
|
|
|
* @data: Everything about this MSI
|
|
|
|
* @msg: Buffer that is filled in by this function
|
|
|
|
*
|
|
|
|
* This function unpacks the IRQ looking for target CPU set, IDT
|
|
|
|
* vector and mode and sends a message to the parent partition
|
|
|
|
* asking for a mapping for that tuple in this partition. The
|
|
|
|
* response supplies a data value and address to which that data
|
|
|
|
* should be written to trigger that interrupt.
|
|
|
|
*/
|
|
|
|
static void hv_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
|
|
|
|
{
|
|
|
|
struct hv_pcibus_device *hbus;
|
2020-04-06 00:15:10 +00:00
|
|
|
struct vmbus_channel *channel;
|
2016-02-16 21:56:23 +00:00
|
|
|
struct hv_pci_dev *hpdev;
|
|
|
|
struct pci_bus *pbus;
|
|
|
|
struct pci_dev *pdev;
|
2017-11-01 20:30:53 +00:00
|
|
|
struct cpumask *dest;
|
2016-02-16 21:56:23 +00:00
|
|
|
struct compose_comp_ctxt comp;
|
|
|
|
struct tran_int_desc *int_desc;
|
2022-05-11 15:23:19 +00:00
|
|
|
struct msi_desc *msi_desc;
|
|
|
|
u8 vector, vector_count;
|
2016-02-16 21:56:23 +00:00
|
|
|
struct {
|
2017-05-24 20:41:28 +00:00
|
|
|
struct pci_packet pci_pkt;
|
|
|
|
union {
|
|
|
|
struct pci_create_interrupt v1;
|
|
|
|
struct pci_create_interrupt2 v2;
|
2021-07-12 21:58:18 +00:00
|
|
|
struct pci_create_interrupt3 v3;
|
2017-05-24 20:41:28 +00:00
|
|
|
} int_pkts;
|
|
|
|
} __packed ctxt;
|
2022-04-19 12:23:25 +00:00
|
|
|
u64 trans_id;
|
2017-05-24 20:41:28 +00:00
|
|
|
u32 size;
|
2016-02-16 21:56:23 +00:00
|
|
|
int ret;
|
|
|
|
|
2022-05-11 15:23:02 +00:00
|
|
|
/* Reuse the previous allocation */
|
|
|
|
if (data->chip_data) {
|
|
|
|
int_desc = data->chip_data;
|
|
|
|
msg->address_hi = int_desc->address >> 32;
|
|
|
|
msg->address_lo = int_desc->address & 0xffffffff;
|
|
|
|
msg->data = int_desc->data;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-11 15:23:19 +00:00
|
|
|
msi_desc = irq_data_get_msi_desc(data);
|
|
|
|
pdev = msi_desc_to_pci_dev(msi_desc);
|
2017-11-01 20:30:53 +00:00
|
|
|
dest = irq_data_get_effective_affinity_mask(data);
|
2016-02-16 21:56:23 +00:00
|
|
|
pbus = pdev->bus;
|
|
|
|
hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata);
|
2020-04-06 00:15:10 +00:00
|
|
|
channel = hbus->hdev->channel;
|
2016-02-16 21:56:23 +00:00
|
|
|
hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
|
|
|
|
if (!hpdev)
|
|
|
|
goto return_null_message;
|
|
|
|
|
2017-03-24 18:07:22 +00:00
|
|
|
int_desc = kzalloc(sizeof(*int_desc), GFP_ATOMIC);
|
2016-02-16 21:56:23 +00:00
|
|
|
if (!int_desc)
|
|
|
|
goto drop_reference;
|
|
|
|
|
2022-05-11 15:23:19 +00:00
|
|
|
if (!msi_desc->pci.msi_attrib.is_msix && msi_desc->nvec_used > 1) {
|
|
|
|
/*
|
|
|
|
* If this is not the first MSI of Multi MSI, we already have
|
|
|
|
* a mapping. Can exit early.
|
|
|
|
*/
|
|
|
|
if (msi_desc->irq != data->irq) {
|
|
|
|
data->chip_data = int_desc;
|
|
|
|
int_desc->address = msi_desc->msg.address_lo |
|
|
|
|
(u64)msi_desc->msg.address_hi << 32;
|
|
|
|
int_desc->data = msi_desc->msg.data +
|
|
|
|
(data->irq - msi_desc->irq);
|
|
|
|
msg->address_hi = msi_desc->msg.address_hi;
|
|
|
|
msg->address_lo = msi_desc->msg.address_lo;
|
|
|
|
msg->data = int_desc->data;
|
|
|
|
put_pcichild(hpdev);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* The vector we select here is a dummy value. The correct
|
|
|
|
* value gets sent to the hypervisor in unmask(). This needs
|
|
|
|
* to be aligned with the count, and also not zero. Multi-msi
|
|
|
|
* is powers of 2 up to 32, so 32 will always work here.
|
|
|
|
*/
|
|
|
|
vector = 32;
|
|
|
|
vector_count = msi_desc->nvec_used;
|
|
|
|
} else {
|
|
|
|
vector = hv_msi_get_int_vector(data);
|
|
|
|
vector_count = 1;
|
|
|
|
}
|
|
|
|
|
2016-02-16 21:56:23 +00:00
|
|
|
memset(&ctxt, 0, sizeof(ctxt));
|
|
|
|
init_completion(&comp.comp_pkt.host_event);
|
2017-05-24 20:41:28 +00:00
|
|
|
ctxt.pci_pkt.completion_func = hv_pci_compose_compl;
|
|
|
|
ctxt.pci_pkt.compl_ctxt = ∁
|
|
|
|
|
2019-11-25 05:33:53 +00:00
|
|
|
switch (hbus->protocol_version) {
|
2017-05-24 20:41:28 +00:00
|
|
|
case PCI_PROTOCOL_VERSION_1_1:
|
|
|
|
size = hv_compose_msi_req_v1(&ctxt.int_pkts.v1,
|
2017-11-01 20:30:53 +00:00
|
|
|
dest,
|
2017-05-24 20:41:28 +00:00
|
|
|
hpdev->desc.win_slot.slot,
|
2022-05-11 15:23:19 +00:00
|
|
|
vector,
|
|
|
|
vector_count);
|
2017-05-24 20:41:28 +00:00
|
|
|
break;
|
2016-02-16 21:56:23 +00:00
|
|
|
|
2017-05-24 20:41:28 +00:00
|
|
|
case PCI_PROTOCOL_VERSION_1_2:
|
2020-02-26 05:06:08 +00:00
|
|
|
case PCI_PROTOCOL_VERSION_1_3:
|
2017-05-24 20:41:28 +00:00
|
|
|
size = hv_compose_msi_req_v2(&ctxt.int_pkts.v2,
|
2017-11-01 20:30:53 +00:00
|
|
|
dest,
|
2017-05-24 20:41:28 +00:00
|
|
|
hpdev->desc.win_slot.slot,
|
2022-05-11 15:23:19 +00:00
|
|
|
vector,
|
|
|
|
vector_count);
|
2017-05-24 20:41:28 +00:00
|
|
|
break;
|
|
|
|
|
2021-07-12 21:58:18 +00:00
|
|
|
case PCI_PROTOCOL_VERSION_1_4:
|
|
|
|
size = hv_compose_msi_req_v3(&ctxt.int_pkts.v3,
|
|
|
|
dest,
|
|
|
|
hpdev->desc.win_slot.slot,
|
2022-05-11 15:23:19 +00:00
|
|
|
vector,
|
|
|
|
vector_count);
|
2021-07-12 21:58:18 +00:00
|
|
|
break;
|
|
|
|
|
2017-05-24 20:41:28 +00:00
|
|
|
default:
|
|
|
|
/* As we only negotiate protocol versions known to this driver,
|
|
|
|
* this path should never hit. However, this is it not a hot
|
|
|
|
* path so we print a message to aid future updates.
|
|
|
|
*/
|
|
|
|
dev_err(&hbus->hdev->device,
|
|
|
|
"Unexpected vPCI protocol, update driver.");
|
|
|
|
goto free_int_desc;
|
2016-02-16 21:56:23 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 12:23:25 +00:00
|
|
|
ret = vmbus_sendpacket_getid(hpdev->hbus->hdev->channel, &ctxt.int_pkts,
|
|
|
|
size, (unsigned long)&ctxt.pci_pkt,
|
|
|
|
&trans_id, VM_PKT_DATA_INBAND,
|
|
|
|
VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
|
2017-05-24 20:41:28 +00:00
|
|
|
if (ret) {
|
|
|
|
dev_err(&hbus->hdev->device,
|
|
|
|
"Sending request for interrupt failed: 0x%x",
|
|
|
|
comp.comp_pkt.completion_status);
|
2016-08-23 04:48:11 +00:00
|
|
|
goto free_int_desc;
|
2017-05-24 20:41:28 +00:00
|
|
|
}
|
2016-08-23 04:48:11 +00:00
|
|
|
|
2020-04-06 00:15:10 +00:00
|
|
|
/*
|
|
|
|
* Prevents hv_pci_onchannelcallback() from running concurrently
|
|
|
|
* in the tasklet.
|
|
|
|
*/
|
2021-03-09 08:42:15 +00:00
|
|
|
tasklet_disable_in_atomic(&channel->callback_event);
|
2020-04-06 00:15:10 +00:00
|
|
|
|
2017-07-31 23:48:29 +00:00
|
|
|
/*
|
|
|
|
* Since this function is called with IRQ locks held, can't
|
|
|
|
* do normal wait for completion; instead poll.
|
|
|
|
*/
|
PCI: hv: Fix 2 hang issues in hv_compose_msi_msg()
1. With the patch "x86/vector/msi: Switch to global reservation mode",
the recent v4.15 and newer kernels always hang for 1-vCPU Hyper-V VM
with SR-IOV. This is because when we reach hv_compose_msi_msg() by
request_irq() -> request_threaded_irq() ->__setup_irq()->irq_startup()
-> __irq_startup() -> irq_domain_activate_irq() -> ... ->
msi_domain_activate() -> ... -> hv_compose_msi_msg(), local irq is
disabled in __setup_irq().
Note: when we reach hv_compose_msi_msg() by another code path:
pci_enable_msix_range() -> ... -> irq_domain_activate_irq() -> ... ->
hv_compose_msi_msg(), local irq is not disabled.
hv_compose_msi_msg() depends on an interrupt from the host.
With interrupts disabled, a UP VM always hangs in the busy loop in
the function, because the interrupt callback hv_pci_onchannelcallback()
can not be called.
We can do nothing but work it around by polling the channel. This
is ugly, but we don't have any other choice.
2. If the host is ejecting the VF device before we reach
hv_compose_msi_msg(), in a UP VM, we can hang in hv_compose_msi_msg()
forever, because at this time the host doesn't respond to the
CREATE_INTERRUPT request. This issue exists the first day the
pci-hyperv driver appears in the kernel.
Luckily, this can also by worked around by polling the channel
for the PCI_EJECT message and hpdev->state, and by checking the
PCI vendor ID.
Note: actually the above 2 issues also happen to a SMP VM, if
"hbus->hdev->channel->target_cpu == smp_processor_id()" is true.
Fixes: 4900be83602b ("x86/vector/msi: Switch to global reservation mode")
Tested-by: Adrian Suhov <v-adsuho@microsoft.com>
Tested-by: Chris Valean <v-chvale@microsoft.com>
Signed-off-by: Dexuan Cui <decui@microsoft.com>
Signed-off-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Reviewed-by: Michael Kelley <mikelley@microsoft.com>
Acked-by: Haiyang Zhang <haiyangz@microsoft.com>
Cc: <stable@vger.kernel.org>
Cc: Stephen Hemminger <sthemmin@microsoft.com>
Cc: K. Y. Srinivasan <kys@microsoft.com>
Cc: Vitaly Kuznetsov <vkuznets@redhat.com>
Cc: Jack Morgenstein <jackm@mellanox.com>
2018-03-15 14:21:08 +00:00
|
|
|
while (!try_wait_for_completion(&comp.comp_pkt.host_event)) {
|
2020-04-06 00:15:10 +00:00
|
|
|
unsigned long flags;
|
|
|
|
|
PCI: hv: Fix 2 hang issues in hv_compose_msi_msg()
1. With the patch "x86/vector/msi: Switch to global reservation mode",
the recent v4.15 and newer kernels always hang for 1-vCPU Hyper-V VM
with SR-IOV. This is because when we reach hv_compose_msi_msg() by
request_irq() -> request_threaded_irq() ->__setup_irq()->irq_startup()
-> __irq_startup() -> irq_domain_activate_irq() -> ... ->
msi_domain_activate() -> ... -> hv_compose_msi_msg(), local irq is
disabled in __setup_irq().
Note: when we reach hv_compose_msi_msg() by another code path:
pci_enable_msix_range() -> ... -> irq_domain_activate_irq() -> ... ->
hv_compose_msi_msg(), local irq is not disabled.
hv_compose_msi_msg() depends on an interrupt from the host.
With interrupts disabled, a UP VM always hangs in the busy loop in
the function, because the interrupt callback hv_pci_onchannelcallback()
can not be called.
We can do nothing but work it around by polling the channel. This
is ugly, but we don't have any other choice.
2. If the host is ejecting the VF device before we reach
hv_compose_msi_msg(), in a UP VM, we can hang in hv_compose_msi_msg()
forever, because at this time the host doesn't respond to the
CREATE_INTERRUPT request. This issue exists the first day the
pci-hyperv driver appears in the kernel.
Luckily, this can also by worked around by polling the channel
for the PCI_EJECT message and hpdev->state, and by checking the
PCI vendor ID.
Note: actually the above 2 issues also happen to a SMP VM, if
"hbus->hdev->channel->target_cpu == smp_processor_id()" is true.
Fixes: 4900be83602b ("x86/vector/msi: Switch to global reservation mode")
Tested-by: Adrian Suhov <v-adsuho@microsoft.com>
Tested-by: Chris Valean <v-chvale@microsoft.com>
Signed-off-by: Dexuan Cui <decui@microsoft.com>
Signed-off-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Reviewed-by: Michael Kelley <mikelley@microsoft.com>
Acked-by: Haiyang Zhang <haiyangz@microsoft.com>
Cc: <stable@vger.kernel.org>
Cc: Stephen Hemminger <sthemmin@microsoft.com>
Cc: K. Y. Srinivasan <kys@microsoft.com>
Cc: Vitaly Kuznetsov <vkuznets@redhat.com>
Cc: Jack Morgenstein <jackm@mellanox.com>
2018-03-15 14:21:08 +00:00
|
|
|
/* 0xFFFF means an invalid PCI VENDOR ID. */
|
|
|
|
if (hv_pcifront_get_vendor_id(hpdev) == 0xFFFF) {
|
|
|
|
dev_err_once(&hbus->hdev->device,
|
|
|
|
"the device has gone\n");
|
2020-04-06 00:15:10 +00:00
|
|
|
goto enable_tasklet;
|
PCI: hv: Fix 2 hang issues in hv_compose_msi_msg()
1. With the patch "x86/vector/msi: Switch to global reservation mode",
the recent v4.15 and newer kernels always hang for 1-vCPU Hyper-V VM
with SR-IOV. This is because when we reach hv_compose_msi_msg() by
request_irq() -> request_threaded_irq() ->__setup_irq()->irq_startup()
-> __irq_startup() -> irq_domain_activate_irq() -> ... ->
msi_domain_activate() -> ... -> hv_compose_msi_msg(), local irq is
disabled in __setup_irq().
Note: when we reach hv_compose_msi_msg() by another code path:
pci_enable_msix_range() -> ... -> irq_domain_activate_irq() -> ... ->
hv_compose_msi_msg(), local irq is not disabled.
hv_compose_msi_msg() depends on an interrupt from the host.
With interrupts disabled, a UP VM always hangs in the busy loop in
the function, because the interrupt callback hv_pci_onchannelcallback()
can not be called.
We can do nothing but work it around by polling the channel. This
is ugly, but we don't have any other choice.
2. If the host is ejecting the VF device before we reach
hv_compose_msi_msg(), in a UP VM, we can hang in hv_compose_msi_msg()
forever, because at this time the host doesn't respond to the
CREATE_INTERRUPT request. This issue exists the first day the
pci-hyperv driver appears in the kernel.
Luckily, this can also by worked around by polling the channel
for the PCI_EJECT message and hpdev->state, and by checking the
PCI vendor ID.
Note: actually the above 2 issues also happen to a SMP VM, if
"hbus->hdev->channel->target_cpu == smp_processor_id()" is true.
Fixes: 4900be83602b ("x86/vector/msi: Switch to global reservation mode")
Tested-by: Adrian Suhov <v-adsuho@microsoft.com>
Tested-by: Chris Valean <v-chvale@microsoft.com>
Signed-off-by: Dexuan Cui <decui@microsoft.com>
Signed-off-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Reviewed-by: Michael Kelley <mikelley@microsoft.com>
Acked-by: Haiyang Zhang <haiyangz@microsoft.com>
Cc: <stable@vger.kernel.org>
Cc: Stephen Hemminger <sthemmin@microsoft.com>
Cc: K. Y. Srinivasan <kys@microsoft.com>
Cc: Vitaly Kuznetsov <vkuznets@redhat.com>
Cc: Jack Morgenstein <jackm@mellanox.com>
2018-03-15 14:21:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2020-04-06 00:15:10 +00:00
|
|
|
* Make sure that the ring buffer data structure doesn't get
|
|
|
|
* freed while we dereference the ring buffer pointer. Test
|
|
|
|
* for the channel's onchannel_callback being NULL within a
|
|
|
|
* sched_lock critical section. See also the inline comments
|
|
|
|
* in vmbus_reset_channel_cb().
|
PCI: hv: Fix 2 hang issues in hv_compose_msi_msg()
1. With the patch "x86/vector/msi: Switch to global reservation mode",
the recent v4.15 and newer kernels always hang for 1-vCPU Hyper-V VM
with SR-IOV. This is because when we reach hv_compose_msi_msg() by
request_irq() -> request_threaded_irq() ->__setup_irq()->irq_startup()
-> __irq_startup() -> irq_domain_activate_irq() -> ... ->
msi_domain_activate() -> ... -> hv_compose_msi_msg(), local irq is
disabled in __setup_irq().
Note: when we reach hv_compose_msi_msg() by another code path:
pci_enable_msix_range() -> ... -> irq_domain_activate_irq() -> ... ->
hv_compose_msi_msg(), local irq is not disabled.
hv_compose_msi_msg() depends on an interrupt from the host.
With interrupts disabled, a UP VM always hangs in the busy loop in
the function, because the interrupt callback hv_pci_onchannelcallback()
can not be called.
We can do nothing but work it around by polling the channel. This
is ugly, but we don't have any other choice.
2. If the host is ejecting the VF device before we reach
hv_compose_msi_msg(), in a UP VM, we can hang in hv_compose_msi_msg()
forever, because at this time the host doesn't respond to the
CREATE_INTERRUPT request. This issue exists the first day the
pci-hyperv driver appears in the kernel.
Luckily, this can also by worked around by polling the channel
for the PCI_EJECT message and hpdev->state, and by checking the
PCI vendor ID.
Note: actually the above 2 issues also happen to a SMP VM, if
"hbus->hdev->channel->target_cpu == smp_processor_id()" is true.
Fixes: 4900be83602b ("x86/vector/msi: Switch to global reservation mode")
Tested-by: Adrian Suhov <v-adsuho@microsoft.com>
Tested-by: Chris Valean <v-chvale@microsoft.com>
Signed-off-by: Dexuan Cui <decui@microsoft.com>
Signed-off-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Reviewed-by: Michael Kelley <mikelley@microsoft.com>
Acked-by: Haiyang Zhang <haiyangz@microsoft.com>
Cc: <stable@vger.kernel.org>
Cc: Stephen Hemminger <sthemmin@microsoft.com>
Cc: K. Y. Srinivasan <kys@microsoft.com>
Cc: Vitaly Kuznetsov <vkuznets@redhat.com>
Cc: Jack Morgenstein <jackm@mellanox.com>
2018-03-15 14:21:08 +00:00
|
|
|
*/
|
2020-04-06 00:15:10 +00:00
|
|
|
spin_lock_irqsave(&channel->sched_lock, flags);
|
|
|
|
if (unlikely(channel->onchannel_callback == NULL)) {
|
|
|
|
spin_unlock_irqrestore(&channel->sched_lock, flags);
|
|
|
|
goto enable_tasklet;
|
|
|
|
}
|
|
|
|
hv_pci_onchannelcallback(hbus);
|
|
|
|
spin_unlock_irqrestore(&channel->sched_lock, flags);
|
PCI: hv: Fix 2 hang issues in hv_compose_msi_msg()
1. With the patch "x86/vector/msi: Switch to global reservation mode",
the recent v4.15 and newer kernels always hang for 1-vCPU Hyper-V VM
with SR-IOV. This is because when we reach hv_compose_msi_msg() by
request_irq() -> request_threaded_irq() ->__setup_irq()->irq_startup()
-> __irq_startup() -> irq_domain_activate_irq() -> ... ->
msi_domain_activate() -> ... -> hv_compose_msi_msg(), local irq is
disabled in __setup_irq().
Note: when we reach hv_compose_msi_msg() by another code path:
pci_enable_msix_range() -> ... -> irq_domain_activate_irq() -> ... ->
hv_compose_msi_msg(), local irq is not disabled.
hv_compose_msi_msg() depends on an interrupt from the host.
With interrupts disabled, a UP VM always hangs in the busy loop in
the function, because the interrupt callback hv_pci_onchannelcallback()
can not be called.
We can do nothing but work it around by polling the channel. This
is ugly, but we don't have any other choice.
2. If the host is ejecting the VF device before we reach
hv_compose_msi_msg(), in a UP VM, we can hang in hv_compose_msi_msg()
forever, because at this time the host doesn't respond to the
CREATE_INTERRUPT request. This issue exists the first day the
pci-hyperv driver appears in the kernel.
Luckily, this can also by worked around by polling the channel
for the PCI_EJECT message and hpdev->state, and by checking the
PCI vendor ID.
Note: actually the above 2 issues also happen to a SMP VM, if
"hbus->hdev->channel->target_cpu == smp_processor_id()" is true.
Fixes: 4900be83602b ("x86/vector/msi: Switch to global reservation mode")
Tested-by: Adrian Suhov <v-adsuho@microsoft.com>
Tested-by: Chris Valean <v-chvale@microsoft.com>
Signed-off-by: Dexuan Cui <decui@microsoft.com>
Signed-off-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Reviewed-by: Michael Kelley <mikelley@microsoft.com>
Acked-by: Haiyang Zhang <haiyangz@microsoft.com>
Cc: <stable@vger.kernel.org>
Cc: Stephen Hemminger <sthemmin@microsoft.com>
Cc: K. Y. Srinivasan <kys@microsoft.com>
Cc: Vitaly Kuznetsov <vkuznets@redhat.com>
Cc: Jack Morgenstein <jackm@mellanox.com>
2018-03-15 14:21:08 +00:00
|
|
|
|
|
|
|
if (hpdev->state == hv_pcichild_ejecting) {
|
|
|
|
dev_err_once(&hbus->hdev->device,
|
|
|
|
"the device is being ejected\n");
|
2020-04-06 00:15:10 +00:00
|
|
|
goto enable_tasklet;
|
PCI: hv: Fix 2 hang issues in hv_compose_msi_msg()
1. With the patch "x86/vector/msi: Switch to global reservation mode",
the recent v4.15 and newer kernels always hang for 1-vCPU Hyper-V VM
with SR-IOV. This is because when we reach hv_compose_msi_msg() by
request_irq() -> request_threaded_irq() ->__setup_irq()->irq_startup()
-> __irq_startup() -> irq_domain_activate_irq() -> ... ->
msi_domain_activate() -> ... -> hv_compose_msi_msg(), local irq is
disabled in __setup_irq().
Note: when we reach hv_compose_msi_msg() by another code path:
pci_enable_msix_range() -> ... -> irq_domain_activate_irq() -> ... ->
hv_compose_msi_msg(), local irq is not disabled.
hv_compose_msi_msg() depends on an interrupt from the host.
With interrupts disabled, a UP VM always hangs in the busy loop in
the function, because the interrupt callback hv_pci_onchannelcallback()
can not be called.
We can do nothing but work it around by polling the channel. This
is ugly, but we don't have any other choice.
2. If the host is ejecting the VF device before we reach
hv_compose_msi_msg(), in a UP VM, we can hang in hv_compose_msi_msg()
forever, because at this time the host doesn't respond to the
CREATE_INTERRUPT request. This issue exists the first day the
pci-hyperv driver appears in the kernel.
Luckily, this can also by worked around by polling the channel
for the PCI_EJECT message and hpdev->state, and by checking the
PCI vendor ID.
Note: actually the above 2 issues also happen to a SMP VM, if
"hbus->hdev->channel->target_cpu == smp_processor_id()" is true.
Fixes: 4900be83602b ("x86/vector/msi: Switch to global reservation mode")
Tested-by: Adrian Suhov <v-adsuho@microsoft.com>
Tested-by: Chris Valean <v-chvale@microsoft.com>
Signed-off-by: Dexuan Cui <decui@microsoft.com>
Signed-off-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Reviewed-by: Michael Kelley <mikelley@microsoft.com>
Acked-by: Haiyang Zhang <haiyangz@microsoft.com>
Cc: <stable@vger.kernel.org>
Cc: Stephen Hemminger <sthemmin@microsoft.com>
Cc: K. Y. Srinivasan <kys@microsoft.com>
Cc: Vitaly Kuznetsov <vkuznets@redhat.com>
Cc: Jack Morgenstein <jackm@mellanox.com>
2018-03-15 14:21:08 +00:00
|
|
|
}
|
|
|
|
|
2017-07-31 23:48:29 +00:00
|
|
|
udelay(100);
|
PCI: hv: Fix 2 hang issues in hv_compose_msi_msg()
1. With the patch "x86/vector/msi: Switch to global reservation mode",
the recent v4.15 and newer kernels always hang for 1-vCPU Hyper-V VM
with SR-IOV. This is because when we reach hv_compose_msi_msg() by
request_irq() -> request_threaded_irq() ->__setup_irq()->irq_startup()
-> __irq_startup() -> irq_domain_activate_irq() -> ... ->
msi_domain_activate() -> ... -> hv_compose_msi_msg(), local irq is
disabled in __setup_irq().
Note: when we reach hv_compose_msi_msg() by another code path:
pci_enable_msix_range() -> ... -> irq_domain_activate_irq() -> ... ->
hv_compose_msi_msg(), local irq is not disabled.
hv_compose_msi_msg() depends on an interrupt from the host.
With interrupts disabled, a UP VM always hangs in the busy loop in
the function, because the interrupt callback hv_pci_onchannelcallback()
can not be called.
We can do nothing but work it around by polling the channel. This
is ugly, but we don't have any other choice.
2. If the host is ejecting the VF device before we reach
hv_compose_msi_msg(), in a UP VM, we can hang in hv_compose_msi_msg()
forever, because at this time the host doesn't respond to the
CREATE_INTERRUPT request. This issue exists the first day the
pci-hyperv driver appears in the kernel.
Luckily, this can also by worked around by polling the channel
for the PCI_EJECT message and hpdev->state, and by checking the
PCI vendor ID.
Note: actually the above 2 issues also happen to a SMP VM, if
"hbus->hdev->channel->target_cpu == smp_processor_id()" is true.
Fixes: 4900be83602b ("x86/vector/msi: Switch to global reservation mode")
Tested-by: Adrian Suhov <v-adsuho@microsoft.com>
Tested-by: Chris Valean <v-chvale@microsoft.com>
Signed-off-by: Dexuan Cui <decui@microsoft.com>
Signed-off-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Reviewed-by: Michael Kelley <mikelley@microsoft.com>
Acked-by: Haiyang Zhang <haiyangz@microsoft.com>
Cc: <stable@vger.kernel.org>
Cc: Stephen Hemminger <sthemmin@microsoft.com>
Cc: K. Y. Srinivasan <kys@microsoft.com>
Cc: Vitaly Kuznetsov <vkuznets@redhat.com>
Cc: Jack Morgenstein <jackm@mellanox.com>
2018-03-15 14:21:08 +00:00
|
|
|
}
|
2016-02-16 21:56:23 +00:00
|
|
|
|
2020-04-06 00:15:10 +00:00
|
|
|
tasklet_enable(&channel->callback_event);
|
|
|
|
|
2016-02-16 21:56:23 +00:00
|
|
|
if (comp.comp_pkt.completion_status < 0) {
|
|
|
|
dev_err(&hbus->hdev->device,
|
|
|
|
"Request for interrupt failed: 0x%x",
|
|
|
|
comp.comp_pkt.completion_status);
|
|
|
|
goto free_int_desc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Record the assignment so that this can be unwound later. Using
|
|
|
|
* irq_set_chip_data() here would be appropriate, but the lock it takes
|
|
|
|
* is already held.
|
|
|
|
*/
|
|
|
|
*int_desc = comp.int_desc;
|
|
|
|
data->chip_data = int_desc;
|
|
|
|
|
|
|
|
/* Pass up the result. */
|
|
|
|
msg->address_hi = comp.int_desc.address >> 32;
|
|
|
|
msg->address_lo = comp.int_desc.address & 0xffffffff;
|
|
|
|
msg->data = comp.int_desc.data;
|
|
|
|
|
2018-05-23 17:11:12 +00:00
|
|
|
put_pcichild(hpdev);
|
2016-02-16 21:56:23 +00:00
|
|
|
return;
|
|
|
|
|
2020-04-06 00:15:10 +00:00
|
|
|
enable_tasklet:
|
|
|
|
tasklet_enable(&channel->callback_event);
|
2022-04-19 12:23:25 +00:00
|
|
|
/*
|
|
|
|
* The completion packet on the stack becomes invalid after 'return';
|
|
|
|
* remove the ID from the VMbus requestor if the identifier is still
|
|
|
|
* mapped to/associated with the packet. (The identifier could have
|
|
|
|
* been 're-used', i.e., already removed and (re-)mapped.)
|
|
|
|
*
|
|
|
|
* Cf. hv_pci_onchannelcallback().
|
|
|
|
*/
|
|
|
|
vmbus_request_addr_match(channel, trans_id, (unsigned long)&ctxt.pci_pkt);
|
2016-02-16 21:56:23 +00:00
|
|
|
free_int_desc:
|
|
|
|
kfree(int_desc);
|
|
|
|
drop_reference:
|
2018-05-23 17:11:12 +00:00
|
|
|
put_pcichild(hpdev);
|
2016-02-16 21:56:23 +00:00
|
|
|
return_null_message:
|
|
|
|
msg->address_hi = 0;
|
|
|
|
msg->address_lo = 0;
|
|
|
|
msg->data = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* HW Interrupt Chip Descriptor */
|
|
|
|
static struct irq_chip hv_msi_irq_chip = {
|
|
|
|
.name = "Hyper-V PCIe MSI",
|
|
|
|
.irq_compose_msi_msg = hv_compose_msi_msg,
|
2022-01-05 19:32:35 +00:00
|
|
|
.irq_set_affinity = irq_chip_set_affinity_parent,
|
2022-01-05 19:32:36 +00:00
|
|
|
#ifdef CONFIG_X86
|
2016-02-16 21:56:23 +00:00
|
|
|
.irq_ack = irq_chip_ack_parent,
|
2022-01-05 19:32:36 +00:00
|
|
|
#elif defined(CONFIG_ARM64)
|
|
|
|
.irq_eoi = irq_chip_eoi_parent,
|
|
|
|
#endif
|
2016-02-16 21:56:23 +00:00
|
|
|
.irq_mask = hv_irq_mask,
|
|
|
|
.irq_unmask = hv_irq_unmask,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct msi_domain_ops hv_msi_ops = {
|
2022-01-05 19:32:35 +00:00
|
|
|
.msi_prepare = hv_msi_prepare,
|
2016-02-16 21:56:23 +00:00
|
|
|
.msi_free = hv_msi_free,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* hv_pcie_init_irq_domain() - Initialize IRQ domain
|
|
|
|
* @hbus: The root PCI bus
|
|
|
|
*
|
|
|
|
* This function creates an IRQ domain which will be used for
|
|
|
|
* interrupts from devices that have been passed through. These
|
|
|
|
* devices only support MSI and MSI-X, not line-based interrupts
|
|
|
|
* or simulations of line-based interrupts through PCIe's
|
|
|
|
* fabric-layer messages. Because interrupts are remapped, we
|
|
|
|
* can support multi-message MSI here.
|
|
|
|
*
|
|
|
|
* Return: '0' on success and error value on failure
|
|
|
|
*/
|
|
|
|
static int hv_pcie_init_irq_domain(struct hv_pcibus_device *hbus)
|
|
|
|
{
|
|
|
|
hbus->msi_info.chip = &hv_msi_irq_chip;
|
|
|
|
hbus->msi_info.ops = &hv_msi_ops;
|
|
|
|
hbus->msi_info.flags = (MSI_FLAG_USE_DEF_DOM_OPS |
|
|
|
|
MSI_FLAG_USE_DEF_CHIP_OPS | MSI_FLAG_MULTI_PCI_MSI |
|
|
|
|
MSI_FLAG_PCI_MSIX);
|
2022-01-05 19:32:35 +00:00
|
|
|
hbus->msi_info.handler = FLOW_HANDLER;
|
|
|
|
hbus->msi_info.handler_name = FLOW_NAME;
|
2016-02-16 21:56:23 +00:00
|
|
|
hbus->msi_info.data = hbus;
|
2021-07-26 18:06:56 +00:00
|
|
|
hbus->irq_domain = pci_msi_create_irq_domain(hbus->fwnode,
|
2016-02-16 21:56:23 +00:00
|
|
|
&hbus->msi_info,
|
2022-01-05 19:32:35 +00:00
|
|
|
hv_pci_get_root_domain());
|
2016-02-16 21:56:23 +00:00
|
|
|
if (!hbus->irq_domain) {
|
|
|
|
dev_err(&hbus->hdev->device,
|
|
|
|
"Failed to build an MSI IRQ domain\n");
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2021-07-26 18:06:56 +00:00
|
|
|
dev_set_msi_domain(&hbus->bridge->dev, hbus->irq_domain);
|
|
|
|
|
2016-02-16 21:56:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get_bar_size() - Get the address space consumed by a BAR
|
|
|
|
* @bar_val: Value that a BAR returned after -1 was written
|
|
|
|
* to it.
|
|
|
|
*
|
|
|
|
* This function returns the size of the BAR, rounded up to 1
|
|
|
|
* page. It has to be rounded up because the hypervisor's page
|
|
|
|
* table entry that maps the BAR into the VM can't specify an
|
|
|
|
* offset within a page. The invariant is that the hypervisor
|
|
|
|
* must place any BARs of smaller than page length at the
|
|
|
|
* beginning of a page.
|
|
|
|
*
|
|
|
|
* Return: Size in bytes of the consumed MMIO space.
|
|
|
|
*/
|
|
|
|
static u64 get_bar_size(u64 bar_val)
|
|
|
|
{
|
|
|
|
return round_up((1 + ~(bar_val & PCI_BASE_ADDRESS_MEM_MASK)),
|
|
|
|
PAGE_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* survey_child_resources() - Total all MMIO requirements
|
|
|
|
* @hbus: Root PCI bus, as understood by this driver
|
|
|
|
*/
|
|
|
|
static void survey_child_resources(struct hv_pcibus_device *hbus)
|
|
|
|
{
|
|
|
|
struct hv_pci_dev *hpdev;
|
|
|
|
resource_size_t bar_size = 0;
|
|
|
|
unsigned long flags;
|
|
|
|
struct completion *event;
|
|
|
|
u64 bar_val;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* If nobody is waiting on the answer, don't compute it. */
|
|
|
|
event = xchg(&hbus->survey_event, NULL);
|
|
|
|
if (!event)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* If the answer has already been computed, go with it. */
|
|
|
|
if (hbus->low_mmio_space || hbus->high_mmio_space) {
|
|
|
|
complete(event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_lock_irqsave(&hbus->device_list_lock, flags);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Due to an interesting quirk of the PCI spec, all memory regions
|
|
|
|
* for a child device are a power of 2 in size and aligned in memory,
|
|
|
|
* so it's sufficient to just add them up without tracking alignment.
|
|
|
|
*/
|
2018-05-23 17:11:14 +00:00
|
|
|
list_for_each_entry(hpdev, &hbus->children, list_entry) {
|
2019-09-27 23:43:08 +00:00
|
|
|
for (i = 0; i < PCI_STD_NUM_BARS; i++) {
|
2016-02-16 21:56:23 +00:00
|
|
|
if (hpdev->probed_bar[i] & PCI_BASE_ADDRESS_SPACE_IO)
|
|
|
|
dev_err(&hbus->hdev->device,
|
|
|
|
"There's an I/O BAR in this list!\n");
|
|
|
|
|
|
|
|
if (hpdev->probed_bar[i] != 0) {
|
|
|
|
/*
|
|
|
|
* A probed BAR has all the upper bits set that
|
|
|
|
* can be changed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
bar_val = hpdev->probed_bar[i];
|
|
|
|
if (bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64)
|
|
|
|
bar_val |=
|
|
|
|
((u64)hpdev->probed_bar[++i] << 32);
|
|
|
|
else
|
|
|
|
bar_val |= 0xffffffff00000000ULL;
|
|
|
|
|
|
|
|
bar_size = get_bar_size(bar_val);
|
|
|
|
|
|
|
|
if (bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64)
|
|
|
|
hbus->high_mmio_space += bar_size;
|
|
|
|
else
|
|
|
|
hbus->low_mmio_space += bar_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_unlock_irqrestore(&hbus->device_list_lock, flags);
|
|
|
|
complete(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* prepopulate_bars() - Fill in BARs with defaults
|
|
|
|
* @hbus: Root PCI bus, as understood by this driver
|
|
|
|
*
|
|
|
|
* The core PCI driver code seems much, much happier if the BARs
|
|
|
|
* for a device have values upon first scan. So fill them in.
|
|
|
|
* The algorithm below works down from large sizes to small,
|
|
|
|
* attempting to pack the assignments optimally. The assumption,
|
|
|
|
* enforced in other parts of the code, is that the beginning of
|
|
|
|
* the memory-mapped I/O space will be aligned on the largest
|
|
|
|
* BAR size.
|
|
|
|
*/
|
|
|
|
static void prepopulate_bars(struct hv_pcibus_device *hbus)
|
|
|
|
{
|
|
|
|
resource_size_t high_size = 0;
|
|
|
|
resource_size_t low_size = 0;
|
|
|
|
resource_size_t high_base = 0;
|
|
|
|
resource_size_t low_base = 0;
|
|
|
|
resource_size_t bar_size;
|
|
|
|
struct hv_pci_dev *hpdev;
|
|
|
|
unsigned long flags;
|
|
|
|
u64 bar_val;
|
|
|
|
u32 command;
|
|
|
|
bool high;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (hbus->low_mmio_space) {
|
|
|
|
low_size = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space));
|
|
|
|
low_base = hbus->low_mmio_res->start;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hbus->high_mmio_space) {
|
|
|
|
high_size = 1ULL <<
|
|
|
|
(63 - __builtin_clzll(hbus->high_mmio_space));
|
|
|
|
high_base = hbus->high_mmio_res->start;
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_lock_irqsave(&hbus->device_list_lock, flags);
|
|
|
|
|
PCI: hv: Add hibernation support
Add suspend() and resume() functions so that Hyper-V virtual PCI devices
are handled properly when the VM hibernates and resumes from
hibernation.
Note that the suspend() function must make sure there are no pending
work items before calling vmbus_close(), since it runs in a process
context as a callback in dpm_suspend(). When it starts to run, the
channel callback hv_pci_onchannelcallback(), which runs in a tasklet
context, can be still running concurrently and scheduling new work items
onto hbus->wq in hv_pci_devices_present() and hv_pci_eject_device(), and
the work item handlers can access the vmbus channel, which can be being
closed by hv_pci_suspend(), e.g. the work item handler
pci_devices_present_work() -> new_pcichild_device() writes to the vmbus
channel.
To eliminate the race, hv_pci_suspend() disables the channel callback
tasklet, sets hbus->state to hv_pcibus_removing, and re-enables the
tasklet. This way, when hv_pci_suspend() proceeds, it knows that no new
work item can be scheduled, and then it flushes hbus->wq and safely
closes the vmbus channel.
Signed-off-by: Dexuan Cui <decui@microsoft.com>
Signed-off-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Reviewed-by: Michael Kelley <mikelley@microsoft.com>
2019-11-25 05:33:52 +00:00
|
|
|
/*
|
|
|
|
* Clear the memory enable bit, in case it's already set. This occurs
|
|
|
|
* in the suspend path of hibernation, where the device is suspended,
|
|
|
|
* resumed and suspended again: see hibernation_snapshot() and
|
|
|
|
* hibernation_platform_enter().
|
|
|
|
*
|
2021-01-26 21:38:55 +00:00
|
|
|
* If the memory enable bit is already set, Hyper-V silently ignores
|
PCI: hv: Add hibernation support
Add suspend() and resume() functions so that Hyper-V virtual PCI devices
are handled properly when the VM hibernates and resumes from
hibernation.
Note that the suspend() function must make sure there are no pending
work items before calling vmbus_close(), since it runs in a process
context as a callback in dpm_suspend(). When it starts to run, the
channel callback hv_pci_onchannelcallback(), which runs in a tasklet
context, can be still running concurrently and scheduling new work items
onto hbus->wq in hv_pci_devices_present() and hv_pci_eject_device(), and
the work item handlers can access the vmbus channel, which can be being
closed by hv_pci_suspend(), e.g. the work item handler
pci_devices_present_work() -> new_pcichild_device() writes to the vmbus
channel.
To eliminate the race, hv_pci_suspend() disables the channel callback
tasklet, sets hbus->state to hv_pcibus_removing, and re-enables the
tasklet. This way, when hv_pci_suspend() proceeds, it knows that no new
work item can be scheduled, and then it flushes hbus->wq and safely
closes the vmbus channel.
Signed-off-by: Dexuan Cui <decui@microsoft.com>
Signed-off-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Reviewed-by: Michael Kelley <mikelley@microsoft.com>
2019-11-25 05:33:52 +00:00
|
|
|
* the below BAR updates, and the related PCI device driver can not
|
|
|
|
* work, because reading from the device register(s) always returns
|
2021-11-18 14:03:34 +00:00
|
|
|
* 0xFFFFFFFF (PCI_ERROR_RESPONSE).
|
PCI: hv: Add hibernation support
Add suspend() and resume() functions so that Hyper-V virtual PCI devices
are handled properly when the VM hibernates and resumes from
hibernation.
Note that the suspend() function must make sure there are no pending
work items before calling vmbus_close(), since it runs in a process
context as a callback in dpm_suspend(). When it starts to run, the
channel callback hv_pci_onchannelcallback(), which runs in a tasklet
context, can be still running concurrently and scheduling new work items
onto hbus->wq in hv_pci_devices_present() and hv_pci_eject_device(), and
the work item handlers can access the vmbus channel, which can be being
closed by hv_pci_suspend(), e.g. the work item handler
pci_devices_present_work() -> new_pcichild_device() writes to the vmbus
channel.
To eliminate the race, hv_pci_suspend() disables the channel callback
tasklet, sets hbus->state to hv_pcibus_removing, and re-enables the
tasklet. This way, when hv_pci_suspend() proceeds, it knows that no new
work item can be scheduled, and then it flushes hbus->wq and safely
closes the vmbus channel.
Signed-off-by: Dexuan Cui <decui@microsoft.com>
Signed-off-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Reviewed-by: Michael Kelley <mikelley@microsoft.com>
2019-11-25 05:33:52 +00:00
|
|
|
*/
|
|
|
|
list_for_each_entry(hpdev, &hbus->children, list_entry) {
|
|
|
|
_hv_pcifront_read_config(hpdev, PCI_COMMAND, 2, &command);
|
|
|
|
command &= ~PCI_COMMAND_MEMORY;
|
|
|
|
_hv_pcifront_write_config(hpdev, PCI_COMMAND, 2, command);
|
|
|
|
}
|
|
|
|
|
2016-02-16 21:56:23 +00:00
|
|
|
/* Pick addresses for the BARs. */
|
|
|
|
do {
|
2018-05-23 17:11:14 +00:00
|
|
|
list_for_each_entry(hpdev, &hbus->children, list_entry) {
|
2019-09-27 23:43:08 +00:00
|
|
|
for (i = 0; i < PCI_STD_NUM_BARS; i++) {
|
2016-02-16 21:56:23 +00:00
|
|
|
bar_val = hpdev->probed_bar[i];
|
|
|
|
if (bar_val == 0)
|
|
|
|
continue;
|
|
|
|
high = bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64;
|
|
|
|
if (high) {
|
|
|
|
bar_val |=
|
|
|
|
((u64)hpdev->probed_bar[i + 1]
|
|
|
|
<< 32);
|
|
|
|
} else {
|
|
|
|
bar_val |= 0xffffffffULL << 32;
|
|
|
|
}
|
|
|
|
bar_size = get_bar_size(bar_val);
|
|
|
|
if (high) {
|
|
|
|
if (high_size != bar_size) {
|
|
|
|
i++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
_hv_pcifront_write_config(hpdev,
|
|
|
|
PCI_BASE_ADDRESS_0 + (4 * i),
|
|
|
|
4,
|
|
|
|
(u32)(high_base & 0xffffff00));
|
|
|
|
i++;
|
|
|
|
_hv_pcifront_write_config(hpdev,
|
|
|
|
PCI_BASE_ADDRESS_0 + (4 * i),
|
|
|
|
4, (u32)(high_base >> 32));
|
|
|
|
high_base += bar_size;
|
|
|
|
} else {
|
|
|
|
if (low_size != bar_size)
|
|
|
|
continue;
|
|
|
|
_hv_pcifront_write_config(hpdev,
|
|
|
|
PCI_BASE_ADDRESS_0 + (4 * i),
|
|
|
|
4,
|
|
|
|
(u32)(low_base & 0xffffff00));
|
|
|
|
low_base += bar_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (high_size <= 1 && low_size <= 1) {
|
2022-05-02 07:42:55 +00:00
|
|
|
/*
|
|
|
|
* No need to set the PCI_COMMAND_MEMORY bit as
|
|
|
|
* the core PCI driver doesn't require the bit
|
|
|
|
* to be pre-set. Actually here we intentionally
|
|
|
|
* keep the bit off so that the PCI BAR probing
|
|
|
|
* in the core PCI driver doesn't cause Hyper-V
|
|
|
|
* to unnecessarily unmap/map the virtual BARs
|
|
|
|
* from/to the physical BARs multiple times.
|
|
|
|
* This reduces the VM boot time significantly
|
|
|
|
* if the BAR sizes are huge.
|
|
|
|
*/
|
2016-02-16 21:56:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
high_size >>= 1;
|
|
|
|
low_size >>= 1;
|
|
|
|
} while (high_size || low_size);
|
|
|
|
|
|
|
|
spin_unlock_irqrestore(&hbus->device_list_lock, flags);
|
|
|
|
}
|
|
|
|
|
2018-09-14 19:54:56 +00:00
|
|
|
/*
|
|
|
|
* Assign entries in sysfs pci slot directory.
|
|
|
|
*
|
|
|
|
* Note that this function does not need to lock the children list
|
|
|
|
* because it is called from pci_devices_present_work which
|
|
|
|
* is serialized with hv_eject_device_work because they are on the
|
|
|
|
* same ordered workqueue. Therefore hbus->children list will not change
|
|
|
|
* even when pci_create_slot sleeps.
|
|
|
|
*/
|
|
|
|
static void hv_pci_assign_slots(struct hv_pcibus_device *hbus)
|
|
|
|
{
|
|
|
|
struct hv_pci_dev *hpdev;
|
|
|
|
char name[SLOT_NAME_SIZE];
|
|
|
|
int slot_nr;
|
|
|
|
|
|
|
|
list_for_each_entry(hpdev, &hbus->children, list_entry) {
|
|
|
|
if (hpdev->pci_slot)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
slot_nr = PCI_SLOT(wslot_to_devfn(hpdev->desc.win_slot.slot));
|
|
|
|
snprintf(name, SLOT_NAME_SIZE, "%u", hpdev->desc.ser);
|
2021-07-26 18:06:54 +00:00
|
|
|
hpdev->pci_slot = pci_create_slot(hbus->bridge->bus, slot_nr,
|
2018-09-14 19:54:56 +00:00
|
|
|
name, NULL);
|
2018-09-21 02:53:17 +00:00
|
|
|
if (IS_ERR(hpdev->pci_slot)) {
|
2018-09-14 19:54:56 +00:00
|
|
|
pr_warn("pci_create slot %s failed\n", name);
|
2018-09-21 02:53:17 +00:00
|
|
|
hpdev->pci_slot = NULL;
|
|
|
|
}
|
2018-09-14 19:54:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 21:34:48 +00:00
|
|
|
/*
|
|
|
|
* Remove entries in sysfs pci slot directory.
|
|
|
|
*/
|
|
|
|
static void hv_pci_remove_slots(struct hv_pcibus_device *hbus)
|
|
|
|
{
|
|
|
|
struct hv_pci_dev *hpdev;
|
|
|
|
|
|
|
|
list_for_each_entry(hpdev, &hbus->children, list_entry) {
|
|
|
|
if (!hpdev->pci_slot)
|
|
|
|
continue;
|
|
|
|
pci_destroy_slot(hpdev->pci_slot);
|
|
|
|
hpdev->pci_slot = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-26 05:06:08 +00:00
|
|
|
/*
|
|
|
|
* Set NUMA node for the devices on the bus
|
|
|
|
*/
|
|
|
|
static void hv_pci_assign_numa_node(struct hv_pcibus_device *hbus)
|
|
|
|
{
|
|
|
|
struct pci_dev *dev;
|
2021-07-26 18:06:54 +00:00
|
|
|
struct pci_bus *bus = hbus->bridge->bus;
|
2020-02-26 05:06:08 +00:00
|
|
|
struct hv_pci_dev *hv_dev;
|
|
|
|
|
|
|
|
list_for_each_entry(dev, &bus->devices, bus_list) {
|
|
|
|
hv_dev = get_pcichild_wslot(hbus, devfn_to_wslot(dev->devfn));
|
|
|
|
if (!hv_dev)
|
|
|
|
continue;
|
|
|
|
|
2022-01-27 01:43:34 +00:00
|
|
|
if (hv_dev->desc.flags & HV_PCI_DEVICE_FLAG_NUMA_AFFINITY &&
|
|
|
|
hv_dev->desc.virtual_numa_node < num_possible_nodes())
|
|
|
|
/*
|
|
|
|
* The kernel may boot with some NUMA nodes offline
|
|
|
|
* (e.g. in a KDUMP kernel) or with NUMA disabled via
|
|
|
|
* "numa=off". In those cases, adjust the host provided
|
|
|
|
* NUMA node to a valid NUMA node used by the kernel.
|
|
|
|
*/
|
|
|
|
set_dev_node(&dev->dev,
|
|
|
|
numa_map_to_online_node(
|
|
|
|
hv_dev->desc.virtual_numa_node));
|
2020-02-26 05:06:08 +00:00
|
|
|
|
|
|
|
put_pcichild(hv_dev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-16 21:56:23 +00:00
|
|
|
/**
|
|
|
|
* create_root_hv_pci_bus() - Expose a new root PCI bus
|
|
|
|
* @hbus: Root PCI bus, as understood by this driver
|
|
|
|
*
|
|
|
|
* Return: 0 on success, -errno on failure
|
|
|
|
*/
|
|
|
|
static int create_root_hv_pci_bus(struct hv_pcibus_device *hbus)
|
|
|
|
{
|
2021-07-26 18:06:54 +00:00
|
|
|
int error;
|
|
|
|
struct pci_host_bridge *bridge = hbus->bridge;
|
|
|
|
|
|
|
|
bridge->dev.parent = &hbus->hdev->device;
|
|
|
|
bridge->sysdata = &hbus->sysdata;
|
|
|
|
bridge->ops = &hv_pcifront_ops;
|
|
|
|
|
|
|
|
error = pci_scan_root_bus_bridge(bridge);
|
|
|
|
if (error)
|
|
|
|
return error;
|
2016-02-16 21:56:23 +00:00
|
|
|
|
2017-03-23 21:58:32 +00:00
|
|
|
pci_lock_rescan_remove();
|
2020-02-26 05:06:08 +00:00
|
|
|
hv_pci_assign_numa_node(hbus);
|
2021-07-26 18:06:54 +00:00
|
|
|
pci_bus_assign_resources(bridge->bus);
|
2018-09-14 19:54:56 +00:00
|
|
|
hv_pci_assign_slots(hbus);
|
2021-07-26 18:06:54 +00:00
|
|
|
pci_bus_add_devices(bridge->bus);
|
2017-03-23 21:58:32 +00:00
|
|
|
pci_unlock_rescan_remove();
|
2016-02-16 21:56:23 +00:00
|
|
|
hbus->state = hv_pcibus_installed;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct q_res_req_compl {
|
|
|
|
struct completion host_event;
|
|
|
|
struct hv_pci_dev *hpdev;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* q_resource_requirements() - Query Resource Requirements
|
|
|
|
* @context: The completion context.
|
|
|
|
* @resp: The response that came from the host.
|
|
|
|
* @resp_packet_size: The size in bytes of resp.
|
|
|
|
*
|
|
|
|
* This function is invoked on completion of a Query Resource
|
|
|
|
* Requirements packet.
|
|
|
|
*/
|
|
|
|
static void q_resource_requirements(void *context, struct pci_response *resp,
|
|
|
|
int resp_packet_size)
|
|
|
|
{
|
|
|
|
struct q_res_req_compl *completion = context;
|
|
|
|
struct pci_q_res_req_response *q_res_req =
|
|
|
|
(struct pci_q_res_req_response *)resp;
|
2022-05-11 22:32:06 +00:00
|
|
|
s32 status;
|
2016-02-16 21:56:23 +00:00
|
|
|
int i;
|
|
|
|
|
2022-05-11 22:32:06 +00:00
|
|
|
status = (resp_packet_size < sizeof(*q_res_req)) ? -1 : resp->status;
|
|
|
|
if (status < 0) {
|
2016-02-16 21:56:23 +00:00
|
|
|
dev_err(&completion->hpdev->hbus->hdev->device,
|
|
|
|
"query resource requirements failed: %x\n",
|
2022-05-11 22:32:06 +00:00
|
|
|
status);
|
2016-02-16 21:56:23 +00:00
|
|
|
} else {
|
2019-09-27 23:43:08 +00:00
|
|
|
for (i = 0; i < PCI_STD_NUM_BARS; i++) {
|
2016-02-16 21:56:23 +00:00
|
|
|
completion->hpdev->probed_bar[i] =
|
|
|
|
q_res_req->probed_bar[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
complete(&completion->host_event);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* new_pcichild_device() - Create a new child device
|
|
|
|
* @hbus: The internal struct tracking this root PCI bus.
|
|
|
|
* @desc: The information supplied so far from the host
|
|
|
|
* about the device.
|
|
|
|
*
|
|
|
|
* This function creates the tracking structure for a new child
|
|
|
|
* device and kicks off the process of figuring out what it is.
|
|
|
|
*
|
|
|
|
* Return: Pointer to the new tracking struct
|
|
|
|
*/
|
|
|
|
static struct hv_pci_dev *new_pcichild_device(struct hv_pcibus_device *hbus,
|
2020-02-26 05:06:07 +00:00
|
|
|
struct hv_pcidev_description *desc)
|
2016-02-16 21:56:23 +00:00
|
|
|
{
|
|
|
|
struct hv_pci_dev *hpdev;
|
|
|
|
struct pci_child_message *res_req;
|
|
|
|
struct q_res_req_compl comp_pkt;
|
2016-11-10 07:17:48 +00:00
|
|
|
struct {
|
|
|
|
struct pci_packet init_packet;
|
|
|
|
u8 buffer[sizeof(struct pci_child_message)];
|
2016-02-16 21:56:23 +00:00
|
|
|
} pkt;
|
|
|
|
unsigned long flags;
|
|
|
|
int ret;
|
|
|
|
|
2018-03-18 14:53:28 +00:00
|
|
|
hpdev = kzalloc(sizeof(*hpdev), GFP_KERNEL);
|
2016-02-16 21:56:23 +00:00
|
|
|
if (!hpdev)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
hpdev->hbus = hbus;
|
|
|
|
|
|
|
|
memset(&pkt, 0, sizeof(pkt));
|
|
|
|
init_completion(&comp_pkt.host_event);
|
|
|
|
comp_pkt.hpdev = hpdev;
|
|
|
|
pkt.init_packet.compl_ctxt = &comp_pkt;
|
|
|
|
pkt.init_packet.completion_func = q_resource_requirements;
|
|
|
|
res_req = (struct pci_child_message *)&pkt.init_packet.message;
|
2016-08-23 04:45:51 +00:00
|
|
|
res_req->message_type.type = PCI_QUERY_RESOURCE_REQUIREMENTS;
|
2016-02-16 21:56:23 +00:00
|
|
|
res_req->wslot.slot = desc->win_slot.slot;
|
|
|
|
|
|
|
|
ret = vmbus_sendpacket(hbus->hdev->channel, res_req,
|
|
|
|
sizeof(struct pci_child_message),
|
|
|
|
(unsigned long)&pkt.init_packet,
|
|
|
|
VM_PKT_DATA_INBAND,
|
|
|
|
VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
|
|
|
|
if (ret)
|
|
|
|
goto error;
|
|
|
|
|
2018-05-23 21:12:01 +00:00
|
|
|
if (wait_for_response(hbus->hdev, &comp_pkt.host_event))
|
|
|
|
goto error;
|
2016-02-16 21:56:23 +00:00
|
|
|
|
|
|
|
hpdev->desc = *desc;
|
2017-04-18 14:02:48 +00:00
|
|
|
refcount_set(&hpdev->refs, 1);
|
2018-05-23 17:11:12 +00:00
|
|
|
get_pcichild(hpdev);
|
2016-02-16 21:56:23 +00:00
|
|
|
spin_lock_irqsave(&hbus->device_list_lock, flags);
|
2017-02-13 18:10:11 +00:00
|
|
|
|
2016-02-16 21:56:23 +00:00
|
|
|
list_add_tail(&hpdev->list_entry, &hbus->children);
|
|
|
|
spin_unlock_irqrestore(&hbus->device_list_lock, flags);
|
|
|
|
return hpdev;
|
|
|
|
|
|
|
|
error:
|
|
|
|
kfree(hpdev);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get_pcichild_wslot() - Find device from slot
|
|
|
|
* @hbus: Root PCI bus, as understood by this driver
|
|
|
|
* @wslot: Location on the bus
|
|
|
|
*
|
|
|
|
* This function looks up a PCI device and returns the internal
|
|
|
|
* representation of it. It acquires a reference on it, so that
|
|
|
|
* the device won't be deleted while somebody is using it. The
|
|
|
|
* caller is responsible for calling put_pcichild() to release
|
|
|
|
* this reference.
|
|
|
|
*
|
|
|
|
* Return: Internal representation of a PCI device
|
|
|
|
*/
|
|
|
|
static struct hv_pci_dev *get_pcichild_wslot(struct hv_pcibus_device *hbus,
|
|
|
|
u32 wslot)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
struct hv_pci_dev *iter, *hpdev = NULL;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&hbus->device_list_lock, flags);
|
|
|
|
list_for_each_entry(iter, &hbus->children, list_entry) {
|
|
|
|
if (iter->desc.win_slot.slot == wslot) {
|
|
|
|
hpdev = iter;
|
2018-05-23 17:11:12 +00:00
|
|
|
get_pcichild(hpdev);
|
2016-02-16 21:56:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
spin_unlock_irqrestore(&hbus->device_list_lock, flags);
|
|
|
|
|
|
|
|
return hpdev;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* pci_devices_present_work() - Handle new list of child devices
|
|
|
|
* @work: Work struct embedded in struct hv_dr_work
|
|
|
|
*
|
|
|
|
* "Bus Relations" is the Windows term for "children of this
|
|
|
|
* bus." The terminology is preserved here for people trying to
|
|
|
|
* debug the interaction between Hyper-V and Linux. This
|
|
|
|
* function is called when the parent partition reports a list
|
|
|
|
* of functions that should be observed under this PCI Express
|
|
|
|
* port (bus).
|
|
|
|
*
|
|
|
|
* This function updates the list, and must tolerate being
|
|
|
|
* called multiple times with the same information. The typical
|
|
|
|
* number of child devices is one, with very atypical cases
|
|
|
|
* involving three or four, so the algorithms used here can be
|
|
|
|
* simple and inefficient.
|
|
|
|
*
|
|
|
|
* It must also treat the omission of a previously observed device as
|
|
|
|
* notification that the device no longer exists.
|
|
|
|
*
|
2018-03-15 14:20:53 +00:00
|
|
|
* Note that this function is serialized with hv_eject_device_work(),
|
|
|
|
* because both are pushed to the ordered workqueue hbus->wq.
|
2016-02-16 21:56:23 +00:00
|
|
|
*/
|
|
|
|
static void pci_devices_present_work(struct work_struct *work)
|
|
|
|
{
|
|
|
|
u32 child_no;
|
|
|
|
bool found;
|
2020-02-26 05:06:07 +00:00
|
|
|
struct hv_pcidev_description *new_desc;
|
2016-02-16 21:56:23 +00:00
|
|
|
struct hv_pci_dev *hpdev;
|
|
|
|
struct hv_pcibus_device *hbus;
|
|
|
|
struct list_head removed;
|
|
|
|
struct hv_dr_work *dr_wrk;
|
|
|
|
struct hv_dr_state *dr = NULL;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
dr_wrk = container_of(work, struct hv_dr_work, wrk);
|
|
|
|
hbus = dr_wrk->bus;
|
|
|
|
kfree(dr_wrk);
|
|
|
|
|
|
|
|
INIT_LIST_HEAD(&removed);
|
|
|
|
|
|
|
|
/* Pull this off the queue and process it if it was the last one. */
|
|
|
|
spin_lock_irqsave(&hbus->device_list_lock, flags);
|
|
|
|
while (!list_empty(&hbus->dr_list)) {
|
|
|
|
dr = list_first_entry(&hbus->dr_list, struct hv_dr_state,
|
|
|
|
list_entry);
|
|
|
|
list_del(&dr->list_entry);
|
|
|
|
|
|
|
|
/* Throw this away if the list still has stuff in it. */
|
|
|
|
if (!list_empty(&hbus->dr_list)) {
|
|
|
|
kfree(dr);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
spin_unlock_irqrestore(&hbus->device_list_lock, flags);
|
|
|
|
|
2021-05-12 08:06:49 +00:00
|
|
|
if (!dr)
|
2016-02-16 21:56:23 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* First, mark all existing children as reported missing. */
|
|
|
|
spin_lock_irqsave(&hbus->device_list_lock, flags);
|
2018-05-23 17:11:14 +00:00
|
|
|
list_for_each_entry(hpdev, &hbus->children, list_entry) {
|
|
|
|
hpdev->reported_missing = true;
|
2016-02-16 21:56:23 +00:00
|
|
|
}
|
|
|
|
spin_unlock_irqrestore(&hbus->device_list_lock, flags);
|
|
|
|
|
|
|
|
/* Next, add back any reported devices. */
|
|
|
|
for (child_no = 0; child_no < dr->device_count; child_no++) {
|
|
|
|
found = false;
|
|
|
|
new_desc = &dr->func[child_no];
|
|
|
|
|
|
|
|
spin_lock_irqsave(&hbus->device_list_lock, flags);
|
2018-05-23 17:11:14 +00:00
|
|
|
list_for_each_entry(hpdev, &hbus->children, list_entry) {
|
|
|
|
if ((hpdev->desc.win_slot.slot == new_desc->win_slot.slot) &&
|
2016-02-16 21:56:23 +00:00
|
|
|
(hpdev->desc.v_id == new_desc->v_id) &&
|
|
|
|
(hpdev->desc.d_id == new_desc->d_id) &&
|
|
|
|
(hpdev->desc.ser == new_desc->ser)) {
|
|
|
|
hpdev->reported_missing = false;
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
spin_unlock_irqrestore(&hbus->device_list_lock, flags);
|
|
|
|
|
|
|
|
if (!found) {
|
|
|
|
hpdev = new_pcichild_device(hbus, new_desc);
|
|
|
|
if (!hpdev)
|
|
|
|
dev_err(&hbus->hdev->device,
|
|
|
|
"couldn't record a child device.\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Move missing children to a list on the stack. */
|
|
|
|
spin_lock_irqsave(&hbus->device_list_lock, flags);
|
|
|
|
do {
|
|
|
|
found = false;
|
2018-05-23 17:11:14 +00:00
|
|
|
list_for_each_entry(hpdev, &hbus->children, list_entry) {
|
2016-02-16 21:56:23 +00:00
|
|
|
if (hpdev->reported_missing) {
|
|
|
|
found = true;
|
2018-05-23 17:11:12 +00:00
|
|
|
put_pcichild(hpdev);
|
2016-07-28 16:16:48 +00:00
|
|
|
list_move_tail(&hpdev->list_entry, &removed);
|
2016-02-16 21:56:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (found);
|
|
|
|
spin_unlock_irqrestore(&hbus->device_list_lock, flags);
|
|
|
|
|
|
|
|
/* Delete everything that should no longer exist. */
|
|
|
|
while (!list_empty(&removed)) {
|
|
|
|
hpdev = list_first_entry(&removed, struct hv_pci_dev,
|
|
|
|
list_entry);
|
|
|
|
list_del(&hpdev->list_entry);
|
PCI: hv: Add pci_destroy_slot() in pci_devices_present_work(), if necessary
When we hot-remove a device, usually the host sends us a PCI_EJECT message,
and a PCI_BUS_RELATIONS message with bus_rel->device_count == 0.
When we execute the quick hot-add/hot-remove test, the host may not send
us the PCI_EJECT message if the guest has not fully finished the
initialization by sending the PCI_RESOURCES_ASSIGNED* message to the
host, so it's potentially unsafe to only depend on the
pci_destroy_slot() in hv_eject_device_work() because the code path
create_root_hv_pci_bus()
-> hv_pci_assign_slots()
is not called in this case. Note: in this case, the host still sends the
guest a PCI_BUS_RELATIONS message with bus_rel->device_count == 0.
In the quick hot-add/hot-remove test, we can have such a race before
the code path
pci_devices_present_work()
-> new_pcichild_device()
adds the new device into the hbus->children list, we may have already
received the PCI_EJECT message, and since the tasklet handler
hv_pci_onchannelcallback()
may fail to find the "hpdev" by calling
get_pcichild_wslot(hbus, dev_message->wslot.slot)
hv_pci_eject_device() is not called; Later, by continuing execution
create_root_hv_pci_bus()
-> hv_pci_assign_slots()
creates the slot and the PCI_BUS_RELATIONS message with
bus_rel->device_count == 0 removes the device from hbus->children, and
we end up being unable to remove the slot in
hv_pci_remove()
-> hv_pci_remove_slots()
Remove the slot in pci_devices_present_work() when the device
is removed to address this race.
pci_devices_present_work() and hv_eject_device_work() run in the
singled-threaded hbus->wq, so there is not a double-remove issue for the
slot.
We cannot offload hv_pci_eject_device() from hv_pci_onchannelcallback()
to the workqueue, because we need the hv_pci_onchannelcallback()
synchronously call hv_pci_eject_device() to poll the channel
ringbuffer to work around the "hangs in hv_compose_msi_msg()" issue
fixed in commit de0aa7b2f97d ("PCI: hv: Fix 2 hang issues in
hv_compose_msi_msg()")
Fixes: a15f2c08c708 ("PCI: hv: support reporting serial number as slot information")
Signed-off-by: Dexuan Cui <decui@microsoft.com>
[lorenzo.pieralisi@arm.com: rewritten commit log]
Signed-off-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Reviewed-by: Stephen Hemminger <stephen@networkplumber.org>
Reviewed-by: Michael Kelley <mikelley@microsoft.com>
Cc: stable@vger.kernel.org
2019-03-04 21:34:49 +00:00
|
|
|
|
|
|
|
if (hpdev->pci_slot)
|
|
|
|
pci_destroy_slot(hpdev->pci_slot);
|
|
|
|
|
2018-05-23 17:11:12 +00:00
|
|
|
put_pcichild(hpdev);
|
2016-02-16 21:56:23 +00:00
|
|
|
}
|
|
|
|
|
2017-05-24 20:41:24 +00:00
|
|
|
switch (hbus->state) {
|
2017-03-23 21:58:10 +00:00
|
|
|
case hv_pcibus_installed:
|
|
|
|
/*
|
2017-05-24 20:41:24 +00:00
|
|
|
* Tell the core to rescan bus
|
|
|
|
* because there may have been changes.
|
|
|
|
*/
|
2016-02-16 21:56:23 +00:00
|
|
|
pci_lock_rescan_remove();
|
2021-07-26 18:06:54 +00:00
|
|
|
pci_scan_child_bus(hbus->bridge->bus);
|
2020-02-26 05:06:08 +00:00
|
|
|
hv_pci_assign_numa_node(hbus);
|
2018-09-14 19:54:56 +00:00
|
|
|
hv_pci_assign_slots(hbus);
|
2016-02-16 21:56:23 +00:00
|
|
|
pci_unlock_rescan_remove();
|
2017-03-23 21:58:10 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case hv_pcibus_init:
|
|
|
|
case hv_pcibus_probed:
|
2016-02-16 21:56:23 +00:00
|
|
|
survey_child_resources(hbus);
|
2017-03-23 21:58:10 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2016-02-16 21:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
kfree(dr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-02-26 05:06:07 +00:00
|
|
|
* hv_pci_start_relations_work() - Queue work to start device discovery
|
2016-02-16 21:56:23 +00:00
|
|
|
* @hbus: Root PCI bus, as understood by this driver
|
2020-02-26 05:06:07 +00:00
|
|
|
* @dr: The list of children returned from host
|
2016-02-16 21:56:23 +00:00
|
|
|
*
|
2020-02-26 05:06:07 +00:00
|
|
|
* Return: 0 on success, -errno on failure
|
2016-02-16 21:56:23 +00:00
|
|
|
*/
|
2020-02-26 05:06:07 +00:00
|
|
|
static int hv_pci_start_relations_work(struct hv_pcibus_device *hbus,
|
|
|
|
struct hv_dr_state *dr)
|
2016-02-16 21:56:23 +00:00
|
|
|
{
|
|
|
|
struct hv_dr_work *dr_wrk;
|
|
|
|
unsigned long flags;
|
2018-03-15 14:22:00 +00:00
|
|
|
bool pending_dr;
|
2016-02-16 21:56:23 +00:00
|
|
|
|
PCI: hv: Add hibernation support
Add suspend() and resume() functions so that Hyper-V virtual PCI devices
are handled properly when the VM hibernates and resumes from
hibernation.
Note that the suspend() function must make sure there are no pending
work items before calling vmbus_close(), since it runs in a process
context as a callback in dpm_suspend(). When it starts to run, the
channel callback hv_pci_onchannelcallback(), which runs in a tasklet
context, can be still running concurrently and scheduling new work items
onto hbus->wq in hv_pci_devices_present() and hv_pci_eject_device(), and
the work item handlers can access the vmbus channel, which can be being
closed by hv_pci_suspend(), e.g. the work item handler
pci_devices_present_work() -> new_pcichild_device() writes to the vmbus
channel.
To eliminate the race, hv_pci_suspend() disables the channel callback
tasklet, sets hbus->state to hv_pcibus_removing, and re-enables the
tasklet. This way, when hv_pci_suspend() proceeds, it knows that no new
work item can be scheduled, and then it flushes hbus->wq and safely
closes the vmbus channel.
Signed-off-by: Dexuan Cui <decui@microsoft.com>
Signed-off-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Reviewed-by: Michael Kelley <mikelley@microsoft.com>
2019-11-25 05:33:52 +00:00
|
|
|
if (hbus->state == hv_pcibus_removing) {
|
|
|
|
dev_info(&hbus->hdev->device,
|
|
|
|
"PCI VMBus BUS_RELATIONS: ignored\n");
|
2020-02-26 05:06:07 +00:00
|
|
|
return -ENOENT;
|
PCI: hv: Add hibernation support
Add suspend() and resume() functions so that Hyper-V virtual PCI devices
are handled properly when the VM hibernates and resumes from
hibernation.
Note that the suspend() function must make sure there are no pending
work items before calling vmbus_close(), since it runs in a process
context as a callback in dpm_suspend(). When it starts to run, the
channel callback hv_pci_onchannelcallback(), which runs in a tasklet
context, can be still running concurrently and scheduling new work items
onto hbus->wq in hv_pci_devices_present() and hv_pci_eject_device(), and
the work item handlers can access the vmbus channel, which can be being
closed by hv_pci_suspend(), e.g. the work item handler
pci_devices_present_work() -> new_pcichild_device() writes to the vmbus
channel.
To eliminate the race, hv_pci_suspend() disables the channel callback
tasklet, sets hbus->state to hv_pcibus_removing, and re-enables the
tasklet. This way, when hv_pci_suspend() proceeds, it knows that no new
work item can be scheduled, and then it flushes hbus->wq and safely
closes the vmbus channel.
Signed-off-by: Dexuan Cui <decui@microsoft.com>
Signed-off-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Reviewed-by: Michael Kelley <mikelley@microsoft.com>
2019-11-25 05:33:52 +00:00
|
|
|
}
|
|
|
|
|
2016-02-16 21:56:23 +00:00
|
|
|
dr_wrk = kzalloc(sizeof(*dr_wrk), GFP_NOWAIT);
|
|
|
|
if (!dr_wrk)
|
2020-02-26 05:06:07 +00:00
|
|
|
return -ENOMEM;
|
2016-02-16 21:56:23 +00:00
|
|
|
|
|
|
|
INIT_WORK(&dr_wrk->wrk, pci_devices_present_work);
|
|
|
|
dr_wrk->bus = hbus;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&hbus->device_list_lock, flags);
|
2018-03-15 14:22:00 +00:00
|
|
|
/*
|
|
|
|
* If pending_dr is true, we have already queued a work,
|
|
|
|
* which will see the new dr. Otherwise, we need to
|
|
|
|
* queue a new work.
|
|
|
|
*/
|
|
|
|
pending_dr = !list_empty(&hbus->dr_list);
|
2016-02-16 21:56:23 +00:00
|
|
|
list_add_tail(&dr->list_entry, &hbus->dr_list);
|
|
|
|
spin_unlock_irqrestore(&hbus->device_list_lock, flags);
|
|
|
|
|
2021-05-12 08:06:49 +00:00
|
|
|
if (pending_dr)
|
2018-03-15 14:22:00 +00:00
|
|
|
kfree(dr_wrk);
|
2021-05-12 08:06:49 +00:00
|
|
|
else
|
2018-03-15 14:22:00 +00:00
|
|
|
queue_work(hbus->wq, &dr_wrk->wrk);
|
2020-02-26 05:06:07 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* hv_pci_devices_present() - Handle list of new children
|
|
|
|
* @hbus: Root PCI bus, as understood by this driver
|
|
|
|
* @relations: Packet from host listing children
|
|
|
|
*
|
|
|
|
* Process a new list of devices on the bus. The list of devices is
|
|
|
|
* discovered by VSP and sent to us via VSP message PCI_BUS_RELATIONS,
|
|
|
|
* whenever a new list of devices for this bus appears.
|
|
|
|
*/
|
|
|
|
static void hv_pci_devices_present(struct hv_pcibus_device *hbus,
|
|
|
|
struct pci_bus_relations *relations)
|
|
|
|
{
|
|
|
|
struct hv_dr_state *dr;
|
|
|
|
int i;
|
|
|
|
|
2020-05-25 16:43:19 +00:00
|
|
|
dr = kzalloc(struct_size(dr, func, relations->device_count),
|
|
|
|
GFP_NOWAIT);
|
2020-02-26 05:06:07 +00:00
|
|
|
if (!dr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
dr->device_count = relations->device_count;
|
|
|
|
for (i = 0; i < dr->device_count; i++) {
|
|
|
|
dr->func[i].v_id = relations->func[i].v_id;
|
|
|
|
dr->func[i].d_id = relations->func[i].d_id;
|
|
|
|
dr->func[i].rev = relations->func[i].rev;
|
|
|
|
dr->func[i].prog_intf = relations->func[i].prog_intf;
|
|
|
|
dr->func[i].subclass = relations->func[i].subclass;
|
|
|
|
dr->func[i].base_class = relations->func[i].base_class;
|
|
|
|
dr->func[i].subsystem_id = relations->func[i].subsystem_id;
|
|
|
|
dr->func[i].win_slot = relations->func[i].win_slot;
|
|
|
|
dr->func[i].ser = relations->func[i].ser;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hv_pci_start_relations_work(hbus, dr))
|
|
|
|
kfree(dr);
|
2016-02-16 21:56:23 +00:00
|
|
|
}
|
|
|
|
|
2020-02-26 05:06:08 +00:00
|
|
|
/**
|
|
|
|
* hv_pci_devices_present2() - Handle list of new children
|
|
|
|
* @hbus: Root PCI bus, as understood by this driver
|
|
|
|
* @relations: Packet from host listing children
|
|
|
|
*
|
|
|
|
* This function is the v2 version of hv_pci_devices_present()
|
|
|
|
*/
|
|
|
|
static void hv_pci_devices_present2(struct hv_pcibus_device *hbus,
|
|
|
|
struct pci_bus_relations2 *relations)
|
|
|
|
{
|
|
|
|
struct hv_dr_state *dr;
|
|
|
|
int i;
|
|
|
|
|
2020-05-25 16:43:19 +00:00
|
|
|
dr = kzalloc(struct_size(dr, func, relations->device_count),
|
|
|
|
GFP_NOWAIT);
|
2020-02-26 05:06:08 +00:00
|
|
|
if (!dr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
dr->device_count = relations->device_count;
|
|
|
|
for (i = 0; i < dr->device_count; i++) {
|
|
|
|
dr->func[i].v_id = relations->func[i].v_id;
|
|
|
|
dr->func[i].d_id = relations->func[i].d_id;
|
|
|
|
dr->func[i].rev = relations->func[i].rev;
|
|
|
|
dr->func[i].prog_intf = relations->func[i].prog_intf;
|
|
|
|
dr->func[i].subclass = relations->func[i].subclass;
|
|
|
|
dr->func[i].base_class = relations->func[i].base_class;
|
|
|
|
dr->func[i].subsystem_id = relations->func[i].subsystem_id;
|
|
|
|
dr->func[i].win_slot = relations->func[i].win_slot;
|
|
|
|
dr->func[i].ser = relations->func[i].ser;
|
|
|
|
dr->func[i].flags = relations->func[i].flags;
|
|
|
|
dr->func[i].virtual_numa_node =
|
|
|
|
relations->func[i].virtual_numa_node;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hv_pci_start_relations_work(hbus, dr))
|
|
|
|
kfree(dr);
|
|
|
|
}
|
|
|
|
|
2016-02-16 21:56:23 +00:00
|
|
|
/**
|
|
|
|
* hv_eject_device_work() - Asynchronously handles ejection
|
|
|
|
* @work: Work struct embedded in internal device struct
|
|
|
|
*
|
|
|
|
* This function handles ejecting a device. Windows will
|
|
|
|
* attempt to gracefully eject a device, waiting 60 seconds to
|
|
|
|
* hear back from the guest OS that this completed successfully.
|
|
|
|
* If this timer expires, the device will be forcibly removed.
|
|
|
|
*/
|
|
|
|
static void hv_eject_device_work(struct work_struct *work)
|
|
|
|
{
|
|
|
|
struct pci_eject_response *ejct_pkt;
|
2019-06-21 23:45:23 +00:00
|
|
|
struct hv_pcibus_device *hbus;
|
2016-02-16 21:56:23 +00:00
|
|
|
struct hv_pci_dev *hpdev;
|
|
|
|
struct pci_dev *pdev;
|
|
|
|
unsigned long flags;
|
|
|
|
int wslot;
|
|
|
|
struct {
|
|
|
|
struct pci_packet pkt;
|
2016-08-23 04:45:51 +00:00
|
|
|
u8 buffer[sizeof(struct pci_eject_response)];
|
2016-02-16 21:56:23 +00:00
|
|
|
} ctxt;
|
|
|
|
|
|
|
|
hpdev = container_of(work, struct hv_pci_dev, wrk);
|
2019-06-21 23:45:23 +00:00
|
|
|
hbus = hpdev->hbus;
|
2016-02-16 21:56:23 +00:00
|
|
|
|
2018-03-15 14:21:43 +00:00
|
|
|
WARN_ON(hpdev->state != hv_pcichild_ejecting);
|
2016-02-16 21:56:23 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Ejection can come before or after the PCI bus has been set up, so
|
|
|
|
* attempt to find it and tear down the bus state, if it exists. This
|
2021-07-26 18:06:54 +00:00
|
|
|
* must be done without constructs like pci_domain_nr(hbus->bridge->bus)
|
|
|
|
* because hbus->bridge->bus may not exist yet.
|
2016-02-16 21:56:23 +00:00
|
|
|
*/
|
|
|
|
wslot = wslot_to_devfn(hpdev->desc.win_slot.slot);
|
2021-07-26 18:06:55 +00:00
|
|
|
pdev = pci_get_domain_bus_and_slot(hbus->bridge->domain_nr, 0, wslot);
|
2016-02-16 21:56:23 +00:00
|
|
|
if (pdev) {
|
2017-03-23 21:58:32 +00:00
|
|
|
pci_lock_rescan_remove();
|
2016-02-16 21:56:23 +00:00
|
|
|
pci_stop_and_remove_bus_device(pdev);
|
|
|
|
pci_dev_put(pdev);
|
2017-03-23 21:58:32 +00:00
|
|
|
pci_unlock_rescan_remove();
|
2016-02-16 21:56:23 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 23:45:23 +00:00
|
|
|
spin_lock_irqsave(&hbus->device_list_lock, flags);
|
2016-11-10 07:19:52 +00:00
|
|
|
list_del(&hpdev->list_entry);
|
2019-06-21 23:45:23 +00:00
|
|
|
spin_unlock_irqrestore(&hbus->device_list_lock, flags);
|
2016-11-10 07:19:52 +00:00
|
|
|
|
2018-09-14 19:54:56 +00:00
|
|
|
if (hpdev->pci_slot)
|
|
|
|
pci_destroy_slot(hpdev->pci_slot);
|
|
|
|
|
2016-02-16 21:56:23 +00:00
|
|
|
memset(&ctxt, 0, sizeof(ctxt));
|
|
|
|
ejct_pkt = (struct pci_eject_response *)&ctxt.pkt.message;
|
2016-08-23 04:45:51 +00:00
|
|
|
ejct_pkt->message_type.type = PCI_EJECTION_COMPLETE;
|
2016-02-16 21:56:23 +00:00
|
|
|
ejct_pkt->wslot.slot = hpdev->desc.win_slot.slot;
|
2019-06-21 23:45:23 +00:00
|
|
|
vmbus_sendpacket(hbus->hdev->channel, ejct_pkt,
|
2022-04-19 12:23:21 +00:00
|
|
|
sizeof(*ejct_pkt), 0,
|
2016-02-16 21:56:23 +00:00
|
|
|
VM_PKT_DATA_INBAND, 0);
|
|
|
|
|
2019-03-04 21:34:48 +00:00
|
|
|
/* For the get_pcichild() in hv_pci_eject_device() */
|
|
|
|
put_pcichild(hpdev);
|
|
|
|
/* For the two refs got in new_pcichild_device() */
|
2018-05-23 17:11:12 +00:00
|
|
|
put_pcichild(hpdev);
|
|
|
|
put_pcichild(hpdev);
|
2019-06-21 23:45:23 +00:00
|
|
|
/* hpdev has been freed. Do not use it any more. */
|
2016-02-16 21:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* hv_pci_eject_device() - Handles device ejection
|
|
|
|
* @hpdev: Internal device tracking struct
|
|
|
|
*
|
|
|
|
* This function is invoked when an ejection packet arrives. It
|
|
|
|
* just schedules work so that we don't re-enter the packet
|
|
|
|
* delivery code handling the ejection.
|
|
|
|
*/
|
|
|
|
static void hv_pci_eject_device(struct hv_pci_dev *hpdev)
|
|
|
|
{
|
PCI: hv: Add hibernation support
Add suspend() and resume() functions so that Hyper-V virtual PCI devices
are handled properly when the VM hibernates and resumes from
hibernation.
Note that the suspend() function must make sure there are no pending
work items before calling vmbus_close(), since it runs in a process
context as a callback in dpm_suspend(). When it starts to run, the
channel callback hv_pci_onchannelcallback(), which runs in a tasklet
context, can be still running concurrently and scheduling new work items
onto hbus->wq in hv_pci_devices_present() and hv_pci_eject_device(), and
the work item handlers can access the vmbus channel, which can be being
closed by hv_pci_suspend(), e.g. the work item handler
pci_devices_present_work() -> new_pcichild_device() writes to the vmbus
channel.
To eliminate the race, hv_pci_suspend() disables the channel callback
tasklet, sets hbus->state to hv_pcibus_removing, and re-enables the
tasklet. This way, when hv_pci_suspend() proceeds, it knows that no new
work item can be scheduled, and then it flushes hbus->wq and safely
closes the vmbus channel.
Signed-off-by: Dexuan Cui <decui@microsoft.com>
Signed-off-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Reviewed-by: Michael Kelley <mikelley@microsoft.com>
2019-11-25 05:33:52 +00:00
|
|
|
struct hv_pcibus_device *hbus = hpdev->hbus;
|
|
|
|
struct hv_device *hdev = hbus->hdev;
|
|
|
|
|
|
|
|
if (hbus->state == hv_pcibus_removing) {
|
|
|
|
dev_info(&hdev->device, "PCI VMBus EJECT: ignored\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-16 21:56:23 +00:00
|
|
|
hpdev->state = hv_pcichild_ejecting;
|
2018-05-23 17:11:12 +00:00
|
|
|
get_pcichild(hpdev);
|
2016-02-16 21:56:23 +00:00
|
|
|
INIT_WORK(&hpdev->wrk, hv_eject_device_work);
|
PCI: hv: Add hibernation support
Add suspend() and resume() functions so that Hyper-V virtual PCI devices
are handled properly when the VM hibernates and resumes from
hibernation.
Note that the suspend() function must make sure there are no pending
work items before calling vmbus_close(), since it runs in a process
context as a callback in dpm_suspend(). When it starts to run, the
channel callback hv_pci_onchannelcallback(), which runs in a tasklet
context, can be still running concurrently and scheduling new work items
onto hbus->wq in hv_pci_devices_present() and hv_pci_eject_device(), and
the work item handlers can access the vmbus channel, which can be being
closed by hv_pci_suspend(), e.g. the work item handler
pci_devices_present_work() -> new_pcichild_device() writes to the vmbus
channel.
To eliminate the race, hv_pci_suspend() disables the channel callback
tasklet, sets hbus->state to hv_pcibus_removing, and re-enables the
tasklet. This way, when hv_pci_suspend() proceeds, it knows that no new
work item can be scheduled, and then it flushes hbus->wq and safely
closes the vmbus channel.
Signed-off-by: Dexuan Cui <decui@microsoft.com>
Signed-off-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Reviewed-by: Michael Kelley <mikelley@microsoft.com>
2019-11-25 05:33:52 +00:00
|
|
|
queue_work(hbus->wq, &hpdev->wrk);
|
2016-02-16 21:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* hv_pci_onchannelcallback() - Handles incoming packets
|
|
|
|
* @context: Internal bus tracking struct
|
|
|
|
*
|
|
|
|
* This function is invoked whenever the host sends a packet to
|
|
|
|
* this channel (which is private to this root PCI bus).
|
|
|
|
*/
|
|
|
|
static void hv_pci_onchannelcallback(void *context)
|
|
|
|
{
|
|
|
|
const int packet_size = 0x100;
|
|
|
|
int ret;
|
|
|
|
struct hv_pcibus_device *hbus = context;
|
2022-04-19 12:23:21 +00:00
|
|
|
struct vmbus_channel *chan = hbus->hdev->channel;
|
2016-02-16 21:56:23 +00:00
|
|
|
u32 bytes_recvd;
|
2022-04-19 12:23:21 +00:00
|
|
|
u64 req_id, req_addr;
|
2016-02-16 21:56:23 +00:00
|
|
|
struct vmpacket_descriptor *desc;
|
|
|
|
unsigned char *buffer;
|
|
|
|
int bufferlen = packet_size;
|
|
|
|
struct pci_packet *comp_packet;
|
|
|
|
struct pci_response *response;
|
|
|
|
struct pci_incoming_message *new_message;
|
|
|
|
struct pci_bus_relations *bus_rel;
|
2020-02-26 05:06:08 +00:00
|
|
|
struct pci_bus_relations2 *bus_rel2;
|
2019-08-22 05:05:37 +00:00
|
|
|
struct pci_dev_inval_block *inval;
|
2016-02-16 21:56:23 +00:00
|
|
|
struct pci_dev_incoming *dev_message;
|
|
|
|
struct hv_pci_dev *hpdev;
|
2022-04-19 12:23:25 +00:00
|
|
|
unsigned long flags;
|
2016-02-16 21:56:23 +00:00
|
|
|
|
|
|
|
buffer = kmalloc(bufferlen, GFP_ATOMIC);
|
|
|
|
if (!buffer)
|
|
|
|
return;
|
|
|
|
|
|
|
|
while (1) {
|
2022-04-19 12:23:21 +00:00
|
|
|
ret = vmbus_recvpacket_raw(chan, buffer, bufferlen,
|
|
|
|
&bytes_recvd, &req_id);
|
2016-02-16 21:56:23 +00:00
|
|
|
|
|
|
|
if (ret == -ENOBUFS) {
|
|
|
|
kfree(buffer);
|
|
|
|
/* Handle large packet */
|
|
|
|
bufferlen = bytes_recvd;
|
|
|
|
buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
|
|
|
|
if (!buffer)
|
|
|
|
return;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-06-17 17:45:30 +00:00
|
|
|
/* Zero length indicates there are no more packets. */
|
|
|
|
if (ret || !bytes_recvd)
|
|
|
|
break;
|
|
|
|
|
2016-02-16 21:56:23 +00:00
|
|
|
/*
|
|
|
|
* All incoming packets must be at least as large as a
|
|
|
|
* response.
|
|
|
|
*/
|
2016-05-30 14:17:58 +00:00
|
|
|
if (bytes_recvd <= sizeof(struct pci_response))
|
2016-06-17 17:45:30 +00:00
|
|
|
continue;
|
2016-02-16 21:56:23 +00:00
|
|
|
desc = (struct vmpacket_descriptor *)buffer;
|
|
|
|
|
|
|
|
switch (desc->type) {
|
|
|
|
case VM_PKT_COMP:
|
|
|
|
|
2022-04-19 12:23:25 +00:00
|
|
|
lock_requestor(chan, flags);
|
|
|
|
req_addr = __vmbus_request_addr_match(chan, req_id,
|
|
|
|
VMBUS_RQST_ADDR_ANY);
|
2022-04-19 12:23:21 +00:00
|
|
|
if (req_addr == VMBUS_RQST_ERROR) {
|
2022-04-19 12:23:25 +00:00
|
|
|
unlock_requestor(chan, flags);
|
2022-04-19 12:23:21 +00:00
|
|
|
dev_err(&hbus->hdev->device,
|
|
|
|
"Invalid transaction ID %llx\n",
|
|
|
|
req_id);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
comp_packet = (struct pci_packet *)req_addr;
|
2016-02-16 21:56:23 +00:00
|
|
|
response = (struct pci_response *)buffer;
|
2022-04-19 12:23:25 +00:00
|
|
|
/*
|
|
|
|
* Call ->completion_func() within the critical section to make
|
|
|
|
* sure that the packet pointer is still valid during the call:
|
|
|
|
* here 'valid' means that there's a task still waiting for the
|
|
|
|
* completion, and that the packet data is still on the waiting
|
|
|
|
* task's stack. Cf. hv_compose_msi_msg().
|
|
|
|
*/
|
2016-02-16 21:56:23 +00:00
|
|
|
comp_packet->completion_func(comp_packet->compl_ctxt,
|
|
|
|
response,
|
|
|
|
bytes_recvd);
|
2022-04-19 12:23:25 +00:00
|
|
|
unlock_requestor(chan, flags);
|
2016-05-30 14:17:58 +00:00
|
|
|
break;
|
2016-02-16 21:56:23 +00:00
|
|
|
|
|
|
|
case VM_PKT_DATA_INBAND:
|
|
|
|
|
|
|
|
new_message = (struct pci_incoming_message *)buffer;
|
2016-08-23 04:45:51 +00:00
|
|
|
switch (new_message->message_type.type) {
|
2016-02-16 21:56:23 +00:00
|
|
|
case PCI_BUS_RELATIONS:
|
|
|
|
|
|
|
|
bus_rel = (struct pci_bus_relations *)buffer;
|
2022-05-11 22:32:06 +00:00
|
|
|
if (bytes_recvd < sizeof(*bus_rel) ||
|
|
|
|
bytes_recvd <
|
2020-05-25 16:43:19 +00:00
|
|
|
struct_size(bus_rel, func,
|
|
|
|
bus_rel->device_count)) {
|
2016-02-16 21:56:23 +00:00
|
|
|
dev_err(&hbus->hdev->device,
|
|
|
|
"bus relations too small\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
hv_pci_devices_present(hbus, bus_rel);
|
|
|
|
break;
|
|
|
|
|
2020-02-26 05:06:08 +00:00
|
|
|
case PCI_BUS_RELATIONS2:
|
|
|
|
|
|
|
|
bus_rel2 = (struct pci_bus_relations2 *)buffer;
|
2022-05-11 22:32:06 +00:00
|
|
|
if (bytes_recvd < sizeof(*bus_rel2) ||
|
|
|
|
bytes_recvd <
|
2020-05-25 16:43:19 +00:00
|
|
|
struct_size(bus_rel2, func,
|
|
|
|
bus_rel2->device_count)) {
|
2020-02-26 05:06:08 +00:00
|
|
|
dev_err(&hbus->hdev->device,
|
|
|
|
"bus relations v2 too small\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
hv_pci_devices_present2(hbus, bus_rel2);
|
|
|
|
break;
|
|
|
|
|
2016-02-16 21:56:23 +00:00
|
|
|
case PCI_EJECT:
|
|
|
|
|
|
|
|
dev_message = (struct pci_dev_incoming *)buffer;
|
2022-05-11 22:32:06 +00:00
|
|
|
if (bytes_recvd < sizeof(*dev_message)) {
|
|
|
|
dev_err(&hbus->hdev->device,
|
|
|
|
"eject message too small\n");
|
|
|
|
break;
|
|
|
|
}
|
2016-02-16 21:56:23 +00:00
|
|
|
hpdev = get_pcichild_wslot(hbus,
|
|
|
|
dev_message->wslot.slot);
|
|
|
|
if (hpdev) {
|
|
|
|
hv_pci_eject_device(hpdev);
|
2018-05-23 17:11:12 +00:00
|
|
|
put_pcichild(hpdev);
|
2016-02-16 21:56:23 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2019-08-22 05:05:37 +00:00
|
|
|
case PCI_INVALIDATE_BLOCK:
|
|
|
|
|
|
|
|
inval = (struct pci_dev_inval_block *)buffer;
|
2022-05-11 22:32:06 +00:00
|
|
|
if (bytes_recvd < sizeof(*inval)) {
|
|
|
|
dev_err(&hbus->hdev->device,
|
|
|
|
"invalidate message too small\n");
|
|
|
|
break;
|
|
|
|
}
|
2019-08-22 05:05:37 +00:00
|
|
|
hpdev = get_pcichild_wslot(hbus,
|
|
|
|
inval->wslot.slot);
|
|
|
|
if (hpdev) {
|
|
|
|
if (hpdev->block_invalidate) {
|
|
|
|
hpdev->block_invalidate(
|
|
|
|
hpdev->invalidate_context,
|
|
|
|
inval->block_mask);
|
|
|
|
}
|
|
|
|
put_pcichild(hpdev);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2016-02-16 21:56:23 +00:00
|
|
|
default:
|
|
|
|
dev_warn(&hbus->hdev->device,
|
|
|
|
"Unimplemented protocol message %x\n",
|
2016-08-23 04:45:51 +00:00
|
|
|
new_message->message_type.type);
|
2016-02-16 21:56:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
dev_err(&hbus->hdev->device,
|
|
|
|
"unhandled packet type %d, tid %llx len %d\n",
|
|
|
|
desc->type, req_id, bytes_recvd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-05-30 14:17:58 +00:00
|
|
|
|
|
|
|
kfree(buffer);
|
2016-02-16 21:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* hv_pci_protocol_negotiation() - Set up protocol
|
2020-09-25 23:47:53 +00:00
|
|
|
* @hdev: VMBus's tracking struct for this root PCI bus.
|
|
|
|
* @version: Array of supported channel protocol versions in
|
|
|
|
* the order of probing - highest go first.
|
|
|
|
* @num_version: Number of elements in the version array.
|
2016-02-16 21:56:23 +00:00
|
|
|
*
|
|
|
|
* This driver is intended to support running on Windows 10
|
|
|
|
* (server) and later versions. It will not run on earlier
|
|
|
|
* versions, as they assume that many of the operations which
|
|
|
|
* Linux needs accomplished with a spinlock held were done via
|
|
|
|
* asynchronous messaging via VMBus. Windows 10 increases the
|
|
|
|
* surface area of PCI emulation so that these actions can take
|
|
|
|
* place by suspending a virtual processor for their duration.
|
|
|
|
*
|
|
|
|
* This function negotiates the channel protocol version,
|
|
|
|
* failing if the host doesn't support the necessary protocol
|
|
|
|
* level.
|
|
|
|
*/
|
2019-11-25 05:33:51 +00:00
|
|
|
static int hv_pci_protocol_negotiation(struct hv_device *hdev,
|
|
|
|
enum pci_protocol_version_t version[],
|
|
|
|
int num_version)
|
2016-02-16 21:56:23 +00:00
|
|
|
{
|
2019-11-25 05:33:53 +00:00
|
|
|
struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
|
2016-02-16 21:56:23 +00:00
|
|
|
struct pci_version_request *version_req;
|
|
|
|
struct hv_pci_compl comp_pkt;
|
|
|
|
struct pci_packet *pkt;
|
|
|
|
int ret;
|
2017-05-24 20:41:27 +00:00
|
|
|
int i;
|
2016-02-16 21:56:23 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Initiate the handshake with the host and negotiate
|
|
|
|
* a version that the host can support. We start with the
|
|
|
|
* highest version number and go down if the host cannot
|
|
|
|
* support it.
|
|
|
|
*/
|
|
|
|
pkt = kzalloc(sizeof(*pkt) + sizeof(*version_req), GFP_KERNEL);
|
|
|
|
if (!pkt)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
init_completion(&comp_pkt.host_event);
|
|
|
|
pkt->completion_func = hv_pci_generic_compl;
|
|
|
|
pkt->compl_ctxt = &comp_pkt;
|
|
|
|
version_req = (struct pci_version_request *)&pkt->message;
|
2016-08-23 04:45:51 +00:00
|
|
|
version_req->message_type.type = PCI_QUERY_PROTOCOL_VERSION;
|
2016-02-16 21:56:23 +00:00
|
|
|
|
2019-11-25 05:33:51 +00:00
|
|
|
for (i = 0; i < num_version; i++) {
|
|
|
|
version_req->protocol_version = version[i];
|
2017-05-24 20:41:27 +00:00
|
|
|
ret = vmbus_sendpacket(hdev->channel, version_req,
|
|
|
|
sizeof(struct pci_version_request),
|
|
|
|
(unsigned long)pkt, VM_PKT_DATA_INBAND,
|
|
|
|
VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
|
2018-05-23 21:12:01 +00:00
|
|
|
if (!ret)
|
|
|
|
ret = wait_for_response(hdev, &comp_pkt.host_event);
|
|
|
|
|
2017-05-24 20:41:27 +00:00
|
|
|
if (ret) {
|
|
|
|
dev_err(&hdev->device,
|
2018-05-23 21:12:01 +00:00
|
|
|
"PCI Pass-through VSP failed to request version: %d",
|
2017-05-24 20:41:27 +00:00
|
|
|
ret);
|
|
|
|
goto exit;
|
|
|
|
}
|
2016-02-16 21:56:23 +00:00
|
|
|
|
2017-05-24 20:41:27 +00:00
|
|
|
if (comp_pkt.completion_status >= 0) {
|
2019-11-25 05:33:53 +00:00
|
|
|
hbus->protocol_version = version[i];
|
2017-05-24 20:41:27 +00:00
|
|
|
dev_info(&hdev->device,
|
|
|
|
"PCI VMBus probing: Using version %#x\n",
|
2019-11-25 05:33:53 +00:00
|
|
|
hbus->protocol_version);
|
2017-05-24 20:41:27 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (comp_pkt.completion_status != STATUS_REVISION_MISMATCH) {
|
|
|
|
dev_err(&hdev->device,
|
|
|
|
"PCI Pass-through VSP failed version request: %#x",
|
|
|
|
comp_pkt.completion_status);
|
|
|
|
ret = -EPROTO;
|
|
|
|
goto exit;
|
|
|
|
}
|
2016-02-16 21:56:23 +00:00
|
|
|
|
2017-05-24 20:41:27 +00:00
|
|
|
reinit_completion(&comp_pkt.host_event);
|
2016-02-16 21:56:23 +00:00
|
|
|
}
|
|
|
|
|
2017-05-24 20:41:27 +00:00
|
|
|
dev_err(&hdev->device,
|
|
|
|
"PCI pass-through VSP failed to find supported version");
|
|
|
|
ret = -EPROTO;
|
2016-02-16 21:56:23 +00:00
|
|
|
|
|
|
|
exit:
|
|
|
|
kfree(pkt);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* hv_pci_free_bridge_windows() - Release memory regions for the
|
|
|
|
* bus
|
|
|
|
* @hbus: Root PCI bus, as understood by this driver
|
|
|
|
*/
|
|
|
|
static void hv_pci_free_bridge_windows(struct hv_pcibus_device *hbus)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Set the resources back to the way they looked when they
|
|
|
|
* were allocated by setting IORESOURCE_BUSY again.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (hbus->low_mmio_space && hbus->low_mmio_res) {
|
|
|
|
hbus->low_mmio_res->flags |= IORESOURCE_BUSY;
|
2016-04-05 17:22:52 +00:00
|
|
|
vmbus_free_mmio(hbus->low_mmio_res->start,
|
|
|
|
resource_size(hbus->low_mmio_res));
|
2016-02-16 21:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (hbus->high_mmio_space && hbus->high_mmio_res) {
|
|
|
|
hbus->high_mmio_res->flags |= IORESOURCE_BUSY;
|
2016-04-05 17:22:52 +00:00
|
|
|
vmbus_free_mmio(hbus->high_mmio_res->start,
|
|
|
|
resource_size(hbus->high_mmio_res));
|
2016-02-16 21:56:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* hv_pci_allocate_bridge_windows() - Allocate memory regions
|
|
|
|
* for the bus
|
|
|
|
* @hbus: Root PCI bus, as understood by this driver
|
|
|
|
*
|
|
|
|
* This function calls vmbus_allocate_mmio(), which is itself a
|
|
|
|
* bit of a compromise. Ideally, we might change the pnp layer
|
|
|
|
* in the kernel such that it comprehends either PCI devices
|
|
|
|
* which are "grandchildren of ACPI," with some intermediate bus
|
|
|
|
* node (in this case, VMBus) or change it such that it
|
|
|
|
* understands VMBus. The pnp layer, however, has been declared
|
|
|
|
* deprecated, and not subject to change.
|
|
|
|
*
|
|
|
|
* The workaround, implemented here, is to ask VMBus to allocate
|
|
|
|
* MMIO space for this bus. VMBus itself knows which ranges are
|
|
|
|
* appropriate by looking at its own ACPI objects. Then, after
|
|
|
|
* these ranges are claimed, they're modified to look like they
|
|
|
|
* would have looked if the ACPI and pnp code had allocated
|
|
|
|
* bridge windows. These descriptors have to exist in this form
|
|
|
|
* in order to satisfy the code which will get invoked when the
|
|
|
|
* endpoint PCI function driver calls request_mem_region() or
|
|
|
|
* request_mem_region_exclusive().
|
|
|
|
*
|
|
|
|
* Return: 0 on success, -errno on failure
|
|
|
|
*/
|
|
|
|
static int hv_pci_allocate_bridge_windows(struct hv_pcibus_device *hbus)
|
|
|
|
{
|
|
|
|
resource_size_t align;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (hbus->low_mmio_space) {
|
|
|
|
align = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space));
|
|
|
|
ret = vmbus_allocate_mmio(&hbus->low_mmio_res, hbus->hdev, 0,
|
|
|
|
(u64)(u32)0xffffffff,
|
|
|
|
hbus->low_mmio_space,
|
|
|
|
align, false);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(&hbus->hdev->device,
|
|
|
|
"Need %#llx of low MMIO space. Consider reconfiguring the VM.\n",
|
|
|
|
hbus->low_mmio_space);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Modify this resource to become a bridge window. */
|
|
|
|
hbus->low_mmio_res->flags |= IORESOURCE_WINDOW;
|
|
|
|
hbus->low_mmio_res->flags &= ~IORESOURCE_BUSY;
|
2021-07-26 18:06:54 +00:00
|
|
|
pci_add_resource(&hbus->bridge->windows, hbus->low_mmio_res);
|
2016-02-16 21:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (hbus->high_mmio_space) {
|
|
|
|
align = 1ULL << (63 - __builtin_clzll(hbus->high_mmio_space));
|
|
|
|
ret = vmbus_allocate_mmio(&hbus->high_mmio_res, hbus->hdev,
|
|
|
|
0x100000000, -1,
|
|
|
|
hbus->high_mmio_space, align,
|
|
|
|
false);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(&hbus->hdev->device,
|
|
|
|
"Need %#llx of high MMIO space. Consider reconfiguring the VM.\n",
|
|
|
|
hbus->high_mmio_space);
|
|
|
|
goto release_low_mmio;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Modify this resource to become a bridge window. */
|
|
|
|
hbus->high_mmio_res->flags |= IORESOURCE_WINDOW;
|
|
|
|
hbus->high_mmio_res->flags &= ~IORESOURCE_BUSY;
|
2021-07-26 18:06:54 +00:00
|
|
|
pci_add_resource(&hbus->bridge->windows, hbus->high_mmio_res);
|
2016-02-16 21:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
release_low_mmio:
|
|
|
|
if (hbus->low_mmio_res) {
|
2016-04-05 17:22:52 +00:00
|
|
|
vmbus_free_mmio(hbus->low_mmio_res->start,
|
|
|
|
resource_size(hbus->low_mmio_res));
|
2016-02-16 21:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* hv_allocate_config_window() - Find MMIO space for PCI Config
|
|
|
|
* @hbus: Root PCI bus, as understood by this driver
|
|
|
|
*
|
|
|
|
* This function claims memory-mapped I/O space for accessing
|
|
|
|
* configuration space for the functions on this bus.
|
|
|
|
*
|
|
|
|
* Return: 0 on success, -errno on failure
|
|
|
|
*/
|
|
|
|
static int hv_allocate_config_window(struct hv_pcibus_device *hbus)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up a region of MMIO space to use for accessing configuration
|
|
|
|
* space.
|
|
|
|
*/
|
|
|
|
ret = vmbus_allocate_mmio(&hbus->mem_config, hbus->hdev, 0, -1,
|
|
|
|
PCI_CONFIG_MMIO_LENGTH, 0x1000, false);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* vmbus_allocate_mmio() gets used for allocating both device endpoint
|
|
|
|
* resource claims (those which cannot be overlapped) and the ranges
|
|
|
|
* which are valid for the children of this bus, which are intended
|
|
|
|
* to be overlapped by those children. Set the flag on this claim
|
|
|
|
* meaning that this region can't be overlapped.
|
|
|
|
*/
|
|
|
|
|
|
|
|
hbus->mem_config->flags |= IORESOURCE_BUSY;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void hv_free_config_window(struct hv_pcibus_device *hbus)
|
|
|
|
{
|
2016-04-05 17:22:52 +00:00
|
|
|
vmbus_free_mmio(hbus->mem_config->start, PCI_CONFIG_MMIO_LENGTH);
|
2016-02-16 21:56:23 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 05:03:00 +00:00
|
|
|
static int hv_pci_bus_exit(struct hv_device *hdev, bool keep_devs);
|
|
|
|
|
2016-02-16 21:56:23 +00:00
|
|
|
/**
|
|
|
|
* hv_pci_enter_d0() - Bring the "bus" into the D0 power state
|
|
|
|
* @hdev: VMBus's tracking struct for this root PCI bus
|
|
|
|
*
|
|
|
|
* Return: 0 on success, -errno on failure
|
|
|
|
*/
|
|
|
|
static int hv_pci_enter_d0(struct hv_device *hdev)
|
|
|
|
{
|
|
|
|
struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
|
|
|
|
struct pci_bus_d0_entry *d0_entry;
|
|
|
|
struct hv_pci_compl comp_pkt;
|
|
|
|
struct pci_packet *pkt;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tell the host that the bus is ready to use, and moved into the
|
|
|
|
* powered-on state. This includes telling the host which region
|
|
|
|
* of memory-mapped I/O space has been chosen for configuration space
|
|
|
|
* access.
|
|
|
|
*/
|
|
|
|
pkt = kzalloc(sizeof(*pkt) + sizeof(*d0_entry), GFP_KERNEL);
|
|
|
|
if (!pkt)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
init_completion(&comp_pkt.host_event);
|
|
|
|
pkt->completion_func = hv_pci_generic_compl;
|
|
|
|
pkt->compl_ctxt = &comp_pkt;
|
|
|
|
d0_entry = (struct pci_bus_d0_entry *)&pkt->message;
|
2016-08-23 04:45:51 +00:00
|
|
|
d0_entry->message_type.type = PCI_BUS_D0ENTRY;
|
2016-02-16 21:56:23 +00:00
|
|
|
d0_entry->mmio_base = hbus->mem_config->start;
|
|
|
|
|
|
|
|
ret = vmbus_sendpacket(hdev->channel, d0_entry, sizeof(*d0_entry),
|
|
|
|
(unsigned long)pkt, VM_PKT_DATA_INBAND,
|
|
|
|
VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
|
2018-05-23 21:12:01 +00:00
|
|
|
if (!ret)
|
|
|
|
ret = wait_for_response(hdev, &comp_pkt.host_event);
|
|
|
|
|
2016-02-16 21:56:23 +00:00
|
|
|
if (ret)
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
if (comp_pkt.completion_status < 0) {
|
|
|
|
dev_err(&hdev->device,
|
|
|
|
"PCI Pass-through VSP failed D0 Entry with status %x\n",
|
|
|
|
comp_pkt.completion_status);
|
|
|
|
ret = -EPROTO;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
exit:
|
|
|
|
kfree(pkt);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* hv_pci_query_relations() - Ask host to send list of child
|
|
|
|
* devices
|
|
|
|
* @hdev: VMBus's tracking struct for this root PCI bus
|
|
|
|
*
|
|
|
|
* Return: 0 on success, -errno on failure
|
|
|
|
*/
|
|
|
|
static int hv_pci_query_relations(struct hv_device *hdev)
|
|
|
|
{
|
|
|
|
struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
|
|
|
|
struct pci_message message;
|
|
|
|
struct completion comp;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Ask the host to send along the list of child devices */
|
|
|
|
init_completion(&comp);
|
|
|
|
if (cmpxchg(&hbus->survey_event, NULL, &comp))
|
|
|
|
return -ENOTEMPTY;
|
|
|
|
|
|
|
|
memset(&message, 0, sizeof(message));
|
2016-08-23 04:45:51 +00:00
|
|
|
message.type = PCI_QUERY_BUS_RELATIONS;
|
2016-02-16 21:56:23 +00:00
|
|
|
|
|
|
|
ret = vmbus_sendpacket(hdev->channel, &message, sizeof(message),
|
|
|
|
0, VM_PKT_DATA_INBAND, 0);
|
2018-05-23 21:12:01 +00:00
|
|
|
if (!ret)
|
|
|
|
ret = wait_for_response(hdev, &comp);
|
2016-02-16 21:56:23 +00:00
|
|
|
|
2018-05-23 21:12:01 +00:00
|
|
|
return ret;
|
2016-02-16 21:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* hv_send_resources_allocated() - Report local resource choices
|
|
|
|
* @hdev: VMBus's tracking struct for this root PCI bus
|
|
|
|
*
|
|
|
|
* The host OS is expecting to be sent a request as a message
|
|
|
|
* which contains all the resources that the device will use.
|
|
|
|
* The response contains those same resources, "translated"
|
|
|
|
* which is to say, the values which should be used by the
|
|
|
|
* hardware, when it delivers an interrupt. (MMIO resources are
|
|
|
|
* used in local terms.) This is nice for Windows, and lines up
|
|
|
|
* with the FDO/PDO split, which doesn't exist in Linux. Linux
|
|
|
|
* is deeply expecting to scan an emulated PCI configuration
|
|
|
|
* space. So this message is sent here only to drive the state
|
|
|
|
* machine on the host forward.
|
|
|
|
*
|
|
|
|
* Return: 0 on success, -errno on failure
|
|
|
|
*/
|
|
|
|
static int hv_send_resources_allocated(struct hv_device *hdev)
|
|
|
|
{
|
|
|
|
struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
|
|
|
|
struct pci_resources_assigned *res_assigned;
|
2017-05-24 20:41:28 +00:00
|
|
|
struct pci_resources_assigned2 *res_assigned2;
|
2016-02-16 21:56:23 +00:00
|
|
|
struct hv_pci_compl comp_pkt;
|
|
|
|
struct hv_pci_dev *hpdev;
|
|
|
|
struct pci_packet *pkt;
|
2017-05-24 20:41:28 +00:00
|
|
|
size_t size_res;
|
2020-05-07 05:02:11 +00:00
|
|
|
int wslot;
|
2016-02-16 21:56:23 +00:00
|
|
|
int ret;
|
|
|
|
|
2019-11-25 05:33:53 +00:00
|
|
|
size_res = (hbus->protocol_version < PCI_PROTOCOL_VERSION_1_2)
|
2017-05-24 20:41:28 +00:00
|
|
|
? sizeof(*res_assigned) : sizeof(*res_assigned2);
|
|
|
|
|
|
|
|
pkt = kmalloc(sizeof(*pkt) + size_res, GFP_KERNEL);
|
2016-02-16 21:56:23 +00:00
|
|
|
if (!pkt)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
for (wslot = 0; wslot < 256; wslot++) {
|
|
|
|
hpdev = get_pcichild_wslot(hbus, wslot);
|
|
|
|
if (!hpdev)
|
|
|
|
continue;
|
|
|
|
|
2017-05-24 20:41:28 +00:00
|
|
|
memset(pkt, 0, sizeof(*pkt) + size_res);
|
2016-02-16 21:56:23 +00:00
|
|
|
init_completion(&comp_pkt.host_event);
|
|
|
|
pkt->completion_func = hv_pci_generic_compl;
|
|
|
|
pkt->compl_ctxt = &comp_pkt;
|
|
|
|
|
2019-11-25 05:33:53 +00:00
|
|
|
if (hbus->protocol_version < PCI_PROTOCOL_VERSION_1_2) {
|
2017-05-24 20:41:28 +00:00
|
|
|
res_assigned =
|
|
|
|
(struct pci_resources_assigned *)&pkt->message;
|
|
|
|
res_assigned->message_type.type =
|
|
|
|
PCI_RESOURCES_ASSIGNED;
|
|
|
|
res_assigned->wslot.slot = hpdev->desc.win_slot.slot;
|
|
|
|
} else {
|
|
|
|
res_assigned2 =
|
|
|
|
(struct pci_resources_assigned2 *)&pkt->message;
|
|
|
|
res_assigned2->message_type.type =
|
|
|
|
PCI_RESOURCES_ASSIGNED2;
|
|
|
|
res_assigned2->wslot.slot = hpdev->desc.win_slot.slot;
|
|
|
|
}
|
2018-05-23 17:11:12 +00:00
|
|
|
put_pcichild(hpdev);
|
2016-02-16 21:56:23 +00:00
|
|
|
|
2017-05-24 20:41:28 +00:00
|
|
|
ret = vmbus_sendpacket(hdev->channel, &pkt->message,
|
|
|
|
size_res, (unsigned long)pkt,
|
|
|
|
VM_PKT_DATA_INBAND,
|
|
|
|
VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
|
2018-05-23 21:12:01 +00:00
|
|
|
if (!ret)
|
|
|
|
ret = wait_for_response(hdev, &comp_pkt.host_event);
|
2016-02-16 21:56:23 +00:00
|
|
|
if (ret)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (comp_pkt.completion_status < 0) {
|
|
|
|
ret = -EPROTO;
|
|
|
|
dev_err(&hdev->device,
|
|
|
|
"resource allocated returned 0x%x",
|
|
|
|
comp_pkt.completion_status);
|
|
|
|
break;
|
|
|
|
}
|
2020-05-07 05:02:11 +00:00
|
|
|
|
|
|
|
hbus->wslot_res_allocated = wslot;
|
2016-02-16 21:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
kfree(pkt);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* hv_send_resources_released() - Report local resources
|
|
|
|
* released
|
|
|
|
* @hdev: VMBus's tracking struct for this root PCI bus
|
|
|
|
*
|
|
|
|
* Return: 0 on success, -errno on failure
|
|
|
|
*/
|
|
|
|
static int hv_send_resources_released(struct hv_device *hdev)
|
|
|
|
{
|
|
|
|
struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
|
|
|
|
struct pci_child_message pkt;
|
|
|
|
struct hv_pci_dev *hpdev;
|
2020-05-07 05:02:11 +00:00
|
|
|
int wslot;
|
2016-02-16 21:56:23 +00:00
|
|
|
int ret;
|
|
|
|
|
2020-05-07 05:02:11 +00:00
|
|
|
for (wslot = hbus->wslot_res_allocated; wslot >= 0; wslot--) {
|
2016-02-16 21:56:23 +00:00
|
|
|
hpdev = get_pcichild_wslot(hbus, wslot);
|
|
|
|
if (!hpdev)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
memset(&pkt, 0, sizeof(pkt));
|
2016-08-23 04:45:51 +00:00
|
|
|
pkt.message_type.type = PCI_RESOURCES_RELEASED;
|
2016-02-16 21:56:23 +00:00
|
|
|
pkt.wslot.slot = hpdev->desc.win_slot.slot;
|
|
|
|
|
2018-05-23 17:11:12 +00:00
|
|
|
put_pcichild(hpdev);
|
2016-02-16 21:56:23 +00:00
|
|
|
|
|
|
|
ret = vmbus_sendpacket(hdev->channel, &pkt, sizeof(pkt), 0,
|
|
|
|
VM_PKT_DATA_INBAND, 0);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2020-05-07 05:02:11 +00:00
|
|
|
|
|
|
|
hbus->wslot_res_allocated = wslot - 1;
|
2016-02-16 21:56:23 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 05:02:11 +00:00
|
|
|
hbus->wslot_res_allocated = -1;
|
|
|
|
|
2016-02-16 21:56:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-08-15 17:01:37 +00:00
|
|
|
#define HVPCI_DOM_MAP_SIZE (64 * 1024)
|
|
|
|
static DECLARE_BITMAP(hvpci_dom_map, HVPCI_DOM_MAP_SIZE);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* PCI domain number 0 is used by emulated devices on Gen1 VMs, so define 0
|
|
|
|
* as invalid for passthrough PCI devices of this driver.
|
|
|
|
*/
|
|
|
|
#define HVPCI_DOM_INVALID 0
|
|
|
|
|
|
|
|
/**
|
|
|
|
* hv_get_dom_num() - Get a valid PCI domain number
|
|
|
|
* Check if the PCI domain number is in use, and return another number if
|
|
|
|
* it is in use.
|
|
|
|
*
|
|
|
|
* @dom: Requested domain number
|
|
|
|
*
|
|
|
|
* return: domain number on success, HVPCI_DOM_INVALID on failure
|
|
|
|
*/
|
|
|
|
static u16 hv_get_dom_num(u16 dom)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if (test_and_set_bit(dom, hvpci_dom_map) == 0)
|
|
|
|
return dom;
|
|
|
|
|
|
|
|
for_each_clear_bit(i, hvpci_dom_map, HVPCI_DOM_MAP_SIZE) {
|
|
|
|
if (test_and_set_bit(i, hvpci_dom_map) == 0)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return HVPCI_DOM_INVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* hv_put_dom_num() - Mark the PCI domain number as free
|
|
|
|
* @dom: Domain number to be freed
|
|
|
|
*/
|
|
|
|
static void hv_put_dom_num(u16 dom)
|
|
|
|
{
|
|
|
|
clear_bit(dom, hvpci_dom_map);
|
|
|
|
}
|
|
|
|
|
2016-02-16 21:56:23 +00:00
|
|
|
/**
|
|
|
|
* hv_pci_probe() - New VMBus channel probe, for a root PCI bus
|
|
|
|
* @hdev: VMBus's tracking struct for this root PCI bus
|
|
|
|
* @dev_id: Identifies the device itself
|
|
|
|
*
|
|
|
|
* Return: 0 on success, -errno on failure
|
|
|
|
*/
|
|
|
|
static int hv_pci_probe(struct hv_device *hdev,
|
|
|
|
const struct hv_vmbus_device_id *dev_id)
|
|
|
|
{
|
2021-07-26 18:06:54 +00:00
|
|
|
struct pci_host_bridge *bridge;
|
2016-02-16 21:56:23 +00:00
|
|
|
struct hv_pcibus_device *hbus;
|
2019-08-15 17:01:37 +00:00
|
|
|
u16 dom_req, dom;
|
2019-08-06 14:23:33 +00:00
|
|
|
char *name;
|
2020-07-27 07:17:31 +00:00
|
|
|
bool enter_d0_retry = true;
|
2016-02-16 21:56:23 +00:00
|
|
|
int ret;
|
|
|
|
|
2017-05-24 20:41:25 +00:00
|
|
|
/*
|
|
|
|
* hv_pcibus_device contains the hypercall arguments for retargeting in
|
|
|
|
* hv_irq_unmask(). Those must not cross a page boundary.
|
|
|
|
*/
|
2019-11-25 05:33:54 +00:00
|
|
|
BUILD_BUG_ON(sizeof(*hbus) > HV_HYP_PAGE_SIZE);
|
2017-05-24 20:41:25 +00:00
|
|
|
|
2021-07-26 18:06:54 +00:00
|
|
|
bridge = devm_pci_alloc_host_bridge(&hdev->device, 0);
|
|
|
|
if (!bridge)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2019-11-25 05:33:54 +00:00
|
|
|
/*
|
|
|
|
* With the recent 59bb47985c1d ("mm, sl[aou]b: guarantee natural
|
|
|
|
* alignment for kmalloc(power-of-two)"), kzalloc() is able to allocate
|
|
|
|
* a 4KB buffer that is guaranteed to be 4KB-aligned. Here the size and
|
|
|
|
* alignment of hbus is important because hbus's field
|
|
|
|
* retarget_msi_interrupt_params must not cross a 4KB page boundary.
|
|
|
|
*
|
|
|
|
* Here we prefer kzalloc to get_zeroed_page(), because a buffer
|
|
|
|
* allocated by the latter is not tracked and scanned by kmemleak, and
|
|
|
|
* hence kmemleak reports the pointer contained in the hbus buffer
|
|
|
|
* (i.e. the hpdev struct, which is created in new_pcichild_device() and
|
|
|
|
* is tracked by hbus->children) as memory leak (false positive).
|
|
|
|
*
|
|
|
|
* If the kernel doesn't have 59bb47985c1d, get_zeroed_page() *must* be
|
|
|
|
* used to allocate the hbus buffer and we can avoid the kmemleak false
|
|
|
|
* positive by using kmemleak_alloc() and kmemleak_free() to ask
|
|
|
|
* kmemleak to track and scan the hbus buffer.
|
|
|
|
*/
|
2020-02-22 05:59:56 +00:00
|
|
|
hbus = kzalloc(HV_HYP_PAGE_SIZE, GFP_KERNEL);
|
2016-02-16 21:56:23 +00:00
|
|
|
if (!hbus)
|
|
|
|
return -ENOMEM;
|
2021-07-26 18:06:54 +00:00
|
|
|
|
|
|
|
hbus->bridge = bridge;
|
2017-03-23 21:58:10 +00:00
|
|
|
hbus->state = hv_pcibus_init;
|
2020-05-07 05:02:11 +00:00
|
|
|
hbus->wslot_res_allocated = -1;
|
2016-02-16 21:56:23 +00:00
|
|
|
|
|
|
|
/*
|
2019-08-15 17:01:37 +00:00
|
|
|
* The PCI bus "domain" is what is called "segment" in ACPI and other
|
|
|
|
* specs. Pull it from the instance ID, to get something usually
|
|
|
|
* unique. In rare cases of collision, we will find out another number
|
|
|
|
* not in use.
|
|
|
|
*
|
|
|
|
* Note that, since this code only runs in a Hyper-V VM, Hyper-V
|
|
|
|
* together with this guest driver can guarantee that (1) The only
|
|
|
|
* domain used by Gen1 VMs for something that looks like a physical
|
|
|
|
* PCI bus (which is actually emulated by the hypervisor) is domain 0.
|
|
|
|
* (2) There will be no overlap between domains (after fixing possible
|
|
|
|
* collisions) in the same VM.
|
2016-02-16 21:56:23 +00:00
|
|
|
*/
|
2019-08-15 17:01:45 +00:00
|
|
|
dom_req = hdev->dev_instance.b[5] << 8 | hdev->dev_instance.b[4];
|
2019-08-15 17:01:37 +00:00
|
|
|
dom = hv_get_dom_num(dom_req);
|
|
|
|
|
|
|
|
if (dom == HVPCI_DOM_INVALID) {
|
|
|
|
dev_err(&hdev->device,
|
2021-10-08 22:27:30 +00:00
|
|
|
"Unable to use dom# 0x%x or other numbers", dom_req);
|
2019-08-15 17:01:37 +00:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto free_bus;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dom != dom_req)
|
|
|
|
dev_info(&hdev->device,
|
2021-10-08 22:27:30 +00:00
|
|
|
"PCI dom# 0x%x has collision, using 0x%x",
|
2019-08-15 17:01:37 +00:00
|
|
|
dom_req, dom);
|
|
|
|
|
2021-07-26 18:06:55 +00:00
|
|
|
hbus->bridge->domain_nr = dom;
|
2021-07-26 18:06:57 +00:00
|
|
|
#ifdef CONFIG_X86
|
2019-08-15 17:01:37 +00:00
|
|
|
hbus->sysdata.domain = dom;
|
2022-03-24 16:14:52 +00:00
|
|
|
#elif defined(CONFIG_ARM64)
|
|
|
|
/*
|
|
|
|
* Set the PCI bus parent to be the corresponding VMbus
|
|
|
|
* device. Then the VMbus device will be assigned as the
|
|
|
|
* ACPI companion in pcibios_root_bridge_prepare() and
|
|
|
|
* pci_dma_configure() will propagate device coherence
|
|
|
|
* information to devices created on the bus.
|
|
|
|
*/
|
|
|
|
hbus->sysdata.parent = hdev->device.parent;
|
2021-07-26 18:06:57 +00:00
|
|
|
#endif
|
2016-02-16 21:56:23 +00:00
|
|
|
|
|
|
|
hbus->hdev = hdev;
|
|
|
|
INIT_LIST_HEAD(&hbus->children);
|
|
|
|
INIT_LIST_HEAD(&hbus->dr_list);
|
|
|
|
spin_lock_init(&hbus->config_lock);
|
|
|
|
spin_lock_init(&hbus->device_list_lock);
|
2016-11-08 22:04:38 +00:00
|
|
|
spin_lock_init(&hbus->retarget_msi_interrupt_lock);
|
2018-03-15 14:20:53 +00:00
|
|
|
hbus->wq = alloc_ordered_workqueue("hv_pci_%x", 0,
|
2021-07-26 18:06:55 +00:00
|
|
|
hbus->bridge->domain_nr);
|
2018-03-15 14:20:53 +00:00
|
|
|
if (!hbus->wq) {
|
|
|
|
ret = -ENOMEM;
|
2019-08-15 17:01:37 +00:00
|
|
|
goto free_dom;
|
2018-03-15 14:20:53 +00:00
|
|
|
}
|
2016-02-16 21:56:23 +00:00
|
|
|
|
2022-04-19 12:23:21 +00:00
|
|
|
hdev->channel->next_request_id_callback = vmbus_next_request_id;
|
|
|
|
hdev->channel->request_addr_callback = vmbus_request_addr;
|
|
|
|
hdev->channel->rqstor_size = HV_PCI_RQSTOR_SIZE;
|
|
|
|
|
2016-02-16 21:56:23 +00:00
|
|
|
ret = vmbus_open(hdev->channel, pci_ring_size, pci_ring_size, NULL, 0,
|
|
|
|
hv_pci_onchannelcallback, hbus);
|
|
|
|
if (ret)
|
2018-03-15 14:20:53 +00:00
|
|
|
goto destroy_wq;
|
2016-02-16 21:56:23 +00:00
|
|
|
|
|
|
|
hv_set_drvdata(hdev, hbus);
|
|
|
|
|
2019-11-25 05:33:51 +00:00
|
|
|
ret = hv_pci_protocol_negotiation(hdev, pci_protocol_versions,
|
|
|
|
ARRAY_SIZE(pci_protocol_versions));
|
2016-02-16 21:56:23 +00:00
|
|
|
if (ret)
|
|
|
|
goto close;
|
|
|
|
|
|
|
|
ret = hv_allocate_config_window(hbus);
|
|
|
|
if (ret)
|
|
|
|
goto close;
|
|
|
|
|
|
|
|
hbus->cfg_addr = ioremap(hbus->mem_config->start,
|
|
|
|
PCI_CONFIG_MMIO_LENGTH);
|
|
|
|
if (!hbus->cfg_addr) {
|
|
|
|
dev_err(&hdev->device,
|
|
|
|
"Unable to map a virtual address for config space\n");
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto free_config;
|
|
|
|
}
|
|
|
|
|
2019-08-06 14:23:33 +00:00
|
|
|
name = kasprintf(GFP_KERNEL, "%pUL", &hdev->dev_instance);
|
|
|
|
if (!name) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto unmap;
|
|
|
|
}
|
|
|
|
|
2021-07-26 18:06:56 +00:00
|
|
|
hbus->fwnode = irq_domain_alloc_named_fwnode(name);
|
2019-08-06 14:23:33 +00:00
|
|
|
kfree(name);
|
2021-07-26 18:06:56 +00:00
|
|
|
if (!hbus->fwnode) {
|
2016-02-16 21:56:23 +00:00
|
|
|
ret = -ENOMEM;
|
|
|
|
goto unmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = hv_pcie_init_irq_domain(hbus);
|
|
|
|
if (ret)
|
|
|
|
goto free_fwnode;
|
|
|
|
|
2020-07-27 07:17:31 +00:00
|
|
|
retry:
|
2016-02-16 21:56:23 +00:00
|
|
|
ret = hv_pci_query_relations(hdev);
|
|
|
|
if (ret)
|
|
|
|
goto free_irq_domain;
|
|
|
|
|
|
|
|
ret = hv_pci_enter_d0(hdev);
|
2020-07-27 07:17:31 +00:00
|
|
|
/*
|
|
|
|
* In certain case (Kdump) the pci device of interest was
|
|
|
|
* not cleanly shut down and resource is still held on host
|
|
|
|
* side, the host could return invalid device status.
|
|
|
|
* We need to explicitly request host to release the resource
|
|
|
|
* and try to enter D0 again.
|
|
|
|
* Since the hv_pci_bus_exit() call releases structures
|
|
|
|
* of all its child devices, we need to start the retry from
|
|
|
|
* hv_pci_query_relations() call, requesting host to send
|
|
|
|
* the synchronous child device relations message before this
|
|
|
|
* information is needed in hv_send_resources_allocated()
|
|
|
|
* call later.
|
|
|
|
*/
|
|
|
|
if (ret == -EPROTO && enter_d0_retry) {
|
|
|
|
enter_d0_retry = false;
|
|
|
|
|
|
|
|
dev_err(&hdev->device, "Retrying D0 Entry\n");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Hv_pci_bus_exit() calls hv_send_resources_released()
|
|
|
|
* to free up resources of its child devices.
|
|
|
|
* In the kdump kernel we need to set the
|
|
|
|
* wslot_res_allocated to 255 so it scans all child
|
|
|
|
* devices to release resources allocated in the
|
|
|
|
* normal kernel before panic happened.
|
|
|
|
*/
|
|
|
|
hbus->wslot_res_allocated = 255;
|
|
|
|
ret = hv_pci_bus_exit(hdev, true);
|
|
|
|
|
|
|
|
if (ret == 0)
|
|
|
|
goto retry;
|
|
|
|
|
|
|
|
dev_err(&hdev->device,
|
|
|
|
"Retrying D0 failed with ret %d\n", ret);
|
|
|
|
}
|
2016-02-16 21:56:23 +00:00
|
|
|
if (ret)
|
|
|
|
goto free_irq_domain;
|
|
|
|
|
|
|
|
ret = hv_pci_allocate_bridge_windows(hbus);
|
|
|
|
if (ret)
|
2020-05-07 05:02:11 +00:00
|
|
|
goto exit_d0;
|
2016-02-16 21:56:23 +00:00
|
|
|
|
|
|
|
ret = hv_send_resources_allocated(hdev);
|
|
|
|
if (ret)
|
|
|
|
goto free_windows;
|
|
|
|
|
|
|
|
prepopulate_bars(hbus);
|
|
|
|
|
|
|
|
hbus->state = hv_pcibus_probed;
|
|
|
|
|
|
|
|
ret = create_root_hv_pci_bus(hbus);
|
|
|
|
if (ret)
|
|
|
|
goto free_windows;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
free_windows:
|
|
|
|
hv_pci_free_bridge_windows(hbus);
|
2020-05-07 05:02:11 +00:00
|
|
|
exit_d0:
|
|
|
|
(void) hv_pci_bus_exit(hdev, true);
|
2016-02-16 21:56:23 +00:00
|
|
|
free_irq_domain:
|
|
|
|
irq_domain_remove(hbus->irq_domain);
|
|
|
|
free_fwnode:
|
2021-07-26 18:06:56 +00:00
|
|
|
irq_domain_free_fwnode(hbus->fwnode);
|
2016-02-16 21:56:23 +00:00
|
|
|
unmap:
|
|
|
|
iounmap(hbus->cfg_addr);
|
|
|
|
free_config:
|
|
|
|
hv_free_config_window(hbus);
|
|
|
|
close:
|
|
|
|
vmbus_close(hdev->channel);
|
2018-03-15 14:20:53 +00:00
|
|
|
destroy_wq:
|
|
|
|
destroy_workqueue(hbus->wq);
|
2019-08-15 17:01:37 +00:00
|
|
|
free_dom:
|
2021-07-26 18:06:55 +00:00
|
|
|
hv_put_dom_num(hbus->bridge->domain_nr);
|
2016-02-16 21:56:23 +00:00
|
|
|
free_bus:
|
2020-02-22 05:59:57 +00:00
|
|
|
kfree(hbus);
|
2016-02-16 21:56:23 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-05-07 05:03:00 +00:00
|
|
|
static int hv_pci_bus_exit(struct hv_device *hdev, bool keep_devs)
|
2016-02-16 21:56:23 +00:00
|
|
|
{
|
2016-11-10 07:18:47 +00:00
|
|
|
struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
|
2022-05-11 22:32:07 +00:00
|
|
|
struct vmbus_channel *chan = hdev->channel;
|
2016-11-10 07:18:47 +00:00
|
|
|
struct {
|
2016-02-16 21:56:23 +00:00
|
|
|
struct pci_packet teardown_packet;
|
2016-11-10 07:18:47 +00:00
|
|
|
u8 buffer[sizeof(struct pci_message)];
|
2016-02-16 21:56:23 +00:00
|
|
|
} pkt;
|
|
|
|
struct hv_pci_compl comp_pkt;
|
2021-05-12 08:06:40 +00:00
|
|
|
struct hv_pci_dev *hpdev, *tmp;
|
|
|
|
unsigned long flags;
|
2022-05-11 22:32:07 +00:00
|
|
|
u64 trans_id;
|
2016-11-10 07:18:47 +00:00
|
|
|
int ret;
|
2016-02-16 21:56:23 +00:00
|
|
|
|
2016-11-10 07:18:47 +00:00
|
|
|
/*
|
|
|
|
* After the host sends the RESCIND_CHANNEL message, it doesn't
|
|
|
|
* access the per-channel ringbuffer any longer.
|
|
|
|
*/
|
2022-05-11 22:32:07 +00:00
|
|
|
if (chan->rescind)
|
2019-11-25 05:33:51 +00:00
|
|
|
return 0;
|
2016-11-10 07:18:47 +00:00
|
|
|
|
2020-05-07 05:03:00 +00:00
|
|
|
if (!keep_devs) {
|
2021-08-30 23:13:27 +00:00
|
|
|
struct list_head removed;
|
|
|
|
|
|
|
|
/* Move all present children to the list on stack */
|
|
|
|
INIT_LIST_HEAD(&removed);
|
2021-05-12 08:06:40 +00:00
|
|
|
spin_lock_irqsave(&hbus->device_list_lock, flags);
|
2021-08-30 23:13:27 +00:00
|
|
|
list_for_each_entry_safe(hpdev, tmp, &hbus->children, list_entry)
|
|
|
|
list_move_tail(&hpdev->list_entry, &removed);
|
|
|
|
spin_unlock_irqrestore(&hbus->device_list_lock, flags);
|
|
|
|
|
|
|
|
/* Remove all children in the list */
|
|
|
|
list_for_each_entry_safe(hpdev, tmp, &removed, list_entry) {
|
2021-05-12 08:06:40 +00:00
|
|
|
list_del(&hpdev->list_entry);
|
|
|
|
if (hpdev->pci_slot)
|
|
|
|
pci_destroy_slot(hpdev->pci_slot);
|
|
|
|
/* For the two refs got in new_pcichild_device() */
|
|
|
|
put_pcichild(hpdev);
|
|
|
|
put_pcichild(hpdev);
|
|
|
|
}
|
2019-11-25 05:33:51 +00:00
|
|
|
}
|
2016-11-10 07:18:47 +00:00
|
|
|
|
|
|
|
ret = hv_send_resources_released(hdev);
|
2019-11-25 05:33:51 +00:00
|
|
|
if (ret) {
|
2016-11-10 07:18:47 +00:00
|
|
|
dev_err(&hdev->device,
|
|
|
|
"Couldn't send resources released packet(s)\n");
|
2019-11-25 05:33:51 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2016-02-16 21:56:23 +00:00
|
|
|
|
|
|
|
memset(&pkt.teardown_packet, 0, sizeof(pkt.teardown_packet));
|
|
|
|
init_completion(&comp_pkt.host_event);
|
|
|
|
pkt.teardown_packet.completion_func = hv_pci_generic_compl;
|
|
|
|
pkt.teardown_packet.compl_ctxt = &comp_pkt;
|
2016-08-23 04:45:51 +00:00
|
|
|
pkt.teardown_packet.message[0].type = PCI_BUS_D0EXIT;
|
2016-02-16 21:56:23 +00:00
|
|
|
|
2022-05-11 22:32:07 +00:00
|
|
|
ret = vmbus_sendpacket_getid(chan, &pkt.teardown_packet.message,
|
|
|
|
sizeof(struct pci_message),
|
|
|
|
(unsigned long)&pkt.teardown_packet,
|
|
|
|
&trans_id, VM_PKT_DATA_INBAND,
|
|
|
|
VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
|
2019-11-25 05:33:51 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2022-05-11 22:32:07 +00:00
|
|
|
if (wait_for_completion_timeout(&comp_pkt.host_event, 10 * HZ) == 0) {
|
|
|
|
/*
|
|
|
|
* The completion packet on the stack becomes invalid after
|
|
|
|
* 'return'; remove the ID from the VMbus requestor if the
|
|
|
|
* identifier is still mapped to/associated with the packet.
|
|
|
|
*
|
|
|
|
* Cf. hv_pci_onchannelcallback().
|
|
|
|
*/
|
|
|
|
vmbus_request_addr_match(chan, trans_id,
|
|
|
|
(unsigned long)&pkt.teardown_packet);
|
2019-11-25 05:33:51 +00:00
|
|
|
return -ETIMEDOUT;
|
2022-05-11 22:32:07 +00:00
|
|
|
}
|
2019-11-25 05:33:51 +00:00
|
|
|
|
|
|
|
return 0;
|
2016-11-10 07:18:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* hv_pci_remove() - Remove routine for this VMBus channel
|
|
|
|
* @hdev: VMBus's tracking struct for this root PCI bus
|
|
|
|
*
|
|
|
|
* Return: 0 on success, -errno on failure
|
|
|
|
*/
|
|
|
|
static int hv_pci_remove(struct hv_device *hdev)
|
|
|
|
{
|
|
|
|
struct hv_pcibus_device *hbus;
|
2019-11-25 05:33:51 +00:00
|
|
|
int ret;
|
2016-02-16 21:56:23 +00:00
|
|
|
|
2016-11-10 07:18:47 +00:00
|
|
|
hbus = hv_get_drvdata(hdev);
|
2016-02-16 21:56:23 +00:00
|
|
|
if (hbus->state == hv_pcibus_installed) {
|
2021-05-12 08:06:40 +00:00
|
|
|
tasklet_disable(&hdev->channel->callback_event);
|
|
|
|
hbus->state = hv_pcibus_removing;
|
|
|
|
tasklet_enable(&hdev->channel->callback_event);
|
|
|
|
destroy_workqueue(hbus->wq);
|
|
|
|
hbus->wq = NULL;
|
|
|
|
/*
|
|
|
|
* At this point, no work is running or can be scheduled
|
|
|
|
* on hbus-wq. We can't race with hv_pci_devices_present()
|
|
|
|
* or hv_pci_eject_device(), it's safe to proceed.
|
|
|
|
*/
|
|
|
|
|
2016-02-16 21:56:23 +00:00
|
|
|
/* Remove the bus from PCI's point of view. */
|
|
|
|
pci_lock_rescan_remove();
|
2021-07-26 18:06:54 +00:00
|
|
|
pci_stop_root_bus(hbus->bridge->bus);
|
2019-03-04 21:34:48 +00:00
|
|
|
hv_pci_remove_slots(hbus);
|
2021-07-26 18:06:54 +00:00
|
|
|
pci_remove_root_bus(hbus->bridge->bus);
|
2016-02-16 21:56:23 +00:00
|
|
|
pci_unlock_rescan_remove();
|
|
|
|
}
|
|
|
|
|
2019-11-25 05:33:51 +00:00
|
|
|
ret = hv_pci_bus_exit(hdev, false);
|
2016-04-29 09:39:10 +00:00
|
|
|
|
2016-02-16 21:56:23 +00:00
|
|
|
vmbus_close(hdev->channel);
|
|
|
|
|
|
|
|
iounmap(hbus->cfg_addr);
|
|
|
|
hv_free_config_window(hbus);
|
|
|
|
hv_pci_free_bridge_windows(hbus);
|
|
|
|
irq_domain_remove(hbus->irq_domain);
|
2021-07-26 18:06:56 +00:00
|
|
|
irq_domain_free_fwnode(hbus->fwnode);
|
2019-08-15 17:01:37 +00:00
|
|
|
|
2021-07-26 18:06:55 +00:00
|
|
|
hv_put_dom_num(hbus->bridge->domain_nr);
|
2019-08-15 17:01:37 +00:00
|
|
|
|
2019-11-25 05:33:54 +00:00
|
|
|
kfree(hbus);
|
2019-11-25 05:33:51 +00:00
|
|
|
return ret;
|
2016-02-16 21:56:23 +00:00
|
|
|
}
|
|
|
|
|
PCI: hv: Add hibernation support
Add suspend() and resume() functions so that Hyper-V virtual PCI devices
are handled properly when the VM hibernates and resumes from
hibernation.
Note that the suspend() function must make sure there are no pending
work items before calling vmbus_close(), since it runs in a process
context as a callback in dpm_suspend(). When it starts to run, the
channel callback hv_pci_onchannelcallback(), which runs in a tasklet
context, can be still running concurrently and scheduling new work items
onto hbus->wq in hv_pci_devices_present() and hv_pci_eject_device(), and
the work item handlers can access the vmbus channel, which can be being
closed by hv_pci_suspend(), e.g. the work item handler
pci_devices_present_work() -> new_pcichild_device() writes to the vmbus
channel.
To eliminate the race, hv_pci_suspend() disables the channel callback
tasklet, sets hbus->state to hv_pcibus_removing, and re-enables the
tasklet. This way, when hv_pci_suspend() proceeds, it knows that no new
work item can be scheduled, and then it flushes hbus->wq and safely
closes the vmbus channel.
Signed-off-by: Dexuan Cui <decui@microsoft.com>
Signed-off-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Reviewed-by: Michael Kelley <mikelley@microsoft.com>
2019-11-25 05:33:52 +00:00
|
|
|
static int hv_pci_suspend(struct hv_device *hdev)
|
|
|
|
{
|
|
|
|
struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
|
|
|
|
enum hv_pcibus_state old_state;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* hv_pci_suspend() must make sure there are no pending work items
|
|
|
|
* before calling vmbus_close(), since it runs in a process context
|
|
|
|
* as a callback in dpm_suspend(). When it starts to run, the channel
|
|
|
|
* callback hv_pci_onchannelcallback(), which runs in a tasklet
|
|
|
|
* context, can be still running concurrently and scheduling new work
|
|
|
|
* items onto hbus->wq in hv_pci_devices_present() and
|
|
|
|
* hv_pci_eject_device(), and the work item handlers can access the
|
|
|
|
* vmbus channel, which can be being closed by hv_pci_suspend(), e.g.
|
|
|
|
* the work item handler pci_devices_present_work() ->
|
|
|
|
* new_pcichild_device() writes to the vmbus channel.
|
|
|
|
*
|
|
|
|
* To eliminate the race, hv_pci_suspend() disables the channel
|
|
|
|
* callback tasklet, sets hbus->state to hv_pcibus_removing, and
|
|
|
|
* re-enables the tasklet. This way, when hv_pci_suspend() proceeds,
|
|
|
|
* it knows that no new work item can be scheduled, and then it flushes
|
|
|
|
* hbus->wq and safely closes the vmbus channel.
|
|
|
|
*/
|
|
|
|
tasklet_disable(&hdev->channel->callback_event);
|
|
|
|
|
|
|
|
/* Change the hbus state to prevent new work items. */
|
|
|
|
old_state = hbus->state;
|
|
|
|
if (hbus->state == hv_pcibus_installed)
|
|
|
|
hbus->state = hv_pcibus_removing;
|
|
|
|
|
|
|
|
tasklet_enable(&hdev->channel->callback_event);
|
|
|
|
|
|
|
|
if (old_state != hv_pcibus_installed)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
flush_workqueue(hbus->wq);
|
|
|
|
|
|
|
|
ret = hv_pci_bus_exit(hdev, true);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
vmbus_close(hdev->channel);
|
|
|
|
|
2016-02-16 21:56:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
PCI: hv: Fix hibernation in case interrupts are not re-created
pci_restore_msi_state() directly writes the MSI/MSI-X related registers
via MMIO. On a physical machine, this works perfectly; for a Linux VM
running on a hypervisor, which typically enables IOMMU interrupt remapping,
the hypervisor usually should trap and emulate the MMIO accesses in order
to re-create the necessary interrupt remapping table entries in the IOMMU,
otherwise the interrupts can not work in the VM after hibernation.
Hyper-V is different from other hypervisors in that it does not trap and
emulate the MMIO accesses, and instead it uses a para-virtualized method,
which requires the VM to call hv_compose_msi_msg() to notify the hypervisor
of the info that would be passed to the hypervisor in the case of the
trap-and-emulate method. This is not an issue to a lot of PCI device
drivers, which destroy and re-create the interrupts across hibernation, so
hv_compose_msi_msg() is called automatically. However, some PCI device
drivers (e.g. the in-tree GPU driver nouveau and the out-of-tree Nvidia
proprietary GPU driver) do not destroy and re-create MSI/MSI-X interrupts
across hibernation, so hv_pci_resume() has to call hv_compose_msi_msg(),
otherwise the PCI device drivers can no longer receive interrupts after
the VM resumes from hibernation.
Hyper-V is also different in that chip->irq_unmask() may fail in a
Linux VM running on Hyper-V (on a physical machine, chip->irq_unmask()
can not fail because unmasking an MSI/MSI-X register just means an MMIO
write): during hibernation, when a CPU is offlined, the kernel tries
to move the interrupt to the remaining CPUs that haven't been offlined
yet. In this case, hv_irq_unmask() -> hv_do_hypercall() always fails
because the vmbus channel has been closed: here the early "return" in
hv_irq_unmask() means the pci_msi_unmask_irq() is not called, i.e. the
desc->masked remains "true", so later after hibernation, the MSI interrupt
always remains masked, which is incorrect. Refer to cpu_disable_common()
-> fixup_irqs() -> irq_migrate_all_off_this_cpu() -> migrate_one_irq():
static bool migrate_one_irq(struct irq_desc *desc)
{
...
if (maskchip && chip->irq_mask)
chip->irq_mask(d);
...
err = irq_do_set_affinity(d, affinity, false);
...
if (maskchip && chip->irq_unmask)
chip->irq_unmask(d);
Fix the issue by calling pci_msi_unmask_irq() unconditionally in
hv_irq_unmask(). Also suppress the error message for hibernation because
the hypercall failure during hibernation does not matter (at this time
all the devices have been frozen). Note: the correct affinity info is
still updated into the irqdata data structure in migrate_one_irq() ->
irq_do_set_affinity() -> hv_set_affinity(), so later when the VM
resumes, hv_pci_restore_msi_state() is able to correctly restore
the interrupt with the correct affinity.
Link: https://lore.kernel.org/r/20201002085158.9168-1-decui@microsoft.com
Fixes: ac82fc832708 ("PCI: hv: Add hibernation support")
Signed-off-by: Dexuan Cui <decui@microsoft.com>
Signed-off-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Reviewed-by: Jake Oshins <jakeo@microsoft.com>
2020-10-02 08:51:58 +00:00
|
|
|
static int hv_pci_restore_msi_msg(struct pci_dev *pdev, void *arg)
|
|
|
|
{
|
|
|
|
struct irq_data *irq_data;
|
2021-12-06 22:51:33 +00:00
|
|
|
struct msi_desc *entry;
|
|
|
|
int ret = 0;
|
PCI: hv: Fix hibernation in case interrupts are not re-created
pci_restore_msi_state() directly writes the MSI/MSI-X related registers
via MMIO. On a physical machine, this works perfectly; for a Linux VM
running on a hypervisor, which typically enables IOMMU interrupt remapping,
the hypervisor usually should trap and emulate the MMIO accesses in order
to re-create the necessary interrupt remapping table entries in the IOMMU,
otherwise the interrupts can not work in the VM after hibernation.
Hyper-V is different from other hypervisors in that it does not trap and
emulate the MMIO accesses, and instead it uses a para-virtualized method,
which requires the VM to call hv_compose_msi_msg() to notify the hypervisor
of the info that would be passed to the hypervisor in the case of the
trap-and-emulate method. This is not an issue to a lot of PCI device
drivers, which destroy and re-create the interrupts across hibernation, so
hv_compose_msi_msg() is called automatically. However, some PCI device
drivers (e.g. the in-tree GPU driver nouveau and the out-of-tree Nvidia
proprietary GPU driver) do not destroy and re-create MSI/MSI-X interrupts
across hibernation, so hv_pci_resume() has to call hv_compose_msi_msg(),
otherwise the PCI device drivers can no longer receive interrupts after
the VM resumes from hibernation.
Hyper-V is also different in that chip->irq_unmask() may fail in a
Linux VM running on Hyper-V (on a physical machine, chip->irq_unmask()
can not fail because unmasking an MSI/MSI-X register just means an MMIO
write): during hibernation, when a CPU is offlined, the kernel tries
to move the interrupt to the remaining CPUs that haven't been offlined
yet. In this case, hv_irq_unmask() -> hv_do_hypercall() always fails
because the vmbus channel has been closed: here the early "return" in
hv_irq_unmask() means the pci_msi_unmask_irq() is not called, i.e. the
desc->masked remains "true", so later after hibernation, the MSI interrupt
always remains masked, which is incorrect. Refer to cpu_disable_common()
-> fixup_irqs() -> irq_migrate_all_off_this_cpu() -> migrate_one_irq():
static bool migrate_one_irq(struct irq_desc *desc)
{
...
if (maskchip && chip->irq_mask)
chip->irq_mask(d);
...
err = irq_do_set_affinity(d, affinity, false);
...
if (maskchip && chip->irq_unmask)
chip->irq_unmask(d);
Fix the issue by calling pci_msi_unmask_irq() unconditionally in
hv_irq_unmask(). Also suppress the error message for hibernation because
the hypercall failure during hibernation does not matter (at this time
all the devices have been frozen). Note: the correct affinity info is
still updated into the irqdata data structure in migrate_one_irq() ->
irq_do_set_affinity() -> hv_set_affinity(), so later when the VM
resumes, hv_pci_restore_msi_state() is able to correctly restore
the interrupt with the correct affinity.
Link: https://lore.kernel.org/r/20201002085158.9168-1-decui@microsoft.com
Fixes: ac82fc832708 ("PCI: hv: Add hibernation support")
Signed-off-by: Dexuan Cui <decui@microsoft.com>
Signed-off-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Reviewed-by: Jake Oshins <jakeo@microsoft.com>
2020-10-02 08:51:58 +00:00
|
|
|
|
2021-12-06 22:51:33 +00:00
|
|
|
msi_lock_descs(&pdev->dev);
|
|
|
|
msi_for_each_desc(entry, &pdev->dev, MSI_DESC_ASSOCIATED) {
|
PCI: hv: Fix hibernation in case interrupts are not re-created
pci_restore_msi_state() directly writes the MSI/MSI-X related registers
via MMIO. On a physical machine, this works perfectly; for a Linux VM
running on a hypervisor, which typically enables IOMMU interrupt remapping,
the hypervisor usually should trap and emulate the MMIO accesses in order
to re-create the necessary interrupt remapping table entries in the IOMMU,
otherwise the interrupts can not work in the VM after hibernation.
Hyper-V is different from other hypervisors in that it does not trap and
emulate the MMIO accesses, and instead it uses a para-virtualized method,
which requires the VM to call hv_compose_msi_msg() to notify the hypervisor
of the info that would be passed to the hypervisor in the case of the
trap-and-emulate method. This is not an issue to a lot of PCI device
drivers, which destroy and re-create the interrupts across hibernation, so
hv_compose_msi_msg() is called automatically. However, some PCI device
drivers (e.g. the in-tree GPU driver nouveau and the out-of-tree Nvidia
proprietary GPU driver) do not destroy and re-create MSI/MSI-X interrupts
across hibernation, so hv_pci_resume() has to call hv_compose_msi_msg(),
otherwise the PCI device drivers can no longer receive interrupts after
the VM resumes from hibernation.
Hyper-V is also different in that chip->irq_unmask() may fail in a
Linux VM running on Hyper-V (on a physical machine, chip->irq_unmask()
can not fail because unmasking an MSI/MSI-X register just means an MMIO
write): during hibernation, when a CPU is offlined, the kernel tries
to move the interrupt to the remaining CPUs that haven't been offlined
yet. In this case, hv_irq_unmask() -> hv_do_hypercall() always fails
because the vmbus channel has been closed: here the early "return" in
hv_irq_unmask() means the pci_msi_unmask_irq() is not called, i.e. the
desc->masked remains "true", so later after hibernation, the MSI interrupt
always remains masked, which is incorrect. Refer to cpu_disable_common()
-> fixup_irqs() -> irq_migrate_all_off_this_cpu() -> migrate_one_irq():
static bool migrate_one_irq(struct irq_desc *desc)
{
...
if (maskchip && chip->irq_mask)
chip->irq_mask(d);
...
err = irq_do_set_affinity(d, affinity, false);
...
if (maskchip && chip->irq_unmask)
chip->irq_unmask(d);
Fix the issue by calling pci_msi_unmask_irq() unconditionally in
hv_irq_unmask(). Also suppress the error message for hibernation because
the hypercall failure during hibernation does not matter (at this time
all the devices have been frozen). Note: the correct affinity info is
still updated into the irqdata data structure in migrate_one_irq() ->
irq_do_set_affinity() -> hv_set_affinity(), so later when the VM
resumes, hv_pci_restore_msi_state() is able to correctly restore
the interrupt with the correct affinity.
Link: https://lore.kernel.org/r/20201002085158.9168-1-decui@microsoft.com
Fixes: ac82fc832708 ("PCI: hv: Add hibernation support")
Signed-off-by: Dexuan Cui <decui@microsoft.com>
Signed-off-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Reviewed-by: Jake Oshins <jakeo@microsoft.com>
2020-10-02 08:51:58 +00:00
|
|
|
irq_data = irq_get_irq_data(entry->irq);
|
2021-12-06 22:51:33 +00:00
|
|
|
if (WARN_ON_ONCE(!irq_data)) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
PCI: hv: Fix hibernation in case interrupts are not re-created
pci_restore_msi_state() directly writes the MSI/MSI-X related registers
via MMIO. On a physical machine, this works perfectly; for a Linux VM
running on a hypervisor, which typically enables IOMMU interrupt remapping,
the hypervisor usually should trap and emulate the MMIO accesses in order
to re-create the necessary interrupt remapping table entries in the IOMMU,
otherwise the interrupts can not work in the VM after hibernation.
Hyper-V is different from other hypervisors in that it does not trap and
emulate the MMIO accesses, and instead it uses a para-virtualized method,
which requires the VM to call hv_compose_msi_msg() to notify the hypervisor
of the info that would be passed to the hypervisor in the case of the
trap-and-emulate method. This is not an issue to a lot of PCI device
drivers, which destroy and re-create the interrupts across hibernation, so
hv_compose_msi_msg() is called automatically. However, some PCI device
drivers (e.g. the in-tree GPU driver nouveau and the out-of-tree Nvidia
proprietary GPU driver) do not destroy and re-create MSI/MSI-X interrupts
across hibernation, so hv_pci_resume() has to call hv_compose_msi_msg(),
otherwise the PCI device drivers can no longer receive interrupts after
the VM resumes from hibernation.
Hyper-V is also different in that chip->irq_unmask() may fail in a
Linux VM running on Hyper-V (on a physical machine, chip->irq_unmask()
can not fail because unmasking an MSI/MSI-X register just means an MMIO
write): during hibernation, when a CPU is offlined, the kernel tries
to move the interrupt to the remaining CPUs that haven't been offlined
yet. In this case, hv_irq_unmask() -> hv_do_hypercall() always fails
because the vmbus channel has been closed: here the early "return" in
hv_irq_unmask() means the pci_msi_unmask_irq() is not called, i.e. the
desc->masked remains "true", so later after hibernation, the MSI interrupt
always remains masked, which is incorrect. Refer to cpu_disable_common()
-> fixup_irqs() -> irq_migrate_all_off_this_cpu() -> migrate_one_irq():
static bool migrate_one_irq(struct irq_desc *desc)
{
...
if (maskchip && chip->irq_mask)
chip->irq_mask(d);
...
err = irq_do_set_affinity(d, affinity, false);
...
if (maskchip && chip->irq_unmask)
chip->irq_unmask(d);
Fix the issue by calling pci_msi_unmask_irq() unconditionally in
hv_irq_unmask(). Also suppress the error message for hibernation because
the hypercall failure during hibernation does not matter (at this time
all the devices have been frozen). Note: the correct affinity info is
still updated into the irqdata data structure in migrate_one_irq() ->
irq_do_set_affinity() -> hv_set_affinity(), so later when the VM
resumes, hv_pci_restore_msi_state() is able to correctly restore
the interrupt with the correct affinity.
Link: https://lore.kernel.org/r/20201002085158.9168-1-decui@microsoft.com
Fixes: ac82fc832708 ("PCI: hv: Add hibernation support")
Signed-off-by: Dexuan Cui <decui@microsoft.com>
Signed-off-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Reviewed-by: Jake Oshins <jakeo@microsoft.com>
2020-10-02 08:51:58 +00:00
|
|
|
|
|
|
|
hv_compose_msi_msg(irq_data, &entry->msg);
|
|
|
|
}
|
2021-12-06 22:51:33 +00:00
|
|
|
msi_unlock_descs(&pdev->dev);
|
PCI: hv: Fix hibernation in case interrupts are not re-created
pci_restore_msi_state() directly writes the MSI/MSI-X related registers
via MMIO. On a physical machine, this works perfectly; for a Linux VM
running on a hypervisor, which typically enables IOMMU interrupt remapping,
the hypervisor usually should trap and emulate the MMIO accesses in order
to re-create the necessary interrupt remapping table entries in the IOMMU,
otherwise the interrupts can not work in the VM after hibernation.
Hyper-V is different from other hypervisors in that it does not trap and
emulate the MMIO accesses, and instead it uses a para-virtualized method,
which requires the VM to call hv_compose_msi_msg() to notify the hypervisor
of the info that would be passed to the hypervisor in the case of the
trap-and-emulate method. This is not an issue to a lot of PCI device
drivers, which destroy and re-create the interrupts across hibernation, so
hv_compose_msi_msg() is called automatically. However, some PCI device
drivers (e.g. the in-tree GPU driver nouveau and the out-of-tree Nvidia
proprietary GPU driver) do not destroy and re-create MSI/MSI-X interrupts
across hibernation, so hv_pci_resume() has to call hv_compose_msi_msg(),
otherwise the PCI device drivers can no longer receive interrupts after
the VM resumes from hibernation.
Hyper-V is also different in that chip->irq_unmask() may fail in a
Linux VM running on Hyper-V (on a physical machine, chip->irq_unmask()
can not fail because unmasking an MSI/MSI-X register just means an MMIO
write): during hibernation, when a CPU is offlined, the kernel tries
to move the interrupt to the remaining CPUs that haven't been offlined
yet. In this case, hv_irq_unmask() -> hv_do_hypercall() always fails
because the vmbus channel has been closed: here the early "return" in
hv_irq_unmask() means the pci_msi_unmask_irq() is not called, i.e. the
desc->masked remains "true", so later after hibernation, the MSI interrupt
always remains masked, which is incorrect. Refer to cpu_disable_common()
-> fixup_irqs() -> irq_migrate_all_off_this_cpu() -> migrate_one_irq():
static bool migrate_one_irq(struct irq_desc *desc)
{
...
if (maskchip && chip->irq_mask)
chip->irq_mask(d);
...
err = irq_do_set_affinity(d, affinity, false);
...
if (maskchip && chip->irq_unmask)
chip->irq_unmask(d);
Fix the issue by calling pci_msi_unmask_irq() unconditionally in
hv_irq_unmask(). Also suppress the error message for hibernation because
the hypercall failure during hibernation does not matter (at this time
all the devices have been frozen). Note: the correct affinity info is
still updated into the irqdata data structure in migrate_one_irq() ->
irq_do_set_affinity() -> hv_set_affinity(), so later when the VM
resumes, hv_pci_restore_msi_state() is able to correctly restore
the interrupt with the correct affinity.
Link: https://lore.kernel.org/r/20201002085158.9168-1-decui@microsoft.com
Fixes: ac82fc832708 ("PCI: hv: Add hibernation support")
Signed-off-by: Dexuan Cui <decui@microsoft.com>
Signed-off-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Reviewed-by: Jake Oshins <jakeo@microsoft.com>
2020-10-02 08:51:58 +00:00
|
|
|
|
2021-12-06 22:51:33 +00:00
|
|
|
return ret;
|
PCI: hv: Fix hibernation in case interrupts are not re-created
pci_restore_msi_state() directly writes the MSI/MSI-X related registers
via MMIO. On a physical machine, this works perfectly; for a Linux VM
running on a hypervisor, which typically enables IOMMU interrupt remapping,
the hypervisor usually should trap and emulate the MMIO accesses in order
to re-create the necessary interrupt remapping table entries in the IOMMU,
otherwise the interrupts can not work in the VM after hibernation.
Hyper-V is different from other hypervisors in that it does not trap and
emulate the MMIO accesses, and instead it uses a para-virtualized method,
which requires the VM to call hv_compose_msi_msg() to notify the hypervisor
of the info that would be passed to the hypervisor in the case of the
trap-and-emulate method. This is not an issue to a lot of PCI device
drivers, which destroy and re-create the interrupts across hibernation, so
hv_compose_msi_msg() is called automatically. However, some PCI device
drivers (e.g. the in-tree GPU driver nouveau and the out-of-tree Nvidia
proprietary GPU driver) do not destroy and re-create MSI/MSI-X interrupts
across hibernation, so hv_pci_resume() has to call hv_compose_msi_msg(),
otherwise the PCI device drivers can no longer receive interrupts after
the VM resumes from hibernation.
Hyper-V is also different in that chip->irq_unmask() may fail in a
Linux VM running on Hyper-V (on a physical machine, chip->irq_unmask()
can not fail because unmasking an MSI/MSI-X register just means an MMIO
write): during hibernation, when a CPU is offlined, the kernel tries
to move the interrupt to the remaining CPUs that haven't been offlined
yet. In this case, hv_irq_unmask() -> hv_do_hypercall() always fails
because the vmbus channel has been closed: here the early "return" in
hv_irq_unmask() means the pci_msi_unmask_irq() is not called, i.e. the
desc->masked remains "true", so later after hibernation, the MSI interrupt
always remains masked, which is incorrect. Refer to cpu_disable_common()
-> fixup_irqs() -> irq_migrate_all_off_this_cpu() -> migrate_one_irq():
static bool migrate_one_irq(struct irq_desc *desc)
{
...
if (maskchip && chip->irq_mask)
chip->irq_mask(d);
...
err = irq_do_set_affinity(d, affinity, false);
...
if (maskchip && chip->irq_unmask)
chip->irq_unmask(d);
Fix the issue by calling pci_msi_unmask_irq() unconditionally in
hv_irq_unmask(). Also suppress the error message for hibernation because
the hypercall failure during hibernation does not matter (at this time
all the devices have been frozen). Note: the correct affinity info is
still updated into the irqdata data structure in migrate_one_irq() ->
irq_do_set_affinity() -> hv_set_affinity(), so later when the VM
resumes, hv_pci_restore_msi_state() is able to correctly restore
the interrupt with the correct affinity.
Link: https://lore.kernel.org/r/20201002085158.9168-1-decui@microsoft.com
Fixes: ac82fc832708 ("PCI: hv: Add hibernation support")
Signed-off-by: Dexuan Cui <decui@microsoft.com>
Signed-off-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Reviewed-by: Jake Oshins <jakeo@microsoft.com>
2020-10-02 08:51:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Upon resume, pci_restore_msi_state() -> ... -> __pci_write_msi_msg()
|
|
|
|
* directly writes the MSI/MSI-X registers via MMIO, but since Hyper-V
|
|
|
|
* doesn't trap and emulate the MMIO accesses, here hv_compose_msi_msg()
|
|
|
|
* must be used to ask Hyper-V to re-create the IOMMU Interrupt Remapping
|
|
|
|
* Table entries.
|
|
|
|
*/
|
|
|
|
static void hv_pci_restore_msi_state(struct hv_pcibus_device *hbus)
|
|
|
|
{
|
2021-07-26 18:06:54 +00:00
|
|
|
pci_walk_bus(hbus->bridge->bus, hv_pci_restore_msi_msg, NULL);
|
PCI: hv: Fix hibernation in case interrupts are not re-created
pci_restore_msi_state() directly writes the MSI/MSI-X related registers
via MMIO. On a physical machine, this works perfectly; for a Linux VM
running on a hypervisor, which typically enables IOMMU interrupt remapping,
the hypervisor usually should trap and emulate the MMIO accesses in order
to re-create the necessary interrupt remapping table entries in the IOMMU,
otherwise the interrupts can not work in the VM after hibernation.
Hyper-V is different from other hypervisors in that it does not trap and
emulate the MMIO accesses, and instead it uses a para-virtualized method,
which requires the VM to call hv_compose_msi_msg() to notify the hypervisor
of the info that would be passed to the hypervisor in the case of the
trap-and-emulate method. This is not an issue to a lot of PCI device
drivers, which destroy and re-create the interrupts across hibernation, so
hv_compose_msi_msg() is called automatically. However, some PCI device
drivers (e.g. the in-tree GPU driver nouveau and the out-of-tree Nvidia
proprietary GPU driver) do not destroy and re-create MSI/MSI-X interrupts
across hibernation, so hv_pci_resume() has to call hv_compose_msi_msg(),
otherwise the PCI device drivers can no longer receive interrupts after
the VM resumes from hibernation.
Hyper-V is also different in that chip->irq_unmask() may fail in a
Linux VM running on Hyper-V (on a physical machine, chip->irq_unmask()
can not fail because unmasking an MSI/MSI-X register just means an MMIO
write): during hibernation, when a CPU is offlined, the kernel tries
to move the interrupt to the remaining CPUs that haven't been offlined
yet. In this case, hv_irq_unmask() -> hv_do_hypercall() always fails
because the vmbus channel has been closed: here the early "return" in
hv_irq_unmask() means the pci_msi_unmask_irq() is not called, i.e. the
desc->masked remains "true", so later after hibernation, the MSI interrupt
always remains masked, which is incorrect. Refer to cpu_disable_common()
-> fixup_irqs() -> irq_migrate_all_off_this_cpu() -> migrate_one_irq():
static bool migrate_one_irq(struct irq_desc *desc)
{
...
if (maskchip && chip->irq_mask)
chip->irq_mask(d);
...
err = irq_do_set_affinity(d, affinity, false);
...
if (maskchip && chip->irq_unmask)
chip->irq_unmask(d);
Fix the issue by calling pci_msi_unmask_irq() unconditionally in
hv_irq_unmask(). Also suppress the error message for hibernation because
the hypercall failure during hibernation does not matter (at this time
all the devices have been frozen). Note: the correct affinity info is
still updated into the irqdata data structure in migrate_one_irq() ->
irq_do_set_affinity() -> hv_set_affinity(), so later when the VM
resumes, hv_pci_restore_msi_state() is able to correctly restore
the interrupt with the correct affinity.
Link: https://lore.kernel.org/r/20201002085158.9168-1-decui@microsoft.com
Fixes: ac82fc832708 ("PCI: hv: Add hibernation support")
Signed-off-by: Dexuan Cui <decui@microsoft.com>
Signed-off-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Reviewed-by: Jake Oshins <jakeo@microsoft.com>
2020-10-02 08:51:58 +00:00
|
|
|
}
|
|
|
|
|
PCI: hv: Add hibernation support
Add suspend() and resume() functions so that Hyper-V virtual PCI devices
are handled properly when the VM hibernates and resumes from
hibernation.
Note that the suspend() function must make sure there are no pending
work items before calling vmbus_close(), since it runs in a process
context as a callback in dpm_suspend(). When it starts to run, the
channel callback hv_pci_onchannelcallback(), which runs in a tasklet
context, can be still running concurrently and scheduling new work items
onto hbus->wq in hv_pci_devices_present() and hv_pci_eject_device(), and
the work item handlers can access the vmbus channel, which can be being
closed by hv_pci_suspend(), e.g. the work item handler
pci_devices_present_work() -> new_pcichild_device() writes to the vmbus
channel.
To eliminate the race, hv_pci_suspend() disables the channel callback
tasklet, sets hbus->state to hv_pcibus_removing, and re-enables the
tasklet. This way, when hv_pci_suspend() proceeds, it knows that no new
work item can be scheduled, and then it flushes hbus->wq and safely
closes the vmbus channel.
Signed-off-by: Dexuan Cui <decui@microsoft.com>
Signed-off-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Reviewed-by: Michael Kelley <mikelley@microsoft.com>
2019-11-25 05:33:52 +00:00
|
|
|
static int hv_pci_resume(struct hv_device *hdev)
|
|
|
|
{
|
|
|
|
struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
|
|
|
|
enum pci_protocol_version_t version[1];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
hbus->state = hv_pcibus_init;
|
|
|
|
|
2022-04-19 12:23:21 +00:00
|
|
|
hdev->channel->next_request_id_callback = vmbus_next_request_id;
|
|
|
|
hdev->channel->request_addr_callback = vmbus_request_addr;
|
|
|
|
hdev->channel->rqstor_size = HV_PCI_RQSTOR_SIZE;
|
|
|
|
|
PCI: hv: Add hibernation support
Add suspend() and resume() functions so that Hyper-V virtual PCI devices
are handled properly when the VM hibernates and resumes from
hibernation.
Note that the suspend() function must make sure there are no pending
work items before calling vmbus_close(), since it runs in a process
context as a callback in dpm_suspend(). When it starts to run, the
channel callback hv_pci_onchannelcallback(), which runs in a tasklet
context, can be still running concurrently and scheduling new work items
onto hbus->wq in hv_pci_devices_present() and hv_pci_eject_device(), and
the work item handlers can access the vmbus channel, which can be being
closed by hv_pci_suspend(), e.g. the work item handler
pci_devices_present_work() -> new_pcichild_device() writes to the vmbus
channel.
To eliminate the race, hv_pci_suspend() disables the channel callback
tasklet, sets hbus->state to hv_pcibus_removing, and re-enables the
tasklet. This way, when hv_pci_suspend() proceeds, it knows that no new
work item can be scheduled, and then it flushes hbus->wq and safely
closes the vmbus channel.
Signed-off-by: Dexuan Cui <decui@microsoft.com>
Signed-off-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Reviewed-by: Michael Kelley <mikelley@microsoft.com>
2019-11-25 05:33:52 +00:00
|
|
|
ret = vmbus_open(hdev->channel, pci_ring_size, pci_ring_size, NULL, 0,
|
|
|
|
hv_pci_onchannelcallback, hbus);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* Only use the version that was in use before hibernation. */
|
2019-11-25 05:33:53 +00:00
|
|
|
version[0] = hbus->protocol_version;
|
PCI: hv: Add hibernation support
Add suspend() and resume() functions so that Hyper-V virtual PCI devices
are handled properly when the VM hibernates and resumes from
hibernation.
Note that the suspend() function must make sure there are no pending
work items before calling vmbus_close(), since it runs in a process
context as a callback in dpm_suspend(). When it starts to run, the
channel callback hv_pci_onchannelcallback(), which runs in a tasklet
context, can be still running concurrently and scheduling new work items
onto hbus->wq in hv_pci_devices_present() and hv_pci_eject_device(), and
the work item handlers can access the vmbus channel, which can be being
closed by hv_pci_suspend(), e.g. the work item handler
pci_devices_present_work() -> new_pcichild_device() writes to the vmbus
channel.
To eliminate the race, hv_pci_suspend() disables the channel callback
tasklet, sets hbus->state to hv_pcibus_removing, and re-enables the
tasklet. This way, when hv_pci_suspend() proceeds, it knows that no new
work item can be scheduled, and then it flushes hbus->wq and safely
closes the vmbus channel.
Signed-off-by: Dexuan Cui <decui@microsoft.com>
Signed-off-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Reviewed-by: Michael Kelley <mikelley@microsoft.com>
2019-11-25 05:33:52 +00:00
|
|
|
ret = hv_pci_protocol_negotiation(hdev, version, 1);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
ret = hv_pci_query_relations(hdev);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
ret = hv_pci_enter_d0(hdev);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
ret = hv_send_resources_allocated(hdev);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
prepopulate_bars(hbus);
|
|
|
|
|
PCI: hv: Fix hibernation in case interrupts are not re-created
pci_restore_msi_state() directly writes the MSI/MSI-X related registers
via MMIO. On a physical machine, this works perfectly; for a Linux VM
running on a hypervisor, which typically enables IOMMU interrupt remapping,
the hypervisor usually should trap and emulate the MMIO accesses in order
to re-create the necessary interrupt remapping table entries in the IOMMU,
otherwise the interrupts can not work in the VM after hibernation.
Hyper-V is different from other hypervisors in that it does not trap and
emulate the MMIO accesses, and instead it uses a para-virtualized method,
which requires the VM to call hv_compose_msi_msg() to notify the hypervisor
of the info that would be passed to the hypervisor in the case of the
trap-and-emulate method. This is not an issue to a lot of PCI device
drivers, which destroy and re-create the interrupts across hibernation, so
hv_compose_msi_msg() is called automatically. However, some PCI device
drivers (e.g. the in-tree GPU driver nouveau and the out-of-tree Nvidia
proprietary GPU driver) do not destroy and re-create MSI/MSI-X interrupts
across hibernation, so hv_pci_resume() has to call hv_compose_msi_msg(),
otherwise the PCI device drivers can no longer receive interrupts after
the VM resumes from hibernation.
Hyper-V is also different in that chip->irq_unmask() may fail in a
Linux VM running on Hyper-V (on a physical machine, chip->irq_unmask()
can not fail because unmasking an MSI/MSI-X register just means an MMIO
write): during hibernation, when a CPU is offlined, the kernel tries
to move the interrupt to the remaining CPUs that haven't been offlined
yet. In this case, hv_irq_unmask() -> hv_do_hypercall() always fails
because the vmbus channel has been closed: here the early "return" in
hv_irq_unmask() means the pci_msi_unmask_irq() is not called, i.e. the
desc->masked remains "true", so later after hibernation, the MSI interrupt
always remains masked, which is incorrect. Refer to cpu_disable_common()
-> fixup_irqs() -> irq_migrate_all_off_this_cpu() -> migrate_one_irq():
static bool migrate_one_irq(struct irq_desc *desc)
{
...
if (maskchip && chip->irq_mask)
chip->irq_mask(d);
...
err = irq_do_set_affinity(d, affinity, false);
...
if (maskchip && chip->irq_unmask)
chip->irq_unmask(d);
Fix the issue by calling pci_msi_unmask_irq() unconditionally in
hv_irq_unmask(). Also suppress the error message for hibernation because
the hypercall failure during hibernation does not matter (at this time
all the devices have been frozen). Note: the correct affinity info is
still updated into the irqdata data structure in migrate_one_irq() ->
irq_do_set_affinity() -> hv_set_affinity(), so later when the VM
resumes, hv_pci_restore_msi_state() is able to correctly restore
the interrupt with the correct affinity.
Link: https://lore.kernel.org/r/20201002085158.9168-1-decui@microsoft.com
Fixes: ac82fc832708 ("PCI: hv: Add hibernation support")
Signed-off-by: Dexuan Cui <decui@microsoft.com>
Signed-off-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Reviewed-by: Jake Oshins <jakeo@microsoft.com>
2020-10-02 08:51:58 +00:00
|
|
|
hv_pci_restore_msi_state(hbus);
|
|
|
|
|
PCI: hv: Add hibernation support
Add suspend() and resume() functions so that Hyper-V virtual PCI devices
are handled properly when the VM hibernates and resumes from
hibernation.
Note that the suspend() function must make sure there are no pending
work items before calling vmbus_close(), since it runs in a process
context as a callback in dpm_suspend(). When it starts to run, the
channel callback hv_pci_onchannelcallback(), which runs in a tasklet
context, can be still running concurrently and scheduling new work items
onto hbus->wq in hv_pci_devices_present() and hv_pci_eject_device(), and
the work item handlers can access the vmbus channel, which can be being
closed by hv_pci_suspend(), e.g. the work item handler
pci_devices_present_work() -> new_pcichild_device() writes to the vmbus
channel.
To eliminate the race, hv_pci_suspend() disables the channel callback
tasklet, sets hbus->state to hv_pcibus_removing, and re-enables the
tasklet. This way, when hv_pci_suspend() proceeds, it knows that no new
work item can be scheduled, and then it flushes hbus->wq and safely
closes the vmbus channel.
Signed-off-by: Dexuan Cui <decui@microsoft.com>
Signed-off-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Reviewed-by: Michael Kelley <mikelley@microsoft.com>
2019-11-25 05:33:52 +00:00
|
|
|
hbus->state = hv_pcibus_installed;
|
|
|
|
return 0;
|
|
|
|
out:
|
|
|
|
vmbus_close(hdev->channel);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-02-16 21:56:23 +00:00
|
|
|
static const struct hv_vmbus_device_id hv_pci_id_table[] = {
|
|
|
|
/* PCI Pass-through Class ID */
|
|
|
|
/* 44C4F61D-4444-4400-9D52-802E27EDE19F */
|
|
|
|
{ HV_PCIE_GUID, },
|
|
|
|
{ },
|
|
|
|
};
|
|
|
|
|
|
|
|
MODULE_DEVICE_TABLE(vmbus, hv_pci_id_table);
|
|
|
|
|
|
|
|
static struct hv_driver hv_pci_drv = {
|
|
|
|
.name = "hv_pci",
|
|
|
|
.id_table = hv_pci_id_table,
|
|
|
|
.probe = hv_pci_probe,
|
|
|
|
.remove = hv_pci_remove,
|
PCI: hv: Add hibernation support
Add suspend() and resume() functions so that Hyper-V virtual PCI devices
are handled properly when the VM hibernates and resumes from
hibernation.
Note that the suspend() function must make sure there are no pending
work items before calling vmbus_close(), since it runs in a process
context as a callback in dpm_suspend(). When it starts to run, the
channel callback hv_pci_onchannelcallback(), which runs in a tasklet
context, can be still running concurrently and scheduling new work items
onto hbus->wq in hv_pci_devices_present() and hv_pci_eject_device(), and
the work item handlers can access the vmbus channel, which can be being
closed by hv_pci_suspend(), e.g. the work item handler
pci_devices_present_work() -> new_pcichild_device() writes to the vmbus
channel.
To eliminate the race, hv_pci_suspend() disables the channel callback
tasklet, sets hbus->state to hv_pcibus_removing, and re-enables the
tasklet. This way, when hv_pci_suspend() proceeds, it knows that no new
work item can be scheduled, and then it flushes hbus->wq and safely
closes the vmbus channel.
Signed-off-by: Dexuan Cui <decui@microsoft.com>
Signed-off-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Reviewed-by: Michael Kelley <mikelley@microsoft.com>
2019-11-25 05:33:52 +00:00
|
|
|
.suspend = hv_pci_suspend,
|
|
|
|
.resume = hv_pci_resume,
|
2016-02-16 21:56:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void __exit exit_hv_pci_drv(void)
|
|
|
|
{
|
|
|
|
vmbus_driver_unregister(&hv_pci_drv);
|
2019-08-22 05:05:41 +00:00
|
|
|
|
|
|
|
hvpci_block_ops.read_block = NULL;
|
|
|
|
hvpci_block_ops.write_block = NULL;
|
|
|
|
hvpci_block_ops.reg_blk_invalidate = NULL;
|
2016-02-16 21:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int __init init_hv_pci_drv(void)
|
|
|
|
{
|
2022-01-05 19:32:35 +00:00
|
|
|
int ret;
|
|
|
|
|
2021-05-25 23:17:33 +00:00
|
|
|
if (!hv_is_hyperv_initialized())
|
|
|
|
return -ENODEV;
|
|
|
|
|
2022-01-05 19:32:35 +00:00
|
|
|
ret = hv_pci_irqchip_init();
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2019-08-15 17:01:37 +00:00
|
|
|
/* Set the invalid domain number's bit, so it will not be used */
|
|
|
|
set_bit(HVPCI_DOM_INVALID, hvpci_dom_map);
|
|
|
|
|
2019-08-22 05:05:41 +00:00
|
|
|
/* Initialize PCI block r/w interface */
|
|
|
|
hvpci_block_ops.read_block = hv_read_config_block;
|
|
|
|
hvpci_block_ops.write_block = hv_write_config_block;
|
|
|
|
hvpci_block_ops.reg_blk_invalidate = hv_register_block_invalidate;
|
|
|
|
|
2016-02-16 21:56:23 +00:00
|
|
|
return vmbus_driver_register(&hv_pci_drv);
|
|
|
|
}
|
|
|
|
|
|
|
|
module_init(init_hv_pci_drv);
|
|
|
|
module_exit(exit_hv_pci_drv);
|
|
|
|
|
|
|
|
MODULE_DESCRIPTION("Hyper-V PCI");
|
|
|
|
MODULE_LICENSE("GPL v2");
|