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