1*82c29810SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2d569d5bbSJames Bottomley /* 3d569d5bbSJames Bottomley * Enclosure Services 4d569d5bbSJames Bottomley * 5d569d5bbSJames Bottomley * Copyright (C) 2008 James Bottomley <James.Bottomley@HansenPartnership.com> 6d569d5bbSJames Bottomley * 7d569d5bbSJames Bottomley **----------------------------------------------------------------------------- 8d569d5bbSJames Bottomley ** 9d569d5bbSJames Bottomley ** 10d569d5bbSJames Bottomley **----------------------------------------------------------------------------- 11d569d5bbSJames Bottomley */ 12d569d5bbSJames Bottomley #include <linux/device.h> 13d569d5bbSJames Bottomley #include <linux/enclosure.h> 14d569d5bbSJames Bottomley #include <linux/err.h> 15d569d5bbSJames Bottomley #include <linux/list.h> 16d569d5bbSJames Bottomley #include <linux/kernel.h> 17d569d5bbSJames Bottomley #include <linux/module.h> 18d569d5bbSJames Bottomley #include <linux/mutex.h> 195a0e3ad6STejun Heo #include <linux/slab.h> 20d569d5bbSJames Bottomley 21d569d5bbSJames Bottomley static LIST_HEAD(container_list); 22d569d5bbSJames Bottomley static DEFINE_MUTEX(container_list_lock); 23d569d5bbSJames Bottomley static struct class enclosure_class; 24d569d5bbSJames Bottomley 25d569d5bbSJames Bottomley /** 26163f52b6SJames Bottomley * enclosure_find - find an enclosure given a parent device 27163f52b6SJames Bottomley * @dev: the parent to match against 28163f52b6SJames Bottomley * @start: Optional enclosure device to start from (NULL if none) 29d569d5bbSJames Bottomley * 30163f52b6SJames Bottomley * Looks through the list of registered enclosures to find all those 31163f52b6SJames Bottomley * with @dev as a parent. Returns NULL if no enclosure is 32163f52b6SJames Bottomley * found. @start can be used as a starting point to obtain multiple 33163f52b6SJames Bottomley * enclosures per parent (should begin with NULL and then be set to 34163f52b6SJames Bottomley * each returned enclosure device). Obtains a reference to the 35163f52b6SJames Bottomley * enclosure class device which must be released with device_put(). 36163f52b6SJames Bottomley * If @start is not NULL, a reference must be taken on it which is 37163f52b6SJames Bottomley * released before returning (this allows a loop through all 38163f52b6SJames Bottomley * enclosures to exit with only the reference on the enclosure of 39163f52b6SJames Bottomley * interest held). Note that the @dev may correspond to the actual 40163f52b6SJames Bottomley * device housing the enclosure, in which case no iteration via @start 41163f52b6SJames Bottomley * is required. 42d569d5bbSJames Bottomley */ 43163f52b6SJames Bottomley struct enclosure_device *enclosure_find(struct device *dev, 44163f52b6SJames Bottomley struct enclosure_device *start) 45d569d5bbSJames Bottomley { 46ee959b00STony Jones struct enclosure_device *edev; 47d569d5bbSJames Bottomley 48d569d5bbSJames Bottomley mutex_lock(&container_list_lock); 49163f52b6SJames Bottomley edev = list_prepare_entry(start, &container_list, node); 50163f52b6SJames Bottomley if (start) 51163f52b6SJames Bottomley put_device(&start->edev); 52163f52b6SJames Bottomley 53163f52b6SJames Bottomley list_for_each_entry_continue(edev, &container_list, node) { 54163f52b6SJames Bottomley struct device *parent = edev->edev.parent; 55163f52b6SJames Bottomley /* parent might not be immediate, so iterate up to 56163f52b6SJames Bottomley * the root of the tree if necessary */ 57163f52b6SJames Bottomley while (parent) { 58163f52b6SJames Bottomley if (parent == dev) { 59ee959b00STony Jones get_device(&edev->edev); 60d569d5bbSJames Bottomley mutex_unlock(&container_list_lock); 61d569d5bbSJames Bottomley return edev; 62d569d5bbSJames Bottomley } 63163f52b6SJames Bottomley parent = parent->parent; 64163f52b6SJames Bottomley } 65d569d5bbSJames Bottomley } 66d569d5bbSJames Bottomley mutex_unlock(&container_list_lock); 67d569d5bbSJames Bottomley 68d569d5bbSJames Bottomley return NULL; 69d569d5bbSJames Bottomley } 70d569d5bbSJames Bottomley EXPORT_SYMBOL_GPL(enclosure_find); 71d569d5bbSJames Bottomley 72d569d5bbSJames Bottomley /** 73d569d5bbSJames Bottomley * enclosure_for_each_device - calls a function for each enclosure 74d569d5bbSJames Bottomley * @fn: the function to call 75d569d5bbSJames Bottomley * @data: the data to pass to each call 76d569d5bbSJames Bottomley * 77d569d5bbSJames Bottomley * Loops over all the enclosures calling the function. 78d569d5bbSJames Bottomley * 79d569d5bbSJames Bottomley * Note, this function uses a mutex which will be held across calls to 80d569d5bbSJames Bottomley * @fn, so it must have non atomic context, and @fn may (although it 81d569d5bbSJames Bottomley * should not) sleep or otherwise cause the mutex to be held for 82d569d5bbSJames Bottomley * indefinite periods 83d569d5bbSJames Bottomley */ 84d569d5bbSJames Bottomley int enclosure_for_each_device(int (*fn)(struct enclosure_device *, void *), 85d569d5bbSJames Bottomley void *data) 86d569d5bbSJames Bottomley { 87d569d5bbSJames Bottomley int error = 0; 88d569d5bbSJames Bottomley struct enclosure_device *edev; 89d569d5bbSJames Bottomley 90d569d5bbSJames Bottomley mutex_lock(&container_list_lock); 91d569d5bbSJames Bottomley list_for_each_entry(edev, &container_list, node) { 92d569d5bbSJames Bottomley error = fn(edev, data); 93d569d5bbSJames Bottomley if (error) 94d569d5bbSJames Bottomley break; 95d569d5bbSJames Bottomley } 96d569d5bbSJames Bottomley mutex_unlock(&container_list_lock); 97d569d5bbSJames Bottomley 98d569d5bbSJames Bottomley return error; 99d569d5bbSJames Bottomley } 100d569d5bbSJames Bottomley EXPORT_SYMBOL_GPL(enclosure_for_each_device); 101d569d5bbSJames Bottomley 102d569d5bbSJames Bottomley /** 103d569d5bbSJames Bottomley * enclosure_register - register device as an enclosure 104d569d5bbSJames Bottomley * 105d569d5bbSJames Bottomley * @dev: device containing the enclosure 106d569d5bbSJames Bottomley * @components: number of components in the enclosure 107d569d5bbSJames Bottomley * 108d569d5bbSJames Bottomley * This sets up the device for being an enclosure. Note that @dev does 109d569d5bbSJames Bottomley * not have to be a dedicated enclosure device. It may be some other type 110d569d5bbSJames Bottomley * of device that additionally responds to enclosure services 111d569d5bbSJames Bottomley */ 112d569d5bbSJames Bottomley struct enclosure_device * 113d569d5bbSJames Bottomley enclosure_register(struct device *dev, const char *name, int components, 114d569d5bbSJames Bottomley struct enclosure_component_callbacks *cb) 115d569d5bbSJames Bottomley { 116d569d5bbSJames Bottomley struct enclosure_device *edev = 117e3575c12SGustavo A. R. Silva kzalloc(struct_size(edev, component, components), GFP_KERNEL); 118d569d5bbSJames Bottomley int err, i; 119d569d5bbSJames Bottomley 120d569d5bbSJames Bottomley BUG_ON(!cb); 121d569d5bbSJames Bottomley 122d569d5bbSJames Bottomley if (!edev) 123d569d5bbSJames Bottomley return ERR_PTR(-ENOMEM); 124d569d5bbSJames Bottomley 125d569d5bbSJames Bottomley edev->components = components; 126d569d5bbSJames Bottomley 127ee959b00STony Jones edev->edev.class = &enclosure_class; 128ee959b00STony Jones edev->edev.parent = get_device(dev); 129d569d5bbSJames Bottomley edev->cb = cb; 1305e43754fSYinghai Lu dev_set_name(&edev->edev, "%s", name); 131ee959b00STony Jones err = device_register(&edev->edev); 132d569d5bbSJames Bottomley if (err) 133d569d5bbSJames Bottomley goto err; 134d569d5bbSJames Bottomley 135921ce7f5SDan Williams for (i = 0; i < components; i++) { 136d569d5bbSJames Bottomley edev->component[i].number = -1; 137921ce7f5SDan Williams edev->component[i].slot = -1; 13875106523SMauricio Faria de Oliveira edev->component[i].power_status = -1; 139921ce7f5SDan Williams } 140d569d5bbSJames Bottomley 141d569d5bbSJames Bottomley mutex_lock(&container_list_lock); 142d569d5bbSJames Bottomley list_add_tail(&edev->node, &container_list); 143d569d5bbSJames Bottomley mutex_unlock(&container_list_lock); 144d569d5bbSJames Bottomley 145d569d5bbSJames Bottomley return edev; 146d569d5bbSJames Bottomley 147d569d5bbSJames Bottomley err: 148ee959b00STony Jones put_device(edev->edev.parent); 149d569d5bbSJames Bottomley kfree(edev); 150d569d5bbSJames Bottomley return ERR_PTR(err); 151d569d5bbSJames Bottomley } 152d569d5bbSJames Bottomley EXPORT_SYMBOL_GPL(enclosure_register); 153d569d5bbSJames Bottomley 154d569d5bbSJames Bottomley static struct enclosure_component_callbacks enclosure_null_callbacks; 155d569d5bbSJames Bottomley 156d569d5bbSJames Bottomley /** 157d569d5bbSJames Bottomley * enclosure_unregister - remove an enclosure 158d569d5bbSJames Bottomley * 159d569d5bbSJames Bottomley * @edev: the registered enclosure to remove; 160d569d5bbSJames Bottomley */ 161d569d5bbSJames Bottomley void enclosure_unregister(struct enclosure_device *edev) 162d569d5bbSJames Bottomley { 163d569d5bbSJames Bottomley int i; 164d569d5bbSJames Bottomley 165d569d5bbSJames Bottomley mutex_lock(&container_list_lock); 166d569d5bbSJames Bottomley list_del(&edev->node); 167d569d5bbSJames Bottomley mutex_unlock(&container_list_lock); 168d569d5bbSJames Bottomley 169d569d5bbSJames Bottomley for (i = 0; i < edev->components; i++) 170d569d5bbSJames Bottomley if (edev->component[i].number != -1) 171ee959b00STony Jones device_unregister(&edev->component[i].cdev); 172d569d5bbSJames Bottomley 173d569d5bbSJames Bottomley /* prevent any callbacks into service user */ 174d569d5bbSJames Bottomley edev->cb = &enclosure_null_callbacks; 175ee959b00STony Jones device_unregister(&edev->edev); 176d569d5bbSJames Bottomley } 177d569d5bbSJames Bottomley EXPORT_SYMBOL_GPL(enclosure_unregister); 178d569d5bbSJames Bottomley 179cb6b7f40SJames Bottomley #define ENCLOSURE_NAME_SIZE 64 180d2fd76e6SMarkus Stockhausen #define COMPONENT_NAME_SIZE 64 181cb6b7f40SJames Bottomley 182cb6b7f40SJames Bottomley static void enclosure_link_name(struct enclosure_component *cdev, char *name) 183cb6b7f40SJames Bottomley { 184cb6b7f40SJames Bottomley strcpy(name, "enclosure_device:"); 18571610f55SKay Sievers strcat(name, dev_name(&cdev->cdev)); 186cb6b7f40SJames Bottomley } 187cb6b7f40SJames Bottomley 188cb6b7f40SJames Bottomley static void enclosure_remove_links(struct enclosure_component *cdev) 189cb6b7f40SJames Bottomley { 190cb6b7f40SJames Bottomley char name[ENCLOSURE_NAME_SIZE]; 191cb6b7f40SJames Bottomley 19211e52a69SJames Bottomley enclosure_link_name(cdev, name); 19311e52a69SJames Bottomley 194a1470c7bSJames Bottomley /* 195a1470c7bSJames Bottomley * In odd circumstances, like multipath devices, something else may 196a1470c7bSJames Bottomley * already have removed the links, so check for this condition first. 197a1470c7bSJames Bottomley */ 19811e52a69SJames Bottomley if (cdev->dev->kobj.sd) 199cb6b7f40SJames Bottomley sysfs_remove_link(&cdev->dev->kobj, name); 20011e52a69SJames Bottomley 20111e52a69SJames Bottomley if (cdev->cdev.kobj.sd) 202cb6b7f40SJames Bottomley sysfs_remove_link(&cdev->cdev.kobj, "device"); 203cb6b7f40SJames Bottomley } 204cb6b7f40SJames Bottomley 205cb6b7f40SJames Bottomley static int enclosure_add_links(struct enclosure_component *cdev) 206cb6b7f40SJames Bottomley { 207cb6b7f40SJames Bottomley int error; 208cb6b7f40SJames Bottomley char name[ENCLOSURE_NAME_SIZE]; 209cb6b7f40SJames Bottomley 210cb6b7f40SJames Bottomley error = sysfs_create_link(&cdev->cdev.kobj, &cdev->dev->kobj, "device"); 211cb6b7f40SJames Bottomley if (error) 212cb6b7f40SJames Bottomley return error; 213cb6b7f40SJames Bottomley 214cb6b7f40SJames Bottomley enclosure_link_name(cdev, name); 215cb6b7f40SJames Bottomley error = sysfs_create_link(&cdev->dev->kobj, &cdev->cdev.kobj, name); 216cb6b7f40SJames Bottomley if (error) 217cb6b7f40SJames Bottomley sysfs_remove_link(&cdev->cdev.kobj, "device"); 218cb6b7f40SJames Bottomley 219cb6b7f40SJames Bottomley return error; 220cb6b7f40SJames Bottomley } 221cb6b7f40SJames Bottomley 222ee959b00STony Jones static void enclosure_release(struct device *cdev) 223d569d5bbSJames Bottomley { 224d569d5bbSJames Bottomley struct enclosure_device *edev = to_enclosure_device(cdev); 225d569d5bbSJames Bottomley 226ee959b00STony Jones put_device(cdev->parent); 227d569d5bbSJames Bottomley kfree(edev); 228d569d5bbSJames Bottomley } 229d569d5bbSJames Bottomley 230ee959b00STony Jones static void enclosure_component_release(struct device *dev) 231d569d5bbSJames Bottomley { 232ee959b00STony Jones struct enclosure_component *cdev = to_enclosure_component(dev); 233ee959b00STony Jones 234cb6b7f40SJames Bottomley if (cdev->dev) { 235cb6b7f40SJames Bottomley enclosure_remove_links(cdev); 236d569d5bbSJames Bottomley put_device(cdev->dev); 237cb6b7f40SJames Bottomley } 238ee959b00STony Jones put_device(dev->parent); 239d569d5bbSJames Bottomley } 240d569d5bbSJames Bottomley 241d2fd76e6SMarkus Stockhausen static struct enclosure_component * 242d2fd76e6SMarkus Stockhausen enclosure_component_find_by_name(struct enclosure_device *edev, 243d2fd76e6SMarkus Stockhausen const char *name) 244d2fd76e6SMarkus Stockhausen { 245d2fd76e6SMarkus Stockhausen int i; 246d2fd76e6SMarkus Stockhausen const char *cname; 247d2fd76e6SMarkus Stockhausen struct enclosure_component *ecomp; 248d2fd76e6SMarkus Stockhausen 249d2fd76e6SMarkus Stockhausen if (!edev || !name || !name[0]) 250d2fd76e6SMarkus Stockhausen return NULL; 251d2fd76e6SMarkus Stockhausen 252d2fd76e6SMarkus Stockhausen for (i = 0; i < edev->components; i++) { 253d2fd76e6SMarkus Stockhausen ecomp = &edev->component[i]; 254d2fd76e6SMarkus Stockhausen cname = dev_name(&ecomp->cdev); 255d2fd76e6SMarkus Stockhausen if (ecomp->number != -1 && 256d2fd76e6SMarkus Stockhausen cname && cname[0] && 257d2fd76e6SMarkus Stockhausen !strcmp(cname, name)) 258d2fd76e6SMarkus Stockhausen return ecomp; 259d2fd76e6SMarkus Stockhausen } 260d2fd76e6SMarkus Stockhausen 261d2fd76e6SMarkus Stockhausen return NULL; 262d2fd76e6SMarkus Stockhausen } 263d2fd76e6SMarkus Stockhausen 264899826f1SGreg Kroah-Hartman static const struct attribute_group *enclosure_component_groups[]; 265cb6b7f40SJames Bottomley 266d569d5bbSJames Bottomley /** 267ed09dcc8SDan Williams * enclosure_component_alloc - prepare a new enclosure component 268d569d5bbSJames Bottomley * @edev: the enclosure to add the component 269d569d5bbSJames Bottomley * @num: the device number 270d569d5bbSJames Bottomley * @type: the type of component being added 271d569d5bbSJames Bottomley * @name: an optional name to appear in sysfs (leave NULL if none) 272d569d5bbSJames Bottomley * 273ed09dcc8SDan Williams * The name is optional for enclosures that give their components a unique 274ed09dcc8SDan Williams * name. If not, leave the field NULL and a name will be assigned. 275d569d5bbSJames Bottomley * 276d569d5bbSJames Bottomley * Returns a pointer to the enclosure component or an error. 277d569d5bbSJames Bottomley */ 278d569d5bbSJames Bottomley struct enclosure_component * 279ed09dcc8SDan Williams enclosure_component_alloc(struct enclosure_device *edev, 280d569d5bbSJames Bottomley unsigned int number, 281d569d5bbSJames Bottomley enum enclosure_component_type type, 282d569d5bbSJames Bottomley const char *name) 283d569d5bbSJames Bottomley { 284d569d5bbSJames Bottomley struct enclosure_component *ecomp; 285ee959b00STony Jones struct device *cdev; 286ed09dcc8SDan Williams int i; 287d2fd76e6SMarkus Stockhausen char newname[COMPONENT_NAME_SIZE]; 288d569d5bbSJames Bottomley 289d569d5bbSJames Bottomley if (number >= edev->components) 290d569d5bbSJames Bottomley return ERR_PTR(-EINVAL); 291d569d5bbSJames Bottomley 292d569d5bbSJames Bottomley ecomp = &edev->component[number]; 293d569d5bbSJames Bottomley 294d569d5bbSJames Bottomley if (ecomp->number != -1) 295d569d5bbSJames Bottomley return ERR_PTR(-EINVAL); 296d569d5bbSJames Bottomley 297d569d5bbSJames Bottomley ecomp->type = type; 298d569d5bbSJames Bottomley ecomp->number = number; 299d569d5bbSJames Bottomley cdev = &ecomp->cdev; 300ee959b00STony Jones cdev->parent = get_device(&edev->edev); 301d2fd76e6SMarkus Stockhausen 302d2fd76e6SMarkus Stockhausen if (name && name[0]) { 303d2fd76e6SMarkus Stockhausen /* Some hardware (e.g. enclosure in RX300 S6) has components 304d2fd76e6SMarkus Stockhausen * with non unique names. Registering duplicates in sysfs 305d2fd76e6SMarkus Stockhausen * will lead to warnings during bootup. So make the names 306d2fd76e6SMarkus Stockhausen * unique by appending consecutive numbers -1, -2, ... */ 307d2fd76e6SMarkus Stockhausen i = 1; 308d2fd76e6SMarkus Stockhausen snprintf(newname, COMPONENT_NAME_SIZE, 309d2fd76e6SMarkus Stockhausen "%s", name); 310d2fd76e6SMarkus Stockhausen while (enclosure_component_find_by_name(edev, newname)) 311d2fd76e6SMarkus Stockhausen snprintf(newname, COMPONENT_NAME_SIZE, 312d2fd76e6SMarkus Stockhausen "%s-%i", name, i++); 313d2fd76e6SMarkus Stockhausen dev_set_name(cdev, "%s", newname); 314d2fd76e6SMarkus Stockhausen } else 31571610f55SKay Sievers dev_set_name(cdev, "%u", number); 316d569d5bbSJames Bottomley 317cb6b7f40SJames Bottomley cdev->release = enclosure_component_release; 318899826f1SGreg Kroah-Hartman cdev->groups = enclosure_component_groups; 319cb6b7f40SJames Bottomley 320ed09dcc8SDan Williams return ecomp; 321ed09dcc8SDan Williams } 322ed09dcc8SDan Williams EXPORT_SYMBOL_GPL(enclosure_component_alloc); 323ed09dcc8SDan Williams 324ed09dcc8SDan Williams /** 325ed09dcc8SDan Williams * enclosure_component_register - publishes an initialized enclosure component 326ed09dcc8SDan Williams * @ecomp: component to add 327ed09dcc8SDan Williams * 328ed09dcc8SDan Williams * Returns 0 on successful registration, releases the component otherwise 329ed09dcc8SDan Williams */ 330ed09dcc8SDan Williams int enclosure_component_register(struct enclosure_component *ecomp) 331ed09dcc8SDan Williams { 332ed09dcc8SDan Williams struct device *cdev; 333ed09dcc8SDan Williams int err; 334ed09dcc8SDan Williams 335ed09dcc8SDan Williams cdev = &ecomp->cdev; 336ee959b00STony Jones err = device_register(cdev); 337a91c1be2SJames Bottomley if (err) { 338a91c1be2SJames Bottomley ecomp->number = -1; 339a91c1be2SJames Bottomley put_device(cdev); 340ed09dcc8SDan Williams return err; 341a91c1be2SJames Bottomley } 342d569d5bbSJames Bottomley 343ed09dcc8SDan Williams return 0; 344d569d5bbSJames Bottomley } 345d569d5bbSJames Bottomley EXPORT_SYMBOL_GPL(enclosure_component_register); 346d569d5bbSJames Bottomley 347d569d5bbSJames Bottomley /** 348d569d5bbSJames Bottomley * enclosure_add_device - add a device as being part of an enclosure 349d569d5bbSJames Bottomley * @edev: the enclosure device being added to. 350d569d5bbSJames Bottomley * @num: the number of the component 351d569d5bbSJames Bottomley * @dev: the device being added 352d569d5bbSJames Bottomley * 353d569d5bbSJames Bottomley * Declares a real device to reside in slot (or identifier) @num of an 354d569d5bbSJames Bottomley * enclosure. This will cause the relevant sysfs links to appear. 355d569d5bbSJames Bottomley * This function may also be used to change a device associated with 356d569d5bbSJames Bottomley * an enclosure without having to call enclosure_remove_device() in 357d569d5bbSJames Bottomley * between. 358d569d5bbSJames Bottomley * 359d569d5bbSJames Bottomley * Returns zero on success or an error. 360d569d5bbSJames Bottomley */ 361d569d5bbSJames Bottomley int enclosure_add_device(struct enclosure_device *edev, int component, 362d569d5bbSJames Bottomley struct device *dev) 363d569d5bbSJames Bottomley { 364ee959b00STony Jones struct enclosure_component *cdev; 36562e62ffdSMaurizio Lombardi int err; 366d569d5bbSJames Bottomley 367d569d5bbSJames Bottomley if (!edev || component >= edev->components) 368d569d5bbSJames Bottomley return -EINVAL; 369d569d5bbSJames Bottomley 370ee959b00STony Jones cdev = &edev->component[component]; 371d569d5bbSJames Bottomley 37221fab1d0SJames Bottomley if (cdev->dev == dev) 37321fab1d0SJames Bottomley return -EEXIST; 37421fab1d0SJames Bottomley 37562e62ffdSMaurizio Lombardi if (cdev->dev) { 376cb6b7f40SJames Bottomley enclosure_remove_links(cdev); 377d569d5bbSJames Bottomley put_device(cdev->dev); 37862e62ffdSMaurizio Lombardi } 379d569d5bbSJames Bottomley cdev->dev = get_device(dev); 38062e62ffdSMaurizio Lombardi err = enclosure_add_links(cdev); 38162e62ffdSMaurizio Lombardi if (err) { 38262e62ffdSMaurizio Lombardi put_device(cdev->dev); 38362e62ffdSMaurizio Lombardi cdev->dev = NULL; 38462e62ffdSMaurizio Lombardi } 38562e62ffdSMaurizio Lombardi return err; 386d569d5bbSJames Bottomley } 387d569d5bbSJames Bottomley EXPORT_SYMBOL_GPL(enclosure_add_device); 388d569d5bbSJames Bottomley 389d569d5bbSJames Bottomley /** 390d569d5bbSJames Bottomley * enclosure_remove_device - remove a device from an enclosure 391d569d5bbSJames Bottomley * @edev: the enclosure device 392d569d5bbSJames Bottomley * @num: the number of the component to remove 393d569d5bbSJames Bottomley * 394d569d5bbSJames Bottomley * Returns zero on success or an error. 395d569d5bbSJames Bottomley * 396d569d5bbSJames Bottomley */ 39743d8eb9cSJames Bottomley int enclosure_remove_device(struct enclosure_device *edev, struct device *dev) 398d569d5bbSJames Bottomley { 399ee959b00STony Jones struct enclosure_component *cdev; 40043d8eb9cSJames Bottomley int i; 401d569d5bbSJames Bottomley 40243d8eb9cSJames Bottomley if (!edev || !dev) 403d569d5bbSJames Bottomley return -EINVAL; 404d569d5bbSJames Bottomley 40543d8eb9cSJames Bottomley for (i = 0; i < edev->components; i++) { 40643d8eb9cSJames Bottomley cdev = &edev->component[i]; 40743d8eb9cSJames Bottomley if (cdev->dev == dev) { 40843d8eb9cSJames Bottomley enclosure_remove_links(cdev); 409ee959b00STony Jones device_del(&cdev->cdev); 41043d8eb9cSJames Bottomley put_device(dev); 411d569d5bbSJames Bottomley cdev->dev = NULL; 412ee959b00STony Jones return device_add(&cdev->cdev); 413d569d5bbSJames Bottomley } 41443d8eb9cSJames Bottomley } 41543d8eb9cSJames Bottomley return -ENODEV; 41643d8eb9cSJames Bottomley } 417d569d5bbSJames Bottomley EXPORT_SYMBOL_GPL(enclosure_remove_device); 418d569d5bbSJames Bottomley 419d569d5bbSJames Bottomley /* 420d569d5bbSJames Bottomley * sysfs pieces below 421d569d5bbSJames Bottomley */ 422d569d5bbSJames Bottomley 423899826f1SGreg Kroah-Hartman static ssize_t components_show(struct device *cdev, 424899826f1SGreg Kroah-Hartman struct device_attribute *attr, char *buf) 425d569d5bbSJames Bottomley { 426d569d5bbSJames Bottomley struct enclosure_device *edev = to_enclosure_device(cdev); 427d569d5bbSJames Bottomley 428d569d5bbSJames Bottomley return snprintf(buf, 40, "%d\n", edev->components); 429d569d5bbSJames Bottomley } 430899826f1SGreg Kroah-Hartman static DEVICE_ATTR_RO(components); 431d569d5bbSJames Bottomley 432967f7babSDan Williams static ssize_t id_show(struct device *cdev, 433967f7babSDan Williams struct device_attribute *attr, 434967f7babSDan Williams char *buf) 435967f7babSDan Williams { 436967f7babSDan Williams struct enclosure_device *edev = to_enclosure_device(cdev); 437967f7babSDan Williams 438967f7babSDan Williams if (edev->cb->show_id) 439967f7babSDan Williams return edev->cb->show_id(edev, buf); 440967f7babSDan Williams return -EINVAL; 441967f7babSDan Williams } 442967f7babSDan Williams static DEVICE_ATTR_RO(id); 443967f7babSDan Williams 444899826f1SGreg Kroah-Hartman static struct attribute *enclosure_class_attrs[] = { 445899826f1SGreg Kroah-Hartman &dev_attr_components.attr, 446967f7babSDan Williams &dev_attr_id.attr, 447899826f1SGreg Kroah-Hartman NULL, 448d569d5bbSJames Bottomley }; 449899826f1SGreg Kroah-Hartman ATTRIBUTE_GROUPS(enclosure_class); 450d569d5bbSJames Bottomley 451d569d5bbSJames Bottomley static struct class enclosure_class = { 452d569d5bbSJames Bottomley .name = "enclosure", 453d569d5bbSJames Bottomley .owner = THIS_MODULE, 454ee959b00STony Jones .dev_release = enclosure_release, 455899826f1SGreg Kroah-Hartman .dev_groups = enclosure_class_groups, 456d569d5bbSJames Bottomley }; 457d569d5bbSJames Bottomley 458d569d5bbSJames Bottomley static const char *const enclosure_status[] = { 459d569d5bbSJames Bottomley [ENCLOSURE_STATUS_UNSUPPORTED] = "unsupported", 460d569d5bbSJames Bottomley [ENCLOSURE_STATUS_OK] = "OK", 461d569d5bbSJames Bottomley [ENCLOSURE_STATUS_CRITICAL] = "critical", 462d569d5bbSJames Bottomley [ENCLOSURE_STATUS_NON_CRITICAL] = "non-critical", 463d569d5bbSJames Bottomley [ENCLOSURE_STATUS_UNRECOVERABLE] = "unrecoverable", 464d569d5bbSJames Bottomley [ENCLOSURE_STATUS_NOT_INSTALLED] = "not installed", 465d569d5bbSJames Bottomley [ENCLOSURE_STATUS_UNKNOWN] = "unknown", 466d569d5bbSJames Bottomley [ENCLOSURE_STATUS_UNAVAILABLE] = "unavailable", 467cc9b2e9fSJames Bottomley [ENCLOSURE_STATUS_MAX] = NULL, 468d569d5bbSJames Bottomley }; 469d569d5bbSJames Bottomley 470d569d5bbSJames Bottomley static const char *const enclosure_type[] = { 471d569d5bbSJames Bottomley [ENCLOSURE_COMPONENT_DEVICE] = "device", 472d569d5bbSJames Bottomley [ENCLOSURE_COMPONENT_ARRAY_DEVICE] = "array device", 473d569d5bbSJames Bottomley }; 474d569d5bbSJames Bottomley 475ee959b00STony Jones static ssize_t get_component_fault(struct device *cdev, 476ee959b00STony Jones struct device_attribute *attr, char *buf) 477d569d5bbSJames Bottomley { 478d569d5bbSJames Bottomley struct enclosure_device *edev = to_enclosure_device(cdev->parent); 479d569d5bbSJames Bottomley struct enclosure_component *ecomp = to_enclosure_component(cdev); 480d569d5bbSJames Bottomley 481d569d5bbSJames Bottomley if (edev->cb->get_fault) 482d569d5bbSJames Bottomley edev->cb->get_fault(edev, ecomp); 483d569d5bbSJames Bottomley return snprintf(buf, 40, "%d\n", ecomp->fault); 484d569d5bbSJames Bottomley } 485d569d5bbSJames Bottomley 486ee959b00STony Jones static ssize_t set_component_fault(struct device *cdev, 487ee959b00STony Jones struct device_attribute *attr, 488ee959b00STony Jones const char *buf, size_t count) 489d569d5bbSJames Bottomley { 490d569d5bbSJames Bottomley struct enclosure_device *edev = to_enclosure_device(cdev->parent); 491d569d5bbSJames Bottomley struct enclosure_component *ecomp = to_enclosure_component(cdev); 492d569d5bbSJames Bottomley int val = simple_strtoul(buf, NULL, 0); 493d569d5bbSJames Bottomley 494d569d5bbSJames Bottomley if (edev->cb->set_fault) 495d569d5bbSJames Bottomley edev->cb->set_fault(edev, ecomp, val); 496d569d5bbSJames Bottomley return count; 497d569d5bbSJames Bottomley } 498d569d5bbSJames Bottomley 499ee959b00STony Jones static ssize_t get_component_status(struct device *cdev, 500ee959b00STony Jones struct device_attribute *attr,char *buf) 501d569d5bbSJames Bottomley { 502d569d5bbSJames Bottomley struct enclosure_device *edev = to_enclosure_device(cdev->parent); 503d569d5bbSJames Bottomley struct enclosure_component *ecomp = to_enclosure_component(cdev); 504d569d5bbSJames Bottomley 505d569d5bbSJames Bottomley if (edev->cb->get_status) 506d569d5bbSJames Bottomley edev->cb->get_status(edev, ecomp); 507d569d5bbSJames Bottomley return snprintf(buf, 40, "%s\n", enclosure_status[ecomp->status]); 508d569d5bbSJames Bottomley } 509d569d5bbSJames Bottomley 510ee959b00STony Jones static ssize_t set_component_status(struct device *cdev, 511ee959b00STony Jones struct device_attribute *attr, 512ee959b00STony Jones const char *buf, size_t count) 513d569d5bbSJames Bottomley { 514d569d5bbSJames Bottomley struct enclosure_device *edev = to_enclosure_device(cdev->parent); 515d569d5bbSJames Bottomley struct enclosure_component *ecomp = to_enclosure_component(cdev); 516d569d5bbSJames Bottomley int i; 517d569d5bbSJames Bottomley 518d569d5bbSJames Bottomley for (i = 0; enclosure_status[i]; i++) { 519d569d5bbSJames Bottomley if (strncmp(buf, enclosure_status[i], 520d569d5bbSJames Bottomley strlen(enclosure_status[i])) == 0 && 521d569d5bbSJames Bottomley (buf[strlen(enclosure_status[i])] == '\n' || 522d569d5bbSJames Bottomley buf[strlen(enclosure_status[i])] == '\0')) 523d569d5bbSJames Bottomley break; 524d569d5bbSJames Bottomley } 525d569d5bbSJames Bottomley 526d569d5bbSJames Bottomley if (enclosure_status[i] && edev->cb->set_status) { 527d569d5bbSJames Bottomley edev->cb->set_status(edev, ecomp, i); 528d569d5bbSJames Bottomley return count; 529d569d5bbSJames Bottomley } else 530d569d5bbSJames Bottomley return -EINVAL; 531d569d5bbSJames Bottomley } 532d569d5bbSJames Bottomley 533ee959b00STony Jones static ssize_t get_component_active(struct device *cdev, 534ee959b00STony Jones struct device_attribute *attr, char *buf) 535d569d5bbSJames Bottomley { 536d569d5bbSJames Bottomley struct enclosure_device *edev = to_enclosure_device(cdev->parent); 537d569d5bbSJames Bottomley struct enclosure_component *ecomp = to_enclosure_component(cdev); 538d569d5bbSJames Bottomley 539d569d5bbSJames Bottomley if (edev->cb->get_active) 540d569d5bbSJames Bottomley edev->cb->get_active(edev, ecomp); 541d569d5bbSJames Bottomley return snprintf(buf, 40, "%d\n", ecomp->active); 542d569d5bbSJames Bottomley } 543d569d5bbSJames Bottomley 544ee959b00STony Jones static ssize_t set_component_active(struct device *cdev, 545ee959b00STony Jones struct device_attribute *attr, 546ee959b00STony Jones const char *buf, size_t count) 547d569d5bbSJames Bottomley { 548d569d5bbSJames Bottomley struct enclosure_device *edev = to_enclosure_device(cdev->parent); 549d569d5bbSJames Bottomley struct enclosure_component *ecomp = to_enclosure_component(cdev); 550d569d5bbSJames Bottomley int val = simple_strtoul(buf, NULL, 0); 551d569d5bbSJames Bottomley 552d569d5bbSJames Bottomley if (edev->cb->set_active) 553d569d5bbSJames Bottomley edev->cb->set_active(edev, ecomp, val); 554d569d5bbSJames Bottomley return count; 555d569d5bbSJames Bottomley } 556d569d5bbSJames Bottomley 557ee959b00STony Jones static ssize_t get_component_locate(struct device *cdev, 558ee959b00STony Jones struct device_attribute *attr, char *buf) 559d569d5bbSJames Bottomley { 560d569d5bbSJames Bottomley struct enclosure_device *edev = to_enclosure_device(cdev->parent); 561d569d5bbSJames Bottomley struct enclosure_component *ecomp = to_enclosure_component(cdev); 562d569d5bbSJames Bottomley 563d569d5bbSJames Bottomley if (edev->cb->get_locate) 564d569d5bbSJames Bottomley edev->cb->get_locate(edev, ecomp); 565d569d5bbSJames Bottomley return snprintf(buf, 40, "%d\n", ecomp->locate); 566d569d5bbSJames Bottomley } 567d569d5bbSJames Bottomley 568ee959b00STony Jones static ssize_t set_component_locate(struct device *cdev, 569ee959b00STony Jones struct device_attribute *attr, 570ee959b00STony Jones const char *buf, size_t count) 571d569d5bbSJames Bottomley { 572d569d5bbSJames Bottomley struct enclosure_device *edev = to_enclosure_device(cdev->parent); 573d569d5bbSJames Bottomley struct enclosure_component *ecomp = to_enclosure_component(cdev); 574d569d5bbSJames Bottomley int val = simple_strtoul(buf, NULL, 0); 575d569d5bbSJames Bottomley 576d569d5bbSJames Bottomley if (edev->cb->set_locate) 577d569d5bbSJames Bottomley edev->cb->set_locate(edev, ecomp, val); 578d569d5bbSJames Bottomley return count; 579d569d5bbSJames Bottomley } 580d569d5bbSJames Bottomley 58108024885SSong Liu static ssize_t get_component_power_status(struct device *cdev, 58208024885SSong Liu struct device_attribute *attr, 58308024885SSong Liu char *buf) 58408024885SSong Liu { 58508024885SSong Liu struct enclosure_device *edev = to_enclosure_device(cdev->parent); 58608024885SSong Liu struct enclosure_component *ecomp = to_enclosure_component(cdev); 58708024885SSong Liu 58808024885SSong Liu if (edev->cb->get_power_status) 58908024885SSong Liu edev->cb->get_power_status(edev, ecomp); 59075106523SMauricio Faria de Oliveira 59175106523SMauricio Faria de Oliveira /* If still uninitialized, the callback failed or does not exist. */ 59275106523SMauricio Faria de Oliveira if (ecomp->power_status == -1) 59375106523SMauricio Faria de Oliveira return (edev->cb->get_power_status) ? -EIO : -ENOTTY; 59475106523SMauricio Faria de Oliveira 59508024885SSong Liu return snprintf(buf, 40, "%s\n", ecomp->power_status ? "on" : "off"); 59608024885SSong Liu } 59708024885SSong Liu 59808024885SSong Liu static ssize_t set_component_power_status(struct device *cdev, 59908024885SSong Liu struct device_attribute *attr, 60008024885SSong Liu const char *buf, size_t count) 60108024885SSong Liu { 60208024885SSong Liu struct enclosure_device *edev = to_enclosure_device(cdev->parent); 60308024885SSong Liu struct enclosure_component *ecomp = to_enclosure_component(cdev); 60408024885SSong Liu int val; 60508024885SSong Liu 60608024885SSong Liu if (strncmp(buf, "on", 2) == 0 && 60708024885SSong Liu (buf[2] == '\n' || buf[2] == '\0')) 60808024885SSong Liu val = 1; 60908024885SSong Liu else if (strncmp(buf, "off", 3) == 0 && 61008024885SSong Liu (buf[3] == '\n' || buf[3] == '\0')) 61108024885SSong Liu val = 0; 61208024885SSong Liu else 61308024885SSong Liu return -EINVAL; 61408024885SSong Liu 61508024885SSong Liu if (edev->cb->set_power_status) 61608024885SSong Liu edev->cb->set_power_status(edev, ecomp, val); 61708024885SSong Liu return count; 61808024885SSong Liu } 61908024885SSong Liu 620ee959b00STony Jones static ssize_t get_component_type(struct device *cdev, 621ee959b00STony Jones struct device_attribute *attr, char *buf) 622d569d5bbSJames Bottomley { 623d569d5bbSJames Bottomley struct enclosure_component *ecomp = to_enclosure_component(cdev); 624d569d5bbSJames Bottomley 625d569d5bbSJames Bottomley return snprintf(buf, 40, "%s\n", enclosure_type[ecomp->type]); 626d569d5bbSJames Bottomley } 627d569d5bbSJames Bottomley 628921ce7f5SDan Williams static ssize_t get_component_slot(struct device *cdev, 629921ce7f5SDan Williams struct device_attribute *attr, char *buf) 630921ce7f5SDan Williams { 631921ce7f5SDan Williams struct enclosure_component *ecomp = to_enclosure_component(cdev); 632921ce7f5SDan Williams int slot; 633921ce7f5SDan Williams 634921ce7f5SDan Williams /* if the enclosure does not override then use 'number' as a stand-in */ 635921ce7f5SDan Williams if (ecomp->slot >= 0) 636921ce7f5SDan Williams slot = ecomp->slot; 637921ce7f5SDan Williams else 638921ce7f5SDan Williams slot = ecomp->number; 639921ce7f5SDan Williams 640921ce7f5SDan Williams return snprintf(buf, 40, "%d\n", slot); 641921ce7f5SDan Williams } 642d569d5bbSJames Bottomley 643cb6b7f40SJames Bottomley static DEVICE_ATTR(fault, S_IRUGO | S_IWUSR, get_component_fault, 644cb6b7f40SJames Bottomley set_component_fault); 645cb6b7f40SJames Bottomley static DEVICE_ATTR(status, S_IRUGO | S_IWUSR, get_component_status, 646cb6b7f40SJames Bottomley set_component_status); 647cb6b7f40SJames Bottomley static DEVICE_ATTR(active, S_IRUGO | S_IWUSR, get_component_active, 648cb6b7f40SJames Bottomley set_component_active); 649cb6b7f40SJames Bottomley static DEVICE_ATTR(locate, S_IRUGO | S_IWUSR, get_component_locate, 650cb6b7f40SJames Bottomley set_component_locate); 65108024885SSong Liu static DEVICE_ATTR(power_status, S_IRUGO | S_IWUSR, get_component_power_status, 65208024885SSong Liu set_component_power_status); 653cb6b7f40SJames Bottomley static DEVICE_ATTR(type, S_IRUGO, get_component_type, NULL); 654921ce7f5SDan Williams static DEVICE_ATTR(slot, S_IRUGO, get_component_slot, NULL); 655cb6b7f40SJames Bottomley 656cb6b7f40SJames Bottomley static struct attribute *enclosure_component_attrs[] = { 657cb6b7f40SJames Bottomley &dev_attr_fault.attr, 658cb6b7f40SJames Bottomley &dev_attr_status.attr, 659cb6b7f40SJames Bottomley &dev_attr_active.attr, 660cb6b7f40SJames Bottomley &dev_attr_locate.attr, 66108024885SSong Liu &dev_attr_power_status.attr, 662cb6b7f40SJames Bottomley &dev_attr_type.attr, 663921ce7f5SDan Williams &dev_attr_slot.attr, 664cb6b7f40SJames Bottomley NULL 665d569d5bbSJames Bottomley }; 666899826f1SGreg Kroah-Hartman ATTRIBUTE_GROUPS(enclosure_component); 667d569d5bbSJames Bottomley 668d569d5bbSJames Bottomley static int __init enclosure_init(void) 669d569d5bbSJames Bottomley { 670750b54deSArvind Yadav return class_register(&enclosure_class); 671d569d5bbSJames Bottomley } 672d569d5bbSJames Bottomley 673d569d5bbSJames Bottomley static void __exit enclosure_exit(void) 674d569d5bbSJames Bottomley { 675d569d5bbSJames Bottomley class_unregister(&enclosure_class); 676d569d5bbSJames Bottomley } 677d569d5bbSJames Bottomley 678d569d5bbSJames Bottomley module_init(enclosure_init); 679d569d5bbSJames Bottomley module_exit(enclosure_exit); 680d569d5bbSJames Bottomley 681d569d5bbSJames Bottomley MODULE_AUTHOR("James Bottomley"); 682d569d5bbSJames Bottomley MODULE_DESCRIPTION("Enclosure Services"); 683d569d5bbSJames Bottomley MODULE_LICENSE("GPL v2"); 684