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
28 #include "hw/boards.h"
29 #include "hw/pci/pci.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 "qapi/error.h"
39 static int pci_read_devaddr(Monitor
*mon
, const char *addr
,
40 int *busp
, unsigned *slotp
)
44 /* strip legacy tag */
45 if (!strncmp(addr
, "pci_addr=", 9)) {
48 if (pci_parse_devaddr(addr
, &dom
, busp
, slotp
, NULL
)) {
49 monitor_printf(mon
, "Invalid pci address\n");
53 monitor_printf(mon
, "Multiple PCI domains not supported, use device_add\n");
59 static PCIDevice
*qemu_pci_hot_add_nic(Monitor
*mon
,
63 Error
*local_err
= NULL
;
65 PCIBus
*root
= pci_find_primary_bus();
70 monitor_printf(mon
, "no primary PCI bus (if there are multiple"
71 " PCI roots, you must use device_add instead)");
75 bus
= pci_get_bus_devfn(&devfn
, root
, devaddr
);
77 monitor_printf(mon
, "Invalid PCI device address %s\n", devaddr
);
80 if (!qbus_is_hotpluggable(BUS(bus
))) {
81 monitor_printf(mon
, "PCI bus doesn't support hotplug\n");
85 opts
= qemu_opts_parse(qemu_find_opts("net"), opts_str
? opts_str
: "", 0);
90 qemu_opt_set(opts
, "type", "nic");
92 ret
= net_client_init(opts
, 0, &local_err
);
94 qerror_report_err(local_err
);
95 error_free(local_err
);
98 if (nd_table
[ret
].devaddr
) {
99 monitor_printf(mon
, "Parameter addr not supported\n");
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
)
110 Error
*local_err
= NULL
;
112 scsibus
= (SCSIBus
*)
113 object_dynamic_cast(OBJECT(QLIST_FIRST(&adapter
->child_bus
)),
116 error_report("Device is not a SCSI adapter");
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
128 dinfo
->unit
= qemu_opt_get_number(dinfo
->opts
, "unit", -1);
129 dinfo
->bus
= scsibus
->busnr
;
130 scsidev
= scsi_bus_legacy_add_drive(scsibus
,
131 blk_by_legacy_dinfo(dinfo
),
132 dinfo
->unit
, false, -1, NULL
,
135 error_report_err(local_err
);
138 dinfo
->unit
= scsidev
->id
;
141 monitor_printf(mon
, "OK bus %d, unit %d\n",
142 scsibus
->busnr
, scsidev
->id
);
146 int pci_drive_hot_add(Monitor
*mon
, const QDict
*qdict
, DriveInfo
*dinfo
)
150 PCIBus
*root
= pci_find_primary_bus();
152 const char *pci_addr
= qdict_get_str(qdict
, "pci_addr");
154 switch (dinfo
->type
) {
157 monitor_printf(mon
, "no primary PCI bus (if there are multiple"
158 " PCI roots, you must use device_add instead)");
161 if (pci_read_devaddr(mon
, pci_addr
, &pci_bus
, &slot
)) {
164 dev
= pci_find_device(root
, pci_bus
, PCI_DEVFN(slot
, 0));
166 monitor_printf(mon
, "no pci device with address %s\n", pci_addr
);
169 if (scsi_hot_add(mon
, &dev
->qdev
, dinfo
, 1) != 0) {
174 monitor_printf(mon
, "Can't hot-add drive to type %d\n", dinfo
->type
);
183 static PCIDevice
*qemu_pci_hot_add_storage(Monitor
*mon
,
188 DriveInfo
*dinfo
= NULL
;
191 PCIBus
*root
= pci_find_primary_bus();
195 if (get_param_value(buf
, sizeof(buf
), "if", opts
)) {
196 if (!strcmp(buf
, "scsi"))
198 else if (!strcmp(buf
, "virtio")) {
201 monitor_printf(mon
, "type %s not a hotpluggable PCI device.\n", buf
);
205 monitor_printf(mon
, "no if= specified\n");
209 if (get_param_value(buf
, sizeof(buf
), "file", opts
)) {
210 dinfo
= add_init_drive(opts
);
213 if (dinfo
->devaddr
) {
214 monitor_printf(mon
, "Parameter addr not supported\n");
222 monitor_printf(mon
, "no primary PCI bus (if there are multiple"
223 " PCI roots, you must use device_add instead)");
226 bus
= pci_get_bus_devfn(&devfn
, root
, devaddr
);
228 monitor_printf(mon
, "Invalid PCI device address %s\n", devaddr
);
231 if (!qbus_is_hotpluggable(BUS(bus
))) {
232 monitor_printf(mon
, "PCI bus doesn't support hotplug\n");
238 dev
= pci_create(bus
, devfn
, "lsi53c895a");
239 if (qdev_init(&dev
->qdev
) < 0)
242 if (scsi_hot_add(mon
, &dev
->qdev
, dinfo
, 0) != 0) {
243 qdev_unplug(&dev
->qdev
, NULL
);
250 monitor_printf(mon
, "virtio requires a backing file/device.\n");
253 dev
= pci_create(bus
, devfn
, "virtio-blk-pci");
254 if (qdev_prop_set_drive(&dev
->qdev
, "drive",
255 blk_by_legacy_dinfo(dinfo
)) < 0) {
256 object_unparent(OBJECT(dev
));
260 if (qdev_init(&dev
->qdev
) < 0)
269 void hmp_pci_add(Monitor
*mon
, const QDict
*qdict
)
271 PCIDevice
*dev
= NULL
;
272 const char *pci_addr
= qdict_get_str(qdict
, "pci_addr");
273 const char *type
= qdict_get_str(qdict
, "type");
274 const char *opts
= qdict_get_try_str(qdict
, "opts");
276 /* strip legacy tag */
277 if (!strncmp(pci_addr
, "pci_addr=", 9)) {
285 if (!strcmp(pci_addr
, "auto"))
288 if (strcmp(type
, "nic") == 0) {
289 dev
= qemu_pci_hot_add_nic(mon
, pci_addr
, opts
);
290 } else if (strcmp(type
, "storage") == 0) {
291 dev
= qemu_pci_hot_add_storage(mon
, pci_addr
, opts
);
293 monitor_printf(mon
, "invalid type: %s\n", type
);
297 monitor_printf(mon
, "OK root bus %s, bus %d, slot %d, function %d\n",
298 pci_root_bus_path(dev
),
299 pci_bus_num(dev
->bus
), PCI_SLOT(dev
->devfn
),
300 PCI_FUNC(dev
->devfn
));
302 monitor_printf(mon
, "failed to add %s\n", opts
);
305 static int pci_device_hot_remove(Monitor
*mon
, const char *pci_addr
)
307 PCIBus
*root
= pci_find_primary_bus();
311 Error
*local_err
= NULL
;
314 monitor_printf(mon
, "no primary PCI bus (if there are multiple"
315 " PCI roots, you must use device_del instead)");
319 if (pci_read_devaddr(mon
, pci_addr
, &bus
, &slot
)) {
323 d
= pci_find_device(root
, bus
, PCI_DEVFN(slot
, 0));
325 monitor_printf(mon
, "slot %d empty\n", slot
);
329 qdev_unplug(&d
->qdev
, &local_err
);
331 monitor_printf(mon
, "%s\n", error_get_pretty(local_err
));
332 error_free(local_err
);
339 void hmp_pci_del(Monitor
*mon
, const QDict
*qdict
)
341 pci_device_hot_remove(mon
, qdict_get_str(qdict
, "pci_addr"));