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