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