Statistics
| Branch: | Revision:

root / hw / pci-hotplug.c @ 02eb84d0

History | View | Annotate | Download (6.5 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 6f338c34 aliguori
    int drive_idx, type, bus;
56 6f338c34 aliguori
    int success = 0;
57 6f338c34 aliguori
    PCIDevice *dev;
58 6f338c34 aliguori
59 6f338c34 aliguori
    if (pci_read_devaddr(pci_addr, &dom, &pci_bus, &slot)) {
60 376253ec aliguori
        monitor_printf(mon, "Invalid pci address\n");
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 6f338c34 aliguori
    drive_idx = add_init_drive(opts);
71 6f338c34 aliguori
    if (drive_idx < 0)
72 6f338c34 aliguori
        return;
73 c2cc47a4 Markus Armbruster
    if (drives_table[drive_idx].devaddr) {
74 c2cc47a4 Markus Armbruster
        monitor_printf(mon, "Parameter addr not supported\n");
75 c2cc47a4 Markus Armbruster
        return;
76 c2cc47a4 Markus Armbruster
    }
77 6f338c34 aliguori
    type = drives_table[drive_idx].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 9be5dafe Paul Brook
        lsi_scsi_attach(&dev->qdev, drives_table[drive_idx].bdrv,
84 9be5dafe Paul Brook
                        drives_table[drive_idx].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 376253ec aliguori
                       drives_table[drive_idx].bus,
93 376253ec aliguori
                       drives_table[drive_idx].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 6f338c34 aliguori
    int type = -1, drive_idx = -1;
103 6f338c34 aliguori
    char buf[128];
104 6f338c34 aliguori
105 6f338c34 aliguori
    if (get_param_value(buf, sizeof(buf), "if", opts)) {
106 6f338c34 aliguori
        if (!strcmp(buf, "scsi"))
107 6f338c34 aliguori
            type = IF_SCSI;
108 6f338c34 aliguori
        else if (!strcmp(buf, "virtio")) {
109 6f338c34 aliguori
            type = IF_VIRTIO;
110 8707ecca aliguori
        } else {
111 8707ecca aliguori
            monitor_printf(mon, "type %s not a hotpluggable PCI device.\n", buf);
112 1f5f6638 Markus Armbruster
            return NULL;
113 6f338c34 aliguori
        }
114 6f338c34 aliguori
    } else {
115 376253ec aliguori
        monitor_printf(mon, "no if= specified\n");
116 1f5f6638 Markus Armbruster
        return NULL;
117 6f338c34 aliguori
    }
118 6f338c34 aliguori
119 6f338c34 aliguori
    if (get_param_value(buf, sizeof(buf), "file", opts)) {
120 6f338c34 aliguori
        drive_idx = add_init_drive(opts);
121 6f338c34 aliguori
        if (drive_idx < 0)
122 1f5f6638 Markus Armbruster
            return NULL;
123 c2cc47a4 Markus Armbruster
        if (drives_table[drive_idx].devaddr) {
124 c2cc47a4 Markus Armbruster
            monitor_printf(mon, "Parameter addr not supported\n");
125 c2cc47a4 Markus Armbruster
            return NULL;
126 c2cc47a4 Markus Armbruster
        }
127 6f338c34 aliguori
    } else if (type == IF_VIRTIO) {
128 376253ec aliguori
        monitor_printf(mon, "virtio requires a backing file/device.\n");
129 1f5f6638 Markus Armbruster
        return 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 1f5f6638 Markus Armbruster
        dev = pci_create("virtio-blk-pci", devaddr);
138 6f338c34 aliguori
        break;
139 1f5f6638 Markus Armbruster
    default:
140 1f5f6638 Markus Armbruster
        dev = NULL;
141 6f338c34 aliguori
    }
142 1f5f6638 Markus Armbruster
    if (dev)
143 1f5f6638 Markus Armbruster
        qdev_init(&dev->qdev);
144 1f5f6638 Markus Armbruster
    return dev;
145 6f338c34 aliguori
}
146 6f338c34 aliguori
147 376253ec aliguori
void pci_device_hot_add(Monitor *mon, const char *pci_addr, const char *type,
148 376253ec aliguori
                        const char *opts)
149 6f338c34 aliguori
{
150 6f338c34 aliguori
    PCIDevice *dev = NULL;
151 1f5f6638 Markus Armbruster
    const char *devaddr = NULL;
152 1f5f6638 Markus Armbruster
    char buf[32];
153 6f338c34 aliguori
154 1f5f6638 Markus Armbruster
    if (!get_param_value(buf, sizeof(buf), "pci_addr", pci_addr)) {
155 376253ec aliguori
        monitor_printf(mon, "Invalid pci address\n");
156 6f338c34 aliguori
        return;
157 6f338c34 aliguori
    }
158 6f338c34 aliguori
159 1f5f6638 Markus Armbruster
    if (strcmp(buf, "auto"))
160 1f5f6638 Markus Armbruster
        devaddr = buf;
161 6f338c34 aliguori
162 6f338c34 aliguori
    if (strcmp(type, "nic") == 0)
163 1f5f6638 Markus Armbruster
        dev = qemu_pci_hot_add_nic(mon, devaddr, opts);
164 6f338c34 aliguori
    else if (strcmp(type, "storage") == 0)
165 1f5f6638 Markus Armbruster
        dev = qemu_pci_hot_add_storage(mon, devaddr, opts);
166 6f338c34 aliguori
    else
167 376253ec aliguori
        monitor_printf(mon, "invalid type: %s\n", type);
168 6f338c34 aliguori
169 6f338c34 aliguori
    if (dev) {
170 1f5f6638 Markus Armbruster
        qemu_system_device_hot_add(pci_bus_num(dev->bus),
171 1f5f6638 Markus Armbruster
                                   PCI_SLOT(dev->devfn), 1);
172 376253ec aliguori
        monitor_printf(mon, "OK domain %d, bus %d, slot %d, function %d\n",
173 376253ec aliguori
                       0, pci_bus_num(dev->bus), PCI_SLOT(dev->devfn),
174 376253ec aliguori
                       PCI_FUNC(dev->devfn));
175 6f338c34 aliguori
    } else
176 376253ec aliguori
        monitor_printf(mon, "failed to add %s\n", opts);
177 6f338c34 aliguori
}
178 6f338c34 aliguori
#endif
179 6f338c34 aliguori
180 376253ec aliguori
void pci_device_hot_remove(Monitor *mon, const char *pci_addr)
181 6f338c34 aliguori
{
182 6f338c34 aliguori
    PCIDevice *d;
183 6f338c34 aliguori
    int dom, bus;
184 6f338c34 aliguori
    unsigned slot;
185 6f338c34 aliguori
186 6f338c34 aliguori
    if (pci_read_devaddr(pci_addr, &dom, &bus, &slot)) {
187 376253ec aliguori
        monitor_printf(mon, "Invalid pci address\n");
188 6f338c34 aliguori
        return;
189 6f338c34 aliguori
    }
190 6f338c34 aliguori
191 6f338c34 aliguori
    d = pci_find_device(bus, slot, 0);
192 6f338c34 aliguori
    if (!d) {
193 376253ec aliguori
        monitor_printf(mon, "slot %d empty\n", slot);
194 6f338c34 aliguori
        return;
195 6f338c34 aliguori
    }
196 6f338c34 aliguori
197 6f338c34 aliguori
    qemu_system_device_hot_add(bus, slot, 0);
198 6f338c34 aliguori
}
199 6f338c34 aliguori
200 6f338c34 aliguori
static int pci_match_fn(void *dev_private, void *arg)
201 6f338c34 aliguori
{
202 6f338c34 aliguori
    PCIDevice *dev = dev_private;
203 6f338c34 aliguori
    PCIDevice *match = arg;
204 6f338c34 aliguori
205 6f338c34 aliguori
    return (dev == match);
206 6f338c34 aliguori
}
207 6f338c34 aliguori
208 6f338c34 aliguori
/*
209 6f338c34 aliguori
 * OS has executed _EJ0 method, we now can remove the device
210 6f338c34 aliguori
 */
211 6f338c34 aliguori
void pci_device_hot_remove_success(int pcibus, int slot)
212 6f338c34 aliguori
{
213 6f338c34 aliguori
    PCIDevice *d = pci_find_device(pcibus, slot, 0);
214 6f338c34 aliguori
    int class_code;
215 6f338c34 aliguori
216 6f338c34 aliguori
    if (!d) {
217 376253ec aliguori
        monitor_printf(cur_mon, "invalid slot %d\n", slot);
218 6f338c34 aliguori
        return;
219 6f338c34 aliguori
    }
220 6f338c34 aliguori
221 6f338c34 aliguori
    class_code = d->config_read(d, PCI_CLASS_DEVICE+1, 1);
222 6f338c34 aliguori
223 6f338c34 aliguori
    switch(class_code) {
224 6f338c34 aliguori
    case PCI_BASE_CLASS_STORAGE:
225 6f338c34 aliguori
        destroy_bdrvs(pci_match_fn, d);
226 6f338c34 aliguori
        break;
227 6f338c34 aliguori
    case PCI_BASE_CLASS_NETWORK:
228 6f338c34 aliguori
        destroy_nic(pci_match_fn, d);
229 6f338c34 aliguori
        break;
230 6f338c34 aliguori
    }
231 6f338c34 aliguori
232 6f338c34 aliguori
    pci_unregister_device(d);
233 6f338c34 aliguori
}