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/device.h>
13 #include <linux/slab.h>
14 #include <linux/uuid.h>
15 #include <linux/mdev.h>
17 #include "mdev_private.h"
19 /* Static functions */
21 static ssize_t mdev_type_attr_show(struct kobject *kobj,
22 struct attribute *__attr, char *buf)
24 struct mdev_type_attribute *attr = to_mdev_type_attr(__attr);
25 struct mdev_type *type = to_mdev_type(kobj);
29 ret = attr->show(type, attr, buf);
33 static ssize_t mdev_type_attr_store(struct kobject *kobj,
34 struct attribute *__attr,
35 const char *buf, size_t count)
37 struct mdev_type_attribute *attr = to_mdev_type_attr(__attr);
38 struct mdev_type *type = to_mdev_type(kobj);
42 ret = attr->store(type, attr, buf, count);
46 static const struct sysfs_ops mdev_type_sysfs_ops = {
47 .show = mdev_type_attr_show,
48 .store = mdev_type_attr_store,
51 static ssize_t create_store(struct mdev_type *mtype,
52 struct mdev_type_attribute *attr, const char *buf,
59 if ((count < UUID_STRING_LEN) || (count > UUID_STRING_LEN + 1))
62 str = kstrndup(buf, count, GFP_KERNEL);
66 ret = guid_parse(str, &uuid);
71 ret = mdev_device_create(mtype, &uuid);
78 static MDEV_TYPE_ATTR_WO(create);
80 static void mdev_type_release(struct kobject *kobj)
82 struct mdev_type *type = to_mdev_type(kobj);
84 pr_debug("Releasing group %s\n", kobj->name);
85 /* Pairs with the get in add_mdev_supported_type() */
86 mdev_put_parent(type->parent);
90 static struct kobj_type mdev_type_ktype = {
91 .sysfs_ops = &mdev_type_sysfs_ops,
92 .release = mdev_type_release,
95 static struct mdev_type *add_mdev_supported_type(struct mdev_parent *parent,
96 unsigned int type_group_id)
98 struct mdev_type *type;
99 struct attribute_group *group =
100 parent->mdev_driver->supported_type_groups[type_group_id];
104 pr_err("%s: Type name empty!\n", __func__);
105 return ERR_PTR(-EINVAL);
108 type = kzalloc(sizeof(*type), GFP_KERNEL);
110 return ERR_PTR(-ENOMEM);
112 type->kobj.kset = parent->mdev_types_kset;
113 type->parent = parent;
114 /* Pairs with the put in mdev_type_release() */
115 mdev_get_parent(parent);
116 type->type_group_id = type_group_id;
118 ret = kobject_init_and_add(&type->kobj, &mdev_type_ktype, NULL,
119 "%s-%s", dev_driver_string(parent->dev),
122 kobject_put(&type->kobj);
126 ret = sysfs_create_file(&type->kobj, &mdev_type_attr_create.attr);
128 goto attr_create_failed;
130 type->devices_kobj = kobject_create_and_add("devices", &type->kobj);
131 if (!type->devices_kobj) {
133 goto attr_devices_failed;
136 ret = sysfs_create_files(&type->kobj,
137 (const struct attribute **)group->attrs);
145 kobject_put(type->devices_kobj);
147 sysfs_remove_file(&type->kobj, &mdev_type_attr_create.attr);
149 kobject_del(&type->kobj);
150 kobject_put(&type->kobj);
154 static void remove_mdev_supported_type(struct mdev_type *type)
156 struct attribute_group *group =
157 type->parent->mdev_driver->supported_type_groups[type->type_group_id];
159 sysfs_remove_files(&type->kobj,
160 (const struct attribute **)group->attrs);
161 kobject_put(type->devices_kobj);
162 sysfs_remove_file(&type->kobj, &mdev_type_attr_create.attr);
163 kobject_del(&type->kobj);
164 kobject_put(&type->kobj);
167 static int add_mdev_supported_type_groups(struct mdev_parent *parent)
171 for (i = 0; parent->mdev_driver->supported_type_groups[i]; i++) {
172 struct mdev_type *type;
174 type = add_mdev_supported_type(parent, i);
176 struct mdev_type *ltype, *tmp;
178 list_for_each_entry_safe(ltype, tmp, &parent->type_list,
180 list_del(<ype->next);
181 remove_mdev_supported_type(ltype);
183 return PTR_ERR(type);
185 list_add(&type->next, &parent->type_list);
190 /* mdev sysfs functions */
191 void parent_remove_sysfs_files(struct mdev_parent *parent)
193 struct mdev_type *type, *tmp;
195 list_for_each_entry_safe(type, tmp, &parent->type_list, next) {
196 list_del(&type->next);
197 remove_mdev_supported_type(type);
200 kset_unregister(parent->mdev_types_kset);
203 int parent_create_sysfs_files(struct mdev_parent *parent)
207 parent->mdev_types_kset = kset_create_and_add("mdev_supported_types",
208 NULL, &parent->dev->kobj);
210 if (!parent->mdev_types_kset)
213 INIT_LIST_HEAD(&parent->type_list);
215 ret = add_mdev_supported_type_groups(parent);
221 kset_unregister(parent->mdev_types_kset);
225 static ssize_t remove_store(struct device *dev, struct device_attribute *attr,
226 const char *buf, size_t count)
228 struct mdev_device *mdev = to_mdev_device(dev);
231 if (kstrtoul(buf, 0, &val) < 0)
234 if (val && device_remove_file_self(dev, attr)) {
237 ret = mdev_device_remove(mdev);
245 static DEVICE_ATTR_WO(remove);
247 static struct attribute *mdev_device_attrs[] = {
248 &dev_attr_remove.attr,
252 static const struct attribute_group mdev_device_group = {
253 .attrs = mdev_device_attrs,
256 const struct attribute_group *mdev_device_groups[] = {
261 int mdev_create_sysfs_files(struct mdev_device *mdev)
263 struct mdev_type *type = mdev->type;
264 struct kobject *kobj = &mdev->dev.kobj;
267 ret = sysfs_create_link(type->devices_kobj, kobj, dev_name(&mdev->dev));
271 ret = sysfs_create_link(kobj, &type->kobj, "mdev_type");
273 goto type_link_failed;
277 sysfs_remove_link(mdev->type->devices_kobj, dev_name(&mdev->dev));
281 void mdev_remove_sysfs_files(struct mdev_device *mdev)
283 struct kobject *kobj = &mdev->dev.kobj;
285 sysfs_remove_link(kobj, "mdev_type");
286 sysfs_remove_link(mdev->type->devices_kobj, dev_name(&mdev->dev));