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