Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
0004  *          Horst Hummel <Horst.Hummel@de.ibm.com>
0005  *          Carsten Otte <Cotte@de.ibm.com>
0006  *          Martin Schwidefsky <schwidefsky@de.ibm.com>
0007  * Bugreports.to..: <Linux390@de.ibm.com>
0008  * Copyright IBM Corp. 1999,2001
0009  *
0010  * Device mapping and dasd= parameter parsing functions. All devmap
0011  * functions may not be called from interrupt context. In particular
0012  * dasd_get_device is a no-no from interrupt context.
0013  *
0014  */
0015 
0016 #define KMSG_COMPONENT "dasd"
0017 
0018 #include <linux/ctype.h>
0019 #include <linux/init.h>
0020 #include <linux/module.h>
0021 #include <linux/slab.h>
0022 
0023 #include <asm/debug.h>
0024 #include <linux/uaccess.h>
0025 #include <asm/ipl.h>
0026 
0027 /* This is ugly... */
0028 #define PRINTK_HEADER "dasd_devmap:"
0029 #define DASD_BUS_ID_SIZE 20
0030 #define DASD_MAX_PARAMS 256
0031 
0032 #include "dasd_int.h"
0033 
0034 struct kmem_cache *dasd_page_cache;
0035 EXPORT_SYMBOL_GPL(dasd_page_cache);
0036 
0037 /*
0038  * dasd_devmap_t is used to store the features and the relation
0039  * between device number and device index. To find a dasd_devmap_t
0040  * that corresponds to a device number of a device index each
0041  * dasd_devmap_t is added to two linked lists, one to search by
0042  * the device number and one to search by the device index. As
0043  * soon as big minor numbers are available the device index list
0044  * can be removed since the device number will then be identical
0045  * to the device index.
0046  */
0047 struct dasd_devmap {
0048     struct list_head list;
0049     char bus_id[DASD_BUS_ID_SIZE];
0050         unsigned int devindex;
0051         unsigned short features;
0052     struct dasd_device *device;
0053 };
0054 
0055 /*
0056  * Parameter parsing functions for dasd= parameter. The syntax is:
0057  *   <devno>        : (0x)?[0-9a-fA-F]+
0058  *   <busid>        : [0-0a-f]\.[0-9a-f]\.(0x)?[0-9a-fA-F]+
0059  *   <feature>      : ro
0060  *   <feature_list> : \(<feature>(:<feature>)*\)
0061  *   <devno-range>  : <devno>(-<devno>)?<feature_list>?
0062  *   <busid-range>  : <busid>(-<busid>)?<feature_list>?
0063  *   <devices>      : <devno-range>|<busid-range>
0064  *   <dasd_module>  : dasd_diag_mod|dasd_eckd_mod|dasd_fba_mod
0065  *
0066  *   <dasd>     : autodetect|probeonly|<devices>(,<devices>)*
0067  */
0068 
0069 int dasd_probeonly =  0;    /* is true, when probeonly mode is active */
0070 int dasd_autodetect = 0;    /* is true, when autodetection is active */
0071 int dasd_nopav = 0;     /* is true, when PAV is disabled */
0072 EXPORT_SYMBOL_GPL(dasd_nopav);
0073 int dasd_nofcx;         /* disable High Performance Ficon */
0074 EXPORT_SYMBOL_GPL(dasd_nofcx);
0075 
0076 /*
0077  * char *dasd[] is intended to hold the ranges supplied by the dasd= statement
0078  * it is named 'dasd' to directly be filled by insmod with the comma separated
0079  * strings when running as a module.
0080  */
0081 static char *dasd[DASD_MAX_PARAMS];
0082 module_param_array(dasd, charp, NULL, S_IRUGO);
0083 
0084 /*
0085  * Single spinlock to protect devmap and servermap structures and lists.
0086  */
0087 static DEFINE_SPINLOCK(dasd_devmap_lock);
0088 
0089 /*
0090  * Hash lists for devmap structures.
0091  */
0092 static struct list_head dasd_hashlists[256];
0093 int dasd_max_devindex;
0094 
0095 static struct dasd_devmap *dasd_add_busid(const char *, int);
0096 
0097 static inline int
0098 dasd_hash_busid(const char *bus_id)
0099 {
0100     int hash, i;
0101 
0102     hash = 0;
0103     for (i = 0; (i < DASD_BUS_ID_SIZE) && *bus_id; i++, bus_id++)
0104         hash += *bus_id;
0105     return hash & 0xff;
0106 }
0107 
0108 #ifndef MODULE
0109 static int __init dasd_call_setup(char *opt)
0110 {
0111     static int i __initdata;
0112     char *tmp;
0113 
0114     while (i < DASD_MAX_PARAMS) {
0115         tmp = strsep(&opt, ",");
0116         if (!tmp)
0117             break;
0118 
0119         dasd[i++] = tmp;
0120     }
0121 
0122     return 1;
0123 }
0124 
0125 __setup ("dasd=", dasd_call_setup);
0126 #endif  /* #ifndef MODULE */
0127 
0128 #define DASD_IPLDEV "ipldev"
0129 
0130 /*
0131  * Read a device busid/devno from a string.
0132  */
0133 static int __init dasd_busid(char *str, int *id0, int *id1, int *devno)
0134 {
0135     unsigned int val;
0136     char *tok;
0137 
0138     /* Interpret ipldev busid */
0139     if (strncmp(DASD_IPLDEV, str, strlen(DASD_IPLDEV)) == 0) {
0140         if (ipl_info.type != IPL_TYPE_CCW) {
0141             pr_err("The IPL device is not a CCW device\n");
0142             return -EINVAL;
0143         }
0144         *id0 = 0;
0145         *id1 = ipl_info.data.ccw.dev_id.ssid;
0146         *devno = ipl_info.data.ccw.dev_id.devno;
0147 
0148         return 0;
0149     }
0150 
0151     /* Old style 0xXXXX or XXXX */
0152     if (!kstrtouint(str, 16, &val)) {
0153         *id0 = *id1 = 0;
0154         if (val > 0xffff)
0155             return -EINVAL;
0156         *devno = val;
0157         return 0;
0158     }
0159 
0160     /* New style x.y.z busid */
0161     tok = strsep(&str, ".");
0162     if (kstrtouint(tok, 16, &val) || val > 0xff)
0163         return -EINVAL;
0164     *id0 = val;
0165 
0166     tok = strsep(&str, ".");
0167     if (kstrtouint(tok, 16, &val) || val > 0xff)
0168         return -EINVAL;
0169     *id1 = val;
0170 
0171     tok = strsep(&str, ".");
0172     if (kstrtouint(tok, 16, &val) || val > 0xffff)
0173         return -EINVAL;
0174     *devno = val;
0175 
0176     return 0;
0177 }
0178 
0179 /*
0180  * Read colon separated list of dasd features.
0181  */
0182 static int __init dasd_feature_list(char *str)
0183 {
0184     int features, len, rc;
0185 
0186     features = 0;
0187     rc = 0;
0188 
0189     if (!str)
0190         return DASD_FEATURE_DEFAULT;
0191 
0192     while (1) {
0193         for (len = 0;
0194              str[len] && str[len] != ':' && str[len] != ')'; len++);
0195         if (len == 2 && !strncmp(str, "ro", 2))
0196             features |= DASD_FEATURE_READONLY;
0197         else if (len == 4 && !strncmp(str, "diag", 4))
0198             features |= DASD_FEATURE_USEDIAG;
0199         else if (len == 3 && !strncmp(str, "raw", 3))
0200             features |= DASD_FEATURE_USERAW;
0201         else if (len == 6 && !strncmp(str, "erplog", 6))
0202             features |= DASD_FEATURE_ERPLOG;
0203         else if (len == 8 && !strncmp(str, "failfast", 8))
0204             features |= DASD_FEATURE_FAILFAST;
0205         else {
0206             pr_warn("%.*s is not a supported device option\n",
0207                 len, str);
0208             rc = -EINVAL;
0209         }
0210         str += len;
0211         if (*str != ':')
0212             break;
0213         str++;
0214     }
0215 
0216     return rc ? : features;
0217 }
0218 
0219 /*
0220  * Try to match the first element on the comma separated parse string
0221  * with one of the known keywords. If a keyword is found, take the approprate
0222  * action and return a pointer to the residual string. If the first element
0223  * could not be matched to any keyword then return an error code.
0224  */
0225 static int __init dasd_parse_keyword(char *keyword)
0226 {
0227     int length = strlen(keyword);
0228 
0229     if (strncmp("autodetect", keyword, length) == 0) {
0230         dasd_autodetect = 1;
0231         pr_info("The autodetection mode has been activated\n");
0232         return 0;
0233         }
0234     if (strncmp("probeonly", keyword, length) == 0) {
0235         dasd_probeonly = 1;
0236         pr_info("The probeonly mode has been activated\n");
0237         return 0;
0238         }
0239     if (strncmp("nopav", keyword, length) == 0) {
0240         if (MACHINE_IS_VM)
0241             pr_info("'nopav' is not supported on z/VM\n");
0242         else {
0243             dasd_nopav = 1;
0244             pr_info("PAV support has be deactivated\n");
0245         }
0246         return 0;
0247     }
0248     if (strncmp("nofcx", keyword, length) == 0) {
0249         dasd_nofcx = 1;
0250         pr_info("High Performance FICON support has been "
0251             "deactivated\n");
0252         return 0;
0253     }
0254     if (strncmp("fixedbuffers", keyword, length) == 0) {
0255         if (dasd_page_cache)
0256             return 0;
0257         dasd_page_cache =
0258             kmem_cache_create("dasd_page_cache", PAGE_SIZE,
0259                       PAGE_SIZE, SLAB_CACHE_DMA,
0260                       NULL);
0261         if (!dasd_page_cache)
0262             DBF_EVENT(DBF_WARNING, "%s", "Failed to create slab, "
0263                 "fixed buffer mode disabled.");
0264         else
0265             DBF_EVENT(DBF_INFO, "%s",
0266                  "turning on fixed buffer mode");
0267         return 0;
0268     }
0269 
0270     return -EINVAL;
0271 }
0272 
0273 /*
0274  * Split a string of a device range into its pieces and return the from, to, and
0275  * feature parts separately.
0276  * e.g.:
0277  * 0.0.1234-0.0.5678(ro:erplog) -> from: 0.0.1234 to: 0.0.5678 features: ro:erplog
0278  * 0.0.8765(raw) -> from: 0.0.8765 to: null features: raw
0279  * 0x4321 -> from: 0x4321 to: null features: null
0280  */
0281 static int __init dasd_evaluate_range_param(char *range, char **from_str,
0282                         char **to_str, char **features_str)
0283 {
0284     int rc = 0;
0285 
0286     /* Do we have a range or a single device? */
0287     if (strchr(range, '-')) {
0288         *from_str = strsep(&range, "-");
0289         *to_str = strsep(&range, "(");
0290         *features_str = strsep(&range, ")");
0291     } else {
0292         *from_str = strsep(&range, "(");
0293         *features_str = strsep(&range, ")");
0294     }
0295 
0296     if (*features_str && !range) {
0297         pr_warn("A closing parenthesis ')' is missing in the dasd= parameter\n");
0298         rc = -EINVAL;
0299     }
0300 
0301     return rc;
0302 }
0303 
0304 /*
0305  * Try to interprete the range string as a device number or a range of devices.
0306  * If the interpretation is successful, create the matching dasd_devmap entries.
0307  * If interpretation fails or in case of an error, return an error code.
0308  */
0309 static int __init dasd_parse_range(const char *range)
0310 {
0311     struct dasd_devmap *devmap;
0312     int from, from_id0, from_id1;
0313     int to, to_id0, to_id1;
0314     int features;
0315     char bus_id[DASD_BUS_ID_SIZE + 1];
0316     char *features_str = NULL;
0317     char *from_str = NULL;
0318     char *to_str = NULL;
0319     int rc = 0;
0320     char *tmp;
0321 
0322     tmp = kstrdup(range, GFP_KERNEL);
0323     if (!tmp)
0324         return -ENOMEM;
0325 
0326     if (dasd_evaluate_range_param(tmp, &from_str, &to_str, &features_str)) {
0327         rc = -EINVAL;
0328         goto out;
0329     }
0330 
0331     if (dasd_busid(from_str, &from_id0, &from_id1, &from)) {
0332         rc = -EINVAL;
0333         goto out;
0334     }
0335 
0336     to = from;
0337     to_id0 = from_id0;
0338     to_id1 = from_id1;
0339     if (to_str) {
0340         if (dasd_busid(to_str, &to_id0, &to_id1, &to)) {
0341             rc = -EINVAL;
0342             goto out;
0343         }
0344         if (from_id0 != to_id0 || from_id1 != to_id1 || from > to) {
0345             pr_err("%s is not a valid device range\n", range);
0346             rc = -EINVAL;
0347             goto out;
0348         }
0349     }
0350 
0351     features = dasd_feature_list(features_str);
0352     if (features < 0) {
0353         rc = -EINVAL;
0354         goto out;
0355     }
0356     /* each device in dasd= parameter should be set initially online */
0357     features |= DASD_FEATURE_INITIAL_ONLINE;
0358     while (from <= to) {
0359         sprintf(bus_id, "%01x.%01x.%04x", from_id0, from_id1, from++);
0360         devmap = dasd_add_busid(bus_id, features);
0361         if (IS_ERR(devmap)) {
0362             rc = PTR_ERR(devmap);
0363             goto out;
0364         }
0365     }
0366 
0367 out:
0368     kfree(tmp);
0369 
0370     return rc;
0371 }
0372 
0373 /*
0374  * Parse parameters stored in dasd[]
0375  * The 'dasd=...' parameter allows to specify a comma separated list of
0376  * keywords and device ranges. The parameters in that list will be stored as
0377  * separate elementes in dasd[].
0378  */
0379 int __init dasd_parse(void)
0380 {
0381     int rc, i;
0382     char *cur;
0383 
0384     rc = 0;
0385     for (i = 0; i < DASD_MAX_PARAMS; i++) {
0386         cur = dasd[i];
0387         if (!cur)
0388             break;
0389         if (*cur == '\0')
0390             continue;
0391 
0392         rc = dasd_parse_keyword(cur);
0393         if (rc)
0394             rc = dasd_parse_range(cur);
0395 
0396         if (rc)
0397             break;
0398     }
0399 
0400     return rc;
0401 }
0402 
0403 /*
0404  * Add a devmap for the device specified by busid. It is possible that
0405  * the devmap already exists (dasd= parameter). The order of the devices
0406  * added through this function will define the kdevs for the individual
0407  * devices.
0408  */
0409 static struct dasd_devmap *
0410 dasd_add_busid(const char *bus_id, int features)
0411 {
0412     struct dasd_devmap *devmap, *new, *tmp;
0413     int hash;
0414 
0415     new = kzalloc(sizeof(struct dasd_devmap), GFP_KERNEL);
0416     if (!new)
0417         return ERR_PTR(-ENOMEM);
0418     spin_lock(&dasd_devmap_lock);
0419     devmap = NULL;
0420     hash = dasd_hash_busid(bus_id);
0421     list_for_each_entry(tmp, &dasd_hashlists[hash], list)
0422         if (strncmp(tmp->bus_id, bus_id, DASD_BUS_ID_SIZE) == 0) {
0423             devmap = tmp;
0424             break;
0425         }
0426     if (!devmap) {
0427         /* This bus_id is new. */
0428         new->devindex = dasd_max_devindex++;
0429         strlcpy(new->bus_id, bus_id, DASD_BUS_ID_SIZE);
0430         new->features = features;
0431         new->device = NULL;
0432         list_add(&new->list, &dasd_hashlists[hash]);
0433         devmap = new;
0434         new = NULL;
0435     }
0436     spin_unlock(&dasd_devmap_lock);
0437     kfree(new);
0438     return devmap;
0439 }
0440 
0441 /*
0442  * Find devmap for device with given bus_id.
0443  */
0444 static struct dasd_devmap *
0445 dasd_find_busid(const char *bus_id)
0446 {
0447     struct dasd_devmap *devmap, *tmp;
0448     int hash;
0449 
0450     spin_lock(&dasd_devmap_lock);
0451     devmap = ERR_PTR(-ENODEV);
0452     hash = dasd_hash_busid(bus_id);
0453     list_for_each_entry(tmp, &dasd_hashlists[hash], list) {
0454         if (strncmp(tmp->bus_id, bus_id, DASD_BUS_ID_SIZE) == 0) {
0455             devmap = tmp;
0456             break;
0457         }
0458     }
0459     spin_unlock(&dasd_devmap_lock);
0460     return devmap;
0461 }
0462 
0463 /*
0464  * Check if busid has been added to the list of dasd ranges.
0465  */
0466 int
0467 dasd_busid_known(const char *bus_id)
0468 {
0469     return IS_ERR(dasd_find_busid(bus_id)) ? -ENOENT : 0;
0470 }
0471 
0472 /*
0473  * Forget all about the device numbers added so far.
0474  * This may only be called at module unload or system shutdown.
0475  */
0476 static void
0477 dasd_forget_ranges(void)
0478 {
0479     struct dasd_devmap *devmap, *n;
0480     int i;
0481 
0482     spin_lock(&dasd_devmap_lock);
0483     for (i = 0; i < 256; i++) {
0484         list_for_each_entry_safe(devmap, n, &dasd_hashlists[i], list) {
0485             BUG_ON(devmap->device != NULL);
0486             list_del(&devmap->list);
0487             kfree(devmap);
0488         }
0489     }
0490     spin_unlock(&dasd_devmap_lock);
0491 }
0492 
0493 /*
0494  * Find the device struct by its device index.
0495  */
0496 struct dasd_device *
0497 dasd_device_from_devindex(int devindex)
0498 {
0499     struct dasd_devmap *devmap, *tmp;
0500     struct dasd_device *device;
0501     int i;
0502 
0503     spin_lock(&dasd_devmap_lock);
0504     devmap = NULL;
0505     for (i = 0; (i < 256) && !devmap; i++)
0506         list_for_each_entry(tmp, &dasd_hashlists[i], list)
0507             if (tmp->devindex == devindex) {
0508                 /* Found the devmap for the device. */
0509                 devmap = tmp;
0510                 break;
0511             }
0512     if (devmap && devmap->device) {
0513         device = devmap->device;
0514         dasd_get_device(device);
0515     } else
0516         device = ERR_PTR(-ENODEV);
0517     spin_unlock(&dasd_devmap_lock);
0518     return device;
0519 }
0520 
0521 /*
0522  * Return devmap for cdev. If no devmap exists yet, create one and
0523  * connect it to the cdev.
0524  */
0525 static struct dasd_devmap *
0526 dasd_devmap_from_cdev(struct ccw_device *cdev)
0527 {
0528     struct dasd_devmap *devmap;
0529 
0530     devmap = dasd_find_busid(dev_name(&cdev->dev));
0531     if (IS_ERR(devmap))
0532         devmap = dasd_add_busid(dev_name(&cdev->dev),
0533                     DASD_FEATURE_DEFAULT);
0534     return devmap;
0535 }
0536 
0537 /*
0538  * Create a dasd device structure for cdev.
0539  */
0540 struct dasd_device *
0541 dasd_create_device(struct ccw_device *cdev)
0542 {
0543     struct dasd_devmap *devmap;
0544     struct dasd_device *device;
0545     unsigned long flags;
0546     int rc;
0547 
0548     devmap = dasd_devmap_from_cdev(cdev);
0549     if (IS_ERR(devmap))
0550         return (void *) devmap;
0551 
0552     device = dasd_alloc_device();
0553     if (IS_ERR(device))
0554         return device;
0555     atomic_set(&device->ref_count, 3);
0556 
0557     spin_lock(&dasd_devmap_lock);
0558     if (!devmap->device) {
0559         devmap->device = device;
0560         device->devindex = devmap->devindex;
0561         device->features = devmap->features;
0562         get_device(&cdev->dev);
0563         device->cdev = cdev;
0564         rc = 0;
0565     } else
0566         /* Someone else was faster. */
0567         rc = -EBUSY;
0568     spin_unlock(&dasd_devmap_lock);
0569 
0570     if (rc) {
0571         dasd_free_device(device);
0572         return ERR_PTR(rc);
0573     }
0574 
0575     spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
0576     dev_set_drvdata(&cdev->dev, device);
0577     spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
0578 
0579     device->paths_info = kset_create_and_add("paths_info", NULL,
0580                          &device->cdev->dev.kobj);
0581     if (!device->paths_info)
0582         dev_warn(&cdev->dev, "Could not create paths_info kset\n");
0583 
0584     return device;
0585 }
0586 
0587 /*
0588  * Wait queue for dasd_delete_device waits.
0589  */
0590 static DECLARE_WAIT_QUEUE_HEAD(dasd_delete_wq);
0591 
0592 /*
0593  * Remove a dasd device structure. The passed referenced
0594  * is destroyed.
0595  */
0596 void
0597 dasd_delete_device(struct dasd_device *device)
0598 {
0599     struct ccw_device *cdev;
0600     struct dasd_devmap *devmap;
0601     unsigned long flags;
0602 
0603     /* First remove device pointer from devmap. */
0604     devmap = dasd_find_busid(dev_name(&device->cdev->dev));
0605     BUG_ON(IS_ERR(devmap));
0606     spin_lock(&dasd_devmap_lock);
0607     if (devmap->device != device) {
0608         spin_unlock(&dasd_devmap_lock);
0609         dasd_put_device(device);
0610         return;
0611     }
0612     devmap->device = NULL;
0613     spin_unlock(&dasd_devmap_lock);
0614 
0615     /* Disconnect dasd_device structure from ccw_device structure. */
0616     spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
0617     dev_set_drvdata(&device->cdev->dev, NULL);
0618     spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
0619 
0620     /*
0621      * Drop ref_count by 3, one for the devmap reference, one for
0622      * the cdev reference and one for the passed reference.
0623      */
0624     atomic_sub(3, &device->ref_count);
0625 
0626     /* Wait for reference counter to drop to zero. */
0627     wait_event(dasd_delete_wq, atomic_read(&device->ref_count) == 0);
0628 
0629     dasd_generic_free_discipline(device);
0630 
0631     kset_unregister(device->paths_info);
0632 
0633     /* Disconnect dasd_device structure from ccw_device structure. */
0634     cdev = device->cdev;
0635     device->cdev = NULL;
0636 
0637     /* Put ccw_device structure. */
0638     put_device(&cdev->dev);
0639 
0640     /* Now the device structure can be freed. */
0641     dasd_free_device(device);
0642 }
0643 
0644 /*
0645  * Reference counter dropped to zero. Wake up waiter
0646  * in dasd_delete_device.
0647  */
0648 void
0649 dasd_put_device_wake(struct dasd_device *device)
0650 {
0651     wake_up(&dasd_delete_wq);
0652 }
0653 EXPORT_SYMBOL_GPL(dasd_put_device_wake);
0654 
0655 /*
0656  * Return dasd_device structure associated with cdev.
0657  * This function needs to be called with the ccw device
0658  * lock held. It can be used from interrupt context.
0659  */
0660 struct dasd_device *
0661 dasd_device_from_cdev_locked(struct ccw_device *cdev)
0662 {
0663     struct dasd_device *device = dev_get_drvdata(&cdev->dev);
0664 
0665     if (!device)
0666         return ERR_PTR(-ENODEV);
0667     dasd_get_device(device);
0668     return device;
0669 }
0670 
0671 /*
0672  * Return dasd_device structure associated with cdev.
0673  */
0674 struct dasd_device *
0675 dasd_device_from_cdev(struct ccw_device *cdev)
0676 {
0677     struct dasd_device *device;
0678     unsigned long flags;
0679 
0680     spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
0681     device = dasd_device_from_cdev_locked(cdev);
0682     spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
0683     return device;
0684 }
0685 
0686 void dasd_add_link_to_gendisk(struct gendisk *gdp, struct dasd_device *device)
0687 {
0688     struct dasd_devmap *devmap;
0689 
0690     devmap = dasd_find_busid(dev_name(&device->cdev->dev));
0691     if (IS_ERR(devmap))
0692         return;
0693     spin_lock(&dasd_devmap_lock);
0694     gdp->private_data = devmap;
0695     spin_unlock(&dasd_devmap_lock);
0696 }
0697 
0698 struct dasd_device *dasd_device_from_gendisk(struct gendisk *gdp)
0699 {
0700     struct dasd_device *device;
0701     struct dasd_devmap *devmap;
0702 
0703     if (!gdp->private_data)
0704         return NULL;
0705     device = NULL;
0706     spin_lock(&dasd_devmap_lock);
0707     devmap = gdp->private_data;
0708     if (devmap && devmap->device) {
0709         device = devmap->device;
0710         dasd_get_device(device);
0711     }
0712     spin_unlock(&dasd_devmap_lock);
0713     return device;
0714 }
0715 
0716 /*
0717  * SECTION: files in sysfs
0718  */
0719 
0720 /*
0721  * failfast controls the behaviour, if no path is available
0722  */
0723 static ssize_t dasd_ff_show(struct device *dev, struct device_attribute *attr,
0724                 char *buf)
0725 {
0726     struct dasd_devmap *devmap;
0727     int ff_flag;
0728 
0729     devmap = dasd_find_busid(dev_name(dev));
0730     if (!IS_ERR(devmap))
0731         ff_flag = (devmap->features & DASD_FEATURE_FAILFAST) != 0;
0732     else
0733         ff_flag = (DASD_FEATURE_DEFAULT & DASD_FEATURE_FAILFAST) != 0;
0734     return sysfs_emit(buf, ff_flag ? "1\n" : "0\n");
0735 }
0736 
0737 static ssize_t dasd_ff_store(struct device *dev, struct device_attribute *attr,
0738           const char *buf, size_t count)
0739 {
0740     unsigned int val;
0741     int rc;
0742 
0743     if (kstrtouint(buf, 0, &val) || val > 1)
0744         return -EINVAL;
0745 
0746     rc = dasd_set_feature(to_ccwdev(dev), DASD_FEATURE_FAILFAST, val);
0747 
0748     return rc ? : count;
0749 }
0750 
0751 static DEVICE_ATTR(failfast, 0644, dasd_ff_show, dasd_ff_store);
0752 
0753 /*
0754  * readonly controls the readonly status of a dasd
0755  */
0756 static ssize_t
0757 dasd_ro_show(struct device *dev, struct device_attribute *attr, char *buf)
0758 {
0759     struct dasd_devmap *devmap;
0760     struct dasd_device *device;
0761     int ro_flag = 0;
0762 
0763     devmap = dasd_find_busid(dev_name(dev));
0764     if (IS_ERR(devmap))
0765         goto out;
0766 
0767     ro_flag = !!(devmap->features & DASD_FEATURE_READONLY);
0768 
0769     spin_lock(&dasd_devmap_lock);
0770     device = devmap->device;
0771     if (device)
0772         ro_flag |= test_bit(DASD_FLAG_DEVICE_RO, &device->flags);
0773     spin_unlock(&dasd_devmap_lock);
0774 
0775 out:
0776     return sysfs_emit(buf, ro_flag ? "1\n" : "0\n");
0777 }
0778 
0779 static ssize_t
0780 dasd_ro_store(struct device *dev, struct device_attribute *attr,
0781           const char *buf, size_t count)
0782 {
0783     struct ccw_device *cdev = to_ccwdev(dev);
0784     struct dasd_device *device;
0785     unsigned long flags;
0786     unsigned int val;
0787     int rc;
0788 
0789     if (kstrtouint(buf, 0, &val) || val > 1)
0790         return -EINVAL;
0791 
0792     rc = dasd_set_feature(cdev, DASD_FEATURE_READONLY, val);
0793     if (rc)
0794         return rc;
0795 
0796     device = dasd_device_from_cdev(cdev);
0797     if (IS_ERR(device))
0798         return count;
0799 
0800     spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
0801     val = val || test_bit(DASD_FLAG_DEVICE_RO, &device->flags);
0802 
0803     if (!device->block || !device->block->gdp ||
0804         test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
0805         spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
0806         goto out;
0807     }
0808     /* Increase open_count to avoid losing the block device */
0809     atomic_inc(&device->block->open_count);
0810     spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
0811 
0812     set_disk_ro(device->block->gdp, val);
0813     atomic_dec(&device->block->open_count);
0814 
0815 out:
0816     dasd_put_device(device);
0817 
0818     return count;
0819 }
0820 
0821 static DEVICE_ATTR(readonly, 0644, dasd_ro_show, dasd_ro_store);
0822 /*
0823  * erplog controls the logging of ERP related data
0824  * (e.g. failing channel programs).
0825  */
0826 static ssize_t
0827 dasd_erplog_show(struct device *dev, struct device_attribute *attr, char *buf)
0828 {
0829     struct dasd_devmap *devmap;
0830     int erplog;
0831 
0832     devmap = dasd_find_busid(dev_name(dev));
0833     if (!IS_ERR(devmap))
0834         erplog = (devmap->features & DASD_FEATURE_ERPLOG) != 0;
0835     else
0836         erplog = (DASD_FEATURE_DEFAULT & DASD_FEATURE_ERPLOG) != 0;
0837     return sysfs_emit(buf, erplog ? "1\n" : "0\n");
0838 }
0839 
0840 static ssize_t
0841 dasd_erplog_store(struct device *dev, struct device_attribute *attr,
0842           const char *buf, size_t count)
0843 {
0844     unsigned int val;
0845     int rc;
0846 
0847     if (kstrtouint(buf, 0, &val) || val > 1)
0848         return -EINVAL;
0849 
0850     rc = dasd_set_feature(to_ccwdev(dev), DASD_FEATURE_ERPLOG, val);
0851 
0852     return rc ? : count;
0853 }
0854 
0855 static DEVICE_ATTR(erplog, 0644, dasd_erplog_show, dasd_erplog_store);
0856 
0857 /*
0858  * use_diag controls whether the driver should use diag rather than ssch
0859  * to talk to the device
0860  */
0861 static ssize_t
0862 dasd_use_diag_show(struct device *dev, struct device_attribute *attr, char *buf)
0863 {
0864     struct dasd_devmap *devmap;
0865     int use_diag;
0866 
0867     devmap = dasd_find_busid(dev_name(dev));
0868     if (!IS_ERR(devmap))
0869         use_diag = (devmap->features & DASD_FEATURE_USEDIAG) != 0;
0870     else
0871         use_diag = (DASD_FEATURE_DEFAULT & DASD_FEATURE_USEDIAG) != 0;
0872     return sprintf(buf, use_diag ? "1\n" : "0\n");
0873 }
0874 
0875 static ssize_t
0876 dasd_use_diag_store(struct device *dev, struct device_attribute *attr,
0877             const char *buf, size_t count)
0878 {
0879     struct dasd_devmap *devmap;
0880     unsigned int val;
0881     ssize_t rc;
0882 
0883     devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
0884     if (IS_ERR(devmap))
0885         return PTR_ERR(devmap);
0886 
0887     if (kstrtouint(buf, 0, &val) || val > 1)
0888         return -EINVAL;
0889 
0890     spin_lock(&dasd_devmap_lock);
0891     /* Changing diag discipline flag is only allowed in offline state. */
0892     rc = count;
0893     if (!devmap->device && !(devmap->features & DASD_FEATURE_USERAW)) {
0894         if (val)
0895             devmap->features |= DASD_FEATURE_USEDIAG;
0896         else
0897             devmap->features &= ~DASD_FEATURE_USEDIAG;
0898     } else
0899         rc = -EPERM;
0900     spin_unlock(&dasd_devmap_lock);
0901     return rc;
0902 }
0903 
0904 static DEVICE_ATTR(use_diag, 0644, dasd_use_diag_show, dasd_use_diag_store);
0905 
0906 /*
0907  * use_raw controls whether the driver should give access to raw eckd data or
0908  * operate in standard mode
0909  */
0910 static ssize_t
0911 dasd_use_raw_show(struct device *dev, struct device_attribute *attr, char *buf)
0912 {
0913     struct dasd_devmap *devmap;
0914     int use_raw;
0915 
0916     devmap = dasd_find_busid(dev_name(dev));
0917     if (!IS_ERR(devmap))
0918         use_raw = (devmap->features & DASD_FEATURE_USERAW) != 0;
0919     else
0920         use_raw = (DASD_FEATURE_DEFAULT & DASD_FEATURE_USERAW) != 0;
0921     return sprintf(buf, use_raw ? "1\n" : "0\n");
0922 }
0923 
0924 static ssize_t
0925 dasd_use_raw_store(struct device *dev, struct device_attribute *attr,
0926             const char *buf, size_t count)
0927 {
0928     struct dasd_devmap *devmap;
0929     ssize_t rc;
0930     unsigned long val;
0931 
0932     devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
0933     if (IS_ERR(devmap))
0934         return PTR_ERR(devmap);
0935 
0936     if ((kstrtoul(buf, 10, &val) != 0) || val > 1)
0937         return -EINVAL;
0938 
0939     spin_lock(&dasd_devmap_lock);
0940     /* Changing diag discipline flag is only allowed in offline state. */
0941     rc = count;
0942     if (!devmap->device && !(devmap->features & DASD_FEATURE_USEDIAG)) {
0943         if (val)
0944             devmap->features |= DASD_FEATURE_USERAW;
0945         else
0946             devmap->features &= ~DASD_FEATURE_USERAW;
0947     } else
0948         rc = -EPERM;
0949     spin_unlock(&dasd_devmap_lock);
0950     return rc;
0951 }
0952 
0953 static DEVICE_ATTR(raw_track_access, 0644, dasd_use_raw_show,
0954            dasd_use_raw_store);
0955 
0956 static ssize_t
0957 dasd_safe_offline_store(struct device *dev, struct device_attribute *attr,
0958             const char *buf, size_t count)
0959 {
0960     struct ccw_device *cdev = to_ccwdev(dev);
0961     struct dasd_device *device;
0962     unsigned long flags;
0963     int rc;
0964 
0965     spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
0966     device = dasd_device_from_cdev_locked(cdev);
0967     if (IS_ERR(device)) {
0968         rc = PTR_ERR(device);
0969         spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
0970         goto out;
0971     }
0972 
0973     if (test_bit(DASD_FLAG_OFFLINE, &device->flags) ||
0974         test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
0975         /* Already doing offline processing */
0976         dasd_put_device(device);
0977         spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
0978         rc = -EBUSY;
0979         goto out;
0980     }
0981 
0982     set_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags);
0983     dasd_put_device(device);
0984     spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
0985 
0986     rc = ccw_device_set_offline(cdev);
0987 
0988 out:
0989     return rc ? rc : count;
0990 }
0991 
0992 static DEVICE_ATTR(safe_offline, 0200, NULL, dasd_safe_offline_store);
0993 
0994 static ssize_t
0995 dasd_access_show(struct device *dev, struct device_attribute *attr,
0996          char *buf)
0997 {
0998     struct ccw_device *cdev = to_ccwdev(dev);
0999     struct dasd_device *device;
1000     int count;
1001 
1002     device = dasd_device_from_cdev(cdev);
1003     if (IS_ERR(device))
1004         return PTR_ERR(device);
1005 
1006     if (!device->discipline)
1007         count = -ENODEV;
1008     else if (!device->discipline->host_access_count)
1009         count = -EOPNOTSUPP;
1010     else
1011         count = device->discipline->host_access_count(device);
1012 
1013     dasd_put_device(device);
1014     if (count < 0)
1015         return count;
1016 
1017     return sprintf(buf, "%d\n", count);
1018 }
1019 
1020 static DEVICE_ATTR(host_access_count, 0444, dasd_access_show, NULL);
1021 
1022 static ssize_t
1023 dasd_discipline_show(struct device *dev, struct device_attribute *attr,
1024              char *buf)
1025 {
1026     struct dasd_device *device;
1027     ssize_t len;
1028 
1029     device = dasd_device_from_cdev(to_ccwdev(dev));
1030     if (IS_ERR(device))
1031         goto out;
1032     else if (!device->discipline) {
1033         dasd_put_device(device);
1034         goto out;
1035     } else {
1036         len = sysfs_emit(buf, "%s\n",
1037                  device->discipline->name);
1038         dasd_put_device(device);
1039         return len;
1040     }
1041 out:
1042     len = sysfs_emit(buf, "none\n");
1043     return len;
1044 }
1045 
1046 static DEVICE_ATTR(discipline, 0444, dasd_discipline_show, NULL);
1047 
1048 static ssize_t
1049 dasd_device_status_show(struct device *dev, struct device_attribute *attr,
1050              char *buf)
1051 {
1052     struct dasd_device *device;
1053     ssize_t len;
1054 
1055     device = dasd_device_from_cdev(to_ccwdev(dev));
1056     if (!IS_ERR(device)) {
1057         switch (device->state) {
1058         case DASD_STATE_NEW:
1059             len = sysfs_emit(buf, "new\n");
1060             break;
1061         case DASD_STATE_KNOWN:
1062             len = sysfs_emit(buf, "detected\n");
1063             break;
1064         case DASD_STATE_BASIC:
1065             len = sysfs_emit(buf, "basic\n");
1066             break;
1067         case DASD_STATE_UNFMT:
1068             len = sysfs_emit(buf, "unformatted\n");
1069             break;
1070         case DASD_STATE_READY:
1071             len = sysfs_emit(buf, "ready\n");
1072             break;
1073         case DASD_STATE_ONLINE:
1074             len = sysfs_emit(buf, "online\n");
1075             break;
1076         default:
1077             len = sysfs_emit(buf, "no stat\n");
1078             break;
1079         }
1080         dasd_put_device(device);
1081     } else
1082         len = sysfs_emit(buf, "unknown\n");
1083     return len;
1084 }
1085 
1086 static DEVICE_ATTR(status, 0444, dasd_device_status_show, NULL);
1087 
1088 static ssize_t dasd_alias_show(struct device *dev,
1089                    struct device_attribute *attr, char *buf)
1090 {
1091     struct dasd_device *device;
1092     struct dasd_uid uid;
1093 
1094     device = dasd_device_from_cdev(to_ccwdev(dev));
1095     if (IS_ERR(device))
1096         return sprintf(buf, "0\n");
1097 
1098     if (device->discipline && device->discipline->get_uid &&
1099         !device->discipline->get_uid(device, &uid)) {
1100         if (uid.type == UA_BASE_PAV_ALIAS ||
1101             uid.type == UA_HYPER_PAV_ALIAS) {
1102             dasd_put_device(device);
1103             return sprintf(buf, "1\n");
1104         }
1105     }
1106     dasd_put_device(device);
1107 
1108     return sprintf(buf, "0\n");
1109 }
1110 
1111 static DEVICE_ATTR(alias, 0444, dasd_alias_show, NULL);
1112 
1113 static ssize_t dasd_vendor_show(struct device *dev,
1114                 struct device_attribute *attr, char *buf)
1115 {
1116     struct dasd_device *device;
1117     struct dasd_uid uid;
1118     char *vendor;
1119 
1120     device = dasd_device_from_cdev(to_ccwdev(dev));
1121     vendor = "";
1122     if (IS_ERR(device))
1123         return sysfs_emit(buf, "%s\n", vendor);
1124 
1125     if (device->discipline && device->discipline->get_uid &&
1126         !device->discipline->get_uid(device, &uid))
1127             vendor = uid.vendor;
1128 
1129     dasd_put_device(device);
1130 
1131     return sysfs_emit(buf, "%s\n", vendor);
1132 }
1133 
1134 static DEVICE_ATTR(vendor, 0444, dasd_vendor_show, NULL);
1135 
1136 #define UID_STRLEN ( /* vendor */ 3 + 1 + /* serial    */ 14 + 1 +\
1137              /* SSID   */ 4 + 1 + /* unit addr */ 2 + 1 +\
1138              /* vduit */ 32 + 1)
1139 
1140 static ssize_t
1141 dasd_uid_show(struct device *dev, struct device_attribute *attr, char *buf)
1142 {
1143     struct dasd_device *device;
1144     struct dasd_uid uid;
1145     char uid_string[UID_STRLEN];
1146     char ua_string[3];
1147 
1148     device = dasd_device_from_cdev(to_ccwdev(dev));
1149     uid_string[0] = 0;
1150     if (IS_ERR(device))
1151         return sysfs_emit(buf, "%s\n", uid_string);
1152 
1153     if (device->discipline && device->discipline->get_uid &&
1154         !device->discipline->get_uid(device, &uid)) {
1155         switch (uid.type) {
1156         case UA_BASE_DEVICE:
1157             snprintf(ua_string, sizeof(ua_string), "%02x",
1158                  uid.real_unit_addr);
1159             break;
1160         case UA_BASE_PAV_ALIAS:
1161             snprintf(ua_string, sizeof(ua_string), "%02x",
1162                  uid.base_unit_addr);
1163             break;
1164         case UA_HYPER_PAV_ALIAS:
1165             snprintf(ua_string, sizeof(ua_string), "xx");
1166             break;
1167         default:
1168             /* should not happen, treat like base device */
1169             snprintf(ua_string, sizeof(ua_string), "%02x",
1170                  uid.real_unit_addr);
1171             break;
1172         }
1173 
1174         if (strlen(uid.vduit) > 0)
1175             snprintf(uid_string, sizeof(uid_string),
1176                  "%s.%s.%04x.%s.%s",
1177                  uid.vendor, uid.serial, uid.ssid, ua_string,
1178                  uid.vduit);
1179         else
1180             snprintf(uid_string, sizeof(uid_string),
1181                  "%s.%s.%04x.%s",
1182                  uid.vendor, uid.serial, uid.ssid, ua_string);
1183     }
1184     dasd_put_device(device);
1185 
1186     return sysfs_emit(buf, "%s\n", uid_string);
1187 }
1188 static DEVICE_ATTR(uid, 0444, dasd_uid_show, NULL);
1189 
1190 /*
1191  * extended error-reporting
1192  */
1193 static ssize_t
1194 dasd_eer_show(struct device *dev, struct device_attribute *attr, char *buf)
1195 {
1196     struct dasd_devmap *devmap;
1197     int eer_flag;
1198 
1199     devmap = dasd_find_busid(dev_name(dev));
1200     if (!IS_ERR(devmap) && devmap->device)
1201         eer_flag = dasd_eer_enabled(devmap->device);
1202     else
1203         eer_flag = 0;
1204     return sysfs_emit(buf, eer_flag ? "1\n" : "0\n");
1205 }
1206 
1207 static ssize_t
1208 dasd_eer_store(struct device *dev, struct device_attribute *attr,
1209            const char *buf, size_t count)
1210 {
1211     struct dasd_device *device;
1212     unsigned int val;
1213     int rc = 0;
1214 
1215     device = dasd_device_from_cdev(to_ccwdev(dev));
1216     if (IS_ERR(device))
1217         return PTR_ERR(device);
1218 
1219     if (kstrtouint(buf, 0, &val) || val > 1)
1220         return -EINVAL;
1221 
1222     if (val)
1223         rc = dasd_eer_enable(device);
1224     else
1225         dasd_eer_disable(device);
1226 
1227     dasd_put_device(device);
1228 
1229     return rc ? : count;
1230 }
1231 
1232 static DEVICE_ATTR(eer_enabled, 0644, dasd_eer_show, dasd_eer_store);
1233 
1234 /*
1235  * expiration time for default requests
1236  */
1237 static ssize_t
1238 dasd_expires_show(struct device *dev, struct device_attribute *attr, char *buf)
1239 {
1240     struct dasd_device *device;
1241     int len;
1242 
1243     device = dasd_device_from_cdev(to_ccwdev(dev));
1244     if (IS_ERR(device))
1245         return -ENODEV;
1246     len = sysfs_emit(buf, "%lu\n", device->default_expires);
1247     dasd_put_device(device);
1248     return len;
1249 }
1250 
1251 static ssize_t
1252 dasd_expires_store(struct device *dev, struct device_attribute *attr,
1253            const char *buf, size_t count)
1254 {
1255     struct dasd_device *device;
1256     unsigned long val;
1257 
1258     device = dasd_device_from_cdev(to_ccwdev(dev));
1259     if (IS_ERR(device))
1260         return -ENODEV;
1261 
1262     if ((kstrtoul(buf, 10, &val) != 0) ||
1263         (val > DASD_EXPIRES_MAX) || val == 0) {
1264         dasd_put_device(device);
1265         return -EINVAL;
1266     }
1267 
1268     if (val)
1269         device->default_expires = val;
1270 
1271     dasd_put_device(device);
1272     return count;
1273 }
1274 
1275 static DEVICE_ATTR(expires, 0644, dasd_expires_show, dasd_expires_store);
1276 
1277 static ssize_t
1278 dasd_retries_show(struct device *dev, struct device_attribute *attr, char *buf)
1279 {
1280     struct dasd_device *device;
1281     int len;
1282 
1283     device = dasd_device_from_cdev(to_ccwdev(dev));
1284     if (IS_ERR(device))
1285         return -ENODEV;
1286     len = sysfs_emit(buf, "%lu\n", device->default_retries);
1287     dasd_put_device(device);
1288     return len;
1289 }
1290 
1291 static ssize_t
1292 dasd_retries_store(struct device *dev, struct device_attribute *attr,
1293            const char *buf, size_t count)
1294 {
1295     struct dasd_device *device;
1296     unsigned long val;
1297 
1298     device = dasd_device_from_cdev(to_ccwdev(dev));
1299     if (IS_ERR(device))
1300         return -ENODEV;
1301 
1302     if ((kstrtoul(buf, 10, &val) != 0) ||
1303         (val > DASD_RETRIES_MAX)) {
1304         dasd_put_device(device);
1305         return -EINVAL;
1306     }
1307 
1308     if (val)
1309         device->default_retries = val;
1310 
1311     dasd_put_device(device);
1312     return count;
1313 }
1314 
1315 static DEVICE_ATTR(retries, 0644, dasd_retries_show, dasd_retries_store);
1316 
1317 static ssize_t
1318 dasd_timeout_show(struct device *dev, struct device_attribute *attr,
1319           char *buf)
1320 {
1321     struct dasd_device *device;
1322     int len;
1323 
1324     device = dasd_device_from_cdev(to_ccwdev(dev));
1325     if (IS_ERR(device))
1326         return -ENODEV;
1327     len = sysfs_emit(buf, "%lu\n", device->blk_timeout);
1328     dasd_put_device(device);
1329     return len;
1330 }
1331 
1332 static ssize_t
1333 dasd_timeout_store(struct device *dev, struct device_attribute *attr,
1334            const char *buf, size_t count)
1335 {
1336     struct dasd_device *device;
1337     struct request_queue *q;
1338     unsigned long val;
1339 
1340     device = dasd_device_from_cdev(to_ccwdev(dev));
1341     if (IS_ERR(device) || !device->block)
1342         return -ENODEV;
1343 
1344     if ((kstrtoul(buf, 10, &val) != 0) ||
1345         val > UINT_MAX / HZ) {
1346         dasd_put_device(device);
1347         return -EINVAL;
1348     }
1349     q = device->block->request_queue;
1350     if (!q) {
1351         dasd_put_device(device);
1352         return -ENODEV;
1353     }
1354 
1355     device->blk_timeout = val;
1356 
1357     blk_queue_rq_timeout(q, device->blk_timeout * HZ);
1358 
1359     dasd_put_device(device);
1360     return count;
1361 }
1362 
1363 static DEVICE_ATTR(timeout, 0644,
1364            dasd_timeout_show, dasd_timeout_store);
1365 
1366 
1367 static ssize_t
1368 dasd_path_reset_store(struct device *dev, struct device_attribute *attr,
1369               const char *buf, size_t count)
1370 {
1371     struct dasd_device *device;
1372     unsigned int val;
1373 
1374     device = dasd_device_from_cdev(to_ccwdev(dev));
1375     if (IS_ERR(device))
1376         return -ENODEV;
1377 
1378     if ((kstrtouint(buf, 16, &val) != 0) || val > 0xff)
1379         val = 0;
1380 
1381     if (device->discipline && device->discipline->reset_path)
1382         device->discipline->reset_path(device, (__u8) val);
1383 
1384     dasd_put_device(device);
1385     return count;
1386 }
1387 
1388 static DEVICE_ATTR(path_reset, 0200, NULL, dasd_path_reset_store);
1389 
1390 static ssize_t dasd_hpf_show(struct device *dev, struct device_attribute *attr,
1391                  char *buf)
1392 {
1393     struct dasd_device *device;
1394     int hpf;
1395 
1396     device = dasd_device_from_cdev(to_ccwdev(dev));
1397     if (IS_ERR(device))
1398         return -ENODEV;
1399     if (!device->discipline || !device->discipline->hpf_enabled) {
1400         dasd_put_device(device);
1401         return sysfs_emit(buf, "%d\n", dasd_nofcx);
1402     }
1403     hpf = device->discipline->hpf_enabled(device);
1404     dasd_put_device(device);
1405     return sysfs_emit(buf, "%d\n", hpf);
1406 }
1407 
1408 static DEVICE_ATTR(hpf, 0444, dasd_hpf_show, NULL);
1409 
1410 static ssize_t dasd_reservation_policy_show(struct device *dev,
1411                         struct device_attribute *attr,
1412                         char *buf)
1413 {
1414     struct dasd_devmap *devmap;
1415     int rc = 0;
1416 
1417     devmap = dasd_find_busid(dev_name(dev));
1418     if (IS_ERR(devmap)) {
1419         rc = sysfs_emit(buf, "ignore\n");
1420     } else {
1421         spin_lock(&dasd_devmap_lock);
1422         if (devmap->features & DASD_FEATURE_FAILONSLCK)
1423             rc = sysfs_emit(buf, "fail\n");
1424         else
1425             rc = sysfs_emit(buf, "ignore\n");
1426         spin_unlock(&dasd_devmap_lock);
1427     }
1428     return rc;
1429 }
1430 
1431 static ssize_t dasd_reservation_policy_store(struct device *dev,
1432                          struct device_attribute *attr,
1433                          const char *buf, size_t count)
1434 {
1435     struct ccw_device *cdev = to_ccwdev(dev);
1436     int rc;
1437 
1438     if (sysfs_streq("ignore", buf))
1439         rc = dasd_set_feature(cdev, DASD_FEATURE_FAILONSLCK, 0);
1440     else if (sysfs_streq("fail", buf))
1441         rc = dasd_set_feature(cdev, DASD_FEATURE_FAILONSLCK, 1);
1442     else
1443         rc = -EINVAL;
1444 
1445     return rc ? : count;
1446 }
1447 
1448 static DEVICE_ATTR(reservation_policy, 0644,
1449            dasd_reservation_policy_show, dasd_reservation_policy_store);
1450 
1451 static ssize_t dasd_reservation_state_show(struct device *dev,
1452                        struct device_attribute *attr,
1453                        char *buf)
1454 {
1455     struct dasd_device *device;
1456     int rc = 0;
1457 
1458     device = dasd_device_from_cdev(to_ccwdev(dev));
1459     if (IS_ERR(device))
1460         return sysfs_emit(buf, "none\n");
1461 
1462     if (test_bit(DASD_FLAG_IS_RESERVED, &device->flags))
1463         rc = sysfs_emit(buf, "reserved\n");
1464     else if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags))
1465         rc = sysfs_emit(buf, "lost\n");
1466     else
1467         rc = sysfs_emit(buf, "none\n");
1468     dasd_put_device(device);
1469     return rc;
1470 }
1471 
1472 static ssize_t dasd_reservation_state_store(struct device *dev,
1473                         struct device_attribute *attr,
1474                         const char *buf, size_t count)
1475 {
1476     struct dasd_device *device;
1477     int rc = 0;
1478 
1479     device = dasd_device_from_cdev(to_ccwdev(dev));
1480     if (IS_ERR(device))
1481         return -ENODEV;
1482     if (sysfs_streq("reset", buf))
1483         clear_bit(DASD_FLAG_LOCK_STOLEN, &device->flags);
1484     else
1485         rc = -EINVAL;
1486     dasd_put_device(device);
1487 
1488     if (rc)
1489         return rc;
1490     else
1491         return count;
1492 }
1493 
1494 static DEVICE_ATTR(last_known_reservation_state, 0644,
1495            dasd_reservation_state_show, dasd_reservation_state_store);
1496 
1497 static ssize_t dasd_pm_show(struct device *dev,
1498                   struct device_attribute *attr, char *buf)
1499 {
1500     struct dasd_device *device;
1501     u8 opm, nppm, cablepm, cuirpm, hpfpm, ifccpm;
1502 
1503     device = dasd_device_from_cdev(to_ccwdev(dev));
1504     if (IS_ERR(device))
1505         return sprintf(buf, "0\n");
1506 
1507     opm = dasd_path_get_opm(device);
1508     nppm = dasd_path_get_nppm(device);
1509     cablepm = dasd_path_get_cablepm(device);
1510     cuirpm = dasd_path_get_cuirpm(device);
1511     hpfpm = dasd_path_get_hpfpm(device);
1512     ifccpm = dasd_path_get_ifccpm(device);
1513     dasd_put_device(device);
1514 
1515     return sprintf(buf, "%02x %02x %02x %02x %02x %02x\n", opm, nppm,
1516                cablepm, cuirpm, hpfpm, ifccpm);
1517 }
1518 
1519 static DEVICE_ATTR(path_masks, 0444, dasd_pm_show, NULL);
1520 
1521 /*
1522  * threshold value for IFCC/CCC errors
1523  */
1524 static ssize_t
1525 dasd_path_threshold_show(struct device *dev,
1526               struct device_attribute *attr, char *buf)
1527 {
1528     struct dasd_device *device;
1529     int len;
1530 
1531     device = dasd_device_from_cdev(to_ccwdev(dev));
1532     if (IS_ERR(device))
1533         return -ENODEV;
1534     len = sysfs_emit(buf, "%lu\n", device->path_thrhld);
1535     dasd_put_device(device);
1536     return len;
1537 }
1538 
1539 static ssize_t
1540 dasd_path_threshold_store(struct device *dev, struct device_attribute *attr,
1541                const char *buf, size_t count)
1542 {
1543     struct dasd_device *device;
1544     unsigned long flags;
1545     unsigned long val;
1546 
1547     device = dasd_device_from_cdev(to_ccwdev(dev));
1548     if (IS_ERR(device))
1549         return -ENODEV;
1550 
1551     if (kstrtoul(buf, 10, &val) != 0 || val > DASD_THRHLD_MAX) {
1552         dasd_put_device(device);
1553         return -EINVAL;
1554     }
1555     spin_lock_irqsave(get_ccwdev_lock(to_ccwdev(dev)), flags);
1556     device->path_thrhld = val;
1557     spin_unlock_irqrestore(get_ccwdev_lock(to_ccwdev(dev)), flags);
1558     dasd_put_device(device);
1559     return count;
1560 }
1561 static DEVICE_ATTR(path_threshold, 0644, dasd_path_threshold_show,
1562            dasd_path_threshold_store);
1563 
1564 /*
1565  * configure if path is disabled after IFCC/CCC error threshold is
1566  * exceeded
1567  */
1568 static ssize_t
1569 dasd_path_autodisable_show(struct device *dev,
1570                    struct device_attribute *attr, char *buf)
1571 {
1572     struct dasd_devmap *devmap;
1573     int flag;
1574 
1575     devmap = dasd_find_busid(dev_name(dev));
1576     if (!IS_ERR(devmap))
1577         flag = (devmap->features & DASD_FEATURE_PATH_AUTODISABLE) != 0;
1578     else
1579         flag = (DASD_FEATURE_DEFAULT &
1580             DASD_FEATURE_PATH_AUTODISABLE) != 0;
1581     return sysfs_emit(buf, flag ? "1\n" : "0\n");
1582 }
1583 
1584 static ssize_t
1585 dasd_path_autodisable_store(struct device *dev,
1586                     struct device_attribute *attr,
1587                     const char *buf, size_t count)
1588 {
1589     unsigned int val;
1590     int rc;
1591 
1592     if (kstrtouint(buf, 0, &val) || val > 1)
1593         return -EINVAL;
1594 
1595     rc = dasd_set_feature(to_ccwdev(dev),
1596                   DASD_FEATURE_PATH_AUTODISABLE, val);
1597 
1598     return rc ? : count;
1599 }
1600 
1601 static DEVICE_ATTR(path_autodisable, 0644,
1602            dasd_path_autodisable_show,
1603            dasd_path_autodisable_store);
1604 /*
1605  * interval for IFCC/CCC checks
1606  * meaning time with no IFCC/CCC error before the error counter
1607  * gets reset
1608  */
1609 static ssize_t
1610 dasd_path_interval_show(struct device *dev,
1611             struct device_attribute *attr, char *buf)
1612 {
1613     struct dasd_device *device;
1614     int len;
1615 
1616     device = dasd_device_from_cdev(to_ccwdev(dev));
1617     if (IS_ERR(device))
1618         return -ENODEV;
1619     len = sysfs_emit(buf, "%lu\n", device->path_interval);
1620     dasd_put_device(device);
1621     return len;
1622 }
1623 
1624 static ssize_t
1625 dasd_path_interval_store(struct device *dev, struct device_attribute *attr,
1626            const char *buf, size_t count)
1627 {
1628     struct dasd_device *device;
1629     unsigned long flags;
1630     unsigned long val;
1631 
1632     device = dasd_device_from_cdev(to_ccwdev(dev));
1633     if (IS_ERR(device))
1634         return -ENODEV;
1635 
1636     if ((kstrtoul(buf, 10, &val) != 0) ||
1637         (val > DASD_INTERVAL_MAX) || val == 0) {
1638         dasd_put_device(device);
1639         return -EINVAL;
1640     }
1641     spin_lock_irqsave(get_ccwdev_lock(to_ccwdev(dev)), flags);
1642     if (val)
1643         device->path_interval = val;
1644     spin_unlock_irqrestore(get_ccwdev_lock(to_ccwdev(dev)), flags);
1645     dasd_put_device(device);
1646     return count;
1647 }
1648 
1649 static DEVICE_ATTR(path_interval, 0644, dasd_path_interval_show,
1650            dasd_path_interval_store);
1651 
1652 static ssize_t
1653 dasd_device_fcs_show(struct device *dev, struct device_attribute *attr,
1654              char *buf)
1655 {
1656     struct dasd_device *device;
1657     int fc_sec;
1658     int rc;
1659 
1660     device = dasd_device_from_cdev(to_ccwdev(dev));
1661     if (IS_ERR(device))
1662         return -ENODEV;
1663     fc_sec = dasd_path_get_fcs_device(device);
1664     if (fc_sec == -EINVAL)
1665         rc = sysfs_emit(buf, "Inconsistent\n");
1666     else
1667         rc = sysfs_emit(buf, "%s\n", dasd_path_get_fcs_str(fc_sec));
1668     dasd_put_device(device);
1669 
1670     return rc;
1671 }
1672 static DEVICE_ATTR(fc_security, 0444, dasd_device_fcs_show, NULL);
1673 
1674 static ssize_t
1675 dasd_path_fcs_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
1676 {
1677     struct dasd_path *path = to_dasd_path(kobj);
1678     unsigned int fc_sec = path->fc_security;
1679 
1680     return sysfs_emit(buf, "%s\n", dasd_path_get_fcs_str(fc_sec));
1681 }
1682 
1683 static struct kobj_attribute path_fcs_attribute =
1684     __ATTR(fc_security, 0444, dasd_path_fcs_show, NULL);
1685 
1686 #define DASD_DEFINE_ATTR(_name, _func)                  \
1687 static ssize_t dasd_##_name##_show(struct device *dev,          \
1688                    struct device_attribute *attr,   \
1689                    char *buf)               \
1690 {                                   \
1691     struct ccw_device *cdev = to_ccwdev(dev);           \
1692     struct dasd_device *device = dasd_device_from_cdev(cdev);   \
1693     int val = 0;                            \
1694                                     \
1695     if (IS_ERR(device))                     \
1696         return -ENODEV;                     \
1697     if (device->discipline && _func)                \
1698         val = _func(device);                    \
1699     dasd_put_device(device);                    \
1700                                     \
1701     return sysfs_emit(buf, "%d\n", val);            \
1702 }                                   \
1703 static DEVICE_ATTR(_name, 0444, dasd_##_name##_show, NULL);     \
1704 
1705 DASD_DEFINE_ATTR(ese, device->discipline->is_ese);
1706 DASD_DEFINE_ATTR(extent_size, device->discipline->ext_size);
1707 DASD_DEFINE_ATTR(pool_id, device->discipline->ext_pool_id);
1708 DASD_DEFINE_ATTR(space_configured, device->discipline->space_configured);
1709 DASD_DEFINE_ATTR(space_allocated, device->discipline->space_allocated);
1710 DASD_DEFINE_ATTR(logical_capacity, device->discipline->logical_capacity);
1711 DASD_DEFINE_ATTR(warn_threshold, device->discipline->ext_pool_warn_thrshld);
1712 DASD_DEFINE_ATTR(cap_at_warnlevel, device->discipline->ext_pool_cap_at_warnlevel);
1713 DASD_DEFINE_ATTR(pool_oos, device->discipline->ext_pool_oos);
1714 
1715 static struct attribute * dasd_attrs[] = {
1716     &dev_attr_readonly.attr,
1717     &dev_attr_discipline.attr,
1718     &dev_attr_status.attr,
1719     &dev_attr_alias.attr,
1720     &dev_attr_vendor.attr,
1721     &dev_attr_uid.attr,
1722     &dev_attr_use_diag.attr,
1723     &dev_attr_raw_track_access.attr,
1724     &dev_attr_eer_enabled.attr,
1725     &dev_attr_erplog.attr,
1726     &dev_attr_failfast.attr,
1727     &dev_attr_expires.attr,
1728     &dev_attr_retries.attr,
1729     &dev_attr_timeout.attr,
1730     &dev_attr_reservation_policy.attr,
1731     &dev_attr_last_known_reservation_state.attr,
1732     &dev_attr_safe_offline.attr,
1733     &dev_attr_host_access_count.attr,
1734     &dev_attr_path_masks.attr,
1735     &dev_attr_path_threshold.attr,
1736     &dev_attr_path_autodisable.attr,
1737     &dev_attr_path_interval.attr,
1738     &dev_attr_path_reset.attr,
1739     &dev_attr_hpf.attr,
1740     &dev_attr_ese.attr,
1741     &dev_attr_fc_security.attr,
1742     NULL,
1743 };
1744 
1745 static const struct attribute_group dasd_attr_group = {
1746     .attrs = dasd_attrs,
1747 };
1748 
1749 static struct attribute *capacity_attrs[] = {
1750     &dev_attr_space_configured.attr,
1751     &dev_attr_space_allocated.attr,
1752     &dev_attr_logical_capacity.attr,
1753     NULL,
1754 };
1755 
1756 static const struct attribute_group capacity_attr_group = {
1757     .name = "capacity",
1758     .attrs = capacity_attrs,
1759 };
1760 
1761 static struct attribute *ext_pool_attrs[] = {
1762     &dev_attr_pool_id.attr,
1763     &dev_attr_extent_size.attr,
1764     &dev_attr_warn_threshold.attr,
1765     &dev_attr_cap_at_warnlevel.attr,
1766     &dev_attr_pool_oos.attr,
1767     NULL,
1768 };
1769 
1770 static const struct attribute_group ext_pool_attr_group = {
1771     .name = "extent_pool",
1772     .attrs = ext_pool_attrs,
1773 };
1774 
1775 const struct attribute_group *dasd_dev_groups[] = {
1776     &dasd_attr_group,
1777     &capacity_attr_group,
1778     &ext_pool_attr_group,
1779     NULL,
1780 };
1781 EXPORT_SYMBOL_GPL(dasd_dev_groups);
1782 
1783 /*
1784  * Return value of the specified feature.
1785  */
1786 int
1787 dasd_get_feature(struct ccw_device *cdev, int feature)
1788 {
1789     struct dasd_devmap *devmap;
1790 
1791     devmap = dasd_find_busid(dev_name(&cdev->dev));
1792     if (IS_ERR(devmap))
1793         return PTR_ERR(devmap);
1794 
1795     return ((devmap->features & feature) != 0);
1796 }
1797 
1798 /*
1799  * Set / reset given feature.
1800  * Flag indicates whether to set (!=0) or the reset (=0) the feature.
1801  */
1802 int
1803 dasd_set_feature(struct ccw_device *cdev, int feature, int flag)
1804 {
1805     struct dasd_devmap *devmap;
1806 
1807     devmap = dasd_devmap_from_cdev(cdev);
1808     if (IS_ERR(devmap))
1809         return PTR_ERR(devmap);
1810 
1811     spin_lock(&dasd_devmap_lock);
1812     if (flag)
1813         devmap->features |= feature;
1814     else
1815         devmap->features &= ~feature;
1816     if (devmap->device)
1817         devmap->device->features = devmap->features;
1818     spin_unlock(&dasd_devmap_lock);
1819     return 0;
1820 }
1821 EXPORT_SYMBOL(dasd_set_feature);
1822 
1823 static struct attribute *paths_info_attrs[] = {
1824     &path_fcs_attribute.attr,
1825     NULL,
1826 };
1827 ATTRIBUTE_GROUPS(paths_info);
1828 
1829 static struct kobj_type path_attr_type = {
1830     .release    = dasd_path_release,
1831     .default_groups = paths_info_groups,
1832     .sysfs_ops  = &kobj_sysfs_ops,
1833 };
1834 
1835 static void dasd_path_init_kobj(struct dasd_device *device, int chp)
1836 {
1837     device->path[chp].kobj.kset = device->paths_info;
1838     kobject_init(&device->path[chp].kobj, &path_attr_type);
1839 }
1840 
1841 void dasd_path_create_kobj(struct dasd_device *device, int chp)
1842 {
1843     int rc;
1844 
1845     if (test_bit(DASD_FLAG_OFFLINE, &device->flags))
1846         return;
1847     if (!device->paths_info) {
1848         dev_warn(&device->cdev->dev, "Unable to create paths objects\n");
1849         return;
1850     }
1851     if (device->path[chp].in_sysfs)
1852         return;
1853     if (!device->path[chp].conf_data)
1854         return;
1855 
1856     dasd_path_init_kobj(device, chp);
1857 
1858     rc = kobject_add(&device->path[chp].kobj, NULL, "%x.%02x",
1859              device->path[chp].cssid, device->path[chp].chpid);
1860     if (rc)
1861         kobject_put(&device->path[chp].kobj);
1862     device->path[chp].in_sysfs = true;
1863 }
1864 EXPORT_SYMBOL(dasd_path_create_kobj);
1865 
1866 void dasd_path_create_kobjects(struct dasd_device *device)
1867 {
1868     u8 lpm, opm;
1869 
1870     opm = dasd_path_get_opm(device);
1871     for (lpm = 0x80; lpm; lpm >>= 1) {
1872         if (!(lpm & opm))
1873             continue;
1874         dasd_path_create_kobj(device, pathmask_to_pos(lpm));
1875     }
1876 }
1877 EXPORT_SYMBOL(dasd_path_create_kobjects);
1878 
1879 static void dasd_path_remove_kobj(struct dasd_device *device, int chp)
1880 {
1881     if (device->path[chp].in_sysfs) {
1882         kobject_put(&device->path[chp].kobj);
1883         device->path[chp].in_sysfs = false;
1884     }
1885 }
1886 
1887 /*
1888  * As we keep kobjects for the lifetime of a device, this function must not be
1889  * called anywhere but in the context of offlining a device.
1890  */
1891 void dasd_path_remove_kobjects(struct dasd_device *device)
1892 {
1893     int i;
1894 
1895     for (i = 0; i < 8; i++)
1896         dasd_path_remove_kobj(device, i);
1897 }
1898 EXPORT_SYMBOL(dasd_path_remove_kobjects);
1899 
1900 int
1901 dasd_devmap_init(void)
1902 {
1903     int i;
1904 
1905     /* Initialize devmap structures. */
1906     dasd_max_devindex = 0;
1907     for (i = 0; i < 256; i++)
1908         INIT_LIST_HEAD(&dasd_hashlists[i]);
1909     return 0;
1910 }
1911 
1912 void
1913 dasd_devmap_exit(void)
1914 {
1915     dasd_forget_ranges();
1916 }