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