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];
75 * Single spinlock to protect devmap structures and lists.
77 static spinlock_t dasd_devmap_lock = SPIN_LOCK_UNLOCKED;
80 * Hash lists for devmap structures.
82 static struct list_head dasd_hashlists[256];
83 int dasd_max_devindex;
85 static struct dasd_devmap *dasd_add_busid(char *, int);
88 dasd_hash_busid(char *bus_id)
93 for (i = 0; (i < BUS_ID_SIZE) && *bus_id; i++, bus_id++)
100 * The parameter parsing functions for builtin-drivers are called
101 * before kmalloc works. Store the pointers to the parameters strings
102 * into dasd[] for later processing.
105 dasd_call_setup(char *str)
107 static int count = 0;
114 __setup ("dasd=", dasd_call_setup);
115 #endif /* #ifndef MODULE */
118 * Read a device busid/devno from a string.
121 dasd_busid(char **str, int *id0, int *id1, int *devno)
125 /* check for leading '0x' */
127 if ((*str)[0] == '0' && (*str)[1] == 'x') {
131 if (!isxdigit((*str)[0])) /* We require at least one hex digit */
133 val = simple_strtoul(*str, str, 16);
134 if (old_style || (*str)[0] != '.') {
136 if (val < 0 || val > 0xffff)
141 /* New style x.y.z busid */
142 if (val < 0 || val > 0xff)
146 if (!isxdigit((*str)[0])) /* We require at least one hex digit */
148 val = simple_strtoul(*str, str, 16);
149 if (val < 0 || val > 0xff || (*str)++[0] != '.')
152 if (!isxdigit((*str)[0])) /* We require at least one hex digit */
154 val = simple_strtoul(*str, str, 16);
155 if (val < 0 || val > 0xffff)
162 * Read colon separated list of dasd features. Currently there is
163 * only one: "ro" for read-only devices. The default feature set
164 * is empty (value 0).
167 dasd_feature_list(char *str, char **endp)
169 int features, len, rc;
174 return DASD_FEATURE_DEFAULT;
181 str[len] && str[len] != ':' && str[len] != ')'; len++);
182 if (len == 2 && !strncmp(str, "ro", 2))
183 features |= DASD_FEATURE_READONLY;
184 else if (len == 4 && !strncmp(str, "diag", 4))
185 features |= DASD_FEATURE_USEDIAG;
187 MESSAGE(KERN_WARNING,
188 "unsupported feature: %*s, "
189 "ignoring setting", len, str);
198 MESSAGE(KERN_WARNING, "%s",
199 "missing ')' in dasd parameter string\n");
210 * Try to match the first element on the comma separated parse string
211 * with one of the known keywords. If a keyword is found, take the approprate
212 * action and return a pointer to the residual string. If the first element
213 * could not be matched to any keyword then return an error code.
216 dasd_parse_keyword( char *parsestring ) {
218 char *nextcomma, *residual_str;
221 nextcomma = strchr(parsestring,',');
223 length = nextcomma - parsestring;
224 residual_str = nextcomma + 1;
226 length = strlen(parsestring);
227 residual_str = parsestring + length;
229 if (strncmp ("autodetect", parsestring, length) == 0) {
231 MESSAGE (KERN_INFO, "%s",
232 "turning to autodetection mode");
235 if (strncmp ("probeonly", parsestring, length) == 0) {
237 MESSAGE(KERN_INFO, "%s",
238 "turning to probeonly mode");
241 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);
395 * Find devmap for device with given bus_id.
397 static struct dasd_devmap *
398 dasd_find_busid(char *bus_id)
400 struct dasd_devmap *devmap, *tmp;
403 spin_lock(&dasd_devmap_lock);
404 devmap = ERR_PTR(-ENODEV);
405 hash = dasd_hash_busid(bus_id);
406 list_for_each_entry(tmp, &dasd_hashlists[hash], list) {
407 if (strncmp(tmp->bus_id, bus_id, BUS_ID_SIZE) == 0) {
412 spin_unlock(&dasd_devmap_lock);
417 * Check if busid has been added to the list of dasd ranges.
420 dasd_busid_known(char *bus_id)
422 return IS_ERR(dasd_find_busid(bus_id)) ? -ENOENT : 0;
426 * Forget all about the device numbers added so far.
427 * This may only be called at module unload or system shutdown.
430 dasd_forget_ranges(void)
432 struct dasd_devmap *devmap, *n;
435 spin_lock(&dasd_devmap_lock);
436 for (i = 0; i < 256; i++) {
437 list_for_each_entry_safe(devmap, n, &dasd_hashlists[i], list) {
438 if (devmap->device != NULL)
440 list_del(&devmap->list);
444 spin_unlock(&dasd_devmap_lock);
448 * Find the device struct by its device index.
451 dasd_device_from_devindex(int devindex)
453 struct dasd_devmap *devmap, *tmp;
454 struct dasd_device *device;
457 spin_lock(&dasd_devmap_lock);
459 for (i = 0; (i < 256) && !devmap; i++)
460 list_for_each_entry(tmp, &dasd_hashlists[i], list)
461 if (tmp->devindex == devindex) {
462 /* Found the devmap for the device. */
466 if (devmap && devmap->device) {
467 device = devmap->device;
468 dasd_get_device(device);
470 device = ERR_PTR(-ENODEV);
471 spin_unlock(&dasd_devmap_lock);
476 * Return devmap for cdev. If no devmap exists yet, create one and
477 * connect it to the cdev.
479 static struct dasd_devmap *
480 dasd_devmap_from_cdev(struct ccw_device *cdev)
482 struct dasd_devmap *devmap;
484 devmap = dasd_find_busid(cdev->dev.bus_id);
486 devmap = dasd_add_busid(cdev->dev.bus_id, 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 if (devmap->features & DASD_FEATURE_READONLY)
515 set_bit(DASD_FLAG_RO, &device->flags);
517 clear_bit(DASD_FLAG_RO, &device->flags);
518 if (devmap->features & DASD_FEATURE_USEDIAG)
519 set_bit(DASD_FLAG_USE_DIAG, &device->flags);
521 clear_bit(DASD_FLAG_USE_DIAG, &device->flags);
522 get_device(&cdev->dev);
526 /* Someone else was faster. */
528 spin_unlock(&dasd_devmap_lock);
531 dasd_free_device(device);
538 * Wait queue for dasd_delete_device waits.
540 static DECLARE_WAIT_QUEUE_HEAD(dasd_delete_wq);
543 * Remove a dasd device structure. The passed referenced
547 dasd_delete_device(struct dasd_device *device)
549 struct ccw_device *cdev;
550 struct dasd_devmap *devmap;
552 /* First remove device pointer from devmap. */
553 devmap = dasd_find_busid(device->cdev->dev.bus_id);
554 spin_lock(&dasd_devmap_lock);
555 if (devmap->device != device) {
556 spin_unlock(&dasd_devmap_lock);
557 dasd_put_device(device);
560 devmap->device = NULL;
561 spin_unlock(&dasd_devmap_lock);
563 /* Drop ref_count by 2, one for the devmap reference and
564 * one for the passed reference. */
565 atomic_sub(2, &device->ref_count);
567 /* Wait for reference counter to drop to zero. */
568 wait_event(dasd_delete_wq, atomic_read(&device->ref_count) == 0);
570 /* Disconnect dasd_device structure from ccw_device structure. */
574 /* Disconnect dasd_devmap structure from ccw_device structure. */
575 cdev->dev.driver_data = NULL;
577 /* Put ccw_device structure. */
578 put_device(&cdev->dev);
580 /* Now the device structure can be freed. */
581 dasd_free_device(device);
585 * Reference counter dropped to zero. Wake up waiter
586 * in dasd_delete_device.
589 dasd_put_device_wake(struct dasd_device *device)
591 wake_up(&dasd_delete_wq);
595 * Return dasd_device structure associated with cdev.
598 dasd_device_from_cdev(struct ccw_device *cdev)
600 struct dasd_devmap *devmap;
601 struct dasd_device *device;
603 device = ERR_PTR(-ENODEV);
604 spin_lock(&dasd_devmap_lock);
605 devmap = cdev->dev.driver_data;
606 if (devmap && devmap->device) {
607 device = devmap->device;
608 dasd_get_device(device);
610 spin_unlock(&dasd_devmap_lock);
615 * SECTION: files in sysfs
619 * readonly controls the readonly status of a dasd
622 dasd_ro_show(struct device *dev, char *buf)
624 struct dasd_devmap *devmap;
627 devmap = dev->driver_data;
629 ro_flag = (devmap->features & DASD_FEATURE_READONLY) != 0;
631 ro_flag = (DASD_FEATURE_DEFAULT & DASD_FEATURE_READONLY) != 0;
632 return snprintf(buf, PAGE_SIZE, ro_flag ? "1\n" : "0\n");
636 dasd_ro_store(struct device *dev, const char *buf, size_t count)
638 struct dasd_devmap *devmap;
641 devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
642 ro_flag = buf[0] == '1';
643 spin_lock(&dasd_devmap_lock);
645 devmap->features |= DASD_FEATURE_READONLY;
647 devmap->features &= ~DASD_FEATURE_READONLY;
648 if (devmap->device) {
649 if (devmap->device->gdp)
650 set_disk_ro(devmap->device->gdp, ro_flag);
652 set_bit(DASD_FLAG_RO, &devmap->device->flags);
654 clear_bit(DASD_FLAG_RO, &devmap->device->flags);
656 spin_unlock(&dasd_devmap_lock);
660 static DEVICE_ATTR(readonly, 0644, dasd_ro_show, dasd_ro_store);
663 * use_diag controls whether the driver should use diag rather than ssch
664 * to talk to the device
666 /* TODO: Implement */
668 dasd_use_diag_show(struct device *dev, char *buf)
670 struct dasd_devmap *devmap;
673 devmap = dev->driver_data;
675 use_diag = (devmap->features & DASD_FEATURE_USEDIAG) != 0;
677 use_diag = (DASD_FEATURE_DEFAULT & DASD_FEATURE_USEDIAG) != 0;
678 return sprintf(buf, use_diag ? "1\n" : "0\n");
682 dasd_use_diag_store(struct device *dev, const char *buf, size_t count)
684 struct dasd_devmap *devmap;
687 devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
688 use_diag = buf[0] == '1';
689 spin_lock(&dasd_devmap_lock);
690 /* 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);
703 DEVICE_ATTR(use_diag, 0644, dasd_use_diag_show, dasd_use_diag_store);
706 dasd_discipline_show(struct device *dev, char *buf)
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);
722 static struct attribute * dasd_attrs[] = {
723 &dev_attr_readonly.attr,
724 &dev_attr_discipline.attr,
725 &dev_attr_use_diag.attr,
729 static struct attribute_group dasd_attr_group = {
734 dasd_add_sysfs_files(struct ccw_device *cdev)
736 return sysfs_create_group(&cdev->dev.kobj, &dasd_attr_group);
740 dasd_remove_sysfs_files(struct ccw_device *cdev)
742 sysfs_remove_group(&cdev->dev.kobj, &dasd_attr_group);
747 dasd_devmap_init(void)
751 /* Initialize devmap structures. */
752 dasd_max_devindex = 0;
753 for (i = 0; i < 256; i++)
754 INIT_LIST_HEAD(&dasd_hashlists[i]);
760 dasd_devmap_exit(void)
762 dasd_forget_ranges();