[PATCH] s390: dasd device identifiers
[linux-2.6.git] / drivers / s390 / block / dasd_eckd.c
1 /* 
2  * File...........: linux/drivers/s390/block/dasd_eckd.c
3  * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
4  *                  Horst Hummel <Horst.Hummel@de.ibm.com> 
5  *                  Carsten Otte <Cotte@de.ibm.com>
6  *                  Martin Schwidefsky <schwidefsky@de.ibm.com>
7  * Bugreports.to..: <Linux390@de.ibm.com>
8  * (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 /*
450  * Generate device unique id that specifies the physical device.
451  */
452 static int
453 dasd_eckd_generate_uid(struct dasd_device *device, struct dasd_uid *uid)
454 {
455         struct dasd_eckd_private *private;
456         struct dasd_eckd_confdata *confdata;
457
458         private = (struct dasd_eckd_private *) device->private;
459         if (!private)
460                 return -ENODEV;
461         confdata = &private->conf_data;
462         if (!confdata)
463                 return -ENODEV;
464
465         memset(uid, 0, sizeof(struct dasd_uid));
466         strncpy(uid->vendor, confdata->ned1.HDA_manufacturer,
467                 sizeof(uid->vendor) - 1);
468         EBCASC(uid->vendor, sizeof(uid->vendor) - 1);
469         strncpy(uid->serial, confdata->ned1.HDA_location,
470                 sizeof(uid->serial) - 1);
471         EBCASC(uid->serial, sizeof(uid->serial) - 1);
472         uid->ssid = confdata->neq.subsystemID;
473         if (confdata->ned2.sneq.flags == 0x40) {
474                 uid->alias = 1;
475                 uid->unit_addr = confdata->ned2.sneq.base_unit_addr;
476         } else
477                 uid->unit_addr = confdata->ned1.unit_addr;
478
479         return 0;
480 }
481
482 static int
483 dasd_eckd_read_conf(struct dasd_device *device)
484 {
485         void *conf_data;
486         int conf_len, conf_data_saved;
487         int rc;
488         __u8 lpm;
489         struct dasd_eckd_private *private;
490         struct dasd_eckd_path *path_data;
491
492         private = (struct dasd_eckd_private *) device->private;
493         path_data = (struct dasd_eckd_path *) &private->path_data;
494         path_data->opm = ccw_device_get_path_mask(device->cdev);
495         lpm = 0x80;
496         conf_data_saved = 0;
497
498         /* get configuration data per operational path */
499         for (lpm = 0x80; lpm; lpm>>= 1) {
500                 if (lpm & path_data->opm){
501                         rc = read_conf_data_lpm(device->cdev, &conf_data,
502                                                 &conf_len, lpm);
503                         if (rc && rc != -EOPNOTSUPP) {  /* -EOPNOTSUPP is ok */
504                                 MESSAGE(KERN_WARNING,
505                                         "Read configuration data returned "
506                                         "error %d", rc);
507                                 return rc;
508                         }
509                         if (conf_data == NULL) {
510                                 MESSAGE(KERN_WARNING, "%s", "No configuration "
511                                         "data retrieved");
512                                 continue;       /* no errror */
513                         }
514                         if (conf_len != sizeof (struct dasd_eckd_confdata)) {
515                                 MESSAGE(KERN_WARNING,
516                                         "sizes of configuration data mismatch"
517                                         "%d (read) vs %ld (expected)",
518                                         conf_len,
519                                         sizeof (struct dasd_eckd_confdata));
520                                 kfree(conf_data);
521                                 continue;       /* no errror */
522                         }
523                         /* save first valid configuration data */
524                         if (!conf_data_saved){
525                                 memcpy(&private->conf_data, conf_data,
526                                        sizeof (struct dasd_eckd_confdata));
527                                 conf_data_saved++;
528                         }
529                         switch (((char *)conf_data)[242] & 0x07){
530                         case 0x02:
531                                 path_data->npm |= lpm;
532                                 break;
533                         case 0x03:
534                                 path_data->ppm |= lpm;
535                                 break;
536                         }
537                         kfree(conf_data);
538                 }
539         }
540         return 0;
541 }
542
543 /*
544  * Check device characteristics.
545  * If the device is accessible using ECKD discipline, the device is enabled.
546  */
547 static int
548 dasd_eckd_check_characteristics(struct dasd_device *device)
549 {
550         struct dasd_eckd_private *private;
551         struct dasd_uid uid;
552         void *rdc_data;
553         int rc;
554
555         private = (struct dasd_eckd_private *) device->private;
556         if (private == NULL) {
557                 private = kmalloc(sizeof(struct dasd_eckd_private),
558                                   GFP_KERNEL | GFP_DMA);
559                 if (private == NULL) {
560                         DEV_MESSAGE(KERN_WARNING, device, "%s",
561                                     "memory allocation failed for private "
562                                     "data");
563                         return -ENOMEM;
564                 }
565                 memset(private, 0, sizeof(struct dasd_eckd_private));
566                 device->private = (void *) private;
567         }
568         /* Invalidate status of initial analysis. */
569         private->init_cqr_status = -1;
570         /* Set default cache operations. */
571         private->attrib.operation = DASD_NORMAL_CACHE;
572         private->attrib.nr_cyl = 0;
573
574         /* Read Device Characteristics */
575         rdc_data = (void *) &(private->rdc_data);
576         memset(rdc_data, 0, sizeof(rdc_data));
577         rc = read_dev_chars(device->cdev, &rdc_data, 64);
578         if (rc) {
579                 DEV_MESSAGE(KERN_WARNING, device,
580                             "Read device characteristics returned error %d",
581                             rc);
582                 return rc;
583         }
584
585         DEV_MESSAGE(KERN_INFO, device,
586                     "%04X/%02X(CU:%04X/%02X) Cyl:%d Head:%d Sec:%d",
587                     private->rdc_data.dev_type,
588                     private->rdc_data.dev_model,
589                     private->rdc_data.cu_type,
590                     private->rdc_data.cu_model.model,
591                     private->rdc_data.no_cyl,
592                     private->rdc_data.trk_per_cyl,
593                     private->rdc_data.sec_per_trk);
594
595         /* Read Configuration Data */
596         rc = dasd_eckd_read_conf (device);
597         if (rc)
598                 return rc;
599
600         /* Generate device unique id and register in devmap */
601         rc = dasd_eckd_generate_uid(device, &uid);
602         if (rc)
603                 return rc;
604
605         rc = dasd_set_uid(device->cdev, &uid);
606
607         return rc;
608 }
609
610 static struct dasd_ccw_req *
611 dasd_eckd_analysis_ccw(struct dasd_device *device)
612 {
613         struct dasd_eckd_private *private;
614         struct eckd_count *count_data;
615         struct LO_eckd_data *LO_data;
616         struct dasd_ccw_req *cqr;
617         struct ccw1 *ccw;
618         int cplength, datasize;
619         int i;
620
621         private = (struct dasd_eckd_private *) device->private;
622
623         cplength = 8;
624         datasize = sizeof(struct DE_eckd_data) + 2*sizeof(struct LO_eckd_data);
625         cqr = dasd_smalloc_request(dasd_eckd_discipline.name,
626                                    cplength, datasize, device);
627         if (IS_ERR(cqr))
628                 return cqr;
629         ccw = cqr->cpaddr;
630         /* Define extent for the first 3 tracks. */
631         define_extent(ccw++, cqr->data, 0, 2,
632                       DASD_ECKD_CCW_READ_COUNT, device);
633         LO_data = cqr->data + sizeof (struct DE_eckd_data);
634         /* Locate record for the first 4 records on track 0. */
635         ccw[-1].flags |= CCW_FLAG_CC;
636         locate_record(ccw++, LO_data++, 0, 0, 4,
637                       DASD_ECKD_CCW_READ_COUNT, device, 0);
638
639         count_data = private->count_area;
640         for (i = 0; i < 4; i++) {
641                 ccw[-1].flags |= CCW_FLAG_CC;
642                 ccw->cmd_code = DASD_ECKD_CCW_READ_COUNT;
643                 ccw->flags = 0;
644                 ccw->count = 8;
645                 ccw->cda = (__u32)(addr_t) count_data;
646                 ccw++;
647                 count_data++;
648         }
649
650         /* Locate record for the first record on track 2. */
651         ccw[-1].flags |= CCW_FLAG_CC;
652         locate_record(ccw++, LO_data++, 2, 0, 1,
653                       DASD_ECKD_CCW_READ_COUNT, device, 0);
654         /* Read count ccw. */
655         ccw[-1].flags |= CCW_FLAG_CC;
656         ccw->cmd_code = DASD_ECKD_CCW_READ_COUNT;
657         ccw->flags = 0;
658         ccw->count = 8;
659         ccw->cda = (__u32)(addr_t) count_data;
660
661         cqr->device = device;
662         cqr->retries = 0;
663         cqr->buildclk = get_clock();
664         cqr->status = DASD_CQR_FILLED;
665         return cqr;
666 }
667
668 /*
669  * This is the callback function for the init_analysis cqr. It saves
670  * the status of the initial analysis ccw before it frees it and kicks
671  * the device to continue the startup sequence. This will call
672  * dasd_eckd_do_analysis again (if the devices has not been marked
673  * for deletion in the meantime).
674  */
675 static void
676 dasd_eckd_analysis_callback(struct dasd_ccw_req *init_cqr, void *data)
677 {
678         struct dasd_eckd_private *private;
679         struct dasd_device *device;
680
681         device = init_cqr->device;
682         private = (struct dasd_eckd_private *) device->private;
683         private->init_cqr_status = init_cqr->status;
684         dasd_sfree_request(init_cqr, device);
685         dasd_kick_device(device);
686 }
687
688 static int
689 dasd_eckd_start_analysis(struct dasd_device *device)
690 {
691         struct dasd_eckd_private *private;
692         struct dasd_ccw_req *init_cqr;
693
694         private = (struct dasd_eckd_private *) device->private;
695         init_cqr = dasd_eckd_analysis_ccw(device);
696         if (IS_ERR(init_cqr))
697                 return PTR_ERR(init_cqr);
698         init_cqr->callback = dasd_eckd_analysis_callback;
699         init_cqr->callback_data = NULL;
700         init_cqr->expires = 5*HZ;
701         dasd_add_request_head(init_cqr);
702         return -EAGAIN;
703 }
704
705 static int
706 dasd_eckd_end_analysis(struct dasd_device *device)
707 {
708         struct dasd_eckd_private *private;
709         struct eckd_count *count_area;
710         unsigned int sb, blk_per_trk;
711         int status, i;
712
713         private = (struct dasd_eckd_private *) device->private;
714         status = private->init_cqr_status;
715         private->init_cqr_status = -1;
716         if (status != DASD_CQR_DONE) {
717                 DEV_MESSAGE(KERN_WARNING, device, "%s",
718                             "volume analysis returned unformatted disk");
719                 return -EMEDIUMTYPE;
720         }
721
722         private->uses_cdl = 1;
723         /* Calculate number of blocks/records per track. */
724         blk_per_trk = recs_per_track(&private->rdc_data, 0, device->bp_block);
725         /* Check Track 0 for Compatible Disk Layout */
726         count_area = NULL;
727         for (i = 0; i < 3; i++) {
728                 if (private->count_area[i].kl != 4 ||
729                     private->count_area[i].dl != dasd_eckd_cdl_reclen(i) - 4) {
730                         private->uses_cdl = 0;
731                         break;
732                 }
733         }
734         if (i == 3)
735                 count_area = &private->count_area[4];
736
737         if (private->uses_cdl == 0) {
738                 for (i = 0; i < 5; i++) {
739                         if ((private->count_area[i].kl != 0) ||
740                             (private->count_area[i].dl !=
741                              private->count_area[0].dl))
742                                 break;
743                 }
744                 if (i == 5)
745                         count_area = &private->count_area[0];
746         } else {
747                 if (private->count_area[3].record == 1)
748                         DEV_MESSAGE(KERN_WARNING, device, "%s",
749                                     "Trk 0: no records after VTOC!");
750         }
751         if (count_area != NULL && count_area->kl == 0) {
752                 /* we found notthing violating our disk layout */
753                 if (dasd_check_blocksize(count_area->dl) == 0)
754                         device->bp_block = count_area->dl;
755         }
756         if (device->bp_block == 0) {
757                 DEV_MESSAGE(KERN_WARNING, device, "%s",
758                             "Volume has incompatible disk layout");
759                 return -EMEDIUMTYPE;
760         }
761         device->s2b_shift = 0;  /* bits to shift 512 to get a block */
762         for (sb = 512; sb < device->bp_block; sb = sb << 1)
763                 device->s2b_shift++;
764
765         blk_per_trk = recs_per_track(&private->rdc_data, 0, device->bp_block);
766         device->blocks = (private->rdc_data.no_cyl *
767                           private->rdc_data.trk_per_cyl *
768                           blk_per_trk);
769
770         DEV_MESSAGE(KERN_INFO, device,
771                     "(%dkB blks): %dkB at %dkB/trk %s",
772                     (device->bp_block >> 10),
773                     ((private->rdc_data.no_cyl *
774                       private->rdc_data.trk_per_cyl *
775                       blk_per_trk * (device->bp_block >> 9)) >> 1),
776                     ((blk_per_trk * device->bp_block) >> 10), 
777                     private->uses_cdl ?
778                     "compatible disk layout" : "linux disk layout");
779
780         return 0;
781 }
782
783 static int
784 dasd_eckd_do_analysis(struct dasd_device *device)
785 {
786         struct dasd_eckd_private *private;
787
788         private = (struct dasd_eckd_private *) device->private;
789         if (private->init_cqr_status < 0)
790                 return dasd_eckd_start_analysis(device);
791         else
792                 return dasd_eckd_end_analysis(device);
793 }
794
795 static int
796 dasd_eckd_fill_geometry(struct dasd_device *device, struct hd_geometry *geo)
797 {
798         struct dasd_eckd_private *private;
799
800         private = (struct dasd_eckd_private *) device->private;
801         if (dasd_check_blocksize(device->bp_block) == 0) {
802                 geo->sectors = recs_per_track(&private->rdc_data,
803                                               0, device->bp_block);
804         }
805         geo->cylinders = private->rdc_data.no_cyl;
806         geo->heads = private->rdc_data.trk_per_cyl;
807         return 0;
808 }
809
810 static struct dasd_ccw_req *
811 dasd_eckd_format_device(struct dasd_device * device,
812                         struct format_data_t * fdata)
813 {
814         struct dasd_eckd_private *private;
815         struct dasd_ccw_req *fcp;
816         struct eckd_count *ect;
817         struct ccw1 *ccw;
818         void *data;
819         int rpt, cyl, head;
820         int cplength, datasize;
821         int i;
822
823         private = (struct dasd_eckd_private *) device->private;
824         rpt = recs_per_track(&private->rdc_data, 0, fdata->blksize);
825         cyl = fdata->start_unit / private->rdc_data.trk_per_cyl;
826         head = fdata->start_unit % private->rdc_data.trk_per_cyl;
827
828         /* Sanity checks. */
829         if (fdata->start_unit >=
830             (private->rdc_data.no_cyl * private->rdc_data.trk_per_cyl)) {
831                 DEV_MESSAGE(KERN_INFO, device, "Track no %d too big!",
832                             fdata->start_unit);
833                 return ERR_PTR(-EINVAL);
834         }
835         if (fdata->start_unit > fdata->stop_unit) {
836                 DEV_MESSAGE(KERN_INFO, device, "Track %d reached! ending.",
837                             fdata->start_unit);
838                 return ERR_PTR(-EINVAL);
839         }
840         if (dasd_check_blocksize(fdata->blksize) != 0) {
841                 DEV_MESSAGE(KERN_WARNING, device,
842                             "Invalid blocksize %d...terminating!",
843                             fdata->blksize);
844                 return ERR_PTR(-EINVAL);
845         }
846
847         /*
848          * fdata->intensity is a bit string that tells us what to do:
849          *   Bit 0: write record zero
850          *   Bit 1: write home address, currently not supported
851          *   Bit 2: invalidate tracks
852          *   Bit 3: use OS/390 compatible disk layout (cdl)
853          * Only some bit combinations do make sense.
854          */
855         switch (fdata->intensity) {
856         case 0x00:      /* Normal format */
857         case 0x08:      /* Normal format, use cdl. */
858                 cplength = 2 + rpt;
859                 datasize = sizeof(struct DE_eckd_data) +
860                         sizeof(struct LO_eckd_data) +
861                         rpt * sizeof(struct eckd_count);
862                 break;
863         case 0x01:      /* Write record zero and format track. */
864         case 0x09:      /* Write record zero and format track, use cdl. */
865                 cplength = 3 + rpt;
866                 datasize = sizeof(struct DE_eckd_data) +
867                         sizeof(struct LO_eckd_data) +
868                         sizeof(struct eckd_count) +
869                         rpt * sizeof(struct eckd_count);
870                 break;
871         case 0x04:      /* Invalidate track. */
872         case 0x0c:      /* Invalidate track, use cdl. */
873                 cplength = 3;
874                 datasize = sizeof(struct DE_eckd_data) +
875                         sizeof(struct LO_eckd_data) +
876                         sizeof(struct eckd_count);
877                 break;
878         default:
879                 DEV_MESSAGE(KERN_WARNING, device, "Invalid flags 0x%x.",
880                             fdata->intensity);
881                 return ERR_PTR(-EINVAL);
882         }
883         /* Allocate the format ccw request. */
884         fcp = dasd_smalloc_request(dasd_eckd_discipline.name,
885                                    cplength, datasize, device);
886         if (IS_ERR(fcp))
887                 return fcp;
888
889         data = fcp->data;
890         ccw = fcp->cpaddr;
891
892         switch (fdata->intensity & ~0x08) {
893         case 0x00: /* Normal format. */
894                 define_extent(ccw++, (struct DE_eckd_data *) data,
895                               fdata->start_unit, fdata->start_unit,
896                               DASD_ECKD_CCW_WRITE_CKD, device);
897                 data += sizeof(struct DE_eckd_data);
898                 ccw[-1].flags |= CCW_FLAG_CC;
899                 locate_record(ccw++, (struct LO_eckd_data *) data,
900                               fdata->start_unit, 0, rpt,
901                               DASD_ECKD_CCW_WRITE_CKD, device,
902                               fdata->blksize);
903                 data += sizeof(struct LO_eckd_data);
904                 break;
905         case 0x01: /* Write record zero + format track. */
906                 define_extent(ccw++, (struct DE_eckd_data *) data,
907                               fdata->start_unit, fdata->start_unit,
908                               DASD_ECKD_CCW_WRITE_RECORD_ZERO,
909                               device);
910                 data += sizeof(struct DE_eckd_data);
911                 ccw[-1].flags |= CCW_FLAG_CC;
912                 locate_record(ccw++, (struct LO_eckd_data *) data,
913                               fdata->start_unit, 0, rpt + 1,
914                               DASD_ECKD_CCW_WRITE_RECORD_ZERO, device,
915                               device->bp_block);
916                 data += sizeof(struct LO_eckd_data);
917                 break;
918         case 0x04: /* Invalidate track. */
919                 define_extent(ccw++, (struct DE_eckd_data *) data,
920                               fdata->start_unit, fdata->start_unit,
921                               DASD_ECKD_CCW_WRITE_CKD, device);
922                 data += sizeof(struct DE_eckd_data);
923                 ccw[-1].flags |= CCW_FLAG_CC;
924                 locate_record(ccw++, (struct LO_eckd_data *) data,
925                               fdata->start_unit, 0, 1,
926                               DASD_ECKD_CCW_WRITE_CKD, device, 8);
927                 data += sizeof(struct LO_eckd_data);
928                 break;
929         }
930         if (fdata->intensity & 0x01) {  /* write record zero */
931                 ect = (struct eckd_count *) data;
932                 data += sizeof(struct eckd_count);
933                 ect->cyl = cyl;
934                 ect->head = head;
935                 ect->record = 0;
936                 ect->kl = 0;
937                 ect->dl = 8;
938                 ccw[-1].flags |= CCW_FLAG_CC;
939                 ccw->cmd_code = DASD_ECKD_CCW_WRITE_RECORD_ZERO;
940                 ccw->flags = CCW_FLAG_SLI;
941                 ccw->count = 8;
942                 ccw->cda = (__u32)(addr_t) ect;
943                 ccw++;
944         }
945         if ((fdata->intensity & ~0x08) & 0x04) {        /* erase track */
946                 ect = (struct eckd_count *) data;
947                 data += sizeof(struct eckd_count);
948                 ect->cyl = cyl;
949                 ect->head = head;
950                 ect->record = 1;
951                 ect->kl = 0;
952                 ect->dl = 0;
953                 ccw[-1].flags |= CCW_FLAG_CC;
954                 ccw->cmd_code = DASD_ECKD_CCW_WRITE_CKD;
955                 ccw->flags = CCW_FLAG_SLI;
956                 ccw->count = 8;
957                 ccw->cda = (__u32)(addr_t) ect;
958         } else {                /* write remaining records */
959                 for (i = 0; i < rpt; i++) {
960                         ect = (struct eckd_count *) data;
961                         data += sizeof(struct eckd_count);
962                         ect->cyl = cyl;
963                         ect->head = head;
964                         ect->record = i + 1;
965                         ect->kl = 0;
966                         ect->dl = fdata->blksize;
967                         /* Check for special tracks 0-1 when formatting CDL */
968                         if ((fdata->intensity & 0x08) &&
969                             fdata->start_unit == 0) {
970                                 if (i < 3) {
971                                         ect->kl = 4;
972                                         ect->dl = sizes_trk0[i] - 4;
973                                 } 
974                         }
975                         if ((fdata->intensity & 0x08) &&
976                             fdata->start_unit == 1) {
977                                 ect->kl = 44;
978                                 ect->dl = LABEL_SIZE - 44;
979                         }
980                         ccw[-1].flags |= CCW_FLAG_CC;
981                         ccw->cmd_code = DASD_ECKD_CCW_WRITE_CKD;
982                         ccw->flags = CCW_FLAG_SLI;
983                         ccw->count = 8;
984                         ccw->cda = (__u32)(addr_t) ect;
985                         ccw++;
986                 }
987         }
988         fcp->device = device;
989         fcp->retries = 2;       /* set retry counter to enable ERP */
990         fcp->buildclk = get_clock();
991         fcp->status = DASD_CQR_FILLED;
992         return fcp;
993 }
994
995 static dasd_era_t
996 dasd_eckd_examine_error(struct dasd_ccw_req * cqr, struct irb * irb)
997 {
998         struct dasd_device *device = (struct dasd_device *) cqr->device;
999         struct ccw_device *cdev = device->cdev;
1000
1001         if (irb->scsw.cstat == 0x00 &&
1002             irb->scsw.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END))
1003                 return dasd_era_none;
1004
1005         switch (cdev->id.cu_type) {
1006         case 0x3990:
1007         case 0x2105:
1008         case 0x2107:
1009         case 0x1750:
1010                 return dasd_3990_erp_examine(cqr, irb);
1011         case 0x9343:
1012                 return dasd_9343_erp_examine(cqr, irb);
1013         case 0x3880:
1014         default:
1015                 DEV_MESSAGE(KERN_WARNING, device, "%s",
1016                             "default (unknown CU type) - RECOVERABLE return");
1017                 return dasd_era_recover;
1018         }
1019 }
1020
1021 static dasd_erp_fn_t
1022 dasd_eckd_erp_action(struct dasd_ccw_req * cqr)
1023 {
1024         struct dasd_device *device = (struct dasd_device *) cqr->device;
1025         struct ccw_device *cdev = device->cdev;
1026
1027         switch (cdev->id.cu_type) {
1028         case 0x3990:
1029         case 0x2105:
1030         case 0x2107:
1031         case 0x1750:
1032                 return dasd_3990_erp_action;
1033         case 0x9343:
1034         case 0x3880:
1035         default:
1036                 return dasd_default_erp_action;
1037         }
1038 }
1039
1040 static dasd_erp_fn_t
1041 dasd_eckd_erp_postaction(struct dasd_ccw_req * cqr)
1042 {
1043         return dasd_default_erp_postaction;
1044 }
1045
1046 static struct dasd_ccw_req *
1047 dasd_eckd_build_cp(struct dasd_device * device, struct request *req)
1048 {
1049         struct dasd_eckd_private *private;
1050         unsigned long *idaws;
1051         struct LO_eckd_data *LO_data;
1052         struct dasd_ccw_req *cqr;
1053         struct ccw1 *ccw;
1054         struct bio *bio;
1055         struct bio_vec *bv;
1056         char *dst;
1057         unsigned int blksize, blk_per_trk, off;
1058         int count, cidaw, cplength, datasize;
1059         sector_t recid, first_rec, last_rec;
1060         sector_t first_trk, last_trk;
1061         unsigned int first_offs, last_offs;
1062         unsigned char cmd, rcmd;
1063         int i;
1064
1065         private = (struct dasd_eckd_private *) device->private;
1066         if (rq_data_dir(req) == READ)
1067                 cmd = DASD_ECKD_CCW_READ_MT;
1068         else if (rq_data_dir(req) == WRITE)
1069                 cmd = DASD_ECKD_CCW_WRITE_MT;
1070         else
1071                 return ERR_PTR(-EINVAL);
1072         /* Calculate number of blocks/records per track. */
1073         blksize = device->bp_block;
1074         blk_per_trk = recs_per_track(&private->rdc_data, 0, blksize);
1075         /* Calculate record id of first and last block. */
1076         first_rec = first_trk = req->sector >> device->s2b_shift;
1077         first_offs = sector_div(first_trk, blk_per_trk);
1078         last_rec = last_trk =
1079                 (req->sector + req->nr_sectors - 1) >> device->s2b_shift;
1080         last_offs = sector_div(last_trk, blk_per_trk);
1081         /* Check struct bio and count the number of blocks for the request. */
1082         count = 0;
1083         cidaw = 0;
1084         rq_for_each_bio(bio, req) {
1085                 bio_for_each_segment(bv, bio, i) {
1086                         if (bv->bv_len & (blksize - 1))
1087                                 /* Eckd can only do full blocks. */
1088                                 return ERR_PTR(-EINVAL);
1089                         count += bv->bv_len >> (device->s2b_shift + 9);
1090 #if defined(CONFIG_64BIT)
1091                         if (idal_is_needed (page_address(bv->bv_page),
1092                                             bv->bv_len))
1093                                 cidaw += bv->bv_len >> (device->s2b_shift + 9);
1094 #endif
1095                 }
1096         }
1097         /* Paranoia. */
1098         if (count != last_rec - first_rec + 1)
1099                 return ERR_PTR(-EINVAL);
1100         /* 1x define extent + 1x locate record + number of blocks */
1101         cplength = 2 + count;
1102         /* 1x define extent + 1x locate record + cidaws*sizeof(long) */
1103         datasize = sizeof(struct DE_eckd_data) + sizeof(struct LO_eckd_data) +
1104                 cidaw * sizeof(unsigned long);
1105         /* Find out the number of additional locate record ccws for cdl. */
1106         if (private->uses_cdl && first_rec < 2*blk_per_trk) {
1107                 if (last_rec >= 2*blk_per_trk)
1108                         count = 2*blk_per_trk - first_rec;
1109                 cplength += count;
1110                 datasize += count*sizeof(struct LO_eckd_data);
1111         }
1112         /* Allocate the ccw request. */
1113         cqr = dasd_smalloc_request(dasd_eckd_discipline.name,
1114                                    cplength, datasize, device);
1115         if (IS_ERR(cqr))
1116                 return cqr;
1117         ccw = cqr->cpaddr;
1118         /* First ccw is define extent. */
1119         define_extent(ccw++, cqr->data, first_trk, last_trk, cmd, device);
1120         /* Build locate_record+read/write/ccws. */
1121         idaws = (unsigned long *) (cqr->data + sizeof(struct DE_eckd_data));
1122         LO_data = (struct LO_eckd_data *) (idaws + cidaw);
1123         recid = first_rec;
1124         if (private->uses_cdl == 0 || recid > 2*blk_per_trk) {
1125                 /* Only standard blocks so there is just one locate record. */
1126                 ccw[-1].flags |= CCW_FLAG_CC;
1127                 locate_record(ccw++, LO_data++, first_trk, first_offs + 1,
1128                               last_rec - recid + 1, cmd, device, blksize);
1129         }
1130         rq_for_each_bio(bio, req) bio_for_each_segment(bv, bio, i) {
1131                 dst = page_address(bv->bv_page) + bv->bv_offset;
1132                 if (dasd_page_cache) {
1133                         char *copy = kmem_cache_alloc(dasd_page_cache,
1134                                                       SLAB_DMA | __GFP_NOWARN);
1135                         if (copy && rq_data_dir(req) == WRITE)
1136                                 memcpy(copy + bv->bv_offset, dst, bv->bv_len);
1137                         if (copy)
1138                                 dst = copy + bv->bv_offset;
1139                 }
1140                 for (off = 0; off < bv->bv_len; off += blksize) {
1141                         sector_t trkid = recid;
1142                         unsigned int recoffs = sector_div(trkid, blk_per_trk);
1143                         rcmd = cmd;
1144                         count = blksize;
1145                         /* Locate record for cdl special block ? */
1146                         if (private->uses_cdl && recid < 2*blk_per_trk) {
1147                                 if (dasd_eckd_cdl_special(blk_per_trk, recid)){
1148                                         rcmd |= 0x8;
1149                                         count = dasd_eckd_cdl_reclen(recid);
1150                                         if (count < blksize &&
1151                                             rq_data_dir(req) == READ)
1152                                                 memset(dst + count, 0xe5,
1153                                                        blksize - count);
1154                                 }
1155                                 ccw[-1].flags |= CCW_FLAG_CC;
1156                                 locate_record(ccw++, LO_data++,
1157                                               trkid, recoffs + 1,
1158                                               1, rcmd, device, count);
1159                         }
1160                         /* Locate record for standard blocks ? */
1161                         if (private->uses_cdl && recid == 2*blk_per_trk) {
1162                                 ccw[-1].flags |= CCW_FLAG_CC;
1163                                 locate_record(ccw++, LO_data++,
1164                                               trkid, recoffs + 1,
1165                                               last_rec - recid + 1,
1166                                               cmd, device, count);
1167                         }
1168                         /* Read/write ccw. */
1169                         ccw[-1].flags |= CCW_FLAG_CC;
1170                         ccw->cmd_code = rcmd;
1171                         ccw->count = count;
1172                         if (idal_is_needed(dst, blksize)) {
1173                                 ccw->cda = (__u32)(addr_t) idaws;
1174                                 ccw->flags = CCW_FLAG_IDA;
1175                                 idaws = idal_create_words(idaws, dst, blksize);
1176                         } else {
1177                                 ccw->cda = (__u32)(addr_t) dst;
1178                                 ccw->flags = 0;
1179                         }
1180                         ccw++;
1181                         dst += blksize;
1182                         recid++;
1183                 }
1184         }
1185         if (req->flags & REQ_FAILFAST)
1186                 set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
1187         cqr->device = device;
1188         cqr->expires = 5 * 60 * HZ;     /* 5 minutes */
1189         cqr->lpm = private->path_data.ppm;
1190         cqr->retries = 256;
1191         cqr->buildclk = get_clock();
1192         cqr->status = DASD_CQR_FILLED;
1193         return cqr;
1194 }
1195
1196 static int
1197 dasd_eckd_free_cp(struct dasd_ccw_req *cqr, struct request *req)
1198 {
1199         struct dasd_eckd_private *private;
1200         struct ccw1 *ccw;
1201         struct bio *bio;
1202         struct bio_vec *bv;
1203         char *dst, *cda;
1204         unsigned int blksize, blk_per_trk, off;
1205         sector_t recid;
1206         int i, status;
1207
1208         if (!dasd_page_cache)
1209                 goto out;
1210         private = (struct dasd_eckd_private *) cqr->device->private;
1211         blksize = cqr->device->bp_block;
1212         blk_per_trk = recs_per_track(&private->rdc_data, 0, blksize);
1213         recid = req->sector >> cqr->device->s2b_shift;
1214         ccw = cqr->cpaddr;
1215         /* Skip over define extent & locate record. */
1216         ccw++;
1217         if (private->uses_cdl == 0 || recid > 2*blk_per_trk)
1218                 ccw++;
1219         rq_for_each_bio(bio, req) bio_for_each_segment(bv, bio, i) {
1220                 dst = page_address(bv->bv_page) + bv->bv_offset;
1221                 for (off = 0; off < bv->bv_len; off += blksize) {
1222                         /* Skip locate record. */
1223                         if (private->uses_cdl && recid <= 2*blk_per_trk)
1224                                 ccw++;
1225                         if (dst) {
1226                                 if (ccw->flags & CCW_FLAG_IDA)
1227                                         cda = *((char **)((addr_t) ccw->cda));
1228                                 else
1229                                         cda = (char *)((addr_t) ccw->cda);
1230                                 if (dst != cda) {
1231                                         if (rq_data_dir(req) == READ)
1232                                                 memcpy(dst, cda, bv->bv_len);
1233                                         kmem_cache_free(dasd_page_cache,
1234                                             (void *)((addr_t)cda & PAGE_MASK));
1235                                 }
1236                                 dst = NULL;
1237                         }
1238                         ccw++;
1239                         recid++;
1240                 }
1241         }
1242 out:
1243         status = cqr->status == DASD_CQR_DONE;
1244         dasd_sfree_request(cqr, cqr->device);
1245         return status;
1246 }
1247
1248 static int
1249 dasd_eckd_fill_info(struct dasd_device * device,
1250                     struct dasd_information2_t * info)
1251 {
1252         struct dasd_eckd_private *private;
1253
1254         private = (struct dasd_eckd_private *) device->private;
1255         info->label_block = 2;
1256         info->FBA_layout = private->uses_cdl ? 0 : 1;
1257         info->format = private->uses_cdl ? DASD_FORMAT_CDL : DASD_FORMAT_LDL;
1258         info->characteristics_size = sizeof(struct dasd_eckd_characteristics);
1259         memcpy(info->characteristics, &private->rdc_data,
1260                sizeof(struct dasd_eckd_characteristics));
1261         info->confdata_size = sizeof (struct dasd_eckd_confdata);
1262         memcpy(info->configuration_data, &private->conf_data,
1263                sizeof (struct dasd_eckd_confdata));
1264         return 0;
1265 }
1266
1267 /*
1268  * SECTION: ioctl functions for eckd devices.
1269  */
1270
1271 /*
1272  * Release device ioctl.
1273  * Buils a channel programm to releases a prior reserved 
1274  * (see dasd_eckd_reserve) device.
1275  */
1276 static int
1277 dasd_eckd_release(struct dasd_device *device)
1278 {
1279         struct dasd_ccw_req *cqr;
1280         int rc;
1281
1282         if (!capable(CAP_SYS_ADMIN))
1283                 return -EACCES;
1284
1285         cqr = dasd_smalloc_request(dasd_eckd_discipline.name,
1286                                    1, 32, device);
1287         if (IS_ERR(cqr)) {
1288                 DEV_MESSAGE(KERN_WARNING, device, "%s",
1289                             "Could not allocate initialization request");
1290                 return PTR_ERR(cqr);
1291         }
1292         cqr->cpaddr->cmd_code = DASD_ECKD_CCW_RELEASE;
1293         cqr->cpaddr->flags |= CCW_FLAG_SLI;
1294         cqr->cpaddr->count = 32;
1295         cqr->cpaddr->cda = (__u32)(addr_t) cqr->data;
1296         cqr->device = device;
1297         clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1298         set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
1299         cqr->retries = 0;
1300         cqr->expires = 2 * HZ;
1301         cqr->buildclk = get_clock();
1302         cqr->status = DASD_CQR_FILLED;
1303
1304         rc = dasd_sleep_on_immediatly(cqr);
1305
1306         dasd_sfree_request(cqr, cqr->device);
1307         return rc;
1308 }
1309
1310 /*
1311  * Reserve device ioctl.
1312  * Options are set to 'synchronous wait for interrupt' and
1313  * 'timeout the request'. This leads to a terminate IO if 
1314  * the interrupt is outstanding for a certain time. 
1315  */
1316 static int
1317 dasd_eckd_reserve(struct dasd_device *device)
1318 {
1319         struct dasd_ccw_req *cqr;
1320         int rc;
1321
1322         if (!capable(CAP_SYS_ADMIN))
1323                 return -EACCES;
1324
1325         cqr = dasd_smalloc_request(dasd_eckd_discipline.name,
1326                                    1, 32, device);
1327         if (IS_ERR(cqr)) {
1328                 DEV_MESSAGE(KERN_WARNING, device, "%s",
1329                             "Could not allocate initialization request");
1330                 return PTR_ERR(cqr);
1331         }
1332         cqr->cpaddr->cmd_code = DASD_ECKD_CCW_RESERVE;
1333         cqr->cpaddr->flags |= CCW_FLAG_SLI;
1334         cqr->cpaddr->count = 32;
1335         cqr->cpaddr->cda = (__u32)(addr_t) cqr->data;
1336         cqr->device = device;
1337         clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1338         set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
1339         cqr->retries = 0;
1340         cqr->expires = 2 * HZ;
1341         cqr->buildclk = get_clock();
1342         cqr->status = DASD_CQR_FILLED;
1343
1344         rc = dasd_sleep_on_immediatly(cqr);
1345
1346         dasd_sfree_request(cqr, cqr->device);
1347         return rc;
1348 }
1349
1350 /*
1351  * Steal lock ioctl - unconditional reserve device.
1352  * Buils a channel programm to break a device's reservation. 
1353  * (unconditional reserve)
1354  */
1355 static int
1356 dasd_eckd_steal_lock(struct dasd_device *device)
1357 {
1358         struct dasd_ccw_req *cqr;
1359         int rc;
1360
1361         if (!capable(CAP_SYS_ADMIN))
1362                 return -EACCES;
1363
1364         cqr = dasd_smalloc_request(dasd_eckd_discipline.name,
1365                                    1, 32, device);
1366         if (IS_ERR(cqr)) {
1367                 DEV_MESSAGE(KERN_WARNING, device, "%s",
1368                             "Could not allocate initialization request");
1369                 return PTR_ERR(cqr);
1370         }
1371         cqr->cpaddr->cmd_code = DASD_ECKD_CCW_SLCK;
1372         cqr->cpaddr->flags |= CCW_FLAG_SLI;
1373         cqr->cpaddr->count = 32;
1374         cqr->cpaddr->cda = (__u32)(addr_t) cqr->data;
1375         cqr->device = device;
1376         clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1377         set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
1378         cqr->retries = 0;
1379         cqr->expires = 2 * HZ;
1380         cqr->buildclk = get_clock();
1381         cqr->status = DASD_CQR_FILLED;
1382
1383         rc = dasd_sleep_on_immediatly(cqr);
1384
1385         dasd_sfree_request(cqr, cqr->device);
1386         return rc;
1387 }
1388
1389 /*
1390  * Read performance statistics
1391  */
1392 static int
1393 dasd_eckd_performance(struct dasd_device *device, void __user *argp)
1394 {
1395         struct dasd_psf_prssd_data *prssdp;
1396         struct dasd_rssd_perf_stats_t *stats;
1397         struct dasd_ccw_req *cqr;
1398         struct ccw1 *ccw;
1399         int rc;
1400
1401         cqr = dasd_smalloc_request(dasd_eckd_discipline.name,
1402                                    1 /* PSF */  + 1 /* RSSD */ ,
1403                                    (sizeof (struct dasd_psf_prssd_data) +
1404                                     sizeof (struct dasd_rssd_perf_stats_t)),
1405                                    device);
1406         if (IS_ERR(cqr)) {
1407                 DEV_MESSAGE(KERN_WARNING, device, "%s",
1408                             "Could not allocate initialization request");
1409                 return PTR_ERR(cqr);
1410         }
1411         cqr->device = device;
1412         cqr->retries = 0;
1413         cqr->expires = 10 * HZ;
1414
1415         /* Prepare for Read Subsystem Data */
1416         prssdp = (struct dasd_psf_prssd_data *) cqr->data;
1417         memset(prssdp, 0, sizeof (struct dasd_psf_prssd_data));
1418         prssdp->order = PSF_ORDER_PRSSD;
1419         prssdp->suborder = 0x01;        /* Perfomance Statistics */
1420         prssdp->varies[1] = 0x01;       /* Perf Statistics for the Subsystem */
1421
1422         ccw = cqr->cpaddr;
1423         ccw->cmd_code = DASD_ECKD_CCW_PSF;
1424         ccw->count = sizeof (struct dasd_psf_prssd_data);
1425         ccw->flags |= CCW_FLAG_CC;
1426         ccw->cda = (__u32)(addr_t) prssdp;
1427
1428         /* Read Subsystem Data - Performance Statistics */
1429         stats = (struct dasd_rssd_perf_stats_t *) (prssdp + 1);
1430         memset(stats, 0, sizeof (struct dasd_rssd_perf_stats_t));
1431
1432         ccw++;
1433         ccw->cmd_code = DASD_ECKD_CCW_RSSD;
1434         ccw->count = sizeof (struct dasd_rssd_perf_stats_t);
1435         ccw->cda = (__u32)(addr_t) stats;
1436
1437         cqr->buildclk = get_clock();
1438         cqr->status = DASD_CQR_FILLED;
1439         rc = dasd_sleep_on(cqr);
1440         if (rc == 0) {
1441                 /* Prepare for Read Subsystem Data */
1442                 prssdp = (struct dasd_psf_prssd_data *) cqr->data;
1443                 stats = (struct dasd_rssd_perf_stats_t *) (prssdp + 1);
1444                 if (copy_to_user(argp, stats,
1445                                  sizeof(struct dasd_rssd_perf_stats_t)))
1446                         rc = -EFAULT;
1447         }
1448         dasd_sfree_request(cqr, cqr->device);
1449         return rc;
1450 }
1451
1452 /*
1453  * Get attributes (cache operations)
1454  * Returnes the cache attributes used in Define Extend (DE).
1455  */
1456 static int
1457 dasd_eckd_get_attrib(struct dasd_device *device, void __user *argp)
1458 {
1459         struct dasd_eckd_private *private =
1460                 (struct dasd_eckd_private *)device->private;
1461         struct attrib_data_t attrib = private->attrib;
1462         int rc;
1463
1464         if (!capable(CAP_SYS_ADMIN))
1465                 return -EACCES;
1466         if (!argp)
1467                 return -EINVAL;
1468
1469         rc = 0;
1470         if (copy_to_user(argp, (long *) &attrib,
1471                          sizeof (struct attrib_data_t)))
1472                 rc = -EFAULT;
1473
1474         return rc;
1475 }
1476
1477 /*
1478  * Set attributes (cache operations)
1479  * Stores the attributes for cache operation to be used in Define Extend (DE).
1480  */
1481 static int
1482 dasd_eckd_set_attrib(struct dasd_device *device, void __user *argp)
1483 {
1484         struct dasd_eckd_private *private =
1485                 (struct dasd_eckd_private *)device->private;
1486         struct attrib_data_t attrib;
1487
1488         if (!capable(CAP_SYS_ADMIN))
1489                 return -EACCES;
1490         if (!argp)
1491                 return -EINVAL;
1492
1493         if (copy_from_user(&attrib, argp, sizeof(struct attrib_data_t)))
1494                 return -EFAULT;
1495         private->attrib = attrib;
1496
1497         DEV_MESSAGE(KERN_INFO, device,
1498                     "cache operation mode set to %x (%i cylinder prestage)",
1499                     private->attrib.operation, private->attrib.nr_cyl);
1500         return 0;
1501 }
1502
1503 static int
1504 dasd_eckd_ioctl(struct dasd_device *device, unsigned int cmd, void __user *argp)
1505 {
1506         switch (cmd) {
1507         case BIODASDGATTR:
1508                 return dasd_eckd_get_attrib(device, argp);
1509         case BIODASDSATTR:
1510                 return dasd_eckd_set_attrib(device, argp);
1511         case BIODASDPSRD:
1512                 return dasd_eckd_performance(device, argp);
1513         case BIODASDRLSE:
1514                 return dasd_eckd_release(device);
1515         case BIODASDRSRV:
1516                 return dasd_eckd_reserve(device);
1517         case BIODASDSLCK:
1518                 return dasd_eckd_steal_lock(device);
1519         default:
1520                 return -ENOIOCTLCMD;
1521         }
1522 }
1523
1524 /*
1525  * Print sense data and related channel program.
1526  * Parts are printed because printk buffer is only 1024 bytes.
1527  */
1528 static void
1529 dasd_eckd_dump_sense(struct dasd_device *device, struct dasd_ccw_req * req,
1530                      struct irb *irb)
1531 {
1532         char *page;
1533         struct ccw1 *act, *end, *last;
1534         int len, sl, sct, count;
1535
1536         page = (char *) get_zeroed_page(GFP_ATOMIC);
1537         if (page == NULL) {
1538                 DEV_MESSAGE(KERN_ERR, device, " %s",
1539                             "No memory to dump sense data");
1540                 return;
1541         }
1542         len = sprintf(page, KERN_ERR PRINTK_HEADER
1543                       " I/O status report for device %s:\n",
1544                       device->cdev->dev.bus_id);
1545         len += sprintf(page + len, KERN_ERR PRINTK_HEADER
1546                        " in req: %p CS: 0x%02X DS: 0x%02X\n", req,
1547                        irb->scsw.cstat, irb->scsw.dstat);
1548         len += sprintf(page + len, KERN_ERR PRINTK_HEADER
1549                        " device %s: Failing CCW: %p\n",
1550                        device->cdev->dev.bus_id,
1551                        (void *) (addr_t) irb->scsw.cpa);
1552         if (irb->esw.esw0.erw.cons) {
1553                 for (sl = 0; sl < 4; sl++) {
1554                         len += sprintf(page + len, KERN_ERR PRINTK_HEADER
1555                                        " Sense(hex) %2d-%2d:",
1556                                        (8 * sl), ((8 * sl) + 7));
1557
1558                         for (sct = 0; sct < 8; sct++) {
1559                                 len += sprintf(page + len, " %02x",
1560                                                irb->ecw[8 * sl + sct]);
1561                         }
1562                         len += sprintf(page + len, "\n");
1563                 }
1564
1565                 if (irb->ecw[27] & DASD_SENSE_BIT_0) {
1566                         /* 24 Byte Sense Data */
1567                         len += sprintf(page + len, KERN_ERR PRINTK_HEADER
1568                                        " 24 Byte: %x MSG %x, "
1569                                        "%s MSGb to SYSOP\n",
1570                                        irb->ecw[7] >> 4, irb->ecw[7] & 0x0f,
1571                                        irb->ecw[1] & 0x10 ? "" : "no");
1572                 } else {
1573                         /* 32 Byte Sense Data */
1574                         len += sprintf(page + len, KERN_ERR PRINTK_HEADER
1575                                        " 32 Byte: Format: %x "
1576                                        "Exception class %x\n",
1577                                        irb->ecw[6] & 0x0f, irb->ecw[22] >> 4);
1578                 }
1579         } else {
1580                 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
1581                                " SORRY - NO VALID SENSE AVAILABLE\n");
1582         }
1583         MESSAGE_LOG(KERN_ERR, "%s",
1584                     page + sizeof(KERN_ERR PRINTK_HEADER));
1585
1586         /* dump the Channel Program */
1587         /* print first CCWs (maximum 8) */
1588         act = req->cpaddr;
1589         for (last = act; last->flags & (CCW_FLAG_CC | CCW_FLAG_DC); last++);
1590         end = min(act + 8, last);
1591         len = sprintf(page, KERN_ERR PRINTK_HEADER
1592                       " Related CP in req: %p\n", req);
1593         while (act <= end) {
1594                 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
1595                                " CCW %p: %08X %08X DAT:",
1596                                act, ((int *) act)[0], ((int *) act)[1]);
1597                 for (count = 0; count < 32 && count < act->count;
1598                      count += sizeof(int))
1599                         len += sprintf(page + len, " %08X",
1600                                        ((int *) (addr_t) act->cda)
1601                                        [(count>>2)]);
1602                 len += sprintf(page + len, "\n");
1603                 act++;
1604         }
1605         MESSAGE_LOG(KERN_ERR, "%s",
1606                     page + sizeof(KERN_ERR PRINTK_HEADER));
1607
1608         /* print failing CCW area */
1609         len = 0;
1610         if (act <  ((struct ccw1 *)(addr_t) irb->scsw.cpa) - 2) {
1611                 act = ((struct ccw1 *)(addr_t) irb->scsw.cpa) - 2;
1612                 len += sprintf(page + len, KERN_ERR PRINTK_HEADER "......\n");
1613         }
1614         end = min((struct ccw1 *)(addr_t) irb->scsw.cpa + 2, last);
1615         while (act <= end) {
1616                 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
1617                                " CCW %p: %08X %08X DAT:",
1618                                act, ((int *) act)[0], ((int *) act)[1]);
1619                 for (count = 0; count < 32 && count < act->count;
1620                      count += sizeof(int))
1621                         len += sprintf(page + len, " %08X",
1622                                        ((int *) (addr_t) act->cda)
1623                                        [(count>>2)]);
1624                 len += sprintf(page + len, "\n");
1625                 act++;
1626         }
1627
1628         /* print last CCWs */
1629         if (act <  last - 2) {
1630                 act = last - 2;
1631                 len += sprintf(page + len, KERN_ERR PRINTK_HEADER "......\n");
1632         }
1633         while (act <= last) {
1634                 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
1635                                " CCW %p: %08X %08X DAT:",
1636                                act, ((int *) act)[0], ((int *) act)[1]);
1637                 for (count = 0; count < 32 && count < act->count;
1638                      count += sizeof(int))
1639                         len += sprintf(page + len, " %08X",
1640                                        ((int *) (addr_t) act->cda)
1641                                        [(count>>2)]);
1642                 len += sprintf(page + len, "\n");
1643                 act++;
1644         }
1645         if (len > 0)
1646                 MESSAGE_LOG(KERN_ERR, "%s",
1647                             page + sizeof(KERN_ERR PRINTK_HEADER));
1648         free_page((unsigned long) page);
1649 }
1650
1651 /*
1652  * max_blocks is dependent on the amount of storage that is available
1653  * in the static io buffer for each device. Currently each device has
1654  * 8192 bytes (=2 pages). For 64 bit one dasd_mchunkt_t structure has
1655  * 24 bytes, the struct dasd_ccw_req has 136 bytes and each block can use
1656  * up to 16 bytes (8 for the ccw and 8 for the idal pointer). In
1657  * addition we have one define extent ccw + 16 bytes of data and one
1658  * locate record ccw + 16 bytes of data. That makes:
1659  * (8192 - 24 - 136 - 8 - 16 - 8 - 16) / 16 = 499 blocks at maximum.
1660  * We want to fit two into the available memory so that we can immediately
1661  * start the next request if one finishes off. That makes 249.5 blocks
1662  * for one request. Give a little safety and the result is 240.
1663  */
1664 static struct dasd_discipline dasd_eckd_discipline = {
1665         .owner = THIS_MODULE,
1666         .name = "ECKD",
1667         .ebcname = "ECKD",
1668         .max_blocks = 240,
1669         .check_device = dasd_eckd_check_characteristics,
1670         .do_analysis = dasd_eckd_do_analysis,
1671         .fill_geometry = dasd_eckd_fill_geometry,
1672         .start_IO = dasd_start_IO,
1673         .term_IO = dasd_term_IO,
1674         .format_device = dasd_eckd_format_device,
1675         .examine_error = dasd_eckd_examine_error,
1676         .erp_action = dasd_eckd_erp_action,
1677         .erp_postaction = dasd_eckd_erp_postaction,
1678         .build_cp = dasd_eckd_build_cp,
1679         .free_cp = dasd_eckd_free_cp,
1680         .dump_sense = dasd_eckd_dump_sense,
1681         .fill_info = dasd_eckd_fill_info,
1682         .ioctl = dasd_eckd_ioctl,
1683 };
1684
1685 static int __init
1686 dasd_eckd_init(void)
1687 {
1688         int ret;
1689
1690         ASCEBC(dasd_eckd_discipline.ebcname, 4);
1691
1692         ret = ccw_driver_register(&dasd_eckd_driver);
1693         if (!ret)
1694                 dasd_generic_auto_online(&dasd_eckd_driver);
1695         return ret;
1696 }
1697
1698 static void __exit
1699 dasd_eckd_cleanup(void)
1700 {
1701         ccw_driver_unregister(&dasd_eckd_driver);
1702 }
1703
1704 module_init(dasd_eckd_init);
1705 module_exit(dasd_eckd_cleanup);
1706
1707 /*
1708  * Overrides for Emacs so that we follow Linus's tabbing style.
1709  * Emacs will notice this stuff at the end of the file and automatically
1710  * adjust the settings for this buffer only.  This must remain at the end
1711  * of the file.
1712  * ---------------------------------------------------------------------------
1713  * Local variables:
1714  * c-indent-level: 4 
1715  * c-brace-imaginary-offset: 0
1716  * c-brace-offset: -4
1717  * c-argdecl-indent: 4
1718  * c-label-offset: -4
1719  * c-continued-statement-offset: 4
1720  * c-continued-brace-offset: 0
1721  * indent-tabs-mode: 1
1722  * tab-width: 8
1723  * End:
1724  */