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