aboutsummaryrefslogtreecommitdiff
path: root/hw/device-hotplug.c
blob: 95b3c7b2482751a28cb6d2d382ec2be4187d94b0 (plain)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "hw.h"
#include "boards.h"
#include "pci.h"
#include "net.h"
#include "sysemu.h"
#include "pc.h"
#include "console.h"
#include "block_int.h"

#define PCI_BASE_CLASS_STORAGE          0x01
#define PCI_BASE_CLASS_NETWORK          0x02

static PCIDevice *qemu_system_hot_add_nic(const char *opts, int bus_nr)
{
    int ret;
    char buf[4096];
    PCIBus *pci_bus;

    pci_bus = pci_find_bus (bus_nr);
    if (!pci_bus) {
        term_printf ("Can't find pci_bus %d\n", bus_nr);
        return NULL;
    }

    memset (buf, 0, sizeof (buf));

    strcpy (buf, "nic,");
    strncat (buf, opts, sizeof (buf) - strlen (buf) - 1);

    ret = net_client_init (buf);
    if (ret < 0 || !nd_table[ret].model)
        return NULL;
    return pci_nic_init (pci_bus, &nd_table[ret], -1);
}

static int add_init_drive(const char *opts)
{
    int drive_opt_idx, drive_idx;
    int ret = -1;

    drive_opt_idx = drive_add(NULL, "%s", opts);
    if (!drive_opt_idx)
        return ret;

    drive_idx = drive_init(&drives_opt[drive_opt_idx], 0, current_machine);
    if (drive_idx == -1) {
        drive_remove(drive_opt_idx);
        return ret;
    }

    return drive_idx;
}

void drive_hot_add(int pcibus, const char *devfn_string, const char *opts)
{
    int drive_idx, type, bus;
    int devfn;
    int success = 0;
    PCIDevice *dev;

    devfn = strtoul(devfn_string, NULL, 0);

    dev = pci_find_device(pcibus, PCI_SLOT(devfn));
    if (!dev) {
        term_printf("no pci device with devfn %d (slot %d)\n", devfn,
                    PCI_SLOT(devfn));
        return;
    }

    drive_idx = add_init_drive(opts);
    if (drive_idx < 0)
        return;
    type = drives_table[drive_idx].type;
    bus = drive_get_max_bus (type);

    switch (type) {
    case IF_SCSI:
        success = 1;
        lsi_scsi_attach (dev, drives_table[drive_idx].bdrv,
                         drives_table[drive_idx].unit);
        break;
    default:
        term_printf("Can't hot-add drive to type %d\n", type);
    }

    if (success)
        term_printf("OK bus %d, unit %d\n", drives_table[drive_idx].bus,
                                            drives_table[drive_idx].unit);
    return;
}

static PCIDevice *qemu_system_hot_add_storage(const char *opts, int bus_nr)
{
    void *opaque = NULL;
    PCIBus *pci_bus;
    int type = -1, drive_idx = -1;
    char buf[128];

    pci_bus = pci_find_bus(bus_nr);
    if (!pci_bus) {
        term_printf("Can't find pci_bus %d\n", bus_nr);
        return NULL;
    }

    if (get_param_value(buf, sizeof(buf), "if", opts)) {
        if (!strcmp(buf, "scsi"))
            type = IF_SCSI;
        else if (!strcmp(buf, "virtio")) {
            type = IF_VIRTIO;
        }
    } else {
        term_printf("no if= specified\n");
        return NULL;
    }

    if (get_param_value(buf, sizeof(buf), "file", opts)) {
        drive_idx = add_init_drive(opts);
        if (drive_idx < 0)
            return NULL;
    } else if (type == IF_VIRTIO) {
        term_printf("virtio requires a backing file/device.\n");
        return NULL;
    }

    switch (type) {
    case IF_SCSI:
        opaque = lsi_scsi_init (pci_bus, -1);
        if (drive_idx >= 0)
            lsi_scsi_attach (opaque, drives_table[drive_idx].bdrv,
                             drives_table[drive_idx].unit);
        break;
    case IF_VIRTIO:
        opaque = virtio_blk_init (pci_bus, 0x1AF4, 0x1001,
                                  drives_table[drive_idx].bdrv);
        break;
    default:
        term_printf ("type %s not a hotpluggable PCI device.\n", buf);
    }

    return opaque;
}

#if defined(TARGET_I386) || defined(TARGET_X86_64)
void device_hot_add(int pcibus, const char *type, const char *opts)
{
    PCIDevice *dev = NULL;

    if (strcmp(type, "nic") == 0)
        dev = qemu_system_hot_add_nic(opts, pcibus);
    else if (strcmp(type, "storage") == 0)
        dev = qemu_system_hot_add_storage(opts, pcibus);
    else
        term_printf("invalid type: %s\n", type);

    if (dev) {
        qemu_system_device_hot_add(pcibus, PCI_SLOT(dev->devfn), 1);
        term_printf("OK bus %d, slot %d, function %d (devfn %d)\n",
                    pci_bus_num(dev->bus), PCI_SLOT(dev->devfn),
                    PCI_FUNC(dev->devfn), dev->devfn);
    } else
        term_printf("failed to add %s\n", opts);
}

void device_hot_remove(int pcibus, int slot)
{
    PCIDevice *d = pci_find_device(pcibus, slot);

    if (!d) {
        term_printf("invalid slot %d\n", slot);
        return;
    }

    qemu_system_device_hot_add(pcibus, slot, 0);
}
#endif

static void destroy_nic(int slot)
{
    int i;

    for (i = 0; i < MAX_NICS; i++)
        if (nd_table[i].used &&
            PCI_SLOT(nd_table[i].devfn) == slot)
                net_client_uninit(&nd_table[i]);
}

static void destroy_bdrvs(int slot)
{
    int i;
    struct BlockDriverState *bs;

    for (i = 0; i <= MAX_DRIVES; i++) {
        bs = drives_table[i].bdrv;
        if (bs && (PCI_SLOT(bs->devfn) == slot)) {
            drive_uninit(bs);
            bdrv_delete(bs);
        }
    }
}

/*
 * OS has executed _EJ0 method, we now can remove the device
 */
void device_hot_remove_success(int pcibus, int slot)
{
    PCIDevice *d = pci_find_device(pcibus, slot);
    int class_code;

    if (!d) {
        term_printf("invalid slot %d\n", slot);
        return;
    }

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

    pci_unregister_device(d);

    switch(class_code) {
    case PCI_BASE_CLASS_STORAGE:
        destroy_bdrvs(slot);
        break;
    case PCI_BASE_CLASS_NETWORK:
        destroy_nic(slot);
        break;
    }

}