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