Pull altix-ce1.0-asic into release branch
[linux-2.6] / drivers / s390 / block / dasd_eckd.c
1 /* 
2  * File...........: linux/drivers/s390/block/dasd_eckd.c
3  * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
4  *                  Horst Hummel <Horst.Hummel@de.ibm.com> 
5  *                  Carsten Otte <Cotte@de.ibm.com>
6  *                  Martin Schwidefsky <schwidefsky@de.ibm.com>
7  * Bugreports.to..: <Linux390@de.ibm.com>
8  * (C) IBM Corporation, IBM Deutschland Entwicklung GmbH, 1999,2000
9  *
10  */
11
12 #include <linux/config.h>
13 #include <linux/stddef.h>
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/hdreg.h>        /* HDIO_GETGEO                      */
17 #include <linux/bio.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
20
21 #include <asm/debug.h>
22 #include <asm/idals.h>
23 #include <asm/ebcdic.h>
24 #include <asm/io.h>
25 #include <asm/todclk.h>
26 #include <asm/uaccess.h>
27 #include <asm/ccwdev.h>
28
29 #include "dasd_int.h"
30 #include "dasd_eckd.h"
31
32 #ifdef PRINTK_HEADER
33 #undef PRINTK_HEADER
34 #endif                          /* PRINTK_HEADER */
35 #define PRINTK_HEADER "dasd(eckd):"
36
37 #define ECKD_C0(i) (i->home_bytes)
38 #define ECKD_F(i) (i->formula)
39 #define ECKD_F1(i) (ECKD_F(i)==0x01?(i->factors.f_0x01.f1):\
40                     (i->factors.f_0x02.f1))
41 #define ECKD_F2(i) (ECKD_F(i)==0x01?(i->factors.f_0x01.f2):\
42                     (i->factors.f_0x02.f2))
43 #define ECKD_F3(i) (ECKD_F(i)==0x01?(i->factors.f_0x01.f3):\
44                     (i->factors.f_0x02.f3))
45 #define ECKD_F4(i) (ECKD_F(i)==0x02?(i->factors.f_0x02.f4):0)
46 #define ECKD_F5(i) (ECKD_F(i)==0x02?(i->factors.f_0x02.f5):0)
47 #define ECKD_F6(i) (i->factor6)
48 #define ECKD_F7(i) (i->factor7)
49 #define ECKD_F8(i) (i->factor8)
50
51 MODULE_LICENSE("GPL");
52
53 static struct dasd_discipline dasd_eckd_discipline;
54
55 struct dasd_eckd_private {
56         struct dasd_eckd_characteristics rdc_data;
57         struct dasd_eckd_confdata conf_data;
58         struct dasd_eckd_path path_data;
59         struct eckd_count count_area[5];
60         int init_cqr_status;
61         int uses_cdl;
62         struct attrib_data_t attrib;    /* e.g. cache operations */
63 };
64
65 /* The ccw bus type uses this table to find devices that it sends to
66  * dasd_eckd_probe */
67 static struct ccw_device_id dasd_eckd_ids[] = {
68         { CCW_DEVICE_DEVTYPE (0x3990, 0, 0x3390, 0), driver_info: 0x1},
69         { CCW_DEVICE_DEVTYPE (0x2105, 0, 0x3390, 0), driver_info: 0x2},
70         { CCW_DEVICE_DEVTYPE (0x3880, 0, 0x3390, 0), driver_info: 0x3},
71         { CCW_DEVICE_DEVTYPE (0x3990, 0, 0x3380, 0), driver_info: 0x4},
72         { CCW_DEVICE_DEVTYPE (0x2105, 0, 0x3380, 0), driver_info: 0x5},
73         { CCW_DEVICE_DEVTYPE (0x9343, 0, 0x9345, 0), driver_info: 0x6},
74         { CCW_DEVICE_DEVTYPE (0x2107, 0, 0x3390, 0), driver_info: 0x7},
75         { CCW_DEVICE_DEVTYPE (0x2107, 0, 0x3380, 0), driver_info: 0x8},
76         { CCW_DEVICE_DEVTYPE (0x1750, 0, 0x3390, 0), driver_info: 0x9},
77         { CCW_DEVICE_DEVTYPE (0x1750, 0, 0x3380, 0), driver_info: 0xa},
78         { /* end of list */ },
79 };
80
81 MODULE_DEVICE_TABLE(ccw, dasd_eckd_ids);
82
83 static struct ccw_driver dasd_eckd_driver; /* see below */
84
85 /* initial attempt at a probe function. this can be simplified once
86  * the other detection code is gone */
87 static int
88 dasd_eckd_probe (struct ccw_device *cdev)
89 {
90         int ret;
91
92         ret = dasd_generic_probe (cdev, &dasd_eckd_discipline);
93         if (ret)
94                 return ret;
95         ccw_device_set_options(cdev, CCWDEV_DO_PATHGROUP | CCWDEV_ALLOW_FORCE);
96         return 0;
97 }
98
99 static int
100 dasd_eckd_set_online(struct ccw_device *cdev)
101 {
102         return dasd_generic_set_online (cdev, &dasd_eckd_discipline);
103 }
104
105 static struct ccw_driver dasd_eckd_driver = {
106         .name        = "dasd-eckd",
107         .owner       = THIS_MODULE,
108         .ids         = dasd_eckd_ids,
109         .probe       = dasd_eckd_probe,
110         .remove      = dasd_generic_remove,
111         .set_offline = dasd_generic_set_offline,
112         .set_online  = dasd_eckd_set_online,
113         .notify      = dasd_generic_notify,
114 };
115
116 static const int sizes_trk0[] = { 28, 148, 84 };
117 #define LABEL_SIZE 140
118
119 static inline unsigned int
120 round_up_multiple(unsigned int no, unsigned int mult)
121 {
122         int rem = no % mult;
123         return (rem ? no - rem + mult : no);
124 }
125
126 static inline unsigned int
127 ceil_quot(unsigned int d1, unsigned int d2)
128 {
129         return (d1 + (d2 - 1)) / d2;
130 }
131
132 static inline int
133 bytes_per_record(struct dasd_eckd_characteristics *rdc, int kl, int dl)
134 {
135         unsigned int fl1, fl2, int1, int2;
136         int bpr;
137
138         switch (rdc->formula) {
139         case 0x01:
140                 fl1 = round_up_multiple(ECKD_F2(rdc) + dl, ECKD_F1(rdc));
141                 fl2 = round_up_multiple(kl ? ECKD_F2(rdc) + kl : 0,
142                                         ECKD_F1(rdc));
143                 bpr = fl1 + fl2;
144                 break;
145         case 0x02:
146                 int1 = ceil_quot(dl + ECKD_F6(rdc), ECKD_F5(rdc) << 1);
147                 int2 = ceil_quot(kl + ECKD_F6(rdc), ECKD_F5(rdc) << 1);
148                 fl1 = round_up_multiple(ECKD_F1(rdc) * ECKD_F2(rdc) + dl +
149                                         ECKD_F6(rdc) + ECKD_F4(rdc) * int1,
150                                         ECKD_F1(rdc));
151                 fl2 = round_up_multiple(ECKD_F1(rdc) * ECKD_F3(rdc) + kl +
152                                         ECKD_F6(rdc) + ECKD_F4(rdc) * int2,
153                                         ECKD_F1(rdc));
154                 bpr = fl1 + fl2;
155                 break;
156         default:
157                 bpr = 0;
158                 break;
159         }
160         return bpr;
161 }
162
163 static inline unsigned int
164 bytes_per_track(struct dasd_eckd_characteristics *rdc)
165 {
166         return *(unsigned int *) (rdc->byte_per_track) >> 8;
167 }
168
169 static inline unsigned int
170 recs_per_track(struct dasd_eckd_characteristics * rdc,
171                unsigned int kl, unsigned int dl)
172 {
173         int dn, kn;
174
175         switch (rdc->dev_type) {
176         case 0x3380:
177                 if (kl)
178                         return 1499 / (15 + 7 + ceil_quot(kl + 12, 32) +
179                                        ceil_quot(dl + 12, 32));
180                 else
181                         return 1499 / (15 + ceil_quot(dl + 12, 32));
182         case 0x3390:
183                 dn = ceil_quot(dl + 6, 232) + 1;
184                 if (kl) {
185                         kn = ceil_quot(kl + 6, 232) + 1;
186                         return 1729 / (10 + 9 + ceil_quot(kl + 6 * kn, 34) +
187                                        9 + ceil_quot(dl + 6 * dn, 34));
188                 } else
189                         return 1729 / (10 + 9 + ceil_quot(dl + 6 * dn, 34));
190         case 0x9345:
191                 dn = ceil_quot(dl + 6, 232) + 1;
192                 if (kl) {
193                         kn = ceil_quot(kl + 6, 232) + 1;
194                         return 1420 / (18 + 7 + ceil_quot(kl + 6 * kn, 34) +
195                                        ceil_quot(dl + 6 * dn, 34));
196                 } else
197                         return 1420 / (18 + 7 + ceil_quot(dl + 6 * dn, 34));
198         }
199         return 0;
200 }
201
202 static inline void
203 check_XRC (struct ccw1         *de_ccw,
204            struct DE_eckd_data *data,
205            struct dasd_device  *device)
206 {
207         struct dasd_eckd_private *private;
208
209         private = (struct dasd_eckd_private *) device->private;
210
211         /* switch on System Time Stamp - needed for XRC Support */
212         if (private->rdc_data.facilities.XRC_supported) {
213                 
214                 data->ga_extended |= 0x08; /* switch on 'Time Stamp Valid'   */
215                 data->ga_extended |= 0x02; /* switch on 'Extended Parameter' */
216                 
217                 data->ep_sys_time = get_clock ();
218                 
219                 de_ccw->count = sizeof (struct DE_eckd_data);
220                 de_ccw->flags |= CCW_FLAG_SLI;  
221         }
222
223         return;
224
225 } /* end check_XRC */
226
227 static inline void
228 define_extent(struct ccw1 * ccw, struct DE_eckd_data * data, int trk,
229               int totrk, int cmd, struct dasd_device * device)
230 {
231         struct dasd_eckd_private *private;
232         struct ch_t geo, beg, end;
233
234         private = (struct dasd_eckd_private *) device->private;
235
236         ccw->cmd_code = DASD_ECKD_CCW_DEFINE_EXTENT;
237         ccw->flags = 0;
238         ccw->count = 16;
239         ccw->cda = (__u32) __pa(data);
240
241         memset(data, 0, sizeof (struct DE_eckd_data));
242         switch (cmd) {
243         case DASD_ECKD_CCW_READ_HOME_ADDRESS:
244         case DASD_ECKD_CCW_READ_RECORD_ZERO:
245         case DASD_ECKD_CCW_READ:
246         case DASD_ECKD_CCW_READ_MT:
247         case DASD_ECKD_CCW_READ_CKD:
248         case DASD_ECKD_CCW_READ_CKD_MT:
249         case DASD_ECKD_CCW_READ_KD:
250         case DASD_ECKD_CCW_READ_KD_MT:
251         case DASD_ECKD_CCW_READ_COUNT:
252                 data->mask.perm = 0x1;
253                 data->attributes.operation = private->attrib.operation;
254                 break;
255         case DASD_ECKD_CCW_WRITE:
256         case DASD_ECKD_CCW_WRITE_MT:
257         case DASD_ECKD_CCW_WRITE_KD:
258         case DASD_ECKD_CCW_WRITE_KD_MT:
259                 data->mask.perm = 0x02;
260                 data->attributes.operation = private->attrib.operation;
261                 check_XRC (ccw, data, device);
262                 break;
263         case DASD_ECKD_CCW_WRITE_CKD:
264         case DASD_ECKD_CCW_WRITE_CKD_MT:
265                 data->attributes.operation = DASD_BYPASS_CACHE;
266                 check_XRC (ccw, data, device);
267                 break;
268         case DASD_ECKD_CCW_ERASE:
269         case DASD_ECKD_CCW_WRITE_HOME_ADDRESS:
270         case DASD_ECKD_CCW_WRITE_RECORD_ZERO:
271                 data->mask.perm = 0x3;
272                 data->mask.auth = 0x1;
273                 data->attributes.operation = DASD_BYPASS_CACHE;
274                 check_XRC (ccw, data, device);
275                 break;
276         default:
277                 DEV_MESSAGE(KERN_ERR, device, "unknown opcode 0x%x", cmd);
278                 break;
279         }
280
281         data->attributes.mode = 0x3;    /* ECKD */
282
283         if ((private->rdc_data.cu_type == 0x2105 ||
284              private->rdc_data.cu_type == 0x2107 ||
285              private->rdc_data.cu_type == 0x1750)
286             && !(private->uses_cdl && trk < 2))
287                 data->ga_extended |= 0x40; /* Regular Data Format Mode */
288
289         geo.cyl = private->rdc_data.no_cyl;
290         geo.head = private->rdc_data.trk_per_cyl;
291         beg.cyl = trk / geo.head;
292         beg.head = trk % geo.head;
293         end.cyl = totrk / geo.head;
294         end.head = totrk % geo.head;
295
296         /* check for sequential prestage - enhance cylinder range */
297         if (data->attributes.operation == DASD_SEQ_PRESTAGE ||
298             data->attributes.operation == DASD_SEQ_ACCESS) {
299                 
300                 if (end.cyl + private->attrib.nr_cyl < geo.cyl) 
301                         end.cyl += private->attrib.nr_cyl;
302                 else
303                         end.cyl = (geo.cyl - 1);
304         }
305
306         data->beg_ext.cyl = beg.cyl;
307         data->beg_ext.head = beg.head;
308         data->end_ext.cyl = end.cyl;
309         data->end_ext.head = end.head;
310 }
311
312 static inline void
313 locate_record(struct ccw1 *ccw, struct LO_eckd_data *data, int trk,
314               int rec_on_trk, int no_rec, int cmd,
315               struct dasd_device * device, int reclen)
316 {
317         struct dasd_eckd_private *private;
318         int sector;
319         int dn, d;
320                                 
321         private = (struct dasd_eckd_private *) device->private;
322
323         DBF_DEV_EVENT(DBF_INFO, device,
324                   "Locate: trk %d, rec %d, no_rec %d, cmd %d, reclen %d",
325                   trk, rec_on_trk, no_rec, cmd, reclen);
326
327         ccw->cmd_code = DASD_ECKD_CCW_LOCATE_RECORD;
328         ccw->flags = 0;
329         ccw->count = 16;
330         ccw->cda = (__u32) __pa(data);
331
332         memset(data, 0, sizeof (struct LO_eckd_data));
333         sector = 0;
334         if (rec_on_trk) {
335                 switch (private->rdc_data.dev_type) {
336                 case 0x3390:
337                         dn = ceil_quot(reclen + 6, 232);
338                         d = 9 + ceil_quot(reclen + 6 * (dn + 1), 34);
339                         sector = (49 + (rec_on_trk - 1) * (10 + d)) / 8;
340                         break;
341                 case 0x3380:
342                         d = 7 + ceil_quot(reclen + 12, 32);
343                         sector = (39 + (rec_on_trk - 1) * (8 + d)) / 7;
344                         break;
345                 }
346         }
347         data->sector = sector;
348         data->count = no_rec;
349         switch (cmd) {
350         case DASD_ECKD_CCW_WRITE_HOME_ADDRESS:
351                 data->operation.orientation = 0x3;
352                 data->operation.operation = 0x03;
353                 break;
354         case DASD_ECKD_CCW_READ_HOME_ADDRESS:
355                 data->operation.orientation = 0x3;
356                 data->operation.operation = 0x16;
357                 break;
358         case DASD_ECKD_CCW_WRITE_RECORD_ZERO:
359                 data->operation.orientation = 0x1;
360                 data->operation.operation = 0x03;
361                 data->count++;
362                 break;
363         case DASD_ECKD_CCW_READ_RECORD_ZERO:
364                 data->operation.orientation = 0x3;
365                 data->operation.operation = 0x16;
366                 data->count++;
367                 break;
368         case DASD_ECKD_CCW_WRITE:
369         case DASD_ECKD_CCW_WRITE_MT:
370         case DASD_ECKD_CCW_WRITE_KD:
371         case DASD_ECKD_CCW_WRITE_KD_MT:
372                 data->auxiliary.last_bytes_used = 0x1;
373                 data->length = reclen;
374                 data->operation.operation = 0x01;
375                 break;
376         case DASD_ECKD_CCW_WRITE_CKD:
377         case DASD_ECKD_CCW_WRITE_CKD_MT:
378                 data->auxiliary.last_bytes_used = 0x1;
379                 data->length = reclen;
380                 data->operation.operation = 0x03;
381                 break;
382         case DASD_ECKD_CCW_READ:
383         case DASD_ECKD_CCW_READ_MT:
384         case DASD_ECKD_CCW_READ_KD:
385         case DASD_ECKD_CCW_READ_KD_MT:
386                 data->auxiliary.last_bytes_used = 0x1;
387                 data->length = reclen;
388                 data->operation.operation = 0x06;
389                 break;
390         case DASD_ECKD_CCW_READ_CKD:
391         case DASD_ECKD_CCW_READ_CKD_MT:
392                 data->auxiliary.last_bytes_used = 0x1;
393                 data->length = reclen;
394                 data->operation.operation = 0x16;
395                 break;
396         case DASD_ECKD_CCW_READ_COUNT:
397                 data->operation.operation = 0x06;
398                 break;
399         case DASD_ECKD_CCW_ERASE:
400                 data->length = reclen;
401                 data->auxiliary.last_bytes_used = 0x1;
402                 data->operation.operation = 0x0b;
403                 break;
404         default:
405                 DEV_MESSAGE(KERN_ERR, device, "unknown opcode 0x%x", cmd);
406         }
407         data->seek_addr.cyl = data->search_arg.cyl =
408                 trk / private->rdc_data.trk_per_cyl;
409         data->seek_addr.head = data->search_arg.head =
410                 trk % private->rdc_data.trk_per_cyl;
411         data->search_arg.record = rec_on_trk;
412 }
413
414 /*
415  * Returns 1 if the block is one of the special blocks that needs
416  * to get read/written with the KD variant of the command.
417  * That is DASD_ECKD_READ_KD_MT instead of DASD_ECKD_READ_MT and
418  * DASD_ECKD_WRITE_KD_MT instead of DASD_ECKD_WRITE_MT.
419  * Luckily the KD variants differ only by one bit (0x08) from the
420  * normal variant. So don't wonder about code like:
421  * if (dasd_eckd_cdl_special(blk_per_trk, recid))
422  *         ccw->cmd_code |= 0x8;
423  */
424 static inline int
425 dasd_eckd_cdl_special(int blk_per_trk, int recid)
426 {
427         if (recid < 3)
428                 return 1;
429         if (recid < blk_per_trk)
430                 return 0;
431         if (recid < 2 * blk_per_trk)
432                 return 1;
433         return 0;
434 }
435
436 /*
437  * Returns the record size for the special blocks of the cdl format.
438  * Only returns something useful if dasd_eckd_cdl_special is true
439  * for the recid.
440  */
441 static inline int
442 dasd_eckd_cdl_reclen(int recid)
443 {
444         if (recid < 3)
445                 return sizes_trk0[recid];
446         return LABEL_SIZE;
447 }
448
449 static int
450 dasd_eckd_read_conf(struct dasd_device *device)
451 {
452         void *conf_data;
453         int conf_len, conf_data_saved;
454         int rc;
455         __u8 lpm;
456         struct dasd_eckd_private *private;
457         struct dasd_eckd_path *path_data;
458
459         private = (struct dasd_eckd_private *) device->private;
460         path_data = (struct dasd_eckd_path *) &private->path_data;
461         path_data->opm = ccw_device_get_path_mask(device->cdev);
462         lpm = 0x80;
463         conf_data_saved = 0;
464
465         /* get configuration data per operational path */
466         for (lpm = 0x80; lpm; lpm>>= 1) {
467                 if (lpm & path_data->opm){
468                         rc = read_conf_data_lpm(device->cdev, &conf_data,
469                                                 &conf_len, lpm);
470                         if (rc && rc != -EOPNOTSUPP) {  /* -EOPNOTSUPP is ok */
471                                 MESSAGE(KERN_WARNING,
472                                         "Read configuration data returned "
473                                         "error %d", rc);
474                                 return rc;
475                         }
476                         if (conf_data == NULL) {
477                                 MESSAGE(KERN_WARNING, "%s", "No configuration "
478                                         "data retrieved");
479                                 continue;       /* no errror */
480                         }
481                         if (conf_len != sizeof (struct dasd_eckd_confdata)) {
482                                 MESSAGE(KERN_WARNING,
483                                         "sizes of configuration data mismatch"
484                                         "%d (read) vs %ld (expected)",
485                                         conf_len,
486                                         sizeof (struct dasd_eckd_confdata));
487                                 kfree(conf_data);
488                                 continue;       /* no errror */
489                         }
490                         /* save first valid configuration data */
491                         if (!conf_data_saved){
492                                 memcpy(&private->conf_data, conf_data,
493                                        sizeof (struct dasd_eckd_confdata));
494                                 conf_data_saved++;
495                         }
496                         switch (((char *)conf_data)[242] & 0x07){
497                         case 0x02:
498                                 path_data->npm |= lpm;
499                                 break;
500                         case 0x03:
501                                 path_data->ppm |= lpm;
502                                 break;
503                         }
504                         kfree(conf_data);
505                 }
506         }
507         return 0;
508 }
509
510
511 static int
512 dasd_eckd_check_characteristics(struct dasd_device *device)
513 {
514         struct dasd_eckd_private *private;
515         void *rdc_data;
516         int rc;
517
518         private = (struct dasd_eckd_private *) device->private;
519         if (private == NULL) {
520                 private = kmalloc(sizeof(struct dasd_eckd_private),
521                                   GFP_KERNEL | GFP_DMA);
522                 if (private == NULL) {
523                         DEV_MESSAGE(KERN_WARNING, device, "%s",
524                                     "memory allocation failed for private "
525                                     "data");
526                         return -ENOMEM;
527                 }
528                 memset(private, 0, sizeof(struct dasd_eckd_private));
529                 device->private = (void *) private;
530         }
531         /* Invalidate status of initial analysis. */
532         private->init_cqr_status = -1;
533         /* Set default cache operations. */
534         private->attrib.operation = DASD_NORMAL_CACHE;
535         private->attrib.nr_cyl = 0;
536
537         /* Read Device Characteristics */
538         rdc_data = (void *) &(private->rdc_data);
539         rc = read_dev_chars(device->cdev, &rdc_data, 64);
540         if (rc) {
541                 DEV_MESSAGE(KERN_WARNING, device,
542                             "Read device characteristics returned error %d",
543                             rc);
544                 return rc;
545         }
546
547         DEV_MESSAGE(KERN_INFO, device,
548                     "%04X/%02X(CU:%04X/%02X) Cyl:%d Head:%d Sec:%d",
549                     private->rdc_data.dev_type,
550                     private->rdc_data.dev_model,
551                     private->rdc_data.cu_type,
552                     private->rdc_data.cu_model.model,
553                     private->rdc_data.no_cyl,
554                     private->rdc_data.trk_per_cyl,
555                     private->rdc_data.sec_per_trk);
556
557         /* Read Configuration Data */
558         rc = dasd_eckd_read_conf (device);
559         return rc;
560
561 }
562
563 static struct dasd_ccw_req *
564 dasd_eckd_analysis_ccw(struct dasd_device *device)
565 {
566         struct dasd_eckd_private *private;
567         struct eckd_count *count_data;
568         struct LO_eckd_data *LO_data;
569         struct dasd_ccw_req *cqr;
570         struct ccw1 *ccw;
571         int cplength, datasize;
572         int i;
573
574         private = (struct dasd_eckd_private *) device->private;
575
576         cplength = 8;
577         datasize = sizeof(struct DE_eckd_data) + 2*sizeof(struct LO_eckd_data);
578         cqr = dasd_smalloc_request(dasd_eckd_discipline.name,
579                                    cplength, datasize, device);
580         if (IS_ERR(cqr))
581                 return cqr;
582         ccw = cqr->cpaddr;
583         /* Define extent for the first 3 tracks. */
584         define_extent(ccw++, cqr->data, 0, 2,
585                       DASD_ECKD_CCW_READ_COUNT, device);
586         LO_data = cqr->data + sizeof (struct DE_eckd_data);
587         /* Locate record for the first 4 records on track 0. */
588         ccw[-1].flags |= CCW_FLAG_CC;
589         locate_record(ccw++, LO_data++, 0, 0, 4,
590                       DASD_ECKD_CCW_READ_COUNT, device, 0);
591
592         count_data = private->count_area;
593         for (i = 0; i < 4; i++) {
594                 ccw[-1].flags |= CCW_FLAG_CC;
595                 ccw->cmd_code = DASD_ECKD_CCW_READ_COUNT;
596                 ccw->flags = 0;
597                 ccw->count = 8;
598                 ccw->cda = (__u32)(addr_t) count_data;
599                 ccw++;
600                 count_data++;
601         }
602
603         /* Locate record for the first record on track 2. */
604         ccw[-1].flags |= CCW_FLAG_CC;
605         locate_record(ccw++, LO_data++, 2, 0, 1,
606                       DASD_ECKD_CCW_READ_COUNT, device, 0);
607         /* Read count ccw. */
608         ccw[-1].flags |= CCW_FLAG_CC;
609         ccw->cmd_code = DASD_ECKD_CCW_READ_COUNT;
610         ccw->flags = 0;
611         ccw->count = 8;
612         ccw->cda = (__u32)(addr_t) count_data;
613
614         cqr->device = device;
615         cqr->retries = 0;
616         cqr->buildclk = get_clock();
617         cqr->status = DASD_CQR_FILLED;
618         return cqr;
619 }
620
621 /*
622  * This is the callback function for the init_analysis cqr. It saves
623  * the status of the initial analysis ccw before it frees it and kicks
624  * the device to continue the startup sequence. This will call
625  * dasd_eckd_do_analysis again (if the devices has not been marked
626  * for deletion in the meantime).
627  */
628 static void
629 dasd_eckd_analysis_callback(struct dasd_ccw_req *init_cqr, void *data)
630 {
631         struct dasd_eckd_private *private;
632         struct dasd_device *device;
633
634         device = init_cqr->device;
635         private = (struct dasd_eckd_private *) device->private;
636         private->init_cqr_status = init_cqr->status;
637         dasd_sfree_request(init_cqr, device);
638         dasd_kick_device(device);
639 }
640
641 static int
642 dasd_eckd_start_analysis(struct dasd_device *device)
643 {
644         struct dasd_eckd_private *private;
645         struct dasd_ccw_req *init_cqr;
646
647         private = (struct dasd_eckd_private *) device->private;
648         init_cqr = dasd_eckd_analysis_ccw(device);
649         if (IS_ERR(init_cqr))
650                 return PTR_ERR(init_cqr);
651         init_cqr->callback = dasd_eckd_analysis_callback;
652         init_cqr->callback_data = NULL;
653         init_cqr->expires = 5*HZ;
654         dasd_add_request_head(init_cqr);
655         return -EAGAIN;
656 }
657
658 static int
659 dasd_eckd_end_analysis(struct dasd_device *device)
660 {
661         struct dasd_eckd_private *private;
662         struct eckd_count *count_area;
663         unsigned int sb, blk_per_trk;
664         int status, i;
665
666         private = (struct dasd_eckd_private *) device->private;
667         status = private->init_cqr_status;
668         private->init_cqr_status = -1;
669         if (status != DASD_CQR_DONE) {
670                 DEV_MESSAGE(KERN_WARNING, device, "%s",
671                             "volume analysis returned unformatted disk");
672                 return -EMEDIUMTYPE;
673         }
674
675         private->uses_cdl = 1;
676         /* Calculate number of blocks/records per track. */
677         blk_per_trk = recs_per_track(&private->rdc_data, 0, device->bp_block);
678         /* Check Track 0 for Compatible Disk Layout */
679         count_area = NULL;
680         for (i = 0; i < 3; i++) {
681                 if (private->count_area[i].kl != 4 ||
682                     private->count_area[i].dl != dasd_eckd_cdl_reclen(i) - 4) {
683                         private->uses_cdl = 0;
684                         break;
685                 }
686         }
687         if (i == 3)
688                 count_area = &private->count_area[4];
689
690         if (private->uses_cdl == 0) {
691                 for (i = 0; i < 5; i++) {
692                         if ((private->count_area[i].kl != 0) ||
693                             (private->count_area[i].dl !=
694                              private->count_area[0].dl))
695                                 break;
696                 }
697                 if (i == 5)
698                         count_area = &private->count_area[0];
699         } else {
700                 if (private->count_area[3].record == 1)
701                         DEV_MESSAGE(KERN_WARNING, device, "%s",
702                                     "Trk 0: no records after VTOC!");
703         }
704         if (count_area != NULL && count_area->kl == 0) {
705                 /* we found notthing violating our disk layout */
706                 if (dasd_check_blocksize(count_area->dl) == 0)
707                         device->bp_block = count_area->dl;
708         }
709         if (device->bp_block == 0) {
710                 DEV_MESSAGE(KERN_WARNING, device, "%s",
711                             "Volume has incompatible disk layout");
712                 return -EMEDIUMTYPE;
713         }
714         device->s2b_shift = 0;  /* bits to shift 512 to get a block */
715         for (sb = 512; sb < device->bp_block; sb = sb << 1)
716                 device->s2b_shift++;
717
718         blk_per_trk = recs_per_track(&private->rdc_data, 0, device->bp_block);
719         device->blocks = (private->rdc_data.no_cyl *
720                           private->rdc_data.trk_per_cyl *
721                           blk_per_trk);
722
723         DEV_MESSAGE(KERN_INFO, device,
724                     "(%dkB blks): %dkB at %dkB/trk %s",
725                     (device->bp_block >> 10),
726                     ((private->rdc_data.no_cyl *
727                       private->rdc_data.trk_per_cyl *
728                       blk_per_trk * (device->bp_block >> 9)) >> 1),
729                     ((blk_per_trk * device->bp_block) >> 10), 
730                     private->uses_cdl ?
731                     "compatible disk layout" : "linux disk layout");
732
733         return 0;
734 }
735
736 static int
737 dasd_eckd_do_analysis(struct dasd_device *device)
738 {
739         struct dasd_eckd_private *private;
740
741         private = (struct dasd_eckd_private *) device->private;
742         if (private->init_cqr_status < 0)
743                 return dasd_eckd_start_analysis(device);
744         else
745                 return dasd_eckd_end_analysis(device);
746 }
747
748 static int
749 dasd_eckd_fill_geometry(struct dasd_device *device, struct hd_geometry *geo)
750 {
751         struct dasd_eckd_private *private;
752
753         private = (struct dasd_eckd_private *) device->private;
754         if (dasd_check_blocksize(device->bp_block) == 0) {
755                 geo->sectors = recs_per_track(&private->rdc_data,
756                                               0, device->bp_block);
757         }
758         geo->cylinders = private->rdc_data.no_cyl;
759         geo->heads = private->rdc_data.trk_per_cyl;
760         return 0;
761 }
762
763 static struct dasd_ccw_req *
764 dasd_eckd_format_device(struct dasd_device * device,
765                         struct format_data_t * fdata)
766 {
767         struct dasd_eckd_private *private;
768         struct dasd_ccw_req *fcp;
769         struct eckd_count *ect;
770         struct ccw1 *ccw;
771         void *data;
772         int rpt, cyl, head;
773         int cplength, datasize;
774         int i;
775
776         private = (struct dasd_eckd_private *) device->private;
777         rpt = recs_per_track(&private->rdc_data, 0, fdata->blksize);
778         cyl = fdata->start_unit / private->rdc_data.trk_per_cyl;
779         head = fdata->start_unit % private->rdc_data.trk_per_cyl;
780
781         /* Sanity checks. */
782         if (fdata->start_unit >=
783             (private->rdc_data.no_cyl * private->rdc_data.trk_per_cyl)) {
784                 DEV_MESSAGE(KERN_INFO, device, "Track no %d too big!",
785                             fdata->start_unit);
786                 return ERR_PTR(-EINVAL);
787         }
788         if (fdata->start_unit > fdata->stop_unit) {
789                 DEV_MESSAGE(KERN_INFO, device, "Track %d reached! ending.",
790                             fdata->start_unit);
791                 return ERR_PTR(-EINVAL);
792         }
793         if (dasd_check_blocksize(fdata->blksize) != 0) {
794                 DEV_MESSAGE(KERN_WARNING, device,
795                             "Invalid blocksize %d...terminating!",
796                             fdata->blksize);
797                 return ERR_PTR(-EINVAL);
798         }
799
800         /*
801          * fdata->intensity is a bit string that tells us what to do:
802          *   Bit 0: write record zero
803          *   Bit 1: write home address, currently not supported
804          *   Bit 2: invalidate tracks
805          *   Bit 3: use OS/390 compatible disk layout (cdl)
806          * Only some bit combinations do make sense.
807          */
808         switch (fdata->intensity) {
809         case 0x00:      /* Normal format */
810         case 0x08:      /* Normal format, use cdl. */
811                 cplength = 2 + rpt;
812                 datasize = sizeof(struct DE_eckd_data) +
813                         sizeof(struct LO_eckd_data) +
814                         rpt * sizeof(struct eckd_count);
815                 break;
816         case 0x01:      /* Write record zero and format track. */
817         case 0x09:      /* Write record zero and format track, use cdl. */
818                 cplength = 3 + rpt;
819                 datasize = sizeof(struct DE_eckd_data) +
820                         sizeof(struct LO_eckd_data) +
821                         sizeof(struct eckd_count) +
822                         rpt * sizeof(struct eckd_count);
823                 break;
824         case 0x04:      /* Invalidate track. */
825         case 0x0c:      /* Invalidate track, use cdl. */
826                 cplength = 3;
827                 datasize = sizeof(struct DE_eckd_data) +
828                         sizeof(struct LO_eckd_data) +
829                         sizeof(struct eckd_count);
830                 break;
831         default:
832                 DEV_MESSAGE(KERN_WARNING, device, "Invalid flags 0x%x.",
833                             fdata->intensity);
834                 return ERR_PTR(-EINVAL);
835         }
836         /* Allocate the format ccw request. */
837         fcp = dasd_smalloc_request(dasd_eckd_discipline.name,
838                                    cplength, datasize, device);
839         if (IS_ERR(fcp))
840                 return fcp;
841
842         data = fcp->data;
843         ccw = fcp->cpaddr;
844
845         switch (fdata->intensity & ~0x08) {
846         case 0x00: /* Normal format. */
847                 define_extent(ccw++, (struct DE_eckd_data *) data,
848                               fdata->start_unit, fdata->start_unit,
849                               DASD_ECKD_CCW_WRITE_CKD, device);
850                 data += sizeof(struct DE_eckd_data);
851                 ccw[-1].flags |= CCW_FLAG_CC;
852                 locate_record(ccw++, (struct LO_eckd_data *) data,
853                               fdata->start_unit, 0, rpt,
854                               DASD_ECKD_CCW_WRITE_CKD, device,
855                               fdata->blksize);
856                 data += sizeof(struct LO_eckd_data);
857                 break;
858         case 0x01: /* Write record zero + format track. */
859                 define_extent(ccw++, (struct DE_eckd_data *) data,
860                               fdata->start_unit, fdata->start_unit,
861                               DASD_ECKD_CCW_WRITE_RECORD_ZERO,
862                               device);
863                 data += sizeof(struct DE_eckd_data);
864                 ccw[-1].flags |= CCW_FLAG_CC;
865                 locate_record(ccw++, (struct LO_eckd_data *) data,
866                               fdata->start_unit, 0, rpt + 1,
867                               DASD_ECKD_CCW_WRITE_RECORD_ZERO, device,
868                               device->bp_block);
869                 data += sizeof(struct LO_eckd_data);
870                 break;
871         case 0x04: /* Invalidate track. */
872                 define_extent(ccw++, (struct DE_eckd_data *) data,
873                               fdata->start_unit, fdata->start_unit,
874                               DASD_ECKD_CCW_WRITE_CKD, device);
875                 data += sizeof(struct DE_eckd_data);
876                 ccw[-1].flags |= CCW_FLAG_CC;
877                 locate_record(ccw++, (struct LO_eckd_data *) data,
878                               fdata->start_unit, 0, 1,
879                               DASD_ECKD_CCW_WRITE_CKD, device, 8);
880                 data += sizeof(struct LO_eckd_data);
881                 break;
882         }
883         if (fdata->intensity & 0x01) {  /* write record zero */
884                 ect = (struct eckd_count *) data;
885                 data += sizeof(struct eckd_count);
886                 ect->cyl = cyl;
887                 ect->head = head;
888                 ect->record = 0;
889                 ect->kl = 0;
890                 ect->dl = 8;
891                 ccw[-1].flags |= CCW_FLAG_CC;
892                 ccw->cmd_code = DASD_ECKD_CCW_WRITE_RECORD_ZERO;
893                 ccw->flags = CCW_FLAG_SLI;
894                 ccw->count = 8;
895                 ccw->cda = (__u32)(addr_t) ect;
896                 ccw++;
897         }
898         if ((fdata->intensity & ~0x08) & 0x04) {        /* erase track */
899                 ect = (struct eckd_count *) data;
900                 data += sizeof(struct eckd_count);
901                 ect->cyl = cyl;
902                 ect->head = head;
903                 ect->record = 1;
904                 ect->kl = 0;
905                 ect->dl = 0;
906                 ccw[-1].flags |= CCW_FLAG_CC;
907                 ccw->cmd_code = DASD_ECKD_CCW_WRITE_CKD;
908                 ccw->flags = CCW_FLAG_SLI;
909                 ccw->count = 8;
910                 ccw->cda = (__u32)(addr_t) ect;
911         } else {                /* write remaining records */
912                 for (i = 0; i < rpt; i++) {
913                         ect = (struct eckd_count *) data;
914                         data += sizeof(struct eckd_count);
915                         ect->cyl = cyl;
916                         ect->head = head;
917                         ect->record = i + 1;
918                         ect->kl = 0;
919                         ect->dl = fdata->blksize;
920                         /* Check for special tracks 0-1 when formatting CDL */
921                         if ((fdata->intensity & 0x08) &&
922                             fdata->start_unit == 0) {
923                                 if (i < 3) {
924                                         ect->kl = 4;
925                                         ect->dl = sizes_trk0[i] - 4;
926                                 } 
927                         }
928                         if ((fdata->intensity & 0x08) &&
929                             fdata->start_unit == 1) {
930                                 ect->kl = 44;
931                                 ect->dl = LABEL_SIZE - 44;
932                         }
933                         ccw[-1].flags |= CCW_FLAG_CC;
934                         ccw->cmd_code = DASD_ECKD_CCW_WRITE_CKD;
935                         ccw->flags = CCW_FLAG_SLI;
936                         ccw->count = 8;
937                         ccw->cda = (__u32)(addr_t) ect;
938                         ccw++;
939                 }
940         }
941         fcp->device = device;
942         fcp->retries = 2;       /* set retry counter to enable ERP */
943         fcp->buildclk = get_clock();
944         fcp->status = DASD_CQR_FILLED;
945         return fcp;
946 }
947
948 static dasd_era_t
949 dasd_eckd_examine_error(struct dasd_ccw_req * cqr, struct irb * irb)
950 {
951         struct dasd_device *device = (struct dasd_device *) cqr->device;
952         struct ccw_device *cdev = device->cdev;
953
954         if (irb->scsw.cstat == 0x00 &&
955             irb->scsw.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END))
956                 return dasd_era_none;
957
958         switch (cdev->id.cu_type) {
959         case 0x3990:
960         case 0x2105:
961         case 0x2107:
962         case 0x1750:
963                 return dasd_3990_erp_examine(cqr, irb);
964         case 0x9343:
965                 return dasd_9343_erp_examine(cqr, irb);
966         case 0x3880:
967         default:
968                 DEV_MESSAGE(KERN_WARNING, device, "%s",
969                             "default (unknown CU type) - RECOVERABLE return");
970                 return dasd_era_recover;
971         }
972 }
973
974 static dasd_erp_fn_t
975 dasd_eckd_erp_action(struct dasd_ccw_req * cqr)
976 {
977         struct dasd_device *device = (struct dasd_device *) cqr->device;
978         struct ccw_device *cdev = device->cdev;
979
980         switch (cdev->id.cu_type) {
981         case 0x3990:
982         case 0x2105:
983         case 0x2107:
984         case 0x1750:
985                 return dasd_3990_erp_action;
986         case 0x9343:
987         case 0x3880:
988         default:
989                 return dasd_default_erp_action;
990         }
991 }
992
993 static dasd_erp_fn_t
994 dasd_eckd_erp_postaction(struct dasd_ccw_req * cqr)
995 {
996         return dasd_default_erp_postaction;
997 }
998
999 static struct dasd_ccw_req *
1000 dasd_eckd_build_cp(struct dasd_device * device, struct request *req)
1001 {
1002         struct dasd_eckd_private *private;
1003         unsigned long *idaws;
1004         struct LO_eckd_data *LO_data;
1005         struct dasd_ccw_req *cqr;
1006         struct ccw1 *ccw;
1007         struct bio *bio;
1008         struct bio_vec *bv;
1009         char *dst;
1010         unsigned int blksize, blk_per_trk, off;
1011         int count, cidaw, cplength, datasize;
1012         sector_t recid, first_rec, last_rec;
1013         sector_t first_trk, last_trk;
1014         unsigned int first_offs, last_offs;
1015         unsigned char cmd, rcmd;
1016         int i;
1017
1018         private = (struct dasd_eckd_private *) device->private;
1019         if (rq_data_dir(req) == READ)
1020                 cmd = DASD_ECKD_CCW_READ_MT;
1021         else if (rq_data_dir(req) == WRITE)
1022                 cmd = DASD_ECKD_CCW_WRITE_MT;
1023         else
1024                 return ERR_PTR(-EINVAL);
1025         /* Calculate number of blocks/records per track. */
1026         blksize = device->bp_block;
1027         blk_per_trk = recs_per_track(&private->rdc_data, 0, blksize);
1028         /* Calculate record id of first and last block. */
1029         first_rec = first_trk = req->sector >> device->s2b_shift;
1030         first_offs = sector_div(first_trk, blk_per_trk);
1031         last_rec = last_trk =
1032                 (req->sector + req->nr_sectors - 1) >> device->s2b_shift;
1033         last_offs = sector_div(last_trk, blk_per_trk);
1034         /* Check struct bio and count the number of blocks for the request. */
1035         count = 0;
1036         cidaw = 0;
1037         rq_for_each_bio(bio, req) {
1038                 bio_for_each_segment(bv, bio, i) {
1039                         if (bv->bv_len & (blksize - 1))
1040                                 /* Eckd can only do full blocks. */
1041                                 return ERR_PTR(-EINVAL);
1042                         count += bv->bv_len >> (device->s2b_shift + 9);
1043 #if defined(CONFIG_64BIT)
1044                         if (idal_is_needed (page_address(bv->bv_page),
1045                                             bv->bv_len))
1046                                 cidaw += bv->bv_len >> (device->s2b_shift + 9);
1047 #endif
1048                 }
1049         }
1050         /* Paranoia. */
1051         if (count != last_rec - first_rec + 1)
1052                 return ERR_PTR(-EINVAL);
1053         /* 1x define extent + 1x locate record + number of blocks */
1054         cplength = 2 + count;
1055         /* 1x define extent + 1x locate record + cidaws*sizeof(long) */
1056         datasize = sizeof(struct DE_eckd_data) + sizeof(struct LO_eckd_data) +
1057                 cidaw * sizeof(unsigned long);
1058         /* Find out the number of additional locate record ccws for cdl. */
1059         if (private->uses_cdl && first_rec < 2*blk_per_trk) {
1060                 if (last_rec >= 2*blk_per_trk)
1061                         count = 2*blk_per_trk - first_rec;
1062                 cplength += count;
1063                 datasize += count*sizeof(struct LO_eckd_data);
1064         }
1065         /* Allocate the ccw request. */
1066         cqr = dasd_smalloc_request(dasd_eckd_discipline.name,
1067                                    cplength, datasize, device);
1068         if (IS_ERR(cqr))
1069                 return cqr;
1070         ccw = cqr->cpaddr;
1071         /* First ccw is define extent. */
1072         define_extent(ccw++, cqr->data, first_trk, last_trk, cmd, device);
1073         /* Build locate_record+read/write/ccws. */
1074         idaws = (unsigned long *) (cqr->data + sizeof(struct DE_eckd_data));
1075         LO_data = (struct LO_eckd_data *) (idaws + cidaw);
1076         recid = first_rec;
1077         if (private->uses_cdl == 0 || recid > 2*blk_per_trk) {
1078                 /* Only standard blocks so there is just one locate record. */
1079                 ccw[-1].flags |= CCW_FLAG_CC;
1080                 locate_record(ccw++, LO_data++, first_trk, first_offs + 1,
1081                               last_rec - recid + 1, cmd, device, blksize);
1082         }
1083         rq_for_each_bio(bio, req) bio_for_each_segment(bv, bio, i) {
1084                 dst = page_address(bv->bv_page) + bv->bv_offset;
1085                 if (dasd_page_cache) {
1086                         char *copy = kmem_cache_alloc(dasd_page_cache,
1087                                                       SLAB_DMA | __GFP_NOWARN);
1088                         if (copy && rq_data_dir(req) == WRITE)
1089                                 memcpy(copy + bv->bv_offset, dst, bv->bv_len);
1090                         if (copy)
1091                                 dst = copy + bv->bv_offset;
1092                 }
1093                 for (off = 0; off < bv->bv_len; off += blksize) {
1094                         sector_t trkid = recid;
1095                         unsigned int recoffs = sector_div(trkid, blk_per_trk);
1096                         rcmd = cmd;
1097                         count = blksize;
1098                         /* Locate record for cdl special block ? */
1099                         if (private->uses_cdl && recid < 2*blk_per_trk) {
1100                                 if (dasd_eckd_cdl_special(blk_per_trk, recid)){
1101                                         rcmd |= 0x8;
1102                                         count = dasd_eckd_cdl_reclen(recid);
1103                                         if (count < blksize &&
1104                                             rq_data_dir(req) == READ)
1105                                                 memset(dst + count, 0xe5,
1106                                                        blksize - count);
1107                                 }
1108                                 ccw[-1].flags |= CCW_FLAG_CC;
1109                                 locate_record(ccw++, LO_data++,
1110                                               trkid, recoffs + 1,
1111                                               1, rcmd, device, count);
1112                         }
1113                         /* Locate record for standard blocks ? */
1114                         if (private->uses_cdl && recid == 2*blk_per_trk) {
1115                                 ccw[-1].flags |= CCW_FLAG_CC;
1116                                 locate_record(ccw++, LO_data++,
1117                                               trkid, recoffs + 1,
1118                                               last_rec - recid + 1,
1119                                               cmd, device, count);
1120                         }
1121                         /* Read/write ccw. */
1122                         ccw[-1].flags |= CCW_FLAG_CC;
1123                         ccw->cmd_code = rcmd;
1124                         ccw->count = count;
1125                         if (idal_is_needed(dst, blksize)) {
1126                                 ccw->cda = (__u32)(addr_t) idaws;
1127                                 ccw->flags = CCW_FLAG_IDA;
1128                                 idaws = idal_create_words(idaws, dst, blksize);
1129                         } else {
1130                                 ccw->cda = (__u32)(addr_t) dst;
1131                                 ccw->flags = 0;
1132                         }
1133                         ccw++;
1134                         dst += blksize;
1135                         recid++;
1136                 }
1137         }
1138         if (req->flags & REQ_FAILFAST)
1139                 set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
1140         cqr->device = device;
1141         cqr->expires = 5 * 60 * HZ;     /* 5 minutes */
1142         cqr->lpm = private->path_data.ppm;
1143         cqr->retries = 256;
1144         cqr->buildclk = get_clock();
1145         cqr->status = DASD_CQR_FILLED;
1146         return cqr;
1147 }
1148
1149 static int
1150 dasd_eckd_free_cp(struct dasd_ccw_req *cqr, struct request *req)
1151 {
1152         struct dasd_eckd_private *private;
1153         struct ccw1 *ccw;
1154         struct bio *bio;
1155         struct bio_vec *bv;
1156         char *dst, *cda;
1157         unsigned int blksize, blk_per_trk, off;
1158         sector_t recid;
1159         int i, status;
1160
1161         if (!dasd_page_cache)
1162                 goto out;
1163         private = (struct dasd_eckd_private *) cqr->device->private;
1164         blksize = cqr->device->bp_block;
1165         blk_per_trk = recs_per_track(&private->rdc_data, 0, blksize);
1166         recid = req->sector >> cqr->device->s2b_shift;
1167         ccw = cqr->cpaddr;
1168         /* Skip over define extent & locate record. */
1169         ccw++;
1170         if (private->uses_cdl == 0 || recid > 2*blk_per_trk)
1171                 ccw++;
1172         rq_for_each_bio(bio, req) bio_for_each_segment(bv, bio, i) {
1173                 dst = page_address(bv->bv_page) + bv->bv_offset;
1174                 for (off = 0; off < bv->bv_len; off += blksize) {
1175                         /* Skip locate record. */
1176                         if (private->uses_cdl && recid <= 2*blk_per_trk)
1177                                 ccw++;
1178                         if (dst) {
1179                                 if (ccw->flags & CCW_FLAG_IDA)
1180                                         cda = *((char **)((addr_t) ccw->cda));
1181                                 else
1182                                         cda = (char *)((addr_t) ccw->cda);
1183                                 if (dst != cda) {
1184                                         if (rq_data_dir(req) == READ)
1185                                                 memcpy(dst, cda, bv->bv_len);
1186                                         kmem_cache_free(dasd_page_cache,
1187                                             (void *)((addr_t)cda & PAGE_MASK));
1188                                 }
1189                                 dst = NULL;
1190                         }
1191                         ccw++;
1192                         recid++;
1193                 }
1194         }
1195 out:
1196         status = cqr->status == DASD_CQR_DONE;
1197         dasd_sfree_request(cqr, cqr->device);
1198         return status;
1199 }
1200
1201 static int
1202 dasd_eckd_fill_info(struct dasd_device * device,
1203                     struct dasd_information2_t * info)
1204 {
1205         struct dasd_eckd_private *private;
1206
1207         private = (struct dasd_eckd_private *) device->private;
1208         info->label_block = 2;
1209         info->FBA_layout = private->uses_cdl ? 0 : 1;
1210         info->format = private->uses_cdl ? DASD_FORMAT_CDL : DASD_FORMAT_LDL;
1211         info->characteristics_size = sizeof(struct dasd_eckd_characteristics);
1212         memcpy(info->characteristics, &private->rdc_data,
1213                sizeof(struct dasd_eckd_characteristics));
1214         info->confdata_size = sizeof (struct dasd_eckd_confdata);
1215         memcpy(info->configuration_data, &private->conf_data,
1216                sizeof (struct dasd_eckd_confdata));
1217         return 0;
1218 }
1219
1220 /*
1221  * SECTION: ioctl functions for eckd devices.
1222  */
1223
1224 /*
1225  * Release device ioctl.
1226  * Buils a channel programm to releases a prior reserved 
1227  * (see dasd_eckd_reserve) device.
1228  */
1229 static int
1230 dasd_eckd_release(struct block_device *bdev, int no, long args)
1231 {
1232         struct dasd_device *device;
1233         struct dasd_ccw_req *cqr;
1234         int rc;
1235
1236         if (!capable(CAP_SYS_ADMIN))
1237                 return -EACCES;
1238
1239         device = bdev->bd_disk->private_data;
1240         if (device == NULL)
1241                 return -ENODEV;
1242
1243         cqr = dasd_smalloc_request(dasd_eckd_discipline.name,
1244                                    1, 32, device);
1245         if (IS_ERR(cqr)) {
1246                 DEV_MESSAGE(KERN_WARNING, device, "%s",
1247                             "Could not allocate initialization request");
1248                 return PTR_ERR(cqr);
1249         }
1250         cqr->cpaddr->cmd_code = DASD_ECKD_CCW_RELEASE;
1251         cqr->cpaddr->flags |= CCW_FLAG_SLI;
1252         cqr->cpaddr->count = 32;
1253         cqr->cpaddr->cda = (__u32)(addr_t) cqr->data;
1254         cqr->device = device;
1255         clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1256         set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
1257         cqr->retries = 0;
1258         cqr->expires = 2 * HZ;
1259         cqr->buildclk = get_clock();
1260         cqr->status = DASD_CQR_FILLED;
1261
1262         rc = dasd_sleep_on_immediatly(cqr);
1263
1264         dasd_sfree_request(cqr, cqr->device);
1265         return rc;
1266 }
1267
1268 /*
1269  * Reserve device ioctl.
1270  * Options are set to 'synchronous wait for interrupt' and
1271  * 'timeout the request'. This leads to a terminate IO if 
1272  * the interrupt is outstanding for a certain time. 
1273  */
1274 static int
1275 dasd_eckd_reserve(struct block_device *bdev, int no, long args)
1276 {
1277         struct dasd_device *device;
1278         struct dasd_ccw_req *cqr;
1279         int rc;
1280
1281         if (!capable(CAP_SYS_ADMIN))
1282                 return -EACCES;
1283
1284         device = bdev->bd_disk->private_data;
1285         if (device == NULL)
1286                 return -ENODEV;
1287
1288         cqr = dasd_smalloc_request(dasd_eckd_discipline.name,
1289                                    1, 32, device);
1290         if (IS_ERR(cqr)) {
1291                 DEV_MESSAGE(KERN_WARNING, device, "%s",
1292                             "Could not allocate initialization request");
1293                 return PTR_ERR(cqr);
1294         }
1295         cqr->cpaddr->cmd_code = DASD_ECKD_CCW_RESERVE;
1296         cqr->cpaddr->flags |= CCW_FLAG_SLI;
1297         cqr->cpaddr->count = 32;
1298         cqr->cpaddr->cda = (__u32)(addr_t) cqr->data;
1299         cqr->device = device;
1300         clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1301         set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
1302         cqr->retries = 0;
1303         cqr->expires = 2 * HZ;
1304         cqr->buildclk = get_clock();
1305         cqr->status = DASD_CQR_FILLED;
1306
1307         rc = dasd_sleep_on_immediatly(cqr);
1308
1309         dasd_sfree_request(cqr, cqr->device);
1310         return rc;
1311 }
1312
1313 /*
1314  * Steal lock ioctl - unconditional reserve device.
1315  * Buils a channel programm to break a device's reservation. 
1316  * (unconditional reserve)
1317  */
1318 static int
1319 dasd_eckd_steal_lock(struct block_device *bdev, int no, long args)
1320 {
1321         struct dasd_device *device;
1322         struct dasd_ccw_req *cqr;
1323         int rc;
1324
1325         if (!capable(CAP_SYS_ADMIN))
1326                 return -EACCES;
1327
1328         device = bdev->bd_disk->private_data;
1329         if (device == NULL)
1330                 return -ENODEV;
1331
1332         cqr = dasd_smalloc_request(dasd_eckd_discipline.name,
1333                                    1, 32, device);
1334         if (IS_ERR(cqr)) {
1335                 DEV_MESSAGE(KERN_WARNING, device, "%s",
1336                             "Could not allocate initialization request");
1337                 return PTR_ERR(cqr);
1338         }
1339         cqr->cpaddr->cmd_code = DASD_ECKD_CCW_SLCK;
1340         cqr->cpaddr->flags |= CCW_FLAG_SLI;
1341         cqr->cpaddr->count = 32;
1342         cqr->cpaddr->cda = (__u32)(addr_t) cqr->data;
1343         cqr->device = device;
1344         clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1345         set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
1346         cqr->retries = 0;
1347         cqr->expires = 2 * HZ;
1348         cqr->buildclk = get_clock();
1349         cqr->status = DASD_CQR_FILLED;
1350
1351         rc = dasd_sleep_on_immediatly(cqr);
1352
1353         dasd_sfree_request(cqr, cqr->device);
1354         return rc;
1355 }
1356
1357 /*
1358  * Read performance statistics
1359  */
1360 static int
1361 dasd_eckd_performance(struct block_device *bdev, int no, long args)
1362 {
1363         struct dasd_device *device;
1364         struct dasd_psf_prssd_data *prssdp;
1365         struct dasd_rssd_perf_stats_t *stats;
1366         struct dasd_ccw_req *cqr;
1367         struct ccw1 *ccw;
1368         int rc;
1369
1370         device = bdev->bd_disk->private_data;
1371         if (device == NULL)
1372                 return -ENODEV;
1373
1374         cqr = dasd_smalloc_request(dasd_eckd_discipline.name,
1375                                    1 /* PSF */  + 1 /* RSSD */ ,
1376                                    (sizeof (struct dasd_psf_prssd_data) +
1377                                     sizeof (struct dasd_rssd_perf_stats_t)),
1378                                    device);
1379         if (IS_ERR(cqr)) {
1380                 DEV_MESSAGE(KERN_WARNING, device, "%s",
1381                             "Could not allocate initialization request");
1382                 return PTR_ERR(cqr);
1383         }
1384         cqr->device = device;
1385         cqr->retries = 0;
1386         cqr->expires = 10 * HZ;
1387
1388         /* Prepare for Read Subsystem Data */
1389         prssdp = (struct dasd_psf_prssd_data *) cqr->data;
1390         memset(prssdp, 0, sizeof (struct dasd_psf_prssd_data));
1391         prssdp->order = PSF_ORDER_PRSSD;
1392         prssdp->suborder = 0x01;        /* Perfomance Statistics */
1393         prssdp->varies[1] = 0x01;       /* Perf Statistics for the Subsystem */
1394
1395         ccw = cqr->cpaddr;
1396         ccw->cmd_code = DASD_ECKD_CCW_PSF;
1397         ccw->count = sizeof (struct dasd_psf_prssd_data);
1398         ccw->flags |= CCW_FLAG_CC;
1399         ccw->cda = (__u32)(addr_t) prssdp;
1400
1401         /* Read Subsystem Data - Performance Statistics */
1402         stats = (struct dasd_rssd_perf_stats_t *) (prssdp + 1);
1403         memset(stats, 0, sizeof (struct dasd_rssd_perf_stats_t));
1404
1405         ccw++;
1406         ccw->cmd_code = DASD_ECKD_CCW_RSSD;
1407         ccw->count = sizeof (struct dasd_rssd_perf_stats_t);
1408         ccw->cda = (__u32)(addr_t) stats;
1409
1410         cqr->buildclk = get_clock();
1411         cqr->status = DASD_CQR_FILLED;
1412         rc = dasd_sleep_on(cqr);
1413         if (rc == 0) {
1414                 /* Prepare for Read Subsystem Data */
1415                 prssdp = (struct dasd_psf_prssd_data *) cqr->data;
1416                 stats = (struct dasd_rssd_perf_stats_t *) (prssdp + 1);
1417                 rc = copy_to_user((long __user *) args, (long *) stats,
1418                                   sizeof(struct dasd_rssd_perf_stats_t));
1419         }
1420         dasd_sfree_request(cqr, cqr->device);
1421         return rc;
1422 }
1423
1424 /*
1425  * Get attributes (cache operations)
1426  * Returnes the cache attributes used in Define Extend (DE).
1427  */
1428 static int
1429 dasd_eckd_get_attrib (struct block_device *bdev, int no, long args)
1430 {
1431         struct dasd_device *device;
1432         struct dasd_eckd_private *private;
1433         struct attrib_data_t attrib;
1434         int rc;
1435
1436         if (!capable(CAP_SYS_ADMIN))
1437                 return -EACCES;
1438         if (!args)
1439                 return -EINVAL;
1440
1441         device = bdev->bd_disk->private_data;
1442         if (device == NULL)
1443                 return -ENODEV;
1444
1445         private = (struct dasd_eckd_private *) device->private;
1446         attrib = private->attrib;
1447
1448         rc = copy_to_user((long __user *) args, (long *) &attrib,
1449                           sizeof (struct attrib_data_t));
1450
1451         return rc;
1452 }
1453
1454 /*
1455  * Set attributes (cache operations)
1456  * Stores the attributes for cache operation to be used in Define Extend (DE).
1457  */
1458 static int
1459 dasd_eckd_set_attrib(struct block_device *bdev, int no, long args)
1460 {
1461         struct dasd_device *device;
1462         struct dasd_eckd_private *private;
1463         struct attrib_data_t attrib;
1464
1465         if (!capable(CAP_SYS_ADMIN))
1466                 return -EACCES;
1467         if (!args)
1468                 return -EINVAL;
1469
1470         device = bdev->bd_disk->private_data;
1471         if (device == NULL)
1472                 return -ENODEV;
1473
1474         if (copy_from_user(&attrib, (void __user *) args,
1475                            sizeof (struct attrib_data_t))) {
1476                 return -EFAULT;
1477         }
1478         private = (struct dasd_eckd_private *) device->private;
1479         private->attrib = attrib;
1480
1481         DEV_MESSAGE(KERN_INFO, device,
1482                     "cache operation mode set to %x (%i cylinder prestage)",
1483                     private->attrib.operation, private->attrib.nr_cyl);
1484         return 0;
1485 }
1486
1487 /*
1488  * Print sense data and related channel program.
1489  * Parts are printed because printk buffer is only 1024 bytes.
1490  */
1491 static void
1492 dasd_eckd_dump_sense(struct dasd_device *device, struct dasd_ccw_req * req,
1493                      struct irb *irb)
1494 {
1495         char *page;
1496         struct ccw1 *act, *end, *last;
1497         int len, sl, sct, count;
1498
1499         page = (char *) get_zeroed_page(GFP_ATOMIC);
1500         if (page == NULL) {
1501                 DEV_MESSAGE(KERN_ERR, device, " %s",
1502                             "No memory to dump sense data");
1503                 return;
1504         }
1505         len = sprintf(page, KERN_ERR PRINTK_HEADER
1506                       " I/O status report for device %s:\n",
1507                       device->cdev->dev.bus_id);
1508         len += sprintf(page + len, KERN_ERR PRINTK_HEADER
1509                        " in req: %p CS: 0x%02X DS: 0x%02X\n", req,
1510                        irb->scsw.cstat, irb->scsw.dstat);
1511         len += sprintf(page + len, KERN_ERR PRINTK_HEADER
1512                        " device %s: Failing CCW: %p\n",
1513                        device->cdev->dev.bus_id,
1514                        (void *) (addr_t) irb->scsw.cpa);
1515         if (irb->esw.esw0.erw.cons) {
1516                 for (sl = 0; sl < 4; sl++) {
1517                         len += sprintf(page + len, KERN_ERR PRINTK_HEADER
1518                                        " Sense(hex) %2d-%2d:",
1519                                        (8 * sl), ((8 * sl) + 7));
1520
1521                         for (sct = 0; sct < 8; sct++) {
1522                                 len += sprintf(page + len, " %02x",
1523                                                irb->ecw[8 * sl + sct]);
1524                         }
1525                         len += sprintf(page + len, "\n");
1526                 }
1527
1528                 if (irb->ecw[27] & DASD_SENSE_BIT_0) {
1529                         /* 24 Byte Sense Data */
1530                         len += sprintf(page + len, KERN_ERR PRINTK_HEADER
1531                                        " 24 Byte: %x MSG %x, "
1532                                        "%s MSGb to SYSOP\n",
1533                                        irb->ecw[7] >> 4, irb->ecw[7] & 0x0f,
1534                                        irb->ecw[1] & 0x10 ? "" : "no");
1535                 } else {
1536                         /* 32 Byte Sense Data */
1537                         len += sprintf(page + len, KERN_ERR PRINTK_HEADER
1538                                        " 32 Byte: Format: %x "
1539                                        "Exception class %x\n",
1540                                        irb->ecw[6] & 0x0f, irb->ecw[22] >> 4);
1541                 }
1542         } else {
1543                 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
1544                                " SORRY - NO VALID SENSE AVAILABLE\n");
1545         }
1546         MESSAGE_LOG(KERN_ERR, "%s",
1547                     page + sizeof(KERN_ERR PRINTK_HEADER));
1548
1549         /* dump the Channel Program */
1550         /* print first CCWs (maximum 8) */
1551         act = req->cpaddr;
1552         for (last = act; last->flags & (CCW_FLAG_CC | CCW_FLAG_DC); last++);
1553         end = min(act + 8, last);
1554         len = sprintf(page, KERN_ERR PRINTK_HEADER
1555                       " Related CP in req: %p\n", req);
1556         while (act <= end) {
1557                 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
1558                                " CCW %p: %08X %08X DAT:",
1559                                act, ((int *) act)[0], ((int *) act)[1]);
1560                 for (count = 0; count < 32 && count < act->count;
1561                      count += sizeof(int))
1562                         len += sprintf(page + len, " %08X",
1563                                        ((int *) (addr_t) act->cda)
1564                                        [(count>>2)]);
1565                 len += sprintf(page + len, "\n");
1566                 act++;
1567         }
1568         MESSAGE_LOG(KERN_ERR, "%s",
1569                     page + sizeof(KERN_ERR PRINTK_HEADER));
1570
1571         /* print failing CCW area */
1572         len = 0;
1573         if (act <  ((struct ccw1 *)(addr_t) irb->scsw.cpa) - 2) {
1574                 act = ((struct ccw1 *)(addr_t) irb->scsw.cpa) - 2;
1575                 len += sprintf(page + len, KERN_ERR PRINTK_HEADER "......\n");
1576         }
1577         end = min((struct ccw1 *)(addr_t) irb->scsw.cpa + 2, last);
1578         while (act <= end) {
1579                 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
1580                                " CCW %p: %08X %08X DAT:",
1581                                act, ((int *) act)[0], ((int *) act)[1]);
1582                 for (count = 0; count < 32 && count < act->count;
1583                      count += sizeof(int))
1584                         len += sprintf(page + len, " %08X",
1585                                        ((int *) (addr_t) act->cda)
1586                                        [(count>>2)]);
1587                 len += sprintf(page + len, "\n");
1588                 act++;
1589         }
1590
1591         /* print last CCWs */
1592         if (act <  last - 2) {
1593                 act = last - 2;
1594                 len += sprintf(page + len, KERN_ERR PRINTK_HEADER "......\n");
1595         }
1596         while (act <= last) {
1597                 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
1598                                " CCW %p: %08X %08X DAT:",
1599                                act, ((int *) act)[0], ((int *) act)[1]);
1600                 for (count = 0; count < 32 && count < act->count;
1601                      count += sizeof(int))
1602                         len += sprintf(page + len, " %08X",
1603                                        ((int *) (addr_t) act->cda)
1604                                        [(count>>2)]);
1605                 len += sprintf(page + len, "\n");
1606                 act++;
1607         }
1608         if (len > 0)
1609                 MESSAGE_LOG(KERN_ERR, "%s",
1610                             page + sizeof(KERN_ERR PRINTK_HEADER));
1611         free_page((unsigned long) page);
1612 }
1613
1614 /*
1615  * max_blocks is dependent on the amount of storage that is available
1616  * in the static io buffer for each device. Currently each device has
1617  * 8192 bytes (=2 pages). For 64 bit one dasd_mchunkt_t structure has
1618  * 24 bytes, the struct dasd_ccw_req has 136 bytes and each block can use
1619  * up to 16 bytes (8 for the ccw and 8 for the idal pointer). In
1620  * addition we have one define extent ccw + 16 bytes of data and one
1621  * locate record ccw + 16 bytes of data. That makes:
1622  * (8192 - 24 - 136 - 8 - 16 - 8 - 16) / 16 = 499 blocks at maximum.
1623  * We want to fit two into the available memory so that we can immediately
1624  * start the next request if one finishes off. That makes 249.5 blocks
1625  * for one request. Give a little safety and the result is 240.
1626  */
1627 static struct dasd_discipline dasd_eckd_discipline = {
1628         .owner = THIS_MODULE,
1629         .name = "ECKD",
1630         .ebcname = "ECKD",
1631         .max_blocks = 240,
1632         .check_device = dasd_eckd_check_characteristics,
1633         .do_analysis = dasd_eckd_do_analysis,
1634         .fill_geometry = dasd_eckd_fill_geometry,
1635         .start_IO = dasd_start_IO,
1636         .term_IO = dasd_term_IO,
1637         .format_device = dasd_eckd_format_device,
1638         .examine_error = dasd_eckd_examine_error,
1639         .erp_action = dasd_eckd_erp_action,
1640         .erp_postaction = dasd_eckd_erp_postaction,
1641         .build_cp = dasd_eckd_build_cp,
1642         .free_cp = dasd_eckd_free_cp,
1643         .dump_sense = dasd_eckd_dump_sense,
1644         .fill_info = dasd_eckd_fill_info,
1645 };
1646
1647 static int __init
1648 dasd_eckd_init(void)
1649 {
1650         int ret;
1651
1652         dasd_ioctl_no_register(THIS_MODULE, BIODASDGATTR,
1653                                dasd_eckd_get_attrib);
1654         dasd_ioctl_no_register(THIS_MODULE, BIODASDSATTR,
1655                                dasd_eckd_set_attrib);
1656         dasd_ioctl_no_register(THIS_MODULE, BIODASDPSRD,
1657                                dasd_eckd_performance);
1658         dasd_ioctl_no_register(THIS_MODULE, BIODASDRLSE,
1659                                dasd_eckd_release);
1660         dasd_ioctl_no_register(THIS_MODULE, BIODASDRSRV,
1661                                dasd_eckd_reserve);
1662         dasd_ioctl_no_register(THIS_MODULE, BIODASDSLCK,
1663                                dasd_eckd_steal_lock);
1664
1665         ASCEBC(dasd_eckd_discipline.ebcname, 4);
1666
1667         ret = ccw_driver_register(&dasd_eckd_driver);
1668         if (ret) {
1669                 dasd_ioctl_no_unregister(THIS_MODULE, BIODASDGATTR,
1670                                          dasd_eckd_get_attrib);
1671                 dasd_ioctl_no_unregister(THIS_MODULE, BIODASDSATTR,
1672                                          dasd_eckd_set_attrib);
1673                 dasd_ioctl_no_unregister(THIS_MODULE, BIODASDPSRD,
1674                                          dasd_eckd_performance);
1675                 dasd_ioctl_no_unregister(THIS_MODULE, BIODASDRLSE,
1676                                          dasd_eckd_release);
1677                 dasd_ioctl_no_unregister(THIS_MODULE, BIODASDRSRV,
1678                                          dasd_eckd_reserve);
1679                 dasd_ioctl_no_unregister(THIS_MODULE, BIODASDSLCK,
1680                                          dasd_eckd_steal_lock);
1681                 return ret;
1682         }
1683
1684         dasd_generic_auto_online(&dasd_eckd_driver);
1685         return 0;
1686 }
1687
1688 static void __exit
1689 dasd_eckd_cleanup(void)
1690 {
1691         ccw_driver_unregister(&dasd_eckd_driver);
1692
1693         dasd_ioctl_no_unregister(THIS_MODULE, BIODASDGATTR,
1694                                  dasd_eckd_get_attrib);
1695         dasd_ioctl_no_unregister(THIS_MODULE, BIODASDSATTR,
1696                                  dasd_eckd_set_attrib);
1697         dasd_ioctl_no_unregister(THIS_MODULE, BIODASDPSRD,
1698                                  dasd_eckd_performance);
1699         dasd_ioctl_no_unregister(THIS_MODULE, BIODASDRLSE,
1700                                  dasd_eckd_release);
1701         dasd_ioctl_no_unregister(THIS_MODULE, BIODASDRSRV,
1702                                  dasd_eckd_reserve);
1703         dasd_ioctl_no_unregister(THIS_MODULE, BIODASDSLCK,
1704                                  dasd_eckd_steal_lock);
1705 }
1706
1707 module_init(dasd_eckd_init);
1708 module_exit(dasd_eckd_cleanup);
1709
1710 /*
1711  * Overrides for Emacs so that we follow Linus's tabbing style.
1712  * Emacs will notice this stuff at the end of the file and automatically
1713  * adjust the settings for this buffer only.  This must remain at the end
1714  * of the file.
1715  * ---------------------------------------------------------------------------
1716  * Local variables:
1717  * c-indent-level: 4 
1718  * c-brace-imaginary-offset: 0
1719  * c-brace-offset: -4
1720  * c-argdecl-indent: 4
1721  * c-label-offset: -4
1722  * c-continued-statement-offset: 4
1723  * c-continued-brace-offset: 0
1724  * indent-tabs-mode: 1
1725  * tab-width: 8
1726  * End:
1727  */