Home | History | Annotate | Line # | Download | only in dev
ct.c revision 1.11
      1 /*	$NetBSD: ct.c,v 1.11 1995/08/04 08:13:00 thorpej 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(), ctdone();
     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 			ctdone(unit, bp);
    511 			return;
    512 		}
    513 		sc->sc_flags |= CTF_IO;
    514 		sc->sc_ioc.unit = C_SUNIT(sc->sc_punit);
    515 		sc->sc_ioc.saddr = C_SADDR;
    516 		sc->sc_ioc.addr0 = 0;
    517 		sc->sc_ioc.addr = sc->sc_blkno;
    518 		sc->sc_ioc.nop2 = C_NOP;
    519 		sc->sc_ioc.slen = C_SLEN;
    520 		sc->sc_ioc.len = sc->sc_resid;
    521 		sc->sc_ioc.nop3 = C_NOP;
    522 		if (bp->b_flags & B_READ)
    523 			sc->sc_ioc.cmd = C_READ;
    524 		else {
    525 			sc->sc_ioc.cmd = C_WRITE;
    526 			sc->sc_flags |= (CTF_WRT | CTF_WRTTN);
    527 		}
    528 		hpibsend(sc->sc_hd->hp_ctlr, sc->sc_hd->hp_slave, C_CMD,
    529 			&sc->sc_ioc, sizeof(sc->sc_ioc));
    530 	}
    531 	hpibawait(sc->sc_hd->hp_ctlr);
    532 }
    533 
    534 ctgo(unit)
    535 	register int unit;
    536 {
    537 	register struct ct_softc *sc = &ct_softc[unit];
    538 	register struct buf *bp;
    539 
    540 	bp = cttab[unit].b_actf;
    541 	hpibgo(sc->sc_hd->hp_ctlr, sc->sc_hd->hp_slave, C_EXEC,
    542 		sc->sc_addr, sc->sc_resid, bp->b_flags & B_READ);
    543 }
    544 
    545 /*
    546  * Hideous grue to handle EOF/EOT (mostly for reads)
    547  */
    548 cteof(sc, bp)
    549 	register struct ct_softc *sc;
    550 	register struct buf *bp;
    551 {
    552 	long blks;
    553 
    554 	/*
    555 	 * EOT on a write is an error.
    556 	 */
    557 	if ((bp->b_flags & B_READ) == 0) {
    558 		bp->b_resid = bp->b_bcount;
    559 		bp->b_flags |= B_ERROR;
    560 		bp->b_error = ENOSPC;
    561 		sc->sc_flags |= CTF_EOT;
    562 		return;
    563 	}
    564 	/*
    565 	 * Use returned block position to determine how many blocks
    566 	 * we really read and update b_resid.
    567 	 */
    568 	blks = sc->sc_stat.c_blk - sc->sc_blkno - 1;
    569 #ifdef DEBUG
    570 	if (ctdebug & CDB_FILES)
    571 		printf("cteof: bc %d oblk %d nblk %d read %d, resid %d\n",
    572 		       bp->b_bcount, sc->sc_blkno, sc->sc_stat.c_blk,
    573 		       blks, bp->b_bcount - CTKTOB(blks));
    574 #endif
    575 	if (blks == -1) { /* 9145 on EOF does not change sc_stat.c_blk */
    576 		blks = 0;
    577 		sc->sc_blkno++;
    578 	}
    579 	else {
    580 		sc->sc_blkno = sc->sc_stat.c_blk;
    581 	}
    582 	bp->b_resid = bp->b_bcount - CTKTOB(blks);
    583 	/*
    584 	 * If we are at physical EOV or were after an EOF,
    585 	 * we are now at logical EOT.
    586 	 */
    587 	if ((sc->sc_stat.c_aef & AEF_EOV) ||
    588 	    (sc->sc_flags & CTF_AEOF)) {
    589 		sc->sc_flags |= CTF_EOT;
    590 		sc->sc_flags &= ~(CTF_AEOF|CTF_BEOF);
    591 	}
    592 	/*
    593 	 * If we were before an EOF or we have just completed a FSF,
    594 	 * we are now after EOF.
    595 	 */
    596 	else if ((sc->sc_flags & CTF_BEOF) ||
    597 		 (sc->sc_flags & CTF_CMD) && sc->sc_cmd == MTFSF) {
    598 		sc->sc_flags |= CTF_AEOF;
    599 		sc->sc_flags &= ~CTF_BEOF;
    600 	}
    601 	/*
    602 	 * Otherwise if we read something we are now before EOF
    603 	 * (and no longer after EOF).
    604 	 */
    605 	else if (blks) {
    606 		sc->sc_flags |= CTF_BEOF;
    607 		sc->sc_flags &= ~CTF_AEOF;
    608 	}
    609 	/*
    610 	 * Finally, if we didn't read anything we just passed an EOF
    611 	 */
    612 	else
    613 		sc->sc_flags |= CTF_AEOF;
    614 #ifdef DEBUG
    615 	if (ctdebug & CDB_FILES)
    616 		printf("cteof: leaving flags %x\n", sc->sc_flags);
    617 #endif
    618 }
    619 
    620 ctintr(unit)
    621 	register int unit;
    622 {
    623 	register struct ct_softc *sc = &ct_softc[unit];
    624 	register struct buf *bp, *dp;
    625 	u_char stat;
    626 
    627 	bp = cttab[unit].b_actf;
    628 	if (bp == NULL) {
    629 		printf("ct%d: bp == NULL\n", unit);
    630 		return;
    631 	}
    632 	if (sc->sc_flags & CTF_IO) {
    633 		sc->sc_flags &= ~CTF_IO;
    634 		if (hpibustart(sc->sc_hd->hp_ctlr))
    635 			ctgo(unit);
    636 		return;
    637 	}
    638 	if ((sc->sc_flags & CTF_STATWAIT) == 0) {
    639 		if (hpibpptest(sc->sc_hd->hp_ctlr, sc->sc_hd->hp_slave) == 0) {
    640 			sc->sc_flags |= CTF_STATWAIT;
    641 			hpibawait(sc->sc_hd->hp_ctlr);
    642 			return;
    643 		}
    644 	} else
    645 		sc->sc_flags &= ~CTF_STATWAIT;
    646 	hpibrecv(sc->sc_hd->hp_ctlr, sc->sc_hd->hp_slave, C_QSTAT, &stat, 1);
    647 #ifdef DEBUG
    648 	if (ctdebug & CDB_FILES)
    649 		printf("ctintr: before flags %x\n", sc->sc_flags);
    650 #endif
    651 	if (stat) {
    652 		sc->sc_rsc.unit = C_SUNIT(sc->sc_punit);
    653 		sc->sc_rsc.cmd = C_STATUS;
    654 		hpibsend(sc->sc_hd->hp_ctlr, sc->sc_hd->hp_slave, C_CMD,
    655 			&sc->sc_rsc, sizeof(sc->sc_rsc));
    656 		hpibrecv(sc->sc_hd->hp_ctlr, sc->sc_hd->hp_slave, C_EXEC,
    657 			&sc->sc_stat, sizeof(sc->sc_stat));
    658 		hpibrecv(sc->sc_hd->hp_ctlr, sc->sc_hd->hp_slave, C_QSTAT,
    659 			&stat, 1);
    660 #ifdef DEBUG
    661 		if (ctdebug & CDB_FILES)
    662 			printf("ctintr: return stat 0x%x, A%x F%x blk %d\n",
    663 			       stat, sc->sc_stat.c_aef,
    664 			       sc->sc_stat.c_fef, sc->sc_stat.c_blk);
    665 #endif
    666 		if (stat == 0) {
    667 			if (sc->sc_stat.c_aef & (AEF_EOF | AEF_EOV)) {
    668 				cteof(sc, bp);
    669 				ctaddeof(unit);
    670 				goto done;
    671 			}
    672 			if (sc->sc_stat.c_fef & FEF_PF) {
    673 				ctreset(sc, sc->sc_hd);
    674 				ctstart(unit);
    675 				return;
    676 			}
    677 			if (sc->sc_stat.c_fef & FEF_REXMT) {
    678 				ctstart(unit);
    679 				return;
    680 			}
    681 			if (sc->sc_stat.c_aef & 0x5800) {
    682 				if (sc->sc_stat.c_aef & 0x4000)
    683 					tprintf(sc->sc_tpr,
    684 						"ct%d: uninitialized media\n",
    685 						unit);
    686 				if (sc->sc_stat.c_aef & 0x1000)
    687 					tprintf(sc->sc_tpr,
    688 						"ct%d: not ready\n", unit);
    689 				if (sc->sc_stat.c_aef & 0x0800)
    690 					tprintf(sc->sc_tpr,
    691 						"ct%d: write protect\n", unit);
    692 			} else {
    693 				printf("ct%d err: v%d u%d ru%d bn%d, ",
    694 				       unit,
    695 				       (sc->sc_stat.c_vu>>4)&0xF,
    696 				       sc->sc_stat.c_vu&0xF,
    697 				       sc->sc_stat.c_pend,
    698 				       sc->sc_stat.c_blk);
    699 				printf("R0x%x F0x%x A0x%x I0x%x\n",
    700 				       sc->sc_stat.c_ref,
    701 				       sc->sc_stat.c_fef,
    702 				       sc->sc_stat.c_aef,
    703 				       sc->sc_stat.c_ief);
    704 			}
    705 		} else
    706 			printf("ct%d: request status failed\n", unit);
    707 		bp->b_flags |= B_ERROR;
    708 		bp->b_error = EIO;
    709 		goto done;
    710 	} else
    711 		bp->b_resid = 0;
    712 	if (sc->sc_flags & CTF_CMD) {
    713 		switch (sc->sc_cmd) {
    714 		case MTFSF:
    715 			sc->sc_flags &= ~(CTF_BEOF|CTF_AEOF);
    716 			sc->sc_blkno += CTBTOK(sc->sc_resid);
    717 			ctstart(unit);
    718 			return;
    719 		case MTBSF:
    720 			sc->sc_flags &= ~(CTF_AEOF|CTF_BEOF|CTF_EOT);
    721 			break;
    722 		case MTBSR:
    723 			sc->sc_flags &= ~CTF_BEOF;
    724 			if (sc->sc_flags & CTF_EOT) {
    725 				sc->sc_flags |= CTF_AEOF;
    726 				sc->sc_flags &= ~CTF_EOT;
    727 			} else if (sc->sc_flags & CTF_AEOF) {
    728 				sc->sc_flags |= CTF_BEOF;
    729 				sc->sc_flags &= ~CTF_AEOF;
    730 			}
    731 			break;
    732 		case MTWEOF:
    733 			sc->sc_flags &= ~CTF_BEOF;
    734 			if (sc->sc_flags & (CTF_AEOF|CTF_EOT)) {
    735 				sc->sc_flags |= CTF_EOT;
    736 				sc->sc_flags &= ~CTF_AEOF;
    737 			} else
    738 				sc->sc_flags |= CTF_AEOF;
    739 			break;
    740 		case MTREW:
    741 		case MTOFFL:
    742 			sc->sc_flags &= ~(CTF_BEOF|CTF_AEOF|CTF_EOT);
    743 			break;
    744 		}
    745 	} else {
    746 		sc->sc_flags &= ~CTF_AEOF;
    747 		sc->sc_blkno += CTBTOK(sc->sc_resid);
    748 	}
    749 done:
    750 #ifdef DEBUG
    751 	if (ctdebug & CDB_FILES)
    752 		printf("ctintr: after flags %x\n", sc->sc_flags);
    753 #endif
    754 	ctdone(unit, bp);
    755 }
    756 
    757 void
    758 ctdone(unit, bp)
    759 	int unit;
    760 	register struct buf *bp;
    761 {
    762 	register struct ct_softc *sc = &ct_softc[unit];
    763 	register struct buf *dp;
    764 
    765 	if (dp = bp->b_actf)
    766 		dp->b_actb = bp->b_actb;
    767 	else
    768 		cttab[unit].b_actb = bp->b_actb;
    769 	*bp->b_actb = dp;
    770 	biodone(bp);
    771 	hpibfree(&sc->sc_dq);
    772 	if (cttab[unit].b_actf == NULL) {
    773 		cttab[unit].b_active = 0;
    774 		return;
    775 	}
    776 	ctustart(unit);
    777 }
    778 
    779 int
    780 ctread(dev, uio, flags)
    781 	dev_t dev;
    782 	struct uio *uio;
    783 	int flags;
    784 {
    785 
    786 	return (physio(ctstrategy, NULL, dev, B_READ, minphys, uio));
    787 }
    788 
    789 int
    790 ctwrite(dev, uio, flags)
    791 	dev_t dev;
    792 	struct uio *uio;
    793 	int flags;
    794 {
    795 
    796 	/* XXX: check for hardware write-protect? */
    797 	return (physio(ctstrategy, NULL, dev, B_WRITE, minphys, uio));
    798 }
    799 
    800 /*ARGSUSED*/
    801 ctioctl(dev, cmd, data, flag, p)
    802 	dev_t dev;
    803 	int cmd, flag;
    804 	caddr_t data;
    805 	struct proc *p;
    806 {
    807 	register struct mtop *op;
    808 	register int cnt;
    809 
    810 	switch (cmd) {
    811 
    812 	case MTIOCTOP:
    813 		op = (struct mtop *)data;
    814 		switch(op->mt_op) {
    815 
    816 		case MTWEOF:
    817 		case MTFSF:
    818 		case MTBSR:
    819 		case MTBSF:
    820 		case MTFSR:
    821 			cnt = op->mt_count;
    822 			break;
    823 
    824 		case MTREW:
    825 		case MTOFFL:
    826 			cnt = 1;
    827 			break;
    828 
    829 		default:
    830 			return(EINVAL);
    831 		}
    832 		ctcommand(dev, op->mt_op, cnt);
    833 		break;
    834 
    835 	case MTIOCGET:
    836 		break;
    837 
    838 	default:
    839 		return(EINVAL);
    840 	}
    841 	return(0);
    842 }
    843 
    844 /*ARGSUSED*/
    845 ctdump(dev)
    846 	dev_t dev;
    847 {
    848 	return(ENXIO);
    849 }
    850 
    851 ctaddeof(unit)
    852 	int unit;
    853 {
    854 	register struct ct_softc *sc = &ct_softc[unit];
    855 
    856 	if (sc->sc_eofp == EOFS - 1)
    857 		sc->sc_eofs[EOFS - 1]++;
    858 	else {
    859 		sc->sc_eofp++;
    860 		if (sc->sc_eofp == EOFS - 1)
    861 			sc->sc_eofs[EOFS - 1] = EOFS;
    862 		else
    863 			/* save blkno */
    864 			sc->sc_eofs[sc->sc_eofp] = sc->sc_blkno - 1;
    865 	}
    866 #ifdef DEBUG
    867 	if (ctdebug & CT_BSF)
    868 		printf("ct%d: add eof pos %d blk %d\n",
    869 		       unit, sc->sc_eofp,
    870 		       sc->sc_eofs[sc->sc_eofp]);
    871 #endif
    872 }
    873 #endif
    874