xref: /openbmc/qemu/hw/ide/ide-dev.c (revision dcaff461)
17bd8b0d4SThomas Huth /*
27bd8b0d4SThomas Huth  * IDE device functions
37bd8b0d4SThomas Huth  *
47bd8b0d4SThomas Huth  * Copyright (c) 2009 Gerd Hoffmann <kraxel@redhat.com>
57bd8b0d4SThomas Huth  *
67bd8b0d4SThomas Huth  * This library is free software; you can redistribute it and/or
77bd8b0d4SThomas Huth  * modify it under the terms of the GNU Lesser General Public
87bd8b0d4SThomas Huth  * License as published by the Free Software Foundation; either
97bd8b0d4SThomas Huth  * version 2.1 of the License, or (at your option) any later version.
107bd8b0d4SThomas Huth  *
117bd8b0d4SThomas Huth  * This library is distributed in the hope that it will be useful,
127bd8b0d4SThomas Huth  * but WITHOUT ANY WARRANTY; without even the implied warranty of
137bd8b0d4SThomas Huth  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
147bd8b0d4SThomas Huth  * Lesser General Public License for more details.
157bd8b0d4SThomas Huth  *
167bd8b0d4SThomas Huth  * You should have received a copy of the GNU Lesser General Public
177bd8b0d4SThomas Huth  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
187bd8b0d4SThomas Huth  */
197bd8b0d4SThomas Huth 
207bd8b0d4SThomas Huth #include "qemu/osdep.h"
217bd8b0d4SThomas Huth #include "qapi/error.h"
227bd8b0d4SThomas Huth #include "qapi/qapi-types-block.h"
237bd8b0d4SThomas Huth #include "qemu/error-report.h"
247bd8b0d4SThomas Huth #include "qemu/module.h"
257bd8b0d4SThomas Huth #include "hw/ide/ide-dev.h"
267bd8b0d4SThomas Huth #include "sysemu/block-backend.h"
277bd8b0d4SThomas Huth #include "sysemu/blockdev.h"
287bd8b0d4SThomas Huth #include "sysemu/sysemu.h"
297bd8b0d4SThomas Huth #include "qapi/visitor.h"
300316482eSPhilippe Mathieu-Daudé #include "ide-internal.h"
317bd8b0d4SThomas Huth 
327bd8b0d4SThomas Huth static Property ide_props[] = {
337bd8b0d4SThomas Huth     DEFINE_PROP_UINT32("unit", IDEDevice, unit, -1),
347bd8b0d4SThomas Huth     DEFINE_PROP_END_OF_LIST(),
357bd8b0d4SThomas Huth };
367bd8b0d4SThomas Huth 
377bd8b0d4SThomas Huth static void ide_qdev_realize(DeviceState *qdev, Error **errp)
387bd8b0d4SThomas Huth {
397bd8b0d4SThomas Huth     IDEDevice *dev = IDE_DEVICE(qdev);
407bd8b0d4SThomas Huth     IDEDeviceClass *dc = IDE_DEVICE_GET_CLASS(dev);
417bd8b0d4SThomas Huth     IDEBus *bus = DO_UPCAST(IDEBus, qbus, qdev->parent_bus);
427bd8b0d4SThomas Huth 
437bd8b0d4SThomas Huth     if (dev->unit == -1) {
447bd8b0d4SThomas Huth         dev->unit = bus->master ? 1 : 0;
457bd8b0d4SThomas Huth     }
467bd8b0d4SThomas Huth 
477bd8b0d4SThomas Huth     if (dev->unit >= bus->max_units) {
487bd8b0d4SThomas Huth         error_setg(errp, "Can't create IDE unit %d, bus supports only %d units",
497bd8b0d4SThomas Huth                      dev->unit, bus->max_units);
507bd8b0d4SThomas Huth         return;
517bd8b0d4SThomas Huth     }
527bd8b0d4SThomas Huth 
537bd8b0d4SThomas Huth     switch (dev->unit) {
547bd8b0d4SThomas Huth     case 0:
557bd8b0d4SThomas Huth         if (bus->master) {
567bd8b0d4SThomas Huth             error_setg(errp, "IDE unit %d is in use", dev->unit);
577bd8b0d4SThomas Huth             return;
587bd8b0d4SThomas Huth         }
597bd8b0d4SThomas Huth         bus->master = dev;
607bd8b0d4SThomas Huth         break;
617bd8b0d4SThomas Huth     case 1:
627bd8b0d4SThomas Huth         if (bus->slave) {
637bd8b0d4SThomas Huth             error_setg(errp, "IDE unit %d is in use", dev->unit);
647bd8b0d4SThomas Huth             return;
657bd8b0d4SThomas Huth         }
667bd8b0d4SThomas Huth         bus->slave = dev;
677bd8b0d4SThomas Huth         break;
687bd8b0d4SThomas Huth     default:
697bd8b0d4SThomas Huth         error_setg(errp, "Invalid IDE unit %d", dev->unit);
707bd8b0d4SThomas Huth         return;
717bd8b0d4SThomas Huth     }
727bd8b0d4SThomas Huth     dc->realize(dev, errp);
737bd8b0d4SThomas Huth }
747bd8b0d4SThomas Huth 
757bd8b0d4SThomas Huth void ide_dev_initfn(IDEDevice *dev, IDEDriveKind kind, Error **errp)
767bd8b0d4SThomas Huth {
777bd8b0d4SThomas Huth     IDEBus *bus = DO_UPCAST(IDEBus, qbus, dev->qdev.parent_bus);
787bd8b0d4SThomas Huth     IDEState *s = bus->ifs + dev->unit;
797bd8b0d4SThomas Huth     int ret;
807bd8b0d4SThomas Huth 
817bd8b0d4SThomas Huth     if (!dev->conf.blk) {
827bd8b0d4SThomas Huth         if (kind != IDE_CD) {
837bd8b0d4SThomas Huth             error_setg(errp, "No drive specified");
847bd8b0d4SThomas Huth             return;
857bd8b0d4SThomas Huth         } else {
867bd8b0d4SThomas Huth             /* Anonymous BlockBackend for an empty drive */
877bd8b0d4SThomas Huth             dev->conf.blk = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL);
887bd8b0d4SThomas Huth             ret = blk_attach_dev(dev->conf.blk, &dev->qdev);
897bd8b0d4SThomas Huth             assert(ret == 0);
907bd8b0d4SThomas Huth         }
917bd8b0d4SThomas Huth     }
927bd8b0d4SThomas Huth 
937bd8b0d4SThomas Huth     if (dev->conf.discard_granularity == -1) {
947bd8b0d4SThomas Huth         dev->conf.discard_granularity = 512;
957bd8b0d4SThomas Huth     } else if (dev->conf.discard_granularity &&
967bd8b0d4SThomas Huth                dev->conf.discard_granularity != 512) {
977bd8b0d4SThomas Huth         error_setg(errp, "discard_granularity must be 512 for ide");
987bd8b0d4SThomas Huth         return;
997bd8b0d4SThomas Huth     }
1007bd8b0d4SThomas Huth 
1017bd8b0d4SThomas Huth     if (!blkconf_blocksizes(&dev->conf, errp)) {
1027bd8b0d4SThomas Huth         return;
1037bd8b0d4SThomas Huth     }
1047bd8b0d4SThomas Huth 
1057bd8b0d4SThomas Huth     if (dev->conf.logical_block_size != 512) {
1067bd8b0d4SThomas Huth         error_setg(errp, "logical_block_size must be 512 for IDE");
1077bd8b0d4SThomas Huth         return;
1087bd8b0d4SThomas Huth     }
1097bd8b0d4SThomas Huth 
1107bd8b0d4SThomas Huth     if (kind != IDE_CD) {
1117bd8b0d4SThomas Huth         if (!blkconf_geometry(&dev->conf, &dev->chs_trans, 65535, 16, 255,
1127bd8b0d4SThomas Huth                               errp)) {
1137bd8b0d4SThomas Huth             return;
1147bd8b0d4SThomas Huth         }
1157bd8b0d4SThomas Huth     }
1167bd8b0d4SThomas Huth     if (!blkconf_apply_backend_options(&dev->conf, kind == IDE_CD,
1177bd8b0d4SThomas Huth                                        kind != IDE_CD, errp)) {
1187bd8b0d4SThomas Huth         return;
1197bd8b0d4SThomas Huth     }
1207bd8b0d4SThomas Huth 
121*dcaff461SPaolo Bonzini     if (ide_init_drive(s, dev, kind, errp) < 0) {
1227bd8b0d4SThomas Huth         return;
1237bd8b0d4SThomas Huth     }
1247bd8b0d4SThomas Huth 
1257bd8b0d4SThomas Huth     if (!dev->version) {
1267bd8b0d4SThomas Huth         dev->version = g_strdup(s->version);
1277bd8b0d4SThomas Huth     }
1287bd8b0d4SThomas Huth     if (!dev->serial) {
1297bd8b0d4SThomas Huth         dev->serial = g_strdup(s->drive_serial_str);
1307bd8b0d4SThomas Huth     }
1317bd8b0d4SThomas Huth 
1327bd8b0d4SThomas Huth     add_boot_device_path(dev->conf.bootindex, &dev->qdev,
1337bd8b0d4SThomas Huth                          dev->unit ? "/disk@1" : "/disk@0");
1347bd8b0d4SThomas Huth 
1357bd8b0d4SThomas Huth     add_boot_device_lchs(&dev->qdev, dev->unit ? "/disk@1" : "/disk@0",
1367bd8b0d4SThomas Huth                          dev->conf.lcyls,
1377bd8b0d4SThomas Huth                          dev->conf.lheads,
1387bd8b0d4SThomas Huth                          dev->conf.lsecs);
1397bd8b0d4SThomas Huth }
1407bd8b0d4SThomas Huth 
1417bd8b0d4SThomas Huth static void ide_dev_get_bootindex(Object *obj, Visitor *v, const char *name,
1427bd8b0d4SThomas Huth                                   void *opaque, Error **errp)
1437bd8b0d4SThomas Huth {
1447bd8b0d4SThomas Huth     IDEDevice *d = IDE_DEVICE(obj);
1457bd8b0d4SThomas Huth 
1467bd8b0d4SThomas Huth     visit_type_int32(v, name, &d->conf.bootindex, errp);
1477bd8b0d4SThomas Huth }
1487bd8b0d4SThomas Huth 
1497bd8b0d4SThomas Huth static void ide_dev_set_bootindex(Object *obj, Visitor *v, const char *name,
1507bd8b0d4SThomas Huth                                   void *opaque, Error **errp)
1517bd8b0d4SThomas Huth {
1527bd8b0d4SThomas Huth     IDEDevice *d = IDE_DEVICE(obj);
1537bd8b0d4SThomas Huth     int32_t boot_index;
1547bd8b0d4SThomas Huth     Error *local_err = NULL;
1557bd8b0d4SThomas Huth 
1567bd8b0d4SThomas Huth     if (!visit_type_int32(v, name, &boot_index, errp)) {
1577bd8b0d4SThomas Huth         return;
1587bd8b0d4SThomas Huth     }
1597bd8b0d4SThomas Huth     /* check whether bootindex is present in fw_boot_order list  */
1607bd8b0d4SThomas Huth     check_boot_index(boot_index, &local_err);
1617bd8b0d4SThomas Huth     if (local_err) {
1627bd8b0d4SThomas Huth         goto out;
1637bd8b0d4SThomas Huth     }
1647bd8b0d4SThomas Huth     /* change bootindex to a new one */
1657bd8b0d4SThomas Huth     d->conf.bootindex = boot_index;
1667bd8b0d4SThomas Huth 
1677bd8b0d4SThomas Huth     if (d->unit != -1) {
1687bd8b0d4SThomas Huth         add_boot_device_path(d->conf.bootindex, &d->qdev,
1697bd8b0d4SThomas Huth                              d->unit ? "/disk@1" : "/disk@0");
1707bd8b0d4SThomas Huth     }
1717bd8b0d4SThomas Huth out:
1727bd8b0d4SThomas Huth     error_propagate(errp, local_err);
1737bd8b0d4SThomas Huth }
1747bd8b0d4SThomas Huth 
1757bd8b0d4SThomas Huth static void ide_dev_instance_init(Object *obj)
1767bd8b0d4SThomas Huth {
1777bd8b0d4SThomas Huth     object_property_add(obj, "bootindex", "int32",
1787bd8b0d4SThomas Huth                         ide_dev_get_bootindex,
1797bd8b0d4SThomas Huth                         ide_dev_set_bootindex, NULL, NULL);
1807bd8b0d4SThomas Huth     object_property_set_int(obj, "bootindex", -1, NULL);
1817bd8b0d4SThomas Huth }
1827bd8b0d4SThomas Huth 
1837bd8b0d4SThomas Huth static void ide_hd_realize(IDEDevice *dev, Error **errp)
1847bd8b0d4SThomas Huth {
1857bd8b0d4SThomas Huth     ide_dev_initfn(dev, IDE_HD, errp);
1867bd8b0d4SThomas Huth }
1877bd8b0d4SThomas Huth 
1887bd8b0d4SThomas Huth static void ide_cd_realize(IDEDevice *dev, Error **errp)
1897bd8b0d4SThomas Huth {
1907bd8b0d4SThomas Huth     ide_dev_initfn(dev, IDE_CD, errp);
1917bd8b0d4SThomas Huth }
1927bd8b0d4SThomas Huth 
1937bd8b0d4SThomas Huth static Property ide_hd_properties[] = {
1947bd8b0d4SThomas Huth     DEFINE_IDE_DEV_PROPERTIES(),
1957bd8b0d4SThomas Huth     DEFINE_BLOCK_CHS_PROPERTIES(IDEDrive, dev.conf),
1967bd8b0d4SThomas Huth     DEFINE_PROP_BIOS_CHS_TRANS("bios-chs-trans",
1977bd8b0d4SThomas Huth                 IDEDrive, dev.chs_trans, BIOS_ATA_TRANSLATION_AUTO),
1987bd8b0d4SThomas Huth     DEFINE_PROP_UINT16("rotation_rate", IDEDrive, dev.rotation_rate, 0),
1997bd8b0d4SThomas Huth     DEFINE_PROP_END_OF_LIST(),
2007bd8b0d4SThomas Huth };
2017bd8b0d4SThomas Huth 
2027bd8b0d4SThomas Huth static void ide_hd_class_init(ObjectClass *klass, void *data)
2037bd8b0d4SThomas Huth {
2047bd8b0d4SThomas Huth     DeviceClass *dc = DEVICE_CLASS(klass);
2057bd8b0d4SThomas Huth     IDEDeviceClass *k = IDE_DEVICE_CLASS(klass);
2067bd8b0d4SThomas Huth 
2077bd8b0d4SThomas Huth     k->realize  = ide_hd_realize;
2087bd8b0d4SThomas Huth     dc->fw_name = "drive";
2097bd8b0d4SThomas Huth     dc->desc    = "virtual IDE disk";
2107bd8b0d4SThomas Huth     device_class_set_props(dc, ide_hd_properties);
2117bd8b0d4SThomas Huth }
2127bd8b0d4SThomas Huth 
2137bd8b0d4SThomas Huth static const TypeInfo ide_hd_info = {
2147bd8b0d4SThomas Huth     .name          = "ide-hd",
2157bd8b0d4SThomas Huth     .parent        = TYPE_IDE_DEVICE,
2167bd8b0d4SThomas Huth     .instance_size = sizeof(IDEDrive),
2177bd8b0d4SThomas Huth     .class_init    = ide_hd_class_init,
2187bd8b0d4SThomas Huth };
2197bd8b0d4SThomas Huth 
2207bd8b0d4SThomas Huth static Property ide_cd_properties[] = {
2217bd8b0d4SThomas Huth     DEFINE_IDE_DEV_PROPERTIES(),
2227bd8b0d4SThomas Huth     DEFINE_PROP_END_OF_LIST(),
2237bd8b0d4SThomas Huth };
2247bd8b0d4SThomas Huth 
2257bd8b0d4SThomas Huth static void ide_cd_class_init(ObjectClass *klass, void *data)
2267bd8b0d4SThomas Huth {
2277bd8b0d4SThomas Huth     DeviceClass *dc = DEVICE_CLASS(klass);
2287bd8b0d4SThomas Huth     IDEDeviceClass *k = IDE_DEVICE_CLASS(klass);
2297bd8b0d4SThomas Huth 
2307bd8b0d4SThomas Huth     k->realize  = ide_cd_realize;
2317bd8b0d4SThomas Huth     dc->fw_name = "drive";
2327bd8b0d4SThomas Huth     dc->desc    = "virtual IDE CD-ROM";
2337bd8b0d4SThomas Huth     device_class_set_props(dc, ide_cd_properties);
2347bd8b0d4SThomas Huth }
2357bd8b0d4SThomas Huth 
2367bd8b0d4SThomas Huth static const TypeInfo ide_cd_info = {
2377bd8b0d4SThomas Huth     .name          = "ide-cd",
2387bd8b0d4SThomas Huth     .parent        = TYPE_IDE_DEVICE,
2397bd8b0d4SThomas Huth     .instance_size = sizeof(IDEDrive),
2407bd8b0d4SThomas Huth     .class_init    = ide_cd_class_init,
2417bd8b0d4SThomas Huth };
2427bd8b0d4SThomas Huth 
2437bd8b0d4SThomas Huth static void ide_device_class_init(ObjectClass *klass, void *data)
2447bd8b0d4SThomas Huth {
2457bd8b0d4SThomas Huth     DeviceClass *k = DEVICE_CLASS(klass);
2467bd8b0d4SThomas Huth     k->realize = ide_qdev_realize;
2477bd8b0d4SThomas Huth     set_bit(DEVICE_CATEGORY_STORAGE, k->categories);
2487bd8b0d4SThomas Huth     k->bus_type = TYPE_IDE_BUS;
2497bd8b0d4SThomas Huth     device_class_set_props(k, ide_props);
2507bd8b0d4SThomas Huth }
2517bd8b0d4SThomas Huth 
2527bd8b0d4SThomas Huth static const TypeInfo ide_device_type_info = {
2537bd8b0d4SThomas Huth     .name = TYPE_IDE_DEVICE,
2547bd8b0d4SThomas Huth     .parent = TYPE_DEVICE,
2557bd8b0d4SThomas Huth     .instance_size = sizeof(IDEDevice),
2567bd8b0d4SThomas Huth     .abstract = true,
2577bd8b0d4SThomas Huth     .class_size = sizeof(IDEDeviceClass),
2587bd8b0d4SThomas Huth     .class_init = ide_device_class_init,
2597bd8b0d4SThomas Huth     .instance_init = ide_dev_instance_init,
2607bd8b0d4SThomas Huth };
2617bd8b0d4SThomas Huth 
2627bd8b0d4SThomas Huth static void ide_register_types(void)
2637bd8b0d4SThomas Huth {
2647bd8b0d4SThomas Huth     type_register_static(&ide_hd_info);
2657bd8b0d4SThomas Huth     type_register_static(&ide_cd_info);
2667bd8b0d4SThomas Huth     type_register_static(&ide_device_type_info);
2677bd8b0d4SThomas Huth }
2687bd8b0d4SThomas Huth 
2697bd8b0d4SThomas Huth type_init(ide_register_types)
270