root / hw / pci-hotplug.c @ a8b7063b
History | View | Annotate | Download (6.1 kB)
1 | 6f338c34 | aliguori | /*
|
---|---|---|---|
2 | 6f338c34 | aliguori | * QEMU PCI hotplug support
|
3 | 6f338c34 | aliguori | *
|
4 | 6f338c34 | aliguori | * Copyright (c) 2004 Fabrice Bellard
|
5 | 6f338c34 | aliguori | *
|
6 | 6f338c34 | aliguori | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 6f338c34 | aliguori | * of this software and associated documentation files (the "Software"), to deal
|
8 | 6f338c34 | aliguori | * in the Software without restriction, including without limitation the rights
|
9 | 6f338c34 | aliguori | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 6f338c34 | aliguori | * copies of the Software, and to permit persons to whom the Software is
|
11 | 6f338c34 | aliguori | * furnished to do so, subject to the following conditions:
|
12 | 6f338c34 | aliguori | *
|
13 | 6f338c34 | aliguori | * The above copyright notice and this permission notice shall be included in
|
14 | 6f338c34 | aliguori | * all copies or substantial portions of the Software.
|
15 | 6f338c34 | aliguori | *
|
16 | 6f338c34 | aliguori | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 6f338c34 | aliguori | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 6f338c34 | aliguori | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 6f338c34 | aliguori | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 6f338c34 | aliguori | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 6f338c34 | aliguori | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 6f338c34 | aliguori | * THE SOFTWARE.
|
23 | 6f338c34 | aliguori | */
|
24 | 6f338c34 | aliguori | |
25 | 6f338c34 | aliguori | #include "hw.h" |
26 | 6f338c34 | aliguori | #include "boards.h" |
27 | 6f338c34 | aliguori | #include "pci.h" |
28 | 6f338c34 | aliguori | #include "net.h" |
29 | 6f338c34 | aliguori | #include "sysemu.h" |
30 | 6f338c34 | aliguori | #include "pc.h" |
31 | 376253ec | aliguori | #include "monitor.h" |
32 | 6f338c34 | aliguori | #include "block_int.h" |
33 | 6f338c34 | aliguori | #include "virtio-blk.h" |
34 | 6f338c34 | aliguori | |
35 | 6f338c34 | aliguori | #if defined(TARGET_I386) || defined(TARGET_X86_64)
|
36 | 10ae5a7a | Jan Kiszka | static PCIDevice *qemu_pci_hot_add_nic(Monitor *mon, PCIBus *pci_bus,
|
37 | 10ae5a7a | Jan Kiszka | const char *opts) |
38 | 6f338c34 | aliguori | { |
39 | 6f338c34 | aliguori | int ret;
|
40 | 6f338c34 | aliguori | |
41 | 10ae5a7a | Jan Kiszka | ret = net_client_init(mon, "nic", opts);
|
42 | eefb4091 | aliguori | if (ret < 0) |
43 | 6f338c34 | aliguori | return NULL; |
44 | eefb4091 | aliguori | return pci_nic_init(pci_bus, &nd_table[ret], -1, "rtl8139"); |
45 | 6f338c34 | aliguori | } |
46 | 6f338c34 | aliguori | |
47 | 376253ec | aliguori | void drive_hot_add(Monitor *mon, const char *pci_addr, const char *opts) |
48 | 6f338c34 | aliguori | { |
49 | 6f338c34 | aliguori | int dom, pci_bus;
|
50 | 6f338c34 | aliguori | unsigned slot;
|
51 | 6f338c34 | aliguori | int drive_idx, type, bus;
|
52 | 6f338c34 | aliguori | int success = 0; |
53 | 6f338c34 | aliguori | PCIDevice *dev; |
54 | 6f338c34 | aliguori | |
55 | 6f338c34 | aliguori | if (pci_read_devaddr(pci_addr, &dom, &pci_bus, &slot)) {
|
56 | 376253ec | aliguori | monitor_printf(mon, "Invalid pci address\n");
|
57 | 6f338c34 | aliguori | return;
|
58 | 6f338c34 | aliguori | } |
59 | 6f338c34 | aliguori | |
60 | 6f338c34 | aliguori | dev = pci_find_device(pci_bus, slot, 0);
|
61 | 6f338c34 | aliguori | if (!dev) {
|
62 | 376253ec | aliguori | monitor_printf(mon, "no pci device with address %s\n", pci_addr);
|
63 | 6f338c34 | aliguori | return;
|
64 | 6f338c34 | aliguori | } |
65 | 6f338c34 | aliguori | |
66 | 6f338c34 | aliguori | drive_idx = add_init_drive(opts); |
67 | 6f338c34 | aliguori | if (drive_idx < 0) |
68 | 6f338c34 | aliguori | return;
|
69 | 6f338c34 | aliguori | type = drives_table[drive_idx].type; |
70 | 6f338c34 | aliguori | bus = drive_get_max_bus (type); |
71 | 6f338c34 | aliguori | |
72 | 6f338c34 | aliguori | switch (type) {
|
73 | 6f338c34 | aliguori | case IF_SCSI:
|
74 | 6f338c34 | aliguori | success = 1;
|
75 | 9be5dafe | Paul Brook | lsi_scsi_attach(&dev->qdev, drives_table[drive_idx].bdrv, |
76 | 9be5dafe | Paul Brook | drives_table[drive_idx].unit); |
77 | 6f338c34 | aliguori | break;
|
78 | 6f338c34 | aliguori | default:
|
79 | 376253ec | aliguori | monitor_printf(mon, "Can't hot-add drive to type %d\n", type);
|
80 | 6f338c34 | aliguori | } |
81 | 6f338c34 | aliguori | |
82 | 6f338c34 | aliguori | if (success)
|
83 | 376253ec | aliguori | monitor_printf(mon, "OK bus %d, unit %d\n",
|
84 | 376253ec | aliguori | drives_table[drive_idx].bus, |
85 | 376253ec | aliguori | drives_table[drive_idx].unit); |
86 | 6f338c34 | aliguori | return;
|
87 | 6f338c34 | aliguori | } |
88 | 6f338c34 | aliguori | |
89 | 376253ec | aliguori | static PCIDevice *qemu_pci_hot_add_storage(Monitor *mon, PCIBus *pci_bus,
|
90 | 376253ec | aliguori | const char *opts) |
91 | 6f338c34 | aliguori | { |
92 | 6f338c34 | aliguori | void *opaque = NULL; |
93 | 6f338c34 | aliguori | int type = -1, drive_idx = -1; |
94 | 6f338c34 | aliguori | char buf[128]; |
95 | 6f338c34 | aliguori | |
96 | 6f338c34 | aliguori | if (get_param_value(buf, sizeof(buf), "if", opts)) { |
97 | 6f338c34 | aliguori | if (!strcmp(buf, "scsi")) |
98 | 6f338c34 | aliguori | type = IF_SCSI; |
99 | 6f338c34 | aliguori | else if (!strcmp(buf, "virtio")) { |
100 | 6f338c34 | aliguori | type = IF_VIRTIO; |
101 | 8707ecca | aliguori | } else {
|
102 | 8707ecca | aliguori | monitor_printf(mon, "type %s not a hotpluggable PCI device.\n", buf);
|
103 | 8707ecca | aliguori | goto out;
|
104 | 6f338c34 | aliguori | } |
105 | 6f338c34 | aliguori | } else {
|
106 | 376253ec | aliguori | monitor_printf(mon, "no if= specified\n");
|
107 | 8707ecca | aliguori | goto out;
|
108 | 6f338c34 | aliguori | } |
109 | 6f338c34 | aliguori | |
110 | 6f338c34 | aliguori | if (get_param_value(buf, sizeof(buf), "file", opts)) { |
111 | 6f338c34 | aliguori | drive_idx = add_init_drive(opts); |
112 | 6f338c34 | aliguori | if (drive_idx < 0) |
113 | 8707ecca | aliguori | goto out;
|
114 | 6f338c34 | aliguori | } else if (type == IF_VIRTIO) { |
115 | 376253ec | aliguori | monitor_printf(mon, "virtio requires a backing file/device.\n");
|
116 | 8707ecca | aliguori | goto out;
|
117 | 6f338c34 | aliguori | } |
118 | 6f338c34 | aliguori | |
119 | 6f338c34 | aliguori | switch (type) {
|
120 | 6f338c34 | aliguori | case IF_SCSI:
|
121 | 9be5dafe | Paul Brook | opaque = pci_create_simple(pci_bus, -1, "lsi53c895a"); |
122 | 6f338c34 | aliguori | break;
|
123 | 6f338c34 | aliguori | case IF_VIRTIO:
|
124 | 53c25cea | Paul Brook | opaque = pci_create_simple(pci_bus, -1, "virtio-blk-pci"); |
125 | 07e3af9a | Paul Brook | qdev_init(opaque); |
126 | 6f338c34 | aliguori | break;
|
127 | 6f338c34 | aliguori | } |
128 | 6f338c34 | aliguori | |
129 | 8707ecca | aliguori | out:
|
130 | 6f338c34 | aliguori | return opaque;
|
131 | 6f338c34 | aliguori | } |
132 | 6f338c34 | aliguori | |
133 | 376253ec | aliguori | void pci_device_hot_add(Monitor *mon, const char *pci_addr, const char *type, |
134 | 376253ec | aliguori | const char *opts) |
135 | 6f338c34 | aliguori | { |
136 | 6f338c34 | aliguori | PCIDevice *dev = NULL;
|
137 | 6f338c34 | aliguori | PCIBus *pci_bus; |
138 | 6f338c34 | aliguori | int dom, bus;
|
139 | 6f338c34 | aliguori | unsigned slot;
|
140 | 6f338c34 | aliguori | |
141 | 6f338c34 | aliguori | if (pci_assign_devaddr(pci_addr, &dom, &bus, &slot)) {
|
142 | 376253ec | aliguori | monitor_printf(mon, "Invalid pci address\n");
|
143 | 6f338c34 | aliguori | return;
|
144 | 6f338c34 | aliguori | } |
145 | 6f338c34 | aliguori | |
146 | 6f338c34 | aliguori | pci_bus = pci_find_bus(bus); |
147 | 6f338c34 | aliguori | if (!pci_bus) {
|
148 | 376253ec | aliguori | monitor_printf(mon, "Can't find pci_bus %d\n", bus);
|
149 | 6f338c34 | aliguori | return;
|
150 | 6f338c34 | aliguori | } |
151 | 6f338c34 | aliguori | |
152 | 6f338c34 | aliguori | if (strcmp(type, "nic") == 0) |
153 | 10ae5a7a | Jan Kiszka | dev = qemu_pci_hot_add_nic(mon, pci_bus, opts); |
154 | 6f338c34 | aliguori | else if (strcmp(type, "storage") == 0) |
155 | 376253ec | aliguori | dev = qemu_pci_hot_add_storage(mon, pci_bus, opts); |
156 | 6f338c34 | aliguori | else
|
157 | 376253ec | aliguori | monitor_printf(mon, "invalid type: %s\n", type);
|
158 | 6f338c34 | aliguori | |
159 | 6f338c34 | aliguori | if (dev) {
|
160 | 6f338c34 | aliguori | qemu_system_device_hot_add(bus, PCI_SLOT(dev->devfn), 1);
|
161 | 376253ec | aliguori | monitor_printf(mon, "OK domain %d, bus %d, slot %d, function %d\n",
|
162 | 376253ec | aliguori | 0, pci_bus_num(dev->bus), PCI_SLOT(dev->devfn),
|
163 | 376253ec | aliguori | PCI_FUNC(dev->devfn)); |
164 | 6f338c34 | aliguori | } else
|
165 | 376253ec | aliguori | monitor_printf(mon, "failed to add %s\n", opts);
|
166 | 6f338c34 | aliguori | } |
167 | 6f338c34 | aliguori | #endif
|
168 | 6f338c34 | aliguori | |
169 | 376253ec | aliguori | void pci_device_hot_remove(Monitor *mon, const char *pci_addr) |
170 | 6f338c34 | aliguori | { |
171 | 6f338c34 | aliguori | PCIDevice *d; |
172 | 6f338c34 | aliguori | int dom, bus;
|
173 | 6f338c34 | aliguori | unsigned slot;
|
174 | 6f338c34 | aliguori | |
175 | 6f338c34 | aliguori | if (pci_read_devaddr(pci_addr, &dom, &bus, &slot)) {
|
176 | 376253ec | aliguori | monitor_printf(mon, "Invalid pci address\n");
|
177 | 6f338c34 | aliguori | return;
|
178 | 6f338c34 | aliguori | } |
179 | 6f338c34 | aliguori | |
180 | 6f338c34 | aliguori | d = pci_find_device(bus, slot, 0);
|
181 | 6f338c34 | aliguori | if (!d) {
|
182 | 376253ec | aliguori | monitor_printf(mon, "slot %d empty\n", slot);
|
183 | 6f338c34 | aliguori | return;
|
184 | 6f338c34 | aliguori | } |
185 | 6f338c34 | aliguori | |
186 | 6f338c34 | aliguori | qemu_system_device_hot_add(bus, slot, 0);
|
187 | 6f338c34 | aliguori | } |
188 | 6f338c34 | aliguori | |
189 | 6f338c34 | aliguori | static int pci_match_fn(void *dev_private, void *arg) |
190 | 6f338c34 | aliguori | { |
191 | 6f338c34 | aliguori | PCIDevice *dev = dev_private; |
192 | 6f338c34 | aliguori | PCIDevice *match = arg; |
193 | 6f338c34 | aliguori | |
194 | 6f338c34 | aliguori | return (dev == match);
|
195 | 6f338c34 | aliguori | } |
196 | 6f338c34 | aliguori | |
197 | 6f338c34 | aliguori | /*
|
198 | 6f338c34 | aliguori | * OS has executed _EJ0 method, we now can remove the device
|
199 | 6f338c34 | aliguori | */
|
200 | 6f338c34 | aliguori | void pci_device_hot_remove_success(int pcibus, int slot) |
201 | 6f338c34 | aliguori | { |
202 | 6f338c34 | aliguori | PCIDevice *d = pci_find_device(pcibus, slot, 0);
|
203 | 6f338c34 | aliguori | int class_code;
|
204 | 6f338c34 | aliguori | |
205 | 6f338c34 | aliguori | if (!d) {
|
206 | 376253ec | aliguori | monitor_printf(cur_mon, "invalid slot %d\n", slot);
|
207 | 6f338c34 | aliguori | return;
|
208 | 6f338c34 | aliguori | } |
209 | 6f338c34 | aliguori | |
210 | 6f338c34 | aliguori | class_code = d->config_read(d, PCI_CLASS_DEVICE+1, 1); |
211 | 6f338c34 | aliguori | |
212 | 6f338c34 | aliguori | switch(class_code) {
|
213 | 6f338c34 | aliguori | case PCI_BASE_CLASS_STORAGE:
|
214 | 6f338c34 | aliguori | destroy_bdrvs(pci_match_fn, d); |
215 | 6f338c34 | aliguori | break;
|
216 | 6f338c34 | aliguori | case PCI_BASE_CLASS_NETWORK:
|
217 | 6f338c34 | aliguori | destroy_nic(pci_match_fn, d); |
218 | 6f338c34 | aliguori | break;
|
219 | 6f338c34 | aliguori | } |
220 | 6f338c34 | aliguori | |
221 | 6f338c34 | aliguori | pci_unregister_device(d); |
222 | 6f338c34 | aliguori | } |