vmstate: port cirrus_vga device
[qemu] / hw / pci-hotplug.c
index acda2bb..5348dd1 100644 (file)
 #include "pc.h"
 #include "monitor.h"
 #include "block_int.h"
+#include "scsi-disk.h"
 #include "virtio-blk.h"
 
 #if defined(TARGET_I386) || defined(TARGET_X86_64)
-static PCIDevice *qemu_pci_hot_add_nic(PCIBus *pci_bus, const char *opts)
+static PCIDevice *qemu_pci_hot_add_nic(Monitor *mon,
+                                       const char *devaddr, const char *opts)
 {
     int ret;
 
-    ret = net_client_init("nic", opts);
+    ret = net_client_init(mon, "nic", opts);
     if (ret < 0)
         return NULL;
-    return pci_nic_init(pci_bus, &nd_table[ret], -1, "rtl8139");
+    if (nd_table[ret].devaddr) {
+        monitor_printf(mon, "Parameter addr not supported\n");
+        return NULL;
+    }
+    return pci_nic_init(&nd_table[ret], "rtl8139", devaddr);
 }
 
-void drive_hot_add(Monitor *mon, const char *pci_addr, const char *opts)
+void drive_hot_add(Monitor *mon, const QDict *qdict)
 {
     int dom, pci_bus;
     unsigned slot;
-    int drive_idx, type, bus;
+    int type, bus;
     int success = 0;
     PCIDevice *dev;
+    DriveInfo *dinfo;
+    const char *pci_addr = qdict_get_str(qdict, "pci_addr");
+    const char *opts = qdict_get_str(qdict, "opts");
+    BusState *scsibus;
 
-    if (pci_read_devaddr(pci_addr, &dom, &pci_bus, &slot)) {
-        monitor_printf(mon, "Invalid pci address\n");
+    if (pci_read_devaddr(mon, pci_addr, &dom, &pci_bus, &slot)) {
         return;
     }
 
@@ -62,17 +71,22 @@ void drive_hot_add(Monitor *mon, const char *pci_addr, const char *opts)
         return;
     }
 
-    drive_idx = add_init_drive(opts);
-    if (drive_idx < 0)
+    dinfo = add_init_drive(opts);
+    if (!dinfo)
+        return;
+    if (dinfo->devaddr) {
+        monitor_printf(mon, "Parameter addr not supported\n");
         return;
-    type = drives_table[drive_idx].type;
+    }
+    type = dinfo->type;
     bus = drive_get_max_bus (type);
 
     switch (type) {
     case IF_SCSI:
         success = 1;
-        lsi_scsi_attach(&dev->qdev, drives_table[drive_idx].bdrv,
-                        drives_table[drive_idx].unit);
+        scsibus = LIST_FIRST(&dev->qdev.child_bus);
+        scsi_bus_legacy_add_drive(DO_UPCAST(SCSIBus, qbus, scsibus),
+                                  dinfo, dinfo->unit);
         break;
     default:
         monitor_printf(mon, "Can't hot-add drive to type %d\n", type);
@@ -80,16 +94,18 @@ void drive_hot_add(Monitor *mon, const char *pci_addr, const char *opts)
 
     if (success)
         monitor_printf(mon, "OK bus %d, unit %d\n",
-                       drives_table[drive_idx].bus,
-                       drives_table[drive_idx].unit);
+                       dinfo->bus,
+                       dinfo->unit);
     return;
 }
 
-static PCIDevice *qemu_pci_hot_add_storage(Monitor *mon, PCIBus *pci_bus,
+static PCIDevice *qemu_pci_hot_add_storage(Monitor *mon,
+                                           const char *devaddr,
                                            const char *opts)
 {
-    void *opaque = NULL;
-    int type = -1, drive_idx = -1;
+    PCIDevice *dev;
+    DriveInfo *dinfo = NULL;
+    int type = -1;
     char buf[128];
 
     if (get_param_value(buf, sizeof(buf), "if", opts)) {
@@ -99,64 +115,74 @@ static PCIDevice *qemu_pci_hot_add_storage(Monitor *mon, PCIBus *pci_bus,
             type = IF_VIRTIO;
         } else {
             monitor_printf(mon, "type %s not a hotpluggable PCI device.\n", buf);
-            goto out;
+            return NULL;
         }
     } else {
         monitor_printf(mon, "no if= specified\n");
-        goto out;
+        return NULL;
     }
 
     if (get_param_value(buf, sizeof(buf), "file", opts)) {
-        drive_idx = add_init_drive(opts);
-        if (drive_idx < 0)
-            goto out;
-    } else if (type == IF_VIRTIO) {
-        monitor_printf(mon, "virtio requires a backing file/device.\n");
-        goto out;
+        dinfo = add_init_drive(opts);
+        if (!dinfo)
+            return NULL;
+        if (dinfo->devaddr) {
+            monitor_printf(mon, "Parameter addr not supported\n");
+            return NULL;
+        }
+    } else {
+        dinfo = NULL;
     }
 
     switch (type) {
     case IF_SCSI:
-        opaque = pci_create_simple(pci_bus, -1, "lsi53c895a");
+        dev = pci_create("lsi53c895a", devaddr);
         break;
     case IF_VIRTIO:
-        opaque = pci_create_simple(pci_bus, -1, "virtio-blk");
-        qdev_init(opaque);
+        if (!dinfo) {
+            monitor_printf(mon, "virtio requires a backing file/device.\n");
+            return NULL;
+        }
+        dev = pci_create("virtio-blk-pci", devaddr);
+        qdev_prop_set_drive(&dev->qdev, "drive", dinfo);
         break;
+    default:
+        dev = NULL;
     }
-
-out:
-    return opaque;
+    if (dev)
+        qdev_init(&dev->qdev);
+    return dev;
 }
 
-void pci_device_hot_add(Monitor *mon, const char *pci_addr, const char *type,
-                        const char *opts)
+void pci_device_hot_add(Monitor *mon, const QDict *qdict)
 {
     PCIDevice *dev = NULL;
-    PCIBus *pci_bus;
-    int dom, bus;
-    unsigned slot;
+    const char *pci_addr = qdict_get_str(qdict, "pci_addr");
+    const char *type = qdict_get_str(qdict, "type");
+    const char *opts = qdict_get_try_str(qdict, "opts");
 
-    if (pci_assign_devaddr(pci_addr, &dom, &bus, &slot)) {
-        monitor_printf(mon, "Invalid pci address\n");
-        return;
+    /* strip legacy tag */
+    if (!strncmp(pci_addr, "pci_addr=", 9)) {
+        pci_addr += 9;
     }
 
-    pci_bus = pci_find_bus(bus);
-    if (!pci_bus) {
-        monitor_printf(mon, "Can't find pci_bus %d\n", bus);
-        return;
+    if (!opts) {
+        opts = "";
     }
 
+    if (!strcmp(pci_addr, "auto"))
+        pci_addr = NULL;
+
     if (strcmp(type, "nic") == 0)
-        dev = qemu_pci_hot_add_nic(pci_bus, opts);
+        dev = qemu_pci_hot_add_nic(mon, pci_addr, opts);
     else if (strcmp(type, "storage") == 0)
-        dev = qemu_pci_hot_add_storage(mon, pci_bus, opts);
+        dev = qemu_pci_hot_add_storage(mon, pci_addr, opts);
     else
         monitor_printf(mon, "invalid type: %s\n", type);
 
     if (dev) {
-        qemu_system_device_hot_add(bus, PCI_SLOT(dev->devfn), 1);
+        qemu_system_device_hot_add(pci_bus_num(dev->bus),
+                                   PCI_SLOT(dev->devfn), 1);
         monitor_printf(mon, "OK domain %d, bus %d, slot %d, function %d\n",
                        0, pci_bus_num(dev->bus), PCI_SLOT(dev->devfn),
                        PCI_FUNC(dev->devfn));
@@ -171,8 +197,7 @@ void pci_device_hot_remove(Monitor *mon, const char *pci_addr)
     int dom, bus;
     unsigned slot;
 
-    if (pci_read_devaddr(pci_addr, &dom, &bus, &slot)) {
-        monitor_printf(mon, "Invalid pci address\n");
+    if (pci_read_devaddr(mon, pci_addr, &dom, &bus, &slot)) {
         return;
     }
 
@@ -185,6 +210,11 @@ void pci_device_hot_remove(Monitor *mon, const char *pci_addr)
     qemu_system_device_hot_add(bus, slot, 0);
 }
 
+void do_pci_device_hot_remove(Monitor *mon, const QDict *qdict)
+{
+    pci_device_hot_remove(mon, qdict_get_str(qdict, "pci_addr"));
+}
+
 static int pci_match_fn(void *dev_private, void *arg)
 {
     PCIDevice *dev = dev_private;