xref: /openbmc/linux/drivers/s390/block/dasd_eckd.c (revision 002dff36)
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, 2009
9  * EMC Symmetrix ioctl Copyright EMC Corporation, 2008
10  * Author.........: Nigel Hislop <hislop_nigel@emc.com>
11  */
12 
13 #define KMSG_COMPONENT "dasd-eckd"
14 
15 #include <linux/stddef.h>
16 #include <linux/kernel.h>
17 #include <linux/slab.h>
18 #include <linux/hdreg.h>	/* HDIO_GETGEO			    */
19 #include <linux/bio.h>
20 #include <linux/module.h>
21 #include <linux/compat.h>
22 #include <linux/init.h>
23 #include <linux/seq_file.h>
24 
25 #include <asm/css_chars.h>
26 #include <asm/debug.h>
27 #include <asm/idals.h>
28 #include <asm/ebcdic.h>
29 #include <asm/io.h>
30 #include <linux/uaccess.h>
31 #include <asm/cio.h>
32 #include <asm/ccwdev.h>
33 #include <asm/itcw.h>
34 #include <asm/schid.h>
35 #include <asm/chpid.h>
36 
37 #include "dasd_int.h"
38 #include "dasd_eckd.h"
39 
40 #ifdef PRINTK_HEADER
41 #undef PRINTK_HEADER
42 #endif				/* PRINTK_HEADER */
43 #define PRINTK_HEADER "dasd(eckd):"
44 
45 /*
46  * raw track access always map to 64k in memory
47  * so it maps to 16 blocks of 4k per track
48  */
49 #define DASD_RAW_BLOCK_PER_TRACK 16
50 #define DASD_RAW_BLOCKSIZE 4096
51 /* 64k are 128 x 512 byte sectors  */
52 #define DASD_RAW_SECTORS_PER_TRACK 128
53 
54 MODULE_LICENSE("GPL");
55 
56 static struct dasd_discipline dasd_eckd_discipline;
57 
58 /* The ccw bus type uses this table to find devices that it sends to
59  * dasd_eckd_probe */
60 static struct ccw_device_id dasd_eckd_ids[] = {
61 	{ CCW_DEVICE_DEVTYPE (0x3990, 0, 0x3390, 0), .driver_info = 0x1},
62 	{ CCW_DEVICE_DEVTYPE (0x2105, 0, 0x3390, 0), .driver_info = 0x2},
63 	{ CCW_DEVICE_DEVTYPE (0x3880, 0, 0x3380, 0), .driver_info = 0x3},
64 	{ CCW_DEVICE_DEVTYPE (0x3990, 0, 0x3380, 0), .driver_info = 0x4},
65 	{ CCW_DEVICE_DEVTYPE (0x2105, 0, 0x3380, 0), .driver_info = 0x5},
66 	{ CCW_DEVICE_DEVTYPE (0x9343, 0, 0x9345, 0), .driver_info = 0x6},
67 	{ CCW_DEVICE_DEVTYPE (0x2107, 0, 0x3390, 0), .driver_info = 0x7},
68 	{ CCW_DEVICE_DEVTYPE (0x2107, 0, 0x3380, 0), .driver_info = 0x8},
69 	{ CCW_DEVICE_DEVTYPE (0x1750, 0, 0x3390, 0), .driver_info = 0x9},
70 	{ CCW_DEVICE_DEVTYPE (0x1750, 0, 0x3380, 0), .driver_info = 0xa},
71 	{ /* end of list */ },
72 };
73 
74 MODULE_DEVICE_TABLE(ccw, dasd_eckd_ids);
75 
76 static struct ccw_driver dasd_eckd_driver; /* see below */
77 
78 static void *rawpadpage;
79 
80 #define INIT_CQR_OK 0
81 #define INIT_CQR_UNFORMATTED 1
82 #define INIT_CQR_ERROR 2
83 
84 /* emergency request for reserve/release */
85 static struct {
86 	struct dasd_ccw_req cqr;
87 	struct ccw1 ccw;
88 	char data[32];
89 } *dasd_reserve_req;
90 static DEFINE_MUTEX(dasd_reserve_mutex);
91 
92 static struct {
93 	struct dasd_ccw_req cqr;
94 	struct ccw1 ccw[2];
95 	char data[40];
96 } *dasd_vol_info_req;
97 static DEFINE_MUTEX(dasd_vol_info_mutex);
98 
99 struct ext_pool_exhaust_work_data {
100 	struct work_struct worker;
101 	struct dasd_device *device;
102 	struct dasd_device *base;
103 };
104 
105 /* definitions for the path verification worker */
106 struct path_verification_work_data {
107 	struct work_struct worker;
108 	struct dasd_device *device;
109 	struct dasd_ccw_req cqr;
110 	struct ccw1 ccw;
111 	__u8 rcd_buffer[DASD_ECKD_RCD_DATA_SIZE];
112 	int isglobal;
113 	__u8 tbvpm;
114 };
115 static struct path_verification_work_data *path_verification_worker;
116 static DEFINE_MUTEX(dasd_path_verification_mutex);
117 
118 struct check_attention_work_data {
119 	struct work_struct worker;
120 	struct dasd_device *device;
121 	__u8 lpum;
122 };
123 
124 static int dasd_eckd_ext_pool_id(struct dasd_device *);
125 static int prepare_itcw(struct itcw *, unsigned int, unsigned int, int,
126 			struct dasd_device *, struct dasd_device *,
127 			unsigned int, int, unsigned int, unsigned int,
128 			unsigned int, unsigned int);
129 
130 /* initial attempt at a probe function. this can be simplified once
131  * the other detection code is gone */
132 static int
133 dasd_eckd_probe (struct ccw_device *cdev)
134 {
135 	int ret;
136 
137 	/* set ECKD specific ccw-device options */
138 	ret = ccw_device_set_options(cdev, CCWDEV_ALLOW_FORCE |
139 				     CCWDEV_DO_PATHGROUP | CCWDEV_DO_MULTIPATH);
140 	if (ret) {
141 		DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
142 				"dasd_eckd_probe: could not set "
143 				"ccw-device options");
144 		return ret;
145 	}
146 	ret = dasd_generic_probe(cdev, &dasd_eckd_discipline);
147 	return ret;
148 }
149 
150 static int
151 dasd_eckd_set_online(struct ccw_device *cdev)
152 {
153 	return dasd_generic_set_online(cdev, &dasd_eckd_discipline);
154 }
155 
156 static const int sizes_trk0[] = { 28, 148, 84 };
157 #define LABEL_SIZE 140
158 
159 /* head and record addresses of count_area read in analysis ccw */
160 static const int count_area_head[] = { 0, 0, 0, 0, 1 };
161 static const int count_area_rec[] = { 1, 2, 3, 4, 1 };
162 
163 static inline unsigned int
164 ceil_quot(unsigned int d1, unsigned int d2)
165 {
166 	return (d1 + (d2 - 1)) / d2;
167 }
168 
169 static unsigned int
170 recs_per_track(struct dasd_eckd_characteristics * rdc,
171 	       unsigned int kl, unsigned int dl)
172 {
173 	int dn, kn;
174 
175 	switch (rdc->dev_type) {
176 	case 0x3380:
177 		if (kl)
178 			return 1499 / (15 + 7 + ceil_quot(kl + 12, 32) +
179 				       ceil_quot(dl + 12, 32));
180 		else
181 			return 1499 / (15 + ceil_quot(dl + 12, 32));
182 	case 0x3390:
183 		dn = ceil_quot(dl + 6, 232) + 1;
184 		if (kl) {
185 			kn = ceil_quot(kl + 6, 232) + 1;
186 			return 1729 / (10 + 9 + ceil_quot(kl + 6 * kn, 34) +
187 				       9 + ceil_quot(dl + 6 * dn, 34));
188 		} else
189 			return 1729 / (10 + 9 + ceil_quot(dl + 6 * dn, 34));
190 	case 0x9345:
191 		dn = ceil_quot(dl + 6, 232) + 1;
192 		if (kl) {
193 			kn = ceil_quot(kl + 6, 232) + 1;
194 			return 1420 / (18 + 7 + ceil_quot(kl + 6 * kn, 34) +
195 				       ceil_quot(dl + 6 * dn, 34));
196 		} else
197 			return 1420 / (18 + 7 + ceil_quot(dl + 6 * dn, 34));
198 	}
199 	return 0;
200 }
201 
202 static void set_ch_t(struct ch_t *geo, __u32 cyl, __u8 head)
203 {
204 	geo->cyl = (__u16) cyl;
205 	geo->head = cyl >> 16;
206 	geo->head <<= 4;
207 	geo->head |= head;
208 }
209 
210 /*
211  * calculate failing track from sense data depending if
212  * it is an EAV device or not
213  */
214 static int dasd_eckd_track_from_irb(struct irb *irb, struct dasd_device *device,
215 				    sector_t *track)
216 {
217 	struct dasd_eckd_private *private = device->private;
218 	u8 *sense = NULL;
219 	u32 cyl;
220 	u8 head;
221 
222 	sense = dasd_get_sense(irb);
223 	if (!sense) {
224 		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
225 			      "ESE error no sense data\n");
226 		return -EINVAL;
227 	}
228 	if (!(sense[27] & DASD_SENSE_BIT_2)) {
229 		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
230 			      "ESE error no valid track data\n");
231 		return -EINVAL;
232 	}
233 
234 	if (sense[27] & DASD_SENSE_BIT_3) {
235 		/* enhanced addressing */
236 		cyl = sense[30] << 20;
237 		cyl |= (sense[31] & 0xF0) << 12;
238 		cyl |= sense[28] << 8;
239 		cyl |= sense[29];
240 	} else {
241 		cyl = sense[29] << 8;
242 		cyl |= sense[30];
243 	}
244 	head = sense[31] & 0x0F;
245 	*track = cyl * private->rdc_data.trk_per_cyl + head;
246 	return 0;
247 }
248 
249 static int set_timestamp(struct ccw1 *ccw, struct DE_eckd_data *data,
250 		     struct dasd_device *device)
251 {
252 	struct dasd_eckd_private *private = device->private;
253 	int rc;
254 
255 	rc = get_phys_clock(&data->ep_sys_time);
256 	/*
257 	 * Ignore return code if XRC is not supported or
258 	 * sync clock is switched off
259 	 */
260 	if ((rc && !private->rdc_data.facilities.XRC_supported) ||
261 	    rc == -EOPNOTSUPP || rc == -EACCES)
262 		return 0;
263 
264 	/* switch on System Time Stamp - needed for XRC Support */
265 	data->ga_extended |= 0x08; /* switch on 'Time Stamp Valid'   */
266 	data->ga_extended |= 0x02; /* switch on 'Extended Parameter' */
267 
268 	if (ccw) {
269 		ccw->count = sizeof(struct DE_eckd_data);
270 		ccw->flags |= CCW_FLAG_SLI;
271 	}
272 
273 	return rc;
274 }
275 
276 static int
277 define_extent(struct ccw1 *ccw, struct DE_eckd_data *data, unsigned int trk,
278 	      unsigned int totrk, int cmd, struct dasd_device *device,
279 	      int blksize)
280 {
281 	struct dasd_eckd_private *private = device->private;
282 	u16 heads, beghead, endhead;
283 	u32 begcyl, endcyl;
284 	int rc = 0;
285 
286 	if (ccw) {
287 		ccw->cmd_code = DASD_ECKD_CCW_DEFINE_EXTENT;
288 		ccw->flags = 0;
289 		ccw->count = 16;
290 		ccw->cda = (__u32)__pa(data);
291 	}
292 
293 	memset(data, 0, sizeof(struct DE_eckd_data));
294 	switch (cmd) {
295 	case DASD_ECKD_CCW_READ_HOME_ADDRESS:
296 	case DASD_ECKD_CCW_READ_RECORD_ZERO:
297 	case DASD_ECKD_CCW_READ:
298 	case DASD_ECKD_CCW_READ_MT:
299 	case DASD_ECKD_CCW_READ_CKD:
300 	case DASD_ECKD_CCW_READ_CKD_MT:
301 	case DASD_ECKD_CCW_READ_KD:
302 	case DASD_ECKD_CCW_READ_KD_MT:
303 		data->mask.perm = 0x1;
304 		data->attributes.operation = private->attrib.operation;
305 		break;
306 	case DASD_ECKD_CCW_READ_COUNT:
307 		data->mask.perm = 0x1;
308 		data->attributes.operation = DASD_BYPASS_CACHE;
309 		break;
310 	case DASD_ECKD_CCW_READ_TRACK:
311 	case DASD_ECKD_CCW_READ_TRACK_DATA:
312 		data->mask.perm = 0x1;
313 		data->attributes.operation = private->attrib.operation;
314 		data->blk_size = 0;
315 		break;
316 	case DASD_ECKD_CCW_WRITE:
317 	case DASD_ECKD_CCW_WRITE_MT:
318 	case DASD_ECKD_CCW_WRITE_KD:
319 	case DASD_ECKD_CCW_WRITE_KD_MT:
320 		data->mask.perm = 0x02;
321 		data->attributes.operation = private->attrib.operation;
322 		rc = set_timestamp(ccw, data, device);
323 		break;
324 	case DASD_ECKD_CCW_WRITE_CKD:
325 	case DASD_ECKD_CCW_WRITE_CKD_MT:
326 		data->attributes.operation = DASD_BYPASS_CACHE;
327 		rc = set_timestamp(ccw, data, device);
328 		break;
329 	case DASD_ECKD_CCW_ERASE:
330 	case DASD_ECKD_CCW_WRITE_HOME_ADDRESS:
331 	case DASD_ECKD_CCW_WRITE_RECORD_ZERO:
332 		data->mask.perm = 0x3;
333 		data->mask.auth = 0x1;
334 		data->attributes.operation = DASD_BYPASS_CACHE;
335 		rc = set_timestamp(ccw, data, device);
336 		break;
337 	case DASD_ECKD_CCW_WRITE_FULL_TRACK:
338 		data->mask.perm = 0x03;
339 		data->attributes.operation = private->attrib.operation;
340 		data->blk_size = 0;
341 		break;
342 	case DASD_ECKD_CCW_WRITE_TRACK_DATA:
343 		data->mask.perm = 0x02;
344 		data->attributes.operation = private->attrib.operation;
345 		data->blk_size = blksize;
346 		rc = set_timestamp(ccw, data, device);
347 		break;
348 	default:
349 		dev_err(&device->cdev->dev,
350 			"0x%x is not a known command\n", cmd);
351 		break;
352 	}
353 
354 	data->attributes.mode = 0x3;	/* ECKD */
355 
356 	if ((private->rdc_data.cu_type == 0x2105 ||
357 	     private->rdc_data.cu_type == 0x2107 ||
358 	     private->rdc_data.cu_type == 0x1750)
359 	    && !(private->uses_cdl && trk < 2))
360 		data->ga_extended |= 0x40; /* Regular Data Format Mode */
361 
362 	heads = private->rdc_data.trk_per_cyl;
363 	begcyl = trk / heads;
364 	beghead = trk % heads;
365 	endcyl = totrk / heads;
366 	endhead = totrk % heads;
367 
368 	/* check for sequential prestage - enhance cylinder range */
369 	if (data->attributes.operation == DASD_SEQ_PRESTAGE ||
370 	    data->attributes.operation == DASD_SEQ_ACCESS) {
371 
372 		if (endcyl + private->attrib.nr_cyl < private->real_cyl)
373 			endcyl += private->attrib.nr_cyl;
374 		else
375 			endcyl = (private->real_cyl - 1);
376 	}
377 
378 	set_ch_t(&data->beg_ext, begcyl, beghead);
379 	set_ch_t(&data->end_ext, endcyl, endhead);
380 	return rc;
381 }
382 
383 
384 static void locate_record_ext(struct ccw1 *ccw, struct LRE_eckd_data *data,
385 			      unsigned int trk, unsigned int rec_on_trk,
386 			      int count, int cmd, struct dasd_device *device,
387 			      unsigned int reclen, unsigned int tlf)
388 {
389 	struct dasd_eckd_private *private = device->private;
390 	int sector;
391 	int dn, d;
392 
393 	if (ccw) {
394 		ccw->cmd_code = DASD_ECKD_CCW_LOCATE_RECORD_EXT;
395 		ccw->flags = 0;
396 		if (cmd == DASD_ECKD_CCW_WRITE_FULL_TRACK)
397 			ccw->count = 22;
398 		else
399 			ccw->count = 20;
400 		ccw->cda = (__u32)__pa(data);
401 	}
402 
403 	memset(data, 0, sizeof(*data));
404 	sector = 0;
405 	if (rec_on_trk) {
406 		switch (private->rdc_data.dev_type) {
407 		case 0x3390:
408 			dn = ceil_quot(reclen + 6, 232);
409 			d = 9 + ceil_quot(reclen + 6 * (dn + 1), 34);
410 			sector = (49 + (rec_on_trk - 1) * (10 + d)) / 8;
411 			break;
412 		case 0x3380:
413 			d = 7 + ceil_quot(reclen + 12, 32);
414 			sector = (39 + (rec_on_trk - 1) * (8 + d)) / 7;
415 			break;
416 		}
417 	}
418 	data->sector = sector;
419 	/* note: meaning of count depends on the operation
420 	 *	 for record based I/O it's the number of records, but for
421 	 *	 track based I/O it's the number of tracks
422 	 */
423 	data->count = count;
424 	switch (cmd) {
425 	case DASD_ECKD_CCW_WRITE_HOME_ADDRESS:
426 		data->operation.orientation = 0x3;
427 		data->operation.operation = 0x03;
428 		break;
429 	case DASD_ECKD_CCW_READ_HOME_ADDRESS:
430 		data->operation.orientation = 0x3;
431 		data->operation.operation = 0x16;
432 		break;
433 	case DASD_ECKD_CCW_WRITE_RECORD_ZERO:
434 		data->operation.orientation = 0x1;
435 		data->operation.operation = 0x03;
436 		data->count++;
437 		break;
438 	case DASD_ECKD_CCW_READ_RECORD_ZERO:
439 		data->operation.orientation = 0x3;
440 		data->operation.operation = 0x16;
441 		data->count++;
442 		break;
443 	case DASD_ECKD_CCW_WRITE:
444 	case DASD_ECKD_CCW_WRITE_MT:
445 	case DASD_ECKD_CCW_WRITE_KD:
446 	case DASD_ECKD_CCW_WRITE_KD_MT:
447 		data->auxiliary.length_valid = 0x1;
448 		data->length = reclen;
449 		data->operation.operation = 0x01;
450 		break;
451 	case DASD_ECKD_CCW_WRITE_CKD:
452 	case DASD_ECKD_CCW_WRITE_CKD_MT:
453 		data->auxiliary.length_valid = 0x1;
454 		data->length = reclen;
455 		data->operation.operation = 0x03;
456 		break;
457 	case DASD_ECKD_CCW_WRITE_FULL_TRACK:
458 		data->operation.orientation = 0x0;
459 		data->operation.operation = 0x3F;
460 		data->extended_operation = 0x11;
461 		data->length = 0;
462 		data->extended_parameter_length = 0x02;
463 		if (data->count > 8) {
464 			data->extended_parameter[0] = 0xFF;
465 			data->extended_parameter[1] = 0xFF;
466 			data->extended_parameter[1] <<= (16 - count);
467 		} else {
468 			data->extended_parameter[0] = 0xFF;
469 			data->extended_parameter[0] <<= (8 - count);
470 			data->extended_parameter[1] = 0x00;
471 		}
472 		data->sector = 0xFF;
473 		break;
474 	case DASD_ECKD_CCW_WRITE_TRACK_DATA:
475 		data->auxiliary.length_valid = 0x1;
476 		data->length = reclen;	/* not tlf, as one might think */
477 		data->operation.operation = 0x3F;
478 		data->extended_operation = 0x23;
479 		break;
480 	case DASD_ECKD_CCW_READ:
481 	case DASD_ECKD_CCW_READ_MT:
482 	case DASD_ECKD_CCW_READ_KD:
483 	case DASD_ECKD_CCW_READ_KD_MT:
484 		data->auxiliary.length_valid = 0x1;
485 		data->length = reclen;
486 		data->operation.operation = 0x06;
487 		break;
488 	case DASD_ECKD_CCW_READ_CKD:
489 	case DASD_ECKD_CCW_READ_CKD_MT:
490 		data->auxiliary.length_valid = 0x1;
491 		data->length = reclen;
492 		data->operation.operation = 0x16;
493 		break;
494 	case DASD_ECKD_CCW_READ_COUNT:
495 		data->operation.operation = 0x06;
496 		break;
497 	case DASD_ECKD_CCW_READ_TRACK:
498 		data->operation.orientation = 0x1;
499 		data->operation.operation = 0x0C;
500 		data->extended_parameter_length = 0;
501 		data->sector = 0xFF;
502 		break;
503 	case DASD_ECKD_CCW_READ_TRACK_DATA:
504 		data->auxiliary.length_valid = 0x1;
505 		data->length = tlf;
506 		data->operation.operation = 0x0C;
507 		break;
508 	case DASD_ECKD_CCW_ERASE:
509 		data->length = reclen;
510 		data->auxiliary.length_valid = 0x1;
511 		data->operation.operation = 0x0b;
512 		break;
513 	default:
514 		DBF_DEV_EVENT(DBF_ERR, device,
515 			    "fill LRE unknown opcode 0x%x", cmd);
516 		BUG();
517 	}
518 	set_ch_t(&data->seek_addr,
519 		 trk / private->rdc_data.trk_per_cyl,
520 		 trk % private->rdc_data.trk_per_cyl);
521 	data->search_arg.cyl = data->seek_addr.cyl;
522 	data->search_arg.head = data->seek_addr.head;
523 	data->search_arg.record = rec_on_trk;
524 }
525 
526 static int prefix_LRE(struct ccw1 *ccw, struct PFX_eckd_data *pfxdata,
527 		      unsigned int trk, unsigned int totrk, int cmd,
528 		      struct dasd_device *basedev, struct dasd_device *startdev,
529 		      unsigned int format, unsigned int rec_on_trk, int count,
530 		      unsigned int blksize, unsigned int tlf)
531 {
532 	struct dasd_eckd_private *basepriv, *startpriv;
533 	struct LRE_eckd_data *lredata;
534 	struct DE_eckd_data *dedata;
535 	int rc = 0;
536 
537 	basepriv = basedev->private;
538 	startpriv = startdev->private;
539 	dedata = &pfxdata->define_extent;
540 	lredata = &pfxdata->locate_record;
541 
542 	ccw->cmd_code = DASD_ECKD_CCW_PFX;
543 	ccw->flags = 0;
544 	if (cmd == DASD_ECKD_CCW_WRITE_FULL_TRACK) {
545 		ccw->count = sizeof(*pfxdata) + 2;
546 		ccw->cda = (__u32) __pa(pfxdata);
547 		memset(pfxdata, 0, sizeof(*pfxdata) + 2);
548 	} else {
549 		ccw->count = sizeof(*pfxdata);
550 		ccw->cda = (__u32) __pa(pfxdata);
551 		memset(pfxdata, 0, sizeof(*pfxdata));
552 	}
553 
554 	/* prefix data */
555 	if (format > 1) {
556 		DBF_DEV_EVENT(DBF_ERR, basedev,
557 			      "PFX LRE unknown format 0x%x", format);
558 		BUG();
559 		return -EINVAL;
560 	}
561 	pfxdata->format = format;
562 	pfxdata->base_address = basepriv->ned->unit_addr;
563 	pfxdata->base_lss = basepriv->ned->ID;
564 	pfxdata->validity.define_extent = 1;
565 
566 	/* private uid is kept up to date, conf_data may be outdated */
567 	if (startpriv->uid.type == UA_BASE_PAV_ALIAS)
568 		pfxdata->validity.verify_base = 1;
569 
570 	if (startpriv->uid.type == UA_HYPER_PAV_ALIAS) {
571 		pfxdata->validity.verify_base = 1;
572 		pfxdata->validity.hyper_pav = 1;
573 	}
574 
575 	rc = define_extent(NULL, dedata, trk, totrk, cmd, basedev, blksize);
576 
577 	/*
578 	 * For some commands the System Time Stamp is set in the define extent
579 	 * data when XRC is supported. The validity of the time stamp must be
580 	 * reflected in the prefix data as well.
581 	 */
582 	if (dedata->ga_extended & 0x08 && dedata->ga_extended & 0x02)
583 		pfxdata->validity.time_stamp = 1; /* 'Time Stamp Valid'   */
584 
585 	if (format == 1) {
586 		locate_record_ext(NULL, lredata, trk, rec_on_trk, count, cmd,
587 				  basedev, blksize, tlf);
588 	}
589 
590 	return rc;
591 }
592 
593 static int prefix(struct ccw1 *ccw, struct PFX_eckd_data *pfxdata,
594 		  unsigned int trk, unsigned int totrk, int cmd,
595 		  struct dasd_device *basedev, struct dasd_device *startdev)
596 {
597 	return prefix_LRE(ccw, pfxdata, trk, totrk, cmd, basedev, startdev,
598 			  0, 0, 0, 0, 0);
599 }
600 
601 static void
602 locate_record(struct ccw1 *ccw, struct LO_eckd_data *data, unsigned int trk,
603 	      unsigned int rec_on_trk, int no_rec, int cmd,
604 	      struct dasd_device * device, int reclen)
605 {
606 	struct dasd_eckd_private *private = device->private;
607 	int sector;
608 	int dn, d;
609 
610 	DBF_DEV_EVENT(DBF_INFO, device,
611 		  "Locate: trk %d, rec %d, no_rec %d, cmd %d, reclen %d",
612 		  trk, rec_on_trk, no_rec, cmd, reclen);
613 
614 	ccw->cmd_code = DASD_ECKD_CCW_LOCATE_RECORD;
615 	ccw->flags = 0;
616 	ccw->count = 16;
617 	ccw->cda = (__u32) __pa(data);
618 
619 	memset(data, 0, sizeof(struct LO_eckd_data));
620 	sector = 0;
621 	if (rec_on_trk) {
622 		switch (private->rdc_data.dev_type) {
623 		case 0x3390:
624 			dn = ceil_quot(reclen + 6, 232);
625 			d = 9 + ceil_quot(reclen + 6 * (dn + 1), 34);
626 			sector = (49 + (rec_on_trk - 1) * (10 + d)) / 8;
627 			break;
628 		case 0x3380:
629 			d = 7 + ceil_quot(reclen + 12, 32);
630 			sector = (39 + (rec_on_trk - 1) * (8 + d)) / 7;
631 			break;
632 		}
633 	}
634 	data->sector = sector;
635 	data->count = no_rec;
636 	switch (cmd) {
637 	case DASD_ECKD_CCW_WRITE_HOME_ADDRESS:
638 		data->operation.orientation = 0x3;
639 		data->operation.operation = 0x03;
640 		break;
641 	case DASD_ECKD_CCW_READ_HOME_ADDRESS:
642 		data->operation.orientation = 0x3;
643 		data->operation.operation = 0x16;
644 		break;
645 	case DASD_ECKD_CCW_WRITE_RECORD_ZERO:
646 		data->operation.orientation = 0x1;
647 		data->operation.operation = 0x03;
648 		data->count++;
649 		break;
650 	case DASD_ECKD_CCW_READ_RECORD_ZERO:
651 		data->operation.orientation = 0x3;
652 		data->operation.operation = 0x16;
653 		data->count++;
654 		break;
655 	case DASD_ECKD_CCW_WRITE:
656 	case DASD_ECKD_CCW_WRITE_MT:
657 	case DASD_ECKD_CCW_WRITE_KD:
658 	case DASD_ECKD_CCW_WRITE_KD_MT:
659 		data->auxiliary.last_bytes_used = 0x1;
660 		data->length = reclen;
661 		data->operation.operation = 0x01;
662 		break;
663 	case DASD_ECKD_CCW_WRITE_CKD:
664 	case DASD_ECKD_CCW_WRITE_CKD_MT:
665 		data->auxiliary.last_bytes_used = 0x1;
666 		data->length = reclen;
667 		data->operation.operation = 0x03;
668 		break;
669 	case DASD_ECKD_CCW_READ:
670 	case DASD_ECKD_CCW_READ_MT:
671 	case DASD_ECKD_CCW_READ_KD:
672 	case DASD_ECKD_CCW_READ_KD_MT:
673 		data->auxiliary.last_bytes_used = 0x1;
674 		data->length = reclen;
675 		data->operation.operation = 0x06;
676 		break;
677 	case DASD_ECKD_CCW_READ_CKD:
678 	case DASD_ECKD_CCW_READ_CKD_MT:
679 		data->auxiliary.last_bytes_used = 0x1;
680 		data->length = reclen;
681 		data->operation.operation = 0x16;
682 		break;
683 	case DASD_ECKD_CCW_READ_COUNT:
684 		data->operation.operation = 0x06;
685 		break;
686 	case DASD_ECKD_CCW_ERASE:
687 		data->length = reclen;
688 		data->auxiliary.last_bytes_used = 0x1;
689 		data->operation.operation = 0x0b;
690 		break;
691 	default:
692 		DBF_DEV_EVENT(DBF_ERR, device, "unknown locate record "
693 			      "opcode 0x%x", cmd);
694 	}
695 	set_ch_t(&data->seek_addr,
696 		 trk / private->rdc_data.trk_per_cyl,
697 		 trk % private->rdc_data.trk_per_cyl);
698 	data->search_arg.cyl = data->seek_addr.cyl;
699 	data->search_arg.head = data->seek_addr.head;
700 	data->search_arg.record = rec_on_trk;
701 }
702 
703 /*
704  * Returns 1 if the block is one of the special blocks that needs
705  * to get read/written with the KD variant of the command.
706  * That is DASD_ECKD_READ_KD_MT instead of DASD_ECKD_READ_MT and
707  * DASD_ECKD_WRITE_KD_MT instead of DASD_ECKD_WRITE_MT.
708  * Luckily the KD variants differ only by one bit (0x08) from the
709  * normal variant. So don't wonder about code like:
710  * if (dasd_eckd_cdl_special(blk_per_trk, recid))
711  *         ccw->cmd_code |= 0x8;
712  */
713 static inline int
714 dasd_eckd_cdl_special(int blk_per_trk, int recid)
715 {
716 	if (recid < 3)
717 		return 1;
718 	if (recid < blk_per_trk)
719 		return 0;
720 	if (recid < 2 * blk_per_trk)
721 		return 1;
722 	return 0;
723 }
724 
725 /*
726  * Returns the record size for the special blocks of the cdl format.
727  * Only returns something useful if dasd_eckd_cdl_special is true
728  * for the recid.
729  */
730 static inline int
731 dasd_eckd_cdl_reclen(int recid)
732 {
733 	if (recid < 3)
734 		return sizes_trk0[recid];
735 	return LABEL_SIZE;
736 }
737 /* create unique id from private structure. */
738 static void create_uid(struct dasd_eckd_private *private)
739 {
740 	int count;
741 	struct dasd_uid *uid;
742 
743 	uid = &private->uid;
744 	memset(uid, 0, sizeof(struct dasd_uid));
745 	memcpy(uid->vendor, private->ned->HDA_manufacturer,
746 	       sizeof(uid->vendor) - 1);
747 	EBCASC(uid->vendor, sizeof(uid->vendor) - 1);
748 	memcpy(uid->serial, private->ned->HDA_location,
749 	       sizeof(uid->serial) - 1);
750 	EBCASC(uid->serial, sizeof(uid->serial) - 1);
751 	uid->ssid = private->gneq->subsystemID;
752 	uid->real_unit_addr = private->ned->unit_addr;
753 	if (private->sneq) {
754 		uid->type = private->sneq->sua_flags;
755 		if (uid->type == UA_BASE_PAV_ALIAS)
756 			uid->base_unit_addr = private->sneq->base_unit_addr;
757 	} else {
758 		uid->type = UA_BASE_DEVICE;
759 	}
760 	if (private->vdsneq) {
761 		for (count = 0; count < 16; count++) {
762 			sprintf(uid->vduit+2*count, "%02x",
763 				private->vdsneq->uit[count]);
764 		}
765 	}
766 }
767 
768 /*
769  * Generate device unique id that specifies the physical device.
770  */
771 static int dasd_eckd_generate_uid(struct dasd_device *device)
772 {
773 	struct dasd_eckd_private *private = device->private;
774 	unsigned long flags;
775 
776 	if (!private)
777 		return -ENODEV;
778 	if (!private->ned || !private->gneq)
779 		return -ENODEV;
780 	spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
781 	create_uid(private);
782 	spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
783 	return 0;
784 }
785 
786 static int dasd_eckd_get_uid(struct dasd_device *device, struct dasd_uid *uid)
787 {
788 	struct dasd_eckd_private *private = device->private;
789 	unsigned long flags;
790 
791 	if (private) {
792 		spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
793 		*uid = private->uid;
794 		spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
795 		return 0;
796 	}
797 	return -EINVAL;
798 }
799 
800 /*
801  * compare device UID with data of a given dasd_eckd_private structure
802  * return 0 for match
803  */
804 static int dasd_eckd_compare_path_uid(struct dasd_device *device,
805 				      struct dasd_eckd_private *private)
806 {
807 	struct dasd_uid device_uid;
808 
809 	create_uid(private);
810 	dasd_eckd_get_uid(device, &device_uid);
811 
812 	return memcmp(&device_uid, &private->uid, sizeof(struct dasd_uid));
813 }
814 
815 static void dasd_eckd_fill_rcd_cqr(struct dasd_device *device,
816 				   struct dasd_ccw_req *cqr,
817 				   __u8 *rcd_buffer,
818 				   __u8 lpm)
819 {
820 	struct ccw1 *ccw;
821 	/*
822 	 * buffer has to start with EBCDIC "V1.0" to show
823 	 * support for virtual device SNEQ
824 	 */
825 	rcd_buffer[0] = 0xE5;
826 	rcd_buffer[1] = 0xF1;
827 	rcd_buffer[2] = 0x4B;
828 	rcd_buffer[3] = 0xF0;
829 
830 	ccw = cqr->cpaddr;
831 	ccw->cmd_code = DASD_ECKD_CCW_RCD;
832 	ccw->flags = 0;
833 	ccw->cda = (__u32)(addr_t)rcd_buffer;
834 	ccw->count = DASD_ECKD_RCD_DATA_SIZE;
835 	cqr->magic = DASD_ECKD_MAGIC;
836 
837 	cqr->startdev = device;
838 	cqr->memdev = device;
839 	cqr->block = NULL;
840 	cqr->expires = 10*HZ;
841 	cqr->lpm = lpm;
842 	cqr->retries = 256;
843 	cqr->buildclk = get_tod_clock();
844 	cqr->status = DASD_CQR_FILLED;
845 	set_bit(DASD_CQR_VERIFY_PATH, &cqr->flags);
846 }
847 
848 /*
849  * Wakeup helper for read_conf
850  * if the cqr is not done and needs some error recovery
851  * the buffer has to be re-initialized with the EBCDIC "V1.0"
852  * to show support for virtual device SNEQ
853  */
854 static void read_conf_cb(struct dasd_ccw_req *cqr, void *data)
855 {
856 	struct ccw1 *ccw;
857 	__u8 *rcd_buffer;
858 
859 	if (cqr->status !=  DASD_CQR_DONE) {
860 		ccw = cqr->cpaddr;
861 		rcd_buffer = (__u8 *)((addr_t) ccw->cda);
862 		memset(rcd_buffer, 0, sizeof(*rcd_buffer));
863 
864 		rcd_buffer[0] = 0xE5;
865 		rcd_buffer[1] = 0xF1;
866 		rcd_buffer[2] = 0x4B;
867 		rcd_buffer[3] = 0xF0;
868 	}
869 	dasd_wakeup_cb(cqr, data);
870 }
871 
872 static int dasd_eckd_read_conf_immediately(struct dasd_device *device,
873 					   struct dasd_ccw_req *cqr,
874 					   __u8 *rcd_buffer,
875 					   __u8 lpm)
876 {
877 	struct ciw *ciw;
878 	int rc;
879 	/*
880 	 * sanity check: scan for RCD command in extended SenseID data
881 	 * some devices do not support RCD
882 	 */
883 	ciw = ccw_device_get_ciw(device->cdev, CIW_TYPE_RCD);
884 	if (!ciw || ciw->cmd != DASD_ECKD_CCW_RCD)
885 		return -EOPNOTSUPP;
886 
887 	dasd_eckd_fill_rcd_cqr(device, cqr, rcd_buffer, lpm);
888 	clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
889 	set_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags);
890 	cqr->retries = 5;
891 	cqr->callback = read_conf_cb;
892 	rc = dasd_sleep_on_immediatly(cqr);
893 	return rc;
894 }
895 
896 static int dasd_eckd_read_conf_lpm(struct dasd_device *device,
897 				   void **rcd_buffer,
898 				   int *rcd_buffer_size, __u8 lpm)
899 {
900 	struct ciw *ciw;
901 	char *rcd_buf = NULL;
902 	int ret;
903 	struct dasd_ccw_req *cqr;
904 
905 	/*
906 	 * sanity check: scan for RCD command in extended SenseID data
907 	 * some devices do not support RCD
908 	 */
909 	ciw = ccw_device_get_ciw(device->cdev, CIW_TYPE_RCD);
910 	if (!ciw || ciw->cmd != DASD_ECKD_CCW_RCD) {
911 		ret = -EOPNOTSUPP;
912 		goto out_error;
913 	}
914 	rcd_buf = kzalloc(DASD_ECKD_RCD_DATA_SIZE, GFP_KERNEL | GFP_DMA);
915 	if (!rcd_buf) {
916 		ret = -ENOMEM;
917 		goto out_error;
918 	}
919 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* RCD */,
920 				   0, /* use rcd_buf as data ara */
921 				   device, NULL);
922 	if (IS_ERR(cqr)) {
923 		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
924 			      "Could not allocate RCD request");
925 		ret = -ENOMEM;
926 		goto out_error;
927 	}
928 	dasd_eckd_fill_rcd_cqr(device, cqr, rcd_buf, lpm);
929 	cqr->callback = read_conf_cb;
930 	ret = dasd_sleep_on(cqr);
931 	/*
932 	 * on success we update the user input parms
933 	 */
934 	dasd_sfree_request(cqr, cqr->memdev);
935 	if (ret)
936 		goto out_error;
937 
938 	*rcd_buffer_size = DASD_ECKD_RCD_DATA_SIZE;
939 	*rcd_buffer = rcd_buf;
940 	return 0;
941 out_error:
942 	kfree(rcd_buf);
943 	*rcd_buffer = NULL;
944 	*rcd_buffer_size = 0;
945 	return ret;
946 }
947 
948 static int dasd_eckd_identify_conf_parts(struct dasd_eckd_private *private)
949 {
950 
951 	struct dasd_sneq *sneq;
952 	int i, count;
953 
954 	private->ned = NULL;
955 	private->sneq = NULL;
956 	private->vdsneq = NULL;
957 	private->gneq = NULL;
958 	count = private->conf_len / sizeof(struct dasd_sneq);
959 	sneq = (struct dasd_sneq *)private->conf_data;
960 	for (i = 0; i < count; ++i) {
961 		if (sneq->flags.identifier == 1 && sneq->format == 1)
962 			private->sneq = sneq;
963 		else if (sneq->flags.identifier == 1 && sneq->format == 4)
964 			private->vdsneq = (struct vd_sneq *)sneq;
965 		else if (sneq->flags.identifier == 2)
966 			private->gneq = (struct dasd_gneq *)sneq;
967 		else if (sneq->flags.identifier == 3 && sneq->res1 == 1)
968 			private->ned = (struct dasd_ned *)sneq;
969 		sneq++;
970 	}
971 	if (!private->ned || !private->gneq) {
972 		private->ned = NULL;
973 		private->sneq = NULL;
974 		private->vdsneq = NULL;
975 		private->gneq = NULL;
976 		return -EINVAL;
977 	}
978 	return 0;
979 
980 };
981 
982 static unsigned char dasd_eckd_path_access(void *conf_data, int conf_len)
983 {
984 	struct dasd_gneq *gneq;
985 	int i, count, found;
986 
987 	count = conf_len / sizeof(*gneq);
988 	gneq = (struct dasd_gneq *)conf_data;
989 	found = 0;
990 	for (i = 0; i < count; ++i) {
991 		if (gneq->flags.identifier == 2) {
992 			found = 1;
993 			break;
994 		}
995 		gneq++;
996 	}
997 	if (found)
998 		return ((char *)gneq)[18] & 0x07;
999 	else
1000 		return 0;
1001 }
1002 
1003 static void dasd_eckd_clear_conf_data(struct dasd_device *device)
1004 {
1005 	struct dasd_eckd_private *private = device->private;
1006 	int i;
1007 
1008 	private->conf_data = NULL;
1009 	private->conf_len = 0;
1010 	for (i = 0; i < 8; i++) {
1011 		kfree(device->path[i].conf_data);
1012 		device->path[i].conf_data = NULL;
1013 		device->path[i].cssid = 0;
1014 		device->path[i].ssid = 0;
1015 		device->path[i].chpid = 0;
1016 	}
1017 }
1018 
1019 
1020 static int dasd_eckd_read_conf(struct dasd_device *device)
1021 {
1022 	void *conf_data;
1023 	int conf_len, conf_data_saved;
1024 	int rc, path_err, pos;
1025 	__u8 lpm, opm;
1026 	struct dasd_eckd_private *private, path_private;
1027 	struct dasd_uid *uid;
1028 	char print_path_uid[60], print_device_uid[60];
1029 	struct channel_path_desc_fmt0 *chp_desc;
1030 	struct subchannel_id sch_id;
1031 
1032 	private = device->private;
1033 	opm = ccw_device_get_path_mask(device->cdev);
1034 	ccw_device_get_schid(device->cdev, &sch_id);
1035 	conf_data_saved = 0;
1036 	path_err = 0;
1037 	/* get configuration data per operational path */
1038 	for (lpm = 0x80; lpm; lpm>>= 1) {
1039 		if (!(lpm & opm))
1040 			continue;
1041 		rc = dasd_eckd_read_conf_lpm(device, &conf_data,
1042 					     &conf_len, lpm);
1043 		if (rc && rc != -EOPNOTSUPP) {	/* -EOPNOTSUPP is ok */
1044 			DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
1045 					"Read configuration data returned "
1046 					"error %d", rc);
1047 			return rc;
1048 		}
1049 		if (conf_data == NULL) {
1050 			DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
1051 					"No configuration data "
1052 					"retrieved");
1053 			/* no further analysis possible */
1054 			dasd_path_add_opm(device, opm);
1055 			continue;	/* no error */
1056 		}
1057 		/* save first valid configuration data */
1058 		if (!conf_data_saved) {
1059 			/* initially clear previously stored conf_data */
1060 			dasd_eckd_clear_conf_data(device);
1061 			private->conf_data = conf_data;
1062 			private->conf_len = conf_len;
1063 			if (dasd_eckd_identify_conf_parts(private)) {
1064 				private->conf_data = NULL;
1065 				private->conf_len = 0;
1066 				kfree(conf_data);
1067 				continue;
1068 			}
1069 			pos = pathmask_to_pos(lpm);
1070 			/* store per path conf_data */
1071 			device->path[pos].conf_data = conf_data;
1072 			device->path[pos].cssid = sch_id.cssid;
1073 			device->path[pos].ssid = sch_id.ssid;
1074 			chp_desc = ccw_device_get_chp_desc(device->cdev, pos);
1075 			if (chp_desc)
1076 				device->path[pos].chpid = chp_desc->chpid;
1077 			kfree(chp_desc);
1078 			/*
1079 			 * build device UID that other path data
1080 			 * can be compared to it
1081 			 */
1082 			dasd_eckd_generate_uid(device);
1083 			conf_data_saved++;
1084 		} else {
1085 			path_private.conf_data = conf_data;
1086 			path_private.conf_len = DASD_ECKD_RCD_DATA_SIZE;
1087 			if (dasd_eckd_identify_conf_parts(
1088 				    &path_private)) {
1089 				path_private.conf_data = NULL;
1090 				path_private.conf_len = 0;
1091 				kfree(conf_data);
1092 				continue;
1093 			}
1094 			if (dasd_eckd_compare_path_uid(
1095 				    device, &path_private)) {
1096 				uid = &path_private.uid;
1097 				if (strlen(uid->vduit) > 0)
1098 					snprintf(print_path_uid,
1099 						 sizeof(print_path_uid),
1100 						 "%s.%s.%04x.%02x.%s",
1101 						 uid->vendor, uid->serial,
1102 						 uid->ssid, uid->real_unit_addr,
1103 						 uid->vduit);
1104 				else
1105 					snprintf(print_path_uid,
1106 						 sizeof(print_path_uid),
1107 						 "%s.%s.%04x.%02x",
1108 						 uid->vendor, uid->serial,
1109 						 uid->ssid,
1110 						 uid->real_unit_addr);
1111 				uid = &private->uid;
1112 				if (strlen(uid->vduit) > 0)
1113 					snprintf(print_device_uid,
1114 						 sizeof(print_device_uid),
1115 						 "%s.%s.%04x.%02x.%s",
1116 						 uid->vendor, uid->serial,
1117 						 uid->ssid, uid->real_unit_addr,
1118 						 uid->vduit);
1119 				else
1120 					snprintf(print_device_uid,
1121 						 sizeof(print_device_uid),
1122 						 "%s.%s.%04x.%02x",
1123 						 uid->vendor, uid->serial,
1124 						 uid->ssid,
1125 						 uid->real_unit_addr);
1126 				dev_err(&device->cdev->dev,
1127 					"Not all channel paths lead to "
1128 					"the same device, path %02X leads to "
1129 					"device %s instead of %s\n", lpm,
1130 					print_path_uid, print_device_uid);
1131 				path_err = -EINVAL;
1132 				dasd_path_add_cablepm(device, lpm);
1133 				continue;
1134 			}
1135 			pos = pathmask_to_pos(lpm);
1136 			/* store per path conf_data */
1137 			device->path[pos].conf_data = conf_data;
1138 			device->path[pos].cssid = sch_id.cssid;
1139 			device->path[pos].ssid = sch_id.ssid;
1140 			chp_desc = ccw_device_get_chp_desc(device->cdev, pos);
1141 			if (chp_desc)
1142 				device->path[pos].chpid = chp_desc->chpid;
1143 			kfree(chp_desc);
1144 			path_private.conf_data = NULL;
1145 			path_private.conf_len = 0;
1146 		}
1147 		switch (dasd_eckd_path_access(conf_data, conf_len)) {
1148 		case 0x02:
1149 			dasd_path_add_nppm(device, lpm);
1150 			break;
1151 		case 0x03:
1152 			dasd_path_add_ppm(device, lpm);
1153 			break;
1154 		}
1155 		if (!dasd_path_get_opm(device)) {
1156 			dasd_path_set_opm(device, lpm);
1157 			dasd_generic_path_operational(device);
1158 		} else {
1159 			dasd_path_add_opm(device, lpm);
1160 		}
1161 	}
1162 
1163 	return path_err;
1164 }
1165 
1166 static u32 get_fcx_max_data(struct dasd_device *device)
1167 {
1168 	struct dasd_eckd_private *private = device->private;
1169 	int fcx_in_css, fcx_in_gneq, fcx_in_features;
1170 	unsigned int mdc;
1171 	int tpm;
1172 
1173 	if (dasd_nofcx)
1174 		return 0;
1175 	/* is transport mode supported? */
1176 	fcx_in_css = css_general_characteristics.fcx;
1177 	fcx_in_gneq = private->gneq->reserved2[7] & 0x04;
1178 	fcx_in_features = private->features.feature[40] & 0x80;
1179 	tpm = fcx_in_css && fcx_in_gneq && fcx_in_features;
1180 
1181 	if (!tpm)
1182 		return 0;
1183 
1184 	mdc = ccw_device_get_mdc(device->cdev, 0);
1185 	if (mdc == 0) {
1186 		dev_warn(&device->cdev->dev, "Detecting the maximum supported data size for zHPF requests failed\n");
1187 		return 0;
1188 	} else {
1189 		return (u32)mdc * FCX_MAX_DATA_FACTOR;
1190 	}
1191 }
1192 
1193 static int verify_fcx_max_data(struct dasd_device *device, __u8 lpm)
1194 {
1195 	struct dasd_eckd_private *private = device->private;
1196 	unsigned int mdc;
1197 	u32 fcx_max_data;
1198 
1199 	if (private->fcx_max_data) {
1200 		mdc = ccw_device_get_mdc(device->cdev, lpm);
1201 		if (mdc == 0) {
1202 			dev_warn(&device->cdev->dev,
1203 				 "Detecting the maximum data size for zHPF "
1204 				 "requests failed (rc=%d) for a new path %x\n",
1205 				 mdc, lpm);
1206 			return mdc;
1207 		}
1208 		fcx_max_data = (u32)mdc * FCX_MAX_DATA_FACTOR;
1209 		if (fcx_max_data < private->fcx_max_data) {
1210 			dev_warn(&device->cdev->dev,
1211 				 "The maximum data size for zHPF requests %u "
1212 				 "on a new path %x is below the active maximum "
1213 				 "%u\n", fcx_max_data, lpm,
1214 				 private->fcx_max_data);
1215 			return -EACCES;
1216 		}
1217 	}
1218 	return 0;
1219 }
1220 
1221 static int rebuild_device_uid(struct dasd_device *device,
1222 			      struct path_verification_work_data *data)
1223 {
1224 	struct dasd_eckd_private *private = device->private;
1225 	__u8 lpm, opm = dasd_path_get_opm(device);
1226 	int rc = -ENODEV;
1227 
1228 	for (lpm = 0x80; lpm; lpm >>= 1) {
1229 		if (!(lpm & opm))
1230 			continue;
1231 		memset(&data->rcd_buffer, 0, sizeof(data->rcd_buffer));
1232 		memset(&data->cqr, 0, sizeof(data->cqr));
1233 		data->cqr.cpaddr = &data->ccw;
1234 		rc = dasd_eckd_read_conf_immediately(device, &data->cqr,
1235 						     data->rcd_buffer,
1236 						     lpm);
1237 
1238 		if (rc) {
1239 			if (rc == -EOPNOTSUPP) /* -EOPNOTSUPP is ok */
1240 				continue;
1241 			DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
1242 					"Read configuration data "
1243 					"returned error %d", rc);
1244 			break;
1245 		}
1246 		memcpy(private->conf_data, data->rcd_buffer,
1247 		       DASD_ECKD_RCD_DATA_SIZE);
1248 		if (dasd_eckd_identify_conf_parts(private)) {
1249 			rc = -ENODEV;
1250 		} else /* first valid path is enough */
1251 			break;
1252 	}
1253 
1254 	if (!rc)
1255 		rc = dasd_eckd_generate_uid(device);
1256 
1257 	return rc;
1258 }
1259 
1260 static void do_path_verification_work(struct work_struct *work)
1261 {
1262 	struct path_verification_work_data *data;
1263 	struct dasd_device *device;
1264 	struct dasd_eckd_private path_private;
1265 	struct dasd_uid *uid;
1266 	__u8 path_rcd_buf[DASD_ECKD_RCD_DATA_SIZE];
1267 	__u8 lpm, opm, npm, ppm, epm, hpfpm, cablepm;
1268 	unsigned long flags;
1269 	char print_uid[60];
1270 	int rc;
1271 
1272 	data = container_of(work, struct path_verification_work_data, worker);
1273 	device = data->device;
1274 
1275 	/* delay path verification until device was resumed */
1276 	if (test_bit(DASD_FLAG_SUSPENDED, &device->flags)) {
1277 		schedule_work(work);
1278 		return;
1279 	}
1280 	/* check if path verification already running and delay if so */
1281 	if (test_and_set_bit(DASD_FLAG_PATH_VERIFY, &device->flags)) {
1282 		schedule_work(work);
1283 		return;
1284 	}
1285 	opm = 0;
1286 	npm = 0;
1287 	ppm = 0;
1288 	epm = 0;
1289 	hpfpm = 0;
1290 	cablepm = 0;
1291 
1292 	for (lpm = 0x80; lpm; lpm >>= 1) {
1293 		if (!(lpm & data->tbvpm))
1294 			continue;
1295 		memset(&data->rcd_buffer, 0, sizeof(data->rcd_buffer));
1296 		memset(&data->cqr, 0, sizeof(data->cqr));
1297 		data->cqr.cpaddr = &data->ccw;
1298 		rc = dasd_eckd_read_conf_immediately(device, &data->cqr,
1299 						     data->rcd_buffer,
1300 						     lpm);
1301 		if (!rc) {
1302 			switch (dasd_eckd_path_access(data->rcd_buffer,
1303 						      DASD_ECKD_RCD_DATA_SIZE)
1304 				) {
1305 			case 0x02:
1306 				npm |= lpm;
1307 				break;
1308 			case 0x03:
1309 				ppm |= lpm;
1310 				break;
1311 			}
1312 			opm |= lpm;
1313 		} else if (rc == -EOPNOTSUPP) {
1314 			DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
1315 					"path verification: No configuration "
1316 					"data retrieved");
1317 			opm |= lpm;
1318 		} else if (rc == -EAGAIN) {
1319 			DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
1320 					"path verification: device is stopped,"
1321 					" try again later");
1322 			epm |= lpm;
1323 		} else {
1324 			dev_warn(&device->cdev->dev,
1325 				 "Reading device feature codes failed "
1326 				 "(rc=%d) for new path %x\n", rc, lpm);
1327 			continue;
1328 		}
1329 		if (verify_fcx_max_data(device, lpm)) {
1330 			opm &= ~lpm;
1331 			npm &= ~lpm;
1332 			ppm &= ~lpm;
1333 			hpfpm |= lpm;
1334 			continue;
1335 		}
1336 
1337 		/*
1338 		 * save conf_data for comparison after
1339 		 * rebuild_device_uid may have changed
1340 		 * the original data
1341 		 */
1342 		memcpy(&path_rcd_buf, data->rcd_buffer,
1343 		       DASD_ECKD_RCD_DATA_SIZE);
1344 		path_private.conf_data = (void *) &path_rcd_buf;
1345 		path_private.conf_len = DASD_ECKD_RCD_DATA_SIZE;
1346 		if (dasd_eckd_identify_conf_parts(&path_private)) {
1347 			path_private.conf_data = NULL;
1348 			path_private.conf_len = 0;
1349 			continue;
1350 		}
1351 
1352 		/*
1353 		 * compare path UID with device UID only if at least
1354 		 * one valid path is left
1355 		 * in other case the device UID may have changed and
1356 		 * the first working path UID will be used as device UID
1357 		 */
1358 		if (dasd_path_get_opm(device) &&
1359 		    dasd_eckd_compare_path_uid(device, &path_private)) {
1360 			/*
1361 			 * the comparison was not successful
1362 			 * rebuild the device UID with at least one
1363 			 * known path in case a z/VM hyperswap command
1364 			 * has changed the device
1365 			 *
1366 			 * after this compare again
1367 			 *
1368 			 * if either the rebuild or the recompare fails
1369 			 * the path can not be used
1370 			 */
1371 			if (rebuild_device_uid(device, data) ||
1372 			    dasd_eckd_compare_path_uid(
1373 				    device, &path_private)) {
1374 				uid = &path_private.uid;
1375 				if (strlen(uid->vduit) > 0)
1376 					snprintf(print_uid, sizeof(print_uid),
1377 						 "%s.%s.%04x.%02x.%s",
1378 						 uid->vendor, uid->serial,
1379 						 uid->ssid, uid->real_unit_addr,
1380 						 uid->vduit);
1381 				else
1382 					snprintf(print_uid, sizeof(print_uid),
1383 						 "%s.%s.%04x.%02x",
1384 						 uid->vendor, uid->serial,
1385 						 uid->ssid,
1386 						 uid->real_unit_addr);
1387 				dev_err(&device->cdev->dev,
1388 					"The newly added channel path %02X "
1389 					"will not be used because it leads "
1390 					"to a different device %s\n",
1391 					lpm, print_uid);
1392 				opm &= ~lpm;
1393 				npm &= ~lpm;
1394 				ppm &= ~lpm;
1395 				cablepm |= lpm;
1396 				continue;
1397 			}
1398 		}
1399 
1400 		/*
1401 		 * There is a small chance that a path is lost again between
1402 		 * above path verification and the following modification of
1403 		 * the device opm mask. We could avoid that race here by using
1404 		 * yet another path mask, but we rather deal with this unlikely
1405 		 * situation in dasd_start_IO.
1406 		 */
1407 		spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1408 		if (!dasd_path_get_opm(device) && opm) {
1409 			dasd_path_set_opm(device, opm);
1410 			dasd_generic_path_operational(device);
1411 		} else {
1412 			dasd_path_add_opm(device, opm);
1413 		}
1414 		dasd_path_add_nppm(device, npm);
1415 		dasd_path_add_ppm(device, ppm);
1416 		dasd_path_add_tbvpm(device, epm);
1417 		dasd_path_add_cablepm(device, cablepm);
1418 		dasd_path_add_nohpfpm(device, hpfpm);
1419 		spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1420 	}
1421 	clear_bit(DASD_FLAG_PATH_VERIFY, &device->flags);
1422 	dasd_put_device(device);
1423 	if (data->isglobal)
1424 		mutex_unlock(&dasd_path_verification_mutex);
1425 	else
1426 		kfree(data);
1427 }
1428 
1429 static int dasd_eckd_verify_path(struct dasd_device *device, __u8 lpm)
1430 {
1431 	struct path_verification_work_data *data;
1432 
1433 	data = kmalloc(sizeof(*data), GFP_ATOMIC | GFP_DMA);
1434 	if (!data) {
1435 		if (mutex_trylock(&dasd_path_verification_mutex)) {
1436 			data = path_verification_worker;
1437 			data->isglobal = 1;
1438 		} else
1439 			return -ENOMEM;
1440 	} else {
1441 		memset(data, 0, sizeof(*data));
1442 		data->isglobal = 0;
1443 	}
1444 	INIT_WORK(&data->worker, do_path_verification_work);
1445 	dasd_get_device(device);
1446 	data->device = device;
1447 	data->tbvpm = lpm;
1448 	schedule_work(&data->worker);
1449 	return 0;
1450 }
1451 
1452 static void dasd_eckd_reset_path(struct dasd_device *device, __u8 pm)
1453 {
1454 	struct dasd_eckd_private *private = device->private;
1455 	unsigned long flags;
1456 
1457 	if (!private->fcx_max_data)
1458 		private->fcx_max_data = get_fcx_max_data(device);
1459 	spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1460 	dasd_path_set_tbvpm(device, pm ? : dasd_path_get_notoperpm(device));
1461 	dasd_schedule_device_bh(device);
1462 	spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1463 }
1464 
1465 static int dasd_eckd_read_features(struct dasd_device *device)
1466 {
1467 	struct dasd_eckd_private *private = device->private;
1468 	struct dasd_psf_prssd_data *prssdp;
1469 	struct dasd_rssd_features *features;
1470 	struct dasd_ccw_req *cqr;
1471 	struct ccw1 *ccw;
1472 	int rc;
1473 
1474 	memset(&private->features, 0, sizeof(struct dasd_rssd_features));
1475 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */	+ 1 /* RSSD */,
1476 				   (sizeof(struct dasd_psf_prssd_data) +
1477 				    sizeof(struct dasd_rssd_features)),
1478 				   device, NULL);
1479 	if (IS_ERR(cqr)) {
1480 		DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s", "Could not "
1481 				"allocate initialization request");
1482 		return PTR_ERR(cqr);
1483 	}
1484 	cqr->startdev = device;
1485 	cqr->memdev = device;
1486 	cqr->block = NULL;
1487 	cqr->retries = 256;
1488 	cqr->expires = 10 * HZ;
1489 
1490 	/* Prepare for Read Subsystem Data */
1491 	prssdp = (struct dasd_psf_prssd_data *) cqr->data;
1492 	memset(prssdp, 0, sizeof(struct dasd_psf_prssd_data));
1493 	prssdp->order = PSF_ORDER_PRSSD;
1494 	prssdp->suborder = 0x41;	/* Read Feature Codes */
1495 	/* all other bytes of prssdp must be zero */
1496 
1497 	ccw = cqr->cpaddr;
1498 	ccw->cmd_code = DASD_ECKD_CCW_PSF;
1499 	ccw->count = sizeof(struct dasd_psf_prssd_data);
1500 	ccw->flags |= CCW_FLAG_CC;
1501 	ccw->cda = (__u32)(addr_t) prssdp;
1502 
1503 	/* Read Subsystem Data - feature codes */
1504 	features = (struct dasd_rssd_features *) (prssdp + 1);
1505 	memset(features, 0, sizeof(struct dasd_rssd_features));
1506 
1507 	ccw++;
1508 	ccw->cmd_code = DASD_ECKD_CCW_RSSD;
1509 	ccw->count = sizeof(struct dasd_rssd_features);
1510 	ccw->cda = (__u32)(addr_t) features;
1511 
1512 	cqr->buildclk = get_tod_clock();
1513 	cqr->status = DASD_CQR_FILLED;
1514 	rc = dasd_sleep_on(cqr);
1515 	if (rc == 0) {
1516 		prssdp = (struct dasd_psf_prssd_data *) cqr->data;
1517 		features = (struct dasd_rssd_features *) (prssdp + 1);
1518 		memcpy(&private->features, features,
1519 		       sizeof(struct dasd_rssd_features));
1520 	} else
1521 		dev_warn(&device->cdev->dev, "Reading device feature codes"
1522 			 " failed with rc=%d\n", rc);
1523 	dasd_sfree_request(cqr, cqr->memdev);
1524 	return rc;
1525 }
1526 
1527 /* Read Volume Information - Volume Storage Query */
1528 static int dasd_eckd_read_vol_info(struct dasd_device *device)
1529 {
1530 	struct dasd_eckd_private *private = device->private;
1531 	struct dasd_psf_prssd_data *prssdp;
1532 	struct dasd_rssd_vsq *vsq;
1533 	struct dasd_ccw_req *cqr;
1534 	struct ccw1 *ccw;
1535 	int useglobal;
1536 	int rc;
1537 
1538 	/* This command cannot be executed on an alias device */
1539 	if (private->uid.type == UA_BASE_PAV_ALIAS ||
1540 	    private->uid.type == UA_HYPER_PAV_ALIAS)
1541 		return 0;
1542 
1543 	useglobal = 0;
1544 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 2 /* PSF + RSSD */,
1545 				   sizeof(*prssdp) + sizeof(*vsq), device, NULL);
1546 	if (IS_ERR(cqr)) {
1547 		DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
1548 				"Could not allocate initialization request");
1549 		mutex_lock(&dasd_vol_info_mutex);
1550 		useglobal = 1;
1551 		cqr = &dasd_vol_info_req->cqr;
1552 		memset(cqr, 0, sizeof(*cqr));
1553 		memset(dasd_vol_info_req, 0, sizeof(*dasd_vol_info_req));
1554 		cqr->cpaddr = &dasd_vol_info_req->ccw;
1555 		cqr->data = &dasd_vol_info_req->data;
1556 		cqr->magic = DASD_ECKD_MAGIC;
1557 	}
1558 
1559 	/* Prepare for Read Subsystem Data */
1560 	prssdp = cqr->data;
1561 	prssdp->order = PSF_ORDER_PRSSD;
1562 	prssdp->suborder = PSF_SUBORDER_VSQ;	/* Volume Storage Query */
1563 	prssdp->lss = private->ned->ID;
1564 	prssdp->volume = private->ned->unit_addr;
1565 
1566 	ccw = cqr->cpaddr;
1567 	ccw->cmd_code = DASD_ECKD_CCW_PSF;
1568 	ccw->count = sizeof(*prssdp);
1569 	ccw->flags |= CCW_FLAG_CC;
1570 	ccw->cda = (__u32)(addr_t)prssdp;
1571 
1572 	/* Read Subsystem Data - Volume Storage Query */
1573 	vsq = (struct dasd_rssd_vsq *)(prssdp + 1);
1574 	memset(vsq, 0, sizeof(*vsq));
1575 
1576 	ccw++;
1577 	ccw->cmd_code = DASD_ECKD_CCW_RSSD;
1578 	ccw->count = sizeof(*vsq);
1579 	ccw->flags |= CCW_FLAG_SLI;
1580 	ccw->cda = (__u32)(addr_t)vsq;
1581 
1582 	cqr->buildclk = get_tod_clock();
1583 	cqr->status = DASD_CQR_FILLED;
1584 	cqr->startdev = device;
1585 	cqr->memdev = device;
1586 	cqr->block = NULL;
1587 	cqr->retries = 256;
1588 	cqr->expires = device->default_expires * HZ;
1589 	/* The command might not be supported. Suppress the error output */
1590 	__set_bit(DASD_CQR_SUPPRESS_CR, &cqr->flags);
1591 
1592 	rc = dasd_sleep_on_interruptible(cqr);
1593 	if (rc == 0) {
1594 		memcpy(&private->vsq, vsq, sizeof(*vsq));
1595 	} else {
1596 		DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
1597 				"Reading the volume storage information failed with rc=%d", rc);
1598 	}
1599 
1600 	if (useglobal)
1601 		mutex_unlock(&dasd_vol_info_mutex);
1602 	else
1603 		dasd_sfree_request(cqr, cqr->memdev);
1604 
1605 	return rc;
1606 }
1607 
1608 static int dasd_eckd_is_ese(struct dasd_device *device)
1609 {
1610 	struct dasd_eckd_private *private = device->private;
1611 
1612 	return private->vsq.vol_info.ese;
1613 }
1614 
1615 static int dasd_eckd_ext_pool_id(struct dasd_device *device)
1616 {
1617 	struct dasd_eckd_private *private = device->private;
1618 
1619 	return private->vsq.extent_pool_id;
1620 }
1621 
1622 /*
1623  * This value represents the total amount of available space. As more space is
1624  * allocated by ESE volumes, this value will decrease.
1625  * The data for this value is therefore updated on any call.
1626  */
1627 static int dasd_eckd_space_configured(struct dasd_device *device)
1628 {
1629 	struct dasd_eckd_private *private = device->private;
1630 	int rc;
1631 
1632 	rc = dasd_eckd_read_vol_info(device);
1633 
1634 	return rc ? : private->vsq.space_configured;
1635 }
1636 
1637 /*
1638  * The value of space allocated by an ESE volume may have changed and is
1639  * therefore updated on any call.
1640  */
1641 static int dasd_eckd_space_allocated(struct dasd_device *device)
1642 {
1643 	struct dasd_eckd_private *private = device->private;
1644 	int rc;
1645 
1646 	rc = dasd_eckd_read_vol_info(device);
1647 
1648 	return rc ? : private->vsq.space_allocated;
1649 }
1650 
1651 static int dasd_eckd_logical_capacity(struct dasd_device *device)
1652 {
1653 	struct dasd_eckd_private *private = device->private;
1654 
1655 	return private->vsq.logical_capacity;
1656 }
1657 
1658 static void dasd_eckd_ext_pool_exhaust_work(struct work_struct *work)
1659 {
1660 	struct ext_pool_exhaust_work_data *data;
1661 	struct dasd_device *device;
1662 	struct dasd_device *base;
1663 
1664 	data = container_of(work, struct ext_pool_exhaust_work_data, worker);
1665 	device = data->device;
1666 	base = data->base;
1667 
1668 	if (!base)
1669 		base = device;
1670 	if (dasd_eckd_space_configured(base) != 0) {
1671 		dasd_generic_space_avail(device);
1672 	} else {
1673 		dev_warn(&device->cdev->dev, "No space left in the extent pool\n");
1674 		DBF_DEV_EVENT(DBF_WARNING, device, "%s", "out of space");
1675 	}
1676 
1677 	dasd_put_device(device);
1678 	kfree(data);
1679 }
1680 
1681 static int dasd_eckd_ext_pool_exhaust(struct dasd_device *device,
1682 				      struct dasd_ccw_req *cqr)
1683 {
1684 	struct ext_pool_exhaust_work_data *data;
1685 
1686 	data = kzalloc(sizeof(*data), GFP_ATOMIC);
1687 	if (!data)
1688 		return -ENOMEM;
1689 	INIT_WORK(&data->worker, dasd_eckd_ext_pool_exhaust_work);
1690 	dasd_get_device(device);
1691 	data->device = device;
1692 
1693 	if (cqr->block)
1694 		data->base = cqr->block->base;
1695 	else if (cqr->basedev)
1696 		data->base = cqr->basedev;
1697 	else
1698 		data->base = NULL;
1699 
1700 	schedule_work(&data->worker);
1701 
1702 	return 0;
1703 }
1704 
1705 static void dasd_eckd_cpy_ext_pool_data(struct dasd_device *device,
1706 					struct dasd_rssd_lcq *lcq)
1707 {
1708 	struct dasd_eckd_private *private = device->private;
1709 	int pool_id = dasd_eckd_ext_pool_id(device);
1710 	struct dasd_ext_pool_sum eps;
1711 	int i;
1712 
1713 	for (i = 0; i < lcq->pool_count; i++) {
1714 		eps = lcq->ext_pool_sum[i];
1715 		if (eps.pool_id == pool_id) {
1716 			memcpy(&private->eps, &eps,
1717 			       sizeof(struct dasd_ext_pool_sum));
1718 		}
1719 	}
1720 }
1721 
1722 /* Read Extent Pool Information - Logical Configuration Query */
1723 static int dasd_eckd_read_ext_pool_info(struct dasd_device *device)
1724 {
1725 	struct dasd_eckd_private *private = device->private;
1726 	struct dasd_psf_prssd_data *prssdp;
1727 	struct dasd_rssd_lcq *lcq;
1728 	struct dasd_ccw_req *cqr;
1729 	struct ccw1 *ccw;
1730 	int rc;
1731 
1732 	/* This command cannot be executed on an alias device */
1733 	if (private->uid.type == UA_BASE_PAV_ALIAS ||
1734 	    private->uid.type == UA_HYPER_PAV_ALIAS)
1735 		return 0;
1736 
1737 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 2 /* PSF + RSSD */,
1738 				   sizeof(*prssdp) + sizeof(*lcq), device, NULL);
1739 	if (IS_ERR(cqr)) {
1740 		DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
1741 				"Could not allocate initialization request");
1742 		return PTR_ERR(cqr);
1743 	}
1744 
1745 	/* Prepare for Read Subsystem Data */
1746 	prssdp = cqr->data;
1747 	memset(prssdp, 0, sizeof(*prssdp));
1748 	prssdp->order = PSF_ORDER_PRSSD;
1749 	prssdp->suborder = PSF_SUBORDER_LCQ;	/* Logical Configuration Query */
1750 
1751 	ccw = cqr->cpaddr;
1752 	ccw->cmd_code = DASD_ECKD_CCW_PSF;
1753 	ccw->count = sizeof(*prssdp);
1754 	ccw->flags |= CCW_FLAG_CC;
1755 	ccw->cda = (__u32)(addr_t)prssdp;
1756 
1757 	lcq = (struct dasd_rssd_lcq *)(prssdp + 1);
1758 	memset(lcq, 0, sizeof(*lcq));
1759 
1760 	ccw++;
1761 	ccw->cmd_code = DASD_ECKD_CCW_RSSD;
1762 	ccw->count = sizeof(*lcq);
1763 	ccw->flags |= CCW_FLAG_SLI;
1764 	ccw->cda = (__u32)(addr_t)lcq;
1765 
1766 	cqr->buildclk = get_tod_clock();
1767 	cqr->status = DASD_CQR_FILLED;
1768 	cqr->startdev = device;
1769 	cqr->memdev = device;
1770 	cqr->block = NULL;
1771 	cqr->retries = 256;
1772 	cqr->expires = device->default_expires * HZ;
1773 	/* The command might not be supported. Suppress the error output */
1774 	__set_bit(DASD_CQR_SUPPRESS_CR, &cqr->flags);
1775 
1776 	rc = dasd_sleep_on_interruptible(cqr);
1777 	if (rc == 0) {
1778 		dasd_eckd_cpy_ext_pool_data(device, lcq);
1779 	} else {
1780 		DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
1781 				"Reading the logical configuration failed with rc=%d", rc);
1782 	}
1783 
1784 	dasd_sfree_request(cqr, cqr->memdev);
1785 
1786 	return rc;
1787 }
1788 
1789 /*
1790  * Depending on the device type, the extent size is specified either as
1791  * cylinders per extent (CKD) or size per extent (FBA)
1792  * A 1GB size corresponds to 1113cyl, and 16MB to 21cyl.
1793  */
1794 static int dasd_eckd_ext_size(struct dasd_device *device)
1795 {
1796 	struct dasd_eckd_private *private = device->private;
1797 	struct dasd_ext_pool_sum eps = private->eps;
1798 
1799 	if (!eps.flags.extent_size_valid)
1800 		return 0;
1801 	if (eps.extent_size.size_1G)
1802 		return 1113;
1803 	if (eps.extent_size.size_16M)
1804 		return 21;
1805 
1806 	return 0;
1807 }
1808 
1809 static int dasd_eckd_ext_pool_warn_thrshld(struct dasd_device *device)
1810 {
1811 	struct dasd_eckd_private *private = device->private;
1812 
1813 	return private->eps.warn_thrshld;
1814 }
1815 
1816 static int dasd_eckd_ext_pool_cap_at_warnlevel(struct dasd_device *device)
1817 {
1818 	struct dasd_eckd_private *private = device->private;
1819 
1820 	return private->eps.flags.capacity_at_warnlevel;
1821 }
1822 
1823 /*
1824  * Extent Pool out of space
1825  */
1826 static int dasd_eckd_ext_pool_oos(struct dasd_device *device)
1827 {
1828 	struct dasd_eckd_private *private = device->private;
1829 
1830 	return private->eps.flags.pool_oos;
1831 }
1832 
1833 /*
1834  * Build CP for Perform Subsystem Function - SSC.
1835  */
1836 static struct dasd_ccw_req *dasd_eckd_build_psf_ssc(struct dasd_device *device,
1837 						    int enable_pav)
1838 {
1839 	struct dasd_ccw_req *cqr;
1840 	struct dasd_psf_ssc_data *psf_ssc_data;
1841 	struct ccw1 *ccw;
1842 
1843 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */ ,
1844 				  sizeof(struct dasd_psf_ssc_data),
1845 				   device, NULL);
1846 
1847 	if (IS_ERR(cqr)) {
1848 		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1849 			   "Could not allocate PSF-SSC request");
1850 		return cqr;
1851 	}
1852 	psf_ssc_data = (struct dasd_psf_ssc_data *)cqr->data;
1853 	psf_ssc_data->order = PSF_ORDER_SSC;
1854 	psf_ssc_data->suborder = 0xc0;
1855 	if (enable_pav) {
1856 		psf_ssc_data->suborder |= 0x08;
1857 		psf_ssc_data->reserved[0] = 0x88;
1858 	}
1859 	ccw = cqr->cpaddr;
1860 	ccw->cmd_code = DASD_ECKD_CCW_PSF;
1861 	ccw->cda = (__u32)(addr_t)psf_ssc_data;
1862 	ccw->count = 66;
1863 
1864 	cqr->startdev = device;
1865 	cqr->memdev = device;
1866 	cqr->block = NULL;
1867 	cqr->retries = 256;
1868 	cqr->expires = 10*HZ;
1869 	cqr->buildclk = get_tod_clock();
1870 	cqr->status = DASD_CQR_FILLED;
1871 	return cqr;
1872 }
1873 
1874 /*
1875  * Perform Subsystem Function.
1876  * It is necessary to trigger CIO for channel revalidation since this
1877  * call might change behaviour of DASD devices.
1878  */
1879 static int
1880 dasd_eckd_psf_ssc(struct dasd_device *device, int enable_pav,
1881 		  unsigned long flags)
1882 {
1883 	struct dasd_ccw_req *cqr;
1884 	int rc;
1885 
1886 	cqr = dasd_eckd_build_psf_ssc(device, enable_pav);
1887 	if (IS_ERR(cqr))
1888 		return PTR_ERR(cqr);
1889 
1890 	/*
1891 	 * set flags e.g. turn on failfast, to prevent blocking
1892 	 * the calling function should handle failed requests
1893 	 */
1894 	cqr->flags |= flags;
1895 
1896 	rc = dasd_sleep_on(cqr);
1897 	if (!rc)
1898 		/* trigger CIO to reprobe devices */
1899 		css_schedule_reprobe();
1900 	else if (cqr->intrc == -EAGAIN)
1901 		rc = -EAGAIN;
1902 
1903 	dasd_sfree_request(cqr, cqr->memdev);
1904 	return rc;
1905 }
1906 
1907 /*
1908  * Valide storage server of current device.
1909  */
1910 static int dasd_eckd_validate_server(struct dasd_device *device,
1911 				     unsigned long flags)
1912 {
1913 	struct dasd_eckd_private *private = device->private;
1914 	int enable_pav, rc;
1915 
1916 	if (private->uid.type == UA_BASE_PAV_ALIAS ||
1917 	    private->uid.type == UA_HYPER_PAV_ALIAS)
1918 		return 0;
1919 	if (dasd_nopav || MACHINE_IS_VM)
1920 		enable_pav = 0;
1921 	else
1922 		enable_pav = 1;
1923 	rc = dasd_eckd_psf_ssc(device, enable_pav, flags);
1924 
1925 	/* may be requested feature is not available on server,
1926 	 * therefore just report error and go ahead */
1927 	DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "PSF-SSC for SSID %04x "
1928 			"returned rc=%d", private->uid.ssid, rc);
1929 	return rc;
1930 }
1931 
1932 /*
1933  * worker to do a validate server in case of a lost pathgroup
1934  */
1935 static void dasd_eckd_do_validate_server(struct work_struct *work)
1936 {
1937 	struct dasd_device *device = container_of(work, struct dasd_device,
1938 						  kick_validate);
1939 	unsigned long flags = 0;
1940 
1941 	set_bit(DASD_CQR_FLAGS_FAILFAST, &flags);
1942 	if (dasd_eckd_validate_server(device, flags)
1943 	    == -EAGAIN) {
1944 		/* schedule worker again if failed */
1945 		schedule_work(&device->kick_validate);
1946 		return;
1947 	}
1948 
1949 	dasd_put_device(device);
1950 }
1951 
1952 static void dasd_eckd_kick_validate_server(struct dasd_device *device)
1953 {
1954 	dasd_get_device(device);
1955 	/* exit if device not online or in offline processing */
1956 	if (test_bit(DASD_FLAG_OFFLINE, &device->flags) ||
1957 	   device->state < DASD_STATE_ONLINE) {
1958 		dasd_put_device(device);
1959 		return;
1960 	}
1961 	/* queue call to do_validate_server to the kernel event daemon. */
1962 	if (!schedule_work(&device->kick_validate))
1963 		dasd_put_device(device);
1964 }
1965 
1966 /*
1967  * Check device characteristics.
1968  * If the device is accessible using ECKD discipline, the device is enabled.
1969  */
1970 static int
1971 dasd_eckd_check_characteristics(struct dasd_device *device)
1972 {
1973 	struct dasd_eckd_private *private = device->private;
1974 	struct dasd_block *block;
1975 	struct dasd_uid temp_uid;
1976 	int rc, i;
1977 	int readonly;
1978 	unsigned long value;
1979 
1980 	/* setup work queue for validate server*/
1981 	INIT_WORK(&device->kick_validate, dasd_eckd_do_validate_server);
1982 	/* setup work queue for summary unit check */
1983 	INIT_WORK(&device->suc_work, dasd_alias_handle_summary_unit_check);
1984 
1985 	if (!ccw_device_is_pathgroup(device->cdev)) {
1986 		dev_warn(&device->cdev->dev,
1987 			 "A channel path group could not be established\n");
1988 		return -EIO;
1989 	}
1990 	if (!ccw_device_is_multipath(device->cdev)) {
1991 		dev_info(&device->cdev->dev,
1992 			 "The DASD is not operating in multipath mode\n");
1993 	}
1994 	if (!private) {
1995 		private = kzalloc(sizeof(*private), GFP_KERNEL | GFP_DMA);
1996 		if (!private) {
1997 			dev_warn(&device->cdev->dev,
1998 				 "Allocating memory for private DASD data "
1999 				 "failed\n");
2000 			return -ENOMEM;
2001 		}
2002 		device->private = private;
2003 	} else {
2004 		memset(private, 0, sizeof(*private));
2005 	}
2006 	/* Invalidate status of initial analysis. */
2007 	private->init_cqr_status = -1;
2008 	/* Set default cache operations. */
2009 	private->attrib.operation = DASD_NORMAL_CACHE;
2010 	private->attrib.nr_cyl = 0;
2011 
2012 	/* Read Configuration Data */
2013 	rc = dasd_eckd_read_conf(device);
2014 	if (rc)
2015 		goto out_err1;
2016 
2017 	/* set some default values */
2018 	device->default_expires = DASD_EXPIRES;
2019 	device->default_retries = DASD_RETRIES;
2020 	device->path_thrhld = DASD_ECKD_PATH_THRHLD;
2021 	device->path_interval = DASD_ECKD_PATH_INTERVAL;
2022 
2023 	if (private->gneq) {
2024 		value = 1;
2025 		for (i = 0; i < private->gneq->timeout.value; i++)
2026 			value = 10 * value;
2027 		value = value * private->gneq->timeout.number;
2028 		/* do not accept useless values */
2029 		if (value != 0 && value <= DASD_EXPIRES_MAX)
2030 			device->default_expires = value;
2031 	}
2032 
2033 	dasd_eckd_get_uid(device, &temp_uid);
2034 	if (temp_uid.type == UA_BASE_DEVICE) {
2035 		block = dasd_alloc_block();
2036 		if (IS_ERR(block)) {
2037 			DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
2038 					"could not allocate dasd "
2039 					"block structure");
2040 			rc = PTR_ERR(block);
2041 			goto out_err1;
2042 		}
2043 		device->block = block;
2044 		block->base = device;
2045 	}
2046 
2047 	/* register lcu with alias handling, enable PAV */
2048 	rc = dasd_alias_make_device_known_to_lcu(device);
2049 	if (rc)
2050 		goto out_err2;
2051 
2052 	dasd_eckd_validate_server(device, 0);
2053 
2054 	/* device may report different configuration data after LCU setup */
2055 	rc = dasd_eckd_read_conf(device);
2056 	if (rc)
2057 		goto out_err3;
2058 
2059 	/* Read Feature Codes */
2060 	dasd_eckd_read_features(device);
2061 
2062 	/* Read Volume Information */
2063 	dasd_eckd_read_vol_info(device);
2064 
2065 	/* Read Extent Pool Information */
2066 	dasd_eckd_read_ext_pool_info(device);
2067 
2068 	/* Read Device Characteristics */
2069 	rc = dasd_generic_read_dev_chars(device, DASD_ECKD_MAGIC,
2070 					 &private->rdc_data, 64);
2071 	if (rc) {
2072 		DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
2073 				"Read device characteristic failed, rc=%d", rc);
2074 		goto out_err3;
2075 	}
2076 
2077 	if ((device->features & DASD_FEATURE_USERAW) &&
2078 	    !(private->rdc_data.facilities.RT_in_LR)) {
2079 		dev_err(&device->cdev->dev, "The storage server does not "
2080 			"support raw-track access\n");
2081 		rc = -EINVAL;
2082 		goto out_err3;
2083 	}
2084 
2085 	/* find the valid cylinder size */
2086 	if (private->rdc_data.no_cyl == LV_COMPAT_CYL &&
2087 	    private->rdc_data.long_no_cyl)
2088 		private->real_cyl = private->rdc_data.long_no_cyl;
2089 	else
2090 		private->real_cyl = private->rdc_data.no_cyl;
2091 
2092 	private->fcx_max_data = get_fcx_max_data(device);
2093 
2094 	readonly = dasd_device_is_ro(device);
2095 	if (readonly)
2096 		set_bit(DASD_FLAG_DEVICE_RO, &device->flags);
2097 
2098 	dev_info(&device->cdev->dev, "New DASD %04X/%02X (CU %04X/%02X) "
2099 		 "with %d cylinders, %d heads, %d sectors%s\n",
2100 		 private->rdc_data.dev_type,
2101 		 private->rdc_data.dev_model,
2102 		 private->rdc_data.cu_type,
2103 		 private->rdc_data.cu_model.model,
2104 		 private->real_cyl,
2105 		 private->rdc_data.trk_per_cyl,
2106 		 private->rdc_data.sec_per_trk,
2107 		 readonly ? ", read-only device" : "");
2108 	return 0;
2109 
2110 out_err3:
2111 	dasd_alias_disconnect_device_from_lcu(device);
2112 out_err2:
2113 	dasd_free_block(device->block);
2114 	device->block = NULL;
2115 out_err1:
2116 	dasd_eckd_clear_conf_data(device);
2117 	kfree(device->private);
2118 	device->private = NULL;
2119 	return rc;
2120 }
2121 
2122 static void dasd_eckd_uncheck_device(struct dasd_device *device)
2123 {
2124 	struct dasd_eckd_private *private = device->private;
2125 
2126 	if (!private)
2127 		return;
2128 
2129 	dasd_alias_disconnect_device_from_lcu(device);
2130 	private->ned = NULL;
2131 	private->sneq = NULL;
2132 	private->vdsneq = NULL;
2133 	private->gneq = NULL;
2134 	dasd_eckd_clear_conf_data(device);
2135 }
2136 
2137 static struct dasd_ccw_req *
2138 dasd_eckd_analysis_ccw(struct dasd_device *device)
2139 {
2140 	struct dasd_eckd_private *private = device->private;
2141 	struct eckd_count *count_data;
2142 	struct LO_eckd_data *LO_data;
2143 	struct dasd_ccw_req *cqr;
2144 	struct ccw1 *ccw;
2145 	int cplength, datasize;
2146 	int i;
2147 
2148 	cplength = 8;
2149 	datasize = sizeof(struct DE_eckd_data) + 2*sizeof(struct LO_eckd_data);
2150 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength, datasize, device,
2151 				   NULL);
2152 	if (IS_ERR(cqr))
2153 		return cqr;
2154 	ccw = cqr->cpaddr;
2155 	/* Define extent for the first 2 tracks. */
2156 	define_extent(ccw++, cqr->data, 0, 1,
2157 		      DASD_ECKD_CCW_READ_COUNT, device, 0);
2158 	LO_data = cqr->data + sizeof(struct DE_eckd_data);
2159 	/* Locate record for the first 4 records on track 0. */
2160 	ccw[-1].flags |= CCW_FLAG_CC;
2161 	locate_record(ccw++, LO_data++, 0, 0, 4,
2162 		      DASD_ECKD_CCW_READ_COUNT, device, 0);
2163 
2164 	count_data = private->count_area;
2165 	for (i = 0; i < 4; i++) {
2166 		ccw[-1].flags |= CCW_FLAG_CC;
2167 		ccw->cmd_code = DASD_ECKD_CCW_READ_COUNT;
2168 		ccw->flags = 0;
2169 		ccw->count = 8;
2170 		ccw->cda = (__u32)(addr_t) count_data;
2171 		ccw++;
2172 		count_data++;
2173 	}
2174 
2175 	/* Locate record for the first record on track 1. */
2176 	ccw[-1].flags |= CCW_FLAG_CC;
2177 	locate_record(ccw++, LO_data++, 1, 0, 1,
2178 		      DASD_ECKD_CCW_READ_COUNT, device, 0);
2179 	/* Read count ccw. */
2180 	ccw[-1].flags |= CCW_FLAG_CC;
2181 	ccw->cmd_code = DASD_ECKD_CCW_READ_COUNT;
2182 	ccw->flags = 0;
2183 	ccw->count = 8;
2184 	ccw->cda = (__u32)(addr_t) count_data;
2185 
2186 	cqr->block = NULL;
2187 	cqr->startdev = device;
2188 	cqr->memdev = device;
2189 	cqr->retries = 255;
2190 	cqr->buildclk = get_tod_clock();
2191 	cqr->status = DASD_CQR_FILLED;
2192 	/* Set flags to suppress output for expected errors */
2193 	set_bit(DASD_CQR_SUPPRESS_NRF, &cqr->flags);
2194 
2195 	return cqr;
2196 }
2197 
2198 /* differentiate between 'no record found' and any other error */
2199 static int dasd_eckd_analysis_evaluation(struct dasd_ccw_req *init_cqr)
2200 {
2201 	char *sense;
2202 	if (init_cqr->status == DASD_CQR_DONE)
2203 		return INIT_CQR_OK;
2204 	else if (init_cqr->status == DASD_CQR_NEED_ERP ||
2205 		 init_cqr->status == DASD_CQR_FAILED) {
2206 		sense = dasd_get_sense(&init_cqr->irb);
2207 		if (sense && (sense[1] & SNS1_NO_REC_FOUND))
2208 			return INIT_CQR_UNFORMATTED;
2209 		else
2210 			return INIT_CQR_ERROR;
2211 	} else
2212 		return INIT_CQR_ERROR;
2213 }
2214 
2215 /*
2216  * This is the callback function for the init_analysis cqr. It saves
2217  * the status of the initial analysis ccw before it frees it and kicks
2218  * the device to continue the startup sequence. This will call
2219  * dasd_eckd_do_analysis again (if the devices has not been marked
2220  * for deletion in the meantime).
2221  */
2222 static void dasd_eckd_analysis_callback(struct dasd_ccw_req *init_cqr,
2223 					void *data)
2224 {
2225 	struct dasd_device *device = init_cqr->startdev;
2226 	struct dasd_eckd_private *private = device->private;
2227 
2228 	private->init_cqr_status = dasd_eckd_analysis_evaluation(init_cqr);
2229 	dasd_sfree_request(init_cqr, device);
2230 	dasd_kick_device(device);
2231 }
2232 
2233 static int dasd_eckd_start_analysis(struct dasd_block *block)
2234 {
2235 	struct dasd_ccw_req *init_cqr;
2236 
2237 	init_cqr = dasd_eckd_analysis_ccw(block->base);
2238 	if (IS_ERR(init_cqr))
2239 		return PTR_ERR(init_cqr);
2240 	init_cqr->callback = dasd_eckd_analysis_callback;
2241 	init_cqr->callback_data = NULL;
2242 	init_cqr->expires = 5*HZ;
2243 	/* first try without ERP, so we can later handle unformatted
2244 	 * devices as special case
2245 	 */
2246 	clear_bit(DASD_CQR_FLAGS_USE_ERP, &init_cqr->flags);
2247 	init_cqr->retries = 0;
2248 	dasd_add_request_head(init_cqr);
2249 	return -EAGAIN;
2250 }
2251 
2252 static int dasd_eckd_end_analysis(struct dasd_block *block)
2253 {
2254 	struct dasd_device *device = block->base;
2255 	struct dasd_eckd_private *private = device->private;
2256 	struct eckd_count *count_area;
2257 	unsigned int sb, blk_per_trk;
2258 	int status, i;
2259 	struct dasd_ccw_req *init_cqr;
2260 
2261 	status = private->init_cqr_status;
2262 	private->init_cqr_status = -1;
2263 	if (status == INIT_CQR_ERROR) {
2264 		/* try again, this time with full ERP */
2265 		init_cqr = dasd_eckd_analysis_ccw(device);
2266 		dasd_sleep_on(init_cqr);
2267 		status = dasd_eckd_analysis_evaluation(init_cqr);
2268 		dasd_sfree_request(init_cqr, device);
2269 	}
2270 
2271 	if (device->features & DASD_FEATURE_USERAW) {
2272 		block->bp_block = DASD_RAW_BLOCKSIZE;
2273 		blk_per_trk = DASD_RAW_BLOCK_PER_TRACK;
2274 		block->s2b_shift = 3;
2275 		goto raw;
2276 	}
2277 
2278 	if (status == INIT_CQR_UNFORMATTED) {
2279 		dev_warn(&device->cdev->dev, "The DASD is not formatted\n");
2280 		return -EMEDIUMTYPE;
2281 	} else if (status == INIT_CQR_ERROR) {
2282 		dev_err(&device->cdev->dev,
2283 			"Detecting the DASD disk layout failed because "
2284 			"of an I/O error\n");
2285 		return -EIO;
2286 	}
2287 
2288 	private->uses_cdl = 1;
2289 	/* Check Track 0 for Compatible Disk Layout */
2290 	count_area = NULL;
2291 	for (i = 0; i < 3; i++) {
2292 		if (private->count_area[i].kl != 4 ||
2293 		    private->count_area[i].dl != dasd_eckd_cdl_reclen(i) - 4 ||
2294 		    private->count_area[i].cyl != 0 ||
2295 		    private->count_area[i].head != count_area_head[i] ||
2296 		    private->count_area[i].record != count_area_rec[i]) {
2297 			private->uses_cdl = 0;
2298 			break;
2299 		}
2300 	}
2301 	if (i == 3)
2302 		count_area = &private->count_area[3];
2303 
2304 	if (private->uses_cdl == 0) {
2305 		for (i = 0; i < 5; i++) {
2306 			if ((private->count_area[i].kl != 0) ||
2307 			    (private->count_area[i].dl !=
2308 			     private->count_area[0].dl) ||
2309 			    private->count_area[i].cyl !=  0 ||
2310 			    private->count_area[i].head != count_area_head[i] ||
2311 			    private->count_area[i].record != count_area_rec[i])
2312 				break;
2313 		}
2314 		if (i == 5)
2315 			count_area = &private->count_area[0];
2316 	} else {
2317 		if (private->count_area[3].record == 1)
2318 			dev_warn(&device->cdev->dev,
2319 				 "Track 0 has no records following the VTOC\n");
2320 	}
2321 
2322 	if (count_area != NULL && count_area->kl == 0) {
2323 		/* we found notthing violating our disk layout */
2324 		if (dasd_check_blocksize(count_area->dl) == 0)
2325 			block->bp_block = count_area->dl;
2326 	}
2327 	if (block->bp_block == 0) {
2328 		dev_warn(&device->cdev->dev,
2329 			 "The disk layout of the DASD is not supported\n");
2330 		return -EMEDIUMTYPE;
2331 	}
2332 	block->s2b_shift = 0;	/* bits to shift 512 to get a block */
2333 	for (sb = 512; sb < block->bp_block; sb = sb << 1)
2334 		block->s2b_shift++;
2335 
2336 	blk_per_trk = recs_per_track(&private->rdc_data, 0, block->bp_block);
2337 
2338 raw:
2339 	block->blocks = ((unsigned long) private->real_cyl *
2340 			  private->rdc_data.trk_per_cyl *
2341 			  blk_per_trk);
2342 
2343 	dev_info(&device->cdev->dev,
2344 		 "DASD with %u KB/block, %lu KB total size, %u KB/track, "
2345 		 "%s\n", (block->bp_block >> 10),
2346 		 (((unsigned long) private->real_cyl *
2347 		   private->rdc_data.trk_per_cyl *
2348 		   blk_per_trk * (block->bp_block >> 9)) >> 1),
2349 		 ((blk_per_trk * block->bp_block) >> 10),
2350 		 private->uses_cdl ?
2351 		 "compatible disk layout" : "linux disk layout");
2352 
2353 	return 0;
2354 }
2355 
2356 static int dasd_eckd_do_analysis(struct dasd_block *block)
2357 {
2358 	struct dasd_eckd_private *private = block->base->private;
2359 
2360 	if (private->init_cqr_status < 0)
2361 		return dasd_eckd_start_analysis(block);
2362 	else
2363 		return dasd_eckd_end_analysis(block);
2364 }
2365 
2366 static int dasd_eckd_basic_to_ready(struct dasd_device *device)
2367 {
2368 	return dasd_alias_add_device(device);
2369 };
2370 
2371 static int dasd_eckd_online_to_ready(struct dasd_device *device)
2372 {
2373 	if (cancel_work_sync(&device->reload_device))
2374 		dasd_put_device(device);
2375 	if (cancel_work_sync(&device->kick_validate))
2376 		dasd_put_device(device);
2377 
2378 	return 0;
2379 };
2380 
2381 static int dasd_eckd_basic_to_known(struct dasd_device *device)
2382 {
2383 	return dasd_alias_remove_device(device);
2384 };
2385 
2386 static int
2387 dasd_eckd_fill_geometry(struct dasd_block *block, struct hd_geometry *geo)
2388 {
2389 	struct dasd_eckd_private *private = block->base->private;
2390 
2391 	if (dasd_check_blocksize(block->bp_block) == 0) {
2392 		geo->sectors = recs_per_track(&private->rdc_data,
2393 					      0, block->bp_block);
2394 	}
2395 	geo->cylinders = private->rdc_data.no_cyl;
2396 	geo->heads = private->rdc_data.trk_per_cyl;
2397 	return 0;
2398 }
2399 
2400 /*
2401  * Build the TCW request for the format check
2402  */
2403 static struct dasd_ccw_req *
2404 dasd_eckd_build_check_tcw(struct dasd_device *base, struct format_data_t *fdata,
2405 			  int enable_pav, struct eckd_count *fmt_buffer,
2406 			  int rpt)
2407 {
2408 	struct dasd_eckd_private *start_priv;
2409 	struct dasd_device *startdev = NULL;
2410 	struct tidaw *last_tidaw = NULL;
2411 	struct dasd_ccw_req *cqr;
2412 	struct itcw *itcw;
2413 	int itcw_size;
2414 	int count;
2415 	int rc;
2416 	int i;
2417 
2418 	if (enable_pav)
2419 		startdev = dasd_alias_get_start_dev(base);
2420 
2421 	if (!startdev)
2422 		startdev = base;
2423 
2424 	start_priv = startdev->private;
2425 
2426 	count = rpt * (fdata->stop_unit - fdata->start_unit + 1);
2427 
2428 	/*
2429 	 * we're adding 'count' amount of tidaw to the itcw.
2430 	 * calculate the corresponding itcw_size
2431 	 */
2432 	itcw_size = itcw_calc_size(0, count, 0);
2433 
2434 	cqr = dasd_fmalloc_request(DASD_ECKD_MAGIC, 0, itcw_size, startdev);
2435 	if (IS_ERR(cqr))
2436 		return cqr;
2437 
2438 	start_priv->count++;
2439 
2440 	itcw = itcw_init(cqr->data, itcw_size, ITCW_OP_READ, 0, count, 0);
2441 	if (IS_ERR(itcw)) {
2442 		rc = -EINVAL;
2443 		goto out_err;
2444 	}
2445 
2446 	cqr->cpaddr = itcw_get_tcw(itcw);
2447 	rc = prepare_itcw(itcw, fdata->start_unit, fdata->stop_unit,
2448 			  DASD_ECKD_CCW_READ_COUNT_MT, base, startdev, 0, count,
2449 			  sizeof(struct eckd_count),
2450 			  count * sizeof(struct eckd_count), 0, rpt);
2451 	if (rc)
2452 		goto out_err;
2453 
2454 	for (i = 0; i < count; i++) {
2455 		last_tidaw = itcw_add_tidaw(itcw, 0, fmt_buffer++,
2456 					    sizeof(struct eckd_count));
2457 		if (IS_ERR(last_tidaw)) {
2458 			rc = -EINVAL;
2459 			goto out_err;
2460 		}
2461 	}
2462 
2463 	last_tidaw->flags |= TIDAW_FLAGS_LAST;
2464 	itcw_finalize(itcw);
2465 
2466 	cqr->cpmode = 1;
2467 	cqr->startdev = startdev;
2468 	cqr->memdev = startdev;
2469 	cqr->basedev = base;
2470 	cqr->retries = startdev->default_retries;
2471 	cqr->expires = startdev->default_expires * HZ;
2472 	cqr->buildclk = get_tod_clock();
2473 	cqr->status = DASD_CQR_FILLED;
2474 	/* Set flags to suppress output for expected errors */
2475 	set_bit(DASD_CQR_SUPPRESS_FP, &cqr->flags);
2476 	set_bit(DASD_CQR_SUPPRESS_IL, &cqr->flags);
2477 
2478 	return cqr;
2479 
2480 out_err:
2481 	dasd_sfree_request(cqr, startdev);
2482 
2483 	return ERR_PTR(rc);
2484 }
2485 
2486 /*
2487  * Build the CCW request for the format check
2488  */
2489 static struct dasd_ccw_req *
2490 dasd_eckd_build_check(struct dasd_device *base, struct format_data_t *fdata,
2491 		      int enable_pav, struct eckd_count *fmt_buffer, int rpt)
2492 {
2493 	struct dasd_eckd_private *start_priv;
2494 	struct dasd_eckd_private *base_priv;
2495 	struct dasd_device *startdev = NULL;
2496 	struct dasd_ccw_req *cqr;
2497 	struct ccw1 *ccw;
2498 	void *data;
2499 	int cplength, datasize;
2500 	int use_prefix;
2501 	int count;
2502 	int i;
2503 
2504 	if (enable_pav)
2505 		startdev = dasd_alias_get_start_dev(base);
2506 
2507 	if (!startdev)
2508 		startdev = base;
2509 
2510 	start_priv = startdev->private;
2511 	base_priv = base->private;
2512 
2513 	count = rpt * (fdata->stop_unit - fdata->start_unit + 1);
2514 
2515 	use_prefix = base_priv->features.feature[8] & 0x01;
2516 
2517 	if (use_prefix) {
2518 		cplength = 1;
2519 		datasize = sizeof(struct PFX_eckd_data);
2520 	} else {
2521 		cplength = 2;
2522 		datasize = sizeof(struct DE_eckd_data) +
2523 			sizeof(struct LO_eckd_data);
2524 	}
2525 	cplength += count;
2526 
2527 	cqr = dasd_fmalloc_request(DASD_ECKD_MAGIC, cplength, datasize, startdev);
2528 	if (IS_ERR(cqr))
2529 		return cqr;
2530 
2531 	start_priv->count++;
2532 	data = cqr->data;
2533 	ccw = cqr->cpaddr;
2534 
2535 	if (use_prefix) {
2536 		prefix_LRE(ccw++, data, fdata->start_unit, fdata->stop_unit,
2537 			   DASD_ECKD_CCW_READ_COUNT, base, startdev, 1, 0,
2538 			   count, 0, 0);
2539 	} else {
2540 		define_extent(ccw++, data, fdata->start_unit, fdata->stop_unit,
2541 			      DASD_ECKD_CCW_READ_COUNT, startdev, 0);
2542 
2543 		data += sizeof(struct DE_eckd_data);
2544 		ccw[-1].flags |= CCW_FLAG_CC;
2545 
2546 		locate_record(ccw++, data, fdata->start_unit, 0, count,
2547 			      DASD_ECKD_CCW_READ_COUNT, base, 0);
2548 	}
2549 
2550 	for (i = 0; i < count; i++) {
2551 		ccw[-1].flags |= CCW_FLAG_CC;
2552 		ccw->cmd_code = DASD_ECKD_CCW_READ_COUNT;
2553 		ccw->flags = CCW_FLAG_SLI;
2554 		ccw->count = 8;
2555 		ccw->cda = (__u32)(addr_t) fmt_buffer;
2556 		ccw++;
2557 		fmt_buffer++;
2558 	}
2559 
2560 	cqr->startdev = startdev;
2561 	cqr->memdev = startdev;
2562 	cqr->basedev = base;
2563 	cqr->retries = DASD_RETRIES;
2564 	cqr->expires = startdev->default_expires * HZ;
2565 	cqr->buildclk = get_tod_clock();
2566 	cqr->status = DASD_CQR_FILLED;
2567 	/* Set flags to suppress output for expected errors */
2568 	set_bit(DASD_CQR_SUPPRESS_NRF, &cqr->flags);
2569 
2570 	return cqr;
2571 }
2572 
2573 static struct dasd_ccw_req *
2574 dasd_eckd_build_format(struct dasd_device *base, struct dasd_device *startdev,
2575 		       struct format_data_t *fdata, int enable_pav)
2576 {
2577 	struct dasd_eckd_private *base_priv;
2578 	struct dasd_eckd_private *start_priv;
2579 	struct dasd_ccw_req *fcp;
2580 	struct eckd_count *ect;
2581 	struct ch_t address;
2582 	struct ccw1 *ccw;
2583 	void *data;
2584 	int rpt;
2585 	int cplength, datasize;
2586 	int i, j;
2587 	int intensity = 0;
2588 	int r0_perm;
2589 	int nr_tracks;
2590 	int use_prefix;
2591 
2592 	if (enable_pav)
2593 		startdev = dasd_alias_get_start_dev(base);
2594 
2595 	if (!startdev)
2596 		startdev = base;
2597 
2598 	start_priv = startdev->private;
2599 	base_priv = base->private;
2600 
2601 	rpt = recs_per_track(&base_priv->rdc_data, 0, fdata->blksize);
2602 
2603 	nr_tracks = fdata->stop_unit - fdata->start_unit + 1;
2604 
2605 	/*
2606 	 * fdata->intensity is a bit string that tells us what to do:
2607 	 *   Bit 0: write record zero
2608 	 *   Bit 1: write home address, currently not supported
2609 	 *   Bit 2: invalidate tracks
2610 	 *   Bit 3: use OS/390 compatible disk layout (cdl)
2611 	 *   Bit 4: do not allow storage subsystem to modify record zero
2612 	 * Only some bit combinations do make sense.
2613 	 */
2614 	if (fdata->intensity & 0x10) {
2615 		r0_perm = 0;
2616 		intensity = fdata->intensity & ~0x10;
2617 	} else {
2618 		r0_perm = 1;
2619 		intensity = fdata->intensity;
2620 	}
2621 
2622 	use_prefix = base_priv->features.feature[8] & 0x01;
2623 
2624 	switch (intensity) {
2625 	case 0x00:	/* Normal format */
2626 	case 0x08:	/* Normal format, use cdl. */
2627 		cplength = 2 + (rpt*nr_tracks);
2628 		if (use_prefix)
2629 			datasize = sizeof(struct PFX_eckd_data) +
2630 				sizeof(struct LO_eckd_data) +
2631 				rpt * nr_tracks * sizeof(struct eckd_count);
2632 		else
2633 			datasize = sizeof(struct DE_eckd_data) +
2634 				sizeof(struct LO_eckd_data) +
2635 				rpt * nr_tracks * sizeof(struct eckd_count);
2636 		break;
2637 	case 0x01:	/* Write record zero and format track. */
2638 	case 0x09:	/* Write record zero and format track, use cdl. */
2639 		cplength = 2 + rpt * nr_tracks;
2640 		if (use_prefix)
2641 			datasize = sizeof(struct PFX_eckd_data) +
2642 				sizeof(struct LO_eckd_data) +
2643 				sizeof(struct eckd_count) +
2644 				rpt * nr_tracks * sizeof(struct eckd_count);
2645 		else
2646 			datasize = sizeof(struct DE_eckd_data) +
2647 				sizeof(struct LO_eckd_data) +
2648 				sizeof(struct eckd_count) +
2649 				rpt * nr_tracks * sizeof(struct eckd_count);
2650 		break;
2651 	case 0x04:	/* Invalidate track. */
2652 	case 0x0c:	/* Invalidate track, use cdl. */
2653 		cplength = 3;
2654 		if (use_prefix)
2655 			datasize = sizeof(struct PFX_eckd_data) +
2656 				sizeof(struct LO_eckd_data) +
2657 				sizeof(struct eckd_count);
2658 		else
2659 			datasize = sizeof(struct DE_eckd_data) +
2660 				sizeof(struct LO_eckd_data) +
2661 				sizeof(struct eckd_count);
2662 		break;
2663 	default:
2664 		dev_warn(&startdev->cdev->dev,
2665 			 "An I/O control call used incorrect flags 0x%x\n",
2666 			 fdata->intensity);
2667 		return ERR_PTR(-EINVAL);
2668 	}
2669 
2670 	fcp = dasd_fmalloc_request(DASD_ECKD_MAGIC, cplength, datasize, startdev);
2671 	if (IS_ERR(fcp))
2672 		return fcp;
2673 
2674 	start_priv->count++;
2675 	data = fcp->data;
2676 	ccw = fcp->cpaddr;
2677 
2678 	switch (intensity & ~0x08) {
2679 	case 0x00: /* Normal format. */
2680 		if (use_prefix) {
2681 			prefix(ccw++, (struct PFX_eckd_data *) data,
2682 			       fdata->start_unit, fdata->stop_unit,
2683 			       DASD_ECKD_CCW_WRITE_CKD, base, startdev);
2684 			/* grant subsystem permission to format R0 */
2685 			if (r0_perm)
2686 				((struct PFX_eckd_data *)data)
2687 					->define_extent.ga_extended |= 0x04;
2688 			data += sizeof(struct PFX_eckd_data);
2689 		} else {
2690 			define_extent(ccw++, (struct DE_eckd_data *) data,
2691 				      fdata->start_unit, fdata->stop_unit,
2692 				      DASD_ECKD_CCW_WRITE_CKD, startdev, 0);
2693 			/* grant subsystem permission to format R0 */
2694 			if (r0_perm)
2695 				((struct DE_eckd_data *) data)
2696 					->ga_extended |= 0x04;
2697 			data += sizeof(struct DE_eckd_data);
2698 		}
2699 		ccw[-1].flags |= CCW_FLAG_CC;
2700 		locate_record(ccw++, (struct LO_eckd_data *) data,
2701 			      fdata->start_unit, 0, rpt*nr_tracks,
2702 			      DASD_ECKD_CCW_WRITE_CKD, base,
2703 			      fdata->blksize);
2704 		data += sizeof(struct LO_eckd_data);
2705 		break;
2706 	case 0x01: /* Write record zero + format track. */
2707 		if (use_prefix) {
2708 			prefix(ccw++, (struct PFX_eckd_data *) data,
2709 			       fdata->start_unit, fdata->stop_unit,
2710 			       DASD_ECKD_CCW_WRITE_RECORD_ZERO,
2711 			       base, startdev);
2712 			data += sizeof(struct PFX_eckd_data);
2713 		} else {
2714 			define_extent(ccw++, (struct DE_eckd_data *) data,
2715 			       fdata->start_unit, fdata->stop_unit,
2716 			       DASD_ECKD_CCW_WRITE_RECORD_ZERO, startdev, 0);
2717 			data += sizeof(struct DE_eckd_data);
2718 		}
2719 		ccw[-1].flags |= CCW_FLAG_CC;
2720 		locate_record(ccw++, (struct LO_eckd_data *) data,
2721 			      fdata->start_unit, 0, rpt * nr_tracks + 1,
2722 			      DASD_ECKD_CCW_WRITE_RECORD_ZERO, base,
2723 			      base->block->bp_block);
2724 		data += sizeof(struct LO_eckd_data);
2725 		break;
2726 	case 0x04: /* Invalidate track. */
2727 		if (use_prefix) {
2728 			prefix(ccw++, (struct PFX_eckd_data *) data,
2729 			       fdata->start_unit, fdata->stop_unit,
2730 			       DASD_ECKD_CCW_WRITE_CKD, base, startdev);
2731 			data += sizeof(struct PFX_eckd_data);
2732 		} else {
2733 			define_extent(ccw++, (struct DE_eckd_data *) data,
2734 			       fdata->start_unit, fdata->stop_unit,
2735 			       DASD_ECKD_CCW_WRITE_CKD, startdev, 0);
2736 			data += sizeof(struct DE_eckd_data);
2737 		}
2738 		ccw[-1].flags |= CCW_FLAG_CC;
2739 		locate_record(ccw++, (struct LO_eckd_data *) data,
2740 			      fdata->start_unit, 0, 1,
2741 			      DASD_ECKD_CCW_WRITE_CKD, base, 8);
2742 		data += sizeof(struct LO_eckd_data);
2743 		break;
2744 	}
2745 
2746 	for (j = 0; j < nr_tracks; j++) {
2747 		/* calculate cylinder and head for the current track */
2748 		set_ch_t(&address,
2749 			 (fdata->start_unit + j) /
2750 			 base_priv->rdc_data.trk_per_cyl,
2751 			 (fdata->start_unit + j) %
2752 			 base_priv->rdc_data.trk_per_cyl);
2753 		if (intensity & 0x01) {	/* write record zero */
2754 			ect = (struct eckd_count *) data;
2755 			data += sizeof(struct eckd_count);
2756 			ect->cyl = address.cyl;
2757 			ect->head = address.head;
2758 			ect->record = 0;
2759 			ect->kl = 0;
2760 			ect->dl = 8;
2761 			ccw[-1].flags |= CCW_FLAG_CC;
2762 			ccw->cmd_code = DASD_ECKD_CCW_WRITE_RECORD_ZERO;
2763 			ccw->flags = CCW_FLAG_SLI;
2764 			ccw->count = 8;
2765 			ccw->cda = (__u32)(addr_t) ect;
2766 			ccw++;
2767 		}
2768 		if ((intensity & ~0x08) & 0x04) {	/* erase track */
2769 			ect = (struct eckd_count *) data;
2770 			data += sizeof(struct eckd_count);
2771 			ect->cyl = address.cyl;
2772 			ect->head = address.head;
2773 			ect->record = 1;
2774 			ect->kl = 0;
2775 			ect->dl = 0;
2776 			ccw[-1].flags |= CCW_FLAG_CC;
2777 			ccw->cmd_code = DASD_ECKD_CCW_WRITE_CKD;
2778 			ccw->flags = CCW_FLAG_SLI;
2779 			ccw->count = 8;
2780 			ccw->cda = (__u32)(addr_t) ect;
2781 		} else {		/* write remaining records */
2782 			for (i = 0; i < rpt; i++) {
2783 				ect = (struct eckd_count *) data;
2784 				data += sizeof(struct eckd_count);
2785 				ect->cyl = address.cyl;
2786 				ect->head = address.head;
2787 				ect->record = i + 1;
2788 				ect->kl = 0;
2789 				ect->dl = fdata->blksize;
2790 				/*
2791 				 * Check for special tracks 0-1
2792 				 * when formatting CDL
2793 				 */
2794 				if ((intensity & 0x08) &&
2795 				    address.cyl == 0 && address.head == 0) {
2796 					if (i < 3) {
2797 						ect->kl = 4;
2798 						ect->dl = sizes_trk0[i] - 4;
2799 					}
2800 				}
2801 				if ((intensity & 0x08) &&
2802 				    address.cyl == 0 && address.head == 1) {
2803 					ect->kl = 44;
2804 					ect->dl = LABEL_SIZE - 44;
2805 				}
2806 				ccw[-1].flags |= CCW_FLAG_CC;
2807 				if (i != 0 || j == 0)
2808 					ccw->cmd_code =
2809 						DASD_ECKD_CCW_WRITE_CKD;
2810 				else
2811 					ccw->cmd_code =
2812 						DASD_ECKD_CCW_WRITE_CKD_MT;
2813 				ccw->flags = CCW_FLAG_SLI;
2814 				ccw->count = 8;
2815 				ccw->cda = (__u32)(addr_t) ect;
2816 				ccw++;
2817 			}
2818 		}
2819 	}
2820 
2821 	fcp->startdev = startdev;
2822 	fcp->memdev = startdev;
2823 	fcp->basedev = base;
2824 	fcp->retries = 256;
2825 	fcp->expires = startdev->default_expires * HZ;
2826 	fcp->buildclk = get_tod_clock();
2827 	fcp->status = DASD_CQR_FILLED;
2828 
2829 	return fcp;
2830 }
2831 
2832 /*
2833  * Wrapper function to build a CCW request depending on input data
2834  */
2835 static struct dasd_ccw_req *
2836 dasd_eckd_format_build_ccw_req(struct dasd_device *base,
2837 			       struct format_data_t *fdata, int enable_pav,
2838 			       int tpm, struct eckd_count *fmt_buffer, int rpt)
2839 {
2840 	struct dasd_ccw_req *ccw_req;
2841 
2842 	if (!fmt_buffer) {
2843 		ccw_req = dasd_eckd_build_format(base, NULL, fdata, enable_pav);
2844 	} else {
2845 		if (tpm)
2846 			ccw_req = dasd_eckd_build_check_tcw(base, fdata,
2847 							    enable_pav,
2848 							    fmt_buffer, rpt);
2849 		else
2850 			ccw_req = dasd_eckd_build_check(base, fdata, enable_pav,
2851 							fmt_buffer, rpt);
2852 	}
2853 
2854 	return ccw_req;
2855 }
2856 
2857 /*
2858  * Sanity checks on format_data
2859  */
2860 static int dasd_eckd_format_sanity_checks(struct dasd_device *base,
2861 					  struct format_data_t *fdata)
2862 {
2863 	struct dasd_eckd_private *private = base->private;
2864 
2865 	if (fdata->start_unit >=
2866 	    (private->real_cyl * private->rdc_data.trk_per_cyl)) {
2867 		dev_warn(&base->cdev->dev,
2868 			 "Start track number %u used in formatting is too big\n",
2869 			 fdata->start_unit);
2870 		return -EINVAL;
2871 	}
2872 	if (fdata->stop_unit >=
2873 	    (private->real_cyl * private->rdc_data.trk_per_cyl)) {
2874 		dev_warn(&base->cdev->dev,
2875 			 "Stop track number %u used in formatting is too big\n",
2876 			 fdata->stop_unit);
2877 		return -EINVAL;
2878 	}
2879 	if (fdata->start_unit > fdata->stop_unit) {
2880 		dev_warn(&base->cdev->dev,
2881 			 "Start track %u used in formatting exceeds end track\n",
2882 			 fdata->start_unit);
2883 		return -EINVAL;
2884 	}
2885 	if (dasd_check_blocksize(fdata->blksize) != 0) {
2886 		dev_warn(&base->cdev->dev,
2887 			 "The DASD cannot be formatted with block size %u\n",
2888 			 fdata->blksize);
2889 		return -EINVAL;
2890 	}
2891 	return 0;
2892 }
2893 
2894 /*
2895  * This function will process format_data originally coming from an IOCTL
2896  */
2897 static int dasd_eckd_format_process_data(struct dasd_device *base,
2898 					 struct format_data_t *fdata,
2899 					 int enable_pav, int tpm,
2900 					 struct eckd_count *fmt_buffer, int rpt,
2901 					 struct irb *irb)
2902 {
2903 	struct dasd_eckd_private *private = base->private;
2904 	struct dasd_ccw_req *cqr, *n;
2905 	struct list_head format_queue;
2906 	struct dasd_device *device;
2907 	char *sense = NULL;
2908 	int old_start, old_stop, format_step;
2909 	int step, retry;
2910 	int rc;
2911 
2912 	rc = dasd_eckd_format_sanity_checks(base, fdata);
2913 	if (rc)
2914 		return rc;
2915 
2916 	INIT_LIST_HEAD(&format_queue);
2917 
2918 	old_start = fdata->start_unit;
2919 	old_stop = fdata->stop_unit;
2920 
2921 	if (!tpm && fmt_buffer != NULL) {
2922 		/* Command Mode / Format Check */
2923 		format_step = 1;
2924 	} else if (tpm && fmt_buffer != NULL) {
2925 		/* Transport Mode / Format Check */
2926 		format_step = DASD_CQR_MAX_CCW / rpt;
2927 	} else {
2928 		/* Normal Formatting */
2929 		format_step = DASD_CQR_MAX_CCW /
2930 			recs_per_track(&private->rdc_data, 0, fdata->blksize);
2931 	}
2932 
2933 	do {
2934 		retry = 0;
2935 		while (fdata->start_unit <= old_stop) {
2936 			step = fdata->stop_unit - fdata->start_unit + 1;
2937 			if (step > format_step) {
2938 				fdata->stop_unit =
2939 					fdata->start_unit + format_step - 1;
2940 			}
2941 
2942 			cqr = dasd_eckd_format_build_ccw_req(base, fdata,
2943 							     enable_pav, tpm,
2944 							     fmt_buffer, rpt);
2945 			if (IS_ERR(cqr)) {
2946 				rc = PTR_ERR(cqr);
2947 				if (rc == -ENOMEM) {
2948 					if (list_empty(&format_queue))
2949 						goto out;
2950 					/*
2951 					 * not enough memory available, start
2952 					 * requests retry after first requests
2953 					 * were finished
2954 					 */
2955 					retry = 1;
2956 					break;
2957 				}
2958 				goto out_err;
2959 			}
2960 			list_add_tail(&cqr->blocklist, &format_queue);
2961 
2962 			if (fmt_buffer) {
2963 				step = fdata->stop_unit - fdata->start_unit + 1;
2964 				fmt_buffer += rpt * step;
2965 			}
2966 			fdata->start_unit = fdata->stop_unit + 1;
2967 			fdata->stop_unit = old_stop;
2968 		}
2969 
2970 		rc = dasd_sleep_on_queue(&format_queue);
2971 
2972 out_err:
2973 		list_for_each_entry_safe(cqr, n, &format_queue, blocklist) {
2974 			device = cqr->startdev;
2975 			private = device->private;
2976 
2977 			if (cqr->status == DASD_CQR_FAILED) {
2978 				/*
2979 				 * Only get sense data if called by format
2980 				 * check
2981 				 */
2982 				if (fmt_buffer && irb) {
2983 					sense = dasd_get_sense(&cqr->irb);
2984 					memcpy(irb, &cqr->irb, sizeof(*irb));
2985 				}
2986 				rc = -EIO;
2987 			}
2988 			list_del_init(&cqr->blocklist);
2989 			dasd_ffree_request(cqr, device);
2990 			private->count--;
2991 		}
2992 
2993 		if (rc && rc != -EIO)
2994 			goto out;
2995 		if (rc == -EIO) {
2996 			/*
2997 			 * In case fewer than the expected records are on the
2998 			 * track, we will most likely get a 'No Record Found'
2999 			 * error (in command mode) or a 'File Protected' error
3000 			 * (in transport mode). Those particular cases shouldn't
3001 			 * pass the -EIO to the IOCTL, therefore reset the rc
3002 			 * and continue.
3003 			 */
3004 			if (sense &&
3005 			    (sense[1] & SNS1_NO_REC_FOUND ||
3006 			     sense[1] & SNS1_FILE_PROTECTED))
3007 				retry = 1;
3008 			else
3009 				goto out;
3010 		}
3011 
3012 	} while (retry);
3013 
3014 out:
3015 	fdata->start_unit = old_start;
3016 	fdata->stop_unit = old_stop;
3017 
3018 	return rc;
3019 }
3020 
3021 static int dasd_eckd_format_device(struct dasd_device *base,
3022 				   struct format_data_t *fdata, int enable_pav)
3023 {
3024 	return dasd_eckd_format_process_data(base, fdata, enable_pav, 0, NULL,
3025 					     0, NULL);
3026 }
3027 
3028 static bool test_and_set_format_track(struct dasd_format_entry *to_format,
3029 				      struct dasd_block *block)
3030 {
3031 	struct dasd_format_entry *format;
3032 	unsigned long flags;
3033 	bool rc = false;
3034 
3035 	spin_lock_irqsave(&block->format_lock, flags);
3036 	list_for_each_entry(format, &block->format_list, list) {
3037 		if (format->track == to_format->track) {
3038 			rc = true;
3039 			goto out;
3040 		}
3041 	}
3042 	list_add_tail(&to_format->list, &block->format_list);
3043 
3044 out:
3045 	spin_unlock_irqrestore(&block->format_lock, flags);
3046 	return rc;
3047 }
3048 
3049 static void clear_format_track(struct dasd_format_entry *format,
3050 			      struct dasd_block *block)
3051 {
3052 	unsigned long flags;
3053 
3054 	spin_lock_irqsave(&block->format_lock, flags);
3055 	list_del_init(&format->list);
3056 	spin_unlock_irqrestore(&block->format_lock, flags);
3057 }
3058 
3059 /*
3060  * Callback function to free ESE format requests.
3061  */
3062 static void dasd_eckd_ese_format_cb(struct dasd_ccw_req *cqr, void *data)
3063 {
3064 	struct dasd_device *device = cqr->startdev;
3065 	struct dasd_eckd_private *private = device->private;
3066 	struct dasd_format_entry *format = data;
3067 
3068 	clear_format_track(format, cqr->basedev->block);
3069 	private->count--;
3070 	dasd_ffree_request(cqr, device);
3071 }
3072 
3073 static struct dasd_ccw_req *
3074 dasd_eckd_ese_format(struct dasd_device *startdev, struct dasd_ccw_req *cqr,
3075 		     struct irb *irb)
3076 {
3077 	struct dasd_eckd_private *private;
3078 	struct dasd_format_entry *format;
3079 	struct format_data_t fdata;
3080 	unsigned int recs_per_trk;
3081 	struct dasd_ccw_req *fcqr;
3082 	struct dasd_device *base;
3083 	struct dasd_block *block;
3084 	unsigned int blksize;
3085 	struct request *req;
3086 	sector_t first_trk;
3087 	sector_t last_trk;
3088 	sector_t curr_trk;
3089 	int rc;
3090 
3091 	req = cqr->callback_data;
3092 	block = cqr->block;
3093 	base = block->base;
3094 	private = base->private;
3095 	blksize = block->bp_block;
3096 	recs_per_trk = recs_per_track(&private->rdc_data, 0, blksize);
3097 	format = &startdev->format_entry;
3098 
3099 	first_trk = blk_rq_pos(req) >> block->s2b_shift;
3100 	sector_div(first_trk, recs_per_trk);
3101 	last_trk =
3102 		(blk_rq_pos(req) + blk_rq_sectors(req) - 1) >> block->s2b_shift;
3103 	sector_div(last_trk, recs_per_trk);
3104 	rc = dasd_eckd_track_from_irb(irb, base, &curr_trk);
3105 	if (rc)
3106 		return ERR_PTR(rc);
3107 
3108 	if (curr_trk < first_trk || curr_trk > last_trk) {
3109 		DBF_DEV_EVENT(DBF_WARNING, startdev,
3110 			      "ESE error track %llu not within range %llu - %llu\n",
3111 			      curr_trk, first_trk, last_trk);
3112 		return ERR_PTR(-EINVAL);
3113 	}
3114 	format->track = curr_trk;
3115 	/* test if track is already in formatting by another thread */
3116 	if (test_and_set_format_track(format, block))
3117 		return ERR_PTR(-EEXIST);
3118 
3119 	fdata.start_unit = curr_trk;
3120 	fdata.stop_unit = curr_trk;
3121 	fdata.blksize = blksize;
3122 	fdata.intensity = private->uses_cdl ? DASD_FMT_INT_COMPAT : 0;
3123 
3124 	rc = dasd_eckd_format_sanity_checks(base, &fdata);
3125 	if (rc)
3126 		return ERR_PTR(-EINVAL);
3127 
3128 	/*
3129 	 * We're building the request with PAV disabled as we're reusing
3130 	 * the former startdev.
3131 	 */
3132 	fcqr = dasd_eckd_build_format(base, startdev, &fdata, 0);
3133 	if (IS_ERR(fcqr))
3134 		return fcqr;
3135 
3136 	fcqr->callback = dasd_eckd_ese_format_cb;
3137 	fcqr->callback_data = (void *) format;
3138 
3139 	return fcqr;
3140 }
3141 
3142 /*
3143  * When data is read from an unformatted area of an ESE volume, this function
3144  * returns zeroed data and thereby mimics a read of zero data.
3145  *
3146  * The first unformatted track is the one that got the NRF error, the address is
3147  * encoded in the sense data.
3148  *
3149  * All tracks before have returned valid data and should not be touched.
3150  * All tracks after the unformatted track might be formatted or not. This is
3151  * currently not known, remember the processed data and return the remainder of
3152  * the request to the blocklayer in __dasd_cleanup_cqr().
3153  */
3154 static int dasd_eckd_ese_read(struct dasd_ccw_req *cqr, struct irb *irb)
3155 {
3156 	struct dasd_eckd_private *private;
3157 	sector_t first_trk, last_trk;
3158 	sector_t first_blk, last_blk;
3159 	unsigned int blksize, off;
3160 	unsigned int recs_per_trk;
3161 	struct dasd_device *base;
3162 	struct req_iterator iter;
3163 	struct dasd_block *block;
3164 	unsigned int skip_block;
3165 	unsigned int blk_count;
3166 	struct request *req;
3167 	struct bio_vec bv;
3168 	sector_t curr_trk;
3169 	sector_t end_blk;
3170 	char *dst;
3171 	int rc;
3172 
3173 	req = (struct request *) cqr->callback_data;
3174 	base = cqr->block->base;
3175 	blksize = base->block->bp_block;
3176 	block =  cqr->block;
3177 	private = base->private;
3178 	skip_block = 0;
3179 	blk_count = 0;
3180 
3181 	recs_per_trk = recs_per_track(&private->rdc_data, 0, blksize);
3182 	first_trk = first_blk = blk_rq_pos(req) >> block->s2b_shift;
3183 	sector_div(first_trk, recs_per_trk);
3184 	last_trk = last_blk =
3185 		(blk_rq_pos(req) + blk_rq_sectors(req) - 1) >> block->s2b_shift;
3186 	sector_div(last_trk, recs_per_trk);
3187 	rc = dasd_eckd_track_from_irb(irb, base, &curr_trk);
3188 	if (rc)
3189 		return rc;
3190 
3191 	/* sanity check if the current track from sense data is valid */
3192 	if (curr_trk < first_trk || curr_trk > last_trk) {
3193 		DBF_DEV_EVENT(DBF_WARNING, base,
3194 			      "ESE error track %llu not within range %llu - %llu\n",
3195 			      curr_trk, first_trk, last_trk);
3196 		return -EINVAL;
3197 	}
3198 
3199 	/*
3200 	 * if not the first track got the NRF error we have to skip over valid
3201 	 * blocks
3202 	 */
3203 	if (curr_trk != first_trk)
3204 		skip_block = curr_trk * recs_per_trk - first_blk;
3205 
3206 	/* we have no information beyond the current track */
3207 	end_blk = (curr_trk + 1) * recs_per_trk;
3208 
3209 	rq_for_each_segment(bv, req, iter) {
3210 		dst = page_address(bv.bv_page) + bv.bv_offset;
3211 		for (off = 0; off < bv.bv_len; off += blksize) {
3212 			if (first_blk + blk_count >= end_blk) {
3213 				cqr->proc_bytes = blk_count * blksize;
3214 				return 0;
3215 			}
3216 			if (dst && !skip_block) {
3217 				dst += off;
3218 				memset(dst, 0, blksize);
3219 			} else {
3220 				skip_block--;
3221 			}
3222 			blk_count++;
3223 		}
3224 	}
3225 	return 0;
3226 }
3227 
3228 /*
3229  * Helper function to count consecutive records of a single track.
3230  */
3231 static int dasd_eckd_count_records(struct eckd_count *fmt_buffer, int start,
3232 				   int max)
3233 {
3234 	int head;
3235 	int i;
3236 
3237 	head = fmt_buffer[start].head;
3238 
3239 	/*
3240 	 * There are 3 conditions where we stop counting:
3241 	 * - if data reoccurs (same head and record may reoccur), which may
3242 	 *   happen due to the way DASD_ECKD_CCW_READ_COUNT works
3243 	 * - when the head changes, because we're iterating over several tracks
3244 	 *   then (DASD_ECKD_CCW_READ_COUNT_MT)
3245 	 * - when we've reached the end of sensible data in the buffer (the
3246 	 *   record will be 0 then)
3247 	 */
3248 	for (i = start; i < max; i++) {
3249 		if (i > start) {
3250 			if ((fmt_buffer[i].head == head &&
3251 			    fmt_buffer[i].record == 1) ||
3252 			    fmt_buffer[i].head != head ||
3253 			    fmt_buffer[i].record == 0)
3254 				break;
3255 		}
3256 	}
3257 
3258 	return i - start;
3259 }
3260 
3261 /*
3262  * Evaluate a given range of tracks. Data like number of records, blocksize,
3263  * record ids, and key length are compared with expected data.
3264  *
3265  * If a mismatch occurs, the corresponding error bit is set, as well as
3266  * additional information, depending on the error.
3267  */
3268 static void dasd_eckd_format_evaluate_tracks(struct eckd_count *fmt_buffer,
3269 					     struct format_check_t *cdata,
3270 					     int rpt_max, int rpt_exp,
3271 					     int trk_per_cyl, int tpm)
3272 {
3273 	struct ch_t geo;
3274 	int max_entries;
3275 	int count = 0;
3276 	int trkcount;
3277 	int blksize;
3278 	int pos = 0;
3279 	int i, j;
3280 	int kl;
3281 
3282 	trkcount = cdata->expect.stop_unit - cdata->expect.start_unit + 1;
3283 	max_entries = trkcount * rpt_max;
3284 
3285 	for (i = cdata->expect.start_unit; i <= cdata->expect.stop_unit; i++) {
3286 		/* Calculate the correct next starting position in the buffer */
3287 		if (tpm) {
3288 			while (fmt_buffer[pos].record == 0 &&
3289 			       fmt_buffer[pos].dl == 0) {
3290 				if (pos++ > max_entries)
3291 					break;
3292 			}
3293 		} else {
3294 			if (i != cdata->expect.start_unit)
3295 				pos += rpt_max - count;
3296 		}
3297 
3298 		/* Calculate the expected geo values for the current track */
3299 		set_ch_t(&geo, i / trk_per_cyl, i % trk_per_cyl);
3300 
3301 		/* Count and check number of records */
3302 		count = dasd_eckd_count_records(fmt_buffer, pos, pos + rpt_max);
3303 
3304 		if (count < rpt_exp) {
3305 			cdata->result = DASD_FMT_ERR_TOO_FEW_RECORDS;
3306 			break;
3307 		}
3308 		if (count > rpt_exp) {
3309 			cdata->result = DASD_FMT_ERR_TOO_MANY_RECORDS;
3310 			break;
3311 		}
3312 
3313 		for (j = 0; j < count; j++, pos++) {
3314 			blksize = cdata->expect.blksize;
3315 			kl = 0;
3316 
3317 			/*
3318 			 * Set special values when checking CDL formatted
3319 			 * devices.
3320 			 */
3321 			if ((cdata->expect.intensity & 0x08) &&
3322 			    geo.cyl == 0 && geo.head == 0) {
3323 				if (j < 3) {
3324 					blksize = sizes_trk0[j] - 4;
3325 					kl = 4;
3326 				}
3327 			}
3328 			if ((cdata->expect.intensity & 0x08) &&
3329 			    geo.cyl == 0 && geo.head == 1) {
3330 				blksize = LABEL_SIZE - 44;
3331 				kl = 44;
3332 			}
3333 
3334 			/* Check blocksize */
3335 			if (fmt_buffer[pos].dl != blksize) {
3336 				cdata->result = DASD_FMT_ERR_BLKSIZE;
3337 				goto out;
3338 			}
3339 			/* Check if key length is 0 */
3340 			if (fmt_buffer[pos].kl != kl) {
3341 				cdata->result = DASD_FMT_ERR_KEY_LENGTH;
3342 				goto out;
3343 			}
3344 			/* Check if record_id is correct */
3345 			if (fmt_buffer[pos].cyl != geo.cyl ||
3346 			    fmt_buffer[pos].head != geo.head ||
3347 			    fmt_buffer[pos].record != (j + 1)) {
3348 				cdata->result = DASD_FMT_ERR_RECORD_ID;
3349 				goto out;
3350 			}
3351 		}
3352 	}
3353 
3354 out:
3355 	/*
3356 	 * In case of no errors, we need to decrease by one
3357 	 * to get the correct positions.
3358 	 */
3359 	if (!cdata->result) {
3360 		i--;
3361 		pos--;
3362 	}
3363 
3364 	cdata->unit = i;
3365 	cdata->num_records = count;
3366 	cdata->rec = fmt_buffer[pos].record;
3367 	cdata->blksize = fmt_buffer[pos].dl;
3368 	cdata->key_length = fmt_buffer[pos].kl;
3369 }
3370 
3371 /*
3372  * Check the format of a range of tracks of a DASD.
3373  */
3374 static int dasd_eckd_check_device_format(struct dasd_device *base,
3375 					 struct format_check_t *cdata,
3376 					 int enable_pav)
3377 {
3378 	struct dasd_eckd_private *private = base->private;
3379 	struct eckd_count *fmt_buffer;
3380 	struct irb irb;
3381 	int rpt_max, rpt_exp;
3382 	int fmt_buffer_size;
3383 	int trk_per_cyl;
3384 	int trkcount;
3385 	int tpm = 0;
3386 	int rc;
3387 
3388 	trk_per_cyl = private->rdc_data.trk_per_cyl;
3389 
3390 	/* Get maximum and expected amount of records per track */
3391 	rpt_max = recs_per_track(&private->rdc_data, 0, 512) + 1;
3392 	rpt_exp = recs_per_track(&private->rdc_data, 0, cdata->expect.blksize);
3393 
3394 	trkcount = cdata->expect.stop_unit - cdata->expect.start_unit + 1;
3395 	fmt_buffer_size = trkcount * rpt_max * sizeof(struct eckd_count);
3396 
3397 	fmt_buffer = kzalloc(fmt_buffer_size, GFP_KERNEL | GFP_DMA);
3398 	if (!fmt_buffer)
3399 		return -ENOMEM;
3400 
3401 	/*
3402 	 * A certain FICON feature subset is needed to operate in transport
3403 	 * mode. Additionally, the support for transport mode is implicitly
3404 	 * checked by comparing the buffer size with fcx_max_data. As long as
3405 	 * the buffer size is smaller we can operate in transport mode and
3406 	 * process multiple tracks. If not, only one track at once is being
3407 	 * processed using command mode.
3408 	 */
3409 	if ((private->features.feature[40] & 0x04) &&
3410 	    fmt_buffer_size <= private->fcx_max_data)
3411 		tpm = 1;
3412 
3413 	rc = dasd_eckd_format_process_data(base, &cdata->expect, enable_pav,
3414 					   tpm, fmt_buffer, rpt_max, &irb);
3415 	if (rc && rc != -EIO)
3416 		goto out;
3417 	if (rc == -EIO) {
3418 		/*
3419 		 * If our first attempt with transport mode enabled comes back
3420 		 * with an incorrect length error, we're going to retry the
3421 		 * check with command mode.
3422 		 */
3423 		if (tpm && scsw_cstat(&irb.scsw) == 0x40) {
3424 			tpm = 0;
3425 			rc = dasd_eckd_format_process_data(base, &cdata->expect,
3426 							   enable_pav, tpm,
3427 							   fmt_buffer, rpt_max,
3428 							   &irb);
3429 			if (rc)
3430 				goto out;
3431 		} else {
3432 			goto out;
3433 		}
3434 	}
3435 
3436 	dasd_eckd_format_evaluate_tracks(fmt_buffer, cdata, rpt_max, rpt_exp,
3437 					 trk_per_cyl, tpm);
3438 
3439 out:
3440 	kfree(fmt_buffer);
3441 
3442 	return rc;
3443 }
3444 
3445 static void dasd_eckd_handle_terminated_request(struct dasd_ccw_req *cqr)
3446 {
3447 	if (cqr->retries < 0) {
3448 		cqr->status = DASD_CQR_FAILED;
3449 		return;
3450 	}
3451 	cqr->status = DASD_CQR_FILLED;
3452 	if (cqr->block && (cqr->startdev != cqr->block->base)) {
3453 		dasd_eckd_reset_ccw_to_base_io(cqr);
3454 		cqr->startdev = cqr->block->base;
3455 		cqr->lpm = dasd_path_get_opm(cqr->block->base);
3456 	}
3457 };
3458 
3459 static dasd_erp_fn_t
3460 dasd_eckd_erp_action(struct dasd_ccw_req * cqr)
3461 {
3462 	struct dasd_device *device = (struct dasd_device *) cqr->startdev;
3463 	struct ccw_device *cdev = device->cdev;
3464 
3465 	switch (cdev->id.cu_type) {
3466 	case 0x3990:
3467 	case 0x2105:
3468 	case 0x2107:
3469 	case 0x1750:
3470 		return dasd_3990_erp_action;
3471 	case 0x9343:
3472 	case 0x3880:
3473 	default:
3474 		return dasd_default_erp_action;
3475 	}
3476 }
3477 
3478 static dasd_erp_fn_t
3479 dasd_eckd_erp_postaction(struct dasd_ccw_req * cqr)
3480 {
3481 	return dasd_default_erp_postaction;
3482 }
3483 
3484 static void dasd_eckd_check_for_device_change(struct dasd_device *device,
3485 					      struct dasd_ccw_req *cqr,
3486 					      struct irb *irb)
3487 {
3488 	char mask;
3489 	char *sense = NULL;
3490 	struct dasd_eckd_private *private = device->private;
3491 
3492 	/* first of all check for state change pending interrupt */
3493 	mask = DEV_STAT_ATTENTION | DEV_STAT_DEV_END | DEV_STAT_UNIT_EXCEP;
3494 	if ((scsw_dstat(&irb->scsw) & mask) == mask) {
3495 		/*
3496 		 * for alias only, not in offline processing
3497 		 * and only if not suspended
3498 		 */
3499 		if (!device->block && private->lcu &&
3500 		    device->state == DASD_STATE_ONLINE &&
3501 		    !test_bit(DASD_FLAG_OFFLINE, &device->flags) &&
3502 		    !test_bit(DASD_FLAG_SUSPENDED, &device->flags)) {
3503 			/* schedule worker to reload device */
3504 			dasd_reload_device(device);
3505 		}
3506 		dasd_generic_handle_state_change(device);
3507 		return;
3508 	}
3509 
3510 	sense = dasd_get_sense(irb);
3511 	if (!sense)
3512 		return;
3513 
3514 	/* summary unit check */
3515 	if ((sense[27] & DASD_SENSE_BIT_0) && (sense[7] == 0x0D) &&
3516 	    (scsw_dstat(&irb->scsw) & DEV_STAT_UNIT_CHECK)) {
3517 		if (test_and_set_bit(DASD_FLAG_SUC, &device->flags)) {
3518 			DBF_DEV_EVENT(DBF_WARNING, device, "%s",
3519 				      "eckd suc: device already notified");
3520 			return;
3521 		}
3522 		sense = dasd_get_sense(irb);
3523 		if (!sense) {
3524 			DBF_DEV_EVENT(DBF_WARNING, device, "%s",
3525 				      "eckd suc: no reason code available");
3526 			clear_bit(DASD_FLAG_SUC, &device->flags);
3527 			return;
3528 
3529 		}
3530 		private->suc_reason = sense[8];
3531 		DBF_DEV_EVENT(DBF_NOTICE, device, "%s %x",
3532 			      "eckd handle summary unit check: reason",
3533 			      private->suc_reason);
3534 		dasd_get_device(device);
3535 		if (!schedule_work(&device->suc_work))
3536 			dasd_put_device(device);
3537 
3538 		return;
3539 	}
3540 
3541 	/* service information message SIM */
3542 	if (!cqr && !(sense[27] & DASD_SENSE_BIT_0) &&
3543 	    ((sense[6] & DASD_SIM_SENSE) == DASD_SIM_SENSE)) {
3544 		dasd_3990_erp_handle_sim(device, sense);
3545 		return;
3546 	}
3547 
3548 	/* loss of device reservation is handled via base devices only
3549 	 * as alias devices may be used with several bases
3550 	 */
3551 	if (device->block && (sense[27] & DASD_SENSE_BIT_0) &&
3552 	    (sense[7] == 0x3F) &&
3553 	    (scsw_dstat(&irb->scsw) & DEV_STAT_UNIT_CHECK) &&
3554 	    test_bit(DASD_FLAG_IS_RESERVED, &device->flags)) {
3555 		if (device->features & DASD_FEATURE_FAILONSLCK)
3556 			set_bit(DASD_FLAG_LOCK_STOLEN, &device->flags);
3557 		clear_bit(DASD_FLAG_IS_RESERVED, &device->flags);
3558 		dev_err(&device->cdev->dev,
3559 			"The device reservation was lost\n");
3560 	}
3561 }
3562 
3563 static int dasd_eckd_ras_sanity_checks(struct dasd_device *device,
3564 				       unsigned int first_trk,
3565 				       unsigned int last_trk)
3566 {
3567 	struct dasd_eckd_private *private = device->private;
3568 	unsigned int trks_per_vol;
3569 	int rc = 0;
3570 
3571 	trks_per_vol = private->real_cyl * private->rdc_data.trk_per_cyl;
3572 
3573 	if (first_trk >= trks_per_vol) {
3574 		dev_warn(&device->cdev->dev,
3575 			 "Start track number %u used in the space release command is too big\n",
3576 			 first_trk);
3577 		rc = -EINVAL;
3578 	} else if (last_trk >= trks_per_vol) {
3579 		dev_warn(&device->cdev->dev,
3580 			 "Stop track number %u used in the space release command is too big\n",
3581 			 last_trk);
3582 		rc = -EINVAL;
3583 	} else if (first_trk > last_trk) {
3584 		dev_warn(&device->cdev->dev,
3585 			 "Start track %u used in the space release command exceeds the end track\n",
3586 			 first_trk);
3587 		rc = -EINVAL;
3588 	}
3589 	return rc;
3590 }
3591 
3592 /*
3593  * Helper function to count the amount of involved extents within a given range
3594  * with extent alignment in mind.
3595  */
3596 static int count_exts(unsigned int from, unsigned int to, int trks_per_ext)
3597 {
3598 	int cur_pos = 0;
3599 	int count = 0;
3600 	int tmp;
3601 
3602 	if (from == to)
3603 		return 1;
3604 
3605 	/* Count first partial extent */
3606 	if (from % trks_per_ext != 0) {
3607 		tmp = from + trks_per_ext - (from % trks_per_ext) - 1;
3608 		if (tmp > to)
3609 			tmp = to;
3610 		cur_pos = tmp - from + 1;
3611 		count++;
3612 	}
3613 	/* Count full extents */
3614 	if (to - (from + cur_pos) + 1 >= trks_per_ext) {
3615 		tmp = to - ((to - trks_per_ext + 1) % trks_per_ext);
3616 		count += (tmp - (from + cur_pos) + 1) / trks_per_ext;
3617 		cur_pos = tmp;
3618 	}
3619 	/* Count last partial extent */
3620 	if (cur_pos < to)
3621 		count++;
3622 
3623 	return count;
3624 }
3625 
3626 /*
3627  * Release allocated space for a given range or an entire volume.
3628  */
3629 static struct dasd_ccw_req *
3630 dasd_eckd_dso_ras(struct dasd_device *device, struct dasd_block *block,
3631 		  struct request *req, unsigned int first_trk,
3632 		  unsigned int last_trk, int by_extent)
3633 {
3634 	struct dasd_eckd_private *private = device->private;
3635 	struct dasd_dso_ras_ext_range *ras_range;
3636 	struct dasd_rssd_features *features;
3637 	struct dasd_dso_ras_data *ras_data;
3638 	u16 heads, beg_head, end_head;
3639 	int cur_to_trk, cur_from_trk;
3640 	struct dasd_ccw_req *cqr;
3641 	u32 beg_cyl, end_cyl;
3642 	struct ccw1 *ccw;
3643 	int trks_per_ext;
3644 	size_t ras_size;
3645 	size_t size;
3646 	int nr_exts;
3647 	void *rq;
3648 	int i;
3649 
3650 	if (dasd_eckd_ras_sanity_checks(device, first_trk, last_trk))
3651 		return ERR_PTR(-EINVAL);
3652 
3653 	rq = req ? blk_mq_rq_to_pdu(req) : NULL;
3654 
3655 	features = &private->features;
3656 
3657 	trks_per_ext = dasd_eckd_ext_size(device) * private->rdc_data.trk_per_cyl;
3658 	nr_exts = 0;
3659 	if (by_extent)
3660 		nr_exts = count_exts(first_trk, last_trk, trks_per_ext);
3661 	ras_size = sizeof(*ras_data);
3662 	size = ras_size + (nr_exts * sizeof(*ras_range));
3663 
3664 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1, size, device, rq);
3665 	if (IS_ERR(cqr)) {
3666 		DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
3667 				"Could not allocate RAS request");
3668 		return cqr;
3669 	}
3670 
3671 	ras_data = cqr->data;
3672 	memset(ras_data, 0, size);
3673 
3674 	ras_data->order = DSO_ORDER_RAS;
3675 	ras_data->flags.vol_type = 0; /* CKD volume */
3676 	/* Release specified extents or entire volume */
3677 	ras_data->op_flags.by_extent = by_extent;
3678 	/*
3679 	 * This bit guarantees initialisation of tracks within an extent that is
3680 	 * not fully specified, but is only supported with a certain feature
3681 	 * subset.
3682 	 */
3683 	ras_data->op_flags.guarantee_init = !!(features->feature[56] & 0x01);
3684 	ras_data->lss = private->ned->ID;
3685 	ras_data->dev_addr = private->ned->unit_addr;
3686 	ras_data->nr_exts = nr_exts;
3687 
3688 	if (by_extent) {
3689 		heads = private->rdc_data.trk_per_cyl;
3690 		cur_from_trk = first_trk;
3691 		cur_to_trk = first_trk + trks_per_ext -
3692 			(first_trk % trks_per_ext) - 1;
3693 		if (cur_to_trk > last_trk)
3694 			cur_to_trk = last_trk;
3695 		ras_range = (struct dasd_dso_ras_ext_range *)(cqr->data + ras_size);
3696 
3697 		for (i = 0; i < nr_exts; i++) {
3698 			beg_cyl = cur_from_trk / heads;
3699 			beg_head = cur_from_trk % heads;
3700 			end_cyl = cur_to_trk / heads;
3701 			end_head = cur_to_trk % heads;
3702 
3703 			set_ch_t(&ras_range->beg_ext, beg_cyl, beg_head);
3704 			set_ch_t(&ras_range->end_ext, end_cyl, end_head);
3705 
3706 			cur_from_trk = cur_to_trk + 1;
3707 			cur_to_trk = cur_from_trk + trks_per_ext - 1;
3708 			if (cur_to_trk > last_trk)
3709 				cur_to_trk = last_trk;
3710 			ras_range++;
3711 		}
3712 	}
3713 
3714 	ccw = cqr->cpaddr;
3715 	ccw->cda = (__u32)(addr_t)cqr->data;
3716 	ccw->cmd_code = DASD_ECKD_CCW_DSO;
3717 	ccw->count = size;
3718 
3719 	cqr->startdev = device;
3720 	cqr->memdev = device;
3721 	cqr->block = block;
3722 	cqr->retries = 256;
3723 	cqr->expires = device->default_expires * HZ;
3724 	cqr->buildclk = get_tod_clock();
3725 	cqr->status = DASD_CQR_FILLED;
3726 
3727 	return cqr;
3728 }
3729 
3730 static int dasd_eckd_release_space_full(struct dasd_device *device)
3731 {
3732 	struct dasd_ccw_req *cqr;
3733 	int rc;
3734 
3735 	cqr = dasd_eckd_dso_ras(device, NULL, NULL, 0, 0, 0);
3736 	if (IS_ERR(cqr))
3737 		return PTR_ERR(cqr);
3738 
3739 	rc = dasd_sleep_on_interruptible(cqr);
3740 
3741 	dasd_sfree_request(cqr, cqr->memdev);
3742 
3743 	return rc;
3744 }
3745 
3746 static int dasd_eckd_release_space_trks(struct dasd_device *device,
3747 					unsigned int from, unsigned int to)
3748 {
3749 	struct dasd_eckd_private *private = device->private;
3750 	struct dasd_block *block = device->block;
3751 	struct dasd_ccw_req *cqr, *n;
3752 	struct list_head ras_queue;
3753 	unsigned int device_exts;
3754 	int trks_per_ext;
3755 	int stop, step;
3756 	int cur_pos;
3757 	int rc = 0;
3758 	int retry;
3759 
3760 	INIT_LIST_HEAD(&ras_queue);
3761 
3762 	device_exts = private->real_cyl / dasd_eckd_ext_size(device);
3763 	trks_per_ext = dasd_eckd_ext_size(device) * private->rdc_data.trk_per_cyl;
3764 
3765 	/* Make sure device limits are not exceeded */
3766 	step = trks_per_ext * min(device_exts, DASD_ECKD_RAS_EXTS_MAX);
3767 	cur_pos = from;
3768 
3769 	do {
3770 		retry = 0;
3771 		while (cur_pos < to) {
3772 			stop = cur_pos + step -
3773 				((cur_pos + step) % trks_per_ext) - 1;
3774 			if (stop > to)
3775 				stop = to;
3776 
3777 			cqr = dasd_eckd_dso_ras(device, NULL, NULL, cur_pos, stop, 1);
3778 			if (IS_ERR(cqr)) {
3779 				rc = PTR_ERR(cqr);
3780 				if (rc == -ENOMEM) {
3781 					if (list_empty(&ras_queue))
3782 						goto out;
3783 					retry = 1;
3784 					break;
3785 				}
3786 				goto err_out;
3787 			}
3788 
3789 			spin_lock_irq(&block->queue_lock);
3790 			list_add_tail(&cqr->blocklist, &ras_queue);
3791 			spin_unlock_irq(&block->queue_lock);
3792 			cur_pos = stop + 1;
3793 		}
3794 
3795 		rc = dasd_sleep_on_queue_interruptible(&ras_queue);
3796 
3797 err_out:
3798 		list_for_each_entry_safe(cqr, n, &ras_queue, blocklist) {
3799 			device = cqr->startdev;
3800 			private = device->private;
3801 
3802 			spin_lock_irq(&block->queue_lock);
3803 			list_del_init(&cqr->blocklist);
3804 			spin_unlock_irq(&block->queue_lock);
3805 			dasd_sfree_request(cqr, device);
3806 			private->count--;
3807 		}
3808 	} while (retry);
3809 
3810 out:
3811 	return rc;
3812 }
3813 
3814 static int dasd_eckd_release_space(struct dasd_device *device,
3815 				   struct format_data_t *rdata)
3816 {
3817 	if (rdata->intensity & DASD_FMT_INT_ESE_FULL)
3818 		return dasd_eckd_release_space_full(device);
3819 	else if (rdata->intensity == 0)
3820 		return dasd_eckd_release_space_trks(device, rdata->start_unit,
3821 						    rdata->stop_unit);
3822 	else
3823 		return -EINVAL;
3824 }
3825 
3826 static struct dasd_ccw_req *dasd_eckd_build_cp_cmd_single(
3827 					       struct dasd_device *startdev,
3828 					       struct dasd_block *block,
3829 					       struct request *req,
3830 					       sector_t first_rec,
3831 					       sector_t last_rec,
3832 					       sector_t first_trk,
3833 					       sector_t last_trk,
3834 					       unsigned int first_offs,
3835 					       unsigned int last_offs,
3836 					       unsigned int blk_per_trk,
3837 					       unsigned int blksize)
3838 {
3839 	struct dasd_eckd_private *private;
3840 	unsigned long *idaws;
3841 	struct LO_eckd_data *LO_data;
3842 	struct dasd_ccw_req *cqr;
3843 	struct ccw1 *ccw;
3844 	struct req_iterator iter;
3845 	struct bio_vec bv;
3846 	char *dst;
3847 	unsigned int off;
3848 	int count, cidaw, cplength, datasize;
3849 	sector_t recid;
3850 	unsigned char cmd, rcmd;
3851 	int use_prefix;
3852 	struct dasd_device *basedev;
3853 
3854 	basedev = block->base;
3855 	private = basedev->private;
3856 	if (rq_data_dir(req) == READ)
3857 		cmd = DASD_ECKD_CCW_READ_MT;
3858 	else if (rq_data_dir(req) == WRITE)
3859 		cmd = DASD_ECKD_CCW_WRITE_MT;
3860 	else
3861 		return ERR_PTR(-EINVAL);
3862 
3863 	/* Check struct bio and count the number of blocks for the request. */
3864 	count = 0;
3865 	cidaw = 0;
3866 	rq_for_each_segment(bv, req, iter) {
3867 		if (bv.bv_len & (blksize - 1))
3868 			/* Eckd can only do full blocks. */
3869 			return ERR_PTR(-EINVAL);
3870 		count += bv.bv_len >> (block->s2b_shift + 9);
3871 		if (idal_is_needed (page_address(bv.bv_page), bv.bv_len))
3872 			cidaw += bv.bv_len >> (block->s2b_shift + 9);
3873 	}
3874 	/* Paranoia. */
3875 	if (count != last_rec - first_rec + 1)
3876 		return ERR_PTR(-EINVAL);
3877 
3878 	/* use the prefix command if available */
3879 	use_prefix = private->features.feature[8] & 0x01;
3880 	if (use_prefix) {
3881 		/* 1x prefix + number of blocks */
3882 		cplength = 2 + count;
3883 		/* 1x prefix + cidaws*sizeof(long) */
3884 		datasize = sizeof(struct PFX_eckd_data) +
3885 			sizeof(struct LO_eckd_data) +
3886 			cidaw * sizeof(unsigned long);
3887 	} else {
3888 		/* 1x define extent + 1x locate record + number of blocks */
3889 		cplength = 2 + count;
3890 		/* 1x define extent + 1x locate record + cidaws*sizeof(long) */
3891 		datasize = sizeof(struct DE_eckd_data) +
3892 			sizeof(struct LO_eckd_data) +
3893 			cidaw * sizeof(unsigned long);
3894 	}
3895 	/* Find out the number of additional locate record ccws for cdl. */
3896 	if (private->uses_cdl && first_rec < 2*blk_per_trk) {
3897 		if (last_rec >= 2*blk_per_trk)
3898 			count = 2*blk_per_trk - first_rec;
3899 		cplength += count;
3900 		datasize += count*sizeof(struct LO_eckd_data);
3901 	}
3902 	/* Allocate the ccw request. */
3903 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength, datasize,
3904 				   startdev, blk_mq_rq_to_pdu(req));
3905 	if (IS_ERR(cqr))
3906 		return cqr;
3907 	ccw = cqr->cpaddr;
3908 	/* First ccw is define extent or prefix. */
3909 	if (use_prefix) {
3910 		if (prefix(ccw++, cqr->data, first_trk,
3911 			   last_trk, cmd, basedev, startdev) == -EAGAIN) {
3912 			/* Clock not in sync and XRC is enabled.
3913 			 * Try again later.
3914 			 */
3915 			dasd_sfree_request(cqr, startdev);
3916 			return ERR_PTR(-EAGAIN);
3917 		}
3918 		idaws = (unsigned long *) (cqr->data +
3919 					   sizeof(struct PFX_eckd_data));
3920 	} else {
3921 		if (define_extent(ccw++, cqr->data, first_trk,
3922 				  last_trk, cmd, basedev, 0) == -EAGAIN) {
3923 			/* Clock not in sync and XRC is enabled.
3924 			 * Try again later.
3925 			 */
3926 			dasd_sfree_request(cqr, startdev);
3927 			return ERR_PTR(-EAGAIN);
3928 		}
3929 		idaws = (unsigned long *) (cqr->data +
3930 					   sizeof(struct DE_eckd_data));
3931 	}
3932 	/* Build locate_record+read/write/ccws. */
3933 	LO_data = (struct LO_eckd_data *) (idaws + cidaw);
3934 	recid = first_rec;
3935 	if (private->uses_cdl == 0 || recid > 2*blk_per_trk) {
3936 		/* Only standard blocks so there is just one locate record. */
3937 		ccw[-1].flags |= CCW_FLAG_CC;
3938 		locate_record(ccw++, LO_data++, first_trk, first_offs + 1,
3939 			      last_rec - recid + 1, cmd, basedev, blksize);
3940 	}
3941 	rq_for_each_segment(bv, req, iter) {
3942 		dst = page_address(bv.bv_page) + bv.bv_offset;
3943 		if (dasd_page_cache) {
3944 			char *copy = kmem_cache_alloc(dasd_page_cache,
3945 						      GFP_DMA | __GFP_NOWARN);
3946 			if (copy && rq_data_dir(req) == WRITE)
3947 				memcpy(copy + bv.bv_offset, dst, bv.bv_len);
3948 			if (copy)
3949 				dst = copy + bv.bv_offset;
3950 		}
3951 		for (off = 0; off < bv.bv_len; off += blksize) {
3952 			sector_t trkid = recid;
3953 			unsigned int recoffs = sector_div(trkid, blk_per_trk);
3954 			rcmd = cmd;
3955 			count = blksize;
3956 			/* Locate record for cdl special block ? */
3957 			if (private->uses_cdl && recid < 2*blk_per_trk) {
3958 				if (dasd_eckd_cdl_special(blk_per_trk, recid)){
3959 					rcmd |= 0x8;
3960 					count = dasd_eckd_cdl_reclen(recid);
3961 					if (count < blksize &&
3962 					    rq_data_dir(req) == READ)
3963 						memset(dst + count, 0xe5,
3964 						       blksize - count);
3965 				}
3966 				ccw[-1].flags |= CCW_FLAG_CC;
3967 				locate_record(ccw++, LO_data++,
3968 					      trkid, recoffs + 1,
3969 					      1, rcmd, basedev, count);
3970 			}
3971 			/* Locate record for standard blocks ? */
3972 			if (private->uses_cdl && recid == 2*blk_per_trk) {
3973 				ccw[-1].flags |= CCW_FLAG_CC;
3974 				locate_record(ccw++, LO_data++,
3975 					      trkid, recoffs + 1,
3976 					      last_rec - recid + 1,
3977 					      cmd, basedev, count);
3978 			}
3979 			/* Read/write ccw. */
3980 			ccw[-1].flags |= CCW_FLAG_CC;
3981 			ccw->cmd_code = rcmd;
3982 			ccw->count = count;
3983 			if (idal_is_needed(dst, blksize)) {
3984 				ccw->cda = (__u32)(addr_t) idaws;
3985 				ccw->flags = CCW_FLAG_IDA;
3986 				idaws = idal_create_words(idaws, dst, blksize);
3987 			} else {
3988 				ccw->cda = (__u32)(addr_t) dst;
3989 				ccw->flags = 0;
3990 			}
3991 			ccw++;
3992 			dst += blksize;
3993 			recid++;
3994 		}
3995 	}
3996 	if (blk_noretry_request(req) ||
3997 	    block->base->features & DASD_FEATURE_FAILFAST)
3998 		set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
3999 	cqr->startdev = startdev;
4000 	cqr->memdev = startdev;
4001 	cqr->block = block;
4002 	cqr->expires = startdev->default_expires * HZ;	/* default 5 minutes */
4003 	cqr->lpm = dasd_path_get_ppm(startdev);
4004 	cqr->retries = startdev->default_retries;
4005 	cqr->buildclk = get_tod_clock();
4006 	cqr->status = DASD_CQR_FILLED;
4007 
4008 	/* Set flags to suppress output for expected errors */
4009 	if (dasd_eckd_is_ese(basedev)) {
4010 		set_bit(DASD_CQR_SUPPRESS_FP, &cqr->flags);
4011 		set_bit(DASD_CQR_SUPPRESS_IL, &cqr->flags);
4012 		set_bit(DASD_CQR_SUPPRESS_NRF, &cqr->flags);
4013 	}
4014 
4015 	return cqr;
4016 }
4017 
4018 static struct dasd_ccw_req *dasd_eckd_build_cp_cmd_track(
4019 					       struct dasd_device *startdev,
4020 					       struct dasd_block *block,
4021 					       struct request *req,
4022 					       sector_t first_rec,
4023 					       sector_t last_rec,
4024 					       sector_t first_trk,
4025 					       sector_t last_trk,
4026 					       unsigned int first_offs,
4027 					       unsigned int last_offs,
4028 					       unsigned int blk_per_trk,
4029 					       unsigned int blksize)
4030 {
4031 	unsigned long *idaws;
4032 	struct dasd_ccw_req *cqr;
4033 	struct ccw1 *ccw;
4034 	struct req_iterator iter;
4035 	struct bio_vec bv;
4036 	char *dst, *idaw_dst;
4037 	unsigned int cidaw, cplength, datasize;
4038 	unsigned int tlf;
4039 	sector_t recid;
4040 	unsigned char cmd;
4041 	struct dasd_device *basedev;
4042 	unsigned int trkcount, count, count_to_trk_end;
4043 	unsigned int idaw_len, seg_len, part_len, len_to_track_end;
4044 	unsigned char new_track, end_idaw;
4045 	sector_t trkid;
4046 	unsigned int recoffs;
4047 
4048 	basedev = block->base;
4049 	if (rq_data_dir(req) == READ)
4050 		cmd = DASD_ECKD_CCW_READ_TRACK_DATA;
4051 	else if (rq_data_dir(req) == WRITE)
4052 		cmd = DASD_ECKD_CCW_WRITE_TRACK_DATA;
4053 	else
4054 		return ERR_PTR(-EINVAL);
4055 
4056 	/* Track based I/O needs IDAWs for each page, and not just for
4057 	 * 64 bit addresses. We need additional idals for pages
4058 	 * that get filled from two tracks, so we use the number
4059 	 * of records as upper limit.
4060 	 */
4061 	cidaw = last_rec - first_rec + 1;
4062 	trkcount = last_trk - first_trk + 1;
4063 
4064 	/* 1x prefix + one read/write ccw per track */
4065 	cplength = 1 + trkcount;
4066 
4067 	datasize = sizeof(struct PFX_eckd_data) + cidaw * sizeof(unsigned long);
4068 
4069 	/* Allocate the ccw request. */
4070 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength, datasize,
4071 				   startdev, blk_mq_rq_to_pdu(req));
4072 	if (IS_ERR(cqr))
4073 		return cqr;
4074 	ccw = cqr->cpaddr;
4075 	/* transfer length factor: how many bytes to read from the last track */
4076 	if (first_trk == last_trk)
4077 		tlf = last_offs - first_offs + 1;
4078 	else
4079 		tlf = last_offs + 1;
4080 	tlf *= blksize;
4081 
4082 	if (prefix_LRE(ccw++, cqr->data, first_trk,
4083 		       last_trk, cmd, basedev, startdev,
4084 		       1 /* format */, first_offs + 1,
4085 		       trkcount, blksize,
4086 		       tlf) == -EAGAIN) {
4087 		/* Clock not in sync and XRC is enabled.
4088 		 * Try again later.
4089 		 */
4090 		dasd_sfree_request(cqr, startdev);
4091 		return ERR_PTR(-EAGAIN);
4092 	}
4093 
4094 	/*
4095 	 * The translation of request into ccw programs must meet the
4096 	 * following conditions:
4097 	 * - all idaws but the first and the last must address full pages
4098 	 *   (or 2K blocks on 31-bit)
4099 	 * - the scope of a ccw and it's idal ends with the track boundaries
4100 	 */
4101 	idaws = (unsigned long *) (cqr->data + sizeof(struct PFX_eckd_data));
4102 	recid = first_rec;
4103 	new_track = 1;
4104 	end_idaw = 0;
4105 	len_to_track_end = 0;
4106 	idaw_dst = NULL;
4107 	idaw_len = 0;
4108 	rq_for_each_segment(bv, req, iter) {
4109 		dst = page_address(bv.bv_page) + bv.bv_offset;
4110 		seg_len = bv.bv_len;
4111 		while (seg_len) {
4112 			if (new_track) {
4113 				trkid = recid;
4114 				recoffs = sector_div(trkid, blk_per_trk);
4115 				count_to_trk_end = blk_per_trk - recoffs;
4116 				count = min((last_rec - recid + 1),
4117 					    (sector_t)count_to_trk_end);
4118 				len_to_track_end = count * blksize;
4119 				ccw[-1].flags |= CCW_FLAG_CC;
4120 				ccw->cmd_code = cmd;
4121 				ccw->count = len_to_track_end;
4122 				ccw->cda = (__u32)(addr_t)idaws;
4123 				ccw->flags = CCW_FLAG_IDA;
4124 				ccw++;
4125 				recid += count;
4126 				new_track = 0;
4127 				/* first idaw for a ccw may start anywhere */
4128 				if (!idaw_dst)
4129 					idaw_dst = dst;
4130 			}
4131 			/* If we start a new idaw, we must make sure that it
4132 			 * starts on an IDA_BLOCK_SIZE boundary.
4133 			 * If we continue an idaw, we must make sure that the
4134 			 * current segment begins where the so far accumulated
4135 			 * idaw ends
4136 			 */
4137 			if (!idaw_dst) {
4138 				if (__pa(dst) & (IDA_BLOCK_SIZE-1)) {
4139 					dasd_sfree_request(cqr, startdev);
4140 					return ERR_PTR(-ERANGE);
4141 				} else
4142 					idaw_dst = dst;
4143 			}
4144 			if ((idaw_dst + idaw_len) != dst) {
4145 				dasd_sfree_request(cqr, startdev);
4146 				return ERR_PTR(-ERANGE);
4147 			}
4148 			part_len = min(seg_len, len_to_track_end);
4149 			seg_len -= part_len;
4150 			dst += part_len;
4151 			idaw_len += part_len;
4152 			len_to_track_end -= part_len;
4153 			/* collected memory area ends on an IDA_BLOCK border,
4154 			 * -> create an idaw
4155 			 * idal_create_words will handle cases where idaw_len
4156 			 * is larger then IDA_BLOCK_SIZE
4157 			 */
4158 			if (!(__pa(idaw_dst + idaw_len) & (IDA_BLOCK_SIZE-1)))
4159 				end_idaw = 1;
4160 			/* We also need to end the idaw at track end */
4161 			if (!len_to_track_end) {
4162 				new_track = 1;
4163 				end_idaw = 1;
4164 			}
4165 			if (end_idaw) {
4166 				idaws = idal_create_words(idaws, idaw_dst,
4167 							  idaw_len);
4168 				idaw_dst = NULL;
4169 				idaw_len = 0;
4170 				end_idaw = 0;
4171 			}
4172 		}
4173 	}
4174 
4175 	if (blk_noretry_request(req) ||
4176 	    block->base->features & DASD_FEATURE_FAILFAST)
4177 		set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
4178 	cqr->startdev = startdev;
4179 	cqr->memdev = startdev;
4180 	cqr->block = block;
4181 	cqr->expires = startdev->default_expires * HZ;	/* default 5 minutes */
4182 	cqr->lpm = dasd_path_get_ppm(startdev);
4183 	cqr->retries = startdev->default_retries;
4184 	cqr->buildclk = get_tod_clock();
4185 	cqr->status = DASD_CQR_FILLED;
4186 
4187 	/* Set flags to suppress output for expected errors */
4188 	if (dasd_eckd_is_ese(basedev))
4189 		set_bit(DASD_CQR_SUPPRESS_NRF, &cqr->flags);
4190 
4191 	return cqr;
4192 }
4193 
4194 static int prepare_itcw(struct itcw *itcw,
4195 			unsigned int trk, unsigned int totrk, int cmd,
4196 			struct dasd_device *basedev,
4197 			struct dasd_device *startdev,
4198 			unsigned int rec_on_trk, int count,
4199 			unsigned int blksize,
4200 			unsigned int total_data_size,
4201 			unsigned int tlf,
4202 			unsigned int blk_per_trk)
4203 {
4204 	struct PFX_eckd_data pfxdata;
4205 	struct dasd_eckd_private *basepriv, *startpriv;
4206 	struct DE_eckd_data *dedata;
4207 	struct LRE_eckd_data *lredata;
4208 	struct dcw *dcw;
4209 
4210 	u32 begcyl, endcyl;
4211 	u16 heads, beghead, endhead;
4212 	u8 pfx_cmd;
4213 
4214 	int rc = 0;
4215 	int sector = 0;
4216 	int dn, d;
4217 
4218 
4219 	/* setup prefix data */
4220 	basepriv = basedev->private;
4221 	startpriv = startdev->private;
4222 	dedata = &pfxdata.define_extent;
4223 	lredata = &pfxdata.locate_record;
4224 
4225 	memset(&pfxdata, 0, sizeof(pfxdata));
4226 	pfxdata.format = 1; /* PFX with LRE */
4227 	pfxdata.base_address = basepriv->ned->unit_addr;
4228 	pfxdata.base_lss = basepriv->ned->ID;
4229 	pfxdata.validity.define_extent = 1;
4230 
4231 	/* private uid is kept up to date, conf_data may be outdated */
4232 	if (startpriv->uid.type == UA_BASE_PAV_ALIAS)
4233 		pfxdata.validity.verify_base = 1;
4234 
4235 	if (startpriv->uid.type == UA_HYPER_PAV_ALIAS) {
4236 		pfxdata.validity.verify_base = 1;
4237 		pfxdata.validity.hyper_pav = 1;
4238 	}
4239 
4240 	switch (cmd) {
4241 	case DASD_ECKD_CCW_READ_TRACK_DATA:
4242 		dedata->mask.perm = 0x1;
4243 		dedata->attributes.operation = basepriv->attrib.operation;
4244 		dedata->blk_size = blksize;
4245 		dedata->ga_extended |= 0x42;
4246 		lredata->operation.orientation = 0x0;
4247 		lredata->operation.operation = 0x0C;
4248 		lredata->auxiliary.check_bytes = 0x01;
4249 		pfx_cmd = DASD_ECKD_CCW_PFX_READ;
4250 		break;
4251 	case DASD_ECKD_CCW_WRITE_TRACK_DATA:
4252 		dedata->mask.perm = 0x02;
4253 		dedata->attributes.operation = basepriv->attrib.operation;
4254 		dedata->blk_size = blksize;
4255 		rc = set_timestamp(NULL, dedata, basedev);
4256 		dedata->ga_extended |= 0x42;
4257 		lredata->operation.orientation = 0x0;
4258 		lredata->operation.operation = 0x3F;
4259 		lredata->extended_operation = 0x23;
4260 		lredata->auxiliary.check_bytes = 0x2;
4261 		/*
4262 		 * If XRC is supported the System Time Stamp is set. The
4263 		 * validity of the time stamp must be reflected in the prefix
4264 		 * data as well.
4265 		 */
4266 		if (dedata->ga_extended & 0x08 && dedata->ga_extended & 0x02)
4267 			pfxdata.validity.time_stamp = 1; /* 'Time Stamp Valid' */
4268 		pfx_cmd = DASD_ECKD_CCW_PFX;
4269 		break;
4270 	case DASD_ECKD_CCW_READ_COUNT_MT:
4271 		dedata->mask.perm = 0x1;
4272 		dedata->attributes.operation = DASD_BYPASS_CACHE;
4273 		dedata->ga_extended |= 0x42;
4274 		dedata->blk_size = blksize;
4275 		lredata->operation.orientation = 0x2;
4276 		lredata->operation.operation = 0x16;
4277 		lredata->auxiliary.check_bytes = 0x01;
4278 		pfx_cmd = DASD_ECKD_CCW_PFX_READ;
4279 		break;
4280 	default:
4281 		DBF_DEV_EVENT(DBF_ERR, basedev,
4282 			      "prepare itcw, unknown opcode 0x%x", cmd);
4283 		BUG();
4284 		break;
4285 	}
4286 	if (rc)
4287 		return rc;
4288 
4289 	dedata->attributes.mode = 0x3;	/* ECKD */
4290 
4291 	heads = basepriv->rdc_data.trk_per_cyl;
4292 	begcyl = trk / heads;
4293 	beghead = trk % heads;
4294 	endcyl = totrk / heads;
4295 	endhead = totrk % heads;
4296 
4297 	/* check for sequential prestage - enhance cylinder range */
4298 	if (dedata->attributes.operation == DASD_SEQ_PRESTAGE ||
4299 	    dedata->attributes.operation == DASD_SEQ_ACCESS) {
4300 
4301 		if (endcyl + basepriv->attrib.nr_cyl < basepriv->real_cyl)
4302 			endcyl += basepriv->attrib.nr_cyl;
4303 		else
4304 			endcyl = (basepriv->real_cyl - 1);
4305 	}
4306 
4307 	set_ch_t(&dedata->beg_ext, begcyl, beghead);
4308 	set_ch_t(&dedata->end_ext, endcyl, endhead);
4309 
4310 	dedata->ep_format = 0x20; /* records per track is valid */
4311 	dedata->ep_rec_per_track = blk_per_trk;
4312 
4313 	if (rec_on_trk) {
4314 		switch (basepriv->rdc_data.dev_type) {
4315 		case 0x3390:
4316 			dn = ceil_quot(blksize + 6, 232);
4317 			d = 9 + ceil_quot(blksize + 6 * (dn + 1), 34);
4318 			sector = (49 + (rec_on_trk - 1) * (10 + d)) / 8;
4319 			break;
4320 		case 0x3380:
4321 			d = 7 + ceil_quot(blksize + 12, 32);
4322 			sector = (39 + (rec_on_trk - 1) * (8 + d)) / 7;
4323 			break;
4324 		}
4325 	}
4326 
4327 	if (cmd == DASD_ECKD_CCW_READ_COUNT_MT) {
4328 		lredata->auxiliary.length_valid = 0;
4329 		lredata->auxiliary.length_scope = 0;
4330 		lredata->sector = 0xff;
4331 	} else {
4332 		lredata->auxiliary.length_valid = 1;
4333 		lredata->auxiliary.length_scope = 1;
4334 		lredata->sector = sector;
4335 	}
4336 	lredata->auxiliary.imbedded_ccw_valid = 1;
4337 	lredata->length = tlf;
4338 	lredata->imbedded_ccw = cmd;
4339 	lredata->count = count;
4340 	set_ch_t(&lredata->seek_addr, begcyl, beghead);
4341 	lredata->search_arg.cyl = lredata->seek_addr.cyl;
4342 	lredata->search_arg.head = lredata->seek_addr.head;
4343 	lredata->search_arg.record = rec_on_trk;
4344 
4345 	dcw = itcw_add_dcw(itcw, pfx_cmd, 0,
4346 		     &pfxdata, sizeof(pfxdata), total_data_size);
4347 	return PTR_ERR_OR_ZERO(dcw);
4348 }
4349 
4350 static struct dasd_ccw_req *dasd_eckd_build_cp_tpm_track(
4351 					       struct dasd_device *startdev,
4352 					       struct dasd_block *block,
4353 					       struct request *req,
4354 					       sector_t first_rec,
4355 					       sector_t last_rec,
4356 					       sector_t first_trk,
4357 					       sector_t last_trk,
4358 					       unsigned int first_offs,
4359 					       unsigned int last_offs,
4360 					       unsigned int blk_per_trk,
4361 					       unsigned int blksize)
4362 {
4363 	struct dasd_ccw_req *cqr;
4364 	struct req_iterator iter;
4365 	struct bio_vec bv;
4366 	char *dst;
4367 	unsigned int trkcount, ctidaw;
4368 	unsigned char cmd;
4369 	struct dasd_device *basedev;
4370 	unsigned int tlf;
4371 	struct itcw *itcw;
4372 	struct tidaw *last_tidaw = NULL;
4373 	int itcw_op;
4374 	size_t itcw_size;
4375 	u8 tidaw_flags;
4376 	unsigned int seg_len, part_len, len_to_track_end;
4377 	unsigned char new_track;
4378 	sector_t recid, trkid;
4379 	unsigned int offs;
4380 	unsigned int count, count_to_trk_end;
4381 	int ret;
4382 
4383 	basedev = block->base;
4384 	if (rq_data_dir(req) == READ) {
4385 		cmd = DASD_ECKD_CCW_READ_TRACK_DATA;
4386 		itcw_op = ITCW_OP_READ;
4387 	} else if (rq_data_dir(req) == WRITE) {
4388 		cmd = DASD_ECKD_CCW_WRITE_TRACK_DATA;
4389 		itcw_op = ITCW_OP_WRITE;
4390 	} else
4391 		return ERR_PTR(-EINVAL);
4392 
4393 	/* trackbased I/O needs address all memory via TIDAWs,
4394 	 * not just for 64 bit addresses. This allows us to map
4395 	 * each segment directly to one tidaw.
4396 	 * In the case of write requests, additional tidaws may
4397 	 * be needed when a segment crosses a track boundary.
4398 	 */
4399 	trkcount = last_trk - first_trk + 1;
4400 	ctidaw = 0;
4401 	rq_for_each_segment(bv, req, iter) {
4402 		++ctidaw;
4403 	}
4404 	if (rq_data_dir(req) == WRITE)
4405 		ctidaw += (last_trk - first_trk);
4406 
4407 	/* Allocate the ccw request. */
4408 	itcw_size = itcw_calc_size(0, ctidaw, 0);
4409 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 0, itcw_size, startdev,
4410 				   blk_mq_rq_to_pdu(req));
4411 	if (IS_ERR(cqr))
4412 		return cqr;
4413 
4414 	/* transfer length factor: how many bytes to read from the last track */
4415 	if (first_trk == last_trk)
4416 		tlf = last_offs - first_offs + 1;
4417 	else
4418 		tlf = last_offs + 1;
4419 	tlf *= blksize;
4420 
4421 	itcw = itcw_init(cqr->data, itcw_size, itcw_op, 0, ctidaw, 0);
4422 	if (IS_ERR(itcw)) {
4423 		ret = -EINVAL;
4424 		goto out_error;
4425 	}
4426 	cqr->cpaddr = itcw_get_tcw(itcw);
4427 	if (prepare_itcw(itcw, first_trk, last_trk,
4428 			 cmd, basedev, startdev,
4429 			 first_offs + 1,
4430 			 trkcount, blksize,
4431 			 (last_rec - first_rec + 1) * blksize,
4432 			 tlf, blk_per_trk) == -EAGAIN) {
4433 		/* Clock not in sync and XRC is enabled.
4434 		 * Try again later.
4435 		 */
4436 		ret = -EAGAIN;
4437 		goto out_error;
4438 	}
4439 	len_to_track_end = 0;
4440 	/*
4441 	 * A tidaw can address 4k of memory, but must not cross page boundaries
4442 	 * We can let the block layer handle this by setting
4443 	 * blk_queue_segment_boundary to page boundaries and
4444 	 * blk_max_segment_size to page size when setting up the request queue.
4445 	 * For write requests, a TIDAW must not cross track boundaries, because
4446 	 * we have to set the CBC flag on the last tidaw for each track.
4447 	 */
4448 	if (rq_data_dir(req) == WRITE) {
4449 		new_track = 1;
4450 		recid = first_rec;
4451 		rq_for_each_segment(bv, req, iter) {
4452 			dst = page_address(bv.bv_page) + bv.bv_offset;
4453 			seg_len = bv.bv_len;
4454 			while (seg_len) {
4455 				if (new_track) {
4456 					trkid = recid;
4457 					offs = sector_div(trkid, blk_per_trk);
4458 					count_to_trk_end = blk_per_trk - offs;
4459 					count = min((last_rec - recid + 1),
4460 						    (sector_t)count_to_trk_end);
4461 					len_to_track_end = count * blksize;
4462 					recid += count;
4463 					new_track = 0;
4464 				}
4465 				part_len = min(seg_len, len_to_track_end);
4466 				seg_len -= part_len;
4467 				len_to_track_end -= part_len;
4468 				/* We need to end the tidaw at track end */
4469 				if (!len_to_track_end) {
4470 					new_track = 1;
4471 					tidaw_flags = TIDAW_FLAGS_INSERT_CBC;
4472 				} else
4473 					tidaw_flags = 0;
4474 				last_tidaw = itcw_add_tidaw(itcw, tidaw_flags,
4475 							    dst, part_len);
4476 				if (IS_ERR(last_tidaw)) {
4477 					ret = -EINVAL;
4478 					goto out_error;
4479 				}
4480 				dst += part_len;
4481 			}
4482 		}
4483 	} else {
4484 		rq_for_each_segment(bv, req, iter) {
4485 			dst = page_address(bv.bv_page) + bv.bv_offset;
4486 			last_tidaw = itcw_add_tidaw(itcw, 0x00,
4487 						    dst, bv.bv_len);
4488 			if (IS_ERR(last_tidaw)) {
4489 				ret = -EINVAL;
4490 				goto out_error;
4491 			}
4492 		}
4493 	}
4494 	last_tidaw->flags |= TIDAW_FLAGS_LAST;
4495 	last_tidaw->flags &= ~TIDAW_FLAGS_INSERT_CBC;
4496 	itcw_finalize(itcw);
4497 
4498 	if (blk_noretry_request(req) ||
4499 	    block->base->features & DASD_FEATURE_FAILFAST)
4500 		set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
4501 	cqr->cpmode = 1;
4502 	cqr->startdev = startdev;
4503 	cqr->memdev = startdev;
4504 	cqr->block = block;
4505 	cqr->expires = startdev->default_expires * HZ;	/* default 5 minutes */
4506 	cqr->lpm = dasd_path_get_ppm(startdev);
4507 	cqr->retries = startdev->default_retries;
4508 	cqr->buildclk = get_tod_clock();
4509 	cqr->status = DASD_CQR_FILLED;
4510 
4511 	/* Set flags to suppress output for expected errors */
4512 	if (dasd_eckd_is_ese(basedev)) {
4513 		set_bit(DASD_CQR_SUPPRESS_FP, &cqr->flags);
4514 		set_bit(DASD_CQR_SUPPRESS_IL, &cqr->flags);
4515 		set_bit(DASD_CQR_SUPPRESS_NRF, &cqr->flags);
4516 	}
4517 
4518 	return cqr;
4519 out_error:
4520 	dasd_sfree_request(cqr, startdev);
4521 	return ERR_PTR(ret);
4522 }
4523 
4524 static struct dasd_ccw_req *dasd_eckd_build_cp(struct dasd_device *startdev,
4525 					       struct dasd_block *block,
4526 					       struct request *req)
4527 {
4528 	int cmdrtd, cmdwtd;
4529 	int use_prefix;
4530 	int fcx_multitrack;
4531 	struct dasd_eckd_private *private;
4532 	struct dasd_device *basedev;
4533 	sector_t first_rec, last_rec;
4534 	sector_t first_trk, last_trk;
4535 	unsigned int first_offs, last_offs;
4536 	unsigned int blk_per_trk, blksize;
4537 	int cdlspecial;
4538 	unsigned int data_size;
4539 	struct dasd_ccw_req *cqr;
4540 
4541 	basedev = block->base;
4542 	private = basedev->private;
4543 
4544 	/* Calculate number of blocks/records per track. */
4545 	blksize = block->bp_block;
4546 	blk_per_trk = recs_per_track(&private->rdc_data, 0, blksize);
4547 	if (blk_per_trk == 0)
4548 		return ERR_PTR(-EINVAL);
4549 	/* Calculate record id of first and last block. */
4550 	first_rec = first_trk = blk_rq_pos(req) >> block->s2b_shift;
4551 	first_offs = sector_div(first_trk, blk_per_trk);
4552 	last_rec = last_trk =
4553 		(blk_rq_pos(req) + blk_rq_sectors(req) - 1) >> block->s2b_shift;
4554 	last_offs = sector_div(last_trk, blk_per_trk);
4555 	cdlspecial = (private->uses_cdl && first_rec < 2*blk_per_trk);
4556 
4557 	fcx_multitrack = private->features.feature[40] & 0x20;
4558 	data_size = blk_rq_bytes(req);
4559 	if (data_size % blksize)
4560 		return ERR_PTR(-EINVAL);
4561 	/* tpm write request add CBC data on each track boundary */
4562 	if (rq_data_dir(req) == WRITE)
4563 		data_size += (last_trk - first_trk) * 4;
4564 
4565 	/* is read track data and write track data in command mode supported? */
4566 	cmdrtd = private->features.feature[9] & 0x20;
4567 	cmdwtd = private->features.feature[12] & 0x40;
4568 	use_prefix = private->features.feature[8] & 0x01;
4569 
4570 	cqr = NULL;
4571 	if (cdlspecial || dasd_page_cache) {
4572 		/* do nothing, just fall through to the cmd mode single case */
4573 	} else if ((data_size <= private->fcx_max_data)
4574 		   && (fcx_multitrack || (first_trk == last_trk))) {
4575 		cqr = dasd_eckd_build_cp_tpm_track(startdev, block, req,
4576 						    first_rec, last_rec,
4577 						    first_trk, last_trk,
4578 						    first_offs, last_offs,
4579 						    blk_per_trk, blksize);
4580 		if (IS_ERR(cqr) && (PTR_ERR(cqr) != -EAGAIN) &&
4581 		    (PTR_ERR(cqr) != -ENOMEM))
4582 			cqr = NULL;
4583 	} else if (use_prefix &&
4584 		   (((rq_data_dir(req) == READ) && cmdrtd) ||
4585 		    ((rq_data_dir(req) == WRITE) && cmdwtd))) {
4586 		cqr = dasd_eckd_build_cp_cmd_track(startdev, block, req,
4587 						   first_rec, last_rec,
4588 						   first_trk, last_trk,
4589 						   first_offs, last_offs,
4590 						   blk_per_trk, blksize);
4591 		if (IS_ERR(cqr) && (PTR_ERR(cqr) != -EAGAIN) &&
4592 		    (PTR_ERR(cqr) != -ENOMEM))
4593 			cqr = NULL;
4594 	}
4595 	if (!cqr)
4596 		cqr = dasd_eckd_build_cp_cmd_single(startdev, block, req,
4597 						    first_rec, last_rec,
4598 						    first_trk, last_trk,
4599 						    first_offs, last_offs,
4600 						    blk_per_trk, blksize);
4601 	return cqr;
4602 }
4603 
4604 static struct dasd_ccw_req *dasd_eckd_build_cp_raw(struct dasd_device *startdev,
4605 						   struct dasd_block *block,
4606 						   struct request *req)
4607 {
4608 	sector_t start_padding_sectors, end_sector_offset, end_padding_sectors;
4609 	unsigned int seg_len, len_to_track_end;
4610 	unsigned int cidaw, cplength, datasize;
4611 	sector_t first_trk, last_trk, sectors;
4612 	struct dasd_eckd_private *base_priv;
4613 	struct dasd_device *basedev;
4614 	struct req_iterator iter;
4615 	struct dasd_ccw_req *cqr;
4616 	unsigned int first_offs;
4617 	unsigned int trkcount;
4618 	unsigned long *idaws;
4619 	unsigned int size;
4620 	unsigned char cmd;
4621 	struct bio_vec bv;
4622 	struct ccw1 *ccw;
4623 	int use_prefix;
4624 	void *data;
4625 	char *dst;
4626 
4627 	/*
4628 	 * raw track access needs to be mutiple of 64k and on 64k boundary
4629 	 * For read requests we can fix an incorrect alignment by padding
4630 	 * the request with dummy pages.
4631 	 */
4632 	start_padding_sectors = blk_rq_pos(req) % DASD_RAW_SECTORS_PER_TRACK;
4633 	end_sector_offset = (blk_rq_pos(req) + blk_rq_sectors(req)) %
4634 		DASD_RAW_SECTORS_PER_TRACK;
4635 	end_padding_sectors = (DASD_RAW_SECTORS_PER_TRACK - end_sector_offset) %
4636 		DASD_RAW_SECTORS_PER_TRACK;
4637 	basedev = block->base;
4638 	if ((start_padding_sectors || end_padding_sectors) &&
4639 	    (rq_data_dir(req) == WRITE)) {
4640 		DBF_DEV_EVENT(DBF_ERR, basedev,
4641 			      "raw write not track aligned (%llu,%llu) req %p",
4642 			      start_padding_sectors, end_padding_sectors, req);
4643 		return ERR_PTR(-EINVAL);
4644 	}
4645 
4646 	first_trk = blk_rq_pos(req) / DASD_RAW_SECTORS_PER_TRACK;
4647 	last_trk = (blk_rq_pos(req) + blk_rq_sectors(req) - 1) /
4648 		DASD_RAW_SECTORS_PER_TRACK;
4649 	trkcount = last_trk - first_trk + 1;
4650 	first_offs = 0;
4651 
4652 	if (rq_data_dir(req) == READ)
4653 		cmd = DASD_ECKD_CCW_READ_TRACK;
4654 	else if (rq_data_dir(req) == WRITE)
4655 		cmd = DASD_ECKD_CCW_WRITE_FULL_TRACK;
4656 	else
4657 		return ERR_PTR(-EINVAL);
4658 
4659 	/*
4660 	 * Raw track based I/O needs IDAWs for each page,
4661 	 * and not just for 64 bit addresses.
4662 	 */
4663 	cidaw = trkcount * DASD_RAW_BLOCK_PER_TRACK;
4664 
4665 	/*
4666 	 * struct PFX_eckd_data and struct LRE_eckd_data can have up to 2 bytes
4667 	 * of extended parameter. This is needed for write full track.
4668 	 */
4669 	base_priv = basedev->private;
4670 	use_prefix = base_priv->features.feature[8] & 0x01;
4671 	if (use_prefix) {
4672 		cplength = 1 + trkcount;
4673 		size = sizeof(struct PFX_eckd_data) + 2;
4674 	} else {
4675 		cplength = 2 + trkcount;
4676 		size = sizeof(struct DE_eckd_data) +
4677 			sizeof(struct LRE_eckd_data) + 2;
4678 	}
4679 	size = ALIGN(size, 8);
4680 
4681 	datasize = size + cidaw * sizeof(unsigned long);
4682 
4683 	/* Allocate the ccw request. */
4684 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength,
4685 				   datasize, startdev, blk_mq_rq_to_pdu(req));
4686 	if (IS_ERR(cqr))
4687 		return cqr;
4688 
4689 	ccw = cqr->cpaddr;
4690 	data = cqr->data;
4691 
4692 	if (use_prefix) {
4693 		prefix_LRE(ccw++, data, first_trk, last_trk, cmd, basedev,
4694 			   startdev, 1, first_offs + 1, trkcount, 0, 0);
4695 	} else {
4696 		define_extent(ccw++, data, first_trk, last_trk, cmd, basedev, 0);
4697 		ccw[-1].flags |= CCW_FLAG_CC;
4698 
4699 		data += sizeof(struct DE_eckd_data);
4700 		locate_record_ext(ccw++, data, first_trk, first_offs + 1,
4701 				  trkcount, cmd, basedev, 0, 0);
4702 	}
4703 
4704 	idaws = (unsigned long *)(cqr->data + size);
4705 	len_to_track_end = 0;
4706 	if (start_padding_sectors) {
4707 		ccw[-1].flags |= CCW_FLAG_CC;
4708 		ccw->cmd_code = cmd;
4709 		/* maximum 3390 track size */
4710 		ccw->count = 57326;
4711 		/* 64k map to one track */
4712 		len_to_track_end = 65536 - start_padding_sectors * 512;
4713 		ccw->cda = (__u32)(addr_t)idaws;
4714 		ccw->flags |= CCW_FLAG_IDA;
4715 		ccw->flags |= CCW_FLAG_SLI;
4716 		ccw++;
4717 		for (sectors = 0; sectors < start_padding_sectors; sectors += 8)
4718 			idaws = idal_create_words(idaws, rawpadpage, PAGE_SIZE);
4719 	}
4720 	rq_for_each_segment(bv, req, iter) {
4721 		dst = page_address(bv.bv_page) + bv.bv_offset;
4722 		seg_len = bv.bv_len;
4723 		if (cmd == DASD_ECKD_CCW_READ_TRACK)
4724 			memset(dst, 0, seg_len);
4725 		if (!len_to_track_end) {
4726 			ccw[-1].flags |= CCW_FLAG_CC;
4727 			ccw->cmd_code = cmd;
4728 			/* maximum 3390 track size */
4729 			ccw->count = 57326;
4730 			/* 64k map to one track */
4731 			len_to_track_end = 65536;
4732 			ccw->cda = (__u32)(addr_t)idaws;
4733 			ccw->flags |= CCW_FLAG_IDA;
4734 			ccw->flags |= CCW_FLAG_SLI;
4735 			ccw++;
4736 		}
4737 		len_to_track_end -= seg_len;
4738 		idaws = idal_create_words(idaws, dst, seg_len);
4739 	}
4740 	for (sectors = 0; sectors < end_padding_sectors; sectors += 8)
4741 		idaws = idal_create_words(idaws, rawpadpage, PAGE_SIZE);
4742 	if (blk_noretry_request(req) ||
4743 	    block->base->features & DASD_FEATURE_FAILFAST)
4744 		set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
4745 	cqr->startdev = startdev;
4746 	cqr->memdev = startdev;
4747 	cqr->block = block;
4748 	cqr->expires = startdev->default_expires * HZ;
4749 	cqr->lpm = dasd_path_get_ppm(startdev);
4750 	cqr->retries = startdev->default_retries;
4751 	cqr->buildclk = get_tod_clock();
4752 	cqr->status = DASD_CQR_FILLED;
4753 
4754 	return cqr;
4755 }
4756 
4757 
4758 static int
4759 dasd_eckd_free_cp(struct dasd_ccw_req *cqr, struct request *req)
4760 {
4761 	struct dasd_eckd_private *private;
4762 	struct ccw1 *ccw;
4763 	struct req_iterator iter;
4764 	struct bio_vec bv;
4765 	char *dst, *cda;
4766 	unsigned int blksize, blk_per_trk, off;
4767 	sector_t recid;
4768 	int status;
4769 
4770 	if (!dasd_page_cache)
4771 		goto out;
4772 	private = cqr->block->base->private;
4773 	blksize = cqr->block->bp_block;
4774 	blk_per_trk = recs_per_track(&private->rdc_data, 0, blksize);
4775 	recid = blk_rq_pos(req) >> cqr->block->s2b_shift;
4776 	ccw = cqr->cpaddr;
4777 	/* Skip over define extent & locate record. */
4778 	ccw++;
4779 	if (private->uses_cdl == 0 || recid > 2*blk_per_trk)
4780 		ccw++;
4781 	rq_for_each_segment(bv, req, iter) {
4782 		dst = page_address(bv.bv_page) + bv.bv_offset;
4783 		for (off = 0; off < bv.bv_len; off += blksize) {
4784 			/* Skip locate record. */
4785 			if (private->uses_cdl && recid <= 2*blk_per_trk)
4786 				ccw++;
4787 			if (dst) {
4788 				if (ccw->flags & CCW_FLAG_IDA)
4789 					cda = *((char **)((addr_t) ccw->cda));
4790 				else
4791 					cda = (char *)((addr_t) ccw->cda);
4792 				if (dst != cda) {
4793 					if (rq_data_dir(req) == READ)
4794 						memcpy(dst, cda, bv.bv_len);
4795 					kmem_cache_free(dasd_page_cache,
4796 					    (void *)((addr_t)cda & PAGE_MASK));
4797 				}
4798 				dst = NULL;
4799 			}
4800 			ccw++;
4801 			recid++;
4802 		}
4803 	}
4804 out:
4805 	status = cqr->status == DASD_CQR_DONE;
4806 	dasd_sfree_request(cqr, cqr->memdev);
4807 	return status;
4808 }
4809 
4810 /*
4811  * Modify ccw/tcw in cqr so it can be started on a base device.
4812  *
4813  * Note that this is not enough to restart the cqr!
4814  * Either reset cqr->startdev as well (summary unit check handling)
4815  * or restart via separate cqr (as in ERP handling).
4816  */
4817 void dasd_eckd_reset_ccw_to_base_io(struct dasd_ccw_req *cqr)
4818 {
4819 	struct ccw1 *ccw;
4820 	struct PFX_eckd_data *pfxdata;
4821 	struct tcw *tcw;
4822 	struct tccb *tccb;
4823 	struct dcw *dcw;
4824 
4825 	if (cqr->cpmode == 1) {
4826 		tcw = cqr->cpaddr;
4827 		tccb = tcw_get_tccb(tcw);
4828 		dcw = (struct dcw *)&tccb->tca[0];
4829 		pfxdata = (struct PFX_eckd_data *)&dcw->cd[0];
4830 		pfxdata->validity.verify_base = 0;
4831 		pfxdata->validity.hyper_pav = 0;
4832 	} else {
4833 		ccw = cqr->cpaddr;
4834 		pfxdata = cqr->data;
4835 		if (ccw->cmd_code == DASD_ECKD_CCW_PFX) {
4836 			pfxdata->validity.verify_base = 0;
4837 			pfxdata->validity.hyper_pav = 0;
4838 		}
4839 	}
4840 }
4841 
4842 #define DASD_ECKD_CHANQ_MAX_SIZE 4
4843 
4844 static struct dasd_ccw_req *dasd_eckd_build_alias_cp(struct dasd_device *base,
4845 						     struct dasd_block *block,
4846 						     struct request *req)
4847 {
4848 	struct dasd_eckd_private *private;
4849 	struct dasd_device *startdev;
4850 	unsigned long flags;
4851 	struct dasd_ccw_req *cqr;
4852 
4853 	startdev = dasd_alias_get_start_dev(base);
4854 	if (!startdev)
4855 		startdev = base;
4856 	private = startdev->private;
4857 	if (private->count >= DASD_ECKD_CHANQ_MAX_SIZE)
4858 		return ERR_PTR(-EBUSY);
4859 
4860 	spin_lock_irqsave(get_ccwdev_lock(startdev->cdev), flags);
4861 	private->count++;
4862 	if ((base->features & DASD_FEATURE_USERAW))
4863 		cqr = dasd_eckd_build_cp_raw(startdev, block, req);
4864 	else
4865 		cqr = dasd_eckd_build_cp(startdev, block, req);
4866 	if (IS_ERR(cqr))
4867 		private->count--;
4868 	spin_unlock_irqrestore(get_ccwdev_lock(startdev->cdev), flags);
4869 	return cqr;
4870 }
4871 
4872 static int dasd_eckd_free_alias_cp(struct dasd_ccw_req *cqr,
4873 				   struct request *req)
4874 {
4875 	struct dasd_eckd_private *private;
4876 	unsigned long flags;
4877 
4878 	spin_lock_irqsave(get_ccwdev_lock(cqr->memdev->cdev), flags);
4879 	private = cqr->memdev->private;
4880 	private->count--;
4881 	spin_unlock_irqrestore(get_ccwdev_lock(cqr->memdev->cdev), flags);
4882 	return dasd_eckd_free_cp(cqr, req);
4883 }
4884 
4885 static int
4886 dasd_eckd_fill_info(struct dasd_device * device,
4887 		    struct dasd_information2_t * info)
4888 {
4889 	struct dasd_eckd_private *private = device->private;
4890 
4891 	info->label_block = 2;
4892 	info->FBA_layout = private->uses_cdl ? 0 : 1;
4893 	info->format = private->uses_cdl ? DASD_FORMAT_CDL : DASD_FORMAT_LDL;
4894 	info->characteristics_size = sizeof(private->rdc_data);
4895 	memcpy(info->characteristics, &private->rdc_data,
4896 	       sizeof(private->rdc_data));
4897 	info->confdata_size = min((unsigned long)private->conf_len,
4898 				  sizeof(info->configuration_data));
4899 	memcpy(info->configuration_data, private->conf_data,
4900 	       info->confdata_size);
4901 	return 0;
4902 }
4903 
4904 /*
4905  * SECTION: ioctl functions for eckd devices.
4906  */
4907 
4908 /*
4909  * Release device ioctl.
4910  * Buils a channel programm to releases a prior reserved
4911  * (see dasd_eckd_reserve) device.
4912  */
4913 static int
4914 dasd_eckd_release(struct dasd_device *device)
4915 {
4916 	struct dasd_ccw_req *cqr;
4917 	int rc;
4918 	struct ccw1 *ccw;
4919 	int useglobal;
4920 
4921 	if (!capable(CAP_SYS_ADMIN))
4922 		return -EACCES;
4923 
4924 	useglobal = 0;
4925 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1, 32, device, NULL);
4926 	if (IS_ERR(cqr)) {
4927 		mutex_lock(&dasd_reserve_mutex);
4928 		useglobal = 1;
4929 		cqr = &dasd_reserve_req->cqr;
4930 		memset(cqr, 0, sizeof(*cqr));
4931 		memset(&dasd_reserve_req->ccw, 0,
4932 		       sizeof(dasd_reserve_req->ccw));
4933 		cqr->cpaddr = &dasd_reserve_req->ccw;
4934 		cqr->data = &dasd_reserve_req->data;
4935 		cqr->magic = DASD_ECKD_MAGIC;
4936 	}
4937 	ccw = cqr->cpaddr;
4938 	ccw->cmd_code = DASD_ECKD_CCW_RELEASE;
4939 	ccw->flags |= CCW_FLAG_SLI;
4940 	ccw->count = 32;
4941 	ccw->cda = (__u32)(addr_t) cqr->data;
4942 	cqr->startdev = device;
4943 	cqr->memdev = device;
4944 	clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
4945 	set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
4946 	cqr->retries = 2;	/* set retry counter to enable basic ERP */
4947 	cqr->expires = 2 * HZ;
4948 	cqr->buildclk = get_tod_clock();
4949 	cqr->status = DASD_CQR_FILLED;
4950 
4951 	rc = dasd_sleep_on_immediatly(cqr);
4952 	if (!rc)
4953 		clear_bit(DASD_FLAG_IS_RESERVED, &device->flags);
4954 
4955 	if (useglobal)
4956 		mutex_unlock(&dasd_reserve_mutex);
4957 	else
4958 		dasd_sfree_request(cqr, cqr->memdev);
4959 	return rc;
4960 }
4961 
4962 /*
4963  * Reserve device ioctl.
4964  * Options are set to 'synchronous wait for interrupt' and
4965  * 'timeout the request'. This leads to a terminate IO if
4966  * the interrupt is outstanding for a certain time.
4967  */
4968 static int
4969 dasd_eckd_reserve(struct dasd_device *device)
4970 {
4971 	struct dasd_ccw_req *cqr;
4972 	int rc;
4973 	struct ccw1 *ccw;
4974 	int useglobal;
4975 
4976 	if (!capable(CAP_SYS_ADMIN))
4977 		return -EACCES;
4978 
4979 	useglobal = 0;
4980 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1, 32, device, NULL);
4981 	if (IS_ERR(cqr)) {
4982 		mutex_lock(&dasd_reserve_mutex);
4983 		useglobal = 1;
4984 		cqr = &dasd_reserve_req->cqr;
4985 		memset(cqr, 0, sizeof(*cqr));
4986 		memset(&dasd_reserve_req->ccw, 0,
4987 		       sizeof(dasd_reserve_req->ccw));
4988 		cqr->cpaddr = &dasd_reserve_req->ccw;
4989 		cqr->data = &dasd_reserve_req->data;
4990 		cqr->magic = DASD_ECKD_MAGIC;
4991 	}
4992 	ccw = cqr->cpaddr;
4993 	ccw->cmd_code = DASD_ECKD_CCW_RESERVE;
4994 	ccw->flags |= CCW_FLAG_SLI;
4995 	ccw->count = 32;
4996 	ccw->cda = (__u32)(addr_t) cqr->data;
4997 	cqr->startdev = device;
4998 	cqr->memdev = device;
4999 	clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
5000 	set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
5001 	cqr->retries = 2;	/* set retry counter to enable basic ERP */
5002 	cqr->expires = 2 * HZ;
5003 	cqr->buildclk = get_tod_clock();
5004 	cqr->status = DASD_CQR_FILLED;
5005 
5006 	rc = dasd_sleep_on_immediatly(cqr);
5007 	if (!rc)
5008 		set_bit(DASD_FLAG_IS_RESERVED, &device->flags);
5009 
5010 	if (useglobal)
5011 		mutex_unlock(&dasd_reserve_mutex);
5012 	else
5013 		dasd_sfree_request(cqr, cqr->memdev);
5014 	return rc;
5015 }
5016 
5017 /*
5018  * Steal lock ioctl - unconditional reserve device.
5019  * Buils a channel programm to break a device's reservation.
5020  * (unconditional reserve)
5021  */
5022 static int
5023 dasd_eckd_steal_lock(struct dasd_device *device)
5024 {
5025 	struct dasd_ccw_req *cqr;
5026 	int rc;
5027 	struct ccw1 *ccw;
5028 	int useglobal;
5029 
5030 	if (!capable(CAP_SYS_ADMIN))
5031 		return -EACCES;
5032 
5033 	useglobal = 0;
5034 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1, 32, device, NULL);
5035 	if (IS_ERR(cqr)) {
5036 		mutex_lock(&dasd_reserve_mutex);
5037 		useglobal = 1;
5038 		cqr = &dasd_reserve_req->cqr;
5039 		memset(cqr, 0, sizeof(*cqr));
5040 		memset(&dasd_reserve_req->ccw, 0,
5041 		       sizeof(dasd_reserve_req->ccw));
5042 		cqr->cpaddr = &dasd_reserve_req->ccw;
5043 		cqr->data = &dasd_reserve_req->data;
5044 		cqr->magic = DASD_ECKD_MAGIC;
5045 	}
5046 	ccw = cqr->cpaddr;
5047 	ccw->cmd_code = DASD_ECKD_CCW_SLCK;
5048 	ccw->flags |= CCW_FLAG_SLI;
5049 	ccw->count = 32;
5050 	ccw->cda = (__u32)(addr_t) cqr->data;
5051 	cqr->startdev = device;
5052 	cqr->memdev = device;
5053 	clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
5054 	set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
5055 	cqr->retries = 2;	/* set retry counter to enable basic ERP */
5056 	cqr->expires = 2 * HZ;
5057 	cqr->buildclk = get_tod_clock();
5058 	cqr->status = DASD_CQR_FILLED;
5059 
5060 	rc = dasd_sleep_on_immediatly(cqr);
5061 	if (!rc)
5062 		set_bit(DASD_FLAG_IS_RESERVED, &device->flags);
5063 
5064 	if (useglobal)
5065 		mutex_unlock(&dasd_reserve_mutex);
5066 	else
5067 		dasd_sfree_request(cqr, cqr->memdev);
5068 	return rc;
5069 }
5070 
5071 /*
5072  * SNID - Sense Path Group ID
5073  * This ioctl may be used in situations where I/O is stalled due to
5074  * a reserve, so if the normal dasd_smalloc_request fails, we use the
5075  * preallocated dasd_reserve_req.
5076  */
5077 static int dasd_eckd_snid(struct dasd_device *device,
5078 			  void __user *argp)
5079 {
5080 	struct dasd_ccw_req *cqr;
5081 	int rc;
5082 	struct ccw1 *ccw;
5083 	int useglobal;
5084 	struct dasd_snid_ioctl_data usrparm;
5085 
5086 	if (!capable(CAP_SYS_ADMIN))
5087 		return -EACCES;
5088 
5089 	if (copy_from_user(&usrparm, argp, sizeof(usrparm)))
5090 		return -EFAULT;
5091 
5092 	useglobal = 0;
5093 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1,
5094 				   sizeof(struct dasd_snid_data), device,
5095 				   NULL);
5096 	if (IS_ERR(cqr)) {
5097 		mutex_lock(&dasd_reserve_mutex);
5098 		useglobal = 1;
5099 		cqr = &dasd_reserve_req->cqr;
5100 		memset(cqr, 0, sizeof(*cqr));
5101 		memset(&dasd_reserve_req->ccw, 0,
5102 		       sizeof(dasd_reserve_req->ccw));
5103 		cqr->cpaddr = &dasd_reserve_req->ccw;
5104 		cqr->data = &dasd_reserve_req->data;
5105 		cqr->magic = DASD_ECKD_MAGIC;
5106 	}
5107 	ccw = cqr->cpaddr;
5108 	ccw->cmd_code = DASD_ECKD_CCW_SNID;
5109 	ccw->flags |= CCW_FLAG_SLI;
5110 	ccw->count = 12;
5111 	ccw->cda = (__u32)(addr_t) cqr->data;
5112 	cqr->startdev = device;
5113 	cqr->memdev = device;
5114 	clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
5115 	set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
5116 	set_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags);
5117 	cqr->retries = 5;
5118 	cqr->expires = 10 * HZ;
5119 	cqr->buildclk = get_tod_clock();
5120 	cqr->status = DASD_CQR_FILLED;
5121 	cqr->lpm = usrparm.path_mask;
5122 
5123 	rc = dasd_sleep_on_immediatly(cqr);
5124 	/* verify that I/O processing didn't modify the path mask */
5125 	if (!rc && usrparm.path_mask && (cqr->lpm != usrparm.path_mask))
5126 		rc = -EIO;
5127 	if (!rc) {
5128 		usrparm.data = *((struct dasd_snid_data *)cqr->data);
5129 		if (copy_to_user(argp, &usrparm, sizeof(usrparm)))
5130 			rc = -EFAULT;
5131 	}
5132 
5133 	if (useglobal)
5134 		mutex_unlock(&dasd_reserve_mutex);
5135 	else
5136 		dasd_sfree_request(cqr, cqr->memdev);
5137 	return rc;
5138 }
5139 
5140 /*
5141  * Read performance statistics
5142  */
5143 static int
5144 dasd_eckd_performance(struct dasd_device *device, void __user *argp)
5145 {
5146 	struct dasd_psf_prssd_data *prssdp;
5147 	struct dasd_rssd_perf_stats_t *stats;
5148 	struct dasd_ccw_req *cqr;
5149 	struct ccw1 *ccw;
5150 	int rc;
5151 
5152 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */  + 1 /* RSSD */,
5153 				   (sizeof(struct dasd_psf_prssd_data) +
5154 				    sizeof(struct dasd_rssd_perf_stats_t)),
5155 				   device, NULL);
5156 	if (IS_ERR(cqr)) {
5157 		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
5158 			    "Could not allocate initialization request");
5159 		return PTR_ERR(cqr);
5160 	}
5161 	cqr->startdev = device;
5162 	cqr->memdev = device;
5163 	cqr->retries = 0;
5164 	clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
5165 	cqr->expires = 10 * HZ;
5166 
5167 	/* Prepare for Read Subsystem Data */
5168 	prssdp = (struct dasd_psf_prssd_data *) cqr->data;
5169 	memset(prssdp, 0, sizeof(struct dasd_psf_prssd_data));
5170 	prssdp->order = PSF_ORDER_PRSSD;
5171 	prssdp->suborder = 0x01;	/* Performance Statistics */
5172 	prssdp->varies[1] = 0x01;	/* Perf Statistics for the Subsystem */
5173 
5174 	ccw = cqr->cpaddr;
5175 	ccw->cmd_code = DASD_ECKD_CCW_PSF;
5176 	ccw->count = sizeof(struct dasd_psf_prssd_data);
5177 	ccw->flags |= CCW_FLAG_CC;
5178 	ccw->cda = (__u32)(addr_t) prssdp;
5179 
5180 	/* Read Subsystem Data - Performance Statistics */
5181 	stats = (struct dasd_rssd_perf_stats_t *) (prssdp + 1);
5182 	memset(stats, 0, sizeof(struct dasd_rssd_perf_stats_t));
5183 
5184 	ccw++;
5185 	ccw->cmd_code = DASD_ECKD_CCW_RSSD;
5186 	ccw->count = sizeof(struct dasd_rssd_perf_stats_t);
5187 	ccw->cda = (__u32)(addr_t) stats;
5188 
5189 	cqr->buildclk = get_tod_clock();
5190 	cqr->status = DASD_CQR_FILLED;
5191 	rc = dasd_sleep_on(cqr);
5192 	if (rc == 0) {
5193 		prssdp = (struct dasd_psf_prssd_data *) cqr->data;
5194 		stats = (struct dasd_rssd_perf_stats_t *) (prssdp + 1);
5195 		if (copy_to_user(argp, stats,
5196 				 sizeof(struct dasd_rssd_perf_stats_t)))
5197 			rc = -EFAULT;
5198 	}
5199 	dasd_sfree_request(cqr, cqr->memdev);
5200 	return rc;
5201 }
5202 
5203 /*
5204  * Get attributes (cache operations)
5205  * Returnes the cache attributes used in Define Extend (DE).
5206  */
5207 static int
5208 dasd_eckd_get_attrib(struct dasd_device *device, void __user *argp)
5209 {
5210 	struct dasd_eckd_private *private = device->private;
5211 	struct attrib_data_t attrib = private->attrib;
5212 	int rc;
5213 
5214         if (!capable(CAP_SYS_ADMIN))
5215                 return -EACCES;
5216 	if (!argp)
5217                 return -EINVAL;
5218 
5219 	rc = 0;
5220 	if (copy_to_user(argp, (long *) &attrib,
5221 			 sizeof(struct attrib_data_t)))
5222 		rc = -EFAULT;
5223 
5224 	return rc;
5225 }
5226 
5227 /*
5228  * Set attributes (cache operations)
5229  * Stores the attributes for cache operation to be used in Define Extend (DE).
5230  */
5231 static int
5232 dasd_eckd_set_attrib(struct dasd_device *device, void __user *argp)
5233 {
5234 	struct dasd_eckd_private *private = device->private;
5235 	struct attrib_data_t attrib;
5236 
5237 	if (!capable(CAP_SYS_ADMIN))
5238 		return -EACCES;
5239 	if (!argp)
5240 		return -EINVAL;
5241 
5242 	if (copy_from_user(&attrib, argp, sizeof(struct attrib_data_t)))
5243 		return -EFAULT;
5244 	private->attrib = attrib;
5245 
5246 	dev_info(&device->cdev->dev,
5247 		 "The DASD cache mode was set to %x (%i cylinder prestage)\n",
5248 		 private->attrib.operation, private->attrib.nr_cyl);
5249 	return 0;
5250 }
5251 
5252 /*
5253  * Issue syscall I/O to EMC Symmetrix array.
5254  * CCWs are PSF and RSSD
5255  */
5256 static int dasd_symm_io(struct dasd_device *device, void __user *argp)
5257 {
5258 	struct dasd_symmio_parms usrparm;
5259 	char *psf_data, *rssd_result;
5260 	struct dasd_ccw_req *cqr;
5261 	struct ccw1 *ccw;
5262 	char psf0, psf1;
5263 	int rc;
5264 
5265 	if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
5266 		return -EACCES;
5267 	psf0 = psf1 = 0;
5268 
5269 	/* Copy parms from caller */
5270 	rc = -EFAULT;
5271 	if (copy_from_user(&usrparm, argp, sizeof(usrparm)))
5272 		goto out;
5273 	if (is_compat_task()) {
5274 		/* Make sure pointers are sane even on 31 bit. */
5275 		rc = -EINVAL;
5276 		if ((usrparm.psf_data >> 32) != 0)
5277 			goto out;
5278 		if ((usrparm.rssd_result >> 32) != 0)
5279 			goto out;
5280 		usrparm.psf_data &= 0x7fffffffULL;
5281 		usrparm.rssd_result &= 0x7fffffffULL;
5282 	}
5283 	/* at least 2 bytes are accessed and should be allocated */
5284 	if (usrparm.psf_data_len < 2) {
5285 		DBF_DEV_EVENT(DBF_WARNING, device,
5286 			      "Symmetrix ioctl invalid data length %d",
5287 			      usrparm.psf_data_len);
5288 		rc = -EINVAL;
5289 		goto out;
5290 	}
5291 	/* alloc I/O data area */
5292 	psf_data = kzalloc(usrparm.psf_data_len, GFP_KERNEL | GFP_DMA);
5293 	rssd_result = kzalloc(usrparm.rssd_result_len, GFP_KERNEL | GFP_DMA);
5294 	if (!psf_data || !rssd_result) {
5295 		rc = -ENOMEM;
5296 		goto out_free;
5297 	}
5298 
5299 	/* get syscall header from user space */
5300 	rc = -EFAULT;
5301 	if (copy_from_user(psf_data,
5302 			   (void __user *)(unsigned long) usrparm.psf_data,
5303 			   usrparm.psf_data_len))
5304 		goto out_free;
5305 	psf0 = psf_data[0];
5306 	psf1 = psf_data[1];
5307 
5308 	/* setup CCWs for PSF + RSSD */
5309 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 2, 0, device, NULL);
5310 	if (IS_ERR(cqr)) {
5311 		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
5312 			"Could not allocate initialization request");
5313 		rc = PTR_ERR(cqr);
5314 		goto out_free;
5315 	}
5316 
5317 	cqr->startdev = device;
5318 	cqr->memdev = device;
5319 	cqr->retries = 3;
5320 	cqr->expires = 10 * HZ;
5321 	cqr->buildclk = get_tod_clock();
5322 	cqr->status = DASD_CQR_FILLED;
5323 
5324 	/* Build the ccws */
5325 	ccw = cqr->cpaddr;
5326 
5327 	/* PSF ccw */
5328 	ccw->cmd_code = DASD_ECKD_CCW_PSF;
5329 	ccw->count = usrparm.psf_data_len;
5330 	ccw->flags |= CCW_FLAG_CC;
5331 	ccw->cda = (__u32)(addr_t) psf_data;
5332 
5333 	ccw++;
5334 
5335 	/* RSSD ccw  */
5336 	ccw->cmd_code = DASD_ECKD_CCW_RSSD;
5337 	ccw->count = usrparm.rssd_result_len;
5338 	ccw->flags = CCW_FLAG_SLI ;
5339 	ccw->cda = (__u32)(addr_t) rssd_result;
5340 
5341 	rc = dasd_sleep_on(cqr);
5342 	if (rc)
5343 		goto out_sfree;
5344 
5345 	rc = -EFAULT;
5346 	if (copy_to_user((void __user *)(unsigned long) usrparm.rssd_result,
5347 			   rssd_result, usrparm.rssd_result_len))
5348 		goto out_sfree;
5349 	rc = 0;
5350 
5351 out_sfree:
5352 	dasd_sfree_request(cqr, cqr->memdev);
5353 out_free:
5354 	kfree(rssd_result);
5355 	kfree(psf_data);
5356 out:
5357 	DBF_DEV_EVENT(DBF_WARNING, device,
5358 		      "Symmetrix ioctl (0x%02x 0x%02x): rc=%d",
5359 		      (int) psf0, (int) psf1, rc);
5360 	return rc;
5361 }
5362 
5363 static int
5364 dasd_eckd_ioctl(struct dasd_block *block, unsigned int cmd, void __user *argp)
5365 {
5366 	struct dasd_device *device = block->base;
5367 
5368 	switch (cmd) {
5369 	case BIODASDGATTR:
5370 		return dasd_eckd_get_attrib(device, argp);
5371 	case BIODASDSATTR:
5372 		return dasd_eckd_set_attrib(device, argp);
5373 	case BIODASDPSRD:
5374 		return dasd_eckd_performance(device, argp);
5375 	case BIODASDRLSE:
5376 		return dasd_eckd_release(device);
5377 	case BIODASDRSRV:
5378 		return dasd_eckd_reserve(device);
5379 	case BIODASDSLCK:
5380 		return dasd_eckd_steal_lock(device);
5381 	case BIODASDSNID:
5382 		return dasd_eckd_snid(device, argp);
5383 	case BIODASDSYMMIO:
5384 		return dasd_symm_io(device, argp);
5385 	default:
5386 		return -ENOTTY;
5387 	}
5388 }
5389 
5390 /*
5391  * Dump the range of CCWs into 'page' buffer
5392  * and return number of printed chars.
5393  */
5394 static int
5395 dasd_eckd_dump_ccw_range(struct ccw1 *from, struct ccw1 *to, char *page)
5396 {
5397 	int len, count;
5398 	char *datap;
5399 
5400 	len = 0;
5401 	while (from <= to) {
5402 		len += sprintf(page + len, PRINTK_HEADER
5403 			       " CCW %p: %08X %08X DAT:",
5404 			       from, ((int *) from)[0], ((int *) from)[1]);
5405 
5406 		/* get pointer to data (consider IDALs) */
5407 		if (from->flags & CCW_FLAG_IDA)
5408 			datap = (char *) *((addr_t *) (addr_t) from->cda);
5409 		else
5410 			datap = (char *) ((addr_t) from->cda);
5411 
5412 		/* dump data (max 32 bytes) */
5413 		for (count = 0; count < from->count && count < 32; count++) {
5414 			if (count % 8 == 0) len += sprintf(page + len, " ");
5415 			if (count % 4 == 0) len += sprintf(page + len, " ");
5416 			len += sprintf(page + len, "%02x", datap[count]);
5417 		}
5418 		len += sprintf(page + len, "\n");
5419 		from++;
5420 	}
5421 	return len;
5422 }
5423 
5424 static void
5425 dasd_eckd_dump_sense_dbf(struct dasd_device *device, struct irb *irb,
5426 			 char *reason)
5427 {
5428 	u64 *sense;
5429 	u64 *stat;
5430 
5431 	sense = (u64 *) dasd_get_sense(irb);
5432 	stat = (u64 *) &irb->scsw;
5433 	if (sense) {
5434 		DBF_DEV_EVENT(DBF_EMERG, device, "%s: %016llx %08x : "
5435 			      "%016llx %016llx %016llx %016llx",
5436 			      reason, *stat, *((u32 *) (stat + 1)),
5437 			      sense[0], sense[1], sense[2], sense[3]);
5438 	} else {
5439 		DBF_DEV_EVENT(DBF_EMERG, device, "%s: %016llx %08x : %s",
5440 			      reason, *stat, *((u32 *) (stat + 1)),
5441 			      "NO VALID SENSE");
5442 	}
5443 }
5444 
5445 /*
5446  * Print sense data and related channel program.
5447  * Parts are printed because printk buffer is only 1024 bytes.
5448  */
5449 static void dasd_eckd_dump_sense_ccw(struct dasd_device *device,
5450 				 struct dasd_ccw_req *req, struct irb *irb)
5451 {
5452 	char *page;
5453 	struct ccw1 *first, *last, *fail, *from, *to;
5454 	int len, sl, sct;
5455 
5456 	page = (char *) get_zeroed_page(GFP_ATOMIC);
5457 	if (page == NULL) {
5458 		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
5459 			      "No memory to dump sense data\n");
5460 		return;
5461 	}
5462 	/* dump the sense data */
5463 	len = sprintf(page, PRINTK_HEADER
5464 		      " I/O status report for device %s:\n",
5465 		      dev_name(&device->cdev->dev));
5466 	len += sprintf(page + len, PRINTK_HEADER
5467 		       " in req: %p CC:%02X FC:%02X AC:%02X SC:%02X DS:%02X "
5468 		       "CS:%02X RC:%d\n",
5469 		       req, scsw_cc(&irb->scsw), scsw_fctl(&irb->scsw),
5470 		       scsw_actl(&irb->scsw), scsw_stctl(&irb->scsw),
5471 		       scsw_dstat(&irb->scsw), scsw_cstat(&irb->scsw),
5472 		       req ? req->intrc : 0);
5473 	len += sprintf(page + len, PRINTK_HEADER
5474 		       " device %s: Failing CCW: %p\n",
5475 		       dev_name(&device->cdev->dev),
5476 		       (void *) (addr_t) irb->scsw.cmd.cpa);
5477 	if (irb->esw.esw0.erw.cons) {
5478 		for (sl = 0; sl < 4; sl++) {
5479 			len += sprintf(page + len, PRINTK_HEADER
5480 				       " Sense(hex) %2d-%2d:",
5481 				       (8 * sl), ((8 * sl) + 7));
5482 
5483 			for (sct = 0; sct < 8; sct++) {
5484 				len += sprintf(page + len, " %02x",
5485 					       irb->ecw[8 * sl + sct]);
5486 			}
5487 			len += sprintf(page + len, "\n");
5488 		}
5489 
5490 		if (irb->ecw[27] & DASD_SENSE_BIT_0) {
5491 			/* 24 Byte Sense Data */
5492 			sprintf(page + len, PRINTK_HEADER
5493 				" 24 Byte: %x MSG %x, "
5494 				"%s MSGb to SYSOP\n",
5495 				irb->ecw[7] >> 4, irb->ecw[7] & 0x0f,
5496 				irb->ecw[1] & 0x10 ? "" : "no");
5497 		} else {
5498 			/* 32 Byte Sense Data */
5499 			sprintf(page + len, PRINTK_HEADER
5500 				" 32 Byte: Format: %x "
5501 				"Exception class %x\n",
5502 				irb->ecw[6] & 0x0f, irb->ecw[22] >> 4);
5503 		}
5504 	} else {
5505 		sprintf(page + len, PRINTK_HEADER
5506 			" SORRY - NO VALID SENSE AVAILABLE\n");
5507 	}
5508 	printk(KERN_ERR "%s", page);
5509 
5510 	if (req) {
5511 		/* req == NULL for unsolicited interrupts */
5512 		/* dump the Channel Program (max 140 Bytes per line) */
5513 		/* Count CCW and print first CCWs (maximum 1024 % 140 = 7) */
5514 		first = req->cpaddr;
5515 		for (last = first; last->flags & (CCW_FLAG_CC | CCW_FLAG_DC); last++);
5516 		to = min(first + 6, last);
5517 		len = sprintf(page, PRINTK_HEADER
5518 			      " Related CP in req: %p\n", req);
5519 		dasd_eckd_dump_ccw_range(first, to, page + len);
5520 		printk(KERN_ERR "%s", page);
5521 
5522 		/* print failing CCW area (maximum 4) */
5523 		/* scsw->cda is either valid or zero  */
5524 		len = 0;
5525 		from = ++to;
5526 		fail = (struct ccw1 *)(addr_t)
5527 				irb->scsw.cmd.cpa; /* failing CCW */
5528 		if (from <  fail - 2) {
5529 			from = fail - 2;     /* there is a gap - print header */
5530 			len += sprintf(page, PRINTK_HEADER "......\n");
5531 		}
5532 		to = min(fail + 1, last);
5533 		len += dasd_eckd_dump_ccw_range(from, to, page + len);
5534 
5535 		/* print last CCWs (maximum 2) */
5536 		from = max(from, ++to);
5537 		if (from < last - 1) {
5538 			from = last - 1;     /* there is a gap - print header */
5539 			len += sprintf(page + len, PRINTK_HEADER "......\n");
5540 		}
5541 		len += dasd_eckd_dump_ccw_range(from, last, page + len);
5542 		if (len > 0)
5543 			printk(KERN_ERR "%s", page);
5544 	}
5545 	free_page((unsigned long) page);
5546 }
5547 
5548 
5549 /*
5550  * Print sense data from a tcw.
5551  */
5552 static void dasd_eckd_dump_sense_tcw(struct dasd_device *device,
5553 				 struct dasd_ccw_req *req, struct irb *irb)
5554 {
5555 	char *page;
5556 	int len, sl, sct, residual;
5557 	struct tsb *tsb;
5558 	u8 *sense, *rcq;
5559 
5560 	page = (char *) get_zeroed_page(GFP_ATOMIC);
5561 	if (page == NULL) {
5562 		DBF_DEV_EVENT(DBF_WARNING, device, " %s",
5563 			    "No memory to dump sense data");
5564 		return;
5565 	}
5566 	/* dump the sense data */
5567 	len = sprintf(page, PRINTK_HEADER
5568 		      " I/O status report for device %s:\n",
5569 		      dev_name(&device->cdev->dev));
5570 	len += sprintf(page + len, PRINTK_HEADER
5571 		       " in req: %p CC:%02X FC:%02X AC:%02X SC:%02X DS:%02X "
5572 		       "CS:%02X fcxs:%02X schxs:%02X RC:%d\n",
5573 		       req, scsw_cc(&irb->scsw), scsw_fctl(&irb->scsw),
5574 		       scsw_actl(&irb->scsw), scsw_stctl(&irb->scsw),
5575 		       scsw_dstat(&irb->scsw), scsw_cstat(&irb->scsw),
5576 		       irb->scsw.tm.fcxs,
5577 		       (irb->scsw.tm.ifob << 7) | irb->scsw.tm.sesq,
5578 		       req ? req->intrc : 0);
5579 	len += sprintf(page + len, PRINTK_HEADER
5580 		       " device %s: Failing TCW: %p\n",
5581 		       dev_name(&device->cdev->dev),
5582 		       (void *) (addr_t) irb->scsw.tm.tcw);
5583 
5584 	tsb = NULL;
5585 	sense = NULL;
5586 	if (irb->scsw.tm.tcw && (irb->scsw.tm.fcxs & 0x01))
5587 		tsb = tcw_get_tsb(
5588 			(struct tcw *)(unsigned long)irb->scsw.tm.tcw);
5589 
5590 	if (tsb) {
5591 		len += sprintf(page + len, PRINTK_HEADER
5592 			       " tsb->length %d\n", tsb->length);
5593 		len += sprintf(page + len, PRINTK_HEADER
5594 			       " tsb->flags %x\n", tsb->flags);
5595 		len += sprintf(page + len, PRINTK_HEADER
5596 			       " tsb->dcw_offset %d\n", tsb->dcw_offset);
5597 		len += sprintf(page + len, PRINTK_HEADER
5598 			       " tsb->count %d\n", tsb->count);
5599 		residual = tsb->count - 28;
5600 		len += sprintf(page + len, PRINTK_HEADER
5601 			       " residual %d\n", residual);
5602 
5603 		switch (tsb->flags & 0x07) {
5604 		case 1:	/* tsa_iostat */
5605 			len += sprintf(page + len, PRINTK_HEADER
5606 			       " tsb->tsa.iostat.dev_time %d\n",
5607 				       tsb->tsa.iostat.dev_time);
5608 			len += sprintf(page + len, PRINTK_HEADER
5609 			       " tsb->tsa.iostat.def_time %d\n",
5610 				       tsb->tsa.iostat.def_time);
5611 			len += sprintf(page + len, PRINTK_HEADER
5612 			       " tsb->tsa.iostat.queue_time %d\n",
5613 				       tsb->tsa.iostat.queue_time);
5614 			len += sprintf(page + len, PRINTK_HEADER
5615 			       " tsb->tsa.iostat.dev_busy_time %d\n",
5616 				       tsb->tsa.iostat.dev_busy_time);
5617 			len += sprintf(page + len, PRINTK_HEADER
5618 			       " tsb->tsa.iostat.dev_act_time %d\n",
5619 				       tsb->tsa.iostat.dev_act_time);
5620 			sense = tsb->tsa.iostat.sense;
5621 			break;
5622 		case 2: /* ts_ddpc */
5623 			len += sprintf(page + len, PRINTK_HEADER
5624 			       " tsb->tsa.ddpc.rc %d\n", tsb->tsa.ddpc.rc);
5625 			for (sl = 0; sl < 2; sl++) {
5626 				len += sprintf(page + len, PRINTK_HEADER
5627 					       " tsb->tsa.ddpc.rcq %2d-%2d: ",
5628 					       (8 * sl), ((8 * sl) + 7));
5629 				rcq = tsb->tsa.ddpc.rcq;
5630 				for (sct = 0; sct < 8; sct++) {
5631 					len += sprintf(page + len, " %02x",
5632 						       rcq[8 * sl + sct]);
5633 				}
5634 				len += sprintf(page + len, "\n");
5635 			}
5636 			sense = tsb->tsa.ddpc.sense;
5637 			break;
5638 		case 3: /* tsa_intrg */
5639 			len += sprintf(page + len, PRINTK_HEADER
5640 				      " tsb->tsa.intrg.: not supported yet\n");
5641 			break;
5642 		}
5643 
5644 		if (sense) {
5645 			for (sl = 0; sl < 4; sl++) {
5646 				len += sprintf(page + len, PRINTK_HEADER
5647 					       " Sense(hex) %2d-%2d:",
5648 					       (8 * sl), ((8 * sl) + 7));
5649 				for (sct = 0; sct < 8; sct++) {
5650 					len += sprintf(page + len, " %02x",
5651 						       sense[8 * sl + sct]);
5652 				}
5653 				len += sprintf(page + len, "\n");
5654 			}
5655 
5656 			if (sense[27] & DASD_SENSE_BIT_0) {
5657 				/* 24 Byte Sense Data */
5658 				sprintf(page + len, PRINTK_HEADER
5659 					" 24 Byte: %x MSG %x, "
5660 					"%s MSGb to SYSOP\n",
5661 					sense[7] >> 4, sense[7] & 0x0f,
5662 					sense[1] & 0x10 ? "" : "no");
5663 			} else {
5664 				/* 32 Byte Sense Data */
5665 				sprintf(page + len, PRINTK_HEADER
5666 					" 32 Byte: Format: %x "
5667 					"Exception class %x\n",
5668 					sense[6] & 0x0f, sense[22] >> 4);
5669 			}
5670 		} else {
5671 			sprintf(page + len, PRINTK_HEADER
5672 				" SORRY - NO VALID SENSE AVAILABLE\n");
5673 		}
5674 	} else {
5675 		sprintf(page + len, PRINTK_HEADER
5676 			" SORRY - NO TSB DATA AVAILABLE\n");
5677 	}
5678 	printk(KERN_ERR "%s", page);
5679 	free_page((unsigned long) page);
5680 }
5681 
5682 static void dasd_eckd_dump_sense(struct dasd_device *device,
5683 				 struct dasd_ccw_req *req, struct irb *irb)
5684 {
5685 	u8 *sense = dasd_get_sense(irb);
5686 
5687 	if (scsw_is_tm(&irb->scsw)) {
5688 		/*
5689 		 * In some cases the 'File Protected' or 'Incorrect Length'
5690 		 * error might be expected and log messages shouldn't be written
5691 		 * then. Check if the according suppress bit is set.
5692 		 */
5693 		if (sense && (sense[1] & SNS1_FILE_PROTECTED) &&
5694 		    test_bit(DASD_CQR_SUPPRESS_FP, &req->flags))
5695 			return;
5696 		if (scsw_cstat(&irb->scsw) == 0x40 &&
5697 		    test_bit(DASD_CQR_SUPPRESS_IL, &req->flags))
5698 			return;
5699 
5700 		dasd_eckd_dump_sense_tcw(device, req, irb);
5701 	} else {
5702 		/*
5703 		 * In some cases the 'Command Reject' or 'No Record Found'
5704 		 * error might be expected and log messages shouldn't be
5705 		 * written then. Check if the according suppress bit is set.
5706 		 */
5707 		if (sense && sense[0] & SNS0_CMD_REJECT &&
5708 		    test_bit(DASD_CQR_SUPPRESS_CR, &req->flags))
5709 			return;
5710 
5711 		if (sense && sense[1] & SNS1_NO_REC_FOUND &&
5712 		    test_bit(DASD_CQR_SUPPRESS_NRF, &req->flags))
5713 			return;
5714 
5715 		dasd_eckd_dump_sense_ccw(device, req, irb);
5716 	}
5717 }
5718 
5719 static int dasd_eckd_pm_freeze(struct dasd_device *device)
5720 {
5721 	/*
5722 	 * the device should be disconnected from our LCU structure
5723 	 * on restore we will reconnect it and reread LCU specific
5724 	 * information like PAV support that might have changed
5725 	 */
5726 	dasd_alias_remove_device(device);
5727 	dasd_alias_disconnect_device_from_lcu(device);
5728 
5729 	return 0;
5730 }
5731 
5732 static int dasd_eckd_restore_device(struct dasd_device *device)
5733 {
5734 	struct dasd_eckd_private *private = device->private;
5735 	struct dasd_eckd_characteristics temp_rdc_data;
5736 	int rc;
5737 	struct dasd_uid temp_uid;
5738 	unsigned long flags;
5739 	unsigned long cqr_flags = 0;
5740 
5741 	/* Read Configuration Data */
5742 	rc = dasd_eckd_read_conf(device);
5743 	if (rc) {
5744 		DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
5745 				"Read configuration data failed, rc=%d", rc);
5746 		goto out_err;
5747 	}
5748 
5749 	dasd_eckd_get_uid(device, &temp_uid);
5750 	/* Generate device unique id */
5751 	rc = dasd_eckd_generate_uid(device);
5752 	spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
5753 	if (memcmp(&private->uid, &temp_uid, sizeof(struct dasd_uid)) != 0)
5754 		dev_err(&device->cdev->dev, "The UID of the DASD has "
5755 			"changed\n");
5756 	spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
5757 	if (rc)
5758 		goto out_err;
5759 
5760 	/* register lcu with alias handling, enable PAV if this is a new lcu */
5761 	rc = dasd_alias_make_device_known_to_lcu(device);
5762 	if (rc)
5763 		goto out_err;
5764 
5765 	set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr_flags);
5766 	dasd_eckd_validate_server(device, cqr_flags);
5767 
5768 	/* RE-Read Configuration Data */
5769 	rc = dasd_eckd_read_conf(device);
5770 	if (rc) {
5771 		DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
5772 			"Read configuration data failed, rc=%d", rc);
5773 		goto out_err2;
5774 	}
5775 
5776 	/* Read Feature Codes */
5777 	dasd_eckd_read_features(device);
5778 
5779 	/* Read Volume Information */
5780 	dasd_eckd_read_vol_info(device);
5781 
5782 	/* Read Extent Pool Information */
5783 	dasd_eckd_read_ext_pool_info(device);
5784 
5785 	/* Read Device Characteristics */
5786 	rc = dasd_generic_read_dev_chars(device, DASD_ECKD_MAGIC,
5787 					 &temp_rdc_data, 64);
5788 	if (rc) {
5789 		DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
5790 				"Read device characteristic failed, rc=%d", rc);
5791 		goto out_err2;
5792 	}
5793 	spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
5794 	memcpy(&private->rdc_data, &temp_rdc_data, sizeof(temp_rdc_data));
5795 	spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
5796 
5797 	/* add device to alias management */
5798 	dasd_alias_add_device(device);
5799 
5800 	return 0;
5801 
5802 out_err2:
5803 	dasd_alias_disconnect_device_from_lcu(device);
5804 out_err:
5805 	return -1;
5806 }
5807 
5808 static int dasd_eckd_reload_device(struct dasd_device *device)
5809 {
5810 	struct dasd_eckd_private *private = device->private;
5811 	int rc, old_base;
5812 	char print_uid[60];
5813 	struct dasd_uid uid;
5814 	unsigned long flags;
5815 
5816 	/*
5817 	 * remove device from alias handling to prevent new requests
5818 	 * from being scheduled on the wrong alias device
5819 	 */
5820 	dasd_alias_remove_device(device);
5821 
5822 	spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
5823 	old_base = private->uid.base_unit_addr;
5824 	spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
5825 
5826 	/* Read Configuration Data */
5827 	rc = dasd_eckd_read_conf(device);
5828 	if (rc)
5829 		goto out_err;
5830 
5831 	rc = dasd_eckd_generate_uid(device);
5832 	if (rc)
5833 		goto out_err;
5834 	/*
5835 	 * update unit address configuration and
5836 	 * add device to alias management
5837 	 */
5838 	dasd_alias_update_add_device(device);
5839 
5840 	dasd_eckd_get_uid(device, &uid);
5841 
5842 	if (old_base != uid.base_unit_addr) {
5843 		if (strlen(uid.vduit) > 0)
5844 			snprintf(print_uid, sizeof(print_uid),
5845 				 "%s.%s.%04x.%02x.%s", uid.vendor, uid.serial,
5846 				 uid.ssid, uid.base_unit_addr, uid.vduit);
5847 		else
5848 			snprintf(print_uid, sizeof(print_uid),
5849 				 "%s.%s.%04x.%02x", uid.vendor, uid.serial,
5850 				 uid.ssid, uid.base_unit_addr);
5851 
5852 		dev_info(&device->cdev->dev,
5853 			 "An Alias device was reassigned to a new base device "
5854 			 "with UID: %s\n", print_uid);
5855 	}
5856 	return 0;
5857 
5858 out_err:
5859 	return -1;
5860 }
5861 
5862 static int dasd_eckd_read_message_buffer(struct dasd_device *device,
5863 					 struct dasd_rssd_messages *messages,
5864 					 __u8 lpum)
5865 {
5866 	struct dasd_rssd_messages *message_buf;
5867 	struct dasd_psf_prssd_data *prssdp;
5868 	struct dasd_ccw_req *cqr;
5869 	struct ccw1 *ccw;
5870 	int rc;
5871 
5872 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */	+ 1 /* RSSD */,
5873 				   (sizeof(struct dasd_psf_prssd_data) +
5874 				    sizeof(struct dasd_rssd_messages)),
5875 				   device, NULL);
5876 	if (IS_ERR(cqr)) {
5877 		DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
5878 				"Could not allocate read message buffer request");
5879 		return PTR_ERR(cqr);
5880 	}
5881 
5882 	cqr->lpm = lpum;
5883 retry:
5884 	cqr->startdev = device;
5885 	cqr->memdev = device;
5886 	cqr->block = NULL;
5887 	cqr->expires = 10 * HZ;
5888 	set_bit(DASD_CQR_VERIFY_PATH, &cqr->flags);
5889 	/* dasd_sleep_on_immediatly does not do complex error
5890 	 * recovery so clear erp flag and set retry counter to
5891 	 * do basic erp */
5892 	clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
5893 	cqr->retries = 256;
5894 
5895 	/* Prepare for Read Subsystem Data */
5896 	prssdp = (struct dasd_psf_prssd_data *) cqr->data;
5897 	memset(prssdp, 0, sizeof(struct dasd_psf_prssd_data));
5898 	prssdp->order = PSF_ORDER_PRSSD;
5899 	prssdp->suborder = 0x03;	/* Message Buffer */
5900 	/* all other bytes of prssdp must be zero */
5901 
5902 	ccw = cqr->cpaddr;
5903 	ccw->cmd_code = DASD_ECKD_CCW_PSF;
5904 	ccw->count = sizeof(struct dasd_psf_prssd_data);
5905 	ccw->flags |= CCW_FLAG_CC;
5906 	ccw->flags |= CCW_FLAG_SLI;
5907 	ccw->cda = (__u32)(addr_t) prssdp;
5908 
5909 	/* Read Subsystem Data - message buffer */
5910 	message_buf = (struct dasd_rssd_messages *) (prssdp + 1);
5911 	memset(message_buf, 0, sizeof(struct dasd_rssd_messages));
5912 
5913 	ccw++;
5914 	ccw->cmd_code = DASD_ECKD_CCW_RSSD;
5915 	ccw->count = sizeof(struct dasd_rssd_messages);
5916 	ccw->flags |= CCW_FLAG_SLI;
5917 	ccw->cda = (__u32)(addr_t) message_buf;
5918 
5919 	cqr->buildclk = get_tod_clock();
5920 	cqr->status = DASD_CQR_FILLED;
5921 	rc = dasd_sleep_on_immediatly(cqr);
5922 	if (rc == 0) {
5923 		prssdp = (struct dasd_psf_prssd_data *) cqr->data;
5924 		message_buf = (struct dasd_rssd_messages *)
5925 			(prssdp + 1);
5926 		memcpy(messages, message_buf,
5927 		       sizeof(struct dasd_rssd_messages));
5928 	} else if (cqr->lpm) {
5929 		/*
5930 		 * on z/VM we might not be able to do I/O on the requested path
5931 		 * but instead we get the required information on any path
5932 		 * so retry with open path mask
5933 		 */
5934 		cqr->lpm = 0;
5935 		goto retry;
5936 	} else
5937 		DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
5938 				"Reading messages failed with rc=%d\n"
5939 				, rc);
5940 	dasd_sfree_request(cqr, cqr->memdev);
5941 	return rc;
5942 }
5943 
5944 static int dasd_eckd_query_host_access(struct dasd_device *device,
5945 				       struct dasd_psf_query_host_access *data)
5946 {
5947 	struct dasd_eckd_private *private = device->private;
5948 	struct dasd_psf_query_host_access *host_access;
5949 	struct dasd_psf_prssd_data *prssdp;
5950 	struct dasd_ccw_req *cqr;
5951 	struct ccw1 *ccw;
5952 	int rc;
5953 
5954 	/* not available for HYPER PAV alias devices */
5955 	if (!device->block && private->lcu->pav == HYPER_PAV)
5956 		return -EOPNOTSUPP;
5957 
5958 	/* may not be supported by the storage server */
5959 	if (!(private->features.feature[14] & 0x80))
5960 		return -EOPNOTSUPP;
5961 
5962 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */	+ 1 /* RSSD */,
5963 				   sizeof(struct dasd_psf_prssd_data) + 1,
5964 				   device, NULL);
5965 	if (IS_ERR(cqr)) {
5966 		DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
5967 				"Could not allocate read message buffer request");
5968 		return PTR_ERR(cqr);
5969 	}
5970 	host_access = kzalloc(sizeof(*host_access), GFP_KERNEL | GFP_DMA);
5971 	if (!host_access) {
5972 		dasd_sfree_request(cqr, device);
5973 		DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
5974 				"Could not allocate host_access buffer");
5975 		return -ENOMEM;
5976 	}
5977 	cqr->startdev = device;
5978 	cqr->memdev = device;
5979 	cqr->block = NULL;
5980 	cqr->retries = 256;
5981 	cqr->expires = 10 * HZ;
5982 
5983 	/* Prepare for Read Subsystem Data */
5984 	prssdp = (struct dasd_psf_prssd_data *) cqr->data;
5985 	memset(prssdp, 0, sizeof(struct dasd_psf_prssd_data));
5986 	prssdp->order = PSF_ORDER_PRSSD;
5987 	prssdp->suborder = PSF_SUBORDER_QHA;	/* query host access */
5988 	/* LSS and Volume that will be queried */
5989 	prssdp->lss = private->ned->ID;
5990 	prssdp->volume = private->ned->unit_addr;
5991 	/* all other bytes of prssdp must be zero */
5992 
5993 	ccw = cqr->cpaddr;
5994 	ccw->cmd_code = DASD_ECKD_CCW_PSF;
5995 	ccw->count = sizeof(struct dasd_psf_prssd_data);
5996 	ccw->flags |= CCW_FLAG_CC;
5997 	ccw->flags |= CCW_FLAG_SLI;
5998 	ccw->cda = (__u32)(addr_t) prssdp;
5999 
6000 	/* Read Subsystem Data - query host access */
6001 	ccw++;
6002 	ccw->cmd_code = DASD_ECKD_CCW_RSSD;
6003 	ccw->count = sizeof(struct dasd_psf_query_host_access);
6004 	ccw->flags |= CCW_FLAG_SLI;
6005 	ccw->cda = (__u32)(addr_t) host_access;
6006 
6007 	cqr->buildclk = get_tod_clock();
6008 	cqr->status = DASD_CQR_FILLED;
6009 	/* the command might not be supported, suppress error message */
6010 	__set_bit(DASD_CQR_SUPPRESS_CR, &cqr->flags);
6011 	rc = dasd_sleep_on_interruptible(cqr);
6012 	if (rc == 0) {
6013 		*data = *host_access;
6014 	} else {
6015 		DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
6016 				"Reading host access data failed with rc=%d\n",
6017 				rc);
6018 		rc = -EOPNOTSUPP;
6019 	}
6020 
6021 	dasd_sfree_request(cqr, cqr->memdev);
6022 	kfree(host_access);
6023 	return rc;
6024 }
6025 /*
6026  * return number of grouped devices
6027  */
6028 static int dasd_eckd_host_access_count(struct dasd_device *device)
6029 {
6030 	struct dasd_psf_query_host_access *access;
6031 	struct dasd_ckd_path_group_entry *entry;
6032 	struct dasd_ckd_host_information *info;
6033 	int count = 0;
6034 	int rc, i;
6035 
6036 	access = kzalloc(sizeof(*access), GFP_NOIO);
6037 	if (!access) {
6038 		DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
6039 				"Could not allocate access buffer");
6040 		return -ENOMEM;
6041 	}
6042 	rc = dasd_eckd_query_host_access(device, access);
6043 	if (rc) {
6044 		kfree(access);
6045 		return rc;
6046 	}
6047 
6048 	info = (struct dasd_ckd_host_information *)
6049 		access->host_access_information;
6050 	for (i = 0; i < info->entry_count; i++) {
6051 		entry = (struct dasd_ckd_path_group_entry *)
6052 			(info->entry + i * info->entry_size);
6053 		if (entry->status_flags & DASD_ECKD_PG_GROUPED)
6054 			count++;
6055 	}
6056 
6057 	kfree(access);
6058 	return count;
6059 }
6060 
6061 /*
6062  * write host access information to a sequential file
6063  */
6064 static int dasd_hosts_print(struct dasd_device *device, struct seq_file *m)
6065 {
6066 	struct dasd_psf_query_host_access *access;
6067 	struct dasd_ckd_path_group_entry *entry;
6068 	struct dasd_ckd_host_information *info;
6069 	char sysplex[9] = "";
6070 	int rc, i;
6071 
6072 	access = kzalloc(sizeof(*access), GFP_NOIO);
6073 	if (!access) {
6074 		DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
6075 				"Could not allocate access buffer");
6076 		return -ENOMEM;
6077 	}
6078 	rc = dasd_eckd_query_host_access(device, access);
6079 	if (rc) {
6080 		kfree(access);
6081 		return rc;
6082 	}
6083 
6084 	info = (struct dasd_ckd_host_information *)
6085 		access->host_access_information;
6086 	for (i = 0; i < info->entry_count; i++) {
6087 		entry = (struct dasd_ckd_path_group_entry *)
6088 			(info->entry + i * info->entry_size);
6089 		/* PGID */
6090 		seq_printf(m, "pgid %*phN\n", 11, entry->pgid);
6091 		/* FLAGS */
6092 		seq_printf(m, "status_flags %02x\n", entry->status_flags);
6093 		/* SYSPLEX NAME */
6094 		memcpy(&sysplex, &entry->sysplex_name, sizeof(sysplex) - 1);
6095 		EBCASC(sysplex, sizeof(sysplex));
6096 		seq_printf(m, "sysplex_name %8s\n", sysplex);
6097 		/* SUPPORTED CYLINDER */
6098 		seq_printf(m, "supported_cylinder %d\n", entry->cylinder);
6099 		/* TIMESTAMP */
6100 		seq_printf(m, "timestamp %lu\n", (unsigned long)
6101 			   entry->timestamp);
6102 	}
6103 	kfree(access);
6104 
6105 	return 0;
6106 }
6107 
6108 /*
6109  * Perform Subsystem Function - CUIR response
6110  */
6111 static int
6112 dasd_eckd_psf_cuir_response(struct dasd_device *device, int response,
6113 			    __u32 message_id, __u8 lpum)
6114 {
6115 	struct dasd_psf_cuir_response *psf_cuir;
6116 	int pos = pathmask_to_pos(lpum);
6117 	struct dasd_ccw_req *cqr;
6118 	struct ccw1 *ccw;
6119 	int rc;
6120 
6121 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */ ,
6122 				   sizeof(struct dasd_psf_cuir_response),
6123 				   device, NULL);
6124 
6125 	if (IS_ERR(cqr)) {
6126 		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
6127 			   "Could not allocate PSF-CUIR request");
6128 		return PTR_ERR(cqr);
6129 	}
6130 
6131 	psf_cuir = (struct dasd_psf_cuir_response *)cqr->data;
6132 	psf_cuir->order = PSF_ORDER_CUIR_RESPONSE;
6133 	psf_cuir->cc = response;
6134 	psf_cuir->chpid = device->path[pos].chpid;
6135 	psf_cuir->message_id = message_id;
6136 	psf_cuir->cssid = device->path[pos].cssid;
6137 	psf_cuir->ssid = device->path[pos].ssid;
6138 	ccw = cqr->cpaddr;
6139 	ccw->cmd_code = DASD_ECKD_CCW_PSF;
6140 	ccw->cda = (__u32)(addr_t)psf_cuir;
6141 	ccw->flags = CCW_FLAG_SLI;
6142 	ccw->count = sizeof(struct dasd_psf_cuir_response);
6143 
6144 	cqr->startdev = device;
6145 	cqr->memdev = device;
6146 	cqr->block = NULL;
6147 	cqr->retries = 256;
6148 	cqr->expires = 10*HZ;
6149 	cqr->buildclk = get_tod_clock();
6150 	cqr->status = DASD_CQR_FILLED;
6151 	set_bit(DASD_CQR_VERIFY_PATH, &cqr->flags);
6152 
6153 	rc = dasd_sleep_on(cqr);
6154 
6155 	dasd_sfree_request(cqr, cqr->memdev);
6156 	return rc;
6157 }
6158 
6159 /*
6160  * return configuration data that is referenced by record selector
6161  * if a record selector is specified or per default return the
6162  * conf_data pointer for the path specified by lpum
6163  */
6164 static struct dasd_conf_data *dasd_eckd_get_ref_conf(struct dasd_device *device,
6165 						     __u8 lpum,
6166 						     struct dasd_cuir_message *cuir)
6167 {
6168 	struct dasd_conf_data *conf_data;
6169 	int path, pos;
6170 
6171 	if (cuir->record_selector == 0)
6172 		goto out;
6173 	for (path = 0x80, pos = 0; path; path >>= 1, pos++) {
6174 		conf_data = device->path[pos].conf_data;
6175 		if (conf_data->gneq.record_selector ==
6176 		    cuir->record_selector)
6177 			return conf_data;
6178 	}
6179 out:
6180 	return device->path[pathmask_to_pos(lpum)].conf_data;
6181 }
6182 
6183 /*
6184  * This function determines the scope of a reconfiguration request by
6185  * analysing the path and device selection data provided in the CUIR request.
6186  * Returns a path mask containing CUIR affected paths for the give device.
6187  *
6188  * If the CUIR request does not contain the required information return the
6189  * path mask of the path the attention message for the CUIR request was reveived
6190  * on.
6191  */
6192 static int dasd_eckd_cuir_scope(struct dasd_device *device, __u8 lpum,
6193 				struct dasd_cuir_message *cuir)
6194 {
6195 	struct dasd_conf_data *ref_conf_data;
6196 	unsigned long bitmask = 0, mask = 0;
6197 	struct dasd_conf_data *conf_data;
6198 	unsigned int pos, path;
6199 	char *ref_gneq, *gneq;
6200 	char *ref_ned, *ned;
6201 	int tbcpm = 0;
6202 
6203 	/* if CUIR request does not specify the scope use the path
6204 	   the attention message was presented on */
6205 	if (!cuir->ned_map ||
6206 	    !(cuir->neq_map[0] | cuir->neq_map[1] | cuir->neq_map[2]))
6207 		return lpum;
6208 
6209 	/* get reference conf data */
6210 	ref_conf_data = dasd_eckd_get_ref_conf(device, lpum, cuir);
6211 	/* reference ned is determined by ned_map field */
6212 	pos = 8 - ffs(cuir->ned_map);
6213 	ref_ned = (char *)&ref_conf_data->neds[pos];
6214 	ref_gneq = (char *)&ref_conf_data->gneq;
6215 	/* transfer 24 bit neq_map to mask */
6216 	mask = cuir->neq_map[2];
6217 	mask |= cuir->neq_map[1] << 8;
6218 	mask |= cuir->neq_map[0] << 16;
6219 
6220 	for (path = 0; path < 8; path++) {
6221 		/* initialise data per path */
6222 		bitmask = mask;
6223 		conf_data = device->path[path].conf_data;
6224 		pos = 8 - ffs(cuir->ned_map);
6225 		ned = (char *) &conf_data->neds[pos];
6226 		/* compare reference ned and per path ned */
6227 		if (memcmp(ref_ned, ned, sizeof(*ned)) != 0)
6228 			continue;
6229 		gneq = (char *)&conf_data->gneq;
6230 		/* compare reference gneq and per_path gneq under
6231 		   24 bit mask where mask bit 0 equals byte 7 of
6232 		   the gneq and mask bit 24 equals byte 31 */
6233 		while (bitmask) {
6234 			pos = ffs(bitmask) - 1;
6235 			if (memcmp(&ref_gneq[31 - pos], &gneq[31 - pos], 1)
6236 			    != 0)
6237 				break;
6238 			clear_bit(pos, &bitmask);
6239 		}
6240 		if (bitmask)
6241 			continue;
6242 		/* device and path match the reference values
6243 		   add path to CUIR scope */
6244 		tbcpm |= 0x80 >> path;
6245 	}
6246 	return tbcpm;
6247 }
6248 
6249 static void dasd_eckd_cuir_notify_user(struct dasd_device *device,
6250 				       unsigned long paths, int action)
6251 {
6252 	int pos;
6253 
6254 	while (paths) {
6255 		/* get position of bit in mask */
6256 		pos = 8 - ffs(paths);
6257 		/* get channel path descriptor from this position */
6258 		if (action == CUIR_QUIESCE)
6259 			pr_warn("Service on the storage server caused path %x.%02x to go offline",
6260 				device->path[pos].cssid,
6261 				device->path[pos].chpid);
6262 		else if (action == CUIR_RESUME)
6263 			pr_info("Path %x.%02x is back online after service on the storage server",
6264 				device->path[pos].cssid,
6265 				device->path[pos].chpid);
6266 		clear_bit(7 - pos, &paths);
6267 	}
6268 }
6269 
6270 static int dasd_eckd_cuir_remove_path(struct dasd_device *device, __u8 lpum,
6271 				      struct dasd_cuir_message *cuir)
6272 {
6273 	unsigned long tbcpm;
6274 
6275 	tbcpm = dasd_eckd_cuir_scope(device, lpum, cuir);
6276 	/* nothing to do if path is not in use */
6277 	if (!(dasd_path_get_opm(device) & tbcpm))
6278 		return 0;
6279 	if (!(dasd_path_get_opm(device) & ~tbcpm)) {
6280 		/* no path would be left if the CUIR action is taken
6281 		   return error */
6282 		return -EINVAL;
6283 	}
6284 	/* remove device from operational path mask */
6285 	dasd_path_remove_opm(device, tbcpm);
6286 	dasd_path_add_cuirpm(device, tbcpm);
6287 	return tbcpm;
6288 }
6289 
6290 /*
6291  * walk through all devices and build a path mask to quiesce them
6292  * return an error if the last path to a device would be removed
6293  *
6294  * if only part of the devices are quiesced and an error
6295  * occurs no onlining necessary, the storage server will
6296  * notify the already set offline devices again
6297  */
6298 static int dasd_eckd_cuir_quiesce(struct dasd_device *device, __u8 lpum,
6299 				  struct dasd_cuir_message *cuir)
6300 {
6301 	struct dasd_eckd_private *private = device->private;
6302 	struct alias_pav_group *pavgroup, *tempgroup;
6303 	struct dasd_device *dev, *n;
6304 	unsigned long paths = 0;
6305 	unsigned long flags;
6306 	int tbcpm;
6307 
6308 	/* active devices */
6309 	list_for_each_entry_safe(dev, n, &private->lcu->active_devices,
6310 				 alias_list) {
6311 		spin_lock_irqsave(get_ccwdev_lock(dev->cdev), flags);
6312 		tbcpm = dasd_eckd_cuir_remove_path(dev, lpum, cuir);
6313 		spin_unlock_irqrestore(get_ccwdev_lock(dev->cdev), flags);
6314 		if (tbcpm < 0)
6315 			goto out_err;
6316 		paths |= tbcpm;
6317 	}
6318 	/* inactive devices */
6319 	list_for_each_entry_safe(dev, n, &private->lcu->inactive_devices,
6320 				 alias_list) {
6321 		spin_lock_irqsave(get_ccwdev_lock(dev->cdev), flags);
6322 		tbcpm = dasd_eckd_cuir_remove_path(dev, lpum, cuir);
6323 		spin_unlock_irqrestore(get_ccwdev_lock(dev->cdev), flags);
6324 		if (tbcpm < 0)
6325 			goto out_err;
6326 		paths |= tbcpm;
6327 	}
6328 	/* devices in PAV groups */
6329 	list_for_each_entry_safe(pavgroup, tempgroup,
6330 				 &private->lcu->grouplist, group) {
6331 		list_for_each_entry_safe(dev, n, &pavgroup->baselist,
6332 					 alias_list) {
6333 			spin_lock_irqsave(get_ccwdev_lock(dev->cdev), flags);
6334 			tbcpm = dasd_eckd_cuir_remove_path(dev, lpum, cuir);
6335 			spin_unlock_irqrestore(
6336 				get_ccwdev_lock(dev->cdev), flags);
6337 			if (tbcpm < 0)
6338 				goto out_err;
6339 			paths |= tbcpm;
6340 		}
6341 		list_for_each_entry_safe(dev, n, &pavgroup->aliaslist,
6342 					 alias_list) {
6343 			spin_lock_irqsave(get_ccwdev_lock(dev->cdev), flags);
6344 			tbcpm = dasd_eckd_cuir_remove_path(dev, lpum, cuir);
6345 			spin_unlock_irqrestore(
6346 				get_ccwdev_lock(dev->cdev), flags);
6347 			if (tbcpm < 0)
6348 				goto out_err;
6349 			paths |= tbcpm;
6350 		}
6351 	}
6352 	/* notify user about all paths affected by CUIR action */
6353 	dasd_eckd_cuir_notify_user(device, paths, CUIR_QUIESCE);
6354 	return 0;
6355 out_err:
6356 	return tbcpm;
6357 }
6358 
6359 static int dasd_eckd_cuir_resume(struct dasd_device *device, __u8 lpum,
6360 				 struct dasd_cuir_message *cuir)
6361 {
6362 	struct dasd_eckd_private *private = device->private;
6363 	struct alias_pav_group *pavgroup, *tempgroup;
6364 	struct dasd_device *dev, *n;
6365 	unsigned long paths = 0;
6366 	int tbcpm;
6367 
6368 	/*
6369 	 * the path may have been added through a generic path event before
6370 	 * only trigger path verification if the path is not already in use
6371 	 */
6372 	list_for_each_entry_safe(dev, n,
6373 				 &private->lcu->active_devices,
6374 				 alias_list) {
6375 		tbcpm = dasd_eckd_cuir_scope(dev, lpum, cuir);
6376 		paths |= tbcpm;
6377 		if (!(dasd_path_get_opm(dev) & tbcpm)) {
6378 			dasd_path_add_tbvpm(dev, tbcpm);
6379 			dasd_schedule_device_bh(dev);
6380 		}
6381 	}
6382 	list_for_each_entry_safe(dev, n,
6383 				 &private->lcu->inactive_devices,
6384 				 alias_list) {
6385 		tbcpm = dasd_eckd_cuir_scope(dev, lpum, cuir);
6386 		paths |= tbcpm;
6387 		if (!(dasd_path_get_opm(dev) & tbcpm)) {
6388 			dasd_path_add_tbvpm(dev, tbcpm);
6389 			dasd_schedule_device_bh(dev);
6390 		}
6391 	}
6392 	/* devices in PAV groups */
6393 	list_for_each_entry_safe(pavgroup, tempgroup,
6394 				 &private->lcu->grouplist,
6395 				 group) {
6396 		list_for_each_entry_safe(dev, n,
6397 					 &pavgroup->baselist,
6398 					 alias_list) {
6399 			tbcpm = dasd_eckd_cuir_scope(dev, lpum, cuir);
6400 			paths |= tbcpm;
6401 			if (!(dasd_path_get_opm(dev) & tbcpm)) {
6402 				dasd_path_add_tbvpm(dev, tbcpm);
6403 				dasd_schedule_device_bh(dev);
6404 			}
6405 		}
6406 		list_for_each_entry_safe(dev, n,
6407 					 &pavgroup->aliaslist,
6408 					 alias_list) {
6409 			tbcpm = dasd_eckd_cuir_scope(dev, lpum, cuir);
6410 			paths |= tbcpm;
6411 			if (!(dasd_path_get_opm(dev) & tbcpm)) {
6412 				dasd_path_add_tbvpm(dev, tbcpm);
6413 				dasd_schedule_device_bh(dev);
6414 			}
6415 		}
6416 	}
6417 	/* notify user about all paths affected by CUIR action */
6418 	dasd_eckd_cuir_notify_user(device, paths, CUIR_RESUME);
6419 	return 0;
6420 }
6421 
6422 static void dasd_eckd_handle_cuir(struct dasd_device *device, void *messages,
6423 				 __u8 lpum)
6424 {
6425 	struct dasd_cuir_message *cuir = messages;
6426 	int response;
6427 
6428 	DBF_DEV_EVENT(DBF_WARNING, device,
6429 		      "CUIR request: %016llx %016llx %016llx %08x",
6430 		      ((u64 *)cuir)[0], ((u64 *)cuir)[1], ((u64 *)cuir)[2],
6431 		      ((u32 *)cuir)[3]);
6432 
6433 	if (cuir->code == CUIR_QUIESCE) {
6434 		/* quiesce */
6435 		if (dasd_eckd_cuir_quiesce(device, lpum, cuir))
6436 			response = PSF_CUIR_LAST_PATH;
6437 		else
6438 			response = PSF_CUIR_COMPLETED;
6439 	} else if (cuir->code == CUIR_RESUME) {
6440 		/* resume */
6441 		dasd_eckd_cuir_resume(device, lpum, cuir);
6442 		response = PSF_CUIR_COMPLETED;
6443 	} else
6444 		response = PSF_CUIR_NOT_SUPPORTED;
6445 
6446 	dasd_eckd_psf_cuir_response(device, response,
6447 				    cuir->message_id, lpum);
6448 	DBF_DEV_EVENT(DBF_WARNING, device,
6449 		      "CUIR response: %d on message ID %08x", response,
6450 		      cuir->message_id);
6451 	/* to make sure there is no attention left schedule work again */
6452 	device->discipline->check_attention(device, lpum);
6453 }
6454 
6455 static void dasd_eckd_oos_resume(struct dasd_device *device)
6456 {
6457 	struct dasd_eckd_private *private = device->private;
6458 	struct alias_pav_group *pavgroup, *tempgroup;
6459 	struct dasd_device *dev, *n;
6460 	unsigned long flags;
6461 
6462 	spin_lock_irqsave(&private->lcu->lock, flags);
6463 	list_for_each_entry_safe(dev, n, &private->lcu->active_devices,
6464 				 alias_list) {
6465 		if (dev->stopped & DASD_STOPPED_NOSPC)
6466 			dasd_generic_space_avail(dev);
6467 	}
6468 	list_for_each_entry_safe(dev, n, &private->lcu->inactive_devices,
6469 				 alias_list) {
6470 		if (dev->stopped & DASD_STOPPED_NOSPC)
6471 			dasd_generic_space_avail(dev);
6472 	}
6473 	/* devices in PAV groups */
6474 	list_for_each_entry_safe(pavgroup, tempgroup,
6475 				 &private->lcu->grouplist,
6476 				 group) {
6477 		list_for_each_entry_safe(dev, n, &pavgroup->baselist,
6478 					 alias_list) {
6479 			if (dev->stopped & DASD_STOPPED_NOSPC)
6480 				dasd_generic_space_avail(dev);
6481 		}
6482 		list_for_each_entry_safe(dev, n, &pavgroup->aliaslist,
6483 					 alias_list) {
6484 			if (dev->stopped & DASD_STOPPED_NOSPC)
6485 				dasd_generic_space_avail(dev);
6486 		}
6487 	}
6488 	spin_unlock_irqrestore(&private->lcu->lock, flags);
6489 }
6490 
6491 static void dasd_eckd_handle_oos(struct dasd_device *device, void *messages,
6492 				 __u8 lpum)
6493 {
6494 	struct dasd_oos_message *oos = messages;
6495 
6496 	switch (oos->code) {
6497 	case REPO_WARN:
6498 	case POOL_WARN:
6499 		dev_warn(&device->cdev->dev,
6500 			 "Extent pool usage has reached a critical value\n");
6501 		dasd_eckd_oos_resume(device);
6502 		break;
6503 	case REPO_EXHAUST:
6504 	case POOL_EXHAUST:
6505 		dev_warn(&device->cdev->dev,
6506 			 "Extent pool is exhausted\n");
6507 		break;
6508 	case REPO_RELIEVE:
6509 	case POOL_RELIEVE:
6510 		dev_info(&device->cdev->dev,
6511 			 "Extent pool physical space constraint has been relieved\n");
6512 		break;
6513 	}
6514 
6515 	/* In any case, update related data */
6516 	dasd_eckd_read_ext_pool_info(device);
6517 
6518 	/* to make sure there is no attention left schedule work again */
6519 	device->discipline->check_attention(device, lpum);
6520 }
6521 
6522 static void dasd_eckd_check_attention_work(struct work_struct *work)
6523 {
6524 	struct check_attention_work_data *data;
6525 	struct dasd_rssd_messages *messages;
6526 	struct dasd_device *device;
6527 	int rc;
6528 
6529 	data = container_of(work, struct check_attention_work_data, worker);
6530 	device = data->device;
6531 	messages = kzalloc(sizeof(*messages), GFP_KERNEL);
6532 	if (!messages) {
6533 		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
6534 			      "Could not allocate attention message buffer");
6535 		goto out;
6536 	}
6537 	rc = dasd_eckd_read_message_buffer(device, messages, data->lpum);
6538 	if (rc)
6539 		goto out;
6540 
6541 	if (messages->length == ATTENTION_LENGTH_CUIR &&
6542 	    messages->format == ATTENTION_FORMAT_CUIR)
6543 		dasd_eckd_handle_cuir(device, messages, data->lpum);
6544 	if (messages->length == ATTENTION_LENGTH_OOS &&
6545 	    messages->format == ATTENTION_FORMAT_OOS)
6546 		dasd_eckd_handle_oos(device, messages, data->lpum);
6547 
6548 out:
6549 	dasd_put_device(device);
6550 	kfree(messages);
6551 	kfree(data);
6552 }
6553 
6554 static int dasd_eckd_check_attention(struct dasd_device *device, __u8 lpum)
6555 {
6556 	struct check_attention_work_data *data;
6557 
6558 	data = kzalloc(sizeof(*data), GFP_ATOMIC);
6559 	if (!data)
6560 		return -ENOMEM;
6561 	INIT_WORK(&data->worker, dasd_eckd_check_attention_work);
6562 	dasd_get_device(device);
6563 	data->device = device;
6564 	data->lpum = lpum;
6565 	schedule_work(&data->worker);
6566 	return 0;
6567 }
6568 
6569 static int dasd_eckd_disable_hpf_path(struct dasd_device *device, __u8 lpum)
6570 {
6571 	if (~lpum & dasd_path_get_opm(device)) {
6572 		dasd_path_add_nohpfpm(device, lpum);
6573 		dasd_path_remove_opm(device, lpum);
6574 		dev_err(&device->cdev->dev,
6575 			"Channel path %02X lost HPF functionality and is disabled\n",
6576 			lpum);
6577 		return 1;
6578 	}
6579 	return 0;
6580 }
6581 
6582 static void dasd_eckd_disable_hpf_device(struct dasd_device *device)
6583 {
6584 	struct dasd_eckd_private *private = device->private;
6585 
6586 	dev_err(&device->cdev->dev,
6587 		"High Performance FICON disabled\n");
6588 	private->fcx_max_data = 0;
6589 }
6590 
6591 static int dasd_eckd_hpf_enabled(struct dasd_device *device)
6592 {
6593 	struct dasd_eckd_private *private = device->private;
6594 
6595 	return private->fcx_max_data ? 1 : 0;
6596 }
6597 
6598 static void dasd_eckd_handle_hpf_error(struct dasd_device *device,
6599 				       struct irb *irb)
6600 {
6601 	struct dasd_eckd_private *private = device->private;
6602 
6603 	if (!private->fcx_max_data) {
6604 		/* sanity check for no HPF, the error makes no sense */
6605 		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
6606 			      "Trying to disable HPF for a non HPF device");
6607 		return;
6608 	}
6609 	if (irb->scsw.tm.sesq == SCSW_SESQ_DEV_NOFCX) {
6610 		dasd_eckd_disable_hpf_device(device);
6611 	} else if (irb->scsw.tm.sesq == SCSW_SESQ_PATH_NOFCX) {
6612 		if (dasd_eckd_disable_hpf_path(device, irb->esw.esw1.lpum))
6613 			return;
6614 		dasd_eckd_disable_hpf_device(device);
6615 		dasd_path_set_tbvpm(device,
6616 				  dasd_path_get_hpfpm(device));
6617 	}
6618 	/*
6619 	 * prevent that any new I/O ist started on the device and schedule a
6620 	 * requeue of existing requests
6621 	 */
6622 	dasd_device_set_stop_bits(device, DASD_STOPPED_NOT_ACC);
6623 	dasd_schedule_requeue(device);
6624 }
6625 
6626 /*
6627  * Initialize block layer request queue.
6628  */
6629 static void dasd_eckd_setup_blk_queue(struct dasd_block *block)
6630 {
6631 	unsigned int logical_block_size = block->bp_block;
6632 	struct request_queue *q = block->request_queue;
6633 	struct dasd_device *device = block->base;
6634 	int max;
6635 
6636 	if (device->features & DASD_FEATURE_USERAW) {
6637 		/*
6638 		 * the max_blocks value for raw_track access is 256
6639 		 * it is higher than the native ECKD value because we
6640 		 * only need one ccw per track
6641 		 * so the max_hw_sectors are
6642 		 * 2048 x 512B = 1024kB = 16 tracks
6643 		 */
6644 		max = DASD_ECKD_MAX_BLOCKS_RAW << block->s2b_shift;
6645 	} else {
6646 		max = DASD_ECKD_MAX_BLOCKS << block->s2b_shift;
6647 	}
6648 	blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
6649 	q->limits.max_dev_sectors = max;
6650 	blk_queue_logical_block_size(q, logical_block_size);
6651 	blk_queue_max_hw_sectors(q, max);
6652 	blk_queue_max_segments(q, USHRT_MAX);
6653 	/* With page sized segments each segment can be translated into one idaw/tidaw */
6654 	blk_queue_max_segment_size(q, PAGE_SIZE);
6655 	blk_queue_segment_boundary(q, PAGE_SIZE - 1);
6656 }
6657 
6658 static struct ccw_driver dasd_eckd_driver = {
6659 	.driver = {
6660 		.name	= "dasd-eckd",
6661 		.owner	= THIS_MODULE,
6662 	},
6663 	.ids	     = dasd_eckd_ids,
6664 	.probe	     = dasd_eckd_probe,
6665 	.remove      = dasd_generic_remove,
6666 	.set_offline = dasd_generic_set_offline,
6667 	.set_online  = dasd_eckd_set_online,
6668 	.notify      = dasd_generic_notify,
6669 	.path_event  = dasd_generic_path_event,
6670 	.shutdown    = dasd_generic_shutdown,
6671 	.freeze      = dasd_generic_pm_freeze,
6672 	.thaw	     = dasd_generic_restore_device,
6673 	.restore     = dasd_generic_restore_device,
6674 	.uc_handler  = dasd_generic_uc_handler,
6675 	.int_class   = IRQIO_DAS,
6676 };
6677 
6678 static struct dasd_discipline dasd_eckd_discipline = {
6679 	.owner = THIS_MODULE,
6680 	.name = "ECKD",
6681 	.ebcname = "ECKD",
6682 	.check_device = dasd_eckd_check_characteristics,
6683 	.uncheck_device = dasd_eckd_uncheck_device,
6684 	.do_analysis = dasd_eckd_do_analysis,
6685 	.verify_path = dasd_eckd_verify_path,
6686 	.basic_to_ready = dasd_eckd_basic_to_ready,
6687 	.online_to_ready = dasd_eckd_online_to_ready,
6688 	.basic_to_known = dasd_eckd_basic_to_known,
6689 	.setup_blk_queue = dasd_eckd_setup_blk_queue,
6690 	.fill_geometry = dasd_eckd_fill_geometry,
6691 	.start_IO = dasd_start_IO,
6692 	.term_IO = dasd_term_IO,
6693 	.handle_terminated_request = dasd_eckd_handle_terminated_request,
6694 	.format_device = dasd_eckd_format_device,
6695 	.check_device_format = dasd_eckd_check_device_format,
6696 	.erp_action = dasd_eckd_erp_action,
6697 	.erp_postaction = dasd_eckd_erp_postaction,
6698 	.check_for_device_change = dasd_eckd_check_for_device_change,
6699 	.build_cp = dasd_eckd_build_alias_cp,
6700 	.free_cp = dasd_eckd_free_alias_cp,
6701 	.dump_sense = dasd_eckd_dump_sense,
6702 	.dump_sense_dbf = dasd_eckd_dump_sense_dbf,
6703 	.fill_info = dasd_eckd_fill_info,
6704 	.ioctl = dasd_eckd_ioctl,
6705 	.freeze = dasd_eckd_pm_freeze,
6706 	.restore = dasd_eckd_restore_device,
6707 	.reload = dasd_eckd_reload_device,
6708 	.get_uid = dasd_eckd_get_uid,
6709 	.kick_validate = dasd_eckd_kick_validate_server,
6710 	.check_attention = dasd_eckd_check_attention,
6711 	.host_access_count = dasd_eckd_host_access_count,
6712 	.hosts_print = dasd_hosts_print,
6713 	.handle_hpf_error = dasd_eckd_handle_hpf_error,
6714 	.disable_hpf = dasd_eckd_disable_hpf_device,
6715 	.hpf_enabled = dasd_eckd_hpf_enabled,
6716 	.reset_path = dasd_eckd_reset_path,
6717 	.is_ese = dasd_eckd_is_ese,
6718 	.space_allocated = dasd_eckd_space_allocated,
6719 	.space_configured = dasd_eckd_space_configured,
6720 	.logical_capacity = dasd_eckd_logical_capacity,
6721 	.release_space = dasd_eckd_release_space,
6722 	.ext_pool_id = dasd_eckd_ext_pool_id,
6723 	.ext_size = dasd_eckd_ext_size,
6724 	.ext_pool_cap_at_warnlevel = dasd_eckd_ext_pool_cap_at_warnlevel,
6725 	.ext_pool_warn_thrshld = dasd_eckd_ext_pool_warn_thrshld,
6726 	.ext_pool_oos = dasd_eckd_ext_pool_oos,
6727 	.ext_pool_exhaust = dasd_eckd_ext_pool_exhaust,
6728 	.ese_format = dasd_eckd_ese_format,
6729 	.ese_read = dasd_eckd_ese_read,
6730 };
6731 
6732 static int __init
6733 dasd_eckd_init(void)
6734 {
6735 	int ret;
6736 
6737 	ASCEBC(dasd_eckd_discipline.ebcname, 4);
6738 	dasd_reserve_req = kmalloc(sizeof(*dasd_reserve_req),
6739 				   GFP_KERNEL | GFP_DMA);
6740 	if (!dasd_reserve_req)
6741 		return -ENOMEM;
6742 	dasd_vol_info_req = kmalloc(sizeof(*dasd_vol_info_req),
6743 				    GFP_KERNEL | GFP_DMA);
6744 	if (!dasd_vol_info_req)
6745 		return -ENOMEM;
6746 	path_verification_worker = kmalloc(sizeof(*path_verification_worker),
6747 				   GFP_KERNEL | GFP_DMA);
6748 	if (!path_verification_worker) {
6749 		kfree(dasd_reserve_req);
6750 		kfree(dasd_vol_info_req);
6751 		return -ENOMEM;
6752 	}
6753 	rawpadpage = (void *)__get_free_page(GFP_KERNEL);
6754 	if (!rawpadpage) {
6755 		kfree(path_verification_worker);
6756 		kfree(dasd_reserve_req);
6757 		kfree(dasd_vol_info_req);
6758 		return -ENOMEM;
6759 	}
6760 	ret = ccw_driver_register(&dasd_eckd_driver);
6761 	if (!ret)
6762 		wait_for_device_probe();
6763 	else {
6764 		kfree(path_verification_worker);
6765 		kfree(dasd_reserve_req);
6766 		kfree(dasd_vol_info_req);
6767 		free_page((unsigned long)rawpadpage);
6768 	}
6769 	return ret;
6770 }
6771 
6772 static void __exit
6773 dasd_eckd_cleanup(void)
6774 {
6775 	ccw_driver_unregister(&dasd_eckd_driver);
6776 	kfree(path_verification_worker);
6777 	kfree(dasd_reserve_req);
6778 	free_page((unsigned long)rawpadpage);
6779 }
6780 
6781 module_init(dasd_eckd_init);
6782 module_exit(dasd_eckd_cleanup);
6783