pci-hotplug.c 8.2 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"
G
Gerd Hoffmann 已提交
33
#include "scsi.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 45 46 47 48 49 50 51 52 53 54
    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;
    }
55

M
Mark McLoughlin 已提交
56 57 58 59 60 61 62 63 64
    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 已提交
65
    ret = net_client_init(mon, opts, 0);
66
    if (ret < 0)
67
        return NULL;
68 69 70 71
    if (nd_table[ret].devaddr) {
        monitor_printf(mon, "Parameter addr not supported\n");
        return NULL;
    }
72
    return pci_nic_init(&nd_table[ret], "rtl8139", devaddr);
73 74
}

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
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;
}

102
void drive_hot_add(Monitor *mon, const QDict *qdict)
103 104 105
{
    int dom, pci_bus;
    unsigned slot;
G
Gerd Hoffmann 已提交
106
    int type, bus;
107
    PCIDevice *dev;
G
Gerd Hoffmann 已提交
108
    DriveInfo *dinfo = NULL;
109 110
    const char *pci_addr = qdict_get_str(qdict, "pci_addr");
    const char *opts = qdict_get_str(qdict, "opts");
111

G
Gerd Hoffmann 已提交
112 113
    dinfo = add_init_drive(opts);
    if (!dinfo)
G
Gerd Hoffmann 已提交
114
        goto err;
G
Gerd Hoffmann 已提交
115
    if (dinfo->devaddr) {
116
        monitor_printf(mon, "Parameter addr not supported\n");
G
Gerd Hoffmann 已提交
117
        goto err;
118
    }
G
Gerd Hoffmann 已提交
119
    type = dinfo->type;
120 121 122 123
    bus = drive_get_max_bus (type);

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

G
Gerd Hoffmann 已提交
145 146 147
err:
    if (dinfo)
        drive_uninit(dinfo);
148 149 150
    return;
}

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

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

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

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

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

230
void pci_device_hot_add(Monitor *mon, const QDict *qdict)
231 232
{
    PCIDevice *dev = NULL;
233 234 235
    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");
236

237 238 239
    /* strip legacy tag */
    if (!strncmp(pci_addr, "pci_addr=", 9)) {
        pci_addr += 9;
240 241
    }

242 243 244 245
    if (!opts) {
        opts = "";
    }

246 247
    if (!strcmp(pci_addr, "auto"))
        pci_addr = NULL;
248 249

    if (strcmp(type, "nic") == 0)
250
        dev = qemu_pci_hot_add_nic(mon, pci_addr, opts);
251
    else if (strcmp(type, "storage") == 0)
252
        dev = qemu_pci_hot_add_storage(mon, pci_addr, opts);
253
    else
A
aliguori 已提交
254
        monitor_printf(mon, "invalid type: %s\n", type);
255 256

    if (dev) {
A
aliguori 已提交
257 258 259
        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));
260
    } else
A
aliguori 已提交
261
        monitor_printf(mon, "failed to add %s\n", opts);
262 263 264
}
#endif

A
aliguori 已提交
265
void pci_device_hot_remove(Monitor *mon, const char *pci_addr)
266 267 268 269 270
{
    PCIDevice *d;
    int dom, bus;
    unsigned slot;

271
    if (pci_read_devaddr(mon, pci_addr, &dom, &bus, &slot)) {
272 273 274
        return;
    }

275
    d = pci_find_device(pci_find_root_bus(0), bus, slot, 0);
276
    if (!d) {
A
aliguori 已提交
277
        monitor_printf(mon, "slot %d empty\n", slot);
278 279
        return;
    }
G
Gerd Hoffmann 已提交
280
    qdev_unplug(&d->qdev);
281 282
}

283 284
void do_pci_device_hot_remove(Monitor *mon, const QDict *qdict,
                              QObject **ret_data)
285
{
286
    pci_device_hot_remove(mon, qdict_get_str(qdict, "pci_addr"));
287
}