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