linux/drivers/s390/block/dasd_devmap.c
<<
>>
Prefs
   1/*
   2 * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
   3 *                  Horst Hummel <Horst.Hummel@de.ibm.com>
   4 *                  Carsten Otte <Cotte@de.ibm.com>
   5 *                  Martin Schwidefsky <schwidefsky@de.ibm.com>
   6 * Bugreports.to..: <Linux390@de.ibm.com>
   7 * Copyright IBM Corp. 1999,2001
   8 *
   9 * Device mapping and dasd= parameter parsing functions. All devmap
  10 * functions may not be called from interrupt context. In particular
  11 * dasd_get_device is a no-no from interrupt context.
  12 *
  13 */
  14
  15#define KMSG_COMPONENT "dasd"
  16
  17#include <linux/ctype.h>
  18#include <linux/init.h>
  19#include <linux/module.h>
  20#include <linux/slab.h>
  21
  22#include <asm/debug.h>
  23#include <asm/uaccess.h>
  24#include <asm/ipl.h>
  25
  26/* This is ugly... */
  27#define PRINTK_HEADER "dasd_devmap:"
  28#define DASD_BUS_ID_SIZE 20
  29
  30#include "dasd_int.h"
  31
  32struct kmem_cache *dasd_page_cache;
  33EXPORT_SYMBOL_GPL(dasd_page_cache);
  34
  35/*
  36 * dasd_devmap_t is used to store the features and the relation
  37 * between device number and device index. To find a dasd_devmap_t
  38 * that corresponds to a device number of a device index each
  39 * dasd_devmap_t is added to two linked lists, one to search by
  40 * the device number and one to search by the device index. As
  41 * soon as big minor numbers are available the device index list
  42 * can be removed since the device number will then be identical
  43 * to the device index.
  44 */
  45struct dasd_devmap {
  46        struct list_head list;
  47        char bus_id[DASD_BUS_ID_SIZE];
  48        unsigned int devindex;
  49        unsigned short features;
  50        struct dasd_device *device;
  51};
  52
  53/*
  54 * Parameter parsing functions for dasd= parameter. The syntax is:
  55 *   <devno>            : (0x)?[0-9a-fA-F]+
  56 *   <busid>            : [0-0a-f]\.[0-9a-f]\.(0x)?[0-9a-fA-F]+
  57 *   <feature>          : ro
  58 *   <feature_list>     : \(<feature>(:<feature>)*\)
  59 *   <devno-range>      : <devno>(-<devno>)?<feature_list>?
  60 *   <busid-range>      : <busid>(-<busid>)?<feature_list>?
  61 *   <devices>          : <devno-range>|<busid-range>
  62 *   <dasd_module>      : dasd_diag_mod|dasd_eckd_mod|dasd_fba_mod
  63 *
  64 *   <dasd>             : autodetect|probeonly|<devices>(,<devices>)*
  65 */
  66
  67int dasd_probeonly =  0;        /* is true, when probeonly mode is active */
  68int dasd_autodetect = 0;        /* is true, when autodetection is active */
  69int dasd_nopav = 0;             /* is true, when PAV is disabled */
  70EXPORT_SYMBOL_GPL(dasd_nopav);
  71int dasd_nofcx;                 /* disable High Performance Ficon */
  72EXPORT_SYMBOL_GPL(dasd_nofcx);
  73
  74/*
  75 * char *dasd[] is intended to hold the ranges supplied by the dasd= statement
  76 * it is named 'dasd' to directly be filled by insmod with the comma separated
  77 * strings when running as a module.
  78 */
  79static char *dasd[256];
  80module_param_array(dasd, charp, NULL, 0);
  81
  82/*
  83 * Single spinlock to protect devmap and servermap structures and lists.
  84 */
  85static DEFINE_SPINLOCK(dasd_devmap_lock);
  86
  87/*
  88 * Hash lists for devmap structures.
  89 */
  90static struct list_head dasd_hashlists[256];
  91int dasd_max_devindex;
  92
  93static struct dasd_devmap *dasd_add_busid(const char *, int);
  94
  95static inline int
  96dasd_hash_busid(const char *bus_id)
  97{
  98        int hash, i;
  99
 100        hash = 0;
 101        for (i = 0; (i < DASD_BUS_ID_SIZE) && *bus_id; i++, bus_id++)
 102                hash += *bus_id;
 103        return hash & 0xff;
 104}
 105
 106#ifndef MODULE
 107/*
 108 * The parameter parsing functions for builtin-drivers are called
 109 * before kmalloc works. Store the pointers to the parameters strings
 110 * into dasd[] for later processing.
 111 */
 112static int __init
 113dasd_call_setup(char *str)
 114{
 115        static int count = 0;
 116
 117        if (count < 256)
 118                dasd[count++] = str;
 119        return 1;
 120}
 121
 122__setup ("dasd=", dasd_call_setup);
 123#endif  /* #ifndef MODULE */
 124
 125#define DASD_IPLDEV     "ipldev"
 126
 127/*
 128 * Read a device busid/devno from a string.
 129 */
 130static int
 131
 132dasd_busid(char **str, int *id0, int *id1, int *devno)
 133{
 134        int val, old_style;
 135
 136        /* Interpret ipldev busid */
 137        if (strncmp(DASD_IPLDEV, *str, strlen(DASD_IPLDEV)) == 0) {
 138                if (ipl_info.type != IPL_TYPE_CCW) {
 139                        pr_err("The IPL device is not a CCW device\n");
 140                        return -EINVAL;
 141                }
 142                *id0 = 0;
 143                *id1 = ipl_info.data.ccw.dev_id.ssid;
 144                *devno = ipl_info.data.ccw.dev_id.devno;
 145                *str += strlen(DASD_IPLDEV);
 146
 147                return 0;
 148        }
 149        /* check for leading '0x' */
 150        old_style = 0;
 151        if ((*str)[0] == '0' && (*str)[1] == 'x') {
 152                *str += 2;
 153                old_style = 1;
 154        }
 155        if (!isxdigit((*str)[0]))       /* We require at least one hex digit */
 156                return -EINVAL;
 157        val = simple_strtoul(*str, str, 16);
 158        if (old_style || (*str)[0] != '.') {
 159                *id0 = *id1 = 0;
 160                if (val < 0 || val > 0xffff)
 161                        return -EINVAL;
 162                *devno = val;
 163                return 0;
 164        }
 165        /* New style x.y.z busid */
 166        if (val < 0 || val > 0xff)
 167                return -EINVAL;
 168        *id0 = val;
 169        (*str)++;
 170        if (!isxdigit((*str)[0]))       /* We require at least one hex digit */
 171                return -EINVAL;
 172        val = simple_strtoul(*str, str, 16);
 173        if (val < 0 || val > 0xff || (*str)++[0] != '.')
 174                return -EINVAL;
 175        *id1 = val;
 176        if (!isxdigit((*str)[0]))       /* We require at least one hex digit */
 177                return -EINVAL;
 178        val = simple_strtoul(*str, str, 16);
 179        if (val < 0 || val > 0xffff)
 180                return -EINVAL;
 181        *devno = val;
 182        return 0;
 183}
 184
 185/*
 186 * Read colon separated list of dasd features. Currently there is
 187 * only one: "ro" for read-only devices. The default feature set
 188 * is empty (value 0).
 189 */
 190static int
 191dasd_feature_list(char *str, char **endp)
 192{
 193        int features, len, rc;
 194
 195        rc = 0;
 196        if (*str != '(') {
 197                *endp = str;
 198                return DASD_FEATURE_DEFAULT;
 199        }
 200        str++;
 201        features = 0;
 202
 203        while (1) {
 204                for (len = 0;
 205                     str[len] && str[len] != ':' && str[len] != ')'; len++);
 206                if (len == 2 && !strncmp(str, "ro", 2))
 207                        features |= DASD_FEATURE_READONLY;
 208                else if (len == 4 && !strncmp(str, "diag", 4))
 209                        features |= DASD_FEATURE_USEDIAG;
 210                else if (len == 3 && !strncmp(str, "raw", 3))
 211                        features |= DASD_FEATURE_USERAW;
 212                else if (len == 6 && !strncmp(str, "erplog", 6))
 213                        features |= DASD_FEATURE_ERPLOG;
 214                else if (len == 8 && !strncmp(str, "failfast", 8))
 215                        features |= DASD_FEATURE_FAILFAST;
 216                else {
 217                        pr_warning("%*s is not a supported device option\n",
 218                                   len, str);
 219                        rc = -EINVAL;
 220                }
 221                str += len;
 222                if (*str != ':')
 223                        break;
 224                str++;
 225        }
 226        if (*str != ')') {
 227                pr_warning("A closing parenthesis ')' is missing in the "
 228                           "dasd= parameter\n");
 229                rc = -EINVAL;
 230        } else
 231                str++;
 232        *endp = str;
 233        if (rc != 0)
 234                return rc;
 235        return features;
 236}
 237
 238/*
 239 * Try to match the first element on the comma separated parse string
 240 * with one of the known keywords. If a keyword is found, take the approprate
 241 * action and return a pointer to the residual string. If the first element
 242 * could not be matched to any keyword then return an error code.
 243 */
 244static char *
 245dasd_parse_keyword( char *parsestring ) {
 246
 247        char *nextcomma, *residual_str;
 248        int length;
 249
 250        nextcomma = strchr(parsestring,',');
 251        if (nextcomma) {
 252                length = nextcomma - parsestring;
 253                residual_str = nextcomma + 1;
 254        } else {
 255                length = strlen(parsestring);
 256                residual_str = parsestring + length;
 257        }
 258        if (strncmp("autodetect", parsestring, length) == 0) {
 259                dasd_autodetect = 1;
 260                pr_info("The autodetection mode has been activated\n");
 261                return residual_str;
 262        }
 263        if (strncmp("probeonly", parsestring, length) == 0) {
 264                dasd_probeonly = 1;
 265                pr_info("The probeonly mode has been activated\n");
 266                return residual_str;
 267        }
 268        if (strncmp("nopav", parsestring, length) == 0) {
 269                if (MACHINE_IS_VM)
 270                        pr_info("'nopav' is not supported on z/VM\n");
 271                else {
 272                        dasd_nopav = 1;
 273                        pr_info("PAV support has be deactivated\n");
 274                }
 275                return residual_str;
 276        }
 277        if (strncmp("nofcx", parsestring, length) == 0) {
 278                dasd_nofcx = 1;
 279                pr_info("High Performance FICON support has been "
 280                        "deactivated\n");
 281                return residual_str;
 282        }
 283        if (strncmp("fixedbuffers", parsestring, length) == 0) {
 284                if (dasd_page_cache)
 285                        return residual_str;
 286                dasd_page_cache =
 287                        kmem_cache_create("dasd_page_cache", PAGE_SIZE,
 288                                          PAGE_SIZE, SLAB_CACHE_DMA,
 289                                          NULL);
 290                if (!dasd_page_cache)
 291                        DBF_EVENT(DBF_WARNING, "%s", "Failed to create slab, "
 292                                "fixed buffer mode disabled.");
 293                else
 294                        DBF_EVENT(DBF_INFO, "%s",
 295                                 "turning on fixed buffer mode");
 296                return residual_str;
 297        }
 298        return ERR_PTR(-EINVAL);
 299}
 300
 301/*
 302 * Try to interprete the first element on the comma separated parse string
 303 * as a device number or a range of devices. If the interpretation is
 304 * successful, create the matching dasd_devmap entries and return a pointer
 305 * to the residual string.
 306 * If interpretation fails or in case of an error, return an error code.
 307 */
 308static char *
 309dasd_parse_range( char *parsestring ) {
 310
 311        struct dasd_devmap *devmap;
 312        int from, from_id0, from_id1;
 313        int to, to_id0, to_id1;
 314        int features, rc;
 315        char bus_id[DASD_BUS_ID_SIZE+1], *str;
 316
 317        str = parsestring;
 318        rc = dasd_busid(&str, &from_id0, &from_id1, &from);
 319        if (rc == 0) {
 320                to = from;
 321                to_id0 = from_id0;
 322                to_id1 = from_id1;
 323                if (*str == '-') {
 324                        str++;
 325                        rc = dasd_busid(&str, &to_id0, &to_id1, &to);
 326                }
 327        }
 328        if (rc == 0 &&
 329            (from_id0 != to_id0 || from_id1 != to_id1 || from > to))
 330                rc = -EINVAL;
 331        if (rc) {
 332                pr_err("%s is not a valid device range\n", parsestring);
 333                return ERR_PTR(rc);
 334        }
 335        features = dasd_feature_list(str, &str);
 336        if (features < 0)
 337                return ERR_PTR(-EINVAL);
 338        /* each device in dasd= parameter should be set initially online */
 339        features |= DASD_FEATURE_INITIAL_ONLINE;
 340        while (from <= to) {
 341                sprintf(bus_id, "%01x.%01x.%04x",
 342                        from_id0, from_id1, from++);
 343                devmap = dasd_add_busid(bus_id, features);
 344                if (IS_ERR(devmap))
 345                        return (char *)devmap;
 346        }
 347        if (*str == ',')
 348                return str + 1;
 349        if (*str == '\0')
 350                return str;
 351        pr_warning("The dasd= parameter value %s has an invalid ending\n",
 352                   str);
 353        return ERR_PTR(-EINVAL);
 354}
 355
 356static char *
 357dasd_parse_next_element( char *parsestring ) {
 358        char * residual_str;
 359        residual_str = dasd_parse_keyword(parsestring);
 360        if (!IS_ERR(residual_str))
 361                return residual_str;
 362        residual_str = dasd_parse_range(parsestring);
 363        return residual_str;
 364}
 365
 366/*
 367 * Parse parameters stored in dasd[]
 368 * The 'dasd=...' parameter allows to specify a comma separated list of
 369 * keywords and device ranges. When the dasd driver is build into the kernel,
 370 * the complete list will be stored as one element of the dasd[] array.
 371 * When the dasd driver is build as a module, then the list is broken into
 372 * it's elements and each dasd[] entry contains one element.
 373 */
 374int
 375dasd_parse(void)
 376{
 377        int rc, i;
 378        char *parsestring;
 379
 380        rc = 0;
 381        for (i = 0; i < 256; i++) {
 382                if (dasd[i] == NULL)
 383                        break;
 384                parsestring = dasd[i];
 385                /* loop over the comma separated list in the parsestring */
 386                while (*parsestring) {
 387                        parsestring = dasd_parse_next_element(parsestring);
 388                        if(IS_ERR(parsestring)) {
 389                                rc = PTR_ERR(parsestring);
 390                                break;
 391                        }
 392                }
 393                if (rc) {
 394                        DBF_EVENT(DBF_ALERT, "%s", "invalid range found");
 395                        break;
 396                }
 397        }
 398        return rc;
 399}
 400
 401/*
 402 * Add a devmap for the device specified by busid. It is possible that
 403 * the devmap already exists (dasd= parameter). The order of the devices
 404 * added through this function will define the kdevs for the individual
 405 * devices.
 406 */
 407static struct dasd_devmap *
 408dasd_add_busid(const char *bus_id, int features)
 409{
 410        struct dasd_devmap *devmap, *new, *tmp;
 411        int hash;
 412
 413        new = kzalloc(sizeof(struct dasd_devmap), GFP_KERNEL);
 414        if (!new)
 415                return ERR_PTR(-ENOMEM);
 416        spin_lock(&dasd_devmap_lock);
 417        devmap = NULL;
 418        hash = dasd_hash_busid(bus_id);
 419        list_for_each_entry(tmp, &dasd_hashlists[hash], list)
 420                if (strncmp(tmp->bus_id, bus_id, DASD_BUS_ID_SIZE) == 0) {
 421                        devmap = tmp;
 422                        break;
 423                }
 424        if (!devmap) {
 425                /* This bus_id is new. */
 426                new->devindex = dasd_max_devindex++;
 427                strncpy(new->bus_id, bus_id, DASD_BUS_ID_SIZE);
 428                new->features = features;
 429                new->device = NULL;
 430                list_add(&new->list, &dasd_hashlists[hash]);
 431                devmap = new;
 432                new = NULL;
 433        }
 434        spin_unlock(&dasd_devmap_lock);
 435        kfree(new);
 436        return devmap;
 437}
 438
 439/*
 440 * Find devmap for device with given bus_id.
 441 */
 442static struct dasd_devmap *
 443dasd_find_busid(const char *bus_id)
 444{
 445        struct dasd_devmap *devmap, *tmp;
 446        int hash;
 447
 448        spin_lock(&dasd_devmap_lock);
 449        devmap = ERR_PTR(-ENODEV);
 450        hash = dasd_hash_busid(bus_id);
 451        list_for_each_entry(tmp, &dasd_hashlists[hash], list) {
 452                if (strncmp(tmp->bus_id, bus_id, DASD_BUS_ID_SIZE) == 0) {
 453                        devmap = tmp;
 454                        break;
 455                }
 456        }
 457        spin_unlock(&dasd_devmap_lock);
 458        return devmap;
 459}
 460
 461/*
 462 * Check if busid has been added to the list of dasd ranges.
 463 */
 464int
 465dasd_busid_known(const char *bus_id)
 466{
 467        return IS_ERR(dasd_find_busid(bus_id)) ? -ENOENT : 0;
 468}
 469
 470/*
 471 * Forget all about the device numbers added so far.
 472 * This may only be called at module unload or system shutdown.
 473 */
 474static void
 475dasd_forget_ranges(void)
 476{
 477        struct dasd_devmap *devmap, *n;
 478        int i;
 479
 480        spin_lock(&dasd_devmap_lock);
 481        for (i = 0; i < 256; i++) {
 482                list_for_each_entry_safe(devmap, n, &dasd_hashlists[i], list) {
 483                        BUG_ON(devmap->device != NULL);
 484                        list_del(&devmap->list);
 485                        kfree(devmap);
 486                }
 487        }
 488        spin_unlock(&dasd_devmap_lock);
 489}
 490
 491/*
 492 * Find the device struct by its device index.
 493 */
 494struct dasd_device *
 495dasd_device_from_devindex(int devindex)
 496{
 497        struct dasd_devmap *devmap, *tmp;
 498        struct dasd_device *device;
 499        int i;
 500
 501        spin_lock(&dasd_devmap_lock);
 502        devmap = NULL;
 503        for (i = 0; (i < 256) && !devmap; i++)
 504                list_for_each_entry(tmp, &dasd_hashlists[i], list)
 505                        if (tmp->devindex == devindex) {
 506                                /* Found the devmap for the device. */
 507                                devmap = tmp;
 508                                break;
 509                        }
 510        if (devmap && devmap->device) {
 511                device = devmap->device;
 512                dasd_get_device(device);
 513        } else
 514                device = ERR_PTR(-ENODEV);
 515        spin_unlock(&dasd_devmap_lock);
 516        return device;
 517}
 518
 519/*
 520 * Return devmap for cdev. If no devmap exists yet, create one and
 521 * connect it to the cdev.
 522 */
 523static struct dasd_devmap *
 524dasd_devmap_from_cdev(struct ccw_device *cdev)
 525{
 526        struct dasd_devmap *devmap;
 527
 528        devmap = dasd_find_busid(dev_name(&cdev->dev));
 529        if (IS_ERR(devmap))
 530                devmap = dasd_add_busid(dev_name(&cdev->dev),
 531                                        DASD_FEATURE_DEFAULT);
 532        return devmap;
 533}
 534
 535/*
 536 * Create a dasd device structure for cdev.
 537 */
 538struct dasd_device *
 539dasd_create_device(struct ccw_device *cdev)
 540{
 541        struct dasd_devmap *devmap;
 542        struct dasd_device *device;
 543        unsigned long flags;
 544        int rc;
 545
 546        devmap = dasd_devmap_from_cdev(cdev);
 547        if (IS_ERR(devmap))
 548                return (void *) devmap;
 549
 550        device = dasd_alloc_device();
 551        if (IS_ERR(device))
 552                return device;
 553        atomic_set(&device->ref_count, 3);
 554
 555        spin_lock(&dasd_devmap_lock);
 556        if (!devmap->device) {
 557                devmap->device = device;
 558                device->devindex = devmap->devindex;
 559                device->features = devmap->features;
 560                get_device(&cdev->dev);
 561                device->cdev = cdev;
 562                rc = 0;
 563        } else
 564                /* Someone else was faster. */
 565                rc = -EBUSY;
 566        spin_unlock(&dasd_devmap_lock);
 567
 568        if (rc) {
 569                dasd_free_device(device);
 570                return ERR_PTR(rc);
 571        }
 572
 573        spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
 574        dev_set_drvdata(&cdev->dev, device);
 575        spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
 576
 577        return device;
 578}
 579
 580/*
 581 * Wait queue for dasd_delete_device waits.
 582 */
 583static DECLARE_WAIT_QUEUE_HEAD(dasd_delete_wq);
 584
 585/*
 586 * Remove a dasd device structure. The passed referenced
 587 * is destroyed.
 588 */
 589void
 590dasd_delete_device(struct dasd_device *device)
 591{
 592        struct ccw_device *cdev;
 593        struct dasd_devmap *devmap;
 594        unsigned long flags;
 595
 596        /* First remove device pointer from devmap. */
 597        devmap = dasd_find_busid(dev_name(&device->cdev->dev));
 598        BUG_ON(IS_ERR(devmap));
 599        spin_lock(&dasd_devmap_lock);
 600        if (devmap->device != device) {
 601                spin_unlock(&dasd_devmap_lock);
 602                dasd_put_device(device);
 603                return;
 604        }
 605        devmap->device = NULL;
 606        spin_unlock(&dasd_devmap_lock);
 607
 608        /* Disconnect dasd_device structure from ccw_device structure. */
 609        spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
 610        dev_set_drvdata(&device->cdev->dev, NULL);
 611        spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
 612
 613        /*
 614         * Drop ref_count by 3, one for the devmap reference, one for
 615         * the cdev reference and one for the passed reference.
 616         */
 617        atomic_sub(3, &device->ref_count);
 618
 619        /* Wait for reference counter to drop to zero. */
 620        wait_event(dasd_delete_wq, atomic_read(&device->ref_count) == 0);
 621
 622        /* Disconnect dasd_device structure from ccw_device structure. */
 623        cdev = device->cdev;
 624        device->cdev = NULL;
 625
 626        /* Put ccw_device structure. */
 627        put_device(&cdev->dev);
 628
 629        /* Now the device structure can be freed. */
 630        dasd_free_device(device);
 631}
 632
 633/*
 634 * Reference counter dropped to zero. Wake up waiter
 635 * in dasd_delete_device.
 636 */
 637void
 638dasd_put_device_wake(struct dasd_device *device)
 639{
 640        wake_up(&dasd_delete_wq);
 641}
 642EXPORT_SYMBOL_GPL(dasd_put_device_wake);
 643
 644/*
 645 * Return dasd_device structure associated with cdev.
 646 * This function needs to be called with the ccw device
 647 * lock held. It can be used from interrupt context.
 648 */
 649struct dasd_device *
 650dasd_device_from_cdev_locked(struct ccw_device *cdev)
 651{
 652        struct dasd_device *device = dev_get_drvdata(&cdev->dev);
 653
 654        if (!device)
 655                return ERR_PTR(-ENODEV);
 656        dasd_get_device(device);
 657        return device;
 658}
 659
 660/*
 661 * Return dasd_device structure associated with cdev.
 662 */
 663struct dasd_device *
 664dasd_device_from_cdev(struct ccw_device *cdev)
 665{
 666        struct dasd_device *device;
 667        unsigned long flags;
 668
 669        spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
 670        device = dasd_device_from_cdev_locked(cdev);
 671        spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
 672        return device;
 673}
 674
 675void dasd_add_link_to_gendisk(struct gendisk *gdp, struct dasd_device *device)
 676{
 677        struct dasd_devmap *devmap;
 678
 679        devmap = dasd_find_busid(dev_name(&device->cdev->dev));
 680        if (IS_ERR(devmap))
 681                return;
 682        spin_lock(&dasd_devmap_lock);
 683        gdp->private_data = devmap;
 684        spin_unlock(&dasd_devmap_lock);
 685}
 686
 687struct dasd_device *dasd_device_from_gendisk(struct gendisk *gdp)
 688{
 689        struct dasd_device *device;
 690        struct dasd_devmap *devmap;
 691
 692        if (!gdp->private_data)
 693                return NULL;
 694        device = NULL;
 695        spin_lock(&dasd_devmap_lock);
 696        devmap = gdp->private_data;
 697        if (devmap && devmap->device) {
 698                device = devmap->device;
 699                dasd_get_device(device);
 700        }
 701        spin_unlock(&dasd_devmap_lock);
 702        return device;
 703}
 704
 705/*
 706 * SECTION: files in sysfs
 707 */
 708
 709/*
 710 * failfast controls the behaviour, if no path is available
 711 */
 712static ssize_t dasd_ff_show(struct device *dev, struct device_attribute *attr,
 713                            char *buf)
 714{
 715        struct dasd_devmap *devmap;
 716        int ff_flag;
 717
 718        devmap = dasd_find_busid(dev_name(dev));
 719        if (!IS_ERR(devmap))
 720                ff_flag = (devmap->features & DASD_FEATURE_FAILFAST) != 0;
 721        else
 722                ff_flag = (DASD_FEATURE_DEFAULT & DASD_FEATURE_FAILFAST) != 0;
 723        return snprintf(buf, PAGE_SIZE, ff_flag ? "1\n" : "0\n");
 724}
 725
 726static ssize_t dasd_ff_store(struct device *dev, struct device_attribute *attr,
 727              const char *buf, size_t count)
 728{
 729        struct dasd_devmap *devmap;
 730        int val;
 731        char *endp;
 732
 733        devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
 734        if (IS_ERR(devmap))
 735                return PTR_ERR(devmap);
 736
 737        val = simple_strtoul(buf, &endp, 0);
 738        if (((endp + 1) < (buf + count)) || (val > 1))
 739                return -EINVAL;
 740
 741        spin_lock(&dasd_devmap_lock);
 742        if (val)
 743                devmap->features |= DASD_FEATURE_FAILFAST;
 744        else
 745                devmap->features &= ~DASD_FEATURE_FAILFAST;
 746        if (devmap->device)
 747                devmap->device->features = devmap->features;
 748        spin_unlock(&dasd_devmap_lock);
 749        return count;
 750}
 751
 752static DEVICE_ATTR(failfast, 0644, dasd_ff_show, dasd_ff_store);
 753
 754/*
 755 * readonly controls the readonly status of a dasd
 756 */
 757static ssize_t
 758dasd_ro_show(struct device *dev, struct device_attribute *attr, char *buf)
 759{
 760        struct dasd_devmap *devmap;
 761        int ro_flag;
 762
 763        devmap = dasd_find_busid(dev_name(dev));
 764        if (!IS_ERR(devmap))
 765                ro_flag = (devmap->features & DASD_FEATURE_READONLY) != 0;
 766        else
 767                ro_flag = (DASD_FEATURE_DEFAULT & DASD_FEATURE_READONLY) != 0;
 768        return snprintf(buf, PAGE_SIZE, ro_flag ? "1\n" : "0\n");
 769}
 770
 771static ssize_t
 772dasd_ro_store(struct device *dev, struct device_attribute *attr,
 773              const char *buf, size_t count)
 774{
 775        struct dasd_devmap *devmap;
 776        struct dasd_device *device;
 777        int val;
 778        char *endp;
 779
 780        devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
 781        if (IS_ERR(devmap))
 782                return PTR_ERR(devmap);
 783
 784        val = simple_strtoul(buf, &endp, 0);
 785        if (((endp + 1) < (buf + count)) || (val > 1))
 786                return -EINVAL;
 787
 788        spin_lock(&dasd_devmap_lock);
 789        if (val)
 790                devmap->features |= DASD_FEATURE_READONLY;
 791        else
 792                devmap->features &= ~DASD_FEATURE_READONLY;
 793        device = devmap->device;
 794        if (device) {
 795                device->features = devmap->features;
 796                val = val || test_bit(DASD_FLAG_DEVICE_RO, &device->flags);
 797        }
 798        spin_unlock(&dasd_devmap_lock);
 799        if (device && device->block && device->block->gdp)
 800                set_disk_ro(device->block->gdp, val);
 801        return count;
 802}
 803
 804static DEVICE_ATTR(readonly, 0644, dasd_ro_show, dasd_ro_store);
 805/*
 806 * erplog controls the logging of ERP related data
 807 * (e.g. failing channel programs).
 808 */
 809static ssize_t
 810dasd_erplog_show(struct device *dev, struct device_attribute *attr, char *buf)
 811{
 812        struct dasd_devmap *devmap;
 813        int erplog;
 814
 815        devmap = dasd_find_busid(dev_name(dev));
 816        if (!IS_ERR(devmap))
 817                erplog = (devmap->features & DASD_FEATURE_ERPLOG) != 0;
 818        else
 819                erplog = (DASD_FEATURE_DEFAULT & DASD_FEATURE_ERPLOG) != 0;
 820        return snprintf(buf, PAGE_SIZE, erplog ? "1\n" : "0\n");
 821}
 822
 823static ssize_t
 824dasd_erplog_store(struct device *dev, struct device_attribute *attr,
 825              const char *buf, size_t count)
 826{
 827        struct dasd_devmap *devmap;
 828        int val;
 829        char *endp;
 830
 831        devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
 832        if (IS_ERR(devmap))
 833                return PTR_ERR(devmap);
 834
 835        val = simple_strtoul(buf, &endp, 0);
 836        if (((endp + 1) < (buf + count)) || (val > 1))
 837                return -EINVAL;
 838
 839        spin_lock(&dasd_devmap_lock);
 840        if (val)
 841                devmap->features |= DASD_FEATURE_ERPLOG;
 842        else
 843                devmap->features &= ~DASD_FEATURE_ERPLOG;
 844        if (devmap->device)
 845                devmap->device->features = devmap->features;
 846        spin_unlock(&dasd_devmap_lock);
 847        return count;
 848}
 849
 850static DEVICE_ATTR(erplog, 0644, dasd_erplog_show, dasd_erplog_store);
 851
 852/*
 853 * use_diag controls whether the driver should use diag rather than ssch
 854 * to talk to the device
 855 */
 856static ssize_t
 857dasd_use_diag_show(struct device *dev, struct device_attribute *attr, char *buf)
 858{
 859        struct dasd_devmap *devmap;
 860        int use_diag;
 861
 862        devmap = dasd_find_busid(dev_name(dev));
 863        if (!IS_ERR(devmap))
 864                use_diag = (devmap->features & DASD_FEATURE_USEDIAG) != 0;
 865        else
 866                use_diag = (DASD_FEATURE_DEFAULT & DASD_FEATURE_USEDIAG) != 0;
 867        return sprintf(buf, use_diag ? "1\n" : "0\n");
 868}
 869
 870static ssize_t
 871dasd_use_diag_store(struct device *dev, struct device_attribute *attr,
 872                    const char *buf, size_t count)
 873{
 874        struct dasd_devmap *devmap;
 875        ssize_t rc;
 876        int val;
 877        char *endp;
 878
 879        devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
 880        if (IS_ERR(devmap))
 881                return PTR_ERR(devmap);
 882
 883        val = simple_strtoul(buf, &endp, 0);
 884        if (((endp + 1) < (buf + count)) || (val > 1))
 885                return -EINVAL;
 886
 887        spin_lock(&dasd_devmap_lock);
 888        /* Changing diag discipline flag is only allowed in offline state. */
 889        rc = count;
 890        if (!devmap->device && !(devmap->features & DASD_FEATURE_USERAW)) {
 891                if (val)
 892                        devmap->features |= DASD_FEATURE_USEDIAG;
 893                else
 894                        devmap->features &= ~DASD_FEATURE_USEDIAG;
 895        } else
 896                rc = -EPERM;
 897        spin_unlock(&dasd_devmap_lock);
 898        return rc;
 899}
 900
 901static DEVICE_ATTR(use_diag, 0644, dasd_use_diag_show, dasd_use_diag_store);
 902
 903/*
 904 * use_raw controls whether the driver should give access to raw eckd data or
 905 * operate in standard mode
 906 */
 907static ssize_t
 908dasd_use_raw_show(struct device *dev, struct device_attribute *attr, char *buf)
 909{
 910        struct dasd_devmap *devmap;
 911        int use_raw;
 912
 913        devmap = dasd_find_busid(dev_name(dev));
 914        if (!IS_ERR(devmap))
 915                use_raw = (devmap->features & DASD_FEATURE_USERAW) != 0;
 916        else
 917                use_raw = (DASD_FEATURE_DEFAULT & DASD_FEATURE_USERAW) != 0;
 918        return sprintf(buf, use_raw ? "1\n" : "0\n");
 919}
 920
 921static ssize_t
 922dasd_use_raw_store(struct device *dev, struct device_attribute *attr,
 923                    const char *buf, size_t count)
 924{
 925        struct dasd_devmap *devmap;
 926        ssize_t rc;
 927        unsigned long val;
 928
 929        devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
 930        if (IS_ERR(devmap))
 931                return PTR_ERR(devmap);
 932
 933        if ((strict_strtoul(buf, 10, &val) != 0) || val > 1)
 934                return -EINVAL;
 935
 936        spin_lock(&dasd_devmap_lock);
 937        /* Changing diag discipline flag is only allowed in offline state. */
 938        rc = count;
 939        if (!devmap->device && !(devmap->features & DASD_FEATURE_USEDIAG)) {
 940                if (val)
 941                        devmap->features |= DASD_FEATURE_USERAW;
 942                else
 943                        devmap->features &= ~DASD_FEATURE_USERAW;
 944        } else
 945                rc = -EPERM;
 946        spin_unlock(&dasd_devmap_lock);
 947        return rc;
 948}
 949
 950static DEVICE_ATTR(raw_track_access, 0644, dasd_use_raw_show,
 951                   dasd_use_raw_store);
 952
 953static ssize_t
 954dasd_safe_offline_store(struct device *dev, struct device_attribute *attr,
 955                        const char *buf, size_t count)
 956{
 957        struct ccw_device *cdev = to_ccwdev(dev);
 958        struct dasd_device *device;
 959        int rc;
 960
 961        device = dasd_device_from_cdev(cdev);
 962        if (IS_ERR(device)) {
 963                rc = PTR_ERR(device);
 964                goto out;
 965        }
 966
 967        if (test_bit(DASD_FLAG_OFFLINE, &device->flags) ||
 968            test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
 969                /* Already doing offline processing */
 970                dasd_put_device(device);
 971                rc = -EBUSY;
 972                goto out;
 973        }
 974
 975        set_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags);
 976        dasd_put_device(device);
 977
 978        rc = ccw_device_set_offline(cdev);
 979
 980out:
 981        return rc ? rc : count;
 982}
 983
 984static DEVICE_ATTR(safe_offline, 0200, NULL, dasd_safe_offline_store);
 985
 986static ssize_t
 987dasd_access_show(struct device *dev, struct device_attribute *attr,
 988                 char *buf)
 989{
 990        struct ccw_device *cdev = to_ccwdev(dev);
 991        struct dasd_device *device;
 992        int count;
 993
 994        device = dasd_device_from_cdev(cdev);
 995        if (IS_ERR(device))
 996                return PTR_ERR(device);
 997
 998        if (!device->discipline)
 999                count = -ENODEV;
1000        else if (!device->discipline->host_access_count)
1001                count = -EOPNOTSUPP;
1002        else
1003                count = device->discipline->host_access_count(device);
1004
1005        dasd_put_device(device);
1006        if (count < 0)
1007                return count;
1008
1009        return sprintf(buf, "%d\n", count);
1010}
1011
1012static DEVICE_ATTR(host_access_count, 0444, dasd_access_show, NULL);
1013
1014static ssize_t
1015dasd_discipline_show(struct device *dev, struct device_attribute *attr,
1016                     char *buf)
1017{
1018        struct dasd_device *device;
1019        ssize_t len;
1020
1021        device = dasd_device_from_cdev(to_ccwdev(dev));
1022        if (IS_ERR(device))
1023                goto out;
1024        else if (!device->discipline) {
1025                dasd_put_device(device);
1026                goto out;
1027        } else {
1028                len = snprintf(buf, PAGE_SIZE, "%s\n",
1029                               device->discipline->name);
1030                dasd_put_device(device);
1031                return len;
1032        }
1033out:
1034        len = snprintf(buf, PAGE_SIZE, "none\n");
1035        return len;
1036}
1037
1038static DEVICE_ATTR(discipline, 0444, dasd_discipline_show, NULL);
1039
1040static ssize_t
1041dasd_device_status_show(struct device *dev, struct device_attribute *attr,
1042                     char *buf)
1043{
1044        struct dasd_device *device;
1045        ssize_t len;
1046
1047        device = dasd_device_from_cdev(to_ccwdev(dev));
1048        if (!IS_ERR(device)) {
1049                switch (device->state) {
1050                case DASD_STATE_NEW:
1051                        len = snprintf(buf, PAGE_SIZE, "new\n");
1052                        break;
1053                case DASD_STATE_KNOWN:
1054                        len = snprintf(buf, PAGE_SIZE, "detected\n");
1055                        break;
1056                case DASD_STATE_BASIC:
1057                        len = snprintf(buf, PAGE_SIZE, "basic\n");
1058                        break;
1059                case DASD_STATE_UNFMT:
1060                        len = snprintf(buf, PAGE_SIZE, "unformatted\n");
1061                        break;
1062                case DASD_STATE_READY:
1063                        len = snprintf(buf, PAGE_SIZE, "ready\n");
1064                        break;
1065                case DASD_STATE_ONLINE:
1066                        len = snprintf(buf, PAGE_SIZE, "online\n");
1067                        break;
1068                default:
1069                        len = snprintf(buf, PAGE_SIZE, "no stat\n");
1070                        break;
1071                }
1072                dasd_put_device(device);
1073        } else
1074                len = snprintf(buf, PAGE_SIZE, "unknown\n");
1075        return len;
1076}
1077
1078static DEVICE_ATTR(status, 0444, dasd_device_status_show, NULL);
1079
1080static ssize_t dasd_alias_show(struct device *dev,
1081                               struct device_attribute *attr, char *buf)
1082{
1083        struct dasd_device *device;
1084        struct dasd_uid uid;
1085
1086        device = dasd_device_from_cdev(to_ccwdev(dev));
1087        if (IS_ERR(device))
1088                return sprintf(buf, "0\n");
1089
1090        if (device->discipline && device->discipline->get_uid &&
1091            !device->discipline->get_uid(device, &uid)) {
1092                if (uid.type == UA_BASE_PAV_ALIAS ||
1093                    uid.type == UA_HYPER_PAV_ALIAS) {
1094                        dasd_put_device(device);
1095                        return sprintf(buf, "1\n");
1096                }
1097        }
1098        dasd_put_device(device);
1099
1100        return sprintf(buf, "0\n");
1101}
1102
1103static DEVICE_ATTR(alias, 0444, dasd_alias_show, NULL);
1104
1105static ssize_t dasd_vendor_show(struct device *dev,
1106                                struct device_attribute *attr, char *buf)
1107{
1108        struct dasd_device *device;
1109        struct dasd_uid uid;
1110        char *vendor;
1111
1112        device = dasd_device_from_cdev(to_ccwdev(dev));
1113        vendor = "";
1114        if (IS_ERR(device))
1115                return snprintf(buf, PAGE_SIZE, "%s\n", vendor);
1116
1117        if (device->discipline && device->discipline->get_uid &&
1118            !device->discipline->get_uid(device, &uid))
1119                        vendor = uid.vendor;
1120
1121        dasd_put_device(device);
1122
1123        return snprintf(buf, PAGE_SIZE, "%s\n", vendor);
1124}
1125
1126static DEVICE_ATTR(vendor, 0444, dasd_vendor_show, NULL);
1127
1128#define UID_STRLEN ( /* vendor */ 3 + 1 + /* serial    */ 14 + 1 +\
1129                     /* SSID   */ 4 + 1 + /* unit addr */ 2 + 1 +\
1130                     /* vduit */ 32 + 1)
1131
1132static ssize_t
1133dasd_uid_show(struct device *dev, struct device_attribute *attr, char *buf)
1134{
1135        struct dasd_device *device;
1136        struct dasd_uid uid;
1137        char uid_string[UID_STRLEN];
1138        char ua_string[3];
1139
1140        device = dasd_device_from_cdev(to_ccwdev(dev));
1141        uid_string[0] = 0;
1142        if (IS_ERR(device))
1143                return snprintf(buf, PAGE_SIZE, "%s\n", uid_string);
1144
1145        if (device->discipline && device->discipline->get_uid &&
1146            !device->discipline->get_uid(device, &uid)) {
1147                switch (uid.type) {
1148                case UA_BASE_DEVICE:
1149                        snprintf(ua_string, sizeof(ua_string), "%02x",
1150                                 uid.real_unit_addr);
1151                        break;
1152                case UA_BASE_PAV_ALIAS:
1153                        snprintf(ua_string, sizeof(ua_string), "%02x",
1154                                 uid.base_unit_addr);
1155                        break;
1156                case UA_HYPER_PAV_ALIAS:
1157                        snprintf(ua_string, sizeof(ua_string), "xx");
1158                        break;
1159                default:
1160                        /* should not happen, treat like base device */
1161                        snprintf(ua_string, sizeof(ua_string), "%02x",
1162                                 uid.real_unit_addr);
1163                        break;
1164                }
1165
1166                if (strlen(uid.vduit) > 0)
1167                        snprintf(uid_string, sizeof(uid_string),
1168                                 "%s.%s.%04x.%s.%s",
1169                                 uid.vendor, uid.serial, uid.ssid, ua_string,
1170                                 uid.vduit);
1171                else
1172                        snprintf(uid_string, sizeof(uid_string),
1173                                 "%s.%s.%04x.%s",
1174                                 uid.vendor, uid.serial, uid.ssid, ua_string);
1175        }
1176        dasd_put_device(device);
1177
1178        return snprintf(buf, PAGE_SIZE, "%s\n", uid_string);
1179}
1180static DEVICE_ATTR(uid, 0444, dasd_uid_show, NULL);
1181
1182/*
1183 * extended error-reporting
1184 */
1185static ssize_t
1186dasd_eer_show(struct device *dev, struct device_attribute *attr, char *buf)
1187{
1188        struct dasd_devmap *devmap;
1189        int eer_flag;
1190
1191        devmap = dasd_find_busid(dev_name(dev));
1192        if (!IS_ERR(devmap) && devmap->device)
1193                eer_flag = dasd_eer_enabled(devmap->device);
1194        else
1195                eer_flag = 0;
1196        return snprintf(buf, PAGE_SIZE, eer_flag ? "1\n" : "0\n");
1197}
1198
1199static ssize_t
1200dasd_eer_store(struct device *dev, struct device_attribute *attr,
1201               const char *buf, size_t count)
1202{
1203        struct dasd_devmap *devmap;
1204        int val, rc;
1205        char *endp;
1206
1207        devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
1208        if (IS_ERR(devmap))
1209                return PTR_ERR(devmap);
1210        if (!devmap->device)
1211                return -ENODEV;
1212
1213        val = simple_strtoul(buf, &endp, 0);
1214        if (((endp + 1) < (buf + count)) || (val > 1))
1215                return -EINVAL;
1216
1217        if (val) {
1218                rc = dasd_eer_enable(devmap->device);
1219                if (rc)
1220                        return rc;
1221        } else
1222                dasd_eer_disable(devmap->device);
1223        return count;
1224}
1225
1226static DEVICE_ATTR(eer_enabled, 0644, dasd_eer_show, dasd_eer_store);
1227
1228/*
1229 * expiration time for default requests
1230 */
1231static ssize_t
1232dasd_expires_show(struct device *dev, struct device_attribute *attr, char *buf)
1233{
1234        struct dasd_device *device;
1235        int len;
1236
1237        device = dasd_device_from_cdev(to_ccwdev(dev));
1238        if (IS_ERR(device))
1239                return -ENODEV;
1240        len = snprintf(buf, PAGE_SIZE, "%lu\n", device->default_expires);
1241        dasd_put_device(device);
1242        return len;
1243}
1244
1245static ssize_t
1246dasd_expires_store(struct device *dev, struct device_attribute *attr,
1247               const char *buf, size_t count)
1248{
1249        struct dasd_device *device;
1250        unsigned long val;
1251
1252        device = dasd_device_from_cdev(to_ccwdev(dev));
1253        if (IS_ERR(device))
1254                return -ENODEV;
1255
1256        if ((strict_strtoul(buf, 10, &val) != 0) ||
1257            (val > DASD_EXPIRES_MAX) || val == 0) {
1258                dasd_put_device(device);
1259                return -EINVAL;
1260        }
1261
1262        if (val)
1263                device->default_expires = val;
1264
1265        dasd_put_device(device);
1266        return count;
1267}
1268
1269static DEVICE_ATTR(expires, 0644, dasd_expires_show, dasd_expires_store);
1270
1271
1272static ssize_t
1273dasd_path_reset_store(struct device *dev, struct device_attribute *attr,
1274                      const char *buf, size_t count)
1275{
1276        struct dasd_device *device;
1277        unsigned int val;
1278
1279        device = dasd_device_from_cdev(to_ccwdev(dev));
1280        if (IS_ERR(device))
1281                return -ENODEV;
1282
1283        if ((kstrtouint(buf, 16, &val) != 0) || val > 0xff)
1284                val = 0;
1285
1286        if (device->discipline && device->discipline->reset_path)
1287                device->discipline->reset_path(device, (__u8) val);
1288
1289        dasd_put_device(device);
1290        return count;
1291}
1292
1293static DEVICE_ATTR(path_reset, 0200, NULL, dasd_path_reset_store);
1294
1295static ssize_t dasd_hpf_show(struct device *dev, struct device_attribute *attr,
1296                             char *buf)
1297{
1298        struct dasd_device *device;
1299        int hpf;
1300
1301        device = dasd_device_from_cdev(to_ccwdev(dev));
1302        if (IS_ERR(device))
1303                return -ENODEV;
1304        if (!device->discipline || !device->discipline->hpf_enabled) {
1305                dasd_put_device(device);
1306                return snprintf(buf, PAGE_SIZE, "%d\n", dasd_nofcx);
1307        }
1308        hpf = device->discipline->hpf_enabled(device);
1309        dasd_put_device(device);
1310        return snprintf(buf, PAGE_SIZE, "%d\n", hpf);
1311}
1312
1313static DEVICE_ATTR(hpf, 0444, dasd_hpf_show, NULL);
1314
1315static ssize_t dasd_reservation_policy_show(struct device *dev,
1316                                            struct device_attribute *attr,
1317                                            char *buf)
1318{
1319        struct dasd_devmap *devmap;
1320        int rc = 0;
1321
1322        devmap = dasd_find_busid(dev_name(dev));
1323        if (IS_ERR(devmap)) {
1324                rc = snprintf(buf, PAGE_SIZE, "ignore\n");
1325        } else {
1326                spin_lock(&dasd_devmap_lock);
1327                if (devmap->features & DASD_FEATURE_FAILONSLCK)
1328                        rc = snprintf(buf, PAGE_SIZE, "fail\n");
1329                else
1330                        rc = snprintf(buf, PAGE_SIZE, "ignore\n");
1331                spin_unlock(&dasd_devmap_lock);
1332        }
1333        return rc;
1334}
1335
1336static ssize_t dasd_reservation_policy_store(struct device *dev,
1337                                             struct device_attribute *attr,
1338                                             const char *buf, size_t count)
1339{
1340        struct dasd_devmap *devmap;
1341        int rc;
1342
1343        devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
1344        if (IS_ERR(devmap))
1345                return PTR_ERR(devmap);
1346        rc = 0;
1347        spin_lock(&dasd_devmap_lock);
1348        if (sysfs_streq("ignore", buf))
1349                devmap->features &= ~DASD_FEATURE_FAILONSLCK;
1350        else if (sysfs_streq("fail", buf))
1351                devmap->features |= DASD_FEATURE_FAILONSLCK;
1352        else
1353                rc = -EINVAL;
1354        if (devmap->device)
1355                devmap->device->features = devmap->features;
1356        spin_unlock(&dasd_devmap_lock);
1357        if (rc)
1358                return rc;
1359        else
1360                return count;
1361}
1362
1363static DEVICE_ATTR(reservation_policy, 0644,
1364                   dasd_reservation_policy_show, dasd_reservation_policy_store);
1365
1366static ssize_t dasd_reservation_state_show(struct device *dev,
1367                                           struct device_attribute *attr,
1368                                           char *buf)
1369{
1370        struct dasd_device *device;
1371        int rc = 0;
1372
1373        device = dasd_device_from_cdev(to_ccwdev(dev));
1374        if (IS_ERR(device))
1375                return snprintf(buf, PAGE_SIZE, "none\n");
1376
1377        if (test_bit(DASD_FLAG_IS_RESERVED, &device->flags))
1378                rc = snprintf(buf, PAGE_SIZE, "reserved\n");
1379        else if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags))
1380                rc = snprintf(buf, PAGE_SIZE, "lost\n");
1381        else
1382                rc = snprintf(buf, PAGE_SIZE, "none\n");
1383        dasd_put_device(device);
1384        return rc;
1385}
1386
1387static ssize_t dasd_reservation_state_store(struct device *dev,
1388                                            struct device_attribute *attr,
1389                                            const char *buf, size_t count)
1390{
1391        struct dasd_device *device;
1392        int rc = 0;
1393
1394        device = dasd_device_from_cdev(to_ccwdev(dev));
1395        if (IS_ERR(device))
1396                return -ENODEV;
1397        if (sysfs_streq("reset", buf))
1398                clear_bit(DASD_FLAG_LOCK_STOLEN, &device->flags);
1399        else
1400                rc = -EINVAL;
1401        dasd_put_device(device);
1402
1403        if (rc)
1404                return rc;
1405        else
1406                return count;
1407}
1408
1409static DEVICE_ATTR(last_known_reservation_state, 0644,
1410                   dasd_reservation_state_show, dasd_reservation_state_store);
1411
1412static ssize_t dasd_pm_show(struct device *dev,
1413                              struct device_attribute *attr, char *buf)
1414{
1415        struct dasd_device *device;
1416        u8 opm, nppm, cablepm, cuirpm, hpfpm, ifccpm;
1417
1418        device = dasd_device_from_cdev(to_ccwdev(dev));
1419        if (IS_ERR(device))
1420                return sprintf(buf, "0\n");
1421
1422        opm = dasd_path_get_opm(device);
1423        nppm = dasd_path_get_nppm(device);
1424        cablepm = dasd_path_get_cablepm(device);
1425        cuirpm = dasd_path_get_cuirpm(device);
1426        hpfpm = dasd_path_get_hpfpm(device);
1427        ifccpm = dasd_path_get_ifccpm(device);
1428        dasd_put_device(device);
1429
1430        return sprintf(buf, "%02x %02x %02x %02x %02x %02x\n", opm, nppm,
1431                       cablepm, cuirpm, hpfpm, ifccpm);
1432}
1433
1434static DEVICE_ATTR(path_masks, 0444, dasd_pm_show, NULL);
1435
1436/*
1437 * threshold value for IFCC/CCC errors
1438 */
1439static ssize_t
1440dasd_path_threshold_show(struct device *dev,
1441                          struct device_attribute *attr, char *buf)
1442{
1443        struct dasd_device *device;
1444        int len;
1445
1446        device = dasd_device_from_cdev(to_ccwdev(dev));
1447        if (IS_ERR(device))
1448                return -ENODEV;
1449        len = snprintf(buf, PAGE_SIZE, "%lu\n", device->path_thrhld);
1450        dasd_put_device(device);
1451        return len;
1452}
1453
1454static ssize_t
1455dasd_path_threshold_store(struct device *dev, struct device_attribute *attr,
1456                           const char *buf, size_t count)
1457{
1458        struct dasd_device *device;
1459        unsigned long flags;
1460        unsigned long val;
1461
1462        device = dasd_device_from_cdev(to_ccwdev(dev));
1463        if (IS_ERR(device))
1464                return -ENODEV;
1465
1466        if (kstrtoul(buf, 10, &val) != 0 || val > DASD_THRHLD_MAX) {
1467                dasd_put_device(device);
1468                return -EINVAL;
1469        }
1470        spin_lock_irqsave(get_ccwdev_lock(to_ccwdev(dev)), flags);
1471        device->path_thrhld = val;
1472        spin_unlock_irqrestore(get_ccwdev_lock(to_ccwdev(dev)), flags);
1473        dasd_put_device(device);
1474        return count;
1475}
1476
1477static DEVICE_ATTR(path_threshold, 0644, dasd_path_threshold_show,
1478                   dasd_path_threshold_store);
1479/*
1480 * interval for IFCC/CCC checks
1481 * meaning time with no IFCC/CCC error before the error counter
1482 * gets reset
1483 */
1484static ssize_t
1485dasd_path_interval_show(struct device *dev,
1486                        struct device_attribute *attr, char *buf)
1487{
1488        struct dasd_device *device;
1489        int len;
1490
1491        device = dasd_device_from_cdev(to_ccwdev(dev));
1492        if (IS_ERR(device))
1493                return -ENODEV;
1494        len = snprintf(buf, PAGE_SIZE, "%lu\n", device->path_interval);
1495        dasd_put_device(device);
1496        return len;
1497}
1498
1499static ssize_t
1500dasd_path_interval_store(struct device *dev, struct device_attribute *attr,
1501               const char *buf, size_t count)
1502{
1503        struct dasd_device *device;
1504        unsigned long flags;
1505        unsigned long val;
1506
1507        device = dasd_device_from_cdev(to_ccwdev(dev));
1508        if (IS_ERR(device))
1509                return -ENODEV;
1510
1511        if ((kstrtoul(buf, 10, &val) != 0) ||
1512            (val > DASD_INTERVAL_MAX) || val == 0) {
1513                dasd_put_device(device);
1514                return -EINVAL;
1515        }
1516        spin_lock_irqsave(get_ccwdev_lock(to_ccwdev(dev)), flags);
1517        if (val)
1518                device->path_interval = val;
1519        spin_unlock_irqrestore(get_ccwdev_lock(to_ccwdev(dev)), flags);
1520        dasd_put_device(device);
1521        return count;
1522}
1523
1524static DEVICE_ATTR(path_interval, 0644, dasd_path_interval_show,
1525                   dasd_path_interval_store);
1526
1527
1528static struct attribute * dasd_attrs[] = {
1529        &dev_attr_readonly.attr,
1530        &dev_attr_discipline.attr,
1531        &dev_attr_status.attr,
1532        &dev_attr_alias.attr,
1533        &dev_attr_vendor.attr,
1534        &dev_attr_uid.attr,
1535        &dev_attr_use_diag.attr,
1536        &dev_attr_raw_track_access.attr,
1537        &dev_attr_eer_enabled.attr,
1538        &dev_attr_erplog.attr,
1539        &dev_attr_failfast.attr,
1540        &dev_attr_expires.attr,
1541        &dev_attr_reservation_policy.attr,
1542        &dev_attr_last_known_reservation_state.attr,
1543        &dev_attr_safe_offline.attr,
1544        &dev_attr_host_access_count.attr,
1545        &dev_attr_path_masks.attr,
1546        &dev_attr_path_threshold.attr,
1547        &dev_attr_path_interval.attr,
1548        &dev_attr_path_reset.attr,
1549        &dev_attr_hpf.attr,
1550        NULL,
1551};
1552
1553static struct attribute_group dasd_attr_group = {
1554        .attrs = dasd_attrs,
1555};
1556
1557/*
1558 * Return value of the specified feature.
1559 */
1560int
1561dasd_get_feature(struct ccw_device *cdev, int feature)
1562{
1563        struct dasd_devmap *devmap;
1564
1565        devmap = dasd_find_busid(dev_name(&cdev->dev));
1566        if (IS_ERR(devmap))
1567                return PTR_ERR(devmap);
1568
1569        return ((devmap->features & feature) != 0);
1570}
1571
1572/*
1573 * Set / reset given feature.
1574 * Flag indicates whether to set (!=0) or the reset (=0) the feature.
1575 */
1576int
1577dasd_set_feature(struct ccw_device *cdev, int feature, int flag)
1578{
1579        struct dasd_devmap *devmap;
1580
1581        devmap = dasd_find_busid(dev_name(&cdev->dev));
1582        if (IS_ERR(devmap))
1583                return PTR_ERR(devmap);
1584
1585        spin_lock(&dasd_devmap_lock);
1586        if (flag)
1587                devmap->features |= feature;
1588        else
1589                devmap->features &= ~feature;
1590        if (devmap->device)
1591                devmap->device->features = devmap->features;
1592        spin_unlock(&dasd_devmap_lock);
1593        return 0;
1594}
1595
1596
1597int
1598dasd_add_sysfs_files(struct ccw_device *cdev)
1599{
1600        return sysfs_create_group(&cdev->dev.kobj, &dasd_attr_group);
1601}
1602
1603void
1604dasd_remove_sysfs_files(struct ccw_device *cdev)
1605{
1606        sysfs_remove_group(&cdev->dev.kobj, &dasd_attr_group);
1607}
1608
1609
1610int
1611dasd_devmap_init(void)
1612{
1613        int i;
1614
1615        /* Initialize devmap structures. */
1616        dasd_max_devindex = 0;
1617        for (i = 0; i < 256; i++)
1618                INIT_LIST_HEAD(&dasd_hashlists[i]);
1619        return 0;
1620}
1621
1622void
1623dasd_devmap_exit(void)
1624{
1625        dasd_forget_ranges();
1626}
1627