#
/*
 * TC-11 DECtape driver
 */

#include "../head/param.h"
#include "../head/systm.h"
#include "../head/conf.h"
#include "../head/buf.h"
#include "../head/bufx.h"
#include "../head/user.h"
#include "../head/userx.h"
#include "../head/elog.h"

struct tcregs {
	int	tccsr;
	int	tccm;
	int	tcwc;
	int	tcba;
	int	tcdt;
};

#define NTC	8
#define	NTCBLK	578
#define	TCADDR	0177340

struct	devtab	tctab;
char	tcper[8];

struct	iostat	tcstat[NTC];
struct	errtab	tcetab { etabinit(E_BLK,NTC,TC,tcstat) };

#define	TAPERR	0100000
#define	TREV	04000
#define	READY	0200
#define	IENABLE	0100
#define	UPS	0200
#define	ENDZ	0100000
#define	BLKM	02000
#define	ILGOP	010000
#define	SELERR	04000

#define	SAT	0
#define	RNUM	02
#define	RDATA	04
#define	SST	010
#define	WDATA	014
#define	GO	01

#define	SFORW	1
#define	SREV	2
#define	SIO	3

tcopen(dev, flag)
{
	if(dev.d_minor >= NTC)
		u.u_error = ENXIO;
}

tcclose(dev)
{
	bflush(dev);
	tcper[dev&07] = 0;
}

tcstrategy(abp)
struct buf *abp;
{
	register struct buf *bp;

	bp = abp;
	if(bp->b_flags&B_PHYS)
		mapalloc(bp);
	if(tcper[bp->b_dev&07]) {
		bp->b_flags =| B_ERROR;
		iodone(bp);
		return;
	}
	if(bp->b_blkno >= NTCBLK) {
		if (bp->b_flags&B_READ)
			bp->b_resid = bp->b_wcount;
		else {
			bp->b_flags =| B_ERROR;
			bp->b_error = ENXIO;
		}
		iodone(bp);
		return;
	}
	bp->av_forw = 0;
	spl6();
	if (tctab.d_actf==0)
		tctab.d_actf = bp;
	else
		tctab.d_actl->av_forw = bp;
	tctab.d_actl = bp;
	if (tctab.d_active==0)
		tcstart();
	spl0();
}

tcstart()
{
	register struct buf *bp;
	register int *tccmp, com;

loop:
	tccmp = &TCADDR->tccm;
	if ((bp = tctab.d_actf) == 0)
		return;
	if(tcper[bp->b_dev&07]) {
		if((tctab.d_actf = bp->av_forw) == 0)
			(*tccmp).lobyte = SAT|GO;
		bp->b_flags =| B_ERROR;
		iodone(bp);
		goto loop;
	}
	if (((*tccmp).hibyte&07) != bp->b_dev.d_minor)
		(*tccmp).lobyte = SAT|GO;
	tctab.d_errcnt = 20;
	tctab.d_active = SFORW;
	tcetab.e_aunit = &tcstat[bp->b_dev.d_minor];
	tcetab.e_aunit->io_misc++;
	com = (bp->b_dev.d_minor<<8) | IENABLE|RNUM|GO;
	if ((TCADDR->tccsr & UPS) == 0) {
		com =| TREV;
		tctab.d_active = SREV;
	}
	*tccmp = com;
}

tcintr()
{
	register struct buf *bp;
	register int *tccmp;
	register int *tcdtp;
	struct tcregs tcregs[0];

	tccmp = &TCADDR->tccm;
	tcdtp = &TCADDR->tccsr;
	bp = tctab.d_actf;
	blkacty =& ~(1<<TC);
	if (*tccmp&TAPERR) {
		if((*tcdtp&(ENDZ|BLKM)) == 0)
			fmtblk(&tcetab,TCADDR,sizeof(tcregs[0])/2,bp);
		if(*tcdtp & (ILGOP|SELERR)) {
			tcper[bp->b_dev&07]++;
			tctab.d_errcnt = 0;
		}
		*tccmp =& ~TAPERR;
		if (--tctab.d_errcnt  <= 0) {
			bp->b_flags =| B_ERROR;
			goto done;
		}
		if(tctab.d_errcnt == 19)
			logerr(&tcetab,E_FIRST);
		if (*tccmp&TREV) {
		setforw:
			tctab.d_active = SFORW;
			*tccmp =& ~TREV;
		} else {
		setback:
			tctab.d_active = SREV;
			*tccmp =| TREV;
		}
		tcetab.e_aunit->io_misc++;
		(*tccmp).lobyte = IENABLE|RNUM|GO;
		return;
	}
	tcdtp = &TCADDR->tcdt;
	switch (tctab.d_active) {

	case SIO:
	done:
		if(tcetab.e_emsg != NULL)
			logerr(&tcetab,E_RETRY);
		tctab.d_active = 0;
		bp->b_resid = TCADDR->tcwc;
		if (tctab.d_actf = bp->av_forw)
			tcstart();
		else
			TCADDR->tccm.lobyte = SAT|GO;
		iodone(bp);
		return;

	case SFORW:
		if (*tcdtp > bp->b_blkno)
			goto setback;
		if (*tcdtp < bp->b_blkno)
			goto setforw;
		*--tcdtp = bp->b_addr;		/* core address */
		*--tcdtp = bp->b_wcount;
		tccmp->lobyte = ((bp->b_xmem & 03) << 4) | IENABLE|GO
		    | (bp->b_flags&B_READ?RDATA:WDATA);
		tctab.d_active = SIO;
		blkacty =| (1<<TC);
		tcetab.e_aunit->io_ops++;
		return;

	case SREV:
		if (*tcdtp+3 > bp->b_blkno)
			goto setback;
		goto setforw;
	}
}
