qdev.c 11 KB

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