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:"
30 * dasd_devmap_t is used to store the features and the relation
31 * between device number and device index. To find a dasd_devmap_t
32 * that corresponds to a device number of a device index each
33 * dasd_devmap_t is added to two linked lists, one to search by
34 * the device number and one to search by the device index. As
35 * soon as big minor numbers are available the device index list
36 * can be removed since the device number will then be identical
37 * to the device index.
40 struct list_head list;
41 char bus_id[BUS_ID_SIZE];
42 unsigned int devindex;
43 unsigned short features;
44 struct dasd_device *device;
48 * Parameter parsing functions for dasd= parameter. The syntax is:
49 * <devno> : (0x)?[0-9a-fA-F]+
50 * <busid> : [0-0a-f]\.[0-9a-f]\.(0x)?[0-9a-fA-F]+
52 * <feature_list> : \(<feature>(:<feature>)*\)
53 * <devno-range> : <devno>(-<devno>)?<feature_list>?
54 * <busid-range> : <busid>(-<busid>)?<feature_list>?
55 * <devices> : <devno-range>|<busid-range>
56 * <dasd_module> : dasd_diag_mod|dasd_eckd_mod|dasd_fba_mod
58 * <dasd> : autodetect|probeonly|<devices>(,<devices>)*
61 int dasd_probeonly = 0; /* is true, when probeonly mode is active */
62 int dasd_autodetect = 0; /* is true, when autodetection is active */
65 * char *dasd[] is intended to hold the ranges supplied by the dasd= statement
66 * it is named 'dasd' to directly be filled by insmod with the comma separated
67 * strings when running as a module.
69 static char *dasd[256];
72 * Single spinlock to protect devmap structures and lists.
74 static spinlock_t dasd_devmap_lock = SPIN_LOCK_UNLOCKED;
77 * Hash lists for devmap structures.
79 static struct list_head dasd_hashlists[256];
80 int dasd_max_devindex;
82 static struct dasd_devmap *dasd_add_busid(char *, int);
85 dasd_hash_busid(char *bus_id)
90 for (i = 0; (i < BUS_ID_SIZE) && *bus_id; i++, bus_id++)
97 * The parameter parsing functions for builtin-drivers are called
98 * before kmalloc works. Store the pointers to the parameters strings
99 * into dasd[] for later processing.
102 dasd_call_setup(char *str)
104 static int count = 0;
111 __setup ("dasd=", dasd_call_setup);
112 #endif /* #ifndef MODULE */
115 * Read a device busid/devno from a string.
118 dasd_busid(char **str, int *id0, int *id1, int *devno)
122 /* check for leading '0x' */
124 if ((*str)[0] == '0' && (*str)[1] == 'x') {
128 if (!isxdigit((*str)[0])) /* We require at least one hex digit */
130 val = simple_strtoul(*str, str, 16);
131 if (old_style || (*str)[0] != '.') {
133 if (val < 0 || val > 0xffff)
138 /* New style x.y.z busid */
139 if (val < 0 || val > 0xff)
143 if (!isxdigit((*str)[0])) /* We require at least one hex digit */
145 val = simple_strtoul(*str, str, 16);
146 if (val < 0 || val > 0xff || (*str)++[0] != '.')
149 if (!isxdigit((*str)[0])) /* We require at least one hex digit */
151 val = simple_strtoul(*str, str, 16);
152 if (val < 0 || val > 0xffff)
159 * Read colon separated list of dasd features. Currently there is
160 * only one: "ro" for read-only devices. The default feature set
161 * is empty (value 0).
164 dasd_feature_list(char *str, char **endp)
166 int features, len, rc;
171 return DASD_FEATURE_DEFAULT;
178 str[len] && str[len] != ':' && str[len] != ')'; len++);
179 if (len == 2 && !strncmp(str, "ro", 2))
180 features |= DASD_FEATURE_READONLY;
181 else if (len == 4 && !strncmp(str, "diag", 4))
182 features |= DASD_FEATURE_USEDIAG;
184 MESSAGE(KERN_WARNING,
185 "unsupported feature: %*s, "
186 "ignoring setting", len, str);
195 MESSAGE(KERN_WARNING, "%s",
196 "missing ')' in dasd parameter string\n");
207 * Read comma separated list of dasd ranges.
210 dasd_ranges_list(char *str)
212 struct dasd_devmap *devmap;
213 int from, from_id0, from_id1;
214 int to, to_id0, to_id1;
216 char bus_id[BUS_ID_SIZE+1], *orig_str;
220 rc = dasd_busid(&str, &from_id0, &from_id1, &from);
227 rc = dasd_busid(&str, &to_id0, &to_id1, &to);
231 (from_id0 != to_id0 || from_id1 != to_id1 || from > to))
234 MESSAGE(KERN_ERR, "Invalid device range %s", orig_str);
237 features = dasd_feature_list(str, &str);
241 sprintf(bus_id, "%01x.%01x.%04x",
242 from_id0, from_id1, from++);
243 devmap = dasd_add_busid(bus_id, features);
245 return PTR_ERR(devmap);
252 MESSAGE(KERN_WARNING,
253 "junk at end of dasd parameter string: %s\n", str);
260 * Parse a single dasd= parameter.
263 dasd_parameter(char *str)
265 if (strcmp ("autodetect", str) == 0) {
267 MESSAGE (KERN_INFO, "%s",
268 "turning to autodetection mode");
271 if (strcmp ("probeonly", str) == 0) {
273 MESSAGE(KERN_INFO, "%s",
274 "turning to probeonly mode");
277 /* turn off autodetect mode and scan for dasd ranges */
279 return dasd_ranges_list(str);
283 * Parse parameters stored in dasd[] and dasd_disciplines[].
291 for (i = 0; i < 256; i++) {
294 rc = dasd_parameter(dasd[i]);
296 DBF_EVENT(DBF_ALERT, "%s", "invalid range found");
304 * Add a devmap for the device specified by busid. It is possible that
305 * the devmap already exists (dasd= parameter). The order of the devices
306 * added through this function will define the kdevs for the individual
309 static struct dasd_devmap *
310 dasd_add_busid(char *bus_id, int features)
312 struct dasd_devmap *devmap, *new, *tmp;
315 new = (struct dasd_devmap *)
316 kmalloc(sizeof(struct dasd_devmap), GFP_KERNEL);
318 return ERR_PTR(-ENOMEM);
319 spin_lock(&dasd_devmap_lock);
321 hash = dasd_hash_busid(bus_id);
322 list_for_each_entry(tmp, &dasd_hashlists[hash], list)
323 if (strncmp(tmp->bus_id, bus_id, BUS_ID_SIZE) == 0) {
328 /* This bus_id is new. */
329 new->devindex = dasd_max_devindex++;
330 strncpy(new->bus_id, bus_id, BUS_ID_SIZE);
331 new->features = features;
333 list_add(&new->list, &dasd_hashlists[hash]);
337 spin_unlock(&dasd_devmap_lock);
344 * Find devmap for device with given bus_id.
346 static struct dasd_devmap *
347 dasd_find_busid(char *bus_id)
349 struct dasd_devmap *devmap, *tmp;
352 spin_lock(&dasd_devmap_lock);
353 devmap = ERR_PTR(-ENODEV);
354 hash = dasd_hash_busid(bus_id);
355 list_for_each_entry(tmp, &dasd_hashlists[hash], list) {
356 if (strncmp(tmp->bus_id, bus_id, BUS_ID_SIZE) == 0) {
361 spin_unlock(&dasd_devmap_lock);
366 * Check if busid has been added to the list of dasd ranges.
369 dasd_busid_known(char *bus_id)
371 return IS_ERR(dasd_find_busid(bus_id)) ? -ENOENT : 0;
375 * Forget all about the device numbers added so far.
376 * This may only be called at module unload or system shutdown.
379 dasd_forget_ranges(void)
381 struct dasd_devmap *devmap, *n;
384 spin_lock(&dasd_devmap_lock);
385 for (i = 0; i < 256; i++) {
386 list_for_each_entry_safe(devmap, n, &dasd_hashlists[i], list) {
387 if (devmap->device != NULL)
389 list_del(&devmap->list);
393 spin_unlock(&dasd_devmap_lock);
397 * Find the device struct by its device index.
400 dasd_device_from_devindex(int devindex)
402 struct dasd_devmap *devmap, *tmp;
403 struct dasd_device *device;
406 spin_lock(&dasd_devmap_lock);
408 for (i = 0; (i < 256) && !devmap; i++)
409 list_for_each_entry(tmp, &dasd_hashlists[i], list)
410 if (tmp->devindex == devindex) {
411 /* Found the devmap for the device. */
415 if (devmap && devmap->device) {
416 device = devmap->device;
417 dasd_get_device(device);
419 device = ERR_PTR(-ENODEV);
420 spin_unlock(&dasd_devmap_lock);
425 * Return devmap for cdev. If no devmap exists yet, create one and
426 * connect it to the cdev.
428 static struct dasd_devmap *
429 dasd_devmap_from_cdev(struct ccw_device *cdev)
431 struct dasd_devmap *devmap;
433 devmap = dasd_find_busid(cdev->dev.bus_id);
435 devmap = dasd_add_busid(cdev->dev.bus_id, DASD_FEATURE_DEFAULT);
440 * Create a dasd device structure for cdev.
443 dasd_create_device(struct ccw_device *cdev)
445 struct dasd_devmap *devmap;
446 struct dasd_device *device;
449 devmap = dasd_devmap_from_cdev(cdev);
451 return (void *) devmap;
452 cdev->dev.driver_data = devmap;
454 device = dasd_alloc_device();
457 atomic_set(&device->ref_count, 2);
459 spin_lock(&dasd_devmap_lock);
460 if (!devmap->device) {
461 devmap->device = device;
462 device->devindex = devmap->devindex;
463 if (devmap->features & DASD_FEATURE_READONLY)
464 set_bit(DASD_FLAG_RO, &device->flags);
466 clear_bit(DASD_FLAG_RO, &device->flags);
467 if (devmap->features & DASD_FEATURE_USEDIAG)
468 set_bit(DASD_FLAG_USE_DIAG, &device->flags);
470 clear_bit(DASD_FLAG_USE_DIAG, &device->flags);
471 get_device(&cdev->dev);
475 /* Someone else was faster. */
477 spin_unlock(&dasd_devmap_lock);
480 dasd_free_device(device);
487 * Wait queue for dasd_delete_device waits.
489 static DECLARE_WAIT_QUEUE_HEAD(dasd_delete_wq);
492 * Remove a dasd device structure. The passed referenced
496 dasd_delete_device(struct dasd_device *device)
498 struct ccw_device *cdev;
499 struct dasd_devmap *devmap;
501 /* First remove device pointer from devmap. */
502 devmap = dasd_find_busid(device->cdev->dev.bus_id);
503 spin_lock(&dasd_devmap_lock);
504 if (devmap->device != device) {
505 spin_unlock(&dasd_devmap_lock);
506 dasd_put_device(device);
509 devmap->device = NULL;
510 spin_unlock(&dasd_devmap_lock);
512 /* Drop ref_count by 2, one for the devmap reference and
513 * one for the passed reference. */
514 atomic_sub(2, &device->ref_count);
516 /* Wait for reference counter to drop to zero. */
517 wait_event(dasd_delete_wq, atomic_read(&device->ref_count) == 0);
519 /* Disconnect dasd_device structure from ccw_device structure. */
523 /* Disconnect dasd_devmap structure from ccw_device structure. */
524 cdev->dev.driver_data = NULL;
526 /* Put ccw_device structure. */
527 put_device(&cdev->dev);
529 /* Now the device structure can be freed. */
530 dasd_free_device(device);
534 * Reference counter dropped to zero. Wake up waiter
535 * in dasd_delete_device.
538 dasd_put_device_wake(struct dasd_device *device)
540 wake_up(&dasd_delete_wq);
544 * Return dasd_device structure associated with cdev.
547 dasd_device_from_cdev(struct ccw_device *cdev)
549 struct dasd_devmap *devmap;
550 struct dasd_device *device;
552 device = ERR_PTR(-ENODEV);
553 spin_lock(&dasd_devmap_lock);
554 devmap = cdev->dev.driver_data;
555 if (devmap && devmap->device) {
556 device = devmap->device;
557 dasd_get_device(device);
559 spin_unlock(&dasd_devmap_lock);
564 * SECTION: files in sysfs
568 * readonly controls the readonly status of a dasd
571 dasd_ro_show(struct device *dev, char *buf)
573 struct dasd_devmap *devmap;
576 devmap = dev->driver_data;
578 ro_flag = (devmap->features & DASD_FEATURE_READONLY) != 0;
580 ro_flag = (DASD_FEATURE_DEFAULT & DASD_FEATURE_READONLY) != 0;
581 return snprintf(buf, PAGE_SIZE, ro_flag ? "1\n" : "0\n");
585 dasd_ro_store(struct device *dev, const char *buf, size_t count)
587 struct dasd_devmap *devmap;
590 devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
591 ro_flag = buf[0] == '1';
592 spin_lock(&dasd_devmap_lock);
594 devmap->features |= DASD_FEATURE_READONLY;
596 devmap->features &= ~DASD_FEATURE_READONLY;
597 if (devmap->device) {
598 if (devmap->device->gdp)
599 set_disk_ro(devmap->device->gdp, ro_flag);
601 set_bit(DASD_FLAG_RO, &devmap->device->flags);
603 clear_bit(DASD_FLAG_RO, &devmap->device->flags);
605 spin_unlock(&dasd_devmap_lock);
609 static DEVICE_ATTR(readonly, 0644, dasd_ro_show, dasd_ro_store);
612 * use_diag controls whether the driver should use diag rather than ssch
613 * to talk to the device
615 /* TODO: Implement */
617 dasd_use_diag_show(struct device *dev, char *buf)
619 struct dasd_devmap *devmap;
622 devmap = dev->driver_data;
624 use_diag = (devmap->features & DASD_FEATURE_USEDIAG) != 0;
626 use_diag = (DASD_FEATURE_DEFAULT & DASD_FEATURE_USEDIAG) != 0;
627 return sprintf(buf, use_diag ? "1\n" : "0\n");
631 dasd_use_diag_store(struct device *dev, const char *buf, size_t count)
633 struct dasd_devmap *devmap;
636 devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
637 use_diag = buf[0] == '1';
638 spin_lock(&dasd_devmap_lock);
639 /* Changing diag discipline flag is only allowed in offline state. */
640 if (!devmap->device) {
642 devmap->features |= DASD_FEATURE_USEDIAG;
644 devmap->features &= ~DASD_FEATURE_USEDIAG;
647 spin_unlock(&dasd_devmap_lock);
652 DEVICE_ATTR(use_diag, 0644, dasd_use_diag_show, dasd_use_diag_store);
655 dasd_discipline_show(struct device *dev, char *buf)
657 struct dasd_devmap *devmap;
660 spin_lock(&dasd_devmap_lock);
662 devmap = dev->driver_data;
663 if (devmap && devmap->device && devmap->device->discipline)
664 dname = devmap->device->discipline->name;
665 spin_unlock(&dasd_devmap_lock);
666 return snprintf(buf, PAGE_SIZE, "%s\n", dname);
669 static DEVICE_ATTR(discipline, 0444, dasd_discipline_show, NULL);
671 static struct attribute * dasd_attrs[] = {
672 &dev_attr_readonly.attr,
673 &dev_attr_discipline.attr,
674 &dev_attr_use_diag.attr,
678 static struct attribute_group dasd_attr_group = {
683 dasd_add_sysfs_files(struct ccw_device *cdev)
685 return sysfs_create_group(&cdev->dev.kobj, &dasd_attr_group);
689 dasd_remove_sysfs_files(struct ccw_device *cdev)
691 sysfs_remove_group(&cdev->dev.kobj, &dasd_attr_group);
696 dasd_devmap_init(void)
700 /* Initialize devmap structures. */
701 dasd_max_devindex = 0;
702 for (i = 0; i < 256; i++)
703 INIT_LIST_HEAD(&dasd_hashlists[i]);
709 dasd_devmap_exit(void)
711 dasd_forget_ranges();