Home | History | Annotate | Line # | Download | only in isa
wt.c revision 1.4
      1 /*-
      2  * Copyright (c) 1991 The Regents of the University of California.
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  *
     33  *	from: @(#)wt.c	7.1 (Berkeley) 5/9/91
     34  *	$Id: wt.c,v 1.4 1993/07/19 16:58:20 cgd Exp $
     35  */
     36 
     37 /*
     38  *
     39  * Copyright (c) 1989 Carnegie-Mellon University.
     40  * All rights reserved.
     41  *
     42  * Authors: Robert Baron
     43  *
     44  * Permission to use, copy, modify and distribute this software and
     45  * its documentation is hereby granted, provided that both the copyright
     46  * notice and this permission notice appear in all copies of the
     47  * software, derivative works or modified versions, and any portions
     48  * thereof, and that both notices appear in supporting documentation.
     49  *
     50  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     51  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     52  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     53  *
     54  * Carnegie Mellon requests users of this software to return to
     55  *
     56  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     57  *  School of Computer Science
     58  *  Carnegie Mellon University
     59  *  Pittsburgh PA 15213-3890
     60  *
     61  * any improvements or extensions that they make and grant Carnegie the
     62  * rights to redistribute these changes.
     63  */
     64 
     65 #include "wt.h"
     66 #if NWT > 0
     67 /*
     68  * (Mach) HISTORY
     69  * Revision 2.2.1.3  90/01/08  13:29:38  rvb
     70  * 	Add Intel copyright.
     71  * 	[90/01/08            rvb]
     72  *
     73  * Revision 2.2.1.2  89/12/21  18:00:09  rvb
     74  * 	Change WTPRI to make the streamer tape read/write
     75  * 	interruptible. 		[lin]
     76  *
     77  * Revision 2.2.1.1  89/11/10  09:49:49  rvb
     78  * 	ORC likes their streamer port at 0x288.
     79  * 	[89/11/08            rvb]
     80  *
     81  * Revision 2.2  89/09/25  12:33:02  rvb
     82  * 	Driver was provided by Intel 9/18/89.
     83  * 	[89/09/23            rvb]
     84  *
     85  */
     86 
     87 /*
     88  *
     89  *  Copyright 1988, 1989 by Intel Corporation
     90  *
     91  *	Support Bell Tech QIC-02 and WANGTEK QIC-36 or QIC-02
     92  */
     93 
     94 /*#include <sys/errno.h>
     95 #include <sys/signal.h>
     96 #include <sys/types.h>*/
     97 #include "sys/param.h"
     98 #include "sys/buf.h"
     99 #include "sys/file.h"
    100 #include "sys/proc.h"
    101 #include "sys/user.h"
    102 #include "i386/isa/wtreg.h"
    103 
    104 #ifdef	ORC
    105 unsigned wtport = 0x288;	/* base I/O port of controller	*/
    106 #else	ORC
    107 unsigned wtport = 0x300;	/* base I/O port of controller	*/
    108 #endif	ORC
    109 				/* standard = 0x300		*/
    110 				/* alternate = 0x338		*/
    111 
    112 unsigned wtchan = 1;		/* DMA channel number		*/
    113 				/* stardard = 1			*/
    114 				/* hardware permits 1, 2 or 3.	*/
    115 		                /* (Avoid DMA 2: used by disks) */
    116 
    117 int	first_wtopen_ever = 1;
    118 
    119 
    120 #define	ERROR 		1	/* return from tape routines */
    121 #define	SUCCESS		0	/* return from tape routines */
    122 
    123 int	wci = 0;
    124 int	exflag = 0;
    125 int	bytes = 0;
    126 
    127 static	unsigned char eqdma = 0x8;
    128 static	unsigned char pagereg = 0x83;
    129 static	unsigned char dmareg = 2;
    130 static	unsigned char dma_write = 0x49;
    131 static	unsigned char dma_read = 0x45;
    132 static	unsigned char dma_done = 2;
    133 static	unsigned char mode = 0;
    134 static	unsigned char mbits;	/* map bits into each other */
    135 static	long bufptr;
    136 static	unsigned numbytes;
    137 /*
    138 _wci		dw	0	; interrupt chain finished normally
    139 _exflag		dw	0	; exception variable
    140 _bytes		dw	0	; current bytes
    141 
    142 eqdma		db	8h	; enable dma command: ch1,ch2=8h, ch3=10h
    143 pagereg		db	83h	; ch1=83h, ch2=81h, ch3=82h
    144 dmareg		db	2	; ch1=2, ch2=4, ch3=6
    145 dma_write	db	49h	; write dma command: 48h+_wtchan
    146 dma_read	db	45h	; read dma command: 44h+_wtchan
    147 dma_done	db	2	; dma done flag: 1<<_wtchan
    148 mode		db	0	; dma operation mode
    149 lbufptr		dw	0	; buffer pointer to data buffers, low word
    150 hbufptr		dw	0	; buffer pointer to data buffers, high word
    151 numbytes	dw	0	; number of bytes to read or write (new)
    152 */
    153 
    154 #define PAGESIZ		4096
    155 #define HZ		60
    156 
    157 /* tape controller ports */
    158 #define STATPORT	wtport
    159 #define CTLPORT		STATPORT
    160 #define CMDPORT		(wtport+1)
    161 #define DATAPORT	CMDPORT
    162 
    163 /* defines for reading out status from wangtek tape controller */
    164 #define READY   	0x01    /* ready bit define        */
    165 #define EXCEP		0x02	/* exception bit define    */
    166 #define STAT		(READY|EXCEP)
    167 #define	RESETMASK	0x7
    168 #define	RESETVAL	(RESETMASK & ~EXCEP)
    169 
    170 /* tape controller control bits (CTLPORT) */
    171 #define	ONLINE	0x01
    172 #define	RESET	0x02
    173 #define	REQUEST	0x04		/* request command */
    174 #define	CMDOFF	0xC0
    175 
    176 /* QIC-02 commands (CMDPORT) */
    177 #define	RDDATA	0x80		/* read data */
    178 #define	READFM	0xA0		/* read file mark */
    179 #define	WRTDATA	0x40		/* write data */
    180 #define	WRITEFM	0x60		/* write file mark */
    181 #define	RDSTAT	0xC0		/* read status command */
    182 #define	REWIND	0x21		/* rewind command (position+bot) */
    183 
    184 /* 8237 DMA controller regs */
    185 #define	STATUSREG	0x8
    186 #define MASKREG		0xA
    187 #define MODEREG		0xB
    188 #define CLEARFF		0xC
    189 
    190 /* streamer tape block size */
    191 #define BLKSIZE	512
    192 
    193 /* Tape characteristics */
    194 #define	NBPS		512	/* 512-byte blocks */
    195 #define	ERROR 		1	/* return from tape routines */
    196 #define	SUCCESS		0	/* return from tape routines */
    197 
    198 /* Minor devs */
    199 #define	TP_REWCLOSE(d)	((minor(d)&04) == 0) /* Rewind tape on close if read/write */
    200 #define	TP_DENS(dev)	((minor(dev) >> 3) & 03) /* set density */
    201 #define TPHOG(d)	0	/* use Hogproc during tape I/O	*/
    202 
    203 /* defines for wtflags */
    204 #define	TPINUSE	0x0001		/* tape is already open */
    205 #define	TPREAD	0x0002		/* tape is only open for reading */
    206 #define	TPWRITE	0x0004		/* tape is only open for writing */
    207 #define	TPSTART 0x0008		/* tape must be rewound and reset */
    208 #define	TPDEAD	0x0010		/* tape drive does not work or driver error */
    209 #define	TPSESS	0x0020		/* no more reads or writes allowed in session */
    210 				/* for example, when tape has to be changed */
    211 #define	TPSTOP	0x0040		/* Stop command outstanding */
    212 #define	TPREW	0x0080		/* Rewind command outstanding, see wtdsl2() */
    213 #define	TPVOL	0x0100		/* Read file mark, or hit end of tape */
    214 #define	TPWO	0x0200		/* write command outstanding */
    215 #define	TPRO	0x0400		/* read command outstanding */
    216 #define TPWANY	0x0800		/* write command requested */
    217 #define TPRANY	0x1000		/* read command requested */
    218 #define	TPWP	0x2000		/* write protect error seen */
    219 
    220 unsigned int	wtflags = TPSTART;	/* state of tape drive */
    221 
    222 struct	buf	rwtbuf;		/* header for raw i/o */
    223 struct  proc	*myproc;	/* process which opened tape driver */
    224 
    225 char wtimeron;			/* wtimer() active flag */
    226 char wtio;			/* dma (i/o) active flag */
    227 char isrlock;			/* isr() flag */
    228 
    229 struct proc * Hogproc;	/* no Hogproc on Microport */
    230 #define	ftoseg(x)	((unsigned) (x >> 16))
    231 
    232 struct	wtstatus {
    233 	ushort	wt_err;		/* code for error encountered */
    234 	ushort	wt_ercnt;	/* number of error blocks */
    235 	ushort	wt_urcnt;	/* number of underruns */
    236 }	wterror;
    237 
    238 /* defines for wtstatus.wt_err */
    239 #define	TP_POR		0x100	/* Power on/reset occurred */
    240 #define	TP_RES1		0x200	/* Reserved for end of media */
    241 #define	TP_RES2		0x400	/* Reserved for bus parity */
    242 #define	TP_BOM		0x800	/* Beginning of media */
    243 #define	TP_MBD		0x1000	/* Marginal block detected */
    244 #define	TP_NDT		0x2000	/* No data detected */
    245 #define	TP_ILL		0x4000	/* Illegal command */
    246 #define	TP_ST1		0x8000	/* Status byte 1 bits */
    247 #define	TP_FIL		0x01	/* File mark detected */
    248 #define	TP_BNL		0x02	/* Bad block not located */
    249 #define	TP_UDA		0x04	/* Unrecoverable data error */
    250 #define	TP_EOM		0x08	/* End of media */
    251 #define	TP_WRP		0x10	/* Write protected cartridge */
    252 #define	TP_USL		0x20	/* Unselected drive */
    253 #define	TP_CNI		0x40	/* Cartridge not in place */
    254 #define	TP_ST0		0x80	/* Status byte 0 bits */
    255 
    256 /* Grounds for reporting I/O error to user */
    257 #define	TP_ERR0		(TP_BNL|TP_UDA|TP_WRP|TP_CNI|TP_FIL|TP_EOM|TP_USL)
    258 #define	TP_ERR1		(TP_MBD|TP_NDT|TP_ILL)
    259 /* TP_ILL should never happen! */
    260 /*
    261 #define	TP_ERR0		0x7f
    262 #define	TP_ERR1		0x7700
    263 */
    264 
    265 /* defines for reading out status from wangtek tape controller */
    266 #define READY   	0x01    /* ready bit define        */
    267 #define EXCEP		0x02	/* exception bit define    */
    268 
    269 /* sleep priority */
    270 #define WTPRI	(PZERO+10)
    271 
    272 char	pagebuf[NBPS];		/* buffer of size NBPS */
    273 unsigned long	pageaddr;	/* physical addr of pagebuf */
    274 				/* pageaddr is used with DMA controller */
    275 time_t Hogtime;			/* lbolt when Hog timer started */
    276 extern time_t	lbolt;
    277 
    278 #define	debug	printf
    279 
    280 /*
    281  * Strategy routine.
    282  *
    283  * Arguments:
    284  *  Pointer to buffer structure
    285  * Function:
    286  *  Start transfer.
    287  *
    288  * It would be nice to have this multiple-threaded.
    289  * There is a version of dump from Berkeley that works with multiple processes
    290  * trading off with disk & tape I/O.
    291  */
    292 
    293 int
    294 wtstrategy(bp)
    295 register struct buf *bp;
    296 {
    297 	unsigned ucnt1, ucnt2, finished;
    298 	unsigned long adr1, adr2;
    299 	int	bad;
    300 
    301 	adr1 = kvtop(bp->b_un.b_addr);
    302 #ifdef DEBUG
    303 	debug("bpaddr %x\n", adr1);
    304 #endif
    305 	ucnt1 = bp->b_bcount % NBPG;
    306 	ucnt2 = 0;
    307 	adr2 = 0;
    308 #ifdef DEBUG
    309 	debug("WTstart: adr1 %lx cnt %x\n", adr1, ucnt1);
    310 #endif
    311 	/* 64K boundary? (XXX) */
    312 	if (ftoseg(adr1) != ftoseg(adr1 + (unsigned) ucnt1 - 1))
    313 	{
    314 		adr2 = (adr1 & 0xffff0000L) + 0x10000L;
    315 		ucnt2 = (adr1 + ucnt1) - adr2;
    316 		ucnt1 -= ucnt2;
    317 	}
    318 	/* page boundary? */
    319 	if (trunc_page(adr1) != trunc_page(adr1 + (unsigned) ucnt1 - 1))
    320 	{ unsigned u;
    321 		u = NBPG - ((unsigned)bp->b_un.b_addr & (NBPG-1));
    322 		adr2 = kvtop(bp->b_un.b_addr + u);
    323 		ucnt2 = ucnt1 - u;
    324 		ucnt1 = u;
    325 	}
    326 	/* at file marks and end of tape, we just return '0 bytes available' */
    327 	if (wtflags & TPVOL) {
    328 		bp->b_resid = bp->b_bcount;
    329 		goto xit;
    330 	}
    331 	if ((Hogproc == (struct proc *) 0) && TPHOG(bp->b_dev))
    332 	{
    333 #ifdef DEBUG
    334 		printf("setting Hogproc\n");
    335 #endif
    336 		Hogtime = 0;
    337 		Hogproc = myproc;
    338 	}
    339 	if (bp->b_flags & B_READ) {
    340 		bad = 0;
    341 
    342 		/* For now, we assume that all data will be copied out */
    343 		/* If read command outstanding, just skip down */
    344 		if (!(wtflags & TPRO)) {
    345 			if (ERROR == wtsense(TP_WRP))	/* clear status */
    346 				goto errxit;
    347 #ifdef DEBUG
    348 			debug("WTread: Start read\n");
    349 #endif
    350 			if (!(wtflags & TPREAD) || (wtflags & TPWANY) ||
    351 			    (rstart() == ERROR))  {
    352 #ifdef DEBUG
    353 				debug("Tpstart: read init error\n"); /* */
    354 #endif
    355 				goto errxit;
    356 			}
    357 			wtflags |= TPRO|TPRANY;
    358 		}
    359 
    360 		finished = 0;
    361 		/* Take a deep breath */
    362 		if (ucnt1) {
    363 			if ((rtape(adr1, ucnt1) == ERROR) &&
    364 					(wtsense(TP_WRP) == ERROR))
    365 				goto endio;
    366 			/* wait for it */
    367 			bad = pollrdy();
    368 			finished = bytes;
    369 			if (bad)
    370 				goto endio;
    371 		}
    372 		/* if a second I/O region, start it */
    373 		if (ucnt2) {
    374 			if ((rtape(adr2, ucnt2) == ERROR) &&
    375 					(wtsense(TP_WRP) == ERROR))
    376 				ucnt2 = 0;	/* don't poll for me */
    377 			}
    378 
    379 		/* if second i/o pending wait for it */
    380 		if (ucnt2) {
    381 			pollrdy();
    382 			/* whether pollrdy is ok or not */
    383 			finished += bytes;
    384 		}
    385 	} else {
    386 		if (wtflags & TPWP)	/* write protected */
    387 			goto errxit;
    388 
    389 		/* If write command outstanding, just skip down */
    390 		if (!(wtflags & TPWO)) {
    391 			if (ERROR == wtsense(0))	/* clear status */
    392 			{
    393 #ifdef DEBUG
    394 				debug("TPstart: sense 0\n");
    395 #endif
    396 				goto errxit;
    397 			}
    398 			if (!(wtflags & TPWRITE) || (wtflags & TPRANY) ||
    399 			    (wstart() == ERROR))  {
    400 #ifdef DEBUG
    401 				debug("Tpstart: write init error\n"); /* */
    402 #endif
    403 				wtsense(0);
    404 
    405 errxit:				bp->b_flags |= B_ERROR;
    406 				bp->b_resid = bp->b_bcount;
    407 				goto xit;
    408 			}
    409 			wtflags |= TPWO|TPWANY;
    410 		}
    411 
    412 		/* and hold your nose */
    413 		if (ucnt1 && ((wtape(adr1, ucnt1) == ERROR)
    414 				&& (wtsense(0) == ERROR)))
    415 			finished = bytes;
    416 
    417 		else if (ucnt2 &&
    418 			(((ucnt1 && pollrdy()) ||
    419 				(wtape(adr2, ucnt2) == ERROR)) &&
    420 				(wtsense(0) == ERROR)))
    421 			finished = ucnt1 + NBPS + bytes;
    422 		/* All writes and/or copyins were fine! */
    423 		else
    424 			finished = bp->b_bcount;
    425 		bad = pollrdy();
    426 	}
    427 
    428 	endio:
    429 	if(bad == EIO) bad = 0;
    430 	wterror.wt_err = 0;
    431 	if (exflag && wtsense((bp->b_flags & B_READ) ? TP_WRP : 0)) {
    432 		if ((wterror.wt_err & TP_ST0)
    433 			&& (wterror.wt_err & (TP_FIL|TP_EOM))) {
    434 #ifdef DEBUG
    435 			debug("WTsta: Hit end of tape\n"); /* */
    436 #endif
    437 			wtflags |= TPVOL;
    438 			if (wterror.wt_err & TP_FIL) {
    439 				if (wtflags & TPRO)
    440 					/* interrupter is bogus */
    441 					rstart();  /* restart read command */
    442 				else
    443 					wtflags &= ~TPWO;
    444 				finished += NBPS;
    445 			}
    446 		/* Reading file marks or writing end of tape return 0 bytes */
    447 		} else	{
    448 			/* XXX (cgd) - for broken 386bsd physio only:
    449 			 * bp->b_flags |= B_ERROR;
    450 			 */
    451 			wtflags &= ~(TPWO|TPRO);
    452 		}
    453 	}
    454 
    455 	if(bad) {
    456 		bp->b_flags |= B_ERROR;
    457 		bp->b_error = bad;
    458 	}
    459 	bp->b_resid = bp->b_bcount - finished;
    460 xit:
    461 	biodone(bp);
    462 	if (wtimeron)
    463 		Hogtime = lbolt;
    464 	else if (Hogproc == myproc)
    465 		Hogproc = (struct proc *) 0;
    466 }
    467 
    468 /*
    469  * simulate an interrupt periodically while I/O is going
    470  * this is necessary in case interrupts get eaten due to
    471  * multiple devices on a single IRQ line
    472  */
    473 wtimer()
    474 {
    475 	/* If I/O going and not in isr(), simulate interrupt
    476 	 * If no I/O for at least 1 second, stop being a Hog
    477 	 * If I/O done and not a Hog, turn off wtimer()
    478 	 */
    479 	if (wtio && !isrlock)
    480 		isr();
    481 
    482 	if ((Hogproc == myproc) && Hogtime && (lbolt-Hogtime > HZ))
    483 		Hogproc = (struct proc *) 0;
    484 
    485 	if (wtio || (Hogproc == myproc))
    486 		timeout(wtimer, (caddr_t) 0, HZ);
    487 	else
    488 		wtimeron = 0;
    489 }
    490 
    491 
    492 wtrawio(bp)
    493 struct buf	*bp;
    494 {
    495 	wtstrategy(bp);
    496 	biowait(bp);
    497 	return(0);
    498 }
    499 
    500 /*
    501  * ioctl routine
    502  *  for user level QIC commands only
    503  */
    504 wtioctl(dev, cmd, arg, mode)
    505 int dev, cmd;
    506 unsigned long arg;
    507 int mode;
    508 {
    509 	if (cmd == WTQICMD)
    510 	{
    511 		if ((qicmd((int)arg) == ERROR) || (rdyexc(HZ) == ERROR))
    512 		{
    513 			wtsense(0);
    514 			return(EIO);
    515 		}
    516 		return(0);
    517 	}
    518 	return(EINVAL);
    519 }
    520 
    521 /*
    522  * open routine
    523  * called on every device open
    524  */
    525 wtopen(dev, flag)
    526 int	dev, flag;
    527 {
    528 	if (first_wtopen_ever) {
    529 		wtinit();
    530 		first_wtopen_ever = 0;
    531 	}
    532 #ifdef DEBUG
    533 	printf("wtopen ...\n");
    534 #endif
    535 	if (!pageaddr) {
    536 		return(ENXIO);
    537 	}
    538 	if (wtflags & (TPINUSE)) {
    539 		return(ENXIO);
    540 	}
    541 	if (wtflags & (TPDEAD)) {
    542 		return(EIO);
    543 	}
    544 	/* If a rewind from the last session is going on, wait */
    545 	while(wtflags & TPREW) {
    546 #ifdef DEBUG
    547 		debug("Waiting for rew to finish\n");
    548 #endif
    549 		DELAY(1000000);	/* delay one second */
    550 	}
    551 	/* Only do reset and select when tape light is off, and tape is rewound.
    552 	 * This allows multiple volumes. */
    553 	if (wtflags & TPSTART) {
    554 		if (t_reset() != SUCCESS) {
    555 			return(ENXIO);
    556 		}
    557 #ifdef DEBUG
    558 		debug("reset done. calling wtsense\n");
    559 #endif
    560 		if (wtsense(TP_WRP) == ERROR) {
    561 			return (EIO);
    562 		}
    563 #ifdef DEBUG
    564 		debug("wtsense done\n");
    565 #endif
    566 		wtflags &= ~TPSTART;
    567 	}
    568 
    569 	wtflags = TPINUSE;
    570 	if (flag & FREAD)
    571 		wtflags |= TPREAD;
    572 	if (flag & FWRITE)
    573 		wtflags |= TPWRITE;
    574 	rwtbuf.b_flags = 0;
    575 	myproc = curproc;		/* for comparison */
    576 #ifdef not
    577 	switch(TP_DENS(dev)) {
    578 case 0:
    579 cmds(0x28);
    580 break;
    581 case 1:
    582 cmds(0x29);
    583 break;
    584 case 2:
    585 cmds(0x27);
    586 break;
    587 case 3:
    588 cmds(0x24);
    589 	}
    590 #endif
    591 	return(0);
    592 }
    593 
    594 /*
    595  * close routine
    596  * called on last device close
    597  * If not rewind-on-close, leave read or write command intact.
    598  */
    599 wtclose(dev)
    600 {
    601 	int wtdsl2();
    602 
    603 #ifdef DEBUG
    604 	debug("WTclose:\n");
    605 #endif
    606 	if (Hogproc == myproc)
    607 		Hogproc = (struct proc *) 0;
    608 	if (!exflag && (wtflags & TPWANY) && !(wtflags & (TPSESS|TPDEAD))) {
    609 		if (!(wtflags & TPWO))
    610 			wstart();
    611 #ifdef DEBUG
    612 		debug("WT: Writing file mark\n");
    613 #endif
    614 		wmark();	/* write file mark */
    615 #ifdef DEBUG
    616 		debug("WT: Wrote file mark, going to wait\n");
    617 #endif
    618 		if (rdyexc(HZ/10) == ERROR) {
    619 			wtsense(0);
    620 			}
    621 		}
    622 	if (TP_REWCLOSE(dev) || (wtflags & (TPSESS|TPDEAD))) {
    623 	/* rewind tape to beginning of tape, deselect tape, and make a note */
    624 	/* don't wait until rewind, though */
    625 		/* Ending read or write causes rewind to happen, if no error,
    626 		 * and READY and EXCEPTION stay up until it finishes */
    627 		if (wtflags & (TPRO|TPWO))
    628 		{
    629 #ifdef DEBUG
    630 			debug("End read or write\n");
    631 #endif
    632 			rdyexc(HZ/10);
    633 			ioend();
    634 			wtflags &= ~(TPRO|TPWO);
    635 		}
    636 		else	wtwind();
    637 		wtflags |= TPSTART | TPREW;
    638 		timeout(wtdsl2, 0, HZ);
    639 	}
    640 	else if (!(wtflags & (TPVOL|TPWANY)))
    641 	{
    642 		/* space forward to after next file mark no writing done */
    643 		/* This allows skipping data without reading it.*/
    644 #ifdef DEBUG
    645 		debug("Reading past file mark\n");
    646 #endif
    647 		if (!(wtflags & TPRO))
    648 			rstart();
    649 		rmark();
    650 		if (rdyexc(HZ/10))
    651 		{
    652 			wtsense(TP_WRP);
    653 		}
    654 	}
    655 	wtflags &= TPREW|TPDEAD|TPSTART|TPRO|TPWO;
    656 	return(0);
    657 }
    658 
    659 /* return ERROR if user I/O request should receive an I/O error code */
    660 
    661 wtsense(ignor)
    662 {
    663 	wtflags &= ~(TPRO|TPWO);
    664 #ifdef DEBUGx
    665 	debug("WTsense: start ");
    666 #endif
    667 	if (rdstatus(&wterror) == ERROR)
    668 	{
    669 #ifdef DEBUG
    670 		debug("WTsense: Can't read status\n");
    671 #endif
    672 		return(ERROR);
    673 	}
    674 #ifdef DEBUG
    675 	if (wterror.wt_err & (TP_ST0|TP_ST1))
    676 	{
    677 		debug("Tperror: status %x error %d underruns %d\n",
    678 			wterror.wt_err, wterror.wt_ercnt, wterror.wt_urcnt);
    679 	}
    680 	else
    681 		debug("done. no error\n");
    682 #endif
    683 	wterror.wt_err &= ~ignor;	/* ignore certain errors */
    684 	reperr(wterror.wt_err);
    685 	if (((wterror.wt_err & TP_ST0) && (wterror.wt_err & TP_ERR0)) ||
    686 		    ((wterror.wt_err & TP_ST1) && (wterror.wt_err & TP_ERR1)))
    687 			return	ERROR;
    688 
    689 	return SUCCESS;
    690 }
    691 
    692 /* lifted from tdriver.c from Wangtek */
    693 reperr(srb0)
    694 int srb0;
    695 {
    696 	int s0 = srb0 & (TP_ERR0|TP_ERR1);	/* find out which exception to report */
    697 
    698 	if (s0) {
    699 		if (s0 & TP_USL)
    700 			sterr("Drive not online");
    701 		else if (s0 & TP_CNI)
    702 			sterr("No cartridge");
    703 		else if ((s0 & TP_WRP) && !(wtflags & TPWP))
    704 		{
    705 			sterr("Tape is write protected");
    706 			wtflags |= TPWP;
    707 		}
    708 		/*
    709 		if (s0 & TP_FIL)
    710 			sterr("Filemark detected");
    711 		*/
    712 		else if (s0 & TP_BNL)
    713 			sterr("Block in error not located");
    714 		else if (s0 & TP_UDA)
    715 			sterr("Unrecoverable data error");
    716 		/*
    717 		else if (s0 & TP_EOM)
    718 			sterr("End of tape");
    719 		*/
    720 		else if (s0 & TP_NDT)
    721 			sterr("No data detected");
    722 		/*
    723 		if (s0 & TP_POR)
    724 			sterr("Reset occured");
    725 		*/
    726 		else if (s0 & TP_BOM)
    727 			sterr("Beginning of tape");
    728 		else if (s0 & TP_ILL)
    729 			sterr("Illegal command");
    730 	}
    731 }
    732 
    733 sterr(errstr)
    734 char	*errstr;
    735 {
    736 	printf("Streamer: %s\n", errstr);
    737 }
    738 
    739 /* Wait until rewind finishes, and deselect drive */
    740 wtdsl2() {
    741 	int	stat;
    742 
    743 	stat = inb(wtport) & (READY|EXCEP);
    744 #ifdef DEBUG
    745 	debug("Timeout: Waiting for rewind to finish: stat %x\n", stat);
    746 #endif
    747 	switch (stat) {
    748 		/* They're active low, ya'know */
    749 		case READY|EXCEP:
    750 			timeout(wtdsl2, (caddr_t) 0, HZ);
    751 			return;
    752 		case EXCEP:
    753 			wtflags &= ~TPREW;
    754 			return;
    755 		case READY:
    756 		case	0:
    757 			wtflags &= ~TPREW;
    758 			sterr("Rewind failed");
    759 			wtsense(TP_WRP);
    760 			return;
    761 			}
    762 	}
    763 
    764 wtwind() {
    765 #ifdef DEBUG
    766 	debug("WT: About to rewind\n");
    767 #endif
    768 	rwind();	/* actually start rewind */
    769 }
    770 
    771 wtintr(unit) {
    772 	if (wtflags & (TPWO|TPRO))
    773 	{
    774 		isrlock = 1;
    775 		if (wtio) isr();
    776 		isrlock = 0;
    777 	}
    778 }
    779 
    780 wtinit() {
    781 	if (wtchan < 1 || wtchan > 3)
    782 	{
    783 		sterr("Bad DMA channel, cannot init driver");
    784 		return;
    785 	}
    786 	wtlinit();	/* init assembly language variables */
    787 	pageset();
    788 }
    789 
    790 rdyexc(ticks)
    791 {
    792 	int s;
    793 #ifdef DEBUG
    794 	int os = 0xffff;		/* force printout first time */
    795 #endif
    796 	for (;;) {			/* loop until ready or exception */
    797 		s=(inb(wtport) & 0xff);	/* read the status register */
    798 #ifdef DEBUG
    799 		if (os != s) {
    800 			debug("Status reg = %x\n", s); /* */
    801 			os = s;
    802 			}
    803 #endif
    804 		if (!(s & EXCEP))	/* check if exception have occured */
    805 			break;
    806 		if (!(s & READY))	/* check if controller is ready */
    807 			break;
    808 		s = splbio();
    809 		DELAY((ticks/HZ)*1000000); /* */
    810 		splx(s);
    811 	}
    812 #ifdef DEBUG
    813 	debug("Status reg = %x on return\n", s); /* */
    814 #endif
    815 	return((s & EXCEP)?SUCCESS:ERROR);  /* return exception if it occured */
    816 }
    817 
    818 pollrdy()
    819 {
    820 	int	 sps;
    821 #ifdef DEBUG
    822 	debug("Pollrdy\n");
    823 #endif
    824 	sps = splbio();
    825 	while (wtio) {
    826 		int error;
    827 
    828 		if (error = tsleep((caddr_t)&wci, WTPRI | PCATCH,
    829 			"wtpoll", 0)) {
    830 			splx(sps);
    831 			return(error);
    832 		}
    833 	}
    834 	splx(sps);
    835 #ifdef DEBUG
    836 	debug("Finish poll, wci %d exflag %d\n", wci, exflag);
    837 #endif
    838 	return (EIO);
    839 }
    840 
    841 wtdma()		/* start up i/o operation, called from dma() in wtlib1.s */
    842 {
    843 	wtio = 1;
    844 	if (!wtimeron)
    845 	{
    846 		wtimeron = 1;
    847 		timeout(wtimer, (caddr_t) 0, HZ/2);
    848 	}
    849 }
    850 
    851 wtwake()	/* end i/o operation, called from isr() in wtlib1.s */
    852 {
    853 	wtio = 0;
    854 	wakeup(&wci);
    855 }
    856 
    857 pageset()
    858 {
    859 	unsigned long pp;
    860 
    861 	pp = (unsigned long) pagebuf;
    862 	pageaddr = kvtop(pp);
    863 #ifdef DEBUG
    864 	debug("pageset: addr %lx\n", pageaddr);
    865 #endif
    866 }
    867 
    868 
    869 
    870 #define near
    871 
    872 static near
    873 sendcmd()
    874 {
    875 	/* desired command in global mbits */
    876 
    877 	outb(CTLPORT, mbits | REQUEST);		/* set request */
    878 	while (inb(STATPORT) & READY);		/* wait for ready */
    879 	outb(CTLPORT, mbits & ~REQUEST);	/* reset request */
    880 	while ((inb(STATPORT) & READY) == 0);	/* wait for not ready */
    881 }
    882 
    883 static near		/* execute command */
    884 cmds(cmd)
    885 {
    886 	register s;
    887 
    888 	do s = inb(STATPORT);
    889 	while ((s & STAT) == STAT);	/* wait for ready */
    890 
    891 	if ((s & EXCEP) == 0)		/* if exception */
    892 		return ERROR;		/* error */
    893 
    894 	outb(CMDPORT, cmd);		/* output the command	*/
    895 
    896 	outb(CTLPORT, mbits=ONLINE);	/* set & send ONLINE	*/
    897 	sendcmd();
    898 
    899 	return SUCCESS;
    900 }
    901 
    902 qicmd(cmd)
    903 {
    904 	return cmds(cmd);
    905 }
    906 
    907 rstart()
    908 {
    909 	return cmds(RDDATA);
    910 }
    911 
    912 rmark()
    913 {
    914 	return cmds(READFM);
    915 }
    916 
    917 wstart()
    918 {
    919 	return cmds(WRTDATA);
    920 }
    921 
    922 ioend()
    923 {
    924 	register s;
    925 	register rval = SUCCESS;
    926 
    927 	do s = inb(STATPORT);
    928 	while ((s & STAT) == STAT);	/* wait for ready */
    929 
    930 	if ((s & EXCEP) == 0)		/* if exception */
    931 		rval = ERROR;		/* error */
    932 
    933 	mbits &= ~ONLINE;
    934 	outb(CTLPORT, mbits);		/* reset ONLINE */
    935 	outb(MASKREG, wtchan+4);	/* turn off dma */
    936 	outb(CLEARFF, 0);		/* reset direction flag */
    937 
    938 	return rval;
    939 }
    940 
    941 wmark()
    942 {
    943 	register s;
    944 
    945 	if (cmds(WRITEFM) == ERROR)
    946 		return ERROR;
    947 
    948 	do s = inb(STATPORT);
    949 	while ((s & STAT) == STAT);	/* wait for ready */
    950 
    951 	if ((s & EXCEP) == 0)		/* if exception */
    952 		return ERROR;		/* error */
    953 
    954 	return SUCCESS;
    955 }
    956 
    957 rwind()
    958 {
    959 	register s;
    960 
    961 	mbits = CMDOFF;
    962 
    963 	do s = inb(STATPORT);
    964 	while ((s & STAT) == STAT);	/* wait for ready */
    965 
    966 	outb(CMDPORT, REWIND);
    967 	sendcmd();
    968 
    969 	return SUCCESS;
    970 }
    971 
    972 rdstatus(stp)
    973 char *stp;		/* pointer to 6 byte buffer */
    974 {
    975 	register s;
    976 	int n;
    977 
    978 	do s = inb(STATPORT);
    979 	while ((s & STAT) == STAT);	/* wait for ready or exception */
    980 
    981 	outb(CMDPORT, RDSTAT);
    982 	sendcmd();			/* send read status command */
    983 
    984 	for (n=0; n<6; n++)
    985 	{
    986 #ifdef DEBUGx
    987 		debug("rdstatus: waiting, byte %d\n", n);
    988 #endif
    989 		do s = inb(STATPORT);
    990 		while ((s & STAT) == STAT);	/* wait for ready */
    991 #ifdef DEBUGx
    992 		debug("rdstatus: done\n");
    993 #endif
    994 		if ((s & EXCEP) == 0)		/* if exception */
    995 			return ERROR;		/* error */
    996 
    997 		*stp++ = inb(DATAPORT);		/* read status byte */
    998 
    999 		outb(CTLPORT, mbits | REQUEST);	/* set request */
   1000 #ifdef DEBUGx
   1001 		debug("rdstatus: waiting after request, byte %d\n", n);
   1002 #endif
   1003 		while ((inb(STATPORT)&READY) == 0);	/* wait for not ready */
   1004 		for (s=100; s>0; s--);		/* wait an additional time */
   1005 
   1006 		outb(CTLPORT, mbits & ~REQUEST);/* unset request */
   1007 #ifdef DEBUGx
   1008 		debug("rdstatus: done\n");
   1009 #endif
   1010 	}
   1011 	return SUCCESS;
   1012 }
   1013 
   1014 t_reset()
   1015 {
   1016 	register i;
   1017 	mbits |= RESET;
   1018 	outb(CTLPORT, mbits);		/* send reset */
   1019 	DELAY(20);
   1020 	mbits &= ~RESET;
   1021 	outb(CTLPORT, mbits);		/* turn off reset */
   1022 	if ((inb(STATPORT) & RESETMASK) == RESETVAL)
   1023 		return SUCCESS;
   1024 	return ERROR;
   1025 }
   1026 
   1027 static
   1028 dma()
   1029 {
   1030 	int x=splbio();
   1031 	wtdma();
   1032 	outb(CLEARFF, 0);
   1033 	outb(MODEREG, mode);	/* set dma mode */
   1034 	outb(dmareg, bufptr & 0xFF);
   1035 	outb(dmareg, (bufptr>>8) & 0xFF);
   1036 	outb(pagereg, (bufptr>>16) & 0xFF);
   1037 	outb(dmareg+1, (BLKSIZE-1) & 0xFF);
   1038 	outb(dmareg+1, (BLKSIZE-1) >> 8);
   1039 	outb(wtport, eqdma+ONLINE);
   1040 	outb(MASKREG, wtchan);	/* enable command to 8237, start dma */
   1041 	splx(x);
   1042 }
   1043 
   1044 static near
   1045 wtstart(buf, cnt)
   1046 long buf;
   1047 int cnt;
   1048 {
   1049 	register s;
   1050 
   1051 	bufptr = buf;		/* init statics */
   1052 	numbytes = cnt;
   1053 	wci = 0;		/* init flags */
   1054 	exflag = 0;
   1055 	bytes = 0;		/* init counter */
   1056 
   1057 	do s = inb(STATPORT) & STAT;
   1058 	while (s == STAT);	/* wait for ready or error */
   1059 
   1060 	if (s & EXCEP)		/* no error */
   1061 	{
   1062 		dma();
   1063 		return SUCCESS;
   1064 	}
   1065 	return ERROR;		/* error */
   1066 }
   1067 
   1068 rtape(buf, cnt)
   1069 long buf;			/* physical address */
   1070 int cnt;			/* number of bytes */
   1071 {
   1072 	mode = dma_read;
   1073 	return wtstart(buf,cnt);
   1074 }
   1075 
   1076 wtape(buf, cnt)
   1077 long buf;			/* physical address */
   1078 int cnt;			/* number of bytes */
   1079 {
   1080 	mode = dma_write;
   1081 	return wtstart(buf,cnt);
   1082 }
   1083 
   1084 isr()
   1085 {
   1086 	int stat = inb(wtport);
   1087 	if (!(stat & EXCEP))	/* exception during I/O */
   1088 	{
   1089 		if (bytes + BLKSIZE >= numbytes) wci = 1;
   1090 		exflag = 1;
   1091 		goto isrwake;
   1092 	}
   1093 	if ((stat & READY) || !(inb(STATUSREG) & dma_done))
   1094 		return;
   1095 	exflag = 0;
   1096 	outb(wtport, ONLINE);
   1097 	bytes += BLKSIZE;
   1098 	if (bytes >= numbytes)	/* normal completion of I/O */
   1099 	{
   1100 		wci = 1;
   1101 isrwake:
   1102 		outb(MASKREG, 4+wtchan);	/* turn off dma */
   1103 		wtwake();			/* wake up user level */
   1104 	}
   1105 	else
   1106 	{			/* continue I/O */
   1107 		bufptr += BLKSIZE;
   1108 		dma();
   1109 	}
   1110 }
   1111 
   1112 wtlinit()
   1113 {
   1114 	switch (wtchan) {
   1115 	case 1:
   1116 		return;
   1117 	case 2:
   1118 		pagereg = 0x81;
   1119 		dma_done = 4;
   1120 		break;
   1121 	case 3:
   1122 		eqdma = 0x10;
   1123 		pagereg = 0x82;
   1124 		dma_done = 8;
   1125 		break;
   1126 	}
   1127 	dma_write = wtchan+0x48;
   1128 	dma_read = wtchan+0x44;
   1129 	dmareg = wtchan+wtchan;
   1130 }
   1131 
   1132 wtsize()
   1133 {
   1134 }
   1135 
   1136 wtdump()
   1137 {
   1138 }
   1139 
   1140 #include "i386/isa/isa_device.h"
   1141 #include "i386/isa/icu.h"
   1142 
   1143 int	wtprobe(), wtattach();
   1144 struct	isa_driver wtdriver = {
   1145 	wtprobe, wtattach, "wt",
   1146 };
   1147 
   1148 wtprobe(dvp)
   1149 	struct isa_device *dvp;
   1150 {
   1151 	int val,i,s;
   1152 
   1153 #ifdef lint
   1154 	wtintr(0);
   1155 #endif
   1156 
   1157 	wtport = dvp->id_iobase;
   1158 	if(t_reset() != SUCCESS) return(0);
   1159 	return(1);
   1160 }
   1161 
   1162 wtattach() { }
   1163 
   1164 #endif NWT
   1165