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