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