xref: /openbmc/linux/drivers/s390/block/dasd_devmap.c (revision 74eda70a)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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  * Copyright IBM Corp. 1999,2001
9  *
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.
13  *
14  */
15 
16 #define KMSG_COMPONENT "dasd"
17 
18 #include <linux/ctype.h>
19 #include <linux/init.h>
20 #include <linux/module.h>
21 #include <linux/slab.h>
22 
23 #include <asm/debug.h>
24 #include <linux/uaccess.h>
25 #include <asm/ipl.h>
26 
27 /* This is ugly... */
28 #define PRINTK_HEADER "dasd_devmap:"
29 #define DASD_MAX_PARAMS 256
30 
31 #include "dasd_int.h"
32 
33 struct kmem_cache *dasd_page_cache;
34 EXPORT_SYMBOL_GPL(dasd_page_cache);
35 
36 /*
37  * dasd_devmap_t is used to store the features and the relation
38  * between device number and device index. To find a dasd_devmap_t
39  * that corresponds to a device number of a device index each
40  * dasd_devmap_t is added to two linked lists, one to search by
41  * the device number and one to search by the device index. As
42  * soon as big minor numbers are available the device index list
43  * can be removed since the device number will then be identical
44  * to the device index.
45  */
46 struct dasd_devmap {
47 	struct list_head list;
48 	char bus_id[DASD_BUS_ID_SIZE];
49         unsigned int devindex;
50         unsigned short features;
51 	struct dasd_device *device;
52 	struct dasd_copy_relation *copy;
53 	unsigned int aq_mask;
54 };
55 
56 /*
57  * Parameter parsing functions for dasd= parameter. The syntax is:
58  *   <devno>		: (0x)?[0-9a-fA-F]+
59  *   <busid>		: [0-0a-f]\.[0-9a-f]\.(0x)?[0-9a-fA-F]+
60  *   <feature>		: ro
61  *   <feature_list>	: \(<feature>(:<feature>)*\)
62  *   <devno-range>	: <devno>(-<devno>)?<feature_list>?
63  *   <busid-range>	: <busid>(-<busid>)?<feature_list>?
64  *   <devices>		: <devno-range>|<busid-range>
65  *   <dasd_module>	: dasd_diag_mod|dasd_eckd_mod|dasd_fba_mod
66  *
67  *   <dasd>		: autodetect|probeonly|<devices>(,<devices>)*
68  */
69 
70 int dasd_probeonly =  0;	/* is true, when probeonly mode is active */
71 int dasd_autodetect = 0;	/* is true, when autodetection is active */
72 int dasd_nopav = 0;		/* is true, when PAV is disabled */
73 EXPORT_SYMBOL_GPL(dasd_nopav);
74 int dasd_nofcx;			/* disable High Performance Ficon */
75 EXPORT_SYMBOL_GPL(dasd_nofcx);
76 
77 /*
78  * char *dasd[] is intended to hold the ranges supplied by the dasd= statement
79  * it is named 'dasd' to directly be filled by insmod with the comma separated
80  * strings when running as a module.
81  */
82 static char *dasd[DASD_MAX_PARAMS];
83 module_param_array(dasd, charp, NULL, S_IRUGO);
84 
85 /*
86  * Single spinlock to protect devmap and servermap structures and lists.
87  */
88 static DEFINE_SPINLOCK(dasd_devmap_lock);
89 
90 /*
91  * Hash lists for devmap structures.
92  */
93 static struct list_head dasd_hashlists[256];
94 int dasd_max_devindex;
95 
96 static struct dasd_devmap *dasd_add_busid(const char *, int);
97 
98 static inline int
99 dasd_hash_busid(const char *bus_id)
100 {
101 	int hash, i;
102 
103 	hash = 0;
104 	for (i = 0; (i < DASD_BUS_ID_SIZE) && *bus_id; i++, bus_id++)
105 		hash += *bus_id;
106 	return hash & 0xff;
107 }
108 
109 #ifndef MODULE
110 static int __init dasd_call_setup(char *opt)
111 {
112 	static int i __initdata;
113 	char *tmp;
114 
115 	while (i < DASD_MAX_PARAMS) {
116 		tmp = strsep(&opt, ",");
117 		if (!tmp)
118 			break;
119 
120 		dasd[i++] = tmp;
121 	}
122 
123 	return 1;
124 }
125 
126 __setup ("dasd=", dasd_call_setup);
127 #endif	/* #ifndef MODULE */
128 
129 #define	DASD_IPLDEV	"ipldev"
130 
131 /*
132  * Read a device busid/devno from a string.
133  */
134 static int dasd_busid(char *str, int *id0, int *id1, int *devno)
135 {
136 	unsigned int val;
137 	char *tok;
138 
139 	/* Interpret ipldev busid */
140 	if (strncmp(DASD_IPLDEV, str, strlen(DASD_IPLDEV)) == 0) {
141 		if (ipl_info.type != IPL_TYPE_CCW) {
142 			pr_err("The IPL device is not a CCW device\n");
143 			return -EINVAL;
144 		}
145 		*id0 = 0;
146 		*id1 = ipl_info.data.ccw.dev_id.ssid;
147 		*devno = ipl_info.data.ccw.dev_id.devno;
148 
149 		return 0;
150 	}
151 
152 	/* Old style 0xXXXX or XXXX */
153 	if (!kstrtouint(str, 16, &val)) {
154 		*id0 = *id1 = 0;
155 		if (val > 0xffff)
156 			return -EINVAL;
157 		*devno = val;
158 		return 0;
159 	}
160 
161 	/* New style x.y.z busid */
162 	tok = strsep(&str, ".");
163 	if (kstrtouint(tok, 16, &val) || val > 0xff)
164 		return -EINVAL;
165 	*id0 = val;
166 
167 	tok = strsep(&str, ".");
168 	if (kstrtouint(tok, 16, &val) || val > 0xff)
169 		return -EINVAL;
170 	*id1 = val;
171 
172 	tok = strsep(&str, ".");
173 	if (kstrtouint(tok, 16, &val) || val > 0xffff)
174 		return -EINVAL;
175 	*devno = val;
176 
177 	return 0;
178 }
179 
180 /*
181  * Read colon separated list of dasd features.
182  */
183 static int __init dasd_feature_list(char *str)
184 {
185 	int features, len, rc;
186 
187 	features = 0;
188 	rc = 0;
189 
190 	if (!str)
191 		return DASD_FEATURE_DEFAULT;
192 
193 	while (1) {
194 		for (len = 0;
195 		     str[len] && str[len] != ':' && str[len] != ')'; len++);
196 		if (len == 2 && !strncmp(str, "ro", 2))
197 			features |= DASD_FEATURE_READONLY;
198 		else if (len == 4 && !strncmp(str, "diag", 4))
199 			features |= DASD_FEATURE_USEDIAG;
200 		else if (len == 3 && !strncmp(str, "raw", 3))
201 			features |= DASD_FEATURE_USERAW;
202 		else if (len == 6 && !strncmp(str, "erplog", 6))
203 			features |= DASD_FEATURE_ERPLOG;
204 		else if (len == 8 && !strncmp(str, "failfast", 8))
205 			features |= DASD_FEATURE_FAILFAST;
206 		else {
207 			pr_warn("%.*s is not a supported device option\n",
208 				len, str);
209 			rc = -EINVAL;
210 		}
211 		str += len;
212 		if (*str != ':')
213 			break;
214 		str++;
215 	}
216 
217 	return rc ? : features;
218 }
219 
220 /*
221  * Try to match the first element on the comma separated parse string
222  * with one of the known keywords. If a keyword is found, take the approprate
223  * action and return a pointer to the residual string. If the first element
224  * could not be matched to any keyword then return an error code.
225  */
226 static int __init dasd_parse_keyword(char *keyword)
227 {
228 	int length = strlen(keyword);
229 
230 	if (strncmp("autodetect", keyword, length) == 0) {
231 		dasd_autodetect = 1;
232 		pr_info("The autodetection mode has been activated\n");
233 		return 0;
234         }
235 	if (strncmp("probeonly", keyword, length) == 0) {
236 		dasd_probeonly = 1;
237 		pr_info("The probeonly mode has been activated\n");
238 		return 0;
239         }
240 	if (strncmp("nopav", keyword, length) == 0) {
241 		if (MACHINE_IS_VM)
242 			pr_info("'nopav' is not supported on z/VM\n");
243 		else {
244 			dasd_nopav = 1;
245 			pr_info("PAV support has be deactivated\n");
246 		}
247 		return 0;
248 	}
249 	if (strncmp("nofcx", keyword, length) == 0) {
250 		dasd_nofcx = 1;
251 		pr_info("High Performance FICON support has been "
252 			"deactivated\n");
253 		return 0;
254 	}
255 	if (strncmp("fixedbuffers", keyword, length) == 0) {
256 		if (dasd_page_cache)
257 			return 0;
258 		dasd_page_cache =
259 			kmem_cache_create("dasd_page_cache", PAGE_SIZE,
260 					  PAGE_SIZE, SLAB_CACHE_DMA,
261 					  NULL);
262 		if (!dasd_page_cache)
263 			DBF_EVENT(DBF_WARNING, "%s", "Failed to create slab, "
264 				"fixed buffer mode disabled.");
265 		else
266 			DBF_EVENT(DBF_INFO, "%s",
267 				 "turning on fixed buffer mode");
268 		return 0;
269 	}
270 
271 	return -EINVAL;
272 }
273 
274 /*
275  * Split a string of a device range into its pieces and return the from, to, and
276  * feature parts separately.
277  * e.g.:
278  * 0.0.1234-0.0.5678(ro:erplog) -> from: 0.0.1234 to: 0.0.5678 features: ro:erplog
279  * 0.0.8765(raw) -> from: 0.0.8765 to: null features: raw
280  * 0x4321 -> from: 0x4321 to: null features: null
281  */
282 static int __init dasd_evaluate_range_param(char *range, char **from_str,
283 					    char **to_str, char **features_str)
284 {
285 	int rc = 0;
286 
287 	/* Do we have a range or a single device? */
288 	if (strchr(range, '-')) {
289 		*from_str = strsep(&range, "-");
290 		*to_str = strsep(&range, "(");
291 		*features_str = strsep(&range, ")");
292 	} else {
293 		*from_str = strsep(&range, "(");
294 		*features_str = strsep(&range, ")");
295 	}
296 
297 	if (*features_str && !range) {
298 		pr_warn("A closing parenthesis ')' is missing in the dasd= parameter\n");
299 		rc = -EINVAL;
300 	}
301 
302 	return rc;
303 }
304 
305 /*
306  * Try to interprete the range string as a device number or a range of devices.
307  * If the interpretation is successful, create the matching dasd_devmap entries.
308  * If interpretation fails or in case of an error, return an error code.
309  */
310 static int __init dasd_parse_range(const char *range)
311 {
312 	struct dasd_devmap *devmap;
313 	int from, from_id0, from_id1;
314 	int to, to_id0, to_id1;
315 	int features;
316 	char bus_id[DASD_BUS_ID_SIZE + 1];
317 	char *features_str = NULL;
318 	char *from_str = NULL;
319 	char *to_str = NULL;
320 	int rc = 0;
321 	char *tmp;
322 
323 	tmp = kstrdup(range, GFP_KERNEL);
324 	if (!tmp)
325 		return -ENOMEM;
326 
327 	if (dasd_evaluate_range_param(tmp, &from_str, &to_str, &features_str)) {
328 		rc = -EINVAL;
329 		goto out;
330 	}
331 
332 	if (dasd_busid(from_str, &from_id0, &from_id1, &from)) {
333 		rc = -EINVAL;
334 		goto out;
335 	}
336 
337 	to = from;
338 	to_id0 = from_id0;
339 	to_id1 = from_id1;
340 	if (to_str) {
341 		if (dasd_busid(to_str, &to_id0, &to_id1, &to)) {
342 			rc = -EINVAL;
343 			goto out;
344 		}
345 		if (from_id0 != to_id0 || from_id1 != to_id1 || from > to) {
346 			pr_err("%s is not a valid device range\n", range);
347 			rc = -EINVAL;
348 			goto out;
349 		}
350 	}
351 
352 	features = dasd_feature_list(features_str);
353 	if (features < 0) {
354 		rc = -EINVAL;
355 		goto out;
356 	}
357 	/* each device in dasd= parameter should be set initially online */
358 	features |= DASD_FEATURE_INITIAL_ONLINE;
359 	while (from <= to) {
360 		sprintf(bus_id, "%01x.%01x.%04x", from_id0, from_id1, from++);
361 		devmap = dasd_add_busid(bus_id, features);
362 		if (IS_ERR(devmap)) {
363 			rc = PTR_ERR(devmap);
364 			goto out;
365 		}
366 	}
367 
368 out:
369 	kfree(tmp);
370 
371 	return rc;
372 }
373 
374 /*
375  * Parse parameters stored in dasd[]
376  * The 'dasd=...' parameter allows to specify a comma separated list of
377  * keywords and device ranges. The parameters in that list will be stored as
378  * separate elementes in dasd[].
379  */
380 int __init dasd_parse(void)
381 {
382 	int rc, i;
383 	char *cur;
384 
385 	rc = 0;
386 	for (i = 0; i < DASD_MAX_PARAMS; i++) {
387 		cur = dasd[i];
388 		if (!cur)
389 			break;
390 		if (*cur == '\0')
391 			continue;
392 
393 		rc = dasd_parse_keyword(cur);
394 		if (rc)
395 			rc = dasd_parse_range(cur);
396 
397 		if (rc)
398 			break;
399 	}
400 
401 	return rc;
402 }
403 
404 /*
405  * Add a devmap for the device specified by busid. It is possible that
406  * the devmap already exists (dasd= parameter). The order of the devices
407  * added through this function will define the kdevs for the individual
408  * devices.
409  */
410 static struct dasd_devmap *
411 dasd_add_busid(const char *bus_id, int features)
412 {
413 	struct dasd_devmap *devmap, *new, *tmp;
414 	int hash;
415 
416 	new = kzalloc(sizeof(struct dasd_devmap), GFP_KERNEL);
417 	if (!new)
418 		return ERR_PTR(-ENOMEM);
419 	spin_lock(&dasd_devmap_lock);
420 	devmap = NULL;
421 	hash = dasd_hash_busid(bus_id);
422 	list_for_each_entry(tmp, &dasd_hashlists[hash], list)
423 		if (strncmp(tmp->bus_id, bus_id, DASD_BUS_ID_SIZE) == 0) {
424 			devmap = tmp;
425 			break;
426 		}
427 	if (!devmap) {
428 		/* This bus_id is new. */
429 		new->devindex = dasd_max_devindex++;
430 		strscpy(new->bus_id, bus_id, DASD_BUS_ID_SIZE);
431 		new->features = features;
432 		new->device = NULL;
433 		list_add(&new->list, &dasd_hashlists[hash]);
434 		devmap = new;
435 		new = NULL;
436 	}
437 	spin_unlock(&dasd_devmap_lock);
438 	kfree(new);
439 	return devmap;
440 }
441 
442 static struct dasd_devmap *
443 dasd_find_busid_locked(const char *bus_id)
444 {
445 	struct dasd_devmap *devmap, *tmp;
446 	int hash;
447 
448 	devmap = ERR_PTR(-ENODEV);
449 	hash = dasd_hash_busid(bus_id);
450 	list_for_each_entry(tmp, &dasd_hashlists[hash], list) {
451 		if (strncmp(tmp->bus_id, bus_id, DASD_BUS_ID_SIZE) == 0) {
452 			devmap = tmp;
453 			break;
454 		}
455 	}
456 	return devmap;
457 }
458 
459 /*
460  * Find devmap for device with given bus_id.
461  */
462 static struct dasd_devmap *
463 dasd_find_busid(const char *bus_id)
464 {
465 	struct dasd_devmap *devmap;
466 
467 	spin_lock(&dasd_devmap_lock);
468 	devmap = dasd_find_busid_locked(bus_id);
469 	spin_unlock(&dasd_devmap_lock);
470 	return devmap;
471 }
472 
473 /*
474  * Check if busid has been added to the list of dasd ranges.
475  */
476 int
477 dasd_busid_known(const char *bus_id)
478 {
479 	return IS_ERR(dasd_find_busid(bus_id)) ? -ENOENT : 0;
480 }
481 
482 /*
483  * Forget all about the device numbers added so far.
484  * This may only be called at module unload or system shutdown.
485  */
486 static void
487 dasd_forget_ranges(void)
488 {
489 	struct dasd_devmap *devmap, *n;
490 	int i;
491 
492 	spin_lock(&dasd_devmap_lock);
493 	for (i = 0; i < 256; i++) {
494 		list_for_each_entry_safe(devmap, n, &dasd_hashlists[i], list) {
495 			BUG_ON(devmap->device != NULL);
496 			list_del(&devmap->list);
497 			kfree(devmap);
498 		}
499 	}
500 	spin_unlock(&dasd_devmap_lock);
501 }
502 
503 /*
504  * Find the device struct by its device index.
505  */
506 struct dasd_device *
507 dasd_device_from_devindex(int devindex)
508 {
509 	struct dasd_devmap *devmap, *tmp;
510 	struct dasd_device *device;
511 	int i;
512 
513 	spin_lock(&dasd_devmap_lock);
514 	devmap = NULL;
515 	for (i = 0; (i < 256) && !devmap; i++)
516 		list_for_each_entry(tmp, &dasd_hashlists[i], list)
517 			if (tmp->devindex == devindex) {
518 				/* Found the devmap for the device. */
519 				devmap = tmp;
520 				break;
521 			}
522 	if (devmap && devmap->device) {
523 		device = devmap->device;
524 		dasd_get_device(device);
525 	} else
526 		device = ERR_PTR(-ENODEV);
527 	spin_unlock(&dasd_devmap_lock);
528 	return device;
529 }
530 
531 /*
532  * Return devmap for cdev. If no devmap exists yet, create one and
533  * connect it to the cdev.
534  */
535 static struct dasd_devmap *
536 dasd_devmap_from_cdev(struct ccw_device *cdev)
537 {
538 	struct dasd_devmap *devmap;
539 
540 	devmap = dasd_find_busid(dev_name(&cdev->dev));
541 	if (IS_ERR(devmap))
542 		devmap = dasd_add_busid(dev_name(&cdev->dev),
543 					DASD_FEATURE_DEFAULT);
544 	return devmap;
545 }
546 
547 /*
548  * Create a dasd device structure for cdev.
549  */
550 struct dasd_device *
551 dasd_create_device(struct ccw_device *cdev)
552 {
553 	struct dasd_devmap *devmap;
554 	struct dasd_device *device;
555 	unsigned long flags;
556 	int rc;
557 
558 	devmap = dasd_devmap_from_cdev(cdev);
559 	if (IS_ERR(devmap))
560 		return (void *) devmap;
561 
562 	device = dasd_alloc_device();
563 	if (IS_ERR(device))
564 		return device;
565 	atomic_set(&device->ref_count, 3);
566 
567 	spin_lock(&dasd_devmap_lock);
568 	if (!devmap->device) {
569 		devmap->device = device;
570 		device->devindex = devmap->devindex;
571 		device->features = devmap->features;
572 		get_device(&cdev->dev);
573 		device->cdev = cdev;
574 		rc = 0;
575 	} else
576 		/* Someone else was faster. */
577 		rc = -EBUSY;
578 	spin_unlock(&dasd_devmap_lock);
579 
580 	if (rc) {
581 		dasd_free_device(device);
582 		return ERR_PTR(rc);
583 	}
584 
585 	spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
586 	dev_set_drvdata(&cdev->dev, device);
587 	spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
588 
589 	device->paths_info = kset_create_and_add("paths_info", NULL,
590 						 &device->cdev->dev.kobj);
591 	if (!device->paths_info)
592 		dev_warn(&cdev->dev, "Could not create paths_info kset\n");
593 
594 	return device;
595 }
596 
597 /*
598  * allocate a PPRC data structure and call the discipline function to fill
599  */
600 static int dasd_devmap_get_pprc_status(struct dasd_device *device,
601 				       struct dasd_pprc_data_sc4 **data)
602 {
603 	struct dasd_pprc_data_sc4 *temp;
604 
605 	if (!device->discipline || !device->discipline->pprc_status) {
606 		dev_warn(&device->cdev->dev, "Unable to query copy relation status\n");
607 		return -EOPNOTSUPP;
608 	}
609 	temp = kzalloc(sizeof(*temp), GFP_KERNEL);
610 	if (!temp)
611 		return -ENOMEM;
612 
613 	/* get PPRC information from storage */
614 	if (device->discipline->pprc_status(device, temp)) {
615 		dev_warn(&device->cdev->dev, "Error during copy relation status query\n");
616 		kfree(temp);
617 		return -EINVAL;
618 	}
619 	*data = temp;
620 
621 	return 0;
622 }
623 
624 /*
625  * find an entry in a PPRC device_info array by a given UID
626  * depending on the primary/secondary state of the device it has to be
627  * matched with the respective fields
628  */
629 static int dasd_devmap_entry_from_pprc_data(struct dasd_pprc_data_sc4 *data,
630 					    struct dasd_uid uid,
631 					    bool primary)
632 {
633 	int i;
634 
635 	for (i = 0; i < DASD_CP_ENTRIES; i++) {
636 		if (primary) {
637 			if (data->dev_info[i].prim_cu_ssid == uid.ssid &&
638 			    data->dev_info[i].primary == uid.real_unit_addr)
639 				return i;
640 		} else {
641 			if (data->dev_info[i].sec_cu_ssid == uid.ssid &&
642 			    data->dev_info[i].secondary == uid.real_unit_addr)
643 				return i;
644 		}
645 	}
646 	return -1;
647 }
648 
649 /*
650  * check the consistency of a specified copy relation by checking
651  * the following things:
652  *
653  *   - is the given device part of a copy pair setup
654  *   - does the state of the device match the state in the PPRC status data
655  *   - does the device UID match with the UID in the PPRC status data
656  *   - to prevent misrouted IO check if the given device is present in all
657  *     related PPRC status data
658  */
659 static int dasd_devmap_check_copy_relation(struct dasd_device *device,
660 					   struct dasd_copy_entry *entry,
661 					   struct dasd_pprc_data_sc4 *data,
662 					   struct dasd_copy_relation *copy)
663 {
664 	struct dasd_pprc_data_sc4 *tmp_dat;
665 	struct dasd_device *tmp_dev;
666 	struct dasd_uid uid;
667 	int i, j;
668 
669 	if (!device->discipline || !device->discipline->get_uid ||
670 	    device->discipline->get_uid(device, &uid))
671 		return 1;
672 
673 	i = dasd_devmap_entry_from_pprc_data(data, uid, entry->primary);
674 	if (i < 0) {
675 		dev_warn(&device->cdev->dev, "Device not part of a copy relation\n");
676 		return 1;
677 	}
678 
679 	/* double check which role the current device has */
680 	if (entry->primary) {
681 		if (data->dev_info[i].flags & 0x80) {
682 			dev_warn(&device->cdev->dev, "Copy pair secondary is setup as primary\n");
683 			return 1;
684 		}
685 		if (data->dev_info[i].prim_cu_ssid != uid.ssid ||
686 		    data->dev_info[i].primary != uid.real_unit_addr) {
687 			dev_warn(&device->cdev->dev,
688 				 "Primary device %s does not match copy pair status primary device %04x\n",
689 				 dev_name(&device->cdev->dev),
690 				 data->dev_info[i].prim_cu_ssid |
691 				 data->dev_info[i].primary);
692 			return 1;
693 		}
694 	} else {
695 		if (!(data->dev_info[i].flags & 0x80)) {
696 			dev_warn(&device->cdev->dev, "Copy pair primary is setup as secondary\n");
697 			return 1;
698 		}
699 		if (data->dev_info[i].sec_cu_ssid != uid.ssid ||
700 		    data->dev_info[i].secondary != uid.real_unit_addr) {
701 			dev_warn(&device->cdev->dev,
702 				 "Secondary device %s does not match copy pair status secondary device %04x\n",
703 				 dev_name(&device->cdev->dev),
704 				 data->dev_info[i].sec_cu_ssid |
705 				 data->dev_info[i].secondary);
706 			return 1;
707 		}
708 	}
709 
710 	/*
711 	 * the current device has to be part of the copy relation of all
712 	 * entries to prevent misrouted IO to another copy pair
713 	 */
714 	for (j = 0; j < DASD_CP_ENTRIES; j++) {
715 		if (entry == &copy->entry[j])
716 			tmp_dev = device;
717 		else
718 			tmp_dev = copy->entry[j].device;
719 
720 		if (!tmp_dev)
721 			continue;
722 
723 		if (dasd_devmap_get_pprc_status(tmp_dev, &tmp_dat))
724 			return 1;
725 
726 		if (dasd_devmap_entry_from_pprc_data(tmp_dat, uid, entry->primary) < 0) {
727 			dev_warn(&tmp_dev->cdev->dev,
728 				 "Copy pair relation does not contain device: %s\n",
729 				 dev_name(&device->cdev->dev));
730 			kfree(tmp_dat);
731 			return 1;
732 		}
733 		kfree(tmp_dat);
734 	}
735 	return 0;
736 }
737 
738 /* delete device from copy relation entry */
739 static void dasd_devmap_delete_copy_relation_device(struct dasd_device *device)
740 {
741 	struct dasd_copy_relation *copy;
742 	int i;
743 
744 	if (!device->copy)
745 		return;
746 
747 	copy = device->copy;
748 	for (i = 0; i < DASD_CP_ENTRIES; i++) {
749 		if (copy->entry[i].device == device)
750 			copy->entry[i].device = NULL;
751 	}
752 	dasd_put_device(device);
753 	device->copy = NULL;
754 }
755 
756 /*
757  * read all required information for a copy relation setup and setup the device
758  * accordingly
759  */
760 int dasd_devmap_set_device_copy_relation(struct ccw_device *cdev,
761 					 bool pprc_enabled)
762 {
763 	struct dasd_pprc_data_sc4 *data = NULL;
764 	struct dasd_copy_entry *entry = NULL;
765 	struct dasd_copy_relation *copy;
766 	struct dasd_devmap *devmap;
767 	struct dasd_device *device;
768 	int i, rc = 0;
769 
770 	devmap = dasd_devmap_from_cdev(cdev);
771 	if (IS_ERR(devmap))
772 		return PTR_ERR(devmap);
773 
774 	device = devmap->device;
775 	if (!device)
776 		return -ENODEV;
777 
778 	copy = devmap->copy;
779 	/* no copy pair setup for this device */
780 	if (!copy)
781 		goto out;
782 
783 	rc = dasd_devmap_get_pprc_status(device, &data);
784 	if (rc)
785 		return rc;
786 
787 	/* print error if PPRC is requested but not enabled on storage server */
788 	if (!pprc_enabled) {
789 		dev_err(&cdev->dev, "Copy relation not enabled on storage server\n");
790 		rc = -EINVAL;
791 		goto out;
792 	}
793 
794 	if (!data->dev_info[0].state) {
795 		dev_warn(&device->cdev->dev, "Copy pair setup requested for device not in copy relation\n");
796 		rc = -EINVAL;
797 		goto out;
798 	}
799 	/* find entry */
800 	for (i = 0; i < DASD_CP_ENTRIES; i++) {
801 		if (copy->entry[i].configured &&
802 		    strncmp(dev_name(&cdev->dev),
803 			    copy->entry[i].busid, DASD_BUS_ID_SIZE) == 0) {
804 			entry = &copy->entry[i];
805 			break;
806 		}
807 	}
808 	if (!entry) {
809 		dev_warn(&device->cdev->dev, "Copy relation entry not found\n");
810 		rc = -EINVAL;
811 		goto out;
812 	}
813 	/* check if the copy relation is valid */
814 	if (dasd_devmap_check_copy_relation(device, entry, data, copy)) {
815 		dev_warn(&device->cdev->dev, "Copy relation faulty\n");
816 		rc = -EINVAL;
817 		goto out;
818 	}
819 
820 	dasd_get_device(device);
821 	copy->entry[i].device = device;
822 	device->copy = copy;
823 out:
824 	kfree(data);
825 	return rc;
826 }
827 EXPORT_SYMBOL_GPL(dasd_devmap_set_device_copy_relation);
828 
829 /*
830  * Wait queue for dasd_delete_device waits.
831  */
832 static DECLARE_WAIT_QUEUE_HEAD(dasd_delete_wq);
833 
834 /*
835  * Remove a dasd device structure. The passed referenced
836  * is destroyed.
837  */
838 void
839 dasd_delete_device(struct dasd_device *device)
840 {
841 	struct ccw_device *cdev;
842 	struct dasd_devmap *devmap;
843 	unsigned long flags;
844 
845 	/* First remove device pointer from devmap. */
846 	devmap = dasd_find_busid(dev_name(&device->cdev->dev));
847 	BUG_ON(IS_ERR(devmap));
848 	spin_lock(&dasd_devmap_lock);
849 	if (devmap->device != device) {
850 		spin_unlock(&dasd_devmap_lock);
851 		dasd_put_device(device);
852 		return;
853 	}
854 	devmap->device = NULL;
855 	spin_unlock(&dasd_devmap_lock);
856 
857 	/* Disconnect dasd_device structure from ccw_device structure. */
858 	spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
859 	dev_set_drvdata(&device->cdev->dev, NULL);
860 	spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
861 
862 	/* Removve copy relation */
863 	dasd_devmap_delete_copy_relation_device(device);
864 	/*
865 	 * Drop ref_count by 3, one for the devmap reference, one for
866 	 * the cdev reference and one for the passed reference.
867 	 */
868 	atomic_sub(3, &device->ref_count);
869 
870 	/* Wait for reference counter to drop to zero. */
871 	wait_event(dasd_delete_wq, atomic_read(&device->ref_count) == 0);
872 
873 	dasd_generic_free_discipline(device);
874 
875 	kset_unregister(device->paths_info);
876 
877 	/* Disconnect dasd_device structure from ccw_device structure. */
878 	cdev = device->cdev;
879 	device->cdev = NULL;
880 
881 	/* Put ccw_device structure. */
882 	put_device(&cdev->dev);
883 
884 	/* Now the device structure can be freed. */
885 	dasd_free_device(device);
886 }
887 
888 /*
889  * Reference counter dropped to zero. Wake up waiter
890  * in dasd_delete_device.
891  */
892 void
893 dasd_put_device_wake(struct dasd_device *device)
894 {
895 	wake_up(&dasd_delete_wq);
896 }
897 EXPORT_SYMBOL_GPL(dasd_put_device_wake);
898 
899 /*
900  * Return dasd_device structure associated with cdev.
901  * This function needs to be called with the ccw device
902  * lock held. It can be used from interrupt context.
903  */
904 struct dasd_device *
905 dasd_device_from_cdev_locked(struct ccw_device *cdev)
906 {
907 	struct dasd_device *device = dev_get_drvdata(&cdev->dev);
908 
909 	if (!device)
910 		return ERR_PTR(-ENODEV);
911 	dasd_get_device(device);
912 	return device;
913 }
914 
915 /*
916  * Return dasd_device structure associated with cdev.
917  */
918 struct dasd_device *
919 dasd_device_from_cdev(struct ccw_device *cdev)
920 {
921 	struct dasd_device *device;
922 	unsigned long flags;
923 
924 	spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
925 	device = dasd_device_from_cdev_locked(cdev);
926 	spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
927 	return device;
928 }
929 
930 void dasd_add_link_to_gendisk(struct gendisk *gdp, struct dasd_device *device)
931 {
932 	struct dasd_devmap *devmap;
933 
934 	devmap = dasd_find_busid(dev_name(&device->cdev->dev));
935 	if (IS_ERR(devmap))
936 		return;
937 	spin_lock(&dasd_devmap_lock);
938 	gdp->private_data = devmap;
939 	spin_unlock(&dasd_devmap_lock);
940 }
941 EXPORT_SYMBOL(dasd_add_link_to_gendisk);
942 
943 struct dasd_device *dasd_device_from_gendisk(struct gendisk *gdp)
944 {
945 	struct dasd_device *device;
946 	struct dasd_devmap *devmap;
947 
948 	if (!gdp->private_data)
949 		return NULL;
950 	device = NULL;
951 	spin_lock(&dasd_devmap_lock);
952 	devmap = gdp->private_data;
953 	if (devmap && devmap->device) {
954 		device = devmap->device;
955 		dasd_get_device(device);
956 	}
957 	spin_unlock(&dasd_devmap_lock);
958 	return device;
959 }
960 
961 /*
962  * SECTION: files in sysfs
963  */
964 
965 /*
966  * failfast controls the behaviour, if no path is available
967  */
968 static ssize_t dasd_ff_show(struct device *dev, struct device_attribute *attr,
969 			    char *buf)
970 {
971 	struct dasd_devmap *devmap;
972 	int ff_flag;
973 
974 	devmap = dasd_find_busid(dev_name(dev));
975 	if (!IS_ERR(devmap))
976 		ff_flag = (devmap->features & DASD_FEATURE_FAILFAST) != 0;
977 	else
978 		ff_flag = (DASD_FEATURE_DEFAULT & DASD_FEATURE_FAILFAST) != 0;
979 	return sysfs_emit(buf, ff_flag ? "1\n" : "0\n");
980 }
981 
982 static ssize_t dasd_ff_store(struct device *dev, struct device_attribute *attr,
983 	      const char *buf, size_t count)
984 {
985 	unsigned int val;
986 	int rc;
987 
988 	if (kstrtouint(buf, 0, &val) || val > 1)
989 		return -EINVAL;
990 
991 	rc = dasd_set_feature(to_ccwdev(dev), DASD_FEATURE_FAILFAST, val);
992 
993 	return rc ? : count;
994 }
995 
996 static DEVICE_ATTR(failfast, 0644, dasd_ff_show, dasd_ff_store);
997 
998 /*
999  * readonly controls the readonly status of a dasd
1000  */
1001 static ssize_t
1002 dasd_ro_show(struct device *dev, struct device_attribute *attr, char *buf)
1003 {
1004 	struct dasd_devmap *devmap;
1005 	struct dasd_device *device;
1006 	int ro_flag = 0;
1007 
1008 	devmap = dasd_find_busid(dev_name(dev));
1009 	if (IS_ERR(devmap))
1010 		goto out;
1011 
1012 	ro_flag = !!(devmap->features & DASD_FEATURE_READONLY);
1013 
1014 	spin_lock(&dasd_devmap_lock);
1015 	device = devmap->device;
1016 	if (device)
1017 		ro_flag |= test_bit(DASD_FLAG_DEVICE_RO, &device->flags);
1018 	spin_unlock(&dasd_devmap_lock);
1019 
1020 out:
1021 	return sysfs_emit(buf, ro_flag ? "1\n" : "0\n");
1022 }
1023 
1024 static ssize_t
1025 dasd_ro_store(struct device *dev, struct device_attribute *attr,
1026 	      const char *buf, size_t count)
1027 {
1028 	struct ccw_device *cdev = to_ccwdev(dev);
1029 	struct dasd_device *device;
1030 	unsigned long flags;
1031 	unsigned int val;
1032 	int rc;
1033 
1034 	if (kstrtouint(buf, 0, &val) || val > 1)
1035 		return -EINVAL;
1036 
1037 	rc = dasd_set_feature(cdev, DASD_FEATURE_READONLY, val);
1038 	if (rc)
1039 		return rc;
1040 
1041 	device = dasd_device_from_cdev(cdev);
1042 	if (IS_ERR(device))
1043 		return count;
1044 
1045 	spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1046 	val = val || test_bit(DASD_FLAG_DEVICE_RO, &device->flags);
1047 
1048 	if (!device->block || !device->block->gdp ||
1049 	    test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
1050 		spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1051 		goto out;
1052 	}
1053 	/* Increase open_count to avoid losing the block device */
1054 	atomic_inc(&device->block->open_count);
1055 	spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1056 
1057 	set_disk_ro(device->block->gdp, val);
1058 	atomic_dec(&device->block->open_count);
1059 
1060 out:
1061 	dasd_put_device(device);
1062 
1063 	return count;
1064 }
1065 
1066 static DEVICE_ATTR(readonly, 0644, dasd_ro_show, dasd_ro_store);
1067 /*
1068  * erplog controls the logging of ERP related data
1069  * (e.g. failing channel programs).
1070  */
1071 static ssize_t
1072 dasd_erplog_show(struct device *dev, struct device_attribute *attr, char *buf)
1073 {
1074 	struct dasd_devmap *devmap;
1075 	int erplog;
1076 
1077 	devmap = dasd_find_busid(dev_name(dev));
1078 	if (!IS_ERR(devmap))
1079 		erplog = (devmap->features & DASD_FEATURE_ERPLOG) != 0;
1080 	else
1081 		erplog = (DASD_FEATURE_DEFAULT & DASD_FEATURE_ERPLOG) != 0;
1082 	return sysfs_emit(buf, erplog ? "1\n" : "0\n");
1083 }
1084 
1085 static ssize_t
1086 dasd_erplog_store(struct device *dev, struct device_attribute *attr,
1087 	      const char *buf, size_t count)
1088 {
1089 	unsigned int val;
1090 	int rc;
1091 
1092 	if (kstrtouint(buf, 0, &val) || val > 1)
1093 		return -EINVAL;
1094 
1095 	rc = dasd_set_feature(to_ccwdev(dev), DASD_FEATURE_ERPLOG, val);
1096 
1097 	return rc ? : count;
1098 }
1099 
1100 static DEVICE_ATTR(erplog, 0644, dasd_erplog_show, dasd_erplog_store);
1101 
1102 /*
1103  * use_diag controls whether the driver should use diag rather than ssch
1104  * to talk to the device
1105  */
1106 static ssize_t
1107 dasd_use_diag_show(struct device *dev, struct device_attribute *attr, char *buf)
1108 {
1109 	struct dasd_devmap *devmap;
1110 	int use_diag;
1111 
1112 	devmap = dasd_find_busid(dev_name(dev));
1113 	if (!IS_ERR(devmap))
1114 		use_diag = (devmap->features & DASD_FEATURE_USEDIAG) != 0;
1115 	else
1116 		use_diag = (DASD_FEATURE_DEFAULT & DASD_FEATURE_USEDIAG) != 0;
1117 	return sprintf(buf, use_diag ? "1\n" : "0\n");
1118 }
1119 
1120 static ssize_t
1121 dasd_use_diag_store(struct device *dev, struct device_attribute *attr,
1122 		    const char *buf, size_t count)
1123 {
1124 	struct dasd_devmap *devmap;
1125 	unsigned int val;
1126 	ssize_t rc;
1127 
1128 	devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
1129 	if (IS_ERR(devmap))
1130 		return PTR_ERR(devmap);
1131 
1132 	if (kstrtouint(buf, 0, &val) || val > 1)
1133 		return -EINVAL;
1134 
1135 	spin_lock(&dasd_devmap_lock);
1136 	/* Changing diag discipline flag is only allowed in offline state. */
1137 	rc = count;
1138 	if (!devmap->device && !(devmap->features & DASD_FEATURE_USERAW)) {
1139 		if (val)
1140 			devmap->features |= DASD_FEATURE_USEDIAG;
1141 		else
1142 			devmap->features &= ~DASD_FEATURE_USEDIAG;
1143 	} else
1144 		rc = -EPERM;
1145 	spin_unlock(&dasd_devmap_lock);
1146 	return rc;
1147 }
1148 
1149 static DEVICE_ATTR(use_diag, 0644, dasd_use_diag_show, dasd_use_diag_store);
1150 
1151 /*
1152  * use_raw controls whether the driver should give access to raw eckd data or
1153  * operate in standard mode
1154  */
1155 static ssize_t
1156 dasd_use_raw_show(struct device *dev, struct device_attribute *attr, char *buf)
1157 {
1158 	struct dasd_devmap *devmap;
1159 	int use_raw;
1160 
1161 	devmap = dasd_find_busid(dev_name(dev));
1162 	if (!IS_ERR(devmap))
1163 		use_raw = (devmap->features & DASD_FEATURE_USERAW) != 0;
1164 	else
1165 		use_raw = (DASD_FEATURE_DEFAULT & DASD_FEATURE_USERAW) != 0;
1166 	return sprintf(buf, use_raw ? "1\n" : "0\n");
1167 }
1168 
1169 static ssize_t
1170 dasd_use_raw_store(struct device *dev, struct device_attribute *attr,
1171 		    const char *buf, size_t count)
1172 {
1173 	struct dasd_devmap *devmap;
1174 	ssize_t rc;
1175 	unsigned long val;
1176 
1177 	devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
1178 	if (IS_ERR(devmap))
1179 		return PTR_ERR(devmap);
1180 
1181 	if ((kstrtoul(buf, 10, &val) != 0) || val > 1)
1182 		return -EINVAL;
1183 
1184 	spin_lock(&dasd_devmap_lock);
1185 	/* Changing diag discipline flag is only allowed in offline state. */
1186 	rc = count;
1187 	if (!devmap->device && !(devmap->features & DASD_FEATURE_USEDIAG)) {
1188 		if (val)
1189 			devmap->features |= DASD_FEATURE_USERAW;
1190 		else
1191 			devmap->features &= ~DASD_FEATURE_USERAW;
1192 	} else
1193 		rc = -EPERM;
1194 	spin_unlock(&dasd_devmap_lock);
1195 	return rc;
1196 }
1197 
1198 static DEVICE_ATTR(raw_track_access, 0644, dasd_use_raw_show,
1199 		   dasd_use_raw_store);
1200 
1201 static ssize_t
1202 dasd_safe_offline_store(struct device *dev, struct device_attribute *attr,
1203 			const char *buf, size_t count)
1204 {
1205 	struct ccw_device *cdev = to_ccwdev(dev);
1206 	struct dasd_device *device;
1207 	unsigned long flags;
1208 	int rc;
1209 
1210 	spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1211 	device = dasd_device_from_cdev_locked(cdev);
1212 	if (IS_ERR(device)) {
1213 		rc = PTR_ERR(device);
1214 		spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1215 		goto out;
1216 	}
1217 
1218 	if (test_bit(DASD_FLAG_OFFLINE, &device->flags) ||
1219 	    test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
1220 		/* Already doing offline processing */
1221 		dasd_put_device(device);
1222 		spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1223 		rc = -EBUSY;
1224 		goto out;
1225 	}
1226 
1227 	set_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags);
1228 	dasd_put_device(device);
1229 	spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1230 
1231 	rc = ccw_device_set_offline(cdev);
1232 
1233 out:
1234 	return rc ? rc : count;
1235 }
1236 
1237 static DEVICE_ATTR(safe_offline, 0200, NULL, dasd_safe_offline_store);
1238 
1239 static ssize_t
1240 dasd_access_show(struct device *dev, struct device_attribute *attr,
1241 		 char *buf)
1242 {
1243 	struct ccw_device *cdev = to_ccwdev(dev);
1244 	struct dasd_device *device;
1245 	int count;
1246 
1247 	device = dasd_device_from_cdev(cdev);
1248 	if (IS_ERR(device))
1249 		return PTR_ERR(device);
1250 
1251 	if (!device->discipline)
1252 		count = -ENODEV;
1253 	else if (!device->discipline->host_access_count)
1254 		count = -EOPNOTSUPP;
1255 	else
1256 		count = device->discipline->host_access_count(device);
1257 
1258 	dasd_put_device(device);
1259 	if (count < 0)
1260 		return count;
1261 
1262 	return sprintf(buf, "%d\n", count);
1263 }
1264 
1265 static DEVICE_ATTR(host_access_count, 0444, dasd_access_show, NULL);
1266 
1267 static ssize_t
1268 dasd_discipline_show(struct device *dev, struct device_attribute *attr,
1269 		     char *buf)
1270 {
1271 	struct dasd_device *device;
1272 	ssize_t len;
1273 
1274 	device = dasd_device_from_cdev(to_ccwdev(dev));
1275 	if (IS_ERR(device))
1276 		goto out;
1277 	else if (!device->discipline) {
1278 		dasd_put_device(device);
1279 		goto out;
1280 	} else {
1281 		len = sysfs_emit(buf, "%s\n",
1282 				 device->discipline->name);
1283 		dasd_put_device(device);
1284 		return len;
1285 	}
1286 out:
1287 	len = sysfs_emit(buf, "none\n");
1288 	return len;
1289 }
1290 
1291 static DEVICE_ATTR(discipline, 0444, dasd_discipline_show, NULL);
1292 
1293 static ssize_t
1294 dasd_device_status_show(struct device *dev, struct device_attribute *attr,
1295 		     char *buf)
1296 {
1297 	struct dasd_device *device;
1298 	ssize_t len;
1299 
1300 	device = dasd_device_from_cdev(to_ccwdev(dev));
1301 	if (!IS_ERR(device)) {
1302 		switch (device->state) {
1303 		case DASD_STATE_NEW:
1304 			len = sysfs_emit(buf, "new\n");
1305 			break;
1306 		case DASD_STATE_KNOWN:
1307 			len = sysfs_emit(buf, "detected\n");
1308 			break;
1309 		case DASD_STATE_BASIC:
1310 			len = sysfs_emit(buf, "basic\n");
1311 			break;
1312 		case DASD_STATE_UNFMT:
1313 			len = sysfs_emit(buf, "unformatted\n");
1314 			break;
1315 		case DASD_STATE_READY:
1316 			len = sysfs_emit(buf, "ready\n");
1317 			break;
1318 		case DASD_STATE_ONLINE:
1319 			len = sysfs_emit(buf, "online\n");
1320 			break;
1321 		default:
1322 			len = sysfs_emit(buf, "no stat\n");
1323 			break;
1324 		}
1325 		dasd_put_device(device);
1326 	} else
1327 		len = sysfs_emit(buf, "unknown\n");
1328 	return len;
1329 }
1330 
1331 static DEVICE_ATTR(status, 0444, dasd_device_status_show, NULL);
1332 
1333 static ssize_t dasd_alias_show(struct device *dev,
1334 			       struct device_attribute *attr, char *buf)
1335 {
1336 	struct dasd_device *device;
1337 	struct dasd_uid uid;
1338 
1339 	device = dasd_device_from_cdev(to_ccwdev(dev));
1340 	if (IS_ERR(device))
1341 		return sprintf(buf, "0\n");
1342 
1343 	if (device->discipline && device->discipline->get_uid &&
1344 	    !device->discipline->get_uid(device, &uid)) {
1345 		if (uid.type == UA_BASE_PAV_ALIAS ||
1346 		    uid.type == UA_HYPER_PAV_ALIAS) {
1347 			dasd_put_device(device);
1348 			return sprintf(buf, "1\n");
1349 		}
1350 	}
1351 	dasd_put_device(device);
1352 
1353 	return sprintf(buf, "0\n");
1354 }
1355 
1356 static DEVICE_ATTR(alias, 0444, dasd_alias_show, NULL);
1357 
1358 static ssize_t dasd_vendor_show(struct device *dev,
1359 				struct device_attribute *attr, char *buf)
1360 {
1361 	struct dasd_device *device;
1362 	struct dasd_uid uid;
1363 	char *vendor;
1364 
1365 	device = dasd_device_from_cdev(to_ccwdev(dev));
1366 	vendor = "";
1367 	if (IS_ERR(device))
1368 		return sysfs_emit(buf, "%s\n", vendor);
1369 
1370 	if (device->discipline && device->discipline->get_uid &&
1371 	    !device->discipline->get_uid(device, &uid))
1372 			vendor = uid.vendor;
1373 
1374 	dasd_put_device(device);
1375 
1376 	return sysfs_emit(buf, "%s\n", vendor);
1377 }
1378 
1379 static DEVICE_ATTR(vendor, 0444, dasd_vendor_show, NULL);
1380 
1381 static ssize_t
1382 dasd_uid_show(struct device *dev, struct device_attribute *attr, char *buf)
1383 {
1384 	char uid_string[DASD_UID_STRLEN];
1385 	struct dasd_device *device;
1386 	struct dasd_uid uid;
1387 	char ua_string[3];
1388 
1389 	device = dasd_device_from_cdev(to_ccwdev(dev));
1390 	uid_string[0] = 0;
1391 	if (IS_ERR(device))
1392 		return sysfs_emit(buf, "%s\n", uid_string);
1393 
1394 	if (device->discipline && device->discipline->get_uid &&
1395 	    !device->discipline->get_uid(device, &uid)) {
1396 		switch (uid.type) {
1397 		case UA_BASE_DEVICE:
1398 			snprintf(ua_string, sizeof(ua_string), "%02x",
1399 				 uid.real_unit_addr);
1400 			break;
1401 		case UA_BASE_PAV_ALIAS:
1402 			snprintf(ua_string, sizeof(ua_string), "%02x",
1403 				 uid.base_unit_addr);
1404 			break;
1405 		case UA_HYPER_PAV_ALIAS:
1406 			snprintf(ua_string, sizeof(ua_string), "xx");
1407 			break;
1408 		default:
1409 			/* should not happen, treat like base device */
1410 			snprintf(ua_string, sizeof(ua_string), "%02x",
1411 				 uid.real_unit_addr);
1412 			break;
1413 		}
1414 
1415 		if (strlen(uid.vduit) > 0)
1416 			snprintf(uid_string, sizeof(uid_string),
1417 				 "%s.%s.%04x.%s.%s",
1418 				 uid.vendor, uid.serial, uid.ssid, ua_string,
1419 				 uid.vduit);
1420 		else
1421 			snprintf(uid_string, sizeof(uid_string),
1422 				 "%s.%s.%04x.%s",
1423 				 uid.vendor, uid.serial, uid.ssid, ua_string);
1424 	}
1425 	dasd_put_device(device);
1426 
1427 	return sysfs_emit(buf, "%s\n", uid_string);
1428 }
1429 static DEVICE_ATTR(uid, 0444, dasd_uid_show, NULL);
1430 
1431 /*
1432  * extended error-reporting
1433  */
1434 static ssize_t
1435 dasd_eer_show(struct device *dev, struct device_attribute *attr, char *buf)
1436 {
1437 	struct dasd_devmap *devmap;
1438 	int eer_flag;
1439 
1440 	devmap = dasd_find_busid(dev_name(dev));
1441 	if (!IS_ERR(devmap) && devmap->device)
1442 		eer_flag = dasd_eer_enabled(devmap->device);
1443 	else
1444 		eer_flag = 0;
1445 	return sysfs_emit(buf, eer_flag ? "1\n" : "0\n");
1446 }
1447 
1448 static ssize_t
1449 dasd_eer_store(struct device *dev, struct device_attribute *attr,
1450 	       const char *buf, size_t count)
1451 {
1452 	struct dasd_device *device;
1453 	unsigned int val;
1454 	int rc = 0;
1455 
1456 	device = dasd_device_from_cdev(to_ccwdev(dev));
1457 	if (IS_ERR(device))
1458 		return PTR_ERR(device);
1459 
1460 	if (kstrtouint(buf, 0, &val) || val > 1)
1461 		return -EINVAL;
1462 
1463 	if (val)
1464 		rc = dasd_eer_enable(device);
1465 	else
1466 		dasd_eer_disable(device);
1467 
1468 	dasd_put_device(device);
1469 
1470 	return rc ? : count;
1471 }
1472 
1473 static DEVICE_ATTR(eer_enabled, 0644, dasd_eer_show, dasd_eer_store);
1474 
1475 /*
1476  * aq_mask controls if the DASD should be quiesced on certain triggers
1477  * The aq_mask attribute is interpreted as bitmap of the DASD_EER_* triggers.
1478  */
1479 static ssize_t dasd_aq_mask_show(struct device *dev, struct device_attribute *attr,
1480 				 char *buf)
1481 {
1482 	struct dasd_devmap *devmap;
1483 	unsigned int aq_mask = 0;
1484 
1485 	devmap = dasd_find_busid(dev_name(dev));
1486 	if (!IS_ERR(devmap))
1487 		aq_mask = devmap->aq_mask;
1488 
1489 	return sysfs_emit(buf, "%d\n", aq_mask);
1490 }
1491 
1492 static ssize_t dasd_aq_mask_store(struct device *dev, struct device_attribute *attr,
1493 				  const char *buf, size_t count)
1494 {
1495 	struct dasd_devmap *devmap;
1496 	unsigned int val;
1497 
1498 	if (kstrtouint(buf, 0, &val) || val > DASD_EER_VALID)
1499 		return -EINVAL;
1500 
1501 	devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
1502 	if (IS_ERR(devmap))
1503 		return PTR_ERR(devmap);
1504 
1505 	spin_lock(&dasd_devmap_lock);
1506 	devmap->aq_mask = val;
1507 	if (devmap->device)
1508 		devmap->device->aq_mask = devmap->aq_mask;
1509 	spin_unlock(&dasd_devmap_lock);
1510 
1511 	return count;
1512 }
1513 
1514 static DEVICE_ATTR(aq_mask, 0644, dasd_aq_mask_show, dasd_aq_mask_store);
1515 
1516 /*
1517  * aq_requeue controls if requests are returned to the blocklayer on quiesce
1518  * or if requests are only not started
1519  */
1520 static ssize_t dasd_aqr_show(struct device *dev, struct device_attribute *attr,
1521 			     char *buf)
1522 {
1523 	struct dasd_devmap *devmap;
1524 	int flag;
1525 
1526 	devmap = dasd_find_busid(dev_name(dev));
1527 	if (!IS_ERR(devmap))
1528 		flag = (devmap->features & DASD_FEATURE_REQUEUEQUIESCE) != 0;
1529 	else
1530 		flag = (DASD_FEATURE_DEFAULT &
1531 			DASD_FEATURE_REQUEUEQUIESCE) != 0;
1532 	return sysfs_emit(buf, "%d\n", flag);
1533 }
1534 
1535 static ssize_t dasd_aqr_store(struct device *dev, struct device_attribute *attr,
1536 			      const char *buf, size_t count)
1537 {
1538 	bool val;
1539 	int rc;
1540 
1541 	if (kstrtobool(buf, &val))
1542 		return -EINVAL;
1543 
1544 	rc = dasd_set_feature(to_ccwdev(dev), DASD_FEATURE_REQUEUEQUIESCE, val);
1545 
1546 	return rc ? : count;
1547 }
1548 
1549 static DEVICE_ATTR(aq_requeue, 0644, dasd_aqr_show, dasd_aqr_store);
1550 
1551 /*
1552  * aq_timeouts controls how much retries have to time out until
1553  * a device gets autoquiesced
1554  */
1555 static ssize_t
1556 dasd_aq_timeouts_show(struct device *dev, struct device_attribute *attr,
1557 		      char *buf)
1558 {
1559 	struct dasd_device *device;
1560 	int len;
1561 
1562 	device = dasd_device_from_cdev(to_ccwdev(dev));
1563 	if (IS_ERR(device))
1564 		return -ENODEV;
1565 	len = sysfs_emit(buf, "%u\n", device->aq_timeouts);
1566 	dasd_put_device(device);
1567 	return len;
1568 }
1569 
1570 static ssize_t
1571 dasd_aq_timeouts_store(struct device *dev, struct device_attribute *attr,
1572 		       const char *buf, size_t count)
1573 {
1574 	struct dasd_device *device;
1575 	unsigned int val;
1576 
1577 	device = dasd_device_from_cdev(to_ccwdev(dev));
1578 	if (IS_ERR(device))
1579 		return -ENODEV;
1580 
1581 	if ((kstrtouint(buf, 10, &val) != 0) ||
1582 	    val > DASD_RETRIES_MAX || val == 0) {
1583 		dasd_put_device(device);
1584 		return -EINVAL;
1585 	}
1586 
1587 	if (val)
1588 		device->aq_timeouts = val;
1589 
1590 	dasd_put_device(device);
1591 	return count;
1592 }
1593 
1594 static DEVICE_ATTR(aq_timeouts, 0644, dasd_aq_timeouts_show,
1595 		   dasd_aq_timeouts_store);
1596 
1597 /*
1598  * expiration time for default requests
1599  */
1600 static ssize_t
1601 dasd_expires_show(struct device *dev, struct device_attribute *attr, char *buf)
1602 {
1603 	struct dasd_device *device;
1604 	int len;
1605 
1606 	device = dasd_device_from_cdev(to_ccwdev(dev));
1607 	if (IS_ERR(device))
1608 		return -ENODEV;
1609 	len = sysfs_emit(buf, "%lu\n", device->default_expires);
1610 	dasd_put_device(device);
1611 	return len;
1612 }
1613 
1614 static ssize_t
1615 dasd_expires_store(struct device *dev, struct device_attribute *attr,
1616 	       const char *buf, size_t count)
1617 {
1618 	struct dasd_device *device;
1619 	unsigned long val;
1620 
1621 	device = dasd_device_from_cdev(to_ccwdev(dev));
1622 	if (IS_ERR(device))
1623 		return -ENODEV;
1624 
1625 	if ((kstrtoul(buf, 10, &val) != 0) ||
1626 	    (val > DASD_EXPIRES_MAX) || val == 0) {
1627 		dasd_put_device(device);
1628 		return -EINVAL;
1629 	}
1630 
1631 	if (val)
1632 		device->default_expires = val;
1633 
1634 	dasd_put_device(device);
1635 	return count;
1636 }
1637 
1638 static DEVICE_ATTR(expires, 0644, dasd_expires_show, dasd_expires_store);
1639 
1640 static ssize_t
1641 dasd_retries_show(struct device *dev, struct device_attribute *attr, char *buf)
1642 {
1643 	struct dasd_device *device;
1644 	int len;
1645 
1646 	device = dasd_device_from_cdev(to_ccwdev(dev));
1647 	if (IS_ERR(device))
1648 		return -ENODEV;
1649 	len = sysfs_emit(buf, "%lu\n", device->default_retries);
1650 	dasd_put_device(device);
1651 	return len;
1652 }
1653 
1654 static ssize_t
1655 dasd_retries_store(struct device *dev, struct device_attribute *attr,
1656 		   const char *buf, size_t count)
1657 {
1658 	struct dasd_device *device;
1659 	unsigned long val;
1660 
1661 	device = dasd_device_from_cdev(to_ccwdev(dev));
1662 	if (IS_ERR(device))
1663 		return -ENODEV;
1664 
1665 	if ((kstrtoul(buf, 10, &val) != 0) ||
1666 	    (val > DASD_RETRIES_MAX)) {
1667 		dasd_put_device(device);
1668 		return -EINVAL;
1669 	}
1670 
1671 	if (val)
1672 		device->default_retries = val;
1673 
1674 	dasd_put_device(device);
1675 	return count;
1676 }
1677 
1678 static DEVICE_ATTR(retries, 0644, dasd_retries_show, dasd_retries_store);
1679 
1680 static ssize_t
1681 dasd_timeout_show(struct device *dev, struct device_attribute *attr,
1682 		  char *buf)
1683 {
1684 	struct dasd_device *device;
1685 	int len;
1686 
1687 	device = dasd_device_from_cdev(to_ccwdev(dev));
1688 	if (IS_ERR(device))
1689 		return -ENODEV;
1690 	len = sysfs_emit(buf, "%lu\n", device->blk_timeout);
1691 	dasd_put_device(device);
1692 	return len;
1693 }
1694 
1695 static ssize_t
1696 dasd_timeout_store(struct device *dev, struct device_attribute *attr,
1697 		   const char *buf, size_t count)
1698 {
1699 	struct dasd_device *device;
1700 	unsigned long val;
1701 
1702 	device = dasd_device_from_cdev(to_ccwdev(dev));
1703 	if (IS_ERR(device) || !device->block)
1704 		return -ENODEV;
1705 
1706 	if ((kstrtoul(buf, 10, &val) != 0) ||
1707 	    val > UINT_MAX / HZ) {
1708 		dasd_put_device(device);
1709 		return -EINVAL;
1710 	}
1711 	if (!device->block->gdp) {
1712 		dasd_put_device(device);
1713 		return -ENODEV;
1714 	}
1715 
1716 	device->blk_timeout = val;
1717 	blk_queue_rq_timeout(device->block->gdp->queue, val * HZ);
1718 
1719 	dasd_put_device(device);
1720 	return count;
1721 }
1722 
1723 static DEVICE_ATTR(timeout, 0644,
1724 		   dasd_timeout_show, dasd_timeout_store);
1725 
1726 
1727 static ssize_t
1728 dasd_path_reset_store(struct device *dev, struct device_attribute *attr,
1729 		      const char *buf, size_t count)
1730 {
1731 	struct dasd_device *device;
1732 	unsigned int val;
1733 
1734 	device = dasd_device_from_cdev(to_ccwdev(dev));
1735 	if (IS_ERR(device))
1736 		return -ENODEV;
1737 
1738 	if ((kstrtouint(buf, 16, &val) != 0) || val > 0xff)
1739 		val = 0;
1740 
1741 	if (device->discipline && device->discipline->reset_path)
1742 		device->discipline->reset_path(device, (__u8) val);
1743 
1744 	dasd_put_device(device);
1745 	return count;
1746 }
1747 
1748 static DEVICE_ATTR(path_reset, 0200, NULL, dasd_path_reset_store);
1749 
1750 static ssize_t dasd_hpf_show(struct device *dev, struct device_attribute *attr,
1751 			     char *buf)
1752 {
1753 	struct dasd_device *device;
1754 	int hpf;
1755 
1756 	device = dasd_device_from_cdev(to_ccwdev(dev));
1757 	if (IS_ERR(device))
1758 		return -ENODEV;
1759 	if (!device->discipline || !device->discipline->hpf_enabled) {
1760 		dasd_put_device(device);
1761 		return sysfs_emit(buf, "%d\n", dasd_nofcx);
1762 	}
1763 	hpf = device->discipline->hpf_enabled(device);
1764 	dasd_put_device(device);
1765 	return sysfs_emit(buf, "%d\n", hpf);
1766 }
1767 
1768 static DEVICE_ATTR(hpf, 0444, dasd_hpf_show, NULL);
1769 
1770 static ssize_t dasd_reservation_policy_show(struct device *dev,
1771 					    struct device_attribute *attr,
1772 					    char *buf)
1773 {
1774 	struct dasd_devmap *devmap;
1775 	int rc = 0;
1776 
1777 	devmap = dasd_find_busid(dev_name(dev));
1778 	if (IS_ERR(devmap)) {
1779 		rc = sysfs_emit(buf, "ignore\n");
1780 	} else {
1781 		spin_lock(&dasd_devmap_lock);
1782 		if (devmap->features & DASD_FEATURE_FAILONSLCK)
1783 			rc = sysfs_emit(buf, "fail\n");
1784 		else
1785 			rc = sysfs_emit(buf, "ignore\n");
1786 		spin_unlock(&dasd_devmap_lock);
1787 	}
1788 	return rc;
1789 }
1790 
1791 static ssize_t dasd_reservation_policy_store(struct device *dev,
1792 					     struct device_attribute *attr,
1793 					     const char *buf, size_t count)
1794 {
1795 	struct ccw_device *cdev = to_ccwdev(dev);
1796 	int rc;
1797 
1798 	if (sysfs_streq("ignore", buf))
1799 		rc = dasd_set_feature(cdev, DASD_FEATURE_FAILONSLCK, 0);
1800 	else if (sysfs_streq("fail", buf))
1801 		rc = dasd_set_feature(cdev, DASD_FEATURE_FAILONSLCK, 1);
1802 	else
1803 		rc = -EINVAL;
1804 
1805 	return rc ? : count;
1806 }
1807 
1808 static DEVICE_ATTR(reservation_policy, 0644,
1809 		   dasd_reservation_policy_show, dasd_reservation_policy_store);
1810 
1811 static ssize_t dasd_reservation_state_show(struct device *dev,
1812 					   struct device_attribute *attr,
1813 					   char *buf)
1814 {
1815 	struct dasd_device *device;
1816 	int rc = 0;
1817 
1818 	device = dasd_device_from_cdev(to_ccwdev(dev));
1819 	if (IS_ERR(device))
1820 		return sysfs_emit(buf, "none\n");
1821 
1822 	if (test_bit(DASD_FLAG_IS_RESERVED, &device->flags))
1823 		rc = sysfs_emit(buf, "reserved\n");
1824 	else if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags))
1825 		rc = sysfs_emit(buf, "lost\n");
1826 	else
1827 		rc = sysfs_emit(buf, "none\n");
1828 	dasd_put_device(device);
1829 	return rc;
1830 }
1831 
1832 static ssize_t dasd_reservation_state_store(struct device *dev,
1833 					    struct device_attribute *attr,
1834 					    const char *buf, size_t count)
1835 {
1836 	struct dasd_device *device;
1837 	int rc = 0;
1838 
1839 	device = dasd_device_from_cdev(to_ccwdev(dev));
1840 	if (IS_ERR(device))
1841 		return -ENODEV;
1842 	if (sysfs_streq("reset", buf))
1843 		clear_bit(DASD_FLAG_LOCK_STOLEN, &device->flags);
1844 	else
1845 		rc = -EINVAL;
1846 	dasd_put_device(device);
1847 
1848 	if (rc)
1849 		return rc;
1850 	else
1851 		return count;
1852 }
1853 
1854 static DEVICE_ATTR(last_known_reservation_state, 0644,
1855 		   dasd_reservation_state_show, dasd_reservation_state_store);
1856 
1857 static ssize_t dasd_pm_show(struct device *dev,
1858 			      struct device_attribute *attr, char *buf)
1859 {
1860 	struct dasd_device *device;
1861 	u8 opm, nppm, cablepm, cuirpm, hpfpm, ifccpm;
1862 
1863 	device = dasd_device_from_cdev(to_ccwdev(dev));
1864 	if (IS_ERR(device))
1865 		return sprintf(buf, "0\n");
1866 
1867 	opm = dasd_path_get_opm(device);
1868 	nppm = dasd_path_get_nppm(device);
1869 	cablepm = dasd_path_get_cablepm(device);
1870 	cuirpm = dasd_path_get_cuirpm(device);
1871 	hpfpm = dasd_path_get_hpfpm(device);
1872 	ifccpm = dasd_path_get_ifccpm(device);
1873 	dasd_put_device(device);
1874 
1875 	return sprintf(buf, "%02x %02x %02x %02x %02x %02x\n", opm, nppm,
1876 		       cablepm, cuirpm, hpfpm, ifccpm);
1877 }
1878 
1879 static DEVICE_ATTR(path_masks, 0444, dasd_pm_show, NULL);
1880 
1881 /*
1882  * threshold value for IFCC/CCC errors
1883  */
1884 static ssize_t
1885 dasd_path_threshold_show(struct device *dev,
1886 			  struct device_attribute *attr, char *buf)
1887 {
1888 	struct dasd_device *device;
1889 	int len;
1890 
1891 	device = dasd_device_from_cdev(to_ccwdev(dev));
1892 	if (IS_ERR(device))
1893 		return -ENODEV;
1894 	len = sysfs_emit(buf, "%lu\n", device->path_thrhld);
1895 	dasd_put_device(device);
1896 	return len;
1897 }
1898 
1899 static ssize_t
1900 dasd_path_threshold_store(struct device *dev, struct device_attribute *attr,
1901 			   const char *buf, size_t count)
1902 {
1903 	struct dasd_device *device;
1904 	unsigned long flags;
1905 	unsigned long val;
1906 
1907 	device = dasd_device_from_cdev(to_ccwdev(dev));
1908 	if (IS_ERR(device))
1909 		return -ENODEV;
1910 
1911 	if (kstrtoul(buf, 10, &val) != 0 || val > DASD_THRHLD_MAX) {
1912 		dasd_put_device(device);
1913 		return -EINVAL;
1914 	}
1915 	spin_lock_irqsave(get_ccwdev_lock(to_ccwdev(dev)), flags);
1916 	device->path_thrhld = val;
1917 	spin_unlock_irqrestore(get_ccwdev_lock(to_ccwdev(dev)), flags);
1918 	dasd_put_device(device);
1919 	return count;
1920 }
1921 static DEVICE_ATTR(path_threshold, 0644, dasd_path_threshold_show,
1922 		   dasd_path_threshold_store);
1923 
1924 /*
1925  * configure if path is disabled after IFCC/CCC error threshold is
1926  * exceeded
1927  */
1928 static ssize_t
1929 dasd_path_autodisable_show(struct device *dev,
1930 				   struct device_attribute *attr, char *buf)
1931 {
1932 	struct dasd_devmap *devmap;
1933 	int flag;
1934 
1935 	devmap = dasd_find_busid(dev_name(dev));
1936 	if (!IS_ERR(devmap))
1937 		flag = (devmap->features & DASD_FEATURE_PATH_AUTODISABLE) != 0;
1938 	else
1939 		flag = (DASD_FEATURE_DEFAULT &
1940 			DASD_FEATURE_PATH_AUTODISABLE) != 0;
1941 	return sysfs_emit(buf, flag ? "1\n" : "0\n");
1942 }
1943 
1944 static ssize_t
1945 dasd_path_autodisable_store(struct device *dev,
1946 				    struct device_attribute *attr,
1947 				    const char *buf, size_t count)
1948 {
1949 	unsigned int val;
1950 	int rc;
1951 
1952 	if (kstrtouint(buf, 0, &val) || val > 1)
1953 		return -EINVAL;
1954 
1955 	rc = dasd_set_feature(to_ccwdev(dev),
1956 			      DASD_FEATURE_PATH_AUTODISABLE, val);
1957 
1958 	return rc ? : count;
1959 }
1960 
1961 static DEVICE_ATTR(path_autodisable, 0644,
1962 		   dasd_path_autodisable_show,
1963 		   dasd_path_autodisable_store);
1964 /*
1965  * interval for IFCC/CCC checks
1966  * meaning time with no IFCC/CCC error before the error counter
1967  * gets reset
1968  */
1969 static ssize_t
1970 dasd_path_interval_show(struct device *dev,
1971 			struct device_attribute *attr, char *buf)
1972 {
1973 	struct dasd_device *device;
1974 	int len;
1975 
1976 	device = dasd_device_from_cdev(to_ccwdev(dev));
1977 	if (IS_ERR(device))
1978 		return -ENODEV;
1979 	len = sysfs_emit(buf, "%lu\n", device->path_interval);
1980 	dasd_put_device(device);
1981 	return len;
1982 }
1983 
1984 static ssize_t
1985 dasd_path_interval_store(struct device *dev, struct device_attribute *attr,
1986 	       const char *buf, size_t count)
1987 {
1988 	struct dasd_device *device;
1989 	unsigned long flags;
1990 	unsigned long val;
1991 
1992 	device = dasd_device_from_cdev(to_ccwdev(dev));
1993 	if (IS_ERR(device))
1994 		return -ENODEV;
1995 
1996 	if ((kstrtoul(buf, 10, &val) != 0) ||
1997 	    (val > DASD_INTERVAL_MAX) || val == 0) {
1998 		dasd_put_device(device);
1999 		return -EINVAL;
2000 	}
2001 	spin_lock_irqsave(get_ccwdev_lock(to_ccwdev(dev)), flags);
2002 	if (val)
2003 		device->path_interval = val;
2004 	spin_unlock_irqrestore(get_ccwdev_lock(to_ccwdev(dev)), flags);
2005 	dasd_put_device(device);
2006 	return count;
2007 }
2008 
2009 static DEVICE_ATTR(path_interval, 0644, dasd_path_interval_show,
2010 		   dasd_path_interval_store);
2011 
2012 static ssize_t
2013 dasd_device_fcs_show(struct device *dev, struct device_attribute *attr,
2014 		     char *buf)
2015 {
2016 	struct dasd_device *device;
2017 	int fc_sec;
2018 	int rc;
2019 
2020 	device = dasd_device_from_cdev(to_ccwdev(dev));
2021 	if (IS_ERR(device))
2022 		return -ENODEV;
2023 	fc_sec = dasd_path_get_fcs_device(device);
2024 	if (fc_sec == -EINVAL)
2025 		rc = sysfs_emit(buf, "Inconsistent\n");
2026 	else
2027 		rc = sysfs_emit(buf, "%s\n", dasd_path_get_fcs_str(fc_sec));
2028 	dasd_put_device(device);
2029 
2030 	return rc;
2031 }
2032 static DEVICE_ATTR(fc_security, 0444, dasd_device_fcs_show, NULL);
2033 
2034 static ssize_t
2035 dasd_path_fcs_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
2036 {
2037 	struct dasd_path *path = to_dasd_path(kobj);
2038 	unsigned int fc_sec = path->fc_security;
2039 
2040 	return sysfs_emit(buf, "%s\n", dasd_path_get_fcs_str(fc_sec));
2041 }
2042 
2043 static struct kobj_attribute path_fcs_attribute =
2044 	__ATTR(fc_security, 0444, dasd_path_fcs_show, NULL);
2045 
2046 /*
2047  * print copy relation in the form
2048  * primary,secondary[1] primary,secondary[2], ...
2049  */
2050 static ssize_t
2051 dasd_copy_pair_show(struct device *dev,
2052 		    struct device_attribute *attr, char *buf)
2053 {
2054 	char prim_busid[DASD_BUS_ID_SIZE];
2055 	struct dasd_copy_relation *copy;
2056 	struct dasd_devmap *devmap;
2057 	int len = 0;
2058 	int i;
2059 
2060 	devmap = dasd_find_busid(dev_name(dev));
2061 	if (IS_ERR(devmap))
2062 		return -ENODEV;
2063 
2064 	if (!devmap->copy)
2065 		return -ENODEV;
2066 
2067 	copy = devmap->copy;
2068 	/* find primary */
2069 	for (i = 0; i < DASD_CP_ENTRIES; i++) {
2070 		if (copy->entry[i].configured && copy->entry[i].primary) {
2071 			strscpy(prim_busid, copy->entry[i].busid,
2072 				DASD_BUS_ID_SIZE);
2073 			break;
2074 		}
2075 	}
2076 	if (i == DASD_CP_ENTRIES)
2077 		goto out;
2078 
2079 	/* print all secondary */
2080 	for (i = 0; i < DASD_CP_ENTRIES; i++) {
2081 		if (copy->entry[i].configured && !copy->entry[i].primary)
2082 			len += sysfs_emit_at(buf, len, "%s,%s ", prim_busid,
2083 					     copy->entry[i].busid);
2084 	}
2085 
2086 	len += sysfs_emit_at(buf, len, "\n");
2087 out:
2088 	return len;
2089 }
2090 
2091 static int dasd_devmap_set_copy_relation(struct dasd_devmap *devmap,
2092 					 struct dasd_copy_relation *copy,
2093 					 char *busid, bool primary)
2094 {
2095 	int i;
2096 
2097 	/* find free entry */
2098 	for (i = 0; i < DASD_CP_ENTRIES; i++) {
2099 		/* current bus_id already included, nothing to do */
2100 		if (copy->entry[i].configured &&
2101 		    strncmp(copy->entry[i].busid, busid, DASD_BUS_ID_SIZE) == 0)
2102 			return 0;
2103 
2104 		if (!copy->entry[i].configured)
2105 			break;
2106 	}
2107 	if (i == DASD_CP_ENTRIES)
2108 		return -EINVAL;
2109 
2110 	copy->entry[i].configured = true;
2111 	strscpy(copy->entry[i].busid, busid, DASD_BUS_ID_SIZE);
2112 	if (primary) {
2113 		copy->active = &copy->entry[i];
2114 		copy->entry[i].primary = true;
2115 	}
2116 	if (!devmap->copy)
2117 		devmap->copy = copy;
2118 
2119 	return 0;
2120 }
2121 
2122 static void dasd_devmap_del_copy_relation(struct dasd_copy_relation *copy,
2123 					  char *busid)
2124 {
2125 	int i;
2126 
2127 	spin_lock(&dasd_devmap_lock);
2128 	/* find entry */
2129 	for (i = 0; i < DASD_CP_ENTRIES; i++) {
2130 		if (copy->entry[i].configured &&
2131 		    strncmp(copy->entry[i].busid, busid, DASD_BUS_ID_SIZE) == 0)
2132 			break;
2133 	}
2134 	if (i == DASD_CP_ENTRIES || !copy->entry[i].configured) {
2135 		spin_unlock(&dasd_devmap_lock);
2136 		return;
2137 	}
2138 
2139 	copy->entry[i].configured = false;
2140 	memset(copy->entry[i].busid, 0, DASD_BUS_ID_SIZE);
2141 	if (copy->active == &copy->entry[i]) {
2142 		copy->active = NULL;
2143 		copy->entry[i].primary = false;
2144 	}
2145 	spin_unlock(&dasd_devmap_lock);
2146 }
2147 
2148 static int dasd_devmap_clear_copy_relation(struct device *dev)
2149 {
2150 	struct dasd_copy_relation *copy;
2151 	struct dasd_devmap *devmap;
2152 	int i, rc = 1;
2153 
2154 	devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
2155 	if (IS_ERR(devmap))
2156 		return 1;
2157 
2158 	spin_lock(&dasd_devmap_lock);
2159 	if (!devmap->copy)
2160 		goto out;
2161 
2162 	copy = devmap->copy;
2163 	/* first check if all secondary devices are offline*/
2164 	for (i = 0; i < DASD_CP_ENTRIES; i++) {
2165 		if (!copy->entry[i].configured)
2166 			continue;
2167 
2168 		if (copy->entry[i].device == copy->active->device)
2169 			continue;
2170 
2171 		if (copy->entry[i].device)
2172 			goto out;
2173 	}
2174 	/* clear all devmap entries */
2175 	for (i = 0; i < DASD_CP_ENTRIES; i++) {
2176 		if (strlen(copy->entry[i].busid) == 0)
2177 			continue;
2178 		if (copy->entry[i].device) {
2179 			dasd_put_device(copy->entry[i].device);
2180 			copy->entry[i].device->copy = NULL;
2181 			copy->entry[i].device = NULL;
2182 		}
2183 		devmap = dasd_find_busid_locked(copy->entry[i].busid);
2184 		devmap->copy = NULL;
2185 		memset(copy->entry[i].busid, 0, DASD_BUS_ID_SIZE);
2186 	}
2187 	kfree(copy);
2188 	rc = 0;
2189 out:
2190 	spin_unlock(&dasd_devmap_lock);
2191 	return rc;
2192 }
2193 
2194 /*
2195  * parse BUSIDs from a copy pair
2196  */
2197 static int dasd_devmap_parse_busid(const char *buf, char *prim_busid,
2198 				   char *sec_busid)
2199 {
2200 	char *primary, *secondary, *tmp, *pt;
2201 	int id0, id1, id2;
2202 
2203 	pt =  kstrdup(buf, GFP_KERNEL);
2204 	tmp = pt;
2205 	if (!tmp)
2206 		return -ENOMEM;
2207 
2208 	primary = strsep(&tmp, ",");
2209 	if (!primary) {
2210 		kfree(pt);
2211 		return -EINVAL;
2212 	}
2213 	secondary = strsep(&tmp, ",");
2214 	if (!secondary) {
2215 		kfree(pt);
2216 		return -EINVAL;
2217 	}
2218 	if (dasd_busid(primary, &id0, &id1, &id2)) {
2219 		kfree(pt);
2220 		return -EINVAL;
2221 	}
2222 	sprintf(prim_busid, "%01x.%01x.%04x", id0, id1, id2);
2223 	if (dasd_busid(secondary, &id0, &id1, &id2)) {
2224 		kfree(pt);
2225 		return -EINVAL;
2226 	}
2227 	sprintf(sec_busid, "%01x.%01x.%04x", id0, id1, id2);
2228 	kfree(pt);
2229 
2230 	return 0;
2231 }
2232 
2233 static ssize_t dasd_copy_pair_store(struct device *dev,
2234 				    struct device_attribute *attr,
2235 				    const char *buf, size_t count)
2236 {
2237 	struct dasd_devmap *prim_devmap, *sec_devmap;
2238 	char prim_busid[DASD_BUS_ID_SIZE];
2239 	char sec_busid[DASD_BUS_ID_SIZE];
2240 	struct dasd_copy_relation *copy;
2241 	struct dasd_device *device;
2242 	bool pprc_enabled;
2243 	int rc;
2244 
2245 	if (strncmp(buf, "clear", strlen("clear")) == 0) {
2246 		if (dasd_devmap_clear_copy_relation(dev))
2247 			return -EINVAL;
2248 		return count;
2249 	}
2250 
2251 	rc = dasd_devmap_parse_busid(buf, prim_busid, sec_busid);
2252 	if (rc)
2253 		return rc;
2254 
2255 	if (strncmp(dev_name(dev), prim_busid, DASD_BUS_ID_SIZE) != 0 &&
2256 	    strncmp(dev_name(dev), sec_busid, DASD_BUS_ID_SIZE) != 0)
2257 		return -EINVAL;
2258 
2259 	/* allocate primary devmap if needed */
2260 	prim_devmap = dasd_find_busid(prim_busid);
2261 	if (IS_ERR(prim_devmap)) {
2262 		prim_devmap = dasd_add_busid(prim_busid, DASD_FEATURE_DEFAULT);
2263 		if (IS_ERR(prim_devmap))
2264 			return PTR_ERR(prim_devmap);
2265 	}
2266 
2267 	/* allocate secondary devmap if needed */
2268 	sec_devmap = dasd_find_busid(sec_busid);
2269 	if (IS_ERR(sec_devmap)) {
2270 		sec_devmap = dasd_add_busid(sec_busid, DASD_FEATURE_DEFAULT);
2271 		if (IS_ERR(sec_devmap))
2272 			return PTR_ERR(sec_devmap);
2273 	}
2274 
2275 	/* setting copy relation is only allowed for offline secondary */
2276 	if (sec_devmap->device)
2277 		return -EINVAL;
2278 
2279 	if (prim_devmap->copy) {
2280 		copy = prim_devmap->copy;
2281 	} else if (sec_devmap->copy) {
2282 		copy = sec_devmap->copy;
2283 	} else {
2284 		copy = kzalloc(sizeof(*copy), GFP_KERNEL);
2285 		if (!copy)
2286 			return -ENOMEM;
2287 	}
2288 	spin_lock(&dasd_devmap_lock);
2289 	rc = dasd_devmap_set_copy_relation(prim_devmap, copy, prim_busid, true);
2290 	if (rc) {
2291 		spin_unlock(&dasd_devmap_lock);
2292 		return rc;
2293 	}
2294 	rc = dasd_devmap_set_copy_relation(sec_devmap, copy, sec_busid, false);
2295 	if (rc) {
2296 		spin_unlock(&dasd_devmap_lock);
2297 		return rc;
2298 	}
2299 	spin_unlock(&dasd_devmap_lock);
2300 
2301 	/* if primary device is already online call device setup directly */
2302 	if (prim_devmap->device && !prim_devmap->device->copy) {
2303 		device = prim_devmap->device;
2304 		if (device->discipline->pprc_enabled) {
2305 			pprc_enabled = device->discipline->pprc_enabled(device);
2306 			rc = dasd_devmap_set_device_copy_relation(device->cdev,
2307 								  pprc_enabled);
2308 		} else {
2309 			rc = -EOPNOTSUPP;
2310 		}
2311 	}
2312 	if (rc) {
2313 		dasd_devmap_del_copy_relation(copy, prim_busid);
2314 		dasd_devmap_del_copy_relation(copy, sec_busid);
2315 		count = rc;
2316 	}
2317 
2318 	return count;
2319 }
2320 static DEVICE_ATTR(copy_pair, 0644, dasd_copy_pair_show,
2321 		   dasd_copy_pair_store);
2322 
2323 static ssize_t
2324 dasd_copy_role_show(struct device *dev,
2325 		    struct device_attribute *attr, char *buf)
2326 {
2327 	struct dasd_copy_relation *copy;
2328 	struct dasd_device *device;
2329 	int len, i;
2330 
2331 	device = dasd_device_from_cdev(to_ccwdev(dev));
2332 	if (IS_ERR(device))
2333 		return -ENODEV;
2334 
2335 	if (!device->copy) {
2336 		len = sysfs_emit(buf, "none\n");
2337 		goto out;
2338 	}
2339 	copy = device->copy;
2340 	/* only the active device is primary */
2341 	if (copy->active->device == device) {
2342 		len = sysfs_emit(buf, "primary\n");
2343 		goto out;
2344 	}
2345 	for (i = 0; i < DASD_CP_ENTRIES; i++) {
2346 		if (copy->entry[i].device == device) {
2347 			len = sysfs_emit(buf, "secondary\n");
2348 			goto out;
2349 		}
2350 	}
2351 	/* not in the list, no COPY role */
2352 	len = sysfs_emit(buf, "none\n");
2353 out:
2354 	dasd_put_device(device);
2355 	return len;
2356 }
2357 static DEVICE_ATTR(copy_role, 0444, dasd_copy_role_show, NULL);
2358 
2359 static ssize_t dasd_device_ping(struct device *dev,
2360 				struct device_attribute *attr,
2361 				const char *buf, size_t count)
2362 {
2363 	struct dasd_device *device;
2364 	size_t rc;
2365 
2366 	device = dasd_device_from_cdev(to_ccwdev(dev));
2367 	if (IS_ERR(device))
2368 		return -ENODEV;
2369 
2370 	/*
2371 	 * do not try during offline processing
2372 	 * early check only
2373 	 * the sleep_on function itself checks for offline
2374 	 * processing again
2375 	 */
2376 	if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2377 		rc = -EBUSY;
2378 		goto out;
2379 	}
2380 	if (!device->discipline || !device->discipline->device_ping) {
2381 		rc = -EOPNOTSUPP;
2382 		goto out;
2383 	}
2384 	rc = device->discipline->device_ping(device);
2385 	if (!rc)
2386 		rc = count;
2387 out:
2388 	dasd_put_device(device);
2389 	return rc;
2390 }
2391 static DEVICE_ATTR(ping, 0200, NULL, dasd_device_ping);
2392 
2393 #define DASD_DEFINE_ATTR(_name, _func)					\
2394 static ssize_t dasd_##_name##_show(struct device *dev,			\
2395 				   struct device_attribute *attr,	\
2396 				   char *buf)				\
2397 {									\
2398 	struct ccw_device *cdev = to_ccwdev(dev);			\
2399 	struct dasd_device *device = dasd_device_from_cdev(cdev);	\
2400 	int val = 0;							\
2401 									\
2402 	if (IS_ERR(device))						\
2403 		return -ENODEV;						\
2404 	if (device->discipline && _func)				\
2405 		val = _func(device);					\
2406 	dasd_put_device(device);					\
2407 									\
2408 	return sysfs_emit(buf, "%d\n", val);			\
2409 }									\
2410 static DEVICE_ATTR(_name, 0444, dasd_##_name##_show, NULL);		\
2411 
2412 DASD_DEFINE_ATTR(ese, device->discipline->is_ese);
2413 DASD_DEFINE_ATTR(extent_size, device->discipline->ext_size);
2414 DASD_DEFINE_ATTR(pool_id, device->discipline->ext_pool_id);
2415 DASD_DEFINE_ATTR(space_configured, device->discipline->space_configured);
2416 DASD_DEFINE_ATTR(space_allocated, device->discipline->space_allocated);
2417 DASD_DEFINE_ATTR(logical_capacity, device->discipline->logical_capacity);
2418 DASD_DEFINE_ATTR(warn_threshold, device->discipline->ext_pool_warn_thrshld);
2419 DASD_DEFINE_ATTR(cap_at_warnlevel, device->discipline->ext_pool_cap_at_warnlevel);
2420 DASD_DEFINE_ATTR(pool_oos, device->discipline->ext_pool_oos);
2421 
2422 static struct attribute * dasd_attrs[] = {
2423 	&dev_attr_readonly.attr,
2424 	&dev_attr_discipline.attr,
2425 	&dev_attr_status.attr,
2426 	&dev_attr_alias.attr,
2427 	&dev_attr_vendor.attr,
2428 	&dev_attr_uid.attr,
2429 	&dev_attr_use_diag.attr,
2430 	&dev_attr_raw_track_access.attr,
2431 	&dev_attr_eer_enabled.attr,
2432 	&dev_attr_erplog.attr,
2433 	&dev_attr_failfast.attr,
2434 	&dev_attr_expires.attr,
2435 	&dev_attr_retries.attr,
2436 	&dev_attr_timeout.attr,
2437 	&dev_attr_reservation_policy.attr,
2438 	&dev_attr_last_known_reservation_state.attr,
2439 	&dev_attr_safe_offline.attr,
2440 	&dev_attr_host_access_count.attr,
2441 	&dev_attr_path_masks.attr,
2442 	&dev_attr_path_threshold.attr,
2443 	&dev_attr_path_autodisable.attr,
2444 	&dev_attr_path_interval.attr,
2445 	&dev_attr_path_reset.attr,
2446 	&dev_attr_hpf.attr,
2447 	&dev_attr_ese.attr,
2448 	&dev_attr_fc_security.attr,
2449 	&dev_attr_copy_pair.attr,
2450 	&dev_attr_copy_role.attr,
2451 	&dev_attr_ping.attr,
2452 	&dev_attr_aq_mask.attr,
2453 	&dev_attr_aq_requeue.attr,
2454 	&dev_attr_aq_timeouts.attr,
2455 	NULL,
2456 };
2457 
2458 static const struct attribute_group dasd_attr_group = {
2459 	.attrs = dasd_attrs,
2460 };
2461 
2462 static struct attribute *capacity_attrs[] = {
2463 	&dev_attr_space_configured.attr,
2464 	&dev_attr_space_allocated.attr,
2465 	&dev_attr_logical_capacity.attr,
2466 	NULL,
2467 };
2468 
2469 static const struct attribute_group capacity_attr_group = {
2470 	.name = "capacity",
2471 	.attrs = capacity_attrs,
2472 };
2473 
2474 static struct attribute *ext_pool_attrs[] = {
2475 	&dev_attr_pool_id.attr,
2476 	&dev_attr_extent_size.attr,
2477 	&dev_attr_warn_threshold.attr,
2478 	&dev_attr_cap_at_warnlevel.attr,
2479 	&dev_attr_pool_oos.attr,
2480 	NULL,
2481 };
2482 
2483 static const struct attribute_group ext_pool_attr_group = {
2484 	.name = "extent_pool",
2485 	.attrs = ext_pool_attrs,
2486 };
2487 
2488 const struct attribute_group *dasd_dev_groups[] = {
2489 	&dasd_attr_group,
2490 	&capacity_attr_group,
2491 	&ext_pool_attr_group,
2492 	NULL,
2493 };
2494 EXPORT_SYMBOL_GPL(dasd_dev_groups);
2495 
2496 /*
2497  * Return value of the specified feature.
2498  */
2499 int
2500 dasd_get_feature(struct ccw_device *cdev, int feature)
2501 {
2502 	struct dasd_devmap *devmap;
2503 
2504 	devmap = dasd_find_busid(dev_name(&cdev->dev));
2505 	if (IS_ERR(devmap))
2506 		return PTR_ERR(devmap);
2507 
2508 	return ((devmap->features & feature) != 0);
2509 }
2510 
2511 /*
2512  * Set / reset given feature.
2513  * Flag indicates whether to set (!=0) or the reset (=0) the feature.
2514  */
2515 int
2516 dasd_set_feature(struct ccw_device *cdev, int feature, int flag)
2517 {
2518 	struct dasd_devmap *devmap;
2519 
2520 	devmap = dasd_devmap_from_cdev(cdev);
2521 	if (IS_ERR(devmap))
2522 		return PTR_ERR(devmap);
2523 
2524 	spin_lock(&dasd_devmap_lock);
2525 	if (flag)
2526 		devmap->features |= feature;
2527 	else
2528 		devmap->features &= ~feature;
2529 	if (devmap->device)
2530 		devmap->device->features = devmap->features;
2531 	spin_unlock(&dasd_devmap_lock);
2532 	return 0;
2533 }
2534 EXPORT_SYMBOL(dasd_set_feature);
2535 
2536 static struct attribute *paths_info_attrs[] = {
2537 	&path_fcs_attribute.attr,
2538 	NULL,
2539 };
2540 ATTRIBUTE_GROUPS(paths_info);
2541 
2542 static struct kobj_type path_attr_type = {
2543 	.release	= dasd_path_release,
2544 	.default_groups	= paths_info_groups,
2545 	.sysfs_ops	= &kobj_sysfs_ops,
2546 };
2547 
2548 static void dasd_path_init_kobj(struct dasd_device *device, int chp)
2549 {
2550 	device->path[chp].kobj.kset = device->paths_info;
2551 	kobject_init(&device->path[chp].kobj, &path_attr_type);
2552 }
2553 
2554 void dasd_path_create_kobj(struct dasd_device *device, int chp)
2555 {
2556 	int rc;
2557 
2558 	if (test_bit(DASD_FLAG_OFFLINE, &device->flags))
2559 		return;
2560 	if (!device->paths_info) {
2561 		dev_warn(&device->cdev->dev, "Unable to create paths objects\n");
2562 		return;
2563 	}
2564 	if (device->path[chp].in_sysfs)
2565 		return;
2566 	if (!device->path[chp].conf_data)
2567 		return;
2568 
2569 	dasd_path_init_kobj(device, chp);
2570 
2571 	rc = kobject_add(&device->path[chp].kobj, NULL, "%x.%02x",
2572 			 device->path[chp].cssid, device->path[chp].chpid);
2573 	if (rc)
2574 		kobject_put(&device->path[chp].kobj);
2575 	device->path[chp].in_sysfs = true;
2576 }
2577 EXPORT_SYMBOL(dasd_path_create_kobj);
2578 
2579 void dasd_path_create_kobjects(struct dasd_device *device)
2580 {
2581 	u8 lpm, opm;
2582 
2583 	opm = dasd_path_get_opm(device);
2584 	for (lpm = 0x80; lpm; lpm >>= 1) {
2585 		if (!(lpm & opm))
2586 			continue;
2587 		dasd_path_create_kobj(device, pathmask_to_pos(lpm));
2588 	}
2589 }
2590 EXPORT_SYMBOL(dasd_path_create_kobjects);
2591 
2592 static void dasd_path_remove_kobj(struct dasd_device *device, int chp)
2593 {
2594 	if (device->path[chp].in_sysfs) {
2595 		kobject_put(&device->path[chp].kobj);
2596 		device->path[chp].in_sysfs = false;
2597 	}
2598 }
2599 
2600 /*
2601  * As we keep kobjects for the lifetime of a device, this function must not be
2602  * called anywhere but in the context of offlining a device.
2603  */
2604 void dasd_path_remove_kobjects(struct dasd_device *device)
2605 {
2606 	int i;
2607 
2608 	for (i = 0; i < 8; i++)
2609 		dasd_path_remove_kobj(device, i);
2610 }
2611 EXPORT_SYMBOL(dasd_path_remove_kobjects);
2612 
2613 int
2614 dasd_devmap_init(void)
2615 {
2616 	int i;
2617 
2618 	/* Initialize devmap structures. */
2619 	dasd_max_devindex = 0;
2620 	for (i = 0; i < 256; i++)
2621 		INIT_LIST_HEAD(&dasd_hashlists[i]);
2622 	return 0;
2623 }
2624 
2625 void
2626 dasd_devmap_exit(void)
2627 {
2628 	dasd_forget_ranges();
2629 }
2630