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