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