Statistics
| Branch: | Revision:

root / hw / pci / pci-hotplug.c @ 062db740

History | View | Annotate | Download (8.4 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 c759b24f Michael S. Tsirkin
#include "hw/hw.h"
26 c759b24f Michael S. Tsirkin
#include "hw/boards.h"
27 c759b24f Michael S. Tsirkin
#include "hw/pci/pci.h"
28 1422e32d Paolo Bonzini
#include "net/net.h"
29 0d09e41a Paolo Bonzini
#include "hw/i386/pc.h"
30 83c9089e Paolo Bonzini
#include "monitor/monitor.h"
31 0d09e41a Paolo Bonzini
#include "hw/scsi/scsi.h"
32 0d09e41a Paolo Bonzini
#include "hw/virtio/virtio-blk.h"
33 1de7afc9 Paolo Bonzini
#include "qemu/config-file.h"
34 9c17d615 Paolo Bonzini
#include "sysemu/blockdev.h"
35 7b1b5d19 Paolo Bonzini
#include "qapi/error.h"
36 6f338c34 aliguori
37 ce88f890 Juan Quintela
#if defined(TARGET_I386)
38 1f5f6638 Markus Armbruster
static PCIDevice *qemu_pci_hot_add_nic(Monitor *mon,
39 c59c7ea9 Mark McLoughlin
                                       const char *devaddr,
40 c59c7ea9 Mark McLoughlin
                                       const char *opts_str)
41 6f338c34 aliguori
{
42 4559a1db Luiz Capitulino
    Error *local_err = NULL;
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 3329f07b Gerd Hoffmann
    opts = qemu_opts_parse(qemu_find_opts("net"), opts_str ? opts_str : "", 0);
58 c59c7ea9 Mark McLoughlin
    if (!opts) {
59 c59c7ea9 Mark McLoughlin
        return NULL;
60 c59c7ea9 Mark McLoughlin
    }
61 c59c7ea9 Mark McLoughlin
62 c59c7ea9 Mark McLoughlin
    qemu_opt_set(opts, "type", "nic");
63 c59c7ea9 Mark McLoughlin
64 4559a1db Luiz Capitulino
    ret = net_client_init(opts, 0, &local_err);
65 4559a1db Luiz Capitulino
    if (error_is_set(&local_err)) {
66 4559a1db Luiz Capitulino
        qerror_report_err(local_err);
67 4559a1db Luiz Capitulino
        error_free(local_err);
68 6f338c34 aliguori
        return NULL;
69 4559a1db Luiz Capitulino
    }
70 5607c388 Markus Armbruster
    if (nd_table[ret].devaddr) {
71 5607c388 Markus Armbruster
        monitor_printf(mon, "Parameter addr not supported\n");
72 5607c388 Markus Armbruster
        return NULL;
73 5607c388 Markus Armbruster
    }
74 1f5f6638 Markus Armbruster
    return pci_nic_init(&nd_table[ret], "rtl8139", devaddr);
75 6f338c34 aliguori
}
76 6f338c34 aliguori
77 6fdb03d5 Markus Armbruster
static int scsi_hot_add(Monitor *mon, DeviceState *adapter,
78 6fdb03d5 Markus Armbruster
                        DriveInfo *dinfo, int printinfo)
79 30d335d6 Gerd Hoffmann
{
80 30d335d6 Gerd Hoffmann
    SCSIBus *scsibus;
81 30d335d6 Gerd Hoffmann
    SCSIDevice *scsidev;
82 30d335d6 Gerd Hoffmann
83 b5007bcc Paolo Bonzini
    scsibus = (SCSIBus *)
84 b5007bcc Paolo Bonzini
        object_dynamic_cast(OBJECT(QLIST_FIRST(&adapter->child_bus)),
85 b5007bcc Paolo Bonzini
                            TYPE_SCSI_BUS);
86 b5007bcc Paolo Bonzini
    if (!scsibus) {
87 b5007bcc Paolo Bonzini
        error_report("Device is not a SCSI adapter");
88 b5007bcc Paolo Bonzini
        return -1;
89 b5007bcc Paolo Bonzini
    }
90 30d335d6 Gerd Hoffmann
91 30d335d6 Gerd Hoffmann
    /*
92 30d335d6 Gerd Hoffmann
     * drive_init() tries to find a default for dinfo->unit.  Doesn't
93 30d335d6 Gerd Hoffmann
     * work at all for hotplug though as we assign the device to a
94 30d335d6 Gerd Hoffmann
     * specific bus instead of the first bus with spare scsi ids.
95 30d335d6 Gerd Hoffmann
     *
96 30d335d6 Gerd Hoffmann
     * Ditch the calculated value and reload from option string (if
97 30d335d6 Gerd Hoffmann
     * specified).
98 30d335d6 Gerd Hoffmann
     */
99 30d335d6 Gerd Hoffmann
    dinfo->unit = qemu_opt_get_number(dinfo->opts, "unit", -1);
100 31e1ea3e Markus Armbruster
    dinfo->bus = scsibus->busnr;
101 ce4e7e46 Paolo Bonzini
    scsidev = scsi_bus_legacy_add_drive(scsibus, dinfo->bdrv, dinfo->unit,
102 76534da7 Kevin Wolf
                                        false, -1, NULL);
103 fa66b909 Markus Armbruster
    if (!scsidev) {
104 fa66b909 Markus Armbruster
        return -1;
105 fa66b909 Markus Armbruster
    }
106 11f4d7f4 Gerd Hoffmann
    dinfo->unit = scsidev->id;
107 30d335d6 Gerd Hoffmann
108 30d335d6 Gerd Hoffmann
    if (printinfo)
109 6fdb03d5 Markus Armbruster
        monitor_printf(mon, "OK bus %d, unit %d\n",
110 6fdb03d5 Markus Armbruster
                       scsibus->busnr, scsidev->id);
111 30d335d6 Gerd Hoffmann
    return 0;
112 30d335d6 Gerd Hoffmann
}
113 30d335d6 Gerd Hoffmann
114 4dbd84e2 Markus Armbruster
int pci_drive_hot_add(Monitor *mon, const QDict *qdict, DriveInfo *dinfo)
115 6f338c34 aliguori
{
116 6f338c34 aliguori
    int dom, pci_bus;
117 6f338c34 aliguori
    unsigned slot;
118 6f338c34 aliguori
    PCIDevice *dev;
119 f18c16de Luiz Capitulino
    const char *pci_addr = qdict_get_str(qdict, "pci_addr");
120 6f338c34 aliguori
121 4dbd84e2 Markus Armbruster
    switch (dinfo->type) {
122 6f338c34 aliguori
    case IF_SCSI:
123 4db49dc0 Gerd Hoffmann
        if (pci_read_devaddr(mon, pci_addr, &dom, &pci_bus, &slot)) {
124 4db49dc0 Gerd Hoffmann
            goto err;
125 4db49dc0 Gerd Hoffmann
        }
126 5256d8bf Isaku Yamahata
        dev = pci_find_device(pci_find_root_bus(dom), pci_bus,
127 5256d8bf Isaku Yamahata
                              PCI_DEVFN(slot, 0));
128 4db49dc0 Gerd Hoffmann
        if (!dev) {
129 4db49dc0 Gerd Hoffmann
            monitor_printf(mon, "no pci device with address %s\n", pci_addr);
130 4db49dc0 Gerd Hoffmann
            goto err;
131 4db49dc0 Gerd Hoffmann
        }
132 6fdb03d5 Markus Armbruster
        if (scsi_hot_add(mon, &dev->qdev, dinfo, 1) != 0) {
133 30d335d6 Gerd Hoffmann
            goto err;
134 30d335d6 Gerd Hoffmann
        }
135 6f338c34 aliguori
        break;
136 6f338c34 aliguori
    default:
137 4dbd84e2 Markus Armbruster
        monitor_printf(mon, "Can't hot-add drive to type %d\n", dinfo->type);
138 4db49dc0 Gerd Hoffmann
        goto err;
139 6f338c34 aliguori
    }
140 6f338c34 aliguori
141 dd97aa8a Alexander Graf
    return 0;
142 4db49dc0 Gerd Hoffmann
err:
143 dd97aa8a Alexander Graf
    return -1;
144 6f338c34 aliguori
}
145 6f338c34 aliguori
146 1f5f6638 Markus Armbruster
static PCIDevice *qemu_pci_hot_add_storage(Monitor *mon,
147 1f5f6638 Markus Armbruster
                                           const char *devaddr,
148 376253ec aliguori
                                           const char *opts)
149 6f338c34 aliguori
{
150 1f5f6638 Markus Armbruster
    PCIDevice *dev;
151 06c79f4e Sebastian Herbszt
    DriveInfo *dinfo = NULL;
152 751c6a17 Gerd Hoffmann
    int type = -1;
153 6f338c34 aliguori
    char buf[128];
154 49bd1458 Markus Armbruster
    PCIBus *bus;
155 49bd1458 Markus Armbruster
    int devfn;
156 6f338c34 aliguori
157 6f338c34 aliguori
    if (get_param_value(buf, sizeof(buf), "if", opts)) {
158 6f338c34 aliguori
        if (!strcmp(buf, "scsi"))
159 6f338c34 aliguori
            type = IF_SCSI;
160 6f338c34 aliguori
        else if (!strcmp(buf, "virtio")) {
161 6f338c34 aliguori
            type = IF_VIRTIO;
162 8707ecca aliguori
        } else {
163 8707ecca aliguori
            monitor_printf(mon, "type %s not a hotpluggable PCI device.\n", buf);
164 1f5f6638 Markus Armbruster
            return NULL;
165 6f338c34 aliguori
        }
166 6f338c34 aliguori
    } else {
167 376253ec aliguori
        monitor_printf(mon, "no if= specified\n");
168 1f5f6638 Markus Armbruster
        return NULL;
169 6f338c34 aliguori
    }
170 6f338c34 aliguori
171 6f338c34 aliguori
    if (get_param_value(buf, sizeof(buf), "file", opts)) {
172 751c6a17 Gerd Hoffmann
        dinfo = add_init_drive(opts);
173 751c6a17 Gerd Hoffmann
        if (!dinfo)
174 1f5f6638 Markus Armbruster
            return NULL;
175 751c6a17 Gerd Hoffmann
        if (dinfo->devaddr) {
176 c2cc47a4 Markus Armbruster
            monitor_printf(mon, "Parameter addr not supported\n");
177 c2cc47a4 Markus Armbruster
            return NULL;
178 c2cc47a4 Markus Armbruster
        }
179 7432ff5d Blue Swirl
    } else {
180 7432ff5d Blue Swirl
        dinfo = NULL;
181 6f338c34 aliguori
    }
182 6f338c34 aliguori
183 49bd1458 Markus Armbruster
    bus = pci_get_bus_devfn(&devfn, devaddr);
184 49bd1458 Markus Armbruster
    if (!bus) {
185 49bd1458 Markus Armbruster
        monitor_printf(mon, "Invalid PCI device address %s\n", devaddr);
186 49bd1458 Markus Armbruster
        return NULL;
187 49bd1458 Markus Armbruster
    }
188 53e0d8af Gerd Hoffmann
    if (!((BusState*)bus)->allow_hotplug) {
189 53e0d8af Gerd Hoffmann
        monitor_printf(mon, "PCI bus doesn't support hotplug\n");
190 53e0d8af Gerd Hoffmann
        return NULL;
191 53e0d8af Gerd Hoffmann
    }
192 49bd1458 Markus Armbruster
193 6f338c34 aliguori
    switch (type) {
194 6f338c34 aliguori
    case IF_SCSI:
195 499cf102 Markus Armbruster
        dev = pci_create(bus, devfn, "lsi53c895a");
196 5b684b5a Gerd Hoffmann
        if (qdev_init(&dev->qdev) < 0)
197 5b684b5a Gerd Hoffmann
            dev = NULL;
198 ec7efac4 Daniel P. Berrange
        if (dev && dinfo) {
199 6fdb03d5 Markus Armbruster
            if (scsi_hot_add(mon, &dev->qdev, dinfo, 0) != 0) {
200 56f9107e Luiz Capitulino
                qdev_unplug(&dev->qdev, NULL);
201 30d335d6 Gerd Hoffmann
                dev = NULL;
202 30d335d6 Gerd Hoffmann
            }
203 5b684b5a Gerd Hoffmann
        }
204 6f338c34 aliguori
        break;
205 6f338c34 aliguori
    case IF_VIRTIO:
206 7432ff5d Blue Swirl
        if (!dinfo) {
207 7432ff5d Blue Swirl
            monitor_printf(mon, "virtio requires a backing file/device.\n");
208 7432ff5d Blue Swirl
            return NULL;
209 7432ff5d Blue Swirl
        }
210 499cf102 Markus Armbruster
        dev = pci_create(bus, devfn, "virtio-blk-pci");
211 18846dee Markus Armbruster
        if (qdev_prop_set_drive(&dev->qdev, "drive", dinfo->bdrv) < 0) {
212 18846dee Markus Armbruster
            qdev_free(&dev->qdev);
213 18846dee Markus Armbruster
            dev = NULL;
214 18846dee Markus Armbruster
            break;
215 18846dee Markus Armbruster
        }
216 5b684b5a Gerd Hoffmann
        if (qdev_init(&dev->qdev) < 0)
217 5b684b5a Gerd Hoffmann
            dev = NULL;
218 6f338c34 aliguori
        break;
219 1f5f6638 Markus Armbruster
    default:
220 1f5f6638 Markus Armbruster
        dev = NULL;
221 6f338c34 aliguori
    }
222 1f5f6638 Markus Armbruster
    return dev;
223 6f338c34 aliguori
}
224 6f338c34 aliguori
225 6c6a58ae Markus Armbruster
void pci_device_hot_add(Monitor *mon, const QDict *qdict)
226 6f338c34 aliguori
{
227 6f338c34 aliguori
    PCIDevice *dev = NULL;
228 1d4daa91 Luiz Capitulino
    const char *pci_addr = qdict_get_str(qdict, "pci_addr");
229 1d4daa91 Luiz Capitulino
    const char *type = qdict_get_str(qdict, "type");
230 1d4daa91 Luiz Capitulino
    const char *opts = qdict_get_try_str(qdict, "opts");
231 6f338c34 aliguori
232 e9283f8b Jan Kiszka
    /* strip legacy tag */
233 e9283f8b Jan Kiszka
    if (!strncmp(pci_addr, "pci_addr=", 9)) {
234 e9283f8b Jan Kiszka
        pci_addr += 9;
235 6f338c34 aliguori
    }
236 6f338c34 aliguori
237 a62acdc0 Jan Kiszka
    if (!opts) {
238 a62acdc0 Jan Kiszka
        opts = "";
239 a62acdc0 Jan Kiszka
    }
240 a62acdc0 Jan Kiszka
241 e9283f8b Jan Kiszka
    if (!strcmp(pci_addr, "auto"))
242 e9283f8b Jan Kiszka
        pci_addr = NULL;
243 6f338c34 aliguori
244 395560c8 Luiz Capitulino
    if (strcmp(type, "nic") == 0) {
245 e9283f8b Jan Kiszka
        dev = qemu_pci_hot_add_nic(mon, pci_addr, opts);
246 395560c8 Luiz Capitulino
    } else if (strcmp(type, "storage") == 0) {
247 e9283f8b Jan Kiszka
        dev = qemu_pci_hot_add_storage(mon, pci_addr, opts);
248 395560c8 Luiz Capitulino
    } else {
249 376253ec aliguori
        monitor_printf(mon, "invalid type: %s\n", type);
250 395560c8 Luiz Capitulino
    }
251 6f338c34 aliguori
252 6f338c34 aliguori
    if (dev) {
253 6c6a58ae Markus Armbruster
        monitor_printf(mon, "OK domain %d, bus %d, slot %d, function %d\n",
254 e075e788 Isaku Yamahata
                       pci_find_domain(dev->bus),
255 e075e788 Isaku Yamahata
                       pci_bus_num(dev->bus), PCI_SLOT(dev->devfn),
256 6c6a58ae Markus Armbruster
                       PCI_FUNC(dev->devfn));
257 6c6a58ae Markus Armbruster
    } else
258 376253ec aliguori
        monitor_printf(mon, "failed to add %s\n", opts);
259 6f338c34 aliguori
}
260 6f338c34 aliguori
#endif
261 6f338c34 aliguori
262 f2b07c92 Isaku Yamahata
static int pci_device_hot_remove(Monitor *mon, const char *pci_addr)
263 6f338c34 aliguori
{
264 6f338c34 aliguori
    PCIDevice *d;
265 6f338c34 aliguori
    int dom, bus;
266 6f338c34 aliguori
    unsigned slot;
267 56f9107e Luiz Capitulino
    Error *local_err = NULL;
268 6f338c34 aliguori
269 e9283f8b Jan Kiszka
    if (pci_read_devaddr(mon, pci_addr, &dom, &bus, &slot)) {
270 053801bc Luiz Capitulino
        return -1;
271 6f338c34 aliguori
    }
272 6f338c34 aliguori
273 5256d8bf Isaku Yamahata
    d = pci_find_device(pci_find_root_bus(dom), bus, PCI_DEVFN(slot, 0));
274 6f338c34 aliguori
    if (!d) {
275 376253ec aliguori
        monitor_printf(mon, "slot %d empty\n", slot);
276 053801bc Luiz Capitulino
        return -1;
277 6f338c34 aliguori
    }
278 56f9107e Luiz Capitulino
279 56f9107e Luiz Capitulino
    qdev_unplug(&d->qdev, &local_err);
280 56f9107e Luiz Capitulino
    if (error_is_set(&local_err)) {
281 56f9107e Luiz Capitulino
        monitor_printf(mon, "%s\n", error_get_pretty(local_err));
282 56f9107e Luiz Capitulino
        error_free(local_err);
283 56f9107e Luiz Capitulino
        return -1;
284 56f9107e Luiz Capitulino
    }
285 56f9107e Luiz Capitulino
286 56f9107e Luiz Capitulino
    return 0;
287 6f338c34 aliguori
}
288 6f338c34 aliguori
289 b752daf0 Markus Armbruster
void do_pci_device_hot_remove(Monitor *mon, const QDict *qdict)
290 38183186 Luiz Capitulino
{
291 b752daf0 Markus Armbruster
    pci_device_hot_remove(mon, qdict_get_str(qdict, "pci_addr"));
292 38183186 Luiz Capitulino
}