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