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