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