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