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