Home | History | Annotate | Line # | Download | only in isa
wt.c revision 1.37.4.1
      1  1.37.4.1   thorpej /*	$NetBSD: wt.c,v 1.37.4.1 1997/08/23 07:13:35 thorpej Exp $	*/
      2      1.18       cgd 
      3      1.10   mycroft /*
      4      1.10   mycroft  * Streamer tape driver.
      5      1.10   mycroft  * Supports Archive and Wangtek compatible QIC-02/QIC-36 boards.
      6      1.10   mycroft  *
      7      1.10   mycroft  * Copyright (C) 1993 by:
      8      1.10   mycroft  *	Sergey Ryzhkov <sir (at) kiae.su>
      9      1.10   mycroft  *	Serge Vakulenko <vak (at) zebub.msk.su>
     10      1.10   mycroft  *
     11      1.10   mycroft  * This software is distributed with NO WARRANTIES, not even the implied
     12      1.10   mycroft  * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
     13       1.1       cgd  *
     14      1.10   mycroft  * Authors grant any other persons or organisations permission to use
     15      1.10   mycroft  * or modify this software as long as this message is kept with the software,
     16      1.10   mycroft  * all derivative works or modified versions.
     17       1.1       cgd  *
     18      1.10   mycroft  * This driver is derived from the old 386bsd Wangtek streamer tape driver,
     19      1.10   mycroft  * made by Robert Baron at CMU, based on Intel sources.
     20       1.1       cgd  */
     21       1.1       cgd 
     22       1.1       cgd /*
     23       1.1       cgd  * Copyright (c) 1989 Carnegie-Mellon University.
     24       1.1       cgd  * All rights reserved.
     25       1.1       cgd  *
     26       1.1       cgd  * Authors: Robert Baron
     27       1.1       cgd  *
     28       1.1       cgd  * Permission to use, copy, modify and distribute this software and
     29       1.1       cgd  * its documentation is hereby granted, provided that both the copyright
     30       1.1       cgd  * notice and this permission notice appear in all copies of the
     31       1.1       cgd  * software, derivative works or modified versions, and any portions
     32       1.1       cgd  * thereof, and that both notices appear in supporting documentation.
     33       1.1       cgd  *
     34       1.1       cgd  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     35       1.1       cgd  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     36       1.1       cgd  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     37       1.1       cgd  *
     38       1.1       cgd  * Carnegie Mellon requests users of this software to return to
     39       1.1       cgd  *
     40       1.1       cgd  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     41       1.1       cgd  *  School of Computer Science
     42       1.1       cgd  *  Carnegie Mellon University
     43       1.1       cgd  *  Pittsburgh PA 15213-3890
     44       1.1       cgd  *
     45       1.1       cgd  * any improvements or extensions that they make and grant Carnegie the
     46       1.1       cgd  * rights to redistribute these changes.
     47       1.1       cgd  */
     48       1.1       cgd 
     49       1.1       cgd /*
     50       1.1       cgd  *  Copyright 1988, 1989 by Intel Corporation
     51       1.1       cgd  */
     52       1.1       cgd 
     53       1.6   mycroft #include <sys/param.h>
     54      1.10   mycroft #include <sys/systm.h>
     55      1.10   mycroft #include <sys/kernel.h>
     56       1.6   mycroft #include <sys/buf.h>
     57      1.10   mycroft #include <sys/fcntl.h>
     58      1.10   mycroft #include <sys/malloc.h>
     59      1.10   mycroft #include <sys/ioctl.h>
     60      1.10   mycroft #include <sys/mtio.h>
     61      1.10   mycroft #include <sys/device.h>
     62      1.32  christos #include <sys/proc.h>
     63      1.32  christos #include <sys/conf.h>
     64      1.10   mycroft 
     65      1.10   mycroft #include <vm/vm_param.h>
     66       1.6   mycroft 
     67      1.33   mycroft #include <machine/intr.h>
     68      1.37   thorpej #include <machine/bus.h>
     69       1.6   mycroft #include <machine/pio.h>
     70       1.6   mycroft 
     71      1.24       cgd #include <dev/isa/isavar.h>
     72      1.24       cgd #include <dev/isa/isadmavar.h>
     73      1.23       cgd #include <dev/isa/wtreg.h>
     74       1.1       cgd 
     75      1.10   mycroft /*
     76      1.10   mycroft  * Uncomment this to enable internal device tracing.
     77      1.10   mycroft  */
     78      1.36  christos #define WTDBPRINT(x)		/* printf x */
     79       1.1       cgd 
     80      1.10   mycroft #define WTPRI			(PZERO+10)	/* sleep priority */
     81       1.1       cgd 
     82       1.1       cgd /*
     83      1.10   mycroft  * Wangtek controller ports
     84       1.1       cgd  */
     85  1.37.4.1   thorpej #define WT_CTLPORT		(ia->ia_iobase + 0 )	/* control, write only */
     86  1.37.4.1   thorpej #define WT_STATPORT		(ia->ia_iobase + 0 )	/* status, read only */
     87  1.37.4.1   thorpej #define WT_CMDPORT		(ia->ia_iobase + 1 )	/* command, write only */
     88  1.37.4.1   thorpej #define WT_DATAPORT		(ia->ia_iobase + 1 )	/* data, read only */
     89      1.10   mycroft #define WT_NPORT		2		/* 2 i/o ports */
     90      1.10   mycroft 
     91      1.10   mycroft /* status port bits */
     92      1.10   mycroft #define WT_BUSY			0x01		/* not ready bit define */
     93      1.10   mycroft #define WT_NOEXCEP		0x02		/* no exception bit define */
     94      1.10   mycroft #define WT_RESETMASK		0x07		/* to check after reset */
     95      1.10   mycroft #define WT_RESETVAL		0x05		/* state after reset */
     96      1.10   mycroft 
     97      1.10   mycroft /* control port bits */
     98      1.10   mycroft #define WT_ONLINE		0x01		/* device selected */
     99      1.10   mycroft #define WT_RESET		0x02		/* reset command */
    100      1.10   mycroft #define WT_REQUEST		0x04		/* request command */
    101      1.10   mycroft #define WT_IEN			0x08		/* enable dma */
    102       1.1       cgd 
    103      1.10   mycroft /*
    104      1.10   mycroft  * Archive controller ports
    105      1.10   mycroft  */
    106  1.37.4.1   thorpej #define AV_DATAPORT		(ia->ia_iobase + 0)	/* data, read only */
    107  1.37.4.1   thorpej #define AV_CMDPORT		(ia->ia_iobase + 0)	/* command, write only */
    108  1.37.4.1   thorpej #define AV_STATPORT		(ia->ia_iobase + 1)	/* status, read only */
    109  1.37.4.1   thorpej #define AV_CTLPORT		(ia->ia_iobase + 1)	/* control, write only */
    110  1.37.4.1   thorpej #define AV_SDMAPORT		(ia->ia_iobase + 2)	/* start dma */
    111  1.37.4.1   thorpej #define AV_RDMAPORT		(ia->ia_iobase + 3)	/* reset dma */
    112      1.10   mycroft #define AV_NPORT		4		/* 4 i/o ports */
    113      1.10   mycroft 
    114      1.10   mycroft /* status port bits */
    115      1.10   mycroft #define AV_BUSY			0x40		/* not ready bit define */
    116      1.10   mycroft #define AV_NOEXCEP		0x20		/* no exception bit define */
    117      1.10   mycroft #define AV_RESETMASK		0xf8		/* to check after reset */
    118      1.10   mycroft #define AV_RESETVAL		0x50		/* state after reset */
    119      1.10   mycroft 
    120      1.10   mycroft /* control port bits */
    121      1.10   mycroft #define AV_RESET		0x80		/* reset command */
    122      1.10   mycroft #define AV_REQUEST		0x40		/* request command */
    123      1.10   mycroft #define AV_IEN			0x20		/* enable interrupts */
    124      1.10   mycroft 
    125      1.10   mycroft enum wttype {
    126      1.10   mycroft 	UNKNOWN = 0,	/* unknown type, driver disabled */
    127      1.10   mycroft 	ARCHIVE,	/* Archive Viper SC499, SC402 etc */
    128      1.10   mycroft 	WANGTEK,	/* Wangtek */
    129      1.10   mycroft };
    130       1.1       cgd 
    131      1.10   mycroft struct wt_softc {
    132      1.10   mycroft 	struct device sc_dev;
    133      1.24       cgd 	void *sc_ih;
    134       1.1       cgd 
    135  1.37.4.1   thorpej 	bus_space_tag_t		sc_iot;
    136  1.37.4.1   thorpej 	bus_space_handle_t	sc_ioh;
    137  1.37.4.1   thorpej 
    138      1.10   mycroft 	enum wttype type;	/* type of controller */
    139      1.21   mycroft 	int chan;		/* dma channel number, 1..3 */
    140      1.10   mycroft 	int flags;		/* state of tape drive */
    141      1.10   mycroft 	unsigned dens;		/* tape density */
    142      1.10   mycroft 	int bsize;		/* tape block size */
    143      1.10   mycroft 	void *buf;		/* internal i/o buffer */
    144      1.10   mycroft 
    145      1.10   mycroft 	void *dmavaddr;		/* virtual address of dma i/o buffer */
    146      1.10   mycroft 	size_t dmatotal;	/* size of i/o buffer */
    147      1.29   mycroft 	int dmaflags;		/* i/o direction */
    148      1.10   mycroft 	size_t dmacount;	/* resulting length of dma i/o */
    149      1.10   mycroft 
    150      1.10   mycroft 	u_short error;		/* code for error encountered */
    151      1.10   mycroft 	u_short ercnt;		/* number of error blocks */
    152      1.10   mycroft 	u_short urcnt;		/* number of underruns */
    153       1.1       cgd 
    154      1.21   mycroft 	int DATAPORT, CMDPORT, STATPORT, CTLPORT, SDMAPORT, RDMAPORT;
    155      1.10   mycroft 	u_char BUSY, NOEXCEP, RESETMASK, RESETVAL, ONLINE, RESET, REQUEST, IEN;
    156      1.10   mycroft };
    157       1.1       cgd 
    158      1.32  christos /* XXX: These don't belong here really */
    159      1.32  christos cdev_decl(wt);
    160      1.32  christos bdev_decl(wt);
    161      1.32  christos 
    162      1.10   mycroft int wtwait __P((struct wt_softc *sc, int catch, char *msg));
    163      1.10   mycroft int wtcmd __P((struct wt_softc *sc, int cmd));
    164      1.10   mycroft int wtstart __P((struct wt_softc *sc, int flag, void *vaddr, size_t len));
    165      1.10   mycroft void wtdma __P((struct wt_softc *sc));
    166      1.14       cgd void wttimer __P((void *arg));
    167      1.10   mycroft void wtclock __P((struct wt_softc *sc));
    168      1.10   mycroft int wtreset __P((struct wt_softc *sc));
    169      1.10   mycroft int wtsense __P((struct wt_softc *sc, int verbose, int ignore));
    170      1.10   mycroft int wtstatus __P((struct wt_softc *sc));
    171      1.10   mycroft void wtrewind __P((struct wt_softc *sc));
    172      1.10   mycroft int wtreadfm __P((struct wt_softc *sc));
    173      1.10   mycroft int wtwritefm __P((struct wt_softc *sc));
    174      1.34   mycroft u_char wtsoft __P((struct wt_softc *sc, int mask, int bits));
    175       1.1       cgd 
    176      1.20   mycroft int wtprobe __P((struct device *, void *, void *));
    177      1.20   mycroft void wtattach __P((struct device *, struct device *, void *));
    178      1.24       cgd int wtintr __P((void *sc));
    179       1.1       cgd 
    180      1.30   thorpej struct cfattach wt_ca = {
    181      1.30   thorpej 	sizeof(struct wt_softc), wtprobe, wtattach
    182      1.30   thorpej };
    183      1.30   thorpej 
    184      1.30   thorpej struct cfdriver wt_cd = {
    185      1.30   thorpej 	NULL, "wt", DV_TAPE
    186      1.10   mycroft };
    187       1.1       cgd 
    188      1.10   mycroft /*
    189      1.10   mycroft  * Probe for the presence of the device.
    190      1.10   mycroft  */
    191      1.10   mycroft int
    192      1.20   mycroft wtprobe(parent, match, aux)
    193      1.20   mycroft 	struct device *parent;
    194      1.20   mycroft 	void *match, *aux;
    195      1.10   mycroft {
    196      1.20   mycroft 	struct wt_softc *sc = match;
    197      1.10   mycroft 	struct isa_attach_args *ia = aux;
    198  1.37.4.1   thorpej 	bus_space_tag_t iot = ia->ia_iot;
    199  1.37.4.1   thorpej 	bus_space_handle_t ioh;
    200  1.37.4.1   thorpej 	int rv = 0;
    201  1.37.4.1   thorpej 
    202  1.37.4.1   thorpej 
    203  1.37.4.1   thorpej 	/* Map i/o space */
    204  1.37.4.1   thorpej 	if (bus_space_map(iot, ia->ia_iobase, AV_NPORT, 0, &ioh))
    205  1.37.4.1   thorpej 		return 0;
    206      1.10   mycroft 
    207  1.37.4.1   thorpej 
    208  1.37.4.1   thorpej 	/* XXX broken_indirect_config */
    209      1.10   mycroft 	sc->chan = ia->ia_drq;
    210      1.10   mycroft 	if (sc->chan < 1 || sc->chan > 3) {
    211      1.36  christos 		printf("%s: Bad drq=%d, should be 1..3\n", sc->sc_dev.dv_xname,
    212      1.10   mycroft 		    sc->chan);
    213  1.37.4.1   thorpej 		rv = 0;
    214  1.37.4.1   thorpej 		goto done;
    215      1.10   mycroft 	}
    216      1.10   mycroft 
    217  1.37.4.1   thorpej 
    218      1.10   mycroft 	/* Try Wangtek. */
    219      1.10   mycroft 	sc->type = WANGTEK;
    220  1.37.4.1   thorpej 	sc->CTLPORT = WT_CTLPORT;
    221  1.37.4.1   thorpej 	sc->STATPORT = WT_STATPORT;
    222  1.37.4.1   thorpej 	sc->CMDPORT = WT_CMDPORT;
    223  1.37.4.1   thorpej 	sc->DATAPORT = WT_DATAPORT;
    224      1.10   mycroft 	sc->SDMAPORT = sc->RDMAPORT = 0;
    225      1.10   mycroft 	sc->BUSY = WT_BUSY;		sc->NOEXCEP = WT_NOEXCEP;
    226      1.10   mycroft 	sc->RESETMASK = WT_RESETMASK;	sc->RESETVAL = WT_RESETVAL;
    227      1.10   mycroft 	sc->ONLINE = WT_ONLINE;		sc->RESET = WT_RESET;
    228      1.10   mycroft 	sc->REQUEST = WT_REQUEST;	sc->IEN = WT_IEN;
    229      1.12   mycroft 	if (wtreset(sc)) {
    230      1.12   mycroft 		ia->ia_iosize = WT_NPORT;
    231  1.37.4.1   thorpej 		rv = 1;
    232  1.37.4.1   thorpej 		goto done;
    233      1.12   mycroft 	}
    234      1.10   mycroft 
    235      1.10   mycroft 	/* Try Archive. */
    236      1.10   mycroft 	sc->type = ARCHIVE;
    237  1.37.4.1   thorpej 	sc->CTLPORT = AV_CTLPORT;
    238  1.37.4.1   thorpej 	sc->STATPORT = AV_STATPORT;
    239  1.37.4.1   thorpej 	sc->CMDPORT = AV_CMDPORT;
    240  1.37.4.1   thorpej 	sc->DATAPORT = AV_DATAPORT;
    241  1.37.4.1   thorpej 	sc->SDMAPORT = AV_SDMAPORT;
    242  1.37.4.1   thorpej 	sc->RDMAPORT = AV_RDMAPORT;
    243      1.10   mycroft 	sc->BUSY = AV_BUSY;		sc->NOEXCEP = AV_NOEXCEP;
    244      1.10   mycroft 	sc->RESETMASK = AV_RESETMASK;	sc->RESETVAL = AV_RESETVAL;
    245      1.10   mycroft 	sc->ONLINE = 0;			sc->RESET = AV_RESET;
    246      1.10   mycroft 	sc->REQUEST = AV_REQUEST;	sc->IEN = AV_IEN;
    247      1.12   mycroft 	if (wtreset(sc)) {
    248      1.12   mycroft 		ia->ia_iosize = AV_NPORT;
    249  1.37.4.1   thorpej 		bus_space_unmap(iot, ioh, AV_NPORT);
    250  1.37.4.1   thorpej 		rv = 1;
    251  1.37.4.1   thorpej 		goto done;
    252      1.12   mycroft 	}
    253      1.10   mycroft 
    254      1.10   mycroft 	/* Tape controller not found. */
    255      1.10   mycroft 	sc->type = UNKNOWN;
    256  1.37.4.1   thorpej 
    257  1.37.4.1   thorpej done:
    258  1.37.4.1   thorpej 	bus_space_unmap(iot, ioh, AV_NPORT);
    259  1.37.4.1   thorpej 	return rv;
    260       1.1       cgd }
    261       1.1       cgd 
    262       1.1       cgd /*
    263      1.10   mycroft  * Device is found, configure it.
    264       1.1       cgd  */
    265      1.10   mycroft void
    266      1.10   mycroft wtattach(parent, self, aux)
    267      1.10   mycroft 	struct device *parent, *self;
    268      1.10   mycroft 	void *aux;
    269      1.10   mycroft {
    270      1.10   mycroft 	struct wt_softc *sc = (void *)self;
    271      1.11   mycroft 	struct isa_attach_args *ia = aux;
    272  1.37.4.1   thorpej 	bus_space_tag_t iot = ia->ia_iot;
    273  1.37.4.1   thorpej 	bus_space_handle_t ioh;
    274  1.37.4.1   thorpej 
    275  1.37.4.1   thorpej 	/* Map i/o space */
    276  1.37.4.1   thorpej 	if (bus_space_map(iot, ia->ia_iobase, AV_NPORT, 0, &ioh))
    277  1.37.4.1   thorpej 		panic("mcdattach: bus_space_map failed!");
    278  1.37.4.1   thorpej 
    279  1.37.4.1   thorpej 	sc->sc_iot = iot;
    280  1.37.4.1   thorpej 	sc->sc_ioh = ioh;
    281      1.10   mycroft 
    282  1.37.4.1   thorpej 	/* XXX broken_indirect_config */
    283      1.10   mycroft 	if (sc->type == ARCHIVE) {
    284      1.36  christos 		printf(": type <Archive>\n");
    285      1.10   mycroft 		/* Reset DMA. */
    286  1.37.4.1   thorpej 		bus_space_write_1(iot, ioh, sc->RDMAPORT, 0);
    287      1.10   mycroft 	} else
    288      1.36  christos 		printf(": type <Wangtek>\n");
    289      1.10   mycroft 	sc->flags = TPSTART;		/* tape is rewound */
    290      1.10   mycroft 	sc->dens = -1;			/* unknown density */
    291      1.11   mycroft 
    292      1.37   thorpej 	if (isa_dmamap_create(parent, sc->chan, MAXPHYS,
    293      1.37   thorpej 	    BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW)) {
    294      1.37   thorpej 		printf("%s: can't set up ISA DMA map\n",
    295      1.37   thorpej 		    sc->sc_dev.dv_xname);
    296      1.37   thorpej 		return;
    297      1.37   thorpej 	}
    298      1.37   thorpej 
    299      1.31       cgd 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
    300      1.31       cgd 	    IPL_BIO, wtintr, sc);
    301      1.10   mycroft }
    302      1.10   mycroft 
    303      1.10   mycroft int
    304      1.25       cgd wtdump(dev, blkno, va, size)
    305      1.10   mycroft 	dev_t dev;
    306      1.25       cgd 	daddr_t blkno;
    307      1.25       cgd 	caddr_t va;
    308      1.25       cgd 	size_t size;
    309       1.1       cgd {
    310       1.1       cgd 
    311      1.10   mycroft 	/* Not implemented. */
    312      1.25       cgd 	return ENXIO;
    313       1.1       cgd }
    314       1.1       cgd 
    315      1.10   mycroft int
    316      1.10   mycroft wtsize(dev)
    317      1.10   mycroft 	dev_t dev;
    318       1.1       cgd {
    319       1.1       cgd 
    320      1.10   mycroft 	/* Not implemented. */
    321      1.10   mycroft 	return -1;
    322       1.1       cgd }
    323       1.1       cgd 
    324       1.1       cgd /*
    325      1.10   mycroft  * Open routine, called on every device open.
    326       1.1       cgd  */
    327      1.10   mycroft int
    328      1.32  christos wtopen(dev, flag, mode, p)
    329      1.10   mycroft 	dev_t dev;
    330      1.10   mycroft 	int flag;
    331      1.32  christos 	int mode;
    332      1.32  christos 	struct proc *p;
    333       1.1       cgd {
    334      1.10   mycroft 	int unit = minor(dev) & T_UNIT;
    335      1.10   mycroft 	struct wt_softc *sc;
    336      1.10   mycroft 	int error;
    337      1.10   mycroft 
    338      1.30   thorpej 	if (unit >= wt_cd.cd_ndevs)
    339      1.10   mycroft 		return ENXIO;
    340      1.30   thorpej 	sc = wt_cd.cd_devs[unit];
    341      1.10   mycroft 	if (!sc)
    342      1.10   mycroft 		return ENXIO;
    343      1.10   mycroft 
    344      1.10   mycroft 	/* Check that device is not in use */
    345      1.10   mycroft 	if (sc->flags & TPINUSE)
    346      1.10   mycroft 		return EBUSY;
    347      1.10   mycroft 
    348      1.10   mycroft 	/* If the tape is in rewound state, check the status and set density. */
    349      1.10   mycroft 	if (sc->flags & TPSTART) {
    350      1.10   mycroft 		/* If rewind is going on, wait */
    351      1.32  christos 		if ((error = wtwait(sc, PCATCH, "wtrew")) != 0)
    352      1.10   mycroft 			return error;
    353      1.10   mycroft 
    354      1.10   mycroft 		/* Check the controller status */
    355      1.10   mycroft 		if (!wtsense(sc, 0, (flag & FWRITE) ? 0 : TP_WRP)) {
    356      1.10   mycroft 			/* Bad status, reset the controller. */
    357      1.10   mycroft 			if (!wtreset(sc))
    358      1.10   mycroft 				return EIO;
    359      1.10   mycroft 			if (!wtsense(sc, 1, (flag & FWRITE) ? 0 : TP_WRP))
    360      1.10   mycroft 				return EIO;
    361      1.10   mycroft 		}
    362      1.10   mycroft 
    363      1.10   mycroft 		/* Set up tape density. */
    364      1.10   mycroft 		if (sc->dens != (minor(dev) & WT_DENSEL)) {
    365      1.10   mycroft 			int d = 0;
    366      1.10   mycroft 
    367      1.10   mycroft 			switch (minor(dev) & WT_DENSEL) {
    368      1.10   mycroft 			case WT_DENSDFLT:
    369      1.10   mycroft 			default:
    370      1.10   mycroft 				break;			/* default density */
    371      1.10   mycroft 			case WT_QIC11:
    372      1.10   mycroft 				d = QIC_FMT11;  break;	/* minor 010 */
    373      1.10   mycroft 			case WT_QIC24:
    374      1.10   mycroft 				d = QIC_FMT24;  break;	/* minor 020 */
    375      1.10   mycroft 			case WT_QIC120:
    376      1.10   mycroft 				d = QIC_FMT120; break;	/* minor 030 */
    377      1.10   mycroft 			case WT_QIC150:
    378      1.10   mycroft 				d = QIC_FMT150; break;	/* minor 040 */
    379      1.10   mycroft 			case WT_QIC300:
    380      1.10   mycroft 				d = QIC_FMT300; break;	/* minor 050 */
    381      1.10   mycroft 			case WT_QIC600:
    382      1.10   mycroft 				d = QIC_FMT600; break;	/* minor 060 */
    383      1.10   mycroft 			}
    384      1.10   mycroft 			if (d) {
    385      1.10   mycroft 				/* Change tape density. */
    386      1.10   mycroft 				if (!wtcmd(sc, d))
    387      1.10   mycroft 					return EIO;
    388      1.10   mycroft 				if (!wtsense(sc, 1, TP_WRP | TP_ILL))
    389      1.10   mycroft 					return EIO;
    390      1.10   mycroft 
    391      1.10   mycroft 				/* Check the status of the controller. */
    392      1.10   mycroft 				if (sc->error & TP_ILL) {
    393      1.36  christos 					printf("%s: invalid tape density\n",
    394      1.10   mycroft 					    sc->sc_dev.dv_xname);
    395      1.10   mycroft 					return ENODEV;
    396      1.10   mycroft 				}
    397      1.10   mycroft 			}
    398      1.10   mycroft 			sc->dens = minor(dev) & WT_DENSEL;
    399       1.1       cgd 		}
    400      1.10   mycroft 		sc->flags &= ~TPSTART;
    401      1.10   mycroft 	} else if (sc->dens != (minor(dev) & WT_DENSEL))
    402      1.10   mycroft 		return ENXIO;
    403      1.10   mycroft 
    404      1.10   mycroft 	sc->bsize = (minor(dev) & WT_BSIZE) ? 1024 : 512;
    405      1.10   mycroft 	sc->buf = malloc(sc->bsize, M_TEMP, M_WAITOK);
    406       1.1       cgd 
    407      1.10   mycroft 	sc->flags = TPINUSE;
    408       1.1       cgd 	if (flag & FREAD)
    409      1.10   mycroft 		sc->flags |= TPREAD;
    410       1.1       cgd 	if (flag & FWRITE)
    411      1.10   mycroft 		sc->flags |= TPWRITE;
    412      1.10   mycroft 	return 0;
    413       1.1       cgd }
    414       1.1       cgd 
    415       1.1       cgd /*
    416      1.10   mycroft  * Close routine, called on last device close.
    417       1.1       cgd  */
    418      1.10   mycroft int
    419      1.32  christos wtclose(dev, flags, mode, p)
    420      1.10   mycroft 	dev_t dev;
    421      1.32  christos 	int flags;
    422      1.32  christos 	int mode;
    423      1.32  christos 	struct proc *p;
    424       1.1       cgd {
    425      1.10   mycroft 	int unit = minor(dev) & T_UNIT;
    426      1.30   thorpej 	struct wt_softc *sc = wt_cd.cd_devs[unit];
    427       1.1       cgd 
    428      1.10   mycroft 	/* If rewind is pending, do nothing */
    429      1.10   mycroft 	if (sc->flags & TPREW)
    430      1.10   mycroft 		goto done;
    431       1.1       cgd 
    432      1.10   mycroft 	/* If seek forward is pending and no rewind on close, do nothing */
    433      1.10   mycroft 	if (sc->flags & TPRMARK) {
    434      1.10   mycroft 		if (minor(dev) & T_NOREWIND)
    435      1.10   mycroft 			goto done;
    436       1.1       cgd 
    437      1.10   mycroft 		/* If read file mark is going on, wait */
    438      1.10   mycroft 		wtwait(sc, 0, "wtrfm");
    439       1.1       cgd 	}
    440      1.10   mycroft 
    441      1.10   mycroft 	if (sc->flags & TPWANY) {
    442      1.10   mycroft 		/* Tape was written.  Write file mark. */
    443      1.10   mycroft 		wtwritefm(sc);
    444       1.1       cgd 	}
    445       1.1       cgd 
    446      1.10   mycroft 	if ((minor(dev) & T_NOREWIND) == 0) {
    447      1.10   mycroft 		/* Rewind to beginning of tape. */
    448      1.10   mycroft 		/* Don't wait until rewind, though. */
    449      1.10   mycroft 		wtrewind(sc);
    450      1.10   mycroft 		goto done;
    451       1.1       cgd 	}
    452      1.10   mycroft 	if ((sc->flags & TPRANY) && (sc->flags & (TPVOL | TPWANY)) == 0) {
    453      1.10   mycroft 		/* Space forward to after next file mark if no writing done. */
    454      1.10   mycroft 		/* Don't wait for completion. */
    455      1.10   mycroft 		wtreadfm(sc);
    456       1.1       cgd 	}
    457       1.1       cgd 
    458      1.10   mycroft done:
    459      1.10   mycroft 	sc->flags &= TPREW | TPRMARK | TPSTART | TPTIMER;
    460      1.10   mycroft 	free(sc->buf, M_TEMP);
    461      1.10   mycroft 	return 0;
    462       1.1       cgd }
    463       1.1       cgd 
    464      1.10   mycroft /*
    465      1.10   mycroft  * Ioctl routine.  Compatible with BSD ioctls.
    466      1.10   mycroft  * Direct QIC-02 commands ERASE and RETENSION added.
    467      1.10   mycroft  * There are three possible ioctls:
    468      1.10   mycroft  * ioctl(int fd, MTIOCGET, struct mtget *buf)	-- get status
    469      1.10   mycroft  * ioctl(int fd, MTIOCTOP, struct mtop *buf)	-- do BSD-like op
    470      1.10   mycroft  * ioctl(int fd, WTQICMD, int qicop)		-- do QIC op
    471      1.10   mycroft  */
    472      1.10   mycroft int
    473      1.32  christos wtioctl(dev, cmd, addr, flag, p)
    474      1.10   mycroft 	dev_t dev;
    475      1.19       cgd 	u_long cmd;
    476      1.32  christos 	caddr_t addr;
    477      1.10   mycroft 	int flag;
    478      1.32  christos 	struct proc *p;
    479      1.10   mycroft {
    480      1.10   mycroft 	int unit = minor(dev) & T_UNIT;
    481      1.30   thorpej 	struct wt_softc *sc = wt_cd.cd_devs[unit];
    482      1.10   mycroft 	int error, count, op;
    483      1.10   mycroft 
    484      1.10   mycroft 	switch (cmd) {
    485      1.10   mycroft 	default:
    486      1.10   mycroft 		return EINVAL;
    487      1.10   mycroft 	case WTQICMD:	/* direct QIC command */
    488      1.10   mycroft 		op = *(int *)addr;
    489      1.10   mycroft 		switch (op) {
    490      1.10   mycroft 		default:
    491      1.10   mycroft 			return EINVAL;
    492      1.10   mycroft 		case QIC_ERASE:		/* erase the whole tape */
    493      1.10   mycroft 			if ((sc->flags & TPWRITE) == 0 || (sc->flags & TPWP))
    494      1.10   mycroft 				return EACCES;
    495      1.32  christos 			if ((error = wtwait(sc, PCATCH, "wterase")) != 0)
    496      1.10   mycroft 				return error;
    497       1.1       cgd 			break;
    498      1.10   mycroft 		case QIC_RETENS:	/* retension the tape */
    499      1.32  christos 			if ((error = wtwait(sc, PCATCH, "wtretens")) != 0)
    500      1.10   mycroft 				return error;
    501       1.1       cgd 			break;
    502      1.10   mycroft 		}
    503      1.10   mycroft 		/* Both ERASE and RETENS operations work like REWIND. */
    504      1.10   mycroft 		/* Simulate the rewind operation here. */
    505      1.10   mycroft 		sc->flags &= ~(TPRO | TPWO | TPVOL);
    506      1.10   mycroft 		if (!wtcmd(sc, op))
    507      1.10   mycroft 			return EIO;
    508      1.10   mycroft 		sc->flags |= TPSTART | TPREW;
    509      1.10   mycroft 		if (op == QIC_ERASE)
    510      1.10   mycroft 			sc->flags |= TPWANY;
    511      1.10   mycroft 		wtclock(sc);
    512      1.10   mycroft 		return 0;
    513      1.10   mycroft 	case MTIOCIEOT:	/* ignore EOT errors */
    514      1.10   mycroft 	case MTIOCEEOT:	/* enable EOT errors */
    515      1.10   mycroft 		return 0;
    516      1.10   mycroft 	case MTIOCGET:
    517      1.10   mycroft 		((struct mtget*)addr)->mt_type =
    518      1.10   mycroft 			sc->type == ARCHIVE ? MT_ISVIPER1 : 0x11;
    519      1.10   mycroft 		((struct mtget*)addr)->mt_dsreg = sc->flags;	/* status */
    520      1.10   mycroft 		((struct mtget*)addr)->mt_erreg = sc->error;	/* errors */
    521      1.10   mycroft 		((struct mtget*)addr)->mt_resid = 0;
    522      1.10   mycroft 		((struct mtget*)addr)->mt_fileno = 0;		/* file */
    523      1.10   mycroft 		((struct mtget*)addr)->mt_blkno = 0;		/* block */
    524      1.10   mycroft 		return 0;
    525      1.10   mycroft 	case MTIOCTOP:
    526      1.10   mycroft 		break;
    527       1.1       cgd 	}
    528       1.1       cgd 
    529      1.10   mycroft 	switch ((short)((struct mtop*)addr)->mt_op) {
    530      1.10   mycroft 	default:
    531      1.10   mycroft #if 0
    532      1.10   mycroft 	case MTFSR:	/* forward space record */
    533      1.10   mycroft 	case MTBSR:	/* backward space record */
    534      1.10   mycroft 	case MTBSF:	/* backward space file */
    535      1.10   mycroft #endif
    536      1.10   mycroft 		return EINVAL;
    537      1.10   mycroft 	case MTNOP:	/* no operation, sets status only */
    538      1.10   mycroft 	case MTCACHE:	/* enable controller cache */
    539      1.10   mycroft 	case MTNOCACHE:	/* disable controller cache */
    540      1.10   mycroft 		return 0;
    541      1.10   mycroft 	case MTREW:	/* rewind */
    542      1.10   mycroft 	case MTOFFL:	/* rewind and put the drive offline */
    543      1.10   mycroft 		if (sc->flags & TPREW)   /* rewind is running */
    544      1.10   mycroft 			return 0;
    545      1.32  christos 		if ((error = wtwait(sc, PCATCH, "wtorew")) != 0)
    546      1.10   mycroft 			return error;
    547      1.10   mycroft 		wtrewind(sc);
    548      1.10   mycroft 		return 0;
    549      1.10   mycroft 	case MTFSF:	/* forward space file */
    550      1.10   mycroft 		for (count = ((struct mtop*)addr)->mt_count; count > 0;
    551      1.10   mycroft 		    --count) {
    552      1.32  christos 			if ((error = wtwait(sc, PCATCH, "wtorfm")) != 0)
    553      1.10   mycroft 				return error;
    554      1.32  christos 			if ((error = wtreadfm(sc)) != 0)
    555      1.10   mycroft 				return error;
    556      1.10   mycroft 		}
    557      1.10   mycroft 		return 0;
    558      1.10   mycroft 	case MTWEOF:	/* write an end-of-file record */
    559      1.10   mycroft 		if ((sc->flags & TPWRITE) == 0 || (sc->flags & TPWP))
    560      1.10   mycroft 			return EACCES;
    561      1.32  christos 		if ((error = wtwait(sc, PCATCH, "wtowfm")) != 0)
    562      1.10   mycroft 			return error;
    563      1.32  christos 		if ((error = wtwritefm(sc)) != 0)
    564      1.10   mycroft 			return error;
    565      1.10   mycroft 		return 0;
    566       1.1       cgd 	}
    567      1.10   mycroft 
    568      1.10   mycroft #ifdef DIAGNOSTIC
    569      1.10   mycroft 	panic("wtioctl: impossible");
    570       1.1       cgd #endif
    571       1.1       cgd }
    572       1.1       cgd 
    573      1.10   mycroft /*
    574      1.10   mycroft  * Strategy routine.
    575      1.10   mycroft  */
    576      1.10   mycroft void
    577      1.10   mycroft wtstrategy(bp)
    578      1.10   mycroft 	struct buf *bp;
    579       1.1       cgd {
    580      1.10   mycroft 	int unit = minor(bp->b_dev) & T_UNIT;
    581      1.30   thorpej 	struct wt_softc *sc = wt_cd.cd_devs[unit];
    582      1.10   mycroft 	int s;
    583       1.1       cgd 
    584      1.10   mycroft 	bp->b_resid = bp->b_bcount;
    585       1.1       cgd 
    586      1.10   mycroft 	/* at file marks and end of tape, we just return '0 bytes available' */
    587      1.10   mycroft 	if (sc->flags & TPVOL)
    588      1.10   mycroft 		goto xit;
    589       1.1       cgd 
    590      1.10   mycroft 	if (bp->b_flags & B_READ) {
    591      1.10   mycroft 		/* Check read access and no previous write to this tape. */
    592      1.10   mycroft 		if ((sc->flags & TPREAD) == 0 || (sc->flags & TPWANY))
    593      1.10   mycroft 			goto errxit;
    594       1.1       cgd 
    595      1.10   mycroft 		/* For now, we assume that all data will be copied out */
    596      1.10   mycroft 		/* If read command outstanding, just skip down */
    597      1.10   mycroft 		if ((sc->flags & TPRO) == 0) {
    598      1.10   mycroft 			if (!wtsense(sc, 1, TP_WRP)) {
    599      1.10   mycroft 				/* Clear status. */
    600      1.10   mycroft 				goto errxit;
    601      1.10   mycroft 			}
    602      1.10   mycroft 			if (!wtcmd(sc, QIC_RDDATA)) {
    603      1.10   mycroft 				/* Set read mode. */
    604      1.10   mycroft 				wtsense(sc, 1, TP_WRP);
    605      1.10   mycroft 				goto errxit;
    606      1.10   mycroft 			}
    607      1.10   mycroft 			sc->flags |= TPRO | TPRANY;
    608      1.10   mycroft 		}
    609      1.10   mycroft 	} else {
    610      1.10   mycroft 		/* Check write access and write protection. */
    611      1.10   mycroft 		/* No previous read from this tape allowed. */
    612      1.10   mycroft 		if ((sc->flags & TPWRITE) == 0 || (sc->flags & (TPWP | TPRANY)))
    613      1.10   mycroft 			goto errxit;
    614       1.1       cgd 
    615      1.10   mycroft 		/* If write command outstanding, just skip down */
    616      1.10   mycroft 		if ((sc->flags & TPWO) == 0) {
    617      1.10   mycroft 			if (!wtsense(sc, 1, 0)) {
    618      1.10   mycroft 				/* Clear status. */
    619      1.10   mycroft 				goto errxit;
    620      1.10   mycroft 			}
    621      1.10   mycroft 			if (!wtcmd(sc, QIC_WRTDATA)) {
    622      1.10   mycroft 				/* Set write mode. */
    623      1.10   mycroft 				wtsense(sc, 1, 0);
    624      1.10   mycroft 				goto errxit;
    625      1.10   mycroft 			}
    626      1.10   mycroft 			sc->flags |= TPWO | TPWANY;
    627      1.10   mycroft 		}
    628      1.10   mycroft 	}
    629       1.1       cgd 
    630      1.10   mycroft 	if (bp->b_bcount == 0)
    631      1.10   mycroft 		goto xit;
    632       1.1       cgd 
    633      1.10   mycroft 	sc->flags &= ~TPEXCEP;
    634      1.10   mycroft 	s = splbio();
    635      1.16   mycroft 	if (wtstart(sc, bp->b_flags, bp->b_data, bp->b_bcount)) {
    636      1.10   mycroft 		wtwait(sc, 0, (bp->b_flags & B_READ) ? "wtread" : "wtwrite");
    637      1.10   mycroft 		bp->b_resid -= sc->dmacount;
    638      1.10   mycroft 	}
    639      1.10   mycroft 	splx(s);
    640       1.1       cgd 
    641      1.10   mycroft 	if (sc->flags & TPEXCEP) {
    642      1.10   mycroft errxit:
    643      1.10   mycroft 		bp->b_flags |= B_ERROR;
    644      1.10   mycroft 		bp->b_error = EIO;
    645      1.10   mycroft 	}
    646      1.10   mycroft xit:
    647      1.10   mycroft 	biodone(bp);
    648      1.10   mycroft 	return;
    649      1.26   mycroft }
    650      1.26   mycroft 
    651      1.26   mycroft int
    652      1.32  christos wtread(dev, uio, flags)
    653      1.26   mycroft 	dev_t dev;
    654      1.26   mycroft 	struct uio *uio;
    655      1.32  christos 	int flags;
    656      1.26   mycroft {
    657      1.26   mycroft 
    658      1.26   mycroft 	return (physio(wtstrategy, NULL, dev, B_READ, minphys, uio));
    659      1.26   mycroft }
    660      1.26   mycroft 
    661      1.26   mycroft int
    662      1.32  christos wtwrite(dev, uio, flags)
    663      1.26   mycroft 	dev_t dev;
    664      1.26   mycroft 	struct uio *uio;
    665      1.32  christos 	int flags;
    666      1.26   mycroft {
    667      1.26   mycroft 
    668      1.26   mycroft 	return (physio(wtstrategy, NULL, dev, B_WRITE, minphys, uio));
    669       1.1       cgd }
    670       1.1       cgd 
    671      1.10   mycroft /*
    672      1.10   mycroft  * Interrupt routine.
    673      1.10   mycroft  */
    674      1.10   mycroft int
    675      1.24       cgd wtintr(arg)
    676      1.24       cgd 	void *arg;
    677       1.1       cgd {
    678      1.24       cgd 	struct wt_softc *sc = arg;
    679      1.17   mycroft 	u_char x;
    680      1.10   mycroft 
    681  1.37.4.1   thorpej 	/* get status */
    682  1.37.4.1   thorpej 	x = bus_space_read_1(sc->sc_iot, sc->sc_ioh, sc->STATPORT);
    683      1.28   thorpej 	WTDBPRINT(("wtintr() status=0x%x -- ", x));
    684      1.17   mycroft 	if ((x & (sc->BUSY | sc->NOEXCEP)) == (sc->BUSY | sc->NOEXCEP)) {
    685      1.28   thorpej 		WTDBPRINT(("busy\n"));
    686      1.10   mycroft 		return 0;			/* device is busy */
    687      1.10   mycroft 	}
    688       1.1       cgd 
    689      1.10   mycroft 	/*
    690      1.10   mycroft 	 * Check if rewind finished.
    691      1.10   mycroft 	 */
    692      1.10   mycroft 	if (sc->flags & TPREW) {
    693      1.28   thorpej 		WTDBPRINT(((x & (sc->BUSY | sc->NOEXCEP)) == (sc->BUSY | sc->NOEXCEP) ?
    694      1.10   mycroft 		    "rewind busy?\n" : "rewind finished\n"));
    695      1.10   mycroft 		sc->flags &= ~TPREW;		/* rewind finished */
    696      1.10   mycroft 		wtsense(sc, 1, TP_WRP);
    697      1.10   mycroft 		wakeup((caddr_t)sc);
    698      1.10   mycroft 		return 1;
    699      1.10   mycroft 	}
    700       1.1       cgd 
    701      1.10   mycroft 	/*
    702      1.10   mycroft 	 * Check if writing/reading of file mark finished.
    703      1.10   mycroft 	 */
    704      1.10   mycroft 	if (sc->flags & (TPRMARK | TPWMARK)) {
    705      1.28   thorpej 		WTDBPRINT(((x & (sc->BUSY | sc->NOEXCEP)) == (sc->BUSY | sc->NOEXCEP) ?
    706      1.10   mycroft 		    "marker r/w busy?\n" : "marker r/w finished\n"));
    707      1.17   mycroft 		if ((x & sc->NOEXCEP) == 0)	/* operation failed */
    708      1.10   mycroft 			wtsense(sc, 1, (sc->flags & TPRMARK) ? TP_WRP : 0);
    709      1.10   mycroft 		sc->flags &= ~(TPRMARK | TPWMARK); /* operation finished */
    710      1.10   mycroft 		wakeup((caddr_t)sc);
    711      1.10   mycroft 		return 1;
    712      1.10   mycroft 	}
    713       1.1       cgd 
    714      1.10   mycroft 	/*
    715      1.10   mycroft 	 * Do we started any i/o?  If no, just return.
    716      1.10   mycroft 	 */
    717      1.10   mycroft 	if ((sc->flags & TPACTIVE) == 0) {
    718      1.28   thorpej 		WTDBPRINT(("unexpected interrupt\n"));
    719      1.10   mycroft 		return 0;
    720      1.10   mycroft 	}
    721      1.10   mycroft 	sc->flags &= ~TPACTIVE;
    722      1.10   mycroft 	sc->dmacount += sc->bsize;		/* increment counter */
    723       1.1       cgd 
    724      1.10   mycroft 	/*
    725      1.10   mycroft 	 * Clean up dma.
    726      1.10   mycroft 	 */
    727      1.29   mycroft 	if ((sc->dmaflags & DMAMODE_READ) &&
    728      1.10   mycroft 	    (sc->dmatotal - sc->dmacount) < sc->bsize) {
    729      1.10   mycroft 		/* If reading short block, copy the internal buffer
    730      1.10   mycroft 		 * to the user memory. */
    731      1.37   thorpej 		isa_dmadone(sc->sc_dev.dv_parent, sc->chan);
    732      1.10   mycroft 		bcopy(sc->buf, sc->dmavaddr, sc->dmatotal - sc->dmacount);
    733      1.10   mycroft 	} else
    734      1.37   thorpej 		isa_dmadone(sc->sc_dev.dv_parent, sc->chan);
    735       1.1       cgd 
    736      1.10   mycroft 	/*
    737      1.10   mycroft 	 * On exception, check for end of file and end of volume.
    738      1.10   mycroft 	 */
    739      1.17   mycroft 	if ((x & sc->NOEXCEP) == 0) {
    740      1.28   thorpej 		WTDBPRINT(("i/o exception\n"));
    741      1.29   mycroft 		wtsense(sc, 1, (sc->dmaflags & DMAMODE_READ) ? TP_WRP : 0);
    742      1.10   mycroft 		if (sc->error & (TP_EOM | TP_FIL))
    743      1.10   mycroft 			sc->flags |= TPVOL;	/* end of file */
    744      1.10   mycroft 		else
    745      1.10   mycroft 			sc->flags |= TPEXCEP;	/* i/o error */
    746      1.10   mycroft 		wakeup((caddr_t)sc);
    747      1.10   mycroft 		return 1;
    748      1.10   mycroft 	}
    749      1.10   mycroft 
    750      1.10   mycroft 	if (sc->dmacount < sc->dmatotal) {
    751      1.10   mycroft 		/* Continue I/O. */
    752      1.10   mycroft 		sc->dmavaddr += sc->bsize;
    753      1.10   mycroft 		wtdma(sc);
    754      1.28   thorpej 		WTDBPRINT(("continue i/o, %d\n", sc->dmacount));
    755      1.10   mycroft 		return 1;
    756      1.10   mycroft 	}
    757      1.10   mycroft 	if (sc->dmacount > sc->dmatotal)	/* short last block */
    758      1.10   mycroft 		sc->dmacount = sc->dmatotal;
    759      1.10   mycroft 	/* Wake up user level. */
    760      1.10   mycroft 	wakeup((caddr_t)sc);
    761      1.28   thorpej 	WTDBPRINT(("i/o finished, %d\n", sc->dmacount));
    762      1.10   mycroft 	return 1;
    763      1.10   mycroft }
    764      1.10   mycroft 
    765      1.10   mycroft /* start the rewind operation */
    766      1.10   mycroft void
    767      1.10   mycroft wtrewind(sc)
    768      1.10   mycroft 	struct wt_softc *sc;
    769      1.10   mycroft {
    770      1.10   mycroft 	int rwmode = sc->flags & (TPRO | TPWO);
    771      1.10   mycroft 
    772      1.10   mycroft 	sc->flags &= ~(TPRO | TPWO | TPVOL);
    773      1.10   mycroft 	/*
    774      1.10   mycroft 	 * Wangtek strictly follows QIC-02 standard:
    775      1.10   mycroft 	 * clearing ONLINE in read/write modes causes rewind.
    776      1.10   mycroft 	 * REWIND command is not allowed in read/write mode
    777      1.10   mycroft 	 * and gives `illegal command' error.
    778      1.10   mycroft 	 */
    779      1.10   mycroft 	if (sc->type == WANGTEK && rwmode) {
    780  1.37.4.1   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, sc->CTLPORT, 0);
    781      1.10   mycroft 	} else if (!wtcmd(sc, QIC_REWIND))
    782      1.10   mycroft 		return;
    783      1.10   mycroft 	sc->flags |= TPSTART | TPREW;
    784      1.10   mycroft 	wtclock(sc);
    785       1.1       cgd }
    786       1.1       cgd 
    787      1.10   mycroft /*
    788      1.10   mycroft  * Start the `read marker' operation.
    789      1.10   mycroft  */
    790      1.10   mycroft int
    791      1.10   mycroft wtreadfm(sc)
    792      1.10   mycroft 	struct wt_softc *sc;
    793       1.1       cgd {
    794       1.1       cgd 
    795      1.10   mycroft 	sc->flags &= ~(TPRO | TPWO | TPVOL);
    796      1.10   mycroft 	if (!wtcmd(sc, QIC_READFM)) {
    797      1.10   mycroft 		wtsense(sc, 1, TP_WRP);
    798      1.10   mycroft 		return EIO;
    799      1.10   mycroft 	}
    800      1.10   mycroft 	sc->flags |= TPRMARK | TPRANY;
    801      1.10   mycroft 	wtclock(sc);
    802      1.10   mycroft 	/* Don't wait for completion here. */
    803      1.10   mycroft 	return 0;
    804       1.1       cgd }
    805       1.1       cgd 
    806      1.10   mycroft /*
    807      1.10   mycroft  * Write marker to the tape.
    808      1.10   mycroft  */
    809      1.10   mycroft int
    810      1.10   mycroft wtwritefm(sc)
    811      1.10   mycroft 	struct wt_softc *sc;
    812       1.1       cgd {
    813       1.1       cgd 
    814      1.10   mycroft 	tsleep((caddr_t)wtwritefm, WTPRI, "wtwfm", hz);
    815      1.10   mycroft 	sc->flags &= ~(TPRO | TPWO);
    816      1.10   mycroft 	if (!wtcmd(sc, QIC_WRITEFM)) {
    817      1.10   mycroft 		wtsense(sc, 1, 0);
    818      1.10   mycroft 		return EIO;
    819      1.10   mycroft 	}
    820      1.10   mycroft 	sc->flags |= TPWMARK | TPWANY;
    821      1.10   mycroft 	wtclock(sc);
    822      1.10   mycroft 	return wtwait(sc, 0, "wtwfm");
    823      1.10   mycroft }
    824       1.1       cgd 
    825      1.10   mycroft /*
    826      1.10   mycroft  * While controller status & mask == bits continue waiting.
    827      1.10   mycroft  */
    828      1.10   mycroft u_char
    829      1.34   mycroft wtsoft(sc, mask, bits)
    830      1.10   mycroft 	struct wt_softc *sc;
    831      1.10   mycroft 	int mask, bits;
    832      1.10   mycroft {
    833  1.37.4.1   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    834  1.37.4.1   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    835      1.17   mycroft 	u_char x;
    836      1.10   mycroft 	int i;
    837      1.10   mycroft 
    838  1.37.4.1   thorpej 
    839      1.10   mycroft 	/* Poll status port, waiting for specified bits. */
    840      1.10   mycroft 	for (i = 0; i < 1000; ++i) {	/* up to 1 msec */
    841  1.37.4.1   thorpej 		x = bus_space_read_1(iot, ioh, sc->STATPORT);
    842      1.17   mycroft 		if ((x & mask) != bits)
    843      1.17   mycroft 			return x;
    844      1.10   mycroft 		delay(1);
    845      1.10   mycroft 	}
    846      1.10   mycroft 	for (i = 0; i < 100; ++i) {	/* up to 10 msec */
    847  1.37.4.1   thorpej 		x = bus_space_read_1(iot, ioh, sc->STATPORT);
    848      1.17   mycroft 		if ((x & mask) != bits)
    849      1.17   mycroft 			return x;
    850      1.10   mycroft 		delay(100);
    851      1.10   mycroft 	}
    852      1.10   mycroft 	for (;;) {			/* forever */
    853  1.37.4.1   thorpej 		x = bus_space_read_1(iot, ioh, sc->STATPORT);
    854      1.17   mycroft 		if ((x & mask) != bits)
    855      1.17   mycroft 			return x;
    856      1.34   mycroft 		tsleep((caddr_t)wtsoft, WTPRI, "wtsoft", 1);
    857      1.10   mycroft 	}
    858       1.1       cgd }
    859       1.1       cgd 
    860      1.10   mycroft /*
    861      1.10   mycroft  * Execute QIC command.
    862      1.10   mycroft  */
    863      1.10   mycroft int
    864      1.10   mycroft wtcmd(sc, cmd)
    865      1.10   mycroft 	struct wt_softc *sc;
    866      1.10   mycroft 	int cmd;
    867      1.10   mycroft {
    868  1.37.4.1   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    869  1.37.4.1   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    870      1.17   mycroft 	u_char x;
    871      1.17   mycroft 	int s;
    872      1.10   mycroft 
    873      1.28   thorpej 	WTDBPRINT(("wtcmd() cmd=0x%x\n", cmd));
    874      1.17   mycroft 	s = splbio();
    875      1.34   mycroft 	x = wtsoft(sc, sc->BUSY | sc->NOEXCEP, sc->BUSY | sc->NOEXCEP); /* ready? */
    876      1.17   mycroft 	if ((x & sc->NOEXCEP) == 0) {			/* error */
    877      1.17   mycroft 		splx(s);
    878      1.10   mycroft 		return 0;
    879      1.17   mycroft 	}
    880      1.10   mycroft 
    881  1.37.4.1   thorpej 	/* output the command */
    882  1.37.4.1   thorpej 	bus_space_write_1(iot, ioh, sc->CMDPORT, cmd);
    883  1.37.4.1   thorpej 
    884  1.37.4.1   thorpej 	/* set request */
    885  1.37.4.1   thorpej 	bus_space_write_1(iot, ioh, sc->CTLPORT, sc->REQUEST | sc->ONLINE);
    886  1.37.4.1   thorpej 
    887  1.37.4.1   thorpej 	/* wait for ready */
    888  1.37.4.1   thorpej 	wtsoft(sc, sc->BUSY, sc->BUSY);
    889       1.1       cgd 
    890  1.37.4.1   thorpej 	/* reset request */
    891  1.37.4.1   thorpej 	bus_space_write_1(iot, ioh, sc->CTLPORT, sc->IEN | sc->ONLINE);
    892  1.37.4.1   thorpej 
    893  1.37.4.1   thorpej 	/* wait for not ready */
    894  1.37.4.1   thorpej 	wtsoft(sc, sc->BUSY, 0);
    895      1.17   mycroft 	splx(s);
    896      1.10   mycroft 	return 1;
    897       1.1       cgd }
    898       1.1       cgd 
    899      1.10   mycroft /* wait for the end of i/o, seeking marker or rewind operation */
    900      1.10   mycroft int
    901      1.10   mycroft wtwait(sc, catch, msg)
    902      1.10   mycroft 	struct wt_softc *sc;
    903      1.10   mycroft 	int catch;
    904      1.10   mycroft 	char *msg;
    905       1.1       cgd {
    906      1.10   mycroft 	int error;
    907       1.1       cgd 
    908      1.28   thorpej 	WTDBPRINT(("wtwait() `%s'\n", msg));
    909      1.10   mycroft 	while (sc->flags & (TPACTIVE | TPREW | TPRMARK | TPWMARK))
    910      1.32  christos 		if ((error = tsleep((caddr_t)sc, WTPRI | catch, msg, 0)) != 0)
    911      1.10   mycroft 			return error;
    912      1.10   mycroft 	return 0;
    913       1.1       cgd }
    914       1.1       cgd 
    915      1.10   mycroft /* initialize dma for the i/o operation */
    916      1.10   mycroft void
    917      1.10   mycroft wtdma(sc)
    918      1.10   mycroft 	struct wt_softc *sc;
    919       1.1       cgd {
    920  1.37.4.1   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    921  1.37.4.1   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    922       1.1       cgd 
    923      1.10   mycroft 	sc->flags |= TPACTIVE;
    924      1.10   mycroft 	wtclock(sc);
    925       1.1       cgd 
    926      1.10   mycroft 	if (sc->type == ARCHIVE) {
    927      1.10   mycroft 		/* Set DMA. */
    928  1.37.4.1   thorpej 		bus_space_write_1(iot, ioh, sc->SDMAPORT, 0);
    929       1.1       cgd 	}
    930       1.1       cgd 
    931      1.29   mycroft 	if ((sc->dmaflags & DMAMODE_READ) &&
    932      1.10   mycroft 	    (sc->dmatotal - sc->dmacount) < sc->bsize) {
    933      1.10   mycroft 		/* Reading short block; do it through the internal buffer. */
    934      1.37   thorpej 		isa_dmastart(sc->sc_dev.dv_parent, sc->chan, sc->buf,
    935      1.37   thorpej 		    sc->bsize, NULL, sc->dmaflags, BUS_DMA_NOWAIT);
    936      1.10   mycroft 	} else
    937      1.37   thorpej 		isa_dmastart(sc->sc_dev.dv_parent, sc->chan, sc->dmavaddr,
    938      1.37   thorpej 		    sc->bsize, NULL, sc->dmaflags, BUS_DMA_NOWAIT);
    939       1.1       cgd }
    940       1.1       cgd 
    941      1.10   mycroft /* start i/o operation */
    942      1.10   mycroft int
    943      1.10   mycroft wtstart(sc, flag, vaddr, len)
    944      1.10   mycroft 	struct wt_softc *sc;
    945      1.10   mycroft 	int flag;
    946      1.10   mycroft 	void *vaddr;
    947      1.10   mycroft 	size_t len;
    948      1.10   mycroft {
    949      1.17   mycroft 	u_char x;
    950      1.10   mycroft 
    951      1.28   thorpej 	WTDBPRINT(("wtstart()\n"));
    952      1.34   mycroft 	x = wtsoft(sc, sc->BUSY | sc->NOEXCEP, sc->BUSY | sc->NOEXCEP); /* ready? */
    953      1.17   mycroft 	if ((x & sc->NOEXCEP) == 0) {
    954      1.10   mycroft 		sc->flags |= TPEXCEP;	/* error */
    955      1.10   mycroft 		return 0;
    956      1.10   mycroft 	}
    957      1.10   mycroft 	sc->flags &= ~TPEXCEP;		/* clear exception flag */
    958      1.10   mycroft 	sc->dmavaddr = vaddr;
    959      1.10   mycroft 	sc->dmatotal = len;
    960      1.10   mycroft 	sc->dmacount = 0;
    961      1.29   mycroft 	sc->dmaflags = flag & B_READ ? DMAMODE_READ : DMAMODE_WRITE;
    962      1.10   mycroft 	wtdma(sc);
    963      1.10   mycroft 	return 1;
    964       1.1       cgd }
    965       1.1       cgd 
    966      1.10   mycroft /*
    967      1.10   mycroft  * Start timer.
    968      1.10   mycroft  */
    969      1.10   mycroft void
    970      1.10   mycroft wtclock(sc)
    971      1.10   mycroft 	struct wt_softc *sc;
    972       1.1       cgd {
    973       1.1       cgd 
    974      1.10   mycroft 	if (sc->flags & TPTIMER)
    975      1.10   mycroft 		return;
    976      1.10   mycroft 	sc->flags |= TPTIMER;
    977      1.10   mycroft 	/*
    978      1.10   mycroft 	 * Some controllers seem to lose dma interrupts too often.  To make the
    979      1.10   mycroft 	 * tape stream we need 1 tick timeout.
    980      1.10   mycroft 	 */
    981      1.15   mycroft 	timeout(wttimer, sc, (sc->flags & TPACTIVE) ? 1 : hz);
    982       1.1       cgd }
    983       1.1       cgd 
    984      1.10   mycroft /*
    985      1.10   mycroft  * Simulate an interrupt periodically while i/o is going.
    986      1.10   mycroft  * This is necessary in case interrupts get eaten due to
    987      1.10   mycroft  * multiple devices on a single IRQ line.
    988      1.10   mycroft  */
    989      1.10   mycroft void
    990      1.14       cgd wttimer(arg)
    991      1.14       cgd 	void *arg;
    992       1.1       cgd {
    993  1.37.4.1   thorpej 	register struct wt_softc *sc = (struct wt_softc *)arg;
    994      1.10   mycroft 	int s;
    995  1.37.4.1   thorpej 	u_char status;
    996      1.10   mycroft 
    997      1.10   mycroft 	sc->flags &= ~TPTIMER;
    998      1.10   mycroft 	if ((sc->flags & (TPACTIVE | TPREW | TPRMARK | TPWMARK)) == 0)
    999      1.10   mycroft 		return;
   1000       1.1       cgd 
   1001      1.10   mycroft 	/* If i/o going, simulate interrupt. */
   1002      1.10   mycroft 	s = splbio();
   1003  1.37.4.1   thorpej 	status = bus_space_read_1(sc->sc_iot, sc->sc_ioh, sc->STATPORT);
   1004  1.37.4.1   thorpej 	if ((status & (sc->BUSY | sc->NOEXCEP)) != (sc->BUSY | sc->NOEXCEP)) {
   1005      1.28   thorpej 		WTDBPRINT(("wttimer() -- "));
   1006      1.11   mycroft 		wtintr(sc);
   1007      1.10   mycroft 	}
   1008      1.10   mycroft 	splx(s);
   1009      1.10   mycroft 
   1010      1.10   mycroft 	/* Restart timer if i/o pending. */
   1011      1.10   mycroft 	if (sc->flags & (TPACTIVE | TPREW | TPRMARK | TPWMARK))
   1012      1.10   mycroft 		wtclock(sc);
   1013       1.1       cgd }
   1014       1.1       cgd 
   1015      1.10   mycroft /*
   1016      1.10   mycroft  * Perform QIC-02 and QIC-36 compatible reset sequence.
   1017      1.10   mycroft  */
   1018      1.10   mycroft int
   1019      1.10   mycroft wtreset(sc)
   1020      1.10   mycroft 	struct wt_softc *sc;
   1021       1.1       cgd {
   1022  1.37.4.1   thorpej 	bus_space_tag_t iot = sc->sc_iot;
   1023  1.37.4.1   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
   1024      1.17   mycroft 	u_char x;
   1025      1.10   mycroft 	int i;
   1026       1.1       cgd 
   1027  1.37.4.1   thorpej 	/* send reset */
   1028  1.37.4.1   thorpej 	bus_space_write_1(iot, ioh, sc->CTLPORT, sc->RESET | sc->ONLINE);
   1029      1.10   mycroft 	delay(30);
   1030  1.37.4.1   thorpej 	/* turn off reset */
   1031  1.37.4.1   thorpej 	bus_space_write_1(iot, ioh, sc->CTLPORT, sc->ONLINE);
   1032      1.10   mycroft 	delay(30);
   1033      1.10   mycroft 
   1034      1.10   mycroft 	/* Read the controller status. */
   1035  1.37.4.1   thorpej 	x = bus_space_read_1(iot, ioh, sc->STATPORT);
   1036      1.17   mycroft 	if (x == 0xff)			/* no port at this address? */
   1037      1.10   mycroft 		return 0;
   1038      1.10   mycroft 
   1039      1.10   mycroft 	/* Wait 3 sec for reset to complete. Needed for QIC-36 boards? */
   1040      1.10   mycroft 	for (i = 0; i < 3000; ++i) {
   1041      1.17   mycroft 		if ((x & sc->BUSY) == 0 || (x & sc->NOEXCEP) == 0)
   1042      1.10   mycroft 			break;
   1043      1.10   mycroft 		delay(1000);
   1044  1.37.4.1   thorpej 		x = bus_space_read_1(iot, ioh, sc->STATPORT);
   1045       1.1       cgd 	}
   1046      1.17   mycroft 	return (x & sc->RESETMASK) == sc->RESETVAL;
   1047       1.1       cgd }
   1048       1.1       cgd 
   1049      1.10   mycroft /*
   1050      1.10   mycroft  * Get controller status information.  Return 0 if user i/o request should
   1051      1.10   mycroft  * receive an i/o error code.
   1052      1.10   mycroft  */
   1053      1.10   mycroft int
   1054      1.10   mycroft wtsense(sc, verbose, ignore)
   1055      1.10   mycroft 	struct wt_softc *sc;
   1056      1.10   mycroft 	int verbose, ignore;
   1057      1.10   mycroft {
   1058      1.10   mycroft 	char *msg = 0;
   1059      1.10   mycroft 	int error;
   1060      1.10   mycroft 
   1061      1.28   thorpej 	WTDBPRINT(("wtsense() ignore=0x%x\n", ignore));
   1062      1.10   mycroft 	sc->flags &= ~(TPRO | TPWO);
   1063      1.10   mycroft 	if (!wtstatus(sc))
   1064      1.10   mycroft 		return 0;
   1065      1.10   mycroft 	if ((sc->error & TP_ST0) == 0)
   1066      1.10   mycroft 		sc->error &= ~TP_ST0MASK;
   1067      1.10   mycroft 	if ((sc->error & TP_ST1) == 0)
   1068      1.10   mycroft 		sc->error &= ~TP_ST1MASK;
   1069      1.10   mycroft 	sc->error &= ~ignore;	/* ignore certain errors */
   1070      1.10   mycroft 	error = sc->error & (TP_FIL | TP_BNL | TP_UDA | TP_EOM | TP_WRP |
   1071      1.10   mycroft 	    TP_USL | TP_CNI | TP_MBD | TP_NDT | TP_ILL);
   1072      1.10   mycroft 	if (!error)
   1073      1.10   mycroft 		return 1;
   1074      1.10   mycroft 	if (!verbose)
   1075      1.10   mycroft 		return 0;
   1076      1.10   mycroft 
   1077      1.10   mycroft 	/* lifted from tdriver.c from Wangtek */
   1078      1.10   mycroft 	if (error & TP_USL)
   1079      1.10   mycroft 		msg = "Drive not online";
   1080      1.10   mycroft 	else if (error & TP_CNI)
   1081      1.10   mycroft 		msg = "No cartridge";
   1082      1.10   mycroft 	else if ((error & TP_WRP) && (sc->flags & TPWP) == 0) {
   1083      1.10   mycroft 		msg = "Tape is write protected";
   1084      1.10   mycroft 		sc->flags |= TPWP;
   1085      1.10   mycroft 	} else if (error & TP_FIL)
   1086      1.10   mycroft 		msg = 0 /*"Filemark detected"*/;
   1087      1.10   mycroft 	else if (error & TP_EOM)
   1088      1.10   mycroft 		msg = 0 /*"End of tape"*/;
   1089      1.10   mycroft 	else if (error & TP_BNL)
   1090      1.10   mycroft 		msg = "Block not located";
   1091      1.10   mycroft 	else if (error & TP_UDA)
   1092      1.10   mycroft 		msg = "Unrecoverable data error";
   1093      1.10   mycroft 	else if (error & TP_NDT)
   1094      1.10   mycroft 		msg = "No data detected";
   1095      1.10   mycroft 	else if (error & TP_ILL)
   1096      1.10   mycroft 		msg = "Illegal command";
   1097      1.10   mycroft 	if (msg)
   1098      1.36  christos 		printf("%s: %s\n", sc->sc_dev.dv_xname, msg);
   1099      1.10   mycroft 	return 0;
   1100       1.1       cgd }
   1101       1.1       cgd 
   1102      1.10   mycroft /*
   1103      1.10   mycroft  * Get controller status information.
   1104      1.10   mycroft  */
   1105      1.10   mycroft int
   1106      1.10   mycroft wtstatus(sc)
   1107      1.10   mycroft 	struct wt_softc *sc;
   1108       1.1       cgd {
   1109  1.37.4.1   thorpej 	bus_space_tag_t iot = sc->sc_iot;
   1110  1.37.4.1   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
   1111      1.10   mycroft 	char *p;
   1112      1.17   mycroft 	int s;
   1113       1.1       cgd 
   1114      1.17   mycroft 	s = splbio();
   1115      1.34   mycroft 	wtsoft(sc, sc->BUSY | sc->NOEXCEP, sc->BUSY | sc->NOEXCEP); /* ready? */
   1116  1.37.4.1   thorpej 	/* send `read status' command */
   1117  1.37.4.1   thorpej 	bus_space_write_1(iot, ioh, sc->CMDPORT, QIC_RDSTAT);
   1118  1.37.4.1   thorpej 
   1119  1.37.4.1   thorpej 	/* set request */
   1120  1.37.4.1   thorpej 	bus_space_write_1(iot, ioh, sc->CTLPORT, sc->REQUEST | sc->ONLINE);
   1121  1.37.4.1   thorpej 
   1122  1.37.4.1   thorpej 	/* wait for ready */
   1123  1.37.4.1   thorpej 	wtsoft(sc, sc->BUSY, sc->BUSY);
   1124  1.37.4.1   thorpej 	/* reset request */
   1125  1.37.4.1   thorpej 	bus_space_write_1(iot, ioh, sc->CTLPORT, sc->ONLINE);
   1126       1.1       cgd 
   1127  1.37.4.1   thorpej 	/* wait for not ready */
   1128  1.37.4.1   thorpej 	wtsoft(sc, sc->BUSY, 0);
   1129       1.1       cgd 
   1130      1.10   mycroft 	p = (char *)&sc->error;
   1131      1.10   mycroft 	while (p < (char *)&sc->error + 6) {
   1132  1.37.4.1   thorpej 		u_char x = wtsoft(sc, sc->BUSY | sc->NOEXCEP,
   1133  1.37.4.1   thorpej 		    sc->BUSY | sc->NOEXCEP);
   1134  1.37.4.1   thorpej 
   1135      1.17   mycroft 		if ((x & sc->NOEXCEP) == 0) {	/* error */
   1136      1.17   mycroft 			splx(s);
   1137      1.10   mycroft 			return 0;
   1138      1.17   mycroft 		}
   1139       1.1       cgd 
   1140  1.37.4.1   thorpej 		/* read status byte */
   1141  1.37.4.1   thorpej 		*p++ = bus_space_read_1(iot, ioh, sc->DATAPORT);
   1142  1.37.4.1   thorpej 
   1143  1.37.4.1   thorpej 		/* set request */
   1144  1.37.4.1   thorpej 		bus_space_write_1(iot, ioh, sc->CTLPORT,
   1145  1.37.4.1   thorpej 		    sc->REQUEST | sc->ONLINE);
   1146  1.37.4.1   thorpej 
   1147  1.37.4.1   thorpej 		/* wait for not ready */
   1148  1.37.4.1   thorpej 		wtsoft(sc, sc->BUSY, 0);
   1149       1.1       cgd 
   1150  1.37.4.1   thorpej 		/* unset request */
   1151  1.37.4.1   thorpej 		bus_space_write_1(iot, ioh, sc->CTLPORT, sc->ONLINE);
   1152      1.10   mycroft 	}
   1153      1.17   mycroft 	splx(s);
   1154      1.10   mycroft 	return 1;
   1155       1.1       cgd }
   1156