Fix hot remove assigned device
[qemu-kvm/fedora.git] / hw / pci-hotplug.c
blob35658512eacfd08e1215d2566725f4f7ea046eb4
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 "console.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(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 term_printf("Invalid pci address\n");
57 return;
60 dev = pci_find_device(pci_bus, slot, 0);
61 if (!dev) {
62 term_printf("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 term_printf("Can't hot-add drive to type %d\n", type);
82 if (success)
83 term_printf("OK bus %d, unit %d\n", drives_table[drive_idx].bus,
84 drives_table[drive_idx].unit);
85 return;
88 static PCIDevice *qemu_pci_hot_add_storage(PCIBus *pci_bus, const char *opts)
90 void *opaque = NULL;
91 int type = -1, drive_idx = -1;
92 char buf[128];
94 if (get_param_value(buf, sizeof(buf), "if", opts)) {
95 if (!strcmp(buf, "scsi"))
96 type = IF_SCSI;
97 else if (!strcmp(buf, "virtio")) {
98 type = IF_VIRTIO;
100 } else {
101 term_printf("no if= specified\n");
102 return NULL;
105 if (get_param_value(buf, sizeof(buf), "file", opts)) {
106 drive_idx = add_init_drive(opts);
107 if (drive_idx < 0)
108 return NULL;
109 } else if (type == IF_VIRTIO) {
110 term_printf("virtio requires a backing file/device.\n");
111 return NULL;
114 switch (type) {
115 case IF_SCSI:
116 opaque = lsi_scsi_init (pci_bus, -1);
117 if (opaque && drive_idx >= 0)
118 lsi_scsi_attach (opaque, drives_table[drive_idx].bdrv,
119 drives_table[drive_idx].unit);
120 break;
121 case IF_VIRTIO:
122 opaque = virtio_blk_init (pci_bus, drives_table[drive_idx].bdrv);
123 break;
124 default:
125 term_printf ("type %s not a hotpluggable PCI device.\n", buf);
128 return opaque;
131 #ifdef USE_KVM_DEVICE_ASSIGNMENT
132 static PCIDevice *qemu_pci_hot_assign_device(PCIBus *pci_bus, const char *opts)
134 AssignedDevInfo *adev;
135 PCIDevice *ret;
137 adev = add_assigned_device(opts);
138 if (adev == NULL) {
139 term_printf ("Error adding device; check syntax\n");
140 return NULL;
143 ret = init_assigned_device(adev, pci_bus);
144 if (ret == NULL) {
145 term_printf("Failed to assign device\n");
146 return NULL;
149 term_printf("Registered host PCI device %02x:%02x.%1x "
150 "(\"%s\") as guest device %02x:%02x.%1x\n",
151 adev->bus, adev->dev, adev->func, adev->name,
152 pci_bus_num(pci_bus), (ret->devfn >> 3) & 0x1f,
153 adev->func);
155 return ret;
158 static void qemu_pci_hot_deassign_device(AssignedDevInfo *adev)
160 remove_assigned_device(adev);
162 term_printf("Unregister host PCI device %02x:%02x.%1x "
163 "(\"%s\") from guest\n",
164 adev->bus, adev->dev, adev->func, adev->name);
166 #endif /* USE_KVM_DEVICE_ASSIGNMENT */
168 void pci_device_hot_add(const char *pci_addr, const char *type, const char *opts)
170 PCIDevice *dev = NULL;
171 PCIBus *pci_bus;
172 int dom, bus;
173 unsigned slot;
175 if (pci_assign_devaddr(pci_addr, &dom, &bus, &slot)) {
176 term_printf("Invalid pci address\n");
177 return;
180 pci_bus = pci_find_bus(bus);
181 if (!pci_bus) {
182 term_printf("Can't find pci_bus %d\n", bus);
183 return;
186 if (strcmp(type, "nic") == 0)
187 dev = qemu_pci_hot_add_nic(pci_bus, opts);
188 else if (strcmp(type, "storage") == 0)
189 dev = qemu_pci_hot_add_storage(pci_bus, opts);
190 #ifdef USE_KVM_DEVICE_ASSIGNMENT
191 else if (strcmp(type, "host") == 0)
192 dev = qemu_pci_hot_assign_device(pci_bus, opts);
193 #endif /* USE_KVM_DEVICE_ASSIGNMENT */
194 else
195 term_printf("invalid type: %s\n", type);
197 if (dev) {
198 qemu_system_device_hot_add(bus, PCI_SLOT(dev->devfn), 1);
199 term_printf("OK domain %d, bus %d, slot %d, function %d\n",
200 0, pci_bus_num(dev->bus), PCI_SLOT(dev->devfn),
201 PCI_FUNC(dev->devfn));
202 } else
203 term_printf("failed to add %s\n", opts);
205 #endif
207 void pci_device_hot_remove(const char *pci_addr)
209 PCIDevice *d;
210 int dom, bus;
211 unsigned slot;
213 if (pci_read_devaddr(pci_addr, &dom, &bus, &slot)) {
214 term_printf("Invalid pci address\n");
215 return;
218 d = pci_find_device(bus, slot, 0);
219 if (!d) {
220 term_printf("slot %d empty\n", slot);
221 return;
224 qemu_system_device_hot_add(bus, slot, 0);
227 static int pci_match_fn(void *dev_private, void *arg)
229 PCIDevice *dev = dev_private;
230 PCIDevice *match = arg;
232 return (dev == match);
236 * OS has executed _EJ0 method, we now can remove the device
238 void pci_device_hot_remove_success(int pcibus, int slot)
240 PCIDevice *d = pci_find_device(pcibus, slot, 0);
241 int class_code;
242 AssignedDevInfo *adev;
244 if (!d) {
245 term_printf("invalid slot %d\n", slot);
246 return;
249 #ifdef USE_KVM_DEVICE_ASSIGNMENT
250 adev = get_assigned_device(pcibus, slot);
251 if (adev) {
252 qemu_pci_hot_deassign_device(adev);
253 return;
255 #endif /* USE_KVM_DEVICE_ASSIGNMENT */
257 class_code = d->config_read(d, PCI_CLASS_DEVICE+1, 1);
259 switch(class_code) {
260 case PCI_BASE_CLASS_STORAGE:
261 destroy_bdrvs(pci_match_fn, d);
262 break;
263 case PCI_BASE_CLASS_NETWORK:
264 destroy_nic(pci_match_fn, d);
265 break;
268 pci_unregister_device(d);