Statistics
| Branch: | Revision:

root / hw / pci-hotplug.c @ 1237ad76

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