pci-hotplug.c 8.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
 * QEMU PCI hotplug support
 *
 * Copyright (c) 2004 Fabrice Bellard
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "hw.h"
#include "boards.h"
#include "pci.h"
#include "net.h"
#include "pc.h"
A
aliguori 已提交
30
#include "monitor.h"
G
Gerd Hoffmann 已提交
31
#include "scsi.h"
32
#include "virtio-blk.h"
M
Mark McLoughlin 已提交
33
#include "qemu-config.h"
B
Blue Swirl 已提交
34
#include "blockdev.h"
35

36
#if defined(TARGET_I386)
37
static PCIDevice *qemu_pci_hot_add_nic(Monitor *mon,
M
Mark McLoughlin 已提交
38 39
                                       const char *devaddr,
                                       const char *opts_str)
40
{
M
Mark McLoughlin 已提交
41
    QemuOpts *opts;
42 43 44 45 46 47 48 49 50 51 52 53
    PCIBus *bus;
    int ret, devfn;

    bus = pci_get_bus_devfn(&devfn, devaddr);
    if (!bus) {
        monitor_printf(mon, "Invalid PCI device address %s\n", devaddr);
        return NULL;
    }
    if (!((BusState*)bus)->allow_hotplug) {
        monitor_printf(mon, "PCI bus doesn't support hotplug\n");
        return NULL;
    }
54

55
    opts = qemu_opts_parse(qemu_find_opts("net"), opts_str ? opts_str : "", 0);
M
Mark McLoughlin 已提交
56 57 58 59 60 61
    if (!opts) {
        return NULL;
    }

    qemu_opt_set(opts, "type", "nic");

M
Mark McLoughlin 已提交
62
    ret = net_client_init(mon, opts, 0);
63
    if (ret < 0)
64
        return NULL;
65 66 67 68
    if (nd_table[ret].devaddr) {
        monitor_printf(mon, "Parameter addr not supported\n");
        return NULL;
    }
69
    return pci_nic_init(&nd_table[ret], "rtl8139", devaddr);
70 71
}

72 73
static int scsi_hot_add(Monitor *mon, DeviceState *adapter,
                        DriveInfo *dinfo, int printinfo)
74 75 76 77 78 79
{
    SCSIBus *scsibus;
    SCSIDevice *scsidev;

    scsibus = DO_UPCAST(SCSIBus, qbus, QLIST_FIRST(&adapter->child_bus));
    if (!scsibus || strcmp(scsibus->qbus.info->name, "SCSI") != 0) {
80
        error_report("Device is not a SCSI adapter");
81 82 83 84 85 86 87 88 89 90 91 92
        return -1;
    }

    /*
     * drive_init() tries to find a default for dinfo->unit.  Doesn't
     * work at all for hotplug though as we assign the device to a
     * specific bus instead of the first bus with spare scsi ids.
     *
     * Ditch the calculated value and reload from option string (if
     * specified).
     */
    dinfo->unit = qemu_opt_get_number(dinfo->opts, "unit", -1);
93
    dinfo->bus = scsibus->busnr;
94 95
    scsidev = scsi_bus_legacy_add_drive(scsibus, dinfo->bdrv, dinfo->unit,
                                        false, -1);
96 97 98
    if (!scsidev) {
        return -1;
    }
G
Gerd Hoffmann 已提交
99
    dinfo->unit = scsidev->id;
100 101

    if (printinfo)
102 103
        monitor_printf(mon, "OK bus %d, unit %d\n",
                       scsibus->busnr, scsidev->id);
104 105 106
    return 0;
}

107
void drive_hot_add(Monitor *mon, const QDict *qdict)
108 109 110
{
    int dom, pci_bus;
    unsigned slot;
111
    int type;
112
    PCIDevice *dev;
G
Gerd Hoffmann 已提交
113
    DriveInfo *dinfo = NULL;
114 115
    const char *pci_addr = qdict_get_str(qdict, "pci_addr");
    const char *opts = qdict_get_str(qdict, "opts");
116

G
Gerd Hoffmann 已提交
117 118
    dinfo = add_init_drive(opts);
    if (!dinfo)
G
Gerd Hoffmann 已提交
119
        goto err;
G
Gerd Hoffmann 已提交
120
    if (dinfo->devaddr) {
121
        monitor_printf(mon, "Parameter addr not supported\n");
G
Gerd Hoffmann 已提交
122
        goto err;
123
    }
G
Gerd Hoffmann 已提交
124
    type = dinfo->type;
125 126 127

    switch (type) {
    case IF_SCSI:
G
Gerd Hoffmann 已提交
128 129 130
        if (pci_read_devaddr(mon, pci_addr, &dom, &pci_bus, &slot)) {
            goto err;
        }
131 132
        dev = pci_find_device(pci_find_root_bus(dom), pci_bus,
                              PCI_DEVFN(slot, 0));
G
Gerd Hoffmann 已提交
133 134 135 136
        if (!dev) {
            monitor_printf(mon, "no pci device with address %s\n", pci_addr);
            goto err;
        }
137
        if (scsi_hot_add(mon, &dev->qdev, dinfo, 1) != 0) {
138 139
            goto err;
        }
140
        break;
G
Gerd Hoffmann 已提交
141 142 143
    case IF_NONE:
        monitor_printf(mon, "OK\n");
        break;
144
    default:
A
aliguori 已提交
145
        monitor_printf(mon, "Can't hot-add drive to type %d\n", type);
G
Gerd Hoffmann 已提交
146
        goto err;
147
    }
G
Gerd Hoffmann 已提交
148
    return;
149

G
Gerd Hoffmann 已提交
150 151
err:
    if (dinfo)
152
        drive_put_ref(dinfo);
153 154 155
    return;
}

156 157
static PCIDevice *qemu_pci_hot_add_storage(Monitor *mon,
                                           const char *devaddr,
A
aliguori 已提交
158
                                           const char *opts)
159
{
160
    PCIDevice *dev;
161
    DriveInfo *dinfo = NULL;
G
Gerd Hoffmann 已提交
162
    int type = -1;
163
    char buf[128];
164 165
    PCIBus *bus;
    int devfn;
166 167 168 169 170 171

    if (get_param_value(buf, sizeof(buf), "if", opts)) {
        if (!strcmp(buf, "scsi"))
            type = IF_SCSI;
        else if (!strcmp(buf, "virtio")) {
            type = IF_VIRTIO;
172 173
        } else {
            monitor_printf(mon, "type %s not a hotpluggable PCI device.\n", buf);
174
            return NULL;
175 176
        }
    } else {
A
aliguori 已提交
177
        monitor_printf(mon, "no if= specified\n");
178
        return NULL;
179 180 181
    }

    if (get_param_value(buf, sizeof(buf), "file", opts)) {
G
Gerd Hoffmann 已提交
182 183
        dinfo = add_init_drive(opts);
        if (!dinfo)
184
            return NULL;
G
Gerd Hoffmann 已提交
185
        if (dinfo->devaddr) {
186 187 188
            monitor_printf(mon, "Parameter addr not supported\n");
            return NULL;
        }
189 190
    } else {
        dinfo = NULL;
191 192
    }

193 194 195 196 197
    bus = pci_get_bus_devfn(&devfn, devaddr);
    if (!bus) {
        monitor_printf(mon, "Invalid PCI device address %s\n", devaddr);
        return NULL;
    }
198 199 200 201
    if (!((BusState*)bus)->allow_hotplug) {
        monitor_printf(mon, "PCI bus doesn't support hotplug\n");
        return NULL;
    }
202

203 204
    switch (type) {
    case IF_SCSI:
205
        dev = pci_create(bus, devfn, "lsi53c895a");
206 207
        if (qdev_init(&dev->qdev) < 0)
            dev = NULL;
208
        if (dev && dinfo) {
209
            if (scsi_hot_add(mon, &dev->qdev, dinfo, 0) != 0) {
210 211 212
                qdev_unplug(&dev->qdev);
                dev = NULL;
            }
213
        }
214 215
        break;
    case IF_VIRTIO:
216 217 218 219
        if (!dinfo) {
            monitor_printf(mon, "virtio requires a backing file/device.\n");
            return NULL;
        }
220
        dev = pci_create(bus, devfn, "virtio-blk-pci");
221 222 223 224 225
        if (qdev_prop_set_drive(&dev->qdev, "drive", dinfo->bdrv) < 0) {
            qdev_free(&dev->qdev);
            dev = NULL;
            break;
        }
226 227
        if (qdev_init(&dev->qdev) < 0)
            dev = NULL;
228
        break;
229 230
    default:
        dev = NULL;
231
    }
232
    return dev;
233 234
}

235
void pci_device_hot_add(Monitor *mon, const QDict *qdict)
236 237
{
    PCIDevice *dev = NULL;
238 239 240
    const char *pci_addr = qdict_get_str(qdict, "pci_addr");
    const char *type = qdict_get_str(qdict, "type");
    const char *opts = qdict_get_try_str(qdict, "opts");
241

242 243 244
    /* strip legacy tag */
    if (!strncmp(pci_addr, "pci_addr=", 9)) {
        pci_addr += 9;
245 246
    }

247 248 249 250
    if (!opts) {
        opts = "";
    }

251 252
    if (!strcmp(pci_addr, "auto"))
        pci_addr = NULL;
253

254
    if (strcmp(type, "nic") == 0) {
255
        dev = qemu_pci_hot_add_nic(mon, pci_addr, opts);
256
    } else if (strcmp(type, "storage") == 0) {
257
        dev = qemu_pci_hot_add_storage(mon, pci_addr, opts);
258
    } else {
A
aliguori 已提交
259
        monitor_printf(mon, "invalid type: %s\n", type);
260
    }
261 262

    if (dev) {
263
        monitor_printf(mon, "OK domain %d, bus %d, slot %d, function %d\n",
264 265
                       pci_find_domain(dev->bus),
                       pci_bus_num(dev->bus), PCI_SLOT(dev->devfn),
266 267
                       PCI_FUNC(dev->devfn));
    } else
A
aliguori 已提交
268
        monitor_printf(mon, "failed to add %s\n", opts);
269 270 271
}
#endif

272
static int pci_device_hot_remove(Monitor *mon, const char *pci_addr)
273 274 275 276 277
{
    PCIDevice *d;
    int dom, bus;
    unsigned slot;

278
    if (pci_read_devaddr(mon, pci_addr, &dom, &bus, &slot)) {
279
        return -1;
280 281
    }

282
    d = pci_find_device(pci_find_root_bus(dom), bus, PCI_DEVFN(slot, 0));
283
    if (!d) {
A
aliguori 已提交
284
        monitor_printf(mon, "slot %d empty\n", slot);
285
        return -1;
286
    }
287
    return qdev_unplug(&d->qdev);
288 289
}

290
void do_pci_device_hot_remove(Monitor *mon, const QDict *qdict)
291
{
292
    pci_device_hot_remove(mon, qdict_get_str(qdict, "pci_addr"));
293
}