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