vfio/mtty: Use mdev_get_type_group_id()
authorJason Gunthorpe <jgg@nvidia.com>
Tue, 6 Apr 2021 19:40:35 +0000 (16:40 -0300)
committerAlex Williamson <alex.williamson@redhat.com>
Wed, 7 Apr 2021 21:39:19 +0000 (15:39 -0600)
The type_group_id directly gives the single or dual port index, no
need for string searching.

Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
Message-Id: <12-v2-d36939638fc6+d54-vfio2_jgg@nvidia.com>
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
samples/vfio-mdev/mtty.c

index ce84a300a4dafd2e6669ef55c5ca6c46d5d05e1e..191a587a8d5ab1b78a746de7db321c86047e1b1b 100644 (file)
@@ -711,23 +711,7 @@ accessfailed:
 static int mtty_create(struct kobject *kobj, struct mdev_device *mdev)
 {
        struct mdev_state *mdev_state;
-       char name[MTTY_STRING_LEN];
-       int nr_ports = 0, i;
-
-       if (!mdev)
-               return -EINVAL;
-
-       for (i = 0; i < 2; i++) {
-               snprintf(name, MTTY_STRING_LEN, "%s-%d",
-                       dev_driver_string(mdev_parent_dev(mdev)), i + 1);
-               if (!strcmp(kobj->name, name)) {
-                       nr_ports = i + 1;
-                       break;
-               }
-       }
-
-       if (!nr_ports)
-               return -EINVAL;
+       int nr_ports = mdev_get_type_group_id(mdev) + 1;
 
        mdev_state = kzalloc(sizeof(struct mdev_state), GFP_KERNEL);
        if (mdev_state == NULL)
@@ -1311,18 +1295,11 @@ static const struct attribute_group *mdev_dev_groups[] = {
 static ssize_t
 name_show(struct kobject *kobj, struct device *dev, char *buf)
 {
-       char name[MTTY_STRING_LEN];
-       int i;
-       const char *name_str[2] = {"Single port serial", "Dual port serial"};
+       static const char *name_str[2] = { "Single port serial",
+                                          "Dual port serial" };
 
-       for (i = 0; i < 2; i++) {
-               snprintf(name, MTTY_STRING_LEN, "%s-%d",
-                        dev_driver_string(dev), i + 1);
-               if (!strcmp(kobj->name, name))
-                       return sprintf(buf, "%s\n", name_str[i]);
-       }
-
-       return -EINVAL;
+       return sysfs_emit(buf, "%s\n",
+                         name_str[mtype_get_type_group_id(kobj)]);
 }
 
 static MDEV_TYPE_ATTR_RO(name);
@@ -1330,22 +1307,9 @@ static MDEV_TYPE_ATTR_RO(name);
 static ssize_t
 available_instances_show(struct kobject *kobj, struct device *dev, char *buf)
 {
-       char name[MTTY_STRING_LEN];
-       int i;
        struct mdev_state *mds;
-       int ports = 0, used = 0;
-
-       for (i = 0; i < 2; i++) {
-               snprintf(name, MTTY_STRING_LEN, "%s-%d",
-                        dev_driver_string(dev), i + 1);
-               if (!strcmp(kobj->name, name)) {
-                       ports = i + 1;
-                       break;
-               }
-       }
-
-       if (!ports)
-               return -EINVAL;
+       unsigned int ports = mtype_get_type_group_id(kobj) + 1;
+       int used = 0;
 
        list_for_each_entry(mds, &mdev_devices_list, next)
                used += mds->nr_ports;