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