From: Marcelo Tosatti <[EMAIL PROTECTED]>

Add monitor command to hot-remove devices.

Remove device data on _EJ0 notification.

Signed-off-by: Marcelo Tosatti <[EMAIL PROTECTED]>
Signed-off-by: Avi Kivity <[EMAIL PROTECTED]>

diff --git a/qemu/hw/acpi.c b/qemu/hw/acpi.c
index 22a684f..3fec172 100644
--- a/qemu/hw/acpi.c
+++ b/qemu/hw/acpi.c
@@ -673,6 +673,8 @@ static void pciej_write(void *opaque, uint32_t addr, 
uint32_t val)
 {
     int slot = ffs(val) - 1;
 
+    device_hot_remove_success(0, slot);
+
 #if defined(DEBUG)
     printf("pciej write %lx <== %d\n", addr, val);
 #endif
@@ -751,7 +753,7 @@ static void disable_device(struct pci_status *p, struct 
gpe_regs *g, int slot)
     p->down |= (1 << slot);
 }
 
-void qemu_system_device_hot_add(int slot, int state)
+void qemu_system_device_hot_add(int pcibus, int slot, int state)
 {
     qemu_set_irq(pm_state->irq, 1);
     pci0_status.up = 0;
diff --git a/qemu/hw/device-hotplug.c b/qemu/hw/device-hotplug.c
index 02e3e17..98a467c 100644
--- a/qemu/hw/device-hotplug.c
+++ b/qemu/hw/device-hotplug.c
@@ -5,6 +5,10 @@
 #include "sysemu.h"
 #include "pc.h"
 #include "console.h"
+#include "block_int.h"
+
+#define PCI_BASE_CLASS_STORAGE          0x01
+#define PCI_BASE_CLASS_NETWORK          0x02
 
 static PCIDevice *qemu_system_hot_add_nic(const char *opts, int bus_nr)
 {
@@ -148,10 +152,75 @@ void device_hot_add(int pcibus, const char *type, const 
char *opts)
         term_printf("invalid type: %s\n", type);
 
     if (dev) {
-        qemu_system_device_hot_add(PCI_SLOT(dev->devfn), 1);
+        qemu_system_device_hot_add(pcibus, PCI_SLOT(dev->devfn), 1);
         term_printf("OK bus %d, slot %d, function %d (devfn %d)\n",
                     pci_bus_num(dev->bus), PCI_SLOT(dev->devfn),
                     PCI_FUNC(dev->devfn), dev->devfn);
     } else
         term_printf("failed to add %s\n", opts);
 }
+
+void device_hot_remove(int pcibus, int slot)
+{
+    PCIDevice *d = pci_find_device(pcibus, slot);
+
+    if (!d) {
+        term_printf("invalid slot %d\n", slot);
+        return;
+    }
+
+    qemu_system_device_hot_add(pcibus, slot, 0);
+}
+
+static void destroy_nic(int slot)
+{
+    int i;
+
+    for (i = 0; i < MAX_NICS; i++)
+        if (nd_table[i].used &&
+            PCI_SLOT(nd_table[i].devfn) == slot)
+                net_client_uninit(&nd_table[i]);
+}
+
+static void destroy_bdrvs(int slot)
+{
+    int i;
+    struct BlockDriverState *bs;
+
+    for (i = 0; i <= MAX_DRIVES; i++) {
+        bs = drives_table[i].bdrv;
+        if (bs && (PCI_SLOT(bs->devfn) == slot)) {
+            drive_uninit(bs);
+            bdrv_delete(bs);
+        }
+    }
+}
+
+/*
+ * OS has executed _EJ0 method, we now can remove the device
+ */
+void device_hot_remove_success(int pcibus, int slot)
+{
+    PCIDevice *d = pci_find_device(pcibus, slot);
+    int class_code;
+
+    if (!d) {
+        term_printf("invalid slot %d\n", slot);
+        return;
+    }
+
+    class_code = d->config_read(d, PCI_CLASS_DEVICE+1, 1);
+
+    pci_unregister_device(d);
+
+    switch(class_code) {
+    case PCI_BASE_CLASS_STORAGE:
+        destroy_bdrvs(slot);
+        break;
+    case PCI_BASE_CLASS_NETWORK:
+        destroy_nic(slot);
+        break;
+    }
+
+}
+
diff --git a/qemu/monitor.c b/qemu/monitor.c
index 66af8da..2077392 100644
--- a/qemu/monitor.c
+++ b/qemu/monitor.c
@@ -1365,6 +1365,7 @@ static term_cmd_t term_cmds[] = {
                                         "[snapshot=on|off][,cache=on|off]",
                                         "add drive to PCI storage controller" 
},
     { "pci_add", "iss", device_hot_add, "bus nic|storage 
[[vlan=n][,macaddr=addr][,model=type]] [file=file][,if=type][,bus=nr]...", 
"hot-add PCI device" },
+    { "pci_del", "ii", device_hot_remove, "bus slot-number", "hot remove PCI 
device" },
     { NULL, NULL, },
 };
 
diff --git a/qemu/sysemu.h b/qemu/sysemu.h
index 7bd492c..9a50876 100644
--- a/qemu/sysemu.h
+++ b/qemu/sysemu.h
@@ -176,11 +176,13 @@ extern int drive_init(struct drive_opt *arg, int 
snapshot, void *machine);
 /* acpi */
 void qemu_system_cpu_hot_add(int cpu, int state);
 void qemu_system_hot_add_init(char *cpu_model);
-void qemu_system_device_hot_add(int slot, int state);
+void qemu_system_device_hot_add(int pcibus, int slot, int state);
 
 /* device-hotplug */
 void device_hot_add(int pcibus, const char *type, const char *opts);
 void drive_hot_add(int pcibus, const char *devfn_string, const char *opts);
+void device_hot_remove(int pcibus, int slot);
+void device_hot_remove_success(int pcibus, int slot);
 
 /* vmchannel devices */
 

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
kvm-commits mailing list
kvm-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/kvm-commits

Reply via email to