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