Merge branch 'master' of git://git.sv.gnu.org/qemu
[qemu-kvm/fedora.git] / hw / pci-hotplug.c
blob271d80010ed104f25b345e10ea857c4e9c0877d2
1 /*
2 * QEMU PCI hotplug support
4 * Copyright (c) 2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 #include "hw.h"
26 #include "boards.h"
27 #include "pci.h"
28 #include "net.h"
29 #include "sysemu.h"
30 #include "pc.h"
31 #include "monitor.h"
32 #include "block_int.h"
33 #include "virtio-blk.h"
34 #include "device-assignment.h"
36 #if defined(TARGET_I386) || defined(TARGET_X86_64)
37 static PCIDevice *qemu_pci_hot_add_nic(Monitor *mon, PCIBus *pci_bus,
38 const char *opts)
40 int ret;
42 ret = net_client_init(mon, "nic", opts);
43 if (ret < 0)
44 return NULL;
45 return pci_nic_init(pci_bus, &nd_table[ret], -1, "rtl8139");
48 void drive_hot_add(Monitor *mon, const char *pci_addr, const char *opts)
50 int dom, pci_bus;
51 unsigned slot;
52 int drive_idx, type, bus;
53 int success = 0;
54 PCIDevice *dev;
56 if (pci_read_devaddr(pci_addr, &dom, &pci_bus, &slot)) {
57 monitor_printf(mon, "Invalid pci address\n");
58 return;
61 dev = pci_find_device(pci_bus, slot, 0);
62 if (!dev) {
63 monitor_printf(mon, "no pci device with address %s\n", pci_addr);
64 return;
67 drive_idx = add_init_drive(opts);
68 if (drive_idx < 0)
69 return;
70 type = drives_table[drive_idx].type;
71 bus = drive_get_max_bus (type);
73 switch (type) {
74 case IF_SCSI:
75 success = 1;
76 lsi_scsi_attach(&dev->qdev, drives_table[drive_idx].bdrv,
77 drives_table[drive_idx].unit);
78 break;
79 default:
80 monitor_printf(mon, "Can't hot-add drive to type %d\n", type);
83 if (success)
84 monitor_printf(mon, "OK bus %d, unit %d\n",
85 drives_table[drive_idx].bus,
86 drives_table[drive_idx].unit);
87 return;
90 static PCIDevice *qemu_pci_hot_add_storage(Monitor *mon, PCIBus *pci_bus,
91 const char *opts)
93 void *opaque = NULL;
94 int type = -1, drive_idx = -1;
95 char buf[128];
97 if (get_param_value(buf, sizeof(buf), "if", opts)) {
98 if (!strcmp(buf, "scsi"))
99 type = IF_SCSI;
100 else if (!strcmp(buf, "virtio")) {
101 type = IF_VIRTIO;
102 } else {
103 monitor_printf(mon, "type %s not a hotpluggable PCI device.\n", buf);
104 goto out;
106 } else {
107 monitor_printf(mon, "no if= specified\n");
108 goto out;
111 if (get_param_value(buf, sizeof(buf), "file", opts)) {
112 drive_idx = add_init_drive(opts);
113 if (drive_idx < 0)
114 goto out;
115 } else if (type == IF_VIRTIO) {
116 monitor_printf(mon, "virtio requires a backing file/device.\n");
117 goto out;
120 switch (type) {
121 case IF_SCSI:
122 opaque = pci_create_simple(pci_bus, -1, "lsi53c895a");
123 break;
124 case IF_VIRTIO:
125 opaque = pci_create_simple(pci_bus, -1, "virtio-blk-pci");
126 break;
129 out:
130 return opaque;
133 #ifdef USE_KVM_DEVICE_ASSIGNMENT
134 static PCIDevice *qemu_pci_hot_assign_device(Monitor *mon,
135 PCIBus *pci_bus, const char *opts)
137 AssignedDevInfo *adev;
138 PCIDevice *ret;
140 adev = add_assigned_device(opts);
141 if (adev == NULL) {
142 monitor_printf(mon, "Error adding device; check syntax\n");
143 return NULL;
146 ret = init_assigned_device(adev, pci_bus);
147 if (ret == NULL) {
148 monitor_printf(mon, "Failed to assign device\n");
149 return NULL;
152 monitor_printf(mon,
153 "Registered host PCI device %02x:%02x.%1x "
154 "(\"%s\") as guest device %02x:%02x.%1x\n",
155 adev->bus, adev->dev, adev->func, adev->name,
156 pci_bus_num(pci_bus), (ret->devfn >> 3) & 0x1f,
157 adev->func);
159 return ret;
162 static void qemu_pci_hot_deassign_device(Monitor *mon, AssignedDevInfo *adev)
164 remove_assigned_device(adev);
166 monitor_printf(mon,
167 "Unregister host PCI device %02x:%02x.%1x "
168 "(\"%s\") from guest\n",
169 adev->bus, adev->dev, adev->func, adev->name);
171 #endif /* USE_KVM_DEVICE_ASSIGNMENT */
173 void pci_device_hot_add(Monitor *mon, const char *pci_addr, const char *type,
174 const char *opts)
176 PCIDevice *dev = NULL;
177 PCIBus *pci_bus;
178 int dom, bus;
179 unsigned slot;
181 if (pci_assign_devaddr(pci_addr, &dom, &bus, &slot)) {
182 monitor_printf(mon, "Invalid pci address\n");
183 return;
186 pci_bus = pci_find_bus(bus);
187 if (!pci_bus) {
188 monitor_printf(mon, "Can't find pci_bus %d\n", bus);
189 return;
192 if (strcmp(type, "nic") == 0)
193 dev = qemu_pci_hot_add_nic(mon, pci_bus, opts);
194 else if (strcmp(type, "storage") == 0)
195 dev = qemu_pci_hot_add_storage(mon, pci_bus, opts);
196 #ifdef USE_KVM_DEVICE_ASSIGNMENT
197 else if (strcmp(type, "host") == 0)
198 dev = qemu_pci_hot_assign_device(mon, pci_bus, opts);
199 #endif /* USE_KVM_DEVICE_ASSIGNMENT */
200 else
201 monitor_printf(mon, "invalid type: %s\n", type);
203 if (dev) {
204 qemu_system_device_hot_add(bus, PCI_SLOT(dev->devfn), 1);
205 monitor_printf(mon, "OK domain %d, bus %d, slot %d, function %d\n",
206 0, pci_bus_num(dev->bus), PCI_SLOT(dev->devfn),
207 PCI_FUNC(dev->devfn));
208 } else
209 monitor_printf(mon, "failed to add %s\n", opts);
211 #endif
213 void pci_device_hot_remove(Monitor *mon, const char *pci_addr)
215 PCIDevice *d;
216 int dom, bus;
217 unsigned slot;
219 if (pci_read_devaddr(pci_addr, &dom, &bus, &slot)) {
220 monitor_printf(mon, "Invalid pci address\n");
221 return;
224 d = pci_find_device(bus, slot, 0);
225 if (!d) {
226 monitor_printf(mon, "slot %d empty\n", slot);
227 return;
230 qemu_system_device_hot_add(bus, slot, 0);
233 static int pci_match_fn(void *dev_private, void *arg)
235 PCIDevice *dev = dev_private;
236 PCIDevice *match = arg;
238 return (dev == match);
242 * OS has executed _EJ0 method, we now can remove the device
244 void pci_device_hot_remove_success(int pcibus, int slot)
246 PCIDevice *d = pci_find_device(pcibus, slot, 0);
247 int class_code;
248 AssignedDevInfo *adev;
250 if (!d) {
251 monitor_printf(cur_mon, "invalid slot %d\n", slot);
252 return;
255 #ifdef USE_KVM_DEVICE_ASSIGNMENT
256 adev = get_assigned_device(pcibus, slot);
257 if (adev) {
258 qemu_pci_hot_deassign_device(cur_mon, adev);
259 return;
261 #endif /* USE_KVM_DEVICE_ASSIGNMENT */
263 class_code = d->config_read(d, PCI_CLASS_DEVICE+1, 1);
265 switch(class_code) {
266 case PCI_BASE_CLASS_STORAGE:
267 destroy_bdrvs(pci_match_fn, d);
268 break;
269 case PCI_BASE_CLASS_NETWORK:
270 destroy_nic(pci_match_fn, d);
271 break;
274 pci_unregister_device(d, 0);