xref: /openbmc/linux/drivers/s390/block/dasd_devmap.c (revision 84e6da7a)
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 #define UID_STRLEN ( /* vendor */ 3 + 1 + /* serial    */ 14 + 1 +\
1382 		     /* SSID   */ 4 + 1 + /* unit addr */ 2 + 1 +\
1383 		     /* vduit */ 32 + 1)
1384 
1385 static ssize_t
1386 dasd_uid_show(struct device *dev, struct device_attribute *attr, char *buf)
1387 {
1388 	struct dasd_device *device;
1389 	struct dasd_uid uid;
1390 	char uid_string[UID_STRLEN];
1391 	char ua_string[3];
1392 
1393 	device = dasd_device_from_cdev(to_ccwdev(dev));
1394 	uid_string[0] = 0;
1395 	if (IS_ERR(device))
1396 		return sysfs_emit(buf, "%s\n", uid_string);
1397 
1398 	if (device->discipline && device->discipline->get_uid &&
1399 	    !device->discipline->get_uid(device, &uid)) {
1400 		switch (uid.type) {
1401 		case UA_BASE_DEVICE:
1402 			snprintf(ua_string, sizeof(ua_string), "%02x",
1403 				 uid.real_unit_addr);
1404 			break;
1405 		case UA_BASE_PAV_ALIAS:
1406 			snprintf(ua_string, sizeof(ua_string), "%02x",
1407 				 uid.base_unit_addr);
1408 			break;
1409 		case UA_HYPER_PAV_ALIAS:
1410 			snprintf(ua_string, sizeof(ua_string), "xx");
1411 			break;
1412 		default:
1413 			/* should not happen, treat like base device */
1414 			snprintf(ua_string, sizeof(ua_string), "%02x",
1415 				 uid.real_unit_addr);
1416 			break;
1417 		}
1418 
1419 		if (strlen(uid.vduit) > 0)
1420 			snprintf(uid_string, sizeof(uid_string),
1421 				 "%s.%s.%04x.%s.%s",
1422 				 uid.vendor, uid.serial, uid.ssid, ua_string,
1423 				 uid.vduit);
1424 		else
1425 			snprintf(uid_string, sizeof(uid_string),
1426 				 "%s.%s.%04x.%s",
1427 				 uid.vendor, uid.serial, uid.ssid, ua_string);
1428 	}
1429 	dasd_put_device(device);
1430 
1431 	return sysfs_emit(buf, "%s\n", uid_string);
1432 }
1433 static DEVICE_ATTR(uid, 0444, dasd_uid_show, NULL);
1434 
1435 /*
1436  * extended error-reporting
1437  */
1438 static ssize_t
1439 dasd_eer_show(struct device *dev, struct device_attribute *attr, char *buf)
1440 {
1441 	struct dasd_devmap *devmap;
1442 	int eer_flag;
1443 
1444 	devmap = dasd_find_busid(dev_name(dev));
1445 	if (!IS_ERR(devmap) && devmap->device)
1446 		eer_flag = dasd_eer_enabled(devmap->device);
1447 	else
1448 		eer_flag = 0;
1449 	return sysfs_emit(buf, eer_flag ? "1\n" : "0\n");
1450 }
1451 
1452 static ssize_t
1453 dasd_eer_store(struct device *dev, struct device_attribute *attr,
1454 	       const char *buf, size_t count)
1455 {
1456 	struct dasd_device *device;
1457 	unsigned int val;
1458 	int rc = 0;
1459 
1460 	device = dasd_device_from_cdev(to_ccwdev(dev));
1461 	if (IS_ERR(device))
1462 		return PTR_ERR(device);
1463 
1464 	if (kstrtouint(buf, 0, &val) || val > 1)
1465 		return -EINVAL;
1466 
1467 	if (val)
1468 		rc = dasd_eer_enable(device);
1469 	else
1470 		dasd_eer_disable(device);
1471 
1472 	dasd_put_device(device);
1473 
1474 	return rc ? : count;
1475 }
1476 
1477 static DEVICE_ATTR(eer_enabled, 0644, dasd_eer_show, dasd_eer_store);
1478 
1479 /*
1480  * aq_mask controls if the DASD should be quiesced on certain triggers
1481  * The aq_mask attribute is interpreted as bitmap of the DASD_EER_* triggers.
1482  */
1483 static ssize_t dasd_aq_mask_show(struct device *dev, struct device_attribute *attr,
1484 				 char *buf)
1485 {
1486 	struct dasd_devmap *devmap;
1487 	unsigned int aq_mask = 0;
1488 
1489 	devmap = dasd_find_busid(dev_name(dev));
1490 	if (!IS_ERR(devmap))
1491 		aq_mask = devmap->aq_mask;
1492 
1493 	return sysfs_emit(buf, "%d\n", aq_mask);
1494 }
1495 
1496 static ssize_t dasd_aq_mask_store(struct device *dev, struct device_attribute *attr,
1497 				  const char *buf, size_t count)
1498 {
1499 	struct dasd_devmap *devmap;
1500 	unsigned int val;
1501 
1502 	if (kstrtouint(buf, 0, &val) || val > DASD_EER_VALID)
1503 		return -EINVAL;
1504 
1505 	devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
1506 	if (IS_ERR(devmap))
1507 		return PTR_ERR(devmap);
1508 
1509 	spin_lock(&dasd_devmap_lock);
1510 	devmap->aq_mask = val;
1511 	if (devmap->device)
1512 		devmap->device->aq_mask = devmap->aq_mask;
1513 	spin_unlock(&dasd_devmap_lock);
1514 
1515 	return count;
1516 }
1517 
1518 static DEVICE_ATTR(aq_mask, 0644, dasd_aq_mask_show, dasd_aq_mask_store);
1519 
1520 /*
1521  * aq_requeue controls if requests are returned to the blocklayer on quiesce
1522  * or if requests are only not started
1523  */
1524 static ssize_t dasd_aqr_show(struct device *dev, struct device_attribute *attr,
1525 			     char *buf)
1526 {
1527 	struct dasd_devmap *devmap;
1528 	int flag;
1529 
1530 	devmap = dasd_find_busid(dev_name(dev));
1531 	if (!IS_ERR(devmap))
1532 		flag = (devmap->features & DASD_FEATURE_REQUEUEQUIESCE) != 0;
1533 	else
1534 		flag = (DASD_FEATURE_DEFAULT &
1535 			DASD_FEATURE_REQUEUEQUIESCE) != 0;
1536 	return sysfs_emit(buf, "%d\n", flag);
1537 }
1538 
1539 static ssize_t dasd_aqr_store(struct device *dev, struct device_attribute *attr,
1540 			      const char *buf, size_t count)
1541 {
1542 	bool val;
1543 	int rc;
1544 
1545 	if (kstrtobool(buf, &val))
1546 		return -EINVAL;
1547 
1548 	rc = dasd_set_feature(to_ccwdev(dev), DASD_FEATURE_REQUEUEQUIESCE, val);
1549 
1550 	return rc ? : count;
1551 }
1552 
1553 static DEVICE_ATTR(aq_requeue, 0644, dasd_aqr_show, dasd_aqr_store);
1554 
1555 /*
1556  * aq_timeouts controls how much retries have to time out until
1557  * a device gets autoquiesced
1558  */
1559 static ssize_t
1560 dasd_aq_timeouts_show(struct device *dev, struct device_attribute *attr,
1561 		      char *buf)
1562 {
1563 	struct dasd_device *device;
1564 	int len;
1565 
1566 	device = dasd_device_from_cdev(to_ccwdev(dev));
1567 	if (IS_ERR(device))
1568 		return -ENODEV;
1569 	len = sysfs_emit(buf, "%u\n", device->aq_timeouts);
1570 	dasd_put_device(device);
1571 	return len;
1572 }
1573 
1574 static ssize_t
1575 dasd_aq_timeouts_store(struct device *dev, struct device_attribute *attr,
1576 		       const char *buf, size_t count)
1577 {
1578 	struct dasd_device *device;
1579 	unsigned int val;
1580 
1581 	device = dasd_device_from_cdev(to_ccwdev(dev));
1582 	if (IS_ERR(device))
1583 		return -ENODEV;
1584 
1585 	if ((kstrtouint(buf, 10, &val) != 0) ||
1586 	    val > DASD_RETRIES_MAX || val == 0) {
1587 		dasd_put_device(device);
1588 		return -EINVAL;
1589 	}
1590 
1591 	if (val)
1592 		device->aq_timeouts = val;
1593 
1594 	dasd_put_device(device);
1595 	return count;
1596 }
1597 
1598 static DEVICE_ATTR(aq_timeouts, 0644, dasd_aq_timeouts_show,
1599 		   dasd_aq_timeouts_store);
1600 
1601 /*
1602  * expiration time for default requests
1603  */
1604 static ssize_t
1605 dasd_expires_show(struct device *dev, struct device_attribute *attr, char *buf)
1606 {
1607 	struct dasd_device *device;
1608 	int len;
1609 
1610 	device = dasd_device_from_cdev(to_ccwdev(dev));
1611 	if (IS_ERR(device))
1612 		return -ENODEV;
1613 	len = sysfs_emit(buf, "%lu\n", device->default_expires);
1614 	dasd_put_device(device);
1615 	return len;
1616 }
1617 
1618 static ssize_t
1619 dasd_expires_store(struct device *dev, struct device_attribute *attr,
1620 	       const char *buf, size_t count)
1621 {
1622 	struct dasd_device *device;
1623 	unsigned long val;
1624 
1625 	device = dasd_device_from_cdev(to_ccwdev(dev));
1626 	if (IS_ERR(device))
1627 		return -ENODEV;
1628 
1629 	if ((kstrtoul(buf, 10, &val) != 0) ||
1630 	    (val > DASD_EXPIRES_MAX) || val == 0) {
1631 		dasd_put_device(device);
1632 		return -EINVAL;
1633 	}
1634 
1635 	if (val)
1636 		device->default_expires = val;
1637 
1638 	dasd_put_device(device);
1639 	return count;
1640 }
1641 
1642 static DEVICE_ATTR(expires, 0644, dasd_expires_show, dasd_expires_store);
1643 
1644 static ssize_t
1645 dasd_retries_show(struct device *dev, struct device_attribute *attr, char *buf)
1646 {
1647 	struct dasd_device *device;
1648 	int len;
1649 
1650 	device = dasd_device_from_cdev(to_ccwdev(dev));
1651 	if (IS_ERR(device))
1652 		return -ENODEV;
1653 	len = sysfs_emit(buf, "%lu\n", device->default_retries);
1654 	dasd_put_device(device);
1655 	return len;
1656 }
1657 
1658 static ssize_t
1659 dasd_retries_store(struct device *dev, struct device_attribute *attr,
1660 		   const char *buf, size_t count)
1661 {
1662 	struct dasd_device *device;
1663 	unsigned long val;
1664 
1665 	device = dasd_device_from_cdev(to_ccwdev(dev));
1666 	if (IS_ERR(device))
1667 		return -ENODEV;
1668 
1669 	if ((kstrtoul(buf, 10, &val) != 0) ||
1670 	    (val > DASD_RETRIES_MAX)) {
1671 		dasd_put_device(device);
1672 		return -EINVAL;
1673 	}
1674 
1675 	if (val)
1676 		device->default_retries = val;
1677 
1678 	dasd_put_device(device);
1679 	return count;
1680 }
1681 
1682 static DEVICE_ATTR(retries, 0644, dasd_retries_show, dasd_retries_store);
1683 
1684 static ssize_t
1685 dasd_timeout_show(struct device *dev, struct device_attribute *attr,
1686 		  char *buf)
1687 {
1688 	struct dasd_device *device;
1689 	int len;
1690 
1691 	device = dasd_device_from_cdev(to_ccwdev(dev));
1692 	if (IS_ERR(device))
1693 		return -ENODEV;
1694 	len = sysfs_emit(buf, "%lu\n", device->blk_timeout);
1695 	dasd_put_device(device);
1696 	return len;
1697 }
1698 
1699 static ssize_t
1700 dasd_timeout_store(struct device *dev, struct device_attribute *attr,
1701 		   const char *buf, size_t count)
1702 {
1703 	struct dasd_device *device;
1704 	unsigned long val;
1705 
1706 	device = dasd_device_from_cdev(to_ccwdev(dev));
1707 	if (IS_ERR(device) || !device->block)
1708 		return -ENODEV;
1709 
1710 	if ((kstrtoul(buf, 10, &val) != 0) ||
1711 	    val > UINT_MAX / HZ) {
1712 		dasd_put_device(device);
1713 		return -EINVAL;
1714 	}
1715 	if (!device->block->gdp) {
1716 		dasd_put_device(device);
1717 		return -ENODEV;
1718 	}
1719 
1720 	device->blk_timeout = val;
1721 	blk_queue_rq_timeout(device->block->gdp->queue, val * HZ);
1722 
1723 	dasd_put_device(device);
1724 	return count;
1725 }
1726 
1727 static DEVICE_ATTR(timeout, 0644,
1728 		   dasd_timeout_show, dasd_timeout_store);
1729 
1730 
1731 static ssize_t
1732 dasd_path_reset_store(struct device *dev, struct device_attribute *attr,
1733 		      const char *buf, size_t count)
1734 {
1735 	struct dasd_device *device;
1736 	unsigned int val;
1737 
1738 	device = dasd_device_from_cdev(to_ccwdev(dev));
1739 	if (IS_ERR(device))
1740 		return -ENODEV;
1741 
1742 	if ((kstrtouint(buf, 16, &val) != 0) || val > 0xff)
1743 		val = 0;
1744 
1745 	if (device->discipline && device->discipline->reset_path)
1746 		device->discipline->reset_path(device, (__u8) val);
1747 
1748 	dasd_put_device(device);
1749 	return count;
1750 }
1751 
1752 static DEVICE_ATTR(path_reset, 0200, NULL, dasd_path_reset_store);
1753 
1754 static ssize_t dasd_hpf_show(struct device *dev, struct device_attribute *attr,
1755 			     char *buf)
1756 {
1757 	struct dasd_device *device;
1758 	int hpf;
1759 
1760 	device = dasd_device_from_cdev(to_ccwdev(dev));
1761 	if (IS_ERR(device))
1762 		return -ENODEV;
1763 	if (!device->discipline || !device->discipline->hpf_enabled) {
1764 		dasd_put_device(device);
1765 		return sysfs_emit(buf, "%d\n", dasd_nofcx);
1766 	}
1767 	hpf = device->discipline->hpf_enabled(device);
1768 	dasd_put_device(device);
1769 	return sysfs_emit(buf, "%d\n", hpf);
1770 }
1771 
1772 static DEVICE_ATTR(hpf, 0444, dasd_hpf_show, NULL);
1773 
1774 static ssize_t dasd_reservation_policy_show(struct device *dev,
1775 					    struct device_attribute *attr,
1776 					    char *buf)
1777 {
1778 	struct dasd_devmap *devmap;
1779 	int rc = 0;
1780 
1781 	devmap = dasd_find_busid(dev_name(dev));
1782 	if (IS_ERR(devmap)) {
1783 		rc = sysfs_emit(buf, "ignore\n");
1784 	} else {
1785 		spin_lock(&dasd_devmap_lock);
1786 		if (devmap->features & DASD_FEATURE_FAILONSLCK)
1787 			rc = sysfs_emit(buf, "fail\n");
1788 		else
1789 			rc = sysfs_emit(buf, "ignore\n");
1790 		spin_unlock(&dasd_devmap_lock);
1791 	}
1792 	return rc;
1793 }
1794 
1795 static ssize_t dasd_reservation_policy_store(struct device *dev,
1796 					     struct device_attribute *attr,
1797 					     const char *buf, size_t count)
1798 {
1799 	struct ccw_device *cdev = to_ccwdev(dev);
1800 	int rc;
1801 
1802 	if (sysfs_streq("ignore", buf))
1803 		rc = dasd_set_feature(cdev, DASD_FEATURE_FAILONSLCK, 0);
1804 	else if (sysfs_streq("fail", buf))
1805 		rc = dasd_set_feature(cdev, DASD_FEATURE_FAILONSLCK, 1);
1806 	else
1807 		rc = -EINVAL;
1808 
1809 	return rc ? : count;
1810 }
1811 
1812 static DEVICE_ATTR(reservation_policy, 0644,
1813 		   dasd_reservation_policy_show, dasd_reservation_policy_store);
1814 
1815 static ssize_t dasd_reservation_state_show(struct device *dev,
1816 					   struct device_attribute *attr,
1817 					   char *buf)
1818 {
1819 	struct dasd_device *device;
1820 	int rc = 0;
1821 
1822 	device = dasd_device_from_cdev(to_ccwdev(dev));
1823 	if (IS_ERR(device))
1824 		return sysfs_emit(buf, "none\n");
1825 
1826 	if (test_bit(DASD_FLAG_IS_RESERVED, &device->flags))
1827 		rc = sysfs_emit(buf, "reserved\n");
1828 	else if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags))
1829 		rc = sysfs_emit(buf, "lost\n");
1830 	else
1831 		rc = sysfs_emit(buf, "none\n");
1832 	dasd_put_device(device);
1833 	return rc;
1834 }
1835 
1836 static ssize_t dasd_reservation_state_store(struct device *dev,
1837 					    struct device_attribute *attr,
1838 					    const char *buf, size_t count)
1839 {
1840 	struct dasd_device *device;
1841 	int rc = 0;
1842 
1843 	device = dasd_device_from_cdev(to_ccwdev(dev));
1844 	if (IS_ERR(device))
1845 		return -ENODEV;
1846 	if (sysfs_streq("reset", buf))
1847 		clear_bit(DASD_FLAG_LOCK_STOLEN, &device->flags);
1848 	else
1849 		rc = -EINVAL;
1850 	dasd_put_device(device);
1851 
1852 	if (rc)
1853 		return rc;
1854 	else
1855 		return count;
1856 }
1857 
1858 static DEVICE_ATTR(last_known_reservation_state, 0644,
1859 		   dasd_reservation_state_show, dasd_reservation_state_store);
1860 
1861 static ssize_t dasd_pm_show(struct device *dev,
1862 			      struct device_attribute *attr, char *buf)
1863 {
1864 	struct dasd_device *device;
1865 	u8 opm, nppm, cablepm, cuirpm, hpfpm, ifccpm;
1866 
1867 	device = dasd_device_from_cdev(to_ccwdev(dev));
1868 	if (IS_ERR(device))
1869 		return sprintf(buf, "0\n");
1870 
1871 	opm = dasd_path_get_opm(device);
1872 	nppm = dasd_path_get_nppm(device);
1873 	cablepm = dasd_path_get_cablepm(device);
1874 	cuirpm = dasd_path_get_cuirpm(device);
1875 	hpfpm = dasd_path_get_hpfpm(device);
1876 	ifccpm = dasd_path_get_ifccpm(device);
1877 	dasd_put_device(device);
1878 
1879 	return sprintf(buf, "%02x %02x %02x %02x %02x %02x\n", opm, nppm,
1880 		       cablepm, cuirpm, hpfpm, ifccpm);
1881 }
1882 
1883 static DEVICE_ATTR(path_masks, 0444, dasd_pm_show, NULL);
1884 
1885 /*
1886  * threshold value for IFCC/CCC errors
1887  */
1888 static ssize_t
1889 dasd_path_threshold_show(struct device *dev,
1890 			  struct device_attribute *attr, char *buf)
1891 {
1892 	struct dasd_device *device;
1893 	int len;
1894 
1895 	device = dasd_device_from_cdev(to_ccwdev(dev));
1896 	if (IS_ERR(device))
1897 		return -ENODEV;
1898 	len = sysfs_emit(buf, "%lu\n", device->path_thrhld);
1899 	dasd_put_device(device);
1900 	return len;
1901 }
1902 
1903 static ssize_t
1904 dasd_path_threshold_store(struct device *dev, struct device_attribute *attr,
1905 			   const char *buf, size_t count)
1906 {
1907 	struct dasd_device *device;
1908 	unsigned long flags;
1909 	unsigned long val;
1910 
1911 	device = dasd_device_from_cdev(to_ccwdev(dev));
1912 	if (IS_ERR(device))
1913 		return -ENODEV;
1914 
1915 	if (kstrtoul(buf, 10, &val) != 0 || val > DASD_THRHLD_MAX) {
1916 		dasd_put_device(device);
1917 		return -EINVAL;
1918 	}
1919 	spin_lock_irqsave(get_ccwdev_lock(to_ccwdev(dev)), flags);
1920 	device->path_thrhld = val;
1921 	spin_unlock_irqrestore(get_ccwdev_lock(to_ccwdev(dev)), flags);
1922 	dasd_put_device(device);
1923 	return count;
1924 }
1925 static DEVICE_ATTR(path_threshold, 0644, dasd_path_threshold_show,
1926 		   dasd_path_threshold_store);
1927 
1928 /*
1929  * configure if path is disabled after IFCC/CCC error threshold is
1930  * exceeded
1931  */
1932 static ssize_t
1933 dasd_path_autodisable_show(struct device *dev,
1934 				   struct device_attribute *attr, char *buf)
1935 {
1936 	struct dasd_devmap *devmap;
1937 	int flag;
1938 
1939 	devmap = dasd_find_busid(dev_name(dev));
1940 	if (!IS_ERR(devmap))
1941 		flag = (devmap->features & DASD_FEATURE_PATH_AUTODISABLE) != 0;
1942 	else
1943 		flag = (DASD_FEATURE_DEFAULT &
1944 			DASD_FEATURE_PATH_AUTODISABLE) != 0;
1945 	return sysfs_emit(buf, flag ? "1\n" : "0\n");
1946 }
1947 
1948 static ssize_t
1949 dasd_path_autodisable_store(struct device *dev,
1950 				    struct device_attribute *attr,
1951 				    const char *buf, size_t count)
1952 {
1953 	unsigned int val;
1954 	int rc;
1955 
1956 	if (kstrtouint(buf, 0, &val) || val > 1)
1957 		return -EINVAL;
1958 
1959 	rc = dasd_set_feature(to_ccwdev(dev),
1960 			      DASD_FEATURE_PATH_AUTODISABLE, val);
1961 
1962 	return rc ? : count;
1963 }
1964 
1965 static DEVICE_ATTR(path_autodisable, 0644,
1966 		   dasd_path_autodisable_show,
1967 		   dasd_path_autodisable_store);
1968 /*
1969  * interval for IFCC/CCC checks
1970  * meaning time with no IFCC/CCC error before the error counter
1971  * gets reset
1972  */
1973 static ssize_t
1974 dasd_path_interval_show(struct device *dev,
1975 			struct device_attribute *attr, char *buf)
1976 {
1977 	struct dasd_device *device;
1978 	int len;
1979 
1980 	device = dasd_device_from_cdev(to_ccwdev(dev));
1981 	if (IS_ERR(device))
1982 		return -ENODEV;
1983 	len = sysfs_emit(buf, "%lu\n", device->path_interval);
1984 	dasd_put_device(device);
1985 	return len;
1986 }
1987 
1988 static ssize_t
1989 dasd_path_interval_store(struct device *dev, struct device_attribute *attr,
1990 	       const char *buf, size_t count)
1991 {
1992 	struct dasd_device *device;
1993 	unsigned long flags;
1994 	unsigned long val;
1995 
1996 	device = dasd_device_from_cdev(to_ccwdev(dev));
1997 	if (IS_ERR(device))
1998 		return -ENODEV;
1999 
2000 	if ((kstrtoul(buf, 10, &val) != 0) ||
2001 	    (val > DASD_INTERVAL_MAX) || val == 0) {
2002 		dasd_put_device(device);
2003 		return -EINVAL;
2004 	}
2005 	spin_lock_irqsave(get_ccwdev_lock(to_ccwdev(dev)), flags);
2006 	if (val)
2007 		device->path_interval = val;
2008 	spin_unlock_irqrestore(get_ccwdev_lock(to_ccwdev(dev)), flags);
2009 	dasd_put_device(device);
2010 	return count;
2011 }
2012 
2013 static DEVICE_ATTR(path_interval, 0644, dasd_path_interval_show,
2014 		   dasd_path_interval_store);
2015 
2016 static ssize_t
2017 dasd_device_fcs_show(struct device *dev, struct device_attribute *attr,
2018 		     char *buf)
2019 {
2020 	struct dasd_device *device;
2021 	int fc_sec;
2022 	int rc;
2023 
2024 	device = dasd_device_from_cdev(to_ccwdev(dev));
2025 	if (IS_ERR(device))
2026 		return -ENODEV;
2027 	fc_sec = dasd_path_get_fcs_device(device);
2028 	if (fc_sec == -EINVAL)
2029 		rc = sysfs_emit(buf, "Inconsistent\n");
2030 	else
2031 		rc = sysfs_emit(buf, "%s\n", dasd_path_get_fcs_str(fc_sec));
2032 	dasd_put_device(device);
2033 
2034 	return rc;
2035 }
2036 static DEVICE_ATTR(fc_security, 0444, dasd_device_fcs_show, NULL);
2037 
2038 static ssize_t
2039 dasd_path_fcs_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
2040 {
2041 	struct dasd_path *path = to_dasd_path(kobj);
2042 	unsigned int fc_sec = path->fc_security;
2043 
2044 	return sysfs_emit(buf, "%s\n", dasd_path_get_fcs_str(fc_sec));
2045 }
2046 
2047 static struct kobj_attribute path_fcs_attribute =
2048 	__ATTR(fc_security, 0444, dasd_path_fcs_show, NULL);
2049 
2050 /*
2051  * print copy relation in the form
2052  * primary,secondary[1] primary,secondary[2], ...
2053  */
2054 static ssize_t
2055 dasd_copy_pair_show(struct device *dev,
2056 		    struct device_attribute *attr, char *buf)
2057 {
2058 	char prim_busid[DASD_BUS_ID_SIZE];
2059 	struct dasd_copy_relation *copy;
2060 	struct dasd_devmap *devmap;
2061 	int len = 0;
2062 	int i;
2063 
2064 	devmap = dasd_find_busid(dev_name(dev));
2065 	if (IS_ERR(devmap))
2066 		return -ENODEV;
2067 
2068 	if (!devmap->copy)
2069 		return -ENODEV;
2070 
2071 	copy = devmap->copy;
2072 	/* find primary */
2073 	for (i = 0; i < DASD_CP_ENTRIES; i++) {
2074 		if (copy->entry[i].configured && copy->entry[i].primary) {
2075 			strscpy(prim_busid, copy->entry[i].busid,
2076 				DASD_BUS_ID_SIZE);
2077 			break;
2078 		}
2079 	}
2080 	if (i == DASD_CP_ENTRIES)
2081 		goto out;
2082 
2083 	/* print all secondary */
2084 	for (i = 0; i < DASD_CP_ENTRIES; i++) {
2085 		if (copy->entry[i].configured && !copy->entry[i].primary)
2086 			len += sysfs_emit_at(buf, len, "%s,%s ", prim_busid,
2087 					     copy->entry[i].busid);
2088 	}
2089 
2090 	len += sysfs_emit_at(buf, len, "\n");
2091 out:
2092 	return len;
2093 }
2094 
2095 static int dasd_devmap_set_copy_relation(struct dasd_devmap *devmap,
2096 					 struct dasd_copy_relation *copy,
2097 					 char *busid, bool primary)
2098 {
2099 	int i;
2100 
2101 	/* find free entry */
2102 	for (i = 0; i < DASD_CP_ENTRIES; i++) {
2103 		/* current bus_id already included, nothing to do */
2104 		if (copy->entry[i].configured &&
2105 		    strncmp(copy->entry[i].busid, busid, DASD_BUS_ID_SIZE) == 0)
2106 			return 0;
2107 
2108 		if (!copy->entry[i].configured)
2109 			break;
2110 	}
2111 	if (i == DASD_CP_ENTRIES)
2112 		return -EINVAL;
2113 
2114 	copy->entry[i].configured = true;
2115 	strscpy(copy->entry[i].busid, busid, DASD_BUS_ID_SIZE);
2116 	if (primary) {
2117 		copy->active = &copy->entry[i];
2118 		copy->entry[i].primary = true;
2119 	}
2120 	if (!devmap->copy)
2121 		devmap->copy = copy;
2122 
2123 	return 0;
2124 }
2125 
2126 static void dasd_devmap_del_copy_relation(struct dasd_copy_relation *copy,
2127 					  char *busid)
2128 {
2129 	int i;
2130 
2131 	spin_lock(&dasd_devmap_lock);
2132 	/* find entry */
2133 	for (i = 0; i < DASD_CP_ENTRIES; i++) {
2134 		if (copy->entry[i].configured &&
2135 		    strncmp(copy->entry[i].busid, busid, DASD_BUS_ID_SIZE) == 0)
2136 			break;
2137 	}
2138 	if (i == DASD_CP_ENTRIES || !copy->entry[i].configured) {
2139 		spin_unlock(&dasd_devmap_lock);
2140 		return;
2141 	}
2142 
2143 	copy->entry[i].configured = false;
2144 	memset(copy->entry[i].busid, 0, DASD_BUS_ID_SIZE);
2145 	if (copy->active == &copy->entry[i]) {
2146 		copy->active = NULL;
2147 		copy->entry[i].primary = false;
2148 	}
2149 	spin_unlock(&dasd_devmap_lock);
2150 }
2151 
2152 static int dasd_devmap_clear_copy_relation(struct device *dev)
2153 {
2154 	struct dasd_copy_relation *copy;
2155 	struct dasd_devmap *devmap;
2156 	int i, rc = 1;
2157 
2158 	devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
2159 	if (IS_ERR(devmap))
2160 		return 1;
2161 
2162 	spin_lock(&dasd_devmap_lock);
2163 	if (!devmap->copy)
2164 		goto out;
2165 
2166 	copy = devmap->copy;
2167 	/* first check if all secondary devices are offline*/
2168 	for (i = 0; i < DASD_CP_ENTRIES; i++) {
2169 		if (!copy->entry[i].configured)
2170 			continue;
2171 
2172 		if (copy->entry[i].device == copy->active->device)
2173 			continue;
2174 
2175 		if (copy->entry[i].device)
2176 			goto out;
2177 	}
2178 	/* clear all devmap entries */
2179 	for (i = 0; i < DASD_CP_ENTRIES; i++) {
2180 		if (strlen(copy->entry[i].busid) == 0)
2181 			continue;
2182 		if (copy->entry[i].device) {
2183 			dasd_put_device(copy->entry[i].device);
2184 			copy->entry[i].device->copy = NULL;
2185 			copy->entry[i].device = NULL;
2186 		}
2187 		devmap = dasd_find_busid_locked(copy->entry[i].busid);
2188 		devmap->copy = NULL;
2189 		memset(copy->entry[i].busid, 0, DASD_BUS_ID_SIZE);
2190 	}
2191 	kfree(copy);
2192 	rc = 0;
2193 out:
2194 	spin_unlock(&dasd_devmap_lock);
2195 	return rc;
2196 }
2197 
2198 /*
2199  * parse BUSIDs from a copy pair
2200  */
2201 static int dasd_devmap_parse_busid(const char *buf, char *prim_busid,
2202 				   char *sec_busid)
2203 {
2204 	char *primary, *secondary, *tmp, *pt;
2205 	int id0, id1, id2;
2206 
2207 	pt =  kstrdup(buf, GFP_KERNEL);
2208 	tmp = pt;
2209 	if (!tmp)
2210 		return -ENOMEM;
2211 
2212 	primary = strsep(&tmp, ",");
2213 	if (!primary) {
2214 		kfree(pt);
2215 		return -EINVAL;
2216 	}
2217 	secondary = strsep(&tmp, ",");
2218 	if (!secondary) {
2219 		kfree(pt);
2220 		return -EINVAL;
2221 	}
2222 	if (dasd_busid(primary, &id0, &id1, &id2)) {
2223 		kfree(pt);
2224 		return -EINVAL;
2225 	}
2226 	sprintf(prim_busid, "%01x.%01x.%04x", id0, id1, id2);
2227 	if (dasd_busid(secondary, &id0, &id1, &id2)) {
2228 		kfree(pt);
2229 		return -EINVAL;
2230 	}
2231 	sprintf(sec_busid, "%01x.%01x.%04x", id0, id1, id2);
2232 	kfree(pt);
2233 
2234 	return 0;
2235 }
2236 
2237 static ssize_t dasd_copy_pair_store(struct device *dev,
2238 				    struct device_attribute *attr,
2239 				    const char *buf, size_t count)
2240 {
2241 	struct dasd_devmap *prim_devmap, *sec_devmap;
2242 	char prim_busid[DASD_BUS_ID_SIZE];
2243 	char sec_busid[DASD_BUS_ID_SIZE];
2244 	struct dasd_copy_relation *copy;
2245 	struct dasd_device *device;
2246 	bool pprc_enabled;
2247 	int rc;
2248 
2249 	if (strncmp(buf, "clear", strlen("clear")) == 0) {
2250 		if (dasd_devmap_clear_copy_relation(dev))
2251 			return -EINVAL;
2252 		return count;
2253 	}
2254 
2255 	rc = dasd_devmap_parse_busid(buf, prim_busid, sec_busid);
2256 	if (rc)
2257 		return rc;
2258 
2259 	if (strncmp(dev_name(dev), prim_busid, DASD_BUS_ID_SIZE) != 0 &&
2260 	    strncmp(dev_name(dev), sec_busid, DASD_BUS_ID_SIZE) != 0)
2261 		return -EINVAL;
2262 
2263 	/* allocate primary devmap if needed */
2264 	prim_devmap = dasd_find_busid(prim_busid);
2265 	if (IS_ERR(prim_devmap))
2266 		prim_devmap = dasd_add_busid(prim_busid, DASD_FEATURE_DEFAULT);
2267 
2268 	/* allocate secondary devmap if needed */
2269 	sec_devmap = dasd_find_busid(sec_busid);
2270 	if (IS_ERR(sec_devmap))
2271 		sec_devmap = dasd_add_busid(sec_busid, DASD_FEATURE_DEFAULT);
2272 
2273 	/* setting copy relation is only allowed for offline secondary */
2274 	if (sec_devmap->device)
2275 		return -EINVAL;
2276 
2277 	if (prim_devmap->copy) {
2278 		copy = prim_devmap->copy;
2279 	} else if (sec_devmap->copy) {
2280 		copy = sec_devmap->copy;
2281 	} else {
2282 		copy = kzalloc(sizeof(*copy), GFP_KERNEL);
2283 		if (!copy)
2284 			return -ENOMEM;
2285 	}
2286 	spin_lock(&dasd_devmap_lock);
2287 	rc = dasd_devmap_set_copy_relation(prim_devmap, copy, prim_busid, true);
2288 	if (rc) {
2289 		spin_unlock(&dasd_devmap_lock);
2290 		return rc;
2291 	}
2292 	rc = dasd_devmap_set_copy_relation(sec_devmap, copy, sec_busid, false);
2293 	if (rc) {
2294 		spin_unlock(&dasd_devmap_lock);
2295 		return rc;
2296 	}
2297 	spin_unlock(&dasd_devmap_lock);
2298 
2299 	/* if primary device is already online call device setup directly */
2300 	if (prim_devmap->device && !prim_devmap->device->copy) {
2301 		device = prim_devmap->device;
2302 		if (device->discipline->pprc_enabled) {
2303 			pprc_enabled = device->discipline->pprc_enabled(device);
2304 			rc = dasd_devmap_set_device_copy_relation(device->cdev,
2305 								  pprc_enabled);
2306 		} else {
2307 			rc = -EOPNOTSUPP;
2308 		}
2309 	}
2310 	if (rc) {
2311 		dasd_devmap_del_copy_relation(copy, prim_busid);
2312 		dasd_devmap_del_copy_relation(copy, sec_busid);
2313 		count = rc;
2314 	}
2315 
2316 	return count;
2317 }
2318 static DEVICE_ATTR(copy_pair, 0644, dasd_copy_pair_show,
2319 		   dasd_copy_pair_store);
2320 
2321 static ssize_t
2322 dasd_copy_role_show(struct device *dev,
2323 		    struct device_attribute *attr, char *buf)
2324 {
2325 	struct dasd_copy_relation *copy;
2326 	struct dasd_device *device;
2327 	int len, i;
2328 
2329 	device = dasd_device_from_cdev(to_ccwdev(dev));
2330 	if (IS_ERR(device))
2331 		return -ENODEV;
2332 
2333 	if (!device->copy) {
2334 		len = sysfs_emit(buf, "none\n");
2335 		goto out;
2336 	}
2337 	copy = device->copy;
2338 	/* only the active device is primary */
2339 	if (copy->active->device == device) {
2340 		len = sysfs_emit(buf, "primary\n");
2341 		goto out;
2342 	}
2343 	for (i = 0; i < DASD_CP_ENTRIES; i++) {
2344 		if (copy->entry[i].device == device) {
2345 			len = sysfs_emit(buf, "secondary\n");
2346 			goto out;
2347 		}
2348 	}
2349 	/* not in the list, no COPY role */
2350 	len = sysfs_emit(buf, "none\n");
2351 out:
2352 	dasd_put_device(device);
2353 	return len;
2354 }
2355 static DEVICE_ATTR(copy_role, 0444, dasd_copy_role_show, NULL);
2356 
2357 static ssize_t dasd_device_ping(struct device *dev,
2358 				struct device_attribute *attr,
2359 				const char *buf, size_t count)
2360 {
2361 	struct dasd_device *device;
2362 	size_t rc;
2363 
2364 	device = dasd_device_from_cdev(to_ccwdev(dev));
2365 	if (IS_ERR(device))
2366 		return -ENODEV;
2367 
2368 	/*
2369 	 * do not try during offline processing
2370 	 * early check only
2371 	 * the sleep_on function itself checks for offline
2372 	 * processing again
2373 	 */
2374 	if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2375 		rc = -EBUSY;
2376 		goto out;
2377 	}
2378 	if (!device->discipline || !device->discipline->device_ping) {
2379 		rc = -EOPNOTSUPP;
2380 		goto out;
2381 	}
2382 	rc = device->discipline->device_ping(device);
2383 	if (!rc)
2384 		rc = count;
2385 out:
2386 	dasd_put_device(device);
2387 	return rc;
2388 }
2389 static DEVICE_ATTR(ping, 0200, NULL, dasd_device_ping);
2390 
2391 #define DASD_DEFINE_ATTR(_name, _func)					\
2392 static ssize_t dasd_##_name##_show(struct device *dev,			\
2393 				   struct device_attribute *attr,	\
2394 				   char *buf)				\
2395 {									\
2396 	struct ccw_device *cdev = to_ccwdev(dev);			\
2397 	struct dasd_device *device = dasd_device_from_cdev(cdev);	\
2398 	int val = 0;							\
2399 									\
2400 	if (IS_ERR(device))						\
2401 		return -ENODEV;						\
2402 	if (device->discipline && _func)				\
2403 		val = _func(device);					\
2404 	dasd_put_device(device);					\
2405 									\
2406 	return sysfs_emit(buf, "%d\n", val);			\
2407 }									\
2408 static DEVICE_ATTR(_name, 0444, dasd_##_name##_show, NULL);		\
2409 
2410 DASD_DEFINE_ATTR(ese, device->discipline->is_ese);
2411 DASD_DEFINE_ATTR(extent_size, device->discipline->ext_size);
2412 DASD_DEFINE_ATTR(pool_id, device->discipline->ext_pool_id);
2413 DASD_DEFINE_ATTR(space_configured, device->discipline->space_configured);
2414 DASD_DEFINE_ATTR(space_allocated, device->discipline->space_allocated);
2415 DASD_DEFINE_ATTR(logical_capacity, device->discipline->logical_capacity);
2416 DASD_DEFINE_ATTR(warn_threshold, device->discipline->ext_pool_warn_thrshld);
2417 DASD_DEFINE_ATTR(cap_at_warnlevel, device->discipline->ext_pool_cap_at_warnlevel);
2418 DASD_DEFINE_ATTR(pool_oos, device->discipline->ext_pool_oos);
2419 
2420 static struct attribute * dasd_attrs[] = {
2421 	&dev_attr_readonly.attr,
2422 	&dev_attr_discipline.attr,
2423 	&dev_attr_status.attr,
2424 	&dev_attr_alias.attr,
2425 	&dev_attr_vendor.attr,
2426 	&dev_attr_uid.attr,
2427 	&dev_attr_use_diag.attr,
2428 	&dev_attr_raw_track_access.attr,
2429 	&dev_attr_eer_enabled.attr,
2430 	&dev_attr_erplog.attr,
2431 	&dev_attr_failfast.attr,
2432 	&dev_attr_expires.attr,
2433 	&dev_attr_retries.attr,
2434 	&dev_attr_timeout.attr,
2435 	&dev_attr_reservation_policy.attr,
2436 	&dev_attr_last_known_reservation_state.attr,
2437 	&dev_attr_safe_offline.attr,
2438 	&dev_attr_host_access_count.attr,
2439 	&dev_attr_path_masks.attr,
2440 	&dev_attr_path_threshold.attr,
2441 	&dev_attr_path_autodisable.attr,
2442 	&dev_attr_path_interval.attr,
2443 	&dev_attr_path_reset.attr,
2444 	&dev_attr_hpf.attr,
2445 	&dev_attr_ese.attr,
2446 	&dev_attr_fc_security.attr,
2447 	&dev_attr_copy_pair.attr,
2448 	&dev_attr_copy_role.attr,
2449 	&dev_attr_ping.attr,
2450 	&dev_attr_aq_mask.attr,
2451 	&dev_attr_aq_requeue.attr,
2452 	&dev_attr_aq_timeouts.attr,
2453 	NULL,
2454 };
2455 
2456 static const struct attribute_group dasd_attr_group = {
2457 	.attrs = dasd_attrs,
2458 };
2459 
2460 static struct attribute *capacity_attrs[] = {
2461 	&dev_attr_space_configured.attr,
2462 	&dev_attr_space_allocated.attr,
2463 	&dev_attr_logical_capacity.attr,
2464 	NULL,
2465 };
2466 
2467 static const struct attribute_group capacity_attr_group = {
2468 	.name = "capacity",
2469 	.attrs = capacity_attrs,
2470 };
2471 
2472 static struct attribute *ext_pool_attrs[] = {
2473 	&dev_attr_pool_id.attr,
2474 	&dev_attr_extent_size.attr,
2475 	&dev_attr_warn_threshold.attr,
2476 	&dev_attr_cap_at_warnlevel.attr,
2477 	&dev_attr_pool_oos.attr,
2478 	NULL,
2479 };
2480 
2481 static const struct attribute_group ext_pool_attr_group = {
2482 	.name = "extent_pool",
2483 	.attrs = ext_pool_attrs,
2484 };
2485 
2486 const struct attribute_group *dasd_dev_groups[] = {
2487 	&dasd_attr_group,
2488 	&capacity_attr_group,
2489 	&ext_pool_attr_group,
2490 	NULL,
2491 };
2492 EXPORT_SYMBOL_GPL(dasd_dev_groups);
2493 
2494 /*
2495  * Return value of the specified feature.
2496  */
2497 int
2498 dasd_get_feature(struct ccw_device *cdev, int feature)
2499 {
2500 	struct dasd_devmap *devmap;
2501 
2502 	devmap = dasd_find_busid(dev_name(&cdev->dev));
2503 	if (IS_ERR(devmap))
2504 		return PTR_ERR(devmap);
2505 
2506 	return ((devmap->features & feature) != 0);
2507 }
2508 
2509 /*
2510  * Set / reset given feature.
2511  * Flag indicates whether to set (!=0) or the reset (=0) the feature.
2512  */
2513 int
2514 dasd_set_feature(struct ccw_device *cdev, int feature, int flag)
2515 {
2516 	struct dasd_devmap *devmap;
2517 
2518 	devmap = dasd_devmap_from_cdev(cdev);
2519 	if (IS_ERR(devmap))
2520 		return PTR_ERR(devmap);
2521 
2522 	spin_lock(&dasd_devmap_lock);
2523 	if (flag)
2524 		devmap->features |= feature;
2525 	else
2526 		devmap->features &= ~feature;
2527 	if (devmap->device)
2528 		devmap->device->features = devmap->features;
2529 	spin_unlock(&dasd_devmap_lock);
2530 	return 0;
2531 }
2532 EXPORT_SYMBOL(dasd_set_feature);
2533 
2534 static struct attribute *paths_info_attrs[] = {
2535 	&path_fcs_attribute.attr,
2536 	NULL,
2537 };
2538 ATTRIBUTE_GROUPS(paths_info);
2539 
2540 static struct kobj_type path_attr_type = {
2541 	.release	= dasd_path_release,
2542 	.default_groups	= paths_info_groups,
2543 	.sysfs_ops	= &kobj_sysfs_ops,
2544 };
2545 
2546 static void dasd_path_init_kobj(struct dasd_device *device, int chp)
2547 {
2548 	device->path[chp].kobj.kset = device->paths_info;
2549 	kobject_init(&device->path[chp].kobj, &path_attr_type);
2550 }
2551 
2552 void dasd_path_create_kobj(struct dasd_device *device, int chp)
2553 {
2554 	int rc;
2555 
2556 	if (test_bit(DASD_FLAG_OFFLINE, &device->flags))
2557 		return;
2558 	if (!device->paths_info) {
2559 		dev_warn(&device->cdev->dev, "Unable to create paths objects\n");
2560 		return;
2561 	}
2562 	if (device->path[chp].in_sysfs)
2563 		return;
2564 	if (!device->path[chp].conf_data)
2565 		return;
2566 
2567 	dasd_path_init_kobj(device, chp);
2568 
2569 	rc = kobject_add(&device->path[chp].kobj, NULL, "%x.%02x",
2570 			 device->path[chp].cssid, device->path[chp].chpid);
2571 	if (rc)
2572 		kobject_put(&device->path[chp].kobj);
2573 	device->path[chp].in_sysfs = true;
2574 }
2575 EXPORT_SYMBOL(dasd_path_create_kobj);
2576 
2577 void dasd_path_create_kobjects(struct dasd_device *device)
2578 {
2579 	u8 lpm, opm;
2580 
2581 	opm = dasd_path_get_opm(device);
2582 	for (lpm = 0x80; lpm; lpm >>= 1) {
2583 		if (!(lpm & opm))
2584 			continue;
2585 		dasd_path_create_kobj(device, pathmask_to_pos(lpm));
2586 	}
2587 }
2588 EXPORT_SYMBOL(dasd_path_create_kobjects);
2589 
2590 static void dasd_path_remove_kobj(struct dasd_device *device, int chp)
2591 {
2592 	if (device->path[chp].in_sysfs) {
2593 		kobject_put(&device->path[chp].kobj);
2594 		device->path[chp].in_sysfs = false;
2595 	}
2596 }
2597 
2598 /*
2599  * As we keep kobjects for the lifetime of a device, this function must not be
2600  * called anywhere but in the context of offlining a device.
2601  */
2602 void dasd_path_remove_kobjects(struct dasd_device *device)
2603 {
2604 	int i;
2605 
2606 	for (i = 0; i < 8; i++)
2607 		dasd_path_remove_kobj(device, i);
2608 }
2609 EXPORT_SYMBOL(dasd_path_remove_kobjects);
2610 
2611 int
2612 dasd_devmap_init(void)
2613 {
2614 	int i;
2615 
2616 	/* Initialize devmap structures. */
2617 	dasd_max_devindex = 0;
2618 	for (i = 0; i < 256; i++)
2619 		INIT_LIST_HEAD(&dasd_hashlists[i]);
2620 	return 0;
2621 }
2622 
2623 void
2624 dasd_devmap_exit(void)
2625 {
2626 	dasd_forget_ranges();
2627 }
2628