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