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