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