[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] Re: [Xen-devel] [Qemu-devel] [PATCH V9 3/8] Introduce HostPCIDevice to access a pci device on the host.
On Wed, Mar 21, 2012 at 20:30, Michael S. Tsirkin <mst@xxxxxxxxxx> wrote: > On Wed, Mar 21, 2012 at 06:29:00PM +0000, Anthony PERARD wrote: >> Signed-off-by: Anthony PERARD <anthony.perard@xxxxxxxxxx> >> Acked-by: Stefano Stabellini <stefano.stabellini@xxxxxxxxxxxxx> > > 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. >> --- >> Â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 >> >> diff --git a/Makefile.target b/Makefile.target >> index 63cf769..0ccfd5b 100644 >> --- a/Makefile.target >> +++ b/Makefile.target >> @@ -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) >> diff --git a/hw/host-pci-device.c b/hw/host-pci-device.c >> new file mode 100644 >> index 0000000..3dacb30 >> --- /dev/null >> +++ b/hw/host-pci-device.c >> @@ -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)) > > namespace pollution. > name all things HOST_PCI_.... > > in this case, open-coding will make things clearer. > > >> + >> +enum error_code { > > seems unused. So why name the type? > >> +  ÂERROR_SYNTAX = 1, > > We return -1 on error, just do that and you won't need ERROR_SYNTAX. Ok, I'll remove this. >> +}; >> + >> +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. >> + >> +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. >> +  Â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 :(. >> +      Â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) > > 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. >> +{ >> +  Â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); >> +} >> + > > Don't start names with pci_. > It would also be better to avoid things like path_to IMO. 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() >> +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. >> +{ >> +  Âchar path[PATH_MAX]; >> + >> +  Âif (d->config_fd < 0) { >> +    Âpath_to(d, "config", path, sizeof (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)); > > strerror is not thread safe > >> +    Â} >> +  Â} >> +  Â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? > >> +  Âint res = 0; > > why initialize here? > >> + >> +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. >> +    Â} >> +    Â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; >> +} >> + > > same comments as above. also, > Don't report errors with fprintf. > >> +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. > >> +    Â*p = buf; > > why not pass in p directly? > >> +  Â} >> +  Â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. > >> +    Â*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. >> +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. >> +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. >> + >> +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) > > Why 32? Ext config offsets are < 12 bit. No apparent reason, the user of this function was just expecting a uint32. >> +{ >> +  Â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) > > 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. >> +{ >> +  Â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) { > > just get_resource(d). > >> +    Â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); >> +} >> diff --git a/hw/host-pci-device.h b/hw/host-pci-device.h >> new file mode 100644 >> index 0000000..c8880eb >> --- /dev/null >> +++ b/hw/host-pci-device.h >> @@ -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. > >> + >> +#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 >> + > > Why do above make sense in an API? > Abstract it in some reasonable way, don't just expose > flags from sysfs as is. Ok. >> + > > kill extra empty lines > >> +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 > -- Anthony PERARD _______________________________________________ Xen-devel mailing list Xen-devel@xxxxxxxxxxxxx http://lists.xen.org/xen-devel
|
Lists.xenproject.org is hosted with RackSpace, monitoring our |