mdev_sysfs.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * File attributes for Mediated devices
  4. *
  5. * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
  6. * Author: Neo Jia <[email protected]>
  7. * Kirti Wankhede <[email protected]>
  8. */
  9. #include <linux/sysfs.h>
  10. #include <linux/ctype.h>
  11. #include <linux/slab.h>
  12. #include <linux/mdev.h>
  13. #include "mdev_private.h"
  14. struct mdev_type_attribute {
  15. struct attribute attr;
  16. ssize_t (*show)(struct mdev_type *mtype,
  17. struct mdev_type_attribute *attr, char *buf);
  18. ssize_t (*store)(struct mdev_type *mtype,
  19. struct mdev_type_attribute *attr, const char *buf,
  20. size_t count);
  21. };
  22. #define MDEV_TYPE_ATTR_RO(_name) \
  23. struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RO(_name)
  24. #define MDEV_TYPE_ATTR_WO(_name) \
  25. struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_WO(_name)
  26. static ssize_t mdev_type_attr_show(struct kobject *kobj,
  27. struct attribute *__attr, char *buf)
  28. {
  29. struct mdev_type_attribute *attr = to_mdev_type_attr(__attr);
  30. struct mdev_type *type = to_mdev_type(kobj);
  31. ssize_t ret = -EIO;
  32. if (attr->show)
  33. ret = attr->show(type, attr, buf);
  34. return ret;
  35. }
  36. static ssize_t mdev_type_attr_store(struct kobject *kobj,
  37. struct attribute *__attr,
  38. const char *buf, size_t count)
  39. {
  40. struct mdev_type_attribute *attr = to_mdev_type_attr(__attr);
  41. struct mdev_type *type = to_mdev_type(kobj);
  42. ssize_t ret = -EIO;
  43. if (attr->store)
  44. ret = attr->store(type, attr, buf, count);
  45. return ret;
  46. }
  47. static const struct sysfs_ops mdev_type_sysfs_ops = {
  48. .show = mdev_type_attr_show,
  49. .store = mdev_type_attr_store,
  50. };
  51. static ssize_t create_store(struct mdev_type *mtype,
  52. struct mdev_type_attribute *attr, const char *buf,
  53. size_t count)
  54. {
  55. char *str;
  56. guid_t uuid;
  57. int ret;
  58. if ((count < UUID_STRING_LEN) || (count > UUID_STRING_LEN + 1))
  59. return -EINVAL;
  60. str = kstrndup(buf, count, GFP_KERNEL);
  61. if (!str)
  62. return -ENOMEM;
  63. ret = guid_parse(str, &uuid);
  64. kfree(str);
  65. if (ret)
  66. return ret;
  67. ret = mdev_device_create(mtype, &uuid);
  68. if (ret)
  69. return ret;
  70. return count;
  71. }
  72. static MDEV_TYPE_ATTR_WO(create);
  73. static ssize_t device_api_show(struct mdev_type *mtype,
  74. struct mdev_type_attribute *attr, char *buf)
  75. {
  76. return sysfs_emit(buf, "%s\n", mtype->parent->mdev_driver->device_api);
  77. }
  78. static MDEV_TYPE_ATTR_RO(device_api);
  79. static ssize_t name_show(struct mdev_type *mtype,
  80. struct mdev_type_attribute *attr, char *buf)
  81. {
  82. return sprintf(buf, "%s\n",
  83. mtype->pretty_name ? mtype->pretty_name : mtype->sysfs_name);
  84. }
  85. static MDEV_TYPE_ATTR_RO(name);
  86. static ssize_t available_instances_show(struct mdev_type *mtype,
  87. struct mdev_type_attribute *attr,
  88. char *buf)
  89. {
  90. struct mdev_driver *drv = mtype->parent->mdev_driver;
  91. if (drv->get_available)
  92. return sysfs_emit(buf, "%u\n", drv->get_available(mtype));
  93. return sysfs_emit(buf, "%u\n",
  94. atomic_read(&mtype->parent->available_instances));
  95. }
  96. static MDEV_TYPE_ATTR_RO(available_instances);
  97. static ssize_t description_show(struct mdev_type *mtype,
  98. struct mdev_type_attribute *attr,
  99. char *buf)
  100. {
  101. return mtype->parent->mdev_driver->show_description(mtype, buf);
  102. }
  103. static MDEV_TYPE_ATTR_RO(description);
  104. static struct attribute *mdev_types_core_attrs[] = {
  105. &mdev_type_attr_create.attr,
  106. &mdev_type_attr_device_api.attr,
  107. &mdev_type_attr_name.attr,
  108. &mdev_type_attr_available_instances.attr,
  109. &mdev_type_attr_description.attr,
  110. NULL,
  111. };
  112. static umode_t mdev_types_core_is_visible(struct kobject *kobj,
  113. struct attribute *attr, int n)
  114. {
  115. if (attr == &mdev_type_attr_description.attr &&
  116. !to_mdev_type(kobj)->parent->mdev_driver->show_description)
  117. return 0;
  118. return attr->mode;
  119. }
  120. static struct attribute_group mdev_type_core_group = {
  121. .attrs = mdev_types_core_attrs,
  122. .is_visible = mdev_types_core_is_visible,
  123. };
  124. static const struct attribute_group *mdev_type_groups[] = {
  125. &mdev_type_core_group,
  126. NULL,
  127. };
  128. static void mdev_type_release(struct kobject *kobj)
  129. {
  130. struct mdev_type *type = to_mdev_type(kobj);
  131. pr_debug("Releasing group %s\n", kobj->name);
  132. /* Pairs with the get in add_mdev_supported_type() */
  133. put_device(type->parent->dev);
  134. }
  135. static struct kobj_type mdev_type_ktype = {
  136. .sysfs_ops = &mdev_type_sysfs_ops,
  137. .release = mdev_type_release,
  138. .default_groups = mdev_type_groups,
  139. };
  140. static int mdev_type_add(struct mdev_parent *parent, struct mdev_type *type)
  141. {
  142. int ret;
  143. type->kobj.kset = parent->mdev_types_kset;
  144. type->parent = parent;
  145. /* Pairs with the put in mdev_type_release() */
  146. get_device(parent->dev);
  147. ret = kobject_init_and_add(&type->kobj, &mdev_type_ktype, NULL,
  148. "%s-%s", dev_driver_string(parent->dev),
  149. type->sysfs_name);
  150. if (ret) {
  151. kobject_put(&type->kobj);
  152. return ret;
  153. }
  154. type->devices_kobj = kobject_create_and_add("devices", &type->kobj);
  155. if (!type->devices_kobj) {
  156. ret = -ENOMEM;
  157. goto attr_devices_failed;
  158. }
  159. return 0;
  160. attr_devices_failed:
  161. kobject_del(&type->kobj);
  162. kobject_put(&type->kobj);
  163. return ret;
  164. }
  165. static void mdev_type_remove(struct mdev_type *type)
  166. {
  167. kobject_put(type->devices_kobj);
  168. kobject_del(&type->kobj);
  169. kobject_put(&type->kobj);
  170. }
  171. /* mdev sysfs functions */
  172. void parent_remove_sysfs_files(struct mdev_parent *parent)
  173. {
  174. int i;
  175. for (i = 0; i < parent->nr_types; i++)
  176. mdev_type_remove(parent->types[i]);
  177. kset_unregister(parent->mdev_types_kset);
  178. }
  179. int parent_create_sysfs_files(struct mdev_parent *parent)
  180. {
  181. int ret, i;
  182. parent->mdev_types_kset = kset_create_and_add("mdev_supported_types",
  183. NULL, &parent->dev->kobj);
  184. if (!parent->mdev_types_kset)
  185. return -ENOMEM;
  186. for (i = 0; i < parent->nr_types; i++) {
  187. ret = mdev_type_add(parent, parent->types[i]);
  188. if (ret)
  189. goto out_err;
  190. }
  191. return 0;
  192. out_err:
  193. while (--i >= 0)
  194. mdev_type_remove(parent->types[i]);
  195. kset_unregister(parent->mdev_types_kset);
  196. return ret;
  197. }
  198. static ssize_t remove_store(struct device *dev, struct device_attribute *attr,
  199. const char *buf, size_t count)
  200. {
  201. struct mdev_device *mdev = to_mdev_device(dev);
  202. unsigned long val;
  203. if (kstrtoul(buf, 0, &val) < 0)
  204. return -EINVAL;
  205. if (val && device_remove_file_self(dev, attr)) {
  206. int ret;
  207. ret = mdev_device_remove(mdev);
  208. if (ret)
  209. return ret;
  210. }
  211. return count;
  212. }
  213. static DEVICE_ATTR_WO(remove);
  214. static struct attribute *mdev_device_attrs[] = {
  215. &dev_attr_remove.attr,
  216. NULL,
  217. };
  218. static const struct attribute_group mdev_device_group = {
  219. .attrs = mdev_device_attrs,
  220. };
  221. const struct attribute_group *mdev_device_groups[] = {
  222. &mdev_device_group,
  223. NULL
  224. };
  225. int mdev_create_sysfs_files(struct mdev_device *mdev)
  226. {
  227. struct mdev_type *type = mdev->type;
  228. struct kobject *kobj = &mdev->dev.kobj;
  229. int ret;
  230. ret = sysfs_create_link(type->devices_kobj, kobj, dev_name(&mdev->dev));
  231. if (ret)
  232. return ret;
  233. ret = sysfs_create_link(kobj, &type->kobj, "mdev_type");
  234. if (ret)
  235. goto type_link_failed;
  236. return ret;
  237. type_link_failed:
  238. sysfs_remove_link(mdev->type->devices_kobj, dev_name(&mdev->dev));
  239. return ret;
  240. }
  241. void mdev_remove_sysfs_files(struct mdev_device *mdev)
  242. {
  243. struct kobject *kobj = &mdev->dev.kobj;
  244. sysfs_remove_link(kobj, "mdev_type");
  245. sysfs_remove_link(mdev->type->devices_kobj, dev_name(&mdev->dev));
  246. }