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