2009-09-15 19:23:34 +00:00
|
|
|
/*
|
2024-02-20 09:55:00 +01:00
|
|
|
* IDE device functions
|
2009-09-15 19:23:34 +00:00
|
|
|
*
|
|
|
|
|
* Copyright (c) 2009 Gerd Hoffmann <kraxel@redhat.com>
|
|
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
* License as published by the Free Software Foundation; either
|
2020-10-23 12:44:24 +00:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2009-09-15 19:23:34 +00:00
|
|
|
*
|
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
2019-05-23 16:35:07 +02:00
|
|
|
|
2016-01-26 18:17:09 +00:00
|
|
|
#include "qemu/osdep.h"
|
2016-03-14 09:01:28 +01:00
|
|
|
#include "qapi/error.h"
|
2019-08-12 07:23:32 +02:00
|
|
|
#include "qapi/qapi-types-block.h"
|
2012-12-17 18:20:00 +01:00
|
|
|
#include "qemu/error-report.h"
|
2019-05-23 16:35:07 +02:00
|
|
|
#include "qemu/module.h"
|
2024-02-20 09:54:59 +01:00
|
|
|
#include "hw/ide/ide-dev.h"
|
2024-12-03 15:20:13 +01:00
|
|
|
#include "system/block-backend.h"
|
|
|
|
|
#include "system/blockdev.h"
|
|
|
|
|
#include "system/system.h"
|
2014-10-07 16:00:29 +08:00
|
|
|
#include "qapi/visitor.h"
|
2024-02-25 18:08:39 +01:00
|
|
|
#include "ide-internal.h"
|
2010-12-08 13:34:59 +02:00
|
|
|
|
2024-12-13 15:41:52 +00:00
|
|
|
static const Property ide_props[] = {
|
2012-03-28 18:01:36 +02:00
|
|
|
DEFINE_PROP_UINT32("unit", IDEDevice, unit, -1),
|
2024-02-20 17:09:30 +01:00
|
|
|
DEFINE_PROP_BOOL("win2k-install-hack", IDEDevice, win2k_install_hack, false),
|
2012-03-28 18:01:36 +02:00
|
|
|
};
|
|
|
|
|
|
2017-09-18 22:05:13 +08:00
|
|
|
static void ide_qdev_realize(DeviceState *qdev, Error **errp)
|
2009-09-15 19:23:34 +00:00
|
|
|
{
|
2011-12-16 13:41:12 -06:00
|
|
|
IDEDevice *dev = IDE_DEVICE(qdev);
|
|
|
|
|
IDEDeviceClass *dc = IDE_DEVICE_GET_CLASS(dev);
|
2009-09-15 19:23:34 +00:00
|
|
|
IDEBus *bus = DO_UPCAST(IDEBus, qbus, qdev->parent_bus);
|
|
|
|
|
|
|
|
|
|
if (dev->unit == -1) {
|
|
|
|
|
dev->unit = bus->master ? 1 : 0;
|
|
|
|
|
}
|
2013-05-06 15:58:04 +02:00
|
|
|
|
|
|
|
|
if (dev->unit >= bus->max_units) {
|
2017-09-18 22:05:13 +08:00
|
|
|
error_setg(errp, "Can't create IDE unit %d, bus supports only %d units",
|
2013-05-06 15:58:04 +02:00
|
|
|
dev->unit, bus->max_units);
|
2017-09-18 22:05:13 +08:00
|
|
|
return;
|
2013-05-06 15:58:04 +02:00
|
|
|
}
|
|
|
|
|
|
2009-09-15 19:23:34 +00:00
|
|
|
switch (dev->unit) {
|
|
|
|
|
case 0:
|
|
|
|
|
if (bus->master) {
|
2017-09-18 22:05:13 +08:00
|
|
|
error_setg(errp, "IDE unit %d is in use", dev->unit);
|
|
|
|
|
return;
|
2009-09-15 19:23:34 +00:00
|
|
|
}
|
|
|
|
|
bus->master = dev;
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
if (bus->slave) {
|
2017-09-18 22:05:13 +08:00
|
|
|
error_setg(errp, "IDE unit %d is in use", dev->unit);
|
|
|
|
|
return;
|
2009-09-15 19:23:34 +00:00
|
|
|
}
|
|
|
|
|
bus->slave = dev;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2017-09-18 22:05:13 +08:00
|
|
|
error_setg(errp, "Invalid IDE unit %d", dev->unit);
|
|
|
|
|
return;
|
2009-09-15 19:23:34 +00:00
|
|
|
}
|
2017-09-18 22:05:13 +08:00
|
|
|
dc->realize(dev, errp);
|
2009-09-15 19:23:34 +00:00
|
|
|
}
|
|
|
|
|
|
2024-02-20 09:54:59 +01:00
|
|
|
void ide_dev_initfn(IDEDevice *dev, IDEDriveKind kind, Error **errp)
|
2009-09-15 19:23:34 +00:00
|
|
|
{
|
|
|
|
|
IDEBus *bus = DO_UPCAST(IDEBus, qbus, dev->qdev.parent_bus);
|
2010-06-01 20:32:32 +02:00
|
|
|
IDEState *s = bus->ifs + dev->unit;
|
2017-07-11 14:04:08 +02:00
|
|
|
int ret;
|
2010-06-01 20:32:32 +02:00
|
|
|
|
2016-07-14 15:49:42 +02:00
|
|
|
if (!dev->conf.blk) {
|
|
|
|
|
if (kind != IDE_CD) {
|
2017-09-18 22:05:13 +08:00
|
|
|
error_setg(errp, "No drive specified");
|
|
|
|
|
return;
|
2016-07-14 15:49:42 +02:00
|
|
|
} else {
|
|
|
|
|
/* Anonymous BlockBackend for an empty drive */
|
2019-04-25 14:25:10 +02:00
|
|
|
dev->conf.blk = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL);
|
2017-07-11 14:04:08 +02:00
|
|
|
ret = blk_attach_dev(dev->conf.blk, &dev->qdev);
|
|
|
|
|
assert(ret == 0);
|
2016-07-14 15:49:42 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-08 14:06:14 +01:00
|
|
|
if (dev->conf.discard_granularity == -1) {
|
|
|
|
|
dev->conf.discard_granularity = 512;
|
|
|
|
|
} else if (dev->conf.discard_granularity &&
|
|
|
|
|
dev->conf.discard_granularity != 512) {
|
2017-09-18 22:05:13 +08:00
|
|
|
error_setg(errp, "discard_granularity must be 512 for ide");
|
|
|
|
|
return;
|
2011-05-19 10:58:19 +02:00
|
|
|
}
|
|
|
|
|
|
2020-05-29 01:55:10 +03:00
|
|
|
if (!blkconf_blocksizes(&dev->conf, errp)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-03 13:21:32 +01:00
|
|
|
if (dev->conf.logical_block_size != 512) {
|
2017-09-18 22:05:13 +08:00
|
|
|
error_setg(errp, "logical_block_size must be 512 for IDE");
|
|
|
|
|
return;
|
2014-12-03 13:21:32 +01:00
|
|
|
}
|
|
|
|
|
|
2018-06-26 14:11:23 -07:00
|
|
|
blkconf_locked(&dev->conf, &dev->locked);
|
|
|
|
|
|
2014-08-12 10:12:54 +08:00
|
|
|
if (kind != IDE_CD) {
|
2017-11-22 11:08:45 +08:00
|
|
|
if (!blkconf_geometry(&dev->conf, &dev->chs_trans, 65535, 16, 255,
|
|
|
|
|
errp)) {
|
2017-09-18 22:05:13 +08:00
|
|
|
return;
|
2014-08-12 10:12:54 +08:00
|
|
|
}
|
2012-07-10 11:12:44 +02:00
|
|
|
}
|
2017-11-22 11:08:45 +08:00
|
|
|
if (!blkconf_apply_backend_options(&dev->conf, kind == IDE_CD,
|
|
|
|
|
kind != IDE_CD, errp)) {
|
2017-09-18 22:05:13 +08:00
|
|
|
return;
|
2017-01-24 13:43:31 +01:00
|
|
|
}
|
2012-07-10 11:12:44 +02:00
|
|
|
|
2024-02-20 17:05:20 +01:00
|
|
|
if (ide_init_drive(s, dev, kind, errp) < 0) {
|
2017-09-18 22:05:13 +08:00
|
|
|
return;
|
2010-06-28 19:07:51 +02:00
|
|
|
}
|
2010-06-01 20:32:32 +02:00
|
|
|
|
2010-06-01 20:32:33 +02:00
|
|
|
if (!dev->version) {
|
2011-08-20 22:09:37 -05:00
|
|
|
dev->version = g_strdup(s->version);
|
2010-06-01 20:32:33 +02:00
|
|
|
}
|
2010-06-01 20:32:32 +02:00
|
|
|
if (!dev->serial) {
|
2011-08-20 22:09:37 -05:00
|
|
|
dev->serial = g_strdup(s->drive_serial_str);
|
2010-06-01 20:32:32 +02:00
|
|
|
}
|
2010-12-08 13:35:05 +02:00
|
|
|
|
|
|
|
|
add_boot_device_path(dev->conf.bootindex, &dev->qdev,
|
|
|
|
|
dev->unit ? "/disk@1" : "/disk@0");
|
2019-10-16 19:41:42 +03:00
|
|
|
|
|
|
|
|
add_boot_device_lchs(&dev->qdev, dev->unit ? "/disk@1" : "/disk@0",
|
|
|
|
|
dev->conf.lcyls,
|
|
|
|
|
dev->conf.lheads,
|
|
|
|
|
dev->conf.lsecs);
|
2009-09-15 19:23:34 +00:00
|
|
|
}
|
|
|
|
|
|
2016-01-29 06:48:55 -07:00
|
|
|
static void ide_dev_get_bootindex(Object *obj, Visitor *v, const char *name,
|
|
|
|
|
void *opaque, Error **errp)
|
2014-10-07 16:00:29 +08:00
|
|
|
{
|
|
|
|
|
IDEDevice *d = IDE_DEVICE(obj);
|
|
|
|
|
|
2016-01-29 06:48:54 -07:00
|
|
|
visit_type_int32(v, name, &d->conf.bootindex, errp);
|
2014-10-07 16:00:29 +08:00
|
|
|
}
|
|
|
|
|
|
2016-01-29 06:48:55 -07:00
|
|
|
static void ide_dev_set_bootindex(Object *obj, Visitor *v, const char *name,
|
|
|
|
|
void *opaque, Error **errp)
|
2014-10-07 16:00:29 +08:00
|
|
|
{
|
|
|
|
|
IDEDevice *d = IDE_DEVICE(obj);
|
|
|
|
|
int32_t boot_index;
|
|
|
|
|
Error *local_err = NULL;
|
|
|
|
|
|
2020-07-07 18:05:47 +02:00
|
|
|
if (!visit_type_int32(v, name, &boot_index, errp)) {
|
|
|
|
|
return;
|
2014-10-07 16:00:29 +08:00
|
|
|
}
|
|
|
|
|
/* check whether bootindex is present in fw_boot_order list */
|
|
|
|
|
check_boot_index(boot_index, &local_err);
|
|
|
|
|
if (local_err) {
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
/* change bootindex to a new one */
|
|
|
|
|
d->conf.bootindex = boot_index;
|
|
|
|
|
|
2014-10-07 16:00:35 +08:00
|
|
|
if (d->unit != -1) {
|
|
|
|
|
add_boot_device_path(d->conf.bootindex, &d->qdev,
|
|
|
|
|
d->unit ? "/disk@1" : "/disk@0");
|
|
|
|
|
}
|
2014-10-07 16:00:29 +08:00
|
|
|
out:
|
2016-06-13 18:57:56 -03:00
|
|
|
error_propagate(errp, local_err);
|
2014-10-07 16:00:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void ide_dev_instance_init(Object *obj)
|
|
|
|
|
{
|
|
|
|
|
object_property_add(obj, "bootindex", "int32",
|
|
|
|
|
ide_dev_get_bootindex,
|
2020-05-05 17:29:22 +02:00
|
|
|
ide_dev_set_bootindex, NULL, NULL);
|
2020-07-07 18:05:54 +02:00
|
|
|
object_property_set_int(obj, "bootindex", -1, NULL);
|
2014-10-07 16:00:29 +08:00
|
|
|
}
|
|
|
|
|
|
2017-09-18 22:05:13 +08:00
|
|
|
static void ide_hd_realize(IDEDevice *dev, Error **errp)
|
2011-05-16 15:04:52 +02:00
|
|
|
{
|
2017-09-18 22:05:13 +08:00
|
|
|
ide_dev_initfn(dev, IDE_HD, errp);
|
2011-05-16 15:04:52 +02:00
|
|
|
}
|
|
|
|
|
|
2017-09-18 22:05:13 +08:00
|
|
|
static void ide_cd_realize(IDEDevice *dev, Error **errp)
|
2011-05-16 15:04:52 +02:00
|
|
|
{
|
2017-09-18 22:05:13 +08:00
|
|
|
ide_dev_initfn(dev, IDE_CD, errp);
|
2011-05-16 15:04:52 +02:00
|
|
|
}
|
|
|
|
|
|
2024-12-13 15:41:52 +00:00
|
|
|
static const Property ide_hd_properties[] = {
|
2011-12-07 21:34:16 -06:00
|
|
|
DEFINE_IDE_DEV_PROPERTIES(),
|
2012-07-10 11:12:44 +02:00
|
|
|
DEFINE_BLOCK_CHS_PROPERTIES(IDEDrive, dev.conf),
|
2012-07-10 11:12:48 +02:00
|
|
|
DEFINE_PROP_BIOS_CHS_TRANS("bios-chs-trans",
|
|
|
|
|
IDEDrive, dev.chs_trans, BIOS_ATA_TRANSLATION_AUTO),
|
2017-10-04 12:40:08 +01:00
|
|
|
DEFINE_PROP_UINT16("rotation_rate", IDEDrive, dev.rotation_rate, 0),
|
2011-12-07 21:34:16 -06:00
|
|
|
};
|
|
|
|
|
|
2025-02-09 23:47:35 +01:00
|
|
|
static void ide_hd_class_init(ObjectClass *klass, const void *data)
|
2011-12-16 13:41:12 -06:00
|
|
|
{
|
2011-12-07 21:34:16 -06:00
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
2011-12-16 13:41:12 -06:00
|
|
|
IDEDeviceClass *k = IDE_DEVICE_CLASS(klass);
|
2017-09-18 22:05:13 +08:00
|
|
|
|
|
|
|
|
k->realize = ide_hd_realize;
|
2011-12-07 21:34:16 -06:00
|
|
|
dc->fw_name = "drive";
|
2017-09-18 22:05:13 +08:00
|
|
|
dc->desc = "virtual IDE disk";
|
2020-01-10 19:30:32 +04:00
|
|
|
device_class_set_props(dc, ide_hd_properties);
|
2011-12-16 13:41:12 -06:00
|
|
|
}
|
|
|
|
|
|
2013-01-10 16:19:07 +01:00
|
|
|
static const TypeInfo ide_hd_info = {
|
2011-12-07 21:34:16 -06:00
|
|
|
.name = "ide-hd",
|
|
|
|
|
.parent = TYPE_IDE_DEVICE,
|
|
|
|
|
.instance_size = sizeof(IDEDrive),
|
|
|
|
|
.class_init = ide_hd_class_init,
|
|
|
|
|
};
|
|
|
|
|
|
2024-12-13 15:41:52 +00:00
|
|
|
static const Property ide_cd_properties[] = {
|
2011-12-07 21:34:16 -06:00
|
|
|
DEFINE_IDE_DEV_PROPERTIES(),
|
2009-09-15 19:23:34 +00:00
|
|
|
};
|
|
|
|
|
|
2025-02-09 23:47:35 +01:00
|
|
|
static void ide_cd_class_init(ObjectClass *klass, const void *data)
|
2011-12-16 13:41:12 -06:00
|
|
|
{
|
2011-12-07 21:34:16 -06:00
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
2011-12-16 13:41:12 -06:00
|
|
|
IDEDeviceClass *k = IDE_DEVICE_CLASS(klass);
|
2017-09-18 22:05:13 +08:00
|
|
|
|
|
|
|
|
k->realize = ide_cd_realize;
|
2011-12-07 21:34:16 -06:00
|
|
|
dc->fw_name = "drive";
|
2017-09-18 22:05:13 +08:00
|
|
|
dc->desc = "virtual IDE CD-ROM";
|
2020-01-10 19:30:32 +04:00
|
|
|
device_class_set_props(dc, ide_cd_properties);
|
2011-12-16 13:41:12 -06:00
|
|
|
}
|
|
|
|
|
|
2013-01-10 16:19:07 +01:00
|
|
|
static const TypeInfo ide_cd_info = {
|
2011-12-07 21:34:16 -06:00
|
|
|
.name = "ide-cd",
|
|
|
|
|
.parent = TYPE_IDE_DEVICE,
|
|
|
|
|
.instance_size = sizeof(IDEDrive),
|
|
|
|
|
.class_init = ide_cd_class_init,
|
|
|
|
|
};
|
|
|
|
|
|
2025-02-09 23:47:35 +01:00
|
|
|
static void ide_device_class_init(ObjectClass *klass, const void *data)
|
2011-12-07 21:34:16 -06:00
|
|
|
{
|
|
|
|
|
DeviceClass *k = DEVICE_CLASS(klass);
|
2017-09-18 22:05:13 +08:00
|
|
|
k->realize = ide_qdev_realize;
|
2013-07-29 17:17:45 +03:00
|
|
|
set_bit(DEVICE_CATEGORY_STORAGE, k->categories);
|
2012-05-02 09:00:20 +02:00
|
|
|
k->bus_type = TYPE_IDE_BUS;
|
2020-01-10 19:30:32 +04:00
|
|
|
device_class_set_props(k, ide_props);
|
2011-12-07 21:34:16 -06:00
|
|
|
}
|
|
|
|
|
|
2013-01-10 16:19:07 +01:00
|
|
|
static const TypeInfo ide_device_type_info = {
|
2011-12-16 13:41:12 -06:00
|
|
|
.name = TYPE_IDE_DEVICE,
|
|
|
|
|
.parent = TYPE_DEVICE,
|
|
|
|
|
.instance_size = sizeof(IDEDevice),
|
|
|
|
|
.abstract = true,
|
|
|
|
|
.class_size = sizeof(IDEDeviceClass),
|
2011-12-07 21:34:16 -06:00
|
|
|
.class_init = ide_device_class_init,
|
2014-10-07 16:00:29 +08:00
|
|
|
.instance_init = ide_dev_instance_init,
|
2011-12-16 13:41:12 -06:00
|
|
|
};
|
|
|
|
|
|
2012-02-09 15:20:55 +01:00
|
|
|
static void ide_register_types(void)
|
2009-09-15 19:23:34 +00:00
|
|
|
{
|
2011-12-07 21:34:16 -06:00
|
|
|
type_register_static(&ide_hd_info);
|
|
|
|
|
type_register_static(&ide_cd_info);
|
2011-12-16 13:41:12 -06:00
|
|
|
type_register_static(&ide_device_type_info);
|
2009-09-15 19:23:34 +00:00
|
|
|
}
|
2012-02-09 15:20:55 +01:00
|
|
|
|
|
|
|
|
type_init(ide_register_types)
|
2026-02-06 19:51:23 -05:00
|
|
|
|
|
|
|
|
#ifdef __ANDROID__
|
|
|
|
|
void xemu_android_force_ide_dev_link(void)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
#endif
|