Home | History | Annotate | Line # | Download | only in dev
ct.c revision 1.14
      1 /*	$NetBSD: ct.c,v 1.14 1996/01/23 00:28:09 scottr 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	ctmatch(), ctstart(), ctgo(), ctintr();
     68 void	ctattach(), ctstrategy(), ctdone();
     69 struct	driver ctdriver = {
     70 	ctmatch, ctattach, "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 int
    139 ctmatch(hd)
    140 	register struct hp_device *hd;
    141 {
    142 	register struct ct_softc *sc = &ct_softc[hd->hp_unit];
    143 	register struct buf *bp;
    144 
    145 	for (bp = cttab; bp < &cttab[NCT]; bp++)
    146 		bp->b_actb = &bp->b_actf;
    147 	sc->sc_hd = hd;
    148 	sc->sc_punit = ctpunit(hd->hp_flags);
    149 	if (ctident(sc, hd, 0) < 0)
    150 		return (0);
    151 
    152 	return (1);
    153 }
    154 
    155 void
    156 ctattach(hd)
    157 	register struct hp_device *hd;
    158 {
    159 	struct ct_softc *sc = &ct_softc[hd->hp_unit];
    160 
    161 	(void)ctident(sc, hd, 1);	/* XXX Ick. */
    162 
    163 	ctreset(sc, hd);
    164 	sc->sc_dq.dq_ctlr = hd->hp_ctlr;
    165 	sc->sc_dq.dq_unit = hd->hp_unit;
    166 	sc->sc_dq.dq_slave = hd->hp_slave;
    167 	sc->sc_dq.dq_driver = &ctdriver;
    168 	sc->sc_flags |= CTF_ALIVE;
    169 }
    170 
    171 int
    172 ctident(sc, hd, verbose)
    173 	register struct ct_softc *sc;
    174 	register struct hp_device *hd;
    175 	int verbose;
    176 {
    177 	struct ct_describe desc;
    178 	u_char stat, cmd[3];
    179 	char name[7];
    180 	int id, i;
    181 
    182 	/*
    183 	 * Read device id and verify that:
    184 	 * 1. It is a CS80 device
    185 	 * 2. It is one of our recognized tape devices
    186 	 * 3. It has the proper physical unit number
    187 	 */
    188 	id = hpibid(hd->hp_ctlr, hd->hp_slave);
    189 	if ((id & 0x200) == 0)
    190 		return(-1);
    191 	for (i = 0; i < nctinfo; i++)
    192 		if (id == ctinfo[i].hwid)
    193 			break;
    194 	if (i == nctinfo || sc->sc_punit != ctinfo[i].punit)
    195 		return(-1);
    196 	id = i;
    197 
    198 	/*
    199 	 * Collect device description.
    200 	 * Right now we only need this to differentiate 7945 from 7946.
    201 	 * Note that we always issue the describe command to unit 0.
    202 	 */
    203 	cmd[0] = C_SUNIT(0);
    204 	cmd[1] = C_SVOL(0);
    205 	cmd[2] = C_DESC;
    206 	hpibsend(hd->hp_ctlr, hd->hp_slave, C_CMD, cmd, sizeof(cmd));
    207 	hpibrecv(hd->hp_ctlr, hd->hp_slave, C_EXEC, &desc, 37);
    208 	hpibrecv(hd->hp_ctlr, hd->hp_slave, C_QSTAT, &stat, sizeof(stat));
    209 	bzero(name, sizeof(name));
    210 	if (!stat) {
    211 		register int n = desc.d_name;
    212 		for (i = 5; i >= 0; i--) {
    213 			name[i] = (n & 0xf) + '0';
    214 			n >>= 4;
    215 		}
    216 	}
    217 	switch (ctinfo[id].hwid) {
    218 	case CT7946ID:
    219 		if (bcmp(name, "079450", 6) == 0)
    220 			return(-1);		/* not really a 7946 */
    221 		/* fall into... */
    222 	case CT9144ID:
    223 	case CT9145ID:
    224 		sc->sc_type = CT9144;
    225 		sc->sc_flags |= CTF_CANSTREAM;
    226 		break;
    227 
    228 	case CT7912PID:
    229 	case CT7914PID:
    230 		sc->sc_type = CT88140;
    231 		break;
    232 	}
    233 	if (verbose)
    234 		printf(": %s %stape\n", ctinfo[id].desc,
    235 		    (sc->sc_flags & CTF_CANSTREAM) ? "streaming " : " ");
    236 	return(id);
    237 }
    238 
    239 ctreset(sc, hd)
    240 	register struct ct_softc *sc;
    241 	register struct hp_device *hd;
    242 {
    243 	u_char stat;
    244 
    245 	sc->sc_clear.unit = C_SUNIT(sc->sc_punit);
    246 	sc->sc_clear.cmd = C_CLEAR;
    247 	hpibsend(hd->hp_ctlr, hd->hp_slave, C_TCMD, &sc->sc_clear,
    248 		sizeof(sc->sc_clear));
    249 	hpibswait(hd->hp_ctlr, hd->hp_slave);
    250 	hpibrecv(hd->hp_ctlr, hd->hp_slave, C_QSTAT, &stat, sizeof(stat));
    251 	sc->sc_src.unit = C_SUNIT(CTCTLR);
    252 	sc->sc_src.nop = C_NOP;
    253 	sc->sc_src.cmd = C_SREL;
    254 	sc->sc_src.param = C_REL;
    255 	hpibsend(hd->hp_ctlr, hd->hp_slave, C_CMD, &sc->sc_src,
    256 		sizeof(sc->sc_src));
    257 	hpibswait(hd->hp_ctlr, hd->hp_slave);
    258 	hpibrecv(hd->hp_ctlr, hd->hp_slave, C_QSTAT, &stat, sizeof(stat));
    259 	sc->sc_ssmc.unit = C_SUNIT(sc->sc_punit);
    260 	sc->sc_ssmc.cmd = C_SSM;
    261 	sc->sc_ssmc.refm = REF_MASK;
    262 	sc->sc_ssmc.fefm = FEF_MASK;
    263 	sc->sc_ssmc.aefm = AEF_MASK;
    264 	sc->sc_ssmc.iefm = IEF_MASK;
    265 	hpibsend(hd->hp_ctlr, hd->hp_slave, C_CMD, &sc->sc_ssmc,
    266 		sizeof(sc->sc_ssmc));
    267 	hpibswait(hd->hp_ctlr, hd->hp_slave);
    268 	hpibrecv(hd->hp_ctlr, hd->hp_slave, C_QSTAT, &stat, sizeof(stat));
    269 	sc->sc_soptc.unit = C_SUNIT(sc->sc_punit);
    270 	sc->sc_soptc.nop = C_NOP;
    271 	sc->sc_soptc.cmd = C_SOPT;
    272 	sc->sc_soptc.opt = C_SPAR;
    273 	hpibsend(hd->hp_ctlr, hd->hp_slave, C_CMD, &sc->sc_soptc,
    274 		sizeof(sc->sc_soptc));
    275 	hpibswait(hd->hp_ctlr, hd->hp_slave);
    276 	hpibrecv(hd->hp_ctlr, hd->hp_slave, C_QSTAT, &stat, sizeof(stat));
    277 }
    278 
    279 /*ARGSUSED*/
    280 ctopen(dev, flag, type, p)
    281 	dev_t dev;
    282 	int flag, type;
    283 	struct proc *p;
    284 {
    285 	register struct ct_softc *sc = &ct_softc[UNIT(dev)];
    286 	u_char stat;
    287 	int cc;
    288 
    289 	if (UNIT(dev) >= NCT || (sc->sc_flags & CTF_ALIVE) == 0)
    290 		return(ENXIO);
    291 	if (sc->sc_flags & CTF_OPEN)
    292 		return(EBUSY);
    293 	sc->sc_soptc.unit = C_SUNIT(sc->sc_punit);
    294 	sc->sc_soptc.nop = C_NOP;
    295 	sc->sc_soptc.cmd = C_SOPT;
    296 	if ((dev & CT_STREAM) && (sc->sc_flags & CTF_CANSTREAM))
    297 		sc->sc_soptc.opt = C_SPAR | C_IMRPT;
    298 	else
    299 		sc->sc_soptc.opt = C_SPAR;
    300 	/*
    301 	 * Check the return of hpibsend() and hpibswait().
    302 	 * Drive could be loading/unloading a tape. If not checked,
    303 	 * driver hangs.
    304 	 */
    305 	cc = hpibsend(sc->sc_hd->hp_ctlr, sc->sc_hd->hp_slave,
    306 	              C_CMD, &sc->sc_soptc, sizeof(sc->sc_soptc));
    307 	if (cc != sizeof(sc->sc_soptc))
    308 		return(EBUSY);
    309 	hpibswait(sc->sc_hd->hp_ctlr, sc->sc_hd->hp_slave);
    310 	cc = hpibrecv(sc->sc_hd->hp_ctlr, sc->sc_hd->hp_slave, C_QSTAT,
    311 	              &stat, sizeof(stat));
    312 	if (cc != sizeof(stat))
    313 		return(EBUSY);
    314 	sc->sc_tpr = tprintf_open(p);
    315 	sc->sc_flags |= CTF_OPEN;
    316 	return(0);
    317 }
    318 
    319 /*ARGSUSED*/
    320 ctclose(dev, flag)
    321 	dev_t dev;
    322 	int flag;
    323 {
    324 	register struct ct_softc *sc = &ct_softc[UNIT(dev)];
    325 
    326 	if ((sc->sc_flags & (CTF_WRT|CTF_WRTTN)) == (CTF_WRT|CTF_WRTTN) &&
    327 	    (sc->sc_flags & CTF_EOT) == 0 ) { /* XXX return error if EOT ?? */
    328 		ctcommand(dev, MTWEOF, 2);
    329 		ctcommand(dev, MTBSR, 1);
    330 		if (sc->sc_eofp == EOFS - 1)
    331 			sc->sc_eofs[EOFS - 1]--;
    332 		else
    333 			sc->sc_eofp--;
    334 #ifdef DEBUG
    335 		if(ctdebug & CT_BSF)
    336 			printf("%s: ctclose backup eofs prt %d blk %d\n",
    337 			       sc->sc_hd->hp_xname, sc->sc_eofp,
    338 			       sc->sc_eofs[sc->sc_eofp]);
    339 #endif
    340 	}
    341 	if ((minor(dev) & CT_NOREW) == 0)
    342 		ctcommand(dev, MTREW, 1);
    343 	sc->sc_flags &= ~(CTF_OPEN | CTF_WRT | CTF_WRTTN);
    344 	tprintf_close(sc->sc_tpr);
    345 #ifdef DEBUG
    346 	if (ctdebug & CDB_FILES)
    347 		printf("ctclose: flags %x\n", sc->sc_flags);
    348 #endif
    349 	return(0);	/* XXX */
    350 }
    351 
    352 ctcommand(dev, cmd, cnt)
    353 	dev_t dev;
    354 	int cmd;
    355 	register int cnt;
    356 {
    357 	register struct ct_softc *sc = &ct_softc[UNIT(dev)];
    358 	register struct buf *bp = &ctbuf[UNIT(dev)];
    359 	register struct buf *nbp = 0;
    360 
    361 	if (cmd == MTBSF && sc->sc_eofp == EOFS - 1) {
    362 		cnt = sc->sc_eofs[EOFS - 1] - cnt;
    363 		ctcommand(dev, MTREW, 1);
    364 		ctcommand(dev, MTFSF, cnt);
    365 		cnt = 2;
    366 		cmd = MTBSR;
    367 	}
    368 
    369 	if (cmd == MTBSF && sc->sc_eofp - cnt < 0) {
    370 		cnt = 1;
    371 		cmd = MTREW;
    372 	}
    373 
    374 	sc->sc_flags |= CTF_CMD;
    375 	sc->sc_bp = bp;
    376 	sc->sc_cmd = cmd;
    377 	bp->b_dev = dev;
    378 	if (cmd == MTFSF) {
    379 		nbp = (struct buf *)geteblk(MAXBSIZE);
    380 		bp->b_un.b_addr = nbp->b_un.b_addr;
    381 		bp->b_bcount = MAXBSIZE;
    382 	}
    383 
    384 	while (cnt-- > 0) {
    385 		bp->b_flags = B_BUSY;
    386 		if (cmd == MTBSF) {
    387 			sc->sc_blkno = sc->sc_eofs[sc->sc_eofp];
    388 			sc->sc_eofp--;
    389 #ifdef DEBUG
    390 			if (ctdebug & CT_BSF)
    391 				printf("%s: backup eof pos %d blk %d\n",
    392 				    sc->sc_hd->hp_xname, sc->sc_eofp,
    393 				    sc->sc_eofs[sc->sc_eofp]);
    394 #endif
    395 		}
    396 		ctstrategy(bp);
    397 		iowait(bp);
    398 	}
    399 	bp->b_flags = 0;
    400 	sc->sc_flags &= ~CTF_CMD;
    401 	if (nbp)
    402 		brelse(nbp);
    403 }
    404 
    405 void
    406 ctstrategy(bp)
    407 	register struct buf *bp;
    408 {
    409 	register struct buf *dp;
    410 	register int s, unit;
    411 
    412 	unit = UNIT(bp->b_dev);
    413 	dp = &cttab[unit];
    414 	bp->b_actf = NULL;
    415 	s = splbio();
    416 	bp->b_actb = dp->b_actb;
    417 	*dp->b_actb = bp;
    418 	dp->b_actb = &bp->b_actf;
    419 	if (dp->b_active == 0) {
    420 		dp->b_active = 1;
    421 		ctustart(unit);
    422 	}
    423 	splx(s);
    424 }
    425 
    426 ctustart(unit)
    427 	register int unit;
    428 {
    429 	register struct ct_softc *sc = &ct_softc[unit];
    430 	register struct buf *bp;
    431 
    432 	bp = cttab[unit].b_actf;
    433 	sc->sc_addr = bp->b_un.b_addr;
    434 	sc->sc_resid = bp->b_bcount;
    435 	if (hpibreq(&sc->sc_dq))
    436 		ctstart(unit);
    437 }
    438 
    439 ctstart(unit)
    440 	register int unit;
    441 {
    442 	register struct ct_softc *sc = &ct_softc[unit];
    443 	register struct buf *bp, *dp;
    444 	register int i;
    445 
    446 	bp = cttab[unit].b_actf;
    447 	if ((sc->sc_flags & CTF_CMD) && sc->sc_bp == bp) {
    448 		switch(sc->sc_cmd) {
    449 
    450 		case MTFSF:
    451 			bp->b_flags |= B_READ;
    452 			goto mustio;
    453 
    454 		case MTBSF:
    455 			goto gotaddr;
    456 
    457 		case MTOFFL:
    458 			sc->sc_blkno = 0;
    459 			sc->sc_ul.unit = C_SUNIT(sc->sc_punit);
    460 			sc->sc_ul.cmd = C_UNLOAD;
    461 			hpibsend(sc->sc_hd->hp_ctlr, sc->sc_hd->hp_slave,
    462 				C_CMD, &sc->sc_ul, sizeof(sc->sc_ul));
    463 			break;
    464 
    465 		case MTWEOF:
    466 			sc->sc_blkno++;
    467 			sc->sc_flags |= CTF_WRT;
    468 			sc->sc_wfm.unit = C_SUNIT(sc->sc_punit);
    469 			sc->sc_wfm.cmd = C_WFM;
    470 			hpibsend(sc->sc_hd->hp_ctlr, sc->sc_hd->hp_slave,
    471 				C_CMD, &sc->sc_wfm, sizeof(sc->sc_wfm));
    472 			ctaddeof(unit);
    473 			break;
    474 
    475 		case MTBSR:
    476 			sc->sc_blkno--;
    477 			goto gotaddr;
    478 
    479 		case MTFSR:
    480 			sc->sc_blkno++;
    481 			goto gotaddr;
    482 
    483 		case MTREW:
    484 			sc->sc_blkno = 0;
    485 #ifdef DEBUG
    486 			if(ctdebug & CT_BSF)
    487 				printf("%s: clearing eofs\n",
    488 				    sc->sc_hd->hp_xname);
    489 #endif
    490 			for (i=0; i<EOFS; i++)
    491 				sc->sc_eofs[i] = 0;
    492 			sc->sc_eofp = 0;
    493 
    494 gotaddr:
    495 			sc->sc_ioc.saddr = C_SADDR;
    496 			sc->sc_ioc.addr0 = 0;
    497 			sc->sc_ioc.addr = sc->sc_blkno;
    498 			sc->sc_ioc.unit = C_SUNIT(sc->sc_punit);
    499 			sc->sc_ioc.nop2 = C_NOP;
    500 			sc->sc_ioc.slen = C_SLEN;
    501 			sc->sc_ioc.len = 0;
    502 			sc->sc_ioc.nop3 = C_NOP;
    503 			sc->sc_ioc.cmd = C_READ;
    504 			hpibsend(sc->sc_hd->hp_ctlr, sc->sc_hd->hp_slave,
    505 				C_CMD, &sc->sc_ioc, sizeof(sc->sc_ioc));
    506 			break;
    507 		}
    508 	}
    509 	else {
    510 mustio:
    511 		if ((bp->b_flags & B_READ) &&
    512 		    sc->sc_flags & (CTF_BEOF|CTF_EOT)) {
    513 #ifdef DEBUG
    514 			if (ctdebug & CDB_FILES)
    515 				printf("ctstart: before flags %x\n", sc->sc_flags);
    516 #endif
    517 			if (sc->sc_flags & CTF_BEOF) {
    518 				sc->sc_flags &= ~CTF_BEOF;
    519 				sc->sc_flags |= CTF_AEOF;
    520 #ifdef DEBUG
    521 				if (ctdebug & CDB_FILES)
    522 					printf("ctstart: after flags %x\n", sc->sc_flags);
    523 #endif
    524 			}
    525 			bp->b_resid = bp->b_bcount;
    526 			ctdone(unit, bp);
    527 			return;
    528 		}
    529 		sc->sc_flags |= CTF_IO;
    530 		sc->sc_ioc.unit = C_SUNIT(sc->sc_punit);
    531 		sc->sc_ioc.saddr = C_SADDR;
    532 		sc->sc_ioc.addr0 = 0;
    533 		sc->sc_ioc.addr = sc->sc_blkno;
    534 		sc->sc_ioc.nop2 = C_NOP;
    535 		sc->sc_ioc.slen = C_SLEN;
    536 		sc->sc_ioc.len = sc->sc_resid;
    537 		sc->sc_ioc.nop3 = C_NOP;
    538 		if (bp->b_flags & B_READ)
    539 			sc->sc_ioc.cmd = C_READ;
    540 		else {
    541 			sc->sc_ioc.cmd = C_WRITE;
    542 			sc->sc_flags |= (CTF_WRT | CTF_WRTTN);
    543 		}
    544 		hpibsend(sc->sc_hd->hp_ctlr, sc->sc_hd->hp_slave, C_CMD,
    545 			&sc->sc_ioc, sizeof(sc->sc_ioc));
    546 	}
    547 	hpibawait(sc->sc_hd->hp_ctlr);
    548 }
    549 
    550 ctgo(unit)
    551 	register int unit;
    552 {
    553 	register struct ct_softc *sc = &ct_softc[unit];
    554 	register struct buf *bp;
    555 	int rw;
    556 
    557 	bp = cttab[unit].b_actf;
    558 	rw = bp->b_flags & B_READ;
    559 	hpibgo(sc->sc_hd->hp_ctlr, sc->sc_hd->hp_slave, C_EXEC,
    560 		sc->sc_addr, sc->sc_resid, rw, rw != 0);
    561 }
    562 
    563 /*
    564  * Hideous grue to handle EOF/EOT (mostly for reads)
    565  */
    566 cteof(sc, bp)
    567 	register struct ct_softc *sc;
    568 	register struct buf *bp;
    569 {
    570 	long blks;
    571 
    572 	/*
    573 	 * EOT on a write is an error.
    574 	 */
    575 	if ((bp->b_flags & B_READ) == 0) {
    576 		bp->b_resid = bp->b_bcount;
    577 		bp->b_flags |= B_ERROR;
    578 		bp->b_error = ENOSPC;
    579 		sc->sc_flags |= CTF_EOT;
    580 		return;
    581 	}
    582 	/*
    583 	 * Use returned block position to determine how many blocks
    584 	 * we really read and update b_resid.
    585 	 */
    586 	blks = sc->sc_stat.c_blk - sc->sc_blkno - 1;
    587 #ifdef DEBUG
    588 	if (ctdebug & CDB_FILES)
    589 		printf("cteof: bc %d oblk %d nblk %d read %d, resid %d\n",
    590 		       bp->b_bcount, sc->sc_blkno, sc->sc_stat.c_blk,
    591 		       blks, bp->b_bcount - CTKTOB(blks));
    592 #endif
    593 	if (blks == -1) { /* 9145 on EOF does not change sc_stat.c_blk */
    594 		blks = 0;
    595 		sc->sc_blkno++;
    596 	}
    597 	else {
    598 		sc->sc_blkno = sc->sc_stat.c_blk;
    599 	}
    600 	bp->b_resid = bp->b_bcount - CTKTOB(blks);
    601 	/*
    602 	 * If we are at physical EOV or were after an EOF,
    603 	 * we are now at logical EOT.
    604 	 */
    605 	if ((sc->sc_stat.c_aef & AEF_EOV) ||
    606 	    (sc->sc_flags & CTF_AEOF)) {
    607 		sc->sc_flags |= CTF_EOT;
    608 		sc->sc_flags &= ~(CTF_AEOF|CTF_BEOF);
    609 	}
    610 	/*
    611 	 * If we were before an EOF or we have just completed a FSF,
    612 	 * we are now after EOF.
    613 	 */
    614 	else if ((sc->sc_flags & CTF_BEOF) ||
    615 		 (sc->sc_flags & CTF_CMD) && sc->sc_cmd == MTFSF) {
    616 		sc->sc_flags |= CTF_AEOF;
    617 		sc->sc_flags &= ~CTF_BEOF;
    618 	}
    619 	/*
    620 	 * Otherwise if we read something we are now before EOF
    621 	 * (and no longer after EOF).
    622 	 */
    623 	else if (blks) {
    624 		sc->sc_flags |= CTF_BEOF;
    625 		sc->sc_flags &= ~CTF_AEOF;
    626 	}
    627 	/*
    628 	 * Finally, if we didn't read anything we just passed an EOF
    629 	 */
    630 	else
    631 		sc->sc_flags |= CTF_AEOF;
    632 #ifdef DEBUG
    633 	if (ctdebug & CDB_FILES)
    634 		printf("cteof: leaving flags %x\n", sc->sc_flags);
    635 #endif
    636 }
    637 
    638 ctintr(unit)
    639 	register int unit;
    640 {
    641 	register struct ct_softc *sc = &ct_softc[unit];
    642 	register struct buf *bp, *dp;
    643 	u_char stat;
    644 
    645 	bp = cttab[unit].b_actf;
    646 	if (bp == NULL) {
    647 		printf("%s: bp == NULL\n", sc->sc_hd->hp_xname);
    648 		return;
    649 	}
    650 	if (sc->sc_flags & CTF_IO) {
    651 		sc->sc_flags &= ~CTF_IO;
    652 		if (hpibustart(sc->sc_hd->hp_ctlr))
    653 			ctgo(unit);
    654 		return;
    655 	}
    656 	if ((sc->sc_flags & CTF_STATWAIT) == 0) {
    657 		if (hpibpptest(sc->sc_hd->hp_ctlr, sc->sc_hd->hp_slave) == 0) {
    658 			sc->sc_flags |= CTF_STATWAIT;
    659 			hpibawait(sc->sc_hd->hp_ctlr);
    660 			return;
    661 		}
    662 	} else
    663 		sc->sc_flags &= ~CTF_STATWAIT;
    664 	hpibrecv(sc->sc_hd->hp_ctlr, sc->sc_hd->hp_slave, C_QSTAT, &stat, 1);
    665 #ifdef DEBUG
    666 	if (ctdebug & CDB_FILES)
    667 		printf("ctintr: before flags %x\n", sc->sc_flags);
    668 #endif
    669 	if (stat) {
    670 		sc->sc_rsc.unit = C_SUNIT(sc->sc_punit);
    671 		sc->sc_rsc.cmd = C_STATUS;
    672 		hpibsend(sc->sc_hd->hp_ctlr, sc->sc_hd->hp_slave, C_CMD,
    673 			&sc->sc_rsc, sizeof(sc->sc_rsc));
    674 		hpibrecv(sc->sc_hd->hp_ctlr, sc->sc_hd->hp_slave, C_EXEC,
    675 			&sc->sc_stat, sizeof(sc->sc_stat));
    676 		hpibrecv(sc->sc_hd->hp_ctlr, sc->sc_hd->hp_slave, C_QSTAT,
    677 			&stat, 1);
    678 #ifdef DEBUG
    679 		if (ctdebug & CDB_FILES)
    680 			printf("ctintr: return stat 0x%x, A%x F%x blk %d\n",
    681 			       stat, sc->sc_stat.c_aef,
    682 			       sc->sc_stat.c_fef, sc->sc_stat.c_blk);
    683 #endif
    684 		if (stat == 0) {
    685 			if (sc->sc_stat.c_aef & (AEF_EOF | AEF_EOV)) {
    686 				cteof(sc, bp);
    687 				ctaddeof(unit);
    688 				goto done;
    689 			}
    690 			if (sc->sc_stat.c_fef & FEF_PF) {
    691 				ctreset(sc, sc->sc_hd);
    692 				ctstart(unit);
    693 				return;
    694 			}
    695 			if (sc->sc_stat.c_fef & FEF_REXMT) {
    696 				ctstart(unit);
    697 				return;
    698 			}
    699 			if (sc->sc_stat.c_aef & 0x5800) {
    700 				if (sc->sc_stat.c_aef & 0x4000)
    701 					tprintf(sc->sc_tpr,
    702 						"%s: uninitialized media\n",
    703 						sc->sc_hd->hp_xname);
    704 				if (sc->sc_stat.c_aef & 0x1000)
    705 					tprintf(sc->sc_tpr,
    706 						"%s: not ready\n",
    707 						sc->sc_hd->hp_xname);
    708 				if (sc->sc_stat.c_aef & 0x0800)
    709 					tprintf(sc->sc_tpr,
    710 						"%s: write protect\n",
    711 						sc->sc_hd->hp_xname);
    712 			} else {
    713 				printf("%s err: v%d u%d ru%d bn%d, ",
    714 				       sc->sc_hd->hp_xname,
    715 				       (sc->sc_stat.c_vu>>4)&0xF,
    716 				       sc->sc_stat.c_vu&0xF,
    717 				       sc->sc_stat.c_pend,
    718 				       sc->sc_stat.c_blk);
    719 				printf("R0x%x F0x%x A0x%x I0x%x\n",
    720 				       sc->sc_stat.c_ref,
    721 				       sc->sc_stat.c_fef,
    722 				       sc->sc_stat.c_aef,
    723 				       sc->sc_stat.c_ief);
    724 			}
    725 		} else
    726 			printf("%s: request status failed\n",
    727 			    sc->sc_hd->hp_xname);
    728 		bp->b_flags |= B_ERROR;
    729 		bp->b_error = EIO;
    730 		goto done;
    731 	} else
    732 		bp->b_resid = 0;
    733 	if (sc->sc_flags & CTF_CMD) {
    734 		switch (sc->sc_cmd) {
    735 		case MTFSF:
    736 			sc->sc_flags &= ~(CTF_BEOF|CTF_AEOF);
    737 			sc->sc_blkno += CTBTOK(sc->sc_resid);
    738 			ctstart(unit);
    739 			return;
    740 		case MTBSF:
    741 			sc->sc_flags &= ~(CTF_AEOF|CTF_BEOF|CTF_EOT);
    742 			break;
    743 		case MTBSR:
    744 			sc->sc_flags &= ~CTF_BEOF;
    745 			if (sc->sc_flags & CTF_EOT) {
    746 				sc->sc_flags |= CTF_AEOF;
    747 				sc->sc_flags &= ~CTF_EOT;
    748 			} else if (sc->sc_flags & CTF_AEOF) {
    749 				sc->sc_flags |= CTF_BEOF;
    750 				sc->sc_flags &= ~CTF_AEOF;
    751 			}
    752 			break;
    753 		case MTWEOF:
    754 			sc->sc_flags &= ~CTF_BEOF;
    755 			if (sc->sc_flags & (CTF_AEOF|CTF_EOT)) {
    756 				sc->sc_flags |= CTF_EOT;
    757 				sc->sc_flags &= ~CTF_AEOF;
    758 			} else
    759 				sc->sc_flags |= CTF_AEOF;
    760 			break;
    761 		case MTREW:
    762 		case MTOFFL:
    763 			sc->sc_flags &= ~(CTF_BEOF|CTF_AEOF|CTF_EOT);
    764 			break;
    765 		}
    766 	} else {
    767 		sc->sc_flags &= ~CTF_AEOF;
    768 		sc->sc_blkno += CTBTOK(sc->sc_resid);
    769 	}
    770 done:
    771 #ifdef DEBUG
    772 	if (ctdebug & CDB_FILES)
    773 		printf("ctintr: after flags %x\n", sc->sc_flags);
    774 #endif
    775 	ctdone(unit, bp);
    776 }
    777 
    778 void
    779 ctdone(unit, bp)
    780 	int unit;
    781 	register struct buf *bp;
    782 {
    783 	register struct ct_softc *sc = &ct_softc[unit];
    784 	register struct buf *dp;
    785 
    786 	if (dp = bp->b_actf)
    787 		dp->b_actb = bp->b_actb;
    788 	else
    789 		cttab[unit].b_actb = bp->b_actb;
    790 	*bp->b_actb = dp;
    791 	biodone(bp);
    792 	hpibfree(&sc->sc_dq);
    793 	if (cttab[unit].b_actf == NULL) {
    794 		cttab[unit].b_active = 0;
    795 		return;
    796 	}
    797 	ctustart(unit);
    798 }
    799 
    800 int
    801 ctread(dev, uio, flags)
    802 	dev_t dev;
    803 	struct uio *uio;
    804 	int flags;
    805 {
    806 	return (physio(ctstrategy, NULL, dev, B_READ, minphys, uio));
    807 }
    808 
    809 int
    810 ctwrite(dev, uio, flags)
    811 	dev_t dev;
    812 	struct uio *uio;
    813 	int flags;
    814 {
    815 	/* XXX: check for hardware write-protect? */
    816 	return (physio(ctstrategy, NULL, dev, B_WRITE, minphys, uio));
    817 }
    818 
    819 /*ARGSUSED*/
    820 ctioctl(dev, cmd, data, flag, p)
    821 	dev_t dev;
    822 	int cmd, flag;
    823 	caddr_t data;
    824 	struct proc *p;
    825 {
    826 	register struct mtop *op;
    827 	register int cnt;
    828 
    829 	switch (cmd) {
    830 
    831 	case MTIOCTOP:
    832 		op = (struct mtop *)data;
    833 		switch(op->mt_op) {
    834 
    835 		case MTWEOF:
    836 		case MTFSF:
    837 		case MTBSR:
    838 		case MTBSF:
    839 		case MTFSR:
    840 			cnt = op->mt_count;
    841 			break;
    842 
    843 		case MTREW:
    844 		case MTOFFL:
    845 			cnt = 1;
    846 			break;
    847 
    848 		default:
    849 			return(EINVAL);
    850 		}
    851 		ctcommand(dev, op->mt_op, cnt);
    852 		break;
    853 
    854 	case MTIOCGET:
    855 		break;
    856 
    857 	default:
    858 		return(EINVAL);
    859 	}
    860 	return(0);
    861 }
    862 
    863 /*ARGSUSED*/
    864 ctdump(dev)
    865 	dev_t dev;
    866 {
    867 	return(ENXIO);
    868 }
    869 
    870 ctaddeof(unit)
    871 	int unit;
    872 {
    873 	register struct ct_softc *sc = &ct_softc[unit];
    874 
    875 	if (sc->sc_eofp == EOFS - 1)
    876 		sc->sc_eofs[EOFS - 1]++;
    877 	else {
    878 		sc->sc_eofp++;
    879 		if (sc->sc_eofp == EOFS - 1)
    880 			sc->sc_eofs[EOFS - 1] = EOFS;
    881 		else
    882 			/* save blkno */
    883 			sc->sc_eofs[sc->sc_eofp] = sc->sc_blkno - 1;
    884 	}
    885 #ifdef DEBUG
    886 	if (ctdebug & CT_BSF)
    887 		printf("%s: add eof pos %d blk %d\n",
    888 		       sc->sc_hd->hp_xname, sc->sc_eofp,
    889 		       sc->sc_eofs[sc->sc_eofp]);
    890 #endif
    891 }
    892 #endif
    893