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) {
245 kmem_cache_create("dasd_page_cache", PAGE_SIZE, 0,
246 SLAB_CACHE_DMA, NULL, NULL );
247 if (!dasd_page_cache)
248 MESSAGE(KERN_WARNING, "%s", "Failed to create slab, "
249 "fixed buffer mode disabled.");
251 MESSAGE (KERN_INFO, "%s",
252 "turning on fixed buffer mode");
255 return ERR_PTR(-EINVAL);
259 * Try to interprete the first element on the comma separated parse string
260 * as a device number or a range of devices. If the interpretation is
261 * successfull, create the matching dasd_devmap entries and return a pointer
262 * to the residual string.
263 * If interpretation fails or in case of an error, return an error code.
266 dasd_parse_range( char *parsestring ) {
268 struct dasd_devmap *devmap;
269 int from, from_id0, from_id1;
270 int to, to_id0, to_id1;
272 char bus_id[BUS_ID_SIZE+1], *str;
275 rc = dasd_busid(&str, &from_id0, &from_id1, &from);
282 rc = dasd_busid(&str, &to_id0, &to_id1, &to);
286 (from_id0 != to_id0 || from_id1 != to_id1 || from > to))
289 MESSAGE(KERN_ERR, "Invalid device range %s", parsestring);
292 features = dasd_feature_list(str, &str);
294 return ERR_PTR(-EINVAL);
296 sprintf(bus_id, "%01x.%01x.%04x",
297 from_id0, from_id1, from++);
298 devmap = dasd_add_busid(bus_id, features);
300 return (char *)devmap;
306 MESSAGE(KERN_WARNING,
307 "junk at end of dasd parameter string: %s\n", str);
308 return ERR_PTR(-EINVAL);
312 dasd_parse_next_element( char *parsestring ) {
314 residual_str = dasd_parse_keyword(parsestring);
315 if (!IS_ERR(residual_str))
317 residual_str = dasd_parse_range(parsestring);
322 * Parse parameters stored in dasd[]
323 * The 'dasd=...' parameter allows to specify a comma separated list of
324 * keywords and device ranges. When the dasd driver is build into the kernel,
325 * the complete list will be stored as one element of the dasd[] array.
326 * When the dasd driver is build as a module, then the list is broken into
327 * it's elements and each dasd[] entry contains one element.
336 for (i = 0; i < 256; i++) {
339 parsestring = dasd[i];
340 /* loop over the comma separated list in the parsestring */
341 while (*parsestring) {
342 parsestring = dasd_parse_next_element(parsestring);
343 if(IS_ERR(parsestring)) {
344 rc = PTR_ERR(parsestring);
349 DBF_EVENT(DBF_ALERT, "%s", "invalid range found");
357 * Add a devmap for the device specified by busid. It is possible that
358 * the devmap already exists (dasd= parameter). The order of the devices
359 * added through this function will define the kdevs for the individual
362 static struct dasd_devmap *
363 dasd_add_busid(char *bus_id, int features)
365 struct dasd_devmap *devmap, *new, *tmp;
368 new = (struct dasd_devmap *)
369 kmalloc(sizeof(struct dasd_devmap), GFP_KERNEL);
371 return ERR_PTR(-ENOMEM);
372 spin_lock(&dasd_devmap_lock);
374 hash = dasd_hash_busid(bus_id);
375 list_for_each_entry(tmp, &dasd_hashlists[hash], list)
376 if (strncmp(tmp->bus_id, bus_id, BUS_ID_SIZE) == 0) {
381 /* This bus_id is new. */
382 new->devindex = dasd_max_devindex++;
383 strncpy(new->bus_id, bus_id, BUS_ID_SIZE);
384 new->features = features;
386 list_add(&new->list, &dasd_hashlists[hash]);
390 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, 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 if (devmap->features & DASD_FEATURE_READONLY)
517 set_bit(DASD_FLAG_RO, &device->flags);
519 clear_bit(DASD_FLAG_RO, &device->flags);
520 if (devmap->features & DASD_FEATURE_USEDIAG)
521 set_bit(DASD_FLAG_USE_DIAG, &device->flags);
523 clear_bit(DASD_FLAG_USE_DIAG, &device->flags);
524 get_device(&cdev->dev);
528 /* Someone else was faster. */
530 spin_unlock(&dasd_devmap_lock);
533 dasd_free_device(device);
540 * Wait queue for dasd_delete_device waits.
542 static DECLARE_WAIT_QUEUE_HEAD(dasd_delete_wq);
545 * Remove a dasd device structure. The passed referenced
549 dasd_delete_device(struct dasd_device *device)
551 struct ccw_device *cdev;
552 struct dasd_devmap *devmap;
554 /* First remove device pointer from devmap. */
555 devmap = dasd_find_busid(device->cdev->dev.bus_id);
556 spin_lock(&dasd_devmap_lock);
557 if (devmap->device != device) {
558 spin_unlock(&dasd_devmap_lock);
559 dasd_put_device(device);
562 devmap->device = NULL;
563 spin_unlock(&dasd_devmap_lock);
565 /* Drop ref_count by 2, one for the devmap reference and
566 * one for the passed reference. */
567 atomic_sub(2, &device->ref_count);
569 /* Wait for reference counter to drop to zero. */
570 wait_event(dasd_delete_wq, atomic_read(&device->ref_count) == 0);
572 /* Disconnect dasd_device structure from ccw_device structure. */
576 /* Disconnect dasd_devmap structure from ccw_device structure. */
577 cdev->dev.driver_data = NULL;
579 /* Put ccw_device structure. */
580 put_device(&cdev->dev);
582 /* Now the device structure can be freed. */
583 dasd_free_device(device);
587 * Reference counter dropped to zero. Wake up waiter
588 * in dasd_delete_device.
591 dasd_put_device_wake(struct dasd_device *device)
593 wake_up(&dasd_delete_wq);
597 * Return dasd_device structure associated with cdev.
600 dasd_device_from_cdev(struct ccw_device *cdev)
602 struct dasd_devmap *devmap;
603 struct dasd_device *device;
605 device = ERR_PTR(-ENODEV);
606 spin_lock(&dasd_devmap_lock);
607 devmap = cdev->dev.driver_data;
608 if (devmap && devmap->device) {
609 device = devmap->device;
610 dasd_get_device(device);
612 spin_unlock(&dasd_devmap_lock);
617 * SECTION: files in sysfs
621 * readonly controls the readonly status of a dasd
624 dasd_ro_show(struct device *dev, char *buf)
626 struct dasd_devmap *devmap;
629 devmap = dev->driver_data;
631 ro_flag = (devmap->features & DASD_FEATURE_READONLY) != 0;
633 ro_flag = (DASD_FEATURE_DEFAULT & DASD_FEATURE_READONLY) != 0;
634 return snprintf(buf, PAGE_SIZE, ro_flag ? "1\n" : "0\n");
638 dasd_ro_store(struct device *dev, const char *buf, size_t count)
640 struct dasd_devmap *devmap;
643 devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
644 ro_flag = buf[0] == '1';
645 spin_lock(&dasd_devmap_lock);
647 devmap->features |= DASD_FEATURE_READONLY;
649 devmap->features &= ~DASD_FEATURE_READONLY;
650 if (devmap->device) {
651 if (devmap->device->gdp)
652 set_disk_ro(devmap->device->gdp, ro_flag);
654 set_bit(DASD_FLAG_RO, &devmap->device->flags);
656 clear_bit(DASD_FLAG_RO, &devmap->device->flags);
658 spin_unlock(&dasd_devmap_lock);
662 static DEVICE_ATTR(readonly, 0644, dasd_ro_show, dasd_ro_store);
665 * use_diag controls whether the driver should use diag rather than ssch
666 * to talk to the device
668 /* TODO: Implement */
670 dasd_use_diag_show(struct device *dev, char *buf)
672 struct dasd_devmap *devmap;
675 devmap = dev->driver_data;
677 use_diag = (devmap->features & DASD_FEATURE_USEDIAG) != 0;
679 use_diag = (DASD_FEATURE_DEFAULT & DASD_FEATURE_USEDIAG) != 0;
680 return sprintf(buf, use_diag ? "1\n" : "0\n");
684 dasd_use_diag_store(struct device *dev, const char *buf, size_t count)
686 struct dasd_devmap *devmap;
689 devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
690 use_diag = buf[0] == '1';
691 spin_lock(&dasd_devmap_lock);
692 /* Changing diag discipline flag is only allowed in offline state. */
693 if (!devmap->device) {
695 devmap->features |= DASD_FEATURE_USEDIAG;
697 devmap->features &= ~DASD_FEATURE_USEDIAG;
700 spin_unlock(&dasd_devmap_lock);
705 DEVICE_ATTR(use_diag, 0644, dasd_use_diag_show, dasd_use_diag_store);
708 dasd_discipline_show(struct device *dev, char *buf)
710 struct dasd_devmap *devmap;
713 spin_lock(&dasd_devmap_lock);
715 devmap = dev->driver_data;
716 if (devmap && devmap->device && devmap->device->discipline)
717 dname = devmap->device->discipline->name;
718 spin_unlock(&dasd_devmap_lock);
719 return snprintf(buf, PAGE_SIZE, "%s\n", dname);
722 static DEVICE_ATTR(discipline, 0444, dasd_discipline_show, NULL);
724 static struct attribute * dasd_attrs[] = {
725 &dev_attr_readonly.attr,
726 &dev_attr_discipline.attr,
727 &dev_attr_use_diag.attr,
731 static struct attribute_group dasd_attr_group = {
736 dasd_add_sysfs_files(struct ccw_device *cdev)
738 return sysfs_create_group(&cdev->dev.kobj, &dasd_attr_group);
742 dasd_remove_sysfs_files(struct ccw_device *cdev)
744 sysfs_remove_group(&cdev->dev.kobj, &dasd_attr_group);
749 dasd_devmap_init(void)
753 /* Initialize devmap structures. */
754 dasd_max_devindex = 0;
755 for (i = 0; i < 256; i++)
756 INIT_LIST_HEAD(&dasd_hashlists[i]);
762 dasd_devmap_exit(void)
764 dasd_forget_ranges();