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