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