Object create and destroy were implemented specifically for dev parts
device objects. Create general purpose APIs for use by upper layer
drivers.

Signed-off-by: Daniel Jurgens <dani...@nvidia.com>
Reviewed-by: Parav Pandit <pa...@nvidia.com>
Reviewed-by: Shahar Shitrit <shshit...@nvidia.com>
Reviewed-by: Yishai Hadas <yish...@nvidia.com>
---
 drivers/vfio/pci/virtio/migrate.c  |   8 +-
 drivers/virtio/virtio.c            |  59 ++++++++++
 drivers/virtio/virtio_pci_modern.c | 175 +++++++++++++++++------------
 include/linux/virtio.h             |   8 ++
 include/linux/virtio_admin.h       |  32 ++++++
 include/linux/virtio_pci_admin.h   |   7 +-
 6 files changed, 212 insertions(+), 77 deletions(-)

diff --git a/drivers/vfio/pci/virtio/migrate.c 
b/drivers/vfio/pci/virtio/migrate.c
index ba92bb4e9af9..a2aa0e32f593 100644
--- a/drivers/vfio/pci/virtio/migrate.c
+++ b/drivers/vfio/pci/virtio/migrate.c
@@ -152,15 +152,15 @@ static int
 virtiovf_pci_alloc_obj_id(struct virtiovf_pci_core_device *virtvdev, u8 type,
                          u32 *obj_id)
 {
-       return virtio_pci_admin_obj_create(virtvdev->core_device.pdev,
-                                          VIRTIO_RESOURCE_OBJ_DEV_PARTS, type, 
obj_id);
+       return virtio_pci_admin_dev_parts_obj_create(virtvdev->core_device.pdev,
+                                                    type, obj_id);
 }
 
 static void
 virtiovf_pci_free_obj_id(struct virtiovf_pci_core_device *virtvdev, u32 obj_id)
 {
-       virtio_pci_admin_obj_destroy(virtvdev->core_device.pdev,
-                       VIRTIO_RESOURCE_OBJ_DEV_PARTS, obj_id);
+       virtio_pci_admin_dev_parts_obj_destroy(virtvdev->core_device.pdev,
+                                              obj_id);
 }
 
 static struct virtiovf_data_buffer *
diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c
index 6bc268c11100..62233ab4501b 100644
--- a/drivers/virtio/virtio.c
+++ b/drivers/virtio/virtio.c
@@ -788,6 +788,65 @@ int virtio_device_cap_set(struct virtio_device *vdev,
 }
 EXPORT_SYMBOL_GPL(virtio_device_cap_set);
 
+/**
+ * virtio_device_object_create - Create an object on a virtio device
+ * @vdev: the virtio device
+ * @obj_type: type of object to create
+ * @obj_id: ID for the new object
+ * @obj_specific_data: object-specific data for creation
+ * @obj_specific_data_size: size of the object-specific data in bytes
+ *
+ * Creates a new object on the virtio device with the specified type and ID.
+ * The object may require object-specific data for proper initialization.
+ *
+ * Return: 0 on success, -EOPNOTSUPP if the device doesn't support admin
+ * operations or object creation, or a negative error code on other failures.
+ */
+int virtio_device_object_create(struct virtio_device *vdev,
+                               u16 obj_type,
+                               u32 obj_id,
+                               const void *obj_specific_data,
+                               size_t obj_specific_data_size)
+{
+       const struct virtio_admin_ops *admin = vdev->admin_ops;
+
+       if (!admin || !admin->object_create)
+               return -EOPNOTSUPP;
+
+       /* All users of this interface use the self group with member id 0 */
+       return admin->object_create(vdev, obj_type, obj_id,
+                                   VIRTIO_ADMIN_GROUP_TYPE_SELF, 0,
+                                   obj_specific_data, obj_specific_data_size);
+}
+EXPORT_SYMBOL_GPL(virtio_device_object_create);
+
+/**
+ * virtio_device_object_destroy - Destroy an object on a virtio device
+ * @vdev: the virtio device
+ * @obj_type: type of object to destroy
+ * @obj_id: ID of the object to destroy
+ *
+ * Destroys a existing object on the virtio device with the specified type
+ * and ID.
+ *
+ * Return: 0 on success, -EOPNOTSUPP if the device doesn't support admin
+ * operations or object destruction, or a negative error code on other 
failures.
+ */
+int virtio_device_object_destroy(struct virtio_device *vdev,
+                                u16 obj_type,
+                                u32 obj_id)
+{
+       const struct virtio_admin_ops *admin = vdev->admin_ops;
+
+       if (!admin || !admin->object_destroy)
+               return -EOPNOTSUPP;
+
+       /* All users of this interface use the self group with member id 0 */
+       return admin->object_destroy(vdev, obj_type, obj_id,
+                                    VIRTIO_ADMIN_GROUP_TYPE_SELF, 0);
+}
+EXPORT_SYMBOL_GPL(virtio_device_object_destroy);
+
 static int virtio_init(void)
 {
        BUILD_BUG_ON(offsetof(struct virtio_device, features) !=
diff --git a/drivers/virtio/virtio_pci_modern.c 
b/drivers/virtio/virtio_pci_modern.c
index c8bbd807371d..ef787a6334c8 100644
--- a/drivers/virtio/virtio_pci_modern.c
+++ b/drivers/virtio/virtio_pci_modern.c
@@ -967,28 +967,61 @@ int virtio_pci_admin_mode_set(struct pci_dev *pdev, u8 
flags)
 }
 EXPORT_SYMBOL_GPL(virtio_pci_admin_mode_set);
 
-/*
- * virtio_pci_admin_obj_create - Creates an object for a given type and 
operation,
- * following the max objects that can be created for that request.
- * @pdev: VF pci_dev
- * @obj_type: Object type
- * @operation_type: Operation type
- * @obj_id: Output unique object id
+static int vp_modern_admin_cmd_obj_create(struct virtio_device *virtio_dev,
+                                         u16 obj_type,
+                                         u32 obj_id,
+                                         u16 group_type,
+                                         u64 group_member_id,
+                                         const void *obj_specific_data,
+                                         size_t obj_specific_data_size)
+{
+       size_t data_size = sizeof(struct 
virtio_admin_cmd_resource_obj_create_data);
+       struct virtio_admin_cmd_resource_obj_create_data *obj_create_data;
+       struct virtio_admin_cmd cmd = {};
+       void *data __free(kfree) = NULL;
+       struct scatterlist data_sg;
+
+       data_size += (obj_specific_data_size);
+       data = kzalloc(data_size, GFP_KERNEL);
+       if (!data)
+               return -ENOMEM;
+
+       obj_create_data = data;
+       obj_create_data->hdr.type = cpu_to_le16(obj_type);
+       obj_create_data->hdr.id = cpu_to_le32(obj_id);
+       memcpy(obj_create_data->resource_obj_specific_data, obj_specific_data,
+              obj_specific_data_size);
+       sg_init_one(&data_sg, data, data_size);
+
+       cmd.opcode = cpu_to_le16(VIRTIO_ADMIN_CMD_RESOURCE_OBJ_CREATE);
+       cmd.group_type = cpu_to_le16(group_type);
+       cmd.group_member_id = cpu_to_le64(group_member_id);
+       cmd.data_sg = &data_sg;
+
+       return vp_modern_admin_cmd_exec(virtio_dev, &cmd);
+}
+
+/**
+ * virtio_pci_admin_dev_parts_obj_create - Create a device parts object
+ * @pdev: VF PCI device
+ * @operation_type: operation type (GET or SET)
+ * @obj_id: pointer to store the output unique object ID
  *
- * Note: caller must serialize access for the given device.
- * Returns 0 on success, or negative on failure.
+ * This function creates a device parts object for the specified VF PCI device.
+ * The object is associated with the SRIOV group and can be used for GET or SET
+ * operations. The caller must serialize access for the given device.
+ *
+ * Return: 0 on success, -ENODEV if the virtio device is not found,
+ * -EINVAL if the operation type is invalid, -EOPNOTSUPP if device parts
+ * objects are not supported, or a negative error code on other failures.
  */
-int virtio_pci_admin_obj_create(struct pci_dev *pdev, u16 obj_type, u8 
operation_type,
-                               u32 *obj_id)
+int virtio_pci_admin_dev_parts_obj_create(struct pci_dev *pdev,
+                                         u8 operation_type,
+                                         u32 *obj_id)
 {
        struct virtio_device *virtio_dev = virtio_pci_vf_get_pf_dev(pdev);
-       u16 data_size = sizeof(struct 
virtio_admin_cmd_resource_obj_create_data);
-       struct virtio_admin_cmd_resource_obj_create_data *obj_create_data;
        struct virtio_resource_obj_dev_parts obj_dev_parts = {};
        struct virtio_pci_admin_vq *avq;
-       struct virtio_admin_cmd cmd = {};
-       struct scatterlist data_sg;
-       void *data;
        int id = -1;
        int vf_id;
        int ret;
@@ -1000,9 +1033,6 @@ int virtio_pci_admin_obj_create(struct pci_dev *pdev, u16 
obj_type, u8 operation
        if (vf_id < 0)
                return vf_id;
 
-       if (obj_type != VIRTIO_RESOURCE_OBJ_DEV_PARTS)
-               return -EOPNOTSUPP;
-
        if (operation_type != VIRTIO_RESOURCE_OBJ_DEV_PARTS_TYPE_GET &&
            operation_type != VIRTIO_RESOURCE_OBJ_DEV_PARTS_TYPE_SET)
                return -EINVAL;
@@ -1016,52 +1046,66 @@ int virtio_pci_admin_obj_create(struct pci_dev *pdev, 
u16 obj_type, u8 operation
        if (id < 0)
                return id;
 
-       *obj_id = id;
-       data_size += sizeof(obj_dev_parts);
-       data = kzalloc(data_size, GFP_KERNEL);
-       if (!data) {
-               ret = -ENOMEM;
-               goto end;
-       }
-
-       obj_create_data = data;
-       obj_create_data->hdr.type = cpu_to_le16(obj_type);
-       obj_create_data->hdr.id = cpu_to_le32(*obj_id);
        obj_dev_parts.type = operation_type;
-       memcpy(obj_create_data->resource_obj_specific_data, &obj_dev_parts,
-              sizeof(obj_dev_parts));
-       sg_init_one(&data_sg, data, data_size);
-       cmd.opcode = cpu_to_le16(VIRTIO_ADMIN_CMD_RESOURCE_OBJ_CREATE);
-       cmd.group_type = cpu_to_le16(VIRTIO_ADMIN_GROUP_TYPE_SRIOV);
-       cmd.group_member_id = cpu_to_le64(vf_id + 1);
-       cmd.data_sg = &data_sg;
-       ret = vp_modern_admin_cmd_exec(virtio_dev, &cmd);
+       ret = vp_modern_admin_cmd_obj_create(virtio_dev,
+                                            VIRTIO_RESOURCE_OBJ_DEV_PARTS,
+                                            id,
+                                            VIRTIO_ADMIN_GROUP_TYPE_SRIOV,
+                                            vf_id + 1,
+                                            &obj_dev_parts,
+                                            sizeof(obj_dev_parts));
 
-       kfree(data);
-end:
        if (ret)
                ida_free(&avq->dev_parts_ida, id);
+       else
+               *obj_id = id;
 
        return ret;
 }
-EXPORT_SYMBOL_GPL(virtio_pci_admin_obj_create);
+EXPORT_SYMBOL_GPL(virtio_pci_admin_dev_parts_obj_create);
 
-/*
- * virtio_pci_admin_obj_destroy - Destroys an object of a given type and id
- * @pdev: VF pci_dev
- * @obj_type: Object type
- * @id: Object id
+static int vp_modern_admin_cmd_obj_destroy(struct virtio_device *virtio_dev,
+                                          u16 obj_type,
+                                          u32 obj_id,
+                                          u16 group_type,
+                                          u64 group_member_id)
+{
+       struct virtio_admin_cmd_resource_obj_cmd_hdr *data __free(kfree) = NULL;
+       struct virtio_admin_cmd cmd = {};
+       struct scatterlist data_sg;
+
+       data = kzalloc(sizeof(*data), GFP_KERNEL);
+       if (!data)
+               return -ENOMEM;
+
+       data->type = cpu_to_le16(obj_type);
+       data->id = cpu_to_le32(obj_id);
+       sg_init_one(&data_sg, data, sizeof(*data));
+       cmd.opcode = cpu_to_le16(VIRTIO_ADMIN_CMD_RESOURCE_OBJ_DESTROY);
+       cmd.group_type = cpu_to_le16(group_type);
+       cmd.group_member_id = cpu_to_le64(group_member_id);
+       cmd.data_sg = &data_sg;
+
+       return vp_modern_admin_cmd_exec(virtio_dev, &cmd);
+}
+
+/**
+ * virtio_pci_admin_dev_parts_obj_destroy - Destroy a device parts object
+ * @pdev: VF PCI device
+ * @obj_id: ID of the object to destroy
  *
- * Note: caller must serialize access for the given device.
- * Returns 0 on success, or negative on failure.
+ * This function destroys a device parts object with the specified ID for the
+ * given VF PCI device. The object must have been previously created using
+ * virtio_pci_admin_dev_parts_obj_create(). The caller must serialize access
+ * for the given device.
+ *
+ * Return: 0 on success, -ENODEV if the virtio device is not found,
+ * or a negative error code on other failures.
  */
-int virtio_pci_admin_obj_destroy(struct pci_dev *pdev, u16 obj_type, u32 id)
+int virtio_pci_admin_dev_parts_obj_destroy(struct pci_dev *pdev, u32 obj_id)
 {
        struct virtio_device *virtio_dev = virtio_pci_vf_get_pf_dev(pdev);
-       struct virtio_admin_cmd_resource_obj_cmd_hdr *data;
        struct virtio_pci_device *vp_dev;
-       struct virtio_admin_cmd cmd = {};
-       struct scatterlist data_sg;
        int vf_id;
        int ret;
 
@@ -1072,30 +1116,19 @@ int virtio_pci_admin_obj_destroy(struct pci_dev *pdev, 
u16 obj_type, u32 id)
        if (vf_id < 0)
                return vf_id;
 
-       if (obj_type != VIRTIO_RESOURCE_OBJ_DEV_PARTS)
-               return -EINVAL;
-
-       data = kzalloc(sizeof(*data), GFP_KERNEL);
-       if (!data)
-               return -ENOMEM;
-
-       data->type = cpu_to_le16(obj_type);
-       data->id = cpu_to_le32(id);
-       sg_init_one(&data_sg, data, sizeof(*data));
-       cmd.opcode = cpu_to_le16(VIRTIO_ADMIN_CMD_RESOURCE_OBJ_DESTROY);
-       cmd.group_type = cpu_to_le16(VIRTIO_ADMIN_GROUP_TYPE_SRIOV);
-       cmd.group_member_id = cpu_to_le64(vf_id + 1);
-       cmd.data_sg = &data_sg;
-       ret = vp_modern_admin_cmd_exec(virtio_dev, &cmd);
+       ret = vp_modern_admin_cmd_obj_destroy(virtio_dev,
+                                             VIRTIO_RESOURCE_OBJ_DEV_PARTS,
+                                             obj_id,
+                                             VIRTIO_ADMIN_GROUP_TYPE_SRIOV,
+                                             vf_id + 1);
        if (!ret) {
                vp_dev = to_vp_device(virtio_dev);
-               ida_free(&vp_dev->admin_vq.dev_parts_ida, id);
+               ida_free(&vp_dev->admin_vq.dev_parts_ida, obj_id);
        }
 
-       kfree(data);
        return ret;
 }
-EXPORT_SYMBOL_GPL(virtio_pci_admin_obj_destroy);
+EXPORT_SYMBOL_GPL(virtio_pci_admin_dev_parts_obj_destroy);
 
 /*
  * virtio_pci_admin_dev_parts_metadata_get - Gets the metadata of the device 
parts
@@ -1289,6 +1322,8 @@ static const struct virtio_admin_ops virtio_pci_admin_ops 
= {
        .cap_id_list_query = vp_modern_admin_cap_id_list_query,
        .cap_get = vp_modern_admin_cmd_cap_get,
        .cap_set = vp_modern_admin_cmd_cap_set,
+       .object_create = vp_modern_admin_cmd_obj_create,
+       .object_destroy = vp_modern_admin_cmd_obj_destroy,
 };
 /* the PCI probing function */
 int virtio_pci_modern_probe(struct virtio_pci_device *vp_dev)
diff --git a/include/linux/virtio.h b/include/linux/virtio.h
index a6e121b6f1f1..651884e3c8c4 100644
--- a/include/linux/virtio.h
+++ b/include/linux/virtio.h
@@ -208,6 +208,14 @@ int virtio_device_cap_set(struct virtio_device *vdev,
                          u16 id,
                          const void *caps,
                          size_t cap_size);
+int virtio_device_object_create(struct virtio_device *virtio_dev,
+                               u16 obj_type,
+                               u32 obj_id,
+                               const void *obj_specific_data,
+                               size_t obj_specific_data_size);
+int virtio_device_object_destroy(struct virtio_device *virtio_dev,
+                                u16 obj_type,
+                                u32 obj_id);
 
 size_t virtio_max_dma_size(const struct virtio_device *vdev);
 
diff --git a/include/linux/virtio_admin.h b/include/linux/virtio_admin.h
index bbf543d20be4..cc6b82461c9f 100644
--- a/include/linux/virtio_admin.h
+++ b/include/linux/virtio_admin.h
@@ -63,6 +63,38 @@ struct virtio_admin_ops {
                       u16 id,
                       const void *caps,
                       size_t cap_size);
+       /**
+        * @object_create: Create a new object of specified type
+        * @virtio_dev: The virtio device
+        * @obj_type: Type of object to create
+        * @obj_id: ID to assign to the created object
+        * @group_type: Type of group the object belongs to
+        * @group_member_id: Member ID within the group
+        * @obj_specific_data: Object-specific data (must be heap allocated)
+        * @obj_specific_data_size: Size of the object-specific data
+        * Returns: 0 on success, negative error code on failure
+        */
+       int (*object_create)(struct virtio_device *virtio_dev,
+                            u16 obj_type,
+                            u32 obj_id,
+                            u16 group_type,
+                            u64 group_member_id,
+                            const void *obj_specific_data,
+                            size_t obj_specific_data_size);
+       /**
+        * @object_destroy: Destroy an existing object
+        * @virtio_dev: The virtio device
+        * @obj_type: Type of object to destroy
+        * @obj_id: ID of the object to destroy
+        * @group_type: Type of group the object belongs to
+        * @group_member_id: Member ID within the group
+        * Returns: 0 on success, negative error code on failure
+        */
+       int (*object_destroy)(struct virtio_device *virtio_dev,
+                             u16 obj_type,
+                             u32 obj_id,
+                             u16 group_type,
+                             u64 group_member_id);
 };
 
 #endif /* _LINUX_VIRTIO_ADMIN_H */
diff --git a/include/linux/virtio_pci_admin.h b/include/linux/virtio_pci_admin.h
index dffc92c17ad2..da9b8495bce4 100644
--- a/include/linux/virtio_pci_admin.h
+++ b/include/linux/virtio_pci_admin.h
@@ -22,9 +22,10 @@ int virtio_pci_admin_legacy_io_notify_info(struct pci_dev 
*pdev,
 
 bool virtio_pci_admin_has_dev_parts(struct pci_dev *pdev);
 int virtio_pci_admin_mode_set(struct pci_dev *pdev, u8 mode);
-int virtio_pci_admin_obj_create(struct pci_dev *pdev, u16 obj_type, u8 
operation_type,
-                               u32 *obj_id);
-int virtio_pci_admin_obj_destroy(struct pci_dev *pdev, u16 obj_type, u32 id);
+int virtio_pci_admin_dev_parts_obj_create(struct pci_dev *pdev,
+                                         u8 operation_type,
+                                         u32 *obj_id);
+int virtio_pci_admin_dev_parts_obj_destroy(struct pci_dev *pdev, u32 obj_id);
 int virtio_pci_admin_dev_parts_metadata_get(struct pci_dev *pdev, u16 obj_type,
                                            u32 id, u8 metadata_type, u32 *out);
 int virtio_pci_admin_dev_parts_get(struct pci_dev *pdev, u16 obj_type, u32 id,
-- 
2.50.1


Reply via email to