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