From patchwork Wed Dec 9 20:13:42 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Graf X-Patchwork-Id: 66073 Received: from vger.kernel.org (vger.kernel.org [209.132.176.167]) by demeter.kernel.org (8.14.2/8.14.2) with ESMTP id nB9KDk8G032750 for ; Wed, 9 Dec 2009 20:13:46 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757629AbZLIUNh (ORCPT ); Wed, 9 Dec 2009 15:13:37 -0500 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1757627AbZLIUNh (ORCPT ); Wed, 9 Dec 2009 15:13:37 -0500 Received: from cantor2.suse.de ([195.135.220.15]:43705 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1757618AbZLIUNg (ORCPT ); Wed, 9 Dec 2009 15:13:36 -0500 Received: from relay2.suse.de (charybdis-ext.suse.de [195.135.221.2]) by mx2.suse.de (Postfix) with ESMTP id 1461F5FC9F; Wed, 9 Dec 2009 21:13:43 +0100 (CET) From: Alexander Graf To: kvm@vger.kernel.org Cc: "Daniel P. Berrange" Subject: [PATCH] Inform users about busy device assignment attempt Date: Wed, 9 Dec 2009 21:13:42 +0100 Message-Id: <1260389622-11499-1-git-send-email-agraf@suse.de> X-Mailer: git-send-email 1.6.0.2 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org diff --git a/hw/device-assignment.c b/hw/device-assignment.c index beb488c..9fbddcf 100644 --- a/hw/device-assignment.c +++ b/hw/device-assignment.c @@ -572,6 +572,36 @@ static int assigned_dev_register_regions(PCIRegion *io_regions, return 0; } +static int get_real_id(const char *devpath, const char *idname, uint16_t *val) +{ + FILE *f; + char name[128]; + long id; + + snprintf(name, sizeof(name), "%s%s", devpath, idname); + f = fopen(name, "r"); + if (f == NULL) { + fprintf(stderr, "%s: %s: %m\n", __func__, name); + return -1; + } + if (fscanf(f, "%li\n", &id) == 1) { + *val = id; + } + fclose(f); + + return 0; +} + +static int get_real_vendor_id(const char *devpath, uint16_t *val) +{ + return get_real_id(devpath, "vendor", val); +} + +static int get_real_device_id(const char *devpath, uint16_t *val) +{ + return get_real_id(devpath, "device", val); +} + static int get_real_device(AssignedDevice *pci_dev, uint8_t r_bus, uint8_t r_dev, uint8_t r_func) { @@ -579,7 +609,7 @@ static int get_real_device(AssignedDevice *pci_dev, uint8_t r_bus, int fd, r = 0; FILE *f; unsigned long long start, end, size, flags; - unsigned long id; + uint16_t id; struct stat statbuf; PCIRegion *rp; PCIDevRegions *dev = &pci_dev->real_device; @@ -645,31 +675,21 @@ again: fclose(f); - /* read and fill device ID */ - snprintf(name, sizeof(name), "%svendor", dir); - f = fopen(name, "r"); - if (f == NULL) { - fprintf(stderr, "%s: %s: %m\n", __func__, name); + /* read and fill vendor ID */ + r = get_real_vendor_id(dir, &id); + if (r) { return 1; } - if (fscanf(f, "%li\n", &id) == 1) { - pci_dev->dev.config[0] = id & 0xff; - pci_dev->dev.config[1] = (id & 0xff00) >> 8; - } - fclose(f); + pci_dev->dev.config[0] = id & 0xff; + pci_dev->dev.config[1] = (id & 0xff00) >> 8; - /* read and fill vendor ID */ - snprintf(name, sizeof(name), "%sdevice", dir); - f = fopen(name, "r"); - if (f == NULL) { - fprintf(stderr, "%s: %s: %m\n", __func__, name); + /* read and fill device ID */ + r = get_real_device_id(dir, &id); + if (r) { return 1; } - if (fscanf(f, "%li\n", &id) == 1) { - pci_dev->dev.config[2] = id & 0xff; - pci_dev->dev.config[3] = (id & 0xff00) >> 8; - } - fclose(f); + pci_dev->dev.config[2] = id & 0xff; + pci_dev->dev.config[3] = (id & 0xff00) >> 8; /* dealing with virtual function device */ snprintf(name, sizeof(name), "%sphysfn/", dir); @@ -747,7 +767,9 @@ static uint32_t calc_assigned_dev_id(uint8_t bus, uint8_t devfn) static int assign_device(AssignedDevice *dev) { struct kvm_assigned_pci_dev assigned_dev_data; - int r; + char name[128], dir[128], driver[128], *ns; + uint16_t vendor_id, device_id; + int r, v; memset(&assigned_dev_data, 0, sizeof(assigned_dev_data)); assigned_dev_data.assigned_dev_id = @@ -769,9 +791,47 @@ static int assign_device(AssignedDevice *dev) #endif r = kvm_assign_pci_device(kvm_context, &assigned_dev_data); - if (r < 0) + if (r < 0) { fprintf(stderr, "Failed to assign device \"%s\" : %s\n", dev->dev.qdev.id, strerror(-r)); + + snprintf(dir, sizeof(dir), + "/sys/bus/pci/devices/0000:%02x:%02x.%x/", + dev->host.bus, dev->host.dev, dev->host.func); + + snprintf(name, sizeof(name), "%sdriver", dir); + + v = readlink(name, driver, sizeof(driver)); + if ((v <= 0) || !(ns = strrchr(driver, '/'))) { + return r; + } + + ns++; + + if (get_real_vendor_id(dir, &vendor_id) || + get_real_device_id(dir, &device_id)) { + return r; + } + + fprintf(stderr, "*** The driver '%s' is occupying your device " + "%02x:%02x.%x.\n", + ns, dev->host.bus, dev->host.dev, dev->host.func); + fprintf(stderr, "***\n"); + fprintf(stderr, "*** You can try the following commands to free " + "it:\n"); + fprintf(stderr, "***\n"); + fprintf(stderr, "*** $ echo \"%04x %04x\" > /sys/bus/pci/drivers/" + "pci-stub/new_id\n", vendor_id, device_id); + fprintf(stderr, "*** $ echo \"%02x:%02x.%x\" > /sys/bus/pci/drivers/%s" + "/unbind\n", dev->host.bus, dev->host.dev, + dev->host.func, ns); + fprintf(stderr, "*** $ echo \"%02x:%02x.%x\" > /sys/bus/pci/drivers/" + "pci-stub/bind\n", dev->host.bus, dev->host.dev, + dev->host.func); + fprintf(stderr, "*** $ echo \"%x %x\" > /sys/bus/pci/drivers/pci-stub" + "/remove_id\n", vendor_id, device_id); + fprintf(stderr, "***\n"); + } return r; }