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