From patchwork Sat Aug 9 14:25:44 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Auger Eric X-Patchwork-Id: 35187 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-yh0-f70.google.com (mail-yh0-f70.google.com [209.85.213.70]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 8E8CA21F5F for ; Sat, 9 Aug 2014 14:26:19 +0000 (UTC) Received: by mail-yh0-f70.google.com with SMTP id b6sf22052760yha.9 for ; Sat, 09 Aug 2014 07:26:19 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:in-reply-to:references:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :list-post:list-help:list-archive:list-unsubscribe; bh=a68p0zBsB+7UmKQOf8UuqE38r8Q0t1+/kC7WJIbVQDk=; b=SFsfe9H3ukm/ASECaPeUgD/1YaYNk4o1m7rEZVDLOD5rlkJa0iPRXzQVW/5ifjxlKN 31Rkz4+GWoKP4jGYexb82WFrowBSC3ScMPT5vWO5I2Uu/cggn3vd6/kVrIh3eSB9OEDt Fy9CNF7g4Ok+7gtoZAO0KZ5HGNNB89GEnfy2i+vY7KoapKfZhira8kL5NfE6sHDB/LCz KJ7dIzEQ9ACKI97ECUIjRWhVllckayweDQT9dlKH84Kkt/s8RSVW55f7rlCJbkqKi4et HOcO/7zoEfubsFcAexgA1kaMe4Qtm39G0gdRhMds3ZBwBH4/3sxu7hIWZVN4qPSmPP3w 6KzA== X-Gm-Message-State: ALoCoQnO5frw+yMKYwmvm3FZsIPrkYWWiifB5kN1jjfeJaQJ5R+4falHFnj9xzu/TjtIIBzxnLIA X-Received: by 10.236.105.197 with SMTP id k45mr8094571yhg.19.1407594379376; Sat, 09 Aug 2014 07:26:19 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.88.6 with SMTP id s6ls850887qgd.22.gmail; Sat, 09 Aug 2014 07:26:19 -0700 (PDT) X-Received: by 10.220.172.8 with SMTP id j8mr27220684vcz.32.1407594379302; Sat, 09 Aug 2014 07:26:19 -0700 (PDT) Received: from mail-vc0-f173.google.com (mail-vc0-f173.google.com [209.85.220.173]) by mx.google.com with ESMTPS id tg8si3898872vcb.64.2014.08.09.07.26.19 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Sat, 09 Aug 2014 07:26:19 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.173 as permitted sender) client-ip=209.85.220.173; Received: by mail-vc0-f173.google.com with SMTP id hy10so9752434vcb.32 for ; Sat, 09 Aug 2014 07:26:19 -0700 (PDT) X-Received: by 10.52.232.200 with SMTP id tq8mr11295721vdc.32.1407594379224; Sat, 09 Aug 2014 07:26:19 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patches@linaro.org Received: by 10.221.37.5 with SMTP id tc5csp34384vcb; Sat, 9 Aug 2014 07:26:18 -0700 (PDT) X-Received: by 10.180.86.202 with SMTP id r10mr7016048wiz.6.1407594377782; Sat, 09 Aug 2014 07:26:17 -0700 (PDT) Received: from mail-we0-f173.google.com (mail-we0-f173.google.com [74.125.82.173]) by mx.google.com with ESMTPS id lv10si16349555wjb.90.2014.08.09.07.26.17 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Sat, 09 Aug 2014 07:26:17 -0700 (PDT) Received-SPF: pass (google.com: domain of eric.auger@linaro.org designates 74.125.82.173 as permitted sender) client-ip=74.125.82.173; Received: by mail-we0-f173.google.com with SMTP id q58so6866427wes.4 for ; Sat, 09 Aug 2014 07:26:17 -0700 (PDT) X-Received: by 10.180.149.163 with SMTP id ub3mr11277851wib.67.1407594377151; Sat, 09 Aug 2014 07:26:17 -0700 (PDT) Received: from midway01-04-00.lavalab ([88.98.47.97]) by mx.google.com with ESMTPSA id f3sm19591736wiz.0.2014.08.09.07.26.16 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Sat, 09 Aug 2014 07:26:16 -0700 (PDT) From: Eric Auger To: eric.auger@st.com, christoffer.dall@linaro.org, qemu-devel@nongnu.org, kim.phillips@freescale.com, a.rigo@virtualopensystems.com Cc: eric.auger@linaro.org, will.deacon@arm.com, kvmarm@lists.cs.columbia.edu, alex.williamson@redhat.com, Bharat.Bhushan@freescale.com, agraf@suse.de, peter.maydell@linaro.org, stuart.yoder@freescale.com, a.motakis@virtualopensystems.com, patches@linaro.org, joel.schopp@amd.com Subject: [PATCH v5 05/10] hw/vfio/pci: split vfio_get_device Date: Sat, 9 Aug 2014 15:25:44 +0100 Message-Id: <1407594349-9291-6-git-send-email-eric.auger@linaro.org> X-Mailer: git-send-email 1.8.3.2 In-Reply-To: <1407594349-9291-1-git-send-email-eric.auger@linaro.org> References: <1407594349-9291-1-git-send-email-eric.auger@linaro.org> X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: eric.auger@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.173 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Precedence: list Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org List-ID: X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , vfio_get_device now takes a VFIODevice as argument. The function is split into 4 functional parts: dev_info query, device check, region populate and interrupt populate. the last 3 are specialized by parent device and are added into DeviceOps. 3 new fields are introduced in VFIODevice to store dev_info. vfio_put_base_device is created. --- v4->v5: - cleanup up of error handling and get/put operations in vfio_check_device, vfio_populate_regions, vfio_populate_interrupts and vfio_get_device. - correct misuse of errno - vfio_populate_regions always returns 0 - VFIODevice .name deallocation done in vfio_put_device instead of vfio_put_base_device - vfio_put_base_device done at vfio_get_device level. Signed-off-by: Eric Auger --- hw/vfio/pci.c | 181 ++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 120 insertions(+), 61 deletions(-) diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index 1a24398..5f218b7 100644 --- a/hw/vfio/pci.c +++ b/hw/vfio/pci.c @@ -213,12 +213,18 @@ typedef struct VFIODevice { bool reset_works; bool needs_reset; VFIODeviceOps *ops; + unsigned int num_irqs; + unsigned int num_regions; + unsigned int flags; } VFIODevice; struct VFIODeviceOps { bool (*vfio_compute_needs_reset)(VFIODevice *vdev); int (*vfio_hot_reset_multi)(VFIODevice *vdev); void (*vfio_eoi)(VFIODevice *vdev); + int (*vfio_check_device)(VFIODevice *vdev); + int (*vfio_populate_regions)(VFIODevice *vdev); + int (*vfio_populate_interrupts)(VFIODevice *vdev); }; typedef struct VFIOPCIDevice { @@ -305,6 +311,10 @@ static uint32_t vfio_pci_read_config(PCIDevice *pdev, uint32_t addr, int len); static void vfio_pci_write_config(PCIDevice *pdev, uint32_t addr, uint32_t val, int len); static void vfio_mmap_set_enabled(VFIOPCIDevice *vdev, bool enabled); +static void vfio_put_base_device(VFIODevice *vbasedev); +static int vfio_check_device(VFIODevice *vbasedev); +static int vfio_populate_regions(VFIODevice *vbasedev); +static int vfio_populate_interrupts(VFIODevice *vbasedev); /* * Common VFIO interrupt disable @@ -3608,6 +3618,9 @@ static VFIODeviceOps vfio_pci_ops = { .vfio_compute_needs_reset = vfio_pci_compute_needs_reset, .vfio_hot_reset_multi = vfio_pci_hot_reset_multi, .vfio_eoi = vfio_eoi, + .vfio_check_device = vfio_check_device, + .vfio_populate_regions = vfio_populate_regions, + .vfio_populate_interrupts = vfio_populate_interrupts, }; static void vfio_reset_handler(void *opaque) @@ -3949,54 +3962,52 @@ static void vfio_put_group(VFIOGroup *group) } } -static int vfio_get_device(VFIOGroup *group, const char *name, - VFIOPCIDevice *vdev) +static int vfio_check_device(VFIODevice *vbasedev) { - struct vfio_device_info dev_info = { .argsz = sizeof(dev_info) }; - struct vfio_region_info reg_info = { .argsz = sizeof(reg_info) }; - struct vfio_irq_info irq_info = { .argsz = sizeof(irq_info) }; - int ret, i; - - ret = ioctl(group->fd, VFIO_GROUP_GET_DEVICE_FD, name); - if (ret < 0) { - error_report("vfio: error getting device %s from group %d: %m", - name, group->groupid); - error_printf("Verify all devices in group %d are bound to vfio-pci " - "or pci-stub and not already in use\n", group->groupid); - return ret; + if (!(vbasedev->flags & VFIO_DEVICE_FLAGS_PCI)) { + error_report("vfio: Um, this isn't a PCI device"); + goto error; } - - vdev->vbasedev.fd = ret; - vdev->vbasedev.group = group; - QLIST_INSERT_HEAD(&group->device_list, &vdev->vbasedev, next); - - /* Sanity check device */ - ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_GET_INFO, &dev_info); - if (ret) { - error_report("vfio: error getting device info: %m"); + if (vbasedev->num_regions < VFIO_PCI_CONFIG_REGION_INDEX + 1) { + error_report("vfio: unexpected number of io regions %u", + vbasedev->num_regions); goto error; } - - DPRINTF("Device %s flags: %u, regions: %u, irgs: %u\n", name, - dev_info.flags, dev_info.num_regions, dev_info.num_irqs); - - if (!(dev_info.flags & VFIO_DEVICE_FLAGS_PCI)) { - error_report("vfio: Um, this isn't a PCI device"); + if (vbasedev->num_irqs < VFIO_PCI_MSIX_IRQ_INDEX + 1) { + error_report("vfio: unexpected number of irqs %u", + vbasedev->num_irqs); goto error; } + return 0; +error: + return -1; +} - vdev->vbasedev.reset_works = !!(dev_info.flags & VFIO_DEVICE_FLAGS_RESET); +static int vfio_populate_interrupts(VFIODevice *vbasedev) +{ + VFIOPCIDevice *vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev); + int ret; + struct vfio_irq_info irq_info = { .argsz = sizeof(irq_info) }; + irq_info.index = VFIO_PCI_ERR_IRQ_INDEX; - if (dev_info.num_regions < VFIO_PCI_CONFIG_REGION_INDEX + 1) { - error_report("vfio: unexpected number of io regions %u", - dev_info.num_regions); - goto error; + ret = ioctl(vbasedev->fd, VFIO_DEVICE_GET_IRQ_INFO, &irq_info); + if (ret) { + /* This can fail for an old kernel or legacy PCI dev */ + DPRINTF("VFIO_DEVICE_GET_IRQ_INFO failure: %m\n"); + } else if (irq_info.count == 1) { + vdev->pci_aer = true; + } else { + error_report("vfio: %s Could not enable error recovery for the device", + vbasedev->name); } + return 0; +} - if (dev_info.num_irqs < VFIO_PCI_MSIX_IRQ_INDEX + 1) { - error_report("vfio: unexpected number of irqs %u", dev_info.num_irqs); - goto error; - } +static int vfio_populate_regions(VFIODevice *vbasedev) +{ + struct vfio_region_info reg_info = { .argsz = sizeof(reg_info) }; + int i, ret = 0; + VFIOPCIDevice *vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev); for (i = VFIO_PCI_BAR0_REGION_INDEX; i < VFIO_PCI_ROM_REGION_INDEX; i++) { reg_info.index = i; @@ -4007,7 +4018,7 @@ static int vfio_get_device(VFIOGroup *group, const char *name, goto error; } - DPRINTF("Device %s region %d:\n", name, i); + DPRINTF("Device %s region %d:\n", vbasedev->name, i); DPRINTF(" size: 0x%lx, offset: 0x%lx, flags: 0x%lx\n", (unsigned long)reg_info.size, (unsigned long)reg_info.offset, (unsigned long)reg_info.flags); @@ -4028,7 +4039,7 @@ static int vfio_get_device(VFIOGroup *group, const char *name, goto error; } - DPRINTF("Device %s config:\n", name); + DPRINTF("Device %s config:\n", vbasedev->name); DPRINTF(" size: 0x%lx, offset: 0x%lx, flags: 0x%lx\n", (unsigned long)reg_info.size, (unsigned long)reg_info.offset, (unsigned long)reg_info.flags); @@ -4040,7 +4051,7 @@ static int vfio_get_device(VFIOGroup *group, const char *name, vdev->config_offset = reg_info.offset; if ((vdev->features & VFIO_FEATURE_ENABLE_VGA) && - dev_info.num_regions > VFIO_PCI_VGA_REGION_INDEX) { + vbasedev->num_regions > VFIO_PCI_VGA_REGION_INDEX) { struct vfio_region_info vga_info = { .argsz = sizeof(vga_info), .index = VFIO_PCI_VGA_REGION_INDEX, @@ -4059,6 +4070,7 @@ static int vfio_get_device(VFIOGroup *group, const char *name, error_report("vfio: Unexpected VGA info, flags 0x%lx, size 0x%lx", (unsigned long)vga_info.flags, (unsigned long)vga_info.size); + ret = -1; goto error; } @@ -4079,42 +4091,89 @@ static int vfio_get_device(VFIOGroup *group, const char *name, vdev->has_vga = true; } - irq_info.index = VFIO_PCI_ERR_IRQ_INDEX; +error: + return ret; +} + +static int vfio_get_device(VFIOGroup *group, const char *name, + VFIODevice *vbasedev) +{ + struct vfio_device_info dev_info = { .argsz = sizeof(dev_info) }; + struct vfio_region_info reg_info = { .argsz = sizeof(reg_info) }; + struct vfio_irq_info irq_info = { .argsz = sizeof(irq_info) }; + int ret; - ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_GET_IRQ_INFO, &irq_info); + ret = ioctl(group->fd, VFIO_GROUP_GET_DEVICE_FD, name); + if (ret < 0) { + error_report("vfio: error getting device %s from group %d: %m", + name, group->groupid); + error_printf("Verify all devices in group %d are bound to vfio-pci " + "or pci-stub and not already in use\n", group->groupid); + return ret; + } + + vbasedev->fd = ret; + vbasedev->group = group; + QLIST_INSERT_HEAD(&group->device_list, vbasedev, next); + + ret = ioctl(vbasedev->fd, VFIO_DEVICE_GET_INFO, &dev_info); if (ret) { - /* This can fail for an old kernel or legacy PCI dev */ - DPRINTF("VFIO_DEVICE_GET_IRQ_INFO failure: %m\n"); - ret = 0; - } else if (irq_info.count == 1) { - vdev->pci_aer = true; - } else { - error_report("vfio: %04x:%02x:%02x.%x " - "Could not enable error recovery for the device", - vdev->host.domain, vdev->host.bus, vdev->host.slot, - vdev->host.function); + error_report("vfio: error getting device info: %m"); + goto error; + } + + vbasedev->num_irqs = dev_info.num_irqs; + vbasedev->num_regions = dev_info.num_regions; + vbasedev->flags = dev_info.flags; + + DPRINTF("Device %s flags: %u, regions: %u, irgs: %u\n", name, + dev_info.flags, dev_info.num_regions, dev_info.num_irqs); + + vbasedev->reset_works = !!(dev_info.flags & VFIO_DEVICE_FLAGS_RESET); + + /* call device specific functions */ + ret = vbasedev->ops->vfio_check_device(vbasedev); + if (ret) { + error_report("vfio: error when checking device %s\n", + vbasedev->name); + goto error; + } + ret = vbasedev->ops->vfio_populate_regions(vbasedev); + if (ret) { + error_report("vfio: error when populating regions of device %s\n", + vbasedev->name); + goto error; + } + ret = vbasedev->ops->vfio_populate_interrupts(vbasedev); + if (ret) { + error_report("vfio: error when populating interrupts of device %s\n", + vbasedev->name); + goto error; } error: if (ret) { - QLIST_REMOVE(&vdev->vbasedev, next); - vdev->vbasedev.group = NULL; - close(vdev->vbasedev.fd); + vfio_put_base_device(vbasedev); } return ret; } +void vfio_put_base_device(VFIODevice *vbasedev) +{ + QLIST_REMOVE(vbasedev, next); + vbasedev->group = NULL; + DPRINTF("vfio_put_base_device: close vdev->fd\n"); + close(vbasedev->fd); +} + static void vfio_put_device(VFIOPCIDevice *vdev) { - QLIST_REMOVE(&vdev->vbasedev, next); - vdev->vbasedev.group = NULL; - DPRINTF("vfio_put_device: close vdev->fd\n"); - close(vdev->vbasedev.fd); g_free(vdev->vbasedev.name); if (vdev->msix) { g_free(vdev->msix); vdev->msix = NULL; } + vfio_put_base_device(&vdev->vbasedev); } static void vfio_err_notifier_handler(void *opaque) @@ -4287,7 +4346,7 @@ static int vfio_initfn(PCIDevice *pdev) } } - ret = vfio_get_device(group, path, vdev); + ret = vfio_get_device(group, path, &vdev->vbasedev); if (ret) { error_report("vfio: failed to get device %s", path); vfio_put_group(group);