blob: 5c6307fa5d236f9481ae8d90260dee3094d96695 [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"
aliguori6f338c32009-02-11 15:21:54 +000029#include "pc.h"
aliguori376253e2009-03-05 23:01:23 +000030#include "monitor.h"
Gerd Hoffmann43b443b2009-10-30 09:54:00 +010031#include "scsi.h"
aliguori6f338c32009-02-11 15:21:54 +000032#include "virtio-blk.h"
Mark McLoughlinc59c7ea2009-10-06 12:17:15 +010033#include "qemu-config.h"
Blue Swirl24463332010-08-24 15:22:24 +000034#include "blockdev.h"
aliguori6f338c32009-02-11 15:21:54 +000035
Juan Quintelace88f892009-10-08 00:00:00 +020036#if defined(TARGET_I386)
Markus Armbruster1f5f6632009-06-18 15:14:09 +020037static PCIDevice *qemu_pci_hot_add_nic(Monitor *mon,
Mark McLoughlinc59c7ea2009-10-06 12:17:15 +010038 const char *devaddr,
39 const char *opts_str)
aliguori6f338c32009-02-11 15:21:54 +000040{
Mark McLoughlinc59c7ea2009-10-06 12:17:15 +010041 QemuOpts *opts;
Gerd Hoffmann53e0d8a2009-12-10 11:11:05 +010042 PCIBus *bus;
43 int ret, devfn;
44
45 bus = pci_get_bus_devfn(&devfn, devaddr);
46 if (!bus) {
47 monitor_printf(mon, "Invalid PCI device address %s\n", devaddr);
48 return NULL;
49 }
50 if (!((BusState*)bus)->allow_hotplug) {
51 monitor_printf(mon, "PCI bus doesn't support hotplug\n");
52 return NULL;
53 }
aliguori6f338c32009-02-11 15:21:54 +000054
Gerd Hoffmann3329f072010-08-20 13:52:01 +020055 opts = qemu_opts_parse(qemu_find_opts("net"), opts_str ? opts_str : "", 0);
Mark McLoughlinc59c7ea2009-10-06 12:17:15 +010056 if (!opts) {
Mark McLoughlinc59c7ea2009-10-06 12:17:15 +010057 return NULL;
58 }
59
60 qemu_opt_set(opts, "type", "nic");
61
Mark McLoughlinf6b134a2009-10-08 19:58:27 +010062 ret = net_client_init(mon, opts, 0);
aliguorieefb4092009-04-17 17:10:47 +000063 if (ret < 0)
aliguori6f338c32009-02-11 15:21:54 +000064 return NULL;
Markus Armbruster5607c382009-06-18 15:14:08 +020065 if (nd_table[ret].devaddr) {
66 monitor_printf(mon, "Parameter addr not supported\n");
67 return NULL;
68 }
Markus Armbruster1f5f6632009-06-18 15:14:09 +020069 return pci_nic_init(&nd_table[ret], "rtl8139", devaddr);
aliguori6f338c32009-02-11 15:21:54 +000070}
71
Markus Armbruster6fdb03d2010-02-18 18:41:52 +010072static int scsi_hot_add(Monitor *mon, DeviceState *adapter,
73 DriveInfo *dinfo, int printinfo)
Gerd Hoffmann30d335d2009-10-14 15:30:22 +020074{
75 SCSIBus *scsibus;
76 SCSIDevice *scsidev;
77
78 scsibus = DO_UPCAST(SCSIBus, qbus, QLIST_FIRST(&adapter->child_bus));
79 if (!scsibus || strcmp(scsibus->qbus.info->name, "SCSI") != 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +010080 error_report("Device is not a SCSI adapter");
Gerd Hoffmann30d335d2009-10-14 15:30:22 +020081 return -1;
82 }
83
84 /*
85 * drive_init() tries to find a default for dinfo->unit. Doesn't
86 * work at all for hotplug though as we assign the device to a
87 * specific bus instead of the first bus with spare scsi ids.
88 *
89 * Ditch the calculated value and reload from option string (if
90 * specified).
91 */
92 dinfo->unit = qemu_opt_get_number(dinfo->opts, "unit", -1);
Markus Armbruster31e1ea32011-01-28 11:21:36 +010093 dinfo->bus = scsibus->busnr;
Paolo Bonzinice4e7e42011-11-18 16:32:00 +010094 scsidev = scsi_bus_legacy_add_drive(scsibus, dinfo->bdrv, dinfo->unit,
95 false, -1);
Markus Armbrusterfa66b902010-06-25 18:53:21 +020096 if (!scsidev) {
97 return -1;
98 }
Gerd Hoffmann11f4d7f2009-12-10 11:11:07 +010099 dinfo->unit = scsidev->id;
Gerd Hoffmann30d335d2009-10-14 15:30:22 +0200100
101 if (printinfo)
Markus Armbruster6fdb03d2010-02-18 18:41:52 +0100102 monitor_printf(mon, "OK bus %d, unit %d\n",
103 scsibus->busnr, scsidev->id);
Gerd Hoffmann30d335d2009-10-14 15:30:22 +0200104 return 0;
105}
106
Alexander Grafdd97aa82010-08-23 23:43:10 +0200107int pci_drive_hot_add(Monitor *mon, const QDict *qdict,
108 DriveInfo *dinfo, int type)
aliguori6f338c32009-02-11 15:21:54 +0000109{
110 int dom, pci_bus;
111 unsigned slot;
aliguori6f338c32009-02-11 15:21:54 +0000112 PCIDevice *dev;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300113 const char *pci_addr = qdict_get_str(qdict, "pci_addr");
aliguori6f338c32009-02-11 15:21:54 +0000114
115 switch (type) {
116 case IF_SCSI:
Gerd Hoffmann4db49dc2009-09-25 21:42:47 +0200117 if (pci_read_devaddr(mon, pci_addr, &dom, &pci_bus, &slot)) {
118 goto err;
119 }
Isaku Yamahata5256d8b2011-01-27 15:56:36 +0900120 dev = pci_find_device(pci_find_root_bus(dom), pci_bus,
121 PCI_DEVFN(slot, 0));
Gerd Hoffmann4db49dc2009-09-25 21:42:47 +0200122 if (!dev) {
123 monitor_printf(mon, "no pci device with address %s\n", pci_addr);
124 goto err;
125 }
Markus Armbruster6fdb03d2010-02-18 18:41:52 +0100126 if (scsi_hot_add(mon, &dev->qdev, dinfo, 1) != 0) {
Gerd Hoffmann30d335d2009-10-14 15:30:22 +0200127 goto err;
128 }
Gerd Hoffmann4db49dc2009-09-25 21:42:47 +0200129 break;
130 default:
131 monitor_printf(mon, "Can't hot-add drive to type %d\n", type);
132 goto err;
133 }
Gerd Hoffmann4db49dc2009-09-25 21:42:47 +0200134
Alexander Grafdd97aa82010-08-23 23:43:10 +0200135 return 0;
Gerd Hoffmann4db49dc2009-09-25 21:42:47 +0200136err:
Alexander Grafdd97aa82010-08-23 23:43:10 +0200137 return -1;
aliguori6f338c32009-02-11 15:21:54 +0000138}
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 }
Gerd Hoffmann53e0d8a2009-12-10 11:11:05 +0100182 if (!((BusState*)bus)->allow_hotplug) {
183 monitor_printf(mon, "PCI bus doesn't support hotplug\n");
184 return NULL;
185 }
Markus Armbruster49bd1452009-09-25 03:53:49 +0200186
aliguori6f338c32009-02-11 15:21:54 +0000187 switch (type) {
188 case IF_SCSI:
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;
Daniel P. Berrangeec7efac2009-12-07 20:51:49 +0000192 if (dev && dinfo) {
Markus Armbruster6fdb03d2010-02-18 18:41:52 +0100193 if (scsi_hot_add(mon, &dev->qdev, dinfo, 0) != 0) {
Gerd Hoffmann30d335d2009-10-14 15:30:22 +0200194 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");
Markus Armbruster18846de2010-06-29 16:58:30 +0200205 if (qdev_prop_set_drive(&dev->qdev, "drive", dinfo->bdrv) < 0) {
206 qdev_free(&dev->qdev);
207 dev = NULL;
208 break;
209 }
Gerd Hoffmann5b684b52009-10-13 13:59:55 +0200210 if (qdev_init(&dev->qdev) < 0)
211 dev = NULL;
aliguori6f338c32009-02-11 15:21:54 +0000212 break;
Markus Armbruster1f5f6632009-06-18 15:14:09 +0200213 default:
214 dev = NULL;
aliguori6f338c32009-02-11 15:21:54 +0000215 }
Markus Armbruster1f5f6632009-06-18 15:14:09 +0200216 return dev;
aliguori6f338c32009-02-11 15:21:54 +0000217}
218
Markus Armbruster6c6a58a2010-05-12 10:53:00 +0200219void pci_device_hot_add(Monitor *mon, const QDict *qdict)
aliguori6f338c32009-02-11 15:21:54 +0000220{
221 PCIDevice *dev = NULL;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300222 const char *pci_addr = qdict_get_str(qdict, "pci_addr");
223 const char *type = qdict_get_str(qdict, "type");
224 const char *opts = qdict_get_try_str(qdict, "opts");
aliguori6f338c32009-02-11 15:21:54 +0000225
Jan Kiszkae9283f82009-06-26 00:04:00 +0200226 /* strip legacy tag */
227 if (!strncmp(pci_addr, "pci_addr=", 9)) {
228 pci_addr += 9;
aliguori6f338c32009-02-11 15:21:54 +0000229 }
230
Jan Kiszkaa62acdc2009-06-26 00:04:10 +0200231 if (!opts) {
232 opts = "";
233 }
234
Jan Kiszkae9283f82009-06-26 00:04:00 +0200235 if (!strcmp(pci_addr, "auto"))
236 pci_addr = NULL;
aliguori6f338c32009-02-11 15:21:54 +0000237
Luiz Capitulino395560c2010-02-10 23:49:55 -0200238 if (strcmp(type, "nic") == 0) {
Jan Kiszkae9283f82009-06-26 00:04:00 +0200239 dev = qemu_pci_hot_add_nic(mon, pci_addr, opts);
Luiz Capitulino395560c2010-02-10 23:49:55 -0200240 } else if (strcmp(type, "storage") == 0) {
Jan Kiszkae9283f82009-06-26 00:04:00 +0200241 dev = qemu_pci_hot_add_storage(mon, pci_addr, opts);
Luiz Capitulino395560c2010-02-10 23:49:55 -0200242 } else {
aliguori376253e2009-03-05 23:01:23 +0000243 monitor_printf(mon, "invalid type: %s\n", type);
Luiz Capitulino395560c2010-02-10 23:49:55 -0200244 }
aliguori6f338c32009-02-11 15:21:54 +0000245
246 if (dev) {
Markus Armbruster6c6a58a2010-05-12 10:53:00 +0200247 monitor_printf(mon, "OK domain %d, bus %d, slot %d, function %d\n",
Isaku Yamahatae075e782010-05-28 18:30:46 +0900248 pci_find_domain(dev->bus),
249 pci_bus_num(dev->bus), PCI_SLOT(dev->devfn),
Markus Armbruster6c6a58a2010-05-12 10:53:00 +0200250 PCI_FUNC(dev->devfn));
251 } else
aliguori376253e2009-03-05 23:01:23 +0000252 monitor_printf(mon, "failed to add %s\n", opts);
aliguori6f338c32009-02-11 15:21:54 +0000253}
254#endif
255
Isaku Yamahataf2b07c92010-06-15 12:48:36 +0900256static int pci_device_hot_remove(Monitor *mon, const char *pci_addr)
aliguori6f338c32009-02-11 15:21:54 +0000257{
258 PCIDevice *d;
259 int dom, bus;
260 unsigned slot;
261
Jan Kiszkae9283f82009-06-26 00:04:00 +0200262 if (pci_read_devaddr(mon, pci_addr, &dom, &bus, &slot)) {
Luiz Capitulino053801b2010-02-10 23:49:56 -0200263 return -1;
aliguori6f338c32009-02-11 15:21:54 +0000264 }
265
Isaku Yamahata5256d8b2011-01-27 15:56:36 +0900266 d = pci_find_device(pci_find_root_bus(dom), bus, PCI_DEVFN(slot, 0));
aliguori6f338c32009-02-11 15:21:54 +0000267 if (!d) {
aliguori376253e2009-03-05 23:01:23 +0000268 monitor_printf(mon, "slot %d empty\n", slot);
Luiz Capitulino053801b2010-02-10 23:49:56 -0200269 return -1;
aliguori6f338c32009-02-11 15:21:54 +0000270 }
Luiz Capitulino053801b2010-02-10 23:49:56 -0200271 return qdev_unplug(&d->qdev);
aliguori6f338c32009-02-11 15:21:54 +0000272}
273
Markus Armbrusterb752daf2010-05-12 10:53:01 +0200274void do_pci_device_hot_remove(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300275{
Markus Armbrusterb752daf2010-05-12 10:53:01 +0200276 pci_device_hot_remove(mon, qdict_get_str(qdict, "pci_addr"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300277}