Statistics
| Branch: | Revision:

root / hw / pci-hotplug.c @ 72cf2d4f

History | View | Annotate | Download (6.8 kB)

1 6f338c34 aliguori
/*
2 6f338c34 aliguori
 * QEMU PCI hotplug support
3 6f338c34 aliguori
 *
4 6f338c34 aliguori
 * Copyright (c) 2004 Fabrice Bellard
5 6f338c34 aliguori
 *
6 6f338c34 aliguori
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 6f338c34 aliguori
 * of this software and associated documentation files (the "Software"), to deal
8 6f338c34 aliguori
 * in the Software without restriction, including without limitation the rights
9 6f338c34 aliguori
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 6f338c34 aliguori
 * copies of the Software, and to permit persons to whom the Software is
11 6f338c34 aliguori
 * furnished to do so, subject to the following conditions:
12 6f338c34 aliguori
 *
13 6f338c34 aliguori
 * The above copyright notice and this permission notice shall be included in
14 6f338c34 aliguori
 * all copies or substantial portions of the Software.
15 6f338c34 aliguori
 *
16 6f338c34 aliguori
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 6f338c34 aliguori
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 6f338c34 aliguori
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 6f338c34 aliguori
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 6f338c34 aliguori
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 6f338c34 aliguori
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 6f338c34 aliguori
 * THE SOFTWARE.
23 6f338c34 aliguori
 */
24 6f338c34 aliguori
25 6f338c34 aliguori
#include "hw.h"
26 6f338c34 aliguori
#include "boards.h"
27 6f338c34 aliguori
#include "pci.h"
28 6f338c34 aliguori
#include "net.h"
29 6f338c34 aliguori
#include "sysemu.h"
30 6f338c34 aliguori
#include "pc.h"
31 376253ec aliguori
#include "monitor.h"
32 6f338c34 aliguori
#include "block_int.h"
33 d52affa7 Gerd Hoffmann
#include "scsi-disk.h"
34 6f338c34 aliguori
#include "virtio-blk.h"
35 6f338c34 aliguori
36 6f338c34 aliguori
#if defined(TARGET_I386) || defined(TARGET_X86_64)
37 1f5f6638 Markus Armbruster
static PCIDevice *qemu_pci_hot_add_nic(Monitor *mon,
38 1f5f6638 Markus Armbruster
                                       const char *devaddr, const char *opts)
39 6f338c34 aliguori
{
40 6f338c34 aliguori
    int ret;
41 6f338c34 aliguori
42 10ae5a7a Jan Kiszka
    ret = net_client_init(mon, "nic", opts);
43 eefb4091 aliguori
    if (ret < 0)
44 6f338c34 aliguori
        return NULL;
45 5607c388 Markus Armbruster
    if (nd_table[ret].devaddr) {
46 5607c388 Markus Armbruster
        monitor_printf(mon, "Parameter addr not supported\n");
47 5607c388 Markus Armbruster
        return NULL;
48 5607c388 Markus Armbruster
    }
49 1f5f6638 Markus Armbruster
    return pci_nic_init(&nd_table[ret], "rtl8139", devaddr);
50 6f338c34 aliguori
}
51 6f338c34 aliguori
52 f18c16de Luiz Capitulino
void drive_hot_add(Monitor *mon, const QDict *qdict)
53 6f338c34 aliguori
{
54 6f338c34 aliguori
    int dom, pci_bus;
55 6f338c34 aliguori
    unsigned slot;
56 751c6a17 Gerd Hoffmann
    int type, bus;
57 6f338c34 aliguori
    int success = 0;
58 6f338c34 aliguori
    PCIDevice *dev;
59 751c6a17 Gerd Hoffmann
    DriveInfo *dinfo;
60 f18c16de Luiz Capitulino
    const char *pci_addr = qdict_get_str(qdict, "pci_addr");
61 f18c16de Luiz Capitulino
    const char *opts = qdict_get_str(qdict, "opts");
62 d52affa7 Gerd Hoffmann
    BusState *scsibus;
63 6f338c34 aliguori
64 e9283f8b Jan Kiszka
    if (pci_read_devaddr(mon, pci_addr, &dom, &pci_bus, &slot)) {
65 6f338c34 aliguori
        return;
66 6f338c34 aliguori
    }
67 6f338c34 aliguori
68 6f338c34 aliguori
    dev = pci_find_device(pci_bus, slot, 0);
69 6f338c34 aliguori
    if (!dev) {
70 376253ec aliguori
        monitor_printf(mon, "no pci device with address %s\n", pci_addr);
71 6f338c34 aliguori
        return;
72 6f338c34 aliguori
    }
73 6f338c34 aliguori
74 751c6a17 Gerd Hoffmann
    dinfo = add_init_drive(opts);
75 751c6a17 Gerd Hoffmann
    if (!dinfo)
76 6f338c34 aliguori
        return;
77 751c6a17 Gerd Hoffmann
    if (dinfo->devaddr) {
78 c2cc47a4 Markus Armbruster
        monitor_printf(mon, "Parameter addr not supported\n");
79 c2cc47a4 Markus Armbruster
        return;
80 c2cc47a4 Markus Armbruster
    }
81 751c6a17 Gerd Hoffmann
    type = dinfo->type;
82 6f338c34 aliguori
    bus = drive_get_max_bus (type);
83 6f338c34 aliguori
84 6f338c34 aliguori
    switch (type) {
85 6f338c34 aliguori
    case IF_SCSI:
86 6f338c34 aliguori
        success = 1;
87 72cf2d4f Blue Swirl
        scsibus = QLIST_FIRST(&dev->qdev.child_bus);
88 d52affa7 Gerd Hoffmann
        scsi_bus_legacy_add_drive(DO_UPCAST(SCSIBus, qbus, scsibus),
89 d52affa7 Gerd Hoffmann
                                  dinfo, dinfo->unit);
90 6f338c34 aliguori
        break;
91 6f338c34 aliguori
    default:
92 376253ec aliguori
        monitor_printf(mon, "Can't hot-add drive to type %d\n", type);
93 6f338c34 aliguori
    }
94 6f338c34 aliguori
95 6f338c34 aliguori
    if (success)
96 376253ec aliguori
        monitor_printf(mon, "OK bus %d, unit %d\n",
97 751c6a17 Gerd Hoffmann
                       dinfo->bus,
98 751c6a17 Gerd Hoffmann
                       dinfo->unit);
99 6f338c34 aliguori
    return;
100 6f338c34 aliguori
}
101 6f338c34 aliguori
102 1f5f6638 Markus Armbruster
static PCIDevice *qemu_pci_hot_add_storage(Monitor *mon,
103 1f5f6638 Markus Armbruster
                                           const char *devaddr,
104 376253ec aliguori
                                           const char *opts)
105 6f338c34 aliguori
{
106 1f5f6638 Markus Armbruster
    PCIDevice *dev;
107 06c79f4e Sebastian Herbszt
    DriveInfo *dinfo = NULL;
108 751c6a17 Gerd Hoffmann
    int type = -1;
109 6f338c34 aliguori
    char buf[128];
110 6f338c34 aliguori
111 6f338c34 aliguori
    if (get_param_value(buf, sizeof(buf), "if", opts)) {
112 6f338c34 aliguori
        if (!strcmp(buf, "scsi"))
113 6f338c34 aliguori
            type = IF_SCSI;
114 6f338c34 aliguori
        else if (!strcmp(buf, "virtio")) {
115 6f338c34 aliguori
            type = IF_VIRTIO;
116 8707ecca aliguori
        } else {
117 8707ecca aliguori
            monitor_printf(mon, "type %s not a hotpluggable PCI device.\n", buf);
118 1f5f6638 Markus Armbruster
            return NULL;
119 6f338c34 aliguori
        }
120 6f338c34 aliguori
    } else {
121 376253ec aliguori
        monitor_printf(mon, "no if= specified\n");
122 1f5f6638 Markus Armbruster
        return NULL;
123 6f338c34 aliguori
    }
124 6f338c34 aliguori
125 6f338c34 aliguori
    if (get_param_value(buf, sizeof(buf), "file", opts)) {
126 751c6a17 Gerd Hoffmann
        dinfo = add_init_drive(opts);
127 751c6a17 Gerd Hoffmann
        if (!dinfo)
128 1f5f6638 Markus Armbruster
            return NULL;
129 751c6a17 Gerd Hoffmann
        if (dinfo->devaddr) {
130 c2cc47a4 Markus Armbruster
            monitor_printf(mon, "Parameter addr not supported\n");
131 c2cc47a4 Markus Armbruster
            return NULL;
132 c2cc47a4 Markus Armbruster
        }
133 7432ff5d Blue Swirl
    } else {
134 7432ff5d Blue Swirl
        dinfo = NULL;
135 6f338c34 aliguori
    }
136 6f338c34 aliguori
137 6f338c34 aliguori
    switch (type) {
138 6f338c34 aliguori
    case IF_SCSI:
139 1f5f6638 Markus Armbruster
        dev = pci_create("lsi53c895a", devaddr);
140 6f338c34 aliguori
        break;
141 6f338c34 aliguori
    case IF_VIRTIO:
142 7432ff5d Blue Swirl
        if (!dinfo) {
143 7432ff5d Blue Swirl
            monitor_printf(mon, "virtio requires a backing file/device.\n");
144 7432ff5d Blue Swirl
            return NULL;
145 7432ff5d Blue Swirl
        }
146 1f5f6638 Markus Armbruster
        dev = pci_create("virtio-blk-pci", devaddr);
147 d176c495 Gerd Hoffmann
        qdev_prop_set_drive(&dev->qdev, "drive", dinfo);
148 6f338c34 aliguori
        break;
149 1f5f6638 Markus Armbruster
    default:
150 1f5f6638 Markus Armbruster
        dev = NULL;
151 6f338c34 aliguori
    }
152 1f5f6638 Markus Armbruster
    if (dev)
153 1f5f6638 Markus Armbruster
        qdev_init(&dev->qdev);
154 1f5f6638 Markus Armbruster
    return dev;
155 6f338c34 aliguori
}
156 6f338c34 aliguori
157 1d4daa91 Luiz Capitulino
void pci_device_hot_add(Monitor *mon, const QDict *qdict)
158 6f338c34 aliguori
{
159 6f338c34 aliguori
    PCIDevice *dev = NULL;
160 1d4daa91 Luiz Capitulino
    const char *pci_addr = qdict_get_str(qdict, "pci_addr");
161 1d4daa91 Luiz Capitulino
    const char *type = qdict_get_str(qdict, "type");
162 1d4daa91 Luiz Capitulino
    const char *opts = qdict_get_try_str(qdict, "opts");
163 6f338c34 aliguori
164 e9283f8b Jan Kiszka
    /* strip legacy tag */
165 e9283f8b Jan Kiszka
    if (!strncmp(pci_addr, "pci_addr=", 9)) {
166 e9283f8b Jan Kiszka
        pci_addr += 9;
167 6f338c34 aliguori
    }
168 6f338c34 aliguori
169 a62acdc0 Jan Kiszka
    if (!opts) {
170 a62acdc0 Jan Kiszka
        opts = "";
171 a62acdc0 Jan Kiszka
    }
172 a62acdc0 Jan Kiszka
173 e9283f8b Jan Kiszka
    if (!strcmp(pci_addr, "auto"))
174 e9283f8b Jan Kiszka
        pci_addr = NULL;
175 6f338c34 aliguori
176 6f338c34 aliguori
    if (strcmp(type, "nic") == 0)
177 e9283f8b Jan Kiszka
        dev = qemu_pci_hot_add_nic(mon, pci_addr, opts);
178 6f338c34 aliguori
    else if (strcmp(type, "storage") == 0)
179 e9283f8b Jan Kiszka
        dev = qemu_pci_hot_add_storage(mon, pci_addr, opts);
180 6f338c34 aliguori
    else
181 376253ec aliguori
        monitor_printf(mon, "invalid type: %s\n", type);
182 6f338c34 aliguori
183 6f338c34 aliguori
    if (dev) {
184 1f5f6638 Markus Armbruster
        qemu_system_device_hot_add(pci_bus_num(dev->bus),
185 1f5f6638 Markus Armbruster
                                   PCI_SLOT(dev->devfn), 1);
186 376253ec aliguori
        monitor_printf(mon, "OK domain %d, bus %d, slot %d, function %d\n",
187 376253ec aliguori
                       0, pci_bus_num(dev->bus), PCI_SLOT(dev->devfn),
188 376253ec aliguori
                       PCI_FUNC(dev->devfn));
189 6f338c34 aliguori
    } else
190 376253ec aliguori
        monitor_printf(mon, "failed to add %s\n", opts);
191 6f338c34 aliguori
}
192 6f338c34 aliguori
#endif
193 6f338c34 aliguori
194 376253ec aliguori
void pci_device_hot_remove(Monitor *mon, const char *pci_addr)
195 6f338c34 aliguori
{
196 6f338c34 aliguori
    PCIDevice *d;
197 6f338c34 aliguori
    int dom, bus;
198 6f338c34 aliguori
    unsigned slot;
199 6f338c34 aliguori
200 e9283f8b Jan Kiszka
    if (pci_read_devaddr(mon, pci_addr, &dom, &bus, &slot)) {
201 6f338c34 aliguori
        return;
202 6f338c34 aliguori
    }
203 6f338c34 aliguori
204 6f338c34 aliguori
    d = pci_find_device(bus, slot, 0);
205 6f338c34 aliguori
    if (!d) {
206 376253ec aliguori
        monitor_printf(mon, "slot %d empty\n", slot);
207 6f338c34 aliguori
        return;
208 6f338c34 aliguori
    }
209 6f338c34 aliguori
210 6f338c34 aliguori
    qemu_system_device_hot_add(bus, slot, 0);
211 6f338c34 aliguori
}
212 6f338c34 aliguori
213 d54908a5 Luiz Capitulino
void do_pci_device_hot_remove(Monitor *mon, const QDict *qdict)
214 38183186 Luiz Capitulino
{
215 d54908a5 Luiz Capitulino
    pci_device_hot_remove(mon, qdict_get_str(qdict, "pci_addr"));
216 38183186 Luiz Capitulino
}
217 38183186 Luiz Capitulino
218 6f338c34 aliguori
static int pci_match_fn(void *dev_private, void *arg)
219 6f338c34 aliguori
{
220 6f338c34 aliguori
    PCIDevice *dev = dev_private;
221 6f338c34 aliguori
    PCIDevice *match = arg;
222 6f338c34 aliguori
223 6f338c34 aliguori
    return (dev == match);
224 6f338c34 aliguori
}
225 6f338c34 aliguori
226 6f338c34 aliguori
/*
227 6f338c34 aliguori
 * OS has executed _EJ0 method, we now can remove the device
228 6f338c34 aliguori
 */
229 6f338c34 aliguori
void pci_device_hot_remove_success(int pcibus, int slot)
230 6f338c34 aliguori
{
231 6f338c34 aliguori
    PCIDevice *d = pci_find_device(pcibus, slot, 0);
232 6f338c34 aliguori
    int class_code;
233 6f338c34 aliguori
234 6f338c34 aliguori
    if (!d) {
235 376253ec aliguori
        monitor_printf(cur_mon, "invalid slot %d\n", slot);
236 6f338c34 aliguori
        return;
237 6f338c34 aliguori
    }
238 6f338c34 aliguori
239 6f338c34 aliguori
    class_code = d->config_read(d, PCI_CLASS_DEVICE+1, 1);
240 6f338c34 aliguori
241 6f338c34 aliguori
    switch(class_code) {
242 6f338c34 aliguori
    case PCI_BASE_CLASS_STORAGE:
243 6f338c34 aliguori
        destroy_bdrvs(pci_match_fn, d);
244 6f338c34 aliguori
        break;
245 6f338c34 aliguori
    case PCI_BASE_CLASS_NETWORK:
246 6f338c34 aliguori
        destroy_nic(pci_match_fn, d);
247 6f338c34 aliguori
        break;
248 6f338c34 aliguori
    }
249 6f338c34 aliguori
250 6f338c34 aliguori
    pci_unregister_device(d);
251 6f338c34 aliguori
}