xref: /openbmc/linux/drivers/s390/block/dasd_eckd.c (revision 6774def6)
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 	schedule_work(&device->kick_validate);
1632 }
1633 
1634 static u32 get_fcx_max_data(struct dasd_device *device)
1635 {
1636 #if defined(CONFIG_64BIT)
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 #else
1661 	return 0;
1662 #endif
1663 }
1664 
1665 /*
1666  * Check device characteristics.
1667  * If the device is accessible using ECKD discipline, the device is enabled.
1668  */
1669 static int
1670 dasd_eckd_check_characteristics(struct dasd_device *device)
1671 {
1672 	struct dasd_eckd_private *private;
1673 	struct dasd_block *block;
1674 	struct dasd_uid temp_uid;
1675 	int rc, i;
1676 	int readonly;
1677 	unsigned long value;
1678 
1679 	/* setup work queue for validate server*/
1680 	INIT_WORK(&device->kick_validate, dasd_eckd_do_validate_server);
1681 
1682 	if (!ccw_device_is_pathgroup(device->cdev)) {
1683 		dev_warn(&device->cdev->dev,
1684 			 "A channel path group could not be established\n");
1685 		return -EIO;
1686 	}
1687 	if (!ccw_device_is_multipath(device->cdev)) {
1688 		dev_info(&device->cdev->dev,
1689 			 "The DASD is not operating in multipath mode\n");
1690 	}
1691 	private = (struct dasd_eckd_private *) device->private;
1692 	if (!private) {
1693 		private = kzalloc(sizeof(*private), GFP_KERNEL | GFP_DMA);
1694 		if (!private) {
1695 			dev_warn(&device->cdev->dev,
1696 				 "Allocating memory for private DASD data "
1697 				 "failed\n");
1698 			return -ENOMEM;
1699 		}
1700 		device->private = (void *) private;
1701 	} else {
1702 		memset(private, 0, sizeof(*private));
1703 	}
1704 	/* Invalidate status of initial analysis. */
1705 	private->init_cqr_status = -1;
1706 	/* Set default cache operations. */
1707 	private->attrib.operation = DASD_NORMAL_CACHE;
1708 	private->attrib.nr_cyl = 0;
1709 
1710 	/* Read Configuration Data */
1711 	rc = dasd_eckd_read_conf(device);
1712 	if (rc)
1713 		goto out_err1;
1714 
1715 	/* set default timeout */
1716 	device->default_expires = DASD_EXPIRES;
1717 	/* set default retry count */
1718 	device->default_retries = DASD_RETRIES;
1719 
1720 	if (private->gneq) {
1721 		value = 1;
1722 		for (i = 0; i < private->gneq->timeout.value; i++)
1723 			value = 10 * value;
1724 		value = value * private->gneq->timeout.number;
1725 		/* do not accept useless values */
1726 		if (value != 0 && value <= DASD_EXPIRES_MAX)
1727 			device->default_expires = value;
1728 	}
1729 
1730 	dasd_eckd_get_uid(device, &temp_uid);
1731 	if (temp_uid.type == UA_BASE_DEVICE) {
1732 		block = dasd_alloc_block();
1733 		if (IS_ERR(block)) {
1734 			DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
1735 					"could not allocate dasd "
1736 					"block structure");
1737 			rc = PTR_ERR(block);
1738 			goto out_err1;
1739 		}
1740 		device->block = block;
1741 		block->base = device;
1742 	}
1743 
1744 	/* register lcu with alias handling, enable PAV */
1745 	rc = dasd_alias_make_device_known_to_lcu(device);
1746 	if (rc)
1747 		goto out_err2;
1748 
1749 	dasd_eckd_validate_server(device, 0);
1750 
1751 	/* device may report different configuration data after LCU setup */
1752 	rc = dasd_eckd_read_conf(device);
1753 	if (rc)
1754 		goto out_err3;
1755 
1756 	/* Read Feature Codes */
1757 	dasd_eckd_read_features(device);
1758 
1759 	/* Read Device Characteristics */
1760 	rc = dasd_generic_read_dev_chars(device, DASD_ECKD_MAGIC,
1761 					 &private->rdc_data, 64);
1762 	if (rc) {
1763 		DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
1764 				"Read device characteristic failed, rc=%d", rc);
1765 		goto out_err3;
1766 	}
1767 
1768 	if ((device->features & DASD_FEATURE_USERAW) &&
1769 	    !(private->rdc_data.facilities.RT_in_LR)) {
1770 		dev_err(&device->cdev->dev, "The storage server does not "
1771 			"support raw-track access\n");
1772 		rc = -EINVAL;
1773 		goto out_err3;
1774 	}
1775 
1776 	/* find the valid cylinder size */
1777 	if (private->rdc_data.no_cyl == LV_COMPAT_CYL &&
1778 	    private->rdc_data.long_no_cyl)
1779 		private->real_cyl = private->rdc_data.long_no_cyl;
1780 	else
1781 		private->real_cyl = private->rdc_data.no_cyl;
1782 
1783 	private->fcx_max_data = get_fcx_max_data(device);
1784 
1785 	readonly = dasd_device_is_ro(device);
1786 	if (readonly)
1787 		set_bit(DASD_FLAG_DEVICE_RO, &device->flags);
1788 
1789 	dev_info(&device->cdev->dev, "New DASD %04X/%02X (CU %04X/%02X) "
1790 		 "with %d cylinders, %d heads, %d sectors%s\n",
1791 		 private->rdc_data.dev_type,
1792 		 private->rdc_data.dev_model,
1793 		 private->rdc_data.cu_type,
1794 		 private->rdc_data.cu_model.model,
1795 		 private->real_cyl,
1796 		 private->rdc_data.trk_per_cyl,
1797 		 private->rdc_data.sec_per_trk,
1798 		 readonly ? ", read-only device" : "");
1799 	return 0;
1800 
1801 out_err3:
1802 	dasd_alias_disconnect_device_from_lcu(device);
1803 out_err2:
1804 	dasd_free_block(device->block);
1805 	device->block = NULL;
1806 out_err1:
1807 	kfree(private->conf_data);
1808 	kfree(device->private);
1809 	device->private = NULL;
1810 	return rc;
1811 }
1812 
1813 static void dasd_eckd_uncheck_device(struct dasd_device *device)
1814 {
1815 	struct dasd_eckd_private *private;
1816 
1817 	private = (struct dasd_eckd_private *) device->private;
1818 	dasd_alias_disconnect_device_from_lcu(device);
1819 	private->ned = NULL;
1820 	private->sneq = NULL;
1821 	private->vdsneq = NULL;
1822 	private->gneq = NULL;
1823 	private->conf_len = 0;
1824 	kfree(private->conf_data);
1825 	private->conf_data = NULL;
1826 }
1827 
1828 static struct dasd_ccw_req *
1829 dasd_eckd_analysis_ccw(struct dasd_device *device)
1830 {
1831 	struct dasd_eckd_private *private;
1832 	struct eckd_count *count_data;
1833 	struct LO_eckd_data *LO_data;
1834 	struct dasd_ccw_req *cqr;
1835 	struct ccw1 *ccw;
1836 	int cplength, datasize;
1837 	int i;
1838 
1839 	private = (struct dasd_eckd_private *) device->private;
1840 
1841 	cplength = 8;
1842 	datasize = sizeof(struct DE_eckd_data) + 2*sizeof(struct LO_eckd_data);
1843 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength, datasize, device);
1844 	if (IS_ERR(cqr))
1845 		return cqr;
1846 	ccw = cqr->cpaddr;
1847 	/* Define extent for the first 3 tracks. */
1848 	define_extent(ccw++, cqr->data, 0, 2,
1849 		      DASD_ECKD_CCW_READ_COUNT, device);
1850 	LO_data = cqr->data + sizeof(struct DE_eckd_data);
1851 	/* Locate record for the first 4 records on track 0. */
1852 	ccw[-1].flags |= CCW_FLAG_CC;
1853 	locate_record(ccw++, LO_data++, 0, 0, 4,
1854 		      DASD_ECKD_CCW_READ_COUNT, device, 0);
1855 
1856 	count_data = private->count_area;
1857 	for (i = 0; i < 4; i++) {
1858 		ccw[-1].flags |= CCW_FLAG_CC;
1859 		ccw->cmd_code = DASD_ECKD_CCW_READ_COUNT;
1860 		ccw->flags = 0;
1861 		ccw->count = 8;
1862 		ccw->cda = (__u32)(addr_t) count_data;
1863 		ccw++;
1864 		count_data++;
1865 	}
1866 
1867 	/* Locate record for the first record on track 2. */
1868 	ccw[-1].flags |= CCW_FLAG_CC;
1869 	locate_record(ccw++, LO_data++, 2, 0, 1,
1870 		      DASD_ECKD_CCW_READ_COUNT, device, 0);
1871 	/* Read count ccw. */
1872 	ccw[-1].flags |= CCW_FLAG_CC;
1873 	ccw->cmd_code = DASD_ECKD_CCW_READ_COUNT;
1874 	ccw->flags = 0;
1875 	ccw->count = 8;
1876 	ccw->cda = (__u32)(addr_t) count_data;
1877 
1878 	cqr->block = NULL;
1879 	cqr->startdev = device;
1880 	cqr->memdev = device;
1881 	cqr->retries = 255;
1882 	cqr->buildclk = get_tod_clock();
1883 	cqr->status = DASD_CQR_FILLED;
1884 	return cqr;
1885 }
1886 
1887 /* differentiate between 'no record found' and any other error */
1888 static int dasd_eckd_analysis_evaluation(struct dasd_ccw_req *init_cqr)
1889 {
1890 	char *sense;
1891 	if (init_cqr->status == DASD_CQR_DONE)
1892 		return INIT_CQR_OK;
1893 	else if (init_cqr->status == DASD_CQR_NEED_ERP ||
1894 		 init_cqr->status == DASD_CQR_FAILED) {
1895 		sense = dasd_get_sense(&init_cqr->irb);
1896 		if (sense && (sense[1] & SNS1_NO_REC_FOUND))
1897 			return INIT_CQR_UNFORMATTED;
1898 		else
1899 			return INIT_CQR_ERROR;
1900 	} else
1901 		return INIT_CQR_ERROR;
1902 }
1903 
1904 /*
1905  * This is the callback function for the init_analysis cqr. It saves
1906  * the status of the initial analysis ccw before it frees it and kicks
1907  * the device to continue the startup sequence. This will call
1908  * dasd_eckd_do_analysis again (if the devices has not been marked
1909  * for deletion in the meantime).
1910  */
1911 static void dasd_eckd_analysis_callback(struct dasd_ccw_req *init_cqr,
1912 					void *data)
1913 {
1914 	struct dasd_eckd_private *private;
1915 	struct dasd_device *device;
1916 
1917 	device = init_cqr->startdev;
1918 	private = (struct dasd_eckd_private *) device->private;
1919 	private->init_cqr_status = dasd_eckd_analysis_evaluation(init_cqr);
1920 	dasd_sfree_request(init_cqr, device);
1921 	dasd_kick_device(device);
1922 }
1923 
1924 static int dasd_eckd_start_analysis(struct dasd_block *block)
1925 {
1926 	struct dasd_ccw_req *init_cqr;
1927 
1928 	init_cqr = dasd_eckd_analysis_ccw(block->base);
1929 	if (IS_ERR(init_cqr))
1930 		return PTR_ERR(init_cqr);
1931 	init_cqr->callback = dasd_eckd_analysis_callback;
1932 	init_cqr->callback_data = NULL;
1933 	init_cqr->expires = 5*HZ;
1934 	/* first try without ERP, so we can later handle unformatted
1935 	 * devices as special case
1936 	 */
1937 	clear_bit(DASD_CQR_FLAGS_USE_ERP, &init_cqr->flags);
1938 	init_cqr->retries = 0;
1939 	dasd_add_request_head(init_cqr);
1940 	return -EAGAIN;
1941 }
1942 
1943 static int dasd_eckd_end_analysis(struct dasd_block *block)
1944 {
1945 	struct dasd_device *device;
1946 	struct dasd_eckd_private *private;
1947 	struct eckd_count *count_area;
1948 	unsigned int sb, blk_per_trk;
1949 	int status, i;
1950 	struct dasd_ccw_req *init_cqr;
1951 
1952 	device = block->base;
1953 	private = (struct dasd_eckd_private *) device->private;
1954 	status = private->init_cqr_status;
1955 	private->init_cqr_status = -1;
1956 	if (status == INIT_CQR_ERROR) {
1957 		/* try again, this time with full ERP */
1958 		init_cqr = dasd_eckd_analysis_ccw(device);
1959 		dasd_sleep_on(init_cqr);
1960 		status = dasd_eckd_analysis_evaluation(init_cqr);
1961 		dasd_sfree_request(init_cqr, device);
1962 	}
1963 
1964 	if (device->features & DASD_FEATURE_USERAW) {
1965 		block->bp_block = DASD_RAW_BLOCKSIZE;
1966 		blk_per_trk = DASD_RAW_BLOCK_PER_TRACK;
1967 		block->s2b_shift = 3;
1968 		goto raw;
1969 	}
1970 
1971 	if (status == INIT_CQR_UNFORMATTED) {
1972 		dev_warn(&device->cdev->dev, "The DASD is not formatted\n");
1973 		return -EMEDIUMTYPE;
1974 	} else if (status == INIT_CQR_ERROR) {
1975 		dev_err(&device->cdev->dev,
1976 			"Detecting the DASD disk layout failed because "
1977 			"of an I/O error\n");
1978 		return -EIO;
1979 	}
1980 
1981 	private->uses_cdl = 1;
1982 	/* Check Track 0 for Compatible Disk Layout */
1983 	count_area = NULL;
1984 	for (i = 0; i < 3; i++) {
1985 		if (private->count_area[i].kl != 4 ||
1986 		    private->count_area[i].dl != dasd_eckd_cdl_reclen(i) - 4 ||
1987 		    private->count_area[i].cyl != 0 ||
1988 		    private->count_area[i].head != count_area_head[i] ||
1989 		    private->count_area[i].record != count_area_rec[i]) {
1990 			private->uses_cdl = 0;
1991 			break;
1992 		}
1993 	}
1994 	if (i == 3)
1995 		count_area = &private->count_area[4];
1996 
1997 	if (private->uses_cdl == 0) {
1998 		for (i = 0; i < 5; i++) {
1999 			if ((private->count_area[i].kl != 0) ||
2000 			    (private->count_area[i].dl !=
2001 			     private->count_area[0].dl) ||
2002 			    private->count_area[i].cyl !=  0 ||
2003 			    private->count_area[i].head != count_area_head[i] ||
2004 			    private->count_area[i].record != count_area_rec[i])
2005 				break;
2006 		}
2007 		if (i == 5)
2008 			count_area = &private->count_area[0];
2009 	} else {
2010 		if (private->count_area[3].record == 1)
2011 			dev_warn(&device->cdev->dev,
2012 				 "Track 0 has no records following the VTOC\n");
2013 	}
2014 
2015 	if (count_area != NULL && count_area->kl == 0) {
2016 		/* we found notthing violating our disk layout */
2017 		if (dasd_check_blocksize(count_area->dl) == 0)
2018 			block->bp_block = count_area->dl;
2019 	}
2020 	if (block->bp_block == 0) {
2021 		dev_warn(&device->cdev->dev,
2022 			 "The disk layout of the DASD is not supported\n");
2023 		return -EMEDIUMTYPE;
2024 	}
2025 	block->s2b_shift = 0;	/* bits to shift 512 to get a block */
2026 	for (sb = 512; sb < block->bp_block; sb = sb << 1)
2027 		block->s2b_shift++;
2028 
2029 	blk_per_trk = recs_per_track(&private->rdc_data, 0, block->bp_block);
2030 
2031 raw:
2032 	block->blocks = (private->real_cyl *
2033 			  private->rdc_data.trk_per_cyl *
2034 			  blk_per_trk);
2035 
2036 	dev_info(&device->cdev->dev,
2037 		 "DASD with %d KB/block, %d KB total size, %d KB/track, "
2038 		 "%s\n", (block->bp_block >> 10),
2039 		 ((private->real_cyl *
2040 		   private->rdc_data.trk_per_cyl *
2041 		   blk_per_trk * (block->bp_block >> 9)) >> 1),
2042 		 ((blk_per_trk * block->bp_block) >> 10),
2043 		 private->uses_cdl ?
2044 		 "compatible disk layout" : "linux disk layout");
2045 
2046 	return 0;
2047 }
2048 
2049 static int dasd_eckd_do_analysis(struct dasd_block *block)
2050 {
2051 	struct dasd_eckd_private *private;
2052 
2053 	private = (struct dasd_eckd_private *) block->base->private;
2054 	if (private->init_cqr_status < 0)
2055 		return dasd_eckd_start_analysis(block);
2056 	else
2057 		return dasd_eckd_end_analysis(block);
2058 }
2059 
2060 static int dasd_eckd_basic_to_ready(struct dasd_device *device)
2061 {
2062 	return dasd_alias_add_device(device);
2063 };
2064 
2065 static int dasd_eckd_online_to_ready(struct dasd_device *device)
2066 {
2067 	cancel_work_sync(&device->reload_device);
2068 	cancel_work_sync(&device->kick_validate);
2069 	return 0;
2070 };
2071 
2072 static int dasd_eckd_basic_to_known(struct dasd_device *device)
2073 {
2074 	return dasd_alias_remove_device(device);
2075 };
2076 
2077 static int
2078 dasd_eckd_fill_geometry(struct dasd_block *block, struct hd_geometry *geo)
2079 {
2080 	struct dasd_eckd_private *private;
2081 
2082 	private = (struct dasd_eckd_private *) block->base->private;
2083 	if (dasd_check_blocksize(block->bp_block) == 0) {
2084 		geo->sectors = recs_per_track(&private->rdc_data,
2085 					      0, block->bp_block);
2086 	}
2087 	geo->cylinders = private->rdc_data.no_cyl;
2088 	geo->heads = private->rdc_data.trk_per_cyl;
2089 	return 0;
2090 }
2091 
2092 static struct dasd_ccw_req *
2093 dasd_eckd_build_format(struct dasd_device *base,
2094 		       struct format_data_t *fdata,
2095 		       int enable_pav)
2096 {
2097 	struct dasd_eckd_private *base_priv;
2098 	struct dasd_eckd_private *start_priv;
2099 	struct dasd_device *startdev = NULL;
2100 	struct dasd_ccw_req *fcp;
2101 	struct eckd_count *ect;
2102 	struct ch_t address;
2103 	struct ccw1 *ccw;
2104 	void *data;
2105 	int rpt;
2106 	int cplength, datasize;
2107 	int i, j;
2108 	int intensity = 0;
2109 	int r0_perm;
2110 	int nr_tracks;
2111 	int use_prefix;
2112 
2113 	if (enable_pav)
2114 		startdev = dasd_alias_get_start_dev(base);
2115 
2116 	if (!startdev)
2117 		startdev = base;
2118 
2119 	start_priv = (struct dasd_eckd_private *) startdev->private;
2120 	base_priv = (struct dasd_eckd_private *) base->private;
2121 
2122 	rpt = recs_per_track(&base_priv->rdc_data, 0, fdata->blksize);
2123 
2124 	nr_tracks = fdata->stop_unit - fdata->start_unit + 1;
2125 
2126 	/*
2127 	 * fdata->intensity is a bit string that tells us what to do:
2128 	 *   Bit 0: write record zero
2129 	 *   Bit 1: write home address, currently not supported
2130 	 *   Bit 2: invalidate tracks
2131 	 *   Bit 3: use OS/390 compatible disk layout (cdl)
2132 	 *   Bit 4: do not allow storage subsystem to modify record zero
2133 	 * Only some bit combinations do make sense.
2134 	 */
2135 	if (fdata->intensity & 0x10) {
2136 		r0_perm = 0;
2137 		intensity = fdata->intensity & ~0x10;
2138 	} else {
2139 		r0_perm = 1;
2140 		intensity = fdata->intensity;
2141 	}
2142 
2143 	use_prefix = base_priv->features.feature[8] & 0x01;
2144 
2145 	switch (intensity) {
2146 	case 0x00:	/* Normal format */
2147 	case 0x08:	/* Normal format, use cdl. */
2148 		cplength = 2 + (rpt*nr_tracks);
2149 		if (use_prefix)
2150 			datasize = sizeof(struct PFX_eckd_data) +
2151 				sizeof(struct LO_eckd_data) +
2152 				rpt * nr_tracks * sizeof(struct eckd_count);
2153 		else
2154 			datasize = sizeof(struct DE_eckd_data) +
2155 				sizeof(struct LO_eckd_data) +
2156 				rpt * nr_tracks * sizeof(struct eckd_count);
2157 		break;
2158 	case 0x01:	/* Write record zero and format track. */
2159 	case 0x09:	/* Write record zero and format track, use cdl. */
2160 		cplength = 2 + rpt * nr_tracks;
2161 		if (use_prefix)
2162 			datasize = sizeof(struct PFX_eckd_data) +
2163 				sizeof(struct LO_eckd_data) +
2164 				sizeof(struct eckd_count) +
2165 				rpt * nr_tracks * sizeof(struct eckd_count);
2166 		else
2167 			datasize = sizeof(struct DE_eckd_data) +
2168 				sizeof(struct LO_eckd_data) +
2169 				sizeof(struct eckd_count) +
2170 				rpt * nr_tracks * sizeof(struct eckd_count);
2171 		break;
2172 	case 0x04:	/* Invalidate track. */
2173 	case 0x0c:	/* Invalidate track, use cdl. */
2174 		cplength = 3;
2175 		if (use_prefix)
2176 			datasize = sizeof(struct PFX_eckd_data) +
2177 				sizeof(struct LO_eckd_data) +
2178 				sizeof(struct eckd_count);
2179 		else
2180 			datasize = sizeof(struct DE_eckd_data) +
2181 				sizeof(struct LO_eckd_data) +
2182 				sizeof(struct eckd_count);
2183 		break;
2184 	default:
2185 		dev_warn(&startdev->cdev->dev,
2186 			 "An I/O control call used incorrect flags 0x%x\n",
2187 			 fdata->intensity);
2188 		return ERR_PTR(-EINVAL);
2189 	}
2190 	/* Allocate the format ccw request. */
2191 	fcp = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength,
2192 				   datasize, startdev);
2193 	if (IS_ERR(fcp))
2194 		return fcp;
2195 
2196 	start_priv->count++;
2197 	data = fcp->data;
2198 	ccw = fcp->cpaddr;
2199 
2200 	switch (intensity & ~0x08) {
2201 	case 0x00: /* Normal format. */
2202 		if (use_prefix) {
2203 			prefix(ccw++, (struct PFX_eckd_data *) data,
2204 			       fdata->start_unit, fdata->stop_unit,
2205 			       DASD_ECKD_CCW_WRITE_CKD, base, startdev);
2206 			/* grant subsystem permission to format R0 */
2207 			if (r0_perm)
2208 				((struct PFX_eckd_data *)data)
2209 					->define_extent.ga_extended |= 0x04;
2210 			data += sizeof(struct PFX_eckd_data);
2211 		} else {
2212 			define_extent(ccw++, (struct DE_eckd_data *) data,
2213 				      fdata->start_unit, fdata->stop_unit,
2214 				      DASD_ECKD_CCW_WRITE_CKD, startdev);
2215 			/* grant subsystem permission to format R0 */
2216 			if (r0_perm)
2217 				((struct DE_eckd_data *) data)
2218 					->ga_extended |= 0x04;
2219 			data += sizeof(struct DE_eckd_data);
2220 		}
2221 		ccw[-1].flags |= CCW_FLAG_CC;
2222 		locate_record(ccw++, (struct LO_eckd_data *) data,
2223 			      fdata->start_unit, 0, rpt*nr_tracks,
2224 			      DASD_ECKD_CCW_WRITE_CKD, base,
2225 			      fdata->blksize);
2226 		data += sizeof(struct LO_eckd_data);
2227 		break;
2228 	case 0x01: /* Write record zero + format track. */
2229 		if (use_prefix) {
2230 			prefix(ccw++, (struct PFX_eckd_data *) data,
2231 			       fdata->start_unit, fdata->stop_unit,
2232 			       DASD_ECKD_CCW_WRITE_RECORD_ZERO,
2233 			       base, startdev);
2234 			data += sizeof(struct PFX_eckd_data);
2235 		} else {
2236 			define_extent(ccw++, (struct DE_eckd_data *) data,
2237 			       fdata->start_unit, fdata->stop_unit,
2238 			       DASD_ECKD_CCW_WRITE_RECORD_ZERO, startdev);
2239 			data += sizeof(struct DE_eckd_data);
2240 		}
2241 		ccw[-1].flags |= CCW_FLAG_CC;
2242 		locate_record(ccw++, (struct LO_eckd_data *) data,
2243 			      fdata->start_unit, 0, rpt * nr_tracks + 1,
2244 			      DASD_ECKD_CCW_WRITE_RECORD_ZERO, base,
2245 			      base->block->bp_block);
2246 		data += sizeof(struct LO_eckd_data);
2247 		break;
2248 	case 0x04: /* Invalidate track. */
2249 		if (use_prefix) {
2250 			prefix(ccw++, (struct PFX_eckd_data *) data,
2251 			       fdata->start_unit, fdata->stop_unit,
2252 			       DASD_ECKD_CCW_WRITE_CKD, base, startdev);
2253 			data += sizeof(struct PFX_eckd_data);
2254 		} else {
2255 			define_extent(ccw++, (struct DE_eckd_data *) data,
2256 			       fdata->start_unit, fdata->stop_unit,
2257 			       DASD_ECKD_CCW_WRITE_CKD, startdev);
2258 			data += sizeof(struct DE_eckd_data);
2259 		}
2260 		ccw[-1].flags |= CCW_FLAG_CC;
2261 		locate_record(ccw++, (struct LO_eckd_data *) data,
2262 			      fdata->start_unit, 0, 1,
2263 			      DASD_ECKD_CCW_WRITE_CKD, base, 8);
2264 		data += sizeof(struct LO_eckd_data);
2265 		break;
2266 	}
2267 
2268 	for (j = 0; j < nr_tracks; j++) {
2269 		/* calculate cylinder and head for the current track */
2270 		set_ch_t(&address,
2271 			 (fdata->start_unit + j) /
2272 			 base_priv->rdc_data.trk_per_cyl,
2273 			 (fdata->start_unit + j) %
2274 			 base_priv->rdc_data.trk_per_cyl);
2275 		if (intensity & 0x01) {	/* write record zero */
2276 			ect = (struct eckd_count *) data;
2277 			data += sizeof(struct eckd_count);
2278 			ect->cyl = address.cyl;
2279 			ect->head = address.head;
2280 			ect->record = 0;
2281 			ect->kl = 0;
2282 			ect->dl = 8;
2283 			ccw[-1].flags |= CCW_FLAG_CC;
2284 			ccw->cmd_code = DASD_ECKD_CCW_WRITE_RECORD_ZERO;
2285 			ccw->flags = CCW_FLAG_SLI;
2286 			ccw->count = 8;
2287 			ccw->cda = (__u32)(addr_t) ect;
2288 			ccw++;
2289 		}
2290 		if ((intensity & ~0x08) & 0x04) {	/* erase track */
2291 			ect = (struct eckd_count *) data;
2292 			data += sizeof(struct eckd_count);
2293 			ect->cyl = address.cyl;
2294 			ect->head = address.head;
2295 			ect->record = 1;
2296 			ect->kl = 0;
2297 			ect->dl = 0;
2298 			ccw[-1].flags |= CCW_FLAG_CC;
2299 			ccw->cmd_code = DASD_ECKD_CCW_WRITE_CKD;
2300 			ccw->flags = CCW_FLAG_SLI;
2301 			ccw->count = 8;
2302 			ccw->cda = (__u32)(addr_t) ect;
2303 		} else {		/* write remaining records */
2304 			for (i = 0; i < rpt; i++) {
2305 				ect = (struct eckd_count *) data;
2306 				data += sizeof(struct eckd_count);
2307 				ect->cyl = address.cyl;
2308 				ect->head = address.head;
2309 				ect->record = i + 1;
2310 				ect->kl = 0;
2311 				ect->dl = fdata->blksize;
2312 				/*
2313 				 * Check for special tracks 0-1
2314 				 * when formatting CDL
2315 				 */
2316 				if ((intensity & 0x08) &&
2317 				    fdata->start_unit == 0) {
2318 					if (i < 3) {
2319 						ect->kl = 4;
2320 						ect->dl = sizes_trk0[i] - 4;
2321 					}
2322 				}
2323 				if ((intensity & 0x08) &&
2324 				    fdata->start_unit == 1) {
2325 					ect->kl = 44;
2326 					ect->dl = LABEL_SIZE - 44;
2327 				}
2328 				ccw[-1].flags |= CCW_FLAG_CC;
2329 				if (i != 0 || j == 0)
2330 					ccw->cmd_code =
2331 						DASD_ECKD_CCW_WRITE_CKD;
2332 				else
2333 					ccw->cmd_code =
2334 						DASD_ECKD_CCW_WRITE_CKD_MT;
2335 				ccw->flags = CCW_FLAG_SLI;
2336 				ccw->count = 8;
2337 					ccw->cda = (__u32)(addr_t) ect;
2338 					ccw++;
2339 			}
2340 		}
2341 	}
2342 
2343 	fcp->startdev = startdev;
2344 	fcp->memdev = startdev;
2345 	fcp->basedev = base;
2346 	fcp->retries = 256;
2347 	fcp->expires = startdev->default_expires * HZ;
2348 	fcp->buildclk = get_tod_clock();
2349 	fcp->status = DASD_CQR_FILLED;
2350 
2351 	return fcp;
2352 }
2353 
2354 static int
2355 dasd_eckd_format_device(struct dasd_device *base,
2356 			struct format_data_t *fdata,
2357 			int enable_pav)
2358 {
2359 	struct dasd_ccw_req *cqr, *n;
2360 	struct dasd_block *block;
2361 	struct dasd_eckd_private *private;
2362 	struct list_head format_queue;
2363 	struct dasd_device *device;
2364 	int old_stop, format_step;
2365 	int step, rc = 0, sleep_rc;
2366 
2367 	block = base->block;
2368 	private = (struct dasd_eckd_private *) base->private;
2369 
2370 	/* Sanity checks. */
2371 	if (fdata->start_unit >=
2372 	    (private->real_cyl * private->rdc_data.trk_per_cyl)) {
2373 		dev_warn(&base->cdev->dev,
2374 			 "Start track number %u used in formatting is too big\n",
2375 			 fdata->start_unit);
2376 		return -EINVAL;
2377 	}
2378 	if (fdata->stop_unit >=
2379 	    (private->real_cyl * private->rdc_data.trk_per_cyl)) {
2380 		dev_warn(&base->cdev->dev,
2381 			 "Stop track number %u used in formatting is too big\n",
2382 			 fdata->stop_unit);
2383 		return -EINVAL;
2384 	}
2385 	if (fdata->start_unit > fdata->stop_unit) {
2386 		dev_warn(&base->cdev->dev,
2387 			 "Start track %u used in formatting exceeds end track\n",
2388 			 fdata->start_unit);
2389 		return -EINVAL;
2390 	}
2391 	if (dasd_check_blocksize(fdata->blksize) != 0) {
2392 		dev_warn(&base->cdev->dev,
2393 			 "The DASD cannot be formatted with block size %u\n",
2394 			 fdata->blksize);
2395 		return -EINVAL;
2396 	}
2397 
2398 	INIT_LIST_HEAD(&format_queue);
2399 
2400 	old_stop = fdata->stop_unit;
2401 	while (fdata->start_unit <= 1) {
2402 		fdata->stop_unit = fdata->start_unit;
2403 		cqr = dasd_eckd_build_format(base, fdata, enable_pav);
2404 		list_add(&cqr->blocklist, &format_queue);
2405 
2406 		fdata->stop_unit = old_stop;
2407 		fdata->start_unit++;
2408 
2409 		if (fdata->start_unit > fdata->stop_unit)
2410 			goto sleep;
2411 	}
2412 
2413 retry:
2414 	format_step = 255 / recs_per_track(&private->rdc_data, 0,
2415 					   fdata->blksize);
2416 	while (fdata->start_unit <= old_stop) {
2417 		step = fdata->stop_unit - fdata->start_unit + 1;
2418 		if (step > format_step)
2419 			fdata->stop_unit = fdata->start_unit + format_step - 1;
2420 
2421 		cqr = dasd_eckd_build_format(base, fdata, enable_pav);
2422 		if (IS_ERR(cqr)) {
2423 			if (PTR_ERR(cqr) == -ENOMEM) {
2424 				/*
2425 				 * not enough memory available
2426 				 * go to out and start requests
2427 				 * retry after first requests were finished
2428 				 */
2429 				fdata->stop_unit = old_stop;
2430 				goto sleep;
2431 			} else
2432 				return PTR_ERR(cqr);
2433 		}
2434 		list_add(&cqr->blocklist, &format_queue);
2435 
2436 		fdata->start_unit = fdata->stop_unit + 1;
2437 		fdata->stop_unit = old_stop;
2438 	}
2439 
2440 sleep:
2441 	sleep_rc = dasd_sleep_on_queue(&format_queue);
2442 
2443 	list_for_each_entry_safe(cqr, n, &format_queue, blocklist) {
2444 		device = cqr->startdev;
2445 		private = (struct dasd_eckd_private *) device->private;
2446 		if (cqr->status == DASD_CQR_FAILED)
2447 			rc = -EIO;
2448 		list_del_init(&cqr->blocklist);
2449 		dasd_sfree_request(cqr, device);
2450 		private->count--;
2451 	}
2452 
2453 	if (sleep_rc)
2454 		return sleep_rc;
2455 
2456 	/*
2457 	 * in case of ENOMEM we need to retry after
2458 	 * first requests are finished
2459 	 */
2460 	if (fdata->start_unit <= fdata->stop_unit)
2461 		goto retry;
2462 
2463 	return rc;
2464 }
2465 
2466 static void dasd_eckd_handle_terminated_request(struct dasd_ccw_req *cqr)
2467 {
2468 	if (cqr->retries < 0) {
2469 		cqr->status = DASD_CQR_FAILED;
2470 		return;
2471 	}
2472 	cqr->status = DASD_CQR_FILLED;
2473 	if (cqr->block && (cqr->startdev != cqr->block->base)) {
2474 		dasd_eckd_reset_ccw_to_base_io(cqr);
2475 		cqr->startdev = cqr->block->base;
2476 		cqr->lpm = cqr->block->base->path_data.opm;
2477 	}
2478 };
2479 
2480 static dasd_erp_fn_t
2481 dasd_eckd_erp_action(struct dasd_ccw_req * cqr)
2482 {
2483 	struct dasd_device *device = (struct dasd_device *) cqr->startdev;
2484 	struct ccw_device *cdev = device->cdev;
2485 
2486 	switch (cdev->id.cu_type) {
2487 	case 0x3990:
2488 	case 0x2105:
2489 	case 0x2107:
2490 	case 0x1750:
2491 		return dasd_3990_erp_action;
2492 	case 0x9343:
2493 	case 0x3880:
2494 	default:
2495 		return dasd_default_erp_action;
2496 	}
2497 }
2498 
2499 static dasd_erp_fn_t
2500 dasd_eckd_erp_postaction(struct dasd_ccw_req * cqr)
2501 {
2502 	return dasd_default_erp_postaction;
2503 }
2504 
2505 static void dasd_eckd_check_for_device_change(struct dasd_device *device,
2506 					      struct dasd_ccw_req *cqr,
2507 					      struct irb *irb)
2508 {
2509 	char mask;
2510 	char *sense = NULL;
2511 	struct dasd_eckd_private *private;
2512 
2513 	private = (struct dasd_eckd_private *) device->private;
2514 	/* first of all check for state change pending interrupt */
2515 	mask = DEV_STAT_ATTENTION | DEV_STAT_DEV_END | DEV_STAT_UNIT_EXCEP;
2516 	if ((scsw_dstat(&irb->scsw) & mask) == mask) {
2517 		/*
2518 		 * for alias only, not in offline processing
2519 		 * and only if not suspended
2520 		 */
2521 		if (!device->block && private->lcu &&
2522 		    device->state == DASD_STATE_ONLINE &&
2523 		    !test_bit(DASD_FLAG_OFFLINE, &device->flags) &&
2524 		    !test_bit(DASD_FLAG_SUSPENDED, &device->flags)) {
2525 			/*
2526 			 * the state change could be caused by an alias
2527 			 * reassignment remove device from alias handling
2528 			 * to prevent new requests from being scheduled on
2529 			 * the wrong alias device
2530 			 */
2531 			dasd_alias_remove_device(device);
2532 
2533 			/* schedule worker to reload device */
2534 			dasd_reload_device(device);
2535 		}
2536 		dasd_generic_handle_state_change(device);
2537 		return;
2538 	}
2539 
2540 	sense = dasd_get_sense(irb);
2541 	if (!sense)
2542 		return;
2543 
2544 	/* summary unit check */
2545 	if ((sense[27] & DASD_SENSE_BIT_0) && (sense[7] == 0x0D) &&
2546 	    (scsw_dstat(&irb->scsw) & DEV_STAT_UNIT_CHECK)) {
2547 		dasd_alias_handle_summary_unit_check(device, irb);
2548 		return;
2549 	}
2550 
2551 	/* service information message SIM */
2552 	if (!cqr && !(sense[27] & DASD_SENSE_BIT_0) &&
2553 	    ((sense[6] & DASD_SIM_SENSE) == DASD_SIM_SENSE)) {
2554 		dasd_3990_erp_handle_sim(device, sense);
2555 		return;
2556 	}
2557 
2558 	/* loss of device reservation is handled via base devices only
2559 	 * as alias devices may be used with several bases
2560 	 */
2561 	if (device->block && (sense[27] & DASD_SENSE_BIT_0) &&
2562 	    (sense[7] == 0x3F) &&
2563 	    (scsw_dstat(&irb->scsw) & DEV_STAT_UNIT_CHECK) &&
2564 	    test_bit(DASD_FLAG_IS_RESERVED, &device->flags)) {
2565 		if (device->features & DASD_FEATURE_FAILONSLCK)
2566 			set_bit(DASD_FLAG_LOCK_STOLEN, &device->flags);
2567 		clear_bit(DASD_FLAG_IS_RESERVED, &device->flags);
2568 		dev_err(&device->cdev->dev,
2569 			"The device reservation was lost\n");
2570 	}
2571 }
2572 
2573 static struct dasd_ccw_req *dasd_eckd_build_cp_cmd_single(
2574 					       struct dasd_device *startdev,
2575 					       struct dasd_block *block,
2576 					       struct request *req,
2577 					       sector_t first_rec,
2578 					       sector_t last_rec,
2579 					       sector_t first_trk,
2580 					       sector_t last_trk,
2581 					       unsigned int first_offs,
2582 					       unsigned int last_offs,
2583 					       unsigned int blk_per_trk,
2584 					       unsigned int blksize)
2585 {
2586 	struct dasd_eckd_private *private;
2587 	unsigned long *idaws;
2588 	struct LO_eckd_data *LO_data;
2589 	struct dasd_ccw_req *cqr;
2590 	struct ccw1 *ccw;
2591 	struct req_iterator iter;
2592 	struct bio_vec bv;
2593 	char *dst;
2594 	unsigned int off;
2595 	int count, cidaw, cplength, datasize;
2596 	sector_t recid;
2597 	unsigned char cmd, rcmd;
2598 	int use_prefix;
2599 	struct dasd_device *basedev;
2600 
2601 	basedev = block->base;
2602 	private = (struct dasd_eckd_private *) basedev->private;
2603 	if (rq_data_dir(req) == READ)
2604 		cmd = DASD_ECKD_CCW_READ_MT;
2605 	else if (rq_data_dir(req) == WRITE)
2606 		cmd = DASD_ECKD_CCW_WRITE_MT;
2607 	else
2608 		return ERR_PTR(-EINVAL);
2609 
2610 	/* Check struct bio and count the number of blocks for the request. */
2611 	count = 0;
2612 	cidaw = 0;
2613 	rq_for_each_segment(bv, req, iter) {
2614 		if (bv.bv_len & (blksize - 1))
2615 			/* Eckd can only do full blocks. */
2616 			return ERR_PTR(-EINVAL);
2617 		count += bv.bv_len >> (block->s2b_shift + 9);
2618 #if defined(CONFIG_64BIT)
2619 		if (idal_is_needed (page_address(bv.bv_page), bv.bv_len))
2620 			cidaw += bv.bv_len >> (block->s2b_shift + 9);
2621 #endif
2622 	}
2623 	/* Paranoia. */
2624 	if (count != last_rec - first_rec + 1)
2625 		return ERR_PTR(-EINVAL);
2626 
2627 	/* use the prefix command if available */
2628 	use_prefix = private->features.feature[8] & 0x01;
2629 	if (use_prefix) {
2630 		/* 1x prefix + number of blocks */
2631 		cplength = 2 + count;
2632 		/* 1x prefix + cidaws*sizeof(long) */
2633 		datasize = sizeof(struct PFX_eckd_data) +
2634 			sizeof(struct LO_eckd_data) +
2635 			cidaw * sizeof(unsigned long);
2636 	} else {
2637 		/* 1x define extent + 1x locate record + number of blocks */
2638 		cplength = 2 + count;
2639 		/* 1x define extent + 1x locate record + cidaws*sizeof(long) */
2640 		datasize = sizeof(struct DE_eckd_data) +
2641 			sizeof(struct LO_eckd_data) +
2642 			cidaw * sizeof(unsigned long);
2643 	}
2644 	/* Find out the number of additional locate record ccws for cdl. */
2645 	if (private->uses_cdl && first_rec < 2*blk_per_trk) {
2646 		if (last_rec >= 2*blk_per_trk)
2647 			count = 2*blk_per_trk - first_rec;
2648 		cplength += count;
2649 		datasize += count*sizeof(struct LO_eckd_data);
2650 	}
2651 	/* Allocate the ccw request. */
2652 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength, datasize,
2653 				   startdev);
2654 	if (IS_ERR(cqr))
2655 		return cqr;
2656 	ccw = cqr->cpaddr;
2657 	/* First ccw is define extent or prefix. */
2658 	if (use_prefix) {
2659 		if (prefix(ccw++, cqr->data, first_trk,
2660 			   last_trk, cmd, basedev, startdev) == -EAGAIN) {
2661 			/* Clock not in sync and XRC is enabled.
2662 			 * Try again later.
2663 			 */
2664 			dasd_sfree_request(cqr, startdev);
2665 			return ERR_PTR(-EAGAIN);
2666 		}
2667 		idaws = (unsigned long *) (cqr->data +
2668 					   sizeof(struct PFX_eckd_data));
2669 	} else {
2670 		if (define_extent(ccw++, cqr->data, first_trk,
2671 				  last_trk, cmd, basedev) == -EAGAIN) {
2672 			/* Clock not in sync and XRC is enabled.
2673 			 * Try again later.
2674 			 */
2675 			dasd_sfree_request(cqr, startdev);
2676 			return ERR_PTR(-EAGAIN);
2677 		}
2678 		idaws = (unsigned long *) (cqr->data +
2679 					   sizeof(struct DE_eckd_data));
2680 	}
2681 	/* Build locate_record+read/write/ccws. */
2682 	LO_data = (struct LO_eckd_data *) (idaws + cidaw);
2683 	recid = first_rec;
2684 	if (private->uses_cdl == 0 || recid > 2*blk_per_trk) {
2685 		/* Only standard blocks so there is just one locate record. */
2686 		ccw[-1].flags |= CCW_FLAG_CC;
2687 		locate_record(ccw++, LO_data++, first_trk, first_offs + 1,
2688 			      last_rec - recid + 1, cmd, basedev, blksize);
2689 	}
2690 	rq_for_each_segment(bv, req, iter) {
2691 		dst = page_address(bv.bv_page) + bv.bv_offset;
2692 		if (dasd_page_cache) {
2693 			char *copy = kmem_cache_alloc(dasd_page_cache,
2694 						      GFP_DMA | __GFP_NOWARN);
2695 			if (copy && rq_data_dir(req) == WRITE)
2696 				memcpy(copy + bv.bv_offset, dst, bv.bv_len);
2697 			if (copy)
2698 				dst = copy + bv.bv_offset;
2699 		}
2700 		for (off = 0; off < bv.bv_len; off += blksize) {
2701 			sector_t trkid = recid;
2702 			unsigned int recoffs = sector_div(trkid, blk_per_trk);
2703 			rcmd = cmd;
2704 			count = blksize;
2705 			/* Locate record for cdl special block ? */
2706 			if (private->uses_cdl && recid < 2*blk_per_trk) {
2707 				if (dasd_eckd_cdl_special(blk_per_trk, recid)){
2708 					rcmd |= 0x8;
2709 					count = dasd_eckd_cdl_reclen(recid);
2710 					if (count < blksize &&
2711 					    rq_data_dir(req) == READ)
2712 						memset(dst + count, 0xe5,
2713 						       blksize - count);
2714 				}
2715 				ccw[-1].flags |= CCW_FLAG_CC;
2716 				locate_record(ccw++, LO_data++,
2717 					      trkid, recoffs + 1,
2718 					      1, rcmd, basedev, count);
2719 			}
2720 			/* Locate record for standard blocks ? */
2721 			if (private->uses_cdl && recid == 2*blk_per_trk) {
2722 				ccw[-1].flags |= CCW_FLAG_CC;
2723 				locate_record(ccw++, LO_data++,
2724 					      trkid, recoffs + 1,
2725 					      last_rec - recid + 1,
2726 					      cmd, basedev, count);
2727 			}
2728 			/* Read/write ccw. */
2729 			ccw[-1].flags |= CCW_FLAG_CC;
2730 			ccw->cmd_code = rcmd;
2731 			ccw->count = count;
2732 			if (idal_is_needed(dst, blksize)) {
2733 				ccw->cda = (__u32)(addr_t) idaws;
2734 				ccw->flags = CCW_FLAG_IDA;
2735 				idaws = idal_create_words(idaws, dst, blksize);
2736 			} else {
2737 				ccw->cda = (__u32)(addr_t) dst;
2738 				ccw->flags = 0;
2739 			}
2740 			ccw++;
2741 			dst += blksize;
2742 			recid++;
2743 		}
2744 	}
2745 	if (blk_noretry_request(req) ||
2746 	    block->base->features & DASD_FEATURE_FAILFAST)
2747 		set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
2748 	cqr->startdev = startdev;
2749 	cqr->memdev = startdev;
2750 	cqr->block = block;
2751 	cqr->expires = startdev->default_expires * HZ;	/* default 5 minutes */
2752 	cqr->lpm = startdev->path_data.ppm;
2753 	cqr->retries = startdev->default_retries;
2754 	cqr->buildclk = get_tod_clock();
2755 	cqr->status = DASD_CQR_FILLED;
2756 	return cqr;
2757 }
2758 
2759 static struct dasd_ccw_req *dasd_eckd_build_cp_cmd_track(
2760 					       struct dasd_device *startdev,
2761 					       struct dasd_block *block,
2762 					       struct request *req,
2763 					       sector_t first_rec,
2764 					       sector_t last_rec,
2765 					       sector_t first_trk,
2766 					       sector_t last_trk,
2767 					       unsigned int first_offs,
2768 					       unsigned int last_offs,
2769 					       unsigned int blk_per_trk,
2770 					       unsigned int blksize)
2771 {
2772 	unsigned long *idaws;
2773 	struct dasd_ccw_req *cqr;
2774 	struct ccw1 *ccw;
2775 	struct req_iterator iter;
2776 	struct bio_vec bv;
2777 	char *dst, *idaw_dst;
2778 	unsigned int cidaw, cplength, datasize;
2779 	unsigned int tlf;
2780 	sector_t recid;
2781 	unsigned char cmd;
2782 	struct dasd_device *basedev;
2783 	unsigned int trkcount, count, count_to_trk_end;
2784 	unsigned int idaw_len, seg_len, part_len, len_to_track_end;
2785 	unsigned char new_track, end_idaw;
2786 	sector_t trkid;
2787 	unsigned int recoffs;
2788 
2789 	basedev = block->base;
2790 	if (rq_data_dir(req) == READ)
2791 		cmd = DASD_ECKD_CCW_READ_TRACK_DATA;
2792 	else if (rq_data_dir(req) == WRITE)
2793 		cmd = DASD_ECKD_CCW_WRITE_TRACK_DATA;
2794 	else
2795 		return ERR_PTR(-EINVAL);
2796 
2797 	/* Track based I/O needs IDAWs for each page, and not just for
2798 	 * 64 bit addresses. We need additional idals for pages
2799 	 * that get filled from two tracks, so we use the number
2800 	 * of records as upper limit.
2801 	 */
2802 	cidaw = last_rec - first_rec + 1;
2803 	trkcount = last_trk - first_trk + 1;
2804 
2805 	/* 1x prefix + one read/write ccw per track */
2806 	cplength = 1 + trkcount;
2807 
2808 	/* on 31-bit we need space for two 32 bit addresses per page
2809 	 * on 64-bit one 64 bit address
2810 	 */
2811 	datasize = sizeof(struct PFX_eckd_data) +
2812 		cidaw * sizeof(unsigned long long);
2813 
2814 	/* Allocate the ccw request. */
2815 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength, datasize,
2816 				   startdev);
2817 	if (IS_ERR(cqr))
2818 		return cqr;
2819 	ccw = cqr->cpaddr;
2820 	/* transfer length factor: how many bytes to read from the last track */
2821 	if (first_trk == last_trk)
2822 		tlf = last_offs - first_offs + 1;
2823 	else
2824 		tlf = last_offs + 1;
2825 	tlf *= blksize;
2826 
2827 	if (prefix_LRE(ccw++, cqr->data, first_trk,
2828 		       last_trk, cmd, basedev, startdev,
2829 		       1 /* format */, first_offs + 1,
2830 		       trkcount, blksize,
2831 		       tlf) == -EAGAIN) {
2832 		/* Clock not in sync and XRC is enabled.
2833 		 * Try again later.
2834 		 */
2835 		dasd_sfree_request(cqr, startdev);
2836 		return ERR_PTR(-EAGAIN);
2837 	}
2838 
2839 	/*
2840 	 * The translation of request into ccw programs must meet the
2841 	 * following conditions:
2842 	 * - all idaws but the first and the last must address full pages
2843 	 *   (or 2K blocks on 31-bit)
2844 	 * - the scope of a ccw and it's idal ends with the track boundaries
2845 	 */
2846 	idaws = (unsigned long *) (cqr->data + sizeof(struct PFX_eckd_data));
2847 	recid = first_rec;
2848 	new_track = 1;
2849 	end_idaw = 0;
2850 	len_to_track_end = 0;
2851 	idaw_dst = NULL;
2852 	idaw_len = 0;
2853 	rq_for_each_segment(bv, req, iter) {
2854 		dst = page_address(bv.bv_page) + bv.bv_offset;
2855 		seg_len = bv.bv_len;
2856 		while (seg_len) {
2857 			if (new_track) {
2858 				trkid = recid;
2859 				recoffs = sector_div(trkid, blk_per_trk);
2860 				count_to_trk_end = blk_per_trk - recoffs;
2861 				count = min((last_rec - recid + 1),
2862 					    (sector_t)count_to_trk_end);
2863 				len_to_track_end = count * blksize;
2864 				ccw[-1].flags |= CCW_FLAG_CC;
2865 				ccw->cmd_code = cmd;
2866 				ccw->count = len_to_track_end;
2867 				ccw->cda = (__u32)(addr_t)idaws;
2868 				ccw->flags = CCW_FLAG_IDA;
2869 				ccw++;
2870 				recid += count;
2871 				new_track = 0;
2872 				/* first idaw for a ccw may start anywhere */
2873 				if (!idaw_dst)
2874 					idaw_dst = dst;
2875 			}
2876 			/* If we start a new idaw, we must make sure that it
2877 			 * starts on an IDA_BLOCK_SIZE boundary.
2878 			 * If we continue an idaw, we must make sure that the
2879 			 * current segment begins where the so far accumulated
2880 			 * idaw ends
2881 			 */
2882 			if (!idaw_dst) {
2883 				if (__pa(dst) & (IDA_BLOCK_SIZE-1)) {
2884 					dasd_sfree_request(cqr, startdev);
2885 					return ERR_PTR(-ERANGE);
2886 				} else
2887 					idaw_dst = dst;
2888 			}
2889 			if ((idaw_dst + idaw_len) != dst) {
2890 				dasd_sfree_request(cqr, startdev);
2891 				return ERR_PTR(-ERANGE);
2892 			}
2893 			part_len = min(seg_len, len_to_track_end);
2894 			seg_len -= part_len;
2895 			dst += part_len;
2896 			idaw_len += part_len;
2897 			len_to_track_end -= part_len;
2898 			/* collected memory area ends on an IDA_BLOCK border,
2899 			 * -> create an idaw
2900 			 * idal_create_words will handle cases where idaw_len
2901 			 * is larger then IDA_BLOCK_SIZE
2902 			 */
2903 			if (!(__pa(idaw_dst + idaw_len) & (IDA_BLOCK_SIZE-1)))
2904 				end_idaw = 1;
2905 			/* We also need to end the idaw at track end */
2906 			if (!len_to_track_end) {
2907 				new_track = 1;
2908 				end_idaw = 1;
2909 			}
2910 			if (end_idaw) {
2911 				idaws = idal_create_words(idaws, idaw_dst,
2912 							  idaw_len);
2913 				idaw_dst = NULL;
2914 				idaw_len = 0;
2915 				end_idaw = 0;
2916 			}
2917 		}
2918 	}
2919 
2920 	if (blk_noretry_request(req) ||
2921 	    block->base->features & DASD_FEATURE_FAILFAST)
2922 		set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
2923 	cqr->startdev = startdev;
2924 	cqr->memdev = startdev;
2925 	cqr->block = block;
2926 	cqr->expires = startdev->default_expires * HZ;	/* default 5 minutes */
2927 	cqr->lpm = startdev->path_data.ppm;
2928 	cqr->retries = startdev->default_retries;
2929 	cqr->buildclk = get_tod_clock();
2930 	cqr->status = DASD_CQR_FILLED;
2931 	return cqr;
2932 }
2933 
2934 static int prepare_itcw(struct itcw *itcw,
2935 			unsigned int trk, unsigned int totrk, int cmd,
2936 			struct dasd_device *basedev,
2937 			struct dasd_device *startdev,
2938 			unsigned int rec_on_trk, int count,
2939 			unsigned int blksize,
2940 			unsigned int total_data_size,
2941 			unsigned int tlf,
2942 			unsigned int blk_per_trk)
2943 {
2944 	struct PFX_eckd_data pfxdata;
2945 	struct dasd_eckd_private *basepriv, *startpriv;
2946 	struct DE_eckd_data *dedata;
2947 	struct LRE_eckd_data *lredata;
2948 	struct dcw *dcw;
2949 
2950 	u32 begcyl, endcyl;
2951 	u16 heads, beghead, endhead;
2952 	u8 pfx_cmd;
2953 
2954 	int rc = 0;
2955 	int sector = 0;
2956 	int dn, d;
2957 
2958 
2959 	/* setup prefix data */
2960 	basepriv = (struct dasd_eckd_private *) basedev->private;
2961 	startpriv = (struct dasd_eckd_private *) startdev->private;
2962 	dedata = &pfxdata.define_extent;
2963 	lredata = &pfxdata.locate_record;
2964 
2965 	memset(&pfxdata, 0, sizeof(pfxdata));
2966 	pfxdata.format = 1; /* PFX with LRE */
2967 	pfxdata.base_address = basepriv->ned->unit_addr;
2968 	pfxdata.base_lss = basepriv->ned->ID;
2969 	pfxdata.validity.define_extent = 1;
2970 
2971 	/* private uid is kept up to date, conf_data may be outdated */
2972 	if (startpriv->uid.type != UA_BASE_DEVICE) {
2973 		pfxdata.validity.verify_base = 1;
2974 		if (startpriv->uid.type == UA_HYPER_PAV_ALIAS)
2975 			pfxdata.validity.hyper_pav = 1;
2976 	}
2977 
2978 	switch (cmd) {
2979 	case DASD_ECKD_CCW_READ_TRACK_DATA:
2980 		dedata->mask.perm = 0x1;
2981 		dedata->attributes.operation = basepriv->attrib.operation;
2982 		dedata->blk_size = blksize;
2983 		dedata->ga_extended |= 0x42;
2984 		lredata->operation.orientation = 0x0;
2985 		lredata->operation.operation = 0x0C;
2986 		lredata->auxiliary.check_bytes = 0x01;
2987 		pfx_cmd = DASD_ECKD_CCW_PFX_READ;
2988 		break;
2989 	case DASD_ECKD_CCW_WRITE_TRACK_DATA:
2990 		dedata->mask.perm = 0x02;
2991 		dedata->attributes.operation = basepriv->attrib.operation;
2992 		dedata->blk_size = blksize;
2993 		rc = check_XRC_on_prefix(&pfxdata, basedev);
2994 		dedata->ga_extended |= 0x42;
2995 		lredata->operation.orientation = 0x0;
2996 		lredata->operation.operation = 0x3F;
2997 		lredata->extended_operation = 0x23;
2998 		lredata->auxiliary.check_bytes = 0x2;
2999 		pfx_cmd = DASD_ECKD_CCW_PFX;
3000 		break;
3001 	default:
3002 		DBF_DEV_EVENT(DBF_ERR, basedev,
3003 			      "prepare itcw, unknown opcode 0x%x", cmd);
3004 		BUG();
3005 		break;
3006 	}
3007 	if (rc)
3008 		return rc;
3009 
3010 	dedata->attributes.mode = 0x3;	/* ECKD */
3011 
3012 	heads = basepriv->rdc_data.trk_per_cyl;
3013 	begcyl = trk / heads;
3014 	beghead = trk % heads;
3015 	endcyl = totrk / heads;
3016 	endhead = totrk % heads;
3017 
3018 	/* check for sequential prestage - enhance cylinder range */
3019 	if (dedata->attributes.operation == DASD_SEQ_PRESTAGE ||
3020 	    dedata->attributes.operation == DASD_SEQ_ACCESS) {
3021 
3022 		if (endcyl + basepriv->attrib.nr_cyl < basepriv->real_cyl)
3023 			endcyl += basepriv->attrib.nr_cyl;
3024 		else
3025 			endcyl = (basepriv->real_cyl - 1);
3026 	}
3027 
3028 	set_ch_t(&dedata->beg_ext, begcyl, beghead);
3029 	set_ch_t(&dedata->end_ext, endcyl, endhead);
3030 
3031 	dedata->ep_format = 0x20; /* records per track is valid */
3032 	dedata->ep_rec_per_track = blk_per_trk;
3033 
3034 	if (rec_on_trk) {
3035 		switch (basepriv->rdc_data.dev_type) {
3036 		case 0x3390:
3037 			dn = ceil_quot(blksize + 6, 232);
3038 			d = 9 + ceil_quot(blksize + 6 * (dn + 1), 34);
3039 			sector = (49 + (rec_on_trk - 1) * (10 + d)) / 8;
3040 			break;
3041 		case 0x3380:
3042 			d = 7 + ceil_quot(blksize + 12, 32);
3043 			sector = (39 + (rec_on_trk - 1) * (8 + d)) / 7;
3044 			break;
3045 		}
3046 	}
3047 
3048 	lredata->auxiliary.length_valid = 1;
3049 	lredata->auxiliary.length_scope = 1;
3050 	lredata->auxiliary.imbedded_ccw_valid = 1;
3051 	lredata->length = tlf;
3052 	lredata->imbedded_ccw = cmd;
3053 	lredata->count = count;
3054 	lredata->sector = sector;
3055 	set_ch_t(&lredata->seek_addr, begcyl, beghead);
3056 	lredata->search_arg.cyl = lredata->seek_addr.cyl;
3057 	lredata->search_arg.head = lredata->seek_addr.head;
3058 	lredata->search_arg.record = rec_on_trk;
3059 
3060 	dcw = itcw_add_dcw(itcw, pfx_cmd, 0,
3061 		     &pfxdata, sizeof(pfxdata), total_data_size);
3062 	return PTR_RET(dcw);
3063 }
3064 
3065 static struct dasd_ccw_req *dasd_eckd_build_cp_tpm_track(
3066 					       struct dasd_device *startdev,
3067 					       struct dasd_block *block,
3068 					       struct request *req,
3069 					       sector_t first_rec,
3070 					       sector_t last_rec,
3071 					       sector_t first_trk,
3072 					       sector_t last_trk,
3073 					       unsigned int first_offs,
3074 					       unsigned int last_offs,
3075 					       unsigned int blk_per_trk,
3076 					       unsigned int blksize)
3077 {
3078 	struct dasd_ccw_req *cqr;
3079 	struct req_iterator iter;
3080 	struct bio_vec bv;
3081 	char *dst;
3082 	unsigned int trkcount, ctidaw;
3083 	unsigned char cmd;
3084 	struct dasd_device *basedev;
3085 	unsigned int tlf;
3086 	struct itcw *itcw;
3087 	struct tidaw *last_tidaw = NULL;
3088 	int itcw_op;
3089 	size_t itcw_size;
3090 	u8 tidaw_flags;
3091 	unsigned int seg_len, part_len, len_to_track_end;
3092 	unsigned char new_track;
3093 	sector_t recid, trkid;
3094 	unsigned int offs;
3095 	unsigned int count, count_to_trk_end;
3096 	int ret;
3097 
3098 	basedev = block->base;
3099 	if (rq_data_dir(req) == READ) {
3100 		cmd = DASD_ECKD_CCW_READ_TRACK_DATA;
3101 		itcw_op = ITCW_OP_READ;
3102 	} else if (rq_data_dir(req) == WRITE) {
3103 		cmd = DASD_ECKD_CCW_WRITE_TRACK_DATA;
3104 		itcw_op = ITCW_OP_WRITE;
3105 	} else
3106 		return ERR_PTR(-EINVAL);
3107 
3108 	/* trackbased I/O needs address all memory via TIDAWs,
3109 	 * not just for 64 bit addresses. This allows us to map
3110 	 * each segment directly to one tidaw.
3111 	 * In the case of write requests, additional tidaws may
3112 	 * be needed when a segment crosses a track boundary.
3113 	 */
3114 	trkcount = last_trk - first_trk + 1;
3115 	ctidaw = 0;
3116 	rq_for_each_segment(bv, req, iter) {
3117 		++ctidaw;
3118 	}
3119 	if (rq_data_dir(req) == WRITE)
3120 		ctidaw += (last_trk - first_trk);
3121 
3122 	/* Allocate the ccw request. */
3123 	itcw_size = itcw_calc_size(0, ctidaw, 0);
3124 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 0, itcw_size, startdev);
3125 	if (IS_ERR(cqr))
3126 		return cqr;
3127 
3128 	/* transfer length factor: how many bytes to read from the last track */
3129 	if (first_trk == last_trk)
3130 		tlf = last_offs - first_offs + 1;
3131 	else
3132 		tlf = last_offs + 1;
3133 	tlf *= blksize;
3134 
3135 	itcw = itcw_init(cqr->data, itcw_size, itcw_op, 0, ctidaw, 0);
3136 	if (IS_ERR(itcw)) {
3137 		ret = -EINVAL;
3138 		goto out_error;
3139 	}
3140 	cqr->cpaddr = itcw_get_tcw(itcw);
3141 	if (prepare_itcw(itcw, first_trk, last_trk,
3142 			 cmd, basedev, startdev,
3143 			 first_offs + 1,
3144 			 trkcount, blksize,
3145 			 (last_rec - first_rec + 1) * blksize,
3146 			 tlf, blk_per_trk) == -EAGAIN) {
3147 		/* Clock not in sync and XRC is enabled.
3148 		 * Try again later.
3149 		 */
3150 		ret = -EAGAIN;
3151 		goto out_error;
3152 	}
3153 	len_to_track_end = 0;
3154 	/*
3155 	 * A tidaw can address 4k of memory, but must not cross page boundaries
3156 	 * We can let the block layer handle this by setting
3157 	 * blk_queue_segment_boundary to page boundaries and
3158 	 * blk_max_segment_size to page size when setting up the request queue.
3159 	 * For write requests, a TIDAW must not cross track boundaries, because
3160 	 * we have to set the CBC flag on the last tidaw for each track.
3161 	 */
3162 	if (rq_data_dir(req) == WRITE) {
3163 		new_track = 1;
3164 		recid = first_rec;
3165 		rq_for_each_segment(bv, req, iter) {
3166 			dst = page_address(bv.bv_page) + bv.bv_offset;
3167 			seg_len = bv.bv_len;
3168 			while (seg_len) {
3169 				if (new_track) {
3170 					trkid = recid;
3171 					offs = sector_div(trkid, blk_per_trk);
3172 					count_to_trk_end = blk_per_trk - offs;
3173 					count = min((last_rec - recid + 1),
3174 						    (sector_t)count_to_trk_end);
3175 					len_to_track_end = count * blksize;
3176 					recid += count;
3177 					new_track = 0;
3178 				}
3179 				part_len = min(seg_len, len_to_track_end);
3180 				seg_len -= part_len;
3181 				len_to_track_end -= part_len;
3182 				/* We need to end the tidaw at track end */
3183 				if (!len_to_track_end) {
3184 					new_track = 1;
3185 					tidaw_flags = TIDAW_FLAGS_INSERT_CBC;
3186 				} else
3187 					tidaw_flags = 0;
3188 				last_tidaw = itcw_add_tidaw(itcw, tidaw_flags,
3189 							    dst, part_len);
3190 				if (IS_ERR(last_tidaw)) {
3191 					ret = -EINVAL;
3192 					goto out_error;
3193 				}
3194 				dst += part_len;
3195 			}
3196 		}
3197 	} else {
3198 		rq_for_each_segment(bv, req, iter) {
3199 			dst = page_address(bv.bv_page) + bv.bv_offset;
3200 			last_tidaw = itcw_add_tidaw(itcw, 0x00,
3201 						    dst, bv.bv_len);
3202 			if (IS_ERR(last_tidaw)) {
3203 				ret = -EINVAL;
3204 				goto out_error;
3205 			}
3206 		}
3207 	}
3208 	last_tidaw->flags |= TIDAW_FLAGS_LAST;
3209 	last_tidaw->flags &= ~TIDAW_FLAGS_INSERT_CBC;
3210 	itcw_finalize(itcw);
3211 
3212 	if (blk_noretry_request(req) ||
3213 	    block->base->features & DASD_FEATURE_FAILFAST)
3214 		set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
3215 	cqr->cpmode = 1;
3216 	cqr->startdev = startdev;
3217 	cqr->memdev = startdev;
3218 	cqr->block = block;
3219 	cqr->expires = startdev->default_expires * HZ;	/* default 5 minutes */
3220 	cqr->lpm = startdev->path_data.ppm;
3221 	cqr->retries = startdev->default_retries;
3222 	cqr->buildclk = get_tod_clock();
3223 	cqr->status = DASD_CQR_FILLED;
3224 	return cqr;
3225 out_error:
3226 	dasd_sfree_request(cqr, startdev);
3227 	return ERR_PTR(ret);
3228 }
3229 
3230 static struct dasd_ccw_req *dasd_eckd_build_cp(struct dasd_device *startdev,
3231 					       struct dasd_block *block,
3232 					       struct request *req)
3233 {
3234 	int cmdrtd, cmdwtd;
3235 	int use_prefix;
3236 	int fcx_multitrack;
3237 	struct dasd_eckd_private *private;
3238 	struct dasd_device *basedev;
3239 	sector_t first_rec, last_rec;
3240 	sector_t first_trk, last_trk;
3241 	unsigned int first_offs, last_offs;
3242 	unsigned int blk_per_trk, blksize;
3243 	int cdlspecial;
3244 	unsigned int data_size;
3245 	struct dasd_ccw_req *cqr;
3246 
3247 	basedev = block->base;
3248 	private = (struct dasd_eckd_private *) basedev->private;
3249 
3250 	/* Calculate number of blocks/records per track. */
3251 	blksize = block->bp_block;
3252 	blk_per_trk = recs_per_track(&private->rdc_data, 0, blksize);
3253 	if (blk_per_trk == 0)
3254 		return ERR_PTR(-EINVAL);
3255 	/* Calculate record id of first and last block. */
3256 	first_rec = first_trk = blk_rq_pos(req) >> block->s2b_shift;
3257 	first_offs = sector_div(first_trk, blk_per_trk);
3258 	last_rec = last_trk =
3259 		(blk_rq_pos(req) + blk_rq_sectors(req) - 1) >> block->s2b_shift;
3260 	last_offs = sector_div(last_trk, blk_per_trk);
3261 	cdlspecial = (private->uses_cdl && first_rec < 2*blk_per_trk);
3262 
3263 	fcx_multitrack = private->features.feature[40] & 0x20;
3264 	data_size = blk_rq_bytes(req);
3265 	if (data_size % blksize)
3266 		return ERR_PTR(-EINVAL);
3267 	/* tpm write request add CBC data on each track boundary */
3268 	if (rq_data_dir(req) == WRITE)
3269 		data_size += (last_trk - first_trk) * 4;
3270 
3271 	/* is read track data and write track data in command mode supported? */
3272 	cmdrtd = private->features.feature[9] & 0x20;
3273 	cmdwtd = private->features.feature[12] & 0x40;
3274 	use_prefix = private->features.feature[8] & 0x01;
3275 
3276 	cqr = NULL;
3277 	if (cdlspecial || dasd_page_cache) {
3278 		/* do nothing, just fall through to the cmd mode single case */
3279 	} else if ((data_size <= private->fcx_max_data)
3280 		   && (fcx_multitrack || (first_trk == last_trk))) {
3281 		cqr = dasd_eckd_build_cp_tpm_track(startdev, block, req,
3282 						    first_rec, last_rec,
3283 						    first_trk, last_trk,
3284 						    first_offs, last_offs,
3285 						    blk_per_trk, blksize);
3286 		if (IS_ERR(cqr) && (PTR_ERR(cqr) != -EAGAIN) &&
3287 		    (PTR_ERR(cqr) != -ENOMEM))
3288 			cqr = NULL;
3289 	} else if (use_prefix &&
3290 		   (((rq_data_dir(req) == READ) && cmdrtd) ||
3291 		    ((rq_data_dir(req) == WRITE) && cmdwtd))) {
3292 		cqr = dasd_eckd_build_cp_cmd_track(startdev, block, req,
3293 						   first_rec, last_rec,
3294 						   first_trk, last_trk,
3295 						   first_offs, last_offs,
3296 						   blk_per_trk, blksize);
3297 		if (IS_ERR(cqr) && (PTR_ERR(cqr) != -EAGAIN) &&
3298 		    (PTR_ERR(cqr) != -ENOMEM))
3299 			cqr = NULL;
3300 	}
3301 	if (!cqr)
3302 		cqr = dasd_eckd_build_cp_cmd_single(startdev, block, req,
3303 						    first_rec, last_rec,
3304 						    first_trk, last_trk,
3305 						    first_offs, last_offs,
3306 						    blk_per_trk, blksize);
3307 	return cqr;
3308 }
3309 
3310 static struct dasd_ccw_req *dasd_raw_build_cp(struct dasd_device *startdev,
3311 					       struct dasd_block *block,
3312 					       struct request *req)
3313 {
3314 	unsigned long *idaws;
3315 	struct dasd_device *basedev;
3316 	struct dasd_ccw_req *cqr;
3317 	struct ccw1 *ccw;
3318 	struct req_iterator iter;
3319 	struct bio_vec bv;
3320 	char *dst;
3321 	unsigned char cmd;
3322 	unsigned int trkcount;
3323 	unsigned int seg_len, len_to_track_end;
3324 	unsigned int first_offs;
3325 	unsigned int cidaw, cplength, datasize;
3326 	sector_t first_trk, last_trk, sectors;
3327 	sector_t start_padding_sectors, end_sector_offset, end_padding_sectors;
3328 	unsigned int pfx_datasize;
3329 
3330 	/*
3331 	 * raw track access needs to be mutiple of 64k and on 64k boundary
3332 	 * For read requests we can fix an incorrect alignment by padding
3333 	 * the request with dummy pages.
3334 	 */
3335 	start_padding_sectors = blk_rq_pos(req) % DASD_RAW_SECTORS_PER_TRACK;
3336 	end_sector_offset = (blk_rq_pos(req) + blk_rq_sectors(req)) %
3337 		DASD_RAW_SECTORS_PER_TRACK;
3338 	end_padding_sectors = (DASD_RAW_SECTORS_PER_TRACK - end_sector_offset) %
3339 		DASD_RAW_SECTORS_PER_TRACK;
3340 	basedev = block->base;
3341 	if ((start_padding_sectors || end_padding_sectors) &&
3342 	    (rq_data_dir(req) == WRITE)) {
3343 		DBF_DEV_EVENT(DBF_ERR, basedev,
3344 			      "raw write not track aligned (%lu,%lu) req %p",
3345 			      start_padding_sectors, end_padding_sectors, req);
3346 		cqr = ERR_PTR(-EINVAL);
3347 		goto out;
3348 	}
3349 
3350 	first_trk = blk_rq_pos(req) / DASD_RAW_SECTORS_PER_TRACK;
3351 	last_trk = (blk_rq_pos(req) + blk_rq_sectors(req) - 1) /
3352 		DASD_RAW_SECTORS_PER_TRACK;
3353 	trkcount = last_trk - first_trk + 1;
3354 	first_offs = 0;
3355 
3356 	if (rq_data_dir(req) == READ)
3357 		cmd = DASD_ECKD_CCW_READ_TRACK;
3358 	else if (rq_data_dir(req) == WRITE)
3359 		cmd = DASD_ECKD_CCW_WRITE_FULL_TRACK;
3360 	else {
3361 		cqr = ERR_PTR(-EINVAL);
3362 		goto out;
3363 	}
3364 
3365 	/*
3366 	 * Raw track based I/O needs IDAWs for each page,
3367 	 * and not just for 64 bit addresses.
3368 	 */
3369 	cidaw = trkcount * DASD_RAW_BLOCK_PER_TRACK;
3370 
3371 	/* 1x prefix + one read/write ccw per track */
3372 	cplength = 1 + trkcount;
3373 
3374 	/*
3375 	 * struct PFX_eckd_data has up to 2 byte as extended parameter
3376 	 * this is needed for write full track and has to be mentioned
3377 	 * separately
3378 	 * add 8 instead of 2 to keep 8 byte boundary
3379 	 */
3380 	pfx_datasize = sizeof(struct PFX_eckd_data) + 8;
3381 
3382 	datasize = pfx_datasize + cidaw * sizeof(unsigned long long);
3383 
3384 	/* Allocate the ccw request. */
3385 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength,
3386 				   datasize, startdev);
3387 	if (IS_ERR(cqr))
3388 		goto out;
3389 	ccw = cqr->cpaddr;
3390 
3391 	if (prefix_LRE(ccw++, cqr->data, first_trk, last_trk, cmd,
3392 		       basedev, startdev, 1 /* format */, first_offs + 1,
3393 		       trkcount, 0, 0) == -EAGAIN) {
3394 		/* Clock not in sync and XRC is enabled.
3395 		 * Try again later.
3396 		 */
3397 		dasd_sfree_request(cqr, startdev);
3398 		cqr = ERR_PTR(-EAGAIN);
3399 		goto out;
3400 	}
3401 
3402 	idaws = (unsigned long *)(cqr->data + pfx_datasize);
3403 	len_to_track_end = 0;
3404 	if (start_padding_sectors) {
3405 		ccw[-1].flags |= CCW_FLAG_CC;
3406 		ccw->cmd_code = cmd;
3407 		/* maximum 3390 track size */
3408 		ccw->count = 57326;
3409 		/* 64k map to one track */
3410 		len_to_track_end = 65536 - start_padding_sectors * 512;
3411 		ccw->cda = (__u32)(addr_t)idaws;
3412 		ccw->flags |= CCW_FLAG_IDA;
3413 		ccw->flags |= CCW_FLAG_SLI;
3414 		ccw++;
3415 		for (sectors = 0; sectors < start_padding_sectors; sectors += 8)
3416 			idaws = idal_create_words(idaws, rawpadpage, PAGE_SIZE);
3417 	}
3418 	rq_for_each_segment(bv, req, iter) {
3419 		dst = page_address(bv.bv_page) + bv.bv_offset;
3420 		seg_len = bv.bv_len;
3421 		if (cmd == DASD_ECKD_CCW_READ_TRACK)
3422 			memset(dst, 0, seg_len);
3423 		if (!len_to_track_end) {
3424 			ccw[-1].flags |= CCW_FLAG_CC;
3425 			ccw->cmd_code = cmd;
3426 			/* maximum 3390 track size */
3427 			ccw->count = 57326;
3428 			/* 64k map to one track */
3429 			len_to_track_end = 65536;
3430 			ccw->cda = (__u32)(addr_t)idaws;
3431 			ccw->flags |= CCW_FLAG_IDA;
3432 			ccw->flags |= CCW_FLAG_SLI;
3433 			ccw++;
3434 		}
3435 		len_to_track_end -= seg_len;
3436 		idaws = idal_create_words(idaws, dst, seg_len);
3437 	}
3438 	for (sectors = 0; sectors < end_padding_sectors; sectors += 8)
3439 		idaws = idal_create_words(idaws, rawpadpage, PAGE_SIZE);
3440 	if (blk_noretry_request(req) ||
3441 	    block->base->features & DASD_FEATURE_FAILFAST)
3442 		set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
3443 	cqr->startdev = startdev;
3444 	cqr->memdev = startdev;
3445 	cqr->block = block;
3446 	cqr->expires = startdev->default_expires * HZ;
3447 	cqr->lpm = startdev->path_data.ppm;
3448 	cqr->retries = startdev->default_retries;
3449 	cqr->buildclk = get_tod_clock();
3450 	cqr->status = DASD_CQR_FILLED;
3451 
3452 	if (IS_ERR(cqr) && PTR_ERR(cqr) != -EAGAIN)
3453 		cqr = NULL;
3454 out:
3455 	return cqr;
3456 }
3457 
3458 
3459 static int
3460 dasd_eckd_free_cp(struct dasd_ccw_req *cqr, struct request *req)
3461 {
3462 	struct dasd_eckd_private *private;
3463 	struct ccw1 *ccw;
3464 	struct req_iterator iter;
3465 	struct bio_vec bv;
3466 	char *dst, *cda;
3467 	unsigned int blksize, blk_per_trk, off;
3468 	sector_t recid;
3469 	int status;
3470 
3471 	if (!dasd_page_cache)
3472 		goto out;
3473 	private = (struct dasd_eckd_private *) cqr->block->base->private;
3474 	blksize = cqr->block->bp_block;
3475 	blk_per_trk = recs_per_track(&private->rdc_data, 0, blksize);
3476 	recid = blk_rq_pos(req) >> cqr->block->s2b_shift;
3477 	ccw = cqr->cpaddr;
3478 	/* Skip over define extent & locate record. */
3479 	ccw++;
3480 	if (private->uses_cdl == 0 || recid > 2*blk_per_trk)
3481 		ccw++;
3482 	rq_for_each_segment(bv, req, iter) {
3483 		dst = page_address(bv.bv_page) + bv.bv_offset;
3484 		for (off = 0; off < bv.bv_len; off += blksize) {
3485 			/* Skip locate record. */
3486 			if (private->uses_cdl && recid <= 2*blk_per_trk)
3487 				ccw++;
3488 			if (dst) {
3489 				if (ccw->flags & CCW_FLAG_IDA)
3490 					cda = *((char **)((addr_t) ccw->cda));
3491 				else
3492 					cda = (char *)((addr_t) ccw->cda);
3493 				if (dst != cda) {
3494 					if (rq_data_dir(req) == READ)
3495 						memcpy(dst, cda, bv.bv_len);
3496 					kmem_cache_free(dasd_page_cache,
3497 					    (void *)((addr_t)cda & PAGE_MASK));
3498 				}
3499 				dst = NULL;
3500 			}
3501 			ccw++;
3502 			recid++;
3503 		}
3504 	}
3505 out:
3506 	status = cqr->status == DASD_CQR_DONE;
3507 	dasd_sfree_request(cqr, cqr->memdev);
3508 	return status;
3509 }
3510 
3511 /*
3512  * Modify ccw/tcw in cqr so it can be started on a base device.
3513  *
3514  * Note that this is not enough to restart the cqr!
3515  * Either reset cqr->startdev as well (summary unit check handling)
3516  * or restart via separate cqr (as in ERP handling).
3517  */
3518 void dasd_eckd_reset_ccw_to_base_io(struct dasd_ccw_req *cqr)
3519 {
3520 	struct ccw1 *ccw;
3521 	struct PFX_eckd_data *pfxdata;
3522 	struct tcw *tcw;
3523 	struct tccb *tccb;
3524 	struct dcw *dcw;
3525 
3526 	if (cqr->cpmode == 1) {
3527 		tcw = cqr->cpaddr;
3528 		tccb = tcw_get_tccb(tcw);
3529 		dcw = (struct dcw *)&tccb->tca[0];
3530 		pfxdata = (struct PFX_eckd_data *)&dcw->cd[0];
3531 		pfxdata->validity.verify_base = 0;
3532 		pfxdata->validity.hyper_pav = 0;
3533 	} else {
3534 		ccw = cqr->cpaddr;
3535 		pfxdata = cqr->data;
3536 		if (ccw->cmd_code == DASD_ECKD_CCW_PFX) {
3537 			pfxdata->validity.verify_base = 0;
3538 			pfxdata->validity.hyper_pav = 0;
3539 		}
3540 	}
3541 }
3542 
3543 #define DASD_ECKD_CHANQ_MAX_SIZE 4
3544 
3545 static struct dasd_ccw_req *dasd_eckd_build_alias_cp(struct dasd_device *base,
3546 						     struct dasd_block *block,
3547 						     struct request *req)
3548 {
3549 	struct dasd_eckd_private *private;
3550 	struct dasd_device *startdev;
3551 	unsigned long flags;
3552 	struct dasd_ccw_req *cqr;
3553 
3554 	startdev = dasd_alias_get_start_dev(base);
3555 	if (!startdev)
3556 		startdev = base;
3557 	private = (struct dasd_eckd_private *) startdev->private;
3558 	if (private->count >= DASD_ECKD_CHANQ_MAX_SIZE)
3559 		return ERR_PTR(-EBUSY);
3560 
3561 	spin_lock_irqsave(get_ccwdev_lock(startdev->cdev), flags);
3562 	private->count++;
3563 	if ((base->features & DASD_FEATURE_USERAW))
3564 		cqr = dasd_raw_build_cp(startdev, block, req);
3565 	else
3566 		cqr = dasd_eckd_build_cp(startdev, block, req);
3567 	if (IS_ERR(cqr))
3568 		private->count--;
3569 	spin_unlock_irqrestore(get_ccwdev_lock(startdev->cdev), flags);
3570 	return cqr;
3571 }
3572 
3573 static int dasd_eckd_free_alias_cp(struct dasd_ccw_req *cqr,
3574 				   struct request *req)
3575 {
3576 	struct dasd_eckd_private *private;
3577 	unsigned long flags;
3578 
3579 	spin_lock_irqsave(get_ccwdev_lock(cqr->memdev->cdev), flags);
3580 	private = (struct dasd_eckd_private *) cqr->memdev->private;
3581 	private->count--;
3582 	spin_unlock_irqrestore(get_ccwdev_lock(cqr->memdev->cdev), flags);
3583 	return dasd_eckd_free_cp(cqr, req);
3584 }
3585 
3586 static int
3587 dasd_eckd_fill_info(struct dasd_device * device,
3588 		    struct dasd_information2_t * info)
3589 {
3590 	struct dasd_eckd_private *private;
3591 
3592 	private = (struct dasd_eckd_private *) device->private;
3593 	info->label_block = 2;
3594 	info->FBA_layout = private->uses_cdl ? 0 : 1;
3595 	info->format = private->uses_cdl ? DASD_FORMAT_CDL : DASD_FORMAT_LDL;
3596 	info->characteristics_size = sizeof(struct dasd_eckd_characteristics);
3597 	memcpy(info->characteristics, &private->rdc_data,
3598 	       sizeof(struct dasd_eckd_characteristics));
3599 	info->confdata_size = min((unsigned long)private->conf_len,
3600 				  sizeof(info->configuration_data));
3601 	memcpy(info->configuration_data, private->conf_data,
3602 	       info->confdata_size);
3603 	return 0;
3604 }
3605 
3606 /*
3607  * SECTION: ioctl functions for eckd devices.
3608  */
3609 
3610 /*
3611  * Release device ioctl.
3612  * Buils a channel programm to releases a prior reserved
3613  * (see dasd_eckd_reserve) device.
3614  */
3615 static int
3616 dasd_eckd_release(struct dasd_device *device)
3617 {
3618 	struct dasd_ccw_req *cqr;
3619 	int rc;
3620 	struct ccw1 *ccw;
3621 	int useglobal;
3622 
3623 	if (!capable(CAP_SYS_ADMIN))
3624 		return -EACCES;
3625 
3626 	useglobal = 0;
3627 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1, 32, device);
3628 	if (IS_ERR(cqr)) {
3629 		mutex_lock(&dasd_reserve_mutex);
3630 		useglobal = 1;
3631 		cqr = &dasd_reserve_req->cqr;
3632 		memset(cqr, 0, sizeof(*cqr));
3633 		memset(&dasd_reserve_req->ccw, 0,
3634 		       sizeof(dasd_reserve_req->ccw));
3635 		cqr->cpaddr = &dasd_reserve_req->ccw;
3636 		cqr->data = &dasd_reserve_req->data;
3637 		cqr->magic = DASD_ECKD_MAGIC;
3638 	}
3639 	ccw = cqr->cpaddr;
3640 	ccw->cmd_code = DASD_ECKD_CCW_RELEASE;
3641 	ccw->flags |= CCW_FLAG_SLI;
3642 	ccw->count = 32;
3643 	ccw->cda = (__u32)(addr_t) cqr->data;
3644 	cqr->startdev = device;
3645 	cqr->memdev = device;
3646 	clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
3647 	set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
3648 	cqr->retries = 2;	/* set retry counter to enable basic ERP */
3649 	cqr->expires = 2 * HZ;
3650 	cqr->buildclk = get_tod_clock();
3651 	cqr->status = DASD_CQR_FILLED;
3652 
3653 	rc = dasd_sleep_on_immediatly(cqr);
3654 	if (!rc)
3655 		clear_bit(DASD_FLAG_IS_RESERVED, &device->flags);
3656 
3657 	if (useglobal)
3658 		mutex_unlock(&dasd_reserve_mutex);
3659 	else
3660 		dasd_sfree_request(cqr, cqr->memdev);
3661 	return rc;
3662 }
3663 
3664 /*
3665  * Reserve device ioctl.
3666  * Options are set to 'synchronous wait for interrupt' and
3667  * 'timeout the request'. This leads to a terminate IO if
3668  * the interrupt is outstanding for a certain time.
3669  */
3670 static int
3671 dasd_eckd_reserve(struct dasd_device *device)
3672 {
3673 	struct dasd_ccw_req *cqr;
3674 	int rc;
3675 	struct ccw1 *ccw;
3676 	int useglobal;
3677 
3678 	if (!capable(CAP_SYS_ADMIN))
3679 		return -EACCES;
3680 
3681 	useglobal = 0;
3682 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1, 32, device);
3683 	if (IS_ERR(cqr)) {
3684 		mutex_lock(&dasd_reserve_mutex);
3685 		useglobal = 1;
3686 		cqr = &dasd_reserve_req->cqr;
3687 		memset(cqr, 0, sizeof(*cqr));
3688 		memset(&dasd_reserve_req->ccw, 0,
3689 		       sizeof(dasd_reserve_req->ccw));
3690 		cqr->cpaddr = &dasd_reserve_req->ccw;
3691 		cqr->data = &dasd_reserve_req->data;
3692 		cqr->magic = DASD_ECKD_MAGIC;
3693 	}
3694 	ccw = cqr->cpaddr;
3695 	ccw->cmd_code = DASD_ECKD_CCW_RESERVE;
3696 	ccw->flags |= CCW_FLAG_SLI;
3697 	ccw->count = 32;
3698 	ccw->cda = (__u32)(addr_t) cqr->data;
3699 	cqr->startdev = device;
3700 	cqr->memdev = device;
3701 	clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
3702 	set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
3703 	cqr->retries = 2;	/* set retry counter to enable basic ERP */
3704 	cqr->expires = 2 * HZ;
3705 	cqr->buildclk = get_tod_clock();
3706 	cqr->status = DASD_CQR_FILLED;
3707 
3708 	rc = dasd_sleep_on_immediatly(cqr);
3709 	if (!rc)
3710 		set_bit(DASD_FLAG_IS_RESERVED, &device->flags);
3711 
3712 	if (useglobal)
3713 		mutex_unlock(&dasd_reserve_mutex);
3714 	else
3715 		dasd_sfree_request(cqr, cqr->memdev);
3716 	return rc;
3717 }
3718 
3719 /*
3720  * Steal lock ioctl - unconditional reserve device.
3721  * Buils a channel programm to break a device's reservation.
3722  * (unconditional reserve)
3723  */
3724 static int
3725 dasd_eckd_steal_lock(struct dasd_device *device)
3726 {
3727 	struct dasd_ccw_req *cqr;
3728 	int rc;
3729 	struct ccw1 *ccw;
3730 	int useglobal;
3731 
3732 	if (!capable(CAP_SYS_ADMIN))
3733 		return -EACCES;
3734 
3735 	useglobal = 0;
3736 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1, 32, device);
3737 	if (IS_ERR(cqr)) {
3738 		mutex_lock(&dasd_reserve_mutex);
3739 		useglobal = 1;
3740 		cqr = &dasd_reserve_req->cqr;
3741 		memset(cqr, 0, sizeof(*cqr));
3742 		memset(&dasd_reserve_req->ccw, 0,
3743 		       sizeof(dasd_reserve_req->ccw));
3744 		cqr->cpaddr = &dasd_reserve_req->ccw;
3745 		cqr->data = &dasd_reserve_req->data;
3746 		cqr->magic = DASD_ECKD_MAGIC;
3747 	}
3748 	ccw = cqr->cpaddr;
3749 	ccw->cmd_code = DASD_ECKD_CCW_SLCK;
3750 	ccw->flags |= CCW_FLAG_SLI;
3751 	ccw->count = 32;
3752 	ccw->cda = (__u32)(addr_t) cqr->data;
3753 	cqr->startdev = device;
3754 	cqr->memdev = device;
3755 	clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
3756 	set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
3757 	cqr->retries = 2;	/* set retry counter to enable basic ERP */
3758 	cqr->expires = 2 * HZ;
3759 	cqr->buildclk = get_tod_clock();
3760 	cqr->status = DASD_CQR_FILLED;
3761 
3762 	rc = dasd_sleep_on_immediatly(cqr);
3763 	if (!rc)
3764 		set_bit(DASD_FLAG_IS_RESERVED, &device->flags);
3765 
3766 	if (useglobal)
3767 		mutex_unlock(&dasd_reserve_mutex);
3768 	else
3769 		dasd_sfree_request(cqr, cqr->memdev);
3770 	return rc;
3771 }
3772 
3773 /*
3774  * SNID - Sense Path Group ID
3775  * This ioctl may be used in situations where I/O is stalled due to
3776  * a reserve, so if the normal dasd_smalloc_request fails, we use the
3777  * preallocated dasd_reserve_req.
3778  */
3779 static int dasd_eckd_snid(struct dasd_device *device,
3780 			  void __user *argp)
3781 {
3782 	struct dasd_ccw_req *cqr;
3783 	int rc;
3784 	struct ccw1 *ccw;
3785 	int useglobal;
3786 	struct dasd_snid_ioctl_data usrparm;
3787 
3788 	if (!capable(CAP_SYS_ADMIN))
3789 		return -EACCES;
3790 
3791 	if (copy_from_user(&usrparm, argp, sizeof(usrparm)))
3792 		return -EFAULT;
3793 
3794 	useglobal = 0;
3795 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1,
3796 				   sizeof(struct dasd_snid_data), device);
3797 	if (IS_ERR(cqr)) {
3798 		mutex_lock(&dasd_reserve_mutex);
3799 		useglobal = 1;
3800 		cqr = &dasd_reserve_req->cqr;
3801 		memset(cqr, 0, sizeof(*cqr));
3802 		memset(&dasd_reserve_req->ccw, 0,
3803 		       sizeof(dasd_reserve_req->ccw));
3804 		cqr->cpaddr = &dasd_reserve_req->ccw;
3805 		cqr->data = &dasd_reserve_req->data;
3806 		cqr->magic = DASD_ECKD_MAGIC;
3807 	}
3808 	ccw = cqr->cpaddr;
3809 	ccw->cmd_code = DASD_ECKD_CCW_SNID;
3810 	ccw->flags |= CCW_FLAG_SLI;
3811 	ccw->count = 12;
3812 	ccw->cda = (__u32)(addr_t) cqr->data;
3813 	cqr->startdev = device;
3814 	cqr->memdev = device;
3815 	clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
3816 	set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
3817 	set_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags);
3818 	cqr->retries = 5;
3819 	cqr->expires = 10 * HZ;
3820 	cqr->buildclk = get_tod_clock();
3821 	cqr->status = DASD_CQR_FILLED;
3822 	cqr->lpm = usrparm.path_mask;
3823 
3824 	rc = dasd_sleep_on_immediatly(cqr);
3825 	/* verify that I/O processing didn't modify the path mask */
3826 	if (!rc && usrparm.path_mask && (cqr->lpm != usrparm.path_mask))
3827 		rc = -EIO;
3828 	if (!rc) {
3829 		usrparm.data = *((struct dasd_snid_data *)cqr->data);
3830 		if (copy_to_user(argp, &usrparm, sizeof(usrparm)))
3831 			rc = -EFAULT;
3832 	}
3833 
3834 	if (useglobal)
3835 		mutex_unlock(&dasd_reserve_mutex);
3836 	else
3837 		dasd_sfree_request(cqr, cqr->memdev);
3838 	return rc;
3839 }
3840 
3841 /*
3842  * Read performance statistics
3843  */
3844 static int
3845 dasd_eckd_performance(struct dasd_device *device, void __user *argp)
3846 {
3847 	struct dasd_psf_prssd_data *prssdp;
3848 	struct dasd_rssd_perf_stats_t *stats;
3849 	struct dasd_ccw_req *cqr;
3850 	struct ccw1 *ccw;
3851 	int rc;
3852 
3853 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */  + 1 /* RSSD */,
3854 				   (sizeof(struct dasd_psf_prssd_data) +
3855 				    sizeof(struct dasd_rssd_perf_stats_t)),
3856 				   device);
3857 	if (IS_ERR(cqr)) {
3858 		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
3859 			    "Could not allocate initialization request");
3860 		return PTR_ERR(cqr);
3861 	}
3862 	cqr->startdev = device;
3863 	cqr->memdev = device;
3864 	cqr->retries = 0;
3865 	clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
3866 	cqr->expires = 10 * HZ;
3867 
3868 	/* Prepare for Read Subsystem Data */
3869 	prssdp = (struct dasd_psf_prssd_data *) cqr->data;
3870 	memset(prssdp, 0, sizeof(struct dasd_psf_prssd_data));
3871 	prssdp->order = PSF_ORDER_PRSSD;
3872 	prssdp->suborder = 0x01;	/* Performance Statistics */
3873 	prssdp->varies[1] = 0x01;	/* Perf Statistics for the Subsystem */
3874 
3875 	ccw = cqr->cpaddr;
3876 	ccw->cmd_code = DASD_ECKD_CCW_PSF;
3877 	ccw->count = sizeof(struct dasd_psf_prssd_data);
3878 	ccw->flags |= CCW_FLAG_CC;
3879 	ccw->cda = (__u32)(addr_t) prssdp;
3880 
3881 	/* Read Subsystem Data - Performance Statistics */
3882 	stats = (struct dasd_rssd_perf_stats_t *) (prssdp + 1);
3883 	memset(stats, 0, sizeof(struct dasd_rssd_perf_stats_t));
3884 
3885 	ccw++;
3886 	ccw->cmd_code = DASD_ECKD_CCW_RSSD;
3887 	ccw->count = sizeof(struct dasd_rssd_perf_stats_t);
3888 	ccw->cda = (__u32)(addr_t) stats;
3889 
3890 	cqr->buildclk = get_tod_clock();
3891 	cqr->status = DASD_CQR_FILLED;
3892 	rc = dasd_sleep_on(cqr);
3893 	if (rc == 0) {
3894 		prssdp = (struct dasd_psf_prssd_data *) cqr->data;
3895 		stats = (struct dasd_rssd_perf_stats_t *) (prssdp + 1);
3896 		if (copy_to_user(argp, stats,
3897 				 sizeof(struct dasd_rssd_perf_stats_t)))
3898 			rc = -EFAULT;
3899 	}
3900 	dasd_sfree_request(cqr, cqr->memdev);
3901 	return rc;
3902 }
3903 
3904 /*
3905  * Get attributes (cache operations)
3906  * Returnes the cache attributes used in Define Extend (DE).
3907  */
3908 static int
3909 dasd_eckd_get_attrib(struct dasd_device *device, void __user *argp)
3910 {
3911 	struct dasd_eckd_private *private =
3912 		(struct dasd_eckd_private *)device->private;
3913 	struct attrib_data_t attrib = private->attrib;
3914 	int rc;
3915 
3916         if (!capable(CAP_SYS_ADMIN))
3917                 return -EACCES;
3918 	if (!argp)
3919                 return -EINVAL;
3920 
3921 	rc = 0;
3922 	if (copy_to_user(argp, (long *) &attrib,
3923 			 sizeof(struct attrib_data_t)))
3924 		rc = -EFAULT;
3925 
3926 	return rc;
3927 }
3928 
3929 /*
3930  * Set attributes (cache operations)
3931  * Stores the attributes for cache operation to be used in Define Extend (DE).
3932  */
3933 static int
3934 dasd_eckd_set_attrib(struct dasd_device *device, void __user *argp)
3935 {
3936 	struct dasd_eckd_private *private =
3937 		(struct dasd_eckd_private *)device->private;
3938 	struct attrib_data_t attrib;
3939 
3940 	if (!capable(CAP_SYS_ADMIN))
3941 		return -EACCES;
3942 	if (!argp)
3943 		return -EINVAL;
3944 
3945 	if (copy_from_user(&attrib, argp, sizeof(struct attrib_data_t)))
3946 		return -EFAULT;
3947 	private->attrib = attrib;
3948 
3949 	dev_info(&device->cdev->dev,
3950 		 "The DASD cache mode was set to %x (%i cylinder prestage)\n",
3951 		 private->attrib.operation, private->attrib.nr_cyl);
3952 	return 0;
3953 }
3954 
3955 /*
3956  * Issue syscall I/O to EMC Symmetrix array.
3957  * CCWs are PSF and RSSD
3958  */
3959 static int dasd_symm_io(struct dasd_device *device, void __user *argp)
3960 {
3961 	struct dasd_symmio_parms usrparm;
3962 	char *psf_data, *rssd_result;
3963 	struct dasd_ccw_req *cqr;
3964 	struct ccw1 *ccw;
3965 	char psf0, psf1;
3966 	int rc;
3967 
3968 	if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
3969 		return -EACCES;
3970 	psf0 = psf1 = 0;
3971 
3972 	/* Copy parms from caller */
3973 	rc = -EFAULT;
3974 	if (copy_from_user(&usrparm, argp, sizeof(usrparm)))
3975 		goto out;
3976 	if (is_compat_task() || sizeof(long) == 4) {
3977 		/* Make sure pointers are sane even on 31 bit. */
3978 		rc = -EINVAL;
3979 		if ((usrparm.psf_data >> 32) != 0)
3980 			goto out;
3981 		if ((usrparm.rssd_result >> 32) != 0)
3982 			goto out;
3983 		usrparm.psf_data &= 0x7fffffffULL;
3984 		usrparm.rssd_result &= 0x7fffffffULL;
3985 	}
3986 	/* alloc I/O data area */
3987 	psf_data = kzalloc(usrparm.psf_data_len, GFP_KERNEL | GFP_DMA);
3988 	rssd_result = kzalloc(usrparm.rssd_result_len, GFP_KERNEL | GFP_DMA);
3989 	if (!psf_data || !rssd_result) {
3990 		rc = -ENOMEM;
3991 		goto out_free;
3992 	}
3993 
3994 	/* get syscall header from user space */
3995 	rc = -EFAULT;
3996 	if (copy_from_user(psf_data,
3997 			   (void __user *)(unsigned long) usrparm.psf_data,
3998 			   usrparm.psf_data_len))
3999 		goto out_free;
4000 	psf0 = psf_data[0];
4001 	psf1 = psf_data[1];
4002 
4003 	/* setup CCWs for PSF + RSSD */
4004 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 2 , 0, device);
4005 	if (IS_ERR(cqr)) {
4006 		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
4007 			"Could not allocate initialization request");
4008 		rc = PTR_ERR(cqr);
4009 		goto out_free;
4010 	}
4011 
4012 	cqr->startdev = device;
4013 	cqr->memdev = device;
4014 	cqr->retries = 3;
4015 	cqr->expires = 10 * HZ;
4016 	cqr->buildclk = get_tod_clock();
4017 	cqr->status = DASD_CQR_FILLED;
4018 
4019 	/* Build the ccws */
4020 	ccw = cqr->cpaddr;
4021 
4022 	/* PSF ccw */
4023 	ccw->cmd_code = DASD_ECKD_CCW_PSF;
4024 	ccw->count = usrparm.psf_data_len;
4025 	ccw->flags |= CCW_FLAG_CC;
4026 	ccw->cda = (__u32)(addr_t) psf_data;
4027 
4028 	ccw++;
4029 
4030 	/* RSSD ccw  */
4031 	ccw->cmd_code = DASD_ECKD_CCW_RSSD;
4032 	ccw->count = usrparm.rssd_result_len;
4033 	ccw->flags = CCW_FLAG_SLI ;
4034 	ccw->cda = (__u32)(addr_t) rssd_result;
4035 
4036 	rc = dasd_sleep_on(cqr);
4037 	if (rc)
4038 		goto out_sfree;
4039 
4040 	rc = -EFAULT;
4041 	if (copy_to_user((void __user *)(unsigned long) usrparm.rssd_result,
4042 			   rssd_result, usrparm.rssd_result_len))
4043 		goto out_sfree;
4044 	rc = 0;
4045 
4046 out_sfree:
4047 	dasd_sfree_request(cqr, cqr->memdev);
4048 out_free:
4049 	kfree(rssd_result);
4050 	kfree(psf_data);
4051 out:
4052 	DBF_DEV_EVENT(DBF_WARNING, device,
4053 		      "Symmetrix ioctl (0x%02x 0x%02x): rc=%d",
4054 		      (int) psf0, (int) psf1, rc);
4055 	return rc;
4056 }
4057 
4058 static int
4059 dasd_eckd_ioctl(struct dasd_block *block, unsigned int cmd, void __user *argp)
4060 {
4061 	struct dasd_device *device = block->base;
4062 
4063 	switch (cmd) {
4064 	case BIODASDGATTR:
4065 		return dasd_eckd_get_attrib(device, argp);
4066 	case BIODASDSATTR:
4067 		return dasd_eckd_set_attrib(device, argp);
4068 	case BIODASDPSRD:
4069 		return dasd_eckd_performance(device, argp);
4070 	case BIODASDRLSE:
4071 		return dasd_eckd_release(device);
4072 	case BIODASDRSRV:
4073 		return dasd_eckd_reserve(device);
4074 	case BIODASDSLCK:
4075 		return dasd_eckd_steal_lock(device);
4076 	case BIODASDSNID:
4077 		return dasd_eckd_snid(device, argp);
4078 	case BIODASDSYMMIO:
4079 		return dasd_symm_io(device, argp);
4080 	default:
4081 		return -ENOTTY;
4082 	}
4083 }
4084 
4085 /*
4086  * Dump the range of CCWs into 'page' buffer
4087  * and return number of printed chars.
4088  */
4089 static int
4090 dasd_eckd_dump_ccw_range(struct ccw1 *from, struct ccw1 *to, char *page)
4091 {
4092 	int len, count;
4093 	char *datap;
4094 
4095 	len = 0;
4096 	while (from <= to) {
4097 		len += sprintf(page + len, PRINTK_HEADER
4098 			       " CCW %p: %08X %08X DAT:",
4099 			       from, ((int *) from)[0], ((int *) from)[1]);
4100 
4101 		/* get pointer to data (consider IDALs) */
4102 		if (from->flags & CCW_FLAG_IDA)
4103 			datap = (char *) *((addr_t *) (addr_t) from->cda);
4104 		else
4105 			datap = (char *) ((addr_t) from->cda);
4106 
4107 		/* dump data (max 32 bytes) */
4108 		for (count = 0; count < from->count && count < 32; count++) {
4109 			if (count % 8 == 0) len += sprintf(page + len, " ");
4110 			if (count % 4 == 0) len += sprintf(page + len, " ");
4111 			len += sprintf(page + len, "%02x", datap[count]);
4112 		}
4113 		len += sprintf(page + len, "\n");
4114 		from++;
4115 	}
4116 	return len;
4117 }
4118 
4119 static void
4120 dasd_eckd_dump_sense_dbf(struct dasd_device *device, struct irb *irb,
4121 			 char *reason)
4122 {
4123 	u64 *sense;
4124 	u64 *stat;
4125 
4126 	sense = (u64 *) dasd_get_sense(irb);
4127 	stat = (u64 *) &irb->scsw;
4128 	if (sense) {
4129 		DBF_DEV_EVENT(DBF_EMERG, device, "%s: %016llx %08x : "
4130 			      "%016llx %016llx %016llx %016llx",
4131 			      reason, *stat, *((u32 *) (stat + 1)),
4132 			      sense[0], sense[1], sense[2], sense[3]);
4133 	} else {
4134 		DBF_DEV_EVENT(DBF_EMERG, device, "%s: %016llx %08x : %s",
4135 			      reason, *stat, *((u32 *) (stat + 1)),
4136 			      "NO VALID SENSE");
4137 	}
4138 }
4139 
4140 /*
4141  * Print sense data and related channel program.
4142  * Parts are printed because printk buffer is only 1024 bytes.
4143  */
4144 static void dasd_eckd_dump_sense_ccw(struct dasd_device *device,
4145 				 struct dasd_ccw_req *req, struct irb *irb)
4146 {
4147 	char *page;
4148 	struct ccw1 *first, *last, *fail, *from, *to;
4149 	int len, sl, sct;
4150 
4151 	page = (char *) get_zeroed_page(GFP_ATOMIC);
4152 	if (page == NULL) {
4153 		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
4154 			      "No memory to dump sense data\n");
4155 		return;
4156 	}
4157 	/* dump the sense data */
4158 	len = sprintf(page, PRINTK_HEADER
4159 		      " I/O status report for device %s:\n",
4160 		      dev_name(&device->cdev->dev));
4161 	len += sprintf(page + len, PRINTK_HEADER
4162 		       " in req: %p CC:%02X FC:%02X AC:%02X SC:%02X DS:%02X "
4163 		       "CS:%02X RC:%d\n",
4164 		       req, scsw_cc(&irb->scsw), scsw_fctl(&irb->scsw),
4165 		       scsw_actl(&irb->scsw), scsw_stctl(&irb->scsw),
4166 		       scsw_dstat(&irb->scsw), scsw_cstat(&irb->scsw),
4167 		       req ? req->intrc : 0);
4168 	len += sprintf(page + len, PRINTK_HEADER
4169 		       " device %s: Failing CCW: %p\n",
4170 		       dev_name(&device->cdev->dev),
4171 		       (void *) (addr_t) irb->scsw.cmd.cpa);
4172 	if (irb->esw.esw0.erw.cons) {
4173 		for (sl = 0; sl < 4; sl++) {
4174 			len += sprintf(page + len, PRINTK_HEADER
4175 				       " Sense(hex) %2d-%2d:",
4176 				       (8 * sl), ((8 * sl) + 7));
4177 
4178 			for (sct = 0; sct < 8; sct++) {
4179 				len += sprintf(page + len, " %02x",
4180 					       irb->ecw[8 * sl + sct]);
4181 			}
4182 			len += sprintf(page + len, "\n");
4183 		}
4184 
4185 		if (irb->ecw[27] & DASD_SENSE_BIT_0) {
4186 			/* 24 Byte Sense Data */
4187 			sprintf(page + len, PRINTK_HEADER
4188 				" 24 Byte: %x MSG %x, "
4189 				"%s MSGb to SYSOP\n",
4190 				irb->ecw[7] >> 4, irb->ecw[7] & 0x0f,
4191 				irb->ecw[1] & 0x10 ? "" : "no");
4192 		} else {
4193 			/* 32 Byte Sense Data */
4194 			sprintf(page + len, PRINTK_HEADER
4195 				" 32 Byte: Format: %x "
4196 				"Exception class %x\n",
4197 				irb->ecw[6] & 0x0f, irb->ecw[22] >> 4);
4198 		}
4199 	} else {
4200 		sprintf(page + len, PRINTK_HEADER
4201 			" SORRY - NO VALID SENSE AVAILABLE\n");
4202 	}
4203 	printk(KERN_ERR "%s", page);
4204 
4205 	if (req) {
4206 		/* req == NULL for unsolicited interrupts */
4207 		/* dump the Channel Program (max 140 Bytes per line) */
4208 		/* Count CCW and print first CCWs (maximum 1024 % 140 = 7) */
4209 		first = req->cpaddr;
4210 		for (last = first; last->flags & (CCW_FLAG_CC | CCW_FLAG_DC); last++);
4211 		to = min(first + 6, last);
4212 		len = sprintf(page, PRINTK_HEADER
4213 			      " Related CP in req: %p\n", req);
4214 		dasd_eckd_dump_ccw_range(first, to, page + len);
4215 		printk(KERN_ERR "%s", page);
4216 
4217 		/* print failing CCW area (maximum 4) */
4218 		/* scsw->cda is either valid or zero  */
4219 		len = 0;
4220 		from = ++to;
4221 		fail = (struct ccw1 *)(addr_t)
4222 				irb->scsw.cmd.cpa; /* failing CCW */
4223 		if (from <  fail - 2) {
4224 			from = fail - 2;     /* there is a gap - print header */
4225 			len += sprintf(page, PRINTK_HEADER "......\n");
4226 		}
4227 		to = min(fail + 1, last);
4228 		len += dasd_eckd_dump_ccw_range(from, to, page + len);
4229 
4230 		/* print last CCWs (maximum 2) */
4231 		from = max(from, ++to);
4232 		if (from < last - 1) {
4233 			from = last - 1;     /* there is a gap - print header */
4234 			len += sprintf(page + len, PRINTK_HEADER "......\n");
4235 		}
4236 		len += dasd_eckd_dump_ccw_range(from, last, page + len);
4237 		if (len > 0)
4238 			printk(KERN_ERR "%s", page);
4239 	}
4240 	free_page((unsigned long) page);
4241 }
4242 
4243 
4244 /*
4245  * Print sense data from a tcw.
4246  */
4247 static void dasd_eckd_dump_sense_tcw(struct dasd_device *device,
4248 				 struct dasd_ccw_req *req, struct irb *irb)
4249 {
4250 	char *page;
4251 	int len, sl, sct, residual;
4252 	struct tsb *tsb;
4253 	u8 *sense, *rcq;
4254 
4255 	page = (char *) get_zeroed_page(GFP_ATOMIC);
4256 	if (page == NULL) {
4257 		DBF_DEV_EVENT(DBF_WARNING, device, " %s",
4258 			    "No memory to dump sense data");
4259 		return;
4260 	}
4261 	/* dump the sense data */
4262 	len = sprintf(page, PRINTK_HEADER
4263 		      " I/O status report for device %s:\n",
4264 		      dev_name(&device->cdev->dev));
4265 	len += sprintf(page + len, PRINTK_HEADER
4266 		       " in req: %p CC:%02X FC:%02X AC:%02X SC:%02X DS:%02X "
4267 		       "CS:%02X fcxs:%02X schxs:%02X RC:%d\n",
4268 		       req, scsw_cc(&irb->scsw), scsw_fctl(&irb->scsw),
4269 		       scsw_actl(&irb->scsw), scsw_stctl(&irb->scsw),
4270 		       scsw_dstat(&irb->scsw), scsw_cstat(&irb->scsw),
4271 		       irb->scsw.tm.fcxs, irb->scsw.tm.schxs,
4272 		       req ? req->intrc : 0);
4273 	len += sprintf(page + len, PRINTK_HEADER
4274 		       " device %s: Failing TCW: %p\n",
4275 		       dev_name(&device->cdev->dev),
4276 		       (void *) (addr_t) irb->scsw.tm.tcw);
4277 
4278 	tsb = NULL;
4279 	sense = NULL;
4280 	if (irb->scsw.tm.tcw && (irb->scsw.tm.fcxs & 0x01))
4281 		tsb = tcw_get_tsb(
4282 			(struct tcw *)(unsigned long)irb->scsw.tm.tcw);
4283 
4284 	if (tsb) {
4285 		len += sprintf(page + len, PRINTK_HEADER
4286 			       " tsb->length %d\n", tsb->length);
4287 		len += sprintf(page + len, PRINTK_HEADER
4288 			       " tsb->flags %x\n", tsb->flags);
4289 		len += sprintf(page + len, PRINTK_HEADER
4290 			       " tsb->dcw_offset %d\n", tsb->dcw_offset);
4291 		len += sprintf(page + len, PRINTK_HEADER
4292 			       " tsb->count %d\n", tsb->count);
4293 		residual = tsb->count - 28;
4294 		len += sprintf(page + len, PRINTK_HEADER
4295 			       " residual %d\n", residual);
4296 
4297 		switch (tsb->flags & 0x07) {
4298 		case 1:	/* tsa_iostat */
4299 			len += sprintf(page + len, PRINTK_HEADER
4300 			       " tsb->tsa.iostat.dev_time %d\n",
4301 				       tsb->tsa.iostat.dev_time);
4302 			len += sprintf(page + len, PRINTK_HEADER
4303 			       " tsb->tsa.iostat.def_time %d\n",
4304 				       tsb->tsa.iostat.def_time);
4305 			len += sprintf(page + len, PRINTK_HEADER
4306 			       " tsb->tsa.iostat.queue_time %d\n",
4307 				       tsb->tsa.iostat.queue_time);
4308 			len += sprintf(page + len, PRINTK_HEADER
4309 			       " tsb->tsa.iostat.dev_busy_time %d\n",
4310 				       tsb->tsa.iostat.dev_busy_time);
4311 			len += sprintf(page + len, PRINTK_HEADER
4312 			       " tsb->tsa.iostat.dev_act_time %d\n",
4313 				       tsb->tsa.iostat.dev_act_time);
4314 			sense = tsb->tsa.iostat.sense;
4315 			break;
4316 		case 2: /* ts_ddpc */
4317 			len += sprintf(page + len, PRINTK_HEADER
4318 			       " tsb->tsa.ddpc.rc %d\n", tsb->tsa.ddpc.rc);
4319 			for (sl = 0; sl < 2; sl++) {
4320 				len += sprintf(page + len, PRINTK_HEADER
4321 					       " tsb->tsa.ddpc.rcq %2d-%2d: ",
4322 					       (8 * sl), ((8 * sl) + 7));
4323 				rcq = tsb->tsa.ddpc.rcq;
4324 				for (sct = 0; sct < 8; sct++) {
4325 					len += sprintf(page + len, " %02x",
4326 						       rcq[8 * sl + sct]);
4327 				}
4328 				len += sprintf(page + len, "\n");
4329 			}
4330 			sense = tsb->tsa.ddpc.sense;
4331 			break;
4332 		case 3: /* tsa_intrg */
4333 			len += sprintf(page + len, PRINTK_HEADER
4334 				      " tsb->tsa.intrg.: not supportet yet\n");
4335 			break;
4336 		}
4337 
4338 		if (sense) {
4339 			for (sl = 0; sl < 4; sl++) {
4340 				len += sprintf(page + len, PRINTK_HEADER
4341 					       " Sense(hex) %2d-%2d:",
4342 					       (8 * sl), ((8 * sl) + 7));
4343 				for (sct = 0; sct < 8; sct++) {
4344 					len += sprintf(page + len, " %02x",
4345 						       sense[8 * sl + sct]);
4346 				}
4347 				len += sprintf(page + len, "\n");
4348 			}
4349 
4350 			if (sense[27] & DASD_SENSE_BIT_0) {
4351 				/* 24 Byte Sense Data */
4352 				sprintf(page + len, PRINTK_HEADER
4353 					" 24 Byte: %x MSG %x, "
4354 					"%s MSGb to SYSOP\n",
4355 					sense[7] >> 4, sense[7] & 0x0f,
4356 					sense[1] & 0x10 ? "" : "no");
4357 			} else {
4358 				/* 32 Byte Sense Data */
4359 				sprintf(page + len, PRINTK_HEADER
4360 					" 32 Byte: Format: %x "
4361 					"Exception class %x\n",
4362 					sense[6] & 0x0f, sense[22] >> 4);
4363 			}
4364 		} else {
4365 			sprintf(page + len, PRINTK_HEADER
4366 				" SORRY - NO VALID SENSE AVAILABLE\n");
4367 		}
4368 	} else {
4369 		sprintf(page + len, PRINTK_HEADER
4370 			" SORRY - NO TSB DATA AVAILABLE\n");
4371 	}
4372 	printk(KERN_ERR "%s", page);
4373 	free_page((unsigned long) page);
4374 }
4375 
4376 static void dasd_eckd_dump_sense(struct dasd_device *device,
4377 				 struct dasd_ccw_req *req, struct irb *irb)
4378 {
4379 	if (scsw_is_tm(&irb->scsw))
4380 		dasd_eckd_dump_sense_tcw(device, req, irb);
4381 	else
4382 		dasd_eckd_dump_sense_ccw(device, req, irb);
4383 }
4384 
4385 static int dasd_eckd_pm_freeze(struct dasd_device *device)
4386 {
4387 	/*
4388 	 * the device should be disconnected from our LCU structure
4389 	 * on restore we will reconnect it and reread LCU specific
4390 	 * information like PAV support that might have changed
4391 	 */
4392 	dasd_alias_remove_device(device);
4393 	dasd_alias_disconnect_device_from_lcu(device);
4394 
4395 	return 0;
4396 }
4397 
4398 static int dasd_eckd_restore_device(struct dasd_device *device)
4399 {
4400 	struct dasd_eckd_private *private;
4401 	struct dasd_eckd_characteristics temp_rdc_data;
4402 	int rc;
4403 	struct dasd_uid temp_uid;
4404 	unsigned long flags;
4405 	unsigned long cqr_flags = 0;
4406 
4407 	private = (struct dasd_eckd_private *) device->private;
4408 
4409 	/* Read Configuration Data */
4410 	dasd_eckd_read_conf(device);
4411 
4412 	dasd_eckd_get_uid(device, &temp_uid);
4413 	/* Generate device unique id */
4414 	rc = dasd_eckd_generate_uid(device);
4415 	spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
4416 	if (memcmp(&private->uid, &temp_uid, sizeof(struct dasd_uid)) != 0)
4417 		dev_err(&device->cdev->dev, "The UID of the DASD has "
4418 			"changed\n");
4419 	spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
4420 	if (rc)
4421 		goto out_err;
4422 
4423 	/* register lcu with alias handling, enable PAV if this is a new lcu */
4424 	rc = dasd_alias_make_device_known_to_lcu(device);
4425 	if (rc)
4426 		return rc;
4427 
4428 	set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr_flags);
4429 	dasd_eckd_validate_server(device, cqr_flags);
4430 
4431 	/* RE-Read Configuration Data */
4432 	dasd_eckd_read_conf(device);
4433 
4434 	/* Read Feature Codes */
4435 	dasd_eckd_read_features(device);
4436 
4437 	/* Read Device Characteristics */
4438 	rc = dasd_generic_read_dev_chars(device, DASD_ECKD_MAGIC,
4439 					 &temp_rdc_data, 64);
4440 	if (rc) {
4441 		DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
4442 				"Read device characteristic failed, rc=%d", rc);
4443 		goto out_err;
4444 	}
4445 	spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
4446 	memcpy(&private->rdc_data, &temp_rdc_data, sizeof(temp_rdc_data));
4447 	spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
4448 
4449 	/* add device to alias management */
4450 	dasd_alias_add_device(device);
4451 
4452 	return 0;
4453 
4454 out_err:
4455 	return -1;
4456 }
4457 
4458 static int dasd_eckd_reload_device(struct dasd_device *device)
4459 {
4460 	struct dasd_eckd_private *private;
4461 	int rc, old_base;
4462 	char print_uid[60];
4463 	struct dasd_uid uid;
4464 	unsigned long flags;
4465 
4466 	private = (struct dasd_eckd_private *) device->private;
4467 
4468 	spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
4469 	old_base = private->uid.base_unit_addr;
4470 	spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
4471 
4472 	/* Read Configuration Data */
4473 	rc = dasd_eckd_read_conf(device);
4474 	if (rc)
4475 		goto out_err;
4476 
4477 	rc = dasd_eckd_generate_uid(device);
4478 	if (rc)
4479 		goto out_err;
4480 	/*
4481 	 * update unit address configuration and
4482 	 * add device to alias management
4483 	 */
4484 	dasd_alias_update_add_device(device);
4485 
4486 	dasd_eckd_get_uid(device, &uid);
4487 
4488 	if (old_base != uid.base_unit_addr) {
4489 		if (strlen(uid.vduit) > 0)
4490 			snprintf(print_uid, sizeof(print_uid),
4491 				 "%s.%s.%04x.%02x.%s", uid.vendor, uid.serial,
4492 				 uid.ssid, uid.base_unit_addr, uid.vduit);
4493 		else
4494 			snprintf(print_uid, sizeof(print_uid),
4495 				 "%s.%s.%04x.%02x", uid.vendor, uid.serial,
4496 				 uid.ssid, uid.base_unit_addr);
4497 
4498 		dev_info(&device->cdev->dev,
4499 			 "An Alias device was reassigned to a new base device "
4500 			 "with UID: %s\n", print_uid);
4501 	}
4502 	return 0;
4503 
4504 out_err:
4505 	return -1;
4506 }
4507 
4508 static int dasd_eckd_read_message_buffer(struct dasd_device *device,
4509 					 struct dasd_rssd_messages *messages,
4510 					 __u8 lpum)
4511 {
4512 	struct dasd_rssd_messages *message_buf;
4513 	struct dasd_psf_prssd_data *prssdp;
4514 	struct dasd_eckd_private *private;
4515 	struct dasd_ccw_req *cqr;
4516 	struct ccw1 *ccw;
4517 	int rc;
4518 
4519 	private = (struct dasd_eckd_private *) device->private;
4520 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */	+ 1 /* RSSD */,
4521 				   (sizeof(struct dasd_psf_prssd_data) +
4522 				    sizeof(struct dasd_rssd_messages)),
4523 				   device);
4524 	if (IS_ERR(cqr)) {
4525 		DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
4526 				"Could not allocate read message buffer request");
4527 		return PTR_ERR(cqr);
4528 	}
4529 
4530 	cqr->startdev = device;
4531 	cqr->memdev = device;
4532 	cqr->block = NULL;
4533 	cqr->retries = 256;
4534 	cqr->expires = 10 * HZ;
4535 
4536 	/* we need to check for messages on exactly this path */
4537 	set_bit(DASD_CQR_VERIFY_PATH, &cqr->flags);
4538 	cqr->lpm = lpum;
4539 
4540 	/* Prepare for Read Subsystem Data */
4541 	prssdp = (struct dasd_psf_prssd_data *) cqr->data;
4542 	memset(prssdp, 0, sizeof(struct dasd_psf_prssd_data));
4543 	prssdp->order = PSF_ORDER_PRSSD;
4544 	prssdp->suborder = 0x03;	/* Message Buffer */
4545 	/* all other bytes of prssdp must be zero */
4546 
4547 	ccw = cqr->cpaddr;
4548 	ccw->cmd_code = DASD_ECKD_CCW_PSF;
4549 	ccw->count = sizeof(struct dasd_psf_prssd_data);
4550 	ccw->flags |= CCW_FLAG_CC;
4551 	ccw->flags |= CCW_FLAG_SLI;
4552 	ccw->cda = (__u32)(addr_t) prssdp;
4553 
4554 	/* Read Subsystem Data - message buffer */
4555 	message_buf = (struct dasd_rssd_messages *) (prssdp + 1);
4556 	memset(message_buf, 0, sizeof(struct dasd_rssd_messages));
4557 
4558 	ccw++;
4559 	ccw->cmd_code = DASD_ECKD_CCW_RSSD;
4560 	ccw->count = sizeof(struct dasd_rssd_messages);
4561 	ccw->flags |= CCW_FLAG_SLI;
4562 	ccw->cda = (__u32)(addr_t) message_buf;
4563 
4564 	cqr->buildclk = get_tod_clock();
4565 	cqr->status = DASD_CQR_FILLED;
4566 	rc = dasd_sleep_on_immediatly(cqr);
4567 	if (rc == 0) {
4568 		prssdp = (struct dasd_psf_prssd_data *) cqr->data;
4569 		message_buf = (struct dasd_rssd_messages *)
4570 			(prssdp + 1);
4571 		memcpy(messages, message_buf,
4572 		       sizeof(struct dasd_rssd_messages));
4573 	} else
4574 		DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
4575 				"Reading messages failed with rc=%d\n"
4576 				, rc);
4577 	dasd_sfree_request(cqr, cqr->memdev);
4578 	return rc;
4579 }
4580 
4581 /*
4582  * Perform Subsystem Function - CUIR response
4583  */
4584 static int
4585 dasd_eckd_psf_cuir_response(struct dasd_device *device, int response,
4586 			    __u32 message_id,
4587 			    struct channel_path_desc *desc,
4588 			    struct subchannel_id sch_id)
4589 {
4590 	struct dasd_psf_cuir_response *psf_cuir;
4591 	struct dasd_ccw_req *cqr;
4592 	struct ccw1 *ccw;
4593 	int rc;
4594 
4595 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */ ,
4596 				  sizeof(struct dasd_psf_cuir_response),
4597 				  device);
4598 
4599 	if (IS_ERR(cqr)) {
4600 		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
4601 			   "Could not allocate PSF-CUIR request");
4602 		return PTR_ERR(cqr);
4603 	}
4604 
4605 	psf_cuir = (struct dasd_psf_cuir_response *)cqr->data;
4606 	psf_cuir->order = PSF_ORDER_CUIR_RESPONSE;
4607 	psf_cuir->cc = response;
4608 	if (desc)
4609 		psf_cuir->chpid = desc->chpid;
4610 	psf_cuir->message_id = message_id;
4611 	psf_cuir->cssid = sch_id.cssid;
4612 	psf_cuir->ssid = sch_id.ssid;
4613 
4614 	ccw = cqr->cpaddr;
4615 	ccw->cmd_code = DASD_ECKD_CCW_PSF;
4616 	ccw->cda = (__u32)(addr_t)psf_cuir;
4617 	ccw->count = sizeof(struct dasd_psf_cuir_response);
4618 
4619 	cqr->startdev = device;
4620 	cqr->memdev = device;
4621 	cqr->block = NULL;
4622 	cqr->retries = 256;
4623 	cqr->expires = 10*HZ;
4624 	cqr->buildclk = get_tod_clock();
4625 	cqr->status = DASD_CQR_FILLED;
4626 
4627 	rc = dasd_sleep_on(cqr);
4628 
4629 	dasd_sfree_request(cqr, cqr->memdev);
4630 	return rc;
4631 }
4632 
4633 static int dasd_eckd_cuir_change_state(struct dasd_device *device, __u8 lpum)
4634 {
4635 	unsigned long flags;
4636 	__u8 tbcpm;
4637 
4638 	spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
4639 	tbcpm = device->path_data.opm & ~lpum;
4640 	if (tbcpm) {
4641 		device->path_data.opm = tbcpm;
4642 		device->path_data.cuirpm |= lpum;
4643 	}
4644 	spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
4645 	return tbcpm ? 0 : PSF_CUIR_LAST_PATH;
4646 }
4647 
4648 /*
4649  * walk through all devices and quiesce them
4650  * if it is the last path return error
4651  *
4652  * if only part of the devices are quiesced and an error
4653  * occurs no onlining necessary, the storage server will
4654  * notify the already set offline devices again
4655  */
4656 static int dasd_eckd_cuir_quiesce(struct dasd_device *device, __u8 lpum,
4657 				 struct channel_path_desc *desc,
4658 				 struct subchannel_id sch_id)
4659 {
4660 	struct alias_pav_group *pavgroup, *tempgroup;
4661 	struct dasd_eckd_private *private;
4662 	struct dasd_device *dev, *n;
4663 	int rc;
4664 
4665 	private = (struct dasd_eckd_private *) device->private;
4666 	rc = 0;
4667 
4668 	/* active devices */
4669 	list_for_each_entry_safe(dev, n,
4670 				 &private->lcu->active_devices,
4671 				 alias_list) {
4672 		rc = dasd_eckd_cuir_change_state(dev, lpum);
4673 		if (rc)
4674 			goto out;
4675 	}
4676 
4677 	/* inactive devices */
4678 	list_for_each_entry_safe(dev, n,
4679 				 &private->lcu->inactive_devices,
4680 				 alias_list) {
4681 		rc = dasd_eckd_cuir_change_state(dev, lpum);
4682 		if (rc)
4683 			goto out;
4684 	}
4685 
4686 	/* devices in PAV groups */
4687 	list_for_each_entry_safe(pavgroup, tempgroup,
4688 				 &private->lcu->grouplist, group) {
4689 		list_for_each_entry_safe(dev, n, &pavgroup->baselist,
4690 					 alias_list) {
4691 			rc = dasd_eckd_cuir_change_state(dev, lpum);
4692 			if (rc)
4693 				goto out;
4694 		}
4695 		list_for_each_entry_safe(dev, n, &pavgroup->aliaslist,
4696 					 alias_list) {
4697 			rc = dasd_eckd_cuir_change_state(dev, lpum);
4698 			if (rc)
4699 				goto out;
4700 		}
4701 	}
4702 
4703 	pr_warn("Service on the storage server caused path %x.%02x to go offline",
4704 		sch_id.cssid, desc ? desc->chpid : 0);
4705 	rc = PSF_CUIR_COMPLETED;
4706 out:
4707 	return rc;
4708 }
4709 
4710 static int dasd_eckd_cuir_resume(struct dasd_device *device, __u8 lpum,
4711 				 struct channel_path_desc *desc,
4712 				 struct subchannel_id sch_id)
4713 {
4714 	struct alias_pav_group *pavgroup, *tempgroup;
4715 	struct dasd_eckd_private *private;
4716 	struct dasd_device *dev, *n;
4717 
4718 	pr_info("Path %x.%02x is back online after service on the storage server",
4719 		sch_id.cssid, desc ? desc->chpid : 0);
4720 	private = (struct dasd_eckd_private *) device->private;
4721 
4722 	/*
4723 	 * the path may have been added through a generic path event before
4724 	 * only trigger path verification if the path is not already in use
4725 	 */
4726 
4727 	list_for_each_entry_safe(dev, n,
4728 				 &private->lcu->active_devices,
4729 				 alias_list) {
4730 		if (!(dev->path_data.opm & lpum)) {
4731 			dev->path_data.tbvpm |= lpum;
4732 			dasd_schedule_device_bh(dev);
4733 		}
4734 	}
4735 
4736 	list_for_each_entry_safe(dev, n,
4737 				 &private->lcu->inactive_devices,
4738 				 alias_list) {
4739 		if (!(dev->path_data.opm & lpum)) {
4740 			dev->path_data.tbvpm |= lpum;
4741 			dasd_schedule_device_bh(dev);
4742 		}
4743 	}
4744 
4745 	/* devices in PAV groups */
4746 	list_for_each_entry_safe(pavgroup, tempgroup,
4747 				 &private->lcu->grouplist,
4748 				 group) {
4749 		list_for_each_entry_safe(dev, n,
4750 					 &pavgroup->baselist,
4751 					 alias_list) {
4752 			if (!(dev->path_data.opm & lpum)) {
4753 				dev->path_data.tbvpm |= lpum;
4754 				dasd_schedule_device_bh(dev);
4755 			}
4756 		}
4757 		list_for_each_entry_safe(dev, n,
4758 					 &pavgroup->aliaslist,
4759 					 alias_list) {
4760 			if (!(dev->path_data.opm & lpum)) {
4761 				dev->path_data.tbvpm |= lpum;
4762 				dasd_schedule_device_bh(dev);
4763 			}
4764 		}
4765 	}
4766 	return PSF_CUIR_COMPLETED;
4767 }
4768 
4769 static void dasd_eckd_handle_cuir(struct dasd_device *device, void *messages,
4770 				 __u8 lpum)
4771 {
4772 	struct dasd_cuir_message *cuir = messages;
4773 	struct channel_path_desc *desc;
4774 	struct subchannel_id sch_id;
4775 	int pos, response;
4776 	ccw_device_get_schid(device->cdev, &sch_id);
4777 
4778 	/* get position of path in mask */
4779 	pos = 8 - ffs(lpum);
4780 	/* get channel path descriptor from this position */
4781 	desc = ccw_device_get_chp_desc(device->cdev, pos);
4782 
4783 	if (cuir->code == CUIR_QUIESCE) {
4784 		/* quiesce */
4785 		response = dasd_eckd_cuir_quiesce(device, lpum, desc, sch_id);
4786 	} else if (cuir->code == CUIR_RESUME) {
4787 		/* resume */
4788 		response = dasd_eckd_cuir_resume(device, lpum, desc, sch_id);
4789 	} else
4790 		response = PSF_CUIR_NOT_SUPPORTED;
4791 
4792 	dasd_eckd_psf_cuir_response(device, response, cuir->message_id,
4793 				    desc, sch_id);
4794 
4795 	/* free descriptor copy */
4796 	kfree(desc);
4797 }
4798 
4799 static void dasd_eckd_check_attention_work(struct work_struct *work)
4800 {
4801 	struct check_attention_work_data *data;
4802 	struct dasd_rssd_messages *messages;
4803 	struct dasd_device *device;
4804 	int rc;
4805 
4806 	data = container_of(work, struct check_attention_work_data, worker);
4807 	device = data->device;
4808 
4809 	messages = kzalloc(sizeof(*messages), GFP_KERNEL);
4810 	if (!messages) {
4811 		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
4812 			      "Could not allocate attention message buffer");
4813 		goto out;
4814 	}
4815 
4816 	rc = dasd_eckd_read_message_buffer(device, messages, data->lpum);
4817 	if (rc)
4818 		goto out;
4819 
4820 	if (messages->length == ATTENTION_LENGTH_CUIR &&
4821 	    messages->format == ATTENTION_FORMAT_CUIR)
4822 		dasd_eckd_handle_cuir(device, messages, data->lpum);
4823 
4824 out:
4825 	dasd_put_device(device);
4826 	kfree(messages);
4827 	kfree(data);
4828 }
4829 
4830 static int dasd_eckd_check_attention(struct dasd_device *device, __u8 lpum)
4831 {
4832 	struct check_attention_work_data *data;
4833 
4834 	data = kzalloc(sizeof(*data), GFP_ATOMIC);
4835 	if (!data)
4836 		return -ENOMEM;
4837 	INIT_WORK(&data->worker, dasd_eckd_check_attention_work);
4838 	dasd_get_device(device);
4839 	data->device = device;
4840 	data->lpum = lpum;
4841 	schedule_work(&data->worker);
4842 	return 0;
4843 }
4844 
4845 static struct ccw_driver dasd_eckd_driver = {
4846 	.driver = {
4847 		.name	= "dasd-eckd",
4848 		.owner	= THIS_MODULE,
4849 	},
4850 	.ids	     = dasd_eckd_ids,
4851 	.probe	     = dasd_eckd_probe,
4852 	.remove      = dasd_generic_remove,
4853 	.set_offline = dasd_generic_set_offline,
4854 	.set_online  = dasd_eckd_set_online,
4855 	.notify      = dasd_generic_notify,
4856 	.path_event  = dasd_generic_path_event,
4857 	.shutdown    = dasd_generic_shutdown,
4858 	.freeze      = dasd_generic_pm_freeze,
4859 	.thaw	     = dasd_generic_restore_device,
4860 	.restore     = dasd_generic_restore_device,
4861 	.uc_handler  = dasd_generic_uc_handler,
4862 	.int_class   = IRQIO_DAS,
4863 };
4864 
4865 /*
4866  * max_blocks is dependent on the amount of storage that is available
4867  * in the static io buffer for each device. Currently each device has
4868  * 8192 bytes (=2 pages). For 64 bit one dasd_mchunkt_t structure has
4869  * 24 bytes, the struct dasd_ccw_req has 136 bytes and each block can use
4870  * up to 16 bytes (8 for the ccw and 8 for the idal pointer). In
4871  * addition we have one define extent ccw + 16 bytes of data and one
4872  * locate record ccw + 16 bytes of data. That makes:
4873  * (8192 - 24 - 136 - 8 - 16 - 8 - 16) / 16 = 499 blocks at maximum.
4874  * We want to fit two into the available memory so that we can immediately
4875  * start the next request if one finishes off. That makes 249.5 blocks
4876  * for one request. Give a little safety and the result is 240.
4877  */
4878 static struct dasd_discipline dasd_eckd_discipline = {
4879 	.owner = THIS_MODULE,
4880 	.name = "ECKD",
4881 	.ebcname = "ECKD",
4882 	.max_blocks = 190,
4883 	.check_device = dasd_eckd_check_characteristics,
4884 	.uncheck_device = dasd_eckd_uncheck_device,
4885 	.do_analysis = dasd_eckd_do_analysis,
4886 	.verify_path = dasd_eckd_verify_path,
4887 	.basic_to_ready = dasd_eckd_basic_to_ready,
4888 	.online_to_ready = dasd_eckd_online_to_ready,
4889 	.basic_to_known = dasd_eckd_basic_to_known,
4890 	.fill_geometry = dasd_eckd_fill_geometry,
4891 	.start_IO = dasd_start_IO,
4892 	.term_IO = dasd_term_IO,
4893 	.handle_terminated_request = dasd_eckd_handle_terminated_request,
4894 	.format_device = dasd_eckd_format_device,
4895 	.erp_action = dasd_eckd_erp_action,
4896 	.erp_postaction = dasd_eckd_erp_postaction,
4897 	.check_for_device_change = dasd_eckd_check_for_device_change,
4898 	.build_cp = dasd_eckd_build_alias_cp,
4899 	.free_cp = dasd_eckd_free_alias_cp,
4900 	.dump_sense = dasd_eckd_dump_sense,
4901 	.dump_sense_dbf = dasd_eckd_dump_sense_dbf,
4902 	.fill_info = dasd_eckd_fill_info,
4903 	.ioctl = dasd_eckd_ioctl,
4904 	.freeze = dasd_eckd_pm_freeze,
4905 	.restore = dasd_eckd_restore_device,
4906 	.reload = dasd_eckd_reload_device,
4907 	.get_uid = dasd_eckd_get_uid,
4908 	.kick_validate = dasd_eckd_kick_validate_server,
4909 	.check_attention = dasd_eckd_check_attention,
4910 };
4911 
4912 static int __init
4913 dasd_eckd_init(void)
4914 {
4915 	int ret;
4916 
4917 	ASCEBC(dasd_eckd_discipline.ebcname, 4);
4918 	dasd_reserve_req = kmalloc(sizeof(*dasd_reserve_req),
4919 				   GFP_KERNEL | GFP_DMA);
4920 	if (!dasd_reserve_req)
4921 		return -ENOMEM;
4922 	path_verification_worker = kmalloc(sizeof(*path_verification_worker),
4923 				   GFP_KERNEL | GFP_DMA);
4924 	if (!path_verification_worker) {
4925 		kfree(dasd_reserve_req);
4926 		return -ENOMEM;
4927 	}
4928 	rawpadpage = (void *)__get_free_page(GFP_KERNEL);
4929 	if (!rawpadpage) {
4930 		kfree(path_verification_worker);
4931 		kfree(dasd_reserve_req);
4932 		return -ENOMEM;
4933 	}
4934 	ret = ccw_driver_register(&dasd_eckd_driver);
4935 	if (!ret)
4936 		wait_for_device_probe();
4937 	else {
4938 		kfree(path_verification_worker);
4939 		kfree(dasd_reserve_req);
4940 		free_page((unsigned long)rawpadpage);
4941 	}
4942 	return ret;
4943 }
4944 
4945 static void __exit
4946 dasd_eckd_cleanup(void)
4947 {
4948 	ccw_driver_unregister(&dasd_eckd_driver);
4949 	kfree(path_verification_worker);
4950 	kfree(dasd_reserve_req);
4951 	free_page((unsigned long)rawpadpage);
4952 }
4953 
4954 module_init(dasd_eckd_init);
4955 module_exit(dasd_eckd_cleanup);
4956