xref: /openbmc/qemu/hw/ide/ide-dev.c (revision 33a24910)
1 /*
2  * IDE device functions
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 "qapi/error.h"
22 #include "qapi/qapi-types-block.h"
23 #include "qemu/error-report.h"
24 #include "qemu/module.h"
25 #include "hw/ide/ide-dev.h"
26 #include "sysemu/block-backend.h"
27 #include "sysemu/blockdev.h"
28 #include "sysemu/sysemu.h"
29 #include "qapi/visitor.h"
30 #include "ide-internal.h"
31 
32 static Property ide_props[] = {
33     DEFINE_PROP_UINT32("unit", IDEDevice, unit, -1),
34     DEFINE_PROP_END_OF_LIST(),
35 };
36 
37 static void ide_qdev_realize(DeviceState *qdev, Error **errp)
38 {
39     IDEDevice *dev = IDE_DEVICE(qdev);
40     IDEDeviceClass *dc = IDE_DEVICE_GET_CLASS(dev);
41     IDEBus *bus = DO_UPCAST(IDEBus, qbus, qdev->parent_bus);
42 
43     if (dev->unit == -1) {
44         dev->unit = bus->master ? 1 : 0;
45     }
46 
47     if (dev->unit >= bus->max_units) {
48         error_setg(errp, "Can't create IDE unit %d, bus supports only %d units",
49                      dev->unit, bus->max_units);
50         return;
51     }
52 
53     switch (dev->unit) {
54     case 0:
55         if (bus->master) {
56             error_setg(errp, "IDE unit %d is in use", dev->unit);
57             return;
58         }
59         bus->master = dev;
60         break;
61     case 1:
62         if (bus->slave) {
63             error_setg(errp, "IDE unit %d is in use", dev->unit);
64             return;
65         }
66         bus->slave = dev;
67         break;
68     default:
69         error_setg(errp, "Invalid IDE unit %d", dev->unit);
70         return;
71     }
72     dc->realize(dev, errp);
73 }
74 
75 void ide_dev_initfn(IDEDevice *dev, IDEDriveKind kind, Error **errp)
76 {
77     IDEBus *bus = DO_UPCAST(IDEBus, qbus, dev->qdev.parent_bus);
78     IDEState *s = bus->ifs + dev->unit;
79     int ret;
80 
81     if (!dev->conf.blk) {
82         if (kind != IDE_CD) {
83             error_setg(errp, "No drive specified");
84             return;
85         } else {
86             /* Anonymous BlockBackend for an empty drive */
87             dev->conf.blk = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL);
88             ret = blk_attach_dev(dev->conf.blk, &dev->qdev);
89             assert(ret == 0);
90         }
91     }
92 
93     if (dev->conf.discard_granularity == -1) {
94         dev->conf.discard_granularity = 512;
95     } else if (dev->conf.discard_granularity &&
96                dev->conf.discard_granularity != 512) {
97         error_setg(errp, "discard_granularity must be 512 for ide");
98         return;
99     }
100 
101     if (!blkconf_blocksizes(&dev->conf, errp)) {
102         return;
103     }
104 
105     if (dev->conf.logical_block_size != 512) {
106         error_setg(errp, "logical_block_size must be 512 for IDE");
107         return;
108     }
109 
110     if (kind != IDE_CD) {
111         if (!blkconf_geometry(&dev->conf, &dev->chs_trans, 65535, 16, 255,
112                               errp)) {
113             return;
114         }
115     }
116     if (!blkconf_apply_backend_options(&dev->conf, kind == IDE_CD,
117                                        kind != IDE_CD, errp)) {
118         return;
119     }
120 
121     if (ide_init_drive(s, dev->conf.blk, kind,
122                        dev->version, dev->serial, dev->model, dev->wwn,
123                        dev->conf.cyls, dev->conf.heads, dev->conf.secs,
124                        dev->chs_trans, errp) < 0) {
125         return;
126     }
127 
128     if (!dev->version) {
129         dev->version = g_strdup(s->version);
130     }
131     if (!dev->serial) {
132         dev->serial = g_strdup(s->drive_serial_str);
133     }
134 
135     add_boot_device_path(dev->conf.bootindex, &dev->qdev,
136                          dev->unit ? "/disk@1" : "/disk@0");
137 
138     add_boot_device_lchs(&dev->qdev, dev->unit ? "/disk@1" : "/disk@0",
139                          dev->conf.lcyls,
140                          dev->conf.lheads,
141                          dev->conf.lsecs);
142 }
143 
144 static void ide_dev_get_bootindex(Object *obj, Visitor *v, const char *name,
145                                   void *opaque, Error **errp)
146 {
147     IDEDevice *d = IDE_DEVICE(obj);
148 
149     visit_type_int32(v, name, &d->conf.bootindex, errp);
150 }
151 
152 static void ide_dev_set_bootindex(Object *obj, Visitor *v, const char *name,
153                                   void *opaque, Error **errp)
154 {
155     IDEDevice *d = IDE_DEVICE(obj);
156     int32_t boot_index;
157     Error *local_err = NULL;
158 
159     if (!visit_type_int32(v, name, &boot_index, errp)) {
160         return;
161     }
162     /* check whether bootindex is present in fw_boot_order list  */
163     check_boot_index(boot_index, &local_err);
164     if (local_err) {
165         goto out;
166     }
167     /* change bootindex to a new one */
168     d->conf.bootindex = boot_index;
169 
170     if (d->unit != -1) {
171         add_boot_device_path(d->conf.bootindex, &d->qdev,
172                              d->unit ? "/disk@1" : "/disk@0");
173     }
174 out:
175     error_propagate(errp, local_err);
176 }
177 
178 static void ide_dev_instance_init(Object *obj)
179 {
180     object_property_add(obj, "bootindex", "int32",
181                         ide_dev_get_bootindex,
182                         ide_dev_set_bootindex, NULL, NULL);
183     object_property_set_int(obj, "bootindex", -1, NULL);
184 }
185 
186 static void ide_hd_realize(IDEDevice *dev, Error **errp)
187 {
188     ide_dev_initfn(dev, IDE_HD, errp);
189 }
190 
191 static void ide_cd_realize(IDEDevice *dev, Error **errp)
192 {
193     ide_dev_initfn(dev, IDE_CD, errp);
194 }
195 
196 static Property ide_hd_properties[] = {
197     DEFINE_IDE_DEV_PROPERTIES(),
198     DEFINE_BLOCK_CHS_PROPERTIES(IDEDrive, dev.conf),
199     DEFINE_PROP_BIOS_CHS_TRANS("bios-chs-trans",
200                 IDEDrive, dev.chs_trans, BIOS_ATA_TRANSLATION_AUTO),
201     DEFINE_PROP_UINT16("rotation_rate", IDEDrive, dev.rotation_rate, 0),
202     DEFINE_PROP_END_OF_LIST(),
203 };
204 
205 static void ide_hd_class_init(ObjectClass *klass, void *data)
206 {
207     DeviceClass *dc = DEVICE_CLASS(klass);
208     IDEDeviceClass *k = IDE_DEVICE_CLASS(klass);
209 
210     k->realize  = ide_hd_realize;
211     dc->fw_name = "drive";
212     dc->desc    = "virtual IDE disk";
213     device_class_set_props(dc, ide_hd_properties);
214 }
215 
216 static const TypeInfo ide_hd_info = {
217     .name          = "ide-hd",
218     .parent        = TYPE_IDE_DEVICE,
219     .instance_size = sizeof(IDEDrive),
220     .class_init    = ide_hd_class_init,
221 };
222 
223 static Property ide_cd_properties[] = {
224     DEFINE_IDE_DEV_PROPERTIES(),
225     DEFINE_PROP_END_OF_LIST(),
226 };
227 
228 static void ide_cd_class_init(ObjectClass *klass, void *data)
229 {
230     DeviceClass *dc = DEVICE_CLASS(klass);
231     IDEDeviceClass *k = IDE_DEVICE_CLASS(klass);
232 
233     k->realize  = ide_cd_realize;
234     dc->fw_name = "drive";
235     dc->desc    = "virtual IDE CD-ROM";
236     device_class_set_props(dc, ide_cd_properties);
237 }
238 
239 static const TypeInfo ide_cd_info = {
240     .name          = "ide-cd",
241     .parent        = TYPE_IDE_DEVICE,
242     .instance_size = sizeof(IDEDrive),
243     .class_init    = ide_cd_class_init,
244 };
245 
246 static void ide_device_class_init(ObjectClass *klass, void *data)
247 {
248     DeviceClass *k = DEVICE_CLASS(klass);
249     k->realize = ide_qdev_realize;
250     set_bit(DEVICE_CATEGORY_STORAGE, k->categories);
251     k->bus_type = TYPE_IDE_BUS;
252     device_class_set_props(k, ide_props);
253 }
254 
255 static const TypeInfo ide_device_type_info = {
256     .name = TYPE_IDE_DEVICE,
257     .parent = TYPE_DEVICE,
258     .instance_size = sizeof(IDEDevice),
259     .abstract = true,
260     .class_size = sizeof(IDEDeviceClass),
261     .class_init = ide_device_class_init,
262     .instance_init = ide_dev_instance_init,
263 };
264 
265 static void ide_register_types(void)
266 {
267     type_register_static(&ide_hd_info);
268     type_register_static(&ide_cd_info);
269     type_register_static(&ide_device_type_info);
270 }
271 
272 type_init(ide_register_types)
273