]>
Commit | Line | Data |
---|---|---|
6f338c34 | 1 | /* |
79ca616f DG |
2 | * Deprecated PCI hotplug interface support |
3 | * This covers the old pci_add / pci_del command, whereas the more general | |
4 | * device_add / device_del commands are now preferred. | |
6f338c34 AL |
5 | * |
6 | * Copyright (c) 2004 Fabrice Bellard | |
7 | * | |
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
9 | * of this software and associated documentation files (the "Software"), to deal | |
10 | * in the Software without restriction, including without limitation the rights | |
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
12 | * copies of the Software, and to permit persons to whom the Software is | |
13 | * furnished to do so, subject to the following conditions: | |
14 | * | |
15 | * The above copyright notice and this permission notice shall be included in | |
16 | * all copies or substantial portions of the Software. | |
17 | * | |
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
21 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
24 | * THE SOFTWARE. | |
25 | */ | |
26 | ||
c759b24f MT |
27 | #include "hw/hw.h" |
28 | #include "hw/boards.h" | |
29 | #include "hw/pci/pci.h" | |
1422e32d | 30 | #include "net/net.h" |
0d09e41a | 31 | #include "hw/i386/pc.h" |
83c9089e | 32 | #include "monitor/monitor.h" |
0d09e41a PB |
33 | #include "hw/scsi/scsi.h" |
34 | #include "hw/virtio/virtio-blk.h" | |
1de7afc9 | 35 | #include "qemu/config-file.h" |
fa1d36df | 36 | #include "sysemu/block-backend.h" |
7b1b5d19 | 37 | #include "qapi/error.h" |
6f338c34 | 38 | |
1ef7a2a2 | 39 | static int pci_read_devaddr(Monitor *mon, const char *addr, |
6ac363b5 DG |
40 | int *busp, unsigned *slotp) |
41 | { | |
1ef7a2a2 DG |
42 | int dom; |
43 | ||
6ac363b5 DG |
44 | /* strip legacy tag */ |
45 | if (!strncmp(addr, "pci_addr=", 9)) { | |
46 | addr += 9; | |
47 | } | |
1ef7a2a2 | 48 | if (pci_parse_devaddr(addr, &dom, busp, slotp, NULL)) { |
6ac363b5 DG |
49 | monitor_printf(mon, "Invalid pci address\n"); |
50 | return -1; | |
51 | } | |
1ef7a2a2 DG |
52 | if (dom != 0) { |
53 | monitor_printf(mon, "Multiple PCI domains not supported, use device_add\n"); | |
54 | return -1; | |
55 | } | |
6ac363b5 DG |
56 | return 0; |
57 | } | |
58 | ||
1f5f6638 | 59 | static PCIDevice *qemu_pci_hot_add_nic(Monitor *mon, |
c59c7ea9 MM |
60 | const char *devaddr, |
61 | const char *opts_str) | |
6f338c34 | 62 | { |
4559a1db | 63 | Error *local_err = NULL; |
c59c7ea9 | 64 | QemuOpts *opts; |
9bc47305 | 65 | PCIBus *root = pci_find_primary_bus(); |
53e0d8af GH |
66 | PCIBus *bus; |
67 | int ret, devfn; | |
68 | ||
9bc47305 DG |
69 | if (!root) { |
70 | monitor_printf(mon, "no primary PCI bus (if there are multiple" | |
71 | " PCI roots, you must use device_add instead)"); | |
72 | return NULL; | |
73 | } | |
74 | ||
75 | bus = pci_get_bus_devfn(&devfn, root, devaddr); | |
53e0d8af GH |
76 | if (!bus) { |
77 | monitor_printf(mon, "Invalid PCI device address %s\n", devaddr); | |
78 | return NULL; | |
79 | } | |
39b888bd | 80 | if (!qbus_is_hotpluggable(BUS(bus))) { |
53e0d8af GH |
81 | monitor_printf(mon, "PCI bus doesn't support hotplug\n"); |
82 | return NULL; | |
83 | } | |
6f338c34 | 84 | |
3329f07b | 85 | opts = qemu_opts_parse(qemu_find_opts("net"), opts_str ? opts_str : "", 0); |
c59c7ea9 | 86 | if (!opts) { |
c59c7ea9 MM |
87 | return NULL; |
88 | } | |
89 | ||
f43e47db | 90 | qemu_opt_set(opts, "type", "nic", &error_abort); |
c59c7ea9 | 91 | |
4559a1db | 92 | ret = net_client_init(opts, 0, &local_err); |
84d18f06 | 93 | if (local_err) { |
4559a1db LC |
94 | qerror_report_err(local_err); |
95 | error_free(local_err); | |
6f338c34 | 96 | return NULL; |
4559a1db | 97 | } |
5607c388 MA |
98 | if (nd_table[ret].devaddr) { |
99 | monitor_printf(mon, "Parameter addr not supported\n"); | |
100 | return NULL; | |
101 | } | |
9bc47305 | 102 | return pci_nic_init(&nd_table[ret], root, "rtl8139", devaddr); |
6f338c34 AL |
103 | } |
104 | ||
6fdb03d5 MA |
105 | static int scsi_hot_add(Monitor *mon, DeviceState *adapter, |
106 | DriveInfo *dinfo, int printinfo) | |
30d335d6 GH |
107 | { |
108 | SCSIBus *scsibus; | |
109 | SCSIDevice *scsidev; | |
22b80e85 | 110 | Error *local_err = NULL; |
30d335d6 | 111 | |
b5007bcc PB |
112 | scsibus = (SCSIBus *) |
113 | object_dynamic_cast(OBJECT(QLIST_FIRST(&adapter->child_bus)), | |
114 | TYPE_SCSI_BUS); | |
115 | if (!scsibus) { | |
116 | error_report("Device is not a SCSI adapter"); | |
117 | return -1; | |
118 | } | |
30d335d6 GH |
119 | |
120 | /* | |
121 | * drive_init() tries to find a default for dinfo->unit. Doesn't | |
122 | * work at all for hotplug though as we assign the device to a | |
123 | * specific bus instead of the first bus with spare scsi ids. | |
124 | * | |
125 | * Ditch the calculated value and reload from option string (if | |
126 | * specified). | |
127 | */ | |
128 | dinfo->unit = qemu_opt_get_number(dinfo->opts, "unit", -1); | |
31e1ea3e | 129 | dinfo->bus = scsibus->busnr; |
fa1d36df | 130 | scsidev = scsi_bus_legacy_add_drive(scsibus, |
4be74634 | 131 | blk_by_legacy_dinfo(dinfo), |
fa1d36df MA |
132 | dinfo->unit, false, -1, NULL, |
133 | &local_err); | |
fa66b909 | 134 | if (!scsidev) { |
565f65d2 | 135 | error_report_err(local_err); |
fa66b909 MA |
136 | return -1; |
137 | } | |
11f4d7f4 | 138 | dinfo->unit = scsidev->id; |
30d335d6 GH |
139 | |
140 | if (printinfo) | |
6fdb03d5 MA |
141 | monitor_printf(mon, "OK bus %d, unit %d\n", |
142 | scsibus->busnr, scsidev->id); | |
30d335d6 GH |
143 | return 0; |
144 | } | |
145 | ||
4dbd84e2 | 146 | int pci_drive_hot_add(Monitor *mon, const QDict *qdict, DriveInfo *dinfo) |
6f338c34 | 147 | { |
1ef7a2a2 | 148 | int pci_bus; |
6f338c34 | 149 | unsigned slot; |
1ef7a2a2 | 150 | PCIBus *root = pci_find_primary_bus(); |
6f338c34 | 151 | PCIDevice *dev; |
f18c16de | 152 | const char *pci_addr = qdict_get_str(qdict, "pci_addr"); |
6f338c34 | 153 | |
4dbd84e2 | 154 | switch (dinfo->type) { |
6f338c34 | 155 | case IF_SCSI: |
1ef7a2a2 | 156 | if (!root) { |
9bc47305 DG |
157 | monitor_printf(mon, "no primary PCI bus (if there are multiple" |
158 | " PCI roots, you must use device_add instead)"); | |
1ef7a2a2 DG |
159 | goto err; |
160 | } | |
161 | if (pci_read_devaddr(mon, pci_addr, &pci_bus, &slot)) { | |
4db49dc0 GH |
162 | goto err; |
163 | } | |
1ef7a2a2 | 164 | dev = pci_find_device(root, pci_bus, PCI_DEVFN(slot, 0)); |
4db49dc0 GH |
165 | if (!dev) { |
166 | monitor_printf(mon, "no pci device with address %s\n", pci_addr); | |
167 | goto err; | |
168 | } | |
6fdb03d5 | 169 | if (scsi_hot_add(mon, &dev->qdev, dinfo, 1) != 0) { |
30d335d6 GH |
170 | goto err; |
171 | } | |
6f338c34 AL |
172 | break; |
173 | default: | |
4dbd84e2 | 174 | monitor_printf(mon, "Can't hot-add drive to type %d\n", dinfo->type); |
4db49dc0 | 175 | goto err; |
6f338c34 AL |
176 | } |
177 | ||
dd97aa8a | 178 | return 0; |
4db49dc0 | 179 | err: |
dd97aa8a | 180 | return -1; |
6f338c34 AL |
181 | } |
182 | ||
1f5f6638 MA |
183 | static PCIDevice *qemu_pci_hot_add_storage(Monitor *mon, |
184 | const char *devaddr, | |
376253ec | 185 | const char *opts) |
6f338c34 | 186 | { |
1f5f6638 | 187 | PCIDevice *dev; |
06c79f4e | 188 | DriveInfo *dinfo = NULL; |
751c6a17 | 189 | int type = -1; |
6f338c34 | 190 | char buf[128]; |
9bc47305 | 191 | PCIBus *root = pci_find_primary_bus(); |
49bd1458 MA |
192 | PCIBus *bus; |
193 | int devfn; | |
6f338c34 AL |
194 | |
195 | if (get_param_value(buf, sizeof(buf), "if", opts)) { | |
196 | if (!strcmp(buf, "scsi")) | |
197 | type = IF_SCSI; | |
198 | else if (!strcmp(buf, "virtio")) { | |
199 | type = IF_VIRTIO; | |
8707ecca AL |
200 | } else { |
201 | monitor_printf(mon, "type %s not a hotpluggable PCI device.\n", buf); | |
1f5f6638 | 202 | return NULL; |
6f338c34 AL |
203 | } |
204 | } else { | |
376253ec | 205 | monitor_printf(mon, "no if= specified\n"); |
1f5f6638 | 206 | return NULL; |
6f338c34 AL |
207 | } |
208 | ||
209 | if (get_param_value(buf, sizeof(buf), "file", opts)) { | |
751c6a17 GH |
210 | dinfo = add_init_drive(opts); |
211 | if (!dinfo) | |
1f5f6638 | 212 | return NULL; |
751c6a17 | 213 | if (dinfo->devaddr) { |
c2cc47a4 MA |
214 | monitor_printf(mon, "Parameter addr not supported\n"); |
215 | return NULL; | |
216 | } | |
7432ff5d BS |
217 | } else { |
218 | dinfo = NULL; | |
6f338c34 AL |
219 | } |
220 | ||
9bc47305 DG |
221 | if (!root) { |
222 | monitor_printf(mon, "no primary PCI bus (if there are multiple" | |
223 | " PCI roots, you must use device_add instead)"); | |
224 | return NULL; | |
225 | } | |
226 | bus = pci_get_bus_devfn(&devfn, root, devaddr); | |
49bd1458 MA |
227 | if (!bus) { |
228 | monitor_printf(mon, "Invalid PCI device address %s\n", devaddr); | |
229 | return NULL; | |
230 | } | |
39b888bd | 231 | if (!qbus_is_hotpluggable(BUS(bus))) { |
53e0d8af GH |
232 | monitor_printf(mon, "PCI bus doesn't support hotplug\n"); |
233 | return NULL; | |
234 | } | |
49bd1458 | 235 | |
6f338c34 AL |
236 | switch (type) { |
237 | case IF_SCSI: | |
499cf102 | 238 | dev = pci_create(bus, devfn, "lsi53c895a"); |
5b684b5a GH |
239 | if (qdev_init(&dev->qdev) < 0) |
240 | dev = NULL; | |
ec7efac4 | 241 | if (dev && dinfo) { |
6fdb03d5 | 242 | if (scsi_hot_add(mon, &dev->qdev, dinfo, 0) != 0) { |
56f9107e | 243 | qdev_unplug(&dev->qdev, NULL); |
30d335d6 GH |
244 | dev = NULL; |
245 | } | |
5b684b5a | 246 | } |
6f338c34 AL |
247 | break; |
248 | case IF_VIRTIO: | |
7432ff5d BS |
249 | if (!dinfo) { |
250 | monitor_printf(mon, "virtio requires a backing file/device.\n"); | |
251 | return NULL; | |
252 | } | |
499cf102 | 253 | dev = pci_create(bus, devfn, "virtio-blk-pci"); |
fa1d36df | 254 | if (qdev_prop_set_drive(&dev->qdev, "drive", |
4be74634 | 255 | blk_by_legacy_dinfo(dinfo)) < 0) { |
02a5c4c9 | 256 | object_unparent(OBJECT(dev)); |
18846dee MA |
257 | dev = NULL; |
258 | break; | |
259 | } | |
5b684b5a GH |
260 | if (qdev_init(&dev->qdev) < 0) |
261 | dev = NULL; | |
6f338c34 | 262 | break; |
1f5f6638 MA |
263 | default: |
264 | dev = NULL; | |
6f338c34 | 265 | } |
1f5f6638 | 266 | return dev; |
6f338c34 AL |
267 | } |
268 | ||
3e5a50d6 | 269 | void hmp_pci_add(Monitor *mon, const QDict *qdict) |
6f338c34 AL |
270 | { |
271 | PCIDevice *dev = NULL; | |
1d4daa91 LC |
272 | const char *pci_addr = qdict_get_str(qdict, "pci_addr"); |
273 | const char *type = qdict_get_str(qdict, "type"); | |
274 | const char *opts = qdict_get_try_str(qdict, "opts"); | |
6f338c34 | 275 | |
e9283f8b JK |
276 | /* strip legacy tag */ |
277 | if (!strncmp(pci_addr, "pci_addr=", 9)) { | |
278 | pci_addr += 9; | |
6f338c34 AL |
279 | } |
280 | ||
a62acdc0 JK |
281 | if (!opts) { |
282 | opts = ""; | |
283 | } | |
284 | ||
e9283f8b JK |
285 | if (!strcmp(pci_addr, "auto")) |
286 | pci_addr = NULL; | |
6f338c34 | 287 | |
395560c8 | 288 | if (strcmp(type, "nic") == 0) { |
e9283f8b | 289 | dev = qemu_pci_hot_add_nic(mon, pci_addr, opts); |
395560c8 | 290 | } else if (strcmp(type, "storage") == 0) { |
e9283f8b | 291 | dev = qemu_pci_hot_add_storage(mon, pci_addr, opts); |
395560c8 | 292 | } else { |
376253ec | 293 | monitor_printf(mon, "invalid type: %s\n", type); |
395560c8 | 294 | } |
6f338c34 AL |
295 | |
296 | if (dev) { | |
568f0690 DG |
297 | monitor_printf(mon, "OK root bus %s, bus %d, slot %d, function %d\n", |
298 | pci_root_bus_path(dev), | |
e075e788 | 299 | pci_bus_num(dev->bus), PCI_SLOT(dev->devfn), |
6c6a58ae MA |
300 | PCI_FUNC(dev->devfn)); |
301 | } else | |
376253ec | 302 | monitor_printf(mon, "failed to add %s\n", opts); |
6f338c34 | 303 | } |
6f338c34 | 304 | |
f2b07c92 | 305 | static int pci_device_hot_remove(Monitor *mon, const char *pci_addr) |
6f338c34 | 306 | { |
1ef7a2a2 | 307 | PCIBus *root = pci_find_primary_bus(); |
6f338c34 | 308 | PCIDevice *d; |
1ef7a2a2 | 309 | int bus; |
6f338c34 | 310 | unsigned slot; |
56f9107e | 311 | Error *local_err = NULL; |
6f338c34 | 312 | |
1ef7a2a2 | 313 | if (!root) { |
9bc47305 DG |
314 | monitor_printf(mon, "no primary PCI bus (if there are multiple" |
315 | " PCI roots, you must use device_del instead)"); | |
1ef7a2a2 DG |
316 | return -1; |
317 | } | |
318 | ||
319 | if (pci_read_devaddr(mon, pci_addr, &bus, &slot)) { | |
053801bc | 320 | return -1; |
6f338c34 AL |
321 | } |
322 | ||
1ef7a2a2 | 323 | d = pci_find_device(root, bus, PCI_DEVFN(slot, 0)); |
6f338c34 | 324 | if (!d) { |
376253ec | 325 | monitor_printf(mon, "slot %d empty\n", slot); |
053801bc | 326 | return -1; |
6f338c34 | 327 | } |
56f9107e LC |
328 | |
329 | qdev_unplug(&d->qdev, &local_err); | |
84d18f06 | 330 | if (local_err) { |
56f9107e LC |
331 | monitor_printf(mon, "%s\n", error_get_pretty(local_err)); |
332 | error_free(local_err); | |
333 | return -1; | |
334 | } | |
335 | ||
336 | return 0; | |
6f338c34 AL |
337 | } |
338 | ||
3e5a50d6 | 339 | void hmp_pci_del(Monitor *mon, const QDict *qdict) |
38183186 | 340 | { |
b752daf0 | 341 | pci_device_hot_remove(mon, qdict_get_str(qdict, "pci_addr")); |
38183186 | 342 | } |