qdev.c 11 KB

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