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