17bd8b0d4SThomas Huth /* 27bd8b0d4SThomas Huth * ide bus support for qdev. 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 "qemu/error-report.h" 237bd8b0d4SThomas Huth #include "qemu/module.h" 24*32cad1ffSPhilippe Mathieu-Daudé #include "system/block-backend.h" 25*32cad1ffSPhilippe Mathieu-Daudé #include "system/blockdev.h" 26*32cad1ffSPhilippe Mathieu-Daudé #include "system/runstate.h" 270316482eSPhilippe Mathieu-Daudé #include "ide-internal.h" 287bd8b0d4SThomas Huth 297bd8b0d4SThomas Huth static char *idebus_get_fw_dev_path(DeviceState *dev); 307bd8b0d4SThomas Huth static void idebus_unrealize(BusState *qdev); 317bd8b0d4SThomas Huth 327bd8b0d4SThomas Huth static void ide_bus_class_init(ObjectClass *klass, void *data) 337bd8b0d4SThomas Huth { 347bd8b0d4SThomas Huth BusClass *k = BUS_CLASS(klass); 357bd8b0d4SThomas Huth 367bd8b0d4SThomas Huth k->get_fw_dev_path = idebus_get_fw_dev_path; 377bd8b0d4SThomas Huth k->unrealize = idebus_unrealize; 387bd8b0d4SThomas Huth } 397bd8b0d4SThomas Huth 407bd8b0d4SThomas Huth static void idebus_unrealize(BusState *bus) 417bd8b0d4SThomas Huth { 427bd8b0d4SThomas Huth IDEBus *ibus = IDE_BUS(bus); 437bd8b0d4SThomas Huth 447bd8b0d4SThomas Huth if (ibus->vmstate) { 457bd8b0d4SThomas Huth qemu_del_vm_change_state_handler(ibus->vmstate); 467bd8b0d4SThomas Huth } 477bd8b0d4SThomas Huth } 487bd8b0d4SThomas Huth 497bd8b0d4SThomas Huth static const TypeInfo ide_bus_info = { 507bd8b0d4SThomas Huth .name = TYPE_IDE_BUS, 517bd8b0d4SThomas Huth .parent = TYPE_BUS, 527bd8b0d4SThomas Huth .instance_size = sizeof(IDEBus), 537bd8b0d4SThomas Huth .class_init = ide_bus_class_init, 547bd8b0d4SThomas Huth }; 557bd8b0d4SThomas Huth 567bd8b0d4SThomas Huth void ide_bus_init(IDEBus *idebus, size_t idebus_size, DeviceState *dev, 577bd8b0d4SThomas Huth int bus_id, int max_units) 587bd8b0d4SThomas Huth { 597bd8b0d4SThomas Huth qbus_init(idebus, idebus_size, TYPE_IDE_BUS, dev, NULL); 607bd8b0d4SThomas Huth idebus->bus_id = bus_id; 617bd8b0d4SThomas Huth idebus->max_units = max_units; 627bd8b0d4SThomas Huth } 637bd8b0d4SThomas Huth 647bd8b0d4SThomas Huth static char *idebus_get_fw_dev_path(DeviceState *dev) 657bd8b0d4SThomas Huth { 667bd8b0d4SThomas Huth char path[30]; 677bd8b0d4SThomas Huth 687bd8b0d4SThomas Huth snprintf(path, sizeof(path), "%s@%x", qdev_fw_name(dev), 697bd8b0d4SThomas Huth ((IDEBus *)dev->parent_bus)->bus_id); 707bd8b0d4SThomas Huth 717bd8b0d4SThomas Huth return g_strdup(path); 727bd8b0d4SThomas Huth } 737bd8b0d4SThomas Huth 747bd8b0d4SThomas Huth IDEDevice *ide_bus_create_drive(IDEBus *bus, int unit, DriveInfo *drive) 757bd8b0d4SThomas Huth { 767bd8b0d4SThomas Huth DeviceState *dev; 777bd8b0d4SThomas Huth 787bd8b0d4SThomas Huth dev = qdev_new(drive->media_cd ? "ide-cd" : "ide-hd"); 797bd8b0d4SThomas Huth qdev_prop_set_uint32(dev, "unit", unit); 807bd8b0d4SThomas Huth qdev_prop_set_drive_err(dev, "drive", blk_by_legacy_dinfo(drive), 817bd8b0d4SThomas Huth &error_fatal); 827bd8b0d4SThomas Huth qdev_realize_and_unref(dev, &bus->qbus, &error_fatal); 837bd8b0d4SThomas Huth return DO_UPCAST(IDEDevice, qdev, dev); 847bd8b0d4SThomas Huth } 857bd8b0d4SThomas Huth 867bd8b0d4SThomas Huth int ide_get_geometry(BusState *bus, int unit, 877bd8b0d4SThomas Huth int16_t *cyls, int8_t *heads, int8_t *secs) 887bd8b0d4SThomas Huth { 897bd8b0d4SThomas Huth IDEState *s = &DO_UPCAST(IDEBus, qbus, bus)->ifs[unit]; 907bd8b0d4SThomas Huth 917bd8b0d4SThomas Huth if (s->drive_kind != IDE_HD || !s->blk) { 927bd8b0d4SThomas Huth return -1; 937bd8b0d4SThomas Huth } 947bd8b0d4SThomas Huth 957bd8b0d4SThomas Huth *cyls = s->cylinders; 967bd8b0d4SThomas Huth *heads = s->heads; 977bd8b0d4SThomas Huth *secs = s->sectors; 987bd8b0d4SThomas Huth return 0; 997bd8b0d4SThomas Huth } 1007bd8b0d4SThomas Huth 1017bd8b0d4SThomas Huth int ide_get_bios_chs_trans(BusState *bus, int unit) 1027bd8b0d4SThomas Huth { 1037bd8b0d4SThomas Huth return DO_UPCAST(IDEBus, qbus, bus)->ifs[unit].chs_trans; 1047bd8b0d4SThomas Huth } 1057bd8b0d4SThomas Huth 1067bd8b0d4SThomas Huth static void ide_bus_register_type(void) 1077bd8b0d4SThomas Huth { 1087bd8b0d4SThomas Huth type_register_static(&ide_bus_info); 1097bd8b0d4SThomas Huth } 1107bd8b0d4SThomas Huth 1117bd8b0d4SThomas Huth type_init(ide_bus_register_type) 112