Commit c594b26f authored by Jason Gunthorpe's avatar Jason Gunthorpe Committed by Alex Williamson
Browse files

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: default avatarChristoph Hellwig <hch@lst.de>
Signed-off-by: default avatarJason Gunthorpe <jgg@nvidia.com>
Message-Id: <12-v2-d36939638fc6+d54-vfio2_jgg@nvidia.com>
Signed-off-by: default avatarAlex Williamson <alex.williamson@redhat.com>
parent 15fcc44b
Loading
Loading
Loading
Loading
+7 −43
Original line number Diff line number Diff line
@@ -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;