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