root/drivers/char/tpqic02.c

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

DEFINITIONS

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

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

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