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),
34*d13f4035SPaolo Bonzini DEFINE_PROP_BOOL("win2k-install-hack", IDEDevice, win2k_install_hack, false),
357bd8b0d4SThomas Huth DEFINE_PROP_END_OF_LIST(),
367bd8b0d4SThomas Huth };
377bd8b0d4SThomas Huth
ide_qdev_realize(DeviceState * qdev,Error ** errp)387bd8b0d4SThomas Huth static void ide_qdev_realize(DeviceState *qdev, Error **errp)
397bd8b0d4SThomas Huth {
407bd8b0d4SThomas Huth IDEDevice *dev = IDE_DEVICE(qdev);
417bd8b0d4SThomas Huth IDEDeviceClass *dc = IDE_DEVICE_GET_CLASS(dev);
427bd8b0d4SThomas Huth IDEBus *bus = DO_UPCAST(IDEBus, qbus, qdev->parent_bus);
437bd8b0d4SThomas Huth
447bd8b0d4SThomas Huth if (dev->unit == -1) {
457bd8b0d4SThomas Huth dev->unit = bus->master ? 1 : 0;
467bd8b0d4SThomas Huth }
477bd8b0d4SThomas Huth
487bd8b0d4SThomas Huth if (dev->unit >= bus->max_units) {
497bd8b0d4SThomas Huth error_setg(errp, "Can't create IDE unit %d, bus supports only %d units",
507bd8b0d4SThomas Huth dev->unit, bus->max_units);
517bd8b0d4SThomas Huth return;
527bd8b0d4SThomas Huth }
537bd8b0d4SThomas Huth
547bd8b0d4SThomas Huth switch (dev->unit) {
557bd8b0d4SThomas Huth case 0:
567bd8b0d4SThomas Huth if (bus->master) {
577bd8b0d4SThomas Huth error_setg(errp, "IDE unit %d is in use", dev->unit);
587bd8b0d4SThomas Huth return;
597bd8b0d4SThomas Huth }
607bd8b0d4SThomas Huth bus->master = dev;
617bd8b0d4SThomas Huth break;
627bd8b0d4SThomas Huth case 1:
637bd8b0d4SThomas Huth if (bus->slave) {
647bd8b0d4SThomas Huth error_setg(errp, "IDE unit %d is in use", dev->unit);
657bd8b0d4SThomas Huth return;
667bd8b0d4SThomas Huth }
677bd8b0d4SThomas Huth bus->slave = dev;
687bd8b0d4SThomas Huth break;
697bd8b0d4SThomas Huth default:
707bd8b0d4SThomas Huth error_setg(errp, "Invalid IDE unit %d", dev->unit);
717bd8b0d4SThomas Huth return;
727bd8b0d4SThomas Huth }
737bd8b0d4SThomas Huth dc->realize(dev, errp);
747bd8b0d4SThomas Huth }
757bd8b0d4SThomas Huth
ide_dev_initfn(IDEDevice * dev,IDEDriveKind kind,Error ** errp)767bd8b0d4SThomas Huth void ide_dev_initfn(IDEDevice *dev, IDEDriveKind kind, Error **errp)
777bd8b0d4SThomas Huth {
787bd8b0d4SThomas Huth IDEBus *bus = DO_UPCAST(IDEBus, qbus, dev->qdev.parent_bus);
797bd8b0d4SThomas Huth IDEState *s = bus->ifs + dev->unit;
807bd8b0d4SThomas Huth int ret;
817bd8b0d4SThomas Huth
827bd8b0d4SThomas Huth if (!dev->conf.blk) {
837bd8b0d4SThomas Huth if (kind != IDE_CD) {
847bd8b0d4SThomas Huth error_setg(errp, "No drive specified");
857bd8b0d4SThomas Huth return;
867bd8b0d4SThomas Huth } else {
877bd8b0d4SThomas Huth /* Anonymous BlockBackend for an empty drive */
887bd8b0d4SThomas Huth dev->conf.blk = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL);
897bd8b0d4SThomas Huth ret = blk_attach_dev(dev->conf.blk, &dev->qdev);
907bd8b0d4SThomas Huth assert(ret == 0);
917bd8b0d4SThomas Huth }
927bd8b0d4SThomas Huth }
937bd8b0d4SThomas Huth
947bd8b0d4SThomas Huth if (dev->conf.discard_granularity == -1) {
957bd8b0d4SThomas Huth dev->conf.discard_granularity = 512;
967bd8b0d4SThomas Huth } else if (dev->conf.discard_granularity &&
977bd8b0d4SThomas Huth dev->conf.discard_granularity != 512) {
987bd8b0d4SThomas Huth error_setg(errp, "discard_granularity must be 512 for ide");
997bd8b0d4SThomas Huth return;
1007bd8b0d4SThomas Huth }
1017bd8b0d4SThomas Huth
1027bd8b0d4SThomas Huth if (!blkconf_blocksizes(&dev->conf, errp)) {
1037bd8b0d4SThomas Huth return;
1047bd8b0d4SThomas Huth }
1057bd8b0d4SThomas Huth
1067bd8b0d4SThomas Huth if (dev->conf.logical_block_size != 512) {
1077bd8b0d4SThomas Huth error_setg(errp, "logical_block_size must be 512 for IDE");
1087bd8b0d4SThomas Huth return;
1097bd8b0d4SThomas Huth }
1107bd8b0d4SThomas Huth
1117bd8b0d4SThomas Huth if (kind != IDE_CD) {
1127bd8b0d4SThomas Huth if (!blkconf_geometry(&dev->conf, &dev->chs_trans, 65535, 16, 255,
1137bd8b0d4SThomas Huth errp)) {
1147bd8b0d4SThomas Huth return;
1157bd8b0d4SThomas Huth }
1167bd8b0d4SThomas Huth }
1177bd8b0d4SThomas Huth if (!blkconf_apply_backend_options(&dev->conf, kind == IDE_CD,
1187bd8b0d4SThomas Huth kind != IDE_CD, errp)) {
1197bd8b0d4SThomas Huth return;
1207bd8b0d4SThomas Huth }
1217bd8b0d4SThomas Huth
122dcaff461SPaolo Bonzini if (ide_init_drive(s, dev, kind, errp) < 0) {
1237bd8b0d4SThomas Huth return;
1247bd8b0d4SThomas Huth }
1257bd8b0d4SThomas Huth
1267bd8b0d4SThomas Huth if (!dev->version) {
1277bd8b0d4SThomas Huth dev->version = g_strdup(s->version);
1287bd8b0d4SThomas Huth }
1297bd8b0d4SThomas Huth if (!dev->serial) {
1307bd8b0d4SThomas Huth dev->serial = g_strdup(s->drive_serial_str);
1317bd8b0d4SThomas Huth }
1327bd8b0d4SThomas Huth
1337bd8b0d4SThomas Huth add_boot_device_path(dev->conf.bootindex, &dev->qdev,
1347bd8b0d4SThomas Huth dev->unit ? "/disk@1" : "/disk@0");
1357bd8b0d4SThomas Huth
1367bd8b0d4SThomas Huth add_boot_device_lchs(&dev->qdev, dev->unit ? "/disk@1" : "/disk@0",
1377bd8b0d4SThomas Huth dev->conf.lcyls,
1387bd8b0d4SThomas Huth dev->conf.lheads,
1397bd8b0d4SThomas Huth dev->conf.lsecs);
1407bd8b0d4SThomas Huth }
1417bd8b0d4SThomas Huth
ide_dev_get_bootindex(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)1427bd8b0d4SThomas Huth static void ide_dev_get_bootindex(Object *obj, Visitor *v, const char *name,
1437bd8b0d4SThomas Huth void *opaque, Error **errp)
1447bd8b0d4SThomas Huth {
1457bd8b0d4SThomas Huth IDEDevice *d = IDE_DEVICE(obj);
1467bd8b0d4SThomas Huth
1477bd8b0d4SThomas Huth visit_type_int32(v, name, &d->conf.bootindex, errp);
1487bd8b0d4SThomas Huth }
1497bd8b0d4SThomas Huth
ide_dev_set_bootindex(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)1507bd8b0d4SThomas Huth static void ide_dev_set_bootindex(Object *obj, Visitor *v, const char *name,
1517bd8b0d4SThomas Huth void *opaque, Error **errp)
1527bd8b0d4SThomas Huth {
1537bd8b0d4SThomas Huth IDEDevice *d = IDE_DEVICE(obj);
1547bd8b0d4SThomas Huth int32_t boot_index;
1557bd8b0d4SThomas Huth Error *local_err = NULL;
1567bd8b0d4SThomas Huth
1577bd8b0d4SThomas Huth if (!visit_type_int32(v, name, &boot_index, errp)) {
1587bd8b0d4SThomas Huth return;
1597bd8b0d4SThomas Huth }
1607bd8b0d4SThomas Huth /* check whether bootindex is present in fw_boot_order list */
1617bd8b0d4SThomas Huth check_boot_index(boot_index, &local_err);
1627bd8b0d4SThomas Huth if (local_err) {
1637bd8b0d4SThomas Huth goto out;
1647bd8b0d4SThomas Huth }
1657bd8b0d4SThomas Huth /* change bootindex to a new one */
1667bd8b0d4SThomas Huth d->conf.bootindex = boot_index;
1677bd8b0d4SThomas Huth
1687bd8b0d4SThomas Huth if (d->unit != -1) {
1697bd8b0d4SThomas Huth add_boot_device_path(d->conf.bootindex, &d->qdev,
1707bd8b0d4SThomas Huth d->unit ? "/disk@1" : "/disk@0");
1717bd8b0d4SThomas Huth }
1727bd8b0d4SThomas Huth out:
1737bd8b0d4SThomas Huth error_propagate(errp, local_err);
1747bd8b0d4SThomas Huth }
1757bd8b0d4SThomas Huth
ide_dev_instance_init(Object * obj)1767bd8b0d4SThomas Huth static void ide_dev_instance_init(Object *obj)
1777bd8b0d4SThomas Huth {
1787bd8b0d4SThomas Huth object_property_add(obj, "bootindex", "int32",
1797bd8b0d4SThomas Huth ide_dev_get_bootindex,
1807bd8b0d4SThomas Huth ide_dev_set_bootindex, NULL, NULL);
1817bd8b0d4SThomas Huth object_property_set_int(obj, "bootindex", -1, NULL);
1827bd8b0d4SThomas Huth }
1837bd8b0d4SThomas Huth
ide_hd_realize(IDEDevice * dev,Error ** errp)1847bd8b0d4SThomas Huth static void ide_hd_realize(IDEDevice *dev, Error **errp)
1857bd8b0d4SThomas Huth {
1867bd8b0d4SThomas Huth ide_dev_initfn(dev, IDE_HD, errp);
1877bd8b0d4SThomas Huth }
1887bd8b0d4SThomas Huth
ide_cd_realize(IDEDevice * dev,Error ** errp)1897bd8b0d4SThomas Huth static void ide_cd_realize(IDEDevice *dev, Error **errp)
1907bd8b0d4SThomas Huth {
1917bd8b0d4SThomas Huth ide_dev_initfn(dev, IDE_CD, errp);
1927bd8b0d4SThomas Huth }
1937bd8b0d4SThomas Huth
1947bd8b0d4SThomas Huth static Property ide_hd_properties[] = {
1957bd8b0d4SThomas Huth DEFINE_IDE_DEV_PROPERTIES(),
1967bd8b0d4SThomas Huth DEFINE_BLOCK_CHS_PROPERTIES(IDEDrive, dev.conf),
1977bd8b0d4SThomas Huth DEFINE_PROP_BIOS_CHS_TRANS("bios-chs-trans",
1987bd8b0d4SThomas Huth IDEDrive, dev.chs_trans, BIOS_ATA_TRANSLATION_AUTO),
1997bd8b0d4SThomas Huth DEFINE_PROP_UINT16("rotation_rate", IDEDrive, dev.rotation_rate, 0),
2007bd8b0d4SThomas Huth DEFINE_PROP_END_OF_LIST(),
2017bd8b0d4SThomas Huth };
2027bd8b0d4SThomas Huth
ide_hd_class_init(ObjectClass * klass,void * data)2037bd8b0d4SThomas Huth static void ide_hd_class_init(ObjectClass *klass, void *data)
2047bd8b0d4SThomas Huth {
2057bd8b0d4SThomas Huth DeviceClass *dc = DEVICE_CLASS(klass);
2067bd8b0d4SThomas Huth IDEDeviceClass *k = IDE_DEVICE_CLASS(klass);
2077bd8b0d4SThomas Huth
2087bd8b0d4SThomas Huth k->realize = ide_hd_realize;
2097bd8b0d4SThomas Huth dc->fw_name = "drive";
2107bd8b0d4SThomas Huth dc->desc = "virtual IDE disk";
2117bd8b0d4SThomas Huth device_class_set_props(dc, ide_hd_properties);
2127bd8b0d4SThomas Huth }
2137bd8b0d4SThomas Huth
2147bd8b0d4SThomas Huth static const TypeInfo ide_hd_info = {
2157bd8b0d4SThomas Huth .name = "ide-hd",
2167bd8b0d4SThomas Huth .parent = TYPE_IDE_DEVICE,
2177bd8b0d4SThomas Huth .instance_size = sizeof(IDEDrive),
2187bd8b0d4SThomas Huth .class_init = ide_hd_class_init,
2197bd8b0d4SThomas Huth };
2207bd8b0d4SThomas Huth
2217bd8b0d4SThomas Huth static Property ide_cd_properties[] = {
2227bd8b0d4SThomas Huth DEFINE_IDE_DEV_PROPERTIES(),
2237bd8b0d4SThomas Huth DEFINE_PROP_END_OF_LIST(),
2247bd8b0d4SThomas Huth };
2257bd8b0d4SThomas Huth
ide_cd_class_init(ObjectClass * klass,void * data)2267bd8b0d4SThomas Huth static void ide_cd_class_init(ObjectClass *klass, void *data)
2277bd8b0d4SThomas Huth {
2287bd8b0d4SThomas Huth DeviceClass *dc = DEVICE_CLASS(klass);
2297bd8b0d4SThomas Huth IDEDeviceClass *k = IDE_DEVICE_CLASS(klass);
2307bd8b0d4SThomas Huth
2317bd8b0d4SThomas Huth k->realize = ide_cd_realize;
2327bd8b0d4SThomas Huth dc->fw_name = "drive";
2337bd8b0d4SThomas Huth dc->desc = "virtual IDE CD-ROM";
2347bd8b0d4SThomas Huth device_class_set_props(dc, ide_cd_properties);
2357bd8b0d4SThomas Huth }
2367bd8b0d4SThomas Huth
2377bd8b0d4SThomas Huth static const TypeInfo ide_cd_info = {
2387bd8b0d4SThomas Huth .name = "ide-cd",
2397bd8b0d4SThomas Huth .parent = TYPE_IDE_DEVICE,
2407bd8b0d4SThomas Huth .instance_size = sizeof(IDEDrive),
2417bd8b0d4SThomas Huth .class_init = ide_cd_class_init,
2427bd8b0d4SThomas Huth };
2437bd8b0d4SThomas Huth
ide_device_class_init(ObjectClass * klass,void * data)2447bd8b0d4SThomas Huth static void ide_device_class_init(ObjectClass *klass, void *data)
2457bd8b0d4SThomas Huth {
2467bd8b0d4SThomas Huth DeviceClass *k = DEVICE_CLASS(klass);
2477bd8b0d4SThomas Huth k->realize = ide_qdev_realize;
2487bd8b0d4SThomas Huth set_bit(DEVICE_CATEGORY_STORAGE, k->categories);
2497bd8b0d4SThomas Huth k->bus_type = TYPE_IDE_BUS;
2507bd8b0d4SThomas Huth device_class_set_props(k, ide_props);
2517bd8b0d4SThomas Huth }
2527bd8b0d4SThomas Huth
2537bd8b0d4SThomas Huth static const TypeInfo ide_device_type_info = {
2547bd8b0d4SThomas Huth .name = TYPE_IDE_DEVICE,
2557bd8b0d4SThomas Huth .parent = TYPE_DEVICE,
2567bd8b0d4SThomas Huth .instance_size = sizeof(IDEDevice),
2577bd8b0d4SThomas Huth .abstract = true,
2587bd8b0d4SThomas Huth .class_size = sizeof(IDEDeviceClass),
2597bd8b0d4SThomas Huth .class_init = ide_device_class_init,
2607bd8b0d4SThomas Huth .instance_init = ide_dev_instance_init,
2617bd8b0d4SThomas Huth };
2627bd8b0d4SThomas Huth
ide_register_types(void)2637bd8b0d4SThomas Huth static void ide_register_types(void)
2647bd8b0d4SThomas Huth {
2657bd8b0d4SThomas Huth type_register_static(&ide_hd_info);
2667bd8b0d4SThomas Huth type_register_static(&ide_cd_info);
2677bd8b0d4SThomas Huth type_register_static(&ide_device_type_info);
2687bd8b0d4SThomas Huth }
2697bd8b0d4SThomas Huth
2707bd8b0d4SThomas Huth type_init(ide_register_types)
271