pci-hotplug.c 7.1 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) || defined(TARGET_X86_64)
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");

54
    ret = net_client_init(mon, opts);
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
void drive_hot_add(Monitor *mon, const QDict *qdict)
65 66 67
{
    int dom, pci_bus;
    unsigned slot;
G
Gerd Hoffmann 已提交
68
    int type, bus;
69
    PCIDevice *dev;
G
Gerd Hoffmann 已提交
70
    DriveInfo *dinfo = NULL;
71 72
    const char *pci_addr = qdict_get_str(qdict, "pci_addr");
    const char *opts = qdict_get_str(qdict, "opts");
73
    BusState *scsibus;
74

G
Gerd Hoffmann 已提交
75 76
    dinfo = add_init_drive(opts);
    if (!dinfo)
G
Gerd Hoffmann 已提交
77
        goto err;
G
Gerd Hoffmann 已提交
78
    if (dinfo->devaddr) {
79
        monitor_printf(mon, "Parameter addr not supported\n");
G
Gerd Hoffmann 已提交
80
        goto err;
81
    }
G
Gerd Hoffmann 已提交
82
    type = dinfo->type;
83 84 85 86
    bus = drive_get_max_bus (type);

    switch (type) {
    case IF_SCSI:
G
Gerd Hoffmann 已提交
87 88 89 90 91 92 93 94
        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;
        }
B
Blue Swirl 已提交
95
        scsibus = QLIST_FIRST(&dev->qdev.child_bus);
96 97
        scsi_bus_legacy_add_drive(DO_UPCAST(SCSIBus, qbus, scsibus),
                                  dinfo, dinfo->unit);
G
Gerd Hoffmann 已提交
98 99 100
        monitor_printf(mon, "OK bus %d, unit %d\n",
                       dinfo->bus,
                       dinfo->unit);
101
        break;
G
Gerd Hoffmann 已提交
102 103 104
    case IF_NONE:
        monitor_printf(mon, "OK\n");
        break;
105
    default:
A
aliguori 已提交
106
        monitor_printf(mon, "Can't hot-add drive to type %d\n", type);
G
Gerd Hoffmann 已提交
107
        goto err;
108
    }
G
Gerd Hoffmann 已提交
109
    return;
110

G
Gerd Hoffmann 已提交
111 112 113
err:
    if (dinfo)
        drive_uninit(dinfo);
114 115 116
    return;
}

117 118
static PCIDevice *qemu_pci_hot_add_storage(Monitor *mon,
                                           const char *devaddr,
A
aliguori 已提交
119
                                           const char *opts)
120
{
121
    PCIDevice *dev;
122
    DriveInfo *dinfo = NULL;
G
Gerd Hoffmann 已提交
123
    int type = -1;
124
    char buf[128];
125 126
    PCIBus *bus;
    int devfn;
127 128 129 130 131 132

    if (get_param_value(buf, sizeof(buf), "if", opts)) {
        if (!strcmp(buf, "scsi"))
            type = IF_SCSI;
        else if (!strcmp(buf, "virtio")) {
            type = IF_VIRTIO;
133 134
        } else {
            monitor_printf(mon, "type %s not a hotpluggable PCI device.\n", buf);
135
            return NULL;
136 137
        }
    } else {
A
aliguori 已提交
138
        monitor_printf(mon, "no if= specified\n");
139
        return NULL;
140 141 142
    }

    if (get_param_value(buf, sizeof(buf), "file", opts)) {
G
Gerd Hoffmann 已提交
143 144
        dinfo = add_init_drive(opts);
        if (!dinfo)
145
            return NULL;
G
Gerd Hoffmann 已提交
146
        if (dinfo->devaddr) {
147 148 149
            monitor_printf(mon, "Parameter addr not supported\n");
            return NULL;
        }
150 151
    } else {
        dinfo = NULL;
152 153
    }

154 155 156 157 158 159
    bus = pci_get_bus_devfn(&devfn, devaddr);
    if (!bus) {
        monitor_printf(mon, "Invalid PCI device address %s\n", devaddr);
        return NULL;
    }

160 161
    switch (type) {
    case IF_SCSI:
162
        dev = pci_create(bus, devfn, "lsi53c895a");
163 164
        break;
    case IF_VIRTIO:
165 166 167 168
        if (!dinfo) {
            monitor_printf(mon, "virtio requires a backing file/device.\n");
            return NULL;
        }
169
        dev = pci_create(bus, devfn, "virtio-blk-pci");
G
Gerd Hoffmann 已提交
170
        qdev_prop_set_drive(&dev->qdev, "drive", dinfo);
171
        break;
172 173
    default:
        dev = NULL;
174
    }
175 176
    if (!dev || qdev_init(&dev->qdev) < 0)
        return NULL;
177
    return dev;
178 179
}

180
void pci_device_hot_add(Monitor *mon, const QDict *qdict)
181 182
{
    PCIDevice *dev = NULL;
183 184 185
    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");
186

187 188 189
    /* strip legacy tag */
    if (!strncmp(pci_addr, "pci_addr=", 9)) {
        pci_addr += 9;
190 191
    }

192 193 194 195
    if (!opts) {
        opts = "";
    }

196 197
    if (!strcmp(pci_addr, "auto"))
        pci_addr = NULL;
198 199

    if (strcmp(type, "nic") == 0)
200
        dev = qemu_pci_hot_add_nic(mon, pci_addr, opts);
201
    else if (strcmp(type, "storage") == 0)
202
        dev = qemu_pci_hot_add_storage(mon, pci_addr, opts);
203
    else
A
aliguori 已提交
204
        monitor_printf(mon, "invalid type: %s\n", type);
205 206

    if (dev) {
A
aliguori 已提交
207 208 209
        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));
210
    } else
A
aliguori 已提交
211
        monitor_printf(mon, "failed to add %s\n", opts);
212 213 214
}
#endif

A
aliguori 已提交
215
void pci_device_hot_remove(Monitor *mon, const char *pci_addr)
216 217 218 219 220
{
    PCIDevice *d;
    int dom, bus;
    unsigned slot;

221
    if (pci_read_devaddr(mon, pci_addr, &dom, &bus, &slot)) {
222 223 224 225 226
        return;
    }

    d = pci_find_device(bus, slot, 0);
    if (!d) {
A
aliguori 已提交
227
        monitor_printf(mon, "slot %d empty\n", slot);
228 229
        return;
    }
G
Gerd Hoffmann 已提交
230
    qdev_unplug(&d->qdev);
231 232
}

233
void do_pci_device_hot_remove(Monitor *mon, const QDict *qdict)
234
{
235
    pci_device_hot_remove(mon, qdict_get_str(qdict, "pci_addr"));
236 237
}

238 239 240 241 242 243 244 245 246 247 248
static int pci_match_fn(void *dev_private, void *arg)
{
    PCIDevice *dev = dev_private;
    PCIDevice *match = arg;

    return (dev == match);
}

/*
 * OS has executed _EJ0 method, we now can remove the device
 */
G
Gerd Hoffmann 已提交
249
void pci_device_hot_remove_success(PCIDevice *d)
250 251 252 253 254 255 256 257 258 259 260 261
{
    int class_code;

    class_code = d->config_read(d, PCI_CLASS_DEVICE+1, 1);

    switch(class_code) {
    case PCI_BASE_CLASS_NETWORK:
        destroy_nic(pci_match_fn, d);
        break;
    }
}