1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) Microsoft Corporation.
6 * Jake Oshins <jakeo@microsoft.com>
8 * This driver acts as a paravirtual front-end for PCI Express root buses.
9 * When a PCI Express function (either an entire device or an SR-IOV
10 * Virtual Function) is being passed through to the VM, this driver exposes
11 * a new bus to the guest VM. This is modeled as a root PCI bus because
12 * no bridges are being exposed to the VM. In fact, with a "Generation 2"
13 * VM within Hyper-V, there may seem to be no PCI bus at all in the VM
14 * until a device as been exposed using this driver.
16 * Each root PCI bus has its own PCI domain, which is called "Segment" in
17 * the PCI Firmware Specifications. Thus while each device passed through
18 * to the VM using this front-end will appear at "device 0", the domain will
19 * be unique. Typically, each bus will have one PCI function on it, though
20 * this driver does support more than one.
22 * In order to map the interrupts from the device through to the guest VM,
23 * this driver also implements an IRQ Domain, which handles interrupts (either
24 * MSI or MSI-X) associated with the functions on the bus. As interrupts are
25 * set up, torn down, or reaffined, this driver communicates with the
26 * underlying hypervisor to adjust the mappings in the I/O MMU so that each
27 * interrupt will be delivered to the correct virtual processor at the right
28 * vector. This driver does not support level-triggered (line-based)
29 * interrupts, and will report that the Interrupt Line register in the
30 * function's configuration space is zero.
32 * The rest of this driver mostly maps PCI concepts onto underlying Hyper-V
33 * facilities. For instance, the configuration space of a function exposed
34 * by Hyper-V is mapped into a single page of memory space, and the
35 * read and write handlers for config space must be aware of this mechanism.
36 * Similarly, device setup and teardown involves messages sent to and from
37 * the PCI back-end driver in Hyper-V.
40 #include <linux/kernel.h>
41 #include <linux/module.h>
42 #include <linux/pci.h>
43 #include <linux/delay.h>
44 #include <linux/semaphore.h>
45 #include <linux/irqdomain.h>
46 #include <asm/irqdomain.h>
48 #include <linux/irq.h>
49 #include <linux/msi.h>
50 #include <linux/hyperv.h>
51 #include <linux/refcount.h>
52 #include <asm/mshyperv.h>
55 * Protocol versions. The low word is the minor version, the high word the
59 #define PCI_MAKE_VERSION(major, minor) ((u32)(((major) << 16) | (minor)))
60 #define PCI_MAJOR_VERSION(version) ((u32)(version) >> 16)
61 #define PCI_MINOR_VERSION(version) ((u32)(version) & 0xff)
63 enum pci_protocol_version_t {
64 PCI_PROTOCOL_VERSION_1_1 = PCI_MAKE_VERSION(1, 1), /* Win10 */
65 PCI_PROTOCOL_VERSION_1_2 = PCI_MAKE_VERSION(1, 2), /* RS1 */
66 PCI_PROTOCOL_VERSION_1_3 = PCI_MAKE_VERSION(1, 3), /* Vibranium */
69 #define CPU_AFFINITY_ALL -1ULL
72 * Supported protocol versions in the order of probing - highest go
75 static enum pci_protocol_version_t pci_protocol_versions[] = {
76 PCI_PROTOCOL_VERSION_1_3,
77 PCI_PROTOCOL_VERSION_1_2,
78 PCI_PROTOCOL_VERSION_1_1,
81 #define PCI_CONFIG_MMIO_LENGTH 0x2000
82 #define CFG_PAGE_OFFSET 0x1000
83 #define CFG_PAGE_SIZE (PCI_CONFIG_MMIO_LENGTH - CFG_PAGE_OFFSET)
85 #define MAX_SUPPORTED_MSI_MESSAGES 0x400
87 #define STATUS_REVISION_MISMATCH 0xC0000059
89 /* space for 32bit serial number as string */
90 #define SLOT_NAME_SIZE 11
96 enum pci_message_type {
100 PCI_MESSAGE_BASE = 0x42490000,
101 PCI_BUS_RELATIONS = PCI_MESSAGE_BASE + 0,
102 PCI_QUERY_BUS_RELATIONS = PCI_MESSAGE_BASE + 1,
103 PCI_POWER_STATE_CHANGE = PCI_MESSAGE_BASE + 4,
104 PCI_QUERY_RESOURCE_REQUIREMENTS = PCI_MESSAGE_BASE + 5,
105 PCI_QUERY_RESOURCE_RESOURCES = PCI_MESSAGE_BASE + 6,
106 PCI_BUS_D0ENTRY = PCI_MESSAGE_BASE + 7,
107 PCI_BUS_D0EXIT = PCI_MESSAGE_BASE + 8,
108 PCI_READ_BLOCK = PCI_MESSAGE_BASE + 9,
109 PCI_WRITE_BLOCK = PCI_MESSAGE_BASE + 0xA,
110 PCI_EJECT = PCI_MESSAGE_BASE + 0xB,
111 PCI_QUERY_STOP = PCI_MESSAGE_BASE + 0xC,
112 PCI_REENABLE = PCI_MESSAGE_BASE + 0xD,
113 PCI_QUERY_STOP_FAILED = PCI_MESSAGE_BASE + 0xE,
114 PCI_EJECTION_COMPLETE = PCI_MESSAGE_BASE + 0xF,
115 PCI_RESOURCES_ASSIGNED = PCI_MESSAGE_BASE + 0x10,
116 PCI_RESOURCES_RELEASED = PCI_MESSAGE_BASE + 0x11,
117 PCI_INVALIDATE_BLOCK = PCI_MESSAGE_BASE + 0x12,
118 PCI_QUERY_PROTOCOL_VERSION = PCI_MESSAGE_BASE + 0x13,
119 PCI_CREATE_INTERRUPT_MESSAGE = PCI_MESSAGE_BASE + 0x14,
120 PCI_DELETE_INTERRUPT_MESSAGE = PCI_MESSAGE_BASE + 0x15,
121 PCI_RESOURCES_ASSIGNED2 = PCI_MESSAGE_BASE + 0x16,
122 PCI_CREATE_INTERRUPT_MESSAGE2 = PCI_MESSAGE_BASE + 0x17,
123 PCI_DELETE_INTERRUPT_MESSAGE2 = PCI_MESSAGE_BASE + 0x18, /* unused */
124 PCI_BUS_RELATIONS2 = PCI_MESSAGE_BASE + 0x19,
129 * Structures defining the virtual PCI Express protocol.
141 * Function numbers are 8-bits wide on Express, as interpreted through ARI,
142 * which is all this driver does. This representation is the one used in
143 * Windows, which is what is expected when sending this back and forth with
144 * the Hyper-V parent partition.
146 union win_slot_encoding {
156 * Pretty much as defined in the PCI Specifications.
158 struct pci_function_description {
159 u16 v_id; /* vendor ID */
160 u16 d_id; /* device ID */
166 union win_slot_encoding win_slot;
167 u32 ser; /* serial number */
170 enum pci_device_description_flags {
171 HV_PCI_DEVICE_FLAG_NONE = 0x0,
172 HV_PCI_DEVICE_FLAG_NUMA_AFFINITY = 0x1,
175 struct pci_function_description2 {
176 u16 v_id; /* vendor ID */
177 u16 d_id; /* device ID */
183 union win_slot_encoding win_slot;
184 u32 ser; /* serial number */
186 u16 virtual_numa_node;
193 * @delivery_mode: As defined in Intel's Programmer's
194 * Reference Manual, Volume 3, Chapter 8.
195 * @vector_count: Number of contiguous entries in the
196 * Interrupt Descriptor Table that are
197 * occupied by this Message-Signaled
198 * Interrupt. For "MSI", as first defined
199 * in PCI 2.2, this can be between 1 and
200 * 32. For "MSI-X," as first defined in PCI
201 * 3.0, this must be 1, as each MSI-X table
202 * entry would have its own descriptor.
203 * @reserved: Empty space
204 * @cpu_mask: All the target virtual processors.
215 * struct hv_msi_desc2 - 1.2 version of hv_msi_desc
217 * @delivery_mode: As defined in Intel's Programmer's
218 * Reference Manual, Volume 3, Chapter 8.
219 * @vector_count: Number of contiguous entries in the
220 * Interrupt Descriptor Table that are
221 * occupied by this Message-Signaled
222 * Interrupt. For "MSI", as first defined
223 * in PCI 2.2, this can be between 1 and
224 * 32. For "MSI-X," as first defined in PCI
225 * 3.0, this must be 1, as each MSI-X table
226 * entry would have its own descriptor.
227 * @processor_count: number of bits enabled in array.
228 * @processor_array: All the target virtual processors.
230 struct hv_msi_desc2 {
235 u16 processor_array[32];
239 * struct tran_int_desc
240 * @reserved: unused, padding
241 * @vector_count: same as in hv_msi_desc
242 * @data: This is the "data payload" value that is
243 * written by the device when it generates
244 * a message-signaled interrupt, either MSI
246 * @address: This is the address to which the data
247 * payload is written on interrupt
250 struct tran_int_desc {
258 * A generic message format for virtual PCI.
259 * Specific message formats are defined later in the file.
266 struct pci_child_message {
267 struct pci_message message_type;
268 union win_slot_encoding wslot;
271 struct pci_incoming_message {
272 struct vmpacket_descriptor hdr;
273 struct pci_message message_type;
276 struct pci_response {
277 struct vmpacket_descriptor hdr;
278 s32 status; /* negative values are failures */
282 void (*completion_func)(void *context, struct pci_response *resp,
283 int resp_packet_size);
286 struct pci_message message[];
290 * Specific message types supporting the PCI protocol.
294 * Version negotiation message. Sent from the guest to the host.
295 * The guest is free to try different versions until the host
296 * accepts the version.
298 * pci_version: The protocol version requested.
299 * is_last_attempt: If TRUE, this is the last version guest will request.
300 * reservedz: Reserved field, set to zero.
303 struct pci_version_request {
304 struct pci_message message_type;
305 u32 protocol_version;
309 * Bus D0 Entry. This is sent from the guest to the host when the virtual
310 * bus (PCI Express port) is ready for action.
313 struct pci_bus_d0_entry {
314 struct pci_message message_type;
319 struct pci_bus_relations {
320 struct pci_incoming_message incoming;
322 struct pci_function_description func[];
325 struct pci_bus_relations2 {
326 struct pci_incoming_message incoming;
328 struct pci_function_description2 func[];
331 struct pci_q_res_req_response {
332 struct vmpacket_descriptor hdr;
333 s32 status; /* negative values are failures */
334 u32 probed_bar[PCI_STD_NUM_BARS];
337 struct pci_set_power {
338 struct pci_message message_type;
339 union win_slot_encoding wslot;
340 u32 power_state; /* In Windows terms */
344 struct pci_set_power_response {
345 struct vmpacket_descriptor hdr;
346 s32 status; /* negative values are failures */
347 union win_slot_encoding wslot;
348 u32 resultant_state; /* In Windows terms */
352 struct pci_resources_assigned {
353 struct pci_message message_type;
354 union win_slot_encoding wslot;
355 u8 memory_range[0x14][6]; /* not used here */
360 struct pci_resources_assigned2 {
361 struct pci_message message_type;
362 union win_slot_encoding wslot;
363 u8 memory_range[0x14][6]; /* not used here */
364 u32 msi_descriptor_count;
368 struct pci_create_interrupt {
369 struct pci_message message_type;
370 union win_slot_encoding wslot;
371 struct hv_msi_desc int_desc;
374 struct pci_create_int_response {
375 struct pci_response response;
377 struct tran_int_desc int_desc;
380 struct pci_create_interrupt2 {
381 struct pci_message message_type;
382 union win_slot_encoding wslot;
383 struct hv_msi_desc2 int_desc;
386 struct pci_delete_interrupt {
387 struct pci_message message_type;
388 union win_slot_encoding wslot;
389 struct tran_int_desc int_desc;
393 * Note: the VM must pass a valid block id, wslot and bytes_requested.
395 struct pci_read_block {
396 struct pci_message message_type;
398 union win_slot_encoding wslot;
402 struct pci_read_block_response {
403 struct vmpacket_descriptor hdr;
405 u8 bytes[HV_CONFIG_BLOCK_SIZE_MAX];
409 * Note: the VM must pass a valid block id, wslot and byte_count.
411 struct pci_write_block {
412 struct pci_message message_type;
414 union win_slot_encoding wslot;
416 u8 bytes[HV_CONFIG_BLOCK_SIZE_MAX];
419 struct pci_dev_inval_block {
420 struct pci_incoming_message incoming;
421 union win_slot_encoding wslot;
425 struct pci_dev_incoming {
426 struct pci_incoming_message incoming;
427 union win_slot_encoding wslot;
430 struct pci_eject_response {
431 struct pci_message message_type;
432 union win_slot_encoding wslot;
436 static int pci_ring_size = (4 * PAGE_SIZE);
439 * Driver specific state.
442 enum hv_pcibus_state {
451 struct hv_pcibus_device {
452 struct pci_sysdata sysdata;
453 /* Protocol version negotiated with the host */
454 enum pci_protocol_version_t protocol_version;
455 enum hv_pcibus_state state;
456 refcount_t remove_lock;
457 struct hv_device *hdev;
458 resource_size_t low_mmio_space;
459 resource_size_t high_mmio_space;
460 struct resource *mem_config;
461 struct resource *low_mmio_res;
462 struct resource *high_mmio_res;
463 struct completion *survey_event;
464 struct completion remove_event;
465 struct pci_bus *pci_bus;
466 spinlock_t config_lock; /* Avoid two threads writing index page */
467 spinlock_t device_list_lock; /* Protect lists below */
468 void __iomem *cfg_addr;
470 struct list_head resources_for_children;
472 struct list_head children;
473 struct list_head dr_list;
475 struct msi_domain_info msi_info;
476 struct msi_controller msi_chip;
477 struct irq_domain *irq_domain;
479 spinlock_t retarget_msi_interrupt_lock;
481 struct workqueue_struct *wq;
483 /* hypercall arg, must not cross page boundary */
484 struct hv_retarget_device_interrupt retarget_msi_interrupt_params;
487 * Don't put anything here: retarget_msi_interrupt_params must be last
492 * Tracks "Device Relations" messages from the host, which must be both
493 * processed in order and deferred so that they don't run in the context
494 * of the incoming packet callback.
497 struct work_struct wrk;
498 struct hv_pcibus_device *bus;
501 struct hv_pcidev_description {
502 u16 v_id; /* vendor ID */
503 u16 d_id; /* device ID */
509 union win_slot_encoding win_slot;
510 u32 ser; /* serial number */
512 u16 virtual_numa_node;
516 struct list_head list_entry;
518 struct hv_pcidev_description func[];
521 enum hv_pcichild_state {
522 hv_pcichild_init = 0,
523 hv_pcichild_requirements,
524 hv_pcichild_resourced,
525 hv_pcichild_ejecting,
530 /* List protected by pci_rescan_remove_lock */
531 struct list_head list_entry;
533 enum hv_pcichild_state state;
534 struct pci_slot *pci_slot;
535 struct hv_pcidev_description desc;
536 bool reported_missing;
537 struct hv_pcibus_device *hbus;
538 struct work_struct wrk;
540 void (*block_invalidate)(void *context, u64 block_mask);
541 void *invalidate_context;
544 * What would be observed if one wrote 0xFFFFFFFF to a BAR and then
545 * read it back, for each of the BAR offsets within config space.
547 u32 probed_bar[PCI_STD_NUM_BARS];
550 struct hv_pci_compl {
551 struct completion host_event;
552 s32 completion_status;
555 static void hv_pci_onchannelcallback(void *context);
558 * hv_pci_generic_compl() - Invoked for a completion packet
559 * @context: Set up by the sender of the packet.
560 * @resp: The response packet
561 * @resp_packet_size: Size in bytes of the packet
563 * This function is used to trigger an event and report status
564 * for any message for which the completion packet contains a
565 * status and nothing else.
567 static void hv_pci_generic_compl(void *context, struct pci_response *resp,
568 int resp_packet_size)
570 struct hv_pci_compl *comp_pkt = context;
572 if (resp_packet_size >= offsetofend(struct pci_response, status))
573 comp_pkt->completion_status = resp->status;
575 comp_pkt->completion_status = -1;
577 complete(&comp_pkt->host_event);
580 static struct hv_pci_dev *get_pcichild_wslot(struct hv_pcibus_device *hbus,
583 static void get_pcichild(struct hv_pci_dev *hpdev)
585 refcount_inc(&hpdev->refs);
588 static void put_pcichild(struct hv_pci_dev *hpdev)
590 if (refcount_dec_and_test(&hpdev->refs))
594 static void get_hvpcibus(struct hv_pcibus_device *hv_pcibus);
595 static void put_hvpcibus(struct hv_pcibus_device *hv_pcibus);
598 * There is no good way to get notified from vmbus_onoffer_rescind(),
599 * so let's use polling here, since this is not a hot path.
601 static int wait_for_response(struct hv_device *hdev,
602 struct completion *comp)
605 if (hdev->channel->rescind) {
606 dev_warn_once(&hdev->device, "The device is gone.\n");
610 if (wait_for_completion_timeout(comp, HZ / 10))
618 * devfn_to_wslot() - Convert from Linux PCI slot to Windows
619 * @devfn: The Linux representation of PCI slot
621 * Windows uses a slightly different representation of PCI slot.
623 * Return: The Windows representation
625 static u32 devfn_to_wslot(int devfn)
627 union win_slot_encoding wslot;
630 wslot.bits.dev = PCI_SLOT(devfn);
631 wslot.bits.func = PCI_FUNC(devfn);
637 * wslot_to_devfn() - Convert from Windows PCI slot to Linux
638 * @wslot: The Windows representation of PCI slot
640 * Windows uses a slightly different representation of PCI slot.
642 * Return: The Linux representation
644 static int wslot_to_devfn(u32 wslot)
646 union win_slot_encoding slot_no;
648 slot_no.slot = wslot;
649 return PCI_DEVFN(slot_no.bits.dev, slot_no.bits.func);
653 * PCI Configuration Space for these root PCI buses is implemented as a pair
654 * of pages in memory-mapped I/O space. Writing to the first page chooses
655 * the PCI function being written or read. Once the first page has been
656 * written to, the following page maps in the entire configuration space of
661 * _hv_pcifront_read_config() - Internal PCI config read
662 * @hpdev: The PCI driver's representation of the device
663 * @where: Offset within config space
664 * @size: Size of the transfer
665 * @val: Pointer to the buffer receiving the data
667 static void _hv_pcifront_read_config(struct hv_pci_dev *hpdev, int where,
671 void __iomem *addr = hpdev->hbus->cfg_addr + CFG_PAGE_OFFSET + where;
674 * If the attempt is to read the IDs or the ROM BAR, simulate that.
676 if (where + size <= PCI_COMMAND) {
677 memcpy(val, ((u8 *)&hpdev->desc.v_id) + where, size);
678 } else if (where >= PCI_CLASS_REVISION && where + size <=
679 PCI_CACHE_LINE_SIZE) {
680 memcpy(val, ((u8 *)&hpdev->desc.rev) + where -
681 PCI_CLASS_REVISION, size);
682 } else if (where >= PCI_SUBSYSTEM_VENDOR_ID && where + size <=
684 memcpy(val, (u8 *)&hpdev->desc.subsystem_id + where -
685 PCI_SUBSYSTEM_VENDOR_ID, size);
686 } else if (where >= PCI_ROM_ADDRESS && where + size <=
687 PCI_CAPABILITY_LIST) {
688 /* ROM BARs are unimplemented */
690 } else if (where >= PCI_INTERRUPT_LINE && where + size <=
693 * Interrupt Line and Interrupt PIN are hard-wired to zero
694 * because this front-end only supports message-signaled
698 } else if (where + size <= CFG_PAGE_SIZE) {
699 spin_lock_irqsave(&hpdev->hbus->config_lock, flags);
700 /* Choose the function to be read. (See comment above) */
701 writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr);
702 /* Make sure the function was chosen before we start reading. */
704 /* Read from that function's config space. */
717 * Make sure the read was done before we release the spinlock
718 * allowing consecutive reads/writes.
721 spin_unlock_irqrestore(&hpdev->hbus->config_lock, flags);
723 dev_err(&hpdev->hbus->hdev->device,
724 "Attempt to read beyond a function's config space.\n");
728 static u16 hv_pcifront_get_vendor_id(struct hv_pci_dev *hpdev)
732 void __iomem *addr = hpdev->hbus->cfg_addr + CFG_PAGE_OFFSET +
735 spin_lock_irqsave(&hpdev->hbus->config_lock, flags);
737 /* Choose the function to be read. (See comment above) */
738 writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr);
739 /* Make sure the function was chosen before we start reading. */
741 /* Read from that function's config space. */
744 * mb() is not required here, because the spin_unlock_irqrestore()
748 spin_unlock_irqrestore(&hpdev->hbus->config_lock, flags);
754 * _hv_pcifront_write_config() - Internal PCI config write
755 * @hpdev: The PCI driver's representation of the device
756 * @where: Offset within config space
757 * @size: Size of the transfer
758 * @val: The data being transferred
760 static void _hv_pcifront_write_config(struct hv_pci_dev *hpdev, int where,
764 void __iomem *addr = hpdev->hbus->cfg_addr + CFG_PAGE_OFFSET + where;
766 if (where >= PCI_SUBSYSTEM_VENDOR_ID &&
767 where + size <= PCI_CAPABILITY_LIST) {
768 /* SSIDs and ROM BARs are read-only */
769 } else if (where >= PCI_COMMAND && where + size <= CFG_PAGE_SIZE) {
770 spin_lock_irqsave(&hpdev->hbus->config_lock, flags);
771 /* Choose the function to be written. (See comment above) */
772 writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr);
773 /* Make sure the function was chosen before we start writing. */
775 /* Write to that function's config space. */
788 * Make sure the write was done before we release the spinlock
789 * allowing consecutive reads/writes.
792 spin_unlock_irqrestore(&hpdev->hbus->config_lock, flags);
794 dev_err(&hpdev->hbus->hdev->device,
795 "Attempt to write beyond a function's config space.\n");
800 * hv_pcifront_read_config() - Read configuration space
801 * @bus: PCI Bus structure
802 * @devfn: Device/function
803 * @where: Offset from base
804 * @size: Byte/word/dword
805 * @val: Value to be read
807 * Return: PCIBIOS_SUCCESSFUL on success
808 * PCIBIOS_DEVICE_NOT_FOUND on failure
810 static int hv_pcifront_read_config(struct pci_bus *bus, unsigned int devfn,
811 int where, int size, u32 *val)
813 struct hv_pcibus_device *hbus =
814 container_of(bus->sysdata, struct hv_pcibus_device, sysdata);
815 struct hv_pci_dev *hpdev;
817 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(devfn));
819 return PCIBIOS_DEVICE_NOT_FOUND;
821 _hv_pcifront_read_config(hpdev, where, size, val);
824 return PCIBIOS_SUCCESSFUL;
828 * hv_pcifront_write_config() - Write configuration space
829 * @bus: PCI Bus structure
830 * @devfn: Device/function
831 * @where: Offset from base
832 * @size: Byte/word/dword
833 * @val: Value to be written to device
835 * Return: PCIBIOS_SUCCESSFUL on success
836 * PCIBIOS_DEVICE_NOT_FOUND on failure
838 static int hv_pcifront_write_config(struct pci_bus *bus, unsigned int devfn,
839 int where, int size, u32 val)
841 struct hv_pcibus_device *hbus =
842 container_of(bus->sysdata, struct hv_pcibus_device, sysdata);
843 struct hv_pci_dev *hpdev;
845 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(devfn));
847 return PCIBIOS_DEVICE_NOT_FOUND;
849 _hv_pcifront_write_config(hpdev, where, size, val);
852 return PCIBIOS_SUCCESSFUL;
855 /* PCIe operations */
856 static struct pci_ops hv_pcifront_ops = {
857 .read = hv_pcifront_read_config,
858 .write = hv_pcifront_write_config,
862 * Paravirtual backchannel
864 * Hyper-V SR-IOV provides a backchannel mechanism in software for
865 * communication between a VF driver and a PF driver. These
866 * "configuration blocks" are similar in concept to PCI configuration space,
867 * but instead of doing reads and writes in 32-bit chunks through a very slow
868 * path, packets of up to 128 bytes can be sent or received asynchronously.
870 * Nearly every SR-IOV device contains just such a communications channel in
871 * hardware, so using this one in software is usually optional. Using the
872 * software channel, however, allows driver implementers to leverage software
873 * tools that fuzz the communications channel looking for vulnerabilities.
875 * The usage model for these packets puts the responsibility for reading or
876 * writing on the VF driver. The VF driver sends a read or a write packet,
877 * indicating which "block" is being referred to by number.
879 * If the PF driver wishes to initiate communication, it can "invalidate" one or
880 * more of the first 64 blocks. This invalidation is delivered via a callback
881 * supplied by the VF driver by this driver.
883 * No protocol is implied, except that supplied by the PF and VF drivers.
886 struct hv_read_config_compl {
887 struct hv_pci_compl comp_pkt;
890 unsigned int bytes_returned;
894 * hv_pci_read_config_compl() - Invoked when a response packet
895 * for a read config block operation arrives.
896 * @context: Identifies the read config operation
897 * @resp: The response packet itself
898 * @resp_packet_size: Size in bytes of the response packet
900 static void hv_pci_read_config_compl(void *context, struct pci_response *resp,
901 int resp_packet_size)
903 struct hv_read_config_compl *comp = context;
904 struct pci_read_block_response *read_resp =
905 (struct pci_read_block_response *)resp;
906 unsigned int data_len, hdr_len;
908 hdr_len = offsetof(struct pci_read_block_response, bytes);
909 if (resp_packet_size < hdr_len) {
910 comp->comp_pkt.completion_status = -1;
914 data_len = resp_packet_size - hdr_len;
915 if (data_len > 0 && read_resp->status == 0) {
916 comp->bytes_returned = min(comp->len, data_len);
917 memcpy(comp->buf, read_resp->bytes, comp->bytes_returned);
919 comp->bytes_returned = 0;
922 comp->comp_pkt.completion_status = read_resp->status;
924 complete(&comp->comp_pkt.host_event);
928 * hv_read_config_block() - Sends a read config block request to
929 * the back-end driver running in the Hyper-V parent partition.
930 * @pdev: The PCI driver's representation for this device.
931 * @buf: Buffer into which the config block will be copied.
932 * @len: Size in bytes of buf.
933 * @block_id: Identifies the config block which has been requested.
934 * @bytes_returned: Size which came back from the back-end driver.
936 * Return: 0 on success, -errno on failure
938 int hv_read_config_block(struct pci_dev *pdev, void *buf, unsigned int len,
939 unsigned int block_id, unsigned int *bytes_returned)
941 struct hv_pcibus_device *hbus =
942 container_of(pdev->bus->sysdata, struct hv_pcibus_device,
945 struct pci_packet pkt;
946 char buf[sizeof(struct pci_read_block)];
948 struct hv_read_config_compl comp_pkt;
949 struct pci_read_block *read_blk;
952 if (len == 0 || len > HV_CONFIG_BLOCK_SIZE_MAX)
955 init_completion(&comp_pkt.comp_pkt.host_event);
959 memset(&pkt, 0, sizeof(pkt));
960 pkt.pkt.completion_func = hv_pci_read_config_compl;
961 pkt.pkt.compl_ctxt = &comp_pkt;
962 read_blk = (struct pci_read_block *)&pkt.pkt.message;
963 read_blk->message_type.type = PCI_READ_BLOCK;
964 read_blk->wslot.slot = devfn_to_wslot(pdev->devfn);
965 read_blk->block_id = block_id;
966 read_blk->bytes_requested = len;
968 ret = vmbus_sendpacket(hbus->hdev->channel, read_blk,
969 sizeof(*read_blk), (unsigned long)&pkt.pkt,
971 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
975 ret = wait_for_response(hbus->hdev, &comp_pkt.comp_pkt.host_event);
979 if (comp_pkt.comp_pkt.completion_status != 0 ||
980 comp_pkt.bytes_returned == 0) {
981 dev_err(&hbus->hdev->device,
982 "Read Config Block failed: 0x%x, bytes_returned=%d\n",
983 comp_pkt.comp_pkt.completion_status,
984 comp_pkt.bytes_returned);
988 *bytes_returned = comp_pkt.bytes_returned;
993 * hv_pci_write_config_compl() - Invoked when a response packet for a write
994 * config block operation arrives.
995 * @context: Identifies the write config operation
996 * @resp: The response packet itself
997 * @resp_packet_size: Size in bytes of the response packet
999 static void hv_pci_write_config_compl(void *context, struct pci_response *resp,
1000 int resp_packet_size)
1002 struct hv_pci_compl *comp_pkt = context;
1004 comp_pkt->completion_status = resp->status;
1005 complete(&comp_pkt->host_event);
1009 * hv_write_config_block() - Sends a write config block request to the
1010 * back-end driver running in the Hyper-V parent partition.
1011 * @pdev: The PCI driver's representation for this device.
1012 * @buf: Buffer from which the config block will be copied.
1013 * @len: Size in bytes of buf.
1014 * @block_id: Identifies the config block which is being written.
1016 * Return: 0 on success, -errno on failure
1018 int hv_write_config_block(struct pci_dev *pdev, void *buf, unsigned int len,
1019 unsigned int block_id)
1021 struct hv_pcibus_device *hbus =
1022 container_of(pdev->bus->sysdata, struct hv_pcibus_device,
1025 struct pci_packet pkt;
1026 char buf[sizeof(struct pci_write_block)];
1029 struct hv_pci_compl comp_pkt;
1030 struct pci_write_block *write_blk;
1034 if (len == 0 || len > HV_CONFIG_BLOCK_SIZE_MAX)
1037 init_completion(&comp_pkt.host_event);
1039 memset(&pkt, 0, sizeof(pkt));
1040 pkt.pkt.completion_func = hv_pci_write_config_compl;
1041 pkt.pkt.compl_ctxt = &comp_pkt;
1042 write_blk = (struct pci_write_block *)&pkt.pkt.message;
1043 write_blk->message_type.type = PCI_WRITE_BLOCK;
1044 write_blk->wslot.slot = devfn_to_wslot(pdev->devfn);
1045 write_blk->block_id = block_id;
1046 write_blk->byte_count = len;
1047 memcpy(write_blk->bytes, buf, len);
1048 pkt_size = offsetof(struct pci_write_block, bytes) + len;
1050 * This quirk is required on some hosts shipped around 2018, because
1051 * these hosts don't check the pkt_size correctly (new hosts have been
1052 * fixed since early 2019). The quirk is also safe on very old hosts
1053 * and new hosts, because, on them, what really matters is the length
1054 * specified in write_blk->byte_count.
1056 pkt_size += sizeof(pkt.reserved);
1058 ret = vmbus_sendpacket(hbus->hdev->channel, write_blk, pkt_size,
1059 (unsigned long)&pkt.pkt, VM_PKT_DATA_INBAND,
1060 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1064 ret = wait_for_response(hbus->hdev, &comp_pkt.host_event);
1068 if (comp_pkt.completion_status != 0) {
1069 dev_err(&hbus->hdev->device,
1070 "Write Config Block failed: 0x%x\n",
1071 comp_pkt.completion_status);
1079 * hv_register_block_invalidate() - Invoked when a config block invalidation
1080 * arrives from the back-end driver.
1081 * @pdev: The PCI driver's representation for this device.
1082 * @context: Identifies the device.
1083 * @block_invalidate: Identifies all of the blocks being invalidated.
1085 * Return: 0 on success, -errno on failure
1087 int hv_register_block_invalidate(struct pci_dev *pdev, void *context,
1088 void (*block_invalidate)(void *context,
1091 struct hv_pcibus_device *hbus =
1092 container_of(pdev->bus->sysdata, struct hv_pcibus_device,
1094 struct hv_pci_dev *hpdev;
1096 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
1100 hpdev->block_invalidate = block_invalidate;
1101 hpdev->invalidate_context = context;
1103 put_pcichild(hpdev);
1108 /* Interrupt management hooks */
1109 static void hv_int_desc_free(struct hv_pci_dev *hpdev,
1110 struct tran_int_desc *int_desc)
1112 struct pci_delete_interrupt *int_pkt;
1114 struct pci_packet pkt;
1115 u8 buffer[sizeof(struct pci_delete_interrupt)];
1118 memset(&ctxt, 0, sizeof(ctxt));
1119 int_pkt = (struct pci_delete_interrupt *)&ctxt.pkt.message;
1120 int_pkt->message_type.type =
1121 PCI_DELETE_INTERRUPT_MESSAGE;
1122 int_pkt->wslot.slot = hpdev->desc.win_slot.slot;
1123 int_pkt->int_desc = *int_desc;
1124 vmbus_sendpacket(hpdev->hbus->hdev->channel, int_pkt, sizeof(*int_pkt),
1125 (unsigned long)&ctxt.pkt, VM_PKT_DATA_INBAND, 0);
1130 * hv_msi_free() - Free the MSI.
1131 * @domain: The interrupt domain pointer
1132 * @info: Extra MSI-related context
1133 * @irq: Identifies the IRQ.
1135 * The Hyper-V parent partition and hypervisor are tracking the
1136 * messages that are in use, keeping the interrupt redirection
1137 * table up to date. This callback sends a message that frees
1138 * the IRT entry and related tracking nonsense.
1140 static void hv_msi_free(struct irq_domain *domain, struct msi_domain_info *info,
1143 struct hv_pcibus_device *hbus;
1144 struct hv_pci_dev *hpdev;
1145 struct pci_dev *pdev;
1146 struct tran_int_desc *int_desc;
1147 struct irq_data *irq_data = irq_domain_get_irq_data(domain, irq);
1148 struct msi_desc *msi = irq_data_get_msi_desc(irq_data);
1150 pdev = msi_desc_to_pci_dev(msi);
1152 int_desc = irq_data_get_irq_chip_data(irq_data);
1156 irq_data->chip_data = NULL;
1157 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
1163 hv_int_desc_free(hpdev, int_desc);
1164 put_pcichild(hpdev);
1167 static int hv_set_affinity(struct irq_data *data, const struct cpumask *dest,
1170 struct irq_data *parent = data->parent_data;
1172 return parent->chip->irq_set_affinity(parent, dest, force);
1175 static void hv_irq_mask(struct irq_data *data)
1177 pci_msi_mask_irq(data);
1181 * hv_irq_unmask() - "Unmask" the IRQ by setting its current
1183 * @data: Describes the IRQ
1185 * Build new a destination for the MSI and make a hypercall to
1186 * update the Interrupt Redirection Table. "Device Logical ID"
1187 * is built out of this PCI bus's instance GUID and the function
1188 * number of the device.
1190 static void hv_irq_unmask(struct irq_data *data)
1192 struct msi_desc *msi_desc = irq_data_get_msi_desc(data);
1193 struct irq_cfg *cfg = irqd_cfg(data);
1194 struct hv_retarget_device_interrupt *params;
1195 struct hv_pcibus_device *hbus;
1196 struct cpumask *dest;
1198 struct pci_bus *pbus;
1199 struct pci_dev *pdev;
1200 unsigned long flags;
1205 dest = irq_data_get_effective_affinity_mask(data);
1206 pdev = msi_desc_to_pci_dev(msi_desc);
1208 hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata);
1210 spin_lock_irqsave(&hbus->retarget_msi_interrupt_lock, flags);
1212 params = &hbus->retarget_msi_interrupt_params;
1213 memset(params, 0, sizeof(*params));
1214 params->partition_id = HV_PARTITION_ID_SELF;
1215 params->int_entry.source = 1; /* MSI(-X) */
1216 hv_set_msi_entry_from_desc(¶ms->int_entry.msi_entry, msi_desc);
1217 params->device_id = (hbus->hdev->dev_instance.b[5] << 24) |
1218 (hbus->hdev->dev_instance.b[4] << 16) |
1219 (hbus->hdev->dev_instance.b[7] << 8) |
1220 (hbus->hdev->dev_instance.b[6] & 0xf8) |
1221 PCI_FUNC(pdev->devfn);
1222 params->int_target.vector = cfg->vector;
1225 * Honoring apic->irq_delivery_mode set to dest_Fixed by
1226 * setting the HV_DEVICE_INTERRUPT_TARGET_MULTICAST flag results in a
1227 * spurious interrupt storm. Not doing so does not seem to have a
1228 * negative effect (yet?).
1231 if (hbus->protocol_version >= PCI_PROTOCOL_VERSION_1_2) {
1233 * PCI_PROTOCOL_VERSION_1_2 supports the VP_SET version of the
1234 * HVCALL_RETARGET_INTERRUPT hypercall, which also coincides
1235 * with >64 VP support.
1236 * ms_hyperv.hints & HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED
1237 * is not sufficient for this hypercall.
1239 params->int_target.flags |=
1240 HV_DEVICE_INTERRUPT_TARGET_PROCESSOR_SET;
1242 if (!alloc_cpumask_var(&tmp, GFP_ATOMIC)) {
1247 cpumask_and(tmp, dest, cpu_online_mask);
1248 nr_bank = cpumask_to_vpset(¶ms->int_target.vp_set, tmp);
1249 free_cpumask_var(tmp);
1257 * var-sized hypercall, var-size starts after vp_mask (thus
1258 * vp_set.format does not count, but vp_set.valid_bank_mask
1261 var_size = 1 + nr_bank;
1263 for_each_cpu_and(cpu, dest, cpu_online_mask) {
1264 params->int_target.vp_mask |=
1265 (1ULL << hv_cpu_number_to_vp_number(cpu));
1269 res = hv_do_hypercall(HVCALL_RETARGET_INTERRUPT | (var_size << 17),
1273 spin_unlock_irqrestore(&hbus->retarget_msi_interrupt_lock, flags);
1276 dev_err(&hbus->hdev->device,
1277 "%s() failed: %#llx", __func__, res);
1281 pci_msi_unmask_irq(data);
1284 struct compose_comp_ctxt {
1285 struct hv_pci_compl comp_pkt;
1286 struct tran_int_desc int_desc;
1289 static void hv_pci_compose_compl(void *context, struct pci_response *resp,
1290 int resp_packet_size)
1292 struct compose_comp_ctxt *comp_pkt = context;
1293 struct pci_create_int_response *int_resp =
1294 (struct pci_create_int_response *)resp;
1296 comp_pkt->comp_pkt.completion_status = resp->status;
1297 comp_pkt->int_desc = int_resp->int_desc;
1298 complete(&comp_pkt->comp_pkt.host_event);
1301 static u32 hv_compose_msi_req_v1(
1302 struct pci_create_interrupt *int_pkt, struct cpumask *affinity,
1303 u32 slot, u8 vector)
1305 int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE;
1306 int_pkt->wslot.slot = slot;
1307 int_pkt->int_desc.vector = vector;
1308 int_pkt->int_desc.vector_count = 1;
1309 int_pkt->int_desc.delivery_mode = dest_Fixed;
1312 * Create MSI w/ dummy vCPU set, overwritten by subsequent retarget in
1315 int_pkt->int_desc.cpu_mask = CPU_AFFINITY_ALL;
1317 return sizeof(*int_pkt);
1320 static u32 hv_compose_msi_req_v2(
1321 struct pci_create_interrupt2 *int_pkt, struct cpumask *affinity,
1322 u32 slot, u8 vector)
1326 int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE2;
1327 int_pkt->wslot.slot = slot;
1328 int_pkt->int_desc.vector = vector;
1329 int_pkt->int_desc.vector_count = 1;
1330 int_pkt->int_desc.delivery_mode = dest_Fixed;
1333 * Create MSI w/ dummy vCPU set targeting just one vCPU, overwritten
1334 * by subsequent retarget in hv_irq_unmask().
1336 cpu = cpumask_first_and(affinity, cpu_online_mask);
1337 int_pkt->int_desc.processor_array[0] =
1338 hv_cpu_number_to_vp_number(cpu);
1339 int_pkt->int_desc.processor_count = 1;
1341 return sizeof(*int_pkt);
1345 * hv_compose_msi_msg() - Supplies a valid MSI address/data
1346 * @data: Everything about this MSI
1347 * @msg: Buffer that is filled in by this function
1349 * This function unpacks the IRQ looking for target CPU set, IDT
1350 * vector and mode and sends a message to the parent partition
1351 * asking for a mapping for that tuple in this partition. The
1352 * response supplies a data value and address to which that data
1353 * should be written to trigger that interrupt.
1355 static void hv_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
1357 struct irq_cfg *cfg = irqd_cfg(data);
1358 struct hv_pcibus_device *hbus;
1359 struct vmbus_channel *channel;
1360 struct hv_pci_dev *hpdev;
1361 struct pci_bus *pbus;
1362 struct pci_dev *pdev;
1363 struct cpumask *dest;
1364 struct compose_comp_ctxt comp;
1365 struct tran_int_desc *int_desc;
1367 struct pci_packet pci_pkt;
1369 struct pci_create_interrupt v1;
1370 struct pci_create_interrupt2 v2;
1377 pdev = msi_desc_to_pci_dev(irq_data_get_msi_desc(data));
1378 dest = irq_data_get_effective_affinity_mask(data);
1380 hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata);
1381 channel = hbus->hdev->channel;
1382 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
1384 goto return_null_message;
1386 /* Free any previous message that might have already been composed. */
1387 if (data->chip_data) {
1388 int_desc = data->chip_data;
1389 data->chip_data = NULL;
1390 hv_int_desc_free(hpdev, int_desc);
1393 int_desc = kzalloc(sizeof(*int_desc), GFP_ATOMIC);
1395 goto drop_reference;
1397 memset(&ctxt, 0, sizeof(ctxt));
1398 init_completion(&comp.comp_pkt.host_event);
1399 ctxt.pci_pkt.completion_func = hv_pci_compose_compl;
1400 ctxt.pci_pkt.compl_ctxt = ∁
1402 switch (hbus->protocol_version) {
1403 case PCI_PROTOCOL_VERSION_1_1:
1404 size = hv_compose_msi_req_v1(&ctxt.int_pkts.v1,
1406 hpdev->desc.win_slot.slot,
1410 case PCI_PROTOCOL_VERSION_1_2:
1411 case PCI_PROTOCOL_VERSION_1_3:
1412 size = hv_compose_msi_req_v2(&ctxt.int_pkts.v2,
1414 hpdev->desc.win_slot.slot,
1419 /* As we only negotiate protocol versions known to this driver,
1420 * this path should never hit. However, this is it not a hot
1421 * path so we print a message to aid future updates.
1423 dev_err(&hbus->hdev->device,
1424 "Unexpected vPCI protocol, update driver.");
1428 ret = vmbus_sendpacket(hpdev->hbus->hdev->channel, &ctxt.int_pkts,
1429 size, (unsigned long)&ctxt.pci_pkt,
1431 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1433 dev_err(&hbus->hdev->device,
1434 "Sending request for interrupt failed: 0x%x",
1435 comp.comp_pkt.completion_status);
1440 * Prevents hv_pci_onchannelcallback() from running concurrently
1443 tasklet_disable(&channel->callback_event);
1446 * Since this function is called with IRQ locks held, can't
1447 * do normal wait for completion; instead poll.
1449 while (!try_wait_for_completion(&comp.comp_pkt.host_event)) {
1450 unsigned long flags;
1452 /* 0xFFFF means an invalid PCI VENDOR ID. */
1453 if (hv_pcifront_get_vendor_id(hpdev) == 0xFFFF) {
1454 dev_err_once(&hbus->hdev->device,
1455 "the device has gone\n");
1456 goto enable_tasklet;
1460 * Make sure that the ring buffer data structure doesn't get
1461 * freed while we dereference the ring buffer pointer. Test
1462 * for the channel's onchannel_callback being NULL within a
1463 * sched_lock critical section. See also the inline comments
1464 * in vmbus_reset_channel_cb().
1466 spin_lock_irqsave(&channel->sched_lock, flags);
1467 if (unlikely(channel->onchannel_callback == NULL)) {
1468 spin_unlock_irqrestore(&channel->sched_lock, flags);
1469 goto enable_tasklet;
1471 hv_pci_onchannelcallback(hbus);
1472 spin_unlock_irqrestore(&channel->sched_lock, flags);
1474 if (hpdev->state == hv_pcichild_ejecting) {
1475 dev_err_once(&hbus->hdev->device,
1476 "the device is being ejected\n");
1477 goto enable_tasklet;
1483 tasklet_enable(&channel->callback_event);
1485 if (comp.comp_pkt.completion_status < 0) {
1486 dev_err(&hbus->hdev->device,
1487 "Request for interrupt failed: 0x%x",
1488 comp.comp_pkt.completion_status);
1493 * Record the assignment so that this can be unwound later. Using
1494 * irq_set_chip_data() here would be appropriate, but the lock it takes
1497 *int_desc = comp.int_desc;
1498 data->chip_data = int_desc;
1500 /* Pass up the result. */
1501 msg->address_hi = comp.int_desc.address >> 32;
1502 msg->address_lo = comp.int_desc.address & 0xffffffff;
1503 msg->data = comp.int_desc.data;
1505 put_pcichild(hpdev);
1509 tasklet_enable(&channel->callback_event);
1513 put_pcichild(hpdev);
1514 return_null_message:
1515 msg->address_hi = 0;
1516 msg->address_lo = 0;
1520 /* HW Interrupt Chip Descriptor */
1521 static struct irq_chip hv_msi_irq_chip = {
1522 .name = "Hyper-V PCIe MSI",
1523 .irq_compose_msi_msg = hv_compose_msi_msg,
1524 .irq_set_affinity = hv_set_affinity,
1525 .irq_ack = irq_chip_ack_parent,
1526 .irq_mask = hv_irq_mask,
1527 .irq_unmask = hv_irq_unmask,
1530 static irq_hw_number_t hv_msi_domain_ops_get_hwirq(struct msi_domain_info *info,
1531 msi_alloc_info_t *arg)
1533 return arg->msi_hwirq;
1536 static struct msi_domain_ops hv_msi_ops = {
1537 .get_hwirq = hv_msi_domain_ops_get_hwirq,
1538 .msi_prepare = pci_msi_prepare,
1539 .set_desc = pci_msi_set_desc,
1540 .msi_free = hv_msi_free,
1544 * hv_pcie_init_irq_domain() - Initialize IRQ domain
1545 * @hbus: The root PCI bus
1547 * This function creates an IRQ domain which will be used for
1548 * interrupts from devices that have been passed through. These
1549 * devices only support MSI and MSI-X, not line-based interrupts
1550 * or simulations of line-based interrupts through PCIe's
1551 * fabric-layer messages. Because interrupts are remapped, we
1552 * can support multi-message MSI here.
1554 * Return: '0' on success and error value on failure
1556 static int hv_pcie_init_irq_domain(struct hv_pcibus_device *hbus)
1558 hbus->msi_info.chip = &hv_msi_irq_chip;
1559 hbus->msi_info.ops = &hv_msi_ops;
1560 hbus->msi_info.flags = (MSI_FLAG_USE_DEF_DOM_OPS |
1561 MSI_FLAG_USE_DEF_CHIP_OPS | MSI_FLAG_MULTI_PCI_MSI |
1563 hbus->msi_info.handler = handle_edge_irq;
1564 hbus->msi_info.handler_name = "edge";
1565 hbus->msi_info.data = hbus;
1566 hbus->irq_domain = pci_msi_create_irq_domain(hbus->sysdata.fwnode,
1569 if (!hbus->irq_domain) {
1570 dev_err(&hbus->hdev->device,
1571 "Failed to build an MSI IRQ domain\n");
1579 * get_bar_size() - Get the address space consumed by a BAR
1580 * @bar_val: Value that a BAR returned after -1 was written
1583 * This function returns the size of the BAR, rounded up to 1
1584 * page. It has to be rounded up because the hypervisor's page
1585 * table entry that maps the BAR into the VM can't specify an
1586 * offset within a page. The invariant is that the hypervisor
1587 * must place any BARs of smaller than page length at the
1588 * beginning of a page.
1590 * Return: Size in bytes of the consumed MMIO space.
1592 static u64 get_bar_size(u64 bar_val)
1594 return round_up((1 + ~(bar_val & PCI_BASE_ADDRESS_MEM_MASK)),
1599 * survey_child_resources() - Total all MMIO requirements
1600 * @hbus: Root PCI bus, as understood by this driver
1602 static void survey_child_resources(struct hv_pcibus_device *hbus)
1604 struct hv_pci_dev *hpdev;
1605 resource_size_t bar_size = 0;
1606 unsigned long flags;
1607 struct completion *event;
1611 /* If nobody is waiting on the answer, don't compute it. */
1612 event = xchg(&hbus->survey_event, NULL);
1616 /* If the answer has already been computed, go with it. */
1617 if (hbus->low_mmio_space || hbus->high_mmio_space) {
1622 spin_lock_irqsave(&hbus->device_list_lock, flags);
1625 * Due to an interesting quirk of the PCI spec, all memory regions
1626 * for a child device are a power of 2 in size and aligned in memory,
1627 * so it's sufficient to just add them up without tracking alignment.
1629 list_for_each_entry(hpdev, &hbus->children, list_entry) {
1630 for (i = 0; i < PCI_STD_NUM_BARS; i++) {
1631 if (hpdev->probed_bar[i] & PCI_BASE_ADDRESS_SPACE_IO)
1632 dev_err(&hbus->hdev->device,
1633 "There's an I/O BAR in this list!\n");
1635 if (hpdev->probed_bar[i] != 0) {
1637 * A probed BAR has all the upper bits set that
1641 bar_val = hpdev->probed_bar[i];
1642 if (bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64)
1644 ((u64)hpdev->probed_bar[++i] << 32);
1646 bar_val |= 0xffffffff00000000ULL;
1648 bar_size = get_bar_size(bar_val);
1650 if (bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64)
1651 hbus->high_mmio_space += bar_size;
1653 hbus->low_mmio_space += bar_size;
1658 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1663 * prepopulate_bars() - Fill in BARs with defaults
1664 * @hbus: Root PCI bus, as understood by this driver
1666 * The core PCI driver code seems much, much happier if the BARs
1667 * for a device have values upon first scan. So fill them in.
1668 * The algorithm below works down from large sizes to small,
1669 * attempting to pack the assignments optimally. The assumption,
1670 * enforced in other parts of the code, is that the beginning of
1671 * the memory-mapped I/O space will be aligned on the largest
1674 static void prepopulate_bars(struct hv_pcibus_device *hbus)
1676 resource_size_t high_size = 0;
1677 resource_size_t low_size = 0;
1678 resource_size_t high_base = 0;
1679 resource_size_t low_base = 0;
1680 resource_size_t bar_size;
1681 struct hv_pci_dev *hpdev;
1682 unsigned long flags;
1688 if (hbus->low_mmio_space) {
1689 low_size = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space));
1690 low_base = hbus->low_mmio_res->start;
1693 if (hbus->high_mmio_space) {
1695 (63 - __builtin_clzll(hbus->high_mmio_space));
1696 high_base = hbus->high_mmio_res->start;
1699 spin_lock_irqsave(&hbus->device_list_lock, flags);
1702 * Clear the memory enable bit, in case it's already set. This occurs
1703 * in the suspend path of hibernation, where the device is suspended,
1704 * resumed and suspended again: see hibernation_snapshot() and
1705 * hibernation_platform_enter().
1707 * If the memory enable bit is already set, Hyper-V sliently ignores
1708 * the below BAR updates, and the related PCI device driver can not
1709 * work, because reading from the device register(s) always returns
1712 list_for_each_entry(hpdev, &hbus->children, list_entry) {
1713 _hv_pcifront_read_config(hpdev, PCI_COMMAND, 2, &command);
1714 command &= ~PCI_COMMAND_MEMORY;
1715 _hv_pcifront_write_config(hpdev, PCI_COMMAND, 2, command);
1718 /* Pick addresses for the BARs. */
1720 list_for_each_entry(hpdev, &hbus->children, list_entry) {
1721 for (i = 0; i < PCI_STD_NUM_BARS; i++) {
1722 bar_val = hpdev->probed_bar[i];
1725 high = bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64;
1728 ((u64)hpdev->probed_bar[i + 1]
1731 bar_val |= 0xffffffffULL << 32;
1733 bar_size = get_bar_size(bar_val);
1735 if (high_size != bar_size) {
1739 _hv_pcifront_write_config(hpdev,
1740 PCI_BASE_ADDRESS_0 + (4 * i),
1742 (u32)(high_base & 0xffffff00));
1744 _hv_pcifront_write_config(hpdev,
1745 PCI_BASE_ADDRESS_0 + (4 * i),
1746 4, (u32)(high_base >> 32));
1747 high_base += bar_size;
1749 if (low_size != bar_size)
1751 _hv_pcifront_write_config(hpdev,
1752 PCI_BASE_ADDRESS_0 + (4 * i),
1754 (u32)(low_base & 0xffffff00));
1755 low_base += bar_size;
1758 if (high_size <= 1 && low_size <= 1) {
1759 /* Set the memory enable bit. */
1760 _hv_pcifront_read_config(hpdev, PCI_COMMAND, 2,
1762 command |= PCI_COMMAND_MEMORY;
1763 _hv_pcifront_write_config(hpdev, PCI_COMMAND, 2,
1771 } while (high_size || low_size);
1773 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1777 * Assign entries in sysfs pci slot directory.
1779 * Note that this function does not need to lock the children list
1780 * because it is called from pci_devices_present_work which
1781 * is serialized with hv_eject_device_work because they are on the
1782 * same ordered workqueue. Therefore hbus->children list will not change
1783 * even when pci_create_slot sleeps.
1785 static void hv_pci_assign_slots(struct hv_pcibus_device *hbus)
1787 struct hv_pci_dev *hpdev;
1788 char name[SLOT_NAME_SIZE];
1791 list_for_each_entry(hpdev, &hbus->children, list_entry) {
1792 if (hpdev->pci_slot)
1795 slot_nr = PCI_SLOT(wslot_to_devfn(hpdev->desc.win_slot.slot));
1796 snprintf(name, SLOT_NAME_SIZE, "%u", hpdev->desc.ser);
1797 hpdev->pci_slot = pci_create_slot(hbus->pci_bus, slot_nr,
1799 if (IS_ERR(hpdev->pci_slot)) {
1800 pr_warn("pci_create slot %s failed\n", name);
1801 hpdev->pci_slot = NULL;
1807 * Remove entries in sysfs pci slot directory.
1809 static void hv_pci_remove_slots(struct hv_pcibus_device *hbus)
1811 struct hv_pci_dev *hpdev;
1813 list_for_each_entry(hpdev, &hbus->children, list_entry) {
1814 if (!hpdev->pci_slot)
1816 pci_destroy_slot(hpdev->pci_slot);
1817 hpdev->pci_slot = NULL;
1822 * Set NUMA node for the devices on the bus
1824 static void hv_pci_assign_numa_node(struct hv_pcibus_device *hbus)
1826 struct pci_dev *dev;
1827 struct pci_bus *bus = hbus->pci_bus;
1828 struct hv_pci_dev *hv_dev;
1830 list_for_each_entry(dev, &bus->devices, bus_list) {
1831 hv_dev = get_pcichild_wslot(hbus, devfn_to_wslot(dev->devfn));
1835 if (hv_dev->desc.flags & HV_PCI_DEVICE_FLAG_NUMA_AFFINITY)
1836 set_dev_node(&dev->dev, hv_dev->desc.virtual_numa_node);
1838 put_pcichild(hv_dev);
1843 * create_root_hv_pci_bus() - Expose a new root PCI bus
1844 * @hbus: Root PCI bus, as understood by this driver
1846 * Return: 0 on success, -errno on failure
1848 static int create_root_hv_pci_bus(struct hv_pcibus_device *hbus)
1850 /* Register the device */
1851 hbus->pci_bus = pci_create_root_bus(&hbus->hdev->device,
1852 0, /* bus number is always zero */
1855 &hbus->resources_for_children);
1859 hbus->pci_bus->msi = &hbus->msi_chip;
1860 hbus->pci_bus->msi->dev = &hbus->hdev->device;
1862 pci_lock_rescan_remove();
1863 pci_scan_child_bus(hbus->pci_bus);
1864 hv_pci_assign_numa_node(hbus);
1865 pci_bus_assign_resources(hbus->pci_bus);
1866 hv_pci_assign_slots(hbus);
1867 pci_bus_add_devices(hbus->pci_bus);
1868 pci_unlock_rescan_remove();
1869 hbus->state = hv_pcibus_installed;
1873 struct q_res_req_compl {
1874 struct completion host_event;
1875 struct hv_pci_dev *hpdev;
1879 * q_resource_requirements() - Query Resource Requirements
1880 * @context: The completion context.
1881 * @resp: The response that came from the host.
1882 * @resp_packet_size: The size in bytes of resp.
1884 * This function is invoked on completion of a Query Resource
1885 * Requirements packet.
1887 static void q_resource_requirements(void *context, struct pci_response *resp,
1888 int resp_packet_size)
1890 struct q_res_req_compl *completion = context;
1891 struct pci_q_res_req_response *q_res_req =
1892 (struct pci_q_res_req_response *)resp;
1895 if (resp->status < 0) {
1896 dev_err(&completion->hpdev->hbus->hdev->device,
1897 "query resource requirements failed: %x\n",
1900 for (i = 0; i < PCI_STD_NUM_BARS; i++) {
1901 completion->hpdev->probed_bar[i] =
1902 q_res_req->probed_bar[i];
1906 complete(&completion->host_event);
1910 * new_pcichild_device() - Create a new child device
1911 * @hbus: The internal struct tracking this root PCI bus.
1912 * @desc: The information supplied so far from the host
1915 * This function creates the tracking structure for a new child
1916 * device and kicks off the process of figuring out what it is.
1918 * Return: Pointer to the new tracking struct
1920 static struct hv_pci_dev *new_pcichild_device(struct hv_pcibus_device *hbus,
1921 struct hv_pcidev_description *desc)
1923 struct hv_pci_dev *hpdev;
1924 struct pci_child_message *res_req;
1925 struct q_res_req_compl comp_pkt;
1927 struct pci_packet init_packet;
1928 u8 buffer[sizeof(struct pci_child_message)];
1930 unsigned long flags;
1933 hpdev = kzalloc(sizeof(*hpdev), GFP_KERNEL);
1939 memset(&pkt, 0, sizeof(pkt));
1940 init_completion(&comp_pkt.host_event);
1941 comp_pkt.hpdev = hpdev;
1942 pkt.init_packet.compl_ctxt = &comp_pkt;
1943 pkt.init_packet.completion_func = q_resource_requirements;
1944 res_req = (struct pci_child_message *)&pkt.init_packet.message;
1945 res_req->message_type.type = PCI_QUERY_RESOURCE_REQUIREMENTS;
1946 res_req->wslot.slot = desc->win_slot.slot;
1948 ret = vmbus_sendpacket(hbus->hdev->channel, res_req,
1949 sizeof(struct pci_child_message),
1950 (unsigned long)&pkt.init_packet,
1952 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1956 if (wait_for_response(hbus->hdev, &comp_pkt.host_event))
1959 hpdev->desc = *desc;
1960 refcount_set(&hpdev->refs, 1);
1961 get_pcichild(hpdev);
1962 spin_lock_irqsave(&hbus->device_list_lock, flags);
1964 list_add_tail(&hpdev->list_entry, &hbus->children);
1965 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1974 * get_pcichild_wslot() - Find device from slot
1975 * @hbus: Root PCI bus, as understood by this driver
1976 * @wslot: Location on the bus
1978 * This function looks up a PCI device and returns the internal
1979 * representation of it. It acquires a reference on it, so that
1980 * the device won't be deleted while somebody is using it. The
1981 * caller is responsible for calling put_pcichild() to release
1984 * Return: Internal representation of a PCI device
1986 static struct hv_pci_dev *get_pcichild_wslot(struct hv_pcibus_device *hbus,
1989 unsigned long flags;
1990 struct hv_pci_dev *iter, *hpdev = NULL;
1992 spin_lock_irqsave(&hbus->device_list_lock, flags);
1993 list_for_each_entry(iter, &hbus->children, list_entry) {
1994 if (iter->desc.win_slot.slot == wslot) {
1996 get_pcichild(hpdev);
2000 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
2006 * pci_devices_present_work() - Handle new list of child devices
2007 * @work: Work struct embedded in struct hv_dr_work
2009 * "Bus Relations" is the Windows term for "children of this
2010 * bus." The terminology is preserved here for people trying to
2011 * debug the interaction between Hyper-V and Linux. This
2012 * function is called when the parent partition reports a list
2013 * of functions that should be observed under this PCI Express
2016 * This function updates the list, and must tolerate being
2017 * called multiple times with the same information. The typical
2018 * number of child devices is one, with very atypical cases
2019 * involving three or four, so the algorithms used here can be
2020 * simple and inefficient.
2022 * It must also treat the omission of a previously observed device as
2023 * notification that the device no longer exists.
2025 * Note that this function is serialized with hv_eject_device_work(),
2026 * because both are pushed to the ordered workqueue hbus->wq.
2028 static void pci_devices_present_work(struct work_struct *work)
2032 struct hv_pcidev_description *new_desc;
2033 struct hv_pci_dev *hpdev;
2034 struct hv_pcibus_device *hbus;
2035 struct list_head removed;
2036 struct hv_dr_work *dr_wrk;
2037 struct hv_dr_state *dr = NULL;
2038 unsigned long flags;
2040 dr_wrk = container_of(work, struct hv_dr_work, wrk);
2044 INIT_LIST_HEAD(&removed);
2046 /* Pull this off the queue and process it if it was the last one. */
2047 spin_lock_irqsave(&hbus->device_list_lock, flags);
2048 while (!list_empty(&hbus->dr_list)) {
2049 dr = list_first_entry(&hbus->dr_list, struct hv_dr_state,
2051 list_del(&dr->list_entry);
2053 /* Throw this away if the list still has stuff in it. */
2054 if (!list_empty(&hbus->dr_list)) {
2059 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
2066 /* First, mark all existing children as reported missing. */
2067 spin_lock_irqsave(&hbus->device_list_lock, flags);
2068 list_for_each_entry(hpdev, &hbus->children, list_entry) {
2069 hpdev->reported_missing = true;
2071 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
2073 /* Next, add back any reported devices. */
2074 for (child_no = 0; child_no < dr->device_count; child_no++) {
2076 new_desc = &dr->func[child_no];
2078 spin_lock_irqsave(&hbus->device_list_lock, flags);
2079 list_for_each_entry(hpdev, &hbus->children, list_entry) {
2080 if ((hpdev->desc.win_slot.slot == new_desc->win_slot.slot) &&
2081 (hpdev->desc.v_id == new_desc->v_id) &&
2082 (hpdev->desc.d_id == new_desc->d_id) &&
2083 (hpdev->desc.ser == new_desc->ser)) {
2084 hpdev->reported_missing = false;
2088 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
2091 hpdev = new_pcichild_device(hbus, new_desc);
2093 dev_err(&hbus->hdev->device,
2094 "couldn't record a child device.\n");
2098 /* Move missing children to a list on the stack. */
2099 spin_lock_irqsave(&hbus->device_list_lock, flags);
2102 list_for_each_entry(hpdev, &hbus->children, list_entry) {
2103 if (hpdev->reported_missing) {
2105 put_pcichild(hpdev);
2106 list_move_tail(&hpdev->list_entry, &removed);
2111 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
2113 /* Delete everything that should no longer exist. */
2114 while (!list_empty(&removed)) {
2115 hpdev = list_first_entry(&removed, struct hv_pci_dev,
2117 list_del(&hpdev->list_entry);
2119 if (hpdev->pci_slot)
2120 pci_destroy_slot(hpdev->pci_slot);
2122 put_pcichild(hpdev);
2125 switch (hbus->state) {
2126 case hv_pcibus_installed:
2128 * Tell the core to rescan bus
2129 * because there may have been changes.
2131 pci_lock_rescan_remove();
2132 pci_scan_child_bus(hbus->pci_bus);
2133 hv_pci_assign_numa_node(hbus);
2134 hv_pci_assign_slots(hbus);
2135 pci_unlock_rescan_remove();
2138 case hv_pcibus_init:
2139 case hv_pcibus_probed:
2140 survey_child_resources(hbus);
2152 * hv_pci_start_relations_work() - Queue work to start device discovery
2153 * @hbus: Root PCI bus, as understood by this driver
2154 * @dr: The list of children returned from host
2156 * Return: 0 on success, -errno on failure
2158 static int hv_pci_start_relations_work(struct hv_pcibus_device *hbus,
2159 struct hv_dr_state *dr)
2161 struct hv_dr_work *dr_wrk;
2162 unsigned long flags;
2165 if (hbus->state == hv_pcibus_removing) {
2166 dev_info(&hbus->hdev->device,
2167 "PCI VMBus BUS_RELATIONS: ignored\n");
2171 dr_wrk = kzalloc(sizeof(*dr_wrk), GFP_NOWAIT);
2175 INIT_WORK(&dr_wrk->wrk, pci_devices_present_work);
2178 spin_lock_irqsave(&hbus->device_list_lock, flags);
2180 * If pending_dr is true, we have already queued a work,
2181 * which will see the new dr. Otherwise, we need to
2184 pending_dr = !list_empty(&hbus->dr_list);
2185 list_add_tail(&dr->list_entry, &hbus->dr_list);
2186 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
2192 queue_work(hbus->wq, &dr_wrk->wrk);
2199 * hv_pci_devices_present() - Handle list of new children
2200 * @hbus: Root PCI bus, as understood by this driver
2201 * @relations: Packet from host listing children
2203 * Process a new list of devices on the bus. The list of devices is
2204 * discovered by VSP and sent to us via VSP message PCI_BUS_RELATIONS,
2205 * whenever a new list of devices for this bus appears.
2207 static void hv_pci_devices_present(struct hv_pcibus_device *hbus,
2208 struct pci_bus_relations *relations)
2210 struct hv_dr_state *dr;
2213 dr = kzalloc(offsetof(struct hv_dr_state, func) +
2214 (sizeof(struct hv_pcidev_description) *
2215 (relations->device_count)), GFP_NOWAIT);
2220 dr->device_count = relations->device_count;
2221 for (i = 0; i < dr->device_count; i++) {
2222 dr->func[i].v_id = relations->func[i].v_id;
2223 dr->func[i].d_id = relations->func[i].d_id;
2224 dr->func[i].rev = relations->func[i].rev;
2225 dr->func[i].prog_intf = relations->func[i].prog_intf;
2226 dr->func[i].subclass = relations->func[i].subclass;
2227 dr->func[i].base_class = relations->func[i].base_class;
2228 dr->func[i].subsystem_id = relations->func[i].subsystem_id;
2229 dr->func[i].win_slot = relations->func[i].win_slot;
2230 dr->func[i].ser = relations->func[i].ser;
2233 if (hv_pci_start_relations_work(hbus, dr))
2238 * hv_pci_devices_present2() - Handle list of new children
2239 * @hbus: Root PCI bus, as understood by this driver
2240 * @relations: Packet from host listing children
2242 * This function is the v2 version of hv_pci_devices_present()
2244 static void hv_pci_devices_present2(struct hv_pcibus_device *hbus,
2245 struct pci_bus_relations2 *relations)
2247 struct hv_dr_state *dr;
2250 dr = kzalloc(offsetof(struct hv_dr_state, func) +
2251 (sizeof(struct hv_pcidev_description) *
2252 (relations->device_count)), GFP_NOWAIT);
2257 dr->device_count = relations->device_count;
2258 for (i = 0; i < dr->device_count; i++) {
2259 dr->func[i].v_id = relations->func[i].v_id;
2260 dr->func[i].d_id = relations->func[i].d_id;
2261 dr->func[i].rev = relations->func[i].rev;
2262 dr->func[i].prog_intf = relations->func[i].prog_intf;
2263 dr->func[i].subclass = relations->func[i].subclass;
2264 dr->func[i].base_class = relations->func[i].base_class;
2265 dr->func[i].subsystem_id = relations->func[i].subsystem_id;
2266 dr->func[i].win_slot = relations->func[i].win_slot;
2267 dr->func[i].ser = relations->func[i].ser;
2268 dr->func[i].flags = relations->func[i].flags;
2269 dr->func[i].virtual_numa_node =
2270 relations->func[i].virtual_numa_node;
2273 if (hv_pci_start_relations_work(hbus, dr))
2278 * hv_eject_device_work() - Asynchronously handles ejection
2279 * @work: Work struct embedded in internal device struct
2281 * This function handles ejecting a device. Windows will
2282 * attempt to gracefully eject a device, waiting 60 seconds to
2283 * hear back from the guest OS that this completed successfully.
2284 * If this timer expires, the device will be forcibly removed.
2286 static void hv_eject_device_work(struct work_struct *work)
2288 struct pci_eject_response *ejct_pkt;
2289 struct hv_pcibus_device *hbus;
2290 struct hv_pci_dev *hpdev;
2291 struct pci_dev *pdev;
2292 unsigned long flags;
2295 struct pci_packet pkt;
2296 u8 buffer[sizeof(struct pci_eject_response)];
2299 hpdev = container_of(work, struct hv_pci_dev, wrk);
2302 WARN_ON(hpdev->state != hv_pcichild_ejecting);
2305 * Ejection can come before or after the PCI bus has been set up, so
2306 * attempt to find it and tear down the bus state, if it exists. This
2307 * must be done without constructs like pci_domain_nr(hbus->pci_bus)
2308 * because hbus->pci_bus may not exist yet.
2310 wslot = wslot_to_devfn(hpdev->desc.win_slot.slot);
2311 pdev = pci_get_domain_bus_and_slot(hbus->sysdata.domain, 0, wslot);
2313 pci_lock_rescan_remove();
2314 pci_stop_and_remove_bus_device(pdev);
2316 pci_unlock_rescan_remove();
2319 spin_lock_irqsave(&hbus->device_list_lock, flags);
2320 list_del(&hpdev->list_entry);
2321 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
2323 if (hpdev->pci_slot)
2324 pci_destroy_slot(hpdev->pci_slot);
2326 memset(&ctxt, 0, sizeof(ctxt));
2327 ejct_pkt = (struct pci_eject_response *)&ctxt.pkt.message;
2328 ejct_pkt->message_type.type = PCI_EJECTION_COMPLETE;
2329 ejct_pkt->wslot.slot = hpdev->desc.win_slot.slot;
2330 vmbus_sendpacket(hbus->hdev->channel, ejct_pkt,
2331 sizeof(*ejct_pkt), (unsigned long)&ctxt.pkt,
2332 VM_PKT_DATA_INBAND, 0);
2334 /* For the get_pcichild() in hv_pci_eject_device() */
2335 put_pcichild(hpdev);
2336 /* For the two refs got in new_pcichild_device() */
2337 put_pcichild(hpdev);
2338 put_pcichild(hpdev);
2339 /* hpdev has been freed. Do not use it any more. */
2345 * hv_pci_eject_device() - Handles device ejection
2346 * @hpdev: Internal device tracking struct
2348 * This function is invoked when an ejection packet arrives. It
2349 * just schedules work so that we don't re-enter the packet
2350 * delivery code handling the ejection.
2352 static void hv_pci_eject_device(struct hv_pci_dev *hpdev)
2354 struct hv_pcibus_device *hbus = hpdev->hbus;
2355 struct hv_device *hdev = hbus->hdev;
2357 if (hbus->state == hv_pcibus_removing) {
2358 dev_info(&hdev->device, "PCI VMBus EJECT: ignored\n");
2362 hpdev->state = hv_pcichild_ejecting;
2363 get_pcichild(hpdev);
2364 INIT_WORK(&hpdev->wrk, hv_eject_device_work);
2366 queue_work(hbus->wq, &hpdev->wrk);
2370 * hv_pci_onchannelcallback() - Handles incoming packets
2371 * @context: Internal bus tracking struct
2373 * This function is invoked whenever the host sends a packet to
2374 * this channel (which is private to this root PCI bus).
2376 static void hv_pci_onchannelcallback(void *context)
2378 const int packet_size = 0x100;
2380 struct hv_pcibus_device *hbus = context;
2383 struct vmpacket_descriptor *desc;
2384 unsigned char *buffer;
2385 int bufferlen = packet_size;
2386 struct pci_packet *comp_packet;
2387 struct pci_response *response;
2388 struct pci_incoming_message *new_message;
2389 struct pci_bus_relations *bus_rel;
2390 struct pci_bus_relations2 *bus_rel2;
2391 struct pci_dev_inval_block *inval;
2392 struct pci_dev_incoming *dev_message;
2393 struct hv_pci_dev *hpdev;
2395 buffer = kmalloc(bufferlen, GFP_ATOMIC);
2400 ret = vmbus_recvpacket_raw(hbus->hdev->channel, buffer,
2401 bufferlen, &bytes_recvd, &req_id);
2403 if (ret == -ENOBUFS) {
2405 /* Handle large packet */
2406 bufferlen = bytes_recvd;
2407 buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
2413 /* Zero length indicates there are no more packets. */
2414 if (ret || !bytes_recvd)
2418 * All incoming packets must be at least as large as a
2421 if (bytes_recvd <= sizeof(struct pci_response))
2423 desc = (struct vmpacket_descriptor *)buffer;
2425 switch (desc->type) {
2429 * The host is trusted, and thus it's safe to interpret
2430 * this transaction ID as a pointer.
2432 comp_packet = (struct pci_packet *)req_id;
2433 response = (struct pci_response *)buffer;
2434 comp_packet->completion_func(comp_packet->compl_ctxt,
2439 case VM_PKT_DATA_INBAND:
2441 new_message = (struct pci_incoming_message *)buffer;
2442 switch (new_message->message_type.type) {
2443 case PCI_BUS_RELATIONS:
2445 bus_rel = (struct pci_bus_relations *)buffer;
2447 offsetof(struct pci_bus_relations, func) +
2448 (sizeof(struct pci_function_description) *
2449 (bus_rel->device_count))) {
2450 dev_err(&hbus->hdev->device,
2451 "bus relations too small\n");
2455 hv_pci_devices_present(hbus, bus_rel);
2458 case PCI_BUS_RELATIONS2:
2460 bus_rel2 = (struct pci_bus_relations2 *)buffer;
2462 offsetof(struct pci_bus_relations2, func) +
2463 (sizeof(struct pci_function_description2) *
2464 (bus_rel2->device_count))) {
2465 dev_err(&hbus->hdev->device,
2466 "bus relations v2 too small\n");
2470 hv_pci_devices_present2(hbus, bus_rel2);
2475 dev_message = (struct pci_dev_incoming *)buffer;
2476 hpdev = get_pcichild_wslot(hbus,
2477 dev_message->wslot.slot);
2479 hv_pci_eject_device(hpdev);
2480 put_pcichild(hpdev);
2484 case PCI_INVALIDATE_BLOCK:
2486 inval = (struct pci_dev_inval_block *)buffer;
2487 hpdev = get_pcichild_wslot(hbus,
2490 if (hpdev->block_invalidate) {
2491 hpdev->block_invalidate(
2492 hpdev->invalidate_context,
2495 put_pcichild(hpdev);
2500 dev_warn(&hbus->hdev->device,
2501 "Unimplemented protocol message %x\n",
2502 new_message->message_type.type);
2508 dev_err(&hbus->hdev->device,
2509 "unhandled packet type %d, tid %llx len %d\n",
2510 desc->type, req_id, bytes_recvd);
2519 * hv_pci_protocol_negotiation() - Set up protocol
2520 * @hdev: VMBus's tracking struct for this root PCI bus
2522 * This driver is intended to support running on Windows 10
2523 * (server) and later versions. It will not run on earlier
2524 * versions, as they assume that many of the operations which
2525 * Linux needs accomplished with a spinlock held were done via
2526 * asynchronous messaging via VMBus. Windows 10 increases the
2527 * surface area of PCI emulation so that these actions can take
2528 * place by suspending a virtual processor for their duration.
2530 * This function negotiates the channel protocol version,
2531 * failing if the host doesn't support the necessary protocol
2534 static int hv_pci_protocol_negotiation(struct hv_device *hdev,
2535 enum pci_protocol_version_t version[],
2538 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
2539 struct pci_version_request *version_req;
2540 struct hv_pci_compl comp_pkt;
2541 struct pci_packet *pkt;
2546 * Initiate the handshake with the host and negotiate
2547 * a version that the host can support. We start with the
2548 * highest version number and go down if the host cannot
2551 pkt = kzalloc(sizeof(*pkt) + sizeof(*version_req), GFP_KERNEL);
2555 init_completion(&comp_pkt.host_event);
2556 pkt->completion_func = hv_pci_generic_compl;
2557 pkt->compl_ctxt = &comp_pkt;
2558 version_req = (struct pci_version_request *)&pkt->message;
2559 version_req->message_type.type = PCI_QUERY_PROTOCOL_VERSION;
2561 for (i = 0; i < num_version; i++) {
2562 version_req->protocol_version = version[i];
2563 ret = vmbus_sendpacket(hdev->channel, version_req,
2564 sizeof(struct pci_version_request),
2565 (unsigned long)pkt, VM_PKT_DATA_INBAND,
2566 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
2568 ret = wait_for_response(hdev, &comp_pkt.host_event);
2571 dev_err(&hdev->device,
2572 "PCI Pass-through VSP failed to request version: %d",
2577 if (comp_pkt.completion_status >= 0) {
2578 hbus->protocol_version = version[i];
2579 dev_info(&hdev->device,
2580 "PCI VMBus probing: Using version %#x\n",
2581 hbus->protocol_version);
2585 if (comp_pkt.completion_status != STATUS_REVISION_MISMATCH) {
2586 dev_err(&hdev->device,
2587 "PCI Pass-through VSP failed version request: %#x",
2588 comp_pkt.completion_status);
2593 reinit_completion(&comp_pkt.host_event);
2596 dev_err(&hdev->device,
2597 "PCI pass-through VSP failed to find supported version");
2606 * hv_pci_free_bridge_windows() - Release memory regions for the
2608 * @hbus: Root PCI bus, as understood by this driver
2610 static void hv_pci_free_bridge_windows(struct hv_pcibus_device *hbus)
2613 * Set the resources back to the way they looked when they
2614 * were allocated by setting IORESOURCE_BUSY again.
2617 if (hbus->low_mmio_space && hbus->low_mmio_res) {
2618 hbus->low_mmio_res->flags |= IORESOURCE_BUSY;
2619 vmbus_free_mmio(hbus->low_mmio_res->start,
2620 resource_size(hbus->low_mmio_res));
2623 if (hbus->high_mmio_space && hbus->high_mmio_res) {
2624 hbus->high_mmio_res->flags |= IORESOURCE_BUSY;
2625 vmbus_free_mmio(hbus->high_mmio_res->start,
2626 resource_size(hbus->high_mmio_res));
2631 * hv_pci_allocate_bridge_windows() - Allocate memory regions
2633 * @hbus: Root PCI bus, as understood by this driver
2635 * This function calls vmbus_allocate_mmio(), which is itself a
2636 * bit of a compromise. Ideally, we might change the pnp layer
2637 * in the kernel such that it comprehends either PCI devices
2638 * which are "grandchildren of ACPI," with some intermediate bus
2639 * node (in this case, VMBus) or change it such that it
2640 * understands VMBus. The pnp layer, however, has been declared
2641 * deprecated, and not subject to change.
2643 * The workaround, implemented here, is to ask VMBus to allocate
2644 * MMIO space for this bus. VMBus itself knows which ranges are
2645 * appropriate by looking at its own ACPI objects. Then, after
2646 * these ranges are claimed, they're modified to look like they
2647 * would have looked if the ACPI and pnp code had allocated
2648 * bridge windows. These descriptors have to exist in this form
2649 * in order to satisfy the code which will get invoked when the
2650 * endpoint PCI function driver calls request_mem_region() or
2651 * request_mem_region_exclusive().
2653 * Return: 0 on success, -errno on failure
2655 static int hv_pci_allocate_bridge_windows(struct hv_pcibus_device *hbus)
2657 resource_size_t align;
2660 if (hbus->low_mmio_space) {
2661 align = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space));
2662 ret = vmbus_allocate_mmio(&hbus->low_mmio_res, hbus->hdev, 0,
2663 (u64)(u32)0xffffffff,
2664 hbus->low_mmio_space,
2667 dev_err(&hbus->hdev->device,
2668 "Need %#llx of low MMIO space. Consider reconfiguring the VM.\n",
2669 hbus->low_mmio_space);
2673 /* Modify this resource to become a bridge window. */
2674 hbus->low_mmio_res->flags |= IORESOURCE_WINDOW;
2675 hbus->low_mmio_res->flags &= ~IORESOURCE_BUSY;
2676 pci_add_resource(&hbus->resources_for_children,
2677 hbus->low_mmio_res);
2680 if (hbus->high_mmio_space) {
2681 align = 1ULL << (63 - __builtin_clzll(hbus->high_mmio_space));
2682 ret = vmbus_allocate_mmio(&hbus->high_mmio_res, hbus->hdev,
2684 hbus->high_mmio_space, align,
2687 dev_err(&hbus->hdev->device,
2688 "Need %#llx of high MMIO space. Consider reconfiguring the VM.\n",
2689 hbus->high_mmio_space);
2690 goto release_low_mmio;
2693 /* Modify this resource to become a bridge window. */
2694 hbus->high_mmio_res->flags |= IORESOURCE_WINDOW;
2695 hbus->high_mmio_res->flags &= ~IORESOURCE_BUSY;
2696 pci_add_resource(&hbus->resources_for_children,
2697 hbus->high_mmio_res);
2703 if (hbus->low_mmio_res) {
2704 vmbus_free_mmio(hbus->low_mmio_res->start,
2705 resource_size(hbus->low_mmio_res));
2712 * hv_allocate_config_window() - Find MMIO space for PCI Config
2713 * @hbus: Root PCI bus, as understood by this driver
2715 * This function claims memory-mapped I/O space for accessing
2716 * configuration space for the functions on this bus.
2718 * Return: 0 on success, -errno on failure
2720 static int hv_allocate_config_window(struct hv_pcibus_device *hbus)
2725 * Set up a region of MMIO space to use for accessing configuration
2728 ret = vmbus_allocate_mmio(&hbus->mem_config, hbus->hdev, 0, -1,
2729 PCI_CONFIG_MMIO_LENGTH, 0x1000, false);
2734 * vmbus_allocate_mmio() gets used for allocating both device endpoint
2735 * resource claims (those which cannot be overlapped) and the ranges
2736 * which are valid for the children of this bus, which are intended
2737 * to be overlapped by those children. Set the flag on this claim
2738 * meaning that this region can't be overlapped.
2741 hbus->mem_config->flags |= IORESOURCE_BUSY;
2746 static void hv_free_config_window(struct hv_pcibus_device *hbus)
2748 vmbus_free_mmio(hbus->mem_config->start, PCI_CONFIG_MMIO_LENGTH);
2752 * hv_pci_enter_d0() - Bring the "bus" into the D0 power state
2753 * @hdev: VMBus's tracking struct for this root PCI bus
2755 * Return: 0 on success, -errno on failure
2757 static int hv_pci_enter_d0(struct hv_device *hdev)
2759 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
2760 struct pci_bus_d0_entry *d0_entry;
2761 struct hv_pci_compl comp_pkt;
2762 struct pci_packet *pkt;
2766 * Tell the host that the bus is ready to use, and moved into the
2767 * powered-on state. This includes telling the host which region
2768 * of memory-mapped I/O space has been chosen for configuration space
2771 pkt = kzalloc(sizeof(*pkt) + sizeof(*d0_entry), GFP_KERNEL);
2775 init_completion(&comp_pkt.host_event);
2776 pkt->completion_func = hv_pci_generic_compl;
2777 pkt->compl_ctxt = &comp_pkt;
2778 d0_entry = (struct pci_bus_d0_entry *)&pkt->message;
2779 d0_entry->message_type.type = PCI_BUS_D0ENTRY;
2780 d0_entry->mmio_base = hbus->mem_config->start;
2782 ret = vmbus_sendpacket(hdev->channel, d0_entry, sizeof(*d0_entry),
2783 (unsigned long)pkt, VM_PKT_DATA_INBAND,
2784 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
2786 ret = wait_for_response(hdev, &comp_pkt.host_event);
2791 if (comp_pkt.completion_status < 0) {
2792 dev_err(&hdev->device,
2793 "PCI Pass-through VSP failed D0 Entry with status %x\n",
2794 comp_pkt.completion_status);
2807 * hv_pci_query_relations() - Ask host to send list of child
2809 * @hdev: VMBus's tracking struct for this root PCI bus
2811 * Return: 0 on success, -errno on failure
2813 static int hv_pci_query_relations(struct hv_device *hdev)
2815 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
2816 struct pci_message message;
2817 struct completion comp;
2820 /* Ask the host to send along the list of child devices */
2821 init_completion(&comp);
2822 if (cmpxchg(&hbus->survey_event, NULL, &comp))
2825 memset(&message, 0, sizeof(message));
2826 message.type = PCI_QUERY_BUS_RELATIONS;
2828 ret = vmbus_sendpacket(hdev->channel, &message, sizeof(message),
2829 0, VM_PKT_DATA_INBAND, 0);
2831 ret = wait_for_response(hdev, &comp);
2837 * hv_send_resources_allocated() - Report local resource choices
2838 * @hdev: VMBus's tracking struct for this root PCI bus
2840 * The host OS is expecting to be sent a request as a message
2841 * which contains all the resources that the device will use.
2842 * The response contains those same resources, "translated"
2843 * which is to say, the values which should be used by the
2844 * hardware, when it delivers an interrupt. (MMIO resources are
2845 * used in local terms.) This is nice for Windows, and lines up
2846 * with the FDO/PDO split, which doesn't exist in Linux. Linux
2847 * is deeply expecting to scan an emulated PCI configuration
2848 * space. So this message is sent here only to drive the state
2849 * machine on the host forward.
2851 * Return: 0 on success, -errno on failure
2853 static int hv_send_resources_allocated(struct hv_device *hdev)
2855 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
2856 struct pci_resources_assigned *res_assigned;
2857 struct pci_resources_assigned2 *res_assigned2;
2858 struct hv_pci_compl comp_pkt;
2859 struct hv_pci_dev *hpdev;
2860 struct pci_packet *pkt;
2865 size_res = (hbus->protocol_version < PCI_PROTOCOL_VERSION_1_2)
2866 ? sizeof(*res_assigned) : sizeof(*res_assigned2);
2868 pkt = kmalloc(sizeof(*pkt) + size_res, GFP_KERNEL);
2874 for (wslot = 0; wslot < 256; wslot++) {
2875 hpdev = get_pcichild_wslot(hbus, wslot);
2879 memset(pkt, 0, sizeof(*pkt) + size_res);
2880 init_completion(&comp_pkt.host_event);
2881 pkt->completion_func = hv_pci_generic_compl;
2882 pkt->compl_ctxt = &comp_pkt;
2884 if (hbus->protocol_version < PCI_PROTOCOL_VERSION_1_2) {
2886 (struct pci_resources_assigned *)&pkt->message;
2887 res_assigned->message_type.type =
2888 PCI_RESOURCES_ASSIGNED;
2889 res_assigned->wslot.slot = hpdev->desc.win_slot.slot;
2892 (struct pci_resources_assigned2 *)&pkt->message;
2893 res_assigned2->message_type.type =
2894 PCI_RESOURCES_ASSIGNED2;
2895 res_assigned2->wslot.slot = hpdev->desc.win_slot.slot;
2897 put_pcichild(hpdev);
2899 ret = vmbus_sendpacket(hdev->channel, &pkt->message,
2900 size_res, (unsigned long)pkt,
2902 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
2904 ret = wait_for_response(hdev, &comp_pkt.host_event);
2908 if (comp_pkt.completion_status < 0) {
2910 dev_err(&hdev->device,
2911 "resource allocated returned 0x%x",
2912 comp_pkt.completion_status);
2922 * hv_send_resources_released() - Report local resources
2924 * @hdev: VMBus's tracking struct for this root PCI bus
2926 * Return: 0 on success, -errno on failure
2928 static int hv_send_resources_released(struct hv_device *hdev)
2930 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
2931 struct pci_child_message pkt;
2932 struct hv_pci_dev *hpdev;
2936 for (wslot = 0; wslot < 256; wslot++) {
2937 hpdev = get_pcichild_wslot(hbus, wslot);
2941 memset(&pkt, 0, sizeof(pkt));
2942 pkt.message_type.type = PCI_RESOURCES_RELEASED;
2943 pkt.wslot.slot = hpdev->desc.win_slot.slot;
2945 put_pcichild(hpdev);
2947 ret = vmbus_sendpacket(hdev->channel, &pkt, sizeof(pkt), 0,
2948 VM_PKT_DATA_INBAND, 0);
2956 static void get_hvpcibus(struct hv_pcibus_device *hbus)
2958 refcount_inc(&hbus->remove_lock);
2961 static void put_hvpcibus(struct hv_pcibus_device *hbus)
2963 if (refcount_dec_and_test(&hbus->remove_lock))
2964 complete(&hbus->remove_event);
2967 #define HVPCI_DOM_MAP_SIZE (64 * 1024)
2968 static DECLARE_BITMAP(hvpci_dom_map, HVPCI_DOM_MAP_SIZE);
2971 * PCI domain number 0 is used by emulated devices on Gen1 VMs, so define 0
2972 * as invalid for passthrough PCI devices of this driver.
2974 #define HVPCI_DOM_INVALID 0
2977 * hv_get_dom_num() - Get a valid PCI domain number
2978 * Check if the PCI domain number is in use, and return another number if
2981 * @dom: Requested domain number
2983 * return: domain number on success, HVPCI_DOM_INVALID on failure
2985 static u16 hv_get_dom_num(u16 dom)
2989 if (test_and_set_bit(dom, hvpci_dom_map) == 0)
2992 for_each_clear_bit(i, hvpci_dom_map, HVPCI_DOM_MAP_SIZE) {
2993 if (test_and_set_bit(i, hvpci_dom_map) == 0)
2997 return HVPCI_DOM_INVALID;
3001 * hv_put_dom_num() - Mark the PCI domain number as free
3002 * @dom: Domain number to be freed
3004 static void hv_put_dom_num(u16 dom)
3006 clear_bit(dom, hvpci_dom_map);
3010 * hv_pci_probe() - New VMBus channel probe, for a root PCI bus
3011 * @hdev: VMBus's tracking struct for this root PCI bus
3012 * @dev_id: Identifies the device itself
3014 * Return: 0 on success, -errno on failure
3016 static int hv_pci_probe(struct hv_device *hdev,
3017 const struct hv_vmbus_device_id *dev_id)
3019 struct hv_pcibus_device *hbus;
3025 * hv_pcibus_device contains the hypercall arguments for retargeting in
3026 * hv_irq_unmask(). Those must not cross a page boundary.
3028 BUILD_BUG_ON(sizeof(*hbus) > HV_HYP_PAGE_SIZE);
3031 * With the recent 59bb47985c1d ("mm, sl[aou]b: guarantee natural
3032 * alignment for kmalloc(power-of-two)"), kzalloc() is able to allocate
3033 * a 4KB buffer that is guaranteed to be 4KB-aligned. Here the size and
3034 * alignment of hbus is important because hbus's field
3035 * retarget_msi_interrupt_params must not cross a 4KB page boundary.
3037 * Here we prefer kzalloc to get_zeroed_page(), because a buffer
3038 * allocated by the latter is not tracked and scanned by kmemleak, and
3039 * hence kmemleak reports the pointer contained in the hbus buffer
3040 * (i.e. the hpdev struct, which is created in new_pcichild_device() and
3041 * is tracked by hbus->children) as memory leak (false positive).
3043 * If the kernel doesn't have 59bb47985c1d, get_zeroed_page() *must* be
3044 * used to allocate the hbus buffer and we can avoid the kmemleak false
3045 * positive by using kmemleak_alloc() and kmemleak_free() to ask
3046 * kmemleak to track and scan the hbus buffer.
3048 hbus = kzalloc(HV_HYP_PAGE_SIZE, GFP_KERNEL);
3051 hbus->state = hv_pcibus_init;
3054 * The PCI bus "domain" is what is called "segment" in ACPI and other
3055 * specs. Pull it from the instance ID, to get something usually
3056 * unique. In rare cases of collision, we will find out another number
3059 * Note that, since this code only runs in a Hyper-V VM, Hyper-V
3060 * together with this guest driver can guarantee that (1) The only
3061 * domain used by Gen1 VMs for something that looks like a physical
3062 * PCI bus (which is actually emulated by the hypervisor) is domain 0.
3063 * (2) There will be no overlap between domains (after fixing possible
3064 * collisions) in the same VM.
3066 dom_req = hdev->dev_instance.b[5] << 8 | hdev->dev_instance.b[4];
3067 dom = hv_get_dom_num(dom_req);
3069 if (dom == HVPCI_DOM_INVALID) {
3070 dev_err(&hdev->device,
3071 "Unable to use dom# 0x%hx or other numbers", dom_req);
3077 dev_info(&hdev->device,
3078 "PCI dom# 0x%hx has collision, using 0x%hx",
3081 hbus->sysdata.domain = dom;
3084 refcount_set(&hbus->remove_lock, 1);
3085 INIT_LIST_HEAD(&hbus->children);
3086 INIT_LIST_HEAD(&hbus->dr_list);
3087 INIT_LIST_HEAD(&hbus->resources_for_children);
3088 spin_lock_init(&hbus->config_lock);
3089 spin_lock_init(&hbus->device_list_lock);
3090 spin_lock_init(&hbus->retarget_msi_interrupt_lock);
3091 init_completion(&hbus->remove_event);
3092 hbus->wq = alloc_ordered_workqueue("hv_pci_%x", 0,
3093 hbus->sysdata.domain);
3099 ret = vmbus_open(hdev->channel, pci_ring_size, pci_ring_size, NULL, 0,
3100 hv_pci_onchannelcallback, hbus);
3104 hv_set_drvdata(hdev, hbus);
3106 ret = hv_pci_protocol_negotiation(hdev, pci_protocol_versions,
3107 ARRAY_SIZE(pci_protocol_versions));
3111 ret = hv_allocate_config_window(hbus);
3115 hbus->cfg_addr = ioremap(hbus->mem_config->start,
3116 PCI_CONFIG_MMIO_LENGTH);
3117 if (!hbus->cfg_addr) {
3118 dev_err(&hdev->device,
3119 "Unable to map a virtual address for config space\n");
3124 name = kasprintf(GFP_KERNEL, "%pUL", &hdev->dev_instance);
3130 hbus->sysdata.fwnode = irq_domain_alloc_named_fwnode(name);
3132 if (!hbus->sysdata.fwnode) {
3137 ret = hv_pcie_init_irq_domain(hbus);
3141 ret = hv_pci_query_relations(hdev);
3143 goto free_irq_domain;
3145 ret = hv_pci_enter_d0(hdev);
3147 goto free_irq_domain;
3149 ret = hv_pci_allocate_bridge_windows(hbus);
3151 goto free_irq_domain;
3153 ret = hv_send_resources_allocated(hdev);
3157 prepopulate_bars(hbus);
3159 hbus->state = hv_pcibus_probed;
3161 ret = create_root_hv_pci_bus(hbus);
3168 hv_pci_free_bridge_windows(hbus);
3170 irq_domain_remove(hbus->irq_domain);
3172 irq_domain_free_fwnode(hbus->sysdata.fwnode);
3174 iounmap(hbus->cfg_addr);
3176 hv_free_config_window(hbus);
3178 vmbus_close(hdev->channel);
3180 destroy_workqueue(hbus->wq);
3182 hv_put_dom_num(hbus->sysdata.domain);
3188 static int hv_pci_bus_exit(struct hv_device *hdev, bool hibernating)
3190 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
3192 struct pci_packet teardown_packet;
3193 u8 buffer[sizeof(struct pci_message)];
3195 struct hv_dr_state *dr;
3196 struct hv_pci_compl comp_pkt;
3200 * After the host sends the RESCIND_CHANNEL message, it doesn't
3201 * access the per-channel ringbuffer any longer.
3203 if (hdev->channel->rescind)
3207 /* Delete any children which might still exist. */
3208 dr = kzalloc(sizeof(*dr), GFP_KERNEL);
3209 if (dr && hv_pci_start_relations_work(hbus, dr))
3213 ret = hv_send_resources_released(hdev);
3215 dev_err(&hdev->device,
3216 "Couldn't send resources released packet(s)\n");
3220 memset(&pkt.teardown_packet, 0, sizeof(pkt.teardown_packet));
3221 init_completion(&comp_pkt.host_event);
3222 pkt.teardown_packet.completion_func = hv_pci_generic_compl;
3223 pkt.teardown_packet.compl_ctxt = &comp_pkt;
3224 pkt.teardown_packet.message[0].type = PCI_BUS_D0EXIT;
3226 ret = vmbus_sendpacket(hdev->channel, &pkt.teardown_packet.message,
3227 sizeof(struct pci_message),
3228 (unsigned long)&pkt.teardown_packet,
3230 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
3234 if (wait_for_completion_timeout(&comp_pkt.host_event, 10 * HZ) == 0)
3241 * hv_pci_remove() - Remove routine for this VMBus channel
3242 * @hdev: VMBus's tracking struct for this root PCI bus
3244 * Return: 0 on success, -errno on failure
3246 static int hv_pci_remove(struct hv_device *hdev)
3248 struct hv_pcibus_device *hbus;
3251 hbus = hv_get_drvdata(hdev);
3252 if (hbus->state == hv_pcibus_installed) {
3253 /* Remove the bus from PCI's point of view. */
3254 pci_lock_rescan_remove();
3255 pci_stop_root_bus(hbus->pci_bus);
3256 hv_pci_remove_slots(hbus);
3257 pci_remove_root_bus(hbus->pci_bus);
3258 pci_unlock_rescan_remove();
3259 hbus->state = hv_pcibus_removed;
3262 ret = hv_pci_bus_exit(hdev, false);
3264 vmbus_close(hdev->channel);
3266 iounmap(hbus->cfg_addr);
3267 hv_free_config_window(hbus);
3268 pci_free_resource_list(&hbus->resources_for_children);
3269 hv_pci_free_bridge_windows(hbus);
3270 irq_domain_remove(hbus->irq_domain);
3271 irq_domain_free_fwnode(hbus->sysdata.fwnode);
3273 wait_for_completion(&hbus->remove_event);
3274 destroy_workqueue(hbus->wq);
3276 hv_put_dom_num(hbus->sysdata.domain);
3282 static int hv_pci_suspend(struct hv_device *hdev)
3284 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
3285 enum hv_pcibus_state old_state;
3289 * hv_pci_suspend() must make sure there are no pending work items
3290 * before calling vmbus_close(), since it runs in a process context
3291 * as a callback in dpm_suspend(). When it starts to run, the channel
3292 * callback hv_pci_onchannelcallback(), which runs in a tasklet
3293 * context, can be still running concurrently and scheduling new work
3294 * items onto hbus->wq in hv_pci_devices_present() and
3295 * hv_pci_eject_device(), and the work item handlers can access the
3296 * vmbus channel, which can be being closed by hv_pci_suspend(), e.g.
3297 * the work item handler pci_devices_present_work() ->
3298 * new_pcichild_device() writes to the vmbus channel.
3300 * To eliminate the race, hv_pci_suspend() disables the channel
3301 * callback tasklet, sets hbus->state to hv_pcibus_removing, and
3302 * re-enables the tasklet. This way, when hv_pci_suspend() proceeds,
3303 * it knows that no new work item can be scheduled, and then it flushes
3304 * hbus->wq and safely closes the vmbus channel.
3306 tasklet_disable(&hdev->channel->callback_event);
3308 /* Change the hbus state to prevent new work items. */
3309 old_state = hbus->state;
3310 if (hbus->state == hv_pcibus_installed)
3311 hbus->state = hv_pcibus_removing;
3313 tasklet_enable(&hdev->channel->callback_event);
3315 if (old_state != hv_pcibus_installed)
3318 flush_workqueue(hbus->wq);
3320 ret = hv_pci_bus_exit(hdev, true);
3324 vmbus_close(hdev->channel);
3329 static int hv_pci_resume(struct hv_device *hdev)
3331 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
3332 enum pci_protocol_version_t version[1];
3335 hbus->state = hv_pcibus_init;
3337 ret = vmbus_open(hdev->channel, pci_ring_size, pci_ring_size, NULL, 0,
3338 hv_pci_onchannelcallback, hbus);
3342 /* Only use the version that was in use before hibernation. */
3343 version[0] = hbus->protocol_version;
3344 ret = hv_pci_protocol_negotiation(hdev, version, 1);
3348 ret = hv_pci_query_relations(hdev);
3352 ret = hv_pci_enter_d0(hdev);
3356 ret = hv_send_resources_allocated(hdev);
3360 prepopulate_bars(hbus);
3362 hbus->state = hv_pcibus_installed;
3365 vmbus_close(hdev->channel);
3369 static const struct hv_vmbus_device_id hv_pci_id_table[] = {
3370 /* PCI Pass-through Class ID */
3371 /* 44C4F61D-4444-4400-9D52-802E27EDE19F */
3376 MODULE_DEVICE_TABLE(vmbus, hv_pci_id_table);
3378 static struct hv_driver hv_pci_drv = {
3380 .id_table = hv_pci_id_table,
3381 .probe = hv_pci_probe,
3382 .remove = hv_pci_remove,
3383 .suspend = hv_pci_suspend,
3384 .resume = hv_pci_resume,
3387 static void __exit exit_hv_pci_drv(void)
3389 vmbus_driver_unregister(&hv_pci_drv);
3391 hvpci_block_ops.read_block = NULL;
3392 hvpci_block_ops.write_block = NULL;
3393 hvpci_block_ops.reg_blk_invalidate = NULL;
3396 static int __init init_hv_pci_drv(void)
3398 /* Set the invalid domain number's bit, so it will not be used */
3399 set_bit(HVPCI_DOM_INVALID, hvpci_dom_map);
3401 /* Initialize PCI block r/w interface */
3402 hvpci_block_ops.read_block = hv_read_config_block;
3403 hvpci_block_ops.write_block = hv_write_config_block;
3404 hvpci_block_ops.reg_blk_invalidate = hv_register_block_invalidate;
3406 return vmbus_driver_register(&hv_pci_drv);
3409 module_init(init_hv_pci_drv);
3410 module_exit(exit_hv_pci_drv);
3412 MODULE_DESCRIPTION("Hyper-V PCI");
3413 MODULE_LICENSE("GPL v2");