]> git.proxmox.com Git - qemu.git/blame - hw/ide/qdev.c
ehci: fix port count.
[qemu.git] / hw / ide / qdev.c
CommitLineData
da4d0419
GH
1/*
2 * ide bus support for qdev.
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 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#include <hw/hw.h>
da4d0419 20#include "dma.h"
f597627f 21#include "qemu-error.h"
da4d0419 22#include <hw/ide/internal.h>
2446333c 23#include "blockdev.h"
1ca4d09a 24#include "sysemu.h"
da4d0419
GH
25
26/* --------------------------------- */
27
dc1a46b6
GN
28static char *idebus_get_fw_dev_path(DeviceState *dev);
29
da4d0419
GH
30static struct BusInfo ide_bus_info = {
31 .name = "IDE",
32 .size = sizeof(IDEBus),
dc1a46b6 33 .get_fw_dev_path = idebus_get_fw_dev_path,
da4d0419
GH
34};
35
3835510f 36void ide_bus_new(IDEBus *idebus, DeviceState *dev, int bus_id)
da4d0419 37{
1f850f10 38 qbus_create_inplace(&idebus->qbus, &ide_bus_info, dev, NULL);
3835510f 39 idebus->bus_id = bus_id;
da4d0419
GH
40}
41
dc1a46b6
GN
42static char *idebus_get_fw_dev_path(DeviceState *dev)
43{
44 char path[30];
45
46 snprintf(path, sizeof(path), "%s@%d", qdev_fw_name(dev),
47 ((IDEBus*)dev->parent_bus)->bus_id);
48
49 return strdup(path);
50}
51
da4d0419
GH
52static int ide_qdev_init(DeviceState *qdev, DeviceInfo *base)
53{
54 IDEDevice *dev = DO_UPCAST(IDEDevice, qdev, qdev);
55 IDEDeviceInfo *info = DO_UPCAST(IDEDeviceInfo, qdev, base);
56 IDEBus *bus = DO_UPCAST(IDEBus, qbus, qdev->parent_bus);
57
f8b6cc00 58 if (!dev->conf.bs) {
f597627f 59 error_report("No drive specified");
da4d0419
GH
60 goto err;
61 }
62 if (dev->unit == -1) {
63 dev->unit = bus->master ? 1 : 0;
64 }
65 switch (dev->unit) {
66 case 0:
67 if (bus->master) {
f597627f 68 error_report("IDE unit %d is in use", dev->unit);
da4d0419
GH
69 goto err;
70 }
71 bus->master = dev;
72 break;
73 case 1:
74 if (bus->slave) {
f597627f 75 error_report("IDE unit %d is in use", dev->unit);
da4d0419
GH
76 goto err;
77 }
78 bus->slave = dev;
79 break;
80 default:
f597627f 81 error_report("Invalid IDE unit %d", dev->unit);
da4d0419
GH
82 goto err;
83 }
84 return info->init(dev);
85
86err:
87 return -1;
88}
89
90static void ide_qdev_register(IDEDeviceInfo *info)
91{
92 info->qdev.init = ide_qdev_init;
93 info->qdev.bus_info = &ide_bus_info;
94 qdev_register(&info->qdev);
95}
96
97IDEDevice *ide_create_drive(IDEBus *bus, int unit, DriveInfo *drive)
98{
99 DeviceState *dev;
100
95b5edcd 101 dev = qdev_create(&bus->qbus, drive->media_cd ? "ide-cd" : "ide-hd");
da4d0419 102 qdev_prop_set_uint32(dev, "unit", unit);
18846dee 103 qdev_prop_set_drive_nofail(dev, "drive", drive->bdrv);
fa12fbbe 104 qdev_init_nofail(dev);
da4d0419
GH
105 return DO_UPCAST(IDEDevice, qdev, dev);
106}
107
c0897e0c
MA
108void ide_get_bs(BlockDriverState *bs[], BusState *qbus)
109{
110 IDEBus *bus = DO_UPCAST(IDEBus, qbus, qbus);
111 bs[0] = bus->master ? bus->master->conf.bs : NULL;
112 bs[1] = bus->slave ? bus->slave->conf.bs : NULL;
113}
114
da4d0419
GH
115/* --------------------------------- */
116
117typedef struct IDEDrive {
118 IDEDevice dev;
119} IDEDrive;
120
1f56e32a 121static int ide_dev_initfn(IDEDevice *dev, IDEDriveKind kind)
da4d0419
GH
122{
123 IDEBus *bus = DO_UPCAST(IDEBus, qbus, dev->qdev.parent_bus);
6ced55a5
MA
124 IDEState *s = bus->ifs + dev->unit;
125 const char *serial;
f8b6cc00 126 DriveInfo *dinfo;
6ced55a5 127
d353fb72
CH
128 if (dev->conf.discard_granularity && dev->conf.discard_granularity != 512) {
129 error_report("discard_granularity must be 512 for ide");
130 return -1;
131 }
132
6ced55a5
MA
133 serial = dev->serial;
134 if (!serial) {
135 /* try to fall back to value set with legacy -drive serial=... */
f8b6cc00
MA
136 dinfo = drive_get_by_blockdev(dev->conf.bs);
137 if (*dinfo->serial) {
138 serial = dinfo->serial;
139 }
6ced55a5
MA
140 }
141
1f56e32a 142 if (ide_init_drive(s, dev->conf.bs, kind, dev->version, serial) < 0) {
c4d74df7
MA
143 return -1;
144 }
6ced55a5 145
03432407
MA
146 if (!dev->version) {
147 dev->version = qemu_strdup(s->version);
148 }
6ced55a5
MA
149 if (!dev->serial) {
150 dev->serial = qemu_strdup(s->drive_serial_str);
151 }
1ca4d09a
GN
152
153 add_boot_device_path(dev->conf.bootindex, &dev->qdev,
154 dev->unit ? "/disk@1" : "/disk@0");
155
da4d0419
GH
156 return 0;
157}
158
1f56e32a
MA
159static int ide_hd_initfn(IDEDevice *dev)
160{
161 return ide_dev_initfn(dev, IDE_HD);
162}
163
164static int ide_cd_initfn(IDEDevice *dev)
165{
166 return ide_dev_initfn(dev, IDE_CD);
167}
168
169static int ide_drive_initfn(IDEDevice *dev)
170{
95b5edcd
MA
171 DriveInfo *dinfo = drive_get_by_blockdev(dev->conf.bs);
172
173 return ide_dev_initfn(dev, dinfo->media_cd ? IDE_CD : IDE_HD);
1f56e32a
MA
174}
175
176#define DEFINE_IDE_DEV_PROPERTIES() \
177 DEFINE_PROP_UINT32("unit", IDEDrive, dev.unit, -1), \
178 DEFINE_BLOCK_PROPERTIES(IDEDrive, dev.conf), \
179 DEFINE_PROP_STRING("ver", IDEDrive, dev.version), \
180 DEFINE_PROP_STRING("serial", IDEDrive, dev.serial)
181
182static IDEDeviceInfo ide_dev_info[] = {
183 {
184 .qdev.name = "ide-hd",
185 .qdev.fw_name = "drive",
186 .qdev.desc = "virtual IDE disk",
187 .qdev.size = sizeof(IDEDrive),
188 .init = ide_hd_initfn,
189 .qdev.props = (Property[]) {
190 DEFINE_IDE_DEV_PROPERTIES(),
191 DEFINE_PROP_END_OF_LIST(),
192 }
193 },{
194 .qdev.name = "ide-cd",
195 .qdev.fw_name = "drive",
196 .qdev.desc = "virtual IDE CD-ROM",
197 .qdev.size = sizeof(IDEDrive),
198 .init = ide_cd_initfn,
199 .qdev.props = (Property[]) {
200 DEFINE_IDE_DEV_PROPERTIES(),
201 DEFINE_PROP_END_OF_LIST(),
202 }
203 },{
204 .qdev.name = "ide-drive", /* legacy -device ide-drive */
205 .qdev.fw_name = "drive",
206 .qdev.desc = "virtual IDE disk or CD-ROM (legacy)",
207 .qdev.size = sizeof(IDEDrive),
208 .init = ide_drive_initfn,
209 .qdev.props = (Property[]) {
210 DEFINE_IDE_DEV_PROPERTIES(),
211 DEFINE_PROP_END_OF_LIST(),
212 }
da4d0419
GH
213 }
214};
215
1f56e32a 216static void ide_dev_register(void)
da4d0419 217{
1f56e32a
MA
218 int i;
219
220 for (i = 0; i < ARRAY_SIZE(ide_dev_info); i++) {
221 ide_qdev_register(&ide_dev_info[i]);
222 }
da4d0419 223}
1f56e32a 224device_init(ide_dev_register);