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