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