Statistics
| Branch: | Revision:

root / hw / pci-hotplug.c @ 6fdb03d5

History | View | Annotate | Download (9.1 kB)

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

    
25
#include "hw.h"
26
#include "boards.h"
27
#include "pci.h"
28
#include "net.h"
29
#include "sysemu.h"
30
#include "pc.h"
31
#include "monitor.h"
32
#include "block_int.h"
33
#include "scsi.h"
34
#include "virtio-blk.h"
35
#include "qemu-config.h"
36
#include "qemu-objects.h"
37

    
38
#if defined(TARGET_I386)
39
static PCIDevice *qemu_pci_hot_add_nic(Monitor *mon,
40
                                       const char *devaddr,
41
                                       const char *opts_str)
42
{
43
    QemuOpts *opts;
44
    PCIBus *bus;
45
    int ret, devfn;
46

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

    
57
    opts = qemu_opts_parse(&qemu_net_opts, opts_str ? opts_str : "", NULL);
58
    if (!opts) {
59
        monitor_printf(mon, "parsing network options '%s' failed\n",
60
                       opts_str ? opts_str : "");
61
        return NULL;
62
    }
63

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

    
66
    ret = net_client_init(mon, opts, 0);
67
    if (ret < 0)
68
        return NULL;
69
    if (nd_table[ret].devaddr) {
70
        monitor_printf(mon, "Parameter addr not supported\n");
71
        return NULL;
72
    }
73
    return pci_nic_init(&nd_table[ret], "rtl8139", devaddr);
74
}
75

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

    
82
    scsibus = DO_UPCAST(SCSIBus, qbus, QLIST_FIRST(&adapter->child_bus));
83
    if (!scsibus || strcmp(scsibus->qbus.info->name, "SCSI") != 0) {
84
        qemu_error("Device is not a SCSI adapter\n");
85
        return -1;
86
    }
87

    
88
    /*
89
     * drive_init() tries to find a default for dinfo->unit.  Doesn't
90
     * work at all for hotplug though as we assign the device to a
91
     * specific bus instead of the first bus with spare scsi ids.
92
     *
93
     * Ditch the calculated value and reload from option string (if
94
     * specified).
95
     */
96
    dinfo->unit = qemu_opt_get_number(dinfo->opts, "unit", -1);
97
    scsidev = scsi_bus_legacy_add_drive(scsibus, dinfo, dinfo->unit);
98
    dinfo->unit = scsidev->id;
99

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

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

    
116
    dinfo = add_init_drive(opts);
117
    if (!dinfo)
118
        goto err;
119
    if (dinfo->devaddr) {
120
        monitor_printf(mon, "Parameter addr not supported\n");
121
        goto err;
122
    }
123
    type = dinfo->type;
124
    bus = drive_get_max_bus (type);
125

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

    
149
err:
150
    if (dinfo)
151
        drive_uninit(dinfo);
152
    return;
153
}
154

    
155
static PCIDevice *qemu_pci_hot_add_storage(Monitor *mon,
156
                                           const char *devaddr,
157
                                           const char *opts)
158
{
159
    PCIDevice *dev;
160
    DriveInfo *dinfo = NULL;
161
    int type = -1;
162
    char buf[128];
163
    PCIBus *bus;
164
    int devfn;
165

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

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

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

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

    
230
void pci_device_hot_add_print(Monitor *mon, const QObject *data)
231
{
232
    QDict *qdict;
233

    
234
    assert(qobject_type(data) == QTYPE_QDICT);
235
    qdict = qobject_to_qdict(data);
236

    
237
    monitor_printf(mon, "OK domain %d, bus %d, slot %d, function %d\n",
238
                   (int) qdict_get_int(qdict, "domain"),
239
                   (int) qdict_get_int(qdict, "bus"),
240
                   (int) qdict_get_int(qdict, "slot"),
241
                   (int) qdict_get_int(qdict, "function"));
242

    
243
}
244

    
245
/**
246
 * pci_device_hot_add(): Hot add a PCI device
247
 *
248
 * Return a QDict with the following device information:
249
 *
250
 * - "domain": domain number
251
 * - "bus": bus number
252
 * - "slot": slot number
253
 * - "function": function number
254
 *
255
 * Example:
256
 *
257
 * { "domain": 0, "bus": 0, "slot": 5, "function": 0 }
258
 */
259
int pci_device_hot_add(Monitor *mon, const QDict *qdict, QObject **ret_data)
260
{
261
    PCIDevice *dev = NULL;
262
    const char *pci_addr = qdict_get_str(qdict, "pci_addr");
263
    const char *type = qdict_get_str(qdict, "type");
264
    const char *opts = qdict_get_try_str(qdict, "opts");
265

    
266
    /* strip legacy tag */
267
    if (!strncmp(pci_addr, "pci_addr=", 9)) {
268
        pci_addr += 9;
269
    }
270

    
271
    if (!opts) {
272
        opts = "";
273
    }
274

    
275
    if (!strcmp(pci_addr, "auto"))
276
        pci_addr = NULL;
277

    
278
    if (strcmp(type, "nic") == 0) {
279
        dev = qemu_pci_hot_add_nic(mon, pci_addr, opts);
280
    } else if (strcmp(type, "storage") == 0) {
281
        dev = qemu_pci_hot_add_storage(mon, pci_addr, opts);
282
    } else {
283
        monitor_printf(mon, "invalid type: %s\n", type);
284
        return -1;
285
    }
286

    
287
    if (dev) {
288
        *ret_data =
289
        qobject_from_jsonf("{ 'domain': 0, 'bus': %d, 'slot': %d, "
290
                           "'function': %d }", pci_bus_num(dev->bus),
291
                           PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
292
    } else {
293
        monitor_printf(mon, "failed to add %s\n", opts);
294
        return -1;
295
    }
296

    
297
    return 0;
298
}
299
#endif
300

    
301
int pci_device_hot_remove(Monitor *mon, const char *pci_addr)
302
{
303
    PCIDevice *d;
304
    int dom, bus;
305
    unsigned slot;
306

    
307
    if (pci_read_devaddr(mon, pci_addr, &dom, &bus, &slot)) {
308
        return -1;
309
    }
310

    
311
    d = pci_find_device(pci_find_root_bus(0), bus, slot, 0);
312
    if (!d) {
313
        monitor_printf(mon, "slot %d empty\n", slot);
314
        return -1;
315
    }
316
    return qdev_unplug(&d->qdev);
317
}
318

    
319
int do_pci_device_hot_remove(Monitor *mon, const QDict *qdict,
320
                             QObject **ret_data)
321
{
322
    return pci_device_hot_remove(mon, qdict_get_str(qdict, "pci_addr"));
323
}