kvm: qemu: add missing include for ia64
[kvm-userspace.git] / qemu / hw / pci-hotplug.c
blobc2248c34ac5af15116446bfe6ef051150d16f169
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(PCIBus *pci_bus, const char *opts)
39 int ret;
41 ret = net_client_init ("nic", opts);
42 if (ret < 0 || !nd_table[ret].model)
43 return NULL;
44 return pci_nic_init (pci_bus, &nd_table[ret], -1, "rtl8139");
47 void drive_hot_add(Monitor *mon, const char *pci_addr, const char *opts)
49 int dom, pci_bus;
50 unsigned slot;
51 int drive_idx, type, bus;
52 int success = 0;
53 PCIDevice *dev;
55 if (pci_read_devaddr(pci_addr, &dom, &pci_bus, &slot)) {
56 monitor_printf(mon, "Invalid pci address\n");
57 return;
60 dev = pci_find_device(pci_bus, slot, 0);
61 if (!dev) {
62 monitor_printf(mon, "no pci device with address %s\n", pci_addr);
63 return;
66 drive_idx = add_init_drive(opts);
67 if (drive_idx < 0)
68 return;
69 type = drives_table[drive_idx].type;
70 bus = drive_get_max_bus (type);
72 switch (type) {
73 case IF_SCSI:
74 success = 1;
75 lsi_scsi_attach (dev, drives_table[drive_idx].bdrv,
76 drives_table[drive_idx].unit);
77 break;
78 default:
79 monitor_printf(mon, "Can't hot-add drive to type %d\n", type);
82 if (success)
83 monitor_printf(mon, "OK bus %d, unit %d\n",
84 drives_table[drive_idx].bus,
85 drives_table[drive_idx].unit);
86 return;
89 static PCIDevice *qemu_pci_hot_add_storage(Monitor *mon, PCIBus *pci_bus,
90 const char *opts)
92 void *opaque = NULL;
93 int type = -1, drive_idx = -1;
94 char buf[128];
96 if (get_param_value(buf, sizeof(buf), "if", opts)) {
97 if (!strcmp(buf, "scsi"))
98 type = IF_SCSI;
99 else if (!strcmp(buf, "virtio")) {
100 type = IF_VIRTIO;
101 } else {
102 monitor_printf(mon, "type %s not a hotpluggable PCI device.\n", buf);
103 goto out;
105 } else {
106 monitor_printf(mon, "no if= specified\n");
107 goto out;
110 if (get_param_value(buf, sizeof(buf), "file", opts)) {
111 drive_idx = add_init_drive(opts);
112 if (drive_idx < 0)
113 goto out;
114 } else if (type == IF_VIRTIO) {
115 monitor_printf(mon, "virtio requires a backing file/device.\n");
116 goto out;
119 switch (type) {
120 case IF_SCSI:
121 opaque = lsi_scsi_init (pci_bus, -1);
122 if (opaque && drive_idx >= 0)
123 lsi_scsi_attach (opaque, drives_table[drive_idx].bdrv,
124 drives_table[drive_idx].unit);
125 break;
126 case IF_VIRTIO:
127 opaque = virtio_blk_init (pci_bus, drives_table[drive_idx].bdrv);
128 break;
131 out:
132 return opaque;
135 #ifdef USE_KVM_DEVICE_ASSIGNMENT
136 static PCIDevice *qemu_pci_hot_assign_device(Monitor *mon,
137 PCIBus *pci_bus, const char *opts)
139 AssignedDevInfo *adev;
140 PCIDevice *ret;
142 adev = add_assigned_device(opts);
143 if (adev == NULL) {
144 monitor_printf(mon, "Error adding device; check syntax\n");
145 return NULL;
148 ret = init_assigned_device(adev, pci_bus);
149 if (ret == NULL) {
150 monitor_printf(mon, "Failed to assign device\n");
151 return NULL;
154 monitor_printf(mon,
155 "Registered host PCI device %02x:%02x.%1x "
156 "(\"%s\") as guest device %02x:%02x.%1x\n",
157 adev->bus, adev->dev, adev->func, adev->name,
158 pci_bus_num(pci_bus), (ret->devfn >> 3) & 0x1f,
159 adev->func);
161 return ret;
164 static void qemu_pci_hot_deassign_device(Monitor *mon, AssignedDevInfo *adev)
166 remove_assigned_device(adev);
168 monitor_printf(mon,
169 "Unregister host PCI device %02x:%02x.%1x "
170 "(\"%s\") from guest\n",
171 adev->bus, adev->dev, adev->func, adev->name);
173 #endif /* USE_KVM_DEVICE_ASSIGNMENT */
175 void pci_device_hot_add(Monitor *mon, const char *pci_addr, const char *type,
176 const char *opts)
178 PCIDevice *dev = NULL;
179 PCIBus *pci_bus;
180 int dom, bus;
181 unsigned slot;
183 if (pci_assign_devaddr(pci_addr, &dom, &bus, &slot)) {
184 monitor_printf(mon, "Invalid pci address\n");
185 return;
188 pci_bus = pci_find_bus(bus);
189 if (!pci_bus) {
190 monitor_printf(mon, "Can't find pci_bus %d\n", bus);
191 return;
194 if (strcmp(type, "nic") == 0)
195 dev = qemu_pci_hot_add_nic(pci_bus, opts);
196 else if (strcmp(type, "storage") == 0)
197 dev = qemu_pci_hot_add_storage(mon, pci_bus, opts);
198 #ifdef USE_KVM_DEVICE_ASSIGNMENT
199 else if (strcmp(type, "host") == 0)
200 dev = qemu_pci_hot_assign_device(mon, pci_bus, opts);
201 #endif /* USE_KVM_DEVICE_ASSIGNMENT */
202 else
203 monitor_printf(mon, "invalid type: %s\n", type);
205 if (dev) {
206 qemu_system_device_hot_add(bus, PCI_SLOT(dev->devfn), 1);
207 monitor_printf(mon, "OK domain %d, bus %d, slot %d, function %d\n",
208 0, pci_bus_num(dev->bus), PCI_SLOT(dev->devfn),
209 PCI_FUNC(dev->devfn));
210 } else
211 monitor_printf(mon, "failed to add %s\n", opts);
213 #endif
215 void pci_device_hot_remove(Monitor *mon, const char *pci_addr)
217 PCIDevice *d;
218 int dom, bus;
219 unsigned slot;
221 if (pci_read_devaddr(pci_addr, &dom, &bus, &slot)) {
222 monitor_printf(mon, "Invalid pci address\n");
223 return;
226 d = pci_find_device(bus, slot, 0);
227 if (!d) {
228 monitor_printf(mon, "slot %d empty\n", slot);
229 return;
232 qemu_system_device_hot_add(bus, slot, 0);
235 static int pci_match_fn(void *dev_private, void *arg)
237 PCIDevice *dev = dev_private;
238 PCIDevice *match = arg;
240 return (dev == match);
244 * OS has executed _EJ0 method, we now can remove the device
246 void pci_device_hot_remove_success(int pcibus, int slot)
248 PCIDevice *d = pci_find_device(pcibus, slot, 0);
249 int class_code;
250 AssignedDevInfo *adev;
252 if (!d) {
253 monitor_printf(cur_mon, "invalid slot %d\n", slot);
254 return;
257 #ifdef USE_KVM_DEVICE_ASSIGNMENT
258 adev = get_assigned_device(pcibus, slot);
259 if (adev) {
260 qemu_pci_hot_deassign_device(cur_mon, adev);
261 return;
263 #endif /* USE_KVM_DEVICE_ASSIGNMENT */
265 class_code = d->config_read(d, PCI_CLASS_DEVICE+1, 1);
267 switch(class_code) {
268 case PCI_BASE_CLASS_STORAGE:
269 destroy_bdrvs(pci_match_fn, d);
270 break;
271 case PCI_BASE_CLASS_NETWORK:
272 destroy_nic(pci_match_fn, d);
273 break;
276 pci_unregister_device(d);