1*7bd8b0d4SThomas Huth /* 2*7bd8b0d4SThomas Huth * IDE device functions 3*7bd8b0d4SThomas Huth * 4*7bd8b0d4SThomas Huth * Copyright (c) 2009 Gerd Hoffmann <kraxel@redhat.com> 5*7bd8b0d4SThomas Huth * 6*7bd8b0d4SThomas Huth * This library is free software; you can redistribute it and/or 7*7bd8b0d4SThomas Huth * modify it under the terms of the GNU Lesser General Public 8*7bd8b0d4SThomas Huth * License as published by the Free Software Foundation; either 9*7bd8b0d4SThomas Huth * version 2.1 of the License, or (at your option) any later version. 10*7bd8b0d4SThomas Huth * 11*7bd8b0d4SThomas Huth * This library is distributed in the hope that it will be useful, 12*7bd8b0d4SThomas Huth * but WITHOUT ANY WARRANTY; without even the implied warranty of 13*7bd8b0d4SThomas Huth * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14*7bd8b0d4SThomas Huth * Lesser General Public License for more details. 15*7bd8b0d4SThomas Huth * 16*7bd8b0d4SThomas Huth * You should have received a copy of the GNU Lesser General Public 17*7bd8b0d4SThomas Huth * License along with this library; if not, see <http://www.gnu.org/licenses/>. 18*7bd8b0d4SThomas Huth */ 19*7bd8b0d4SThomas Huth 20*7bd8b0d4SThomas Huth #include "qemu/osdep.h" 21*7bd8b0d4SThomas Huth #include "qapi/error.h" 22*7bd8b0d4SThomas Huth #include "qapi/qapi-types-block.h" 23*7bd8b0d4SThomas Huth #include "qemu/error-report.h" 24*7bd8b0d4SThomas Huth #include "qemu/module.h" 25*7bd8b0d4SThomas Huth #include "hw/ide/ide-dev.h" 26*7bd8b0d4SThomas Huth #include "sysemu/block-backend.h" 27*7bd8b0d4SThomas Huth #include "sysemu/blockdev.h" 28*7bd8b0d4SThomas Huth #include "sysemu/sysemu.h" 29*7bd8b0d4SThomas Huth #include "qapi/visitor.h" 30*7bd8b0d4SThomas Huth 31*7bd8b0d4SThomas Huth static Property ide_props[] = { 32*7bd8b0d4SThomas Huth DEFINE_PROP_UINT32("unit", IDEDevice, unit, -1), 33*7bd8b0d4SThomas Huth DEFINE_PROP_END_OF_LIST(), 34*7bd8b0d4SThomas Huth }; 35*7bd8b0d4SThomas Huth 36*7bd8b0d4SThomas Huth static void ide_qdev_realize(DeviceState *qdev, Error **errp) 37*7bd8b0d4SThomas Huth { 38*7bd8b0d4SThomas Huth IDEDevice *dev = IDE_DEVICE(qdev); 39*7bd8b0d4SThomas Huth IDEDeviceClass *dc = IDE_DEVICE_GET_CLASS(dev); 40*7bd8b0d4SThomas Huth IDEBus *bus = DO_UPCAST(IDEBus, qbus, qdev->parent_bus); 41*7bd8b0d4SThomas Huth 42*7bd8b0d4SThomas Huth if (dev->unit == -1) { 43*7bd8b0d4SThomas Huth dev->unit = bus->master ? 1 : 0; 44*7bd8b0d4SThomas Huth } 45*7bd8b0d4SThomas Huth 46*7bd8b0d4SThomas Huth if (dev->unit >= bus->max_units) { 47*7bd8b0d4SThomas Huth error_setg(errp, "Can't create IDE unit %d, bus supports only %d units", 48*7bd8b0d4SThomas Huth dev->unit, bus->max_units); 49*7bd8b0d4SThomas Huth return; 50*7bd8b0d4SThomas Huth } 51*7bd8b0d4SThomas Huth 52*7bd8b0d4SThomas Huth switch (dev->unit) { 53*7bd8b0d4SThomas Huth case 0: 54*7bd8b0d4SThomas Huth if (bus->master) { 55*7bd8b0d4SThomas Huth error_setg(errp, "IDE unit %d is in use", dev->unit); 56*7bd8b0d4SThomas Huth return; 57*7bd8b0d4SThomas Huth } 58*7bd8b0d4SThomas Huth bus->master = dev; 59*7bd8b0d4SThomas Huth break; 60*7bd8b0d4SThomas Huth case 1: 61*7bd8b0d4SThomas Huth if (bus->slave) { 62*7bd8b0d4SThomas Huth error_setg(errp, "IDE unit %d is in use", dev->unit); 63*7bd8b0d4SThomas Huth return; 64*7bd8b0d4SThomas Huth } 65*7bd8b0d4SThomas Huth bus->slave = dev; 66*7bd8b0d4SThomas Huth break; 67*7bd8b0d4SThomas Huth default: 68*7bd8b0d4SThomas Huth error_setg(errp, "Invalid IDE unit %d", dev->unit); 69*7bd8b0d4SThomas Huth return; 70*7bd8b0d4SThomas Huth } 71*7bd8b0d4SThomas Huth dc->realize(dev, errp); 72*7bd8b0d4SThomas Huth } 73*7bd8b0d4SThomas Huth 74*7bd8b0d4SThomas Huth void ide_dev_initfn(IDEDevice *dev, IDEDriveKind kind, Error **errp) 75*7bd8b0d4SThomas Huth { 76*7bd8b0d4SThomas Huth IDEBus *bus = DO_UPCAST(IDEBus, qbus, dev->qdev.parent_bus); 77*7bd8b0d4SThomas Huth IDEState *s = bus->ifs + dev->unit; 78*7bd8b0d4SThomas Huth int ret; 79*7bd8b0d4SThomas Huth 80*7bd8b0d4SThomas Huth if (!dev->conf.blk) { 81*7bd8b0d4SThomas Huth if (kind != IDE_CD) { 82*7bd8b0d4SThomas Huth error_setg(errp, "No drive specified"); 83*7bd8b0d4SThomas Huth return; 84*7bd8b0d4SThomas Huth } else { 85*7bd8b0d4SThomas Huth /* Anonymous BlockBackend for an empty drive */ 86*7bd8b0d4SThomas Huth dev->conf.blk = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL); 87*7bd8b0d4SThomas Huth ret = blk_attach_dev(dev->conf.blk, &dev->qdev); 88*7bd8b0d4SThomas Huth assert(ret == 0); 89*7bd8b0d4SThomas Huth } 90*7bd8b0d4SThomas Huth } 91*7bd8b0d4SThomas Huth 92*7bd8b0d4SThomas Huth if (dev->conf.discard_granularity == -1) { 93*7bd8b0d4SThomas Huth dev->conf.discard_granularity = 512; 94*7bd8b0d4SThomas Huth } else if (dev->conf.discard_granularity && 95*7bd8b0d4SThomas Huth dev->conf.discard_granularity != 512) { 96*7bd8b0d4SThomas Huth error_setg(errp, "discard_granularity must be 512 for ide"); 97*7bd8b0d4SThomas Huth return; 98*7bd8b0d4SThomas Huth } 99*7bd8b0d4SThomas Huth 100*7bd8b0d4SThomas Huth if (!blkconf_blocksizes(&dev->conf, errp)) { 101*7bd8b0d4SThomas Huth return; 102*7bd8b0d4SThomas Huth } 103*7bd8b0d4SThomas Huth 104*7bd8b0d4SThomas Huth if (dev->conf.logical_block_size != 512) { 105*7bd8b0d4SThomas Huth error_setg(errp, "logical_block_size must be 512 for IDE"); 106*7bd8b0d4SThomas Huth return; 107*7bd8b0d4SThomas Huth } 108*7bd8b0d4SThomas Huth 109*7bd8b0d4SThomas Huth if (kind != IDE_CD) { 110*7bd8b0d4SThomas Huth if (!blkconf_geometry(&dev->conf, &dev->chs_trans, 65535, 16, 255, 111*7bd8b0d4SThomas Huth errp)) { 112*7bd8b0d4SThomas Huth return; 113*7bd8b0d4SThomas Huth } 114*7bd8b0d4SThomas Huth } 115*7bd8b0d4SThomas Huth if (!blkconf_apply_backend_options(&dev->conf, kind == IDE_CD, 116*7bd8b0d4SThomas Huth kind != IDE_CD, errp)) { 117*7bd8b0d4SThomas Huth return; 118*7bd8b0d4SThomas Huth } 119*7bd8b0d4SThomas Huth 120*7bd8b0d4SThomas Huth if (ide_init_drive(s, dev->conf.blk, kind, 121*7bd8b0d4SThomas Huth dev->version, dev->serial, dev->model, dev->wwn, 122*7bd8b0d4SThomas Huth dev->conf.cyls, dev->conf.heads, dev->conf.secs, 123*7bd8b0d4SThomas Huth dev->chs_trans, errp) < 0) { 124*7bd8b0d4SThomas Huth return; 125*7bd8b0d4SThomas Huth } 126*7bd8b0d4SThomas Huth 127*7bd8b0d4SThomas Huth if (!dev->version) { 128*7bd8b0d4SThomas Huth dev->version = g_strdup(s->version); 129*7bd8b0d4SThomas Huth } 130*7bd8b0d4SThomas Huth if (!dev->serial) { 131*7bd8b0d4SThomas Huth dev->serial = g_strdup(s->drive_serial_str); 132*7bd8b0d4SThomas Huth } 133*7bd8b0d4SThomas Huth 134*7bd8b0d4SThomas Huth add_boot_device_path(dev->conf.bootindex, &dev->qdev, 135*7bd8b0d4SThomas Huth dev->unit ? "/disk@1" : "/disk@0"); 136*7bd8b0d4SThomas Huth 137*7bd8b0d4SThomas Huth add_boot_device_lchs(&dev->qdev, dev->unit ? "/disk@1" : "/disk@0", 138*7bd8b0d4SThomas Huth dev->conf.lcyls, 139*7bd8b0d4SThomas Huth dev->conf.lheads, 140*7bd8b0d4SThomas Huth dev->conf.lsecs); 141*7bd8b0d4SThomas Huth } 142*7bd8b0d4SThomas Huth 143*7bd8b0d4SThomas Huth static void ide_dev_get_bootindex(Object *obj, Visitor *v, const char *name, 144*7bd8b0d4SThomas Huth void *opaque, Error **errp) 145*7bd8b0d4SThomas Huth { 146*7bd8b0d4SThomas Huth IDEDevice *d = IDE_DEVICE(obj); 147*7bd8b0d4SThomas Huth 148*7bd8b0d4SThomas Huth visit_type_int32(v, name, &d->conf.bootindex, errp); 149*7bd8b0d4SThomas Huth } 150*7bd8b0d4SThomas Huth 151*7bd8b0d4SThomas Huth static void ide_dev_set_bootindex(Object *obj, Visitor *v, const char *name, 152*7bd8b0d4SThomas Huth void *opaque, Error **errp) 153*7bd8b0d4SThomas Huth { 154*7bd8b0d4SThomas Huth IDEDevice *d = IDE_DEVICE(obj); 155*7bd8b0d4SThomas Huth int32_t boot_index; 156*7bd8b0d4SThomas Huth Error *local_err = NULL; 157*7bd8b0d4SThomas Huth 158*7bd8b0d4SThomas Huth if (!visit_type_int32(v, name, &boot_index, errp)) { 159*7bd8b0d4SThomas Huth return; 160*7bd8b0d4SThomas Huth } 161*7bd8b0d4SThomas Huth /* check whether bootindex is present in fw_boot_order list */ 162*7bd8b0d4SThomas Huth check_boot_index(boot_index, &local_err); 163*7bd8b0d4SThomas Huth if (local_err) { 164*7bd8b0d4SThomas Huth goto out; 165*7bd8b0d4SThomas Huth } 166*7bd8b0d4SThomas Huth /* change bootindex to a new one */ 167*7bd8b0d4SThomas Huth d->conf.bootindex = boot_index; 168*7bd8b0d4SThomas Huth 169*7bd8b0d4SThomas Huth if (d->unit != -1) { 170*7bd8b0d4SThomas Huth add_boot_device_path(d->conf.bootindex, &d->qdev, 171*7bd8b0d4SThomas Huth d->unit ? "/disk@1" : "/disk@0"); 172*7bd8b0d4SThomas Huth } 173*7bd8b0d4SThomas Huth out: 174*7bd8b0d4SThomas Huth error_propagate(errp, local_err); 175*7bd8b0d4SThomas Huth } 176*7bd8b0d4SThomas Huth 177*7bd8b0d4SThomas Huth static void ide_dev_instance_init(Object *obj) 178*7bd8b0d4SThomas Huth { 179*7bd8b0d4SThomas Huth object_property_add(obj, "bootindex", "int32", 180*7bd8b0d4SThomas Huth ide_dev_get_bootindex, 181*7bd8b0d4SThomas Huth ide_dev_set_bootindex, NULL, NULL); 182*7bd8b0d4SThomas Huth object_property_set_int(obj, "bootindex", -1, NULL); 183*7bd8b0d4SThomas Huth } 184*7bd8b0d4SThomas Huth 185*7bd8b0d4SThomas Huth static void ide_hd_realize(IDEDevice *dev, Error **errp) 186*7bd8b0d4SThomas Huth { 187*7bd8b0d4SThomas Huth ide_dev_initfn(dev, IDE_HD, errp); 188*7bd8b0d4SThomas Huth } 189*7bd8b0d4SThomas Huth 190*7bd8b0d4SThomas Huth static void ide_cd_realize(IDEDevice *dev, Error **errp) 191*7bd8b0d4SThomas Huth { 192*7bd8b0d4SThomas Huth ide_dev_initfn(dev, IDE_CD, errp); 193*7bd8b0d4SThomas Huth } 194*7bd8b0d4SThomas Huth 195*7bd8b0d4SThomas Huth static Property ide_hd_properties[] = { 196*7bd8b0d4SThomas Huth DEFINE_IDE_DEV_PROPERTIES(), 197*7bd8b0d4SThomas Huth DEFINE_BLOCK_CHS_PROPERTIES(IDEDrive, dev.conf), 198*7bd8b0d4SThomas Huth DEFINE_PROP_BIOS_CHS_TRANS("bios-chs-trans", 199*7bd8b0d4SThomas Huth IDEDrive, dev.chs_trans, BIOS_ATA_TRANSLATION_AUTO), 200*7bd8b0d4SThomas Huth DEFINE_PROP_UINT16("rotation_rate", IDEDrive, dev.rotation_rate, 0), 201*7bd8b0d4SThomas Huth DEFINE_PROP_END_OF_LIST(), 202*7bd8b0d4SThomas Huth }; 203*7bd8b0d4SThomas Huth 204*7bd8b0d4SThomas Huth static void ide_hd_class_init(ObjectClass *klass, void *data) 205*7bd8b0d4SThomas Huth { 206*7bd8b0d4SThomas Huth DeviceClass *dc = DEVICE_CLASS(klass); 207*7bd8b0d4SThomas Huth IDEDeviceClass *k = IDE_DEVICE_CLASS(klass); 208*7bd8b0d4SThomas Huth 209*7bd8b0d4SThomas Huth k->realize = ide_hd_realize; 210*7bd8b0d4SThomas Huth dc->fw_name = "drive"; 211*7bd8b0d4SThomas Huth dc->desc = "virtual IDE disk"; 212*7bd8b0d4SThomas Huth device_class_set_props(dc, ide_hd_properties); 213*7bd8b0d4SThomas Huth } 214*7bd8b0d4SThomas Huth 215*7bd8b0d4SThomas Huth static const TypeInfo ide_hd_info = { 216*7bd8b0d4SThomas Huth .name = "ide-hd", 217*7bd8b0d4SThomas Huth .parent = TYPE_IDE_DEVICE, 218*7bd8b0d4SThomas Huth .instance_size = sizeof(IDEDrive), 219*7bd8b0d4SThomas Huth .class_init = ide_hd_class_init, 220*7bd8b0d4SThomas Huth }; 221*7bd8b0d4SThomas Huth 222*7bd8b0d4SThomas Huth static Property ide_cd_properties[] = { 223*7bd8b0d4SThomas Huth DEFINE_IDE_DEV_PROPERTIES(), 224*7bd8b0d4SThomas Huth DEFINE_PROP_END_OF_LIST(), 225*7bd8b0d4SThomas Huth }; 226*7bd8b0d4SThomas Huth 227*7bd8b0d4SThomas Huth static void ide_cd_class_init(ObjectClass *klass, void *data) 228*7bd8b0d4SThomas Huth { 229*7bd8b0d4SThomas Huth DeviceClass *dc = DEVICE_CLASS(klass); 230*7bd8b0d4SThomas Huth IDEDeviceClass *k = IDE_DEVICE_CLASS(klass); 231*7bd8b0d4SThomas Huth 232*7bd8b0d4SThomas Huth k->realize = ide_cd_realize; 233*7bd8b0d4SThomas Huth dc->fw_name = "drive"; 234*7bd8b0d4SThomas Huth dc->desc = "virtual IDE CD-ROM"; 235*7bd8b0d4SThomas Huth device_class_set_props(dc, ide_cd_properties); 236*7bd8b0d4SThomas Huth } 237*7bd8b0d4SThomas Huth 238*7bd8b0d4SThomas Huth static const TypeInfo ide_cd_info = { 239*7bd8b0d4SThomas Huth .name = "ide-cd", 240*7bd8b0d4SThomas Huth .parent = TYPE_IDE_DEVICE, 241*7bd8b0d4SThomas Huth .instance_size = sizeof(IDEDrive), 242*7bd8b0d4SThomas Huth .class_init = ide_cd_class_init, 243*7bd8b0d4SThomas Huth }; 244*7bd8b0d4SThomas Huth 245*7bd8b0d4SThomas Huth static void ide_device_class_init(ObjectClass *klass, void *data) 246*7bd8b0d4SThomas Huth { 247*7bd8b0d4SThomas Huth DeviceClass *k = DEVICE_CLASS(klass); 248*7bd8b0d4SThomas Huth k->realize = ide_qdev_realize; 249*7bd8b0d4SThomas Huth set_bit(DEVICE_CATEGORY_STORAGE, k->categories); 250*7bd8b0d4SThomas Huth k->bus_type = TYPE_IDE_BUS; 251*7bd8b0d4SThomas Huth device_class_set_props(k, ide_props); 252*7bd8b0d4SThomas Huth } 253*7bd8b0d4SThomas Huth 254*7bd8b0d4SThomas Huth static const TypeInfo ide_device_type_info = { 255*7bd8b0d4SThomas Huth .name = TYPE_IDE_DEVICE, 256*7bd8b0d4SThomas Huth .parent = TYPE_DEVICE, 257*7bd8b0d4SThomas Huth .instance_size = sizeof(IDEDevice), 258*7bd8b0d4SThomas Huth .abstract = true, 259*7bd8b0d4SThomas Huth .class_size = sizeof(IDEDeviceClass), 260*7bd8b0d4SThomas Huth .class_init = ide_device_class_init, 261*7bd8b0d4SThomas Huth .instance_init = ide_dev_instance_init, 262*7bd8b0d4SThomas Huth }; 263*7bd8b0d4SThomas Huth 264*7bd8b0d4SThomas Huth static void ide_register_types(void) 265*7bd8b0d4SThomas Huth { 266*7bd8b0d4SThomas Huth type_register_static(&ide_hd_info); 267*7bd8b0d4SThomas Huth type_register_static(&ide_cd_info); 268*7bd8b0d4SThomas Huth type_register_static(&ide_device_type_info); 269*7bd8b0d4SThomas Huth } 270*7bd8b0d4SThomas Huth 271*7bd8b0d4SThomas Huth type_init(ide_register_types) 272