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