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