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>
19 #include <linux/module.h>
21 #include <asm/debug.h>
22 #include <asm/uaccess.h>
25 #define PRINTK_HEADER "dasd_devmap:"
29 kmem_cache_t *dasd_page_cache;
30 EXPORT_SYMBOL(dasd_page_cache);
33 * dasd_devmap_t is used to store the features and the relation
34 * between device number and device index. To find a dasd_devmap_t
35 * that corresponds to a device number of a device index each
36 * dasd_devmap_t is added to two linked lists, one to search by
37 * the device number and one to search by the device index. As
38 * soon as big minor numbers are available the device index list
39 * can be removed since the device number will then be identical
40 * to the device index.
43 struct list_head list;
44 char bus_id[BUS_ID_SIZE];
45 unsigned int devindex;
46 unsigned short features;
47 struct dasd_device *device;
52 * Parameter parsing functions for dasd= parameter. The syntax is:
53 * <devno> : (0x)?[0-9a-fA-F]+
54 * <busid> : [0-0a-f]\.[0-9a-f]\.(0x)?[0-9a-fA-F]+
56 * <feature_list> : \(<feature>(:<feature>)*\)
57 * <devno-range> : <devno>(-<devno>)?<feature_list>?
58 * <busid-range> : <busid>(-<busid>)?<feature_list>?
59 * <devices> : <devno-range>|<busid-range>
60 * <dasd_module> : dasd_diag_mod|dasd_eckd_mod|dasd_fba_mod
62 * <dasd> : autodetect|probeonly|<devices>(,<devices>)*
65 int dasd_probeonly = 0; /* is true, when probeonly mode is active */
66 int dasd_autodetect = 0; /* is true, when autodetection is active */
69 * char *dasd[] is intended to hold the ranges supplied by the dasd= statement
70 * it is named 'dasd' to directly be filled by insmod with the comma separated
71 * strings when running as a module.
73 static char *dasd[256];
74 module_param_array(dasd, charp, NULL, 0);
77 * Single spinlock to protect devmap structures and lists.
79 static DEFINE_SPINLOCK(dasd_devmap_lock);
82 * Hash lists for devmap structures.
84 static struct list_head dasd_hashlists[256];
85 int dasd_max_devindex;
87 static struct dasd_devmap *dasd_add_busid(char *, int);
90 dasd_hash_busid(char *bus_id)
95 for (i = 0; (i < BUS_ID_SIZE) && *bus_id; i++, bus_id++)
102 * The parameter parsing functions for builtin-drivers are called
103 * before kmalloc works. Store the pointers to the parameters strings
104 * into dasd[] for later processing.
107 dasd_call_setup(char *str)
109 static int count = 0;
116 __setup ("dasd=", dasd_call_setup);
117 #endif /* #ifndef MODULE */
120 * Read a device busid/devno from a string.
123 dasd_busid(char **str, int *id0, int *id1, int *devno)
127 /* check for leading '0x' */
129 if ((*str)[0] == '0' && (*str)[1] == 'x') {
133 if (!isxdigit((*str)[0])) /* We require at least one hex digit */
135 val = simple_strtoul(*str, str, 16);
136 if (old_style || (*str)[0] != '.') {
138 if (val < 0 || val > 0xffff)
143 /* New style x.y.z busid */
144 if (val < 0 || val > 0xff)
148 if (!isxdigit((*str)[0])) /* We require at least one hex digit */
150 val = simple_strtoul(*str, str, 16);
151 if (val < 0 || val > 0xff || (*str)++[0] != '.')
154 if (!isxdigit((*str)[0])) /* We require at least one hex digit */
156 val = simple_strtoul(*str, str, 16);
157 if (val < 0 || val > 0xffff)
164 * Read colon separated list of dasd features. Currently there is
165 * only one: "ro" for read-only devices. The default feature set
166 * is empty (value 0).
169 dasd_feature_list(char *str, char **endp)
171 int features, len, rc;
176 return DASD_FEATURE_DEFAULT;
183 str[len] && str[len] != ':' && str[len] != ')'; len++);
184 if (len == 2 && !strncmp(str, "ro", 2))
185 features |= DASD_FEATURE_READONLY;
186 else if (len == 4 && !strncmp(str, "diag", 4))
187 features |= DASD_FEATURE_USEDIAG;
189 MESSAGE(KERN_WARNING,
190 "unsupported feature: %*s, "
191 "ignoring setting", len, str);
200 MESSAGE(KERN_WARNING, "%s",
201 "missing ')' in dasd parameter string\n");
212 * Try to match the first element on the comma separated parse string
213 * with one of the known keywords. If a keyword is found, take the approprate
214 * action and return a pointer to the residual string. If the first element
215 * could not be matched to any keyword then return an error code.
218 dasd_parse_keyword( char *parsestring ) {
220 char *nextcomma, *residual_str;
223 nextcomma = strchr(parsestring,',');
225 length = nextcomma - parsestring;
226 residual_str = nextcomma + 1;
228 length = strlen(parsestring);
229 residual_str = parsestring + length;
231 if (strncmp ("autodetect", parsestring, length) == 0) {
233 MESSAGE (KERN_INFO, "%s",
234 "turning to autodetection mode");
237 if (strncmp ("probeonly", parsestring, length) == 0) {
239 MESSAGE(KERN_INFO, "%s",
240 "turning to probeonly mode");
243 if (strncmp ("fixedbuffers", parsestring, length) == 0) {
247 kmem_cache_create("dasd_page_cache", PAGE_SIZE, 0,
248 SLAB_CACHE_DMA, NULL, NULL );
249 if (!dasd_page_cache)
250 MESSAGE(KERN_WARNING, "%s", "Failed to create slab, "
251 "fixed buffer mode disabled.");
253 MESSAGE (KERN_INFO, "%s",
254 "turning on fixed buffer mode");
257 return ERR_PTR(-EINVAL);
261 * Try to interprete the first element on the comma separated parse string
262 * as a device number or a range of devices. If the interpretation is
263 * successfull, create the matching dasd_devmap entries and return a pointer
264 * to the residual string.
265 * If interpretation fails or in case of an error, return an error code.
268 dasd_parse_range( char *parsestring ) {
270 struct dasd_devmap *devmap;
271 int from, from_id0, from_id1;
272 int to, to_id0, to_id1;
274 char bus_id[BUS_ID_SIZE+1], *str;
277 rc = dasd_busid(&str, &from_id0, &from_id1, &from);
284 rc = dasd_busid(&str, &to_id0, &to_id1, &to);
288 (from_id0 != to_id0 || from_id1 != to_id1 || from > to))
291 MESSAGE(KERN_ERR, "Invalid device range %s", parsestring);
294 features = dasd_feature_list(str, &str);
296 return ERR_PTR(-EINVAL);
298 sprintf(bus_id, "%01x.%01x.%04x",
299 from_id0, from_id1, from++);
300 devmap = dasd_add_busid(bus_id, features);
302 return (char *)devmap;
308 MESSAGE(KERN_WARNING,
309 "junk at end of dasd parameter string: %s\n", str);
310 return ERR_PTR(-EINVAL);
314 dasd_parse_next_element( char *parsestring ) {
316 residual_str = dasd_parse_keyword(parsestring);
317 if (!IS_ERR(residual_str))
319 residual_str = dasd_parse_range(parsestring);
324 * Parse parameters stored in dasd[]
325 * The 'dasd=...' parameter allows to specify a comma separated list of
326 * keywords and device ranges. When the dasd driver is build into the kernel,
327 * the complete list will be stored as one element of the dasd[] array.
328 * When the dasd driver is build as a module, then the list is broken into
329 * it's elements and each dasd[] entry contains one element.
338 for (i = 0; i < 256; i++) {
341 parsestring = dasd[i];
342 /* loop over the comma separated list in the parsestring */
343 while (*parsestring) {
344 parsestring = dasd_parse_next_element(parsestring);
345 if(IS_ERR(parsestring)) {
346 rc = PTR_ERR(parsestring);
351 DBF_EVENT(DBF_ALERT, "%s", "invalid range found");
359 * Add a devmap for the device specified by busid. It is possible that
360 * the devmap already exists (dasd= parameter). The order of the devices
361 * added through this function will define the kdevs for the individual
364 static struct dasd_devmap *
365 dasd_add_busid(char *bus_id, int features)
367 struct dasd_devmap *devmap, *new, *tmp;
370 new = (struct dasd_devmap *)
371 kzalloc(sizeof(struct dasd_devmap), GFP_KERNEL);
373 return ERR_PTR(-ENOMEM);
374 spin_lock(&dasd_devmap_lock);
376 hash = dasd_hash_busid(bus_id);
377 list_for_each_entry(tmp, &dasd_hashlists[hash], list)
378 if (strncmp(tmp->bus_id, bus_id, BUS_ID_SIZE) == 0) {
383 /* This bus_id is new. */
384 new->devindex = dasd_max_devindex++;
385 strncpy(new->bus_id, bus_id, BUS_ID_SIZE);
386 new->features = features;
388 list_add(&new->list, &dasd_hashlists[hash]);
392 spin_unlock(&dasd_devmap_lock);
398 * Find devmap for device with given bus_id.
400 static struct dasd_devmap *
401 dasd_find_busid(char *bus_id)
403 struct dasd_devmap *devmap, *tmp;
406 spin_lock(&dasd_devmap_lock);
407 devmap = ERR_PTR(-ENODEV);
408 hash = dasd_hash_busid(bus_id);
409 list_for_each_entry(tmp, &dasd_hashlists[hash], list) {
410 if (strncmp(tmp->bus_id, bus_id, BUS_ID_SIZE) == 0) {
415 spin_unlock(&dasd_devmap_lock);
420 * Check if busid has been added to the list of dasd ranges.
423 dasd_busid_known(char *bus_id)
425 return IS_ERR(dasd_find_busid(bus_id)) ? -ENOENT : 0;
429 * Forget all about the device numbers added so far.
430 * This may only be called at module unload or system shutdown.
433 dasd_forget_ranges(void)
435 struct dasd_devmap *devmap, *n;
438 spin_lock(&dasd_devmap_lock);
439 for (i = 0; i < 256; i++) {
440 list_for_each_entry_safe(devmap, n, &dasd_hashlists[i], list) {
441 BUG_ON(devmap->device != NULL);
442 list_del(&devmap->list);
446 spin_unlock(&dasd_devmap_lock);
450 * Find the device struct by its device index.
453 dasd_device_from_devindex(int devindex)
455 struct dasd_devmap *devmap, *tmp;
456 struct dasd_device *device;
459 spin_lock(&dasd_devmap_lock);
461 for (i = 0; (i < 256) && !devmap; i++)
462 list_for_each_entry(tmp, &dasd_hashlists[i], list)
463 if (tmp->devindex == devindex) {
464 /* Found the devmap for the device. */
468 if (devmap && devmap->device) {
469 device = devmap->device;
470 dasd_get_device(device);
472 device = ERR_PTR(-ENODEV);
473 spin_unlock(&dasd_devmap_lock);
478 * Return devmap for cdev. If no devmap exists yet, create one and
479 * connect it to the cdev.
481 static struct dasd_devmap *
482 dasd_devmap_from_cdev(struct ccw_device *cdev)
484 struct dasd_devmap *devmap;
486 devmap = dasd_find_busid(cdev->dev.bus_id);
488 devmap = dasd_add_busid(cdev->dev.bus_id,
489 DASD_FEATURE_DEFAULT);
494 * Create a dasd device structure for cdev.
497 dasd_create_device(struct ccw_device *cdev)
499 struct dasd_devmap *devmap;
500 struct dasd_device *device;
503 devmap = dasd_devmap_from_cdev(cdev);
505 return (void *) devmap;
506 cdev->dev.driver_data = devmap;
508 device = dasd_alloc_device();
511 atomic_set(&device->ref_count, 2);
513 spin_lock(&dasd_devmap_lock);
514 if (!devmap->device) {
515 devmap->device = device;
516 device->devindex = devmap->devindex;
517 device->features = devmap->features;
518 get_device(&cdev->dev);
522 /* Someone else was faster. */
524 spin_unlock(&dasd_devmap_lock);
527 dasd_free_device(device);
534 * Wait queue for dasd_delete_device waits.
536 static DECLARE_WAIT_QUEUE_HEAD(dasd_delete_wq);
539 * Remove a dasd device structure. The passed referenced
543 dasd_delete_device(struct dasd_device *device)
545 struct ccw_device *cdev;
546 struct dasd_devmap *devmap;
548 /* First remove device pointer from devmap. */
549 devmap = dasd_find_busid(device->cdev->dev.bus_id);
550 BUG_ON(IS_ERR(devmap));
551 spin_lock(&dasd_devmap_lock);
552 if (devmap->device != device) {
553 spin_unlock(&dasd_devmap_lock);
554 dasd_put_device(device);
557 devmap->device = NULL;
558 spin_unlock(&dasd_devmap_lock);
560 /* Drop ref_count by 2, one for the devmap reference and
561 * one for the passed reference. */
562 atomic_sub(2, &device->ref_count);
564 /* Wait for reference counter to drop to zero. */
565 wait_event(dasd_delete_wq, atomic_read(&device->ref_count) == 0);
567 /* Disconnect dasd_device structure from ccw_device structure. */
571 /* Disconnect dasd_devmap structure from ccw_device structure. */
572 cdev->dev.driver_data = NULL;
574 /* Put ccw_device structure. */
575 put_device(&cdev->dev);
577 /* Now the device structure can be freed. */
578 dasd_free_device(device);
582 * Reference counter dropped to zero. Wake up waiter
583 * in dasd_delete_device.
586 dasd_put_device_wake(struct dasd_device *device)
588 wake_up(&dasd_delete_wq);
592 * Return dasd_device structure associated with cdev.
595 dasd_device_from_cdev(struct ccw_device *cdev)
597 struct dasd_devmap *devmap;
598 struct dasd_device *device;
600 device = ERR_PTR(-ENODEV);
601 spin_lock(&dasd_devmap_lock);
602 devmap = cdev->dev.driver_data;
603 if (devmap && devmap->device) {
604 device = devmap->device;
605 dasd_get_device(device);
607 spin_unlock(&dasd_devmap_lock);
612 * SECTION: files in sysfs
616 * readonly controls the readonly status of a dasd
619 dasd_ro_show(struct device *dev, struct device_attribute *attr, char *buf)
621 struct dasd_devmap *devmap;
624 devmap = dasd_find_busid(dev->bus_id);
626 ro_flag = (devmap->features & DASD_FEATURE_READONLY) != 0;
628 ro_flag = (DASD_FEATURE_DEFAULT & DASD_FEATURE_READONLY) != 0;
629 return snprintf(buf, PAGE_SIZE, ro_flag ? "1\n" : "0\n");
633 dasd_ro_store(struct device *dev, struct device_attribute *attr,
634 const char *buf, size_t count)
636 struct dasd_devmap *devmap;
639 devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
641 return PTR_ERR(devmap);
642 ro_flag = buf[0] == '1';
643 spin_lock(&dasd_devmap_lock);
645 devmap->features |= DASD_FEATURE_READONLY;
647 devmap->features &= ~DASD_FEATURE_READONLY;
649 devmap->device->features = devmap->features;
650 if (devmap->device && devmap->device->gdp)
651 set_disk_ro(devmap->device->gdp, ro_flag);
652 spin_unlock(&dasd_devmap_lock);
656 static DEVICE_ATTR(readonly, 0644, dasd_ro_show, dasd_ro_store);
659 * use_diag controls whether the driver should use diag rather than ssch
660 * to talk to the device
663 dasd_use_diag_show(struct device *dev, struct device_attribute *attr, char *buf)
665 struct dasd_devmap *devmap;
668 devmap = dasd_find_busid(dev->bus_id);
670 use_diag = (devmap->features & DASD_FEATURE_USEDIAG) != 0;
672 use_diag = (DASD_FEATURE_DEFAULT & DASD_FEATURE_USEDIAG) != 0;
673 return sprintf(buf, use_diag ? "1\n" : "0\n");
677 dasd_use_diag_store(struct device *dev, struct device_attribute *attr,
678 const char *buf, size_t count)
680 struct dasd_devmap *devmap;
684 devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
686 return PTR_ERR(devmap);
687 use_diag = buf[0] == '1';
688 spin_lock(&dasd_devmap_lock);
689 /* Changing diag discipline flag is only allowed in offline state. */
691 if (!devmap->device) {
693 devmap->features |= DASD_FEATURE_USEDIAG;
695 devmap->features &= ~DASD_FEATURE_USEDIAG;
698 spin_unlock(&dasd_devmap_lock);
702 static DEVICE_ATTR(use_diag, 0644, dasd_use_diag_show, dasd_use_diag_store);
705 dasd_discipline_show(struct device *dev, struct device_attribute *attr,
708 struct dasd_devmap *devmap;
711 spin_lock(&dasd_devmap_lock);
713 devmap = dev->driver_data;
714 if (devmap && devmap->device && devmap->device->discipline)
715 dname = devmap->device->discipline->name;
716 spin_unlock(&dasd_devmap_lock);
717 return snprintf(buf, PAGE_SIZE, "%s\n", dname);
720 static DEVICE_ATTR(discipline, 0444, dasd_discipline_show, NULL);
723 dasd_alias_show(struct device *dev, struct device_attribute *attr, char *buf)
725 struct dasd_devmap *devmap;
728 devmap = dasd_find_busid(dev->bus_id);
729 spin_lock(&dasd_devmap_lock);
731 alias = devmap->uid.alias;
734 spin_unlock(&dasd_devmap_lock);
736 return sprintf(buf, alias ? "1\n" : "0\n");
739 static DEVICE_ATTR(alias, 0444, dasd_alias_show, NULL);
742 dasd_vendor_show(struct device *dev, struct device_attribute *attr, char *buf)
744 struct dasd_devmap *devmap;
747 devmap = dasd_find_busid(dev->bus_id);
748 spin_lock(&dasd_devmap_lock);
749 if (!IS_ERR(devmap) && strlen(devmap->uid.vendor) > 0)
750 vendor = devmap->uid.vendor;
753 spin_unlock(&dasd_devmap_lock);
755 return snprintf(buf, PAGE_SIZE, "%s\n", vendor);
758 static DEVICE_ATTR(vendor, 0444, dasd_vendor_show, NULL);
760 #define UID_STRLEN ( /* vendor */ 3 + 1 + /* serial */ 14 + 1 +\
761 /* SSID */ 4 + 1 + /* unit addr */ 2 + 1)
764 dasd_uid_show(struct device *dev, struct device_attribute *attr, char *buf)
766 struct dasd_devmap *devmap;
767 char uid[UID_STRLEN];
769 devmap = dasd_find_busid(dev->bus_id);
770 spin_lock(&dasd_devmap_lock);
771 if (!IS_ERR(devmap) && strlen(devmap->uid.vendor) > 0)
772 snprintf(uid, sizeof(uid), "%s.%s.%04x.%02x",
773 devmap->uid.vendor, devmap->uid.serial,
774 devmap->uid.ssid, devmap->uid.unit_addr);
777 spin_unlock(&dasd_devmap_lock);
779 return snprintf(buf, PAGE_SIZE, "%s\n", uid);
782 static DEVICE_ATTR(uid, 0444, dasd_uid_show, NULL);
785 * extended error-reporting
788 dasd_eer_show(struct device *dev, struct device_attribute *attr, char *buf)
790 struct dasd_devmap *devmap;
793 devmap = dasd_find_busid(dev->bus_id);
794 if (!IS_ERR(devmap) && devmap->device)
795 eer_flag = dasd_eer_enabled(devmap->device);
798 return snprintf(buf, PAGE_SIZE, eer_flag ? "1\n" : "0\n");
802 dasd_eer_store(struct device *dev, struct device_attribute *attr,
803 const char *buf, size_t count)
805 struct dasd_devmap *devmap;
808 devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
810 return PTR_ERR(devmap);
814 rc = dasd_eer_enable(devmap->device);
818 dasd_eer_disable(devmap->device);
822 static DEVICE_ATTR(eer_enabled, 0644, dasd_eer_show, dasd_eer_store);
824 static struct attribute * dasd_attrs[] = {
825 &dev_attr_readonly.attr,
826 &dev_attr_discipline.attr,
827 &dev_attr_alias.attr,
828 &dev_attr_vendor.attr,
830 &dev_attr_use_diag.attr,
831 &dev_attr_eer_enabled.attr,
835 static struct attribute_group dasd_attr_group = {
841 * Return copy of the device unique identifier.
844 dasd_get_uid(struct ccw_device *cdev, struct dasd_uid *uid)
846 struct dasd_devmap *devmap;
848 devmap = dasd_find_busid(cdev->dev.bus_id);
850 return PTR_ERR(devmap);
851 spin_lock(&dasd_devmap_lock);
853 spin_unlock(&dasd_devmap_lock);
858 * Register the given device unique identifier into devmap struct.
861 dasd_set_uid(struct ccw_device *cdev, struct dasd_uid *uid)
863 struct dasd_devmap *devmap;
865 devmap = dasd_find_busid(cdev->dev.bus_id);
867 return PTR_ERR(devmap);
868 spin_lock(&dasd_devmap_lock);
870 spin_unlock(&dasd_devmap_lock);
873 EXPORT_SYMBOL(dasd_set_uid);
876 * Return value of the specified feature.
879 dasd_get_feature(struct ccw_device *cdev, int feature)
881 struct dasd_devmap *devmap;
883 devmap = dasd_find_busid(cdev->dev.bus_id);
885 return (int) PTR_ERR(devmap);
887 return ((devmap->features & feature) != 0);
891 * Set / reset given feature.
892 * Flag indicates wether to set (!=0) or the reset (=0) the feature.
895 dasd_set_feature(struct ccw_device *cdev, int feature, int flag)
897 struct dasd_devmap *devmap;
899 devmap = dasd_find_busid(cdev->dev.bus_id);
901 return (int) PTR_ERR(devmap);
903 spin_lock(&dasd_devmap_lock);
905 devmap->features |= feature;
907 devmap->features &= ~feature;
909 devmap->device->features = devmap->features;
910 spin_unlock(&dasd_devmap_lock);
916 dasd_add_sysfs_files(struct ccw_device *cdev)
918 return sysfs_create_group(&cdev->dev.kobj, &dasd_attr_group);
922 dasd_remove_sysfs_files(struct ccw_device *cdev)
924 sysfs_remove_group(&cdev->dev.kobj, &dasd_attr_group);
929 dasd_devmap_init(void)
933 /* Initialize devmap structures. */
934 dasd_max_devindex = 0;
935 for (i = 0; i < 256; i++)
936 INIT_LIST_HEAD(&dasd_hashlists[i]);
942 dasd_devmap_exit(void)
944 dasd_forget_ranges();