xref: /csrg-svn/sys/vax/mba/ht.c (revision 6812)
1*6812Swnj /*	ht.c	4.22	82/05/12	*/
22961Swnj 
32980Swnj #include "tu.h"
41563Sbill #if NHT > 0
522Sbill /*
62926Swnj  * TM03/TU?? tape driver
73094Swnj  *
83094Swnj  * TODO:
93204Swnj  *	cleanup messages on errors
103094Swnj  *	test ioctl's
113094Swnj  *	see how many rewind interrups we get if we kick when not at BOT
123204Swnj  *	fixup rle error on block tape code
1322Sbill  */
1422Sbill #include "../h/param.h"
1522Sbill #include "../h/systm.h"
1622Sbill #include "../h/buf.h"
1722Sbill #include "../h/conf.h"
1822Sbill #include "../h/dir.h"
1922Sbill #include "../h/file.h"
2022Sbill #include "../h/user.h"
2122Sbill #include "../h/map.h"
22420Sbill #include "../h/pte.h"
232980Swnj #include "../h/mbareg.h"
242980Swnj #include "../h/mbavar.h"
252926Swnj #include "../h/mtio.h"
262926Swnj #include "../h/ioctl.h"
271917Swnj #include "../h/cmap.h"
282961Swnj #include "../h/cpu.h"
2922Sbill 
302926Swnj #include "../h/htreg.h"
3122Sbill 
322926Swnj struct	buf	rhtbuf[NHT];
332926Swnj struct	buf	chtbuf[NHT];
3422Sbill 
352926Swnj short	httypes[] =
363181Swnj 	{ MBDT_TM03, MBDT_TE16, MBDT_TU45, MBDT_TU77, 0 };
372980Swnj struct	mba_device *htinfo[NHT];
383103Swnj int	htattach(), htslave(), htustart(), htndtint(), htdtint();
392926Swnj struct	mba_driver htdriver =
402980Swnj     { htattach, htslave, htustart, 0, htdtint, htndtint,
412980Swnj       httypes, "ht", "tu", htinfo };
4222Sbill 
432926Swnj #define MASKREG(r)	((r) & 0xffff)
4422Sbill 
452926Swnj /* bits in minor device */
462980Swnj #define	TUUNIT(dev)	(minor(dev)&03)
472926Swnj #define	H_NOREWIND	04
482926Swnj #define	H_1600BPI	08
4922Sbill 
503094Swnj #define HTUNIT(dev)	(tutoht[TUUNIT(dev)])
512980Swnj 
522926Swnj #define	INF	(daddr_t)1000000L	/* a block number that wont exist */
532926Swnj 
543094Swnj struct	tu_softc {
552926Swnj 	char	sc_openf;
562926Swnj 	char	sc_flags;
572926Swnj 	daddr_t	sc_blkno;
582926Swnj 	daddr_t	sc_nxrec;
592926Swnj 	u_short	sc_erreg;
602926Swnj 	u_short	sc_dsreg;
612926Swnj 	short	sc_resid;
622926Swnj 	short	sc_dens;
632980Swnj 	struct	mba_device *sc_mi;
642980Swnj 	int	sc_slave;
653094Swnj } tu_softc[NTU];
663094Swnj short	tutoht[NTU];
672926Swnj 
682926Swnj /*
692926Swnj  * Bits for sc_flags.
702926Swnj  */
712926Swnj #define	H_WRITTEN 1	/* last operation was a write */
722926Swnj #define H_ERASED  2	/* last write retry was an erase gap */
732926Swnj #define H_REWIND  4	/* last unit start was a rewind */
7422Sbill 
753204Swnj char	hter_bits[] = HTER_BITS;
763204Swnj char	htds_bits[] = HTDS_BITS;
773204Swnj 
782926Swnj /*ARGSUSED*/
792980Swnj htattach(mi)
802980Swnj 	struct mba_device *mi;
812926Swnj {
822926Swnj 
832926Swnj }
842926Swnj 
852980Swnj htslave(mi, ms)
862980Swnj 	struct mba_device *mi;
872980Swnj 	struct mba_slave *ms;
882980Swnj {
893094Swnj 	register struct tu_softc *sc = &tu_softc[ms->ms_unit];
904756Swnj 	register struct htdevice *htaddr = (struct htdevice *)mi->mi_drv;
912980Swnj 
924756Swnj 	htaddr->httc = ms->ms_slave;
934756Swnj 	if (htaddr->htdt & HTDT_SPR) {
944756Swnj 		sc->sc_mi = mi;
954756Swnj 		sc->sc_slave = ms->ms_slave;
964756Swnj 		tutoht[ms->ms_unit] = mi->mi_unit;
974756Swnj 		return (1);
984756Swnj 	} else
994756Swnj 		return (0);
1002980Swnj }
1012980Swnj 
10222Sbill htopen(dev, flag)
1032926Swnj 	dev_t dev;
1042926Swnj 	int flag;
10522Sbill {
1063094Swnj 	register int tuunit;
1072980Swnj 	register struct mba_device *mi;
1083094Swnj 	register struct tu_softc *sc;
1093203Swnj 	int olddens, dens;
11022Sbill 
1113094Swnj 	tuunit = TUUNIT(dev);
1123094Swnj 	if (tuunit >= NTU || (sc = &tu_softc[tuunit])->sc_openf ||
1132980Swnj 	    (mi = htinfo[HTUNIT(dev)]) == 0 || mi->mi_alive == 0) {
11422Sbill 		u.u_error = ENXIO;
11522Sbill 		return;
11622Sbill 	}
1173203Swnj 	olddens = sc->sc_dens;
1183204Swnj 	dens = sc->sc_dens =
1193094Swnj 	    ((minor(dev)&H_1600BPI)?HTTC_1600BPI:HTTC_800BPI)|
1203094Swnj 		HTTC_PDP11|sc->sc_slave;
1213203Swnj 	htcommand(dev, HT_SENSE, 1);
1223203Swnj 	sc->sc_dens = olddens;
1233707Sroot 	if ((sc->sc_dsreg & HTDS_MOL) == 0) {
1243717Sroot 		uprintf("tu%d: not online\n", tuunit);
1252926Swnj 		u.u_error = EIO;
1262926Swnj 		return;
1272926Swnj 	}
1283707Sroot 	if ((flag&FWRITE) && (sc->sc_dsreg&HTDS_WRL)) {
1293717Sroot 		uprintf("tu%d: no write ring\n", tuunit);
1303707Sroot 		u.u_error = EIO;
1313707Sroot 		return;
1323707Sroot 	}
1333707Sroot 	if ((sc->sc_dsreg & HTDS_BOT) == 0 && (flag&FWRITE) &&
1343707Sroot 	    dens != sc->sc_dens) {
1353717Sroot 		uprintf("tu%d: can't change density in mid-tape\n", tuunit);
1363707Sroot 		u.u_error = EIO;
1373707Sroot 		return;
1383707Sroot 	}
1392926Swnj 	sc->sc_openf = 1;
1402926Swnj 	sc->sc_blkno = (daddr_t)0;
1412926Swnj 	sc->sc_nxrec = INF;
1422926Swnj 	sc->sc_flags = 0;
1433094Swnj 	sc->sc_dens = dens;
14422Sbill }
14522Sbill 
14622Sbill htclose(dev, flag)
1472926Swnj 	register dev_t dev;
1482926Swnj 	register flag;
14922Sbill {
1503094Swnj 	register struct tu_softc *sc = &tu_softc[TUUNIT(dev)];
15122Sbill 
1522926Swnj 	if (flag == FWRITE || ((flag&FWRITE) && (sc->sc_flags&H_WRITTEN))) {
1532926Swnj 		htcommand(dev, HT_WEOF, 1);
1542926Swnj 		htcommand(dev, HT_WEOF, 1);
1552926Swnj 		htcommand(dev, HT_SREV, 1);
15622Sbill 	}
1572926Swnj 	if ((minor(dev)&H_NOREWIND) == 0)
1582926Swnj 		htcommand(dev, HT_REW, 0);
1592926Swnj 	sc->sc_openf = 0;
16022Sbill }
16122Sbill 
1622926Swnj htcommand(dev, com, count)
1632926Swnj 	dev_t dev;
1642926Swnj 	int com, count;
16522Sbill {
16622Sbill 	register struct buf *bp;
1675436Sroot 	register int s;
16822Sbill 
1692926Swnj 	bp = &chtbuf[HTUNIT(dev)];
1705436Sroot 	s = spl5();
1712926Swnj 	while (bp->b_flags&B_BUSY) {
1723157Swnj 		if(bp->b_repcnt == 0 && (bp->b_flags&B_DONE))
1732980Swnj 			break;
17422Sbill 		bp->b_flags |= B_WANTED;
17522Sbill 		sleep((caddr_t)bp, PRIBIO);
17622Sbill 	}
1772943Swnj 	bp->b_flags = B_BUSY|B_READ;
1785436Sroot 	splx(s);
17922Sbill 	bp->b_dev = dev;
1802926Swnj 	bp->b_command = com;
1812926Swnj 	bp->b_repcnt = count;
18222Sbill 	bp->b_blkno = 0;
18322Sbill 	htstrategy(bp);
1842926Swnj 	if (count == 0)
1852926Swnj 		return;
18622Sbill 	iowait(bp);
1872926Swnj 	if (bp->b_flags&B_WANTED)
18822Sbill 		wakeup((caddr_t)bp);
1892926Swnj 	bp->b_flags &= B_ERROR;
19022Sbill }
19122Sbill 
19222Sbill htstrategy(bp)
1932926Swnj 	register struct buf *bp;
19422Sbill {
1953094Swnj 	register struct mba_device *mi = htinfo[HTUNIT(bp->b_dev)];
1962926Swnj 	register struct buf *dp;
1975436Sroot 	register int s;
19822Sbill 
19922Sbill 	bp->av_forw = NULL;
2002926Swnj 	dp = &mi->mi_tab;
2015436Sroot 	s = spl5();
2022926Swnj 	if (dp->b_actf == NULL)
2032926Swnj 		dp->b_actf = bp;
20422Sbill 	else
2052926Swnj 		dp->b_actl->av_forw = bp;
2062926Swnj 	dp->b_actl = bp;
2072926Swnj 	if (dp->b_active == 0)
2082926Swnj 		mbustart(mi);
2095436Sroot 	splx(s);
21022Sbill }
21122Sbill 
2122926Swnj htustart(mi)
2132980Swnj 	register struct mba_device *mi;
21422Sbill {
2152926Swnj 	register struct htdevice *htaddr =
2162926Swnj 	    (struct htdevice *)mi->mi_drv;
2172926Swnj 	register struct buf *bp = mi->mi_tab.b_actf;
2183094Swnj 	register struct tu_softc *sc = &tu_softc[TUUNIT(bp->b_dev)];
21922Sbill 	daddr_t blkno;
22022Sbill 
2212926Swnj 	htaddr->httc = sc->sc_dens;
2223181Swnj 	if (bp == &chtbuf[HTUNIT(bp->b_dev)] && bp->b_command == HT_SENSE) {
2233157Swnj 		htaddr->htcs1 = HT_SENSE|HT_GO;
2243157Swnj 		mbclrattn(mi);
2253157Swnj 	}
2262926Swnj 	sc->sc_dsreg = htaddr->htds;
2272926Swnj 	sc->sc_erreg = htaddr->hter;
2282926Swnj 	sc->sc_resid = htaddr->htfc;
2292926Swnj 	sc->sc_flags &= ~(H_WRITTEN|H_REWIND);
2302926Swnj 	if ((htaddr->htdt & HTDT_SPR) == 0 || (htaddr->htds & HTDS_MOL) == 0)
2312926Swnj 		if (sc->sc_openf > 0)
2322926Swnj 			sc->sc_openf = -1;
2332926Swnj 	if (sc->sc_openf < 0) {
2342926Swnj 		bp->b_flags |= B_ERROR;
2352926Swnj 		return (MBU_NEXT);
2362926Swnj 	}
2373094Swnj 	if (bp != &chtbuf[HTUNIT(bp->b_dev)]) {
2382926Swnj 		if (dbtofsb(bp->b_blkno) > sc->sc_nxrec) {
2392926Swnj 			bp->b_flags |= B_ERROR;
2402926Swnj 			bp->b_error = ENXIO;
2412961Swnj 			return (MBU_NEXT);
2423094Swnj 		}
2433094Swnj 		if (dbtofsb(bp->b_blkno) == sc->sc_nxrec &&
2442926Swnj 		    bp->b_flags&B_READ) {
2452926Swnj 			bp->b_resid = bp->b_bcount;
2462926Swnj 			clrbuf(bp);
2472961Swnj 			return (MBU_NEXT);
2483094Swnj 		}
2493094Swnj 		if ((bp->b_flags&B_READ)==0)
2502926Swnj 			sc->sc_nxrec = dbtofsb(bp->b_blkno) + 1;
2512926Swnj 	} else {
2522961Swnj 		if (bp->b_command == HT_SENSE)
2532926Swnj 			return (MBU_NEXT);
2542926Swnj 		if (bp->b_command == HT_REW)
2552926Swnj 			sc->sc_flags |= H_REWIND;
2562926Swnj 		else
2572926Swnj 			htaddr->htfc = -bp->b_bcount;
2582926Swnj 		htaddr->htcs1 = bp->b_command|HT_GO;
2592926Swnj 		return (MBU_STARTED);
2602926Swnj 	}
2612926Swnj 	if ((blkno = sc->sc_blkno) == dbtofsb(bp->b_blkno)) {
2622926Swnj 		htaddr->htfc = -bp->b_bcount;
2632926Swnj 		if ((bp->b_flags&B_READ) == 0) {
2643094Swnj 			if (mi->mi_tab.b_errcnt) {
2653094Swnj 				if ((sc->sc_flags & H_ERASED) == 0) {
2662926Swnj 					sc->sc_flags |= H_ERASED;
2672926Swnj 					htaddr->htcs1 = HT_ERASE | HT_GO;
2682926Swnj 					return (MBU_STARTED);
2692926Swnj 				}
2703094Swnj 				sc->sc_flags &= ~H_ERASED;
2713094Swnj 			}
2722926Swnj 			if (htaddr->htds & HTDS_EOT) {
2732926Swnj 				bp->b_resid = bp->b_bcount;
274*6812Swnj 				bp->b_flags |= B_ERROR;
2752926Swnj 				return (MBU_NEXT);
2762926Swnj 			}
27722Sbill 		}
2782926Swnj 		return (MBU_DODATA);
27922Sbill 	}
2802926Swnj 	if (blkno < dbtofsb(bp->b_blkno)) {
2812926Swnj 		htaddr->htfc = blkno - dbtofsb(bp->b_blkno);
2822926Swnj 		htaddr->htcs1 = HT_SFORW|HT_GO;
28322Sbill 	} else {
2842926Swnj 		htaddr->htfc = dbtofsb(bp->b_blkno) - blkno;
2852926Swnj 		htaddr->htcs1 = HT_SREV|HT_GO;
28622Sbill 	}
2872926Swnj 	return (MBU_STARTED);
28822Sbill }
28922Sbill 
2903094Swnj htdtint(mi, mbsr)
2912980Swnj 	register struct mba_device *mi;
2923094Swnj 	int mbsr;
29322Sbill {
2942926Swnj 	register struct htdevice *htaddr = (struct htdevice *)mi->mi_drv;
2952926Swnj 	register struct buf *bp = mi->mi_tab.b_actf;
2963094Swnj 	register struct tu_softc *sc;
2972961Swnj 	int ds, er, mbs;
29822Sbill 
2993094Swnj 	sc = &tu_softc[TUUNIT(bp->b_dev)];
3002926Swnj 	ds = sc->sc_dsreg = MASKREG(htaddr->htds);
3012926Swnj 	er = sc->sc_erreg = MASKREG(htaddr->hter);
3022926Swnj 	sc->sc_resid = MASKREG(htaddr->htfc);
3033094Swnj 	mbs = mbsr;
3042926Swnj 	sc->sc_blkno++;
3052926Swnj 	if((bp->b_flags & B_READ) == 0)
3062926Swnj 		sc->sc_flags |= H_WRITTEN;
3073094Swnj 	if ((ds&(HTDS_ERR|HTDS_MOL)) != HTDS_MOL || mbs & MBSR_EBITS) {
3082926Swnj 		htaddr->htcs1 = HT_DCLR|HT_GO;
3092961Swnj 		mbclrattn(mi);
3102961Swnj 		if (bp == &rhtbuf[HTUNIT(bp->b_dev)]) {
3112926Swnj 			er &= ~HTER_FCE;
3123094Swnj 			mbs &= ~(MBSR_DTABT|MBSR_MBEXC);
3134276Sroot 		}
3142926Swnj 		if (bp->b_flags & B_READ && ds & HTDS_PES)
3152926Swnj 			er &= ~(HTER_CSITM|HTER_CORCRC);
3163094Swnj 		if (er&HTER_HARD || mbs&MBSR_EBITS || (ds&HTDS_MOL) == 0 ||
3172961Swnj 		    er && ++mi->mi_tab.b_errcnt >= 7) {
3182926Swnj 			if ((ds & HTDS_MOL) == 0 && sc->sc_openf > 0)
3192926Swnj 				sc->sc_openf = -1;
3203157Swnj 			if ((er&HTER_HARD) == HTER_FCE &&
3213157Swnj 			    (mbs&MBSR_EBITS) == (MBSR_DTABT|MBSR_MBEXC) &&
3223157Swnj 			    (ds&HTDS_MOL))
3233157Swnj 				goto noprint;
3243204Swnj 			printf("tu%d: hard error bn%d mbsr=%b er=%b ds=%b\n",
3252980Swnj 			    TUUNIT(bp->b_dev), bp->b_blkno,
3263094Swnj 			    mbsr, mbsr_bits,
3273204Swnj 			    sc->sc_erreg, hter_bits,
3283204Swnj 			    sc->sc_dsreg, htds_bits);
3293157Swnj noprint:
33022Sbill 			bp->b_flags |= B_ERROR;
3312926Swnj 			return (MBD_DONE);
33222Sbill 		}
3332926Swnj 		if (er)
3342926Swnj 			return (MBD_RETRY);
33522Sbill 	}
3362926Swnj 	bp->b_resid = 0;
3372926Swnj 	if (bp->b_flags & B_READ)
3382926Swnj 		if (ds&HTDS_TM) {		/* must be a read, right? */
3392926Swnj 			bp->b_resid = bp->b_bcount;
3402926Swnj 			sc->sc_nxrec = dbtofsb(bp->b_blkno);
3412926Swnj 		} else if(bp->b_bcount > MASKREG(htaddr->htfc))
3422926Swnj 			bp->b_resid = bp->b_bcount - MASKREG(htaddr->htfc);
3432926Swnj 	return (MBD_DONE);
3442926Swnj }
34522Sbill 
3462926Swnj htndtint(mi)
3472980Swnj 	register struct mba_device *mi;
3482926Swnj {
3492926Swnj 	register struct htdevice *htaddr = (struct htdevice *)mi->mi_drv;
3502926Swnj 	register struct buf *bp = mi->mi_tab.b_actf;
3513094Swnj 	register struct tu_softc *sc;
3522926Swnj 	int er, ds, fc;
35322Sbill 
3543094Swnj 	ds = MASKREG(htaddr->htds);
3553094Swnj 	er = MASKREG(htaddr->hter);
3563094Swnj 	fc = MASKREG(htaddr->htfc);
3573094Swnj 	if (er) {
3582926Swnj 		htaddr->htcs1 = HT_DCLR|HT_GO;
3592961Swnj 		mbclrattn(mi);
3602961Swnj 	}
3613094Swnj 	if (bp == 0)
3623094Swnj 		return (MBN_SKIP);
3633094Swnj 	sc = &tu_softc[TUUNIT(bp->b_dev)];
3643094Swnj 	sc->sc_dsreg = ds;
3653094Swnj 	sc->sc_erreg = er;
3663094Swnj 	sc->sc_resid = fc;
3673094Swnj 	if (bp == &chtbuf[HTUNIT(bp->b_dev)]) {
3683094Swnj 		switch (bp->b_command) {
3693094Swnj 		case HT_REWOFFL:
3702926Swnj 			/* offline is on purpose; don't do anything special */
3712926Swnj 			ds |= HTDS_MOL;
3723094Swnj 			break;
3733094Swnj 		case HT_SREV:
3743094Swnj 			/* if backspace file hit bot, its not an error */
3753094Swnj 		        if (er == (HTER_NEF|HTER_FCE) && ds&HTDS_BOT &&
3763094Swnj 			    bp->b_repcnt == INF)
3773094Swnj 				er &= ~HTER_NEF;
3783094Swnj 			break;
3793094Swnj 		}
3802926Swnj 		er &= ~HTER_FCE;
3812926Swnj 		if (er == 0)
3822926Swnj 			ds &= ~HTDS_ERR;
38322Sbill 	}
3842926Swnj 	if ((ds & (HTDS_ERR|HTDS_MOL)) != HTDS_MOL) {
3852926Swnj 		if ((ds & HTDS_MOL) == 0 && sc->sc_openf > 0)
3862926Swnj 			sc->sc_openf = -1;
3873204Swnj 		printf("tu%d: hard error bn%d er=%b ds=%b\n",
3882980Swnj 		    TUUNIT(bp->b_dev), bp->b_blkno,
3893204Swnj 		    sc->sc_erreg, hter_bits, sc->sc_dsreg, htds_bits);
3902926Swnj 		bp->b_flags |= B_ERROR;
3912926Swnj 		return (MBN_DONE);
3922926Swnj 	}
3933094Swnj 	if (bp == &chtbuf[HTUNIT(bp->b_dev)]) {
3942926Swnj 		if (sc->sc_flags & H_REWIND)
3952926Swnj 			return (ds & HTDS_BOT ? MBN_DONE : MBN_RETRY);
3962926Swnj 		bp->b_resid = -sc->sc_resid;
3972926Swnj 		return (MBN_DONE);
3982926Swnj 	}
3992926Swnj 	if (ds & HTDS_TM)
4003094Swnj 		if (sc->sc_blkno > dbtofsb(bp->b_blkno)) {
4012926Swnj 			sc->sc_nxrec = dbtofsb(bp->b_blkno) - fc;
4022926Swnj 			sc->sc_blkno = sc->sc_nxrec;
4033094Swnj 		} else {
4042926Swnj 			sc->sc_blkno = dbtofsb(bp->b_blkno) + fc;
4052926Swnj 			sc->sc_nxrec = sc->sc_blkno - 1;
4062926Swnj 		}
4072926Swnj 	else
4082926Swnj 		sc->sc_blkno = dbtofsb(bp->b_blkno);
4092926Swnj 	return (MBN_RETRY);
41022Sbill }
41122Sbill 
41222Sbill htread(dev)
4132926Swnj 	dev_t dev;
41422Sbill {
4152926Swnj 
41622Sbill 	htphys(dev);
4172926Swnj 	if (u.u_error)
4182926Swnj 		return;
4192926Swnj 	physio(htstrategy, &rhtbuf[HTUNIT(dev)], dev, B_READ, minphys);
42022Sbill }
42122Sbill 
42222Sbill htwrite(dev)
42322Sbill {
4242926Swnj 
42522Sbill 	htphys(dev);
4262926Swnj 	if (u.u_error)
4272926Swnj 		return;
4282926Swnj 	physio(htstrategy, &rhtbuf[HTUNIT(dev)], dev, B_WRITE, minphys);
42922Sbill }
43022Sbill 
43122Sbill htphys(dev)
4322926Swnj 	dev_t dev;
43322Sbill {
4343094Swnj 	register int htunit;
4353094Swnj 	register struct tu_softc *sc;
4363094Swnj 	register struct mba_device *mi;
43722Sbill 	daddr_t a;
43822Sbill 
4393094Swnj 	htunit = HTUNIT(dev);
4403094Swnj 	if (htunit >= NHT || (mi = htinfo[htunit]) == 0 || mi->mi_alive == 0) {
4412926Swnj 		u.u_error = ENXIO;
4422926Swnj 		return;
44322Sbill 	}
4442926Swnj 	a = u.u_offset >> 9;
4453094Swnj 	sc = &tu_softc[TUUNIT(dev)];
4462926Swnj 	sc->sc_blkno = dbtofsb(a);
4472926Swnj 	sc->sc_nxrec = dbtofsb(a)+1;
44822Sbill }
4491917Swnj 
4502926Swnj /*ARGSUSED*/
4512926Swnj htioctl(dev, cmd, addr, flag)
4522926Swnj 	dev_t dev;
4532926Swnj 	int cmd;
4542926Swnj 	caddr_t addr;
4552926Swnj 	int flag;
4562926Swnj {
4573094Swnj 	register struct tu_softc *sc = &tu_softc[TUUNIT(dev)];
4583094Swnj 	register struct buf *bp = &chtbuf[HTUNIT(dev)];
4592926Swnj 	register callcount;
4602926Swnj 	int fcount;
4612926Swnj 	struct mtop mtop;
4622926Swnj 	struct mtget mtget;
4632926Swnj 	/* we depend of the values and order of the MT codes here */
4642926Swnj 	static htops[] =
4652926Swnj    {HT_WEOF,HT_SFORW,HT_SREV,HT_SFORW,HT_SREV,HT_REW,HT_REWOFFL,HT_SENSE};
4661917Swnj 
4672926Swnj 	switch (cmd) {
4682926Swnj 		case MTIOCTOP:	/* tape operation */
4692926Swnj 		if (copyin((caddr_t)addr, (caddr_t)&mtop, sizeof(mtop))) {
4702926Swnj 			u.u_error = EFAULT;
4712926Swnj 			return;
4722926Swnj 		}
4732926Swnj 		switch(mtop.mt_op) {
4742926Swnj 		case MTWEOF:
4752926Swnj 			callcount = mtop.mt_count;
4762926Swnj 			fcount = 1;
4772926Swnj 			break;
4782926Swnj 		case MTFSF: case MTBSF:
4792926Swnj 			callcount = mtop.mt_count;
4802926Swnj 			fcount = INF;
4812926Swnj 			break;
4822926Swnj 		case MTFSR: case MTBSR:
4832926Swnj 			callcount = 1;
4842926Swnj 			fcount = mtop.mt_count;
4852926Swnj 			break;
4862926Swnj 		case MTREW: case MTOFFL:
4872926Swnj 			callcount = 1;
4882926Swnj 			fcount = 1;
4892926Swnj 			break;
4902926Swnj 		default:
4912926Swnj 			u.u_error = ENXIO;
4922926Swnj 			return;
4932926Swnj 		}
4942926Swnj 		if (callcount <= 0 || fcount <= 0) {
4952926Swnj 			u.u_error = ENXIO;
4962926Swnj 			return;
4972926Swnj 		}
4982926Swnj 		while (--callcount >= 0) {
4992926Swnj 			htcommand(dev, htops[mtop.mt_op], fcount);
5002926Swnj 			if ((mtop.mt_op == MTFSR || mtop.mt_op == MTBSR) &&
5012926Swnj 			    bp->b_resid) {
5022926Swnj 				u.u_error = EIO;
5032926Swnj 				break;
5042926Swnj 			}
5053094Swnj 			if ((bp->b_flags&B_ERROR) || sc->sc_dsreg&HTDS_BOT)
5062926Swnj 				break;
5072926Swnj 		}
5082926Swnj 		geterror(bp);
5092926Swnj 		return;
5102926Swnj 	case MTIOCGET:
5112926Swnj 		mtget.mt_dsreg = sc->sc_dsreg;
5122926Swnj 		mtget.mt_erreg = sc->sc_erreg;
5132926Swnj 		mtget.mt_resid = sc->sc_resid;
5143480Stoy 		mtget.mt_type = MT_ISHT;
5152926Swnj 		if (copyout((caddr_t)&mtget, addr, sizeof(mtget)))
5162926Swnj 			u.u_error = EFAULT;
5172926Swnj 		return;
5182926Swnj 	default:
5192926Swnj 		u.u_error = ENXIO;
5202926Swnj 	}
5212926Swnj }
5222926Swnj 
5231917Swnj #define	DBSIZE	20
5241917Swnj 
5252926Swnj htdump()
5261917Swnj {
5272980Swnj 	register struct mba_device *mi;
5282926Swnj 	register struct mba_regs *mp;
5292926Swnj 	register struct htdevice *htaddr;
5302926Swnj 	int blk, num;
5312926Swnj 	int start;
5321917Swnj 
5332926Swnj 	start = 0;
5342926Swnj 	num = maxfree;
5352926Swnj #define	phys(a,b)		((b)((int)(a)&0x7fffffff))
5362926Swnj 	if (htinfo[0] == 0)
5372926Swnj 		return (ENXIO);
5382980Swnj 	mi = phys(htinfo[0], struct mba_device *);
5392926Swnj 	mp = phys(mi->mi_hd, struct mba_hd *)->mh_physmba;
5403157Swnj 	mp->mba_cr = MBCR_IE;
5412926Swnj 	htaddr = (struct htdevice *)&mp->mba_drv[mi->mi_drive];
5422926Swnj 	htaddr->httc = HTTC_PDP11|HTTC_1600BPI;
5432926Swnj 	htaddr->htcs1 = HT_DCLR|HT_GO;
5441917Swnj 	while (num > 0) {
5451917Swnj 		blk = num > DBSIZE ? DBSIZE : num;
5462926Swnj 		htdwrite(start, blk, htaddr, mp);
5472926Swnj 		start += blk;
5481917Swnj 		num -= blk;
5491917Swnj 	}
5503157Swnj 	hteof(htaddr);
5513157Swnj 	hteof(htaddr);
5522926Swnj 	htwait(htaddr);
5533181Swnj 	if (htaddr->htds&HTDS_ERR)
5543157Swnj 		return (EIO);
5552926Swnj 	htaddr->htcs1 = HT_REW|HT_GO;
5563103Swnj 	return (0);
5571917Swnj }
5581917Swnj 
5592926Swnj htdwrite(dbuf, num, htaddr, mp)
5602926Swnj 	register dbuf, num;
5612926Swnj 	register struct htdevice *htaddr;
5622926Swnj 	struct mba_regs *mp;
5631917Swnj {
5642926Swnj 	register struct pte *io;
5651917Swnj 	register int i;
5661917Swnj 
5672926Swnj 	htwait(htaddr);
5682926Swnj 	io = mp->mba_map;
5691917Swnj 	for (i = 0; i < num; i++)
5702926Swnj 		*(int *)io++ = dbuf++ | PG_V;
5712926Swnj 	htaddr->htfc = -(num*NBPG);
5722926Swnj 	mp->mba_sr = -1;
5732926Swnj 	mp->mba_bcr = -(num*NBPG);
5742926Swnj 	mp->mba_var = 0;
5752926Swnj 	htaddr->htcs1 = HT_WCOM|HT_GO;
5761917Swnj }
5771917Swnj 
5782926Swnj htwait(htaddr)
5792926Swnj 	struct htdevice *htaddr;
5801917Swnj {
5811917Swnj 	register s;
5821917Swnj 
5831917Swnj 	do
5842926Swnj 		s = htaddr->htds;
5852926Swnj 	while ((s & HTDS_DRY) == 0);
5861917Swnj }
5871917Swnj 
5882926Swnj hteof(htaddr)
5892926Swnj 	struct htdevice *htaddr;
5901917Swnj {
5911917Swnj 
5922926Swnj 	htwait(htaddr);
5932926Swnj 	htaddr->htcs1 = HT_WEOF|HT_GO;
5941917Swnj }
5951563Sbill #endif
596