pata_cs5536.c bugfix
[linux-2.6/libata-dev.git] / drivers / s390 / block / dasd_devmap.c
blobf4fb40257348e3bd3e42b59b1061332233e1eed0
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/ctype.h>
17 #include <linux/init.h>
18 #include <linux/module.h>
20 #include <asm/debug.h>
21 #include <asm/uaccess.h>
22 #include <asm/ipl.h>
24 /* This is ugly... */
25 #define PRINTK_HEADER "dasd_devmap:"
27 #include "dasd_int.h"
29 struct kmem_cache *dasd_page_cache;
30 EXPORT_SYMBOL_GPL(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.
42 struct dasd_devmap {
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;
48 struct dasd_uid uid;
52 * Parameter parsing functions for dasd= parameter. The syntax is:
53 * <devno> : (0x)?[0-9a-fA-F]+
54 * <busid> : [0-0a-f]\.[0-9a-f]\.(0x)?[0-9a-fA-F]+
55 * <feature> : ro
56 * <feature_list> : \(<feature>(:<feature>)*\)
57 * <devno-range> : <devno>(-<devno>)?<feature_list>?
58 * <busid-range> : <busid>(-<busid>)?<feature_list>?
59 * <devices> : <devno-range>|<busid-range>
60 * <dasd_module> : dasd_diag_mod|dasd_eckd_mod|dasd_fba_mod
62 * <dasd> : autodetect|probeonly|<devices>(,<devices>)*
65 int dasd_probeonly = 0; /* is true, when probeonly mode is active */
66 int dasd_autodetect = 0; /* is true, when autodetection is active */
67 int dasd_nopav = 0; /* is true, when PAV is disabled */
68 EXPORT_SYMBOL_GPL(dasd_nopav);
71 * char *dasd[] is intended to hold the ranges supplied by the dasd= statement
72 * it is named 'dasd' to directly be filled by insmod with the comma separated
73 * strings when running as a module.
75 static char *dasd[256];
76 module_param_array(dasd, charp, NULL, 0);
79 * Single spinlock to protect devmap and servermap structures and lists.
81 static DEFINE_SPINLOCK(dasd_devmap_lock);
84 * Hash lists for devmap structures.
86 static struct list_head dasd_hashlists[256];
87 int dasd_max_devindex;
89 static struct dasd_devmap *dasd_add_busid(char *, int);
91 static inline int
92 dasd_hash_busid(char *bus_id)
94 int hash, i;
96 hash = 0;
97 for (i = 0; (i < BUS_ID_SIZE) && *bus_id; i++, bus_id++)
98 hash += *bus_id;
99 return hash & 0xff;
102 #ifndef MODULE
104 * The parameter parsing functions for builtin-drivers are called
105 * before kmalloc works. Store the pointers to the parameters strings
106 * into dasd[] for later processing.
108 static int __init
109 dasd_call_setup(char *str)
111 static int count = 0;
113 if (count < 256)
114 dasd[count++] = str;
115 return 1;
118 __setup ("dasd=", dasd_call_setup);
119 #endif /* #ifndef MODULE */
121 #define DASD_IPLDEV "ipldev"
124 * Read a device busid/devno from a string.
126 static int
127 dasd_busid(char **str, int *id0, int *id1, int *devno)
129 int val, old_style;
131 /* Interpret ipldev busid */
132 if (strncmp(DASD_IPLDEV, *str, strlen(DASD_IPLDEV)) == 0) {
133 if (ipl_info.type != IPL_TYPE_CCW) {
134 MESSAGE(KERN_ERR, "%s", "ipl device is not a ccw "
135 "device");
136 return -EINVAL;
138 *id0 = 0;
139 *id1 = ipl_info.data.ccw.dev_id.ssid;
140 *devno = ipl_info.data.ccw.dev_id.devno;
141 *str += strlen(DASD_IPLDEV);
143 return 0;
145 /* check for leading '0x' */
146 old_style = 0;
147 if ((*str)[0] == '0' && (*str)[1] == 'x') {
148 *str += 2;
149 old_style = 1;
151 if (!isxdigit((*str)[0])) /* We require at least one hex digit */
152 return -EINVAL;
153 val = simple_strtoul(*str, str, 16);
154 if (old_style || (*str)[0] != '.') {
155 *id0 = *id1 = 0;
156 if (val < 0 || val > 0xffff)
157 return -EINVAL;
158 *devno = val;
159 return 0;
161 /* New style x.y.z busid */
162 if (val < 0 || val > 0xff)
163 return -EINVAL;
164 *id0 = val;
165 (*str)++;
166 if (!isxdigit((*str)[0])) /* We require at least one hex digit */
167 return -EINVAL;
168 val = simple_strtoul(*str, str, 16);
169 if (val < 0 || val > 0xff || (*str)++[0] != '.')
170 return -EINVAL;
171 *id1 = val;
172 if (!isxdigit((*str)[0])) /* We require at least one hex digit */
173 return -EINVAL;
174 val = simple_strtoul(*str, str, 16);
175 if (val < 0 || val > 0xffff)
176 return -EINVAL;
177 *devno = val;
178 return 0;
182 * Read colon separated list of dasd features. Currently there is
183 * only one: "ro" for read-only devices. The default feature set
184 * is empty (value 0).
186 static int
187 dasd_feature_list(char *str, char **endp)
189 int features, len, rc;
191 rc = 0;
192 if (*str != '(') {
193 *endp = str;
194 return DASD_FEATURE_DEFAULT;
196 str++;
197 features = 0;
199 while (1) {
200 for (len = 0;
201 str[len] && str[len] != ':' && str[len] != ')'; len++);
202 if (len == 2 && !strncmp(str, "ro", 2))
203 features |= DASD_FEATURE_READONLY;
204 else if (len == 4 && !strncmp(str, "diag", 4))
205 features |= DASD_FEATURE_USEDIAG;
206 else if (len == 6 && !strncmp(str, "erplog", 6))
207 features |= DASD_FEATURE_ERPLOG;
208 else {
209 MESSAGE(KERN_WARNING,
210 "unsupported feature: %*s, "
211 "ignoring setting", len, str);
212 rc = -EINVAL;
214 str += len;
215 if (*str != ':')
216 break;
217 str++;
219 if (*str != ')') {
220 MESSAGE(KERN_WARNING, "%s",
221 "missing ')' in dasd parameter string\n");
222 rc = -EINVAL;
223 } else
224 str++;
225 *endp = str;
226 if (rc != 0)
227 return rc;
228 return features;
232 * Try to match the first element on the comma separated parse string
233 * with one of the known keywords. If a keyword is found, take the approprate
234 * action and return a pointer to the residual string. If the first element
235 * could not be matched to any keyword then return an error code.
237 static char *
238 dasd_parse_keyword( char *parsestring ) {
240 char *nextcomma, *residual_str;
241 int length;
243 nextcomma = strchr(parsestring,',');
244 if (nextcomma) {
245 length = nextcomma - parsestring;
246 residual_str = nextcomma + 1;
247 } else {
248 length = strlen(parsestring);
249 residual_str = parsestring + length;
251 if (strncmp("autodetect", parsestring, length) == 0) {
252 dasd_autodetect = 1;
253 MESSAGE (KERN_INFO, "%s",
254 "turning to autodetection mode");
255 return residual_str;
257 if (strncmp("probeonly", parsestring, length) == 0) {
258 dasd_probeonly = 1;
259 MESSAGE(KERN_INFO, "%s",
260 "turning to probeonly mode");
261 return residual_str;
263 if (strncmp("nopav", parsestring, length) == 0) {
264 if (MACHINE_IS_VM)
265 MESSAGE(KERN_INFO, "%s", "'nopav' not supported on VM");
266 else {
267 dasd_nopav = 1;
268 MESSAGE(KERN_INFO, "%s", "disable PAV mode");
270 return residual_str;
272 if (strncmp("fixedbuffers", parsestring, length) == 0) {
273 if (dasd_page_cache)
274 return residual_str;
275 dasd_page_cache =
276 kmem_cache_create("dasd_page_cache", PAGE_SIZE,
277 PAGE_SIZE, SLAB_CACHE_DMA,
278 NULL);
279 if (!dasd_page_cache)
280 MESSAGE(KERN_WARNING, "%s", "Failed to create slab, "
281 "fixed buffer mode disabled.");
282 else
283 MESSAGE (KERN_INFO, "%s",
284 "turning on fixed buffer mode");
285 return residual_str;
287 return ERR_PTR(-EINVAL);
291 * Try to interprete the first element on the comma separated parse string
292 * as a device number or a range of devices. If the interpretation is
293 * successfull, create the matching dasd_devmap entries and return a pointer
294 * to the residual string.
295 * If interpretation fails or in case of an error, return an error code.
297 static char *
298 dasd_parse_range( char *parsestring ) {
300 struct dasd_devmap *devmap;
301 int from, from_id0, from_id1;
302 int to, to_id0, to_id1;
303 int features, rc;
304 char bus_id[BUS_ID_SIZE+1], *str;
306 str = parsestring;
307 rc = dasd_busid(&str, &from_id0, &from_id1, &from);
308 if (rc == 0) {
309 to = from;
310 to_id0 = from_id0;
311 to_id1 = from_id1;
312 if (*str == '-') {
313 str++;
314 rc = dasd_busid(&str, &to_id0, &to_id1, &to);
317 if (rc == 0 &&
318 (from_id0 != to_id0 || from_id1 != to_id1 || from > to))
319 rc = -EINVAL;
320 if (rc) {
321 MESSAGE(KERN_ERR, "Invalid device range %s", parsestring);
322 return ERR_PTR(rc);
324 features = dasd_feature_list(str, &str);
325 if (features < 0)
326 return ERR_PTR(-EINVAL);
327 /* each device in dasd= parameter should be set initially online */
328 features |= DASD_FEATURE_INITIAL_ONLINE;
329 while (from <= to) {
330 sprintf(bus_id, "%01x.%01x.%04x",
331 from_id0, from_id1, from++);
332 devmap = dasd_add_busid(bus_id, features);
333 if (IS_ERR(devmap))
334 return (char *)devmap;
336 if (*str == ',')
337 return str + 1;
338 if (*str == '\0')
339 return str;
340 MESSAGE(KERN_WARNING,
341 "junk at end of dasd parameter string: %s\n", str);
342 return ERR_PTR(-EINVAL);
345 static char *
346 dasd_parse_next_element( char *parsestring ) {
347 char * residual_str;
348 residual_str = dasd_parse_keyword(parsestring);
349 if (!IS_ERR(residual_str))
350 return residual_str;
351 residual_str = dasd_parse_range(parsestring);
352 return residual_str;
356 * Parse parameters stored in dasd[]
357 * The 'dasd=...' parameter allows to specify a comma separated list of
358 * keywords and device ranges. When the dasd driver is build into the kernel,
359 * the complete list will be stored as one element of the dasd[] array.
360 * When the dasd driver is build as a module, then the list is broken into
361 * it's elements and each dasd[] entry contains one element.
364 dasd_parse(void)
366 int rc, i;
367 char *parsestring;
369 rc = 0;
370 for (i = 0; i < 256; i++) {
371 if (dasd[i] == NULL)
372 break;
373 parsestring = dasd[i];
374 /* loop over the comma separated list in the parsestring */
375 while (*parsestring) {
376 parsestring = dasd_parse_next_element(parsestring);
377 if(IS_ERR(parsestring)) {
378 rc = PTR_ERR(parsestring);
379 break;
382 if (rc) {
383 DBF_EVENT(DBF_ALERT, "%s", "invalid range found");
384 break;
387 return rc;
391 * Add a devmap for the device specified by busid. It is possible that
392 * the devmap already exists (dasd= parameter). The order of the devices
393 * added through this function will define the kdevs for the individual
394 * devices.
396 static struct dasd_devmap *
397 dasd_add_busid(char *bus_id, int features)
399 struct dasd_devmap *devmap, *new, *tmp;
400 int hash;
402 new = (struct dasd_devmap *)
403 kzalloc(sizeof(struct dasd_devmap), GFP_KERNEL);
404 if (!new)
405 return ERR_PTR(-ENOMEM);
406 spin_lock(&dasd_devmap_lock);
407 devmap = NULL;
408 hash = dasd_hash_busid(bus_id);
409 list_for_each_entry(tmp, &dasd_hashlists[hash], list)
410 if (strncmp(tmp->bus_id, bus_id, BUS_ID_SIZE) == 0) {
411 devmap = tmp;
412 break;
414 if (!devmap) {
415 /* This bus_id is new. */
416 new->devindex = dasd_max_devindex++;
417 strncpy(new->bus_id, bus_id, BUS_ID_SIZE);
418 new->features = features;
419 new->device = NULL;
420 list_add(&new->list, &dasd_hashlists[hash]);
421 devmap = new;
422 new = NULL;
424 spin_unlock(&dasd_devmap_lock);
425 kfree(new);
426 return devmap;
430 * Find devmap for device with given bus_id.
432 static struct dasd_devmap *
433 dasd_find_busid(char *bus_id)
435 struct dasd_devmap *devmap, *tmp;
436 int hash;
438 spin_lock(&dasd_devmap_lock);
439 devmap = ERR_PTR(-ENODEV);
440 hash = dasd_hash_busid(bus_id);
441 list_for_each_entry(tmp, &dasd_hashlists[hash], list) {
442 if (strncmp(tmp->bus_id, bus_id, BUS_ID_SIZE) == 0) {
443 devmap = tmp;
444 break;
447 spin_unlock(&dasd_devmap_lock);
448 return devmap;
452 * Check if busid has been added to the list of dasd ranges.
455 dasd_busid_known(char *bus_id)
457 return IS_ERR(dasd_find_busid(bus_id)) ? -ENOENT : 0;
461 * Forget all about the device numbers added so far.
462 * This may only be called at module unload or system shutdown.
464 static void
465 dasd_forget_ranges(void)
467 struct dasd_devmap *devmap, *n;
468 int i;
470 spin_lock(&dasd_devmap_lock);
471 for (i = 0; i < 256; i++) {
472 list_for_each_entry_safe(devmap, n, &dasd_hashlists[i], list) {
473 BUG_ON(devmap->device != NULL);
474 list_del(&devmap->list);
475 kfree(devmap);
478 spin_unlock(&dasd_devmap_lock);
482 * Find the device struct by its device index.
484 struct dasd_device *
485 dasd_device_from_devindex(int devindex)
487 struct dasd_devmap *devmap, *tmp;
488 struct dasd_device *device;
489 int i;
491 spin_lock(&dasd_devmap_lock);
492 devmap = NULL;
493 for (i = 0; (i < 256) && !devmap; i++)
494 list_for_each_entry(tmp, &dasd_hashlists[i], list)
495 if (tmp->devindex == devindex) {
496 /* Found the devmap for the device. */
497 devmap = tmp;
498 break;
500 if (devmap && devmap->device) {
501 device = devmap->device;
502 dasd_get_device(device);
503 } else
504 device = ERR_PTR(-ENODEV);
505 spin_unlock(&dasd_devmap_lock);
506 return device;
510 * Return devmap for cdev. If no devmap exists yet, create one and
511 * connect it to the cdev.
513 static struct dasd_devmap *
514 dasd_devmap_from_cdev(struct ccw_device *cdev)
516 struct dasd_devmap *devmap;
518 devmap = dasd_find_busid(cdev->dev.bus_id);
519 if (IS_ERR(devmap))
520 devmap = dasd_add_busid(cdev->dev.bus_id,
521 DASD_FEATURE_DEFAULT);
522 return devmap;
526 * Create a dasd device structure for cdev.
528 struct dasd_device *
529 dasd_create_device(struct ccw_device *cdev)
531 struct dasd_devmap *devmap;
532 struct dasd_device *device;
533 unsigned long flags;
534 int rc;
536 devmap = dasd_devmap_from_cdev(cdev);
537 if (IS_ERR(devmap))
538 return (void *) devmap;
540 device = dasd_alloc_device();
541 if (IS_ERR(device))
542 return device;
543 atomic_set(&device->ref_count, 3);
545 spin_lock(&dasd_devmap_lock);
546 if (!devmap->device) {
547 devmap->device = device;
548 device->devindex = devmap->devindex;
549 device->features = devmap->features;
550 get_device(&cdev->dev);
551 device->cdev = cdev;
552 rc = 0;
553 } else
554 /* Someone else was faster. */
555 rc = -EBUSY;
556 spin_unlock(&dasd_devmap_lock);
558 if (rc) {
559 dasd_free_device(device);
560 return ERR_PTR(rc);
563 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
564 cdev->dev.driver_data = device;
565 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
567 return device;
571 * Wait queue for dasd_delete_device waits.
573 static DECLARE_WAIT_QUEUE_HEAD(dasd_delete_wq);
576 * Remove a dasd device structure. The passed referenced
577 * is destroyed.
579 void
580 dasd_delete_device(struct dasd_device *device)
582 struct ccw_device *cdev;
583 struct dasd_devmap *devmap;
584 unsigned long flags;
586 /* First remove device pointer from devmap. */
587 devmap = dasd_find_busid(device->cdev->dev.bus_id);
588 BUG_ON(IS_ERR(devmap));
589 spin_lock(&dasd_devmap_lock);
590 if (devmap->device != device) {
591 spin_unlock(&dasd_devmap_lock);
592 dasd_put_device(device);
593 return;
595 devmap->device = NULL;
596 spin_unlock(&dasd_devmap_lock);
598 /* Disconnect dasd_device structure from ccw_device structure. */
599 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
600 device->cdev->dev.driver_data = NULL;
601 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
604 * Drop ref_count by 3, one for the devmap reference, one for
605 * the cdev reference and one for the passed reference.
607 atomic_sub(3, &device->ref_count);
609 /* Wait for reference counter to drop to zero. */
610 wait_event(dasd_delete_wq, atomic_read(&device->ref_count) == 0);
612 /* Disconnect dasd_device structure from ccw_device structure. */
613 cdev = device->cdev;
614 device->cdev = NULL;
616 /* Put ccw_device structure. */
617 put_device(&cdev->dev);
619 /* Now the device structure can be freed. */
620 dasd_free_device(device);
624 * Reference counter dropped to zero. Wake up waiter
625 * in dasd_delete_device.
627 void
628 dasd_put_device_wake(struct dasd_device *device)
630 wake_up(&dasd_delete_wq);
634 * Return dasd_device structure associated with cdev.
635 * This function needs to be called with the ccw device
636 * lock held. It can be used from interrupt context.
638 struct dasd_device *
639 dasd_device_from_cdev_locked(struct ccw_device *cdev)
641 struct dasd_device *device = cdev->dev.driver_data;
643 if (!device)
644 return ERR_PTR(-ENODEV);
645 dasd_get_device(device);
646 return device;
650 * Return dasd_device structure associated with cdev.
652 struct dasd_device *
653 dasd_device_from_cdev(struct ccw_device *cdev)
655 struct dasd_device *device;
656 unsigned long flags;
658 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
659 device = dasd_device_from_cdev_locked(cdev);
660 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
661 return device;
665 * SECTION: files in sysfs
669 * readonly controls the readonly status of a dasd
671 static ssize_t
672 dasd_ro_show(struct device *dev, struct device_attribute *attr, char *buf)
674 struct dasd_devmap *devmap;
675 int ro_flag;
677 devmap = dasd_find_busid(dev->bus_id);
678 if (!IS_ERR(devmap))
679 ro_flag = (devmap->features & DASD_FEATURE_READONLY) != 0;
680 else
681 ro_flag = (DASD_FEATURE_DEFAULT & DASD_FEATURE_READONLY) != 0;
682 return snprintf(buf, PAGE_SIZE, ro_flag ? "1\n" : "0\n");
685 static ssize_t
686 dasd_ro_store(struct device *dev, struct device_attribute *attr,
687 const char *buf, size_t count)
689 struct dasd_devmap *devmap;
690 int val;
691 char *endp;
693 devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
694 if (IS_ERR(devmap))
695 return PTR_ERR(devmap);
697 val = simple_strtoul(buf, &endp, 0);
698 if (((endp + 1) < (buf + count)) || (val > 1))
699 return -EINVAL;
701 spin_lock(&dasd_devmap_lock);
702 if (val)
703 devmap->features |= DASD_FEATURE_READONLY;
704 else
705 devmap->features &= ~DASD_FEATURE_READONLY;
706 if (devmap->device)
707 devmap->device->features = devmap->features;
708 if (devmap->device && devmap->device->block
709 && devmap->device->block->gdp)
710 set_disk_ro(devmap->device->block->gdp, val);
711 spin_unlock(&dasd_devmap_lock);
712 return count;
715 static DEVICE_ATTR(readonly, 0644, dasd_ro_show, dasd_ro_store);
717 * erplog controls the logging of ERP related data
718 * (e.g. failing channel programs).
720 static ssize_t
721 dasd_erplog_show(struct device *dev, struct device_attribute *attr, char *buf)
723 struct dasd_devmap *devmap;
724 int erplog;
726 devmap = dasd_find_busid(dev->bus_id);
727 if (!IS_ERR(devmap))
728 erplog = (devmap->features & DASD_FEATURE_ERPLOG) != 0;
729 else
730 erplog = (DASD_FEATURE_DEFAULT & DASD_FEATURE_ERPLOG) != 0;
731 return snprintf(buf, PAGE_SIZE, erplog ? "1\n" : "0\n");
734 static ssize_t
735 dasd_erplog_store(struct device *dev, struct device_attribute *attr,
736 const char *buf, size_t count)
738 struct dasd_devmap *devmap;
739 int val;
740 char *endp;
742 devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
743 if (IS_ERR(devmap))
744 return PTR_ERR(devmap);
746 val = simple_strtoul(buf, &endp, 0);
747 if (((endp + 1) < (buf + count)) || (val > 1))
748 return -EINVAL;
750 spin_lock(&dasd_devmap_lock);
751 if (val)
752 devmap->features |= DASD_FEATURE_ERPLOG;
753 else
754 devmap->features &= ~DASD_FEATURE_ERPLOG;
755 if (devmap->device)
756 devmap->device->features = devmap->features;
757 spin_unlock(&dasd_devmap_lock);
758 return count;
761 static DEVICE_ATTR(erplog, 0644, dasd_erplog_show, dasd_erplog_store);
764 * use_diag controls whether the driver should use diag rather than ssch
765 * to talk to the device
767 static ssize_t
768 dasd_use_diag_show(struct device *dev, struct device_attribute *attr, char *buf)
770 struct dasd_devmap *devmap;
771 int use_diag;
773 devmap = dasd_find_busid(dev->bus_id);
774 if (!IS_ERR(devmap))
775 use_diag = (devmap->features & DASD_FEATURE_USEDIAG) != 0;
776 else
777 use_diag = (DASD_FEATURE_DEFAULT & DASD_FEATURE_USEDIAG) != 0;
778 return sprintf(buf, use_diag ? "1\n" : "0\n");
781 static ssize_t
782 dasd_use_diag_store(struct device *dev, struct device_attribute *attr,
783 const char *buf, size_t count)
785 struct dasd_devmap *devmap;
786 ssize_t rc;
787 int val;
788 char *endp;
790 devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
791 if (IS_ERR(devmap))
792 return PTR_ERR(devmap);
794 val = simple_strtoul(buf, &endp, 0);
795 if (((endp + 1) < (buf + count)) || (val > 1))
796 return -EINVAL;
798 spin_lock(&dasd_devmap_lock);
799 /* Changing diag discipline flag is only allowed in offline state. */
800 rc = count;
801 if (!devmap->device) {
802 if (val)
803 devmap->features |= DASD_FEATURE_USEDIAG;
804 else
805 devmap->features &= ~DASD_FEATURE_USEDIAG;
806 } else
807 rc = -EPERM;
808 spin_unlock(&dasd_devmap_lock);
809 return rc;
812 static DEVICE_ATTR(use_diag, 0644, dasd_use_diag_show, dasd_use_diag_store);
814 static ssize_t
815 dasd_discipline_show(struct device *dev, struct device_attribute *attr,
816 char *buf)
818 struct dasd_device *device;
819 ssize_t len;
821 device = dasd_device_from_cdev(to_ccwdev(dev));
822 if (!IS_ERR(device) && device->discipline) {
823 len = snprintf(buf, PAGE_SIZE, "%s\n",
824 device->discipline->name);
825 dasd_put_device(device);
826 } else
827 len = snprintf(buf, PAGE_SIZE, "none\n");
828 return len;
831 static DEVICE_ATTR(discipline, 0444, dasd_discipline_show, NULL);
833 static ssize_t
834 dasd_device_status_show(struct device *dev, struct device_attribute *attr,
835 char *buf)
837 struct dasd_device *device;
838 ssize_t len;
840 device = dasd_device_from_cdev(to_ccwdev(dev));
841 if (!IS_ERR(device)) {
842 switch (device->state) {
843 case DASD_STATE_NEW:
844 len = snprintf(buf, PAGE_SIZE, "new\n");
845 break;
846 case DASD_STATE_KNOWN:
847 len = snprintf(buf, PAGE_SIZE, "detected\n");
848 break;
849 case DASD_STATE_BASIC:
850 len = snprintf(buf, PAGE_SIZE, "basic\n");
851 break;
852 case DASD_STATE_UNFMT:
853 len = snprintf(buf, PAGE_SIZE, "unformatted\n");
854 break;
855 case DASD_STATE_READY:
856 len = snprintf(buf, PAGE_SIZE, "ready\n");
857 break;
858 case DASD_STATE_ONLINE:
859 len = snprintf(buf, PAGE_SIZE, "online\n");
860 break;
861 default:
862 len = snprintf(buf, PAGE_SIZE, "no stat\n");
863 break;
865 dasd_put_device(device);
866 } else
867 len = snprintf(buf, PAGE_SIZE, "unknown\n");
868 return len;
871 static DEVICE_ATTR(status, 0444, dasd_device_status_show, NULL);
873 static ssize_t
874 dasd_alias_show(struct device *dev, struct device_attribute *attr, char *buf)
876 struct dasd_devmap *devmap;
877 int alias;
879 devmap = dasd_find_busid(dev->bus_id);
880 spin_lock(&dasd_devmap_lock);
881 if (IS_ERR(devmap) || strlen(devmap->uid.vendor) == 0) {
882 spin_unlock(&dasd_devmap_lock);
883 return sprintf(buf, "0\n");
885 if (devmap->uid.type == UA_BASE_PAV_ALIAS ||
886 devmap->uid.type == UA_HYPER_PAV_ALIAS)
887 alias = 1;
888 else
889 alias = 0;
890 spin_unlock(&dasd_devmap_lock);
891 return sprintf(buf, alias ? "1\n" : "0\n");
894 static DEVICE_ATTR(alias, 0444, dasd_alias_show, NULL);
896 static ssize_t
897 dasd_vendor_show(struct device *dev, struct device_attribute *attr, char *buf)
899 struct dasd_devmap *devmap;
900 char *vendor;
902 devmap = dasd_find_busid(dev->bus_id);
903 spin_lock(&dasd_devmap_lock);
904 if (!IS_ERR(devmap) && strlen(devmap->uid.vendor) > 0)
905 vendor = devmap->uid.vendor;
906 else
907 vendor = "";
908 spin_unlock(&dasd_devmap_lock);
910 return snprintf(buf, PAGE_SIZE, "%s\n", vendor);
913 static DEVICE_ATTR(vendor, 0444, dasd_vendor_show, NULL);
915 #define UID_STRLEN ( /* vendor */ 3 + 1 + /* serial */ 14 + 1 +\
916 /* SSID */ 4 + 1 + /* unit addr */ 2 + 1)
918 static ssize_t
919 dasd_uid_show(struct device *dev, struct device_attribute *attr, char *buf)
921 struct dasd_devmap *devmap;
922 char uid_string[UID_STRLEN];
923 char ua_string[3];
924 struct dasd_uid *uid;
926 devmap = dasd_find_busid(dev->bus_id);
927 spin_lock(&dasd_devmap_lock);
928 if (IS_ERR(devmap) || strlen(devmap->uid.vendor) == 0) {
929 spin_unlock(&dasd_devmap_lock);
930 return sprintf(buf, "\n");
932 uid = &devmap->uid;
933 switch (uid->type) {
934 case UA_BASE_DEVICE:
935 sprintf(ua_string, "%02x", uid->real_unit_addr);
936 break;
937 case UA_BASE_PAV_ALIAS:
938 sprintf(ua_string, "%02x", uid->base_unit_addr);
939 break;
940 case UA_HYPER_PAV_ALIAS:
941 sprintf(ua_string, "xx");
942 break;
943 default:
944 /* should not happen, treat like base device */
945 sprintf(ua_string, "%02x", uid->real_unit_addr);
946 break;
948 snprintf(uid_string, sizeof(uid_string), "%s.%s.%04x.%s",
949 uid->vendor, uid->serial, uid->ssid, ua_string);
950 spin_unlock(&dasd_devmap_lock);
951 return snprintf(buf, PAGE_SIZE, "%s\n", uid_string);
954 static DEVICE_ATTR(uid, 0444, dasd_uid_show, NULL);
957 * extended error-reporting
959 static ssize_t
960 dasd_eer_show(struct device *dev, struct device_attribute *attr, char *buf)
962 struct dasd_devmap *devmap;
963 int eer_flag;
965 devmap = dasd_find_busid(dev->bus_id);
966 if (!IS_ERR(devmap) && devmap->device)
967 eer_flag = dasd_eer_enabled(devmap->device);
968 else
969 eer_flag = 0;
970 return snprintf(buf, PAGE_SIZE, eer_flag ? "1\n" : "0\n");
973 static ssize_t
974 dasd_eer_store(struct device *dev, struct device_attribute *attr,
975 const char *buf, size_t count)
977 struct dasd_devmap *devmap;
978 int val, rc;
979 char *endp;
981 devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
982 if (IS_ERR(devmap))
983 return PTR_ERR(devmap);
984 if (!devmap->device)
985 return -ENODEV;
987 val = simple_strtoul(buf, &endp, 0);
988 if (((endp + 1) < (buf + count)) || (val > 1))
989 return -EINVAL;
991 if (val) {
992 rc = dasd_eer_enable(devmap->device);
993 if (rc)
994 return rc;
995 } else
996 dasd_eer_disable(devmap->device);
997 return count;
1000 static DEVICE_ATTR(eer_enabled, 0644, dasd_eer_show, dasd_eer_store);
1002 static struct attribute * dasd_attrs[] = {
1003 &dev_attr_readonly.attr,
1004 &dev_attr_discipline.attr,
1005 &dev_attr_status.attr,
1006 &dev_attr_alias.attr,
1007 &dev_attr_vendor.attr,
1008 &dev_attr_uid.attr,
1009 &dev_attr_use_diag.attr,
1010 &dev_attr_eer_enabled.attr,
1011 &dev_attr_erplog.attr,
1012 NULL,
1015 static struct attribute_group dasd_attr_group = {
1016 .attrs = dasd_attrs,
1020 * Return copy of the device unique identifier.
1023 dasd_get_uid(struct ccw_device *cdev, struct dasd_uid *uid)
1025 struct dasd_devmap *devmap;
1027 devmap = dasd_find_busid(cdev->dev.bus_id);
1028 if (IS_ERR(devmap))
1029 return PTR_ERR(devmap);
1030 spin_lock(&dasd_devmap_lock);
1031 *uid = devmap->uid;
1032 spin_unlock(&dasd_devmap_lock);
1033 return 0;
1037 * Register the given device unique identifier into devmap struct.
1038 * In addition check if the related storage server subsystem ID is already
1039 * contained in the dasd_server_ssid_list. If subsystem ID is not contained,
1040 * create new entry.
1041 * Return 0 if server was already in serverlist,
1042 * 1 if the server was added successful
1043 * <0 in case of error.
1046 dasd_set_uid(struct ccw_device *cdev, struct dasd_uid *uid)
1048 struct dasd_devmap *devmap;
1050 devmap = dasd_find_busid(cdev->dev.bus_id);
1051 if (IS_ERR(devmap))
1052 return PTR_ERR(devmap);
1054 spin_lock(&dasd_devmap_lock);
1055 devmap->uid = *uid;
1056 spin_unlock(&dasd_devmap_lock);
1058 return 0;
1060 EXPORT_SYMBOL_GPL(dasd_set_uid);
1063 * Return value of the specified feature.
1066 dasd_get_feature(struct ccw_device *cdev, int feature)
1068 struct dasd_devmap *devmap;
1070 devmap = dasd_find_busid(cdev->dev.bus_id);
1071 if (IS_ERR(devmap))
1072 return PTR_ERR(devmap);
1074 return ((devmap->features & feature) != 0);
1078 * Set / reset given feature.
1079 * Flag indicates wether to set (!=0) or the reset (=0) the feature.
1082 dasd_set_feature(struct ccw_device *cdev, int feature, int flag)
1084 struct dasd_devmap *devmap;
1086 devmap = dasd_find_busid(cdev->dev.bus_id);
1087 if (IS_ERR(devmap))
1088 return PTR_ERR(devmap);
1090 spin_lock(&dasd_devmap_lock);
1091 if (flag)
1092 devmap->features |= feature;
1093 else
1094 devmap->features &= ~feature;
1095 if (devmap->device)
1096 devmap->device->features = devmap->features;
1097 spin_unlock(&dasd_devmap_lock);
1098 return 0;
1103 dasd_add_sysfs_files(struct ccw_device *cdev)
1105 return sysfs_create_group(&cdev->dev.kobj, &dasd_attr_group);
1108 void
1109 dasd_remove_sysfs_files(struct ccw_device *cdev)
1111 sysfs_remove_group(&cdev->dev.kobj, &dasd_attr_group);
1116 dasd_devmap_init(void)
1118 int i;
1120 /* Initialize devmap structures. */
1121 dasd_max_devindex = 0;
1122 for (i = 0; i < 256; i++)
1123 INIT_LIST_HEAD(&dasd_hashlists[i]);
1124 return 0;
1127 void
1128 dasd_devmap_exit(void)
1130 dasd_forget_ranges();