Home | History | Annotate | Line # | Download | only in dev
mt.c revision 1.23.2.1
      1  1.23.2.1     skrll /*	$NetBSD: mt.c,v 1.23.2.1 2004/08/03 10:34:23 skrll Exp $	*/
      2       1.9   thorpej 
      3       1.9   thorpej /*-
      4       1.9   thorpej  * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
      5       1.9   thorpej  * All rights reserved.
      6       1.9   thorpej  *
      7       1.9   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8       1.9   thorpej  * by Jason R. Thorpe.
      9       1.9   thorpej  *
     10       1.9   thorpej  * Redistribution and use in source and binary forms, with or without
     11       1.9   thorpej  * modification, are permitted provided that the following conditions
     12       1.9   thorpej  * are met:
     13       1.9   thorpej  * 1. Redistributions of source code must retain the above copyright
     14       1.9   thorpej  *    notice, this list of conditions and the following disclaimer.
     15       1.9   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.9   thorpej  *    notice, this list of conditions and the following disclaimer in the
     17       1.9   thorpej  *    documentation and/or other materials provided with the distribution.
     18       1.9   thorpej  * 3. All advertising materials mentioning features or use of this software
     19       1.9   thorpej  *    must display the following acknowledgement:
     20       1.9   thorpej  *	This product includes software developed by the NetBSD
     21       1.9   thorpej  *	Foundation, Inc. and its contributors.
     22       1.9   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23       1.9   thorpej  *    contributors may be used to endorse or promote products derived
     24       1.9   thorpej  *    from this software without specific prior written permission.
     25       1.9   thorpej  *
     26       1.9   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27       1.9   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28       1.9   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29       1.9   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30       1.9   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31       1.9   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32       1.9   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33       1.9   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34       1.9   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35       1.9   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36       1.9   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     37       1.9   thorpej  */
     38       1.1   thorpej 
     39  1.23.2.1     skrll /*
     40       1.1   thorpej  * Copyright (c) 1992, The University of Utah and
     41       1.1   thorpej  * the Computer Systems Laboratory at the University of Utah (CSL).
     42       1.1   thorpej  * All rights reserved.
     43       1.1   thorpej  *
     44       1.1   thorpej  * Permission to use, copy, modify and distribute this software is hereby
     45       1.1   thorpej  * granted provided that (1) source code retains these copyright, permission,
     46       1.1   thorpej  * and disclaimer notices, and (2) redistributions including binaries
     47       1.1   thorpej  * reproduce the notices in supporting documentation, and (3) all advertising
     48       1.1   thorpej  * materials mentioning features or use of this software display the following
     49       1.1   thorpej  * acknowledgement: ``This product includes software developed by the
     50       1.1   thorpej  * Computer Systems Laboratory at the University of Utah.''
     51       1.1   thorpej  *
     52       1.1   thorpej  * THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS
     53       1.1   thorpej  * IS" CONDITION.  THE UNIVERSITY OF UTAH AND CSL DISCLAIM ANY LIABILITY OF
     54       1.1   thorpej  * ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     55       1.1   thorpej  *
     56       1.1   thorpej  * CSL requests users of this software to return to csl-dist (at) cs.utah.edu any
     57       1.1   thorpej  * improvements that they make and grant CSL redistribution rights.
     58       1.1   thorpej  *
     59       1.1   thorpej  *	Utah $Hdr: mt.c 1.8 95/09/12$
     60       1.1   thorpej  */
     61       1.1   thorpej /*	@(#)mt.c	3.9	90/07/10	mt Xinu
     62       1.1   thorpej  *
     63       1.1   thorpej  * Magnetic tape driver (7974a, 7978a/b, 7979a, 7980a, 7980xc)
     64       1.1   thorpej  * Original version contributed by Mt. Xinu.
     65       1.1   thorpej  * Modified for 4.4BSD by Mark Davies and Andrew Vignaux, Department of
     66       1.1   thorpej  * Computer Science, Victoria University of Wellington
     67       1.1   thorpej  */
     68      1.16  gmcgarry 
     69      1.16  gmcgarry #include <sys/cdefs.h>
     70  1.23.2.1     skrll __KERNEL_RCSID(0, "$NetBSD: mt.c,v 1.23.2.1 2004/08/03 10:34:23 skrll Exp $");
     71       1.1   thorpej 
     72       1.1   thorpej #include <sys/param.h>
     73       1.1   thorpej #include <sys/systm.h>
     74      1.13   thorpej #include <sys/callout.h>
     75       1.1   thorpej #include <sys/buf.h>
     76       1.1   thorpej #include <sys/ioctl.h>
     77       1.1   thorpej #include <sys/mtio.h>
     78       1.1   thorpej #include <sys/file.h>
     79       1.1   thorpej #include <sys/proc.h>
     80       1.1   thorpej #include <sys/errno.h>
     81       1.1   thorpej #include <sys/syslog.h>
     82       1.1   thorpej #include <sys/tty.h>
     83       1.1   thorpej #include <sys/kernel.h>
     84       1.1   thorpej #include <sys/tprintf.h>
     85       1.7   thorpej #include <sys/device.h>
     86       1.7   thorpej #include <sys/conf.h>
     87       1.1   thorpej 
     88       1.1   thorpej #include <hp300/dev/hpibvar.h>
     89       1.7   thorpej 
     90       1.1   thorpej #include <hp300/dev/mtreg.h>
     91       1.1   thorpej 
     92       1.1   thorpej struct	mtinfo {
     93       1.1   thorpej 	u_short	hwid;
     94       1.1   thorpej 	char	*desc;
     95       1.1   thorpej } mtinfo[] = {
     96       1.8    scottr 	{ MT7978ID,	"7978"	},
     97       1.8    scottr 	{ MT7979AID,	"7979A"	},
     98       1.8    scottr 	{ MT7980ID,	"7980"	},
     99       1.8    scottr 	{ MT7974AID,	"7974A"	},
    100       1.1   thorpej };
    101       1.1   thorpej int	nmtinfo = sizeof(mtinfo) / sizeof(mtinfo[0]);
    102       1.1   thorpej 
    103       1.1   thorpej struct	mt_softc {
    104       1.7   thorpej 	struct	device sc_dev;
    105      1.13   thorpej 	struct	callout sc_start_ch;
    106      1.13   thorpej 	struct	callout sc_intr_ch;
    107       1.7   thorpej 	int	sc_hpibno;	/* logical HPIB this slave it attached to */
    108       1.7   thorpej 	int	sc_slave;	/* HPIB slave address (0-6) */
    109       1.1   thorpej 	short	sc_flags;	/* see below */
    110       1.1   thorpej 	u_char	sc_lastdsj;	/* place for DSJ in mtreaddsj() */
    111       1.1   thorpej 	u_char	sc_lastecmd;	/* place for End Command in mtreaddsj() */
    112       1.1   thorpej 	short	sc_recvtimeo;	/* count of hpibsend timeouts to prevent hang */
    113       1.1   thorpej 	short	sc_statindex;	/* index for next sc_stat when MTF_STATTIMEO */
    114       1.1   thorpej 	struct	mt_stat sc_stat;/* status bytes last read from device */
    115       1.1   thorpej 	short	sc_density;	/* current density of tape (mtio.h format) */
    116       1.1   thorpej 	short	sc_type;	/* tape drive model (hardware IDs) */
    117       1.7   thorpej 	struct	hpibqueue sc_hq; /* HPIB device queue member */
    118       1.1   thorpej 	tpr_t	sc_ttyp;
    119      1.17   hannken 	struct bufq_state sc_tab;/* buf queue */
    120      1.11   thorpej 	int	sc_active;
    121       1.7   thorpej 	struct buf sc_bufstore;	/* XXX buffer storage */
    122       1.7   thorpej };
    123       1.1   thorpej 
    124       1.1   thorpej #ifdef DEBUG
    125       1.1   thorpej int	mtdebug = 0;
    126       1.1   thorpej #define	dlog	if (mtdebug) log
    127       1.1   thorpej #else
    128       1.1   thorpej #define	dlog	if (0) log
    129       1.1   thorpej #endif
    130       1.1   thorpej 
    131       1.1   thorpej #define	UNIT(x)		(minor(x) & 3)
    132       1.1   thorpej 
    133       1.1   thorpej #define B_CMD		B_XXX		/* command buf instead of data */
    134       1.1   thorpej #define	b_cmd		b_blkno		/* blkno holds cmd when B_CMD */
    135       1.1   thorpej 
    136       1.7   thorpej int	mtmatch __P((struct device *, struct cfdata *, void *));
    137       1.7   thorpej void	mtattach __P((struct device *, struct device *, void *));
    138       1.7   thorpej 
    139      1.20   thorpej CFATTACH_DECL(mt, sizeof(struct mt_softc),
    140      1.20   thorpej     mtmatch, mtattach, NULL, NULL);
    141       1.1   thorpej 
    142      1.10   thorpej extern struct cfdriver mt_cd;
    143       1.7   thorpej 
    144      1.18   gehenna dev_type_open(mtopen);
    145      1.18   gehenna dev_type_close(mtclose);
    146      1.18   gehenna dev_type_read(mtread);
    147      1.18   gehenna dev_type_write(mtwrite);
    148      1.18   gehenna dev_type_ioctl(mtioctl);
    149      1.18   gehenna dev_type_strategy(mtstrategy);
    150      1.18   gehenna 
    151      1.18   gehenna const struct bdevsw mt_bdevsw = {
    152      1.18   gehenna 	mtopen, mtclose, mtstrategy, mtioctl, nodump, nosize, D_TAPE
    153      1.18   gehenna };
    154      1.18   gehenna 
    155      1.18   gehenna const struct cdevsw mt_cdevsw = {
    156      1.18   gehenna 	mtopen, mtclose, mtread, mtwrite, mtioctl,
    157      1.21  jdolecek 	nostop, notty, nopoll, nommap, nokqfilter, D_TAPE
    158      1.18   gehenna };
    159      1.18   gehenna 
    160       1.7   thorpej int	mtident __P((struct mt_softc *, struct hpibbus_attach_args *));
    161       1.7   thorpej void	mtustart __P((struct mt_softc *));
    162       1.7   thorpej int	mtreaddsj __P((struct mt_softc *, int));
    163       1.7   thorpej int	mtcommand __P((dev_t, int, int));
    164       1.7   thorpej void	spl_mtintr __P((void *));
    165       1.7   thorpej void	spl_mtstart __P((void *));
    166       1.7   thorpej 
    167       1.7   thorpej void	mtstart __P((void *));
    168       1.7   thorpej void	mtgo __P((void *));
    169       1.7   thorpej void	mtintr __P((void *));
    170       1.7   thorpej 
    171       1.2   thorpej int
    172       1.7   thorpej mtmatch(parent, match, aux)
    173       1.7   thorpej 	struct device *parent;
    174       1.7   thorpej 	struct cfdata *match;
    175       1.7   thorpej 	void *aux;
    176       1.1   thorpej {
    177       1.7   thorpej 	struct hpibbus_attach_args *ha = aux;
    178       1.2   thorpej 
    179       1.7   thorpej 	return (mtident(NULL, ha));
    180       1.2   thorpej }
    181       1.2   thorpej 
    182       1.2   thorpej void
    183       1.7   thorpej mtattach(parent, self, aux)
    184       1.7   thorpej 	struct device *parent, *self;
    185       1.7   thorpej 	void *aux;
    186       1.7   thorpej {
    187       1.7   thorpej 	struct mt_softc *sc = (struct mt_softc *)self;
    188       1.7   thorpej 	struct hpibbus_attach_args *ha = aux;
    189       1.7   thorpej 	int unit, hpibno, slave;
    190       1.7   thorpej 
    191       1.7   thorpej 	if (mtident(sc, ha) == 0) {
    192       1.7   thorpej 		printf("\n%s: impossible!\n", sc->sc_dev.dv_xname);
    193       1.7   thorpej 		return;
    194       1.7   thorpej 	}
    195       1.2   thorpej 
    196       1.7   thorpej 	unit = self->dv_unit;
    197       1.7   thorpej 	hpibno = parent->dv_unit;
    198       1.7   thorpej 	slave = ha->ha_slave;
    199       1.1   thorpej 
    200      1.17   hannken 	bufq_alloc(&sc->sc_tab, BUFQ_FCFS);
    201      1.13   thorpej 	callout_init(&sc->sc_start_ch);
    202      1.13   thorpej 	callout_init(&sc->sc_intr_ch);
    203       1.1   thorpej 
    204       1.1   thorpej 	sc->sc_hpibno = hpibno;
    205       1.1   thorpej 	sc->sc_slave = slave;
    206       1.1   thorpej 	sc->sc_flags = MTF_EXISTS;
    207       1.6   thorpej 
    208       1.7   thorpej 	/* Initialize hpib job queue entry. */
    209       1.7   thorpej 	sc->sc_hq.hq_softc = sc;
    210       1.7   thorpej 	sc->sc_hq.hq_slave = sc->sc_slave;
    211       1.7   thorpej 	sc->sc_hq.hq_start = mtstart;
    212       1.7   thorpej 	sc->sc_hq.hq_go = mtgo;
    213       1.7   thorpej 	sc->sc_hq.hq_intr = mtintr;
    214       1.7   thorpej }
    215       1.7   thorpej 
    216       1.7   thorpej int
    217       1.7   thorpej mtident(sc, ha)
    218       1.7   thorpej 	struct mt_softc *sc;
    219       1.7   thorpej 	struct hpibbus_attach_args *ha;
    220       1.7   thorpej {
    221       1.7   thorpej 	int i;
    222       1.7   thorpej 
    223       1.7   thorpej 	for (i = 0; i < nmtinfo; i++) {
    224       1.7   thorpej 		if (ha->ha_id == mtinfo[i].hwid) {
    225       1.7   thorpej 			if (sc != NULL) {
    226       1.7   thorpej 				sc->sc_type = mtinfo[i].hwid;
    227       1.7   thorpej 				printf(": %s tape\n", mtinfo[i].desc);
    228       1.7   thorpej 			}
    229       1.7   thorpej 			return (1);
    230       1.7   thorpej 		}
    231       1.7   thorpej 	}
    232       1.7   thorpej 	return (0);
    233       1.1   thorpej }
    234       1.1   thorpej 
    235       1.1   thorpej /*
    236       1.1   thorpej  * Perform a read of "Device Status Jump" register and update the
    237       1.1   thorpej  * status if necessary.  If status is read, the given "ecmd" is also
    238       1.1   thorpej  * performed, unless "ecmd" is zero.  Returns DSJ value, -1 on failure
    239       1.1   thorpej  * and -2 on "temporary" failure.
    240       1.1   thorpej  */
    241       1.7   thorpej int
    242       1.7   thorpej mtreaddsj(sc, ecmd)
    243       1.7   thorpej 	struct mt_softc *sc;
    244       1.1   thorpej 	int ecmd;
    245       1.1   thorpej {
    246       1.1   thorpej 	int retval;
    247       1.1   thorpej 
    248       1.1   thorpej 	if (sc->sc_flags & MTF_STATTIMEO)
    249       1.1   thorpej 		goto getstats;
    250       1.1   thorpej 	retval = hpibrecv(sc->sc_hpibno,
    251       1.7   thorpej 	    (sc->sc_flags & MTF_DSJTIMEO) ? -1 : sc->sc_slave,
    252       1.7   thorpej 	    MTT_DSJ, &(sc->sc_lastdsj), 1);
    253       1.1   thorpej 	sc->sc_flags &= ~MTF_DSJTIMEO;
    254       1.1   thorpej 	if (retval != 1) {
    255       1.7   thorpej 		dlog(LOG_DEBUG, "%s can't hpibrecv DSJ",
    256       1.7   thorpej 		    sc->sc_dev.dv_xname);
    257       1.1   thorpej 		if (sc->sc_recvtimeo == 0)
    258       1.1   thorpej 			sc->sc_recvtimeo = hz;
    259       1.1   thorpej 		if (--sc->sc_recvtimeo == 0)
    260       1.1   thorpej 			return (-1);
    261       1.1   thorpej 		if (retval == 0)
    262       1.1   thorpej 			sc->sc_flags |= MTF_DSJTIMEO;
    263       1.1   thorpej 		return (-2);
    264       1.1   thorpej 	}
    265       1.1   thorpej 	sc->sc_recvtimeo = 0;
    266       1.1   thorpej 	sc->sc_statindex = 0;
    267       1.7   thorpej 	dlog(LOG_DEBUG, "%s readdsj: 0x%x", sc->sc_dev.dv_xname,
    268       1.7   thorpej 	    sc->sc_lastdsj);
    269       1.1   thorpej 	sc->sc_lastecmd = ecmd;
    270       1.1   thorpej 	switch (sc->sc_lastdsj) {
    271       1.1   thorpej 	    case 0:
    272       1.1   thorpej 		if (ecmd & MTE_DSJ_FORCE)
    273       1.1   thorpej 			break;
    274       1.1   thorpej 		return (0);
    275       1.1   thorpej 
    276       1.1   thorpej 	    case 2:
    277       1.1   thorpej 		sc->sc_lastecmd = MTE_COMPLETE;
    278       1.1   thorpej 	    case 1:
    279       1.1   thorpej 		break;
    280       1.1   thorpej 
    281       1.1   thorpej 	    default:
    282       1.7   thorpej 		log(LOG_ERR, "%s readdsj: DSJ 0x%x\n", sc->sc_dev.dv_xname,
    283       1.7   thorpej 		    sc->sc_lastdsj);
    284       1.1   thorpej 		return (-1);
    285       1.1   thorpej 	}
    286       1.1   thorpej     getstats:
    287       1.1   thorpej 	retval = hpibrecv(sc->sc_hpibno,
    288       1.7   thorpej 	    (sc->sc_flags & MTF_STATCONT) ? -1 : sc->sc_slave,
    289       1.7   thorpej 	    MTT_STAT, ((char *)&(sc->sc_stat)) + sc->sc_statindex,
    290       1.7   thorpej 	    sizeof(sc->sc_stat) - sc->sc_statindex);
    291       1.1   thorpej 	sc->sc_flags &= ~(MTF_STATTIMEO | MTF_STATCONT);
    292       1.1   thorpej 	if (retval != sizeof(sc->sc_stat) - sc->sc_statindex) {
    293       1.1   thorpej 		if (sc->sc_recvtimeo == 0)
    294       1.1   thorpej 			sc->sc_recvtimeo = hz;
    295       1.1   thorpej 		if (--sc->sc_recvtimeo != 0) {
    296       1.1   thorpej 			if (retval >= 0) {
    297       1.1   thorpej 				sc->sc_statindex += retval;
    298       1.1   thorpej 				sc->sc_flags |= MTF_STATCONT;
    299       1.1   thorpej 			}
    300       1.1   thorpej 			sc->sc_flags |= MTF_STATTIMEO;
    301       1.1   thorpej 			return (-2);
    302       1.1   thorpej 		}
    303       1.7   thorpej 		log(LOG_ERR, "%s readdsj: can't read status",
    304       1.7   thorpej 		    sc->sc_dev.dv_xname);
    305       1.1   thorpej 		return (-1);
    306       1.1   thorpej 	}
    307       1.1   thorpej 	sc->sc_recvtimeo = 0;
    308       1.1   thorpej 	sc->sc_statindex = 0;
    309       1.7   thorpej 	dlog(LOG_DEBUG, "%s readdsj: status is %x %x %x %x %x %x",
    310       1.7   thorpej 	    sc->sc_dev.dv_xname,
    311       1.7   thorpej 	    sc->sc_stat1, sc->sc_stat2, sc->sc_stat3,
    312       1.7   thorpej 	    sc->sc_stat4, sc->sc_stat5, sc->sc_stat6);
    313       1.1   thorpej 	if (sc->sc_lastecmd)
    314       1.1   thorpej 		(void) hpibsend(sc->sc_hpibno, sc->sc_slave,
    315       1.7   thorpej 		    MTL_ECMD, &(sc->sc_lastecmd), 1);
    316       1.1   thorpej 	return ((int) sc->sc_lastdsj);
    317       1.1   thorpej }
    318       1.1   thorpej 
    319       1.7   thorpej int
    320      1.23      fvdl mtopen(dev, flag, mode, p)
    321       1.1   thorpej 	dev_t dev;
    322       1.1   thorpej 	int flag, mode;
    323      1.23      fvdl 	struct proc *p;
    324       1.1   thorpej {
    325       1.8    scottr 	int unit = UNIT(dev);
    326       1.7   thorpej 	struct mt_softc *sc;
    327       1.8    scottr 	int req_den;
    328       1.1   thorpej 	int error;
    329       1.1   thorpej 
    330       1.7   thorpej 	if (unit >= mt_cd.cd_ndevs ||
    331       1.7   thorpej 	    (sc = mt_cd.cd_devs[unit]) == NULL ||
    332       1.7   thorpej 	    (sc->sc_flags & MTF_EXISTS) == 0)
    333       1.1   thorpej 		return (ENXIO);
    334       1.7   thorpej 
    335       1.7   thorpej 	dlog(LOG_DEBUG, "%s open: flags 0x%x", sc->sc_dev.dv_xname,
    336       1.7   thorpej 	    sc->sc_flags);
    337       1.1   thorpej 	if (sc->sc_flags & MTF_OPEN)
    338       1.1   thorpej 		return (EBUSY);
    339       1.1   thorpej 	sc->sc_flags |= MTF_OPEN;
    340      1.23      fvdl 	sc->sc_ttyp = tprintf_open(p);
    341       1.1   thorpej 	if ((sc->sc_flags & MTF_ALIVE) == 0) {
    342       1.1   thorpej 		error = mtcommand(dev, MTRESET, 0);
    343       1.1   thorpej 		if (error != 0 || (sc->sc_flags & MTF_ALIVE) == 0)
    344       1.1   thorpej 			goto errout;
    345       1.1   thorpej 		if ((sc->sc_stat1 & (SR1_BOT | SR1_ONLINE)) == SR1_ONLINE)
    346       1.1   thorpej 			(void) mtcommand(dev, MTREW, 0);
    347       1.1   thorpej 	}
    348       1.1   thorpej 	for (;;) {
    349       1.1   thorpej 		if ((error = mtcommand(dev, MTNOP, 0)) != 0)
    350       1.1   thorpej 			goto errout;
    351       1.1   thorpej 		if (!(sc->sc_flags & MTF_REW))
    352       1.1   thorpej 			break;
    353       1.7   thorpej 		if (tsleep((caddr_t) &lbolt, PCATCH | (PZERO + 1),
    354       1.7   thorpej 		    "mt", 0) != 0) {
    355       1.1   thorpej 			error = EINTR;
    356       1.1   thorpej 			goto errout;
    357       1.1   thorpej 		}
    358       1.1   thorpej 	}
    359       1.1   thorpej 	if ((flag & FWRITE) && (sc->sc_stat1 & SR1_RO)) {
    360       1.1   thorpej 		error = EROFS;
    361       1.1   thorpej 		goto errout;
    362       1.1   thorpej 	}
    363       1.1   thorpej 	if (!(sc->sc_stat1 & SR1_ONLINE)) {
    364       1.7   thorpej 		uprintf("%s: not online\n", sc->sc_dev.dv_xname);
    365       1.1   thorpej 		error = EIO;
    366       1.1   thorpej 		goto errout;
    367       1.1   thorpej 	}
    368       1.1   thorpej 	/*
    369       1.1   thorpej 	 * Select density:
    370       1.1   thorpej 	 *  - find out what density the drive is set to
    371       1.1   thorpej 	 *	(i.e. the density of the current tape)
    372       1.1   thorpej 	 *  - if we are going to write
    373       1.1   thorpej 	 *    - if we're not at the beginning of the tape
    374       1.1   thorpej 	 *      - complain if we want to change densities
    375       1.1   thorpej 	 *    - otherwise, select the mtcommand to set the density
    376       1.1   thorpej 	 *
    377       1.1   thorpej 	 * If the drive doesn't support it then don't change the recorded
    378       1.1   thorpej 	 * density.
    379       1.1   thorpej 	 *
    380       1.1   thorpej 	 * The original MOREbsd code had these additional conditions
    381       1.1   thorpej 	 * for the mid-tape change
    382       1.1   thorpej 	 *
    383       1.1   thorpej 	 *	req_den != T_BADBPI &&
    384       1.1   thorpej 	 *	sc->sc_density != T_6250BPI
    385       1.1   thorpej 	 *
    386       1.1   thorpej 	 * which suggests that it would be possible to write multiple
    387       1.1   thorpej 	 * densities if req_den == T_BAD_BPI or the current tape
    388       1.1   thorpej 	 * density was 6250.  Testing of our 7980 suggests that the
    389       1.1   thorpej 	 * device cannot change densities mid-tape.
    390       1.1   thorpej 	 *
    391       1.1   thorpej 	 * ajv (at) comp.vuw.ac.nz
    392       1.1   thorpej 	 */
    393       1.1   thorpej 	sc->sc_density = (sc->sc_stat2 & SR2_6250) ? T_6250BPI : (
    394       1.1   thorpej 			 (sc->sc_stat3 & SR3_1600) ? T_1600BPI : (
    395       1.1   thorpej 			 (sc->sc_stat3 & SR3_800) ? T_800BPI : -1));
    396       1.1   thorpej 	req_den = (dev & T_DENSEL);
    397       1.1   thorpej 
    398       1.1   thorpej 	if (flag & FWRITE) {
    399       1.1   thorpej 		if (!(sc->sc_stat1 & SR1_BOT)) {
    400       1.1   thorpej 			if (sc->sc_density != req_den) {
    401       1.2   thorpej 				uprintf("%s: can't change density mid-tape\n",
    402       1.7   thorpej 				    sc->sc_dev.dv_xname);
    403       1.1   thorpej 				error = EIO;
    404       1.1   thorpej 				goto errout;
    405       1.1   thorpej 			}
    406       1.1   thorpej 		}
    407       1.1   thorpej 		else {
    408       1.1   thorpej 			int mtset_density =
    409       1.1   thorpej 			    (req_den == T_800BPI  ? MTSET800BPI : (
    410       1.1   thorpej 			     req_den == T_1600BPI ? MTSET1600BPI : (
    411       1.1   thorpej 			     req_den == T_6250BPI ? MTSET6250BPI : (
    412       1.1   thorpej 			     sc->sc_type == MT7980ID
    413       1.1   thorpej 						  ? MTSET6250DC
    414       1.1   thorpej 						  : MTSET6250BPI))));
    415       1.1   thorpej 			if (mtcommand(dev, mtset_density, 0) == 0)
    416       1.1   thorpej 				sc->sc_density = req_den;
    417       1.1   thorpej 		}
    418       1.1   thorpej 	}
    419       1.1   thorpej 	return (0);
    420       1.1   thorpej errout:
    421       1.1   thorpej 	sc->sc_flags &= ~MTF_OPEN;
    422       1.1   thorpej 	return (error);
    423       1.1   thorpej }
    424       1.1   thorpej 
    425       1.7   thorpej int
    426      1.23      fvdl mtclose(dev, flag, fmt, p)
    427       1.1   thorpej 	dev_t dev;
    428       1.7   thorpej 	int flag, fmt;
    429      1.23      fvdl 	struct proc *p;
    430       1.1   thorpej {
    431       1.7   thorpej 	struct mt_softc *sc = mt_cd.cd_devs[UNIT(dev)];
    432       1.1   thorpej 
    433       1.1   thorpej 	if (sc->sc_flags & MTF_WRT) {
    434       1.1   thorpej 		(void) mtcommand(dev, MTWEOF, 2);
    435       1.1   thorpej 		(void) mtcommand(dev, MTBSF, 0);
    436       1.1   thorpej 	}
    437       1.1   thorpej 	if ((minor(dev) & T_NOREWIND) == 0)
    438       1.1   thorpej 		(void) mtcommand(dev, MTREW, 0);
    439       1.1   thorpej 	sc->sc_flags &= ~MTF_OPEN;
    440       1.1   thorpej 	tprintf_close(sc->sc_ttyp);
    441       1.1   thorpej 	return (0);
    442       1.1   thorpej }
    443       1.1   thorpej 
    444       1.7   thorpej int
    445       1.1   thorpej mtcommand(dev, cmd, cnt)
    446       1.1   thorpej 	dev_t dev;
    447       1.1   thorpej 	int cmd;
    448       1.1   thorpej 	int cnt;
    449       1.1   thorpej {
    450       1.7   thorpej 	struct mt_softc *sc = mt_cd.cd_devs[UNIT(dev)];
    451       1.7   thorpej 	struct buf *bp = &sc->sc_bufstore;
    452       1.1   thorpej 	int error = 0;
    453       1.1   thorpej 
    454       1.1   thorpej #if 1
    455       1.1   thorpej 	if (bp->b_flags & B_BUSY)
    456       1.1   thorpej 		return (EBUSY);
    457       1.1   thorpej #endif
    458       1.1   thorpej 	bp->b_cmd = cmd;
    459       1.1   thorpej 	bp->b_dev = dev;
    460       1.1   thorpej 	do {
    461       1.1   thorpej 		bp->b_flags = B_BUSY | B_CMD;
    462       1.1   thorpej 		mtstrategy(bp);
    463      1.11   thorpej 		biowait(bp);
    464       1.1   thorpej 		if (bp->b_flags & B_ERROR) {
    465       1.1   thorpej 			error = (int) (unsigned) bp->b_error;
    466       1.1   thorpej 			break;
    467       1.1   thorpej 		}
    468       1.1   thorpej 	} while (--cnt > 0);
    469       1.1   thorpej #if 0
    470       1.1   thorpej 	bp->b_flags = 0 /*&= ~B_BUSY*/;
    471       1.1   thorpej #else
    472       1.1   thorpej 	bp->b_flags &= ~B_BUSY;
    473       1.1   thorpej #endif
    474       1.1   thorpej 	return (error);
    475       1.1   thorpej }
    476       1.1   thorpej 
    477       1.1   thorpej /*
    478       1.1   thorpej  * Only thing to check here is for legal record lengths (writes only).
    479       1.1   thorpej  */
    480       1.1   thorpej void
    481       1.1   thorpej mtstrategy(bp)
    482       1.8    scottr 	struct buf *bp;
    483       1.1   thorpej {
    484       1.8    scottr 	struct mt_softc *sc;
    485       1.8    scottr 	int unit;
    486       1.8    scottr 	int s;
    487       1.1   thorpej 
    488       1.1   thorpej 	unit = UNIT(bp->b_dev);
    489       1.7   thorpej 	sc = mt_cd.cd_devs[unit];
    490       1.7   thorpej 	dlog(LOG_DEBUG, "%s strategy", sc->sc_dev.dv_xname);
    491       1.1   thorpej 	if ((bp->b_flags & (B_CMD | B_READ)) == 0) {
    492       1.1   thorpej #define WRITE_BITS_IGNORED	8
    493       1.1   thorpej #if 0
    494       1.1   thorpej 		if (bp->b_bcount & ((1 << WRITE_BITS_IGNORED) - 1)) {
    495       1.1   thorpej 			tprintf(sc->sc_ttyp,
    496       1.2   thorpej 				"%s: write record must be multiple of %d\n",
    497       1.7   thorpej 				sc->sc_dev.dv_xname, 1 << WRITE_BITS_IGNORED);
    498       1.1   thorpej 			goto error;
    499       1.1   thorpej 		}
    500       1.1   thorpej #endif
    501       1.1   thorpej 		s = 16 * 1024;
    502       1.1   thorpej 		if (sc->sc_stat2 & SR2_LONGREC) {
    503       1.1   thorpej 			switch (sc->sc_density) {
    504       1.1   thorpej 			    case T_1600BPI:
    505       1.1   thorpej 				s = 32 * 1024;
    506       1.1   thorpej 				break;
    507       1.1   thorpej 
    508       1.1   thorpej 			    case T_6250BPI:
    509       1.1   thorpej 			    case T_BADBPI:
    510       1.1   thorpej 				s = 60 * 1024;
    511       1.1   thorpej 				break;
    512       1.1   thorpej 			}
    513       1.1   thorpej 		}
    514       1.1   thorpej 		if (bp->b_bcount > s) {
    515       1.1   thorpej 			tprintf(sc->sc_ttyp,
    516       1.8    scottr 				"%s: write record (%ld) too big: limit (%d)\n",
    517       1.7   thorpej 				sc->sc_dev.dv_xname, bp->b_bcount, s);
    518       1.8    scottr #if 0 /* XXX see above */
    519       1.1   thorpej 	    error:
    520       1.8    scottr #endif
    521       1.1   thorpej 			bp->b_flags |= B_ERROR;
    522       1.1   thorpej 			bp->b_error = EIO;
    523      1.11   thorpej 			biodone(bp);
    524       1.1   thorpej 			return;
    525       1.1   thorpej 		}
    526       1.1   thorpej 	}
    527       1.1   thorpej 	s = splbio();
    528      1.17   hannken 	BUFQ_PUT(&sc->sc_tab, bp);
    529      1.11   thorpej 	if (sc->sc_active == 0) {
    530      1.11   thorpej 		sc->sc_active = 1;
    531       1.7   thorpej 		mtustart(sc);
    532       1.1   thorpej 	}
    533       1.1   thorpej 	splx(s);
    534       1.1   thorpej }
    535       1.1   thorpej 
    536       1.1   thorpej void
    537       1.7   thorpej mtustart(sc)
    538       1.7   thorpej 	struct mt_softc *sc;
    539       1.1   thorpej {
    540       1.1   thorpej 
    541       1.7   thorpej 	dlog(LOG_DEBUG, "%s ustart", sc->sc_dev.dv_xname);
    542       1.7   thorpej 	if (hpibreq(sc->sc_dev.dv_parent, &sc->sc_hq))
    543       1.7   thorpej 		mtstart(sc);
    544       1.1   thorpej }
    545       1.1   thorpej 
    546       1.1   thorpej void
    547       1.3   thorpej spl_mtintr(arg)
    548       1.3   thorpej 	void *arg;
    549       1.1   thorpej {
    550       1.3   thorpej 	struct mt_softc *sc = arg;
    551       1.1   thorpej 	int s = splbio();
    552       1.1   thorpej 
    553       1.3   thorpej 	hpibppclear(sc->sc_hpibno);
    554       1.3   thorpej 	mtintr(sc);
    555      1.15  gmcgarry 	splx(s);
    556       1.1   thorpej }
    557       1.1   thorpej 
    558       1.1   thorpej void
    559       1.7   thorpej spl_mtstart(arg)
    560       1.7   thorpej 	void *arg;
    561       1.1   thorpej {
    562       1.1   thorpej 	int s = splbio();
    563       1.1   thorpej 
    564       1.7   thorpej 	mtstart(arg);
    565      1.15  gmcgarry 	splx(s);
    566       1.1   thorpej }
    567       1.1   thorpej 
    568       1.1   thorpej void
    569       1.7   thorpej mtstart(arg)
    570       1.7   thorpej 	void *arg;
    571       1.1   thorpej {
    572       1.7   thorpej 	struct mt_softc *sc = arg;
    573      1.12   frueauf 	struct buf *bp;
    574       1.1   thorpej 	short	cmdcount = 1;
    575       1.1   thorpej 	u_char	cmdbuf[2];
    576       1.1   thorpej 
    577       1.7   thorpej 	dlog(LOG_DEBUG, "%s start", sc->sc_dev.dv_xname);
    578       1.1   thorpej 	sc->sc_flags &= ~MTF_WRT;
    579      1.17   hannken 	bp = BUFQ_PEEK(&sc->sc_tab);
    580       1.1   thorpej 	if ((sc->sc_flags & MTF_ALIVE) == 0 &&
    581       1.1   thorpej 	    ((bp->b_flags & B_CMD) == 0 || bp->b_cmd != MTRESET))
    582       1.1   thorpej 		goto fatalerror;
    583       1.1   thorpej 
    584       1.1   thorpej 	if (sc->sc_flags & MTF_REW) {
    585       1.1   thorpej 		if (!hpibpptest(sc->sc_hpibno, sc->sc_slave))
    586       1.1   thorpej 			goto stillrew;
    587       1.7   thorpej 		switch (mtreaddsj(sc, MTE_DSJ_FORCE|MTE_COMPLETE|MTE_IDLE)) {
    588       1.1   thorpej 		    case 0:
    589       1.1   thorpej 		    case 1:
    590       1.1   thorpej 		stillrew:
    591       1.1   thorpej 			if ((sc->sc_stat1 & SR1_BOT) ||
    592       1.1   thorpej 			    !(sc->sc_stat1 & SR1_ONLINE)) {
    593       1.1   thorpej 				sc->sc_flags &= ~MTF_REW;
    594       1.1   thorpej 				break;
    595       1.1   thorpej 			}
    596       1.1   thorpej 		    case -2:
    597       1.1   thorpej 			/*
    598       1.1   thorpej 			 * -2 means "timeout" reading DSJ, which is probably
    599       1.1   thorpej 			 * temporary.  This is considered OK when doing a NOP,
    600       1.1   thorpej 			 * but not otherwise.
    601       1.1   thorpej 			 */
    602       1.1   thorpej 			if (sc->sc_flags & (MTF_DSJTIMEO | MTF_STATTIMEO)) {
    603      1.13   thorpej 				callout_reset(&sc->sc_start_ch, hz >> 5,
    604      1.13   thorpej 				    spl_mtstart, sc);
    605       1.1   thorpej 				return;
    606       1.1   thorpej 			}
    607       1.1   thorpej 		    case 2:
    608       1.1   thorpej 			if (bp->b_cmd != MTNOP || !(bp->b_flags & B_CMD)) {
    609       1.1   thorpej 				bp->b_error = EBUSY;
    610       1.1   thorpej 				goto errdone;
    611       1.1   thorpej 			}
    612       1.1   thorpej 			goto done;
    613       1.1   thorpej 
    614       1.1   thorpej 		    default:
    615       1.1   thorpej 			goto fatalerror;
    616       1.1   thorpej 		}
    617       1.1   thorpej 	}
    618       1.1   thorpej 	if (bp->b_flags & B_CMD) {
    619       1.1   thorpej 		if (sc->sc_flags & MTF_PASTEOT) {
    620       1.1   thorpej 			switch(bp->b_cmd) {
    621       1.1   thorpej 			    case MTFSF:
    622       1.1   thorpej 			    case MTWEOF:
    623       1.1   thorpej 			    case MTFSR:
    624       1.1   thorpej 				bp->b_error = ENOSPC;
    625       1.1   thorpej 				goto errdone;
    626       1.1   thorpej 
    627       1.1   thorpej 			    case MTBSF:
    628       1.1   thorpej 			    case MTOFFL:
    629       1.1   thorpej 			    case MTBSR:
    630       1.1   thorpej 			    case MTREW:
    631       1.1   thorpej 				sc->sc_flags &= ~(MTF_PASTEOT | MTF_ATEOT);
    632       1.1   thorpej 				break;
    633       1.1   thorpej 			}
    634       1.1   thorpej 		}
    635       1.1   thorpej 		switch(bp->b_cmd) {
    636       1.1   thorpej 		    case MTFSF:
    637       1.1   thorpej 			if (sc->sc_flags & MTF_HITEOF)
    638       1.1   thorpej 				goto done;
    639       1.1   thorpej 			cmdbuf[0] = MTTC_FSF;
    640       1.1   thorpej 			break;
    641       1.1   thorpej 
    642       1.1   thorpej 		    case MTBSF:
    643       1.1   thorpej 			if (sc->sc_flags & MTF_HITBOF)
    644       1.1   thorpej 				goto done;
    645       1.1   thorpej 			cmdbuf[0] = MTTC_BSF;
    646       1.1   thorpej 			break;
    647       1.1   thorpej 
    648       1.1   thorpej 		    case MTOFFL:
    649       1.1   thorpej 			sc->sc_flags |= MTF_REW;
    650       1.1   thorpej 			cmdbuf[0] = MTTC_REWOFF;
    651       1.1   thorpej 			break;
    652       1.1   thorpej 
    653       1.1   thorpej 		    case MTWEOF:
    654       1.1   thorpej 			cmdbuf[0] = MTTC_WFM;
    655       1.1   thorpej 			break;
    656       1.1   thorpej 
    657       1.1   thorpej 		    case MTBSR:
    658       1.1   thorpej 			cmdbuf[0] = MTTC_BSR;
    659       1.1   thorpej 			break;
    660       1.1   thorpej 
    661       1.1   thorpej 		    case MTFSR:
    662       1.1   thorpej 			cmdbuf[0] = MTTC_FSR;
    663       1.1   thorpej 			break;
    664       1.1   thorpej 
    665       1.1   thorpej 		    case MTREW:
    666       1.1   thorpej 			sc->sc_flags |= MTF_REW;
    667       1.1   thorpej 			cmdbuf[0] = MTTC_REW;
    668       1.1   thorpej 			break;
    669       1.1   thorpej 
    670       1.1   thorpej 		    case MTNOP:
    671       1.1   thorpej 			/*
    672       1.1   thorpej 			 * NOP is supposed to set status bits.
    673       1.1   thorpej 			 * Force readdsj to do it.
    674       1.1   thorpej 			 */
    675       1.7   thorpej 			switch (mtreaddsj(sc,
    676       1.7   thorpej 			  MTE_DSJ_FORCE | MTE_COMPLETE | MTE_IDLE)) {
    677       1.1   thorpej 			    default:
    678       1.1   thorpej 				goto done;
    679       1.1   thorpej 
    680       1.1   thorpej 			    case -1:
    681       1.1   thorpej 				/*
    682       1.1   thorpej 				 * If this fails, perform a device clear
    683       1.1   thorpej 				 * to fix any protocol problems and (most
    684       1.1   thorpej 				 * likely) get the status.
    685       1.1   thorpej 				 */
    686       1.1   thorpej 				bp->b_cmd = MTRESET;
    687       1.1   thorpej 				break;
    688       1.1   thorpej 
    689       1.1   thorpej 			    case -2:
    690      1.13   thorpej 				callout_reset(&sc->sc_start_ch, hz >> 5,
    691      1.13   thorpej 				    spl_mtstart, sc);
    692       1.1   thorpej 				return;
    693       1.1   thorpej 			}
    694       1.1   thorpej 
    695       1.1   thorpej 		    case MTRESET:
    696       1.1   thorpej 			/*
    697       1.1   thorpej 			 * 1) selected device clear (send with "-2" secondary)
    698       1.1   thorpej 			 * 2) set timeout, then wait for "service request"
    699       1.1   thorpej 			 * 3) interrupt will read DSJ (and END COMPLETE-IDLE)
    700       1.1   thorpej 			 */
    701       1.1   thorpej 			if (hpibsend(sc->sc_hpibno, sc->sc_slave, -2, NULL, 0)){
    702       1.7   thorpej 				log(LOG_ERR, "%s can't reset",
    703       1.7   thorpej 				    sc->sc_dev.dv_xname);
    704       1.1   thorpej 				goto fatalerror;
    705       1.1   thorpej 			}
    706      1.13   thorpej 			callout_reset(&sc->sc_intr_ch, 4 * hz, spl_mtintr, sc);
    707       1.8    scottr 			hpibawait(sc->sc_hpibno);
    708       1.1   thorpej 			return;
    709       1.1   thorpej 
    710       1.1   thorpej 		    case MTSET800BPI:
    711       1.1   thorpej 			cmdbuf[0] = MTTC_800;
    712       1.1   thorpej 			break;
    713       1.1   thorpej 
    714       1.1   thorpej 		    case MTSET1600BPI:
    715       1.1   thorpej 			cmdbuf[0] = MTTC_1600;
    716       1.1   thorpej 			break;
    717       1.1   thorpej 
    718       1.1   thorpej 		    case MTSET6250BPI:
    719       1.1   thorpej 			cmdbuf[0] = MTTC_6250;
    720       1.1   thorpej 			break;
    721       1.1   thorpej 
    722       1.1   thorpej 		    case MTSET6250DC:
    723       1.1   thorpej 			cmdbuf[0] = MTTC_DC6250;
    724       1.1   thorpej 			break;
    725       1.1   thorpej 		}
    726       1.1   thorpej 	} else {
    727       1.1   thorpej 		if (sc->sc_flags & MTF_PASTEOT) {
    728       1.1   thorpej 			bp->b_error = ENOSPC;
    729       1.1   thorpej 			goto errdone;
    730       1.1   thorpej 		}
    731       1.1   thorpej 		if (bp->b_flags & B_READ) {
    732       1.1   thorpej 			sc->sc_flags |= MTF_IO;
    733       1.1   thorpej 			cmdbuf[0] = MTTC_READ;
    734       1.1   thorpej 		} else {
    735       1.1   thorpej 			sc->sc_flags |= MTF_WRT | MTF_IO;
    736       1.1   thorpej 			cmdbuf[0] = MTTC_WRITE;
    737       1.1   thorpej 			cmdbuf[1] = (bp->b_bcount + ((1 << WRITE_BITS_IGNORED) - 1)) >> WRITE_BITS_IGNORED;
    738       1.1   thorpej 			cmdcount = 2;
    739       1.1   thorpej 		}
    740       1.1   thorpej 	}
    741       1.1   thorpej 	if (hpibsend(sc->sc_hpibno, sc->sc_slave, MTL_TCMD, cmdbuf, cmdcount)
    742       1.1   thorpej 	    == cmdcount) {
    743       1.1   thorpej 		if (sc->sc_flags & MTF_REW)
    744       1.1   thorpej 			goto done;
    745       1.1   thorpej 		hpibawait(sc->sc_hpibno);
    746       1.1   thorpej 		return;
    747       1.1   thorpej 	}
    748       1.1   thorpej fatalerror:
    749       1.1   thorpej 	/*
    750       1.1   thorpej 	 * If anything fails, the drive is probably hosed, so mark it not
    751       1.1   thorpej 	 * "ALIVE" (but it EXISTS and is OPEN or we wouldn't be here, and
    752       1.1   thorpej 	 * if, last we heard, it was REWinding, remember that).
    753       1.1   thorpej 	 */
    754       1.1   thorpej 	sc->sc_flags &= MTF_EXISTS | MTF_OPEN | MTF_REW;
    755       1.1   thorpej 	bp->b_error = EIO;
    756       1.1   thorpej errdone:
    757       1.1   thorpej 	bp->b_flags |= B_ERROR;
    758       1.1   thorpej done:
    759       1.1   thorpej 	sc->sc_flags &= ~(MTF_HITEOF | MTF_HITBOF);
    760      1.17   hannken 	(void)BUFQ_GET(&sc->sc_tab);
    761      1.11   thorpej 	biodone(bp);
    762       1.7   thorpej 	hpibfree(sc->sc_dev.dv_parent, &sc->sc_hq);
    763      1.17   hannken 	if ((bp = BUFQ_PEEK(&sc->sc_tab)) == NULL)
    764      1.11   thorpej 		sc->sc_active = 0;
    765       1.1   thorpej 	else
    766       1.7   thorpej 		mtustart(sc);
    767       1.1   thorpej }
    768       1.1   thorpej 
    769       1.1   thorpej /*
    770       1.1   thorpej  * The Utah code had a bug which meant that the driver was unable to read.
    771       1.1   thorpej  * "rw" was initialized to bp->b_flags & B_READ before "bp" was initialized.
    772       1.1   thorpej  *   -- ajv (at) comp.vuw.ac.nz
    773       1.1   thorpej  */
    774       1.1   thorpej void
    775       1.7   thorpej mtgo(arg)
    776       1.7   thorpej 	void *arg;
    777       1.1   thorpej {
    778       1.7   thorpej 	struct mt_softc *sc = arg;
    779       1.7   thorpej 	struct buf *bp;
    780       1.1   thorpej 	int rw;
    781       1.1   thorpej 
    782       1.7   thorpej 	dlog(LOG_DEBUG, "%s go", sc->sc_dev.dv_xname);
    783      1.17   hannken 	bp = BUFQ_PEEK(&sc->sc_tab);
    784       1.1   thorpej 	rw = bp->b_flags & B_READ;
    785       1.1   thorpej 	hpibgo(sc->sc_hpibno, sc->sc_slave, rw ? MTT_READ : MTL_WRITE,
    786      1.14   thorpej 	    bp->b_data, bp->b_bcount, rw, rw != 0);
    787       1.1   thorpej }
    788       1.1   thorpej 
    789       1.7   thorpej void
    790       1.3   thorpej mtintr(arg)
    791       1.3   thorpej 	void *arg;
    792       1.1   thorpej {
    793       1.7   thorpej 	struct mt_softc *sc = arg;
    794      1.12   frueauf 	struct buf *bp;
    795       1.7   thorpej 	int i;
    796       1.7   thorpej 	u_char cmdbuf[4];
    797       1.1   thorpej 
    798      1.17   hannken 	bp = BUFQ_PEEK(&sc->sc_tab);
    799       1.1   thorpej 	if (bp == NULL) {
    800       1.7   thorpej 		log(LOG_ERR, "%s intr: bp == NULL", sc->sc_dev.dv_xname);
    801       1.1   thorpej 		return;
    802       1.1   thorpej 	}
    803       1.7   thorpej 
    804       1.7   thorpej 	dlog(LOG_DEBUG, "%s intr", sc->sc_dev.dv_xname);
    805       1.7   thorpej 
    806       1.1   thorpej 	/*
    807       1.1   thorpej 	 * Some operation completed.  Read status bytes and report errors.
    808       1.1   thorpej 	 * Clear EOF flags here `cause they're set once on specific conditions
    809       1.1   thorpej 	 * below when a command succeeds.
    810       1.1   thorpej 	 * A DSJ of 2 always means keep waiting.  If the command was READ
    811       1.1   thorpej 	 * (and we're in data DMA phase) stop data transfer first.
    812       1.1   thorpej 	 */
    813       1.1   thorpej 	sc->sc_flags &= ~(MTF_HITEOF | MTF_HITBOF);
    814       1.1   thorpej 	if ((bp->b_flags & (B_CMD|B_READ)) == B_READ &&
    815       1.1   thorpej 	    !(sc->sc_flags & (MTF_IO | MTF_STATTIMEO | MTF_DSJTIMEO))){
    816       1.1   thorpej 		cmdbuf[0] = MTE_STOP;
    817       1.1   thorpej 		(void) hpibsend(sc->sc_hpibno, sc->sc_slave, MTL_ECMD,cmdbuf,1);
    818       1.1   thorpej 	}
    819       1.7   thorpej 	switch (mtreaddsj(sc, 0)) {
    820       1.1   thorpej 	    case 0:
    821       1.1   thorpej 		break;
    822       1.1   thorpej 
    823       1.1   thorpej 	    case 1:
    824       1.1   thorpej 		/*
    825       1.1   thorpej 		 * If we're in the middle of a READ/WRITE and have yet to
    826       1.1   thorpej 		 * start the data transfer, a DSJ of one should terminate it.
    827       1.1   thorpej 		 */
    828       1.1   thorpej 		sc->sc_flags &= ~MTF_IO;
    829       1.1   thorpej 		break;
    830       1.1   thorpej 
    831       1.1   thorpej 	    case 2:
    832       1.1   thorpej 		(void) hpibawait(sc->sc_hpibno);
    833       1.1   thorpej 		return;
    834       1.1   thorpej 
    835       1.1   thorpej 	    case -2:
    836       1.1   thorpej 		/*
    837       1.1   thorpej 		 * -2 means that the drive failed to respond quickly enough
    838       1.1   thorpej 		 * to the request for DSJ.  It's probably just "busy" figuring
    839       1.1   thorpej 		 * it out and will know in a little bit...
    840       1.1   thorpej 		 */
    841      1.13   thorpej 		callout_reset(&sc->sc_intr_ch, hz >> 5, spl_mtintr, sc);
    842       1.1   thorpej 		return;
    843       1.1   thorpej 
    844       1.1   thorpej 	    default:
    845       1.7   thorpej 		log(LOG_ERR, "%s intr: can't get drive stat",
    846       1.7   thorpej 		    sc->sc_dev.dv_xname);
    847       1.1   thorpej 		goto error;
    848       1.1   thorpej 	}
    849       1.1   thorpej 	if (sc->sc_stat1 & (SR1_ERR | SR1_REJECT)) {
    850       1.1   thorpej 		i = sc->sc_stat4 & SR4_ERCLMASK;
    851       1.7   thorpej 		log(LOG_ERR, "%s: %s error, retry %d, SR2/3 %x/%x, code %d",
    852       1.7   thorpej 			sc->sc_dev.dv_xname, i == SR4_DEVICE ? "device" :
    853       1.1   thorpej 			(i == SR4_PROTOCOL ? "protocol" :
    854       1.1   thorpej 			(i == SR4_SELFTEST ? "selftest" : "unknown")),
    855       1.1   thorpej 			sc->sc_stat4 & SR4_RETRYMASK, sc->sc_stat2,
    856       1.1   thorpej 			sc->sc_stat3, sc->sc_stat5);
    857       1.1   thorpej 
    858       1.1   thorpej 		if ((bp->b_flags & B_CMD) && bp->b_cmd == MTRESET)
    859      1.13   thorpej 			callout_stop(&sc->sc_intr_ch);
    860       1.1   thorpej 		if (sc->sc_stat3 & SR3_POWERUP)
    861       1.1   thorpej 			sc->sc_flags &= MTF_OPEN | MTF_EXISTS;
    862       1.1   thorpej 		goto error;
    863       1.1   thorpej 	}
    864       1.1   thorpej 	/*
    865       1.1   thorpej 	 * Report and clear any soft errors.
    866       1.1   thorpej 	 */
    867       1.1   thorpej 	if (sc->sc_stat1 & SR1_SOFTERR) {
    868       1.2   thorpej 		log(LOG_WARNING, "%s: soft error, retry %d\n",
    869       1.7   thorpej 			sc->sc_dev.dv_xname, sc->sc_stat4 & SR4_RETRYMASK);
    870       1.1   thorpej 		sc->sc_stat1 &= ~SR1_SOFTERR;
    871       1.1   thorpej 	}
    872       1.1   thorpej 	/*
    873       1.1   thorpej 	 * We've initiated a read or write, but haven't actually started to
    874       1.1   thorpej 	 * DMA the data yet.  At this point, the drive's ready.
    875       1.1   thorpej 	 */
    876       1.1   thorpej 	if (sc->sc_flags & MTF_IO) {
    877       1.1   thorpej 		sc->sc_flags &= ~MTF_IO;
    878       1.1   thorpej 		if (hpibustart(sc->sc_hpibno))
    879       1.7   thorpej 			mtgo(sc);
    880       1.1   thorpej 		return;
    881       1.1   thorpej 	}
    882       1.1   thorpej 	/*
    883       1.1   thorpej 	 * Check for End Of Tape - we're allowed to hit EOT and then write (or
    884       1.1   thorpej 	 * read) one more record.  If we get here and have not already hit EOT,
    885       1.1   thorpej 	 * return ENOSPC to inform the process that it's hit it.  If we get
    886       1.1   thorpej 	 * here and HAVE already hit EOT, don't allow any more operations that
    887       1.1   thorpej 	 * move the tape forward.
    888       1.1   thorpej 	 */
    889       1.1   thorpej 	if (sc->sc_stat1 & SR1_EOT) {
    890       1.1   thorpej 		if (sc->sc_flags & MTF_ATEOT)
    891       1.1   thorpej 			sc->sc_flags |= MTF_PASTEOT;
    892       1.1   thorpej 		else {
    893       1.1   thorpej 			bp->b_flags |= B_ERROR;
    894       1.1   thorpej 			bp->b_error = ENOSPC;
    895       1.1   thorpej 			sc->sc_flags |= MTF_ATEOT;
    896       1.1   thorpej 		}
    897       1.1   thorpej 	}
    898       1.1   thorpej 	/*
    899       1.1   thorpej 	 * If a motion command was being executed, check for Tape Marks.
    900       1.1   thorpej 	 * If we were doing data, make sure we got the right amount, and
    901       1.1   thorpej 	 * check for hitting tape marks on reads.
    902       1.1   thorpej 	 */
    903       1.1   thorpej 	if (bp->b_flags & B_CMD) {
    904       1.1   thorpej 		if (sc->sc_stat1 & SR1_EOF) {
    905       1.1   thorpej 			if (bp->b_cmd == MTFSR)
    906       1.1   thorpej 				sc->sc_flags |= MTF_HITEOF;
    907       1.1   thorpej 			if (bp->b_cmd == MTBSR)
    908       1.1   thorpej 				sc->sc_flags |= MTF_HITBOF;
    909       1.1   thorpej 		}
    910       1.1   thorpej 		if (bp->b_cmd == MTRESET) {
    911      1.13   thorpej 			callout_stop(&sc->sc_intr_ch);
    912       1.1   thorpej 			sc->sc_flags |= MTF_ALIVE;
    913       1.1   thorpej 		}
    914       1.1   thorpej 	} else {
    915       1.1   thorpej 		i = hpibrecv(sc->sc_hpibno, sc->sc_slave, MTT_BCNT, cmdbuf, 2);
    916       1.1   thorpej 		if (i != 2) {
    917       1.8    scottr 			log(LOG_ERR, "%s intr: can't get xfer length\n",
    918       1.8    scottr 			    sc->sc_dev.dv_xname);
    919       1.1   thorpej 			goto error;
    920       1.1   thorpej 		}
    921       1.1   thorpej 		i = (int) *((u_short *) cmdbuf);
    922       1.1   thorpej 		if (i <= bp->b_bcount) {
    923       1.1   thorpej 			if (i == 0)
    924       1.1   thorpej 				sc->sc_flags |= MTF_HITEOF;
    925       1.1   thorpej 			bp->b_resid = bp->b_bcount - i;
    926       1.8    scottr 			dlog(LOG_DEBUG, "%s intr: bcount %ld, resid %ld",
    927       1.7   thorpej 			    sc->sc_dev.dv_xname, bp->b_bcount, bp->b_resid);
    928       1.1   thorpej 		} else {
    929       1.1   thorpej 			tprintf(sc->sc_ttyp,
    930       1.8    scottr 				"%s: record (%d) larger than wanted (%ld)\n",
    931       1.7   thorpej 				sc->sc_dev.dv_xname, i, bp->b_bcount);
    932       1.1   thorpej     error:
    933       1.1   thorpej 			sc->sc_flags &= ~MTF_IO;
    934       1.1   thorpej 			bp->b_error = EIO;
    935       1.1   thorpej 			bp->b_flags |= B_ERROR;
    936       1.1   thorpej 		}
    937       1.1   thorpej 	}
    938       1.1   thorpej 	/*
    939       1.1   thorpej 	 * The operation is completely done.
    940       1.1   thorpej 	 * Let the drive know with an END command.
    941       1.1   thorpej 	 */
    942       1.1   thorpej 	cmdbuf[0] = MTE_COMPLETE | MTE_IDLE;
    943       1.1   thorpej 	(void) hpibsend(sc->sc_hpibno, sc->sc_slave, MTL_ECMD, cmdbuf, 1);
    944       1.1   thorpej 	bp->b_flags &= ~B_CMD;
    945      1.17   hannken 	(void)BUFQ_GET(&sc->sc_tab);
    946      1.11   thorpej 	biodone(bp);
    947       1.7   thorpej 	hpibfree(sc->sc_dev.dv_parent, &sc->sc_hq);
    948      1.17   hannken 	if (BUFQ_PEEK(&sc->sc_tab) == NULL)
    949      1.11   thorpej 		sc->sc_active = 0;
    950       1.1   thorpej 	else
    951       1.7   thorpej 		mtustart(sc);
    952       1.1   thorpej }
    953       1.1   thorpej 
    954       1.7   thorpej int
    955       1.7   thorpej mtread(dev, uio, flags)
    956       1.1   thorpej 	dev_t dev;
    957       1.1   thorpej 	struct uio *uio;
    958       1.7   thorpej 	int flags;
    959       1.1   thorpej {
    960       1.7   thorpej 	struct mt_softc *sc = mt_cd.cd_devs[UNIT(dev)];
    961       1.7   thorpej 
    962       1.7   thorpej 	return(physio(mtstrategy, &sc->sc_bufstore,
    963       1.7   thorpej 	    dev, B_READ, minphys, uio));
    964       1.1   thorpej }
    965       1.1   thorpej 
    966       1.7   thorpej int
    967       1.7   thorpej mtwrite(dev, uio, flags)
    968       1.1   thorpej 	dev_t dev;
    969       1.1   thorpej 	struct uio *uio;
    970       1.7   thorpej 	int flags;
    971       1.1   thorpej {
    972       1.7   thorpej 	struct mt_softc *sc = mt_cd.cd_devs[UNIT(dev)];
    973       1.7   thorpej 
    974       1.7   thorpej 	return(physio(mtstrategy, &sc->sc_bufstore,
    975       1.7   thorpej 	    dev, B_WRITE, minphys, uio));
    976       1.1   thorpej }
    977       1.1   thorpej 
    978       1.7   thorpej int
    979      1.23      fvdl mtioctl(dev, cmd, data, flag, p)
    980       1.1   thorpej 	dev_t dev;
    981       1.1   thorpej 	u_long cmd;
    982       1.1   thorpej 	caddr_t data;
    983       1.1   thorpej 	int flag;
    984      1.23      fvdl 	struct proc *p;
    985       1.1   thorpej {
    986       1.8    scottr 	struct mtop *op;
    987       1.1   thorpej 	int cnt;
    988       1.1   thorpej 
    989       1.1   thorpej 	switch (cmd) {
    990       1.1   thorpej 	    case MTIOCTOP:
    991       1.1   thorpej 		op = (struct mtop *)data;
    992       1.1   thorpej 		switch(op->mt_op) {
    993       1.1   thorpej 		    case MTWEOF:
    994       1.1   thorpej 		    case MTFSF:
    995       1.1   thorpej 		    case MTBSR:
    996       1.1   thorpej 		    case MTBSF:
    997       1.1   thorpej 		    case MTFSR:
    998       1.1   thorpej 			cnt = op->mt_count;
    999       1.1   thorpej 			break;
   1000       1.1   thorpej 
   1001       1.1   thorpej 		    case MTOFFL:
   1002       1.1   thorpej 		    case MTREW:
   1003       1.1   thorpej 		    case MTNOP:
   1004       1.1   thorpej 			cnt = 0;
   1005       1.1   thorpej 			break;
   1006       1.1   thorpej 
   1007       1.1   thorpej 		    default:
   1008       1.1   thorpej 			return (EINVAL);
   1009       1.1   thorpej 		}
   1010       1.1   thorpej 		return (mtcommand(dev, op->mt_op, cnt));
   1011       1.1   thorpej 
   1012       1.1   thorpej 	    case MTIOCGET:
   1013       1.1   thorpej 		break;
   1014       1.1   thorpej 
   1015       1.1   thorpej 	    default:
   1016       1.1   thorpej 		return (EINVAL);
   1017       1.1   thorpej 	}
   1018       1.1   thorpej 	return (0);
   1019       1.1   thorpej }
   1020