blob: 081d6d1ac94eff9677eb07140b5178a28e64e2ba [file] [log] [blame]
aliguori6f338c32009-02-11 15:21:54 +00001/*
2 * QEMU PCI hotplug support
3 *
4 * Copyright (c) 2004 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25#include "hw.h"
26#include "boards.h"
27#include "pci.h"
28#include "net.h"
29#include "sysemu.h"
30#include "pc.h"
aliguori376253e2009-03-05 23:01:23 +000031#include "monitor.h"
aliguori6f338c32009-02-11 15:21:54 +000032#include "block_int.h"
Gerd Hoffmann43b443b2009-10-30 09:54:00 +010033#include "scsi.h"
aliguori6f338c32009-02-11 15:21:54 +000034#include "virtio-blk.h"
Mark McLoughlinc59c7ea2009-10-06 12:17:15 +010035#include "qemu-config.h"
aliguori6f338c32009-02-11 15:21:54 +000036
Juan Quintelace88f892009-10-08 00:00:00 +020037#if defined(TARGET_I386)
Markus Armbruster1f5f6632009-06-18 15:14:09 +020038static PCIDevice *qemu_pci_hot_add_nic(Monitor *mon,
Mark McLoughlinc59c7ea2009-10-06 12:17:15 +010039 const char *devaddr,
40 const char *opts_str)
aliguori6f338c32009-02-11 15:21:54 +000041{
Mark McLoughlinc59c7ea2009-10-06 12:17:15 +010042 QemuOpts *opts;
aliguori6f338c32009-02-11 15:21:54 +000043 int ret;
44
Mark McLoughlinc59c7ea2009-10-06 12:17:15 +010045 opts = qemu_opts_parse(&qemu_net_opts, opts_str ? opts_str : "", NULL);
46 if (!opts) {
47 monitor_printf(mon, "parsing network options '%s' failed\n",
48 opts_str ? opts_str : "");
49 return NULL;
50 }
51
52 qemu_opt_set(opts, "type", "nic");
53
Mark McLoughlinf6b134a2009-10-08 19:58:27 +010054 ret = net_client_init(mon, opts, 0);
aliguorieefb4092009-04-17 17:10:47 +000055 if (ret < 0)
aliguori6f338c32009-02-11 15:21:54 +000056 return NULL;
Markus Armbruster5607c382009-06-18 15:14:08 +020057 if (nd_table[ret].devaddr) {
58 monitor_printf(mon, "Parameter addr not supported\n");
59 return NULL;
60 }
Markus Armbruster1f5f6632009-06-18 15:14:09 +020061 return pci_nic_init(&nd_table[ret], "rtl8139", devaddr);
aliguori6f338c32009-02-11 15:21:54 +000062}
63
Gerd Hoffmann30d335d2009-10-14 15:30:22 +020064static int scsi_hot_add(DeviceState *adapter, DriveInfo *dinfo, int printinfo)
65{
66 SCSIBus *scsibus;
67 SCSIDevice *scsidev;
68
69 scsibus = DO_UPCAST(SCSIBus, qbus, QLIST_FIRST(&adapter->child_bus));
70 if (!scsibus || strcmp(scsibus->qbus.info->name, "SCSI") != 0) {
71 qemu_error("Device is not a SCSI adapter\n");
72 return -1;
73 }
74
75 /*
76 * drive_init() tries to find a default for dinfo->unit. Doesn't
77 * work at all for hotplug though as we assign the device to a
78 * specific bus instead of the first bus with spare scsi ids.
79 *
80 * Ditch the calculated value and reload from option string (if
81 * specified).
82 */
83 dinfo->unit = qemu_opt_get_number(dinfo->opts, "unit", -1);
84 scsidev = scsi_bus_legacy_add_drive(scsibus, dinfo, dinfo->unit);
85
86 if (printinfo)
87 qemu_error("OK bus %d, unit %d\n", scsibus->busnr, scsidev->id);
88 return 0;
89}
90
Luiz Capitulinof18c16d2009-08-28 15:27:14 -030091void drive_hot_add(Monitor *mon, const QDict *qdict)
aliguori6f338c32009-02-11 15:21:54 +000092{
93 int dom, pci_bus;
94 unsigned slot;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +020095 int type, bus;
aliguori6f338c32009-02-11 15:21:54 +000096 PCIDevice *dev;
Gerd Hoffmann4db49dc2009-09-25 21:42:47 +020097 DriveInfo *dinfo = NULL;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -030098 const char *pci_addr = qdict_get_str(qdict, "pci_addr");
99 const char *opts = qdict_get_str(qdict, "opts");
aliguori6f338c32009-02-11 15:21:54 +0000100
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200101 dinfo = add_init_drive(opts);
102 if (!dinfo)
Gerd Hoffmann4db49dc2009-09-25 21:42:47 +0200103 goto err;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200104 if (dinfo->devaddr) {
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +0200105 monitor_printf(mon, "Parameter addr not supported\n");
Gerd Hoffmann4db49dc2009-09-25 21:42:47 +0200106 goto err;
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +0200107 }
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200108 type = dinfo->type;
aliguori6f338c32009-02-11 15:21:54 +0000109 bus = drive_get_max_bus (type);
110
111 switch (type) {
112 case IF_SCSI:
Gerd Hoffmann4db49dc2009-09-25 21:42:47 +0200113 if (pci_read_devaddr(mon, pci_addr, &dom, &pci_bus, &slot)) {
114 goto err;
115 }
Isaku Yamahatac469e1d2009-11-12 14:58:36 +0900116 dev = pci_find_device(pci_find_root_bus(0), pci_bus, slot, 0);
Gerd Hoffmann4db49dc2009-09-25 21:42:47 +0200117 if (!dev) {
118 monitor_printf(mon, "no pci device with address %s\n", pci_addr);
119 goto err;
120 }
Gerd Hoffmann30d335d2009-10-14 15:30:22 +0200121 if (scsi_hot_add(&dev->qdev, dinfo, 1) != 0) {
122 goto err;
123 }
Gerd Hoffmann4db49dc2009-09-25 21:42:47 +0200124 break;
Gerd Hoffmann71011742009-09-25 21:42:48 +0200125 case IF_NONE:
126 monitor_printf(mon, "OK\n");
127 break;
Gerd Hoffmann4db49dc2009-09-25 21:42:47 +0200128 default:
129 monitor_printf(mon, "Can't hot-add drive to type %d\n", type);
130 goto err;
131 }
132 return;
133
134err:
135 if (dinfo)
136 drive_uninit(dinfo);
aliguori6f338c32009-02-11 15:21:54 +0000137 return;
138}
139
Markus Armbruster1f5f6632009-06-18 15:14:09 +0200140static PCIDevice *qemu_pci_hot_add_storage(Monitor *mon,
141 const char *devaddr,
aliguori376253e2009-03-05 23:01:23 +0000142 const char *opts)
aliguori6f338c32009-02-11 15:21:54 +0000143{
Markus Armbruster1f5f6632009-06-18 15:14:09 +0200144 PCIDevice *dev;
Sebastian Herbszt06c79f42009-08-16 14:07:54 +0200145 DriveInfo *dinfo = NULL;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200146 int type = -1;
aliguori6f338c32009-02-11 15:21:54 +0000147 char buf[128];
Markus Armbruster49bd1452009-09-25 03:53:49 +0200148 PCIBus *bus;
149 int devfn;
aliguori6f338c32009-02-11 15:21:54 +0000150
151 if (get_param_value(buf, sizeof(buf), "if", opts)) {
152 if (!strcmp(buf, "scsi"))
153 type = IF_SCSI;
154 else if (!strcmp(buf, "virtio")) {
155 type = IF_VIRTIO;
aliguori8707ecc2009-04-05 17:40:55 +0000156 } else {
157 monitor_printf(mon, "type %s not a hotpluggable PCI device.\n", buf);
Markus Armbruster1f5f6632009-06-18 15:14:09 +0200158 return NULL;
aliguori6f338c32009-02-11 15:21:54 +0000159 }
160 } else {
aliguori376253e2009-03-05 23:01:23 +0000161 monitor_printf(mon, "no if= specified\n");
Markus Armbruster1f5f6632009-06-18 15:14:09 +0200162 return NULL;
aliguori6f338c32009-02-11 15:21:54 +0000163 }
164
165 if (get_param_value(buf, sizeof(buf), "file", opts)) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200166 dinfo = add_init_drive(opts);
167 if (!dinfo)
Markus Armbruster1f5f6632009-06-18 15:14:09 +0200168 return NULL;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200169 if (dinfo->devaddr) {
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +0200170 monitor_printf(mon, "Parameter addr not supported\n");
171 return NULL;
172 }
Blue Swirl7432ff52009-08-23 06:12:54 +0000173 } else {
174 dinfo = NULL;
aliguori6f338c32009-02-11 15:21:54 +0000175 }
176
Markus Armbruster49bd1452009-09-25 03:53:49 +0200177 bus = pci_get_bus_devfn(&devfn, devaddr);
178 if (!bus) {
179 monitor_printf(mon, "Invalid PCI device address %s\n", devaddr);
180 return NULL;
181 }
182
aliguori6f338c32009-02-11 15:21:54 +0000183 switch (type) {
184 case IF_SCSI:
Gerd Hoffmann5b684b52009-10-13 13:59:55 +0200185 if (!dinfo) {
186 monitor_printf(mon, "scsi requires a backing file/device.\n");
187 return NULL;
188 }
Markus Armbruster499cf102009-09-25 03:53:53 +0200189 dev = pci_create(bus, devfn, "lsi53c895a");
Gerd Hoffmann5b684b52009-10-13 13:59:55 +0200190 if (qdev_init(&dev->qdev) < 0)
191 dev = NULL;
192 if (dev) {
Gerd Hoffmann30d335d2009-10-14 15:30:22 +0200193 if (scsi_hot_add(&dev->qdev, dinfo, 0) != 0) {
194 qdev_unplug(&dev->qdev);
195 dev = NULL;
196 }
Gerd Hoffmann5b684b52009-10-13 13:59:55 +0200197 }
aliguori6f338c32009-02-11 15:21:54 +0000198 break;
199 case IF_VIRTIO:
Blue Swirl7432ff52009-08-23 06:12:54 +0000200 if (!dinfo) {
201 monitor_printf(mon, "virtio requires a backing file/device.\n");
202 return NULL;
203 }
Markus Armbruster499cf102009-09-25 03:53:53 +0200204 dev = pci_create(bus, devfn, "virtio-blk-pci");
Gerd Hoffmannd176c492009-07-31 12:25:41 +0200205 qdev_prop_set_drive(&dev->qdev, "drive", dinfo);
Gerd Hoffmann5b684b52009-10-13 13:59:55 +0200206 if (qdev_init(&dev->qdev) < 0)
207 dev = NULL;
aliguori6f338c32009-02-11 15:21:54 +0000208 break;
Markus Armbruster1f5f6632009-06-18 15:14:09 +0200209 default:
210 dev = NULL;
aliguori6f338c32009-02-11 15:21:54 +0000211 }
Markus Armbruster1f5f6632009-06-18 15:14:09 +0200212 return dev;
aliguori6f338c32009-02-11 15:21:54 +0000213}
214
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300215void pci_device_hot_add(Monitor *mon, const QDict *qdict)
aliguori6f338c32009-02-11 15:21:54 +0000216{
217 PCIDevice *dev = NULL;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300218 const char *pci_addr = qdict_get_str(qdict, "pci_addr");
219 const char *type = qdict_get_str(qdict, "type");
220 const char *opts = qdict_get_try_str(qdict, "opts");
aliguori6f338c32009-02-11 15:21:54 +0000221
Jan Kiszkae9283f82009-06-26 00:04:00 +0200222 /* strip legacy tag */
223 if (!strncmp(pci_addr, "pci_addr=", 9)) {
224 pci_addr += 9;
aliguori6f338c32009-02-11 15:21:54 +0000225 }
226
Jan Kiszkaa62acdc2009-06-26 00:04:10 +0200227 if (!opts) {
228 opts = "";
229 }
230
Jan Kiszkae9283f82009-06-26 00:04:00 +0200231 if (!strcmp(pci_addr, "auto"))
232 pci_addr = NULL;
aliguori6f338c32009-02-11 15:21:54 +0000233
234 if (strcmp(type, "nic") == 0)
Jan Kiszkae9283f82009-06-26 00:04:00 +0200235 dev = qemu_pci_hot_add_nic(mon, pci_addr, opts);
aliguori6f338c32009-02-11 15:21:54 +0000236 else if (strcmp(type, "storage") == 0)
Jan Kiszkae9283f82009-06-26 00:04:00 +0200237 dev = qemu_pci_hot_add_storage(mon, pci_addr, opts);
aliguori6f338c32009-02-11 15:21:54 +0000238 else
aliguori376253e2009-03-05 23:01:23 +0000239 monitor_printf(mon, "invalid type: %s\n", type);
aliguori6f338c32009-02-11 15:21:54 +0000240
241 if (dev) {
aliguori376253e2009-03-05 23:01:23 +0000242 monitor_printf(mon, "OK domain %d, bus %d, slot %d, function %d\n",
243 0, pci_bus_num(dev->bus), PCI_SLOT(dev->devfn),
244 PCI_FUNC(dev->devfn));
aliguori6f338c32009-02-11 15:21:54 +0000245 } else
aliguori376253e2009-03-05 23:01:23 +0000246 monitor_printf(mon, "failed to add %s\n", opts);
aliguori6f338c32009-02-11 15:21:54 +0000247}
248#endif
249
aliguori376253e2009-03-05 23:01:23 +0000250void pci_device_hot_remove(Monitor *mon, const char *pci_addr)
aliguori6f338c32009-02-11 15:21:54 +0000251{
252 PCIDevice *d;
253 int dom, bus;
254 unsigned slot;
255
Jan Kiszkae9283f82009-06-26 00:04:00 +0200256 if (pci_read_devaddr(mon, pci_addr, &dom, &bus, &slot)) {
aliguori6f338c32009-02-11 15:21:54 +0000257 return;
258 }
259
Isaku Yamahatac469e1d2009-11-12 14:58:36 +0900260 d = pci_find_device(pci_find_root_bus(0), bus, slot, 0);
aliguori6f338c32009-02-11 15:21:54 +0000261 if (!d) {
aliguori376253e2009-03-05 23:01:23 +0000262 monitor_printf(mon, "slot %d empty\n", slot);
aliguori6f338c32009-02-11 15:21:54 +0000263 return;
264 }
Gerd Hoffmann3f848652009-09-25 21:42:45 +0200265 qdev_unplug(&d->qdev);
aliguori6f338c32009-02-11 15:21:54 +0000266}
267
Luiz Capitulino6848d822009-10-16 12:23:48 -0300268void do_pci_device_hot_remove(Monitor *mon, const QDict *qdict,
269 QObject **ret_data)
Luiz Capitulino38183182009-08-28 15:27:08 -0300270{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300271 pci_device_hot_remove(mon, qdict_get_str(qdict, "pci_addr"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300272}