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