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