1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com>
5 * VirtIO Sandbox transport driver, for testing purpose only
10 #include <virtio_types.h>
12 #include <virtio_ring.h>
13 #include <linux/compat.h>
16 struct virtio_sandbox_priv {
22 ulong queue_available;
26 static int virtio_sandbox_get_config(struct udevice *udev, unsigned int offset,
27 void *buf, unsigned int len)
32 static int virtio_sandbox_set_config(struct udevice *udev, unsigned int offset,
33 const void *buf, unsigned int len)
38 static int virtio_sandbox_get_status(struct udevice *udev, u8 *status)
40 struct virtio_sandbox_priv *priv = dev_get_priv(udev);
42 *status = priv->status;
47 static int virtio_sandbox_set_status(struct udevice *udev, u8 status)
49 struct virtio_sandbox_priv *priv = dev_get_priv(udev);
51 /* We should never be setting status to 0 */
54 priv->status = status;
59 static int virtio_sandbox_reset(struct udevice *udev)
61 struct virtio_sandbox_priv *priv = dev_get_priv(udev);
63 /* 0 status means a reset */
69 static int virtio_sandbox_get_features(struct udevice *udev, u64 *features)
71 struct virtio_sandbox_priv *priv = dev_get_priv(udev);
73 *features = priv->device_features;
78 static int virtio_sandbox_set_features(struct udevice *udev)
80 struct virtio_sandbox_priv *priv = dev_get_priv(udev);
81 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
83 priv->driver_features = uc_priv->features;
88 static struct virtqueue *virtio_sandbox_setup_vq(struct udevice *udev,
91 struct virtio_sandbox_priv *priv = dev_get_priv(udev);
96 /* Create the vring */
97 vq = vring_create_virtqueue(index, 4, 4096, udev);
100 goto error_new_virtqueue;
103 addr = virtqueue_get_desc_addr(vq);
104 priv->queue_desc = addr;
106 addr = virtqueue_get_avail_addr(vq);
107 priv->queue_available = addr;
109 addr = virtqueue_get_used_addr(vq);
110 priv->queue_used = addr;
118 static void virtio_sandbox_del_vq(struct virtqueue *vq)
120 vring_del_virtqueue(vq);
123 static int virtio_sandbox_del_vqs(struct udevice *udev)
125 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
126 struct virtqueue *vq, *n;
128 list_for_each_entry_safe(vq, n, &uc_priv->vqs, list)
129 virtio_sandbox_del_vq(vq);
134 static int virtio_sandbox_find_vqs(struct udevice *udev, unsigned int nvqs,
135 struct virtqueue *vqs[])
139 for (i = 0; i < nvqs; ++i) {
140 vqs[i] = virtio_sandbox_setup_vq(udev, i);
141 if (IS_ERR(vqs[i])) {
142 virtio_sandbox_del_vqs(udev);
143 return PTR_ERR(vqs[i]);
150 static int virtio_sandbox_notify(struct udevice *udev, struct virtqueue *vq)
155 static int virtio_sandbox_probe(struct udevice *udev)
157 struct virtio_sandbox_priv *priv = dev_get_priv(udev);
158 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
160 /* fake some information for testing */
161 priv->device_features = VIRTIO_F_VERSION_1;
162 uc_priv->device = VIRTIO_ID_BLOCK;
163 uc_priv->vendor = ('u' << 24) | ('b' << 16) | ('o' << 8) | 't';
168 /* check virtio device driver's remove routine was called to reset the device */
169 static int virtio_sandbox_child_post_remove(struct udevice *vdev)
173 virtio_get_status(vdev, &status);
175 panic("virtio device was not reset\n");
180 static const struct dm_virtio_ops virtio_sandbox1_ops = {
181 .get_config = virtio_sandbox_get_config,
182 .set_config = virtio_sandbox_set_config,
183 .get_status = virtio_sandbox_get_status,
184 .set_status = virtio_sandbox_set_status,
185 .reset = virtio_sandbox_reset,
186 .get_features = virtio_sandbox_get_features,
187 .set_features = virtio_sandbox_set_features,
188 .find_vqs = virtio_sandbox_find_vqs,
189 .del_vqs = virtio_sandbox_del_vqs,
190 .notify = virtio_sandbox_notify,
193 static const struct udevice_id virtio_sandbox1_ids[] = {
194 { .compatible = "sandbox,virtio1" },
198 U_BOOT_DRIVER(virtio_sandbox1) = {
199 .name = "virtio-sandbox1",
201 .of_match = virtio_sandbox1_ids,
202 .ops = &virtio_sandbox1_ops,
203 .probe = virtio_sandbox_probe,
204 .child_post_remove = virtio_sandbox_child_post_remove,
205 .priv_auto_alloc_size = sizeof(struct virtio_sandbox_priv),
208 /* this one without notify op */
209 static const struct dm_virtio_ops virtio_sandbox2_ops = {
210 .get_config = virtio_sandbox_get_config,
211 .set_config = virtio_sandbox_set_config,
212 .get_status = virtio_sandbox_get_status,
213 .set_status = virtio_sandbox_set_status,
214 .reset = virtio_sandbox_reset,
215 .get_features = virtio_sandbox_get_features,
216 .set_features = virtio_sandbox_set_features,
217 .find_vqs = virtio_sandbox_find_vqs,
218 .del_vqs = virtio_sandbox_del_vqs,
221 static const struct udevice_id virtio_sandbox2_ids[] = {
222 { .compatible = "sandbox,virtio2" },
226 U_BOOT_DRIVER(virtio_sandbox2) = {
227 .name = "virtio-sandbox2",
229 .of_match = virtio_sandbox2_ids,
230 .ops = &virtio_sandbox2_ops,
231 .probe = virtio_sandbox_probe,
232 .priv_auto_alloc_size = sizeof(struct virtio_sandbox_priv),