mdev_core.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Mediated device Core Driver
  4. *
  5. * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
  6. * Author: Neo Jia <cjia@nvidia.com>
  7. * Kirti Wankhede <kwankhede@nvidia.com>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/slab.h>
  11. #include <linux/sysfs.h>
  12. #include <linux/mdev.h>
  13. #include "mdev_private.h"
  14. #define DRIVER_VERSION "0.1"
  15. #define DRIVER_AUTHOR "NVIDIA Corporation"
  16. #define DRIVER_DESC "Mediated device Core Driver"
  17. static struct class_compat *mdev_bus_compat_class;
  18. static LIST_HEAD(mdev_list);
  19. static DEFINE_MUTEX(mdev_list_lock);
  20. /* Caller must hold parent unreg_sem read or write lock */
  21. static void mdev_device_remove_common(struct mdev_device *mdev)
  22. {
  23. struct mdev_parent *parent = mdev->type->parent;
  24. mdev_remove_sysfs_files(mdev);
  25. device_del(&mdev->dev);
  26. lockdep_assert_held(&parent->unreg_sem);
  27. /* Balances with device_initialize() */
  28. put_device(&mdev->dev);
  29. }
  30. static int mdev_device_remove_cb(struct device *dev, void *data)
  31. {
  32. if (dev->bus == &mdev_bus_type)
  33. mdev_device_remove_common(to_mdev_device(dev));
  34. return 0;
  35. }
  36. /*
  37. * mdev_register_parent: Register a device as parent for mdevs
  38. * @parent: parent structure registered
  39. * @dev: device structure representing parent device.
  40. * @mdev_driver: Device driver to bind to the newly created mdev
  41. * @types: Array of supported mdev types
  42. * @nr_types: Number of entries in @types
  43. *
  44. * Registers the @parent stucture as a parent for mdev types and thus mdev
  45. * devices. The caller needs to hold a reference on @dev that must not be
  46. * released until after the call to mdev_unregister_parent().
  47. *
  48. * Returns a negative value on error, otherwise 0.
  49. */
  50. int mdev_register_parent(struct mdev_parent *parent, struct device *dev,
  51. struct mdev_driver *mdev_driver, struct mdev_type **types,
  52. unsigned int nr_types)
  53. {
  54. char *env_string = "MDEV_STATE=registered";
  55. char *envp[] = { env_string, NULL };
  56. int ret;
  57. memset(parent, 0, sizeof(*parent));
  58. init_rwsem(&parent->unreg_sem);
  59. parent->dev = dev;
  60. parent->mdev_driver = mdev_driver;
  61. parent->types = types;
  62. parent->nr_types = nr_types;
  63. atomic_set(&parent->available_instances, mdev_driver->max_instances);
  64. ret = parent_create_sysfs_files(parent);
  65. if (ret)
  66. return ret;
  67. ret = class_compat_create_link(mdev_bus_compat_class, dev, NULL);
  68. if (ret)
  69. dev_warn(dev, "Failed to create compatibility class link\n");
  70. dev_info(dev, "MDEV: Registered\n");
  71. kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
  72. return 0;
  73. }
  74. EXPORT_SYMBOL(mdev_register_parent);
  75. /*
  76. * mdev_unregister_parent : Unregister a parent device
  77. * @parent: parent structure to unregister
  78. */
  79. void mdev_unregister_parent(struct mdev_parent *parent)
  80. {
  81. char *env_string = "MDEV_STATE=unregistered";
  82. char *envp[] = { env_string, NULL };
  83. dev_info(parent->dev, "MDEV: Unregistering\n");
  84. down_write(&parent->unreg_sem);
  85. class_compat_remove_link(mdev_bus_compat_class, parent->dev, NULL);
  86. device_for_each_child(parent->dev, NULL, mdev_device_remove_cb);
  87. parent_remove_sysfs_files(parent);
  88. up_write(&parent->unreg_sem);
  89. kobject_uevent_env(&parent->dev->kobj, KOBJ_CHANGE, envp);
  90. }
  91. EXPORT_SYMBOL(mdev_unregister_parent);
  92. static void mdev_device_release(struct device *dev)
  93. {
  94. struct mdev_device *mdev = to_mdev_device(dev);
  95. struct mdev_parent *parent = mdev->type->parent;
  96. mutex_lock(&mdev_list_lock);
  97. list_del(&mdev->next);
  98. if (!parent->mdev_driver->get_available)
  99. atomic_inc(&parent->available_instances);
  100. mutex_unlock(&mdev_list_lock);
  101. /* Pairs with the get in mdev_device_create() */
  102. kobject_put(&mdev->type->kobj);
  103. dev_dbg(&mdev->dev, "MDEV: destroying\n");
  104. kfree(mdev);
  105. }
  106. int mdev_device_create(struct mdev_type *type, const guid_t *uuid)
  107. {
  108. int ret;
  109. struct mdev_device *mdev, *tmp;
  110. struct mdev_parent *parent = type->parent;
  111. struct mdev_driver *drv = parent->mdev_driver;
  112. mutex_lock(&mdev_list_lock);
  113. /* Check for duplicate */
  114. list_for_each_entry(tmp, &mdev_list, next) {
  115. if (guid_equal(&tmp->uuid, uuid)) {
  116. mutex_unlock(&mdev_list_lock);
  117. return -EEXIST;
  118. }
  119. }
  120. if (!drv->get_available) {
  121. /*
  122. * Note: that non-atomic read and dec is fine here because
  123. * all modifications are under mdev_list_lock.
  124. */
  125. if (!atomic_read(&parent->available_instances)) {
  126. mutex_unlock(&mdev_list_lock);
  127. return -EUSERS;
  128. }
  129. atomic_dec(&parent->available_instances);
  130. }
  131. mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
  132. if (!mdev) {
  133. mutex_unlock(&mdev_list_lock);
  134. return -ENOMEM;
  135. }
  136. device_initialize(&mdev->dev);
  137. mdev->dev.parent = parent->dev;
  138. mdev->dev.bus = &mdev_bus_type;
  139. mdev->dev.release = mdev_device_release;
  140. mdev->dev.groups = mdev_device_groups;
  141. mdev->type = type;
  142. /* Pairs with the put in mdev_device_release() */
  143. kobject_get(&type->kobj);
  144. guid_copy(&mdev->uuid, uuid);
  145. list_add(&mdev->next, &mdev_list);
  146. mutex_unlock(&mdev_list_lock);
  147. ret = dev_set_name(&mdev->dev, "%pUl", uuid);
  148. if (ret)
  149. goto out_put_device;
  150. /* Check if parent unregistration has started */
  151. if (!down_read_trylock(&parent->unreg_sem)) {
  152. ret = -ENODEV;
  153. goto out_put_device;
  154. }
  155. ret = device_add(&mdev->dev);
  156. if (ret)
  157. goto out_unlock;
  158. ret = device_driver_attach(&drv->driver, &mdev->dev);
  159. if (ret)
  160. goto out_del;
  161. ret = mdev_create_sysfs_files(mdev);
  162. if (ret)
  163. goto out_del;
  164. mdev->active = true;
  165. dev_dbg(&mdev->dev, "MDEV: created\n");
  166. up_read(&parent->unreg_sem);
  167. return 0;
  168. out_del:
  169. device_del(&mdev->dev);
  170. out_unlock:
  171. up_read(&parent->unreg_sem);
  172. out_put_device:
  173. put_device(&mdev->dev);
  174. return ret;
  175. }
  176. int mdev_device_remove(struct mdev_device *mdev)
  177. {
  178. struct mdev_device *tmp;
  179. struct mdev_parent *parent = mdev->type->parent;
  180. mutex_lock(&mdev_list_lock);
  181. list_for_each_entry(tmp, &mdev_list, next) {
  182. if (tmp == mdev)
  183. break;
  184. }
  185. if (tmp != mdev) {
  186. mutex_unlock(&mdev_list_lock);
  187. return -ENODEV;
  188. }
  189. if (!mdev->active) {
  190. mutex_unlock(&mdev_list_lock);
  191. return -EAGAIN;
  192. }
  193. mdev->active = false;
  194. mutex_unlock(&mdev_list_lock);
  195. /* Check if parent unregistration has started */
  196. if (!down_read_trylock(&parent->unreg_sem))
  197. return -ENODEV;
  198. mdev_device_remove_common(mdev);
  199. up_read(&parent->unreg_sem);
  200. return 0;
  201. }
  202. static int __init mdev_init(void)
  203. {
  204. int ret;
  205. ret = bus_register(&mdev_bus_type);
  206. if (ret)
  207. return ret;
  208. mdev_bus_compat_class = class_compat_register("mdev_bus");
  209. if (!mdev_bus_compat_class) {
  210. bus_unregister(&mdev_bus_type);
  211. return -ENOMEM;
  212. }
  213. return 0;
  214. }
  215. static void __exit mdev_exit(void)
  216. {
  217. class_compat_unregister(mdev_bus_compat_class);
  218. bus_unregister(&mdev_bus_type);
  219. }
  220. subsys_initcall(mdev_init)
  221. module_exit(mdev_exit)
  222. MODULE_VERSION(DRIVER_VERSION);
  223. MODULE_LICENSE("GPL v2");
  224. MODULE_AUTHOR(DRIVER_AUTHOR);
  225. MODULE_DESCRIPTION(DRIVER_DESC);