linux/drivers/s390/char/tape_3590.c
<<
>>
Prefs
   1/*
   2 *  drivers/s390/char/tape_3590.c
   3 *    tape device discipline for 3590 tapes.
   4 *
   5 *    Copyright IBM Corp. 2001, 2009
   6 *    Author(s): Stefan Bader <shbader@de.ibm.com>
   7 *               Michael Holzheu <holzheu@de.ibm.com>
   8 *               Martin Schwidefsky <schwidefsky@de.ibm.com>
   9 */
  10
  11#define KMSG_COMPONENT "tape_3590"
  12
  13#include <linux/module.h>
  14#include <linux/init.h>
  15#include <linux/bio.h>
  16#include <asm/ebcdic.h>
  17
  18#define TAPE_DBF_AREA   tape_3590_dbf
  19#define BUFSIZE 512     /* size of buffers for dynamic generated messages */
  20
  21#include "tape.h"
  22#include "tape_std.h"
  23#include "tape_3590.h"
  24
  25/*
  26 * Pointer to debug area.
  27 */
  28debug_info_t *TAPE_DBF_AREA = NULL;
  29EXPORT_SYMBOL(TAPE_DBF_AREA);
  30
  31/*******************************************************************
  32 * Error Recovery fuctions:
  33 * - Read Opposite:              implemented
  34 * - Read Device (buffered) log: BRA
  35 * - Read Library log:           BRA
  36 * - Swap Devices:               BRA
  37 * - Long Busy:                  implemented
  38 * - Special Intercept:          BRA
  39 * - Read Alternate:             implemented
  40 *******************************************************************/
  41
  42static const char *tape_3590_msg[TAPE_3590_MAX_MSG] = {
  43        [0x00] = "",
  44        [0x10] = "Lost Sense",
  45        [0x11] = "Assigned Elsewhere",
  46        [0x12] = "Allegiance Reset",
  47        [0x13] = "Shared Access Violation",
  48        [0x20] = "Command Reject",
  49        [0x21] = "Configuration Error",
  50        [0x22] = "Protection Exception",
  51        [0x23] = "Write Protect",
  52        [0x24] = "Write Length",
  53        [0x25] = "Read-Only Format",
  54        [0x31] = "Beginning of Partition",
  55        [0x33] = "End of Partition",
  56        [0x34] = "End of Data",
  57        [0x35] = "Block not found",
  58        [0x40] = "Device Intervention",
  59        [0x41] = "Loader Intervention",
  60        [0x42] = "Library Intervention",
  61        [0x50] = "Write Error",
  62        [0x51] = "Erase Error",
  63        [0x52] = "Formatting Error",
  64        [0x53] = "Read Error",
  65        [0x54] = "Unsupported Format",
  66        [0x55] = "No Formatting",
  67        [0x56] = "Positioning lost",
  68        [0x57] = "Read Length",
  69        [0x60] = "Unsupported Medium",
  70        [0x61] = "Medium Length Error",
  71        [0x62] = "Medium removed",
  72        [0x64] = "Load Check",
  73        [0x65] = "Unload Check",
  74        [0x70] = "Equipment Check",
  75        [0x71] = "Bus out Check",
  76        [0x72] = "Protocol Error",
  77        [0x73] = "Interface Error",
  78        [0x74] = "Overrun",
  79        [0x75] = "Halt Signal",
  80        [0x90] = "Device fenced",
  81        [0x91] = "Device Path fenced",
  82        [0xa0] = "Volume misplaced",
  83        [0xa1] = "Volume inaccessible",
  84        [0xa2] = "Volume in input",
  85        [0xa3] = "Volume ejected",
  86        [0xa4] = "All categories reserved",
  87        [0xa5] = "Duplicate Volume",
  88        [0xa6] = "Library Manager Offline",
  89        [0xa7] = "Library Output Station full",
  90        [0xa8] = "Vision System non-operational",
  91        [0xa9] = "Library Manager Equipment Check",
  92        [0xaa] = "Library Equipment Check",
  93        [0xab] = "All Library Cells full",
  94        [0xac] = "No Cleaner Volumes in Library",
  95        [0xad] = "I/O Station door open",
  96        [0xae] = "Subsystem environmental alert",
  97};
  98
  99static int crypt_supported(struct tape_device *device)
 100{
 101        return TAPE390_CRYPT_SUPPORTED(TAPE_3590_CRYPT_INFO(device));
 102}
 103
 104static int crypt_enabled(struct tape_device *device)
 105{
 106        return TAPE390_CRYPT_ON(TAPE_3590_CRYPT_INFO(device));
 107}
 108
 109static void ext_to_int_kekl(struct tape390_kekl *in,
 110                            struct tape3592_kekl *out)
 111{
 112        int i;
 113
 114        memset(out, 0, sizeof(*out));
 115        if (in->type == TAPE390_KEKL_TYPE_HASH)
 116                out->flags |= 0x40;
 117        if (in->type_on_tape == TAPE390_KEKL_TYPE_HASH)
 118                out->flags |= 0x80;
 119        strncpy(out->label, in->label, 64);
 120        for (i = strlen(in->label); i < sizeof(out->label); i++)
 121                out->label[i] = ' ';
 122        ASCEBC(out->label, sizeof(out->label));
 123}
 124
 125static void int_to_ext_kekl(struct tape3592_kekl *in,
 126                            struct tape390_kekl *out)
 127{
 128        memset(out, 0, sizeof(*out));
 129        if(in->flags & 0x40)
 130                out->type = TAPE390_KEKL_TYPE_HASH;
 131        else
 132                out->type = TAPE390_KEKL_TYPE_LABEL;
 133        if(in->flags & 0x80)
 134                out->type_on_tape = TAPE390_KEKL_TYPE_HASH;
 135        else
 136                out->type_on_tape = TAPE390_KEKL_TYPE_LABEL;
 137        memcpy(out->label, in->label, sizeof(in->label));
 138        EBCASC(out->label, sizeof(in->label));
 139        strstrip(out->label);
 140}
 141
 142static void int_to_ext_kekl_pair(struct tape3592_kekl_pair *in,
 143                                 struct tape390_kekl_pair *out)
 144{
 145        if (in->count == 0) {
 146                out->kekl[0].type = TAPE390_KEKL_TYPE_NONE;
 147                out->kekl[0].type_on_tape = TAPE390_KEKL_TYPE_NONE;
 148                out->kekl[1].type = TAPE390_KEKL_TYPE_NONE;
 149                out->kekl[1].type_on_tape = TAPE390_KEKL_TYPE_NONE;
 150        } else if (in->count == 1) {
 151                int_to_ext_kekl(&in->kekl[0], &out->kekl[0]);
 152                out->kekl[1].type = TAPE390_KEKL_TYPE_NONE;
 153                out->kekl[1].type_on_tape = TAPE390_KEKL_TYPE_NONE;
 154        } else if (in->count == 2) {
 155                int_to_ext_kekl(&in->kekl[0], &out->kekl[0]);
 156                int_to_ext_kekl(&in->kekl[1], &out->kekl[1]);
 157        } else {
 158                printk("Invalid KEKL number: %d\n", in->count);
 159                BUG();
 160        }
 161}
 162
 163static int check_ext_kekl(struct tape390_kekl *kekl)
 164{
 165        if (kekl->type == TAPE390_KEKL_TYPE_NONE)
 166                goto invalid;
 167        if (kekl->type > TAPE390_KEKL_TYPE_HASH)
 168                goto invalid;
 169        if (kekl->type_on_tape == TAPE390_KEKL_TYPE_NONE)
 170                goto invalid;
 171        if (kekl->type_on_tape > TAPE390_KEKL_TYPE_HASH)
 172                goto invalid;
 173        if ((kekl->type == TAPE390_KEKL_TYPE_HASH) &&
 174            (kekl->type_on_tape == TAPE390_KEKL_TYPE_LABEL))
 175                goto invalid;
 176
 177        return 0;
 178invalid:
 179        return -EINVAL;
 180}
 181
 182static int check_ext_kekl_pair(struct tape390_kekl_pair *kekls)
 183{
 184        if (check_ext_kekl(&kekls->kekl[0]))
 185                goto invalid;
 186        if (check_ext_kekl(&kekls->kekl[1]))
 187                goto invalid;
 188
 189        return 0;
 190invalid:
 191        return -EINVAL;
 192}
 193
 194/*
 195 * Query KEKLs
 196 */
 197static int tape_3592_kekl_query(struct tape_device *device,
 198                                struct tape390_kekl_pair *ext_kekls)
 199{
 200        struct tape_request *request;
 201        struct tape3592_kekl_query_order *order;
 202        struct tape3592_kekl_query_data *int_kekls;
 203        int rc;
 204
 205        DBF_EVENT(6, "tape3592_kekl_query\n");
 206        int_kekls = kmalloc(sizeof(*int_kekls), GFP_KERNEL|GFP_DMA);
 207        if (!int_kekls)
 208                return -ENOMEM;
 209        request = tape_alloc_request(2, sizeof(*order));
 210        if (IS_ERR(request)) {
 211                rc = PTR_ERR(request);
 212                goto fail_malloc;
 213        }
 214        order = request->cpdata;
 215        memset(order,0,sizeof(*order));
 216        order->code = 0xe2;
 217        order->max_count = 2;
 218        request->op = TO_KEKL_QUERY;
 219        tape_ccw_cc(request->cpaddr, PERF_SUBSYS_FUNC, sizeof(*order), order);
 220        tape_ccw_end(request->cpaddr + 1, READ_SS_DATA, sizeof(*int_kekls),
 221                     int_kekls);
 222        rc = tape_do_io(device, request);
 223        if (rc)
 224                goto fail_request;
 225        int_to_ext_kekl_pair(&int_kekls->kekls, ext_kekls);
 226
 227        rc = 0;
 228fail_request:
 229        tape_free_request(request);
 230fail_malloc:
 231        kfree(int_kekls);
 232        return rc;
 233}
 234
 235/*
 236 * IOCTL: Query KEKLs
 237 */
 238static int tape_3592_ioctl_kekl_query(struct tape_device *device,
 239                                      unsigned long arg)
 240{
 241        int rc;
 242        struct tape390_kekl_pair *ext_kekls;
 243
 244        DBF_EVENT(6, "tape_3592_ioctl_kekl_query\n");
 245        if (!crypt_supported(device))
 246                return -ENOSYS;
 247        if (!crypt_enabled(device))
 248                return -EUNATCH;
 249        ext_kekls = kmalloc(sizeof(*ext_kekls), GFP_KERNEL);
 250        if (!ext_kekls)
 251                return -ENOMEM;
 252        rc = tape_3592_kekl_query(device, ext_kekls);
 253        if (rc != 0)
 254                goto fail;
 255        if (copy_to_user((char __user *) arg, ext_kekls, sizeof(*ext_kekls))) {
 256                rc = -EFAULT;
 257                goto fail;
 258        }
 259        rc = 0;
 260fail:
 261        kfree(ext_kekls);
 262        return rc;
 263}
 264
 265static int tape_3590_mttell(struct tape_device *device, int mt_count);
 266
 267/*
 268 * Set KEKLs
 269 */
 270static int tape_3592_kekl_set(struct tape_device *device,
 271                              struct tape390_kekl_pair *ext_kekls)
 272{
 273        struct tape_request *request;
 274        struct tape3592_kekl_set_order *order;
 275
 276        DBF_EVENT(6, "tape3592_kekl_set\n");
 277        if (check_ext_kekl_pair(ext_kekls)) {
 278                DBF_EVENT(6, "invalid kekls\n");
 279                return -EINVAL;
 280        }
 281        if (tape_3590_mttell(device, 0) != 0)
 282                return -EBADSLT;
 283        request = tape_alloc_request(1, sizeof(*order));
 284        if (IS_ERR(request))
 285                return PTR_ERR(request);
 286        order = request->cpdata;
 287        memset(order, 0, sizeof(*order));
 288        order->code = 0xe3;
 289        order->kekls.count = 2;
 290        ext_to_int_kekl(&ext_kekls->kekl[0], &order->kekls.kekl[0]);
 291        ext_to_int_kekl(&ext_kekls->kekl[1], &order->kekls.kekl[1]);
 292        request->op = TO_KEKL_SET;
 293        tape_ccw_end(request->cpaddr, PERF_SUBSYS_FUNC, sizeof(*order), order);
 294
 295        return tape_do_io_free(device, request);
 296}
 297
 298/*
 299 * IOCTL: Set KEKLs
 300 */
 301static int tape_3592_ioctl_kekl_set(struct tape_device *device,
 302                                    unsigned long arg)
 303{
 304        int rc;
 305        struct tape390_kekl_pair *ext_kekls;
 306
 307        DBF_EVENT(6, "tape_3592_ioctl_kekl_set\n");
 308        if (!crypt_supported(device))
 309                return -ENOSYS;
 310        if (!crypt_enabled(device))
 311                return -EUNATCH;
 312        ext_kekls = kmalloc(sizeof(*ext_kekls), GFP_KERNEL);
 313        if (!ext_kekls)
 314                return -ENOMEM;
 315        if (copy_from_user(ext_kekls, (char __user *)arg, sizeof(*ext_kekls))) {
 316                rc = -EFAULT;
 317                goto out;
 318        }
 319        rc = tape_3592_kekl_set(device, ext_kekls);
 320out:
 321        kfree(ext_kekls);
 322        return rc;
 323}
 324
 325/*
 326 * Enable encryption
 327 */
 328static int tape_3592_enable_crypt(struct tape_device *device)
 329{
 330        struct tape_request *request;
 331        char *data;
 332
 333        DBF_EVENT(6, "tape_3592_enable_crypt\n");
 334        if (!crypt_supported(device))
 335                return -ENOSYS;
 336        request = tape_alloc_request(2, 72);
 337        if (IS_ERR(request))
 338                return PTR_ERR(request);
 339        data = request->cpdata;
 340        memset(data,0,72);
 341
 342        data[0]       = 0x05;
 343        data[36 + 0]  = 0x03;
 344        data[36 + 1]  = 0x03;
 345        data[36 + 4]  = 0x40;
 346        data[36 + 6]  = 0x01;
 347        data[36 + 14] = 0x2f;
 348        data[36 + 18] = 0xc3;
 349        data[36 + 35] = 0x72;
 350        request->op = TO_CRYPT_ON;
 351        tape_ccw_cc(request->cpaddr, MODE_SET_CB, 36, data);
 352        tape_ccw_end(request->cpaddr + 1, MODE_SET_CB, 36, data + 36);
 353        return tape_do_io_free(device, request);
 354}
 355
 356/*
 357 * Disable encryption
 358 */
 359static int tape_3592_disable_crypt(struct tape_device *device)
 360{
 361        struct tape_request *request;
 362        char *data;
 363
 364        DBF_EVENT(6, "tape_3592_disable_crypt\n");
 365        if (!crypt_supported(device))
 366                return -ENOSYS;
 367        request = tape_alloc_request(2, 72);
 368        if (IS_ERR(request))
 369                return PTR_ERR(request);
 370        data = request->cpdata;
 371        memset(data,0,72);
 372
 373        data[0]       = 0x05;
 374        data[36 + 0]  = 0x03;
 375        data[36 + 1]  = 0x03;
 376        data[36 + 35] = 0x32;
 377
 378        request->op = TO_CRYPT_OFF;
 379        tape_ccw_cc(request->cpaddr, MODE_SET_CB, 36, data);
 380        tape_ccw_end(request->cpaddr + 1, MODE_SET_CB, 36, data + 36);
 381
 382        return tape_do_io_free(device, request);
 383}
 384
 385/*
 386 * IOCTL: Set encryption status
 387 */
 388static int tape_3592_ioctl_crypt_set(struct tape_device *device,
 389                                     unsigned long arg)
 390{
 391        struct tape390_crypt_info info;
 392
 393        DBF_EVENT(6, "tape_3592_ioctl_crypt_set\n");
 394        if (!crypt_supported(device))
 395                return -ENOSYS;
 396        if (copy_from_user(&info, (char __user *)arg, sizeof(info)))
 397                return -EFAULT;
 398        if (info.status & ~TAPE390_CRYPT_ON_MASK)
 399                return -EINVAL;
 400        if (info.status & TAPE390_CRYPT_ON_MASK)
 401                return tape_3592_enable_crypt(device);
 402        else
 403                return tape_3592_disable_crypt(device);
 404}
 405
 406static int tape_3590_sense_medium(struct tape_device *device);
 407
 408/*
 409 * IOCTL: Query enryption status
 410 */
 411static int tape_3592_ioctl_crypt_query(struct tape_device *device,
 412                                       unsigned long arg)
 413{
 414        DBF_EVENT(6, "tape_3592_ioctl_crypt_query\n");
 415        if (!crypt_supported(device))
 416                return -ENOSYS;
 417        tape_3590_sense_medium(device);
 418        if (copy_to_user((char __user *) arg, &TAPE_3590_CRYPT_INFO(device),
 419                sizeof(TAPE_3590_CRYPT_INFO(device))))
 420                return -EFAULT;
 421        else
 422                return 0;
 423}
 424
 425/*
 426 * 3590 IOCTL Overload
 427 */
 428static int
 429tape_3590_ioctl(struct tape_device *device, unsigned int cmd, unsigned long arg)
 430{
 431        switch (cmd) {
 432        case TAPE390_DISPLAY: {
 433                struct display_struct disp;
 434
 435                if (copy_from_user(&disp, (char __user *) arg, sizeof(disp)))
 436                        return -EFAULT;
 437
 438                return tape_std_display(device, &disp);
 439        }
 440        case TAPE390_KEKL_SET:
 441                return tape_3592_ioctl_kekl_set(device, arg);
 442        case TAPE390_KEKL_QUERY:
 443                return tape_3592_ioctl_kekl_query(device, arg);
 444        case TAPE390_CRYPT_SET:
 445                return tape_3592_ioctl_crypt_set(device, arg);
 446        case TAPE390_CRYPT_QUERY:
 447                return tape_3592_ioctl_crypt_query(device, arg);
 448        default:
 449                return -EINVAL; /* no additional ioctls */
 450        }
 451}
 452
 453/*
 454 * SENSE Medium: Get Sense data about medium state
 455 */
 456static int
 457tape_3590_sense_medium(struct tape_device *device)
 458{
 459        struct tape_request *request;
 460
 461        request = tape_alloc_request(1, 128);
 462        if (IS_ERR(request))
 463                return PTR_ERR(request);
 464        request->op = TO_MSEN;
 465        tape_ccw_end(request->cpaddr, MEDIUM_SENSE, 128, request->cpdata);
 466        return tape_do_io_free(device, request);
 467}
 468
 469/*
 470 * MTTELL: Tell block. Return the number of block relative to current file.
 471 */
 472static int
 473tape_3590_mttell(struct tape_device *device, int mt_count)
 474{
 475        __u64 block_id;
 476        int rc;
 477
 478        rc = tape_std_read_block_id(device, &block_id);
 479        if (rc)
 480                return rc;
 481        return block_id >> 32;
 482}
 483
 484/*
 485 * MTSEEK: seek to the specified block.
 486 */
 487static int
 488tape_3590_mtseek(struct tape_device *device, int count)
 489{
 490        struct tape_request *request;
 491
 492        DBF_EVENT(6, "xsee id: %x\n", count);
 493        request = tape_alloc_request(3, 4);
 494        if (IS_ERR(request))
 495                return PTR_ERR(request);
 496        request->op = TO_LBL;
 497        tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
 498        *(__u32 *) request->cpdata = count;
 499        tape_ccw_cc(request->cpaddr + 1, LOCATE, 4, request->cpdata);
 500        tape_ccw_end(request->cpaddr + 2, NOP, 0, NULL);
 501        return tape_do_io_free(device, request);
 502}
 503
 504/*
 505 * Read Opposite Error Recovery Function:
 506 * Used, when Read Forward does not work
 507 */
 508static void
 509tape_3590_read_opposite(struct tape_device *device,
 510                        struct tape_request *request)
 511{
 512        struct tape_3590_disc_data *data;
 513
 514        /*
 515         * We have allocated 4 ccws in tape_std_read, so we can now
 516         * transform the request to a read backward, followed by a
 517         * forward space block.
 518         */
 519        request->op = TO_RBA;
 520        tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
 521        data = device->discdata;
 522        tape_ccw_cc_idal(request->cpaddr + 1, data->read_back_op,
 523                         device->char_data.idal_buf);
 524        tape_ccw_cc(request->cpaddr + 2, FORSPACEBLOCK, 0, NULL);
 525        tape_ccw_end(request->cpaddr + 3, NOP, 0, NULL);
 526        DBF_EVENT(6, "xrop ccwg\n");
 527}
 528
 529/*
 530 * Read Attention Msg
 531 * This should be done after an interrupt with attention bit (0x80)
 532 * in device state.
 533 *
 534 * After a "read attention message" request there are two possible
 535 * results:
 536 *
 537 * 1. A unit check is presented, when attention sense is present (e.g. when
 538 * a medium has been unloaded). The attention sense comes then
 539 * together with the unit check. The recovery action is either "retry"
 540 * (in case there is an attention message pending) or "permanent error".
 541 *
 542 * 2. The attention msg is written to the "read subsystem data" buffer.
 543 * In this case we probably should print it to the console.
 544 */
 545static int
 546tape_3590_read_attmsg(struct tape_device *device)
 547{
 548        struct tape_request *request;
 549        char *buf;
 550
 551        request = tape_alloc_request(3, 4096);
 552        if (IS_ERR(request))
 553                return PTR_ERR(request);
 554        request->op = TO_READ_ATTMSG;
 555        buf = request->cpdata;
 556        buf[0] = PREP_RD_SS_DATA;
 557        buf[6] = RD_ATTMSG;     /* read att msg */
 558        tape_ccw_cc(request->cpaddr, PERFORM_SS_FUNC, 12, buf);
 559        tape_ccw_cc(request->cpaddr + 1, READ_SS_DATA, 4096 - 12, buf + 12);
 560        tape_ccw_end(request->cpaddr + 2, NOP, 0, NULL);
 561        return tape_do_io_free(device, request);
 562}
 563
 564/*
 565 * These functions are used to schedule follow-up actions from within an
 566 * interrupt context (like unsolicited interrupts).
 567 */
 568struct work_handler_data {
 569        struct tape_device *device;
 570        enum tape_op        op;
 571        struct work_struct  work;
 572};
 573
 574static void
 575tape_3590_work_handler(struct work_struct *work)
 576{
 577        struct work_handler_data *p =
 578                container_of(work, struct work_handler_data, work);
 579
 580        switch (p->op) {
 581        case TO_MSEN:
 582                tape_3590_sense_medium(p->device);
 583                break;
 584        case TO_READ_ATTMSG:
 585                tape_3590_read_attmsg(p->device);
 586                break;
 587        case TO_CRYPT_ON:
 588                tape_3592_enable_crypt(p->device);
 589                break;
 590        case TO_CRYPT_OFF:
 591                tape_3592_disable_crypt(p->device);
 592                break;
 593        default:
 594                DBF_EVENT(3, "T3590: work handler undefined for "
 595                          "operation 0x%02x\n", p->op);
 596        }
 597        tape_put_device(p->device);
 598        kfree(p);
 599}
 600
 601static int
 602tape_3590_schedule_work(struct tape_device *device, enum tape_op op)
 603{
 604        struct work_handler_data *p;
 605
 606        if ((p = kzalloc(sizeof(*p), GFP_ATOMIC)) == NULL)
 607                return -ENOMEM;
 608
 609        INIT_WORK(&p->work, tape_3590_work_handler);
 610
 611        p->device = tape_get_device_reference(device);
 612        p->op = op;
 613
 614        schedule_work(&p->work);
 615        return 0;
 616}
 617
 618#ifdef CONFIG_S390_TAPE_BLOCK
 619/*
 620 * Tape Block READ
 621 */
 622static struct tape_request *
 623tape_3590_bread(struct tape_device *device, struct request *req)
 624{
 625        struct tape_request *request;
 626        struct ccw1 *ccw;
 627        int count = 0, start_block;
 628        unsigned off;
 629        char *dst;
 630        struct bio_vec *bv;
 631        struct req_iterator iter;
 632
 633        DBF_EVENT(6, "xBREDid:");
 634        start_block = blk_rq_pos(req) >> TAPEBLOCK_HSEC_S2B;
 635        DBF_EVENT(6, "start_block = %i\n", start_block);
 636
 637        rq_for_each_segment(bv, req, iter)
 638                count += bv->bv_len >> (TAPEBLOCK_HSEC_S2B + 9);
 639
 640        request = tape_alloc_request(2 + count + 1, 4);
 641        if (IS_ERR(request))
 642                return request;
 643        request->op = TO_BLOCK;
 644        *(__u32 *) request->cpdata = start_block;
 645        ccw = request->cpaddr;
 646        ccw = tape_ccw_cc(ccw, MODE_SET_DB, 1, device->modeset_byte);
 647
 648        /*
 649         * We always setup a nop after the mode set ccw. This slot is
 650         * used in tape_std_check_locate to insert a locate ccw if the
 651         * current tape position doesn't match the start block to be read.
 652         */
 653        ccw = tape_ccw_cc(ccw, NOP, 0, NULL);
 654
 655        rq_for_each_segment(bv, req, iter) {
 656                dst = page_address(bv->bv_page) + bv->bv_offset;
 657                for (off = 0; off < bv->bv_len; off += TAPEBLOCK_HSEC_SIZE) {
 658                        ccw->flags = CCW_FLAG_CC;
 659                        ccw->cmd_code = READ_FORWARD;
 660                        ccw->count = TAPEBLOCK_HSEC_SIZE;
 661                        set_normalized_cda(ccw, (void *) __pa(dst));
 662                        ccw++;
 663                        dst += TAPEBLOCK_HSEC_SIZE;
 664                }
 665                BUG_ON(off > bv->bv_len);
 666        }
 667        ccw = tape_ccw_end(ccw, NOP, 0, NULL);
 668        DBF_EVENT(6, "xBREDccwg\n");
 669        return request;
 670}
 671
 672static void
 673tape_3590_free_bread(struct tape_request *request)
 674{
 675        struct ccw1 *ccw;
 676
 677        /* Last ccw is a nop and doesn't need clear_normalized_cda */
 678        for (ccw = request->cpaddr; ccw->flags & CCW_FLAG_CC; ccw++)
 679                if (ccw->cmd_code == READ_FORWARD)
 680                        clear_normalized_cda(ccw);
 681        tape_free_request(request);
 682}
 683
 684/*
 685 * check_locate is called just before the tape request is passed to
 686 * the common io layer for execution. It has to check the current
 687 * tape position and insert a locate ccw if it doesn't match the
 688 * start block for the request.
 689 */
 690static void
 691tape_3590_check_locate(struct tape_device *device, struct tape_request *request)
 692{
 693        __u32 *start_block;
 694
 695        start_block = (__u32 *) request->cpdata;
 696        if (*start_block != device->blk_data.block_position) {
 697                /* Add the start offset of the file to get the real block. */
 698                *start_block += device->bof;
 699                tape_ccw_cc(request->cpaddr + 1, LOCATE, 4, request->cpdata);
 700        }
 701}
 702#endif
 703
 704static void tape_3590_med_state_set(struct tape_device *device,
 705                                    struct tape_3590_med_sense *sense)
 706{
 707        struct tape390_crypt_info *c_info;
 708
 709        c_info = &TAPE_3590_CRYPT_INFO(device);
 710
 711        DBF_EVENT(6, "medium state: %x:%x\n", sense->macst, sense->masst);
 712        switch (sense->macst) {
 713        case 0x04:
 714        case 0x05:
 715        case 0x06:
 716                tape_med_state_set(device, MS_UNLOADED);
 717                TAPE_3590_CRYPT_INFO(device).medium_status = 0;
 718                return;
 719        case 0x08:
 720        case 0x09:
 721                tape_med_state_set(device, MS_LOADED);
 722                break;
 723        default:
 724                tape_med_state_set(device, MS_UNKNOWN);
 725                return;
 726        }
 727        c_info->medium_status |= TAPE390_MEDIUM_LOADED_MASK;
 728        if (sense->flags & MSENSE_CRYPT_MASK) {
 729                DBF_EVENT(6, "Medium is encrypted (%04x)\n", sense->flags);
 730                c_info->medium_status |= TAPE390_MEDIUM_ENCRYPTED_MASK;
 731        } else  {
 732                DBF_EVENT(6, "Medium is not encrypted %04x\n", sense->flags);
 733                c_info->medium_status &= ~TAPE390_MEDIUM_ENCRYPTED_MASK;
 734        }
 735}
 736
 737/*
 738 * The done handler is called at device/channel end and wakes up the sleeping
 739 * process
 740 */
 741static int
 742tape_3590_done(struct tape_device *device, struct tape_request *request)
 743{
 744        struct tape_3590_disc_data *disc_data;
 745
 746        DBF_EVENT(6, "%s done\n", tape_op_verbose[request->op]);
 747        disc_data = device->discdata;
 748
 749        switch (request->op) {
 750        case TO_BSB:
 751        case TO_BSF:
 752        case TO_DSE:
 753        case TO_FSB:
 754        case TO_FSF:
 755        case TO_LBL:
 756        case TO_RFO:
 757        case TO_RBA:
 758        case TO_REW:
 759        case TO_WRI:
 760        case TO_WTM:
 761        case TO_BLOCK:
 762        case TO_LOAD:
 763                tape_med_state_set(device, MS_LOADED);
 764                break;
 765        case TO_RUN:
 766                tape_med_state_set(device, MS_UNLOADED);
 767                tape_3590_schedule_work(device, TO_CRYPT_OFF);
 768                break;
 769        case TO_MSEN:
 770                tape_3590_med_state_set(device, request->cpdata);
 771                break;
 772        case TO_CRYPT_ON:
 773                TAPE_3590_CRYPT_INFO(device).status
 774                        |= TAPE390_CRYPT_ON_MASK;
 775                *(device->modeset_byte) |= 0x03;
 776                break;
 777        case TO_CRYPT_OFF:
 778                TAPE_3590_CRYPT_INFO(device).status
 779                        &= ~TAPE390_CRYPT_ON_MASK;
 780                *(device->modeset_byte) &= ~0x03;
 781                break;
 782        case TO_RBI:    /* RBI seems to succeed even without medium loaded. */
 783        case TO_NOP:    /* Same to NOP. */
 784        case TO_READ_CONFIG:
 785        case TO_READ_ATTMSG:
 786        case TO_DIS:
 787        case TO_ASSIGN:
 788        case TO_UNASSIGN:
 789        case TO_SIZE:
 790        case TO_KEKL_SET:
 791        case TO_KEKL_QUERY:
 792        case TO_RDC:
 793                break;
 794        }
 795        return TAPE_IO_SUCCESS;
 796}
 797
 798/*
 799 * This fuction is called, when error recovery was successfull
 800 */
 801static inline int
 802tape_3590_erp_succeded(struct tape_device *device, struct tape_request *request)
 803{
 804        DBF_EVENT(3, "Error Recovery successful for %s\n",
 805                  tape_op_verbose[request->op]);
 806        return tape_3590_done(device, request);
 807}
 808
 809/*
 810 * This fuction is called, when error recovery was not successfull
 811 */
 812static inline int
 813tape_3590_erp_failed(struct tape_device *device, struct tape_request *request,
 814                     struct irb *irb, int rc)
 815{
 816        DBF_EVENT(3, "Error Recovery failed for %s\n",
 817                  tape_op_verbose[request->op]);
 818        tape_dump_sense_dbf(device, request, irb);
 819        return rc;
 820}
 821
 822/*
 823 * Error Recovery do retry
 824 */
 825static inline int
 826tape_3590_erp_retry(struct tape_device *device, struct tape_request *request,
 827                    struct irb *irb)
 828{
 829        DBF_EVENT(2, "Retry: %s\n", tape_op_verbose[request->op]);
 830        tape_dump_sense_dbf(device, request, irb);
 831        return TAPE_IO_RETRY;
 832}
 833
 834/*
 835 * Handle unsolicited interrupts
 836 */
 837static int
 838tape_3590_unsolicited_irq(struct tape_device *device, struct irb *irb)
 839{
 840        if (irb->scsw.cmd.dstat == DEV_STAT_CHN_END)
 841                /* Probably result of halt ssch */
 842                return TAPE_IO_PENDING;
 843        else if (irb->scsw.cmd.dstat == 0x85)
 844                /* Device Ready */
 845                DBF_EVENT(3, "unsol.irq! tape ready: %08x\n", device->cdev_id);
 846        else if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) {
 847                tape_3590_schedule_work(device, TO_READ_ATTMSG);
 848        } else {
 849                DBF_EVENT(3, "unsol.irq! dev end: %08x\n", device->cdev_id);
 850                tape_dump_sense_dbf(device, NULL, irb);
 851        }
 852        /* check medium state */
 853        tape_3590_schedule_work(device, TO_MSEN);
 854        return TAPE_IO_SUCCESS;
 855}
 856
 857/*
 858 * Basic Recovery routine
 859 */
 860static int
 861tape_3590_erp_basic(struct tape_device *device, struct tape_request *request,
 862                    struct irb *irb, int rc)
 863{
 864        struct tape_3590_sense *sense;
 865
 866        sense = (struct tape_3590_sense *) irb->ecw;
 867
 868        switch (sense->bra) {
 869        case SENSE_BRA_PER:
 870                return tape_3590_erp_failed(device, request, irb, rc);
 871        case SENSE_BRA_CONT:
 872                return tape_3590_erp_succeded(device, request);
 873        case SENSE_BRA_RE:
 874                return tape_3590_erp_retry(device, request, irb);
 875        case SENSE_BRA_DRE:
 876                return tape_3590_erp_failed(device, request, irb, rc);
 877        default:
 878                BUG();
 879                return TAPE_IO_STOP;
 880        }
 881}
 882
 883/*
 884 *  RDL: Read Device (buffered) log
 885 */
 886static int
 887tape_3590_erp_read_buf_log(struct tape_device *device,
 888                           struct tape_request *request, struct irb *irb)
 889{
 890        /*
 891         * We just do the basic error recovery at the moment (retry).
 892         * Perhaps in the future, we read the log and dump it somewhere...
 893         */
 894        return tape_3590_erp_basic(device, request, irb, -EIO);
 895}
 896
 897/*
 898 *  SWAP: Swap Devices
 899 */
 900static int
 901tape_3590_erp_swap(struct tape_device *device, struct tape_request *request,
 902                   struct irb *irb)
 903{
 904        /*
 905         * This error recovery should swap the tapes
 906         * if the original has a problem. The operation
 907         * should proceed with the new tape... this
 908         * should probably be done in user space!
 909         */
 910        dev_warn (&device->cdev->dev, "The tape medium must be loaded into a "
 911                "different tape unit\n");
 912        return tape_3590_erp_basic(device, request, irb, -EIO);
 913}
 914
 915/*
 916 *  LBY: Long Busy
 917 */
 918static int
 919tape_3590_erp_long_busy(struct tape_device *device,
 920                        struct tape_request *request, struct irb *irb)
 921{
 922        DBF_EVENT(6, "Device is busy\n");
 923        return TAPE_IO_LONG_BUSY;
 924}
 925
 926/*
 927 *  SPI: Special Intercept
 928 */
 929static int
 930tape_3590_erp_special_interrupt(struct tape_device *device,
 931                                struct tape_request *request, struct irb *irb)
 932{
 933        return tape_3590_erp_basic(device, request, irb, -EIO);
 934}
 935
 936/*
 937 *  RDA: Read Alternate
 938 */
 939static int
 940tape_3590_erp_read_alternate(struct tape_device *device,
 941                             struct tape_request *request, struct irb *irb)
 942{
 943        struct tape_3590_disc_data *data;
 944
 945        /*
 946         * The issued Read Backward or Read Previous command is not
 947         * supported by the device
 948         * The recovery action should be to issue another command:
 949         * Read Revious: if Read Backward is not supported
 950         * Read Backward: if Read Previous is not supported
 951         */
 952        data = device->discdata;
 953        if (data->read_back_op == READ_PREVIOUS) {
 954                DBF_EVENT(2, "(%08x): No support for READ_PREVIOUS command\n",
 955                          device->cdev_id);
 956                data->read_back_op = READ_BACKWARD;
 957        } else {
 958                DBF_EVENT(2, "(%08x): No support for READ_BACKWARD command\n",
 959                          device->cdev_id);
 960                data->read_back_op = READ_PREVIOUS;
 961        }
 962        tape_3590_read_opposite(device, request);
 963        return tape_3590_erp_retry(device, request, irb);
 964}
 965
 966/*
 967 * Error Recovery read opposite
 968 */
 969static int
 970tape_3590_erp_read_opposite(struct tape_device *device,
 971                            struct tape_request *request, struct irb *irb)
 972{
 973        switch (request->op) {
 974        case TO_RFO:
 975                /*
 976                 * We did read forward, but the data could not be read.
 977                 * We will read backward and then skip forward again.
 978                 */
 979                tape_3590_read_opposite(device, request);
 980                return tape_3590_erp_retry(device, request, irb);
 981        case TO_RBA:
 982                /* We tried to read forward and backward, but hat no success */
 983                return tape_3590_erp_failed(device, request, irb, -EIO);
 984                break;
 985        default:
 986                return tape_3590_erp_failed(device, request, irb, -EIO);
 987        }
 988}
 989
 990/*
 991 * Print an MIM (Media Information  Message) (message code f0)
 992 */
 993static void
 994tape_3590_print_mim_msg_f0(struct tape_device *device, struct irb *irb)
 995{
 996        struct tape_3590_sense *sense;
 997        char *exception, *service;
 998
 999        exception = kmalloc(BUFSIZE, GFP_ATOMIC);
1000        service = kmalloc(BUFSIZE, GFP_ATOMIC);
1001
1002        if (!exception || !service)
1003                goto out_nomem;
1004
1005        sense = (struct tape_3590_sense *) irb->ecw;
1006        /* Exception Message */
1007        switch (sense->fmt.f70.emc) {
1008        case 0x02:
1009                snprintf(exception, BUFSIZE, "Data degraded");
1010                break;
1011        case 0x03:
1012                snprintf(exception, BUFSIZE, "Data degraded in partion %i",
1013                        sense->fmt.f70.mp);
1014                break;
1015        case 0x04:
1016                snprintf(exception, BUFSIZE, "Medium degraded");
1017                break;
1018        case 0x05:
1019                snprintf(exception, BUFSIZE, "Medium degraded in partition %i",
1020                        sense->fmt.f70.mp);
1021                break;
1022        case 0x06:
1023                snprintf(exception, BUFSIZE, "Block 0 Error");
1024                break;
1025        case 0x07:
1026                snprintf(exception, BUFSIZE, "Medium Exception 0x%02x",
1027                        sense->fmt.f70.md);
1028                break;
1029        default:
1030                snprintf(exception, BUFSIZE, "0x%02x",
1031                        sense->fmt.f70.emc);
1032                break;
1033        }
1034        /* Service Message */
1035        switch (sense->fmt.f70.smc) {
1036        case 0x02:
1037                snprintf(service, BUFSIZE, "Reference Media maintenance "
1038                        "procedure %i", sense->fmt.f70.md);
1039                break;
1040        default:
1041                snprintf(service, BUFSIZE, "0x%02x",
1042                        sense->fmt.f70.smc);
1043                break;
1044        }
1045
1046        dev_warn (&device->cdev->dev, "Tape media information: exception %s, "
1047                "service %s\n", exception, service);
1048
1049out_nomem:
1050        kfree(exception);
1051        kfree(service);
1052}
1053
1054/*
1055 * Print an I/O Subsystem Service Information Message (message code f1)
1056 */
1057static void
1058tape_3590_print_io_sim_msg_f1(struct tape_device *device, struct irb *irb)
1059{
1060        struct tape_3590_sense *sense;
1061        char *exception, *service;
1062
1063        exception = kmalloc(BUFSIZE, GFP_ATOMIC);
1064        service = kmalloc(BUFSIZE, GFP_ATOMIC);
1065
1066        if (!exception || !service)
1067                goto out_nomem;
1068
1069        sense = (struct tape_3590_sense *) irb->ecw;
1070        /* Exception Message */
1071        switch (sense->fmt.f71.emc) {
1072        case 0x01:
1073                snprintf(exception, BUFSIZE, "Effect of failure is unknown");
1074                break;
1075        case 0x02:
1076                snprintf(exception, BUFSIZE, "CU Exception - no performance "
1077                        "impact");
1078                break;
1079        case 0x03:
1080                snprintf(exception, BUFSIZE, "CU Exception on channel "
1081                        "interface 0x%02x", sense->fmt.f71.md[0]);
1082                break;
1083        case 0x04:
1084                snprintf(exception, BUFSIZE, "CU Exception on device path "
1085                        "0x%02x", sense->fmt.f71.md[0]);
1086                break;
1087        case 0x05:
1088                snprintf(exception, BUFSIZE, "CU Exception on library path "
1089                        "0x%02x", sense->fmt.f71.md[0]);
1090                break;
1091        case 0x06:
1092                snprintf(exception, BUFSIZE, "CU Exception on node 0x%02x",
1093                        sense->fmt.f71.md[0]);
1094                break;
1095        case 0x07:
1096                snprintf(exception, BUFSIZE, "CU Exception on partition "
1097                        "0x%02x", sense->fmt.f71.md[0]);
1098                break;
1099        default:
1100                snprintf(exception, BUFSIZE, "0x%02x",
1101                        sense->fmt.f71.emc);
1102        }
1103        /* Service Message */
1104        switch (sense->fmt.f71.smc) {
1105        case 0x01:
1106                snprintf(service, BUFSIZE, "Repair impact is unknown");
1107                break;
1108        case 0x02:
1109                snprintf(service, BUFSIZE, "Repair will not impact cu "
1110                        "performance");
1111                break;
1112        case 0x03:
1113                if (sense->fmt.f71.mdf == 0)
1114                        snprintf(service, BUFSIZE, "Repair will disable node "
1115                                "0x%x on CU", sense->fmt.f71.md[1]);
1116                else
1117                        snprintf(service, BUFSIZE, "Repair will disable "
1118                                "nodes (0x%x-0x%x) on CU", sense->fmt.f71.md[1],
1119                                sense->fmt.f71.md[2]);
1120                break;
1121        case 0x04:
1122                if (sense->fmt.f71.mdf == 0)
1123                        snprintf(service, BUFSIZE, "Repair will disable "
1124                                "channel path 0x%x on CU",
1125                                sense->fmt.f71.md[1]);
1126                else
1127                        snprintf(service, BUFSIZE, "Repair will disable cannel"
1128                                " paths (0x%x-0x%x) on CU",
1129                                sense->fmt.f71.md[1], sense->fmt.f71.md[2]);
1130                break;
1131        case 0x05:
1132                if (sense->fmt.f71.mdf == 0)
1133                        snprintf(service, BUFSIZE, "Repair will disable device"
1134                                " path 0x%x on CU", sense->fmt.f71.md[1]);
1135                else
1136                        snprintf(service, BUFSIZE, "Repair will disable device"
1137                                " paths (0x%x-0x%x) on CU",
1138                                sense->fmt.f71.md[1], sense->fmt.f71.md[2]);
1139                break;
1140        case 0x06:
1141                if (sense->fmt.f71.mdf == 0)
1142                        snprintf(service, BUFSIZE, "Repair will disable "
1143                                "library path 0x%x on CU",
1144                                sense->fmt.f71.md[1]);
1145                else
1146                        snprintf(service, BUFSIZE, "Repair will disable "
1147                                "library paths (0x%x-0x%x) on CU",
1148                                sense->fmt.f71.md[1], sense->fmt.f71.md[2]);
1149                break;
1150        case 0x07:
1151                snprintf(service, BUFSIZE, "Repair will disable access to CU");
1152                break;
1153        default:
1154                snprintf(service, BUFSIZE, "0x%02x",
1155                        sense->fmt.f71.smc);
1156        }
1157
1158        dev_warn (&device->cdev->dev, "I/O subsystem information: exception"
1159                " %s, service %s\n", exception, service);
1160out_nomem:
1161        kfree(exception);
1162        kfree(service);
1163}
1164
1165/*
1166 * Print an Device Subsystem Service Information Message (message code f2)
1167 */
1168static void
1169tape_3590_print_dev_sim_msg_f2(struct tape_device *device, struct irb *irb)
1170{
1171        struct tape_3590_sense *sense;
1172        char *exception, *service;
1173
1174        exception = kmalloc(BUFSIZE, GFP_ATOMIC);
1175        service = kmalloc(BUFSIZE, GFP_ATOMIC);
1176
1177        if (!exception || !service)
1178                goto out_nomem;
1179
1180        sense = (struct tape_3590_sense *) irb->ecw;
1181        /* Exception Message */
1182        switch (sense->fmt.f71.emc) {
1183        case 0x01:
1184                snprintf(exception, BUFSIZE, "Effect of failure is unknown");
1185                break;
1186        case 0x02:
1187                snprintf(exception, BUFSIZE, "DV Exception - no performance"
1188                        " impact");
1189                break;
1190        case 0x03:
1191                snprintf(exception, BUFSIZE, "DV Exception on channel "
1192                        "interface 0x%02x", sense->fmt.f71.md[0]);
1193                break;
1194        case 0x04:
1195                snprintf(exception, BUFSIZE, "DV Exception on loader 0x%02x",
1196                        sense->fmt.f71.md[0]);
1197                break;
1198        case 0x05:
1199                snprintf(exception, BUFSIZE, "DV Exception on message display"
1200                        " 0x%02x", sense->fmt.f71.md[0]);
1201                break;
1202        case 0x06:
1203                snprintf(exception, BUFSIZE, "DV Exception in tape path");
1204                break;
1205        case 0x07:
1206                snprintf(exception, BUFSIZE, "DV Exception in drive");
1207                break;
1208        default:
1209                snprintf(exception, BUFSIZE, "0x%02x",
1210                        sense->fmt.f71.emc);
1211        }
1212        /* Service Message */
1213        switch (sense->fmt.f71.smc) {
1214        case 0x01:
1215                snprintf(service, BUFSIZE, "Repair impact is unknown");
1216                break;
1217        case 0x02:
1218                snprintf(service, BUFSIZE, "Repair will not impact device "
1219                        "performance");
1220                break;
1221        case 0x03:
1222                if (sense->fmt.f71.mdf == 0)
1223                        snprintf(service, BUFSIZE, "Repair will disable "
1224                                "channel path 0x%x on DV",
1225                                sense->fmt.f71.md[1]);
1226                else
1227                        snprintf(service, BUFSIZE, "Repair will disable "
1228                                "channel path (0x%x-0x%x) on DV",
1229                                sense->fmt.f71.md[1], sense->fmt.f71.md[2]);
1230                break;
1231        case 0x04:
1232                if (sense->fmt.f71.mdf == 0)
1233                        snprintf(service, BUFSIZE, "Repair will disable "
1234                                "interface 0x%x on DV", sense->fmt.f71.md[1]);
1235                else
1236                        snprintf(service, BUFSIZE, "Repair will disable "
1237                                "interfaces (0x%x-0x%x) on DV",
1238                                sense->fmt.f71.md[1], sense->fmt.f71.md[2]);
1239                break;
1240        case 0x05:
1241                if (sense->fmt.f71.mdf == 0)
1242                        snprintf(service, BUFSIZE, "Repair will disable loader"
1243                                " 0x%x on DV", sense->fmt.f71.md[1]);
1244                else
1245                        snprintf(service, BUFSIZE, "Repair will disable loader"
1246                                " (0x%x-0x%x) on DV",
1247                                sense->fmt.f71.md[1], sense->fmt.f71.md[2]);
1248                break;
1249        case 0x07:
1250                snprintf(service, BUFSIZE, "Repair will disable access to DV");
1251                break;
1252        case 0x08:
1253                if (sense->fmt.f71.mdf == 0)
1254                        snprintf(service, BUFSIZE, "Repair will disable "
1255                                "message display 0x%x on DV",
1256                                sense->fmt.f71.md[1]);
1257                else
1258                        snprintf(service, BUFSIZE, "Repair will disable "
1259                                "message displays (0x%x-0x%x) on DV",
1260                                 sense->fmt.f71.md[1], sense->fmt.f71.md[2]);
1261                break;
1262        case 0x09:
1263                snprintf(service, BUFSIZE, "Clean DV");
1264                break;
1265        default:
1266                snprintf(service, BUFSIZE, "0x%02x",
1267                        sense->fmt.f71.smc);
1268        }
1269
1270        dev_warn (&device->cdev->dev, "Device subsystem information: exception"
1271                " %s, service %s\n", exception, service);
1272out_nomem:
1273        kfree(exception);
1274        kfree(service);
1275}
1276
1277/*
1278 * Print standard ERA Message
1279 */
1280static void
1281tape_3590_print_era_msg(struct tape_device *device, struct irb *irb)
1282{
1283        struct tape_3590_sense *sense;
1284
1285        sense = (struct tape_3590_sense *) irb->ecw;
1286        if (sense->mc == 0)
1287                return;
1288        if ((sense->mc > 0) && (sense->mc < TAPE_3590_MAX_MSG)) {
1289                if (tape_3590_msg[sense->mc] != NULL)
1290                        dev_warn (&device->cdev->dev, "The tape unit has "
1291                                "issued sense message %s\n",
1292                                tape_3590_msg[sense->mc]);
1293                else
1294                        dev_warn (&device->cdev->dev, "The tape unit has "
1295                                "issued an unknown sense message code 0x%x\n",
1296                                sense->mc);
1297                return;
1298        }
1299        if (sense->mc == 0xf0) {
1300                /* Standard Media Information Message */
1301                dev_warn (&device->cdev->dev, "MIM SEV=%i, MC=%02x, ES=%x/%x, "
1302                        "RC=%02x-%04x-%02x\n", sense->fmt.f70.sev, sense->mc,
1303                        sense->fmt.f70.emc, sense->fmt.f70.smc,
1304                        sense->fmt.f70.refcode, sense->fmt.f70.mid,
1305                        sense->fmt.f70.fid);
1306                tape_3590_print_mim_msg_f0(device, irb);
1307                return;
1308        }
1309        if (sense->mc == 0xf1) {
1310                /* Standard I/O Subsystem Service Information Message */
1311                dev_warn (&device->cdev->dev, "IOSIM SEV=%i, DEVTYPE=3590/%02x,"
1312                        " MC=%02x, ES=%x/%x, REF=0x%04x-0x%04x-0x%04x\n",
1313                        sense->fmt.f71.sev, device->cdev->id.dev_model,
1314                        sense->mc, sense->fmt.f71.emc, sense->fmt.f71.smc,
1315                        sense->fmt.f71.refcode1, sense->fmt.f71.refcode2,
1316                        sense->fmt.f71.refcode3);
1317                tape_3590_print_io_sim_msg_f1(device, irb);
1318                return;
1319        }
1320        if (sense->mc == 0xf2) {
1321                /* Standard Device Service Information Message */
1322                dev_warn (&device->cdev->dev, "DEVSIM SEV=%i, DEVTYPE=3590/%02x"
1323                        ", MC=%02x, ES=%x/%x, REF=0x%04x-0x%04x-0x%04x\n",
1324                        sense->fmt.f71.sev, device->cdev->id.dev_model,
1325                        sense->mc, sense->fmt.f71.emc, sense->fmt.f71.smc,
1326                        sense->fmt.f71.refcode1, sense->fmt.f71.refcode2,
1327                        sense->fmt.f71.refcode3);
1328                tape_3590_print_dev_sim_msg_f2(device, irb);
1329                return;
1330        }
1331        if (sense->mc == 0xf3) {
1332                /* Standard Library Service Information Message */
1333                return;
1334        }
1335        dev_warn (&device->cdev->dev, "The tape unit has issued an unknown "
1336                "sense message code %x\n", sense->mc);
1337}
1338
1339static int tape_3590_crypt_error(struct tape_device *device,
1340                                 struct tape_request *request, struct irb *irb)
1341{
1342        u8 cu_rc, ekm_rc1;
1343        u16 ekm_rc2;
1344        u32 drv_rc;
1345        const char *bus_id;
1346        char *sense;
1347
1348        sense = ((struct tape_3590_sense *) irb->ecw)->fmt.data;
1349        bus_id = dev_name(&device->cdev->dev);
1350        cu_rc = sense[0];
1351        drv_rc = *((u32*) &sense[5]) & 0xffffff;
1352        ekm_rc1 = sense[9];
1353        ekm_rc2 = *((u16*) &sense[10]);
1354        if ((cu_rc == 0) && (ekm_rc2 == 0xee31))
1355                /* key not defined on EKM */
1356                return tape_3590_erp_basic(device, request, irb, -EKEYREJECTED);
1357        if ((cu_rc == 1) || (cu_rc == 2))
1358                /* No connection to EKM */
1359                return tape_3590_erp_basic(device, request, irb, -ENOTCONN);
1360
1361        dev_err (&device->cdev->dev, "The tape unit failed to obtain the "
1362                "encryption key from EKM\n");
1363
1364        return tape_3590_erp_basic(device, request, irb, -ENOKEY);
1365}
1366
1367/*
1368 *  3590 error Recovery routine:
1369 *  If possible, it tries to recover from the error. If this is not possible,
1370 *  inform the user about the problem.
1371 */
1372static int
1373tape_3590_unit_check(struct tape_device *device, struct tape_request *request,
1374                     struct irb *irb)
1375{
1376        struct tape_3590_sense *sense;
1377        int rc;
1378
1379#ifdef CONFIG_S390_TAPE_BLOCK
1380        if (request->op == TO_BLOCK) {
1381                /*
1382                 * Recovery for block device requests. Set the block_position
1383                 * to something invalid and retry.
1384                 */
1385                device->blk_data.block_position = -1;
1386                if (request->retries-- <= 0)
1387                        return tape_3590_erp_failed(device, request, irb, -EIO);
1388                else
1389                        return tape_3590_erp_retry(device, request, irb);
1390        }
1391#endif
1392
1393        sense = (struct tape_3590_sense *) irb->ecw;
1394
1395        DBF_EVENT(6, "Unit Check: RQC = %x\n", sense->rc_rqc);
1396
1397        /*
1398         * First check all RC-QRCs where we want to do something special
1399         *   - "break":     basic error recovery is done
1400         *   - "goto out:": just print error message if available
1401         */
1402        rc = -EIO;
1403        switch (sense->rc_rqc) {
1404
1405        case 0x1110:
1406                tape_3590_print_era_msg(device, irb);
1407                return tape_3590_erp_read_buf_log(device, request, irb);
1408
1409        case 0x2011:
1410                tape_3590_print_era_msg(device, irb);
1411                return tape_3590_erp_read_alternate(device, request, irb);
1412
1413        case 0x2230:
1414        case 0x2231:
1415                tape_3590_print_era_msg(device, irb);
1416                return tape_3590_erp_special_interrupt(device, request, irb);
1417        case 0x2240:
1418                return tape_3590_crypt_error(device, request, irb);
1419
1420        case 0x3010:
1421                DBF_EVENT(2, "(%08x): Backward at Beginning of Partition\n",
1422                          device->cdev_id);
1423                return tape_3590_erp_basic(device, request, irb, -ENOSPC);
1424        case 0x3012:
1425                DBF_EVENT(2, "(%08x): Forward at End of Partition\n",
1426                          device->cdev_id);
1427                return tape_3590_erp_basic(device, request, irb, -ENOSPC);
1428        case 0x3020:
1429                DBF_EVENT(2, "(%08x): End of Data Mark\n", device->cdev_id);
1430                return tape_3590_erp_basic(device, request, irb, -ENOSPC);
1431
1432        case 0x3122:
1433                DBF_EVENT(2, "(%08x): Rewind Unload initiated\n",
1434                          device->cdev_id);
1435                return tape_3590_erp_basic(device, request, irb, -EIO);
1436        case 0x3123:
1437                DBF_EVENT(2, "(%08x): Rewind Unload complete\n",
1438                          device->cdev_id);
1439                tape_med_state_set(device, MS_UNLOADED);
1440                tape_3590_schedule_work(device, TO_CRYPT_OFF);
1441                return tape_3590_erp_basic(device, request, irb, 0);
1442
1443        case 0x4010:
1444                /*
1445                 * print additional msg since default msg
1446                 * "device intervention" is not very meaningfull
1447                 */
1448                tape_med_state_set(device, MS_UNLOADED);
1449                tape_3590_schedule_work(device, TO_CRYPT_OFF);
1450                return tape_3590_erp_basic(device, request, irb, -ENOMEDIUM);
1451        case 0x4012:            /* Device Long Busy */
1452                /* XXX: Also use long busy handling here? */
1453                DBF_EVENT(6, "(%08x): LONG BUSY\n", device->cdev_id);
1454                tape_3590_print_era_msg(device, irb);
1455                return tape_3590_erp_basic(device, request, irb, -EBUSY);
1456        case 0x4014:
1457                DBF_EVENT(6, "(%08x): Crypto LONG BUSY\n", device->cdev_id);
1458                return tape_3590_erp_long_busy(device, request, irb);
1459
1460        case 0x5010:
1461                if (sense->rac == 0xd0) {
1462                        /* Swap */
1463                        tape_3590_print_era_msg(device, irb);
1464                        return tape_3590_erp_swap(device, request, irb);
1465                }
1466                if (sense->rac == 0x26) {
1467                        /* Read Opposite */
1468                        tape_3590_print_era_msg(device, irb);
1469                        return tape_3590_erp_read_opposite(device, request,
1470                                                           irb);
1471                }
1472                return tape_3590_erp_basic(device, request, irb, -EIO);
1473        case 0x5020:
1474        case 0x5021:
1475        case 0x5022:
1476        case 0x5040:
1477        case 0x5041:
1478        case 0x5042:
1479                tape_3590_print_era_msg(device, irb);
1480                return tape_3590_erp_swap(device, request, irb);
1481
1482        case 0x5110:
1483        case 0x5111:
1484                return tape_3590_erp_basic(device, request, irb, -EMEDIUMTYPE);
1485
1486        case 0x5120:
1487        case 0x1120:
1488                tape_med_state_set(device, MS_UNLOADED);
1489                tape_3590_schedule_work(device, TO_CRYPT_OFF);
1490                return tape_3590_erp_basic(device, request, irb, -ENOMEDIUM);
1491
1492        case 0x6020:
1493                return tape_3590_erp_basic(device, request, irb, -EMEDIUMTYPE);
1494
1495        case 0x8011:
1496                return tape_3590_erp_basic(device, request, irb, -EPERM);
1497        case 0x8013:
1498                dev_warn (&device->cdev->dev, "A different host has privileged"
1499                        " access to the tape unit\n");
1500                return tape_3590_erp_basic(device, request, irb, -EPERM);
1501        default:
1502                return tape_3590_erp_basic(device, request, irb, -EIO);
1503        }
1504}
1505
1506/*
1507 * 3590 interrupt handler:
1508 */
1509static int
1510tape_3590_irq(struct tape_device *device, struct tape_request *request,
1511              struct irb *irb)
1512{
1513        if (request == NULL)
1514                return tape_3590_unsolicited_irq(device, irb);
1515
1516        if ((irb->scsw.cmd.dstat & DEV_STAT_UNIT_EXCEP) &&
1517            (irb->scsw.cmd.dstat & DEV_STAT_DEV_END) &&
1518            (request->op == TO_WRI)) {
1519                /* Write at end of volume */
1520                DBF_EVENT(2, "End of volume\n");
1521                return tape_3590_erp_failed(device, request, irb, -ENOSPC);
1522        }
1523
1524        if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK)
1525                return tape_3590_unit_check(device, request, irb);
1526
1527        if (irb->scsw.cmd.dstat & DEV_STAT_DEV_END) {
1528                if (irb->scsw.cmd.dstat == DEV_STAT_UNIT_EXCEP) {
1529                        if (request->op == TO_FSB || request->op == TO_BSB)
1530                                request->rescnt++;
1531                        else
1532                                DBF_EVENT(5, "Unit Exception!\n");
1533                }
1534
1535                return tape_3590_done(device, request);
1536        }
1537
1538        if (irb->scsw.cmd.dstat & DEV_STAT_CHN_END) {
1539                DBF_EVENT(2, "cannel end\n");
1540                return TAPE_IO_PENDING;
1541        }
1542
1543        if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) {
1544                DBF_EVENT(2, "Unit Attention when busy..\n");
1545                return TAPE_IO_PENDING;
1546        }
1547
1548        DBF_EVENT(6, "xunknownirq\n");
1549        tape_dump_sense_dbf(device, request, irb);
1550        return TAPE_IO_STOP;
1551}
1552
1553
1554static int tape_3590_read_dev_chars(struct tape_device *device,
1555                                    struct tape_3590_rdc_data *rdc_data)
1556{
1557        int rc;
1558        struct tape_request *request;
1559
1560        request = tape_alloc_request(1, sizeof(*rdc_data));
1561        if (IS_ERR(request))
1562                return PTR_ERR(request);
1563        request->op = TO_RDC;
1564        tape_ccw_end(request->cpaddr, CCW_CMD_RDC, sizeof(*rdc_data),
1565                     request->cpdata);
1566        rc = tape_do_io(device, request);
1567        if (rc == 0)
1568                memcpy(rdc_data, request->cpdata, sizeof(*rdc_data));
1569        tape_free_request(request);
1570        return rc;
1571}
1572
1573/*
1574 * Setup device function
1575 */
1576static int
1577tape_3590_setup_device(struct tape_device *device)
1578{
1579        int rc;
1580        struct tape_3590_disc_data *data;
1581        struct tape_3590_rdc_data *rdc_data;
1582
1583        DBF_EVENT(6, "3590 device setup\n");
1584        data = kzalloc(sizeof(struct tape_3590_disc_data), GFP_KERNEL | GFP_DMA);
1585        if (data == NULL)
1586                return -ENOMEM;
1587        data->read_back_op = READ_PREVIOUS;
1588        device->discdata = data;
1589
1590        rdc_data = kmalloc(sizeof(*rdc_data), GFP_KERNEL | GFP_DMA);
1591        if (!rdc_data) {
1592                rc = -ENOMEM;
1593                goto fail_kmalloc;
1594        }
1595        rc = tape_3590_read_dev_chars(device, rdc_data);
1596        if (rc) {
1597                DBF_LH(3, "Read device characteristics failed!\n");
1598                goto fail_rdc_data;
1599        }
1600        rc = tape_std_assign(device);
1601        if (rc)
1602                goto fail_rdc_data;
1603        if (rdc_data->data[31] == 0x13) {
1604                data->crypt_info.capability |= TAPE390_CRYPT_SUPPORTED_MASK;
1605                tape_3592_disable_crypt(device);
1606        } else {
1607                DBF_EVENT(6, "Device has NO crypto support\n");
1608        }
1609        /* Try to find out if medium is loaded */
1610        rc = tape_3590_sense_medium(device);
1611        if (rc) {
1612                DBF_LH(3, "3590 medium sense returned %d\n", rc);
1613                goto fail_rdc_data;
1614        }
1615        return 0;
1616
1617fail_rdc_data:
1618        kfree(rdc_data);
1619fail_kmalloc:
1620        kfree(data);
1621        return rc;
1622}
1623
1624/*
1625 * Cleanup device function
1626 */
1627static void
1628tape_3590_cleanup_device(struct tape_device *device)
1629{
1630        flush_scheduled_work();
1631        tape_std_unassign(device);
1632
1633        kfree(device->discdata);
1634        device->discdata = NULL;
1635}
1636
1637/*
1638 * List of 3590 magnetic tape commands.
1639 */
1640static tape_mtop_fn tape_3590_mtop[TAPE_NR_MTOPS] = {
1641        [MTRESET]        = tape_std_mtreset,
1642        [MTFSF]          = tape_std_mtfsf,
1643        [MTBSF]          = tape_std_mtbsf,
1644        [MTFSR]          = tape_std_mtfsr,
1645        [MTBSR]          = tape_std_mtbsr,
1646        [MTWEOF]         = tape_std_mtweof,
1647        [MTREW]          = tape_std_mtrew,
1648        [MTOFFL]         = tape_std_mtoffl,
1649        [MTNOP]          = tape_std_mtnop,
1650        [MTRETEN]        = tape_std_mtreten,
1651        [MTBSFM]         = tape_std_mtbsfm,
1652        [MTFSFM]         = tape_std_mtfsfm,
1653        [MTEOM]          = tape_std_mteom,
1654        [MTERASE]        = tape_std_mterase,
1655        [MTRAS1]         = NULL,
1656        [MTRAS2]         = NULL,
1657        [MTRAS3]         = NULL,
1658        [MTSETBLK]       = tape_std_mtsetblk,
1659        [MTSETDENSITY]   = NULL,
1660        [MTSEEK]         = tape_3590_mtseek,
1661        [MTTELL]         = tape_3590_mttell,
1662        [MTSETDRVBUFFER] = NULL,
1663        [MTFSS]          = NULL,
1664        [MTBSS]          = NULL,
1665        [MTWSM]          = NULL,
1666        [MTLOCK]         = NULL,
1667        [MTUNLOCK]       = NULL,
1668        [MTLOAD]         = tape_std_mtload,
1669        [MTUNLOAD]       = tape_std_mtunload,
1670        [MTCOMPRESSION]  = tape_std_mtcompression,
1671        [MTSETPART]      = NULL,
1672        [MTMKPART]       = NULL
1673};
1674
1675/*
1676 * Tape discipline structure for 3590.
1677 */
1678static struct tape_discipline tape_discipline_3590 = {
1679        .owner = THIS_MODULE,
1680        .setup_device = tape_3590_setup_device,
1681        .cleanup_device = tape_3590_cleanup_device,
1682        .process_eov = tape_std_process_eov,
1683        .irq = tape_3590_irq,
1684        .read_block = tape_std_read_block,
1685        .write_block = tape_std_write_block,
1686#ifdef CONFIG_S390_TAPE_BLOCK
1687        .bread = tape_3590_bread,
1688        .free_bread = tape_3590_free_bread,
1689        .check_locate = tape_3590_check_locate,
1690#endif
1691        .ioctl_fn = tape_3590_ioctl,
1692        .mtop_array = tape_3590_mtop
1693};
1694
1695static struct ccw_device_id tape_3590_ids[] = {
1696        {CCW_DEVICE_DEVTYPE(0x3590, 0, 0x3590, 0), .driver_info = tape_3590},
1697        {CCW_DEVICE_DEVTYPE(0x3592, 0, 0x3592, 0), .driver_info = tape_3592},
1698        { /* end of list */ }
1699};
1700
1701static int
1702tape_3590_online(struct ccw_device *cdev)
1703{
1704        return tape_generic_online(dev_get_drvdata(&cdev->dev),
1705                                   &tape_discipline_3590);
1706}
1707
1708static struct ccw_driver tape_3590_driver = {
1709        .name = "tape_3590",
1710        .owner = THIS_MODULE,
1711        .ids = tape_3590_ids,
1712        .probe = tape_generic_probe,
1713        .remove = tape_generic_remove,
1714        .set_offline = tape_generic_offline,
1715        .set_online = tape_3590_online,
1716        .freeze = tape_generic_pm_suspend,
1717};
1718
1719/*
1720 * Setup discipline structure.
1721 */
1722static int
1723tape_3590_init(void)
1724{
1725        int rc;
1726
1727        TAPE_DBF_AREA = debug_register("tape_3590", 2, 2, 4 * sizeof(long));
1728        debug_register_view(TAPE_DBF_AREA, &debug_sprintf_view);
1729#ifdef DBF_LIKE_HELL
1730        debug_set_level(TAPE_DBF_AREA, 6);
1731#endif
1732
1733        DBF_EVENT(3, "3590 init\n");
1734        /* Register driver for 3590 tapes. */
1735        rc = ccw_driver_register(&tape_3590_driver);
1736        if (rc)
1737                DBF_EVENT(3, "3590 init failed\n");
1738        else
1739                DBF_EVENT(3, "3590 registered\n");
1740        return rc;
1741}
1742
1743static void
1744tape_3590_exit(void)
1745{
1746        ccw_driver_unregister(&tape_3590_driver);
1747
1748        debug_unregister(TAPE_DBF_AREA);
1749}
1750
1751MODULE_DEVICE_TABLE(ccw, tape_3590_ids);
1752MODULE_AUTHOR("(C) 2001,2006 IBM Corporation");
1753MODULE_DESCRIPTION("Linux on zSeries channel attached 3590 tape device driver");
1754MODULE_LICENSE("GPL");
1755
1756module_init(tape_3590_init);
1757module_exit(tape_3590_exit);
1758