transport_class.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * transport_class.c - implementation of generic transport classes
  3. * using attribute_containers
  4. *
  5. * Copyright (c) 2005 - James Bottomley <James.Bottomley@steeleye.com>
  6. *
  7. * This file is licensed under GPLv2
  8. *
  9. * The basic idea here is to allow any "device controller" (which
  10. * would most often be a Host Bus Adapter to use the services of one
  11. * or more tranport classes for performing transport specific
  12. * services. Transport specific services are things that the generic
  13. * command layer doesn't want to know about (speed settings, line
  14. * condidtioning, etc), but which the user might be interested in.
  15. * Thus, the HBA's use the routines exported by the transport classes
  16. * to perform these functions. The transport classes export certain
  17. * values to the user via sysfs using attribute containers.
  18. *
  19. * Note: because not every HBA will care about every transport
  20. * attribute, there's a many to one relationship that goes like this:
  21. *
  22. * transport class<-----attribute container<----class device
  23. *
  24. * Usually the attribute container is per-HBA, but the design doesn't
  25. * mandate that. Although most of the services will be specific to
  26. * the actual external storage connection used by the HBA, the generic
  27. * transport class is framed entirely in terms of generic devices to
  28. * allow it to be used by any physical HBA in the system.
  29. */
  30. #include <linux/attribute_container.h>
  31. #include <linux/transport_class.h>
  32. /**
  33. * transport_class_register - register an initial transport class
  34. *
  35. * @tclass: a pointer to the transport class structure to be initialised
  36. *
  37. * The transport class contains an embedded class which is used to
  38. * identify it. The caller should initialise this structure with
  39. * zeros and then generic class must have been initialised with the
  40. * actual transport class unique name. There's a macro
  41. * DECLARE_TRANSPORT_CLASS() to do this (declared classes still must
  42. * be registered).
  43. *
  44. * Returns 0 on success or error on failure.
  45. */
  46. int transport_class_register(struct transport_class *tclass)
  47. {
  48. return class_register(&tclass->class);
  49. }
  50. EXPORT_SYMBOL_GPL(transport_class_register);
  51. /**
  52. * transport_class_unregister - unregister a previously registered class
  53. *
  54. * @tclass: The transport class to unregister
  55. *
  56. * Must be called prior to deallocating the memory for the transport
  57. * class.
  58. */
  59. void transport_class_unregister(struct transport_class *tclass)
  60. {
  61. class_unregister(&tclass->class);
  62. }
  63. EXPORT_SYMBOL_GPL(transport_class_unregister);
  64. static int anon_transport_dummy_function(struct transport_container *tc,
  65. struct device *dev,
  66. struct device *cdev)
  67. {
  68. /* do nothing */
  69. return 0;
  70. }
  71. /**
  72. * anon_transport_class_register - register an anonymous class
  73. *
  74. * @atc: The anon transport class to register
  75. *
  76. * The anonymous transport class contains both a transport class and a
  77. * container. The idea of an anonymous class is that it never
  78. * actually has any device attributes associated with it (and thus
  79. * saves on container storage). So it can only be used for triggering
  80. * events. Use prezero and then use DECLARE_ANON_TRANSPORT_CLASS() to
  81. * initialise the anon transport class storage.
  82. */
  83. int anon_transport_class_register(struct anon_transport_class *atc)
  84. {
  85. int error;
  86. atc->container.class = &atc->tclass.class;
  87. attribute_container_set_no_classdevs(&atc->container);
  88. error = attribute_container_register(&atc->container);
  89. if (error)
  90. return error;
  91. atc->tclass.setup = anon_transport_dummy_function;
  92. atc->tclass.remove = anon_transport_dummy_function;
  93. return 0;
  94. }
  95. EXPORT_SYMBOL_GPL(anon_transport_class_register);
  96. /**
  97. * anon_transport_class_unregister - unregister an anon class
  98. *
  99. * @atc: Pointer to the anon transport class to unregister
  100. *
  101. * Must be called prior to deallocating the memory for the anon
  102. * transport class.
  103. */
  104. void anon_transport_class_unregister(struct anon_transport_class *atc)
  105. {
  106. if (unlikely(attribute_container_unregister(&atc->container)))
  107. BUG();
  108. }
  109. EXPORT_SYMBOL_GPL(anon_transport_class_unregister);
  110. static int transport_setup_classdev(struct attribute_container *cont,
  111. struct device *dev,
  112. struct device *classdev)
  113. {
  114. struct transport_class *tclass = class_to_transport_class(cont->class);
  115. struct transport_container *tcont = attribute_container_to_transport_container(cont);
  116. if (tclass->setup)
  117. tclass->setup(tcont, dev, classdev);
  118. return 0;
  119. }
  120. /**
  121. * transport_setup_device - declare a new dev for transport class association but don't make it visible yet.
  122. * @dev: the generic device representing the entity being added
  123. *
  124. * Usually, dev represents some component in the HBA system (either
  125. * the HBA itself or a device remote across the HBA bus). This
  126. * routine is simply a trigger point to see if any set of transport
  127. * classes wishes to associate with the added device. This allocates
  128. * storage for the class device and initialises it, but does not yet
  129. * add it to the system or add attributes to it (you do this with
  130. * transport_add_device). If you have no need for a separate setup
  131. * and add operations, use transport_register_device (see
  132. * transport_class.h).
  133. */
  134. void transport_setup_device(struct device *dev)
  135. {
  136. attribute_container_add_device(dev, transport_setup_classdev);
  137. }
  138. EXPORT_SYMBOL_GPL(transport_setup_device);
  139. static int transport_add_class_device(struct attribute_container *cont,
  140. struct device *dev,
  141. struct device *classdev)
  142. {
  143. int error = attribute_container_add_class_device(classdev);
  144. struct transport_container *tcont =
  145. attribute_container_to_transport_container(cont);
  146. if (!error && tcont->statistics)
  147. error = sysfs_create_group(&classdev->kobj, tcont->statistics);
  148. return error;
  149. }
  150. /**
  151. * transport_add_device - declare a new dev for transport class association
  152. *
  153. * @dev: the generic device representing the entity being added
  154. *
  155. * Usually, dev represents some component in the HBA system (either
  156. * the HBA itself or a device remote across the HBA bus). This
  157. * routine is simply a trigger point used to add the device to the
  158. * system and register attributes for it.
  159. */
  160. void transport_add_device(struct device *dev)
  161. {
  162. attribute_container_device_trigger(dev, transport_add_class_device);
  163. }
  164. EXPORT_SYMBOL_GPL(transport_add_device);
  165. static int transport_configure(struct attribute_container *cont,
  166. struct device *dev,
  167. struct device *cdev)
  168. {
  169. struct transport_class *tclass = class_to_transport_class(cont->class);
  170. struct transport_container *tcont = attribute_container_to_transport_container(cont);
  171. if (tclass->configure)
  172. tclass->configure(tcont, dev, cdev);
  173. return 0;
  174. }
  175. /**
  176. * transport_configure_device - configure an already set up device
  177. *
  178. * @dev: generic device representing device to be configured
  179. *
  180. * The idea of configure is simply to provide a point within the setup
  181. * process to allow the transport class to extract information from a
  182. * device after it has been setup. This is used in SCSI because we
  183. * have to have a setup device to begin using the HBA, but after we
  184. * send the initial inquiry, we use configure to extract the device
  185. * parameters. The device need not have been added to be configured.
  186. */
  187. void transport_configure_device(struct device *dev)
  188. {
  189. attribute_container_device_trigger(dev, transport_configure);
  190. }
  191. EXPORT_SYMBOL_GPL(transport_configure_device);
  192. static int transport_remove_classdev(struct attribute_container *cont,
  193. struct device *dev,
  194. struct device *classdev)
  195. {
  196. struct transport_container *tcont =
  197. attribute_container_to_transport_container(cont);
  198. struct transport_class *tclass = class_to_transport_class(cont->class);
  199. if (tclass->remove)
  200. tclass->remove(tcont, dev, classdev);
  201. if (tclass->remove != anon_transport_dummy_function) {
  202. if (tcont->statistics)
  203. sysfs_remove_group(&classdev->kobj, tcont->statistics);
  204. attribute_container_class_device_del(classdev);
  205. }
  206. return 0;
  207. }
  208. /**
  209. * transport_remove_device - remove the visibility of a device
  210. *
  211. * @dev: generic device to remove
  212. *
  213. * This call removes the visibility of the device (to the user from
  214. * sysfs), but does not destroy it. To eliminate a device entirely
  215. * you must also call transport_destroy_device. If you don't need to
  216. * do remove and destroy as separate operations, use
  217. * transport_unregister_device() (see transport_class.h) which will
  218. * perform both calls for you.
  219. */
  220. void transport_remove_device(struct device *dev)
  221. {
  222. attribute_container_device_trigger(dev, transport_remove_classdev);
  223. }
  224. EXPORT_SYMBOL_GPL(transport_remove_device);
  225. static void transport_destroy_classdev(struct attribute_container *cont,
  226. struct device *dev,
  227. struct device *classdev)
  228. {
  229. struct transport_class *tclass = class_to_transport_class(cont->class);
  230. if (tclass->remove != anon_transport_dummy_function)
  231. put_device(classdev);
  232. }
  233. /**
  234. * transport_destroy_device - destroy a removed device
  235. *
  236. * @dev: device to eliminate from the transport class.
  237. *
  238. * This call triggers the elimination of storage associated with the
  239. * transport classdev. Note: all it really does is relinquish a
  240. * reference to the classdev. The memory will not be freed until the
  241. * last reference goes to zero. Note also that the classdev retains a
  242. * reference count on dev, so dev too will remain for as long as the
  243. * transport class device remains around.
  244. */
  245. void transport_destroy_device(struct device *dev)
  246. {
  247. attribute_container_remove_device(dev, transport_destroy_classdev);
  248. }
  249. EXPORT_SYMBOL_GPL(transport_destroy_device);