enclosure.c 14 KB

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