]> git.openfabrics.org - ~shefty/rdma-dev.git/blob - drivers/s390/block/dasd_eckd.c
[S390] dasd: fix unsolicited interrupt recognition
[~shefty/rdma-dev.git] / drivers / s390 / block / dasd_eckd.c
1 /*
2  * File...........: linux/drivers/s390/block/dasd_eckd.c
3  * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
4  *                  Horst Hummel <Horst.Hummel@de.ibm.com>
5  *                  Carsten Otte <Cotte@de.ibm.com>
6  *                  Martin Schwidefsky <schwidefsky@de.ibm.com>
7  * Bugreports.to..: <Linux390@de.ibm.com>
8  * Copyright IBM Corp. 1999, 2009
9  * EMC Symmetrix ioctl Copyright EMC Corporation, 2008
10  * Author.........: Nigel Hislop <hislop_nigel@emc.com>
11  */
12
13 #define KMSG_COMPONENT "dasd-eckd"
14
15 #include <linux/stddef.h>
16 #include <linux/kernel.h>
17 #include <linux/slab.h>
18 #include <linux/hdreg.h>        /* HDIO_GETGEO                      */
19 #include <linux/bio.h>
20 #include <linux/module.h>
21 #include <linux/init.h>
22
23 #include <asm/debug.h>
24 #include <asm/idals.h>
25 #include <asm/ebcdic.h>
26 #include <asm/compat.h>
27 #include <asm/io.h>
28 #include <asm/uaccess.h>
29 #include <asm/cio.h>
30 #include <asm/ccwdev.h>
31 #include <asm/itcw.h>
32
33 #include "dasd_int.h"
34 #include "dasd_eckd.h"
35 #include "../cio/chsc.h"
36
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
57 MODULE_LICENSE("GPL");
58
59 static struct dasd_discipline dasd_eckd_discipline;
60
61 /* The ccw bus type uses this table to find devices that it sends to
62  * dasd_eckd_probe */
63 static struct ccw_device_id dasd_eckd_ids[] = {
64         { CCW_DEVICE_DEVTYPE (0x3990, 0, 0x3390, 0), .driver_info = 0x1},
65         { CCW_DEVICE_DEVTYPE (0x2105, 0, 0x3390, 0), .driver_info = 0x2},
66         { CCW_DEVICE_DEVTYPE (0x3880, 0, 0x3390, 0), .driver_info = 0x3},
67         { CCW_DEVICE_DEVTYPE (0x3990, 0, 0x3380, 0), .driver_info = 0x4},
68         { CCW_DEVICE_DEVTYPE (0x2105, 0, 0x3380, 0), .driver_info = 0x5},
69         { CCW_DEVICE_DEVTYPE (0x9343, 0, 0x9345, 0), .driver_info = 0x6},
70         { CCW_DEVICE_DEVTYPE (0x2107, 0, 0x3390, 0), .driver_info = 0x7},
71         { CCW_DEVICE_DEVTYPE (0x2107, 0, 0x3380, 0), .driver_info = 0x8},
72         { CCW_DEVICE_DEVTYPE (0x1750, 0, 0x3390, 0), .driver_info = 0x9},
73         { CCW_DEVICE_DEVTYPE (0x1750, 0, 0x3380, 0), .driver_info = 0xa},
74         { /* end of list */ },
75 };
76
77 MODULE_DEVICE_TABLE(ccw, dasd_eckd_ids);
78
79 static struct ccw_driver dasd_eckd_driver; /* see below */
80
81 #define INIT_CQR_OK 0
82 #define INIT_CQR_UNFORMATTED 1
83 #define INIT_CQR_ERROR 2
84
85 /* emergency request for reserve/release */
86 static struct {
87         struct dasd_ccw_req cqr;
88         struct ccw1 ccw;
89         char data[32];
90 } *dasd_reserve_req;
91 static DEFINE_MUTEX(dasd_reserve_mutex);
92
93
94 /* initial attempt at a probe function. this can be simplified once
95  * the other detection code is gone */
96 static int
97 dasd_eckd_probe (struct ccw_device *cdev)
98 {
99         int ret;
100
101         /* set ECKD specific ccw-device options */
102         ret = ccw_device_set_options(cdev, CCWDEV_ALLOW_FORCE |
103                                      CCWDEV_DO_PATHGROUP | CCWDEV_DO_MULTIPATH);
104         if (ret) {
105                 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
106                                 "dasd_eckd_probe: could not set "
107                                 "ccw-device options");
108                 return ret;
109         }
110         ret = dasd_generic_probe(cdev, &dasd_eckd_discipline);
111         return ret;
112 }
113
114 static int
115 dasd_eckd_set_online(struct ccw_device *cdev)
116 {
117         return dasd_generic_set_online(cdev, &dasd_eckd_discipline);
118 }
119
120 static const int sizes_trk0[] = { 28, 148, 84 };
121 #define LABEL_SIZE 140
122
123 static inline unsigned int
124 round_up_multiple(unsigned int no, unsigned int mult)
125 {
126         int rem = no % mult;
127         return (rem ? no - rem + mult : no);
128 }
129
130 static inline unsigned int
131 ceil_quot(unsigned int d1, unsigned int d2)
132 {
133         return (d1 + (d2 - 1)) / d2;
134 }
135
136 static unsigned int
137 recs_per_track(struct dasd_eckd_characteristics * rdc,
138                unsigned int kl, unsigned int dl)
139 {
140         int dn, kn;
141
142         switch (rdc->dev_type) {
143         case 0x3380:
144                 if (kl)
145                         return 1499 / (15 + 7 + ceil_quot(kl + 12, 32) +
146                                        ceil_quot(dl + 12, 32));
147                 else
148                         return 1499 / (15 + ceil_quot(dl + 12, 32));
149         case 0x3390:
150                 dn = ceil_quot(dl + 6, 232) + 1;
151                 if (kl) {
152                         kn = ceil_quot(kl + 6, 232) + 1;
153                         return 1729 / (10 + 9 + ceil_quot(kl + 6 * kn, 34) +
154                                        9 + ceil_quot(dl + 6 * dn, 34));
155                 } else
156                         return 1729 / (10 + 9 + ceil_quot(dl + 6 * dn, 34));
157         case 0x9345:
158                 dn = ceil_quot(dl + 6, 232) + 1;
159                 if (kl) {
160                         kn = ceil_quot(kl + 6, 232) + 1;
161                         return 1420 / (18 + 7 + ceil_quot(kl + 6 * kn, 34) +
162                                        ceil_quot(dl + 6 * dn, 34));
163                 } else
164                         return 1420 / (18 + 7 + ceil_quot(dl + 6 * dn, 34));
165         }
166         return 0;
167 }
168
169 static void set_ch_t(struct ch_t *geo, __u32 cyl, __u8 head)
170 {
171         geo->cyl = (__u16) cyl;
172         geo->head = cyl >> 16;
173         geo->head <<= 4;
174         geo->head |= head;
175 }
176
177 static int
178 check_XRC (struct ccw1         *de_ccw,
179            struct DE_eckd_data *data,
180            struct dasd_device  *device)
181 {
182         struct dasd_eckd_private *private;
183         int rc;
184
185         private = (struct dasd_eckd_private *) device->private;
186         if (!private->rdc_data.facilities.XRC_supported)
187                 return 0;
188
189         /* switch on System Time Stamp - needed for XRC Support */
190         data->ga_extended |= 0x08; /* switch on 'Time Stamp Valid'   */
191         data->ga_extended |= 0x02; /* switch on 'Extended Parameter' */
192
193         rc = get_sync_clock(&data->ep_sys_time);
194         /* Ignore return code if sync clock is switched off. */
195         if (rc == -ENOSYS || rc == -EACCES)
196                 rc = 0;
197
198         de_ccw->count = sizeof(struct DE_eckd_data);
199         de_ccw->flags |= CCW_FLAG_SLI;
200         return rc;
201 }
202
203 static int
204 define_extent(struct ccw1 *ccw, struct DE_eckd_data *data, unsigned int trk,
205               unsigned int totrk, int cmd, struct dasd_device *device)
206 {
207         struct dasd_eckd_private *private;
208         u32 begcyl, endcyl;
209         u16 heads, beghead, endhead;
210         int rc = 0;
211
212         private = (struct dasd_eckd_private *) device->private;
213
214         ccw->cmd_code = DASD_ECKD_CCW_DEFINE_EXTENT;
215         ccw->flags = 0;
216         ccw->count = 16;
217         ccw->cda = (__u32) __pa(data);
218
219         memset(data, 0, sizeof(struct DE_eckd_data));
220         switch (cmd) {
221         case DASD_ECKD_CCW_READ_HOME_ADDRESS:
222         case DASD_ECKD_CCW_READ_RECORD_ZERO:
223         case DASD_ECKD_CCW_READ:
224         case DASD_ECKD_CCW_READ_MT:
225         case DASD_ECKD_CCW_READ_CKD:
226         case DASD_ECKD_CCW_READ_CKD_MT:
227         case DASD_ECKD_CCW_READ_KD:
228         case DASD_ECKD_CCW_READ_KD_MT:
229         case DASD_ECKD_CCW_READ_COUNT:
230                 data->mask.perm = 0x1;
231                 data->attributes.operation = private->attrib.operation;
232                 break;
233         case DASD_ECKD_CCW_WRITE:
234         case DASD_ECKD_CCW_WRITE_MT:
235         case DASD_ECKD_CCW_WRITE_KD:
236         case DASD_ECKD_CCW_WRITE_KD_MT:
237                 data->mask.perm = 0x02;
238                 data->attributes.operation = private->attrib.operation;
239                 rc = check_XRC (ccw, data, device);
240                 break;
241         case DASD_ECKD_CCW_WRITE_CKD:
242         case DASD_ECKD_CCW_WRITE_CKD_MT:
243                 data->attributes.operation = DASD_BYPASS_CACHE;
244                 rc = check_XRC (ccw, data, device);
245                 break;
246         case DASD_ECKD_CCW_ERASE:
247         case DASD_ECKD_CCW_WRITE_HOME_ADDRESS:
248         case DASD_ECKD_CCW_WRITE_RECORD_ZERO:
249                 data->mask.perm = 0x3;
250                 data->mask.auth = 0x1;
251                 data->attributes.operation = DASD_BYPASS_CACHE;
252                 rc = check_XRC (ccw, data, device);
253                 break;
254         default:
255                 dev_err(&device->cdev->dev,
256                         "0x%x is not a known command\n", cmd);
257                 break;
258         }
259
260         data->attributes.mode = 0x3;    /* ECKD */
261
262         if ((private->rdc_data.cu_type == 0x2105 ||
263              private->rdc_data.cu_type == 0x2107 ||
264              private->rdc_data.cu_type == 0x1750)
265             && !(private->uses_cdl && trk < 2))
266                 data->ga_extended |= 0x40; /* Regular Data Format Mode */
267
268         heads = private->rdc_data.trk_per_cyl;
269         begcyl = trk / heads;
270         beghead = trk % heads;
271         endcyl = totrk / heads;
272         endhead = totrk % heads;
273
274         /* check for sequential prestage - enhance cylinder range */
275         if (data->attributes.operation == DASD_SEQ_PRESTAGE ||
276             data->attributes.operation == DASD_SEQ_ACCESS) {
277
278                 if (endcyl + private->attrib.nr_cyl < private->real_cyl)
279                         endcyl += private->attrib.nr_cyl;
280                 else
281                         endcyl = (private->real_cyl - 1);
282         }
283
284         set_ch_t(&data->beg_ext, begcyl, beghead);
285         set_ch_t(&data->end_ext, endcyl, endhead);
286         return rc;
287 }
288
289 static int check_XRC_on_prefix(struct PFX_eckd_data *pfxdata,
290                                struct dasd_device  *device)
291 {
292         struct dasd_eckd_private *private;
293         int rc;
294
295         private = (struct dasd_eckd_private *) device->private;
296         if (!private->rdc_data.facilities.XRC_supported)
297                 return 0;
298
299         /* switch on System Time Stamp - needed for XRC Support */
300         pfxdata->define_extent.ga_extended |= 0x08; /* 'Time Stamp Valid'   */
301         pfxdata->define_extent.ga_extended |= 0x02; /* 'Extended Parameter' */
302         pfxdata->validity.time_stamp = 1;           /* 'Time Stamp Valid'   */
303
304         rc = get_sync_clock(&pfxdata->define_extent.ep_sys_time);
305         /* Ignore return code if sync clock is switched off. */
306         if (rc == -ENOSYS || rc == -EACCES)
307                 rc = 0;
308         return rc;
309 }
310
311 static void fill_LRE_data(struct LRE_eckd_data *data, unsigned int trk,
312                           unsigned int rec_on_trk, int count, int cmd,
313                           struct dasd_device *device, unsigned int reclen,
314                           unsigned int tlf)
315 {
316         struct dasd_eckd_private *private;
317         int sector;
318         int dn, d;
319
320         private = (struct dasd_eckd_private *) device->private;
321
322         memset(data, 0, sizeof(*data));
323         sector = 0;
324         if (rec_on_trk) {
325                 switch (private->rdc_data.dev_type) {
326                 case 0x3390:
327                         dn = ceil_quot(reclen + 6, 232);
328                         d = 9 + ceil_quot(reclen + 6 * (dn + 1), 34);
329                         sector = (49 + (rec_on_trk - 1) * (10 + d)) / 8;
330                         break;
331                 case 0x3380:
332                         d = 7 + ceil_quot(reclen + 12, 32);
333                         sector = (39 + (rec_on_trk - 1) * (8 + d)) / 7;
334                         break;
335                 }
336         }
337         data->sector = sector;
338         /* note: meaning of count depends on the operation
339          *       for record based I/O it's the number of records, but for
340          *       track based I/O it's the number of tracks
341          */
342         data->count = count;
343         switch (cmd) {
344         case DASD_ECKD_CCW_WRITE_HOME_ADDRESS:
345                 data->operation.orientation = 0x3;
346                 data->operation.operation = 0x03;
347                 break;
348         case DASD_ECKD_CCW_READ_HOME_ADDRESS:
349                 data->operation.orientation = 0x3;
350                 data->operation.operation = 0x16;
351                 break;
352         case DASD_ECKD_CCW_WRITE_RECORD_ZERO:
353                 data->operation.orientation = 0x1;
354                 data->operation.operation = 0x03;
355                 data->count++;
356                 break;
357         case DASD_ECKD_CCW_READ_RECORD_ZERO:
358                 data->operation.orientation = 0x3;
359                 data->operation.operation = 0x16;
360                 data->count++;
361                 break;
362         case DASD_ECKD_CCW_WRITE:
363         case DASD_ECKD_CCW_WRITE_MT:
364         case DASD_ECKD_CCW_WRITE_KD:
365         case DASD_ECKD_CCW_WRITE_KD_MT:
366                 data->auxiliary.length_valid = 0x1;
367                 data->length = reclen;
368                 data->operation.operation = 0x01;
369                 break;
370         case DASD_ECKD_CCW_WRITE_CKD:
371         case DASD_ECKD_CCW_WRITE_CKD_MT:
372                 data->auxiliary.length_valid = 0x1;
373                 data->length = reclen;
374                 data->operation.operation = 0x03;
375                 break;
376         case DASD_ECKD_CCW_WRITE_TRACK_DATA:
377                 data->auxiliary.length_valid = 0x1;
378                 data->length = reclen;  /* not tlf, as one might think */
379                 data->operation.operation = 0x3F;
380                 data->extended_operation = 0x23;
381                 break;
382         case DASD_ECKD_CCW_READ:
383         case DASD_ECKD_CCW_READ_MT:
384         case DASD_ECKD_CCW_READ_KD:
385         case DASD_ECKD_CCW_READ_KD_MT:
386                 data->auxiliary.length_valid = 0x1;
387                 data->length = reclen;
388                 data->operation.operation = 0x06;
389                 break;
390         case DASD_ECKD_CCW_READ_CKD:
391         case DASD_ECKD_CCW_READ_CKD_MT:
392                 data->auxiliary.length_valid = 0x1;
393                 data->length = reclen;
394                 data->operation.operation = 0x16;
395                 break;
396         case DASD_ECKD_CCW_READ_COUNT:
397                 data->operation.operation = 0x06;
398                 break;
399         case DASD_ECKD_CCW_READ_TRACK_DATA:
400                 data->auxiliary.length_valid = 0x1;
401                 data->length = tlf;
402                 data->operation.operation = 0x0C;
403                 break;
404         case DASD_ECKD_CCW_ERASE:
405                 data->length = reclen;
406                 data->auxiliary.length_valid = 0x1;
407                 data->operation.operation = 0x0b;
408                 break;
409         default:
410                 DBF_DEV_EVENT(DBF_ERR, device,
411                             "fill LRE unknown opcode 0x%x", cmd);
412                 BUG();
413         }
414         set_ch_t(&data->seek_addr,
415                  trk / private->rdc_data.trk_per_cyl,
416                  trk % private->rdc_data.trk_per_cyl);
417         data->search_arg.cyl = data->seek_addr.cyl;
418         data->search_arg.head = data->seek_addr.head;
419         data->search_arg.record = rec_on_trk;
420 }
421
422 static int prefix_LRE(struct ccw1 *ccw, struct PFX_eckd_data *pfxdata,
423                       unsigned int trk, unsigned int totrk, int cmd,
424                       struct dasd_device *basedev, struct dasd_device *startdev,
425                       unsigned char format, unsigned int rec_on_trk, int count,
426                       unsigned int blksize, unsigned int tlf)
427 {
428         struct dasd_eckd_private *basepriv, *startpriv;
429         struct DE_eckd_data *dedata;
430         struct LRE_eckd_data *lredata;
431         u32 begcyl, endcyl;
432         u16 heads, beghead, endhead;
433         int rc = 0;
434
435         basepriv = (struct dasd_eckd_private *) basedev->private;
436         startpriv = (struct dasd_eckd_private *) startdev->private;
437         dedata = &pfxdata->define_extent;
438         lredata = &pfxdata->locate_record;
439
440         ccw->cmd_code = DASD_ECKD_CCW_PFX;
441         ccw->flags = 0;
442         ccw->count = sizeof(*pfxdata);
443         ccw->cda = (__u32) __pa(pfxdata);
444
445         memset(pfxdata, 0, sizeof(*pfxdata));
446         /* prefix data */
447         if (format > 1) {
448                 DBF_DEV_EVENT(DBF_ERR, basedev,
449                               "PFX LRE unknown format 0x%x", format);
450                 BUG();
451                 return -EINVAL;
452         }
453         pfxdata->format = format;
454         pfxdata->base_address = basepriv->ned->unit_addr;
455         pfxdata->base_lss = basepriv->ned->ID;
456         pfxdata->validity.define_extent = 1;
457
458         /* private uid is kept up to date, conf_data may be outdated */
459         if (startpriv->uid.type != UA_BASE_DEVICE) {
460                 pfxdata->validity.verify_base = 1;
461                 if (startpriv->uid.type == UA_HYPER_PAV_ALIAS)
462                         pfxdata->validity.hyper_pav = 1;
463         }
464
465         /* define extend data (mostly)*/
466         switch (cmd) {
467         case DASD_ECKD_CCW_READ_HOME_ADDRESS:
468         case DASD_ECKD_CCW_READ_RECORD_ZERO:
469         case DASD_ECKD_CCW_READ:
470         case DASD_ECKD_CCW_READ_MT:
471         case DASD_ECKD_CCW_READ_CKD:
472         case DASD_ECKD_CCW_READ_CKD_MT:
473         case DASD_ECKD_CCW_READ_KD:
474         case DASD_ECKD_CCW_READ_KD_MT:
475         case DASD_ECKD_CCW_READ_COUNT:
476                 dedata->mask.perm = 0x1;
477                 dedata->attributes.operation = basepriv->attrib.operation;
478                 break;
479         case DASD_ECKD_CCW_READ_TRACK_DATA:
480                 dedata->mask.perm = 0x1;
481                 dedata->attributes.operation = basepriv->attrib.operation;
482                 dedata->blk_size = 0;
483                 break;
484         case DASD_ECKD_CCW_WRITE:
485         case DASD_ECKD_CCW_WRITE_MT:
486         case DASD_ECKD_CCW_WRITE_KD:
487         case DASD_ECKD_CCW_WRITE_KD_MT:
488                 dedata->mask.perm = 0x02;
489                 dedata->attributes.operation = basepriv->attrib.operation;
490                 rc = check_XRC_on_prefix(pfxdata, basedev);
491                 break;
492         case DASD_ECKD_CCW_WRITE_CKD:
493         case DASD_ECKD_CCW_WRITE_CKD_MT:
494                 dedata->attributes.operation = DASD_BYPASS_CACHE;
495                 rc = check_XRC_on_prefix(pfxdata, basedev);
496                 break;
497         case DASD_ECKD_CCW_ERASE:
498         case DASD_ECKD_CCW_WRITE_HOME_ADDRESS:
499         case DASD_ECKD_CCW_WRITE_RECORD_ZERO:
500                 dedata->mask.perm = 0x3;
501                 dedata->mask.auth = 0x1;
502                 dedata->attributes.operation = DASD_BYPASS_CACHE;
503                 rc = check_XRC_on_prefix(pfxdata, basedev);
504                 break;
505         case DASD_ECKD_CCW_WRITE_TRACK_DATA:
506                 dedata->mask.perm = 0x02;
507                 dedata->attributes.operation = basepriv->attrib.operation;
508                 dedata->blk_size = blksize;
509                 rc = check_XRC_on_prefix(pfxdata, basedev);
510                 break;
511         default:
512                 DBF_DEV_EVENT(DBF_ERR, basedev,
513                             "PFX LRE unknown opcode 0x%x", cmd);
514                 BUG();
515                 return -EINVAL;
516         }
517
518         dedata->attributes.mode = 0x3;  /* ECKD */
519
520         if ((basepriv->rdc_data.cu_type == 0x2105 ||
521              basepriv->rdc_data.cu_type == 0x2107 ||
522              basepriv->rdc_data.cu_type == 0x1750)
523             && !(basepriv->uses_cdl && trk < 2))
524                 dedata->ga_extended |= 0x40; /* Regular Data Format Mode */
525
526         heads = basepriv->rdc_data.trk_per_cyl;
527         begcyl = trk / heads;
528         beghead = trk % heads;
529         endcyl = totrk / heads;
530         endhead = totrk % heads;
531
532         /* check for sequential prestage - enhance cylinder range */
533         if (dedata->attributes.operation == DASD_SEQ_PRESTAGE ||
534             dedata->attributes.operation == DASD_SEQ_ACCESS) {
535
536                 if (endcyl + basepriv->attrib.nr_cyl < basepriv->real_cyl)
537                         endcyl += basepriv->attrib.nr_cyl;
538                 else
539                         endcyl = (basepriv->real_cyl - 1);
540         }
541
542         set_ch_t(&dedata->beg_ext, begcyl, beghead);
543         set_ch_t(&dedata->end_ext, endcyl, endhead);
544
545         if (format == 1) {
546                 fill_LRE_data(lredata, trk, rec_on_trk, count, cmd,
547                               basedev, blksize, tlf);
548         }
549
550         return rc;
551 }
552
553 static int prefix(struct ccw1 *ccw, struct PFX_eckd_data *pfxdata,
554                   unsigned int trk, unsigned int totrk, int cmd,
555                   struct dasd_device *basedev, struct dasd_device *startdev)
556 {
557         return prefix_LRE(ccw, pfxdata, trk, totrk, cmd, basedev, startdev,
558                           0, 0, 0, 0, 0);
559 }
560
561 static void
562 locate_record(struct ccw1 *ccw, struct LO_eckd_data *data, unsigned int trk,
563               unsigned int rec_on_trk, int no_rec, int cmd,
564               struct dasd_device * device, int reclen)
565 {
566         struct dasd_eckd_private *private;
567         int sector;
568         int dn, d;
569
570         private = (struct dasd_eckd_private *) device->private;
571
572         DBF_DEV_EVENT(DBF_INFO, device,
573                   "Locate: trk %d, rec %d, no_rec %d, cmd %d, reclen %d",
574                   trk, rec_on_trk, no_rec, cmd, reclen);
575
576         ccw->cmd_code = DASD_ECKD_CCW_LOCATE_RECORD;
577         ccw->flags = 0;
578         ccw->count = 16;
579         ccw->cda = (__u32) __pa(data);
580
581         memset(data, 0, sizeof(struct LO_eckd_data));
582         sector = 0;
583         if (rec_on_trk) {
584                 switch (private->rdc_data.dev_type) {
585                 case 0x3390:
586                         dn = ceil_quot(reclen + 6, 232);
587                         d = 9 + ceil_quot(reclen + 6 * (dn + 1), 34);
588                         sector = (49 + (rec_on_trk - 1) * (10 + d)) / 8;
589                         break;
590                 case 0x3380:
591                         d = 7 + ceil_quot(reclen + 12, 32);
592                         sector = (39 + (rec_on_trk - 1) * (8 + d)) / 7;
593                         break;
594                 }
595         }
596         data->sector = sector;
597         data->count = no_rec;
598         switch (cmd) {
599         case DASD_ECKD_CCW_WRITE_HOME_ADDRESS:
600                 data->operation.orientation = 0x3;
601                 data->operation.operation = 0x03;
602                 break;
603         case DASD_ECKD_CCW_READ_HOME_ADDRESS:
604                 data->operation.orientation = 0x3;
605                 data->operation.operation = 0x16;
606                 break;
607         case DASD_ECKD_CCW_WRITE_RECORD_ZERO:
608                 data->operation.orientation = 0x1;
609                 data->operation.operation = 0x03;
610                 data->count++;
611                 break;
612         case DASD_ECKD_CCW_READ_RECORD_ZERO:
613                 data->operation.orientation = 0x3;
614                 data->operation.operation = 0x16;
615                 data->count++;
616                 break;
617         case DASD_ECKD_CCW_WRITE:
618         case DASD_ECKD_CCW_WRITE_MT:
619         case DASD_ECKD_CCW_WRITE_KD:
620         case DASD_ECKD_CCW_WRITE_KD_MT:
621                 data->auxiliary.last_bytes_used = 0x1;
622                 data->length = reclen;
623                 data->operation.operation = 0x01;
624                 break;
625         case DASD_ECKD_CCW_WRITE_CKD:
626         case DASD_ECKD_CCW_WRITE_CKD_MT:
627                 data->auxiliary.last_bytes_used = 0x1;
628                 data->length = reclen;
629                 data->operation.operation = 0x03;
630                 break;
631         case DASD_ECKD_CCW_READ:
632         case DASD_ECKD_CCW_READ_MT:
633         case DASD_ECKD_CCW_READ_KD:
634         case DASD_ECKD_CCW_READ_KD_MT:
635                 data->auxiliary.last_bytes_used = 0x1;
636                 data->length = reclen;
637                 data->operation.operation = 0x06;
638                 break;
639         case DASD_ECKD_CCW_READ_CKD:
640         case DASD_ECKD_CCW_READ_CKD_MT:
641                 data->auxiliary.last_bytes_used = 0x1;
642                 data->length = reclen;
643                 data->operation.operation = 0x16;
644                 break;
645         case DASD_ECKD_CCW_READ_COUNT:
646                 data->operation.operation = 0x06;
647                 break;
648         case DASD_ECKD_CCW_ERASE:
649                 data->length = reclen;
650                 data->auxiliary.last_bytes_used = 0x1;
651                 data->operation.operation = 0x0b;
652                 break;
653         default:
654                 DBF_DEV_EVENT(DBF_ERR, device, "unknown locate record "
655                               "opcode 0x%x", cmd);
656         }
657         set_ch_t(&data->seek_addr,
658                  trk / private->rdc_data.trk_per_cyl,
659                  trk % private->rdc_data.trk_per_cyl);
660         data->search_arg.cyl = data->seek_addr.cyl;
661         data->search_arg.head = data->seek_addr.head;
662         data->search_arg.record = rec_on_trk;
663 }
664
665 /*
666  * Returns 1 if the block is one of the special blocks that needs
667  * to get read/written with the KD variant of the command.
668  * That is DASD_ECKD_READ_KD_MT instead of DASD_ECKD_READ_MT and
669  * DASD_ECKD_WRITE_KD_MT instead of DASD_ECKD_WRITE_MT.
670  * Luckily the KD variants differ only by one bit (0x08) from the
671  * normal variant. So don't wonder about code like:
672  * if (dasd_eckd_cdl_special(blk_per_trk, recid))
673  *         ccw->cmd_code |= 0x8;
674  */
675 static inline int
676 dasd_eckd_cdl_special(int blk_per_trk, int recid)
677 {
678         if (recid < 3)
679                 return 1;
680         if (recid < blk_per_trk)
681                 return 0;
682         if (recid < 2 * blk_per_trk)
683                 return 1;
684         return 0;
685 }
686
687 /*
688  * Returns the record size for the special blocks of the cdl format.
689  * Only returns something useful if dasd_eckd_cdl_special is true
690  * for the recid.
691  */
692 static inline int
693 dasd_eckd_cdl_reclen(int recid)
694 {
695         if (recid < 3)
696                 return sizes_trk0[recid];
697         return LABEL_SIZE;
698 }
699
700 /*
701  * Generate device unique id that specifies the physical device.
702  */
703 static int dasd_eckd_generate_uid(struct dasd_device *device)
704 {
705         struct dasd_eckd_private *private;
706         struct dasd_uid *uid;
707         int count;
708         unsigned long flags;
709
710         private = (struct dasd_eckd_private *) device->private;
711         if (!private)
712                 return -ENODEV;
713         if (!private->ned || !private->gneq)
714                 return -ENODEV;
715         uid = &private->uid;
716         spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
717         memset(uid, 0, sizeof(struct dasd_uid));
718         memcpy(uid->vendor, private->ned->HDA_manufacturer,
719                sizeof(uid->vendor) - 1);
720         EBCASC(uid->vendor, sizeof(uid->vendor) - 1);
721         memcpy(uid->serial, private->ned->HDA_location,
722                sizeof(uid->serial) - 1);
723         EBCASC(uid->serial, sizeof(uid->serial) - 1);
724         uid->ssid = private->gneq->subsystemID;
725         uid->real_unit_addr = private->ned->unit_addr;
726         if (private->sneq) {
727                 uid->type = private->sneq->sua_flags;
728                 if (uid->type == UA_BASE_PAV_ALIAS)
729                         uid->base_unit_addr = private->sneq->base_unit_addr;
730         } else {
731                 uid->type = UA_BASE_DEVICE;
732         }
733         if (private->vdsneq) {
734                 for (count = 0; count < 16; count++) {
735                         sprintf(uid->vduit+2*count, "%02x",
736                                 private->vdsneq->uit[count]);
737                 }
738         }
739         spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
740         return 0;
741 }
742
743 static int dasd_eckd_get_uid(struct dasd_device *device, struct dasd_uid *uid)
744 {
745         struct dasd_eckd_private *private;
746         unsigned long flags;
747
748         if (device->private) {
749                 private = (struct dasd_eckd_private *)device->private;
750                 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
751                 *uid = private->uid;
752                 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
753                 return 0;
754         }
755         return -EINVAL;
756 }
757
758 static struct dasd_ccw_req *dasd_eckd_build_rcd_lpm(struct dasd_device *device,
759                                                     void *rcd_buffer,
760                                                     struct ciw *ciw, __u8 lpm)
761 {
762         struct dasd_ccw_req *cqr;
763         struct ccw1 *ccw;
764
765         cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* RCD */, ciw->count,
766                                    device);
767
768         if (IS_ERR(cqr)) {
769                 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
770                               "Could not allocate RCD request");
771                 return cqr;
772         }
773
774         ccw = cqr->cpaddr;
775         ccw->cmd_code = ciw->cmd;
776         ccw->cda = (__u32)(addr_t)rcd_buffer;
777         ccw->count = ciw->count;
778
779         cqr->startdev = device;
780         cqr->memdev = device;
781         cqr->block = NULL;
782         cqr->expires = 10*HZ;
783         cqr->lpm = lpm;
784         cqr->retries = 256;
785         cqr->buildclk = get_clock();
786         cqr->status = DASD_CQR_FILLED;
787         return cqr;
788 }
789
790 static int dasd_eckd_read_conf_lpm(struct dasd_device *device,
791                                    void **rcd_buffer,
792                                    int *rcd_buffer_size, __u8 lpm)
793 {
794         struct ciw *ciw;
795         char *rcd_buf = NULL;
796         int ret;
797         struct dasd_ccw_req *cqr;
798
799         /*
800          * scan for RCD command in extended SenseID data
801          */
802         ciw = ccw_device_get_ciw(device->cdev, CIW_TYPE_RCD);
803         if (!ciw || ciw->cmd == 0) {
804                 ret = -EOPNOTSUPP;
805                 goto out_error;
806         }
807         rcd_buf = kzalloc(ciw->count, GFP_KERNEL | GFP_DMA);
808         if (!rcd_buf) {
809                 ret = -ENOMEM;
810                 goto out_error;
811         }
812
813         /*
814          * buffer has to start with EBCDIC "V1.0" to show
815          * support for virtual device SNEQ
816          */
817         rcd_buf[0] = 0xE5;
818         rcd_buf[1] = 0xF1;
819         rcd_buf[2] = 0x4B;
820         rcd_buf[3] = 0xF0;
821         cqr = dasd_eckd_build_rcd_lpm(device, rcd_buf, ciw, lpm);
822         if (IS_ERR(cqr)) {
823                 ret =  PTR_ERR(cqr);
824                 goto out_error;
825         }
826         ret = dasd_sleep_on(cqr);
827         /*
828          * on success we update the user input parms
829          */
830         dasd_sfree_request(cqr, cqr->memdev);
831         if (ret)
832                 goto out_error;
833
834         *rcd_buffer_size = ciw->count;
835         *rcd_buffer = rcd_buf;
836         return 0;
837 out_error:
838         kfree(rcd_buf);
839         *rcd_buffer = NULL;
840         *rcd_buffer_size = 0;
841         return ret;
842 }
843
844 static int dasd_eckd_identify_conf_parts(struct dasd_eckd_private *private)
845 {
846
847         struct dasd_sneq *sneq;
848         int i, count;
849
850         private->ned = NULL;
851         private->sneq = NULL;
852         private->vdsneq = NULL;
853         private->gneq = NULL;
854         count = private->conf_len / sizeof(struct dasd_sneq);
855         sneq = (struct dasd_sneq *)private->conf_data;
856         for (i = 0; i < count; ++i) {
857                 if (sneq->flags.identifier == 1 && sneq->format == 1)
858                         private->sneq = sneq;
859                 else if (sneq->flags.identifier == 1 && sneq->format == 4)
860                         private->vdsneq = (struct vd_sneq *)sneq;
861                 else if (sneq->flags.identifier == 2)
862                         private->gneq = (struct dasd_gneq *)sneq;
863                 else if (sneq->flags.identifier == 3 && sneq->res1 == 1)
864                         private->ned = (struct dasd_ned *)sneq;
865                 sneq++;
866         }
867         if (!private->ned || !private->gneq) {
868                 private->ned = NULL;
869                 private->sneq = NULL;
870                 private->vdsneq = NULL;
871                 private->gneq = NULL;
872                 return -EINVAL;
873         }
874         return 0;
875
876 };
877
878 static unsigned char dasd_eckd_path_access(void *conf_data, int conf_len)
879 {
880         struct dasd_gneq *gneq;
881         int i, count, found;
882
883         count = conf_len / sizeof(*gneq);
884         gneq = (struct dasd_gneq *)conf_data;
885         found = 0;
886         for (i = 0; i < count; ++i) {
887                 if (gneq->flags.identifier == 2) {
888                         found = 1;
889                         break;
890                 }
891                 gneq++;
892         }
893         if (found)
894                 return ((char *)gneq)[18] & 0x07;
895         else
896                 return 0;
897 }
898
899 static int dasd_eckd_read_conf(struct dasd_device *device)
900 {
901         void *conf_data;
902         int conf_len, conf_data_saved;
903         int rc;
904         __u8 lpm;
905         struct dasd_eckd_private *private;
906         struct dasd_eckd_path *path_data;
907
908         private = (struct dasd_eckd_private *) device->private;
909         path_data = (struct dasd_eckd_path *) &private->path_data;
910         path_data->opm = ccw_device_get_path_mask(device->cdev);
911         lpm = 0x80;
912         conf_data_saved = 0;
913         /* get configuration data per operational path */
914         for (lpm = 0x80; lpm; lpm>>= 1) {
915                 if (lpm & path_data->opm){
916                         rc = dasd_eckd_read_conf_lpm(device, &conf_data,
917                                                      &conf_len, lpm);
918                         if (rc && rc != -EOPNOTSUPP) {  /* -EOPNOTSUPP is ok */
919                                 DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
920                                           "Read configuration data returned "
921                                           "error %d", rc);
922                                 return rc;
923                         }
924                         if (conf_data == NULL) {
925                                 DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
926                                                 "No configuration data "
927                                                 "retrieved");
928                                 continue;       /* no error */
929                         }
930                         /* save first valid configuration data */
931                         if (!conf_data_saved) {
932                                 kfree(private->conf_data);
933                                 private->conf_data = conf_data;
934                                 private->conf_len = conf_len;
935                                 if (dasd_eckd_identify_conf_parts(private)) {
936                                         private->conf_data = NULL;
937                                         private->conf_len = 0;
938                                         kfree(conf_data);
939                                         continue;
940                                 }
941                                 conf_data_saved++;
942                         }
943                         switch (dasd_eckd_path_access(conf_data, conf_len)) {
944                         case 0x02:
945                                 path_data->npm |= lpm;
946                                 break;
947                         case 0x03:
948                                 path_data->ppm |= lpm;
949                                 break;
950                         }
951                         if (conf_data != private->conf_data)
952                                 kfree(conf_data);
953                 }
954         }
955         return 0;
956 }
957
958 static int dasd_eckd_read_features(struct dasd_device *device)
959 {
960         struct dasd_psf_prssd_data *prssdp;
961         struct dasd_rssd_features *features;
962         struct dasd_ccw_req *cqr;
963         struct ccw1 *ccw;
964         int rc;
965         struct dasd_eckd_private *private;
966
967         private = (struct dasd_eckd_private *) device->private;
968         memset(&private->features, 0, sizeof(struct dasd_rssd_features));
969         cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */ + 1 /* RSSD */,
970                                    (sizeof(struct dasd_psf_prssd_data) +
971                                     sizeof(struct dasd_rssd_features)),
972                                    device);
973         if (IS_ERR(cqr)) {
974                 DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s", "Could not "
975                                 "allocate initialization request");
976                 return PTR_ERR(cqr);
977         }
978         cqr->startdev = device;
979         cqr->memdev = device;
980         cqr->block = NULL;
981         cqr->retries = 256;
982         cqr->expires = 10 * HZ;
983
984         /* Prepare for Read Subsystem Data */
985         prssdp = (struct dasd_psf_prssd_data *) cqr->data;
986         memset(prssdp, 0, sizeof(struct dasd_psf_prssd_data));
987         prssdp->order = PSF_ORDER_PRSSD;
988         prssdp->suborder = 0x41;        /* Read Feature Codes */
989         /* all other bytes of prssdp must be zero */
990
991         ccw = cqr->cpaddr;
992         ccw->cmd_code = DASD_ECKD_CCW_PSF;
993         ccw->count = sizeof(struct dasd_psf_prssd_data);
994         ccw->flags |= CCW_FLAG_CC;
995         ccw->cda = (__u32)(addr_t) prssdp;
996
997         /* Read Subsystem Data - feature codes */
998         features = (struct dasd_rssd_features *) (prssdp + 1);
999         memset(features, 0, sizeof(struct dasd_rssd_features));
1000
1001         ccw++;
1002         ccw->cmd_code = DASD_ECKD_CCW_RSSD;
1003         ccw->count = sizeof(struct dasd_rssd_features);
1004         ccw->cda = (__u32)(addr_t) features;
1005
1006         cqr->buildclk = get_clock();
1007         cqr->status = DASD_CQR_FILLED;
1008         rc = dasd_sleep_on(cqr);
1009         if (rc == 0) {
1010                 prssdp = (struct dasd_psf_prssd_data *) cqr->data;
1011                 features = (struct dasd_rssd_features *) (prssdp + 1);
1012                 memcpy(&private->features, features,
1013                        sizeof(struct dasd_rssd_features));
1014         } else
1015                 dev_warn(&device->cdev->dev, "Reading device feature codes"
1016                          " failed with rc=%d\n", rc);
1017         dasd_sfree_request(cqr, cqr->memdev);
1018         return rc;
1019 }
1020
1021
1022 /*
1023  * Build CP for Perform Subsystem Function - SSC.
1024  */
1025 static struct dasd_ccw_req *dasd_eckd_build_psf_ssc(struct dasd_device *device,
1026                                                     int enable_pav)
1027 {
1028         struct dasd_ccw_req *cqr;
1029         struct dasd_psf_ssc_data *psf_ssc_data;
1030         struct ccw1 *ccw;
1031
1032         cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */ ,
1033                                   sizeof(struct dasd_psf_ssc_data),
1034                                   device);
1035
1036         if (IS_ERR(cqr)) {
1037                 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1038                            "Could not allocate PSF-SSC request");
1039                 return cqr;
1040         }
1041         psf_ssc_data = (struct dasd_psf_ssc_data *)cqr->data;
1042         psf_ssc_data->order = PSF_ORDER_SSC;
1043         psf_ssc_data->suborder = 0xc0;
1044         if (enable_pav) {
1045                 psf_ssc_data->suborder |= 0x08;
1046                 psf_ssc_data->reserved[0] = 0x88;
1047         }
1048         ccw = cqr->cpaddr;
1049         ccw->cmd_code = DASD_ECKD_CCW_PSF;
1050         ccw->cda = (__u32)(addr_t)psf_ssc_data;
1051         ccw->count = 66;
1052
1053         cqr->startdev = device;
1054         cqr->memdev = device;
1055         cqr->block = NULL;
1056         cqr->retries = 256;
1057         cqr->expires = 10*HZ;
1058         cqr->buildclk = get_clock();
1059         cqr->status = DASD_CQR_FILLED;
1060         return cqr;
1061 }
1062
1063 /*
1064  * Perform Subsystem Function.
1065  * It is necessary to trigger CIO for channel revalidation since this
1066  * call might change behaviour of DASD devices.
1067  */
1068 static int
1069 dasd_eckd_psf_ssc(struct dasd_device *device, int enable_pav)
1070 {
1071         struct dasd_ccw_req *cqr;
1072         int rc;
1073
1074         cqr = dasd_eckd_build_psf_ssc(device, enable_pav);
1075         if (IS_ERR(cqr))
1076                 return PTR_ERR(cqr);
1077
1078         rc = dasd_sleep_on(cqr);
1079         if (!rc)
1080                 /* trigger CIO to reprobe devices */
1081                 css_schedule_reprobe();
1082         dasd_sfree_request(cqr, cqr->memdev);
1083         return rc;
1084 }
1085
1086 /*
1087  * Valide storage server of current device.
1088  */
1089 static void dasd_eckd_validate_server(struct dasd_device *device)
1090 {
1091         int rc;
1092         struct dasd_eckd_private *private;
1093         int enable_pav;
1094
1095         if (dasd_nopav || MACHINE_IS_VM)
1096                 enable_pav = 0;
1097         else
1098                 enable_pav = 1;
1099         rc = dasd_eckd_psf_ssc(device, enable_pav);
1100
1101         /* may be requested feature is not available on server,
1102          * therefore just report error and go ahead */
1103         private = (struct dasd_eckd_private *) device->private;
1104         DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "PSF-SSC for SSID %04x "
1105                         "returned rc=%d", private->uid.ssid, rc);
1106 }
1107
1108 /*
1109  * Check device characteristics.
1110  * If the device is accessible using ECKD discipline, the device is enabled.
1111  */
1112 static int
1113 dasd_eckd_check_characteristics(struct dasd_device *device)
1114 {
1115         struct dasd_eckd_private *private;
1116         struct dasd_block *block;
1117         struct dasd_uid temp_uid;
1118         int is_known, rc, i;
1119         int readonly;
1120         unsigned long value;
1121
1122         if (!ccw_device_is_pathgroup(device->cdev)) {
1123                 dev_warn(&device->cdev->dev,
1124                          "A channel path group could not be established\n");
1125                 return -EIO;
1126         }
1127         if (!ccw_device_is_multipath(device->cdev)) {
1128                 dev_info(&device->cdev->dev,
1129                          "The DASD is not operating in multipath mode\n");
1130         }
1131         private = (struct dasd_eckd_private *) device->private;
1132         if (!private) {
1133                 private = kzalloc(sizeof(*private), GFP_KERNEL | GFP_DMA);
1134                 if (!private) {
1135                         dev_warn(&device->cdev->dev,
1136                                  "Allocating memory for private DASD data "
1137                                  "failed\n");
1138                         return -ENOMEM;
1139                 }
1140                 device->private = (void *) private;
1141         } else {
1142                 memset(private, 0, sizeof(*private));
1143         }
1144         /* Invalidate status of initial analysis. */
1145         private->init_cqr_status = -1;
1146         /* Set default cache operations. */
1147         private->attrib.operation = DASD_NORMAL_CACHE;
1148         private->attrib.nr_cyl = 0;
1149
1150         /* Read Configuration Data */
1151         rc = dasd_eckd_read_conf(device);
1152         if (rc)
1153                 goto out_err1;
1154
1155         /* set default timeout */
1156         device->default_expires = DASD_EXPIRES;
1157         if (private->gneq) {
1158                 value = 1;
1159                 for (i = 0; i < private->gneq->timeout.value; i++)
1160                         value = 10 * value;
1161                 value = value * private->gneq->timeout.number;
1162                 /* do not accept useless values */
1163                 if (value != 0 && value <= DASD_EXPIRES_MAX)
1164                         device->default_expires = value;
1165         }
1166
1167         /* Generate device unique id */
1168         rc = dasd_eckd_generate_uid(device);
1169         if (rc)
1170                 goto out_err1;
1171
1172         dasd_eckd_get_uid(device, &temp_uid);
1173         if (temp_uid.type == UA_BASE_DEVICE) {
1174                 block = dasd_alloc_block();
1175                 if (IS_ERR(block)) {
1176                         DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
1177                                         "could not allocate dasd "
1178                                         "block structure");
1179                         rc = PTR_ERR(block);
1180                         goto out_err1;
1181                 }
1182                 device->block = block;
1183                 block->base = device;
1184         }
1185
1186         /* register lcu with alias handling, enable PAV if this is a new lcu */
1187         is_known = dasd_alias_make_device_known_to_lcu(device);
1188         if (is_known < 0) {
1189                 rc = is_known;
1190                 goto out_err2;
1191         }
1192         /*
1193          * dasd_eckd_validate_server is done on the first device that
1194          * is found for an LCU. All later other devices have to wait
1195          * for it, so they will read the correct feature codes.
1196          */
1197         if (!is_known) {
1198                 dasd_eckd_validate_server(device);
1199                 dasd_alias_lcu_setup_complete(device);
1200         } else
1201                 dasd_alias_wait_for_lcu_setup(device);
1202
1203         /* device may report different configuration data after LCU setup */
1204         rc = dasd_eckd_read_conf(device);
1205         if (rc)
1206                 goto out_err3;
1207
1208         /* Read Feature Codes */
1209         dasd_eckd_read_features(device);
1210
1211         /* Read Device Characteristics */
1212         rc = dasd_generic_read_dev_chars(device, DASD_ECKD_MAGIC,
1213                                          &private->rdc_data, 64);
1214         if (rc) {
1215                 DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
1216                                 "Read device characteristic failed, rc=%d", rc);
1217                 goto out_err3;
1218         }
1219         /* find the valid cylinder size */
1220         if (private->rdc_data.no_cyl == LV_COMPAT_CYL &&
1221             private->rdc_data.long_no_cyl)
1222                 private->real_cyl = private->rdc_data.long_no_cyl;
1223         else
1224                 private->real_cyl = private->rdc_data.no_cyl;
1225
1226         readonly = dasd_device_is_ro(device);
1227         if (readonly)
1228                 set_bit(DASD_FLAG_DEVICE_RO, &device->flags);
1229
1230         dev_info(&device->cdev->dev, "New DASD %04X/%02X (CU %04X/%02X) "
1231                  "with %d cylinders, %d heads, %d sectors%s\n",
1232                  private->rdc_data.dev_type,
1233                  private->rdc_data.dev_model,
1234                  private->rdc_data.cu_type,
1235                  private->rdc_data.cu_model.model,
1236                  private->real_cyl,
1237                  private->rdc_data.trk_per_cyl,
1238                  private->rdc_data.sec_per_trk,
1239                  readonly ? ", read-only device" : "");
1240         return 0;
1241
1242 out_err3:
1243         dasd_alias_disconnect_device_from_lcu(device);
1244 out_err2:
1245         dasd_free_block(device->block);
1246         device->block = NULL;
1247 out_err1:
1248         kfree(private->conf_data);
1249         kfree(device->private);
1250         device->private = NULL;
1251         return rc;
1252 }
1253
1254 static void dasd_eckd_uncheck_device(struct dasd_device *device)
1255 {
1256         struct dasd_eckd_private *private;
1257
1258         private = (struct dasd_eckd_private *) device->private;
1259         dasd_alias_disconnect_device_from_lcu(device);
1260         private->ned = NULL;
1261         private->sneq = NULL;
1262         private->vdsneq = NULL;
1263         private->gneq = NULL;
1264         private->conf_len = 0;
1265         kfree(private->conf_data);
1266         private->conf_data = NULL;
1267 }
1268
1269 static struct dasd_ccw_req *
1270 dasd_eckd_analysis_ccw(struct dasd_device *device)
1271 {
1272         struct dasd_eckd_private *private;
1273         struct eckd_count *count_data;
1274         struct LO_eckd_data *LO_data;
1275         struct dasd_ccw_req *cqr;
1276         struct ccw1 *ccw;
1277         int cplength, datasize;
1278         int i;
1279
1280         private = (struct dasd_eckd_private *) device->private;
1281
1282         cplength = 8;
1283         datasize = sizeof(struct DE_eckd_data) + 2*sizeof(struct LO_eckd_data);
1284         cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength, datasize, device);
1285         if (IS_ERR(cqr))
1286                 return cqr;
1287         ccw = cqr->cpaddr;
1288         /* Define extent for the first 3 tracks. */
1289         define_extent(ccw++, cqr->data, 0, 2,
1290                       DASD_ECKD_CCW_READ_COUNT, device);
1291         LO_data = cqr->data + sizeof(struct DE_eckd_data);
1292         /* Locate record for the first 4 records on track 0. */
1293         ccw[-1].flags |= CCW_FLAG_CC;
1294         locate_record(ccw++, LO_data++, 0, 0, 4,
1295                       DASD_ECKD_CCW_READ_COUNT, device, 0);
1296
1297         count_data = private->count_area;
1298         for (i = 0; i < 4; i++) {
1299                 ccw[-1].flags |= CCW_FLAG_CC;
1300                 ccw->cmd_code = DASD_ECKD_CCW_READ_COUNT;
1301                 ccw->flags = 0;
1302                 ccw->count = 8;
1303                 ccw->cda = (__u32)(addr_t) count_data;
1304                 ccw++;
1305                 count_data++;
1306         }
1307
1308         /* Locate record for the first record on track 2. */
1309         ccw[-1].flags |= CCW_FLAG_CC;
1310         locate_record(ccw++, LO_data++, 2, 0, 1,
1311                       DASD_ECKD_CCW_READ_COUNT, device, 0);
1312         /* Read count ccw. */
1313         ccw[-1].flags |= CCW_FLAG_CC;
1314         ccw->cmd_code = DASD_ECKD_CCW_READ_COUNT;
1315         ccw->flags = 0;
1316         ccw->count = 8;
1317         ccw->cda = (__u32)(addr_t) count_data;
1318
1319         cqr->block = NULL;
1320         cqr->startdev = device;
1321         cqr->memdev = device;
1322         cqr->retries = 255;
1323         cqr->buildclk = get_clock();
1324         cqr->status = DASD_CQR_FILLED;
1325         return cqr;
1326 }
1327
1328 /* differentiate between 'no record found' and any other error */
1329 static int dasd_eckd_analysis_evaluation(struct dasd_ccw_req *init_cqr)
1330 {
1331         char *sense;
1332         if (init_cqr->status == DASD_CQR_DONE)
1333                 return INIT_CQR_OK;
1334         else if (init_cqr->status == DASD_CQR_NEED_ERP ||
1335                  init_cqr->status == DASD_CQR_FAILED) {
1336                 sense = dasd_get_sense(&init_cqr->irb);
1337                 if (sense && (sense[1] & SNS1_NO_REC_FOUND))
1338                         return INIT_CQR_UNFORMATTED;
1339                 else
1340                         return INIT_CQR_ERROR;
1341         } else
1342                 return INIT_CQR_ERROR;
1343 }
1344
1345 /*
1346  * This is the callback function for the init_analysis cqr. It saves
1347  * the status of the initial analysis ccw before it frees it and kicks
1348  * the device to continue the startup sequence. This will call
1349  * dasd_eckd_do_analysis again (if the devices has not been marked
1350  * for deletion in the meantime).
1351  */
1352 static void dasd_eckd_analysis_callback(struct dasd_ccw_req *init_cqr,
1353                                         void *data)
1354 {
1355         struct dasd_eckd_private *private;
1356         struct dasd_device *device;
1357
1358         device = init_cqr->startdev;
1359         private = (struct dasd_eckd_private *) device->private;
1360         private->init_cqr_status = dasd_eckd_analysis_evaluation(init_cqr);
1361         dasd_sfree_request(init_cqr, device);
1362         dasd_kick_device(device);
1363 }
1364
1365 static int dasd_eckd_start_analysis(struct dasd_block *block)
1366 {
1367         struct dasd_eckd_private *private;
1368         struct dasd_ccw_req *init_cqr;
1369
1370         private = (struct dasd_eckd_private *) block->base->private;
1371         init_cqr = dasd_eckd_analysis_ccw(block->base);
1372         if (IS_ERR(init_cqr))
1373                 return PTR_ERR(init_cqr);
1374         init_cqr->callback = dasd_eckd_analysis_callback;
1375         init_cqr->callback_data = NULL;
1376         init_cqr->expires = 5*HZ;
1377         /* first try without ERP, so we can later handle unformatted
1378          * devices as special case
1379          */
1380         clear_bit(DASD_CQR_FLAGS_USE_ERP, &init_cqr->flags);
1381         init_cqr->retries = 0;
1382         dasd_add_request_head(init_cqr);
1383         return -EAGAIN;
1384 }
1385
1386 static int dasd_eckd_end_analysis(struct dasd_block *block)
1387 {
1388         struct dasd_device *device;
1389         struct dasd_eckd_private *private;
1390         struct eckd_count *count_area;
1391         unsigned int sb, blk_per_trk;
1392         int status, i;
1393         struct dasd_ccw_req *init_cqr;
1394
1395         device = block->base;
1396         private = (struct dasd_eckd_private *) device->private;
1397         status = private->init_cqr_status;
1398         private->init_cqr_status = -1;
1399         if (status == INIT_CQR_ERROR) {
1400                 /* try again, this time with full ERP */
1401                 init_cqr = dasd_eckd_analysis_ccw(device);
1402                 dasd_sleep_on(init_cqr);
1403                 status = dasd_eckd_analysis_evaluation(init_cqr);
1404                 dasd_sfree_request(init_cqr, device);
1405         }
1406
1407         if (status == INIT_CQR_UNFORMATTED) {
1408                 dev_warn(&device->cdev->dev, "The DASD is not formatted\n");
1409                 return -EMEDIUMTYPE;
1410         } else if (status == INIT_CQR_ERROR) {
1411                 dev_err(&device->cdev->dev,
1412                         "Detecting the DASD disk layout failed because "
1413                         "of an I/O error\n");
1414                 return -EIO;
1415         }
1416
1417         private->uses_cdl = 1;
1418         /* Check Track 0 for Compatible Disk Layout */
1419         count_area = NULL;
1420         for (i = 0; i < 3; i++) {
1421                 if (private->count_area[i].kl != 4 ||
1422                     private->count_area[i].dl != dasd_eckd_cdl_reclen(i) - 4) {
1423                         private->uses_cdl = 0;
1424                         break;
1425                 }
1426         }
1427         if (i == 3)
1428                 count_area = &private->count_area[4];
1429
1430         if (private->uses_cdl == 0) {
1431                 for (i = 0; i < 5; i++) {
1432                         if ((private->count_area[i].kl != 0) ||
1433                             (private->count_area[i].dl !=
1434                              private->count_area[0].dl))
1435                                 break;
1436                 }
1437                 if (i == 5)
1438                         count_area = &private->count_area[0];
1439         } else {
1440                 if (private->count_area[3].record == 1)
1441                         dev_warn(&device->cdev->dev,
1442                                  "Track 0 has no records following the VTOC\n");
1443         }
1444         if (count_area != NULL && count_area->kl == 0) {
1445                 /* we found notthing violating our disk layout */
1446                 if (dasd_check_blocksize(count_area->dl) == 0)
1447                         block->bp_block = count_area->dl;
1448         }
1449         if (block->bp_block == 0) {
1450                 dev_warn(&device->cdev->dev,
1451                          "The disk layout of the DASD is not supported\n");
1452                 return -EMEDIUMTYPE;
1453         }
1454         block->s2b_shift = 0;   /* bits to shift 512 to get a block */
1455         for (sb = 512; sb < block->bp_block; sb = sb << 1)
1456                 block->s2b_shift++;
1457
1458         blk_per_trk = recs_per_track(&private->rdc_data, 0, block->bp_block);
1459         block->blocks = (private->real_cyl *
1460                           private->rdc_data.trk_per_cyl *
1461                           blk_per_trk);
1462
1463         dev_info(&device->cdev->dev,
1464                  "DASD with %d KB/block, %d KB total size, %d KB/track, "
1465                  "%s\n", (block->bp_block >> 10),
1466                  ((private->real_cyl *
1467                    private->rdc_data.trk_per_cyl *
1468                    blk_per_trk * (block->bp_block >> 9)) >> 1),
1469                  ((blk_per_trk * block->bp_block) >> 10),
1470                  private->uses_cdl ?
1471                  "compatible disk layout" : "linux disk layout");
1472
1473         return 0;
1474 }
1475
1476 static int dasd_eckd_do_analysis(struct dasd_block *block)
1477 {
1478         struct dasd_eckd_private *private;
1479
1480         private = (struct dasd_eckd_private *) block->base->private;
1481         if (private->init_cqr_status < 0)
1482                 return dasd_eckd_start_analysis(block);
1483         else
1484                 return dasd_eckd_end_analysis(block);
1485 }
1486
1487 static int dasd_eckd_ready_to_online(struct dasd_device *device)
1488 {
1489         return dasd_alias_add_device(device);
1490 };
1491
1492 static int dasd_eckd_online_to_ready(struct dasd_device *device)
1493 {
1494         cancel_work_sync(&device->reload_device);
1495         return dasd_alias_remove_device(device);
1496 };
1497
1498 static int
1499 dasd_eckd_fill_geometry(struct dasd_block *block, struct hd_geometry *geo)
1500 {
1501         struct dasd_eckd_private *private;
1502
1503         private = (struct dasd_eckd_private *) block->base->private;
1504         if (dasd_check_blocksize(block->bp_block) == 0) {
1505                 geo->sectors = recs_per_track(&private->rdc_data,
1506                                               0, block->bp_block);
1507         }
1508         geo->cylinders = private->rdc_data.no_cyl;
1509         geo->heads = private->rdc_data.trk_per_cyl;
1510         return 0;
1511 }
1512
1513 static struct dasd_ccw_req *
1514 dasd_eckd_format_device(struct dasd_device * device,
1515                         struct format_data_t * fdata)
1516 {
1517         struct dasd_eckd_private *private;
1518         struct dasd_ccw_req *fcp;
1519         struct eckd_count *ect;
1520         struct ccw1 *ccw;
1521         void *data;
1522         int rpt;
1523         struct ch_t address;
1524         int cplength, datasize;
1525         int i;
1526         int intensity = 0;
1527         int r0_perm;
1528
1529         private = (struct dasd_eckd_private *) device->private;
1530         rpt = recs_per_track(&private->rdc_data, 0, fdata->blksize);
1531         set_ch_t(&address,
1532                  fdata->start_unit / private->rdc_data.trk_per_cyl,
1533                  fdata->start_unit % private->rdc_data.trk_per_cyl);
1534
1535         /* Sanity checks. */
1536         if (fdata->start_unit >=
1537             (private->real_cyl * private->rdc_data.trk_per_cyl)) {
1538                 dev_warn(&device->cdev->dev, "Start track number %d used in "
1539                          "formatting is too big\n", fdata->start_unit);
1540                 return ERR_PTR(-EINVAL);
1541         }
1542         if (fdata->start_unit > fdata->stop_unit) {
1543                 dev_warn(&device->cdev->dev, "Start track %d used in "
1544                          "formatting exceeds end track\n", fdata->start_unit);
1545                 return ERR_PTR(-EINVAL);
1546         }
1547         if (dasd_check_blocksize(fdata->blksize) != 0) {
1548                 dev_warn(&device->cdev->dev,
1549                          "The DASD cannot be formatted with block size %d\n",
1550                          fdata->blksize);
1551                 return ERR_PTR(-EINVAL);
1552         }
1553
1554         /*
1555          * fdata->intensity is a bit string that tells us what to do:
1556          *   Bit 0: write record zero
1557          *   Bit 1: write home address, currently not supported
1558          *   Bit 2: invalidate tracks
1559          *   Bit 3: use OS/390 compatible disk layout (cdl)
1560          *   Bit 4: do not allow storage subsystem to modify record zero
1561          * Only some bit combinations do make sense.
1562          */
1563         if (fdata->intensity & 0x10) {
1564                 r0_perm = 0;
1565                 intensity = fdata->intensity & ~0x10;
1566         } else {
1567                 r0_perm = 1;
1568                 intensity = fdata->intensity;
1569         }
1570         switch (intensity) {
1571         case 0x00:      /* Normal format */
1572         case 0x08:      /* Normal format, use cdl. */
1573                 cplength = 2 + rpt;
1574                 datasize = sizeof(struct DE_eckd_data) +
1575                         sizeof(struct LO_eckd_data) +
1576                         rpt * sizeof(struct eckd_count);
1577                 break;
1578         case 0x01:      /* Write record zero and format track. */
1579         case 0x09:      /* Write record zero and format track, use cdl. */
1580                 cplength = 3 + rpt;
1581                 datasize = sizeof(struct DE_eckd_data) +
1582                         sizeof(struct LO_eckd_data) +
1583                         sizeof(struct eckd_count) +
1584                         rpt * sizeof(struct eckd_count);
1585                 break;
1586         case 0x04:      /* Invalidate track. */
1587         case 0x0c:      /* Invalidate track, use cdl. */
1588                 cplength = 3;
1589                 datasize = sizeof(struct DE_eckd_data) +
1590                         sizeof(struct LO_eckd_data) +
1591                         sizeof(struct eckd_count);
1592                 break;
1593         default:
1594                 dev_warn(&device->cdev->dev, "An I/O control call used "
1595                          "incorrect flags 0x%x\n", fdata->intensity);
1596                 return ERR_PTR(-EINVAL);
1597         }
1598         /* Allocate the format ccw request. */
1599         fcp = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength, datasize, device);
1600         if (IS_ERR(fcp))
1601                 return fcp;
1602
1603         data = fcp->data;
1604         ccw = fcp->cpaddr;
1605
1606         switch (intensity & ~0x08) {
1607         case 0x00: /* Normal format. */
1608                 define_extent(ccw++, (struct DE_eckd_data *) data,
1609                               fdata->start_unit, fdata->start_unit,
1610                               DASD_ECKD_CCW_WRITE_CKD, device);
1611                 /* grant subsystem permission to format R0 */
1612                 if (r0_perm)
1613                         ((struct DE_eckd_data *)data)->ga_extended |= 0x04;
1614                 data += sizeof(struct DE_eckd_data);
1615                 ccw[-1].flags |= CCW_FLAG_CC;
1616                 locate_record(ccw++, (struct LO_eckd_data *) data,
1617                               fdata->start_unit, 0, rpt,
1618                               DASD_ECKD_CCW_WRITE_CKD, device,
1619                               fdata->blksize);
1620                 data += sizeof(struct LO_eckd_data);
1621                 break;
1622         case 0x01: /* Write record zero + format track. */
1623                 define_extent(ccw++, (struct DE_eckd_data *) data,
1624                               fdata->start_unit, fdata->start_unit,
1625                               DASD_ECKD_CCW_WRITE_RECORD_ZERO,
1626                               device);
1627                 data += sizeof(struct DE_eckd_data);
1628                 ccw[-1].flags |= CCW_FLAG_CC;
1629                 locate_record(ccw++, (struct LO_eckd_data *) data,
1630                               fdata->start_unit, 0, rpt + 1,
1631                               DASD_ECKD_CCW_WRITE_RECORD_ZERO, device,
1632                               device->block->bp_block);
1633                 data += sizeof(struct LO_eckd_data);
1634                 break;
1635         case 0x04: /* Invalidate track. */
1636                 define_extent(ccw++, (struct DE_eckd_data *) data,
1637                               fdata->start_unit, fdata->start_unit,
1638                               DASD_ECKD_CCW_WRITE_CKD, device);
1639                 data += sizeof(struct DE_eckd_data);
1640                 ccw[-1].flags |= CCW_FLAG_CC;
1641                 locate_record(ccw++, (struct LO_eckd_data *) data,
1642                               fdata->start_unit, 0, 1,
1643                               DASD_ECKD_CCW_WRITE_CKD, device, 8);
1644                 data += sizeof(struct LO_eckd_data);
1645                 break;
1646         }
1647         if (intensity & 0x01) { /* write record zero */
1648                 ect = (struct eckd_count *) data;
1649                 data += sizeof(struct eckd_count);
1650                 ect->cyl = address.cyl;
1651                 ect->head = address.head;
1652                 ect->record = 0;
1653                 ect->kl = 0;
1654                 ect->dl = 8;
1655                 ccw[-1].flags |= CCW_FLAG_CC;
1656                 ccw->cmd_code = DASD_ECKD_CCW_WRITE_RECORD_ZERO;
1657                 ccw->flags = CCW_FLAG_SLI;
1658                 ccw->count = 8;
1659                 ccw->cda = (__u32)(addr_t) ect;
1660                 ccw++;
1661         }
1662         if ((intensity & ~0x08) & 0x04) {       /* erase track */
1663                 ect = (struct eckd_count *) data;
1664                 data += sizeof(struct eckd_count);
1665                 ect->cyl = address.cyl;
1666                 ect->head = address.head;
1667                 ect->record = 1;
1668                 ect->kl = 0;
1669                 ect->dl = 0;
1670                 ccw[-1].flags |= CCW_FLAG_CC;
1671                 ccw->cmd_code = DASD_ECKD_CCW_WRITE_CKD;
1672                 ccw->flags = CCW_FLAG_SLI;
1673                 ccw->count = 8;
1674                 ccw->cda = (__u32)(addr_t) ect;
1675         } else {                /* write remaining records */
1676                 for (i = 0; i < rpt; i++) {
1677                         ect = (struct eckd_count *) data;
1678                         data += sizeof(struct eckd_count);
1679                         ect->cyl = address.cyl;
1680                         ect->head = address.head;
1681                         ect->record = i + 1;
1682                         ect->kl = 0;
1683                         ect->dl = fdata->blksize;
1684                         /* Check for special tracks 0-1 when formatting CDL */
1685                         if ((intensity & 0x08) &&
1686                             fdata->start_unit == 0) {
1687                                 if (i < 3) {
1688                                         ect->kl = 4;
1689                                         ect->dl = sizes_trk0[i] - 4;
1690                                 }
1691                         }
1692                         if ((intensity & 0x08) &&
1693                             fdata->start_unit == 1) {
1694                                 ect->kl = 44;
1695                                 ect->dl = LABEL_SIZE - 44;
1696                         }
1697                         ccw[-1].flags |= CCW_FLAG_CC;
1698                         ccw->cmd_code = DASD_ECKD_CCW_WRITE_CKD;
1699                         ccw->flags = CCW_FLAG_SLI;
1700                         ccw->count = 8;
1701                         ccw->cda = (__u32)(addr_t) ect;
1702                         ccw++;
1703                 }
1704         }
1705         fcp->startdev = device;
1706         fcp->memdev = device;
1707         fcp->retries = 256;
1708         fcp->buildclk = get_clock();
1709         fcp->status = DASD_CQR_FILLED;
1710         return fcp;
1711 }
1712
1713 static void dasd_eckd_handle_terminated_request(struct dasd_ccw_req *cqr)
1714 {
1715         cqr->status = DASD_CQR_FILLED;
1716         if (cqr->block && (cqr->startdev != cqr->block->base)) {
1717                 dasd_eckd_reset_ccw_to_base_io(cqr);
1718                 cqr->startdev = cqr->block->base;
1719         }
1720 };
1721
1722 static dasd_erp_fn_t
1723 dasd_eckd_erp_action(struct dasd_ccw_req * cqr)
1724 {
1725         struct dasd_device *device = (struct dasd_device *) cqr->startdev;
1726         struct ccw_device *cdev = device->cdev;
1727
1728         switch (cdev->id.cu_type) {
1729         case 0x3990:
1730         case 0x2105:
1731         case 0x2107:
1732         case 0x1750:
1733                 return dasd_3990_erp_action;
1734         case 0x9343:
1735         case 0x3880:
1736         default:
1737                 return dasd_default_erp_action;
1738         }
1739 }
1740
1741 static dasd_erp_fn_t
1742 dasd_eckd_erp_postaction(struct dasd_ccw_req * cqr)
1743 {
1744         return dasd_default_erp_postaction;
1745 }
1746
1747
1748 static void dasd_eckd_handle_unsolicited_interrupt(struct dasd_device *device,
1749                                                    struct irb *irb)
1750 {
1751         char mask;
1752         char *sense = NULL;
1753         struct dasd_eckd_private *private;
1754
1755         private = (struct dasd_eckd_private *) device->private;
1756         /* first of all check for state change pending interrupt */
1757         mask = DEV_STAT_ATTENTION | DEV_STAT_DEV_END | DEV_STAT_UNIT_EXCEP;
1758         if ((scsw_dstat(&irb->scsw) & mask) == mask) {
1759                 /* for alias only and not in offline processing*/
1760                 if (!device->block && private->lcu &&
1761                     !test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
1762                         /*
1763                          * the state change could be caused by an alias
1764                          * reassignment remove device from alias handling
1765                          * to prevent new requests from being scheduled on
1766                          * the wrong alias device
1767                          */
1768                         dasd_alias_remove_device(device);
1769
1770                         /* schedule worker to reload device */
1771                         dasd_reload_device(device);
1772                 }
1773
1774                 dasd_generic_handle_state_change(device);
1775                 return;
1776         }
1777
1778         /* summary unit check */
1779         sense = dasd_get_sense(irb);
1780         if (sense && (sense[7] == 0x0D) &&
1781             (scsw_dstat(&irb->scsw) & DEV_STAT_UNIT_CHECK)) {
1782                 dasd_alias_handle_summary_unit_check(device, irb);
1783                 return;
1784         }
1785
1786         /* service information message SIM */
1787         if (sense && !(sense[27] & DASD_SENSE_BIT_0) &&
1788             ((sense[6] & DASD_SIM_SENSE) == DASD_SIM_SENSE)) {
1789                 dasd_3990_erp_handle_sim(device, sense);
1790                 dasd_schedule_device_bh(device);
1791                 return;
1792         }
1793
1794         if ((scsw_cc(&irb->scsw) == 1) && !sense &&
1795             (scsw_fctl(&irb->scsw) == SCSW_FCTL_START_FUNC) &&
1796             (scsw_actl(&irb->scsw) == SCSW_ACTL_START_PEND) &&
1797             (scsw_stctl(&irb->scsw) == SCSW_STCTL_STATUS_PEND)) {
1798                 /* fake irb do nothing, they are handled elsewhere */
1799                 dasd_schedule_device_bh(device);
1800                 return;
1801         }
1802
1803         dasd_schedule_device_bh(device);
1804         return;
1805 };
1806
1807
1808 static struct dasd_ccw_req *dasd_eckd_build_cp_cmd_single(
1809                                                struct dasd_device *startdev,
1810                                                struct dasd_block *block,
1811                                                struct request *req,
1812                                                sector_t first_rec,
1813                                                sector_t last_rec,
1814                                                sector_t first_trk,
1815                                                sector_t last_trk,
1816                                                unsigned int first_offs,
1817                                                unsigned int last_offs,
1818                                                unsigned int blk_per_trk,
1819                                                unsigned int blksize)
1820 {
1821         struct dasd_eckd_private *private;
1822         unsigned long *idaws;
1823         struct LO_eckd_data *LO_data;
1824         struct dasd_ccw_req *cqr;
1825         struct ccw1 *ccw;
1826         struct req_iterator iter;
1827         struct bio_vec *bv;
1828         char *dst;
1829         unsigned int off;
1830         int count, cidaw, cplength, datasize;
1831         sector_t recid;
1832         unsigned char cmd, rcmd;
1833         int use_prefix;
1834         struct dasd_device *basedev;
1835
1836         basedev = block->base;
1837         private = (struct dasd_eckd_private *) basedev->private;
1838         if (rq_data_dir(req) == READ)
1839                 cmd = DASD_ECKD_CCW_READ_MT;
1840         else if (rq_data_dir(req) == WRITE)
1841                 cmd = DASD_ECKD_CCW_WRITE_MT;
1842         else
1843                 return ERR_PTR(-EINVAL);
1844
1845         /* Check struct bio and count the number of blocks for the request. */
1846         count = 0;
1847         cidaw = 0;
1848         rq_for_each_segment(bv, req, iter) {
1849                 if (bv->bv_len & (blksize - 1))
1850                         /* Eckd can only do full blocks. */
1851                         return ERR_PTR(-EINVAL);
1852                 count += bv->bv_len >> (block->s2b_shift + 9);
1853 #if defined(CONFIG_64BIT)
1854                 if (idal_is_needed (page_address(bv->bv_page), bv->bv_len))
1855                         cidaw += bv->bv_len >> (block->s2b_shift + 9);
1856 #endif
1857         }
1858         /* Paranoia. */
1859         if (count != last_rec - first_rec + 1)
1860                 return ERR_PTR(-EINVAL);
1861
1862         /* use the prefix command if available */
1863         use_prefix = private->features.feature[8] & 0x01;
1864         if (use_prefix) {
1865                 /* 1x prefix + number of blocks */
1866                 cplength = 2 + count;
1867                 /* 1x prefix + cidaws*sizeof(long) */
1868                 datasize = sizeof(struct PFX_eckd_data) +
1869                         sizeof(struct LO_eckd_data) +
1870                         cidaw * sizeof(unsigned long);
1871         } else {
1872                 /* 1x define extent + 1x locate record + number of blocks */
1873                 cplength = 2 + count;
1874                 /* 1x define extent + 1x locate record + cidaws*sizeof(long) */
1875                 datasize = sizeof(struct DE_eckd_data) +
1876                         sizeof(struct LO_eckd_data) +
1877                         cidaw * sizeof(unsigned long);
1878         }
1879         /* Find out the number of additional locate record ccws for cdl. */
1880         if (private->uses_cdl && first_rec < 2*blk_per_trk) {
1881                 if (last_rec >= 2*blk_per_trk)
1882                         count = 2*blk_per_trk - first_rec;
1883                 cplength += count;
1884                 datasize += count*sizeof(struct LO_eckd_data);
1885         }
1886         /* Allocate the ccw request. */
1887         cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength, datasize,
1888                                    startdev);
1889         if (IS_ERR(cqr))
1890                 return cqr;
1891         ccw = cqr->cpaddr;
1892         /* First ccw is define extent or prefix. */
1893         if (use_prefix) {
1894                 if (prefix(ccw++, cqr->data, first_trk,
1895                            last_trk, cmd, basedev, startdev) == -EAGAIN) {
1896                         /* Clock not in sync and XRC is enabled.
1897                          * Try again later.
1898                          */
1899                         dasd_sfree_request(cqr, startdev);
1900                         return ERR_PTR(-EAGAIN);
1901                 }
1902                 idaws = (unsigned long *) (cqr->data +
1903                                            sizeof(struct PFX_eckd_data));
1904         } else {
1905                 if (define_extent(ccw++, cqr->data, first_trk,
1906                                   last_trk, cmd, startdev) == -EAGAIN) {
1907                         /* Clock not in sync and XRC is enabled.
1908                          * Try again later.
1909                          */
1910                         dasd_sfree_request(cqr, startdev);
1911                         return ERR_PTR(-EAGAIN);
1912                 }
1913                 idaws = (unsigned long *) (cqr->data +
1914                                            sizeof(struct DE_eckd_data));
1915         }
1916         /* Build locate_record+read/write/ccws. */
1917         LO_data = (struct LO_eckd_data *) (idaws + cidaw);
1918         recid = first_rec;
1919         if (private->uses_cdl == 0 || recid > 2*blk_per_trk) {
1920                 /* Only standard blocks so there is just one locate record. */
1921                 ccw[-1].flags |= CCW_FLAG_CC;
1922                 locate_record(ccw++, LO_data++, first_trk, first_offs + 1,
1923                               last_rec - recid + 1, cmd, basedev, blksize);
1924         }
1925         rq_for_each_segment(bv, req, iter) {
1926                 dst = page_address(bv->bv_page) + bv->bv_offset;
1927                 if (dasd_page_cache) {
1928                         char *copy = kmem_cache_alloc(dasd_page_cache,
1929                                                       GFP_DMA | __GFP_NOWARN);
1930                         if (copy && rq_data_dir(req) == WRITE)
1931                                 memcpy(copy + bv->bv_offset, dst, bv->bv_len);
1932                         if (copy)
1933                                 dst = copy + bv->bv_offset;
1934                 }
1935                 for (off = 0; off < bv->bv_len; off += blksize) {
1936                         sector_t trkid = recid;
1937                         unsigned int recoffs = sector_div(trkid, blk_per_trk);
1938                         rcmd = cmd;
1939                         count = blksize;
1940                         /* Locate record for cdl special block ? */
1941                         if (private->uses_cdl && recid < 2*blk_per_trk) {
1942                                 if (dasd_eckd_cdl_special(blk_per_trk, recid)){
1943                                         rcmd |= 0x8;
1944                                         count = dasd_eckd_cdl_reclen(recid);
1945                                         if (count < blksize &&
1946                                             rq_data_dir(req) == READ)
1947                                                 memset(dst + count, 0xe5,
1948                                                        blksize - count);
1949                                 }
1950                                 ccw[-1].flags |= CCW_FLAG_CC;
1951                                 locate_record(ccw++, LO_data++,
1952                                               trkid, recoffs + 1,
1953                                               1, rcmd, basedev, count);
1954                         }
1955                         /* Locate record for standard blocks ? */
1956                         if (private->uses_cdl && recid == 2*blk_per_trk) {
1957                                 ccw[-1].flags |= CCW_FLAG_CC;
1958                                 locate_record(ccw++, LO_data++,
1959                                               trkid, recoffs + 1,
1960                                               last_rec - recid + 1,
1961                                               cmd, basedev, count);
1962                         }
1963                         /* Read/write ccw. */
1964                         ccw[-1].flags |= CCW_FLAG_CC;
1965                         ccw->cmd_code = rcmd;
1966                         ccw->count = count;
1967                         if (idal_is_needed(dst, blksize)) {
1968                                 ccw->cda = (__u32)(addr_t) idaws;
1969                                 ccw->flags = CCW_FLAG_IDA;
1970                                 idaws = idal_create_words(idaws, dst, blksize);
1971                         } else {
1972                                 ccw->cda = (__u32)(addr_t) dst;
1973                                 ccw->flags = 0;
1974                         }
1975                         ccw++;
1976                         dst += blksize;
1977                         recid++;
1978                 }
1979         }
1980         if (blk_noretry_request(req) ||
1981             block->base->features & DASD_FEATURE_FAILFAST)
1982                 set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
1983         cqr->startdev = startdev;
1984         cqr->memdev = startdev;
1985         cqr->block = block;
1986         cqr->expires = startdev->default_expires * HZ;  /* default 5 minutes */
1987         cqr->lpm = private->path_data.ppm;
1988         cqr->retries = 256;
1989         cqr->buildclk = get_clock();
1990         cqr->status = DASD_CQR_FILLED;
1991         return cqr;
1992 }
1993
1994 static struct dasd_ccw_req *dasd_eckd_build_cp_cmd_track(
1995                                                struct dasd_device *startdev,
1996                                                struct dasd_block *block,
1997                                                struct request *req,
1998                                                sector_t first_rec,
1999                                                sector_t last_rec,
2000                                                sector_t first_trk,
2001                                                sector_t last_trk,
2002                                                unsigned int first_offs,
2003                                                unsigned int last_offs,
2004                                                unsigned int blk_per_trk,
2005                                                unsigned int blksize)
2006 {
2007         struct dasd_eckd_private *private;
2008         unsigned long *idaws;
2009         struct dasd_ccw_req *cqr;
2010         struct ccw1 *ccw;
2011         struct req_iterator iter;
2012         struct bio_vec *bv;
2013         char *dst, *idaw_dst;
2014         unsigned int cidaw, cplength, datasize;
2015         unsigned int tlf;
2016         sector_t recid;
2017         unsigned char cmd;
2018         struct dasd_device *basedev;
2019         unsigned int trkcount, count, count_to_trk_end;
2020         unsigned int idaw_len, seg_len, part_len, len_to_track_end;
2021         unsigned char new_track, end_idaw;
2022         sector_t trkid;
2023         unsigned int recoffs;
2024
2025         basedev = block->base;
2026         private = (struct dasd_eckd_private *) basedev->private;
2027         if (rq_data_dir(req) == READ)
2028                 cmd = DASD_ECKD_CCW_READ_TRACK_DATA;
2029         else if (rq_data_dir(req) == WRITE)
2030                 cmd = DASD_ECKD_CCW_WRITE_TRACK_DATA;
2031         else
2032                 return ERR_PTR(-EINVAL);
2033
2034         /* Track based I/O needs IDAWs for each page, and not just for
2035          * 64 bit addresses. We need additional idals for pages
2036          * that get filled from two tracks, so we use the number
2037          * of records as upper limit.
2038          */
2039         cidaw = last_rec - first_rec + 1;
2040         trkcount = last_trk - first_trk + 1;
2041
2042         /* 1x prefix + one read/write ccw per track */
2043         cplength = 1 + trkcount;
2044
2045         /* on 31-bit we need space for two 32 bit addresses per page
2046          * on 64-bit one 64 bit address
2047          */
2048         datasize = sizeof(struct PFX_eckd_data) +
2049                 cidaw * sizeof(unsigned long long);
2050
2051         /* Allocate the ccw request. */
2052         cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength, datasize,
2053                                    startdev);
2054         if (IS_ERR(cqr))
2055                 return cqr;
2056         ccw = cqr->cpaddr;
2057         /* transfer length factor: how many bytes to read from the last track */
2058         if (first_trk == last_trk)
2059                 tlf = last_offs - first_offs + 1;
2060         else
2061                 tlf = last_offs + 1;
2062         tlf *= blksize;
2063
2064         if (prefix_LRE(ccw++, cqr->data, first_trk,
2065                        last_trk, cmd, basedev, startdev,
2066                        1 /* format */, first_offs + 1,
2067                        trkcount, blksize,
2068                        tlf) == -EAGAIN) {
2069                 /* Clock not in sync and XRC is enabled.
2070                  * Try again later.
2071                  */
2072                 dasd_sfree_request(cqr, startdev);
2073                 return ERR_PTR(-EAGAIN);
2074         }
2075
2076         /*
2077          * The translation of request into ccw programs must meet the
2078          * following conditions:
2079          * - all idaws but the first and the last must address full pages
2080          *   (or 2K blocks on 31-bit)
2081          * - the scope of a ccw and it's idal ends with the track boundaries
2082          */
2083         idaws = (unsigned long *) (cqr->data + sizeof(struct PFX_eckd_data));
2084         recid = first_rec;
2085         new_track = 1;
2086         end_idaw = 0;
2087         len_to_track_end = 0;
2088         idaw_dst = 0;
2089         idaw_len = 0;
2090         rq_for_each_segment(bv, req, iter) {
2091                 dst = page_address(bv->bv_page) + bv->bv_offset;
2092                 seg_len = bv->bv_len;
2093                 while (seg_len) {
2094                         if (new_track) {
2095                                 trkid = recid;
2096                                 recoffs = sector_div(trkid, blk_per_trk);
2097                                 count_to_trk_end = blk_per_trk - recoffs;
2098                                 count = min((last_rec - recid + 1),
2099                                             (sector_t)count_to_trk_end);
2100                                 len_to_track_end = count * blksize;
2101                                 ccw[-1].flags |= CCW_FLAG_CC;
2102                                 ccw->cmd_code = cmd;
2103                                 ccw->count = len_to_track_end;
2104                                 ccw->cda = (__u32)(addr_t)idaws;
2105                                 ccw->flags = CCW_FLAG_IDA;
2106                                 ccw++;
2107                                 recid += count;
2108                                 new_track = 0;
2109                                 /* first idaw for a ccw may start anywhere */
2110                                 if (!idaw_dst)
2111                                         idaw_dst = dst;
2112                         }
2113                         /* If we start a new idaw, we must make sure that it
2114                          * starts on an IDA_BLOCK_SIZE boundary.
2115                          * If we continue an idaw, we must make sure that the
2116                          * current segment begins where the so far accumulated
2117                          * idaw ends
2118                          */
2119                         if (!idaw_dst) {
2120                                 if (__pa(dst) & (IDA_BLOCK_SIZE-1)) {
2121                                         dasd_sfree_request(cqr, startdev);
2122                                         return ERR_PTR(-ERANGE);
2123                                 } else
2124                                         idaw_dst = dst;
2125                         }
2126                         if ((idaw_dst + idaw_len) != dst) {
2127                                 dasd_sfree_request(cqr, startdev);
2128                                 return ERR_PTR(-ERANGE);
2129                         }
2130                         part_len = min(seg_len, len_to_track_end);
2131                         seg_len -= part_len;
2132                         dst += part_len;
2133                         idaw_len += part_len;
2134                         len_to_track_end -= part_len;
2135                         /* collected memory area ends on an IDA_BLOCK border,
2136                          * -> create an idaw
2137                          * idal_create_words will handle cases where idaw_len
2138                          * is larger then IDA_BLOCK_SIZE
2139                          */
2140                         if (!(__pa(idaw_dst + idaw_len) & (IDA_BLOCK_SIZE-1)))
2141                                 end_idaw = 1;
2142                         /* We also need to end the idaw at track end */
2143                         if (!len_to_track_end) {
2144                                 new_track = 1;
2145                                 end_idaw = 1;
2146                         }
2147                         if (end_idaw) {
2148                                 idaws = idal_create_words(idaws, idaw_dst,
2149                                                           idaw_len);
2150                                 idaw_dst = 0;
2151                                 idaw_len = 0;
2152                                 end_idaw = 0;
2153                         }
2154                 }
2155         }
2156
2157         if (blk_noretry_request(req) ||
2158             block->base->features & DASD_FEATURE_FAILFAST)
2159                 set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
2160         cqr->startdev = startdev;
2161         cqr->memdev = startdev;
2162         cqr->block = block;
2163         cqr->expires = startdev->default_expires * HZ;  /* default 5 minutes */
2164         cqr->lpm = private->path_data.ppm;
2165         cqr->retries = 256;
2166         cqr->buildclk = get_clock();
2167         cqr->status = DASD_CQR_FILLED;
2168         return cqr;
2169 }
2170
2171 static int prepare_itcw(struct itcw *itcw,
2172                         unsigned int trk, unsigned int totrk, int cmd,
2173                         struct dasd_device *basedev,
2174                         struct dasd_device *startdev,
2175                         unsigned int rec_on_trk, int count,
2176                         unsigned int blksize,
2177                         unsigned int total_data_size,
2178                         unsigned int tlf,
2179                         unsigned int blk_per_trk)
2180 {
2181         struct PFX_eckd_data pfxdata;
2182         struct dasd_eckd_private *basepriv, *startpriv;
2183         struct DE_eckd_data *dedata;
2184         struct LRE_eckd_data *lredata;
2185         struct dcw *dcw;
2186
2187         u32 begcyl, endcyl;
2188         u16 heads, beghead, endhead;
2189         u8 pfx_cmd;
2190
2191         int rc = 0;
2192         int sector = 0;
2193         int dn, d;
2194
2195
2196         /* setup prefix data */
2197         basepriv = (struct dasd_eckd_private *) basedev->private;
2198         startpriv = (struct dasd_eckd_private *) startdev->private;
2199         dedata = &pfxdata.define_extent;
2200         lredata = &pfxdata.locate_record;
2201
2202         memset(&pfxdata, 0, sizeof(pfxdata));
2203         pfxdata.format = 1; /* PFX with LRE */
2204         pfxdata.base_address = basepriv->ned->unit_addr;
2205         pfxdata.base_lss = basepriv->ned->ID;
2206         pfxdata.validity.define_extent = 1;
2207
2208         /* private uid is kept up to date, conf_data may be outdated */
2209         if (startpriv->uid.type != UA_BASE_DEVICE) {
2210                 pfxdata.validity.verify_base = 1;
2211                 if (startpriv->uid.type == UA_HYPER_PAV_ALIAS)
2212                         pfxdata.validity.hyper_pav = 1;
2213         }
2214
2215         switch (cmd) {
2216         case DASD_ECKD_CCW_READ_TRACK_DATA:
2217                 dedata->mask.perm = 0x1;
2218                 dedata->attributes.operation = basepriv->attrib.operation;
2219                 dedata->blk_size = blksize;
2220                 dedata->ga_extended |= 0x42;
2221                 lredata->operation.orientation = 0x0;
2222                 lredata->operation.operation = 0x0C;
2223                 lredata->auxiliary.check_bytes = 0x01;
2224                 pfx_cmd = DASD_ECKD_CCW_PFX_READ;
2225                 break;
2226         case DASD_ECKD_CCW_WRITE_TRACK_DATA:
2227                 dedata->mask.perm = 0x02;
2228                 dedata->attributes.operation = basepriv->attrib.operation;
2229                 dedata->blk_size = blksize;
2230                 rc = check_XRC_on_prefix(&pfxdata, basedev);
2231                 dedata->ga_extended |= 0x42;
2232                 lredata->operation.orientation = 0x0;
2233                 lredata->operation.operation = 0x3F;
2234                 lredata->extended_operation = 0x23;
2235                 lredata->auxiliary.check_bytes = 0x2;
2236                 pfx_cmd = DASD_ECKD_CCW_PFX;
2237                 break;
2238         default:
2239                 DBF_DEV_EVENT(DBF_ERR, basedev,
2240                               "prepare itcw, unknown opcode 0x%x", cmd);
2241                 BUG();
2242                 break;
2243         }
2244         if (rc)
2245                 return rc;
2246
2247         dedata->attributes.mode = 0x3;  /* ECKD */
2248
2249         heads = basepriv->rdc_data.trk_per_cyl;
2250         begcyl = trk / heads;
2251         beghead = trk % heads;
2252         endcyl = totrk / heads;
2253         endhead = totrk % heads;
2254
2255         /* check for sequential prestage - enhance cylinder range */
2256         if (dedata->attributes.operation == DASD_SEQ_PRESTAGE ||
2257             dedata->attributes.operation == DASD_SEQ_ACCESS) {
2258
2259                 if (endcyl + basepriv->attrib.nr_cyl < basepriv->real_cyl)
2260                         endcyl += basepriv->attrib.nr_cyl;
2261                 else
2262                         endcyl = (basepriv->real_cyl - 1);
2263         }
2264
2265         set_ch_t(&dedata->beg_ext, begcyl, beghead);
2266         set_ch_t(&dedata->end_ext, endcyl, endhead);
2267
2268         dedata->ep_format = 0x20; /* records per track is valid */
2269         dedata->ep_rec_per_track = blk_per_trk;
2270
2271         if (rec_on_trk) {
2272                 switch (basepriv->rdc_data.dev_type) {
2273                 case 0x3390:
2274                         dn = ceil_quot(blksize + 6, 232);
2275                         d = 9 + ceil_quot(blksize + 6 * (dn + 1), 34);
2276                         sector = (49 + (rec_on_trk - 1) * (10 + d)) / 8;
2277                         break;
2278                 case 0x3380:
2279                         d = 7 + ceil_quot(blksize + 12, 32);
2280                         sector = (39 + (rec_on_trk - 1) * (8 + d)) / 7;
2281                         break;
2282                 }
2283         }
2284
2285         lredata->auxiliary.length_valid = 1;
2286         lredata->auxiliary.length_scope = 1;
2287         lredata->auxiliary.imbedded_ccw_valid = 1;
2288         lredata->length = tlf;
2289         lredata->imbedded_ccw = cmd;
2290         lredata->count = count;
2291         lredata->sector = sector;
2292         set_ch_t(&lredata->seek_addr, begcyl, beghead);
2293         lredata->search_arg.cyl = lredata->seek_addr.cyl;
2294         lredata->search_arg.head = lredata->seek_addr.head;
2295         lredata->search_arg.record = rec_on_trk;
2296
2297         dcw = itcw_add_dcw(itcw, pfx_cmd, 0,
2298                      &pfxdata, sizeof(pfxdata), total_data_size);
2299
2300         return rc;
2301 }
2302
2303 static struct dasd_ccw_req *dasd_eckd_build_cp_tpm_track(
2304                                                struct dasd_device *startdev,
2305                                                struct dasd_block *block,
2306                                                struct request *req,
2307                                                sector_t first_rec,
2308                                                sector_t last_rec,
2309                                                sector_t first_trk,
2310                                                sector_t last_trk,
2311                                                unsigned int first_offs,
2312                                                unsigned int last_offs,
2313                                                unsigned int blk_per_trk,
2314                                                unsigned int blksize)
2315 {
2316         struct dasd_eckd_private *private;
2317         struct dasd_ccw_req *cqr;
2318         struct req_iterator iter;
2319         struct bio_vec *bv;
2320         char *dst;
2321         unsigned int trkcount, ctidaw;
2322         unsigned char cmd;
2323         struct dasd_device *basedev;
2324         unsigned int tlf;
2325         struct itcw *itcw;
2326         struct tidaw *last_tidaw = NULL;
2327         int itcw_op;
2328         size_t itcw_size;
2329
2330         basedev = block->base;
2331         private = (struct dasd_eckd_private *) basedev->private;
2332         if (rq_data_dir(req) == READ) {
2333                 cmd = DASD_ECKD_CCW_READ_TRACK_DATA;
2334                 itcw_op = ITCW_OP_READ;
2335         } else if (rq_data_dir(req) == WRITE) {
2336                 cmd = DASD_ECKD_CCW_WRITE_TRACK_DATA;
2337                 itcw_op = ITCW_OP_WRITE;
2338         } else
2339                 return ERR_PTR(-EINVAL);
2340
2341         /* trackbased I/O needs address all memory via TIDAWs,
2342          * not just for 64 bit addresses. This allows us to map
2343          * each segment directly to one tidaw.
2344          */
2345         trkcount = last_trk - first_trk + 1;
2346         ctidaw = 0;
2347         rq_for_each_segment(bv, req, iter) {
2348                 ++ctidaw;
2349         }
2350
2351         /* Allocate the ccw request. */
2352         itcw_size = itcw_calc_size(0, ctidaw, 0);
2353         cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 0, itcw_size, startdev);
2354         if (IS_ERR(cqr))
2355                 return cqr;
2356
2357         cqr->cpmode = 1;
2358         cqr->startdev = startdev;
2359         cqr->memdev = startdev;
2360         cqr->block = block;
2361         cqr->expires = 100*HZ;
2362         cqr->buildclk = get_clock();
2363         cqr->status = DASD_CQR_FILLED;
2364         cqr->retries = 10;
2365
2366         /* transfer length factor: how many bytes to read from the last track */
2367         if (first_trk == last_trk)
2368                 tlf = last_offs - first_offs + 1;
2369         else
2370                 tlf = last_offs + 1;
2371         tlf *= blksize;
2372
2373         itcw = itcw_init(cqr->data, itcw_size, itcw_op, 0, ctidaw, 0);
2374         cqr->cpaddr = itcw_get_tcw(itcw);
2375
2376         if (prepare_itcw(itcw, first_trk, last_trk,
2377                          cmd, basedev, startdev,
2378                          first_offs + 1,
2379                          trkcount, blksize,
2380                          (last_rec - first_rec + 1) * blksize,
2381                          tlf, blk_per_trk) == -EAGAIN) {
2382                 /* Clock not in sync and XRC is enabled.
2383                  * Try again later.
2384                  */
2385                 dasd_sfree_request(cqr, startdev);
2386                 return ERR_PTR(-EAGAIN);
2387         }
2388
2389         /*
2390          * A tidaw can address 4k of memory, but must not cross page boundaries
2391          * We can let the block layer handle this by setting
2392          * blk_queue_segment_boundary to page boundaries and
2393          * blk_max_segment_size to page size when setting up the request queue.
2394          */
2395         rq_for_each_segment(bv, req, iter) {
2396                 dst = page_address(bv->bv_page) + bv->bv_offset;
2397                 last_tidaw = itcw_add_tidaw(itcw, 0x00, dst, bv->bv_len);
2398                 if (IS_ERR(last_tidaw))
2399                         return (struct dasd_ccw_req *)last_tidaw;
2400         }
2401
2402         last_tidaw->flags |= 0x80;
2403         itcw_finalize(itcw);
2404
2405         if (blk_noretry_request(req) ||
2406             block->base->features & DASD_FEATURE_FAILFAST)
2407                 set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
2408         cqr->startdev = startdev;
2409         cqr->memdev = startdev;
2410         cqr->block = block;
2411         cqr->expires = startdev->default_expires * HZ;  /* default 5 minutes */
2412         cqr->lpm = private->path_data.ppm;
2413         cqr->retries = 256;
2414         cqr->buildclk = get_clock();
2415         cqr->status = DASD_CQR_FILLED;
2416         return cqr;
2417 }
2418
2419 static struct dasd_ccw_req *dasd_eckd_build_cp(struct dasd_device *startdev,
2420                                                struct dasd_block *block,
2421                                                struct request *req)
2422 {
2423         int tpm, cmdrtd, cmdwtd;
2424         int use_prefix;
2425 #if defined(CONFIG_64BIT)
2426         int fcx_in_css, fcx_in_gneq, fcx_in_features;
2427 #endif
2428         struct dasd_eckd_private *private;
2429         struct dasd_device *basedev;
2430         sector_t first_rec, last_rec;
2431         sector_t first_trk, last_trk;
2432         unsigned int first_offs, last_offs;
2433         unsigned int blk_per_trk, blksize;
2434         int cdlspecial;
2435         struct dasd_ccw_req *cqr;
2436
2437         basedev = block->base;
2438         private = (struct dasd_eckd_private *) basedev->private;
2439
2440         /* Calculate number of blocks/records per track. */
2441         blksize = block->bp_block;
2442         blk_per_trk = recs_per_track(&private->rdc_data, 0, blksize);
2443         if (blk_per_trk == 0)
2444                 return ERR_PTR(-EINVAL);
2445         /* Calculate record id of first and last block. */
2446         first_rec = first_trk = blk_rq_pos(req) >> block->s2b_shift;
2447         first_offs = sector_div(first_trk, blk_per_trk);
2448         last_rec = last_trk =
2449                 (blk_rq_pos(req) + blk_rq_sectors(req) - 1) >> block->s2b_shift;
2450         last_offs = sector_div(last_trk, blk_per_trk);
2451         cdlspecial = (private->uses_cdl && first_rec < 2*blk_per_trk);
2452
2453         /* is transport mode supported? */
2454 #if defined(CONFIG_64BIT)
2455         fcx_in_css = css_general_characteristics.fcx;
2456         fcx_in_gneq = private->gneq->reserved2[7] & 0x04;
2457         fcx_in_features = private->features.feature[40] & 0x80;
2458         tpm = fcx_in_css && fcx_in_gneq && fcx_in_features;
2459 #else
2460         tpm = 0;
2461 #endif
2462
2463         /* is read track data and write track data in command mode supported? */
2464         cmdrtd = private->features.feature[9] & 0x20;
2465         cmdwtd = private->features.feature[12] & 0x40;
2466         use_prefix = private->features.feature[8] & 0x01;
2467
2468         cqr = NULL;
2469         if (cdlspecial || dasd_page_cache) {
2470                 /* do nothing, just fall through to the cmd mode single case */
2471         } else if (!dasd_nofcx && tpm && (first_trk == last_trk)) {
2472                 cqr = dasd_eckd_build_cp_tpm_track(startdev, block, req,
2473                                                     first_rec, last_rec,
2474                                                     first_trk, last_trk,
2475                                                     first_offs, last_offs,
2476                                                     blk_per_trk, blksize);
2477                 if (IS_ERR(cqr) && PTR_ERR(cqr) != -EAGAIN)
2478                         cqr = NULL;
2479         } else if (use_prefix &&
2480                    (((rq_data_dir(req) == READ) && cmdrtd) ||
2481                     ((rq_data_dir(req) == WRITE) && cmdwtd))) {
2482                 cqr = dasd_eckd_build_cp_cmd_track(startdev, block, req,
2483                                                    first_rec, last_rec,
2484                                                    first_trk, last_trk,
2485                                                    first_offs, last_offs,
2486                                                    blk_per_trk, blksize);
2487                 if (IS_ERR(cqr) && PTR_ERR(cqr) != -EAGAIN)
2488                         cqr = NULL;
2489         }
2490         if (!cqr)
2491                 cqr = dasd_eckd_build_cp_cmd_single(startdev, block, req,
2492                                                     first_rec, last_rec,
2493                                                     first_trk, last_trk,
2494                                                     first_offs, last_offs,
2495                                                     blk_per_trk, blksize);
2496         return cqr;
2497 }
2498
2499 static int
2500 dasd_eckd_free_cp(struct dasd_ccw_req *cqr, struct request *req)
2501 {
2502         struct dasd_eckd_private *private;
2503         struct ccw1 *ccw;
2504         struct req_iterator iter;
2505         struct bio_vec *bv;
2506         char *dst, *cda;
2507         unsigned int blksize, blk_per_trk, off;
2508         sector_t recid;
2509         int status;
2510
2511         if (!dasd_page_cache)
2512                 goto out;
2513         private = (struct dasd_eckd_private *) cqr->block->base->private;
2514         blksize = cqr->block->bp_block;
2515         blk_per_trk = recs_per_track(&private->rdc_data, 0, blksize);
2516         recid = blk_rq_pos(req) >> cqr->block->s2b_shift;
2517         ccw = cqr->cpaddr;
2518         /* Skip over define extent & locate record. */
2519         ccw++;
2520         if (private->uses_cdl == 0 || recid > 2*blk_per_trk)
2521                 ccw++;
2522         rq_for_each_segment(bv, req, iter) {
2523                 dst = page_address(bv->bv_page) + bv->bv_offset;
2524                 for (off = 0; off < bv->bv_len; off += blksize) {
2525                         /* Skip locate record. */
2526                         if (private->uses_cdl && recid <= 2*blk_per_trk)
2527                                 ccw++;
2528                         if (dst) {
2529                                 if (ccw->flags & CCW_FLAG_IDA)
2530                                         cda = *((char **)((addr_t) ccw->cda));
2531                                 else
2532                                         cda = (char *)((addr_t) ccw->cda);
2533                                 if (dst != cda) {
2534                                         if (rq_data_dir(req) == READ)
2535                                                 memcpy(dst, cda, bv->bv_len);
2536                                         kmem_cache_free(dasd_page_cache,
2537                                             (void *)((addr_t)cda & PAGE_MASK));
2538                                 }
2539                                 dst = NULL;
2540                         }
2541                         ccw++;
2542                         recid++;
2543                 }
2544         }
2545 out:
2546         status = cqr->status == DASD_CQR_DONE;
2547         dasd_sfree_request(cqr, cqr->memdev);
2548         return status;
2549 }
2550
2551 /*
2552  * Modify ccw/tcw in cqr so it can be started on a base device.
2553  *
2554  * Note that this is not enough to restart the cqr!
2555  * Either reset cqr->startdev as well (summary unit check handling)
2556  * or restart via separate cqr (as in ERP handling).
2557  */
2558 void dasd_eckd_reset_ccw_to_base_io(struct dasd_ccw_req *cqr)
2559 {
2560         struct ccw1 *ccw;
2561         struct PFX_eckd_data *pfxdata;
2562         struct tcw *tcw;
2563         struct tccb *tccb;
2564         struct dcw *dcw;
2565
2566         if (cqr->cpmode == 1) {
2567                 tcw = cqr->cpaddr;
2568                 tccb = tcw_get_tccb(tcw);
2569                 dcw = (struct dcw *)&tccb->tca[0];
2570                 pfxdata = (struct PFX_eckd_data *)&dcw->cd[0];
2571                 pfxdata->validity.verify_base = 0;
2572                 pfxdata->validity.hyper_pav = 0;
2573         } else {
2574                 ccw = cqr->cpaddr;
2575                 pfxdata = cqr->data;
2576                 if (ccw->cmd_code == DASD_ECKD_CCW_PFX) {
2577                         pfxdata->validity.verify_base = 0;
2578                         pfxdata->validity.hyper_pav = 0;
2579                 }
2580         }
2581 }
2582
2583 #define DASD_ECKD_CHANQ_MAX_SIZE 4
2584
2585 static struct dasd_ccw_req *dasd_eckd_build_alias_cp(struct dasd_device *base,
2586                                                      struct dasd_block *block,
2587                                                      struct request *req)
2588 {
2589         struct dasd_eckd_private *private;
2590         struct dasd_device *startdev;
2591         unsigned long flags;
2592         struct dasd_ccw_req *cqr;
2593
2594         startdev = dasd_alias_get_start_dev(base);
2595         if (!startdev)
2596                 startdev = base;
2597         private = (struct dasd_eckd_private *) startdev->private;
2598         if (private->count >= DASD_ECKD_CHANQ_MAX_SIZE)
2599                 return ERR_PTR(-EBUSY);
2600
2601         spin_lock_irqsave(get_ccwdev_lock(startdev->cdev), flags);
2602         private->count++;
2603         cqr = dasd_eckd_build_cp(startdev, block, req);
2604         if (IS_ERR(cqr))
2605                 private->count--;
2606         spin_unlock_irqrestore(get_ccwdev_lock(startdev->cdev), flags);
2607         return cqr;
2608 }
2609
2610 static int dasd_eckd_free_alias_cp(struct dasd_ccw_req *cqr,
2611                                    struct request *req)
2612 {
2613         struct dasd_eckd_private *private;
2614         unsigned long flags;
2615
2616         spin_lock_irqsave(get_ccwdev_lock(cqr->memdev->cdev), flags);
2617         private = (struct dasd_eckd_private *) cqr->memdev->private;
2618         private->count--;
2619         spin_unlock_irqrestore(get_ccwdev_lock(cqr->memdev->cdev), flags);
2620         return dasd_eckd_free_cp(cqr, req);
2621 }
2622
2623 static int
2624 dasd_eckd_fill_info(struct dasd_device * device,
2625                     struct dasd_information2_t * info)
2626 {
2627         struct dasd_eckd_private *private;
2628
2629         private = (struct dasd_eckd_private *) device->private;
2630         info->label_block = 2;
2631         info->FBA_layout = private->uses_cdl ? 0 : 1;
2632         info->format = private->uses_cdl ? DASD_FORMAT_CDL : DASD_FORMAT_LDL;
2633         info->characteristics_size = sizeof(struct dasd_eckd_characteristics);
2634         memcpy(info->characteristics, &private->rdc_data,
2635                sizeof(struct dasd_eckd_characteristics));
2636         info->confdata_size = min((unsigned long)private->conf_len,
2637                                   sizeof(info->configuration_data));
2638         memcpy(info->configuration_data, private->conf_data,
2639                info->confdata_size);
2640         return 0;
2641 }
2642
2643 /*
2644  * SECTION: ioctl functions for eckd devices.
2645  */
2646
2647 /*
2648  * Release device ioctl.
2649  * Buils a channel programm to releases a prior reserved
2650  * (see dasd_eckd_reserve) device.
2651  */
2652 static int
2653 dasd_eckd_release(struct dasd_device *device)
2654 {
2655         struct dasd_ccw_req *cqr;
2656         int rc;
2657         struct ccw1 *ccw;
2658         int useglobal;
2659
2660         if (!capable(CAP_SYS_ADMIN))
2661                 return -EACCES;
2662
2663         useglobal = 0;
2664         cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1, 32, device);
2665         if (IS_ERR(cqr)) {
2666                 mutex_lock(&dasd_reserve_mutex);
2667                 useglobal = 1;
2668                 cqr = &dasd_reserve_req->cqr;
2669                 memset(cqr, 0, sizeof(*cqr));
2670                 memset(&dasd_reserve_req->ccw, 0,
2671                        sizeof(dasd_reserve_req->ccw));
2672                 cqr->cpaddr = &dasd_reserve_req->ccw;
2673                 cqr->data = &dasd_reserve_req->data;
2674                 cqr->magic = DASD_ECKD_MAGIC;
2675         }
2676         ccw = cqr->cpaddr;
2677         ccw->cmd_code = DASD_ECKD_CCW_RELEASE;
2678         ccw->flags |= CCW_FLAG_SLI;
2679         ccw->count = 32;
2680         ccw->cda = (__u32)(addr_t) cqr->data;
2681         cqr->startdev = device;
2682         cqr->memdev = device;
2683         clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
2684         set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
2685         cqr->retries = 2;       /* set retry counter to enable basic ERP */
2686         cqr->expires = 2 * HZ;
2687         cqr->buildclk = get_clock();
2688         cqr->status = DASD_CQR_FILLED;
2689
2690         rc = dasd_sleep_on_immediatly(cqr);
2691
2692         if (useglobal)
2693                 mutex_unlock(&dasd_reserve_mutex);
2694         else
2695                 dasd_sfree_request(cqr, cqr->memdev);
2696         return rc;
2697 }
2698
2699 /*
2700  * Reserve device ioctl.
2701  * Options are set to 'synchronous wait for interrupt' and
2702  * 'timeout the request'. This leads to a terminate IO if
2703  * the interrupt is outstanding for a certain time.
2704  */
2705 static int
2706 dasd_eckd_reserve(struct dasd_device *device)
2707 {
2708         struct dasd_ccw_req *cqr;
2709         int rc;
2710         struct ccw1 *ccw;
2711         int useglobal;
2712
2713         if (!capable(CAP_SYS_ADMIN))
2714                 return -EACCES;
2715
2716         useglobal = 0;
2717         cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1, 32, device);
2718         if (IS_ERR(cqr)) {
2719                 mutex_lock(&dasd_reserve_mutex);
2720                 useglobal = 1;
2721                 cqr = &dasd_reserve_req->cqr;
2722                 memset(cqr, 0, sizeof(*cqr));
2723                 memset(&dasd_reserve_req->ccw, 0,
2724                        sizeof(dasd_reserve_req->ccw));
2725                 cqr->cpaddr = &dasd_reserve_req->ccw;
2726                 cqr->data = &dasd_reserve_req->data;
2727                 cqr->magic = DASD_ECKD_MAGIC;
2728         }
2729         ccw = cqr->cpaddr;
2730         ccw->cmd_code = DASD_ECKD_CCW_RESERVE;
2731         ccw->flags |= CCW_FLAG_SLI;
2732         ccw->count = 32;
2733         ccw->cda = (__u32)(addr_t) cqr->data;
2734         cqr->startdev = device;
2735         cqr->memdev = device;
2736         clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
2737         set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
2738         cqr->retries = 2;       /* set retry counter to enable basic ERP */
2739         cqr->expires = 2 * HZ;
2740         cqr->buildclk = get_clock();
2741         cqr->status = DASD_CQR_FILLED;
2742
2743         rc = dasd_sleep_on_immediatly(cqr);
2744
2745         if (useglobal)
2746                 mutex_unlock(&dasd_reserve_mutex);
2747         else
2748                 dasd_sfree_request(cqr, cqr->memdev);
2749         return rc;
2750 }
2751
2752 /*
2753  * Steal lock ioctl - unconditional reserve device.
2754  * Buils a channel programm to break a device's reservation.
2755  * (unconditional reserve)
2756  */
2757 static int
2758 dasd_eckd_steal_lock(struct dasd_device *device)
2759 {
2760         struct dasd_ccw_req *cqr;
2761         int rc;
2762         struct ccw1 *ccw;
2763         int useglobal;
2764
2765         if (!capable(CAP_SYS_ADMIN))
2766                 return -EACCES;
2767
2768         useglobal = 0;
2769         cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1, 32, device);
2770         if (IS_ERR(cqr)) {
2771                 mutex_lock(&dasd_reserve_mutex);
2772                 useglobal = 1;
2773                 cqr = &dasd_reserve_req->cqr;
2774                 memset(cqr, 0, sizeof(*cqr));
2775                 memset(&dasd_reserve_req->ccw, 0,
2776                        sizeof(dasd_reserve_req->ccw));
2777                 cqr->cpaddr = &dasd_reserve_req->ccw;
2778                 cqr->data = &dasd_reserve_req->data;
2779                 cqr->magic = DASD_ECKD_MAGIC;
2780         }
2781         ccw = cqr->cpaddr;
2782         ccw->cmd_code = DASD_ECKD_CCW_SLCK;
2783         ccw->flags |= CCW_FLAG_SLI;
2784         ccw->count = 32;
2785         ccw->cda = (__u32)(addr_t) cqr->data;
2786         cqr->startdev = device;
2787         cqr->memdev = device;
2788         clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
2789         set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
2790         cqr->retries = 2;       /* set retry counter to enable basic ERP */
2791         cqr->expires = 2 * HZ;
2792         cqr->buildclk = get_clock();
2793         cqr->status = DASD_CQR_FILLED;
2794
2795         rc = dasd_sleep_on_immediatly(cqr);
2796
2797         if (useglobal)
2798                 mutex_unlock(&dasd_reserve_mutex);
2799         else
2800                 dasd_sfree_request(cqr, cqr->memdev);
2801         return rc;
2802 }
2803
2804 /*
2805  * Read performance statistics
2806  */
2807 static int
2808 dasd_eckd_performance(struct dasd_device *device, void __user *argp)
2809 {
2810         struct dasd_psf_prssd_data *prssdp;
2811         struct dasd_rssd_perf_stats_t *stats;
2812         struct dasd_ccw_req *cqr;
2813         struct ccw1 *ccw;
2814         int rc;
2815
2816         cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */  + 1 /* RSSD */,
2817                                    (sizeof(struct dasd_psf_prssd_data) +
2818                                     sizeof(struct dasd_rssd_perf_stats_t)),
2819                                    device);
2820         if (IS_ERR(cqr)) {
2821                 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
2822                             "Could not allocate initialization request");
2823                 return PTR_ERR(cqr);
2824         }
2825         cqr->startdev = device;
2826         cqr->memdev = device;
2827         cqr->retries = 0;
2828         clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
2829         cqr->expires = 10 * HZ;
2830
2831         /* Prepare for Read Subsystem Data */
2832         prssdp = (struct dasd_psf_prssd_data *) cqr->data;
2833         memset(prssdp, 0, sizeof(struct dasd_psf_prssd_data));
2834         prssdp->order = PSF_ORDER_PRSSD;
2835         prssdp->suborder = 0x01;        /* Performance Statistics */
2836         prssdp->varies[1] = 0x01;       /* Perf Statistics for the Subsystem */
2837
2838         ccw = cqr->cpaddr;
2839         ccw->cmd_code = DASD_ECKD_CCW_PSF;
2840         ccw->count = sizeof(struct dasd_psf_prssd_data);
2841         ccw->flags |= CCW_FLAG_CC;
2842         ccw->cda = (__u32)(addr_t) prssdp;
2843
2844         /* Read Subsystem Data - Performance Statistics */
2845         stats = (struct dasd_rssd_perf_stats_t *) (prssdp + 1);
2846         memset(stats, 0, sizeof(struct dasd_rssd_perf_stats_t));
2847
2848         ccw++;
2849         ccw->cmd_code = DASD_ECKD_CCW_RSSD;
2850         ccw->count = sizeof(struct dasd_rssd_perf_stats_t);
2851         ccw->cda = (__u32)(addr_t) stats;
2852
2853         cqr->buildclk = get_clock();
2854         cqr->status = DASD_CQR_FILLED;
2855         rc = dasd_sleep_on(cqr);
2856         if (rc == 0) {
2857                 prssdp = (struct dasd_psf_prssd_data *) cqr->data;
2858                 stats = (struct dasd_rssd_perf_stats_t *) (prssdp + 1);
2859                 if (copy_to_user(argp, stats,
2860                                  sizeof(struct dasd_rssd_perf_stats_t)))
2861                         rc = -EFAULT;
2862         }
2863         dasd_sfree_request(cqr, cqr->memdev);
2864         return rc;
2865 }
2866
2867 /*
2868  * Get attributes (cache operations)
2869  * Returnes the cache attributes used in Define Extend (DE).
2870  */
2871 static int
2872 dasd_eckd_get_attrib(struct dasd_device *device, void __user *argp)
2873 {
2874         struct dasd_eckd_private *private =
2875                 (struct dasd_eckd_private *)device->private;
2876         struct attrib_data_t attrib = private->attrib;
2877         int rc;
2878
2879         if (!capable(CAP_SYS_ADMIN))
2880                 return -EACCES;
2881         if (!argp)
2882                 return -EINVAL;
2883
2884         rc = 0;
2885         if (copy_to_user(argp, (long *) &attrib,
2886                          sizeof(struct attrib_data_t)))
2887                 rc = -EFAULT;
2888
2889         return rc;
2890 }
2891
2892 /*
2893  * Set attributes (cache operations)
2894  * Stores the attributes for cache operation to be used in Define Extend (DE).
2895  */
2896 static int
2897 dasd_eckd_set_attrib(struct dasd_device *device, void __user *argp)
2898 {
2899         struct dasd_eckd_private *private =
2900                 (struct dasd_eckd_private *)device->private;
2901         struct attrib_data_t attrib;
2902
2903         if (!capable(CAP_SYS_ADMIN))
2904                 return -EACCES;
2905         if (!argp)
2906                 return -EINVAL;
2907
2908         if (copy_from_user(&attrib, argp, sizeof(struct attrib_data_t)))
2909                 return -EFAULT;
2910         private->attrib = attrib;
2911
2912         dev_info(&device->cdev->dev,
2913                  "The DASD cache mode was set to %x (%i cylinder prestage)\n",
2914                  private->attrib.operation, private->attrib.nr_cyl);
2915         return 0;
2916 }
2917
2918 /*
2919  * Issue syscall I/O to EMC Symmetrix array.
2920  * CCWs are PSF and RSSD
2921  */
2922 static int dasd_symm_io(struct dasd_device *device, void __user *argp)
2923 {
2924         struct dasd_symmio_parms usrparm;
2925         char *psf_data, *rssd_result;
2926         struct dasd_ccw_req *cqr;
2927         struct ccw1 *ccw;
2928         char psf0, psf1;
2929         int rc;
2930
2931         if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
2932                 return -EACCES;
2933         psf0 = psf1 = 0;
2934
2935         /* Copy parms from caller */
2936         rc = -EFAULT;
2937         if (copy_from_user(&usrparm, argp, sizeof(usrparm)))
2938                 goto out;
2939         if (is_compat_task() || sizeof(long) == 4) {
2940                 /* Make sure pointers are sane even on 31 bit. */
2941                 rc = -EINVAL;
2942                 if ((usrparm.psf_data >> 32) != 0)
2943                         goto out;
2944                 if ((usrparm.rssd_result >> 32) != 0)
2945                         goto out;
2946                 usrparm.psf_data &= 0x7fffffffULL;
2947                 usrparm.rssd_result &= 0x7fffffffULL;
2948         }
2949         /* alloc I/O data area */
2950         psf_data = kzalloc(usrparm.psf_data_len, GFP_KERNEL | GFP_DMA);
2951         rssd_result = kzalloc(usrparm.rssd_result_len, GFP_KERNEL | GFP_DMA);
2952         if (!psf_data || !rssd_result) {
2953                 rc = -ENOMEM;
2954                 goto out_free;
2955         }
2956
2957         /* get syscall header from user space */
2958         rc = -EFAULT;
2959         if (copy_from_user(psf_data,
2960                            (void __user *)(unsigned long) usrparm.psf_data,
2961                            usrparm.psf_data_len))
2962                 goto out_free;
2963         psf0 = psf_data[0];
2964         psf1 = psf_data[1];
2965
2966         /* setup CCWs for PSF + RSSD */
2967         cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 2 , 0, device);
2968         if (IS_ERR(cqr)) {
2969                 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
2970                         "Could not allocate initialization request");
2971                 rc = PTR_ERR(cqr);
2972                 goto out_free;
2973         }
2974
2975         cqr->startdev = device;
2976         cqr->memdev = device;
2977         cqr->retries = 3;
2978         cqr->expires = 10 * HZ;
2979         cqr->buildclk = get_clock();
2980         cqr->status = DASD_CQR_FILLED;
2981
2982         /* Build the ccws */
2983         ccw = cqr->cpaddr;
2984
2985         /* PSF ccw */
2986         ccw->cmd_code = DASD_ECKD_CCW_PSF;
2987         ccw->count = usrparm.psf_data_len;
2988         ccw->flags |= CCW_FLAG_CC;
2989         ccw->cda = (__u32)(addr_t) psf_data;
2990
2991         ccw++;
2992
2993         /* RSSD ccw  */
2994         ccw->cmd_code = DASD_ECKD_CCW_RSSD;
2995         ccw->count = usrparm.rssd_result_len;
2996         ccw->flags = CCW_FLAG_SLI ;
2997         ccw->cda = (__u32)(addr_t) rssd_result;
2998
2999         rc = dasd_sleep_on(cqr);
3000         if (rc)
3001                 goto out_sfree;
3002
3003         rc = -EFAULT;
3004         if (copy_to_user((void __user *)(unsigned long) usrparm.rssd_result,
3005                            rssd_result, usrparm.rssd_result_len))
3006                 goto out_sfree;
3007         rc = 0;
3008
3009 out_sfree:
3010         dasd_sfree_request(cqr, cqr->memdev);
3011 out_free:
3012         kfree(rssd_result);
3013         kfree(psf_data);
3014 out:
3015         DBF_DEV_EVENT(DBF_WARNING, device,
3016                       "Symmetrix ioctl (0x%02x 0x%02x): rc=%d",
3017                       (int) psf0, (int) psf1, rc);
3018         return rc;
3019 }
3020
3021 static int
3022 dasd_eckd_ioctl(struct dasd_block *block, unsigned int cmd, void __user *argp)
3023 {
3024         struct dasd_device *device = block->base;
3025
3026         switch (cmd) {
3027         case BIODASDGATTR:
3028                 return dasd_eckd_get_attrib(device, argp);
3029         case BIODASDSATTR:
3030                 return dasd_eckd_set_attrib(device, argp);
3031         case BIODASDPSRD:
3032                 return dasd_eckd_performance(device, argp);
3033         case BIODASDRLSE:
3034                 return dasd_eckd_release(device);
3035         case BIODASDRSRV:
3036                 return dasd_eckd_reserve(device);
3037         case BIODASDSLCK:
3038                 return dasd_eckd_steal_lock(device);
3039         case BIODASDSYMMIO:
3040                 return dasd_symm_io(device, argp);
3041         default:
3042                 return -ENOIOCTLCMD;
3043         }
3044 }
3045
3046 /*
3047  * Dump the range of CCWs into 'page' buffer
3048  * and return number of printed chars.
3049  */
3050 static int
3051 dasd_eckd_dump_ccw_range(struct ccw1 *from, struct ccw1 *to, char *page)
3052 {
3053         int len, count;
3054         char *datap;
3055
3056         len = 0;
3057         while (from <= to) {
3058                 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
3059                                " CCW %p: %08X %08X DAT:",
3060                                from, ((int *) from)[0], ((int *) from)[1]);
3061
3062                 /* get pointer to data (consider IDALs) */
3063                 if (from->flags & CCW_FLAG_IDA)
3064                         datap = (char *) *((addr_t *) (addr_t) from->cda);
3065                 else
3066                         datap = (char *) ((addr_t) from->cda);
3067
3068                 /* dump data (max 32 bytes) */
3069                 for (count = 0; count < from->count && count < 32; count++) {
3070                         if (count % 8 == 0) len += sprintf(page + len, " ");
3071                         if (count % 4 == 0) len += sprintf(page + len, " ");
3072                         len += sprintf(page + len, "%02x", datap[count]);
3073                 }
3074                 len += sprintf(page + len, "\n");
3075                 from++;
3076         }
3077         return len;
3078 }
3079
3080 static void
3081 dasd_eckd_dump_sense_dbf(struct dasd_device *device, struct irb *irb,
3082                          char *reason)
3083 {
3084         u64 *sense;
3085         u64 *stat;
3086
3087         sense = (u64 *) dasd_get_sense(irb);
3088         stat = (u64 *) &irb->scsw;
3089         if (sense) {
3090                 DBF_DEV_EVENT(DBF_EMERG, device, "%s: %016llx %08x : "
3091                               "%016llx %016llx %016llx %016llx",
3092                               reason, *stat, *((u32 *) (stat + 1)),
3093                               sense[0], sense[1], sense[2], sense[3]);
3094         } else {
3095                 DBF_DEV_EVENT(DBF_EMERG, device, "%s: %016llx %08x : %s",
3096                               reason, *stat, *((u32 *) (stat + 1)),
3097                               "NO VALID SENSE");
3098         }
3099 }
3100
3101 /*
3102  * Print sense data and related channel program.
3103  * Parts are printed because printk buffer is only 1024 bytes.
3104  */
3105 static void dasd_eckd_dump_sense_ccw(struct dasd_device *device,
3106                                  struct dasd_ccw_req *req, struct irb *irb)
3107 {
3108         char *page;
3109         struct ccw1 *first, *last, *fail, *from, *to;
3110         int len, sl, sct;
3111
3112         page = (char *) get_zeroed_page(GFP_ATOMIC);
3113         if (page == NULL) {
3114                 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
3115                               "No memory to dump sense data\n");
3116                 return;
3117         }
3118         /* dump the sense data */
3119         len = sprintf(page,  KERN_ERR PRINTK_HEADER
3120                       " I/O status report for device %s:\n",
3121                       dev_name(&device->cdev->dev));
3122 &nb