[PATCH] Fix RocketPort driver
[linux-2.6/verdex.git] / drivers / s390 / block / dasd_devmap.c
blob1629b27c48ab7d57a73e433156269a323d4a900e
1 /*
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.
16 #include <linux/config.h>
17 #include <linux/ctype.h>
18 #include <linux/init.h>
20 #include <asm/debug.h>
21 #include <asm/uaccess.h>
23 /* This is ugly... */
24 #define PRINTK_HEADER "dasd_devmap:"
26 #include "dasd_int.h"
28 kmem_cache_t *dasd_page_cache;
29 EXPORT_SYMBOL(dasd_page_cache);
32 * dasd_devmap_t is used to store the features and the relation
33 * between device number and device index. To find a dasd_devmap_t
34 * that corresponds to a device number of a device index each
35 * dasd_devmap_t is added to two linked lists, one to search by
36 * the device number and one to search by the device index. As
37 * soon as big minor numbers are available the device index list
38 * can be removed since the device number will then be identical
39 * to the device index.
41 struct dasd_devmap {
42 struct list_head list;
43 char bus_id[BUS_ID_SIZE];
44 unsigned int devindex;
45 unsigned short features;
46 struct dasd_device *device;
50 * Parameter parsing functions for dasd= parameter. The syntax is:
51 * <devno> : (0x)?[0-9a-fA-F]+
52 * <busid> : [0-0a-f]\.[0-9a-f]\.(0x)?[0-9a-fA-F]+
53 * <feature> : ro
54 * <feature_list> : \(<feature>(:<feature>)*\)
55 * <devno-range> : <devno>(-<devno>)?<feature_list>?
56 * <busid-range> : <busid>(-<busid>)?<feature_list>?
57 * <devices> : <devno-range>|<busid-range>
58 * <dasd_module> : dasd_diag_mod|dasd_eckd_mod|dasd_fba_mod
60 * <dasd> : autodetect|probeonly|<devices>(,<devices>)*
63 int dasd_probeonly = 0; /* is true, when probeonly mode is active */
64 int dasd_autodetect = 0; /* is true, when autodetection is active */
67 * char *dasd[] is intended to hold the ranges supplied by the dasd= statement
68 * it is named 'dasd' to directly be filled by insmod with the comma separated
69 * strings when running as a module.
71 static char *dasd[256];
73 * Single spinlock to protect devmap structures and lists.
75 static DEFINE_SPINLOCK(dasd_devmap_lock);
78 * Hash lists for devmap structures.
80 static struct list_head dasd_hashlists[256];
81 int dasd_max_devindex;
83 static struct dasd_devmap *dasd_add_busid(char *, int);
85 static inline int
86 dasd_hash_busid(char *bus_id)
88 int hash, i;
90 hash = 0;
91 for (i = 0; (i < BUS_ID_SIZE) && *bus_id; i++, bus_id++)
92 hash += *bus_id;
93 return hash & 0xff;
96 #ifndef MODULE
98 * The parameter parsing functions for builtin-drivers are called
99 * before kmalloc works. Store the pointers to the parameters strings
100 * into dasd[] for later processing.
102 static int __init
103 dasd_call_setup(char *str)
105 static int count = 0;
107 if (count < 256)
108 dasd[count++] = str;
109 return 1;
112 __setup ("dasd=", dasd_call_setup);
113 #endif /* #ifndef MODULE */
116 * Read a device busid/devno from a string.
118 static inline int
119 dasd_busid(char **str, int *id0, int *id1, int *devno)
121 int val, old_style;
123 /* check for leading '0x' */
124 old_style = 0;
125 if ((*str)[0] == '0' && (*str)[1] == 'x') {
126 *str += 2;
127 old_style = 1;
129 if (!isxdigit((*str)[0])) /* We require at least one hex digit */
130 return -EINVAL;
131 val = simple_strtoul(*str, str, 16);
132 if (old_style || (*str)[0] != '.') {
133 *id0 = *id1 = 0;
134 if (val < 0 || val > 0xffff)
135 return -EINVAL;
136 *devno = val;
137 return 0;
139 /* New style x.y.z busid */
140 if (val < 0 || val > 0xff)
141 return -EINVAL;
142 *id0 = val;
143 (*str)++;
144 if (!isxdigit((*str)[0])) /* We require at least one hex digit */
145 return -EINVAL;
146 val = simple_strtoul(*str, str, 16);
147 if (val < 0 || val > 0xff || (*str)++[0] != '.')
148 return -EINVAL;
149 *id1 = val;
150 if (!isxdigit((*str)[0])) /* We require at least one hex digit */
151 return -EINVAL;
152 val = simple_strtoul(*str, str, 16);
153 if (val < 0 || val > 0xffff)
154 return -EINVAL;
155 *devno = val;
156 return 0;
160 * Read colon separated list of dasd features. Currently there is
161 * only one: "ro" for read-only devices. The default feature set
162 * is empty (value 0).
164 static inline int
165 dasd_feature_list(char *str, char **endp)
167 int features, len, rc;
169 rc = 0;
170 if (*str != '(') {
171 *endp = str;
172 return DASD_FEATURE_DEFAULT;
174 str++;
175 features = 0;
177 while (1) {
178 for (len = 0;
179 str[len] && str[len] != ':' && str[len] != ')'; len++);
180 if (len == 2 && !strncmp(str, "ro", 2))
181 features |= DASD_FEATURE_READONLY;
182 else if (len == 4 && !strncmp(str, "diag", 4))
183 features |= DASD_FEATURE_USEDIAG;
184 else {
185 MESSAGE(KERN_WARNING,
186 "unsupported feature: %*s, "
187 "ignoring setting", len, str);
188 rc = -EINVAL;
190 str += len;
191 if (*str != ':')
192 break;
193 str++;
195 if (*str != ')') {
196 MESSAGE(KERN_WARNING, "%s",
197 "missing ')' in dasd parameter string\n");
198 rc = -EINVAL;
199 } else
200 str++;
201 *endp = str;
202 if (rc != 0)
203 return rc;
204 return features;
208 * Try to match the first element on the comma separated parse string
209 * with one of the known keywords. If a keyword is found, take the approprate
210 * action and return a pointer to the residual string. If the first element
211 * could not be matched to any keyword then return an error code.
213 static char *
214 dasd_parse_keyword( char *parsestring ) {
216 char *nextcomma, *residual_str;
217 int length;
219 nextcomma = strchr(parsestring,',');
220 if (nextcomma) {
221 length = nextcomma - parsestring;
222 residual_str = nextcomma + 1;
223 } else {
224 length = strlen(parsestring);
225 residual_str = parsestring + length;
227 if (strncmp ("autodetect", parsestring, length) == 0) {
228 dasd_autodetect = 1;
229 MESSAGE (KERN_INFO, "%s",
230 "turning to autodetection mode");
231 return residual_str;
233 if (strncmp ("probeonly", parsestring, length) == 0) {
234 dasd_probeonly = 1;
235 MESSAGE(KERN_INFO, "%s",
236 "turning to probeonly mode");
237 return residual_str;
239 if (strncmp ("fixedbuffers", parsestring, length) == 0) {
240 if (dasd_page_cache)
241 return residual_str;
242 dasd_page_cache =
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.");
248 else
249 MESSAGE (KERN_INFO, "%s",
250 "turning on fixed buffer mode");
251 return residual_str;
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.
263 static char *
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;
269 int features, rc;
270 char bus_id[BUS_ID_SIZE+1], *str;
272 str = parsestring;
273 rc = dasd_busid(&str, &from_id0, &from_id1, &from);
274 if (rc == 0) {
275 to = from;
276 to_id0 = from_id0;
277 to_id1 = from_id1;
278 if (*str == '-') {
279 str++;
280 rc = dasd_busid(&str, &to_id0, &to_id1, &to);
283 if (rc == 0 &&
284 (from_id0 != to_id0 || from_id1 != to_id1 || from > to))
285 rc = -EINVAL;
286 if (rc) {
287 MESSAGE(KERN_ERR, "Invalid device range %s", parsestring);
288 return ERR_PTR(rc);
290 features = dasd_feature_list(str, &str);
291 if (features < 0)
292 return ERR_PTR(-EINVAL);
293 while (from <= to) {
294 sprintf(bus_id, "%01x.%01x.%04x",
295 from_id0, from_id1, from++);
296 devmap = dasd_add_busid(bus_id, features);
297 if (IS_ERR(devmap))
298 return (char *)devmap;
300 if (*str == ',')
301 return str + 1;
302 if (*str == '\0')
303 return str;
304 MESSAGE(KERN_WARNING,
305 "junk at end of dasd parameter string: %s\n", str);
306 return ERR_PTR(-EINVAL);
309 static inline char *
310 dasd_parse_next_element( char *parsestring ) {
311 char * residual_str;
312 residual_str = dasd_parse_keyword(parsestring);
313 if (!IS_ERR(residual_str))
314 return residual_str;
315 residual_str = dasd_parse_range(parsestring);
316 return residual_str;
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.
328 dasd_parse(void)
330 int rc, i;
331 char *parsestring;
333 rc = 0;
334 for (i = 0; i < 256; i++) {
335 if (dasd[i] == NULL)
336 break;
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);
343 break;
346 if (rc) {
347 DBF_EVENT(DBF_ALERT, "%s", "invalid range found");
348 break;
351 return rc;
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
358 * devices.
360 static struct dasd_devmap *
361 dasd_add_busid(char *bus_id, int features)
363 struct dasd_devmap *devmap, *new, *tmp;
364 int hash;
366 new = (struct dasd_devmap *)
367 kmalloc(sizeof(struct dasd_devmap), GFP_KERNEL);
368 if (!new)
369 return ERR_PTR(-ENOMEM);
370 spin_lock(&dasd_devmap_lock);
371 devmap = 0;
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) {
375 devmap = tmp;
376 break;
378 if (!devmap) {
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;
383 new->device = 0;
384 list_add(&new->list, &dasd_hashlists[hash]);
385 devmap = new;
386 new = 0;
388 spin_unlock(&dasd_devmap_lock);
389 kfree(new);
390 return devmap;
394 * Find devmap for device with given bus_id.
396 static struct dasd_devmap *
397 dasd_find_busid(char *bus_id)
399 struct dasd_devmap *devmap, *tmp;
400 int hash;
402 spin_lock(&dasd_devmap_lock);
403 devmap = ERR_PTR(-ENODEV);
404 hash = dasd_hash_busid(bus_id);
405 list_for_each_entry(tmp, &dasd_hashlists[hash], list) {
406 if (strncmp(tmp->bus_id, bus_id, BUS_ID_SIZE) == 0) {
407 devmap = tmp;
408 break;
411 spin_unlock(&dasd_devmap_lock);
412 return devmap;
416 * Check if busid has been added to the list of dasd ranges.
419 dasd_busid_known(char *bus_id)
421 return IS_ERR(dasd_find_busid(bus_id)) ? -ENOENT : 0;
425 * Forget all about the device numbers added so far.
426 * This may only be called at module unload or system shutdown.
428 static void
429 dasd_forget_ranges(void)
431 struct dasd_devmap *devmap, *n;
432 int i;
434 spin_lock(&dasd_devmap_lock);
435 for (i = 0; i < 256; i++) {
436 list_for_each_entry_safe(devmap, n, &dasd_hashlists[i], list) {
437 if (devmap->device != NULL)
438 BUG();
439 list_del(&devmap->list);
440 kfree(devmap);
443 spin_unlock(&dasd_devmap_lock);
447 * Find the device struct by its device index.
449 struct dasd_device *
450 dasd_device_from_devindex(int devindex)
452 struct dasd_devmap *devmap, *tmp;
453 struct dasd_device *device;
454 int i;
456 spin_lock(&dasd_devmap_lock);
457 devmap = 0;
458 for (i = 0; (i < 256) && !devmap; i++)
459 list_for_each_entry(tmp, &dasd_hashlists[i], list)
460 if (tmp->devindex == devindex) {
461 /* Found the devmap for the device. */
462 devmap = tmp;
463 break;
465 if (devmap && devmap->device) {
466 device = devmap->device;
467 dasd_get_device(device);
468 } else
469 device = ERR_PTR(-ENODEV);
470 spin_unlock(&dasd_devmap_lock);
471 return device;
475 * Return devmap for cdev. If no devmap exists yet, create one and
476 * connect it to the cdev.
478 static struct dasd_devmap *
479 dasd_devmap_from_cdev(struct ccw_device *cdev)
481 struct dasd_devmap *devmap;
483 devmap = dasd_find_busid(cdev->dev.bus_id);
484 if (IS_ERR(devmap))
485 devmap = dasd_add_busid(cdev->dev.bus_id,
486 DASD_FEATURE_DEFAULT);
487 return devmap;
491 * Create a dasd device structure for cdev.
493 struct dasd_device *
494 dasd_create_device(struct ccw_device *cdev)
496 struct dasd_devmap *devmap;
497 struct dasd_device *device;
498 int rc;
500 devmap = dasd_devmap_from_cdev(cdev);
501 if (IS_ERR(devmap))
502 return (void *) devmap;
503 cdev->dev.driver_data = devmap;
505 device = dasd_alloc_device();
506 if (IS_ERR(device))
507 return 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 device->features = devmap->features;
515 get_device(&cdev->dev);
516 device->cdev = cdev;
517 rc = 0;
518 } else
519 /* Someone else was faster. */
520 rc = -EBUSY;
521 spin_unlock(&dasd_devmap_lock);
523 if (rc) {
524 dasd_free_device(device);
525 return ERR_PTR(rc);
527 return device;
531 * Wait queue for dasd_delete_device waits.
533 static DECLARE_WAIT_QUEUE_HEAD(dasd_delete_wq);
536 * Remove a dasd device structure. The passed referenced
537 * is destroyed.
539 void
540 dasd_delete_device(struct dasd_device *device)
542 struct ccw_device *cdev;
543 struct dasd_devmap *devmap;
545 /* First remove device pointer from devmap. */
546 devmap = dasd_find_busid(device->cdev->dev.bus_id);
547 if (IS_ERR(devmap))
548 BUG();
549 spin_lock(&dasd_devmap_lock);
550 if (devmap->device != device) {
551 spin_unlock(&dasd_devmap_lock);
552 dasd_put_device(device);
553 return;
555 devmap->device = NULL;
556 spin_unlock(&dasd_devmap_lock);
558 /* Drop ref_count by 2, one for the devmap reference and
559 * one for the passed reference. */
560 atomic_sub(2, &device->ref_count);
562 /* Wait for reference counter to drop to zero. */
563 wait_event(dasd_delete_wq, atomic_read(&device->ref_count) == 0);
565 /* Disconnect dasd_device structure from ccw_device structure. */
566 cdev = device->cdev;
567 device->cdev = NULL;
569 /* Disconnect dasd_devmap structure from ccw_device structure. */
570 cdev->dev.driver_data = NULL;
572 /* Put ccw_device structure. */
573 put_device(&cdev->dev);
575 /* Now the device structure can be freed. */
576 dasd_free_device(device);
580 * Reference counter dropped to zero. Wake up waiter
581 * in dasd_delete_device.
583 void
584 dasd_put_device_wake(struct dasd_device *device)
586 wake_up(&dasd_delete_wq);
590 * Return dasd_device structure associated with cdev.
592 struct dasd_device *
593 dasd_device_from_cdev(struct ccw_device *cdev)
595 struct dasd_devmap *devmap;
596 struct dasd_device *device;
598 device = ERR_PTR(-ENODEV);
599 spin_lock(&dasd_devmap_lock);
600 devmap = cdev->dev.driver_data;
601 if (devmap && devmap->device) {
602 device = devmap->device;
603 dasd_get_device(device);
605 spin_unlock(&dasd_devmap_lock);
606 return device;
610 * SECTION: files in sysfs
614 * readonly controls the readonly status of a dasd
616 static ssize_t
617 dasd_ro_show(struct device *dev, struct device_attribute *attr, char *buf)
619 struct dasd_devmap *devmap;
620 int ro_flag;
622 devmap = dasd_find_busid(dev->bus_id);
623 if (!IS_ERR(devmap))
624 ro_flag = (devmap->features & DASD_FEATURE_READONLY) != 0;
625 else
626 ro_flag = (DASD_FEATURE_DEFAULT & DASD_FEATURE_READONLY) != 0;
627 return snprintf(buf, PAGE_SIZE, ro_flag ? "1\n" : "0\n");
630 static ssize_t
631 dasd_ro_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
633 struct dasd_devmap *devmap;
634 int ro_flag;
636 devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
637 if (IS_ERR(devmap))
638 return PTR_ERR(devmap);
639 ro_flag = buf[0] == '1';
640 spin_lock(&dasd_devmap_lock);
641 if (ro_flag)
642 devmap->features |= DASD_FEATURE_READONLY;
643 else
644 devmap->features &= ~DASD_FEATURE_READONLY;
645 if (devmap->device)
646 devmap->device->features = devmap->features;
647 if (devmap->device && devmap->device->gdp)
648 set_disk_ro(devmap->device->gdp, ro_flag);
649 spin_unlock(&dasd_devmap_lock);
650 return count;
653 static DEVICE_ATTR(readonly, 0644, dasd_ro_show, dasd_ro_store);
656 * use_diag controls whether the driver should use diag rather than ssch
657 * to talk to the device
659 static ssize_t
660 dasd_use_diag_show(struct device *dev, struct device_attribute *attr, char *buf)
662 struct dasd_devmap *devmap;
663 int use_diag;
665 devmap = dasd_find_busid(dev->bus_id);
666 if (!IS_ERR(devmap))
667 use_diag = (devmap->features & DASD_FEATURE_USEDIAG) != 0;
668 else
669 use_diag = (DASD_FEATURE_DEFAULT & DASD_FEATURE_USEDIAG) != 0;
670 return sprintf(buf, use_diag ? "1\n" : "0\n");
673 static ssize_t
674 dasd_use_diag_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
676 struct dasd_devmap *devmap;
677 ssize_t rc;
678 int use_diag;
680 devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
681 if (IS_ERR(devmap))
682 return PTR_ERR(devmap);
683 use_diag = buf[0] == '1';
684 spin_lock(&dasd_devmap_lock);
685 /* Changing diag discipline flag is only allowed in offline state. */
686 rc = count;
687 if (!devmap->device) {
688 if (use_diag)
689 devmap->features |= DASD_FEATURE_USEDIAG;
690 else
691 devmap->features &= ~DASD_FEATURE_USEDIAG;
692 } else
693 rc = -EPERM;
694 spin_unlock(&dasd_devmap_lock);
695 return rc;
698 static
699 DEVICE_ATTR(use_diag, 0644, dasd_use_diag_show, dasd_use_diag_store);
701 static ssize_t
702 dasd_discipline_show(struct device *dev, struct device_attribute *attr, char *buf)
704 struct dasd_devmap *devmap;
705 char *dname;
707 spin_lock(&dasd_devmap_lock);
708 dname = "none";
709 devmap = dev->driver_data;
710 if (devmap && devmap->device && devmap->device->discipline)
711 dname = devmap->device->discipline->name;
712 spin_unlock(&dasd_devmap_lock);
713 return snprintf(buf, PAGE_SIZE, "%s\n", dname);
716 static DEVICE_ATTR(discipline, 0444, dasd_discipline_show, NULL);
718 static struct attribute * dasd_attrs[] = {
719 &dev_attr_readonly.attr,
720 &dev_attr_discipline.attr,
721 &dev_attr_use_diag.attr,
722 NULL,
725 static struct attribute_group dasd_attr_group = {
726 .attrs = dasd_attrs,
730 * Return value of the specified feature.
733 dasd_get_feature(struct ccw_device *cdev, int feature)
735 struct dasd_devmap *devmap;
737 devmap = dasd_find_busid(cdev->dev.bus_id);
738 if (IS_ERR(devmap))
739 return (int) PTR_ERR(devmap);
741 return ((devmap->features & feature) != 0);
745 * Set / reset given feature.
746 * Flag indicates wether to set (!=0) or the reset (=0) the feature.
749 dasd_set_feature(struct ccw_device *cdev, int feature, int flag)
751 struct dasd_devmap *devmap;
753 devmap = dasd_find_busid(cdev->dev.bus_id);
754 if (IS_ERR(devmap))
755 return (int) PTR_ERR(devmap);
757 spin_lock(&dasd_devmap_lock);
758 if (flag)
759 devmap->features |= feature;
760 else
761 devmap->features &= ~feature;
762 if (devmap->device)
763 devmap->device->features = devmap->features;
764 spin_unlock(&dasd_devmap_lock);
765 return 0;
770 dasd_add_sysfs_files(struct ccw_device *cdev)
772 return sysfs_create_group(&cdev->dev.kobj, &dasd_attr_group);
775 void
776 dasd_remove_sysfs_files(struct ccw_device *cdev)
778 sysfs_remove_group(&cdev->dev.kobj, &dasd_attr_group);
783 dasd_devmap_init(void)
785 int i;
787 /* Initialize devmap structures. */
788 dasd_max_devindex = 0;
789 for (i = 0; i < 256; i++)
790 INIT_LIST_HEAD(&dasd_hashlists[i]);
791 return 0;
795 void
796 dasd_devmap_exit(void)
798 dasd_forget_ranges();