2019-05-22 07:51:24 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2014-12-11 11:59:51 +00:00
|
|
|
/*
|
|
|
|
* Virtio PCI driver - modern (virtio 1.0) device support
|
|
|
|
*
|
|
|
|
* This module allows virtio devices to be used over a virtual PCI device.
|
|
|
|
* This can be used with QEMU based VMMs like KVM or Xen.
|
|
|
|
*
|
|
|
|
* Copyright IBM Corp. 2007
|
|
|
|
* Copyright Red Hat, Inc. 2014
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Anthony Liguori <aliguori@us.ibm.com>
|
|
|
|
* Rusty Russell <rusty@rustcorp.com.au>
|
|
|
|
* Michael S. Tsirkin <mst@redhat.com>
|
|
|
|
*/
|
|
|
|
|
2016-04-03 12:23:37 +00:00
|
|
|
#include <linux/delay.h>
|
2014-12-11 11:59:51 +00:00
|
|
|
#define VIRTIO_PCI_NO_LEGACY
|
2019-09-11 12:49:53 +00:00
|
|
|
#define VIRTIO_RING_NO_LEGACY
|
2014-12-11 11:59:51 +00:00
|
|
|
#include "virtio_pci_common.h"
|
|
|
|
|
2021-01-04 06:54:51 +00:00
|
|
|
static u64 vp_get_features(struct virtio_device *vdev)
|
|
|
|
{
|
|
|
|
struct virtio_pci_device *vp_dev = to_vp_device(vdev);
|
|
|
|
|
|
|
|
return vp_modern_get_features(&vp_dev->mdev);
|
|
|
|
}
|
|
|
|
|
2018-06-01 04:02:39 +00:00
|
|
|
static void vp_transport_features(struct virtio_device *vdev, u64 features)
|
|
|
|
{
|
|
|
|
struct virtio_pci_device *vp_dev = to_vp_device(vdev);
|
|
|
|
struct pci_dev *pci_dev = vp_dev->pci_dev;
|
|
|
|
|
|
|
|
if ((features & BIT_ULL(VIRTIO_F_SR_IOV)) &&
|
|
|
|
pci_find_ext_capability(pci_dev, PCI_EXT_CAP_ID_SRIOV))
|
|
|
|
__virtio_set_bit(vdev, VIRTIO_F_SR_IOV);
|
2022-08-01 06:38:52 +00:00
|
|
|
|
|
|
|
if (features & BIT_ULL(VIRTIO_F_RING_RESET))
|
|
|
|
__virtio_set_bit(vdev, VIRTIO_F_RING_RESET);
|
2018-06-01 04:02:39 +00:00
|
|
|
}
|
|
|
|
|
2014-12-11 11:59:51 +00:00
|
|
|
/* virtio config->finalize_features() implementation */
|
|
|
|
static int vp_finalize_features(struct virtio_device *vdev)
|
|
|
|
{
|
|
|
|
struct virtio_pci_device *vp_dev = to_vp_device(vdev);
|
2018-06-01 04:02:39 +00:00
|
|
|
u64 features = vdev->features;
|
2014-12-11 11:59:51 +00:00
|
|
|
|
|
|
|
/* Give virtio_ring a chance to accept features. */
|
|
|
|
vring_transport_features(vdev);
|
|
|
|
|
2018-06-01 04:02:39 +00:00
|
|
|
/* Give virtio_pci a chance to accept features. */
|
|
|
|
vp_transport_features(vdev, features);
|
|
|
|
|
2014-12-11 11:59:51 +00:00
|
|
|
if (!__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) {
|
|
|
|
dev_err(&vdev->dev, "virtio: device uses modern interface "
|
|
|
|
"but does not have VIRTIO_F_VERSION_1\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2021-01-04 06:54:51 +00:00
|
|
|
vp_modern_set_features(&vp_dev->mdev, vdev->features);
|
2014-12-11 11:59:51 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* virtio config->get() implementation */
|
2022-04-18 02:54:35 +00:00
|
|
|
static void vp_get(struct virtio_device *vdev, unsigned int offset,
|
|
|
|
void *buf, unsigned int len)
|
2014-12-11 11:59:51 +00:00
|
|
|
{
|
|
|
|
struct virtio_pci_device *vp_dev = to_vp_device(vdev);
|
2021-01-04 06:54:46 +00:00
|
|
|
struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
|
|
|
|
void __iomem *device = mdev->device;
|
2014-12-11 11:59:51 +00:00
|
|
|
u8 b;
|
|
|
|
__le16 w;
|
|
|
|
__le32 l;
|
|
|
|
|
2021-01-04 06:54:46 +00:00
|
|
|
BUG_ON(offset + len > mdev->device_len);
|
2014-12-11 11:59:51 +00:00
|
|
|
|
|
|
|
switch (len) {
|
|
|
|
case 1:
|
2021-01-04 06:54:45 +00:00
|
|
|
b = ioread8(device + offset);
|
2014-12-11 11:59:51 +00:00
|
|
|
memcpy(buf, &b, sizeof b);
|
|
|
|
break;
|
|
|
|
case 2:
|
2021-01-04 06:54:45 +00:00
|
|
|
w = cpu_to_le16(ioread16(device + offset));
|
2014-12-11 11:59:51 +00:00
|
|
|
memcpy(buf, &w, sizeof w);
|
|
|
|
break;
|
|
|
|
case 4:
|
2021-01-04 06:54:45 +00:00
|
|
|
l = cpu_to_le32(ioread32(device + offset));
|
2014-12-11 11:59:51 +00:00
|
|
|
memcpy(buf, &l, sizeof l);
|
|
|
|
break;
|
|
|
|
case 8:
|
2021-01-04 06:54:45 +00:00
|
|
|
l = cpu_to_le32(ioread32(device + offset));
|
2014-12-11 11:59:51 +00:00
|
|
|
memcpy(buf, &l, sizeof l);
|
2021-01-04 06:54:45 +00:00
|
|
|
l = cpu_to_le32(ioread32(device + offset + sizeof l));
|
2014-12-11 11:59:51 +00:00
|
|
|
memcpy(buf + sizeof l, &l, sizeof l);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
BUG();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* the config->set() implementation. it's symmetric to the config->get()
|
|
|
|
* implementation */
|
2022-04-18 02:54:35 +00:00
|
|
|
static void vp_set(struct virtio_device *vdev, unsigned int offset,
|
|
|
|
const void *buf, unsigned int len)
|
2014-12-11 11:59:51 +00:00
|
|
|
{
|
|
|
|
struct virtio_pci_device *vp_dev = to_vp_device(vdev);
|
2021-01-04 06:54:46 +00:00
|
|
|
struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
|
|
|
|
void __iomem *device = mdev->device;
|
2014-12-11 11:59:51 +00:00
|
|
|
u8 b;
|
|
|
|
__le16 w;
|
|
|
|
__le32 l;
|
|
|
|
|
2021-01-04 06:54:46 +00:00
|
|
|
BUG_ON(offset + len > mdev->device_len);
|
2014-12-11 11:59:51 +00:00
|
|
|
|
|
|
|
switch (len) {
|
|
|
|
case 1:
|
|
|
|
memcpy(&b, buf, sizeof b);
|
2021-01-04 06:54:45 +00:00
|
|
|
iowrite8(b, device + offset);
|
2014-12-11 11:59:51 +00:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
memcpy(&w, buf, sizeof w);
|
2021-01-04 06:54:45 +00:00
|
|
|
iowrite16(le16_to_cpu(w), device + offset);
|
2014-12-11 11:59:51 +00:00
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
memcpy(&l, buf, sizeof l);
|
2021-01-04 06:54:45 +00:00
|
|
|
iowrite32(le32_to_cpu(l), device + offset);
|
2014-12-11 11:59:51 +00:00
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
memcpy(&l, buf, sizeof l);
|
2021-01-04 06:54:45 +00:00
|
|
|
iowrite32(le32_to_cpu(l), device + offset);
|
2014-12-11 11:59:51 +00:00
|
|
|
memcpy(&l, buf + sizeof l, sizeof l);
|
2021-01-04 06:54:45 +00:00
|
|
|
iowrite32(le32_to_cpu(l), device + offset + sizeof l);
|
2014-12-11 11:59:51 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
BUG();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-04 06:54:52 +00:00
|
|
|
static u32 vp_generation(struct virtio_device *vdev)
|
|
|
|
{
|
|
|
|
struct virtio_pci_device *vp_dev = to_vp_device(vdev);
|
|
|
|
|
|
|
|
return vp_modern_generation(&vp_dev->mdev);
|
2014-12-11 11:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* config->{get,set}_status() implementations */
|
|
|
|
static u8 vp_get_status(struct virtio_device *vdev)
|
|
|
|
{
|
|
|
|
struct virtio_pci_device *vp_dev = to_vp_device(vdev);
|
2021-01-04 06:54:50 +00:00
|
|
|
|
|
|
|
return vp_modern_get_status(&vp_dev->mdev);
|
|
|
|
}
|
|
|
|
|
2014-12-11 11:59:51 +00:00
|
|
|
static void vp_set_status(struct virtio_device *vdev, u8 status)
|
|
|
|
{
|
|
|
|
struct virtio_pci_device *vp_dev = to_vp_device(vdev);
|
2021-01-04 06:54:45 +00:00
|
|
|
|
2014-12-11 11:59:51 +00:00
|
|
|
/* We should never be setting status to 0. */
|
|
|
|
BUG_ON(status == 0);
|
2021-01-04 06:54:50 +00:00
|
|
|
vp_modern_set_status(&vp_dev->mdev, status);
|
2014-12-11 11:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void vp_reset(struct virtio_device *vdev)
|
|
|
|
{
|
|
|
|
struct virtio_pci_device *vp_dev = to_vp_device(vdev);
|
2021-01-04 06:54:46 +00:00
|
|
|
struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
|
2021-01-04 06:54:45 +00:00
|
|
|
|
2014-12-11 11:59:51 +00:00
|
|
|
/* 0 status means a reset. */
|
2021-01-04 06:54:50 +00:00
|
|
|
vp_modern_set_status(mdev, 0);
|
2016-04-03 12:23:37 +00:00
|
|
|
/* After writing 0 to device_status, the driver MUST wait for a read of
|
|
|
|
* device_status to return 0 before reinitializing the device.
|
|
|
|
* This will flush out the status write, and flush in device writes,
|
|
|
|
* including MSI-X interrupts, if any.
|
|
|
|
*/
|
2021-01-04 06:54:50 +00:00
|
|
|
while (vp_modern_get_status(mdev))
|
2016-04-03 12:23:37 +00:00
|
|
|
msleep(1);
|
2022-03-23 03:15:24 +00:00
|
|
|
/* Flush pending VQ/configuration callbacks. */
|
|
|
|
vp_synchronize_vectors(vdev);
|
2014-12-11 11:59:51 +00:00
|
|
|
}
|
|
|
|
|
2022-08-01 06:38:51 +00:00
|
|
|
static int vp_active_vq(struct virtqueue *vq, u16 msix_vec)
|
|
|
|
{
|
|
|
|
struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
|
|
|
|
struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
|
|
|
|
unsigned long index;
|
|
|
|
|
|
|
|
index = vq->index;
|
|
|
|
|
|
|
|
/* activate the queue */
|
|
|
|
vp_modern_set_queue_size(mdev, index, virtqueue_get_vring_size(vq));
|
|
|
|
vp_modern_queue_address(mdev, index, virtqueue_get_desc_addr(vq),
|
|
|
|
virtqueue_get_avail_addr(vq),
|
|
|
|
virtqueue_get_used_addr(vq));
|
|
|
|
|
|
|
|
if (msix_vec != VIRTIO_MSI_NO_VECTOR) {
|
|
|
|
msix_vec = vp_modern_queue_vector(mdev, index, msix_vec);
|
|
|
|
if (msix_vec == VIRTIO_MSI_NO_VECTOR)
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-08-01 06:38:52 +00:00
|
|
|
static int vp_modern_disable_vq_and_reset(struct virtqueue *vq)
|
|
|
|
{
|
|
|
|
struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
|
|
|
|
struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
|
|
|
|
struct virtio_pci_vq_info *info;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
if (!virtio_has_feature(vq->vdev, VIRTIO_F_RING_RESET))
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
vp_modern_set_queue_reset(mdev, vq->index);
|
|
|
|
|
|
|
|
info = vp_dev->vqs[vq->index];
|
|
|
|
|
|
|
|
/* delete vq from irq handler */
|
|
|
|
spin_lock_irqsave(&vp_dev->lock, flags);
|
|
|
|
list_del(&info->node);
|
|
|
|
spin_unlock_irqrestore(&vp_dev->lock, flags);
|
|
|
|
|
|
|
|
INIT_LIST_HEAD(&info->node);
|
|
|
|
|
|
|
|
#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
|
|
|
|
__virtqueue_break(vq);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* For the case where vq has an exclusive irq, call synchronize_irq() to
|
|
|
|
* wait for completion.
|
|
|
|
*
|
|
|
|
* note: We can't use disable_irq() since it conflicts with the affinity
|
|
|
|
* managed IRQ that is used by some drivers.
|
|
|
|
*/
|
|
|
|
if (vp_dev->per_vq_vectors && info->msix_vector != VIRTIO_MSI_NO_VECTOR)
|
|
|
|
synchronize_irq(pci_irq_vector(vp_dev->pci_dev, info->msix_vector));
|
|
|
|
|
|
|
|
vq->reset = true;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int vp_modern_enable_vq_after_reset(struct virtqueue *vq)
|
|
|
|
{
|
|
|
|
struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
|
|
|
|
struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
|
|
|
|
struct virtio_pci_vq_info *info;
|
|
|
|
unsigned long flags, index;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (!vq->reset)
|
|
|
|
return -EBUSY;
|
|
|
|
|
|
|
|
index = vq->index;
|
|
|
|
info = vp_dev->vqs[index];
|
|
|
|
|
|
|
|
if (vp_modern_get_queue_reset(mdev, index))
|
|
|
|
return -EBUSY;
|
|
|
|
|
|
|
|
if (vp_modern_get_queue_enable(mdev, index))
|
|
|
|
return -EBUSY;
|
|
|
|
|
|
|
|
err = vp_active_vq(vq, info->msix_vector);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
if (vq->callback) {
|
|
|
|
spin_lock_irqsave(&vp_dev->lock, flags);
|
|
|
|
list_add(&info->node, &vp_dev->virtqueues);
|
|
|
|
spin_unlock_irqrestore(&vp_dev->lock, flags);
|
|
|
|
} else {
|
|
|
|
INIT_LIST_HEAD(&info->node);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
|
|
|
|
__virtqueue_unbreak(vq);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
vp_modern_set_queue_enable(&vp_dev->mdev, index, true);
|
|
|
|
vq->reset = false;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-01-04 06:54:49 +00:00
|
|
|
static u16 vp_config_vector(struct virtio_pci_device *vp_dev, u16 vector)
|
|
|
|
{
|
|
|
|
return vp_modern_config_vector(&vp_dev->mdev, vector);
|
|
|
|
}
|
|
|
|
|
2014-12-11 11:59:51 +00:00
|
|
|
static struct virtqueue *setup_vq(struct virtio_pci_device *vp_dev,
|
2017-04-04 18:44:44 +00:00
|
|
|
struct virtio_pci_vq_info *info,
|
2022-04-18 02:54:35 +00:00
|
|
|
unsigned int index,
|
2014-12-11 11:59:51 +00:00
|
|
|
void (*callback)(struct virtqueue *vq),
|
|
|
|
const char *name,
|
2017-03-06 16:32:29 +00:00
|
|
|
bool ctx,
|
2014-12-11 11:59:51 +00:00
|
|
|
u16 msix_vec)
|
|
|
|
{
|
2021-01-04 06:54:46 +00:00
|
|
|
|
|
|
|
struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
|
2014-12-11 11:59:51 +00:00
|
|
|
struct virtqueue *vq;
|
2021-04-15 07:31:42 +00:00
|
|
|
u16 num;
|
2014-12-11 11:59:51 +00:00
|
|
|
int err;
|
|
|
|
|
2021-01-04 06:54:57 +00:00
|
|
|
if (index >= vp_modern_get_num_queues(mdev))
|
2014-12-11 11:59:51 +00:00
|
|
|
return ERR_PTR(-ENOENT);
|
|
|
|
|
|
|
|
/* Check if queue is either not available or already active. */
|
2021-01-04 06:54:56 +00:00
|
|
|
num = vp_modern_get_queue_size(mdev, index);
|
2021-01-04 06:54:55 +00:00
|
|
|
if (!num || vp_modern_get_queue_enable(mdev, index))
|
2014-12-11 11:59:51 +00:00
|
|
|
return ERR_PTR(-ENOENT);
|
|
|
|
|
2022-08-16 05:36:40 +00:00
|
|
|
if (num & (num - 1)) {
|
|
|
|
dev_warn(&vp_dev->pci_dev->dev, "bad queue size %u", num);
|
2014-12-11 11:59:51 +00:00
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
}
|
|
|
|
|
2017-04-04 18:44:44 +00:00
|
|
|
info->msix_vector = msix_vec;
|
|
|
|
|
2014-12-11 11:59:51 +00:00
|
|
|
/* create the vring */
|
2022-08-16 05:36:40 +00:00
|
|
|
vq = vring_create_virtqueue(index, num,
|
2016-02-03 05:46:39 +00:00
|
|
|
SMP_CACHE_BYTES, &vp_dev->vdev,
|
2017-03-06 16:32:29 +00:00
|
|
|
true, true, ctx,
|
|
|
|
vp_notify, callback, name);
|
2016-02-03 05:46:39 +00:00
|
|
|
if (!vq)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
2014-12-11 11:59:51 +00:00
|
|
|
|
2022-08-01 06:38:21 +00:00
|
|
|
vq->num_max = num;
|
|
|
|
|
2022-08-01 06:38:51 +00:00
|
|
|
err = vp_active_vq(vq, msix_vec);
|
|
|
|
if (err)
|
|
|
|
goto err;
|
2014-12-11 11:59:51 +00:00
|
|
|
|
2021-05-04 08:12:10 +00:00
|
|
|
vq->priv = (void __force *)vp_modern_map_vq_notify(mdev, index, NULL);
|
2014-12-11 11:59:51 +00:00
|
|
|
if (!vq->priv) {
|
|
|
|
err = -ENOMEM;
|
2022-08-01 06:38:51 +00:00
|
|
|
goto err;
|
2014-12-11 11:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return vq;
|
|
|
|
|
2022-08-01 06:38:51 +00:00
|
|
|
err:
|
2014-12-11 11:59:51 +00:00
|
|
|
vring_del_virtqueue(vq);
|
|
|
|
return ERR_PTR(err);
|
|
|
|
}
|
|
|
|
|
2022-04-18 02:54:35 +00:00
|
|
|
static int vp_modern_find_vqs(struct virtio_device *vdev, unsigned int nvqs,
|
2017-03-06 16:32:29 +00:00
|
|
|
struct virtqueue *vqs[],
|
|
|
|
vq_callback_t *callbacks[],
|
2022-08-16 05:36:58 +00:00
|
|
|
const char * const names[], const bool *ctx,
|
2017-03-06 16:32:29 +00:00
|
|
|
struct irq_affinity *desc)
|
2014-12-11 11:59:51 +00:00
|
|
|
{
|
|
|
|
struct virtio_pci_device *vp_dev = to_vp_device(vdev);
|
|
|
|
struct virtqueue *vq;
|
2022-08-16 05:36:58 +00:00
|
|
|
int rc = vp_find_vqs(vdev, nvqs, vqs, callbacks, names, ctx, desc);
|
2014-12-11 11:59:51 +00:00
|
|
|
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
/* Select and activate all queues. Has to be done last: once we do
|
|
|
|
* this, there's no way to go back except reset.
|
|
|
|
*/
|
2021-01-04 06:54:55 +00:00
|
|
|
list_for_each_entry(vq, &vdev->vqs, list)
|
|
|
|
vp_modern_set_queue_enable(&vp_dev->mdev, vq->index, true);
|
2014-12-11 11:59:51 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-04-04 18:44:44 +00:00
|
|
|
static void del_vq(struct virtio_pci_vq_info *info)
|
2014-12-11 11:59:51 +00:00
|
|
|
{
|
2017-04-04 18:44:44 +00:00
|
|
|
struct virtqueue *vq = info->vq;
|
2014-12-11 11:59:51 +00:00
|
|
|
struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
|
2021-01-04 06:54:46 +00:00
|
|
|
struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
|
2014-12-11 11:59:51 +00:00
|
|
|
|
2021-01-04 06:54:53 +00:00
|
|
|
if (vp_dev->msix_enabled)
|
|
|
|
vp_modern_queue_vector(mdev, vq->index,
|
|
|
|
VIRTIO_MSI_NO_VECTOR);
|
2014-12-11 11:59:51 +00:00
|
|
|
|
2021-01-04 06:54:46 +00:00
|
|
|
if (!mdev->notify_base)
|
|
|
|
pci_iounmap(mdev->pci_dev, (void __force __iomem *)vq->priv);
|
2014-12-11 11:59:51 +00:00
|
|
|
|
|
|
|
vring_del_virtqueue(vq);
|
|
|
|
}
|
|
|
|
|
2020-08-19 22:19:42 +00:00
|
|
|
static int virtio_pci_find_shm_cap(struct pci_dev *dev, u8 required_id,
|
|
|
|
u8 *bar, u64 *offset, u64 *len)
|
|
|
|
{
|
|
|
|
int pos;
|
|
|
|
|
|
|
|
for (pos = pci_find_capability(dev, PCI_CAP_ID_VNDR); pos > 0;
|
|
|
|
pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_VNDR)) {
|
2022-03-23 14:07:27 +00:00
|
|
|
u8 type, cap_len, id, res_bar;
|
2020-08-19 22:19:42 +00:00
|
|
|
u32 tmp32;
|
|
|
|
u64 res_offset, res_length;
|
|
|
|
|
|
|
|
pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
|
|
|
|
cfg_type), &type);
|
|
|
|
if (type != VIRTIO_PCI_CAP_SHARED_MEMORY_CFG)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
|
|
|
|
cap_len), &cap_len);
|
|
|
|
if (cap_len != sizeof(struct virtio_pci_cap64)) {
|
|
|
|
dev_err(&dev->dev, "%s: shm cap with bad size offset:"
|
|
|
|
" %d size: %d\n", __func__, pos, cap_len);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
|
|
|
|
id), &id);
|
|
|
|
if (id != required_id)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
|
2022-03-23 14:07:27 +00:00
|
|
|
bar), &res_bar);
|
|
|
|
if (res_bar >= PCI_STD_NUM_BARS)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Type and ID match, and the BAR value isn't reserved.
|
|
|
|
* Looks good.
|
|
|
|
*/
|
2020-08-19 22:19:42 +00:00
|
|
|
|
|
|
|
/* Read the lower 32bit of length and offset */
|
|
|
|
pci_read_config_dword(dev, pos + offsetof(struct virtio_pci_cap,
|
|
|
|
offset), &tmp32);
|
|
|
|
res_offset = tmp32;
|
|
|
|
pci_read_config_dword(dev, pos + offsetof(struct virtio_pci_cap,
|
|
|
|
length), &tmp32);
|
|
|
|
res_length = tmp32;
|
|
|
|
|
|
|
|
/* and now the top half */
|
|
|
|
pci_read_config_dword(dev,
|
|
|
|
pos + offsetof(struct virtio_pci_cap64,
|
|
|
|
offset_hi), &tmp32);
|
|
|
|
res_offset |= ((u64)tmp32) << 32;
|
|
|
|
pci_read_config_dword(dev,
|
|
|
|
pos + offsetof(struct virtio_pci_cap64,
|
|
|
|
length_hi), &tmp32);
|
|
|
|
res_length |= ((u64)tmp32) << 32;
|
|
|
|
|
2022-03-23 14:07:27 +00:00
|
|
|
*bar = res_bar;
|
2020-08-19 22:19:42 +00:00
|
|
|
*offset = res_offset;
|
|
|
|
*len = res_length;
|
|
|
|
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool vp_get_shm_region(struct virtio_device *vdev,
|
|
|
|
struct virtio_shm_region *region, u8 id)
|
|
|
|
{
|
|
|
|
struct virtio_pci_device *vp_dev = to_vp_device(vdev);
|
|
|
|
struct pci_dev *pci_dev = vp_dev->pci_dev;
|
|
|
|
u8 bar;
|
|
|
|
u64 offset, len;
|
|
|
|
phys_addr_t phys_addr;
|
|
|
|
size_t bar_len;
|
|
|
|
|
|
|
|
if (!virtio_pci_find_shm_cap(pci_dev, id, &bar, &offset, &len))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
phys_addr = pci_resource_start(pci_dev, bar);
|
|
|
|
bar_len = pci_resource_len(pci_dev, bar);
|
|
|
|
|
|
|
|
if ((offset + len) < offset) {
|
|
|
|
dev_err(&pci_dev->dev, "%s: cap offset+len overflow detected\n",
|
|
|
|
__func__);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (offset + len > bar_len) {
|
|
|
|
dev_err(&pci_dev->dev, "%s: bar shorter than cap offset+len\n",
|
|
|
|
__func__);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
region->len = len;
|
|
|
|
region->addr = (u64) phys_addr + offset;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-13 14:34:58 +00:00
|
|
|
static const struct virtio_config_ops virtio_pci_config_nodev_ops = {
|
|
|
|
.get = NULL,
|
|
|
|
.set = NULL,
|
|
|
|
.generation = vp_generation,
|
|
|
|
.get_status = vp_get_status,
|
|
|
|
.set_status = vp_set_status,
|
|
|
|
.reset = vp_reset,
|
|
|
|
.find_vqs = vp_modern_find_vqs,
|
|
|
|
.del_vqs = vp_del_vqs,
|
2022-05-27 06:01:15 +00:00
|
|
|
.synchronize_cbs = vp_synchronize_vectors,
|
2015-01-13 14:34:58 +00:00
|
|
|
.get_features = vp_get_features,
|
|
|
|
.finalize_features = vp_finalize_features,
|
|
|
|
.bus_name = vp_bus_name,
|
|
|
|
.set_vq_affinity = vp_set_vq_affinity,
|
2017-02-05 17:15:23 +00:00
|
|
|
.get_vq_affinity = vp_get_vq_affinity,
|
2020-08-19 22:19:42 +00:00
|
|
|
.get_shm_region = vp_get_shm_region,
|
2022-08-01 06:38:52 +00:00
|
|
|
.disable_vq_and_reset = vp_modern_disable_vq_and_reset,
|
|
|
|
.enable_vq_after_reset = vp_modern_enable_vq_after_reset,
|
2015-01-13 14:34:58 +00:00
|
|
|
};
|
|
|
|
|
2014-12-11 11:59:51 +00:00
|
|
|
static const struct virtio_config_ops virtio_pci_config_ops = {
|
|
|
|
.get = vp_get,
|
|
|
|
.set = vp_set,
|
|
|
|
.generation = vp_generation,
|
|
|
|
.get_status = vp_get_status,
|
|
|
|
.set_status = vp_set_status,
|
|
|
|
.reset = vp_reset,
|
|
|
|
.find_vqs = vp_modern_find_vqs,
|
|
|
|
.del_vqs = vp_del_vqs,
|
2022-05-27 06:01:15 +00:00
|
|
|
.synchronize_cbs = vp_synchronize_vectors,
|
2014-12-11 11:59:51 +00:00
|
|
|
.get_features = vp_get_features,
|
|
|
|
.finalize_features = vp_finalize_features,
|
|
|
|
.bus_name = vp_bus_name,
|
|
|
|
.set_vq_affinity = vp_set_vq_affinity,
|
2017-02-05 17:15:23 +00:00
|
|
|
.get_vq_affinity = vp_get_vq_affinity,
|
2020-08-19 22:19:42 +00:00
|
|
|
.get_shm_region = vp_get_shm_region,
|
2022-08-01 06:38:52 +00:00
|
|
|
.disable_vq_and_reset = vp_modern_disable_vq_and_reset,
|
|
|
|
.enable_vq_after_reset = vp_modern_enable_vq_after_reset,
|
2014-12-11 11:59:51 +00:00
|
|
|
};
|
|
|
|
|
2021-01-04 06:54:47 +00:00
|
|
|
/* the PCI probing function */
|
|
|
|
int virtio_pci_modern_probe(struct virtio_pci_device *vp_dev)
|
|
|
|
{
|
|
|
|
struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
|
|
|
|
struct pci_dev *pci_dev = vp_dev->pci_dev;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
mdev->pci_dev = pci_dev;
|
|
|
|
|
|
|
|
err = vp_modern_probe(mdev);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
if (mdev->device)
|
|
|
|
vp_dev->vdev.config = &virtio_pci_config_ops;
|
|
|
|
else
|
|
|
|
vp_dev->vdev.config = &virtio_pci_config_nodev_ops;
|
|
|
|
|
|
|
|
vp_dev->config_vector = vp_config_vector;
|
|
|
|
vp_dev->setup_vq = setup_vq;
|
|
|
|
vp_dev->del_vq = del_vq;
|
|
|
|
vp_dev->isr = mdev->isr;
|
|
|
|
vp_dev->vdev.id = mdev->id;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-01-04 06:54:48 +00:00
|
|
|
void virtio_pci_modern_remove(struct virtio_pci_device *vp_dev)
|
|
|
|
{
|
|
|
|
struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
|
|
|
|
|
|
|
|
vp_modern_remove(mdev);
|
|
|
|
}
|