diff mbox

[v2] device-assignment: Rework "name" of assigned pci device

Message ID 4C29B080.6030701@jp.fujitsu.com (mailing list archive)
State New, archived
Headers show

Commit Message

Hidetoshi Seto June 29, 2010, 8:36 a.m. UTC
None
diff mbox

Patch

diff --git a/hw/device-assignment.c b/hw/device-assignment.c
index 585162b..cda6ee3 100644
--- a/hw/device-assignment.c
+++ b/hw/device-assignment.c
@@ -62,6 +62,25 @@  static void assigned_dev_load_option_rom(AssignedDevice *dev);
 
 static void assigned_dev_unregister_msix_mmio(AssignedDevice *dev);
 
+static const char *assigned_dev_name(AssignedDevice *dev)
+{
+    /* use user-defined "name" if specified */
+    if (dev->u_name)
+        return dev->u_name;
+    /* else use "id" if available */
+    if (dev->dev.qdev.id)
+        return dev->dev.qdev.id;
+    /* otherwise use address of host device instead */
+    if (!dev->r_name) {
+        char buf[32];
+
+        snprintf(buf, sizeof(buf), "%04x:%02x:%02x.%01x",
+                 dev->host.seg, dev->host.bus, dev->host.dev, dev->host.func);
+        dev->r_name = qemu_strdup(buf);
+    }
+    return dev->r_name;
+}
+
 static uint32_t guest_to_host_ioport(AssignedDevRegion *region, uint32_t addr)
 {
     return region->u.r_baseport + (addr - region->e_physbase);
@@ -798,6 +817,10 @@  static void free_assigned_device(AssignedDevice *dev)
             dev->real_device.config_fd = 0;
         }
 
+    if (dev->r_name) {
+        qemu_free(dev->r_name);
+    }
+
 #ifdef KVM_CAP_IRQ_ROUTING
         free_dev_irq_entries(dev);
 #endif
@@ -885,7 +908,7 @@  static int assign_device(AssignedDevice *dev)
     if (dev->use_iommu) {
         if (!kvm_check_extension(kvm_state, KVM_CAP_IOMMU)) {
             fprintf(stderr, "No IOMMU found.  Unable to assign device \"%s\"\n",
-                    dev->dev.qdev.id);
+                    assigned_dev_name(dev));
             return -ENODEV;
         }
         assigned_dev_data.flags |= KVM_DEV_ASSIGN_ENABLE_IOMMU;
@@ -897,7 +920,7 @@  static int assign_device(AssignedDevice *dev)
     r = kvm_assign_pci_device(kvm_context, &assigned_dev_data);
     if (r < 0) {
         fprintf(stderr, "Failed to assign device \"%s\" : %s\n",
-                dev->dev.qdev.id, strerror(-r));
+                assigned_dev_name(dev), strerror(-r));
 
         switch (r) {
             case -EBUSY:
@@ -953,7 +976,7 @@  static int assign_irq(AssignedDevice *dev)
     r = kvm_assign_irq(kvm_context, &assigned_irq_data);
     if (r < 0) {
         fprintf(stderr, "Failed to assign irq for \"%s\": %s\n",
-                dev->dev.qdev.id, strerror(-r));
+                assigned_dev_name(dev), strerror(-r));
         fprintf(stderr, "Perhaps you are assigning a device "
                 "that shares an IRQ with another device?\n");
         return r;
@@ -977,7 +1000,7 @@  static void deassign_device(AssignedDevice *dev)
     r = kvm_deassign_pci_device(kvm_context, &assigned_dev_data);
     if (r < 0)
 	fprintf(stderr, "Failed to deassign device \"%s\" : %s\n",
-                dev->dev.qdev.id, strerror(-r));
+                assigned_dev_name(dev), strerror(-r));
 #endif
 }
 
@@ -1421,7 +1444,7 @@  static int assigned_initfn(struct PCIDevice *pci_dev)
     if (get_real_device(dev, dev->host.seg, dev->host.bus,
                         dev->host.dev, dev->host.func)) {
         error_report("pci-assign: Error: Couldn't get real device (%s)!",
-                     dev->dev.qdev.id);
+                     assigned_dev_name(dev));
         goto out;
     }
 
@@ -1520,6 +1543,7 @@  static PCIDeviceInfo assign_info = {
         DEFINE_PROP("host", AssignedDevice, host, qdev_prop_hostaddr, PCIHostDevice),
         DEFINE_PROP_UINT32("iommu", AssignedDevice, use_iommu, 1),
         DEFINE_PROP_STRING("configfd", AssignedDevice, configfd_name),
+        DEFINE_PROP_STRING("name", AssignedDevice, u_name),
         DEFINE_PROP_END_OF_LIST(),
     },
 };
@@ -1545,24 +1569,28 @@  device_init(assign_register_devices)
 QemuOpts *add_assigned_device(const char *arg)
 {
     QemuOpts *opts = NULL;
-    char host[64], id[64], dma[8];
+    char host[64], buf[64], dma[8], *id = NULL;
     int r;
 
+    /* "host" is mandatory */
     r = get_param_value(host, sizeof(host), "host", arg);
     if (!r)
          goto bad;
-    r = get_param_value(id, sizeof(id), "id", arg);
-    if (!r)
-        r = get_param_value(id, sizeof(id), "name", arg);
-    if (!r)
-        r = get_param_value(id, sizeof(id), "host", arg);
 
+    r = get_param_value(buf, sizeof(buf), "id", arg);
+    if (r)
+         id = buf;
     opts = qemu_opts_create(&qemu_device_opts, id, 0);
     if (!opts)
         goto bad;
     qemu_opt_set(opts, "driver", "pci-assign");
     qemu_opt_set(opts, "host", host);
 
+    /* Log outputs use "name" if specified */
+    r = get_param_value(buf, sizeof(buf), "name", arg);
+    if (r)
+        qemu_opt_set(opts, "name", buf);
+
 #ifdef KVM_CAP_IOMMU
     r = get_param_value(dma, sizeof(dma), "dma", arg);
     if (r && !strncmp(dma, "none", 4))
@@ -1574,8 +1602,6 @@  QemuOpts *add_assigned_device(const char *arg)
 bad:
     fprintf(stderr, "pcidevice argument parse error; "
             "please check the help text for usage\n");
-    if (opts)
-        qemu_opts_del(opts);
     return NULL;
 }
 
diff --git a/hw/device-assignment.h b/hw/device-assignment.h
index 4e7fe87..fb00e29 100644
--- a/hw/device-assignment.h
+++ b/hw/device-assignment.h
@@ -86,6 +86,8 @@  typedef struct AssignedDevice {
     unsigned int h_segnr;
     unsigned char h_busnr;
     unsigned int h_devfn;
+    char *u_name;
+    char *r_name;
     int irq_requested_type;
     int bound;
     struct {