root/drivers/char/tpqic02.c

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

DEFINITIONS

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

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

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