enclosure.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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 device_put().
  42. */
  43. struct enclosure_device *enclosure_find(struct device *dev)
  44. {
  45. struct enclosure_device *edev;
  46. mutex_lock(&container_list_lock);
  47. list_for_each_entry(edev, &container_list, node) {
  48. if (edev->edev.parent == dev) {
  49. get_device(&edev->edev);
  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->edev.class = &enclosure_class;
  109. edev->edev.parent = get_device(dev);
  110. edev->cb = cb;
  111. snprintf(edev->edev.bus_id, BUS_ID_SIZE, "%s", name);
  112. err = device_register(&edev->edev);
  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->edev.parent);
  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. device_unregister(&edev->component[i].cdev);
  142. /* prevent any callbacks into service user */
  143. edev->cb = &enclosure_null_callbacks;
  144. device_unregister(&edev->edev);
  145. }
  146. EXPORT_SYMBOL_GPL(enclosure_unregister);
  147. static void enclosure_release(struct device *cdev)
  148. {
  149. struct enclosure_device *edev = to_enclosure_device(cdev);
  150. put_device(cdev->parent);
  151. kfree(edev);
  152. }
  153. static void enclosure_component_release(struct device *dev)
  154. {
  155. struct enclosure_component *cdev = to_enclosure_component(dev);
  156. put_device(cdev->dev);
  157. put_device(dev->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 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 = get_device(&edev->edev);
  190. cdev->class = &enclosure_component_class;
  191. if (name)
  192. snprintf(cdev->bus_id, BUS_ID_SIZE, "%s", name);
  193. else
  194. snprintf(cdev->bus_id, BUS_ID_SIZE, "%u", number);
  195. err = 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 enclosure_component *cdev;
  219. if (!edev || component >= edev->components)
  220. return -EINVAL;
  221. cdev = &edev->component[component];
  222. device_del(&cdev->cdev);
  223. put_device(cdev->dev);
  224. cdev->dev = get_device(dev);
  225. return device_add(&cdev->cdev);
  226. }
  227. EXPORT_SYMBOL_GPL(enclosure_add_device);
  228. /**
  229. * enclosure_remove_device - remove a device from an enclosure
  230. * @edev: the enclosure device
  231. * @num: the number of the component to remove
  232. *
  233. * Returns zero on success or an error.
  234. *
  235. */
  236. int enclosure_remove_device(struct enclosure_device *edev, int component)
  237. {
  238. struct enclosure_component *cdev;
  239. if (!edev || component >= edev->components)
  240. return -EINVAL;
  241. cdev = &edev->component[component];
  242. device_del(&cdev->cdev);
  243. put_device(cdev->dev);
  244. cdev->dev = NULL;
  245. return device_add(&cdev->cdev);
  246. }
  247. EXPORT_SYMBOL_GPL(enclosure_remove_device);
  248. /*
  249. * sysfs pieces below
  250. */
  251. static ssize_t enclosure_show_components(struct device *cdev,
  252. struct device_attribute *attr,
  253. 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 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. .dev_release = enclosure_release,
  266. .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 device *cdev,
  283. struct device_attribute *attr, char *buf)
  284. {
  285. struct enclosure_device *edev = to_enclosure_device(cdev->parent);
  286. struct enclosure_component *ecomp = to_enclosure_component(cdev);
  287. if (edev->cb->get_fault)
  288. edev->cb->get_fault(edev, ecomp);
  289. return snprintf(buf, 40, "%d\n", ecomp->fault);
  290. }
  291. static ssize_t set_component_fault(struct device *cdev,
  292. struct device_attribute *attr,
  293. const char *buf, size_t count)
  294. {
  295. struct enclosure_device *edev = to_enclosure_device(cdev->parent);
  296. struct enclosure_component *ecomp = to_enclosure_component(cdev);
  297. int val = simple_strtoul(buf, NULL, 0);
  298. if (edev->cb->set_fault)
  299. edev->cb->set_fault(edev, ecomp, val);
  300. return count;
  301. }
  302. static ssize_t get_component_status(struct device *cdev,
  303. struct device_attribute *attr,char *buf)
  304. {
  305. struct enclosure_device *edev = to_enclosure_device(cdev->parent);
  306. struct enclosure_component *ecomp = to_enclosure_component(cdev);
  307. if (edev->cb->get_status)
  308. edev->cb->get_status(edev, ecomp);
  309. return snprintf(buf, 40, "%s\n", enclosure_status[ecomp->status]);
  310. }
  311. static ssize_t set_component_status(struct device *cdev,
  312. struct device_attribute *attr,
  313. const char *buf, size_t count)
  314. {
  315. struct enclosure_device *edev = to_enclosure_device(cdev->parent);
  316. struct enclosure_component *ecomp = to_enclosure_component(cdev);
  317. int i;
  318. for (i = 0; enclosure_status[i]; i++) {
  319. if (strncmp(buf, enclosure_status[i],
  320. strlen(enclosure_status[i])) == 0 &&
  321. (buf[strlen(enclosure_status[i])] == '\n' ||
  322. buf[strlen(enclosure_status[i])] == '\0'))
  323. break;
  324. }
  325. if (enclosure_status[i] && edev->cb->set_status) {
  326. edev->cb->set_status(edev, ecomp, i);
  327. return count;
  328. } else
  329. return -EINVAL;
  330. }
  331. static ssize_t get_component_active(struct device *cdev,
  332. struct device_attribute *attr, char *buf)
  333. {
  334. struct enclosure_device *edev = to_enclosure_device(cdev->parent);
  335. struct enclosure_component *ecomp = to_enclosure_component(cdev);
  336. if (edev->cb->get_active)
  337. edev->cb->get_active(edev, ecomp);
  338. return snprintf(buf, 40, "%d\n", ecomp->active);
  339. }
  340. static ssize_t set_component_active(struct device *cdev,
  341. struct device_attribute *attr,
  342. const char *buf, size_t count)
  343. {
  344. struct enclosure_device *edev = to_enclosure_device(cdev->parent);
  345. struct enclosure_component *ecomp = to_enclosure_component(cdev);
  346. int val = simple_strtoul(buf, NULL, 0);
  347. if (edev->cb->set_active)
  348. edev->cb->set_active(edev, ecomp, val);
  349. return count;
  350. }
  351. static ssize_t get_component_locate(struct device *cdev,
  352. struct device_attribute *attr, char *buf)
  353. {
  354. struct enclosure_device *edev = to_enclosure_device(cdev->parent);
  355. struct enclosure_component *ecomp = to_enclosure_component(cdev);
  356. if (edev->cb->get_locate)
  357. edev->cb->get_locate(edev, ecomp);
  358. return snprintf(buf, 40, "%d\n", ecomp->locate);
  359. }
  360. static ssize_t set_component_locate(struct device *cdev,
  361. struct device_attribute *attr,
  362. const char *buf, size_t count)
  363. {
  364. struct enclosure_device *edev = to_enclosure_device(cdev->parent);
  365. struct enclosure_component *ecomp = to_enclosure_component(cdev);
  366. int val = simple_strtoul(buf, NULL, 0);
  367. if (edev->cb->set_locate)
  368. edev->cb->set_locate(edev, ecomp, val);
  369. return count;
  370. }
  371. static ssize_t get_component_type(struct device *cdev,
  372. struct device_attribute *attr, char *buf)
  373. {
  374. struct enclosure_component *ecomp = to_enclosure_component(cdev);
  375. return snprintf(buf, 40, "%s\n", enclosure_type[ecomp->type]);
  376. }
  377. static struct device_attribute enclosure_component_attrs[] = {
  378. __ATTR(fault, S_IRUGO | S_IWUSR, get_component_fault,
  379. set_component_fault),
  380. __ATTR(status, S_IRUGO | S_IWUSR, get_component_status,
  381. set_component_status),
  382. __ATTR(active, S_IRUGO | S_IWUSR, get_component_active,
  383. set_component_active),
  384. __ATTR(locate, S_IRUGO | S_IWUSR, get_component_locate,
  385. set_component_locate),
  386. __ATTR(type, S_IRUGO, get_component_type, NULL),
  387. __ATTR_NULL
  388. };
  389. static struct class enclosure_component_class = {
  390. .name = "enclosure_component",
  391. .owner = THIS_MODULE,
  392. .dev_attrs = enclosure_component_attrs,
  393. .dev_release = enclosure_component_release,
  394. };
  395. static int __init enclosure_init(void)
  396. {
  397. int err;
  398. err = class_register(&enclosure_class);
  399. if (err)
  400. return err;
  401. err = class_register(&enclosure_component_class);
  402. if (err)
  403. goto err_out;
  404. return 0;
  405. err_out:
  406. class_unregister(&enclosure_class);
  407. return err;
  408. }
  409. static void __exit enclosure_exit(void)
  410. {
  411. class_unregister(&enclosure_component_class);
  412. class_unregister(&enclosure_class);
  413. }
  414. module_init(enclosure_init);
  415. module_exit(enclosure_exit);
  416. MODULE_AUTHOR("James Bottomley");
  417. MODULE_DESCRIPTION("Enclosure Services");
  418. MODULE_LICENSE("GPL v2");