Skip to content

Commit ee241d7

Browse files
agrafphilmd
authored andcommitted
hw/vmapple/virtio-blk: Add support for apple virtio-blk
Apple has its own virtio-blk PCI device ID where it deviates from the official virtio-pci spec slightly: It puts a new "apple type" field at a static offset in config space and introduces a new barrier command. This patch first creates a mechanism for virtio-blk downstream classes to handle unknown commands. It then creates such a downstream class and a new vmapple-virtio-blk-pci class which support the additional apple type config identifier as well as the barrier command. The 'aux' or 'root' device type are selected using the 'variant' property. Signed-off-by: Alexander Graf <[email protected]> Signed-off-by: Phil Dennis-Jordan <[email protected]> Reviewed-by: Akihiko Odaki <[email protected]> Tested-by: Akihiko Odaki <[email protected]> Reviewed-by: Michael S. Tsirkin <[email protected]> Message-ID: <[email protected]> Signed-off-by: Philippe Mathieu-Daudé <[email protected]>
1 parent 33b5446 commit ee241d7

File tree

10 files changed

+264
-4
lines changed

10 files changed

+264
-4
lines changed

hw/block/virtio-blk.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ static void virtio_blk_init_request(VirtIOBlock *s, VirtQueue *vq,
5050
req->mr_next = NULL;
5151
}
5252

53-
static void virtio_blk_req_complete(VirtIOBlockReq *req, unsigned char status)
53+
void virtio_blk_req_complete(VirtIOBlockReq *req, unsigned char status)
5454
{
5555
VirtIOBlock *s = req->dev;
5656
VirtIODevice *vdev = VIRTIO_DEVICE(s);
@@ -961,8 +961,18 @@ static int virtio_blk_handle_request(VirtIOBlockReq *req, MultiReqBuffer *mrb)
961961
break;
962962
}
963963
default:
964-
virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
965-
g_free(req);
964+
{
965+
/*
966+
* Give subclasses a chance to handle unknown requests. This way the
967+
* class lookup is not in the hot path.
968+
*/
969+
VirtIOBlkClass *vbk = VIRTIO_BLK_GET_CLASS(s);
970+
if (!vbk->handle_unknown_request ||
971+
!vbk->handle_unknown_request(req, mrb, type)) {
972+
virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
973+
g_free(req);
974+
}
975+
}
966976
}
967977
return 0;
968978
}
@@ -2029,6 +2039,7 @@ static const TypeInfo virtio_blk_info = {
20292039
.instance_size = sizeof(VirtIOBlock),
20302040
.instance_init = virtio_blk_instance_init,
20312041
.class_init = virtio_blk_class_init,
2042+
.class_size = sizeof(VirtIOBlkClass),
20322043
};
20332044

20342045
static void virtio_register_types(void)

hw/core/qdev-properties-system.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,3 +1294,12 @@ const PropertyInfo qdev_prop_endian_mode = {
12941294
.set = qdev_propinfo_set_enum,
12951295
.set_default_value = qdev_propinfo_set_default_value_enum,
12961296
};
1297+
1298+
const PropertyInfo qdev_prop_vmapple_virtio_blk_variant = {
1299+
.name = "VMAppleVirtioBlkVariant",
1300+
.description = "unspecified/root/aux",
1301+
.enum_table = &VMAppleVirtioBlkVariant_lookup,
1302+
.get = qdev_propinfo_get_enum,
1303+
.set = qdev_propinfo_set_enum,
1304+
.set_default_value = qdev_propinfo_set_default_value_enum,
1305+
};

hw/vmapple/Kconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ config VMAPPLE_BDIF
88

99
config VMAPPLE_CFG
1010
bool
11+
12+
config VMAPPLE_VIRTIO_BLK
13+
bool

hw/vmapple/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
system_ss.add(when: 'CONFIG_VMAPPLE_AES', if_true: files('aes.c'))
44
system_ss.add(when: 'CONFIG_VMAPPLE_BDIF', if_true: files('bdif.c'))
55
system_ss.add(when: 'CONFIG_VMAPPLE_CFG', if_true: files('cfg.c'))
6+
system_ss.add(when: 'CONFIG_VMAPPLE_VIRTIO_BLK', if_true: files('virtio-blk.c'))

hw/vmapple/virtio-blk.c

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

include/hw/pci/pci_ids.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@
191191
#define PCI_DEVICE_ID_APPLE_UNI_N_AGP 0x0020
192192
#define PCI_DEVICE_ID_APPLE_U3_AGP 0x004b
193193
#define PCI_DEVICE_ID_APPLE_UNI_N_GMAC 0x0021
194+
#define PCI_DEVICE_ID_APPLE_VIRTIO_BLK 0x1a00
194195

195196
#define PCI_VENDOR_ID_SUN 0x108e
196197
#define PCI_DEVICE_ID_SUN_EBUS 0x1000

include/hw/qdev-properties-system.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ extern const PropertyInfo qdev_prop_pcie_link_width;
3131
extern const PropertyInfo qdev_prop_cpus390entitlement;
3232
extern const PropertyInfo qdev_prop_iothread_vq_mapping_list;
3333
extern const PropertyInfo qdev_prop_endian_mode;
34+
extern const PropertyInfo qdev_prop_vmapple_virtio_blk_variant;
3435

3536
#define DEFINE_PROP_PCI_DEVFN(_n, _s, _f, _d) \
3637
DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_pci_devfn, int32_t)
@@ -104,4 +105,9 @@ extern const PropertyInfo qdev_prop_endian_mode;
104105
#define DEFINE_PROP_ENDIAN_NODEFAULT(_name, _state, _field) \
105106
DEFINE_PROP_ENDIAN(_name, _state, _field, ENDIAN_MODE_UNSPECIFIED)
106107

108+
#define DEFINE_PROP_VMAPPLE_VIRTIO_BLK_VARIANT(_name, _state, _fld, _default) \
109+
DEFINE_PROP_UNSIGNED(_name, _state, _fld, _default, \
110+
qdev_prop_vmapple_virtio_blk_variant, \
111+
VMAppleVirtioBlkVariant)
112+
107113
#endif

include/hw/virtio/virtio-blk.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include "qapi/qapi-types-virtio.h"
2525

2626
#define TYPE_VIRTIO_BLK "virtio-blk-device"
27-
OBJECT_DECLARE_SIMPLE_TYPE(VirtIOBlock, VIRTIO_BLK)
27+
OBJECT_DECLARE_TYPE(VirtIOBlock, VirtIOBlkClass, VIRTIO_BLK)
2828

2929
/* This is the last element of the write scatter-gather list */
3030
struct virtio_blk_inhdr
@@ -100,6 +100,15 @@ typedef struct MultiReqBuffer {
100100
bool is_write;
101101
} MultiReqBuffer;
102102

103+
typedef struct VirtIOBlkClass {
104+
/*< private >*/
105+
VirtioDeviceClass parent;
106+
/*< public >*/
107+
bool (*handle_unknown_request)(VirtIOBlockReq *req, MultiReqBuffer *mrb,
108+
uint32_t type);
109+
} VirtIOBlkClass;
110+
103111
void virtio_blk_handle_vq(VirtIOBlock *s, VirtQueue *vq);
112+
void virtio_blk_req_complete(VirtIOBlockReq *req, unsigned char status);
104113

105114
#endif

include/hw/vmapple/vmapple.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@
1818

1919
#define TYPE_VMAPPLE_CFG "vmapple-cfg"
2020

21+
#define TYPE_VMAPPLE_VIRTIO_BLK_PCI "vmapple-virtio-blk-pci"
22+
2123
#endif /* HW_VMAPPLE_VMAPPLE_H */

qapi/virtio.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,3 +992,17 @@
992992
##
993993
{ 'enum': 'GranuleMode',
994994
'data': [ '4k', '8k', '16k', '64k', 'host' ] }
995+
996+
##
997+
# @VMAppleVirtioBlkVariant:
998+
#
999+
# @unspecified: The default, not a valid setting.
1000+
#
1001+
# @root: Block device holding the root volume
1002+
#
1003+
# @aux: Block device holding auxiliary data required for boot
1004+
#
1005+
# Since: 9.2
1006+
##
1007+
{ 'enum': 'VMAppleVirtioBlkVariant',
1008+
'data': [ 'unspecified', 'root', 'aux' ] }

0 commit comments

Comments
 (0)