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
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
)
41 ret
= net_client_init ("nic", opts
);
42 if (ret
< 0 || !nd_table
[ret
].model
)
44 return pci_nic_init (pci_bus
, &nd_table
[ret
], -1, "rtl8139");
47 void drive_hot_add(const char *pci_addr
, const char *opts
)
51 int drive_idx
, type
, bus
;
55 if (pci_read_devaddr(pci_addr
, &dom
, &pci_bus
, &slot
)) {
56 term_printf("Invalid pci address\n");
60 dev
= pci_find_device(pci_bus
, slot
, 0);
62 term_printf("no pci device with address %s\n", pci_addr
);
66 drive_idx
= add_init_drive(opts
);
69 type
= drives_table
[drive_idx
].type
;
70 bus
= drive_get_max_bus (type
);
75 lsi_scsi_attach (dev
, drives_table
[drive_idx
].bdrv
,
76 drives_table
[drive_idx
].unit
);
79 term_printf("Can't hot-add drive to type %d\n", type
);
83 term_printf("OK bus %d, unit %d\n", drives_table
[drive_idx
].bus
,
84 drives_table
[drive_idx
].unit
);
88 static PCIDevice
*qemu_pci_hot_add_storage(PCIBus
*pci_bus
, const char *opts
)
91 int type
= -1, drive_idx
= -1;
94 if (get_param_value(buf
, sizeof(buf
), "if", opts
)) {
95 if (!strcmp(buf
, "scsi"))
97 else if (!strcmp(buf
, "virtio")) {
101 term_printf("no if= specified\n");
105 if (get_param_value(buf
, sizeof(buf
), "file", opts
)) {
106 drive_idx
= add_init_drive(opts
);
109 } else if (type
== IF_VIRTIO
) {
110 term_printf("virtio requires a backing file/device.\n");
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
);
122 opaque
= virtio_blk_init (pci_bus
, drives_table
[drive_idx
].bdrv
);
125 term_printf ("type %s not a hotpluggable PCI device.\n", buf
);
131 #ifdef USE_KVM_DEVICE_ASSIGNMENT
132 static PCIDevice
*qemu_pci_hot_assign_device(PCIBus
*pci_bus
, const char *opts
)
134 AssignedDevInfo
*adev
;
137 adev
= add_assigned_device(opts
);
139 term_printf ("Error adding device; check syntax\n");
143 ret
= init_assigned_device(adev
, pci_bus
);
145 term_printf("Failed to assign device\n");
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,
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
;
175 if (pci_assign_devaddr(pci_addr
, &dom
, &bus
, &slot
)) {
176 term_printf("Invalid pci address\n");
180 pci_bus
= pci_find_bus(bus
);
182 term_printf("Can't find pci_bus %d\n", bus
);
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 */
195 term_printf("invalid type: %s\n", type
);
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
));
203 term_printf("failed to add %s\n", opts
);
207 void pci_device_hot_remove(const char *pci_addr
)
213 if (pci_read_devaddr(pci_addr
, &dom
, &bus
, &slot
)) {
214 term_printf("Invalid pci address\n");
218 d
= pci_find_device(bus
, slot
, 0);
220 term_printf("slot %d empty\n", slot
);
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);
242 AssignedDevInfo
*adev
;
245 term_printf("invalid slot %d\n", slot
);
249 #ifdef USE_KVM_DEVICE_ASSIGNMENT
250 adev
= get_assigned_device(pcibus
, slot
);
252 qemu_pci_hot_deassign_device(adev
);
255 #endif /* USE_KVM_DEVICE_ASSIGNMENT */
257 class_code
= d
->config_read(d
, PCI_CLASS_DEVICE
+1, 1);
260 case PCI_BASE_CLASS_STORAGE
:
261 destroy_bdrvs(pci_match_fn
, d
);
263 case PCI_BASE_CLASS_NETWORK
:
264 destroy_nic(pci_match_fn
, d
);
268 pci_unregister_device(d
);