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