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