Statistics
| Branch: | Revision:

root / hw / pci-hotplug.c @ 4e4fa398

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