James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Enclosure Services |
| 3 | * |
| 4 | * Copyright (C) 2008 James Bottomley <James.Bottomley@HansenPartnership.com> |
| 5 | * |
| 6 | **----------------------------------------------------------------------------- |
| 7 | ** |
| 8 | ** This program is free software; you can redistribute it and/or |
| 9 | ** modify it under the terms of the GNU General Public License |
| 10 | ** version 2 as published by the Free Software Foundation. |
| 11 | ** |
| 12 | ** This program is distributed in the hope that it will be useful, |
| 13 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | ** GNU General Public License for more details. |
| 16 | ** |
| 17 | ** You should have received a copy of the GNU General Public License |
| 18 | ** along with this program; if not, write to the Free Software |
| 19 | ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | ** |
| 21 | **----------------------------------------------------------------------------- |
| 22 | */ |
| 23 | #include <linux/device.h> |
| 24 | #include <linux/enclosure.h> |
| 25 | #include <linux/err.h> |
| 26 | #include <linux/list.h> |
| 27 | #include <linux/kernel.h> |
| 28 | #include <linux/module.h> |
| 29 | #include <linux/mutex.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 30 | #include <linux/slab.h> |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 31 | |
| 32 | static LIST_HEAD(container_list); |
| 33 | static DEFINE_MUTEX(container_list_lock); |
| 34 | static struct class enclosure_class; |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 35 | |
| 36 | /** |
James Bottomley | 163f52b | 2009-08-01 00:39:36 +0000 | [diff] [blame] | 37 | * enclosure_find - find an enclosure given a parent device |
| 38 | * @dev: the parent to match against |
| 39 | * @start: Optional enclosure device to start from (NULL if none) |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 40 | * |
James Bottomley | 163f52b | 2009-08-01 00:39:36 +0000 | [diff] [blame] | 41 | * Looks through the list of registered enclosures to find all those |
| 42 | * with @dev as a parent. Returns NULL if no enclosure is |
| 43 | * found. @start can be used as a starting point to obtain multiple |
| 44 | * enclosures per parent (should begin with NULL and then be set to |
| 45 | * each returned enclosure device). Obtains a reference to the |
| 46 | * enclosure class device which must be released with device_put(). |
| 47 | * If @start is not NULL, a reference must be taken on it which is |
| 48 | * released before returning (this allows a loop through all |
| 49 | * enclosures to exit with only the reference on the enclosure of |
| 50 | * interest held). Note that the @dev may correspond to the actual |
| 51 | * device housing the enclosure, in which case no iteration via @start |
| 52 | * is required. |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 53 | */ |
James Bottomley | 163f52b | 2009-08-01 00:39:36 +0000 | [diff] [blame] | 54 | struct enclosure_device *enclosure_find(struct device *dev, |
| 55 | struct enclosure_device *start) |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 56 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 57 | struct enclosure_device *edev; |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 58 | |
| 59 | mutex_lock(&container_list_lock); |
James Bottomley | 163f52b | 2009-08-01 00:39:36 +0000 | [diff] [blame] | 60 | edev = list_prepare_entry(start, &container_list, node); |
| 61 | if (start) |
| 62 | put_device(&start->edev); |
| 63 | |
| 64 | list_for_each_entry_continue(edev, &container_list, node) { |
| 65 | struct device *parent = edev->edev.parent; |
| 66 | /* parent might not be immediate, so iterate up to |
| 67 | * the root of the tree if necessary */ |
| 68 | while (parent) { |
| 69 | if (parent == dev) { |
| 70 | get_device(&edev->edev); |
| 71 | mutex_unlock(&container_list_lock); |
| 72 | return edev; |
| 73 | } |
| 74 | parent = parent->parent; |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | mutex_unlock(&container_list_lock); |
| 78 | |
| 79 | return NULL; |
| 80 | } |
| 81 | EXPORT_SYMBOL_GPL(enclosure_find); |
| 82 | |
| 83 | /** |
| 84 | * enclosure_for_each_device - calls a function for each enclosure |
| 85 | * @fn: the function to call |
| 86 | * @data: the data to pass to each call |
| 87 | * |
| 88 | * Loops over all the enclosures calling the function. |
| 89 | * |
| 90 | * Note, this function uses a mutex which will be held across calls to |
| 91 | * @fn, so it must have non atomic context, and @fn may (although it |
| 92 | * should not) sleep or otherwise cause the mutex to be held for |
| 93 | * indefinite periods |
| 94 | */ |
| 95 | int enclosure_for_each_device(int (*fn)(struct enclosure_device *, void *), |
| 96 | void *data) |
| 97 | { |
| 98 | int error = 0; |
| 99 | struct enclosure_device *edev; |
| 100 | |
| 101 | mutex_lock(&container_list_lock); |
| 102 | list_for_each_entry(edev, &container_list, node) { |
| 103 | error = fn(edev, data); |
| 104 | if (error) |
| 105 | break; |
| 106 | } |
| 107 | mutex_unlock(&container_list_lock); |
| 108 | |
| 109 | return error; |
| 110 | } |
| 111 | EXPORT_SYMBOL_GPL(enclosure_for_each_device); |
| 112 | |
| 113 | /** |
| 114 | * enclosure_register - register device as an enclosure |
| 115 | * |
| 116 | * @dev: device containing the enclosure |
| 117 | * @components: number of components in the enclosure |
| 118 | * |
| 119 | * This sets up the device for being an enclosure. Note that @dev does |
| 120 | * not have to be a dedicated enclosure device. It may be some other type |
| 121 | * of device that additionally responds to enclosure services |
| 122 | */ |
| 123 | struct enclosure_device * |
| 124 | enclosure_register(struct device *dev, const char *name, int components, |
| 125 | struct enclosure_component_callbacks *cb) |
| 126 | { |
| 127 | struct enclosure_device *edev = |
| 128 | kzalloc(sizeof(struct enclosure_device) + |
| 129 | sizeof(struct enclosure_component)*components, |
| 130 | GFP_KERNEL); |
| 131 | int err, i; |
| 132 | |
| 133 | BUG_ON(!cb); |
| 134 | |
| 135 | if (!edev) |
| 136 | return ERR_PTR(-ENOMEM); |
| 137 | |
| 138 | edev->components = components; |
| 139 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 140 | edev->edev.class = &enclosure_class; |
| 141 | edev->edev.parent = get_device(dev); |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 142 | edev->cb = cb; |
Yinghai Lu | 5e43754 | 2009-04-30 19:13:41 -0700 | [diff] [blame] | 143 | dev_set_name(&edev->edev, "%s", name); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 144 | err = device_register(&edev->edev); |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 145 | if (err) |
| 146 | goto err; |
| 147 | |
Dan Williams | 921ce7f | 2014-12-30 14:46:17 -0800 | [diff] [blame^] | 148 | for (i = 0; i < components; i++) { |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 149 | edev->component[i].number = -1; |
Dan Williams | 921ce7f | 2014-12-30 14:46:17 -0800 | [diff] [blame^] | 150 | edev->component[i].slot = -1; |
| 151 | } |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 152 | |
| 153 | mutex_lock(&container_list_lock); |
| 154 | list_add_tail(&edev->node, &container_list); |
| 155 | mutex_unlock(&container_list_lock); |
| 156 | |
| 157 | return edev; |
| 158 | |
| 159 | err: |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 160 | put_device(edev->edev.parent); |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 161 | kfree(edev); |
| 162 | return ERR_PTR(err); |
| 163 | } |
| 164 | EXPORT_SYMBOL_GPL(enclosure_register); |
| 165 | |
| 166 | static struct enclosure_component_callbacks enclosure_null_callbacks; |
| 167 | |
| 168 | /** |
| 169 | * enclosure_unregister - remove an enclosure |
| 170 | * |
| 171 | * @edev: the registered enclosure to remove; |
| 172 | */ |
| 173 | void enclosure_unregister(struct enclosure_device *edev) |
| 174 | { |
| 175 | int i; |
| 176 | |
| 177 | mutex_lock(&container_list_lock); |
| 178 | list_del(&edev->node); |
| 179 | mutex_unlock(&container_list_lock); |
| 180 | |
| 181 | for (i = 0; i < edev->components; i++) |
| 182 | if (edev->component[i].number != -1) |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 183 | device_unregister(&edev->component[i].cdev); |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 184 | |
| 185 | /* prevent any callbacks into service user */ |
| 186 | edev->cb = &enclosure_null_callbacks; |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 187 | device_unregister(&edev->edev); |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 188 | } |
| 189 | EXPORT_SYMBOL_GPL(enclosure_unregister); |
| 190 | |
James Bottomley | cb6b7f4 | 2008-03-15 13:01:40 -0500 | [diff] [blame] | 191 | #define ENCLOSURE_NAME_SIZE 64 |
Markus Stockhausen | d2fd76e | 2014-10-04 13:35:15 +0000 | [diff] [blame] | 192 | #define COMPONENT_NAME_SIZE 64 |
James Bottomley | cb6b7f4 | 2008-03-15 13:01:40 -0500 | [diff] [blame] | 193 | |
| 194 | static void enclosure_link_name(struct enclosure_component *cdev, char *name) |
| 195 | { |
| 196 | strcpy(name, "enclosure_device:"); |
Kay Sievers | 71610f5 | 2008-12-03 22:41:36 +0100 | [diff] [blame] | 197 | strcat(name, dev_name(&cdev->cdev)); |
James Bottomley | cb6b7f4 | 2008-03-15 13:01:40 -0500 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | static void enclosure_remove_links(struct enclosure_component *cdev) |
| 201 | { |
| 202 | char name[ENCLOSURE_NAME_SIZE]; |
| 203 | |
James Bottomley | a1470c7 | 2013-11-15 14:58:00 -0800 | [diff] [blame] | 204 | /* |
| 205 | * In odd circumstances, like multipath devices, something else may |
| 206 | * already have removed the links, so check for this condition first. |
| 207 | */ |
| 208 | if (!cdev->dev->kobj.sd) |
| 209 | return; |
| 210 | |
James Bottomley | cb6b7f4 | 2008-03-15 13:01:40 -0500 | [diff] [blame] | 211 | enclosure_link_name(cdev, name); |
| 212 | sysfs_remove_link(&cdev->dev->kobj, name); |
| 213 | sysfs_remove_link(&cdev->cdev.kobj, "device"); |
| 214 | } |
| 215 | |
| 216 | static int enclosure_add_links(struct enclosure_component *cdev) |
| 217 | { |
| 218 | int error; |
| 219 | char name[ENCLOSURE_NAME_SIZE]; |
| 220 | |
| 221 | error = sysfs_create_link(&cdev->cdev.kobj, &cdev->dev->kobj, "device"); |
| 222 | if (error) |
| 223 | return error; |
| 224 | |
| 225 | enclosure_link_name(cdev, name); |
| 226 | error = sysfs_create_link(&cdev->dev->kobj, &cdev->cdev.kobj, name); |
| 227 | if (error) |
| 228 | sysfs_remove_link(&cdev->cdev.kobj, "device"); |
| 229 | |
| 230 | return error; |
| 231 | } |
| 232 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 233 | static void enclosure_release(struct device *cdev) |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 234 | { |
| 235 | struct enclosure_device *edev = to_enclosure_device(cdev); |
| 236 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 237 | put_device(cdev->parent); |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 238 | kfree(edev); |
| 239 | } |
| 240 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 241 | static void enclosure_component_release(struct device *dev) |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 242 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 243 | struct enclosure_component *cdev = to_enclosure_component(dev); |
| 244 | |
James Bottomley | cb6b7f4 | 2008-03-15 13:01:40 -0500 | [diff] [blame] | 245 | if (cdev->dev) { |
| 246 | enclosure_remove_links(cdev); |
| 247 | put_device(cdev->dev); |
| 248 | } |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 249 | put_device(dev->parent); |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 250 | } |
| 251 | |
Markus Stockhausen | d2fd76e | 2014-10-04 13:35:15 +0000 | [diff] [blame] | 252 | static struct enclosure_component * |
| 253 | enclosure_component_find_by_name(struct enclosure_device *edev, |
| 254 | const char *name) |
| 255 | { |
| 256 | int i; |
| 257 | const char *cname; |
| 258 | struct enclosure_component *ecomp; |
| 259 | |
| 260 | if (!edev || !name || !name[0]) |
| 261 | return NULL; |
| 262 | |
| 263 | for (i = 0; i < edev->components; i++) { |
| 264 | ecomp = &edev->component[i]; |
| 265 | cname = dev_name(&ecomp->cdev); |
| 266 | if (ecomp->number != -1 && |
| 267 | cname && cname[0] && |
| 268 | !strcmp(cname, name)) |
| 269 | return ecomp; |
| 270 | } |
| 271 | |
| 272 | return NULL; |
| 273 | } |
| 274 | |
Greg Kroah-Hartman | 899826f | 2013-07-24 15:05:16 -0700 | [diff] [blame] | 275 | static const struct attribute_group *enclosure_component_groups[]; |
James Bottomley | cb6b7f4 | 2008-03-15 13:01:40 -0500 | [diff] [blame] | 276 | |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 277 | /** |
Dan Williams | ed09dcc | 2014-12-30 14:46:14 -0800 | [diff] [blame] | 278 | * enclosure_component_alloc - prepare a new enclosure component |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 279 | * @edev: the enclosure to add the component |
| 280 | * @num: the device number |
| 281 | * @type: the type of component being added |
| 282 | * @name: an optional name to appear in sysfs (leave NULL if none) |
| 283 | * |
Dan Williams | ed09dcc | 2014-12-30 14:46:14 -0800 | [diff] [blame] | 284 | * The name is optional for enclosures that give their components a unique |
| 285 | * name. If not, leave the field NULL and a name will be assigned. |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 286 | * |
| 287 | * Returns a pointer to the enclosure component or an error. |
| 288 | */ |
| 289 | struct enclosure_component * |
Dan Williams | ed09dcc | 2014-12-30 14:46:14 -0800 | [diff] [blame] | 290 | enclosure_component_alloc(struct enclosure_device *edev, |
| 291 | unsigned int number, |
| 292 | enum enclosure_component_type type, |
| 293 | const char *name) |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 294 | { |
| 295 | struct enclosure_component *ecomp; |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 296 | struct device *cdev; |
Dan Williams | ed09dcc | 2014-12-30 14:46:14 -0800 | [diff] [blame] | 297 | int i; |
Markus Stockhausen | d2fd76e | 2014-10-04 13:35:15 +0000 | [diff] [blame] | 298 | char newname[COMPONENT_NAME_SIZE]; |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 299 | |
| 300 | if (number >= edev->components) |
| 301 | return ERR_PTR(-EINVAL); |
| 302 | |
| 303 | ecomp = &edev->component[number]; |
| 304 | |
| 305 | if (ecomp->number != -1) |
| 306 | return ERR_PTR(-EINVAL); |
| 307 | |
| 308 | ecomp->type = type; |
| 309 | ecomp->number = number; |
| 310 | cdev = &ecomp->cdev; |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 311 | cdev->parent = get_device(&edev->edev); |
Markus Stockhausen | d2fd76e | 2014-10-04 13:35:15 +0000 | [diff] [blame] | 312 | |
| 313 | if (name && name[0]) { |
| 314 | /* Some hardware (e.g. enclosure in RX300 S6) has components |
| 315 | * with non unique names. Registering duplicates in sysfs |
| 316 | * will lead to warnings during bootup. So make the names |
| 317 | * unique by appending consecutive numbers -1, -2, ... */ |
| 318 | i = 1; |
| 319 | snprintf(newname, COMPONENT_NAME_SIZE, |
| 320 | "%s", name); |
| 321 | while (enclosure_component_find_by_name(edev, newname)) |
| 322 | snprintf(newname, COMPONENT_NAME_SIZE, |
| 323 | "%s-%i", name, i++); |
| 324 | dev_set_name(cdev, "%s", newname); |
| 325 | } else |
Kay Sievers | 71610f5 | 2008-12-03 22:41:36 +0100 | [diff] [blame] | 326 | dev_set_name(cdev, "%u", number); |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 327 | |
James Bottomley | cb6b7f4 | 2008-03-15 13:01:40 -0500 | [diff] [blame] | 328 | cdev->release = enclosure_component_release; |
Greg Kroah-Hartman | 899826f | 2013-07-24 15:05:16 -0700 | [diff] [blame] | 329 | cdev->groups = enclosure_component_groups; |
James Bottomley | cb6b7f4 | 2008-03-15 13:01:40 -0500 | [diff] [blame] | 330 | |
Dan Williams | ed09dcc | 2014-12-30 14:46:14 -0800 | [diff] [blame] | 331 | return ecomp; |
| 332 | } |
| 333 | EXPORT_SYMBOL_GPL(enclosure_component_alloc); |
| 334 | |
| 335 | /** |
| 336 | * enclosure_component_register - publishes an initialized enclosure component |
| 337 | * @ecomp: component to add |
| 338 | * |
| 339 | * Returns 0 on successful registration, releases the component otherwise |
| 340 | */ |
| 341 | int enclosure_component_register(struct enclosure_component *ecomp) |
| 342 | { |
| 343 | struct device *cdev; |
| 344 | int err; |
| 345 | |
| 346 | cdev = &ecomp->cdev; |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 347 | err = device_register(cdev); |
James Bottomley | a91c1be | 2010-03-12 16:14:42 -0600 | [diff] [blame] | 348 | if (err) { |
| 349 | ecomp->number = -1; |
| 350 | put_device(cdev); |
Dan Williams | ed09dcc | 2014-12-30 14:46:14 -0800 | [diff] [blame] | 351 | return err; |
James Bottomley | a91c1be | 2010-03-12 16:14:42 -0600 | [diff] [blame] | 352 | } |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 353 | |
Dan Williams | ed09dcc | 2014-12-30 14:46:14 -0800 | [diff] [blame] | 354 | return 0; |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 355 | } |
| 356 | EXPORT_SYMBOL_GPL(enclosure_component_register); |
| 357 | |
| 358 | /** |
| 359 | * enclosure_add_device - add a device as being part of an enclosure |
| 360 | * @edev: the enclosure device being added to. |
| 361 | * @num: the number of the component |
| 362 | * @dev: the device being added |
| 363 | * |
| 364 | * Declares a real device to reside in slot (or identifier) @num of an |
| 365 | * enclosure. This will cause the relevant sysfs links to appear. |
| 366 | * This function may also be used to change a device associated with |
| 367 | * an enclosure without having to call enclosure_remove_device() in |
| 368 | * between. |
| 369 | * |
| 370 | * Returns zero on success or an error. |
| 371 | */ |
| 372 | int enclosure_add_device(struct enclosure_device *edev, int component, |
| 373 | struct device *dev) |
| 374 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 375 | struct enclosure_component *cdev; |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 376 | |
| 377 | if (!edev || component >= edev->components) |
| 378 | return -EINVAL; |
| 379 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 380 | cdev = &edev->component[component]; |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 381 | |
James Bottomley | 21fab1d | 2009-08-01 00:43:59 +0000 | [diff] [blame] | 382 | if (cdev->dev == dev) |
| 383 | return -EEXIST; |
| 384 | |
James Bottomley | cb6b7f4 | 2008-03-15 13:01:40 -0500 | [diff] [blame] | 385 | if (cdev->dev) |
| 386 | enclosure_remove_links(cdev); |
| 387 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 388 | put_device(cdev->dev); |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 389 | cdev->dev = get_device(dev); |
James Bottomley | cb6b7f4 | 2008-03-15 13:01:40 -0500 | [diff] [blame] | 390 | return enclosure_add_links(cdev); |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 391 | } |
| 392 | EXPORT_SYMBOL_GPL(enclosure_add_device); |
| 393 | |
| 394 | /** |
| 395 | * enclosure_remove_device - remove a device from an enclosure |
| 396 | * @edev: the enclosure device |
| 397 | * @num: the number of the component to remove |
| 398 | * |
| 399 | * Returns zero on success or an error. |
| 400 | * |
| 401 | */ |
James Bottomley | 43d8eb9 | 2009-08-01 00:41:22 +0000 | [diff] [blame] | 402 | int enclosure_remove_device(struct enclosure_device *edev, struct device *dev) |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 403 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 404 | struct enclosure_component *cdev; |
James Bottomley | 43d8eb9 | 2009-08-01 00:41:22 +0000 | [diff] [blame] | 405 | int i; |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 406 | |
James Bottomley | 43d8eb9 | 2009-08-01 00:41:22 +0000 | [diff] [blame] | 407 | if (!edev || !dev) |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 408 | return -EINVAL; |
| 409 | |
James Bottomley | 43d8eb9 | 2009-08-01 00:41:22 +0000 | [diff] [blame] | 410 | for (i = 0; i < edev->components; i++) { |
| 411 | cdev = &edev->component[i]; |
| 412 | if (cdev->dev == dev) { |
| 413 | enclosure_remove_links(cdev); |
| 414 | device_del(&cdev->cdev); |
| 415 | put_device(dev); |
| 416 | cdev->dev = NULL; |
| 417 | return device_add(&cdev->cdev); |
| 418 | } |
| 419 | } |
| 420 | return -ENODEV; |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 421 | } |
| 422 | EXPORT_SYMBOL_GPL(enclosure_remove_device); |
| 423 | |
| 424 | /* |
| 425 | * sysfs pieces below |
| 426 | */ |
| 427 | |
Greg Kroah-Hartman | 899826f | 2013-07-24 15:05:16 -0700 | [diff] [blame] | 428 | static ssize_t components_show(struct device *cdev, |
| 429 | struct device_attribute *attr, char *buf) |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 430 | { |
| 431 | struct enclosure_device *edev = to_enclosure_device(cdev); |
| 432 | |
| 433 | return snprintf(buf, 40, "%d\n", edev->components); |
| 434 | } |
Greg Kroah-Hartman | 899826f | 2013-07-24 15:05:16 -0700 | [diff] [blame] | 435 | static DEVICE_ATTR_RO(components); |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 436 | |
Dan Williams | 967f7ba | 2014-12-30 14:46:16 -0800 | [diff] [blame] | 437 | static ssize_t id_show(struct device *cdev, |
| 438 | struct device_attribute *attr, |
| 439 | char *buf) |
| 440 | { |
| 441 | struct enclosure_device *edev = to_enclosure_device(cdev); |
| 442 | |
| 443 | if (edev->cb->show_id) |
| 444 | return edev->cb->show_id(edev, buf); |
| 445 | return -EINVAL; |
| 446 | } |
| 447 | static DEVICE_ATTR_RO(id); |
| 448 | |
Greg Kroah-Hartman | 899826f | 2013-07-24 15:05:16 -0700 | [diff] [blame] | 449 | static struct attribute *enclosure_class_attrs[] = { |
| 450 | &dev_attr_components.attr, |
Dan Williams | 967f7ba | 2014-12-30 14:46:16 -0800 | [diff] [blame] | 451 | &dev_attr_id.attr, |
Greg Kroah-Hartman | 899826f | 2013-07-24 15:05:16 -0700 | [diff] [blame] | 452 | NULL, |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 453 | }; |
Greg Kroah-Hartman | 899826f | 2013-07-24 15:05:16 -0700 | [diff] [blame] | 454 | ATTRIBUTE_GROUPS(enclosure_class); |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 455 | |
| 456 | static struct class enclosure_class = { |
| 457 | .name = "enclosure", |
| 458 | .owner = THIS_MODULE, |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 459 | .dev_release = enclosure_release, |
Greg Kroah-Hartman | 899826f | 2013-07-24 15:05:16 -0700 | [diff] [blame] | 460 | .dev_groups = enclosure_class_groups, |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 461 | }; |
| 462 | |
| 463 | static const char *const enclosure_status [] = { |
| 464 | [ENCLOSURE_STATUS_UNSUPPORTED] = "unsupported", |
| 465 | [ENCLOSURE_STATUS_OK] = "OK", |
| 466 | [ENCLOSURE_STATUS_CRITICAL] = "critical", |
| 467 | [ENCLOSURE_STATUS_NON_CRITICAL] = "non-critical", |
| 468 | [ENCLOSURE_STATUS_UNRECOVERABLE] = "unrecoverable", |
| 469 | [ENCLOSURE_STATUS_NOT_INSTALLED] = "not installed", |
| 470 | [ENCLOSURE_STATUS_UNKNOWN] = "unknown", |
| 471 | [ENCLOSURE_STATUS_UNAVAILABLE] = "unavailable", |
James Bottomley | cc9b2e9 | 2009-11-26 09:50:20 -0600 | [diff] [blame] | 472 | [ENCLOSURE_STATUS_MAX] = NULL, |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 473 | }; |
| 474 | |
| 475 | static const char *const enclosure_type [] = { |
| 476 | [ENCLOSURE_COMPONENT_DEVICE] = "device", |
| 477 | [ENCLOSURE_COMPONENT_ARRAY_DEVICE] = "array device", |
| 478 | }; |
| 479 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 480 | static ssize_t get_component_fault(struct device *cdev, |
| 481 | struct device_attribute *attr, char *buf) |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 482 | { |
| 483 | struct enclosure_device *edev = to_enclosure_device(cdev->parent); |
| 484 | struct enclosure_component *ecomp = to_enclosure_component(cdev); |
| 485 | |
| 486 | if (edev->cb->get_fault) |
| 487 | edev->cb->get_fault(edev, ecomp); |
| 488 | return snprintf(buf, 40, "%d\n", ecomp->fault); |
| 489 | } |
| 490 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 491 | static ssize_t set_component_fault(struct device *cdev, |
| 492 | struct device_attribute *attr, |
| 493 | const char *buf, size_t count) |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 494 | { |
| 495 | struct enclosure_device *edev = to_enclosure_device(cdev->parent); |
| 496 | struct enclosure_component *ecomp = to_enclosure_component(cdev); |
| 497 | int val = simple_strtoul(buf, NULL, 0); |
| 498 | |
| 499 | if (edev->cb->set_fault) |
| 500 | edev->cb->set_fault(edev, ecomp, val); |
| 501 | return count; |
| 502 | } |
| 503 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 504 | static ssize_t get_component_status(struct device *cdev, |
| 505 | struct device_attribute *attr,char *buf) |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 506 | { |
| 507 | struct enclosure_device *edev = to_enclosure_device(cdev->parent); |
| 508 | struct enclosure_component *ecomp = to_enclosure_component(cdev); |
| 509 | |
| 510 | if (edev->cb->get_status) |
| 511 | edev->cb->get_status(edev, ecomp); |
| 512 | return snprintf(buf, 40, "%s\n", enclosure_status[ecomp->status]); |
| 513 | } |
| 514 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 515 | static ssize_t set_component_status(struct device *cdev, |
| 516 | struct device_attribute *attr, |
| 517 | const char *buf, size_t count) |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 518 | { |
| 519 | struct enclosure_device *edev = to_enclosure_device(cdev->parent); |
| 520 | struct enclosure_component *ecomp = to_enclosure_component(cdev); |
| 521 | int i; |
| 522 | |
| 523 | for (i = 0; enclosure_status[i]; i++) { |
| 524 | if (strncmp(buf, enclosure_status[i], |
| 525 | strlen(enclosure_status[i])) == 0 && |
| 526 | (buf[strlen(enclosure_status[i])] == '\n' || |
| 527 | buf[strlen(enclosure_status[i])] == '\0')) |
| 528 | break; |
| 529 | } |
| 530 | |
| 531 | if (enclosure_status[i] && edev->cb->set_status) { |
| 532 | edev->cb->set_status(edev, ecomp, i); |
| 533 | return count; |
| 534 | } else |
| 535 | return -EINVAL; |
| 536 | } |
| 537 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 538 | static ssize_t get_component_active(struct device *cdev, |
| 539 | struct device_attribute *attr, char *buf) |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 540 | { |
| 541 | struct enclosure_device *edev = to_enclosure_device(cdev->parent); |
| 542 | struct enclosure_component *ecomp = to_enclosure_component(cdev); |
| 543 | |
| 544 | if (edev->cb->get_active) |
| 545 | edev->cb->get_active(edev, ecomp); |
| 546 | return snprintf(buf, 40, "%d\n", ecomp->active); |
| 547 | } |
| 548 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 549 | static ssize_t set_component_active(struct device *cdev, |
| 550 | struct device_attribute *attr, |
| 551 | const char *buf, size_t count) |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 552 | { |
| 553 | struct enclosure_device *edev = to_enclosure_device(cdev->parent); |
| 554 | struct enclosure_component *ecomp = to_enclosure_component(cdev); |
| 555 | int val = simple_strtoul(buf, NULL, 0); |
| 556 | |
| 557 | if (edev->cb->set_active) |
| 558 | edev->cb->set_active(edev, ecomp, val); |
| 559 | return count; |
| 560 | } |
| 561 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 562 | static ssize_t get_component_locate(struct device *cdev, |
| 563 | struct device_attribute *attr, char *buf) |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 564 | { |
| 565 | struct enclosure_device *edev = to_enclosure_device(cdev->parent); |
| 566 | struct enclosure_component *ecomp = to_enclosure_component(cdev); |
| 567 | |
| 568 | if (edev->cb->get_locate) |
| 569 | edev->cb->get_locate(edev, ecomp); |
| 570 | return snprintf(buf, 40, "%d\n", ecomp->locate); |
| 571 | } |
| 572 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 573 | static ssize_t set_component_locate(struct device *cdev, |
| 574 | struct device_attribute *attr, |
| 575 | const char *buf, size_t count) |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 576 | { |
| 577 | struct enclosure_device *edev = to_enclosure_device(cdev->parent); |
| 578 | struct enclosure_component *ecomp = to_enclosure_component(cdev); |
| 579 | int val = simple_strtoul(buf, NULL, 0); |
| 580 | |
| 581 | if (edev->cb->set_locate) |
| 582 | edev->cb->set_locate(edev, ecomp, val); |
| 583 | return count; |
| 584 | } |
| 585 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 586 | static ssize_t get_component_type(struct device *cdev, |
| 587 | struct device_attribute *attr, char *buf) |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 588 | { |
| 589 | struct enclosure_component *ecomp = to_enclosure_component(cdev); |
| 590 | |
| 591 | return snprintf(buf, 40, "%s\n", enclosure_type[ecomp->type]); |
| 592 | } |
| 593 | |
Dan Williams | 921ce7f | 2014-12-30 14:46:17 -0800 | [diff] [blame^] | 594 | static ssize_t get_component_slot(struct device *cdev, |
| 595 | struct device_attribute *attr, char *buf) |
| 596 | { |
| 597 | struct enclosure_component *ecomp = to_enclosure_component(cdev); |
| 598 | int slot; |
| 599 | |
| 600 | /* if the enclosure does not override then use 'number' as a stand-in */ |
| 601 | if (ecomp->slot >= 0) |
| 602 | slot = ecomp->slot; |
| 603 | else |
| 604 | slot = ecomp->number; |
| 605 | |
| 606 | return snprintf(buf, 40, "%d\n", slot); |
| 607 | } |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 608 | |
James Bottomley | cb6b7f4 | 2008-03-15 13:01:40 -0500 | [diff] [blame] | 609 | static DEVICE_ATTR(fault, S_IRUGO | S_IWUSR, get_component_fault, |
| 610 | set_component_fault); |
| 611 | static DEVICE_ATTR(status, S_IRUGO | S_IWUSR, get_component_status, |
| 612 | set_component_status); |
| 613 | static DEVICE_ATTR(active, S_IRUGO | S_IWUSR, get_component_active, |
| 614 | set_component_active); |
| 615 | static DEVICE_ATTR(locate, S_IRUGO | S_IWUSR, get_component_locate, |
| 616 | set_component_locate); |
| 617 | static DEVICE_ATTR(type, S_IRUGO, get_component_type, NULL); |
Dan Williams | 921ce7f | 2014-12-30 14:46:17 -0800 | [diff] [blame^] | 618 | static DEVICE_ATTR(slot, S_IRUGO, get_component_slot, NULL); |
James Bottomley | cb6b7f4 | 2008-03-15 13:01:40 -0500 | [diff] [blame] | 619 | |
| 620 | static struct attribute *enclosure_component_attrs[] = { |
| 621 | &dev_attr_fault.attr, |
| 622 | &dev_attr_status.attr, |
| 623 | &dev_attr_active.attr, |
| 624 | &dev_attr_locate.attr, |
| 625 | &dev_attr_type.attr, |
Dan Williams | 921ce7f | 2014-12-30 14:46:17 -0800 | [diff] [blame^] | 626 | &dev_attr_slot.attr, |
James Bottomley | cb6b7f4 | 2008-03-15 13:01:40 -0500 | [diff] [blame] | 627 | NULL |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 628 | }; |
Greg Kroah-Hartman | 899826f | 2013-07-24 15:05:16 -0700 | [diff] [blame] | 629 | ATTRIBUTE_GROUPS(enclosure_component); |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 630 | |
| 631 | static int __init enclosure_init(void) |
| 632 | { |
| 633 | int err; |
| 634 | |
| 635 | err = class_register(&enclosure_class); |
| 636 | if (err) |
| 637 | return err; |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 638 | |
| 639 | return 0; |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | static void __exit enclosure_exit(void) |
| 643 | { |
James Bottomley | d569d5b | 2008-02-03 15:40:56 -0600 | [diff] [blame] | 644 | class_unregister(&enclosure_class); |
| 645 | } |
| 646 | |
| 647 | module_init(enclosure_init); |
| 648 | module_exit(enclosure_exit); |
| 649 | |
| 650 | MODULE_AUTHOR("James Bottomley"); |
| 651 | MODULE_DESCRIPTION("Enclosure Services"); |
| 652 | MODULE_LICENSE("GPL v2"); |