2
0

ide-dev.c 7.7 KB

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