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