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