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"
35 #if defined(TARGET_I386) || defined(TARGET_X86_64)
36 static PCIDevice
*qemu_pci_hot_add_nic(PCIBus
*pci_bus
, const char *opts
)
40 ret
= net_client_init("nic", opts
);
43 return pci_nic_init(pci_bus
, &nd_table
[ret
], -1, "rtl8139");
46 void drive_hot_add(Monitor
*mon
, const char *pci_addr
, const char *opts
)
50 int drive_idx
, type
, bus
;
54 if (pci_read_devaddr(pci_addr
, &dom
, &pci_bus
, &slot
)) {
55 monitor_printf(mon
, "Invalid pci address\n");
59 dev
= pci_find_device(pci_bus
, slot
, 0);
61 monitor_printf(mon
, "no pci device with address %s\n", pci_addr
);
65 drive_idx
= add_init_drive(opts
);
68 type
= drives_table
[drive_idx
].type
;
69 bus
= drive_get_max_bus (type
);
74 lsi_scsi_attach (dev
, drives_table
[drive_idx
].bdrv
,
75 drives_table
[drive_idx
].unit
);
78 monitor_printf(mon
, "Can't hot-add drive to type %d\n", type
);
82 monitor_printf(mon
, "OK bus %d, unit %d\n",
83 drives_table
[drive_idx
].bus
,
84 drives_table
[drive_idx
].unit
);
88 static PCIDevice
*qemu_pci_hot_add_storage(Monitor
*mon
, PCIBus
*pci_bus
,
92 int type
= -1, drive_idx
= -1;
95 if (get_param_value(buf
, sizeof(buf
), "if", opts
)) {
96 if (!strcmp(buf
, "scsi"))
98 else if (!strcmp(buf
, "virtio")) {
101 monitor_printf(mon
, "type %s not a hotpluggable PCI device.\n", buf
);
105 monitor_printf(mon
, "no if= specified\n");
109 if (get_param_value(buf
, sizeof(buf
), "file", opts
)) {
110 drive_idx
= add_init_drive(opts
);
113 } else if (type
== IF_VIRTIO
) {
114 monitor_printf(mon
, "virtio requires a backing file/device.\n");
120 opaque
= lsi_scsi_init (pci_bus
, -1);
121 if (opaque
&& drive_idx
>= 0)
122 lsi_scsi_attach (opaque
, drives_table
[drive_idx
].bdrv
,
123 drives_table
[drive_idx
].unit
);
126 opaque
= virtio_blk_init (pci_bus
, drives_table
[drive_idx
].bdrv
);
134 void pci_device_hot_add(Monitor
*mon
, const char *pci_addr
, const char *type
,
137 PCIDevice
*dev
= NULL
;
142 if (pci_assign_devaddr(pci_addr
, &dom
, &bus
, &slot
)) {
143 monitor_printf(mon
, "Invalid pci address\n");
147 pci_bus
= pci_find_bus(bus
);
149 monitor_printf(mon
, "Can't find pci_bus %d\n", bus
);
153 if (strcmp(type
, "nic") == 0)
154 dev
= qemu_pci_hot_add_nic(pci_bus
, opts
);
155 else if (strcmp(type
, "storage") == 0)
156 dev
= qemu_pci_hot_add_storage(mon
, pci_bus
, opts
);
158 monitor_printf(mon
, "invalid type: %s\n", type
);
161 qemu_system_device_hot_add(bus
, PCI_SLOT(dev
->devfn
), 1);
162 monitor_printf(mon
, "OK domain %d, bus %d, slot %d, function %d\n",
163 0, pci_bus_num(dev
->bus
), PCI_SLOT(dev
->devfn
),
164 PCI_FUNC(dev
->devfn
));
166 monitor_printf(mon
, "failed to add %s\n", opts
);
170 void pci_device_hot_remove(Monitor
*mon
, const char *pci_addr
)
176 if (pci_read_devaddr(pci_addr
, &dom
, &bus
, &slot
)) {
177 monitor_printf(mon
, "Invalid pci address\n");
181 d
= pci_find_device(bus
, slot
, 0);
183 monitor_printf(mon
, "slot %d empty\n", slot
);
187 qemu_system_device_hot_add(bus
, slot
, 0);
190 static int pci_match_fn(void *dev_private
, void *arg
)
192 PCIDevice
*dev
= dev_private
;
193 PCIDevice
*match
= arg
;
195 return (dev
== match
);
199 * OS has executed _EJ0 method, we now can remove the device
201 void pci_device_hot_remove_success(int pcibus
, int slot
)
203 PCIDevice
*d
= pci_find_device(pcibus
, slot
, 0);
207 monitor_printf(cur_mon
, "invalid slot %d\n", slot
);
211 class_code
= d
->config_read(d
, PCI_CLASS_DEVICE
+1, 1);
214 case PCI_BASE_CLASS_STORAGE
:
215 destroy_bdrvs(pci_match_fn
, d
);
217 case PCI_BASE_CLASS_NETWORK
:
218 destroy_nic(pci_match_fn
, d
);
222 pci_unregister_device(d
);