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