root/drivers/char/tpqic02.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. byte_swap_w
  2. ifc_init
  3. report_exception
  4. decode_exception_nr
  5. report_error
  6. handle_exception
  7. is_exception
  8. tape_reset
  9. notify_cmd
  10. wait_for_ready
  11. send_qic02_data
  12. send_qic02_cmd
  13. rdstatus
  14. get_status
  15. get_ext_status3
  16. tp_sense
  17. wait_for_rewind
  18. ll_do_qic_cmd
  19. terminate_read
  20. terminate_write
  21. finish_rw
  22. do_qic_cmd
  23. do_ioctl_cmd
  24. dma_transfer
  25. start_dma
  26. end_dma
  27. qic02_tape_times_out
  28. qic02_tape_interrupt
  29. qic02_tape_lseek
  30. qic02_tape_read
  31. qic02_tape_write
  32. qic02_tape_open
  33. qic02_tape_release
  34. update_ifc_masks
  35. qic02_tape_ioctl
  36. align_buffer
  37. qic02_release_resources
  38. qic02_get_resources
  39. qic02_tape_init

   1 /* $Id: tpqic02.c,v 0.4.1.5 1994/10/29 02:46:13 root Exp root $
   2  *
   3  * Driver for tape drive support for Linux-i386 1.1.58
   4  *
   5  * Copyright (c) 1992, 1993, 1994 by H. H. Bergman. All rights reserved.
   6  * Current e-mail address: hennus@sky.ow.org [This is a UUCP link.]
   7  * [If you are unable to reach me directly, try the TAPE mailing list
   8  * channel on linux-activists@niksula.hut.fi using "X-Mn-Key: TAPE" as
   9  * the first line in your message.]
  10  *
  11  * Distribution of this program in executable form is only allowed if
  12  * all of the corresponding source files are made available through the same
  13  * medium at no extra cost.
  14  *
  15  * I will not accept any responsibility for damage caused directly or
  16  * indirectly by this program, or code derived from this program.
  17  *
  18  * Use this code at your own risk. Don't blame me if it destroys your data!
  19  * Make sure you have a backup before you try this code.
  20  *
  21  * If you make changes to my code and redistribute it in source or binary
  22  * form you must make it clear to even casual users of your code that you
  23  * have modified my code, clearly point out what the changes exactly are
  24  * (preferably in the form of a context diff file), how to undo your changes,
  25  * where the original can be obtained, and that complaints/requests about the
  26  * modified code should be directed to you instead of me.
  27  *
  28  * This driver was partially inspired by the 'wt' driver in the 386BSD
  29  * source distribution, which carries the following copyright notice:
  30  *
  31  *  Copyright (c) 1991 The Regents of the University of California.
  32  *  All rights reserved.
  33  *
  34  * You are not allowed to change this line nor the text above.
  35  *
  36  * $Log: tpqic02.c,v $
  37  * Revision 0.4.1.5  1994/10/29  02:46:13  root
  38  * Minor cleanups.
  39  *
  40  * Revision 0.4.1.4  1994/07/21  02:15:45  root
  41  * ifdef'd DDI. Exception masks.
  42  *
  43  * Revision 0.4.1.3  1994/05/03  01:49:09  root
  44  * Initial attempt at Mountain support for the Mountain 7150.
  45  * Based on patches provided by Erik Jacobson.
  46  *
  47  * Revision 0.4.1.2  1994/03/18  21:16:50  root
  48  * Many driver messages can now be turned off (runtime selectable).
  49  *
  50  * Revision 0.4.1.1  1994/02/16  19:47:22  root
  51  * First stab at runtime debug-variable.
  52  *
  53  * Revision 0.4  1994/02/15  01:53:16  root
  54  * DYNCONF mark II.
  55  * Minor cleanups.
  56  *
  57  * Revision 0.3  1994/02/07  01:23:16  root
  58  * More improved DYNCONF.
  59  * Archive changes & some cleanups by Eddy Olk.
  60  * Removed status_open, more cleanups, misc other.
  61  *
  62  * Revision 0.2.1.25  1994/01/24  02:01:33  root
  63  * Changed tape_qic02 to QIC02_TAPE.
  64  * Changes to prepare for DYNCONF.
  65  *
  66  * Revision 0.2.1.24  1994/01/23  07:27:18  root
  67  * Attempt to remove compilation warnings, G++ bug,
  68  * Linus changed TAPE_QIC02 to QIC02_TAPE.
  69  *
  70  * Revision 0.2.1.23  1994/01/20  23:49:28  root
  71  * Changed some exception decoding stuff.
  72  * TP_HAVE_SEEK, TP_HAVE_DENS. byte_swap_w() on arg, not global.
  73  * Attempt to fix cartridge-changed-problem for 2150L.
  74  * Release irq and dma reservations if initial reset fails.
  75  *
  76  * Revision 0.2.1.22  1994/01/19  20:56:55  root
  77  * Speed measuring stuff moved from aperf.h to delay.h.
  78  * BogoMips (tm) introduced by Linus.
  79  *
  80  * Revision 0.2.1.21  1993/06/18  19:04:33  root
  81  * minor fixes for 0.99.10.
  82  *
  83  * Revision 0.2.1.20  1993/06/11  21:38:51  root
  84  * Added exception code for status 0x8000 (Cypher weirdness).
  85  *
  86  * Revision 0.2.1.19  1993/04/19  23:13:59  root
  87  * Cleanups. Changed to 0.99.8.
  88  *
  89  * Revision 0.2.1.18  1993/03/22  17:39:47  root
  90  * Moved to 0.99.7. Added Archive MTSEEK and MTTELL support.
  91  *
  92  * Revision 0.2.1.17  1993/03/08  18:51:59  root
  93  * Tried to `fix' write-once bug in previous release.
  94  *
  95  * Revision 0.2.1.16  1993/03/01  00:06:16  root
  96  * Use register_chrdev() for 0.99.6.
  97  *
  98  * Revision 0.2.1.15  1993/02/25  00:14:25  root
  99  * minor cleanups.
 100  *
 101  * Revision 0.2.1.14  1993/01/25  00:06:14  root
 102  * Kernel udelay. Eof fixups.
 103  * Removed report_ read/write dummies; have strace(1) now.
 104  *
 105  * Revision 0.2.1.13  1993/01/10  02:24:43  root
 106  * Rewrote wait_for_ready() to use newer schedule() features.
 107  * This improves performance for rewinds etc.
 108  *
 109  * Revision 0.2.1.12  1993/01/05  18:44:09  root
 110  * Changes for 0.99.1. Fixed `restartable reads'.
 111  *
 112  * Revision 0.2.1.11  1992/11/28  01:19:10  root
 113  * Changes to exception handling (significant).
 114  * Changed returned error codes. Hopefully they're correct now.
 115  * Changed declarations to please gcc-2.3.1.
 116  * Patch to deal with bogus interrupts for Archive cards.
 117  *
 118  * Revision 0.2.1.10  1992/10/28  00:50:44  root
 119  * underrun/error counter needed byte swapping.
 120  *
 121  * Revision 0.2.1.9  1992/10/15  17:06:01  root
 122  * Removed online() stuff. Changed EOF handling.
 123  *
 124  * Revision 0.2.1.8  1992/10/02  22:25:48  root
 125  * Removed `no_sleep' parameters (got usleep() now),
 126  * cleaned up some comments.
 127  *
 128  * Revision 0.2.1.7  1992/09/27  01:41:55  root
 129  * Changed write() to do entire user buffer in one go, rather than just
 130  * a kernel-buffer sized portion each time.
 131  *
 132  * Revision 0.2.1.6  1992/09/21  02:15:30  root
 133  * Introduced udelay() function for microsecond-delays.
 134  * Trying to use get_dma_residue rather than TC flags.
 135  * Patch to fill entire user buffer on reads before
 136  * returning.
 137  *
 138  * Revision 0.2.1.5  1992/09/19  02:31:28  root
 139  * Some changes based on patches by Eddy Olk to
 140  * support Archive SC402/SC499R controller cards.
 141  *
 142  * Revision 0.2.1.4  1992/09/07  01:37:37  root
 143  * Minor changes
 144  *
 145  * Revision 0.2.1.3  1992/08/13  00:11:02  root
 146  * Added some support for Archive SC402 and SC499 cards.
 147  * (Untested.)
 148  *
 149  * Revision 0.2.1.2  1992/08/10  02:02:36  root
 150  * Changed from linux/system.h macros to asm/dma.h inline functions.
 151  *
 152  * Revision 0.2.1.1  1992/08/08  01:12:39  root
 153  * cleaned up a bit. added stuff for selftesting.
 154  * preparing for asm/dma.h instead of linux/system.h
 155  *
 156  * Revision 0.2  1992/08/03  20:11:30  root
 157  * Changed to use new IRQ allocation. Padding now done at runtime, pads to
 158  * 512 bytes. Because of this the page regs must be re-programmed every
 159  * block! Added hooks for selftest commands.
 160  * Moved to linux-0.97.
 161  *
 162  * Revision 0.1.0.5  1992/06/22  22:20:30  root
 163  * moved to Linux 0.96b
 164  *
 165  * Revision 0.1.0.4  1992/06/18  02:00:04  root
 166  * Use minor bit-7 to enable/disable printing of extra debugging info
 167  * when do tape access.
 168  * Added semop stuff for DMA/IRQ allocation checking. Don't think this
 169  * is the right way to do it though.
 170  *
 171  * Revision 0.1.0.3  1992/06/01  01:57:34  root
 172  * changed DRQ to DMA. added TDEBUG ifdefs to reduce output.
 173  *
 174  * Revision 0.1.0.2  1992/05/31  14:02:38  root
 175  * changed SET_DMA_PAGE handling slightly.
 176  *
 177  * Revision 0.1.0.1  1992/05/27  12:12:03  root
 178  * Can now use multiple files on tape (sort of).
 179  * First release.
 180  *
 181  * Revision 0.1  1992/05/26  01:16:31  root
 182  * Initial version. Copyright H. H. Bergman 1992
 183  *
 184  */
 185 
 186 /* After the legalese, now the important bits:
 187  * 
 188  * This is a driver for the Wangtek 5150 tape drive with 
 189  * a QIC-02 controller for ISA-PC type computers.
 190  * Hopefully it will work with other QIC-02 tape drives as well.
 191  *
 192  * Make sure your setup matches the configuration parameters.
 193  * Also, be careful to avoid IO conflicts with other devices!
 194  */
 195 
 196 
 197 /*
 198 #define TDEBUG
 199 */
 200 
 201 #define REALLY_SLOW_IO          /* it sure is ... */
 202 
 203 #include <linux/sched.h>
 204 #include <linux/timer.h>
 205 #include <linux/fs.h>
 206 #include <linux/kernel.h>
 207 #include <linux/major.h>
 208 #include <linux/errno.h>
 209 #include <linux/mtio.h>
 210 #include <linux/fcntl.h>
 211 #include <linux/delay.h>
 212 #include <linux/tpqic02.h>
 213 #include <linux/config.h>
 214 #include <linux/mm.h>
 215 
 216 #include <asm/dma.h>
 217 #include <asm/system.h>
 218 #include <asm/io.h>
 219 #include <asm/segment.h>
 220 
 221 /* We really shouldn't be using this define.. */
 222 #define IOCCMD_MASK 0x0000ffff
 223 
 224 /* check existence of required configuration parameters */
 225 #if !defined(QIC02_CMD_PORT) || \
 226     !defined(QIC02_TAPE_IRQ) || \
 227     !defined(QIC02_TAPE_DMA)
 228 #error qic02_tape configuration error
 229 #endif
 230 
 231 
 232 #define TPQIC02_NAME    "tpqic02"
 233 
 234 /* Linux outb() commands have (value,port) as parameters.
 235  * One might expect (port,value) instead, so beware!
 236  */
 237 
 238 #ifdef CONFIG_QIC02_DYNCONF
 239 /* This holds the dynamic configuration info for the interface
 240  * card+drive info if runtime configuration has been selected.
 241  */
 242 struct mtconfiginfo qic02_tape_dynconf = { 0, };        /* user settable */
 243 struct qic02_ccb qic02_tape_ccb = { 0, };               /* private stuff */
 244 
 245 #else
 246 
 247 unsigned long qic02_tape_debug;
 248 
 249 # if ((QIC02_TAPE_IFC!=WANGTEK) && (QIC02_TAPE_IFC!=ARCHIVE) && (QIC02_TAPE_IFC!=MOUNTAIN))
 250 #  error No valid interface card specified
 251 # endif
 252 #endif
 253 
 254 static volatile int ctlbits = 0;     /* control reg bits for tape interface */
 255 
 256 static struct wait_queue *qic02_tape_transfer = NULL; /* sync rw with interrupts */
 257 
 258 static volatile struct mtget ioctl_status;      /* current generic status */
 259 
 260 static volatile struct tpstatus tperror;        /* last drive status */
 261 
 262 static char rcs_revision[] = "$Revision: 0.4.1.5 $";
 263 static char rcs_date[] = "$Date: 1994/10/29 02:46:13 $";
 264 
 265 /* Flag bits for status and outstanding requests.
 266  * (Could all be put in one bit-field-struct.)
 267  * Some variables need `volatile' because they may be modified
 268  * by an interrupt.
 269  */
 270 static volatile flag status_dead = YES; /* device is legally dead until proven alive */
 271 static          flag status_zombie = YES; /* it's `zombie' until irq/dma allocated */
 272 
 273 static volatile flag status_bytes_wr = NO;      /* write FM at close or not */
 274 static volatile flag status_bytes_rd = NO;      /* (rd|wr) used for rewinding */
 275 
 276 static volatile unsigned long status_cmd_pending = 0; /* cmd in progress */
 277 static volatile flag status_expect_int = NO;    /* ready for interrupts */
 278 static volatile flag status_timer_on = NO;      /* using time-out */
 279 static volatile int  status_error = 0;          /* int handler may detect error */
 280 static volatile flag status_eof_detected = NO;  /* end of file */
 281 static volatile flag status_eom_detected = NO;  /* end of recorded media */
 282 static volatile flag status_eot_detected = NO;  /* end of tape */
 283 static volatile flag doing_read = NO;
 284 static volatile flag doing_write = NO;
 285 
 286 static volatile unsigned long dma_bytes_todo;
 287 static volatile unsigned long dma_bytes_done;
 288 static volatile unsigned dma_mode = 0;          /* !=0 also means DMA in use */
 289 static          flag need_rewind = YES;
 290 
 291 static kdev_t current_tape_dev;
 292 static int extra_blocks_left = BLOCKS_BEYOND_EW;
 293 
 294 
 295 /* return_*_eof:
 296  *      NO:     not at EOF,
 297  *      YES:    tell app EOF was reached (return 0).
 298  *
 299  * return_*_eof==YES && reported_*_eof==NO  ==>
 300  *      return current buffer, next time(s) return EOF.
 301  *
 302  * return_*_eof==YES && reported_*_eof==YES  ==>
 303  *      at EOF and application knows it, so we can
 304  *      move on to the next file.
 305  *
 306  */
 307 static flag return_read_eof = NO;       /* set to signal app EOF was reached */
 308 static flag return_write_eof = NO;
 309 static flag reported_read_eof = NO;     /* set when we've done that */
 310 static flag reported_write_eof = NO;
 311 
 312 
 313 /* This is for doing `mt seek <blocknr>' */
 314 static char seek_addr_buf[AR_SEEK_BUF_SIZE];
 315 
 316 
 317 /* In write mode, we have to write a File Mark after the last block written, 
 318  * when the tape device is closed. Tape repositioning and reading in write
 319  * mode is allowed as long as no actual writing has been done. After writing
 320  * the File Mark, repositioning and reading are allowed again.
 321  */
 322 static int  mode_access;        /* access mode: READ or WRITE */
 323 
 324 
 325 /* This is the actual kernel buffer where the interrupt routines read
 326  * from/write to. It is needed because the DMA channels 1 and 3 cannot
 327  * always access the user buffers. [The kernel buffer must reside in the
 328  * lower 16MBytes of system memory because of the DMA controller.]
 329  * The user must ensure that a large enough buffer is passed to the
 330  * kernel, in order to reduce tape repositioning.
 331  *
 332  * The buffer is 512 bytes larger than expected, because I want to align it
 333  * at 512 bytes, to prevent problems with 64k boundaries.
 334  */
 335 
 336 static volatile char qic02_tape_buf[TPQBUF_SIZE+TAPE_BLKSIZE];
 337 /* A really good compiler would be able to align this at 512 bytes... :-( */
 338 
 339 static unsigned long buffaddr;  /* aligned physical address of buffer */
 340 
 341 
 342 /* This translates minor numbers to the corresponding recording format: */
 343 static const char *format_names[] = {
 344         "not set",      /* for dumb drives unable to handle format selection */
 345         "11",           /* extinct */
 346         "24",
 347         "120",
 348         "150",
 349         "300",          /* untested. */
 350         "600"           /* untested. */
 351 };
 352 
 353 
 354 /* `exception_list' is needed for exception status reporting.
 355  * Exceptions 1..14 are defined by QIC-02 rev F.
 356  * The drive status is matched sequentially to each entry,
 357  * ignoring irrelevant bits, until a match is found. If no
 358  * match is found, exception number 0 is used. (That should of
 359  * course never happen...) The original table was based on the
 360  * "Exception Status Summary" in QIC-02 rev F, but some changes
 361  * were required to make it work with real-world drives.
 362  *
 363  * Exception 2 (CNI) is changed to also cover status 0x00e0 (mask USL),
 364  * Exception 4 (EOM) is changed to also cover status 0x8288 (mask EOR),
 365  * Exception 11 (FIL) is changed to also cover status 0x0089 (mask EOM).
 366  * Exception 15 (EOR) is added for seek-to-end-of-data (catch EOR),
 367  * Exception 16 (BOM) is added for beginning-of-media (catch BOM).
 368  *
 369  * Had to swap EXC_NDRV and EXC_NCART to ensure that extended EXC_NCART
 370  * (because of the incorrect Wangtek status code) doesn't catch the
 371  * EXC_NDRV first.
 372  */
 373 static struct exception_list_type {
 374         unsigned short mask, code;
 375         const char *msg;
 376         /* EXC_nr attribute should match with tpqic02.h */
 377 } exception_list[] = {
 378         {0, 0,
 379                 "Unknown exception status code",                /* extra: 0 */},
 380         {~(0), TP_ST0|TP_CNI|TP_USL|TP_WRP,
 381                 "Drive not online"                              /* 1 */},
 382                 /* Drive presence goes before cartridge presence. */
 383         {~(TP_WRP|TP_USL), TP_ST0|TP_CNI,
 384                 /* My Wangtek 5150EQ sometimes reports a status code
 385                  * of 0x00e0, which is not a valid exception code, but
 386                  * I think it should be recognized as "NO CARTRIDGE".
 387                  */
 388                 "Cartridge not in place"                        /* 2 */},
 389         {(unsigned short) ~(TP_ST1|TP_BOM), (TP_ST0|TP_WRP),
 390                 "Write protected cartridge"                     /* 3 */},
 391         {(unsigned short) ~(TP_ST1|TP_EOR), (TP_ST0|TP_EOM),
 392                 "End of media"                                  /* 4 */},
 393         {~TP_WRP, TP_ST0|TP_UDA| TP_ST1|TP_BOM,
 394                 "Read or Write abort. Rewind tape."             /* 5 */},
 395         {~TP_WRP, TP_ST0|TP_UDA,
 396                 "Read error. Bad block transferred."            /* 6 */},
 397         {~TP_WRP, TP_ST0|TP_UDA|TP_BNL,
 398                 "Read error. Filler block transferred."         /* 7 */},
 399         {~TP_WRP, TP_ST0|TP_UDA|TP_BNL |TP_ST1|TP_NDT,
 400                 "Read error. No data detected."                 /* 8 */},
 401         {~TP_WRP, TP_ST0|TP_EOM|TP_UDA|TP_BNL |TP_ST1|TP_NDT,
 402                 "Read error. No data detected. EOM."            /* 9 */},
 403         {~(TP_WRP|TP_MBD|TP_PAR|TP_EOR), TP_ST0|TP_UDA|TP_BNL |TP_ST1|TP_NDT|TP_BOM,
 404                 "Read error. No data detected. BOM."            /* 10 */},
 405         {~(TP_WRP|TP_EOM), TP_ST0|TP_FIL,
 406                 /* Status 0x0089 (EOM & FM) is viewed as an FM,
 407                  * because it can only happen during a read.
 408                  * EOM is checked separately for an FM condition.
 409                  */
 410                 "File mark detected"                            /* 11 */},
 411         {~(TP_ST0|TP_CNI|TP_USL|TP_WRP|TP_BOM), TP_ST1|TP_ILL,
 412                 "Illegal command"                               /* 12 */},
 413         {~(TP_ST0|TP_CNI|TP_USL|TP_WRP|TP_BOM), TP_ST1|TP_POR,
 414                 "Reset occurred"                                        /* 13 */},
 415         {~TP_WRP, TP_ST0|TP_FIL|TP_MBD,         /* NOTE: ST1 not set! */
 416                 "Marginal block detected"                       /* 14 */},
 417         {~(TP_ST0|TP_WRP|TP_EOM|TP_UDA|TP_BNL|TP_FIL |TP_NDT), TP_ST1|TP_EOR,
 418                 /********** Is the extra TP_NDT really needed Eddy? **********/
 419                 "End of recorded media"                         /* extra: 15 */},
 420                 /* 15 is returned when SEEKEOD completes successfully */
 421         {~(TP_WRP|TP_ST0), TP_ST1|TP_BOM,
 422                 "Beginning of media"                            /* extra: 16 */}
 423 };
 424 #define NR_OF_EXC       (sizeof(exception_list)/sizeof(struct exception_list_type))
 425 
 426 
 427 
 428 static void tpqputs(unsigned long flags, const char *s)
 429 {
 430         if ((flags & TPQD_ALWAYS) || (flags & QIC02_TAPE_DEBUG))
 431                 printk(TPQIC02_NAME ": %s\n", s);
 432 } /* tpqputs */
 433 
 434 
 435 
 436 
 437 /* Perform byte order swapping for a 16-bit word.
 438  *
 439  * [FIXME] This should probably be in include/asm/
 440  * ([FIXME] i486 can do this faster)
 441  */
 442 static inline void byte_swap_w(volatile unsigned short * w)
     /* [previous][next][first][last][top][bottom][index][help] */
 443 {
 444         int t = *w;
 445 
 446         *w = (t>>8) | ((t & 0xff)<<8);
 447 }
 448 
 449 
 450 
 451 /* Init control register bits on interface card.
 452  * For Archive, interrupts must be enabled explicitly.
 453  * Wangtek interface card requires ONLINE to be set, Archive SC402/SC499R
 454  * cards keep it active all the time.
 455  */
 456 static void ifc_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 457 {
 458         if (QIC02_TAPE_IFC == WANGTEK) /* || (QIC02_TAPE_IFC == EVEREX) */ {
 459                 ctlbits = WT_CTL_ONLINE;        /* online */
 460                 outb_p(ctlbits, QIC02_CTL_PORT);
 461 
 462         } else if (QIC02_TAPE_IFC == ARCHIVE) {
 463                 ctlbits = 0;                    /* no interrupts yet */
 464                 outb_p(ctlbits, QIC02_CTL_PORT);
 465                 outb_p(0, AR_RESET_DMA_PORT);   /* dummy write to reset DMA */
 466 
 467         } else /* MOUNTAIN */ {
 468                 ctlbits = MTN_CTL_ONLINE;       /* online, and logic enabled */
 469                 outb_p(ctlbits, QIC02_CTL_PORT);
 470         }
 471 } /* ifc_init */
 472 
 473 
 474 static void report_exception(unsigned n)
     /* [previous][next][first][last][top][bottom][index][help] */
 475 {
 476         if (n >= NR_OF_EXC) { tpqputs(TPQD_ALWAYS, "Oops -- report_exception"); n = 0; }
 477         if (TPQDBG(SENSE_TEXT) || n==0)
 478                 printk(TPQIC02_NAME ": sense: %s\n", exception_list[n].msg);
 479 } /* report_exception */
 480 
 481 
 482 /* Try to map the drive-exception bits `s' to a predefined "exception number",
 483  * by comparing the significant exception bits for each entry in the
 484  * exception table (`exception_list[]').
 485  * It is assumed that s!=0.
 486  */
 487 static int decode_exception_nr(unsigned s)
     /* [previous][next][first][last][top][bottom][index][help] */
 488 {
 489         int i;
 490 
 491         for (i=1; i<NR_OF_EXC; i++) {
 492                 if ((s & exception_list[i].mask)==exception_list[i].code)
 493                         return i;
 494         }
 495         printk(TPQIC02_NAME ": decode_exception_nr: exception(%x) not recognized\n", s);
 496         return 0;
 497 } /* decode_exception_nr */
 498 
 499 
 500 #ifdef OBSOLETE
 501 /* There are exactly 14 possible exceptions, as defined in QIC-02 rev F.
 502  * Some are FATAL, some aren't. Currently all exceptions are treated as fatal.
 503  * Especially 6 and 14 should not abort the transfer. RSN...
 504  * Should probably let sense() figure out the exception number using the code
 505  * below, and just report the error based on the number here, returning a code
 506  * for FATAL/CONTINUABLE.
 507  */
 508 static void report_error(int s)
     /* [previous][next][first][last][top][bottom][index][help] */
 509 {
 510         short n = -1;
 511 
 512         if (s & TP_ST1) {
 513                 if (s & TP_ILL)         /* 12: Illegal command. FATAL */
 514                         n = 12;
 515                 if (s & TP_POR)         /* 13: Reset occurred. FATAL */
 516                         n = 13;
 517         } 
 518         else if (s & TP_ST0) {
 519                 if (s & TP_EOR) {       /* extra: 15: End of Recorded Media. CONTINUABLE */
 520                         n = 15;
 521                         /********** should set flag here **********/
 522                 }
 523                 else if (s & TP_EOM)            /* 4: End Of Media. CONTINUABLE */
 524                         n = 4;
 525                 else if (s & TP_USL)            /* 2: Drive not online. FATAL */
 526                         n = 2;
 527                 else if (s & TP_CNI) {  /* 1: Cartridge not in place. FATAL */
 528                         n = 1;
 529                         need_rewind = YES;
 530                         status_eof_detected = NO;
 531                         status_eom_detected = NO;
 532                 }
 533                 else if (s & TP_UDA) {
 534                         if (s & TP_BNL) {
 535                                 if (s & TP_NDT) {
 536                                         if (s & TP_BOM)         /* 9: Read error. No data detected & EOM. CONTINUABLE */
 537                                                 n = 9;
 538                                         else if (s & TP_EOM)    /* 10: Read error. No data detected & BOM. CONTINUABLE */
 539                                                 n = 10;
 540                                         else                    /* 8: Read error. No data detected. CONTINUABLE */
 541                                                 n = 8;
 542                                 } else { /* 7: Read error. Cannot recover block, filler substituted. CONTINUABLE */
 543                                         tpqputs(TPQD_ALWAYS, "[Bad block -- filler data transferred.]");
 544                                         n = 7;
 545                                 }
 546                         }
 547                         else {
 548                                 if (s & TP_EOM) /* 5: Read or Write error. Rewind tape. FATAL */
 549                                         n = 5;
 550                                 else {          /* 6: Read error. Bad block transferred. CONTINUABLE */
 551                                         /* block is bad, but transfer may continue.
 552                                          * This is why some people prefer not to
 553                                          * use compression on backups...
 554                                          */
 555                                         tpqputs(TPQD_ALWAYS, "[CRC failed!]");
 556                                         n = 6;
 557                                 }
 558                         }
 559                 }
 560                 else if (s & TP_FIL) {
 561                         if (s & TP_MBD) {       /* 14: Marginal block detected. CONTINUABLE */
 562                                 tpqputs(TPQD_ALWAYS, "[Marginal block]");
 563                                 n = 14;
 564                         } else                  /* 11: File mark detected. CONTINUABLE */
 565                                 n = 11;
 566                 }
 567                 else if (s & TP_WRP)            /* 3: Write protected cartridge. FATAL */
 568                         n = 3;
 569         }
 570         if (n >= 0)
 571                 sensemsg(n);
 572 } /* report_error */
 573 #endif
 574 
 575 
 576 /* Perform appropriate action for certain exceptions.
 577  * should return a value to indicate stop/continue (in case of bad blocks)
 578  */
 579 static void handle_exception(int exnr, int exbits)
     /* [previous][next][first][last][top][bottom][index][help] */
 580 {
 581         if (exnr==EXC_NCART) {
 582                 /* Cartridge was changed. Redo sense().
 583                  * EXC_NCART should be handled in open().
 584                  * It is not permitted to remove the tape while
 585                  * the tape driver has open files. 
 586                  */
 587                 need_rewind = YES;
 588                 status_eof_detected = NO;
 589                 status_eom_detected = NO;
 590         }
 591         else if (exnr==EXC_XFILLER)
 592                 tpqputs(TPQD_ALWAYS, "[Bad block -- filler data transferred.]");
 593         else if (exnr==EXC_XBAD)
 594                 tpqputs(TPQD_ALWAYS, "[CRC failed!]");
 595         else if (exnr==EXC_MARGINAL) {
 596                 /* A marginal block behaves much like a FM.
 597                  * User may continue reading, if desired.
 598                  */
 599                 tpqputs(TPQD_ALWAYS, "[Marginal block]");
 600                 doing_read = NO;
 601         } else if (exnr==EXC_FM)
 602                 doing_read = NO;
 603 } /* handle_exception */
 604 
 605 
 606 static inline int is_exception(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 607 {
 608         return (inb(QIC02_STAT_PORT) & QIC02_STAT_EXCEPTION) == 0;
 609 } /* is_exception */
 610 
 611 
 612 /* Reset the tape drive and controller.
 613  * When reset fails, it marks  the drive as dead and all
 614  * requests (except reset) are to be ignored (ENXIO).
 615  */
 616 static int tape_reset(int verbose)
     /* [previous][next][first][last][top][bottom][index][help] */
 617 {
 618         ifc_init();                             /* reset interface card */
 619 
 620         /* assert reset */
 621         if (QIC02_TAPE_IFC == MOUNTAIN)
 622                 outb_p(ctlbits & ~MTN_QIC02_CTL_RESET_NOT, QIC02_CTL_PORT);
 623         else /* WANGTEK, ARCHIVE */
 624                 outb_p(ctlbits | QIC02_CTL_RESET, QIC02_CTL_PORT);
 625 
 626         /* Next, we need to wait >=25 usec. */
 627         udelay(30);
 628 
 629         /* after reset, we will be at BOT (modulo an automatic rewind) */
 630         status_eof_detected = NO;
 631         status_eom_detected = NO;
 632         status_cmd_pending = 0;
 633         need_rewind = YES;
 634         doing_read = doing_write = NO;
 635         ioctl_status.mt_fileno = ioctl_status.mt_blkno = 0;
 636 
 637         /* de-assert reset */
 638         if (QIC02_TAPE_IFC == MOUNTAIN)
 639                 outb_p(ctlbits | MTN_QIC02_CTL_RESET_NOT, QIC02_CTL_PORT);
 640         else
 641                 outb_p(ctlbits & ~QIC02_CTL_RESET, QIC02_CTL_PORT);
 642 
 643         /* KLUDGE FOR G++ BUG */
 644         { int stat = inb_p(QIC02_STAT_PORT);
 645           status_dead = ((stat & QIC02_STAT_RESETMASK) != QIC02_STAT_RESETVAL); }
 646         /* if successful, inb(STAT) returned RESETVAL */
 647         if (status_dead == YES)
 648                 printk(TPQIC02_NAME ": reset failed!\n");
 649         else if (verbose)
 650                 printk(TPQIC02_NAME ": reset successful\n");
 651 
 652         return (status_dead == YES)? TE_DEAD : TE_OK;
 653 } /* tape_reset */
 654 
 655 
 656 
 657 /* Notify tape drive of a new command. It only waits for the
 658  * command to be accepted, not for the actual command to complete.
 659  *
 660  * Before calling this routine, QIC02_CMD_PORT must have been loaded
 661  * with the command to be executed.
 662  * After this routine, the exception bit must be checked.
 663  * This routine is also used by rdstatus(), so in that case, any exception
 664  * must be ignored (`ignore_ex' flag).
 665  */
 666 static int notify_cmd(char cmd, short ignore_ex)
     /* [previous][next][first][last][top][bottom][index][help] */
 667 {
 668         int i;
 669 
 670         outb_p(cmd, QIC02_CMD_PORT);    /* output the command */
 671 
 672         /* wait 1 usec before asserting /REQUEST */
 673         udelay(1);
 674 
 675         if ((!ignore_ex) && is_exception()) {
 676                 tpqputs(TPQD_ALWAYS, "*** exception detected in notify_cmd");
 677                 /** force a reset here **/
 678                 if (tape_reset(1)==TE_DEAD)
 679                         return TE_DEAD;
 680                 if (is_exception()) {
 681                         tpqputs(TPQD_ALWAYS, "exception persists after reset.");
 682                         tpqputs(TPQD_ALWAYS, " ^ exception ignored.");
 683                 }
 684         }
 685 
 686         outb_p(ctlbits | QIC02_CTL_REQUEST, QIC02_CTL_PORT);  /* set request bit */
 687         i = TAPE_NOTIFY_TIMEOUT;
 688         /* The specs say this takes about 500 usec, but there is no upper limit!
 689          * If the drive were busy retensioning or something like that,
 690          * it could be *much* longer!
 691          */
 692         while ((inb_p(QIC02_STAT_PORT) & QIC02_STAT_READY) && (--i>0))
 693                 /*skip*/;                         /* wait for ready */
 694         if (i==0) {
 695                 tpqputs(TPQD_ALWAYS, "timed out waiting for ready in notify_cmd");
 696                 status_dead = YES;
 697                 return TE_TIM;
 698         }
 699 
 700         outb_p(ctlbits & ~QIC02_CTL_REQUEST, QIC02_CTL_PORT); /* reset request bit */
 701         i = TAPE_NOTIFY_TIMEOUT;
 702         /* according to the specs this one should never time-out */
 703         while (((inb_p(QIC02_STAT_PORT) & QIC02_STAT_READY) == 0) && (--i>0))
 704                 /*skip*/;                         /* wait for not ready */
 705         if (i==0) {
 706                 tpqputs(TPQD_ALWAYS, "timed out waiting for !ready in notify_cmd");
 707                 status_dead = YES;
 708                 return TE_TIM;
 709         }
 710         /* command accepted */
 711         return TE_OK;
 712 } /* notify_cmd */
 713 
 714 
 715 
 716 /* Wait for a command to complete, with timeout */
 717 static int wait_for_ready(time_t timeout)
     /* [previous][next][first][last][top][bottom][index][help] */
 718 {
 719         int stat;
 720         time_t spin_t;
 721 
 722         /* Wait for ready or exception, without driving the loadavg up too much.
 723          * In most cases, the tape drive already has READY asserted,
 724          * so optimize for that case.
 725          *
 726          * First, busy wait a few usec:
 727          */
 728         spin_t = 50;
 729         while (((stat = inb_p(QIC02_STAT_PORT) & QIC02_STAT_MASK) == QIC02_STAT_MASK) && (--spin_t>0))
 730                 /*SKIP*/;
 731         if ((stat & QIC02_STAT_READY) == 0)
 732                 return TE_OK;                   /* covers 99.99% of all calls */
 733 
 734         /* Then use schedule() a few times */
 735         spin_t = 3;     /* max 0.03 sec busy waiting */
 736         if (spin_t > timeout)
 737                 spin_t = timeout;
 738         timeout -= spin_t;
 739         spin_t += jiffies;
 740 
 741         while (((stat = inb_p(QIC02_STAT_PORT) & QIC02_STAT_MASK) == QIC02_STAT_MASK) && (jiffies<spin_t))
 742                 schedule();             /* don't waste all the CPU time */
 743         if ((stat & QIC02_STAT_READY) == 0)
 744                 return TE_OK;
 745 
 746         /* If we reach this point, we probably need to wait much longer, or
 747          * an exception occurred. Either case is not very time-critical.
 748          * Check the status port only a few times every second.
 749          * A interval of less than 0.10 sec will not be noticed by the user,
 750          * more than 0.40 sec may give noticeable delays.
 751          */
 752         spin_t += timeout;
 753         TPQDEB({printk("wait_for_ready: additional timeout: %d\n", spin_t);})
 754 
 755                 /* not ready and no exception && timeout not expired yet */
 756         while (((stat = inb_p(QIC02_STAT_PORT) & QIC02_STAT_MASK) == QIC02_STAT_MASK) && (jiffies<spin_t)) {
 757                 /* be `nice` to other processes on long operations... */
 758                 current->timeout = jiffies + 3*HZ/10;   /* nap 0.30 sec between checks, */
 759                 current->state = TASK_INTERRUPTIBLE;
 760                 schedule();              /* but could be woken up earlier by signals... */
 761         }
 762 
 763         /* don't use jiffies for this test because it may have changed by now */
 764         if ((stat & QIC02_STAT_MASK) == QIC02_STAT_MASK) {
 765                 tpqputs(TPQD_ALWAYS, "wait_for_ready() timed out");
 766                 return TE_TIM;
 767         }
 768 
 769         if ((stat & QIC02_STAT_EXCEPTION) == 0) {
 770                 tpqputs(TPQD_ALWAYS, "exception detected after waiting_for_ready");
 771                 return TE_EX;
 772         } else {
 773                 return TE_OK;
 774         }
 775 } /* wait_for_ready */
 776 
 777 
 778 
 779 /* Send some data to the drive */
 780 static int send_qic02_data(char sb[], unsigned size, int ignore_ex)
     /* [previous][next][first][last][top][bottom][index][help] */
 781 {
 782         int i, stat;
 783 
 784         for (i=0; i<size; i++) {
 785 
 786                 stat = wait_for_ready(TIM_S);
 787                 if (stat != TE_OK)
 788                         return stat;
 789 
 790                 stat = notify_cmd(sb[i], ignore_ex);
 791                 if (stat != TE_OK)
 792                         return stat;
 793         }
 794         return TE_OK;
 795         
 796 } /* send_qic02_data */
 797 
 798 
 799 /* Send a QIC-02 command (`cmd') to the tape drive, with
 800  * a time-out (`timeout').
 801  * This one is also used by tp_sense(), so we must have
 802  * a flag to disable exception checking (`ignore_ex'). 
 803  *
 804  * On entry, the controller is supposed to be READY.
 805  */
 806 static int send_qic02_cmd(int cmd, time_t timeout, int ignore_ex)
     /* [previous][next][first][last][top][bottom][index][help] */
 807 {
 808         int stat;
 809 
 810         stat = inb_p(QIC02_STAT_PORT);
 811         if ((stat & QIC02_STAT_EXCEPTION) == 0) {       /* if exception */
 812                 tpqputs(TPQD_ALWAYS, "send_qic02_cmd: Exception!");
 813                 return TE_EX;
 814         }
 815         if (stat & QIC02_STAT_READY) {                  /* if not ready */
 816                 tpqputs(TPQD_ALWAYS, "send_qic02_cmd: not Ready!");
 817                 return TE_ERR;
 818         }
 819 
 820         /* assert(ready & !exception) */
 821 
 822         /* Remember current command for later re-use with dma transfers.
 823          * (For reading/writing multiple blocks.)
 824          */
 825         status_cmd_pending = cmd;
 826 
 827         stat = notify_cmd(cmd, ignore_ex); /* tell drive new command was loaded, */
 828                                            /* inherit exception check. */
 829         if (TP_HAVE_SEEK && (cmd == AR_QCMDV_SEEK_BLK)) {
 830                 /* This one needs to send 3 more bytes, MSB first */
 831                 stat = send_qic02_data(seek_addr_buf, sizeof(seek_addr_buf), ignore_ex);
 832         }
 833 
 834         if (stat != TE_OK) {
 835                 tpqputs(TPQD_ALWAYS, "send_qic02_cmd failed");
 836         }
 837         return stat;
 838 } /* send_qic02_cmd */
 839 
 840 
 841 
 842 /* Get drive status. Assume drive is ready or has exception set.
 843  * (or will be in <1000 usec.)
 844  * Extra parameters added because of 'Read Extended Status 3' command.
 845  */
 846 static int rdstatus(char *stp, unsigned size, char qcmd)
     /* [previous][next][first][last][top][bottom][index][help] */
 847 {
 848         int     s, n;
 849         char    *q = stp;
 850 
 851         /* Try to busy-wait a few (700) usec, after that de-schedule.
 852          *
 853          * The problem is, if we don't de-schedule, performance will
 854          * drop to zero when the drive is not responding and if we
 855          * de-schedule immediately, we waste a lot of time because a
 856          * task switch is much longer than we usually have to wait here.
 857          */
 858         n = 1000;       /* 500 is not enough on a 486/33 */
 859         while ((n>0) && ((inb_p(QIC02_STAT_PORT) & QIC02_STAT_MASK) == QIC02_STAT_MASK))
 860                 n--;  /* wait for ready or exception or timeout */
 861         if (n==0) {
 862                 /* n (above) should be chosen such that on your machine
 863                  * you rarely ever see the message below, and it should
 864                  * be small enough to give reasonable response time.]
 865                  */
 866                 tpqputs(TPQD_ALWAYS, "waiting looong in rdstatus() -- drive dead?");
 867                 while ((inb_p(QIC02_STAT_PORT) & QIC02_STAT_MASK) == QIC02_STAT_MASK)
 868                         schedule();
 869                 tpqputs(TPQD_ALWAYS, "finished waiting in rdstatus()");
 870         }
 871 
 872         (void) notify_cmd(qcmd, 1);                     /* send read status command */
 873         /* ignore return code -- should always be ok, STAT may contain 
 874          * exception flag from previous exception which we are trying to clear.
 875          */
 876 
 877         if (TP_DIAGS(current_tape_dev))
 878                 printk(TPQIC02_NAME ": reading status bytes: ");
 879 
 880         for (q=stp; q<stp+size; q++)
 881         {
 882                 do s = inb_p(QIC02_STAT_PORT);
 883                 while ((s & QIC02_STAT_MASK) == QIC02_STAT_MASK);       /* wait for ready or exception */
 884 
 885                 if ((s & QIC02_STAT_EXCEPTION) == 0) {          /* if exception */
 886                         tpqputs(TPQD_ALWAYS, "rdstatus: exception error");
 887                         ioctl_status.mt_erreg = 0;              /* dunno... */
 888                         return TE_NS;                           /* error, shouldn't happen... */
 889                 }
 890 
 891                 *q = inb_p(QIC02_DATA_PORT);                    /* read status byte */
 892 
 893                 if (TP_DIAGS(current_tape_dev))
 894                         printk("[%1d]=0x%x  ", q-stp, (unsigned) (*q) & 0xff);
 895 
 896                 outb_p(ctlbits | QIC02_CTL_REQUEST, QIC02_CTL_PORT);    /* set request */
 897 
 898                 while ((inb_p(QIC02_STAT_PORT) & QIC02_STAT_READY) == 0);       /* wait for not ready */
 899 
 900                 udelay(22);     /* delay >20 usec */
 901 
 902                 outb_p(ctlbits & ~QIC02_CTL_REQUEST, QIC02_CTL_PORT);   /* un-set request */
 903 
 904         }
 905 
 906         /* Specs say we should wait for READY here.
 907          * My drive doesn't seem to need it here yet, but others do?
 908          */
 909         while (inb_p(QIC02_STAT_PORT) & QIC02_STAT_READY)
 910                 /*skip*/;                         /* wait for ready */
 911 
 912         if (TP_DIAGS(current_tape_dev))
 913                 printk("\n");
 914 
 915         return TE_OK;
 916 } /* rdstatus */
 917 
 918 
 919 
 920 /* Get standard status (6 bytes).
 921  * The `.dec' and `.urc' fields are in MSB-first byte-order,
 922  * so they have to be swapped first.
 923  */
 924 static int get_status(volatile struct tpstatus *stp)
     /* [previous][next][first][last][top][bottom][index][help] */
 925 {
 926         int stat = rdstatus((char *) stp, TPSTATSIZE, QCMD_RD_STAT);
 927 #if defined(i386) || defined(i486)
 928         byte_swap_w(&(stp->dec));
 929         byte_swap_w(&(stp->urc));
 930 #else
 931         /* should probably swap status bytes #definition */
 932 #endif
 933         return stat;
 934 } /* get_status */
 935 
 936 
 937 #if 0
 938 /* This fails for my Wangtek drive */
 939 /* get "Extended Status Register 3" (64 bytes)
 940  *
 941  * If the meaning of the returned bytes were known, the MT_TYPE
 942  * identifier could be used to decode them, since they are
 943  * "vendor unique". :-(
 944  */
 945 static int get_ext_status3(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 946 {
 947         char vus[64];   /* vendor unique status */
 948         int stat, i;
 949 
 950         tpqputs(TPQD_ALWAYS, "Attempting to read Extended Status 3...");
 951         stat = rdstatus(vus, sizeof(vus), QCMD_RD_STAT_X3);
 952         if (stat != TE_OK)
 953                 return stat;
 954 
 955         tpqputs(TPQD_ALWAYS, "Returned status bytes:");
 956         for (i=0; i<sizeof(vus); i++) {
 957                 if ( i % 8 == 0 )
 958                         printk("\n" TPQIC02_NAME ": %2d:");
 959                 printk(" %2x", vus[i] & 0xff);
 960         }
 961         printk("\n");
 962 
 963         return TE_OK;
 964 } /* get_ext_status3 */
 965 #endif
 966 
 967 
 968 /* Read drive status and set generic status too.
 969  * NOTE: Once we do a tp_sense(), read/write transfers are killed.
 970  */
 971 static int tp_sense(int ignore)
     /* [previous][next][first][last][top][bottom][index][help] */
 972 {
 973         unsigned err = 0, exnr = 0, gs = 0;
 974         static void finish_rw(int cmd);
 975 
 976         if (TPQDBG(SENSE_TEXT))
 977                 printk(TPQIC02_NAME ": tp_sense(ignore=0x%x) enter\n", ignore);
 978 
 979         /* sense() is not allowed during a read or write cycle */
 980         if (doing_write == YES)
 981                 tpqputs(TPQD_ALWAYS, "Warning: File Mark inserted because of sense() request");
 982         /* The extra test is to avoid calling finish_rw during booting */
 983         if ((doing_read!=NO) || (doing_write!=NO))
 984                 finish_rw(QCMD_RD_STAT);
 985 
 986         if (get_status(&tperror) != TE_OK) {
 987                 tpqputs(TPQD_ALWAYS, "tp_sense: could not read tape drive status");
 988                 return TE_ERR;
 989         }
 990 
 991         err = tperror.exs;      /* get exception status bits */
 992         if (err & (TP_ST0|TP_ST1))
 993                 printk(TPQIC02_NAME ": tp_sense: status: %x, error count: %d, underruns: %d\n",
 994                         tperror.exs, tperror.dec, tperror.urc);
 995         else if ((tperror.dec!=0) || (tperror.urc!=0) || TPQDBG(SENSE_CNTS))
 996                 printk(TPQIC02_NAME ": tp_sense: no hard errors, soft error count: %d, underruns: %d\n",
 997                         tperror.dec, tperror.urc);
 998 
 999         /* Set generic status. HP-UX defines these, but some extra would 
1000          * be useful. Problem is to remain compatible. [Do we want to be
1001          * compatible??]
1002          */
1003         if (err & TP_ST0) {
1004                 if (err & TP_CNI)               /* no cartridge */
1005                         gs |= GMT_DR_OPEN(-1);
1006                 if (status_dead == NO)
1007                         gs |= GMT_ONLINE(-1);   /* always online */
1008                 if (err & TP_USL)               /* not online */
1009                         gs &= ~GMT_ONLINE(-1);
1010                 if (err & TP_WRP)
1011                         gs |= GMT_WR_PROT(-1);
1012                 if (err & TP_EOM) {             /* end of media */
1013                         gs |= GMT_EOT(-1);      /* not sure this is correct for writes */
1014                         status_eom_detected = YES;
1015                         /* I don't know whether drive always reports EOF at or before EOM. */
1016                         status_eof_detected = YES;
1017                 }
1018                 /** if (err & TP_UDA) "Unrecoverable data error" **/
1019                 /** if (err & TP_BNL) "Bad block not located" **/
1020                 if (err & TP_FIL) {
1021                         gs |= GMT_EOF(-1);
1022                         status_eof_detected = YES;
1023                 }
1024         }
1025         if (err & TP_ST1) {
1026                 /** if (err & TP_ILL) "Illegal command" **/
1027                 /** if (err & TP_NDT) "No data detected" **/
1028                 /** if (err & TP_MBD) "Marginal block detected" **/
1029                 if (err & TP_BOM)
1030                         gs |= GMT_BOT(-1);      /* beginning of tape */
1031         }
1032         ioctl_status.mt_gstat = gs;
1033         ioctl_status.mt_dsreg = tperror.exs;    /* "drive status" */
1034         ioctl_status.mt_erreg = tperror.dec;    /* "sense key error" */
1035 
1036         if (err & (TP_ST0|TP_ST1)) {
1037                 /* My Wangtek occasionally reports `status' 1212 which should be ignored. */
1038                 exnr = decode_exception_nr(err);
1039                 handle_exception(exnr, err);            /* update driver state wrt drive status */
1040                 report_exception(exnr);
1041         }
1042         err &= ~ignore;         /* mask unwanted errors -- not the correct way, use exception nrs?? */
1043         if (((err & TP_ST0) && (err & REPORT_ERR0)) ||
1044             ((err & TP_ST1) && (err & REPORT_ERR1)))
1045                 return TE_ERR;
1046         return TE_OK;
1047 } /* tp_sense */
1048 
1049 
1050 
1051 /* Wait for a wind or rewind operation to finish or
1052  * to time-out. (May take very long).
1053  */
1054 static int wait_for_rewind(time_t timeout)
     /* [previous][next][first][last][top][bottom][index][help] */
1055 {
1056         int stat;
1057 
1058         stat = inb(QIC02_STAT_PORT) & QIC02_STAT_MASK;
1059         if (TPQDBG(REWIND))
1060                 printk(TPQIC02_NAME ": Waiting for (re-)wind to finish: stat=0x%x\n", stat);
1061 
1062         stat = wait_for_ready(timeout);
1063 
1064         if (stat != TE_OK) {
1065                         tpqputs(TPQD_ALWAYS, "(re-) winding failed\n");
1066         }
1067         return stat;
1068 } /* wait_for_rewind */
1069 
1070 
1071 
1072 /* Perform a full QIC02 command, and wait for completion,
1073  * check status when done. Complain about exceptions.
1074  *
1075  * This function should return an OS error code when
1076  * something goes wrong, 0 otherwise.
1077  */
1078 static int ll_do_qic_cmd(int cmd, time_t timeout)
     /* [previous][next][first][last][top][bottom][index][help] */
1079 {
1080         int stat;
1081 
1082         if (status_dead == YES) {
1083                 tpqputs(TPQD_ALWAYS, "Drive is dead. Do a `mt reset`.");
1084                 return -ENXIO;                  /* User should do an MTRESET. */
1085         }
1086 
1087         stat = wait_for_ready(timeout);         /* wait for ready or exception */
1088         if (stat == TE_EX) {
1089                 if (tp_sense(TP_WRP|TP_BOM|TP_EOM|TP_FIL)!=TE_OK)
1090                         return -EIO;
1091                 /* else nothing to worry about, I hope */
1092                 stat = TE_OK;
1093         }
1094         if (stat != TE_OK) {
1095                 printk(TPQIC02_NAME ": ll_do_qic_cmd(%x, %ld) failed\n", cmd, (long) timeout);
1096                 return -EIO;
1097         }
1098 
1099 
1100 #if OBSOLETE
1101         /* wait for ready since it may not be active immediately after reading status */
1102         while ((inb_p(QIC02_STAT_PORT) & QIC02_STAT_READY) != 0);
1103 #endif
1104 
1105         stat = send_qic02_cmd(cmd, timeout, 0); /* (checks for exceptions) */
1106 
1107         if (cmd==QCMD_RD_FM) {
1108                 status_eof_detected = NO;
1109                 ioctl_status.mt_fileno++;
1110                 /* Should update block count as well, but can't.
1111                  * Can do a `read address' for some drives, when MTNOP is done.
1112                  */
1113         } else if (cmd==QCMD_WRT_FM) {
1114                 status_eof_detected = NO;
1115                 ioctl_status.mt_fileno++;
1116         } else if ((cmd==QCMD_REWIND) || (cmd==QCMD_ERASE) || (cmd==QCMD_RETEN)) {
1117                 status_eof_detected = NO;
1118                 status_eom_detected = NO;
1119                 status_eot_detected = NO;
1120                 need_rewind = NO;
1121                 ioctl_status.mt_fileno = ioctl_status.mt_blkno = 0;
1122                 extra_blocks_left = BLOCKS_BEYOND_EW;
1123                 return_write_eof = NO;
1124                 return_read_eof = NO;
1125                 reported_read_eof = NO;
1126                 reported_write_eof = NO;
1127         }
1128         /* sense() will set eof/eom as required */
1129         if (stat==TE_EX) {
1130                 if (tp_sense(TP_WRP|TP_BOM|TP_EOM|TP_FIL)!=TE_OK) {
1131                         printk(TPQIC02_NAME ": Exception persist in ll_do_qic_cmd[1](%x, %ld)", cmd, (long) timeout);
1132                         status_dead = YES;
1133                         return -ENXIO;
1134                         /* if rdstatus fails too, we're in trouble */
1135                 }
1136         }
1137         else if (stat!=TE_OK) {
1138                 printk(TPQIC02_NAME ": ll_do_qic_cmd: send_qic02_cmd failed, stat = 0x%x\n", stat);
1139                 return -EIO;    /*** -EIO is probably not always appropriate */
1140         }
1141 
1142 
1143         if (timeout == TIM_R)
1144                 stat = wait_for_rewind(timeout);
1145         else
1146                 stat = wait_for_ready(timeout);
1147 
1148         if (stat==TE_EX) {
1149                 if (tp_sense((cmd==QCMD_SEEK_EOD ?              /*****************************/
1150                       TP_EOR|TP_NDT|TP_UDA|TP_BNL|TP_WRP|TP_BOM|TP_EOM|TP_FIL :
1151                       TP_WRP|TP_BOM|TP_EOM|TP_FIL))!=TE_OK) {
1152                         printk(TPQIC02_NAME ": Exception persist in ll_do_qic_cmd[2](%x, %ld)\n", cmd, (long) timeout);
1153                         if (cmd!=QCMD_RD_FM)
1154                                 status_dead = YES;
1155                         return -ENXIO;
1156                         /* if rdstatus fails too, we're in trouble */
1157                 }
1158         }
1159         else if (stat!=TE_OK) {
1160                 printk(TPQIC02_NAME ": ll_do_qic_cmd %x: wait failed, stat == 0x%x\n", cmd, stat);
1161                 return -EIO;
1162         }
1163         return 0;
1164 } /* ll_do_qic_cmd */
1165 
1166 
1167 /* 
1168  * Problem: What to do when the user cancels a read/write operation
1169  * in-progress?
1170  *
1171  * "Deactivating ONLINE during a READ also causes the"
1172  * "tape to be rewound to BOT." Ditto for WRITEs, except
1173  * a FM is written first. "The host may alternatively terminate
1174  * the READ/WRITE command by issuing a RFM/WFM command."
1175  *
1176  * For READs:
1177  * Neither option will leave the tape positioned where it was.
1178  * Another (better?) solution is to terminate the READ by two
1179  * subsequent sense() operations, the first to stop the current
1180  * READ cycle, the second to clear the `Illegal command' exception,
1181  * because the QIC-02 specs didn't anticipate this. This is
1182  * delayed until actually needed, so a tar listing can be aborted
1183  * by the user and continued later.
1184  * If anybody has a better solution, let me know! [Also, let me
1185  * know if your drive (mine is a Wangtek5150EQ) does not accept
1186  * this sequence for canceling the read-cycle.]
1187  *
1188  * For WRITEs it's simple: Just do a WRITE_FM, leaving the tape
1189  * positioned after the FM.
1190  */
1191 
1192 static void terminate_read(int cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
1193 {
1194         if (doing_read == YES) {
1195                 doing_read = NO;
1196                 if (cmd != QCMD_RD_FM) {
1197                         /* if the command is a RFM, there is no need to do this
1198                          * because a RFM will legally terminate the read-cycle.
1199                          */
1200                         tpqputs(TPQD_ALWAYS, "terminating pending read-cycle");
1201 
1202                         /* I'm not too sure about this part  -- hhb */
1203                         if (QIC02_TAPE_IFC == MOUNTAIN) {
1204                                 /* Mountain reference says can terminate by de-asserting online */
1205                                 ctlbits &= ~MTN_QIC02_CTL_ONLINE;
1206                         }
1207  
1208                         if (tp_sense(TP_FIL|TP_EOM|TP_WRP) != TE_OK) {
1209                                 tpqputs(TPQD_ALWAYS, "finish_rw[read1]: ignore the 2 lines above");
1210                                 if (is_exception()) {
1211                                         if (tp_sense(TP_ILL|TP_FIL|TP_EOM|TP_WRP) != TE_OK)
1212                                                 tpqputs(TPQD_ALWAYS, "finish_rw[read2]: read cycle error");
1213                                 }
1214                         }
1215                 }
1216         }
1217 } /* terminate_read */
1218 
1219 
1220 static void terminate_write(int cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
1221 {
1222         int stat;
1223 
1224         if (doing_write == YES) {
1225                 doing_write = NO;
1226                 /* Finish writing by appending a FileMark at the end. */
1227                 if (cmd != QCMD_WRT_FM) {
1228                         /* finish off write cycle */
1229                         stat = ll_do_qic_cmd(QCMD_WRT_FM, TIM_M);
1230                         if (stat != TE_OK)
1231                                 tpqputs(TPQD_ALWAYS, "Couldn't finish write cycle properly");
1232                         (void) tp_sense(0);
1233                 }
1234                 /* If there is an EOF token waiting to be returned to
1235                  * the (writing) application, discard it now.
1236                  * We could be at EOT, so don't reset return_write_eof.
1237                  */
1238                 reported_write_eof=YES;
1239         }
1240 } /* terminate_write */
1241 
1242 
1243 /* terminate read or write cycle because of command `cmd' */
1244 static void finish_rw(int cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
1245 {
1246         if (wait_for_ready(TIM_S) != TE_OK) {
1247                 tpqputs(TPQD_ALWAYS, "error: drive not ready in finish_rw() !");
1248                 return;
1249         }
1250         terminate_read(cmd);
1251         terminate_write(cmd);
1252 } /* finish_rw */
1253 
1254 
1255 /* Perform a QIC command through ll_do_qic_cmd().
1256  * If necessary, rewind the tape first.
1257  * Return an OS error code if something goes wrong, 0 if all is well.
1258  */
1259 static int do_qic_cmd(int cmd, time_t timeout)
     /* [previous][next][first][last][top][bottom][index][help] */
1260 {
1261         int stat;
1262 
1263 
1264         finish_rw(cmd);
1265 
1266         if (need_rewind) {
1267                 tpqputs(TPQD_REWIND, "Rewinding tape...");
1268                 stat = ll_do_qic_cmd(QCMD_REWIND, TIM_R);
1269                 if (stat != 0) {
1270                         printk(TPQIC02_NAME ": rewind failed in do_qic_cmd(). stat=0x%2x", stat);
1271                         return stat;
1272                 }
1273                 need_rewind = NO;
1274                 if (cmd==QCMD_REWIND)   /* don't wind beyond BOT ;-) */
1275                         return 0;
1276         }
1277 
1278         return ll_do_qic_cmd(cmd, timeout);
1279 } /* do_qic_cmd */
1280 
1281 
1282 /* Not all ioctls are supported for all drives. Some rely on
1283  * optional QIC-02 commands. Check tpqic02.h for configuration.
1284  * Some of these commands may require ONLINE to be active.
1285  */
1286 static int do_ioctl_cmd(int cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
1287 {
1288         int stat;
1289 
1290         /* It is not permitted to read or wind the tape after bytes have
1291          * been written. It is not permitted to write the tape while in
1292          * read mode.
1293          * We try to be kind and allow reading again after writing a FM...
1294          */
1295 
1296         switch (cmd) {
1297                 case MTRESET:
1298                         /* reset verbose */
1299                         return (tape_reset(1)==TE_OK)? 0 : -EIO;
1300 
1301                 case MTFSF:
1302                         tpqputs(TPQD_IOCTLS, "MTFSF forward searching filemark");
1303                         if ((mode_access==WRITE) && status_bytes_wr)
1304                                 return -EACCES;
1305                         return do_qic_cmd(QCMD_RD_FM, TIM_F);
1306 
1307                 case MTBSF:
1308                         if (TP_HAVE_BSF) {
1309                                 tpqputs(TPQD_IOCTLS, "MTBSF backward searching filemark -- optional command");
1310                                 if ((mode_access==WRITE) && status_bytes_wr)
1311                                         return -EACCES;
1312                                 stat = do_qic_cmd(QCMD_RD_FM_BCK, TIM_F);
1313                         } else {
1314                                 stat = -ENXIO;
1315                         }
1316                         status_eom_detected = status_eof_detected = NO;
1317                         return stat;
1318 
1319                 case MTFSR:
1320                         if (TP_HAVE_FSR) { /* This is an optional QIC-02 command */
1321                                 tpqputs(TPQD_IOCTLS, "MTFSR forward space record");
1322                                 if ((mode_access==WRITE) && status_bytes_wr)
1323                                         return -EACCES;
1324                                 stat = do_qic_cmd(QCMD_SPACE_FWD, TIM_F);
1325                         } else {
1326                                 /**** fake it by doing a read data block command? ******/
1327                                 tpqputs(TPQD_IOCTLS, "MTFSR not supported");
1328                                 stat = -ENXIO;
1329                         }
1330                         return stat;
1331 
1332                 case MTBSR:
1333                         if (TP_HAVE_BSR) { /* This is an optional QIC-02 command */
1334                                 /* we need this for appending files with GNU tar!! */
1335                                 tpqputs(TPQD_IOCTLS, "MTFSR backward space record");
1336                                 if ((mode_access==WRITE) && status_bytes_wr)
1337                                         return -EACCES;
1338                                 stat = do_qic_cmd(QCMD_SPACE_BCK, TIM_F);
1339                         } else {
1340                                 tpqputs(TPQD_IOCTLS, "MTBSR not supported");
1341                                 stat = -ENXIO;
1342                         }
1343                         status_eom_detected = status_eof_detected = NO;
1344                         return stat;
1345 
1346                 case MTWEOF:
1347                         tpqputs(TPQD_IOCTLS, "MTWEOF write eof mark");
1348                         /* Plain GNU mt(1) 2.2 uses read-only mode for writing FM. :-( */
1349                         if (mode_access==READ)
1350                                 return -EACCES;
1351 
1352                         /* allow tape movement after writing FM */
1353                         status_bytes_rd = status_bytes_wr;      /* Kludge-O-Matic */
1354                         status_bytes_wr = NO;
1355                         return do_qic_cmd(QCMD_WRT_FM, TIM_M);
1356                         /* not sure what to do with status_bytes when WFM should fail */
1357 
1358                 case MTREW:
1359                         tpqputs(TPQD_IOCTLS, "MTREW rewinding tape");
1360                         if ((mode_access==WRITE) && status_bytes_wr)
1361                                 return -EACCES;
1362                         status_eom_detected = status_eof_detected = NO;
1363                         return do_qic_cmd(QCMD_REWIND, TIM_R);
1364 
1365                 case MTOFFL:
1366                         tpqputs(TPQD_IOCTLS, "MTOFFL rewinding & going offline");
1367                         /* Doing a drive select will clear (unlock) the current drive.
1368                          * But that requires support for multiple drives and locking.
1369                          */
1370                         if ((mode_access==WRITE) && status_bytes_wr)
1371                                 return -EACCES;
1372                         status_eom_detected = status_eof_detected = NO;
1373                         /**** do rewind depending on minor bits??? ***/
1374                         stat = do_qic_cmd(QCMD_REWIND, TIM_R);
1375                         return stat;
1376 
1377                 case MTNOP:
1378                         tpqputs(TPQD_IOCTLS, "MTNOP setting status only");
1379                         /********** should do `read position' for drives that support it **********/
1380                         return (tp_sense(-1)==TE_OK)? 0 : -EIO; /**** check return codes ****/
1381 
1382                 case MTRETEN:
1383                         tpqputs(TPQD_IOCTLS, "MTRETEN retension tape");
1384                         if ((mode_access==WRITE) && status_bytes_wr)
1385                                 return -EACCES;
1386                         status_eom_detected = status_eof_detected = NO;
1387                         return do_qic_cmd(QCMD_RETEN, TIM_R);
1388 
1389                 case MTBSFM:
1390                         /* Think think is like MTBSF, except that
1391                          * we shouldn't skip the FM. Tricky.
1392                          * Maybe use RD_FM_BCK, then do a SPACE_FWD?
1393                          */
1394                         tpqputs(TPQD_IOCTLS, "MTBSFM not supported");
1395                         if ((mode_access==WRITE) && status_bytes_wr)
1396                                 return -EACCES;
1397                         return -ENXIO;
1398 
1399                 case MTFSFM:
1400                         /* I think this is like MTFSF, except that
1401                          * we shouldn't skip the FM. Tricky.
1402                          * Maybe use QCMD_RD_DATA until we get a TP_FIL exception?
1403                          * But then the FM will have been skipped...
1404                          * Maybe use RD_FM, then RD_FM_BCK, but not all
1405                          * drives will support that!
1406                          */
1407                         tpqputs(TPQD_IOCTLS, "MTFSFM not supported");
1408                         if ((mode_access==WRITE) && status_bytes_wr)
1409                                 return -EACCES;
1410                         return -ENXIO;
1411 
1412                 case MTEOM:
1413                         /* This should leave the tape ready for appending
1414                          * another file to the end, such that it would append
1415                          * after the last FM on tape.
1416                          */
1417                         tpqputs(TPQD_IOCTLS, "MTEOM search for End Of recorded Media");
1418                         if ((mode_access==WRITE) && status_bytes_wr)
1419                                 return -EACCES;
1420                         if (TP_HAVE_EOD) {
1421                                 /* Use faster seeking when possible.
1422                                  * This requires the absence of data beyond the EOM.
1423                                  * It seems that my drive does not always perform the
1424                                  * SEEK_EOD correctly, unless it is preceded by a
1425                                  * rewind command.
1426                                  */
1427 # if 0
1428                                 status_eom_detected = status_eof_detected = NO;
1429 # endif
1430                                 stat = do_qic_cmd(QCMD_REWIND, TIM_R);
1431                                 if (stat)
1432                                         return stat;
1433                                 stat = do_qic_cmd(QCMD_SEEK_EOD, TIM_F);
1434                                 /* After a successful seek, TP_EOR should be returned */
1435                         } else {
1436                                 /* else just seek until the drive returns exception "No Data" */
1437                                 stat = 0;
1438                                 while ((stat==0) && (!status_eom_detected)) {
1439                                         stat = do_qic_cmd(QCMD_RD_FM, TIM_F); /***** should use MTFSFM here???? ******/
1440                                 }
1441                                 if (tperror.exs & TP_NDT)
1442                                         return 0;
1443                         }
1444                         return stat;
1445 
1446                 case MTERASE:
1447                         tpqputs(TPQD_IOCTLS, "MTERASE -- ERASE TAPE !");
1448                         if  ((tperror.exs & TP_ST0) && (tperror.exs & TP_WRP)) {
1449                                 tpqputs(TPQD_ALWAYS, "Cartridge is write-protected.");
1450                                 return -EACCES;
1451                         } else {
1452                                 time_t t = jiffies;
1453 
1454                                 /* Plain GNU mt(1) 2.2 erases a tape in O_RDONLY. :-( */
1455                                 if (mode_access==READ) 
1456                                         return -EACCES;
1457 
1458                                 /* give user a few seconds to pull out tape */
1459                                 while (jiffies - t < 4*HZ)
1460                                         schedule();
1461                         }
1462 
1463                         /* don't bother writing filemark first */
1464                         status_eom_detected = status_eof_detected = NO;
1465                         return do_qic_cmd(QCMD_ERASE, TIM_R);
1466 
1467                 case MTRAS1:
1468                         if (TP_HAVE_RAS1) {
1469                                 tpqputs(TPQD_IOCTLS, "MTRAS1: non-destructive self test");
1470                                 stat = do_qic_cmd(QCMD_SELF_TST1, TIM_R);
1471                                 if (stat != 0) {
1472                                         tpqputs(TPQD_ALWAYS, "RAS1 failed");
1473                                         return stat;
1474                                 }
1475                                 return (tp_sense(0)==TE_OK)? 0 : -EIO; /* get_ext_status3(); */
1476                         }
1477                         tpqputs(TPQD_IOCTLS, "RAS1 not supported");
1478                         return -ENXIO;
1479 
1480                 case MTRAS2:
1481                         if (TP_HAVE_RAS2) {
1482                                 tpqputs(TPQD_IOCTLS, "MTRAS2: destructive self test");
1483                                 stat = do_qic_cmd(QCMD_SELF_TST2, TIM_R);
1484                                 if (stat != 0) {
1485                                         tpqputs(TPQD_ALWAYS, "RAS2 failed");
1486                                         return stat;
1487                                 }
1488                                 return (tp_sense(0)==TE_OK)? 0 : -EIO; /* get_ext_status3(); */
1489                         }
1490                         tpqputs(TPQD_IOCTLS, "RAS2 not supported");
1491                         return -ENXIO;
1492 
1493                 case MTSEEK:
1494                         if (TP_HAVE_SEEK && (QIC02_TAPE_IFC==ARCHIVE)) {
1495                                 tpqputs(TPQD_IOCTLS, "MTSEEK seeking block");
1496                                 if ((mode_access==WRITE) && status_bytes_wr)
1497                                         return -EACCES;
1498                                 /* NOTE: address (24 bits) is in seek_addr_buf[] */
1499                                 return do_qic_cmd(AR_QCMDV_SEEK_BLK, TIM_F);
1500                         }
1501                         else
1502                                 return -ENOTTY;
1503 
1504                 default:
1505                         return -ENOTTY;
1506         }
1507 } /* do_ioctl_cmd */
1508 
1509 
1510 /* dma_transfer(): This routine is called for every 512 bytes to be read
1511  * from/written to the tape controller. Speed is important here!
1512  * (There must be enough time left for the hd controller!)
1513  * When other devices use DMA they must ensure they use un-interruptible
1514  * double byte accesses to the DMA controller. Floppy.c is ok.
1515  * Must have interrupts disabled when this function is invoked,
1516  * otherwise, the double-byte transfers to the DMA controller will not
1517  * be atomic. That could lead to nasty problems when they are interrupted
1518  * by other DMA interrupt-routines.
1519  *
1520  * This routine merely does the least possible to keep
1521  * the transfers going:
1522  *      - set the DMA count register for the next 512 bytes
1523  *      - adjust the DMA address and page registers
1524  *      - adjust the timeout
1525  *      - tell the tape controller to start transferring
1526  * We assume the dma address and mode are, and remain, valid.
1527  */ 
1528 static inline void dma_transfer(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1529 {
1530 
1531         if (QIC02_TAPE_IFC == WANGTEK) /* or EVEREX */
1532                 outb_p(WT_CTL_ONLINE, QIC02_CTL_PORT);  /* back to normal */
1533         else if (QIC02_TAPE_IFC == ARCHIVE)
1534                 outb_p(0, AR_RESET_DMA_PORT);
1535         else /* QIC02_TAPE_IFC == MOUNTAIN */
1536                 outb_p(ctlbits, QIC02_CTL_PORT);
1537 
1538 
1539         clear_dma_ff(QIC02_TAPE_DMA);
1540         set_dma_mode(QIC02_TAPE_DMA, dma_mode);
1541         set_dma_addr(QIC02_TAPE_DMA, buffaddr+dma_bytes_done);  /* full address */
1542         set_dma_count(QIC02_TAPE_DMA, TAPE_BLKSIZE);
1543 
1544         /* start tape DMA controller */
1545         if (QIC02_TAPE_IFC == WANGTEK) /* or EVEREX */
1546                 outb_p(WT_CTL_DMA | WT_CTL_ONLINE, QIC02_CTL_PORT); /* trigger DMA transfer */
1547 
1548         else if (QIC02_TAPE_IFC == ARCHIVE) {
1549                 outb_p(AR_CTL_IEN | AR_CTL_DNIEN, QIC02_CTL_PORT);  /* enable interrupts again */
1550                 outb_p(0, AR_START_DMA_PORT);                     /* start DMA transfer */
1551                 /* In dma_end() AR_RESET_DMA_PORT is written too. */
1552 
1553         } else /* QIC02_TAPE_IFC == MOUNTAIN */ {
1554                 inb(MTN_R_DESELECT_DMA_PORT);
1555                 outb_p(ctlbits | (MTN_CTL_EXC_IEN | MTN_CTL_DNIEN), QIC02_CTL_PORT);
1556                 outb_p(0, MTN_W_SELECT_DMA_PORT);        /* start DMA transfer */
1557                 if (dma_mode == DMA_MODE_WRITE)
1558                         outb_p(0, MTN_W_DMA_WRITE_PORT); /* start DMA transfer */
1559         }
1560 
1561         /* start computer DMA controller */
1562         enable_dma(QIC02_TAPE_DMA);
1563         /* block transfer should start now, jumping to the 
1564          * interrupt routine when done or an exception was detected.
1565          */
1566 } /* dma_transfer */
1567 
1568 
1569 /* start_dma() sets a DMA transfer up between the tape controller and
1570  * the kernel qic02_tape_buf buffer.
1571  * Normally bytes_todo==dma_bytes_done at the end of a DMA transfer. If not,
1572  * a filemark was read, or an attempt to write beyond the End Of Tape 
1573  * was made. [Or some other bad thing happened.]
1574  * Must do a sense() before returning error.
1575  */
1576 static int start_dma(short mode, unsigned long bytes_todo)
     /* [previous][next][first][last][top][bottom][index][help] */
1577 /* assume 'bytes_todo'>0 */
1578 {
1579         int stat;
1580         
1581         tpqputs(TPQD_DEBUG, "start_dma() enter");
1582         TPQDEB({printk(TPQIC02_NAME ": doing_read==%d, doing_write==%d\n", doing_read, doing_write);})
1583 
1584         dma_bytes_done = 0;
1585         dma_bytes_todo = bytes_todo;
1586         status_error = NO;
1587         /* dma_mode!=0 indicates that the dma controller is in use */
1588         dma_mode = (mode == WRITE)? DMA_MODE_WRITE : DMA_MODE_READ;     
1589 
1590         /* Only give READ/WRITE DATA command to tape drive if we haven't
1591          * done that already. Otherwise the drive will rewind to the beginning
1592          * of the current file on tape. Any QIC command given other than
1593          * R/W FM will break the read/write transfer cycle.
1594          * do_qic_cmd() will terminate doing_{read,write}
1595          */
1596         if ((doing_read == NO) && (doing_write == NO)) {
1597                 /* First, we have to clear the status -- maybe remove TP_FIL???
1598                  */
1599 
1600 #if 0
1601                 /* Next dummy get status is to make sure CNI is valid,
1602                    since we're only just starting a read/write it doesn't
1603                    matter some exceptions are cleared by reading the status;
1604                    we're only interested in CNI and WRP. -Eddy */
1605                 get_status(&tperror);
1606 #else
1607                 /* TP_CNI should now be handled in open(). -Hennus */
1608 #endif
1609 
1610                 stat = tp_sense(((mode == WRITE)? 0 : TP_WRP) | TP_BOM | TP_FIL);
1611                 if (stat != TE_OK)
1612                         return stat;
1613 
1614 #if OBSOLETE
1615                 /************* not needed iff rd_status() would wait for ready!!!!!! **********/
1616                 if (wait_for_ready(TIM_S) != TE_OK) {   /*** not sure this is needed ***/
1617                         tpqputs(TPQD_ALWAYS, "wait_for_ready failed in start_dma");
1618                         return -EIO;
1619                 }
1620 #endif
1621 
1622                 if (QIC02_TAPE_IFC == MOUNTAIN) {
1623                         /* Set control bits to select ONLINE during command */
1624                         ctlbits |= MTN_QIC02_CTL_ONLINE;
1625                 }
1626 
1627                 /* Tell the controller the data direction */
1628 
1629                 /* r/w, timeout medium, check exceptions, sets status_cmd_pending. */
1630                 stat = send_qic02_cmd((mode == WRITE)? QCMD_WRT_DATA : QCMD_RD_DATA, TIM_M, 0);
1631                 if (stat!=TE_OK) {
1632                         printk(TPQIC02_NAME ": start_dma: init %s failed\n",
1633                                 (mode == WRITE)? "write" : "read");
1634                         (void) tp_sense(0);
1635                         return stat;
1636                 }
1637 
1638                 /* Do this last, because sense() will clear the doing_{read,write}
1639                  * flags, causing trouble next time around.
1640                  */
1641                 if (wait_for_ready(TIM_M) != TE_OK)
1642                         return -EIO;
1643                 switch (mode) {
1644                         case READ:
1645                                 doing_read = YES;
1646                                 break;
1647                         case WRITE:
1648                                 doing_write = YES;
1649                                 break;
1650                         default:
1651                                 printk(TPQIC02_NAME ": requested unknown mode %d\n", mode);
1652                                 panic(TPQIC02_NAME ": invalid mode in start_dma()");
1653                 }
1654 
1655         } else if (is_exception()) {
1656                 /* This is for Archive drives, to handle reads with 0 bytes
1657                  * left for the last read request.
1658                  *
1659                  * ******** this also affects EOF/EOT handling! ************
1660                  */
1661                 tpqputs(TPQD_ALWAYS, "detected exception in start_dma() while transfer in progress");
1662                 status_error = YES;
1663                 return TE_END;
1664         }
1665 
1666 
1667         status_expect_int = YES;
1668 
1669         /* This assumes tape is already positioned, but these
1670          * semi-'intelligent' drives are unpredictable...
1671          */
1672         TIMERON(TIM_M*2);
1673 
1674         /* initiate first data block read from/write to the tape controller */
1675 
1676         cli();
1677         dma_transfer();
1678         sti();
1679 
1680         TPQPUTS("start_dma() end");
1681         return TE_OK;
1682 } /* start_dma */
1683 
1684 
1685 /* This cleans up after the dma transfer has completed
1686  * (or failed). If an exception occurred, a sense()
1687  * must be done. If the exception was caused by a FM,
1688  * sense() will set `status_eof_detected' and
1689  * `status_eom_detected', as required.
1690  */
1691 static void end_dma(unsigned long * bytes_done)
     /* [previous][next][first][last][top][bottom][index][help] */
1692 {
1693         int stat = TE_OK;
1694 
1695         TIMEROFF;
1696 
1697         TPQPUTS("end_dma() enter");
1698 
1699         disable_dma(QIC02_TAPE_DMA);
1700         clear_dma_ff(QIC02_TAPE_DMA);
1701 
1702         if (QIC02_TAPE_IFC == WANGTEK) /* or EVEREX */
1703                 outb_p(WT_CTL_ONLINE, QIC02_CTL_PORT);  /* back to normal */
1704         else if (QIC02_TAPE_IFC == ARCHIVE)
1705                 outb_p(0, AR_RESET_DMA_PORT);
1706         else /* QIC02_TAPE_IFC == MOUNTAIN */ {
1707                 /* Clear control bits, de-select ONLINE during tp_sense */
1708                 ctlbits &= ~MTN_QIC02_CTL_ONLINE;
1709         }
1710 
1711         stat = wait_for_ready(TIM_M);
1712         if (status_error || (stat!=TE_OK)) {
1713                 tpqputs(TPQD_DMAX, "DMA transfer exception");
1714                 stat = tp_sense((dma_mode==READ)? TP_WRP : 0);
1715                 /* no return here -- got to clean up first! */
1716         } else /* if (QIC02_TAPE_IFC == MOUNTAIN) */ {
1717                 outb_p(ctlbits, QIC02_CTL_PORT);
1718         }
1719 
1720         if (QIC02_TAPE_IFC == MOUNTAIN)
1721                 inb(MTN_R_DESELECT_DMA_PORT);
1722 
1723         /* take the tape controller offline */
1724 
1725         /* finish off DMA stuff */
1726 
1727 
1728         dma_mode = 0;
1729         /* Note: The drive is left on-line, ready for the next
1730          * data transfer.
1731          * If the next command to the drive does not continue
1732          * the pending cycle, it must do 2 sense()s first.
1733          */
1734 
1735         *bytes_done = dma_bytes_done;
1736         status_expect_int = NO;
1737         ioctl_status.mt_blkno += (dma_bytes_done / TAPE_BLKSIZE);
1738 
1739         TPQPUTS("end_dma() exit");
1740         /*** could return stat here ***/
1741 } /* end_dma */
1742 
1743 /*********** Below are the (public) OS-interface procedures ***********/
1744 
1745 
1746 /* qic02_tape_times_out() is called when a DMA transfer doesn't complete
1747  * quickly enough. Usually this means there is something seriously wrong
1748  * with the hardware/software, but it could just be that the controller
1749  * has decided to do a long rewind, just when I didn't expect it.
1750  * Just try again.
1751  */
1752 static void qic02_tape_times_out(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1753 {
1754         printk("time-out in %s driver\n", TPQIC02_NAME);
1755         if ((status_cmd_pending>0) || dma_mode) {
1756                 /* takes tooo long, shut it down */
1757                 status_dead = YES;
1758                 status_cmd_pending = 0;
1759                 status_timer_on = NO;
1760                 status_expect_int = NO;
1761                 status_error = YES;
1762                 if (dma_mode) {
1763                         dma_mode = 0;   /* signal end to read/write routine */
1764                         wake_up(&qic02_tape_transfer);
1765                 }
1766         }
1767 } /* qic02_tape_times_out */
1768 
1769 /*
1770  * Interrupt handling:
1771  *
1772  * 1) Interrupt is generated iff at the end of 
1773  *    a 512-DMA-block transfer.
1774  * 2) EXCEPTION is not raised unless something 
1775  *    is wrong or EOT/FM is detected.
1776  * 3) FM EXCEPTION is set *after* the last byte has
1777  *    been transferred by DMA. By the time the interrupt
1778  *    is handled, the EXCEPTION may already be set.
1779  *
1780  * So,
1781  * 1) On EXCEPTION, assume data has been transferred, so
1782  *    continue as usual, but set a flag to indicate the
1783  *    exception was detected.
1784  *    Do a sense status when the flag is found set.
1785  * 2) Do not attempt to continue a transfer after an exception.
1786  *    [??? What about marginal blocks???????]
1787  */
1788 
1789 
1790 /* qic02_tape_interrupt() is called when the tape controller completes 
1791  * a DMA transfer.
1792  * We are not allowed to sleep here! 
1793  *
1794  * Check if the transfer was successful, check if we need to transfer
1795  * more. If the buffer contains enough data/is empty enough, signal the
1796  * read/write() thread to copy to/from user space.
1797  * When we are finished, set flags to indicate end, disable timer.
1798  * NOTE: This *must* be fast! 
1799  */
1800 static void qic02_tape_interrupt(int irq, struct pt_regs *regs)
     /* [previous][next][first][last][top][bottom][index][help] */
1801 {
1802         int stat, r, i;
1803 
1804         TIMEROFF;
1805 
1806         if (status_expect_int) {
1807 #ifdef WANT_EXTRA_FULL_DEBUGGING
1808                 if (TP_DIAGS(current_tape_dev))
1809                         printk("@");
1810 #endif
1811                 stat = inb(QIC02_STAT_PORT);    /* Knock, knock */
1812                 if (QIC02_TAPE_IFC == ARCHIVE) {        /* "Who's there?" */
1813                         if (((stat & (AR_STAT_DMADONE)) == 0) &&
1814                               ((stat & (QIC02_STAT_EXCEPTION)) != 0)) {
1815                                 TIMERCONT;
1816                                 return;                 /* "Linux with IRQ sharing" */
1817                         }
1818                 }
1819 
1820                 if ((stat & QIC02_STAT_EXCEPTION) == 0) {       /* exception occurred */
1821                         /* Possible causes for an exception during a transfer:
1822                          *      - during a write-cycle: end of tape (EW) hole detected.
1823                          *      - during a read-cycle: filemark or EOD detected.
1824                          *      - something went wrong
1825                          * So don't continue with the next block.
1826                          */
1827                         tpqputs(TPQD_ALWAYS, "isr: exception on tape controller");
1828                         printk("      status %02x\n", stat);
1829                         status_error = TE_EX;
1830 
1831                         dma_bytes_done += TAPE_BLKSIZE;
1832 
1833                         dma_mode = 0;   /* wake up rw() */
1834                         status_expect_int = NO;
1835                         wake_up(&qic02_tape_transfer);
1836                         return;
1837                 }
1838                 /* return if tape controller not ready, or
1839                  * if dma channel hasn't finished last byte yet.
1840                  */
1841                 r = 0;
1842 /* Skip next ready check for Archive controller because
1843  * it may be busy reading ahead. Weird. --hhb
1844  */
1845                 if (QIC02_TAPE_IFC == WANGTEK)  /* I think this is a drive-dependency, not IFC -- hhb */
1846                         if (stat & QIC02_STAT_READY) {          /* not ready */
1847                                 tpqputs(TPQD_ALWAYS, "isr: ? Tape controller not ready");
1848                                 r = 1;
1849                         }
1850 
1851                 if ( (i = get_dma_residue(QIC02_TAPE_DMA)) != 0 ) {
1852                         printk(TPQIC02_NAME ": dma_residue == %x !!!\n", i);
1853                         r = 1;  /* big trouble, but can't do much about it... */
1854                 }
1855 
1856                 if (r) 
1857                         return;
1858 
1859                 /* finish DMA cycle */
1860 
1861                 /* no errors detected, continue */
1862                 dma_bytes_done += TAPE_BLKSIZE;
1863                 if (dma_bytes_done >= dma_bytes_todo) {
1864                         /* finished! Wakeup rw() */
1865                         dma_mode = 0;
1866                         status_expect_int = NO;
1867                         TPQPUTS("isr: dma_bytes_done");
1868                         wake_up(&qic02_tape_transfer);
1869                 } else {
1870                         /* start next transfer, account for track-switching time */
1871                         timer_table[QIC02_TAPE_TIMER].expires = jiffies + 6*HZ;
1872                         dma_transfer();
1873                 }
1874         } else {
1875                 printk(TPQIC02_NAME ": Unexpected interrupt, stat == %x\n",
1876                        inb(QIC02_STAT_PORT));
1877         }
1878 } /* qic02_tape_interrupt */
1879 
1880 
1881 static int qic02_tape_lseek(struct inode * inode, struct file * file, off_t offset, int origin)
     /* [previous][next][first][last][top][bottom][index][help] */
1882 {
1883         return -EINVAL; /* not supported */
1884 } /* qic02_tape_lseek */
1885 
1886 
1887 /* read/write routines:
1888  * This code copies between a kernel buffer and a user buffer. The 
1889  * actual data transfer is done using DMA and interrupts. Time-outs
1890  * are also used.
1891  *
1892  * When a filemark is read, we return '0 bytes read' and continue with the
1893  * next file after that.
1894  * When EOM is read, we return '0 bytes read' twice.
1895  * When the EOT marker is detected on writes, '0 bytes read' should be
1896  * returned twice. If user program does a MTNOP after that, 2 additional
1897  * blocks may be written.       ------- FIXME: Implement this correctly  *************************************************
1898  *
1899  * Only read/writes in multiples of 512 bytes are accepted.
1900  * When no bytes are available, we sleep() until they are. The controller will
1901  * generate an interrupt, and we (should) get a wake_up() call.
1902  *
1903  * Simple buffering is used. User program should ensure that a large enough
1904  * buffer is used. Usually the drive does some buffering as well (something
1905  * like 4k or so).
1906  *
1907  * Scott S. Bertilson suggested to continue filling the user buffer, rather
1908  * than waste time on a context switch, when the kernel buffer fills up.
1909  */
1910 
1911 /*
1912  * Problem: tar(1) doesn't always read the entire file. Sometimes the entire file
1913  * has been read, but the EOF token is never returned to tar(1), simply because
1914  * tar(1) knows it has already read all of the data it needs. So we must use
1915  * open/release to reset the `reported_read_eof' flag. If we don't, the next read
1916  * request would return the EOF flag for the previous file.
1917  */
1918 
1919 static int qic02_tape_read(struct inode * inode, struct file * filp, char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
1920 {
1921         int error;
1922         kdev_t dev = inode->i_rdev;
1923         unsigned short flags = filp->f_flags;
1924         unsigned long bytes_todo, bytes_done, total_bytes_done = 0;
1925         int stat;
1926 
1927         if (status_zombie==YES) {
1928                 tpqputs(TPQD_ALWAYS, "configs not set");                
1929                 return -ENXIO;
1930         }
1931 
1932         if (TP_DIAGS(current_tape_dev))
1933                 /* can't print a ``long long'' (for filp->f_pos), so chop it */
1934                 printk(TPQIC02_NAME ": request READ, minor=%x, buf=%p, count=%x, pos=%lx, flags=%x\n",
1935                         MINOR(dev), buf, count, (unsigned long) filp->f_pos, flags);
1936 
1937         if (count % TAPE_BLKSIZE) {     /* Only allow mod 512 bytes at a time. */
1938                 tpqputs(TPQD_BLKSZ, "Wrong block size");
1939                 return -EINVAL;
1940         }
1941 
1942         /* Just assume everything is ok. Controller will scream if not. */
1943 
1944         if (status_bytes_wr)    /* Once written, no more reads, 'till after WFM. */
1945                 return -EACCES;
1946 
1947 
1948         /* Make sure buffer is safe to write into. */
1949         error = verify_area(VERIFY_WRITE, buf, count);
1950         if (error)
1951                 return error;
1952 
1953         /* This is rather ugly because it has to implement a finite state
1954          * machine in order to handle the EOF situations properly.
1955          */
1956         while (count>=0) {
1957                 bytes_done = 0;
1958                 /* see how much fits in the kernel buffer */
1959                 bytes_todo = TPQBUF_SIZE;
1960                 if (bytes_todo>count)
1961                         bytes_todo = count;
1962 
1963                 /* Must ensure that user program sees exactly one EOF token (==0) */
1964                 if (return_read_eof==YES) {
1965                         if (TPQDBG(DEBUG))
1966                                 printk("read: return_read_eof==%d, reported_read_eof==%d, total_bytes_done==%lu\n", return_read_eof, reported_read_eof, total_bytes_done);
1967 
1968                         if (reported_read_eof==NO) {
1969                                 /* have not yet returned EOF to user program */
1970                                 if (total_bytes_done>0) {
1971                                         return total_bytes_done; /* next time return EOF */
1972                                 } else {
1973                                         reported_read_eof = YES; /* move on next time */
1974                                         return 0;                /* return EOF */
1975                                 }                               
1976                         } else {
1977                                 /* Application program has already received EOF
1978                                  * (above), now continue with next file on tape,
1979                                  * if possible.
1980                                  * When the FM is reached, EXCEPTION is set,
1981                                  * causing a sense(). Subsequent read/writes will
1982                                  * continue after the FM.
1983                                  */
1984 /*********** ?????????? this should check for (EOD|NDT), not EOM, 'cause we can read past EW: ************/
1985                                 if (status_eom_detected)
1986                                         /* If EOM, nothing left to read, so keep returning EOFs.
1987                                          *** should probably set some flag to avoid clearing
1988                                          *** status_eom_detected through ioctls or something
1989                                          */
1990                                         return 0;
1991                                 else {
1992                                         /* just eof, there may be more files ahead... */
1993                                         return_read_eof = NO;
1994                                         reported_read_eof = NO;
1995                                         status_eof_detected = NO; /* reset this too */
1996                                         /*fall through*/
1997                                 }
1998                         }
1999                 }
2000 
2001 /*****************************/
2002                 if (bytes_todo==0)
2003                         return total_bytes_done;
2004 
2005                 if (bytes_todo>0) {
2006                         /* start reading data */
2007                         if (is_exception())     /****************************************/
2008                                 tpqputs(TPQD_DMAX, "is_exception() before start_dma()!");
2009 /******************************************************************
2010  ***** if start_dma() fails because the head is positioned 0 bytes
2011  ***** before the FM, (causing EXCEPTION to be set) return_read_eof should
2012  ***** be set to YES, and we should return total_bytes_done, rather than -ENXIO.
2013  ***** The app should recognize this as an EOF condition.
2014  ***************************************************************************/
2015                         stat = start_dma(READ, bytes_todo);
2016                         if (stat == TE_OK) {
2017                                 /* Wait for transfer to complete, interrupt should wake us */
2018                                 while (dma_mode != 0) {
2019                                         sleep_on(&qic02_tape_transfer);
2020                                 }
2021                                 if (status_error)
2022                                         return_read_eof = YES;
2023                         } else if (stat != TE_END) {
2024                                 /* should do sense() on error here */
2025 #if 0
2026                                 return -ENXIO;
2027 #else
2028                                 printk("Trouble: stat==%02x\n", stat);
2029                                 return_read_eof = YES;
2030                                 /*************** check EOF/EOT handling!!!!!! **/
2031 #endif
2032                         }
2033                         end_dma(&bytes_done);
2034                         if (bytes_done>bytes_todo) {
2035                                 tpqputs(TPQD_ALWAYS, "read: Oops, read more bytes than requested");
2036                                 return -EIO;
2037                         }
2038                         /* copy buffer to user-space in one go */
2039                         if (bytes_done>0)
2040                                 memcpy_tofs( (void *) buf, (void *) buffaddr, bytes_done);
2041 #if 1
2042                         /* Checks Ton's patch below */
2043                         if ((return_read_eof == NO) && (status_eof_detected == YES)) {
2044                                 printk(TPQIC02_NAME ": read(): return_read_eof=%d, status_eof_detected=YES. return_read_eof:=YES\n", return_read_eof);
2045                         }
2046 #endif
2047                         if ((bytes_todo != bytes_done) || (status_eof_detected == YES))
2048                                 /* EOF or EOM detected. return EOF next time. */
2049                                 return_read_eof = YES;
2050                 } /* else: ignore read request for 0 bytes */
2051 
2052                 if (bytes_done>0) {
2053                         status_bytes_rd = YES;
2054                         buf += bytes_done;
2055                         filp->f_pos += bytes_done;
2056                         total_bytes_done += bytes_done;
2057                         count -= bytes_done;
2058                 }
2059         }
2060         tpqputs(TPQD_ALWAYS, "read request for <0 bytes");
2061         return -EINVAL;
2062 } /* qic02_tape_read */
2063 
2064 
2065 
2066 /* The drive detects near-EOT by means of the holes in the tape.
2067  * When the holes are detected, there is some space left. The drive
2068  * reports this as a TP_EOM exception. After clearing the exception,
2069  * the drive should accept two extra blocks.
2070  *
2071  * It seems there are some archiver programs that would like to use the
2072  * extra space for writing a continuation marker. The driver should return
2073  * end-of-file to the user program on writes, when the holes are detected.
2074  * If the user-program wants to use the extra space, it should use the
2075  * MTNOP ioctl() to get the generic status register and may then continue
2076  * writing (max 1kB).   ----------- doesn't work yet...............
2077  *
2078  * EOF behaviour on writes:
2079  * If there is enough room, write all of the data.
2080  * If there is insufficient room, write as much as will fit and
2081  * return the amount written. If the requested amount differs from the
2082  * written amount, the application program should recognize that as the
2083  * end of file. Subsequent writes will return -ENOSPC.
2084  * Unless the minor bits specify a rewind-on-close, the tape will not
2085  * be rewound when it is full. The user-program should do that, if desired.
2086  * If the driver were to do that automatically, a user-program could be 
2087  * confused about the EOT/BOT condition after re-opening the tape device.
2088  *
2089  * Multiple volume support: Tar closes the tape device before prompting for
2090  * the next tape. The user may then insert a new tape and tar will open the
2091  * tape device again. The driver will detect an exception status in (No Cartridge)
2092  * and force a rewind. After that tar may continue writing.
2093  */
2094 static int qic02_tape_write(struct inode * inode, struct file * filp, const char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
2095 {
2096         int error;
2097         kdev_t dev = inode->i_rdev;
2098         unsigned short flags = filp->f_flags;
2099         unsigned long bytes_todo, bytes_done, total_bytes_done = 0;
2100 
2101         if (status_zombie==YES) {
2102                 tpqputs(TPQD_ALWAYS, "configs not set");                
2103                 return -ENXIO;
2104         }
2105 
2106         if (TP_DIAGS(current_tape_dev))
2107                 /* can't print a ``long long'' (for filp->f_pos), so chop it */
2108                 printk(TPQIC02_NAME ": request WRITE, minor=%x, buf=%p, count=%x, pos=%lx, flags=%x\n",
2109                         MINOR(dev), buf, count, (unsigned long) filp->f_pos, flags);
2110 
2111         if (count % TAPE_BLKSIZE) {     /* only allow mod 512 bytes at a time */
2112                 tpqputs(TPQD_BLKSZ, "Wrong block size");
2113                 return -EINVAL;
2114         }
2115 
2116         if (mode_access==READ) {
2117                 tpqputs(TPQD_ALWAYS, "Not in write mode");
2118                 return -EACCES;
2119         }
2120 
2121         /* open() does a sense() and we can assume the tape isn't changed
2122          * between open() and release(), so the tperror.exs bits will still
2123          * be valid.
2124          */
2125         if ((tperror.exs & TP_ST0) && (tperror.exs & TP_WRP)) {
2126                 tpqputs(TPQD_ALWAYS, "Cartridge is write-protected.");
2127                 return -EACCES; /* don't even try when write protected */
2128         }
2129 
2130         /* Make sure buffer is safe to read from. */
2131         error = verify_area(VERIFY_READ, buf, count);
2132         if (error)
2133                 return error;
2134 
2135         if (doing_read == YES)
2136                 terminate_read(0);
2137 
2138         while (count>=0) {
2139                 /* see how much fits in the kernel buffer */
2140                 bytes_done = 0;
2141                 bytes_todo = TPQBUF_SIZE;
2142                 if (bytes_todo>count)
2143                         bytes_todo = count;
2144         
2145                 if (return_write_eof == YES) {
2146                         /* return_write_eof should be reset on reverse tape movements. */
2147 
2148                         if (reported_write_eof==NO) {
2149                                 if (bytes_todo>0) {
2150                                         tpqputs(TPQD_ALWAYS, "partial write");
2151                                         /* partial write signals EOF to user program */
2152                                 }
2153                                 reported_write_eof = YES;
2154                                 return total_bytes_done;
2155                         } else {
2156                                 return -ENOSPC;          /* return error */
2157                         }       
2158                 }
2159 
2160                 /* Quit when done. */
2161                 if (bytes_todo==0)
2162                         return total_bytes_done;
2163 
2164 
2165                 /* copy from user to DMA buffer and initiate transfer. */
2166                 if (bytes_todo>0) {
2167                         memcpy_fromfs( (void *) buffaddr, (const void *) buf, bytes_todo);
2168 
2169 /****************** similar problem with read() at FM could happen here at EOT.
2170  ******************/
2171 
2172 /***** if at EOT, 0 bytes can be written. start_dma() will
2173  ***** fail and write() will return ENXIO error
2174  *****/
2175                         if (start_dma(WRITE, bytes_todo) != TE_OK) {
2176                                 tpqputs(TPQD_ALWAYS, "write: start_dma() failed");
2177                                 /* should do sense() on error here */
2178                                 return -ENXIO;  /*********** FIXTHIS **************/
2179                         }
2180 
2181                         /* Wait for write to complete, interrupt should wake us. */
2182                         while ((status_error == 0) && (dma_mode != 0)) {
2183                                 sleep_on(&qic02_tape_transfer);
2184                         }
2185 
2186                         end_dma(&bytes_done);
2187                         if (bytes_done>bytes_todo) {
2188                                 tpqputs(TPQD_ALWAYS, "write: Oops, wrote more bytes than requested");
2189                                 return -EIO;
2190                         }
2191                         /* If the dma-transfer was aborted because of an exception,
2192                          * status_error will have been set in the interrupt handler.
2193                          * Then end_dma() will do a sense().
2194                          * If the exception was EXC_EOM, the EW-hole was encountered
2195                          * and two more blocks could be written. For the time being we'll
2196                          * just consider this to be the EOT.
2197                          * Otherwise, something Bad happened, such as the maximum number
2198                          * of block-rewrites was exceeded. [e.g. A very bad spot on tape was
2199                          * encountered. Normally short dropouts are compensated for by
2200                          * rewriting the block in error, up to 16 times. I'm not sure
2201                          * QIC-24 drives can do this.]
2202                          */
2203                         if (status_error) {
2204                                 if (status_eom_detected == YES) {
2205                                         tpqputs(TPQD_ALWAYS, "write: EW detected");
2206                                         return_write_eof = YES;
2207                                 } else {
2208                                         /* probably EXC_RWA */
2209                                         tpqputs(TPQD_ALWAYS, "write: dma: error in writing");
2210                                         return -EIO;
2211                                 }
2212                         }
2213                         if (bytes_todo != bytes_done)
2214                                 /* EOF or EOM detected. return EOT next time. */
2215                                 return_write_eof = YES;
2216                 }
2217                 /* else: ignore write request for 0 bytes. */
2218 
2219                 if (bytes_done>0) {
2220                         status_bytes_wr = YES;
2221                         buf += bytes_done;
2222                         filp->f_pos += bytes_done;
2223                         total_bytes_done += bytes_done;
2224                         count -= bytes_done;
2225                 }
2226         }
2227         tpqputs(TPQD_ALWAYS, "write request for <0 bytes");
2228         if (TPQDBG(DEBUG))
2229                 printk(TPQIC02_NAME ": status_bytes_wr %x, buf %p, total_bytes_done %lx, count %x\n", status_bytes_wr, buf, total_bytes_done, count);
2230         return -EINVAL;
2231 } /* qic02_tape_write */
2232 
2233 
2234 
2235 /* qic02_tape_open()
2236  * We allow the device to be opened, even if it is marked 'dead' because
2237  * we want to be able to reset the tape device without rebooting.
2238  * Only one open tape file at a time, except when minor=255.
2239  * Minor 255 is only allowed for resetting and always returns <0.
2240  * 
2241  * The density command is only allowed when TP_BOM is set. Thus, remember
2242  * the most recently used minor bits. When they are different from the
2243  * remembered values, rewind the tape and set the required density.
2244  * Don't rewind if the minor bits specify density 0.
2245  */
2246 static int qic02_tape_open(struct inode * inode, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
2247 {
2248         kdev_t dev = inode->i_rdev;
2249         unsigned short flags = filp->f_flags;
2250         unsigned short dens = 0;
2251         int s;
2252 
2253 
2254         if (TP_DIAGS(dev)) {
2255                 printk("qic02_tape_open: dev=%s, flags=%x     ",
2256                        kdevname(dev), flags);
2257         }
2258 
2259         if (MINOR(dev)==255)    /* special case for resetting */
2260                 if (suser())
2261                         return (tape_reset(1)==TE_OK) ? -EAGAIN : -ENXIO;
2262                 else
2263                         return -EPERM;
2264 
2265         if (status_dead==YES)
2266                 /* Allow `mt reset' ioctl() even when already open()ed. */
2267                 return 0;
2268 
2269         /* Only one at a time from here on... */
2270         if (filp->f_count>1) {  /* filp->f_count==1 for the first open() */
2271                 return -EBUSY;
2272         }
2273 
2274         if (status_zombie==YES)
2275                 /* no irq/dma/port stuff allocated yet, no reset done
2276                  * yet, so return until MTSETCONFIG has been done.
2277                  */
2278                 return 0;
2279 
2280         status_bytes_rd = NO;
2281         status_bytes_wr = NO;
2282 
2283         return_read_eof = NO;   /********????????????????*****/
2284         return_write_eof = (status_eot_detected)? YES : NO;
2285 
2286         /* Clear this in case user app close()d before reading EOF token */
2287         status_eof_detected = NO;
2288 
2289         reported_read_eof = NO;
2290         reported_write_eof = NO;
2291 
2292 
2293         switch (flags & O_ACCMODE) {
2294                 case O_RDONLY:
2295                         mode_access = READ;
2296                         break;
2297                 case O_WRONLY:  /* Fallthru... Strictly speaking this is not correct... */
2298                 case O_RDWR:    /* Reads are allowed as long as nothing is written */
2299                         mode_access = WRITE;
2300                         break;
2301         }
2302 
2303         /* This is to avoid tape-changed problems (TP_CNI exception).
2304          *
2305          * Since removing the cartridge will not raise an exception,
2306          * we always do a tp_sense() to make sure we have the proper
2307          * CNI status, the 2150L may need an additional sense.... - Eddy
2308          */
2309         s = tp_sense(TP_WRP|TP_EOM|TP_BOM|TP_CNI|TP_EOR);
2310 
2311         if (s == TE_OK)
2312                 /* Try to clear cartridge-changed status for Archive-2150L */
2313                 if ((tperror.exs & TP_ST0) && (tperror.exs & TP_CNI))
2314                         s = tp_sense(TP_WRP|TP_EOM|TP_BOM|TP_CNI|TP_EOR);
2315 
2316         if (s != TE_OK) {
2317                 tpqputs(TPQD_ALWAYS, "open: sense() failed");
2318                 return -EIO;
2319         }
2320 
2321         /* exception bits should be up-to-date now, so check for
2322          * tape presence and exit if absent.
2323          * Even `mt stat' will fail without a tape.
2324          */
2325         if ((tperror.exs & TP_ST0) && (tperror.exs & TP_CNI)) {
2326                 tpqputs(TPQD_ALWAYS, "No tape present.");
2327                 return -EIO;
2328         }
2329 
2330         /* At this point we can assume that a tape is present and
2331          * that it will remain present until release() is called.
2332          */
2333 
2334         /* not allowed to do QCMD_DENS_* unless tape is rewound */
2335         if ((TP_DENS(dev)!=0) && (TP_DENS(current_tape_dev) != TP_DENS(dev))) {
2336                 /* force rewind if minor bits have changed,
2337                  * i.e. user wants to use tape in different format.
2338                  * [assuming single drive operation]
2339                  */
2340                 if (TP_HAVE_DENS) {
2341                         tpqputs(TPQD_REWIND, "Density minor bits have changed. Forcing rewind.");
2342                         need_rewind = YES;
2343                 }
2344         } else {
2345                 /* density bits still the same, but TP_DIAGS bit 
2346                  * may have changed.
2347                  */
2348                 current_tape_dev = dev;
2349         }
2350 
2351         if (need_rewind == YES) { /***************** CHECK THIS!!!!!!!! **********/
2352                 s = do_qic_cmd(QCMD_REWIND, TIM_R);
2353                 if (s != 0) {
2354                         tpqputs(TPQD_ALWAYS, "open: rewind failed");
2355                         return -EIO;
2356                 }
2357         }
2358 
2359 
2360 /* Note: After a reset command, the controller will rewind the tape
2361  *       just before performing any tape movement operation! ************ SO SET need_rewind flag!!!!!
2362  */ 
2363         if (status_dead==YES) {
2364                 tpqputs(TPQD_ALWAYS, "open: tape dead, attempting reset");
2365                 if (tape_reset(1)!=TE_OK) {
2366                         return -ENXIO;
2367                 } else {
2368                         status_dead = NO;
2369                         if (tp_sense(~(TP_ST1|TP_ILL)) != TE_OK) {
2370                                 tpqputs(TPQD_ALWAYS, "open: tp_sense() failed\n");
2371                                 status_dead = YES;      /* try reset next time */
2372                                 return -EIO;
2373                         }
2374                 }
2375         }
2376 
2377         /* things should be ok, once we get here */
2378 
2379 
2380         /* set density: only allowed when TP_BOM status bit is set,
2381          * so we must have done a rewind by now. If not, just skip over.
2382          * Only give set density command when minor bits have changed.
2383          */
2384         if (TP_DENS(current_tape_dev) == TP_DENS(dev) )
2385                 return 0;
2386 
2387         current_tape_dev = dev;
2388         need_rewind = NO;
2389         if (TP_HAVE_DENS)
2390                 dens = TP_DENS(dev);
2391 
2392         if (dens < sizeof(format_names)/sizeof(char *))
2393                 printk(TPQIC02_NAME ": format: %s%s\n", (dens!=0)? "QIC-" : "", format_names[dens]);
2394         else
2395                 tpqputs(TPQD_REWIND, "Wait for retensioning...");
2396 
2397         switch (TP_DENS(dev)) {
2398                 case 0: /* Minor 0 is for drives without set-density support */
2399                         s = 0;
2400                         break;
2401                 case 1:
2402                         s = do_qic_cmd(QCMD_DENS_11, TIM_S);
2403                         break;
2404                 case 2:
2405                         s = do_qic_cmd(QCMD_DENS_24, TIM_S);
2406                         break;
2407                 case 3:
2408                         s = do_qic_cmd(QCMD_DENS_120, TIM_S);
2409                         break;
2410                 case 4:
2411                         s = do_qic_cmd(QCMD_DENS_150, TIM_S);
2412                         break;
2413                 case 5:
2414                         s = do_qic_cmd(QCMD_DENS_300, TIM_S);
2415                         break;
2416                 case 6:
2417                         s = do_qic_cmd(QCMD_DENS_600, TIM_S);
2418                         break;
2419                 default:  /* otherwise do a retension before anything else */
2420                         s = do_qic_cmd(QCMD_RETEN, TIM_R);
2421         }
2422         if (s != 0) {
2423                 status_dead = YES;      /* force reset */
2424                 current_tape_dev = 0; /* earlier 0xff80 */
2425                 return -EIO;
2426         }
2427 
2428         return 0;
2429 } /* qic02_tape_open */
2430 
2431 
2432 static void qic02_tape_release(struct inode * inode, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
2433 {
2434         kdev_t dev = inode->i_rdev;
2435 
2436         if (TP_DIAGS(dev))
2437                 printk("qic02_tape_release: dev=%s\n",
2438                        kdevname(dev));
2439 
2440         if (status_zombie==YES)         /* don't rewind in zombie mode */
2441                 return;
2442 
2443         /* Terminate any pending write cycle. Terminating the read-cycle
2444          * is delayed until it is required to do so for a new command.
2445          */
2446         terminate_write(-1);
2447 
2448         if (status_dead==YES)
2449                 tpqputs(TPQD_ALWAYS, "release: device dead!?");
2450 
2451         /* Rewind only if minor number requires it AND 
2452          * read/writes have been done. ************* IS THIS CORRECT??????????
2453          */
2454         if ((TP_REWCLOSE(dev)) && (status_bytes_rd | status_bytes_wr)) {
2455                 tpqputs(TPQD_REWIND, "release: Doing rewind...");
2456                 (void) do_qic_cmd(QCMD_REWIND, TIM_R);
2457         }
2458 
2459         return;
2460 } /* qic02_tape_release */
2461 
2462 
2463 #ifdef CONFIG_QIC02_DYNCONF
2464 /* Set masks etc. based on the interface card type. */
2465 int update_ifc_masks(int ifc)
     /* [previous][next][first][last][top][bottom][index][help] */
2466 {
2467         QIC02_TAPE_IFC = ifc;
2468 
2469         if ((QIC02_TAPE_IFC == WANGTEK) || (QIC02_TAPE_IFC == EVEREX)) {
2470                 QIC02_STAT_PORT = QIC02_TAPE_PORT;
2471                 QIC02_CTL_PORT = QIC02_TAPE_PORT;
2472                 QIC02_CMD_PORT = QIC02_TAPE_PORT+1;
2473                 QIC02_DATA_PORT = QIC02_TAPE_PORT+1;
2474                 QIC02_STAT_READY = WT_QIC02_STAT_READY;
2475                 QIC02_STAT_EXCEPTION = WT_QIC02_STAT_EXCEPTION;
2476                 QIC02_STAT_MASK = WT_QIC02_STAT_MASK;
2477 
2478                 QIC02_STAT_RESETMASK = WT_QIC02_STAT_RESETMASK;
2479                 QIC02_STAT_RESETVAL = WT_QIC02_STAT_RESETVAL;
2480 
2481                 QIC02_CTL_RESET = WT_QIC02_CTL_RESET;
2482                 QIC02_CTL_REQUEST = WT_QIC02_CTL_REQUEST;
2483  
2484                 if (QIC02_TAPE_DMA == 3)
2485                         WT_CTL_DMA = WT_CTL_DMA3;
2486                 else if (QIC02_TAPE_DMA == 1)
2487                         WT_CTL_DMA = WT_CTL_DMA1;
2488                 else {
2489                         tpqputs(TPQD_ALWAYS, "Unsupported or incorrect DMA channel");
2490                         return -EIO;
2491                 } 
2492 
2493                 if (QIC02_TAPE_IFC == EVEREX) {
2494                         /* Everex is a special case for Wangtek (actually
2495                          * it's the other way 'round, but I saw Wangtek first)
2496                          */
2497                         if (QIC02_TAPE_DMA==3)
2498                                 WT_CTL_DMA = WT_CTL_DMA1;
2499                         /* Fixup the kernel copy of the IFC type to that
2500                          * we don't have to distinguish between Wangtek and
2501                          * and Everex at runtime.
2502                          */
2503                         QIC02_TAPE_IFC = WANGTEK;
2504                 }
2505         } else if (QIC02_TAPE_IFC == ARCHIVE) {
2506                 QIC02_STAT_PORT = QIC02_TAPE_PORT+1;
2507                 QIC02_CTL_PORT = QIC02_TAPE_PORT+1;
2508                 QIC02_CMD_PORT = QIC02_TAPE_PORT;
2509                 QIC02_DATA_PORT = QIC02_TAPE_PORT;
2510                 QIC02_STAT_READY = AR_QIC02_STAT_READY;
2511                 QIC02_STAT_EXCEPTION  = AR_QIC02_STAT_EXCEPTION;
2512                 QIC02_STAT_MASK = AR_QIC02_STAT_MASK;
2513 
2514                 QIC02_STAT_RESETMASK = AR_QIC02_STAT_RESETMASK;
2515                 QIC02_STAT_RESETVAL = AR_QIC02_STAT_RESETVAL;
2516 
2517                 QIC02_CTL_RESET = AR_QIC02_CTL_RESET;
2518                 QIC02_CTL_REQUEST = AR_QIC02_CTL_REQUEST;
2519  
2520                 if (QIC02_TAPE_DMA > 3) {
2521                         tpqputs(TPQD_ALWAYS, "Unsupported or incorrect DMA channel");
2522                         return -EIO;
2523                 } 
2524         } else if (QIC02_TAPE_IFC == MOUNTAIN) {
2525                 QIC02_STAT_PORT = QIC02_TAPE_PORT+1;
2526                 QIC02_CTL_PORT = QIC02_TAPE_PORT+1;
2527                 QIC02_CMD_PORT = QIC02_TAPE_PORT;
2528                 QIC02_DATA_PORT = QIC02_TAPE_PORT;
2529 
2530                 QIC02_STAT_READY = MTN_QIC02_STAT_READY;
2531                 QIC02_STAT_EXCEPTION  = MTN_QIC02_STAT_EXCEPTION;
2532                 QIC02_STAT_MASK = MTN_QIC02_STAT_MASK;
2533 
2534                 QIC02_STAT_RESETMASK = MTN_QIC02_STAT_RESETMASK;
2535                 QIC02_STAT_RESETVAL = MTN_QIC02_STAT_RESETVAL;
2536 
2537                 QIC02_CTL_RESET = MTN_QIC02_CTL_RESET;
2538                 QIC02_CTL_REQUEST = MTN_QIC02_CTL_REQUEST;
2539  
2540                 if (QIC02_TAPE_DMA > 3) {
2541                         tpqputs(TPQD_ALWAYS, "Unsupported or incorrect DMA channel");
2542                         return -EIO;
2543                 } 
2544         } else {
2545                 tpqputs(TPQD_ALWAYS, "Invalid interface type");
2546                 return -ENXIO;
2547         }
2548         return 0;
2549 } /* update_ifc-masks */
2550 #endif
2551 
2552 
2553 /* ioctl allows user programs to rewind the tape and stuff like that */
2554 static int qic02_tape_ioctl(struct inode * inode, struct file * filp, 
     /* [previous][next][first][last][top][bottom][index][help] */
2555                      unsigned int iocmd, unsigned long ioarg)
2556 {
2557         int error;
2558         short i;
2559         int dev_maj = MAJOR(inode->i_rdev);
2560         int c;
2561         struct mtop operation;
2562         char *stp, *argp;
2563         unsigned char blk_addr[6];
2564         struct mtpos ioctl_tell;
2565 
2566 
2567         if (TP_DIAGS(current_tape_dev))
2568                 printk(TPQIC02_NAME ": ioctl(%4x, %4x, %4lx)\n", dev_maj, iocmd, ioarg);
2569 
2570         if (!inode || !ioarg)
2571                 return -EINVAL;
2572 
2573         /* check iocmd first */
2574 
2575         if (dev_maj != QIC02_TAPE_MAJOR) {
2576                 printk(TPQIC02_NAME ": Oops! Wrong device?\n");
2577                 /* A panic() would be appropriate here */
2578                 return -ENODEV;
2579         }
2580 
2581         c = iocmd & IOCCMD_MASK;
2582 
2583 #ifdef DDIOCSDBG
2584         /* Check for DDI Debug Control, contributed by FvK, edited by HHB. */
2585         if (c == DDIOCSDBG) {
2586                 if (!suser())
2587                         return -EPERM;
2588                 verify_area(VERIFY_READ, (int *) ioarg, sizeof(int));
2589                 c = get_user_long((int *) ioarg);
2590                 if (c==0) {
2591                         QIC02_TAPE_DEBUG = 0;
2592                         return 0;
2593                 }
2594                 if ((c>=1) && (c<=32)) {
2595                         QIC02_TAPE_DEBUG |= (1 << (c-1));
2596                         return 0;
2597                 }
2598                 if (c >= 128) {
2599                         QIC02_TAPE_DEBUG &= ~(1 << (c - 128));
2600                         return 0;
2601                 }
2602                 return -EINVAL;
2603         }
2604 #endif
2605 
2606 #ifdef CONFIG_QIC02_DYNCONF
2607         if (c == (MTIOCGETCONFIG & IOCCMD_MASK)) {
2608                 if (((iocmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT) != sizeof(struct mtconfiginfo)) {
2609                         tpqputs(TPQD_ALWAYS, "sizeof(struct mtconfiginfo) does not match!");
2610                         return -EFAULT;
2611                 }
2612 
2613                 /* check for valid user address */
2614                 error = verify_area(VERIFY_WRITE, (void *) ioarg, sizeof(qic02_tape_dynconf));
2615                 if (error)
2616                         return error;
2617                 /* copy current settings to user space */
2618                 stp = (char *) &qic02_tape_dynconf;
2619                 argp = (char *) ioarg;
2620                 for (i=0; i<sizeof(qic02_tape_dynconf); i++) 
2621                         put_user_byte(*stp++, argp++);
2622                 return 0;
2623 
2624         } else if (c == (MTIOCSETCONFIG & IOCCMD_MASK)) {
2625                 static int qic02_get_resources(void), qic02_release_resources(void);
2626 
2627                 /* One should always do a MTIOCGETCONFIG first, then update
2628                  * user-settings, then write back with MTIOCSETCONFIG.
2629                  * Re-open() the device before actual use to make sure
2630                  * everything is initialized.
2631                  */
2632                 if (((iocmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT) != sizeof(struct mtconfiginfo)) {
2633                         tpqputs(TPQD_ALWAYS, "sizeof(struct mtconfiginfo) does not match!");
2634                         return -EFAULT;
2635                 }
2636                 if (!suser())
2637                         return -EPERM;
2638                 if ((doing_read!=NO) || (doing_write!=NO))
2639                         return -EBUSY;
2640                 error = verify_area(VERIFY_READ, (char *) ioarg, sizeof(qic02_tape_dynconf));
2641                 if (error)
2642                         return error;
2643 
2644                 /* copy struct from user space to kernel space */
2645                 stp = (char *) &qic02_tape_dynconf;
2646                 argp = (char *) ioarg;
2647                 for (i=0; i<sizeof(qic02_tape_dynconf); i++)
2648                         *stp++ = get_user_byte(argp++);
2649                 if (status_zombie==NO)
2650                         qic02_release_resources();      /* and go zombie */
2651                 if (update_ifc_masks(qic02_tape_dynconf.ifc_type))
2652                         return -EIO;
2653                 if (qic02_get_resources())
2654                         return -ENXIO;
2655                 return 0;
2656 
2657         }
2658         if (status_zombie==YES) {
2659                 tpqputs(TPQD_ALWAYS, "Configs not set");
2660                 return -ENXIO;
2661         }
2662 #endif
2663         if (c == (MTIOCTOP & IOCCMD_MASK)) {
2664 
2665                 /* Compare expected struct size and actual struct size. This
2666                  * is useful to catch programs compiled with old #includes.
2667                  */
2668                 if (((iocmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT) != sizeof(struct mtop)) {
2669                         tpqputs(TPQD_ALWAYS, "sizeof(struct mtop) does not match!");
2670                         return -EFAULT;
2671                 }
2672                 error = verify_area(VERIFY_READ, (char *) ioarg, sizeof(operation));
2673                 if (error)
2674                         return error;
2675 
2676                 /* copy mtop struct from user space to kernel space */
2677                 stp = (char *) &operation;
2678                 argp = (char *) ioarg;
2679                 for (i=0; i<sizeof(operation); i++)
2680                         *stp++ = get_user_byte(argp++);
2681 
2682                 /* ---note: mt_count is signed, negative seeks must be
2683                  * ---      translated to seeks in opposite direction!
2684                  * (only needed for Sun-programs, I think.)
2685                  */
2686                 /* ---note: MTFSF with count 0 should position the
2687                  * ---      tape at the beginning of the current file.
2688                  */
2689 
2690                 if (TP_DIAGS(current_tape_dev))
2691                         printk("OP op=%4x, count=%4x\n", operation.mt_op, operation.mt_count);
2692 
2693                 if (operation.mt_count < 0)
2694                         tpqputs(TPQD_ALWAYS, "Warning: negative mt_count ignored");
2695 
2696                 ioctl_status.mt_resid = operation.mt_count;
2697                 if (operation.mt_op == MTSEEK) {
2698                         if (!TP_HAVE_SEEK)
2699                                 return -ENOTTY;
2700                         seek_addr_buf[0] = (operation.mt_count>>16)&0xff;
2701                         seek_addr_buf[1] = (operation.mt_count>>8)&0xff;
2702                         seek_addr_buf[2] = (operation.mt_count)&0xff;
2703                         if (operation.mt_count>>24)
2704                                 return -EINVAL;
2705                         if ((error = do_ioctl_cmd(operation.mt_op)) != 0)
2706                                 return error;
2707                         ioctl_status.mt_resid = 0;
2708                 } else {
2709                         while (operation.mt_count > 0) {
2710                                 operation.mt_count--;
2711                                 if ((error = do_ioctl_cmd(operation.mt_op)) != 0)
2712                                         return error;
2713                                 ioctl_status.mt_resid = operation.mt_count;
2714                         }
2715                 }
2716                 return 0;
2717 
2718         } else if (c == (MTIOCGET & IOCCMD_MASK)) {
2719                 if (TP_DIAGS(current_tape_dev))
2720                         printk("GET ");
2721 
2722                 /* compare expected struct size and actual struct size */
2723                 if (((iocmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT) != sizeof(struct mtget)) {
2724                         tpqputs(TPQD_ALWAYS, "sizeof(struct mtget) does not match!");
2725                         return -EFAULT;
2726                 }
2727 
2728                 /* check for valid user address */
2729                 error = verify_area(VERIFY_WRITE, (void *) ioarg, sizeof(ioctl_status));
2730                 if (error)
2731                         return error;
2732 
2733                 /* It appears (gmt(1)) that it is normal behaviour to
2734                  * first set the status with MTNOP, and then to read
2735                  * it out with MTIOCGET
2736                  */
2737 
2738                 /* copy results to user space */
2739                 stp = (char *) &ioctl_status;
2740                 argp = (char *) ioarg;
2741                 for (i=0; i<sizeof(ioctl_status); i++) 
2742                         put_user_byte(*stp++, argp++);
2743                 return 0;
2744 
2745 
2746         } else if (TP_HAVE_TELL && (c == (MTIOCPOS & IOCCMD_MASK))) {
2747                 if (TP_DIAGS(current_tape_dev))
2748                         printk("POS ");
2749 
2750                 /* compare expected struct size and actual struct size */
2751                 if (((iocmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT) != sizeof(struct mtpos)) {
2752                         tpqputs(TPQD_ALWAYS, "sizeof(struct mtpos) does not match!");
2753                         return -EFAULT;
2754                 }
2755 
2756                 /* check for valid user address */
2757                 error = verify_area(VERIFY_WRITE, (void *) ioarg, sizeof(ioctl_tell));
2758                 if (error)
2759                         return error;
2760 
2761                 tpqputs(TPQD_IOCTLS, "MTTELL reading block address");
2762                 if ((doing_read==YES) || (doing_write==YES))
2763                         finish_rw(AR_QCMDV_TELL_BLK);
2764 
2765                 c = rdstatus((char *) blk_addr, sizeof(blk_addr), AR_QCMDV_TELL_BLK);
2766                 if (c!=TE_OK)
2767                         return -EIO;
2768 
2769                 ioctl_tell.mt_blkno = (blk_addr[3] << 16) | (blk_addr[4] << 8) | blk_addr[5];
2770 
2771                 /* copy results to user space */
2772                 stp = (char *) &ioctl_tell;
2773                 argp = (char *) ioarg;
2774                 for (i=0; i<sizeof(ioctl_tell); i++) 
2775                         put_user_byte(*stp++, argp++);
2776                 return 0;
2777 
2778         } else
2779                 return -ENOTTY; /* Other cmds not supported. */
2780 } /* qic02_tape_ioctl */
2781 
2782 
2783 
2784 /* These are (most) of the interface functions: */
2785 static struct file_operations qic02_tape_fops = {
2786         qic02_tape_lseek,               /* not allowed */
2787         qic02_tape_read,                /* read */
2788         qic02_tape_write,               /* write */
2789         NULL,                           /* readdir not allowed */
2790         NULL,                           /* select ??? */
2791         qic02_tape_ioctl,               /* ioctl */
2792         NULL,                           /* mmap not allowed */
2793         qic02_tape_open,                /* open */
2794         qic02_tape_release,             /* release */
2795         NULL,                           /* fsync */
2796         NULL,                           /* fasync */
2797         NULL,                           /* check_media_change */
2798         NULL                            /* revalidate */
2799 };
2800 
2801 /* align `a' at `size' bytes. `size' must be a power of 2 */
2802 static inline unsigned long const align_buffer(unsigned long a, unsigned size)
     /* [previous][next][first][last][top][bottom][index][help] */
2803 {
2804         if (a & (size-1))                       /* if not aligned */
2805                 return (a | (size-1)) + 1;
2806         else                                    /* else is aligned */
2807                 return a;
2808 }
2809 
2810 
2811 
2812 static void qic02_release_resources(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2813 {
2814         free_irq(QIC02_TAPE_IRQ);
2815         free_dma(QIC02_TAPE_DMA);
2816         status_zombie = YES;
2817 } /* qic02_release_resources */
2818 
2819 
2820 
2821 
2822 static int qic02_get_resources(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2823 {
2824         /* First perform some checks. If one of them fails,
2825          * the tape driver will not be registered to the system.
2826          */
2827         if (QIC02_TAPE_IRQ>16) {
2828                 tpqputs(TPQD_ALWAYS, "Bogus interrupt number.");
2829                 return -1;
2830         }
2831 
2832         /* for DYNCONF, allocating DMA & IRQ should not be done until 
2833          * the config parameters have been set using MTSETCONFIG.
2834          */
2835 
2836         /* get IRQ */
2837         if (request_irq(QIC02_TAPE_IRQ, qic02_tape_interrupt, SA_INTERRUPT, "QIC-02")) {
2838                 printk(TPQIC02_NAME ": can't allocate IRQ%d for QIC-02 tape\n",
2839                         QIC02_TAPE_IRQ);
2840                 status_zombie = YES;
2841                 return -1;
2842         }
2843 
2844         /* After IRQ, allocate DMA channel */
2845         if (request_dma(QIC02_TAPE_DMA,"QIC-02")) {
2846                 printk(TPQIC02_NAME ": can't allocate DMA%d for QIC-02 tape\n",
2847                         QIC02_TAPE_DMA);
2848                 free_irq(QIC02_TAPE_IRQ);
2849                 status_zombie = YES;
2850                 return -1;
2851         }
2852 
2853         printk(TPQIC02_NAME ": Settings: IRQ %d, DMA %d, IO 0x%x, IFC %s\n",
2854                 QIC02_TAPE_IRQ, QIC02_TAPE_DMA,
2855                 ((QIC02_TAPE_IFC==ARCHIVE) || (QIC02_TAPE_IFC==MOUNTAIN))?
2856                         QIC02_CMD_PORT : QIC02_STAT_PORT,
2857                 (QIC02_TAPE_IFC==MOUNTAIN)? "Mountain" :
2858                         ((QIC02_TAPE_IFC==ARCHIVE)? "Archive" : "Wangtek"));
2859 
2860         if (tape_reset(0)!=TE_OK || tp_sense(TP_WRP|TP_POR|TP_CNI)!=TE_OK) {
2861                 /* No drive detected, so vanish */
2862                 tpqputs(TPQD_ALWAYS, "No drive detected -- releasing irq and dma.");
2863                 status_dead = YES;
2864                 qic02_release_resources();
2865                 return -1;
2866         }
2867 
2868         /* All should be ok now */
2869         status_zombie = NO;
2870         return 0;
2871 } /* qic02_get_resources */
2872 
2873 
2874 int qic02_tape_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2875         /* Shouldn't this be a caddr_t ? */
2876 {
2877 
2878         if (TPSTATSIZE != 6) {
2879                 printk(TPQIC02_NAME ": internal error: tpstatus struct incorrect!\n");
2880                 return -ENODEV;
2881         }
2882         if ((TPQBUF_SIZE<512) || (TPQBUF_SIZE>=0x10000)) {
2883                 printk(TPQIC02_NAME ": internal error: DMA buffer size out of range\n");
2884                 return -ENODEV;
2885         }
2886 
2887         QIC02_TAPE_DEBUG = TPQD_DEFAULT_FLAGS;
2888 
2889         current_tape_dev = MKDEV(QIC02_TAPE_MAJOR, 0);
2890 
2891 #ifndef CONFIG_QIC02_DYNCONF
2892         printk(TPQIC02_NAME ": IRQ %d, DMA %d, IO 0x%x, IFC %s, %s, %s\n",
2893                  QIC02_TAPE_IRQ, QIC02_TAPE_DMA,
2894 # if QIC02_TAPE_IFC == WANGTEK
2895                  QIC02_STAT_PORT, "Wangtek",
2896 # elif QIC02_TAPE_IFC == ARCHIVE
2897                  QIC02_CMD_PORT, "Archive",
2898 # elif QIC02_TAPE_IFC == MOUNTAIN
2899                  QIC02_CMD_PORT, "Mountain",
2900 # else
2901 #  error
2902 # endif
2903                  rcs_revision, rcs_date);
2904         if (qic02_get_resources())
2905                 return -ENODEV;
2906 #else
2907         printk(TPQIC02_NAME ": Runtime config, %s, %s\n", 
2908                  rcs_revision, rcs_date);
2909         
2910         QIC02_TAPE_IRQ = BOGUS_IRQ;     /* invalid value */
2911 #endif
2912 
2913         printk(TPQIC02_NAME ": DMA buffers: %u blocks", NR_BLK_BUF);
2914 
2915         /* Setup the page-address for the dma transfer.
2916          * This assumes a one-to-one identity mapping between
2917          * kernel addresses and physical memory.
2918          */
2919         buffaddr = align_buffer((unsigned long) &qic02_tape_buf, TAPE_BLKSIZE);
2920         printk(", at address 0x%lx (0x%lx)\n", buffaddr, (unsigned long) &qic02_tape_buf);
2921 
2922 #ifndef CONFIG_MAX_16M
2923         if (buffaddr+TPQBUF_SIZE>=0x1000000) {
2924                 printk(TPQIC02_NAME ": DMA buffer *must* be in lower 16MB\n");
2925                 return -ENODEV;
2926         }
2927 #endif
2928 
2929         /* If we got this far, install driver functions */
2930         if (register_chrdev(QIC02_TAPE_MAJOR, TPQIC02_NAME, &qic02_tape_fops)) {
2931                 printk(TPQIC02_NAME ": Unable to get chrdev major %d\n", QIC02_TAPE_MAJOR);
2932 #ifndef CONFIG_QIC02_DYNCONF
2933                 free_irq(QIC02_TAPE_IRQ);
2934                 free_dma(QIC02_TAPE_DMA);
2935 #endif
2936                 return -ENODEV;
2937         }
2938 
2939         /* prepare timer */
2940         TIMEROFF;
2941         timer_table[QIC02_TAPE_TIMER].expires = 0;
2942         timer_table[QIC02_TAPE_TIMER].fn = qic02_tape_times_out;
2943 
2944 #ifndef CONFIG_QIC02_DYNCONF
2945         if (tape_reset(0)!=TE_OK || tp_sense(TP_WRP|TP_POR|TP_CNI)!=TE_OK) {
2946                 /* No drive detected, so vanish */
2947                 tpqputs(TPQD_ALWAYS, "No drive detected -- driver going on vacation...");
2948                 status_dead = YES;
2949                 free_irq(QIC02_TAPE_IRQ);
2950                 free_dma(QIC02_TAPE_DMA);
2951                 unregister_chrdev(QIC02_TAPE_MAJOR, TPQIC02_NAME);
2952                 return -ENODEV;
2953         } else {
2954                 if (is_exception()) {
2955                         tpqputs(TPQD_ALWAYS, "exception detected\n");
2956                         (void) tp_sense(TP_WRP|TP_POR|TP_CNI);
2957                 }
2958         }
2959 #endif
2960 
2961         /* initialize generic status for ioctl requests */
2962 
2963         ioctl_status.mt_type    = QIC02_TAPE_DRIVE;     /* MT_IS* id nr */
2964 
2965         ioctl_status.mt_resid   = 0;    /* ---residual count */
2966         ioctl_status.mt_gstat   = 0;    /* ---generic status */
2967         ioctl_status.mt_erreg   = 0;    /* not used */
2968         ioctl_status.mt_fileno  = 0;    /* number of current file on tape */
2969         ioctl_status.mt_blkno   = 0;    /* number of current (logical) block */
2970 
2971         return 0;
2972 } /* qic02_tape_init */
2973 

/* [previous][next][first][last][top][bottom][index][help] */