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