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