Statistics
| Branch: | Revision:

root / hw / pci-hotplug.c @ 7105b056

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