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