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