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