1 /* 2 * drivers/s390/char/tape_core.c 3 * basic function of the tape device driver 4 * 5 * S390 and zSeries version 6 * Copyright (C) 2001,2005 IBM Deutschland Entwicklung GmbH, IBM Corporation 7 * Author(s): Carsten Otte <cotte@de.ibm.com> 8 * Michael Holzheu <holzheu@de.ibm.com> 9 * Tuan Ngo-Anh <ngoanh@de.ibm.com> 10 * Martin Schwidefsky <schwidefsky@de.ibm.com> 11 * Stefan Bader <shbader@de.ibm.com> 12 */ 13 14 #include <linux/config.h> 15 #include <linux/module.h> 16 #include <linux/init.h> // for kernel parameters 17 #include <linux/kmod.h> // for requesting modules 18 #include <linux/spinlock.h> // for locks 19 #include <linux/vmalloc.h> 20 #include <linux/list.h> 21 22 #include <asm/types.h> // for variable types 23 24 #define TAPE_DBF_AREA tape_core_dbf 25 26 #include "tape.h" 27 #include "tape_std.h" 28 29 #define PRINTK_HEADER "TAPE_CORE: " 30 31 static void __tape_do_irq (struct ccw_device *, unsigned long, struct irb *); 32 static void tape_delayed_next_request(void * data); 33 34 /* 35 * One list to contain all tape devices of all disciplines, so 36 * we can assign the devices to minor numbers of the same major 37 * The list is protected by the rwlock 38 */ 39 static struct list_head tape_device_list = LIST_HEAD_INIT(tape_device_list); 40 static DEFINE_RWLOCK(tape_device_lock); 41 42 /* 43 * Pointer to debug area. 44 */ 45 debug_info_t *TAPE_DBF_AREA = NULL; 46 EXPORT_SYMBOL(TAPE_DBF_AREA); 47 48 /* 49 * Printable strings for tape enumerations. 50 */ 51 const char *tape_state_verbose[TS_SIZE] = 52 { 53 [TS_UNUSED] = "UNUSED", 54 [TS_IN_USE] = "IN_USE", 55 [TS_BLKUSE] = "BLKUSE", 56 [TS_INIT] = "INIT ", 57 [TS_NOT_OPER] = "NOT_OP" 58 }; 59 60 const char *tape_op_verbose[TO_SIZE] = 61 { 62 [TO_BLOCK] = "BLK", [TO_BSB] = "BSB", 63 [TO_BSF] = "BSF", [TO_DSE] = "DSE", 64 [TO_FSB] = "FSB", [TO_FSF] = "FSF", 65 [TO_LBL] = "LBL", [TO_NOP] = "NOP", 66 [TO_RBA] = "RBA", [TO_RBI] = "RBI", 67 [TO_RFO] = "RFO", [TO_REW] = "REW", 68 [TO_RUN] = "RUN", [TO_WRI] = "WRI", 69 [TO_WTM] = "WTM", [TO_MSEN] = "MSN", 70 [TO_LOAD] = "LOA", [TO_READ_CONFIG] = "RCF", 71 [TO_READ_ATTMSG] = "RAT", 72 [TO_DIS] = "DIS", [TO_ASSIGN] = "ASS", 73 [TO_UNASSIGN] = "UAS" 74 }; 75 76 static inline int 77 busid_to_int(char *bus_id) 78 { 79 int dec; 80 int d; 81 char * s; 82 83 for(s = bus_id, d = 0; *s != '\0' && *s != '.'; s++) 84 d = (d * 10) + (*s - '0'); 85 dec = d; 86 for(s++, d = 0; *s != '\0' && *s != '.'; s++) 87 d = (d * 10) + (*s - '0'); 88 dec = (dec << 8) + d; 89 90 for(s++; *s != '\0'; s++) { 91 if (*s >= '0' && *s <= '9') { 92 d = *s - '0'; 93 } else if (*s >= 'a' && *s <= 'f') { 94 d = *s - 'a' + 10; 95 } else { 96 d = *s - 'A' + 10; 97 } 98 dec = (dec << 4) + d; 99 } 100 101 return dec; 102 } 103 104 /* 105 * Some channel attached tape specific attributes. 106 * 107 * FIXME: In the future the first_minor and blocksize attribute should be 108 * replaced by a link to the cdev tree. 109 */ 110 static ssize_t 111 tape_medium_state_show(struct device *dev, struct device_attribute *attr, char *buf) 112 { 113 struct tape_device *tdev; 114 115 tdev = (struct tape_device *) dev->driver_data; 116 return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->medium_state); 117 } 118 119 static 120 DEVICE_ATTR(medium_state, 0444, tape_medium_state_show, NULL); 121 122 static ssize_t 123 tape_first_minor_show(struct device *dev, struct device_attribute *attr, char *buf) 124 { 125 struct tape_device *tdev; 126 127 tdev = (struct tape_device *) dev->driver_data; 128 return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->first_minor); 129 } 130 131 static 132 DEVICE_ATTR(first_minor, 0444, tape_first_minor_show, NULL); 133 134 static ssize_t 135 tape_state_show(struct device *dev, struct device_attribute *attr, char *buf) 136 { 137 struct tape_device *tdev; 138 139 tdev = (struct tape_device *) dev->driver_data; 140 return scnprintf(buf, PAGE_SIZE, "%s\n", (tdev->first_minor < 0) ? 141 "OFFLINE" : tape_state_verbose[tdev->tape_state]); 142 } 143 144 static 145 DEVICE_ATTR(state, 0444, tape_state_show, NULL); 146 147 static ssize_t 148 tape_operation_show(struct device *dev, struct device_attribute *attr, char *buf) 149 { 150 struct tape_device *tdev; 151 ssize_t rc; 152 153 tdev = (struct tape_device *) dev->driver_data; 154 if (tdev->first_minor < 0) 155 return scnprintf(buf, PAGE_SIZE, "N/A\n"); 156 157 spin_lock_irq(get_ccwdev_lock(tdev->cdev)); 158 if (list_empty(&tdev->req_queue)) 159 rc = scnprintf(buf, PAGE_SIZE, "---\n"); 160 else { 161 struct tape_request *req; 162 163 req = list_entry(tdev->req_queue.next, struct tape_request, 164 list); 165 rc = scnprintf(buf,PAGE_SIZE, "%s\n", tape_op_verbose[req->op]); 166 } 167 spin_unlock_irq(get_ccwdev_lock(tdev->cdev)); 168 return rc; 169 } 170 171 static 172 DEVICE_ATTR(operation, 0444, tape_operation_show, NULL); 173 174 static ssize_t 175 tape_blocksize_show(struct device *dev, struct device_attribute *attr, char *buf) 176 { 177 struct tape_device *tdev; 178 179 tdev = (struct tape_device *) dev->driver_data; 180 181 return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->char_data.block_size); 182 } 183 184 static 185 DEVICE_ATTR(blocksize, 0444, tape_blocksize_show, NULL); 186 187 static struct attribute *tape_attrs[] = { 188 &dev_attr_medium_state.attr, 189 &dev_attr_first_minor.attr, 190 &dev_attr_state.attr, 191 &dev_attr_operation.attr, 192 &dev_attr_blocksize.attr, 193 NULL 194 }; 195 196 static struct attribute_group tape_attr_group = { 197 .attrs = tape_attrs, 198 }; 199 200 /* 201 * Tape state functions 202 */ 203 void 204 tape_state_set(struct tape_device *device, enum tape_state newstate) 205 { 206 const char *str; 207 208 if (device->tape_state == TS_NOT_OPER) { 209 DBF_EVENT(3, "ts_set err: not oper\n"); 210 return; 211 } 212 DBF_EVENT(4, "ts. dev: %x\n", device->first_minor); 213 if (device->tape_state < TO_SIZE && device->tape_state >= 0) 214 str = tape_state_verbose[device->tape_state]; 215 else 216 str = "UNKNOWN TS"; 217 DBF_EVENT(4, "old ts: %s\n", str); 218 if (device->tape_state < TO_SIZE && device->tape_state >=0 ) 219 str = tape_state_verbose[device->tape_state]; 220 else 221 str = "UNKNOWN TS"; 222 DBF_EVENT(4, "%s\n", str); 223 DBF_EVENT(4, "new ts:\t\n"); 224 if (newstate < TO_SIZE && newstate >= 0) 225 str = tape_state_verbose[newstate]; 226 else 227 str = "UNKNOWN TS"; 228 DBF_EVENT(4, "%s\n", str); 229 device->tape_state = newstate; 230 wake_up(&device->state_change_wq); 231 } 232 233 void 234 tape_med_state_set(struct tape_device *device, enum tape_medium_state newstate) 235 { 236 if (device->medium_state == newstate) 237 return; 238 switch(newstate){ 239 case MS_UNLOADED: 240 device->tape_generic_status |= GMT_DR_OPEN(~0); 241 PRINT_INFO("(%s): Tape is unloaded\n", 242 device->cdev->dev.bus_id); 243 break; 244 case MS_LOADED: 245 device->tape_generic_status &= ~GMT_DR_OPEN(~0); 246 PRINT_INFO("(%s): Tape has been mounted\n", 247 device->cdev->dev.bus_id); 248 break; 249 default: 250 // print nothing 251 break; 252 } 253 device->medium_state = newstate; 254 wake_up(&device->state_change_wq); 255 } 256 257 /* 258 * Stop running ccw. Has to be called with the device lock held. 259 */ 260 static inline int 261 __tape_cancel_io(struct tape_device *device, struct tape_request *request) 262 { 263 int retries; 264 int rc; 265 266 /* Check if interrupt has already been processed */ 267 if (request->callback == NULL) 268 return 0; 269 270 rc = 0; 271 for (retries = 0; retries < 5; retries++) { 272 rc = ccw_device_clear(device->cdev, (long) request); 273 274 switch (rc) { 275 case 0: 276 request->status = TAPE_REQUEST_DONE; 277 return 0; 278 case -EBUSY: 279 request->status = TAPE_REQUEST_CANCEL; 280 schedule_work(&device->tape_dnr); 281 return 0; 282 case -ENODEV: 283 DBF_EXCEPTION(2, "device gone, retry\n"); 284 break; 285 case -EIO: 286 DBF_EXCEPTION(2, "I/O error, retry\n"); 287 break; 288 default: 289 BUG(); 290 } 291 } 292 293 return rc; 294 } 295 296 /* 297 * Add device into the sorted list, giving it the first 298 * available minor number. 299 */ 300 static int 301 tape_assign_minor(struct tape_device *device) 302 { 303 struct tape_device *tmp; 304 int minor; 305 306 minor = 0; 307 write_lock(&tape_device_lock); 308 list_for_each_entry(tmp, &tape_device_list, node) { 309 if (minor < tmp->first_minor) 310 break; 311 minor += TAPE_MINORS_PER_DEV; 312 } 313 if (minor >= 256) { 314 write_unlock(&tape_device_lock); 315 return -ENODEV; 316 } 317 device->first_minor = minor; 318 list_add_tail(&device->node, &tmp->node); 319 write_unlock(&tape_device_lock); 320 return 0; 321 } 322 323 /* remove device from the list */ 324 static void 325 tape_remove_minor(struct tape_device *device) 326 { 327 write_lock(&tape_device_lock); 328 list_del_init(&device->node); 329 device->first_minor = -1; 330 write_unlock(&tape_device_lock); 331 } 332 333 /* 334 * Set a device online. 335 * 336 * This function is called by the common I/O layer to move a device from the 337 * detected but offline into the online state. 338 * If we return an error (RC < 0) the device remains in the offline state. This 339 * can happen if the device is assigned somewhere else, for example. 340 */ 341 int 342 tape_generic_online(struct tape_device *device, 343 struct tape_discipline *discipline) 344 { 345 int rc; 346 347 DBF_LH(6, "tape_enable_device(%p, %p)\n", device, discipline); 348 349 if (device->tape_state != TS_INIT) { 350 DBF_LH(3, "Tapestate not INIT (%d)\n", device->tape_state); 351 return -EINVAL; 352 } 353 354 /* Let the discipline have a go at the device. */ 355 device->discipline = discipline; 356 if (!try_module_get(discipline->owner)) { 357 PRINT_ERR("Cannot get module. Module gone.\n"); 358 return -EINVAL; 359 } 360 361 rc = discipline->setup_device(device); 362 if (rc) 363 goto out; 364 rc = tape_assign_minor(device); 365 if (rc) 366 goto out_discipline; 367 368 rc = tapechar_setup_device(device); 369 if (rc) 370 goto out_minor; 371 rc = tapeblock_setup_device(device); 372 if (rc) 373 goto out_char; 374 375 tape_state_set(device, TS_UNUSED); 376 377 DBF_LH(3, "(%08x): Drive set online\n", device->cdev_id); 378 379 return 0; 380 381 out_char: 382 tapechar_cleanup_device(device); 383 out_discipline: 384 device->discipline->cleanup_device(device); 385 device->discipline = NULL; 386 out_minor: 387 tape_remove_minor(device); 388 out: 389 module_put(discipline->owner); 390 return rc; 391 } 392 393 static inline void 394 tape_cleanup_device(struct tape_device *device) 395 { 396 tapeblock_cleanup_device(device); 397 tapechar_cleanup_device(device); 398 device->discipline->cleanup_device(device); 399 module_put(device->discipline->owner); 400 tape_remove_minor(device); 401 tape_med_state_set(device, MS_UNKNOWN); 402 } 403 404 /* 405 * Set device offline. 406 * 407 * Called by the common I/O layer if the drive should set offline on user 408 * request. We may prevent this by returning an error. 409 * Manual offline is only allowed while the drive is not in use. 410 */ 411 int 412 tape_generic_offline(struct tape_device *device) 413 { 414 if (!device) { 415 PRINT_ERR("tape_generic_offline: no such device\n"); 416 return -ENODEV; 417 } 418 419 DBF_LH(3, "(%08x): tape_generic_offline(%p)\n", 420 device->cdev_id, device); 421 422 spin_lock_irq(get_ccwdev_lock(device->cdev)); 423 switch (device->tape_state) { 424 case TS_INIT: 425 case TS_NOT_OPER: 426 spin_unlock_irq(get_ccwdev_lock(device->cdev)); 427 break; 428 case TS_UNUSED: 429 tape_state_set(device, TS_INIT); 430 spin_unlock_irq(get_ccwdev_lock(device->cdev)); 431 tape_cleanup_device(device); 432 break; 433 default: 434 DBF_EVENT(3, "(%08x): Set offline failed " 435 "- drive in use.\n", 436 device->cdev_id); 437 PRINT_WARN("(%s): Set offline failed " 438 "- drive in use.\n", 439 device->cdev->dev.bus_id); 440 spin_unlock_irq(get_ccwdev_lock(device->cdev)); 441 return -EBUSY; 442 } 443 444 DBF_LH(3, "(%08x): Drive set offline.\n", device->cdev_id); 445 return 0; 446 } 447 448 /* 449 * Allocate memory for a new device structure. 450 */ 451 static struct tape_device * 452 tape_alloc_device(void) 453 { 454 struct tape_device *device; 455 456 device = kzalloc(sizeof(struct tape_device), GFP_KERNEL); 457 if (device == NULL) { 458 DBF_EXCEPTION(2, "ti:no mem\n"); 459 PRINT_INFO ("can't allocate memory for " 460 "tape info structure\n"); 461 return ERR_PTR(-ENOMEM); 462 } 463 device->modeset_byte = kmalloc(1, GFP_KERNEL | GFP_DMA); 464 if (device->modeset_byte == NULL) { 465 DBF_EXCEPTION(2, "ti:no mem\n"); 466 PRINT_INFO("can't allocate memory for modeset byte\n"); 467 kfree(device); 468 return ERR_PTR(-ENOMEM); 469 } 470 INIT_LIST_HEAD(&device->req_queue); 471 INIT_LIST_HEAD(&device->node); 472 init_waitqueue_head(&device->state_change_wq); 473 device->tape_state = TS_INIT; 474 device->medium_state = MS_UNKNOWN; 475 *device->modeset_byte = 0; 476 device->first_minor = -1; 477 atomic_set(&device->ref_count, 1); 478 INIT_WORK(&device->tape_dnr, tape_delayed_next_request, device); 479 480 return device; 481 } 482 483 /* 484 * Get a reference to an existing device structure. This will automatically 485 * increment the reference count. 486 */ 487 struct tape_device * 488 tape_get_device_reference(struct tape_device *device) 489 { 490 DBF_EVENT(4, "tape_get_device_reference(%p) = %i\n", device, 491 atomic_inc_return(&device->ref_count)); 492 493 return device; 494 } 495 496 /* 497 * Decrease the reference counter of a devices structure. If the 498 * reference counter reaches zero free the device structure. 499 * The function returns a NULL pointer to be used by the caller 500 * for clearing reference pointers. 501 */ 502 struct tape_device * 503 tape_put_device(struct tape_device *device) 504 { 505 int remain; 506 507 remain = atomic_dec_return(&device->ref_count); 508 if (remain > 0) { 509 DBF_EVENT(4, "tape_put_device(%p) -> %i\n", device, remain); 510 } else { 511 if (remain < 0) { 512 DBF_EVENT(4, "put device without reference\n"); 513 PRINT_ERR("put device without reference\n"); 514 } else { 515 DBF_EVENT(4, "tape_free_device(%p)\n", device); 516 kfree(device->modeset_byte); 517 kfree(device); 518 } 519 } 520 521 return NULL; 522 } 523 524 /* 525 * Find tape device by a device index. 526 */ 527 struct tape_device * 528 tape_get_device(int devindex) 529 { 530 struct tape_device *device, *tmp; 531 532 device = ERR_PTR(-ENODEV); 533 read_lock(&tape_device_lock); 534 list_for_each_entry(tmp, &tape_device_list, node) { 535 if (tmp->first_minor / TAPE_MINORS_PER_DEV == devindex) { 536 device = tape_get_device_reference(tmp); 537 break; 538 } 539 } 540 read_unlock(&tape_device_lock); 541 return device; 542 } 543 544 /* 545 * Driverfs tape probe function. 546 */ 547 int 548 tape_generic_probe(struct ccw_device *cdev) 549 { 550 struct tape_device *device; 551 552 device = tape_alloc_device(); 553 if (IS_ERR(device)) 554 return -ENODEV; 555 PRINT_INFO("tape device %s found\n", cdev->dev.bus_id); 556 cdev->dev.driver_data = device; 557 device->cdev = cdev; 558 device->cdev_id = busid_to_int(cdev->dev.bus_id); 559 cdev->handler = __tape_do_irq; 560 561 ccw_device_set_options(cdev, CCWDEV_DO_PATHGROUP); 562 sysfs_create_group(&cdev->dev.kobj, &tape_attr_group); 563 564 return 0; 565 } 566 567 static inline void 568 __tape_discard_requests(struct tape_device *device) 569 { 570 struct tape_request * request; 571 struct list_head * l, *n; 572 573 list_for_each_safe(l, n, &device->req_queue) { 574 request = list_entry(l, struct tape_request, list); 575 if (request->status == TAPE_REQUEST_IN_IO) 576 request->status = TAPE_REQUEST_DONE; 577 list_del(&request->list); 578 579 /* Decrease ref_count for removed request. */ 580 request->device = tape_put_device(device); 581 request->rc = -EIO; 582 if (request->callback != NULL) 583 request->callback(request, request->callback_data); 584 } 585 } 586 587 /* 588 * Driverfs tape remove function. 589 * 590 * This function is called whenever the common I/O layer detects the device 591 * gone. This can happen at any time and we cannot refuse. 592 */ 593 void 594 tape_generic_remove(struct ccw_device *cdev) 595 { 596 struct tape_device * device; 597 598 device = cdev->dev.driver_data; 599 if (!device) { 600 PRINT_ERR("No device pointer in tape_generic_remove!\n"); 601 return; 602 } 603 DBF_LH(3, "(%08x): tape_generic_remove(%p)\n", device->cdev_id, cdev); 604 605 spin_lock_irq(get_ccwdev_lock(device->cdev)); 606 switch (device->tape_state) { 607 case TS_INIT: 608 tape_state_set(device, TS_NOT_OPER); 609 case TS_NOT_OPER: 610 /* 611 * Nothing to do. 612 */ 613 spin_unlock_irq(get_ccwdev_lock(device->cdev)); 614 break; 615 case TS_UNUSED: 616 /* 617 * Need only to release the device. 618 */ 619 tape_state_set(device, TS_NOT_OPER); 620 spin_unlock_irq(get_ccwdev_lock(device->cdev)); 621 tape_cleanup_device(device); 622 break; 623 default: 624 /* 625 * There may be requests on the queue. We will not get 626 * an interrupt for a request that was running. So we 627 * just post them all as I/O errors. 628 */ 629 DBF_EVENT(3, "(%08x): Drive in use vanished!\n", 630 device->cdev_id); 631 PRINT_WARN("(%s): Drive in use vanished - " 632 "expect trouble!\n", 633 device->cdev->dev.bus_id); 634 PRINT_WARN("State was %i\n", device->tape_state); 635 tape_state_set(device, TS_NOT_OPER); 636 __tape_discard_requests(device); 637 spin_unlock_irq(get_ccwdev_lock(device->cdev)); 638 tape_cleanup_device(device); 639 } 640 641 if (cdev->dev.driver_data != NULL) { 642 sysfs_remove_group(&cdev->dev.kobj, &tape_attr_group); 643 cdev->dev.driver_data = tape_put_device(cdev->dev.driver_data); 644 } 645 } 646 647 /* 648 * Allocate a new tape ccw request 649 */ 650 struct tape_request * 651 tape_alloc_request(int cplength, int datasize) 652 { 653 struct tape_request *request; 654 655 if (datasize > PAGE_SIZE || (cplength*sizeof(struct ccw1)) > PAGE_SIZE) 656 BUG(); 657 658 DBF_LH(6, "tape_alloc_request(%d, %d)\n", cplength, datasize); 659 660 request = kzalloc(sizeof(struct tape_request), GFP_KERNEL); 661 if (request == NULL) { 662 DBF_EXCEPTION(1, "cqra nomem\n"); 663 return ERR_PTR(-ENOMEM); 664 } 665 /* allocate channel program */ 666 if (cplength > 0) { 667 request->cpaddr = kcalloc(cplength, sizeof(struct ccw1), 668 GFP_ATOMIC | GFP_DMA); 669 if (request->cpaddr == NULL) { 670 DBF_EXCEPTION(1, "cqra nomem\n"); 671 kfree(request); 672 return ERR_PTR(-ENOMEM); 673 } 674 } 675 /* alloc small kernel buffer */ 676 if (datasize > 0) { 677 request->cpdata = kzalloc(datasize, GFP_KERNEL | GFP_DMA); 678 if (request->cpdata == NULL) { 679 DBF_EXCEPTION(1, "cqra nomem\n"); 680 kfree(request->cpaddr); 681 kfree(request); 682 return ERR_PTR(-ENOMEM); 683 } 684 } 685 DBF_LH(6, "New request %p(%p/%p)\n", request, request->cpaddr, 686 request->cpdata); 687 688 return request; 689 } 690 691 /* 692 * Free tape ccw request 693 */ 694 void 695 tape_free_request (struct tape_request * request) 696 { 697 DBF_LH(6, "Free request %p\n", request); 698 699 if (request->device != NULL) { 700 request->device = tape_put_device(request->device); 701 } 702 kfree(request->cpdata); 703 kfree(request->cpaddr); 704 kfree(request); 705 } 706 707 static inline int 708 __tape_start_io(struct tape_device *device, struct tape_request *request) 709 { 710 int rc; 711 712 #ifdef CONFIG_S390_TAPE_BLOCK 713 if (request->op == TO_BLOCK) 714 device->discipline->check_locate(device, request); 715 #endif 716 rc = ccw_device_start( 717 device->cdev, 718 request->cpaddr, 719 (unsigned long) request, 720 0x00, 721 request->options 722 ); 723 if (rc == 0) { 724 request->status = TAPE_REQUEST_IN_IO; 725 } else if (rc == -EBUSY) { 726 /* The common I/O subsystem is currently busy. Retry later. */ 727 request->status = TAPE_REQUEST_QUEUED; 728 schedule_work(&device->tape_dnr); 729 rc = 0; 730 } else { 731 /* Start failed. Remove request and indicate failure. */ 732 DBF_EVENT(1, "tape: start request failed with RC = %i\n", rc); 733 } 734 return rc; 735 } 736 737 static inline void 738 __tape_start_next_request(struct tape_device *device) 739 { 740 struct list_head *l, *n; 741 struct tape_request *request; 742 int rc; 743 744 DBF_LH(6, "__tape_start_next_request(%p)\n", device); 745 /* 746 * Try to start each request on request queue until one is 747 * started successful. 748 */ 749 list_for_each_safe(l, n, &device->req_queue) { 750 request = list_entry(l, struct tape_request, list); 751 752 /* 753 * Avoid race condition if bottom-half was triggered more than 754 * once. 755 */ 756 if (request->status == TAPE_REQUEST_IN_IO) 757 return; 758 /* 759 * Request has already been stopped. We have to wait until 760 * the request is removed from the queue in the interrupt 761 * handling. 762 */ 763 if (request->status == TAPE_REQUEST_DONE) 764 return; 765 766 /* 767 * We wanted to cancel the request but the common I/O layer 768 * was busy at that time. This can only happen if this 769 * function is called by delayed_next_request. 770 * Otherwise we start the next request on the queue. 771 */ 772 if (request->status == TAPE_REQUEST_CANCEL) { 773 rc = __tape_cancel_io(device, request); 774 } else { 775 rc = __tape_start_io(device, request); 776 } 777 if (rc == 0) 778 return; 779 780 /* Set ending status. */ 781 request->rc = rc; 782 request->status = TAPE_REQUEST_DONE; 783 784 /* Remove from request queue. */ 785 list_del(&request->list); 786 787 /* Do callback. */ 788 if (request->callback != NULL) 789 request->callback(request, request->callback_data); 790 } 791 } 792 793 static void 794 tape_delayed_next_request(void *data) 795 { 796 struct tape_device * device; 797 798 device = (struct tape_device *) data; 799 DBF_LH(6, "tape_delayed_next_request(%p)\n", device); 800 spin_lock_irq(get_ccwdev_lock(device->cdev)); 801 __tape_start_next_request(device); 802 spin_unlock_irq(get_ccwdev_lock(device->cdev)); 803 } 804 805 static inline void 806 __tape_end_request( 807 struct tape_device * device, 808 struct tape_request * request, 809 int rc) 810 { 811 DBF_LH(6, "__tape_end_request(%p, %p, %i)\n", device, request, rc); 812 if (request) { 813 request->rc = rc; 814 request->status = TAPE_REQUEST_DONE; 815 816 /* Remove from request queue. */ 817 list_del(&request->list); 818 819 /* Do callback. */ 820 if (request->callback != NULL) 821 request->callback(request, request->callback_data); 822 } 823 824 /* Start next request. */ 825 if (!list_empty(&device->req_queue)) 826 __tape_start_next_request(device); 827 } 828 829 /* 830 * Write sense data to console/dbf 831 */ 832 void 833 tape_dump_sense(struct tape_device* device, struct tape_request *request, 834 struct irb *irb) 835 { 836 unsigned int *sptr; 837 838 PRINT_INFO("-------------------------------------------------\n"); 839 PRINT_INFO("DSTAT : %02x CSTAT: %02x CPA: %04x\n", 840 irb->scsw.dstat, irb->scsw.cstat, irb->scsw.cpa); 841 PRINT_INFO("DEVICE: %s\n", device->cdev->dev.bus_id); 842 if (request != NULL) 843 PRINT_INFO("OP : %s\n", tape_op_verbose[request->op]); 844 845 sptr = (unsigned int *) irb->ecw; 846 PRINT_INFO("Sense data: %08X %08X %08X %08X \n", 847 sptr[0], sptr[1], sptr[2], sptr[3]); 848 PRINT_INFO("Sense data: %08X %08X %08X %08X \n", 849 sptr[4], sptr[5], sptr[6], sptr[7]); 850 PRINT_INFO("--------------------------------------------------\n"); 851 } 852 853 /* 854 * Write sense data to dbf 855 */ 856 void 857 tape_dump_sense_dbf(struct tape_device *device, struct tape_request *request, 858 struct irb *irb) 859 { 860 unsigned int *sptr; 861 const char* op; 862 863 if (request != NULL) 864 op = tape_op_verbose[request->op]; 865 else 866 op = "---"; 867 DBF_EVENT(3, "DSTAT : %02x CSTAT: %02x\n", 868 irb->scsw.dstat,irb->scsw.cstat); 869 DBF_EVENT(3, "DEVICE: %08x OP\t: %s\n", device->cdev_id, op); 870 sptr = (unsigned int *) irb->ecw; 871 DBF_EVENT(3, "%08x %08x\n", sptr[0], sptr[1]); 872 DBF_EVENT(3, "%08x %08x\n", sptr[2], sptr[3]); 873 DBF_EVENT(3, "%08x %08x\n", sptr[4], sptr[5]); 874 DBF_EVENT(3, "%08x %08x\n", sptr[6], sptr[7]); 875 } 876 877 /* 878 * I/O helper function. Adds the request to the request queue 879 * and starts it if the tape is idle. Has to be called with 880 * the device lock held. 881 */ 882 static inline int 883 __tape_start_request(struct tape_device *device, struct tape_request *request) 884 { 885 int rc; 886 887 switch (request->op) { 888 case TO_MSEN: 889 case TO_ASSIGN: 890 case TO_UNASSIGN: 891 case TO_READ_ATTMSG: 892 if (device->tape_state == TS_INIT) 893 break; 894 if (device->tape_state == TS_UNUSED) 895 break; 896 default: 897 if (device->tape_state == TS_BLKUSE) 898 break; 899 if (device->tape_state != TS_IN_USE) 900 return -ENODEV; 901 } 902 903 /* Increase use count of device for the added request. */ 904 request->device = tape_get_device_reference(device); 905 906 if (list_empty(&device->req_queue)) { 907 /* No other requests are on the queue. Start this one. */ 908 rc = __tape_start_io(device, request); 909 if (rc) 910 return rc; 911 912 DBF_LH(5, "Request %p added for execution.\n", request); 913 list_add(&request->list, &device->req_queue); 914 } else { 915 DBF_LH(5, "Request %p add to queue.\n", request); 916 request->status = TAPE_REQUEST_QUEUED; 917 list_add_tail(&request->list, &device->req_queue); 918 } 919 return 0; 920 } 921 922 /* 923 * Add the request to the request queue, try to start it if the 924 * tape is idle. Return without waiting for end of i/o. 925 */ 926 int 927 tape_do_io_async(struct tape_device *device, struct tape_request *request) 928 { 929 int rc; 930 931 DBF_LH(6, "tape_do_io_async(%p, %p)\n", device, request); 932 933 spin_lock_irq(get_ccwdev_lock(device->cdev)); 934 /* Add request to request queue and try to start it. */ 935 rc = __tape_start_request(device, request); 936 spin_unlock_irq(get_ccwdev_lock(device->cdev)); 937 return rc; 938 } 939 940 /* 941 * tape_do_io/__tape_wake_up 942 * Add the request to the request queue, try to start it if the 943 * tape is idle and wait uninterruptible for its completion. 944 */ 945 static void 946 __tape_wake_up(struct tape_request *request, void *data) 947 { 948 request->callback = NULL; 949 wake_up((wait_queue_head_t *) data); 950 } 951 952 int 953 tape_do_io(struct tape_device *device, struct tape_request *request) 954 { 955 wait_queue_head_t wq; 956 int rc; 957 958 init_waitqueue_head(&wq); 959 spin_lock_irq(get_ccwdev_lock(device->cdev)); 960 /* Setup callback */ 961 request->callback = __tape_wake_up; 962 request->callback_data = &wq; 963 /* Add request to request queue and try to start it. */ 964 rc = __tape_start_request(device, request); 965 spin_unlock_irq(get_ccwdev_lock(device->cdev)); 966 if (rc) 967 return rc; 968 /* Request added to the queue. Wait for its completion. */ 969 wait_event(wq, (request->callback == NULL)); 970 /* Get rc from request */ 971 return request->rc; 972 } 973 974 /* 975 * tape_do_io_interruptible/__tape_wake_up_interruptible 976 * Add the request to the request queue, try to start it if the 977 * tape is idle and wait uninterruptible for its completion. 978 */ 979 static void 980 __tape_wake_up_interruptible(struct tape_request *request, void *data) 981 { 982 request->callback = NULL; 983 wake_up_interruptible((wait_queue_head_t *) data); 984 } 985 986 int 987 tape_do_io_interruptible(struct tape_device *device, 988 struct tape_request *request) 989 { 990 wait_queue_head_t wq; 991 int rc; 992 993 init_waitqueue_head(&wq); 994 spin_lock_irq(get_ccwdev_lock(device->cdev)); 995 /* Setup callback */ 996 request->callback = __tape_wake_up_interruptible; 997 request->callback_data = &wq; 998 rc = __tape_start_request(device, request); 999 spin_unlock_irq(get_ccwdev_lock(device->cdev)); 1000 if (rc) 1001 return rc; 1002 /* Request added to the queue. Wait for its completion. */ 1003 rc = wait_event_interruptible(wq, (request->callback == NULL)); 1004 if (rc != -ERESTARTSYS) 1005 /* Request finished normally. */ 1006 return request->rc; 1007 1008 /* Interrupted by a signal. We have to stop the current request. */ 1009 spin_lock_irq(get_ccwdev_lock(device->cdev)); 1010 rc = __tape_cancel_io(device, request); 1011 spin_unlock_irq(get_ccwdev_lock(device->cdev)); 1012 if (rc == 0) { 1013 /* Wait for the interrupt that acknowledges the halt. */ 1014 do { 1015 rc = wait_event_interruptible( 1016 wq, 1017 (request->callback == NULL) 1018 ); 1019 } while (rc == -ERESTARTSYS); 1020 1021 DBF_EVENT(3, "IO stopped on %08x\n", device->cdev_id); 1022 rc = -ERESTARTSYS; 1023 } 1024 return rc; 1025 } 1026 1027 /* 1028 * Stop running ccw. 1029 */ 1030 int 1031 tape_cancel_io(struct tape_device *device, struct tape_request *request) 1032 { 1033 int rc; 1034 1035 spin_lock_irq(get_ccwdev_lock(device->cdev)); 1036 rc = __tape_cancel_io(device, request); 1037 spin_unlock_irq(get_ccwdev_lock(device->cdev)); 1038 return rc; 1039 } 1040 1041 /* 1042 * Tape interrupt routine, called from the ccw_device layer 1043 */ 1044 static void 1045 __tape_do_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb) 1046 { 1047 struct tape_device *device; 1048 struct tape_request *request; 1049 int rc; 1050 1051 device = (struct tape_device *) cdev->dev.driver_data; 1052 if (device == NULL) { 1053 PRINT_ERR("could not get device structure for %s " 1054 "in interrupt\n", cdev->dev.bus_id); 1055 return; 1056 } 1057 request = (struct tape_request *) intparm; 1058 1059 DBF_LH(6, "__tape_do_irq(device=%p, request=%p)\n", device, request); 1060 1061 /* On special conditions irb is an error pointer */ 1062 if (IS_ERR(irb)) { 1063 /* FIXME: What to do with the request? */ 1064 switch (PTR_ERR(irb)) { 1065 case -ETIMEDOUT: 1066 PRINT_WARN("(%s): Request timed out\n", 1067 cdev->dev.bus_id); 1068 case -EIO: 1069 __tape_end_request(device, request, -EIO); 1070 break; 1071 default: 1072 PRINT_ERR("(%s): Unexpected i/o error %li\n", 1073 cdev->dev.bus_id, 1074 PTR_ERR(irb)); 1075 } 1076 return; 1077 } 1078 1079 /* 1080 * If the condition code is not zero and the start function bit is 1081 * still set, this is an deferred error and the last start I/O did 1082 * not succeed. At this point the condition that caused the deferred 1083 * error might still apply. So we just schedule the request to be 1084 * started later. 1085 */ 1086 if (irb->scsw.cc != 0 && (irb->scsw.fctl & SCSW_FCTL_START_FUNC) && 1087 (request->status == TAPE_REQUEST_IN_IO)) { 1088 DBF_EVENT(3,"(%08x): deferred cc=%i, fctl=%i. restarting\n", 1089 device->cdev_id, irb->scsw.cc, irb->scsw.fctl); 1090 request->status = TAPE_REQUEST_QUEUED; 1091 schedule_delayed_work(&device->tape_dnr, HZ); 1092 return; 1093 } 1094 1095 /* May be an unsolicited irq */ 1096 if(request != NULL) 1097 request->rescnt = irb->scsw.count; 1098 1099 if (irb->scsw.dstat != 0x0c) { 1100 /* Set the 'ONLINE' flag depending on sense byte 1 */ 1101 if(*(((__u8 *) irb->ecw) + 1) & SENSE_DRIVE_ONLINE) 1102 device->tape_generic_status |= GMT_ONLINE(~0); 1103 else 1104 device->tape_generic_status &= ~GMT_ONLINE(~0); 1105 1106 /* 1107 * Any request that does not come back with channel end 1108 * and device end is unusual. Log the sense data. 1109 */ 1110 DBF_EVENT(3,"-- Tape Interrupthandler --\n"); 1111 tape_dump_sense_dbf(device, request, irb); 1112 } else { 1113 /* Upon normal completion the device _is_ online */ 1114 device->tape_generic_status |= GMT_ONLINE(~0); 1115 } 1116 if (device->tape_state == TS_NOT_OPER) { 1117 DBF_EVENT(6, "tape:device is not operational\n"); 1118 return; 1119 } 1120 1121 /* 1122 * Request that were canceled still come back with an interrupt. 1123 * To detect these request the state will be set to TAPE_REQUEST_DONE. 1124 */ 1125 if(request != NULL && request->status == TAPE_REQUEST_DONE) { 1126 __tape_end_request(device, request, -EIO); 1127 return; 1128 } 1129 1130 rc = device->discipline->irq(device, request, irb); 1131 /* 1132 * rc < 0 : request finished unsuccessfully. 1133 * rc == TAPE_IO_SUCCESS: request finished successfully. 1134 * rc == TAPE_IO_PENDING: request is still running. Ignore rc. 1135 * rc == TAPE_IO_RETRY: request finished but needs another go. 1136 * rc == TAPE_IO_STOP: request needs to get terminated. 1137 */ 1138 switch (rc) { 1139 case TAPE_IO_SUCCESS: 1140 /* Upon normal completion the device _is_ online */ 1141 device->tape_generic_status |= GMT_ONLINE(~0); 1142 __tape_end_request(device, request, rc); 1143 break; 1144 case TAPE_IO_PENDING: 1145 break; 1146 case TAPE_IO_RETRY: 1147 rc = __tape_start_io(device, request); 1148 if (rc) 1149 __tape_end_request(device, request, rc); 1150 break; 1151 case TAPE_IO_STOP: 1152 rc = __tape_cancel_io(device, request); 1153 if (rc) 1154 __tape_end_request(device, request, rc); 1155 break; 1156 default: 1157 if (rc > 0) { 1158 DBF_EVENT(6, "xunknownrc\n"); 1159 PRINT_ERR("Invalid return code from discipline " 1160 "interrupt function.\n"); 1161 __tape_end_request(device, request, -EIO); 1162 } else { 1163 __tape_end_request(device, request, rc); 1164 } 1165 break; 1166 } 1167 } 1168 1169 /* 1170 * Tape device open function used by tape_char & tape_block frontends. 1171 */ 1172 int 1173 tape_open(struct tape_device *device) 1174 { 1175 int rc; 1176 1177 spin_lock(get_ccwdev_lock(device->cdev)); 1178 if (device->tape_state == TS_NOT_OPER) { 1179 DBF_EVENT(6, "TAPE:nodev\n"); 1180 rc = -ENODEV; 1181 } else if (device->tape_state == TS_IN_USE) { 1182 DBF_EVENT(6, "TAPE:dbusy\n"); 1183 rc = -EBUSY; 1184 } else if (device->tape_state == TS_BLKUSE) { 1185 DBF_EVENT(6, "TAPE:dbusy\n"); 1186 rc = -EBUSY; 1187 } else if (device->discipline != NULL && 1188 !try_module_get(device->discipline->owner)) { 1189 DBF_EVENT(6, "TAPE:nodisc\n"); 1190 rc = -ENODEV; 1191 } else { 1192 tape_state_set(device, TS_IN_USE); 1193 rc = 0; 1194 } 1195 spin_unlock(get_ccwdev_lock(device->cdev)); 1196 return rc; 1197 } 1198 1199 /* 1200 * Tape device release function used by tape_char & tape_block frontends. 1201 */ 1202 int 1203 tape_release(struct tape_device *device) 1204 { 1205 spin_lock(get_ccwdev_lock(device->cdev)); 1206 if (device->tape_state == TS_IN_USE) 1207 tape_state_set(device, TS_UNUSED); 1208 module_put(device->discipline->owner); 1209 spin_unlock(get_ccwdev_lock(device->cdev)); 1210 return 0; 1211 } 1212 1213 /* 1214 * Execute a magnetic tape command a number of times. 1215 */ 1216 int 1217 tape_mtop(struct tape_device *device, int mt_op, int mt_count) 1218 { 1219 tape_mtop_fn fn; 1220 int rc; 1221 1222 DBF_EVENT(6, "TAPE:mtio\n"); 1223 DBF_EVENT(6, "TAPE:ioop: %x\n", mt_op); 1224 DBF_EVENT(6, "TAPE:arg: %x\n", mt_count); 1225 1226 if (mt_op < 0 || mt_op >= TAPE_NR_MTOPS) 1227 return -EINVAL; 1228 fn = device->discipline->mtop_array[mt_op]; 1229 if (fn == NULL) 1230 return -EINVAL; 1231 1232 /* We assume that the backends can handle count up to 500. */ 1233 if (mt_op == MTBSR || mt_op == MTFSR || mt_op == MTFSF || 1234 mt_op == MTBSF || mt_op == MTFSFM || mt_op == MTBSFM) { 1235 rc = 0; 1236 for (; mt_count > 500; mt_count -= 500) 1237 if ((rc = fn(device, 500)) != 0) 1238 break; 1239 if (rc == 0) 1240 rc = fn(device, mt_count); 1241 } else 1242 rc = fn(device, mt_count); 1243 return rc; 1244 1245 } 1246 1247 /* 1248 * Tape init function. 1249 */ 1250 static int 1251 tape_init (void) 1252 { 1253 TAPE_DBF_AREA = debug_register ( "tape", 2, 2, 4*sizeof(long)); 1254 debug_register_view(TAPE_DBF_AREA, &debug_sprintf_view); 1255 #ifdef DBF_LIKE_HELL 1256 debug_set_level(TAPE_DBF_AREA, 6); 1257 #endif 1258 DBF_EVENT(3, "tape init\n"); 1259 tape_proc_init(); 1260 tapechar_init (); 1261 tapeblock_init (); 1262 return 0; 1263 } 1264 1265 /* 1266 * Tape exit function. 1267 */ 1268 static void 1269 tape_exit(void) 1270 { 1271 DBF_EVENT(6, "tape exit\n"); 1272 1273 /* Get rid of the frontends */ 1274 tapechar_exit(); 1275 tapeblock_exit(); 1276 tape_proc_cleanup(); 1277 debug_unregister (TAPE_DBF_AREA); 1278 } 1279 1280 MODULE_AUTHOR("(C) 2001 IBM Deutschland Entwicklung GmbH by Carsten Otte and " 1281 "Michael Holzheu (cotte@de.ibm.com,holzheu@de.ibm.com)"); 1282 MODULE_DESCRIPTION("Linux on zSeries channel attached tape device driver"); 1283 MODULE_LICENSE("GPL"); 1284 1285 module_init(tape_init); 1286 module_exit(tape_exit); 1287 1288 EXPORT_SYMBOL(tape_generic_remove); 1289 EXPORT_SYMBOL(tape_generic_probe); 1290 EXPORT_SYMBOL(tape_generic_online); 1291 EXPORT_SYMBOL(tape_generic_offline); 1292 EXPORT_SYMBOL(tape_put_device); 1293 EXPORT_SYMBOL(tape_get_device_reference); 1294 EXPORT_SYMBOL(tape_state_verbose); 1295 EXPORT_SYMBOL(tape_op_verbose); 1296 EXPORT_SYMBOL(tape_state_set); 1297 EXPORT_SYMBOL(tape_med_state_set); 1298 EXPORT_SYMBOL(tape_alloc_request); 1299 EXPORT_SYMBOL(tape_free_request); 1300 EXPORT_SYMBOL(tape_dump_sense); 1301 EXPORT_SYMBOL(tape_dump_sense_dbf); 1302 EXPORT_SYMBOL(tape_do_io); 1303 EXPORT_SYMBOL(tape_do_io_async); 1304 EXPORT_SYMBOL(tape_do_io_interruptible); 1305 EXPORT_SYMBOL(tape_cancel_io); 1306 EXPORT_SYMBOL(tape_mtop); 1307