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