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