1 // SPDX-License-Identifier: GPL-2.0-only
3 * VIRTIO based driver for vDPA device
5 * Copyright (c) 2020, Red Hat. All rights reserved.
6 * Author: Jason Wang <jasowang@redhat.com>
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/device.h>
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/uuid.h>
16 #include <linux/virtio.h>
17 #include <linux/vdpa.h>
18 #include <linux/virtio_config.h>
19 #include <linux/virtio_ring.h>
21 #define MOD_VERSION "0.1"
22 #define MOD_AUTHOR "Jason Wang <jasowang@redhat.com>"
23 #define MOD_DESC "vDPA bus driver for virtio devices"
24 #define MOD_LICENSE "GPL v2"
26 struct virtio_vdpa_device {
27 struct virtio_device vdev;
28 struct vdpa_device *vdpa;
31 /* The lock to protect virtqueue list */
33 /* List of virtio_vdpa_vq_info */
34 struct list_head virtqueues;
37 struct virtio_vdpa_vq_info {
38 /* the actual virtqueue */
41 /* the list node for the virtqueues list */
42 struct list_head node;
45 static inline struct virtio_vdpa_device *
46 to_virtio_vdpa_device(struct virtio_device *dev)
48 return container_of(dev, struct virtio_vdpa_device, vdev);
51 static struct vdpa_device *vd_get_vdpa(struct virtio_device *vdev)
53 return to_virtio_vdpa_device(vdev)->vdpa;
56 static void virtio_vdpa_get(struct virtio_device *vdev, unsigned offset,
57 void *buf, unsigned len)
59 struct vdpa_device *vdpa = vd_get_vdpa(vdev);
61 vdpa_get_config(vdpa, offset, buf, len);
64 static void virtio_vdpa_set(struct virtio_device *vdev, unsigned offset,
65 const void *buf, unsigned len)
67 struct vdpa_device *vdpa = vd_get_vdpa(vdev);
69 vdpa_set_config(vdpa, offset, buf, len);
72 static u32 virtio_vdpa_generation(struct virtio_device *vdev)
74 struct vdpa_device *vdpa = vd_get_vdpa(vdev);
75 const struct vdpa_config_ops *ops = vdpa->config;
77 if (ops->get_generation)
78 return ops->get_generation(vdpa);
83 static u8 virtio_vdpa_get_status(struct virtio_device *vdev)
85 struct vdpa_device *vdpa = vd_get_vdpa(vdev);
86 const struct vdpa_config_ops *ops = vdpa->config;
88 return ops->get_status(vdpa);
91 static void virtio_vdpa_set_status(struct virtio_device *vdev, u8 status)
93 struct vdpa_device *vdpa = vd_get_vdpa(vdev);
94 const struct vdpa_config_ops *ops = vdpa->config;
96 return ops->set_status(vdpa, status);
99 static void virtio_vdpa_reset(struct virtio_device *vdev)
101 struct vdpa_device *vdpa = vd_get_vdpa(vdev);
106 static bool virtio_vdpa_notify(struct virtqueue *vq)
108 struct vdpa_device *vdpa = vd_get_vdpa(vq->vdev);
109 const struct vdpa_config_ops *ops = vdpa->config;
111 ops->kick_vq(vdpa, vq->index);
116 static irqreturn_t virtio_vdpa_config_cb(void *private)
118 struct virtio_vdpa_device *vd_dev = private;
120 virtio_config_changed(&vd_dev->vdev);
125 static irqreturn_t virtio_vdpa_virtqueue_cb(void *private)
127 struct virtio_vdpa_vq_info *info = private;
129 return vring_interrupt(0, info->vq);
132 static struct virtqueue *
133 virtio_vdpa_setup_vq(struct virtio_device *vdev, unsigned int index,
134 void (*callback)(struct virtqueue *vq),
135 const char *name, bool ctx)
137 struct virtio_vdpa_device *vd_dev = to_virtio_vdpa_device(vdev);
138 struct vdpa_device *vdpa = vd_get_vdpa(vdev);
139 const struct vdpa_config_ops *ops = vdpa->config;
140 struct virtio_vdpa_vq_info *info;
141 struct vdpa_callback cb;
142 struct virtqueue *vq;
143 u64 desc_addr, driver_addr, device_addr;
144 /* Assume split virtqueue, switch to packed if necessary */
145 struct vdpa_vq_state state = {0};
147 u32 align, max_num, min_num = 1;
148 bool may_reduce_num = true;
154 if (index >= vdpa->nvqs)
155 return ERR_PTR(-ENOENT);
157 /* Queue shouldn't already be set up. */
158 if (ops->get_vq_ready(vdpa, index))
159 return ERR_PTR(-ENOENT);
161 /* Allocate and fill out our active queue description */
162 info = kmalloc(sizeof(*info), GFP_KERNEL);
164 return ERR_PTR(-ENOMEM);
166 max_num = ops->get_vq_num_max(vdpa);
169 goto error_new_virtqueue;
172 if (ops->get_vq_num_min)
173 min_num = ops->get_vq_num_min(vdpa);
175 may_reduce_num = (max_num == min_num) ? false : true;
177 /* Create the vring */
178 align = ops->get_vq_align(vdpa);
179 vq = vring_create_virtqueue(index, max_num, align, vdev,
180 true, may_reduce_num, ctx,
181 virtio_vdpa_notify, callback, name);
184 goto error_new_virtqueue;
187 /* Setup virtqueue callback */
188 cb.callback = virtio_vdpa_virtqueue_cb;
190 ops->set_vq_cb(vdpa, index, &cb);
191 ops->set_vq_num(vdpa, index, virtqueue_get_vring_size(vq));
193 desc_addr = virtqueue_get_desc_addr(vq);
194 driver_addr = virtqueue_get_avail_addr(vq);
195 device_addr = virtqueue_get_used_addr(vq);
197 if (ops->set_vq_address(vdpa, index,
198 desc_addr, driver_addr,
204 /* reset virtqueue state index */
205 if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED)) {
206 struct vdpa_vq_state_packed *s = &state.packed;
208 s->last_avail_counter = 1;
209 s->last_avail_idx = 0;
210 s->last_used_counter = 1;
211 s->last_used_idx = 0;
213 err = ops->set_vq_state(vdpa, index, &state);
217 ops->set_vq_ready(vdpa, index, 1);
222 spin_lock_irqsave(&vd_dev->lock, flags);
223 list_add(&info->node, &vd_dev->virtqueues);
224 spin_unlock_irqrestore(&vd_dev->lock, flags);
229 vring_del_virtqueue(vq);
231 ops->set_vq_ready(vdpa, index, 0);
232 /* VDPA driver should make sure vq is stopeed here */
233 WARN_ON(ops->get_vq_ready(vdpa, index));
238 static void virtio_vdpa_del_vq(struct virtqueue *vq)
240 struct virtio_vdpa_device *vd_dev = to_virtio_vdpa_device(vq->vdev);
241 struct vdpa_device *vdpa = vd_dev->vdpa;
242 const struct vdpa_config_ops *ops = vdpa->config;
243 struct virtio_vdpa_vq_info *info = vq->priv;
244 unsigned int index = vq->index;
247 spin_lock_irqsave(&vd_dev->lock, flags);
248 list_del(&info->node);
249 spin_unlock_irqrestore(&vd_dev->lock, flags);
251 /* Select and deactivate the queue (best effort) */
252 ops->set_vq_ready(vdpa, index, 0);
254 vring_del_virtqueue(vq);
259 static void virtio_vdpa_del_vqs(struct virtio_device *vdev)
261 struct virtqueue *vq, *n;
263 list_for_each_entry_safe(vq, n, &vdev->vqs, list)
264 virtio_vdpa_del_vq(vq);
267 static int virtio_vdpa_find_vqs(struct virtio_device *vdev, unsigned nvqs,
268 struct virtqueue *vqs[],
269 vq_callback_t *callbacks[],
270 const char * const names[],
272 struct irq_affinity *desc)
274 struct virtio_vdpa_device *vd_dev = to_virtio_vdpa_device(vdev);
275 struct vdpa_device *vdpa = vd_get_vdpa(vdev);
276 const struct vdpa_config_ops *ops = vdpa->config;
277 struct vdpa_callback cb;
278 int i, err, queue_idx = 0;
280 for (i = 0; i < nvqs; ++i) {
286 vqs[i] = virtio_vdpa_setup_vq(vdev, queue_idx++,
287 callbacks[i], names[i], ctx ?
289 if (IS_ERR(vqs[i])) {
290 err = PTR_ERR(vqs[i]);
295 cb.callback = virtio_vdpa_config_cb;
297 ops->set_config_cb(vdpa, &cb);
302 virtio_vdpa_del_vqs(vdev);
306 static u64 virtio_vdpa_get_features(struct virtio_device *vdev)
308 struct vdpa_device *vdpa = vd_get_vdpa(vdev);
309 const struct vdpa_config_ops *ops = vdpa->config;
311 return ops->get_features(vdpa);
314 static int virtio_vdpa_finalize_features(struct virtio_device *vdev)
316 struct vdpa_device *vdpa = vd_get_vdpa(vdev);
318 /* Give virtio_ring a chance to accept features. */
319 vring_transport_features(vdev);
321 return vdpa_set_features(vdpa, vdev->features);
324 static const char *virtio_vdpa_bus_name(struct virtio_device *vdev)
326 struct virtio_vdpa_device *vd_dev = to_virtio_vdpa_device(vdev);
327 struct vdpa_device *vdpa = vd_dev->vdpa;
329 return dev_name(&vdpa->dev);
332 static const struct virtio_config_ops virtio_vdpa_config_ops = {
333 .get = virtio_vdpa_get,
334 .set = virtio_vdpa_set,
335 .generation = virtio_vdpa_generation,
336 .get_status = virtio_vdpa_get_status,
337 .set_status = virtio_vdpa_set_status,
338 .reset = virtio_vdpa_reset,
339 .find_vqs = virtio_vdpa_find_vqs,
340 .del_vqs = virtio_vdpa_del_vqs,
341 .get_features = virtio_vdpa_get_features,
342 .finalize_features = virtio_vdpa_finalize_features,
343 .bus_name = virtio_vdpa_bus_name,
346 static void virtio_vdpa_release_dev(struct device *_d)
348 struct virtio_device *vdev =
349 container_of(_d, struct virtio_device, dev);
350 struct virtio_vdpa_device *vd_dev =
351 container_of(vdev, struct virtio_vdpa_device, vdev);
356 static int virtio_vdpa_probe(struct vdpa_device *vdpa)
358 const struct vdpa_config_ops *ops = vdpa->config;
359 struct virtio_vdpa_device *vd_dev, *reg_dev = NULL;
362 vd_dev = kzalloc(sizeof(*vd_dev), GFP_KERNEL);
366 vd_dev->vdev.dev.parent = vdpa_get_dma_dev(vdpa);
367 vd_dev->vdev.dev.release = virtio_vdpa_release_dev;
368 vd_dev->vdev.config = &virtio_vdpa_config_ops;
370 INIT_LIST_HEAD(&vd_dev->virtqueues);
371 spin_lock_init(&vd_dev->lock);
373 vd_dev->vdev.id.device = ops->get_device_id(vdpa);
374 if (vd_dev->vdev.id.device == 0)
377 vd_dev->vdev.id.vendor = ops->get_vendor_id(vdpa);
378 ret = register_virtio_device(&vd_dev->vdev);
383 vdpa_set_drvdata(vdpa, vd_dev);
389 put_device(&vd_dev->vdev.dev);
395 static void virtio_vdpa_remove(struct vdpa_device *vdpa)
397 struct virtio_vdpa_device *vd_dev = vdpa_get_drvdata(vdpa);
399 unregister_virtio_device(&vd_dev->vdev);
402 static struct vdpa_driver virtio_vdpa_driver = {
404 .name = "virtio_vdpa",
406 .probe = virtio_vdpa_probe,
407 .remove = virtio_vdpa_remove,
410 module_vdpa_driver(virtio_vdpa_driver);
412 MODULE_VERSION(MOD_VERSION);
413 MODULE_LICENSE(MOD_LICENSE);
414 MODULE_AUTHOR(MOD_AUTHOR);
415 MODULE_DESCRIPTION(MOD_DESC);