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