blob: 789d12128c2496564e00224fdc6285108fd04419 [file] [log] [blame]
James Bottomleyd569d5b2008-02-03 15:40:56 -06001/*
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
31static LIST_HEAD(container_list);
32static DEFINE_MUTEX(container_list_lock);
33static struct class enclosure_class;
James Bottomleyd569d5b2008-02-03 15:40:56 -060034
35/**
James Bottomley163f52b2009-08-01 00:39:36 +000036 * enclosure_find - find an enclosure given a parent device
37 * @dev: the parent to match against
38 * @start: Optional enclosure device to start from (NULL if none)
James Bottomleyd569d5b2008-02-03 15:40:56 -060039 *
James Bottomley163f52b2009-08-01 00:39:36 +000040 * Looks through the list of registered enclosures to find all those
41 * with @dev as a parent. Returns NULL if no enclosure is
42 * found. @start can be used as a starting point to obtain multiple
43 * enclosures per parent (should begin with NULL and then be set to
44 * each returned enclosure device). Obtains a reference to the
45 * enclosure class device which must be released with device_put().
46 * If @start is not NULL, a reference must be taken on it which is
47 * released before returning (this allows a loop through all
48 * enclosures to exit with only the reference on the enclosure of
49 * interest held). Note that the @dev may correspond to the actual
50 * device housing the enclosure, in which case no iteration via @start
51 * is required.
James Bottomleyd569d5b2008-02-03 15:40:56 -060052 */
James Bottomley163f52b2009-08-01 00:39:36 +000053struct enclosure_device *enclosure_find(struct device *dev,
54 struct enclosure_device *start)
James Bottomleyd569d5b2008-02-03 15:40:56 -060055{
Tony Jonesee959b02008-02-22 00:13:36 +010056 struct enclosure_device *edev;
James Bottomleyd569d5b2008-02-03 15:40:56 -060057
58 mutex_lock(&container_list_lock);
James Bottomley163f52b2009-08-01 00:39:36 +000059 edev = list_prepare_entry(start, &container_list, node);
60 if (start)
61 put_device(&start->edev);
62
63 list_for_each_entry_continue(edev, &container_list, node) {
64 struct device *parent = edev->edev.parent;
65 /* parent might not be immediate, so iterate up to
66 * the root of the tree if necessary */
67 while (parent) {
68 if (parent == dev) {
69 get_device(&edev->edev);
70 mutex_unlock(&container_list_lock);
71 return edev;
72 }
73 parent = parent->parent;
James Bottomleyd569d5b2008-02-03 15:40:56 -060074 }
75 }
76 mutex_unlock(&container_list_lock);
77
78 return NULL;
79}
80EXPORT_SYMBOL_GPL(enclosure_find);
81
82/**
83 * enclosure_for_each_device - calls a function for each enclosure
84 * @fn: the function to call
85 * @data: the data to pass to each call
86 *
87 * Loops over all the enclosures calling the function.
88 *
89 * Note, this function uses a mutex which will be held across calls to
90 * @fn, so it must have non atomic context, and @fn may (although it
91 * should not) sleep or otherwise cause the mutex to be held for
92 * indefinite periods
93 */
94int enclosure_for_each_device(int (*fn)(struct enclosure_device *, void *),
95 void *data)
96{
97 int error = 0;
98 struct enclosure_device *edev;
99
100 mutex_lock(&container_list_lock);
101 list_for_each_entry(edev, &container_list, node) {
102 error = fn(edev, data);
103 if (error)
104 break;
105 }
106 mutex_unlock(&container_list_lock);
107
108 return error;
109}
110EXPORT_SYMBOL_GPL(enclosure_for_each_device);
111
112/**
113 * enclosure_register - register device as an enclosure
114 *
115 * @dev: device containing the enclosure
116 * @components: number of components in the enclosure
117 *
118 * This sets up the device for being an enclosure. Note that @dev does
119 * not have to be a dedicated enclosure device. It may be some other type
120 * of device that additionally responds to enclosure services
121 */
122struct enclosure_device *
123enclosure_register(struct device *dev, const char *name, int components,
124 struct enclosure_component_callbacks *cb)
125{
126 struct enclosure_device *edev =
127 kzalloc(sizeof(struct enclosure_device) +
128 sizeof(struct enclosure_component)*components,
129 GFP_KERNEL);
130 int err, i;
131
132 BUG_ON(!cb);
133
134 if (!edev)
135 return ERR_PTR(-ENOMEM);
136
137 edev->components = components;
138
Tony Jonesee959b02008-02-22 00:13:36 +0100139 edev->edev.class = &enclosure_class;
140 edev->edev.parent = get_device(dev);
James Bottomleyd569d5b2008-02-03 15:40:56 -0600141 edev->cb = cb;
Yinghai Lu5e437542009-04-30 19:13:41 -0700142 dev_set_name(&edev->edev, "%s", name);
Tony Jonesee959b02008-02-22 00:13:36 +0100143 err = device_register(&edev->edev);
James Bottomleyd569d5b2008-02-03 15:40:56 -0600144 if (err)
145 goto err;
146
147 for (i = 0; i < components; i++)
148 edev->component[i].number = -1;
149
150 mutex_lock(&container_list_lock);
151 list_add_tail(&edev->node, &container_list);
152 mutex_unlock(&container_list_lock);
153
154 return edev;
155
156 err:
Tony Jonesee959b02008-02-22 00:13:36 +0100157 put_device(edev->edev.parent);
James Bottomleyd569d5b2008-02-03 15:40:56 -0600158 kfree(edev);
159 return ERR_PTR(err);
160}
161EXPORT_SYMBOL_GPL(enclosure_register);
162
163static struct enclosure_component_callbacks enclosure_null_callbacks;
164
165/**
166 * enclosure_unregister - remove an enclosure
167 *
168 * @edev: the registered enclosure to remove;
169 */
170void enclosure_unregister(struct enclosure_device *edev)
171{
172 int i;
173
174 mutex_lock(&container_list_lock);
175 list_del(&edev->node);
176 mutex_unlock(&container_list_lock);
177
178 for (i = 0; i < edev->components; i++)
179 if (edev->component[i].number != -1)
Tony Jonesee959b02008-02-22 00:13:36 +0100180 device_unregister(&edev->component[i].cdev);
James Bottomleyd569d5b2008-02-03 15:40:56 -0600181
182 /* prevent any callbacks into service user */
183 edev->cb = &enclosure_null_callbacks;
Tony Jonesee959b02008-02-22 00:13:36 +0100184 device_unregister(&edev->edev);
James Bottomleyd569d5b2008-02-03 15:40:56 -0600185}
186EXPORT_SYMBOL_GPL(enclosure_unregister);
187
James Bottomleycb6b7f42008-03-15 13:01:40 -0500188#define ENCLOSURE_NAME_SIZE 64
189
190static void enclosure_link_name(struct enclosure_component *cdev, char *name)
191{
192 strcpy(name, "enclosure_device:");
Kay Sievers71610f52008-12-03 22:41:36 +0100193 strcat(name, dev_name(&cdev->cdev));
James Bottomleycb6b7f42008-03-15 13:01:40 -0500194}
195
196static void enclosure_remove_links(struct enclosure_component *cdev)
197{
198 char name[ENCLOSURE_NAME_SIZE];
199
200 enclosure_link_name(cdev, name);
201 sysfs_remove_link(&cdev->dev->kobj, name);
202 sysfs_remove_link(&cdev->cdev.kobj, "device");
203}
204
205static int enclosure_add_links(struct enclosure_component *cdev)
206{
207 int error;
208 char name[ENCLOSURE_NAME_SIZE];
209
210 error = sysfs_create_link(&cdev->cdev.kobj, &cdev->dev->kobj, "device");
211 if (error)
212 return error;
213
214 enclosure_link_name(cdev, name);
215 error = sysfs_create_link(&cdev->dev->kobj, &cdev->cdev.kobj, name);
216 if (error)
217 sysfs_remove_link(&cdev->cdev.kobj, "device");
218
219 return error;
220}
221
Tony Jonesee959b02008-02-22 00:13:36 +0100222static void enclosure_release(struct device *cdev)
James Bottomleyd569d5b2008-02-03 15:40:56 -0600223{
224 struct enclosure_device *edev = to_enclosure_device(cdev);
225
Tony Jonesee959b02008-02-22 00:13:36 +0100226 put_device(cdev->parent);
James Bottomleyd569d5b2008-02-03 15:40:56 -0600227 kfree(edev);
228}
229
Tony Jonesee959b02008-02-22 00:13:36 +0100230static void enclosure_component_release(struct device *dev)
James Bottomleyd569d5b2008-02-03 15:40:56 -0600231{
Tony Jonesee959b02008-02-22 00:13:36 +0100232 struct enclosure_component *cdev = to_enclosure_component(dev);
233
James Bottomleycb6b7f42008-03-15 13:01:40 -0500234 if (cdev->dev) {
235 enclosure_remove_links(cdev);
236 put_device(cdev->dev);
237 }
Tony Jonesee959b02008-02-22 00:13:36 +0100238 put_device(dev->parent);
James Bottomleyd569d5b2008-02-03 15:40:56 -0600239}
240
James Bottomleycb6b7f42008-03-15 13:01:40 -0500241static struct attribute_group *enclosure_groups[];
242
James Bottomleyd569d5b2008-02-03 15:40:56 -0600243/**
244 * enclosure_component_register - add a particular component to an enclosure
245 * @edev: the enclosure to add the component
246 * @num: the device number
247 * @type: the type of component being added
248 * @name: an optional name to appear in sysfs (leave NULL if none)
249 *
250 * Registers the component. The name is optional for enclosures that
251 * give their components a unique name. If not, leave the field NULL
252 * and a name will be assigned.
253 *
254 * Returns a pointer to the enclosure component or an error.
255 */
256struct enclosure_component *
257enclosure_component_register(struct enclosure_device *edev,
258 unsigned int number,
259 enum enclosure_component_type type,
260 const char *name)
261{
262 struct enclosure_component *ecomp;
Tony Jonesee959b02008-02-22 00:13:36 +0100263 struct device *cdev;
James Bottomleyd569d5b2008-02-03 15:40:56 -0600264 int err;
265
266 if (number >= edev->components)
267 return ERR_PTR(-EINVAL);
268
269 ecomp = &edev->component[number];
270
271 if (ecomp->number != -1)
272 return ERR_PTR(-EINVAL);
273
274 ecomp->type = type;
275 ecomp->number = number;
276 cdev = &ecomp->cdev;
Tony Jonesee959b02008-02-22 00:13:36 +0100277 cdev->parent = get_device(&edev->edev);
Yinghai Lu5e437542009-04-30 19:13:41 -0700278 if (name && name[0])
279 dev_set_name(cdev, "%s", name);
James Bottomleyd569d5b2008-02-03 15:40:56 -0600280 else
Kay Sievers71610f52008-12-03 22:41:36 +0100281 dev_set_name(cdev, "%u", number);
James Bottomleyd569d5b2008-02-03 15:40:56 -0600282
James Bottomleycb6b7f42008-03-15 13:01:40 -0500283 cdev->release = enclosure_component_release;
284 cdev->groups = enclosure_groups;
285
Tony Jonesee959b02008-02-22 00:13:36 +0100286 err = device_register(cdev);
James Bottomleyd569d5b2008-02-03 15:40:56 -0600287 if (err)
288 ERR_PTR(err);
289
290 return ecomp;
291}
292EXPORT_SYMBOL_GPL(enclosure_component_register);
293
294/**
295 * enclosure_add_device - add a device as being part of an enclosure
296 * @edev: the enclosure device being added to.
297 * @num: the number of the component
298 * @dev: the device being added
299 *
300 * Declares a real device to reside in slot (or identifier) @num of an
301 * enclosure. This will cause the relevant sysfs links to appear.
302 * This function may also be used to change a device associated with
303 * an enclosure without having to call enclosure_remove_device() in
304 * between.
305 *
306 * Returns zero on success or an error.
307 */
308int enclosure_add_device(struct enclosure_device *edev, int component,
309 struct device *dev)
310{
Tony Jonesee959b02008-02-22 00:13:36 +0100311 struct enclosure_component *cdev;
James Bottomleyd569d5b2008-02-03 15:40:56 -0600312
313 if (!edev || component >= edev->components)
314 return -EINVAL;
315
Tony Jonesee959b02008-02-22 00:13:36 +0100316 cdev = &edev->component[component];
James Bottomleyd569d5b2008-02-03 15:40:56 -0600317
James Bottomleycb6b7f42008-03-15 13:01:40 -0500318 if (cdev->dev)
319 enclosure_remove_links(cdev);
320
Tony Jonesee959b02008-02-22 00:13:36 +0100321 put_device(cdev->dev);
James Bottomleyd569d5b2008-02-03 15:40:56 -0600322 cdev->dev = get_device(dev);
James Bottomleycb6b7f42008-03-15 13:01:40 -0500323 return enclosure_add_links(cdev);
James Bottomleyd569d5b2008-02-03 15:40:56 -0600324}
325EXPORT_SYMBOL_GPL(enclosure_add_device);
326
327/**
328 * enclosure_remove_device - remove a device from an enclosure
329 * @edev: the enclosure device
330 * @num: the number of the component to remove
331 *
332 * Returns zero on success or an error.
333 *
334 */
335int enclosure_remove_device(struct enclosure_device *edev, int component)
336{
Tony Jonesee959b02008-02-22 00:13:36 +0100337 struct enclosure_component *cdev;
James Bottomleyd569d5b2008-02-03 15:40:56 -0600338
339 if (!edev || component >= edev->components)
340 return -EINVAL;
341
Tony Jonesee959b02008-02-22 00:13:36 +0100342 cdev = &edev->component[component];
James Bottomleyd569d5b2008-02-03 15:40:56 -0600343
Tony Jonesee959b02008-02-22 00:13:36 +0100344 device_del(&cdev->cdev);
345 put_device(cdev->dev);
James Bottomleyd569d5b2008-02-03 15:40:56 -0600346 cdev->dev = NULL;
Tony Jonesee959b02008-02-22 00:13:36 +0100347 return device_add(&cdev->cdev);
James Bottomleyd569d5b2008-02-03 15:40:56 -0600348}
349EXPORT_SYMBOL_GPL(enclosure_remove_device);
350
351/*
352 * sysfs pieces below
353 */
354
Tony Jonesee959b02008-02-22 00:13:36 +0100355static ssize_t enclosure_show_components(struct device *cdev,
356 struct device_attribute *attr,
357 char *buf)
James Bottomleyd569d5b2008-02-03 15:40:56 -0600358{
359 struct enclosure_device *edev = to_enclosure_device(cdev);
360
361 return snprintf(buf, 40, "%d\n", edev->components);
362}
363
Tony Jonesee959b02008-02-22 00:13:36 +0100364static struct device_attribute enclosure_attrs[] = {
James Bottomleyd569d5b2008-02-03 15:40:56 -0600365 __ATTR(components, S_IRUGO, enclosure_show_components, NULL),
366 __ATTR_NULL
367};
368
369static struct class enclosure_class = {
370 .name = "enclosure",
371 .owner = THIS_MODULE,
Tony Jonesee959b02008-02-22 00:13:36 +0100372 .dev_release = enclosure_release,
373 .dev_attrs = enclosure_attrs,
James Bottomleyd569d5b2008-02-03 15:40:56 -0600374};
375
376static const char *const enclosure_status [] = {
377 [ENCLOSURE_STATUS_UNSUPPORTED] = "unsupported",
378 [ENCLOSURE_STATUS_OK] = "OK",
379 [ENCLOSURE_STATUS_CRITICAL] = "critical",
380 [ENCLOSURE_STATUS_NON_CRITICAL] = "non-critical",
381 [ENCLOSURE_STATUS_UNRECOVERABLE] = "unrecoverable",
382 [ENCLOSURE_STATUS_NOT_INSTALLED] = "not installed",
383 [ENCLOSURE_STATUS_UNKNOWN] = "unknown",
384 [ENCLOSURE_STATUS_UNAVAILABLE] = "unavailable",
385};
386
387static const char *const enclosure_type [] = {
388 [ENCLOSURE_COMPONENT_DEVICE] = "device",
389 [ENCLOSURE_COMPONENT_ARRAY_DEVICE] = "array device",
390};
391
Tony Jonesee959b02008-02-22 00:13:36 +0100392static ssize_t get_component_fault(struct device *cdev,
393 struct device_attribute *attr, char *buf)
James Bottomleyd569d5b2008-02-03 15:40:56 -0600394{
395 struct enclosure_device *edev = to_enclosure_device(cdev->parent);
396 struct enclosure_component *ecomp = to_enclosure_component(cdev);
397
398 if (edev->cb->get_fault)
399 edev->cb->get_fault(edev, ecomp);
400 return snprintf(buf, 40, "%d\n", ecomp->fault);
401}
402
Tony Jonesee959b02008-02-22 00:13:36 +0100403static ssize_t set_component_fault(struct device *cdev,
404 struct device_attribute *attr,
405 const char *buf, size_t count)
James Bottomleyd569d5b2008-02-03 15:40:56 -0600406{
407 struct enclosure_device *edev = to_enclosure_device(cdev->parent);
408 struct enclosure_component *ecomp = to_enclosure_component(cdev);
409 int val = simple_strtoul(buf, NULL, 0);
410
411 if (edev->cb->set_fault)
412 edev->cb->set_fault(edev, ecomp, val);
413 return count;
414}
415
Tony Jonesee959b02008-02-22 00:13:36 +0100416static ssize_t get_component_status(struct device *cdev,
417 struct device_attribute *attr,char *buf)
James Bottomleyd569d5b2008-02-03 15:40:56 -0600418{
419 struct enclosure_device *edev = to_enclosure_device(cdev->parent);
420 struct enclosure_component *ecomp = to_enclosure_component(cdev);
421
422 if (edev->cb->get_status)
423 edev->cb->get_status(edev, ecomp);
424 return snprintf(buf, 40, "%s\n", enclosure_status[ecomp->status]);
425}
426
Tony Jonesee959b02008-02-22 00:13:36 +0100427static ssize_t set_component_status(struct device *cdev,
428 struct device_attribute *attr,
429 const char *buf, size_t count)
James Bottomleyd569d5b2008-02-03 15:40:56 -0600430{
431 struct enclosure_device *edev = to_enclosure_device(cdev->parent);
432 struct enclosure_component *ecomp = to_enclosure_component(cdev);
433 int i;
434
435 for (i = 0; enclosure_status[i]; i++) {
436 if (strncmp(buf, enclosure_status[i],
437 strlen(enclosure_status[i])) == 0 &&
438 (buf[strlen(enclosure_status[i])] == '\n' ||
439 buf[strlen(enclosure_status[i])] == '\0'))
440 break;
441 }
442
443 if (enclosure_status[i] && edev->cb->set_status) {
444 edev->cb->set_status(edev, ecomp, i);
445 return count;
446 } else
447 return -EINVAL;
448}
449
Tony Jonesee959b02008-02-22 00:13:36 +0100450static ssize_t get_component_active(struct device *cdev,
451 struct device_attribute *attr, char *buf)
James Bottomleyd569d5b2008-02-03 15:40:56 -0600452{
453 struct enclosure_device *edev = to_enclosure_device(cdev->parent);
454 struct enclosure_component *ecomp = to_enclosure_component(cdev);
455
456 if (edev->cb->get_active)
457 edev->cb->get_active(edev, ecomp);
458 return snprintf(buf, 40, "%d\n", ecomp->active);
459}
460
Tony Jonesee959b02008-02-22 00:13:36 +0100461static ssize_t set_component_active(struct device *cdev,
462 struct device_attribute *attr,
463 const char *buf, size_t count)
James Bottomleyd569d5b2008-02-03 15:40:56 -0600464{
465 struct enclosure_device *edev = to_enclosure_device(cdev->parent);
466 struct enclosure_component *ecomp = to_enclosure_component(cdev);
467 int val = simple_strtoul(buf, NULL, 0);
468
469 if (edev->cb->set_active)
470 edev->cb->set_active(edev, ecomp, val);
471 return count;
472}
473
Tony Jonesee959b02008-02-22 00:13:36 +0100474static ssize_t get_component_locate(struct device *cdev,
475 struct device_attribute *attr, char *buf)
James Bottomleyd569d5b2008-02-03 15:40:56 -0600476{
477 struct enclosure_device *edev = to_enclosure_device(cdev->parent);
478 struct enclosure_component *ecomp = to_enclosure_component(cdev);
479
480 if (edev->cb->get_locate)
481 edev->cb->get_locate(edev, ecomp);
482 return snprintf(buf, 40, "%d\n", ecomp->locate);
483}
484
Tony Jonesee959b02008-02-22 00:13:36 +0100485static ssize_t set_component_locate(struct device *cdev,
486 struct device_attribute *attr,
487 const char *buf, size_t count)
James Bottomleyd569d5b2008-02-03 15:40:56 -0600488{
489 struct enclosure_device *edev = to_enclosure_device(cdev->parent);
490 struct enclosure_component *ecomp = to_enclosure_component(cdev);
491 int val = simple_strtoul(buf, NULL, 0);
492
493 if (edev->cb->set_locate)
494 edev->cb->set_locate(edev, ecomp, val);
495 return count;
496}
497
Tony Jonesee959b02008-02-22 00:13:36 +0100498static ssize_t get_component_type(struct device *cdev,
499 struct device_attribute *attr, char *buf)
James Bottomleyd569d5b2008-02-03 15:40:56 -0600500{
501 struct enclosure_component *ecomp = to_enclosure_component(cdev);
502
503 return snprintf(buf, 40, "%s\n", enclosure_type[ecomp->type]);
504}
505
506
James Bottomleycb6b7f42008-03-15 13:01:40 -0500507static DEVICE_ATTR(fault, S_IRUGO | S_IWUSR, get_component_fault,
508 set_component_fault);
509static DEVICE_ATTR(status, S_IRUGO | S_IWUSR, get_component_status,
510 set_component_status);
511static DEVICE_ATTR(active, S_IRUGO | S_IWUSR, get_component_active,
512 set_component_active);
513static DEVICE_ATTR(locate, S_IRUGO | S_IWUSR, get_component_locate,
514 set_component_locate);
515static DEVICE_ATTR(type, S_IRUGO, get_component_type, NULL);
516
517static struct attribute *enclosure_component_attrs[] = {
518 &dev_attr_fault.attr,
519 &dev_attr_status.attr,
520 &dev_attr_active.attr,
521 &dev_attr_locate.attr,
522 &dev_attr_type.attr,
523 NULL
James Bottomleyd569d5b2008-02-03 15:40:56 -0600524};
525
James Bottomleycb6b7f42008-03-15 13:01:40 -0500526static struct attribute_group enclosure_group = {
527 .attrs = enclosure_component_attrs,
528};
529
530static struct attribute_group *enclosure_groups[] = {
531 &enclosure_group,
532 NULL
James Bottomleyd569d5b2008-02-03 15:40:56 -0600533};
534
535static int __init enclosure_init(void)
536{
537 int err;
538
539 err = class_register(&enclosure_class);
540 if (err)
541 return err;
James Bottomleyd569d5b2008-02-03 15:40:56 -0600542
543 return 0;
James Bottomleyd569d5b2008-02-03 15:40:56 -0600544}
545
546static void __exit enclosure_exit(void)
547{
James Bottomleyd569d5b2008-02-03 15:40:56 -0600548 class_unregister(&enclosure_class);
549}
550
551module_init(enclosure_init);
552module_exit(enclosure_exit);
553
554MODULE_AUTHOR("James Bottomley");
555MODULE_DESCRIPTION("Enclosure Services");
556MODULE_LICENSE("GPL v2");