Statistics
| Branch: | Revision:

root / hw / pci-hotplug.c @ a9f49946

History | View | Annotate | Download (7.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 "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 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 c59c7ea9 Mark McLoughlin
    QemuOpts *opts;
43 6f338c34 aliguori
    int ret;
44 6f338c34 aliguori
45 c59c7ea9 Mark McLoughlin
    opts = qemu_opts_parse(&qemu_net_opts, opts_str ? opts_str : "", NULL);
46 c59c7ea9 Mark McLoughlin
    if (!opts) {
47 c59c7ea9 Mark McLoughlin
        monitor_printf(mon, "parsing network options '%s' failed\n",
48 c59c7ea9 Mark McLoughlin
                       opts_str ? opts_str : "");
49 c59c7ea9 Mark McLoughlin
        return NULL;
50 c59c7ea9 Mark McLoughlin
    }
51 c59c7ea9 Mark McLoughlin
52 c59c7ea9 Mark McLoughlin
    qemu_opt_set(opts, "type", "nic");
53 c59c7ea9 Mark McLoughlin
54 f6b134ac Mark McLoughlin
    ret = net_client_init(mon, opts, 0);
55 eefb4091 aliguori
    if (ret < 0)
56 6f338c34 aliguori
        return NULL;
57 5607c388 Markus Armbruster
    if (nd_table[ret].devaddr) {
58 5607c388 Markus Armbruster
        monitor_printf(mon, "Parameter addr not supported\n");
59 5607c388 Markus Armbruster
        return NULL;
60 5607c388 Markus Armbruster
    }
61 1f5f6638 Markus Armbruster
    return pci_nic_init(&nd_table[ret], "rtl8139", devaddr);
62 6f338c34 aliguori
}
63 6f338c34 aliguori
64 30d335d6 Gerd Hoffmann
static int scsi_hot_add(DeviceState *adapter, DriveInfo *dinfo, int printinfo)
65 30d335d6 Gerd Hoffmann
{
66 30d335d6 Gerd Hoffmann
    SCSIBus *scsibus;
67 30d335d6 Gerd Hoffmann
    SCSIDevice *scsidev;
68 30d335d6 Gerd Hoffmann
69 30d335d6 Gerd Hoffmann
    scsibus = DO_UPCAST(SCSIBus, qbus, QLIST_FIRST(&adapter->child_bus));
70 30d335d6 Gerd Hoffmann
    if (!scsibus || strcmp(scsibus->qbus.info->name, "SCSI") != 0) {
71 30d335d6 Gerd Hoffmann
        qemu_error("Device is not a SCSI adapter\n");
72 30d335d6 Gerd Hoffmann
        return -1;
73 30d335d6 Gerd Hoffmann
    }
74 30d335d6 Gerd Hoffmann
75 30d335d6 Gerd Hoffmann
    /*
76 30d335d6 Gerd Hoffmann
     * drive_init() tries to find a default for dinfo->unit.  Doesn't
77 30d335d6 Gerd Hoffmann
     * work at all for hotplug though as we assign the device to a
78 30d335d6 Gerd Hoffmann
     * specific bus instead of the first bus with spare scsi ids.
79 30d335d6 Gerd Hoffmann
     *
80 30d335d6 Gerd Hoffmann
     * Ditch the calculated value and reload from option string (if
81 30d335d6 Gerd Hoffmann
     * specified).
82 30d335d6 Gerd Hoffmann
     */
83 30d335d6 Gerd Hoffmann
    dinfo->unit = qemu_opt_get_number(dinfo->opts, "unit", -1);
84 30d335d6 Gerd Hoffmann
    scsidev = scsi_bus_legacy_add_drive(scsibus, dinfo, dinfo->unit);
85 30d335d6 Gerd Hoffmann
86 30d335d6 Gerd Hoffmann
    if (printinfo)
87 30d335d6 Gerd Hoffmann
        qemu_error("OK bus %d, unit %d\n", scsibus->busnr, scsidev->id);
88 30d335d6 Gerd Hoffmann
    return 0;
89 30d335d6 Gerd Hoffmann
}
90 30d335d6 Gerd Hoffmann
91 f18c16de Luiz Capitulino
void drive_hot_add(Monitor *mon, const QDict *qdict)
92 6f338c34 aliguori
{
93 6f338c34 aliguori
    int dom, pci_bus;
94 6f338c34 aliguori
    unsigned slot;
95 751c6a17 Gerd Hoffmann
    int type, bus;
96 6f338c34 aliguori
    PCIDevice *dev;
97 4db49dc0 Gerd Hoffmann
    DriveInfo *dinfo = NULL;
98 f18c16de Luiz Capitulino
    const char *pci_addr = qdict_get_str(qdict, "pci_addr");
99 f18c16de Luiz Capitulino
    const char *opts = qdict_get_str(qdict, "opts");
100 6f338c34 aliguori
101 751c6a17 Gerd Hoffmann
    dinfo = add_init_drive(opts);
102 751c6a17 Gerd Hoffmann
    if (!dinfo)
103 4db49dc0 Gerd Hoffmann
        goto err;
104 751c6a17 Gerd Hoffmann
    if (dinfo->devaddr) {
105 c2cc47a4 Markus Armbruster
        monitor_printf(mon, "Parameter addr not supported\n");
106 4db49dc0 Gerd Hoffmann
        goto err;
107 c2cc47a4 Markus Armbruster
    }
108 751c6a17 Gerd Hoffmann
    type = dinfo->type;
109 6f338c34 aliguori
    bus = drive_get_max_bus (type);
110 6f338c34 aliguori
111 6f338c34 aliguori
    switch (type) {
112 6f338c34 aliguori
    case IF_SCSI:
113 4db49dc0 Gerd Hoffmann
        if (pci_read_devaddr(mon, pci_addr, &dom, &pci_bus, &slot)) {
114 4db49dc0 Gerd Hoffmann
            goto err;
115 4db49dc0 Gerd Hoffmann
        }
116 e822a52a Isaku Yamahata
        dev = pci_find_device(pci_find_host_bus(0), pci_bus, slot, 0);
117 4db49dc0 Gerd Hoffmann
        if (!dev) {
118 4db49dc0 Gerd Hoffmann
            monitor_printf(mon, "no pci device with address %s\n", pci_addr);
119 4db49dc0 Gerd Hoffmann
            goto err;
120 4db49dc0 Gerd Hoffmann
        }
121 30d335d6 Gerd Hoffmann
        if (scsi_hot_add(&dev->qdev, dinfo, 1) != 0) {
122 30d335d6 Gerd Hoffmann
            goto err;
123 30d335d6 Gerd Hoffmann
        }
124 6f338c34 aliguori
        break;
125 7101174e Gerd Hoffmann
    case IF_NONE:
126 7101174e Gerd Hoffmann
        monitor_printf(mon, "OK\n");
127 7101174e Gerd Hoffmann
        break;
128 6f338c34 aliguori
    default:
129 376253ec aliguori
        monitor_printf(mon, "Can't hot-add drive to type %d\n", type);
130 4db49dc0 Gerd Hoffmann
        goto err;
131 6f338c34 aliguori
    }
132 4db49dc0 Gerd Hoffmann
    return;
133 6f338c34 aliguori
134 4db49dc0 Gerd Hoffmann
err:
135 4db49dc0 Gerd Hoffmann
    if (dinfo)
136 4db49dc0 Gerd Hoffmann
        drive_uninit(dinfo);
137 6f338c34 aliguori
    return;
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 49bd1458 Markus Armbruster
183 6f338c34 aliguori
    switch (type) {
184 6f338c34 aliguori
    case IF_SCSI:
185 5b684b5a Gerd Hoffmann
        if (!dinfo) {
186 5b684b5a Gerd Hoffmann
            monitor_printf(mon, "scsi requires a backing file/device.\n");
187 5b684b5a Gerd Hoffmann
            return NULL;
188 5b684b5a Gerd Hoffmann
        }
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 5b684b5a Gerd Hoffmann
        if (dev) {
193 30d335d6 Gerd Hoffmann
            if (scsi_hot_add(&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 d176c495 Gerd Hoffmann
        qdev_prop_set_drive(&dev->qdev, "drive", dinfo);
206 5b684b5a Gerd Hoffmann
        if (qdev_init(&dev->qdev) < 0)
207 5b684b5a Gerd Hoffmann
            dev = NULL;
208 6f338c34 aliguori
        break;
209 1f5f6638 Markus Armbruster
    default:
210 1f5f6638 Markus Armbruster
        dev = NULL;
211 6f338c34 aliguori
    }
212 1f5f6638 Markus Armbruster
    return dev;
213 6f338c34 aliguori
}
214 6f338c34 aliguori
215 1d4daa91 Luiz Capitulino
void pci_device_hot_add(Monitor *mon, const QDict *qdict)
216 6f338c34 aliguori
{
217 6f338c34 aliguori
    PCIDevice *dev = NULL;
218 1d4daa91 Luiz Capitulino
    const char *pci_addr = qdict_get_str(qdict, "pci_addr");
219 1d4daa91 Luiz Capitulino
    const char *type = qdict_get_str(qdict, "type");
220 1d4daa91 Luiz Capitulino
    const char *opts = qdict_get_try_str(qdict, "opts");
221 6f338c34 aliguori
222 e9283f8b Jan Kiszka
    /* strip legacy tag */
223 e9283f8b Jan Kiszka
    if (!strncmp(pci_addr, "pci_addr=", 9)) {
224 e9283f8b Jan Kiszka
        pci_addr += 9;
225 6f338c34 aliguori
    }
226 6f338c34 aliguori
227 a62acdc0 Jan Kiszka
    if (!opts) {
228 a62acdc0 Jan Kiszka
        opts = "";
229 a62acdc0 Jan Kiszka
    }
230 a62acdc0 Jan Kiszka
231 e9283f8b Jan Kiszka
    if (!strcmp(pci_addr, "auto"))
232 e9283f8b Jan Kiszka
        pci_addr = NULL;
233 6f338c34 aliguori
234 6f338c34 aliguori
    if (strcmp(type, "nic") == 0)
235 e9283f8b Jan Kiszka
        dev = qemu_pci_hot_add_nic(mon, pci_addr, opts);
236 6f338c34 aliguori
    else if (strcmp(type, "storage") == 0)
237 e9283f8b Jan Kiszka
        dev = qemu_pci_hot_add_storage(mon, pci_addr, opts);
238 6f338c34 aliguori
    else
239 376253ec aliguori
        monitor_printf(mon, "invalid type: %s\n", type);
240 6f338c34 aliguori
241 6f338c34 aliguori
    if (dev) {
242 376253ec aliguori
        monitor_printf(mon, "OK domain %d, bus %d, slot %d, function %d\n",
243 376253ec aliguori
                       0, pci_bus_num(dev->bus), PCI_SLOT(dev->devfn),
244 376253ec aliguori
                       PCI_FUNC(dev->devfn));
245 6f338c34 aliguori
    } else
246 376253ec aliguori
        monitor_printf(mon, "failed to add %s\n", opts);
247 6f338c34 aliguori
}
248 6f338c34 aliguori
#endif
249 6f338c34 aliguori
250 376253ec aliguori
void pci_device_hot_remove(Monitor *mon, const char *pci_addr)
251 6f338c34 aliguori
{
252 6f338c34 aliguori
    PCIDevice *d;
253 6f338c34 aliguori
    int dom, bus;
254 6f338c34 aliguori
    unsigned slot;
255 6f338c34 aliguori
256 e9283f8b Jan Kiszka
    if (pci_read_devaddr(mon, pci_addr, &dom, &bus, &slot)) {
257 6f338c34 aliguori
        return;
258 6f338c34 aliguori
    }
259 6f338c34 aliguori
260 e822a52a Isaku Yamahata
    d = pci_find_device(pci_find_host_bus(0), bus, slot, 0);
261 6f338c34 aliguori
    if (!d) {
262 376253ec aliguori
        monitor_printf(mon, "slot %d empty\n", slot);
263 6f338c34 aliguori
        return;
264 6f338c34 aliguori
    }
265 3f84865a Gerd Hoffmann
    qdev_unplug(&d->qdev);
266 6f338c34 aliguori
}
267 6f338c34 aliguori
268 6848d827 Luiz Capitulino
void do_pci_device_hot_remove(Monitor *mon, const QDict *qdict,
269 6848d827 Luiz Capitulino
                              QObject **ret_data)
270 38183186 Luiz Capitulino
{
271 d54908a5 Luiz Capitulino
    pci_device_hot_remove(mon, qdict_get_str(qdict, "pci_addr"));
272 38183186 Luiz Capitulino
}