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