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