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