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.
17 #include <linux/config.h>
18 #include <linux/ctype.h>
19 #include <linux/init.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];
74 * Single spinlock to protect devmap structures and lists.
76 static DEFINE_SPINLOCK(dasd_devmap_lock);
79 * Hash lists for devmap structures.
81 static struct list_head dasd_hashlists[256];
82 int dasd_max_devindex;
84 static struct dasd_devmap *dasd_add_busid(char *, int);
87 dasd_hash_busid(char *bus_id)
92 for (i = 0; (i < BUS_ID_SIZE) && *bus_id; i++, bus_id++)
99 * The parameter parsing functions for builtin-drivers are called
100 * before kmalloc works. Store the pointers to the parameters strings
101 * into dasd[] for later processing.
104 dasd_call_setup(char *str)
106 static int count = 0;
113 __setup ("dasd=", dasd_call_setup);
114 #endif /* #ifndef MODULE */
117 * Read a device busid/devno from a string.
120 dasd_busid(char **str, int *id0, int *id1, int *devno)
124 /* check for leading '0x' */
126 if ((*str)[0] == '0' && (*str)[1] == 'x') {
130 if (!isxdigit((*str)[0])) /* We require at least one hex digit */
132 val = simple_strtoul(*str, str, 16);
133 if (old_style || (*str)[0] != '.') {
135 if (val < 0 || val > 0xffff)
140 /* New style x.y.z busid */
141 if (val < 0 || val > 0xff)
145 if (!isxdigit((*str)[0])) /* We require at least one hex digit */
147 val = simple_strtoul(*str, str, 16);
148 if (val < 0 || val > 0xff || (*str)++[0] != '.')
151 if (!isxdigit((*str)[0])) /* We require at least one hex digit */
153 val = simple_strtoul(*str, str, 16);
154 if (val < 0 || val > 0xffff)
161 * Read colon separated list of dasd features. Currently there is
162 * only one: "ro" for read-only devices. The default feature set
163 * is empty (value 0).
166 dasd_feature_list(char *str, char **endp)
168 int features, len, rc;
173 return DASD_FEATURE_DEFAULT;
180 str[len] && str[len] != ':' && str[len] != ')'; len++);
181 if (len == 2 && !strncmp(str, "ro", 2))
182 features |= DASD_FEATURE_READONLY;
183 else if (len == 4 && !strncmp(str, "diag", 4))
184 features |= DASD_FEATURE_USEDIAG;
186 MESSAGE(KERN_WARNING,
187 "unsupported feature: %*s, "
188 "ignoring setting", len, str);
197 MESSAGE(KERN_WARNING, "%s",
198 "missing ')' in dasd parameter string\n");
209 * Try to match the first element on the comma separated parse string
210 * with one of the known keywords. If a keyword is found, take the approprate
211 * action and return a pointer to the residual string. If the first element
212 * could not be matched to any keyword then return an error code.
215 dasd_parse_keyword( char *parsestring ) {
217 char *nextcomma, *residual_str;
220 nextcomma = strchr(parsestring,',');
222 length = nextcomma - parsestring;
223 residual_str = nextcomma + 1;
225 length = strlen(parsestring);
226 residual_str = parsestring + length;
228 if (strncmp ("autodetect", parsestring, length) == 0) {
230 MESSAGE (KERN_INFO, "%s",
231 "turning to autodetection mode");
234 if (strncmp ("probeonly", parsestring, length) == 0) {
236 MESSAGE(KERN_INFO, "%s",
237 "turning to probeonly mode");
240 if (strncmp ("fixedbuffers", parsestring, length) == 0) {
244 kmem_cache_create("dasd_page_cache", PAGE_SIZE, 0,
245 SLAB_CACHE_DMA, NULL, NULL );
246 if (!dasd_page_cache)
247 MESSAGE(KERN_WARNING, "%s", "Failed to create slab, "
248 "fixed buffer mode disabled.");
250 MESSAGE (KERN_INFO, "%s",
251 "turning on fixed buffer mode");
254 return ERR_PTR(-EINVAL);
258 * Try to interprete the first element on the comma separated parse string
259 * as a device number or a range of devices. If the interpretation is
260 * successfull, create the matching dasd_devmap entries and return a pointer
261 * to the residual string.
262 * If interpretation fails or in case of an error, return an error code.
265 dasd_parse_range( char *parsestring ) {
267 struct dasd_devmap *devmap;
268 int from, from_id0, from_id1;
269 int to, to_id0, to_id1;
271 char bus_id[BUS_ID_SIZE+1], *str;
274 rc = dasd_busid(&str, &from_id0, &from_id1, &from);
281 rc = dasd_busid(&str, &to_id0, &to_id1, &to);
285 (from_id0 != to_id0 || from_id1 != to_id1 || from > to))
288 MESSAGE(KERN_ERR, "Invalid device range %s", parsestring);
291 features = dasd_feature_list(str, &str);
293 return ERR_PTR(-EINVAL);
295 sprintf(bus_id, "%01x.%01x.%04x",
296 from_id0, from_id1, from++);
297 devmap = dasd_add_busid(bus_id, features);
299 return (char *)devmap;
305 MESSAGE(KERN_WARNING,
306 "junk at end of dasd parameter string: %s\n", str);
307 return ERR_PTR(-EINVAL);
311 dasd_parse_next_element( char *parsestring ) {
313 residual_str = dasd_parse_keyword(parsestring);
314 if (!IS_ERR(residual_str))
316 residual_str = dasd_parse_range(parsestring);
321 * Parse parameters stored in dasd[]
322 * The 'dasd=...' parameter allows to specify a comma separated list of
323 * keywords and device ranges. When the dasd driver is build into the kernel,
324 * the complete list will be stored as one element of the dasd[] array.
325 * When the dasd driver is build as a module, then the list is broken into
326 * it's elements and each dasd[] entry contains one element.
335 for (i = 0; i < 256; i++) {
338 parsestring = dasd[i];
339 /* loop over the comma separated list in the parsestring */
340 while (*parsestring) {
341 parsestring = dasd_parse_next_element(parsestring);
342 if(IS_ERR(parsestring)) {
343 rc = PTR_ERR(parsestring);
348 DBF_EVENT(DBF_ALERT, "%s", "invalid range found");
356 * Add a devmap for the device specified by busid. It is possible that
357 * the devmap already exists (dasd= parameter). The order of the devices
358 * added through this function will define the kdevs for the individual
361 static struct dasd_devmap *
362 dasd_add_busid(char *bus_id, int features)
364 struct dasd_devmap *devmap, *new, *tmp;
367 new = (struct dasd_devmap *)
368 kmalloc(sizeof(struct dasd_devmap), GFP_KERNEL);
370 return ERR_PTR(-ENOMEM);
371 spin_lock(&dasd_devmap_lock);
373 hash = dasd_hash_busid(bus_id);
374 list_for_each_entry(tmp, &dasd_hashlists[hash], list)
375 if (strncmp(tmp->bus_id, bus_id, BUS_ID_SIZE) == 0) {
380 /* This bus_id is new. */
381 new->devindex = dasd_max_devindex++;
382 strncpy(new->bus_id, bus_id, BUS_ID_SIZE);
383 new->features = features;
385 list_add(&new->list, &dasd_hashlists[hash]);
389 spin_unlock(&dasd_devmap_lock);
396 * Find devmap for device with given bus_id.
398 static struct dasd_devmap *
399 dasd_find_busid(char *bus_id)
401 struct dasd_devmap *devmap, *tmp;
404 spin_lock(&dasd_devmap_lock);
405 devmap = ERR_PTR(-ENODEV);
406 hash = dasd_hash_busid(bus_id);
407 list_for_each_entry(tmp, &dasd_hashlists[hash], list) {
408 if (strncmp(tmp->bus_id, bus_id, BUS_ID_SIZE) == 0) {
413 spin_unlock(&dasd_devmap_lock);
418 * Check if busid has been added to the list of dasd ranges.
421 dasd_busid_known(char *bus_id)
423 return IS_ERR(dasd_find_busid(bus_id)) ? -ENOENT : 0;
427 * Forget all about the device numbers added so far.
428 * This may only be called at module unload or system shutdown.
431 dasd_forget_ranges(void)
433 struct dasd_devmap *devmap, *n;
436 spin_lock(&dasd_devmap_lock);
437 for (i = 0; i < 256; i++) {
438 list_for_each_entry_safe(devmap, n, &dasd_hashlists[i], list) {
439 if (devmap->device != NULL)
441 list_del(&devmap->list);
445 spin_unlock(&dasd_devmap_lock);
449 * Find the device struct by its device index.
452 dasd_device_from_devindex(int devindex)
454 struct dasd_devmap *devmap, *tmp;
455 struct dasd_device *device;
458 spin_lock(&dasd_devmap_lock);
460 for (i = 0; (i < 256) && !devmap; i++)
461 list_for_each_entry(tmp, &dasd_hashlists[i], list)
462 if (tmp->devindex == devindex) {
463 /* Found the devmap for the device. */
467 if (devmap && devmap->device) {
468 device = devmap->device;
469 dasd_get_device(device);
471 device = ERR_PTR(-ENODEV);
472 spin_unlock(&dasd_devmap_lock);
477 * Return devmap for cdev. If no devmap exists yet, create one and
478 * connect it to the cdev.
480 static struct dasd_devmap *
481 dasd_devmap_from_cdev(struct ccw_device *cdev)
483 struct dasd_devmap *devmap;
485 devmap = dasd_find_busid(cdev->dev.bus_id);
487 devmap = dasd_add_busid(cdev->dev.bus_id,
488 DASD_FEATURE_DEFAULT);
493 * Create a dasd device structure for cdev.
496 dasd_create_device(struct ccw_device *cdev)
498 struct dasd_devmap *devmap;
499 struct dasd_device *device;
502 devmap = dasd_devmap_from_cdev(cdev);
504 return (void *) devmap;
505 cdev->dev.driver_data = devmap;
507 device = dasd_alloc_device();
510 atomic_set(&device->ref_count, 2);
512 spin_lock(&dasd_devmap_lock);
513 if (!devmap->device) {
514 devmap->device = device;
515 device->devindex = devmap->devindex;
516 device->features = devmap->features;
517 get_device(&cdev->dev);
521 /* Someone else was faster. */
523 spin_unlock(&dasd_devmap_lock);
526 dasd_free_device(device);
533 * Wait queue for dasd_delete_device waits.
535 static DECLARE_WAIT_QUEUE_HEAD(dasd_delete_wq);
538 * Remove a dasd device structure. The passed referenced
542 dasd_delete_device(struct dasd_device *device)
544 struct ccw_device *cdev;
545 struct dasd_devmap *devmap;
547 /* First remove device pointer from devmap. */
548 devmap = dasd_find_busid(device->cdev->dev.bus_id);
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, const char *buf, size_t count)
635 struct dasd_devmap *devmap;
638 devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
640 return PTR_ERR(devmap);
641 ro_flag = buf[0] == '1';
642 spin_lock(&dasd_devmap_lock);
644 devmap->features |= DASD_FEATURE_READONLY;
646 devmap->features &= ~DASD_FEATURE_READONLY;
648 devmap->device->features = devmap->features;
649 if (devmap->device && devmap->device->gdp)
650 set_disk_ro(devmap->device->gdp, ro_flag);
651 spin_unlock(&dasd_devmap_lock);
655 static DEVICE_ATTR(readonly, 0644, dasd_ro_show, dasd_ro_store);
658 * use_diag controls whether the driver should use diag rather than ssch
659 * to talk to the device
662 dasd_use_diag_show(struct device *dev, struct device_attribute *attr, char *buf)
664 struct dasd_devmap *devmap;
667 devmap = dasd_find_busid(dev->bus_id);
669 use_diag = (devmap->features & DASD_FEATURE_USEDIAG) != 0;
671 use_diag = (DASD_FEATURE_DEFAULT & DASD_FEATURE_USEDIAG) != 0;
672 return sprintf(buf, use_diag ? "1\n" : "0\n");
676 dasd_use_diag_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
678 struct dasd_devmap *devmap;
682 devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
684 return PTR_ERR(devmap);
685 use_diag = buf[0] == '1';
686 spin_lock(&dasd_devmap_lock);
687 /* Changing diag discipline flag is only allowed in offline state. */
689 if (!devmap->device) {
691 devmap->features |= DASD_FEATURE_USEDIAG;
693 devmap->features &= ~DASD_FEATURE_USEDIAG;
696 spin_unlock(&dasd_devmap_lock);
701 DEVICE_ATTR(use_diag, 0644, dasd_use_diag_show, dasd_use_diag_store);
704 dasd_discipline_show(struct device *dev, struct device_attribute *attr, char *buf)
706 struct dasd_devmap *devmap;
709 spin_lock(&dasd_devmap_lock);
711 devmap = dev->driver_data;
712 if (devmap && devmap->device && devmap->device->discipline)
713 dname = devmap->device->discipline->name;
714 spin_unlock(&dasd_devmap_lock);
715 return snprintf(buf, PAGE_SIZE, "%s\n", dname);
718 static DEVICE_ATTR(discipline, 0444, dasd_discipline_show, NULL);
720 static struct attribute * dasd_attrs[] = {
721 &dev_attr_readonly.attr,
722 &dev_attr_discipline.attr,
723 &dev_attr_use_diag.attr,
727 static struct attribute_group dasd_attr_group = {
732 * Return value of the specified feature.
735 dasd_get_feature(struct ccw_device *cdev, int feature)
737 struct dasd_devmap *devmap;
739 devmap = dasd_find_busid(cdev->dev.bus_id);
741 return (int) PTR_ERR(devmap);
743 return ((devmap->features & feature) != 0);
747 * Set / reset given feature.
748 * Flag indicates wether to set (!=0) or the reset (=0) the feature.
751 dasd_set_feature(struct ccw_device *cdev, int feature, int flag)
753 struct dasd_devmap *devmap;
755 devmap = dasd_find_busid(cdev->dev.bus_id);
757 return (int) PTR_ERR(devmap);
759 spin_lock(&dasd_devmap_lock);
761 devmap->features |= feature;
763 devmap->features &= ~feature;
765 devmap->device->features = devmap->features;
766 spin_unlock(&dasd_devmap_lock);
772 dasd_add_sysfs_files(struct ccw_device *cdev)
774 return sysfs_create_group(&cdev->dev.kobj, &dasd_attr_group);
778 dasd_remove_sysfs_files(struct ccw_device *cdev)
780 sysfs_remove_group(&cdev->dev.kobj, &dasd_attr_group);
785 dasd_devmap_init(void)
789 /* Initialize devmap structures. */
790 dasd_max_devindex = 0;
791 for (i = 0; i < 256; i++)
792 INIT_LIST_HEAD(&dasd_hashlists[i]);
798 dasd_devmap_exit(void)
800 dasd_forget_ranges();