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