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