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