1 // SPDX-License-Identifier: GPL-2.0-only
3 * File attributes for Mediated devices
5 * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
6 * Author: Neo Jia <cjia@nvidia.com>
7 * Kirti Wankhede <kwankhede@nvidia.com>
10 #include <linux/sysfs.h>
11 #include <linux/ctype.h>
12 #include <linux/slab.h>
13 #include <linux/mdev.h>
15 #include "mdev_private.h"
17 /* Static functions */
19 static ssize_t mdev_type_attr_show(struct kobject *kobj,
20 struct attribute *__attr, char *buf)
22 struct mdev_type_attribute *attr = to_mdev_type_attr(__attr);
23 struct mdev_type *type = to_mdev_type(kobj);
27 ret = attr->show(type, attr, buf);
31 static ssize_t mdev_type_attr_store(struct kobject *kobj,
32 struct attribute *__attr,
33 const char *buf, size_t count)
35 struct mdev_type_attribute *attr = to_mdev_type_attr(__attr);
36 struct mdev_type *type = to_mdev_type(kobj);
40 ret = attr->store(type, attr, buf, count);
44 static const struct sysfs_ops mdev_type_sysfs_ops = {
45 .show = mdev_type_attr_show,
46 .store = mdev_type_attr_store,
49 static ssize_t create_store(struct mdev_type *mtype,
50 struct mdev_type_attribute *attr, const char *buf,
57 if ((count < UUID_STRING_LEN) || (count > UUID_STRING_LEN + 1))
60 str = kstrndup(buf, count, GFP_KERNEL);
64 ret = guid_parse(str, &uuid);
69 ret = mdev_device_create(mtype, &uuid);
76 static MDEV_TYPE_ATTR_WO(create);
78 static void mdev_type_release(struct kobject *kobj)
80 struct mdev_type *type = to_mdev_type(kobj);
82 pr_debug("Releasing group %s\n", kobj->name);
83 /* Pairs with the get in add_mdev_supported_type() */
84 mdev_put_parent(type->parent);
88 static struct kobj_type mdev_type_ktype = {
89 .sysfs_ops = &mdev_type_sysfs_ops,
90 .release = mdev_type_release,
93 static struct mdev_type *add_mdev_supported_type(struct mdev_parent *parent,
94 unsigned int type_group_id)
96 struct mdev_type *type;
97 struct attribute_group *group =
98 parent->mdev_driver->supported_type_groups[type_group_id];
102 pr_err("%s: Type name empty!\n", __func__);
103 return ERR_PTR(-EINVAL);
106 type = kzalloc(sizeof(*type), GFP_KERNEL);
108 return ERR_PTR(-ENOMEM);
110 type->kobj.kset = parent->mdev_types_kset;
111 type->parent = parent;
112 /* Pairs with the put in mdev_type_release() */
113 mdev_get_parent(parent);
114 type->type_group_id = type_group_id;
116 ret = kobject_init_and_add(&type->kobj, &mdev_type_ktype, NULL,
117 "%s-%s", dev_driver_string(parent->dev),
120 kobject_put(&type->kobj);
124 ret = sysfs_create_file(&type->kobj, &mdev_type_attr_create.attr);
126 goto attr_create_failed;
128 type->devices_kobj = kobject_create_and_add("devices", &type->kobj);
129 if (!type->devices_kobj) {
131 goto attr_devices_failed;
134 ret = sysfs_create_files(&type->kobj,
135 (const struct attribute **)group->attrs);
143 kobject_put(type->devices_kobj);
145 sysfs_remove_file(&type->kobj, &mdev_type_attr_create.attr);
147 kobject_del(&type->kobj);
148 kobject_put(&type->kobj);
152 static void remove_mdev_supported_type(struct mdev_type *type)
154 struct attribute_group *group =
155 type->parent->mdev_driver->supported_type_groups[type->type_group_id];
157 sysfs_remove_files(&type->kobj,
158 (const struct attribute **)group->attrs);
159 kobject_put(type->devices_kobj);
160 sysfs_remove_file(&type->kobj, &mdev_type_attr_create.attr);
161 kobject_del(&type->kobj);
162 kobject_put(&type->kobj);
165 static int add_mdev_supported_type_groups(struct mdev_parent *parent)
169 for (i = 0; parent->mdev_driver->supported_type_groups[i]; i++) {
170 struct mdev_type *type;
172 type = add_mdev_supported_type(parent, i);
174 struct mdev_type *ltype, *tmp;
176 list_for_each_entry_safe(ltype, tmp, &parent->type_list,
178 list_del(<ype->next);
179 remove_mdev_supported_type(ltype);
181 return PTR_ERR(type);
183 list_add(&type->next, &parent->type_list);
188 /* mdev sysfs functions */
189 void parent_remove_sysfs_files(struct mdev_parent *parent)
191 struct mdev_type *type, *tmp;
193 list_for_each_entry_safe(type, tmp, &parent->type_list, next) {
194 list_del(&type->next);
195 remove_mdev_supported_type(type);
198 kset_unregister(parent->mdev_types_kset);
201 int parent_create_sysfs_files(struct mdev_parent *parent)
205 parent->mdev_types_kset = kset_create_and_add("mdev_supported_types",
206 NULL, &parent->dev->kobj);
208 if (!parent->mdev_types_kset)
211 INIT_LIST_HEAD(&parent->type_list);
213 ret = add_mdev_supported_type_groups(parent);
219 kset_unregister(parent->mdev_types_kset);
223 static ssize_t remove_store(struct device *dev, struct device_attribute *attr,
224 const char *buf, size_t count)
226 struct mdev_device *mdev = to_mdev_device(dev);
229 if (kstrtoul(buf, 0, &val) < 0)
232 if (val && device_remove_file_self(dev, attr)) {
235 ret = mdev_device_remove(mdev);
243 static DEVICE_ATTR_WO(remove);
245 static struct attribute *mdev_device_attrs[] = {
246 &dev_attr_remove.attr,
250 static const struct attribute_group mdev_device_group = {
251 .attrs = mdev_device_attrs,
254 const struct attribute_group *mdev_device_groups[] = {
259 int mdev_create_sysfs_files(struct mdev_device *mdev)
261 struct mdev_type *type = mdev->type;
262 struct kobject *kobj = &mdev->dev.kobj;
265 ret = sysfs_create_link(type->devices_kobj, kobj, dev_name(&mdev->dev));
269 ret = sysfs_create_link(kobj, &type->kobj, "mdev_type");
271 goto type_link_failed;
275 sysfs_remove_link(mdev->type->devices_kobj, dev_name(&mdev->dev));
279 void mdev_remove_sysfs_files(struct mdev_device *mdev)
281 struct kobject *kobj = &mdev->dev.kobj;
283 sysfs_remove_link(kobj, "mdev_type");
284 sysfs_remove_link(mdev->type->devices_kobj, dev_name(&mdev->dev));