]> git.proxmox.com Git - qemu.git/blob - hw/pci/pci-hotplug-old.c
619fe473e8fdc30e4e02427c1ff1b29aab0b1d1d
[qemu.git] / hw / pci / pci-hotplug-old.c
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.
5 *
6 * Copyright (c) 2004 Fabrice Bellard
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
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.
25 */
26
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"
38
39 static int pci_read_devaddr(Monitor *mon, const char *addr,
40 int *busp, unsigned *slotp)
41 {
42 int dom;
43
44 /* strip legacy tag */
45 if (!strncmp(addr, "pci_addr=", 9)) {
46 addr += 9;
47 }
48 if (pci_parse_devaddr(addr, &dom, busp, slotp, NULL)) {
49 monitor_printf(mon, "Invalid pci address\n");
50 return -1;
51 }
52 if (dom != 0) {
53 monitor_printf(mon, "Multiple PCI domains not supported, use device_add\n");
54 return -1;
55 }
56 return 0;
57 }
58
59 static PCIDevice *qemu_pci_hot_add_nic(Monitor *mon,
60 const char *devaddr,
61 const char *opts_str)
62 {
63 Error *local_err = NULL;
64 QemuOpts *opts;
65 PCIBus *root = pci_find_primary_bus();
66 PCIBus *bus;
67 int ret, devfn;
68
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;
73 }
74
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;
79 }
80 if (!((BusState*)bus)->allow_hotplug) {
81 monitor_printf(mon, "PCI bus doesn't support hotplug\n");
82 return NULL;
83 }
84
85 opts = qemu_opts_parse(qemu_find_opts("net"), opts_str ? opts_str : "", 0);
86 if (!opts) {
87 return NULL;
88 }
89
90 qemu_opt_set(opts, "type", "nic");
91
92 ret = net_client_init(opts, 0, &local_err);
93 if (error_is_set(&local_err)) {
94 qerror_report_err(local_err);
95 error_free(local_err);
96 return NULL;
97 }
98 if (nd_table[ret].devaddr) {
99 monitor_printf(mon, "Parameter addr not supported\n");
100 return NULL;
101 }
102 return pci_nic_init(&nd_table[ret], root, "rtl8139", devaddr);
103 }
104
105 static int scsi_hot_add(Monitor *mon, DeviceState *adapter,
106 DriveInfo *dinfo, int printinfo)
107 {
108 SCSIBus *scsibus;
109 SCSIDevice *scsidev;
110
111 scsibus = (SCSIBus *)
112 object_dynamic_cast(OBJECT(QLIST_FIRST(&adapter->child_bus)),
113 TYPE_SCSI_BUS);
114 if (!scsibus) {
115 error_report("Device is not a SCSI adapter");
116 return -1;
117 }
118
119 /*
120 * drive_init() tries to find a default for dinfo->unit. Doesn't
121 * work at all for hotplug though as we assign the device to a
122 * specific bus instead of the first bus with spare scsi ids.
123 *
124 * Ditch the calculated value and reload from option string (if
125 * specified).
126 */
127 dinfo->unit = qemu_opt_get_number(dinfo->opts, "unit", -1);
128 dinfo->bus = scsibus->busnr;
129 scsidev = scsi_bus_legacy_add_drive(scsibus, dinfo->bdrv, dinfo->unit,
130 false, -1, NULL, NULL);
131 if (!scsidev) {
132 return -1;
133 }
134 dinfo->unit = scsidev->id;
135
136 if (printinfo)
137 monitor_printf(mon, "OK bus %d, unit %d\n",
138 scsibus->busnr, scsidev->id);
139 return 0;
140 }
141
142 int pci_drive_hot_add(Monitor *mon, const QDict *qdict, DriveInfo *dinfo)
143 {
144 int pci_bus;
145 unsigned slot;
146 PCIBus *root = pci_find_primary_bus();
147 PCIDevice *dev;
148 const char *pci_addr = qdict_get_str(qdict, "pci_addr");
149
150 switch (dinfo->type) {
151 case IF_SCSI:
152 if (!root) {
153 monitor_printf(mon, "no primary PCI bus (if there are multiple"
154 " PCI roots, you must use device_add instead)");
155 goto err;
156 }
157 if (pci_read_devaddr(mon, pci_addr, &pci_bus, &slot)) {
158 goto err;
159 }
160 dev = pci_find_device(root, pci_bus, PCI_DEVFN(slot, 0));
161 if (!dev) {
162 monitor_printf(mon, "no pci device with address %s\n", pci_addr);
163 goto err;
164 }
165 if (scsi_hot_add(mon, &dev->qdev, dinfo, 1) != 0) {
166 goto err;
167 }
168 break;
169 default:
170 monitor_printf(mon, "Can't hot-add drive to type %d\n", dinfo->type);
171 goto err;
172 }
173
174 return 0;
175 err:
176 return -1;
177 }
178
179 static PCIDevice *qemu_pci_hot_add_storage(Monitor *mon,
180 const char *devaddr,
181 const char *opts)
182 {
183 PCIDevice *dev;
184 DriveInfo *dinfo = NULL;
185 int type = -1;
186 char buf[128];
187 PCIBus *root = pci_find_primary_bus();
188 PCIBus *bus;
189 int devfn;
190
191 if (get_param_value(buf, sizeof(buf), "if", opts)) {
192 if (!strcmp(buf, "scsi"))
193 type = IF_SCSI;
194 else if (!strcmp(buf, "virtio")) {
195 type = IF_VIRTIO;
196 } else {
197 monitor_printf(mon, "type %s not a hotpluggable PCI device.\n", buf);
198 return NULL;
199 }
200 } else {
201 monitor_printf(mon, "no if= specified\n");
202 return NULL;
203 }
204
205 if (get_param_value(buf, sizeof(buf), "file", opts)) {
206 dinfo = add_init_drive(opts);
207 if (!dinfo)
208 return NULL;
209 if (dinfo->devaddr) {
210 monitor_printf(mon, "Parameter addr not supported\n");
211 return NULL;
212 }
213 } else {
214 dinfo = NULL;
215 }
216
217 if (!root) {
218 monitor_printf(mon, "no primary PCI bus (if there are multiple"
219 " PCI roots, you must use device_add instead)");
220 return NULL;
221 }
222 bus = pci_get_bus_devfn(&devfn, root, devaddr);
223 if (!bus) {
224 monitor_printf(mon, "Invalid PCI device address %s\n", devaddr);
225 return NULL;
226 }
227 if (!((BusState*)bus)->allow_hotplug) {
228 monitor_printf(mon, "PCI bus doesn't support hotplug\n");
229 return NULL;
230 }
231
232 switch (type) {
233 case IF_SCSI:
234 dev = pci_create(bus, devfn, "lsi53c895a");
235 if (qdev_init(&dev->qdev) < 0)
236 dev = NULL;
237 if (dev && dinfo) {
238 if (scsi_hot_add(mon, &dev->qdev, dinfo, 0) != 0) {
239 qdev_unplug(&dev->qdev, NULL);
240 dev = NULL;
241 }
242 }
243 break;
244 case IF_VIRTIO:
245 if (!dinfo) {
246 monitor_printf(mon, "virtio requires a backing file/device.\n");
247 return NULL;
248 }
249 dev = pci_create(bus, devfn, "virtio-blk-pci");
250 if (qdev_prop_set_drive(&dev->qdev, "drive", dinfo->bdrv) < 0) {
251 qdev_free(&dev->qdev);
252 dev = NULL;
253 break;
254 }
255 if (qdev_init(&dev->qdev) < 0)
256 dev = NULL;
257 break;
258 default:
259 dev = NULL;
260 }
261 return dev;
262 }
263
264 void pci_device_hot_add(Monitor *mon, const QDict *qdict)
265 {
266 PCIDevice *dev = NULL;
267 const char *pci_addr = qdict_get_str(qdict, "pci_addr");
268 const char *type = qdict_get_str(qdict, "type");
269 const char *opts = qdict_get_try_str(qdict, "opts");
270
271 /* strip legacy tag */
272 if (!strncmp(pci_addr, "pci_addr=", 9)) {
273 pci_addr += 9;
274 }
275
276 if (!opts) {
277 opts = "";
278 }
279
280 if (!strcmp(pci_addr, "auto"))
281 pci_addr = NULL;
282
283 if (strcmp(type, "nic") == 0) {
284 dev = qemu_pci_hot_add_nic(mon, pci_addr, opts);
285 } else if (strcmp(type, "storage") == 0) {
286 dev = qemu_pci_hot_add_storage(mon, pci_addr, opts);
287 } else {
288 monitor_printf(mon, "invalid type: %s\n", type);
289 }
290
291 if (dev) {
292 monitor_printf(mon, "OK root bus %s, bus %d, slot %d, function %d\n",
293 pci_root_bus_path(dev),
294 pci_bus_num(dev->bus), PCI_SLOT(dev->devfn),
295 PCI_FUNC(dev->devfn));
296 } else
297 monitor_printf(mon, "failed to add %s\n", opts);
298 }
299
300 static int pci_device_hot_remove(Monitor *mon, const char *pci_addr)
301 {
302 PCIBus *root = pci_find_primary_bus();
303 PCIDevice *d;
304 int bus;
305 unsigned slot;
306 Error *local_err = NULL;
307
308 if (!root) {
309 monitor_printf(mon, "no primary PCI bus (if there are multiple"
310 " PCI roots, you must use device_del instead)");
311 return -1;
312 }
313
314 if (pci_read_devaddr(mon, pci_addr, &bus, &slot)) {
315 return -1;
316 }
317
318 d = pci_find_device(root, bus, PCI_DEVFN(slot, 0));
319 if (!d) {
320 monitor_printf(mon, "slot %d empty\n", slot);
321 return -1;
322 }
323
324 qdev_unplug(&d->qdev, &local_err);
325 if (error_is_set(&local_err)) {
326 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
327 error_free(local_err);
328 return -1;
329 }
330
331 return 0;
332 }
333
334 void do_pci_device_hot_remove(Monitor *mon, const QDict *qdict)
335 {
336 pci_device_hot_remove(mon, qdict_get_str(qdict, "pci_addr"));
337 }