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