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