xref: /openbmc/linux/drivers/misc/enclosure.c (revision 08024885a2a3ed432716e9d50046a620a5b2df05)
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 =
128d569d5bbSJames Bottomley 		kzalloc(sizeof(struct enclosure_device) +
129d569d5bbSJames Bottomley 			sizeof(struct enclosure_component)*components,
130d569d5bbSJames Bottomley 			GFP_KERNEL);
131d569d5bbSJames Bottomley 	int err, i;
132d569d5bbSJames Bottomley 
133d569d5bbSJames Bottomley 	BUG_ON(!cb);
134d569d5bbSJames Bottomley 
135d569d5bbSJames Bottomley 	if (!edev)
136d569d5bbSJames Bottomley 		return ERR_PTR(-ENOMEM);
137d569d5bbSJames Bottomley 
138d569d5bbSJames Bottomley 	edev->components = components;
139d569d5bbSJames Bottomley 
140ee959b00STony Jones 	edev->edev.class = &enclosure_class;
141ee959b00STony Jones 	edev->edev.parent = get_device(dev);
142d569d5bbSJames Bottomley 	edev->cb = cb;
1435e43754fSYinghai Lu 	dev_set_name(&edev->edev, "%s", name);
144ee959b00STony Jones 	err = device_register(&edev->edev);
145d569d5bbSJames Bottomley 	if (err)
146d569d5bbSJames Bottomley 		goto err;
147d569d5bbSJames Bottomley 
148921ce7f5SDan Williams 	for (i = 0; i < components; i++) {
149d569d5bbSJames Bottomley 		edev->component[i].number = -1;
150921ce7f5SDan Williams 		edev->component[i].slot = -1;
151*08024885SSong Liu 		edev->component[i].power_status = 1;
152921ce7f5SDan Williams 	}
153d569d5bbSJames Bottomley 
154d569d5bbSJames Bottomley 	mutex_lock(&container_list_lock);
155d569d5bbSJames Bottomley 	list_add_tail(&edev->node, &container_list);
156d569d5bbSJames Bottomley 	mutex_unlock(&container_list_lock);
157d569d5bbSJames Bottomley 
158d569d5bbSJames Bottomley 	return edev;
159d569d5bbSJames Bottomley 
160d569d5bbSJames Bottomley  err:
161ee959b00STony Jones 	put_device(edev->edev.parent);
162d569d5bbSJames Bottomley 	kfree(edev);
163d569d5bbSJames Bottomley 	return ERR_PTR(err);
164d569d5bbSJames Bottomley }
165d569d5bbSJames Bottomley EXPORT_SYMBOL_GPL(enclosure_register);
166d569d5bbSJames Bottomley 
167d569d5bbSJames Bottomley static struct enclosure_component_callbacks enclosure_null_callbacks;
168d569d5bbSJames Bottomley 
169d569d5bbSJames Bottomley /**
170d569d5bbSJames Bottomley  * enclosure_unregister - remove an enclosure
171d569d5bbSJames Bottomley  *
172d569d5bbSJames Bottomley  * @edev:	the registered enclosure to remove;
173d569d5bbSJames Bottomley  */
174d569d5bbSJames Bottomley void enclosure_unregister(struct enclosure_device *edev)
175d569d5bbSJames Bottomley {
176d569d5bbSJames Bottomley 	int i;
177d569d5bbSJames Bottomley 
178d569d5bbSJames Bottomley 	mutex_lock(&container_list_lock);
179d569d5bbSJames Bottomley 	list_del(&edev->node);
180d569d5bbSJames Bottomley 	mutex_unlock(&container_list_lock);
181d569d5bbSJames Bottomley 
182d569d5bbSJames Bottomley 	for (i = 0; i < edev->components; i++)
183d569d5bbSJames Bottomley 		if (edev->component[i].number != -1)
184ee959b00STony Jones 			device_unregister(&edev->component[i].cdev);
185d569d5bbSJames Bottomley 
186d569d5bbSJames Bottomley 	/* prevent any callbacks into service user */
187d569d5bbSJames Bottomley 	edev->cb = &enclosure_null_callbacks;
188ee959b00STony Jones 	device_unregister(&edev->edev);
189d569d5bbSJames Bottomley }
190d569d5bbSJames Bottomley EXPORT_SYMBOL_GPL(enclosure_unregister);
191d569d5bbSJames Bottomley 
192cb6b7f40SJames Bottomley #define ENCLOSURE_NAME_SIZE	64
193d2fd76e6SMarkus Stockhausen #define COMPONENT_NAME_SIZE	64
194cb6b7f40SJames Bottomley 
195cb6b7f40SJames Bottomley static void enclosure_link_name(struct enclosure_component *cdev, char *name)
196cb6b7f40SJames Bottomley {
197cb6b7f40SJames Bottomley 	strcpy(name, "enclosure_device:");
19871610f55SKay Sievers 	strcat(name, dev_name(&cdev->cdev));
199cb6b7f40SJames Bottomley }
200cb6b7f40SJames Bottomley 
201cb6b7f40SJames Bottomley static void enclosure_remove_links(struct enclosure_component *cdev)
202cb6b7f40SJames Bottomley {
203cb6b7f40SJames Bottomley 	char name[ENCLOSURE_NAME_SIZE];
204cb6b7f40SJames 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 	 */
209a1470c7bSJames Bottomley 	if (!cdev->dev->kobj.sd)
210a1470c7bSJames Bottomley 		return;
211a1470c7bSJames Bottomley 
212cb6b7f40SJames Bottomley 	enclosure_link_name(cdev, name);
213cb6b7f40SJames Bottomley 	sysfs_remove_link(&cdev->dev->kobj, name);
214cb6b7f40SJames Bottomley 	sysfs_remove_link(&cdev->cdev.kobj, "device");
215cb6b7f40SJames Bottomley }
216cb6b7f40SJames Bottomley 
217cb6b7f40SJames Bottomley static int enclosure_add_links(struct enclosure_component *cdev)
218cb6b7f40SJames Bottomley {
219cb6b7f40SJames Bottomley 	int error;
220cb6b7f40SJames Bottomley 	char name[ENCLOSURE_NAME_SIZE];
221cb6b7f40SJames Bottomley 
222cb6b7f40SJames Bottomley 	error = sysfs_create_link(&cdev->cdev.kobj, &cdev->dev->kobj, "device");
223cb6b7f40SJames Bottomley 	if (error)
224cb6b7f40SJames Bottomley 		return error;
225cb6b7f40SJames Bottomley 
226cb6b7f40SJames Bottomley 	enclosure_link_name(cdev, name);
227cb6b7f40SJames Bottomley 	error = sysfs_create_link(&cdev->dev->kobj, &cdev->cdev.kobj, name);
228cb6b7f40SJames Bottomley 	if (error)
229cb6b7f40SJames Bottomley 		sysfs_remove_link(&cdev->cdev.kobj, "device");
230cb6b7f40SJames Bottomley 
231cb6b7f40SJames Bottomley 	return error;
232cb6b7f40SJames Bottomley }
233cb6b7f40SJames Bottomley 
234ee959b00STony Jones static void enclosure_release(struct device *cdev)
235d569d5bbSJames Bottomley {
236d569d5bbSJames Bottomley 	struct enclosure_device *edev = to_enclosure_device(cdev);
237d569d5bbSJames Bottomley 
238ee959b00STony Jones 	put_device(cdev->parent);
239d569d5bbSJames Bottomley 	kfree(edev);
240d569d5bbSJames Bottomley }
241d569d5bbSJames Bottomley 
242ee959b00STony Jones static void enclosure_component_release(struct device *dev)
243d569d5bbSJames Bottomley {
244ee959b00STony Jones 	struct enclosure_component *cdev = to_enclosure_component(dev);
245ee959b00STony Jones 
246cb6b7f40SJames Bottomley 	if (cdev->dev) {
247cb6b7f40SJames Bottomley 		enclosure_remove_links(cdev);
248d569d5bbSJames Bottomley 		put_device(cdev->dev);
249cb6b7f40SJames Bottomley 	}
250ee959b00STony Jones 	put_device(dev->parent);
251d569d5bbSJames Bottomley }
252d569d5bbSJames Bottomley 
253d2fd76e6SMarkus Stockhausen static struct enclosure_component *
254d2fd76e6SMarkus Stockhausen enclosure_component_find_by_name(struct enclosure_device *edev,
255d2fd76e6SMarkus Stockhausen 				const char *name)
256d2fd76e6SMarkus Stockhausen {
257d2fd76e6SMarkus Stockhausen 	int i;
258d2fd76e6SMarkus Stockhausen 	const char *cname;
259d2fd76e6SMarkus Stockhausen 	struct enclosure_component *ecomp;
260d2fd76e6SMarkus Stockhausen 
261d2fd76e6SMarkus Stockhausen 	if (!edev || !name || !name[0])
262d2fd76e6SMarkus Stockhausen 		return NULL;
263d2fd76e6SMarkus Stockhausen 
264d2fd76e6SMarkus Stockhausen 	for (i = 0; i < edev->components; i++) {
265d2fd76e6SMarkus Stockhausen 		ecomp = &edev->component[i];
266d2fd76e6SMarkus Stockhausen 		cname = dev_name(&ecomp->cdev);
267d2fd76e6SMarkus Stockhausen 		if (ecomp->number != -1 &&
268d2fd76e6SMarkus Stockhausen 		    cname && cname[0] &&
269d2fd76e6SMarkus Stockhausen 		    !strcmp(cname, name))
270d2fd76e6SMarkus Stockhausen 			return ecomp;
271d2fd76e6SMarkus Stockhausen 	}
272d2fd76e6SMarkus Stockhausen 
273d2fd76e6SMarkus Stockhausen 	return NULL;
274d2fd76e6SMarkus Stockhausen }
275d2fd76e6SMarkus Stockhausen 
276899826f1SGreg Kroah-Hartman static const struct attribute_group *enclosure_component_groups[];
277cb6b7f40SJames Bottomley 
278d569d5bbSJames Bottomley /**
279ed09dcc8SDan Williams  * enclosure_component_alloc - prepare a new enclosure component
280d569d5bbSJames Bottomley  * @edev:	the enclosure to add the component
281d569d5bbSJames Bottomley  * @num:	the device number
282d569d5bbSJames Bottomley  * @type:	the type of component being added
283d569d5bbSJames Bottomley  * @name:	an optional name to appear in sysfs (leave NULL if none)
284d569d5bbSJames Bottomley  *
285ed09dcc8SDan Williams  * The name is optional for enclosures that give their components a unique
286ed09dcc8SDan Williams  * name.  If not, leave the field NULL and a name will be assigned.
287d569d5bbSJames Bottomley  *
288d569d5bbSJames Bottomley  * Returns a pointer to the enclosure component or an error.
289d569d5bbSJames Bottomley  */
290d569d5bbSJames Bottomley struct enclosure_component *
291ed09dcc8SDan Williams enclosure_component_alloc(struct enclosure_device *edev,
292d569d5bbSJames Bottomley 			  unsigned int number,
293d569d5bbSJames Bottomley 			  enum enclosure_component_type type,
294d569d5bbSJames Bottomley 			  const char *name)
295d569d5bbSJames Bottomley {
296d569d5bbSJames Bottomley 	struct enclosure_component *ecomp;
297ee959b00STony Jones 	struct device *cdev;
298ed09dcc8SDan Williams 	int i;
299d2fd76e6SMarkus Stockhausen 	char newname[COMPONENT_NAME_SIZE];
300d569d5bbSJames Bottomley 
301d569d5bbSJames Bottomley 	if (number >= edev->components)
302d569d5bbSJames Bottomley 		return ERR_PTR(-EINVAL);
303d569d5bbSJames Bottomley 
304d569d5bbSJames Bottomley 	ecomp = &edev->component[number];
305d569d5bbSJames Bottomley 
306d569d5bbSJames Bottomley 	if (ecomp->number != -1)
307d569d5bbSJames Bottomley 		return ERR_PTR(-EINVAL);
308d569d5bbSJames Bottomley 
309d569d5bbSJames Bottomley 	ecomp->type = type;
310d569d5bbSJames Bottomley 	ecomp->number = number;
311d569d5bbSJames Bottomley 	cdev = &ecomp->cdev;
312ee959b00STony Jones 	cdev->parent = get_device(&edev->edev);
313d2fd76e6SMarkus Stockhausen 
314d2fd76e6SMarkus Stockhausen 	if (name && name[0]) {
315d2fd76e6SMarkus Stockhausen 		/* Some hardware (e.g. enclosure in RX300 S6) has components
316d2fd76e6SMarkus Stockhausen 		 * with non unique names. Registering duplicates in sysfs
317d2fd76e6SMarkus Stockhausen 		 * will lead to warnings during bootup. So make the names
318d2fd76e6SMarkus Stockhausen 		 * unique by appending consecutive numbers -1, -2, ... */
319d2fd76e6SMarkus Stockhausen 		i = 1;
320d2fd76e6SMarkus Stockhausen 		snprintf(newname, COMPONENT_NAME_SIZE,
321d2fd76e6SMarkus Stockhausen 			 "%s", name);
322d2fd76e6SMarkus Stockhausen 		while (enclosure_component_find_by_name(edev, newname))
323d2fd76e6SMarkus Stockhausen 			snprintf(newname, COMPONENT_NAME_SIZE,
324d2fd76e6SMarkus Stockhausen 				 "%s-%i", name, i++);
325d2fd76e6SMarkus Stockhausen 		dev_set_name(cdev, "%s", newname);
326d2fd76e6SMarkus Stockhausen 	} else
32771610f55SKay Sievers 		dev_set_name(cdev, "%u", number);
328d569d5bbSJames Bottomley 
329cb6b7f40SJames Bottomley 	cdev->release = enclosure_component_release;
330899826f1SGreg Kroah-Hartman 	cdev->groups = enclosure_component_groups;
331cb6b7f40SJames Bottomley 
332ed09dcc8SDan Williams 	return ecomp;
333ed09dcc8SDan Williams }
334ed09dcc8SDan Williams EXPORT_SYMBOL_GPL(enclosure_component_alloc);
335ed09dcc8SDan Williams 
336ed09dcc8SDan Williams /**
337ed09dcc8SDan Williams  * enclosure_component_register - publishes an initialized enclosure component
338ed09dcc8SDan Williams  * @ecomp:	component to add
339ed09dcc8SDan Williams  *
340ed09dcc8SDan Williams  * Returns 0 on successful registration, releases the component otherwise
341ed09dcc8SDan Williams  */
342ed09dcc8SDan Williams int enclosure_component_register(struct enclosure_component *ecomp)
343ed09dcc8SDan Williams {
344ed09dcc8SDan Williams 	struct device *cdev;
345ed09dcc8SDan Williams 	int err;
346ed09dcc8SDan Williams 
347ed09dcc8SDan Williams 	cdev = &ecomp->cdev;
348ee959b00STony Jones 	err = device_register(cdev);
349a91c1be2SJames Bottomley 	if (err) {
350a91c1be2SJames Bottomley 		ecomp->number = -1;
351a91c1be2SJames Bottomley 		put_device(cdev);
352ed09dcc8SDan Williams 		return err;
353a91c1be2SJames Bottomley 	}
354d569d5bbSJames Bottomley 
355ed09dcc8SDan Williams 	return 0;
356d569d5bbSJames Bottomley }
357d569d5bbSJames Bottomley EXPORT_SYMBOL_GPL(enclosure_component_register);
358d569d5bbSJames Bottomley 
359d569d5bbSJames Bottomley /**
360d569d5bbSJames Bottomley  * enclosure_add_device - add a device as being part of an enclosure
361d569d5bbSJames Bottomley  * @edev:	the enclosure device being added to.
362d569d5bbSJames Bottomley  * @num:	the number of the component
363d569d5bbSJames Bottomley  * @dev:	the device being added
364d569d5bbSJames Bottomley  *
365d569d5bbSJames Bottomley  * Declares a real device to reside in slot (or identifier) @num of an
366d569d5bbSJames Bottomley  * enclosure.  This will cause the relevant sysfs links to appear.
367d569d5bbSJames Bottomley  * This function may also be used to change a device associated with
368d569d5bbSJames Bottomley  * an enclosure without having to call enclosure_remove_device() in
369d569d5bbSJames Bottomley  * between.
370d569d5bbSJames Bottomley  *
371d569d5bbSJames Bottomley  * Returns zero on success or an error.
372d569d5bbSJames Bottomley  */
373d569d5bbSJames Bottomley int enclosure_add_device(struct enclosure_device *edev, int component,
374d569d5bbSJames Bottomley 			 struct device *dev)
375d569d5bbSJames Bottomley {
376ee959b00STony Jones 	struct enclosure_component *cdev;
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 
386cb6b7f40SJames Bottomley 	if (cdev->dev)
387cb6b7f40SJames Bottomley 		enclosure_remove_links(cdev);
388cb6b7f40SJames Bottomley 
389d569d5bbSJames Bottomley 	put_device(cdev->dev);
390d569d5bbSJames Bottomley 	cdev->dev = get_device(dev);
391cb6b7f40SJames Bottomley 	return enclosure_add_links(cdev);
392d569d5bbSJames Bottomley }
393d569d5bbSJames Bottomley EXPORT_SYMBOL_GPL(enclosure_add_device);
394d569d5bbSJames Bottomley 
395d569d5bbSJames Bottomley /**
396d569d5bbSJames Bottomley  * enclosure_remove_device - remove a device from an enclosure
397d569d5bbSJames Bottomley  * @edev:	the enclosure device
398d569d5bbSJames Bottomley  * @num:	the number of the component to remove
399d569d5bbSJames Bottomley  *
400d569d5bbSJames Bottomley  * Returns zero on success or an error.
401d569d5bbSJames Bottomley  *
402d569d5bbSJames Bottomley  */
40343d8eb9cSJames Bottomley int enclosure_remove_device(struct enclosure_device *edev, struct device *dev)
404d569d5bbSJames Bottomley {
405ee959b00STony Jones 	struct enclosure_component *cdev;
40643d8eb9cSJames Bottomley 	int i;
407d569d5bbSJames Bottomley 
40843d8eb9cSJames Bottomley 	if (!edev || !dev)
409d569d5bbSJames Bottomley 		return -EINVAL;
410d569d5bbSJames Bottomley 
41143d8eb9cSJames Bottomley 	for (i = 0; i < edev->components; i++) {
41243d8eb9cSJames Bottomley 		cdev = &edev->component[i];
41343d8eb9cSJames Bottomley 		if (cdev->dev == dev) {
41443d8eb9cSJames Bottomley 			enclosure_remove_links(cdev);
415ee959b00STony Jones 			device_del(&cdev->cdev);
41643d8eb9cSJames Bottomley 			put_device(dev);
417d569d5bbSJames Bottomley 			cdev->dev = NULL;
418ee959b00STony Jones 			return device_add(&cdev->cdev);
419d569d5bbSJames Bottomley 		}
42043d8eb9cSJames Bottomley 	}
42143d8eb9cSJames Bottomley 	return -ENODEV;
42243d8eb9cSJames Bottomley }
423d569d5bbSJames Bottomley EXPORT_SYMBOL_GPL(enclosure_remove_device);
424d569d5bbSJames Bottomley 
425d569d5bbSJames Bottomley /*
426d569d5bbSJames Bottomley  * sysfs pieces below
427d569d5bbSJames Bottomley  */
428d569d5bbSJames Bottomley 
429899826f1SGreg Kroah-Hartman static ssize_t components_show(struct device *cdev,
430899826f1SGreg Kroah-Hartman 			       struct device_attribute *attr, char *buf)
431d569d5bbSJames Bottomley {
432d569d5bbSJames Bottomley 	struct enclosure_device *edev = to_enclosure_device(cdev);
433d569d5bbSJames Bottomley 
434d569d5bbSJames Bottomley 	return snprintf(buf, 40, "%d\n", edev->components);
435d569d5bbSJames Bottomley }
436899826f1SGreg Kroah-Hartman static DEVICE_ATTR_RO(components);
437d569d5bbSJames Bottomley 
438967f7babSDan Williams static ssize_t id_show(struct device *cdev,
439967f7babSDan Williams 				 struct device_attribute *attr,
440967f7babSDan Williams 				 char *buf)
441967f7babSDan Williams {
442967f7babSDan Williams 	struct enclosure_device *edev = to_enclosure_device(cdev);
443967f7babSDan Williams 
444967f7babSDan Williams 	if (edev->cb->show_id)
445967f7babSDan Williams 		return edev->cb->show_id(edev, buf);
446967f7babSDan Williams 	return -EINVAL;
447967f7babSDan Williams }
448967f7babSDan Williams static DEVICE_ATTR_RO(id);
449967f7babSDan Williams 
450899826f1SGreg Kroah-Hartman static struct attribute *enclosure_class_attrs[] = {
451899826f1SGreg Kroah-Hartman 	&dev_attr_components.attr,
452967f7babSDan Williams 	&dev_attr_id.attr,
453899826f1SGreg Kroah-Hartman 	NULL,
454d569d5bbSJames Bottomley };
455899826f1SGreg Kroah-Hartman ATTRIBUTE_GROUPS(enclosure_class);
456d569d5bbSJames Bottomley 
457d569d5bbSJames Bottomley static struct class enclosure_class = {
458d569d5bbSJames Bottomley 	.name			= "enclosure",
459d569d5bbSJames Bottomley 	.owner			= THIS_MODULE,
460ee959b00STony Jones 	.dev_release		= enclosure_release,
461899826f1SGreg Kroah-Hartman 	.dev_groups		= enclosure_class_groups,
462d569d5bbSJames Bottomley };
463d569d5bbSJames Bottomley 
464d569d5bbSJames Bottomley static const char *const enclosure_status [] = {
465d569d5bbSJames Bottomley 	[ENCLOSURE_STATUS_UNSUPPORTED] = "unsupported",
466d569d5bbSJames Bottomley 	[ENCLOSURE_STATUS_OK] = "OK",
467d569d5bbSJames Bottomley 	[ENCLOSURE_STATUS_CRITICAL] = "critical",
468d569d5bbSJames Bottomley 	[ENCLOSURE_STATUS_NON_CRITICAL] = "non-critical",
469d569d5bbSJames Bottomley 	[ENCLOSURE_STATUS_UNRECOVERABLE] = "unrecoverable",
470d569d5bbSJames Bottomley 	[ENCLOSURE_STATUS_NOT_INSTALLED] = "not installed",
471d569d5bbSJames Bottomley 	[ENCLOSURE_STATUS_UNKNOWN] = "unknown",
472d569d5bbSJames Bottomley 	[ENCLOSURE_STATUS_UNAVAILABLE] = "unavailable",
473cc9b2e9fSJames Bottomley 	[ENCLOSURE_STATUS_MAX] = NULL,
474d569d5bbSJames Bottomley };
475d569d5bbSJames Bottomley 
476d569d5bbSJames Bottomley static const char *const enclosure_type [] = {
477d569d5bbSJames Bottomley 	[ENCLOSURE_COMPONENT_DEVICE] = "device",
478d569d5bbSJames Bottomley 	[ENCLOSURE_COMPONENT_ARRAY_DEVICE] = "array device",
479d569d5bbSJames Bottomley };
480d569d5bbSJames Bottomley 
481ee959b00STony Jones static ssize_t get_component_fault(struct device *cdev,
482ee959b00STony Jones 				   struct device_attribute *attr, char *buf)
483d569d5bbSJames Bottomley {
484d569d5bbSJames Bottomley 	struct enclosure_device *edev = to_enclosure_device(cdev->parent);
485d569d5bbSJames Bottomley 	struct enclosure_component *ecomp = to_enclosure_component(cdev);
486d569d5bbSJames Bottomley 
487d569d5bbSJames Bottomley 	if (edev->cb->get_fault)
488d569d5bbSJames Bottomley 		edev->cb->get_fault(edev, ecomp);
489d569d5bbSJames Bottomley 	return snprintf(buf, 40, "%d\n", ecomp->fault);
490d569d5bbSJames Bottomley }
491d569d5bbSJames Bottomley 
492ee959b00STony Jones static ssize_t set_component_fault(struct device *cdev,
493ee959b00STony Jones 				   struct device_attribute *attr,
494ee959b00STony Jones 				   const char *buf, size_t count)
495d569d5bbSJames Bottomley {
496d569d5bbSJames Bottomley 	struct enclosure_device *edev = to_enclosure_device(cdev->parent);
497d569d5bbSJames Bottomley 	struct enclosure_component *ecomp = to_enclosure_component(cdev);
498d569d5bbSJames Bottomley 	int val = simple_strtoul(buf, NULL, 0);
499d569d5bbSJames Bottomley 
500d569d5bbSJames Bottomley 	if (edev->cb->set_fault)
501d569d5bbSJames Bottomley 		edev->cb->set_fault(edev, ecomp, val);
502d569d5bbSJames Bottomley 	return count;
503d569d5bbSJames Bottomley }
504d569d5bbSJames Bottomley 
505ee959b00STony Jones static ssize_t get_component_status(struct device *cdev,
506ee959b00STony Jones 				    struct device_attribute *attr,char *buf)
507d569d5bbSJames Bottomley {
508d569d5bbSJames Bottomley 	struct enclosure_device *edev = to_enclosure_device(cdev->parent);
509d569d5bbSJames Bottomley 	struct enclosure_component *ecomp = to_enclosure_component(cdev);
510d569d5bbSJames Bottomley 
511d569d5bbSJames Bottomley 	if (edev->cb->get_status)
512d569d5bbSJames Bottomley 		edev->cb->get_status(edev, ecomp);
513d569d5bbSJames Bottomley 	return snprintf(buf, 40, "%s\n", enclosure_status[ecomp->status]);
514d569d5bbSJames Bottomley }
515d569d5bbSJames Bottomley 
516ee959b00STony Jones static ssize_t set_component_status(struct device *cdev,
517ee959b00STony Jones 				    struct device_attribute *attr,
518ee959b00STony Jones 				    const char *buf, size_t count)
519d569d5bbSJames Bottomley {
520d569d5bbSJames Bottomley 	struct enclosure_device *edev = to_enclosure_device(cdev->parent);
521d569d5bbSJames Bottomley 	struct enclosure_component *ecomp = to_enclosure_component(cdev);
522d569d5bbSJames Bottomley 	int i;
523d569d5bbSJames Bottomley 
524d569d5bbSJames Bottomley 	for (i = 0; enclosure_status[i]; i++) {
525d569d5bbSJames Bottomley 		if (strncmp(buf, enclosure_status[i],
526d569d5bbSJames Bottomley 			    strlen(enclosure_status[i])) == 0 &&
527d569d5bbSJames Bottomley 		    (buf[strlen(enclosure_status[i])] == '\n' ||
528d569d5bbSJames Bottomley 		     buf[strlen(enclosure_status[i])] == '\0'))
529d569d5bbSJames Bottomley 			break;
530d569d5bbSJames Bottomley 	}
531d569d5bbSJames Bottomley 
532d569d5bbSJames Bottomley 	if (enclosure_status[i] && edev->cb->set_status) {
533d569d5bbSJames Bottomley 		edev->cb->set_status(edev, ecomp, i);
534d569d5bbSJames Bottomley 		return count;
535d569d5bbSJames Bottomley 	} else
536d569d5bbSJames Bottomley 		return -EINVAL;
537d569d5bbSJames Bottomley }
538d569d5bbSJames Bottomley 
539ee959b00STony Jones static ssize_t get_component_active(struct device *cdev,
540ee959b00STony Jones 				    struct device_attribute *attr, char *buf)
541d569d5bbSJames Bottomley {
542d569d5bbSJames Bottomley 	struct enclosure_device *edev = to_enclosure_device(cdev->parent);
543d569d5bbSJames Bottomley 	struct enclosure_component *ecomp = to_enclosure_component(cdev);
544d569d5bbSJames Bottomley 
545d569d5bbSJames Bottomley 	if (edev->cb->get_active)
546d569d5bbSJames Bottomley 		edev->cb->get_active(edev, ecomp);
547d569d5bbSJames Bottomley 	return snprintf(buf, 40, "%d\n", ecomp->active);
548d569d5bbSJames Bottomley }
549d569d5bbSJames Bottomley 
550ee959b00STony Jones static ssize_t set_component_active(struct device *cdev,
551ee959b00STony Jones 				    struct device_attribute *attr,
552ee959b00STony Jones 				    const char *buf, size_t count)
553d569d5bbSJames Bottomley {
554d569d5bbSJames Bottomley 	struct enclosure_device *edev = to_enclosure_device(cdev->parent);
555d569d5bbSJames Bottomley 	struct enclosure_component *ecomp = to_enclosure_component(cdev);
556d569d5bbSJames Bottomley 	int val = simple_strtoul(buf, NULL, 0);
557d569d5bbSJames Bottomley 
558d569d5bbSJames Bottomley 	if (edev->cb->set_active)
559d569d5bbSJames Bottomley 		edev->cb->set_active(edev, ecomp, val);
560d569d5bbSJames Bottomley 	return count;
561d569d5bbSJames Bottomley }
562d569d5bbSJames Bottomley 
563ee959b00STony Jones static ssize_t get_component_locate(struct device *cdev,
564ee959b00STony Jones 				    struct device_attribute *attr, char *buf)
565d569d5bbSJames Bottomley {
566d569d5bbSJames Bottomley 	struct enclosure_device *edev = to_enclosure_device(cdev->parent);
567d569d5bbSJames Bottomley 	struct enclosure_component *ecomp = to_enclosure_component(cdev);
568d569d5bbSJames Bottomley 
569d569d5bbSJames Bottomley 	if (edev->cb->get_locate)
570d569d5bbSJames Bottomley 		edev->cb->get_locate(edev, ecomp);
571d569d5bbSJames Bottomley 	return snprintf(buf, 40, "%d\n", ecomp->locate);
572d569d5bbSJames Bottomley }
573d569d5bbSJames Bottomley 
574ee959b00STony Jones static ssize_t set_component_locate(struct device *cdev,
575ee959b00STony Jones 				    struct device_attribute *attr,
576ee959b00STony Jones 				    const char *buf, size_t count)
577d569d5bbSJames Bottomley {
578d569d5bbSJames Bottomley 	struct enclosure_device *edev = to_enclosure_device(cdev->parent);
579d569d5bbSJames Bottomley 	struct enclosure_component *ecomp = to_enclosure_component(cdev);
580d569d5bbSJames Bottomley 	int val = simple_strtoul(buf, NULL, 0);
581d569d5bbSJames Bottomley 
582d569d5bbSJames Bottomley 	if (edev->cb->set_locate)
583d569d5bbSJames Bottomley 		edev->cb->set_locate(edev, ecomp, val);
584d569d5bbSJames Bottomley 	return count;
585d569d5bbSJames Bottomley }
586d569d5bbSJames Bottomley 
587*08024885SSong Liu static ssize_t get_component_power_status(struct device *cdev,
588*08024885SSong Liu 					  struct device_attribute *attr,
589*08024885SSong Liu 					  char *buf)
590*08024885SSong Liu {
591*08024885SSong Liu 	struct enclosure_device *edev = to_enclosure_device(cdev->parent);
592*08024885SSong Liu 	struct enclosure_component *ecomp = to_enclosure_component(cdev);
593*08024885SSong Liu 
594*08024885SSong Liu 	if (edev->cb->get_power_status)
595*08024885SSong Liu 		edev->cb->get_power_status(edev, ecomp);
596*08024885SSong Liu 	return snprintf(buf, 40, "%s\n", ecomp->power_status ? "on" : "off");
597*08024885SSong Liu }
598*08024885SSong Liu 
599*08024885SSong Liu static ssize_t set_component_power_status(struct device *cdev,
600*08024885SSong Liu 					  struct device_attribute *attr,
601*08024885SSong Liu 					  const char *buf, size_t count)
602*08024885SSong Liu {
603*08024885SSong Liu 	struct enclosure_device *edev = to_enclosure_device(cdev->parent);
604*08024885SSong Liu 	struct enclosure_component *ecomp = to_enclosure_component(cdev);
605*08024885SSong Liu 	int val;
606*08024885SSong Liu 
607*08024885SSong Liu 	if (strncmp(buf, "on", 2) == 0 &&
608*08024885SSong Liu 	    (buf[2] == '\n' || buf[2] == '\0'))
609*08024885SSong Liu 		val = 1;
610*08024885SSong Liu 	else if (strncmp(buf, "off", 3) == 0 &&
611*08024885SSong Liu 	    (buf[3] == '\n' || buf[3] == '\0'))
612*08024885SSong Liu 		val = 0;
613*08024885SSong Liu 	else
614*08024885SSong Liu 		return -EINVAL;
615*08024885SSong Liu 
616*08024885SSong Liu 	if (edev->cb->set_power_status)
617*08024885SSong Liu 		edev->cb->set_power_status(edev, ecomp, val);
618*08024885SSong Liu 	return count;
619*08024885SSong Liu }
620*08024885SSong Liu 
621ee959b00STony Jones static ssize_t get_component_type(struct device *cdev,
622ee959b00STony Jones 				  struct device_attribute *attr, char *buf)
623d569d5bbSJames Bottomley {
624d569d5bbSJames Bottomley 	struct enclosure_component *ecomp = to_enclosure_component(cdev);
625d569d5bbSJames Bottomley 
626d569d5bbSJames Bottomley 	return snprintf(buf, 40, "%s\n", enclosure_type[ecomp->type]);
627d569d5bbSJames Bottomley }
628d569d5bbSJames Bottomley 
629921ce7f5SDan Williams static ssize_t get_component_slot(struct device *cdev,
630921ce7f5SDan Williams 				  struct device_attribute *attr, char *buf)
631921ce7f5SDan Williams {
632921ce7f5SDan Williams 	struct enclosure_component *ecomp = to_enclosure_component(cdev);
633921ce7f5SDan Williams 	int slot;
634921ce7f5SDan Williams 
635921ce7f5SDan Williams 	/* if the enclosure does not override then use 'number' as a stand-in */
636921ce7f5SDan Williams 	if (ecomp->slot >= 0)
637921ce7f5SDan Williams 		slot = ecomp->slot;
638921ce7f5SDan Williams 	else
639921ce7f5SDan Williams 		slot = ecomp->number;
640921ce7f5SDan Williams 
641921ce7f5SDan Williams 	return snprintf(buf, 40, "%d\n", slot);
642921ce7f5SDan Williams }
643d569d5bbSJames Bottomley 
644cb6b7f40SJames Bottomley static DEVICE_ATTR(fault, S_IRUGO | S_IWUSR, get_component_fault,
645cb6b7f40SJames Bottomley 		    set_component_fault);
646cb6b7f40SJames Bottomley static DEVICE_ATTR(status, S_IRUGO | S_IWUSR, get_component_status,
647cb6b7f40SJames Bottomley 		   set_component_status);
648cb6b7f40SJames Bottomley static DEVICE_ATTR(active, S_IRUGO | S_IWUSR, get_component_active,
649cb6b7f40SJames Bottomley 		   set_component_active);
650cb6b7f40SJames Bottomley static DEVICE_ATTR(locate, S_IRUGO | S_IWUSR, get_component_locate,
651cb6b7f40SJames Bottomley 		   set_component_locate);
652*08024885SSong Liu static DEVICE_ATTR(power_status, S_IRUGO | S_IWUSR, get_component_power_status,
653*08024885SSong Liu 		   set_component_power_status);
654cb6b7f40SJames Bottomley static DEVICE_ATTR(type, S_IRUGO, get_component_type, NULL);
655921ce7f5SDan Williams static DEVICE_ATTR(slot, S_IRUGO, get_component_slot, NULL);
656cb6b7f40SJames Bottomley 
657cb6b7f40SJames Bottomley static struct attribute *enclosure_component_attrs[] = {
658cb6b7f40SJames Bottomley 	&dev_attr_fault.attr,
659cb6b7f40SJames Bottomley 	&dev_attr_status.attr,
660cb6b7f40SJames Bottomley 	&dev_attr_active.attr,
661cb6b7f40SJames Bottomley 	&dev_attr_locate.attr,
662*08024885SSong Liu 	&dev_attr_power_status.attr,
663cb6b7f40SJames Bottomley 	&dev_attr_type.attr,
664921ce7f5SDan Williams 	&dev_attr_slot.attr,
665cb6b7f40SJames Bottomley 	NULL
666d569d5bbSJames Bottomley };
667899826f1SGreg Kroah-Hartman ATTRIBUTE_GROUPS(enclosure_component);
668d569d5bbSJames Bottomley 
669d569d5bbSJames Bottomley static int __init enclosure_init(void)
670d569d5bbSJames Bottomley {
671d569d5bbSJames Bottomley 	int err;
672d569d5bbSJames Bottomley 
673d569d5bbSJames Bottomley 	err = class_register(&enclosure_class);
674d569d5bbSJames Bottomley 	if (err)
675d569d5bbSJames Bottomley 		return err;
676d569d5bbSJames Bottomley 
677d569d5bbSJames Bottomley 	return 0;
678d569d5bbSJames Bottomley }
679d569d5bbSJames Bottomley 
680d569d5bbSJames Bottomley static void __exit enclosure_exit(void)
681d569d5bbSJames Bottomley {
682d569d5bbSJames Bottomley 	class_unregister(&enclosure_class);
683d569d5bbSJames Bottomley }
684d569d5bbSJames Bottomley 
685d569d5bbSJames Bottomley module_init(enclosure_init);
686d569d5bbSJames Bottomley module_exit(enclosure_exit);
687d569d5bbSJames Bottomley 
688d569d5bbSJames Bottomley MODULE_AUTHOR("James Bottomley");
689d569d5bbSJames Bottomley MODULE_DESCRIPTION("Enclosure Services");
690d569d5bbSJames Bottomley MODULE_LICENSE("GPL v2");
691