Home | History | Annotate | Line # | Download | only in dev
ct.c revision 1.9
      1 /*	$NetBSD: ct.c,v 1.9 1994/10/26 07:23:28 cgd Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1990, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  *	@(#)ct.c	8.2 (Berkeley) 1/12/94
     36  */
     37 
     38 #include "ct.h"
     39 #if NCT > 0
     40 /*
     41  * CS80 cartridge tape driver (9144, 88140, 9145)
     42  *
     43  * Reminder:
     44  *	C_CC bit (character count option) when used in the CS/80 command
     45  *	'set options' will cause the tape not to stream.
     46  *
     47  * TODO:
     48  *	make filesystem compatible
     49  *	make block mode work according to mtio(4) spec. (if possible)
     50  *	merge with cs80 disk driver
     51  *	finish support of 9145
     52  */
     53 
     54 #include <sys/param.h>
     55 #include <sys/buf.h>
     56 #include <sys/ioctl.h>
     57 #include <sys/mtio.h>
     58 #include <sys/tprintf.h>
     59 #include <sys/proc.h>
     60 
     61 #include <hp300/dev/device.h>
     62 #include <hp300/dev/ctreg.h>
     63 
     64 /* number of eof marks to remember */
     65 #define EOFS	128
     66 
     67 int	ctinit(), ctstart(), ctgo(), ctintr();
     68 void	ctstrategy();
     69 struct	driver ctdriver = {
     70 	ctinit, "ct", ctstart, ctgo, ctintr,
     71 };
     72 
     73 struct	ct_softc {
     74 	struct	hp_device *sc_hd;
     75 	struct	ct_iocmd sc_ioc;
     76 	struct	ct_rscmd sc_rsc;
     77 	struct	ct_stat sc_stat;
     78 	struct	ct_ssmcmd sc_ssmc;
     79 	struct	ct_srcmd sc_src;
     80 	struct	ct_soptcmd sc_soptc;
     81 	struct	ct_ulcmd sc_ul;
     82 	struct	ct_wfmcmd sc_wfm;
     83 	struct	ct_clearcmd sc_clear;
     84 	struct	buf *sc_bp;
     85 	int	sc_blkno;
     86 	int	sc_cmd;
     87 	int	sc_resid;
     88 	char	*sc_addr;
     89 	int	sc_flags;
     90 	short	sc_type;
     91 	short	sc_punit;
     92 	tpr_t	sc_tpr;
     93 	struct	devqueue sc_dq;
     94 	int	sc_eofp;
     95 	int	sc_eofs[EOFS];
     96 } ct_softc[NCT];
     97 
     98 /* flags */
     99 #define	CTF_OPEN	0x01
    100 #define	CTF_ALIVE	0x02
    101 #define	CTF_WRT		0x04
    102 #define	CTF_CMD		0x08
    103 #define	CTF_IO		0x10
    104 #define	CTF_BEOF	0x20
    105 #define	CTF_AEOF	0x40
    106 #define	CTF_EOT		0x80
    107 #define	CTF_STATWAIT	0x100
    108 #define CTF_CANSTREAM	0x200
    109 #define	CTF_WRTTN	0x400
    110 
    111 struct	ctinfo {
    112 	short	hwid;
    113 	short	punit;
    114 	char	*desc;
    115 } ctinfo[] = {
    116 	CT7946ID,	1,	"7946A",
    117 	CT7912PID,	1,	"7912P",
    118 	CT7914PID,	1,	"7914P",
    119 	CT9144ID,	0,	"9144",
    120 	CT9145ID,	0,	"9145",
    121 };
    122 int	nctinfo = sizeof(ctinfo) / sizeof(ctinfo[0]);
    123 
    124 struct	buf cttab[NCT];
    125 struct	buf ctbuf[NCT];
    126 
    127 #define	CT_NOREW	4
    128 #define	CT_STREAM	8
    129 #define	UNIT(x)		(minor(x) & 3)
    130 #define	ctpunit(x)	((x) & 7)
    131 
    132 #ifdef DEBUG
    133 int ctdebug = 0;
    134 #define CDB_FILES	0x01
    135 #define CT_BSF		0x02
    136 #endif
    137 
    138 ctinit(hd)
    139 	register struct hp_device *hd;
    140 {
    141 	register struct ct_softc *sc = &ct_softc[hd->hp_unit];
    142 	register struct buf *bp;
    143 
    144 	for (bp = cttab; bp < &cttab[NCT]; bp++)
    145 		bp->b_actb = &bp->b_actf;
    146 	sc->sc_hd = hd;
    147 	sc->sc_punit = ctpunit(hd->hp_flags);
    148 	if (ctident(sc, hd) < 0)
    149 		return(0);
    150 	ctreset(sc, hd);
    151 	sc->sc_dq.dq_ctlr = hd->hp_ctlr;
    152 	sc->sc_dq.dq_unit = hd->hp_unit;
    153 	sc->sc_dq.dq_slave = hd->hp_slave;
    154 	sc->sc_dq.dq_driver = &ctdriver;
    155 	sc->sc_flags |= CTF_ALIVE;
    156 	return(1);
    157 }
    158 
    159 ctident(sc, hd)
    160 	register struct ct_softc *sc;
    161 	register struct hp_device *hd;
    162 {
    163 	struct ct_describe desc;
    164 	u_char stat, cmd[3];
    165 	char name[7];
    166 	int id, i;
    167 
    168 	/*
    169 	 * Read device id and verify that:
    170 	 * 1. It is a CS80 device
    171 	 * 2. It is one of our recognized tape devices
    172 	 * 3. It has the proper physical unit number
    173 	 */
    174 	id = hpibid(hd->hp_ctlr, hd->hp_slave);
    175 	if ((id & 0x200) == 0)
    176 		return(-1);
    177 	for (i = 0; i < nctinfo; i++)
    178 		if (id == ctinfo[i].hwid)
    179 			break;
    180 	if (i == nctinfo || sc->sc_punit != ctinfo[i].punit)
    181 		return(-1);
    182 	id = i;
    183 
    184 	/*
    185 	 * Collect device description.
    186 	 * Right now we only need this to differentiate 7945 from 7946.
    187 	 * Note that we always issue the describe command to unit 0.
    188 	 */
    189 	cmd[0] = C_SUNIT(0);
    190 	cmd[1] = C_SVOL(0);
    191 	cmd[2] = C_DESC;
    192 	hpibsend(hd->hp_ctlr, hd->hp_slave, C_CMD, cmd, sizeof(cmd));
    193 	hpibrecv(hd->hp_ctlr, hd->hp_slave, C_EXEC, &desc, 37);
    194 	hpibrecv(hd->hp_ctlr, hd->hp_slave, C_QSTAT, &stat, sizeof(stat));
    195 	bzero(name, sizeof(name));
    196 	if (!stat) {
    197 		register int n = desc.d_name;
    198 		for (i = 5; i >= 0; i--) {
    199 			name[i] = (n & 0xf) + '0';
    200 			n >>= 4;
    201 		}
    202 	}
    203 	switch (ctinfo[id].hwid) {
    204 	case CT7946ID:
    205 		if (bcmp(name, "079450", 6) == 0)
    206 			return(-1);		/* not really a 7946 */
    207 		/* fall into... */
    208 	case CT9144ID:
    209 	case CT9145ID:
    210 		sc->sc_type = CT9144;
    211 		sc->sc_flags |= CTF_CANSTREAM;
    212 		break;
    213 
    214 	case CT7912PID:
    215 	case CT7914PID:
    216 		sc->sc_type = CT88140;
    217 		break;
    218 	}
    219 	printf("ct%d: %s %stape\n", hd->hp_unit, ctinfo[id].desc,
    220 	       (sc->sc_flags & CTF_CANSTREAM) ? "streaming " : " ");
    221 	return(id);
    222 }
    223 
    224 ctreset(sc, hd)
    225 	register struct ct_softc *sc;
    226 	register struct hp_device *hd;
    227 {
    228 	u_char stat;
    229 
    230 	sc->sc_clear.unit = C_SUNIT(sc->sc_punit);
    231 	sc->sc_clear.cmd = C_CLEAR;
    232 	hpibsend(hd->hp_ctlr, hd->hp_slave, C_TCMD, &sc->sc_clear,
    233 		sizeof(sc->sc_clear));
    234 	hpibswait(hd->hp_ctlr, hd->hp_slave);
    235 	hpibrecv(hd->hp_ctlr, hd->hp_slave, C_QSTAT, &stat, sizeof(stat));
    236 	sc->sc_src.unit = C_SUNIT(CTCTLR);
    237 	sc->sc_src.nop = C_NOP;
    238 	sc->sc_src.cmd = C_SREL;
    239 	sc->sc_src.param = C_REL;
    240 	hpibsend(hd->hp_ctlr, hd->hp_slave, C_CMD, &sc->sc_src,
    241 		sizeof(sc->sc_src));
    242 	hpibswait(hd->hp_ctlr, hd->hp_slave);
    243 	hpibrecv(hd->hp_ctlr, hd->hp_slave, C_QSTAT, &stat, sizeof(stat));
    244 	sc->sc_ssmc.unit = C_SUNIT(sc->sc_punit);
    245 	sc->sc_ssmc.cmd = C_SSM;
    246 	sc->sc_ssmc.refm = REF_MASK;
    247 	sc->sc_ssmc.fefm = FEF_MASK;
    248 	sc->sc_ssmc.aefm = AEF_MASK;
    249 	sc->sc_ssmc.iefm = IEF_MASK;
    250 	hpibsend(hd->hp_ctlr, hd->hp_slave, C_CMD, &sc->sc_ssmc,
    251 		sizeof(sc->sc_ssmc));
    252 	hpibswait(hd->hp_ctlr, hd->hp_slave);
    253 	hpibrecv(hd->hp_ctlr, hd->hp_slave, C_QSTAT, &stat, sizeof(stat));
    254 	sc->sc_soptc.unit = C_SUNIT(sc->sc_punit);
    255 	sc->sc_soptc.nop = C_NOP;
    256 	sc->sc_soptc.cmd = C_SOPT;
    257 	sc->sc_soptc.opt = C_SPAR;
    258 	hpibsend(hd->hp_ctlr, hd->hp_slave, C_CMD, &sc->sc_soptc,
    259 		sizeof(sc->sc_soptc));
    260 	hpibswait(hd->hp_ctlr, hd->hp_slave);
    261 	hpibrecv(hd->hp_ctlr, hd->hp_slave, C_QSTAT, &stat, sizeof(stat));
    262 }
    263 
    264 /*ARGSUSED*/
    265 ctopen(dev, flag, type, p)
    266 	dev_t dev;
    267 	int flag, type;
    268 	struct proc *p;
    269 {
    270 	register struct ct_softc *sc = &ct_softc[UNIT(dev)];
    271 	u_char stat;
    272 	int cc;
    273 
    274 	if (UNIT(dev) >= NCT || (sc->sc_flags & CTF_ALIVE) == 0)
    275 		return(ENXIO);
    276 	if (sc->sc_flags & CTF_OPEN)
    277 		return(EBUSY);
    278 	sc->sc_soptc.unit = C_SUNIT(sc->sc_punit);
    279 	sc->sc_soptc.nop = C_NOP;
    280 	sc->sc_soptc.cmd = C_SOPT;
    281 	if ((dev & CT_STREAM) && (sc->sc_flags & CTF_CANSTREAM))
    282 		sc->sc_soptc.opt = C_SPAR | C_IMRPT;
    283 	else
    284 		sc->sc_soptc.opt = C_SPAR;
    285 	/*
    286 	 * Check the return of hpibsend() and hpibswait().
    287 	 * Drive could be loading/unloading a tape. If not checked,
    288 	 * driver hangs.
    289 	 */
    290 	cc = hpibsend(sc->sc_hd->hp_ctlr, sc->sc_hd->hp_slave,
    291 	              C_CMD, &sc->sc_soptc, sizeof(sc->sc_soptc));
    292 	if (cc != sizeof(sc->sc_soptc))
    293 		return(EBUSY);
    294 	hpibswait(sc->sc_hd->hp_ctlr, sc->sc_hd->hp_slave);
    295 	cc = hpibrecv(sc->sc_hd->hp_ctlr, sc->sc_hd->hp_slave, C_QSTAT,
    296 	              &stat, sizeof(stat));
    297 	if (cc != sizeof(stat))
    298 		return(EBUSY);
    299 	sc->sc_tpr = tprintf_open(p);
    300 	sc->sc_flags |= CTF_OPEN;
    301 	return(0);
    302 }
    303 
    304 /*ARGSUSED*/
    305 ctclose(dev, flag)
    306 	dev_t dev;
    307 	int flag;
    308 {
    309 	register struct ct_softc *sc = &ct_softc[UNIT(dev)];
    310 
    311 	if ((sc->sc_flags & (CTF_WRT|CTF_WRTTN)) == (CTF_WRT|CTF_WRTTN) &&
    312 	    (sc->sc_flags & CTF_EOT) == 0 ) { /* XXX return error if EOT ?? */
    313 		ctcommand(dev, MTWEOF, 2);
    314 		ctcommand(dev, MTBSR, 1);
    315 		if (sc->sc_eofp == EOFS - 1)
    316 			sc->sc_eofs[EOFS - 1]--;
    317 		else
    318 			sc->sc_eofp--;
    319 #ifdef DEBUG
    320 		if(ctdebug & CT_BSF)
    321 			printf("ct%d: ctclose backup eofs prt %d blk %d\n",
    322 			       UNIT(dev), sc->sc_eofp, sc->sc_eofs[sc->sc_eofp]);
    323 #endif
    324 	}
    325 	if ((minor(dev) & CT_NOREW) == 0)
    326 		ctcommand(dev, MTREW, 1);
    327 	sc->sc_flags &= ~(CTF_OPEN | CTF_WRT | CTF_WRTTN);
    328 	tprintf_close(sc->sc_tpr);
    329 #ifdef DEBUG
    330 	if (ctdebug & CDB_FILES)
    331 		printf("ctclose: flags %x\n", sc->sc_flags);
    332 #endif
    333 	return(0);	/* XXX */
    334 }
    335 
    336 ctcommand(dev, cmd, cnt)
    337 	dev_t dev;
    338 	int cmd;
    339 	register int cnt;
    340 {
    341 	register struct ct_softc *sc = &ct_softc[UNIT(dev)];
    342 	register struct buf *bp = &ctbuf[UNIT(dev)];
    343 	register struct buf *nbp = 0;
    344 
    345 	if (cmd == MTBSF && sc->sc_eofp == EOFS - 1) {
    346 		cnt = sc->sc_eofs[EOFS - 1] - cnt;
    347 		ctcommand(dev, MTREW, 1);
    348 		ctcommand(dev, MTFSF, cnt);
    349 		cnt = 2;
    350 		cmd = MTBSR;
    351 	}
    352 
    353 	if (cmd == MTBSF && sc->sc_eofp - cnt < 0) {
    354 		cnt = 1;
    355 		cmd = MTREW;
    356 	}
    357 
    358 	sc->sc_flags |= CTF_CMD;
    359 	sc->sc_bp = bp;
    360 	sc->sc_cmd = cmd;
    361 	bp->b_dev = dev;
    362 	if (cmd == MTFSF) {
    363 		nbp = (struct buf *)geteblk(MAXBSIZE);
    364 		bp->b_un.b_addr = nbp->b_un.b_addr;
    365 		bp->b_bcount = MAXBSIZE;
    366 	}
    367 again:
    368 	bp->b_flags = B_BUSY;
    369 	if (cmd == MTBSF) {
    370 		sc->sc_blkno = sc->sc_eofs[sc->sc_eofp];
    371 		sc->sc_eofp--;
    372 #ifdef DEBUG
    373 		if (ctdebug & CT_BSF)
    374 			printf("ct%d: backup eof pos %d blk %d\n",
    375 			       UNIT(dev), sc->sc_eofp,
    376 			       sc->sc_eofs[sc->sc_eofp]);
    377 #endif
    378 	}
    379 	ctstrategy(bp);
    380 	iowait(bp);
    381 	if (--cnt > 0)
    382 		goto again;
    383 	bp->b_flags = 0;
    384 	sc->sc_flags &= ~CTF_CMD;
    385 	if (nbp)
    386 		brelse(nbp);
    387 }
    388 
    389 void
    390 ctstrategy(bp)
    391 	register struct buf *bp;
    392 {
    393 	register struct buf *dp;
    394 	register int s, unit;
    395 
    396 	unit = UNIT(bp->b_dev);
    397 	dp = &cttab[unit];
    398 	bp->b_actf = NULL;
    399 	s = splbio();
    400 	bp->b_actb = dp->b_actb;
    401 	*dp->b_actb = bp;
    402 	dp->b_actb = &bp->b_actf;
    403 	if (dp->b_active == 0) {
    404 		dp->b_active = 1;
    405 		ctustart(unit);
    406 	}
    407 	splx(s);
    408 }
    409 
    410 ctustart(unit)
    411 	register int unit;
    412 {
    413 	register struct ct_softc *sc = &ct_softc[unit];
    414 	register struct buf *bp;
    415 
    416 	bp = cttab[unit].b_actf;
    417 	sc->sc_addr = bp->b_un.b_addr;
    418 	sc->sc_resid = bp->b_bcount;
    419 	if (hpibreq(&sc->sc_dq))
    420 		ctstart(unit);
    421 }
    422 
    423 ctstart(unit)
    424 	register int unit;
    425 {
    426 	register struct ct_softc *sc = &ct_softc[unit];
    427 	register struct buf *bp, *dp;
    428 	register int i;
    429 
    430 	bp = cttab[unit].b_actf;
    431 again:
    432 	if ((sc->sc_flags & CTF_CMD) && sc->sc_bp == bp) {
    433 		switch(sc->sc_cmd) {
    434 
    435 		case MTFSF:
    436 			bp->b_flags |= B_READ;
    437 			goto mustio;
    438 
    439 		case MTBSF:
    440 			goto gotaddr;
    441 
    442 		case MTOFFL:
    443 			sc->sc_blkno = 0;
    444 			sc->sc_ul.unit = C_SUNIT(sc->sc_punit);
    445 			sc->sc_ul.cmd = C_UNLOAD;
    446 			hpibsend(sc->sc_hd->hp_ctlr, sc->sc_hd->hp_slave,
    447 				C_CMD, &sc->sc_ul, sizeof(sc->sc_ul));
    448 			break;
    449 
    450 		case MTWEOF:
    451 			sc->sc_blkno++;
    452 			sc->sc_flags |= CTF_WRT;
    453 			sc->sc_wfm.unit = C_SUNIT(sc->sc_punit);
    454 			sc->sc_wfm.cmd = C_WFM;
    455 			hpibsend(sc->sc_hd->hp_ctlr, sc->sc_hd->hp_slave,
    456 				C_CMD, &sc->sc_wfm, sizeof(sc->sc_wfm));
    457 			ctaddeof(unit);
    458 			break;
    459 
    460 		case MTBSR:
    461 			sc->sc_blkno--;
    462 			goto gotaddr;
    463 
    464 		case MTFSR:
    465 			sc->sc_blkno++;
    466 			goto gotaddr;
    467 
    468 		case MTREW:
    469 			sc->sc_blkno = 0;
    470 #ifdef DEBUG
    471 			if(ctdebug & CT_BSF)
    472 				printf("ct%d: clearing eofs\n", unit);
    473 #endif
    474 			for (i=0; i<EOFS; i++)
    475 				sc->sc_eofs[i] = 0;
    476 			sc->sc_eofp = 0;
    477 
    478 gotaddr:
    479 			sc->sc_ioc.saddr = C_SADDR;
    480 			sc->sc_ioc.addr0 = 0;
    481 			sc->sc_ioc.addr = sc->sc_blkno;
    482 			sc->sc_ioc.unit = C_SUNIT(sc->sc_punit);
    483 			sc->sc_ioc.nop2 = C_NOP;
    484 			sc->sc_ioc.slen = C_SLEN;
    485 			sc->sc_ioc.len = 0;
    486 			sc->sc_ioc.nop3 = C_NOP;
    487 			sc->sc_ioc.cmd = C_READ;
    488 			hpibsend(sc->sc_hd->hp_ctlr, sc->sc_hd->hp_slave,
    489 				C_CMD, &sc->sc_ioc, sizeof(sc->sc_ioc));
    490 			break;
    491 		}
    492 	}
    493 	else {
    494 mustio:
    495 		if ((bp->b_flags & B_READ) &&
    496 		    sc->sc_flags & (CTF_BEOF|CTF_EOT)) {
    497 #ifdef DEBUG
    498 			if (ctdebug & CDB_FILES)
    499 				printf("ctstart: before flags %x\n", sc->sc_flags);
    500 #endif
    501 			if (sc->sc_flags & CTF_BEOF) {
    502 				sc->sc_flags &= ~CTF_BEOF;
    503 				sc->sc_flags |= CTF_AEOF;
    504 #ifdef DEBUG
    505 				if (ctdebug & CDB_FILES)
    506 					printf("ctstart: after flags %x\n", sc->sc_flags);
    507 #endif
    508 			}
    509 			bp->b_resid = bp->b_bcount;
    510 			iodone(bp);
    511 			hpibfree(&sc->sc_dq);
    512 			if (dp = bp->b_actf)
    513 				dp->b_actb = bp->b_actb;
    514 			else
    515 				cttab[unit].b_actb = bp->b_actb;
    516 			*bp->b_actb = dp;
    517 			if ((bp = dp) == NULL) {
    518 				cttab[unit].b_active = 0;
    519 				return;
    520 			}
    521 			sc->sc_addr = bp->b_un.b_addr;
    522 			sc->sc_resid = bp->b_bcount;
    523 			if (hpibreq(&sc->sc_dq))
    524 				goto again;
    525 			return;
    526 		}
    527 		sc->sc_flags |= CTF_IO;
    528 		sc->sc_ioc.unit = C_SUNIT(sc->sc_punit);
    529 		sc->sc_ioc.saddr = C_SADDR;
    530 		sc->sc_ioc.addr0 = 0;
    531 		sc->sc_ioc.addr = sc->sc_blkno;
    532 		sc->sc_ioc.nop2 = C_NOP;
    533 		sc->sc_ioc.slen = C_SLEN;
    534 		sc->sc_ioc.len = sc->sc_resid;
    535 		sc->sc_ioc.nop3 = C_NOP;
    536 		if (bp->b_flags & B_READ)
    537 			sc->sc_ioc.cmd = C_READ;
    538 		else {
    539 			sc->sc_ioc.cmd = C_WRITE;
    540 			sc->sc_flags |= (CTF_WRT | CTF_WRTTN);
    541 		}
    542 		hpibsend(sc->sc_hd->hp_ctlr, sc->sc_hd->hp_slave, C_CMD,
    543 			&sc->sc_ioc, sizeof(sc->sc_ioc));
    544 	}
    545 	hpibawait(sc->sc_hd->hp_ctlr);
    546 }
    547 
    548 ctgo(unit)
    549 	register int unit;
    550 {
    551 	register struct ct_softc *sc = &ct_softc[unit];
    552 	register struct buf *bp;
    553 
    554 	bp = cttab[unit].b_actf;
    555 	hpibgo(sc->sc_hd->hp_ctlr, sc->sc_hd->hp_slave, C_EXEC,
    556 		sc->sc_addr, sc->sc_resid, bp->b_flags & B_READ);
    557 }
    558 
    559 /*
    560  * Hideous grue to handle EOF/EOT (mostly for reads)
    561  */
    562 cteof(sc, bp)
    563 	register struct ct_softc *sc;
    564 	register struct buf *bp;
    565 {
    566 	long blks;
    567 
    568 	/*
    569 	 * EOT on a write is an error.
    570 	 */
    571 	if ((bp->b_flags & B_READ) == 0) {
    572 		bp->b_resid = bp->b_bcount;
    573 		bp->b_flags |= B_ERROR;
    574 		bp->b_error = ENOSPC;
    575 		sc->sc_flags |= CTF_EOT;
    576 		return;
    577 	}
    578 	/*
    579 	 * Use returned block position to determine how many blocks
    580 	 * we really read and update b_resid.
    581 	 */
    582 	blks = sc->sc_stat.c_blk - sc->sc_blkno - 1;
    583 #ifdef DEBUG
    584 	if (ctdebug & CDB_FILES)
    585 		printf("cteof: bc %d oblk %d nblk %d read %d, resid %d\n",
    586 		       bp->b_bcount, sc->sc_blkno, sc->sc_stat.c_blk,
    587 		       blks, bp->b_bcount - CTKTOB(blks));
    588 #endif
    589 	if (blks == -1) { /* 9145 on EOF does not change sc_stat.c_blk */
    590 		blks = 0;
    591 		sc->sc_blkno++;
    592 	}
    593 	else {
    594 		sc->sc_blkno = sc->sc_stat.c_blk;
    595 	}
    596 	bp->b_resid = bp->b_bcount - CTKTOB(blks);
    597 	/*
    598 	 * If we are at physical EOV or were after an EOF,
    599 	 * we are now at logical EOT.
    600 	 */
    601 	if ((sc->sc_stat.c_aef & AEF_EOV) ||
    602 	    (sc->sc_flags & CTF_AEOF)) {
    603 		sc->sc_flags |= CTF_EOT;
    604 		sc->sc_flags &= ~(CTF_AEOF|CTF_BEOF);
    605 	}
    606 	/*
    607 	 * If we were before an EOF or we have just completed a FSF,
    608 	 * we are now after EOF.
    609 	 */
    610 	else if ((sc->sc_flags & CTF_BEOF) ||
    611 		 (sc->sc_flags & CTF_CMD) && sc->sc_cmd == MTFSF) {
    612 		sc->sc_flags |= CTF_AEOF;
    613 		sc->sc_flags &= ~CTF_BEOF;
    614 	}
    615 	/*
    616 	 * Otherwise if we read something we are now before EOF
    617 	 * (and no longer after EOF).
    618 	 */
    619 	else if (blks) {
    620 		sc->sc_flags |= CTF_BEOF;
    621 		sc->sc_flags &= ~CTF_AEOF;
    622 	}
    623 	/*
    624 	 * Finally, if we didn't read anything we just passed an EOF
    625 	 */
    626 	else
    627 		sc->sc_flags |= CTF_AEOF;
    628 #ifdef DEBUG
    629 	if (ctdebug & CDB_FILES)
    630 		printf("cteof: leaving flags %x\n", sc->sc_flags);
    631 #endif
    632 }
    633 
    634 ctintr(unit)
    635 	register int unit;
    636 {
    637 	register struct ct_softc *sc = &ct_softc[unit];
    638 	register struct buf *bp, *dp;
    639 	u_char stat;
    640 
    641 	bp = cttab[unit].b_actf;
    642 	if (bp == NULL) {
    643 		printf("ct%d: bp == NULL\n", unit);
    644 		return;
    645 	}
    646 	if (sc->sc_flags & CTF_IO) {
    647 		sc->sc_flags &= ~CTF_IO;
    648 		if (hpibustart(sc->sc_hd->hp_ctlr))
    649 			ctgo(unit);
    650 		return;
    651 	}
    652 	if ((sc->sc_flags & CTF_STATWAIT) == 0) {
    653 		if (hpibpptest(sc->sc_hd->hp_ctlr, sc->sc_hd->hp_slave) == 0) {
    654 			sc->sc_flags |= CTF_STATWAIT;
    655 			hpibawait(sc->sc_hd->hp_ctlr);
    656 			return;
    657 		}
    658 	} else
    659 		sc->sc_flags &= ~CTF_STATWAIT;
    660 	hpibrecv(sc->sc_hd->hp_ctlr, sc->sc_hd->hp_slave, C_QSTAT, &stat, 1);
    661 #ifdef DEBUG
    662 	if (ctdebug & CDB_FILES)
    663 		printf("ctintr: before flags %x\n", sc->sc_flags);
    664 #endif
    665 	if (stat) {
    666 		sc->sc_rsc.unit = C_SUNIT(sc->sc_punit);
    667 		sc->sc_rsc.cmd = C_STATUS;
    668 		hpibsend(sc->sc_hd->hp_ctlr, sc->sc_hd->hp_slave, C_CMD,
    669 			&sc->sc_rsc, sizeof(sc->sc_rsc));
    670 		hpibrecv(sc->sc_hd->hp_ctlr, sc->sc_hd->hp_slave, C_EXEC,
    671 			&sc->sc_stat, sizeof(sc->sc_stat));
    672 		hpibrecv(sc->sc_hd->hp_ctlr, sc->sc_hd->hp_slave, C_QSTAT,
    673 			&stat, 1);
    674 #ifdef DEBUG
    675 		if (ctdebug & CDB_FILES)
    676 			printf("ctintr: return stat 0x%x, A%x F%x blk %d\n",
    677 			       stat, sc->sc_stat.c_aef,
    678 			       sc->sc_stat.c_fef, sc->sc_stat.c_blk);
    679 #endif
    680 		if (stat == 0) {
    681 			if (sc->sc_stat.c_aef & (AEF_EOF | AEF_EOV)) {
    682 				cteof(sc, bp);
    683 				ctaddeof(unit);
    684 				goto done;
    685 			}
    686 			if (sc->sc_stat.c_fef & FEF_PF) {
    687 				ctreset(sc, sc->sc_hd);
    688 				ctstart(unit);
    689 				return;
    690 			}
    691 			if (sc->sc_stat.c_fef & FEF_REXMT) {
    692 				ctstart(unit);
    693 				return;
    694 			}
    695 			if (sc->sc_stat.c_aef & 0x5800) {
    696 				if (sc->sc_stat.c_aef & 0x4000)
    697 					tprintf(sc->sc_tpr,
    698 						"ct%d: uninitialized media\n",
    699 						unit);
    700 				if (sc->sc_stat.c_aef & 0x1000)
    701 					tprintf(sc->sc_tpr,
    702 						"ct%d: not ready\n", unit);
    703 				if (sc->sc_stat.c_aef & 0x0800)
    704 					tprintf(sc->sc_tpr,
    705 						"ct%d: write protect\n", unit);
    706 			} else {
    707 				printf("ct%d err: v%d u%d ru%d bn%d, ",
    708 				       unit,
    709 				       (sc->sc_stat.c_vu>>4)&0xF,
    710 				       sc->sc_stat.c_vu&0xF,
    711 				       sc->sc_stat.c_pend,
    712 				       sc->sc_stat.c_blk);
    713 				printf("R0x%x F0x%x A0x%x I0x%x\n",
    714 				       sc->sc_stat.c_ref,
    715 				       sc->sc_stat.c_fef,
    716 				       sc->sc_stat.c_aef,
    717 				       sc->sc_stat.c_ief);
    718 			}
    719 		} else
    720 			printf("ct%d: request status failed\n", unit);
    721 		bp->b_flags |= B_ERROR;
    722 		bp->b_error = EIO;
    723 		goto done;
    724 	} else
    725 		bp->b_resid = 0;
    726 	if (sc->sc_flags & CTF_CMD) {
    727 		switch (sc->sc_cmd) {
    728 		case MTFSF:
    729 			sc->sc_flags &= ~(CTF_BEOF|CTF_AEOF);
    730 			sc->sc_blkno += CTBTOK(sc->sc_resid);
    731 			ctstart(unit);
    732 			return;
    733 		case MTBSF:
    734 			sc->sc_flags &= ~(CTF_AEOF|CTF_BEOF|CTF_EOT);
    735 			break;
    736 		case MTBSR:
    737 			sc->sc_flags &= ~CTF_BEOF;
    738 			if (sc->sc_flags & CTF_EOT) {
    739 				sc->sc_flags |= CTF_AEOF;
    740 				sc->sc_flags &= ~CTF_EOT;
    741 			} else if (sc->sc_flags & CTF_AEOF) {
    742 				sc->sc_flags |= CTF_BEOF;
    743 				sc->sc_flags &= ~CTF_AEOF;
    744 			}
    745 			break;
    746 		case MTWEOF:
    747 			sc->sc_flags &= ~CTF_BEOF;
    748 			if (sc->sc_flags & (CTF_AEOF|CTF_EOT)) {
    749 				sc->sc_flags |= CTF_EOT;
    750 				sc->sc_flags &= ~CTF_AEOF;
    751 			} else
    752 				sc->sc_flags |= CTF_AEOF;
    753 			break;
    754 		case MTREW:
    755 		case MTOFFL:
    756 			sc->sc_flags &= ~(CTF_BEOF|CTF_AEOF|CTF_EOT);
    757 			break;
    758 		}
    759 	} else {
    760 		sc->sc_flags &= ~CTF_AEOF;
    761 		sc->sc_blkno += CTBTOK(sc->sc_resid);
    762 	}
    763 done:
    764 #ifdef DEBUG
    765 	if (ctdebug & CDB_FILES)
    766 		printf("ctintr: after flags %x\n", sc->sc_flags);
    767 #endif
    768 	if (dp = bp->b_actf)
    769 		dp->b_actb = bp->b_actb;
    770 	else
    771 		cttab[unit].b_actb = bp->b_actb;
    772 	*bp->b_actb = dp;
    773 	iodone(bp);
    774 	hpibfree(&sc->sc_dq);
    775 	if (cttab[unit].b_actf == NULL) {
    776 		cttab[unit].b_active = 0;
    777 		return;
    778 	}
    779 	ctustart(unit);
    780 }
    781 
    782 /*ARGSUSED*/
    783 ctioctl(dev, cmd, data, flag, p)
    784 	dev_t dev;
    785 	int cmd, flag;
    786 	caddr_t data;
    787 	struct proc *p;
    788 {
    789 	register struct mtop *op;
    790 	register int cnt;
    791 
    792 	switch (cmd) {
    793 
    794 	case MTIOCTOP:
    795 		op = (struct mtop *)data;
    796 		switch(op->mt_op) {
    797 
    798 		case MTWEOF:
    799 		case MTFSF:
    800 		case MTBSR:
    801 		case MTBSF:
    802 		case MTFSR:
    803 			cnt = op->mt_count;
    804 			break;
    805 
    806 		case MTREW:
    807 		case MTOFFL:
    808 			cnt = 1;
    809 			break;
    810 
    811 		default:
    812 			return(EINVAL);
    813 		}
    814 		ctcommand(dev, op->mt_op, cnt);
    815 		break;
    816 
    817 	case MTIOCGET:
    818 		break;
    819 
    820 	default:
    821 		return(EINVAL);
    822 	}
    823 	return(0);
    824 }
    825 
    826 /*ARGSUSED*/
    827 ctdump(dev)
    828 	dev_t dev;
    829 {
    830 	return(ENXIO);
    831 }
    832 
    833 ctaddeof(unit)
    834 	int unit;
    835 {
    836 	register struct ct_softc *sc = &ct_softc[unit];
    837 
    838 	if (sc->sc_eofp == EOFS - 1)
    839 		sc->sc_eofs[EOFS - 1]++;
    840 	else {
    841 		sc->sc_eofp++;
    842 		if (sc->sc_eofp == EOFS - 1)
    843 			sc->sc_eofs[EOFS - 1] = EOFS;
    844 		else
    845 			/* save blkno */
    846 			sc->sc_eofs[sc->sc_eofp] = sc->sc_blkno - 1;
    847 	}
    848 #ifdef DEBUG
    849 	if (ctdebug & CT_BSF)
    850 		printf("ct%d: add eof pos %d blk %d\n",
    851 		       unit, sc->sc_eofp,
    852 		       sc->sc_eofs[sc->sc_eofp]);
    853 #endif
    854 }
    855 #endif
    856