2 * Copyright (c) Microsoft Corporation.
5 * Jake Oshins <jakeo@microsoft.com>
7 * This driver acts as a paravirtual front-end for PCI Express root buses.
8 * When a PCI Express function (either an entire device or an SR-IOV
9 * Virtual Function) is being passed through to the VM, this driver exposes
10 * a new bus to the guest VM. This is modeled as a root PCI bus because
11 * no bridges are being exposed to the VM. In fact, with a "Generation 2"
12 * VM within Hyper-V, there may seem to be no PCI bus at all in the VM
13 * until a device as been exposed using this driver.
15 * Each root PCI bus has its own PCI domain, which is called "Segment" in
16 * the PCI Firmware Specifications. Thus while each device passed through
17 * to the VM using this front-end will appear at "device 0", the domain will
18 * be unique. Typically, each bus will have one PCI function on it, though
19 * this driver does support more than one.
21 * In order to map the interrupts from the device through to the guest VM,
22 * this driver also implements an IRQ Domain, which handles interrupts (either
23 * MSI or MSI-X) associated with the functions on the bus. As interrupts are
24 * set up, torn down, or reaffined, this driver communicates with the
25 * underlying hypervisor to adjust the mappings in the I/O MMU so that each
26 * interrupt will be delivered to the correct virtual processor at the right
27 * vector. This driver does not support level-triggered (line-based)
28 * interrupts, and will report that the Interrupt Line register in the
29 * function's configuration space is zero.
31 * The rest of this driver mostly maps PCI concepts onto underlying Hyper-V
32 * facilities. For instance, the configuration space of a function exposed
33 * by Hyper-V is mapped into a single page of memory space, and the
34 * read and write handlers for config space must be aware of this mechanism.
35 * Similarly, device setup and teardown involves messages sent to and from
36 * the PCI back-end driver in Hyper-V.
38 * This program is free software; you can redistribute it and/or modify it
39 * under the terms of the GNU General Public License version 2 as published
40 * by the Free Software Foundation.
42 * This program is distributed in the hope that it will be useful, but
43 * WITHOUT ANY WARRANTY; without even the implied warranty of
44 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
45 * NON INFRINGEMENT. See the GNU General Public License for more
50 #include <linux/kernel.h>
51 #include <linux/module.h>
52 #include <linux/pci.h>
53 #include <linux/semaphore.h>
54 #include <linux/irqdomain.h>
55 #include <asm/irqdomain.h>
57 #include <linux/msi.h>
58 #include <linux/hyperv.h>
59 #include <asm/mshyperv.h>
62 * Protocol versions. The low word is the minor version, the high word the
66 #define PCI_MAKE_VERSION(major, minor) ((u32)(((major) << 16) | (major)))
67 #define PCI_MAJOR_VERSION(version) ((u32)(version) >> 16)
68 #define PCI_MINOR_VERSION(version) ((u32)(version) & 0xff)
71 PCI_PROTOCOL_VERSION_1_1
= PCI_MAKE_VERSION(1, 1),
72 PCI_PROTOCOL_VERSION_CURRENT
= PCI_PROTOCOL_VERSION_1_1
75 #define PCI_CONFIG_MMIO_LENGTH 0x2000
76 #define CFG_PAGE_OFFSET 0x1000
77 #define CFG_PAGE_SIZE (PCI_CONFIG_MMIO_LENGTH - CFG_PAGE_OFFSET)
79 #define MAX_SUPPORTED_MSI_MESSAGES 0x400
85 enum pci_message_type
{
89 PCI_MESSAGE_BASE
= 0x42490000,
90 PCI_BUS_RELATIONS
= PCI_MESSAGE_BASE
+ 0,
91 PCI_QUERY_BUS_RELATIONS
= PCI_MESSAGE_BASE
+ 1,
92 PCI_POWER_STATE_CHANGE
= PCI_MESSAGE_BASE
+ 4,
93 PCI_QUERY_RESOURCE_REQUIREMENTS
= PCI_MESSAGE_BASE
+ 5,
94 PCI_QUERY_RESOURCE_RESOURCES
= PCI_MESSAGE_BASE
+ 6,
95 PCI_BUS_D0ENTRY
= PCI_MESSAGE_BASE
+ 7,
96 PCI_BUS_D0EXIT
= PCI_MESSAGE_BASE
+ 8,
97 PCI_READ_BLOCK
= PCI_MESSAGE_BASE
+ 9,
98 PCI_WRITE_BLOCK
= PCI_MESSAGE_BASE
+ 0xA,
99 PCI_EJECT
= PCI_MESSAGE_BASE
+ 0xB,
100 PCI_QUERY_STOP
= PCI_MESSAGE_BASE
+ 0xC,
101 PCI_REENABLE
= PCI_MESSAGE_BASE
+ 0xD,
102 PCI_QUERY_STOP_FAILED
= PCI_MESSAGE_BASE
+ 0xE,
103 PCI_EJECTION_COMPLETE
= PCI_MESSAGE_BASE
+ 0xF,
104 PCI_RESOURCES_ASSIGNED
= PCI_MESSAGE_BASE
+ 0x10,
105 PCI_RESOURCES_RELEASED
= PCI_MESSAGE_BASE
+ 0x11,
106 PCI_INVALIDATE_BLOCK
= PCI_MESSAGE_BASE
+ 0x12,
107 PCI_QUERY_PROTOCOL_VERSION
= PCI_MESSAGE_BASE
+ 0x13,
108 PCI_CREATE_INTERRUPT_MESSAGE
= PCI_MESSAGE_BASE
+ 0x14,
109 PCI_DELETE_INTERRUPT_MESSAGE
= PCI_MESSAGE_BASE
+ 0x15,
114 * Structures defining the virtual PCI Express protocol.
126 * Function numbers are 8-bits wide on Express, as interpreted through ARI,
127 * which is all this driver does. This representation is the one used in
128 * Windows, which is what is expected when sending this back and forth with
129 * the Hyper-V parent partition.
131 union win_slot_encoding
{
141 * Pretty much as defined in the PCI Specifications.
143 struct pci_function_description
{
144 u16 v_id
; /* vendor ID */
145 u16 d_id
; /* device ID */
151 union win_slot_encoding win_slot
;
152 u32 ser
; /* serial number */
158 * @delivery_mode: As defined in Intel's Programmer's
159 * Reference Manual, Volume 3, Chapter 8.
160 * @vector_count: Number of contiguous entries in the
161 * Interrupt Descriptor Table that are
162 * occupied by this Message-Signaled
163 * Interrupt. For "MSI", as first defined
164 * in PCI 2.2, this can be between 1 and
165 * 32. For "MSI-X," as first defined in PCI
166 * 3.0, this must be 1, as each MSI-X table
167 * entry would have its own descriptor.
168 * @reserved: Empty space
169 * @cpu_mask: All the target virtual processors.
180 * struct tran_int_desc
181 * @reserved: unused, padding
182 * @vector_count: same as in hv_msi_desc
183 * @data: This is the "data payload" value that is
184 * written by the device when it generates
185 * a message-signaled interrupt, either MSI
187 * @address: This is the address to which the data
188 * payload is written on interrupt
191 struct tran_int_desc
{
199 * A generic message format for virtual PCI.
200 * Specific message formats are defined later in the file.
207 struct pci_child_message
{
208 struct pci_message message_type
;
209 union win_slot_encoding wslot
;
212 struct pci_incoming_message
{
213 struct vmpacket_descriptor hdr
;
214 struct pci_message message_type
;
217 struct pci_response
{
218 struct vmpacket_descriptor hdr
;
219 s32 status
; /* negative values are failures */
223 void (*completion_func
)(void *context
, struct pci_response
*resp
,
224 int resp_packet_size
);
227 struct pci_message message
[0];
231 * Specific message types supporting the PCI protocol.
235 * Version negotiation message. Sent from the guest to the host.
236 * The guest is free to try different versions until the host
237 * accepts the version.
239 * pci_version: The protocol version requested.
240 * is_last_attempt: If TRUE, this is the last version guest will request.
241 * reservedz: Reserved field, set to zero.
244 struct pci_version_request
{
245 struct pci_message message_type
;
246 enum pci_message_type protocol_version
;
250 * Bus D0 Entry. This is sent from the guest to the host when the virtual
251 * bus (PCI Express port) is ready for action.
254 struct pci_bus_d0_entry
{
255 struct pci_message message_type
;
260 struct pci_bus_relations
{
261 struct pci_incoming_message incoming
;
263 struct pci_function_description func
[0];
266 struct pci_q_res_req_response
{
267 struct vmpacket_descriptor hdr
;
268 s32 status
; /* negative values are failures */
272 struct pci_set_power
{
273 struct pci_message message_type
;
274 union win_slot_encoding wslot
;
275 u32 power_state
; /* In Windows terms */
279 struct pci_set_power_response
{
280 struct vmpacket_descriptor hdr
;
281 s32 status
; /* negative values are failures */
282 union win_slot_encoding wslot
;
283 u32 resultant_state
; /* In Windows terms */
287 struct pci_resources_assigned
{
288 struct pci_message message_type
;
289 union win_slot_encoding wslot
;
290 u8 memory_range
[0x14][6]; /* not used here */
295 struct pci_create_interrupt
{
296 struct pci_message message_type
;
297 union win_slot_encoding wslot
;
298 struct hv_msi_desc int_desc
;
301 struct pci_create_int_response
{
302 struct pci_response response
;
304 struct tran_int_desc int_desc
;
307 struct pci_delete_interrupt
{
308 struct pci_message message_type
;
309 union win_slot_encoding wslot
;
310 struct tran_int_desc int_desc
;
313 struct pci_dev_incoming
{
314 struct pci_incoming_message incoming
;
315 union win_slot_encoding wslot
;
318 struct pci_eject_response
{
319 struct pci_message message_type
;
320 union win_slot_encoding wslot
;
324 static int pci_ring_size
= (4 * PAGE_SIZE
);
327 * Definitions or interrupt steering hypercall.
329 #define HV_PARTITION_ID_SELF ((u64)-1)
330 #define HVCALL_RETARGET_INTERRUPT 0x7e
332 struct retarget_msi_interrupt
{
333 u64 partition_id
; /* use "self" */
335 u32 source
; /* 1 for MSI(-X) */
346 * Driver specific state.
349 enum hv_pcibus_state
{
356 struct hv_pcibus_device
{
357 struct pci_sysdata sysdata
;
358 enum hv_pcibus_state state
;
359 atomic_t remove_lock
;
360 struct hv_device
*hdev
;
361 resource_size_t low_mmio_space
;
362 resource_size_t high_mmio_space
;
363 struct resource
*mem_config
;
364 struct resource
*low_mmio_res
;
365 struct resource
*high_mmio_res
;
366 struct completion
*survey_event
;
367 struct completion remove_event
;
368 struct pci_bus
*pci_bus
;
369 spinlock_t config_lock
; /* Avoid two threads writing index page */
370 spinlock_t device_list_lock
; /* Protect lists below */
371 void __iomem
*cfg_addr
;
373 struct semaphore enum_sem
;
374 struct list_head resources_for_children
;
376 struct list_head children
;
377 struct list_head dr_list
;
379 struct msi_domain_info msi_info
;
380 struct msi_controller msi_chip
;
381 struct irq_domain
*irq_domain
;
382 struct retarget_msi_interrupt retarget_msi_interrupt_params
;
383 spinlock_t retarget_msi_interrupt_lock
;
387 * Tracks "Device Relations" messages from the host, which must be both
388 * processed in order and deferred so that they don't run in the context
389 * of the incoming packet callback.
392 struct work_struct wrk
;
393 struct hv_pcibus_device
*bus
;
397 struct list_head list_entry
;
399 struct pci_function_description func
[0];
402 enum hv_pcichild_state
{
403 hv_pcichild_init
= 0,
404 hv_pcichild_requirements
,
405 hv_pcichild_resourced
,
406 hv_pcichild_ejecting
,
410 enum hv_pcidev_ref_reason
{
411 hv_pcidev_ref_invalid
= 0,
412 hv_pcidev_ref_initial
,
413 hv_pcidev_ref_by_slot
,
414 hv_pcidev_ref_packet
,
416 hv_pcidev_ref_childlist
,
422 /* List protected by pci_rescan_remove_lock */
423 struct list_head list_entry
;
425 enum hv_pcichild_state state
;
426 struct pci_function_description desc
;
427 bool reported_missing
;
428 struct hv_pcibus_device
*hbus
;
429 struct work_struct wrk
;
432 * What would be observed if one wrote 0xFFFFFFFF to a BAR and then
433 * read it back, for each of the BAR offsets within config space.
438 struct hv_pci_compl
{
439 struct completion host_event
;
440 s32 completion_status
;
444 * hv_pci_generic_compl() - Invoked for a completion packet
445 * @context: Set up by the sender of the packet.
446 * @resp: The response packet
447 * @resp_packet_size: Size in bytes of the packet
449 * This function is used to trigger an event and report status
450 * for any message for which the completion packet contains a
451 * status and nothing else.
453 static void hv_pci_generic_compl(void *context
, struct pci_response
*resp
,
454 int resp_packet_size
)
456 struct hv_pci_compl
*comp_pkt
= context
;
458 if (resp_packet_size
>= offsetofend(struct pci_response
, status
))
459 comp_pkt
->completion_status
= resp
->status
;
461 comp_pkt
->completion_status
= -1;
463 complete(&comp_pkt
->host_event
);
466 static struct hv_pci_dev
*get_pcichild_wslot(struct hv_pcibus_device
*hbus
,
468 static void get_pcichild(struct hv_pci_dev
*hv_pcidev
,
469 enum hv_pcidev_ref_reason reason
);
470 static void put_pcichild(struct hv_pci_dev
*hv_pcidev
,
471 enum hv_pcidev_ref_reason reason
);
473 static void get_hvpcibus(struct hv_pcibus_device
*hv_pcibus
);
474 static void put_hvpcibus(struct hv_pcibus_device
*hv_pcibus
);
477 * devfn_to_wslot() - Convert from Linux PCI slot to Windows
478 * @devfn: The Linux representation of PCI slot
480 * Windows uses a slightly different representation of PCI slot.
482 * Return: The Windows representation
484 static u32
devfn_to_wslot(int devfn
)
486 union win_slot_encoding wslot
;
489 wslot
.bits
.dev
= PCI_SLOT(devfn
);
490 wslot
.bits
.func
= PCI_FUNC(devfn
);
496 * wslot_to_devfn() - Convert from Windows PCI slot to Linux
497 * @wslot: The Windows representation of PCI slot
499 * Windows uses a slightly different representation of PCI slot.
501 * Return: The Linux representation
503 static int wslot_to_devfn(u32 wslot
)
505 union win_slot_encoding slot_no
;
507 slot_no
.slot
= wslot
;
508 return PCI_DEVFN(slot_no
.bits
.dev
, slot_no
.bits
.func
);
512 * PCI Configuration Space for these root PCI buses is implemented as a pair
513 * of pages in memory-mapped I/O space. Writing to the first page chooses
514 * the PCI function being written or read. Once the first page has been
515 * written to, the following page maps in the entire configuration space of
520 * _hv_pcifront_read_config() - Internal PCI config read
521 * @hpdev: The PCI driver's representation of the device
522 * @where: Offset within config space
523 * @size: Size of the transfer
524 * @val: Pointer to the buffer receiving the data
526 static void _hv_pcifront_read_config(struct hv_pci_dev
*hpdev
, int where
,
530 void __iomem
*addr
= hpdev
->hbus
->cfg_addr
+ CFG_PAGE_OFFSET
+ where
;
533 * If the attempt is to read the IDs or the ROM BAR, simulate that.
535 if (where
+ size
<= PCI_COMMAND
) {
536 memcpy(val
, ((u8
*)&hpdev
->desc
.v_id
) + where
, size
);
537 } else if (where
>= PCI_CLASS_REVISION
&& where
+ size
<=
538 PCI_CACHE_LINE_SIZE
) {
539 memcpy(val
, ((u8
*)&hpdev
->desc
.rev
) + where
-
540 PCI_CLASS_REVISION
, size
);
541 } else if (where
>= PCI_SUBSYSTEM_VENDOR_ID
&& where
+ size
<=
543 memcpy(val
, (u8
*)&hpdev
->desc
.subsystem_id
+ where
-
544 PCI_SUBSYSTEM_VENDOR_ID
, size
);
545 } else if (where
>= PCI_ROM_ADDRESS
&& where
+ size
<=
546 PCI_CAPABILITY_LIST
) {
547 /* ROM BARs are unimplemented */
549 } else if (where
>= PCI_INTERRUPT_LINE
&& where
+ size
<=
552 * Interrupt Line and Interrupt PIN are hard-wired to zero
553 * because this front-end only supports message-signaled
557 } else if (where
+ size
<= CFG_PAGE_SIZE
) {
558 spin_lock_irqsave(&hpdev
->hbus
->config_lock
, flags
);
559 /* Choose the function to be read. (See comment above) */
560 writel(hpdev
->desc
.win_slot
.slot
, hpdev
->hbus
->cfg_addr
);
561 /* Make sure the function was chosen before we start reading. */
563 /* Read from that function's config space. */
576 * Make sure the write was done before we release the spinlock
577 * allowing consecutive reads/writes.
580 spin_unlock_irqrestore(&hpdev
->hbus
->config_lock
, flags
);
582 dev_err(&hpdev
->hbus
->hdev
->device
,
583 "Attempt to read beyond a function's config space.\n");
588 * _hv_pcifront_write_config() - Internal PCI config write
589 * @hpdev: The PCI driver's representation of the device
590 * @where: Offset within config space
591 * @size: Size of the transfer
592 * @val: The data being transferred
594 static void _hv_pcifront_write_config(struct hv_pci_dev
*hpdev
, int where
,
598 void __iomem
*addr
= hpdev
->hbus
->cfg_addr
+ CFG_PAGE_OFFSET
+ where
;
600 if (where
>= PCI_SUBSYSTEM_VENDOR_ID
&&
601 where
+ size
<= PCI_CAPABILITY_LIST
) {
602 /* SSIDs and ROM BARs are read-only */
603 } else if (where
>= PCI_COMMAND
&& where
+ size
<= CFG_PAGE_SIZE
) {
604 spin_lock_irqsave(&hpdev
->hbus
->config_lock
, flags
);
605 /* Choose the function to be written. (See comment above) */
606 writel(hpdev
->desc
.win_slot
.slot
, hpdev
->hbus
->cfg_addr
);
607 /* Make sure the function was chosen before we start writing. */
609 /* Write to that function's config space. */
622 * Make sure the write was done before we release the spinlock
623 * allowing consecutive reads/writes.
626 spin_unlock_irqrestore(&hpdev
->hbus
->config_lock
, flags
);
628 dev_err(&hpdev
->hbus
->hdev
->device
,
629 "Attempt to write beyond a function's config space.\n");
634 * hv_pcifront_read_config() - Read configuration space
635 * @bus: PCI Bus structure
636 * @devfn: Device/function
637 * @where: Offset from base
638 * @size: Byte/word/dword
639 * @val: Value to be read
641 * Return: PCIBIOS_SUCCESSFUL on success
642 * PCIBIOS_DEVICE_NOT_FOUND on failure
644 static int hv_pcifront_read_config(struct pci_bus
*bus
, unsigned int devfn
,
645 int where
, int size
, u32
*val
)
647 struct hv_pcibus_device
*hbus
=
648 container_of(bus
->sysdata
, struct hv_pcibus_device
, sysdata
);
649 struct hv_pci_dev
*hpdev
;
651 hpdev
= get_pcichild_wslot(hbus
, devfn_to_wslot(devfn
));
653 return PCIBIOS_DEVICE_NOT_FOUND
;
655 _hv_pcifront_read_config(hpdev
, where
, size
, val
);
657 put_pcichild(hpdev
, hv_pcidev_ref_by_slot
);
658 return PCIBIOS_SUCCESSFUL
;
662 * hv_pcifront_write_config() - Write configuration space
663 * @bus: PCI Bus structure
664 * @devfn: Device/function
665 * @where: Offset from base
666 * @size: Byte/word/dword
667 * @val: Value to be written to device
669 * Return: PCIBIOS_SUCCESSFUL on success
670 * PCIBIOS_DEVICE_NOT_FOUND on failure
672 static int hv_pcifront_write_config(struct pci_bus
*bus
, unsigned int devfn
,
673 int where
, int size
, u32 val
)
675 struct hv_pcibus_device
*hbus
=
676 container_of(bus
->sysdata
, struct hv_pcibus_device
, sysdata
);
677 struct hv_pci_dev
*hpdev
;
679 hpdev
= get_pcichild_wslot(hbus
, devfn_to_wslot(devfn
));
681 return PCIBIOS_DEVICE_NOT_FOUND
;
683 _hv_pcifront_write_config(hpdev
, where
, size
, val
);
685 put_pcichild(hpdev
, hv_pcidev_ref_by_slot
);
686 return PCIBIOS_SUCCESSFUL
;
689 /* PCIe operations */
690 static struct pci_ops hv_pcifront_ops
= {
691 .read
= hv_pcifront_read_config
,
692 .write
= hv_pcifront_write_config
,
695 /* Interrupt management hooks */
696 static void hv_int_desc_free(struct hv_pci_dev
*hpdev
,
697 struct tran_int_desc
*int_desc
)
699 struct pci_delete_interrupt
*int_pkt
;
701 struct pci_packet pkt
;
702 u8 buffer
[sizeof(struct pci_delete_interrupt
)];
705 memset(&ctxt
, 0, sizeof(ctxt
));
706 int_pkt
= (struct pci_delete_interrupt
*)&ctxt
.pkt
.message
;
707 int_pkt
->message_type
.type
=
708 PCI_DELETE_INTERRUPT_MESSAGE
;
709 int_pkt
->wslot
.slot
= hpdev
->desc
.win_slot
.slot
;
710 int_pkt
->int_desc
= *int_desc
;
711 vmbus_sendpacket(hpdev
->hbus
->hdev
->channel
, int_pkt
, sizeof(*int_pkt
),
712 (unsigned long)&ctxt
.pkt
, VM_PKT_DATA_INBAND
, 0);
717 * hv_msi_free() - Free the MSI.
718 * @domain: The interrupt domain pointer
719 * @info: Extra MSI-related context
720 * @irq: Identifies the IRQ.
722 * The Hyper-V parent partition and hypervisor are tracking the
723 * messages that are in use, keeping the interrupt redirection
724 * table up to date. This callback sends a message that frees
725 * the IRT entry and related tracking nonsense.
727 static void hv_msi_free(struct irq_domain
*domain
, struct msi_domain_info
*info
,
730 struct hv_pcibus_device
*hbus
;
731 struct hv_pci_dev
*hpdev
;
732 struct pci_dev
*pdev
;
733 struct tran_int_desc
*int_desc
;
734 struct irq_data
*irq_data
= irq_domain_get_irq_data(domain
, irq
);
735 struct msi_desc
*msi
= irq_data_get_msi_desc(irq_data
);
737 pdev
= msi_desc_to_pci_dev(msi
);
739 int_desc
= irq_data_get_irq_chip_data(irq_data
);
743 irq_data
->chip_data
= NULL
;
744 hpdev
= get_pcichild_wslot(hbus
, devfn_to_wslot(pdev
->devfn
));
750 hv_int_desc_free(hpdev
, int_desc
);
751 put_pcichild(hpdev
, hv_pcidev_ref_by_slot
);
754 static int hv_set_affinity(struct irq_data
*data
, const struct cpumask
*dest
,
757 struct irq_data
*parent
= data
->parent_data
;
759 return parent
->chip
->irq_set_affinity(parent
, dest
, force
);
762 static void hv_irq_mask(struct irq_data
*data
)
764 pci_msi_mask_irq(data
);
768 * hv_irq_unmask() - "Unmask" the IRQ by setting its current
770 * @data: Describes the IRQ
772 * Build new a destination for the MSI and make a hypercall to
773 * update the Interrupt Redirection Table. "Device Logical ID"
774 * is built out of this PCI bus's instance GUID and the function
775 * number of the device.
777 static void hv_irq_unmask(struct irq_data
*data
)
779 struct msi_desc
*msi_desc
= irq_data_get_msi_desc(data
);
780 struct irq_cfg
*cfg
= irqd_cfg(data
);
781 struct retarget_msi_interrupt
*params
;
782 struct hv_pcibus_device
*hbus
;
783 struct cpumask
*dest
;
784 struct pci_bus
*pbus
;
785 struct pci_dev
*pdev
;
789 dest
= irq_data_get_affinity_mask(data
);
790 pdev
= msi_desc_to_pci_dev(msi_desc
);
792 hbus
= container_of(pbus
->sysdata
, struct hv_pcibus_device
, sysdata
);
794 spin_lock_irqsave(&hbus
->retarget_msi_interrupt_lock
, flags
);
796 params
= &hbus
->retarget_msi_interrupt_params
;
797 memset(params
, 0, sizeof(*params
));
798 params
->partition_id
= HV_PARTITION_ID_SELF
;
799 params
->source
= 1; /* MSI(-X) */
800 params
->address
= msi_desc
->msg
.address_lo
;
801 params
->data
= msi_desc
->msg
.data
;
802 params
->device_id
= (hbus
->hdev
->dev_instance
.b
[5] << 24) |
803 (hbus
->hdev
->dev_instance
.b
[4] << 16) |
804 (hbus
->hdev
->dev_instance
.b
[7] << 8) |
805 (hbus
->hdev
->dev_instance
.b
[6] & 0xf8) |
806 PCI_FUNC(pdev
->devfn
);
807 params
->vector
= cfg
->vector
;
809 for_each_cpu_and(cpu
, dest
, cpu_online_mask
)
810 params
->vp_mask
|= (1ULL << vmbus_cpu_number_to_vp_number(cpu
));
812 hv_do_hypercall(HVCALL_RETARGET_INTERRUPT
, params
, NULL
);
814 spin_unlock_irqrestore(&hbus
->retarget_msi_interrupt_lock
, flags
);
816 pci_msi_unmask_irq(data
);
819 struct compose_comp_ctxt
{
820 struct hv_pci_compl comp_pkt
;
821 struct tran_int_desc int_desc
;
824 static void hv_pci_compose_compl(void *context
, struct pci_response
*resp
,
825 int resp_packet_size
)
827 struct compose_comp_ctxt
*comp_pkt
= context
;
828 struct pci_create_int_response
*int_resp
=
829 (struct pci_create_int_response
*)resp
;
831 comp_pkt
->comp_pkt
.completion_status
= resp
->status
;
832 comp_pkt
->int_desc
= int_resp
->int_desc
;
833 complete(&comp_pkt
->comp_pkt
.host_event
);
837 * hv_compose_msi_msg() - Supplies a valid MSI address/data
838 * @data: Everything about this MSI
839 * @msg: Buffer that is filled in by this function
841 * This function unpacks the IRQ looking for target CPU set, IDT
842 * vector and mode and sends a message to the parent partition
843 * asking for a mapping for that tuple in this partition. The
844 * response supplies a data value and address to which that data
845 * should be written to trigger that interrupt.
847 static void hv_compose_msi_msg(struct irq_data
*data
, struct msi_msg
*msg
)
849 struct irq_cfg
*cfg
= irqd_cfg(data
);
850 struct hv_pcibus_device
*hbus
;
851 struct hv_pci_dev
*hpdev
;
852 struct pci_bus
*pbus
;
853 struct pci_dev
*pdev
;
854 struct pci_create_interrupt
*int_pkt
;
855 struct compose_comp_ctxt comp
;
856 struct tran_int_desc
*int_desc
;
857 struct cpumask
*affinity
;
859 struct pci_packet pkt
;
860 u8 buffer
[sizeof(struct pci_create_interrupt
)];
865 pdev
= msi_desc_to_pci_dev(irq_data_get_msi_desc(data
));
867 hbus
= container_of(pbus
->sysdata
, struct hv_pcibus_device
, sysdata
);
868 hpdev
= get_pcichild_wslot(hbus
, devfn_to_wslot(pdev
->devfn
));
870 goto return_null_message
;
872 /* Free any previous message that might have already been composed. */
873 if (data
->chip_data
) {
874 int_desc
= data
->chip_data
;
875 data
->chip_data
= NULL
;
876 hv_int_desc_free(hpdev
, int_desc
);
879 int_desc
= kzalloc(sizeof(*int_desc
), GFP_KERNEL
);
883 memset(&ctxt
, 0, sizeof(ctxt
));
884 init_completion(&comp
.comp_pkt
.host_event
);
885 ctxt
.pkt
.completion_func
= hv_pci_compose_compl
;
886 ctxt
.pkt
.compl_ctxt
= &comp
;
887 int_pkt
= (struct pci_create_interrupt
*)&ctxt
.pkt
.message
;
888 int_pkt
->message_type
.type
= PCI_CREATE_INTERRUPT_MESSAGE
;
889 int_pkt
->wslot
.slot
= hpdev
->desc
.win_slot
.slot
;
890 int_pkt
->int_desc
.vector
= cfg
->vector
;
891 int_pkt
->int_desc
.vector_count
= 1;
892 int_pkt
->int_desc
.delivery_mode
=
893 (apic
->irq_delivery_mode
== dest_LowestPrio
) ? 1 : 0;
896 * This bit doesn't have to work on machines with more than 64
897 * processors because Hyper-V only supports 64 in a guest.
899 affinity
= irq_data_get_affinity_mask(data
);
900 for_each_cpu_and(cpu
, affinity
, cpu_online_mask
) {
901 int_pkt
->int_desc
.cpu_mask
|=
902 (1ULL << vmbus_cpu_number_to_vp_number(cpu
));
905 ret
= vmbus_sendpacket(hpdev
->hbus
->hdev
->channel
, int_pkt
,
906 sizeof(*int_pkt
), (unsigned long)&ctxt
.pkt
,
908 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
912 wait_for_completion(&comp
.comp_pkt
.host_event
);
914 if (comp
.comp_pkt
.completion_status
< 0) {
915 dev_err(&hbus
->hdev
->device
,
916 "Request for interrupt failed: 0x%x",
917 comp
.comp_pkt
.completion_status
);
922 * Record the assignment so that this can be unwound later. Using
923 * irq_set_chip_data() here would be appropriate, but the lock it takes
926 *int_desc
= comp
.int_desc
;
927 data
->chip_data
= int_desc
;
929 /* Pass up the result. */
930 msg
->address_hi
= comp
.int_desc
.address
>> 32;
931 msg
->address_lo
= comp
.int_desc
.address
& 0xffffffff;
932 msg
->data
= comp
.int_desc
.data
;
934 put_pcichild(hpdev
, hv_pcidev_ref_by_slot
);
940 put_pcichild(hpdev
, hv_pcidev_ref_by_slot
);
947 /* HW Interrupt Chip Descriptor */
948 static struct irq_chip hv_msi_irq_chip
= {
949 .name
= "Hyper-V PCIe MSI",
950 .irq_compose_msi_msg
= hv_compose_msi_msg
,
951 .irq_set_affinity
= hv_set_affinity
,
952 .irq_ack
= irq_chip_ack_parent
,
953 .irq_mask
= hv_irq_mask
,
954 .irq_unmask
= hv_irq_unmask
,
957 static irq_hw_number_t
hv_msi_domain_ops_get_hwirq(struct msi_domain_info
*info
,
958 msi_alloc_info_t
*arg
)
960 return arg
->msi_hwirq
;
963 static struct msi_domain_ops hv_msi_ops
= {
964 .get_hwirq
= hv_msi_domain_ops_get_hwirq
,
965 .msi_prepare
= pci_msi_prepare
,
966 .set_desc
= pci_msi_set_desc
,
967 .msi_free
= hv_msi_free
,
971 * hv_pcie_init_irq_domain() - Initialize IRQ domain
972 * @hbus: The root PCI bus
974 * This function creates an IRQ domain which will be used for
975 * interrupts from devices that have been passed through. These
976 * devices only support MSI and MSI-X, not line-based interrupts
977 * or simulations of line-based interrupts through PCIe's
978 * fabric-layer messages. Because interrupts are remapped, we
979 * can support multi-message MSI here.
981 * Return: '0' on success and error value on failure
983 static int hv_pcie_init_irq_domain(struct hv_pcibus_device
*hbus
)
985 hbus
->msi_info
.chip
= &hv_msi_irq_chip
;
986 hbus
->msi_info
.ops
= &hv_msi_ops
;
987 hbus
->msi_info
.flags
= (MSI_FLAG_USE_DEF_DOM_OPS
|
988 MSI_FLAG_USE_DEF_CHIP_OPS
| MSI_FLAG_MULTI_PCI_MSI
|
990 hbus
->msi_info
.handler
= handle_edge_irq
;
991 hbus
->msi_info
.handler_name
= "edge";
992 hbus
->msi_info
.data
= hbus
;
993 hbus
->irq_domain
= pci_msi_create_irq_domain(hbus
->sysdata
.fwnode
,
996 if (!hbus
->irq_domain
) {
997 dev_err(&hbus
->hdev
->device
,
998 "Failed to build an MSI IRQ domain\n");
1006 * get_bar_size() - Get the address space consumed by a BAR
1007 * @bar_val: Value that a BAR returned after -1 was written
1010 * This function returns the size of the BAR, rounded up to 1
1011 * page. It has to be rounded up because the hypervisor's page
1012 * table entry that maps the BAR into the VM can't specify an
1013 * offset within a page. The invariant is that the hypervisor
1014 * must place any BARs of smaller than page length at the
1015 * beginning of a page.
1017 * Return: Size in bytes of the consumed MMIO space.
1019 static u64
get_bar_size(u64 bar_val
)
1021 return round_up((1 + ~(bar_val
& PCI_BASE_ADDRESS_MEM_MASK
)),
1026 * survey_child_resources() - Total all MMIO requirements
1027 * @hbus: Root PCI bus, as understood by this driver
1029 static void survey_child_resources(struct hv_pcibus_device
*hbus
)
1031 struct list_head
*iter
;
1032 struct hv_pci_dev
*hpdev
;
1033 resource_size_t bar_size
= 0;
1034 unsigned long flags
;
1035 struct completion
*event
;
1039 /* If nobody is waiting on the answer, don't compute it. */
1040 event
= xchg(&hbus
->survey_event
, NULL
);
1044 /* If the answer has already been computed, go with it. */
1045 if (hbus
->low_mmio_space
|| hbus
->high_mmio_space
) {
1050 spin_lock_irqsave(&hbus
->device_list_lock
, flags
);
1053 * Due to an interesting quirk of the PCI spec, all memory regions
1054 * for a child device are a power of 2 in size and aligned in memory,
1055 * so it's sufficient to just add them up without tracking alignment.
1057 list_for_each(iter
, &hbus
->children
) {
1058 hpdev
= container_of(iter
, struct hv_pci_dev
, list_entry
);
1059 for (i
= 0; i
< 6; i
++) {
1060 if (hpdev
->probed_bar
[i
] & PCI_BASE_ADDRESS_SPACE_IO
)
1061 dev_err(&hbus
->hdev
->device
,
1062 "There's an I/O BAR in this list!\n");
1064 if (hpdev
->probed_bar
[i
] != 0) {
1066 * A probed BAR has all the upper bits set that
1070 bar_val
= hpdev
->probed_bar
[i
];
1071 if (bar_val
& PCI_BASE_ADDRESS_MEM_TYPE_64
)
1073 ((u64
)hpdev
->probed_bar
[++i
] << 32);
1075 bar_val
|= 0xffffffff00000000ULL
;
1077 bar_size
= get_bar_size(bar_val
);
1079 if (bar_val
& PCI_BASE_ADDRESS_MEM_TYPE_64
)
1080 hbus
->high_mmio_space
+= bar_size
;
1082 hbus
->low_mmio_space
+= bar_size
;
1087 spin_unlock_irqrestore(&hbus
->device_list_lock
, flags
);
1092 * prepopulate_bars() - Fill in BARs with defaults
1093 * @hbus: Root PCI bus, as understood by this driver
1095 * The core PCI driver code seems much, much happier if the BARs
1096 * for a device have values upon first scan. So fill them in.
1097 * The algorithm below works down from large sizes to small,
1098 * attempting to pack the assignments optimally. The assumption,
1099 * enforced in other parts of the code, is that the beginning of
1100 * the memory-mapped I/O space will be aligned on the largest
1103 static void prepopulate_bars(struct hv_pcibus_device
*hbus
)
1105 resource_size_t high_size
= 0;
1106 resource_size_t low_size
= 0;
1107 resource_size_t high_base
= 0;
1108 resource_size_t low_base
= 0;
1109 resource_size_t bar_size
;
1110 struct hv_pci_dev
*hpdev
;
1111 struct list_head
*iter
;
1112 unsigned long flags
;
1118 if (hbus
->low_mmio_space
) {
1119 low_size
= 1ULL << (63 - __builtin_clzll(hbus
->low_mmio_space
));
1120 low_base
= hbus
->low_mmio_res
->start
;
1123 if (hbus
->high_mmio_space
) {
1125 (63 - __builtin_clzll(hbus
->high_mmio_space
));
1126 high_base
= hbus
->high_mmio_res
->start
;
1129 spin_lock_irqsave(&hbus
->device_list_lock
, flags
);
1131 /* Pick addresses for the BARs. */
1133 list_for_each(iter
, &hbus
->children
) {
1134 hpdev
= container_of(iter
, struct hv_pci_dev
,
1136 for (i
= 0; i
< 6; i
++) {
1137 bar_val
= hpdev
->probed_bar
[i
];
1140 high
= bar_val
& PCI_BASE_ADDRESS_MEM_TYPE_64
;
1143 ((u64
)hpdev
->probed_bar
[i
+ 1]
1146 bar_val
|= 0xffffffffULL
<< 32;
1148 bar_size
= get_bar_size(bar_val
);
1150 if (high_size
!= bar_size
) {
1154 _hv_pcifront_write_config(hpdev
,
1155 PCI_BASE_ADDRESS_0
+ (4 * i
),
1157 (u32
)(high_base
& 0xffffff00));
1159 _hv_pcifront_write_config(hpdev
,
1160 PCI_BASE_ADDRESS_0
+ (4 * i
),
1161 4, (u32
)(high_base
>> 32));
1162 high_base
+= bar_size
;
1164 if (low_size
!= bar_size
)
1166 _hv_pcifront_write_config(hpdev
,
1167 PCI_BASE_ADDRESS_0
+ (4 * i
),
1169 (u32
)(low_base
& 0xffffff00));
1170 low_base
+= bar_size
;
1173 if (high_size
<= 1 && low_size
<= 1) {
1174 /* Set the memory enable bit. */
1175 _hv_pcifront_read_config(hpdev
, PCI_COMMAND
, 2,
1177 command
|= PCI_COMMAND_MEMORY
;
1178 _hv_pcifront_write_config(hpdev
, PCI_COMMAND
, 2,
1186 } while (high_size
|| low_size
);
1188 spin_unlock_irqrestore(&hbus
->device_list_lock
, flags
);
1192 * create_root_hv_pci_bus() - Expose a new root PCI bus
1193 * @hbus: Root PCI bus, as understood by this driver
1195 * Return: 0 on success, -errno on failure
1197 static int create_root_hv_pci_bus(struct hv_pcibus_device
*hbus
)
1199 /* Register the device */
1200 hbus
->pci_bus
= pci_create_root_bus(&hbus
->hdev
->device
,
1201 0, /* bus number is always zero */
1204 &hbus
->resources_for_children
);
1208 hbus
->pci_bus
->msi
= &hbus
->msi_chip
;
1209 hbus
->pci_bus
->msi
->dev
= &hbus
->hdev
->device
;
1211 pci_scan_child_bus(hbus
->pci_bus
);
1212 pci_bus_assign_resources(hbus
->pci_bus
);
1213 pci_bus_add_devices(hbus
->pci_bus
);
1214 hbus
->state
= hv_pcibus_installed
;
1218 struct q_res_req_compl
{
1219 struct completion host_event
;
1220 struct hv_pci_dev
*hpdev
;
1224 * q_resource_requirements() - Query Resource Requirements
1225 * @context: The completion context.
1226 * @resp: The response that came from the host.
1227 * @resp_packet_size: The size in bytes of resp.
1229 * This function is invoked on completion of a Query Resource
1230 * Requirements packet.
1232 static void q_resource_requirements(void *context
, struct pci_response
*resp
,
1233 int resp_packet_size
)
1235 struct q_res_req_compl
*completion
= context
;
1236 struct pci_q_res_req_response
*q_res_req
=
1237 (struct pci_q_res_req_response
*)resp
;
1240 if (resp
->status
< 0) {
1241 dev_err(&completion
->hpdev
->hbus
->hdev
->device
,
1242 "query resource requirements failed: %x\n",
1245 for (i
= 0; i
< 6; i
++) {
1246 completion
->hpdev
->probed_bar
[i
] =
1247 q_res_req
->probed_bar
[i
];
1251 complete(&completion
->host_event
);
1254 static void get_pcichild(struct hv_pci_dev
*hpdev
,
1255 enum hv_pcidev_ref_reason reason
)
1257 atomic_inc(&hpdev
->refs
);
1260 static void put_pcichild(struct hv_pci_dev
*hpdev
,
1261 enum hv_pcidev_ref_reason reason
)
1263 if (atomic_dec_and_test(&hpdev
->refs
))
1268 * new_pcichild_device() - Create a new child device
1269 * @hbus: The internal struct tracking this root PCI bus.
1270 * @desc: The information supplied so far from the host
1273 * This function creates the tracking structure for a new child
1274 * device and kicks off the process of figuring out what it is.
1276 * Return: Pointer to the new tracking struct
1278 static struct hv_pci_dev
*new_pcichild_device(struct hv_pcibus_device
*hbus
,
1279 struct pci_function_description
*desc
)
1281 struct hv_pci_dev
*hpdev
;
1282 struct pci_child_message
*res_req
;
1283 struct q_res_req_compl comp_pkt
;
1285 struct pci_packet init_packet
;
1286 u8 buffer
[sizeof(struct pci_child_message
)];
1288 unsigned long flags
;
1291 hpdev
= kzalloc(sizeof(*hpdev
), GFP_ATOMIC
);
1297 memset(&pkt
, 0, sizeof(pkt
));
1298 init_completion(&comp_pkt
.host_event
);
1299 comp_pkt
.hpdev
= hpdev
;
1300 pkt
.init_packet
.compl_ctxt
= &comp_pkt
;
1301 pkt
.init_packet
.completion_func
= q_resource_requirements
;
1302 res_req
= (struct pci_child_message
*)&pkt
.init_packet
.message
;
1303 res_req
->message_type
.type
= PCI_QUERY_RESOURCE_REQUIREMENTS
;
1304 res_req
->wslot
.slot
= desc
->win_slot
.slot
;
1306 ret
= vmbus_sendpacket(hbus
->hdev
->channel
, res_req
,
1307 sizeof(struct pci_child_message
),
1308 (unsigned long)&pkt
.init_packet
,
1310 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
1314 wait_for_completion(&comp_pkt
.host_event
);
1316 hpdev
->desc
= *desc
;
1317 get_pcichild(hpdev
, hv_pcidev_ref_initial
);
1318 get_pcichild(hpdev
, hv_pcidev_ref_childlist
);
1319 spin_lock_irqsave(&hbus
->device_list_lock
, flags
);
1320 list_add_tail(&hpdev
->list_entry
, &hbus
->children
);
1321 spin_unlock_irqrestore(&hbus
->device_list_lock
, flags
);
1330 * get_pcichild_wslot() - Find device from slot
1331 * @hbus: Root PCI bus, as understood by this driver
1332 * @wslot: Location on the bus
1334 * This function looks up a PCI device and returns the internal
1335 * representation of it. It acquires a reference on it, so that
1336 * the device won't be deleted while somebody is using it. The
1337 * caller is responsible for calling put_pcichild() to release
1340 * Return: Internal representation of a PCI device
1342 static struct hv_pci_dev
*get_pcichild_wslot(struct hv_pcibus_device
*hbus
,
1345 unsigned long flags
;
1346 struct hv_pci_dev
*iter
, *hpdev
= NULL
;
1348 spin_lock_irqsave(&hbus
->device_list_lock
, flags
);
1349 list_for_each_entry(iter
, &hbus
->children
, list_entry
) {
1350 if (iter
->desc
.win_slot
.slot
== wslot
) {
1352 get_pcichild(hpdev
, hv_pcidev_ref_by_slot
);
1356 spin_unlock_irqrestore(&hbus
->device_list_lock
, flags
);
1362 * pci_devices_present_work() - Handle new list of child devices
1363 * @work: Work struct embedded in struct hv_dr_work
1365 * "Bus Relations" is the Windows term for "children of this
1366 * bus." The terminology is preserved here for people trying to
1367 * debug the interaction between Hyper-V and Linux. This
1368 * function is called when the parent partition reports a list
1369 * of functions that should be observed under this PCI Express
1372 * This function updates the list, and must tolerate being
1373 * called multiple times with the same information. The typical
1374 * number of child devices is one, with very atypical cases
1375 * involving three or four, so the algorithms used here can be
1376 * simple and inefficient.
1378 * It must also treat the omission of a previously observed device as
1379 * notification that the device no longer exists.
1381 * Note that this function is a work item, and it may not be
1382 * invoked in the order that it was queued. Back to back
1383 * updates of the list of present devices may involve queuing
1384 * multiple work items, and this one may run before ones that
1385 * were sent later. As such, this function only does something
1386 * if is the last one in the queue.
1388 static void pci_devices_present_work(struct work_struct
*work
)
1392 struct list_head
*iter
;
1393 struct pci_function_description
*new_desc
;
1394 struct hv_pci_dev
*hpdev
;
1395 struct hv_pcibus_device
*hbus
;
1396 struct list_head removed
;
1397 struct hv_dr_work
*dr_wrk
;
1398 struct hv_dr_state
*dr
= NULL
;
1399 unsigned long flags
;
1401 dr_wrk
= container_of(work
, struct hv_dr_work
, wrk
);
1405 INIT_LIST_HEAD(&removed
);
1407 if (down_interruptible(&hbus
->enum_sem
)) {
1412 /* Pull this off the queue and process it if it was the last one. */
1413 spin_lock_irqsave(&hbus
->device_list_lock
, flags
);
1414 while (!list_empty(&hbus
->dr_list
)) {
1415 dr
= list_first_entry(&hbus
->dr_list
, struct hv_dr_state
,
1417 list_del(&dr
->list_entry
);
1419 /* Throw this away if the list still has stuff in it. */
1420 if (!list_empty(&hbus
->dr_list
)) {
1425 spin_unlock_irqrestore(&hbus
->device_list_lock
, flags
);
1428 up(&hbus
->enum_sem
);
1433 /* First, mark all existing children as reported missing. */
1434 spin_lock_irqsave(&hbus
->device_list_lock
, flags
);
1435 list_for_each(iter
, &hbus
->children
) {
1436 hpdev
= container_of(iter
, struct hv_pci_dev
,
1438 hpdev
->reported_missing
= true;
1440 spin_unlock_irqrestore(&hbus
->device_list_lock
, flags
);
1442 /* Next, add back any reported devices. */
1443 for (child_no
= 0; child_no
< dr
->device_count
; child_no
++) {
1445 new_desc
= &dr
->func
[child_no
];
1447 spin_lock_irqsave(&hbus
->device_list_lock
, flags
);
1448 list_for_each(iter
, &hbus
->children
) {
1449 hpdev
= container_of(iter
, struct hv_pci_dev
,
1451 if ((hpdev
->desc
.win_slot
.slot
==
1452 new_desc
->win_slot
.slot
) &&
1453 (hpdev
->desc
.v_id
== new_desc
->v_id
) &&
1454 (hpdev
->desc
.d_id
== new_desc
->d_id
) &&
1455 (hpdev
->desc
.ser
== new_desc
->ser
)) {
1456 hpdev
->reported_missing
= false;
1460 spin_unlock_irqrestore(&hbus
->device_list_lock
, flags
);
1463 hpdev
= new_pcichild_device(hbus
, new_desc
);
1465 dev_err(&hbus
->hdev
->device
,
1466 "couldn't record a child device.\n");
1470 /* Move missing children to a list on the stack. */
1471 spin_lock_irqsave(&hbus
->device_list_lock
, flags
);
1474 list_for_each(iter
, &hbus
->children
) {
1475 hpdev
= container_of(iter
, struct hv_pci_dev
,
1477 if (hpdev
->reported_missing
) {
1479 put_pcichild(hpdev
, hv_pcidev_ref_childlist
);
1480 list_move_tail(&hpdev
->list_entry
, &removed
);
1485 spin_unlock_irqrestore(&hbus
->device_list_lock
, flags
);
1487 /* Delete everything that should no longer exist. */
1488 while (!list_empty(&removed
)) {
1489 hpdev
= list_first_entry(&removed
, struct hv_pci_dev
,
1491 list_del(&hpdev
->list_entry
);
1492 put_pcichild(hpdev
, hv_pcidev_ref_initial
);
1495 /* Tell the core to rescan bus because there may have been changes. */
1496 if (hbus
->state
== hv_pcibus_installed
) {
1497 pci_lock_rescan_remove();
1498 pci_scan_child_bus(hbus
->pci_bus
);
1499 pci_unlock_rescan_remove();
1501 survey_child_resources(hbus
);
1504 up(&hbus
->enum_sem
);
1510 * hv_pci_devices_present() - Handles list of new children
1511 * @hbus: Root PCI bus, as understood by this driver
1512 * @relations: Packet from host listing children
1514 * This function is invoked whenever a new list of devices for
1517 static void hv_pci_devices_present(struct hv_pcibus_device
*hbus
,
1518 struct pci_bus_relations
*relations
)
1520 struct hv_dr_state
*dr
;
1521 struct hv_dr_work
*dr_wrk
;
1522 unsigned long flags
;
1524 dr_wrk
= kzalloc(sizeof(*dr_wrk
), GFP_NOWAIT
);
1528 dr
= kzalloc(offsetof(struct hv_dr_state
, func
) +
1529 (sizeof(struct pci_function_description
) *
1530 (relations
->device_count
)), GFP_NOWAIT
);
1536 INIT_WORK(&dr_wrk
->wrk
, pci_devices_present_work
);
1538 dr
->device_count
= relations
->device_count
;
1539 if (dr
->device_count
!= 0) {
1540 memcpy(dr
->func
, relations
->func
,
1541 sizeof(struct pci_function_description
) *
1545 spin_lock_irqsave(&hbus
->device_list_lock
, flags
);
1546 list_add_tail(&dr
->list_entry
, &hbus
->dr_list
);
1547 spin_unlock_irqrestore(&hbus
->device_list_lock
, flags
);
1550 schedule_work(&dr_wrk
->wrk
);
1554 * hv_eject_device_work() - Asynchronously handles ejection
1555 * @work: Work struct embedded in internal device struct
1557 * This function handles ejecting a device. Windows will
1558 * attempt to gracefully eject a device, waiting 60 seconds to
1559 * hear back from the guest OS that this completed successfully.
1560 * If this timer expires, the device will be forcibly removed.
1562 static void hv_eject_device_work(struct work_struct
*work
)
1564 struct pci_eject_response
*ejct_pkt
;
1565 struct hv_pci_dev
*hpdev
;
1566 struct pci_dev
*pdev
;
1567 unsigned long flags
;
1570 struct pci_packet pkt
;
1571 u8 buffer
[sizeof(struct pci_eject_response
)];
1574 hpdev
= container_of(work
, struct hv_pci_dev
, wrk
);
1576 if (hpdev
->state
!= hv_pcichild_ejecting
) {
1577 put_pcichild(hpdev
, hv_pcidev_ref_pnp
);
1582 * Ejection can come before or after the PCI bus has been set up, so
1583 * attempt to find it and tear down the bus state, if it exists. This
1584 * must be done without constructs like pci_domain_nr(hbus->pci_bus)
1585 * because hbus->pci_bus may not exist yet.
1587 wslot
= wslot_to_devfn(hpdev
->desc
.win_slot
.slot
);
1588 pdev
= pci_get_domain_bus_and_slot(hpdev
->hbus
->sysdata
.domain
, 0,
1591 pci_stop_and_remove_bus_device(pdev
);
1595 spin_lock_irqsave(&hpdev
->hbus
->device_list_lock
, flags
);
1596 list_del(&hpdev
->list_entry
);
1597 spin_unlock_irqrestore(&hpdev
->hbus
->device_list_lock
, flags
);
1599 memset(&ctxt
, 0, sizeof(ctxt
));
1600 ejct_pkt
= (struct pci_eject_response
*)&ctxt
.pkt
.message
;
1601 ejct_pkt
->message_type
.type
= PCI_EJECTION_COMPLETE
;
1602 ejct_pkt
->wslot
.slot
= hpdev
->desc
.win_slot
.slot
;
1603 vmbus_sendpacket(hpdev
->hbus
->hdev
->channel
, ejct_pkt
,
1604 sizeof(*ejct_pkt
), (unsigned long)&ctxt
.pkt
,
1605 VM_PKT_DATA_INBAND
, 0);
1607 put_pcichild(hpdev
, hv_pcidev_ref_childlist
);
1608 put_pcichild(hpdev
, hv_pcidev_ref_pnp
);
1609 put_hvpcibus(hpdev
->hbus
);
1613 * hv_pci_eject_device() - Handles device ejection
1614 * @hpdev: Internal device tracking struct
1616 * This function is invoked when an ejection packet arrives. It
1617 * just schedules work so that we don't re-enter the packet
1618 * delivery code handling the ejection.
1620 static void hv_pci_eject_device(struct hv_pci_dev
*hpdev
)
1622 hpdev
->state
= hv_pcichild_ejecting
;
1623 get_pcichild(hpdev
, hv_pcidev_ref_pnp
);
1624 INIT_WORK(&hpdev
->wrk
, hv_eject_device_work
);
1625 get_hvpcibus(hpdev
->hbus
);
1626 schedule_work(&hpdev
->wrk
);
1630 * hv_pci_onchannelcallback() - Handles incoming packets
1631 * @context: Internal bus tracking struct
1633 * This function is invoked whenever the host sends a packet to
1634 * this channel (which is private to this root PCI bus).
1636 static void hv_pci_onchannelcallback(void *context
)
1638 const int packet_size
= 0x100;
1640 struct hv_pcibus_device
*hbus
= context
;
1643 struct vmpacket_descriptor
*desc
;
1644 unsigned char *buffer
;
1645 int bufferlen
= packet_size
;
1646 struct pci_packet
*comp_packet
;
1647 struct pci_response
*response
;
1648 struct pci_incoming_message
*new_message
;
1649 struct pci_bus_relations
*bus_rel
;
1650 struct pci_dev_incoming
*dev_message
;
1651 struct hv_pci_dev
*hpdev
;
1653 buffer
= kmalloc(bufferlen
, GFP_ATOMIC
);
1658 ret
= vmbus_recvpacket_raw(hbus
->hdev
->channel
, buffer
,
1659 bufferlen
, &bytes_recvd
, &req_id
);
1661 if (ret
== -ENOBUFS
) {
1663 /* Handle large packet */
1664 bufferlen
= bytes_recvd
;
1665 buffer
= kmalloc(bytes_recvd
, GFP_ATOMIC
);
1671 /* Zero length indicates there are no more packets. */
1672 if (ret
|| !bytes_recvd
)
1676 * All incoming packets must be at least as large as a
1679 if (bytes_recvd
<= sizeof(struct pci_response
))
1681 desc
= (struct vmpacket_descriptor
*)buffer
;
1683 switch (desc
->type
) {
1687 * The host is trusted, and thus it's safe to interpret
1688 * this transaction ID as a pointer.
1690 comp_packet
= (struct pci_packet
*)req_id
;
1691 response
= (struct pci_response
*)buffer
;
1692 comp_packet
->completion_func(comp_packet
->compl_ctxt
,
1697 case VM_PKT_DATA_INBAND
:
1699 new_message
= (struct pci_incoming_message
*)buffer
;
1700 switch (new_message
->message_type
.type
) {
1701 case PCI_BUS_RELATIONS
:
1703 bus_rel
= (struct pci_bus_relations
*)buffer
;
1705 offsetof(struct pci_bus_relations
, func
) +
1706 (sizeof(struct pci_function_description
) *
1707 (bus_rel
->device_count
))) {
1708 dev_err(&hbus
->hdev
->device
,
1709 "bus relations too small\n");
1713 hv_pci_devices_present(hbus
, bus_rel
);
1718 dev_message
= (struct pci_dev_incoming
*)buffer
;
1719 hpdev
= get_pcichild_wslot(hbus
,
1720 dev_message
->wslot
.slot
);
1722 hv_pci_eject_device(hpdev
);
1724 hv_pcidev_ref_by_slot
);
1729 dev_warn(&hbus
->hdev
->device
,
1730 "Unimplemented protocol message %x\n",
1731 new_message
->message_type
.type
);
1737 dev_err(&hbus
->hdev
->device
,
1738 "unhandled packet type %d, tid %llx len %d\n",
1739 desc
->type
, req_id
, bytes_recvd
);
1748 * hv_pci_protocol_negotiation() - Set up protocol
1749 * @hdev: VMBus's tracking struct for this root PCI bus
1751 * This driver is intended to support running on Windows 10
1752 * (server) and later versions. It will not run on earlier
1753 * versions, as they assume that many of the operations which
1754 * Linux needs accomplished with a spinlock held were done via
1755 * asynchronous messaging via VMBus. Windows 10 increases the
1756 * surface area of PCI emulation so that these actions can take
1757 * place by suspending a virtual processor for their duration.
1759 * This function negotiates the channel protocol version,
1760 * failing if the host doesn't support the necessary protocol
1763 static int hv_pci_protocol_negotiation(struct hv_device
*hdev
)
1765 struct pci_version_request
*version_req
;
1766 struct hv_pci_compl comp_pkt
;
1767 struct pci_packet
*pkt
;
1771 * Initiate the handshake with the host and negotiate
1772 * a version that the host can support. We start with the
1773 * highest version number and go down if the host cannot
1776 pkt
= kzalloc(sizeof(*pkt
) + sizeof(*version_req
), GFP_KERNEL
);
1780 init_completion(&comp_pkt
.host_event
);
1781 pkt
->completion_func
= hv_pci_generic_compl
;
1782 pkt
->compl_ctxt
= &comp_pkt
;
1783 version_req
= (struct pci_version_request
*)&pkt
->message
;
1784 version_req
->message_type
.type
= PCI_QUERY_PROTOCOL_VERSION
;
1785 version_req
->protocol_version
= PCI_PROTOCOL_VERSION_CURRENT
;
1787 ret
= vmbus_sendpacket(hdev
->channel
, version_req
,
1788 sizeof(struct pci_version_request
),
1789 (unsigned long)pkt
, VM_PKT_DATA_INBAND
,
1790 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
1794 wait_for_completion(&comp_pkt
.host_event
);
1796 if (comp_pkt
.completion_status
< 0) {
1797 dev_err(&hdev
->device
,
1798 "PCI Pass-through VSP failed version request %x\n",
1799 comp_pkt
.completion_status
);
1812 * hv_pci_free_bridge_windows() - Release memory regions for the
1814 * @hbus: Root PCI bus, as understood by this driver
1816 static void hv_pci_free_bridge_windows(struct hv_pcibus_device
*hbus
)
1819 * Set the resources back to the way they looked when they
1820 * were allocated by setting IORESOURCE_BUSY again.
1823 if (hbus
->low_mmio_space
&& hbus
->low_mmio_res
) {
1824 hbus
->low_mmio_res
->flags
|= IORESOURCE_BUSY
;
1825 vmbus_free_mmio(hbus
->low_mmio_res
->start
,
1826 resource_size(hbus
->low_mmio_res
));
1829 if (hbus
->high_mmio_space
&& hbus
->high_mmio_res
) {
1830 hbus
->high_mmio_res
->flags
|= IORESOURCE_BUSY
;
1831 vmbus_free_mmio(hbus
->high_mmio_res
->start
,
1832 resource_size(hbus
->high_mmio_res
));
1837 * hv_pci_allocate_bridge_windows() - Allocate memory regions
1839 * @hbus: Root PCI bus, as understood by this driver
1841 * This function calls vmbus_allocate_mmio(), which is itself a
1842 * bit of a compromise. Ideally, we might change the pnp layer
1843 * in the kernel such that it comprehends either PCI devices
1844 * which are "grandchildren of ACPI," with some intermediate bus
1845 * node (in this case, VMBus) or change it such that it
1846 * understands VMBus. The pnp layer, however, has been declared
1847 * deprecated, and not subject to change.
1849 * The workaround, implemented here, is to ask VMBus to allocate
1850 * MMIO space for this bus. VMBus itself knows which ranges are
1851 * appropriate by looking at its own ACPI objects. Then, after
1852 * these ranges are claimed, they're modified to look like they
1853 * would have looked if the ACPI and pnp code had allocated
1854 * bridge windows. These descriptors have to exist in this form
1855 * in order to satisfy the code which will get invoked when the
1856 * endpoint PCI function driver calls request_mem_region() or
1857 * request_mem_region_exclusive().
1859 * Return: 0 on success, -errno on failure
1861 static int hv_pci_allocate_bridge_windows(struct hv_pcibus_device
*hbus
)
1863 resource_size_t align
;
1866 if (hbus
->low_mmio_space
) {
1867 align
= 1ULL << (63 - __builtin_clzll(hbus
->low_mmio_space
));
1868 ret
= vmbus_allocate_mmio(&hbus
->low_mmio_res
, hbus
->hdev
, 0,
1869 (u64
)(u32
)0xffffffff,
1870 hbus
->low_mmio_space
,
1873 dev_err(&hbus
->hdev
->device
,
1874 "Need %#llx of low MMIO space. Consider reconfiguring the VM.\n",
1875 hbus
->low_mmio_space
);
1879 /* Modify this resource to become a bridge window. */
1880 hbus
->low_mmio_res
->flags
|= IORESOURCE_WINDOW
;
1881 hbus
->low_mmio_res
->flags
&= ~IORESOURCE_BUSY
;
1882 pci_add_resource(&hbus
->resources_for_children
,
1883 hbus
->low_mmio_res
);
1886 if (hbus
->high_mmio_space
) {
1887 align
= 1ULL << (63 - __builtin_clzll(hbus
->high_mmio_space
));
1888 ret
= vmbus_allocate_mmio(&hbus
->high_mmio_res
, hbus
->hdev
,
1890 hbus
->high_mmio_space
, align
,
1893 dev_err(&hbus
->hdev
->device
,
1894 "Need %#llx of high MMIO space. Consider reconfiguring the VM.\n",
1895 hbus
->high_mmio_space
);
1896 goto release_low_mmio
;
1899 /* Modify this resource to become a bridge window. */
1900 hbus
->high_mmio_res
->flags
|= IORESOURCE_WINDOW
;
1901 hbus
->high_mmio_res
->flags
&= ~IORESOURCE_BUSY
;
1902 pci_add_resource(&hbus
->resources_for_children
,
1903 hbus
->high_mmio_res
);
1909 if (hbus
->low_mmio_res
) {
1910 vmbus_free_mmio(hbus
->low_mmio_res
->start
,
1911 resource_size(hbus
->low_mmio_res
));
1918 * hv_allocate_config_window() - Find MMIO space for PCI Config
1919 * @hbus: Root PCI bus, as understood by this driver
1921 * This function claims memory-mapped I/O space for accessing
1922 * configuration space for the functions on this bus.
1924 * Return: 0 on success, -errno on failure
1926 static int hv_allocate_config_window(struct hv_pcibus_device
*hbus
)
1931 * Set up a region of MMIO space to use for accessing configuration
1934 ret
= vmbus_allocate_mmio(&hbus
->mem_config
, hbus
->hdev
, 0, -1,
1935 PCI_CONFIG_MMIO_LENGTH
, 0x1000, false);
1940 * vmbus_allocate_mmio() gets used for allocating both device endpoint
1941 * resource claims (those which cannot be overlapped) and the ranges
1942 * which are valid for the children of this bus, which are intended
1943 * to be overlapped by those children. Set the flag on this claim
1944 * meaning that this region can't be overlapped.
1947 hbus
->mem_config
->flags
|= IORESOURCE_BUSY
;
1952 static void hv_free_config_window(struct hv_pcibus_device
*hbus
)
1954 vmbus_free_mmio(hbus
->mem_config
->start
, PCI_CONFIG_MMIO_LENGTH
);
1958 * hv_pci_enter_d0() - Bring the "bus" into the D0 power state
1959 * @hdev: VMBus's tracking struct for this root PCI bus
1961 * Return: 0 on success, -errno on failure
1963 static int hv_pci_enter_d0(struct hv_device
*hdev
)
1965 struct hv_pcibus_device
*hbus
= hv_get_drvdata(hdev
);
1966 struct pci_bus_d0_entry
*d0_entry
;
1967 struct hv_pci_compl comp_pkt
;
1968 struct pci_packet
*pkt
;
1972 * Tell the host that the bus is ready to use, and moved into the
1973 * powered-on state. This includes telling the host which region
1974 * of memory-mapped I/O space has been chosen for configuration space
1977 pkt
= kzalloc(sizeof(*pkt
) + sizeof(*d0_entry
), GFP_KERNEL
);
1981 init_completion(&comp_pkt
.host_event
);
1982 pkt
->completion_func
= hv_pci_generic_compl
;
1983 pkt
->compl_ctxt
= &comp_pkt
;
1984 d0_entry
= (struct pci_bus_d0_entry
*)&pkt
->message
;
1985 d0_entry
->message_type
.type
= PCI_BUS_D0ENTRY
;
1986 d0_entry
->mmio_base
= hbus
->mem_config
->start
;
1988 ret
= vmbus_sendpacket(hdev
->channel
, d0_entry
, sizeof(*d0_entry
),
1989 (unsigned long)pkt
, VM_PKT_DATA_INBAND
,
1990 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
1994 wait_for_completion(&comp_pkt
.host_event
);
1996 if (comp_pkt
.completion_status
< 0) {
1997 dev_err(&hdev
->device
,
1998 "PCI Pass-through VSP failed D0 Entry with status %x\n",
1999 comp_pkt
.completion_status
);
2012 * hv_pci_query_relations() - Ask host to send list of child
2014 * @hdev: VMBus's tracking struct for this root PCI bus
2016 * Return: 0 on success, -errno on failure
2018 static int hv_pci_query_relations(struct hv_device
*hdev
)
2020 struct hv_pcibus_device
*hbus
= hv_get_drvdata(hdev
);
2021 struct pci_message message
;
2022 struct completion comp
;
2025 /* Ask the host to send along the list of child devices */
2026 init_completion(&comp
);
2027 if (cmpxchg(&hbus
->survey_event
, NULL
, &comp
))
2030 memset(&message
, 0, sizeof(message
));
2031 message
.type
= PCI_QUERY_BUS_RELATIONS
;
2033 ret
= vmbus_sendpacket(hdev
->channel
, &message
, sizeof(message
),
2034 0, VM_PKT_DATA_INBAND
, 0);
2038 wait_for_completion(&comp
);
2043 * hv_send_resources_allocated() - Report local resource choices
2044 * @hdev: VMBus's tracking struct for this root PCI bus
2046 * The host OS is expecting to be sent a request as a message
2047 * which contains all the resources that the device will use.
2048 * The response contains those same resources, "translated"
2049 * which is to say, the values which should be used by the
2050 * hardware, when it delivers an interrupt. (MMIO resources are
2051 * used in local terms.) This is nice for Windows, and lines up
2052 * with the FDO/PDO split, which doesn't exist in Linux. Linux
2053 * is deeply expecting to scan an emulated PCI configuration
2054 * space. So this message is sent here only to drive the state
2055 * machine on the host forward.
2057 * Return: 0 on success, -errno on failure
2059 static int hv_send_resources_allocated(struct hv_device
*hdev
)
2061 struct hv_pcibus_device
*hbus
= hv_get_drvdata(hdev
);
2062 struct pci_resources_assigned
*res_assigned
;
2063 struct hv_pci_compl comp_pkt
;
2064 struct hv_pci_dev
*hpdev
;
2065 struct pci_packet
*pkt
;
2069 pkt
= kmalloc(sizeof(*pkt
) + sizeof(*res_assigned
), GFP_KERNEL
);
2075 for (wslot
= 0; wslot
< 256; wslot
++) {
2076 hpdev
= get_pcichild_wslot(hbus
, wslot
);
2080 memset(pkt
, 0, sizeof(*pkt
) + sizeof(*res_assigned
));
2081 init_completion(&comp_pkt
.host_event
);
2082 pkt
->completion_func
= hv_pci_generic_compl
;
2083 pkt
->compl_ctxt
= &comp_pkt
;
2084 res_assigned
= (struct pci_resources_assigned
*)&pkt
->message
;
2085 res_assigned
->message_type
.type
= PCI_RESOURCES_ASSIGNED
;
2086 res_assigned
->wslot
.slot
= hpdev
->desc
.win_slot
.slot
;
2088 put_pcichild(hpdev
, hv_pcidev_ref_by_slot
);
2090 ret
= vmbus_sendpacket(
2091 hdev
->channel
, &pkt
->message
,
2092 sizeof(*res_assigned
),
2095 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
2099 wait_for_completion(&comp_pkt
.host_event
);
2101 if (comp_pkt
.completion_status
< 0) {
2103 dev_err(&hdev
->device
,
2104 "resource allocated returned 0x%x",
2105 comp_pkt
.completion_status
);
2115 * hv_send_resources_released() - Report local resources
2117 * @hdev: VMBus's tracking struct for this root PCI bus
2119 * Return: 0 on success, -errno on failure
2121 static int hv_send_resources_released(struct hv_device
*hdev
)
2123 struct hv_pcibus_device
*hbus
= hv_get_drvdata(hdev
);
2124 struct pci_child_message pkt
;
2125 struct hv_pci_dev
*hpdev
;
2129 for (wslot
= 0; wslot
< 256; wslot
++) {
2130 hpdev
= get_pcichild_wslot(hbus
, wslot
);
2134 memset(&pkt
, 0, sizeof(pkt
));
2135 pkt
.message_type
.type
= PCI_RESOURCES_RELEASED
;
2136 pkt
.wslot
.slot
= hpdev
->desc
.win_slot
.slot
;
2138 put_pcichild(hpdev
, hv_pcidev_ref_by_slot
);
2140 ret
= vmbus_sendpacket(hdev
->channel
, &pkt
, sizeof(pkt
), 0,
2141 VM_PKT_DATA_INBAND
, 0);
2149 static void get_hvpcibus(struct hv_pcibus_device
*hbus
)
2151 atomic_inc(&hbus
->remove_lock
);
2154 static void put_hvpcibus(struct hv_pcibus_device
*hbus
)
2156 if (atomic_dec_and_test(&hbus
->remove_lock
))
2157 complete(&hbus
->remove_event
);
2161 * hv_pci_probe() - New VMBus channel probe, for a root PCI bus
2162 * @hdev: VMBus's tracking struct for this root PCI bus
2163 * @dev_id: Identifies the device itself
2165 * Return: 0 on success, -errno on failure
2167 static int hv_pci_probe(struct hv_device
*hdev
,
2168 const struct hv_vmbus_device_id
*dev_id
)
2170 struct hv_pcibus_device
*hbus
;
2173 hbus
= kzalloc(sizeof(*hbus
), GFP_KERNEL
);
2178 * The PCI bus "domain" is what is called "segment" in ACPI and
2179 * other specs. Pull it from the instance ID, to get something
2180 * unique. Bytes 8 and 9 are what is used in Windows guests, so
2181 * do the same thing for consistency. Note that, since this code
2182 * only runs in a Hyper-V VM, Hyper-V can (and does) guarantee
2183 * that (1) the only domain in use for something that looks like
2184 * a physical PCI bus (which is actually emulated by the
2185 * hypervisor) is domain 0 and (2) there will be no overlap
2186 * between domains derived from these instance IDs in the same
2189 hbus
->sysdata
.domain
= hdev
->dev_instance
.b
[9] |
2190 hdev
->dev_instance
.b
[8] << 8;
2193 atomic_inc(&hbus
->remove_lock
);
2194 INIT_LIST_HEAD(&hbus
->children
);
2195 INIT_LIST_HEAD(&hbus
->dr_list
);
2196 INIT_LIST_HEAD(&hbus
->resources_for_children
);
2197 spin_lock_init(&hbus
->config_lock
);
2198 spin_lock_init(&hbus
->device_list_lock
);
2199 spin_lock_init(&hbus
->retarget_msi_interrupt_lock
);
2200 sema_init(&hbus
->enum_sem
, 1);
2201 init_completion(&hbus
->remove_event
);
2203 ret
= vmbus_open(hdev
->channel
, pci_ring_size
, pci_ring_size
, NULL
, 0,
2204 hv_pci_onchannelcallback
, hbus
);
2208 hv_set_drvdata(hdev
, hbus
);
2210 ret
= hv_pci_protocol_negotiation(hdev
);
2214 ret
= hv_allocate_config_window(hbus
);
2218 hbus
->cfg_addr
= ioremap(hbus
->mem_config
->start
,
2219 PCI_CONFIG_MMIO_LENGTH
);
2220 if (!hbus
->cfg_addr
) {
2221 dev_err(&hdev
->device
,
2222 "Unable to map a virtual address for config space\n");
2227 hbus
->sysdata
.fwnode
= irq_domain_alloc_fwnode(hbus
);
2228 if (!hbus
->sysdata
.fwnode
) {
2233 ret
= hv_pcie_init_irq_domain(hbus
);
2237 ret
= hv_pci_query_relations(hdev
);
2239 goto free_irq_domain
;
2241 ret
= hv_pci_enter_d0(hdev
);
2243 goto free_irq_domain
;
2245 ret
= hv_pci_allocate_bridge_windows(hbus
);
2247 goto free_irq_domain
;
2249 ret
= hv_send_resources_allocated(hdev
);
2253 prepopulate_bars(hbus
);
2255 hbus
->state
= hv_pcibus_probed
;
2257 ret
= create_root_hv_pci_bus(hbus
);
2264 hv_pci_free_bridge_windows(hbus
);
2266 irq_domain_remove(hbus
->irq_domain
);
2268 irq_domain_free_fwnode(hbus
->sysdata
.fwnode
);
2270 iounmap(hbus
->cfg_addr
);
2272 hv_free_config_window(hbus
);
2274 vmbus_close(hdev
->channel
);
2280 static void hv_pci_bus_exit(struct hv_device
*hdev
)
2282 struct hv_pcibus_device
*hbus
= hv_get_drvdata(hdev
);
2284 struct pci_packet teardown_packet
;
2285 u8 buffer
[sizeof(struct pci_message
)];
2287 struct pci_bus_relations relations
;
2288 struct hv_pci_compl comp_pkt
;
2292 * After the host sends the RESCIND_CHANNEL message, it doesn't
2293 * access the per-channel ringbuffer any longer.
2295 if (hdev
->channel
->rescind
)
2298 /* Delete any children which might still exist. */
2299 memset(&relations
, 0, sizeof(relations
));
2300 hv_pci_devices_present(hbus
, &relations
);
2302 ret
= hv_send_resources_released(hdev
);
2304 dev_err(&hdev
->device
,
2305 "Couldn't send resources released packet(s)\n");
2307 memset(&pkt
.teardown_packet
, 0, sizeof(pkt
.teardown_packet
));
2308 init_completion(&comp_pkt
.host_event
);
2309 pkt
.teardown_packet
.completion_func
= hv_pci_generic_compl
;
2310 pkt
.teardown_packet
.compl_ctxt
= &comp_pkt
;
2311 pkt
.teardown_packet
.message
[0].type
= PCI_BUS_D0EXIT
;
2313 ret
= vmbus_sendpacket(hdev
->channel
, &pkt
.teardown_packet
.message
,
2314 sizeof(struct pci_message
),
2315 (unsigned long)&pkt
.teardown_packet
,
2317 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
2319 wait_for_completion_timeout(&comp_pkt
.host_event
, 10 * HZ
);
2323 * hv_pci_remove() - Remove routine for this VMBus channel
2324 * @hdev: VMBus's tracking struct for this root PCI bus
2326 * Return: 0 on success, -errno on failure
2328 static int hv_pci_remove(struct hv_device
*hdev
)
2330 struct hv_pcibus_device
*hbus
;
2332 hbus
= hv_get_drvdata(hdev
);
2333 if (hbus
->state
== hv_pcibus_installed
) {
2334 /* Remove the bus from PCI's point of view. */
2335 pci_lock_rescan_remove();
2336 pci_stop_root_bus(hbus
->pci_bus
);
2337 pci_remove_root_bus(hbus
->pci_bus
);
2338 pci_unlock_rescan_remove();
2341 hv_pci_bus_exit(hdev
);
2343 vmbus_close(hdev
->channel
);
2345 iounmap(hbus
->cfg_addr
);
2346 hv_free_config_window(hbus
);
2347 pci_free_resource_list(&hbus
->resources_for_children
);
2348 hv_pci_free_bridge_windows(hbus
);
2349 irq_domain_remove(hbus
->irq_domain
);
2350 irq_domain_free_fwnode(hbus
->sysdata
.fwnode
);
2352 wait_for_completion(&hbus
->remove_event
);
2357 static const struct hv_vmbus_device_id hv_pci_id_table
[] = {
2358 /* PCI Pass-through Class ID */
2359 /* 44C4F61D-4444-4400-9D52-802E27EDE19F */
2364 MODULE_DEVICE_TABLE(vmbus
, hv_pci_id_table
);
2366 static struct hv_driver hv_pci_drv
= {
2368 .id_table
= hv_pci_id_table
,
2369 .probe
= hv_pci_probe
,
2370 .remove
= hv_pci_remove
,
2373 static void __exit
exit_hv_pci_drv(void)
2375 vmbus_driver_unregister(&hv_pci_drv
);
2378 static int __init
init_hv_pci_drv(void)
2380 return vmbus_driver_register(&hv_pci_drv
);
2383 module_init(init_hv_pci_drv
);
2384 module_exit(exit_hv_pci_drv
);
2386 MODULE_DESCRIPTION("Hyper-V PCI");
2387 MODULE_LICENSE("GPL v2");