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