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