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