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