aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 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" |
| 29 | #include "sysemu.h" |
| 30 | #include "pc.h" |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 31 | #include "monitor.h" |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 32 | #include "block_int.h" |
Gerd Hoffmann | 43b443b | 2009-10-30 09:54:00 +0100 | [diff] [blame] | 33 | #include "scsi.h" |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 34 | #include "virtio-blk.h" |
Mark McLoughlin | c59c7ea | 2009-10-06 12:17:15 +0100 | [diff] [blame] | 35 | #include "qemu-config.h" |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 36 | |
Juan Quintela | ce88f89 | 2009-10-08 00:00:00 +0200 | [diff] [blame] | 37 | #if defined(TARGET_I386) |
Markus Armbruster | 1f5f663 | 2009-06-18 15:14:09 +0200 | [diff] [blame] | 38 | static PCIDevice *qemu_pci_hot_add_nic(Monitor *mon, |
Mark McLoughlin | c59c7ea | 2009-10-06 12:17:15 +0100 | [diff] [blame] | 39 | const char *devaddr, |
| 40 | const char *opts_str) |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 41 | { |
Mark McLoughlin | c59c7ea | 2009-10-06 12:17:15 +0100 | [diff] [blame] | 42 | QemuOpts *opts; |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 43 | int ret; |
| 44 | |
Mark McLoughlin | c59c7ea | 2009-10-06 12:17:15 +0100 | [diff] [blame] | 45 | opts = qemu_opts_parse(&qemu_net_opts, opts_str ? opts_str : "", NULL); |
| 46 | if (!opts) { |
| 47 | monitor_printf(mon, "parsing network options '%s' failed\n", |
| 48 | opts_str ? opts_str : ""); |
| 49 | return NULL; |
| 50 | } |
| 51 | |
| 52 | qemu_opt_set(opts, "type", "nic"); |
| 53 | |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 54 | ret = net_client_init(mon, opts, 0); |
aliguori | eefb409 | 2009-04-17 17:10:47 +0000 | [diff] [blame] | 55 | if (ret < 0) |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 56 | return NULL; |
Markus Armbruster | 5607c38 | 2009-06-18 15:14:08 +0200 | [diff] [blame] | 57 | if (nd_table[ret].devaddr) { |
| 58 | monitor_printf(mon, "Parameter addr not supported\n"); |
| 59 | return NULL; |
| 60 | } |
Markus Armbruster | 1f5f663 | 2009-06-18 15:14:09 +0200 | [diff] [blame] | 61 | return pci_nic_init(&nd_table[ret], "rtl8139", devaddr); |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Gerd Hoffmann | 30d335d | 2009-10-14 15:30:22 +0200 | [diff] [blame] | 64 | static int scsi_hot_add(DeviceState *adapter, DriveInfo *dinfo, int printinfo) |
| 65 | { |
| 66 | SCSIBus *scsibus; |
| 67 | SCSIDevice *scsidev; |
| 68 | |
| 69 | scsibus = DO_UPCAST(SCSIBus, qbus, QLIST_FIRST(&adapter->child_bus)); |
| 70 | if (!scsibus || strcmp(scsibus->qbus.info->name, "SCSI") != 0) { |
| 71 | qemu_error("Device is not a SCSI adapter\n"); |
| 72 | return -1; |
| 73 | } |
| 74 | |
| 75 | /* |
| 76 | * drive_init() tries to find a default for dinfo->unit. Doesn't |
| 77 | * work at all for hotplug though as we assign the device to a |
| 78 | * specific bus instead of the first bus with spare scsi ids. |
| 79 | * |
| 80 | * Ditch the calculated value and reload from option string (if |
| 81 | * specified). |
| 82 | */ |
| 83 | dinfo->unit = qemu_opt_get_number(dinfo->opts, "unit", -1); |
| 84 | scsidev = scsi_bus_legacy_add_drive(scsibus, dinfo, dinfo->unit); |
| 85 | |
| 86 | if (printinfo) |
| 87 | qemu_error("OK bus %d, unit %d\n", scsibus->busnr, scsidev->id); |
| 88 | return 0; |
| 89 | } |
| 90 | |
Luiz Capitulino | f18c16d | 2009-08-28 15:27:14 -0300 | [diff] [blame] | 91 | void drive_hot_add(Monitor *mon, const QDict *qdict) |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 92 | { |
| 93 | int dom, pci_bus; |
| 94 | unsigned slot; |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 95 | int type, bus; |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 96 | PCIDevice *dev; |
Gerd Hoffmann | 4db49dc | 2009-09-25 21:42:47 +0200 | [diff] [blame] | 97 | DriveInfo *dinfo = NULL; |
Luiz Capitulino | f18c16d | 2009-08-28 15:27:14 -0300 | [diff] [blame] | 98 | const char *pci_addr = qdict_get_str(qdict, "pci_addr"); |
| 99 | const char *opts = qdict_get_str(qdict, "opts"); |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 100 | |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 101 | dinfo = add_init_drive(opts); |
| 102 | if (!dinfo) |
Gerd Hoffmann | 4db49dc | 2009-09-25 21:42:47 +0200 | [diff] [blame] | 103 | goto err; |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 104 | if (dinfo->devaddr) { |
Markus Armbruster | c2cc47a | 2009-06-18 15:14:10 +0200 | [diff] [blame] | 105 | monitor_printf(mon, "Parameter addr not supported\n"); |
Gerd Hoffmann | 4db49dc | 2009-09-25 21:42:47 +0200 | [diff] [blame] | 106 | goto err; |
Markus Armbruster | c2cc47a | 2009-06-18 15:14:10 +0200 | [diff] [blame] | 107 | } |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 108 | type = dinfo->type; |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 109 | bus = drive_get_max_bus (type); |
| 110 | |
| 111 | switch (type) { |
| 112 | case IF_SCSI: |
Gerd Hoffmann | 4db49dc | 2009-09-25 21:42:47 +0200 | [diff] [blame] | 113 | if (pci_read_devaddr(mon, pci_addr, &dom, &pci_bus, &slot)) { |
| 114 | goto err; |
| 115 | } |
Isaku Yamahata | c469e1d | 2009-11-12 14:58:36 +0900 | [diff] [blame] | 116 | dev = pci_find_device(pci_find_root_bus(0), pci_bus, slot, 0); |
Gerd Hoffmann | 4db49dc | 2009-09-25 21:42:47 +0200 | [diff] [blame] | 117 | if (!dev) { |
| 118 | monitor_printf(mon, "no pci device with address %s\n", pci_addr); |
| 119 | goto err; |
| 120 | } |
Gerd Hoffmann | 30d335d | 2009-10-14 15:30:22 +0200 | [diff] [blame] | 121 | if (scsi_hot_add(&dev->qdev, dinfo, 1) != 0) { |
| 122 | goto err; |
| 123 | } |
Gerd Hoffmann | 4db49dc | 2009-09-25 21:42:47 +0200 | [diff] [blame] | 124 | break; |
Gerd Hoffmann | 7101174 | 2009-09-25 21:42:48 +0200 | [diff] [blame] | 125 | case IF_NONE: |
| 126 | monitor_printf(mon, "OK\n"); |
| 127 | break; |
Gerd Hoffmann | 4db49dc | 2009-09-25 21:42:47 +0200 | [diff] [blame] | 128 | default: |
| 129 | monitor_printf(mon, "Can't hot-add drive to type %d\n", type); |
| 130 | goto err; |
| 131 | } |
| 132 | return; |
| 133 | |
| 134 | err: |
| 135 | if (dinfo) |
| 136 | drive_uninit(dinfo); |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 137 | return; |
| 138 | } |
| 139 | |
Markus Armbruster | 1f5f663 | 2009-06-18 15:14:09 +0200 | [diff] [blame] | 140 | static PCIDevice *qemu_pci_hot_add_storage(Monitor *mon, |
| 141 | const char *devaddr, |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 142 | const char *opts) |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 143 | { |
Markus Armbruster | 1f5f663 | 2009-06-18 15:14:09 +0200 | [diff] [blame] | 144 | PCIDevice *dev; |
Sebastian Herbszt | 06c79f4 | 2009-08-16 14:07:54 +0200 | [diff] [blame] | 145 | DriveInfo *dinfo = NULL; |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 146 | int type = -1; |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 147 | char buf[128]; |
Markus Armbruster | 49bd145 | 2009-09-25 03:53:49 +0200 | [diff] [blame] | 148 | PCIBus *bus; |
| 149 | int devfn; |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 150 | |
| 151 | if (get_param_value(buf, sizeof(buf), "if", opts)) { |
| 152 | if (!strcmp(buf, "scsi")) |
| 153 | type = IF_SCSI; |
| 154 | else if (!strcmp(buf, "virtio")) { |
| 155 | type = IF_VIRTIO; |
aliguori | 8707ecc | 2009-04-05 17:40:55 +0000 | [diff] [blame] | 156 | } else { |
| 157 | monitor_printf(mon, "type %s not a hotpluggable PCI device.\n", buf); |
Markus Armbruster | 1f5f663 | 2009-06-18 15:14:09 +0200 | [diff] [blame] | 158 | return NULL; |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 159 | } |
| 160 | } else { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 161 | monitor_printf(mon, "no if= specified\n"); |
Markus Armbruster | 1f5f663 | 2009-06-18 15:14:09 +0200 | [diff] [blame] | 162 | return NULL; |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | if (get_param_value(buf, sizeof(buf), "file", opts)) { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 166 | dinfo = add_init_drive(opts); |
| 167 | if (!dinfo) |
Markus Armbruster | 1f5f663 | 2009-06-18 15:14:09 +0200 | [diff] [blame] | 168 | return NULL; |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 169 | if (dinfo->devaddr) { |
Markus Armbruster | c2cc47a | 2009-06-18 15:14:10 +0200 | [diff] [blame] | 170 | monitor_printf(mon, "Parameter addr not supported\n"); |
| 171 | return NULL; |
| 172 | } |
Blue Swirl | 7432ff5 | 2009-08-23 06:12:54 +0000 | [diff] [blame] | 173 | } else { |
| 174 | dinfo = NULL; |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Markus Armbruster | 49bd145 | 2009-09-25 03:53:49 +0200 | [diff] [blame] | 177 | bus = pci_get_bus_devfn(&devfn, devaddr); |
| 178 | if (!bus) { |
| 179 | monitor_printf(mon, "Invalid PCI device address %s\n", devaddr); |
| 180 | return NULL; |
| 181 | } |
| 182 | |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 183 | switch (type) { |
| 184 | case IF_SCSI: |
Gerd Hoffmann | 5b684b5 | 2009-10-13 13:59:55 +0200 | [diff] [blame] | 185 | if (!dinfo) { |
| 186 | monitor_printf(mon, "scsi requires a backing file/device.\n"); |
| 187 | return NULL; |
| 188 | } |
Markus Armbruster | 499cf10 | 2009-09-25 03:53:53 +0200 | [diff] [blame] | 189 | dev = pci_create(bus, devfn, "lsi53c895a"); |
Gerd Hoffmann | 5b684b5 | 2009-10-13 13:59:55 +0200 | [diff] [blame] | 190 | if (qdev_init(&dev->qdev) < 0) |
| 191 | dev = NULL; |
| 192 | if (dev) { |
Gerd Hoffmann | 30d335d | 2009-10-14 15:30:22 +0200 | [diff] [blame] | 193 | if (scsi_hot_add(&dev->qdev, dinfo, 0) != 0) { |
| 194 | qdev_unplug(&dev->qdev); |
| 195 | dev = NULL; |
| 196 | } |
Gerd Hoffmann | 5b684b5 | 2009-10-13 13:59:55 +0200 | [diff] [blame] | 197 | } |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 198 | break; |
| 199 | case IF_VIRTIO: |
Blue Swirl | 7432ff5 | 2009-08-23 06:12:54 +0000 | [diff] [blame] | 200 | if (!dinfo) { |
| 201 | monitor_printf(mon, "virtio requires a backing file/device.\n"); |
| 202 | return NULL; |
| 203 | } |
Markus Armbruster | 499cf10 | 2009-09-25 03:53:53 +0200 | [diff] [blame] | 204 | dev = pci_create(bus, devfn, "virtio-blk-pci"); |
Gerd Hoffmann | d176c49 | 2009-07-31 12:25:41 +0200 | [diff] [blame] | 205 | qdev_prop_set_drive(&dev->qdev, "drive", dinfo); |
Gerd Hoffmann | 5b684b5 | 2009-10-13 13:59:55 +0200 | [diff] [blame] | 206 | if (qdev_init(&dev->qdev) < 0) |
| 207 | dev = NULL; |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 208 | break; |
Markus Armbruster | 1f5f663 | 2009-06-18 15:14:09 +0200 | [diff] [blame] | 209 | default: |
| 210 | dev = NULL; |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 211 | } |
Markus Armbruster | 1f5f663 | 2009-06-18 15:14:09 +0200 | [diff] [blame] | 212 | return dev; |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Luiz Capitulino | 1d4daa9 | 2009-08-28 15:27:15 -0300 | [diff] [blame] | 215 | void pci_device_hot_add(Monitor *mon, const QDict *qdict) |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 216 | { |
| 217 | PCIDevice *dev = NULL; |
Luiz Capitulino | 1d4daa9 | 2009-08-28 15:27:15 -0300 | [diff] [blame] | 218 | const char *pci_addr = qdict_get_str(qdict, "pci_addr"); |
| 219 | const char *type = qdict_get_str(qdict, "type"); |
| 220 | const char *opts = qdict_get_try_str(qdict, "opts"); |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 221 | |
Jan Kiszka | e9283f8 | 2009-06-26 00:04:00 +0200 | [diff] [blame] | 222 | /* strip legacy tag */ |
| 223 | if (!strncmp(pci_addr, "pci_addr=", 9)) { |
| 224 | pci_addr += 9; |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Jan Kiszka | a62acdc | 2009-06-26 00:04:10 +0200 | [diff] [blame] | 227 | if (!opts) { |
| 228 | opts = ""; |
| 229 | } |
| 230 | |
Jan Kiszka | e9283f8 | 2009-06-26 00:04:00 +0200 | [diff] [blame] | 231 | if (!strcmp(pci_addr, "auto")) |
| 232 | pci_addr = NULL; |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 233 | |
| 234 | if (strcmp(type, "nic") == 0) |
Jan Kiszka | e9283f8 | 2009-06-26 00:04:00 +0200 | [diff] [blame] | 235 | dev = qemu_pci_hot_add_nic(mon, pci_addr, opts); |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 236 | else if (strcmp(type, "storage") == 0) |
Jan Kiszka | e9283f8 | 2009-06-26 00:04:00 +0200 | [diff] [blame] | 237 | dev = qemu_pci_hot_add_storage(mon, pci_addr, opts); |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 238 | else |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 239 | monitor_printf(mon, "invalid type: %s\n", type); |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 240 | |
| 241 | if (dev) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 242 | monitor_printf(mon, "OK domain %d, bus %d, slot %d, function %d\n", |
| 243 | 0, pci_bus_num(dev->bus), PCI_SLOT(dev->devfn), |
| 244 | PCI_FUNC(dev->devfn)); |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 245 | } else |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 246 | monitor_printf(mon, "failed to add %s\n", opts); |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 247 | } |
| 248 | #endif |
| 249 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 250 | void pci_device_hot_remove(Monitor *mon, const char *pci_addr) |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 251 | { |
| 252 | PCIDevice *d; |
| 253 | int dom, bus; |
| 254 | unsigned slot; |
| 255 | |
Jan Kiszka | e9283f8 | 2009-06-26 00:04:00 +0200 | [diff] [blame] | 256 | if (pci_read_devaddr(mon, pci_addr, &dom, &bus, &slot)) { |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 257 | return; |
| 258 | } |
| 259 | |
Isaku Yamahata | c469e1d | 2009-11-12 14:58:36 +0900 | [diff] [blame] | 260 | d = pci_find_device(pci_find_root_bus(0), bus, slot, 0); |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 261 | if (!d) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 262 | monitor_printf(mon, "slot %d empty\n", slot); |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 263 | return; |
| 264 | } |
Gerd Hoffmann | 3f84865 | 2009-09-25 21:42:45 +0200 | [diff] [blame] | 265 | qdev_unplug(&d->qdev); |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Luiz Capitulino | 6848d82 | 2009-10-16 12:23:48 -0300 | [diff] [blame] | 268 | void do_pci_device_hot_remove(Monitor *mon, const QDict *qdict, |
| 269 | QObject **ret_data) |
Luiz Capitulino | 3818318 | 2009-08-28 15:27:08 -0300 | [diff] [blame] | 270 | { |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 271 | pci_device_hot_remove(mon, qdict_get_str(qdict, "pci_addr")); |
Luiz Capitulino | 3818318 | 2009-08-28 15:27:08 -0300 | [diff] [blame] | 272 | } |