Linux 2.6.34-rc3
[pohmelfs.git] / drivers / s390 / block / dasd_fba.c
blob37282b90eeccea91d4fa476d40c96995d24bdadc
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 * Copyright IBM Corp. 1999, 2009
6 */
8 #define KMSG_COMPONENT "dasd-fba"
10 #include <linux/stddef.h>
11 #include <linux/kernel.h>
12 #include <asm/debug.h>
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>
20 #include <asm/idals.h>
21 #include <asm/ebcdic.h>
22 #include <asm/io.h>
23 #include <asm/ccwdev.h>
25 #include "dasd_int.h"
26 #include "dasd_fba.h"
28 #ifdef PRINTK_HEADER
29 #undef PRINTK_HEADER
30 #endif /* PRINTK_HEADER */
31 #define PRINTK_HEADER "dasd(fba):"
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
38 MODULE_LICENSE("GPL");
40 static struct dasd_discipline dasd_fba_discipline;
42 struct dasd_fba_private {
43 struct dasd_fba_characteristics rdc_data;
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 */ },
52 MODULE_DEVICE_TABLE(ccw, dasd_fba_ids);
54 static struct ccw_driver dasd_fba_driver; /* see below */
55 static int
56 dasd_fba_probe(struct ccw_device *cdev)
58 return dasd_generic_probe(cdev, &dasd_fba_discipline);
61 static int
62 dasd_fba_set_online(struct ccw_device *cdev)
64 return dasd_generic_set_online(cdev, &dasd_fba_discipline);
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 .freeze = dasd_generic_pm_freeze,
77 .thaw = dasd_generic_restore_device,
78 .restore = dasd_generic_restore_device,
81 static void
82 define_extent(struct ccw1 * ccw, struct DE_fba_data *data, int rw,
83 int blksize, int beg, int nr)
85 ccw->cmd_code = DASD_FBA_CCW_DEFINE_EXTENT;
86 ccw->flags = 0;
87 ccw->count = 16;
88 ccw->cda = (__u32) __pa(data);
89 memset(data, 0, sizeof (struct DE_fba_data));
90 if (rw == WRITE)
91 (data->mask).perm = 0x0;
92 else if (rw == READ)
93 (data->mask).perm = 0x1;
94 else
95 data->mask.perm = 0x2;
96 data->blk_size = blksize;
97 data->ext_loc = beg;
98 data->ext_end = nr - 1;
101 static void
102 locate_record(struct ccw1 * ccw, struct LO_fba_data *data, int rw,
103 int block_nr, int block_ct)
105 ccw->cmd_code = DASD_FBA_CCW_LOCATE;
106 ccw->flags = 0;
107 ccw->count = 8;
108 ccw->cda = (__u32) __pa(data);
109 memset(data, 0, sizeof (struct LO_fba_data));
110 if (rw == WRITE)
111 data->operation.cmd = 0x5;
112 else if (rw == READ)
113 data->operation.cmd = 0x6;
114 else
115 data->operation.cmd = 0x8;
116 data->blk_nr = block_nr;
117 data->blk_ct = block_ct;
120 static int
121 dasd_fba_check_characteristics(struct dasd_device *device)
123 struct dasd_block *block;
124 struct dasd_fba_private *private;
125 struct ccw_device *cdev = device->cdev;
126 int rc;
127 int readonly;
129 private = (struct dasd_fba_private *) device->private;
130 if (!private) {
131 private = kzalloc(sizeof(*private), GFP_KERNEL | GFP_DMA);
132 if (!private) {
133 dev_warn(&device->cdev->dev,
134 "Allocating memory for private DASD "
135 "data failed\n");
136 return -ENOMEM;
138 device->private = (void *) private;
139 } else {
140 memset(private, 0, sizeof(*private));
142 block = dasd_alloc_block();
143 if (IS_ERR(block)) {
144 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s", "could not allocate "
145 "dasd block structure");
146 device->private = NULL;
147 kfree(private);
148 return PTR_ERR(block);
150 device->block = block;
151 block->base = device;
153 /* Read Device Characteristics */
154 rc = dasd_generic_read_dev_chars(device, DASD_FBA_MAGIC,
155 &private->rdc_data, 32);
156 if (rc) {
157 DBF_EVENT_DEVID(DBF_WARNING, cdev, "Read device "
158 "characteristics returned error %d", rc);
159 device->block = NULL;
160 dasd_free_block(block);
161 device->private = NULL;
162 kfree(private);
163 return rc;
166 readonly = dasd_device_is_ro(device);
167 if (readonly)
168 set_bit(DASD_FLAG_DEVICE_RO, &device->flags);
170 dev_info(&device->cdev->dev,
171 "New FBA DASD %04X/%02X (CU %04X/%02X) with %d MB "
172 "and %d B/blk%s\n",
173 cdev->id.dev_type,
174 cdev->id.dev_model,
175 cdev->id.cu_type,
176 cdev->id.cu_model,
177 ((private->rdc_data.blk_bdsa *
178 (private->rdc_data.blk_size >> 9)) >> 11),
179 private->rdc_data.blk_size,
180 readonly ? ", read-only device" : "");
181 return 0;
184 static int dasd_fba_do_analysis(struct dasd_block *block)
186 struct dasd_fba_private *private;
187 int sb, rc;
189 private = (struct dasd_fba_private *) block->base->private;
190 rc = dasd_check_blocksize(private->rdc_data.blk_size);
191 if (rc) {
192 DBF_DEV_EVENT(DBF_WARNING, block->base, "unknown blocksize %d",
193 private->rdc_data.blk_size);
194 return rc;
196 block->blocks = private->rdc_data.blk_bdsa;
197 block->bp_block = private->rdc_data.blk_size;
198 block->s2b_shift = 0; /* bits to shift 512 to get a block */
199 for (sb = 512; sb < private->rdc_data.blk_size; sb = sb << 1)
200 block->s2b_shift++;
201 return 0;
204 static int dasd_fba_fill_geometry(struct dasd_block *block,
205 struct hd_geometry *geo)
207 if (dasd_check_blocksize(block->bp_block) != 0)
208 return -EINVAL;
209 geo->cylinders = (block->blocks << block->s2b_shift) >> 10;
210 geo->heads = 16;
211 geo->sectors = 128 >> block->s2b_shift;
212 return 0;
215 static dasd_erp_fn_t
216 dasd_fba_erp_action(struct dasd_ccw_req * cqr)
218 return dasd_default_erp_action;
221 static dasd_erp_fn_t
222 dasd_fba_erp_postaction(struct dasd_ccw_req * cqr)
224 if (cqr->function == dasd_default_erp_action)
225 return dasd_default_erp_postaction;
227 DBF_DEV_EVENT(DBF_WARNING, cqr->startdev, "unknown ERP action %p",
228 cqr->function);
229 return NULL;
232 static void dasd_fba_handle_unsolicited_interrupt(struct dasd_device *device,
233 struct irb *irb)
235 char mask;
237 /* first of all check for state change pending interrupt */
238 mask = DEV_STAT_ATTENTION | DEV_STAT_DEV_END | DEV_STAT_UNIT_EXCEP;
239 if ((irb->scsw.cmd.dstat & mask) == mask) {
240 dasd_generic_handle_state_change(device);
241 return;
244 /* check for unsolicited interrupts */
245 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
246 "unsolicited interrupt received");
247 device->discipline->dump_sense_dbf(device, irb, "unsolicited");
248 dasd_schedule_device_bh(device);
249 return;
252 static struct dasd_ccw_req *dasd_fba_build_cp(struct dasd_device * memdev,
253 struct dasd_block *block,
254 struct request *req)
256 struct dasd_fba_private *private;
257 unsigned long *idaws;
258 struct LO_fba_data *LO_data;
259 struct dasd_ccw_req *cqr;
260 struct ccw1 *ccw;
261 struct req_iterator iter;
262 struct bio_vec *bv;
263 char *dst;
264 int count, cidaw, cplength, datasize;
265 sector_t recid, first_rec, last_rec;
266 unsigned int blksize, off;
267 unsigned char cmd;
269 private = (struct dasd_fba_private *) block->base->private;
270 if (rq_data_dir(req) == READ) {
271 cmd = DASD_FBA_CCW_READ;
272 } else if (rq_data_dir(req) == WRITE) {
273 cmd = DASD_FBA_CCW_WRITE;
274 } else
275 return ERR_PTR(-EINVAL);
276 blksize = block->bp_block;
277 /* Calculate record id of first and last block. */
278 first_rec = blk_rq_pos(req) >> block->s2b_shift;
279 last_rec =
280 (blk_rq_pos(req) + blk_rq_sectors(req) - 1) >> block->s2b_shift;
281 /* Check struct bio and count the number of blocks for the request. */
282 count = 0;
283 cidaw = 0;
284 rq_for_each_segment(bv, req, iter) {
285 if (bv->bv_len & (blksize - 1))
286 /* Fba can only do full blocks. */
287 return ERR_PTR(-EINVAL);
288 count += bv->bv_len >> (block->s2b_shift + 9);
289 #if defined(CONFIG_64BIT)
290 if (idal_is_needed (page_address(bv->bv_page), bv->bv_len))
291 cidaw += bv->bv_len / blksize;
292 #endif
294 /* Paranoia. */
295 if (count != last_rec - first_rec + 1)
296 return ERR_PTR(-EINVAL);
297 /* 1x define extent + 1x locate record + number of blocks */
298 cplength = 2 + count;
299 /* 1x define extent + 1x locate record */
300 datasize = sizeof(struct DE_fba_data) + sizeof(struct LO_fba_data) +
301 cidaw * sizeof(unsigned long);
303 * Find out number of additional locate record ccws if the device
304 * can't do data chaining.
306 if (private->rdc_data.mode.bits.data_chain == 0) {
307 cplength += count - 1;
308 datasize += (count - 1)*sizeof(struct LO_fba_data);
310 /* Allocate the ccw request. */
311 cqr = dasd_smalloc_request(DASD_FBA_MAGIC, cplength, datasize, memdev);
312 if (IS_ERR(cqr))
313 return cqr;
314 ccw = cqr->cpaddr;
315 /* First ccw is define extent. */
316 define_extent(ccw++, cqr->data, rq_data_dir(req),
317 block->bp_block, blk_rq_pos(req), blk_rq_sectors(req));
318 /* Build locate_record + read/write ccws. */
319 idaws = (unsigned long *) (cqr->data + sizeof(struct DE_fba_data));
320 LO_data = (struct LO_fba_data *) (idaws + cidaw);
321 /* Locate record for all blocks for smart devices. */
322 if (private->rdc_data.mode.bits.data_chain != 0) {
323 ccw[-1].flags |= CCW_FLAG_CC;
324 locate_record(ccw++, LO_data++, rq_data_dir(req), 0, count);
326 recid = first_rec;
327 rq_for_each_segment(bv, req, iter) {
328 dst = page_address(bv->bv_page) + bv->bv_offset;
329 if (dasd_page_cache) {
330 char *copy = kmem_cache_alloc(dasd_page_cache,
331 GFP_DMA | __GFP_NOWARN);
332 if (copy && rq_data_dir(req) == WRITE)
333 memcpy(copy + bv->bv_offset, dst, bv->bv_len);
334 if (copy)
335 dst = copy + bv->bv_offset;
337 for (off = 0; off < bv->bv_len; off += blksize) {
338 /* Locate record for stupid devices. */
339 if (private->rdc_data.mode.bits.data_chain == 0) {
340 ccw[-1].flags |= CCW_FLAG_CC;
341 locate_record(ccw, LO_data++,
342 rq_data_dir(req),
343 recid - first_rec, 1);
344 ccw->flags = CCW_FLAG_CC;
345 ccw++;
346 } else {
347 if (recid > first_rec)
348 ccw[-1].flags |= CCW_FLAG_DC;
349 else
350 ccw[-1].flags |= CCW_FLAG_CC;
352 ccw->cmd_code = cmd;
353 ccw->count = block->bp_block;
354 if (idal_is_needed(dst, blksize)) {
355 ccw->cda = (__u32)(addr_t) idaws;
356 ccw->flags = CCW_FLAG_IDA;
357 idaws = idal_create_words(idaws, dst, blksize);
358 } else {
359 ccw->cda = (__u32)(addr_t) dst;
360 ccw->flags = 0;
362 ccw++;
363 dst += blksize;
364 recid++;
367 if (blk_noretry_request(req) ||
368 block->base->features & DASD_FEATURE_FAILFAST)
369 set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
370 cqr->startdev = memdev;
371 cqr->memdev = memdev;
372 cqr->block = block;
373 cqr->expires = 5 * 60 * HZ; /* 5 minutes */
374 cqr->retries = 32;
375 cqr->buildclk = get_clock();
376 cqr->status = DASD_CQR_FILLED;
377 return cqr;
380 static int
381 dasd_fba_free_cp(struct dasd_ccw_req *cqr, struct request *req)
383 struct dasd_fba_private *private;
384 struct ccw1 *ccw;
385 struct req_iterator iter;
386 struct bio_vec *bv;
387 char *dst, *cda;
388 unsigned int blksize, off;
389 int status;
391 if (!dasd_page_cache)
392 goto out;
393 private = (struct dasd_fba_private *) cqr->block->base->private;
394 blksize = cqr->block->bp_block;
395 ccw = cqr->cpaddr;
396 /* Skip over define extent & locate record. */
397 ccw++;
398 if (private->rdc_data.mode.bits.data_chain != 0)
399 ccw++;
400 rq_for_each_segment(bv, req, iter) {
401 dst = page_address(bv->bv_page) + bv->bv_offset;
402 for (off = 0; off < bv->bv_len; off += blksize) {
403 /* Skip locate record. */
404 if (private->rdc_data.mode.bits.data_chain == 0)
405 ccw++;
406 if (dst) {
407 if (ccw->flags & CCW_FLAG_IDA)
408 cda = *((char **)((addr_t) ccw->cda));
409 else
410 cda = (char *)((addr_t) ccw->cda);
411 if (dst != cda) {
412 if (rq_data_dir(req) == READ)
413 memcpy(dst, cda, bv->bv_len);
414 kmem_cache_free(dasd_page_cache,
415 (void *)((addr_t)cda & PAGE_MASK));
417 dst = NULL;
419 ccw++;
422 out:
423 status = cqr->status == DASD_CQR_DONE;
424 dasd_sfree_request(cqr, cqr->memdev);
425 return status;
428 static void dasd_fba_handle_terminated_request(struct dasd_ccw_req *cqr)
430 cqr->status = DASD_CQR_FILLED;
433 static int
434 dasd_fba_fill_info(struct dasd_device * device,
435 struct dasd_information2_t * info)
437 info->label_block = 1;
438 info->FBA_layout = 1;
439 info->format = DASD_FORMAT_LDL;
440 info->characteristics_size = sizeof(struct dasd_fba_characteristics);
441 memcpy(info->characteristics,
442 &((struct dasd_fba_private *) device->private)->rdc_data,
443 sizeof (struct dasd_fba_characteristics));
444 info->confdata_size = 0;
445 return 0;
448 static void
449 dasd_fba_dump_sense_dbf(struct dasd_device *device, struct irb *irb,
450 char *reason)
452 u64 *sense;
454 sense = (u64 *) dasd_get_sense(irb);
455 if (sense) {
456 DBF_DEV_EVENT(DBF_EMERG, device,
457 "%s: %s %02x%02x%02x %016llx %016llx %016llx "
458 "%016llx", reason,
459 scsw_is_tm(&irb->scsw) ? "t" : "c",
460 scsw_cc(&irb->scsw), scsw_cstat(&irb->scsw),
461 scsw_dstat(&irb->scsw), sense[0], sense[1],
462 sense[2], sense[3]);
463 } else {
464 DBF_DEV_EVENT(DBF_EMERG, device, "%s",
465 "SORRY - NO VALID SENSE AVAILABLE\n");
470 static void
471 dasd_fba_dump_sense(struct dasd_device *device, struct dasd_ccw_req * req,
472 struct irb *irb)
474 char *page;
475 struct ccw1 *act, *end, *last;
476 int len, sl, sct, count;
478 page = (char *) get_zeroed_page(GFP_ATOMIC);
479 if (page == NULL) {
480 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
481 "No memory to dump sense data");
482 return;
484 len = sprintf(page, KERN_ERR PRINTK_HEADER
485 " I/O status report for device %s:\n",
486 dev_name(&device->cdev->dev));
487 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
488 " in req: %p CS: 0x%02X DS: 0x%02X\n", req,
489 irb->scsw.cmd.cstat, irb->scsw.cmd.dstat);
490 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
491 " device %s: Failing CCW: %p\n",
492 dev_name(&device->cdev->dev),
493 (void *) (addr_t) irb->scsw.cmd.cpa);
494 if (irb->esw.esw0.erw.cons) {
495 for (sl = 0; sl < 4; sl++) {
496 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
497 " Sense(hex) %2d-%2d:",
498 (8 * sl), ((8 * sl) + 7));
500 for (sct = 0; sct < 8; sct++) {
501 len += sprintf(page + len, " %02x",
502 irb->ecw[8 * sl + sct]);
504 len += sprintf(page + len, "\n");
506 } else {
507 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
508 " SORRY - NO VALID SENSE AVAILABLE\n");
510 printk(KERN_ERR "%s", page);
512 /* dump the Channel Program */
513 /* print first CCWs (maximum 8) */
514 act = req->cpaddr;
515 for (last = act; last->flags & (CCW_FLAG_CC | CCW_FLAG_DC); last++);
516 end = min(act + 8, last);
517 len = sprintf(page, KERN_ERR PRINTK_HEADER
518 " Related CP in req: %p\n", req);
519 while (act <= end) {
520 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
521 " CCW %p: %08X %08X DAT:",
522 act, ((int *) act)[0], ((int *) act)[1]);
523 for (count = 0; count < 32 && count < act->count;
524 count += sizeof(int))
525 len += sprintf(page + len, " %08X",
526 ((int *) (addr_t) act->cda)
527 [(count>>2)]);
528 len += sprintf(page + len, "\n");
529 act++;
531 printk(KERN_ERR "%s", page);
534 /* print failing CCW area */
535 len = 0;
536 if (act < ((struct ccw1 *)(addr_t) irb->scsw.cmd.cpa) - 2) {
537 act = ((struct ccw1 *)(addr_t) irb->scsw.cmd.cpa) - 2;
538 len += sprintf(page + len, KERN_ERR PRINTK_HEADER "......\n");
540 end = min((struct ccw1 *)(addr_t) irb->scsw.cmd.cpa + 2, last);
541 while (act <= end) {
542 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
543 " CCW %p: %08X %08X DAT:",
544 act, ((int *) act)[0], ((int *) act)[1]);
545 for (count = 0; count < 32 && count < act->count;
546 count += sizeof(int))
547 len += sprintf(page + len, " %08X",
548 ((int *) (addr_t) act->cda)
549 [(count>>2)]);
550 len += sprintf(page + len, "\n");
551 act++;
554 /* print last CCWs */
555 if (act < last - 2) {
556 act = last - 2;
557 len += sprintf(page + len, KERN_ERR PRINTK_HEADER "......\n");
559 while (act <= last) {
560 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
561 " CCW %p: %08X %08X DAT:",
562 act, ((int *) act)[0], ((int *) act)[1]);
563 for (count = 0; count < 32 && count < act->count;
564 count += sizeof(int))
565 len += sprintf(page + len, " %08X",
566 ((int *) (addr_t) act->cda)
567 [(count>>2)]);
568 len += sprintf(page + len, "\n");
569 act++;
571 if (len > 0)
572 printk(KERN_ERR "%s", page);
573 free_page((unsigned long) page);
577 * max_blocks is dependent on the amount of storage that is available
578 * in the static io buffer for each device. Currently each device has
579 * 8192 bytes (=2 pages). For 64 bit one dasd_mchunkt_t structure has
580 * 24 bytes, the struct dasd_ccw_req has 136 bytes and each block can use
581 * up to 16 bytes (8 for the ccw and 8 for the idal pointer). In
582 * addition we have one define extent ccw + 16 bytes of data and a
583 * locate record ccw for each block (stupid devices!) + 16 bytes of data.
584 * That makes:
585 * (8192 - 24 - 136 - 8 - 16) / 40 = 200.2 blocks at maximum.
586 * We want to fit two into the available memory so that we can immediately
587 * start the next request if one finishes off. That makes 100.1 blocks
588 * for one request. Give a little safety and the result is 96.
590 static struct dasd_discipline dasd_fba_discipline = {
591 .owner = THIS_MODULE,
592 .name = "FBA ",
593 .ebcname = "FBA ",
594 .max_blocks = 96,
595 .check_device = dasd_fba_check_characteristics,
596 .do_analysis = dasd_fba_do_analysis,
597 .fill_geometry = dasd_fba_fill_geometry,
598 .start_IO = dasd_start_IO,
599 .term_IO = dasd_term_IO,
600 .handle_terminated_request = dasd_fba_handle_terminated_request,
601 .erp_action = dasd_fba_erp_action,
602 .erp_postaction = dasd_fba_erp_postaction,
603 .handle_unsolicited_interrupt = dasd_fba_handle_unsolicited_interrupt,
604 .build_cp = dasd_fba_build_cp,
605 .free_cp = dasd_fba_free_cp,
606 .dump_sense = dasd_fba_dump_sense,
607 .dump_sense_dbf = dasd_fba_dump_sense_dbf,
608 .fill_info = dasd_fba_fill_info,
611 static int __init
612 dasd_fba_init(void)
614 int ret;
616 ASCEBC(dasd_fba_discipline.ebcname, 4);
617 ret = ccw_driver_register(&dasd_fba_driver);
618 if (!ret)
619 wait_for_device_probe();
621 return ret;
624 static void __exit
625 dasd_fba_cleanup(void)
627 ccw_driver_unregister(&dasd_fba_driver);
630 module_init(dasd_fba_init);
631 module_exit(dasd_fba_cleanup);