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
;
166 for (i
= 0; i
< queues
; i
++) {
167 if (vp_vdpa
->vring
[i
].cb
.callback
)
171 ret
= pci_alloc_irq_vectors(pdev
, vectors
, vectors
, PCI_IRQ_MSIX
);
172 if (ret
!= vectors
) {
174 "vp_vdpa: fail to allocate irq vectors want %d but %d\n",
179 vp_vdpa
->vectors
= vectors
;
181 for (i
= 0; i
< queues
; i
++) {
182 if (!vp_vdpa
->vring
[i
].cb
.callback
)
185 snprintf(vp_vdpa
->vring
[i
].msix_name
, VP_VDPA_NAME_SIZE
,
186 "vp-vdpa[%s]-%d\n", pci_name(pdev
), i
);
187 irq
= pci_irq_vector(pdev
, msix_vec
);
188 ret
= devm_request_irq(&pdev
->dev
, irq
,
190 0, vp_vdpa
->vring
[i
].msix_name
,
194 "vp_vdpa: fail to request irq for vq %d\n", i
);
197 vp_modern_queue_vector(mdev
, i
, msix_vec
);
198 vp_vdpa
->vring
[i
].irq
= irq
;
202 snprintf(vp_vdpa
->msix_name
, VP_VDPA_NAME_SIZE
, "vp-vdpa[%s]-config\n",
204 irq
= pci_irq_vector(pdev
, msix_vec
);
205 ret
= devm_request_irq(&pdev
->dev
, irq
, vp_vdpa_config_handler
, 0,
206 vp_vdpa
->msix_name
, vp_vdpa
);
209 "vp_vdpa: fail to request irq for config: %d\n", ret
);
212 vp_modern_config_vector(mdev
, msix_vec
);
213 vp_vdpa
->config_irq
= irq
;
217 vp_vdpa_free_irq(vp_vdpa
);
221 static void vp_vdpa_set_status(struct vdpa_device
*vdpa
, u8 status
)
223 struct vp_vdpa
*vp_vdpa
= vdpa_to_vp(vdpa
);
224 struct virtio_pci_modern_device
*mdev
= vp_vdpa_to_mdev(vp_vdpa
);
225 u8 s
= vp_vdpa_get_status(vdpa
);
227 if (status
& VIRTIO_CONFIG_S_DRIVER_OK
&&
228 !(s
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
229 if (vp_vdpa_request_irq(vp_vdpa
)) {
235 vp_modern_set_status(mdev
, status
);
238 static int vp_vdpa_reset(struct vdpa_device
*vdpa
)
240 struct vp_vdpa
*vp_vdpa
= vdpa_to_vp(vdpa
);
241 struct virtio_pci_modern_device
*mdev
= vp_vdpa_to_mdev(vp_vdpa
);
242 u8 s
= vp_vdpa_get_status(vdpa
);
244 vp_modern_set_status(mdev
, 0);
246 if (s
& VIRTIO_CONFIG_S_DRIVER_OK
)
247 vp_vdpa_free_irq(vp_vdpa
);
252 static u16
vp_vdpa_get_vq_num_max(struct vdpa_device
*vdpa
)
254 return VP_VDPA_QUEUE_MAX
;
257 static int vp_vdpa_get_vq_state(struct vdpa_device
*vdpa
, u16 qid
,
258 struct vdpa_vq_state
*state
)
260 /* Note that this is not supported by virtio specification, so
261 * we return -EOPNOTSUPP here. This means we can't support live
262 * migration, vhost device start/stop.
267 static int vp_vdpa_set_vq_state_split(struct vdpa_device
*vdpa
,
268 const struct vdpa_vq_state
*state
)
270 const struct vdpa_vq_state_split
*split
= &state
->split
;
272 if (split
->avail_index
== 0)
278 static int vp_vdpa_set_vq_state_packed(struct vdpa_device
*vdpa
,
279 const struct vdpa_vq_state
*state
)
281 const struct vdpa_vq_state_packed
*packed
= &state
->packed
;
283 if (packed
->last_avail_counter
== 1 &&
284 packed
->last_avail_idx
== 0 &&
285 packed
->last_used_counter
== 1 &&
286 packed
->last_used_idx
== 0)
292 static int vp_vdpa_set_vq_state(struct vdpa_device
*vdpa
, u16 qid
,
293 const struct vdpa_vq_state
*state
)
295 struct virtio_pci_modern_device
*mdev
= vdpa_to_mdev(vdpa
);
297 /* Note that this is not supported by virtio specification.
298 * But if the state is by chance equal to the device initial
299 * state, we can let it go.
301 if ((vp_modern_get_status(mdev
) & VIRTIO_CONFIG_S_FEATURES_OK
) &&
302 !vp_modern_get_queue_enable(mdev
, qid
)) {
303 if (vp_modern_get_driver_features(mdev
) &
304 BIT_ULL(VIRTIO_F_RING_PACKED
))
305 return vp_vdpa_set_vq_state_packed(vdpa
, state
);
307 return vp_vdpa_set_vq_state_split(vdpa
, state
);
313 static void vp_vdpa_set_vq_cb(struct vdpa_device
*vdpa
, u16 qid
,
314 struct vdpa_callback
*cb
)
316 struct vp_vdpa
*vp_vdpa
= vdpa_to_vp(vdpa
);
318 vp_vdpa
->vring
[qid
].cb
= *cb
;
321 static void vp_vdpa_set_vq_ready(struct vdpa_device
*vdpa
,
324 struct virtio_pci_modern_device
*mdev
= vdpa_to_mdev(vdpa
);
326 vp_modern_set_queue_enable(mdev
, qid
, ready
);
329 static bool vp_vdpa_get_vq_ready(struct vdpa_device
*vdpa
, u16 qid
)
331 struct virtio_pci_modern_device
*mdev
= vdpa_to_mdev(vdpa
);
333 return vp_modern_get_queue_enable(mdev
, qid
);
336 static void vp_vdpa_set_vq_num(struct vdpa_device
*vdpa
, u16 qid
,
339 struct virtio_pci_modern_device
*mdev
= vdpa_to_mdev(vdpa
);
341 vp_modern_set_queue_size(mdev
, qid
, num
);
344 static u16
vp_vdpa_get_vq_size(struct vdpa_device
*vdpa
, u16 qid
)
346 struct virtio_pci_modern_device
*mdev
= vdpa_to_mdev(vdpa
);
348 return vp_modern_get_queue_size(mdev
, qid
);
351 static int vp_vdpa_set_vq_address(struct vdpa_device
*vdpa
, u16 qid
,
352 u64 desc_area
, u64 driver_area
,
355 struct virtio_pci_modern_device
*mdev
= vdpa_to_mdev(vdpa
);
357 vp_modern_queue_address(mdev
, qid
, desc_area
,
358 driver_area
, device_area
);
363 static void vp_vdpa_kick_vq(struct vdpa_device
*vdpa
, u16 qid
)
365 struct vp_vdpa
*vp_vdpa
= vdpa_to_vp(vdpa
);
367 vp_iowrite16(qid
, vp_vdpa
->vring
[qid
].notify
);
370 static u32
vp_vdpa_get_generation(struct vdpa_device
*vdpa
)
372 struct virtio_pci_modern_device
*mdev
= vdpa_to_mdev(vdpa
);
374 return vp_modern_generation(mdev
);
377 static u32
vp_vdpa_get_device_id(struct vdpa_device
*vdpa
)
379 struct virtio_pci_modern_device
*mdev
= vdpa_to_mdev(vdpa
);
381 return mdev
->id
.device
;
384 static u32
vp_vdpa_get_vendor_id(struct vdpa_device
*vdpa
)
386 struct virtio_pci_modern_device
*mdev
= vdpa_to_mdev(vdpa
);
388 return mdev
->id
.vendor
;
391 static u32
vp_vdpa_get_vq_align(struct vdpa_device
*vdpa
)
396 static size_t vp_vdpa_get_config_size(struct vdpa_device
*vdpa
)
398 struct virtio_pci_modern_device
*mdev
= vdpa_to_mdev(vdpa
);
400 return mdev
->device_len
;
403 static void vp_vdpa_get_config(struct vdpa_device
*vdpa
,
405 void *buf
, unsigned int len
)
407 struct vp_vdpa
*vp_vdpa
= vdpa_to_vp(vdpa
);
408 struct virtio_pci_modern_device
*mdev
= vp_vdpa_to_mdev(vp_vdpa
);
414 old
= vp_ioread8(&mdev
->common
->config_generation
);
416 for (i
= 0; i
< len
; i
++)
417 *p
++ = vp_ioread8(mdev
->device
+ offset
+ i
);
419 new = vp_ioread8(&mdev
->common
->config_generation
);
420 } while (old
!= new);
423 static void vp_vdpa_set_config(struct vdpa_device
*vdpa
,
424 unsigned int offset
, const void *buf
,
427 struct vp_vdpa
*vp_vdpa
= vdpa_to_vp(vdpa
);
428 struct virtio_pci_modern_device
*mdev
= vp_vdpa_to_mdev(vp_vdpa
);
432 for (i
= 0; i
< len
; i
++)
433 vp_iowrite8(*p
++, mdev
->device
+ offset
+ i
);
436 static void vp_vdpa_set_config_cb(struct vdpa_device
*vdpa
,
437 struct vdpa_callback
*cb
)
439 struct vp_vdpa
*vp_vdpa
= vdpa_to_vp(vdpa
);
441 vp_vdpa
->config_cb
= *cb
;
444 static struct vdpa_notification_area
445 vp_vdpa_get_vq_notification(struct vdpa_device
*vdpa
, u16 qid
)
447 struct vp_vdpa
*vp_vdpa
= vdpa_to_vp(vdpa
);
448 struct virtio_pci_modern_device
*mdev
= vp_vdpa_to_mdev(vp_vdpa
);
449 struct vdpa_notification_area notify
;
451 notify
.addr
= vp_vdpa
->vring
[qid
].notify_pa
;
452 notify
.size
= mdev
->notify_offset_multiplier
;
457 static const struct vdpa_config_ops vp_vdpa_ops
= {
458 .get_device_features
= vp_vdpa_get_device_features
,
459 .set_driver_features
= vp_vdpa_set_driver_features
,
460 .get_driver_features
= vp_vdpa_get_driver_features
,
461 .get_status
= vp_vdpa_get_status
,
462 .set_status
= vp_vdpa_set_status
,
463 .reset
= vp_vdpa_reset
,
464 .get_vq_num_max
= vp_vdpa_get_vq_num_max
,
465 .get_vq_state
= vp_vdpa_get_vq_state
,
466 .get_vq_notification
= vp_vdpa_get_vq_notification
,
467 .set_vq_state
= vp_vdpa_set_vq_state
,
468 .set_vq_cb
= vp_vdpa_set_vq_cb
,
469 .set_vq_ready
= vp_vdpa_set_vq_ready
,
470 .get_vq_ready
= vp_vdpa_get_vq_ready
,
471 .set_vq_num
= vp_vdpa_set_vq_num
,
472 .get_vq_size
= vp_vdpa_get_vq_size
,
473 .set_vq_address
= vp_vdpa_set_vq_address
,
474 .kick_vq
= vp_vdpa_kick_vq
,
475 .get_generation
= vp_vdpa_get_generation
,
476 .get_device_id
= vp_vdpa_get_device_id
,
477 .get_vendor_id
= vp_vdpa_get_vendor_id
,
478 .get_vq_align
= vp_vdpa_get_vq_align
,
479 .get_config_size
= vp_vdpa_get_config_size
,
480 .get_config
= vp_vdpa_get_config
,
481 .set_config
= vp_vdpa_set_config
,
482 .set_config_cb
= vp_vdpa_set_config_cb
,
483 .get_vq_irq
= vp_vdpa_get_vq_irq
,
486 static void vp_vdpa_free_irq_vectors(void *data
)
488 pci_free_irq_vectors(data
);
491 static int vp_vdpa_dev_add(struct vdpa_mgmt_dev
*v_mdev
, const char *name
,
492 const struct vdpa_dev_set_config
*add_config
)
494 struct vp_vdpa_mgmtdev
*vp_vdpa_mgtdev
=
495 container_of(v_mdev
, struct vp_vdpa_mgmtdev
, mgtdev
);
497 struct virtio_pci_modern_device
*mdev
= vp_vdpa_mgtdev
->mdev
;
498 struct pci_dev
*pdev
= mdev
->pci_dev
;
499 struct device
*dev
= &pdev
->dev
;
500 struct vp_vdpa
*vp_vdpa
= NULL
;
504 vp_vdpa
= vdpa_alloc_device(struct vp_vdpa
, vdpa
,
505 dev
, &vp_vdpa_ops
, 1, 1, name
, false);
507 if (IS_ERR(vp_vdpa
)) {
508 dev_err(dev
, "vp_vdpa: Failed to allocate vDPA structure\n");
509 return PTR_ERR(vp_vdpa
);
512 vp_vdpa_mgtdev
->vp_vdpa
= vp_vdpa
;
514 vp_vdpa
->vdpa
.dma_dev
= &pdev
->dev
;
515 vp_vdpa
->queues
= vp_modern_get_num_queues(mdev
);
516 vp_vdpa
->mdev
= mdev
;
518 device_features
= vp_modern_get_features(mdev
);
519 if (add_config
->mask
& BIT_ULL(VDPA_ATTR_DEV_FEATURES
)) {
520 if (add_config
->device_features
& ~device_features
) {
522 dev_err(&pdev
->dev
, "Try to provision features "
523 "that are not supported by the device: "
524 "device_features 0x%llx provisioned 0x%llx\n",
525 device_features
, add_config
->device_features
);
528 device_features
= add_config
->device_features
;
530 vp_vdpa
->device_features
= device_features
;
532 ret
= devm_add_action_or_reset(dev
, vp_vdpa_free_irq_vectors
, pdev
);
535 "Failed for adding devres for freeing irq vectors\n");
539 vp_vdpa
->vring
= devm_kcalloc(&pdev
->dev
, vp_vdpa
->queues
,
540 sizeof(*vp_vdpa
->vring
),
542 if (!vp_vdpa
->vring
) {
544 dev_err(&pdev
->dev
, "Fail to allocate virtqueues\n");
548 for (i
= 0; i
< vp_vdpa
->queues
; i
++) {
549 vp_vdpa
->vring
[i
].irq
= VIRTIO_MSI_NO_VECTOR
;
550 vp_vdpa
->vring
[i
].notify
=
551 vp_modern_map_vq_notify(mdev
, i
,
552 &vp_vdpa
->vring
[i
].notify_pa
);
553 if (!vp_vdpa
->vring
[i
].notify
) {
555 dev_warn(&pdev
->dev
, "Fail to map vq notify %d\n", i
);
559 vp_vdpa
->config_irq
= VIRTIO_MSI_NO_VECTOR
;
561 vp_vdpa
->vdpa
.mdev
= &vp_vdpa_mgtdev
->mgtdev
;
562 ret
= _vdpa_register_device(&vp_vdpa
->vdpa
, vp_vdpa
->queues
);
564 dev_err(&pdev
->dev
, "Failed to register to vdpa bus\n");
571 put_device(&vp_vdpa
->vdpa
.dev
);
575 static void vp_vdpa_dev_del(struct vdpa_mgmt_dev
*v_mdev
,
576 struct vdpa_device
*dev
)
578 struct vp_vdpa_mgmtdev
*vp_vdpa_mgtdev
=
579 container_of(v_mdev
, struct vp_vdpa_mgmtdev
, mgtdev
);
581 struct vp_vdpa
*vp_vdpa
= vp_vdpa_mgtdev
->vp_vdpa
;
583 _vdpa_unregister_device(&vp_vdpa
->vdpa
);
584 vp_vdpa_mgtdev
->vp_vdpa
= NULL
;
587 static const struct vdpa_mgmtdev_ops vp_vdpa_mdev_ops
= {
588 .dev_add
= vp_vdpa_dev_add
,
589 .dev_del
= vp_vdpa_dev_del
,
592 static int vp_vdpa_probe(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
594 struct vp_vdpa_mgmtdev
*vp_vdpa_mgtdev
= NULL
;
595 struct vdpa_mgmt_dev
*mgtdev
;
596 struct device
*dev
= &pdev
->dev
;
597 struct virtio_pci_modern_device
*mdev
= NULL
;
598 struct virtio_device_id
*mdev_id
= NULL
;
601 vp_vdpa_mgtdev
= kzalloc(sizeof(*vp_vdpa_mgtdev
), GFP_KERNEL
);
605 mgtdev
= &vp_vdpa_mgtdev
->mgtdev
;
606 mgtdev
->ops
= &vp_vdpa_mdev_ops
;
607 mgtdev
->device
= dev
;
609 mdev
= kzalloc(sizeof(struct virtio_pci_modern_device
), GFP_KERNEL
);
616 * id_table should be a null terminated array, so allocate one additional
617 * entry here, see vdpa_mgmtdev_get_classes().
619 mdev_id
= kcalloc(2, sizeof(struct virtio_device_id
), GFP_KERNEL
);
625 vp_vdpa_mgtdev
->mdev
= mdev
;
626 mdev
->pci_dev
= pdev
;
628 err
= pcim_enable_device(pdev
);
633 err
= vp_modern_probe(mdev
);
635 dev_err(&pdev
->dev
, "Failed to probe modern PCI device\n");
639 mdev_id
[0].device
= mdev
->id
.device
;
640 mdev_id
[0].vendor
= mdev
->id
.vendor
;
641 mgtdev
->id_table
= mdev_id
;
642 mgtdev
->max_supported_vqs
= vp_modern_get_num_queues(mdev
);
643 mgtdev
->supported_features
= vp_modern_get_features(mdev
);
644 mgtdev
->config_attr_mask
= (1 << VDPA_ATTR_DEV_FEATURES
);
645 pci_set_master(pdev
);
646 pci_set_drvdata(pdev
, vp_vdpa_mgtdev
);
648 err
= vdpa_mgmtdev_register(mgtdev
);
650 dev_err(&pdev
->dev
, "Failed to register vdpa mgmtdev device\n");
657 vp_modern_remove(vp_vdpa_mgtdev
->mdev
);
663 kfree(vp_vdpa_mgtdev
);
667 static void vp_vdpa_remove(struct pci_dev
*pdev
)
669 struct vp_vdpa_mgmtdev
*vp_vdpa_mgtdev
= pci_get_drvdata(pdev
);
670 struct virtio_pci_modern_device
*mdev
= NULL
;
672 mdev
= vp_vdpa_mgtdev
->mdev
;
673 vdpa_mgmtdev_unregister(&vp_vdpa_mgtdev
->mgtdev
);
674 vp_modern_remove(mdev
);
675 kfree(vp_vdpa_mgtdev
->mgtdev
.id_table
);
677 kfree(vp_vdpa_mgtdev
);
680 static struct pci_driver vp_vdpa_driver
= {
682 .id_table
= NULL
, /* only dynamic ids */
683 .probe
= vp_vdpa_probe
,
684 .remove
= vp_vdpa_remove
,
687 module_pci_driver(vp_vdpa_driver
);
689 MODULE_AUTHOR("Jason Wang <jasowang@redhat.com>");
690 MODULE_DESCRIPTION("vp-vdpa");
691 MODULE_LICENSE("GPL");