* Copyright (c) 1989 Carnegie-Mellon University
* All rights reserved. The CMU software License Agreement specifies
* the terms and conditions for use and redistribution.
* @(#)wt.c 1.1 (Berkeley) %G%
* Revision 2.2.1.3 90/01/08 13:29:38 rvb
* Revision 2.2.1.2 89/12/21 18:00:09 rvb
* Change WTPRI to make the streamer tape read/write
* Revision 2.2.1.1 89/11/10 09:49:49 rvb
* ORC likes their streamer port at 0x288.
* Revision 2.2 89/09/25 12:33:02 rvb
* Driver was provided by Intel 9/18/89.
* Copyright 1988, 1989 by Intel Corporation
* Support Bell Tech QIC-02 and WANGTEK QIC-36 or QIC-02
#include <machine/isa/wtreg.h>
unsigned wtport
= 0x288; /* base I/O port of controller */
unsigned wtport
= 0x300; /* base I/O port of controller */
unsigned wtchan
= 1; /* DMA channel number */
/* hardware permits 1, 2 or 3. */
/* (Avoid DMA 2: used by disks) */
int first_wtopen_ever
= 1;
#define ERROR 1 /* return from tape routines */
#define SUCCESS 0 /* return from tape routines */
static unsigned char eqdma
= 0x8;
static unsigned char pagereg
= 0x83;
static unsigned char dmareg
= 2;
static unsigned char dma_write
= 0x49;
static unsigned char dma_read
= 0x45;
static unsigned char dma_done
= 2;
static unsigned char mode
= 0;
static unsigned char mbits
; /* map bits into each other */
static unsigned numbytes
;
_wci dw 0 ; interrupt chain finished normally
_exflag dw 0 ; exception variable
_bytes dw 0 ; current bytes
eqdma db 8h ; enable dma command: ch1,ch2=8h, ch3=10h
pagereg db 83h ; ch1=83h, ch2=81h, ch3=82h
dmareg db 2 ; ch1=2, ch2=4, ch3=6
dma_write db 49h ; write dma command: 48h+_wtchan
dma_read db 45h ; read dma command: 44h+_wtchan
dma_done db 2 ; dma done flag: 1<<_wtchan
mode db 0 ; dma operation mode
lbufptr dw 0 ; buffer pointer to data buffers, low word
hbufptr dw 0 ; buffer pointer to data buffers, high word
numbytes dw 0 ; number of bytes to read or write (new)
/* tape controller ports */
#define CMDPORT (wtport+1)
/* defines for reading out status from wangtek tape controller */
#define READY 0x01 /* ready bit define */
#define EXCEP 0x02 /* exception bit define */
#define STAT (READY|EXCEP)
#define RESETVAL (RESETMASK & ~EXCEP)
/* tape controller control bits (CTLPORT) */
#define REQUEST 0x04 /* request command */
/* QIC-02 commands (CMDPORT) */
#define RDDATA 0x80 /* read data */
#define READFM 0xA0 /* read file mark */
#define WRTDATA 0x40 /* write data */
#define WRITEFM 0x60 /* write file mark */
#define RDSTAT 0xC0 /* read status command */
#define REWIND 0x21 /* rewind command (position+bot) */
/* 8237 DMA controller regs */
/* streamer tape block size */
/* Tape characteristics */
#define NBPS 512 /* 512-byte blocks */
#define ERROR 1 /* return from tape routines */
#define SUCCESS 0 /* return from tape routines */
#define TP_REWCLOSE(d) ((minor(d)&04) == 0) /* Rewind tape on close if read/write */
#define TP_DENS(dev) ((minor(dev) >> 3) & 03) /* set density */
#define TPHOG(d) 0 /* use Hogproc during tape I/O */
/* defines for wtflags */
#define TPINUSE 0x0001 /* tape is already open */
#define TPREAD 0x0002 /* tape is only open for reading */
#define TPWRITE 0x0004 /* tape is only open for writing */
#define TPSTART 0x0008 /* tape must be rewound and reset */
#define TPDEAD 0x0010 /* tape drive does not work or driver error */
#define TPSESS 0x0020 /* no more reads or writes allowed in session */
/* for example, when tape has to be changed */
#define TPSTOP 0x0040 /* Stop command outstanding */
#define TPREW 0x0080 /* Rewind command outstanding, see wtdsl2() */
#define TPVOL 0x0100 /* Read file mark, or hit end of tape */
#define TPWO 0x0200 /* write command outstanding */
#define TPRO 0x0400 /* read command outstanding */
#define TPWANY 0x0800 /* write command requested */
#define TPRANY 0x1000 /* read command requested */
#define TPWP 0x2000 /* write protect error seen */
unsigned int wtflags
= TPSTART
; /* state of tape drive */
struct buf rwtbuf
; /* header for raw i/o */
struct proc
*myproc
; /* process which opened tape driver */
char wtimeron
; /* wtimer() active flag */
char wtio
; /* dma (i/o) active flag */
char isrlock
; /* isr() flag */
struct proc
* Hogproc
; /* no Hogproc on Microport */
#define ftoseg(x) ((unsigned) (x >> 16))
#define seterror(err) u.u_error = err
ushort wt_err
; /* code for error encountered */
ushort wt_ercnt
; /* number of error blocks */
ushort wt_urcnt
; /* number of underruns */
/* defines for wtstatus.wt_err */
#define TP_POR 0x100 /* Power on/reset occurred */
#define TP_RES1 0x200 /* Reserved for end of media */
#define TP_RES2 0x400 /* Reserved for bus parity */
#define TP_BOM 0x800 /* Beginning of media */
#define TP_MBD 0x1000 /* Marginal block detected */
#define TP_NDT 0x2000 /* No data detected */
#define TP_ILL 0x4000 /* Illegal command */
#define TP_ST1 0x8000 /* Status byte 1 bits */
#define TP_FIL 0x01 /* File mark detected */
#define TP_BNL 0x02 /* Bad block not located */
#define TP_UDA 0x04 /* Unrecoverable data error */
#define TP_EOM 0x08 /* End of media */
#define TP_WRP 0x10 /* Write protected cartridge */
#define TP_USL 0x20 /* Unselected drive */
#define TP_CNI 0x40 /* Cartridge not in place */
#define TP_ST0 0x80 /* Status byte 0 bits */
/* Grounds for reporting I/O error to user */
#define TP_ERR0 (TP_BNL|TP_UDA|TP_WRP|TP_CNI|TP_FIL|TP_EOM|TP_USL)
#define TP_ERR1 (TP_MBD|TP_NDT|TP_ILL)
/* TP_ILL should never happen! */
/* defines for reading out status from wangtek tape controller */
#define READY 0x01 /* ready bit define */
#define EXCEP 0x02 /* exception bit define */
char pagebuf
[NBPS
]; /* buffer of size NBPS */
unsigned long pageaddr
; /* physical addr of pagebuf */
/* pageaddr is used with DMA controller */
time_t Hogtime
; /* lbolt when Hog timer started */
* Pointer to buffer structure
* It would be nice to have this multiple-threaded.
* There is a version of dump from Berkeley that works with multiple processes
* trading off with disk & tape I/O.
unsigned ucnt1
, ucnt2
, finished
;
unsigned long adr1
, adr2
;
adr1
= kvtop(bp
->b_un
.b_addr
);
debug("bpaddr %x\n", adr1
);
debug("WTstart: adr1 %lx cnt %x\n", adr1
, ucnt1
);
if (ftoseg(adr1
) != ftoseg(adr1
+ (unsigned) ucnt1
- 1))
adr2
= (adr1
& 0xffff0000L
) + 0x10000L
;
ucnt2
= (adr1
+ ucnt1
) - adr2
;
/* at file marks and end of tape, we just return '0 bytes available' */
bp
->b_resid
= bp
->b_bcount
;
if ((Hogproc
== (struct proc
*) 0) && TPHOG(bp
->b_dev
))
printf("setting Hogproc\n");
if (bp
->b_flags
& B_READ
) {
/* For now, we assume that all data will be copied out */
/* If read command outstanding, just skip down */
if (ERROR
== wtsense(TP_WRP
)) /* clear status */
debug("WTread: Start read\n");
if (!(wtflags
& TPREAD
) || (wtflags
& TPWANY
) ||
debug("Tpstart: read init error\n"); /* */
if ((rtape(adr1
, ucnt1
) == ERROR
) &&
(wtsense(TP_WRP
) == ERROR
))
/* if a second I/O region, start it */
if ((rtape(adr2
, ucnt2
) == ERROR
) &&
(wtsense(TP_WRP
) == ERROR
))
ucnt2
= 0; /* don't poll for me */
/* if second i/o pending wait for it */
/* whether pollrdy is ok or not */
if (wtflags
& TPWP
) /* write protected */
/* If write command outstanding, just skip down */
if (ERROR
== wtsense(0)) /* clear status */
debug("TPstart: sense 0\n");
if (!(wtflags
& TPWRITE
) || (wtflags
& TPRANY
) ||
debug("Tpstart: write init error\n"); /* */
errxit
: bp
->b_flags
|= B_ERROR
;
bp
->b_resid
= bp
->b_bcount
;
if (ucnt1
&& ((wtape(adr1
, ucnt1
) == ERROR
)
&& (wtsense(0) == ERROR
)))
(((ucnt1
&& pollrdy()) ||
(wtape(adr2
, ucnt2
) == ERROR
)) &&
finished
= ucnt1
+ NBPS
+ bytes
;
/* All writes and/or copyins were fine! */
if (exflag
&& wtsense((bp
->b_flags
& B_READ
) ? TP_WRP
: 0)) {
if ((wterror
.wt_err
& TP_ST0
)
&& (wterror
.wt_err
& (TP_FIL
|TP_EOM
))) {
debug("WTsta: Hit end of tape\n"); /* */
if (wterror
.wt_err
& TP_FIL
) {
/* interrupter is bogus */
rstart(); /* restart read command */
/* Reading file marks or writing end of tape return 0 bytes */
bp
->b_resid
= bp
->b_bcount
- finished
;
else if (Hogproc
== myproc
)
Hogproc
= (struct proc
*) 0;
* simulate an interrupt periodically while I/O is going
* this is necessary in case interrupts get eaten due to
* multiple devices on a single IRQ line
/* If I/O going and not in isr(), simulate interrupt
* If no I/O for at least 1 second, stop being a Hog
* If I/O done and not a Hog, turn off wtimer()
if ((Hogproc
== myproc
) && Hogtime
&& (lbolt
-Hogtime
> HZ
))
Hogproc
= (struct proc
*) 0;
if (wtio
|| (Hogproc
== myproc
))
timeout(wtimer
, (caddr_t
) 0, HZ
);
if (bp
->b_bcount
> PAGESIZ
)
physio(wtrawio
, &rwtbuf
, dev
, B_READ
, wt_minphys
, uio
);
physio(wtrawio
, &rwtbuf
, dev
, B_WRITE
, wt_minphys
, uio
);
* for user level QIC commands only
wtioctl(dev
, cmd
, arg
, mode
)
if ((qicmd((int)arg
) == ERROR
) || (rdyexc(HZ
) == ERROR
))
* called on every device open
if (wtflags
& (TPINUSE
)) {
if (wtflags
& (TPDEAD
)) {
/* If a rewind from the last session is going on, wait */
debug("Waiting for rew to finish\n");
delay(1000000); /* delay one second */
/* Only do reset and select when tape light is off, and tape is rewound.
* This allows multiple volumes. */
if (t_reset() != SUCCESS
) {
debug("reset done. calling wtsense\n");
if (wtsense(TP_WRP
) == ERROR
) {
myproc
= u
.u_procp
; /* for comparison */
* called on last device close
* If not rewind-on-close, leave read or write command intact.
Hogproc
= (struct proc
*) 0;
if (!exflag
&& (wtflags
& TPWANY
) && !(wtflags
& (TPSESS
|TPDEAD
))) {
debug("WT: Writing file mark\n");
wmark(); /* write file mark */
debug("WT: Wrote file mark, going to wait\n");
if (rdyexc(HZ
/10) == ERROR
) {
if (TP_REWCLOSE(dev
) || (wtflags
& (TPSESS
|TPDEAD
))) {
/* rewind tape to beginning of tape, deselect tape, and make a note */
/* don't wait until rewind, though */
/* Ending read or write causes rewind to happen, if no error,
* and READY and EXCEPTION stay up until it finishes */
if (wtflags
& (TPRO
|TPWO
))
debug("End read or write\n");
wtflags
|= TPSTART
| TPREW
;
else if (!(wtflags
& (TPVOL
|TPWANY
)))
/* space forward to after next file mark no writing done */
/* This allows skipping data without reading it.*/
debug("Reading past file mark\n");
wtflags
&= TPREW
|TPDEAD
|TPSTART
|TPRO
|TPWO
;
/* return ERROR if user I/O request should receive an I/O error code */
debug("WTsense: start ");
if (rdstatus(&wterror
) == ERROR
)
debug("WTsense: Can't read status\n");
if (wterror
.wt_err
& (TP_ST0
|TP_ST1
))
debug("Tperror: status %x error %d underruns %d\n",
wterror
.wt_err
, wterror
.wt_ercnt
, wterror
.wt_urcnt
);
debug("done. no error\n");
wterror
.wt_err
&= ~ignor
; /* ignore certain errors */
if (((wterror
.wt_err
& TP_ST0
) && (wterror
.wt_err
& TP_ERR0
)) ||
((wterror
.wt_err
& TP_ST1
) && (wterror
.wt_err
& TP_ERR1
)))
/* lifted from tdriver.c from Wangtek */
int s0
= srb0
& (TP_ERR0
|TP_ERR1
); /* find out which exception to report */
sterr("Drive not online");
else if ((s0
& TP_WRP
) && !(wtflags
& TPWP
))
sterr("Tape is write protected");
sterr("Filemark detected");
sterr("Block in error not located");
sterr("Unrecoverable data error");
sterr("No data detected");
sterr("Beginning of tape");
sterr("Illegal command");
printf("Streamer: %s\n", errstr
);
/* Wait until rewind finishes, and deselect drive */
stat
= inb(wtport
) & (READY
|EXCEP
);
debug("Timeout: Waiting for rewind to finish: stat %x\n", stat
);
/* They're active low, ya'know */
timeout(wtdsl2
, (caddr_t
) 0, HZ
);
debug("WT: About to rewind\n");
rwind(); /* actually start rewind */
if (wtflags
& (TPWO
|TPRO
))
if (wtchan
< 1 || wtchan
> 3)
sterr("Bad DMA channel, cannot init driver");
wtlinit(); /* init assembly language variables */
int os
= 0xffff; /* force printout first time */
for (;;) { /* loop until ready or exception */
s
=(inb(wtport
) & 0xff); /* read the status register */
debug("Status reg = %x\n", s
); /* */
if (!(s
& EXCEP
)) /* check if exception have occured */
if (!(s
& READY
)) /* check if controller is ready */
delay((ticks
/HZ
)*1000000); /* */
debug("Status reg = %x on return\n", s
); /* */
return((s
& EXCEP
)?SUCCESS
:ERROR
); /* return exception if it occured */
debug("Finish poll, wci %d exflag %d\n", wci
, exflag
);
wtdma() /* start up i/o operation, called from dma() in wtlib1.s */
timeout(wtimer
, (caddr_t
) 0, HZ
/2);
wtwake() /* end i/o operation, called from isr() in wtlib1.s */
pp
= (unsigned long) pagebuf
;
debug("pageset: addr %lx\n", pageaddr
);
/* desired command in global mbits */
outb(CTLPORT
, mbits
| REQUEST
); /* set request */
while (inb(STATPORT
) & READY
); /* wait for ready */
outb(CTLPORT
, mbits
& ~REQUEST
); /* reset request */
while ((inb(STATPORT
) & READY
) == 0); /* wait for not ready */
static near
/* execute command */
while ((s
& STAT
) == STAT
); /* wait for ready */
if ((s
& EXCEP
) == 0) /* if exception */
return ERROR
; /* error */
outb(CMDPORT
, cmd
); /* output the command */
outb(CTLPORT
, mbits
=ONLINE
); /* set & send ONLINE */
while ((s
& STAT
) == STAT
); /* wait for ready */
if ((s
& EXCEP
) == 0) /* if exception */
rval
= ERROR
; /* error */
outb(CTLPORT
, mbits
); /* reset ONLINE */
outb(MASKREG
, wtchan
+4); /* turn off dma */
outb(CLEARFF
, 0); /* reset direction flag */
if (cmds(WRITEFM
) == ERROR
)
while ((s
& STAT
) == STAT
); /* wait for ready */
if ((s
& EXCEP
) == 0) /* if exception */
return ERROR
; /* error */
while ((s
& STAT
) == STAT
); /* wait for ready */
char *stp
; /* pointer to 6 byte buffer */
while ((s
& STAT
) == STAT
); /* wait for ready or exception */
sendcmd(); /* send read status command */
debug("rdstatus: waiting, byte %d\n", n
);
while ((s
& STAT
) == STAT
); /* wait for ready */
debug("rdstatus: done\n");
if ((s
& EXCEP
) == 0) /* if exception */
return ERROR
; /* error */
*stp
++ = inb(DATAPORT
); /* read status byte */
outb(CTLPORT
, mbits
| REQUEST
); /* set request */
debug("rdstatus: waiting after request, byte %d\n", n
);
while ((inb(STATPORT
)&READY
) == 0); /* wait for not ready */
for (s
=100; s
>0; s
--); /* wait an additional time */
outb(CTLPORT
, mbits
& ~REQUEST
);/* unset request */
debug("rdstatus: done\n");
outb(CTLPORT
, mbits
); /* send reset */
outb(CTLPORT
, mbits
); /* turn off reset */
if ((inb(STATPORT
) & RESETMASK
) == RESETVAL
)
outb(MODEREG
, mode
); /* set dma mode */
outb(dmareg
, bufptr
& 0xFF);
outb(dmareg
, (bufptr
>>8) & 0xFF);
outb(pagereg
, (bufptr
>>16) & 0xFF);
outb(dmareg
+1, (BLKSIZE
-1) & 0xFF);
outb(dmareg
+1, (BLKSIZE
-1) >> 8);
outb(wtport
, eqdma
+ONLINE
);
outb(MASKREG
, wtchan
); /* enable command to 8237, start dma */
bufptr
= buf
; /* init statics */
wci
= 0; /* init flags */
bytes
= 0; /* init counter */
do s
= inb(STATPORT
) & STAT
;
while (s
== STAT
); /* wait for ready or error */
if (s
& EXCEP
) /* no error */
return ERROR
; /* error */
long buf
; /* physical address */
int cnt
; /* number of bytes */
long buf
; /* physical address */
int cnt
; /* number of bytes */
if (!(stat
& EXCEP
)) /* exception during I/O */
if (bytes
+ BLKSIZE
>= numbytes
) wci
= 1;
if ((stat
& READY
) || !(inb(STATUSREG
) & dma_done
))
if (bytes
>= numbytes
) /* normal completion of I/O */
outb(MASKREG
, 4+wtchan
); /* turn off dma */
wtwake(); /* wake up user level */
#include "machine/isa/device.h"
#include "machine/isa/icu.h"
int wtprobe(), wtattach();
struct isa_driver wtdriver
= {
if(t_reset() != SUCCESS
) return(0);