xref: /openbmc/linux/drivers/misc/enclosure.c (revision e3575c1201f009a24885804975b859d868314d9c)
1d569d5bbSJames Bottomley /*
2d569d5bbSJames Bottomley  * Enclosure Services
3d569d5bbSJames Bottomley  *
4d569d5bbSJames Bottomley  * Copyright (C) 2008 James Bottomley <James.Bottomley@HansenPartnership.com>
5d569d5bbSJames Bottomley  *
6d569d5bbSJames Bottomley **-----------------------------------------------------------------------------
7d569d5bbSJames Bottomley **
8d569d5bbSJames Bottomley **  This program is free software; you can redistribute it and/or
9d569d5bbSJames Bottomley **  modify it under the terms of the GNU General Public License
10d569d5bbSJames Bottomley **  version 2 as published by the Free Software Foundation.
11d569d5bbSJames Bottomley **
12d569d5bbSJames Bottomley **  This program is distributed in the hope that it will be useful,
13d569d5bbSJames Bottomley **  but WITHOUT ANY WARRANTY; without even the implied warranty of
14d569d5bbSJames Bottomley **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15d569d5bbSJames Bottomley **  GNU General Public License for more details.
16d569d5bbSJames Bottomley **
17d569d5bbSJames Bottomley **  You should have received a copy of the GNU General Public License
18d569d5bbSJames Bottomley **  along with this program; if not, write to the Free Software
19d569d5bbSJames Bottomley **  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20d569d5bbSJames Bottomley **
21d569d5bbSJames Bottomley **-----------------------------------------------------------------------------
22d569d5bbSJames Bottomley */
23d569d5bbSJames Bottomley #include <linux/device.h>
24d569d5bbSJames Bottomley #include <linux/enclosure.h>
25d569d5bbSJames Bottomley #include <linux/err.h>
26d569d5bbSJames Bottomley #include <linux/list.h>
27d569d5bbSJames Bottomley #include <linux/kernel.h>
28d569d5bbSJames Bottomley #include <linux/module.h>
29d569d5bbSJames Bottomley #include <linux/mutex.h>
305a0e3ad6STejun Heo #include <linux/slab.h>
31d569d5bbSJames Bottomley 
32d569d5bbSJames Bottomley static LIST_HEAD(container_list);
33d569d5bbSJames Bottomley static DEFINE_MUTEX(container_list_lock);
34d569d5bbSJames Bottomley static struct class enclosure_class;
35d569d5bbSJames Bottomley 
36d569d5bbSJames Bottomley /**
37163f52b6SJames Bottomley  * enclosure_find - find an enclosure given a parent device
38163f52b6SJames Bottomley  * @dev:	the parent to match against
39163f52b6SJames Bottomley  * @start:	Optional enclosure device to start from (NULL if none)
40d569d5bbSJames Bottomley  *
41163f52b6SJames Bottomley  * Looks through the list of registered enclosures to find all those
42163f52b6SJames Bottomley  * with @dev as a parent.  Returns NULL if no enclosure is
43163f52b6SJames Bottomley  * found. @start can be used as a starting point to obtain multiple
44163f52b6SJames Bottomley  * enclosures per parent (should begin with NULL and then be set to
45163f52b6SJames Bottomley  * each returned enclosure device). Obtains a reference to the
46163f52b6SJames Bottomley  * enclosure class device which must be released with device_put().
47163f52b6SJames Bottomley  * If @start is not NULL, a reference must be taken on it which is
48163f52b6SJames Bottomley  * released before returning (this allows a loop through all
49163f52b6SJames Bottomley  * enclosures to exit with only the reference on the enclosure of
50163f52b6SJames Bottomley  * interest held).  Note that the @dev may correspond to the actual
51163f52b6SJames Bottomley  * device housing the enclosure, in which case no iteration via @start
52163f52b6SJames Bottomley  * is required.
53d569d5bbSJames Bottomley  */
54163f52b6SJames Bottomley struct enclosure_device *enclosure_find(struct device *dev,
55163f52b6SJames Bottomley 					struct enclosure_device *start)
56d569d5bbSJames Bottomley {
57ee959b00STony Jones 	struct enclosure_device *edev;
58d569d5bbSJames Bottomley 
59d569d5bbSJames Bottomley 	mutex_lock(&container_list_lock);
60163f52b6SJames Bottomley 	edev = list_prepare_entry(start, &container_list, node);
61163f52b6SJames Bottomley 	if (start)
62163f52b6SJames Bottomley 		put_device(&start->edev);
63163f52b6SJames Bottomley 
64163f52b6SJames Bottomley 	list_for_each_entry_continue(edev, &container_list, node) {
65163f52b6SJames Bottomley 		struct device *parent = edev->edev.parent;
66163f52b6SJames Bottomley 		/* parent might not be immediate, so iterate up to
67163f52b6SJames Bottomley 		 * the root of the tree if necessary */
68163f52b6SJames Bottomley 		while (parent) {
69163f52b6SJames Bottomley 			if (parent == dev) {
70ee959b00STony Jones 				get_device(&edev->edev);
71d569d5bbSJames Bottomley 				mutex_unlock(&container_list_lock);
72d569d5bbSJames Bottomley 				return edev;
73d569d5bbSJames Bottomley 			}
74163f52b6SJames Bottomley 			parent = parent->parent;
75163f52b6SJames Bottomley 		}
76d569d5bbSJames Bottomley 	}
77d569d5bbSJames Bottomley 	mutex_unlock(&container_list_lock);
78d569d5bbSJames Bottomley 
79d569d5bbSJames Bottomley 	return NULL;
80d569d5bbSJames Bottomley }
81d569d5bbSJames Bottomley EXPORT_SYMBOL_GPL(enclosure_find);
82d569d5bbSJames Bottomley 
83d569d5bbSJames Bottomley /**
84d569d5bbSJames Bottomley  * enclosure_for_each_device - calls a function for each enclosure
85d569d5bbSJames Bottomley  * @fn:		the function to call
86d569d5bbSJames Bottomley  * @data:	the data to pass to each call
87d569d5bbSJames Bottomley  *
88d569d5bbSJames Bottomley  * Loops over all the enclosures calling the function.
89d569d5bbSJames Bottomley  *
90d569d5bbSJames Bottomley  * Note, this function uses a mutex which will be held across calls to
91d569d5bbSJames Bottomley  * @fn, so it must have non atomic context, and @fn may (although it
92d569d5bbSJames Bottomley  * should not) sleep or otherwise cause the mutex to be held for
93d569d5bbSJames Bottomley  * indefinite periods
94d569d5bbSJames Bottomley  */
95d569d5bbSJames Bottomley int enclosure_for_each_device(int (*fn)(struct enclosure_device *, void *),
96d569d5bbSJames Bottomley 			      void *data)
97d569d5bbSJames Bottomley {
98d569d5bbSJames Bottomley 	int error = 0;
99d569d5bbSJames Bottomley 	struct enclosure_device *edev;
100d569d5bbSJames Bottomley 
101d569d5bbSJames Bottomley 	mutex_lock(&container_list_lock);
102d569d5bbSJames Bottomley 	list_for_each_entry(edev, &container_list, node) {
103d569d5bbSJames Bottomley 		error = fn(edev, data);
104d569d5bbSJames Bottomley 		if (error)
105d569d5bbSJames Bottomley 			break;
106d569d5bbSJames Bottomley 	}
107d569d5bbSJames Bottomley 	mutex_unlock(&container_list_lock);
108d569d5bbSJames Bottomley 
109d569d5bbSJames Bottomley 	return error;
110d569d5bbSJames Bottomley }
111d569d5bbSJames Bottomley EXPORT_SYMBOL_GPL(enclosure_for_each_device);
112d569d5bbSJames Bottomley 
113d569d5bbSJames Bottomley /**
114d569d5bbSJames Bottomley  * enclosure_register - register device as an enclosure
115d569d5bbSJames Bottomley  *
116d569d5bbSJames Bottomley  * @dev:	device containing the enclosure
117d569d5bbSJames Bottomley  * @components:	number of components in the enclosure
118d569d5bbSJames Bottomley  *
119d569d5bbSJames Bottomley  * This sets up the device for being an enclosure.  Note that @dev does
120d569d5bbSJames Bottomley  * not have to be a dedicated enclosure device.  It may be some other type
121d569d5bbSJames Bottomley  * of device that additionally responds to enclosure services
122d569d5bbSJames Bottomley  */
123d569d5bbSJames Bottomley struct enclosure_device *
124d569d5bbSJames Bottomley enclosure_register(struct device *dev, const char *name, int components,
125d569d5bbSJames Bottomley 		   struct enclosure_component_callbacks *cb)
126d569d5bbSJames Bottomley {
127d569d5bbSJames Bottomley 	struct enclosure_device *edev =
128*e3575c12SGustavo A. R. Silva 		kzalloc(struct_size(edev, component, components), GFP_KERNEL);
129d569d5bbSJames Bottomley 	int err, i;
130d569d5bbSJames Bottomley 
131d569d5bbSJames Bottomley 	BUG_ON(!cb);
132d569d5bbSJames Bottomley 
133d569d5bbSJames Bottomley 	if (!edev)
134d569d5bbSJames Bottomley 		return ERR_PTR(-ENOMEM);
135d569d5bbSJames Bottomley 
136d569d5bbSJames Bottomley 	edev->components = components;
137d569d5bbSJames Bottomley 
138ee959b00STony Jones 	edev->edev.class = &enclosure_class;
139ee959b00STony Jones 	edev->edev.parent = get_device(dev);
140d569d5bbSJames Bottomley 	edev->cb = cb;
1415e43754fSYinghai Lu 	dev_set_name(&edev->edev, "%s", name);
142ee959b00STony Jones 	err = device_register(&edev->edev);
143d569d5bbSJames Bottomley 	if (err)
144d569d5bbSJames Bottomley 		goto err;
145d569d5bbSJames Bottomley 
146921ce7f5SDan Williams 	for (i = 0; i < components; i++) {
147d569d5bbSJames Bottomley 		edev->component[i].number = -1;
148921ce7f5SDan Williams 		edev->component[i].slot = -1;
14975106523SMauricio Faria de Oliveira 		edev->component[i].power_status = -1;
150921ce7f5SDan Williams 	}
151d569d5bbSJames Bottomley 
152d569d5bbSJames Bottomley 	mutex_lock(&container_list_lock);
153d569d5bbSJames Bottomley 	list_add_tail(&edev->node, &container_list);
154d569d5bbSJames Bottomley 	mutex_unlock(&container_list_lock);
155d569d5bbSJames Bottomley 
156d569d5bbSJames Bottomley 	return edev;
157d569d5bbSJames Bottomley 
158d569d5bbSJames Bottomley  err:
159ee959b00STony Jones 	put_device(edev->edev.parent);
160d569d5bbSJames Bottomley 	kfree(edev);
161d569d5bbSJames Bottomley 	return ERR_PTR(err);
162d569d5bbSJames Bottomley }
163d569d5bbSJames Bottomley EXPORT_SYMBOL_GPL(enclosure_register);
164d569d5bbSJames Bottomley 
165d569d5bbSJames Bottomley static struct enclosure_component_callbacks enclosure_null_callbacks;
166d569d5bbSJames Bottomley 
167d569d5bbSJames Bottomley /**
168d569d5bbSJames Bottomley  * enclosure_unregister - remove an enclosure
169d569d5bbSJames Bottomley  *
170d569d5bbSJames Bottomley  * @edev:	the registered enclosure to remove;
171d569d5bbSJames Bottomley  */
172d569d5bbSJames Bottomley void enclosure_unregister(struct enclosure_device *edev)
173d569d5bbSJames Bottomley {
174d569d5bbSJames Bottomley 	int i;
175d569d5bbSJames Bottomley 
176d569d5bbSJames Bottomley 	mutex_lock(&container_list_lock);
177d569d5bbSJames Bottomley 	list_del(&edev->node);
178d569d5bbSJames Bottomley 	mutex_unlock(&container_list_lock);
179d569d5bbSJames Bottomley 
180d569d5bbSJames Bottomley 	for (i = 0; i < edev->components; i++)
181d569d5bbSJames Bottomley 		if (edev->component[i].number != -1)
182ee959b00STony Jones 			device_unregister(&edev->component[i].cdev);
183d569d5bbSJames Bottomley 
184d569d5bbSJames Bottomley 	/* prevent any callbacks into service user */
185d569d5bbSJames Bottomley 	edev->cb = &enclosure_null_callbacks;
186ee959b00STony Jones 	device_unregister(&edev->edev);
187d569d5bbSJames Bottomley }
188d569d5bbSJames Bottomley EXPORT_SYMBOL_GPL(enclosure_unregister);
189d569d5bbSJames Bottomley 
190cb6b7f40SJames Bottomley #define ENCLOSURE_NAME_SIZE	64
191d2fd76e6SMarkus Stockhausen #define COMPONENT_NAME_SIZE	64
192cb6b7f40SJames Bottomley 
193cb6b7f40SJames Bottomley static void enclosure_link_name(struct enclosure_component *cdev, char *name)
194cb6b7f40SJames Bottomley {
195cb6b7f40SJames Bottomley 	strcpy(name, "enclosure_device:");
19671610f55SKay Sievers 	strcat(name, dev_name(&cdev->cdev));
197cb6b7f40SJames Bottomley }
198cb6b7f40SJames Bottomley 
199cb6b7f40SJames Bottomley static void enclosure_remove_links(struct enclosure_component *cdev)
200cb6b7f40SJames Bottomley {
201cb6b7f40SJames Bottomley 	char name[ENCLOSURE_NAME_SIZE];
202cb6b7f40SJames Bottomley 
20311e52a69SJames Bottomley 	enclosure_link_name(cdev, name);
20411e52a69SJames Bottomley 
205a1470c7bSJames Bottomley 	/*
206a1470c7bSJames Bottomley 	 * In odd circumstances, like multipath devices, something else may
207a1470c7bSJames Bottomley 	 * already have removed the links, so check for this condition first.
208a1470c7bSJames Bottomley 	 */
20911e52a69SJames Bottomley 	if (cdev->dev->kobj.sd)
210cb6b7f40SJames Bottomley 		sysfs_remove_link(&cdev->dev->kobj, name);
21111e52a69SJames Bottomley 
21211e52a69SJames Bottomley 	if (cdev->cdev.kobj.sd)
213cb6b7f40SJames Bottomley 		sysfs_remove_link(&cdev->cdev.kobj, "device");
214cb6b7f40SJames Bottomley }
215cb6b7f40SJames Bottomley 
216cb6b7f40SJames Bottomley static int enclosure_add_links(struct enclosure_component *cdev)
217cb6b7f40SJames Bottomley {
218cb6b7f40SJames Bottomley 	int error;
219cb6b7f40SJames Bottomley 	char name[ENCLOSURE_NAME_SIZE];
220cb6b7f40SJames Bottomley 
221cb6b7f40SJames Bottomley 	error = sysfs_create_link(&cdev->cdev.kobj, &cdev->dev->kobj, "device");
222cb6b7f40SJames Bottomley 	if (error)
223cb6b7f40SJames Bottomley 		return error;
224cb6b7f40SJames Bottomley 
225cb6b7f40SJames Bottomley 	enclosure_link_name(cdev, name);
226cb6b7f40SJames Bottomley 	error = sysfs_create_link(&cdev->dev->kobj, &cdev->cdev.kobj, name);
227cb6b7f40SJames Bottomley 	if (error)
228cb6b7f40SJames Bottomley 		sysfs_remove_link(&cdev->cdev.kobj, "device");
229cb6b7f40SJames Bottomley 
230cb6b7f40SJames Bottomley 	return error;
231cb6b7f40SJames Bottomley }
232cb6b7f40SJames Bottomley 
233ee959b00STony Jones static void enclosure_release(struct device *cdev)
234d569d5bbSJames Bottomley {
235d569d5bbSJames Bottomley 	struct enclosure_device *edev = to_enclosure_device(cdev);
236d569d5bbSJames Bottomley 
237ee959b00STony Jones 	put_device(cdev->parent);
238d569d5bbSJames Bottomley 	kfree(edev);
239d569d5bbSJames Bottomley }
240d569d5bbSJames Bottomley 
241ee959b00STony Jones static void enclosure_component_release(struct device *dev)
242d569d5bbSJames Bottomley {
243ee959b00STony Jones 	struct enclosure_component *cdev = to_enclosure_component(dev);
244ee959b00STony Jones 
245cb6b7f40SJames Bottomley 	if (cdev->dev) {
246cb6b7f40SJames Bottomley 		enclosure_remove_links(cdev);
247d569d5bbSJames Bottomley 		put_device(cdev->dev);
248cb6b7f40SJames Bottomley 	}
249ee959b00STony Jones 	put_device(dev->parent);
250d569d5bbSJames Bottomley }
251d569d5bbSJames Bottomley 
252d2fd76e6SMarkus Stockhausen static struct enclosure_component *
253d2fd76e6SMarkus Stockhausen enclosure_component_find_by_name(struct enclosure_device *edev,
254d2fd76e6SMarkus Stockhausen 				const char *name)
255d2fd76e6SMarkus Stockhausen {
256d2fd76e6SMarkus Stockhausen 	int i;
257d2fd76e6SMarkus Stockhausen 	const char *cname;
258d2fd76e6SMarkus Stockhausen 	struct enclosure_component *ecomp;
259d2fd76e6SMarkus Stockhausen 
260d2fd76e6SMarkus Stockhausen 	if (!edev || !name || !name[0])
261d2fd76e6SMarkus Stockhausen 		return NULL;
262d2fd76e6SMarkus Stockhausen 
263d2fd76e6SMarkus Stockhausen 	for (i = 0; i < edev->components; i++) {
264d2fd76e6SMarkus Stockhausen 		ecomp = &edev->component[i];
265d2fd76e6SMarkus Stockhausen 		cname = dev_name(&ecomp->cdev);
266d2fd76e6SMarkus Stockhausen 		if (ecomp->number != -1 &&
267d2fd76e6SMarkus Stockhausen 		    cname && cname[0] &&
268d2fd76e6SMarkus Stockhausen 		    !strcmp(cname, name))
269d2fd76e6SMarkus Stockhausen 			return ecomp;
270d2fd76e6SMarkus Stockhausen 	}
271d2fd76e6SMarkus Stockhausen 
272d2fd76e6SMarkus Stockhausen 	return NULL;
273d2fd76e6SMarkus Stockhausen }
274d2fd76e6SMarkus Stockhausen 
275899826f1SGreg Kroah-Hartman static const struct attribute_group *enclosure_component_groups[];
276cb6b7f40SJames Bottomley 
277d569d5bbSJames Bottomley /**
278ed09dcc8SDan Williams  * enclosure_component_alloc - prepare a new enclosure component
279d569d5bbSJames Bottomley  * @edev:	the enclosure to add the component
280d569d5bbSJames Bottomley  * @num:	the device number
281d569d5bbSJames Bottomley  * @type:	the type of component being added
282d569d5bbSJames Bottomley  * @name:	an optional name to appear in sysfs (leave NULL if none)
283d569d5bbSJames Bottomley  *
284ed09dcc8SDan Williams  * The name is optional for enclosures that give their components a unique
285ed09dcc8SDan Williams  * name.  If not, leave the field NULL and a name will be assigned.
286d569d5bbSJames Bottomley  *
287d569d5bbSJames Bottomley  * Returns a pointer to the enclosure component or an error.
288d569d5bbSJames Bottomley  */
289d569d5bbSJames Bottomley struct enclosure_component *
290ed09dcc8SDan Williams enclosure_component_alloc(struct enclosure_device *edev,
291d569d5bbSJames Bottomley 			  unsigned int number,
292d569d5bbSJames Bottomley 			  enum enclosure_component_type type,
293d569d5bbSJames Bottomley 			  const char *name)
294d569d5bbSJames Bottomley {
295d569d5bbSJames Bottomley 	struct enclosure_component *ecomp;
296ee959b00STony Jones 	struct device *cdev;
297ed09dcc8SDan Williams 	int i;
298d2fd76e6SMarkus Stockhausen 	char newname[COMPONENT_NAME_SIZE];
299d569d5bbSJames Bottomley 
300d569d5bbSJames Bottomley 	if (number >= edev->components)
301d569d5bbSJames Bottomley 		return ERR_PTR(-EINVAL);
302d569d5bbSJames Bottomley 
303d569d5bbSJames Bottomley 	ecomp = &edev->component[number];
304d569d5bbSJames Bottomley 
305d569d5bbSJames Bottomley 	if (ecomp->number != -1)
306d569d5bbSJames Bottomley 		return ERR_PTR(-EINVAL);
307d569d5bbSJames Bottomley 
308d569d5bbSJames Bottomley 	ecomp->type = type;
309d569d5bbSJames Bottomley 	ecomp->number = number;
310d569d5bbSJames Bottomley 	cdev = &ecomp->cdev;
311ee959b00STony Jones 	cdev->parent = get_device(&edev->edev);
312d2fd76e6SMarkus Stockhausen 
313d2fd76e6SMarkus Stockhausen 	if (name && name[0]) {
314d2fd76e6SMarkus Stockhausen 		/* Some hardware (e.g. enclosure in RX300 S6) has components
315d2fd76e6SMarkus Stockhausen 		 * with non unique names. Registering duplicates in sysfs
316d2fd76e6SMarkus Stockhausen 		 * will lead to warnings during bootup. So make the names
317d2fd76e6SMarkus Stockhausen 		 * unique by appending consecutive numbers -1, -2, ... */
318d2fd76e6SMarkus Stockhausen 		i = 1;
319d2fd76e6SMarkus Stockhausen 		snprintf(newname, COMPONENT_NAME_SIZE,
320d2fd76e6SMarkus Stockhausen 			 "%s", name);
321d2fd76e6SMarkus Stockhausen 		while (enclosure_component_find_by_name(edev, newname))
322d2fd76e6SMarkus Stockhausen 			snprintf(newname, COMPONENT_NAME_SIZE,
323d2fd76e6SMarkus Stockhausen 				 "%s-%i", name, i++);
324d2fd76e6SMarkus Stockhausen 		dev_set_name(cdev, "%s", newname);
325d2fd76e6SMarkus Stockhausen 	} else
32671610f55SKay Sievers 		dev_set_name(cdev, "%u", number);
327d569d5bbSJames Bottomley 
328cb6b7f40SJames Bottomley 	cdev->release = enclosure_component_release;
329899826f1SGreg Kroah-Hartman 	cdev->groups = enclosure_component_groups;
330cb6b7f40SJames Bottomley 
331ed09dcc8SDan Williams 	return ecomp;
332ed09dcc8SDan Williams }
333ed09dcc8SDan Williams EXPORT_SYMBOL_GPL(enclosure_component_alloc);
334ed09dcc8SDan Williams 
335ed09dcc8SDan Williams /**
336ed09dcc8SDan Williams  * enclosure_component_register - publishes an initialized enclosure component
337ed09dcc8SDan Williams  * @ecomp:	component to add
338ed09dcc8SDan Williams  *
339ed09dcc8SDan Williams  * Returns 0 on successful registration, releases the component otherwise
340ed09dcc8SDan Williams  */
341ed09dcc8SDan Williams int enclosure_component_register(struct enclosure_component *ecomp)
342ed09dcc8SDan Williams {
343ed09dcc8SDan Williams 	struct device *cdev;
344ed09dcc8SDan Williams 	int err;
345ed09dcc8SDan Williams 
346ed09dcc8SDan Williams 	cdev = &ecomp->cdev;
347ee959b00STony Jones 	err = device_register(cdev);
348a91c1be2SJames Bottomley 	if (err) {
349a91c1be2SJames Bottomley 		ecomp->number = -1;
350a91c1be2SJames Bottomley 		put_device(cdev);
351ed09dcc8SDan Williams 		return err;
352a91c1be2SJames Bottomley 	}
353d569d5bbSJames Bottomley 
354ed09dcc8SDan Williams 	return 0;
355d569d5bbSJames Bottomley }
356d569d5bbSJames Bottomley EXPORT_SYMBOL_GPL(enclosure_component_register);
357d569d5bbSJames Bottomley 
358d569d5bbSJames Bottomley /**
359d569d5bbSJames Bottomley  * enclosure_add_device - add a device as being part of an enclosure
360d569d5bbSJames Bottomley  * @edev:	the enclosure device being added to.
361d569d5bbSJames Bottomley  * @num:	the number of the component
362d569d5bbSJames Bottomley  * @dev:	the device being added
363d569d5bbSJames Bottomley  *
364d569d5bbSJames Bottomley  * Declares a real device to reside in slot (or identifier) @num of an
365d569d5bbSJames Bottomley  * enclosure.  This will cause the relevant sysfs links to appear.
366d569d5bbSJames Bottomley  * This function may also be used to change a device associated with
367d569d5bbSJames Bottomley  * an enclosure without having to call enclosure_remove_device() in
368d569d5bbSJames Bottomley  * between.
369d569d5bbSJames Bottomley  *
370d569d5bbSJames Bottomley  * Returns zero on success or an error.
371d569d5bbSJames Bottomley  */
372d569d5bbSJames Bottomley int enclosure_add_device(struct enclosure_device *edev, int component,
373d569d5bbSJames Bottomley 			 struct device *dev)
374d569d5bbSJames Bottomley {
375ee959b00STony Jones 	struct enclosure_component *cdev;
37662e62ffdSMaurizio Lombardi 	int err;
377d569d5bbSJames Bottomley 
378d569d5bbSJames Bottomley 	if (!edev || component >= edev->components)
379d569d5bbSJames Bottomley 		return -EINVAL;
380d569d5bbSJames Bottomley 
381ee959b00STony Jones 	cdev = &edev->component[component];
382d569d5bbSJames Bottomley 
38321fab1d0SJames Bottomley 	if (cdev->dev == dev)
38421fab1d0SJames Bottomley 		return -EEXIST;
38521fab1d0SJames Bottomley 
38662e62ffdSMaurizio Lombardi 	if (cdev->dev) {
387cb6b7f40SJames Bottomley 		enclosure_remove_links(cdev);
388d569d5bbSJames Bottomley 		put_device(cdev->dev);
38962e62ffdSMaurizio Lombardi 	}
390d569d5bbSJames Bottomley 	cdev->dev = get_device(dev);
39162e62ffdSMaurizio Lombardi 	err = enclosure_add_links(cdev);
39262e62ffdSMaurizio Lombardi 	if (err) {
39362e62ffdSMaurizio Lombardi 		put_device(cdev->dev);
39462e62ffdSMaurizio Lombardi 		cdev->dev = NULL;
39562e62ffdSMaurizio Lombardi 	}
39662e62ffdSMaurizio Lombardi 	return err;
397d569d5bbSJames Bottomley }
398d569d5bbSJames Bottomley EXPORT_SYMBOL_GPL(enclosure_add_device);
399d569d5bbSJames Bottomley 
400d569d5bbSJames Bottomley /**
401d569d5bbSJames Bottomley  * enclosure_remove_device - remove a device from an enclosure
402d569d5bbSJames Bottomley  * @edev:	the enclosure device
403d569d5bbSJames Bottomley  * @num:	the number of the component to remove
404d569d5bbSJames Bottomley  *
405d569d5bbSJames Bottomley  * Returns zero on success or an error.
406d569d5bbSJames Bottomley  *
407d569d5bbSJames Bottomley  */
40843d8eb9cSJames Bottomley int enclosure_remove_device(struct enclosure_device *edev, struct device *dev)
409d569d5bbSJames Bottomley {
410ee959b00STony Jones 	struct enclosure_component *cdev;
41143d8eb9cSJames Bottomley 	int i;
412d569d5bbSJames Bottomley 
41343d8eb9cSJames Bottomley 	if (!edev || !dev)
414d569d5bbSJames Bottomley 		return -EINVAL;
415d569d5bbSJames Bottomley 
41643d8eb9cSJames Bottomley 	for (i = 0; i < edev->components; i++) {
41743d8eb9cSJames Bottomley 		cdev = &edev->component[i];
41843d8eb9cSJames Bottomley 		if (cdev->dev == dev) {
41943d8eb9cSJames Bottomley 			enclosure_remove_links(cdev);
420ee959b00STony Jones 			device_del(&cdev->cdev);
42143d8eb9cSJames Bottomley 			put_device(dev);
422d569d5bbSJames Bottomley 			cdev->dev = NULL;
423ee959b00STony Jones 			return device_add(&cdev->cdev);
424d569d5bbSJames Bottomley 		}
42543d8eb9cSJames Bottomley 	}
42643d8eb9cSJames Bottomley 	return -ENODEV;
42743d8eb9cSJames Bottomley }
428d569d5bbSJames Bottomley EXPORT_SYMBOL_GPL(enclosure_remove_device);
429d569d5bbSJames Bottomley 
430d569d5bbSJames Bottomley /*
431d569d5bbSJames Bottomley  * sysfs pieces below
432d569d5bbSJames Bottomley  */
433d569d5bbSJames Bottomley 
434899826f1SGreg Kroah-Hartman static ssize_t components_show(struct device *cdev,
435899826f1SGreg Kroah-Hartman 			       struct device_attribute *attr, char *buf)
436d569d5bbSJames Bottomley {
437d569d5bbSJames Bottomley 	struct enclosure_device *edev = to_enclosure_device(cdev);
438d569d5bbSJames Bottomley 
439d569d5bbSJames Bottomley 	return snprintf(buf, 40, "%d\n", edev->components);
440d569d5bbSJames Bottomley }
441899826f1SGreg Kroah-Hartman static DEVICE_ATTR_RO(components);
442d569d5bbSJames Bottomley 
443967f7babSDan Williams static ssize_t id_show(struct device *cdev,
444967f7babSDan Williams 				 struct device_attribute *attr,
445967f7babSDan Williams 				 char *buf)
446967f7babSDan Williams {
447967f7babSDan Williams 	struct enclosure_device *edev = to_enclosure_device(cdev);
448967f7babSDan Williams 
449967f7babSDan Williams 	if (edev->cb->show_id)
450967f7babSDan Williams 		return edev->cb->show_id(edev, buf);
451967f7babSDan Williams 	return -EINVAL;
452967f7babSDan Williams }
453967f7babSDan Williams static DEVICE_ATTR_RO(id);
454967f7babSDan Williams 
455899826f1SGreg Kroah-Hartman static struct attribute *enclosure_class_attrs[] = {
456899826f1SGreg Kroah-Hartman 	&dev_attr_components.attr,
457967f7babSDan Williams 	&dev_attr_id.attr,
458899826f1SGreg Kroah-Hartman 	NULL,
459d569d5bbSJames Bottomley };
460899826f1SGreg Kroah-Hartman ATTRIBUTE_GROUPS(enclosure_class);
461d569d5bbSJames Bottomley 
462d569d5bbSJames Bottomley static struct class enclosure_class = {
463d569d5bbSJames Bottomley 	.name			= "enclosure",
464d569d5bbSJames Bottomley 	.owner			= THIS_MODULE,
465ee959b00STony Jones 	.dev_release		= enclosure_release,
466899826f1SGreg Kroah-Hartman 	.dev_groups		= enclosure_class_groups,
467d569d5bbSJames Bottomley };
468d569d5bbSJames Bottomley 
469d569d5bbSJames Bottomley static const char *const enclosure_status[] = {
470d569d5bbSJames Bottomley 	[ENCLOSURE_STATUS_UNSUPPORTED] = "unsupported",
471d569d5bbSJames Bottomley 	[ENCLOSURE_STATUS_OK] = "OK",
472d569d5bbSJames Bottomley 	[ENCLOSURE_STATUS_CRITICAL] = "critical",
473d569d5bbSJames Bottomley 	[ENCLOSURE_STATUS_NON_CRITICAL] = "non-critical",
474d569d5bbSJames Bottomley 	[ENCLOSURE_STATUS_UNRECOVERABLE] = "unrecoverable",
475d569d5bbSJames Bottomley 	[ENCLOSURE_STATUS_NOT_INSTALLED] = "not installed",
476d569d5bbSJames Bottomley 	[ENCLOSURE_STATUS_UNKNOWN] = "unknown",
477d569d5bbSJames Bottomley 	[ENCLOSURE_STATUS_UNAVAILABLE] = "unavailable",
478cc9b2e9fSJames Bottomley 	[ENCLOSURE_STATUS_MAX] = NULL,
479d569d5bbSJames Bottomley };
480d569d5bbSJames Bottomley 
481d569d5bbSJames Bottomley static const char *const enclosure_type[] = {
482d569d5bbSJames Bottomley 	[ENCLOSURE_COMPONENT_DEVICE] = "device",
483d569d5bbSJames Bottomley 	[ENCLOSURE_COMPONENT_ARRAY_DEVICE] = "array device",
484d569d5bbSJames Bottomley };
485d569d5bbSJames Bottomley 
486ee959b00STony Jones static ssize_t get_component_fault(struct device *cdev,
487ee959b00STony Jones 				   struct device_attribute *attr, char *buf)
488d569d5bbSJames Bottomley {
489d569d5bbSJames Bottomley 	struct enclosure_device *edev = to_enclosure_device(cdev->parent);
490d569d5bbSJames Bottomley 	struct enclosure_component *ecomp = to_enclosure_component(cdev);
491d569d5bbSJames Bottomley 
492d569d5bbSJames Bottomley 	if (edev->cb->get_fault)
493d569d5bbSJames Bottomley 		edev->cb->get_fault(edev, ecomp);
494d569d5bbSJames Bottomley 	return snprintf(buf, 40, "%d\n", ecomp->fault);
495d569d5bbSJames Bottomley }
496d569d5bbSJames Bottomley 
497ee959b00STony Jones static ssize_t set_component_fault(struct device *cdev,
498ee959b00STony Jones 				   struct device_attribute *attr,
499ee959b00STony Jones 				   const char *buf, size_t count)
500d569d5bbSJames Bottomley {
501d569d5bbSJames Bottomley 	struct enclosure_device *edev = to_enclosure_device(cdev->parent);
502d569d5bbSJames Bottomley 	struct enclosure_component *ecomp = to_enclosure_component(cdev);
503d569d5bbSJames Bottomley 	int val = simple_strtoul(buf, NULL, 0);
504d569d5bbSJames Bottomley 
505d569d5bbSJames Bottomley 	if (edev->cb->set_fault)
506d569d5bbSJames Bottomley 		edev->cb->set_fault(edev, ecomp, val);
507d569d5bbSJames Bottomley 	return count;
508d569d5bbSJames Bottomley }
509d569d5bbSJames Bottomley 
510ee959b00STony Jones static ssize_t get_component_status(struct device *cdev,
511ee959b00STony Jones 				    struct device_attribute *attr,char *buf)
512d569d5bbSJames Bottomley {
513d569d5bbSJames Bottomley 	struct enclosure_device *edev = to_enclosure_device(cdev->parent);
514d569d5bbSJames Bottomley 	struct enclosure_component *ecomp = to_enclosure_component(cdev);
515d569d5bbSJames Bottomley 
516d569d5bbSJames Bottomley 	if (edev->cb->get_status)
517d569d5bbSJames Bottomley 		edev->cb->get_status(edev, ecomp);
518d569d5bbSJames Bottomley 	return snprintf(buf, 40, "%s\n", enclosure_status[ecomp->status]);
519d569d5bbSJames Bottomley }
520d569d5bbSJames Bottomley 
521ee959b00STony Jones static ssize_t set_component_status(struct device *cdev,
522ee959b00STony Jones 				    struct device_attribute *attr,
523ee959b00STony Jones 				    const char *buf, size_t count)
524d569d5bbSJames Bottomley {
525d569d5bbSJames Bottomley 	struct enclosure_device *edev = to_enclosure_device(cdev->parent);
526d569d5bbSJames Bottomley 	struct enclosure_component *ecomp = to_enclosure_component(cdev);
527d569d5bbSJames Bottomley 	int i;
528d569d5bbSJames Bottomley 
529d569d5bbSJames Bottomley 	for (i = 0; enclosure_status[i]; i++) {
530d569d5bbSJames Bottomley 		if (strncmp(buf, enclosure_status[i],
531d569d5bbSJames Bottomley 			    strlen(enclosure_status[i])) == 0 &&
532d569d5bbSJames Bottomley 		    (buf[strlen(enclosure_status[i])] == '\n' ||
533d569d5bbSJames Bottomley 		     buf[strlen(enclosure_status[i])] == '\0'))
534d569d5bbSJames Bottomley 			break;
535d569d5bbSJames Bottomley 	}
536d569d5bbSJames Bottomley 
537d569d5bbSJames Bottomley 	if (enclosure_status[i] && edev->cb->set_status) {
538d569d5bbSJames Bottomley 		edev->cb->set_status(edev, ecomp, i);
539d569d5bbSJames Bottomley 		return count;
540d569d5bbSJames Bottomley 	} else
541d569d5bbSJames Bottomley 		return -EINVAL;
542d569d5bbSJames Bottomley }
543d569d5bbSJames Bottomley 
544ee959b00STony Jones static ssize_t get_component_active(struct device *cdev,
545ee959b00STony Jones 				    struct device_attribute *attr, char *buf)
546d569d5bbSJames Bottomley {
547d569d5bbSJames Bottomley 	struct enclosure_device *edev = to_enclosure_device(cdev->parent);
548d569d5bbSJames Bottomley 	struct enclosure_component *ecomp = to_enclosure_component(cdev);
549d569d5bbSJames Bottomley 
550d569d5bbSJames Bottomley 	if (edev->cb->get_active)
551d569d5bbSJames Bottomley 		edev->cb->get_active(edev, ecomp);
552d569d5bbSJames Bottomley 	return snprintf(buf, 40, "%d\n", ecomp->active);
553d569d5bbSJames Bottomley }
554d569d5bbSJames Bottomley 
555ee959b00STony Jones static ssize_t set_component_active(struct device *cdev,
556ee959b00STony Jones 				    struct device_attribute *attr,
557ee959b00STony Jones 				    const char *buf, size_t count)
558d569d5bbSJames Bottomley {
559d569d5bbSJames Bottomley 	struct enclosure_device *edev = to_enclosure_device(cdev->parent);
560d569d5bbSJames Bottomley 	struct enclosure_component *ecomp = to_enclosure_component(cdev);
561d569d5bbSJames Bottomley 	int val = simple_strtoul(buf, NULL, 0);
562d569d5bbSJames Bottomley 
563d569d5bbSJames Bottomley 	if (edev->cb->set_active)
564d569d5bbSJames Bottomley 		edev->cb->set_active(edev, ecomp, val);
565d569d5bbSJames Bottomley 	return count;
566d569d5bbSJames Bottomley }
567d569d5bbSJames Bottomley 
568ee959b00STony Jones static ssize_t get_component_locate(struct device *cdev,
569ee959b00STony Jones 				    struct device_attribute *attr, char *buf)
570d569d5bbSJames Bottomley {
571d569d5bbSJames Bottomley 	struct enclosure_device *edev = to_enclosure_device(cdev->parent);
572d569d5bbSJames Bottomley 	struct enclosure_component *ecomp = to_enclosure_component(cdev);
573d569d5bbSJames Bottomley 
574d569d5bbSJames Bottomley 	if (edev->cb->get_locate)
575d569d5bbSJames Bottomley 		edev->cb->get_locate(edev, ecomp);
576d569d5bbSJames Bottomley 	return snprintf(buf, 40, "%d\n", ecomp->locate);
577d569d5bbSJames Bottomley }
578d569d5bbSJames Bottomley 
579ee959b00STony Jones static ssize_t set_component_locate(struct device *cdev,
580ee959b00STony Jones 				    struct device_attribute *attr,
581ee959b00STony Jones 				    const char *buf, size_t count)
582d569d5bbSJames Bottomley {
583d569d5bbSJames Bottomley 	struct enclosure_device *edev = to_enclosure_device(cdev->parent);
584d569d5bbSJames Bottomley 	struct enclosure_component *ecomp = to_enclosure_component(cdev);
585d569d5bbSJames Bottomley 	int val = simple_strtoul(buf, NULL, 0);
586d569d5bbSJames Bottomley 
587d569d5bbSJames Bottomley 	if (edev->cb->set_locate)
588d569d5bbSJames Bottomley 		edev->cb->set_locate(edev, ecomp, val);
589d569d5bbSJames Bottomley 	return count;
590d569d5bbSJames Bottomley }
591d569d5bbSJames Bottomley 
59208024885SSong Liu static ssize_t get_component_power_status(struct device *cdev,
59308024885SSong Liu 					  struct device_attribute *attr,
59408024885SSong Liu 					  char *buf)
59508024885SSong Liu {
59608024885SSong Liu 	struct enclosure_device *edev = to_enclosure_device(cdev->parent);
59708024885SSong Liu 	struct enclosure_component *ecomp = to_enclosure_component(cdev);
59808024885SSong Liu 
59908024885SSong Liu 	if (edev->cb->get_power_status)
60008024885SSong Liu 		edev->cb->get_power_status(edev, ecomp);
60175106523SMauricio Faria de Oliveira 
60275106523SMauricio Faria de Oliveira 	/* If still uninitialized, the callback failed or does not exist. */
60375106523SMauricio Faria de Oliveira 	if (ecomp->power_status == -1)
60475106523SMauricio Faria de Oliveira 		return (edev->cb->get_power_status) ? -EIO : -ENOTTY;
60575106523SMauricio Faria de Oliveira 
60608024885SSong Liu 	return snprintf(buf, 40, "%s\n", ecomp->power_status ? "on" : "off");
60708024885SSong Liu }
60808024885SSong Liu 
60908024885SSong Liu static ssize_t set_component_power_status(struct device *cdev,
61008024885SSong Liu 					  struct device_attribute *attr,
61108024885SSong Liu 					  const char *buf, size_t count)
61208024885SSong Liu {
61308024885SSong Liu 	struct enclosure_device *edev = to_enclosure_device(cdev->parent);
61408024885SSong Liu 	struct enclosure_component *ecomp = to_enclosure_component(cdev);
61508024885SSong Liu 	int val;
61608024885SSong Liu 
61708024885SSong Liu 	if (strncmp(buf, "on", 2) == 0 &&
61808024885SSong Liu 	    (buf[2] == '\n' || buf[2] == '\0'))
61908024885SSong Liu 		val = 1;
62008024885SSong Liu 	else if (strncmp(buf, "off", 3) == 0 &&
62108024885SSong Liu 	    (buf[3] == '\n' || buf[3] == '\0'))
62208024885SSong Liu 		val = 0;
62308024885SSong Liu 	else
62408024885SSong Liu 		return -EINVAL;
62508024885SSong Liu 
62608024885SSong Liu 	if (edev->cb->set_power_status)
62708024885SSong Liu 		edev->cb->set_power_status(edev, ecomp, val);
62808024885SSong Liu 	return count;
62908024885SSong Liu }
63008024885SSong Liu 
631ee959b00STony Jones static ssize_t get_component_type(struct device *cdev,
632ee959b00STony Jones 				  struct device_attribute *attr, char *buf)
633d569d5bbSJames Bottomley {
634d569d5bbSJames Bottomley 	struct enclosure_component *ecomp = to_enclosure_component(cdev);
635d569d5bbSJames Bottomley 
636d569d5bbSJames Bottomley 	return snprintf(buf, 40, "%s\n", enclosure_type[ecomp->type]);
637d569d5bbSJames Bottomley }
638d569d5bbSJames Bottomley 
639921ce7f5SDan Williams static ssize_t get_component_slot(struct device *cdev,
640921ce7f5SDan Williams 				  struct device_attribute *attr, char *buf)
641921ce7f5SDan Williams {
642921ce7f5SDan Williams 	struct enclosure_component *ecomp = to_enclosure_component(cdev);
643921ce7f5SDan Williams 	int slot;
644921ce7f5SDan Williams 
645921ce7f5SDan Williams 	/* if the enclosure does not override then use 'number' as a stand-in */
646921ce7f5SDan Williams 	if (ecomp->slot >= 0)
647921ce7f5SDan Williams 		slot = ecomp->slot;
648921ce7f5SDan Williams 	else
649921ce7f5SDan Williams 		slot = ecomp->number;
650921ce7f5SDan Williams 
651921ce7f5SDan Williams 	return snprintf(buf, 40, "%d\n", slot);
652921ce7f5SDan Williams }
653d569d5bbSJames Bottomley 
654cb6b7f40SJames Bottomley static DEVICE_ATTR(fault, S_IRUGO | S_IWUSR, get_component_fault,
655cb6b7f40SJames Bottomley 		    set_component_fault);
656cb6b7f40SJames Bottomley static DEVICE_ATTR(status, S_IRUGO | S_IWUSR, get_component_status,
657cb6b7f40SJames Bottomley 		   set_component_status);
658cb6b7f40SJames Bottomley static DEVICE_ATTR(active, S_IRUGO | S_IWUSR, get_component_active,
659cb6b7f40SJames Bottomley 		   set_component_active);
660cb6b7f40SJames Bottomley static DEVICE_ATTR(locate, S_IRUGO | S_IWUSR, get_component_locate,
661cb6b7f40SJames Bottomley 		   set_component_locate);
66208024885SSong Liu static DEVICE_ATTR(power_status, S_IRUGO | S_IWUSR, get_component_power_status,
66308024885SSong Liu 		   set_component_power_status);
664cb6b7f40SJames Bottomley static DEVICE_ATTR(type, S_IRUGO, get_component_type, NULL);
665921ce7f5SDan Williams static DEVICE_ATTR(slot, S_IRUGO, get_component_slot, NULL);
666cb6b7f40SJames Bottomley 
667cb6b7f40SJames Bottomley static struct attribute *enclosure_component_attrs[] = {
668cb6b7f40SJames Bottomley 	&dev_attr_fault.attr,
669cb6b7f40SJames Bottomley 	&dev_attr_status.attr,
670cb6b7f40SJames Bottomley 	&dev_attr_active.attr,
671cb6b7f40SJames Bottomley 	&dev_attr_locate.attr,
67208024885SSong Liu 	&dev_attr_power_status.attr,
673cb6b7f40SJames Bottomley 	&dev_attr_type.attr,
674921ce7f5SDan Williams 	&dev_attr_slot.attr,
675cb6b7f40SJames Bottomley 	NULL
676d569d5bbSJames Bottomley };
677899826f1SGreg Kroah-Hartman ATTRIBUTE_GROUPS(enclosure_component);
678d569d5bbSJames Bottomley 
679d569d5bbSJames Bottomley static int __init enclosure_init(void)
680d569d5bbSJames Bottomley {
681750b54deSArvind Yadav 	return class_register(&enclosure_class);
682d569d5bbSJames Bottomley }
683d569d5bbSJames Bottomley 
684d569d5bbSJames Bottomley static void __exit enclosure_exit(void)
685d569d5bbSJames Bottomley {
686d569d5bbSJames Bottomley 	class_unregister(&enclosure_class);
687d569d5bbSJames Bottomley }
688d569d5bbSJames Bottomley 
689d569d5bbSJames Bottomley module_init(enclosure_init);
690d569d5bbSJames Bottomley module_exit(enclosure_exit);
691d569d5bbSJames Bottomley 
692d569d5bbSJames Bottomley MODULE_AUTHOR("James Bottomley");
693d569d5bbSJames Bottomley MODULE_DESCRIPTION("Enclosure Services");
694d569d5bbSJames Bottomley MODULE_LICENSE("GPL v2");
695