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