10 #define PCI_BASE_CLASS_STORAGE 0x01
11 #define PCI_BASE_CLASS_NETWORK 0x02
13 static PCIDevice
*qemu_system_hot_add_nic(const char *opts
, int bus_nr
)
18 pci_bus
= pci_find_bus (bus_nr
);
20 term_printf ("Can't find pci_bus %d\n", bus_nr
);
24 ret
= net_client_init ("nic", opts
);
25 if (ret
< 0 || !nd_table
[ret
].model
)
27 return pci_nic_init (pci_bus
, &nd_table
[ret
], -1);
30 static int add_init_drive(const char *opts
)
32 int drive_opt_idx
, drive_idx
;
35 drive_opt_idx
= drive_add(NULL
, "%s", opts
);
39 drive_idx
= drive_init(&drives_opt
[drive_opt_idx
], 0, current_machine
);
40 if (drive_idx
== -1) {
41 drive_remove(drive_opt_idx
);
48 void drive_hot_add(int pcibus
, const char *devfn_string
, const char *opts
)
50 int drive_idx
, type
, bus
;
55 devfn
= strtoul(devfn_string
, NULL
, 0);
57 dev
= pci_find_device(pcibus
, PCI_SLOT(devfn
));
59 term_printf("no pci device with devfn %d (slot %d)\n", devfn
,
64 drive_idx
= add_init_drive(opts
);
67 type
= drives_table
[drive_idx
].type
;
68 bus
= drive_get_max_bus (type
);
73 lsi_scsi_attach (dev
, drives_table
[drive_idx
].bdrv
,
74 drives_table
[drive_idx
].unit
);
77 term_printf("Can't hot-add drive to type %d\n", type
);
81 term_printf("OK bus %d, unit %d\n", drives_table
[drive_idx
].bus
,
82 drives_table
[drive_idx
].unit
);
86 static PCIDevice
*qemu_system_hot_add_storage(const char *opts
, int bus_nr
)
90 int type
= -1, drive_idx
= -1;
93 pci_bus
= pci_find_bus(bus_nr
);
95 term_printf("Can't find pci_bus %d\n", bus_nr
);
99 if (get_param_value(buf
, sizeof(buf
), "if", opts
)) {
100 if (!strcmp(buf
, "scsi"))
102 else if (!strcmp(buf
, "virtio")) {
106 term_printf("no if= specified\n");
110 if (get_param_value(buf
, sizeof(buf
), "file", opts
)) {
111 drive_idx
= add_init_drive(opts
);
114 } else if (type
== IF_VIRTIO
) {
115 term_printf("virtio requires a backing file/device.\n");
121 opaque
= lsi_scsi_init (pci_bus
, -1);
122 if (opaque
&& drive_idx
>= 0)
123 lsi_scsi_attach (opaque
, drives_table
[drive_idx
].bdrv
,
124 drives_table
[drive_idx
].unit
);
127 opaque
= virtio_blk_init (pci_bus
, 0x1AF4, 0x1001,
128 drives_table
[drive_idx
].bdrv
);
131 term_printf ("type %s not a hotpluggable PCI device.\n", buf
);
137 #if defined(TARGET_I386) || defined(TARGET_X86_64)
138 void device_hot_add(int pcibus
, const char *type
, const char *opts
)
140 PCIDevice
*dev
= NULL
;
142 if (strcmp(type
, "nic") == 0)
143 dev
= qemu_system_hot_add_nic(opts
, pcibus
);
144 else if (strcmp(type
, "storage") == 0)
145 dev
= qemu_system_hot_add_storage(opts
, pcibus
);
147 term_printf("invalid type: %s\n", type
);
150 qemu_system_device_hot_add(pcibus
, PCI_SLOT(dev
->devfn
), 1);
151 term_printf("OK bus %d, slot %d, function %d (devfn %d)\n",
152 pci_bus_num(dev
->bus
), PCI_SLOT(dev
->devfn
),
153 PCI_FUNC(dev
->devfn
), dev
->devfn
);
155 term_printf("failed to add %s\n", opts
);
158 void device_hot_remove(int pcibus
, int slot
)
160 PCIDevice
*d
= pci_find_device(pcibus
, slot
);
163 term_printf("invalid slot %d\n", slot
);
167 qemu_system_device_hot_add(pcibus
, slot
, 0);
171 static void destroy_nic(int slot
)
175 for (i
= 0; i
< MAX_NICS
; i
++)
176 if (nd_table
[i
].used
&&
177 PCI_SLOT(nd_table
[i
].devfn
) == slot
)
178 net_client_uninit(&nd_table
[i
]);
181 static void destroy_bdrvs(int slot
)
184 struct BlockDriverState
*bs
;
186 for (i
= 0; i
<= MAX_DRIVES
; i
++) {
187 bs
= drives_table
[i
].bdrv
;
188 if (bs
&& (PCI_SLOT(bs
->devfn
) == slot
)) {
196 * OS has executed _EJ0 method, we now can remove the device
198 void device_hot_remove_success(int pcibus
, int slot
)
200 PCIDevice
*d
= pci_find_device(pcibus
, slot
);
204 term_printf("invalid slot %d\n", slot
);
208 class_code
= d
->config_read(d
, PCI_CLASS_DEVICE
+1, 1);
210 pci_unregister_device(d
);
213 case PCI_BASE_CLASS_STORAGE
:
216 case PCI_BASE_CLASS_NETWORK
: