1 // SPDX-License-Identifier: GPL-2.0-only
3 * vDPA bridge driver for modern virtio-pci device
5 * Copyright (c) 2020, Red Hat Inc. All rights reserved.
6 * Author: Jason Wang <jasowang@redhat.com>
8 * Based on virtio_pci_modern.c.
11 #include <linux/interrupt.h>
12 #include <linux/module.h>
13 #include <linux/pci.h>
14 #include <linux/vdpa.h>
15 #include <linux/virtio.h>
16 #include <linux/virtio_config.h>
17 #include <linux/virtio_ring.h>
18 #include <linux/virtio_pci.h>
19 #include <linux/virtio_pci_modern.h>
20 #include <uapi/linux/vdpa.h>
22 #define VP_VDPA_QUEUE_MAX 256
23 #define VP_VDPA_DRIVER_NAME "vp_vdpa"
24 #define VP_VDPA_NAME_SIZE 256
28 char msix_name[VP_VDPA_NAME_SIZE];
29 struct vdpa_callback cb;
30 resource_size_t notify_pa;
35 struct vdpa_device vdpa;
36 struct virtio_pci_modern_device *mdev;
37 struct vp_vring *vring;
38 struct vdpa_callback config_cb;
40 char msix_name[VP_VDPA_NAME_SIZE];
46 struct vp_vdpa_mgmtdev {
47 struct vdpa_mgmt_dev mgtdev;
48 struct virtio_pci_modern_device *mdev;
49 struct vp_vdpa *vp_vdpa;
52 static struct vp_vdpa *vdpa_to_vp(struct vdpa_device *vdpa)
54 return container_of(vdpa, struct vp_vdpa, vdpa);
57 static struct virtio_pci_modern_device *vdpa_to_mdev(struct vdpa_device *vdpa)
59 struct vp_vdpa *vp_vdpa = vdpa_to_vp(vdpa);
64 static struct virtio_pci_modern_device *vp_vdpa_to_mdev(struct vp_vdpa *vp_vdpa)
69 static u64 vp_vdpa_get_device_features(struct vdpa_device *vdpa)
71 struct vp_vdpa *vp_vdpa = vdpa_to_vp(vdpa);
73 return vp_vdpa->device_features;
76 static int vp_vdpa_set_driver_features(struct vdpa_device *vdpa, u64 features)
78 struct virtio_pci_modern_device *mdev = vdpa_to_mdev(vdpa);
80 vp_modern_set_features(mdev, features);
85 static u64 vp_vdpa_get_driver_features(struct vdpa_device *vdpa)
87 struct virtio_pci_modern_device *mdev = vdpa_to_mdev(vdpa);
89 return vp_modern_get_driver_features(mdev);
92 static u8 vp_vdpa_get_status(struct vdpa_device *vdpa)
94 struct virtio_pci_modern_device *mdev = vdpa_to_mdev(vdpa);
96 return vp_modern_get_status(mdev);
99 static int vp_vdpa_get_vq_irq(struct vdpa_device *vdpa, u16 idx)
101 struct vp_vdpa *vp_vdpa = vdpa_to_vp(vdpa);
102 int irq = vp_vdpa->vring[idx].irq;
104 if (irq == VIRTIO_MSI_NO_VECTOR)
110 static void vp_vdpa_free_irq(struct vp_vdpa *vp_vdpa)
112 struct virtio_pci_modern_device *mdev = vp_vdpa_to_mdev(vp_vdpa);
113 struct pci_dev *pdev = mdev->pci_dev;
116 for (i = 0; i < vp_vdpa->queues; i++) {
117 if (vp_vdpa->vring[i].irq != VIRTIO_MSI_NO_VECTOR) {
118 vp_modern_queue_vector(mdev, i, VIRTIO_MSI_NO_VECTOR);
119 devm_free_irq(&pdev->dev, vp_vdpa->vring[i].irq,
121 vp_vdpa->vring[i].irq = VIRTIO_MSI_NO_VECTOR;
125 if (vp_vdpa->config_irq != VIRTIO_MSI_NO_VECTOR) {
126 vp_modern_config_vector(mdev, VIRTIO_MSI_NO_VECTOR);
127 devm_free_irq(&pdev->dev, vp_vdpa->config_irq, vp_vdpa);
128 vp_vdpa->config_irq = VIRTIO_MSI_NO_VECTOR;
131 if (vp_vdpa->vectors) {
132 pci_free_irq_vectors(pdev);
133 vp_vdpa->vectors = 0;
137 static irqreturn_t vp_vdpa_vq_handler(int irq, void *arg)
139 struct vp_vring *vring = arg;
141 if (vring->cb.callback)
142 return vring->cb.callback(vring->cb.private);
147 static irqreturn_t vp_vdpa_config_handler(int irq, void *arg)
149 struct vp_vdpa *vp_vdpa = arg;
151 if (vp_vdpa->config_cb.callback)
152 return vp_vdpa->config_cb.callback(vp_vdpa->config_cb.private);
157 static int vp_vdpa_request_irq(struct vp_vdpa *vp_vdpa)
159 struct virtio_pci_modern_device *mdev = vp_vdpa_to_mdev(vp_vdpa);
160 struct pci_dev *pdev = mdev->pci_dev;
162 int queues = vp_vdpa->queues;
163 int vectors = queues + 1;
165 ret = pci_alloc_irq_vectors(pdev, vectors, vectors, PCI_IRQ_MSIX);
166 if (ret != vectors) {
168 "vp_vdpa: fail to allocate irq vectors want %d but %d\n",
173 vp_vdpa->vectors = vectors;
175 for (i = 0; i < queues; i++) {
176 snprintf(vp_vdpa->vring[i].msix_name, VP_VDPA_NAME_SIZE,
177 "vp-vdpa[%s]-%d\n", pci_name(pdev), i);
178 irq = pci_irq_vector(pdev, i);
179 ret = devm_request_irq(&pdev->dev, irq,
181 0, vp_vdpa->vring[i].msix_name,
185 "vp_vdpa: fail to request irq for vq %d\n", i);
188 vp_modern_queue_vector(mdev, i, i);
189 vp_vdpa->vring[i].irq = irq;
192 snprintf(vp_vdpa->msix_name, VP_VDPA_NAME_SIZE, "vp-vdpa[%s]-config\n",
194 irq = pci_irq_vector(pdev, queues);
195 ret = devm_request_irq(&pdev->dev, irq, vp_vdpa_config_handler, 0,
196 vp_vdpa->msix_name, vp_vdpa);
199 "vp_vdpa: fail to request irq for vq %d\n", i);
202 vp_modern_config_vector(mdev, queues);
203 vp_vdpa->config_irq = irq;
207 vp_vdpa_free_irq(vp_vdpa);
211 static void vp_vdpa_set_status(struct vdpa_device *vdpa, u8 status)
213 struct vp_vdpa *vp_vdpa = vdpa_to_vp(vdpa);
214 struct virtio_pci_modern_device *mdev = vp_vdpa_to_mdev(vp_vdpa);
215 u8 s = vp_vdpa_get_status(vdpa);
217 if (status & VIRTIO_CONFIG_S_DRIVER_OK &&
218 !(s & VIRTIO_CONFIG_S_DRIVER_OK)) {
219 vp_vdpa_request_irq(vp_vdpa);
222 vp_modern_set_status(mdev, status);
225 static int vp_vdpa_reset(struct vdpa_device *vdpa)
227 struct vp_vdpa *vp_vdpa = vdpa_to_vp(vdpa);
228 struct virtio_pci_modern_device *mdev = vp_vdpa_to_mdev(vp_vdpa);
229 u8 s = vp_vdpa_get_status(vdpa);
231 vp_modern_set_status(mdev, 0);
233 if (s & VIRTIO_CONFIG_S_DRIVER_OK)
234 vp_vdpa_free_irq(vp_vdpa);
239 static u16 vp_vdpa_get_vq_num_max(struct vdpa_device *vdpa)
241 return VP_VDPA_QUEUE_MAX;
244 static int vp_vdpa_get_vq_state(struct vdpa_device *vdpa, u16 qid,
245 struct vdpa_vq_state *state)
247 /* Note that this is not supported by virtio specification, so
248 * we return -EOPNOTSUPP here. This means we can't support live
249 * migration, vhost device start/stop.
254 static int vp_vdpa_set_vq_state_split(struct vdpa_device *vdpa,
255 const struct vdpa_vq_state *state)
257 const struct vdpa_vq_state_split *split = &state->split;
259 if (split->avail_index == 0)
265 static int vp_vdpa_set_vq_state_packed(struct vdpa_device *vdpa,
266 const struct vdpa_vq_state *state)
268 const struct vdpa_vq_state_packed *packed = &state->packed;
270 if (packed->last_avail_counter == 1 &&
271 packed->last_avail_idx == 0 &&
272 packed->last_used_counter == 1 &&
273 packed->last_used_idx == 0)
279 static int vp_vdpa_set_vq_state(struct vdpa_device *vdpa, u16 qid,
280 const struct vdpa_vq_state *state)
282 struct virtio_pci_modern_device *mdev = vdpa_to_mdev(vdpa);
284 /* Note that this is not supported by virtio specification.
285 * But if the state is by chance equal to the device initial
286 * state, we can let it go.
288 if ((vp_modern_get_status(mdev) & VIRTIO_CONFIG_S_FEATURES_OK) &&
289 !vp_modern_get_queue_enable(mdev, qid)) {
290 if (vp_modern_get_driver_features(mdev) &
291 BIT_ULL(VIRTIO_F_RING_PACKED))
292 return vp_vdpa_set_vq_state_packed(vdpa, state);
294 return vp_vdpa_set_vq_state_split(vdpa, state);
300 static void vp_vdpa_set_vq_cb(struct vdpa_device *vdpa, u16 qid,
301 struct vdpa_callback *cb)
303 struct vp_vdpa *vp_vdpa = vdpa_to_vp(vdpa);
305 vp_vdpa->vring[qid].cb = *cb;
308 static void vp_vdpa_set_vq_ready(struct vdpa_device *vdpa,
311 struct virtio_pci_modern_device *mdev = vdpa_to_mdev(vdpa);
313 vp_modern_set_queue_enable(mdev, qid, ready);
316 static bool vp_vdpa_get_vq_ready(struct vdpa_device *vdpa, u16 qid)
318 struct virtio_pci_modern_device *mdev = vdpa_to_mdev(vdpa);
320 return vp_modern_get_queue_enable(mdev, qid);
323 static void vp_vdpa_set_vq_num(struct vdpa_device *vdpa, u16 qid,
326 struct virtio_pci_modern_device *mdev = vdpa_to_mdev(vdpa);
328 vp_modern_set_queue_size(mdev, qid, num);
331 static int vp_vdpa_set_vq_address(struct vdpa_device *vdpa, u16 qid,
332 u64 desc_area, u64 driver_area,
335 struct virtio_pci_modern_device *mdev = vdpa_to_mdev(vdpa);
337 vp_modern_queue_address(mdev, qid, desc_area,
338 driver_area, device_area);
343 static void vp_vdpa_kick_vq(struct vdpa_device *vdpa, u16 qid)
345 struct vp_vdpa *vp_vdpa = vdpa_to_vp(vdpa);
347 vp_iowrite16(qid, vp_vdpa->vring[qid].notify);
350 static u32 vp_vdpa_get_generation(struct vdpa_device *vdpa)
352 struct virtio_pci_modern_device *mdev = vdpa_to_mdev(vdpa);
354 return vp_modern_generation(mdev);
357 static u32 vp_vdpa_get_device_id(struct vdpa_device *vdpa)
359 struct virtio_pci_modern_device *mdev = vdpa_to_mdev(vdpa);
361 return mdev->id.device;
364 static u32 vp_vdpa_get_vendor_id(struct vdpa_device *vdpa)
366 struct virtio_pci_modern_device *mdev = vdpa_to_mdev(vdpa);
368 return mdev->id.vendor;
371 static u32 vp_vdpa_get_vq_align(struct vdpa_device *vdpa)
376 static size_t vp_vdpa_get_config_size(struct vdpa_device *vdpa)
378 struct virtio_pci_modern_device *mdev = vdpa_to_mdev(vdpa);
380 return mdev->device_len;
383 static void vp_vdpa_get_config(struct vdpa_device *vdpa,
385 void *buf, unsigned int len)
387 struct vp_vdpa *vp_vdpa = vdpa_to_vp(vdpa);
388 struct virtio_pci_modern_device *mdev = vp_vdpa_to_mdev(vp_vdpa);
394 old = vp_ioread8(&mdev->common->config_generation);
396 for (i = 0; i < len; i++)
397 *p++ = vp_ioread8(mdev->device + offset + i);
399 new = vp_ioread8(&mdev->common->config_generation);
400 } while (old != new);
403 static void vp_vdpa_set_config(struct vdpa_device *vdpa,
404 unsigned int offset, const void *buf,
407 struct vp_vdpa *vp_vdpa = vdpa_to_vp(vdpa);
408 struct virtio_pci_modern_device *mdev = vp_vdpa_to_mdev(vp_vdpa);
412 for (i = 0; i < len; i++)
413 vp_iowrite8(*p++, mdev->device + offset + i);
416 static void vp_vdpa_set_config_cb(struct vdpa_device *vdpa,
417 struct vdpa_callback *cb)
419 struct vp_vdpa *vp_vdpa = vdpa_to_vp(vdpa);
421 vp_vdpa->config_cb = *cb;
424 static struct vdpa_notification_area
425 vp_vdpa_get_vq_notification(struct vdpa_device *vdpa, u16 qid)
427 struct vp_vdpa *vp_vdpa = vdpa_to_vp(vdpa);
428 struct virtio_pci_modern_device *mdev = vp_vdpa_to_mdev(vp_vdpa);
429 struct vdpa_notification_area notify;
431 notify.addr = vp_vdpa->vring[qid].notify_pa;
432 notify.size = mdev->notify_offset_multiplier;
437 static const struct vdpa_config_ops vp_vdpa_ops = {
438 .get_device_features = vp_vdpa_get_device_features,
439 .set_driver_features = vp_vdpa_set_driver_features,
440 .get_driver_features = vp_vdpa_get_driver_features,
441 .get_status = vp_vdpa_get_status,
442 .set_status = vp_vdpa_set_status,
443 .reset = vp_vdpa_reset,
444 .get_vq_num_max = vp_vdpa_get_vq_num_max,
445 .get_vq_state = vp_vdpa_get_vq_state,
446 .get_vq_notification = vp_vdpa_get_vq_notification,
447 .set_vq_state = vp_vdpa_set_vq_state,
448 .set_vq_cb = vp_vdpa_set_vq_cb,
449 .set_vq_ready = vp_vdpa_set_vq_ready,
450 .get_vq_ready = vp_vdpa_get_vq_ready,
451 .set_vq_num = vp_vdpa_set_vq_num,
452 .set_vq_address = vp_vdpa_set_vq_address,
453 .kick_vq = vp_vdpa_kick_vq,
454 .get_generation = vp_vdpa_get_generation,
455 .get_device_id = vp_vdpa_get_device_id,
456 .get_vendor_id = vp_vdpa_get_vendor_id,
457 .get_vq_align = vp_vdpa_get_vq_align,
458 .get_config_size = vp_vdpa_get_config_size,
459 .get_config = vp_vdpa_get_config,
460 .set_config = vp_vdpa_set_config,
461 .set_config_cb = vp_vdpa_set_config_cb,
462 .get_vq_irq = vp_vdpa_get_vq_irq,
465 static void vp_vdpa_free_irq_vectors(void *data)
467 pci_free_irq_vectors(data);
470 static int vp_vdpa_dev_add(struct vdpa_mgmt_dev *v_mdev, const char *name,
471 const struct vdpa_dev_set_config *add_config)
473 struct vp_vdpa_mgmtdev *vp_vdpa_mgtdev =
474 container_of(v_mdev, struct vp_vdpa_mgmtdev, mgtdev);
476 struct virtio_pci_modern_device *mdev = vp_vdpa_mgtdev->mdev;
477 struct pci_dev *pdev = mdev->pci_dev;
478 struct device *dev = &pdev->dev;
479 struct vp_vdpa *vp_vdpa = NULL;
483 vp_vdpa = vdpa_alloc_device(struct vp_vdpa, vdpa,
484 dev, &vp_vdpa_ops, 1, 1, name, false);
486 if (IS_ERR(vp_vdpa)) {
487 dev_err(dev, "vp_vdpa: Failed to allocate vDPA structure\n");
488 return PTR_ERR(vp_vdpa);
491 vp_vdpa_mgtdev->vp_vdpa = vp_vdpa;
493 vp_vdpa->vdpa.dma_dev = &pdev->dev;
494 vp_vdpa->queues = vp_modern_get_num_queues(mdev);
495 vp_vdpa->mdev = mdev;
497 device_features = vp_modern_get_features(mdev);
498 if (add_config->mask & BIT_ULL(VDPA_ATTR_DEV_FEATURES)) {
499 if (add_config->device_features & ~device_features) {
501 dev_err(&pdev->dev, "Try to provision features "
502 "that are not supported by the device: "
503 "device_features 0x%llx provisioned 0x%llx\n",
504 device_features, add_config->device_features);
507 device_features = add_config->device_features;
509 vp_vdpa->device_features = device_features;
511 ret = devm_add_action_or_reset(dev, vp_vdpa_free_irq_vectors, pdev);
514 "Failed for adding devres for freeing irq vectors\n");
518 vp_vdpa->vring = devm_kcalloc(&pdev->dev, vp_vdpa->queues,
519 sizeof(*vp_vdpa->vring),
521 if (!vp_vdpa->vring) {
523 dev_err(&pdev->dev, "Fail to allocate virtqueues\n");
527 for (i = 0; i < vp_vdpa->queues; i++) {
528 vp_vdpa->vring[i].irq = VIRTIO_MSI_NO_VECTOR;
529 vp_vdpa->vring[i].notify =
530 vp_modern_map_vq_notify(mdev, i,
531 &vp_vdpa->vring[i].notify_pa);
532 if (!vp_vdpa->vring[i].notify) {
534 dev_warn(&pdev->dev, "Fail to map vq notify %d\n", i);
538 vp_vdpa->config_irq = VIRTIO_MSI_NO_VECTOR;
540 vp_vdpa->vdpa.mdev = &vp_vdpa_mgtdev->mgtdev;
541 ret = _vdpa_register_device(&vp_vdpa->vdpa, vp_vdpa->queues);
543 dev_err(&pdev->dev, "Failed to register to vdpa bus\n");
550 put_device(&vp_vdpa->vdpa.dev);
554 static void vp_vdpa_dev_del(struct vdpa_mgmt_dev *v_mdev,
555 struct vdpa_device *dev)
557 struct vp_vdpa_mgmtdev *vp_vdpa_mgtdev =
558 container_of(v_mdev, struct vp_vdpa_mgmtdev, mgtdev);
560 struct vp_vdpa *vp_vdpa = vp_vdpa_mgtdev->vp_vdpa;
562 _vdpa_unregister_device(&vp_vdpa->vdpa);
563 vp_vdpa_mgtdev->vp_vdpa = NULL;
566 static const struct vdpa_mgmtdev_ops vp_vdpa_mdev_ops = {
567 .dev_add = vp_vdpa_dev_add,
568 .dev_del = vp_vdpa_dev_del,
571 static int vp_vdpa_probe(struct pci_dev *pdev, const struct pci_device_id *id)
573 struct vp_vdpa_mgmtdev *vp_vdpa_mgtdev = NULL;
574 struct vdpa_mgmt_dev *mgtdev;
575 struct device *dev = &pdev->dev;
576 struct virtio_pci_modern_device *mdev = NULL;
577 struct virtio_device_id *mdev_id = NULL;
580 vp_vdpa_mgtdev = kzalloc(sizeof(*vp_vdpa_mgtdev), GFP_KERNEL);
584 mgtdev = &vp_vdpa_mgtdev->mgtdev;
585 mgtdev->ops = &vp_vdpa_mdev_ops;
586 mgtdev->device = dev;
588 mdev = kzalloc(sizeof(struct virtio_pci_modern_device), GFP_KERNEL);
594 mdev_id = kzalloc(sizeof(struct virtio_device_id), GFP_KERNEL);
600 vp_vdpa_mgtdev->mdev = mdev;
601 mdev->pci_dev = pdev;
603 err = pcim_enable_device(pdev);
608 err = vp_modern_probe(mdev);
610 dev_err(&pdev->dev, "Failed to probe modern PCI device\n");
614 mdev_id->device = mdev->id.device;
615 mdev_id->vendor = mdev->id.vendor;
616 mgtdev->id_table = mdev_id;
617 mgtdev->max_supported_vqs = vp_modern_get_num_queues(mdev);
618 mgtdev->supported_features = vp_modern_get_features(mdev);
619 mgtdev->config_attr_mask = (1 << VDPA_ATTR_DEV_FEATURES);
620 pci_set_master(pdev);
621 pci_set_drvdata(pdev, vp_vdpa_mgtdev);
623 err = vdpa_mgmtdev_register(mgtdev);
625 dev_err(&pdev->dev, "Failed to register vdpa mgmtdev device\n");
632 vp_modern_remove(vp_vdpa_mgtdev->mdev);
638 kfree(vp_vdpa_mgtdev);
642 static void vp_vdpa_remove(struct pci_dev *pdev)
644 struct vp_vdpa_mgmtdev *vp_vdpa_mgtdev = pci_get_drvdata(pdev);
645 struct virtio_pci_modern_device *mdev = NULL;
647 mdev = vp_vdpa_mgtdev->mdev;
648 vdpa_mgmtdev_unregister(&vp_vdpa_mgtdev->mgtdev);
649 vp_modern_remove(mdev);
650 kfree(vp_vdpa_mgtdev->mgtdev.id_table);
652 kfree(vp_vdpa_mgtdev);
655 static struct pci_driver vp_vdpa_driver = {
657 .id_table = NULL, /* only dynamic ids */
658 .probe = vp_vdpa_probe,
659 .remove = vp_vdpa_remove,
662 module_pci_driver(vp_vdpa_driver);
664 MODULE_AUTHOR("Jason Wang <jasowang@redhat.com>");
665 MODULE_DESCRIPTION("vp-vdpa");
666 MODULE_LICENSE("GPL");