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