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