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