block: Make BlockBackend own its BlockDriverState
[qemu-kvm.git] / hw / pci / pci-hotplug-old.c
blob6ab28b703cfe6e6f056b21cfec22f19c6ca6fc02
1 /*
2 * Deprecated PCI hotplug interface support
3 * This covers the old pci_add / pci_del command, whereas the more general
4 * device_add / device_del commands are now preferred.
6 * Copyright (c) 2004 Fabrice Bellard
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
27 #include "hw/hw.h"
28 #include "hw/boards.h"
29 #include "hw/pci/pci.h"
30 #include "net/net.h"
31 #include "hw/i386/pc.h"
32 #include "monitor/monitor.h"
33 #include "hw/scsi/scsi.h"
34 #include "hw/virtio/virtio-blk.h"
35 #include "qemu/config-file.h"
36 #include "sysemu/blockdev.h"
37 #include "qapi/error.h"
39 static int pci_read_devaddr(Monitor *mon, const char *addr,
40 int *busp, unsigned *slotp)
42 int dom;
44 /* strip legacy tag */
45 if (!strncmp(addr, "pci_addr=", 9)) {
46 addr += 9;
48 if (pci_parse_devaddr(addr, &dom, busp, slotp, NULL)) {
49 monitor_printf(mon, "Invalid pci address\n");
50 return -1;
52 if (dom != 0) {
53 monitor_printf(mon, "Multiple PCI domains not supported, use device_add\n");
54 return -1;
56 return 0;
59 static PCIDevice *qemu_pci_hot_add_nic(Monitor *mon,
60 const char *devaddr,
61 const char *opts_str)
63 Error *local_err = NULL;
64 QemuOpts *opts;
65 PCIBus *root = pci_find_primary_bus();
66 PCIBus *bus;
67 int ret, devfn;
69 if (!root) {
70 monitor_printf(mon, "no primary PCI bus (if there are multiple"
71 " PCI roots, you must use device_add instead)");
72 return NULL;
75 bus = pci_get_bus_devfn(&devfn, root, devaddr);
76 if (!bus) {
77 monitor_printf(mon, "Invalid PCI device address %s\n", devaddr);
78 return NULL;
80 if (!qbus_is_hotpluggable(BUS(bus))) {
81 monitor_printf(mon, "PCI bus doesn't support hotplug\n");
82 return NULL;
85 opts = qemu_opts_parse(qemu_find_opts("net"), opts_str ? opts_str : "", 0);
86 if (!opts) {
87 return NULL;
90 qemu_opt_set(opts, "type", "nic");
92 ret = net_client_init(opts, 0, &local_err);
93 if (local_err) {
94 qerror_report_err(local_err);
95 error_free(local_err);
96 return NULL;
98 if (nd_table[ret].devaddr) {
99 monitor_printf(mon, "Parameter addr not supported\n");
100 return NULL;
102 return pci_nic_init(&nd_table[ret], root, "rtl8139", devaddr);
105 static int scsi_hot_add(Monitor *mon, DeviceState *adapter,
106 DriveInfo *dinfo, int printinfo)
108 SCSIBus *scsibus;
109 SCSIDevice *scsidev;
110 Error *local_err = NULL;
112 scsibus = (SCSIBus *)
113 object_dynamic_cast(OBJECT(QLIST_FIRST(&adapter->child_bus)),
114 TYPE_SCSI_BUS);
115 if (!scsibus) {
116 error_report("Device is not a SCSI adapter");
117 return -1;
121 * drive_init() tries to find a default for dinfo->unit. Doesn't
122 * work at all for hotplug though as we assign the device to a
123 * specific bus instead of the first bus with spare scsi ids.
125 * Ditch the calculated value and reload from option string (if
126 * specified).
128 dinfo->unit = qemu_opt_get_number(dinfo->opts, "unit", -1);
129 dinfo->bus = scsibus->busnr;
130 scsidev = scsi_bus_legacy_add_drive(scsibus, dinfo->bdrv, dinfo->unit,
131 false, -1, NULL, &local_err);
132 if (!scsidev) {
133 error_report("%s", error_get_pretty(local_err));
134 error_free(local_err);
135 return -1;
137 dinfo->unit = scsidev->id;
139 if (printinfo)
140 monitor_printf(mon, "OK bus %d, unit %d\n",
141 scsibus->busnr, scsidev->id);
142 return 0;
145 int pci_drive_hot_add(Monitor *mon, const QDict *qdict, DriveInfo *dinfo)
147 int pci_bus;
148 unsigned slot;
149 PCIBus *root = pci_find_primary_bus();
150 PCIDevice *dev;
151 const char *pci_addr = qdict_get_str(qdict, "pci_addr");
153 switch (dinfo->type) {
154 case IF_SCSI:
155 if (!root) {
156 monitor_printf(mon, "no primary PCI bus (if there are multiple"
157 " PCI roots, you must use device_add instead)");
158 goto err;
160 if (pci_read_devaddr(mon, pci_addr, &pci_bus, &slot)) {
161 goto err;
163 dev = pci_find_device(root, pci_bus, PCI_DEVFN(slot, 0));
164 if (!dev) {
165 monitor_printf(mon, "no pci device with address %s\n", pci_addr);
166 goto err;
168 if (scsi_hot_add(mon, &dev->qdev, dinfo, 1) != 0) {
169 goto err;
171 break;
172 default:
173 monitor_printf(mon, "Can't hot-add drive to type %d\n", dinfo->type);
174 goto err;
177 return 0;
178 err:
179 return -1;
182 static PCIDevice *qemu_pci_hot_add_storage(Monitor *mon,
183 const char *devaddr,
184 const char *opts)
186 PCIDevice *dev;
187 DriveInfo *dinfo = NULL;
188 int type = -1;
189 char buf[128];
190 PCIBus *root = pci_find_primary_bus();
191 PCIBus *bus;
192 int devfn;
194 if (get_param_value(buf, sizeof(buf), "if", opts)) {
195 if (!strcmp(buf, "scsi"))
196 type = IF_SCSI;
197 else if (!strcmp(buf, "virtio")) {
198 type = IF_VIRTIO;
199 } else {
200 monitor_printf(mon, "type %s not a hotpluggable PCI device.\n", buf);
201 return NULL;
203 } else {
204 monitor_printf(mon, "no if= specified\n");
205 return NULL;
208 if (get_param_value(buf, sizeof(buf), "file", opts)) {
209 dinfo = add_init_drive(opts);
210 if (!dinfo)
211 return NULL;
212 if (dinfo->devaddr) {
213 monitor_printf(mon, "Parameter addr not supported\n");
214 return NULL;
216 } else {
217 dinfo = NULL;
220 if (!root) {
221 monitor_printf(mon, "no primary PCI bus (if there are multiple"
222 " PCI roots, you must use device_add instead)");
223 return NULL;
225 bus = pci_get_bus_devfn(&devfn, root, devaddr);
226 if (!bus) {
227 monitor_printf(mon, "Invalid PCI device address %s\n", devaddr);
228 return NULL;
230 if (!qbus_is_hotpluggable(BUS(bus))) {
231 monitor_printf(mon, "PCI bus doesn't support hotplug\n");
232 return NULL;
235 switch (type) {
236 case IF_SCSI:
237 dev = pci_create(bus, devfn, "lsi53c895a");
238 if (qdev_init(&dev->qdev) < 0)
239 dev = NULL;
240 if (dev && dinfo) {
241 if (scsi_hot_add(mon, &dev->qdev, dinfo, 0) != 0) {
242 qdev_unplug(&dev->qdev, NULL);
243 dev = NULL;
246 break;
247 case IF_VIRTIO:
248 if (!dinfo) {
249 monitor_printf(mon, "virtio requires a backing file/device.\n");
250 return NULL;
252 dev = pci_create(bus, devfn, "virtio-blk-pci");
253 if (qdev_prop_set_drive(&dev->qdev, "drive", dinfo->bdrv) < 0) {
254 object_unparent(OBJECT(dev));
255 dev = NULL;
256 break;
258 if (qdev_init(&dev->qdev) < 0)
259 dev = NULL;
260 break;
261 default:
262 dev = NULL;
264 return dev;
267 void pci_device_hot_add(Monitor *mon, const QDict *qdict)
269 PCIDevice *dev = NULL;
270 const char *pci_addr = qdict_get_str(qdict, "pci_addr");
271 const char *type = qdict_get_str(qdict, "type");
272 const char *opts = qdict_get_try_str(qdict, "opts");
274 /* strip legacy tag */
275 if (!strncmp(pci_addr, "pci_addr=", 9)) {
276 pci_addr += 9;
279 if (!opts) {
280 opts = "";
283 if (!strcmp(pci_addr, "auto"))
284 pci_addr = NULL;
286 if (strcmp(type, "nic") == 0) {
287 dev = qemu_pci_hot_add_nic(mon, pci_addr, opts);
288 } else if (strcmp(type, "storage") == 0) {
289 dev = qemu_pci_hot_add_storage(mon, pci_addr, opts);
290 } else {
291 monitor_printf(mon, "invalid type: %s\n", type);
294 if (dev) {
295 monitor_printf(mon, "OK root bus %s, bus %d, slot %d, function %d\n",
296 pci_root_bus_path(dev),
297 pci_bus_num(dev->bus), PCI_SLOT(dev->devfn),
298 PCI_FUNC(dev->devfn));
299 } else
300 monitor_printf(mon, "failed to add %s\n", opts);
303 static int pci_device_hot_remove(Monitor *mon, const char *pci_addr)
305 PCIBus *root = pci_find_primary_bus();
306 PCIDevice *d;
307 int bus;
308 unsigned slot;
309 Error *local_err = NULL;
311 if (!root) {
312 monitor_printf(mon, "no primary PCI bus (if there are multiple"
313 " PCI roots, you must use device_del instead)");
314 return -1;
317 if (pci_read_devaddr(mon, pci_addr, &bus, &slot)) {
318 return -1;
321 d = pci_find_device(root, bus, PCI_DEVFN(slot, 0));
322 if (!d) {
323 monitor_printf(mon, "slot %d empty\n", slot);
324 return -1;
327 qdev_unplug(&d->qdev, &local_err);
328 if (local_err) {
329 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
330 error_free(local_err);
331 return -1;
334 return 0;
337 void do_pci_device_hot_remove(Monitor *mon, const QDict *qdict)
339 pci_device_hot_remove(mon, qdict_get_str(qdict, "pci_addr"));