pci-hotplug.c 8.0 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

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

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

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

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

    scsibus = DO_UPCAST(SCSIBus, qbus, QLIST_FIRST(&adapter->child_bus));
    if (!scsibus || strcmp(scsibus->qbus.info->name, "SCSI") != 0) {
81
        error_report("Device is not a SCSI adapter");
82 83 84 85 86 87 88 89 90 91 92 93 94
        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);
G
Gerd Hoffmann 已提交
95
    dinfo->unit = scsidev->id;
96 97

    if (printinfo)
98 99
        monitor_printf(mon, "OK bus %d, unit %d\n",
                       scsibus->busnr, scsidev->id);
100 101 102
    return 0;
}

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

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

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

226
void pci_device_hot_add(Monitor *mon, const QDict *qdict)
227 228
{
    PCIDevice *dev = NULL;
229 230 231
    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");
232

233 234 235
    /* strip legacy tag */
    if (!strncmp(pci_addr, "pci_addr=", 9)) {
        pci_addr += 9;
236 237
    }

238 239 240 241
    if (!opts) {
        opts = "";
    }

242 243
    if (!strcmp(pci_addr, "auto"))
        pci_addr = NULL;
244

245
    if (strcmp(type, "nic") == 0) {
246
        dev = qemu_pci_hot_add_nic(mon, pci_addr, opts);
247
    } else if (strcmp(type, "storage") == 0) {
248
        dev = qemu_pci_hot_add_storage(mon, pci_addr, opts);
249
    } else {
A
aliguori 已提交
250
        monitor_printf(mon, "invalid type: %s\n", type);
251
    }
252 253

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

262
int pci_device_hot_remove(Monitor *mon, const char *pci_addr)
263 264 265 266 267
{
    PCIDevice *d;
    int dom, bus;
    unsigned slot;

268
    if (pci_read_devaddr(mon, pci_addr, &dom, &bus, &slot)) {
269
        return -1;
270 271
    }

272
    d = pci_find_device(pci_find_root_bus(0), bus, slot, 0);
273
    if (!d) {
A
aliguori 已提交
274
        monitor_printf(mon, "slot %d empty\n", slot);
275
        return -1;
276
    }
277
    return qdev_unplug(&d->qdev);
278 279
}

280
void do_pci_device_hot_remove(Monitor *mon, const QDict *qdict)
281
{
282
    pci_device_hot_remove(mon, qdict_get_str(qdict, "pci_addr"));
283
}