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