1 // SPDX-License-Identifier: GPL-2.0-only
3 * VDPA simulator for block device.
5 * Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
6 * Copyright (c) 2021, Red Hat Inc. All rights reserved.
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/device.h>
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/blkdev.h>
16 #include <linux/vringh.h>
17 #include <linux/vdpa.h>
18 #include <linux/blkdev.h>
19 #include <uapi/linux/virtio_blk.h>
23 #define DRV_VERSION "0.1"
24 #define DRV_AUTHOR "Max Gurtovoy <mgurtovoy@nvidia.com>"
25 #define DRV_DESC "vDPA Device Simulator for block device"
26 #define DRV_LICENSE "GPL v2"
28 #define VDPASIM_BLK_FEATURES (VDPASIM_FEATURES | \
29 (1ULL << VIRTIO_BLK_F_SIZE_MAX) | \
30 (1ULL << VIRTIO_BLK_F_SEG_MAX) | \
31 (1ULL << VIRTIO_BLK_F_BLK_SIZE) | \
32 (1ULL << VIRTIO_BLK_F_TOPOLOGY) | \
33 (1ULL << VIRTIO_BLK_F_MQ))
35 #define VDPASIM_BLK_CAPACITY 0x40000
36 #define VDPASIM_BLK_SIZE_MAX 0x1000
37 #define VDPASIM_BLK_SEG_MAX 32
38 #define VDPASIM_BLK_VQ_NUM 1
40 static char vdpasim_blk_id[VIRTIO_BLK_ID_BYTES] = "vdpa_blk_sim";
42 static bool vdpasim_blk_check_range(u64 start_sector, size_t range_size)
44 u64 range_sectors = range_size >> SECTOR_SHIFT;
46 if (range_size > VDPASIM_BLK_SIZE_MAX * VDPASIM_BLK_SEG_MAX)
49 if (start_sector > VDPASIM_BLK_CAPACITY)
52 if (range_sectors > VDPASIM_BLK_CAPACITY - start_sector)
58 /* Returns 'true' if the request is handled (with or without an I/O error)
59 * and the status is correctly written in the last byte of the 'in iov',
62 static bool vdpasim_blk_handle_req(struct vdpasim *vdpasim,
63 struct vdpasim_virtqueue *vq)
65 size_t pushed = 0, to_pull, to_push;
66 struct virtio_blk_outhdr hdr;
74 ret = vringh_getdesc_iotlb(&vq->vring, &vq->out_iov, &vq->in_iov,
75 &vq->head, GFP_ATOMIC);
79 if (vq->out_iov.used < 1 || vq->in_iov.used < 1) {
80 dev_err(&vdpasim->vdpa.dev, "missing headers - out_iov: %u in_iov %u\n",
81 vq->out_iov.used, vq->in_iov.used);
85 if (vq->in_iov.iov[vq->in_iov.used - 1].iov_len < 1) {
86 dev_err(&vdpasim->vdpa.dev, "request in header too short\n");
90 /* The last byte is the status and we checked if the last iov has
93 to_push = vringh_kiov_length(&vq->in_iov) - 1;
95 to_pull = vringh_kiov_length(&vq->out_iov);
97 bytes = vringh_iov_pull_iotlb(&vq->vring, &vq->out_iov, &hdr,
99 if (bytes != sizeof(hdr)) {
100 dev_err(&vdpasim->vdpa.dev, "request out header too short\n");
106 type = vdpasim32_to_cpu(vdpasim, hdr.type);
107 sector = vdpasim64_to_cpu(vdpasim, hdr.sector);
108 offset = sector << SECTOR_SHIFT;
109 status = VIRTIO_BLK_S_OK;
112 case VIRTIO_BLK_T_IN:
113 if (!vdpasim_blk_check_range(sector, to_push)) {
114 dev_err(&vdpasim->vdpa.dev,
115 "reading over the capacity - offset: 0x%llx len: 0x%zx\n",
117 status = VIRTIO_BLK_S_IOERR;
121 bytes = vringh_iov_push_iotlb(&vq->vring, &vq->in_iov,
122 vdpasim->buffer + offset,
125 dev_err(&vdpasim->vdpa.dev,
126 "vringh_iov_push_iotlb() error: %zd offset: 0x%llx len: 0x%zx\n",
127 bytes, offset, to_push);
128 status = VIRTIO_BLK_S_IOERR;
135 case VIRTIO_BLK_T_OUT:
136 if (!vdpasim_blk_check_range(sector, to_pull)) {
137 dev_err(&vdpasim->vdpa.dev,
138 "writing over the capacity - offset: 0x%llx len: 0x%zx\n",
140 status = VIRTIO_BLK_S_IOERR;
144 bytes = vringh_iov_pull_iotlb(&vq->vring, &vq->out_iov,
145 vdpasim->buffer + offset,
148 dev_err(&vdpasim->vdpa.dev,
149 "vringh_iov_pull_iotlb() error: %zd offset: 0x%llx len: 0x%zx\n",
150 bytes, offset, to_pull);
151 status = VIRTIO_BLK_S_IOERR;
156 case VIRTIO_BLK_T_GET_ID:
157 bytes = vringh_iov_push_iotlb(&vq->vring, &vq->in_iov,
159 VIRTIO_BLK_ID_BYTES);
161 dev_err(&vdpasim->vdpa.dev,
162 "vringh_iov_push_iotlb() error: %zd\n", bytes);
163 status = VIRTIO_BLK_S_IOERR;
171 dev_warn(&vdpasim->vdpa.dev,
172 "Unsupported request type %d\n", type);
173 status = VIRTIO_BLK_S_IOERR;
177 /* If some operations fail, we need to skip the remaining bytes
178 * to put the status in the last byte
180 if (to_push - pushed > 0)
181 vringh_kiov_advance(&vq->in_iov, to_push - pushed);
183 /* Last byte is the status */
184 bytes = vringh_iov_push_iotlb(&vq->vring, &vq->in_iov, &status, 1);
190 /* Make sure data is wrote before advancing index */
193 vringh_complete_iotlb(&vq->vring, vq->head, pushed);
198 static void vdpasim_blk_work(struct work_struct *work)
200 struct vdpasim *vdpasim = container_of(work, struct vdpasim, work);
203 spin_lock(&vdpasim->lock);
205 if (!(vdpasim->status & VIRTIO_CONFIG_S_DRIVER_OK))
208 for (i = 0; i < VDPASIM_BLK_VQ_NUM; i++) {
209 struct vdpasim_virtqueue *vq = &vdpasim->vqs[i];
214 while (vdpasim_blk_handle_req(vdpasim, vq)) {
215 /* Make sure used is visible before rasing the interrupt. */
219 if (vringh_need_notify_iotlb(&vq->vring) > 0)
220 vringh_notify(&vq->vring);
225 spin_unlock(&vdpasim->lock);
228 static void vdpasim_blk_get_config(struct vdpasim *vdpasim, void *config)
230 struct virtio_blk_config *blk_config = config;
232 memset(config, 0, sizeof(struct virtio_blk_config));
234 blk_config->capacity = cpu_to_vdpasim64(vdpasim, VDPASIM_BLK_CAPACITY);
235 blk_config->size_max = cpu_to_vdpasim32(vdpasim, VDPASIM_BLK_SIZE_MAX);
236 blk_config->seg_max = cpu_to_vdpasim32(vdpasim, VDPASIM_BLK_SEG_MAX);
237 blk_config->num_queues = cpu_to_vdpasim16(vdpasim, VDPASIM_BLK_VQ_NUM);
238 blk_config->min_io_size = cpu_to_vdpasim16(vdpasim, 1);
239 blk_config->opt_io_size = cpu_to_vdpasim32(vdpasim, 1);
240 blk_config->blk_size = cpu_to_vdpasim32(vdpasim, SECTOR_SIZE);
243 static void vdpasim_blk_mgmtdev_release(struct device *dev)
247 static struct device vdpasim_blk_mgmtdev = {
248 .init_name = "vdpasim_blk",
249 .release = vdpasim_blk_mgmtdev_release,
252 static int vdpasim_blk_dev_add(struct vdpa_mgmt_dev *mdev, const char *name)
254 struct vdpasim_dev_attr dev_attr = {};
255 struct vdpasim *simdev;
258 dev_attr.mgmt_dev = mdev;
259 dev_attr.name = name;
260 dev_attr.id = VIRTIO_ID_BLOCK;
261 dev_attr.supported_features = VDPASIM_BLK_FEATURES;
262 dev_attr.nvqs = VDPASIM_BLK_VQ_NUM;
263 dev_attr.config_size = sizeof(struct virtio_blk_config);
264 dev_attr.get_config = vdpasim_blk_get_config;
265 dev_attr.work_fn = vdpasim_blk_work;
266 dev_attr.buffer_size = VDPASIM_BLK_CAPACITY << SECTOR_SHIFT;
268 simdev = vdpasim_create(&dev_attr);
270 return PTR_ERR(simdev);
272 ret = _vdpa_register_device(&simdev->vdpa, VDPASIM_BLK_VQ_NUM);
279 put_device(&simdev->vdpa.dev);
283 static void vdpasim_blk_dev_del(struct vdpa_mgmt_dev *mdev,
284 struct vdpa_device *dev)
286 struct vdpasim *simdev = container_of(dev, struct vdpasim, vdpa);
288 _vdpa_unregister_device(&simdev->vdpa);
291 static const struct vdpa_mgmtdev_ops vdpasim_blk_mgmtdev_ops = {
292 .dev_add = vdpasim_blk_dev_add,
293 .dev_del = vdpasim_blk_dev_del
296 static struct virtio_device_id id_table[] = {
297 { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
301 static struct vdpa_mgmt_dev mgmt_dev = {
302 .device = &vdpasim_blk_mgmtdev,
303 .id_table = id_table,
304 .ops = &vdpasim_blk_mgmtdev_ops,
307 static int __init vdpasim_blk_init(void)
311 ret = device_register(&vdpasim_blk_mgmtdev);
315 ret = vdpa_mgmtdev_register(&mgmt_dev);
322 device_unregister(&vdpasim_blk_mgmtdev);
326 static void __exit vdpasim_blk_exit(void)
328 vdpa_mgmtdev_unregister(&mgmt_dev);
329 device_unregister(&vdpasim_blk_mgmtdev);
332 module_init(vdpasim_blk_init)
333 module_exit(vdpasim_blk_exit)
335 MODULE_VERSION(DRV_VERSION);
336 MODULE_LICENSE(DRV_LICENSE);
337 MODULE_AUTHOR(DRV_AUTHOR);
338 MODULE_DESCRIPTION(DRV_DESC);