Home | History | Annotate | Line # | Download | only in qbus
ts.c revision 1.2.6.1
      1  1.2.6.1   fvdl /*	$NetBSD: ts.c,v 1.2.6.1 2001/10/10 11:56:59 fvdl Exp $ */
      2      1.1  ragge 
      3      1.1  ragge /*-
      4      1.1  ragge  * Copyright (c) 1991 The Regents of the University of California.
      5      1.1  ragge  * All rights reserved.
      6      1.1  ragge  *
      7      1.1  ragge  * Redistribution and use in source and binary forms, with or without
      8      1.1  ragge  * modification, are permitted provided that the following conditions
      9      1.1  ragge  * are met:
     10      1.1  ragge  * 1. Redistributions of source code must retain the above copyright
     11      1.1  ragge  *    notice, this list of conditions and the following disclaimer.
     12      1.1  ragge  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1  ragge  *    notice, this list of conditions and the following disclaimer in the
     14      1.1  ragge  *    documentation and/or other materials provided with the distribution.
     15      1.1  ragge  * 3. All advertising materials mentioning features or use of this software
     16      1.1  ragge  *    must display the following acknowledgement:
     17      1.1  ragge  *	This product includes software developed by the University of
     18      1.1  ragge  *	California, Berkeley and its contributors.
     19      1.1  ragge  * 4. Neither the name of the University nor the names of its contributors
     20      1.1  ragge  *    may be used to endorse or promote products derived from this software
     21      1.1  ragge  *    without specific prior written permission.
     22      1.1  ragge  *
     23      1.1  ragge  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24      1.1  ragge  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25      1.1  ragge  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26      1.1  ragge  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27      1.1  ragge  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28      1.1  ragge  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29      1.1  ragge  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30      1.1  ragge  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31      1.1  ragge  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32      1.1  ragge  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33      1.1  ragge  * SUCH DAMAGE.
     34      1.1  ragge  *
     35      1.1  ragge  *	@(#)tmscp.c	7.16 (Berkeley) 5/9/91
     36      1.1  ragge  */
     37      1.1  ragge 
     38      1.1  ragge /*
     39      1.1  ragge  * sccsid = "@(#)tmscp.c	1.24	(ULTRIX)	1/21/86";
     40      1.1  ragge  */
     41      1.1  ragge 
     42      1.1  ragge /************************************************************************
     43      1.1  ragge  *									*
     44      1.1  ragge  *	  Licensed from Digital Equipment Corporation			*
     45      1.1  ragge  *			 Copyright (c)					*
     46      1.1  ragge  *		 Digital Equipment Corporation				*
     47      1.1  ragge  *		     Maynard, Massachusetts				*
     48      1.1  ragge  *			   1985, 1986					*
     49      1.1  ragge  *		      All rights reserved.				*
     50      1.1  ragge  *									*
     51      1.1  ragge  *	  The Information in this software is subject to change		*
     52      1.1  ragge  *   without notice and should not be construed as a commitment		*
     53      1.1  ragge  *   by	 Digital  Equipment  Corporation.   Digital   makes  no		*
     54      1.1  ragge  *   representations about the suitability of this software for		*
     55      1.1  ragge  *   any purpose.  It is supplied "As Is" without expressed  or		*
     56      1.1  ragge  *   implied  warranty.							*
     57      1.1  ragge  *									*
     58      1.1  ragge  *	  If the Regents of the University of California or its		*
     59      1.1  ragge  *   licensees modify the software in a manner creating			*
     60      1.1  ragge  *   derivative copyright rights, appropriate copyright			*
     61      1.1  ragge  *   legends may be placed on  the derivative work in addition		*
     62      1.1  ragge  *   to that set forth above.						*
     63      1.1  ragge  *									*
     64      1.1  ragge  ************************************************************************/
     65      1.1  ragge 
     66      1.1  ragge /*
     67      1.1  ragge  * TSV05/TS05 device driver, written by Bertram Barth.
     68      1.1  ragge  *
     69      1.1  ragge  * should be TS11 compatible (untested)
     70      1.1  ragge  */
     71      1.1  ragge 
     72      1.2  ragge #undef	TSDEBUG
     73      1.1  ragge 
     74      1.1  ragge /*
     75      1.2  ragge  * TODO:
     76      1.1  ragge  *
     77      1.2  ragge  * Keep track of tape position so that lseek et al works.
     78      1.2  ragge  * Use tprintf to inform the user, not the system console.
     79      1.1  ragge  */
     80      1.1  ragge 
     81      1.1  ragge 
     82      1.1  ragge #include <sys/param.h>
     83      1.1  ragge #include <sys/systm.h>
     84      1.1  ragge #include <sys/kernel.h>
     85      1.1  ragge #include <sys/buf.h>
     86      1.1  ragge #include <sys/conf.h>
     87      1.1  ragge #include <sys/errno.h>
     88      1.1  ragge #include <sys/file.h>
     89      1.1  ragge #include <sys/map.h>
     90      1.1  ragge #include <sys/syslog.h>
     91      1.1  ragge #include <sys/ioctl.h>
     92      1.1  ragge #include <sys/mtio.h>
     93      1.1  ragge #include <sys/uio.h>
     94      1.1  ragge #include <sys/proc.h>
     95  1.2.6.1   fvdl #include <sys/vnode.h>
     96      1.1  ragge 
     97      1.2  ragge #include <machine/bus.h>
     98      1.1  ragge 
     99      1.1  ragge #include <dev/qbus/ubareg.h>
    100      1.1  ragge #include <dev/qbus/ubavar.h>
    101      1.1  ragge 
    102      1.2  ragge #include <dev/qbus/tsreg.h>
    103      1.1  ragge 
    104      1.2  ragge #include "ioconf.h"
    105      1.1  ragge 
    106      1.1  ragge struct	ts {
    107      1.2  ragge 	struct	cmd cmd;	/* command packet */
    108      1.2  ragge 	struct	chr chr;	/* characteristics packet */
    109      1.2  ragge 	struct	status status;	/* status packet */
    110      1.2  ragge };
    111      1.1  ragge 
    112      1.1  ragge /*
    113      1.1  ragge  * Software status, per controller.
    114      1.1  ragge  * also status per tape-unit, since only one unit per controller
    115      1.1  ragge  * (thus we have no struct ts_info)
    116      1.1  ragge  */
    117      1.1  ragge struct	ts_softc {
    118      1.1  ragge 	struct	device sc_dev;		/* Autoconf ... */
    119      1.1  ragge 	struct	uba_unit sc_unit;	/* Struct common for UBA to talk */
    120      1.2  ragge 	struct	evcnt sc_intrcnt;	/* Interrupt counting */
    121      1.2  ragge 	struct	ubinfo sc_ui;		/* mapping info for struct ts */
    122      1.2  ragge 	struct	uba_unit sc_uu;		/* Struct for UBA to communicate */
    123      1.2  ragge 	bus_space_tag_t sc_iot;
    124      1.2  ragge 	bus_addr_t sc_ioh;
    125      1.2  ragge 	bus_dma_tag_t sc_dmat;
    126      1.2  ragge 	bus_dmamap_t sc_dmam;
    127      1.2  ragge 	volatile struct ts *sc_vts;	/* Memory address of ts struct */
    128      1.2  ragge 	struct	ts *sc_bts;		/* Unibus address of ts struct */
    129      1.2  ragge 	int	sc_type;		/* TS11 or TS05? */
    130      1.2  ragge 	short	sc_waddr;		/* Value to write to TSDB */
    131      1.2  ragge 	struct	buf_queue sc_bufq;	/* pending I/O requests */
    132      1.2  ragge 
    133      1.1  ragge 	short	sc_mapped;		/* Unibus map allocated ? */
    134      1.1  ragge 	short	sc_state;		/* see below: ST_xxx */
    135      1.1  ragge 	short	sc_rtc;			/* retry count for lcmd */
    136      1.1  ragge 	short	sc_openf;		/* lock against multiple opens */
    137      1.1  ragge 	short	sc_liowf;		/* last operation was write */
    138      1.2  ragge 	struct	buf ts_cbuf;		/* internal cmd buffer (for ioctls) */
    139      1.1  ragge };
    140      1.1  ragge 
    141      1.2  ragge #define	XNAME	sc->sc_dev.dv_xname
    142      1.2  ragge 
    143      1.2  ragge #define	TS_WCSR(csr, val) \
    144      1.2  ragge 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, csr, val)
    145      1.2  ragge #define TS_RCSR(csr) \
    146      1.2  ragge 	bus_space_read_2(sc->sc_iot, sc->sc_ioh, csr)
    147      1.2  ragge 
    148      1.2  ragge #define LOWORD(x)	((int)(x) & 0xffff)
    149      1.2  ragge #define HIWORD(x)	(((int)(x) >> 16) & 0x3f)
    150      1.2  ragge 
    151      1.2  ragge #define	TYPE_TS11	0
    152      1.2  ragge #define	TYPE_TS05	1
    153      1.2  ragge #define	TYPE_TU80	2
    154      1.2  ragge 
    155      1.2  ragge #define	TS_INVALID	0
    156      1.2  ragge #define	TS_INIT		1
    157      1.2  ragge #define	TS_RUNNING	2
    158      1.2  ragge #define	TS_FASTREPOS	3
    159      1.2  ragge 
    160      1.2  ragge static	void tsintr(void *);
    161      1.2  ragge static	void tsinit(struct ts_softc *);
    162  1.2.6.1   fvdl static	void tscommand(struct ts_softc *, struct vnode *, int, int);
    163      1.2  ragge static	int tsstart(struct ts_softc *, int);
    164      1.2  ragge static	void tswchar(struct ts_softc *);
    165      1.2  ragge static	int tsreset(struct ts_softc *);
    166      1.2  ragge static	int tsmatch(struct device *, struct cfdata *, void *);
    167      1.2  ragge static	void tsattach(struct device *, struct device *, void *);
    168      1.2  ragge static	int tsready(struct uba_unit *);
    169      1.2  ragge 
    170      1.2  ragge cdev_decl(ts);
    171      1.2  ragge bdev_decl(ts);
    172      1.1  ragge 
    173      1.1  ragge struct	cfattach ts_ca = {
    174      1.1  ragge 	sizeof(struct ts_softc), tsmatch, tsattach
    175      1.1  ragge };
    176      1.1  ragge 
    177      1.1  ragge /* Bits in minor device */
    178      1.1  ragge #define TS_UNIT(dev)	(minor(dev)&03)
    179      1.1  ragge #define TS_HIDENSITY	010
    180      1.1  ragge 
    181      1.1  ragge #define TS_PRI	LOG_INFO
    182      1.1  ragge 
    183      1.1  ragge 
    184      1.1  ragge /*
    185      1.2  ragge  * Probe for device. If found, try to raise an interrupt.
    186      1.1  ragge  */
    187      1.2  ragge int
    188      1.2  ragge tsmatch(struct device *parent, struct cfdata *match, void *aux)
    189      1.2  ragge {
    190      1.2  ragge 	struct ts_softc ssc;
    191      1.2  ragge 	struct ts_softc *sc = &ssc;
    192      1.2  ragge 	struct uba_attach_args *ua = aux;
    193      1.2  ragge 	int i;
    194      1.1  ragge 
    195      1.2  ragge 	sc->sc_iot = ua->ua_iot;
    196      1.2  ragge 	sc->sc_ioh = ua->ua_ioh;
    197      1.2  ragge 	sc->sc_mapped = 0;
    198      1.2  ragge 	sc->sc_dev.dv_parent = parent;
    199      1.2  ragge 	strcpy(XNAME, "ts");
    200      1.1  ragge 
    201      1.2  ragge 	/* Try to reset the device */
    202      1.2  ragge 	for (i = 0; i < 3; i++)
    203      1.2  ragge 		if (tsreset(sc) == 1)
    204      1.2  ragge 			break;
    205      1.2  ragge 
    206      1.2  ragge 	if (i == 3)
    207      1.2  ragge 		return 0;
    208      1.1  ragge 
    209      1.2  ragge 	tsinit(sc);
    210      1.2  ragge 	tswchar(sc);		/* write charact. to enable interrupts */
    211      1.2  ragge 				/* completion of this will raise the intr. */
    212      1.1  ragge 
    213      1.2  ragge 	DELAY(1000000);		/* Wait for interrupt */
    214      1.2  ragge 	ubmemfree((void *)parent, &sc->sc_ui);
    215      1.2  ragge 	return 1;
    216      1.1  ragge }
    217      1.1  ragge 
    218      1.1  ragge /*
    219      1.1  ragge  */
    220      1.2  ragge void
    221      1.2  ragge tsattach(struct device *parent, struct device *self, void *aux)
    222      1.1  ragge {
    223      1.2  ragge 	struct uba_softc *uh = (void *)parent;
    224      1.2  ragge 	struct ts_softc *sc = (void *)self;
    225      1.2  ragge 	struct uba_attach_args *ua = aux;
    226      1.2  ragge 	int error;
    227      1.2  ragge 	char *t;
    228      1.2  ragge 
    229      1.2  ragge 	sc->sc_iot = ua->ua_iot;
    230      1.2  ragge 	sc->sc_ioh = ua->ua_ioh;
    231      1.2  ragge 	sc->sc_dmat = ua->ua_dmat;
    232      1.2  ragge 
    233      1.2  ragge 	sc->sc_uu.uu_softc = sc;
    234      1.2  ragge 	sc->sc_uu.uu_ready = tsready;
    235      1.1  ragge 
    236      1.2  ragge 	tsinit(sc);	/* reset and map */
    237      1.1  ragge 
    238      1.2  ragge 	if ((error = bus_dmamap_create(sc->sc_dmat, (64*1024), 16, (64*1024),
    239      1.2  ragge 	    0, BUS_DMA_NOWAIT, &sc->sc_dmam)))
    240      1.2  ragge 		return printf(": failed create DMA map %d\n", error);
    241      1.1  ragge 
    242      1.2  ragge 	BUFQ_INIT(&sc->sc_bufq);
    243      1.1  ragge 
    244      1.1  ragge 	/*
    245      1.2  ragge 	 * write the characteristics (again)
    246      1.1  ragge 	 */
    247      1.2  ragge 	sc->sc_state = TS_INIT;		/* tsintr() checks this ... */
    248      1.2  ragge 	tswchar(sc);
    249      1.2  ragge 	if (tsleep(sc, PRIBIO, "tsattach", 100))
    250      1.2  ragge 		return printf(": failed SET CHARACTERISTICS\n");
    251      1.2  ragge 
    252      1.2  ragge 	sc->sc_state = TS_RUNNING;
    253      1.2  ragge 	if (uh->uh_type == UBA_UBA) {
    254      1.2  ragge 		if (sc->sc_vts->status.xst2 & TS_SF_TU80) {
    255      1.2  ragge 			sc->sc_type = TYPE_TU80;
    256      1.2  ragge 			t = "TU80";
    257      1.2  ragge 		} else {
    258      1.2  ragge 			sc->sc_type = TYPE_TS11;
    259      1.2  ragge 			t = "TS11";
    260      1.1  ragge 		}
    261      1.2  ragge 	} else {
    262      1.2  ragge 		sc->sc_type = TYPE_TS05;
    263      1.2  ragge 		t = "TS05";
    264      1.1  ragge 	}
    265      1.1  ragge 
    266      1.2  ragge 	printf("\n%s: %s\n", XNAME, t);
    267      1.2  ragge 	printf("%s: rev %d, extended features %s, transport %s\n",
    268      1.2  ragge 		XNAME, (sc->sc_vts->status.xst2 & TS_SF_MCRL) >> 2,
    269      1.2  ragge 		(sc->sc_vts->status.xst2 & TS_SF_EFES ? "enabled" : "disabled"),
    270      1.2  ragge 		(TS_RCSR(TSSR) & TS_OFL ? "offline" : "online"));
    271      1.2  ragge 
    272      1.2  ragge 	uba_intr_establish(ua->ua_icookie, ua->ua_cvec, tsintr,
    273      1.2  ragge 	    sc, &sc->sc_intrcnt);
    274      1.2  ragge 	evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, ua->ua_evcnt,
    275      1.2  ragge 	    sc->sc_dev.dv_xname, "intr");
    276      1.2  ragge }
    277      1.2  ragge 
    278      1.2  ragge /*
    279      1.2  ragge  * Initialize a TS device. Set up UBA mapping registers,
    280      1.2  ragge  * initialize data structures, what else ???
    281      1.2  ragge  */
    282      1.2  ragge void
    283      1.2  ragge tsinit(struct ts_softc *sc)
    284      1.2  ragge {
    285      1.2  ragge 	if (sc->sc_mapped == 0) {
    286      1.2  ragge 
    287      1.2  ragge 		/*
    288      1.2  ragge 		 * Map the communications area and command and message
    289      1.2  ragge 		 * buffer into Unibus address space.
    290      1.2  ragge 		 */
    291      1.2  ragge 		sc->sc_ui.ui_size = sizeof(struct ts);
    292      1.2  ragge 		if ((ubmemalloc((void *)sc->sc_dev.dv_parent,
    293      1.2  ragge 		    &sc->sc_ui, UBA_CANTWAIT)))
    294      1.2  ragge 			return;
    295      1.2  ragge 		sc->sc_vts = (void *)sc->sc_ui.ui_vaddr;
    296      1.2  ragge 		sc->sc_bts = (void *)sc->sc_ui.ui_baddr;
    297      1.2  ragge 		sc->sc_waddr = sc->sc_ui.ui_baddr |
    298      1.2  ragge 		    ((sc->sc_ui.ui_baddr >> 16) & 3);
    299      1.2  ragge 		sc->sc_mapped = 1;
    300      1.2  ragge 	}
    301      1.2  ragge 	tsreset(sc);
    302      1.1  ragge }
    303      1.1  ragge 
    304      1.1  ragge /*
    305      1.1  ragge  * Execute a (ioctl) command on the tape drive a specified number of times.
    306      1.1  ragge  * This routine sets up a buffer and calls the strategy routine which
    307      1.1  ragge  * issues the command to the controller.
    308      1.1  ragge  */
    309      1.1  ragge void
    310  1.2.6.1   fvdl tscommand(struct ts_softc *sc, struct vnode *devvp, int cmd, int count)
    311      1.1  ragge {
    312      1.2  ragge 	struct buf *bp;
    313      1.2  ragge 	int s;
    314      1.1  ragge 
    315      1.2  ragge #ifdef TSDEBUG
    316      1.2  ragge 	printf("tscommand (%x, %d)\n", cmd, count);
    317      1.2  ragge #endif
    318      1.2  ragge 
    319      1.2  ragge 	bp = &sc->ts_cbuf;
    320      1.1  ragge 
    321      1.1  ragge 	s = splbio();
    322      1.1  ragge 	while (bp->b_flags & B_BUSY) {
    323      1.1  ragge 		/*
    324      1.1  ragge 		 * This special check is because B_BUSY never
    325      1.1  ragge 		 * gets cleared in the non-waiting rewind case. ???
    326      1.1  ragge 		 */
    327      1.1  ragge 		if (bp->b_bcount == 0 && (bp->b_flags & B_DONE))
    328      1.1  ragge 			break;
    329      1.1  ragge 		bp->b_flags |= B_WANTED;
    330      1.1  ragge 		(void) tsleep(bp, PRIBIO, "tscmd", 0);
    331      1.1  ragge 		/* check MOT-flag !!! */
    332      1.1  ragge 	}
    333      1.1  ragge 	bp->b_flags = B_BUSY | B_READ;
    334      1.1  ragge 	splx(s);
    335      1.1  ragge 
    336      1.1  ragge 	/*
    337      1.1  ragge 	 * Load the buffer.  The b_count field gets used to hold the command
    338      1.1  ragge 	 * count.  the b_resid field gets used to hold the command mneumonic.
    339      1.1  ragge 	 * These 2 fields are "known" to be "safe" to use for this purpose.
    340      1.1  ragge 	 * (Most other drivers also use these fields in this way.)
    341      1.1  ragge 	 */
    342  1.2.6.1   fvdl 	bp->b_devvp = devvp;
    343      1.1  ragge 	bp->b_bcount = count;
    344      1.1  ragge 	bp->b_resid = cmd;
    345      1.1  ragge 	bp->b_blkno = 0;
    346      1.2  ragge 	tsstrategy(bp);
    347      1.1  ragge 	/*
    348      1.1  ragge 	 * In case of rewind from close, don't wait.
    349      1.1  ragge 	 * This is the only case where count can be 0.
    350      1.1  ragge 	 */
    351      1.2  ragge 	if (count == 0)
    352      1.1  ragge 		return;
    353      1.2  ragge 	biowait(bp);
    354      1.1  ragge 	if (bp->b_flags & B_WANTED)
    355      1.2  ragge 		wakeup((caddr_t)bp);
    356      1.1  ragge 	bp->b_flags &= B_ERROR;
    357      1.1  ragge }
    358      1.1  ragge 
    359      1.1  ragge /*
    360      1.1  ragge  * Start an I/O operation on TS05 controller
    361      1.1  ragge  */
    362      1.1  ragge int
    363      1.2  ragge tsstart(struct ts_softc *sc, int isloaded)
    364      1.1  ragge {
    365      1.2  ragge 	struct buf *bp;
    366      1.1  ragge 	int cmd;
    367      1.1  ragge 
    368      1.2  ragge 	if (TAILQ_EMPTY(&sc->sc_bufq.bq_head))
    369      1.2  ragge 		return 0;
    370      1.1  ragge 
    371      1.2  ragge 	bp = BUFQ_FIRST(&sc->sc_bufq);
    372      1.2  ragge #ifdef TSDEBUG
    373      1.2  ragge 	printf("buf: %p bcount %ld blkno %d\n", bp, bp->b_bcount, bp->b_blkno);
    374      1.2  ragge #endif
    375      1.1  ragge 	/*
    376      1.1  ragge 	 * Check if command is an ioctl or not (ie. read or write).
    377      1.1  ragge 	 * If it's an ioctl then just set the flags for later use;
    378      1.1  ragge 	 * For other commands attempt to setup a buffer pointer.
    379      1.1  ragge 	 */
    380      1.2  ragge 	if (bp == &sc->ts_cbuf) {
    381      1.1  ragge 		switch ((int)bp->b_resid) {
    382      1.1  ragge 		case MTWEOF:
    383      1.1  ragge 			cmd = TS_CMD_WTM;
    384      1.1  ragge 			break;
    385      1.1  ragge 		case MTFSF:
    386      1.1  ragge 			cmd = TS_CMD_STMF;
    387      1.2  ragge 			sc->sc_vts->cmd.cw1 = bp->b_bcount;
    388      1.1  ragge 			break;
    389      1.1  ragge 		case MTBSF:
    390      1.1  ragge 			cmd = TS_CMD_STMR;
    391      1.2  ragge 			sc->sc_vts->cmd.cw1 = bp->b_bcount;
    392      1.1  ragge 			break;
    393      1.1  ragge 		case MTFSR:
    394      1.1  ragge 			cmd = TS_CMD_SRF;
    395      1.2  ragge 			sc->sc_vts->cmd.cw1 = bp->b_bcount;
    396      1.1  ragge 			break;
    397      1.1  ragge 		case MTBSR:
    398      1.1  ragge 			cmd = TS_CMD_SRR;
    399      1.2  ragge 			sc->sc_vts->cmd.cw1 = bp->b_bcount;
    400      1.1  ragge 			break;
    401      1.1  ragge 		case MTREW:
    402      1.1  ragge 			cmd = TS_CMD_RWND;
    403      1.1  ragge 			break;
    404      1.1  ragge 		case MTOFFL:
    405      1.1  ragge 			cmd = TS_CMD_RWUL;
    406      1.1  ragge 			break;
    407      1.1  ragge 		case MTNOP:
    408      1.1  ragge 			cmd = TS_CMD_STAT;
    409      1.1  ragge 			break;
    410      1.1  ragge 		default:
    411      1.1  ragge 			printf ("%s: bad ioctl %d\n", sc->sc_dev.dv_xname,
    412      1.1  ragge 				(int)bp->b_resid);
    413      1.1  ragge 			/* Need a no-op. get status */
    414      1.1  ragge 			cmd = TS_CMD_STAT;
    415      1.1  ragge 		} /* end switch (bp->b_resid) */
    416      1.2  ragge 	} else {
    417      1.2  ragge 		if (isloaded == 0) {
    418      1.2  ragge 			/*
    419      1.2  ragge 			 * now we try to map the buffer into uba map space (???)
    420      1.2  ragge 			 */
    421      1.2  ragge 			if (bus_dmamap_load(sc->sc_dmat, sc->sc_dmam,
    422      1.2  ragge 			    bp->b_data,
    423      1.2  ragge 			    bp->b_bcount, bp->b_proc, BUS_DMA_NOWAIT)) {
    424      1.2  ragge 				uba_enqueue(&sc->sc_uu);
    425      1.2  ragge 				return 0;
    426      1.2  ragge 			}
    427      1.2  ragge 			sc->sc_rtc = 0;
    428      1.2  ragge 		}
    429      1.2  ragge 		sc->sc_vts->cmd.cw1 = LOWORD(sc->sc_dmam->dm_segs[0].ds_addr);
    430      1.2  ragge 		sc->sc_vts->cmd.cw2 = HIWORD(sc->sc_dmam->dm_segs[0].ds_addr);
    431      1.2  ragge 		sc->sc_vts->cmd.cw3 = bp->b_bcount;
    432      1.2  ragge 		bp->b_error = 0; /* Used for error count */
    433      1.2  ragge #ifdef TSDEBUG
    434      1.2  ragge 		printf("tsstart-1: err %d\n", bp->b_error);
    435      1.2  ragge #endif
    436      1.2  ragge 		if (bp->b_flags & B_READ)
    437      1.1  ragge 			cmd = TS_CMD_RNF;
    438      1.2  ragge 		else
    439      1.1  ragge 			cmd = TS_CMD_WD;
    440      1.1  ragge 	}
    441      1.1  ragge 
    442      1.1  ragge 	/*
    443      1.1  ragge 	 * Now that the command-buffer is setup, give it to the controller
    444      1.1  ragge 	 */
    445      1.2  ragge 	sc->sc_vts->cmd.cmdr = TS_CF_ACK | TS_CF_IE | cmd;
    446      1.2  ragge #ifdef TSDEBUG
    447      1.2  ragge 	printf("tsstart: sending cmdr %x\n", sc->sc_vts->cmd.cmdr);
    448      1.2  ragge #endif
    449      1.2  ragge 	TS_WCSR(TSDB, sc->sc_waddr);
    450      1.1  ragge }
    451      1.1  ragge 
    452      1.1  ragge /*
    453      1.2  ragge  * Called when there are free uba resources.
    454      1.1  ragge  */
    455      1.2  ragge int
    456      1.2  ragge tsready(struct uba_unit *uu)
    457      1.1  ragge {
    458      1.2  ragge 	struct ts_softc *sc = uu->uu_softc;
    459      1.2  ragge 	struct buf *bp = BUFQ_FIRST(&sc->sc_bufq);
    460      1.1  ragge 
    461      1.2  ragge 	if (bus_dmamap_load(sc->sc_dmat, sc->sc_dmam, bp->b_data,
    462      1.2  ragge 	    bp->b_bcount, bp->b_proc, BUS_DMA_NOWAIT))
    463      1.2  ragge 		return 0;
    464      1.1  ragge 
    465      1.2  ragge 	tsstart(sc, 1);
    466      1.2  ragge 	return 1;
    467      1.1  ragge }
    468      1.1  ragge 
    469      1.1  ragge /*
    470      1.2  ragge  * initialize the controller by sending WRITE CHARACTERISTICS command.
    471      1.2  ragge  * contents of command- and message-buffer are assembled during this
    472      1.2  ragge  * function.
    473      1.1  ragge  */
    474      1.2  ragge void
    475      1.2  ragge tswchar(struct ts_softc *sc)
    476      1.1  ragge {
    477      1.1  ragge 	/*
    478      1.2  ragge 	 * assemble and send "WRITE CHARACTERISTICS" command
    479      1.1  ragge 	 */
    480      1.1  ragge 
    481      1.2  ragge 	sc->sc_vts->cmd.cmdr = TS_CF_ACK | TS_CF_IE | TS_CMD_WCHAR;
    482      1.2  ragge 	sc->sc_vts->cmd.cw1  = LOWORD(&sc->sc_bts->chr);
    483      1.2  ragge 	sc->sc_vts->cmd.cw2  = HIWORD(&sc->sc_bts->chr);
    484      1.2  ragge 	sc->sc_vts->cmd.cw3  = 010;		   /* size of charact.-data */
    485      1.2  ragge 
    486      1.2  ragge 	sc->sc_vts->chr.sadrl = LOWORD(&sc->sc_bts->status);
    487      1.2  ragge 	sc->sc_vts->chr.sadrh = HIWORD(&sc->sc_bts->status);
    488      1.2  ragge 	sc->sc_vts->chr.onesix = (sc->sc_type == TYPE_TS05 ? 020 : 016);
    489      1.2  ragge 	sc->sc_vts->chr.chrw = TS_WC_ESS;
    490      1.2  ragge 	sc->sc_vts->chr.xchrw = TS_WCX_HSP|TS_WCX_RBUF|TS_WCX_WBUF;
    491      1.2  ragge 
    492      1.2  ragge 	TS_WCSR(TSDB, sc->sc_waddr);
    493      1.1  ragge }
    494      1.1  ragge 
    495      1.1  ragge /*
    496      1.2  ragge  * Reset the TS11. Return 1 if failed, 0 if succeeded.
    497      1.1  ragge  */
    498      1.1  ragge int
    499      1.2  ragge tsreset(struct ts_softc *sc)
    500      1.1  ragge {
    501      1.2  ragge 	int timeout;
    502      1.1  ragge 
    503      1.1  ragge 	/*
    504      1.2  ragge 	 * reset ctlr by writing into TSSR, then write characteristics
    505      1.1  ragge 	 */
    506      1.2  ragge 	timeout = 0;		/* timeout in 10 seconds */
    507      1.2  ragge 	TS_WCSR(TSSR, 0);	/* start initialization */
    508      1.2  ragge 	while ((TS_RCSR(TSSR) & TS_SSR) == 0) {
    509      1.1  ragge 		DELAY(10000);
    510      1.2  ragge 		if (timeout++ > 1000)
    511      1.2  ragge 			return 0;
    512      1.2  ragge 	}
    513      1.2  ragge 	return 1;
    514      1.1  ragge }
    515      1.1  ragge 
    516      1.2  ragge static void
    517      1.2  ragge prtstat(struct ts_softc *sc, int sr)
    518      1.1  ragge {
    519      1.2  ragge 	char buf[100];
    520      1.1  ragge 
    521      1.2  ragge 	bitmask_snprintf(sr, TS_TSSR_BITS, buf, sizeof(buf));
    522      1.2  ragge 	printf("%s: TSSR=%s\n", XNAME, buf);
    523      1.2  ragge 	bitmask_snprintf(sc->sc_vts->status.xst0,TS_XST0_BITS,buf,sizeof(buf));
    524      1.2  ragge 	printf("%s: XST0=%s\n", XNAME, buf);
    525      1.1  ragge }
    526      1.1  ragge 
    527      1.1  ragge /*
    528      1.1  ragge  * TSV05/TS05 interrupt routine
    529      1.1  ragge  */
    530      1.2  ragge static void
    531      1.2  ragge tsintr(void *arg)
    532      1.1  ragge {
    533      1.2  ragge 	struct ts_softc *sc = arg;
    534      1.2  ragge 	struct buf *bp;
    535      1.1  ragge 
    536      1.2  ragge 	unsigned short sr = TS_RCSR(TSSR);	/* save TSSR */
    537      1.2  ragge 	unsigned short mh = sc->sc_vts->status.hdr;	/* and msg-header */
    538      1.1  ragge 		/* clear the message header ??? */
    539      1.1  ragge 
    540      1.2  ragge 	short ccode = sc->sc_vts->cmd.cmdr & TS_CF_CCODE;
    541      1.1  ragge 
    542      1.2  ragge #ifdef TSDEBUG
    543      1.1  ragge 	{
    544      1.2  ragge 		char buf[100];
    545      1.2  ragge 		bitmask_snprintf(sr, TS_TSSR_BITS, buf, sizeof(buf));
    546      1.2  ragge 		printf("tsintr: sr %x mh %x\n", sr, mh);
    547      1.2  ragge 		printf("srbits: %s\n", buf);
    548      1.1  ragge 	}
    549      1.1  ragge #endif
    550      1.1  ragge 	/*
    551      1.1  ragge 	 * There are two different things which can (and should) be checked:
    552      1.1  ragge 	 * the actual (internal) state and the device's result (tssr/msg.hdr)
    553      1.1  ragge 	 *
    554      1.1  ragge 	 * For each state there's only one "normal" interrupt. Anything else
    555      1.1  ragge 	 * has to be checked more intensively. Thus in a first run according
    556      1.1  ragge 	 * to the internal state the expected interrupt is checked/handled.
    557      1.1  ragge 	 *
    558      1.1  ragge 	 * In a second run the remaining (not yet handled) interrupts are
    559      1.1  ragge 	 * checked according to the drive's result.
    560      1.1  ragge 	 */
    561      1.1  ragge 	switch (sc->sc_state) {
    562      1.1  ragge 
    563      1.2  ragge 	case TS_INVALID:
    564      1.1  ragge 		/*
    565      1.1  ragge 		 * Ignore unsolicited interrupts.
    566      1.1  ragge 		 */
    567      1.2  ragge 		log(LOG_WARNING, "%s: stray intr [%x,%x]\n",
    568      1.1  ragge 			sc->sc_dev.dv_xname, sr, mh);
    569      1.1  ragge 		return;
    570      1.1  ragge 
    571      1.2  ragge 	case TS_INIT:
    572      1.1  ragge 		/*
    573      1.2  ragge 		 * Init phase ready.
    574      1.1  ragge 		 */
    575      1.2  ragge 		wakeup(sc);
    576      1.1  ragge 		return;
    577      1.1  ragge 
    578      1.2  ragge 	case TS_RUNNING:
    579      1.2  ragge 	case TS_FASTREPOS:
    580      1.1  ragge 		/*
    581      1.1  ragge 		 * Here we expect interrupts indicating the end of
    582      1.1  ragge 		 * commands or indicating problems.
    583      1.1  ragge 		 */
    584      1.1  ragge 		/*
    585      1.1  ragge 		 * Anything else is handled outside this switch ...
    586      1.1  ragge 		 */
    587      1.1  ragge 		break;
    588      1.1  ragge 
    589      1.1  ragge 	default:
    590      1.1  ragge 		printf ("%s: unexpected interrupt during state %d [%x,%x]\n",
    591      1.1  ragge 			sc->sc_dev.dv_xname, sc->sc_state, sr, mh);
    592      1.1  ragge 		return;
    593      1.1  ragge 	}
    594      1.1  ragge 
    595      1.1  ragge 	/*
    596      1.1  ragge 	 * now we check the termination class.
    597      1.1  ragge 	 */
    598      1.1  ragge 	switch (sr & TS_TC) {
    599      1.1  ragge 
    600      1.1  ragge 	case TS_TC_NORM:
    601      1.1  ragge 		/*
    602      1.1  ragge 		 * Normal termination -- The operation is completed
    603      1.1  ragge 		 * witout incident.
    604      1.1  ragge 		 */
    605      1.2  ragge 		if (sc->sc_state == TS_FASTREPOS) {
    606      1.2  ragge #ifdef TSDEBUG
    607      1.2  ragge 			printf("Fast repos interrupt\n");
    608      1.2  ragge #endif
    609      1.2  ragge 			/* Fast repos succeeded, start normal data xfer */
    610      1.2  ragge 			sc->sc_state = TS_RUNNING;
    611      1.2  ragge 			tsstart(sc, 1);
    612      1.2  ragge 			return;
    613      1.2  ragge 		}
    614      1.1  ragge 		sc->sc_liowf = (ccode == TS_CC_WRITE);
    615      1.2  ragge 		break;
    616      1.1  ragge 
    617      1.1  ragge 	case TS_TC_ATTN:
    618      1.1  ragge 		/*
    619      1.1  ragge 		 * Attention condition -- this code indicates that the
    620      1.1  ragge 		 * drive has undergone a status change, such as going
    621      1.1  ragge 		 * off-line or coming on-line.
    622      1.1  ragge 		 * (Without EAI enabled, no Attention interrupts occur.
    623      1.1  ragge 		 * drive status changes are signaled by the VCK flag.)
    624      1.1  ragge 		 */
    625      1.1  ragge 		return;
    626      1.1  ragge 
    627      1.1  ragge 	case TS_TC_TSA:
    628      1.1  ragge 		/*
    629      1.1  ragge 		 * Tape Status Alert -- A status condition is encountered
    630      1.1  ragge 		 * that may have significance to the program. Bits of
    631      1.1  ragge 		 * interest in the extended status registers include
    632      1.1  ragge 		 * TMK, EOT and RLL.
    633      1.1  ragge 		 */
    634      1.2  ragge #ifdef TSDEBUG
    635      1.2  ragge 		{
    636      1.2  ragge 			char buf[100];
    637      1.2  ragge 			bitmask_snprintf(sc->sc_vts->status.xst0,
    638      1.2  ragge 			    TS_XST0_BITS, buf, sizeof(buf));
    639      1.2  ragge 			printf("TSA: sr %x bits %s\n",
    640      1.2  ragge 			    sc->sc_vts->status.xst0, buf);
    641      1.2  ragge 		}
    642      1.2  ragge #endif
    643      1.2  ragge 		if (sc->sc_vts->status.xst0 & TS_SF_TMK) {
    644      1.2  ragge 			/* Read to end-of-file. No error. */
    645      1.2  ragge 			break;
    646      1.2  ragge 		}
    647      1.2  ragge 		if (sc->sc_vts->status.xst0 & TS_SF_EOT) {
    648      1.2  ragge 			/* End of tape. Bad. */
    649      1.2  ragge #ifdef TSDEBUG
    650      1.2  ragge 			printf("TS_TC_TSA: EOT\n");
    651      1.2  ragge #endif
    652      1.2  ragge 			bp->b_flags |= B_ERROR;
    653      1.2  ragge 			bp->b_error = EIO;
    654      1.2  ragge 			break;
    655      1.2  ragge 		}
    656      1.2  ragge 		if (sc->sc_vts->status.xst0 & TS_SF_RLS)
    657      1.2  ragge 			break;
    658      1.2  ragge 		if (sc->sc_vts->status.xst0 & TS_SF_TMK) {
    659      1.2  ragge #ifdef TSDEBUG
    660      1.2  ragge 			printf(("Tape Mark detected"));
    661      1.2  ragge #endif
    662      1.2  ragge 		}
    663      1.2  ragge 		if (sc->sc_vts->status.xst0 & TS_SF_EOT) {
    664      1.2  ragge #ifdef TSDEBUG
    665      1.2  ragge 			printf(("End of Tape"));
    666      1.2  ragge #endif
    667      1.1  ragge 		}
    668      1.2  ragge #ifndef TSDEBUG
    669      1.2  ragge 		{
    670      1.2  ragge 			char buf[100];
    671      1.2  ragge 			bitmask_snprintf(sc->sc_vts->status.xst0,
    672      1.2  ragge 			    TS_XST0_BITS, buf, sizeof(buf));
    673      1.2  ragge 			printf("TSA: sr %x bits %s\n",
    674      1.2  ragge 			    sc->sc_vts->status.xst0, buf);
    675      1.1  ragge 		}
    676      1.2  ragge #endif
    677      1.1  ragge 		break;
    678      1.1  ragge 
    679      1.1  ragge 	case TS_TC_FR:
    680      1.1  ragge 		/*
    681      1.1  ragge 		 * Function Reject -- The specified function was not
    682      1.1  ragge 		 * initiated. Bits of interest include OFL, VCK, BOT,
    683      1.1  ragge 		 * WLE, ILC and ILA.
    684      1.1  ragge 		 */
    685      1.2  ragge 		if (sr & TS_OFL)
    686      1.2  ragge 			printf("tape is off-line.\n");
    687      1.2  ragge #ifdef TSDEBUG
    688      1.2  ragge 		{
    689      1.2  ragge 			char buf[100];
    690      1.2  ragge 			bitmask_snprintf(sc->sc_vts->status.xst0,
    691      1.2  ragge 			    TS_XST0_BITS, buf, sizeof(buf));
    692      1.2  ragge 			printf("FR: sr %x bits %s\n",
    693      1.2  ragge 			    sc->sc_vts->status.xst0, buf);
    694      1.2  ragge 		}
    695      1.2  ragge #endif
    696      1.2  ragge 		if (sc->sc_vts->status.xst0 & TS_SF_VCK)
    697      1.2  ragge 			printf("Volume check\n");
    698      1.2  ragge 		if (sc->sc_vts->status.xst0 & TS_SF_BOT)
    699      1.2  ragge 			printf("Start of tape.\n");
    700      1.2  ragge 		if (sc->sc_vts->status.xst0 & TS_SF_WLE)
    701      1.2  ragge 			printf("Write Lock Error\n");
    702      1.2  ragge 		if (sc->sc_vts->status.xst0 & TS_SF_EOT)
    703      1.2  ragge 			printf("End of tape.\n");
    704      1.1  ragge 		break;
    705      1.1  ragge 
    706      1.1  ragge 	case TS_TC_TPD:
    707      1.1  ragge 		/*
    708      1.1  ragge 		 * Recoverable Error -- Tape position is a record beyond
    709      1.1  ragge 		 * what its position was when the function was initiated.
    710      1.1  ragge 		 * Suggested recovery procedure is to log the error and
    711      1.1  ragge 		 * issue the appropriate retry command.
    712      1.2  ragge 		 *
    713      1.2  ragge 		 * Do a fast repositioning one record back.
    714      1.1  ragge 		 */
    715      1.2  ragge 		sc->sc_state = TS_FASTREPOS;
    716      1.2  ragge #ifdef TSDEBUG
    717      1.2  ragge 		printf("TS_TC_TPD: errcnt %d\n", sc->sc_rtc);
    718      1.2  ragge #endif
    719      1.2  ragge 		if (sc->sc_rtc++ == 8) {
    720      1.2  ragge 			printf("%s: failed 8 retries\n", XNAME);
    721      1.2  ragge 			prtstat(sc, sr);
    722      1.2  ragge 			bp->b_flags |= B_ERROR;
    723      1.2  ragge 			bp->b_error = EIO;
    724      1.2  ragge 			break;
    725      1.1  ragge 		}
    726      1.2  ragge 		sc->sc_vts->cmd.cmdr = TS_CF_ACK | TS_CF_IE | TS_CMD_SRR;
    727      1.2  ragge 		sc->sc_vts->cmd.cw1 = 1;
    728      1.2  ragge 		TS_WCSR(TSDB, sc->sc_waddr);
    729      1.2  ragge 		return;
    730      1.2  ragge 
    731      1.1  ragge 		break;
    732      1.1  ragge 
    733      1.1  ragge 	case TS_TC_TNM:
    734      1.1  ragge 		/*
    735      1.1  ragge 		 * Recoverable Error -- Tape position has not changed.
    736      1.1  ragge 		 * Suggested recovery procedure is to log the error and
    737      1.1  ragge 		 * reissue the original command.
    738      1.1  ragge 		 */
    739      1.2  ragge 		if (sc->sc_rtc++ == 8) {
    740      1.2  ragge 			printf("%s: failed 8 retries\n", XNAME);
    741      1.2  ragge 			prtstat(sc, sr);
    742      1.2  ragge 			bp->b_flags |= B_ERROR;
    743      1.2  ragge 			bp->b_error = EIO;
    744      1.2  ragge 			break;
    745      1.1  ragge 		}
    746      1.2  ragge 		tsstart(sc, 1);
    747      1.2  ragge 		return;
    748      1.1  ragge 
    749      1.1  ragge 	case TS_TC_TPL:
    750      1.1  ragge 		/*
    751      1.1  ragge 		 * Unrecoverable Error -- Tape position has been lost.
    752      1.1  ragge 		 * No valid recovery procedures exist unless the tape
    753      1.1  ragge 		 * has labels or sequence numbers.
    754      1.1  ragge 		 */
    755      1.2  ragge 		printf("Tape position lost\n");
    756      1.2  ragge 		bp->b_flags |= B_ERROR;
    757      1.2  ragge 		bp->b_error = EIO;
    758      1.1  ragge 		break;
    759      1.1  ragge 
    760      1.1  ragge 	case TS_TC_FCE:
    761      1.1  ragge 		/*
    762      1.1  ragge 		 * Fatal subsytem Error -- The subsytem is incapable
    763      1.1  ragge 		 * of properly performing commands, or at least its
    764      1.1  ragge 		 * integrity is seriously questionable. Refer to the
    765      1.1  ragge 		 * fatal class code field in the TSSR register for
    766      1.1  ragge 		 * additional information on the type of fatal error.
    767      1.1  ragge 		 */
    768      1.1  ragge 		printf ("Fatal Controller Error\n");
    769      1.2  ragge 		prtstat(sc, sr);
    770      1.2  ragge 		break;
    771      1.1  ragge 
    772      1.1  ragge 	default:
    773      1.1  ragge 		printf ("%s: error 0x%x, resetting controller\n",
    774      1.1  ragge 			sc->sc_dev.dv_xname, sr & TS_TC);
    775      1.2  ragge 		tsreset(sc);
    776      1.1  ragge 	}
    777      1.2  ragge 	if ((bp = TAILQ_FIRST(&sc->sc_bufq.bq_head)) != NULL) {
    778      1.2  ragge 		BUFQ_REMOVE(&sc->sc_bufq, bp);
    779      1.1  ragge 
    780      1.2  ragge #ifdef TSDEBUG
    781      1.2  ragge 		printf("tsintr2: que %p\n", TAILQ_FIRST(&sc->sc_bufq.bq_head));
    782      1.2  ragge #endif
    783      1.2  ragge 		if (bp != &sc->ts_cbuf) {	/* no ioctl */
    784      1.2  ragge 			bus_dmamap_unload(sc->sc_dmat, sc->sc_dmam);
    785      1.2  ragge 			uba_done((void *)sc->sc_dev.dv_parent);
    786      1.2  ragge 		}
    787      1.2  ragge 		bp->b_resid = sc->sc_vts->status.rbpcr;
    788      1.2  ragge 		if ((bp->b_flags & B_ERROR) == 0)
    789      1.2  ragge 			bp->b_error = 0;
    790      1.1  ragge 		biodone (bp);
    791      1.1  ragge 	}
    792      1.2  ragge 	tsstart(sc, 0);
    793      1.1  ragge }
    794      1.1  ragge 
    795      1.1  ragge 
    796      1.1  ragge /*
    797      1.1  ragge  * Open a ts device and set the unit online.  If the controller is not
    798      1.1  ragge  * in the run state, call init to initialize the ts controller first.
    799      1.1  ragge  */
    800      1.1  ragge int
    801  1.2.6.1   fvdl tsopen(struct vnode *devvp, int flag, int type, struct proc *p)
    802      1.1  ragge {
    803      1.2  ragge 	struct ts_softc *sc;
    804  1.2.6.1   fvdl 	dev_t dev = vdev_rdev(devvp);
    805      1.2  ragge 	int unit = TS_UNIT(dev);
    806      1.1  ragge 
    807      1.1  ragge 	if (unit >= ts_cd.cd_ndevs)
    808      1.1  ragge 		return ENXIO;
    809      1.1  ragge 
    810      1.1  ragge 	sc = ts_cd.cd_devs[unit];
    811      1.1  ragge 	if (sc == 0)
    812      1.1  ragge 		return ENXIO;
    813      1.1  ragge 
    814      1.2  ragge 	if (sc->sc_state < TS_RUNNING)
    815      1.2  ragge 		return ENXIO;
    816      1.2  ragge 
    817      1.1  ragge 	if (sc->sc_openf)
    818      1.1  ragge 		return EBUSY;
    819  1.2.6.1   fvdl 
    820  1.2.6.1   fvdl 	vdev_setprivdata(devvp, sc);
    821  1.2.6.1   fvdl 
    822      1.1  ragge 	sc->sc_openf = 1;
    823      1.1  ragge 
    824      1.1  ragge 	/*
    825      1.1  ragge 	 * check if transport is really online.
    826      1.1  ragge 	 * (without attention-interrupts enabled, we really don't know
    827      1.1  ragge 	 * the actual state of the transport. Thus we call get-status
    828      1.1  ragge 	 * (ie. MTNOP) once and check the actual status.)
    829      1.1  ragge 	 */
    830      1.2  ragge 	if (TS_RCSR(TSSR) & TS_OFL) {
    831      1.2  ragge 		uprintf("%s: transport is offline.\n", XNAME);
    832      1.1  ragge 		sc->sc_openf = 0;
    833      1.1  ragge 		return EIO;		/* transport is offline */
    834      1.1  ragge 	}
    835  1.2.6.1   fvdl 	tscommand(sc, devvp, MTNOP, 1);
    836      1.2  ragge 	if ((flag & FWRITE) && (sc->sc_vts->status.xst0 & TS_SF_WLK)) {
    837      1.2  ragge 		uprintf("%s: no write ring.\n", XNAME);
    838      1.2  ragge 		sc->sc_openf = 0;
    839      1.2  ragge 		return EROFS;
    840      1.2  ragge 	}
    841      1.2  ragge 	if (sc->sc_vts->status.xst0 & TS_SF_VCK) {
    842      1.2  ragge 		sc->sc_vts->cmd.cmdr = TS_CF_CVC|TS_CF_ACK;
    843      1.2  ragge 		TS_WCSR(TSDB, sc->sc_waddr);
    844      1.2  ragge 	}
    845  1.2.6.1   fvdl 	tscommand(sc, devvp, MTNOP, 1);
    846      1.2  ragge #ifdef TSDEBUG
    847      1.2  ragge 	{
    848      1.2  ragge 		char buf[100];
    849      1.2  ragge 		bitmask_snprintf(sc->sc_vts->status.xst0,
    850      1.2  ragge 		    TS_XST0_BITS, buf, sizeof(buf));
    851      1.2  ragge 		printf("tsopen: xst0 %s\n", buf);
    852      1.2  ragge 	}
    853      1.2  ragge #endif
    854      1.1  ragge 	sc->sc_liowf = 0;
    855      1.1  ragge 	return 0;
    856      1.1  ragge }
    857      1.1  ragge 
    858      1.1  ragge 
    859      1.1  ragge /*
    860      1.1  ragge  * Close tape device.
    861      1.1  ragge  *
    862      1.1  ragge  * If tape was open for writing or last operation was
    863      1.1  ragge  * a write, then write two EOF's and backspace over the last one.
    864      1.1  ragge  * Unless this is a non-rewinding special file, rewind the tape.
    865      1.1  ragge  *
    866      1.1  ragge  * Make the tape available to others, by clearing openf flag.
    867      1.1  ragge  */
    868      1.1  ragge int
    869  1.2.6.1   fvdl tsclose(struct vnode *devvp, int flag, int type, struct proc *p)
    870      1.1  ragge {
    871  1.2.6.1   fvdl 	struct ts_softc *sc = vdev_privdata(devvp);
    872      1.1  ragge 
    873      1.1  ragge 	if (flag == FWRITE || ((flag & FWRITE) && sc->sc_liowf)) {
    874      1.1  ragge 		/*
    875      1.1  ragge 		 * We are writing two tape marks (EOT), but place the tape
    876      1.1  ragge 		 * before the second one, so that another write operation
    877      1.1  ragge 		 * will overwrite the second one and leave and EOF-mark.
    878      1.1  ragge 		 */
    879  1.2.6.1   fvdl 		tscommand(sc, devvp, MTWEOF, 1);	/* Write Tape Mark */
    880  1.2.6.1   fvdl 		tscommand(sc, devvp, MTWEOF, 1);	/* Write Tape Mark */
    881  1.2.6.1   fvdl 		tscommand(sc, devvp, MTBSF, 1);	/* Skip Tape Marks Reverse */
    882      1.1  ragge 	}
    883      1.1  ragge 
    884      1.1  ragge 	if ((dev & T_NOREWIND) == 0)
    885  1.2.6.1   fvdl 		tscommand(sc, devvp, MTREW, 0);
    886      1.1  ragge 
    887      1.1  ragge 	sc->sc_openf = 0;
    888      1.1  ragge 	sc->sc_liowf = 0;
    889      1.1  ragge 	return 0;
    890      1.1  ragge }
    891      1.1  ragge 
    892      1.1  ragge 
    893      1.1  ragge /*
    894      1.1  ragge  * Manage buffers and perform block mode read and write operations.
    895      1.1  ragge  */
    896      1.1  ragge void
    897      1.2  ragge tsstrategy(struct buf *bp)
    898      1.1  ragge {
    899  1.2.6.1   fvdl 	struct ts_softc *sc = vdev_privdata(bp->b_devvp);
    900      1.2  ragge 	int s, empty;
    901      1.1  ragge 
    902      1.2  ragge #ifdef TSDEBUG
    903      1.2  ragge 	printf("buf: %p bcount %ld blkno %d\n", bp, bp->b_bcount, bp->b_blkno);
    904      1.2  ragge #endif
    905      1.1  ragge 	s = splbio ();
    906      1.2  ragge 	empty = TAILQ_EMPTY(&sc->sc_bufq.bq_head);
    907      1.2  ragge 	BUFQ_INSERT_TAIL(&sc->sc_bufq, bp);
    908      1.2  ragge 	if (empty)
    909      1.2  ragge 		tsstart(sc, 0);
    910      1.1  ragge 	splx(s);
    911      1.1  ragge }
    912      1.1  ragge 
    913      1.1  ragge 
    914      1.1  ragge /*
    915      1.1  ragge  * Catch ioctl commands, and call the "command" routine to do them.
    916      1.1  ragge  */
    917      1.1  ragge int
    918  1.2.6.1   fvdl tsioctl(devvp, cmd, data, flag, p)
    919  1.2.6.1   fvdl 	struct vnode *devvp;
    920      1.1  ragge 	u_long cmd;
    921      1.1  ragge 	caddr_t data;
    922      1.1  ragge 	int flag;
    923      1.1  ragge 	struct proc *p;
    924      1.1  ragge {
    925      1.2  ragge 	struct buf *bp;
    926      1.2  ragge 	struct ts_softc *sc;
    927      1.2  ragge 	struct mtop *mtop;	/* mag tape cmd op to perform */
    928      1.2  ragge 	struct mtget *mtget;	/* mag tape struct to get info in */
    929      1.2  ragge 	int callcount;		/* number of times to call routine */
    930      1.1  ragge 	int scount;			/* number of files/records to space */
    931      1.1  ragge 	int spaceop = 0;		/* flag for skip/space operation */
    932      1.1  ragge 	int error = 0;
    933      1.1  ragge 
    934      1.2  ragge #ifdef TSDEBUG
    935  1.2.6.1   fvdl 	printf("tsioctl (%x, %lx, %p, %d)\n", vdev_rdev(devvp), cmd, data,
    936  1.2.6.1   fvdl 	    flag);
    937      1.2  ragge #endif
    938      1.2  ragge 
    939  1.2.6.1   fvdl 	sc = vdev_privdata(devvp);
    940      1.2  ragge 	bp = &sc->ts_cbuf;
    941      1.1  ragge 
    942      1.1  ragge 	switch (cmd) {
    943      1.1  ragge 	case MTIOCTOP:			/* do a mag tape op */
    944      1.1  ragge 		mtop = (struct mtop *)data;
    945      1.1  ragge 		switch (mtop->mt_op) {
    946      1.1  ragge 		case MTWEOF:		/* write an end-of-file record */
    947      1.1  ragge 			callcount = mtop->mt_count;
    948      1.1  ragge 			scount = 1;
    949      1.1  ragge 			break;
    950      1.1  ragge 		case MTFSR:		/* forward space record */
    951      1.1  ragge 		case MTBSR:		/* backward space record */
    952      1.1  ragge 			spaceop = 1;
    953      1.2  ragge 		case MTFSF:		/* forward space file */
    954      1.2  ragge 		case MTBSF:		/* backward space file */
    955      1.1  ragge 			callcount = 1;
    956      1.1  ragge 			scount = mtop->mt_count;
    957      1.1  ragge 			break;
    958      1.1  ragge 		case MTREW:		/* rewind */
    959      1.1  ragge 		case MTOFFL:		/* rewind and put the drive offline */
    960      1.1  ragge 		case MTNOP:		/* no operation, sets status only */
    961      1.1  ragge 			callcount = 1;
    962      1.1  ragge 			scount = 1;		/* wait for this rewind */
    963      1.1  ragge 			break;
    964      1.1  ragge 		case MTRETEN:		/* retension */
    965      1.1  ragge 		case MTERASE:		/* erase entire tape */
    966      1.1  ragge 		case MTEOM:		/* forward to end of media */
    967      1.1  ragge 		case MTNBSF:		/* backward space to begin of file */
    968      1.1  ragge 		case MTCACHE:		/* enable controller cache */
    969      1.1  ragge 		case MTNOCACHE:		/* disable controller cache */
    970      1.1  ragge 		case MTSETBSIZ:		/* set block size; 0 for variable */
    971      1.1  ragge 		case MTSETDNSTY:	/* set density code for current mode */
    972      1.2  ragge 			printf("ioctl %d not implemented.\n", mtop->mt_op);
    973      1.1  ragge 			return (ENXIO);
    974      1.1  ragge 		default:
    975      1.2  ragge #ifdef TSDEBUG
    976      1.2  ragge 			printf("invalid ioctl %d\n", mtop->mt_op);
    977      1.2  ragge #endif
    978      1.1  ragge 			return (ENXIO);
    979      1.1  ragge 		}	/* switch (mtop->mt_op) */
    980      1.1  ragge 
    981      1.1  ragge 		if (callcount <= 0 || scount <= 0) {
    982      1.2  ragge #ifdef TSDEBUG
    983      1.2  ragge 			printf("invalid values %d/%d\n", callcount, scount);
    984      1.2  ragge #endif
    985      1.1  ragge 			return (EINVAL);
    986      1.1  ragge 		}
    987      1.1  ragge 		do {
    988      1.2  ragge 			tscommand(sc, dev, mtop->mt_op, scount);
    989      1.1  ragge 			if (spaceop && bp->b_resid) {
    990      1.2  ragge #ifdef TSDEBUG
    991      1.2  ragge 				printf(("spaceop didn't complete\n"));
    992      1.2  ragge #endif
    993      1.1  ragge 				return (EIO);
    994      1.1  ragge 			}
    995      1.1  ragge 			if (bp->b_flags & B_ERROR) {
    996      1.2  ragge #ifdef TSDEBUG
    997      1.2  ragge 				printf("error in ioctl %d\n", mtop->mt_op);
    998      1.2  ragge #endif
    999      1.1  ragge 				break;
   1000      1.1  ragge 			}
   1001      1.1  ragge 		} while (--callcount > 0);
   1002      1.1  ragge 		if (bp->b_flags & B_ERROR)
   1003      1.1  ragge 			if ((error = bp->b_error) == 0)
   1004      1.1  ragge 				return (EIO);
   1005      1.1  ragge 		return (error);
   1006      1.1  ragge 
   1007      1.1  ragge 	case MTIOCGET:			/* get tape status */
   1008      1.1  ragge 		mtget = (struct mtget *)data;
   1009      1.1  ragge 		mtget->mt_type = MT_ISTS;
   1010      1.2  ragge 		mtget->mt_dsreg = TS_RCSR(TSSR);
   1011      1.2  ragge 		mtget->mt_erreg = sc->sc_vts->status.xst0;
   1012      1.1  ragge 		mtget->mt_resid = 0;		/* ??? */
   1013      1.1  ragge 		mtget->mt_density = 0;		/* ??? */
   1014      1.1  ragge 		break;
   1015      1.1  ragge 
   1016      1.1  ragge 	case MTIOCIEOT:			/* ignore EOT error */
   1017      1.2  ragge #ifdef TSDEBUG
   1018      1.2  ragge 		printf(("MTIOCIEOT not implemented.\n"));
   1019      1.2  ragge #endif
   1020      1.1  ragge 		return (ENXIO);
   1021      1.1  ragge 
   1022      1.1  ragge 	case MTIOCEEOT:			/* enable EOT error */
   1023      1.2  ragge #ifdef TSDEBUG
   1024      1.2  ragge 		printf(("MTIOCEEOT not implemented.\n"));
   1025      1.2  ragge #endif
   1026      1.1  ragge 		return (ENXIO);
   1027      1.1  ragge 
   1028      1.1  ragge 	default:
   1029      1.2  ragge #ifdef TSDEBUG
   1030      1.2  ragge 		printf("invalid ioctl cmd 0x%lx\n", cmd);
   1031      1.2  ragge #endif
   1032      1.1  ragge 		return (ENXIO);
   1033      1.1  ragge 	}
   1034      1.1  ragge 
   1035      1.1  ragge 	return (0);
   1036      1.1  ragge }
   1037      1.1  ragge 
   1038      1.1  ragge 
   1039      1.1  ragge /*
   1040      1.1  ragge  *
   1041      1.1  ragge  */
   1042      1.1  ragge int
   1043  1.2.6.1   fvdl tsread(struct vnode *devvp, struct uio *uio, int flag)
   1044      1.1  ragge {
   1045  1.2.6.1   fvdl 	return (physio (tsstrategy, NULL, devvp, B_READ, minphys, uio));
   1046      1.1  ragge }
   1047      1.1  ragge 
   1048      1.1  ragge /*
   1049      1.1  ragge  *
   1050      1.1  ragge  */
   1051      1.1  ragge int
   1052  1.2.6.1   fvdl tswrite(struct vnode *devvp, struct uio *uio, int flag)
   1053      1.1  ragge {
   1054  1.2.6.1   fvdl 	return (physio (tsstrategy, NULL, devvp, B_WRITE, minphys, uio));
   1055      1.1  ragge }
   1056      1.1  ragge 
   1057      1.1  ragge /*
   1058      1.1  ragge  *
   1059      1.1  ragge  */
   1060      1.1  ragge int
   1061      1.1  ragge tsdump(dev, blkno, va, size)
   1062      1.1  ragge 	dev_t dev;
   1063      1.1  ragge 	daddr_t blkno;
   1064      1.1  ragge 	caddr_t va;
   1065      1.1  ragge 	size_t size;
   1066      1.1  ragge {
   1067      1.2  ragge 	return EIO;
   1068      1.1  ragge }
   1069