Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * File...........: linux/drivers/s390/block/dasd_devmap.c |
| 3 | * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com> |
| 4 | * Horst Hummel <Horst.Hummel@de.ibm.com> |
| 5 | * Carsten Otte <Cotte@de.ibm.com> |
| 6 | * Martin Schwidefsky <schwidefsky@de.ibm.com> |
| 7 | * Bugreports.to..: <Linux390@de.ibm.com> |
| 8 | * (C) IBM Corporation, IBM Deutschland Entwicklung GmbH, 1999-2001 |
| 9 | * |
| 10 | * Device mapping and dasd= parameter parsing functions. All devmap |
| 11 | * functions may not be called from interrupt context. In particular |
| 12 | * dasd_get_device is a no-no from interrupt context. |
| 13 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <linux/config.h> |
| 17 | #include <linux/ctype.h> |
| 18 | #include <linux/init.h> |
Rusty Russell | 8d3b33f | 2006-03-25 03:07:05 -0800 | [diff] [blame] | 19 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
| 21 | #include <asm/debug.h> |
| 22 | #include <asm/uaccess.h> |
| 23 | |
| 24 | /* This is ugly... */ |
| 25 | #define PRINTK_HEADER "dasd_devmap:" |
| 26 | |
| 27 | #include "dasd_int.h" |
| 28 | |
| 29 | kmem_cache_t *dasd_page_cache; |
| 30 | EXPORT_SYMBOL(dasd_page_cache); |
| 31 | |
| 32 | /* |
| 33 | * dasd_devmap_t is used to store the features and the relation |
| 34 | * between device number and device index. To find a dasd_devmap_t |
| 35 | * that corresponds to a device number of a device index each |
| 36 | * dasd_devmap_t is added to two linked lists, one to search by |
| 37 | * the device number and one to search by the device index. As |
| 38 | * soon as big minor numbers are available the device index list |
| 39 | * can be removed since the device number will then be identical |
| 40 | * to the device index. |
| 41 | */ |
| 42 | struct dasd_devmap { |
| 43 | struct list_head list; |
| 44 | char bus_id[BUS_ID_SIZE]; |
| 45 | unsigned int devindex; |
| 46 | unsigned short features; |
| 47 | struct dasd_device *device; |
Horst Hummel | 3d05259 | 2006-04-27 18:40:28 -0700 | [diff] [blame] | 48 | struct dasd_uid uid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | /* |
| 52 | * Parameter parsing functions for dasd= parameter. The syntax is: |
| 53 | * <devno> : (0x)?[0-9a-fA-F]+ |
| 54 | * <busid> : [0-0a-f]\.[0-9a-f]\.(0x)?[0-9a-fA-F]+ |
| 55 | * <feature> : ro |
| 56 | * <feature_list> : \(<feature>(:<feature>)*\) |
| 57 | * <devno-range> : <devno>(-<devno>)?<feature_list>? |
| 58 | * <busid-range> : <busid>(-<busid>)?<feature_list>? |
| 59 | * <devices> : <devno-range>|<busid-range> |
| 60 | * <dasd_module> : dasd_diag_mod|dasd_eckd_mod|dasd_fba_mod |
| 61 | * |
| 62 | * <dasd> : autodetect|probeonly|<devices>(,<devices>)* |
| 63 | */ |
| 64 | |
| 65 | int dasd_probeonly = 0; /* is true, when probeonly mode is active */ |
| 66 | int dasd_autodetect = 0; /* is true, when autodetection is active */ |
| 67 | |
| 68 | /* |
| 69 | * char *dasd[] is intended to hold the ranges supplied by the dasd= statement |
| 70 | * it is named 'dasd' to directly be filled by insmod with the comma separated |
| 71 | * strings when running as a module. |
| 72 | */ |
| 73 | static char *dasd[256]; |
Rusty Russell | 8d3b33f | 2006-03-25 03:07:05 -0800 | [diff] [blame] | 74 | module_param_array(dasd, charp, NULL, 0); |
| 75 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | /* |
| 77 | * Single spinlock to protect devmap structures and lists. |
| 78 | */ |
| 79 | static DEFINE_SPINLOCK(dasd_devmap_lock); |
| 80 | |
| 81 | /* |
| 82 | * Hash lists for devmap structures. |
| 83 | */ |
| 84 | static struct list_head dasd_hashlists[256]; |
| 85 | int dasd_max_devindex; |
| 86 | |
| 87 | static struct dasd_devmap *dasd_add_busid(char *, int); |
| 88 | |
| 89 | static inline int |
| 90 | dasd_hash_busid(char *bus_id) |
| 91 | { |
| 92 | int hash, i; |
| 93 | |
| 94 | hash = 0; |
| 95 | for (i = 0; (i < BUS_ID_SIZE) && *bus_id; i++, bus_id++) |
| 96 | hash += *bus_id; |
| 97 | return hash & 0xff; |
| 98 | } |
| 99 | |
| 100 | #ifndef MODULE |
| 101 | /* |
| 102 | * The parameter parsing functions for builtin-drivers are called |
| 103 | * before kmalloc works. Store the pointers to the parameters strings |
| 104 | * into dasd[] for later processing. |
| 105 | */ |
| 106 | static int __init |
| 107 | dasd_call_setup(char *str) |
| 108 | { |
| 109 | static int count = 0; |
| 110 | |
| 111 | if (count < 256) |
| 112 | dasd[count++] = str; |
| 113 | return 1; |
| 114 | } |
| 115 | |
| 116 | __setup ("dasd=", dasd_call_setup); |
| 117 | #endif /* #ifndef MODULE */ |
| 118 | |
| 119 | /* |
| 120 | * Read a device busid/devno from a string. |
| 121 | */ |
| 122 | static inline int |
| 123 | dasd_busid(char **str, int *id0, int *id1, int *devno) |
| 124 | { |
| 125 | int val, old_style; |
Horst Hummel | 138c014 | 2006-06-29 14:58:12 +0200 | [diff] [blame^] | 126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | /* check for leading '0x' */ |
| 128 | old_style = 0; |
| 129 | if ((*str)[0] == '0' && (*str)[1] == 'x') { |
| 130 | *str += 2; |
| 131 | old_style = 1; |
| 132 | } |
| 133 | if (!isxdigit((*str)[0])) /* We require at least one hex digit */ |
| 134 | return -EINVAL; |
| 135 | val = simple_strtoul(*str, str, 16); |
| 136 | if (old_style || (*str)[0] != '.') { |
| 137 | *id0 = *id1 = 0; |
| 138 | if (val < 0 || val > 0xffff) |
| 139 | return -EINVAL; |
| 140 | *devno = val; |
| 141 | return 0; |
| 142 | } |
| 143 | /* New style x.y.z busid */ |
| 144 | if (val < 0 || val > 0xff) |
| 145 | return -EINVAL; |
| 146 | *id0 = val; |
| 147 | (*str)++; |
| 148 | if (!isxdigit((*str)[0])) /* We require at least one hex digit */ |
| 149 | return -EINVAL; |
| 150 | val = simple_strtoul(*str, str, 16); |
| 151 | if (val < 0 || val > 0xff || (*str)++[0] != '.') |
| 152 | return -EINVAL; |
| 153 | *id1 = val; |
| 154 | if (!isxdigit((*str)[0])) /* We require at least one hex digit */ |
| 155 | return -EINVAL; |
| 156 | val = simple_strtoul(*str, str, 16); |
| 157 | if (val < 0 || val > 0xffff) |
| 158 | return -EINVAL; |
| 159 | *devno = val; |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * Read colon separated list of dasd features. Currently there is |
| 165 | * only one: "ro" for read-only devices. The default feature set |
| 166 | * is empty (value 0). |
| 167 | */ |
| 168 | static inline int |
| 169 | dasd_feature_list(char *str, char **endp) |
| 170 | { |
| 171 | int features, len, rc; |
| 172 | |
| 173 | rc = 0; |
| 174 | if (*str != '(') { |
| 175 | *endp = str; |
| 176 | return DASD_FEATURE_DEFAULT; |
| 177 | } |
| 178 | str++; |
| 179 | features = 0; |
| 180 | |
| 181 | while (1) { |
Horst Hummel | 138c014 | 2006-06-29 14:58:12 +0200 | [diff] [blame^] | 182 | for (len = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | str[len] && str[len] != ':' && str[len] != ')'; len++); |
| 184 | if (len == 2 && !strncmp(str, "ro", 2)) |
| 185 | features |= DASD_FEATURE_READONLY; |
| 186 | else if (len == 4 && !strncmp(str, "diag", 4)) |
| 187 | features |= DASD_FEATURE_USEDIAG; |
| 188 | else { |
| 189 | MESSAGE(KERN_WARNING, |
| 190 | "unsupported feature: %*s, " |
| 191 | "ignoring setting", len, str); |
| 192 | rc = -EINVAL; |
| 193 | } |
| 194 | str += len; |
| 195 | if (*str != ':') |
| 196 | break; |
| 197 | str++; |
| 198 | } |
| 199 | if (*str != ')') { |
| 200 | MESSAGE(KERN_WARNING, "%s", |
| 201 | "missing ')' in dasd parameter string\n"); |
| 202 | rc = -EINVAL; |
| 203 | } else |
| 204 | str++; |
| 205 | *endp = str; |
| 206 | if (rc != 0) |
| 207 | return rc; |
| 208 | return features; |
| 209 | } |
| 210 | |
| 211 | /* |
| 212 | * Try to match the first element on the comma separated parse string |
| 213 | * with one of the known keywords. If a keyword is found, take the approprate |
| 214 | * action and return a pointer to the residual string. If the first element |
| 215 | * could not be matched to any keyword then return an error code. |
| 216 | */ |
| 217 | static char * |
| 218 | dasd_parse_keyword( char *parsestring ) { |
| 219 | |
| 220 | char *nextcomma, *residual_str; |
| 221 | int length; |
| 222 | |
| 223 | nextcomma = strchr(parsestring,','); |
| 224 | if (nextcomma) { |
| 225 | length = nextcomma - parsestring; |
| 226 | residual_str = nextcomma + 1; |
| 227 | } else { |
| 228 | length = strlen(parsestring); |
| 229 | residual_str = parsestring + length; |
| 230 | } |
| 231 | if (strncmp ("autodetect", parsestring, length) == 0) { |
| 232 | dasd_autodetect = 1; |
| 233 | MESSAGE (KERN_INFO, "%s", |
| 234 | "turning to autodetection mode"); |
| 235 | return residual_str; |
| 236 | } |
| 237 | if (strncmp ("probeonly", parsestring, length) == 0) { |
| 238 | dasd_probeonly = 1; |
| 239 | MESSAGE(KERN_INFO, "%s", |
| 240 | "turning to probeonly mode"); |
| 241 | return residual_str; |
| 242 | } |
| 243 | if (strncmp ("fixedbuffers", parsestring, length) == 0) { |
| 244 | if (dasd_page_cache) |
| 245 | return residual_str; |
| 246 | dasd_page_cache = |
| 247 | kmem_cache_create("dasd_page_cache", PAGE_SIZE, 0, |
| 248 | SLAB_CACHE_DMA, NULL, NULL ); |
| 249 | if (!dasd_page_cache) |
| 250 | MESSAGE(KERN_WARNING, "%s", "Failed to create slab, " |
| 251 | "fixed buffer mode disabled."); |
| 252 | else |
| 253 | MESSAGE (KERN_INFO, "%s", |
| 254 | "turning on fixed buffer mode"); |
| 255 | return residual_str; |
| 256 | } |
| 257 | return ERR_PTR(-EINVAL); |
| 258 | } |
| 259 | |
| 260 | /* |
| 261 | * Try to interprete the first element on the comma separated parse string |
| 262 | * as a device number or a range of devices. If the interpretation is |
| 263 | * successfull, create the matching dasd_devmap entries and return a pointer |
| 264 | * to the residual string. |
| 265 | * If interpretation fails or in case of an error, return an error code. |
| 266 | */ |
| 267 | static char * |
| 268 | dasd_parse_range( char *parsestring ) { |
| 269 | |
| 270 | struct dasd_devmap *devmap; |
| 271 | int from, from_id0, from_id1; |
| 272 | int to, to_id0, to_id1; |
| 273 | int features, rc; |
| 274 | char bus_id[BUS_ID_SIZE+1], *str; |
| 275 | |
| 276 | str = parsestring; |
| 277 | rc = dasd_busid(&str, &from_id0, &from_id1, &from); |
| 278 | if (rc == 0) { |
| 279 | to = from; |
| 280 | to_id0 = from_id0; |
| 281 | to_id1 = from_id1; |
| 282 | if (*str == '-') { |
| 283 | str++; |
| 284 | rc = dasd_busid(&str, &to_id0, &to_id1, &to); |
| 285 | } |
| 286 | } |
| 287 | if (rc == 0 && |
| 288 | (from_id0 != to_id0 || from_id1 != to_id1 || from > to)) |
| 289 | rc = -EINVAL; |
| 290 | if (rc) { |
| 291 | MESSAGE(KERN_ERR, "Invalid device range %s", parsestring); |
| 292 | return ERR_PTR(rc); |
| 293 | } |
| 294 | features = dasd_feature_list(str, &str); |
| 295 | if (features < 0) |
| 296 | return ERR_PTR(-EINVAL); |
| 297 | while (from <= to) { |
| 298 | sprintf(bus_id, "%01x.%01x.%04x", |
| 299 | from_id0, from_id1, from++); |
| 300 | devmap = dasd_add_busid(bus_id, features); |
| 301 | if (IS_ERR(devmap)) |
| 302 | return (char *)devmap; |
| 303 | } |
| 304 | if (*str == ',') |
| 305 | return str + 1; |
| 306 | if (*str == '\0') |
| 307 | return str; |
| 308 | MESSAGE(KERN_WARNING, |
| 309 | "junk at end of dasd parameter string: %s\n", str); |
| 310 | return ERR_PTR(-EINVAL); |
| 311 | } |
| 312 | |
| 313 | static inline char * |
| 314 | dasd_parse_next_element( char *parsestring ) { |
| 315 | char * residual_str; |
| 316 | residual_str = dasd_parse_keyword(parsestring); |
| 317 | if (!IS_ERR(residual_str)) |
| 318 | return residual_str; |
| 319 | residual_str = dasd_parse_range(parsestring); |
| 320 | return residual_str; |
| 321 | } |
| 322 | |
| 323 | /* |
| 324 | * Parse parameters stored in dasd[] |
| 325 | * The 'dasd=...' parameter allows to specify a comma separated list of |
| 326 | * keywords and device ranges. When the dasd driver is build into the kernel, |
| 327 | * the complete list will be stored as one element of the dasd[] array. |
| 328 | * When the dasd driver is build as a module, then the list is broken into |
| 329 | * it's elements and each dasd[] entry contains one element. |
| 330 | */ |
| 331 | int |
| 332 | dasd_parse(void) |
| 333 | { |
| 334 | int rc, i; |
| 335 | char *parsestring; |
| 336 | |
| 337 | rc = 0; |
| 338 | for (i = 0; i < 256; i++) { |
| 339 | if (dasd[i] == NULL) |
| 340 | break; |
| 341 | parsestring = dasd[i]; |
| 342 | /* loop over the comma separated list in the parsestring */ |
| 343 | while (*parsestring) { |
| 344 | parsestring = dasd_parse_next_element(parsestring); |
| 345 | if(IS_ERR(parsestring)) { |
| 346 | rc = PTR_ERR(parsestring); |
| 347 | break; |
| 348 | } |
| 349 | } |
| 350 | if (rc) { |
| 351 | DBF_EVENT(DBF_ALERT, "%s", "invalid range found"); |
| 352 | break; |
| 353 | } |
| 354 | } |
| 355 | return rc; |
| 356 | } |
| 357 | |
| 358 | /* |
| 359 | * Add a devmap for the device specified by busid. It is possible that |
| 360 | * the devmap already exists (dasd= parameter). The order of the devices |
| 361 | * added through this function will define the kdevs for the individual |
Horst Hummel | 138c014 | 2006-06-29 14:58:12 +0200 | [diff] [blame^] | 362 | * devices. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | */ |
| 364 | static struct dasd_devmap * |
| 365 | dasd_add_busid(char *bus_id, int features) |
| 366 | { |
| 367 | struct dasd_devmap *devmap, *new, *tmp; |
| 368 | int hash; |
| 369 | |
| 370 | new = (struct dasd_devmap *) |
Horst Hummel | 138c014 | 2006-06-29 14:58:12 +0200 | [diff] [blame^] | 371 | kzalloc(sizeof(struct dasd_devmap), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | if (!new) |
| 373 | return ERR_PTR(-ENOMEM); |
| 374 | spin_lock(&dasd_devmap_lock); |
| 375 | devmap = 0; |
| 376 | hash = dasd_hash_busid(bus_id); |
| 377 | list_for_each_entry(tmp, &dasd_hashlists[hash], list) |
| 378 | if (strncmp(tmp->bus_id, bus_id, BUS_ID_SIZE) == 0) { |
| 379 | devmap = tmp; |
| 380 | break; |
| 381 | } |
| 382 | if (!devmap) { |
| 383 | /* This bus_id is new. */ |
| 384 | new->devindex = dasd_max_devindex++; |
| 385 | strncpy(new->bus_id, bus_id, BUS_ID_SIZE); |
| 386 | new->features = features; |
| 387 | new->device = 0; |
| 388 | list_add(&new->list, &dasd_hashlists[hash]); |
| 389 | devmap = new; |
| 390 | new = 0; |
| 391 | } |
| 392 | spin_unlock(&dasd_devmap_lock); |
Jesper Juhl | 17fd682 | 2005-11-07 01:01:30 -0800 | [diff] [blame] | 393 | kfree(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | return devmap; |
| 395 | } |
| 396 | |
| 397 | /* |
| 398 | * Find devmap for device with given bus_id. |
| 399 | */ |
| 400 | static struct dasd_devmap * |
| 401 | dasd_find_busid(char *bus_id) |
| 402 | { |
| 403 | struct dasd_devmap *devmap, *tmp; |
| 404 | int hash; |
| 405 | |
| 406 | spin_lock(&dasd_devmap_lock); |
| 407 | devmap = ERR_PTR(-ENODEV); |
| 408 | hash = dasd_hash_busid(bus_id); |
| 409 | list_for_each_entry(tmp, &dasd_hashlists[hash], list) { |
| 410 | if (strncmp(tmp->bus_id, bus_id, BUS_ID_SIZE) == 0) { |
| 411 | devmap = tmp; |
| 412 | break; |
| 413 | } |
| 414 | } |
| 415 | spin_unlock(&dasd_devmap_lock); |
| 416 | return devmap; |
| 417 | } |
| 418 | |
| 419 | /* |
| 420 | * Check if busid has been added to the list of dasd ranges. |
| 421 | */ |
| 422 | int |
| 423 | dasd_busid_known(char *bus_id) |
| 424 | { |
| 425 | return IS_ERR(dasd_find_busid(bus_id)) ? -ENOENT : 0; |
| 426 | } |
| 427 | |
| 428 | /* |
| 429 | * Forget all about the device numbers added so far. |
| 430 | * This may only be called at module unload or system shutdown. |
| 431 | */ |
| 432 | static void |
| 433 | dasd_forget_ranges(void) |
| 434 | { |
| 435 | struct dasd_devmap *devmap, *n; |
| 436 | int i; |
| 437 | |
| 438 | spin_lock(&dasd_devmap_lock); |
| 439 | for (i = 0; i < 256; i++) { |
| 440 | list_for_each_entry_safe(devmap, n, &dasd_hashlists[i], list) { |
Eric Sesterhenn | 606f442 | 2006-03-26 18:33:07 +0200 | [diff] [blame] | 441 | BUG_ON(devmap->device != NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | list_del(&devmap->list); |
| 443 | kfree(devmap); |
| 444 | } |
| 445 | } |
| 446 | spin_unlock(&dasd_devmap_lock); |
| 447 | } |
| 448 | |
| 449 | /* |
| 450 | * Find the device struct by its device index. |
| 451 | */ |
| 452 | struct dasd_device * |
| 453 | dasd_device_from_devindex(int devindex) |
| 454 | { |
| 455 | struct dasd_devmap *devmap, *tmp; |
| 456 | struct dasd_device *device; |
| 457 | int i; |
| 458 | |
| 459 | spin_lock(&dasd_devmap_lock); |
| 460 | devmap = 0; |
| 461 | for (i = 0; (i < 256) && !devmap; i++) |
| 462 | list_for_each_entry(tmp, &dasd_hashlists[i], list) |
| 463 | if (tmp->devindex == devindex) { |
| 464 | /* Found the devmap for the device. */ |
| 465 | devmap = tmp; |
| 466 | break; |
| 467 | } |
| 468 | if (devmap && devmap->device) { |
| 469 | device = devmap->device; |
| 470 | dasd_get_device(device); |
| 471 | } else |
| 472 | device = ERR_PTR(-ENODEV); |
| 473 | spin_unlock(&dasd_devmap_lock); |
| 474 | return device; |
| 475 | } |
| 476 | |
| 477 | /* |
| 478 | * Return devmap for cdev. If no devmap exists yet, create one and |
| 479 | * connect it to the cdev. |
| 480 | */ |
| 481 | static struct dasd_devmap * |
| 482 | dasd_devmap_from_cdev(struct ccw_device *cdev) |
| 483 | { |
| 484 | struct dasd_devmap *devmap; |
| 485 | |
| 486 | devmap = dasd_find_busid(cdev->dev.bus_id); |
| 487 | if (IS_ERR(devmap)) |
| 488 | devmap = dasd_add_busid(cdev->dev.bus_id, |
| 489 | DASD_FEATURE_DEFAULT); |
| 490 | return devmap; |
| 491 | } |
| 492 | |
| 493 | /* |
| 494 | * Create a dasd device structure for cdev. |
| 495 | */ |
| 496 | struct dasd_device * |
| 497 | dasd_create_device(struct ccw_device *cdev) |
| 498 | { |
| 499 | struct dasd_devmap *devmap; |
| 500 | struct dasd_device *device; |
| 501 | int rc; |
| 502 | |
| 503 | devmap = dasd_devmap_from_cdev(cdev); |
| 504 | if (IS_ERR(devmap)) |
| 505 | return (void *) devmap; |
| 506 | cdev->dev.driver_data = devmap; |
| 507 | |
| 508 | device = dasd_alloc_device(); |
| 509 | if (IS_ERR(device)) |
| 510 | return device; |
| 511 | atomic_set(&device->ref_count, 2); |
| 512 | |
| 513 | spin_lock(&dasd_devmap_lock); |
| 514 | if (!devmap->device) { |
| 515 | devmap->device = device; |
| 516 | device->devindex = devmap->devindex; |
Horst Hummel | c6eb7b7 | 2005-09-03 15:57:58 -0700 | [diff] [blame] | 517 | device->features = devmap->features; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | get_device(&cdev->dev); |
| 519 | device->cdev = cdev; |
| 520 | rc = 0; |
| 521 | } else |
| 522 | /* Someone else was faster. */ |
| 523 | rc = -EBUSY; |
| 524 | spin_unlock(&dasd_devmap_lock); |
| 525 | |
| 526 | if (rc) { |
| 527 | dasd_free_device(device); |
| 528 | return ERR_PTR(rc); |
| 529 | } |
| 530 | return device; |
| 531 | } |
| 532 | |
| 533 | /* |
| 534 | * Wait queue for dasd_delete_device waits. |
| 535 | */ |
| 536 | static DECLARE_WAIT_QUEUE_HEAD(dasd_delete_wq); |
| 537 | |
| 538 | /* |
| 539 | * Remove a dasd device structure. The passed referenced |
| 540 | * is destroyed. |
| 541 | */ |
| 542 | void |
| 543 | dasd_delete_device(struct dasd_device *device) |
| 544 | { |
| 545 | struct ccw_device *cdev; |
| 546 | struct dasd_devmap *devmap; |
| 547 | |
| 548 | /* First remove device pointer from devmap. */ |
| 549 | devmap = dasd_find_busid(device->cdev->dev.bus_id); |
Eric Sesterhenn | 606f442 | 2006-03-26 18:33:07 +0200 | [diff] [blame] | 550 | BUG_ON(IS_ERR(devmap)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | spin_lock(&dasd_devmap_lock); |
| 552 | if (devmap->device != device) { |
| 553 | spin_unlock(&dasd_devmap_lock); |
| 554 | dasd_put_device(device); |
| 555 | return; |
| 556 | } |
| 557 | devmap->device = NULL; |
| 558 | spin_unlock(&dasd_devmap_lock); |
| 559 | |
| 560 | /* Drop ref_count by 2, one for the devmap reference and |
| 561 | * one for the passed reference. */ |
| 562 | atomic_sub(2, &device->ref_count); |
| 563 | |
| 564 | /* Wait for reference counter to drop to zero. */ |
| 565 | wait_event(dasd_delete_wq, atomic_read(&device->ref_count) == 0); |
| 566 | |
| 567 | /* Disconnect dasd_device structure from ccw_device structure. */ |
| 568 | cdev = device->cdev; |
| 569 | device->cdev = NULL; |
| 570 | |
| 571 | /* Disconnect dasd_devmap structure from ccw_device structure. */ |
| 572 | cdev->dev.driver_data = NULL; |
| 573 | |
| 574 | /* Put ccw_device structure. */ |
| 575 | put_device(&cdev->dev); |
| 576 | |
| 577 | /* Now the device structure can be freed. */ |
| 578 | dasd_free_device(device); |
| 579 | } |
| 580 | |
| 581 | /* |
| 582 | * Reference counter dropped to zero. Wake up waiter |
| 583 | * in dasd_delete_device. |
| 584 | */ |
| 585 | void |
| 586 | dasd_put_device_wake(struct dasd_device *device) |
| 587 | { |
| 588 | wake_up(&dasd_delete_wq); |
| 589 | } |
| 590 | |
| 591 | /* |
| 592 | * Return dasd_device structure associated with cdev. |
| 593 | */ |
| 594 | struct dasd_device * |
| 595 | dasd_device_from_cdev(struct ccw_device *cdev) |
| 596 | { |
| 597 | struct dasd_devmap *devmap; |
| 598 | struct dasd_device *device; |
| 599 | |
| 600 | device = ERR_PTR(-ENODEV); |
| 601 | spin_lock(&dasd_devmap_lock); |
| 602 | devmap = cdev->dev.driver_data; |
| 603 | if (devmap && devmap->device) { |
| 604 | device = devmap->device; |
| 605 | dasd_get_device(device); |
| 606 | } |
| 607 | spin_unlock(&dasd_devmap_lock); |
| 608 | return device; |
| 609 | } |
| 610 | |
| 611 | /* |
| 612 | * SECTION: files in sysfs |
| 613 | */ |
| 614 | |
| 615 | /* |
| 616 | * readonly controls the readonly status of a dasd |
| 617 | */ |
| 618 | static ssize_t |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 619 | dasd_ro_show(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | { |
| 621 | struct dasd_devmap *devmap; |
| 622 | int ro_flag; |
| 623 | |
| 624 | devmap = dasd_find_busid(dev->bus_id); |
| 625 | if (!IS_ERR(devmap)) |
| 626 | ro_flag = (devmap->features & DASD_FEATURE_READONLY) != 0; |
| 627 | else |
| 628 | ro_flag = (DASD_FEATURE_DEFAULT & DASD_FEATURE_READONLY) != 0; |
| 629 | return snprintf(buf, PAGE_SIZE, ro_flag ? "1\n" : "0\n"); |
| 630 | } |
| 631 | |
| 632 | static ssize_t |
Horst Hummel | 138c014 | 2006-06-29 14:58:12 +0200 | [diff] [blame^] | 633 | dasd_ro_store(struct device *dev, struct device_attribute *attr, |
| 634 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | { |
| 636 | struct dasd_devmap *devmap; |
| 637 | int ro_flag; |
| 638 | |
| 639 | devmap = dasd_devmap_from_cdev(to_ccwdev(dev)); |
| 640 | if (IS_ERR(devmap)) |
| 641 | return PTR_ERR(devmap); |
| 642 | ro_flag = buf[0] == '1'; |
| 643 | spin_lock(&dasd_devmap_lock); |
| 644 | if (ro_flag) |
| 645 | devmap->features |= DASD_FEATURE_READONLY; |
| 646 | else |
| 647 | devmap->features &= ~DASD_FEATURE_READONLY; |
Horst Hummel | c6eb7b7 | 2005-09-03 15:57:58 -0700 | [diff] [blame] | 648 | if (devmap->device) |
| 649 | devmap->device->features = devmap->features; |
Horst Hummel | f24acd4 | 2005-05-01 08:58:59 -0700 | [diff] [blame] | 650 | if (devmap->device && devmap->device->gdp) |
| 651 | set_disk_ro(devmap->device->gdp, ro_flag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | spin_unlock(&dasd_devmap_lock); |
| 653 | return count; |
| 654 | } |
| 655 | |
| 656 | static DEVICE_ATTR(readonly, 0644, dasd_ro_show, dasd_ro_store); |
| 657 | |
| 658 | /* |
| 659 | * use_diag controls whether the driver should use diag rather than ssch |
| 660 | * to talk to the device |
| 661 | */ |
Horst Hummel | 138c014 | 2006-06-29 14:58:12 +0200 | [diff] [blame^] | 662 | static ssize_t |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 663 | dasd_use_diag_show(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | { |
| 665 | struct dasd_devmap *devmap; |
| 666 | int use_diag; |
| 667 | |
| 668 | devmap = dasd_find_busid(dev->bus_id); |
| 669 | if (!IS_ERR(devmap)) |
| 670 | use_diag = (devmap->features & DASD_FEATURE_USEDIAG) != 0; |
| 671 | else |
| 672 | use_diag = (DASD_FEATURE_DEFAULT & DASD_FEATURE_USEDIAG) != 0; |
| 673 | return sprintf(buf, use_diag ? "1\n" : "0\n"); |
| 674 | } |
| 675 | |
| 676 | static ssize_t |
Horst Hummel | 138c014 | 2006-06-29 14:58:12 +0200 | [diff] [blame^] | 677 | dasd_use_diag_store(struct device *dev, struct device_attribute *attr, |
| 678 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | { |
| 680 | struct dasd_devmap *devmap; |
| 681 | ssize_t rc; |
| 682 | int use_diag; |
| 683 | |
| 684 | devmap = dasd_devmap_from_cdev(to_ccwdev(dev)); |
| 685 | if (IS_ERR(devmap)) |
| 686 | return PTR_ERR(devmap); |
| 687 | use_diag = buf[0] == '1'; |
| 688 | spin_lock(&dasd_devmap_lock); |
| 689 | /* Changing diag discipline flag is only allowed in offline state. */ |
| 690 | rc = count; |
| 691 | if (!devmap->device) { |
| 692 | if (use_diag) |
| 693 | devmap->features |= DASD_FEATURE_USEDIAG; |
| 694 | else |
| 695 | devmap->features &= ~DASD_FEATURE_USEDIAG; |
| 696 | } else |
| 697 | rc = -EPERM; |
| 698 | spin_unlock(&dasd_devmap_lock); |
| 699 | return rc; |
| 700 | } |
| 701 | |
Horst Hummel | 138c014 | 2006-06-29 14:58:12 +0200 | [diff] [blame^] | 702 | static DEVICE_ATTR(use_diag, 0644, dasd_use_diag_show, dasd_use_diag_store); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | |
| 704 | static ssize_t |
Horst Hummel | 138c014 | 2006-06-29 14:58:12 +0200 | [diff] [blame^] | 705 | dasd_discipline_show(struct device *dev, struct device_attribute *attr, |
| 706 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | { |
| 708 | struct dasd_devmap *devmap; |
| 709 | char *dname; |
| 710 | |
| 711 | spin_lock(&dasd_devmap_lock); |
| 712 | dname = "none"; |
| 713 | devmap = dev->driver_data; |
| 714 | if (devmap && devmap->device && devmap->device->discipline) |
| 715 | dname = devmap->device->discipline->name; |
| 716 | spin_unlock(&dasd_devmap_lock); |
| 717 | return snprintf(buf, PAGE_SIZE, "%s\n", dname); |
| 718 | } |
| 719 | |
| 720 | static DEVICE_ATTR(discipline, 0444, dasd_discipline_show, NULL); |
| 721 | |
Horst Hummel | 3d05259 | 2006-04-27 18:40:28 -0700 | [diff] [blame] | 722 | static ssize_t |
| 723 | dasd_alias_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 724 | { |
| 725 | struct dasd_devmap *devmap; |
| 726 | int alias; |
| 727 | |
| 728 | devmap = dasd_find_busid(dev->bus_id); |
| 729 | spin_lock(&dasd_devmap_lock); |
| 730 | if (!IS_ERR(devmap)) |
| 731 | alias = devmap->uid.alias; |
| 732 | else |
| 733 | alias = 0; |
| 734 | spin_unlock(&dasd_devmap_lock); |
| 735 | |
| 736 | return sprintf(buf, alias ? "1\n" : "0\n"); |
| 737 | } |
| 738 | |
| 739 | static DEVICE_ATTR(alias, 0444, dasd_alias_show, NULL); |
| 740 | |
| 741 | static ssize_t |
| 742 | dasd_vendor_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 743 | { |
| 744 | struct dasd_devmap *devmap; |
| 745 | char *vendor; |
| 746 | |
| 747 | devmap = dasd_find_busid(dev->bus_id); |
| 748 | spin_lock(&dasd_devmap_lock); |
| 749 | if (!IS_ERR(devmap) && strlen(devmap->uid.vendor) > 0) |
| 750 | vendor = devmap->uid.vendor; |
| 751 | else |
| 752 | vendor = ""; |
| 753 | spin_unlock(&dasd_devmap_lock); |
| 754 | |
| 755 | return snprintf(buf, PAGE_SIZE, "%s\n", vendor); |
| 756 | } |
| 757 | |
| 758 | static DEVICE_ATTR(vendor, 0444, dasd_vendor_show, NULL); |
| 759 | |
| 760 | #define UID_STRLEN ( /* vendor */ 3 + 1 + /* serial */ 14 + 1 +\ |
| 761 | /* SSID */ 4 + 1 + /* unit addr */ 2 + 1) |
| 762 | |
| 763 | static ssize_t |
| 764 | dasd_uid_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 765 | { |
| 766 | struct dasd_devmap *devmap; |
| 767 | char uid[UID_STRLEN]; |
| 768 | |
| 769 | devmap = dasd_find_busid(dev->bus_id); |
| 770 | spin_lock(&dasd_devmap_lock); |
| 771 | if (!IS_ERR(devmap) && strlen(devmap->uid.vendor) > 0) |
| 772 | snprintf(uid, sizeof(uid), "%s.%s.%04x.%02x", |
| 773 | devmap->uid.vendor, devmap->uid.serial, |
| 774 | devmap->uid.ssid, devmap->uid.unit_addr); |
| 775 | else |
| 776 | uid[0] = 0; |
| 777 | spin_unlock(&dasd_devmap_lock); |
| 778 | |
| 779 | return snprintf(buf, PAGE_SIZE, "%s\n", uid); |
| 780 | } |
| 781 | |
| 782 | static DEVICE_ATTR(uid, 0444, dasd_uid_show, NULL); |
| 783 | |
Stefan Weinhuber | 20c6446 | 2006-03-24 03:15:25 -0800 | [diff] [blame] | 784 | /* |
| 785 | * extended error-reporting |
| 786 | */ |
| 787 | static ssize_t |
| 788 | dasd_eer_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 789 | { |
| 790 | struct dasd_devmap *devmap; |
| 791 | int eer_flag; |
| 792 | |
| 793 | devmap = dasd_find_busid(dev->bus_id); |
| 794 | if (!IS_ERR(devmap) && devmap->device) |
| 795 | eer_flag = dasd_eer_enabled(devmap->device); |
| 796 | else |
| 797 | eer_flag = 0; |
| 798 | return snprintf(buf, PAGE_SIZE, eer_flag ? "1\n" : "0\n"); |
| 799 | } |
| 800 | |
| 801 | static ssize_t |
| 802 | dasd_eer_store(struct device *dev, struct device_attribute *attr, |
| 803 | const char *buf, size_t count) |
| 804 | { |
| 805 | struct dasd_devmap *devmap; |
| 806 | int rc; |
| 807 | |
| 808 | devmap = dasd_devmap_from_cdev(to_ccwdev(dev)); |
| 809 | if (IS_ERR(devmap)) |
| 810 | return PTR_ERR(devmap); |
| 811 | if (!devmap->device) |
| 812 | return count; |
| 813 | if (buf[0] == '1') { |
| 814 | rc = dasd_eer_enable(devmap->device); |
| 815 | if (rc) |
| 816 | return rc; |
| 817 | } else |
| 818 | dasd_eer_disable(devmap->device); |
| 819 | return count; |
| 820 | } |
| 821 | |
| 822 | static DEVICE_ATTR(eer_enabled, 0644, dasd_eer_show, dasd_eer_store); |
| 823 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | static struct attribute * dasd_attrs[] = { |
| 825 | &dev_attr_readonly.attr, |
| 826 | &dev_attr_discipline.attr, |
Horst Hummel | 3d05259 | 2006-04-27 18:40:28 -0700 | [diff] [blame] | 827 | &dev_attr_alias.attr, |
| 828 | &dev_attr_vendor.attr, |
| 829 | &dev_attr_uid.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | &dev_attr_use_diag.attr, |
Stefan Weinhuber | 20c6446 | 2006-03-24 03:15:25 -0800 | [diff] [blame] | 831 | &dev_attr_eer_enabled.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | NULL, |
| 833 | }; |
| 834 | |
| 835 | static struct attribute_group dasd_attr_group = { |
| 836 | .attrs = dasd_attrs, |
| 837 | }; |
| 838 | |
Horst Hummel | 3d05259 | 2006-04-27 18:40:28 -0700 | [diff] [blame] | 839 | |
| 840 | /* |
| 841 | * Return copy of the device unique identifier. |
| 842 | */ |
| 843 | int |
| 844 | dasd_get_uid(struct ccw_device *cdev, struct dasd_uid *uid) |
| 845 | { |
| 846 | struct dasd_devmap *devmap; |
| 847 | |
| 848 | devmap = dasd_find_busid(cdev->dev.bus_id); |
| 849 | if (IS_ERR(devmap)) |
| 850 | return PTR_ERR(devmap); |
| 851 | spin_lock(&dasd_devmap_lock); |
| 852 | *uid = devmap->uid; |
| 853 | spin_unlock(&dasd_devmap_lock); |
| 854 | return 0; |
| 855 | } |
| 856 | |
| 857 | /* |
| 858 | * Register the given device unique identifier into devmap struct. |
| 859 | */ |
| 860 | int |
| 861 | dasd_set_uid(struct ccw_device *cdev, struct dasd_uid *uid) |
| 862 | { |
| 863 | struct dasd_devmap *devmap; |
| 864 | |
| 865 | devmap = dasd_find_busid(cdev->dev.bus_id); |
| 866 | if (IS_ERR(devmap)) |
| 867 | return PTR_ERR(devmap); |
| 868 | spin_lock(&dasd_devmap_lock); |
| 869 | devmap->uid = *uid; |
| 870 | spin_unlock(&dasd_devmap_lock); |
| 871 | return 0; |
| 872 | } |
| 873 | EXPORT_SYMBOL(dasd_set_uid); |
| 874 | |
Horst Hummel | f24acd4 | 2005-05-01 08:58:59 -0700 | [diff] [blame] | 875 | /* |
| 876 | * Return value of the specified feature. |
| 877 | */ |
| 878 | int |
| 879 | dasd_get_feature(struct ccw_device *cdev, int feature) |
| 880 | { |
| 881 | struct dasd_devmap *devmap; |
| 882 | |
| 883 | devmap = dasd_find_busid(cdev->dev.bus_id); |
| 884 | if (IS_ERR(devmap)) |
| 885 | return (int) PTR_ERR(devmap); |
| 886 | |
| 887 | return ((devmap->features & feature) != 0); |
| 888 | } |
| 889 | |
| 890 | /* |
| 891 | * Set / reset given feature. |
| 892 | * Flag indicates wether to set (!=0) or the reset (=0) the feature. |
| 893 | */ |
| 894 | int |
| 895 | dasd_set_feature(struct ccw_device *cdev, int feature, int flag) |
| 896 | { |
| 897 | struct dasd_devmap *devmap; |
| 898 | |
| 899 | devmap = dasd_find_busid(cdev->dev.bus_id); |
| 900 | if (IS_ERR(devmap)) |
| 901 | return (int) PTR_ERR(devmap); |
| 902 | |
| 903 | spin_lock(&dasd_devmap_lock); |
| 904 | if (flag) |
| 905 | devmap->features |= feature; |
| 906 | else |
| 907 | devmap->features &= ~feature; |
Horst Hummel | c6eb7b7 | 2005-09-03 15:57:58 -0700 | [diff] [blame] | 908 | if (devmap->device) |
| 909 | devmap->device->features = devmap->features; |
Horst Hummel | f24acd4 | 2005-05-01 08:58:59 -0700 | [diff] [blame] | 910 | spin_unlock(&dasd_devmap_lock); |
| 911 | return 0; |
| 912 | } |
| 913 | |
| 914 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | int |
| 916 | dasd_add_sysfs_files(struct ccw_device *cdev) |
| 917 | { |
| 918 | return sysfs_create_group(&cdev->dev.kobj, &dasd_attr_group); |
| 919 | } |
| 920 | |
| 921 | void |
| 922 | dasd_remove_sysfs_files(struct ccw_device *cdev) |
| 923 | { |
| 924 | sysfs_remove_group(&cdev->dev.kobj, &dasd_attr_group); |
| 925 | } |
| 926 | |
| 927 | |
| 928 | int |
| 929 | dasd_devmap_init(void) |
| 930 | { |
| 931 | int i; |
| 932 | |
| 933 | /* Initialize devmap structures. */ |
| 934 | dasd_max_devindex = 0; |
| 935 | for (i = 0; i < 256; i++) |
| 936 | INIT_LIST_HEAD(&dasd_hashlists[i]); |
| 937 | return 0; |
| 938 | |
| 939 | } |
| 940 | |
| 941 | void |
| 942 | dasd_devmap_exit(void) |
| 943 | { |
| 944 | dasd_forget_ranges(); |
| 945 | } |