Annotation of qemu/hw/pci-hotplug.c, revision 1.1.1.10

1.1       root        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 "pc.h"
1.1.1.4   root       30: #include "monitor.h"
1.1.1.5   root       31: #include "scsi.h"
1.1       root       32: #include "virtio-blk.h"
1.1.1.5   root       33: #include "qemu-config.h"
1.1.1.7   root       34: #include "blockdev.h"
1.1.1.10! root       35: #include "error.h"
1.1       root       36: 
1.1.1.5   root       37: #if defined(TARGET_I386)
1.1.1.4   root       38: static PCIDevice *qemu_pci_hot_add_nic(Monitor *mon,
1.1.1.5   root       39:                                        const char *devaddr,
                     40:                                        const char *opts_str)
1.1       root       41: {
1.1.1.5   root       42:     QemuOpts *opts;
                     43:     PCIBus *bus;
                     44:     int ret, devfn;
                     45: 
                     46:     bus = pci_get_bus_devfn(&devfn, devaddr);
                     47:     if (!bus) {
                     48:         monitor_printf(mon, "Invalid PCI device address %s\n", devaddr);
                     49:         return NULL;
                     50:     }
                     51:     if (!((BusState*)bus)->allow_hotplug) {
                     52:         monitor_printf(mon, "PCI bus doesn't support hotplug\n");
                     53:         return NULL;
                     54:     }
                     55: 
1.1.1.7   root       56:     opts = qemu_opts_parse(qemu_find_opts("net"), opts_str ? opts_str : "", 0);
1.1.1.5   root       57:     if (!opts) {
                     58:         return NULL;
                     59:     }
1.1       root       60: 
1.1.1.5   root       61:     qemu_opt_set(opts, "type", "nic");
                     62: 
                     63:     ret = net_client_init(mon, opts, 0);
1.1.1.3   root       64:     if (ret < 0)
1.1       root       65:         return NULL;
1.1.1.4   root       66:     if (nd_table[ret].devaddr) {
                     67:         monitor_printf(mon, "Parameter addr not supported\n");
                     68:         return NULL;
                     69:     }
                     70:     return pci_nic_init(&nd_table[ret], "rtl8139", devaddr);
1.1       root       71: }
                     72: 
1.1.1.6   root       73: static int scsi_hot_add(Monitor *mon, DeviceState *adapter,
                     74:                         DriveInfo *dinfo, int printinfo)
1.1.1.5   root       75: {
                     76:     SCSIBus *scsibus;
                     77:     SCSIDevice *scsidev;
                     78: 
                     79:     scsibus = DO_UPCAST(SCSIBus, qbus, QLIST_FIRST(&adapter->child_bus));
                     80:     if (!scsibus || strcmp(scsibus->qbus.info->name, "SCSI") != 0) {
1.1.1.6   root       81:         error_report("Device is not a SCSI adapter");
1.1.1.5   root       82:         return -1;
                     83:     }
                     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);
1.1.1.7   root       94:     dinfo->bus = scsibus->busnr;
1.1.1.9   root       95:     scsidev = scsi_bus_legacy_add_drive(scsibus, dinfo->bdrv, dinfo->unit,
                     96:                                         false, -1);
1.1.1.6   root       97:     if (!scsidev) {
                     98:         return -1;
                     99:     }
1.1.1.5   root      100:     dinfo->unit = scsidev->id;
                    101: 
                    102:     if (printinfo)
1.1.1.6   root      103:         monitor_printf(mon, "OK bus %d, unit %d\n",
                    104:                        scsibus->busnr, scsidev->id);
1.1.1.5   root      105:     return 0;
                    106: }
                    107: 
1.1.1.10! root      108: int pci_drive_hot_add(Monitor *mon, const QDict *qdict,
        !           109:                       DriveInfo *dinfo, int type)
1.1       root      110: {
                    111:     int dom, pci_bus;
                    112:     unsigned slot;
                    113:     PCIDevice *dev;
1.1.1.5   root      114:     const char *pci_addr = qdict_get_str(qdict, "pci_addr");
1.1       root      115: 
                    116:     switch (type) {
                    117:     case IF_SCSI:
1.1.1.5   root      118:         if (pci_read_devaddr(mon, pci_addr, &dom, &pci_bus, &slot)) {
                    119:             goto err;
                    120:         }
1.1.1.8   root      121:         dev = pci_find_device(pci_find_root_bus(dom), pci_bus,
                    122:                               PCI_DEVFN(slot, 0));
1.1.1.5   root      123:         if (!dev) {
                    124:             monitor_printf(mon, "no pci device with address %s\n", pci_addr);
                    125:             goto err;
                    126:         }
1.1.1.6   root      127:         if (scsi_hot_add(mon, &dev->qdev, dinfo, 1) != 0) {
1.1.1.5   root      128:             goto err;
                    129:         }
                    130:         break;
1.1       root      131:     default:
1.1.1.4   root      132:         monitor_printf(mon, "Can't hot-add drive to type %d\n", type);
1.1.1.5   root      133:         goto err;
1.1       root      134:     }
                    135: 
1.1.1.10! root      136:     return 0;
1.1.1.5   root      137: err:
1.1.1.10! root      138:     return -1;
1.1       root      139: }
                    140: 
1.1.1.4   root      141: static PCIDevice *qemu_pci_hot_add_storage(Monitor *mon,
                    142:                                            const char *devaddr,
                    143:                                            const char *opts)
1.1       root      144: {
1.1.1.4   root      145:     PCIDevice *dev;
1.1.1.5   root      146:     DriveInfo *dinfo = NULL;
                    147:     int type = -1;
1.1       root      148:     char buf[128];
1.1.1.5   root      149:     PCIBus *bus;
                    150:     int devfn;
1.1       root      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;
1.1.1.2   root      157:         } else {
1.1.1.4   root      158:             monitor_printf(mon, "type %s not a hotpluggable PCI device.\n", buf);
                    159:             return NULL;
1.1       root      160:         }
                    161:     } else {
1.1.1.4   root      162:         monitor_printf(mon, "no if= specified\n");
                    163:         return NULL;
1.1       root      164:     }
                    165: 
                    166:     if (get_param_value(buf, sizeof(buf), "file", opts)) {
1.1.1.5   root      167:         dinfo = add_init_drive(opts);
                    168:         if (!dinfo)
1.1.1.4   root      169:             return NULL;
1.1.1.5   root      170:         if (dinfo->devaddr) {
1.1.1.4   root      171:             monitor_printf(mon, "Parameter addr not supported\n");
                    172:             return NULL;
                    173:         }
1.1.1.5   root      174:     } else {
                    175:         dinfo = NULL;
                    176:     }
                    177: 
                    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:     }
                    183:     if (!((BusState*)bus)->allow_hotplug) {
                    184:         monitor_printf(mon, "PCI bus doesn't support hotplug\n");
1.1.1.4   root      185:         return NULL;
1.1       root      186:     }
                    187: 
                    188:     switch (type) {
                    189:     case IF_SCSI:
1.1.1.5   root      190:         dev = pci_create(bus, devfn, "lsi53c895a");
                    191:         if (qdev_init(&dev->qdev) < 0)
                    192:             dev = NULL;
                    193:         if (dev && dinfo) {
1.1.1.6   root      194:             if (scsi_hot_add(mon, &dev->qdev, dinfo, 0) != 0) {
1.1.1.10! root      195:                 qdev_unplug(&dev->qdev, NULL);
1.1.1.5   root      196:                 dev = NULL;
                    197:             }
                    198:         }
1.1       root      199:         break;
                    200:     case IF_VIRTIO:
1.1.1.5   root      201:         if (!dinfo) {
                    202:             monitor_printf(mon, "virtio requires a backing file/device.\n");
                    203:             return NULL;
                    204:         }
                    205:         dev = pci_create(bus, devfn, "virtio-blk-pci");
1.1.1.6   root      206:         if (qdev_prop_set_drive(&dev->qdev, "drive", dinfo->bdrv) < 0) {
                    207:             qdev_free(&dev->qdev);
                    208:             dev = NULL;
                    209:             break;
                    210:         }
1.1.1.5   root      211:         if (qdev_init(&dev->qdev) < 0)
                    212:             dev = NULL;
1.1       root      213:         break;
1.1.1.4   root      214:     default:
                    215:         dev = NULL;
1.1       root      216:     }
1.1.1.4   root      217:     return dev;
1.1       root      218: }
                    219: 
1.1.1.6   root      220: void pci_device_hot_add(Monitor *mon, const QDict *qdict)
1.1       root      221: {
                    222:     PCIDevice *dev = NULL;
1.1.1.5   root      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");
1.1       root      226: 
1.1.1.4   root      227:     /* strip legacy tag */
                    228:     if (!strncmp(pci_addr, "pci_addr=", 9)) {
                    229:         pci_addr += 9;
1.1       root      230:     }
                    231: 
1.1.1.4   root      232:     if (!opts) {
                    233:         opts = "";
1.1       root      234:     }
                    235: 
1.1.1.4   root      236:     if (!strcmp(pci_addr, "auto"))
                    237:         pci_addr = NULL;
                    238: 
1.1.1.6   root      239:     if (strcmp(type, "nic") == 0) {
1.1.1.4   root      240:         dev = qemu_pci_hot_add_nic(mon, pci_addr, opts);
1.1.1.6   root      241:     } else if (strcmp(type, "storage") == 0) {
1.1.1.4   root      242:         dev = qemu_pci_hot_add_storage(mon, pci_addr, opts);
1.1.1.6   root      243:     } else {
1.1.1.4   root      244:         monitor_printf(mon, "invalid type: %s\n", type);
1.1.1.6   root      245:     }
1.1       root      246: 
                    247:     if (dev) {
1.1.1.6   root      248:         monitor_printf(mon, "OK domain %d, bus %d, slot %d, function %d\n",
                    249:                        pci_find_domain(dev->bus),
                    250:                        pci_bus_num(dev->bus), PCI_SLOT(dev->devfn),
                    251:                        PCI_FUNC(dev->devfn));
1.1       root      252:     } else
1.1.1.4   root      253:         monitor_printf(mon, "failed to add %s\n", opts);
1.1       root      254: }
                    255: #endif
                    256: 
1.1.1.6   root      257: static int pci_device_hot_remove(Monitor *mon, const char *pci_addr)
1.1       root      258: {
                    259:     PCIDevice *d;
                    260:     int dom, bus;
                    261:     unsigned slot;
1.1.1.10! root      262:     Error *local_err = NULL;
1.1       root      263: 
1.1.1.4   root      264:     if (pci_read_devaddr(mon, pci_addr, &dom, &bus, &slot)) {
1.1.1.6   root      265:         return -1;
1.1       root      266:     }
                    267: 
1.1.1.8   root      268:     d = pci_find_device(pci_find_root_bus(dom), bus, PCI_DEVFN(slot, 0));
1.1       root      269:     if (!d) {
1.1.1.4   root      270:         monitor_printf(mon, "slot %d empty\n", slot);
1.1.1.6   root      271:         return -1;
1.1       root      272:     }
1.1.1.10! root      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;
1.1       root      282: }
                    283: 
1.1.1.6   root      284: void do_pci_device_hot_remove(Monitor *mon, const QDict *qdict)
1.1       root      285: {
1.1.1.5   root      286:     pci_device_hot_remove(mon, qdict_get_str(qdict, "pci_addr"));
1.1       root      287: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.