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