From c594b26ff78e2cb315101ade73155baf868158eb Mon Sep 17 00:00:00 2001 From: Jason Gunthorpe Date: Tue, 6 Apr 2021 16:40:35 -0300 Subject: [PATCH] vfio/mtty: Use mdev_get_type_group_id() The type_group_id directly gives the single or dual port index, no need for string searching. Reviewed-by: Christoph Hellwig Signed-off-by: Jason Gunthorpe Message-Id: <12-v2-d36939638fc6+d54-vfio2_jgg@nvidia.com> Signed-off-by: Alex Williamson --- samples/vfio-mdev/mtty.c | 50 ++++++---------------------------------- 1 file changed, 7 insertions(+), 43 deletions(-) diff --git a/samples/vfio-mdev/mtty.c b/samples/vfio-mdev/mtty.c index ce84a300a4da..191a587a8d5a 100644 --- a/samples/vfio-mdev/mtty.c +++ b/samples/vfio-mdev/mtty.c @@ -711,23 +711,7 @@ static ssize_t mdev_access(struct mdev_device *mdev, u8 *buf, size_t count, 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;