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