enclosure.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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. #include <linux/slab.h>
  31. static LIST_HEAD(container_list);
  32. static DEFINE_MUTEX(container_list_lock);
  33. static struct class enclosure_class;
  34. /**
  35. * enclosure_find - find an enclosure given a parent device
  36. * @dev: the parent to match against
  37. * @start: Optional enclosure device to start from (NULL if none)
  38. *
  39. * Looks through the list of registered enclosures to find all those
  40. * with @dev as a parent. Returns NULL if no enclosure is
  41. * found. @start can be used as a starting point to obtain multiple
  42. * enclosures per parent (should begin with NULL and then be set to
  43. * each returned enclosure device). Obtains a reference to the
  44. * enclosure class device which must be released with device_put().
  45. * If @start is not NULL, a reference must be taken on it which is
  46. * released before returning (this allows a loop through all
  47. * enclosures to exit with only the reference on the enclosure of
  48. * interest held). Note that the @dev may correspond to the actual
  49. * device housing the enclosure, in which case no iteration via @start
  50. * is required.
  51. */
  52. struct enclosure_device *enclosure_find(struct device *dev,
  53. struct enclosure_device *start)
  54. {
  55. struct enclosure_device *edev;
  56. mutex_lock(&container_list_lock);
  57. edev = list_prepare_entry(start, &container_list, node);
  58. if (start)
  59. put_device(&start->edev);
  60. list_for_each_entry_continue(edev, &container_list, node) {
  61. struct device *parent = edev->edev.parent;
  62. /* parent might not be immediate, so iterate up to
  63. * the root of the tree if necessary */
  64. while (parent) {
  65. if (parent == dev) {
  66. get_device(&edev->edev);
  67. mutex_unlock(&container_list_lock);
  68. return edev;
  69. }
  70. parent = parent->parent;
  71. }
  72. }
  73. mutex_unlock(&container_list_lock);
  74. return NULL;
  75. }
  76. EXPORT_SYMBOL_GPL(enclosure_find);
  77. /**
  78. * enclosure_for_each_device - calls a function for each enclosure
  79. * @fn: the function to call
  80. * @data: the data to pass to each call
  81. *
  82. * Loops over all the enclosures calling the function.
  83. *
  84. * Note, this function uses a mutex which will be held across calls to
  85. * @fn, so it must have non atomic context, and @fn may (although it
  86. * should not) sleep or otherwise cause the mutex to be held for
  87. * indefinite periods
  88. */
  89. int enclosure_for_each_device(int (*fn)(struct enclosure_device *, void *),
  90. void *data)
  91. {
  92. int error = 0;
  93. struct enclosure_device *edev;
  94. mutex_lock(&container_list_lock);
  95. list_for_each_entry(edev, &container_list, node) {
  96. error = fn(edev, data);
  97. if (error)
  98. break;
  99. }
  100. mutex_unlock(&container_list_lock);
  101. return error;
  102. }
  103. EXPORT_SYMBOL_GPL(enclosure_for_each_device);
  104. /**
  105. * enclosure_register - register device as an enclosure
  106. *
  107. * @dev: device containing the enclosure
  108. * @components: number of components in the enclosure
  109. *
  110. * This sets up the device for being an enclosure. Note that @dev does
  111. * not have to be a dedicated enclosure device. It may be some other type
  112. * of device that additionally responds to enclosure services
  113. */
  114. struct enclosure_device *
  115. enclosure_register(struct device *dev, const char *name, int components,
  116. struct enclosure_component_callbacks *cb)
  117. {
  118. struct enclosure_device *edev =
  119. kzalloc(sizeof(struct enclosure_device) +
  120. sizeof(struct enclosure_component)*components,
  121. GFP_KERNEL);
  122. int err, i;
  123. BUG_ON(!cb);
  124. if (!edev)
  125. return ERR_PTR(-ENOMEM);
  126. edev->components = components;
  127. edev->edev.class = &enclosure_class;
  128. edev->edev.parent = get_device(dev);
  129. edev->cb = cb;
  130. dev_set_name(&edev->edev, "%s", name);
  131. err = device_register(&edev->edev);
  132. if (err)
  133. goto err;
  134. for (i = 0; i < components; i++)
  135. edev->component[i].number = -1;
  136. mutex_lock(&container_list_lock);
  137. list_add_tail(&edev->node, &container_list);
  138. mutex_unlock(&container_list_lock);
  139. return edev;
  140. err:
  141. put_device(edev->edev.parent);
  142. kfree(edev);
  143. return ERR_PTR(err);
  144. }
  145. EXPORT_SYMBOL_GPL(enclosure_register);
  146. static struct enclosure_component_callbacks enclosure_null_callbacks;
  147. /**
  148. * enclosure_unregister - remove an enclosure
  149. *
  150. * @edev: the registered enclosure to remove;
  151. */
  152. void enclosure_unregister(struct enclosure_device *edev)
  153. {
  154. int i;
  155. mutex_lock(&container_list_lock);
  156. list_del(&edev->node);
  157. mutex_unlock(&container_list_lock);
  158. for (i = 0; i < edev->components; i++)
  159. if (edev->component[i].number != -1)
  160. device_unregister(&edev->component[i].cdev);
  161. /* prevent any callbacks into service user */
  162. edev->cb = &enclosure_null_callbacks;
  163. device_unregister(&edev->edev);
  164. }
  165. EXPORT_SYMBOL_GPL(enclosure_unregister);
  166. #define ENCLOSURE_NAME_SIZE 64
  167. static void enclosure_link_name(struct enclosure_component *cdev, char *name)
  168. {
  169. strcpy(name, "enclosure_device:");
  170. strcat(name, dev_name(&cdev->cdev));
  171. }
  172. static void enclosure_remove_links(struct enclosure_component *cdev)
  173. {
  174. char name[ENCLOSURE_NAME_SIZE];
  175. enclosure_link_name(cdev, name);
  176. sysfs_remove_link(&cdev->dev->kobj, name);
  177. sysfs_remove_link(&cdev->cdev.kobj, "device");
  178. }
  179. static int enclosure_add_links(struct enclosure_component *cdev)
  180. {
  181. int error;
  182. char name[ENCLOSURE_NAME_SIZE];
  183. error = sysfs_create_link(&cdev->cdev.kobj, &cdev->dev->kobj, "device");
  184. if (error)
  185. return error;
  186. enclosure_link_name(cdev, name);
  187. error = sysfs_create_link(&cdev->dev->kobj, &cdev->cdev.kobj, name);
  188. if (error)
  189. sysfs_remove_link(&cdev->cdev.kobj, "device");
  190. return error;
  191. }
  192. static void enclosure_release(struct device *cdev)
  193. {
  194. struct enclosure_device *edev = to_enclosure_device(cdev);
  195. put_device(cdev->parent);
  196. kfree(edev);
  197. }
  198. static void enclosure_component_release(struct device *dev)
  199. {
  200. struct enclosure_component *cdev = to_enclosure_component(dev);
  201. if (cdev->dev) {
  202. enclosure_remove_links(cdev);
  203. put_device(cdev->dev);
  204. }
  205. put_device(dev->parent);
  206. }
  207. static const struct attribute_group *enclosure_groups[];
  208. /**
  209. * enclosure_component_register - add a particular component to an enclosure
  210. * @edev: the enclosure to add the component
  211. * @num: the device number
  212. * @type: the type of component being added
  213. * @name: an optional name to appear in sysfs (leave NULL if none)
  214. *
  215. * Registers the component. The name is optional for enclosures that
  216. * give their components a unique name. If not, leave the field NULL
  217. * and a name will be assigned.
  218. *
  219. * Returns a pointer to the enclosure component or an error.
  220. */
  221. struct enclosure_component *
  222. enclosure_component_register(struct enclosure_device *edev,
  223. unsigned int number,
  224. enum enclosure_component_type type,
  225. const char *name)
  226. {
  227. struct enclosure_component *ecomp;
  228. struct device *cdev;
  229. int err;
  230. if (number >= edev->components)
  231. return ERR_PTR(-EINVAL);
  232. ecomp = &edev->component[number];
  233. if (ecomp->number != -1)
  234. return ERR_PTR(-EINVAL);
  235. ecomp->type = type;
  236. ecomp->number = number;
  237. cdev = &ecomp->cdev;
  238. cdev->parent = get_device(&edev->edev);
  239. if (name && name[0])
  240. dev_set_name(cdev, "%s", name);
  241. else
  242. dev_set_name(cdev, "%u", number);
  243. cdev->release = enclosure_component_release;
  244. cdev->groups = enclosure_groups;
  245. err = device_register(cdev);
  246. if (err) {
  247. ecomp->number = -1;
  248. put_device(cdev);
  249. return ERR_PTR(err);
  250. }
  251. return ecomp;
  252. }
  253. EXPORT_SYMBOL_GPL(enclosure_component_register);
  254. /**
  255. * enclosure_add_device - add a device as being part of an enclosure
  256. * @edev: the enclosure device being added to.
  257. * @num: the number of the component
  258. * @dev: the device being added
  259. *
  260. * Declares a real device to reside in slot (or identifier) @num of an
  261. * enclosure. This will cause the relevant sysfs links to appear.
  262. * This function may also be used to change a device associated with
  263. * an enclosure without having to call enclosure_remove_device() in
  264. * between.
  265. *
  266. * Returns zero on success or an error.
  267. */
  268. int enclosure_add_device(struct enclosure_device *edev, int component,
  269. struct device *dev)
  270. {
  271. struct enclosure_component *cdev;
  272. if (!edev || component >= edev->components)
  273. return -EINVAL;
  274. cdev = &edev->component[component];
  275. if (cdev->dev == dev)
  276. return -EEXIST;
  277. if (cdev->dev)
  278. enclosure_remove_links(cdev);
  279. put_device(cdev->dev);
  280. cdev->dev = get_device(dev);
  281. return enclosure_add_links(cdev);
  282. }
  283. EXPORT_SYMBOL_GPL(enclosure_add_device);
  284. /**
  285. * enclosure_remove_device - remove a device from an enclosure
  286. * @edev: the enclosure device
  287. * @num: the number of the component to remove
  288. *
  289. * Returns zero on success or an error.
  290. *
  291. */
  292. int enclosure_remove_device(struct enclosure_device *edev, struct device *dev)
  293. {
  294. struct enclosure_component *cdev;
  295. int i;
  296. if (!edev || !dev)
  297. return -EINVAL;
  298. for (i = 0; i < edev->components; i++) {
  299. cdev = &edev->component[i];
  300. if (cdev->dev == dev) {
  301. enclosure_remove_links(cdev);
  302. device_del(&cdev->cdev);
  303. put_device(dev);
  304. cdev->dev = NULL;
  305. return device_add(&cdev->cdev);
  306. }
  307. }
  308. return -ENODEV;
  309. }
  310. EXPORT_SYMBOL_GPL(enclosure_remove_device);
  311. /*
  312. * sysfs pieces below
  313. */
  314. static ssize_t enclosure_show_components(struct device *cdev,
  315. struct device_attribute *attr,
  316. char *buf)
  317. {
  318. struct enclosure_device *edev = to_enclosure_device(cdev);
  319. return snprintf(buf, 40, "%d\n", edev->components);
  320. }
  321. static struct device_attribute enclosure_attrs[] = {
  322. __ATTR(components, S_IRUGO, enclosure_show_components, NULL),
  323. __ATTR_NULL
  324. };
  325. static struct class enclosure_class = {
  326. .name = "enclosure",
  327. .owner = THIS_MODULE,
  328. .dev_release = enclosure_release,
  329. .dev_attrs = enclosure_attrs,
  330. };
  331. static const char *const enclosure_status [] = {
  332. [ENCLOSURE_STATUS_UNSUPPORTED] = "unsupported",
  333. [ENCLOSURE_STATUS_OK] = "OK",
  334. [ENCLOSURE_STATUS_CRITICAL] = "critical",
  335. [ENCLOSURE_STATUS_NON_CRITICAL] = "non-critical",
  336. [ENCLOSURE_STATUS_UNRECOVERABLE] = "unrecoverable",
  337. [ENCLOSURE_STATUS_NOT_INSTALLED] = "not installed",
  338. [ENCLOSURE_STATUS_UNKNOWN] = "unknown",
  339. [ENCLOSURE_STATUS_UNAVAILABLE] = "unavailable",
  340. [ENCLOSURE_STATUS_MAX] = NULL,
  341. };
  342. static const char *const enclosure_type [] = {
  343. [ENCLOSURE_COMPONENT_DEVICE] = "device",
  344. [ENCLOSURE_COMPONENT_ARRAY_DEVICE] = "array device",
  345. };
  346. static ssize_t get_component_fault(struct device *cdev,
  347. struct device_attribute *attr, char *buf)
  348. {
  349. struct enclosure_device *edev = to_enclosure_device(cdev->parent);
  350. struct enclosure_component *ecomp = to_enclosure_component(cdev);
  351. if (edev->cb->get_fault)
  352. edev->cb->get_fault(edev, ecomp);
  353. return snprintf(buf, 40, "%d\n", ecomp->fault);
  354. }
  355. static ssize_t set_component_fault(struct device *cdev,
  356. struct device_attribute *attr,
  357. const char *buf, size_t count)
  358. {
  359. struct enclosure_device *edev = to_enclosure_device(cdev->parent);
  360. struct enclosure_component *ecomp = to_enclosure_component(cdev);
  361. int val = simple_strtoul(buf, NULL, 0);
  362. if (edev->cb->set_fault)
  363. edev->cb->set_fault(edev, ecomp, val);
  364. return count;
  365. }
  366. static ssize_t get_component_status(struct device *cdev,
  367. struct device_attribute *attr,char *buf)
  368. {
  369. struct enclosure_device *edev = to_enclosure_device(cdev->parent);
  370. struct enclosure_component *ecomp = to_enclosure_component(cdev);
  371. if (edev->cb->get_status)
  372. edev->cb->get_status(edev, ecomp);
  373. return snprintf(buf, 40, "%s\n", enclosure_status[ecomp->status]);
  374. }
  375. static ssize_t set_component_status(struct device *cdev,
  376. struct device_attribute *attr,
  377. const char *buf, size_t count)
  378. {
  379. struct enclosure_device *edev = to_enclosure_device(cdev->parent);
  380. struct enclosure_component *ecomp = to_enclosure_component(cdev);
  381. int i;
  382. for (i = 0; enclosure_status[i]; i++) {
  383. if (strncmp(buf, enclosure_status[i],
  384. strlen(enclosure_status[i])) == 0 &&
  385. (buf[strlen(enclosure_status[i])] == '\n' ||
  386. buf[strlen(enclosure_status[i])] == '\0'))
  387. break;
  388. }
  389. if (enclosure_status[i] && edev->cb->set_status) {
  390. edev->cb->set_status(edev, ecomp, i);
  391. return count;
  392. } else
  393. return -EINVAL;
  394. }
  395. static ssize_t get_component_active(struct device *cdev,
  396. struct device_attribute *attr, char *buf)
  397. {
  398. struct enclosure_device *edev = to_enclosure_device(cdev->parent);
  399. struct enclosure_component *ecomp = to_enclosure_component(cdev);
  400. if (edev->cb->get_active)
  401. edev->cb->get_active(edev, ecomp);
  402. return snprintf(buf, 40, "%d\n", ecomp->active);
  403. }
  404. static ssize_t set_component_active(struct device *cdev,
  405. struct device_attribute *attr,
  406. const char *buf, size_t count)
  407. {
  408. struct enclosure_device *edev = to_enclosure_device(cdev->parent);
  409. struct enclosure_component *ecomp = to_enclosure_component(cdev);
  410. int val = simple_strtoul(buf, NULL, 0);
  411. if (edev->cb->set_active)
  412. edev->cb->set_active(edev, ecomp, val);
  413. return count;
  414. }
  415. static ssize_t get_component_locate(struct device *cdev,
  416. struct device_attribute *attr, char *buf)
  417. {
  418. struct enclosure_device *edev = to_enclosure_device(cdev->parent);
  419. struct enclosure_component *ecomp = to_enclosure_component(cdev);
  420. if (edev->cb->get_locate)
  421. edev->cb->get_locate(edev, ecomp);
  422. return snprintf(buf, 40, "%d\n", ecomp->locate);
  423. }
  424. static ssize_t set_component_locate(struct device *cdev,
  425. struct device_attribute *attr,
  426. const char *buf, size_t count)
  427. {
  428. struct enclosure_device *edev = to_enclosure_device(cdev->parent);
  429. struct enclosure_component *ecomp = to_enclosure_component(cdev);
  430. int val = simple_strtoul(buf, NULL, 0);
  431. if (edev->cb->set_locate)
  432. edev->cb->set_locate(edev, ecomp, val);
  433. return count;
  434. }
  435. static ssize_t get_component_type(struct device *cdev,
  436. struct device_attribute *attr, char *buf)
  437. {
  438. struct enclosure_component *ecomp = to_enclosure_component(cdev);
  439. return snprintf(buf, 40, "%s\n", enclosure_type[ecomp->type]);
  440. }
  441. static DEVICE_ATTR(fault, S_IRUGO | S_IWUSR, get_component_fault,
  442. set_component_fault);
  443. static DEVICE_ATTR(status, S_IRUGO | S_IWUSR, get_component_status,
  444. set_component_status);
  445. static DEVICE_ATTR(active, S_IRUGO | S_IWUSR, get_component_active,
  446. set_component_active);
  447. static DEVICE_ATTR(locate, S_IRUGO | S_IWUSR, get_component_locate,
  448. set_component_locate);
  449. static DEVICE_ATTR(type, S_IRUGO, get_component_type, NULL);
  450. static struct attribute *enclosure_component_attrs[] = {
  451. &dev_attr_fault.attr,
  452. &dev_attr_status.attr,
  453. &dev_attr_active.attr,
  454. &dev_attr_locate.attr,
  455. &dev_attr_type.attr,
  456. NULL
  457. };
  458. static struct attribute_group enclosure_group = {
  459. .attrs = enclosure_component_attrs,
  460. };
  461. static const struct attribute_group *enclosure_groups[] = {
  462. &enclosure_group,
  463. NULL
  464. };
  465. static int __init enclosure_init(void)
  466. {
  467. int err;
  468. err = class_register(&enclosure_class);
  469. if (err)
  470. return err;
  471. return 0;
  472. }
  473. static void __exit enclosure_exit(void)
  474. {
  475. class_unregister(&enclosure_class);
  476. }
  477. module_init(enclosure_init);
  478. module_exit(enclosure_exit);
  479. MODULE_AUTHOR("James Bottomley");
  480. MODULE_DESCRIPTION("Enclosure Services");
  481. MODULE_LICENSE("GPL v2");