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 if (cdev->dev.driver_data)
434 return (struct dasd_devmap *) cdev->dev.driver_data;
435 devmap = dasd_find_busid(cdev->dev.bus_id);
436 if (!IS_ERR(devmap)) {
437 cdev->dev.driver_data = devmap;
440 devmap = dasd_add_busid(cdev->dev.bus_id, DASD_FEATURE_DEFAULT);
442 cdev->dev.driver_data = devmap;
447 * Create a dasd device structure for cdev.
450 dasd_create_device(struct ccw_device *cdev)
452 struct dasd_devmap *devmap;
453 struct dasd_device *device;
456 devmap = dasd_devmap_from_cdev(cdev);
458 return (void *) devmap;
460 device = dasd_alloc_device();
463 atomic_set(&device->ref_count, 2);
465 spin_lock(&dasd_devmap_lock);
466 if (!devmap->device) {
467 devmap->device = device;
468 device->devindex = devmap->devindex;
469 if (devmap->features & DASD_FEATURE_READONLY)
470 set_bit(DASD_FLAG_RO, &device->flags);
472 clear_bit(DASD_FLAG_RO, &device->flags);
473 if (devmap->features & DASD_FEATURE_USEDIAG)
474 set_bit(DASD_FLAG_USE_DIAG, &device->flags);
476 clear_bit(DASD_FLAG_USE_DIAG, &device->flags);
477 get_device(&cdev->dev);
481 /* Someone else was faster. */
483 spin_unlock(&dasd_devmap_lock);
486 dasd_free_device(device);
493 * Wait queue for dasd_delete_device waits.
495 static DECLARE_WAIT_QUEUE_HEAD(dasd_delete_wq);
498 * Remove a dasd device structure. The passed referenced
502 dasd_delete_device(struct dasd_device *device)
504 struct ccw_device *cdev;
505 struct dasd_devmap *devmap;
507 /* First remove device pointer from devmap. */
508 devmap = dasd_find_busid(device->cdev->dev.bus_id);
509 spin_lock(&dasd_devmap_lock);
510 if (devmap->device != device) {
511 spin_unlock(&dasd_devmap_lock);
512 dasd_put_device(device);
515 devmap->device = NULL;
516 spin_unlock(&dasd_devmap_lock);
518 /* Drop ref_count by 2, one for the devmap reference and
519 * one for the passed reference. */
520 atomic_sub(2, &device->ref_count);
522 /* Wait for reference counter to drop to zero. */
523 wait_event(dasd_delete_wq, atomic_read(&device->ref_count) == 0);
525 /* Disconnect dasd_device structure from ccw_device structure. */
529 /* Disconnect dasd_devmap structure from ccw_device structure. */
530 cdev->dev.driver_data = NULL;
532 /* Put ccw_device structure. */
533 put_device(&cdev->dev);
535 /* Now the device structure can be freed. */
536 dasd_free_device(device);
540 * Reference counter dropped to zero. Wake up waiter
541 * in dasd_delete_device.
544 dasd_put_device_wake(struct dasd_device *device)
546 wake_up(&dasd_delete_wq);
550 * Return dasd_device structure associated with cdev.
553 dasd_device_from_cdev(struct ccw_device *cdev)
555 struct dasd_devmap *devmap;
556 struct dasd_device *device;
558 device = ERR_PTR(-ENODEV);
559 spin_lock(&dasd_devmap_lock);
560 devmap = cdev->dev.driver_data;
561 if (devmap && devmap->device) {
562 device = devmap->device;
563 dasd_get_device(device);
565 spin_unlock(&dasd_devmap_lock);
570 * SECTION: files in sysfs
574 * readonly controls the readonly status of a dasd
577 dasd_ro_show(struct device *dev, char *buf)
579 struct dasd_devmap *devmap;
582 devmap = dev->driver_data;
584 ro_flag = (devmap->features & DASD_FEATURE_READONLY) != 0;
586 ro_flag = (DASD_FEATURE_DEFAULT & DASD_FEATURE_READONLY) != 0;
587 return snprintf(buf, PAGE_SIZE, ro_flag ? "1\n" : "0\n");
591 dasd_ro_store(struct device *dev, const char *buf, size_t count)
593 struct dasd_devmap *devmap;
596 devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
597 ro_flag = buf[0] == '1';
598 spin_lock(&dasd_devmap_lock);
600 devmap->features |= DASD_FEATURE_READONLY;
602 devmap->features &= ~DASD_FEATURE_READONLY;
603 if (devmap->device) {
604 if (devmap->device->gdp)
605 set_disk_ro(devmap->device->gdp, ro_flag);
607 set_bit(DASD_FLAG_RO, &devmap->device->flags);
609 clear_bit(DASD_FLAG_RO, &devmap->device->flags);
611 spin_unlock(&dasd_devmap_lock);
615 static DEVICE_ATTR(readonly, 0644, dasd_ro_show, dasd_ro_store);
618 * use_diag controls whether the driver should use diag rather than ssch
619 * to talk to the device
621 /* TODO: Implement */
623 dasd_use_diag_show(struct device *dev, char *buf)
625 struct dasd_devmap *devmap;
628 devmap = dev->driver_data;
630 use_diag = (devmap->features & DASD_FEATURE_USEDIAG) != 0;
632 use_diag = (DASD_FEATURE_DEFAULT & DASD_FEATURE_USEDIAG) != 0;
633 return sprintf(buf, use_diag ? "1\n" : "0\n");
637 dasd_use_diag_store(struct device *dev, const char *buf, size_t count)
639 struct dasd_devmap *devmap;
642 devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
643 use_diag = buf[0] == '1';
644 spin_lock(&dasd_devmap_lock);
645 /* Changing diag discipline flag is only allowed in offline state. */
646 if (!devmap->device) {
648 devmap->features |= DASD_FEATURE_USEDIAG;
650 devmap->features &= ~DASD_FEATURE_USEDIAG;
653 spin_unlock(&dasd_devmap_lock);
658 DEVICE_ATTR(use_diag, 0644, dasd_use_diag_show, dasd_use_diag_store);
661 dasd_discipline_show(struct device *dev, char *buf)
663 struct dasd_devmap *devmap;
666 spin_lock(&dasd_devmap_lock);
668 devmap = dev->driver_data;
669 if (devmap && devmap->device && devmap->device->discipline)
670 dname = devmap->device->discipline->name;
671 spin_unlock(&dasd_devmap_lock);
672 return snprintf(buf, PAGE_SIZE, "%s\n", dname);
675 static DEVICE_ATTR(discipline, 0444, dasd_discipline_show, NULL);
677 static struct attribute * dasd_attrs[] = {
678 &dev_attr_readonly.attr,
679 &dev_attr_discipline.attr,
680 &dev_attr_use_diag.attr,
684 static struct attribute_group dasd_attr_group = {
689 dasd_add_sysfs_files(struct ccw_device *cdev)
691 return sysfs_create_group(&cdev->dev.kobj, &dasd_attr_group);
695 dasd_remove_sysfs_files(struct ccw_device *cdev)
697 sysfs_remove_group(&cdev->dev.kobj, &dasd_attr_group);
702 dasd_devmap_init(void)
706 /* Initialize devmap structures. */
707 dasd_max_devindex = 0;
708 for (i = 0; i < 256; i++)
709 INIT_LIST_HEAD(&dasd_hashlists[i]);
715 dasd_devmap_exit(void)
717 dasd_forget_ranges();