Xen PCI Passthrough
v9 cover Xen PCI Passthrough
Hi all,
This patch series introduces the PCI passthrough for Xen.
Please review patches number 1, 2, 3, 4 and 7.
First, we have HostPCIDevice that help to access one PCI device of the host.
Last part, but not least, the PCI passthrough device himself. Cut in 3 parts (or file), there is one to take care of the initialisation of a passthrough device. The second one handle everything about the config address space, there are specifics functions for every config register. The third one is to handle MSI.
There is a patch series on xen-devel (applied to xen-unstable) that add the support of setting a PCI passthrough device through QMP from libxl (xen tool stack). It is just a call to device_add, with the driver parametter hostaddr="0000:07:00.1".
Change v8-v9:
- rename PCI_DEVICE_ID_INTEL_82599_VF to PCI_DEVICE_ID_INTEL_82599_SFP_VF to be consistant with Linux.
- remove the patch about checking bar overlaps, the function is now in xen_pci_passthrough.c and uses pci_for_each_device.
- Introduce an opaque argument to the function pci_for_each_device.
- Fix the usage of memory listener: declare a stub function for every callback in the MemoryListener.
Change v7-v8:
- rework of the memory mapping of BARs. We now use a memory_listener to update a xen memory_mapping when a memory_region is updated.
- address few comment from Michael in the pci_check_overlap function.
- fix the handling of the ROM slot.
Change v6-v7:
- few fix and rebased on master
- remove of the power management capability, keep the minimum like if it is always desactivated.
- new patch: port of patch from the qemu-xen fork.
Change v5-v6:
- msitraslate code have been removed.
- code for the power management capability is removed, but will be re-added for the next version of the patch series as a separate patch.
- new patch to remove a check in pci_parse_devaddr.
- use pci_default_config_write, so no more hack to handle the BAR mapping in QEMU.
- improve the code in general (a bit more comprehensible).
- update to QOM.
Change v4-v5:
- return -errno if there is an error in host_pci_get_*
- rename internal function get_value to get_hex_value (and return the same error value has get_resource)
Change v3-v4:
- host_pci_get_* can now return an error, and take an extra parameter, a pointer to store the wanted value.
- The memory_region for the PCI BAR are handled "manualy" because calling pci_default_write_config was not possible, because the XenPT handle the PCIIORegion it self. This make possible to do a device_remove.
- Introduction of PT_ERR and PT_WARN macro to print debug and error messages. Also, these macro as well as PT_LOG will always print the short BDF of the device in the guest point of view.
- PT_ERR is print by default (for all error messages).
- Some debug/error message have been improve and should be a bit more useful.
- hw_error have been removed from the code, and have been replaced by either a call to qemu_system_shudown_request() (that lead to a domain destroy) or a failed in the initialisation of the device.
- Now, every patchs should compile with no error.
Change v2-v3;
- in host-pci-device.c:
- Return more usefull error code in get_ressource().
- Use macro in host_pci_find_ext_cap_offset instead of raw number. But I still not sure if PCI_MAX_EXT_CAP is right, it's result is 480 like it was before, so it's maybe ok.
- All use of MSI stuff in two first pci passthrough patch have been removed and move to the last patch.
Change v1-v2:
- fix style issue (checkpatch.pl)
- set the original authors, add some missing copyright headers
- HostPCIDevice:
- introduce HostPCIIORegions (with base_addr, size, flags)
- save all flags from ./resource and store it in a separate field.
- fix endianess on write
- new host_pci_dev_put function
- use pci.c like interface host_pci_get/set_byte/word/long (instead of host_pci_read/write_)
- compile HostPCIDevice only on linux (as well as xen_pci_passthrough)
- introduce apic-msidef.h file.
- no more run_one_timer, if a pci device is in the middle of a power transition, just "return an error" in config read/write
- use a global var mapped_machine_irq (local to xen_pci_passthrough.c)
- add msitranslate and power-mgmt ad qdev property
Allen Kay (2):
Introduce Xen PCI Passthrough, qdevice (1/3) Introduce Xen PCI Passthrough, PCI config space helpers (2/3)
Anthony PERARD (5):
pci_ids: Add INTEL_82599_VF id.
configure: Introduce --enable-xen-pci-passthrough. Introduce HostPCIDevice to access a pci device on the host. pci.c: Add opaque argument to pci_for_each_device. Introduce apic-msidef.h
Jiang Yunhong (1):
Introduce Xen PCI Passthrough, MSI (3/3)
Makefile.target | 6 + configure | 25 + hw/apic-msidef.h | 30 + hw/apic.c | 11 +- hw/host-pci-device.c | 278 +++++ hw/host-pci-device.h | 75 ++ hw/pci.c | 11 +- hw/pci.h | 4 +- hw/pci_ids.h | 1 + hw/xen_common.h | 3 + hw/xen_pci_passthrough.c | 847 +++++++++++++++ hw/xen_pci_passthrough.h | 303 ++++++ hw/xen_pci_passthrough_config_init.c | 1866 ++++++++++++++++++++++++++++++++++ hw/xen_pci_passthrough_msi.c | 615 +++++++++++ hw/xen_platform.c | 8 +- xen-all.c | 12 + 16 files changed, 4076 insertions(+), 19 deletions(-) create mode 100644 hw/apic-msidef.h create mode 100644 hw/host-pci-device.c create mode 100644 hw/host-pci-device.h create mode 100644 hw/xen_pci_passthrough.c create mode 100644 hw/xen_pci_passthrough.h create mode 100644 hw/xen_pci_passthrough_config_init.c create mode 100644 hw/xen_pci_passthrough_msi.c
Signature · 1 line
Anthony PERARD
PATCH v9 1/8 pci_ids: Add INTEL_82599_SFP_VF id.
--- hw/pci_ids.h | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
hw/pci_ids.h +1 -0
@@ -118,6 +118,7 @@#define PCI_DEVICE_ID_INTEL_82801I_UHCI6 0x2939#define PCI_DEVICE_ID_INTEL_82801I_EHCI1 0x293a#define PCI_DEVICE_ID_INTEL_82801I_EHCI2 0x293c +#define PCI_DEVICE_ID_INTEL_82599_SFP_VF 0x10ed#define PCI_VENDOR_ID_XEN 0x5853#define PCI_DEVICE_ID_XEN_PLATFORM 0x0001
Signature · 1 line
Anthony PERARD
PATCH v9 2/8 configure: Introduce --enable-xen-pci-passthrough.
In reply to Anthony PERARD--- configure | 25 +++++++++++++++++++++++++ 1 files changed, 25 insertions(+), 0 deletions(-)
configure +25 -0
@@ -136,6 +136,7 @@ vnc_png="" vnc_thread="no" xen="" xen_ctrl_version="" +xen_pci_passthrough="" linux_aio="" cap_ng="" attr=""
@@ -682,6 +683,10 @@ for opt do ;; --enable-xen) xen="yes" ;; + --disable-xen-pci-passthrough) xen_pci_passthrough="no" + ;; + --enable-xen-pci-passthrough) xen_pci_passthrough="yes" + ;; --disable-brlapi) brlapi="no" ;; --enable-brlapi) brlapi="yes"
@@ -1034,6 +1039,8 @@ echo " (affects only QEMU, not qemu-img)" echo " --enable-mixemu enable mixer emulation" echo " --disable-xen disable xen backend driver support" echo " --enable-xen enable xen backend driver support" +echo " --disable-xen-pci-passthrough" +echo " --enable-xen-pci-passthrough" echo " --disable-brlapi disable BrlAPI" echo " --enable-brlapi enable BrlAPI" echo " --disable-vnc-tls disable TLS encryption for VNC server"
@@ -1478,6 +1485,21 @@ EOF fi fi +if test "$xen_pci_passthrough" != "no"; then + if test "$xen" = "yes" && test "$linux" = "yes"; then + xen_pci_passthrough=yes + else + if test "$xen_pci_passthrough" = "yes"; then + echo "ERROR" + echo "ERROR: User requested feature Xen PCI Passthrough" + echo "ERROR: but this feature require /sys from Linux" + echo "ERROR" + exit 1; + fi + xen_pci_passthrough=no + fi +fi + ########################################## # pkg-config probe
@@ -3635,6 +3657,9 @@ case "$target_arch2" in if test "$xen" = "yes" -a "$target_softmmu" = "yes" ; then target_phys_bits=64 echo "CONFIG_XEN=y" >> $config_target_mak + if test "$xen_pci_passthrough" = yes; then + echo "CONFIG_XEN_PCI_PASSTHROUGH=y" >> "$config_target_mak" + fi else echo "CONFIG_NO_XEN=y" >> $config_target_mak fi
Signature · 1 line
Anthony PERARD
PATCH v9 3/8 Introduce HostPCIDevice to access a pci device on the host.
In reply to Anthony PERARD--- Makefile.target | 3 + hw/host-pci-device.c | 278 ++++++++++++++++++++++++++++++++++++++++++++++++++ hw/host-pci-device.h | 75 ++++++++++++++ 3 files changed, 356 insertions(+), 0 deletions(-) create mode 100644 hw/host-pci-device.c create mode 100644 hw/host-pci-device.h
Makefile.target +3 -0
@@ -232,6 +232,9 @@ obj-$(CONFIG_NO_XEN) += xen-stub.o obj-i386-$(CONFIG_XEN) += xen_platform.o +# Xen PCI Passthrough +obj-i386-$(CONFIG_XEN_PCI_PASSTHROUGH) += host-pci-device.o + # Inter-VM PCI shared memory CONFIG_IVSHMEM = ifeq ($(CONFIG_KVM), y)
hw/host-pci-device.c new file +278 -0
278 lines not on this page · raw message
hw/host-pci-device.h new file +75 -0
75 lines not on this page · raw message
Signature · 1 line
Anthony PERARD
PATCH v9 4/8 pci.c: Add opaque argument to pci_for_each_device.
In reply to Anthony PERARD--- hw/pci.c | 11 +++++++---- hw/pci.h | 4 +++- hw/xen_platform.c | 8 ++++---- 3 files changed, 14 insertions(+), 9 deletions(-)
hw/pci.c +7 -4
@@ -1123,7 +1123,9 @@ static const pci_class_desc pci_class_descriptions[] = };static void pci_for_each_device_under_bus (PCIBus *bus, -void (*fn)(PCIBus *b,PCIDevice *d)) +void (*fn)(PCIBus *b,PCIDevice *d, +void *opaque), +void *opaque) {PCIDevice *d;int devfn;
@@ -1131,18 +1133,19 @@ static void pci_for_each_device_under_bus(PCIBus *bus,for (devfn =0 ; devfn <ARRAY_SIZE (bus->devices); devfn++) { d = bus->devices[devfn];if (d) { -fn (bus, d); +fn (bus, d, opaque); } } }void pci_for_each_device (PCIBus *bus,int bus_num, -void (*fn)(PCIBus *b,PCIDevice *d)) +void (*fn)(PCIBus *b,PCIDevice *d,void *opaque), +void *opaque) { bus =pci_find_bus (bus, bus_num);if (bus) { -pci_for_each_device_under_bus (bus, fn); +pci_for_each_device_under_bus (bus, fn, opaque); } }
hw/pci.h +3 -1
@@ -296,7 +296,9 @@ PCIDevice *pci_nic_init(NICInfo *nd, const char *default_model,PCIDevice *pci_nic_init_nofail (NICInfo *nd,const char *default_model,const char *default_devaddr);int pci_bus_num (PCIBus *s); -void pci_for_each_device (PCIBus *bus,int bus_num,void (*fn)(PCIBus *bus,PCIDevice *d)); +void pci_for_each_device (PCIBus *bus,int bus_num, +void (*fn)(PCIBus *bus,PCIDevice *d,void *opaque), +void *opaque);PCIBus *pci_find_root_bus (int domain);int pci_find_domain (const PCIBus *bus);PCIBus *pci_find_bus (PCIBus *bus,int bus_num);
hw/xen_platform.c +4 -4
@@ -83,7 +83,7 @@ static void log_writeb(PCIXenPlatformState *s, char val)#define UNPLUG_ALL_NICS 2#define UNPLUG_AUX_IDE_DISKS 4 -static void unplug_nic (PCIBus *b,PCIDevice *d) +static void unplug_nic (PCIBus *b,PCIDevice *d,void *o) {if (pci_get_word (d->config +PCI_CLASS_DEVICE ) ==PCI_CLASS_NETWORK_ETHERNET ) {
@@ -93,10 +93,10 @@ static void unplug_nic(PCIBus *b, PCIDevice *d)static void pci_unplug_nics (PCIBus *bus) { -pci_for_each_device (bus,0 , unplug_nic); +pci_for_each_device (bus,0 , unplug_nic,NULL ); } -static void unplug_disks (PCIBus *b,PCIDevice *d) +static void unplug_disks (PCIBus *b,PCIDevice *d,void *o) {if (pci_get_word (d->config +PCI_CLASS_DEVICE ) ==PCI_CLASS_STORAGE_IDE ) {
@@ -106,7 +106,7 @@ static void unplug_disks(PCIBus *b, PCIDevice *d)static void pci_unplug_disks (PCIBus *bus) { -pci_for_each_device (bus,0 , unplug_disks); +pci_for_each_device (bus,0 , unplug_disks,NULL ); }static void platform_fixed_ioport_writew (void *opaque,uint32_t addr,uint32_t val)
Signature · 1 line
Anthony PERARD
PATCH v9 5/8 Introduce Xen PCI Passthrough, qdevice (1/3)
In reply to Anthony PERARDFrom: Allen Kay <[email protected]>
A more complete history can be found here:git://xenbits.xensource.com/qemu-xen-unstable.git
--- Makefile.target | 2 + hw/xen_common.h | 3 + hw/xen_pci_passthrough.c | 808 ++++++++++++++++++++++++++++++++++ hw/xen_pci_passthrough.h | 250 +++++++++++ hw/xen_pci_passthrough_config_init.c | 11 + xen-all.c | 12 + 6 files changed, 1086 insertions(+), 0 deletions(-) create mode 100644 hw/xen_pci_passthrough.c create mode 100644 hw/xen_pci_passthrough.h create mode 100644 hw/xen_pci_passthrough_config_init.c
Makefile.target +2 -0
@@ -234,6 +234,8 @@ obj-i386-$(CONFIG_XEN) += xen_platform.o # Xen PCI Passthrough obj-i386-$(CONFIG_XEN_PCI_PASSTHROUGH) += host-pci-device.o +obj-i386-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen_pci_passthrough.o +obj-i386-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen_pci_passthrough_config_init.o # Inter-VM PCI shared memory CONFIG_IVSHMEM =
hw/xen_common.h +3 -0
@@ -135,4 +135,7 @@ static inline int xc_fd(xc_interface *xen_xc)void destroy_hvm_domain (void ); +/* shutdown/destroy current domain because of an error */ +void xen_shutdown_fatal_error(const char *fmt, ...) GCC_FMT_ATTR(1, 2); +#endif /* QEMU_HW_XEN_COMMON_H */
hw/xen_pci_passthrough.c new file +808 -0
808 lines not on this page · raw message
hw/xen_pci_passthrough.h new file +250 -0
250 lines not on this page · raw message
hw/xen_pci_passthrough_config_init.c new file +11 -0
@@ -0,0 +1,11 @@ +#include "xen_pci_passthrough.h" + +XenPTRegGroup *pt_find_reg_grp (XenPCIPassthroughState *s,uint32_t address) +{ +return NULL ; +} + +XenPTReg *pt_find_reg (XenPTRegGroup *reg_grp,uint32_t address) +{ +return NULL ; +}
xen-all.c +12 -0
@@ -1145,3 +1145,15 @@ void xen_register_framebuffer(MemoryRegion *mr) { framebuffer = mr; } + +void xen_shutdown_fatal_error (const char *fmt, ...) +{ +va_list ap; + +va_start (ap, fmt); +vfprintf (stderr, fmt, ap); +va_end (ap); +fprintf (stderr,"Will destroy the domain.\n" ); +/* destroy the domain */ +qemu_system_shutdown_request (); +}
Signature · 1 line
Anthony PERARD
PATCH v9 6/8 Introduce Xen PCI Passthrough, PCI config space helpers (2/3)
In reply to Anthony PERARDFrom: Allen Kay <[email protected]>
A more complete history can be found here:git://xenbits.xensource.com/qemu-xen-unstable.git
--- hw/xen_pci_passthrough.c | 10 + hw/xen_pci_passthrough.h | 2 + hw/xen_pci_passthrough_config_init.c | 1384 ++++++++++++++++++++++++++++++++++ 3 files changed, 1396 insertions(+), 0 deletions(-)
hw/xen_pci_passthrough.c +10 -0
@@ -669,6 +669,13 @@ static int pt_initfn(PCIDevice *d)/* Handle real device's MMIO/PIO BARs */ pt_register_regions (s); +/* reinitialize each config register to be emulated */ +if (pt_config_init (s)) { +PT_ERR (d,"PCI Config space initialisation failed.\n" ); +host_pci_device_put (s->real_device); +return -1 ; + } +/* Bind interrupt */ if (!s->dev.config[PCI_INTERRUPT_PIN ]) {PT_LOG (d,"no pin interrupt\n" );
@@ -767,6 +774,9 @@ static int pt_unregister_device(PCIDevice *d) } } +/* delete all emulated config registers */ +pt_config_delete (s); +pt_unregister_regions (s);memory_listener_unregister (&s->memory_listener);
hw/xen_pci_passthrough.h +2 -0
@@ -64,6 +64,8 @@ typedef int (*conf_byte_read)#define PT_BAR_ALLF 0xFFFFFFFF/* BAR ALLF value */ #define PT_PCI_BAR_UNMAPPED (-1) +#define PCI_CAP_MAX 48 + typedef enum { GRP_TYPE_HARDWIRED = 0, /* 0 Hardwired reg group */
hw/xen_pci_passthrough_config_init.c +1384 -0
1,395 lines not on this page · raw message
Signature · 1 line
Anthony PERARD
PATCH v9 7/8 Introduce apic-msidef.h
In reply to Anthony PERARDThis patch move the msi definition from apic.c to apic-msidef.h. So it can be used also by other .c files.
--- hw/apic-msidef.h | 30 ++++++++++++++++++++++++++++++ hw/apic.c | 11 +---------- 2 files changed, 31 insertions(+), 10 deletions(-) create mode 100644 hw/apic-msidef.h
hw/apic-msidef.h new file +30 -0
@@ -0,0 +1,30 @@ +#ifndef HW_APIC_MSIDEF_H +#define HW_APIC_MSIDEF_H + +/* +* Intel APIC constants: from include/asm/msidef.h +*/ + +/* +* Shifts for MSI data +*/ + +#define MSI_DATA_VECTOR_SHIFT 0 +#define MSI_DATA_VECTOR_MASK 0x000000ff + +#define MSI_DATA_DELIVERY_MODE_SHIFT 8 +#define MSI_DATA_LEVEL_SHIFT 14 +#define MSI_DATA_TRIGGER_SHIFT 15 + +/* +* Shift/mask fields for msi address +*/ + +#define MSI_ADDR_DEST_MODE_SHIFT 2 + +#define MSI_ADDR_REDIRECTION_SHIFT 3 + +#define MSI_ADDR_DEST_ID_SHIFT 12 +#define MSI_ADDR_DEST_ID_MASK 0x00ffff0 + +#endif /* HW_APIC_MSIDEF_H */
hw/apic.c +1 -10
@@ -22,19 +22,10 @@#include "host-utils.h" #include "trace.h" #include "pc.h" +#include "apic-msidef.h" #define MAX_APIC_WORDS 8 -/* Intel APIC constants: from include/asm/msidef.h */ -#define MSI_DATA_VECTOR_SHIFT 0 -#define MSI_DATA_VECTOR_MASK 0x000000ff -#define MSI_DATA_DELIVERY_MODE_SHIFT 8 -#define MSI_DATA_TRIGGER_SHIFT 15 -#define MSI_DATA_LEVEL_SHIFT 14 -#define MSI_ADDR_DEST_MODE_SHIFT 2 -#define MSI_ADDR_DEST_ID_SHIFT 12 -#define MSI_ADDR_DEST_ID_MASK 0x00ffff0 -#define SYNC_FROM_VAPIC 0x1#define SYNC_TO_VAPIC 0x2#define SYNC_ISR_IRR_TO_VAPIC 0x4
Signature · 1 line
Anthony PERARD
PATCH v9 8/8 Introduce Xen PCI Passthrough, MSI (3/3)
In reply to Anthony PERARDFrom: Jiang Yunhong <[email protected]>
A more complete history can be found here:git://xenbits.xensource.com/qemu-xen-unstable.git
--- Makefile.target | 1 + hw/xen_pci_passthrough.c | 31 ++- hw/xen_pci_passthrough.h | 51 +++ hw/xen_pci_passthrough_config_init.c | 471 ++++++++++++++++++++++++++ hw/xen_pci_passthrough_msi.c | 615 ++++++++++++++++++++++++++++++++++ 5 files changed, 1168 insertions(+), 1 deletions(-) create mode 100644 hw/xen_pci_passthrough_msi.c
Makefile.target +1 -0
@@ -236,6 +236,7 @@ obj-i386-$(CONFIG_XEN) += xen_platform.o obj-i386-$(CONFIG_XEN_PCI_PASSTHROUGH) += host-pci-device.o obj-i386-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen_pci_passthrough.o obj-i386-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen_pci_passthrough_config_init.o +obj-i386-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen_pci_passthrough_msi.o # Inter-VM PCI shared memory CONFIG_IVSHMEM =
hw/xen_pci_passthrough.c +30 -1
@@ -36,6 +36,20 @@* * Write '1' * - Set real bit to '1'. +* +* MSI interrupt: +* Initialize MSI register(pt_msi_setup, pt_msi_update) +* Bind MSI(xc_domain_update_msi_irq) +* <fail> +* - Unmap MSI. +* - Set dev->msi->pirq to '-1'. +* +* MSI-X interrupt: +* Initialize MSI-X register(pt_msix_update_one) +* Bind MSI-X(xc_domain_update_msi_irq) +* <fail> +* - Unmap MSI-X. +* - Set entry->pirq to '-1'. */ #include <sys/ioctl.h>
@@ -529,7 +543,15 @@ static void pt_region_update(XenPCIPassthroughState *s, }; bar =pt_bar_from_region (s, mr); -if (bar ==-1 ) { +if (bar ==-1 && (!s->msix || &s->msix->mmio != mr)) { +return ; + } + +if (s->msix && &s->msix->mmio == mr) { +if (adding) { + s->msix->mmio_base_addr = sec->offset_within_address_space; + rc =pt_msix_update_remap (s, s->msix->bar_index); + }return ; }
@@ -760,6 +782,13 @@ static int pt_unregister_device(PCIDevice *d) } } +if (s->msi) { +pt_msi_disable (s); + } +if (s->msix) { +pt_msix_disable (s); + } +if (machine_irq) { mapped_machine_irq[machine_irq]--;
hw/xen_pci_passthrough.h +51 -0
67 lines not on this page · raw message
hw/xen_pci_passthrough_config_init.c +471 -0
501 lines not on this page · raw message
hw/xen_pci_passthrough_msi.c new file +615 -0
615 lines not on this page · raw message
Signature · 1 line
Anthony PERARD
Re: [Qemu-devel] [PATCH V9 3/8] Introduce HostPCIDevice to access a pci device on the host.
In reply to Anthony PERARDQuoted 2 lines · Anthony PERARD
Signed-off-by Anthony PERARD <[email protected]>
Host is really linux sysfs, right?
Better reflect this in naming.
Or maybe even xen-host-pci-device?
Quoted 389 lines
--- Makefile.target | 3 + hw/host-pci-device.c | 278 ++++++++++++++++++++++++++++++++++++++++++++++++++ hw/host-pci-device.h | 75 ++++++++++++++ 3 files changed, 356 insertions(+), 0 deletions(-) create mode 100644 hw/host-pci-device.c create mode 100644 hw/host-pci-device.hMakefile.target +3
-0@@ -232,6 +232,9 @@ obj-$(CONFIG_NO_XEN) += xen-stub.o obj-i386-$(CONFIG_XEN) += xen_platform.o +# Xen PCI Passthrough +obj-i386-$(CONFIG_XEN_PCI_PASSTHROUGH) += host-pci-device.o + # Inter-VM PCI shared memory CONFIG_IVSHMEM = ifeq ($(CONFIG_KVM), y)hw/host-pci-device.c new file +278
-0@@ -0,0 +1,278 @@ +/* +* Copyright (C) 2011 Citrix Ltd. +* +* This work is licensed under the terms of the GNU GPL, version 2. See +* the COPYING file in the top-level directory. +* +*/ + +#include "qemu-common.h" +#include "host-pci-device.h" + +#define PCI_MAX_EXT_CAP \ + ((PCIE_CONFIG_SPACE_SIZE - PCI_CONFIG_SPACE_SIZE) / (PCI_CAP_SIZEOF + 4)) + +enum error_code { +ERROR_SYNTAX =1 , +}; + +static int path_to (const HostPCIDevice *d, +const char *name,char *buf,ssize_t size) +{ +return snprintf (buf, size,"/sys/bus/pci/devices/%04x:%02x:%02x.%x/%s" , + d->domain, d->bus, d->dev, d->func, name); +} + +static int get_resource (HostPCIDevice *d) +{ +int i, rc =0 ; +FILE *f; +char path[PATH_MAX ]; +unsigned long long start, end, flags, size; + +path_to (d,"resource" , path,sizeof (path)); + f =fopen (path,"r" ); +if (!f) { +fprintf (stderr,"Error: Can't open %s: %s\n" , path,strerror (errno)); +return -errno; + } + +for (i =0 ; i <PCI_NUM_REGIONS ; i++) { +if (fscanf (f,"%llx %llx %llx" , &start, &end, &flags) !=3 ) { +fprintf (stderr,"Error: Syntax error in %s\n" , path); + rc =ERROR_SYNTAX ; +break ; + } +if (start) { + size = end - start +1 ; + }else { + size =0 ; + } + +if (i <PCI_ROM_SLOT ) { + d->io_regions[i].base_addr = start; + d->io_regions[i].size = size; + d->io_regions[i].flags = flags; + }else { + d->rom.base_addr = start; + d->rom.size = size; + d->rom.flags = flags; + } + } + +fclose (f); +return rc; +} + +static int get_hex_value (HostPCIDevice *d,const char *name, +unsigned long *pvalue) +{ +char path[PATH_MAX ]; +FILE *f; +unsigned long value; + +path_to (d, name, path,sizeof (path)); + f =fopen (path,"r" ); +if (!f) { +fprintf (stderr,"Error: Can't open %s: %s\n" , path,strerror (errno)); +return -errno; + } +if (fscanf (f,"%lx\n" , &value) !=1 ) { +fprintf (stderr,"Error: Syntax error in %s\n" , path); +fclose (f); +return ERROR_SYNTAX ; + } +fclose (f); + *pvalue = value; +return 0 ; +} + +static bool pci_dev_is_virtfn (HostPCIDevice *d) +{ +char path[PATH_MAX ]; +struct stat buf; + +path_to (d,"physfn" , path,sizeof (path)); +return !stat (path, &buf); +} + +static int host_pci_config_fd (HostPCIDevice *d) +{ +char path[PATH_MAX ]; + +if (d->config_fd <0 ) { +path_to (d,"config" , path,sizeof (path)); + d->config_fd =open (path,O_RDWR ); +if (d->config_fd <0 ) { +fprintf (stderr,"HostPCIDevice: Can not open '%s': %s\n" , + path,strerror (errno)); + } + } +return d->config_fd; +} +static int host_pci_config_read (HostPCIDevice *d,int pos,void *buf,int len) +{ +int fd =host_pci_config_fd (d); +int res =0 ; + +again: + res =pread (fd, buf, len, pos); +if (res != len) { +if (res <0 && (errno ==EINTR || errno ==EAGAIN )) { + goto again; + } +fprintf (stderr,"%s: read failed: %s (fd: %i)\n" , + __func__,strerror (errno), fd); +return -errno; + } +return 0 ; +} +static int host_pci_config_write (HostPCIDevice *d, +int pos,const void *buf,int len) +{ +int fd =host_pci_config_fd (d); +int res =0 ; + +again: + res =pwrite (fd, buf, len, pos); +if (res != len) { +if (res <0 && (errno ==EINTR || errno ==EAGAIN )) { + goto again; + } +fprintf (stderr,"%s: write failed: %s\n" , + __func__,strerror (errno)); +return -errno; + } +return 0 ; +} + +int host_pci_get_byte (HostPCIDevice *d,int pos,uint8_t *p) +{ +uint8_t buf; +int rc =host_pci_config_read (d, pos, &buf,1 ); +if (rc ==0 ) { + *p = buf; + } +return rc; +} +int host_pci_get_word (HostPCIDevice *d,int pos,uint16_t *p) +{ +uint16_t buf; +int rc =host_pci_config_read (d, pos, &buf,2 ); +if (rc ==0 ) { + *p =le16_to_cpu (buf); + } +return rc; +} +int host_pci_get_long (HostPCIDevice *d,int pos,uint32_t *p) +{ +uint32_t buf; +int rc =host_pci_config_read (d, pos, &buf,4 ); +if (rc ==0 ) { + *p =le32_to_cpu (buf); + } +return rc; +} +int host_pci_get_block (HostPCIDevice *d,int pos,uint8_t *buf,int len) +{ +return host_pci_config_read (d, pos, buf, len); +} + +int host_pci_set_byte (HostPCIDevice *d,int pos,uint8_t data) +{ +return host_pci_config_write (d, pos, &data,1 ); +} +int host_pci_set_word (HostPCIDevice *d,int pos,uint16_t data) +{ + data =cpu_to_le16 (data); +return host_pci_config_write (d, pos, &data,2 ); +} +int host_pci_set_long (HostPCIDevice *d,int pos,uint32_t data) +{ + data =cpu_to_le32 (data); +return host_pci_config_write (d, pos, &data,4 ); +} +int host_pci_set_block (HostPCIDevice *d,int pos,uint8_t *buf,int len) +{ +return host_pci_config_write (d, pos, buf, len); +} + +uint32_t host_pci_find_ext_cap_offset (HostPCIDevice *d,uint32_t cap) +{ +uint32_t header =0 ; +int max_cap =PCI_MAX_EXT_CAP ; +int pos =PCI_CONFIG_SPACE_SIZE ; + +do { +if (host_pci_get_long (d, pos, &header)) { +break ; + } +/* +* If we have no capabilities, this is indicated by cap ID, +* cap version and next pointer all being 0. +*/ +if (header ==0 ) { +break ; + } + +if (PCI_EXT_CAP_ID (header) == cap) { +return pos; + } + + pos =PCI_EXT_CAP_NEXT (header); +if (pos <PCI_CONFIG_SPACE_SIZE ) { +break ; + } + + max_cap--; + }while (max_cap >0 ); + +return 0 ; +} + +HostPCIDevice *host_pci_device_get (uint8_t bus,uint8_t dev,uint8_t func) +{ +HostPCIDevice *d =NULL ; +unsigned long v =0 ; + + d =g_new0 (HostPCIDevice,1 ); + + d->config_fd =-1 ; + d->domain =0 ; + d->bus = bus; + d->dev = dev; + d->func = func; + +if (host_pci_config_fd (d) ==-1 ) { + goto error; + } +if (get_resource (d) !=0 ) { + goto error; + } + +if (get_hex_value (d,"vendor" , &v)) { + goto error; + } + d->vendor_id = v; +if (get_hex_value (d,"device" , &v)) { + goto error; + } + d->device_id = v; + d->is_virtfn =pci_dev_is_virtfn (d); + +return d; +error: +if (d->config_fd >=0 ) { +close (d->config_fd); + } +g_free (d); +return NULL ; +} + +void host_pci_device_put (HostPCIDevice *d) +{ +if (d->config_fd >=0 ) { +close (d->config_fd); + } +g_free (d); +}hw/host-pci-device.h new file +75
-0@@ -0,0 +1,75 @@ +#ifndef HW_HOST_PCI_DEVICE +# define HW_HOST_PCI_DEVICE + +#include "pci.h" + +/* +* from linux/ioport.h +* IO resources have these defined flags. +*/ +#define IORESOURCE_BITS 0x000000ff/* Bus-specific bits */ + +#define IORESOURCE_TYPE_BITS 0x00000f00/* Resource type */ +#define IORESOURCE_IO 0x00000100 +#define IORESOURCE_MEM 0x00000200 +#define IORESOURCE_IRQ 0x00000400 +#define IORESOURCE_DMA 0x00000800 + +#define IORESOURCE_PREFETCH 0x00001000/* No side effects */ +#define IORESOURCE_READONLY 0x00002000 +#define IORESOURCE_CACHEABLE 0x00004000 +#define IORESOURCE_RANGELENGTH 0x00008000 +#define IORESOURCE_SHADOWABLE 0x00010000 + +#define IORESOURCE_SIZEALIGN 0x00020000/* size indicates alignment */ +#define IORESOURCE_STARTALIGN 0x00040000/* start field is alignment */ + +#define IORESOURCE_MEM_64 0x00100000 + +/* Userland may not map this resource */ +#define IORESOURCE_EXCLUSIVE 0x08000000 +#define IORESOURCE_DISABLED 0x10000000 +#define IORESOURCE_UNSET 0x20000000 +#define IORESOURCE_AUTO 0x40000000 +/* Driver has marked this resource busy */ +#define IORESOURCE_BUSY 0x80000000 + + +typedef struct HostPCIIORegion { +unsigned long flags; +pcibus_t base_addr; +pcibus_t size; +}HostPCIIORegion ; + +typedef struct HostPCIDevice { +uint16_t domain; +uint8_t bus; +uint8_t dev; +uint8_t func; + +uint16_t vendor_id; +uint16_t device_id; + +HostPCIIORegion io_regions[PCI_NUM_REGIONS -1 ]; +HostPCIIORegion rom; + +bool is_virtfn; + +int config_fd; +}HostPCIDevice ; + +HostPCIDevice *host_pci_device_get (uint8_t bus,uint8_t dev,uint8_t func); +void host_pci_device_put (HostPCIDevice *pci_dev); + +int host_pci_get_byte (HostPCIDevice *d,int pos,uint8_t *p); +int host_pci_get_word (HostPCIDevice *d,int pos,uint16_t *p); +int host_pci_get_long (HostPCIDevice *d,int pos,uint32_t *p); +int host_pci_get_block (HostPCIDevice *d,int pos,uint8_t *buf,int len); +int host_pci_set_byte (HostPCIDevice *d,int pos,uint8_t data); +int host_pci_set_word (HostPCIDevice *d,int pos,uint16_t data); +int host_pci_set_long (HostPCIDevice *d,int pos,uint32_t data); +int host_pci_set_block (HostPCIDevice *d,int pos,uint8_t *buf,int len); + +uint32_t host_pci_find_ext_cap_offset (HostPCIDevice *s,uint32_t cap); + +#endif /* !HW_HOST_PCI_DEVICE */ Signature · 1 line
Anthony PERARD
Re: [Qemu-devel] [PATCH V9 3/8] Introduce HostPCIDevice to access a pci device on the host.
Quoted 1 line · Michael S. Tsirkin
Host is really linux sysfs, right?
Yes, it is.
Quoted 1 line
Better reflect this in naming.
If someone need to access to a device by an other ways, it's could be implemented also in this file, no ?
Quoted 1 line
Or maybe even xen-host-pci-device?
These functions are not very Xen specific and just provide a layer to access to a pci device.
Signature · 1 line
Anthony PERARD
Re: [Qemu-devel] [PATCH V9 3/8] Introduce HostPCIDevice to access a pci device on the host.
In reply to Anthony PERARDQuoted 2 lines · Anthony PERARD
Signed-off-by Anthony PERARD <[email protected]>
So this interface is really LinuxSysfsPCIDevice.
For example the assumption that you can just open
device by pci address is broken with vfio.
Domain number is also not something anyone
besides linux knows about.
If I were you I would just call it xen- ....
and if it comes in handy it can be later renamed.
Quoted 31 lines
--- Makefile.target | 3 + hw/host-pci-device.c | 278 ++++++++++++++++++++++++++++++++++++++++++++++++++ hw/host-pci-device.h | 75 ++++++++++++++ 3 files changed, 356 insertions(+), 0 deletions(-) create mode 100644 hw/host-pci-device.c create mode 100644 hw/host-pci-device.hMakefile.target +3
-0@@ -232,6 +232,9 @@ obj-$(CONFIG_NO_XEN) += xen-stub.o obj-i386-$(CONFIG_XEN) += xen_platform.o +# Xen PCI Passthrough +obj-i386-$(CONFIG_XEN_PCI_PASSTHROUGH) += host-pci-device.o + # Inter-VM PCI shared memory CONFIG_IVSHMEM = ifeq ($(CONFIG_KVM), y)hw/host-pci-device.c new file +3
-0@@ -0,0 +1,278 @@ +/* +* Copyright (C) 2011 Citrix Ltd. +*
hw/host-pci-device.c line 13
Quoted 10 lines
hw/host-pci-device.c new file +10
-0+* This work is licensed under the terms of the GNU GPL, version 2. See +* the COPYING file in the top-level directory. +* +*/ + +#include "qemu-common.h" +#include "host-pci-device.h" + +#define PCI_MAX_EXT_CAP \ + ((PCIE_CONFIG_SPACE_SIZE - PCI_CONFIG_SPACE_SIZE) / (PCI_CAP_SIZEOF + 4))
namespace pollution.
name all things HOST_PCI_....
in this case, open-coding will make things clearer.
hw/host-pci-device.c line 15
Quoted 2 lines
hw/host-pci-device.c new file +2
-0+ +enum error_code {
seems unused. So why name the type?
hw/host-pci-device.c line 16
Quoted 1 line
hw/host-pci-device.c new file +1
-0+ ERROR_SYNTAX = 1,
We return -1 on error, just do that and you won't need ERROR_SYNTAX.
hw/host-pci-device.c line 24
Quoted 8 lines
hw/host-pci-device.c new file +8
-0+}; + +static int path_to (const HostPCIDevice *d, +const char *name,char *buf,ssize_t size) +{ +return snprintf (buf, size,"/sys/bus/pci/devices/%04x:%02x:%02x.%x/%s" , + d->domain, d->bus, d->dev, d->func, name); +}
users ignore return value. Also, want to check no overflow and assert?
hw/host-pci-device.c line 33
Quoted 9 lines
hw/host-pci-device.c new file +9
-0+ +static int get_resource (HostPCIDevice *d) +{ +int i, rc =0 ; +FILE *f; +char path[PATH_MAX ]; +unsigned long long start, end, flags, size; + +path_to (d,"resource" , path,sizeof (path));
I think this might not fit, snprintf needs an extra byte for \0.
hw/host-pci-device.c line 41
Quoted 8 lines
hw/host-pci-device.c new file +8
-0+ f =fopen (path,"r" ); +if (!f) { +fprintf (stderr,"Error: Can't open %s: %s\n" , path,strerror (errno)); +return -errno; + } + +for (i =0 ; i <PCI_NUM_REGIONS ; i++) { +if (fscanf (f,"%llx %llx %llx" , &start, &end, &flags) !=3 ) {
People mentioned that scanf is not a good way to parse input. Applies here.
Quoted 17 lines
hw/host-pci-device.c new file +17
-0+fprintf (stderr,"Error: Syntax error in %s\n" , path); + rc =ERROR_SYNTAX ; +break ; + } +if (start) { + size = end - start +1 ; + }else { + size =0 ; + } + +if (i <PCI_ROM_SLOT ) { + d->io_regions[i].base_addr = start; + d->io_regions[i].size = size; + d->io_regions[i].flags = flags; + }else { + d->rom.base_addr = start; + d->rom.size = size;
hw/host-pci-device.c line 68
Quoted 10 lines
hw/host-pci-device.c new file +10
-0+ d->rom.flags = flags; + } + } + +fclose (f); +return rc; +} + +static int get_hex_value (HostPCIDevice *d,const char *name, +unsigned long *pvalue)
why long?
Quoted 20 lines
hw/host-pci-device.c new file +20
-0+{ +char path[PATH_MAX ]; +FILE *f; +unsigned long value; + +path_to (d, name, path,sizeof (path)); + f =fopen (path,"r" ); +if (!f) { +fprintf (stderr,"Error: Can't open %s: %s\n" , path,strerror (errno)); +return -errno; + } +if (fscanf (f,"%lx\n" , &value) !=1 ) { +fprintf (stderr,"Error: Syntax error in %s\n" , path); +fclose (f); +return ERROR_SYNTAX ; + } +fclose (f); + *pvalue = value; +return 0 ; +}
hw/host-pci-device.c line 98
Quoted 10 lines
hw/host-pci-device.c new file +10
-0+ +static bool pci_dev_is_virtfn (HostPCIDevice *d) +{ +char path[PATH_MAX ]; +struct stat buf; + +path_to (d,"physfn" , path,sizeof (path)); +return !stat (path, &buf); +} +
Don't start names with pci_.
It would also be better to avoid things like path_to IMO.
hw/host-pci-device.c line 99
Quoted 1 line
hw/host-pci-device.c new file +1
-0+static int host_pci_config_fd (HostPCIDevice *d)
So this opens if needed, and returns.
Why not explicitly open on get?
then you won't need these hacks.
hw/host-pci-device.c line 104
Quoted 5 lines
hw/host-pci-device.c new file +5
-0+{ +char path[PATH_MAX ]; + +if (d->config_fd <0 ) { +path_to (d,"config" , path,sizeof (path));
sizeof path
hw/host-pci-device.c line 108
Quoted 4 lines
hw/host-pci-device.c new file +4
-0+ d->config_fd =open (path,O_RDWR ); +if (d->config_fd <0 ) { +fprintf (stderr,"HostPCIDevice: Can not open '%s': %s\n" , + path,strerror (errno));
strerror is not thread safe
hw/host-pci-device.c line 115
Quoted 7 lines
hw/host-pci-device.c new file +7
-0+ } + } +return d->config_fd; +} +static int host_pci_config_read (HostPCIDevice *d,int pos,void *buf,int len) +{ +int fd =host_pci_config_fd (d);
You open file on each access?
hw/host-pci-device.c line 116
Quoted 1 line
hw/host-pci-device.c new file +1
-0+int res =0 ;
why initialize here?
hw/host-pci-device.c line 122
Quoted 6 lines
hw/host-pci-device.c new file +6
-0+ +again: + res =pread (fd, buf, len, pos); +if (res != len) { +if (res <0 && (errno ==EINTR || errno ==EAGAIN )) { + goto again;
code loops with while or for.
Quoted 16 lines
hw/host-pci-device.c new file +16
-0+ } +fprintf (stderr,"%s: read failed: %s (fd: %i)\n" , + __func__,strerror (errno), fd); +return -errno; + } +return 0 ; +} +static int host_pci_config_write (HostPCIDevice *d, +int pos,const void *buf,int len) +{ +int fd =host_pci_config_fd (d); +int res =0 ; + +again: + res =pwrite (fd, buf, len, pos); +if (res != len) {
hw/host-pci-device.c line 148
Quoted 10 lines
hw/host-pci-device.c new file +10
-0+if (res <0 && (errno ==EINTR || errno ==EAGAIN )) { + goto again; + } +fprintf (stderr,"%s: write failed: %s\n" , + __func__,strerror (errno)); +return -errno; + } +return 0 ; +} +
same comments as above. also,
Don't report errors with fprintf.
hw/host-pci-device.c line 153
Quoted 5 lines
hw/host-pci-device.c new file +5
-0+int host_pci_get_byte (HostPCIDevice *d,int pos,uint8_t *p) +{ +uint8_t buf; +int rc =host_pci_config_read (d, pos, &buf,1 ); +if (rc ==0 ) {
!rc.
hw/host-pci-device.c line 154
Quoted 1 line
hw/host-pci-device.c new file +1
-0+ *p = buf;
why not pass in p directly?
hw/host-pci-device.c line 162
Quoted 8 lines
hw/host-pci-device.c new file +8
-0+ } +return rc; +} +int host_pci_get_word (HostPCIDevice *d,int pos,uint16_t *p) +{ +uint16_t buf; +int rc =host_pci_config_read (d, pos, &buf,2 ); +if (rc ==0 ) {
!rc.
hw/host-pci-device.c line 166
Quoted 4 lines
hw/host-pci-device.c new file +4
-0+ *p =le16_to_cpu (buf); + } +return rc; +}
This looks wrong wrt endian-ness.
hw/host-pci-device.c line 175
Quoted 9 lines
hw/host-pci-device.c new file +9
-0+int host_pci_get_long (HostPCIDevice *d,int pos,uint32_t *p) +{ +uint32_t buf; +int rc =host_pci_config_read (d, pos, &buf,4 ); +if (rc ==0 ) { + *p =le32_to_cpu (buf); + } +return rc; +}
Add empty lines between {}
hw/host-pci-device.c line 179
Quoted 4 lines
hw/host-pci-device.c new file +4
-0+int host_pci_get_block (HostPCIDevice *d,int pos,uint8_t *buf,int len) +{ +return host_pci_config_read (d, pos, buf, len); +}
when would this be useful?
Quoted 11 lines
hw/host-pci-device.c new file +11
-0+ +int host_pci_set_byte (HostPCIDevice *d,int pos,uint8_t data) +{ +return host_pci_config_write (d, pos, &data,1 ); +} +int host_pci_set_word (HostPCIDevice *d,int pos,uint16_t data) +{ + data =cpu_to_le16 (data); +return host_pci_config_write (d, pos, &data,2 ); +} +int host_pci_set_long (HostPCIDevice *d,int pos,uint32_t data)
hw/host-pci-device.c line 200
Quoted 10 lines
hw/host-pci-device.c new file +10
-0+{ + data =cpu_to_le32 (data); +return host_pci_config_write (d, pos, &data,4 ); +} +int host_pci_set_block (HostPCIDevice *d,int pos,uint8_t *buf,int len) +{ +return host_pci_config_write (d, pos, buf, len); +} + +uint32_t host_pci_find_ext_cap_offset (HostPCIDevice *d,uint32_t cap)
Why 32? Ext config offsets are < 12 bit.
Quoted 23 lines
hw/host-pci-device.c new file +23
-0+{ +uint32_t header =0 ; +int max_cap =PCI_MAX_EXT_CAP ; +int pos =PCI_CONFIG_SPACE_SIZE ; + + do { + if (host_pci_get_long(d, pos, &header)) { + break; + } + /* + * If we have no capabilities, this is indicated by cap ID, + * cap version and next pointer all being 0. + */ + if (header == 0) { + break; + } + + if (PCI_EXT_CAP_ID(header) == cap) { + return pos; + } + + pos = PCI_EXT_CAP_NEXT(header); + if (pos < PCI_CONFIG_SPACE_SIZE) {
hw/host-pci-device.c line 233
Quoted 10 lines
hw/host-pci-device.c new file +10
-0+break ; + } + + max_cap--; + }while (max_cap >0 ); + +return 0 ; +} + +HostPCIDevice *host_pci_device_get (uint8_t bus,uint8_t dev,uint8_t func)
Why skip domain in the interface?
Also, HostPCIDevice structure is public so there is little value in allocating, just get it by pointer and init/cleanup.
Quoted 6 lines
hw/host-pci-device.c new file +6
-0+{ +HostPCIDevice *d =NULL ; +unsigned long v =0 ; + + d =g_new0 (HostPCIDevice,1 ); +
hw/host-pci-device.c line 249
Quoted 10 lines
hw/host-pci-device.c new file +10
-0+ d->config_fd =-1 ; + d->domain =0 ; + d->bus = bus; + d->dev = dev; + d->func = func; + +if (host_pci_config_fd (d) ==-1 ) { + goto error; + } +if (get_resource (d) !=0 ) {
just get_resource(d).
Quoted 35 lines
hw/host-pci-device.c new file +29
-0+ goto error; + } + +if (get_hex_value (d,"vendor" , &v)) { + goto error; + } + d->vendor_id = v; +if (get_hex_value (d,"device" , &v)) { + goto error; + } + d->device_id = v; + d->is_virtfn =pci_dev_is_virtfn (d); + +return d; +error: +if (d->config_fd >=0 ) { +close (d->config_fd); + } +g_free (d); +return NULL ; +} + +void host_pci_device_put (HostPCIDevice *d) +{ +if (d->config_fd >=0 ) { +close (d->config_fd); + } +g_free (d); +}hw/host-pci-device.h new file +0
-0
hw/host-pci-device.h line 2
Quoted 2 lines
hw/host-pci-device.h new file +2
-0@@ -0,0 +1,75 @@ +#ifndef HW_HOST_PCI_DEVICE +# define HW_HOST_PCI_DEVICE
Don't put space after #.
Also HOST_PCI_DEVICE_H would be less likely to confuse.
Quoted 24 lines
hw/host-pci-device.h new file +24
-0+ +#include "pci.h" + +/* +* from linux/ioport.h +* IO resources have these defined flags. +*/ +#define IORESOURCE_BITS 0x000000ff/* Bus-specific bits */ + +#define IORESOURCE_TYPE_BITS 0x00000f00/* Resource type */ +#define IORESOURCE_IO 0x00000100 +#define IORESOURCE_MEM 0x00000200 +#define IORESOURCE_IRQ 0x00000400 +#define IORESOURCE_DMA 0x00000800 + +#define IORESOURCE_PREFETCH 0x00001000/* No side effects */ +#define IORESOURCE_READONLY 0x00002000 +#define IORESOURCE_CACHEABLE 0x00004000 +#define IORESOURCE_RANGELENGTH 0x00008000 +#define IORESOURCE_SHADOWABLE 0x00010000 + +#define IORESOURCE_SIZEALIGN 0x00020000/* size indicates alignment */ +#define IORESOURCE_STARTALIGN 0x00040000/* start field is alignment */ +
hw/host-pci-device.h line 36
Quoted 10 lines
hw/host-pci-device.h new file +10
-0+#define IORESOURCE_MEM_64 0x00100000 + +/* Userland may not map this resource */ +#define IORESOURCE_EXCLUSIVE 0x08000000 +#define IORESOURCE_DISABLED 0x10000000 +#define IORESOURCE_UNSET 0x20000000 +#define IORESOURCE_AUTO 0x40000000 +/* Driver has marked this resource busy */ +#define IORESOURCE_BUSY 0x80000000 +
Why do above make sense in an API?
Abstract it in some reasonable way, don't just expose flags from sysfs as is.
hw/host-pci-device.h line 37
Quoted 1 line
hw/host-pci-device.h new file +1
-0+
kill extra empty lines
Quoted 40 lines
hw/host-pci-device.h new file +38
-0+typedef struct HostPCIIORegion { +unsigned long flags; +pcibus_t base_addr; +pcibus_t size; +}HostPCIIORegion ; + +typedef struct HostPCIDevice { +uint16_t domain; +uint8_t bus; +uint8_t dev; +uint8_t func; + +uint16_t vendor_id; +uint16_t device_id; + +HostPCIIORegion io_regions[PCI_NUM_REGIONS -1 ]; +HostPCIIORegion rom; + +bool is_virtfn; + +int config_fd; +}HostPCIDevice ; + +HostPCIDevice *host_pci_device_get (uint8_t bus,uint8_t dev,uint8_t func); +void host_pci_device_put (HostPCIDevice *pci_dev); + +int host_pci_get_byte (HostPCIDevice *d,int pos,uint8_t *p); +int host_pci_get_word (HostPCIDevice *d,int pos,uint16_t *p); +int host_pci_get_long (HostPCIDevice *d,int pos,uint32_t *p); +int host_pci_get_block (HostPCIDevice *d,int pos,uint8_t *buf,int len); +int host_pci_set_byte (HostPCIDevice *d,int pos,uint8_t data); +int host_pci_set_word (HostPCIDevice *d,int pos,uint16_t data); +int host_pci_set_long (HostPCIDevice *d,int pos,uint32_t data); +int host_pci_set_block (HostPCIDevice *d,int pos,uint8_t *buf,int len); + +uint32_t host_pci_find_ext_cap_offset (HostPCIDevice *s,uint32_t cap); + +#endif /* !HW_HOST_PCI_DEVICE */ Signature · 1 line
Anthony PERARD
Re: [Qemu-devel] [PATCH V9 5/8] Introduce Xen PCI Passthrough, qdevice (1/3)
In reply to Anthony PERARDhw/xen_pci_passthrough.h line 2
Quoted 8 lines · Anthony PERARD
hw/xen_pci_passthrough.h new file +2
-0@@ -0,0 +1,250 @@ +#ifndef QEMU_HW_XEN_PCI_PASSTHROUGH_H +# define QEMU_HW_XEN_PCI_PASSTHROUGH_H
Way overboard. QEMU_HW_ is not needed.
hw/xen_pci_passthrough.h line 10
Quoted 8 lines
hw/xen_pci_passthrough.h new file +8
-0+ +#include "qemu-common.h" +#include "xen_common.h" +#include "pci.h" +#include "host-pci-device.h" + +/* #define PT_LOGGING_ENABLED */ +/* #define PT_DEBUG_PCI_CONFIG_ACCESS */
Don't keep dead code around esp not in headers.
Quoted 10 lines
hw/xen_pci_passthrough.h new file +10
-0+ +void pt_log(const PCIDevice *d, const char *f, ...) GCC_FMT_ATTR(2, 3); + +#define PT_ERR(d, _f, _a...) pt_log(d, "%s: Error: " _f, __func__, ##_a) + +#ifdef PT_LOGGING_ENABLED +# define PT_LOG(d, _f, _a...) pt_log(d, "%s: " _f, __func__, ##_a) +# define PT_WARN(d, _f, _a...) pt_log(d, "%s: Warning: " _f, __func__, ##_a) +#else +# define PT_LOG(d, _f, _a...)
hw/xen_pci_passthrough.h line 30
Quoted 10 lines
hw/xen_pci_passthrough.h new file +10
-0+# define PT_WARN(d, _f, _a...) +#endif + +#ifdef PT_DEBUG_PCI_CONFIG_ACCESS +# define PT_LOG_CONFIG (d, addr, val, len) \ + pt_log(d, "%s: address=0x%04x val=0x%08x len=%d\n", \ + __func__, addr, val, len) +#else +# define PT_LOG_CONFIG (d, addr, val, len) +#endif
There's no reason not to prefix xen things with XEN_PT_ xen_pt_ etc.
And if you make names consistens with file names
naming files xen_pt_xxxx.x, people will know where to look for them.
Signature · 1 line
MST
Re: [Qemu-devel] [PATCH V9 1/8] pci_ids: Add INTEL_82599_SFP_VF id.
In reply to Anthony PERARDQuoted 1 line · Anthony PERARD
Signed-off-by Anthony PERARD <[email protected]>
Re: [Qemu-devel] [PATCH V9 4/8] pci.c: Add opaque argument to pci_for_each_device.
In reply to Anthony PERARDQuoted 1 line · Anthony PERARD
Signed-off-by Anthony PERARD <[email protected]>
Re: [Qemu-devel] [PATCH V9 5/8] Introduce Xen PCI Passthrough, qdevice (1/3)
In reply to Michael S. TsirkinQuoted 40 lines · Michael S. Tsirkin
hw/xen_pci_passthrough.h line 2
Quoted 8 lines · Anthony PERARD
hw/xen_pci_passthrough.h new file +2
-0@@ -0,0 +1,250 @@ +#ifndef QEMU_HW_XEN_PCI_PASSTHROUGH_H +# define QEMU_HW_XEN_PCI_PASSTHROUGH_HWay overboard.
QEMU_HW_is not needed.hw/xen_pci_passthrough.h line 10
Quoted 8 lines
hw/xen_pci_passthrough.h new file +8
-0+ +#include "qemu-common.h" +#include "xen_common.h" +#include "pci.h" +#include "host-pci-device.h" + +/* #define PT_LOGGING_ENABLED */ +/* #define PT_DEBUG_PCI_CONFIG_ACCESS */ Don't keep dead code around esp not in headers.
Quoted 17 lines
hw/xen_pci_passthrough.h new file +17
-0+ +void pt_log(const PCIDevice *d, const char *f, ...) GCC_FMT_ATTR(2, 3); + +#define PT_ERR(d, _f, _a...) pt_log(d, "%s: Error: " _f, __func__, ##_a) + +#ifdef PT_LOGGING_ENABLED +# define PT_LOG(d, _f, _a...) pt_log(d, "%s: " _f, __func__, ##_a) +# define PT_WARN(d, _f, _a...) pt_log(d, "%s: Warning: " _f, __func__, ##_a) +#else +# define PT_LOG(d, _f, _a...) +# define PT_WARN(d, _f, _a...) +#endif + +#ifdef PT_DEBUG_PCI_CONFIG_ACCESS +# define PT_LOG_CONFIG(d, addr, val, len) \ + pt_log(d, "%s: address=0x%04x val=0x%08x len=%d\n", \ + __func__, addr, val, len)
Quoted 8 lines
hw/xen_pci_passthrough.h line 30
Quoted 3 lines
hw/xen_pci_passthrough.h new file +3
-0+#else +# define PT_LOG_CONFIG(d, addr, val, len) +#endif There's no reason not to prefix xen things with
XEN_PT_xen_pt_etc.
And if you make names consistens with file names
naming files xen_pt_xxxx.x, people will know where to look for them.
OK, I'll change that.
Thanks,+
Signature · 1 line
Anthony PERARD
Re: [Qemu-devel] [PATCH V9 3/8] Introduce HostPCIDevice to access a pci device on the host.
In reply to Michael S. TsirkinQuoted 4 lines · Michael S. Tsirkin
Quoted 2 lines · Anthony PERARD
Signed-off-by Anthony PERARD <[email protected]>
Quoted 8 lines
So this interface is really LinuxSysfsPCIDevice.
For example the assumption that you can just open
device by pci address is broken with vfio.
Domain number is also not something anyone
besides linux knows about.If I were you I would just call it xen- ....
and if it comes in handy it can be later renamed.
Ok, I will rename that XenHostPCIDevice.
Quoted 48 lines
Quoted 31 lines
---
Makefile.target | 3 +
hw/host-pci-device.c | 278 ++++++++++++++++++++++++++++++++++++++++++++++++++hw/host-pci-device.h| 75 ++++++++++++++3 files changed, 356 insertions(+), 0 deletions(-)create mode 100644
hw/host-pci-device.c
create mode 100644hw/host-pci-device.hMakefile.target +0
-0@@ -232,6 +232,9 @@ obj-$(CONFIG_NO_XEN) += xen-stub.oobj-i386-$(CONFIG_XEN) += xen_platform.o
Makefile.target +3
-0+# Xen PCI Passthrough +obj-i386-$(CONFIG_XEN_PCI_PASSTHROUGH) += host-pci-device.o +# Inter-VM PCI shared memory
CONFIG_IVSHMEM =
ifeq ($(CONFIG_KVM), y)hw/host-pci-device.c new file +3
-0@@ -0,0 +1,278 @@ +/* +* Copyright (C) 2011 Citrix Ltd. +* hw/host-pci-device.c line 13
Quoted 10 lines
hw/host-pci-device.c new file +10
-0+* This work is licensed under the terms of the GNU GPL, version 2. See +* the COPYING file in the top-level directory. +* +*/ + +#include "qemu-common.h" +#include "host-pci-device.h" + +#define PCI_MAX_EXT_CAP \ + ((PCIE_CONFIG_SPACE_SIZE - PCI_CONFIG_SPACE_SIZE) / (PCI_CAP_SIZEOF + 4))namespace pollution.
name all thingsHOST_PCI_....in this case, open-coding will make things clearer.
Quoted 8 lines
hw/host-pci-device.c line 15
Quoted 2 lines
hw/host-pci-device.c new file +2
-0+ +enum error_code {seems unused. So why name the type?
hw/host-pci-device.c line 16
Quoted 1 line
hw/host-pci-device.c new file +1
-0+ ERROR_SYNTAX = 1,We return -1 on error, just do that and you won't need
ERROR_SYNTAX.
Ok, I'll remove this.
Quoted 11 lines
hw/host-pci-device.c line 24
Quoted 8 lines
hw/host-pci-device.c new file +8
-0+}; + +static int path_to (const HostPCIDevice *d, + const char *name, char *buf, ssize_t size) +{ + return snprintf(buf, size, "/sys/bus/pci/devices/%04x:%02x:%02x.%x/%s", + d->domain, d->bus, d->dev, d->func, name); +}users ignore return value. Also, want to check no overflow and assert?
I will check the return value in this function an then return 0 or -1.
Quoted 11 lines
hw/host-pci-device.c line 33
Quoted 9 lines
hw/host-pci-device.c new file +9
-0+ +static int get_resource (HostPCIDevice *d) +{ + int i, rc = 0; + FILE *f; + char path[PATH_MAX]; + unsigned long long start, end, flags, size; + + path_to(d, "resource", path, sizeof (path));I think this might not fit, snprintf needs an extra byte for \0.
I just check snprintf write size byte including the \0, sw we should just give the size of the buffer.
Quoted 11 lines
hw/host-pci-device.c line 41
Quoted 8 lines
hw/host-pci-device.c new file +8
-0+ f = fopen(path, "r"); + if (!f) { + fprintf(stderr, "Error: Can't open %s: %s\n", path, strerror(errno)); + return -errno; + } + + for (i = 0; i < PCI_NUM_REGIONS; i++) { + if (fscanf(f, "%llx %llx %llx", &start, &end, &flags) != 3) {People mentioned that scanf is not a good way to parse input. Applies here.
Ok, I'll do a manual parsing :(.
Quoted 21 lines
Quoted 21 lines
hw/host-pci-device.c new file +21
-0+ fprintf(stderr, "Error: Syntax error in %s\n", path); + rc = ERROR_SYNTAX; + break; + } + if (start) { + size = end - start + 1; + } else { + size = 0; + } + + if (i < PCI_ROM_SLOT) { + d->io_regions[i].base_addr = start; + d->io_regions[i].size = size; + d->io_regions[i].flags = flags; + } else { + d->rom.base_addr = start; + d->rom.size = size; + d->rom.flags = flags; + } + } +
Quoted 8 lines
hw/host-pci-device.c line 68
Quoted 6 lines
hw/host-pci-device.c new file +6
-0+ fclose(f); + return rc; +} + +static int get_hex_value (HostPCIDevice *d,const char *name, + unsigned long *pvalue)why long?
Do be a bit generic I suppose, but I just use this function for vendor_id and device_id, I probably just need an int.
Quoted 25 lines
Quoted 25 lines
hw/host-pci-device.c new file +25
-0+{ + char path[PATH_MAX]; + FILE *f; + unsigned long value; + + path_to(d, name, path, sizeof (path)); + f = fopen(path, "r"); + if (!f) { + fprintf(stderr, "Error: Can't open %s: %s\n", path, strerror(errno)); + return -errno; + } + if (fscanf(f, "%lx\n", &value) != 1) { + fprintf(stderr, "Error: Syntax error in %s\n", path); + fclose(f); + return ERROR_SYNTAX; + } + fclose(f); + *pvalue = value; + return 0; +} + +static bool pci_dev_is_virtfn (HostPCIDevice *d) +{ + char path[PATH_MAX]; + struct stat buf;
Quoted 8 lines
hw/host-pci-device.c line 98
Quoted 5 lines
hw/host-pci-device.c new file +5
-0+ + path_to(d, "physfn", path, sizeof (path)); + return !stat(path, &buf); +} +Don't start names with
pci_.
It would also be better to avoid things likepath_toIMO.
Do you mean avoiding the name or the purpose of the function path_to ? For the name, I can probably rename it to sysfs_device_path()
Quoted 5 lines
hw/host-pci-device.c line 99
Quoted 1 line
hw/host-pci-device.c new file +1
-0+static int host_pci_config_fd (HostPCIDevice *d)So this opens if needed, and returns.
Why not explicitly open on get?
then you won't need these hacks.
Ok, I'll change that.
Quoted 29 lines
hw/host-pci-device.c line 104
Quoted 5 lines
hw/host-pci-device.c new file +5
-0+{ + char path[PATH_MAX]; + + if (d->config_fd < 0) { + path_to(d, "config", path, sizeof (path));sizeof path
hw/host-pci-device.c line 108
Quoted 4 lines
hw/host-pci-device.c new file +4
-0+ d->config_fd = open(path, O_RDWR); + if (d->config_fd < 0) { + fprintf(stderr, "HostPCIDevice: Can not open '%s': %s\n", + path, strerror(errno));strerror is not thread safe
hw/host-pci-device.c line 115
Quoted 7 lines
hw/host-pci-device.c new file +7
-0+ } + } + return d->config_fd; +} +static int host_pci_config_read (HostPCIDevice *d,int pos,void *buf,int len) +{ + int fd = host_pci_config_fd(d);You open file on each access?
hw/host-pci-device.c line 116
Quoted 1 line
hw/host-pci-device.c new file +1
-0+ int res = 0;why initialize here?
Quoted 8 lines
hw/host-pci-device.c line 122
Quoted 6 lines
hw/host-pci-device.c new file +6
-0+ +again: + res = pread(fd, buf, len, pos); + if (res != len) { + if (res < 0 && (errno == EINTR || errno == EAGAIN)) { + goto again;code loops with while or for.
ok.
Quoted 53 lines
Quoted 16 lines
hw/host-pci-device.c new file +16
-0+ } + fprintf(stderr, "%s: read failed: %s (fd: %i)\n", + __func__, strerror(errno), fd); + return -errno; + } + return 0; +} +static int host_pci_config_write (HostPCIDevice *d, + int pos, const void *buf, int len) +{ + int fd = host_pci_config_fd(d); + int res = 0; + +again: + res = pwrite(fd, buf, len, pos); + if (res != len) {hw/host-pci-device.c line 148
Quoted 10 lines
hw/host-pci-device.c new file +10
-0+ if (res < 0 && (errno == EINTR || errno == EAGAIN)) { + goto again; + } + fprintf(stderr, "%s: write failed: %s\n", + __func__, strerror(errno)); + return -errno; + } + return 0; +} +same comments as above. also,
Don't report errors with fprintf.hw/host-pci-device.c line 153
Quoted 5 lines
hw/host-pci-device.c new file +5
-0+int host_pci_get_byte (HostPCIDevice *d,int pos,uint8_t *p) +{ + uint8_t buf; + int rc = host_pci_config_read(d, pos, &buf, 1); + if (rc == 0) {!rc.
hw/host-pci-device.c line 154
Quoted 1 line
hw/host-pci-device.c new file +1
-0+ *p = buf;why not pass in p directly?
hw/host-pci-device.c line 162
Quoted 8 lines
hw/host-pci-device.c new file +8
-0+ } + return rc; +} +int host_pci_get_word (HostPCIDevice *d,int pos,uint16_t *p) +{ + uint16_t buf; + int rc = host_pci_config_read(d, pos, &buf, 2); + if (rc == 0) {!rc.
Quoted 6 lines
hw/host-pci-device.c line 166
Quoted 4 lines
hw/host-pci-device.c new file +4
-0+ *p = le16_to_cpu(buf); + } + return rc; +}This looks wrong wrt endian-ness.
It's seams that PCI config space registers are little-endian, so, get/read a word/dword from the pci config space should be converted from little-endian to the cpu endian-ness.
Quoted 11 lines
hw/host-pci-device.c line 175
Quoted 9 lines
hw/host-pci-device.c new file +9
-0+int host_pci_get_long (HostPCIDevice *d,int pos,uint32_t *p) +{ + uint32_t buf; + int rc = host_pci_config_read(d, pos, &buf, 4); + if (rc == 0) { + *p = le32_to_cpu(buf); + } + return rc; +}Add empty lines between {}
It's look nicer when I fold the function to only see one line :), but, I add this empty lines.
Quoted 6 lines
hw/host-pci-device.c line 179
Quoted 4 lines
hw/host-pci-device.c new file +4
-0+int host_pci_get_block (HostPCIDevice *d,int pos,uint8_t *buf,int len) +{ + return host_pci_config_read(d, pos, buf, len); +}when would this be useful?
It's used to initialize the "emulated" config space (of pci.h) and every time a pci config read or write is issued by the guest.
Quoted 15 lines
Quoted 15 lines
hw/host-pci-device.c new file +15
-0+ +int host_pci_set_byte (HostPCIDevice *d,int pos,uint8_t data) +{ + return host_pci_config_write(d, pos, &data, 1); +} +int host_pci_set_word (HostPCIDevice *d,int pos,uint16_t data) +{ + data = cpu_to_le16(data); + return host_pci_config_write(d, pos, &data, 2); +} +int host_pci_set_long (HostPCIDevice *d,int pos,uint32_t data) +{ + data = cpu_to_le32(data); + return host_pci_config_write(d, pos, &data, 4); +}
Quoted 8 lines
hw/host-pci-device.c line 200
Quoted 6 lines
hw/host-pci-device.c new file +6
-0+int host_pci_set_block (HostPCIDevice *d,int pos,uint8_t *buf,int len) +{ + return host_pci_config_write(d, pos, buf, len); +} + +uint32_t host_pci_find_ext_cap_offset (HostPCIDevice *d,uint32_t cap)Why 32? Ext config offsets are < 12 bit.
No apparent reason, the user of this function was just expecting a uint32.
Quoted 29 lines
Quoted 29 lines
hw/host-pci-device.c new file +29
-0+{ + uint32_t header = 0; + int max_cap = PCI_MAX_EXT_CAP; + int pos = PCI_CONFIG_SPACE_SIZE; + + do { + if (host_pci_get_long(d, pos, &header)) { + break; + } + /* + * If we have no capabilities, this is indicated by cap ID, + * cap version and next pointer all being 0. + */ + if (header == 0) { + break; + } + + if (PCI_EXT_CAP_ID(header) == cap) { + return pos; + } + + pos = PCI_EXT_CAP_NEXT(header); + if (pos < PCI_CONFIG_SPACE_SIZE) { + break; + } + + max_cap--; + } while (max_cap > 0); +
Quoted 8 lines
hw/host-pci-device.c line 233
Quoted 4 lines
hw/host-pci-device.c new file +4
-0+ return 0; +} + +HostPCIDevice *host_pci_device_get (uint8_t bus,uint8_t dev,uint8_t func)Why skip domain in the interface?
Also, HostPCIDevice structure is public so there is little value in allocating, just get it by pointer and init/cleanup.
You mean like pci_bus_new_inplace ? Ok, I'll do that.
Quoted 91 lines
Quoted 6 lines
hw/host-pci-device.c new file +6
-0+{ + HostPCIDevice *d = NULL; + unsigned long v = 0; + + d = g_new0(HostPCIDevice, 1); +hw/host-pci-device.c line 249
Quoted 10 lines
hw/host-pci-device.c new file +10
-0+ d->config_fd = -1; + d->domain = 0; + d->bus = bus; + d->dev = dev; + d->func = func; + + if (host_pci_config_fd(d) == -1) { + goto error; + } + if (get_resource(d) != 0) {just
get_resource(d).Quoted 35 lines
hw/host-pci-device.c new file +29
-0+ goto error; + } + + if (get_hex_value(d, "vendor", &v)) { + goto error; + } + d->vendor_id = v; + if (get_hex_value(d, "device", &v)) { + goto error; + } + d->device_id = v; + d->is_virtfn = pci_dev_is_virtfn(d); + + return d; +error: + if (d->config_fd >= 0) { + close(d->config_fd); + } + g_free(d); + return NULL; +} + +void host_pci_device_put (HostPCIDevice *d) +{ + if (d->config_fd >= 0) { + close(d->config_fd); + } + g_free(d); +}hw/host-pci-device.h new file +0
-0hw/host-pci-device.h line 2
Quoted 2 lines
hw/host-pci-device.h new file +2
-0@@ -0,0 +1,75 @@ +#ifndef HW_HOST_PCI_DEVICE +# define HW_HOST_PCI_DEVICEDon't put space after #.
Also
HOST_PCI_DEVICE_Hwould be less likely to confuse.Quoted 30 lines
hw/host-pci-device.h new file +30
-0+ +#include "pci.h" + +/* +* from linux/ioport.h +* IO resources have these defined flags. +*/ +#define IORESOURCE_BITS 0x000000ff/* Bus-specific bits */ + +#define IORESOURCE_TYPE_BITS 0x00000f00/* Resource type */ +#define IORESOURCE_IO 0x00000100 +#define IORESOURCE_MEM 0x00000200 +#define IORESOURCE_IRQ 0x00000400 +#define IORESOURCE_DMA 0x00000800 + +#define IORESOURCE_PREFETCH 0x00001000/* No side effects */ +#define IORESOURCE_READONLY 0x00002000 +#define IORESOURCE_CACHEABLE 0x00004000 +#define IORESOURCE_RANGELENGTH 0x00008000 +#define IORESOURCE_SHADOWABLE 0x00010000 + +#define IORESOURCE_SIZEALIGN 0x00020000/* size indicates alignment */ +#define IORESOURCE_STARTALIGN 0x00040000/* start field is alignment */ + +#define IORESOURCE_MEM_64 0x00100000 + + /* Userland may not map this resource */ +#define IORESOURCE_EXCLUSIVE 0x08000000 +#define IORESOURCE_DISABLED 0x10000000 +#define IORESOURCE_UNSET 0x20000000
Quoted 8 lines
hw/host-pci-device.h line 36
Quoted 4 lines
hw/host-pci-device.h new file +4
-0+#define IORESOURCE_AUTO 0x40000000 + /* Driver has marked this resource busy */ +#define IORESOURCE_BUSY 0x80000000 +Why do above make sense in an API?
Abstract it in some reasonable way, don't just expose flags from sysfs as is.
Ok.
Quoted 45 lines
hw/host-pci-device.h line 37
Quoted 1 line
hw/host-pci-device.h new file +1
-0+kill extra empty lines
Quoted 40 lines
hw/host-pci-device.h new file +38
-1+typedef struct HostPCIIORegion { + unsigned long flags; + pcibus_t base_addr; + pcibus_t size; +}HostPCIIORegion ; + +typedef struct HostPCIDevice { + uint16_t domain; + uint8_t bus; + uint8_t dev; + uint8_t func; + + uint16_t vendor_id; + uint16_t device_id; + + HostPCIIORegion io_regions[PCI_NUM_REGIONS - 1]; + HostPCIIORegion rom; + + bool is_virtfn; + + int config_fd; +}HostPCIDevice ; + +HostPCIDevice *host_pci_device_get (uint8_t bus,uint8_t dev,uint8_t func); +void host_pci_device_put (HostPCIDevice *pci_dev); + +int host_pci_get_byte (HostPCIDevice *d,int pos,uint8_t *p); +int host_pci_get_word (HostPCIDevice *d,int pos,uint16_t *p); +int host_pci_get_long (HostPCIDevice *d,int pos,uint32_t *p); +int host_pci_get_block (HostPCIDevice *d,int pos,uint8_t *buf,int len); +int host_pci_set_byte (HostPCIDevice *d,int pos,uint8_t data); +int host_pci_set_word (HostPCIDevice *d,int pos,uint16_t data); +int host_pci_set_long (HostPCIDevice *d,int pos,uint32_t data); +int host_pci_set_block (HostPCIDevice *d,int pos,uint8_t *buf,int len); + +uint32_t host_pci_find_ext_cap_offset (HostPCIDevice *s,uint32_t cap); + +#endif /* !HW_HOST_PCI_DEVICE */ --Anthony PERARD
Signature · 1 line
Anthony PERARD