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