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