Blame view

drivers/misc/enclosure.c 18.3 KB
82c298100   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-only
d569d5bb3   James Bottomley   [SCSI] enclosure:...
2
3
4
5
6
7
8
  /*
   * Enclosure Services
   *
   * Copyright (C) 2008 James Bottomley <James.Bottomley@HansenPartnership.com>
   *
  **-----------------------------------------------------------------------------
  **
d569d5bb3   James Bottomley   [SCSI] enclosure:...
9
10
11
12
13
14
15
16
17
18
  **
  **-----------------------------------------------------------------------------
  */
  #include <linux/device.h>
  #include <linux/enclosure.h>
  #include <linux/err.h>
  #include <linux/list.h>
  #include <linux/kernel.h>
  #include <linux/module.h>
  #include <linux/mutex.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
19
  #include <linux/slab.h>
d569d5bb3   James Bottomley   [SCSI] enclosure:...
20
21
22
23
  
  static LIST_HEAD(container_list);
  static DEFINE_MUTEX(container_list_lock);
  static struct class enclosure_class;
d569d5bb3   James Bottomley   [SCSI] enclosure:...
24
25
  
  /**
163f52b6c   James Bottomley   [SCSI] ses: fix h...
26
27
28
   * enclosure_find - find an enclosure given a parent device
   * @dev:	the parent to match against
   * @start:	Optional enclosure device to start from (NULL if none)
d569d5bb3   James Bottomley   [SCSI] enclosure:...
29
   *
163f52b6c   James Bottomley   [SCSI] ses: fix h...
30
31
32
33
34
35
36
37
38
39
40
41
   * Looks through the list of registered enclosures to find all those
   * with @dev as a parent.  Returns NULL if no enclosure is
   * found. @start can be used as a starting point to obtain multiple
   * enclosures per parent (should begin with NULL and then be set to
   * each returned enclosure device). Obtains a reference to the
   * enclosure class device which must be released with device_put().
   * If @start is not NULL, a reference must be taken on it which is
   * released before returning (this allows a loop through all
   * enclosures to exit with only the reference on the enclosure of
   * interest held).  Note that the @dev may correspond to the actual
   * device housing the enclosure, in which case no iteration via @start
   * is required.
d569d5bb3   James Bottomley   [SCSI] enclosure:...
42
   */
163f52b6c   James Bottomley   [SCSI] ses: fix h...
43
44
  struct enclosure_device *enclosure_find(struct device *dev,
  					struct enclosure_device *start)
d569d5bb3   James Bottomley   [SCSI] enclosure:...
45
  {
ee959b00c   Tony Jones   SCSI: convert str...
46
  	struct enclosure_device *edev;
d569d5bb3   James Bottomley   [SCSI] enclosure:...
47
48
  
  	mutex_lock(&container_list_lock);
163f52b6c   James Bottomley   [SCSI] ses: fix h...
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
  	edev = list_prepare_entry(start, &container_list, node);
  	if (start)
  		put_device(&start->edev);
  
  	list_for_each_entry_continue(edev, &container_list, node) {
  		struct device *parent = edev->edev.parent;
  		/* parent might not be immediate, so iterate up to
  		 * the root of the tree if necessary */
  		while (parent) {
  			if (parent == dev) {
  				get_device(&edev->edev);
  				mutex_unlock(&container_list_lock);
  				return edev;
  			}
  			parent = parent->parent;
d569d5bb3   James Bottomley   [SCSI] enclosure:...
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
  		}
  	}
  	mutex_unlock(&container_list_lock);
  
  	return NULL;
  }
  EXPORT_SYMBOL_GPL(enclosure_find);
  
  /**
   * enclosure_for_each_device - calls a function for each enclosure
   * @fn:		the function to call
   * @data:	the data to pass to each call
   *
   * Loops over all the enclosures calling the function.
   *
   * Note, this function uses a mutex which will be held across calls to
   * @fn, so it must have non atomic context, and @fn may (although it
   * should not) sleep or otherwise cause the mutex to be held for
   * indefinite periods
   */
  int enclosure_for_each_device(int (*fn)(struct enclosure_device *, void *),
  			      void *data)
  {
  	int error = 0;
  	struct enclosure_device *edev;
  
  	mutex_lock(&container_list_lock);
  	list_for_each_entry(edev, &container_list, node) {
  		error = fn(edev, data);
  		if (error)
  			break;
  	}
  	mutex_unlock(&container_list_lock);
  
  	return error;
  }
  EXPORT_SYMBOL_GPL(enclosure_for_each_device);
  
  /**
   * enclosure_register - register device as an enclosure
   *
   * @dev:	device containing the enclosure
   * @components:	number of components in the enclosure
   *
   * This sets up the device for being an enclosure.  Note that @dev does
   * not have to be a dedicated enclosure device.  It may be some other type
   * of device that additionally responds to enclosure services
   */
  struct enclosure_device *
  enclosure_register(struct device *dev, const char *name, int components,
  		   struct enclosure_component_callbacks *cb)
  {
  	struct enclosure_device *edev =
e3575c120   Gustavo A. R. Silva   misc: enclosure: ...
117
  		kzalloc(struct_size(edev, component, components), GFP_KERNEL);
d569d5bb3   James Bottomley   [SCSI] enclosure:...
118
119
120
121
122
123
124
125
  	int err, i;
  
  	BUG_ON(!cb);
  
  	if (!edev)
  		return ERR_PTR(-ENOMEM);
  
  	edev->components = components;
ee959b00c   Tony Jones   SCSI: convert str...
126
127
  	edev->edev.class = &enclosure_class;
  	edev->edev.parent = get_device(dev);
d569d5bb3   James Bottomley   [SCSI] enclosure:...
128
  	edev->cb = cb;
5e43754fd   Yinghai Lu   [SCSI] ses: fix p...
129
  	dev_set_name(&edev->edev, "%s", name);
ee959b00c   Tony Jones   SCSI: convert str...
130
  	err = device_register(&edev->edev);
d569d5bb3   James Bottomley   [SCSI] enclosure:...
131
132
  	if (err)
  		goto err;
921ce7f57   Dan Williams   ses: add reliable...
133
  	for (i = 0; i < components; i++) {
d569d5bb3   James Bottomley   [SCSI] enclosure:...
134
  		edev->component[i].number = -1;
921ce7f57   Dan Williams   ses: add reliable...
135
  		edev->component[i].slot = -1;
75106523f   Mauricio Faria de Oliveira   scsi: ses: don't ...
136
  		edev->component[i].power_status = -1;
921ce7f57   Dan Williams   ses: add reliable...
137
  	}
d569d5bb3   James Bottomley   [SCSI] enclosure:...
138
139
140
141
142
143
144
145
  
  	mutex_lock(&container_list_lock);
  	list_add_tail(&edev->node, &container_list);
  	mutex_unlock(&container_list_lock);
  
  	return edev;
  
   err:
ee959b00c   Tony Jones   SCSI: convert str...
146
  	put_device(edev->edev.parent);
d569d5bb3   James Bottomley   [SCSI] enclosure:...
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
  	kfree(edev);
  	return ERR_PTR(err);
  }
  EXPORT_SYMBOL_GPL(enclosure_register);
  
  static struct enclosure_component_callbacks enclosure_null_callbacks;
  
  /**
   * enclosure_unregister - remove an enclosure
   *
   * @edev:	the registered enclosure to remove;
   */
  void enclosure_unregister(struct enclosure_device *edev)
  {
  	int i;
  
  	mutex_lock(&container_list_lock);
  	list_del(&edev->node);
  	mutex_unlock(&container_list_lock);
  
  	for (i = 0; i < edev->components; i++)
  		if (edev->component[i].number != -1)
ee959b00c   Tony Jones   SCSI: convert str...
169
  			device_unregister(&edev->component[i].cdev);
d569d5bb3   James Bottomley   [SCSI] enclosure:...
170
171
172
  
  	/* prevent any callbacks into service user */
  	edev->cb = &enclosure_null_callbacks;
ee959b00c   Tony Jones   SCSI: convert str...
173
  	device_unregister(&edev->edev);
d569d5bb3   James Bottomley   [SCSI] enclosure:...
174
175
  }
  EXPORT_SYMBOL_GPL(enclosure_unregister);
cb6b7f406   James Bottomley   [SCSI] ses: fix u...
176
  #define ENCLOSURE_NAME_SIZE	64
d2fd76e6f   Markus Stockhausen   enclosure: handle...
177
  #define COMPONENT_NAME_SIZE	64
cb6b7f406   James Bottomley   [SCSI] ses: fix u...
178
179
180
181
  
  static void enclosure_link_name(struct enclosure_component *cdev, char *name)
  {
  	strcpy(name, "enclosure_device:");
71610f55f   Kay Sievers   [SCSI] struct dev...
182
  	strcat(name, dev_name(&cdev->cdev));
cb6b7f406   James Bottomley   [SCSI] ses: fix u...
183
184
185
186
187
  }
  
  static void enclosure_remove_links(struct enclosure_component *cdev)
  {
  	char name[ENCLOSURE_NAME_SIZE];
11e52a699   James Bottomley   enclosure: fix WA...
188
  	enclosure_link_name(cdev, name);
a1470c7bf   James Bottomley   [SCSI] enclosure:...
189
190
191
192
  	/*
  	 * In odd circumstances, like multipath devices, something else may
  	 * already have removed the links, so check for this condition first.
  	 */
11e52a699   James Bottomley   enclosure: fix WA...
193
194
  	if (cdev->dev->kobj.sd)
  		sysfs_remove_link(&cdev->dev->kobj, name);
a1470c7bf   James Bottomley   [SCSI] enclosure:...
195

11e52a699   James Bottomley   enclosure: fix WA...
196
197
  	if (cdev->cdev.kobj.sd)
  		sysfs_remove_link(&cdev->cdev.kobj, "device");
cb6b7f406   James Bottomley   [SCSI] ses: fix u...
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
  }
  
  static int enclosure_add_links(struct enclosure_component *cdev)
  {
  	int error;
  	char name[ENCLOSURE_NAME_SIZE];
  
  	error = sysfs_create_link(&cdev->cdev.kobj, &cdev->dev->kobj, "device");
  	if (error)
  		return error;
  
  	enclosure_link_name(cdev, name);
  	error = sysfs_create_link(&cdev->dev->kobj, &cdev->cdev.kobj, name);
  	if (error)
  		sysfs_remove_link(&cdev->cdev.kobj, "device");
  
  	return error;
  }
ee959b00c   Tony Jones   SCSI: convert str...
216
  static void enclosure_release(struct device *cdev)
d569d5bb3   James Bottomley   [SCSI] enclosure:...
217
218
  {
  	struct enclosure_device *edev = to_enclosure_device(cdev);
ee959b00c   Tony Jones   SCSI: convert str...
219
  	put_device(cdev->parent);
d569d5bb3   James Bottomley   [SCSI] enclosure:...
220
221
  	kfree(edev);
  }
ee959b00c   Tony Jones   SCSI: convert str...
222
  static void enclosure_component_release(struct device *dev)
d569d5bb3   James Bottomley   [SCSI] enclosure:...
223
  {
ee959b00c   Tony Jones   SCSI: convert str...
224
  	struct enclosure_component *cdev = to_enclosure_component(dev);
cb6b7f406   James Bottomley   [SCSI] ses: fix u...
225
226
227
228
  	if (cdev->dev) {
  		enclosure_remove_links(cdev);
  		put_device(cdev->dev);
  	}
ee959b00c   Tony Jones   SCSI: convert str...
229
  	put_device(dev->parent);
d569d5bb3   James Bottomley   [SCSI] enclosure:...
230
  }
d2fd76e6f   Markus Stockhausen   enclosure: handle...
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
  static struct enclosure_component *
  enclosure_component_find_by_name(struct enclosure_device *edev,
  				const char *name)
  {
  	int i;
  	const char *cname;
  	struct enclosure_component *ecomp;
  
  	if (!edev || !name || !name[0])
  		return NULL;
  
  	for (i = 0; i < edev->components; i++) {
  		ecomp = &edev->component[i];
  		cname = dev_name(&ecomp->cdev);
  		if (ecomp->number != -1 &&
  		    cname && cname[0] &&
  		    !strcmp(cname, name))
  			return ecomp;
  	}
  
  	return NULL;
  }
899826f16   Greg Kroah-Hartman   enclosure: conver...
253
  static const struct attribute_group *enclosure_component_groups[];
cb6b7f406   James Bottomley   [SCSI] ses: fix u...
254

d569d5bb3   James Bottomley   [SCSI] enclosure:...
255
  /**
ed09dcc8b   Dan Williams   ses: close potent...
256
   * enclosure_component_alloc - prepare a new enclosure component
d569d5bb3   James Bottomley   [SCSI] enclosure:...
257
258
259
260
261
   * @edev:	the enclosure to add the component
   * @num:	the device number
   * @type:	the type of component being added
   * @name:	an optional name to appear in sysfs (leave NULL if none)
   *
ed09dcc8b   Dan Williams   ses: close potent...
262
263
   * The name is optional for enclosures that give their components a unique
   * name.  If not, leave the field NULL and a name will be assigned.
d569d5bb3   James Bottomley   [SCSI] enclosure:...
264
265
266
267
   *
   * Returns a pointer to the enclosure component or an error.
   */
  struct enclosure_component *
ed09dcc8b   Dan Williams   ses: close potent...
268
269
270
271
  enclosure_component_alloc(struct enclosure_device *edev,
  			  unsigned int number,
  			  enum enclosure_component_type type,
  			  const char *name)
d569d5bb3   James Bottomley   [SCSI] enclosure:...
272
273
  {
  	struct enclosure_component *ecomp;
ee959b00c   Tony Jones   SCSI: convert str...
274
  	struct device *cdev;
ed09dcc8b   Dan Williams   ses: close potent...
275
  	int i;
d2fd76e6f   Markus Stockhausen   enclosure: handle...
276
  	char newname[COMPONENT_NAME_SIZE];
d569d5bb3   James Bottomley   [SCSI] enclosure:...
277
278
279
280
281
282
283
284
285
286
287
288
  
  	if (number >= edev->components)
  		return ERR_PTR(-EINVAL);
  
  	ecomp = &edev->component[number];
  
  	if (ecomp->number != -1)
  		return ERR_PTR(-EINVAL);
  
  	ecomp->type = type;
  	ecomp->number = number;
  	cdev = &ecomp->cdev;
ee959b00c   Tony Jones   SCSI: convert str...
289
  	cdev->parent = get_device(&edev->edev);
d2fd76e6f   Markus Stockhausen   enclosure: handle...
290
291
292
293
294
295
296
297
298
299
300
301
302
303
  
  	if (name && name[0]) {
  		/* Some hardware (e.g. enclosure in RX300 S6) has components
  		 * with non unique names. Registering duplicates in sysfs
  		 * will lead to warnings during bootup. So make the names
  		 * unique by appending consecutive numbers -1, -2, ... */
  		i = 1;
  		snprintf(newname, COMPONENT_NAME_SIZE,
  			 "%s", name);
  		while (enclosure_component_find_by_name(edev, newname))
  			snprintf(newname, COMPONENT_NAME_SIZE,
  				 "%s-%i", name, i++);
  		dev_set_name(cdev, "%s", newname);
  	} else
71610f55f   Kay Sievers   [SCSI] struct dev...
304
  		dev_set_name(cdev, "%u", number);
d569d5bb3   James Bottomley   [SCSI] enclosure:...
305

cb6b7f406   James Bottomley   [SCSI] ses: fix u...
306
  	cdev->release = enclosure_component_release;
899826f16   Greg Kroah-Hartman   enclosure: conver...
307
  	cdev->groups = enclosure_component_groups;
cb6b7f406   James Bottomley   [SCSI] ses: fix u...
308

ed09dcc8b   Dan Williams   ses: close potent...
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
  	return ecomp;
  }
  EXPORT_SYMBOL_GPL(enclosure_component_alloc);
  
  /**
   * enclosure_component_register - publishes an initialized enclosure component
   * @ecomp:	component to add
   *
   * Returns 0 on successful registration, releases the component otherwise
   */
  int enclosure_component_register(struct enclosure_component *ecomp)
  {
  	struct device *cdev;
  	int err;
  
  	cdev = &ecomp->cdev;
ee959b00c   Tony Jones   SCSI: convert str...
325
  	err = device_register(cdev);
a91c1be21   James Bottomley   [SCSI] enclosure:...
326
327
328
  	if (err) {
  		ecomp->number = -1;
  		put_device(cdev);
ed09dcc8b   Dan Williams   ses: close potent...
329
  		return err;
a91c1be21   James Bottomley   [SCSI] enclosure:...
330
  	}
d569d5bb3   James Bottomley   [SCSI] enclosure:...
331

ed09dcc8b   Dan Williams   ses: close potent...
332
  	return 0;
d569d5bb3   James Bottomley   [SCSI] enclosure:...
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
  }
  EXPORT_SYMBOL_GPL(enclosure_component_register);
  
  /**
   * enclosure_add_device - add a device as being part of an enclosure
   * @edev:	the enclosure device being added to.
   * @num:	the number of the component
   * @dev:	the device being added
   *
   * Declares a real device to reside in slot (or identifier) @num of an
   * enclosure.  This will cause the relevant sysfs links to appear.
   * This function may also be used to change a device associated with
   * an enclosure without having to call enclosure_remove_device() in
   * between.
   *
   * Returns zero on success or an error.
   */
  int enclosure_add_device(struct enclosure_device *edev, int component,
  			 struct device *dev)
  {
ee959b00c   Tony Jones   SCSI: convert str...
353
  	struct enclosure_component *cdev;
62e62ffd9   Maurizio Lombardi   scsi: ses: do not...
354
  	int err;
d569d5bb3   James Bottomley   [SCSI] enclosure:...
355
356
357
  
  	if (!edev || component >= edev->components)
  		return -EINVAL;
ee959b00c   Tony Jones   SCSI: convert str...
358
  	cdev = &edev->component[component];
d569d5bb3   James Bottomley   [SCSI] enclosure:...
359

21fab1d05   James Bottomley   [SCSI] ses: updat...
360
361
  	if (cdev->dev == dev)
  		return -EEXIST;
62e62ffd9   Maurizio Lombardi   scsi: ses: do not...
362
  	if (cdev->dev) {
cb6b7f406   James Bottomley   [SCSI] ses: fix u...
363
  		enclosure_remove_links(cdev);
62e62ffd9   Maurizio Lombardi   scsi: ses: do not...
364
365
  		put_device(cdev->dev);
  	}
d569d5bb3   James Bottomley   [SCSI] enclosure:...
366
  	cdev->dev = get_device(dev);
62e62ffd9   Maurizio Lombardi   scsi: ses: do not...
367
368
369
370
371
372
  	err = enclosure_add_links(cdev);
  	if (err) {
  		put_device(cdev->dev);
  		cdev->dev = NULL;
  	}
  	return err;
d569d5bb3   James Bottomley   [SCSI] enclosure:...
373
374
375
376
377
378
379
380
381
382
383
  }
  EXPORT_SYMBOL_GPL(enclosure_add_device);
  
  /**
   * enclosure_remove_device - remove a device from an enclosure
   * @edev:	the enclosure device
   * @num:	the number of the component to remove
   *
   * Returns zero on success or an error.
   *
   */
43d8eb9cf   James Bottomley   [SCSI] ses: add s...
384
  int enclosure_remove_device(struct enclosure_device *edev, struct device *dev)
d569d5bb3   James Bottomley   [SCSI] enclosure:...
385
  {
ee959b00c   Tony Jones   SCSI: convert str...
386
  	struct enclosure_component *cdev;
43d8eb9cf   James Bottomley   [SCSI] ses: add s...
387
  	int i;
d569d5bb3   James Bottomley   [SCSI] enclosure:...
388

43d8eb9cf   James Bottomley   [SCSI] ses: add s...
389
  	if (!edev || !dev)
d569d5bb3   James Bottomley   [SCSI] enclosure:...
390
  		return -EINVAL;
43d8eb9cf   James Bottomley   [SCSI] ses: add s...
391
392
393
394
395
396
397
398
399
400
401
  	for (i = 0; i < edev->components; i++) {
  		cdev = &edev->component[i];
  		if (cdev->dev == dev) {
  			enclosure_remove_links(cdev);
  			device_del(&cdev->cdev);
  			put_device(dev);
  			cdev->dev = NULL;
  			return device_add(&cdev->cdev);
  		}
  	}
  	return -ENODEV;
d569d5bb3   James Bottomley   [SCSI] enclosure:...
402
403
404
405
406
407
  }
  EXPORT_SYMBOL_GPL(enclosure_remove_device);
  
  /*
   * sysfs pieces below
   */
899826f16   Greg Kroah-Hartman   enclosure: conver...
408
409
  static ssize_t components_show(struct device *cdev,
  			       struct device_attribute *attr, char *buf)
d569d5bb3   James Bottomley   [SCSI] enclosure:...
410
411
412
413
414
415
  {
  	struct enclosure_device *edev = to_enclosure_device(cdev);
  
  	return snprintf(buf, 40, "%d
  ", edev->components);
  }
899826f16   Greg Kroah-Hartman   enclosure: conver...
416
  static DEVICE_ATTR_RO(components);
d569d5bb3   James Bottomley   [SCSI] enclosure:...
417

967f7bab0   Dan Williams   ses: add enclosur...
418
419
420
421
422
423
424
425
426
427
428
  static ssize_t id_show(struct device *cdev,
  				 struct device_attribute *attr,
  				 char *buf)
  {
  	struct enclosure_device *edev = to_enclosure_device(cdev);
  
  	if (edev->cb->show_id)
  		return edev->cb->show_id(edev, buf);
  	return -EINVAL;
  }
  static DEVICE_ATTR_RO(id);
899826f16   Greg Kroah-Hartman   enclosure: conver...
429
430
  static struct attribute *enclosure_class_attrs[] = {
  	&dev_attr_components.attr,
967f7bab0   Dan Williams   ses: add enclosur...
431
  	&dev_attr_id.attr,
899826f16   Greg Kroah-Hartman   enclosure: conver...
432
  	NULL,
d569d5bb3   James Bottomley   [SCSI] enclosure:...
433
  };
899826f16   Greg Kroah-Hartman   enclosure: conver...
434
  ATTRIBUTE_GROUPS(enclosure_class);
d569d5bb3   James Bottomley   [SCSI] enclosure:...
435
436
437
438
  
  static struct class enclosure_class = {
  	.name			= "enclosure",
  	.owner			= THIS_MODULE,
ee959b00c   Tony Jones   SCSI: convert str...
439
  	.dev_release		= enclosure_release,
899826f16   Greg Kroah-Hartman   enclosure: conver...
440
  	.dev_groups		= enclosure_class_groups,
d569d5bb3   James Bottomley   [SCSI] enclosure:...
441
  };
367ef846b   Arvind Yadav   misc: enclosure: ...
442
  static const char *const enclosure_status[] = {
d569d5bb3   James Bottomley   [SCSI] enclosure:...
443
444
445
446
447
448
449
450
  	[ENCLOSURE_STATUS_UNSUPPORTED] = "unsupported",
  	[ENCLOSURE_STATUS_OK] = "OK",
  	[ENCLOSURE_STATUS_CRITICAL] = "critical",
  	[ENCLOSURE_STATUS_NON_CRITICAL] = "non-critical",
  	[ENCLOSURE_STATUS_UNRECOVERABLE] = "unrecoverable",
  	[ENCLOSURE_STATUS_NOT_INSTALLED] = "not installed",
  	[ENCLOSURE_STATUS_UNKNOWN] = "unknown",
  	[ENCLOSURE_STATUS_UNAVAILABLE] = "unavailable",
cc9b2e9f6   James Bottomley   [SCSI] enclosure:...
451
  	[ENCLOSURE_STATUS_MAX] = NULL,
d569d5bb3   James Bottomley   [SCSI] enclosure:...
452
  };
367ef846b   Arvind Yadav   misc: enclosure: ...
453
  static const char *const enclosure_type[] = {
d569d5bb3   James Bottomley   [SCSI] enclosure:...
454
455
456
  	[ENCLOSURE_COMPONENT_DEVICE] = "device",
  	[ENCLOSURE_COMPONENT_ARRAY_DEVICE] = "array device",
  };
ee959b00c   Tony Jones   SCSI: convert str...
457
458
  static ssize_t get_component_fault(struct device *cdev,
  				   struct device_attribute *attr, char *buf)
d569d5bb3   James Bottomley   [SCSI] enclosure:...
459
460
461
462
463
464
465
466
467
  {
  	struct enclosure_device *edev = to_enclosure_device(cdev->parent);
  	struct enclosure_component *ecomp = to_enclosure_component(cdev);
  
  	if (edev->cb->get_fault)
  		edev->cb->get_fault(edev, ecomp);
  	return snprintf(buf, 40, "%d
  ", ecomp->fault);
  }
ee959b00c   Tony Jones   SCSI: convert str...
468
469
470
  static ssize_t set_component_fault(struct device *cdev,
  				   struct device_attribute *attr,
  				   const char *buf, size_t count)
d569d5bb3   James Bottomley   [SCSI] enclosure:...
471
472
473
474
475
476
477
478
479
  {
  	struct enclosure_device *edev = to_enclosure_device(cdev->parent);
  	struct enclosure_component *ecomp = to_enclosure_component(cdev);
  	int val = simple_strtoul(buf, NULL, 0);
  
  	if (edev->cb->set_fault)
  		edev->cb->set_fault(edev, ecomp, val);
  	return count;
  }
ee959b00c   Tony Jones   SCSI: convert str...
480
481
  static ssize_t get_component_status(struct device *cdev,
  				    struct device_attribute *attr,char *buf)
d569d5bb3   James Bottomley   [SCSI] enclosure:...
482
483
484
485
486
487
488
489
490
  {
  	struct enclosure_device *edev = to_enclosure_device(cdev->parent);
  	struct enclosure_component *ecomp = to_enclosure_component(cdev);
  
  	if (edev->cb->get_status)
  		edev->cb->get_status(edev, ecomp);
  	return snprintf(buf, 40, "%s
  ", enclosure_status[ecomp->status]);
  }
ee959b00c   Tony Jones   SCSI: convert str...
491
492
493
  static ssize_t set_component_status(struct device *cdev,
  				    struct device_attribute *attr,
  				    const char *buf, size_t count)
d569d5bb3   James Bottomley   [SCSI] enclosure:...
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
  {
  	struct enclosure_device *edev = to_enclosure_device(cdev->parent);
  	struct enclosure_component *ecomp = to_enclosure_component(cdev);
  	int i;
  
  	for (i = 0; enclosure_status[i]; i++) {
  		if (strncmp(buf, enclosure_status[i],
  			    strlen(enclosure_status[i])) == 0 &&
  		    (buf[strlen(enclosure_status[i])] == '
  ' ||
  		     buf[strlen(enclosure_status[i])] == '\0'))
  			break;
  	}
  
  	if (enclosure_status[i] && edev->cb->set_status) {
  		edev->cb->set_status(edev, ecomp, i);
  		return count;
  	} else
  		return -EINVAL;
  }
ee959b00c   Tony Jones   SCSI: convert str...
514
515
  static ssize_t get_component_active(struct device *cdev,
  				    struct device_attribute *attr, char *buf)
d569d5bb3   James Bottomley   [SCSI] enclosure:...
516
517
518
519
520
521
522
523
524
  {
  	struct enclosure_device *edev = to_enclosure_device(cdev->parent);
  	struct enclosure_component *ecomp = to_enclosure_component(cdev);
  
  	if (edev->cb->get_active)
  		edev->cb->get_active(edev, ecomp);
  	return snprintf(buf, 40, "%d
  ", ecomp->active);
  }
ee959b00c   Tony Jones   SCSI: convert str...
525
526
527
  static ssize_t set_component_active(struct device *cdev,
  				    struct device_attribute *attr,
  				    const char *buf, size_t count)
d569d5bb3   James Bottomley   [SCSI] enclosure:...
528
529
530
531
532
533
534
535
536
  {
  	struct enclosure_device *edev = to_enclosure_device(cdev->parent);
  	struct enclosure_component *ecomp = to_enclosure_component(cdev);
  	int val = simple_strtoul(buf, NULL, 0);
  
  	if (edev->cb->set_active)
  		edev->cb->set_active(edev, ecomp, val);
  	return count;
  }
ee959b00c   Tony Jones   SCSI: convert str...
537
538
  static ssize_t get_component_locate(struct device *cdev,
  				    struct device_attribute *attr, char *buf)
d569d5bb3   James Bottomley   [SCSI] enclosure:...
539
540
541
542
543
544
545
546
547
  {
  	struct enclosure_device *edev = to_enclosure_device(cdev->parent);
  	struct enclosure_component *ecomp = to_enclosure_component(cdev);
  
  	if (edev->cb->get_locate)
  		edev->cb->get_locate(edev, ecomp);
  	return snprintf(buf, 40, "%d
  ", ecomp->locate);
  }
ee959b00c   Tony Jones   SCSI: convert str...
548
549
550
  static ssize_t set_component_locate(struct device *cdev,
  				    struct device_attribute *attr,
  				    const char *buf, size_t count)
d569d5bb3   James Bottomley   [SCSI] enclosure:...
551
552
553
554
555
556
557
558
559
  {
  	struct enclosure_device *edev = to_enclosure_device(cdev->parent);
  	struct enclosure_component *ecomp = to_enclosure_component(cdev);
  	int val = simple_strtoul(buf, NULL, 0);
  
  	if (edev->cb->set_locate)
  		edev->cb->set_locate(edev, ecomp, val);
  	return count;
  }
08024885a   Song Liu   ses: Add power_st...
560
561
562
563
564
565
566
567
568
  static ssize_t get_component_power_status(struct device *cdev,
  					  struct device_attribute *attr,
  					  char *buf)
  {
  	struct enclosure_device *edev = to_enclosure_device(cdev->parent);
  	struct enclosure_component *ecomp = to_enclosure_component(cdev);
  
  	if (edev->cb->get_power_status)
  		edev->cb->get_power_status(edev, ecomp);
75106523f   Mauricio Faria de Oliveira   scsi: ses: don't ...
569
570
571
572
  
  	/* If still uninitialized, the callback failed or does not exist. */
  	if (ecomp->power_status == -1)
  		return (edev->cb->get_power_status) ? -EIO : -ENOTTY;
08024885a   Song Liu   ses: Add power_st...
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
  	return snprintf(buf, 40, "%s
  ", ecomp->power_status ? "on" : "off");
  }
  
  static ssize_t set_component_power_status(struct device *cdev,
  					  struct device_attribute *attr,
  					  const char *buf, size_t count)
  {
  	struct enclosure_device *edev = to_enclosure_device(cdev->parent);
  	struct enclosure_component *ecomp = to_enclosure_component(cdev);
  	int val;
  
  	if (strncmp(buf, "on", 2) == 0 &&
  	    (buf[2] == '
  ' || buf[2] == '\0'))
  		val = 1;
  	else if (strncmp(buf, "off", 3) == 0 &&
  	    (buf[3] == '
  ' || buf[3] == '\0'))
  		val = 0;
  	else
  		return -EINVAL;
  
  	if (edev->cb->set_power_status)
  		edev->cb->set_power_status(edev, ecomp, val);
  	return count;
  }
ee959b00c   Tony Jones   SCSI: convert str...
600
601
  static ssize_t get_component_type(struct device *cdev,
  				  struct device_attribute *attr, char *buf)
d569d5bb3   James Bottomley   [SCSI] enclosure:...
602
603
604
605
606
607
  {
  	struct enclosure_component *ecomp = to_enclosure_component(cdev);
  
  	return snprintf(buf, 40, "%s
  ", enclosure_type[ecomp->type]);
  }
921ce7f57   Dan Williams   ses: add reliable...
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
  static ssize_t get_component_slot(struct device *cdev,
  				  struct device_attribute *attr, char *buf)
  {
  	struct enclosure_component *ecomp = to_enclosure_component(cdev);
  	int slot;
  
  	/* if the enclosure does not override then use 'number' as a stand-in */
  	if (ecomp->slot >= 0)
  		slot = ecomp->slot;
  	else
  		slot = ecomp->number;
  
  	return snprintf(buf, 40, "%d
  ", slot);
  }
d569d5bb3   James Bottomley   [SCSI] enclosure:...
623

cb6b7f406   James Bottomley   [SCSI] ses: fix u...
624
625
626
627
628
629
630
631
  static DEVICE_ATTR(fault, S_IRUGO | S_IWUSR, get_component_fault,
  		    set_component_fault);
  static DEVICE_ATTR(status, S_IRUGO | S_IWUSR, get_component_status,
  		   set_component_status);
  static DEVICE_ATTR(active, S_IRUGO | S_IWUSR, get_component_active,
  		   set_component_active);
  static DEVICE_ATTR(locate, S_IRUGO | S_IWUSR, get_component_locate,
  		   set_component_locate);
08024885a   Song Liu   ses: Add power_st...
632
633
  static DEVICE_ATTR(power_status, S_IRUGO | S_IWUSR, get_component_power_status,
  		   set_component_power_status);
cb6b7f406   James Bottomley   [SCSI] ses: fix u...
634
  static DEVICE_ATTR(type, S_IRUGO, get_component_type, NULL);
921ce7f57   Dan Williams   ses: add reliable...
635
  static DEVICE_ATTR(slot, S_IRUGO, get_component_slot, NULL);
cb6b7f406   James Bottomley   [SCSI] ses: fix u...
636
637
638
639
640
641
  
  static struct attribute *enclosure_component_attrs[] = {
  	&dev_attr_fault.attr,
  	&dev_attr_status.attr,
  	&dev_attr_active.attr,
  	&dev_attr_locate.attr,
08024885a   Song Liu   ses: Add power_st...
642
  	&dev_attr_power_status.attr,
cb6b7f406   James Bottomley   [SCSI] ses: fix u...
643
  	&dev_attr_type.attr,
921ce7f57   Dan Williams   ses: add reliable...
644
  	&dev_attr_slot.attr,
cb6b7f406   James Bottomley   [SCSI] ses: fix u...
645
  	NULL
d569d5bb3   James Bottomley   [SCSI] enclosure:...
646
  };
899826f16   Greg Kroah-Hartman   enclosure: conver...
647
  ATTRIBUTE_GROUPS(enclosure_component);
d569d5bb3   James Bottomley   [SCSI] enclosure:...
648
649
650
  
  static int __init enclosure_init(void)
  {
750b54deb   Arvind Yadav   misc: enclosure: ...
651
  	return class_register(&enclosure_class);
d569d5bb3   James Bottomley   [SCSI] enclosure:...
652
653
654
655
  }
  
  static void __exit enclosure_exit(void)
  {
d569d5bb3   James Bottomley   [SCSI] enclosure:...
656
657
658
659
660
661
662
663
664
  	class_unregister(&enclosure_class);
  }
  
  module_init(enclosure_init);
  module_exit(enclosure_exit);
  
  MODULE_AUTHOR("James Bottomley");
  MODULE_DESCRIPTION("Enclosure Services");
  MODULE_LICENSE("GPL v2");