This patch implements features provisioning for vdpa_sim_net.
1) validating the provisioned features to be a subset of the parent
   features.
2) clearing the features that is not wanted by the userspace
For example:
vdpasim_net:
  supported_classes net
  max_supported_vqs 3
  dev_features MTU MAC CTRL_VQ CTRL_MAC_ADDR ANY_LAYOUT VERSION_1 ACCESS_PLATFORM
1) provision vDPA device with all features that are supported by the
   net simulator
dev1: mac 00:00:00:00:00:00 link up link_announce false mtu 1500
  negotiated_features MTU MAC CTRL_VQ CTRL_MAC_ADDR VERSION_1 ACCESS_PLATFORM
2) provision vDPA device with a subset of the features
dev1: mac 00:00:00:00:00:00 link up link_announce false mtu 1500
  negotiated_features CTRL_VQ VERSION_1 ACCESS_PLATFORM
Reviewed-by: Eli Cohen <elic@nvidia.com>
Signed-off-by: Jason Wang <jasowang@redhat.com>
Message-Id: <
20220927074810.28627-3-jasowang@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Reviewed-by: Stefano Garzarella <sgarzare@redhat.com>
 #include <linux/vdpa.h>
 #include <linux/vhost_iotlb.h>
 #include <linux/iova.h>
+#include <uapi/linux/vdpa.h>
 
 #include "vdpa_sim.h"
 
 static const struct vdpa_config_ops vdpasim_config_ops;
 static const struct vdpa_config_ops vdpasim_batch_config_ops;
 
-struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr)
+struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr,
+                              const struct vdpa_dev_set_config *config)
 {
        const struct vdpa_config_ops *ops;
        struct vdpasim *vdpasim;
        struct device *dev;
        int i, ret = -ENOMEM;
 
+       if (config->mask & BIT_ULL(VDPA_ATTR_DEV_FEATURES)) {
+               if (config->device_features &
+                   ~dev_attr->supported_features)
+                       return ERR_PTR(-EINVAL);
+               dev_attr->supported_features =
+                       config->device_features;
+       }
+
        if (batch_mapping)
                ops = &vdpasim_batch_config_ops;
        else
 
        spinlock_t iommu_lock;
 };
 
-struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *attr);
+struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *attr,
+                              const struct vdpa_dev_set_config *config);
 
 /* TODO: cross-endian support */
 static inline bool vdpasim_is_little_endian(struct vdpasim *vdpasim)
 
        dev_attr.work_fn = vdpasim_blk_work;
        dev_attr.buffer_size = VDPASIM_BLK_CAPACITY << SECTOR_SHIFT;
 
-       simdev = vdpasim_create(&dev_attr);
+       simdev = vdpasim_create(&dev_attr, config);
        if (IS_ERR(simdev))
                return PTR_ERR(simdev);
 
 
        dev_attr.work_fn = vdpasim_net_work;
        dev_attr.buffer_size = PAGE_SIZE;
 
-       simdev = vdpasim_create(&dev_attr);
+       simdev = vdpasim_create(&dev_attr, config);
        if (IS_ERR(simdev))
                return PTR_ERR(simdev);
 
        .id_table = id_table,
        .ops = &vdpasim_net_mgmtdev_ops,
        .config_attr_mask = (1 << VDPA_ATTR_DEV_NET_CFG_MACADDR |
-                            1 << VDPA_ATTR_DEV_NET_CFG_MTU),
+                            1 << VDPA_ATTR_DEV_NET_CFG_MTU |
+                            1 << VDPA_ATTR_DEV_FEATURES),
        .max_supported_vqs = VDPASIM_NET_VQ_NUM,
        .supported_features = VDPASIM_NET_FEATURES,
 };