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;
51 * Parameter parsing functions for dasd= parameter. The syntax is:
52 * <devno> : (0x)?[0-9a-fA-F]+
53 * <busid> : [0-0a-f]\.[0-9a-f]\.(0x)?[0-9a-fA-F]+
55 * <feature_list> : \(<feature>(:<feature>)*\)
56 * <devno-range> : <devno>(-<devno>)?<feature_list>?
57 * <busid-range> : <busid>(-<busid>)?<feature_list>?
58 * <devices> : <devno-range>|<busid-range>
59 * <dasd_module> : dasd_diag_mod|dasd_eckd_mod|dasd_fba_mod
61 * <dasd> : autodetect|probeonly|<devices>(,<devices>)*
64 int dasd_probeonly = 0; /* is true, when probeonly mode is active */
65 int dasd_autodetect = 0; /* is true, when autodetection is active */
68 * char *dasd[] is intended to hold the ranges supplied by the dasd= statement
69 * it is named 'dasd' to directly be filled by insmod with the comma separated
70 * strings when running as a module.
72 static char *dasd[256];
73 module_param_array(dasd, charp, NULL, 0);
76 * Single spinlock to protect devmap structures and lists.
78 static DEFINE_SPINLOCK(dasd_devmap_lock);
81 * Hash lists for devmap structures.
83 static struct list_head dasd_hashlists[256];
84 int dasd_max_devindex;
86 static struct dasd_devmap *dasd_add_busid(char *, int);
89 dasd_hash_busid(char *bus_id)
94 for (i = 0; (i < BUS_ID_SIZE) && *bus_id; i++, bus_id++)
101 * The parameter parsing functions for builtin-drivers are called
102 * before kmalloc works. Store the pointers to the parameters strings
103 * into dasd[] for later processing.
106 dasd_call_setup(char *str)
108 static int count = 0;
115 __setup ("dasd=", dasd_call_setup);
116 #endif /* #ifndef MODULE */
119 * Read a device busid/devno from a string.
122 dasd_busid(char **str, int *id0, int *id1, int *devno)
126 /* check for leading '0x' */
128 if ((*str)[0] == '0' && (*str)[1] == 'x') {
132 if (!isxdigit((*str)[0])) /* We require at least one hex digit */
134 val = simple_strtoul(*str, str, 16);
135 if (old_style || (*str)[0] != '.') {
137 if (val < 0 || val > 0xffff)
142 /* New style x.y.z busid */
143 if (val < 0 || val > 0xff)
147 if (!isxdigit((*str)[0])) /* We require at least one hex digit */
149 val = simple_strtoul(*str, str, 16);
150 if (val < 0 || val > 0xff || (*str)++[0] != '.')
153 if (!isxdigit((*str)[0])) /* We require at least one hex digit */
155 val = simple_strtoul(*str, str, 16);
156 if (val < 0 || val > 0xffff)
163 * Read colon separated list of dasd features. Currently there is
164 * only one: "ro" for read-only devices. The default feature set
165 * is empty (value 0).
168 dasd_feature_list(char *str, char **endp)
170 int features, len, rc;
175 return DASD_FEATURE_DEFAULT;
182 str[len] && str[len] != ':' && str[len] != ')'; len++);
183 if (len == 2 && !strncmp(str, "ro", 2))
184 features |= DASD_FEATURE_READONLY;
185 else if (len == 4 && !strncmp(str, "diag", 4))
186 features |= DASD_FEATURE_USEDIAG;
188 MESSAGE(KERN_WARNING,
189 "unsupported feature: %*s, "
190 "ignoring setting", len, str);
199 MESSAGE(KERN_WARNING, "%s",
200 "missing ')' in dasd parameter string\n");
211 * Try to match the first element on the comma separated parse string
212 * with one of the known keywords. If a keyword is found, take the approprate
213 * action and return a pointer to the residual string. If the first element
214 * could not be matched to any keyword then return an error code.
217 dasd_parse_keyword( char *parsestring ) {
219 char *nextcomma, *residual_str;
222 nextcomma = strchr(parsestring,',');
224 length = nextcomma - parsestring;
225 residual_str = nextcomma + 1;
227 length = strlen(parsestring);
228 residual_str = parsestring + length;
230 if (strncmp ("autodetect", parsestring, length) == 0) {
232 MESSAGE (KERN_INFO, "%s",
233 "turning to autodetection mode");
236 if (strncmp ("probeonly", parsestring, length) == 0) {
238 MESSAGE(KERN_INFO, "%s",
239 "turning to probeonly mode");
242 if (strncmp ("fixedbuffers", parsestring, length) == 0) {
246 kmem_cache_create("dasd_page_cache", PAGE_SIZE, 0,
247 SLAB_CACHE_DMA, NULL, NULL );
248 if (!dasd_page_cache)
249 MESSAGE(KERN_WARNING, "%s", "Failed to create slab, "
250 "fixed buffer mode disabled.");
252 MESSAGE (KERN_INFO, "%s",
253 "turning on fixed buffer mode");
256 return ERR_PTR(-EINVAL);
260 * Try to interprete the first element on the comma separated parse string
261 * as a device number or a range of devices. If the interpretation is
262 * successfull, create the matching dasd_devmap entries and return a pointer
263 * to the residual string.
264 * If interpretation fails or in case of an error, return an error code.
267 dasd_parse_range( char *parsestring ) {
269 struct dasd_devmap *devmap;
270 int from, from_id0, from_id1;
271 int to, to_id0, to_id1;
273 char bus_id[BUS_ID_SIZE+1], *str;
276 rc = dasd_busid(&str, &from_id0, &from_id1, &from);
283 rc = dasd_busid(&str, &to_id0, &to_id1, &to);
287 (from_id0 != to_id0 || from_id1 != to_id1 || from > to))
290 MESSAGE(KERN_ERR, "Invalid device range %s", parsestring);
293 features = dasd_feature_list(str, &str);
295 return ERR_PTR(-EINVAL);
297 sprintf(bus_id, "%01x.%01x.%04x",
298 from_id0, from_id1, from++);
299 devmap = dasd_add_busid(bus_id, features);
301 return (char *)devmap;
307 MESSAGE(KERN_WARNING,
308 "junk at end of dasd parameter string: %s\n", str);
309 return ERR_PTR(-EINVAL);
313 dasd_parse_next_element( char *parsestring ) {
315 residual_str = dasd_parse_keyword(parsestring);
316 if (!IS_ERR(residual_str))
318 residual_str = dasd_parse_range(parsestring);
323 * Parse parameters stored in dasd[]
324 * The 'dasd=...' parameter allows to specify a comma separated list of
325 * keywords and device ranges. When the dasd driver is build into the kernel,
326 * the complete list will be stored as one element of the dasd[] array.
327 * When the dasd driver is build as a module, then the list is broken into
328 * it's elements and each dasd[] entry contains one element.
337 for (i = 0; i < 256; i++) {
340 parsestring = dasd[i];
341 /* loop over the comma separated list in the parsestring */
342 while (*parsestring) {
343 parsestring = dasd_parse_next_element(parsestring);
344 if(IS_ERR(parsestring)) {
345 rc = PTR_ERR(parsestring);
350 DBF_EVENT(DBF_ALERT, "%s", "invalid range found");
358 * Add a devmap for the device specified by busid. It is possible that
359 * the devmap already exists (dasd= parameter). The order of the devices
360 * added through this function will define the kdevs for the individual
363 static struct dasd_devmap *
364 dasd_add_busid(char *bus_id, int features)
366 struct dasd_devmap *devmap, *new, *tmp;
369 new = (struct dasd_devmap *)
370 kmalloc(sizeof(struct dasd_devmap), GFP_KERNEL);
372 return ERR_PTR(-ENOMEM);
373 spin_lock(&dasd_devmap_lock);
375 hash = dasd_hash_busid(bus_id);
376 list_for_each_entry(tmp, &dasd_hashlists[hash], list)
377 if (strncmp(tmp->bus_id, bus_id, BUS_ID_SIZE) == 0) {
382 /* This bus_id is new. */
383 new->devindex = dasd_max_devindex++;
384 strncpy(new->bus_id, bus_id, BUS_ID_SIZE);
385 new->features = features;
387 list_add(&new->list, &dasd_hashlists[hash]);
391 spin_unlock(&dasd_devmap_lock);
397 * Find devmap for device with given bus_id.
399 static struct dasd_devmap *
400 dasd_find_busid(char *bus_id)
402 struct dasd_devmap *devmap, *tmp;
405 spin_lock(&dasd_devmap_lock);
406 devmap = ERR_PTR(-ENODEV);
407 hash = dasd_hash_busid(bus_id);
408 list_for_each_entry(tmp, &dasd_hashlists[hash], list) {
409 if (strncmp(tmp->bus_id, bus_id, BUS_ID_SIZE) == 0) {
414 spin_unlock(&dasd_devmap_lock);
419 * Check if busid has been added to the list of dasd ranges.
422 dasd_busid_known(char *bus_id)
424 return IS_ERR(dasd_find_busid(bus_id)) ? -ENOENT : 0;
428 * Forget all about the device numbers added so far.
429 * This may only be called at module unload or system shutdown.
432 dasd_forget_ranges(void)
434 struct dasd_devmap *devmap, *n;
437 spin_lock(&dasd_devmap_lock);
438 for (i = 0; i < 256; i++) {
439 list_for_each_entry_safe(devmap, n, &dasd_hashlists[i], list) {
440 if (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);
552 spin_lock(&dasd_devmap_lock);
553 if (devmap->device != device) {
554 spin_unlock(&dasd_devmap_lock);
555 dasd_put_device(device);
558 devmap->device = NULL;
559 spin_unlock(&dasd_devmap_lock);
561 /* Drop ref_count by 2, one for the devmap reference and
562 * one for the passed reference. */
563 atomic_sub(2, &device->ref_count);
565 /* Wait for reference counter to drop to zero. */
566 wait_event(dasd_delete_wq, atomic_read(&device->ref_count) == 0);
568 /* Disconnect dasd_device structure from ccw_device structure. */
572 /* Disconnect dasd_devmap structure from ccw_device structure. */
573 cdev->dev.driver_data = NULL;
575 /* Put ccw_device structure. */
576 put_device(&cdev->dev);
578 /* Now the device structure can be freed. */
579 dasd_free_device(device);
583 * Reference counter dropped to zero. Wake up waiter
584 * in dasd_delete_device.
587 dasd_put_device_wake(struct dasd_device *device)
589 wake_up(&dasd_delete_wq);
593 * Return dasd_device structure associated with cdev.
596 dasd_device_from_cdev(struct ccw_device *cdev)
598 struct dasd_devmap *devmap;
599 struct dasd_device *device;
601 device = ERR_PTR(-ENODEV);
602 spin_lock(&dasd_devmap_lock);
603 devmap = cdev->dev.driver_data;
604 if (devmap && devmap->device) {
605 device = devmap->device;
606 dasd_get_device(device);
608 spin_unlock(&dasd_devmap_lock);
613 * SECTION: files in sysfs
617 * readonly controls the readonly status of a dasd
620 dasd_ro_show(struct device *dev, struct device_attribute *attr, char *buf)
622 struct dasd_devmap *devmap;
625 devmap = dasd_find_busid(dev->bus_id);
627 ro_flag = (devmap->features & DASD_FEATURE_READONLY) != 0;
629 ro_flag = (DASD_FEATURE_DEFAULT & DASD_FEATURE_READONLY) != 0;
630 return snprintf(buf, PAGE_SIZE, ro_flag ? "1\n" : "0\n");
634 dasd_ro_store(struct device *dev, struct device_attribute *attr, 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, const char *buf, size_t count)
679 struct dasd_devmap *devmap;
683 devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
685 return PTR_ERR(devmap);
686 use_diag = buf[0] == '1';
687 spin_lock(&dasd_devmap_lock);
688 /* Changing diag discipline flag is only allowed in offline state. */
690 if (!devmap->device) {
692 devmap->features |= DASD_FEATURE_USEDIAG;
694 devmap->features &= ~DASD_FEATURE_USEDIAG;
697 spin_unlock(&dasd_devmap_lock);
702 DEVICE_ATTR(use_diag, 0644, dasd_use_diag_show, dasd_use_diag_store);
705 dasd_discipline_show(struct device *dev, struct device_attribute *attr, char *buf)
707 struct dasd_devmap *devmap;
710 spin_lock(&dasd_devmap_lock);
712 devmap = dev->driver_data;
713 if (devmap && devmap->device && devmap->device->discipline)
714 dname = devmap->device->discipline->name;
715 spin_unlock(&dasd_devmap_lock);
716 return snprintf(buf, PAGE_SIZE, "%s\n", dname);
719 static DEVICE_ATTR(discipline, 0444, dasd_discipline_show, NULL);
722 * extended error-reporting
725 dasd_eer_show(struct device *dev, struct device_attribute *attr, char *buf)
727 struct dasd_devmap *devmap;
730 devmap = dasd_find_busid(dev->bus_id);
731 if (!IS_ERR(devmap) && devmap->device)
732 eer_flag = dasd_eer_enabled(devmap->device);
735 return snprintf(buf, PAGE_SIZE, eer_flag ? "1\n" : "0\n");
739 dasd_eer_store(struct device *dev, struct device_attribute *attr,
740 const char *buf, size_t count)
742 struct dasd_devmap *devmap;
745 devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
747 return PTR_ERR(devmap);
751 rc = dasd_eer_enable(devmap->device);
755 dasd_eer_disable(devmap->device);
759 static DEVICE_ATTR(eer_enabled, 0644, dasd_eer_show, dasd_eer_store);
761 static struct attribute * dasd_attrs[] = {
762 &dev_attr_readonly.attr,
763 &dev_attr_discipline.attr,
764 &dev_attr_use_diag.attr,
765 &dev_attr_eer_enabled.attr,
769 static struct attribute_group dasd_attr_group = {
774 * Return value of the specified feature.
777 dasd_get_feature(struct ccw_device *cdev, int feature)
779 struct dasd_devmap *devmap;
781 devmap = dasd_find_busid(cdev->dev.bus_id);
783 return (int) PTR_ERR(devmap);
785 return ((devmap->features & feature) != 0);
789 * Set / reset given feature.
790 * Flag indicates wether to set (!=0) or the reset (=0) the feature.
793 dasd_set_feature(struct ccw_device *cdev, int feature, int flag)
795 struct dasd_devmap *devmap;
797 devmap = dasd_find_busid(cdev->dev.bus_id);
799 return (int) PTR_ERR(devmap);
801 spin_lock(&dasd_devmap_lock);
803 devmap->features |= feature;
805 devmap->features &= ~feature;
807 devmap->device->features = devmap->features;
808 spin_unlock(&dasd_devmap_lock);
814 dasd_add_sysfs_files(struct ccw_device *cdev)
816 return sysfs_create_group(&cdev->dev.kobj, &dasd_attr_group);
820 dasd_remove_sysfs_files(struct ccw_device *cdev)
822 sysfs_remove_group(&cdev->dev.kobj, &dasd_attr_group);
827 dasd_devmap_init(void)
831 /* Initialize devmap structures. */
832 dasd_max_devindex = 0;
833 for (i = 0; i < 256; i++)
834 INIT_LIST_HEAD(&dasd_hashlists[i]);
840 dasd_devmap_exit(void)
842 dasd_forget_ranges();