Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/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                 device->private = NULL;
143                 kfree(private);
144                 return PTR_ERR(block);
145         }
146         device->block = block;
147         block->base = device;
148
149         /* Read Device Characteristics */
150         rdc_data = (void *) &(private->rdc_data);
151         rc = dasd_generic_read_dev_chars(device, "FBA ", &rdc_data, 32);
152         if (rc) {
153                 DEV_MESSAGE(KERN_WARNING, device,
154                             "Read device characteristics returned error %d",
155                             rc);
156                 device->block = NULL;
157                 dasd_free_block(block);
158                 device->private = NULL;
159                 kfree(private);
160                 return rc;
161         }
162
163         DEV_MESSAGE(KERN_INFO, device,
164                     "%04X/%02X(CU:%04X/%02X) %dMB at(%d B/blk)",
165                     cdev->id.dev_type,
166                     cdev->id.dev_model,
167                     cdev->id.cu_type,
168                     cdev->id.cu_model,
169                     ((private->rdc_data.blk_bdsa *
170                       (private->rdc_data.blk_size >> 9)) >> 11),
171                     private->rdc_data.blk_size);
172         return 0;
173 }
174
175 static int dasd_fba_do_analysis(struct dasd_block *block)
176 {
177         struct dasd_fba_private *private;
178         int sb, rc;
179
180         private = (struct dasd_fba_private *) block->base->private;
181         rc = dasd_check_blocksize(private->rdc_data.blk_size);
182         if (rc) {
183                 DEV_MESSAGE(KERN_INFO, block->base, "unknown blocksize %d",
184                             private->rdc_data.blk_size);
185                 return rc;
186         }
187         block->blocks = private->rdc_data.blk_bdsa;
188         block->bp_block = private->rdc_data.blk_size;
189         block->s2b_shift = 0;   /* bits to shift 512 to get a block */
190         for (sb = 512; sb < private->rdc_data.blk_size; sb = sb << 1)
191                 block->s2b_shift++;
192         return 0;
193 }
194
195 static int dasd_fba_fill_geometry(struct dasd_block *block,
196                                   struct hd_geometry *geo)
197 {
198         if (dasd_check_blocksize(block->bp_block) != 0)
199                 return -EINVAL;
200         geo->cylinders = (block->blocks << block->s2b_shift) >> 10;
201         geo->heads = 16;
202         geo->sectors = 128 >> block->s2b_shift;
203         return 0;
204 }
205
206 static dasd_erp_fn_t
207 dasd_fba_erp_action(struct dasd_ccw_req * cqr)
208 {
209         return dasd_default_erp_action;
210 }
211
212 static dasd_erp_fn_t
213 dasd_fba_erp_postaction(struct dasd_ccw_req * cqr)
214 {
215         if (cqr->function == dasd_default_erp_action)
216                 return dasd_default_erp_postaction;
217
218         DEV_MESSAGE(KERN_WARNING, cqr->startdev, "unknown ERP action %p",
219                     cqr->function);
220         return NULL;
221 }
222
223 static void dasd_fba_handle_unsolicited_interrupt(struct dasd_device *device,
224                                                    struct irb *irb)
225 {
226         char mask;
227
228         /* first of all check for state change pending interrupt */
229         mask = DEV_STAT_ATTENTION | DEV_STAT_DEV_END | DEV_STAT_UNIT_EXCEP;
230         if ((irb->scsw.cmd.dstat & mask) == mask) {
231                 dasd_generic_handle_state_change(device);
232                 return;
233         }
234
235         /* check for unsolicited interrupts */
236         DEV_MESSAGE(KERN_DEBUG, device, "%s",
237                     "unsolicited interrupt received");
238         device->discipline->dump_sense(device, NULL, irb);
239         dasd_schedule_device_bh(device);
240         return;
241 };
242
243 static struct dasd_ccw_req *dasd_fba_build_cp(struct dasd_device * memdev,
244                                               struct dasd_block *block,
245                                               struct request *req)
246 {
247         struct dasd_fba_private *private;
248         unsigned long *idaws;
249         struct LO_fba_data *LO_data;
250         struct dasd_ccw_req *cqr;
251         struct ccw1 *ccw;
252         struct req_iterator iter;
253         struct bio_vec *bv;
254         char *dst;
255         int count, cidaw, cplength, datasize;
256         sector_t recid, first_rec, last_rec;
257         unsigned int blksize, off;
258         unsigned char cmd;
259
260         private = (struct dasd_fba_private *) block->base->private;
261         if (rq_data_dir(req) == READ) {
262                 cmd = DASD_FBA_CCW_READ;
263         } else if (rq_data_dir(req) == WRITE) {
264                 cmd = DASD_FBA_CCW_WRITE;
265         } else
266                 return ERR_PTR(-EINVAL);
267         blksize = block->bp_block;
268         /* Calculate record id of first and last block. */
269         first_rec = req->sector >> block->s2b_shift;
270         last_rec = (req->sector + req->nr_sectors - 1) >> block->s2b_shift;
271         /* Check struct bio and count the number of blocks for the request. */
272         count = 0;
273         cidaw = 0;
274         rq_for_each_segment(bv, req, iter) {
275                 if (bv->bv_len & (blksize - 1))
276                         /* Fba can only do full blocks. */
277                         return ERR_PTR(-EINVAL);
278                 count += bv->bv_len >> (block->s2b_shift + 9);
279 #if defined(CONFIG_64BIT)
280                 if (idal_is_needed (page_address(bv->bv_page), bv->bv_len))
281                         cidaw += bv->bv_len / blksize;
282 #endif
283         }
284         /* Paranoia. */
285         if (count != last_rec - first_rec + 1)
286                 return ERR_PTR(-EINVAL);
287         /* 1x define extent + 1x locate record + number of blocks */
288         cplength = 2 + count;
289         /* 1x define extent + 1x locate record */
290         datasize = sizeof(struct DE_fba_data) + sizeof(struct LO_fba_data) +
291                 cidaw * sizeof(unsigned long);
292         /*
293          * Find out number of additional locate record ccws if the device
294          * can't do data chaining.
295          */
296         if (private->rdc_data.mode.bits.data_chain == 0) {
297                 cplength += count - 1;
298                 datasize += (count - 1)*sizeof(struct LO_fba_data);
299         }
300         /* Allocate the ccw request. */
301         cqr = dasd_smalloc_request(dasd_fba_discipline.name,
302                                    cplength, datasize, memdev);
303         if (IS_ERR(cqr))
304                 return cqr;
305         ccw = cqr->cpaddr;
306         /* First ccw is define extent. */
307         define_extent(ccw++, cqr->data, rq_data_dir(req),
308                       block->bp_block, req->sector, req->nr_sectors);
309         /* Build locate_record + read/write ccws. */
310         idaws = (unsigned long *) (cqr->data + sizeof(struct DE_fba_data));
311         LO_data = (struct LO_fba_data *) (idaws + cidaw);
312         /* Locate record for all blocks for smart devices. */
313         if (private->rdc_data.mode.bits.data_chain != 0) {
314                 ccw[-1].flags |= CCW_FLAG_CC;
315                 locate_record(ccw++, LO_data++, rq_data_dir(req), 0, count);
316         }
317         recid = first_rec;
318         rq_for_each_segment(bv, req, iter) {
319                 dst = page_address(bv->bv_page) + bv->bv_offset;
320                 if (dasd_page_cache) {
321                         char *copy = kmem_cache_alloc(dasd_page_cache,
322                                                       GFP_DMA | __GFP_NOWARN);
323                         if (copy && rq_data_dir(req) == WRITE)
324                                 memcpy(copy + bv->bv_offset, dst, bv->bv_len);
325                         if (copy)
326                                 dst = copy + bv->bv_offset;
327                 }
328                 for (off = 0; off < bv->bv_len; off += blksize) {
329                         /* Locate record for stupid devices. */
330                         if (private->rdc_data.mode.bits.data_chain == 0) {
331                                 ccw[-1].flags |= CCW_FLAG_CC;
332                                 locate_record(ccw, LO_data++,
333                                               rq_data_dir(req),
334                                               recid - first_rec, 1);
335                                 ccw->flags = CCW_FLAG_CC;
336                                 ccw++;
337                         } else {
338                                 if (recid > first_rec)
339                                         ccw[-1].flags |= CCW_FLAG_DC;
340                                 else
341                                         ccw[-1].flags |= CCW_FLAG_CC;
342                         }
343                         ccw->cmd_code = cmd;
344                         ccw->count = block->bp_block;
345                         if (idal_is_needed(dst, blksize)) {
346                                 ccw->cda = (__u32)(addr_t) idaws;
347                                 ccw->flags = CCW_FLAG_IDA;
348                                 idaws = idal_create_words(idaws, dst, blksize);
349                         } else {
350                                 ccw->cda = (__u32)(addr_t) dst;
351                                 ccw->flags = 0;
352                         }
353                         ccw++;
354                         dst += blksize;
355                         recid++;
356                 }
357         }
358         if (blk_noretry_request(req))
359                 set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
360         cqr->startdev = memdev;
361         cqr->memdev = memdev;
362         cqr->block = block;
363         cqr->expires = 5 * 60 * HZ;     /* 5 minutes */
364         cqr->retries = 32;
365         cqr->buildclk = get_clock();
366         cqr->status = DASD_CQR_FILLED;
367         return cqr;
368 }
369
370 static int
371 dasd_fba_free_cp(struct dasd_ccw_req *cqr, struct request *req)
372 {
373         struct dasd_fba_private *private;
374         struct ccw1 *ccw;
375         struct req_iterator iter;
376         struct bio_vec *bv;
377         char *dst, *cda;
378         unsigned int blksize, off;
379         int status;
380
381         if (!dasd_page_cache)
382                 goto out;
383         private = (struct dasd_fba_private *) cqr->block->base->private;
384         blksize = cqr->block->bp_block;
385         ccw = cqr->cpaddr;
386         /* Skip over define extent & locate record. */
387         ccw++;
388         if (private->rdc_data.mode.bits.data_chain != 0)
389                 ccw++;
390         rq_for_each_segment(bv, req, iter) {
391                 dst = page_address(bv->bv_page) + bv->bv_offset;
392                 for (off = 0; off < bv->bv_len; off += blksize) {
393                         /* Skip locate record. */
394                         if (private->rdc_data.mode.bits.data_chain == 0)
395                                 ccw++;
396                         if (dst) {
397                                 if (ccw->flags & CCW_FLAG_IDA)
398                                         cda = *((char **)((addr_t) ccw->cda));
399                                 else
400                                         cda = (char *)((addr_t) ccw->cda);
401                                 if (dst != cda) {
402                                         if (rq_data_dir(req) == READ)
403                                                 memcpy(dst, cda, bv->bv_len);
404                                         kmem_cache_free(dasd_page_cache,
405                                             (void *)((addr_t)cda & PAGE_MASK));
406                                 }
407                                 dst = NULL;
408                         }
409                         ccw++;
410                 }
411         }
412 out:
413         status = cqr->status == DASD_CQR_DONE;
414         dasd_sfree_request(cqr, cqr->memdev);
415         return status;
416 }
417
418 static void dasd_fba_handle_terminated_request(struct dasd_ccw_req *cqr)
419 {
420         cqr->status = DASD_CQR_FILLED;
421 };
422
423 static int
424 dasd_fba_fill_info(struct dasd_device * device,
425                    struct dasd_information2_t * info)
426 {
427         info->label_block = 1;
428         info->FBA_layout = 1;
429         info->format = DASD_FORMAT_LDL;
430         info->characteristics_size = sizeof(struct dasd_fba_characteristics);
431         memcpy(info->characteristics,
432                &((struct dasd_fba_private *) device->private)->rdc_data,
433                sizeof (struct dasd_fba_characteristics));
434         info->confdata_size = 0;
435         return 0;
436 }
437
438 static void
439 dasd_fba_dump_sense(struct dasd_device *device, struct dasd_ccw_req * req,
440                     struct irb *irb)
441 {
442         char *page;
443         struct ccw1 *act, *end, *last;
444         int len, sl, sct, count;
445
446         page = (char *) get_zeroed_page(GFP_ATOMIC);
447         if (page == NULL) {
448                 DEV_MESSAGE(KERN_ERR, device, " %s",
449                             "No memory to dump sense data");
450                 return;
451         }
452         len = sprintf(page, KERN_ERR PRINTK_HEADER
453                       " I/O status report for device %s:\n",
454                       dev_name(&device->cdev->dev));
455         len += sprintf(page + len, KERN_ERR PRINTK_HEADER
456                        " in req: %p CS: 0x%02X DS: 0x%02X\n", req,
457                        irb->scsw.cmd.cstat, irb->scsw.cmd.dstat);
458         len += sprintf(page + len, KERN_ERR PRINTK_HEADER
459                        " device %s: Failing CCW: %p\n",
460                        dev_name(&device->cdev->dev),
461                        (void *) (addr_t) irb->scsw.cmd.cpa);
462         if (irb->esw.esw0.erw.cons) {
463                 for (sl = 0; sl < 4; sl++) {
464                         len += sprintf(page + len, KERN_ERR PRINTK_HEADER
465                                        " Sense(hex) %2d-%2d:",
466                                        (8 * sl), ((8 * sl) + 7));
467
468                         for (sct = 0; sct < 8; sct++) {
469                                 len += sprintf(page + len, " %02x",
470                                                irb->ecw[8 * sl + sct]);
471                         }
472                         len += sprintf(page + len, "\n");
473                 }
474         } else {
475                 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
476                                " SORRY - NO VALID SENSE AVAILABLE\n");
477         }
478         MESSAGE_LOG(KERN_ERR, "%s",
479                     page + sizeof(KERN_ERR PRINTK_HEADER));
480
481         /* dump the Channel Program */
482         /* print first CCWs (maximum 8) */
483         act = req->cpaddr;
484         for (last = act; last->flags & (CCW_FLAG_CC | CCW_FLAG_DC); last++);
485         end = min(act + 8, last);
486         len = sprintf(page, KERN_ERR PRINTK_HEADER
487                       " Related CP in req: %p\n", req);
488         while (act <= end) {
489                 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
490                                " CCW %p: %08X %08X DAT:",
491                                act, ((int *) act)[0], ((int *) act)[1]);
492                 for (count = 0; count < 32 && count < act->count;
493                      count += sizeof(int))
494                         len += sprintf(page + len, " %08X",
495                                        ((int *) (addr_t) act->cda)
496                                        [(count>>2)]);
497                 len += sprintf(page + len, "\n");
498                 act++;
499         }
500         MESSAGE_LOG(KERN_ERR, "%s",
501                     page + sizeof(KERN_ERR PRINTK_HEADER));
502
503
504         /* print failing CCW area */
505         len = 0;
506         if (act <  ((struct ccw1 *)(addr_t) irb->scsw.cmd.cpa) - 2) {
507                 act = ((struct ccw1 *)(addr_t) irb->scsw.cmd.cpa) - 2;
508                 len += sprintf(page + len, KERN_ERR PRINTK_HEADER "......\n");
509         }
510         end = min((struct ccw1 *)(addr_t) irb->scsw.cmd.cpa + 2, last);
511         while (act <= end) {
512                 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
513                                " CCW %p: %08X %08X DAT:",
514                                act, ((int *) act)[0], ((int *) act)[1]);
515                 for (count = 0; count < 32 && count < act->count;
516                      count += sizeof(int))
517                         len += sprintf(page + len, " %08X",
518                                        ((int *) (addr_t) act->cda)
519                                        [(count>>2)]);
520                 len += sprintf(page + len, "\n");
521                 act++;
522         }
523
524         /* print last CCWs */
525         if (act <  last - 2) {
526                 act = last - 2;
527                 len += sprintf(page + len, KERN_ERR PRINTK_HEADER "......\n");
528         }
529         while (act <= last) {
530                 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
531                                " CCW %p: %08X %08X DAT:",
532                                act, ((int *) act)[0], ((int *) act)[1]);
533                 for (count = 0; count < 32 && count < act->count;
534                      count += sizeof(int))
535                         len += sprintf(page + len, " %08X",
536                                        ((int *) (addr_t) act->cda)
537                                        [(count>>2)]);
538                 len += sprintf(page + len, "\n");
539                 act++;
540         }
541         if (len > 0)
542                 MESSAGE_LOG(KERN_ERR, "%s",
543                             page + sizeof(KERN_ERR PRINTK_HEADER));
544         free_page((unsigned long) page);
545 }
546
547 /*
548  * max_blocks is dependent on the amount of storage that is available
549  * in the static io buffer for each device. Currently each device has
550  * 8192 bytes (=2 pages). For 64 bit one dasd_mchunkt_t structure has
551  * 24 bytes, the struct dasd_ccw_req has 136 bytes and each block can use
552  * up to 16 bytes (8 for the ccw and 8 for the idal pointer). In
553  * addition we have one define extent ccw + 16 bytes of data and a
554  * locate record ccw for each block (stupid devices!) + 16 bytes of data.
555  * That makes:
556  * (8192 - 24 - 136 - 8 - 16) / 40 = 200.2 blocks at maximum.
557  * We want to fit two into the available memory so that we can immediately
558  * start the next request if one finishes off. That makes 100.1 blocks
559  * for one request. Give a little safety and the result is 96.
560  */
561 static struct dasd_discipline dasd_fba_discipline = {
562         .owner = THIS_MODULE,
563         .name = "FBA ",
564         .ebcname = "FBA ",
565         .max_blocks = 96,
566         .check_device = dasd_fba_check_characteristics,
567         .do_analysis = dasd_fba_do_analysis,
568         .fill_geometry = dasd_fba_fill_geometry,
569         .start_IO = dasd_start_IO,
570         .term_IO = dasd_term_IO,
571         .handle_terminated_request = dasd_fba_handle_terminated_request,
572         .erp_action = dasd_fba_erp_action,
573         .erp_postaction = dasd_fba_erp_postaction,
574         .handle_unsolicited_interrupt = dasd_fba_handle_unsolicited_interrupt,
575         .build_cp = dasd_fba_build_cp,
576         .free_cp = dasd_fba_free_cp,
577         .dump_sense = dasd_fba_dump_sense,
578         .fill_info = dasd_fba_fill_info,
579 };
580
581 static int __init
582 dasd_fba_init(void)
583 {
584         ASCEBC(dasd_fba_discipline.ebcname, 4);
585         return ccw_driver_register(&dasd_fba_driver);
586 }
587
588 static void __exit
589 dasd_fba_cleanup(void)
590 {
591         ccw_driver_unregister(&dasd_fba_driver);
592 }
593
594 module_init(dasd_fba_init);
595 module_exit(dasd_fba_cleanup);