virtio-blk.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * VMApple specific VirtIO Block implementation
  3. *
  4. * Copyright © 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  5. *
  6. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  7. * See the COPYING file in the top-level directory.
  8. *
  9. * SPDX-License-Identifier: GPL-2.0-or-later
  10. *
  11. * VMApple uses almost standard VirtIO Block, but with a few key differences:
  12. *
  13. * - Different PCI device/vendor ID
  14. * - An additional "type" identifier to differentiate AUX and Root volumes
  15. * - An additional BARRIER command
  16. */
  17. #include "qemu/osdep.h"
  18. #include "hw/vmapple/vmapple.h"
  19. #include "hw/virtio/virtio-blk.h"
  20. #include "hw/virtio/virtio-pci.h"
  21. #include "qemu/bswap.h"
  22. #include "qemu/log.h"
  23. #include "qemu/module.h"
  24. #include "qapi/error.h"
  25. #define TYPE_VMAPPLE_VIRTIO_BLK "vmapple-virtio-blk"
  26. OBJECT_DECLARE_TYPE(VMAppleVirtIOBlk, VMAppleVirtIOBlkClass, VMAPPLE_VIRTIO_BLK)
  27. typedef struct VMAppleVirtIOBlkClass {
  28. VirtIOBlkClass parent;
  29. void (*get_config)(VirtIODevice *vdev, uint8_t *config);
  30. } VMAppleVirtIOBlkClass;
  31. typedef struct VMAppleVirtIOBlk {
  32. VirtIOBlock parent_obj;
  33. uint32_t apple_type;
  34. } VMAppleVirtIOBlk;
  35. /*
  36. * vmapple-virtio-blk-pci: This extends VirtioPCIProxy.
  37. */
  38. OBJECT_DECLARE_SIMPLE_TYPE(VMAppleVirtIOBlkPCI, VMAPPLE_VIRTIO_BLK_PCI)
  39. #define VIRTIO_BLK_T_APPLE_BARRIER 0x10000
  40. static bool vmapple_virtio_blk_handle_unknown_request(VirtIOBlockReq *req,
  41. MultiReqBuffer *mrb,
  42. uint32_t type)
  43. {
  44. switch (type) {
  45. case VIRTIO_BLK_T_APPLE_BARRIER:
  46. qemu_log_mask(LOG_UNIMP, "%s: Barrier requests are currently no-ops\n",
  47. __func__);
  48. virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
  49. g_free(req);
  50. return true;
  51. default:
  52. return false;
  53. }
  54. }
  55. /*
  56. * VMApple virtio-blk uses the same config format as normal virtio, with one
  57. * exception: It adds an "apple type" specififer at the same location that
  58. * the spec reserves for max_secure_erase_sectors. Let's hook into the
  59. * get_config code path here, run it as usual and then patch in the apple type.
  60. */
  61. static void vmapple_virtio_blk_get_config(VirtIODevice *vdev, uint8_t *config)
  62. {
  63. VMAppleVirtIOBlk *dev = VMAPPLE_VIRTIO_BLK(vdev);
  64. VMAppleVirtIOBlkClass *vvbk = VMAPPLE_VIRTIO_BLK_GET_CLASS(dev);
  65. struct virtio_blk_config *blkcfg = (struct virtio_blk_config *)config;
  66. vvbk->get_config(vdev, config);
  67. g_assert(dev->parent_obj.config_size >= endof(struct virtio_blk_config, zoned));
  68. /* Apple abuses the field for max_secure_erase_sectors as type id */
  69. stl_he_p(&blkcfg->max_secure_erase_sectors, dev->apple_type);
  70. }
  71. static void vmapple_virtio_blk_class_init(ObjectClass *klass, void *data)
  72. {
  73. VirtIOBlkClass *vbk = VIRTIO_BLK_CLASS(klass);
  74. VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
  75. VMAppleVirtIOBlkClass *vvbk = VMAPPLE_VIRTIO_BLK_CLASS(klass);
  76. vbk->handle_unknown_request = vmapple_virtio_blk_handle_unknown_request;
  77. vvbk->get_config = vdc->get_config;
  78. vdc->get_config = vmapple_virtio_blk_get_config;
  79. }
  80. static const TypeInfo vmapple_virtio_blk_info = {
  81. .name = TYPE_VMAPPLE_VIRTIO_BLK,
  82. .parent = TYPE_VIRTIO_BLK,
  83. .instance_size = sizeof(VMAppleVirtIOBlk),
  84. .class_size = sizeof(VMAppleVirtIOBlkClass),
  85. .class_init = vmapple_virtio_blk_class_init,
  86. };
  87. /* PCI Devices */
  88. struct VMAppleVirtIOBlkPCI {
  89. VirtIOPCIProxy parent_obj;
  90. VMAppleVirtIOBlk vdev;
  91. VMAppleVirtioBlkVariant variant;
  92. };
  93. static const Property vmapple_virtio_blk_pci_properties[] = {
  94. DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
  95. DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
  96. VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
  97. DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
  98. DEV_NVECTORS_UNSPECIFIED),
  99. DEFINE_PROP_VMAPPLE_VIRTIO_BLK_VARIANT("variant", VMAppleVirtIOBlkPCI, variant,
  100. VM_APPLE_VIRTIO_BLK_VARIANT_UNSPECIFIED),
  101. };
  102. static void vmapple_virtio_blk_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
  103. {
  104. ERRP_GUARD();
  105. VMAppleVirtIOBlkPCI *dev = VMAPPLE_VIRTIO_BLK_PCI(vpci_dev);
  106. DeviceState *vdev = DEVICE(&dev->vdev);
  107. VirtIOBlkConf *conf = &dev->vdev.parent_obj.conf;
  108. if (dev->variant == VM_APPLE_VIRTIO_BLK_VARIANT_UNSPECIFIED) {
  109. error_setg(errp, "vmapple virtio block device variant unspecified");
  110. error_append_hint(errp,
  111. "Variant property must be set to 'aux' or 'root'.\n"
  112. "Use a regular virtio-blk-pci device instead when "
  113. "neither is applicaple.\n");
  114. return;
  115. }
  116. if (conf->num_queues == VIRTIO_BLK_AUTO_NUM_QUEUES) {
  117. conf->num_queues = virtio_pci_optimal_num_queues(0);
  118. }
  119. if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
  120. vpci_dev->nvectors = conf->num_queues + 1;
  121. }
  122. /*
  123. * We don't support zones, but we need the additional config space size.
  124. * Let's just expose the feature so the rest of the virtio-blk logic
  125. * allocates enough space for us. The guest will ignore zones anyway.
  126. */
  127. virtio_add_feature(&dev->vdev.parent_obj.host_features, VIRTIO_BLK_F_ZONED);
  128. /* Propagate the apple type down to the virtio-blk device */
  129. dev->vdev.apple_type = dev->variant;
  130. /* and spawn the virtio-blk device */
  131. qdev_realize(vdev, BUS(&vpci_dev->bus), errp);
  132. /*
  133. * The virtio-pci machinery adjusts its vendor/device ID based on whether
  134. * we support modern or legacy virtio. Let's patch it back to the Apple
  135. * identifiers here.
  136. */
  137. pci_config_set_vendor_id(vpci_dev->pci_dev.config, PCI_VENDOR_ID_APPLE);
  138. pci_config_set_device_id(vpci_dev->pci_dev.config,
  139. PCI_DEVICE_ID_APPLE_VIRTIO_BLK);
  140. }
  141. static void vmapple_virtio_blk_pci_class_init(ObjectClass *klass, void *data)
  142. {
  143. DeviceClass *dc = DEVICE_CLASS(klass);
  144. VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
  145. PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
  146. set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
  147. device_class_set_props(dc, vmapple_virtio_blk_pci_properties);
  148. k->realize = vmapple_virtio_blk_pci_realize;
  149. pcidev_k->vendor_id = PCI_VENDOR_ID_APPLE;
  150. pcidev_k->device_id = PCI_DEVICE_ID_APPLE_VIRTIO_BLK;
  151. pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
  152. pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
  153. }
  154. static void vmapple_virtio_blk_pci_instance_init(Object *obj)
  155. {
  156. VMAppleVirtIOBlkPCI *dev = VMAPPLE_VIRTIO_BLK_PCI(obj);
  157. virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
  158. TYPE_VMAPPLE_VIRTIO_BLK);
  159. }
  160. static const VirtioPCIDeviceTypeInfo vmapple_virtio_blk_pci_info = {
  161. .generic_name = TYPE_VMAPPLE_VIRTIO_BLK_PCI,
  162. .instance_size = sizeof(VMAppleVirtIOBlkPCI),
  163. .instance_init = vmapple_virtio_blk_pci_instance_init,
  164. .class_init = vmapple_virtio_blk_pci_class_init,
  165. };
  166. static void vmapple_virtio_blk_register_types(void)
  167. {
  168. type_register_static(&vmapple_virtio_blk_info);
  169. virtio_pci_types_register(&vmapple_virtio_blk_pci_info);
  170. }
  171. type_init(vmapple_virtio_blk_register_types)