Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  *    tape device discipline for 3480/3490 tapes.
0004  *
0005  *    Copyright IBM Corp. 2001, 2009
0006  *    Author(s): Carsten Otte <cotte@de.ibm.com>
0007  *       Tuan Ngo-Anh <ngoanh@de.ibm.com>
0008  *       Martin Schwidefsky <schwidefsky@de.ibm.com>
0009  */
0010 
0011 #define KMSG_COMPONENT "tape_34xx"
0012 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
0013 
0014 #include <linux/module.h>
0015 #include <linux/init.h>
0016 #include <linux/bio.h>
0017 #include <linux/workqueue.h>
0018 #include <linux/slab.h>
0019 
0020 #define TAPE_DBF_AREA   tape_34xx_dbf
0021 
0022 #include "tape.h"
0023 #include "tape_std.h"
0024 
0025 /*
0026  * Pointer to debug area.
0027  */
0028 debug_info_t *TAPE_DBF_AREA = NULL;
0029 EXPORT_SYMBOL(TAPE_DBF_AREA);
0030 
0031 #define TAPE34XX_FMT_3480   0
0032 #define TAPE34XX_FMT_3480_2_XF  1
0033 #define TAPE34XX_FMT_3480_XF    2
0034 
0035 struct tape_34xx_block_id {
0036     unsigned int    wrap        : 1;
0037     unsigned int    segment     : 7;
0038     unsigned int    format      : 2;
0039     unsigned int    block       : 22;
0040 };
0041 
0042 /*
0043  * A list of block ID's is used to faster seek blocks.
0044  */
0045 struct tape_34xx_sbid {
0046     struct list_head        list;
0047     struct tape_34xx_block_id   bid;
0048 };
0049 
0050 static void tape_34xx_delete_sbid_from(struct tape_device *, int);
0051 
0052 /*
0053  * Medium sense for 34xx tapes. There is no 'real' medium sense call.
0054  * So we just do a normal sense.
0055  */
0056 static void __tape_34xx_medium_sense(struct tape_request *request)
0057 {
0058     struct tape_device *device = request->device;
0059     unsigned char *sense;
0060 
0061     if (request->rc == 0) {
0062         sense = request->cpdata;
0063 
0064         /*
0065          * This isn't quite correct. But since INTERVENTION_REQUIRED
0066          * means that the drive is 'neither ready nor on-line' it is
0067          * only slightly inaccurate to say there is no tape loaded if
0068          * the drive isn't online...
0069          */
0070         if (sense[0] & SENSE_INTERVENTION_REQUIRED)
0071             tape_med_state_set(device, MS_UNLOADED);
0072         else
0073             tape_med_state_set(device, MS_LOADED);
0074 
0075         if (sense[1] & SENSE_WRITE_PROTECT)
0076             device->tape_generic_status |= GMT_WR_PROT(~0);
0077         else
0078             device->tape_generic_status &= ~GMT_WR_PROT(~0);
0079     } else
0080         DBF_EVENT(4, "tape_34xx: medium sense failed with rc=%d\n",
0081             request->rc);
0082     tape_free_request(request);
0083 }
0084 
0085 static int tape_34xx_medium_sense(struct tape_device *device)
0086 {
0087     struct tape_request *request;
0088     int rc;
0089 
0090     request = tape_alloc_request(1, 32);
0091     if (IS_ERR(request)) {
0092         DBF_EXCEPTION(6, "MSEN fail\n");
0093         return PTR_ERR(request);
0094     }
0095 
0096     request->op = TO_MSEN;
0097     tape_ccw_end(request->cpaddr, SENSE, 32, request->cpdata);
0098     rc = tape_do_io_interruptible(device, request);
0099     __tape_34xx_medium_sense(request);
0100     return rc;
0101 }
0102 
0103 static void tape_34xx_medium_sense_async(struct tape_device *device)
0104 {
0105     struct tape_request *request;
0106 
0107     request = tape_alloc_request(1, 32);
0108     if (IS_ERR(request)) {
0109         DBF_EXCEPTION(6, "MSEN fail\n");
0110         return;
0111     }
0112 
0113     request->op = TO_MSEN;
0114     tape_ccw_end(request->cpaddr, SENSE, 32, request->cpdata);
0115     request->callback = (void *) __tape_34xx_medium_sense;
0116     request->callback_data = NULL;
0117     tape_do_io_async(device, request);
0118 }
0119 
0120 struct tape_34xx_work {
0121     struct tape_device  *device;
0122     enum tape_op         op;
0123     struct work_struct   work;
0124 };
0125 
0126 /*
0127  * These functions are currently used only to schedule a medium_sense for
0128  * later execution. This is because we get an interrupt whenever a medium
0129  * is inserted but cannot call tape_do_io* from an interrupt context.
0130  * Maybe that's useful for other actions we want to start from the
0131  * interrupt handler.
0132  * Note: the work handler is called by the system work queue. The tape
0133  * commands started by the handler need to be asynchrounous, otherwise
0134  * a deadlock can occur e.g. in case of a deferred cc=1 (see __tape_do_irq).
0135  */
0136 static void
0137 tape_34xx_work_handler(struct work_struct *work)
0138 {
0139     struct tape_34xx_work *p =
0140         container_of(work, struct tape_34xx_work, work);
0141     struct tape_device *device = p->device;
0142 
0143     switch(p->op) {
0144         case TO_MSEN:
0145             tape_34xx_medium_sense_async(device);
0146             break;
0147         default:
0148             DBF_EVENT(3, "T34XX: internal error: unknown work\n");
0149     }
0150     tape_put_device(device);
0151     kfree(p);
0152 }
0153 
0154 static int
0155 tape_34xx_schedule_work(struct tape_device *device, enum tape_op op)
0156 {
0157     struct tape_34xx_work *p;
0158 
0159     if ((p = kzalloc(sizeof(*p), GFP_ATOMIC)) == NULL)
0160         return -ENOMEM;
0161 
0162     INIT_WORK(&p->work, tape_34xx_work_handler);
0163 
0164     p->device = tape_get_device(device);
0165     p->op     = op;
0166 
0167     schedule_work(&p->work);
0168     return 0;
0169 }
0170 
0171 /*
0172  * Done Handler is called when dev stat = DEVICE-END (successful operation)
0173  */
0174 static inline int
0175 tape_34xx_done(struct tape_request *request)
0176 {
0177     DBF_EVENT(6, "%s done\n", tape_op_verbose[request->op]);
0178 
0179     switch (request->op) {
0180         case TO_DSE:
0181         case TO_RUN:
0182         case TO_WRI:
0183         case TO_WTM:
0184         case TO_ASSIGN:
0185         case TO_UNASSIGN:
0186             tape_34xx_delete_sbid_from(request->device, 0);
0187             break;
0188         default:
0189             ;
0190     }
0191     return TAPE_IO_SUCCESS;
0192 }
0193 
0194 static inline int
0195 tape_34xx_erp_failed(struct tape_request *request, int rc)
0196 {
0197     DBF_EVENT(3, "Error recovery failed for %s (RC=%d)\n",
0198           tape_op_verbose[request->op], rc);
0199     return rc;
0200 }
0201 
0202 static inline int
0203 tape_34xx_erp_succeeded(struct tape_request *request)
0204 {
0205     DBF_EVENT(3, "Error Recovery successful for %s\n",
0206           tape_op_verbose[request->op]);
0207     return tape_34xx_done(request);
0208 }
0209 
0210 static inline int
0211 tape_34xx_erp_retry(struct tape_request *request)
0212 {
0213     DBF_EVENT(3, "xerp retr %s\n", tape_op_verbose[request->op]);
0214     return TAPE_IO_RETRY;
0215 }
0216 
0217 /*
0218  * This function is called, when no request is outstanding and we get an
0219  * interrupt
0220  */
0221 static int
0222 tape_34xx_unsolicited_irq(struct tape_device *device, struct irb *irb)
0223 {
0224     if (irb->scsw.cmd.dstat == 0x85) { /* READY */
0225         /* A medium was inserted in the drive. */
0226         DBF_EVENT(6, "xuud med\n");
0227         tape_34xx_delete_sbid_from(device, 0);
0228         tape_34xx_schedule_work(device, TO_MSEN);
0229     } else {
0230         DBF_EVENT(3, "unsol.irq! dev end: %08x\n", device->cdev_id);
0231         tape_dump_sense_dbf(device, NULL, irb);
0232     }
0233     return TAPE_IO_SUCCESS;
0234 }
0235 
0236 /*
0237  * Read Opposite Error Recovery Function:
0238  * Used, when Read Forward does not work
0239  */
0240 static int
0241 tape_34xx_erp_read_opposite(struct tape_device *device,
0242                 struct tape_request *request)
0243 {
0244     if (request->op == TO_RFO) {
0245         /*
0246          * We did read forward, but the data could not be read
0247          * *correctly*. We transform the request to a read backward
0248          * and try again.
0249          */
0250         tape_std_read_backward(device, request);
0251         return tape_34xx_erp_retry(request);
0252     }
0253 
0254     /*
0255      * We tried to read forward and backward, but hat no
0256      * success -> failed.
0257      */
0258     return tape_34xx_erp_failed(request, -EIO);
0259 }
0260 
0261 static int
0262 tape_34xx_erp_bug(struct tape_device *device, struct tape_request *request,
0263           struct irb *irb, int no)
0264 {
0265     if (request->op != TO_ASSIGN) {
0266         dev_err(&device->cdev->dev, "An unexpected condition %d "
0267             "occurred in tape error recovery\n", no);
0268         tape_dump_sense_dbf(device, request, irb);
0269     }
0270     return tape_34xx_erp_failed(request, -EIO);
0271 }
0272 
0273 /*
0274  * Handle data overrun between cu and drive. The channel speed might
0275  * be too slow.
0276  */
0277 static int
0278 tape_34xx_erp_overrun(struct tape_device *device, struct tape_request *request,
0279               struct irb *irb)
0280 {
0281     if (irb->ecw[3] == 0x40) {
0282         dev_warn (&device->cdev->dev, "A data overrun occurred between"
0283             " the control unit and tape unit\n");
0284         return tape_34xx_erp_failed(request, -EIO);
0285     }
0286     return tape_34xx_erp_bug(device, request, irb, -1);
0287 }
0288 
0289 /*
0290  * Handle record sequence error.
0291  */
0292 static int
0293 tape_34xx_erp_sequence(struct tape_device *device,
0294                struct tape_request *request, struct irb *irb)
0295 {
0296     if (irb->ecw[3] == 0x41) {
0297         /*
0298          * cu detected incorrect block-id sequence on tape.
0299          */
0300         dev_warn (&device->cdev->dev, "The block ID sequence on the "
0301             "tape is incorrect\n");
0302         return tape_34xx_erp_failed(request, -EIO);
0303     }
0304     /*
0305      * Record sequence error bit is set, but erpa does not
0306      * show record sequence error.
0307      */
0308     return tape_34xx_erp_bug(device, request, irb, -2);
0309 }
0310 
0311 /*
0312  * This function analyses the tape's sense-data in case of a unit-check.
0313  * If possible, it tries to recover from the error. Else the user is
0314  * informed about the problem.
0315  */
0316 static int
0317 tape_34xx_unit_check(struct tape_device *device, struct tape_request *request,
0318              struct irb *irb)
0319 {
0320     int inhibit_cu_recovery;
0321     __u8* sense;
0322 
0323     inhibit_cu_recovery = (*device->modeset_byte & 0x80) ? 1 : 0;
0324     sense = irb->ecw;
0325 
0326     if (
0327         sense[0] & SENSE_COMMAND_REJECT &&
0328         sense[1] & SENSE_WRITE_PROTECT
0329     ) {
0330         if (
0331             request->op == TO_DSE ||
0332             request->op == TO_WRI ||
0333             request->op == TO_WTM
0334         ) {
0335             /* medium is write protected */
0336             return tape_34xx_erp_failed(request, -EACCES);
0337         } else {
0338             return tape_34xx_erp_bug(device, request, irb, -3);
0339         }
0340     }
0341 
0342     /*
0343      * Special cases for various tape-states when reaching
0344      * end of recorded area
0345      *
0346      * FIXME: Maybe a special case of the special case:
0347      *        sense[0] == SENSE_EQUIPMENT_CHECK &&
0348      *        sense[1] == SENSE_DRIVE_ONLINE    &&
0349      *        sense[3] == 0x47 (Volume Fenced)
0350      *
0351      *        This was caused by continued FSF or FSR after an
0352      *        'End Of Data'.
0353      */
0354     if ((
0355         sense[0] == SENSE_DATA_CHECK      ||
0356         sense[0] == SENSE_EQUIPMENT_CHECK ||
0357         sense[0] == (SENSE_EQUIPMENT_CHECK | SENSE_DEFERRED_UNIT_CHECK)
0358     ) && (
0359         sense[1] == SENSE_DRIVE_ONLINE ||
0360         sense[1] == (SENSE_BEGINNING_OF_TAPE | SENSE_WRITE_MODE)
0361     )) {
0362         switch (request->op) {
0363         /*
0364          * sense[0] == SENSE_DATA_CHECK   &&
0365          * sense[1] == SENSE_DRIVE_ONLINE
0366          * sense[3] == 0x36 (End Of Data)
0367          *
0368          * Further seeks might return a 'Volume Fenced'.
0369          */
0370         case TO_FSF:
0371         case TO_FSB:
0372             /* Trying to seek beyond end of recorded area */
0373             return tape_34xx_erp_failed(request, -ENOSPC);
0374         case TO_BSB:
0375             return tape_34xx_erp_retry(request);
0376 
0377         /*
0378          * sense[0] == SENSE_DATA_CHECK   &&
0379          * sense[1] == SENSE_DRIVE_ONLINE &&
0380          * sense[3] == 0x36 (End Of Data)
0381          */
0382         case TO_LBL:
0383             /* Block could not be located. */
0384             tape_34xx_delete_sbid_from(device, 0);
0385             return tape_34xx_erp_failed(request, -EIO);
0386 
0387         case TO_RFO:
0388             /* Read beyond end of recorded area -> 0 bytes read */
0389             return tape_34xx_erp_failed(request, 0);
0390 
0391         /*
0392          * sense[0] == SENSE_EQUIPMENT_CHECK &&
0393          * sense[1] == SENSE_DRIVE_ONLINE    &&
0394          * sense[3] == 0x38 (Physical End Of Volume)
0395          */
0396         case TO_WRI:
0397             /* Writing at physical end of volume */
0398             return tape_34xx_erp_failed(request, -ENOSPC);
0399         default:
0400             return tape_34xx_erp_failed(request, 0);
0401         }
0402     }
0403 
0404     /* Sensing special bits */
0405     if (sense[0] & SENSE_BUS_OUT_CHECK)
0406         return tape_34xx_erp_retry(request);
0407 
0408     if (sense[0] & SENSE_DATA_CHECK) {
0409         /*
0410          * hardware failure, damaged tape or improper
0411          * operating conditions
0412          */
0413         switch (sense[3]) {
0414         case 0x23:
0415             /* a read data check occurred */
0416             if ((sense[2] & SENSE_TAPE_SYNC_MODE) ||
0417                 inhibit_cu_recovery)
0418                 // data check is not permanent, may be
0419                 // recovered. We always use async-mode with
0420                 // cu-recovery, so this should *never* happen.
0421                 return tape_34xx_erp_bug(device, request,
0422                              irb, -4);
0423 
0424             /* data check is permanent, CU recovery has failed */
0425             dev_warn (&device->cdev->dev, "A read error occurred "
0426                 "that cannot be recovered\n");
0427             return tape_34xx_erp_failed(request, -EIO);
0428         case 0x25:
0429             // a write data check occurred
0430             if ((sense[2] & SENSE_TAPE_SYNC_MODE) ||
0431                 inhibit_cu_recovery)
0432                 // data check is not permanent, may be
0433                 // recovered. We always use async-mode with
0434                 // cu-recovery, so this should *never* happen.
0435                 return tape_34xx_erp_bug(device, request,
0436                              irb, -5);
0437 
0438             // data check is permanent, cu-recovery has failed
0439             dev_warn (&device->cdev->dev, "A write error on the "
0440                 "tape cannot be recovered\n");
0441             return tape_34xx_erp_failed(request, -EIO);
0442         case 0x26:
0443             /* Data Check (read opposite) occurred. */
0444             return tape_34xx_erp_read_opposite(device, request);
0445         case 0x28:
0446             /* ID-Mark at tape start couldn't be written */
0447             dev_warn (&device->cdev->dev, "Writing the ID-mark "
0448                 "failed\n");
0449             return tape_34xx_erp_failed(request, -EIO);
0450         case 0x31:
0451             /* Tape void. Tried to read beyond end of device. */
0452             dev_warn (&device->cdev->dev, "Reading the tape beyond"
0453                 " the end of the recorded area failed\n");
0454             return tape_34xx_erp_failed(request, -ENOSPC);
0455         case 0x41:
0456             /* Record sequence error. */
0457             dev_warn (&device->cdev->dev, "The tape contains an "
0458                 "incorrect block ID sequence\n");
0459             return tape_34xx_erp_failed(request, -EIO);
0460         default:
0461             /* all data checks for 3480 should result in one of
0462              * the above erpa-codes. For 3490, other data-check
0463              * conditions do exist. */
0464             if (device->cdev->id.driver_info == tape_3480)
0465                 return tape_34xx_erp_bug(device, request,
0466                              irb, -6);
0467         }
0468     }
0469 
0470     if (sense[0] & SENSE_OVERRUN)
0471         return tape_34xx_erp_overrun(device, request, irb);
0472 
0473     if (sense[1] & SENSE_RECORD_SEQUENCE_ERR)
0474         return tape_34xx_erp_sequence(device, request, irb);
0475 
0476     /* Sensing erpa codes */
0477     switch (sense[3]) {
0478     case 0x00:
0479         /* Unit check with erpa code 0. Report and ignore. */
0480         return TAPE_IO_SUCCESS;
0481     case 0x21:
0482         /*
0483          * Data streaming not operational. CU will switch to
0484          * interlock mode. Reissue the command.
0485          */
0486         return tape_34xx_erp_retry(request);
0487     case 0x22:
0488         /*
0489          * Path equipment check. Might be drive adapter error, buffer
0490          * error on the lower interface, internal path not usable,
0491          * or error during cartridge load.
0492          */
0493         dev_warn (&device->cdev->dev, "A path equipment check occurred"
0494             " for the tape device\n");
0495         return tape_34xx_erp_failed(request, -EIO);
0496     case 0x24:
0497         /*
0498          * Load display check. Load display was command was issued,
0499          * but the drive is displaying a drive check message. Can
0500          * be threated as "device end".
0501          */
0502         return tape_34xx_erp_succeeded(request);
0503     case 0x27:
0504         /*
0505          * Command reject. May indicate illegal channel program or
0506          * buffer over/underrun. Since all channel programs are
0507          * issued by this driver and ought be correct, we assume a
0508          * over/underrun situation and retry the channel program.
0509          */
0510         return tape_34xx_erp_retry(request);
0511     case 0x29:
0512         /*
0513          * Function incompatible. Either the tape is idrc compressed
0514          * but the hardware isn't capable to do idrc, or a perform
0515          * subsystem func is issued and the CU is not on-line.
0516          */
0517         return tape_34xx_erp_failed(request, -EIO);
0518     case 0x2a:
0519         /*
0520          * Unsolicited environmental data. An internal counter
0521          * overflows, we can ignore this and reissue the cmd.
0522          */
0523         return tape_34xx_erp_retry(request);
0524     case 0x2b:
0525         /*
0526          * Environmental data present. Indicates either unload
0527          * completed ok or read buffered log command completed ok.
0528          */
0529         if (request->op == TO_RUN) {
0530             /* Rewind unload completed ok. */
0531             tape_med_state_set(device, MS_UNLOADED);
0532             return tape_34xx_erp_succeeded(request);
0533         }
0534         /* tape_34xx doesn't use read buffered log commands. */
0535         return tape_34xx_erp_bug(device, request, irb, sense[3]);
0536     case 0x2c:
0537         /*
0538          * Permanent equipment check. CU has tried recovery, but
0539          * did not succeed.
0540          */
0541         return tape_34xx_erp_failed(request, -EIO);
0542     case 0x2d:
0543         /* Data security erase failure. */
0544         if (request->op == TO_DSE)
0545             return tape_34xx_erp_failed(request, -EIO);
0546         /* Data security erase failure, but no such command issued. */
0547         return tape_34xx_erp_bug(device, request, irb, sense[3]);
0548     case 0x2e:
0549         /*
0550          * Not capable. This indicates either that the drive fails
0551          * reading the format id mark or that format specified
0552          * is not supported by the drive.
0553          */
0554         dev_warn (&device->cdev->dev, "The tape unit cannot process "
0555             "the tape format\n");
0556         return tape_34xx_erp_failed(request, -EMEDIUMTYPE);
0557     case 0x30:
0558         /* The medium is write protected. */
0559         dev_warn (&device->cdev->dev, "The tape medium is write-"
0560             "protected\n");
0561         return tape_34xx_erp_failed(request, -EACCES);
0562     case 0x32:
0563         // Tension loss. We cannot recover this, it's an I/O error.
0564         dev_warn (&device->cdev->dev, "The tape does not have the "
0565             "required tape tension\n");
0566         return tape_34xx_erp_failed(request, -EIO);
0567     case 0x33:
0568         /*
0569          * Load Failure. The cartridge was not inserted correctly or
0570          * the tape is not threaded correctly.
0571          */
0572         dev_warn (&device->cdev->dev, "The tape unit failed to load"
0573             " the cartridge\n");
0574         tape_34xx_delete_sbid_from(device, 0);
0575         return tape_34xx_erp_failed(request, -EIO);
0576     case 0x34:
0577         /*
0578          * Unload failure. The drive cannot maintain tape tension
0579          * and control tape movement during an unload operation.
0580          */
0581         dev_warn (&device->cdev->dev, "Automatic unloading of the tape"
0582             " cartridge failed\n");
0583         if (request->op == TO_RUN)
0584             return tape_34xx_erp_failed(request, -EIO);
0585         return tape_34xx_erp_bug(device, request, irb, sense[3]);
0586     case 0x35:
0587         /*
0588          * Drive equipment check. One of the following:
0589          * - cu cannot recover from a drive detected error
0590          * - a check code message is shown on drive display
0591          * - the cartridge loader does not respond correctly
0592          * - a failure occurs during an index, load, or unload cycle
0593          */
0594         dev_warn (&device->cdev->dev, "An equipment check has occurred"
0595             " on the tape unit\n");
0596         return tape_34xx_erp_failed(request, -EIO);
0597     case 0x36:
0598         if (device->cdev->id.driver_info == tape_3490)
0599             /* End of data. */
0600             return tape_34xx_erp_failed(request, -EIO);
0601         /* This erpa is reserved for 3480 */
0602         return tape_34xx_erp_bug(device, request, irb, sense[3]);
0603     case 0x37:
0604         /*
0605          * Tape length error. The tape is shorter than reported in
0606          * the beginning-of-tape data.
0607          */
0608         dev_warn (&device->cdev->dev, "The tape information states an"
0609             " incorrect length\n");
0610         return tape_34xx_erp_failed(request, -EIO);
0611     case 0x38:
0612         /*
0613          * Physical end of tape. A read/write operation reached
0614          * the physical end of tape.
0615          */
0616         if (request->op==TO_WRI ||
0617             request->op==TO_DSE ||
0618             request->op==TO_WTM)
0619             return tape_34xx_erp_failed(request, -ENOSPC);
0620         return tape_34xx_erp_failed(request, -EIO);
0621     case 0x39:
0622         /* Backward at Beginning of tape. */
0623         return tape_34xx_erp_failed(request, -EIO);
0624     case 0x3a:
0625         /* Drive switched to not ready. */
0626         dev_warn (&device->cdev->dev, "The tape unit is not ready\n");
0627         return tape_34xx_erp_failed(request, -EIO);
0628     case 0x3b:
0629         /* Manual rewind or unload. This causes an I/O error. */
0630         dev_warn (&device->cdev->dev, "The tape medium has been "
0631             "rewound or unloaded manually\n");
0632         tape_34xx_delete_sbid_from(device, 0);
0633         return tape_34xx_erp_failed(request, -EIO);
0634     case 0x42:
0635         /*
0636          * Degraded mode. A condition that can cause degraded
0637          * performance is detected.
0638          */
0639         dev_warn (&device->cdev->dev, "The tape subsystem is running "
0640             "in degraded mode\n");
0641         return tape_34xx_erp_retry(request);
0642     case 0x43:
0643         /* Drive not ready. */
0644         tape_34xx_delete_sbid_from(device, 0);
0645         tape_med_state_set(device, MS_UNLOADED);
0646         /* Some commands commands are successful even in this case */
0647         if (sense[1] & SENSE_DRIVE_ONLINE) {
0648             switch(request->op) {
0649                 case TO_ASSIGN:
0650                 case TO_UNASSIGN:
0651                 case TO_DIS:
0652                 case TO_NOP:
0653                     return tape_34xx_done(request);
0654                     break;
0655                 default:
0656                     break;
0657             }
0658         }
0659         return tape_34xx_erp_failed(request, -ENOMEDIUM);
0660     case 0x44:
0661         /* Locate Block unsuccessful. */
0662         if (request->op != TO_BLOCK && request->op != TO_LBL)
0663             /* No locate block was issued. */
0664             return tape_34xx_erp_bug(device, request,
0665                          irb, sense[3]);
0666         return tape_34xx_erp_failed(request, -EIO);
0667     case 0x45:
0668         /* The drive is assigned to a different channel path. */
0669         dev_warn (&device->cdev->dev, "The tape unit is already "
0670             "assigned\n");
0671         return tape_34xx_erp_failed(request, -EIO);
0672     case 0x46:
0673         /*
0674          * Drive not on-line. Drive may be switched offline,
0675          * the power supply may be switched off or
0676          * the drive address may not be set correctly.
0677          */
0678         dev_warn (&device->cdev->dev, "The tape unit is not online\n");
0679         return tape_34xx_erp_failed(request, -EIO);
0680     case 0x47:
0681         /* Volume fenced. CU reports volume integrity is lost. */
0682         dev_warn (&device->cdev->dev, "The control unit has fenced "
0683             "access to the tape volume\n");
0684         tape_34xx_delete_sbid_from(device, 0);
0685         return tape_34xx_erp_failed(request, -EIO);
0686     case 0x48:
0687         /* Log sense data and retry request. */
0688         return tape_34xx_erp_retry(request);
0689     case 0x49:
0690         /* Bus out check. A parity check error on the bus was found. */
0691         dev_warn (&device->cdev->dev, "A parity error occurred on the "
0692             "tape bus\n");
0693         return tape_34xx_erp_failed(request, -EIO);
0694     case 0x4a:
0695         /* Control unit erp failed. */
0696         dev_warn (&device->cdev->dev, "I/O error recovery failed on "
0697             "the tape control unit\n");
0698         return tape_34xx_erp_failed(request, -EIO);
0699     case 0x4b:
0700         /*
0701          * CU and drive incompatible. The drive requests micro-program
0702          * patches, which are not available on the CU.
0703          */
0704         dev_warn (&device->cdev->dev, "The tape unit requires a "
0705             "firmware update\n");
0706         return tape_34xx_erp_failed(request, -EIO);
0707     case 0x4c:
0708         /*
0709          * Recovered Check-One failure. Cu develops a hardware error,
0710          * but is able to recover.
0711          */
0712         return tape_34xx_erp_retry(request);
0713     case 0x4d:
0714         if (device->cdev->id.driver_info == tape_3490)
0715             /*
0716              * Resetting event received. Since the driver does
0717              * not support resetting event recovery (which has to
0718              * be handled by the I/O Layer), retry our command.
0719              */
0720             return tape_34xx_erp_retry(request);
0721         /* This erpa is reserved for 3480. */
0722         return tape_34xx_erp_bug(device, request, irb, sense[3]);
0723     case 0x4e:
0724         if (device->cdev->id.driver_info == tape_3490) {
0725             /*
0726              * Maximum block size exceeded. This indicates, that
0727              * the block to be written is larger than allowed for
0728              * buffered mode.
0729              */
0730             dev_warn (&device->cdev->dev, "The maximum block size"
0731                 " for buffered mode is exceeded\n");
0732             return tape_34xx_erp_failed(request, -ENOBUFS);
0733         }
0734         /* This erpa is reserved for 3480. */
0735         return tape_34xx_erp_bug(device, request, irb, sense[3]);
0736     case 0x50:
0737         /*
0738          * Read buffered log (Overflow). CU is running in extended
0739          * buffered log mode, and a counter overflows. This should
0740          * never happen, since we're never running in extended
0741          * buffered log mode.
0742          */
0743         return tape_34xx_erp_retry(request);
0744     case 0x51:
0745         /*
0746          * Read buffered log (EOV). EOF processing occurs while the
0747          * CU is in extended buffered log mode. This should never
0748          * happen, since we're never running in extended buffered
0749          * log mode.
0750          */
0751         return tape_34xx_erp_retry(request);
0752     case 0x52:
0753         /* End of Volume complete. Rewind unload completed ok. */
0754         if (request->op == TO_RUN) {
0755             tape_med_state_set(device, MS_UNLOADED);
0756             tape_34xx_delete_sbid_from(device, 0);
0757             return tape_34xx_erp_succeeded(request);
0758         }
0759         return tape_34xx_erp_bug(device, request, irb, sense[3]);
0760     case 0x53:
0761         /* Global command intercept. */
0762         return tape_34xx_erp_retry(request);
0763     case 0x54:
0764         /* Channel interface recovery (temporary). */
0765         return tape_34xx_erp_retry(request);
0766     case 0x55:
0767         /* Channel interface recovery (permanent). */
0768         dev_warn (&device->cdev->dev, "A channel interface error cannot be"
0769             " recovered\n");
0770         return tape_34xx_erp_failed(request, -EIO);
0771     case 0x56:
0772         /* Channel protocol error. */
0773         dev_warn (&device->cdev->dev, "A channel protocol error "
0774             "occurred\n");
0775         return tape_34xx_erp_failed(request, -EIO);
0776     case 0x57:
0777         /*
0778          * 3480: Attention intercept.
0779          * 3490: Global status intercept.
0780          */
0781         return tape_34xx_erp_retry(request);
0782     case 0x5a:
0783         /*
0784          * Tape length incompatible. The tape inserted is too long,
0785          * which could cause damage to the tape or the drive.
0786          */
0787         dev_warn (&device->cdev->dev, "The tape unit does not support "
0788             "the tape length\n");
0789         return tape_34xx_erp_failed(request, -EIO);
0790     case 0x5b:
0791         /* Format 3480 XF incompatible */
0792         if (sense[1] & SENSE_BEGINNING_OF_TAPE)
0793             /* The tape will get overwritten. */
0794             return tape_34xx_erp_retry(request);
0795         dev_warn (&device->cdev->dev, "The tape unit does not support"
0796             " format 3480 XF\n");
0797         return tape_34xx_erp_failed(request, -EIO);
0798     case 0x5c:
0799         /* Format 3480-2 XF incompatible */
0800         dev_warn (&device->cdev->dev, "The tape unit does not support tape "
0801             "format 3480-2 XF\n");
0802         return tape_34xx_erp_failed(request, -EIO);
0803     case 0x5d:
0804         /* Tape length violation. */
0805         dev_warn (&device->cdev->dev, "The tape unit does not support"
0806             " the current tape length\n");
0807         return tape_34xx_erp_failed(request, -EMEDIUMTYPE);
0808     case 0x5e:
0809         /* Compaction algorithm incompatible. */
0810         dev_warn (&device->cdev->dev, "The tape unit does not support"
0811             " the compaction algorithm\n");
0812         return tape_34xx_erp_failed(request, -EMEDIUMTYPE);
0813 
0814         /* The following erpas should have been covered earlier. */
0815     case 0x23: /* Read data check. */
0816     case 0x25: /* Write data check. */
0817     case 0x26: /* Data check (read opposite). */
0818     case 0x28: /* Write id mark check. */
0819     case 0x31: /* Tape void. */
0820     case 0x40: /* Overrun error. */
0821     case 0x41: /* Record sequence error. */
0822         /* All other erpas are reserved for future use. */
0823     default:
0824         return tape_34xx_erp_bug(device, request, irb, sense[3]);
0825     }
0826 }
0827 
0828 /*
0829  * 3480/3490 interrupt handler
0830  */
0831 static int
0832 tape_34xx_irq(struct tape_device *device, struct tape_request *request,
0833           struct irb *irb)
0834 {
0835     if (request == NULL)
0836         return tape_34xx_unsolicited_irq(device, irb);
0837 
0838     if ((irb->scsw.cmd.dstat & DEV_STAT_UNIT_EXCEP) &&
0839         (irb->scsw.cmd.dstat & DEV_STAT_DEV_END) &&
0840         (request->op == TO_WRI)) {
0841         /* Write at end of volume */
0842         return tape_34xx_erp_failed(request, -ENOSPC);
0843     }
0844 
0845     if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK)
0846         return tape_34xx_unit_check(device, request, irb);
0847 
0848     if (irb->scsw.cmd.dstat & DEV_STAT_DEV_END) {
0849         /*
0850          * A unit exception occurs on skipping over a tapemark block.
0851          */
0852         if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_EXCEP) {
0853             if (request->op == TO_BSB || request->op == TO_FSB)
0854                 request->rescnt++;
0855             else
0856                 DBF_EVENT(5, "Unit Exception!\n");
0857         }
0858         return tape_34xx_done(request);
0859     }
0860 
0861     DBF_EVENT(6, "xunknownirq\n");
0862     tape_dump_sense_dbf(device, request, irb);
0863     return TAPE_IO_STOP;
0864 }
0865 
0866 /*
0867  * ioctl_overload
0868  */
0869 static int
0870 tape_34xx_ioctl(struct tape_device *device, unsigned int cmd, unsigned long arg)
0871 {
0872     if (cmd == TAPE390_DISPLAY) {
0873         struct display_struct disp;
0874 
0875         if (copy_from_user(&disp, (char __user *) arg, sizeof(disp)) != 0)
0876             return -EFAULT;
0877 
0878         return tape_std_display(device, &disp);
0879     } else
0880         return -EINVAL;
0881 }
0882 
0883 static inline void
0884 tape_34xx_append_new_sbid(struct tape_34xx_block_id bid, struct list_head *l)
0885 {
0886     struct tape_34xx_sbid * new_sbid;
0887 
0888     new_sbid = kmalloc(sizeof(*new_sbid), GFP_ATOMIC);
0889     if (!new_sbid)
0890         return;
0891 
0892     new_sbid->bid = bid;
0893     list_add(&new_sbid->list, l);
0894 }
0895 
0896 /*
0897  * Build up the search block ID list. The block ID consists of a logical
0898  * block number and a hardware specific part. The hardware specific part
0899  * helps the tape drive to speed up searching for a specific block.
0900  */
0901 static void
0902 tape_34xx_add_sbid(struct tape_device *device, struct tape_34xx_block_id bid)
0903 {
0904     struct list_head *  sbid_list;
0905     struct tape_34xx_sbid * sbid;
0906     struct list_head *  l;
0907 
0908     /*
0909      * immediately return if there is no list at all or the block to add
0910      * is located in segment 1 of wrap 0 because this position is used
0911      * if no hardware position data is supplied.
0912      */
0913     sbid_list = (struct list_head *) device->discdata;
0914     if (!sbid_list || (bid.segment < 2 && bid.wrap == 0))
0915         return;
0916 
0917     /*
0918      * Search the position where to insert the new entry. Hardware
0919      * acceleration uses only the segment and wrap number. So we
0920      * need only one entry for a specific wrap/segment combination.
0921      * If there is a block with a lower number but the same hard-
0922      * ware position data we just update the block number in the
0923      * existing entry.
0924      */
0925     list_for_each(l, sbid_list) {
0926         sbid = list_entry(l, struct tape_34xx_sbid, list);
0927 
0928         if (
0929             (sbid->bid.segment == bid.segment) &&
0930             (sbid->bid.wrap    == bid.wrap)
0931         ) {
0932             if (bid.block < sbid->bid.block)
0933                 sbid->bid = bid;
0934             else return;
0935             break;
0936         }
0937 
0938         /* Sort in according to logical block number. */
0939         if (bid.block < sbid->bid.block) {
0940             tape_34xx_append_new_sbid(bid, l->prev);
0941             break;
0942         }
0943     }
0944     /* List empty or new block bigger than last entry. */
0945     if (l == sbid_list)
0946         tape_34xx_append_new_sbid(bid, l->prev);
0947 
0948     DBF_LH(4, "Current list is:\n");
0949     list_for_each(l, sbid_list) {
0950         sbid = list_entry(l, struct tape_34xx_sbid, list);
0951         DBF_LH(4, "%d:%03d@%05d\n",
0952             sbid->bid.wrap,
0953             sbid->bid.segment,
0954             sbid->bid.block
0955         );
0956     }
0957 }
0958 
0959 /*
0960  * Delete all entries from the search block ID list that belong to tape blocks
0961  * equal or higher than the given number.
0962  */
0963 static void
0964 tape_34xx_delete_sbid_from(struct tape_device *device, int from)
0965 {
0966     struct list_head *  sbid_list;
0967     struct tape_34xx_sbid * sbid;
0968     struct list_head *  l;
0969     struct list_head *  n;
0970 
0971     sbid_list = (struct list_head *) device->discdata;
0972     if (!sbid_list)
0973         return;
0974 
0975     list_for_each_safe(l, n, sbid_list) {
0976         sbid = list_entry(l, struct tape_34xx_sbid, list);
0977         if (sbid->bid.block >= from) {
0978             DBF_LH(4, "Delete sbid %d:%03d@%05d\n",
0979                 sbid->bid.wrap,
0980                 sbid->bid.segment,
0981                 sbid->bid.block
0982             );
0983             list_del(l);
0984             kfree(sbid);
0985         }
0986     }
0987 }
0988 
0989 /*
0990  * Merge hardware position data into a block id.
0991  */
0992 static void
0993 tape_34xx_merge_sbid(
0994     struct tape_device *        device,
0995     struct tape_34xx_block_id * bid
0996 ) {
0997     struct tape_34xx_sbid * sbid;
0998     struct tape_34xx_sbid * sbid_to_use;
0999     struct list_head *  sbid_list;
1000     struct list_head *  l;
1001 
1002     sbid_list = (struct list_head *) device->discdata;
1003     bid->wrap    = 0;
1004     bid->segment = 1;
1005 
1006     if (!sbid_list || list_empty(sbid_list))
1007         return;
1008 
1009     sbid_to_use = NULL;
1010     list_for_each(l, sbid_list) {
1011         sbid = list_entry(l, struct tape_34xx_sbid, list);
1012 
1013         if (sbid->bid.block >= bid->block)
1014             break;
1015         sbid_to_use = sbid;
1016     }
1017     if (sbid_to_use) {
1018         bid->wrap    = sbid_to_use->bid.wrap;
1019         bid->segment = sbid_to_use->bid.segment;
1020         DBF_LH(4, "Use %d:%03d@%05d for %05d\n",
1021             sbid_to_use->bid.wrap,
1022             sbid_to_use->bid.segment,
1023             sbid_to_use->bid.block,
1024             bid->block
1025         );
1026     }
1027 }
1028 
1029 static int
1030 tape_34xx_setup_device(struct tape_device * device)
1031 {
1032     int         rc;
1033     struct list_head *  discdata;
1034 
1035     DBF_EVENT(6, "34xx device setup\n");
1036     if ((rc = tape_std_assign(device)) == 0) {
1037         if ((rc = tape_34xx_medium_sense(device)) != 0) {
1038             DBF_LH(3, "34xx medium sense returned %d\n", rc);
1039         }
1040     }
1041     discdata = kmalloc(sizeof(struct list_head), GFP_KERNEL);
1042     if (discdata) {
1043             INIT_LIST_HEAD(discdata);
1044             device->discdata = discdata;
1045     }
1046 
1047     return rc;
1048 }
1049 
1050 static void
1051 tape_34xx_cleanup_device(struct tape_device *device)
1052 {
1053     tape_std_unassign(device);
1054     
1055     if (device->discdata) {
1056         tape_34xx_delete_sbid_from(device, 0);
1057         kfree(device->discdata);
1058         device->discdata = NULL;
1059     }
1060 }
1061 
1062 
1063 /*
1064  * MTTELL: Tell block. Return the number of block relative to current file.
1065  */
1066 static int
1067 tape_34xx_mttell(struct tape_device *device, int mt_count)
1068 {
1069     struct {
1070         struct tape_34xx_block_id   cbid;
1071         struct tape_34xx_block_id   dbid;
1072     } __attribute__ ((packed)) block_id;
1073     int rc;
1074 
1075     rc = tape_std_read_block_id(device, (__u64 *) &block_id);
1076     if (rc)
1077         return rc;
1078 
1079     tape_34xx_add_sbid(device, block_id.cbid);
1080     return block_id.cbid.block;
1081 }
1082 
1083 /*
1084  * MTSEEK: seek to the specified block.
1085  */
1086 static int
1087 tape_34xx_mtseek(struct tape_device *device, int mt_count)
1088 {
1089     struct tape_request *request;
1090     struct tape_34xx_block_id * bid;
1091 
1092     if (mt_count > 0x3fffff) {
1093         DBF_EXCEPTION(6, "xsee parm\n");
1094         return -EINVAL;
1095     }
1096     request = tape_alloc_request(3, 4);
1097     if (IS_ERR(request))
1098         return PTR_ERR(request);
1099 
1100     /* setup ccws */
1101     request->op = TO_LBL;
1102     bid         = (struct tape_34xx_block_id *) request->cpdata;
1103     bid->format = (*device->modeset_byte & 0x08) ?
1104             TAPE34XX_FMT_3480_XF : TAPE34XX_FMT_3480;
1105     bid->block  = mt_count;
1106     tape_34xx_merge_sbid(device, bid);
1107 
1108     tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
1109     tape_ccw_cc(request->cpaddr + 1, LOCATE, 4, request->cpdata);
1110     tape_ccw_end(request->cpaddr + 2, NOP, 0, NULL);
1111 
1112     /* execute it */
1113     return tape_do_io_free(device, request);
1114 }
1115 
1116 /*
1117  * List of 3480/3490 magnetic tape commands.
1118  */
1119 static tape_mtop_fn tape_34xx_mtop[TAPE_NR_MTOPS] = {
1120     [MTRESET]    = tape_std_mtreset,
1121     [MTFSF]      = tape_std_mtfsf,
1122     [MTBSF]      = tape_std_mtbsf,
1123     [MTFSR]      = tape_std_mtfsr,
1124     [MTBSR]      = tape_std_mtbsr,
1125     [MTWEOF]     = tape_std_mtweof,
1126     [MTREW]      = tape_std_mtrew,
1127     [MTOFFL]     = tape_std_mtoffl,
1128     [MTNOP]      = tape_std_mtnop,
1129     [MTRETEN]    = tape_std_mtreten,
1130     [MTBSFM]     = tape_std_mtbsfm,
1131     [MTFSFM]     = tape_std_mtfsfm,
1132     [MTEOM]      = tape_std_mteom,
1133     [MTERASE]    = tape_std_mterase,
1134     [MTRAS1]     = NULL,
1135     [MTRAS2]     = NULL,
1136     [MTRAS3]     = NULL,
1137     [MTSETBLK]   = tape_std_mtsetblk,
1138     [MTSETDENSITY]   = NULL,
1139     [MTSEEK]     = tape_34xx_mtseek,
1140     [MTTELL]     = tape_34xx_mttell,
1141     [MTSETDRVBUFFER] = NULL,
1142     [MTFSS]      = NULL,
1143     [MTBSS]      = NULL,
1144     [MTWSM]      = NULL,
1145     [MTLOCK]     = NULL,
1146     [MTUNLOCK]   = NULL,
1147     [MTLOAD]     = tape_std_mtload,
1148     [MTUNLOAD]   = tape_std_mtunload,
1149     [MTCOMPRESSION]  = tape_std_mtcompression,
1150     [MTSETPART]  = NULL,
1151     [MTMKPART]   = NULL
1152 };
1153 
1154 /*
1155  * Tape discipline structure for 3480 and 3490.
1156  */
1157 static struct tape_discipline tape_discipline_34xx = {
1158     .owner = THIS_MODULE,
1159     .setup_device = tape_34xx_setup_device,
1160     .cleanup_device = tape_34xx_cleanup_device,
1161     .process_eov = tape_std_process_eov,
1162     .irq = tape_34xx_irq,
1163     .read_block = tape_std_read_block,
1164     .write_block = tape_std_write_block,
1165     .ioctl_fn = tape_34xx_ioctl,
1166     .mtop_array = tape_34xx_mtop
1167 };
1168 
1169 static struct ccw_device_id tape_34xx_ids[] = {
1170     { CCW_DEVICE_DEVTYPE(0x3480, 0, 0x3480, 0), .driver_info = tape_3480},
1171     { CCW_DEVICE_DEVTYPE(0x3490, 0, 0x3490, 0), .driver_info = tape_3490},
1172     { /* end of list */ },
1173 };
1174 
1175 static int
1176 tape_34xx_online(struct ccw_device *cdev)
1177 {
1178     return tape_generic_online(
1179         dev_get_drvdata(&cdev->dev),
1180         &tape_discipline_34xx
1181     );
1182 }
1183 
1184 static struct ccw_driver tape_34xx_driver = {
1185     .driver = {
1186         .name = "tape_34xx",
1187         .owner = THIS_MODULE,
1188     },
1189     .ids = tape_34xx_ids,
1190     .probe = tape_generic_probe,
1191     .remove = tape_generic_remove,
1192     .set_online = tape_34xx_online,
1193     .set_offline = tape_generic_offline,
1194     .int_class = IRQIO_TAP,
1195 };
1196 
1197 static int
1198 tape_34xx_init (void)
1199 {
1200     int rc;
1201 
1202     TAPE_DBF_AREA = debug_register ( "tape_34xx", 2, 2, 4*sizeof(long));
1203     debug_register_view(TAPE_DBF_AREA, &debug_sprintf_view);
1204 #ifdef DBF_LIKE_HELL
1205     debug_set_level(TAPE_DBF_AREA, 6);
1206 #endif
1207 
1208     DBF_EVENT(3, "34xx init\n");
1209     /* Register driver for 3480/3490 tapes. */
1210     rc = ccw_driver_register(&tape_34xx_driver);
1211     if (rc)
1212         DBF_EVENT(3, "34xx init failed\n");
1213     else
1214         DBF_EVENT(3, "34xx registered\n");
1215     return rc;
1216 }
1217 
1218 static void
1219 tape_34xx_exit(void)
1220 {
1221     ccw_driver_unregister(&tape_34xx_driver);
1222 
1223     debug_unregister(TAPE_DBF_AREA);
1224 }
1225 
1226 MODULE_DEVICE_TABLE(ccw, tape_34xx_ids);
1227 MODULE_AUTHOR("(C) 2001-2002 IBM Deutschland Entwicklung GmbH");
1228 MODULE_DESCRIPTION("Linux on zSeries channel attached 3480 tape device driver");
1229 MODULE_LICENSE("GPL");
1230 
1231 module_init(tape_34xx_init);
1232 module_exit(tape_34xx_exit);