Merge branch 'upstream'
[linux-2.6] / drivers / s390 / block / dasd_fba.c
1 /* 
2  * File...........: linux/drivers/s390/block/dasd_fba.c
3  * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
4  * Bugreports.to..: <Linux390@de.ibm.com>
5  * (C) IBM Corporation, IBM Deutschland Entwicklung GmbH, 1999,2000
6  *
7  */
8
9 #include <linux/config.h>
10 #include <linux/stddef.h>
11 #include <linux/kernel.h>
12 #include <asm/debug.h>
13
14 #include <linux/slab.h>
15 #include <linux/hdreg.h>        /* HDIO_GETGEO                      */
16 #include <linux/bio.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19
20 #include <asm/idals.h>
21 #include <asm/ebcdic.h>
22 #include <asm/io.h>
23 #include <asm/todclk.h>
24 #include <asm/ccwdev.h>
25
26 #include "dasd_int.h"
27 #include "dasd_fba.h"
28
29 #ifdef PRINTK_HEADER
30 #undef PRINTK_HEADER
31 #endif                          /* PRINTK_HEADER */
32 #define PRINTK_HEADER "dasd(fba):"
33
34 #define DASD_FBA_CCW_WRITE 0x41
35 #define DASD_FBA_CCW_READ 0x42
36 #define DASD_FBA_CCW_LOCATE 0x43
37 #define DASD_FBA_CCW_DEFINE_EXTENT 0x63
38
39 MODULE_LICENSE("GPL");
40
41 static struct dasd_discipline dasd_fba_discipline;
42
43 struct dasd_fba_private {
44         struct dasd_fba_characteristics rdc_data;
45 };
46
47 static struct ccw_device_id dasd_fba_ids[] = {
48         { CCW_DEVICE_DEVTYPE (0x6310, 0, 0x9336, 0), driver_info: 0x1},
49         { CCW_DEVICE_DEVTYPE (0x3880, 0, 0x3370, 0), driver_info: 0x2},
50         { /* end of list */ },
51 };
52
53 MODULE_DEVICE_TABLE(ccw, dasd_fba_ids);
54
55 static struct ccw_driver dasd_fba_driver; /* see below */
56 static int
57 dasd_fba_probe(struct ccw_device *cdev)
58 {
59         int ret;
60
61         ret = dasd_generic_probe (cdev, &dasd_fba_discipline);
62         if (ret)
63                 return ret;
64         ccw_device_set_options(cdev, CCWDEV_DO_PATHGROUP);
65         return 0;
66 }
67
68 static int
69 dasd_fba_set_online(struct ccw_device *cdev)
70 {
71         return dasd_generic_set_online (cdev, &dasd_fba_discipline);
72 }
73
74 static struct ccw_driver dasd_fba_driver = {
75         .name        = "dasd-fba",
76         .owner       = THIS_MODULE,
77         .ids         = dasd_fba_ids,
78         .probe       = dasd_fba_probe,
79         .remove      = dasd_generic_remove,
80         .set_offline = dasd_generic_set_offline,
81         .set_online  = dasd_fba_set_online,
82         .notify      = dasd_generic_notify,
83 };
84
85 static inline void
86 define_extent(struct ccw1 * ccw, struct DE_fba_data *data, int rw,
87               int blksize, int beg, int nr)
88 {
89         ccw->cmd_code = DASD_FBA_CCW_DEFINE_EXTENT;
90         ccw->flags = 0;
91         ccw->count = 16;
92         ccw->cda = (__u32) __pa(data);
93         memset(data, 0, sizeof (struct DE_fba_data));
94         if (rw == WRITE)
95                 (data->mask).perm = 0x0;
96         else if (rw == READ)
97                 (data->mask).perm = 0x1;
98         else
99                 data->mask.perm = 0x2;
100         data->blk_size = blksize;
101         data->ext_loc = beg;
102         data->ext_end = nr - 1;
103 }
104
105 static inline void
106 locate_record(struct ccw1 * ccw, struct LO_fba_data *data, int rw,
107               int block_nr, int block_ct)
108 {
109         ccw->cmd_code = DASD_FBA_CCW_LOCATE;
110         ccw->flags = 0;
111         ccw->count = 8;
112         ccw->cda = (__u32) __pa(data);
113         memset(data, 0, sizeof (struct LO_fba_data));
114         if (rw == WRITE)
115                 data->operation.cmd = 0x5;
116         else if (rw == READ)
117                 data->operation.cmd = 0x6;
118         else
119                 data->operation.cmd = 0x8;
120         data->blk_nr = block_nr;
121         data->blk_ct = block_ct;
122 }
123
124 static int
125 dasd_fba_check_characteristics(struct dasd_device *device)
126 {
127         struct dasd_fba_private *private;
128         struct ccw_device *cdev = device->cdev; 
129         void *rdc_data;
130         int rc;
131
132         private = (struct dasd_fba_private *) device->private;
133         if (private == NULL) {
134                 private = kmalloc(sizeof(struct dasd_fba_private), GFP_KERNEL);
135                 if (private == NULL) {
136                         DEV_MESSAGE(KERN_WARNING, device, "%s",
137                                     "memory allocation failed for private "
138                                     "data");
139                         return -ENOMEM;
140                 }
141                 device->private = (void *) private;
142         }
143         /* Read Device Characteristics */
144         rdc_data = (void *) &(private->rdc_data);
145         rc = read_dev_chars(device->cdev, &rdc_data, 32);
146         if (rc) {
147                 DEV_MESSAGE(KERN_WARNING, device,
148                             "Read device characteristics returned error %d",
149                             rc);
150                 return rc;
151         }
152
153         DEV_MESSAGE(KERN_INFO, device,
154                     "%04X/%02X(CU:%04X/%02X) %dMB at(%d B/blk)",
155                     cdev->id.dev_type,
156                     cdev->id.dev_model,
157                     cdev->id.cu_type,
158                     cdev->id.cu_model,
159                     ((private->rdc_data.blk_bdsa *
160                       (private->rdc_data.blk_size >> 9)) >> 11),
161                     private->rdc_data.blk_size);
162         return 0;
163 }
164
165 static int
166 dasd_fba_do_analysis(struct dasd_device *device)
167 {
168         struct dasd_fba_private *private;
169         int sb, rc;
170
171         private = (struct dasd_fba_private *) device->private;
172         rc = dasd_check_blocksize(private->rdc_data.blk_size);
173         if (rc) {
174                 DEV_MESSAGE(KERN_INFO, device, "unknown blocksize %d",
175                             private->rdc_data.blk_size);
176                 return rc;
177         }
178         device->blocks = private->rdc_data.blk_bdsa;
179         device->bp_block = private->rdc_data.blk_size;
180         device->s2b_shift = 0;  /* bits to shift 512 to get a block */
181         for (sb = 512; sb < private->rdc_data.blk_size; sb = sb << 1)
182                 device->s2b_shift++;
183         return 0;
184 }
185
186 static int
187 dasd_fba_fill_geometry(struct dasd_device *device, struct hd_geometry *geo)
188 {
189         if (dasd_check_blocksize(device->bp_block) != 0)
190                 return -EINVAL;
191         geo->cylinders = (device->blocks << device->s2b_shift) >> 10;
192         geo->heads = 16;
193         geo->sectors = 128 >> device->s2b_shift;
194         return 0;
195 }
196
197 static dasd_era_t
198 dasd_fba_examine_error(struct dasd_ccw_req * cqr, struct irb * irb)
199 {
200         struct dasd_device *device;
201         struct ccw_device *cdev;
202
203         device = (struct dasd_device *) cqr->device;
204         if (irb->scsw.cstat == 0x00 &&
205             irb->scsw.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END))
206                 return dasd_era_none;
207         
208         cdev = device->cdev;
209         switch (cdev->id.dev_type) {
210         case 0x3370:
211                 return dasd_3370_erp_examine(cqr, irb);
212         case 0x9336:
213                 return dasd_9336_erp_examine(cqr, irb);
214         default:
215                 return dasd_era_recover;
216         }
217 }
218
219 static dasd_erp_fn_t
220 dasd_fba_erp_action(struct dasd_ccw_req * cqr)
221 {
222         return dasd_default_erp_action;
223 }
224
225 static dasd_erp_fn_t
226 dasd_fba_erp_postaction(struct dasd_ccw_req * cqr)
227 {
228         if (cqr->function == dasd_default_erp_action)
229                 return dasd_default_erp_postaction;
230
231         DEV_MESSAGE(KERN_WARNING, cqr->device, "unknown ERP action %p",
232                     cqr->function);
233         return NULL;
234 }
235
236 static struct dasd_ccw_req *
237 dasd_fba_build_cp(struct dasd_device * device, struct request *req)
238 {
239         struct dasd_fba_private *private;
240         unsigned long *idaws;
241         struct LO_fba_data *LO_data;
242         struct dasd_ccw_req *cqr;
243         struct ccw1 *ccw;
244         struct bio *bio;
245         struct bio_vec *bv;
246         char *dst;
247         int count, cidaw, cplength, datasize;
248         sector_t recid, first_rec, last_rec;
249         unsigned int blksize, off;
250         unsigned char cmd;
251         int i;
252
253         private = (struct dasd_fba_private *) device->private;
254         if (rq_data_dir(req) == READ) {
255                 cmd = DASD_FBA_CCW_READ;
256         } else if (rq_data_dir(req) == WRITE) {
257                 cmd = DASD_FBA_CCW_WRITE;
258         } else
259                 return ERR_PTR(-EINVAL);
260         blksize = device->bp_block;
261         /* Calculate record id of first and last block. */
262         first_rec = req->sector >> device->s2b_shift;
263         last_rec = (req->sector + req->nr_sectors - 1) >> device->s2b_shift;
264         /* Check struct bio and count the number of blocks for the request. */
265         count = 0;
266         cidaw = 0;
267         rq_for_each_bio(bio, req) {
268                 bio_for_each_segment(bv, bio, i) {
269                         if (bv->bv_len & (blksize - 1))
270                                 /* Fba can only do full blocks. */
271                                 return ERR_PTR(-EINVAL);
272                         count += bv->bv_len >> (device->s2b_shift + 9);
273 #if defined(CONFIG_64BIT)
274                         if (idal_is_needed (page_address(bv->bv_page),
275                                             bv->bv_len))
276                                 cidaw += bv->bv_len / blksize;
277 #endif
278                 }
279         }
280         /* Paranoia. */
281         if (count != last_rec - first_rec + 1)
282                 return ERR_PTR(-EINVAL);
283         /* 1x define extent + 1x locate record + number of blocks */
284         cplength = 2 + count;
285         /* 1x define extent + 1x locate record */
286         datasize = sizeof(struct DE_fba_data) + sizeof(struct LO_fba_data) +
287                 cidaw * sizeof(unsigned long);
288         /*
289          * Find out number of additional locate record ccws if the device
290          * can't do data chaining.
291          */
292         if (private->rdc_data.mode.bits.data_chain == 0) {
293                 cplength += count - 1;
294                 datasize += (count - 1)*sizeof(struct LO_fba_data);
295         }
296         /* Allocate the ccw request. */
297         cqr = dasd_smalloc_request(dasd_fba_discipline.name,
298                                    cplength, datasize, device);
299         if (IS_ERR(cqr))
300                 return cqr;
301         ccw = cqr->cpaddr;
302         /* First ccw is define extent. */
303         define_extent(ccw++, cqr->data, rq_data_dir(req),
304                       device->bp_block, req->sector, req->nr_sectors);
305         /* Build locate_record + read/write ccws. */
306         idaws = (unsigned long *) (cqr->data + sizeof(struct DE_fba_data));
307         LO_data = (struct LO_fba_data *) (idaws + cidaw);
308         /* Locate record for all blocks for smart devices. */
309         if (private->rdc_data.mode.bits.data_chain != 0) {
310                 ccw[-1].flags |= CCW_FLAG_CC;
311                 locate_record(ccw++, LO_data++, rq_data_dir(req), 0, count);
312         }
313         recid = first_rec;
314         rq_for_each_bio(bio, req) bio_for_each_segment(bv, bio, i) {
315                 dst = page_address(bv->bv_page) + bv->bv_offset;
316                 if (dasd_page_cache) {
317                         char *copy = kmem_cache_alloc(dasd_page_cache,
318                                                       SLAB_DMA | __GFP_NOWARN);
319                         if (copy && rq_data_dir(req) == WRITE)
320                                 memcpy(copy + bv->bv_offset, dst, bv->bv_len);
321                         if (copy)
322                                 dst = copy + bv->bv_offset;
323                 }
324                 for (off = 0; off < bv->bv_len; off += blksize) {
325                         /* Locate record for stupid devices. */
326                         if (private->rdc_data.mode.bits.data_chain == 0) {
327                                 ccw[-1].flags |= CCW_FLAG_CC;
328                                 locate_record(ccw, LO_data++,
329                                               rq_data_dir(req),
330                                               recid - first_rec, 1);
331                                 ccw->flags = CCW_FLAG_CC;
332                                 ccw++;
333                         } else {
334                                 if (recid > first_rec)
335                                         ccw[-1].flags |= CCW_FLAG_DC;
336                                 else
337                                         ccw[-1].flags |= CCW_FLAG_CC;
338                         }
339                         ccw->cmd_code = cmd;
340                         ccw->count = device->bp_block;
341                         if (idal_is_needed(dst, blksize)) {
342                                 ccw->cda = (__u32)(addr_t) idaws;
343                                 ccw->flags = CCW_FLAG_IDA;
344                                 idaws = idal_create_words(idaws, dst, blksize);
345                         } else {
346                                 ccw->cda = (__u32)(addr_t) dst;
347                                 ccw->flags = 0;
348                         }
349                         ccw++;
350                         dst += blksize;
351                         recid++;
352                 }
353         }
354         if (req->flags & REQ_FAILFAST)
355                 set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
356         cqr->device = device;
357         cqr->expires = 5 * 60 * HZ;     /* 5 minutes */
358         cqr->retries = 32;
359         cqr->buildclk = get_clock();
360         cqr->status = DASD_CQR_FILLED;
361         return cqr;
362 }
363
364 static int
365 dasd_fba_free_cp(struct dasd_ccw_req *cqr, struct request *req)
366 {
367         struct dasd_fba_private *private;
368         struct ccw1 *ccw;
369         struct bio *bio;
370         struct bio_vec *bv;
371         char *dst, *cda;
372         unsigned int blksize, off;
373         int i, status;
374
375         if (!dasd_page_cache)
376                 goto out;
377         private = (struct dasd_fba_private *) cqr->device->private;
378         blksize = cqr->device->bp_block;
379         ccw = cqr->cpaddr;
380         /* Skip over define extent & locate record. */
381         ccw++;
382         if (private->rdc_data.mode.bits.data_chain != 0)
383                 ccw++;
384         rq_for_each_bio(bio, req) bio_for_each_segment(bv, bio, i) {
385                 dst = page_address(bv->bv_page) + bv->bv_offset;
386                 for (off = 0; off < bv->bv_len; off += blksize) {
387                         /* Skip locate record. */
388                         if (private->rdc_data.mode.bits.data_chain == 0)
389                                 ccw++;
390                         if (dst) {
391                                 if (ccw->flags & CCW_FLAG_IDA)
392                                         cda = *((char **)((addr_t) ccw->cda));
393                                 else
394                                         cda = (char *)((addr_t) ccw->cda);
395                                 if (dst != cda) {
396                                         if (rq_data_dir(req) == READ)
397                                                 memcpy(dst, cda, bv->bv_len);
398                                         kmem_cache_free(dasd_page_cache,
399                                             (void *)((addr_t)cda & PAGE_MASK));
400                                 }
401                                 dst = NULL;
402                         }
403                         ccw++;
404                 }
405         }
406 out:
407         status = cqr->status == DASD_CQR_DONE;
408         dasd_sfree_request(cqr, cqr->device);
409         return status;
410 }
411
412 static int
413 dasd_fba_fill_info(struct dasd_device * device,
414                    struct dasd_information2_t * info)
415 {
416         info->label_block = 1;
417         info->FBA_layout = 1;
418         info->format = DASD_FORMAT_LDL;
419         info->characteristics_size = sizeof(struct dasd_fba_characteristics);
420         memcpy(info->characteristics,
421                &((struct dasd_fba_private *) device->private)->rdc_data,
422                sizeof (struct dasd_fba_characteristics));
423         info->confdata_size = 0;
424         return 0;
425 }
426
427 static void
428 dasd_fba_dump_sense(struct dasd_device *device, struct dasd_ccw_req * req,
429                     struct irb *irb)
430 {
431         char *page;
432         struct ccw1 *act, *end, *last;
433         int len, sl, sct, count;
434
435         page = (char *) get_zeroed_page(GFP_ATOMIC);
436         if (page == NULL) {
437                 DEV_MESSAGE(KERN_ERR, device, " %s",
438                             "No memory to dump sense data");
439                 return;
440         }
441         len = sprintf(page, KERN_ERR PRINTK_HEADER
442                       " I/O status report for device %s:\n",
443                       device->cdev->dev.bus_id);
444         len += sprintf(page + len, KERN_ERR PRINTK_HEADER
445                        " in req: %p CS: 0x%02X DS: 0x%02X\n", req,
446                        irb->scsw.cstat, irb->scsw.dstat);
447         len += sprintf(page + len, KERN_ERR PRINTK_HEADER
448                        " device %s: Failing CCW: %p\n",
449                        device->cdev->dev.bus_id,
450                        (void *) (addr_t) irb->scsw.cpa);
451         if (irb->esw.esw0.erw.cons) {
452                 for (sl = 0; sl < 4; sl++) {
453                         len += sprintf(page + len, KERN_ERR PRINTK_HEADER
454                                        " Sense(hex) %2d-%2d:",
455                                        (8 * sl), ((8 * sl) + 7));
456
457                         for (sct = 0; sct < 8; sct++) {
458                                 len += sprintf(page + len, " %02x",
459                                                irb->ecw[8 * sl + sct]);
460                         }
461                         len += sprintf(page + len, "\n");
462                 }
463         } else {
464                 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
465                                " SORRY - NO VALID SENSE AVAILABLE\n");
466         }
467         MESSAGE_LOG(KERN_ERR, "%s",
468                     page + sizeof(KERN_ERR PRINTK_HEADER));
469
470         /* dump the Channel Program */
471         /* print first CCWs (maximum 8) */
472         act = req->cpaddr;
473         for (last = act; last->flags & (CCW_FLAG_CC | CCW_FLAG_DC); last++);
474         end = min(act + 8, last);
475         len = sprintf(page, KERN_ERR PRINTK_HEADER
476                       " Related CP in req: %p\n", req);
477         while (act <= end) {
478                 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
479                                " CCW %p: %08X %08X DAT:",
480                                act, ((int *) act)[0], ((int *) act)[1]);
481                 for (count = 0; count < 32 && count < act->count;
482                      count += sizeof(int))
483                         len += sprintf(page + len, " %08X",
484                                        ((int *) (addr_t) act->cda)
485                                        [(count>>2)]);
486                 len += sprintf(page + len, "\n");
487                 act++;
488         }
489         MESSAGE_LOG(KERN_ERR, "%s",
490                     page + sizeof(KERN_ERR PRINTK_HEADER));
491
492
493         /* print failing CCW area */
494         len = 0;
495         if (act <  ((struct ccw1 *)(addr_t) irb->scsw.cpa) - 2) {
496                 act = ((struct ccw1 *)(addr_t) irb->scsw.cpa) - 2;
497                 len += sprintf(page + len, KERN_ERR PRINTK_HEADER "......\n");
498         }
499         end = min((struct ccw1 *)(addr_t) irb->scsw.cpa + 2, last);
500         while (act <= end) {
501                 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
502                                " CCW %p: %08X %08X DAT:",
503                                act, ((int *) act)[0], ((int *) act)[1]);
504                 for (count = 0; count < 32 && count < act->count;
505                      count += sizeof(int))
506                         len += sprintf(page + len, " %08X",
507                                        ((int *) (addr_t) act->cda)
508                                        [(count>>2)]);
509                 len += sprintf(page + len, "\n");
510                 act++;
511         }
512
513         /* print last CCWs */
514         if (act <  last - 2) {
515                 act = last - 2;
516                 len += sprintf(page + len, KERN_ERR PRINTK_HEADER "......\n");
517         }
518         while (act <= last) {
519                 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
520                                " CCW %p: %08X %08X DAT:",
521                                act, ((int *) act)[0], ((int *) act)[1]);
522                 for (count = 0; count < 32 && count < act->count;
523                      count += sizeof(int))
524                         len += sprintf(page + len, " %08X",
525                                        ((int *) (addr_t) act->cda)
526                                        [(count>>2)]);
527                 len += sprintf(page + len, "\n");
528                 act++;
529         }
530         if (len > 0)
531                 MESSAGE_LOG(KERN_ERR, "%s",
532                             page + sizeof(KERN_ERR PRINTK_HEADER));
533         free_page((unsigned long) page);
534 }
535
536 /*
537  * max_blocks is dependent on the amount of storage that is available
538  * in the static io buffer for each device. Currently each device has
539  * 8192 bytes (=2 pages). For 64 bit one dasd_mchunkt_t structure has
540  * 24 bytes, the struct dasd_ccw_req has 136 bytes and each block can use
541  * up to 16 bytes (8 for the ccw and 8 for the idal pointer). In
542  * addition we have one define extent ccw + 16 bytes of data and a 
543  * locate record ccw for each block (stupid devices!) + 16 bytes of data.
544  * That makes:
545  * (8192 - 24 - 136 - 8 - 16) / 40 = 200.2 blocks at maximum.
546  * We want to fit two into the available memory so that we can immediately
547  * start the next request if one finishes off. That makes 100.1 blocks
548  * for one request. Give a little safety and the result is 96.
549  */
550 static struct dasd_discipline dasd_fba_discipline = {
551         .owner = THIS_MODULE,
552         .name = "FBA ",
553         .ebcname = "FBA ",
554         .max_blocks = 96,
555         .check_device = dasd_fba_check_characteristics,
556         .do_analysis = dasd_fba_do_analysis,
557         .fill_geometry = dasd_fba_fill_geometry,
558         .start_IO = dasd_start_IO,
559         .term_IO = dasd_term_IO,
560         .examine_error = dasd_fba_examine_error,
561         .erp_action = dasd_fba_erp_action,
562         .erp_postaction = dasd_fba_erp_postaction,
563         .build_cp = dasd_fba_build_cp,
564         .free_cp = dasd_fba_free_cp,
565         .dump_sense = dasd_fba_dump_sense,
566         .fill_info = dasd_fba_fill_info,
567 };
568
569 static int __init
570 dasd_fba_init(void)
571 {
572         int ret;
573
574         ASCEBC(dasd_fba_discipline.ebcname, 4);
575
576         ret = ccw_driver_register(&dasd_fba_driver);
577         if (ret)
578                 return ret;
579
580         dasd_generic_auto_online(&dasd_fba_driver);
581         return 0;
582 }
583
584 static void __exit
585 dasd_fba_cleanup(void)
586 {
587         ccw_driver_unregister(&dasd_fba_driver);
588 }
589
590 module_init(dasd_fba_init);
591 module_exit(dasd_fba_cleanup);
592
593 /*
594  * Overrides for Emacs so that we follow Linus's tabbing style.
595  * Emacs will notice this stuff at the end of the file and automatically
596  * adjust the settings for this buffer only.  This must remain at the end
597  * of the file.
598  * ---------------------------------------------------------------------------
599  * Local variables:
600  * c-indent-level: 4 
601  * c-brace-imaginary-offset: 0
602  * c-brace-offset: -4
603  * c-argdecl-indent: 4
604  * c-label-offset: -4
605  * c-continued-statement-offset: 4
606  * c-continued-brace-offset: 0
607  * indent-tabs-mode: 1
608  * tab-width: 8
609  * End:
610  */