Statistics
| Branch: | Revision:

root / hw / pci-hotplug.c @ 2be24aaa

History | View | Annotate | Download (9.1 kB)

1 6f338c34 aliguori
/*
2 6f338c34 aliguori
 * QEMU PCI hotplug support
3 6f338c34 aliguori
 *
4 6f338c34 aliguori
 * Copyright (c) 2004 Fabrice Bellard
5 6f338c34 aliguori
 *
6 6f338c34 aliguori
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 6f338c34 aliguori
 * of this software and associated documentation files (the "Software"), to deal
8 6f338c34 aliguori
 * in the Software without restriction, including without limitation the rights
9 6f338c34 aliguori
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 6f338c34 aliguori
 * copies of the Software, and to permit persons to whom the Software is
11 6f338c34 aliguori
 * furnished to do so, subject to the following conditions:
12 6f338c34 aliguori
 *
13 6f338c34 aliguori
 * The above copyright notice and this permission notice shall be included in
14 6f338c34 aliguori
 * all copies or substantial portions of the Software.
15 6f338c34 aliguori
 *
16 6f338c34 aliguori
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 6f338c34 aliguori
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 6f338c34 aliguori
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 6f338c34 aliguori
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 6f338c34 aliguori
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 6f338c34 aliguori
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 6f338c34 aliguori
 * THE SOFTWARE.
23 6f338c34 aliguori
 */
24 6f338c34 aliguori
25 6f338c34 aliguori
#include "hw.h"
26 6f338c34 aliguori
#include "boards.h"
27 6f338c34 aliguori
#include "pci.h"
28 6f338c34 aliguori
#include "net.h"
29 6f338c34 aliguori
#include "sysemu.h"
30 6f338c34 aliguori
#include "pc.h"
31 376253ec aliguori
#include "monitor.h"
32 6f338c34 aliguori
#include "block_int.h"
33 43b443b6 Gerd Hoffmann
#include "scsi.h"
34 6f338c34 aliguori
#include "virtio-blk.h"
35 c59c7ea9 Mark McLoughlin
#include "qemu-config.h"
36 7a344f7a Luiz Capitulino
#include "qemu-objects.h"
37 6f338c34 aliguori
38 ce88f890 Juan Quintela
#if defined(TARGET_I386)
39 1f5f6638 Markus Armbruster
static PCIDevice *qemu_pci_hot_add_nic(Monitor *mon,
40 c59c7ea9 Mark McLoughlin
                                       const char *devaddr,
41 c59c7ea9 Mark McLoughlin
                                       const char *opts_str)
42 6f338c34 aliguori
{
43 c59c7ea9 Mark McLoughlin
    QemuOpts *opts;
44 53e0d8af Gerd Hoffmann
    PCIBus *bus;
45 53e0d8af Gerd Hoffmann
    int ret, devfn;
46 53e0d8af Gerd Hoffmann
47 53e0d8af Gerd Hoffmann
    bus = pci_get_bus_devfn(&devfn, devaddr);
48 53e0d8af Gerd Hoffmann
    if (!bus) {
49 53e0d8af Gerd Hoffmann
        monitor_printf(mon, "Invalid PCI device address %s\n", devaddr);
50 53e0d8af Gerd Hoffmann
        return NULL;
51 53e0d8af Gerd Hoffmann
    }
52 53e0d8af Gerd Hoffmann
    if (!((BusState*)bus)->allow_hotplug) {
53 53e0d8af Gerd Hoffmann
        monitor_printf(mon, "PCI bus doesn't support hotplug\n");
54 53e0d8af Gerd Hoffmann
        return NULL;
55 53e0d8af Gerd Hoffmann
    }
56 6f338c34 aliguori
57 8212c64f Markus Armbruster
    opts = qemu_opts_parse(&qemu_net_opts, opts_str ? opts_str : "", 0);
58 c59c7ea9 Mark McLoughlin
    if (!opts) {
59 c59c7ea9 Mark McLoughlin
        monitor_printf(mon, "parsing network options '%s' failed\n",
60 c59c7ea9 Mark McLoughlin
                       opts_str ? opts_str : "");
61 c59c7ea9 Mark McLoughlin
        return NULL;
62 c59c7ea9 Mark McLoughlin
    }
63 c59c7ea9 Mark McLoughlin
64 c59c7ea9 Mark McLoughlin
    qemu_opt_set(opts, "type", "nic");
65 c59c7ea9 Mark McLoughlin
66 f6b134ac Mark McLoughlin
    ret = net_client_init(mon, opts, 0);
67 eefb4091 aliguori
    if (ret < 0)
68 6f338c34 aliguori
        return NULL;
69 5607c388 Markus Armbruster
    if (nd_table[ret].devaddr) {
70 5607c388 Markus Armbruster
        monitor_printf(mon, "Parameter addr not supported\n");
71 5607c388 Markus Armbruster
        return NULL;
72 5607c388 Markus Armbruster
    }
73 1f5f6638 Markus Armbruster
    return pci_nic_init(&nd_table[ret], "rtl8139", devaddr);
74 6f338c34 aliguori
}
75 6f338c34 aliguori
76 6fdb03d5 Markus Armbruster
static int scsi_hot_add(Monitor *mon, DeviceState *adapter,
77 6fdb03d5 Markus Armbruster
                        DriveInfo *dinfo, int printinfo)
78 30d335d6 Gerd Hoffmann
{
79 30d335d6 Gerd Hoffmann
    SCSIBus *scsibus;
80 30d335d6 Gerd Hoffmann
    SCSIDevice *scsidev;
81 30d335d6 Gerd Hoffmann
82 30d335d6 Gerd Hoffmann
    scsibus = DO_UPCAST(SCSIBus, qbus, QLIST_FIRST(&adapter->child_bus));
83 30d335d6 Gerd Hoffmann
    if (!scsibus || strcmp(scsibus->qbus.info->name, "SCSI") != 0) {
84 1ecda02b Markus Armbruster
        error_report("Device is not a SCSI adapter");
85 30d335d6 Gerd Hoffmann
        return -1;
86 30d335d6 Gerd Hoffmann
    }
87 30d335d6 Gerd Hoffmann
88 30d335d6 Gerd Hoffmann
    /*
89 30d335d6 Gerd Hoffmann
     * drive_init() tries to find a default for dinfo->unit.  Doesn't
90 30d335d6 Gerd Hoffmann
     * work at all for hotplug though as we assign the device to a
91 30d335d6 Gerd Hoffmann
     * specific bus instead of the first bus with spare scsi ids.
92 30d335d6 Gerd Hoffmann
     *
93 30d335d6 Gerd Hoffmann
     * Ditch the calculated value and reload from option string (if
94 30d335d6 Gerd Hoffmann
     * specified).
95 30d335d6 Gerd Hoffmann
     */
96 30d335d6 Gerd Hoffmann
    dinfo->unit = qemu_opt_get_number(dinfo->opts, "unit", -1);
97 30d335d6 Gerd Hoffmann
    scsidev = scsi_bus_legacy_add_drive(scsibus, dinfo, dinfo->unit);
98 11f4d7f4 Gerd Hoffmann
    dinfo->unit = scsidev->id;
99 30d335d6 Gerd Hoffmann
100 30d335d6 Gerd Hoffmann
    if (printinfo)
101 6fdb03d5 Markus Armbruster
        monitor_printf(mon, "OK bus %d, unit %d\n",
102 6fdb03d5 Markus Armbruster
                       scsibus->busnr, scsidev->id);
103 30d335d6 Gerd Hoffmann
    return 0;
104 30d335d6 Gerd Hoffmann
}
105 30d335d6 Gerd Hoffmann
106 f18c16de Luiz Capitulino
void drive_hot_add(Monitor *mon, const QDict *qdict)
107 6f338c34 aliguori
{
108 6f338c34 aliguori
    int dom, pci_bus;
109 6f338c34 aliguori
    unsigned slot;
110 751c6a17 Gerd Hoffmann
    int type, bus;
111 6f338c34 aliguori
    PCIDevice *dev;
112 4db49dc0 Gerd Hoffmann
    DriveInfo *dinfo = NULL;
113 f18c16de Luiz Capitulino
    const char *pci_addr = qdict_get_str(qdict, "pci_addr");
114 f18c16de Luiz Capitulino
    const char *opts = qdict_get_str(qdict, "opts");
115 6f338c34 aliguori
116 751c6a17 Gerd Hoffmann
    dinfo = add_init_drive(opts);
117 751c6a17 Gerd Hoffmann
    if (!dinfo)
118 4db49dc0 Gerd Hoffmann
        goto err;
119 751c6a17 Gerd Hoffmann
    if (dinfo->devaddr) {
120 c2cc47a4 Markus Armbruster
        monitor_printf(mon, "Parameter addr not supported\n");
121 4db49dc0 Gerd Hoffmann
        goto err;
122 c2cc47a4 Markus Armbruster
    }
123 751c6a17 Gerd Hoffmann
    type = dinfo->type;
124 6f338c34 aliguori
    bus = drive_get_max_bus (type);
125 6f338c34 aliguori
126 6f338c34 aliguori
    switch (type) {
127 6f338c34 aliguori
    case IF_SCSI:
128 4db49dc0 Gerd Hoffmann
        if (pci_read_devaddr(mon, pci_addr, &dom, &pci_bus, &slot)) {
129 4db49dc0 Gerd Hoffmann
            goto err;
130 4db49dc0 Gerd Hoffmann
        }
131 c469e1dd Isaku Yamahata
        dev = pci_find_device(pci_find_root_bus(0), pci_bus, slot, 0);
132 4db49dc0 Gerd Hoffmann
        if (!dev) {
133 4db49dc0 Gerd Hoffmann
            monitor_printf(mon, "no pci device with address %s\n", pci_addr);
134 4db49dc0 Gerd Hoffmann
            goto err;
135 4db49dc0 Gerd Hoffmann
        }
136 6fdb03d5 Markus Armbruster
        if (scsi_hot_add(mon, &dev->qdev, dinfo, 1) != 0) {
137 30d335d6 Gerd Hoffmann
            goto err;
138 30d335d6 Gerd Hoffmann
        }
139 6f338c34 aliguori
        break;
140 7101174e Gerd Hoffmann
    case IF_NONE:
141 7101174e Gerd Hoffmann
        monitor_printf(mon, "OK\n");
142 7101174e Gerd Hoffmann
        break;
143 6f338c34 aliguori
    default:
144 376253ec aliguori
        monitor_printf(mon, "Can't hot-add drive to type %d\n", type);
145 4db49dc0 Gerd Hoffmann
        goto err;
146 6f338c34 aliguori
    }
147 4db49dc0 Gerd Hoffmann
    return;
148 6f338c34 aliguori
149 4db49dc0 Gerd Hoffmann
err:
150 4db49dc0 Gerd Hoffmann
    if (dinfo)
151 4db49dc0 Gerd Hoffmann
        drive_uninit(dinfo);
152 6f338c34 aliguori
    return;
153 6f338c34 aliguori
}
154 6f338c34 aliguori
155 1f5f6638 Markus Armbruster
static PCIDevice *qemu_pci_hot_add_storage(Monitor *mon,
156 1f5f6638 Markus Armbruster
                                           const char *devaddr,
157 376253ec aliguori
                                           const char *opts)
158 6f338c34 aliguori
{
159 1f5f6638 Markus Armbruster
    PCIDevice *dev;
160 06c79f4e Sebastian Herbszt
    DriveInfo *dinfo = NULL;
161 751c6a17 Gerd Hoffmann
    int type = -1;
162 6f338c34 aliguori
    char buf[128];
163 49bd1458 Markus Armbruster
    PCIBus *bus;
164 49bd1458 Markus Armbruster
    int devfn;
165 6f338c34 aliguori
166 6f338c34 aliguori
    if (get_param_value(buf, sizeof(buf), "if", opts)) {
167 6f338c34 aliguori
        if (!strcmp(buf, "scsi"))
168 6f338c34 aliguori
            type = IF_SCSI;
169 6f338c34 aliguori
        else if (!strcmp(buf, "virtio")) {
170 6f338c34 aliguori
            type = IF_VIRTIO;
171 8707ecca aliguori
        } else {
172 8707ecca aliguori
            monitor_printf(mon, "type %s not a hotpluggable PCI device.\n", buf);
173 1f5f6638 Markus Armbruster
            return NULL;
174 6f338c34 aliguori
        }
175 6f338c34 aliguori
    } else {
176 376253ec aliguori
        monitor_printf(mon, "no if= specified\n");
177 1f5f6638 Markus Armbruster
        return NULL;
178 6f338c34 aliguori
    }
179 6f338c34 aliguori
180 6f338c34 aliguori
    if (get_param_value(buf, sizeof(buf), "file", opts)) {
181 751c6a17 Gerd Hoffmann
        dinfo = add_init_drive(opts);
182 751c6a17 Gerd Hoffmann
        if (!dinfo)
183 1f5f6638 Markus Armbruster
            return NULL;
184 751c6a17 Gerd Hoffmann
        if (dinfo->devaddr) {
185 c2cc47a4 Markus Armbruster
            monitor_printf(mon, "Parameter addr not supported\n");
186 c2cc47a4 Markus Armbruster
            return NULL;
187 c2cc47a4 Markus Armbruster
        }
188 7432ff5d Blue Swirl
    } else {
189 7432ff5d Blue Swirl
        dinfo = NULL;
190 6f338c34 aliguori
    }
191 6f338c34 aliguori
192 49bd1458 Markus Armbruster
    bus = pci_get_bus_devfn(&devfn, devaddr);
193 49bd1458 Markus Armbruster
    if (!bus) {
194 49bd1458 Markus Armbruster
        monitor_printf(mon, "Invalid PCI device address %s\n", devaddr);
195 49bd1458 Markus Armbruster
        return NULL;
196 49bd1458 Markus Armbruster
    }
197 53e0d8af Gerd Hoffmann
    if (!((BusState*)bus)->allow_hotplug) {
198 53e0d8af Gerd Hoffmann
        monitor_printf(mon, "PCI bus doesn't support hotplug\n");
199 53e0d8af Gerd Hoffmann
        return NULL;
200 53e0d8af Gerd Hoffmann
    }
201 49bd1458 Markus Armbruster
202 6f338c34 aliguori
    switch (type) {
203 6f338c34 aliguori
    case IF_SCSI:
204 499cf102 Markus Armbruster
        dev = pci_create(bus, devfn, "lsi53c895a");
205 5b684b5a Gerd Hoffmann
        if (qdev_init(&dev->qdev) < 0)
206 5b684b5a Gerd Hoffmann
            dev = NULL;
207 ec7efac4 Daniel P. Berrange
        if (dev && dinfo) {
208 6fdb03d5 Markus Armbruster
            if (scsi_hot_add(mon, &dev->qdev, dinfo, 0) != 0) {
209 30d335d6 Gerd Hoffmann
                qdev_unplug(&dev->qdev);
210 30d335d6 Gerd Hoffmann
                dev = NULL;
211 30d335d6 Gerd Hoffmann
            }
212 5b684b5a Gerd Hoffmann
        }
213 6f338c34 aliguori
        break;
214 6f338c34 aliguori
    case IF_VIRTIO:
215 7432ff5d Blue Swirl
        if (!dinfo) {
216 7432ff5d Blue Swirl
            monitor_printf(mon, "virtio requires a backing file/device.\n");
217 7432ff5d Blue Swirl
            return NULL;
218 7432ff5d Blue Swirl
        }
219 499cf102 Markus Armbruster
        dev = pci_create(bus, devfn, "virtio-blk-pci");
220 d176c495 Gerd Hoffmann
        qdev_prop_set_drive(&dev->qdev, "drive", dinfo);
221 5b684b5a Gerd Hoffmann
        if (qdev_init(&dev->qdev) < 0)
222 5b684b5a Gerd Hoffmann
            dev = NULL;
223 6f338c34 aliguori
        break;
224 1f5f6638 Markus Armbruster
    default:
225 1f5f6638 Markus Armbruster
        dev = NULL;
226 6f338c34 aliguori
    }
227 1f5f6638 Markus Armbruster
    return dev;
228 6f338c34 aliguori
}
229 6f338c34 aliguori
230 7a344f7a Luiz Capitulino
void pci_device_hot_add_print(Monitor *mon, const QObject *data)
231 7a344f7a Luiz Capitulino
{
232 7a344f7a Luiz Capitulino
    QDict *qdict;
233 7a344f7a Luiz Capitulino
234 7a344f7a Luiz Capitulino
    assert(qobject_type(data) == QTYPE_QDICT);
235 7a344f7a Luiz Capitulino
    qdict = qobject_to_qdict(data);
236 7a344f7a Luiz Capitulino
237 7a344f7a Luiz Capitulino
    monitor_printf(mon, "OK domain %d, bus %d, slot %d, function %d\n",
238 7a344f7a Luiz Capitulino
                   (int) qdict_get_int(qdict, "domain"),
239 7a344f7a Luiz Capitulino
                   (int) qdict_get_int(qdict, "bus"),
240 7a344f7a Luiz Capitulino
                   (int) qdict_get_int(qdict, "slot"),
241 7a344f7a Luiz Capitulino
                   (int) qdict_get_int(qdict, "function"));
242 7a344f7a Luiz Capitulino
243 7a344f7a Luiz Capitulino
}
244 7a344f7a Luiz Capitulino
245 7a344f7a Luiz Capitulino
/**
246 7a344f7a Luiz Capitulino
 * pci_device_hot_add(): Hot add a PCI device
247 7a344f7a Luiz Capitulino
 *
248 7a344f7a Luiz Capitulino
 * Return a QDict with the following device information:
249 7a344f7a Luiz Capitulino
 *
250 7a344f7a Luiz Capitulino
 * - "domain": domain number
251 7a344f7a Luiz Capitulino
 * - "bus": bus number
252 7a344f7a Luiz Capitulino
 * - "slot": slot number
253 7a344f7a Luiz Capitulino
 * - "function": function number
254 7a344f7a Luiz Capitulino
 *
255 7a344f7a Luiz Capitulino
 * Example:
256 7a344f7a Luiz Capitulino
 *
257 7a344f7a Luiz Capitulino
 * { "domain": 0, "bus": 0, "slot": 5, "function": 0 }
258 7a344f7a Luiz Capitulino
 */
259 395560c8 Luiz Capitulino
int pci_device_hot_add(Monitor *mon, const QDict *qdict, QObject **ret_data)
260 6f338c34 aliguori
{
261 6f338c34 aliguori
    PCIDevice *dev = NULL;
262 1d4daa91 Luiz Capitulino
    const char *pci_addr = qdict_get_str(qdict, "pci_addr");
263 1d4daa91 Luiz Capitulino
    const char *type = qdict_get_str(qdict, "type");
264 1d4daa91 Luiz Capitulino
    const char *opts = qdict_get_try_str(qdict, "opts");
265 6f338c34 aliguori
266 e9283f8b Jan Kiszka
    /* strip legacy tag */
267 e9283f8b Jan Kiszka
    if (!strncmp(pci_addr, "pci_addr=", 9)) {
268 e9283f8b Jan Kiszka
        pci_addr += 9;
269 6f338c34 aliguori
    }
270 6f338c34 aliguori
271 a62acdc0 Jan Kiszka
    if (!opts) {
272 a62acdc0 Jan Kiszka
        opts = "";
273 a62acdc0 Jan Kiszka
    }
274 a62acdc0 Jan Kiszka
275 e9283f8b Jan Kiszka
    if (!strcmp(pci_addr, "auto"))
276 e9283f8b Jan Kiszka
        pci_addr = NULL;
277 6f338c34 aliguori
278 395560c8 Luiz Capitulino
    if (strcmp(type, "nic") == 0) {
279 e9283f8b Jan Kiszka
        dev = qemu_pci_hot_add_nic(mon, pci_addr, opts);
280 395560c8 Luiz Capitulino
    } else if (strcmp(type, "storage") == 0) {
281 e9283f8b Jan Kiszka
        dev = qemu_pci_hot_add_storage(mon, pci_addr, opts);
282 395560c8 Luiz Capitulino
    } else {
283 376253ec aliguori
        monitor_printf(mon, "invalid type: %s\n", type);
284 395560c8 Luiz Capitulino
        return -1;
285 395560c8 Luiz Capitulino
    }
286 6f338c34 aliguori
287 6f338c34 aliguori
    if (dev) {
288 7a344f7a Luiz Capitulino
        *ret_data =
289 7a344f7a Luiz Capitulino
        qobject_from_jsonf("{ 'domain': 0, 'bus': %d, 'slot': %d, "
290 7a344f7a Luiz Capitulino
                           "'function': %d }", pci_bus_num(dev->bus),
291 7a344f7a Luiz Capitulino
                           PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
292 395560c8 Luiz Capitulino
    } else {
293 376253ec aliguori
        monitor_printf(mon, "failed to add %s\n", opts);
294 395560c8 Luiz Capitulino
        return -1;
295 395560c8 Luiz Capitulino
    }
296 395560c8 Luiz Capitulino
297 395560c8 Luiz Capitulino
    return 0;
298 6f338c34 aliguori
}
299 6f338c34 aliguori
#endif
300 6f338c34 aliguori
301 053801bc Luiz Capitulino
int pci_device_hot_remove(Monitor *mon, const char *pci_addr)
302 6f338c34 aliguori
{
303 6f338c34 aliguori
    PCIDevice *d;
304 6f338c34 aliguori
    int dom, bus;
305 6f338c34 aliguori
    unsigned slot;
306 6f338c34 aliguori
307 e9283f8b Jan Kiszka
    if (pci_read_devaddr(mon, pci_addr, &dom, &bus, &slot)) {
308 053801bc Luiz Capitulino
        return -1;
309 6f338c34 aliguori
    }
310 6f338c34 aliguori
311 c469e1dd Isaku Yamahata
    d = pci_find_device(pci_find_root_bus(0), bus, slot, 0);
312 6f338c34 aliguori
    if (!d) {
313 376253ec aliguori
        monitor_printf(mon, "slot %d empty\n", slot);
314 053801bc Luiz Capitulino
        return -1;
315 6f338c34 aliguori
    }
316 053801bc Luiz Capitulino
    return qdev_unplug(&d->qdev);
317 6f338c34 aliguori
}
318 6f338c34 aliguori
319 053801bc Luiz Capitulino
int do_pci_device_hot_remove(Monitor *mon, const QDict *qdict,
320 053801bc Luiz Capitulino
                             QObject **ret_data)
321 38183186 Luiz Capitulino
{
322 053801bc Luiz Capitulino
    return pci_device_hot_remove(mon, qdict_get_str(qdict, "pci_addr"));
323 38183186 Luiz Capitulino
}