]>
Commit | Line | Data |
---|---|---|
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 | */ | |
0b8fa32f | 19 | |
53239262 | 20 | #include "qemu/osdep.h" |
9c17d615 | 21 | #include "sysemu/dma.h" |
da34e65c | 22 | #include "qapi/error.h" |
2ae16a6a | 23 | #include "qapi/qapi-types-block.h" |
1de7afc9 | 24 | #include "qemu/error-report.h" |
db725815 | 25 | #include "qemu/main-loop.h" |
0b8fa32f | 26 | #include "qemu/module.h" |
a9c94277 | 27 | #include "hw/ide/internal.h" |
a27bd6c7 | 28 | #include "hw/qdev-properties.h" |
fa1d36df | 29 | #include "sysemu/block-backend.h" |
9c17d615 | 30 | #include "sysemu/blockdev.h" |
0d09e41a | 31 | #include "hw/block/block.h" |
9c17d615 | 32 | #include "sysemu/sysemu.h" |
45563630 | 33 | #include "qapi/visitor.h" |
da4d0419 GH |
34 | |
35 | /* --------------------------------- */ | |
36 | ||
dc1a46b6 | 37 | static char *idebus_get_fw_dev_path(DeviceState *dev); |
44a109c1 | 38 | static void idebus_unrealize(BusState *qdev, Error **errp); |
dc1a46b6 | 39 | |
3cb75a7c PB |
40 | static Property ide_props[] = { |
41 | DEFINE_PROP_UINT32("unit", IDEDevice, unit, -1), | |
42 | DEFINE_PROP_END_OF_LIST(), | |
43 | }; | |
44 | ||
0d936928 AL |
45 | static void ide_bus_class_init(ObjectClass *klass, void *data) |
46 | { | |
47 | BusClass *k = BUS_CLASS(klass); | |
48 | ||
49 | k->get_fw_dev_path = idebus_get_fw_dev_path; | |
44a109c1 | 50 | k->unrealize = idebus_unrealize; |
0d936928 AL |
51 | } |
52 | ||
44a109c1 | 53 | static void idebus_unrealize(BusState *bus, Error **errp) |
ca44141d | 54 | { |
44a109c1 | 55 | IDEBus *ibus = IDE_BUS(bus); |
ca44141d | 56 | |
44a109c1 LQ |
57 | if (ibus->vmstate) { |
58 | qemu_del_vm_change_state_handler(ibus->vmstate); | |
ca44141d AA |
59 | } |
60 | } | |
61 | ||
0d936928 AL |
62 | static const TypeInfo ide_bus_info = { |
63 | .name = TYPE_IDE_BUS, | |
64 | .parent = TYPE_BUS, | |
65 | .instance_size = sizeof(IDEBus), | |
66 | .class_init = ide_bus_class_init, | |
da4d0419 GH |
67 | }; |
68 | ||
c6baf942 AF |
69 | void ide_bus_new(IDEBus *idebus, size_t idebus_size, DeviceState *dev, |
70 | int bus_id, int max_units) | |
da4d0419 | 71 | { |
fb17dfe0 | 72 | qbus_create_inplace(idebus, idebus_size, TYPE_IDE_BUS, dev, NULL); |
3835510f | 73 | idebus->bus_id = bus_id; |
0ee20e66 | 74 | idebus->max_units = max_units; |
da4d0419 GH |
75 | } |
76 | ||
dc1a46b6 GN |
77 | static char *idebus_get_fw_dev_path(DeviceState *dev) |
78 | { | |
79 | char path[30]; | |
80 | ||
28fa7133 | 81 | snprintf(path, sizeof(path), "%s@%x", qdev_fw_name(dev), |
dc1a46b6 GN |
82 | ((IDEBus*)dev->parent_bus)->bus_id); |
83 | ||
a5cf8262 | 84 | return g_strdup(path); |
dc1a46b6 GN |
85 | } |
86 | ||
794939e8 | 87 | static void ide_qdev_realize(DeviceState *qdev, Error **errp) |
da4d0419 | 88 | { |
d148211c AL |
89 | IDEDevice *dev = IDE_DEVICE(qdev); |
90 | IDEDeviceClass *dc = IDE_DEVICE_GET_CLASS(dev); | |
da4d0419 GH |
91 | IDEBus *bus = DO_UPCAST(IDEBus, qbus, qdev->parent_bus); |
92 | ||
da4d0419 GH |
93 | if (dev->unit == -1) { |
94 | dev->unit = bus->master ? 1 : 0; | |
95 | } | |
0ee20e66 KW |
96 | |
97 | if (dev->unit >= bus->max_units) { | |
794939e8 | 98 | error_setg(errp, "Can't create IDE unit %d, bus supports only %d units", |
0ee20e66 | 99 | dev->unit, bus->max_units); |
794939e8 | 100 | return; |
0ee20e66 KW |
101 | } |
102 | ||
da4d0419 GH |
103 | switch (dev->unit) { |
104 | case 0: | |
105 | if (bus->master) { | |
794939e8 MZ |
106 | error_setg(errp, "IDE unit %d is in use", dev->unit); |
107 | return; | |
da4d0419 GH |
108 | } |
109 | bus->master = dev; | |
110 | break; | |
111 | case 1: | |
112 | if (bus->slave) { | |
794939e8 MZ |
113 | error_setg(errp, "IDE unit %d is in use", dev->unit); |
114 | return; | |
da4d0419 GH |
115 | } |
116 | bus->slave = dev; | |
117 | break; | |
118 | default: | |
794939e8 MZ |
119 | error_setg(errp, "Invalid IDE unit %d", dev->unit); |
120 | return; | |
da4d0419 | 121 | } |
794939e8 | 122 | dc->realize(dev, errp); |
da4d0419 GH |
123 | } |
124 | ||
da4d0419 GH |
125 | IDEDevice *ide_create_drive(IDEBus *bus, int unit, DriveInfo *drive) |
126 | { | |
127 | DeviceState *dev; | |
128 | ||
95b5edcd | 129 | dev = qdev_create(&bus->qbus, drive->media_cd ? "ide-cd" : "ide-hd"); |
da4d0419 | 130 | qdev_prop_set_uint32(dev, "unit", unit); |
6231a6da MA |
131 | qdev_prop_set_drive(dev, "drive", blk_by_legacy_dinfo(drive), |
132 | &error_fatal); | |
fa12fbbe | 133 | qdev_init_nofail(dev); |
da4d0419 GH |
134 | return DO_UPCAST(IDEDevice, qdev, dev); |
135 | } | |
136 | ||
9139046c MA |
137 | int ide_get_geometry(BusState *bus, int unit, |
138 | int16_t *cyls, int8_t *heads, int8_t *secs) | |
c0897e0c | 139 | { |
9139046c MA |
140 | IDEState *s = &DO_UPCAST(IDEBus, qbus, bus)->ifs[unit]; |
141 | ||
4be74634 | 142 | if (s->drive_kind != IDE_HD || !s->blk) { |
9139046c MA |
143 | return -1; |
144 | } | |
145 | ||
146 | *cyls = s->cylinders; | |
147 | *heads = s->heads; | |
148 | *secs = s->sectors; | |
149 | return 0; | |
150 | } | |
151 | ||
152 | int ide_get_bios_chs_trans(BusState *bus, int unit) | |
153 | { | |
154 | return DO_UPCAST(IDEBus, qbus, bus)->ifs[unit].chs_trans; | |
c0897e0c MA |
155 | } |
156 | ||
da4d0419 GH |
157 | /* --------------------------------- */ |
158 | ||
159 | typedef struct IDEDrive { | |
160 | IDEDevice dev; | |
161 | } IDEDrive; | |
162 | ||
794939e8 | 163 | static void ide_dev_initfn(IDEDevice *dev, IDEDriveKind kind, Error **errp) |
da4d0419 GH |
164 | { |
165 | IDEBus *bus = DO_UPCAST(IDEBus, qbus, dev->qdev.parent_bus); | |
6ced55a5 | 166 | IDEState *s = bus->ifs + dev->unit; |
947231ad | 167 | int ret; |
6ced55a5 | 168 | |
67c75f3d KW |
169 | if (!dev->conf.blk) { |
170 | if (kind != IDE_CD) { | |
794939e8 MZ |
171 | error_setg(errp, "No drive specified"); |
172 | return; | |
67c75f3d KW |
173 | } else { |
174 | /* Anonymous BlockBackend for an empty drive */ | |
d861ab3a | 175 | dev->conf.blk = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL); |
947231ad KW |
176 | ret = blk_attach_dev(dev->conf.blk, &dev->qdev); |
177 | assert(ret == 0); | |
67c75f3d KW |
178 | } |
179 | } | |
180 | ||
215e47b9 PB |
181 | if (dev->conf.discard_granularity == -1) { |
182 | dev->conf.discard_granularity = 512; | |
183 | } else if (dev->conf.discard_granularity && | |
184 | dev->conf.discard_granularity != 512) { | |
794939e8 MZ |
185 | error_setg(errp, "discard_granularity must be 512 for ide"); |
186 | return; | |
d353fb72 CH |
187 | } |
188 | ||
0eb28a42 | 189 | blkconf_blocksizes(&dev->conf); |
d2005185 | 190 | if (dev->conf.logical_block_size != 512) { |
794939e8 MZ |
191 | error_setg(errp, "logical_block_size must be 512 for IDE"); |
192 | return; | |
d2005185 KW |
193 | } |
194 | ||
5ff5efb4 | 195 | if (kind != IDE_CD) { |
ceff3e1f MZ |
196 | if (!blkconf_geometry(&dev->conf, &dev->chs_trans, 65535, 16, 255, |
197 | errp)) { | |
794939e8 | 198 | return; |
5ff5efb4 | 199 | } |
ba801960 | 200 | } |
ceff3e1f MZ |
201 | if (!blkconf_apply_backend_options(&dev->conf, kind == IDE_CD, |
202 | kind != IDE_CD, errp)) { | |
794939e8 | 203 | return; |
a17c17a2 | 204 | } |
ba801960 | 205 | |
4be74634 | 206 | if (ide_init_drive(s, dev->conf.blk, kind, |
911525db | 207 | dev->version, dev->serial, dev->model, dev->wwn, |
ba801960 | 208 | dev->conf.cyls, dev->conf.heads, dev->conf.secs, |
794939e8 MZ |
209 | dev->chs_trans, errp) < 0) { |
210 | return; | |
c4d74df7 | 211 | } |
6ced55a5 | 212 | |
03432407 | 213 | if (!dev->version) { |
7267c094 | 214 | dev->version = g_strdup(s->version); |
03432407 | 215 | } |
6ced55a5 | 216 | if (!dev->serial) { |
7267c094 | 217 | dev->serial = g_strdup(s->drive_serial_str); |
6ced55a5 | 218 | } |
1ca4d09a GN |
219 | |
220 | add_boot_device_path(dev->conf.bootindex, &dev->qdev, | |
221 | dev->unit ? "/disk@1" : "/disk@0"); | |
da4d0419 GH |
222 | } |
223 | ||
d7bce999 EB |
224 | static void ide_dev_get_bootindex(Object *obj, Visitor *v, const char *name, |
225 | void *opaque, Error **errp) | |
45563630 GA |
226 | { |
227 | IDEDevice *d = IDE_DEVICE(obj); | |
228 | ||
51e72bc1 | 229 | visit_type_int32(v, name, &d->conf.bootindex, errp); |
45563630 GA |
230 | } |
231 | ||
d7bce999 EB |
232 | static void ide_dev_set_bootindex(Object *obj, Visitor *v, const char *name, |
233 | void *opaque, Error **errp) | |
45563630 GA |
234 | { |
235 | IDEDevice *d = IDE_DEVICE(obj); | |
236 | int32_t boot_index; | |
237 | Error *local_err = NULL; | |
238 | ||
51e72bc1 | 239 | visit_type_int32(v, name, &boot_index, &local_err); |
45563630 GA |
240 | if (local_err) { |
241 | goto out; | |
242 | } | |
243 | /* check whether bootindex is present in fw_boot_order list */ | |
244 | check_boot_index(boot_index, &local_err); | |
245 | if (local_err) { | |
246 | goto out; | |
247 | } | |
248 | /* change bootindex to a new one */ | |
249 | d->conf.bootindex = boot_index; | |
250 | ||
d2b186f9 GA |
251 | if (d->unit != -1) { |
252 | add_boot_device_path(d->conf.bootindex, &d->qdev, | |
253 | d->unit ? "/disk@1" : "/disk@0"); | |
254 | } | |
45563630 | 255 | out: |
621ff94d | 256 | error_propagate(errp, local_err); |
45563630 GA |
257 | } |
258 | ||
259 | static void ide_dev_instance_init(Object *obj) | |
260 | { | |
261 | object_property_add(obj, "bootindex", "int32", | |
262 | ide_dev_get_bootindex, | |
263 | ide_dev_set_bootindex, NULL, NULL, NULL); | |
d2b186f9 | 264 | object_property_set_int(obj, -1, "bootindex", NULL); |
45563630 GA |
265 | } |
266 | ||
794939e8 | 267 | static void ide_hd_realize(IDEDevice *dev, Error **errp) |
1f56e32a | 268 | { |
794939e8 | 269 | ide_dev_initfn(dev, IDE_HD, errp); |
1f56e32a MA |
270 | } |
271 | ||
794939e8 | 272 | static void ide_cd_realize(IDEDevice *dev, Error **errp) |
1f56e32a | 273 | { |
794939e8 | 274 | ide_dev_initfn(dev, IDE_CD, errp); |
1f56e32a MA |
275 | } |
276 | ||
794939e8 | 277 | static void ide_drive_realize(IDEDevice *dev, Error **errp) |
1f56e32a | 278 | { |
67c75f3d KW |
279 | DriveInfo *dinfo = NULL; |
280 | ||
281 | if (dev->conf.blk) { | |
282 | dinfo = blk_legacy_dinfo(dev->conf.blk); | |
283 | } | |
95b5edcd | 284 | |
794939e8 | 285 | ide_dev_initfn(dev, dinfo && dinfo->media_cd ? IDE_CD : IDE_HD, errp); |
1f56e32a MA |
286 | } |
287 | ||
288 | #define DEFINE_IDE_DEV_PROPERTIES() \ | |
1f56e32a | 289 | DEFINE_BLOCK_PROPERTIES(IDEDrive, dev.conf), \ |
8c398252 | 290 | DEFINE_BLOCK_ERROR_PROPERTIES(IDEDrive, dev.conf), \ |
1f56e32a | 291 | DEFINE_PROP_STRING("ver", IDEDrive, dev.version), \ |
c7bcc85d | 292 | DEFINE_PROP_UINT64("wwn", IDEDrive, dev.wwn, 0), \ |
27e0c9a1 FB |
293 | DEFINE_PROP_STRING("serial", IDEDrive, dev.serial),\ |
294 | DEFINE_PROP_STRING("model", IDEDrive, dev.model) | |
1f56e32a | 295 | |
39bffca2 AL |
296 | static Property ide_hd_properties[] = { |
297 | DEFINE_IDE_DEV_PROPERTIES(), | |
ba801960 | 298 | DEFINE_BLOCK_CHS_PROPERTIES(IDEDrive, dev.conf), |
6e6f61a6 MA |
299 | DEFINE_PROP_BIOS_CHS_TRANS("bios-chs-trans", |
300 | IDEDrive, dev.chs_trans, BIOS_ATA_TRANSLATION_AUTO), | |
3b19f450 | 301 | DEFINE_PROP_UINT16("rotation_rate", IDEDrive, dev.rotation_rate, 0), |
39bffca2 AL |
302 | DEFINE_PROP_END_OF_LIST(), |
303 | }; | |
304 | ||
d148211c AL |
305 | static void ide_hd_class_init(ObjectClass *klass, void *data) |
306 | { | |
39bffca2 | 307 | DeviceClass *dc = DEVICE_CLASS(klass); |
d148211c | 308 | IDEDeviceClass *k = IDE_DEVICE_CLASS(klass); |
794939e8 MZ |
309 | |
310 | k->realize = ide_hd_realize; | |
39bffca2 | 311 | dc->fw_name = "drive"; |
794939e8 MZ |
312 | dc->desc = "virtual IDE disk"; |
313 | dc->props = ide_hd_properties; | |
d148211c AL |
314 | } |
315 | ||
8c43a6f0 | 316 | static const TypeInfo ide_hd_info = { |
39bffca2 AL |
317 | .name = "ide-hd", |
318 | .parent = TYPE_IDE_DEVICE, | |
319 | .instance_size = sizeof(IDEDrive), | |
320 | .class_init = ide_hd_class_init, | |
321 | }; | |
322 | ||
323 | static Property ide_cd_properties[] = { | |
324 | DEFINE_IDE_DEV_PROPERTIES(), | |
325 | DEFINE_PROP_END_OF_LIST(), | |
da4d0419 GH |
326 | }; |
327 | ||
d148211c | 328 | static void ide_cd_class_init(ObjectClass *klass, void *data) |
da4d0419 | 329 | { |
39bffca2 | 330 | DeviceClass *dc = DEVICE_CLASS(klass); |
d148211c | 331 | IDEDeviceClass *k = IDE_DEVICE_CLASS(klass); |
794939e8 MZ |
332 | |
333 | k->realize = ide_cd_realize; | |
39bffca2 | 334 | dc->fw_name = "drive"; |
794939e8 MZ |
335 | dc->desc = "virtual IDE CD-ROM"; |
336 | dc->props = ide_cd_properties; | |
d148211c | 337 | } |
1f56e32a | 338 | |
8c43a6f0 | 339 | static const TypeInfo ide_cd_info = { |
39bffca2 AL |
340 | .name = "ide-cd", |
341 | .parent = TYPE_IDE_DEVICE, | |
342 | .instance_size = sizeof(IDEDrive), | |
343 | .class_init = ide_cd_class_init, | |
344 | }; | |
345 | ||
346 | static Property ide_drive_properties[] = { | |
347 | DEFINE_IDE_DEV_PROPERTIES(), | |
348 | DEFINE_PROP_END_OF_LIST(), | |
d148211c AL |
349 | }; |
350 | ||
351 | static void ide_drive_class_init(ObjectClass *klass, void *data) | |
352 | { | |
39bffca2 | 353 | DeviceClass *dc = DEVICE_CLASS(klass); |
d148211c | 354 | IDEDeviceClass *k = IDE_DEVICE_CLASS(klass); |
794939e8 MZ |
355 | |
356 | k->realize = ide_drive_realize; | |
39bffca2 | 357 | dc->fw_name = "drive"; |
794939e8 MZ |
358 | dc->desc = "virtual IDE disk or CD-ROM (legacy)"; |
359 | dc->props = ide_drive_properties; | |
d148211c AL |
360 | } |
361 | ||
8c43a6f0 | 362 | static const TypeInfo ide_drive_info = { |
39bffca2 AL |
363 | .name = "ide-drive", |
364 | .parent = TYPE_IDE_DEVICE, | |
365 | .instance_size = sizeof(IDEDrive), | |
366 | .class_init = ide_drive_class_init, | |
d148211c AL |
367 | }; |
368 | ||
39bffca2 AL |
369 | static void ide_device_class_init(ObjectClass *klass, void *data) |
370 | { | |
371 | DeviceClass *k = DEVICE_CLASS(klass); | |
794939e8 | 372 | k->realize = ide_qdev_realize; |
125ee0ed | 373 | set_bit(DEVICE_CATEGORY_STORAGE, k->categories); |
0d936928 | 374 | k->bus_type = TYPE_IDE_BUS; |
bce54474 | 375 | k->props = ide_props; |
39bffca2 AL |
376 | } |
377 | ||
8c43a6f0 | 378 | static const TypeInfo ide_device_type_info = { |
d148211c AL |
379 | .name = TYPE_IDE_DEVICE, |
380 | .parent = TYPE_DEVICE, | |
381 | .instance_size = sizeof(IDEDevice), | |
382 | .abstract = true, | |
383 | .class_size = sizeof(IDEDeviceClass), | |
39bffca2 | 384 | .class_init = ide_device_class_init, |
45563630 | 385 | .instance_init = ide_dev_instance_init, |
d148211c AL |
386 | }; |
387 | ||
83f7d43a | 388 | static void ide_register_types(void) |
d148211c | 389 | { |
0d936928 | 390 | type_register_static(&ide_bus_info); |
39bffca2 AL |
391 | type_register_static(&ide_hd_info); |
392 | type_register_static(&ide_cd_info); | |
393 | type_register_static(&ide_drive_info); | |
d148211c | 394 | type_register_static(&ide_device_type_info); |
da4d0419 | 395 | } |
83f7d43a AF |
396 | |
397 | type_init(ide_register_types) |