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/group_cpus.h> 17 #include <linux/virtio.h> 18 #include <linux/vdpa.h> 19 #include <linux/virtio_config.h> 20 #include <linux/virtio_ring.h> 21 22 #define MOD_VERSION "0.1" 23 #define MOD_AUTHOR "Jason Wang <jasowang@redhat.com>" 24 #define MOD_DESC "vDPA bus driver for virtio devices" 25 #define MOD_LICENSE "GPL v2" 26 27 struct virtio_vdpa_device { 28 struct virtio_device vdev; 29 struct vdpa_device *vdpa; 30 u64 features; 31 }; 32 33 static inline struct virtio_vdpa_device * 34 to_virtio_vdpa_device(struct virtio_device *dev) 35 { 36 return container_of(dev, struct virtio_vdpa_device, vdev); 37 } 38 39 static struct vdpa_device *vd_get_vdpa(struct virtio_device *vdev) 40 { 41 return to_virtio_vdpa_device(vdev)->vdpa; 42 } 43 44 static void virtio_vdpa_get(struct virtio_device *vdev, unsigned int offset, 45 void *buf, unsigned int len) 46 { 47 struct vdpa_device *vdpa = vd_get_vdpa(vdev); 48 49 vdpa_get_config(vdpa, offset, buf, len); 50 } 51 52 static void virtio_vdpa_set(struct virtio_device *vdev, unsigned int offset, 53 const void *buf, unsigned int len) 54 { 55 struct vdpa_device *vdpa = vd_get_vdpa(vdev); 56 57 vdpa_set_config(vdpa, offset, buf, len); 58 } 59 60 static u32 virtio_vdpa_generation(struct virtio_device *vdev) 61 { 62 struct vdpa_device *vdpa = vd_get_vdpa(vdev); 63 const struct vdpa_config_ops *ops = vdpa->config; 64 65 if (ops->get_generation) 66 return ops->get_generation(vdpa); 67 68 return 0; 69 } 70 71 static u8 virtio_vdpa_get_status(struct virtio_device *vdev) 72 { 73 struct vdpa_device *vdpa = vd_get_vdpa(vdev); 74 const struct vdpa_config_ops *ops = vdpa->config; 75 76 return ops->get_status(vdpa); 77 } 78 79 static void virtio_vdpa_set_status(struct virtio_device *vdev, u8 status) 80 { 81 struct vdpa_device *vdpa = vd_get_vdpa(vdev); 82 83 return vdpa_set_status(vdpa, status); 84 } 85 86 static void virtio_vdpa_reset(struct virtio_device *vdev) 87 { 88 struct vdpa_device *vdpa = vd_get_vdpa(vdev); 89 90 vdpa_reset(vdpa, 0); 91 } 92 93 static bool virtio_vdpa_notify(struct virtqueue *vq) 94 { 95 struct vdpa_device *vdpa = vd_get_vdpa(vq->vdev); 96 const struct vdpa_config_ops *ops = vdpa->config; 97 98 ops->kick_vq(vdpa, vq->index); 99 100 return true; 101 } 102 103 static bool virtio_vdpa_notify_with_data(struct virtqueue *vq) 104 { 105 struct vdpa_device *vdpa = vd_get_vdpa(vq->vdev); 106 const struct vdpa_config_ops *ops = vdpa->config; 107 u32 data = vring_notification_data(vq); 108 109 ops->kick_vq_with_data(vdpa, data); 110 111 return true; 112 } 113 114 static irqreturn_t virtio_vdpa_config_cb(void *private) 115 { 116 struct virtio_vdpa_device *vd_dev = private; 117 118 virtio_config_changed(&vd_dev->vdev); 119 120 return IRQ_HANDLED; 121 } 122 123 static irqreturn_t virtio_vdpa_virtqueue_cb(void *private) 124 { 125 struct virtqueue *vq = private; 126 127 return vring_interrupt(0, vq); 128 } 129 130 static struct virtqueue * 131 virtio_vdpa_setup_vq(struct virtio_device *vdev, unsigned int index, 132 void (*callback)(struct virtqueue *vq), 133 const char *name, bool ctx) 134 { 135 struct vdpa_device *vdpa = vd_get_vdpa(vdev); 136 struct device *dma_dev; 137 const struct vdpa_config_ops *ops = vdpa->config; 138 bool (*notify)(struct virtqueue *vq) = virtio_vdpa_notify; 139 struct vdpa_callback cb; 140 struct virtqueue *vq; 141 u64 desc_addr, driver_addr, device_addr; 142 /* Assume split virtqueue, switch to packed if necessary */ 143 struct vdpa_vq_state state = {0}; 144 u32 align, max_num, min_num = 1; 145 bool may_reduce_num = true; 146 int err; 147 148 if (!name) 149 return NULL; 150 151 if (index >= vdpa->nvqs) 152 return ERR_PTR(-ENOENT); 153 154 /* We cannot accept VIRTIO_F_NOTIFICATION_DATA without kick_vq_with_data */ 155 if (__virtio_test_bit(vdev, VIRTIO_F_NOTIFICATION_DATA)) { 156 if (ops->kick_vq_with_data) 157 notify = virtio_vdpa_notify_with_data; 158 else 159 __virtio_clear_bit(vdev, VIRTIO_F_NOTIFICATION_DATA); 160 } 161 162 /* Queue shouldn't already be set up. */ 163 if (ops->get_vq_ready(vdpa, index)) 164 return ERR_PTR(-ENOENT); 165 166 if (ops->get_vq_size) 167 max_num = ops->get_vq_size(vdpa, index); 168 else 169 max_num = ops->get_vq_num_max(vdpa); 170 171 if (max_num == 0) { 172 err = -ENOENT; 173 goto error_new_virtqueue; 174 } 175 176 if (ops->get_vq_num_min) 177 min_num = ops->get_vq_num_min(vdpa); 178 179 may_reduce_num = (max_num == min_num) ? false : true; 180 181 /* Create the vring */ 182 align = ops->get_vq_align(vdpa); 183 184 if (ops->get_vq_dma_dev) 185 dma_dev = ops->get_vq_dma_dev(vdpa, index); 186 else 187 dma_dev = vdpa_get_dma_dev(vdpa); 188 vq = vring_create_virtqueue_dma(index, max_num, align, vdev, 189 true, may_reduce_num, ctx, 190 notify, callback, name, dma_dev); 191 if (!vq) { 192 err = -ENOMEM; 193 goto error_new_virtqueue; 194 } 195 196 vq->num_max = max_num; 197 198 /* Setup virtqueue callback */ 199 cb.callback = callback ? virtio_vdpa_virtqueue_cb : NULL; 200 cb.private = vq; 201 cb.trigger = NULL; 202 ops->set_vq_cb(vdpa, index, &cb); 203 ops->set_vq_num(vdpa, index, virtqueue_get_vring_size(vq)); 204 205 desc_addr = virtqueue_get_desc_addr(vq); 206 driver_addr = virtqueue_get_avail_addr(vq); 207 device_addr = virtqueue_get_used_addr(vq); 208 209 if (ops->set_vq_address(vdpa, index, 210 desc_addr, driver_addr, 211 device_addr)) { 212 err = -EINVAL; 213 goto err_vq; 214 } 215 216 /* reset virtqueue state index */ 217 if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED)) { 218 struct vdpa_vq_state_packed *s = &state.packed; 219 220 s->last_avail_counter = 1; 221 s->last_avail_idx = 0; 222 s->last_used_counter = 1; 223 s->last_used_idx = 0; 224 } 225 err = ops->set_vq_state(vdpa, index, &state); 226 if (err) 227 goto err_vq; 228 229 ops->set_vq_ready(vdpa, index, 1); 230 231 return vq; 232 233 err_vq: 234 vring_del_virtqueue(vq); 235 error_new_virtqueue: 236 ops->set_vq_ready(vdpa, index, 0); 237 /* VDPA driver should make sure vq is stopeed here */ 238 WARN_ON(ops->get_vq_ready(vdpa, index)); 239 return ERR_PTR(err); 240 } 241 242 static void virtio_vdpa_del_vq(struct virtqueue *vq) 243 { 244 struct virtio_vdpa_device *vd_dev = to_virtio_vdpa_device(vq->vdev); 245 struct vdpa_device *vdpa = vd_dev->vdpa; 246 const struct vdpa_config_ops *ops = vdpa->config; 247 unsigned int index = vq->index; 248 249 /* Select and deactivate the queue (best effort) */ 250 ops->set_vq_ready(vdpa, index, 0); 251 252 vring_del_virtqueue(vq); 253 } 254 255 static void virtio_vdpa_del_vqs(struct virtio_device *vdev) 256 { 257 struct virtqueue *vq, *n; 258 259 list_for_each_entry_safe(vq, n, &vdev->vqs, list) 260 virtio_vdpa_del_vq(vq); 261 } 262 263 static void default_calc_sets(struct irq_affinity *affd, unsigned int affvecs) 264 { 265 affd->nr_sets = 1; 266 affd->set_size[0] = affvecs; 267 } 268 269 static struct cpumask * 270 create_affinity_masks(unsigned int nvecs, struct irq_affinity *affd) 271 { 272 unsigned int affvecs = 0, curvec, usedvecs, i; 273 struct cpumask *masks = NULL; 274 275 if (nvecs > affd->pre_vectors + affd->post_vectors) 276 affvecs = nvecs - affd->pre_vectors - affd->post_vectors; 277 278 if (!affd->calc_sets) 279 affd->calc_sets = default_calc_sets; 280 281 affd->calc_sets(affd, affvecs); 282 283 if (!affvecs) 284 return NULL; 285 286 masks = kcalloc(nvecs, sizeof(*masks), GFP_KERNEL); 287 if (!masks) 288 return NULL; 289 290 /* Fill out vectors at the beginning that don't need affinity */ 291 for (curvec = 0; curvec < affd->pre_vectors; curvec++) 292 cpumask_setall(&masks[curvec]); 293 294 for (i = 0, usedvecs = 0; i < affd->nr_sets; i++) { 295 unsigned int this_vecs = affd->set_size[i]; 296 unsigned int nr_masks; 297 int j; 298 struct cpumask *result = group_cpus_evenly(this_vecs, &nr_masks); 299 300 if (!result) { 301 kfree(masks); 302 return NULL; 303 } 304 305 for (j = 0; j < nr_masks; j++) 306 cpumask_copy(&masks[curvec + j], &result[j]); 307 kfree(result); 308 309 curvec += nr_masks; 310 usedvecs += nr_masks; 311 } 312 313 /* Fill out vectors at the end that don't need affinity */ 314 if (usedvecs >= affvecs) 315 curvec = affd->pre_vectors + affvecs; 316 else 317 curvec = affd->pre_vectors + usedvecs; 318 for (; curvec < nvecs; curvec++) 319 cpumask_setall(&masks[curvec]); 320 321 return masks; 322 } 323 324 static int virtio_vdpa_find_vqs(struct virtio_device *vdev, unsigned int nvqs, 325 struct virtqueue *vqs[], 326 struct virtqueue_info vqs_info[], 327 struct irq_affinity *desc) 328 { 329 struct virtio_vdpa_device *vd_dev = to_virtio_vdpa_device(vdev); 330 struct vdpa_device *vdpa = vd_get_vdpa(vdev); 331 const struct vdpa_config_ops *ops = vdpa->config; 332 struct cpumask *masks; 333 struct vdpa_callback cb; 334 bool has_affinity = desc && ops->set_vq_affinity; 335 int i, err, queue_idx = 0; 336 337 if (has_affinity) { 338 masks = create_affinity_masks(nvqs, desc); 339 if (!masks) 340 return -ENOMEM; 341 } 342 343 for (i = 0; i < nvqs; ++i) { 344 struct virtqueue_info *vqi = &vqs_info[i]; 345 346 if (!vqi->name) { 347 vqs[i] = NULL; 348 continue; 349 } 350 351 vqs[i] = virtio_vdpa_setup_vq(vdev, queue_idx++, vqi->callback, 352 vqi->name, vqi->ctx); 353 if (IS_ERR(vqs[i])) { 354 err = PTR_ERR(vqs[i]); 355 goto err_setup_vq; 356 } 357 358 if (has_affinity) 359 ops->set_vq_affinity(vdpa, i, &masks[i]); 360 } 361 362 cb.callback = virtio_vdpa_config_cb; 363 cb.private = vd_dev; 364 ops->set_config_cb(vdpa, &cb); 365 if (has_affinity) 366 kfree(masks); 367 368 return 0; 369 370 err_setup_vq: 371 virtio_vdpa_del_vqs(vdev); 372 if (has_affinity) 373 kfree(masks); 374 return err; 375 } 376 377 static u64 virtio_vdpa_get_features(struct virtio_device *vdev) 378 { 379 struct vdpa_device *vdpa = vd_get_vdpa(vdev); 380 const struct vdpa_config_ops *ops = vdpa->config; 381 382 return ops->get_device_features(vdpa); 383 } 384 385 static int virtio_vdpa_finalize_features(struct virtio_device *vdev) 386 { 387 struct vdpa_device *vdpa = vd_get_vdpa(vdev); 388 389 /* Give virtio_ring a chance to accept features. */ 390 vring_transport_features(vdev); 391 392 return vdpa_set_features(vdpa, vdev->features); 393 } 394 395 static const char *virtio_vdpa_bus_name(struct virtio_device *vdev) 396 { 397 struct virtio_vdpa_device *vd_dev = to_virtio_vdpa_device(vdev); 398 struct vdpa_device *vdpa = vd_dev->vdpa; 399 400 return dev_name(&vdpa->dev); 401 } 402 403 static int virtio_vdpa_set_vq_affinity(struct virtqueue *vq, 404 const struct cpumask *cpu_mask) 405 { 406 struct virtio_vdpa_device *vd_dev = to_virtio_vdpa_device(vq->vdev); 407 struct vdpa_device *vdpa = vd_dev->vdpa; 408 const struct vdpa_config_ops *ops = vdpa->config; 409 unsigned int index = vq->index; 410 411 if (ops->set_vq_affinity) 412 return ops->set_vq_affinity(vdpa, index, cpu_mask); 413 414 return 0; 415 } 416 417 static const struct cpumask * 418 virtio_vdpa_get_vq_affinity(struct virtio_device *vdev, int index) 419 { 420 struct vdpa_device *vdpa = vd_get_vdpa(vdev); 421 const struct vdpa_config_ops *ops = vdpa->config; 422 423 if (ops->get_vq_affinity) 424 return ops->get_vq_affinity(vdpa, index); 425 426 return NULL; 427 } 428 429 static const struct virtio_config_ops virtio_vdpa_config_ops = { 430 .get = virtio_vdpa_get, 431 .set = virtio_vdpa_set, 432 .generation = virtio_vdpa_generation, 433 .get_status = virtio_vdpa_get_status, 434 .set_status = virtio_vdpa_set_status, 435 .reset = virtio_vdpa_reset, 436 .find_vqs = virtio_vdpa_find_vqs, 437 .del_vqs = virtio_vdpa_del_vqs, 438 .get_features = virtio_vdpa_get_features, 439 .finalize_features = virtio_vdpa_finalize_features, 440 .bus_name = virtio_vdpa_bus_name, 441 .set_vq_affinity = virtio_vdpa_set_vq_affinity, 442 .get_vq_affinity = virtio_vdpa_get_vq_affinity, 443 }; 444 445 static void virtio_vdpa_release_dev(struct device *_d) 446 { 447 struct virtio_device *vdev = 448 container_of(_d, struct virtio_device, dev); 449 struct virtio_vdpa_device *vd_dev = 450 container_of(vdev, struct virtio_vdpa_device, vdev); 451 452 kfree(vd_dev); 453 } 454 455 static int virtio_vdpa_probe(struct vdpa_device *vdpa) 456 { 457 const struct vdpa_config_ops *ops = vdpa->config; 458 struct virtio_vdpa_device *vd_dev, *reg_dev = NULL; 459 int ret = -EINVAL; 460 461 vd_dev = kzalloc(sizeof(*vd_dev), GFP_KERNEL); 462 if (!vd_dev) 463 return -ENOMEM; 464 465 vd_dev->vdev.dev.parent = vdpa_get_dma_dev(vdpa); 466 vd_dev->vdev.dev.release = virtio_vdpa_release_dev; 467 vd_dev->vdev.config = &virtio_vdpa_config_ops; 468 vd_dev->vdpa = vdpa; 469 470 vd_dev->vdev.id.device = ops->get_device_id(vdpa); 471 if (vd_dev->vdev.id.device == 0) 472 goto err; 473 474 vd_dev->vdev.id.vendor = ops->get_vendor_id(vdpa); 475 ret = register_virtio_device(&vd_dev->vdev); 476 reg_dev = vd_dev; 477 if (ret) 478 goto err; 479 480 vdpa_set_drvdata(vdpa, vd_dev); 481 482 return 0; 483 484 err: 485 if (reg_dev) 486 put_device(&vd_dev->vdev.dev); 487 else 488 kfree(vd_dev); 489 return ret; 490 } 491 492 static void virtio_vdpa_remove(struct vdpa_device *vdpa) 493 { 494 struct virtio_vdpa_device *vd_dev = vdpa_get_drvdata(vdpa); 495 496 unregister_virtio_device(&vd_dev->vdev); 497 } 498 499 static struct vdpa_driver virtio_vdpa_driver = { 500 .driver = { 501 .name = "virtio_vdpa", 502 }, 503 .probe = virtio_vdpa_probe, 504 .remove = virtio_vdpa_remove, 505 }; 506 507 module_vdpa_driver(virtio_vdpa_driver); 508 509 MODULE_VERSION(MOD_VERSION); 510 MODULE_LICENSE(MOD_LICENSE); 511 MODULE_AUTHOR(MOD_AUTHOR); 512 MODULE_DESCRIPTION(MOD_DESC); 513