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