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_readdir
  34. qic02_tape_release
  35. update_ifc_masks
  36. qic02_tape_ioctl
  37. align_buffer
  38. qic02_release_resources
  39. qic02_get_resources
  40. qic02_tape_init

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

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