enclosure.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /*
  2. * Enclosure Services
  3. *
  4. * Copyright (C) 2008 James Bottomley <James.Bottomley@HansenPartnership.com>
  5. *
  6. **-----------------------------------------------------------------------------
  7. **
  8. ** This program is free software; you can redistribute it and/or
  9. ** modify it under the terms of the GNU General Public License
  10. ** version 2 as published by the Free Software Foundation.
  11. **
  12. ** This program is distributed in the hope that it will be useful,
  13. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ** GNU General Public License for more details.
  16. **
  17. ** You should have received a copy of the GNU General Public License
  18. ** along with this program; if not, write to the Free Software
  19. ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. **
  21. **-----------------------------------------------------------------------------
  22. */
  23. #include <linux/device.h>
  24. #include <linux/enclosure.h>
  25. #include <linux/err.h>
  26. #include <linux/list.h>
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/mutex.h>
  30. static LIST_HEAD(container_list);
  31. static DEFINE_MUTEX(container_list_lock);
  32. static struct class enclosure_class;
  33. static struct class enclosure_component_class;
  34. /**
  35. * enclosure_find - find an enclosure given a device
  36. * @dev: the device to find for
  37. *
  38. * Looks through the list of registered enclosures to see
  39. * if it can find a match for a device. Returns NULL if no
  40. * enclosure is found. Obtains a reference to the enclosure class
  41. * device which must be released with class_device_put().
  42. */
  43. struct enclosure_device *enclosure_find(struct device *dev)
  44. {
  45. struct enclosure_device *edev = NULL;
  46. mutex_lock(&container_list_lock);
  47. list_for_each_entry(edev, &container_list, node) {
  48. if (edev->cdev.dev == dev) {
  49. class_device_get(&edev->cdev);
  50. mutex_unlock(&container_list_lock);
  51. return edev;
  52. }
  53. }
  54. mutex_unlock(&container_list_lock);
  55. return NULL;
  56. }
  57. EXPORT_SYMBOL_GPL(enclosure_find);
  58. /**
  59. * enclosure_for_each_device - calls a function for each enclosure
  60. * @fn: the function to call
  61. * @data: the data to pass to each call
  62. *
  63. * Loops over all the enclosures calling the function.
  64. *
  65. * Note, this function uses a mutex which will be held across calls to
  66. * @fn, so it must have non atomic context, and @fn may (although it
  67. * should not) sleep or otherwise cause the mutex to be held for
  68. * indefinite periods
  69. */
  70. int enclosure_for_each_device(int (*fn)(struct enclosure_device *, void *),
  71. void *data)
  72. {
  73. int error = 0;
  74. struct enclosure_device *edev;
  75. mutex_lock(&container_list_lock);
  76. list_for_each_entry(edev, &container_list, node) {
  77. error = fn(edev, data);
  78. if (error)
  79. break;
  80. }
  81. mutex_unlock(&container_list_lock);
  82. return error;
  83. }
  84. EXPORT_SYMBOL_GPL(enclosure_for_each_device);
  85. /**
  86. * enclosure_register - register device as an enclosure
  87. *
  88. * @dev: device containing the enclosure
  89. * @components: number of components in the enclosure
  90. *
  91. * This sets up the device for being an enclosure. Note that @dev does
  92. * not have to be a dedicated enclosure device. It may be some other type
  93. * of device that additionally responds to enclosure services
  94. */
  95. struct enclosure_device *
  96. enclosure_register(struct device *dev, const char *name, int components,
  97. struct enclosure_component_callbacks *cb)
  98. {
  99. struct enclosure_device *edev =
  100. kzalloc(sizeof(struct enclosure_device) +
  101. sizeof(struct enclosure_component)*components,
  102. GFP_KERNEL);
  103. int err, i;
  104. BUG_ON(!cb);
  105. if (!edev)
  106. return ERR_PTR(-ENOMEM);
  107. edev->components = components;
  108. edev->cdev.class = &enclosure_class;
  109. edev->cdev.dev = get_device(dev);
  110. edev->cb = cb;
  111. snprintf(edev->cdev.class_id, BUS_ID_SIZE, "%s", name);
  112. err = class_device_register(&edev->cdev);
  113. if (err)
  114. goto err;
  115. for (i = 0; i < components; i++)
  116. edev->component[i].number = -1;
  117. mutex_lock(&container_list_lock);
  118. list_add_tail(&edev->node, &container_list);
  119. mutex_unlock(&container_list_lock);
  120. return edev;
  121. err:
  122. put_device(edev->cdev.dev);
  123. kfree(edev);
  124. return ERR_PTR(err);
  125. }
  126. EXPORT_SYMBOL_GPL(enclosure_register);
  127. static struct enclosure_component_callbacks enclosure_null_callbacks;
  128. /**
  129. * enclosure_unregister - remove an enclosure
  130. *
  131. * @edev: the registered enclosure to remove;
  132. */
  133. void enclosure_unregister(struct enclosure_device *edev)
  134. {
  135. int i;
  136. mutex_lock(&container_list_lock);
  137. list_del(&edev->node);
  138. mutex_unlock(&container_list_lock);
  139. for (i = 0; i < edev->components; i++)
  140. if (edev->component[i].number != -1)
  141. class_device_unregister(&edev->component[i].cdev);
  142. /* prevent any callbacks into service user */
  143. edev->cb = &enclosure_null_callbacks;
  144. class_device_unregister(&edev->cdev);
  145. }
  146. EXPORT_SYMBOL_GPL(enclosure_unregister);
  147. static void enclosure_release(struct class_device *cdev)
  148. {
  149. struct enclosure_device *edev = to_enclosure_device(cdev);
  150. put_device(cdev->dev);
  151. kfree(edev);
  152. }
  153. static void enclosure_component_release(struct class_device *cdev)
  154. {
  155. if (cdev->dev)
  156. put_device(cdev->dev);
  157. class_device_put(cdev->parent);
  158. }
  159. /**
  160. * enclosure_component_register - add a particular component to an enclosure
  161. * @edev: the enclosure to add the component
  162. * @num: the device number
  163. * @type: the type of component being added
  164. * @name: an optional name to appear in sysfs (leave NULL if none)
  165. *
  166. * Registers the component. The name is optional for enclosures that
  167. * give their components a unique name. If not, leave the field NULL
  168. * and a name will be assigned.
  169. *
  170. * Returns a pointer to the enclosure component or an error.
  171. */
  172. struct enclosure_component *
  173. enclosure_component_register(struct enclosure_device *edev,
  174. unsigned int number,
  175. enum enclosure_component_type type,
  176. const char *name)
  177. {
  178. struct enclosure_component *ecomp;
  179. struct class_device *cdev;
  180. int err;
  181. if (number >= edev->components)
  182. return ERR_PTR(-EINVAL);
  183. ecomp = &edev->component[number];
  184. if (ecomp->number != -1)
  185. return ERR_PTR(-EINVAL);
  186. ecomp->type = type;
  187. ecomp->number = number;
  188. cdev = &ecomp->cdev;
  189. cdev->parent = class_device_get(&edev->cdev);
  190. cdev->class = &enclosure_component_class;
  191. if (name)
  192. snprintf(cdev->class_id, BUS_ID_SIZE, "%s", name);
  193. else
  194. snprintf(cdev->class_id, BUS_ID_SIZE, "%u", number);
  195. err = class_device_register(cdev);
  196. if (err)
  197. ERR_PTR(err);
  198. return ecomp;
  199. }
  200. EXPORT_SYMBOL_GPL(enclosure_component_register);
  201. /**
  202. * enclosure_add_device - add a device as being part of an enclosure
  203. * @edev: the enclosure device being added to.
  204. * @num: the number of the component
  205. * @dev: the device being added
  206. *
  207. * Declares a real device to reside in slot (or identifier) @num of an
  208. * enclosure. This will cause the relevant sysfs links to appear.
  209. * This function may also be used to change a device associated with
  210. * an enclosure without having to call enclosure_remove_device() in
  211. * between.
  212. *
  213. * Returns zero on success or an error.
  214. */
  215. int enclosure_add_device(struct enclosure_device *edev, int component,
  216. struct device *dev)
  217. {
  218. struct class_device *cdev;
  219. if (!edev || component >= edev->components)
  220. return -EINVAL;
  221. cdev = &edev->component[component].cdev;
  222. class_device_del(cdev);
  223. if (cdev->dev)
  224. put_device(cdev->dev);
  225. cdev->dev = get_device(dev);
  226. return class_device_add(cdev);
  227. }
  228. EXPORT_SYMBOL_GPL(enclosure_add_device);
  229. /**
  230. * enclosure_remove_device - remove a device from an enclosure
  231. * @edev: the enclosure device
  232. * @num: the number of the component to remove
  233. *
  234. * Returns zero on success or an error.
  235. *
  236. */
  237. int enclosure_remove_device(struct enclosure_device *edev, int component)
  238. {
  239. struct class_device *cdev;
  240. if (!edev || component >= edev->components)
  241. return -EINVAL;
  242. cdev = &edev->component[component].cdev;
  243. class_device_del(cdev);
  244. if (cdev->dev)
  245. put_device(cdev->dev);
  246. cdev->dev = NULL;
  247. return class_device_add(cdev);
  248. }
  249. EXPORT_SYMBOL_GPL(enclosure_remove_device);
  250. /*
  251. * sysfs pieces below
  252. */
  253. static ssize_t enclosure_show_components(struct class_device *cdev, char *buf)
  254. {
  255. struct enclosure_device *edev = to_enclosure_device(cdev);
  256. return snprintf(buf, 40, "%d\n", edev->components);
  257. }
  258. static struct class_device_attribute enclosure_attrs[] = {
  259. __ATTR(components, S_IRUGO, enclosure_show_components, NULL),
  260. __ATTR_NULL
  261. };
  262. static struct class enclosure_class = {
  263. .name = "enclosure",
  264. .owner = THIS_MODULE,
  265. .release = enclosure_release,
  266. .class_dev_attrs = enclosure_attrs,
  267. };
  268. static const char *const enclosure_status [] = {
  269. [ENCLOSURE_STATUS_UNSUPPORTED] = "unsupported",
  270. [ENCLOSURE_STATUS_OK] = "OK",
  271. [ENCLOSURE_STATUS_CRITICAL] = "critical",
  272. [ENCLOSURE_STATUS_NON_CRITICAL] = "non-critical",
  273. [ENCLOSURE_STATUS_UNRECOVERABLE] = "unrecoverable",
  274. [ENCLOSURE_STATUS_NOT_INSTALLED] = "not installed",
  275. [ENCLOSURE_STATUS_UNKNOWN] = "unknown",
  276. [ENCLOSURE_STATUS_UNAVAILABLE] = "unavailable",
  277. };
  278. static const char *const enclosure_type [] = {
  279. [ENCLOSURE_COMPONENT_DEVICE] = "device",
  280. [ENCLOSURE_COMPONENT_ARRAY_DEVICE] = "array device",
  281. };
  282. static ssize_t get_component_fault(struct class_device *cdev, char *buf)
  283. {
  284. struct enclosure_device *edev = to_enclosure_device(cdev->parent);
  285. struct enclosure_component *ecomp = to_enclosure_component(cdev);
  286. if (edev->cb->get_fault)
  287. edev->cb->get_fault(edev, ecomp);
  288. return snprintf(buf, 40, "%d\n", ecomp->fault);
  289. }
  290. static ssize_t set_component_fault(struct class_device *cdev, const char *buf,
  291. size_t count)
  292. {
  293. struct enclosure_device *edev = to_enclosure_device(cdev->parent);
  294. struct enclosure_component *ecomp = to_enclosure_component(cdev);
  295. int val = simple_strtoul(buf, NULL, 0);
  296. if (edev->cb->set_fault)
  297. edev->cb->set_fault(edev, ecomp, val);
  298. return count;
  299. }
  300. static ssize_t get_component_status(struct class_device *cdev, char *buf)
  301. {
  302. struct enclosure_device *edev = to_enclosure_device(cdev->parent);
  303. struct enclosure_component *ecomp = to_enclosure_component(cdev);
  304. if (edev->cb->get_status)
  305. edev->cb->get_status(edev, ecomp);
  306. return snprintf(buf, 40, "%s\n", enclosure_status[ecomp->status]);
  307. }
  308. static ssize_t set_component_status(struct class_device *cdev, const char *buf,
  309. size_t count)
  310. {
  311. struct enclosure_device *edev = to_enclosure_device(cdev->parent);
  312. struct enclosure_component *ecomp = to_enclosure_component(cdev);
  313. int i;
  314. for (i = 0; enclosure_status[i]; i++) {
  315. if (strncmp(buf, enclosure_status[i],
  316. strlen(enclosure_status[i])) == 0 &&
  317. (buf[strlen(enclosure_status[i])] == '\n' ||
  318. buf[strlen(enclosure_status[i])] == '\0'))
  319. break;
  320. }
  321. if (enclosure_status[i] && edev->cb->set_status) {
  322. edev->cb->set_status(edev, ecomp, i);
  323. return count;
  324. } else
  325. return -EINVAL;
  326. }
  327. static ssize_t get_component_active(struct class_device *cdev, char *buf)
  328. {
  329. struct enclosure_device *edev = to_enclosure_device(cdev->parent);
  330. struct enclosure_component *ecomp = to_enclosure_component(cdev);
  331. if (edev->cb->get_active)
  332. edev->cb->get_active(edev, ecomp);
  333. return snprintf(buf, 40, "%d\n", ecomp->active);
  334. }
  335. static ssize_t set_component_active(struct class_device *cdev, const char *buf,
  336. size_t count)
  337. {
  338. struct enclosure_device *edev = to_enclosure_device(cdev->parent);
  339. struct enclosure_component *ecomp = to_enclosure_component(cdev);
  340. int val = simple_strtoul(buf, NULL, 0);
  341. if (edev->cb->set_active)
  342. edev->cb->set_active(edev, ecomp, val);
  343. return count;
  344. }
  345. static ssize_t get_component_locate(struct class_device *cdev, char *buf)
  346. {
  347. struct enclosure_device *edev = to_enclosure_device(cdev->parent);
  348. struct enclosure_component *ecomp = to_enclosure_component(cdev);
  349. if (edev->cb->get_locate)
  350. edev->cb->get_locate(edev, ecomp);
  351. return snprintf(buf, 40, "%d\n", ecomp->locate);
  352. }
  353. static ssize_t set_component_locate(struct class_device *cdev, const char *buf,
  354. size_t count)
  355. {
  356. struct enclosure_device *edev = to_enclosure_device(cdev->parent);
  357. struct enclosure_component *ecomp = to_enclosure_component(cdev);
  358. int val = simple_strtoul(buf, NULL, 0);
  359. if (edev->cb->set_locate)
  360. edev->cb->set_locate(edev, ecomp, val);
  361. return count;
  362. }
  363. static ssize_t get_component_type(struct class_device *cdev, char *buf)
  364. {
  365. struct enclosure_component *ecomp = to_enclosure_component(cdev);
  366. return snprintf(buf, 40, "%s\n", enclosure_type[ecomp->type]);
  367. }
  368. static struct class_device_attribute enclosure_component_attrs[] = {
  369. __ATTR(fault, S_IRUGO | S_IWUSR, get_component_fault,
  370. set_component_fault),
  371. __ATTR(status, S_IRUGO | S_IWUSR, get_component_status,
  372. set_component_status),
  373. __ATTR(active, S_IRUGO | S_IWUSR, get_component_active,
  374. set_component_active),
  375. __ATTR(locate, S_IRUGO | S_IWUSR, get_component_locate,
  376. set_component_locate),
  377. __ATTR(type, S_IRUGO, get_component_type, NULL),
  378. __ATTR_NULL
  379. };
  380. static struct class enclosure_component_class = {
  381. .name = "enclosure_component",
  382. .owner = THIS_MODULE,
  383. .class_dev_attrs = enclosure_component_attrs,
  384. .release = enclosure_component_release,
  385. };
  386. static int __init enclosure_init(void)
  387. {
  388. int err;
  389. err = class_register(&enclosure_class);
  390. if (err)
  391. return err;
  392. err = class_register(&enclosure_component_class);
  393. if (err)
  394. goto err_out;
  395. return 0;
  396. err_out:
  397. class_unregister(&enclosure_class);
  398. return err;
  399. }
  400. static void __exit enclosure_exit(void)
  401. {
  402. class_unregister(&enclosure_component_class);
  403. class_unregister(&enclosure_class);
  404. }
  405. module_init(enclosure_init);
  406. module_exit(enclosure_exit);
  407. MODULE_AUTHOR("James Bottomley");
  408. MODULE_DESCRIPTION("Enclosure Services");
  409. MODULE_LICENSE("GPL v2");