root/drivers/char/tpqic02.c

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

DEFINITIONS

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

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

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