]>
Commit | Line | Data |
---|---|---|
da4d0419 GH |
1 | /* |
2 | * ide bus support for qdev. | |
3 | * | |
4 | * Copyright (c) 2009 Gerd Hoffmann <[email protected]> | |
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 |
28 | static char *idebus_get_fw_dev_path(DeviceState *dev); |
29 | ||
3cb75a7c PB |
30 | static Property ide_props[] = { |
31 | DEFINE_PROP_UINT32("unit", IDEDevice, unit, -1), | |
32 | DEFINE_PROP_END_OF_LIST(), | |
33 | }; | |
34 | ||
da4d0419 GH |
35 | static struct BusInfo ide_bus_info = { |
36 | .name = "IDE", | |
37 | .size = sizeof(IDEBus), | |
dc1a46b6 | 38 | .get_fw_dev_path = idebus_get_fw_dev_path, |
da4d0419 GH |
39 | }; |
40 | ||
3835510f | 41 | void ide_bus_new(IDEBus *idebus, DeviceState *dev, int bus_id) |
da4d0419 | 42 | { |
1f850f10 | 43 | qbus_create_inplace(&idebus->qbus, &ide_bus_info, dev, NULL); |
3835510f | 44 | idebus->bus_id = bus_id; |
da4d0419 GH |
45 | } |
46 | ||
dc1a46b6 GN |
47 | static char *idebus_get_fw_dev_path(DeviceState *dev) |
48 | { | |
49 | char path[30]; | |
50 | ||
51 | snprintf(path, sizeof(path), "%s@%d", qdev_fw_name(dev), | |
52 | ((IDEBus*)dev->parent_bus)->bus_id); | |
53 | ||
54 | return strdup(path); | |
55 | } | |
56 | ||
d307af79 | 57 | static int ide_qdev_init(DeviceState *qdev) |
da4d0419 | 58 | { |
d148211c AL |
59 | IDEDevice *dev = IDE_DEVICE(qdev); |
60 | IDEDeviceClass *dc = IDE_DEVICE_GET_CLASS(dev); | |
da4d0419 GH |
61 | IDEBus *bus = DO_UPCAST(IDEBus, qbus, qdev->parent_bus); |
62 | ||
f8b6cc00 | 63 | if (!dev->conf.bs) { |
f597627f | 64 | error_report("No drive specified"); |
da4d0419 GH |
65 | goto err; |
66 | } | |
67 | if (dev->unit == -1) { | |
68 | dev->unit = bus->master ? 1 : 0; | |
69 | } | |
70 | switch (dev->unit) { | |
71 | case 0: | |
72 | if (bus->master) { | |
f597627f | 73 | error_report("IDE unit %d is in use", dev->unit); |
da4d0419 GH |
74 | goto err; |
75 | } | |
76 | bus->master = dev; | |
77 | break; | |
78 | case 1: | |
79 | if (bus->slave) { | |
f597627f | 80 | error_report("IDE unit %d is in use", dev->unit); |
da4d0419 GH |
81 | goto err; |
82 | } | |
83 | bus->slave = dev; | |
84 | break; | |
85 | default: | |
f597627f | 86 | error_report("Invalid IDE unit %d", dev->unit); |
da4d0419 GH |
87 | goto err; |
88 | } | |
d148211c | 89 | return dc->init(dev); |
da4d0419 GH |
90 | |
91 | err: | |
92 | return -1; | |
93 | } | |
94 | ||
da4d0419 GH |
95 | IDEDevice *ide_create_drive(IDEBus *bus, int unit, DriveInfo *drive) |
96 | { | |
97 | DeviceState *dev; | |
98 | ||
95b5edcd | 99 | dev = qdev_create(&bus->qbus, drive->media_cd ? "ide-cd" : "ide-hd"); |
da4d0419 | 100 | qdev_prop_set_uint32(dev, "unit", unit); |
18846dee | 101 | qdev_prop_set_drive_nofail(dev, "drive", drive->bdrv); |
fa12fbbe | 102 | qdev_init_nofail(dev); |
da4d0419 GH |
103 | return DO_UPCAST(IDEDevice, qdev, dev); |
104 | } | |
105 | ||
c0897e0c MA |
106 | void ide_get_bs(BlockDriverState *bs[], BusState *qbus) |
107 | { | |
108 | IDEBus *bus = DO_UPCAST(IDEBus, qbus, qbus); | |
109 | bs[0] = bus->master ? bus->master->conf.bs : NULL; | |
110 | bs[1] = bus->slave ? bus->slave->conf.bs : NULL; | |
111 | } | |
112 | ||
da4d0419 GH |
113 | /* --------------------------------- */ |
114 | ||
115 | typedef struct IDEDrive { | |
116 | IDEDevice dev; | |
117 | } IDEDrive; | |
118 | ||
1f56e32a | 119 | static int ide_dev_initfn(IDEDevice *dev, IDEDriveKind kind) |
da4d0419 GH |
120 | { |
121 | IDEBus *bus = DO_UPCAST(IDEBus, qbus, dev->qdev.parent_bus); | |
6ced55a5 MA |
122 | IDEState *s = bus->ifs + dev->unit; |
123 | const char *serial; | |
f8b6cc00 | 124 | DriveInfo *dinfo; |
6ced55a5 | 125 | |
d353fb72 CH |
126 | if (dev->conf.discard_granularity && dev->conf.discard_granularity != 512) { |
127 | error_report("discard_granularity must be 512 for ide"); | |
128 | return -1; | |
129 | } | |
130 | ||
6ced55a5 MA |
131 | serial = dev->serial; |
132 | if (!serial) { | |
133 | /* try to fall back to value set with legacy -drive serial=... */ | |
f8b6cc00 MA |
134 | dinfo = drive_get_by_blockdev(dev->conf.bs); |
135 | if (*dinfo->serial) { | |
136 | serial = dinfo->serial; | |
137 | } | |
6ced55a5 MA |
138 | } |
139 | ||
27e0c9a1 | 140 | if (ide_init_drive(s, dev->conf.bs, kind, |
95ebda85 | 141 | dev->version, serial, dev->model, dev->wwn) < 0) { |
c4d74df7 MA |
142 | return -1; |
143 | } | |
6ced55a5 | 144 | |
03432407 | 145 | if (!dev->version) { |
7267c094 | 146 | dev->version = g_strdup(s->version); |
03432407 | 147 | } |
6ced55a5 | 148 | if (!dev->serial) { |
7267c094 | 149 | dev->serial = g_strdup(s->drive_serial_str); |
6ced55a5 | 150 | } |
1ca4d09a GN |
151 | |
152 | add_boot_device_path(dev->conf.bootindex, &dev->qdev, | |
153 | dev->unit ? "/disk@1" : "/disk@0"); | |
154 | ||
da4d0419 GH |
155 | return 0; |
156 | } | |
157 | ||
1f56e32a MA |
158 | static int ide_hd_initfn(IDEDevice *dev) |
159 | { | |
160 | return ide_dev_initfn(dev, IDE_HD); | |
161 | } | |
162 | ||
163 | static int ide_cd_initfn(IDEDevice *dev) | |
164 | { | |
165 | return ide_dev_initfn(dev, IDE_CD); | |
166 | } | |
167 | ||
168 | static int ide_drive_initfn(IDEDevice *dev) | |
169 | { | |
95b5edcd MA |
170 | DriveInfo *dinfo = drive_get_by_blockdev(dev->conf.bs); |
171 | ||
172 | return ide_dev_initfn(dev, dinfo->media_cd ? IDE_CD : IDE_HD); | |
1f56e32a MA |
173 | } |
174 | ||
175 | #define DEFINE_IDE_DEV_PROPERTIES() \ | |
1f56e32a MA |
176 | DEFINE_BLOCK_PROPERTIES(IDEDrive, dev.conf), \ |
177 | DEFINE_PROP_STRING("ver", IDEDrive, dev.version), \ | |
95ebda85 | 178 | DEFINE_PROP_HEX64("wwn", IDEDrive, dev.wwn, 0), \ |
27e0c9a1 FB |
179 | DEFINE_PROP_STRING("serial", IDEDrive, dev.serial),\ |
180 | DEFINE_PROP_STRING("model", IDEDrive, dev.model) | |
1f56e32a | 181 | |
39bffca2 AL |
182 | static Property ide_hd_properties[] = { |
183 | DEFINE_IDE_DEV_PROPERTIES(), | |
184 | DEFINE_PROP_END_OF_LIST(), | |
185 | }; | |
186 | ||
d148211c AL |
187 | static void ide_hd_class_init(ObjectClass *klass, void *data) |
188 | { | |
39bffca2 | 189 | DeviceClass *dc = DEVICE_CLASS(klass); |
d148211c AL |
190 | IDEDeviceClass *k = IDE_DEVICE_CLASS(klass); |
191 | k->init = ide_hd_initfn; | |
39bffca2 AL |
192 | dc->fw_name = "drive"; |
193 | dc->desc = "virtual IDE disk"; | |
194 | dc->props = ide_hd_properties; | |
d148211c AL |
195 | } |
196 | ||
39bffca2 AL |
197 | static TypeInfo ide_hd_info = { |
198 | .name = "ide-hd", | |
199 | .parent = TYPE_IDE_DEVICE, | |
200 | .instance_size = sizeof(IDEDrive), | |
201 | .class_init = ide_hd_class_init, | |
202 | }; | |
203 | ||
204 | static Property ide_cd_properties[] = { | |
205 | DEFINE_IDE_DEV_PROPERTIES(), | |
206 | DEFINE_PROP_END_OF_LIST(), | |
da4d0419 GH |
207 | }; |
208 | ||
d148211c | 209 | static void ide_cd_class_init(ObjectClass *klass, void *data) |
da4d0419 | 210 | { |
39bffca2 | 211 | DeviceClass *dc = DEVICE_CLASS(klass); |
d148211c AL |
212 | IDEDeviceClass *k = IDE_DEVICE_CLASS(klass); |
213 | k->init = ide_cd_initfn; | |
39bffca2 AL |
214 | dc->fw_name = "drive"; |
215 | dc->desc = "virtual IDE CD-ROM"; | |
216 | dc->props = ide_cd_properties; | |
d148211c | 217 | } |
1f56e32a | 218 | |
39bffca2 AL |
219 | static TypeInfo ide_cd_info = { |
220 | .name = "ide-cd", | |
221 | .parent = TYPE_IDE_DEVICE, | |
222 | .instance_size = sizeof(IDEDrive), | |
223 | .class_init = ide_cd_class_init, | |
224 | }; | |
225 | ||
226 | static Property ide_drive_properties[] = { | |
227 | DEFINE_IDE_DEV_PROPERTIES(), | |
228 | DEFINE_PROP_END_OF_LIST(), | |
d148211c AL |
229 | }; |
230 | ||
231 | static void ide_drive_class_init(ObjectClass *klass, void *data) | |
232 | { | |
39bffca2 | 233 | DeviceClass *dc = DEVICE_CLASS(klass); |
d148211c AL |
234 | IDEDeviceClass *k = IDE_DEVICE_CLASS(klass); |
235 | k->init = ide_drive_initfn; | |
39bffca2 AL |
236 | dc->fw_name = "drive"; |
237 | dc->desc = "virtual IDE disk or CD-ROM (legacy)"; | |
238 | dc->props = ide_drive_properties; | |
d148211c AL |
239 | } |
240 | ||
39bffca2 AL |
241 | static TypeInfo ide_drive_info = { |
242 | .name = "ide-drive", | |
243 | .parent = TYPE_IDE_DEVICE, | |
244 | .instance_size = sizeof(IDEDrive), | |
245 | .class_init = ide_drive_class_init, | |
d148211c AL |
246 | }; |
247 | ||
39bffca2 AL |
248 | static void ide_device_class_init(ObjectClass *klass, void *data) |
249 | { | |
250 | DeviceClass *k = DEVICE_CLASS(klass); | |
251 | k->init = ide_qdev_init; | |
252 | k->bus_info = &ide_bus_info; | |
bce54474 | 253 | k->props = ide_props; |
39bffca2 AL |
254 | } |
255 | ||
d148211c AL |
256 | static TypeInfo ide_device_type_info = { |
257 | .name = TYPE_IDE_DEVICE, | |
258 | .parent = TYPE_DEVICE, | |
259 | .instance_size = sizeof(IDEDevice), | |
260 | .abstract = true, | |
261 | .class_size = sizeof(IDEDeviceClass), | |
39bffca2 | 262 | .class_init = ide_device_class_init, |
d148211c AL |
263 | }; |
264 | ||
83f7d43a | 265 | static void ide_register_types(void) |
d148211c | 266 | { |
39bffca2 AL |
267 | type_register_static(&ide_hd_info); |
268 | type_register_static(&ide_cd_info); | |
269 | type_register_static(&ide_drive_info); | |
d148211c | 270 | type_register_static(&ide_device_type_info); |
da4d0419 | 271 | } |
83f7d43a AF |
272 | |
273 | type_init(ide_register_types) |