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