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