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