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