• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
3  *		    Horst Hummel <Horst.Hummel@de.ibm.com>
4  *		    Carsten Otte <Cotte@de.ibm.com>
5  *		    Martin Schwidefsky <schwidefsky@de.ibm.com>
6  * Bugreports.to..: <Linux390@de.ibm.com>
7  * Copyright IBM Corp. 1999, 2009
8  * EMC Symmetrix ioctl Copyright EMC Corporation, 2008
9  * Author.........: Nigel Hislop <hislop_nigel@emc.com>
10  */
11 
12 #define KMSG_COMPONENT "dasd-eckd"
13 
14 #include <linux/stddef.h>
15 #include <linux/kernel.h>
16 #include <linux/slab.h>
17 #include <linux/hdreg.h>	/* HDIO_GETGEO			    */
18 #include <linux/bio.h>
19 #include <linux/module.h>
20 #include <linux/compat.h>
21 #include <linux/init.h>
22 
23 #include <asm/css_chars.h>
24 #include <asm/debug.h>
25 #include <asm/idals.h>
26 #include <asm/ebcdic.h>
27 #include <asm/io.h>
28 #include <asm/uaccess.h>
29 #include <asm/cio.h>
30 #include <asm/ccwdev.h>
31 #include <asm/itcw.h>
32 
33 #include "dasd_int.h"
34 #include "dasd_eckd.h"
35 
36 #ifdef PRINTK_HEADER
37 #undef PRINTK_HEADER
38 #endif				/* PRINTK_HEADER */
39 #define PRINTK_HEADER "dasd(eckd):"
40 
41 #define ECKD_C0(i) (i->home_bytes)
42 #define ECKD_F(i) (i->formula)
43 #define ECKD_F1(i) (ECKD_F(i)==0x01?(i->factors.f_0x01.f1):\
44 		    (i->factors.f_0x02.f1))
45 #define ECKD_F2(i) (ECKD_F(i)==0x01?(i->factors.f_0x01.f2):\
46 		    (i->factors.f_0x02.f2))
47 #define ECKD_F3(i) (ECKD_F(i)==0x01?(i->factors.f_0x01.f3):\
48 		    (i->factors.f_0x02.f3))
49 #define ECKD_F4(i) (ECKD_F(i)==0x02?(i->factors.f_0x02.f4):0)
50 #define ECKD_F5(i) (ECKD_F(i)==0x02?(i->factors.f_0x02.f5):0)
51 #define ECKD_F6(i) (i->factor6)
52 #define ECKD_F7(i) (i->factor7)
53 #define ECKD_F8(i) (i->factor8)
54 
55 /*
56  * raw track access always map to 64k in memory
57  * so it maps to 16 blocks of 4k per track
58  */
59 #define DASD_RAW_BLOCK_PER_TRACK 16
60 #define DASD_RAW_BLOCKSIZE 4096
61 /* 64k are 128 x 512 byte sectors  */
62 #define DASD_RAW_SECTORS_PER_TRACK 128
63 
64 MODULE_LICENSE("GPL");
65 
66 static struct dasd_discipline dasd_eckd_discipline;
67 
68 /* The ccw bus type uses this table to find devices that it sends to
69  * dasd_eckd_probe */
70 static struct ccw_device_id dasd_eckd_ids[] = {
71 	{ CCW_DEVICE_DEVTYPE (0x3990, 0, 0x3390, 0), .driver_info = 0x1},
72 	{ CCW_DEVICE_DEVTYPE (0x2105, 0, 0x3390, 0), .driver_info = 0x2},
73 	{ CCW_DEVICE_DEVTYPE (0x3880, 0, 0x3380, 0), .driver_info = 0x3},
74 	{ CCW_DEVICE_DEVTYPE (0x3990, 0, 0x3380, 0), .driver_info = 0x4},
75 	{ CCW_DEVICE_DEVTYPE (0x2105, 0, 0x3380, 0), .driver_info = 0x5},
76 	{ CCW_DEVICE_DEVTYPE (0x9343, 0, 0x9345, 0), .driver_info = 0x6},
77 	{ CCW_DEVICE_DEVTYPE (0x2107, 0, 0x3390, 0), .driver_info = 0x7},
78 	{ CCW_DEVICE_DEVTYPE (0x2107, 0, 0x3380, 0), .driver_info = 0x8},
79 	{ CCW_DEVICE_DEVTYPE (0x1750, 0, 0x3390, 0), .driver_info = 0x9},
80 	{ CCW_DEVICE_DEVTYPE (0x1750, 0, 0x3380, 0), .driver_info = 0xa},
81 	{ /* end of list */ },
82 };
83 
84 MODULE_DEVICE_TABLE(ccw, dasd_eckd_ids);
85 
86 static struct ccw_driver dasd_eckd_driver; /* see below */
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 /* definitions for the path verification worker */
101 struct path_verification_work_data {
102 	struct work_struct worker;
103 	struct dasd_device *device;
104 	struct dasd_ccw_req cqr;
105 	struct ccw1 ccw;
106 	__u8 rcd_buffer[DASD_ECKD_RCD_DATA_SIZE];
107 	int isglobal;
108 	__u8 tbvpm;
109 };
110 static struct path_verification_work_data *path_verification_worker;
111 static DEFINE_MUTEX(dasd_path_verification_mutex);
112 
113 /* initial attempt at a probe function. this can be simplified once
114  * the other detection code is gone */
115 static int
dasd_eckd_probe(struct ccw_device * cdev)116 dasd_eckd_probe (struct ccw_device *cdev)
117 {
118 	int ret;
119 
120 	/* set ECKD specific ccw-device options */
121 	ret = ccw_device_set_options(cdev, CCWDEV_ALLOW_FORCE |
122 				     CCWDEV_DO_PATHGROUP | CCWDEV_DO_MULTIPATH);
123 	if (ret) {
124 		DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
125 				"dasd_eckd_probe: could not set "
126 				"ccw-device options");
127 		return ret;
128 	}
129 	ret = dasd_generic_probe(cdev, &dasd_eckd_discipline);
130 	return ret;
131 }
132 
133 static int
dasd_eckd_set_online(struct ccw_device * cdev)134 dasd_eckd_set_online(struct ccw_device *cdev)
135 {
136 	return dasd_generic_set_online(cdev, &dasd_eckd_discipline);
137 }
138 
139 static const int sizes_trk0[] = { 28, 148, 84 };
140 #define LABEL_SIZE 140
141 
142 /* head and record addresses of count_area read in analysis ccw */
143 static const int count_area_head[] = { 0, 0, 0, 0, 2 };
144 static const int count_area_rec[] = { 1, 2, 3, 4, 1 };
145 
146 static inline unsigned int
round_up_multiple(unsigned int no,unsigned int mult)147 round_up_multiple(unsigned int no, unsigned int mult)
148 {
149 	int rem = no % mult;
150 	return (rem ? no - rem + mult : no);
151 }
152 
153 static inline unsigned int
ceil_quot(unsigned int d1,unsigned int d2)154 ceil_quot(unsigned int d1, unsigned int d2)
155 {
156 	return (d1 + (d2 - 1)) / d2;
157 }
158 
159 static unsigned int
recs_per_track(struct dasd_eckd_characteristics * rdc,unsigned int kl,unsigned int dl)160 recs_per_track(struct dasd_eckd_characteristics * rdc,
161 	       unsigned int kl, unsigned int dl)
162 {
163 	int dn, kn;
164 
165 	switch (rdc->dev_type) {
166 	case 0x3380:
167 		if (kl)
168 			return 1499 / (15 + 7 + ceil_quot(kl + 12, 32) +
169 				       ceil_quot(dl + 12, 32));
170 		else
171 			return 1499 / (15 + ceil_quot(dl + 12, 32));
172 	case 0x3390:
173 		dn = ceil_quot(dl + 6, 232) + 1;
174 		if (kl) {
175 			kn = ceil_quot(kl + 6, 232) + 1;
176 			return 1729 / (10 + 9 + ceil_quot(kl + 6 * kn, 34) +
177 				       9 + ceil_quot(dl + 6 * dn, 34));
178 		} else
179 			return 1729 / (10 + 9 + ceil_quot(dl + 6 * dn, 34));
180 	case 0x9345:
181 		dn = ceil_quot(dl + 6, 232) + 1;
182 		if (kl) {
183 			kn = ceil_quot(kl + 6, 232) + 1;
184 			return 1420 / (18 + 7 + ceil_quot(kl + 6 * kn, 34) +
185 				       ceil_quot(dl + 6 * dn, 34));
186 		} else
187 			return 1420 / (18 + 7 + ceil_quot(dl + 6 * dn, 34));
188 	}
189 	return 0;
190 }
191 
set_ch_t(struct ch_t * geo,__u32 cyl,__u8 head)192 static void set_ch_t(struct ch_t *geo, __u32 cyl, __u8 head)
193 {
194 	geo->cyl = (__u16) cyl;
195 	geo->head = cyl >> 16;
196 	geo->head <<= 4;
197 	geo->head |= head;
198 }
199 
200 static int
check_XRC(struct ccw1 * de_ccw,struct DE_eckd_data * data,struct dasd_device * device)201 check_XRC (struct ccw1         *de_ccw,
202            struct DE_eckd_data *data,
203            struct dasd_device  *device)
204 {
205         struct dasd_eckd_private *private;
206 	int rc;
207 
208         private = (struct dasd_eckd_private *) device->private;
209 	if (!private->rdc_data.facilities.XRC_supported)
210 		return 0;
211 
212         /* switch on System Time Stamp - needed for XRC Support */
213 	data->ga_extended |= 0x08; /* switch on 'Time Stamp Valid'   */
214 	data->ga_extended |= 0x02; /* switch on 'Extended Parameter' */
215 
216 	rc = get_sync_clock(&data->ep_sys_time);
217 	/* Ignore return code if sync clock is switched off. */
218 	if (rc == -EOPNOTSUPP || rc == -EACCES)
219 		rc = 0;
220 
221 	de_ccw->count = sizeof(struct DE_eckd_data);
222 	de_ccw->flags |= CCW_FLAG_SLI;
223 	return rc;
224 }
225 
226 static int
define_extent(struct ccw1 * ccw,struct DE_eckd_data * data,unsigned int trk,unsigned int totrk,int cmd,struct dasd_device * device)227 define_extent(struct ccw1 *ccw, struct DE_eckd_data *data, unsigned int trk,
228 	      unsigned int totrk, int cmd, struct dasd_device *device)
229 {
230 	struct dasd_eckd_private *private;
231 	u32 begcyl, endcyl;
232 	u16 heads, beghead, endhead;
233 	int rc = 0;
234 
235 	private = (struct dasd_eckd_private *) device->private;
236 
237 	ccw->cmd_code = DASD_ECKD_CCW_DEFINE_EXTENT;
238 	ccw->flags = 0;
239 	ccw->count = 16;
240 	ccw->cda = (__u32) __pa(data);
241 
242 	memset(data, 0, sizeof(struct DE_eckd_data));
243 	switch (cmd) {
244 	case DASD_ECKD_CCW_READ_HOME_ADDRESS:
245 	case DASD_ECKD_CCW_READ_RECORD_ZERO:
246 	case DASD_ECKD_CCW_READ:
247 	case DASD_ECKD_CCW_READ_MT:
248 	case DASD_ECKD_CCW_READ_CKD:
249 	case DASD_ECKD_CCW_READ_CKD_MT:
250 	case DASD_ECKD_CCW_READ_KD:
251 	case DASD_ECKD_CCW_READ_KD_MT:
252 	case DASD_ECKD_CCW_READ_COUNT:
253 		data->mask.perm = 0x1;
254 		data->attributes.operation = private->attrib.operation;
255 		break;
256 	case DASD_ECKD_CCW_WRITE:
257 	case DASD_ECKD_CCW_WRITE_MT:
258 	case DASD_ECKD_CCW_WRITE_KD:
259 	case DASD_ECKD_CCW_WRITE_KD_MT:
260 		data->mask.perm = 0x02;
261 		data->attributes.operation = private->attrib.operation;
262 		rc = check_XRC (ccw, data, device);
263 		break;
264 	case DASD_ECKD_CCW_WRITE_CKD:
265 	case DASD_ECKD_CCW_WRITE_CKD_MT:
266 		data->attributes.operation = DASD_BYPASS_CACHE;
267 		rc = check_XRC (ccw, data, device);
268 		break;
269 	case DASD_ECKD_CCW_ERASE:
270 	case DASD_ECKD_CCW_WRITE_HOME_ADDRESS:
271 	case DASD_ECKD_CCW_WRITE_RECORD_ZERO:
272 		data->mask.perm = 0x3;
273 		data->mask.auth = 0x1;
274 		data->attributes.operation = DASD_BYPASS_CACHE;
275 		rc = check_XRC (ccw, data, device);
276 		break;
277 	default:
278 		dev_err(&device->cdev->dev,
279 			"0x%x is not a known command\n", cmd);
280 		break;
281 	}
282 
283 	data->attributes.mode = 0x3;	/* ECKD */
284 
285 	if ((private->rdc_data.cu_type == 0x2105 ||
286 	     private->rdc_data.cu_type == 0x2107 ||
287 	     private->rdc_data.cu_type == 0x1750)
288 	    && !(private->uses_cdl && trk < 2))
289 		data->ga_extended |= 0x40; /* Regular Data Format Mode */
290 
291 	heads = private->rdc_data.trk_per_cyl;
292 	begcyl = trk / heads;
293 	beghead = trk % heads;
294 	endcyl = totrk / heads;
295 	endhead = totrk % heads;
296 
297 	/* check for sequential prestage - enhance cylinder range */
298 	if (data->attributes.operation == DASD_SEQ_PRESTAGE ||
299 	    data->attributes.operation == DASD_SEQ_ACCESS) {
300 
301 		if (endcyl + private->attrib.nr_cyl < private->real_cyl)
302 			endcyl += private->attrib.nr_cyl;
303 		else
304 			endcyl = (private->real_cyl - 1);
305 	}
306 
307 	set_ch_t(&data->beg_ext, begcyl, beghead);
308 	set_ch_t(&data->end_ext, endcyl, endhead);
309 	return rc;
310 }
311 
check_XRC_on_prefix(struct PFX_eckd_data * pfxdata,struct dasd_device * device)312 static int check_XRC_on_prefix(struct PFX_eckd_data *pfxdata,
313 			       struct dasd_device  *device)
314 {
315 	struct dasd_eckd_private *private;
316 	int rc;
317 
318 	private = (struct dasd_eckd_private *) device->private;
319 	if (!private->rdc_data.facilities.XRC_supported)
320 		return 0;
321 
322 	/* switch on System Time Stamp - needed for XRC Support */
323 	pfxdata->define_extent.ga_extended |= 0x08; /* 'Time Stamp Valid'   */
324 	pfxdata->define_extent.ga_extended |= 0x02; /* 'Extended Parameter' */
325 	pfxdata->validity.time_stamp = 1;	    /* 'Time Stamp Valid'   */
326 
327 	rc = get_sync_clock(&pfxdata->define_extent.ep_sys_time);
328 	/* Ignore return code if sync clock is switched off. */
329 	if (rc == -EOPNOTSUPP || rc == -EACCES)
330 		rc = 0;
331 	return rc;
332 }
333 
fill_LRE_data(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)334 static void fill_LRE_data(struct LRE_eckd_data *data, unsigned int trk,
335 			  unsigned int rec_on_trk, int count, int cmd,
336 			  struct dasd_device *device, unsigned int reclen,
337 			  unsigned int tlf)
338 {
339 	struct dasd_eckd_private *private;
340 	int sector;
341 	int dn, d;
342 
343 	private = (struct dasd_eckd_private *) device->private;
344 
345 	memset(data, 0, sizeof(*data));
346 	sector = 0;
347 	if (rec_on_trk) {
348 		switch (private->rdc_data.dev_type) {
349 		case 0x3390:
350 			dn = ceil_quot(reclen + 6, 232);
351 			d = 9 + ceil_quot(reclen + 6 * (dn + 1), 34);
352 			sector = (49 + (rec_on_trk - 1) * (10 + d)) / 8;
353 			break;
354 		case 0x3380:
355 			d = 7 + ceil_quot(reclen + 12, 32);
356 			sector = (39 + (rec_on_trk - 1) * (8 + d)) / 7;
357 			break;
358 		}
359 	}
360 	data->sector = sector;
361 	/* note: meaning of count depends on the operation
362 	 *	 for record based I/O it's the number of records, but for
363 	 *	 track based I/O it's the number of tracks
364 	 */
365 	data->count = count;
366 	switch (cmd) {
367 	case DASD_ECKD_CCW_WRITE_HOME_ADDRESS:
368 		data->operation.orientation = 0x3;
369 		data->operation.operation = 0x03;
370 		break;
371 	case DASD_ECKD_CCW_READ_HOME_ADDRESS:
372 		data->operation.orientation = 0x3;
373 		data->operation.operation = 0x16;
374 		break;
375 	case DASD_ECKD_CCW_WRITE_RECORD_ZERO:
376 		data->operation.orientation = 0x1;
377 		data->operation.operation = 0x03;
378 		data->count++;
379 		break;
380 	case DASD_ECKD_CCW_READ_RECORD_ZERO:
381 		data->operation.orientation = 0x3;
382 		data->operation.operation = 0x16;
383 		data->count++;
384 		break;
385 	case DASD_ECKD_CCW_WRITE:
386 	case DASD_ECKD_CCW_WRITE_MT:
387 	case DASD_ECKD_CCW_WRITE_KD:
388 	case DASD_ECKD_CCW_WRITE_KD_MT:
389 		data->auxiliary.length_valid = 0x1;
390 		data->length = reclen;
391 		data->operation.operation = 0x01;
392 		break;
393 	case DASD_ECKD_CCW_WRITE_CKD:
394 	case DASD_ECKD_CCW_WRITE_CKD_MT:
395 		data->auxiliary.length_valid = 0x1;
396 		data->length = reclen;
397 		data->operation.operation = 0x03;
398 		break;
399 	case DASD_ECKD_CCW_WRITE_FULL_TRACK:
400 		data->operation.orientation = 0x0;
401 		data->operation.operation = 0x3F;
402 		data->extended_operation = 0x11;
403 		data->length = 0;
404 		data->extended_parameter_length = 0x02;
405 		if (data->count > 8) {
406 			data->extended_parameter[0] = 0xFF;
407 			data->extended_parameter[1] = 0xFF;
408 			data->extended_parameter[1] <<= (16 - count);
409 		} else {
410 			data->extended_parameter[0] = 0xFF;
411 			data->extended_parameter[0] <<= (8 - count);
412 			data->extended_parameter[1] = 0x00;
413 		}
414 		data->sector = 0xFF;
415 		break;
416 	case DASD_ECKD_CCW_WRITE_TRACK_DATA:
417 		data->auxiliary.length_valid = 0x1;
418 		data->length = reclen;	/* not tlf, as one might think */
419 		data->operation.operation = 0x3F;
420 		data->extended_operation = 0x23;
421 		break;
422 	case DASD_ECKD_CCW_READ:
423 	case DASD_ECKD_CCW_READ_MT:
424 	case DASD_ECKD_CCW_READ_KD:
425 	case DASD_ECKD_CCW_READ_KD_MT:
426 		data->auxiliary.length_valid = 0x1;
427 		data->length = reclen;
428 		data->operation.operation = 0x06;
429 		break;
430 	case DASD_ECKD_CCW_READ_CKD:
431 	case DASD_ECKD_CCW_READ_CKD_MT:
432 		data->auxiliary.length_valid = 0x1;
433 		data->length = reclen;
434 		data->operation.operation = 0x16;
435 		break;
436 	case DASD_ECKD_CCW_READ_COUNT:
437 		data->operation.operation = 0x06;
438 		break;
439 	case DASD_ECKD_CCW_READ_TRACK:
440 		data->operation.orientation = 0x1;
441 		data->operation.operation = 0x0C;
442 		data->extended_parameter_length = 0;
443 		data->sector = 0xFF;
444 		break;
445 	case DASD_ECKD_CCW_READ_TRACK_DATA:
446 		data->auxiliary.length_valid = 0x1;
447 		data->length = tlf;
448 		data->operation.operation = 0x0C;
449 		break;
450 	case DASD_ECKD_CCW_ERASE:
451 		data->length = reclen;
452 		data->auxiliary.length_valid = 0x1;
453 		data->operation.operation = 0x0b;
454 		break;
455 	default:
456 		DBF_DEV_EVENT(DBF_ERR, device,
457 			    "fill LRE unknown opcode 0x%x", cmd);
458 		BUG();
459 	}
460 	set_ch_t(&data->seek_addr,
461 		 trk / private->rdc_data.trk_per_cyl,
462 		 trk % private->rdc_data.trk_per_cyl);
463 	data->search_arg.cyl = data->seek_addr.cyl;
464 	data->search_arg.head = data->seek_addr.head;
465 	data->search_arg.record = rec_on_trk;
466 }
467 
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 char format,unsigned int rec_on_trk,int count,unsigned int blksize,unsigned int tlf)468 static int prefix_LRE(struct ccw1 *ccw, struct PFX_eckd_data *pfxdata,
469 		      unsigned int trk, unsigned int totrk, int cmd,
470 		      struct dasd_device *basedev, struct dasd_device *startdev,
471 		      unsigned char format, unsigned int rec_on_trk, int count,
472 		      unsigned int blksize, unsigned int tlf)
473 {
474 	struct dasd_eckd_private *basepriv, *startpriv;
475 	struct DE_eckd_data *dedata;
476 	struct LRE_eckd_data *lredata;
477 	u32 begcyl, endcyl;
478 	u16 heads, beghead, endhead;
479 	int rc = 0;
480 
481 	basepriv = (struct dasd_eckd_private *) basedev->private;
482 	startpriv = (struct dasd_eckd_private *) startdev->private;
483 	dedata = &pfxdata->define_extent;
484 	lredata = &pfxdata->locate_record;
485 
486 	ccw->cmd_code = DASD_ECKD_CCW_PFX;
487 	ccw->flags = 0;
488 	if (cmd == DASD_ECKD_CCW_WRITE_FULL_TRACK) {
489 		ccw->count = sizeof(*pfxdata) + 2;
490 		ccw->cda = (__u32) __pa(pfxdata);
491 		memset(pfxdata, 0, sizeof(*pfxdata) + 2);
492 	} else {
493 		ccw->count = sizeof(*pfxdata);
494 		ccw->cda = (__u32) __pa(pfxdata);
495 		memset(pfxdata, 0, sizeof(*pfxdata));
496 	}
497 
498 	/* prefix data */
499 	if (format > 1) {
500 		DBF_DEV_EVENT(DBF_ERR, basedev,
501 			      "PFX LRE unknown format 0x%x", format);
502 		BUG();
503 		return -EINVAL;
504 	}
505 	pfxdata->format = format;
506 	pfxdata->base_address = basepriv->ned->unit_addr;
507 	pfxdata->base_lss = basepriv->ned->ID;
508 	pfxdata->validity.define_extent = 1;
509 
510 	/* private uid is kept up to date, conf_data may be outdated */
511 	if (startpriv->uid.type != UA_BASE_DEVICE) {
512 		pfxdata->validity.verify_base = 1;
513 		if (startpriv->uid.type == UA_HYPER_PAV_ALIAS)
514 			pfxdata->validity.hyper_pav = 1;
515 	}
516 
517 	/* define extend data (mostly)*/
518 	switch (cmd) {
519 	case DASD_ECKD_CCW_READ_HOME_ADDRESS:
520 	case DASD_ECKD_CCW_READ_RECORD_ZERO:
521 	case DASD_ECKD_CCW_READ:
522 	case DASD_ECKD_CCW_READ_MT:
523 	case DASD_ECKD_CCW_READ_CKD:
524 	case DASD_ECKD_CCW_READ_CKD_MT:
525 	case DASD_ECKD_CCW_READ_KD:
526 	case DASD_ECKD_CCW_READ_KD_MT:
527 	case DASD_ECKD_CCW_READ_COUNT:
528 		dedata->mask.perm = 0x1;
529 		dedata->attributes.operation = basepriv->attrib.operation;
530 		break;
531 	case DASD_ECKD_CCW_READ_TRACK:
532 	case DASD_ECKD_CCW_READ_TRACK_DATA:
533 		dedata->mask.perm = 0x1;
534 		dedata->attributes.operation = basepriv->attrib.operation;
535 		dedata->blk_size = 0;
536 		break;
537 	case DASD_ECKD_CCW_WRITE:
538 	case DASD_ECKD_CCW_WRITE_MT:
539 	case DASD_ECKD_CCW_WRITE_KD:
540 	case DASD_ECKD_CCW_WRITE_KD_MT:
541 		dedata->mask.perm = 0x02;
542 		dedata->attributes.operation = basepriv->attrib.operation;
543 		rc = check_XRC_on_prefix(pfxdata, basedev);
544 		break;
545 	case DASD_ECKD_CCW_WRITE_CKD:
546 	case DASD_ECKD_CCW_WRITE_CKD_MT:
547 		dedata->attributes.operation = DASD_BYPASS_CACHE;
548 		rc = check_XRC_on_prefix(pfxdata, basedev);
549 		break;
550 	case DASD_ECKD_CCW_ERASE:
551 	case DASD_ECKD_CCW_WRITE_HOME_ADDRESS:
552 	case DASD_ECKD_CCW_WRITE_RECORD_ZERO:
553 		dedata->mask.perm = 0x3;
554 		dedata->mask.auth = 0x1;
555 		dedata->attributes.operation = DASD_BYPASS_CACHE;
556 		rc = check_XRC_on_prefix(pfxdata, basedev);
557 		break;
558 	case DASD_ECKD_CCW_WRITE_FULL_TRACK:
559 		dedata->mask.perm = 0x03;
560 		dedata->attributes.operation = basepriv->attrib.operation;
561 		dedata->blk_size = 0;
562 		break;
563 	case DASD_ECKD_CCW_WRITE_TRACK_DATA:
564 		dedata->mask.perm = 0x02;
565 		dedata->attributes.operation = basepriv->attrib.operation;
566 		dedata->blk_size = blksize;
567 		rc = check_XRC_on_prefix(pfxdata, basedev);
568 		break;
569 	default:
570 		DBF_DEV_EVENT(DBF_ERR, basedev,
571 			    "PFX LRE unknown opcode 0x%x", cmd);
572 		BUG();
573 		return -EINVAL;
574 	}
575 
576 	dedata->attributes.mode = 0x3;	/* ECKD */
577 
578 	if ((basepriv->rdc_data.cu_type == 0x2105 ||
579 	     basepriv->rdc_data.cu_type == 0x2107 ||
580 	     basepriv->rdc_data.cu_type == 0x1750)
581 	    && !(basepriv->uses_cdl && trk < 2))
582 		dedata->ga_extended |= 0x40; /* Regular Data Format Mode */
583 
584 	heads = basepriv->rdc_data.trk_per_cyl;
585 	begcyl = trk / heads;
586 	beghead = trk % heads;
587 	endcyl = totrk / heads;
588 	endhead = totrk % heads;
589 
590 	/* check for sequential prestage - enhance cylinder range */
591 	if (dedata->attributes.operation == DASD_SEQ_PRESTAGE ||
592 	    dedata->attributes.operation == DASD_SEQ_ACCESS) {
593 
594 		if (endcyl + basepriv->attrib.nr_cyl < basepriv->real_cyl)
595 			endcyl += basepriv->attrib.nr_cyl;
596 		else
597 			endcyl = (basepriv->real_cyl - 1);
598 	}
599 
600 	set_ch_t(&dedata->beg_ext, begcyl, beghead);
601 	set_ch_t(&dedata->end_ext, endcyl, endhead);
602 
603 	if (format == 1) {
604 		fill_LRE_data(lredata, trk, rec_on_trk, count, cmd,
605 			      basedev, blksize, tlf);
606 	}
607 
608 	return rc;
609 }
610 
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)611 static int prefix(struct ccw1 *ccw, struct PFX_eckd_data *pfxdata,
612 		  unsigned int trk, unsigned int totrk, int cmd,
613 		  struct dasd_device *basedev, struct dasd_device *startdev)
614 {
615 	return prefix_LRE(ccw, pfxdata, trk, totrk, cmd, basedev, startdev,
616 			  0, 0, 0, 0, 0);
617 }
618 
619 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)620 locate_record(struct ccw1 *ccw, struct LO_eckd_data *data, unsigned int trk,
621 	      unsigned int rec_on_trk, int no_rec, int cmd,
622 	      struct dasd_device * device, int reclen)
623 {
624 	struct dasd_eckd_private *private;
625 	int sector;
626 	int dn, d;
627 
628 	private = (struct dasd_eckd_private *) device->private;
629 
630 	DBF_DEV_EVENT(DBF_INFO, device,
631 		  "Locate: trk %d, rec %d, no_rec %d, cmd %d, reclen %d",
632 		  trk, rec_on_trk, no_rec, cmd, reclen);
633 
634 	ccw->cmd_code = DASD_ECKD_CCW_LOCATE_RECORD;
635 	ccw->flags = 0;
636 	ccw->count = 16;
637 	ccw->cda = (__u32) __pa(data);
638 
639 	memset(data, 0, sizeof(struct LO_eckd_data));
640 	sector = 0;
641 	if (rec_on_trk) {
642 		switch (private->rdc_data.dev_type) {
643 		case 0x3390:
644 			dn = ceil_quot(reclen + 6, 232);
645 			d = 9 + ceil_quot(reclen + 6 * (dn + 1), 34);
646 			sector = (49 + (rec_on_trk - 1) * (10 + d)) / 8;
647 			break;
648 		case 0x3380:
649 			d = 7 + ceil_quot(reclen + 12, 32);
650 			sector = (39 + (rec_on_trk - 1) * (8 + d)) / 7;
651 			break;
652 		}
653 	}
654 	data->sector = sector;
655 	data->count = no_rec;
656 	switch (cmd) {
657 	case DASD_ECKD_CCW_WRITE_HOME_ADDRESS:
658 		data->operation.orientation = 0x3;
659 		data->operation.operation = 0x03;
660 		break;
661 	case DASD_ECKD_CCW_READ_HOME_ADDRESS:
662 		data->operation.orientation = 0x3;
663 		data->operation.operation = 0x16;
664 		break;
665 	case DASD_ECKD_CCW_WRITE_RECORD_ZERO:
666 		data->operation.orientation = 0x1;
667 		data->operation.operation = 0x03;
668 		data->count++;
669 		break;
670 	case DASD_ECKD_CCW_READ_RECORD_ZERO:
671 		data->operation.orientation = 0x3;
672 		data->operation.operation = 0x16;
673 		data->count++;
674 		break;
675 	case DASD_ECKD_CCW_WRITE:
676 	case DASD_ECKD_CCW_WRITE_MT:
677 	case DASD_ECKD_CCW_WRITE_KD:
678 	case DASD_ECKD_CCW_WRITE_KD_MT:
679 		data->auxiliary.last_bytes_used = 0x1;
680 		data->length = reclen;
681 		data->operation.operation = 0x01;
682 		break;
683 	case DASD_ECKD_CCW_WRITE_CKD:
684 	case DASD_ECKD_CCW_WRITE_CKD_MT:
685 		data->auxiliary.last_bytes_used = 0x1;
686 		data->length = reclen;
687 		data->operation.operation = 0x03;
688 		break;
689 	case DASD_ECKD_CCW_READ:
690 	case DASD_ECKD_CCW_READ_MT:
691 	case DASD_ECKD_CCW_READ_KD:
692 	case DASD_ECKD_CCW_READ_KD_MT:
693 		data->auxiliary.last_bytes_used = 0x1;
694 		data->length = reclen;
695 		data->operation.operation = 0x06;
696 		break;
697 	case DASD_ECKD_CCW_READ_CKD:
698 	case DASD_ECKD_CCW_READ_CKD_MT:
699 		data->auxiliary.last_bytes_used = 0x1;
700 		data->length = reclen;
701 		data->operation.operation = 0x16;
702 		break;
703 	case DASD_ECKD_CCW_READ_COUNT:
704 		data->operation.operation = 0x06;
705 		break;
706 	case DASD_ECKD_CCW_ERASE:
707 		data->length = reclen;
708 		data->auxiliary.last_bytes_used = 0x1;
709 		data->operation.operation = 0x0b;
710 		break;
711 	default:
712 		DBF_DEV_EVENT(DBF_ERR, device, "unknown locate record "
713 			      "opcode 0x%x", cmd);
714 	}
715 	set_ch_t(&data->seek_addr,
716 		 trk / private->rdc_data.trk_per_cyl,
717 		 trk % private->rdc_data.trk_per_cyl);
718 	data->search_arg.cyl = data->seek_addr.cyl;
719 	data->search_arg.head = data->seek_addr.head;
720 	data->search_arg.record = rec_on_trk;
721 }
722 
723 /*
724  * Returns 1 if the block is one of the special blocks that needs
725  * to get read/written with the KD variant of the command.
726  * That is DASD_ECKD_READ_KD_MT instead of DASD_ECKD_READ_MT and
727  * DASD_ECKD_WRITE_KD_MT instead of DASD_ECKD_WRITE_MT.
728  * Luckily the KD variants differ only by one bit (0x08) from the
729  * normal variant. So don't wonder about code like:
730  * if (dasd_eckd_cdl_special(blk_per_trk, recid))
731  *         ccw->cmd_code |= 0x8;
732  */
733 static inline int
dasd_eckd_cdl_special(int blk_per_trk,int recid)734 dasd_eckd_cdl_special(int blk_per_trk, int recid)
735 {
736 	if (recid < 3)
737 		return 1;
738 	if (recid < blk_per_trk)
739 		return 0;
740 	if (recid < 2 * blk_per_trk)
741 		return 1;
742 	return 0;
743 }
744 
745 /*
746  * Returns the record size for the special blocks of the cdl format.
747  * Only returns something useful if dasd_eckd_cdl_special is true
748  * for the recid.
749  */
750 static inline int
dasd_eckd_cdl_reclen(int recid)751 dasd_eckd_cdl_reclen(int recid)
752 {
753 	if (recid < 3)
754 		return sizes_trk0[recid];
755 	return LABEL_SIZE;
756 }
757 /* create unique id from private structure. */
create_uid(struct dasd_eckd_private * private)758 static void create_uid(struct dasd_eckd_private *private)
759 {
760 	int count;
761 	struct dasd_uid *uid;
762 
763 	uid = &private->uid;
764 	memset(uid, 0, sizeof(struct dasd_uid));
765 	memcpy(uid->vendor, private->ned->HDA_manufacturer,
766 	       sizeof(uid->vendor) - 1);
767 	EBCASC(uid->vendor, sizeof(uid->vendor) - 1);
768 	memcpy(uid->serial, private->ned->HDA_location,
769 	       sizeof(uid->serial) - 1);
770 	EBCASC(uid->serial, sizeof(uid->serial) - 1);
771 	uid->ssid = private->gneq->subsystemID;
772 	uid->real_unit_addr = private->ned->unit_addr;
773 	if (private->sneq) {
774 		uid->type = private->sneq->sua_flags;
775 		if (uid->type == UA_BASE_PAV_ALIAS)
776 			uid->base_unit_addr = private->sneq->base_unit_addr;
777 	} else {
778 		uid->type = UA_BASE_DEVICE;
779 	}
780 	if (private->vdsneq) {
781 		for (count = 0; count < 16; count++) {
782 			sprintf(uid->vduit+2*count, "%02x",
783 				private->vdsneq->uit[count]);
784 		}
785 	}
786 }
787 
788 /*
789  * Generate device unique id that specifies the physical device.
790  */
dasd_eckd_generate_uid(struct dasd_device * device)791 static int dasd_eckd_generate_uid(struct dasd_device *device)
792 {
793 	struct dasd_eckd_private *private;
794 	unsigned long flags;
795 
796 	private = (struct dasd_eckd_private *) device->private;
797 	if (!private)
798 		return -ENODEV;
799 	if (!private->ned || !private->gneq)
800 		return -ENODEV;
801 	spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
802 	create_uid(private);
803 	spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
804 	return 0;
805 }
806 
dasd_eckd_get_uid(struct dasd_device * device,struct dasd_uid * uid)807 static int dasd_eckd_get_uid(struct dasd_device *device, struct dasd_uid *uid)
808 {
809 	struct dasd_eckd_private *private;
810 	unsigned long flags;
811 
812 	if (device->private) {
813 		private = (struct dasd_eckd_private *)device->private;
814 		spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
815 		*uid = private->uid;
816 		spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
817 		return 0;
818 	}
819 	return -EINVAL;
820 }
821 
822 /*
823  * compare device UID with data of a given dasd_eckd_private structure
824  * return 0 for match
825  */
dasd_eckd_compare_path_uid(struct dasd_device * device,struct dasd_eckd_private * private)826 static int dasd_eckd_compare_path_uid(struct dasd_device *device,
827 				      struct dasd_eckd_private *private)
828 {
829 	struct dasd_uid device_uid;
830 
831 	create_uid(private);
832 	dasd_eckd_get_uid(device, &device_uid);
833 
834 	return memcmp(&device_uid, &private->uid, sizeof(struct dasd_uid));
835 }
836 
dasd_eckd_fill_rcd_cqr(struct dasd_device * device,struct dasd_ccw_req * cqr,__u8 * rcd_buffer,__u8 lpm)837 static void dasd_eckd_fill_rcd_cqr(struct dasd_device *device,
838 				   struct dasd_ccw_req *cqr,
839 				   __u8 *rcd_buffer,
840 				   __u8 lpm)
841 {
842 	struct ccw1 *ccw;
843 	/*
844 	 * buffer has to start with EBCDIC "V1.0" to show
845 	 * support for virtual device SNEQ
846 	 */
847 	rcd_buffer[0] = 0xE5;
848 	rcd_buffer[1] = 0xF1;
849 	rcd_buffer[2] = 0x4B;
850 	rcd_buffer[3] = 0xF0;
851 
852 	ccw = cqr->cpaddr;
853 	ccw->cmd_code = DASD_ECKD_CCW_RCD;
854 	ccw->flags = 0;
855 	ccw->cda = (__u32)(addr_t)rcd_buffer;
856 	ccw->count = DASD_ECKD_RCD_DATA_SIZE;
857 	cqr->magic = DASD_ECKD_MAGIC;
858 
859 	cqr->startdev = device;
860 	cqr->memdev = device;
861 	cqr->block = NULL;
862 	cqr->expires = 10*HZ;
863 	cqr->lpm = lpm;
864 	cqr->retries = 256;
865 	cqr->buildclk = get_tod_clock();
866 	cqr->status = DASD_CQR_FILLED;
867 	set_bit(DASD_CQR_VERIFY_PATH, &cqr->flags);
868 }
869 
870 /*
871  * Wakeup helper for read_conf
872  * if the cqr is not done and needs some error recovery
873  * the buffer has to be re-initialized with the EBCDIC "V1.0"
874  * to show support for virtual device SNEQ
875  */
read_conf_cb(struct dasd_ccw_req * cqr,void * data)876 static void read_conf_cb(struct dasd_ccw_req *cqr, void *data)
877 {
878 	struct ccw1 *ccw;
879 	__u8 *rcd_buffer;
880 
881 	if (cqr->status !=  DASD_CQR_DONE) {
882 		ccw = cqr->cpaddr;
883 		rcd_buffer = (__u8 *)((addr_t) ccw->cda);
884 		memset(rcd_buffer, 0, sizeof(*rcd_buffer));
885 
886 		rcd_buffer[0] = 0xE5;
887 		rcd_buffer[1] = 0xF1;
888 		rcd_buffer[2] = 0x4B;
889 		rcd_buffer[3] = 0xF0;
890 	}
891 	dasd_wakeup_cb(cqr, data);
892 }
893 
dasd_eckd_read_conf_immediately(struct dasd_device * device,struct dasd_ccw_req * cqr,__u8 * rcd_buffer,__u8 lpm)894 static int dasd_eckd_read_conf_immediately(struct dasd_device *device,
895 					   struct dasd_ccw_req *cqr,
896 					   __u8 *rcd_buffer,
897 					   __u8 lpm)
898 {
899 	struct ciw *ciw;
900 	int rc;
901 	/*
902 	 * sanity check: scan for RCD command in extended SenseID data
903 	 * some devices do not support RCD
904 	 */
905 	ciw = ccw_device_get_ciw(device->cdev, CIW_TYPE_RCD);
906 	if (!ciw || ciw->cmd != DASD_ECKD_CCW_RCD)
907 		return -EOPNOTSUPP;
908 
909 	dasd_eckd_fill_rcd_cqr(device, cqr, rcd_buffer, lpm);
910 	clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
911 	set_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags);
912 	cqr->retries = 5;
913 	cqr->callback = read_conf_cb;
914 	rc = dasd_sleep_on_immediatly(cqr);
915 	return rc;
916 }
917 
dasd_eckd_read_conf_lpm(struct dasd_device * device,void ** rcd_buffer,int * rcd_buffer_size,__u8 lpm)918 static int dasd_eckd_read_conf_lpm(struct dasd_device *device,
919 				   void **rcd_buffer,
920 				   int *rcd_buffer_size, __u8 lpm)
921 {
922 	struct ciw *ciw;
923 	char *rcd_buf = NULL;
924 	int ret;
925 	struct dasd_ccw_req *cqr;
926 
927 	/*
928 	 * sanity check: scan for RCD command in extended SenseID data
929 	 * some devices do not support RCD
930 	 */
931 	ciw = ccw_device_get_ciw(device->cdev, CIW_TYPE_RCD);
932 	if (!ciw || ciw->cmd != DASD_ECKD_CCW_RCD) {
933 		ret = -EOPNOTSUPP;
934 		goto out_error;
935 	}
936 	rcd_buf = kzalloc(DASD_ECKD_RCD_DATA_SIZE, GFP_KERNEL | GFP_DMA);
937 	if (!rcd_buf) {
938 		ret = -ENOMEM;
939 		goto out_error;
940 	}
941 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* RCD */,
942 				   0, /* use rcd_buf as data ara */
943 				   device);
944 	if (IS_ERR(cqr)) {
945 		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
946 			      "Could not allocate RCD request");
947 		ret = -ENOMEM;
948 		goto out_error;
949 	}
950 	dasd_eckd_fill_rcd_cqr(device, cqr, rcd_buf, lpm);
951 	cqr->callback = read_conf_cb;
952 	ret = dasd_sleep_on(cqr);
953 	/*
954 	 * on success we update the user input parms
955 	 */
956 	dasd_sfree_request(cqr, cqr->memdev);
957 	if (ret)
958 		goto out_error;
959 
960 	*rcd_buffer_size = DASD_ECKD_RCD_DATA_SIZE;
961 	*rcd_buffer = rcd_buf;
962 	return 0;
963 out_error:
964 	kfree(rcd_buf);
965 	*rcd_buffer = NULL;
966 	*rcd_buffer_size = 0;
967 	return ret;
968 }
969 
dasd_eckd_identify_conf_parts(struct dasd_eckd_private * private)970 static int dasd_eckd_identify_conf_parts(struct dasd_eckd_private *private)
971 {
972 
973 	struct dasd_sneq *sneq;
974 	int i, count;
975 
976 	private->ned = NULL;
977 	private->sneq = NULL;
978 	private->vdsneq = NULL;
979 	private->gneq = NULL;
980 	count = private->conf_len / sizeof(struct dasd_sneq);
981 	sneq = (struct dasd_sneq *)private->conf_data;
982 	for (i = 0; i < count; ++i) {
983 		if (sneq->flags.identifier == 1 && sneq->format == 1)
984 			private->sneq = sneq;
985 		else if (sneq->flags.identifier == 1 && sneq->format == 4)
986 			private->vdsneq = (struct vd_sneq *)sneq;
987 		else if (sneq->flags.identifier == 2)
988 			private->gneq = (struct dasd_gneq *)sneq;
989 		else if (sneq->flags.identifier == 3 && sneq->res1 == 1)
990 			private->ned = (struct dasd_ned *)sneq;
991 		sneq++;
992 	}
993 	if (!private->ned || !private->gneq) {
994 		private->ned = NULL;
995 		private->sneq = NULL;
996 		private->vdsneq = NULL;
997 		private->gneq = NULL;
998 		return -EINVAL;
999 	}
1000 	return 0;
1001 
1002 };
1003 
dasd_eckd_path_access(void * conf_data,int conf_len)1004 static unsigned char dasd_eckd_path_access(void *conf_data, int conf_len)
1005 {
1006 	struct dasd_gneq *gneq;
1007 	int i, count, found;
1008 
1009 	count = conf_len / sizeof(*gneq);
1010 	gneq = (struct dasd_gneq *)conf_data;
1011 	found = 0;
1012 	for (i = 0; i < count; ++i) {
1013 		if (gneq->flags.identifier == 2) {
1014 			found = 1;
1015 			break;
1016 		}
1017 		gneq++;
1018 	}
1019 	if (found)
1020 		return ((char *)gneq)[18] & 0x07;
1021 	else
1022 		return 0;
1023 }
1024 
dasd_eckd_read_conf(struct dasd_device * device)1025 static int dasd_eckd_read_conf(struct dasd_device *device)
1026 {
1027 	void *conf_data;
1028 	int conf_len, conf_data_saved;
1029 	int rc, path_err;
1030 	__u8 lpm, opm;
1031 	struct dasd_eckd_private *private, path_private;
1032 	struct dasd_path *path_data;
1033 	struct dasd_uid *uid;
1034 	char print_path_uid[60], print_device_uid[60];
1035 
1036 	private = (struct dasd_eckd_private *) device->private;
1037 	path_data = &device->path_data;
1038 	opm = ccw_device_get_path_mask(device->cdev);
1039 	conf_data_saved = 0;
1040 	path_err = 0;
1041 	/* get configuration data per operational path */
1042 	for (lpm = 0x80; lpm; lpm>>= 1) {
1043 		if (!(lpm & opm))
1044 			continue;
1045 		rc = dasd_eckd_read_conf_lpm(device, &conf_data,
1046 					     &conf_len, lpm);
1047 		if (rc && rc != -EOPNOTSUPP) {	/* -EOPNOTSUPP is ok */
1048 			DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
1049 					"Read configuration data returned "
1050 					"error %d", rc);
1051 			return rc;
1052 		}
1053 		if (conf_data == NULL) {
1054 			DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
1055 					"No configuration data "
1056 					"retrieved");
1057 			/* no further analysis possible */
1058 			path_data->opm |= lpm;
1059 			continue;	/* no error */
1060 		}
1061 		/* save first valid configuration data */
1062 		if (!conf_data_saved) {
1063 			kfree(private->conf_data);
1064 			private->conf_data = conf_data;
1065 			private->conf_len = conf_len;
1066 			if (dasd_eckd_identify_conf_parts(private)) {
1067 				private->conf_data = NULL;
1068 				private->conf_len = 0;
1069 				kfree(conf_data);
1070 				continue;
1071 			}
1072 			/*
1073 			 * build device UID that other path data
1074 			 * can be compared to it
1075 			 */
1076 			dasd_eckd_generate_uid(device);
1077 			conf_data_saved++;
1078 		} else {
1079 			path_private.conf_data = conf_data;
1080 			path_private.conf_len = DASD_ECKD_RCD_DATA_SIZE;
1081 			if (dasd_eckd_identify_conf_parts(
1082 				    &path_private)) {
1083 				path_private.conf_data = NULL;
1084 				path_private.conf_len = 0;
1085 				kfree(conf_data);
1086 				continue;
1087 			}
1088 
1089 			if (dasd_eckd_compare_path_uid(
1090 				    device, &path_private)) {
1091 				uid = &path_private.uid;
1092 				if (strlen(uid->vduit) > 0)
1093 					snprintf(print_path_uid,
1094 						 sizeof(print_path_uid),
1095 						 "%s.%s.%04x.%02x.%s",
1096 						 uid->vendor, uid->serial,
1097 						 uid->ssid, uid->real_unit_addr,
1098 						 uid->vduit);
1099 				else
1100 					snprintf(print_path_uid,
1101 						 sizeof(print_path_uid),
1102 						 "%s.%s.%04x.%02x",
1103 						 uid->vendor, uid->serial,
1104 						 uid->ssid,
1105 						 uid->real_unit_addr);
1106 				uid = &private->uid;
1107 				if (strlen(uid->vduit) > 0)
1108 					snprintf(print_device_uid,
1109 						 sizeof(print_device_uid),
1110 						 "%s.%s.%04x.%02x.%s",
1111 						 uid->vendor, uid->serial,
1112 						 uid->ssid, uid->real_unit_addr,
1113 						 uid->vduit);
1114 				else
1115 					snprintf(print_device_uid,
1116 						 sizeof(print_device_uid),
1117 						 "%s.%s.%04x.%02x",
1118 						 uid->vendor, uid->serial,
1119 						 uid->ssid,
1120 						 uid->real_unit_addr);
1121 				dev_err(&device->cdev->dev,
1122 					"Not all channel paths lead to "
1123 					"the same device, path %02X leads to "
1124 					"device %s instead of %s\n", lpm,
1125 					print_path_uid, print_device_uid);
1126 				path_err = -EINVAL;
1127 				continue;
1128 			}
1129 
1130 			path_private.conf_data = NULL;
1131 			path_private.conf_len = 0;
1132 		}
1133 		switch (dasd_eckd_path_access(conf_data, conf_len)) {
1134 		case 0x02:
1135 			path_data->npm |= lpm;
1136 			break;
1137 		case 0x03:
1138 			path_data->ppm |= lpm;
1139 			break;
1140 		}
1141 		path_data->opm |= lpm;
1142 
1143 		if (conf_data != private->conf_data)
1144 			kfree(conf_data);
1145 	}
1146 
1147 	return path_err;
1148 }
1149 
verify_fcx_max_data(struct dasd_device * device,__u8 lpm)1150 static int verify_fcx_max_data(struct dasd_device *device, __u8 lpm)
1151 {
1152 	struct dasd_eckd_private *private;
1153 	int mdc;
1154 	u32 fcx_max_data;
1155 
1156 	private = (struct dasd_eckd_private *) device->private;
1157 	if (private->fcx_max_data) {
1158 		mdc = ccw_device_get_mdc(device->cdev, lpm);
1159 		if ((mdc < 0)) {
1160 			dev_warn(&device->cdev->dev,
1161 				 "Detecting the maximum data size for zHPF "
1162 				 "requests failed (rc=%d) for a new path %x\n",
1163 				 mdc, lpm);
1164 			return mdc;
1165 		}
1166 		fcx_max_data = mdc * FCX_MAX_DATA_FACTOR;
1167 		if (fcx_max_data < private->fcx_max_data) {
1168 			dev_warn(&device->cdev->dev,
1169 				 "The maximum data size for zHPF requests %u "
1170 				 "on a new path %x is below the active maximum "
1171 				 "%u\n", fcx_max_data, lpm,
1172 				 private->fcx_max_data);
1173 			return -EACCES;
1174 		}
1175 	}
1176 	return 0;
1177 }
1178 
rebuild_device_uid(struct dasd_device * device,struct path_verification_work_data * data)1179 static int rebuild_device_uid(struct dasd_device *device,
1180 			      struct path_verification_work_data *data)
1181 {
1182 	struct dasd_eckd_private *private;
1183 	struct dasd_path *path_data;
1184 	__u8 lpm, opm;
1185 	int rc;
1186 
1187 	rc = -ENODEV;
1188 	private = (struct dasd_eckd_private *) device->private;
1189 	path_data = &device->path_data;
1190 	opm = device->path_data.opm;
1191 
1192 	for (lpm = 0x80; lpm; lpm >>= 1) {
1193 		if (!(lpm & opm))
1194 			continue;
1195 		memset(&data->rcd_buffer, 0, sizeof(data->rcd_buffer));
1196 		memset(&data->cqr, 0, sizeof(data->cqr));
1197 		data->cqr.cpaddr = &data->ccw;
1198 		rc = dasd_eckd_read_conf_immediately(device, &data->cqr,
1199 						     data->rcd_buffer,
1200 						     lpm);
1201 
1202 		if (rc) {
1203 			if (rc == -EOPNOTSUPP) /* -EOPNOTSUPP is ok */
1204 				continue;
1205 			DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
1206 					"Read configuration data "
1207 					"returned error %d", rc);
1208 			break;
1209 		}
1210 		memcpy(private->conf_data, data->rcd_buffer,
1211 		       DASD_ECKD_RCD_DATA_SIZE);
1212 		if (dasd_eckd_identify_conf_parts(private)) {
1213 			rc = -ENODEV;
1214 		} else /* first valid path is enough */
1215 			break;
1216 	}
1217 
1218 	if (!rc)
1219 		rc = dasd_eckd_generate_uid(device);
1220 
1221 	return rc;
1222 }
1223 
do_path_verification_work(struct work_struct * work)1224 static void do_path_verification_work(struct work_struct *work)
1225 {
1226 	struct path_verification_work_data *data;
1227 	struct dasd_device *device;
1228 	struct dasd_eckd_private path_private;
1229 	struct dasd_uid *uid;
1230 	__u8 path_rcd_buf[DASD_ECKD_RCD_DATA_SIZE];
1231 	__u8 lpm, opm, npm, ppm, epm;
1232 	unsigned long flags;
1233 	char print_uid[60];
1234 	int rc;
1235 
1236 	data = container_of(work, struct path_verification_work_data, worker);
1237 	device = data->device;
1238 
1239 	/* delay path verification until device was resumed */
1240 	if (test_bit(DASD_FLAG_SUSPENDED, &device->flags)) {
1241 		schedule_work(work);
1242 		return;
1243 	}
1244 
1245 	opm = 0;
1246 	npm = 0;
1247 	ppm = 0;
1248 	epm = 0;
1249 	for (lpm = 0x80; lpm; lpm >>= 1) {
1250 		if (!(lpm & data->tbvpm))
1251 			continue;
1252 		memset(&data->rcd_buffer, 0, sizeof(data->rcd_buffer));
1253 		memset(&data->cqr, 0, sizeof(data->cqr));
1254 		data->cqr.cpaddr = &data->ccw;
1255 		rc = dasd_eckd_read_conf_immediately(device, &data->cqr,
1256 						     data->rcd_buffer,
1257 						     lpm);
1258 		if (!rc) {
1259 			switch (dasd_eckd_path_access(data->rcd_buffer,
1260 						      DASD_ECKD_RCD_DATA_SIZE)
1261 				) {
1262 			case 0x02:
1263 				npm |= lpm;
1264 				break;
1265 			case 0x03:
1266 				ppm |= lpm;
1267 				break;
1268 			}
1269 			opm |= lpm;
1270 		} else if (rc == -EOPNOTSUPP) {
1271 			DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
1272 					"path verification: No configuration "
1273 					"data retrieved");
1274 			opm |= lpm;
1275 		} else if (rc == -EAGAIN) {
1276 			DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
1277 					"path verification: device is stopped,"
1278 					" try again later");
1279 			epm |= lpm;
1280 		} else {
1281 			dev_warn(&device->cdev->dev,
1282 				 "Reading device feature codes failed "
1283 				 "(rc=%d) for new path %x\n", rc, lpm);
1284 			continue;
1285 		}
1286 		if (verify_fcx_max_data(device, lpm)) {
1287 			opm &= ~lpm;
1288 			npm &= ~lpm;
1289 			ppm &= ~lpm;
1290 			continue;
1291 		}
1292 
1293 		/*
1294 		 * save conf_data for comparison after
1295 		 * rebuild_device_uid may have changed
1296 		 * the original data
1297 		 */
1298 		memcpy(&path_rcd_buf, data->rcd_buffer,
1299 		       DASD_ECKD_RCD_DATA_SIZE);
1300 		path_private.conf_data = (void *) &path_rcd_buf;
1301 		path_private.conf_len = DASD_ECKD_RCD_DATA_SIZE;
1302 		if (dasd_eckd_identify_conf_parts(&path_private)) {
1303 			path_private.conf_data = NULL;
1304 			path_private.conf_len = 0;
1305 			continue;
1306 		}
1307 
1308 		/*
1309 		 * compare path UID with device UID only if at least
1310 		 * one valid path is left
1311 		 * in other case the device UID may have changed and
1312 		 * the first working path UID will be used as device UID
1313 		 */
1314 		if (device->path_data.opm &&
1315 		    dasd_eckd_compare_path_uid(device, &path_private)) {
1316 			/*
1317 			 * the comparison was not successful
1318 			 * rebuild the device UID with at least one
1319 			 * known path in case a z/VM hyperswap command
1320 			 * has changed the device
1321 			 *
1322 			 * after this compare again
1323 			 *
1324 			 * if either the rebuild or the recompare fails
1325 			 * the path can not be used
1326 			 */
1327 			if (rebuild_device_uid(device, data) ||
1328 			    dasd_eckd_compare_path_uid(
1329 				    device, &path_private)) {
1330 				uid = &path_private.uid;
1331 				if (strlen(uid->vduit) > 0)
1332 					snprintf(print_uid, sizeof(print_uid),
1333 						 "%s.%s.%04x.%02x.%s",
1334 						 uid->vendor, uid->serial,
1335 						 uid->ssid, uid->real_unit_addr,
1336 						 uid->vduit);
1337 				else
1338 					snprintf(print_uid, sizeof(print_uid),
1339 						 "%s.%s.%04x.%02x",
1340 						 uid->vendor, uid->serial,
1341 						 uid->ssid,
1342 						 uid->real_unit_addr);
1343 				dev_err(&device->cdev->dev,
1344 					"The newly added channel path %02X "
1345 					"will not be used because it leads "
1346 					"to a different device %s\n",
1347 					lpm, print_uid);
1348 				opm &= ~lpm;
1349 				npm &= ~lpm;
1350 				ppm &= ~lpm;
1351 				continue;
1352 			}
1353 		}
1354 
1355 		/*
1356 		 * There is a small chance that a path is lost again between
1357 		 * above path verification and the following modification of
1358 		 * the device opm mask. We could avoid that race here by using
1359 		 * yet another path mask, but we rather deal with this unlikely
1360 		 * situation in dasd_start_IO.
1361 		 */
1362 		spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1363 		if (!device->path_data.opm && opm) {
1364 			device->path_data.opm = opm;
1365 			dasd_generic_path_operational(device);
1366 		} else
1367 			device->path_data.opm |= opm;
1368 		device->path_data.npm |= npm;
1369 		device->path_data.ppm |= ppm;
1370 		device->path_data.tbvpm |= epm;
1371 		spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1372 	}
1373 
1374 	dasd_put_device(device);
1375 	if (data->isglobal)
1376 		mutex_unlock(&dasd_path_verification_mutex);
1377 	else
1378 		kfree(data);
1379 }
1380 
dasd_eckd_verify_path(struct dasd_device * device,__u8 lpm)1381 static int dasd_eckd_verify_path(struct dasd_device *device, __u8 lpm)
1382 {
1383 	struct path_verification_work_data *data;
1384 
1385 	data = kmalloc(sizeof(*data), GFP_ATOMIC | GFP_DMA);
1386 	if (!data) {
1387 		if (mutex_trylock(&dasd_path_verification_mutex)) {
1388 			data = path_verification_worker;
1389 			data->isglobal = 1;
1390 		} else
1391 			return -ENOMEM;
1392 	} else {
1393 		memset(data, 0, sizeof(*data));
1394 		data->isglobal = 0;
1395 	}
1396 	INIT_WORK(&data->worker, do_path_verification_work);
1397 	dasd_get_device(device);
1398 	data->device = device;
1399 	data->tbvpm = lpm;
1400 	schedule_work(&data->worker);
1401 	return 0;
1402 }
1403 
dasd_eckd_read_features(struct dasd_device * device)1404 static int dasd_eckd_read_features(struct dasd_device *device)
1405 {
1406 	struct dasd_psf_prssd_data *prssdp;
1407 	struct dasd_rssd_features *features;
1408 	struct dasd_ccw_req *cqr;
1409 	struct ccw1 *ccw;
1410 	int rc;
1411 	struct dasd_eckd_private *private;
1412 
1413 	private = (struct dasd_eckd_private *) device->private;
1414 	memset(&private->features, 0, sizeof(struct dasd_rssd_features));
1415 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */	+ 1 /* RSSD */,
1416 				   (sizeof(struct dasd_psf_prssd_data) +
1417 				    sizeof(struct dasd_rssd_features)),
1418 				   device);
1419 	if (IS_ERR(cqr)) {
1420 		DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s", "Could not "
1421 				"allocate initialization request");
1422 		return PTR_ERR(cqr);
1423 	}
1424 	cqr->startdev = device;
1425 	cqr->memdev = device;
1426 	cqr->block = NULL;
1427 	cqr->retries = 256;
1428 	cqr->expires = 10 * HZ;
1429 
1430 	/* Prepare for Read Subsystem Data */
1431 	prssdp = (struct dasd_psf_prssd_data *) cqr->data;
1432 	memset(prssdp, 0, sizeof(struct dasd_psf_prssd_data));
1433 	prssdp->order = PSF_ORDER_PRSSD;
1434 	prssdp->suborder = 0x41;	/* Read Feature Codes */
1435 	/* all other bytes of prssdp must be zero */
1436 
1437 	ccw = cqr->cpaddr;
1438 	ccw->cmd_code = DASD_ECKD_CCW_PSF;
1439 	ccw->count = sizeof(struct dasd_psf_prssd_data);
1440 	ccw->flags |= CCW_FLAG_CC;
1441 	ccw->cda = (__u32)(addr_t) prssdp;
1442 
1443 	/* Read Subsystem Data - feature codes */
1444 	features = (struct dasd_rssd_features *) (prssdp + 1);
1445 	memset(features, 0, sizeof(struct dasd_rssd_features));
1446 
1447 	ccw++;
1448 	ccw->cmd_code = DASD_ECKD_CCW_RSSD;
1449 	ccw->count = sizeof(struct dasd_rssd_features);
1450 	ccw->cda = (__u32)(addr_t) features;
1451 
1452 	cqr->buildclk = get_tod_clock();
1453 	cqr->status = DASD_CQR_FILLED;
1454 	rc = dasd_sleep_on(cqr);
1455 	if (rc == 0) {
1456 		prssdp = (struct dasd_psf_prssd_data *) cqr->data;
1457 		features = (struct dasd_rssd_features *) (prssdp + 1);
1458 		memcpy(&private->features, features,
1459 		       sizeof(struct dasd_rssd_features));
1460 	} else
1461 		dev_warn(&device->cdev->dev, "Reading device feature codes"
1462 			 " failed with rc=%d\n", rc);
1463 	dasd_sfree_request(cqr, cqr->memdev);
1464 	return rc;
1465 }
1466 
1467 
1468 /*
1469  * Build CP for Perform Subsystem Function - SSC.
1470  */
dasd_eckd_build_psf_ssc(struct dasd_device * device,int enable_pav)1471 static struct dasd_ccw_req *dasd_eckd_build_psf_ssc(struct dasd_device *device,
1472 						    int enable_pav)
1473 {
1474 	struct dasd_ccw_req *cqr;
1475 	struct dasd_psf_ssc_data *psf_ssc_data;
1476 	struct ccw1 *ccw;
1477 
1478 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */ ,
1479 				  sizeof(struct dasd_psf_ssc_data),
1480 				  device);
1481 
1482 	if (IS_ERR(cqr)) {
1483 		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1484 			   "Could not allocate PSF-SSC request");
1485 		return cqr;
1486 	}
1487 	psf_ssc_data = (struct dasd_psf_ssc_data *)cqr->data;
1488 	psf_ssc_data->order = PSF_ORDER_SSC;
1489 	psf_ssc_data->suborder = 0xc0;
1490 	if (enable_pav) {
1491 		psf_ssc_data->suborder |= 0x08;
1492 		psf_ssc_data->reserved[0] = 0x88;
1493 	}
1494 	ccw = cqr->cpaddr;
1495 	ccw->cmd_code = DASD_ECKD_CCW_PSF;
1496 	ccw->cda = (__u32)(addr_t)psf_ssc_data;
1497 	ccw->count = 66;
1498 
1499 	cqr->startdev = device;
1500 	cqr->memdev = device;
1501 	cqr->block = NULL;
1502 	cqr->retries = 256;
1503 	cqr->expires = 10*HZ;
1504 	cqr->buildclk = get_tod_clock();
1505 	cqr->status = DASD_CQR_FILLED;
1506 	return cqr;
1507 }
1508 
1509 /*
1510  * Perform Subsystem Function.
1511  * It is necessary to trigger CIO for channel revalidation since this
1512  * call might change behaviour of DASD devices.
1513  */
1514 static int
dasd_eckd_psf_ssc(struct dasd_device * device,int enable_pav,unsigned long flags)1515 dasd_eckd_psf_ssc(struct dasd_device *device, int enable_pav,
1516 		  unsigned long flags)
1517 {
1518 	struct dasd_ccw_req *cqr;
1519 	int rc;
1520 
1521 	cqr = dasd_eckd_build_psf_ssc(device, enable_pav);
1522 	if (IS_ERR(cqr))
1523 		return PTR_ERR(cqr);
1524 
1525 	/*
1526 	 * set flags e.g. turn on failfast, to prevent blocking
1527 	 * the calling function should handle failed requests
1528 	 */
1529 	cqr->flags |= flags;
1530 
1531 	rc = dasd_sleep_on(cqr);
1532 	if (!rc)
1533 		/* trigger CIO to reprobe devices */
1534 		css_schedule_reprobe();
1535 	else if (cqr->intrc == -EAGAIN)
1536 		rc = -EAGAIN;
1537 
1538 	dasd_sfree_request(cqr, cqr->memdev);
1539 	return rc;
1540 }
1541 
1542 /*
1543  * Valide storage server of current device.
1544  */
dasd_eckd_validate_server(struct dasd_device * device,unsigned long flags)1545 static int dasd_eckd_validate_server(struct dasd_device *device,
1546 				     unsigned long flags)
1547 {
1548 	int rc;
1549 	struct dasd_eckd_private *private;
1550 	int enable_pav;
1551 
1552 	private = (struct dasd_eckd_private *) device->private;
1553 	if (private->uid.type == UA_BASE_PAV_ALIAS ||
1554 	    private->uid.type == UA_HYPER_PAV_ALIAS)
1555 		return 0;
1556 	if (dasd_nopav || MACHINE_IS_VM)
1557 		enable_pav = 0;
1558 	else
1559 		enable_pav = 1;
1560 	rc = dasd_eckd_psf_ssc(device, enable_pav, flags);
1561 
1562 	/* may be requested feature is not available on server,
1563 	 * therefore just report error and go ahead */
1564 	DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "PSF-SSC for SSID %04x "
1565 			"returned rc=%d", private->uid.ssid, rc);
1566 	return rc;
1567 }
1568 
1569 /*
1570  * worker to do a validate server in case of a lost pathgroup
1571  */
dasd_eckd_do_validate_server(struct work_struct * work)1572 static void dasd_eckd_do_validate_server(struct work_struct *work)
1573 {
1574 	struct dasd_device *device = container_of(work, struct dasd_device,
1575 						  kick_validate);
1576 	unsigned long flags = 0;
1577 
1578 	set_bit(DASD_CQR_FLAGS_FAILFAST, &flags);
1579 	if (dasd_eckd_validate_server(device, flags)
1580 	    == -EAGAIN) {
1581 		/* schedule worker again if failed */
1582 		schedule_work(&device->kick_validate);
1583 		return;
1584 	}
1585 
1586 	dasd_put_device(device);
1587 }
1588 
dasd_eckd_kick_validate_server(struct dasd_device * device)1589 static void dasd_eckd_kick_validate_server(struct dasd_device *device)
1590 {
1591 	dasd_get_device(device);
1592 	/* exit if device not online or in offline processing */
1593 	if (test_bit(DASD_FLAG_OFFLINE, &device->flags) ||
1594 	   device->state < DASD_STATE_ONLINE) {
1595 		dasd_put_device(device);
1596 		return;
1597 	}
1598 	/* queue call to do_validate_server to the kernel event daemon. */
1599 	schedule_work(&device->kick_validate);
1600 }
1601 
get_fcx_max_data(struct dasd_device * device)1602 static u32 get_fcx_max_data(struct dasd_device *device)
1603 {
1604 #if defined(CONFIG_64BIT)
1605 	int tpm, mdc;
1606 	int fcx_in_css, fcx_in_gneq, fcx_in_features;
1607 	struct dasd_eckd_private *private;
1608 
1609 	if (dasd_nofcx)
1610 		return 0;
1611 	/* is transport mode supported? */
1612 	private = (struct dasd_eckd_private *) device->private;
1613 	fcx_in_css = css_general_characteristics.fcx;
1614 	fcx_in_gneq = private->gneq->reserved2[7] & 0x04;
1615 	fcx_in_features = private->features.feature[40] & 0x80;
1616 	tpm = fcx_in_css && fcx_in_gneq && fcx_in_features;
1617 
1618 	if (!tpm)
1619 		return 0;
1620 
1621 	mdc = ccw_device_get_mdc(device->cdev, 0);
1622 	if (mdc < 0) {
1623 		dev_warn(&device->cdev->dev, "Detecting the maximum supported"
1624 			 " data size for zHPF requests failed\n");
1625 		return 0;
1626 	} else
1627 		return mdc * FCX_MAX_DATA_FACTOR;
1628 #else
1629 	return 0;
1630 #endif
1631 }
1632 
1633 /*
1634  * Check device characteristics.
1635  * If the device is accessible using ECKD discipline, the device is enabled.
1636  */
1637 static int
dasd_eckd_check_characteristics(struct dasd_device * device)1638 dasd_eckd_check_characteristics(struct dasd_device *device)
1639 {
1640 	struct dasd_eckd_private *private;
1641 	struct dasd_block *block;
1642 	struct dasd_uid temp_uid;
1643 	int rc, i;
1644 	int readonly;
1645 	unsigned long value;
1646 
1647 	/* setup work queue for validate server*/
1648 	INIT_WORK(&device->kick_validate, dasd_eckd_do_validate_server);
1649 
1650 	if (!ccw_device_is_pathgroup(device->cdev)) {
1651 		dev_warn(&device->cdev->dev,
1652 			 "A channel path group could not be established\n");
1653 		return -EIO;
1654 	}
1655 	if (!ccw_device_is_multipath(device->cdev)) {
1656 		dev_info(&device->cdev->dev,
1657 			 "The DASD is not operating in multipath mode\n");
1658 	}
1659 	private = (struct dasd_eckd_private *) device->private;
1660 	if (!private) {
1661 		private = kzalloc(sizeof(*private), GFP_KERNEL | GFP_DMA);
1662 		if (!private) {
1663 			dev_warn(&device->cdev->dev,
1664 				 "Allocating memory for private DASD data "
1665 				 "failed\n");
1666 			return -ENOMEM;
1667 		}
1668 		device->private = (void *) private;
1669 	} else {
1670 		memset(private, 0, sizeof(*private));
1671 	}
1672 	/* Invalidate status of initial analysis. */
1673 	private->init_cqr_status = -1;
1674 	/* Set default cache operations. */
1675 	private->attrib.operation = DASD_NORMAL_CACHE;
1676 	private->attrib.nr_cyl = 0;
1677 
1678 	/* Read Configuration Data */
1679 	rc = dasd_eckd_read_conf(device);
1680 	if (rc)
1681 		goto out_err1;
1682 
1683 	/* set default timeout */
1684 	device->default_expires = DASD_EXPIRES;
1685 	if (private->gneq) {
1686 		value = 1;
1687 		for (i = 0; i < private->gneq->timeout.value; i++)
1688 			value = 10 * value;
1689 		value = value * private->gneq->timeout.number;
1690 		/* do not accept useless values */
1691 		if (value != 0 && value <= DASD_EXPIRES_MAX)
1692 			device->default_expires = value;
1693 	}
1694 
1695 	dasd_eckd_get_uid(device, &temp_uid);
1696 	if (temp_uid.type == UA_BASE_DEVICE) {
1697 		block = dasd_alloc_block();
1698 		if (IS_ERR(block)) {
1699 			DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
1700 					"could not allocate dasd "
1701 					"block structure");
1702 			rc = PTR_ERR(block);
1703 			goto out_err1;
1704 		}
1705 		device->block = block;
1706 		block->base = device;
1707 	}
1708 
1709 	/* register lcu with alias handling, enable PAV */
1710 	rc = dasd_alias_make_device_known_to_lcu(device);
1711 	if (rc)
1712 		goto out_err2;
1713 
1714 	dasd_eckd_validate_server(device, 0);
1715 
1716 	/* device may report different configuration data after LCU setup */
1717 	rc = dasd_eckd_read_conf(device);
1718 	if (rc)
1719 		goto out_err3;
1720 
1721 	/* Read Feature Codes */
1722 	dasd_eckd_read_features(device);
1723 
1724 	/* Read Device Characteristics */
1725 	rc = dasd_generic_read_dev_chars(device, DASD_ECKD_MAGIC,
1726 					 &private->rdc_data, 64);
1727 	if (rc) {
1728 		DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
1729 				"Read device characteristic failed, rc=%d", rc);
1730 		goto out_err3;
1731 	}
1732 
1733 	if ((device->features & DASD_FEATURE_USERAW) &&
1734 	    !(private->rdc_data.facilities.RT_in_LR)) {
1735 		dev_err(&device->cdev->dev, "The storage server does not "
1736 			"support raw-track access\n");
1737 		rc = -EINVAL;
1738 		goto out_err3;
1739 	}
1740 
1741 	/* find the valid cylinder size */
1742 	if (private->rdc_data.no_cyl == LV_COMPAT_CYL &&
1743 	    private->rdc_data.long_no_cyl)
1744 		private->real_cyl = private->rdc_data.long_no_cyl;
1745 	else
1746 		private->real_cyl = private->rdc_data.no_cyl;
1747 
1748 	private->fcx_max_data = get_fcx_max_data(device);
1749 
1750 	readonly = dasd_device_is_ro(device);
1751 	if (readonly)
1752 		set_bit(DASD_FLAG_DEVICE_RO, &device->flags);
1753 
1754 	dev_info(&device->cdev->dev, "New DASD %04X/%02X (CU %04X/%02X) "
1755 		 "with %d cylinders, %d heads, %d sectors%s\n",
1756 		 private->rdc_data.dev_type,
1757 		 private->rdc_data.dev_model,
1758 		 private->rdc_data.cu_type,
1759 		 private->rdc_data.cu_model.model,
1760 		 private->real_cyl,
1761 		 private->rdc_data.trk_per_cyl,
1762 		 private->rdc_data.sec_per_trk,
1763 		 readonly ? ", read-only device" : "");
1764 	return 0;
1765 
1766 out_err3:
1767 	dasd_alias_disconnect_device_from_lcu(device);
1768 out_err2:
1769 	dasd_free_block(device->block);
1770 	device->block = NULL;
1771 out_err1:
1772 	kfree(private->conf_data);
1773 	kfree(device->private);
1774 	device->private = NULL;
1775 	return rc;
1776 }
1777 
dasd_eckd_uncheck_device(struct dasd_device * device)1778 static void dasd_eckd_uncheck_device(struct dasd_device *device)
1779 {
1780 	struct dasd_eckd_private *private;
1781 
1782 	private = (struct dasd_eckd_private *) device->private;
1783 	dasd_alias_disconnect_device_from_lcu(device);
1784 	private->ned = NULL;
1785 	private->sneq = NULL;
1786 	private->vdsneq = NULL;
1787 	private->gneq = NULL;
1788 	private->conf_len = 0;
1789 	kfree(private->conf_data);
1790 	private->conf_data = NULL;
1791 }
1792 
1793 static struct dasd_ccw_req *
dasd_eckd_analysis_ccw(struct dasd_device * device)1794 dasd_eckd_analysis_ccw(struct dasd_device *device)
1795 {
1796 	struct dasd_eckd_private *private;
1797 	struct eckd_count *count_data;
1798 	struct LO_eckd_data *LO_data;
1799 	struct dasd_ccw_req *cqr;
1800 	struct ccw1 *ccw;
1801 	int cplength, datasize;
1802 	int i;
1803 
1804 	private = (struct dasd_eckd_private *) device->private;
1805 
1806 	cplength = 8;
1807 	datasize = sizeof(struct DE_eckd_data) + 2*sizeof(struct LO_eckd_data);
1808 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength, datasize, device);
1809 	if (IS_ERR(cqr))
1810 		return cqr;
1811 	ccw = cqr->cpaddr;
1812 	/* Define extent for the first 3 tracks. */
1813 	define_extent(ccw++, cqr->data, 0, 2,
1814 		      DASD_ECKD_CCW_READ_COUNT, device);
1815 	LO_data = cqr->data + sizeof(struct DE_eckd_data);
1816 	/* Locate record for the first 4 records on track 0. */
1817 	ccw[-1].flags |= CCW_FLAG_CC;
1818 	locate_record(ccw++, LO_data++, 0, 0, 4,
1819 		      DASD_ECKD_CCW_READ_COUNT, device, 0);
1820 
1821 	count_data = private->count_area;
1822 	for (i = 0; i < 4; i++) {
1823 		ccw[-1].flags |= CCW_FLAG_CC;
1824 		ccw->cmd_code = DASD_ECKD_CCW_READ_COUNT;
1825 		ccw->flags = 0;
1826 		ccw->count = 8;
1827 		ccw->cda = (__u32)(addr_t) count_data;
1828 		ccw++;
1829 		count_data++;
1830 	}
1831 
1832 	/* Locate record for the first record on track 2. */
1833 	ccw[-1].flags |= CCW_FLAG_CC;
1834 	locate_record(ccw++, LO_data++, 2, 0, 1,
1835 		      DASD_ECKD_CCW_READ_COUNT, device, 0);
1836 	/* Read count ccw. */
1837 	ccw[-1].flags |= CCW_FLAG_CC;
1838 	ccw->cmd_code = DASD_ECKD_CCW_READ_COUNT;
1839 	ccw->flags = 0;
1840 	ccw->count = 8;
1841 	ccw->cda = (__u32)(addr_t) count_data;
1842 
1843 	cqr->block = NULL;
1844 	cqr->startdev = device;
1845 	cqr->memdev = device;
1846 	cqr->retries = 255;
1847 	cqr->buildclk = get_tod_clock();
1848 	cqr->status = DASD_CQR_FILLED;
1849 	return cqr;
1850 }
1851 
1852 /* differentiate between 'no record found' and any other error */
dasd_eckd_analysis_evaluation(struct dasd_ccw_req * init_cqr)1853 static int dasd_eckd_analysis_evaluation(struct dasd_ccw_req *init_cqr)
1854 {
1855 	char *sense;
1856 	if (init_cqr->status == DASD_CQR_DONE)
1857 		return INIT_CQR_OK;
1858 	else if (init_cqr->status == DASD_CQR_NEED_ERP ||
1859 		 init_cqr->status == DASD_CQR_FAILED) {
1860 		sense = dasd_get_sense(&init_cqr->irb);
1861 		if (sense && (sense[1] & SNS1_NO_REC_FOUND))
1862 			return INIT_CQR_UNFORMATTED;
1863 		else
1864 			return INIT_CQR_ERROR;
1865 	} else
1866 		return INIT_CQR_ERROR;
1867 }
1868 
1869 /*
1870  * This is the callback function for the init_analysis cqr. It saves
1871  * the status of the initial analysis ccw before it frees it and kicks
1872  * the device to continue the startup sequence. This will call
1873  * dasd_eckd_do_analysis again (if the devices has not been marked
1874  * for deletion in the meantime).
1875  */
dasd_eckd_analysis_callback(struct dasd_ccw_req * init_cqr,void * data)1876 static void dasd_eckd_analysis_callback(struct dasd_ccw_req *init_cqr,
1877 					void *data)
1878 {
1879 	struct dasd_eckd_private *private;
1880 	struct dasd_device *device;
1881 
1882 	device = init_cqr->startdev;
1883 	private = (struct dasd_eckd_private *) device->private;
1884 	private->init_cqr_status = dasd_eckd_analysis_evaluation(init_cqr);
1885 	dasd_sfree_request(init_cqr, device);
1886 	dasd_kick_device(device);
1887 }
1888 
dasd_eckd_start_analysis(struct dasd_block * block)1889 static int dasd_eckd_start_analysis(struct dasd_block *block)
1890 {
1891 	struct dasd_ccw_req *init_cqr;
1892 
1893 	init_cqr = dasd_eckd_analysis_ccw(block->base);
1894 	if (IS_ERR(init_cqr))
1895 		return PTR_ERR(init_cqr);
1896 	init_cqr->callback = dasd_eckd_analysis_callback;
1897 	init_cqr->callback_data = NULL;
1898 	init_cqr->expires = 5*HZ;
1899 	/* first try without ERP, so we can later handle unformatted
1900 	 * devices as special case
1901 	 */
1902 	clear_bit(DASD_CQR_FLAGS_USE_ERP, &init_cqr->flags);
1903 	init_cqr->retries = 0;
1904 	dasd_add_request_head(init_cqr);
1905 	return -EAGAIN;
1906 }
1907 
dasd_eckd_end_analysis(struct dasd_block * block)1908 static int dasd_eckd_end_analysis(struct dasd_block *block)
1909 {
1910 	struct dasd_device *device;
1911 	struct dasd_eckd_private *private;
1912 	struct eckd_count *count_area;
1913 	unsigned int sb, blk_per_trk;
1914 	int status, i;
1915 	struct dasd_ccw_req *init_cqr;
1916 
1917 	device = block->base;
1918 	private = (struct dasd_eckd_private *) device->private;
1919 	status = private->init_cqr_status;
1920 	private->init_cqr_status = -1;
1921 	if (status == INIT_CQR_ERROR) {
1922 		/* try again, this time with full ERP */
1923 		init_cqr = dasd_eckd_analysis_ccw(device);
1924 		dasd_sleep_on(init_cqr);
1925 		status = dasd_eckd_analysis_evaluation(init_cqr);
1926 		dasd_sfree_request(init_cqr, device);
1927 	}
1928 
1929 	if (device->features & DASD_FEATURE_USERAW) {
1930 		block->bp_block = DASD_RAW_BLOCKSIZE;
1931 		blk_per_trk = DASD_RAW_BLOCK_PER_TRACK;
1932 		block->s2b_shift = 3;
1933 		goto raw;
1934 	}
1935 
1936 	if (status == INIT_CQR_UNFORMATTED) {
1937 		dev_warn(&device->cdev->dev, "The DASD is not formatted\n");
1938 		return -EMEDIUMTYPE;
1939 	} else if (status == INIT_CQR_ERROR) {
1940 		dev_err(&device->cdev->dev,
1941 			"Detecting the DASD disk layout failed because "
1942 			"of an I/O error\n");
1943 		return -EIO;
1944 	}
1945 
1946 	private->uses_cdl = 1;
1947 	/* Check Track 0 for Compatible Disk Layout */
1948 	count_area = NULL;
1949 	for (i = 0; i < 3; i++) {
1950 		if (private->count_area[i].kl != 4 ||
1951 		    private->count_area[i].dl != dasd_eckd_cdl_reclen(i) - 4 ||
1952 		    private->count_area[i].cyl != 0 ||
1953 		    private->count_area[i].head != count_area_head[i] ||
1954 		    private->count_area[i].record != count_area_rec[i]) {
1955 			private->uses_cdl = 0;
1956 			break;
1957 		}
1958 	}
1959 	if (i == 3)
1960 		count_area = &private->count_area[4];
1961 
1962 	if (private->uses_cdl == 0) {
1963 		for (i = 0; i < 5; i++) {
1964 			if ((private->count_area[i].kl != 0) ||
1965 			    (private->count_area[i].dl !=
1966 			     private->count_area[0].dl) ||
1967 			    private->count_area[i].cyl !=  0 ||
1968 			    private->count_area[i].head != count_area_head[i] ||
1969 			    private->count_area[i].record != count_area_rec[i])
1970 				break;
1971 		}
1972 		if (i == 5)
1973 			count_area = &private->count_area[0];
1974 	} else {
1975 		if (private->count_area[3].record == 1)
1976 			dev_warn(&device->cdev->dev,
1977 				 "Track 0 has no records following the VTOC\n");
1978 	}
1979 
1980 	if (count_area != NULL && count_area->kl == 0) {
1981 		/* we found notthing violating our disk layout */
1982 		if (dasd_check_blocksize(count_area->dl) == 0)
1983 			block->bp_block = count_area->dl;
1984 	}
1985 	if (block->bp_block == 0) {
1986 		dev_warn(&device->cdev->dev,
1987 			 "The disk layout of the DASD is not supported\n");
1988 		return -EMEDIUMTYPE;
1989 	}
1990 	block->s2b_shift = 0;	/* bits to shift 512 to get a block */
1991 	for (sb = 512; sb < block->bp_block; sb = sb << 1)
1992 		block->s2b_shift++;
1993 
1994 	blk_per_trk = recs_per_track(&private->rdc_data, 0, block->bp_block);
1995 
1996 raw:
1997 	block->blocks = (private->real_cyl *
1998 			  private->rdc_data.trk_per_cyl *
1999 			  blk_per_trk);
2000 
2001 	dev_info(&device->cdev->dev,
2002 		 "DASD with %d KB/block, %d KB total size, %d KB/track, "
2003 		 "%s\n", (block->bp_block >> 10),
2004 		 ((private->real_cyl *
2005 		   private->rdc_data.trk_per_cyl *
2006 		   blk_per_trk * (block->bp_block >> 9)) >> 1),
2007 		 ((blk_per_trk * block->bp_block) >> 10),
2008 		 private->uses_cdl ?
2009 		 "compatible disk layout" : "linux disk layout");
2010 
2011 	return 0;
2012 }
2013 
dasd_eckd_do_analysis(struct dasd_block * block)2014 static int dasd_eckd_do_analysis(struct dasd_block *block)
2015 {
2016 	struct dasd_eckd_private *private;
2017 
2018 	private = (struct dasd_eckd_private *) block->base->private;
2019 	if (private->init_cqr_status < 0)
2020 		return dasd_eckd_start_analysis(block);
2021 	else
2022 		return dasd_eckd_end_analysis(block);
2023 }
2024 
dasd_eckd_basic_to_ready(struct dasd_device * device)2025 static int dasd_eckd_basic_to_ready(struct dasd_device *device)
2026 {
2027 	return dasd_alias_add_device(device);
2028 };
2029 
dasd_eckd_online_to_ready(struct dasd_device * device)2030 static int dasd_eckd_online_to_ready(struct dasd_device *device)
2031 {
2032 	cancel_work_sync(&device->reload_device);
2033 	cancel_work_sync(&device->kick_validate);
2034 	return 0;
2035 };
2036 
dasd_eckd_ready_to_basic(struct dasd_device * device)2037 static int dasd_eckd_ready_to_basic(struct dasd_device *device)
2038 {
2039 	return dasd_alias_remove_device(device);
2040 };
2041 
2042 static int
dasd_eckd_fill_geometry(struct dasd_block * block,struct hd_geometry * geo)2043 dasd_eckd_fill_geometry(struct dasd_block *block, struct hd_geometry *geo)
2044 {
2045 	struct dasd_eckd_private *private;
2046 
2047 	private = (struct dasd_eckd_private *) block->base->private;
2048 	if (dasd_check_blocksize(block->bp_block) == 0) {
2049 		geo->sectors = recs_per_track(&private->rdc_data,
2050 					      0, block->bp_block);
2051 	}
2052 	geo->cylinders = private->rdc_data.no_cyl;
2053 	geo->heads = private->rdc_data.trk_per_cyl;
2054 	return 0;
2055 }
2056 
2057 static struct dasd_ccw_req *
dasd_eckd_build_format(struct dasd_device * base,struct format_data_t * fdata)2058 dasd_eckd_build_format(struct dasd_device *base,
2059 		       struct format_data_t *fdata)
2060 {
2061 	struct dasd_eckd_private *base_priv;
2062 	struct dasd_eckd_private *start_priv;
2063 	struct dasd_device *startdev;
2064 	struct dasd_ccw_req *fcp;
2065 	struct eckd_count *ect;
2066 	struct ch_t address;
2067 	struct ccw1 *ccw;
2068 	void *data;
2069 	int rpt;
2070 	int cplength, datasize;
2071 	int i, j;
2072 	int intensity = 0;
2073 	int r0_perm;
2074 	int nr_tracks;
2075 
2076 	startdev = dasd_alias_get_start_dev(base);
2077 	if (!startdev)
2078 		startdev = base;
2079 
2080 	start_priv = (struct dasd_eckd_private *) startdev->private;
2081 	base_priv = (struct dasd_eckd_private *) base->private;
2082 
2083 	rpt = recs_per_track(&base_priv->rdc_data, 0, fdata->blksize);
2084 
2085 	nr_tracks = fdata->stop_unit - fdata->start_unit + 1;
2086 
2087 	/*
2088 	 * fdata->intensity is a bit string that tells us what to do:
2089 	 *   Bit 0: write record zero
2090 	 *   Bit 1: write home address, currently not supported
2091 	 *   Bit 2: invalidate tracks
2092 	 *   Bit 3: use OS/390 compatible disk layout (cdl)
2093 	 *   Bit 4: do not allow storage subsystem to modify record zero
2094 	 * Only some bit combinations do make sense.
2095 	 */
2096 	if (fdata->intensity & 0x10) {
2097 		r0_perm = 0;
2098 		intensity = fdata->intensity & ~0x10;
2099 	} else {
2100 		r0_perm = 1;
2101 		intensity = fdata->intensity;
2102 	}
2103 
2104 	switch (intensity) {
2105 	case 0x00:	/* Normal format */
2106 	case 0x08:	/* Normal format, use cdl. */
2107 		cplength = 2 + (rpt*nr_tracks);
2108 		datasize = sizeof(struct PFX_eckd_data) +
2109 			sizeof(struct LO_eckd_data) +
2110 			rpt * nr_tracks * sizeof(struct eckd_count);
2111 		break;
2112 	case 0x01:	/* Write record zero and format track. */
2113 	case 0x09:	/* Write record zero and format track, use cdl. */
2114 		cplength = 2 + rpt * nr_tracks;
2115 		datasize = sizeof(struct PFX_eckd_data) +
2116 			sizeof(struct LO_eckd_data) +
2117 			sizeof(struct eckd_count) +
2118 			rpt * nr_tracks * sizeof(struct eckd_count);
2119 		break;
2120 	case 0x04:	/* Invalidate track. */
2121 	case 0x0c:	/* Invalidate track, use cdl. */
2122 		cplength = 3;
2123 		datasize = sizeof(struct PFX_eckd_data) +
2124 			sizeof(struct LO_eckd_data) +
2125 			sizeof(struct eckd_count);
2126 		break;
2127 	default:
2128 		dev_warn(&startdev->cdev->dev,
2129 			 "An I/O control call used incorrect flags 0x%x\n",
2130 			 fdata->intensity);
2131 		return ERR_PTR(-EINVAL);
2132 	}
2133 	/* Allocate the format ccw request. */
2134 	fcp = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength,
2135 				   datasize, startdev);
2136 	if (IS_ERR(fcp))
2137 		return fcp;
2138 
2139 	start_priv->count++;
2140 	data = fcp->data;
2141 	ccw = fcp->cpaddr;
2142 
2143 	switch (intensity & ~0x08) {
2144 	case 0x00: /* Normal format. */
2145 		prefix(ccw++, (struct PFX_eckd_data *) data,
2146 		       fdata->start_unit, fdata->stop_unit,
2147 		       DASD_ECKD_CCW_WRITE_CKD, base, startdev);
2148 		/* grant subsystem permission to format R0 */
2149 		if (r0_perm)
2150 			((struct PFX_eckd_data *)data)
2151 				->define_extent.ga_extended |= 0x04;
2152 		data += sizeof(struct PFX_eckd_data);
2153 		ccw[-1].flags |= CCW_FLAG_CC;
2154 		locate_record(ccw++, (struct LO_eckd_data *) data,
2155 			      fdata->start_unit, 0, rpt*nr_tracks,
2156 			      DASD_ECKD_CCW_WRITE_CKD, base,
2157 			      fdata->blksize);
2158 		data += sizeof(struct LO_eckd_data);
2159 		break;
2160 	case 0x01: /* Write record zero + format track. */
2161 		prefix(ccw++, (struct PFX_eckd_data *) data,
2162 		       fdata->start_unit, fdata->stop_unit,
2163 		       DASD_ECKD_CCW_WRITE_RECORD_ZERO,
2164 		       base, startdev);
2165 		data += sizeof(struct PFX_eckd_data);
2166 		ccw[-1].flags |= CCW_FLAG_CC;
2167 		locate_record(ccw++, (struct LO_eckd_data *) data,
2168 			      fdata->start_unit, 0, rpt * nr_tracks + 1,
2169 			      DASD_ECKD_CCW_WRITE_RECORD_ZERO, base,
2170 			      base->block->bp_block);
2171 		data += sizeof(struct LO_eckd_data);
2172 		break;
2173 	case 0x04: /* Invalidate track. */
2174 		prefix(ccw++, (struct PFX_eckd_data *) data,
2175 		       fdata->start_unit, fdata->stop_unit,
2176 		       DASD_ECKD_CCW_WRITE_CKD, base, startdev);
2177 		data += sizeof(struct PFX_eckd_data);
2178 		ccw[-1].flags |= CCW_FLAG_CC;
2179 		locate_record(ccw++, (struct LO_eckd_data *) data,
2180 			      fdata->start_unit, 0, 1,
2181 			      DASD_ECKD_CCW_WRITE_CKD, base, 8);
2182 		data += sizeof(struct LO_eckd_data);
2183 		break;
2184 	}
2185 
2186 	for (j = 0; j < nr_tracks; j++) {
2187 		/* calculate cylinder and head for the current track */
2188 		set_ch_t(&address,
2189 			 (fdata->start_unit + j) /
2190 			 base_priv->rdc_data.trk_per_cyl,
2191 			 (fdata->start_unit + j) %
2192 			 base_priv->rdc_data.trk_per_cyl);
2193 		if (intensity & 0x01) {	/* write record zero */
2194 			ect = (struct eckd_count *) data;
2195 			data += sizeof(struct eckd_count);
2196 			ect->cyl = address.cyl;
2197 			ect->head = address.head;
2198 			ect->record = 0;
2199 			ect->kl = 0;
2200 			ect->dl = 8;
2201 			ccw[-1].flags |= CCW_FLAG_CC;
2202 			ccw->cmd_code = DASD_ECKD_CCW_WRITE_RECORD_ZERO;
2203 			ccw->flags = CCW_FLAG_SLI;
2204 			ccw->count = 8;
2205 			ccw->cda = (__u32)(addr_t) ect;
2206 			ccw++;
2207 		}
2208 		if ((intensity & ~0x08) & 0x04) {	/* erase track */
2209 			ect = (struct eckd_count *) data;
2210 			data += sizeof(struct eckd_count);
2211 			ect->cyl = address.cyl;
2212 			ect->head = address.head;
2213 			ect->record = 1;
2214 			ect->kl = 0;
2215 			ect->dl = 0;
2216 			ccw[-1].flags |= CCW_FLAG_CC;
2217 			ccw->cmd_code = DASD_ECKD_CCW_WRITE_CKD;
2218 			ccw->flags = CCW_FLAG_SLI;
2219 			ccw->count = 8;
2220 			ccw->cda = (__u32)(addr_t) ect;
2221 		} else {		/* write remaining records */
2222 			for (i = 0; i < rpt; i++) {
2223 				ect = (struct eckd_count *) data;
2224 				data += sizeof(struct eckd_count);
2225 				ect->cyl = address.cyl;
2226 				ect->head = address.head;
2227 				ect->record = i + 1;
2228 				ect->kl = 0;
2229 				ect->dl = fdata->blksize;
2230 				/*
2231 				 * Check for special tracks 0-1
2232 				 * when formatting CDL
2233 				 */
2234 				if ((intensity & 0x08) &&
2235 				    fdata->start_unit == 0) {
2236 					if (i < 3) {
2237 						ect->kl = 4;
2238 						ect->dl = sizes_trk0[i] - 4;
2239 					}
2240 				}
2241 				if ((intensity & 0x08) &&
2242 				    fdata->start_unit == 1) {
2243 					ect->kl = 44;
2244 					ect->dl = LABEL_SIZE - 44;
2245 				}
2246 				ccw[-1].flags |= CCW_FLAG_CC;
2247 				if (i != 0 || j == 0)
2248 					ccw->cmd_code =
2249 						DASD_ECKD_CCW_WRITE_CKD;
2250 				else
2251 					ccw->cmd_code =
2252 						DASD_ECKD_CCW_WRITE_CKD_MT;
2253 				ccw->flags = CCW_FLAG_SLI;
2254 				ccw->count = 8;
2255 					ccw->cda = (__u32)(addr_t) ect;
2256 					ccw++;
2257 			}
2258 		}
2259 	}
2260 
2261 	fcp->startdev = startdev;
2262 	fcp->memdev = startdev;
2263 	fcp->retries = 256;
2264 	fcp->expires = startdev->default_expires * HZ;
2265 	fcp->buildclk = get_tod_clock();
2266 	fcp->status = DASD_CQR_FILLED;
2267 
2268 	return fcp;
2269 }
2270 
2271 static int
dasd_eckd_format_device(struct dasd_device * base,struct format_data_t * fdata)2272 dasd_eckd_format_device(struct dasd_device *base,
2273 			struct format_data_t *fdata)
2274 {
2275 	struct dasd_ccw_req *cqr, *n;
2276 	struct dasd_block *block;
2277 	struct dasd_eckd_private *private;
2278 	struct list_head format_queue;
2279 	struct dasd_device *device;
2280 	int old_stop, format_step;
2281 	int step, rc = 0;
2282 
2283 	block = base->block;
2284 	private = (struct dasd_eckd_private *) base->private;
2285 
2286 	/* Sanity checks. */
2287 	if (fdata->start_unit >=
2288 	    (private->real_cyl * private->rdc_data.trk_per_cyl)) {
2289 		dev_warn(&base->cdev->dev,
2290 			 "Start track number %u used in formatting is too big\n",
2291 			 fdata->start_unit);
2292 		return -EINVAL;
2293 	}
2294 	if (fdata->stop_unit >=
2295 	    (private->real_cyl * private->rdc_data.trk_per_cyl)) {
2296 		dev_warn(&base->cdev->dev,
2297 			 "Stop track number %u used in formatting is too big\n",
2298 			 fdata->stop_unit);
2299 		return -EINVAL;
2300 	}
2301 	if (fdata->start_unit > fdata->stop_unit) {
2302 		dev_warn(&base->cdev->dev,
2303 			 "Start track %u used in formatting exceeds end track\n",
2304 			 fdata->start_unit);
2305 		return -EINVAL;
2306 	}
2307 	if (dasd_check_blocksize(fdata->blksize) != 0) {
2308 		dev_warn(&base->cdev->dev,
2309 			 "The DASD cannot be formatted with block size %u\n",
2310 			 fdata->blksize);
2311 		return -EINVAL;
2312 	}
2313 
2314 	INIT_LIST_HEAD(&format_queue);
2315 	old_stop = fdata->stop_unit;
2316 
2317 	while (fdata->start_unit <= 1) {
2318 		fdata->stop_unit = fdata->start_unit;
2319 		cqr = dasd_eckd_build_format(base, fdata);
2320 		list_add(&cqr->blocklist, &format_queue);
2321 
2322 		fdata->stop_unit = old_stop;
2323 		fdata->start_unit++;
2324 
2325 		if (fdata->start_unit > fdata->stop_unit)
2326 			goto sleep;
2327 	}
2328 
2329 retry:
2330 	format_step = 255 / recs_per_track(&private->rdc_data, 0,
2331 					   fdata->blksize);
2332 	while (fdata->start_unit <= old_stop) {
2333 		step = fdata->stop_unit - fdata->start_unit + 1;
2334 		if (step > format_step)
2335 			fdata->stop_unit = fdata->start_unit + format_step - 1;
2336 
2337 		cqr = dasd_eckd_build_format(base, fdata);
2338 		if (IS_ERR(cqr)) {
2339 			if (PTR_ERR(cqr) == -ENOMEM) {
2340 				/*
2341 				 * not enough memory available
2342 				 * go to out and start requests
2343 				 * retry after first requests were finished
2344 				 */
2345 				fdata->stop_unit = old_stop;
2346 				goto sleep;
2347 			} else
2348 				return PTR_ERR(cqr);
2349 		}
2350 		list_add(&cqr->blocklist, &format_queue);
2351 
2352 		fdata->start_unit = fdata->stop_unit + 1;
2353 		fdata->stop_unit = old_stop;
2354 	}
2355 
2356 sleep:
2357 	dasd_sleep_on_queue(&format_queue);
2358 
2359 	list_for_each_entry_safe(cqr, n, &format_queue, blocklist) {
2360 		device = cqr->startdev;
2361 		private = (struct dasd_eckd_private *) device->private;
2362 		if (cqr->status == DASD_CQR_FAILED)
2363 			rc = -EIO;
2364 		list_del_init(&cqr->blocklist);
2365 		dasd_sfree_request(cqr, device);
2366 		private->count--;
2367 	}
2368 
2369 	/*
2370 	 * in case of ENOMEM we need to retry after
2371 	 * first requests are finished
2372 	 */
2373 	if (fdata->start_unit <= fdata->stop_unit)
2374 		goto retry;
2375 
2376 	return rc;
2377 }
2378 
dasd_eckd_handle_terminated_request(struct dasd_ccw_req * cqr)2379 static void dasd_eckd_handle_terminated_request(struct dasd_ccw_req *cqr)
2380 {
2381 	cqr->status = DASD_CQR_FILLED;
2382 	if (cqr->block && (cqr->startdev != cqr->block->base)) {
2383 		dasd_eckd_reset_ccw_to_base_io(cqr);
2384 		cqr->startdev = cqr->block->base;
2385 		cqr->lpm = cqr->block->base->path_data.opm;
2386 	}
2387 };
2388 
2389 static dasd_erp_fn_t
dasd_eckd_erp_action(struct dasd_ccw_req * cqr)2390 dasd_eckd_erp_action(struct dasd_ccw_req * cqr)
2391 {
2392 	struct dasd_device *device = (struct dasd_device *) cqr->startdev;
2393 	struct ccw_device *cdev = device->cdev;
2394 
2395 	switch (cdev->id.cu_type) {
2396 	case 0x3990:
2397 	case 0x2105:
2398 	case 0x2107:
2399 	case 0x1750:
2400 		return dasd_3990_erp_action;
2401 	case 0x9343:
2402 	case 0x3880:
2403 	default:
2404 		return dasd_default_erp_action;
2405 	}
2406 }
2407 
2408 static dasd_erp_fn_t
dasd_eckd_erp_postaction(struct dasd_ccw_req * cqr)2409 dasd_eckd_erp_postaction(struct dasd_ccw_req * cqr)
2410 {
2411 	return dasd_default_erp_postaction;
2412 }
2413 
dasd_eckd_check_for_device_change(struct dasd_device * device,struct dasd_ccw_req * cqr,struct irb * irb)2414 static void dasd_eckd_check_for_device_change(struct dasd_device *device,
2415 					      struct dasd_ccw_req *cqr,
2416 					      struct irb *irb)
2417 {
2418 	char mask;
2419 	char *sense = NULL;
2420 	struct dasd_eckd_private *private;
2421 
2422 	private = (struct dasd_eckd_private *) device->private;
2423 	/* first of all check for state change pending interrupt */
2424 	mask = DEV_STAT_ATTENTION | DEV_STAT_DEV_END | DEV_STAT_UNIT_EXCEP;
2425 	if ((scsw_dstat(&irb->scsw) & mask) == mask) {
2426 		/*
2427 		 * for alias only, not in offline processing
2428 		 * and only if not suspended
2429 		 */
2430 		if (!device->block && private->lcu &&
2431 		    device->state == DASD_STATE_ONLINE &&
2432 		    !test_bit(DASD_FLAG_OFFLINE, &device->flags) &&
2433 		    !test_bit(DASD_FLAG_SUSPENDED, &device->flags)) {
2434 			/*
2435 			 * the state change could be caused by an alias
2436 			 * reassignment remove device from alias handling
2437 			 * to prevent new requests from being scheduled on
2438 			 * the wrong alias device
2439 			 */
2440 			dasd_alias_remove_device(device);
2441 
2442 			/* schedule worker to reload device */
2443 			dasd_reload_device(device);
2444 		}
2445 		dasd_generic_handle_state_change(device);
2446 		return;
2447 	}
2448 
2449 	sense = dasd_get_sense(irb);
2450 	if (!sense)
2451 		return;
2452 
2453 	/* summary unit check */
2454 	if ((sense[27] & DASD_SENSE_BIT_0) && (sense[7] == 0x0D) &&
2455 	    (scsw_dstat(&irb->scsw) & DEV_STAT_UNIT_CHECK)) {
2456 		dasd_alias_handle_summary_unit_check(device, irb);
2457 		return;
2458 	}
2459 
2460 	/* service information message SIM */
2461 	if (!cqr && !(sense[27] & DASD_SENSE_BIT_0) &&
2462 	    ((sense[6] & DASD_SIM_SENSE) == DASD_SIM_SENSE)) {
2463 		dasd_3990_erp_handle_sim(device, sense);
2464 		return;
2465 	}
2466 
2467 	/* loss of device reservation is handled via base devices only
2468 	 * as alias devices may be used with several bases
2469 	 */
2470 	if (device->block && (sense[27] & DASD_SENSE_BIT_0) &&
2471 	    (sense[7] == 0x3F) &&
2472 	    (scsw_dstat(&irb->scsw) & DEV_STAT_UNIT_CHECK) &&
2473 	    test_bit(DASD_FLAG_IS_RESERVED, &device->flags)) {
2474 		if (device->features & DASD_FEATURE_FAILONSLCK)
2475 			set_bit(DASD_FLAG_LOCK_STOLEN, &device->flags);
2476 		clear_bit(DASD_FLAG_IS_RESERVED, &device->flags);
2477 		dev_err(&device->cdev->dev,
2478 			"The device reservation was lost\n");
2479 	}
2480 }
2481 
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)2482 static struct dasd_ccw_req *dasd_eckd_build_cp_cmd_single(
2483 					       struct dasd_device *startdev,
2484 					       struct dasd_block *block,
2485 					       struct request *req,
2486 					       sector_t first_rec,
2487 					       sector_t last_rec,
2488 					       sector_t first_trk,
2489 					       sector_t last_trk,
2490 					       unsigned int first_offs,
2491 					       unsigned int last_offs,
2492 					       unsigned int blk_per_trk,
2493 					       unsigned int blksize)
2494 {
2495 	struct dasd_eckd_private *private;
2496 	unsigned long *idaws;
2497 	struct LO_eckd_data *LO_data;
2498 	struct dasd_ccw_req *cqr;
2499 	struct ccw1 *ccw;
2500 	struct req_iterator iter;
2501 	struct bio_vec *bv;
2502 	char *dst;
2503 	unsigned int off;
2504 	int count, cidaw, cplength, datasize;
2505 	sector_t recid;
2506 	unsigned char cmd, rcmd;
2507 	int use_prefix;
2508 	struct dasd_device *basedev;
2509 
2510 	basedev = block->base;
2511 	private = (struct dasd_eckd_private *) basedev->private;
2512 	if (rq_data_dir(req) == READ)
2513 		cmd = DASD_ECKD_CCW_READ_MT;
2514 	else if (rq_data_dir(req) == WRITE)
2515 		cmd = DASD_ECKD_CCW_WRITE_MT;
2516 	else
2517 		return ERR_PTR(-EINVAL);
2518 
2519 	/* Check struct bio and count the number of blocks for the request. */
2520 	count = 0;
2521 	cidaw = 0;
2522 	rq_for_each_segment(bv, req, iter) {
2523 		if (bv->bv_len & (blksize - 1))
2524 			/* Eckd can only do full blocks. */
2525 			return ERR_PTR(-EINVAL);
2526 		count += bv->bv_len >> (block->s2b_shift + 9);
2527 #if defined(CONFIG_64BIT)
2528 		if (idal_is_needed (page_address(bv->bv_page), bv->bv_len))
2529 			cidaw += bv->bv_len >> (block->s2b_shift + 9);
2530 #endif
2531 	}
2532 	/* Paranoia. */
2533 	if (count != last_rec - first_rec + 1)
2534 		return ERR_PTR(-EINVAL);
2535 
2536 	/* use the prefix command if available */
2537 	use_prefix = private->features.feature[8] & 0x01;
2538 	if (use_prefix) {
2539 		/* 1x prefix + number of blocks */
2540 		cplength = 2 + count;
2541 		/* 1x prefix + cidaws*sizeof(long) */
2542 		datasize = sizeof(struct PFX_eckd_data) +
2543 			sizeof(struct LO_eckd_data) +
2544 			cidaw * sizeof(unsigned long);
2545 	} else {
2546 		/* 1x define extent + 1x locate record + number of blocks */
2547 		cplength = 2 + count;
2548 		/* 1x define extent + 1x locate record + cidaws*sizeof(long) */
2549 		datasize = sizeof(struct DE_eckd_data) +
2550 			sizeof(struct LO_eckd_data) +
2551 			cidaw * sizeof(unsigned long);
2552 	}
2553 	/* Find out the number of additional locate record ccws for cdl. */
2554 	if (private->uses_cdl && first_rec < 2*blk_per_trk) {
2555 		if (last_rec >= 2*blk_per_trk)
2556 			count = 2*blk_per_trk - first_rec;
2557 		cplength += count;
2558 		datasize += count*sizeof(struct LO_eckd_data);
2559 	}
2560 	/* Allocate the ccw request. */
2561 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength, datasize,
2562 				   startdev);
2563 	if (IS_ERR(cqr))
2564 		return cqr;
2565 	ccw = cqr->cpaddr;
2566 	/* First ccw is define extent or prefix. */
2567 	if (use_prefix) {
2568 		if (prefix(ccw++, cqr->data, first_trk,
2569 			   last_trk, cmd, basedev, startdev) == -EAGAIN) {
2570 			/* Clock not in sync and XRC is enabled.
2571 			 * Try again later.
2572 			 */
2573 			dasd_sfree_request(cqr, startdev);
2574 			return ERR_PTR(-EAGAIN);
2575 		}
2576 		idaws = (unsigned long *) (cqr->data +
2577 					   sizeof(struct PFX_eckd_data));
2578 	} else {
2579 		if (define_extent(ccw++, cqr->data, first_trk,
2580 				  last_trk, cmd, basedev) == -EAGAIN) {
2581 			/* Clock not in sync and XRC is enabled.
2582 			 * Try again later.
2583 			 */
2584 			dasd_sfree_request(cqr, startdev);
2585 			return ERR_PTR(-EAGAIN);
2586 		}
2587 		idaws = (unsigned long *) (cqr->data +
2588 					   sizeof(struct DE_eckd_data));
2589 	}
2590 	/* Build locate_record+read/write/ccws. */
2591 	LO_data = (struct LO_eckd_data *) (idaws + cidaw);
2592 	recid = first_rec;
2593 	if (private->uses_cdl == 0 || recid > 2*blk_per_trk) {
2594 		/* Only standard blocks so there is just one locate record. */
2595 		ccw[-1].flags |= CCW_FLAG_CC;
2596 		locate_record(ccw++, LO_data++, first_trk, first_offs + 1,
2597 			      last_rec - recid + 1, cmd, basedev, blksize);
2598 	}
2599 	rq_for_each_segment(bv, req, iter) {
2600 		dst = page_address(bv->bv_page) + bv->bv_offset;
2601 		if (dasd_page_cache) {
2602 			char *copy = kmem_cache_alloc(dasd_page_cache,
2603 						      GFP_DMA | __GFP_NOWARN);
2604 			if (copy && rq_data_dir(req) == WRITE)
2605 				memcpy(copy + bv->bv_offset, dst, bv->bv_len);
2606 			if (copy)
2607 				dst = copy + bv->bv_offset;
2608 		}
2609 		for (off = 0; off < bv->bv_len; off += blksize) {
2610 			sector_t trkid = recid;
2611 			unsigned int recoffs = sector_div(trkid, blk_per_trk);
2612 			rcmd = cmd;
2613 			count = blksize;
2614 			/* Locate record for cdl special block ? */
2615 			if (private->uses_cdl && recid < 2*blk_per_trk) {
2616 				if (dasd_eckd_cdl_special(blk_per_trk, recid)){
2617 					rcmd |= 0x8;
2618 					count = dasd_eckd_cdl_reclen(recid);
2619 					if (count < blksize &&
2620 					    rq_data_dir(req) == READ)
2621 						memset(dst + count, 0xe5,
2622 						       blksize - count);
2623 				}
2624 				ccw[-1].flags |= CCW_FLAG_CC;
2625 				locate_record(ccw++, LO_data++,
2626 					      trkid, recoffs + 1,
2627 					      1, rcmd, basedev, count);
2628 			}
2629 			/* Locate record for standard blocks ? */
2630 			if (private->uses_cdl && recid == 2*blk_per_trk) {
2631 				ccw[-1].flags |= CCW_FLAG_CC;
2632 				locate_record(ccw++, LO_data++,
2633 					      trkid, recoffs + 1,
2634 					      last_rec - recid + 1,
2635 					      cmd, basedev, count);
2636 			}
2637 			/* Read/write ccw. */
2638 			ccw[-1].flags |= CCW_FLAG_CC;
2639 			ccw->cmd_code = rcmd;
2640 			ccw->count = count;
2641 			if (idal_is_needed(dst, blksize)) {
2642 				ccw->cda = (__u32)(addr_t) idaws;
2643 				ccw->flags = CCW_FLAG_IDA;
2644 				idaws = idal_create_words(idaws, dst, blksize);
2645 			} else {
2646 				ccw->cda = (__u32)(addr_t) dst;
2647 				ccw->flags = 0;
2648 			}
2649 			ccw++;
2650 			dst += blksize;
2651 			recid++;
2652 		}
2653 	}
2654 	if (blk_noretry_request(req) ||
2655 	    block->base->features & DASD_FEATURE_FAILFAST)
2656 		set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
2657 	cqr->startdev = startdev;
2658 	cqr->memdev = startdev;
2659 	cqr->block = block;
2660 	cqr->expires = startdev->default_expires * HZ;	/* default 5 minutes */
2661 	cqr->lpm = startdev->path_data.ppm;
2662 	cqr->retries = 256;
2663 	cqr->buildclk = get_tod_clock();
2664 	cqr->status = DASD_CQR_FILLED;
2665 	return cqr;
2666 }
2667 
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)2668 static struct dasd_ccw_req *dasd_eckd_build_cp_cmd_track(
2669 					       struct dasd_device *startdev,
2670 					       struct dasd_block *block,
2671 					       struct request *req,
2672 					       sector_t first_rec,
2673 					       sector_t last_rec,
2674 					       sector_t first_trk,
2675 					       sector_t last_trk,
2676 					       unsigned int first_offs,
2677 					       unsigned int last_offs,
2678 					       unsigned int blk_per_trk,
2679 					       unsigned int blksize)
2680 {
2681 	unsigned long *idaws;
2682 	struct dasd_ccw_req *cqr;
2683 	struct ccw1 *ccw;
2684 	struct req_iterator iter;
2685 	struct bio_vec *bv;
2686 	char *dst, *idaw_dst;
2687 	unsigned int cidaw, cplength, datasize;
2688 	unsigned int tlf;
2689 	sector_t recid;
2690 	unsigned char cmd;
2691 	struct dasd_device *basedev;
2692 	unsigned int trkcount, count, count_to_trk_end;
2693 	unsigned int idaw_len, seg_len, part_len, len_to_track_end;
2694 	unsigned char new_track, end_idaw;
2695 	sector_t trkid;
2696 	unsigned int recoffs;
2697 
2698 	basedev = block->base;
2699 	if (rq_data_dir(req) == READ)
2700 		cmd = DASD_ECKD_CCW_READ_TRACK_DATA;
2701 	else if (rq_data_dir(req) == WRITE)
2702 		cmd = DASD_ECKD_CCW_WRITE_TRACK_DATA;
2703 	else
2704 		return ERR_PTR(-EINVAL);
2705 
2706 	/* Track based I/O needs IDAWs for each page, and not just for
2707 	 * 64 bit addresses. We need additional idals for pages
2708 	 * that get filled from two tracks, so we use the number
2709 	 * of records as upper limit.
2710 	 */
2711 	cidaw = last_rec - first_rec + 1;
2712 	trkcount = last_trk - first_trk + 1;
2713 
2714 	/* 1x prefix + one read/write ccw per track */
2715 	cplength = 1 + trkcount;
2716 
2717 	/* on 31-bit we need space for two 32 bit addresses per page
2718 	 * on 64-bit one 64 bit address
2719 	 */
2720 	datasize = sizeof(struct PFX_eckd_data) +
2721 		cidaw * sizeof(unsigned long long);
2722 
2723 	/* Allocate the ccw request. */
2724 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength, datasize,
2725 				   startdev);
2726 	if (IS_ERR(cqr))
2727 		return cqr;
2728 	ccw = cqr->cpaddr;
2729 	/* transfer length factor: how many bytes to read from the last track */
2730 	if (first_trk == last_trk)
2731 		tlf = last_offs - first_offs + 1;
2732 	else
2733 		tlf = last_offs + 1;
2734 	tlf *= blksize;
2735 
2736 	if (prefix_LRE(ccw++, cqr->data, first_trk,
2737 		       last_trk, cmd, basedev, startdev,
2738 		       1 /* format */, first_offs + 1,
2739 		       trkcount, blksize,
2740 		       tlf) == -EAGAIN) {
2741 		/* Clock not in sync and XRC is enabled.
2742 		 * Try again later.
2743 		 */
2744 		dasd_sfree_request(cqr, startdev);
2745 		return ERR_PTR(-EAGAIN);
2746 	}
2747 
2748 	/*
2749 	 * The translation of request into ccw programs must meet the
2750 	 * following conditions:
2751 	 * - all idaws but the first and the last must address full pages
2752 	 *   (or 2K blocks on 31-bit)
2753 	 * - the scope of a ccw and it's idal ends with the track boundaries
2754 	 */
2755 	idaws = (unsigned long *) (cqr->data + sizeof(struct PFX_eckd_data));
2756 	recid = first_rec;
2757 	new_track = 1;
2758 	end_idaw = 0;
2759 	len_to_track_end = 0;
2760 	idaw_dst = NULL;
2761 	idaw_len = 0;
2762 	rq_for_each_segment(bv, req, iter) {
2763 		dst = page_address(bv->bv_page) + bv->bv_offset;
2764 		seg_len = bv->bv_len;
2765 		while (seg_len) {
2766 			if (new_track) {
2767 				trkid = recid;
2768 				recoffs = sector_div(trkid, blk_per_trk);
2769 				count_to_trk_end = blk_per_trk - recoffs;
2770 				count = min((last_rec - recid + 1),
2771 					    (sector_t)count_to_trk_end);
2772 				len_to_track_end = count * blksize;
2773 				ccw[-1].flags |= CCW_FLAG_CC;
2774 				ccw->cmd_code = cmd;
2775 				ccw->count = len_to_track_end;
2776 				ccw->cda = (__u32)(addr_t)idaws;
2777 				ccw->flags = CCW_FLAG_IDA;
2778 				ccw++;
2779 				recid += count;
2780 				new_track = 0;
2781 				/* first idaw for a ccw may start anywhere */
2782 				if (!idaw_dst)
2783 					idaw_dst = dst;
2784 			}
2785 			/* If we start a new idaw, we must make sure that it
2786 			 * starts on an IDA_BLOCK_SIZE boundary.
2787 			 * If we continue an idaw, we must make sure that the
2788 			 * current segment begins where the so far accumulated
2789 			 * idaw ends
2790 			 */
2791 			if (!idaw_dst) {
2792 				if (__pa(dst) & (IDA_BLOCK_SIZE-1)) {
2793 					dasd_sfree_request(cqr, startdev);
2794 					return ERR_PTR(-ERANGE);
2795 				} else
2796 					idaw_dst = dst;
2797 			}
2798 			if ((idaw_dst + idaw_len) != dst) {
2799 				dasd_sfree_request(cqr, startdev);
2800 				return ERR_PTR(-ERANGE);
2801 			}
2802 			part_len = min(seg_len, len_to_track_end);
2803 			seg_len -= part_len;
2804 			dst += part_len;
2805 			idaw_len += part_len;
2806 			len_to_track_end -= part_len;
2807 			/* collected memory area ends on an IDA_BLOCK border,
2808 			 * -> create an idaw
2809 			 * idal_create_words will handle cases where idaw_len
2810 			 * is larger then IDA_BLOCK_SIZE
2811 			 */
2812 			if (!(__pa(idaw_dst + idaw_len) & (IDA_BLOCK_SIZE-1)))
2813 				end_idaw = 1;
2814 			/* We also need to end the idaw at track end */
2815 			if (!len_to_track_end) {
2816 				new_track = 1;
2817 				end_idaw = 1;
2818 			}
2819 			if (end_idaw) {
2820 				idaws = idal_create_words(idaws, idaw_dst,
2821 							  idaw_len);
2822 				idaw_dst = NULL;
2823 				idaw_len = 0;
2824 				end_idaw = 0;
2825 			}
2826 		}
2827 	}
2828 
2829 	if (blk_noretry_request(req) ||
2830 	    block->base->features & DASD_FEATURE_FAILFAST)
2831 		set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
2832 	cqr->startdev = startdev;
2833 	cqr->memdev = startdev;
2834 	cqr->block = block;
2835 	cqr->expires = startdev->default_expires * HZ;	/* default 5 minutes */
2836 	cqr->lpm = startdev->path_data.ppm;
2837 	cqr->retries = 256;
2838 	cqr->buildclk = get_tod_clock();
2839 	cqr->status = DASD_CQR_FILLED;
2840 	return cqr;
2841 }
2842 
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)2843 static int prepare_itcw(struct itcw *itcw,
2844 			unsigned int trk, unsigned int totrk, int cmd,
2845 			struct dasd_device *basedev,
2846 			struct dasd_device *startdev,
2847 			unsigned int rec_on_trk, int count,
2848 			unsigned int blksize,
2849 			unsigned int total_data_size,
2850 			unsigned int tlf,
2851 			unsigned int blk_per_trk)
2852 {
2853 	struct PFX_eckd_data pfxdata;
2854 	struct dasd_eckd_private *basepriv, *startpriv;
2855 	struct DE_eckd_data *dedata;
2856 	struct LRE_eckd_data *lredata;
2857 	struct dcw *dcw;
2858 
2859 	u32 begcyl, endcyl;
2860 	u16 heads, beghead, endhead;
2861 	u8 pfx_cmd;
2862 
2863 	int rc = 0;
2864 	int sector = 0;
2865 	int dn, d;
2866 
2867 
2868 	/* setup prefix data */
2869 	basepriv = (struct dasd_eckd_private *) basedev->private;
2870 	startpriv = (struct dasd_eckd_private *) startdev->private;
2871 	dedata = &pfxdata.define_extent;
2872 	lredata = &pfxdata.locate_record;
2873 
2874 	memset(&pfxdata, 0, sizeof(pfxdata));
2875 	pfxdata.format = 1; /* PFX with LRE */
2876 	pfxdata.base_address = basepriv->ned->unit_addr;
2877 	pfxdata.base_lss = basepriv->ned->ID;
2878 	pfxdata.validity.define_extent = 1;
2879 
2880 	/* private uid is kept up to date, conf_data may be outdated */
2881 	if (startpriv->uid.type != UA_BASE_DEVICE) {
2882 		pfxdata.validity.verify_base = 1;
2883 		if (startpriv->uid.type == UA_HYPER_PAV_ALIAS)
2884 			pfxdata.validity.hyper_pav = 1;
2885 	}
2886 
2887 	switch (cmd) {
2888 	case DASD_ECKD_CCW_READ_TRACK_DATA:
2889 		dedata->mask.perm = 0x1;
2890 		dedata->attributes.operation = basepriv->attrib.operation;
2891 		dedata->blk_size = blksize;
2892 		dedata->ga_extended |= 0x42;
2893 		lredata->operation.orientation = 0x0;
2894 		lredata->operation.operation = 0x0C;
2895 		lredata->auxiliary.check_bytes = 0x01;
2896 		pfx_cmd = DASD_ECKD_CCW_PFX_READ;
2897 		break;
2898 	case DASD_ECKD_CCW_WRITE_TRACK_DATA:
2899 		dedata->mask.perm = 0x02;
2900 		dedata->attributes.operation = basepriv->attrib.operation;
2901 		dedata->blk_size = blksize;
2902 		rc = check_XRC_on_prefix(&pfxdata, basedev);
2903 		dedata->ga_extended |= 0x42;
2904 		lredata->operation.orientation = 0x0;
2905 		lredata->operation.operation = 0x3F;
2906 		lredata->extended_operation = 0x23;
2907 		lredata->auxiliary.check_bytes = 0x2;
2908 		pfx_cmd = DASD_ECKD_CCW_PFX;
2909 		break;
2910 	default:
2911 		DBF_DEV_EVENT(DBF_ERR, basedev,
2912 			      "prepare itcw, unknown opcode 0x%x", cmd);
2913 		BUG();
2914 		break;
2915 	}
2916 	if (rc)
2917 		return rc;
2918 
2919 	dedata->attributes.mode = 0x3;	/* ECKD */
2920 
2921 	heads = basepriv->rdc_data.trk_per_cyl;
2922 	begcyl = trk / heads;
2923 	beghead = trk % heads;
2924 	endcyl = totrk / heads;
2925 	endhead = totrk % heads;
2926 
2927 	/* check for sequential prestage - enhance cylinder range */
2928 	if (dedata->attributes.operation == DASD_SEQ_PRESTAGE ||
2929 	    dedata->attributes.operation == DASD_SEQ_ACCESS) {
2930 
2931 		if (endcyl + basepriv->attrib.nr_cyl < basepriv->real_cyl)
2932 			endcyl += basepriv->attrib.nr_cyl;
2933 		else
2934 			endcyl = (basepriv->real_cyl - 1);
2935 	}
2936 
2937 	set_ch_t(&dedata->beg_ext, begcyl, beghead);
2938 	set_ch_t(&dedata->end_ext, endcyl, endhead);
2939 
2940 	dedata->ep_format = 0x20; /* records per track is valid */
2941 	dedata->ep_rec_per_track = blk_per_trk;
2942 
2943 	if (rec_on_trk) {
2944 		switch (basepriv->rdc_data.dev_type) {
2945 		case 0x3390:
2946 			dn = ceil_quot(blksize + 6, 232);
2947 			d = 9 + ceil_quot(blksize + 6 * (dn + 1), 34);
2948 			sector = (49 + (rec_on_trk - 1) * (10 + d)) / 8;
2949 			break;
2950 		case 0x3380:
2951 			d = 7 + ceil_quot(blksize + 12, 32);
2952 			sector = (39 + (rec_on_trk - 1) * (8 + d)) / 7;
2953 			break;
2954 		}
2955 	}
2956 
2957 	lredata->auxiliary.length_valid = 1;
2958 	lredata->auxiliary.length_scope = 1;
2959 	lredata->auxiliary.imbedded_ccw_valid = 1;
2960 	lredata->length = tlf;
2961 	lredata->imbedded_ccw = cmd;
2962 	lredata->count = count;
2963 	lredata->sector = sector;
2964 	set_ch_t(&lredata->seek_addr, begcyl, beghead);
2965 	lredata->search_arg.cyl = lredata->seek_addr.cyl;
2966 	lredata->search_arg.head = lredata->seek_addr.head;
2967 	lredata->search_arg.record = rec_on_trk;
2968 
2969 	dcw = itcw_add_dcw(itcw, pfx_cmd, 0,
2970 		     &pfxdata, sizeof(pfxdata), total_data_size);
2971 	return IS_ERR(dcw) ? PTR_ERR(dcw) : 0;
2972 }
2973 
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)2974 static struct dasd_ccw_req *dasd_eckd_build_cp_tpm_track(
2975 					       struct dasd_device *startdev,
2976 					       struct dasd_block *block,
2977 					       struct request *req,
2978 					       sector_t first_rec,
2979 					       sector_t last_rec,
2980 					       sector_t first_trk,
2981 					       sector_t last_trk,
2982 					       unsigned int first_offs,
2983 					       unsigned int last_offs,
2984 					       unsigned int blk_per_trk,
2985 					       unsigned int blksize)
2986 {
2987 	struct dasd_ccw_req *cqr;
2988 	struct req_iterator iter;
2989 	struct bio_vec *bv;
2990 	char *dst;
2991 	unsigned int trkcount, ctidaw;
2992 	unsigned char cmd;
2993 	struct dasd_device *basedev;
2994 	unsigned int tlf;
2995 	struct itcw *itcw;
2996 	struct tidaw *last_tidaw = NULL;
2997 	int itcw_op;
2998 	size_t itcw_size;
2999 	u8 tidaw_flags;
3000 	unsigned int seg_len, part_len, len_to_track_end;
3001 	unsigned char new_track;
3002 	sector_t recid, trkid;
3003 	unsigned int offs;
3004 	unsigned int count, count_to_trk_end;
3005 	int ret;
3006 
3007 	basedev = block->base;
3008 	if (rq_data_dir(req) == READ) {
3009 		cmd = DASD_ECKD_CCW_READ_TRACK_DATA;
3010 		itcw_op = ITCW_OP_READ;
3011 	} else if (rq_data_dir(req) == WRITE) {
3012 		cmd = DASD_ECKD_CCW_WRITE_TRACK_DATA;
3013 		itcw_op = ITCW_OP_WRITE;
3014 	} else
3015 		return ERR_PTR(-EINVAL);
3016 
3017 	/* trackbased I/O needs address all memory via TIDAWs,
3018 	 * not just for 64 bit addresses. This allows us to map
3019 	 * each segment directly to one tidaw.
3020 	 * In the case of write requests, additional tidaws may
3021 	 * be needed when a segment crosses a track boundary.
3022 	 */
3023 	trkcount = last_trk - first_trk + 1;
3024 	ctidaw = 0;
3025 	rq_for_each_segment(bv, req, iter) {
3026 		++ctidaw;
3027 	}
3028 	if (rq_data_dir(req) == WRITE)
3029 		ctidaw += (last_trk - first_trk);
3030 
3031 	/* Allocate the ccw request. */
3032 	itcw_size = itcw_calc_size(0, ctidaw, 0);
3033 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 0, itcw_size, startdev);
3034 	if (IS_ERR(cqr))
3035 		return cqr;
3036 
3037 	/* transfer length factor: how many bytes to read from the last track */
3038 	if (first_trk == last_trk)
3039 		tlf = last_offs - first_offs + 1;
3040 	else
3041 		tlf = last_offs + 1;
3042 	tlf *= blksize;
3043 
3044 	itcw = itcw_init(cqr->data, itcw_size, itcw_op, 0, ctidaw, 0);
3045 	if (IS_ERR(itcw)) {
3046 		ret = -EINVAL;
3047 		goto out_error;
3048 	}
3049 	cqr->cpaddr = itcw_get_tcw(itcw);
3050 	if (prepare_itcw(itcw, first_trk, last_trk,
3051 			 cmd, basedev, startdev,
3052 			 first_offs + 1,
3053 			 trkcount, blksize,
3054 			 (last_rec - first_rec + 1) * blksize,
3055 			 tlf, blk_per_trk) == -EAGAIN) {
3056 		/* Clock not in sync and XRC is enabled.
3057 		 * Try again later.
3058 		 */
3059 		ret = -EAGAIN;
3060 		goto out_error;
3061 	}
3062 	len_to_track_end = 0;
3063 	/*
3064 	 * A tidaw can address 4k of memory, but must not cross page boundaries
3065 	 * We can let the block layer handle this by setting
3066 	 * blk_queue_segment_boundary to page boundaries and
3067 	 * blk_max_segment_size to page size when setting up the request queue.
3068 	 * For write requests, a TIDAW must not cross track boundaries, because
3069 	 * we have to set the CBC flag on the last tidaw for each track.
3070 	 */
3071 	if (rq_data_dir(req) == WRITE) {
3072 		new_track = 1;
3073 		recid = first_rec;
3074 		rq_for_each_segment(bv, req, iter) {
3075 			dst = page_address(bv->bv_page) + bv->bv_offset;
3076 			seg_len = bv->bv_len;
3077 			while (seg_len) {
3078 				if (new_track) {
3079 					trkid = recid;
3080 					offs = sector_div(trkid, blk_per_trk);
3081 					count_to_trk_end = blk_per_trk - offs;
3082 					count = min((last_rec - recid + 1),
3083 						    (sector_t)count_to_trk_end);
3084 					len_to_track_end = count * blksize;
3085 					recid += count;
3086 					new_track = 0;
3087 				}
3088 				part_len = min(seg_len, len_to_track_end);
3089 				seg_len -= part_len;
3090 				len_to_track_end -= part_len;
3091 				/* We need to end the tidaw at track end */
3092 				if (!len_to_track_end) {
3093 					new_track = 1;
3094 					tidaw_flags = TIDAW_FLAGS_INSERT_CBC;
3095 				} else
3096 					tidaw_flags = 0;
3097 				last_tidaw = itcw_add_tidaw(itcw, tidaw_flags,
3098 							    dst, part_len);
3099 				if (IS_ERR(last_tidaw)) {
3100 					ret = -EINVAL;
3101 					goto out_error;
3102 				}
3103 				dst += part_len;
3104 			}
3105 		}
3106 	} else {
3107 		rq_for_each_segment(bv, req, iter) {
3108 			dst = page_address(bv->bv_page) + bv->bv_offset;
3109 			last_tidaw = itcw_add_tidaw(itcw, 0x00,
3110 						    dst, bv->bv_len);
3111 			if (IS_ERR(last_tidaw)) {
3112 				ret = -EINVAL;
3113 				goto out_error;
3114 			}
3115 		}
3116 	}
3117 	last_tidaw->flags |= TIDAW_FLAGS_LAST;
3118 	last_tidaw->flags &= ~TIDAW_FLAGS_INSERT_CBC;
3119 	itcw_finalize(itcw);
3120 
3121 	if (blk_noretry_request(req) ||
3122 	    block->base->features & DASD_FEATURE_FAILFAST)
3123 		set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
3124 	cqr->cpmode = 1;
3125 	cqr->startdev = startdev;
3126 	cqr->memdev = startdev;
3127 	cqr->block = block;
3128 	cqr->expires = startdev->default_expires * HZ;	/* default 5 minutes */
3129 	cqr->lpm = startdev->path_data.ppm;
3130 	cqr->retries = 256;
3131 	cqr->buildclk = get_tod_clock();
3132 	cqr->status = DASD_CQR_FILLED;
3133 	return cqr;
3134 out_error:
3135 	dasd_sfree_request(cqr, startdev);
3136 	return ERR_PTR(ret);
3137 }
3138 
dasd_eckd_build_cp(struct dasd_device * startdev,struct dasd_block * block,struct request * req)3139 static struct dasd_ccw_req *dasd_eckd_build_cp(struct dasd_device *startdev,
3140 					       struct dasd_block *block,
3141 					       struct request *req)
3142 {
3143 	int cmdrtd, cmdwtd;
3144 	int use_prefix;
3145 	int fcx_multitrack;
3146 	struct dasd_eckd_private *private;
3147 	struct dasd_device *basedev;
3148 	sector_t first_rec, last_rec;
3149 	sector_t first_trk, last_trk;
3150 	unsigned int first_offs, last_offs;
3151 	unsigned int blk_per_trk, blksize;
3152 	int cdlspecial;
3153 	unsigned int data_size;
3154 	struct dasd_ccw_req *cqr;
3155 
3156 	basedev = block->base;
3157 	private = (struct dasd_eckd_private *) basedev->private;
3158 
3159 	/* Calculate number of blocks/records per track. */
3160 	blksize = block->bp_block;
3161 	blk_per_trk = recs_per_track(&private->rdc_data, 0, blksize);
3162 	if (blk_per_trk == 0)
3163 		return ERR_PTR(-EINVAL);
3164 	/* Calculate record id of first and last block. */
3165 	first_rec = first_trk = blk_rq_pos(req) >> block->s2b_shift;
3166 	first_offs = sector_div(first_trk, blk_per_trk);
3167 	last_rec = last_trk =
3168 		(blk_rq_pos(req) + blk_rq_sectors(req) - 1) >> block->s2b_shift;
3169 	last_offs = sector_div(last_trk, blk_per_trk);
3170 	cdlspecial = (private->uses_cdl && first_rec < 2*blk_per_trk);
3171 
3172 	fcx_multitrack = private->features.feature[40] & 0x20;
3173 	data_size = blk_rq_bytes(req);
3174 	/* tpm write request add CBC data on each track boundary */
3175 	if (rq_data_dir(req) == WRITE)
3176 		data_size += (last_trk - first_trk) * 4;
3177 
3178 	/* is read track data and write track data in command mode supported? */
3179 	cmdrtd = private->features.feature[9] & 0x20;
3180 	cmdwtd = private->features.feature[12] & 0x40;
3181 	use_prefix = private->features.feature[8] & 0x01;
3182 
3183 	cqr = NULL;
3184 	if (cdlspecial || dasd_page_cache) {
3185 		/* do nothing, just fall through to the cmd mode single case */
3186 	} else if ((data_size <= private->fcx_max_data)
3187 		   && (fcx_multitrack || (first_trk == last_trk))) {
3188 		cqr = dasd_eckd_build_cp_tpm_track(startdev, block, req,
3189 						    first_rec, last_rec,
3190 						    first_trk, last_trk,
3191 						    first_offs, last_offs,
3192 						    blk_per_trk, blksize);
3193 		if (IS_ERR(cqr) && (PTR_ERR(cqr) != -EAGAIN) &&
3194 		    (PTR_ERR(cqr) != -ENOMEM))
3195 			cqr = NULL;
3196 	} else if (use_prefix &&
3197 		   (((rq_data_dir(req) == READ) && cmdrtd) ||
3198 		    ((rq_data_dir(req) == WRITE) && cmdwtd))) {
3199 		cqr = dasd_eckd_build_cp_cmd_track(startdev, block, req,
3200 						   first_rec, last_rec,
3201 						   first_trk, last_trk,
3202 						   first_offs, last_offs,
3203 						   blk_per_trk, blksize);
3204 		if (IS_ERR(cqr) && (PTR_ERR(cqr) != -EAGAIN) &&
3205 		    (PTR_ERR(cqr) != -ENOMEM))
3206 			cqr = NULL;
3207 	}
3208 	if (!cqr)
3209 		cqr = dasd_eckd_build_cp_cmd_single(startdev, block, req,
3210 						    first_rec, last_rec,
3211 						    first_trk, last_trk,
3212 						    first_offs, last_offs,
3213 						    blk_per_trk, blksize);
3214 	return cqr;
3215 }
3216 
dasd_raw_build_cp(struct dasd_device * startdev,struct dasd_block * block,struct request * req)3217 static struct dasd_ccw_req *dasd_raw_build_cp(struct dasd_device *startdev,
3218 					       struct dasd_block *block,
3219 					       struct request *req)
3220 {
3221 	unsigned long *idaws;
3222 	struct dasd_device *basedev;
3223 	struct dasd_ccw_req *cqr;
3224 	struct ccw1 *ccw;
3225 	struct req_iterator iter;
3226 	struct bio_vec *bv;
3227 	char *dst;
3228 	unsigned char cmd;
3229 	unsigned int trkcount;
3230 	unsigned int seg_len, len_to_track_end;
3231 	unsigned int first_offs;
3232 	unsigned int cidaw, cplength, datasize;
3233 	sector_t first_trk, last_trk;
3234 	unsigned int pfx_datasize;
3235 
3236 	/*
3237 	 * raw track access needs to be mutiple of 64k and on 64k boundary
3238 	 */
3239 	if ((blk_rq_pos(req) % DASD_RAW_SECTORS_PER_TRACK) != 0) {
3240 		cqr = ERR_PTR(-EINVAL);
3241 		goto out;
3242 	}
3243 	if (((blk_rq_pos(req) + blk_rq_sectors(req)) %
3244 	     DASD_RAW_SECTORS_PER_TRACK) != 0) {
3245 		cqr = ERR_PTR(-EINVAL);
3246 		goto out;
3247 	}
3248 
3249 	first_trk = blk_rq_pos(req) / DASD_RAW_SECTORS_PER_TRACK;
3250 	last_trk = (blk_rq_pos(req) + blk_rq_sectors(req) - 1) /
3251 		DASD_RAW_SECTORS_PER_TRACK;
3252 	trkcount = last_trk - first_trk + 1;
3253 	first_offs = 0;
3254 	basedev = block->base;
3255 
3256 	if (rq_data_dir(req) == READ)
3257 		cmd = DASD_ECKD_CCW_READ_TRACK;
3258 	else if (rq_data_dir(req) == WRITE)
3259 		cmd = DASD_ECKD_CCW_WRITE_FULL_TRACK;
3260 	else {
3261 		cqr = ERR_PTR(-EINVAL);
3262 		goto out;
3263 	}
3264 
3265 	/*
3266 	 * Raw track based I/O needs IDAWs for each page,
3267 	 * and not just for 64 bit addresses.
3268 	 */
3269 	cidaw = trkcount * DASD_RAW_BLOCK_PER_TRACK;
3270 
3271 	/* 1x prefix + one read/write ccw per track */
3272 	cplength = 1 + trkcount;
3273 
3274 	/*
3275 	 * struct PFX_eckd_data has up to 2 byte as extended parameter
3276 	 * this is needed for write full track and has to be mentioned
3277 	 * separately
3278 	 * add 8 instead of 2 to keep 8 byte boundary
3279 	 */
3280 	pfx_datasize = sizeof(struct PFX_eckd_data) + 8;
3281 
3282 	datasize = pfx_datasize + cidaw * sizeof(unsigned long long);
3283 
3284 	/* Allocate the ccw request. */
3285 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength,
3286 				   datasize, startdev);
3287 	if (IS_ERR(cqr))
3288 		goto out;
3289 	ccw = cqr->cpaddr;
3290 
3291 	if (prefix_LRE(ccw++, cqr->data, first_trk, last_trk, cmd,
3292 		       basedev, startdev, 1 /* format */, first_offs + 1,
3293 		       trkcount, 0, 0) == -EAGAIN) {
3294 		/* Clock not in sync and XRC is enabled.
3295 		 * Try again later.
3296 		 */
3297 		dasd_sfree_request(cqr, startdev);
3298 		cqr = ERR_PTR(-EAGAIN);
3299 		goto out;
3300 	}
3301 
3302 	idaws = (unsigned long *)(cqr->data + pfx_datasize);
3303 
3304 	len_to_track_end = 0;
3305 
3306 	rq_for_each_segment(bv, req, iter) {
3307 		dst = page_address(bv->bv_page) + bv->bv_offset;
3308 		seg_len = bv->bv_len;
3309 		if (!len_to_track_end) {
3310 			ccw[-1].flags |= CCW_FLAG_CC;
3311 			ccw->cmd_code = cmd;
3312 			/* maximum 3390 track size */
3313 			ccw->count = 57326;
3314 			/* 64k map to one track */
3315 			len_to_track_end = 65536;
3316 			ccw->cda = (__u32)(addr_t)idaws;
3317 			ccw->flags |= CCW_FLAG_IDA;
3318 			ccw->flags |= CCW_FLAG_SLI;
3319 			ccw++;
3320 		}
3321 		len_to_track_end -= seg_len;
3322 		idaws = idal_create_words(idaws, dst, seg_len);
3323 	}
3324 
3325 	if (blk_noretry_request(req) ||
3326 	    block->base->features & DASD_FEATURE_FAILFAST)
3327 		set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
3328 	cqr->startdev = startdev;
3329 	cqr->memdev = startdev;
3330 	cqr->block = block;
3331 	cqr->expires = startdev->default_expires * HZ;
3332 	cqr->lpm = startdev->path_data.ppm;
3333 	cqr->retries = 256;
3334 	cqr->buildclk = get_tod_clock();
3335 	cqr->status = DASD_CQR_FILLED;
3336 
3337 	if (IS_ERR(cqr) && PTR_ERR(cqr) != -EAGAIN)
3338 		cqr = NULL;
3339 out:
3340 	return cqr;
3341 }
3342 
3343 
3344 static int
dasd_eckd_free_cp(struct dasd_ccw_req * cqr,struct request * req)3345 dasd_eckd_free_cp(struct dasd_ccw_req *cqr, struct request *req)
3346 {
3347 	struct dasd_eckd_private *private;
3348 	struct ccw1 *ccw;
3349 	struct req_iterator iter;
3350 	struct bio_vec *bv;
3351 	char *dst, *cda;
3352 	unsigned int blksize, blk_per_trk, off;
3353 	sector_t recid;
3354 	int status;
3355 
3356 	if (!dasd_page_cache)
3357 		goto out;
3358 	private = (struct dasd_eckd_private *) cqr->block->base->private;
3359 	blksize = cqr->block->bp_block;
3360 	blk_per_trk = recs_per_track(&private->rdc_data, 0, blksize);
3361 	recid = blk_rq_pos(req) >> cqr->block->s2b_shift;
3362 	ccw = cqr->cpaddr;
3363 	/* Skip over define extent & locate record. */
3364 	ccw++;
3365 	if (private->uses_cdl == 0 || recid > 2*blk_per_trk)
3366 		ccw++;
3367 	rq_for_each_segment(bv, req, iter) {
3368 		dst = page_address(bv->bv_page) + bv->bv_offset;
3369 		for (off = 0; off < bv->bv_len; off += blksize) {
3370 			/* Skip locate record. */
3371 			if (private->uses_cdl && recid <= 2*blk_per_trk)
3372 				ccw++;
3373 			if (dst) {
3374 				if (ccw->flags & CCW_FLAG_IDA)
3375 					cda = *((char **)((addr_t) ccw->cda));
3376 				else
3377 					cda = (char *)((addr_t) ccw->cda);
3378 				if (dst != cda) {
3379 					if (rq_data_dir(req) == READ)
3380 						memcpy(dst, cda, bv->bv_len);
3381 					kmem_cache_free(dasd_page_cache,
3382 					    (void *)((addr_t)cda & PAGE_MASK));
3383 				}
3384 				dst = NULL;
3385 			}
3386 			ccw++;
3387 			recid++;
3388 		}
3389 	}
3390 out:
3391 	status = cqr->status == DASD_CQR_DONE;
3392 	dasd_sfree_request(cqr, cqr->memdev);
3393 	return status;
3394 }
3395 
3396 /*
3397  * Modify ccw/tcw in cqr so it can be started on a base device.
3398  *
3399  * Note that this is not enough to restart the cqr!
3400  * Either reset cqr->startdev as well (summary unit check handling)
3401  * or restart via separate cqr (as in ERP handling).
3402  */
dasd_eckd_reset_ccw_to_base_io(struct dasd_ccw_req * cqr)3403 void dasd_eckd_reset_ccw_to_base_io(struct dasd_ccw_req *cqr)
3404 {
3405 	struct ccw1 *ccw;
3406 	struct PFX_eckd_data *pfxdata;
3407 	struct tcw *tcw;
3408 	struct tccb *tccb;
3409 	struct dcw *dcw;
3410 
3411 	if (cqr->cpmode == 1) {
3412 		tcw = cqr->cpaddr;
3413 		tccb = tcw_get_tccb(tcw);
3414 		dcw = (struct dcw *)&tccb->tca[0];
3415 		pfxdata = (struct PFX_eckd_data *)&dcw->cd[0];
3416 		pfxdata->validity.verify_base = 0;
3417 		pfxdata->validity.hyper_pav = 0;
3418 	} else {
3419 		ccw = cqr->cpaddr;
3420 		pfxdata = cqr->data;
3421 		if (ccw->cmd_code == DASD_ECKD_CCW_PFX) {
3422 			pfxdata->validity.verify_base = 0;
3423 			pfxdata->validity.hyper_pav = 0;
3424 		}
3425 	}
3426 }
3427 
3428 #define DASD_ECKD_CHANQ_MAX_SIZE 4
3429 
dasd_eckd_build_alias_cp(struct dasd_device * base,struct dasd_block * block,struct request * req)3430 static struct dasd_ccw_req *dasd_eckd_build_alias_cp(struct dasd_device *base,
3431 						     struct dasd_block *block,
3432 						     struct request *req)
3433 {
3434 	struct dasd_eckd_private *private;
3435 	struct dasd_device *startdev;
3436 	unsigned long flags;
3437 	struct dasd_ccw_req *cqr;
3438 
3439 	startdev = dasd_alias_get_start_dev(base);
3440 	if (!startdev)
3441 		startdev = base;
3442 	private = (struct dasd_eckd_private *) startdev->private;
3443 	if (private->count >= DASD_ECKD_CHANQ_MAX_SIZE)
3444 		return ERR_PTR(-EBUSY);
3445 
3446 	spin_lock_irqsave(get_ccwdev_lock(startdev->cdev), flags);
3447 	private->count++;
3448 	if ((base->features & DASD_FEATURE_USERAW))
3449 		cqr = dasd_raw_build_cp(startdev, block, req);
3450 	else
3451 		cqr = dasd_eckd_build_cp(startdev, block, req);
3452 	if (IS_ERR(cqr))
3453 		private->count--;
3454 	spin_unlock_irqrestore(get_ccwdev_lock(startdev->cdev), flags);
3455 	return cqr;
3456 }
3457 
dasd_eckd_free_alias_cp(struct dasd_ccw_req * cqr,struct request * req)3458 static int dasd_eckd_free_alias_cp(struct dasd_ccw_req *cqr,
3459 				   struct request *req)
3460 {
3461 	struct dasd_eckd_private *private;
3462 	unsigned long flags;
3463 
3464 	spin_lock_irqsave(get_ccwdev_lock(cqr->memdev->cdev), flags);
3465 	private = (struct dasd_eckd_private *) cqr->memdev->private;
3466 	private->count--;
3467 	spin_unlock_irqrestore(get_ccwdev_lock(cqr->memdev->cdev), flags);
3468 	return dasd_eckd_free_cp(cqr, req);
3469 }
3470 
3471 static int
dasd_eckd_fill_info(struct dasd_device * device,struct dasd_information2_t * info)3472 dasd_eckd_fill_info(struct dasd_device * device,
3473 		    struct dasd_information2_t * info)
3474 {
3475 	struct dasd_eckd_private *private;
3476 
3477 	private = (struct dasd_eckd_private *) device->private;
3478 	info->label_block = 2;
3479 	info->FBA_layout = private->uses_cdl ? 0 : 1;
3480 	info->format = private->uses_cdl ? DASD_FORMAT_CDL : DASD_FORMAT_LDL;
3481 	info->characteristics_size = sizeof(struct dasd_eckd_characteristics);
3482 	memcpy(info->characteristics, &private->rdc_data,
3483 	       sizeof(struct dasd_eckd_characteristics));
3484 	info->confdata_size = min((unsigned long)private->conf_len,
3485 				  sizeof(info->configuration_data));
3486 	memcpy(info->configuration_data, private->conf_data,
3487 	       info->confdata_size);
3488 	return 0;
3489 }
3490 
3491 /*
3492  * SECTION: ioctl functions for eckd devices.
3493  */
3494 
3495 /*
3496  * Release device ioctl.
3497  * Buils a channel programm to releases a prior reserved
3498  * (see dasd_eckd_reserve) device.
3499  */
3500 static int
dasd_eckd_release(struct dasd_device * device)3501 dasd_eckd_release(struct dasd_device *device)
3502 {
3503 	struct dasd_ccw_req *cqr;
3504 	int rc;
3505 	struct ccw1 *ccw;
3506 	int useglobal;
3507 
3508 	if (!capable(CAP_SYS_ADMIN))
3509 		return -EACCES;
3510 
3511 	useglobal = 0;
3512 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1, 32, device);
3513 	if (IS_ERR(cqr)) {
3514 		mutex_lock(&dasd_reserve_mutex);
3515 		useglobal = 1;
3516 		cqr = &dasd_reserve_req->cqr;
3517 		memset(cqr, 0, sizeof(*cqr));
3518 		memset(&dasd_reserve_req->ccw, 0,
3519 		       sizeof(dasd_reserve_req->ccw));
3520 		cqr->cpaddr = &dasd_reserve_req->ccw;
3521 		cqr->data = &dasd_reserve_req->data;
3522 		cqr->magic = DASD_ECKD_MAGIC;
3523 	}
3524 	ccw = cqr->cpaddr;
3525 	ccw->cmd_code = DASD_ECKD_CCW_RELEASE;
3526 	ccw->flags |= CCW_FLAG_SLI;
3527 	ccw->count = 32;
3528 	ccw->cda = (__u32)(addr_t) cqr->data;
3529 	cqr->startdev = device;
3530 	cqr->memdev = device;
3531 	clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
3532 	set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
3533 	cqr->retries = 2;	/* set retry counter to enable basic ERP */
3534 	cqr->expires = 2 * HZ;
3535 	cqr->buildclk = get_tod_clock();
3536 	cqr->status = DASD_CQR_FILLED;
3537 
3538 	rc = dasd_sleep_on_immediatly(cqr);
3539 	if (!rc)
3540 		clear_bit(DASD_FLAG_IS_RESERVED, &device->flags);
3541 
3542 	if (useglobal)
3543 		mutex_unlock(&dasd_reserve_mutex);
3544 	else
3545 		dasd_sfree_request(cqr, cqr->memdev);
3546 	return rc;
3547 }
3548 
3549 /*
3550  * Reserve device ioctl.
3551  * Options are set to 'synchronous wait for interrupt' and
3552  * 'timeout the request'. This leads to a terminate IO if
3553  * the interrupt is outstanding for a certain time.
3554  */
3555 static int
dasd_eckd_reserve(struct dasd_device * device)3556 dasd_eckd_reserve(struct dasd_device *device)
3557 {
3558 	struct dasd_ccw_req *cqr;
3559 	int rc;
3560 	struct ccw1 *ccw;
3561 	int useglobal;
3562 
3563 	if (!capable(CAP_SYS_ADMIN))
3564 		return -EACCES;
3565 
3566 	useglobal = 0;
3567 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1, 32, device);
3568 	if (IS_ERR(cqr)) {
3569 		mutex_lock(&dasd_reserve_mutex);
3570 		useglobal = 1;
3571 		cqr = &dasd_reserve_req->cqr;
3572 		memset(cqr, 0, sizeof(*cqr));
3573 		memset(&dasd_reserve_req->ccw, 0,
3574 		       sizeof(dasd_reserve_req->ccw));
3575 		cqr->cpaddr = &dasd_reserve_req->ccw;
3576 		cqr->data = &dasd_reserve_req->data;
3577 		cqr->magic = DASD_ECKD_MAGIC;
3578 	}
3579 	ccw = cqr->cpaddr;
3580 	ccw->cmd_code = DASD_ECKD_CCW_RESERVE;
3581 	ccw->flags |= CCW_FLAG_SLI;
3582 	ccw->count = 32;
3583 	ccw->cda = (__u32)(addr_t) cqr->data;
3584 	cqr->startdev = device;
3585 	cqr->memdev = device;
3586 	clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
3587 	set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
3588 	cqr->retries = 2;	/* set retry counter to enable basic ERP */
3589 	cqr->expires = 2 * HZ;
3590 	cqr->buildclk = get_tod_clock();
3591 	cqr->status = DASD_CQR_FILLED;
3592 
3593 	rc = dasd_sleep_on_immediatly(cqr);
3594 	if (!rc)
3595 		set_bit(DASD_FLAG_IS_RESERVED, &device->flags);
3596 
3597 	if (useglobal)
3598 		mutex_unlock(&dasd_reserve_mutex);
3599 	else
3600 		dasd_sfree_request(cqr, cqr->memdev);
3601 	return rc;
3602 }
3603 
3604 /*
3605  * Steal lock ioctl - unconditional reserve device.
3606  * Buils a channel programm to break a device's reservation.
3607  * (unconditional reserve)
3608  */
3609 static int
dasd_eckd_steal_lock(struct dasd_device * device)3610 dasd_eckd_steal_lock(struct dasd_device *device)
3611 {
3612 	struct dasd_ccw_req *cqr;
3613 	int rc;
3614 	struct ccw1 *ccw;
3615 	int useglobal;
3616 
3617 	if (!capable(CAP_SYS_ADMIN))
3618 		return -EACCES;
3619 
3620 	useglobal = 0;
3621 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1, 32, device);
3622 	if (IS_ERR(cqr)) {
3623 		mutex_lock(&dasd_reserve_mutex);
3624 		useglobal = 1;
3625 		cqr = &dasd_reserve_req->cqr;
3626 		memset(cqr, 0, sizeof(*cqr));
3627 		memset(&dasd_reserve_req->ccw, 0,
3628 		       sizeof(dasd_reserve_req->ccw));
3629 		cqr->cpaddr = &dasd_reserve_req->ccw;
3630 		cqr->data = &dasd_reserve_req->data;
3631 		cqr->magic = DASD_ECKD_MAGIC;
3632 	}
3633 	ccw = cqr->cpaddr;
3634 	ccw->cmd_code = DASD_ECKD_CCW_SLCK;
3635 	ccw->flags |= CCW_FLAG_SLI;
3636 	ccw->count = 32;
3637 	ccw->cda = (__u32)(addr_t) cqr->data;
3638 	cqr->startdev = device;
3639 	cqr->memdev = device;
3640 	clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
3641 	set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
3642 	cqr->retries = 2;	/* set retry counter to enable basic ERP */
3643 	cqr->expires = 2 * HZ;
3644 	cqr->buildclk = get_tod_clock();
3645 	cqr->status = DASD_CQR_FILLED;
3646 
3647 	rc = dasd_sleep_on_immediatly(cqr);
3648 	if (!rc)
3649 		set_bit(DASD_FLAG_IS_RESERVED, &device->flags);
3650 
3651 	if (useglobal)
3652 		mutex_unlock(&dasd_reserve_mutex);
3653 	else
3654 		dasd_sfree_request(cqr, cqr->memdev);
3655 	return rc;
3656 }
3657 
3658 /*
3659  * SNID - Sense Path Group ID
3660  * This ioctl may be used in situations where I/O is stalled due to
3661  * a reserve, so if the normal dasd_smalloc_request fails, we use the
3662  * preallocated dasd_reserve_req.
3663  */
dasd_eckd_snid(struct dasd_device * device,void __user * argp)3664 static int dasd_eckd_snid(struct dasd_device *device,
3665 			  void __user *argp)
3666 {
3667 	struct dasd_ccw_req *cqr;
3668 	int rc;
3669 	struct ccw1 *ccw;
3670 	int useglobal;
3671 	struct dasd_snid_ioctl_data usrparm;
3672 
3673 	if (!capable(CAP_SYS_ADMIN))
3674 		return -EACCES;
3675 
3676 	if (copy_from_user(&usrparm, argp, sizeof(usrparm)))
3677 		return -EFAULT;
3678 
3679 	useglobal = 0;
3680 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1,
3681 				   sizeof(struct dasd_snid_data), device);
3682 	if (IS_ERR(cqr)) {
3683 		mutex_lock(&dasd_reserve_mutex);
3684 		useglobal = 1;
3685 		cqr = &dasd_reserve_req->cqr;
3686 		memset(cqr, 0, sizeof(*cqr));
3687 		memset(&dasd_reserve_req->ccw, 0,
3688 		       sizeof(dasd_reserve_req->ccw));
3689 		cqr->cpaddr = &dasd_reserve_req->ccw;
3690 		cqr->data = &dasd_reserve_req->data;
3691 		cqr->magic = DASD_ECKD_MAGIC;
3692 	}
3693 	ccw = cqr->cpaddr;
3694 	ccw->cmd_code = DASD_ECKD_CCW_SNID;
3695 	ccw->flags |= CCW_FLAG_SLI;
3696 	ccw->count = 12;
3697 	ccw->cda = (__u32)(addr_t) cqr->data;
3698 	cqr->startdev = device;
3699 	cqr->memdev = device;
3700 	clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
3701 	set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
3702 	set_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags);
3703 	cqr->retries = 5;
3704 	cqr->expires = 10 * HZ;
3705 	cqr->buildclk = get_tod_clock();
3706 	cqr->status = DASD_CQR_FILLED;
3707 	cqr->lpm = usrparm.path_mask;
3708 
3709 	rc = dasd_sleep_on_immediatly(cqr);
3710 	/* verify that I/O processing didn't modify the path mask */
3711 	if (!rc && usrparm.path_mask && (cqr->lpm != usrparm.path_mask))
3712 		rc = -EIO;
3713 	if (!rc) {
3714 		usrparm.data = *((struct dasd_snid_data *)cqr->data);
3715 		if (copy_to_user(argp, &usrparm, sizeof(usrparm)))
3716 			rc = -EFAULT;
3717 	}
3718 
3719 	if (useglobal)
3720 		mutex_unlock(&dasd_reserve_mutex);
3721 	else
3722 		dasd_sfree_request(cqr, cqr->memdev);
3723 	return rc;
3724 }
3725 
3726 /*
3727  * Read performance statistics
3728  */
3729 static int
dasd_eckd_performance(struct dasd_device * device,void __user * argp)3730 dasd_eckd_performance(struct dasd_device *device, void __user *argp)
3731 {
3732 	struct dasd_psf_prssd_data *prssdp;
3733 	struct dasd_rssd_perf_stats_t *stats;
3734 	struct dasd_ccw_req *cqr;
3735 	struct ccw1 *ccw;
3736 	int rc;
3737 
3738 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */  + 1 /* RSSD */,
3739 				   (sizeof(struct dasd_psf_prssd_data) +
3740 				    sizeof(struct dasd_rssd_perf_stats_t)),
3741 				   device);
3742 	if (IS_ERR(cqr)) {
3743 		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
3744 			    "Could not allocate initialization request");
3745 		return PTR_ERR(cqr);
3746 	}
3747 	cqr->startdev = device;
3748 	cqr->memdev = device;
3749 	cqr->retries = 0;
3750 	clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
3751 	cqr->expires = 10 * HZ;
3752 
3753 	/* Prepare for Read Subsystem Data */
3754 	prssdp = (struct dasd_psf_prssd_data *) cqr->data;
3755 	memset(prssdp, 0, sizeof(struct dasd_psf_prssd_data));
3756 	prssdp->order = PSF_ORDER_PRSSD;
3757 	prssdp->suborder = 0x01;	/* Performance Statistics */
3758 	prssdp->varies[1] = 0x01;	/* Perf Statistics for the Subsystem */
3759 
3760 	ccw = cqr->cpaddr;
3761 	ccw->cmd_code = DASD_ECKD_CCW_PSF;
3762 	ccw->count = sizeof(struct dasd_psf_prssd_data);
3763 	ccw->flags |= CCW_FLAG_CC;
3764 	ccw->cda = (__u32)(addr_t) prssdp;
3765 
3766 	/* Read Subsystem Data - Performance Statistics */
3767 	stats = (struct dasd_rssd_perf_stats_t *) (prssdp + 1);
3768 	memset(stats, 0, sizeof(struct dasd_rssd_perf_stats_t));
3769 
3770 	ccw++;
3771 	ccw->cmd_code = DASD_ECKD_CCW_RSSD;
3772 	ccw->count = sizeof(struct dasd_rssd_perf_stats_t);
3773 	ccw->cda = (__u32)(addr_t) stats;
3774 
3775 	cqr->buildclk = get_tod_clock();
3776 	cqr->status = DASD_CQR_FILLED;
3777 	rc = dasd_sleep_on(cqr);
3778 	if (rc == 0) {
3779 		prssdp = (struct dasd_psf_prssd_data *) cqr->data;
3780 		stats = (struct dasd_rssd_perf_stats_t *) (prssdp + 1);
3781 		if (copy_to_user(argp, stats,
3782 				 sizeof(struct dasd_rssd_perf_stats_t)))
3783 			rc = -EFAULT;
3784 	}
3785 	dasd_sfree_request(cqr, cqr->memdev);
3786 	return rc;
3787 }
3788 
3789 /*
3790  * Get attributes (cache operations)
3791  * Returnes the cache attributes used in Define Extend (DE).
3792  */
3793 static int
dasd_eckd_get_attrib(struct dasd_device * device,void __user * argp)3794 dasd_eckd_get_attrib(struct dasd_device *device, void __user *argp)
3795 {
3796 	struct dasd_eckd_private *private =
3797 		(struct dasd_eckd_private *)device->private;
3798 	struct attrib_data_t attrib = private->attrib;
3799 	int rc;
3800 
3801         if (!capable(CAP_SYS_ADMIN))
3802                 return -EACCES;
3803 	if (!argp)
3804                 return -EINVAL;
3805 
3806 	rc = 0;
3807 	if (copy_to_user(argp, (long *) &attrib,
3808 			 sizeof(struct attrib_data_t)))
3809 		rc = -EFAULT;
3810 
3811 	return rc;
3812 }
3813 
3814 /*
3815  * Set attributes (cache operations)
3816  * Stores the attributes for cache operation to be used in Define Extend (DE).
3817  */
3818 static int
dasd_eckd_set_attrib(struct dasd_device * device,void __user * argp)3819 dasd_eckd_set_attrib(struct dasd_device *device, void __user *argp)
3820 {
3821 	struct dasd_eckd_private *private =
3822 		(struct dasd_eckd_private *)device->private;
3823 	struct attrib_data_t attrib;
3824 
3825 	if (!capable(CAP_SYS_ADMIN))
3826 		return -EACCES;
3827 	if (!argp)
3828 		return -EINVAL;
3829 
3830 	if (copy_from_user(&attrib, argp, sizeof(struct attrib_data_t)))
3831 		return -EFAULT;
3832 	private->attrib = attrib;
3833 
3834 	dev_info(&device->cdev->dev,
3835 		 "The DASD cache mode was set to %x (%i cylinder prestage)\n",
3836 		 private->attrib.operation, private->attrib.nr_cyl);
3837 	return 0;
3838 }
3839 
3840 /*
3841  * Issue syscall I/O to EMC Symmetrix array.
3842  * CCWs are PSF and RSSD
3843  */
dasd_symm_io(struct dasd_device * device,void __user * argp)3844 static int dasd_symm_io(struct dasd_device *device, void __user *argp)
3845 {
3846 	struct dasd_symmio_parms usrparm;
3847 	char *psf_data, *rssd_result;
3848 	struct dasd_ccw_req *cqr;
3849 	struct ccw1 *ccw;
3850 	char psf0, psf1;
3851 	int rc;
3852 
3853 	if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
3854 		return -EACCES;
3855 	psf0 = psf1 = 0;
3856 
3857 	/* Copy parms from caller */
3858 	rc = -EFAULT;
3859 	if (copy_from_user(&usrparm, argp, sizeof(usrparm)))
3860 		goto out;
3861 	if (is_compat_task() || sizeof(long) == 4) {
3862 		/* Make sure pointers are sane even on 31 bit. */
3863 		rc = -EINVAL;
3864 		if ((usrparm.psf_data >> 32) != 0)
3865 			goto out;
3866 		if ((usrparm.rssd_result >> 32) != 0)
3867 			goto out;
3868 		usrparm.psf_data &= 0x7fffffffULL;
3869 		usrparm.rssd_result &= 0x7fffffffULL;
3870 	}
3871 	/* alloc I/O data area */
3872 	psf_data = kzalloc(usrparm.psf_data_len, GFP_KERNEL | GFP_DMA);
3873 	rssd_result = kzalloc(usrparm.rssd_result_len, GFP_KERNEL | GFP_DMA);
3874 	if (!psf_data || !rssd_result) {
3875 		rc = -ENOMEM;
3876 		goto out_free;
3877 	}
3878 
3879 	/* get syscall header from user space */
3880 	rc = -EFAULT;
3881 	if (copy_from_user(psf_data,
3882 			   (void __user *)(unsigned long) usrparm.psf_data,
3883 			   usrparm.psf_data_len))
3884 		goto out_free;
3885 	psf0 = psf_data[0];
3886 	psf1 = psf_data[1];
3887 
3888 	/* setup CCWs for PSF + RSSD */
3889 	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 2 , 0, device);
3890 	if (IS_ERR(cqr)) {
3891 		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
3892 			"Could not allocate initialization request");
3893 		rc = PTR_ERR(cqr);
3894 		goto out_free;
3895 	}
3896 
3897 	cqr->startdev = device;
3898 	cqr->memdev = device;
3899 	cqr->retries = 3;
3900 	cqr->expires = 10 * HZ;
3901 	cqr->buildclk = get_tod_clock();
3902 	cqr->status = DASD_CQR_FILLED;
3903 
3904 	/* Build the ccws */
3905 	ccw = cqr->cpaddr;
3906 
3907 	/* PSF ccw */
3908 	ccw->cmd_code = DASD_ECKD_CCW_PSF;
3909 	ccw->count = usrparm.psf_data_len;
3910 	ccw->flags |= CCW_FLAG_CC;
3911 	ccw->cda = (__u32)(addr_t) psf_data;
3912 
3913 	ccw++;
3914 
3915 	/* RSSD ccw  */
3916 	ccw->cmd_code = DASD_ECKD_CCW_RSSD;
3917 	ccw->count = usrparm.rssd_result_len;
3918 	ccw->flags = CCW_FLAG_SLI ;
3919 	ccw->cda = (__u32)(addr_t) rssd_result;
3920 
3921 	rc = dasd_sleep_on(cqr);
3922 	if (rc)
3923 		goto out_sfree;
3924 
3925 	rc = -EFAULT;
3926 	if (copy_to_user((void __user *)(unsigned long) usrparm.rssd_result,
3927 			   rssd_result, usrparm.rssd_result_len))
3928 		goto out_sfree;
3929 	rc = 0;
3930 
3931 out_sfree:
3932 	dasd_sfree_request(cqr, cqr->memdev);
3933 out_free:
3934 	kfree(rssd_result);
3935 	kfree(psf_data);
3936 out:
3937 	DBF_DEV_EVENT(DBF_WARNING, device,
3938 		      "Symmetrix ioctl (0x%02x 0x%02x): rc=%d",
3939 		      (int) psf0, (int) psf1, rc);
3940 	return rc;
3941 }
3942 
3943 static int
dasd_eckd_ioctl(struct dasd_block * block,unsigned int cmd,void __user * argp)3944 dasd_eckd_ioctl(struct dasd_block *block, unsigned int cmd, void __user *argp)
3945 {
3946 	struct dasd_device *device = block->base;
3947 
3948 	switch (cmd) {
3949 	case BIODASDGATTR:
3950 		return dasd_eckd_get_attrib(device, argp);
3951 	case BIODASDSATTR:
3952 		return dasd_eckd_set_attrib(device, argp);
3953 	case BIODASDPSRD:
3954 		return dasd_eckd_performance(device, argp);
3955 	case BIODASDRLSE:
3956 		return dasd_eckd_release(device);
3957 	case BIODASDRSRV:
3958 		return dasd_eckd_reserve(device);
3959 	case BIODASDSLCK:
3960 		return dasd_eckd_steal_lock(device);
3961 	case BIODASDSNID:
3962 		return dasd_eckd_snid(device, argp);
3963 	case BIODASDSYMMIO:
3964 		return dasd_symm_io(device, argp);
3965 	default:
3966 		return -ENOTTY;
3967 	}
3968 }
3969 
3970 /*
3971  * Dump the range of CCWs into 'page' buffer
3972  * and return number of printed chars.
3973  */
3974 static int
dasd_eckd_dump_ccw_range(struct ccw1 * from,struct ccw1 * to,char * page)3975 dasd_eckd_dump_ccw_range(struct ccw1 *from, struct ccw1 *to, char *page)
3976 {
3977 	int len, count;
3978 	char *datap;
3979 
3980 	len = 0;
3981 	while (from <= to) {
3982 		len += sprintf(page + len, PRINTK_HEADER
3983 			       " CCW %p: %08X %08X DAT:",
3984 			       from, ((int *) from)[0], ((int *) from)[1]);
3985 
3986 		/* get pointer to data (consider IDALs) */
3987 		if (from->flags & CCW_FLAG_IDA)
3988 			datap = (char *) *((addr_t *) (addr_t) from->cda);
3989 		else
3990 			datap = (char *) ((addr_t) from->cda);
3991 
3992 		/* dump data (max 32 bytes) */
3993 		for (count = 0; count < from->count && count < 32; count++) {
3994 			if (count % 8 == 0) len += sprintf(page + len, " ");
3995 			if (count % 4 == 0) len += sprintf(page + len, " ");
3996 			len += sprintf(page + len, "%02x", datap[count]);
3997 		}
3998 		len += sprintf(page + len, "\n");
3999 		from++;
4000 	}
4001 	return len;
4002 }
4003 
4004 static void
dasd_eckd_dump_sense_dbf(struct dasd_device * device,struct irb * irb,char * reason)4005 dasd_eckd_dump_sense_dbf(struct dasd_device *device, struct irb *irb,
4006 			 char *reason)
4007 {
4008 	u64 *sense;
4009 	u64 *stat;
4010 
4011 	sense = (u64 *) dasd_get_sense(irb);
4012 	stat = (u64 *) &irb->scsw;
4013 	if (sense) {
4014 		DBF_DEV_EVENT(DBF_EMERG, device, "%s: %016llx %08x : "
4015 			      "%016llx %016llx %016llx %016llx",
4016 			      reason, *stat, *((u32 *) (stat + 1)),
4017 			      sense[0], sense[1], sense[2], sense[3]);
4018 	} else {
4019 		DBF_DEV_EVENT(DBF_EMERG, device, "%s: %016llx %08x : %s",
4020 			      reason, *stat, *((u32 *) (stat + 1)),
4021 			      "NO VALID SENSE");
4022 	}
4023 }
4024 
4025 /*
4026  * Print sense data and related channel program.
4027  * Parts are printed because printk buffer is only 1024 bytes.
4028  */
dasd_eckd_dump_sense_ccw(struct dasd_device * device,struct dasd_ccw_req * req,struct irb * irb)4029 static void dasd_eckd_dump_sense_ccw(struct dasd_device *device,
4030 				 struct dasd_ccw_req *req, struct irb *irb)
4031 {
4032 	char *page;
4033 	struct ccw1 *first, *last, *fail, *from, *to;
4034 	int len, sl, sct;
4035 
4036 	page = (char *) get_zeroed_page(GFP_ATOMIC);
4037 	if (page == NULL) {
4038 		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
4039 			      "No memory to dump sense data\n");
4040 		return;
4041 	}
4042 	/* dump the sense data */
4043 	len = sprintf(page, PRINTK_HEADER
4044 		      " I/O status report for device %s:\n",
4045 		      dev_name(&device->cdev->dev));
4046 	len += sprintf(page + len, PRINTK_HEADER
4047 		       " in req: %p CC:%02X FC:%02X AC:%02X SC:%02X DS:%02X "
4048 		       "CS:%02X RC:%d\n",
4049 		       req, scsw_cc(&irb->scsw), scsw_fctl(&irb->scsw),
4050 		       scsw_actl(&irb->scsw), scsw_stctl(&irb->scsw),
4051 		       scsw_dstat(&irb->scsw), scsw_cstat(&irb->scsw),
4052 		       req ? req->intrc : 0);
4053 	len += sprintf(page + len, PRINTK_HEADER
4054 		       " device %s: Failing CCW: %p\n",
4055 		       dev_name(&device->cdev->dev),
4056 		       (void *) (addr_t) irb->scsw.cmd.cpa);
4057 	if (irb->esw.esw0.erw.cons) {
4058 		for (sl = 0; sl < 4; sl++) {
4059 			len += sprintf(page + len, PRINTK_HEADER
4060 				       " Sense(hex) %2d-%2d:",
4061 				       (8 * sl), ((8 * sl) + 7));
4062 
4063 			for (sct = 0; sct < 8; sct++) {
4064 				len += sprintf(page + len, " %02x",
4065 					       irb->ecw[8 * sl + sct]);
4066 			}
4067 			len += sprintf(page + len, "\n");
4068 		}
4069 
4070 		if (irb->ecw[27] & DASD_SENSE_BIT_0) {
4071 			/* 24 Byte Sense Data */
4072 			sprintf(page + len, PRINTK_HEADER
4073 				" 24 Byte: %x MSG %x, "
4074 				"%s MSGb to SYSOP\n",
4075 				irb->ecw[7] >> 4, irb->ecw[7] & 0x0f,
4076 				irb->ecw[1] & 0x10 ? "" : "no");
4077 		} else {
4078 			/* 32 Byte Sense Data */
4079 			sprintf(page + len, PRINTK_HEADER
4080 				" 32 Byte: Format: %x "
4081 				"Exception class %x\n",
4082 				irb->ecw[6] & 0x0f, irb->ecw[22] >> 4);
4083 		}
4084 	} else {
4085 		sprintf(page + len, PRINTK_HEADER
4086 			" SORRY - NO VALID SENSE AVAILABLE\n");
4087 	}
4088 	printk(KERN_ERR "%s", page);
4089 
4090 	if (req) {
4091 		/* req == NULL for unsolicited interrupts */
4092 		/* dump the Channel Program (max 140 Bytes per line) */
4093 		/* Count CCW and print first CCWs (maximum 1024 % 140 = 7) */
4094 		first = req->cpaddr;
4095 		for (last = first; last->flags & (CCW_FLAG_CC | CCW_FLAG_DC); last++);
4096 		to = min(first + 6, last);
4097 		len = sprintf(page, PRINTK_HEADER
4098 			      " Related CP in req: %p\n", req);
4099 		dasd_eckd_dump_ccw_range(first, to, page + len);
4100 		printk(KERN_ERR "%s", page);
4101 
4102 		/* print failing CCW area (maximum 4) */
4103 		/* scsw->cda is either valid or zero  */
4104 		len = 0;
4105 		from = ++to;
4106 		fail = (struct ccw1 *)(addr_t)
4107 				irb->scsw.cmd.cpa; /* failing CCW */
4108 		if (from <  fail - 2) {
4109 			from = fail - 2;     /* there is a gap - print header */
4110 			len += sprintf(page, PRINTK_HEADER "......\n");
4111 		}
4112 		to = min(fail + 1, last);
4113 		len += dasd_eckd_dump_ccw_range(from, to, page + len);
4114 
4115 		/* print last CCWs (maximum 2) */
4116 		from = max(from, ++to);
4117 		if (from < last - 1) {
4118 			from = last - 1;     /* there is a gap - print header */
4119 			len += sprintf(page + len, PRINTK_HEADER "......\n");
4120 		}
4121 		len += dasd_eckd_dump_ccw_range(from, last, page + len);
4122 		if (len > 0)
4123 			printk(KERN_ERR "%s", page);
4124 	}
4125 	free_page((unsigned long) page);
4126 }
4127 
4128 
4129 /*
4130  * Print sense data from a tcw.
4131  */
dasd_eckd_dump_sense_tcw(struct dasd_device * device,struct dasd_ccw_req * req,struct irb * irb)4132 static void dasd_eckd_dump_sense_tcw(struct dasd_device *device,
4133 				 struct dasd_ccw_req *req, struct irb *irb)
4134 {
4135 	char *page;
4136 	int len, sl, sct, residual;
4137 	struct tsb *tsb;
4138 	u8 *sense, *rcq;
4139 
4140 	page = (char *) get_zeroed_page(GFP_ATOMIC);
4141 	if (page == NULL) {
4142 		DBF_DEV_EVENT(DBF_WARNING, device, " %s",
4143 			    "No memory to dump sense data");
4144 		return;
4145 	}
4146 	/* dump the sense data */
4147 	len = sprintf(page, PRINTK_HEADER
4148 		      " I/O status report for device %s:\n",
4149 		      dev_name(&device->cdev->dev));
4150 	len += sprintf(page + len, PRINTK_HEADER
4151 		       " in req: %p CC:%02X FC:%02X AC:%02X SC:%02X DS:%02X "
4152 		       "CS:%02X fcxs:%02X schxs:%02X RC:%d\n",
4153 		       req, scsw_cc(&irb->scsw), scsw_fctl(&irb->scsw),
4154 		       scsw_actl(&irb->scsw), scsw_stctl(&irb->scsw),
4155 		       scsw_dstat(&irb->scsw), scsw_cstat(&irb->scsw),
4156 		       irb->scsw.tm.fcxs, irb->scsw.tm.schxs,
4157 		       req ? req->intrc : 0);
4158 	len += sprintf(page + len, PRINTK_HEADER
4159 		       " device %s: Failing TCW: %p\n",
4160 		       dev_name(&device->cdev->dev),
4161 		       (void *) (addr_t) irb->scsw.tm.tcw);
4162 
4163 	tsb = NULL;
4164 	sense = NULL;
4165 	if (irb->scsw.tm.tcw && (irb->scsw.tm.fcxs & 0x01))
4166 		tsb = tcw_get_tsb(
4167 			(struct tcw *)(unsigned long)irb->scsw.tm.tcw);
4168 
4169 	if (tsb) {
4170 		len += sprintf(page + len, PRINTK_HEADER
4171 			       " tsb->length %d\n", tsb->length);
4172 		len += sprintf(page + len, PRINTK_HEADER
4173 			       " tsb->flags %x\n", tsb->flags);
4174 		len += sprintf(page + len, PRINTK_HEADER
4175 			       " tsb->dcw_offset %d\n", tsb->dcw_offset);
4176 		len += sprintf(page + len, PRINTK_HEADER
4177 			       " tsb->count %d\n", tsb->count);
4178 		residual = tsb->count - 28;
4179 		len += sprintf(page + len, PRINTK_HEADER
4180 			       " residual %d\n", residual);
4181 
4182 		switch (tsb->flags & 0x07) {
4183 		case 1:	/* tsa_iostat */
4184 			len += sprintf(page + len, PRINTK_HEADER
4185 			       " tsb->tsa.iostat.dev_time %d\n",
4186 				       tsb->tsa.iostat.dev_time);
4187 			len += sprintf(page + len, PRINTK_HEADER
4188 			       " tsb->tsa.iostat.def_time %d\n",
4189 				       tsb->tsa.iostat.def_time);
4190 			len += sprintf(page + len, PRINTK_HEADER
4191 			       " tsb->tsa.iostat.queue_time %d\n",
4192 				       tsb->tsa.iostat.queue_time);
4193 			len += sprintf(page + len, PRINTK_HEADER
4194 			       " tsb->tsa.iostat.dev_busy_time %d\n",
4195 				       tsb->tsa.iostat.dev_busy_time);
4196 			len += sprintf(page + len, PRINTK_HEADER
4197 			       " tsb->tsa.iostat.dev_act_time %d\n",
4198 				       tsb->tsa.iostat.dev_act_time);
4199 			sense = tsb->tsa.iostat.sense;
4200 			break;
4201 		case 2: /* ts_ddpc */
4202 			len += sprintf(page + len, PRINTK_HEADER
4203 			       " tsb->tsa.ddpc.rc %d\n", tsb->tsa.ddpc.rc);
4204 			for (sl = 0; sl < 2; sl++) {
4205 				len += sprintf(page + len, PRINTK_HEADER
4206 					       " tsb->tsa.ddpc.rcq %2d-%2d: ",
4207 					       (8 * sl), ((8 * sl) + 7));
4208 				rcq = tsb->tsa.ddpc.rcq;
4209 				for (sct = 0; sct < 8; sct++) {
4210 					len += sprintf(page + len, " %02x",
4211 						       rcq[8 * sl + sct]);
4212 				}
4213 				len += sprintf(page + len, "\n");
4214 			}
4215 			sense = tsb->tsa.ddpc.sense;
4216 			break;
4217 		case 3: /* tsa_intrg */
4218 			len += sprintf(page + len, PRINTK_HEADER
4219 				      " tsb->tsa.intrg.: not supportet yet\n");
4220 			break;
4221 		}
4222 
4223 		if (sense) {
4224 			for (sl = 0; sl < 4; sl++) {
4225 				len += sprintf(page + len, PRINTK_HEADER
4226 					       " Sense(hex) %2d-%2d:",
4227 					       (8 * sl), ((8 * sl) + 7));
4228 				for (sct = 0; sct < 8; sct++) {
4229 					len += sprintf(page + len, " %02x",
4230 						       sense[8 * sl + sct]);
4231 				}
4232 				len += sprintf(page + len, "\n");
4233 			}
4234 
4235 			if (sense[27] & DASD_SENSE_BIT_0) {
4236 				/* 24 Byte Sense Data */
4237 				sprintf(page + len, PRINTK_HEADER
4238 					" 24 Byte: %x MSG %x, "
4239 					"%s MSGb to SYSOP\n",
4240 					sense[7] >> 4, sense[7] & 0x0f,
4241 					sense[1] & 0x10 ? "" : "no");
4242 			} else {
4243 				/* 32 Byte Sense Data */
4244 				sprintf(page + len, PRINTK_HEADER
4245 					" 32 Byte: Format: %x "
4246 					"Exception class %x\n",
4247 					sense[6] & 0x0f, sense[22] >> 4);
4248 			}
4249 		} else {
4250 			sprintf(page + len, PRINTK_HEADER
4251 				" SORRY - NO VALID SENSE AVAILABLE\n");
4252 		}
4253 	} else {
4254 		sprintf(page + len, PRINTK_HEADER
4255 			" SORRY - NO TSB DATA AVAILABLE\n");
4256 	}
4257 	printk(KERN_ERR "%s", page);
4258 	free_page((unsigned long) page);
4259 }
4260 
dasd_eckd_dump_sense(struct dasd_device * device,struct dasd_ccw_req * req,struct irb * irb)4261 static void dasd_eckd_dump_sense(struct dasd_device *device,
4262 				 struct dasd_ccw_req *req, struct irb *irb)
4263 {
4264 	if (scsw_is_tm(&irb->scsw))
4265 		dasd_eckd_dump_sense_tcw(device, req, irb);
4266 	else
4267 		dasd_eckd_dump_sense_ccw(device, req, irb);
4268 }
4269 
dasd_eckd_pm_freeze(struct dasd_device * device)4270 static int dasd_eckd_pm_freeze(struct dasd_device *device)
4271 {
4272 	/*
4273 	 * the device should be disconnected from our LCU structure
4274 	 * on restore we will reconnect it and reread LCU specific
4275 	 * information like PAV support that might have changed
4276 	 */
4277 	dasd_alias_remove_device(device);
4278 	dasd_alias_disconnect_device_from_lcu(device);
4279 
4280 	return 0;
4281 }
4282 
dasd_eckd_restore_device(struct dasd_device * device)4283 static int dasd_eckd_restore_device(struct dasd_device *device)
4284 {
4285 	struct dasd_eckd_private *private;
4286 	struct dasd_eckd_characteristics temp_rdc_data;
4287 	int rc;
4288 	struct dasd_uid temp_uid;
4289 	unsigned long flags;
4290 	unsigned long cqr_flags = 0;
4291 
4292 	private = (struct dasd_eckd_private *) device->private;
4293 
4294 	/* Read Configuration Data */
4295 	dasd_eckd_read_conf(device);
4296 
4297 	dasd_eckd_get_uid(device, &temp_uid);
4298 	/* Generate device unique id */
4299 	rc = dasd_eckd_generate_uid(device);
4300 	spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
4301 	if (memcmp(&private->uid, &temp_uid, sizeof(struct dasd_uid)) != 0)
4302 		dev_err(&device->cdev->dev, "The UID of the DASD has "
4303 			"changed\n");
4304 	spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
4305 	if (rc)
4306 		goto out_err;
4307 
4308 	/* register lcu with alias handling, enable PAV if this is a new lcu */
4309 	rc = dasd_alias_make_device_known_to_lcu(device);
4310 	if (rc)
4311 		return rc;
4312 
4313 	set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr_flags);
4314 	dasd_eckd_validate_server(device, cqr_flags);
4315 
4316 	/* RE-Read Configuration Data */
4317 	dasd_eckd_read_conf(device);
4318 
4319 	/* Read Feature Codes */
4320 	dasd_eckd_read_features(device);
4321 
4322 	/* Read Device Characteristics */
4323 	rc = dasd_generic_read_dev_chars(device, DASD_ECKD_MAGIC,
4324 					 &temp_rdc_data, 64);
4325 	if (rc) {
4326 		DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
4327 				"Read device characteristic failed, rc=%d", rc);
4328 		goto out_err;
4329 	}
4330 	spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
4331 	memcpy(&private->rdc_data, &temp_rdc_data, sizeof(temp_rdc_data));
4332 	spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
4333 
4334 	/* add device to alias management */
4335 	dasd_alias_add_device(device);
4336 
4337 	return 0;
4338 
4339 out_err:
4340 	return -1;
4341 }
4342 
dasd_eckd_reload_device(struct dasd_device * device)4343 static int dasd_eckd_reload_device(struct dasd_device *device)
4344 {
4345 	struct dasd_eckd_private *private;
4346 	int rc, old_base;
4347 	char print_uid[60];
4348 	struct dasd_uid uid;
4349 	unsigned long flags;
4350 
4351 	private = (struct dasd_eckd_private *) device->private;
4352 
4353 	spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
4354 	old_base = private->uid.base_unit_addr;
4355 	spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
4356 
4357 	/* Read Configuration Data */
4358 	rc = dasd_eckd_read_conf(device);
4359 	if (rc)
4360 		goto out_err;
4361 
4362 	rc = dasd_eckd_generate_uid(device);
4363 	if (rc)
4364 		goto out_err;
4365 	/*
4366 	 * update unit address configuration and
4367 	 * add device to alias management
4368 	 */
4369 	dasd_alias_update_add_device(device);
4370 
4371 	dasd_eckd_get_uid(device, &uid);
4372 
4373 	if (old_base != uid.base_unit_addr) {
4374 		if (strlen(uid.vduit) > 0)
4375 			snprintf(print_uid, sizeof(print_uid),
4376 				 "%s.%s.%04x.%02x.%s", uid.vendor, uid.serial,
4377 				 uid.ssid, uid.base_unit_addr, uid.vduit);
4378 		else
4379 			snprintf(print_uid, sizeof(print_uid),
4380 				 "%s.%s.%04x.%02x", uid.vendor, uid.serial,
4381 				 uid.ssid, uid.base_unit_addr);
4382 
4383 		dev_info(&device->cdev->dev,
4384 			 "An Alias device was reassigned to a new base device "
4385 			 "with UID: %s\n", print_uid);
4386 	}
4387 	return 0;
4388 
4389 out_err:
4390 	return -1;
4391 }
4392 
4393 static struct ccw_driver dasd_eckd_driver = {
4394 	.driver = {
4395 		.name	= "dasd-eckd",
4396 		.owner	= THIS_MODULE,
4397 	},
4398 	.ids	     = dasd_eckd_ids,
4399 	.probe	     = dasd_eckd_probe,
4400 	.remove      = dasd_generic_remove,
4401 	.set_offline = dasd_generic_set_offline,
4402 	.set_online  = dasd_eckd_set_online,
4403 	.notify      = dasd_generic_notify,
4404 	.path_event  = dasd_generic_path_event,
4405 	.shutdown    = dasd_generic_shutdown,
4406 	.freeze      = dasd_generic_pm_freeze,
4407 	.thaw	     = dasd_generic_restore_device,
4408 	.restore     = dasd_generic_restore_device,
4409 	.uc_handler  = dasd_generic_uc_handler,
4410 	.int_class   = IRQIO_DAS,
4411 };
4412 
4413 /*
4414  * max_blocks is dependent on the amount of storage that is available
4415  * in the static io buffer for each device. Currently each device has
4416  * 8192 bytes (=2 pages). For 64 bit one dasd_mchunkt_t structure has
4417  * 24 bytes, the struct dasd_ccw_req has 136 bytes and each block can use
4418  * up to 16 bytes (8 for the ccw and 8 for the idal pointer). In
4419  * addition we have one define extent ccw + 16 bytes of data and one
4420  * locate record ccw + 16 bytes of data. That makes:
4421  * (8192 - 24 - 136 - 8 - 16 - 8 - 16) / 16 = 499 blocks at maximum.
4422  * We want to fit two into the available memory so that we can immediately
4423  * start the next request if one finishes off. That makes 249.5 blocks
4424  * for one request. Give a little safety and the result is 240.
4425  */
4426 static struct dasd_discipline dasd_eckd_discipline = {
4427 	.owner = THIS_MODULE,
4428 	.name = "ECKD",
4429 	.ebcname = "ECKD",
4430 	.max_blocks = 190,
4431 	.check_device = dasd_eckd_check_characteristics,
4432 	.uncheck_device = dasd_eckd_uncheck_device,
4433 	.do_analysis = dasd_eckd_do_analysis,
4434 	.verify_path = dasd_eckd_verify_path,
4435 	.basic_to_ready = dasd_eckd_basic_to_ready,
4436 	.online_to_ready = dasd_eckd_online_to_ready,
4437 	.ready_to_basic = dasd_eckd_ready_to_basic,
4438 	.fill_geometry = dasd_eckd_fill_geometry,
4439 	.start_IO = dasd_start_IO,
4440 	.term_IO = dasd_term_IO,
4441 	.handle_terminated_request = dasd_eckd_handle_terminated_request,
4442 	.format_device = dasd_eckd_format_device,
4443 	.erp_action = dasd_eckd_erp_action,
4444 	.erp_postaction = dasd_eckd_erp_postaction,
4445 	.check_for_device_change = dasd_eckd_check_for_device_change,
4446 	.build_cp = dasd_eckd_build_alias_cp,
4447 	.free_cp = dasd_eckd_free_alias_cp,
4448 	.dump_sense = dasd_eckd_dump_sense,
4449 	.dump_sense_dbf = dasd_eckd_dump_sense_dbf,
4450 	.fill_info = dasd_eckd_fill_info,
4451 	.ioctl = dasd_eckd_ioctl,
4452 	.freeze = dasd_eckd_pm_freeze,
4453 	.restore = dasd_eckd_restore_device,
4454 	.reload = dasd_eckd_reload_device,
4455 	.get_uid = dasd_eckd_get_uid,
4456 	.kick_validate = dasd_eckd_kick_validate_server,
4457 };
4458 
4459 static int __init
dasd_eckd_init(void)4460 dasd_eckd_init(void)
4461 {
4462 	int ret;
4463 
4464 	ASCEBC(dasd_eckd_discipline.ebcname, 4);
4465 	dasd_reserve_req = kmalloc(sizeof(*dasd_reserve_req),
4466 				   GFP_KERNEL | GFP_DMA);
4467 	if (!dasd_reserve_req)
4468 		return -ENOMEM;
4469 	path_verification_worker = kmalloc(sizeof(*path_verification_worker),
4470 				   GFP_KERNEL | GFP_DMA);
4471 	if (!path_verification_worker) {
4472 		kfree(dasd_reserve_req);
4473 		return -ENOMEM;
4474 	}
4475 	ret = ccw_driver_register(&dasd_eckd_driver);
4476 	if (!ret)
4477 		wait_for_device_probe();
4478 	else {
4479 		kfree(path_verification_worker);
4480 		kfree(dasd_reserve_req);
4481 	}
4482 	return ret;
4483 }
4484 
4485 static void __exit
dasd_eckd_cleanup(void)4486 dasd_eckd_cleanup(void)
4487 {
4488 	ccw_driver_unregister(&dasd_eckd_driver);
4489 	kfree(path_verification_worker);
4490 	kfree(dasd_reserve_req);
4491 }
4492 
4493 module_init(dasd_eckd_init);
4494 module_exit(dasd_eckd_cleanup);
4495