--- /dev/null
+vdpa_sim-remove-hard-coded-virtq-count.patch
+vdpa_sim-add-struct-vdpasim_dev_attr-for-device-attributes.patch
+vdpa_sim-store-parsed-mac-address-in-a-buffer.patch
+vdpa_sim-make-config-generic-and-usable-for-any-device-type.patch
+vdpa_sim-add-get_config-callback-in-vdpasim_dev_attr.patch
--- /dev/null
+From 65b709586e222fa6ffd4166ac7fdb5d5dad113ee Mon Sep 17 00:00:00 2001
+From: Stefano Garzarella <sgarzare@redhat.com>
+Date: Tue, 15 Dec 2020 15:42:50 +0100
+Subject: vdpa_sim: add get_config callback in vdpasim_dev_attr
+
+From: Stefano Garzarella <sgarzare@redhat.com>
+
+commit 65b709586e222fa6ffd4166ac7fdb5d5dad113ee upstream.
+
+The get_config callback can be used by the device to fill the
+config structure.
+The callback will be invoked in vdpasim_get_config() before copying
+bytes into caller buffer.
+
+Move vDPA-net config updates from vdpasim_set_features() in the
+new vdpasim_net_get_config() callback.
+This is safe since in vdpa_get_config() we already check that
+.set_features() callback is called before .get_config().
+
+Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
+Link: https://lore.kernel.org/r/20201215144256.155342-13-sgarzare@redhat.com
+Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
+Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+---
+ drivers/vdpa/vdpa_sim/vdpa_sim.c | 35 +++++++++++++++++++++--------------
+ 1 file changed, 21 insertions(+), 14 deletions(-)
+
+--- a/drivers/vdpa/vdpa_sim/vdpa_sim.c
++++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c
+@@ -69,9 +69,12 @@ static u64 vdpasim_features = (1ULL << V
+ (1ULL << VIRTIO_F_ACCESS_PLATFORM) |
+ (1ULL << VIRTIO_NET_F_MAC);
+
++struct vdpasim;
++
+ struct vdpasim_dev_attr {
+ size_t config_size;
+ int nvqs;
++ void (*get_config)(struct vdpasim *vdpasim, void *config);
+ };
+
+ /* State of each vdpasim device */
+@@ -524,8 +527,6 @@ static u64 vdpasim_get_features(struct v
+ static int vdpasim_set_features(struct vdpa_device *vdpa, u64 features)
+ {
+ struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
+- struct virtio_net_config *config =
+- (struct virtio_net_config *)vdpasim->config;
+
+ /* DMA mapping must be done by driver */
+ if (!(features & (1ULL << VIRTIO_F_ACCESS_PLATFORM)))
+@@ -533,16 +534,6 @@ static int vdpasim_set_features(struct v
+
+ vdpasim->features = features & vdpasim_features;
+
+- /* We generally only know whether guest is using the legacy interface
+- * here, so generally that's the earliest we can set config fields.
+- * Note: We actually require VIRTIO_F_ACCESS_PLATFORM above which
+- * implies VIRTIO_F_VERSION_1, but let's not try to be clever here.
+- */
+-
+- config->mtu = cpu_to_vdpasim16(vdpasim, 1500);
+- config->status = cpu_to_vdpasim16(vdpasim, VIRTIO_NET_S_LINK_UP);
+- memcpy(config->mac, macaddr_buf, ETH_ALEN);
+-
+ return 0;
+ }
+
+@@ -595,8 +586,13 @@ static void vdpasim_get_config(struct vd
+ {
+ struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
+
+- if (offset + len < vdpasim->dev_attr.config_size)
+- memcpy(buf, vdpasim->config + offset, len);
++ if (offset + len > vdpasim->dev_attr.config_size)
++ return;
++
++ if (vdpasim->dev_attr.get_config)
++ vdpasim->dev_attr.get_config(vdpasim, vdpasim->config);
++
++ memcpy(buf, vdpasim->config + offset, len);
+ }
+
+ static void vdpasim_set_config(struct vdpa_device *vdpa, unsigned int offset,
+@@ -739,12 +735,23 @@ static const struct vdpa_config_ops vdpa
+ .free = vdpasim_free,
+ };
+
++static void vdpasim_net_get_config(struct vdpasim *vdpasim, void *config)
++{
++ struct virtio_net_config *net_config =
++ (struct virtio_net_config *)config;
++
++ net_config->mtu = cpu_to_vdpasim16(vdpasim, 1500);
++ net_config->status = cpu_to_vdpasim16(vdpasim, VIRTIO_NET_S_LINK_UP);
++ memcpy(net_config->mac, macaddr_buf, ETH_ALEN);
++}
++
+ static int __init vdpasim_dev_init(void)
+ {
+ struct vdpasim_dev_attr dev_attr = {};
+
+ dev_attr.nvqs = VDPASIM_VQ_NUM;
+ dev_attr.config_size = sizeof(struct virtio_net_config);
++ dev_attr.get_config = vdpasim_net_get_config;
+
+ vdpasim_dev = vdpasim_create(&dev_attr);
+
--- /dev/null
+From 6c6e28fe45794054410ad8cd2770af69fbe0338d Mon Sep 17 00:00:00 2001
+From: Stefano Garzarella <sgarzare@redhat.com>
+Date: Tue, 15 Dec 2020 15:42:44 +0100
+Subject: vdpa_sim: add struct vdpasim_dev_attr for device attributes
+
+From: Stefano Garzarella <sgarzare@redhat.com>
+
+commit 6c6e28fe45794054410ad8cd2770af69fbe0338d upstream.
+
+vdpasim_dev_attr will contain device specific attributes. We starting
+moving the number of virtqueues (i.e. nvqs) to vdpasim_dev_attr.
+
+vdpasim_create() creates a new vDPA simulator following the device
+attributes defined in the vdpasim_dev_attr parameter.
+
+Co-developed-by: Max Gurtovoy <mgurtovoy@nvidia.com>
+Signed-off-by: Max Gurtovoy <mgurtovoy@nvidia.com>
+Acked-by: Jason Wang <jasowang@redhat.com>
+Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
+Link: https://lore.kernel.org/r/20201215144256.155342-7-sgarzare@redhat.com
+Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
+Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+---
+ drivers/vdpa/vdpa_sim/vdpa_sim.c | 25 +++++++++++++++++--------
+ 1 file changed, 17 insertions(+), 8 deletions(-)
+
+--- a/drivers/vdpa/vdpa_sim/vdpa_sim.c
++++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c
+@@ -67,11 +67,16 @@ static u64 vdpasim_features = (1ULL << V
+ (1ULL << VIRTIO_F_ACCESS_PLATFORM) |
+ (1ULL << VIRTIO_NET_F_MAC);
+
++struct vdpasim_dev_attr {
++ int nvqs;
++};
++
+ /* State of each vdpasim device */
+ struct vdpasim {
+ struct vdpa_device vdpa;
+ struct vdpasim_virtqueue *vqs;
+ struct work_struct work;
++ struct vdpasim_dev_attr dev_attr;
+ /* spinlock to synchronize virtqueue state */
+ spinlock_t lock;
+ struct virtio_net_config config;
+@@ -80,7 +85,6 @@ struct vdpasim {
+ u32 status;
+ u32 generation;
+ u64 features;
+- int nvqs;
+ /* spinlock to synchronize iommu table */
+ spinlock_t iommu_lock;
+ };
+@@ -145,7 +149,7 @@ static void vdpasim_reset(struct vdpasim
+ {
+ int i;
+
+- for (i = 0; i < vdpasim->nvqs; i++)
++ for (i = 0; i < vdpasim->dev_attr.nvqs; i++)
+ vdpasim_vq_reset(&vdpasim->vqs[i]);
+
+ spin_lock(&vdpasim->iommu_lock);
+@@ -346,7 +350,7 @@ static const struct dma_map_ops vdpasim_
+ static const struct vdpa_config_ops vdpasim_net_config_ops;
+ static const struct vdpa_config_ops vdpasim_net_batch_config_ops;
+
+-static struct vdpasim *vdpasim_create(void)
++static struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr)
+ {
+ const struct vdpa_config_ops *ops;
+ struct vdpasim *vdpasim;
+@@ -358,11 +362,12 @@ static struct vdpasim *vdpasim_create(vo
+ else
+ ops = &vdpasim_net_config_ops;
+
+- vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops, VDPASIM_VQ_NUM);
++ vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops,
++ dev_attr->nvqs);
+ if (!vdpasim)
+ goto err_alloc;
+
+- vdpasim->nvqs = VDPASIM_VQ_NUM;
++ vdpasim->dev_attr = *dev_attr;
+ INIT_WORK(&vdpasim->work, vdpasim_work);
+ spin_lock_init(&vdpasim->lock);
+ spin_lock_init(&vdpasim->iommu_lock);
+@@ -373,7 +378,7 @@ static struct vdpasim *vdpasim_create(vo
+ goto err_iommu;
+ set_dma_ops(dev, &vdpasim_dma_ops);
+
+- vdpasim->vqs = kcalloc(vdpasim->nvqs, sizeof(struct vdpasim_virtqueue),
++ vdpasim->vqs = kcalloc(dev_attr->nvqs, sizeof(struct vdpasim_virtqueue),
+ GFP_KERNEL);
+ if (!vdpasim->vqs)
+ goto err_iommu;
+@@ -396,7 +401,7 @@ static struct vdpasim *vdpasim_create(vo
+ eth_random_addr(vdpasim->config.mac);
+ }
+
+- for (i = 0; i < vdpasim->nvqs; i++)
++ for (i = 0; i < dev_attr->nvqs; i++)
+ vringh_set_iotlb(&vdpasim->vqs[i].vring, vdpasim->iommu);
+
+ vdpasim->vdpa.dma_dev = dev;
+@@ -724,7 +729,11 @@ static const struct vdpa_config_ops vdpa
+
+ static int __init vdpasim_dev_init(void)
+ {
+- vdpasim_dev = vdpasim_create();
++ struct vdpasim_dev_attr dev_attr = {};
++
++ dev_attr.nvqs = VDPASIM_VQ_NUM;
++
++ vdpasim_dev = vdpasim_create(&dev_attr);
+
+ if (!IS_ERR(vdpasim_dev))
+ return 0;
--- /dev/null
+From f37cbbc65178e0a45823d281d290c4c02da9631c Mon Sep 17 00:00:00 2001
+From: Stefano Garzarella <sgarzare@redhat.com>
+Date: Tue, 15 Dec 2020 15:42:49 +0100
+Subject: vdpa_sim: make 'config' generic and usable for any device type
+
+From: Stefano Garzarella <sgarzare@redhat.com>
+
+commit f37cbbc65178e0a45823d281d290c4c02da9631c upstream.
+
+Add new 'config_size' attribute in 'vdpasim_dev_attr' and allocates
+'config' dynamically to support any device types.
+
+Acked-by: Jason Wang <jasowang@redhat.com>
+Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
+Link: https://lore.kernel.org/r/20201215144256.155342-12-sgarzare@redhat.com
+Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
+Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+---
+ drivers/vdpa/vdpa_sim/vdpa_sim.c | 17 +++++++++++++----
+ 1 file changed, 13 insertions(+), 4 deletions(-)
+
+--- a/drivers/vdpa/vdpa_sim/vdpa_sim.c
++++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c
+@@ -70,6 +70,7 @@ static u64 vdpasim_features = (1ULL << V
+ (1ULL << VIRTIO_NET_F_MAC);
+
+ struct vdpasim_dev_attr {
++ size_t config_size;
+ int nvqs;
+ };
+
+@@ -81,7 +82,8 @@ struct vdpasim {
+ struct vdpasim_dev_attr dev_attr;
+ /* spinlock to synchronize virtqueue state */
+ spinlock_t lock;
+- struct virtio_net_config config;
++ /* virtio config according to device type */
++ void *config;
+ struct vhost_iotlb *iommu;
+ void *buffer;
+ u32 status;
+@@ -380,6 +382,10 @@ static struct vdpasim *vdpasim_create(st
+ goto err_iommu;
+ 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),
+ GFP_KERNEL);
+ if (!vdpasim->vqs)
+@@ -518,7 +524,8 @@ static u64 vdpasim_get_features(struct v
+ static int vdpasim_set_features(struct vdpa_device *vdpa, u64 features)
+ {
+ 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 */
+ if (!(features & (1ULL << VIRTIO_F_ACCESS_PLATFORM)))
+@@ -588,8 +595,8 @@ static void vdpasim_get_config(struct vd
+ {
+ struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
+
+- if (offset + len < sizeof(struct virtio_net_config))
+- memcpy(buf, (u8 *)&vdpasim->config + offset, len);
++ if (offset + len < vdpasim->dev_attr.config_size)
++ memcpy(buf, vdpasim->config + offset, len);
+ }
+
+ static void vdpasim_set_config(struct vdpa_device *vdpa, unsigned int offset,
+@@ -676,6 +683,7 @@ static void vdpasim_free(struct vdpa_dev
+ if (vdpasim->iommu)
+ vhost_iotlb_free(vdpasim->iommu);
+ kfree(vdpasim->vqs);
++ kfree(vdpasim->config);
+ }
+
+ static const struct vdpa_config_ops vdpasim_net_config_ops = {
+@@ -736,6 +744,7 @@ static int __init vdpasim_dev_init(void)
+ struct vdpasim_dev_attr dev_attr = {};
+
+ dev_attr.nvqs = VDPASIM_VQ_NUM;
++ dev_attr.config_size = sizeof(struct virtio_net_config);
+
+ vdpasim_dev = vdpasim_create(&dev_attr);
+
--- /dev/null
+From 423248d60d2b655321fc49eca1545f95a1bc9d6c Mon Sep 17 00:00:00 2001
+From: Max Gurtovoy <mgurtovoy@nvidia.com>
+Date: Tue, 15 Dec 2020 15:42:41 +0100
+Subject: vdpa_sim: remove hard-coded virtq count
+
+From: Max Gurtovoy <mgurtovoy@nvidia.com>
+
+commit 423248d60d2b655321fc49eca1545f95a1bc9d6c upstream.
+
+Add a new attribute that will define the number of virt queues to be
+created for the vdpasim device.
+
+Signed-off-by: Max Gurtovoy <mgurtovoy@nvidia.com>
+[sgarzare: replace kmalloc_array() with kcalloc()]
+Acked-by: Jason Wang <jasowang@redhat.com>
+Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
+Link: https://lore.kernel.org/r/20201215144256.155342-4-sgarzare@redhat.com
+Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
+Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+---
+ drivers/vdpa/vdpa_sim/vdpa_sim.c | 18 +++++++++++++-----
+ 1 file changed, 13 insertions(+), 5 deletions(-)
+
+--- a/drivers/vdpa/vdpa_sim/vdpa_sim.c
++++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c
+@@ -70,7 +70,7 @@ static u64 vdpasim_features = (1ULL << V
+ /* State of each vdpasim device */
+ struct vdpasim {
+ struct vdpa_device vdpa;
+- struct vdpasim_virtqueue vqs[VDPASIM_VQ_NUM];
++ struct vdpasim_virtqueue *vqs;
+ struct work_struct work;
+ /* spinlock to synchronize virtqueue state */
+ spinlock_t lock;
+@@ -80,6 +80,7 @@ struct vdpasim {
+ u32 status;
+ u32 generation;
+ u64 features;
++ int nvqs;
+ /* spinlock to synchronize iommu table */
+ spinlock_t iommu_lock;
+ };
+@@ -144,7 +145,7 @@ static void vdpasim_reset(struct vdpasim
+ {
+ int i;
+
+- for (i = 0; i < VDPASIM_VQ_NUM; i++)
++ for (i = 0; i < vdpasim->nvqs; i++)
+ vdpasim_vq_reset(&vdpasim->vqs[i]);
+
+ spin_lock(&vdpasim->iommu_lock);
+@@ -350,7 +351,7 @@ static struct vdpasim *vdpasim_create(vo
+ const struct vdpa_config_ops *ops;
+ struct vdpasim *vdpasim;
+ struct device *dev;
+- int ret = -ENOMEM;
++ int i, ret = -ENOMEM;
+
+ if (batch_mapping)
+ ops = &vdpasim_net_batch_config_ops;
+@@ -361,6 +362,7 @@ static struct vdpasim *vdpasim_create(vo
+ if (!vdpasim)
+ goto err_alloc;
+
++ vdpasim->nvqs = VDPASIM_VQ_NUM;
+ INIT_WORK(&vdpasim->work, vdpasim_work);
+ spin_lock_init(&vdpasim->lock);
+ spin_lock_init(&vdpasim->iommu_lock);
+@@ -371,6 +373,11 @@ static struct vdpasim *vdpasim_create(vo
+ goto err_iommu;
+ set_dma_ops(dev, &vdpasim_dma_ops);
+
++ vdpasim->vqs = kcalloc(vdpasim->nvqs, sizeof(struct vdpasim_virtqueue),
++ GFP_KERNEL);
++ if (!vdpasim->vqs)
++ goto err_iommu;
++
+ vdpasim->iommu = vhost_iotlb_alloc(2048, 0);
+ if (!vdpasim->iommu)
+ goto err_iommu;
+@@ -389,8 +396,8 @@ static struct vdpasim *vdpasim_create(vo
+ eth_random_addr(vdpasim->config.mac);
+ }
+
+- vringh_set_iotlb(&vdpasim->vqs[0].vring, vdpasim->iommu);
+- vringh_set_iotlb(&vdpasim->vqs[1].vring, vdpasim->iommu);
++ for (i = 0; i < vdpasim->nvqs; i++)
++ vringh_set_iotlb(&vdpasim->vqs[i].vring, vdpasim->iommu);
+
+ vdpasim->vdpa.dma_dev = dev;
+ ret = vdpa_register_device(&vdpasim->vdpa);
+@@ -659,6 +666,7 @@ static void vdpasim_free(struct vdpa_dev
+ kfree(vdpasim->buffer);
+ if (vdpasim->iommu)
+ vhost_iotlb_free(vdpasim->iommu);
++ kfree(vdpasim->vqs);
+ }
+
+ static const struct vdpa_config_ops vdpasim_net_config_ops = {
--- /dev/null
+From cf1a3b35382c10ce315c32bd2b3d7789897fbe13 Mon Sep 17 00:00:00 2001
+From: Stefano Garzarella <sgarzare@redhat.com>
+Date: Tue, 15 Dec 2020 15:42:48 +0100
+Subject: vdpa_sim: store parsed MAC address in a buffer
+
+From: Stefano Garzarella <sgarzare@redhat.com>
+
+commit cf1a3b35382c10ce315c32bd2b3d7789897fbe13 upstream.
+
+As preparation for the next patches, we store the MAC address,
+parsed during the vdpasim_create(), in a buffer that will be used
+to fill 'config' together with other configurations.
+
+Acked-by: Jason Wang <jasowang@redhat.com>
+Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
+Link: https://lore.kernel.org/r/20201215144256.155342-11-sgarzare@redhat.com
+Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
+Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+---
+ drivers/vdpa/vdpa_sim/vdpa_sim.c | 10 +++++++---
+ 1 file changed, 7 insertions(+), 3 deletions(-)
+
+--- a/drivers/vdpa/vdpa_sim/vdpa_sim.c
++++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c
+@@ -42,6 +42,8 @@ static char *macaddr;
+ module_param(macaddr, charp, 0);
+ MODULE_PARM_DESC(macaddr, "Ethernet MAC address");
+
++u8 macaddr_buf[ETH_ALEN];
++
+ struct vdpasim_virtqueue {
+ struct vringh vring;
+ struct vringh_kiov iov;
+@@ -392,13 +394,13 @@ static struct vdpasim *vdpasim_create(st
+ goto err_iommu;
+
+ if (macaddr) {
+- mac_pton(macaddr, vdpasim->config.mac);
+- if (!is_valid_ether_addr(vdpasim->config.mac)) {
++ mac_pton(macaddr, macaddr_buf);
++ if (!is_valid_ether_addr(macaddr_buf)) {
+ ret = -EADDRNOTAVAIL;
+ goto err_iommu;
+ }
+ } else {
+- eth_random_addr(vdpasim->config.mac);
++ eth_random_addr(macaddr_buf);
+ }
+
+ for (i = 0; i < dev_attr->nvqs; i++)
+@@ -532,6 +534,8 @@ static int vdpasim_set_features(struct v
+
+ config->mtu = cpu_to_vdpasim16(vdpasim, 1500);
+ config->status = cpu_to_vdpasim16(vdpasim, VIRTIO_NET_S_LINK_UP);
++ memcpy(config->mac, macaddr_buf, ETH_ALEN);
++
+ return 0;
+ }
+