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