pci-hotplug.c 7.7 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 30
/*
 * 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 "sysemu.h"
#include "pc.h"
A
aliguori 已提交
31
#include "monitor.h"
32
#include "block_int.h"
33
#include "scsi-disk.h"
34
#include "virtio-blk.h"
M
Mark McLoughlin 已提交
35
#include "qemu-config.h"
36

37
#if defined(TARGET_I386)
38
static PCIDevice *qemu_pci_hot_add_nic(Monitor *mon,
M
Mark McLoughlin 已提交
39 40
                                       const char *devaddr,
                                       const char *opts_str)
41
{
M
Mark McLoughlin 已提交
42
    QemuOpts *opts;
43 44
    int ret;

M
Mark McLoughlin 已提交
45 46 47 48 49 50 51 52 53
    opts = qemu_opts_parse(&qemu_net_opts, opts_str ? opts_str : "", NULL);
    if (!opts) {
        monitor_printf(mon, "parsing network options '%s' failed\n",
                       opts_str ? opts_str : "");
        return NULL;
    }

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

M
Mark McLoughlin 已提交
54
    ret = net_client_init(mon, opts, 0);
55
    if (ret < 0)
56
        return NULL;
57 58 59 60
    if (nd_table[ret].devaddr) {
        monitor_printf(mon, "Parameter addr not supported\n");
        return NULL;
    }
61
    return pci_nic_init(&nd_table[ret], "rtl8139", devaddr);
62 63
}

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
static int scsi_hot_add(DeviceState *adapter, DriveInfo *dinfo, int printinfo)
{
    SCSIBus *scsibus;
    SCSIDevice *scsidev;

    scsibus = DO_UPCAST(SCSIBus, qbus, QLIST_FIRST(&adapter->child_bus));
    if (!scsibus || strcmp(scsibus->qbus.info->name, "SCSI") != 0) {
        qemu_error("Device is not a SCSI adapter\n");
        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);
    scsidev = scsi_bus_legacy_add_drive(scsibus, dinfo, dinfo->unit);

    if (printinfo)
        qemu_error("OK bus %d, unit %d\n", scsibus->busnr, scsidev->id);
    return 0;
}

91
void drive_hot_add(Monitor *mon, const QDict *qdict)
92 93 94
{
    int dom, pci_bus;
    unsigned slot;
G
Gerd Hoffmann 已提交
95
    int type, bus;
96
    PCIDevice *dev;
G
Gerd Hoffmann 已提交
97
    DriveInfo *dinfo = NULL;
98 99
    const char *pci_addr = qdict_get_str(qdict, "pci_addr");
    const char *opts = qdict_get_str(qdict, "opts");
100

G
Gerd Hoffmann 已提交
101 102
    dinfo = add_init_drive(opts);
    if (!dinfo)
G
Gerd Hoffmann 已提交
103
        goto err;
G
Gerd Hoffmann 已提交
104
    if (dinfo->devaddr) {
105
        monitor_printf(mon, "Parameter addr not supported\n");
G
Gerd Hoffmann 已提交
106
        goto err;
107
    }
G
Gerd Hoffmann 已提交
108
    type = dinfo->type;
109 110 111 112
    bus = drive_get_max_bus (type);

    switch (type) {
    case IF_SCSI:
G
Gerd Hoffmann 已提交
113 114 115 116 117 118 119 120
        if (pci_read_devaddr(mon, pci_addr, &dom, &pci_bus, &slot)) {
            goto err;
        }
        dev = pci_find_device(pci_bus, slot, 0);
        if (!dev) {
            monitor_printf(mon, "no pci device with address %s\n", pci_addr);
            goto err;
        }
121 122 123
        if (scsi_hot_add(&dev->qdev, dinfo, 1) != 0) {
            goto err;
        }
124
        break;
G
Gerd Hoffmann 已提交
125 126 127
    case IF_NONE:
        monitor_printf(mon, "OK\n");
        break;
128
    default:
A
aliguori 已提交
129
        monitor_printf(mon, "Can't hot-add drive to type %d\n", type);
G
Gerd Hoffmann 已提交
130
        goto err;
131
    }
G
Gerd Hoffmann 已提交
132
    return;
133

G
Gerd Hoffmann 已提交
134 135 136
err:
    if (dinfo)
        drive_uninit(dinfo);
137 138 139
    return;
}

140 141
static PCIDevice *qemu_pci_hot_add_storage(Monitor *mon,
                                           const char *devaddr,
A
aliguori 已提交
142
                                           const char *opts)
143
{
144
    PCIDevice *dev;
145
    DriveInfo *dinfo = NULL;
G
Gerd Hoffmann 已提交
146
    int type = -1;
147
    char buf[128];
148 149
    PCIBus *bus;
    int devfn;
150 151 152 153 154 155

    if (get_param_value(buf, sizeof(buf), "if", opts)) {
        if (!strcmp(buf, "scsi"))
            type = IF_SCSI;
        else if (!strcmp(buf, "virtio")) {
            type = IF_VIRTIO;
156 157
        } else {
            monitor_printf(mon, "type %s not a hotpluggable PCI device.\n", buf);
158
            return NULL;
159 160
        }
    } else {
A
aliguori 已提交
161
        monitor_printf(mon, "no if= specified\n");
162
        return NULL;
163 164 165
    }

    if (get_param_value(buf, sizeof(buf), "file", opts)) {
G
Gerd Hoffmann 已提交
166 167
        dinfo = add_init_drive(opts);
        if (!dinfo)
168
            return NULL;
G
Gerd Hoffmann 已提交
169
        if (dinfo->devaddr) {
170 171 172
            monitor_printf(mon, "Parameter addr not supported\n");
            return NULL;
        }
173 174
    } else {
        dinfo = NULL;
175 176
    }

177 178 179 180 181 182
    bus = pci_get_bus_devfn(&devfn, devaddr);
    if (!bus) {
        monitor_printf(mon, "Invalid PCI device address %s\n", devaddr);
        return NULL;
    }

183 184
    switch (type) {
    case IF_SCSI:
185 186 187 188
        if (!dinfo) {
            monitor_printf(mon, "scsi requires a backing file/device.\n");
            return NULL;
        }
189
        dev = pci_create(bus, devfn, "lsi53c895a");
190 191 192
        if (qdev_init(&dev->qdev) < 0)
            dev = NULL;
        if (dev) {
193 194 195 196
            if (scsi_hot_add(&dev->qdev, dinfo, 0) != 0) {
                qdev_unplug(&dev->qdev);
                dev = NULL;
            }
197
        }
198 199
        break;
    case IF_VIRTIO:
200 201 202 203
        if (!dinfo) {
            monitor_printf(mon, "virtio requires a backing file/device.\n");
            return NULL;
        }
204
        dev = pci_create(bus, devfn, "virtio-blk-pci");
G
Gerd Hoffmann 已提交
205
        qdev_prop_set_drive(&dev->qdev, "drive", dinfo);
206 207
        if (qdev_init(&dev->qdev) < 0)
            dev = NULL;
208
        break;
209 210
    default:
        dev = NULL;
211
    }
212
    return dev;
213 214
}

215
void pci_device_hot_add(Monitor *mon, const QDict *qdict)
216 217
{
    PCIDevice *dev = NULL;
218 219 220
    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");
221

222 223 224
    /* strip legacy tag */
    if (!strncmp(pci_addr, "pci_addr=", 9)) {
        pci_addr += 9;
225 226
    }

227 228 229 230
    if (!opts) {
        opts = "";
    }

231 232
    if (!strcmp(pci_addr, "auto"))
        pci_addr = NULL;
233 234

    if (strcmp(type, "nic") == 0)
235
        dev = qemu_pci_hot_add_nic(mon, pci_addr, opts);
236
    else if (strcmp(type, "storage") == 0)
237
        dev = qemu_pci_hot_add_storage(mon, pci_addr, opts);
238
    else
A
aliguori 已提交
239
        monitor_printf(mon, "invalid type: %s\n", type);
240 241

    if (dev) {
A
aliguori 已提交
242 243 244
        monitor_printf(mon, "OK domain %d, bus %d, slot %d, function %d\n",
                       0, pci_bus_num(dev->bus), PCI_SLOT(dev->devfn),
                       PCI_FUNC(dev->devfn));
245
    } else
A
aliguori 已提交
246
        monitor_printf(mon, "failed to add %s\n", opts);
247 248 249
}
#endif

A
aliguori 已提交
250
void pci_device_hot_remove(Monitor *mon, const char *pci_addr)
251 252 253 254 255
{
    PCIDevice *d;
    int dom, bus;
    unsigned slot;

256
    if (pci_read_devaddr(mon, pci_addr, &dom, &bus, &slot)) {
257 258 259 260 261
        return;
    }

    d = pci_find_device(bus, slot, 0);
    if (!d) {
A
aliguori 已提交
262
        monitor_printf(mon, "slot %d empty\n", slot);
263 264
        return;
    }
G
Gerd Hoffmann 已提交
265
    qdev_unplug(&d->qdev);
266 267
}

268 269
void do_pci_device_hot_remove(Monitor *mon, const QDict *qdict,
                              QObject **ret_data)
270
{
271
    pci_device_hot_remove(mon, qdict_get_str(qdict, "pci_addr"));
272
}