]>
Commit | Line | Data |
---|---|---|
6f338c34 AL |
1 | /* |
2 | * QEMU PCI hotplug support | |
3 | * | |
4 | * Copyright (c) 2004 Fabrice Bellard | |
5 | * | |
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: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
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. | |
23 | */ | |
24 | ||
25 | #include "hw.h" | |
26 | #include "boards.h" | |
27 | #include "pci.h" | |
28 | #include "net.h" | |
6f338c34 | 29 | #include "pc.h" |
376253ec | 30 | #include "monitor.h" |
43b443b6 | 31 | #include "scsi.h" |
6f338c34 | 32 | #include "virtio-blk.h" |
c59c7ea9 | 33 | #include "qemu-config.h" |
2446333c | 34 | #include "blockdev.h" |
56f9107e | 35 | #include "error.h" |
6f338c34 | 36 | |
ce88f890 | 37 | #if defined(TARGET_I386) |
1f5f6638 | 38 | static PCIDevice *qemu_pci_hot_add_nic(Monitor *mon, |
c59c7ea9 MM |
39 | const char *devaddr, |
40 | const char *opts_str) | |
6f338c34 | 41 | { |
4559a1db | 42 | Error *local_err = NULL; |
c59c7ea9 | 43 | QemuOpts *opts; |
53e0d8af GH |
44 | PCIBus *bus; |
45 | int ret, devfn; | |
46 | ||
47 | bus = pci_get_bus_devfn(&devfn, devaddr); | |
48 | if (!bus) { | |
49 | monitor_printf(mon, "Invalid PCI device address %s\n", devaddr); | |
50 | return NULL; | |
51 | } | |
52 | if (!((BusState*)bus)->allow_hotplug) { | |
53 | monitor_printf(mon, "PCI bus doesn't support hotplug\n"); | |
54 | return NULL; | |
55 | } | |
6f338c34 | 56 | |
3329f07b | 57 | opts = qemu_opts_parse(qemu_find_opts("net"), opts_str ? opts_str : "", 0); |
c59c7ea9 | 58 | if (!opts) { |
c59c7ea9 MM |
59 | return NULL; |
60 | } | |
61 | ||
62 | qemu_opt_set(opts, "type", "nic"); | |
63 | ||
4559a1db LC |
64 | ret = net_client_init(opts, 0, &local_err); |
65 | if (error_is_set(&local_err)) { | |
66 | qerror_report_err(local_err); | |
67 | error_free(local_err); | |
6f338c34 | 68 | return NULL; |
4559a1db | 69 | } |
5607c388 MA |
70 | if (nd_table[ret].devaddr) { |
71 | monitor_printf(mon, "Parameter addr not supported\n"); | |
72 | return NULL; | |
73 | } | |
1f5f6638 | 74 | return pci_nic_init(&nd_table[ret], "rtl8139", devaddr); |
6f338c34 AL |
75 | } |
76 | ||
6fdb03d5 MA |
77 | static int scsi_hot_add(Monitor *mon, DeviceState *adapter, |
78 | DriveInfo *dinfo, int printinfo) | |
30d335d6 GH |
79 | { |
80 | SCSIBus *scsibus; | |
81 | SCSIDevice *scsidev; | |
82 | ||
0d936928 | 83 | scsibus = SCSI_BUS(QLIST_FIRST(&adapter->child_bus)); |
30d335d6 GH |
84 | |
85 | /* | |
86 | * drive_init() tries to find a default for dinfo->unit. Doesn't | |
87 | * work at all for hotplug though as we assign the device to a | |
88 | * specific bus instead of the first bus with spare scsi ids. | |
89 | * | |
90 | * Ditch the calculated value and reload from option string (if | |
91 | * specified). | |
92 | */ | |
93 | dinfo->unit = qemu_opt_get_number(dinfo->opts, "unit", -1); | |
31e1ea3e | 94 | dinfo->bus = scsibus->busnr; |
ce4e7e46 PB |
95 | scsidev = scsi_bus_legacy_add_drive(scsibus, dinfo->bdrv, dinfo->unit, |
96 | false, -1); | |
fa66b909 MA |
97 | if (!scsidev) { |
98 | return -1; | |
99 | } | |
11f4d7f4 | 100 | dinfo->unit = scsidev->id; |
30d335d6 GH |
101 | |
102 | if (printinfo) | |
6fdb03d5 MA |
103 | monitor_printf(mon, "OK bus %d, unit %d\n", |
104 | scsibus->busnr, scsidev->id); | |
30d335d6 GH |
105 | return 0; |
106 | } | |
107 | ||
dd97aa8a AG |
108 | int pci_drive_hot_add(Monitor *mon, const QDict *qdict, |
109 | DriveInfo *dinfo, int type) | |
6f338c34 AL |
110 | { |
111 | int dom, pci_bus; | |
112 | unsigned slot; | |
6f338c34 | 113 | PCIDevice *dev; |
f18c16de | 114 | const char *pci_addr = qdict_get_str(qdict, "pci_addr"); |
6f338c34 AL |
115 | |
116 | switch (type) { | |
117 | case IF_SCSI: | |
4db49dc0 GH |
118 | if (pci_read_devaddr(mon, pci_addr, &dom, &pci_bus, &slot)) { |
119 | goto err; | |
120 | } | |
5256d8bf IY |
121 | dev = pci_find_device(pci_find_root_bus(dom), pci_bus, |
122 | PCI_DEVFN(slot, 0)); | |
4db49dc0 GH |
123 | if (!dev) { |
124 | monitor_printf(mon, "no pci device with address %s\n", pci_addr); | |
125 | goto err; | |
126 | } | |
6fdb03d5 | 127 | if (scsi_hot_add(mon, &dev->qdev, dinfo, 1) != 0) { |
30d335d6 GH |
128 | goto err; |
129 | } | |
6f338c34 AL |
130 | break; |
131 | default: | |
376253ec | 132 | monitor_printf(mon, "Can't hot-add drive to type %d\n", type); |
4db49dc0 | 133 | goto err; |
6f338c34 AL |
134 | } |
135 | ||
dd97aa8a | 136 | return 0; |
4db49dc0 | 137 | err: |
dd97aa8a | 138 | return -1; |
6f338c34 AL |
139 | } |
140 | ||
1f5f6638 MA |
141 | static PCIDevice *qemu_pci_hot_add_storage(Monitor *mon, |
142 | const char *devaddr, | |
376253ec | 143 | const char *opts) |
6f338c34 | 144 | { |
1f5f6638 | 145 | PCIDevice *dev; |
06c79f4e | 146 | DriveInfo *dinfo = NULL; |
751c6a17 | 147 | int type = -1; |
6f338c34 | 148 | char buf[128]; |
49bd1458 MA |
149 | PCIBus *bus; |
150 | int devfn; | |
6f338c34 AL |
151 | |
152 | if (get_param_value(buf, sizeof(buf), "if", opts)) { | |
153 | if (!strcmp(buf, "scsi")) | |
154 | type = IF_SCSI; | |
155 | else if (!strcmp(buf, "virtio")) { | |
156 | type = IF_VIRTIO; | |
8707ecca AL |
157 | } else { |
158 | monitor_printf(mon, "type %s not a hotpluggable PCI device.\n", buf); | |
1f5f6638 | 159 | return NULL; |
6f338c34 AL |
160 | } |
161 | } else { | |
376253ec | 162 | monitor_printf(mon, "no if= specified\n"); |
1f5f6638 | 163 | return NULL; |
6f338c34 AL |
164 | } |
165 | ||
166 | if (get_param_value(buf, sizeof(buf), "file", opts)) { | |
751c6a17 GH |
167 | dinfo = add_init_drive(opts); |
168 | if (!dinfo) | |
1f5f6638 | 169 | return NULL; |
751c6a17 | 170 | if (dinfo->devaddr) { |
c2cc47a4 MA |
171 | monitor_printf(mon, "Parameter addr not supported\n"); |
172 | return NULL; | |
173 | } | |
7432ff5d BS |
174 | } else { |
175 | dinfo = NULL; | |
6f338c34 AL |
176 | } |
177 | ||
49bd1458 MA |
178 | bus = pci_get_bus_devfn(&devfn, devaddr); |
179 | if (!bus) { | |
180 | monitor_printf(mon, "Invalid PCI device address %s\n", devaddr); | |
181 | return NULL; | |
182 | } | |
53e0d8af GH |
183 | if (!((BusState*)bus)->allow_hotplug) { |
184 | monitor_printf(mon, "PCI bus doesn't support hotplug\n"); | |
185 | return NULL; | |
186 | } | |
49bd1458 | 187 | |
6f338c34 AL |
188 | switch (type) { |
189 | case IF_SCSI: | |
499cf102 | 190 | dev = pci_create(bus, devfn, "lsi53c895a"); |
5b684b5a GH |
191 | if (qdev_init(&dev->qdev) < 0) |
192 | dev = NULL; | |
ec7efac4 | 193 | if (dev && dinfo) { |
6fdb03d5 | 194 | if (scsi_hot_add(mon, &dev->qdev, dinfo, 0) != 0) { |
56f9107e | 195 | qdev_unplug(&dev->qdev, NULL); |
30d335d6 GH |
196 | dev = NULL; |
197 | } | |
5b684b5a | 198 | } |
6f338c34 AL |
199 | break; |
200 | case IF_VIRTIO: | |
7432ff5d BS |
201 | if (!dinfo) { |
202 | monitor_printf(mon, "virtio requires a backing file/device.\n"); | |
203 | return NULL; | |
204 | } | |
499cf102 | 205 | dev = pci_create(bus, devfn, "virtio-blk-pci"); |
18846dee MA |
206 | if (qdev_prop_set_drive(&dev->qdev, "drive", dinfo->bdrv) < 0) { |
207 | qdev_free(&dev->qdev); | |
208 | dev = NULL; | |
209 | break; | |
210 | } | |
5b684b5a GH |
211 | if (qdev_init(&dev->qdev) < 0) |
212 | dev = NULL; | |
6f338c34 | 213 | break; |
1f5f6638 MA |
214 | default: |
215 | dev = NULL; | |
6f338c34 | 216 | } |
1f5f6638 | 217 | return dev; |
6f338c34 AL |
218 | } |
219 | ||
6c6a58ae | 220 | void pci_device_hot_add(Monitor *mon, const QDict *qdict) |
6f338c34 AL |
221 | { |
222 | PCIDevice *dev = NULL; | |
1d4daa91 LC |
223 | const char *pci_addr = qdict_get_str(qdict, "pci_addr"); |
224 | const char *type = qdict_get_str(qdict, "type"); | |
225 | const char *opts = qdict_get_try_str(qdict, "opts"); | |
6f338c34 | 226 | |
e9283f8b JK |
227 | /* strip legacy tag */ |
228 | if (!strncmp(pci_addr, "pci_addr=", 9)) { | |
229 | pci_addr += 9; | |
6f338c34 AL |
230 | } |
231 | ||
a62acdc0 JK |
232 | if (!opts) { |
233 | opts = ""; | |
234 | } | |
235 | ||
e9283f8b JK |
236 | if (!strcmp(pci_addr, "auto")) |
237 | pci_addr = NULL; | |
6f338c34 | 238 | |
395560c8 | 239 | if (strcmp(type, "nic") == 0) { |
e9283f8b | 240 | dev = qemu_pci_hot_add_nic(mon, pci_addr, opts); |
395560c8 | 241 | } else if (strcmp(type, "storage") == 0) { |
e9283f8b | 242 | dev = qemu_pci_hot_add_storage(mon, pci_addr, opts); |
395560c8 | 243 | } else { |
376253ec | 244 | monitor_printf(mon, "invalid type: %s\n", type); |
395560c8 | 245 | } |
6f338c34 AL |
246 | |
247 | if (dev) { | |
6c6a58ae | 248 | monitor_printf(mon, "OK domain %d, bus %d, slot %d, function %d\n", |
e075e788 IY |
249 | pci_find_domain(dev->bus), |
250 | pci_bus_num(dev->bus), PCI_SLOT(dev->devfn), | |
6c6a58ae MA |
251 | PCI_FUNC(dev->devfn)); |
252 | } else | |
376253ec | 253 | monitor_printf(mon, "failed to add %s\n", opts); |
6f338c34 AL |
254 | } |
255 | #endif | |
256 | ||
f2b07c92 | 257 | static int pci_device_hot_remove(Monitor *mon, const char *pci_addr) |
6f338c34 AL |
258 | { |
259 | PCIDevice *d; | |
260 | int dom, bus; | |
261 | unsigned slot; | |
56f9107e | 262 | Error *local_err = NULL; |
6f338c34 | 263 | |
e9283f8b | 264 | if (pci_read_devaddr(mon, pci_addr, &dom, &bus, &slot)) { |
053801bc | 265 | return -1; |
6f338c34 AL |
266 | } |
267 | ||
5256d8bf | 268 | d = pci_find_device(pci_find_root_bus(dom), bus, PCI_DEVFN(slot, 0)); |
6f338c34 | 269 | if (!d) { |
376253ec | 270 | monitor_printf(mon, "slot %d empty\n", slot); |
053801bc | 271 | return -1; |
6f338c34 | 272 | } |
56f9107e LC |
273 | |
274 | qdev_unplug(&d->qdev, &local_err); | |
275 | if (error_is_set(&local_err)) { | |
276 | monitor_printf(mon, "%s\n", error_get_pretty(local_err)); | |
277 | error_free(local_err); | |
278 | return -1; | |
279 | } | |
280 | ||
281 | return 0; | |
6f338c34 AL |
282 | } |
283 | ||
b752daf0 | 284 | void do_pci_device_hot_remove(Monitor *mon, const QDict *qdict) |
38183186 | 285 | { |
b752daf0 | 286 | pci_device_hot_remove(mon, qdict_get_str(qdict, "pci_addr")); |
38183186 | 287 | } |