enclosure.c 15 KB

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