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