Statistics
| Branch: | Revision:

root / hw / pci-hotplug.c @ a8a358bf

History | View | Annotate | Download (6.4 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 6f338c34 aliguori
#include "virtio-blk.h"
34 6f338c34 aliguori
35 6f338c34 aliguori
#if defined(TARGET_I386) || defined(TARGET_X86_64)
36 1f5f6638 Markus Armbruster
static PCIDevice *qemu_pci_hot_add_nic(Monitor *mon,
37 1f5f6638 Markus Armbruster
                                       const char *devaddr, const char *opts)
38 6f338c34 aliguori
{
39 6f338c34 aliguori
    int ret;
40 6f338c34 aliguori
41 10ae5a7a Jan Kiszka
    ret = net_client_init(mon, "nic", opts);
42 eefb4091 aliguori
    if (ret < 0)
43 6f338c34 aliguori
        return NULL;
44 5607c388 Markus Armbruster
    if (nd_table[ret].devaddr) {
45 5607c388 Markus Armbruster
        monitor_printf(mon, "Parameter addr not supported\n");
46 5607c388 Markus Armbruster
        return NULL;
47 5607c388 Markus Armbruster
    }
48 1f5f6638 Markus Armbruster
    return pci_nic_init(&nd_table[ret], "rtl8139", devaddr);
49 6f338c34 aliguori
}
50 6f338c34 aliguori
51 376253ec aliguori
void drive_hot_add(Monitor *mon, const char *pci_addr, const char *opts)
52 6f338c34 aliguori
{
53 6f338c34 aliguori
    int dom, pci_bus;
54 6f338c34 aliguori
    unsigned slot;
55 751c6a17 Gerd Hoffmann
    int type, bus;
56 6f338c34 aliguori
    int success = 0;
57 6f338c34 aliguori
    PCIDevice *dev;
58 751c6a17 Gerd Hoffmann
    DriveInfo *dinfo;
59 6f338c34 aliguori
60 e9283f8b Jan Kiszka
    if (pci_read_devaddr(mon, pci_addr, &dom, &pci_bus, &slot)) {
61 6f338c34 aliguori
        return;
62 6f338c34 aliguori
    }
63 6f338c34 aliguori
64 6f338c34 aliguori
    dev = pci_find_device(pci_bus, slot, 0);
65 6f338c34 aliguori
    if (!dev) {
66 376253ec aliguori
        monitor_printf(mon, "no pci device with address %s\n", pci_addr);
67 6f338c34 aliguori
        return;
68 6f338c34 aliguori
    }
69 6f338c34 aliguori
70 751c6a17 Gerd Hoffmann
    dinfo = add_init_drive(opts);
71 751c6a17 Gerd Hoffmann
    if (!dinfo)
72 6f338c34 aliguori
        return;
73 751c6a17 Gerd Hoffmann
    if (dinfo->devaddr) {
74 c2cc47a4 Markus Armbruster
        monitor_printf(mon, "Parameter addr not supported\n");
75 c2cc47a4 Markus Armbruster
        return;
76 c2cc47a4 Markus Armbruster
    }
77 751c6a17 Gerd Hoffmann
    type = dinfo->type;
78 6f338c34 aliguori
    bus = drive_get_max_bus (type);
79 6f338c34 aliguori
80 6f338c34 aliguori
    switch (type) {
81 6f338c34 aliguori
    case IF_SCSI:
82 6f338c34 aliguori
        success = 1;
83 751c6a17 Gerd Hoffmann
        lsi_scsi_attach(&dev->qdev, dinfo->bdrv,
84 751c6a17 Gerd Hoffmann
                        dinfo->unit);
85 6f338c34 aliguori
        break;
86 6f338c34 aliguori
    default:
87 376253ec aliguori
        monitor_printf(mon, "Can't hot-add drive to type %d\n", type);
88 6f338c34 aliguori
    }
89 6f338c34 aliguori
90 6f338c34 aliguori
    if (success)
91 376253ec aliguori
        monitor_printf(mon, "OK bus %d, unit %d\n",
92 751c6a17 Gerd Hoffmann
                       dinfo->bus,
93 751c6a17 Gerd Hoffmann
                       dinfo->unit);
94 6f338c34 aliguori
    return;
95 6f338c34 aliguori
}
96 6f338c34 aliguori
97 1f5f6638 Markus Armbruster
static PCIDevice *qemu_pci_hot_add_storage(Monitor *mon,
98 1f5f6638 Markus Armbruster
                                           const char *devaddr,
99 376253ec aliguori
                                           const char *opts)
100 6f338c34 aliguori
{
101 1f5f6638 Markus Armbruster
    PCIDevice *dev;
102 751c6a17 Gerd Hoffmann
    DriveInfo *dinfo;
103 751c6a17 Gerd Hoffmann
    int type = -1;
104 6f338c34 aliguori
    char buf[128];
105 6f338c34 aliguori
106 6f338c34 aliguori
    if (get_param_value(buf, sizeof(buf), "if", opts)) {
107 6f338c34 aliguori
        if (!strcmp(buf, "scsi"))
108 6f338c34 aliguori
            type = IF_SCSI;
109 6f338c34 aliguori
        else if (!strcmp(buf, "virtio")) {
110 6f338c34 aliguori
            type = IF_VIRTIO;
111 8707ecca aliguori
        } else {
112 8707ecca aliguori
            monitor_printf(mon, "type %s not a hotpluggable PCI device.\n", buf);
113 1f5f6638 Markus Armbruster
            return NULL;
114 6f338c34 aliguori
        }
115 6f338c34 aliguori
    } else {
116 376253ec aliguori
        monitor_printf(mon, "no if= specified\n");
117 1f5f6638 Markus Armbruster
        return NULL;
118 6f338c34 aliguori
    }
119 6f338c34 aliguori
120 6f338c34 aliguori
    if (get_param_value(buf, sizeof(buf), "file", opts)) {
121 751c6a17 Gerd Hoffmann
        dinfo = add_init_drive(opts);
122 751c6a17 Gerd Hoffmann
        if (!dinfo)
123 1f5f6638 Markus Armbruster
            return NULL;
124 751c6a17 Gerd Hoffmann
        if (dinfo->devaddr) {
125 c2cc47a4 Markus Armbruster
            monitor_printf(mon, "Parameter addr not supported\n");
126 c2cc47a4 Markus Armbruster
            return NULL;
127 c2cc47a4 Markus Armbruster
        }
128 7432ff5d Blue Swirl
    } else {
129 7432ff5d Blue Swirl
        dinfo = NULL;
130 6f338c34 aliguori
    }
131 6f338c34 aliguori
132 6f338c34 aliguori
    switch (type) {
133 6f338c34 aliguori
    case IF_SCSI:
134 1f5f6638 Markus Armbruster
        dev = pci_create("lsi53c895a", devaddr);
135 6f338c34 aliguori
        break;
136 6f338c34 aliguori
    case IF_VIRTIO:
137 7432ff5d Blue Swirl
        if (!dinfo) {
138 7432ff5d Blue Swirl
            monitor_printf(mon, "virtio requires a backing file/device.\n");
139 7432ff5d Blue Swirl
            return NULL;
140 7432ff5d Blue Swirl
        }
141 1f5f6638 Markus Armbruster
        dev = pci_create("virtio-blk-pci", devaddr);
142 d176c495 Gerd Hoffmann
        qdev_prop_set_drive(&dev->qdev, "drive", dinfo);
143 6f338c34 aliguori
        break;
144 1f5f6638 Markus Armbruster
    default:
145 1f5f6638 Markus Armbruster
        dev = NULL;
146 6f338c34 aliguori
    }
147 1f5f6638 Markus Armbruster
    if (dev)
148 1f5f6638 Markus Armbruster
        qdev_init(&dev->qdev);
149 1f5f6638 Markus Armbruster
    return dev;
150 6f338c34 aliguori
}
151 6f338c34 aliguori
152 376253ec aliguori
void pci_device_hot_add(Monitor *mon, const char *pci_addr, const char *type,
153 376253ec aliguori
                        const char *opts)
154 6f338c34 aliguori
{
155 6f338c34 aliguori
    PCIDevice *dev = NULL;
156 6f338c34 aliguori
157 e9283f8b Jan Kiszka
    /* strip legacy tag */
158 e9283f8b Jan Kiszka
    if (!strncmp(pci_addr, "pci_addr=", 9)) {
159 e9283f8b Jan Kiszka
        pci_addr += 9;
160 6f338c34 aliguori
    }
161 6f338c34 aliguori
162 a62acdc0 Jan Kiszka
    if (!opts) {
163 a62acdc0 Jan Kiszka
        opts = "";
164 a62acdc0 Jan Kiszka
    }
165 a62acdc0 Jan Kiszka
166 e9283f8b Jan Kiszka
    if (!strcmp(pci_addr, "auto"))
167 e9283f8b Jan Kiszka
        pci_addr = NULL;
168 6f338c34 aliguori
169 6f338c34 aliguori
    if (strcmp(type, "nic") == 0)
170 e9283f8b Jan Kiszka
        dev = qemu_pci_hot_add_nic(mon, pci_addr, opts);
171 6f338c34 aliguori
    else if (strcmp(type, "storage") == 0)
172 e9283f8b Jan Kiszka
        dev = qemu_pci_hot_add_storage(mon, pci_addr, opts);
173 6f338c34 aliguori
    else
174 376253ec aliguori
        monitor_printf(mon, "invalid type: %s\n", type);
175 6f338c34 aliguori
176 6f338c34 aliguori
    if (dev) {
177 1f5f6638 Markus Armbruster
        qemu_system_device_hot_add(pci_bus_num(dev->bus),
178 1f5f6638 Markus Armbruster
                                   PCI_SLOT(dev->devfn), 1);
179 376253ec aliguori
        monitor_printf(mon, "OK domain %d, bus %d, slot %d, function %d\n",
180 376253ec aliguori
                       0, pci_bus_num(dev->bus), PCI_SLOT(dev->devfn),
181 376253ec aliguori
                       PCI_FUNC(dev->devfn));
182 6f338c34 aliguori
    } else
183 376253ec aliguori
        monitor_printf(mon, "failed to add %s\n", opts);
184 6f338c34 aliguori
}
185 6f338c34 aliguori
#endif
186 6f338c34 aliguori
187 376253ec aliguori
void pci_device_hot_remove(Monitor *mon, const char *pci_addr)
188 6f338c34 aliguori
{
189 6f338c34 aliguori
    PCIDevice *d;
190 6f338c34 aliguori
    int dom, bus;
191 6f338c34 aliguori
    unsigned slot;
192 6f338c34 aliguori
193 e9283f8b Jan Kiszka
    if (pci_read_devaddr(mon, pci_addr, &dom, &bus, &slot)) {
194 6f338c34 aliguori
        return;
195 6f338c34 aliguori
    }
196 6f338c34 aliguori
197 6f338c34 aliguori
    d = pci_find_device(bus, slot, 0);
198 6f338c34 aliguori
    if (!d) {
199 376253ec aliguori
        monitor_printf(mon, "slot %d empty\n", slot);
200 6f338c34 aliguori
        return;
201 6f338c34 aliguori
    }
202 6f338c34 aliguori
203 6f338c34 aliguori
    qemu_system_device_hot_add(bus, slot, 0);
204 6f338c34 aliguori
}
205 6f338c34 aliguori
206 6f338c34 aliguori
static int pci_match_fn(void *dev_private, void *arg)
207 6f338c34 aliguori
{
208 6f338c34 aliguori
    PCIDevice *dev = dev_private;
209 6f338c34 aliguori
    PCIDevice *match = arg;
210 6f338c34 aliguori
211 6f338c34 aliguori
    return (dev == match);
212 6f338c34 aliguori
}
213 6f338c34 aliguori
214 6f338c34 aliguori
/*
215 6f338c34 aliguori
 * OS has executed _EJ0 method, we now can remove the device
216 6f338c34 aliguori
 */
217 6f338c34 aliguori
void pci_device_hot_remove_success(int pcibus, int slot)
218 6f338c34 aliguori
{
219 6f338c34 aliguori
    PCIDevice *d = pci_find_device(pcibus, slot, 0);
220 6f338c34 aliguori
    int class_code;
221 6f338c34 aliguori
222 6f338c34 aliguori
    if (!d) {
223 376253ec aliguori
        monitor_printf(cur_mon, "invalid slot %d\n", slot);
224 6f338c34 aliguori
        return;
225 6f338c34 aliguori
    }
226 6f338c34 aliguori
227 6f338c34 aliguori
    class_code = d->config_read(d, PCI_CLASS_DEVICE+1, 1);
228 6f338c34 aliguori
229 6f338c34 aliguori
    switch(class_code) {
230 6f338c34 aliguori
    case PCI_BASE_CLASS_STORAGE:
231 6f338c34 aliguori
        destroy_bdrvs(pci_match_fn, d);
232 6f338c34 aliguori
        break;
233 6f338c34 aliguori
    case PCI_BASE_CLASS_NETWORK:
234 6f338c34 aliguori
        destroy_nic(pci_match_fn, d);
235 6f338c34 aliguori
        break;
236 6f338c34 aliguori
    }
237 6f338c34 aliguori
238 6f338c34 aliguori
    pci_unregister_device(d);
239 6f338c34 aliguori
}