2 * File...........: linux/drivers/s390/block/dasd_devmap.c
3 * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
4 * Horst Hummel <Horst.Hummel@de.ibm.com>
5 * Carsten Otte <Cotte@de.ibm.com>
6 * Martin Schwidefsky <schwidefsky@de.ibm.com>
7 * Bugreports.to..: <Linux390@de.ibm.com>
8 * (C) IBM Corporation, IBM Deutschland Entwicklung GmbH, 1999-2001
10 * Device mapping and dasd= parameter parsing functions. All devmap
11 * functions may not be called from interrupt context. In particular
12 * dasd_get_device is a no-no from interrupt context.
16 #include <linux/config.h>
17 #include <linux/ctype.h>
18 #include <linux/init.h>
20 #include <asm/debug.h>
21 #include <asm/uaccess.h>
24 #define PRINTK_HEADER "dasd_devmap:"
28 kmem_cache_t
*dasd_page_cache
;
29 EXPORT_SYMBOL(dasd_page_cache
);
32 * dasd_devmap_t is used to store the features and the relation
33 * between device number and device index. To find a dasd_devmap_t
34 * that corresponds to a device number of a device index each
35 * dasd_devmap_t is added to two linked lists, one to search by
36 * the device number and one to search by the device index. As
37 * soon as big minor numbers are available the device index list
38 * can be removed since the device number will then be identical
39 * to the device index.
42 struct list_head list
;
43 char bus_id
[BUS_ID_SIZE
];
44 unsigned int devindex
;
45 unsigned short features
;
46 struct dasd_device
*device
;
50 * Parameter parsing functions for dasd= parameter. The syntax is:
51 * <devno> : (0x)?[0-9a-fA-F]+
52 * <busid> : [0-0a-f]\.[0-9a-f]\.(0x)?[0-9a-fA-F]+
54 * <feature_list> : \(<feature>(:<feature>)*\)
55 * <devno-range> : <devno>(-<devno>)?<feature_list>?
56 * <busid-range> : <busid>(-<busid>)?<feature_list>?
57 * <devices> : <devno-range>|<busid-range>
58 * <dasd_module> : dasd_diag_mod|dasd_eckd_mod|dasd_fba_mod
60 * <dasd> : autodetect|probeonly|<devices>(,<devices>)*
63 int dasd_probeonly
= 0; /* is true, when probeonly mode is active */
64 int dasd_autodetect
= 0; /* is true, when autodetection is active */
67 * char *dasd[] is intended to hold the ranges supplied by the dasd= statement
68 * it is named 'dasd' to directly be filled by insmod with the comma separated
69 * strings when running as a module.
71 static char *dasd
[256];
73 * Single spinlock to protect devmap structures and lists.
75 static DEFINE_SPINLOCK(dasd_devmap_lock
);
78 * Hash lists for devmap structures.
80 static struct list_head dasd_hashlists
[256];
81 int dasd_max_devindex
;
83 static struct dasd_devmap
*dasd_add_busid(char *, int);
86 dasd_hash_busid(char *bus_id
)
91 for (i
= 0; (i
< BUS_ID_SIZE
) && *bus_id
; i
++, bus_id
++)
98 * The parameter parsing functions for builtin-drivers are called
99 * before kmalloc works. Store the pointers to the parameters strings
100 * into dasd[] for later processing.
103 dasd_call_setup(char *str
)
105 static int count
= 0;
112 __setup ("dasd=", dasd_call_setup
);
113 #endif /* #ifndef MODULE */
116 * Read a device busid/devno from a string.
119 dasd_busid(char **str
, int *id0
, int *id1
, int *devno
)
123 /* check for leading '0x' */
125 if ((*str
)[0] == '0' && (*str
)[1] == 'x') {
129 if (!isxdigit((*str
)[0])) /* We require at least one hex digit */
131 val
= simple_strtoul(*str
, str
, 16);
132 if (old_style
|| (*str
)[0] != '.') {
134 if (val
< 0 || val
> 0xffff)
139 /* New style x.y.z busid */
140 if (val
< 0 || val
> 0xff)
144 if (!isxdigit((*str
)[0])) /* We require at least one hex digit */
146 val
= simple_strtoul(*str
, str
, 16);
147 if (val
< 0 || val
> 0xff || (*str
)++[0] != '.')
150 if (!isxdigit((*str
)[0])) /* We require at least one hex digit */
152 val
= simple_strtoul(*str
, str
, 16);
153 if (val
< 0 || val
> 0xffff)
160 * Read colon separated list of dasd features. Currently there is
161 * only one: "ro" for read-only devices. The default feature set
162 * is empty (value 0).
165 dasd_feature_list(char *str
, char **endp
)
167 int features
, len
, rc
;
172 return DASD_FEATURE_DEFAULT
;
179 str
[len
] && str
[len
] != ':' && str
[len
] != ')'; len
++);
180 if (len
== 2 && !strncmp(str
, "ro", 2))
181 features
|= DASD_FEATURE_READONLY
;
182 else if (len
== 4 && !strncmp(str
, "diag", 4))
183 features
|= DASD_FEATURE_USEDIAG
;
185 MESSAGE(KERN_WARNING
,
186 "unsupported feature: %*s, "
187 "ignoring setting", len
, str
);
196 MESSAGE(KERN_WARNING
, "%s",
197 "missing ')' in dasd parameter string\n");
208 * Try to match the first element on the comma separated parse string
209 * with one of the known keywords. If a keyword is found, take the approprate
210 * action and return a pointer to the residual string. If the first element
211 * could not be matched to any keyword then return an error code.
214 dasd_parse_keyword( char *parsestring
) {
216 char *nextcomma
, *residual_str
;
219 nextcomma
= strchr(parsestring
,',');
221 length
= nextcomma
- parsestring
;
222 residual_str
= nextcomma
+ 1;
224 length
= strlen(parsestring
);
225 residual_str
= parsestring
+ length
;
227 if (strncmp ("autodetect", parsestring
, length
) == 0) {
229 MESSAGE (KERN_INFO
, "%s",
230 "turning to autodetection mode");
233 if (strncmp ("probeonly", parsestring
, length
) == 0) {
235 MESSAGE(KERN_INFO
, "%s",
236 "turning to probeonly mode");
239 if (strncmp ("fixedbuffers", parsestring
, length
) == 0) {
243 kmem_cache_create("dasd_page_cache", PAGE_SIZE
, 0,
244 SLAB_CACHE_DMA
, NULL
, NULL
);
245 if (!dasd_page_cache
)
246 MESSAGE(KERN_WARNING
, "%s", "Failed to create slab, "
247 "fixed buffer mode disabled.");
249 MESSAGE (KERN_INFO
, "%s",
250 "turning on fixed buffer mode");
253 return ERR_PTR(-EINVAL
);
257 * Try to interprete the first element on the comma separated parse string
258 * as a device number or a range of devices. If the interpretation is
259 * successfull, create the matching dasd_devmap entries and return a pointer
260 * to the residual string.
261 * If interpretation fails or in case of an error, return an error code.
264 dasd_parse_range( char *parsestring
) {
266 struct dasd_devmap
*devmap
;
267 int from
, from_id0
, from_id1
;
268 int to
, to_id0
, to_id1
;
270 char bus_id
[BUS_ID_SIZE
+1], *str
;
273 rc
= dasd_busid(&str
, &from_id0
, &from_id1
, &from
);
280 rc
= dasd_busid(&str
, &to_id0
, &to_id1
, &to
);
284 (from_id0
!= to_id0
|| from_id1
!= to_id1
|| from
> to
))
287 MESSAGE(KERN_ERR
, "Invalid device range %s", parsestring
);
290 features
= dasd_feature_list(str
, &str
);
292 return ERR_PTR(-EINVAL
);
294 sprintf(bus_id
, "%01x.%01x.%04x",
295 from_id0
, from_id1
, from
++);
296 devmap
= dasd_add_busid(bus_id
, features
);
298 return (char *)devmap
;
304 MESSAGE(KERN_WARNING
,
305 "junk at end of dasd parameter string: %s\n", str
);
306 return ERR_PTR(-EINVAL
);
310 dasd_parse_next_element( char *parsestring
) {
312 residual_str
= dasd_parse_keyword(parsestring
);
313 if (!IS_ERR(residual_str
))
315 residual_str
= dasd_parse_range(parsestring
);
320 * Parse parameters stored in dasd[]
321 * The 'dasd=...' parameter allows to specify a comma separated list of
322 * keywords and device ranges. When the dasd driver is build into the kernel,
323 * the complete list will be stored as one element of the dasd[] array.
324 * When the dasd driver is build as a module, then the list is broken into
325 * it's elements and each dasd[] entry contains one element.
334 for (i
= 0; i
< 256; i
++) {
337 parsestring
= dasd
[i
];
338 /* loop over the comma separated list in the parsestring */
339 while (*parsestring
) {
340 parsestring
= dasd_parse_next_element(parsestring
);
341 if(IS_ERR(parsestring
)) {
342 rc
= PTR_ERR(parsestring
);
347 DBF_EVENT(DBF_ALERT
, "%s", "invalid range found");
355 * Add a devmap for the device specified by busid. It is possible that
356 * the devmap already exists (dasd= parameter). The order of the devices
357 * added through this function will define the kdevs for the individual
360 static struct dasd_devmap
*
361 dasd_add_busid(char *bus_id
, int features
)
363 struct dasd_devmap
*devmap
, *new, *tmp
;
366 new = (struct dasd_devmap
*)
367 kmalloc(sizeof(struct dasd_devmap
), GFP_KERNEL
);
369 return ERR_PTR(-ENOMEM
);
370 spin_lock(&dasd_devmap_lock
);
372 hash
= dasd_hash_busid(bus_id
);
373 list_for_each_entry(tmp
, &dasd_hashlists
[hash
], list
)
374 if (strncmp(tmp
->bus_id
, bus_id
, BUS_ID_SIZE
) == 0) {
379 /* This bus_id is new. */
380 new->devindex
= dasd_max_devindex
++;
381 strncpy(new->bus_id
, bus_id
, BUS_ID_SIZE
);
382 new->features
= features
;
384 list_add(&new->list
, &dasd_hashlists
[hash
]);
388 spin_unlock(&dasd_devmap_lock
);
394 * Find devmap for device with given bus_id.
396 static struct dasd_devmap
*
397 dasd_find_busid(char *bus_id
)
399 struct dasd_devmap
*devmap
, *tmp
;
402 spin_lock(&dasd_devmap_lock
);
403 devmap
= ERR_PTR(-ENODEV
);
404 hash
= dasd_hash_busid(bus_id
);
405 list_for_each_entry(tmp
, &dasd_hashlists
[hash
], list
) {
406 if (strncmp(tmp
->bus_id
, bus_id
, BUS_ID_SIZE
) == 0) {
411 spin_unlock(&dasd_devmap_lock
);
416 * Check if busid has been added to the list of dasd ranges.
419 dasd_busid_known(char *bus_id
)
421 return IS_ERR(dasd_find_busid(bus_id
)) ? -ENOENT
: 0;
425 * Forget all about the device numbers added so far.
426 * This may only be called at module unload or system shutdown.
429 dasd_forget_ranges(void)
431 struct dasd_devmap
*devmap
, *n
;
434 spin_lock(&dasd_devmap_lock
);
435 for (i
= 0; i
< 256; i
++) {
436 list_for_each_entry_safe(devmap
, n
, &dasd_hashlists
[i
], list
) {
437 if (devmap
->device
!= NULL
)
439 list_del(&devmap
->list
);
443 spin_unlock(&dasd_devmap_lock
);
447 * Find the device struct by its device index.
450 dasd_device_from_devindex(int devindex
)
452 struct dasd_devmap
*devmap
, *tmp
;
453 struct dasd_device
*device
;
456 spin_lock(&dasd_devmap_lock
);
458 for (i
= 0; (i
< 256) && !devmap
; i
++)
459 list_for_each_entry(tmp
, &dasd_hashlists
[i
], list
)
460 if (tmp
->devindex
== devindex
) {
461 /* Found the devmap for the device. */
465 if (devmap
&& devmap
->device
) {
466 device
= devmap
->device
;
467 dasd_get_device(device
);
469 device
= ERR_PTR(-ENODEV
);
470 spin_unlock(&dasd_devmap_lock
);
475 * Return devmap for cdev. If no devmap exists yet, create one and
476 * connect it to the cdev.
478 static struct dasd_devmap
*
479 dasd_devmap_from_cdev(struct ccw_device
*cdev
)
481 struct dasd_devmap
*devmap
;
483 devmap
= dasd_find_busid(cdev
->dev
.bus_id
);
485 devmap
= dasd_add_busid(cdev
->dev
.bus_id
,
486 DASD_FEATURE_DEFAULT
);
491 * Create a dasd device structure for cdev.
494 dasd_create_device(struct ccw_device
*cdev
)
496 struct dasd_devmap
*devmap
;
497 struct dasd_device
*device
;
500 devmap
= dasd_devmap_from_cdev(cdev
);
502 return (void *) devmap
;
503 cdev
->dev
.driver_data
= devmap
;
505 device
= dasd_alloc_device();
508 atomic_set(&device
->ref_count
, 2);
510 spin_lock(&dasd_devmap_lock
);
511 if (!devmap
->device
) {
512 devmap
->device
= device
;
513 device
->devindex
= devmap
->devindex
;
514 device
->features
= devmap
->features
;
515 get_device(&cdev
->dev
);
519 /* Someone else was faster. */
521 spin_unlock(&dasd_devmap_lock
);
524 dasd_free_device(device
);
531 * Wait queue for dasd_delete_device waits.
533 static DECLARE_WAIT_QUEUE_HEAD(dasd_delete_wq
);
536 * Remove a dasd device structure. The passed referenced
540 dasd_delete_device(struct dasd_device
*device
)
542 struct ccw_device
*cdev
;
543 struct dasd_devmap
*devmap
;
545 /* First remove device pointer from devmap. */
546 devmap
= dasd_find_busid(device
->cdev
->dev
.bus_id
);
549 spin_lock(&dasd_devmap_lock
);
550 if (devmap
->device
!= device
) {
551 spin_unlock(&dasd_devmap_lock
);
552 dasd_put_device(device
);
555 devmap
->device
= NULL
;
556 spin_unlock(&dasd_devmap_lock
);
558 /* Drop ref_count by 2, one for the devmap reference and
559 * one for the passed reference. */
560 atomic_sub(2, &device
->ref_count
);
562 /* Wait for reference counter to drop to zero. */
563 wait_event(dasd_delete_wq
, atomic_read(&device
->ref_count
) == 0);
565 /* Disconnect dasd_device structure from ccw_device structure. */
569 /* Disconnect dasd_devmap structure from ccw_device structure. */
570 cdev
->dev
.driver_data
= NULL
;
572 /* Put ccw_device structure. */
573 put_device(&cdev
->dev
);
575 /* Now the device structure can be freed. */
576 dasd_free_device(device
);
580 * Reference counter dropped to zero. Wake up waiter
581 * in dasd_delete_device.
584 dasd_put_device_wake(struct dasd_device
*device
)
586 wake_up(&dasd_delete_wq
);
590 * Return dasd_device structure associated with cdev.
593 dasd_device_from_cdev(struct ccw_device
*cdev
)
595 struct dasd_devmap
*devmap
;
596 struct dasd_device
*device
;
598 device
= ERR_PTR(-ENODEV
);
599 spin_lock(&dasd_devmap_lock
);
600 devmap
= cdev
->dev
.driver_data
;
601 if (devmap
&& devmap
->device
) {
602 device
= devmap
->device
;
603 dasd_get_device(device
);
605 spin_unlock(&dasd_devmap_lock
);
610 * SECTION: files in sysfs
614 * readonly controls the readonly status of a dasd
617 dasd_ro_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
619 struct dasd_devmap
*devmap
;
622 devmap
= dasd_find_busid(dev
->bus_id
);
624 ro_flag
= (devmap
->features
& DASD_FEATURE_READONLY
) != 0;
626 ro_flag
= (DASD_FEATURE_DEFAULT
& DASD_FEATURE_READONLY
) != 0;
627 return snprintf(buf
, PAGE_SIZE
, ro_flag
? "1\n" : "0\n");
631 dasd_ro_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
633 struct dasd_devmap
*devmap
;
636 devmap
= dasd_devmap_from_cdev(to_ccwdev(dev
));
638 return PTR_ERR(devmap
);
639 ro_flag
= buf
[0] == '1';
640 spin_lock(&dasd_devmap_lock
);
642 devmap
->features
|= DASD_FEATURE_READONLY
;
644 devmap
->features
&= ~DASD_FEATURE_READONLY
;
646 devmap
->device
->features
= devmap
->features
;
647 if (devmap
->device
&& devmap
->device
->gdp
)
648 set_disk_ro(devmap
->device
->gdp
, ro_flag
);
649 spin_unlock(&dasd_devmap_lock
);
653 static DEVICE_ATTR(readonly
, 0644, dasd_ro_show
, dasd_ro_store
);
656 * use_diag controls whether the driver should use diag rather than ssch
657 * to talk to the device
660 dasd_use_diag_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
662 struct dasd_devmap
*devmap
;
665 devmap
= dasd_find_busid(dev
->bus_id
);
667 use_diag
= (devmap
->features
& DASD_FEATURE_USEDIAG
) != 0;
669 use_diag
= (DASD_FEATURE_DEFAULT
& DASD_FEATURE_USEDIAG
) != 0;
670 return sprintf(buf
, use_diag
? "1\n" : "0\n");
674 dasd_use_diag_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
676 struct dasd_devmap
*devmap
;
680 devmap
= dasd_devmap_from_cdev(to_ccwdev(dev
));
682 return PTR_ERR(devmap
);
683 use_diag
= buf
[0] == '1';
684 spin_lock(&dasd_devmap_lock
);
685 /* Changing diag discipline flag is only allowed in offline state. */
687 if (!devmap
->device
) {
689 devmap
->features
|= DASD_FEATURE_USEDIAG
;
691 devmap
->features
&= ~DASD_FEATURE_USEDIAG
;
694 spin_unlock(&dasd_devmap_lock
);
699 DEVICE_ATTR(use_diag
, 0644, dasd_use_diag_show
, dasd_use_diag_store
);
702 dasd_discipline_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
704 struct dasd_devmap
*devmap
;
707 spin_lock(&dasd_devmap_lock
);
709 devmap
= dev
->driver_data
;
710 if (devmap
&& devmap
->device
&& devmap
->device
->discipline
)
711 dname
= devmap
->device
->discipline
->name
;
712 spin_unlock(&dasd_devmap_lock
);
713 return snprintf(buf
, PAGE_SIZE
, "%s\n", dname
);
716 static DEVICE_ATTR(discipline
, 0444, dasd_discipline_show
, NULL
);
718 static struct attribute
* dasd_attrs
[] = {
719 &dev_attr_readonly
.attr
,
720 &dev_attr_discipline
.attr
,
721 &dev_attr_use_diag
.attr
,
725 static struct attribute_group dasd_attr_group
= {
730 * Return value of the specified feature.
733 dasd_get_feature(struct ccw_device
*cdev
, int feature
)
735 struct dasd_devmap
*devmap
;
737 devmap
= dasd_find_busid(cdev
->dev
.bus_id
);
739 return (int) PTR_ERR(devmap
);
741 return ((devmap
->features
& feature
) != 0);
745 * Set / reset given feature.
746 * Flag indicates wether to set (!=0) or the reset (=0) the feature.
749 dasd_set_feature(struct ccw_device
*cdev
, int feature
, int flag
)
751 struct dasd_devmap
*devmap
;
753 devmap
= dasd_find_busid(cdev
->dev
.bus_id
);
755 return (int) PTR_ERR(devmap
);
757 spin_lock(&dasd_devmap_lock
);
759 devmap
->features
|= feature
;
761 devmap
->features
&= ~feature
;
763 devmap
->device
->features
= devmap
->features
;
764 spin_unlock(&dasd_devmap_lock
);
770 dasd_add_sysfs_files(struct ccw_device
*cdev
)
772 return sysfs_create_group(&cdev
->dev
.kobj
, &dasd_attr_group
);
776 dasd_remove_sysfs_files(struct ccw_device
*cdev
)
778 sysfs_remove_group(&cdev
->dev
.kobj
, &dasd_attr_group
);
783 dasd_devmap_init(void)
787 /* Initialize devmap structures. */
788 dasd_max_devindex
= 0;
789 for (i
= 0; i
< 256; i
++)
790 INIT_LIST_HEAD(&dasd_hashlists
[i
]);
796 dasd_devmap_exit(void)
798 dasd_forget_ranges();