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