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