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 = blk_rq_pos(req) >> block->s2b_shift; 274 last_rec = 275 (blk_rq_pos(req) + blk_rq_sectors(req) - 1) >> block->s2b_shift; 276 /* Check struct bio and count the number of blocks for the request. */ 277 count = 0; 278 cidaw = 0; 279 rq_for_each_segment(bv, req, iter) { 280 if (bv->bv_len & (blksize - 1)) 281 /* Fba can only do full blocks. */ 282 return ERR_PTR(-EINVAL); 283 count += bv->bv_len >> (block->s2b_shift + 9); 284 #if defined(CONFIG_64BIT) 285 if (idal_is_needed (page_address(bv->bv_page), bv->bv_len)) 286 cidaw += bv->bv_len / blksize; 287 #endif 288 } 289 /* Paranoia. */ 290 if (count != last_rec - first_rec + 1) 291 return ERR_PTR(-EINVAL); 292 /* 1x define extent + 1x locate record + number of blocks */ 293 cplength = 2 + count; 294 /* 1x define extent + 1x locate record */ 295 datasize = sizeof(struct DE_fba_data) + sizeof(struct LO_fba_data) + 296 cidaw * sizeof(unsigned long); 297 /* 298 * Find out number of additional locate record ccws if the device 299 * can't do data chaining. 300 */ 301 if (private->rdc_data.mode.bits.data_chain == 0) { 302 cplength += count - 1; 303 datasize += (count - 1)*sizeof(struct LO_fba_data); 304 } 305 /* Allocate the ccw request. */ 306 cqr = dasd_smalloc_request(dasd_fba_discipline.name, 307 cplength, datasize, memdev); 308 if (IS_ERR(cqr)) 309 return cqr; 310 ccw = cqr->cpaddr; 311 /* First ccw is define extent. */ 312 define_extent(ccw++, cqr->data, rq_data_dir(req), 313 block->bp_block, blk_rq_pos(req), blk_rq_sectors(req)); 314 /* Build locate_record + read/write ccws. */ 315 idaws = (unsigned long *) (cqr->data + sizeof(struct DE_fba_data)); 316 LO_data = (struct LO_fba_data *) (idaws + cidaw); 317 /* Locate record for all blocks for smart devices. */ 318 if (private->rdc_data.mode.bits.data_chain != 0) { 319 ccw[-1].flags |= CCW_FLAG_CC; 320 locate_record(ccw++, LO_data++, rq_data_dir(req), 0, count); 321 } 322 recid = first_rec; 323 rq_for_each_segment(bv, req, iter) { 324 dst = page_address(bv->bv_page) + bv->bv_offset; 325 if (dasd_page_cache) { 326 char *copy = kmem_cache_alloc(dasd_page_cache, 327 GFP_DMA | __GFP_NOWARN); 328 if (copy && rq_data_dir(req) == WRITE) 329 memcpy(copy + bv->bv_offset, dst, bv->bv_len); 330 if (copy) 331 dst = copy + bv->bv_offset; 332 } 333 for (off = 0; off < bv->bv_len; off += blksize) { 334 /* Locate record for stupid devices. */ 335 if (private->rdc_data.mode.bits.data_chain == 0) { 336 ccw[-1].flags |= CCW_FLAG_CC; 337 locate_record(ccw, LO_data++, 338 rq_data_dir(req), 339 recid - first_rec, 1); 340 ccw->flags = CCW_FLAG_CC; 341 ccw++; 342 } else { 343 if (recid > first_rec) 344 ccw[-1].flags |= CCW_FLAG_DC; 345 else 346 ccw[-1].flags |= CCW_FLAG_CC; 347 } 348 ccw->cmd_code = cmd; 349 ccw->count = block->bp_block; 350 if (idal_is_needed(dst, blksize)) { 351 ccw->cda = (__u32)(addr_t) idaws; 352 ccw->flags = CCW_FLAG_IDA; 353 idaws = idal_create_words(idaws, dst, blksize); 354 } else { 355 ccw->cda = (__u32)(addr_t) dst; 356 ccw->flags = 0; 357 } 358 ccw++; 359 dst += blksize; 360 recid++; 361 } 362 } 363 if (blk_noretry_request(req) || 364 block->base->features & DASD_FEATURE_FAILFAST) 365 set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags); 366 cqr->startdev = memdev; 367 cqr->memdev = memdev; 368 cqr->block = block; 369 cqr->expires = 5 * 60 * HZ; /* 5 minutes */ 370 cqr->retries = 32; 371 cqr->buildclk = get_clock(); 372 cqr->status = DASD_CQR_FILLED; 373 return cqr; 374 } 375 376 static int 377 dasd_fba_free_cp(struct dasd_ccw_req *cqr, struct request *req) 378 { 379 struct dasd_fba_private *private; 380 struct ccw1 *ccw; 381 struct req_iterator iter; 382 struct bio_vec *bv; 383 char *dst, *cda; 384 unsigned int blksize, off; 385 int status; 386 387 if (!dasd_page_cache) 388 goto out; 389 private = (struct dasd_fba_private *) cqr->block->base->private; 390 blksize = cqr->block->bp_block; 391 ccw = cqr->cpaddr; 392 /* Skip over define extent & locate record. */ 393 ccw++; 394 if (private->rdc_data.mode.bits.data_chain != 0) 395 ccw++; 396 rq_for_each_segment(bv, req, iter) { 397 dst = page_address(bv->bv_page) + bv->bv_offset; 398 for (off = 0; off < bv->bv_len; off += blksize) { 399 /* Skip locate record. */ 400 if (private->rdc_data.mode.bits.data_chain == 0) 401 ccw++; 402 if (dst) { 403 if (ccw->flags & CCW_FLAG_IDA) 404 cda = *((char **)((addr_t) ccw->cda)); 405 else 406 cda = (char *)((addr_t) ccw->cda); 407 if (dst != cda) { 408 if (rq_data_dir(req) == READ) 409 memcpy(dst, cda, bv->bv_len); 410 kmem_cache_free(dasd_page_cache, 411 (void *)((addr_t)cda & PAGE_MASK)); 412 } 413 dst = NULL; 414 } 415 ccw++; 416 } 417 } 418 out: 419 status = cqr->status == DASD_CQR_DONE; 420 dasd_sfree_request(cqr, cqr->memdev); 421 return status; 422 } 423 424 static void dasd_fba_handle_terminated_request(struct dasd_ccw_req *cqr) 425 { 426 cqr->status = DASD_CQR_FILLED; 427 }; 428 429 static int 430 dasd_fba_fill_info(struct dasd_device * device, 431 struct dasd_information2_t * info) 432 { 433 info->label_block = 1; 434 info->FBA_layout = 1; 435 info->format = DASD_FORMAT_LDL; 436 info->characteristics_size = sizeof(struct dasd_fba_characteristics); 437 memcpy(info->characteristics, 438 &((struct dasd_fba_private *) device->private)->rdc_data, 439 sizeof (struct dasd_fba_characteristics)); 440 info->confdata_size = 0; 441 return 0; 442 } 443 444 static void 445 dasd_fba_dump_sense_dbf(struct dasd_device *device, struct dasd_ccw_req *req, 446 struct irb *irb, char *reason) 447 { 448 int sl; 449 if (irb->esw.esw0.erw.cons) { 450 for (sl = 0; sl < 4; sl++) { 451 DBF_DEV_EVENT(DBF_EMERG, device, 452 "%s: %08x %08x %08x %08x", 453 reason, irb->ecw[8 * 0], irb->ecw[8 * 1], 454 irb->ecw[8 * 2], irb->ecw[8 * 3]); 455 } 456 } else { 457 DBF_DEV_EVENT(DBF_EMERG, device, "%s", 458 "SORRY - NO VALID SENSE AVAILABLE\n"); 459 } 460 } 461 462 463 static void 464 dasd_fba_dump_sense(struct dasd_device *device, struct dasd_ccw_req * req, 465 struct irb *irb) 466 { 467 char *page; 468 struct ccw1 *act, *end, *last; 469 int len, sl, sct, count; 470 471 page = (char *) get_zeroed_page(GFP_ATOMIC); 472 if (page == NULL) { 473 DBF_DEV_EVENT(DBF_WARNING, device, "%s", 474 "No memory to dump sense data"); 475 return; 476 } 477 len = sprintf(page, KERN_ERR PRINTK_HEADER 478 " I/O status report for device %s:\n", 479 dev_name(&device->cdev->dev)); 480 len += sprintf(page + len, KERN_ERR PRINTK_HEADER 481 " in req: %p CS: 0x%02X DS: 0x%02X\n", req, 482 irb->scsw.cmd.cstat, irb->scsw.cmd.dstat); 483 len += sprintf(page + len, KERN_ERR PRINTK_HEADER 484 " device %s: Failing CCW: %p\n", 485 dev_name(&device->cdev->dev), 486 (void *) (addr_t) irb->scsw.cmd.cpa); 487 if (irb->esw.esw0.erw.cons) { 488 for (sl = 0; sl < 4; sl++) { 489 len += sprintf(page + len, KERN_ERR PRINTK_HEADER 490 " Sense(hex) %2d-%2d:", 491 (8 * sl), ((8 * sl) + 7)); 492 493 for (sct = 0; sct < 8; sct++) { 494 len += sprintf(page + len, " %02x", 495 irb->ecw[8 * sl + sct]); 496 } 497 len += sprintf(page + len, "\n"); 498 } 499 } else { 500 len += sprintf(page + len, KERN_ERR PRINTK_HEADER 501 " SORRY - NO VALID SENSE AVAILABLE\n"); 502 } 503 printk(KERN_ERR "%s", page); 504 505 /* dump the Channel Program */ 506 /* print first CCWs (maximum 8) */ 507 act = req->cpaddr; 508 for (last = act; last->flags & (CCW_FLAG_CC | CCW_FLAG_DC); last++); 509 end = min(act + 8, last); 510 len = sprintf(page, KERN_ERR PRINTK_HEADER 511 " Related CP in req: %p\n", req); 512 while (act <= end) { 513 len += sprintf(page + len, KERN_ERR PRINTK_HEADER 514 " CCW %p: %08X %08X DAT:", 515 act, ((int *) act)[0], ((int *) act)[1]); 516 for (count = 0; count < 32 && count < act->count; 517 count += sizeof(int)) 518 len += sprintf(page + len, " %08X", 519 ((int *) (addr_t) act->cda) 520 [(count>>2)]); 521 len += sprintf(page + len, "\n"); 522 act++; 523 } 524 printk(KERN_ERR "%s", page); 525 526 527 /* print failing CCW area */ 528 len = 0; 529 if (act < ((struct ccw1 *)(addr_t) irb->scsw.cmd.cpa) - 2) { 530 act = ((struct ccw1 *)(addr_t) irb->scsw.cmd.cpa) - 2; 531 len += sprintf(page + len, KERN_ERR PRINTK_HEADER "......\n"); 532 } 533 end = min((struct ccw1 *)(addr_t) irb->scsw.cmd.cpa + 2, last); 534 while (act <= end) { 535 len += sprintf(page + len, KERN_ERR PRINTK_HEADER 536 " CCW %p: %08X %08X DAT:", 537 act, ((int *) act)[0], ((int *) act)[1]); 538 for (count = 0; count < 32 && count < act->count; 539 count += sizeof(int)) 540 len += sprintf(page + len, " %08X", 541 ((int *) (addr_t) act->cda) 542 [(count>>2)]); 543 len += sprintf(page + len, "\n"); 544 act++; 545 } 546 547 /* print last CCWs */ 548 if (act < last - 2) { 549 act = last - 2; 550 len += sprintf(page + len, KERN_ERR PRINTK_HEADER "......\n"); 551 } 552 while (act <= last) { 553 len += sprintf(page + len, KERN_ERR PRINTK_HEADER 554 " CCW %p: %08X %08X DAT:", 555 act, ((int *) act)[0], ((int *) act)[1]); 556 for (count = 0; count < 32 && count < act->count; 557 count += sizeof(int)) 558 len += sprintf(page + len, " %08X", 559 ((int *) (addr_t) act->cda) 560 [(count>>2)]); 561 len += sprintf(page + len, "\n"); 562 act++; 563 } 564 if (len > 0) 565 printk(KERN_ERR "%s", page); 566 free_page((unsigned long) page); 567 } 568 569 /* 570 * max_blocks is dependent on the amount of storage that is available 571 * in the static io buffer for each device. Currently each device has 572 * 8192 bytes (=2 pages). For 64 bit one dasd_mchunkt_t structure has 573 * 24 bytes, the struct dasd_ccw_req has 136 bytes and each block can use 574 * up to 16 bytes (8 for the ccw and 8 for the idal pointer). In 575 * addition we have one define extent ccw + 16 bytes of data and a 576 * locate record ccw for each block (stupid devices!) + 16 bytes of data. 577 * That makes: 578 * (8192 - 24 - 136 - 8 - 16) / 40 = 200.2 blocks at maximum. 579 * We want to fit two into the available memory so that we can immediately 580 * start the next request if one finishes off. That makes 100.1 blocks 581 * for one request. Give a little safety and the result is 96. 582 */ 583 static struct dasd_discipline dasd_fba_discipline = { 584 .owner = THIS_MODULE, 585 .name = "FBA ", 586 .ebcname = "FBA ", 587 .max_blocks = 96, 588 .check_device = dasd_fba_check_characteristics, 589 .do_analysis = dasd_fba_do_analysis, 590 .fill_geometry = dasd_fba_fill_geometry, 591 .start_IO = dasd_start_IO, 592 .term_IO = dasd_term_IO, 593 .handle_terminated_request = dasd_fba_handle_terminated_request, 594 .erp_action = dasd_fba_erp_action, 595 .erp_postaction = dasd_fba_erp_postaction, 596 .handle_unsolicited_interrupt = dasd_fba_handle_unsolicited_interrupt, 597 .build_cp = dasd_fba_build_cp, 598 .free_cp = dasd_fba_free_cp, 599 .dump_sense = dasd_fba_dump_sense, 600 .dump_sense_dbf = dasd_fba_dump_sense_dbf, 601 .fill_info = dasd_fba_fill_info, 602 }; 603 604 static int __init 605 dasd_fba_init(void) 606 { 607 ASCEBC(dasd_fba_discipline.ebcname, 4); 608 return ccw_driver_register(&dasd_fba_driver); 609 } 610 611 static void __exit 612 dasd_fba_cleanup(void) 613 { 614 ccw_driver_unregister(&dasd_fba_driver); 615 } 616 617 module_init(dasd_fba_init); 618 module_exit(dasd_fba_cleanup); 619