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