Import 2.3.10pre5
[davej-history.git] / drivers / char / tpqic02.c
blob53b90d071b01130b93914823b818762b3ccf56b1
1 /* $Id: tpqic02.c,v 1.10 1997/01/26 07:13:20 davem Exp $
3 * Driver for tape drive support for Linux-i386
5 * Copyright (c) 1992--1996 by H. H. Bergman. All rights reserved.
6 * Current e-mail address: hennus@cybercomm.nl
8 * Distribution of this program in executable form is only allowed if
9 * all of the corresponding source files are made available through the same
10 * medium at no extra cost.
12 * I will not accept any responsibility for damage caused directly or
13 * indirectly by this program, or code derived from this program.
15 * Use this code at your own risk. Don't blame me if it destroys your data!
16 * Make sure you have a backup before you try this code.
18 * If you make changes to my code and redistribute it in source or binary
19 * form you must make it clear to even casual users of your code that you
20 * have modified my code, clearly point out what the changes exactly are
21 * (preferably in the form of a context diff file), how to undo your changes,
22 * where the original can be obtained, and that complaints/requests about the
23 * modified code should be directed to you instead of me.
25 * This driver was partially inspired by the 'wt' driver in the 386BSD
26 * source distribution, which carries the following copyright notice:
28 * Copyright (c) 1991 The Regents of the University of California.
29 * All rights reserved.
31 * You are not allowed to change this line nor the text above.
33 * 1996/10/10 Emerald changes
35 * 1996/05/21 Misc changes+merges+cleanups + I/O reservations
37 * 1996/05/20 Module support patches submitted by Brian McCauley.
39 * 1994/05/03 Initial attempt at Mountain support for the Mountain 7150.
40 * Based on patches provided by Erik Jacobson. Still incomplete, I suppose.
42 * 1994/02/07 Archive changes & some cleanups by Eddy Olk.
44 * 1994/01/19 Speed measuring stuff moved from aperf.h to delay.h.
45 * BogoMips (tm) introduced by Linus.
47 * 1993/01/25 Kernel udelay. Eof fixups.
49 * 1992/09/19 Some changes based on patches by Eddy Olk to support
50 * Archive SC402/SC499R controller cards.
52 * 1992/05/27 First release.
54 * 1992/05/26 Initial version. Copyright H. H. Bergman 1992
57 /* After the legalese, now the important bits:
59 * This is a driver for the Wangtek 5150 tape drive with
60 * a QIC-02 controller for ISA-PC type computers.
61 * Hopefully it will work with other QIC-02 tape drives as well.
63 * Make sure your setup matches the configuration parameters.
64 * Also, be careful to avoid IO conflicts with other devices!
69 #define TDEBUG
72 #define REALLY_SLOW_IO /* it sure is ... */
74 #include <linux/module.h>
76 #include <linux/config.h>
78 #include <linux/sched.h>
79 #include <linux/timer.h>
80 #include <linux/fs.h>
81 #include <linux/kernel.h>
82 #include <linux/major.h>
83 #include <linux/errno.h>
84 #include <linux/mtio.h>
85 #include <linux/fcntl.h>
86 #include <linux/delay.h>
87 #include <linux/ioport.h>
88 #include <linux/tpqic02.h>
89 #include <linux/mm.h>
90 #include <linux/malloc.h>
91 #include <linux/init.h>
93 #include <asm/dma.h>
94 #include <asm/system.h>
95 #include <asm/io.h>
96 #include <asm/uaccess.h>
98 /* check existence of required configuration parameters */
99 #if !defined(QIC02_CMD_PORT) || !defined(QIC02_TAPE_IRQ) || !defined(QIC02_TAPE_DMA)
100 # error qic02_tape configuration error
101 #endif
104 #define TPQIC02_NAME "tpqic02"
106 /* Linux outb() commands have (value,port) as parameters.
107 * One might expect (port,value) instead, so beware!
110 #ifdef CONFIG_QIC02_DYNCONF
111 /* This holds the dynamic configuration info for the interface
112 * card+drive info if runtime configuration has been selected.
115 static struct mtconfiginfo qic02_tape_dynconf = /* user settable */
116 { 0, 0, BOGUS_IRQ, 0, 0, TPQD_DEFAULT_FLAGS, };
117 static struct qic02_ccb qic02_tape_ccb = { 0, }; /* private stuff */
119 #else
121 unsigned long qic02_tape_debug = TPQD_DEFAULT_FLAGS;
123 # if ((QIC02_TAPE_IFC!=WANGTEK) && (QIC02_TAPE_IFC!=ARCHIVE) && (QIC02_TAPE_IFC!=MOUNTAIN))
124 # error No valid interface card specified
125 # endif
126 #endif /* CONFIG_QIC02_DYNCONF */
128 static volatile int ctlbits = 0; /* control reg bits for tape interface */
130 static wait_queue_head_t qic02_tape_transfer; /* sync rw with interrupts */
132 static volatile struct mtget ioctl_status; /* current generic status */
134 static volatile struct tpstatus tperror; /* last drive status */
136 static char rcs_revision[] = "$Revision: 1.10 $";
137 static char rcs_date[] = "$Date: 1997/01/26 07:13:20 $";
139 /* Flag bits for status and outstanding requests.
140 * (Could all be put in one bit-field-struct.)
141 * Some variables need `volatile' because they may be modified
142 * by an interrupt.
144 static volatile flag status_dead = YES; /* device is legally dead until proven alive */
145 static flag status_zombie = YES; /* it's `zombie' until irq/dma allocated */
147 static volatile flag status_bytes_wr = NO; /* write FM at close or not */
148 static volatile flag status_bytes_rd = NO; /* (rd|wr) used for rewinding */
150 static volatile unsigned long status_cmd_pending = 0; /* cmd in progress */
151 static volatile flag status_expect_int = NO; /* ready for interrupts */
152 static volatile flag status_timer_on = NO; /* using time-out */
153 static volatile int status_error = 0; /* int handler may detect error */
154 static volatile flag status_eof_detected = NO; /* end of file */
155 static volatile flag status_eom_detected = NO; /* end of recorded media */
156 static volatile flag status_eot_detected = NO; /* end of tape */
157 static volatile flag doing_read = NO;
158 static volatile flag doing_write = NO;
160 static volatile unsigned long dma_bytes_todo;
161 static volatile unsigned long dma_bytes_done;
162 static volatile unsigned dma_mode = 0; /* !=0 also means DMA in use */
163 static flag need_rewind = YES;
165 static kdev_t current_tape_dev;
166 static int extra_blocks_left = BLOCKS_BEYOND_EW;
169 /* return_*_eof:
170 * NO: not at EOF,
171 * YES: tell app EOF was reached (return 0).
173 * return_*_eof==YES && reported_*_eof==NO ==>
174 * return current buffer, next time(s) return EOF.
176 * return_*_eof==YES && reported_*_eof==YES ==>
177 * at EOF and application knows it, so we can
178 * move on to the next file.
181 static flag return_read_eof = NO; /* set to signal app EOF was reached */
182 static flag return_write_eof = NO;
183 static flag reported_read_eof = NO; /* set when we've done that */
184 static flag reported_write_eof = NO;
187 /* This is for doing `mt seek <blocknr>' */
188 static char seek_addr_buf[AR_SEEK_BUF_SIZE];
191 /* In write mode, we have to write a File Mark after the last block written,
192 * when the tape device is closed. Tape repositioning and reading in write
193 * mode is allowed as long as no actual writing has been done. After writing
194 * the File Mark, repositioning and reading are allowed again.
196 static int mode_access; /* access mode: READ or WRITE */
198 static int qic02_get_resources(void);
199 static void qic02_release_resources(void);
201 /* This is a pointer to the actual kernel buffer where the interrupt routines
202 * read from/write to. It is needed because the DMA channels 1 and 3 cannot
203 * always access the user buffers. [The kernel buffer must reside in the
204 * lower 16MBytes of system memory because of the DMA controller.] The user
205 * must ensure that a large enough buffer is passed to the kernel, in order
206 * to reduce tape repositioning wear and tear.
208 static unsigned long buffaddr = 0; /* physical address of buffer */
210 /* This translates minor numbers to the corresponding recording format: */
211 static const char *format_names[] = {
212 "not set", /* for dumb drives unable to handle format selection */
213 "11", /* extinct */
214 "24",
215 "120",
216 "150",
217 "300", /* untested. */
218 "600" /* untested. */
222 /* `exception_list' is needed for exception status reporting.
223 * Exceptions 1..14 are defined by QIC-02 rev F.
224 * The drive status is matched sequentially to each entry,
225 * ignoring irrelevant bits, until a match is found. If no
226 * match is found, exception number 0 is used. (That should of
227 * course never happen...) The original table was based on the
228 * "Exception Status Summary" in QIC-02 rev F, but some changes
229 * were required to make it work with real-world drives.
231 * Exception 2 (CNI) is changed to also cover status 0x00e0 (mask USL),
232 * Exception 4 (EOM) is changed to also cover status 0x8288 (mask EOR),
233 * Exception 11 (FIL) is changed to also cover status 0x0089 (mask EOM).
234 * Exception 15 (EOR) is added for seek-to-end-of-data (catch EOR),
235 * Exception 16 (BOM) is added for beginning-of-media (catch BOM).
237 * Had to swap EXC_NDRV and EXC_NCART to ensure that extended EXC_NCART
238 * (because of the incorrect Wangtek status code) doesn't catch the
239 * EXC_NDRV first.
241 static struct exception_list_type {
242 unsigned short mask, code;
243 const char *msg;
244 /* EXC_nr attribute should match with tpqic02.h */
245 } exception_list[] = {
246 {0, 0,
247 "Unknown exception status code", /* extra: 0 */},
248 {~(0), TP_ST0|TP_CNI|TP_USL|TP_WRP,
249 "Drive not online" /* 1 */},
250 /* Drive presence goes before cartridge presence. */
251 {~(TP_WRP|TP_USL), TP_ST0|TP_CNI,
252 /* My Wangtek 5150EQ sometimes reports a status code
253 * of 0x00e0, which is not a valid exception code, but
254 * I think it should be recognized as "NO CARTRIDGE".
256 "Cartridge not in place" /* 2 */},
257 {(unsigned short) ~(TP_ST1|TP_BOM), (TP_ST0|TP_WRP),
258 "Write protected cartridge" /* 3 */},
259 {(unsigned short) ~(TP_ST1|TP_EOR), (TP_ST0|TP_EOM),
260 "End of media" /* 4 */},
261 {~TP_WRP, TP_ST0|TP_UDA| TP_ST1|TP_BOM,
262 "Read or Write abort. Rewind tape." /* 5 */},
263 {~TP_WRP, TP_ST0|TP_UDA,
264 "Read error. Bad block transferred." /* 6 */},
265 {~TP_WRP, TP_ST0|TP_UDA|TP_BNL,
266 "Read error. Filler block transferred." /* 7 */},
267 {~TP_WRP, TP_ST0|TP_UDA|TP_BNL |TP_ST1|TP_NDT,
268 "Read error. No data detected." /* 8 */},
269 {~TP_WRP, TP_ST0|TP_EOM|TP_UDA|TP_BNL |TP_ST1|TP_NDT,
270 "Read error. No data detected. EOM." /* 9 */},
271 {~(TP_WRP|TP_MBD|TP_PAR|TP_EOR), TP_ST0|TP_UDA|TP_BNL |TP_ST1|TP_NDT|TP_BOM,
272 "Read error. No data detected. BOM." /* 10 */},
273 {~(TP_WRP|TP_EOM), TP_ST0|TP_FIL,
274 /* Status 0x0089 (EOM & FM) is viewed as an FM,
275 * because it can only happen during a read.
276 * EOM is checked separately for an FM condition.
278 "File mark detected" /* 11 */},
279 {~(TP_ST0|TP_CNI|TP_USL|TP_WRP|TP_BOM), TP_ST1|TP_ILL,
280 "Illegal command" /* 12 */},
281 {~(TP_ST0|TP_CNI|TP_USL|TP_WRP|TP_BOM), TP_ST1|TP_POR,
282 "Reset occurred" /* 13 */},
283 {~TP_WRP, TP_ST0|TP_FIL|TP_MBD, /* NOTE: ST1 not set! */
284 "Marginal block detected" /* 14 */},
285 {~(TP_ST0|TP_WRP|TP_EOM|TP_UDA|TP_BNL|TP_FIL |TP_NDT), TP_ST1|TP_EOR,
286 /********** Is the extra TP_NDT really needed Eddy? **********/
287 "End of recorded media" /* extra: 15 */},
288 /* 15 is returned when SEEKEOD completes successfully */
289 {~(TP_WRP|TP_ST0), TP_ST1|TP_BOM,
290 "Beginning of media" /* extra: 16 */}
292 #define NR_OF_EXC (sizeof(exception_list)/sizeof(struct exception_list_type))
294 /* Compare expected struct size and actual struct size. This
295 * is useful to catch programs compiled with old #includes.
297 #define CHECK_IOC_SIZE(structure) \
298 if (_IOC_SIZE(iocmd) != sizeof(struct structure)) { \
299 tpqputs(TPQD_ALWAYS, "sizeof(struct " #structure \
300 ") does not match!"); \
301 return -EFAULT; \
304 static void tpqputs(unsigned long flags, const char *s)
306 if ((flags & TPQD_ALWAYS) || (flags & QIC02_TAPE_DEBUG))
307 printk(TPQIC02_NAME ": %s\n", s);
308 } /* tpqputs */
311 /* Perform byte order swapping for a 16-bit word.
313 * [FIXME] This should probably be in include/asm/
314 * ([FIXME] i486 can do this faster)
316 static inline void byte_swap_w(volatile unsigned short * w)
318 int t = *w;
320 *w = (t>>8) | ((t & 0xff)<<8);
325 /* Init control register bits on interface card.
326 * For Archive, interrupts must be enabled explicitly.
327 * Wangtek interface card requires ONLINE to be set, Archive SC402/SC499R
328 * cards keep it active all the time.
330 static void ifc_init(void)
332 if (QIC02_TAPE_IFC == WANGTEK) /* || (QIC02_TAPE_IFC == EVEREX) */
334 ctlbits = WT_CTL_ONLINE; /* online */
335 outb_p(ctlbits, QIC02_CTL_PORT);
337 else if (QIC02_TAPE_IFC == ARCHIVE)
339 ctlbits = 0; /* no interrupts yet */
340 outb_p(ctlbits, QIC02_CTL_PORT);
341 outb_p(0, AR_RESET_DMA_PORT); /* dummy write to reset DMA */
343 else /* MOUNTAIN */
345 ctlbits = MTN_CTL_ONLINE; /* online, and logic enabled */
346 outb_p(ctlbits, QIC02_CTL_PORT);
348 } /* ifc_init */
351 static void report_qic_exception(unsigned n)
353 if (n >= NR_OF_EXC)
355 tpqputs(TPQD_ALWAYS, "Oops -- report_qic_exception");
356 n = 0;
358 if (TPQDBG(SENSE_TEXT) || n==0)
360 printk(TPQIC02_NAME ": sense: %s\n", exception_list[n].msg);
362 } /* report_qic_exception */
365 /* Try to map the drive-exception bits `s' to a predefined "exception number",
366 * by comparing the significant exception bits for each entry in the
367 * exception table (`exception_list[]').
368 * It is assumed that s!=0.
370 static int decode_qic_exception_nr(unsigned s)
372 int i;
374 for (i=1; i<NR_OF_EXC; i++)
376 if ((s & exception_list[i].mask)==exception_list[i].code)
378 return i;
381 printk(TPQIC02_NAME ": decode_qic_exception_nr: exception(%x) not recognized\n", s);
382 return 0;
383 } /* decode_qic_exception_nr */
387 /* Perform appropriate action for certain exceptions.
388 * should return a value to indicate stop/continue (in case of bad blocks)
390 static void handle_qic_exception(int exnr, int exbits)
392 if (exnr==EXC_NCART)
394 /* Cartridge was changed. Redo sense().
395 * EXC_NCART should be handled in open().
396 * It is not permitted to remove the tape while
397 * the tape driver has open files.
399 need_rewind = YES;
400 status_eof_detected = NO;
401 status_eom_detected = NO;
403 else if (exnr==EXC_XFILLER)
405 tpqputs(TPQD_ALWAYS, "[Bad block -- filler data transferred.]");
407 else if (exnr==EXC_XBAD)
409 tpqputs(TPQD_ALWAYS, "[CRC failed!]");
411 else if (exnr==EXC_MARGINAL)
413 /* A marginal block behaves much like a FM.
414 * User may continue reading, if desired.
416 tpqputs(TPQD_ALWAYS, "[Marginal block]");
417 doing_read = NO;
419 else if (exnr==EXC_FM)
421 doing_read = NO;
423 } /* handle_qic_exception */
426 static inline int is_exception(void)
428 return (inb(QIC02_STAT_PORT) & QIC02_STAT_EXCEPTION) == 0;
429 } /* is_exception */
432 /* Reset the tape drive and controller.
433 * When reset fails, it marks the drive as dead and all
434 * requests (except reset) are to be ignored (ENXIO).
436 static int tape_reset(int verbose)
438 ifc_init(); /* reset interface card */
440 /* assert reset */
441 if (QIC02_TAPE_IFC == MOUNTAIN)
443 outb_p(ctlbits & ~MTN_QIC02_CTL_RESET_NOT, QIC02_CTL_PORT);
445 else /* WANGTEK, ARCHIVE */
447 outb_p(ctlbits | QIC02_CTL_RESET, QIC02_CTL_PORT);
450 /* Next, we need to wait >=25 usec. */
451 udelay(30);
453 /* after reset, we will be at BOT (modulo an automatic rewind) */
454 status_eof_detected = NO;
455 status_eom_detected = NO;
456 status_cmd_pending = 0;
457 need_rewind = YES;
458 doing_read = doing_write = NO;
459 ioctl_status.mt_fileno = ioctl_status.mt_blkno = 0;
461 /* de-assert reset */
462 if (QIC02_TAPE_IFC == MOUNTAIN)
464 outb_p(ctlbits | MTN_QIC02_CTL_RESET_NOT, QIC02_CTL_PORT);
466 else
468 outb_p(ctlbits & ~QIC02_CTL_RESET, QIC02_CTL_PORT);
471 /* KLUDGE FOR G++ BUG */
472 { int stat = inb_p(QIC02_STAT_PORT);
473 status_dead = ((stat & QIC02_STAT_RESETMASK) != QIC02_STAT_RESETVAL); }
474 /* if successful, inb(STAT) returned RESETVAL */
475 if (status_dead == YES)
477 printk(TPQIC02_NAME ": reset failed!\n");
479 else if (verbose)
481 printk(TPQIC02_NAME ": reset successful\n");
484 return (status_dead == YES)? TE_DEAD : TE_OK;
485 } /* tape_reset */
489 /* Notify tape drive of a new command. It only waits for the
490 * command to be accepted, not for the actual command to complete.
492 * Before calling this routine, QIC02_CMD_PORT must have been loaded
493 * with the command to be executed.
494 * After this routine, the exception bit must be checked.
495 * This routine is also used by rdstatus(), so in that case, any exception
496 * must be ignored (`ignore_ex' flag).
498 static int notify_cmd(char cmd, short ignore_ex)
500 int i;
502 outb_p(cmd, QIC02_CMD_PORT); /* output the command */
504 /* wait 1 usec before asserting /REQUEST */
505 udelay(1);
507 if ((!ignore_ex) && is_exception()) {
508 tpqputs(TPQD_ALWAYS, "*** exception detected in notify_cmd");
509 /** force a reset here **/
510 if (tape_reset(1)==TE_DEAD)
511 return TE_DEAD;
512 if (is_exception()) {
513 tpqputs(TPQD_ALWAYS, "exception persists after reset.");
514 tpqputs(TPQD_ALWAYS, " ^ exception ignored.");
518 outb_p(ctlbits | QIC02_CTL_REQUEST, QIC02_CTL_PORT); /* set request bit */
519 i = TAPE_NOTIFY_TIMEOUT;
520 /* The specs say this takes about 500 usec, but there is no upper limit!
521 * If the drive were busy retensioning or something like that,
522 * it could be *much* longer!
524 while ((inb_p(QIC02_STAT_PORT) & QIC02_STAT_READY) && (--i>0))
525 /*skip*/; /* wait for ready */
526 if (i==0) {
527 tpqputs(TPQD_ALWAYS, "timed out waiting for ready in notify_cmd");
528 status_dead = YES;
529 return TE_TIM;
532 outb_p(ctlbits & ~QIC02_CTL_REQUEST, QIC02_CTL_PORT); /* reset request bit */
533 i = TAPE_NOTIFY_TIMEOUT;
534 /* according to the specs this one should never time-out */
535 while (((inb_p(QIC02_STAT_PORT) & QIC02_STAT_READY) == 0) && (--i>0))
536 /*skip*/; /* wait for not ready */
537 if (i==0) {
538 tpqputs(TPQD_ALWAYS, "timed out waiting for !ready in notify_cmd");
539 status_dead = YES;
540 return TE_TIM;
542 /* command accepted */
543 return TE_OK;
544 } /* notify_cmd */
548 /* Wait for a command to complete, with timeout */
549 static int wait_for_ready(time_t timeout)
551 int stat;
552 time_t spin_t;
554 /* Wait for ready or exception, without driving the loadavg up too much.
555 * In most cases, the tape drive already has READY asserted,
556 * so optimize for that case.
558 * First, busy wait a few usec:
560 spin_t = 50;
561 while (((stat = inb_p(QIC02_STAT_PORT) & QIC02_STAT_MASK) == QIC02_STAT_MASK) && (--spin_t>0))
562 /*SKIP*/;
563 if ((stat & QIC02_STAT_READY) == 0)
564 return TE_OK; /* covers 99.99% of all calls */
566 /* Then use schedule() a few times */
567 spin_t = 3; /* max 0.03 sec busy waiting */
568 if (spin_t > timeout)
569 spin_t = timeout;
570 timeout -= spin_t;
571 spin_t += jiffies;
573 while (((stat = inb_p(QIC02_STAT_PORT) & QIC02_STAT_MASK) == QIC02_STAT_MASK) && time_before(jiffies, spin_t))
574 schedule(); /* don't waste all the CPU time */
575 if ((stat & QIC02_STAT_READY) == 0)
576 return TE_OK;
578 /* If we reach this point, we probably need to wait much longer, or
579 * an exception occurred. Either case is not very time-critical.
580 * Check the status port only a few times every second.
581 * A interval of less than 0.10 sec will not be noticed by the user,
582 * more than 0.40 sec may give noticeable delays.
584 spin_t += timeout;
585 TPQDEB({printk("wait_for_ready: additional timeout: %d\n", spin_t);})
587 /* not ready and no exception && timeout not expired yet */
588 while (((stat = inb_p(QIC02_STAT_PORT) & QIC02_STAT_MASK) == QIC02_STAT_MASK) && time_before(jiffies, spin_t)) {
589 /* be `nice` to other processes on long operations... */
590 current->state = TASK_INTERRUPTIBLE;
591 /* nap 0.30 sec between checks, */
592 /* but could be woken up earlier by signals... */
593 schedule_timeout(3*HZ/10);
596 /* don't use jiffies for this test because it may have changed by now */
597 if ((stat & QIC02_STAT_MASK) == QIC02_STAT_MASK) {
598 tpqputs(TPQD_ALWAYS, "wait_for_ready() timed out");
599 return TE_TIM;
602 if ((stat & QIC02_STAT_EXCEPTION) == 0) {
603 tpqputs(TPQD_ALWAYS, "exception detected after waiting_for_ready");
604 return TE_EX;
605 } else {
606 return TE_OK;
608 } /* wait_for_ready */
612 /* Send some data to the drive */
613 static int send_qic02_data(char sb[], unsigned size, int ignore_ex)
615 int i, stat;
617 for (i=0; i<size; i++) {
619 stat = wait_for_ready(TIM_S);
620 if (stat != TE_OK)
621 return stat;
623 stat = notify_cmd(sb[i], ignore_ex);
624 if (stat != TE_OK)
625 return stat;
627 return TE_OK;
629 } /* send_qic02_data */
632 /* Send a QIC-02 command (`cmd') to the tape drive, with
633 * a time-out (`timeout').
634 * This one is also used by tp_sense(), so we must have
635 * a flag to disable exception checking (`ignore_ex').
637 * On entry, the controller is supposed to be READY.
639 static int send_qic02_cmd(int cmd, time_t timeout, int ignore_ex)
641 int stat;
643 stat = inb_p(QIC02_STAT_PORT);
644 if ((stat & QIC02_STAT_EXCEPTION) == 0) { /* if exception */
645 tpqputs(TPQD_ALWAYS, "send_qic02_cmd: Exception!");
646 return TE_EX;
648 if (stat & QIC02_STAT_READY) { /* if not ready */
649 tpqputs(TPQD_ALWAYS, "send_qic02_cmd: not Ready!");
650 return TE_ERR;
653 /* assert(ready & !exception) */
655 /* Remember current command for later re-use with dma transfers.
656 * (For reading/writing multiple blocks.)
658 status_cmd_pending = cmd;
660 stat = notify_cmd(cmd, ignore_ex); /* tell drive new command was loaded, */
661 /* inherit exception check. */
662 if (TP_HAVE_SEEK && (cmd == AR_QCMDV_SEEK_BLK)) {
663 /* This one needs to send 3 more bytes, MSB first */
664 stat = send_qic02_data(seek_addr_buf, sizeof(seek_addr_buf), ignore_ex);
667 if (stat != TE_OK) {
668 tpqputs(TPQD_ALWAYS, "send_qic02_cmd failed");
670 return stat;
671 } /* send_qic02_cmd */
675 /* Get drive status. Assume drive is ready or has exception set.
676 * (or will be in <1000 usec.)
677 * Extra parameters added because of 'Read Extended Status 3' command.
679 static int rdstatus(char *stp, unsigned size, char qcmd)
681 int s, n;
682 char *q = stp;
684 /* Try to busy-wait a few (700) usec, after that de-schedule.
686 * The problem is, if we don't de-schedule, performance will
687 * drop to zero when the drive is not responding and if we
688 * de-schedule immediately, we waste a lot of time because a
689 * task switch is much longer than we usually have to wait here.
691 n = 1000; /* 500 is not enough on a 486/33 */
692 while ((n>0) && ((inb_p(QIC02_STAT_PORT) & QIC02_STAT_MASK) == QIC02_STAT_MASK))
693 n--; /* wait for ready or exception or timeout */
694 if (n==0) {
695 /* n (above) should be chosen such that on your machine
696 * you rarely ever see the message below, and it should
697 * be small enough to give reasonable response time.]
699 tpqputs(TPQD_ALWAYS, "waiting looong in rdstatus() -- drive dead?");
700 while ((inb_p(QIC02_STAT_PORT) & QIC02_STAT_MASK) == QIC02_STAT_MASK)
701 schedule();
702 tpqputs(TPQD_ALWAYS, "finished waiting in rdstatus()");
705 (void) notify_cmd(qcmd, 1); /* send read status command */
706 /* ignore return code -- should always be ok, STAT may contain
707 * exception flag from previous exception which we are trying to clear.
710 if (TP_DIAGS(current_tape_dev))
711 printk(TPQIC02_NAME ": reading status bytes: ");
713 for (q=stp; q<stp+size; q++)
715 do s = inb_p(QIC02_STAT_PORT);
716 while ((s & QIC02_STAT_MASK) == QIC02_STAT_MASK); /* wait for ready or exception */
718 if ((s & QIC02_STAT_EXCEPTION) == 0) { /* if exception */
719 tpqputs(TPQD_ALWAYS, "rdstatus: exception error");
720 ioctl_status.mt_erreg = 0; /* dunno... */
721 return TE_NS; /* error, shouldn't happen... */
724 *q = inb_p(QIC02_DATA_PORT); /* read status byte */
726 if (TP_DIAGS(current_tape_dev))
727 printk("[%1d]=0x%x ", q-stp, (unsigned) (*q) & 0xff);
729 outb_p(ctlbits | QIC02_CTL_REQUEST, QIC02_CTL_PORT); /* set request */
731 while ((inb_p(QIC02_STAT_PORT) & QIC02_STAT_READY) == 0); /* wait for not ready */
733 udelay(22); /* delay >20 usec */
735 outb_p(ctlbits & ~QIC02_CTL_REQUEST, QIC02_CTL_PORT); /* un-set request */
739 /* Specs say we should wait for READY here.
740 * My drive doesn't seem to need it here yet, but others do?
742 while (inb_p(QIC02_STAT_PORT) & QIC02_STAT_READY)
743 /*skip*/; /* wait for ready */
745 if (TP_DIAGS(current_tape_dev))
746 printk("\n");
748 return TE_OK;
749 } /* rdstatus */
753 /* Get standard status (6 bytes).
754 * The `.dec' and `.urc' fields are in MSB-first byte-order,
755 * so they have to be swapped first.
757 static int get_status(volatile struct tpstatus *stp)
759 int stat = rdstatus((char *) stp, TPSTATSIZE, QCMD_RD_STAT);
760 #if defined(i386) || defined(i486)
761 byte_swap_w(&(stp->dec));
762 byte_swap_w(&(stp->urc));
763 #else
764 /* should probably swap status bytes #definition */
765 #endif
766 return stat;
767 } /* get_status */
770 #if 0
771 /* This fails for my Wangtek drive */
772 /* get "Extended Status Register 3" (64 bytes)
774 * If the meaning of the returned bytes were known, the MT_TYPE
775 * identifier could be used to decode them, since they are
776 * "vendor unique". :-(
778 static int get_ext_status3(void)
780 char vus[64]; /* vendor unique status */
781 int stat, i;
783 tpqputs(TPQD_ALWAYS, "Attempting to read Extended Status 3...");
784 stat = rdstatus(vus, sizeof(vus), QCMD_RD_STAT_X3);
785 if (stat != TE_OK)
786 return stat;
788 tpqputs(TPQD_ALWAYS, "Returned status bytes:");
789 for (i=0; i<sizeof(vus); i++) {
790 if ( i % 8 == 0 )
791 printk("\n" TPQIC02_NAME ": %2d:");
792 printk(" %2x", vus[i] & 0xff);
794 printk("\n");
796 return TE_OK;
797 } /* get_ext_status3 */
798 #endif
801 /* Read drive status and set generic status too.
802 * NOTE: Once we do a tp_sense(), read/write transfers are killed.
804 static int tp_sense(int ignore)
806 unsigned err = 0, exnr = 0, gs = 0;
807 static void finish_rw(int cmd);
809 if (TPQDBG(SENSE_TEXT))
810 printk(TPQIC02_NAME ": tp_sense(ignore=0x%x) enter\n", ignore);
812 /* sense() is not allowed during a read or write cycle */
813 if (doing_write == YES)
814 tpqputs(TPQD_ALWAYS, "Warning: File Mark inserted because of sense() request");
815 /* The extra test is to avoid calling finish_rw during booting */
816 if ((doing_read!=NO) || (doing_write!=NO))
817 finish_rw(QCMD_RD_STAT);
819 if (get_status(&tperror) != TE_OK) {
820 tpqputs(TPQD_ALWAYS, "tp_sense: could not read tape drive status");
821 return TE_ERR;
824 err = tperror.exs; /* get exception status bits */
825 if (err & (TP_ST0|TP_ST1))
826 printk(TPQIC02_NAME ": tp_sense: status: %x, error count: %d, underruns: %d\n",
827 tperror.exs, tperror.dec, tperror.urc);
828 else if ((tperror.dec!=0) || (tperror.urc!=0) || TPQDBG(SENSE_CNTS))
829 printk(TPQIC02_NAME ": tp_sense: no hard errors, soft error count: %d, underruns: %d\n",
830 tperror.dec, tperror.urc);
832 /* Set generic status. HP-UX defines these, but some extra would
833 * be useful. Problem is to remain compatible. [Do we want to be
834 * compatible??]
836 if (err & TP_ST0) {
837 if (err & TP_CNI) /* no cartridge */
838 gs |= GMT_DR_OPEN(-1);
839 if (status_dead == NO)
840 gs |= GMT_ONLINE(-1); /* always online */
841 if (err & TP_USL) /* not online */
842 gs &= ~GMT_ONLINE(-1);
843 if (err & TP_WRP)
844 gs |= GMT_WR_PROT(-1);
845 if (err & TP_EOM) { /* end of media */
846 gs |= GMT_EOT(-1); /* not sure this is correct for writes */
847 status_eom_detected = YES;
848 /* I don't know whether drive always reports EOF at or before EOM. */
849 status_eof_detected = YES;
851 /** if (err & TP_UDA) "Unrecoverable data error" **/
852 /** if (err & TP_BNL) "Bad block not located" **/
853 if (err & TP_FIL) {
854 gs |= GMT_EOF(-1);
855 status_eof_detected = YES;
858 if (err & TP_ST1) {
859 /** if (err & TP_ILL) "Illegal command" **/
860 /** if (err & TP_NDT) "No data detected" **/
861 /** if (err & TP_MBD) "Marginal block detected" **/
862 if (err & TP_BOM)
863 gs |= GMT_BOT(-1); /* beginning of tape */
865 ioctl_status.mt_gstat = gs;
866 ioctl_status.mt_dsreg = tperror.exs; /* "drive status" */
867 ioctl_status.mt_erreg = tperror.dec; /* "sense key error" */
869 if (err & (TP_ST0|TP_ST1)) {
870 /* My Wangtek occasionally reports `status' 1212 which should be ignored. */
871 exnr = decode_qic_exception_nr(err);
872 handle_qic_exception(exnr, err); /* update driver state wrt drive status */
873 report_qic_exception(exnr);
875 err &= ~ignore; /* mask unwanted errors -- not the correct way, use exception nrs?? */
876 if (((err & TP_ST0) && (err & REPORT_ERR0)) ||
877 ((err & TP_ST1) && (err & REPORT_ERR1)))
878 return TE_ERR;
879 return TE_OK;
880 } /* tp_sense */
884 /* Wait for a wind or rewind operation to finish or
885 * to time-out. (May take very long).
887 static int wait_for_rewind(time_t timeout)
889 int stat;
891 stat = inb(QIC02_STAT_PORT) & QIC02_STAT_MASK;
892 if (TPQDBG(REWIND))
893 printk(TPQIC02_NAME ": Waiting for (re-)wind to finish: stat=0x%x\n", stat);
895 stat = wait_for_ready(timeout);
897 if (stat != TE_OK) {
898 tpqputs(TPQD_ALWAYS, "(re-) winding failed\n");
900 return stat;
901 } /* wait_for_rewind */
905 /* Perform a full QIC02 command, and wait for completion,
906 * check status when done. Complain about exceptions.
908 * This function should return an OS error code when
909 * something goes wrong, 0 otherwise.
911 static int ll_do_qic_cmd(int cmd, time_t timeout)
913 int stat;
915 if (status_dead == YES) {
916 tpqputs(TPQD_ALWAYS, "Drive is dead. Do a `mt reset`.");
917 return -ENXIO; /* User should do an MTRESET. */
920 stat = wait_for_ready(timeout); /* wait for ready or exception */
921 if (stat == TE_EX) {
922 if (tp_sense(TP_WRP|TP_BOM|TP_EOM|TP_FIL)!=TE_OK)
923 return -EIO;
924 /* else nothing to worry about, I hope */
925 stat = TE_OK;
927 if (stat != TE_OK) {
928 printk(TPQIC02_NAME ": ll_do_qic_cmd(%x, %ld) failed\n", cmd, (long) timeout);
929 return -EIO;
933 #if OBSOLETE
934 /* wait for ready since it may not be active immediately after reading status */
935 while ((inb_p(QIC02_STAT_PORT) & QIC02_STAT_READY) != 0);
936 #endif
938 stat = send_qic02_cmd(cmd, timeout, 0); /* (checks for exceptions) */
940 if (cmd==QCMD_RD_FM) {
941 status_eof_detected = NO;
942 ioctl_status.mt_fileno++;
943 /* Should update block count as well, but can't.
944 * Can do a `read address' for some drives, when MTNOP is done.
946 } else if (cmd==QCMD_WRT_FM) {
947 status_eof_detected = NO;
948 ioctl_status.mt_fileno++;
949 } else if ((cmd==QCMD_REWIND) || (cmd==QCMD_ERASE) || (cmd==QCMD_RETEN)) {
950 status_eof_detected = NO;
951 status_eom_detected = NO;
952 status_eot_detected = NO;
953 need_rewind = NO;
954 ioctl_status.mt_fileno = ioctl_status.mt_blkno = 0;
955 extra_blocks_left = BLOCKS_BEYOND_EW;
956 return_write_eof = NO;
957 return_read_eof = NO;
958 reported_read_eof = NO;
959 reported_write_eof = NO;
961 /* sense() will set eof/eom as required */
962 if (stat==TE_EX) {
963 if (tp_sense(TP_WRP|TP_BOM|TP_EOM|TP_FIL)!=TE_OK) {
964 printk(TPQIC02_NAME ": Exception persist in ll_do_qic_cmd[1](%x, %ld)", cmd, (long) timeout);
965 status_dead = YES;
966 return -ENXIO;
967 /* if rdstatus fails too, we're in trouble */
970 else if (stat!=TE_OK) {
971 printk(TPQIC02_NAME ": ll_do_qic_cmd: send_qic02_cmd failed, stat = 0x%x\n", stat);
972 return -EIO; /*** -EIO is probably not always appropriate */
976 if (timeout == TIM_R)
977 stat = wait_for_rewind(timeout);
978 else
979 stat = wait_for_ready(timeout);
981 if (stat==TE_EX) {
982 if (tp_sense((cmd==QCMD_SEEK_EOD ? /*****************************/
983 TP_EOR|TP_NDT|TP_UDA|TP_BNL|TP_WRP|TP_BOM|TP_EOM|TP_FIL :
984 TP_WRP|TP_BOM|TP_EOM|TP_FIL))!=TE_OK) {
985 printk(TPQIC02_NAME ": Exception persist in ll_do_qic_cmd[2](%x, %ld)\n", cmd, (long) timeout);
986 if (cmd!=QCMD_RD_FM)
987 status_dead = YES;
988 return -ENXIO;
989 /* if rdstatus fails too, we're in trouble */
992 else if (stat!=TE_OK) {
993 printk(TPQIC02_NAME ": ll_do_qic_cmd %x: wait failed, stat == 0x%x\n", cmd, stat);
994 return -EIO;
996 return 0;
997 } /* ll_do_qic_cmd */
1001 * Problem: What to do when the user cancels a read/write operation
1002 * in-progress?
1004 * "Deactivating ONLINE during a READ also causes the"
1005 * "tape to be rewound to BOT." Ditto for WRITEs, except
1006 * a FM is written first. "The host may alternatively terminate
1007 * the READ/WRITE command by issuing a RFM/WFM command."
1009 * For READs:
1010 * Neither option will leave the tape positioned where it was.
1011 * Another (better?) solution is to terminate the READ by two
1012 * subsequent sense() operations, the first to stop the current
1013 * READ cycle, the second to clear the `Illegal command' exception,
1014 * because the QIC-02 specs didn't anticipate this. This is
1015 * delayed until actually needed, so a tar listing can be aborted
1016 * by the user and continued later.
1017 * If anybody has a better solution, let me know! [Also, let me
1018 * know if your drive (mine is a Wangtek5150EQ) does not accept
1019 * this sequence for canceling the read-cycle.]
1021 * For WRITEs it's simple: Just do a WRITE_FM, leaving the tape
1022 * positioned after the FM.
1025 static void terminate_read(int cmd)
1027 if (doing_read == YES) {
1028 doing_read = NO;
1029 if (cmd != QCMD_RD_FM) {
1030 /* if the command is a RFM, there is no need to do this
1031 * because a RFM will legally terminate the read-cycle.
1033 tpqputs(TPQD_ALWAYS, "terminating pending read-cycle");
1035 /* I'm not too sure about this part -- hhb */
1036 if (QIC02_TAPE_IFC == MOUNTAIN) {
1037 /* Mountain reference says can terminate by de-asserting online */
1038 ctlbits &= ~MTN_QIC02_CTL_ONLINE;
1041 if (tp_sense(TP_FIL|TP_EOM|TP_WRP) != TE_OK) {
1042 tpqputs(TPQD_ALWAYS, "finish_rw[read1]: ignore the 2 lines above");
1043 if (is_exception()) {
1044 if (tp_sense(TP_ILL|TP_FIL|TP_EOM|TP_WRP) != TE_OK)
1045 tpqputs(TPQD_ALWAYS, "finish_rw[read2]: read cycle error");
1050 } /* terminate_read */
1053 static void terminate_write(int cmd)
1055 int stat;
1057 if (doing_write == YES) {
1058 doing_write = NO;
1059 /* Finish writing by appending a FileMark at the end. */
1060 if (cmd != QCMD_WRT_FM) {
1061 /* finish off write cycle */
1062 stat = ll_do_qic_cmd(QCMD_WRT_FM, TIM_M);
1063 if (stat != TE_OK)
1064 tpqputs(TPQD_ALWAYS, "Couldn't finish write cycle properly");
1065 (void) tp_sense(0);
1067 /* If there is an EOF token waiting to be returned to
1068 * the (writing) application, discard it now.
1069 * We could be at EOT, so don't reset return_write_eof.
1071 reported_write_eof=YES;
1073 } /* terminate_write */
1076 /* terminate read or write cycle because of command `cmd' */
1077 static void finish_rw(int cmd)
1079 if (wait_for_ready(TIM_S) != TE_OK) {
1080 tpqputs(TPQD_ALWAYS, "error: drive not ready in finish_rw() !");
1081 return;
1083 terminate_read(cmd);
1084 terminate_write(cmd);
1085 } /* finish_rw */
1088 /* Perform a QIC command through ll_do_qic_cmd().
1089 * If necessary, rewind the tape first.
1090 * Return an OS error code if something goes wrong, 0 if all is well.
1092 static int do_qic_cmd(int cmd, time_t timeout)
1094 int stat;
1097 finish_rw(cmd);
1099 if (need_rewind) {
1100 tpqputs(TPQD_REWIND, "Rewinding tape...");
1101 stat = ll_do_qic_cmd(QCMD_REWIND, TIM_R);
1102 if (stat != 0) {
1103 printk(TPQIC02_NAME ": rewind failed in do_qic_cmd(). stat=0x%2x", stat);
1104 return stat;
1106 need_rewind = NO;
1107 if (cmd==QCMD_REWIND) /* don't wind beyond BOT ;-) */
1108 return 0;
1111 return ll_do_qic_cmd(cmd, timeout);
1112 } /* do_qic_cmd */
1115 /* Not all ioctls are supported for all drives. Some rely on
1116 * optional QIC-02 commands. Check tpqic02.h for configuration.
1117 * Some of these commands may require ONLINE to be active.
1119 static int do_ioctl_cmd(int cmd)
1121 int stat;
1123 /* It is not permitted to read or wind the tape after bytes have
1124 * been written. It is not permitted to write the tape while in
1125 * read mode.
1126 * We try to be kind and allow reading again after writing a FM...
1129 switch (cmd) {
1130 case MTRESET:
1131 /* reset verbose */
1132 return (tape_reset(1)==TE_OK)? 0 : -EIO;
1134 case MTFSF:
1135 tpqputs(TPQD_IOCTLS, "MTFSF forward searching filemark");
1136 if ((mode_access==WRITE) && status_bytes_wr)
1137 return -EACCES;
1138 return do_qic_cmd(QCMD_RD_FM, TIM_F);
1140 case MTBSF:
1141 if (TP_HAVE_BSF) {
1142 tpqputs(TPQD_IOCTLS, "MTBSF backward searching filemark -- optional command");
1143 if ((mode_access==WRITE) && status_bytes_wr)
1144 return -EACCES;
1145 stat = do_qic_cmd(QCMD_RD_FM_BCK, TIM_F);
1146 } else {
1147 stat = -ENXIO;
1149 status_eom_detected = status_eof_detected = NO;
1150 return stat;
1152 case MTFSR:
1153 if (TP_HAVE_FSR) { /* This is an optional QIC-02 command */
1154 tpqputs(TPQD_IOCTLS, "MTFSR forward space record");
1155 if ((mode_access==WRITE) && status_bytes_wr)
1156 return -EACCES;
1157 stat = do_qic_cmd(QCMD_SPACE_FWD, TIM_F);
1158 } else {
1159 /**** fake it by doing a read data block command? ******/
1160 tpqputs(TPQD_IOCTLS, "MTFSR not supported");
1161 stat = -ENXIO;
1163 return stat;
1165 case MTBSR:
1166 if (TP_HAVE_BSR) { /* This is an optional QIC-02 command */
1167 /* we need this for appending files with GNU tar!! */
1168 tpqputs(TPQD_IOCTLS, "MTFSR backward space record");
1169 if ((mode_access==WRITE) && status_bytes_wr)
1170 return -EACCES;
1171 stat = do_qic_cmd(QCMD_SPACE_BCK, TIM_F);
1172 } else {
1173 tpqputs(TPQD_IOCTLS, "MTBSR not supported");
1174 stat = -ENXIO;
1176 status_eom_detected = status_eof_detected = NO;
1177 return stat;
1179 case MTWEOF:
1180 tpqputs(TPQD_IOCTLS, "MTWEOF write eof mark");
1181 /* Plain GNU mt(1) 2.2 uses read-only mode for writing FM. :-( */
1182 if (mode_access==READ)
1183 return -EACCES;
1185 /* allow tape movement after writing FM */
1186 status_bytes_rd = status_bytes_wr; /* Kludge-O-Matic */
1187 status_bytes_wr = NO;
1188 return do_qic_cmd(QCMD_WRT_FM, TIM_M);
1189 /* not sure what to do with status_bytes when WFM should fail */
1191 case MTREW:
1192 tpqputs(TPQD_IOCTLS, "MTREW rewinding tape");
1193 if ((mode_access==WRITE) && status_bytes_wr)
1194 return -EACCES;
1195 status_eom_detected = status_eof_detected = NO;
1196 return do_qic_cmd(QCMD_REWIND, TIM_R);
1198 case MTOFFL:
1199 tpqputs(TPQD_IOCTLS, "MTOFFL rewinding & going offline");
1200 /* Doing a drive select will clear (unlock) the current drive.
1201 * But that requires support for multiple drives and locking.
1203 if ((mode_access==WRITE) && status_bytes_wr)
1204 return -EACCES;
1205 status_eom_detected = status_eof_detected = NO;
1206 /**** do rewind depending on minor bits??? ***/
1207 stat = do_qic_cmd(QCMD_REWIND, TIM_R);
1208 return stat;
1210 case MTNOP:
1211 tpqputs(TPQD_IOCTLS, "MTNOP setting status only");
1212 /********** should do `read position' for drives that support it **********/
1213 return (tp_sense(-1)==TE_OK)? 0 : -EIO; /**** check return codes ****/
1215 case MTRETEN:
1216 tpqputs(TPQD_IOCTLS, "MTRETEN retension tape");
1217 if ((mode_access==WRITE) && status_bytes_wr)
1218 return -EACCES;
1219 status_eom_detected = status_eof_detected = NO;
1220 return do_qic_cmd(QCMD_RETEN, TIM_R);
1222 case MTBSFM:
1223 /* Think think is like MTBSF, except that
1224 * we shouldn't skip the FM. Tricky.
1225 * Maybe use RD_FM_BCK, then do a SPACE_FWD?
1227 tpqputs(TPQD_IOCTLS, "MTBSFM not supported");
1228 if ((mode_access==WRITE) && status_bytes_wr)
1229 return -EACCES;
1230 return -ENXIO;
1232 case MTFSFM:
1233 /* I think this is like MTFSF, except that
1234 * we shouldn't skip the FM. Tricky.
1235 * Maybe use QCMD_RD_DATA until we get a TP_FIL exception?
1236 * But then the FM will have been skipped...
1237 * Maybe use RD_FM, then RD_FM_BCK, but not all
1238 * drives will support that!
1240 tpqputs(TPQD_IOCTLS, "MTFSFM not supported");
1241 if ((mode_access==WRITE) && status_bytes_wr)
1242 return -EACCES;
1243 return -ENXIO;
1245 case MTEOM:
1246 /* This should leave the tape ready for appending
1247 * another file to the end, such that it would append
1248 * after the last FM on tape.
1250 tpqputs(TPQD_IOCTLS, "MTEOM search for End Of recorded Media");
1251 if ((mode_access==WRITE) && status_bytes_wr)
1252 return -EACCES;
1253 if (TP_HAVE_EOD) {
1254 /* Use faster seeking when possible.
1255 * This requires the absence of data beyond the EOM.
1256 * It seems that my drive does not always perform the
1257 * SEEK_EOD correctly, unless it is preceded by a
1258 * rewind command.
1260 # if 0
1261 status_eom_detected = status_eof_detected = NO;
1262 # endif
1263 stat = do_qic_cmd(QCMD_REWIND, TIM_R);
1264 if (stat)
1265 return stat;
1266 stat = do_qic_cmd(QCMD_SEEK_EOD, TIM_F);
1267 /* After a successful seek, TP_EOR should be returned */
1268 } else {
1269 /* else just seek until the drive returns exception "No Data" */
1270 stat = 0;
1271 while ((stat==0) && (!status_eom_detected)) {
1272 stat = do_qic_cmd(QCMD_RD_FM, TIM_F); /***** should use MTFSFM here???? ******/
1274 if (tperror.exs & TP_NDT)
1275 return 0;
1277 return stat;
1279 case MTERASE:
1280 tpqputs(TPQD_IOCTLS, "MTERASE -- ERASE TAPE !");
1281 if ((tperror.exs & TP_ST0) && (tperror.exs & TP_WRP)) {
1282 tpqputs(TPQD_ALWAYS, "Cartridge is write-protected.");
1283 return -EACCES;
1284 } else {
1285 time_t t = jiffies;
1287 /* Plain GNU mt(1) 2.2 erases a tape in O_RDONLY. :-( */
1288 if (mode_access==READ)
1289 return -EACCES;
1291 /* give user a few seconds to pull out tape */
1292 while (jiffies - t < 4*HZ)
1293 schedule();
1296 /* don't bother writing filemark first */
1297 status_eom_detected = status_eof_detected = NO;
1298 return do_qic_cmd(QCMD_ERASE, TIM_R);
1300 case MTRAS1:
1301 if (TP_HAVE_RAS1) {
1302 tpqputs(TPQD_IOCTLS, "MTRAS1: non-destructive self test");
1303 stat = do_qic_cmd(QCMD_SELF_TST1, TIM_R);
1304 if (stat != 0) {
1305 tpqputs(TPQD_ALWAYS, "RAS1 failed");
1306 return stat;
1308 return (tp_sense(0)==TE_OK)? 0 : -EIO; /* get_ext_status3(); */
1310 tpqputs(TPQD_IOCTLS, "RAS1 not supported");
1311 return -ENXIO;
1313 case MTRAS2:
1314 if (TP_HAVE_RAS2) {
1315 tpqputs(TPQD_IOCTLS, "MTRAS2: destructive self test");
1316 stat = do_qic_cmd(QCMD_SELF_TST2, TIM_R);
1317 if (stat != 0) {
1318 tpqputs(TPQD_ALWAYS, "RAS2 failed");
1319 return stat;
1321 return (tp_sense(0)==TE_OK)? 0 : -EIO; /* get_ext_status3(); */
1323 tpqputs(TPQD_IOCTLS, "RAS2 not supported");
1324 return -ENXIO;
1326 case MTSEEK:
1327 if (TP_HAVE_SEEK && (QIC02_TAPE_IFC==ARCHIVE)) {
1328 tpqputs(TPQD_IOCTLS, "MTSEEK seeking block");
1329 if ((mode_access==WRITE) && status_bytes_wr)
1330 return -EACCES;
1331 /* NOTE: address (24 bits) is in seek_addr_buf[] */
1332 return do_qic_cmd(AR_QCMDV_SEEK_BLK, TIM_F);
1334 else
1335 return -ENOTTY;
1337 default:
1338 return -ENOTTY;
1340 } /* do_ioctl_cmd */
1343 /* dma_transfer(): This routine is called for every 512 bytes to be read
1344 * from/written to the tape controller. Speed is important here!
1345 * (There must be enough time left for the hd controller!)
1346 * When other devices use DMA they must ensure they use un-interruptible
1347 * double byte accesses to the DMA controller. Floppy.c is ok.
1348 * Must have interrupts disabled when this function is invoked,
1349 * otherwise, the double-byte transfers to the DMA controller will not
1350 * be atomic. That could lead to nasty problems when they are interrupted
1351 * by other DMA interrupt-routines.
1353 * This routine merely does the least possible to keep
1354 * the transfers going:
1355 * - set the DMA count register for the next 512 bytes
1356 * - adjust the DMA address and page registers
1357 * - adjust the timeout
1358 * - tell the tape controller to start transferring
1359 * We assume the dma address and mode are, and remain, valid.
1361 static inline void dma_transfer(void)
1363 unsigned long flags;
1365 if (QIC02_TAPE_IFC == WANGTEK) /* or EVEREX */
1366 outb_p(WT_CTL_ONLINE, QIC02_CTL_PORT); /* back to normal */
1367 else if (QIC02_TAPE_IFC == ARCHIVE)
1368 outb_p(0, AR_RESET_DMA_PORT);
1369 else /* QIC02_TAPE_IFC == MOUNTAIN */
1370 outb_p(ctlbits, QIC02_CTL_PORT);
1373 flags=claim_dma_lock();
1374 clear_dma_ff(QIC02_TAPE_DMA);
1375 set_dma_mode(QIC02_TAPE_DMA, dma_mode);
1376 set_dma_addr(QIC02_TAPE_DMA, buffaddr+dma_bytes_done); /* full address */
1377 set_dma_count(QIC02_TAPE_DMA, TAPE_BLKSIZE);
1379 /* start tape DMA controller */
1380 if (QIC02_TAPE_IFC == WANGTEK) /* or EVEREX */
1381 outb_p(WT_CTL_DMA | WT_CTL_ONLINE, QIC02_CTL_PORT); /* trigger DMA transfer */
1383 else if (QIC02_TAPE_IFC == ARCHIVE) {
1384 outb_p(AR_CTL_IEN | AR_CTL_DNIEN, QIC02_CTL_PORT); /* enable interrupts again */
1385 outb_p(0, AR_START_DMA_PORT); /* start DMA transfer */
1386 /* In dma_end() AR_RESET_DMA_PORT is written too. */
1388 } else /* QIC02_TAPE_IFC == MOUNTAIN */ {
1389 inb(MTN_R_DESELECT_DMA_PORT);
1390 outb_p(ctlbits | (MTN_CTL_EXC_IEN | MTN_CTL_DNIEN), QIC02_CTL_PORT);
1391 outb_p(0, MTN_W_SELECT_DMA_PORT); /* start DMA transfer */
1392 if (dma_mode == DMA_MODE_WRITE)
1393 outb_p(0, MTN_W_DMA_WRITE_PORT); /* start DMA transfer */
1396 /* start computer DMA controller */
1397 enable_dma(QIC02_TAPE_DMA);
1399 release_dma_lock(flags);
1401 /* block transfer should start now, jumping to the
1402 * interrupt routine when done or an exception was detected.
1404 } /* dma_transfer */
1407 /* start_dma() sets a DMA transfer up between the tape controller and
1408 * the kernel qic02_tape_buf buffer.
1409 * Normally bytes_todo==dma_bytes_done at the end of a DMA transfer. If not,
1410 * a filemark was read, or an attempt to write beyond the End Of Tape
1411 * was made. [Or some other bad thing happened.]
1412 * Must do a sense() before returning error.
1414 static int start_dma(short mode, unsigned long bytes_todo)
1415 /* assume 'bytes_todo'>0 */
1417 int stat;
1418 unsigned long flags;
1420 tpqputs(TPQD_DEBUG, "start_dma() enter");
1421 TPQDEB({printk(TPQIC02_NAME ": doing_read==%d, doing_write==%d\n", doing_read, doing_write);})
1423 dma_bytes_done = 0;
1424 dma_bytes_todo = bytes_todo;
1425 status_error = NO;
1426 /* dma_mode!=0 indicates that the dma controller is in use */
1427 dma_mode = (mode == WRITE)? DMA_MODE_WRITE : DMA_MODE_READ;
1429 /* Only give READ/WRITE DATA command to tape drive if we haven't
1430 * done that already. Otherwise the drive will rewind to the beginning
1431 * of the current file on tape. Any QIC command given other than
1432 * R/W FM will break the read/write transfer cycle.
1433 * do_qic_cmd() will terminate doing_{read,write}
1435 if ((doing_read == NO) && (doing_write == NO)) {
1436 /* First, we have to clear the status -- maybe remove TP_FIL???
1439 #if 0
1440 /* Next dummy get status is to make sure CNI is valid,
1441 since we're only just starting a read/write it doesn't
1442 matter some exceptions are cleared by reading the status;
1443 we're only interested in CNI and WRP. -Eddy */
1444 get_status(&tperror);
1445 #else
1446 /* TP_CNI should now be handled in open(). -Hennus */
1447 #endif
1449 stat = tp_sense(((mode == WRITE)? 0 : TP_WRP) | TP_BOM | TP_FIL);
1450 if (stat != TE_OK)
1451 return stat;
1453 #if OBSOLETE
1454 /************* not needed iff rd_status() would wait for ready!!!!!! **********/
1455 if (wait_for_ready(TIM_S) != TE_OK) { /*** not sure this is needed ***/
1456 tpqputs(TPQD_ALWAYS, "wait_for_ready failed in start_dma");
1457 return -EIO;
1459 #endif
1461 if (QIC02_TAPE_IFC == MOUNTAIN) {
1462 /* Set control bits to select ONLINE during command */
1463 ctlbits |= MTN_QIC02_CTL_ONLINE;
1466 /* Tell the controller the data direction */
1468 /* r/w, timeout medium, check exceptions, sets status_cmd_pending. */
1469 stat = send_qic02_cmd((mode == WRITE)? QCMD_WRT_DATA : QCMD_RD_DATA, TIM_M, 0);
1470 if (stat!=TE_OK) {
1471 printk(TPQIC02_NAME ": start_dma: init %s failed\n",
1472 (mode == WRITE)? "write" : "read");
1473 (void) tp_sense(0);
1474 return stat;
1477 /* Do this last, because sense() will clear the doing_{read,write}
1478 * flags, causing trouble next time around.
1480 if (wait_for_ready(TIM_M) != TE_OK)
1481 return -EIO;
1482 switch (mode) {
1483 case READ:
1484 doing_read = YES;
1485 break;
1486 case WRITE:
1487 doing_write = YES;
1488 break;
1489 default:
1490 printk(TPQIC02_NAME ": requested unknown mode %d\n", mode);
1491 panic(TPQIC02_NAME ": invalid mode in start_dma()");
1494 } else if (is_exception()) {
1495 /* This is for Archive drives, to handle reads with 0 bytes
1496 * left for the last read request.
1498 * ******** this also affects EOF/EOT handling! ************
1500 tpqputs(TPQD_ALWAYS, "detected exception in start_dma() while transfer in progress");
1501 status_error = YES;
1502 return TE_END;
1506 status_expect_int = YES;
1508 /* This assumes tape is already positioned, but these
1509 * semi-'intelligent' drives are unpredictable...
1511 TIMERON(TIM_M*2);
1513 /* initiate first data block read from/write to the tape controller */
1515 save_flags(flags);
1516 cli();
1517 dma_transfer();
1518 restore_flags(flags);
1520 TPQPUTS("start_dma() end");
1521 return TE_OK;
1522 } /* start_dma */
1525 /* This cleans up after the dma transfer has completed
1526 * (or failed). If an exception occurred, a sense()
1527 * must be done. If the exception was caused by a FM,
1528 * sense() will set `status_eof_detected' and
1529 * `status_eom_detected', as required.
1531 static void end_dma(unsigned long * bytes_done)
1533 int stat = TE_OK;
1534 unsigned long flags;
1536 TIMEROFF;
1538 TPQPUTS("end_dma() enter");
1540 flags=claim_dma_lock();
1542 disable_dma(QIC02_TAPE_DMA);
1543 clear_dma_ff(QIC02_TAPE_DMA);
1545 release_dma_lock(flags);
1547 if (QIC02_TAPE_IFC == WANGTEK) /* or EVEREX */
1548 outb_p(WT_CTL_ONLINE, QIC02_CTL_PORT); /* back to normal */
1549 else if (QIC02_TAPE_IFC == ARCHIVE)
1550 outb_p(0, AR_RESET_DMA_PORT);
1551 else /* QIC02_TAPE_IFC == MOUNTAIN */ {
1552 /* Clear control bits, de-select ONLINE during tp_sense */
1553 ctlbits &= ~MTN_QIC02_CTL_ONLINE;
1556 stat = wait_for_ready(TIM_M);
1557 if (status_error || (stat!=TE_OK)) {
1558 tpqputs(TPQD_DMAX, "DMA transfer exception");
1559 stat = tp_sense((dma_mode==READ)? TP_WRP : 0);
1560 /* no return here -- got to clean up first! */
1561 } else /* if (QIC02_TAPE_IFC == MOUNTAIN) */ {
1562 outb_p(ctlbits, QIC02_CTL_PORT);
1565 if (QIC02_TAPE_IFC == MOUNTAIN)
1566 inb(MTN_R_DESELECT_DMA_PORT);
1568 /* take the tape controller offline */
1570 /* finish off DMA stuff */
1573 dma_mode = 0;
1574 /* Note: The drive is left on-line, ready for the next
1575 * data transfer.
1576 * If the next command to the drive does not continue
1577 * the pending cycle, it must do 2 sense()s first.
1580 *bytes_done = dma_bytes_done;
1581 status_expect_int = NO;
1582 ioctl_status.mt_blkno += (dma_bytes_done / TAPE_BLKSIZE);
1584 TPQPUTS("end_dma() exit");
1585 /*** could return stat here ***/
1586 } /* end_dma */
1588 /*********** Below are the (public) OS-interface procedures ***********/
1591 /* qic02_tape_times_out() is called when a DMA transfer doesn't complete
1592 * quickly enough. Usually this means there is something seriously wrong
1593 * with the hardware/software, but it could just be that the controller
1594 * has decided to do a long rewind, just when I didn't expect it.
1595 * Just try again.
1597 static void qic02_tape_times_out(void)
1599 printk("time-out in %s driver\n", TPQIC02_NAME);
1600 if ((status_cmd_pending>0) || dma_mode) {
1601 /* takes tooo long, shut it down */
1602 status_dead = YES;
1603 status_cmd_pending = 0;
1604 status_timer_on = NO;
1605 status_expect_int = NO;
1606 status_error = YES;
1607 if (dma_mode) {
1608 dma_mode = 0; /* signal end to read/write routine */
1609 wake_up(&qic02_tape_transfer);
1612 } /* qic02_tape_times_out */
1615 * Interrupt handling:
1617 * 1) Interrupt is generated iff at the end of
1618 * a 512-DMA-block transfer.
1619 * 2) EXCEPTION is not raised unless something
1620 * is wrong or EOT/FM is detected.
1621 * 3) FM EXCEPTION is set *after* the last byte has
1622 * been transferred by DMA. By the time the interrupt
1623 * is handled, the EXCEPTION may already be set.
1625 * So,
1626 * 1) On EXCEPTION, assume data has been transferred, so
1627 * continue as usual, but set a flag to indicate the
1628 * exception was detected.
1629 * Do a sense status when the flag is found set.
1630 * 2) Do not attempt to continue a transfer after an exception.
1631 * [??? What about marginal blocks???????]
1635 /* qic02_tape_interrupt() is called when the tape controller completes
1636 * a DMA transfer.
1637 * We are not allowed to sleep here!
1639 * Check if the transfer was successful, check if we need to transfer
1640 * more. If the buffer contains enough data/is empty enough, signal the
1641 * read/write() thread to copy to/from user space.
1642 * When we are finished, set flags to indicate end, disable timer.
1643 * NOTE: This *must* be fast!
1645 static void qic02_tape_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1647 int stat, r, i;
1648 unsigned long flags;
1650 TIMEROFF;
1652 if (status_expect_int) {
1653 #ifdef WANT_EXTRA_FULL_DEBUGGING
1654 if (TP_DIAGS(current_tape_dev))
1655 printk("@");
1656 #endif
1657 stat = inb(QIC02_STAT_PORT); /* Knock, knock */
1658 if (QIC02_TAPE_IFC == ARCHIVE) { /* "Who's there?" */
1659 if (((stat & (AR_STAT_DMADONE)) == 0) &&
1660 ((stat & (QIC02_STAT_EXCEPTION)) != 0)) {
1661 TIMERCONT;
1662 return; /* "Linux with IRQ sharing" */
1666 if ((stat & QIC02_STAT_EXCEPTION) == 0) { /* exception occurred */
1667 /* Possible causes for an exception during a transfer:
1668 * - during a write-cycle: end of tape (EW) hole detected.
1669 * - during a read-cycle: filemark or EOD detected.
1670 * - something went wrong
1671 * So don't continue with the next block.
1673 tpqputs(TPQD_ALWAYS, "isr: exception on tape controller");
1674 printk(" status %02x\n", stat);
1675 status_error = TE_EX;
1677 dma_bytes_done += TAPE_BLKSIZE;
1679 dma_mode = 0; /* wake up rw() */
1680 status_expect_int = NO;
1681 wake_up(&qic02_tape_transfer);
1682 return;
1684 /* return if tape controller not ready, or
1685 * if dma channel hasn't finished last byte yet.
1687 r = 0;
1689 /* Skip next ready check for Archive controller because
1690 * it may be busy reading ahead. Weird. --hhb
1692 if (QIC02_TAPE_IFC == WANGTEK) /* I think this is a drive-dependency, not IFC -- hhb */
1693 if (stat & QIC02_STAT_READY) { /* not ready */
1694 tpqputs(TPQD_ALWAYS, "isr: ? Tape controller not ready");
1695 r = 1;
1698 flags=claim_dma_lock();
1700 if ( (i = get_dma_residue(QIC02_TAPE_DMA)) != 0 ) {
1701 printk(TPQIC02_NAME ": dma_residue == %x !!!\n", i);
1702 r = 1; /* big trouble, but can't do much about it... */
1705 release_dma_lock(flags);
1707 if (r)
1708 return;
1710 /* finish DMA cycle */
1712 /* no errors detected, continue */
1713 dma_bytes_done += TAPE_BLKSIZE;
1714 if (dma_bytes_done >= dma_bytes_todo) {
1715 /* finished! Wakeup rw() */
1716 dma_mode = 0;
1717 status_expect_int = NO;
1718 TPQPUTS("isr: dma_bytes_done");
1719 wake_up(&qic02_tape_transfer);
1720 } else {
1721 /* start next transfer, account for track-switching time */
1722 timer_table[QIC02_TAPE_TIMER].expires = jiffies + 6*HZ;
1723 dma_transfer();
1725 } else {
1726 printk(TPQIC02_NAME ": Unexpected interrupt, stat == %x\n",
1727 inb(QIC02_STAT_PORT));
1729 } /* qic02_tape_interrupt */
1732 static long long qic02_tape_lseek(struct file * file, long long offset, int origin)
1734 return -EINVAL; /* not supported */
1735 } /* qic02_tape_lseek */
1738 /* read/write routines:
1739 * This code copies between a kernel buffer and a user buffer. The
1740 * actual data transfer is done using DMA and interrupts. Time-outs
1741 * are also used.
1743 * When a filemark is read, we return '0 bytes read' and continue with the
1744 * next file after that.
1745 * When EOM is read, we return '0 bytes read' twice.
1746 * When the EOT marker is detected on writes, '0 bytes read' should be
1747 * returned twice. If user program does a MTNOP after that, 2 additional
1748 * blocks may be written. ------- FIXME: Implement this correctly *************************************************
1750 * Only read/writes in multiples of 512 bytes are accepted.
1751 * When no bytes are available, we sleep() until they are. The controller will
1752 * generate an interrupt, and we (should) get a wake_up() call.
1754 * Simple buffering is used. User program should ensure that a large enough
1755 * buffer is used. Usually the drive does some buffering as well (something
1756 * like 4k or so).
1758 * Scott S. Bertilson suggested to continue filling the user buffer, rather
1759 * than waste time on a context switch, when the kernel buffer fills up.
1763 * Problem: tar(1) doesn't always read the entire file. Sometimes the entire file
1764 * has been read, but the EOF token is never returned to tar(1), simply because
1765 * tar(1) knows it has already read all of the data it needs. So we must use
1766 * open/release to reset the `reported_read_eof' flag. If we don't, the next read
1767 * request would return the EOF flag for the previous file.
1770 static ssize_t qic02_tape_read(struct file * filp, char * buf, size_t count, loff_t *ppos)
1772 int err;
1773 kdev_t dev = filp->f_dentry->d_inode->i_rdev;
1774 unsigned short flags = filp->f_flags;
1775 unsigned long bytes_todo, bytes_done, total_bytes_done = 0;
1776 int stat;
1778 if (status_zombie==YES)
1780 tpqputs(TPQD_ALWAYS, "configs not set");
1781 return -ENXIO;
1784 if (TP_DIAGS(current_tape_dev))
1785 /* can't print a ``long long'' (for filp->f_pos), so chop it */
1786 printk(TPQIC02_NAME ": request READ, minor=%x, buf=%p, count=%lx"
1787 ", pos=%lx, flags=%x\n",
1788 MINOR(dev), buf, (long) count,
1789 (unsigned long) filp->f_pos, flags);
1791 if (count % TAPE_BLKSIZE) /* Only allow mod 512 bytes at a time. */
1793 tpqputs(TPQD_BLKSZ, "Wrong block size");
1794 return -EINVAL;
1797 /* Just assume everything is ok. Controller will scream if not. */
1799 if (status_bytes_wr) /* Once written, no more reads, 'till after WFM. */
1801 return -EACCES;
1804 /* This is rather ugly because it has to implement a finite state
1805 * machine in order to handle the EOF situations properly.
1807 while ((signed)count>=0)
1809 bytes_done = 0;
1810 /* see how much fits in the kernel buffer */
1811 bytes_todo = TPQBUF_SIZE;
1812 if (bytes_todo>count)
1814 bytes_todo = count;
1817 /* Must ensure that user program sees exactly one EOF token (==0) */
1818 if (return_read_eof==YES)
1820 if (TPQDBG(DEBUG))
1822 printk("read: return_read_eof==%d, reported_read_eof==%d, total_bytes_done==%lu\n", return_read_eof, reported_read_eof, total_bytes_done);
1825 if (reported_read_eof==NO)
1827 /* have not yet returned EOF to user program */
1828 if (total_bytes_done>0)
1830 return total_bytes_done; /* next time return EOF */
1832 else
1834 reported_read_eof = YES; /* move on next time */
1835 return 0; /* return EOF */
1838 else
1840 /* Application program has already received EOF
1841 * (above), now continue with next file on tape,
1842 * if possible.
1843 * When the FM is reached, EXCEPTION is set,
1844 * causing a sense(). Subsequent read/writes will
1845 * continue after the FM.
1847 /*********** ?????????? this should check for (EOD|NDT), not EOM, 'cause we can read past EW: ************/
1848 if (status_eom_detected)
1850 /* If EOM, nothing left to read, so keep returning EOFs.
1851 *** should probably set some flag to avoid clearing
1852 *** status_eom_detected through ioctls or something
1854 return 0;
1856 else
1858 /* just eof, there may be more files ahead... */
1859 return_read_eof = NO;
1860 reported_read_eof = NO;
1861 status_eof_detected = NO; /* reset this too */
1862 /*fall through*/
1867 /*****************************/
1868 if (bytes_todo==0)
1870 return total_bytes_done;
1873 if (bytes_todo>0)
1875 /* start reading data */
1876 if (is_exception()) /****************************************/
1878 tpqputs(TPQD_DMAX, "is_exception() before start_dma()!");
1881 /******************************************************************
1882 ***** if start_dma() fails because the head is positioned 0 bytes
1883 ***** before the FM, (causing EXCEPTION to be set) return_read_eof should
1884 ***** be set to YES, and we should return total_bytes_done, rather than -ENXIO.
1885 ***** The app should recognize this as an EOF condition.
1886 ***************************************************************************/
1887 stat = start_dma(READ, bytes_todo);
1888 if (stat == TE_OK)
1890 /* Wait for transfer to complete, interrupt should wake us */
1891 while (dma_mode != 0)
1893 sleep_on(&qic02_tape_transfer);
1895 if (status_error)
1897 return_read_eof = YES;
1901 else if (stat != TE_END)
1903 /* should do sense() on error here */
1904 #if 0
1905 return -ENXIO;
1906 #else
1907 printk("Trouble: stat==%02x\n", stat);
1908 return_read_eof = YES;
1909 /*************** check EOF/EOT handling!!!!!! **/
1910 #endif
1912 end_dma(&bytes_done);
1913 if (bytes_done>bytes_todo)
1915 tpqputs(TPQD_ALWAYS, "read: Oops, read more bytes than requested");
1916 return -EIO;
1918 /* copy buffer to user-space in one go */
1919 if (bytes_done>0)
1921 err = copy_to_user( (void *) buf, (void *) bus_to_virt(buffaddr), bytes_done);
1922 if (err)
1924 return -EFAULT;
1927 #if 1
1928 /* Checks Ton's patch below */
1929 if ((return_read_eof == NO) && (status_eof_detected == YES))
1931 printk(TPQIC02_NAME ": read(): return_read_eof=%d, status_eof_detected=YES. return_read_eof:=YES\n", return_read_eof);
1933 #endif
1934 if ((bytes_todo != bytes_done) || (status_eof_detected == YES))
1936 /* EOF or EOM detected. return EOF next time. */
1937 return_read_eof = YES;
1940 } /* else: ignore read request for 0 bytes */
1942 if (bytes_done>0)
1944 status_bytes_rd = YES;
1945 buf += bytes_done;
1946 *ppos += bytes_done;
1947 total_bytes_done += bytes_done;
1948 count -= bytes_done;
1951 tpqputs(TPQD_ALWAYS, "read request for <0 bytes");
1952 return -EINVAL;
1953 } /* qic02_tape_read */
1957 /* The drive detects near-EOT by means of the holes in the tape.
1958 * When the holes are detected, there is some space left. The drive
1959 * reports this as a TP_EOM exception. After clearing the exception,
1960 * the drive should accept two extra blocks.
1962 * It seems there are some archiver programs that would like to use the
1963 * extra space for writing a continuation marker. The driver should return
1964 * end-of-file to the user program on writes, when the holes are detected.
1965 * If the user-program wants to use the extra space, it should use the
1966 * MTNOP ioctl() to get the generic status register and may then continue
1967 * writing (max 1kB). ----------- doesn't work yet...............
1969 * EOF behaviour on writes:
1970 * If there is enough room, write all of the data.
1971 * If there is insufficient room, write as much as will fit and
1972 * return the amount written. If the requested amount differs from the
1973 * written amount, the application program should recognize that as the
1974 * end of file. Subsequent writes will return -ENOSPC.
1975 * Unless the minor bits specify a rewind-on-close, the tape will not
1976 * be rewound when it is full. The user-program should do that, if desired.
1977 * If the driver were to do that automatically, a user-program could be
1978 * confused about the EOT/BOT condition after re-opening the tape device.
1980 * Multiple volume support: Tar closes the tape device before prompting for
1981 * the next tape. The user may then insert a new tape and tar will open the
1982 * tape device again. The driver will detect an exception status in (No Cartridge)
1983 * and force a rewind. After that tar may continue writing.
1985 static ssize_t qic02_tape_write( struct file * filp, const char * buf,
1986 size_t count, loff_t *ppos)
1988 int err;
1989 kdev_t dev = filp->f_dentry->d_inode->i_rdev;
1990 unsigned short flags = filp->f_flags;
1991 unsigned long bytes_todo, bytes_done, total_bytes_done = 0;
1993 if (status_zombie==YES)
1995 tpqputs(TPQD_ALWAYS, "configs not set");
1996 return -ENXIO;
1999 if (TP_DIAGS(current_tape_dev))
2001 /* can't print a ``long long'' (for filp->f_pos), so chop it */
2002 printk(TPQIC02_NAME ": request WRITE, minor=%x, buf=%p"
2003 ", count=%lx, pos=%lx, flags=%x\n",
2004 MINOR(dev), buf,
2005 (long) count, (unsigned long) filp->f_pos, flags);
2008 if (count % TAPE_BLKSIZE) /* only allow mod 512 bytes at a time */
2010 tpqputs(TPQD_BLKSZ, "Wrong block size");
2011 return -EINVAL;
2014 if (mode_access==READ)
2016 tpqputs(TPQD_ALWAYS, "Not in write mode");
2017 return -EACCES;
2020 /* open() does a sense() and we can assume the tape isn't changed
2021 * between open() and release(), so the tperror.exs bits will still
2022 * be valid.
2024 if ((tperror.exs & TP_ST0) && (tperror.exs & TP_WRP))
2026 tpqputs(TPQD_ALWAYS, "Cartridge is write-protected.");
2027 return -EACCES; /* don't even try when write protected */
2030 if (doing_read == YES)
2032 terminate_read(0);
2035 while ((signed)count>=0)
2037 /* see how much fits in the kernel buffer */
2038 bytes_done = 0;
2039 bytes_todo = TPQBUF_SIZE;
2040 if (bytes_todo>count)
2042 bytes_todo = count;
2045 if (return_write_eof == YES)
2047 /* return_write_eof should be reset on reverse tape movements. */
2049 if (reported_write_eof==NO)
2051 if (bytes_todo>0)
2053 tpqputs(TPQD_ALWAYS, "partial write");
2054 /* partial write signals EOF to user program */
2056 reported_write_eof = YES;
2057 return total_bytes_done;
2059 else
2061 return -ENOSPC; /* return error */
2065 /* Quit when done. */
2066 if (bytes_todo==0)
2068 return total_bytes_done;
2071 /* copy from user to DMA buffer and initiate transfer. */
2072 if (bytes_todo>0)
2074 err = copy_from_user( (void *) bus_to_virt(buffaddr), (const void *) buf, bytes_todo);
2075 if (err)
2077 return -EFAULT;
2080 /****************** similar problem with read() at FM could happen here at EOT.
2081 ******************/
2083 /***** if at EOT, 0 bytes can be written. start_dma() will
2084 ***** fail and write() will return ENXIO error
2085 *****/
2086 if (start_dma(WRITE, bytes_todo) != TE_OK)
2088 tpqputs(TPQD_ALWAYS, "write: start_dma() failed");
2089 /* should do sense() on error here */
2090 return -ENXIO; /*********** FIXTHIS **************/
2093 /* Wait for write to complete, interrupt should wake us. */
2094 while ((status_error == 0) && (dma_mode != 0))
2096 sleep_on(&qic02_tape_transfer);
2099 end_dma(&bytes_done);
2100 if (bytes_done>bytes_todo)
2102 tpqputs(TPQD_ALWAYS, "write: Oops, wrote more bytes than requested");
2103 return -EIO;
2105 /* If the dma-transfer was aborted because of an exception,
2106 * status_error will have been set in the interrupt handler.
2107 * Then end_dma() will do a sense().
2108 * If the exception was EXC_EOM, the EW-hole was encountered
2109 * and two more blocks could be written. For the time being we'll
2110 * just consider this to be the EOT.
2111 * Otherwise, something Bad happened, such as the maximum number
2112 * of block-rewrites was exceeded. [e.g. A very bad spot on tape was
2113 * encountered. Normally short dropouts are compensated for by
2114 * rewriting the block in error, up to 16 times. I'm not sure
2115 * QIC-24 drives can do this.]
2117 if (status_error)
2119 if (status_eom_detected == YES)
2121 tpqputs(TPQD_ALWAYS, "write: EW detected");
2122 return_write_eof = YES;
2124 else
2126 /* probably EXC_RWA */
2127 tpqputs(TPQD_ALWAYS, "write: dma: error in writing");
2128 return -EIO;
2131 if (bytes_todo != bytes_done)
2133 /* EOF or EOM detected. return EOT next time. */
2134 return_write_eof = YES;
2137 /* else: ignore write request for 0 bytes. */
2139 if (bytes_done>0)
2141 status_bytes_wr = YES;
2142 buf += bytes_done;
2143 *ppos += bytes_done;
2144 total_bytes_done += bytes_done;
2145 count -= bytes_done;
2149 tpqputs(TPQD_ALWAYS, "write request for <0 bytes");
2150 if (TPQDBG(DEBUG))
2152 printk(TPQIC02_NAME ": status_bytes_wr %x, buf %p"
2153 ", total_bytes_done %lx, count %lx\n",
2154 status_bytes_wr, buf, total_bytes_done, (long) count);
2156 return -EINVAL;
2157 } /* qic02_tape_write */
2161 /* qic02_tape_open()
2162 * We allow the device to be opened, even if it is marked 'dead' because
2163 * we want to be able to reset the tape device without rebooting.
2164 * Only one open tape file at a time, except when minor=255.
2165 * Minor 255 is only allowed for resetting and always returns <0.
2167 * The density command is only allowed when TP_BOM is set. Thus, remember
2168 * the most recently used minor bits. When they are different from the
2169 * remembered values, rewind the tape and set the required density.
2170 * Don't rewind if the minor bits specify density 0.
2173 static int qic02_tape_open(struct inode * inode, struct file * filp)
2175 static int qic02_tape_open_no_use_count(struct inode *, struct file *);
2176 int open_error;
2178 open_error = qic02_tape_open_no_use_count(inode, filp);
2179 if (!open_error)
2181 MOD_INC_USE_COUNT;
2183 return open_error;
2186 static int qic02_tape_open_no_use_count(struct inode * inode, struct file * filp)
2188 kdev_t dev = inode->i_rdev;
2189 unsigned short flags = filp->f_flags;
2190 unsigned short dens = 0;
2191 int s;
2194 if (TP_DIAGS(dev))
2196 printk("qic02_tape_open: dev=%s, flags=%x ",
2197 kdevname(dev), flags);
2200 if (MINOR(dev)==255) /* special case for resetting */
2202 if (suser())
2204 return (tape_reset(1)==TE_OK) ? -EAGAIN : -ENXIO;
2206 else
2208 return -EPERM;
2212 if (status_dead==YES)
2214 /* Allow `mt reset' ioctl() even when already open()ed. */
2215 return 0;
2218 /* Only one at a time from here on... */
2219 if (file_count(filp)>1) /* filp->f_count==1 for the first open() */
2221 return -EBUSY;
2224 if (status_zombie==YES)
2226 /* no irq/dma/port stuff allocated yet, no reset done
2227 * yet, so return until MTSETCONFIG has been done.
2229 return 0;
2232 status_bytes_rd = NO;
2233 status_bytes_wr = NO;
2235 return_read_eof = NO; /********????????????????*****/
2236 return_write_eof = (status_eot_detected)? YES : NO;
2238 /* Clear this in case user app close()d before reading EOF token */
2239 status_eof_detected = NO;
2241 reported_read_eof = NO;
2242 reported_write_eof = NO;
2245 switch (flags & O_ACCMODE)
2247 case O_RDONLY:
2248 mode_access = READ;
2249 break;
2250 case O_WRONLY: /* Fallthru... Strictly speaking this is not correct... */
2251 case O_RDWR: /* Reads are allowed as long as nothing is written */
2252 mode_access = WRITE;
2253 break;
2256 /* This is to avoid tape-changed problems (TP_CNI exception).
2258 * Since removing the cartridge will not raise an exception,
2259 * we always do a tp_sense() to make sure we have the proper
2260 * CNI status, the 2150L may need an additional sense.... - Eddy
2262 s = tp_sense(TP_WRP|TP_EOM|TP_BOM|TP_CNI|TP_EOR);
2264 if (s == TE_OK)
2266 /* Try to clear cartridge-changed status for Archive-2150L */
2267 if ((tperror.exs & TP_ST0) && (tperror.exs & TP_CNI))
2269 s = tp_sense(TP_WRP|TP_EOM|TP_BOM|TP_CNI|TP_EOR);
2273 if (s != TE_OK)
2275 tpqputs(TPQD_ALWAYS, "open: sense() failed");
2276 return -EIO;
2279 /* exception bits should be up-to-date now, so check for
2280 * tape presence and exit if absent.
2281 * Even `mt stat' will fail without a tape.
2283 if ((tperror.exs & TP_ST0) && (tperror.exs & TP_CNI))
2285 tpqputs(TPQD_ALWAYS, "No tape present.");
2286 return -EIO;
2289 /* At this point we can assume that a tape is present and
2290 * that it will remain present until release() is called.
2293 /* not allowed to do QCMD_DENS_* unless tape is rewound */
2294 if ((TP_DENS(dev)!=0) && (TP_DENS(current_tape_dev) != TP_DENS(dev)))
2296 /* force rewind if minor bits have changed,
2297 * i.e. user wants to use tape in different format.
2298 * [assuming single drive operation]
2300 if (TP_HAVE_DENS)
2302 tpqputs(TPQD_REWIND, "Density minor bits have changed. Forcing rewind.");
2303 need_rewind = YES;
2306 else
2308 /* density bits still the same, but TP_DIAGS bit
2309 * may have changed.
2311 current_tape_dev = dev;
2314 if (need_rewind == YES) /***************** CHECK THIS!!!!!!!! **********/
2316 s = do_qic_cmd(QCMD_REWIND, TIM_R);
2317 if (s != 0)
2319 tpqputs(TPQD_ALWAYS, "open: rewind failed");
2320 return -EIO;
2325 /* Note: After a reset command, the controller will rewind the tape
2326 * just before performing any tape movement operation! ************ SO SET need_rewind flag!!!!!
2328 if (status_dead==YES)
2330 tpqputs(TPQD_ALWAYS, "open: tape dead, attempting reset");
2331 if (tape_reset(1)!=TE_OK)
2333 return -ENXIO;
2335 else
2337 status_dead = NO;
2338 if (tp_sense(~(TP_ST1|TP_ILL)) != TE_OK)
2340 tpqputs(TPQD_ALWAYS, "open: tp_sense() failed\n");
2341 status_dead = YES; /* try reset next time */
2342 return -EIO;
2347 /* things should be ok, once we get here */
2350 /* set density: only allowed when TP_BOM status bit is set,
2351 * so we must have done a rewind by now. If not, just skip over.
2352 * Only give set density command when minor bits have changed.
2354 if (TP_DENS(current_tape_dev) == TP_DENS(dev) )
2356 return 0;
2359 current_tape_dev = dev;
2360 need_rewind = NO;
2361 if (TP_HAVE_DENS)
2363 dens = TP_DENS(dev);
2366 if (dens < sizeof(format_names)/sizeof(char *))
2368 printk(TPQIC02_NAME ": format: %s%s\n", (dens!=0)? "QIC-" : "", format_names[dens]);
2370 else
2372 tpqputs(TPQD_REWIND, "Wait for retensioning...");
2375 switch (TP_DENS(dev))
2377 case 0: /* Minor 0 is for drives without set-density support */
2378 s = 0;
2379 break;
2380 case 1:
2381 s = do_qic_cmd(QCMD_DENS_11, TIM_S);
2382 break;
2383 case 2:
2384 s = do_qic_cmd(QCMD_DENS_24, TIM_S);
2385 break;
2386 case 3:
2387 s = do_qic_cmd(QCMD_DENS_120, TIM_S);
2388 break;
2389 case 4:
2390 s = do_qic_cmd(QCMD_DENS_150, TIM_S);
2391 break;
2392 case 5:
2393 s = do_qic_cmd(QCMD_DENS_300, TIM_S);
2394 break;
2395 case 6:
2396 s = do_qic_cmd(QCMD_DENS_600, TIM_S);
2397 break;
2398 default: /* otherwise do a retension before anything else */
2399 s = do_qic_cmd(QCMD_RETEN, TIM_R);
2401 if (s != 0)
2403 status_dead = YES; /* force reset */
2404 current_tape_dev = 0; /* earlier 0xff80 */
2405 return -EIO;
2408 return 0;
2409 } /* qic02_tape_open */
2412 static int qic02_tape_release(struct inode * inode, struct file * filp)
2414 kdev_t dev = inode->i_rdev;
2416 if (TP_DIAGS(dev))
2418 printk("qic02_tape_release: dev=%s\n", kdevname(dev));
2421 if (status_zombie==NO) /* don't rewind in zombie mode */
2423 /* Terminate any pending write cycle. Terminating the read-cycle
2424 * is delayed until it is required to do so for a new command.
2426 terminate_write(-1);
2428 if (status_dead==YES)
2430 tpqputs(TPQD_ALWAYS, "release: device dead!?");
2433 /* Rewind only if minor number requires it AND
2434 * read/writes have been done. ************* IS THIS CORRECT??????????
2436 if ((TP_REWCLOSE(dev)) && (status_bytes_rd | status_bytes_wr))
2438 tpqputs(TPQD_REWIND, "release: Doing rewind...");
2439 (void) do_qic_cmd(QCMD_REWIND, TIM_R);
2442 #ifdef MODULE
2443 MOD_DEC_USE_COUNT;
2444 #endif
2445 return 0;
2446 } /* qic02_tape_release */
2449 #ifdef CONFIG_QIC02_DYNCONF
2450 /* Set masks etc. based on the interface card type. */
2451 static int update_ifc_masks(int ifc)
2453 QIC02_TAPE_IFC = ifc;
2455 if ((QIC02_TAPE_IFC == WANGTEK) || (QIC02_TAPE_IFC == EVEREX))
2457 QIC02_STAT_PORT = QIC02_TAPE_PORT;
2458 QIC02_CTL_PORT = QIC02_TAPE_PORT;
2459 QIC02_CMD_PORT = QIC02_TAPE_PORT+1;
2460 QIC02_DATA_PORT = QIC02_TAPE_PORT+1;
2461 QIC02_STAT_READY = WT_QIC02_STAT_READY;
2462 QIC02_STAT_EXCEPTION = WT_QIC02_STAT_EXCEPTION;
2463 QIC02_STAT_MASK = WT_QIC02_STAT_MASK;
2465 QIC02_STAT_RESETMASK = WT_QIC02_STAT_RESETMASK;
2466 QIC02_STAT_RESETVAL = WT_QIC02_STAT_RESETVAL;
2468 QIC02_CTL_RESET = WT_QIC02_CTL_RESET;
2469 QIC02_CTL_REQUEST = WT_QIC02_CTL_REQUEST;
2471 if (QIC02_TAPE_DMA == 3)
2473 WT_CTL_DMA = WT_CTL_DMA3;
2475 else if (QIC02_TAPE_DMA == 1)
2477 WT_CTL_DMA = WT_CTL_DMA1;
2479 else
2481 tpqputs(TPQD_ALWAYS, "Unsupported or incorrect DMA channel");
2482 return -EIO;
2485 if (QIC02_TAPE_IFC == EVEREX)
2487 /* Everex is a special case for Wangtek (actually
2488 * it's the other way 'round, but I saw Wangtek first)
2490 if (QIC02_TAPE_DMA==3)
2492 WT_CTL_DMA = WT_CTL_DMA1;
2495 /* Fixup the kernel copy of the IFC type to that
2496 * we don't have to distinguish between Wangtek and
2497 * and Everex at runtime.
2499 QIC02_TAPE_IFC = WANGTEK;
2502 else if (QIC02_TAPE_IFC == ARCHIVE)
2504 QIC02_STAT_PORT = QIC02_TAPE_PORT+1;
2505 QIC02_CTL_PORT = QIC02_TAPE_PORT+1;
2506 QIC02_CMD_PORT = QIC02_TAPE_PORT;
2507 QIC02_DATA_PORT = QIC02_TAPE_PORT;
2508 QIC02_STAT_READY = AR_QIC02_STAT_READY;
2509 QIC02_STAT_EXCEPTION = AR_QIC02_STAT_EXCEPTION;
2510 QIC02_STAT_MASK = AR_QIC02_STAT_MASK;
2512 QIC02_STAT_RESETMASK = AR_QIC02_STAT_RESETMASK;
2513 QIC02_STAT_RESETVAL = AR_QIC02_STAT_RESETVAL;
2515 QIC02_CTL_RESET = AR_QIC02_CTL_RESET;
2516 QIC02_CTL_REQUEST = AR_QIC02_CTL_REQUEST;
2518 if (QIC02_TAPE_DMA > 3)
2520 tpqputs(TPQD_ALWAYS, "Unsupported or incorrect DMA channel");
2521 return -EIO;
2524 else if (QIC02_TAPE_IFC == MOUNTAIN)
2526 QIC02_STAT_PORT = QIC02_TAPE_PORT+1;
2527 QIC02_CTL_PORT = QIC02_TAPE_PORT+1;
2528 QIC02_CMD_PORT = QIC02_TAPE_PORT;
2529 QIC02_DATA_PORT = QIC02_TAPE_PORT;
2531 QIC02_STAT_READY = MTN_QIC02_STAT_READY;
2532 QIC02_STAT_EXCEPTION = MTN_QIC02_STAT_EXCEPTION;
2533 QIC02_STAT_MASK = MTN_QIC02_STAT_MASK;
2535 QIC02_STAT_RESETMASK = MTN_QIC02_STAT_RESETMASK;
2536 QIC02_STAT_RESETVAL = MTN_QIC02_STAT_RESETVAL;
2538 QIC02_CTL_RESET = MTN_QIC02_CTL_RESET;
2539 QIC02_CTL_REQUEST = MTN_QIC02_CTL_REQUEST;
2541 if (QIC02_TAPE_DMA > 3)
2543 tpqputs(TPQD_ALWAYS, "Unsupported or incorrect DMA channel");
2544 return -EIO;
2547 else
2549 tpqputs(TPQD_ALWAYS, "Invalid interface type");
2550 return -ENXIO;
2552 return qic02_get_resources();
2553 } /* update_ifc_masks */
2554 #endif
2557 /* ioctl allows user programs to rewind the tape and stuff like that */
2558 static int qic02_tape_ioctl(struct inode * inode, struct file * filp,
2559 unsigned int iocmd, unsigned long ioarg)
2561 int error;
2562 int dev_maj = MAJOR(inode->i_rdev);
2563 int c;
2564 struct mtop operation;
2565 unsigned char blk_addr[6];
2566 struct mtpos ioctl_tell;
2569 if (TP_DIAGS(current_tape_dev))
2571 printk(TPQIC02_NAME ": ioctl(%4x, %4x, %4lx)\n", dev_maj, iocmd, ioarg);
2574 if (!inode || !ioarg)
2576 return -EINVAL;
2579 /* check iocmd first */
2581 if (dev_maj != QIC02_TAPE_MAJOR)
2583 printk(TPQIC02_NAME ": Oops! Wrong device?\n");
2584 /* A panic() would be appropriate here */
2585 return -ENODEV;
2588 c = _IOC_NR(iocmd);
2590 #ifdef CONFIG_QIC02_DYNCONF
2591 if (c == _IOC_NR(MTIOCGETCONFIG))
2593 CHECK_IOC_SIZE(mtconfiginfo);
2595 if (copy_to_user((char *) ioarg, (char *) &qic02_tape_dynconf, sizeof(qic02_tape_dynconf)))
2597 return -EFAULT;
2599 return 0;
2602 else if (c == _IOC_NR(MTIOCSETCONFIG))
2604 /* One should always do a MTIOCGETCONFIG first, then update
2605 * user-settings, then write back with MTIOCSETCONFIG.
2606 * The qic02conf program should re-open() the device before actual
2607 * use, to make sure everything is initialized.
2610 CHECK_IOC_SIZE(mtconfiginfo);
2612 if (!suser())
2614 return -EPERM;
2617 if ((doing_read!=NO) || (doing_write!=NO))
2619 return -EBUSY;
2622 if (status_zombie==NO)
2624 qic02_release_resources(); /* and go zombie */
2627 /* copy struct from user space to kernel space */
2628 if (copy_from_user((char *) &qic02_tape_dynconf, (char *) ioarg, sizeof(qic02_tape_dynconf)))
2630 return -EFAULT;
2632 return update_ifc_masks(qic02_tape_dynconf.ifc_type);
2634 if (status_zombie==YES)
2636 tpqputs(TPQD_ALWAYS, "Configs not set");
2637 return -ENXIO;
2639 #endif
2640 if (c == _IOC_NR(MTIOCTOP))
2642 CHECK_IOC_SIZE(mtop);
2644 /* copy mtop struct from user space to kernel space */
2645 if (copy_from_user((char *) &operation, (char *) ioarg, sizeof(operation)))
2647 return -EFAULT;
2650 /* ---note: mt_count is signed, negative seeks must be
2651 * --- translated to seeks in opposite direction!
2652 * (only needed for Sun-programs, I think.)
2654 /* ---note: MTFSF with count 0 should position the
2655 * --- tape at the beginning of the current file.
2658 if (TP_DIAGS(current_tape_dev))
2660 printk("OP op=%4x, count=%4x\n", operation.mt_op, operation.mt_count);
2663 if (operation.mt_count < 0)
2665 tpqputs(TPQD_ALWAYS, "Warning: negative mt_count ignored");
2668 ioctl_status.mt_resid = operation.mt_count;
2669 if (operation.mt_op == MTSEEK)
2671 if (!TP_HAVE_SEEK)
2673 return -ENOTTY;
2676 seek_addr_buf[0] = (operation.mt_count>>16)&0xff;
2677 seek_addr_buf[1] = (operation.mt_count>>8)&0xff;
2678 seek_addr_buf[2] = (operation.mt_count)&0xff;
2679 if (operation.mt_count>>24)
2681 return -EINVAL;
2683 if ((error = do_ioctl_cmd(operation.mt_op)) != 0)
2685 return error;
2688 ioctl_status.mt_resid = 0;
2690 else
2692 while (operation.mt_count > 0)
2694 operation.mt_count--;
2695 if ((error = do_ioctl_cmd(operation.mt_op)) != 0)
2697 return error;
2700 ioctl_status.mt_resid = operation.mt_count;
2703 return 0;
2706 else if (c == _IOC_NR(MTIOCGET))
2708 if (TP_DIAGS(current_tape_dev))
2710 printk("GET ");
2713 CHECK_IOC_SIZE(mtget);
2715 /* It appears (gmt(1)) that it is normal behaviour to
2716 * first set the status with MTNOP, and then to read
2717 * it out with MTIOCGET
2720 /* copy results to user space */
2721 if (copy_to_user((char *) ioarg, (char *) &ioctl_status, sizeof(ioctl_status)))
2723 return -EFAULT;
2725 return 0;
2727 else if (TP_HAVE_TELL && (c == _IOC_NR(MTIOCPOS)))
2729 if (TP_DIAGS(current_tape_dev))
2731 printk("POS ");
2734 CHECK_IOC_SIZE(mtpos);
2736 tpqputs(TPQD_IOCTLS, "MTTELL reading block address");
2737 if ((doing_read==YES) || (doing_write==YES))
2739 finish_rw(AR_QCMDV_TELL_BLK);
2742 c = rdstatus((char *) blk_addr, sizeof(blk_addr), AR_QCMDV_TELL_BLK);
2743 if (c!=TE_OK)
2745 return -EIO;
2748 ioctl_tell.mt_blkno = (blk_addr[3] << 16) | (blk_addr[4] << 8) | blk_addr[5];
2750 /* copy results to user space */
2751 if (copy_to_user((char *) ioarg, (char *) &ioctl_tell, sizeof(ioctl_tell)))
2753 return -EFAULT;
2755 return 0;
2758 else
2760 return -ENOTTY; /* Other cmds not supported. */
2762 } /* qic02_tape_ioctl */
2766 /* These are (most) of the interface functions: */
2767 static struct file_operations qic02_tape_fops = {
2768 qic02_tape_lseek, /* not allowed */
2769 qic02_tape_read, /* read */
2770 qic02_tape_write, /* write */
2771 NULL, /* readdir not allowed */
2772 NULL, /* poll ??? */
2773 qic02_tape_ioctl, /* ioctl */
2774 NULL, /* mmap not allowed */
2775 qic02_tape_open, /* open */
2776 NULL, /* flush */
2777 qic02_tape_release, /* release */
2778 NULL, /* fsync */
2779 NULL, /* fasync */
2780 NULL, /* check_media_change */
2781 NULL /* revalidate */
2785 /* Why is this not is one place? */
2786 /* Pure 2^n version of get_order */
2787 static inline int __get_order(unsigned long size)
2789 int order;
2791 size = (size-1) >> (PAGE_SHIFT-1);
2792 order = -1;
2795 size >>= 1;
2796 order++;
2797 } while (size);
2798 return order;
2802 static void qic02_release_resources(void)
2804 free_irq(QIC02_TAPE_IRQ, NULL);
2805 free_dma(QIC02_TAPE_DMA);
2806 release_region(QIC02_TAPE_PORT, QIC02_TAPE_PORT_RANGE);
2807 if (buffaddr)
2809 free_pages(buffaddr, __get_order(TPQBUF_SIZE));
2811 buffaddr = 0; /* Better to cause a panic than overwite someone else */
2812 status_zombie = YES;
2813 } /* qic02_release_resources */
2816 static int qic02_get_resources(void)
2818 /* First perform some checks. If one of them fails,
2819 * the tape driver will not be registered to the system.
2821 if (QIC02_TAPE_IRQ>16)
2823 tpqputs(TPQD_ALWAYS, "Bogus interrupt number.");
2824 return -ENXIO;
2827 /* for DYNCONF, allocating IO, DMA and IRQ should not be done until
2828 * the config parameters have been set using MTSETCONFIG.
2831 if (check_region(QIC02_TAPE_PORT, QIC02_TAPE_PORT_RANGE))
2833 printk(TPQIC02_NAME ": IO space at 0x%x [%d ports] already reserved\n",
2834 QIC02_TAPE_PORT, QIC02_TAPE_PORT_RANGE);
2835 return -ENXIO;
2838 /* get IRQ */
2839 if (request_irq(QIC02_TAPE_IRQ, qic02_tape_interrupt, SA_INTERRUPT, "QIC-02", NULL))
2841 printk(TPQIC02_NAME ": can't allocate IRQ%d for QIC-02 tape\n",
2842 QIC02_TAPE_IRQ);
2843 return -EBUSY;
2846 /* After IRQ, allocate DMA channel */
2847 if (request_dma(QIC02_TAPE_DMA,"QIC-02"))
2849 printk(TPQIC02_NAME ": can't allocate DMA%d for QIC-02 tape\n",
2850 QIC02_TAPE_DMA);
2851 free_irq(QIC02_TAPE_IRQ, NULL);
2852 return -EBUSY;
2855 /* Grab the IO region. We already made sure it's available. */
2856 request_region(QIC02_TAPE_PORT, QIC02_TAPE_PORT_RANGE, TPQIC02_NAME);
2858 /* Setup the page-address for the dma transfer. */
2860 /*** TODO: does _get_dma_pages() really return the physical address?? ****/
2861 buffaddr = __get_dma_pages(GFP_KERNEL,__get_order(TPQBUF_SIZE));
2863 if (!buffaddr)
2865 qic02_release_resources();
2866 return -EBUSY; /* Not ideal, EAGAIN perhaps? */
2869 memset( (void*) buffaddr, 0, TPQBUF_SIZE );
2871 printk(TPQIC02_NAME ": Settings: IRQ %d, DMA %d, IO 0x%x, IFC %s\n",
2872 QIC02_TAPE_IRQ, QIC02_TAPE_DMA,
2873 ((QIC02_TAPE_IFC==ARCHIVE) || (QIC02_TAPE_IFC==MOUNTAIN))?
2874 QIC02_CMD_PORT : QIC02_STAT_PORT,
2875 (QIC02_TAPE_IFC==MOUNTAIN)? "Mountain" :
2876 ((QIC02_TAPE_IFC==ARCHIVE)? "Archive" : "Wangtek"));
2878 if (tape_reset(0)!=TE_OK || tp_sense(TP_WRP|TP_POR|TP_CNI)!=TE_OK)
2880 /* No drive detected, so vanish */
2881 tpqputs(TPQD_ALWAYS, "No drive detected -- releasing IO/IRQ/DMA.");
2882 status_dead = YES;
2883 qic02_release_resources();
2884 return -EIO;
2887 /* All should be ok now */
2888 status_zombie = NO;
2889 return 0;
2890 } /* qic02_get_resources */
2892 int __init qic02_tape_init(void)
2894 if (TPSTATSIZE != 6)
2896 printk(TPQIC02_NAME ": internal error: tpstatus struct incorrect!\n");
2897 return -ENODEV;
2899 if ((TPQBUF_SIZE<512) || (TPQBUF_SIZE>=0x10000))
2901 printk(TPQIC02_NAME ": internal error: DMA buffer size out of range\n");
2902 return -ENODEV;
2905 current_tape_dev = MKDEV(QIC02_TAPE_MAJOR, 0);
2907 #ifndef CONFIG_QIC02_DYNCONF
2908 printk(TPQIC02_NAME ": IRQ %d, DMA %d, IO 0x%x, IFC %s, %s, %s\n",
2909 QIC02_TAPE_IRQ, QIC02_TAPE_DMA,
2910 # if QIC02_TAPE_IFC == WANGTEK
2911 QIC02_STAT_PORT, "Wangtek",
2912 # elif QIC02_TAPE_IFC == ARCHIVE
2913 QIC02_CMD_PORT, "Archive",
2914 # elif QIC02_TAPE_IFC == MOUNTAIN
2915 QIC02_CMD_PORT, "Mountain",
2916 # else
2917 # error
2918 # endif
2919 rcs_revision, rcs_date);
2920 if (qic02_get_resources())
2922 return -ENODEV;
2924 #else
2925 printk(TPQIC02_NAME ": Runtime config, %s, %s\n",
2926 rcs_revision, rcs_date);
2927 #endif
2928 printk(TPQIC02_NAME ": DMA buffers: %u blocks\n", NR_BLK_BUF);
2929 /* If we got this far, install driver functions */
2930 if (register_chrdev(QIC02_TAPE_MAJOR, TPQIC02_NAME, &qic02_tape_fops))
2932 printk(TPQIC02_NAME ": Unable to get chrdev major %d\n", QIC02_TAPE_MAJOR);
2933 #ifndef CONFIG_QIC02_DYNCONF
2934 qic02_release_resources();
2935 #endif
2936 return -ENODEV;
2939 init_waitqueue_head(&qic02_tape_transfer);
2940 /* prepare timer */
2941 TIMEROFF;
2942 timer_table[QIC02_TAPE_TIMER].expires = 0;
2943 timer_table[QIC02_TAPE_TIMER].fn = qic02_tape_times_out;
2945 #ifndef CONFIG_QIC02_DYNCONF
2946 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 qic02_release_resources();
2951 status_dead = YES;
2952 return -ENODEV;
2954 else
2956 if (is_exception())
2958 tpqputs(TPQD_ALWAYS, "exception detected\n");
2959 (void) tp_sense(TP_WRP|TP_POR|TP_CNI);
2962 #endif
2964 /* initialize generic status for ioctl requests */
2966 ioctl_status.mt_type = QIC02_TAPE_DRIVE; /* MT_IS* id nr */
2968 ioctl_status.mt_resid = 0; /* ---residual count */
2969 ioctl_status.mt_gstat = 0; /* ---generic status */
2970 ioctl_status.mt_erreg = 0; /* not used */
2971 ioctl_status.mt_fileno = 0; /* number of current file on tape */
2972 ioctl_status.mt_blkno = 0; /* number of current (logical) block */
2974 return 0;
2975 } /* qic02_tape_init */
2977 #ifdef MODULE
2979 void cleanup_module(void)
2981 if (status_zombie == NO)
2983 qic02_release_resources();
2985 unregister_chrdev(QIC02_TAPE_MAJOR, TPQIC02_NAME);
2988 int init_module(void)
2990 int retval;
2991 retval=qic02_tape_init();
2992 # ifdef CONFIG_QIC02_DYNCONF
2993 /* This allows the dynamic config program to setup the card
2994 * by presetting qic02_tape_dynconf via insmod
2996 if (!retval && qic02_tape_dynconf.ifc_type)
2998 retval=update_ifc_masks(qic02_tape_dynconf.ifc_type);
2999 if (retval)
3001 cleanup_module();
3004 # endif
3005 return retval;
3007 #endif