1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * VIRTIO based driver for vDPA device 4 * 5 * Copyright (c) 2020, Red Hat. All rights reserved. 6 * Author: Jason Wang <jasowang@redhat.com> 7 * 8 */ 9 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> 20 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" 25 26 struct virtio_vdpa_device { 27 struct virtio_device vdev; 28 struct vdpa_device *vdpa; 29 u64 features; 30 31 /* The lock to protect virtqueue list */ 32 spinlock_t lock; 33 /* List of virtio_vdpa_vq_info */ 34 struct list_head virtqueues; 35 }; 36 37 struct virtio_vdpa_vq_info { 38 /* the actual virtqueue */ 39 struct virtqueue *vq; 40 41 /* the list node for the virtqueues list */ 42 struct list_head node; 43 }; 44 45 static inline struct virtio_vdpa_device * 46 to_virtio_vdpa_device(struct virtio_device *dev) 47 { 48 return container_of(dev, struct virtio_vdpa_device, vdev); 49 } 50 51 static struct vdpa_device *vd_get_vdpa(struct virtio_device *vdev) 52 { 53 return to_virtio_vdpa_device(vdev)->vdpa; 54 } 55 56 static void virtio_vdpa_get(struct virtio_device *vdev, unsigned int offset, 57 void *buf, unsigned int len) 58 { 59 struct vdpa_device *vdpa = vd_get_vdpa(vdev); 60 61 vdpa_get_config(vdpa, offset, buf, len); 62 } 63 64 static void virtio_vdpa_set(struct virtio_device *vdev, unsigned int offset, 65 const void *buf, unsigned int len) 66 { 67 struct vdpa_device *vdpa = vd_get_vdpa(vdev); 68 69 vdpa_set_config(vdpa, offset, buf, len); 70 } 71 72 static u32 virtio_vdpa_generation(struct virtio_device *vdev) 73 { 74 struct vdpa_device *vdpa = vd_get_vdpa(vdev); 75 const struct vdpa_config_ops *ops = vdpa->config; 76 77 if (ops->get_generation) 78 return ops->get_generation(vdpa); 79 80 return 0; 81 } 82 83 static u8 virtio_vdpa_get_status(struct virtio_device *vdev) 84 { 85 struct vdpa_device *vdpa = vd_get_vdpa(vdev); 86 const struct vdpa_config_ops *ops = vdpa->config; 87 88 return ops->get_status(vdpa); 89 } 90 91 static void virtio_vdpa_set_status(struct virtio_device *vdev, u8 status) 92 { 93 struct vdpa_device *vdpa = vd_get_vdpa(vdev); 94 95 return vdpa_set_status(vdpa, status); 96 } 97 98 static void virtio_vdpa_reset(struct virtio_device *vdev) 99 { 100 struct vdpa_device *vdpa = vd_get_vdpa(vdev); 101 102 vdpa_reset(vdpa); 103 } 104 105 static bool virtio_vdpa_notify(struct virtqueue *vq) 106 { 107 struct vdpa_device *vdpa = vd_get_vdpa(vq->vdev); 108 const struct vdpa_config_ops *ops = vdpa->config; 109 110 ops->kick_vq(vdpa, vq->index); 111 112 return true; 113 } 114 115 static irqreturn_t virtio_vdpa_config_cb(void *private) 116 { 117 struct virtio_vdpa_device *vd_dev = private; 118 119 virtio_config_changed(&vd_dev->vdev); 120 121 return IRQ_HANDLED; 122 } 123 124 static irqreturn_t virtio_vdpa_virtqueue_cb(void *private) 125 { 126 struct virtio_vdpa_vq_info *info = private; 127 128 return vring_interrupt(0, info->vq); 129 } 130 131 static struct virtqueue * 132 virtio_vdpa_setup_vq(struct virtio_device *vdev, unsigned int index, 133 void (*callback)(struct virtqueue *vq), 134 const char *name, bool ctx) 135 { 136 struct virtio_vdpa_device *vd_dev = to_virtio_vdpa_device(vdev); 137 struct vdpa_device *vdpa = vd_get_vdpa(vdev); 138 struct device *dma_dev; 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}; 146 unsigned long flags; 147 u32 align, max_num, min_num = 1; 148 bool may_reduce_num = true; 149 int err; 150 151 if (!name) 152 return NULL; 153 154 if (index >= vdpa->nvqs) 155 return ERR_PTR(-ENOENT); 156 157 /* Queue shouldn't already be set up. */ 158 if (ops->get_vq_ready(vdpa, index)) 159 return ERR_PTR(-ENOENT); 160 161 /* Allocate and fill out our active queue description */ 162 info = kmalloc(sizeof(*info), GFP_KERNEL); 163 if (!info) 164 return ERR_PTR(-ENOMEM); 165 166 max_num = ops->get_vq_num_max(vdpa); 167 if (max_num == 0) { 168 err = -ENOENT; 169 goto error_new_virtqueue; 170 } 171 172 if (ops->get_vq_num_min) 173 min_num = ops->get_vq_num_min(vdpa); 174 175 may_reduce_num = (max_num == min_num) ? false : true; 176 177 /* Create the vring */ 178 align = ops->get_vq_align(vdpa); 179 180 if (ops->get_vq_dma_dev) 181 dma_dev = ops->get_vq_dma_dev(vdpa, index); 182 else 183 dma_dev = vdpa_get_dma_dev(vdpa); 184 vq = vring_create_virtqueue_dma(index, max_num, align, vdev, 185 true, may_reduce_num, ctx, 186 virtio_vdpa_notify, callback, 187 name, dma_dev); 188 if (!vq) { 189 err = -ENOMEM; 190 goto error_new_virtqueue; 191 } 192 193 vq->num_max = max_num; 194 195 /* Setup virtqueue callback */ 196 cb.callback = callback ? virtio_vdpa_virtqueue_cb : NULL; 197 cb.private = info; 198 ops->set_vq_cb(vdpa, index, &cb); 199 ops->set_vq_num(vdpa, index, virtqueue_get_vring_size(vq)); 200 201 desc_addr = virtqueue_get_desc_addr(vq); 202 driver_addr = virtqueue_get_avail_addr(vq); 203 device_addr = virtqueue_get_used_addr(vq); 204 205 if (ops->set_vq_address(vdpa, index, 206 desc_addr, driver_addr, 207 device_addr)) { 208 err = -EINVAL; 209 goto err_vq; 210 } 211 212 /* reset virtqueue state index */ 213 if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED)) { 214 struct vdpa_vq_state_packed *s = &state.packed; 215 216 s->last_avail_counter = 1; 217 s->last_avail_idx = 0; 218 s->last_used_counter = 1; 219 s->last_used_idx = 0; 220 } 221 err = ops->set_vq_state(vdpa, index, &state); 222 if (err) 223 goto err_vq; 224 225 ops->set_vq_ready(vdpa, index, 1); 226 227 vq->priv = info; 228 info->vq = vq; 229 230 spin_lock_irqsave(&vd_dev->lock, flags); 231 list_add(&info->node, &vd_dev->virtqueues); 232 spin_unlock_irqrestore(&vd_dev->lock, flags); 233 234 return vq; 235 236 err_vq: 237 vring_del_virtqueue(vq); 238 error_new_virtqueue: 239 ops->set_vq_ready(vdpa, index, 0); 240 /* VDPA driver should make sure vq is stopeed here */ 241 WARN_ON(ops->get_vq_ready(vdpa, index)); 242 kfree(info); 243 return ERR_PTR(err); 244 } 245 246 static void virtio_vdpa_del_vq(struct virtqueue *vq) 247 { 248 struct virtio_vdpa_device *vd_dev = to_virtio_vdpa_device(vq->vdev); 249 struct vdpa_device *vdpa = vd_dev->vdpa; 250 const struct vdpa_config_ops *ops = vdpa->config; 251 struct virtio_vdpa_vq_info *info = vq->priv; 252 unsigned int index = vq->index; 253 unsigned long flags; 254 255 spin_lock_irqsave(&vd_dev->lock, flags); 256 list_del(&info->node); 257 spin_unlock_irqrestore(&vd_dev->lock, flags); 258 259 /* Select and deactivate the queue (best effort) */ 260 ops->set_vq_ready(vdpa, index, 0); 261 262 vring_del_virtqueue(vq); 263 264 kfree(info); 265 } 266 267 static void virtio_vdpa_del_vqs(struct virtio_device *vdev) 268 { 269 struct virtqueue *vq, *n; 270 271 list_for_each_entry_safe(vq, n, &vdev->vqs, list) 272 virtio_vdpa_del_vq(vq); 273 } 274 275 static int virtio_vdpa_find_vqs(struct virtio_device *vdev, unsigned int nvqs, 276 struct virtqueue *vqs[], 277 vq_callback_t *callbacks[], 278 const char * const names[], 279 const bool *ctx, 280 struct irq_affinity *desc) 281 { 282 struct virtio_vdpa_device *vd_dev = to_virtio_vdpa_device(vdev); 283 struct vdpa_device *vdpa = vd_get_vdpa(vdev); 284 const struct vdpa_config_ops *ops = vdpa->config; 285 struct vdpa_callback cb; 286 int i, err, queue_idx = 0; 287 288 for (i = 0; i < nvqs; ++i) { 289 if (!names[i]) { 290 vqs[i] = NULL; 291 continue; 292 } 293 294 vqs[i] = virtio_vdpa_setup_vq(vdev, queue_idx++, 295 callbacks[i], names[i], ctx ? 296 ctx[i] : false); 297 if (IS_ERR(vqs[i])) { 298 err = PTR_ERR(vqs[i]); 299 goto err_setup_vq; 300 } 301 } 302 303 cb.callback = virtio_vdpa_config_cb; 304 cb.private = vd_dev; 305 ops->set_config_cb(vdpa, &cb); 306 307 return 0; 308 309 err_setup_vq: 310 virtio_vdpa_del_vqs(vdev); 311 return err; 312 } 313 314 static u64 virtio_vdpa_get_features(struct virtio_device *vdev) 315 { 316 struct vdpa_device *vdpa = vd_get_vdpa(vdev); 317 const struct vdpa_config_ops *ops = vdpa->config; 318 319 return ops->get_device_features(vdpa); 320 } 321 322 static int virtio_vdpa_finalize_features(struct virtio_device *vdev) 323 { 324 struct vdpa_device *vdpa = vd_get_vdpa(vdev); 325 326 /* Give virtio_ring a chance to accept features. */ 327 vring_transport_features(vdev); 328 329 return vdpa_set_features(vdpa, vdev->features); 330 } 331 332 static const char *virtio_vdpa_bus_name(struct virtio_device *vdev) 333 { 334 struct virtio_vdpa_device *vd_dev = to_virtio_vdpa_device(vdev); 335 struct vdpa_device *vdpa = vd_dev->vdpa; 336 337 return dev_name(&vdpa->dev); 338 } 339 340 static const struct virtio_config_ops virtio_vdpa_config_ops = { 341 .get = virtio_vdpa_get, 342 .set = virtio_vdpa_set, 343 .generation = virtio_vdpa_generation, 344 .get_status = virtio_vdpa_get_status, 345 .set_status = virtio_vdpa_set_status, 346 .reset = virtio_vdpa_reset, 347 .find_vqs = virtio_vdpa_find_vqs, 348 .del_vqs = virtio_vdpa_del_vqs, 349 .get_features = virtio_vdpa_get_features, 350 .finalize_features = virtio_vdpa_finalize_features, 351 .bus_name = virtio_vdpa_bus_name, 352 }; 353 354 static void virtio_vdpa_release_dev(struct device *_d) 355 { 356 struct virtio_device *vdev = 357 container_of(_d, struct virtio_device, dev); 358 struct virtio_vdpa_device *vd_dev = 359 container_of(vdev, struct virtio_vdpa_device, vdev); 360 361 kfree(vd_dev); 362 } 363 364 static int virtio_vdpa_probe(struct vdpa_device *vdpa) 365 { 366 const struct vdpa_config_ops *ops = vdpa->config; 367 struct virtio_vdpa_device *vd_dev, *reg_dev = NULL; 368 int ret = -EINVAL; 369 370 vd_dev = kzalloc(sizeof(*vd_dev), GFP_KERNEL); 371 if (!vd_dev) 372 return -ENOMEM; 373 374 vd_dev->vdev.dev.parent = vdpa_get_dma_dev(vdpa); 375 vd_dev->vdev.dev.release = virtio_vdpa_release_dev; 376 vd_dev->vdev.config = &virtio_vdpa_config_ops; 377 vd_dev->vdpa = vdpa; 378 INIT_LIST_HEAD(&vd_dev->virtqueues); 379 spin_lock_init(&vd_dev->lock); 380 381 vd_dev->vdev.id.device = ops->get_device_id(vdpa); 382 if (vd_dev->vdev.id.device == 0) 383 goto err; 384 385 vd_dev->vdev.id.vendor = ops->get_vendor_id(vdpa); 386 ret = register_virtio_device(&vd_dev->vdev); 387 reg_dev = vd_dev; 388 if (ret) 389 goto err; 390 391 vdpa_set_drvdata(vdpa, vd_dev); 392 393 return 0; 394 395 err: 396 if (reg_dev) 397 put_device(&vd_dev->vdev.dev); 398 else 399 kfree(vd_dev); 400 return ret; 401 } 402 403 static void virtio_vdpa_remove(struct vdpa_device *vdpa) 404 { 405 struct virtio_vdpa_device *vd_dev = vdpa_get_drvdata(vdpa); 406 407 unregister_virtio_device(&vd_dev->vdev); 408 } 409 410 static struct vdpa_driver virtio_vdpa_driver = { 411 .driver = { 412 .name = "virtio_vdpa", 413 }, 414 .probe = virtio_vdpa_probe, 415 .remove = virtio_vdpa_remove, 416 }; 417 418 module_vdpa_driver(virtio_vdpa_driver); 419 420 MODULE_VERSION(MOD_VERSION); 421 MODULE_LICENSE(MOD_LICENSE); 422 MODULE_AUTHOR(MOD_AUTHOR); 423 MODULE_DESCRIPTION(MOD_DESC); 424