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