Commit f37cbbc6 authored by Stefano Garzarella's avatar Stefano Garzarella Committed by Michael S. Tsirkin

vdpa_sim: make 'config' generic and usable for any device type

Add new 'config_size' attribute in 'vdpasim_dev_attr' and allocates
'config' dynamically to support any device types.
Acked-by: default avatarJason Wang <jasowang@redhat.com>
Signed-off-by: default avatarStefano Garzarella <sgarzare@redhat.com>
Link: https://lore.kernel.org/r/20201215144256.155342-12-sgarzare@redhat.comSigned-off-by: default avatarMichael S. Tsirkin <mst@redhat.com>
parent cf1a3b35
...@@ -70,6 +70,7 @@ struct vdpasim_virtqueue { ...@@ -70,6 +70,7 @@ struct vdpasim_virtqueue {
struct vdpasim_dev_attr { struct vdpasim_dev_attr {
u64 supported_features; u64 supported_features;
size_t config_size;
int nvqs; int nvqs;
u32 id; u32 id;
...@@ -84,7 +85,8 @@ struct vdpasim { ...@@ -84,7 +85,8 @@ struct vdpasim {
struct vdpasim_dev_attr dev_attr; struct vdpasim_dev_attr dev_attr;
/* spinlock to synchronize virtqueue state */ /* spinlock to synchronize virtqueue state */
spinlock_t lock; spinlock_t lock;
struct virtio_net_config config; /* virtio config according to device type */
void *config;
struct vhost_iotlb *iommu; struct vhost_iotlb *iommu;
void *buffer; void *buffer;
u32 status; u32 status;
...@@ -384,6 +386,10 @@ static struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr) ...@@ -384,6 +386,10 @@ static struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr)
goto err_iommu; goto err_iommu;
set_dma_ops(dev, &vdpasim_dma_ops); set_dma_ops(dev, &vdpasim_dma_ops);
vdpasim->config = kzalloc(dev_attr->config_size, GFP_KERNEL);
if (!vdpasim->config)
goto err_iommu;
vdpasim->vqs = kcalloc(dev_attr->nvqs, sizeof(struct vdpasim_virtqueue), vdpasim->vqs = kcalloc(dev_attr->nvqs, sizeof(struct vdpasim_virtqueue),
GFP_KERNEL); GFP_KERNEL);
if (!vdpasim->vqs) if (!vdpasim->vqs)
...@@ -524,7 +530,8 @@ static u64 vdpasim_get_features(struct vdpa_device *vdpa) ...@@ -524,7 +530,8 @@ static u64 vdpasim_get_features(struct vdpa_device *vdpa)
static int vdpasim_set_features(struct vdpa_device *vdpa, u64 features) static int vdpasim_set_features(struct vdpa_device *vdpa, u64 features)
{ {
struct vdpasim *vdpasim = vdpa_to_sim(vdpa); struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
struct virtio_net_config *config = &vdpasim->config; struct virtio_net_config *config =
(struct virtio_net_config *)vdpasim->config;
/* DMA mapping must be done by driver */ /* DMA mapping must be done by driver */
if (!(features & (1ULL << VIRTIO_F_ACCESS_PLATFORM))) if (!(features & (1ULL << VIRTIO_F_ACCESS_PLATFORM)))
...@@ -596,8 +603,8 @@ static void vdpasim_get_config(struct vdpa_device *vdpa, unsigned int offset, ...@@ -596,8 +603,8 @@ static void vdpasim_get_config(struct vdpa_device *vdpa, unsigned int offset,
{ {
struct vdpasim *vdpasim = vdpa_to_sim(vdpa); struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
if (offset + len < sizeof(struct virtio_net_config)) if (offset + len < vdpasim->dev_attr.config_size)
memcpy(buf, (u8 *)&vdpasim->config + offset, len); memcpy(buf, vdpasim->config + offset, len);
} }
static void vdpasim_set_config(struct vdpa_device *vdpa, unsigned int offset, static void vdpasim_set_config(struct vdpa_device *vdpa, unsigned int offset,
...@@ -684,6 +691,7 @@ static void vdpasim_free(struct vdpa_device *vdpa) ...@@ -684,6 +691,7 @@ static void vdpasim_free(struct vdpa_device *vdpa)
if (vdpasim->iommu) if (vdpasim->iommu)
vhost_iotlb_free(vdpasim->iommu); vhost_iotlb_free(vdpasim->iommu);
kfree(vdpasim->vqs); kfree(vdpasim->vqs);
kfree(vdpasim->config);
} }
static const struct vdpa_config_ops vdpasim_config_ops = { static const struct vdpa_config_ops vdpasim_config_ops = {
...@@ -746,6 +754,7 @@ static int __init vdpasim_dev_init(void) ...@@ -746,6 +754,7 @@ static int __init vdpasim_dev_init(void)
dev_attr.id = VIRTIO_ID_NET; dev_attr.id = VIRTIO_ID_NET;
dev_attr.supported_features = VDPASIM_NET_FEATURES; dev_attr.supported_features = VDPASIM_NET_FEATURES;
dev_attr.nvqs = VDPASIM_VQ_NUM; dev_attr.nvqs = VDPASIM_VQ_NUM;
dev_attr.config_size = sizeof(struct virtio_net_config);
dev_attr.work_fn = vdpasim_net_work; dev_attr.work_fn = vdpasim_net_work;
vdpasim_dev = vdpasim_create(&dev_attr); vdpasim_dev = vdpasim_create(&dev_attr);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment