wt.c revision 1.25 1 /* $NetBSD: wt.c,v 1.25 1995/06/26 05:37:16 cgd Exp $ */
2
3 /*
4 * Streamer tape driver.
5 * Supports Archive and Wangtek compatible QIC-02/QIC-36 boards.
6 *
7 * Copyright (C) 1993 by:
8 * Sergey Ryzhkov <sir (at) kiae.su>
9 * Serge Vakulenko <vak (at) zebub.msk.su>
10 *
11 * This software is distributed with NO WARRANTIES, not even the implied
12 * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 *
14 * Authors grant any other persons or organisations permission to use
15 * or modify this software as long as this message is kept with the software,
16 * all derivative works or modified versions.
17 *
18 * This driver is derived from the old 386bsd Wangtek streamer tape driver,
19 * made by Robert Baron at CMU, based on Intel sources.
20 */
21
22 /*
23 * Copyright (c) 1989 Carnegie-Mellon University.
24 * All rights reserved.
25 *
26 * Authors: Robert Baron
27 *
28 * Permission to use, copy, modify and distribute this software and
29 * its documentation is hereby granted, provided that both the copyright
30 * notice and this permission notice appear in all copies of the
31 * software, derivative works or modified versions, and any portions
32 * thereof, and that both notices appear in supporting documentation.
33 *
34 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
35 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
36 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
37 *
38 * Carnegie Mellon requests users of this software to return to
39 *
40 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
41 * School of Computer Science
42 * Carnegie Mellon University
43 * Pittsburgh PA 15213-3890
44 *
45 * any improvements or extensions that they make and grant Carnegie the
46 * rights to redistribute these changes.
47 */
48
49 /*
50 * Copyright 1988, 1989 by Intel Corporation
51 */
52
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/kernel.h>
56 #include <sys/buf.h>
57 #include <sys/fcntl.h>
58 #include <sys/malloc.h>
59 #include <sys/ioctl.h>
60 #include <sys/mtio.h>
61 #include <sys/device.h>
62
63 #include <vm/vm_param.h>
64
65 #include <machine/pio.h>
66
67 #include <dev/isa/isavar.h>
68 #include <dev/isa/isadmavar.h>
69 #include <dev/isa/wtreg.h>
70
71 /*
72 * Uncomment this to enable internal device tracing.
73 */
74 #define DEBUG(x) /* printf x */
75
76 #define WTPRI (PZERO+10) /* sleep priority */
77
78 /*
79 * Wangtek controller ports
80 */
81 #define WT_CTLPORT(base) ((base)+0) /* control, write only */
82 #define WT_STATPORT(base) ((base)+0) /* status, read only */
83 #define WT_CMDPORT(base) ((base)+1) /* command, write only */
84 #define WT_DATAPORT(base) ((base)+1) /* data, read only */
85 #define WT_NPORT 2 /* 2 i/o ports */
86
87 /* status port bits */
88 #define WT_BUSY 0x01 /* not ready bit define */
89 #define WT_NOEXCEP 0x02 /* no exception bit define */
90 #define WT_RESETMASK 0x07 /* to check after reset */
91 #define WT_RESETVAL 0x05 /* state after reset */
92
93 /* control port bits */
94 #define WT_ONLINE 0x01 /* device selected */
95 #define WT_RESET 0x02 /* reset command */
96 #define WT_REQUEST 0x04 /* request command */
97 #define WT_IEN 0x08 /* enable dma */
98
99 /*
100 * Archive controller ports
101 */
102 #define AV_DATAPORT(base) ((base)+0) /* data, read only */
103 #define AV_CMDPORT(base) ((base)+0) /* command, write only */
104 #define AV_STATPORT(base) ((base)+1) /* status, read only */
105 #define AV_CTLPORT(base) ((base)+1) /* control, write only */
106 #define AV_SDMAPORT(base) ((base)+2) /* start dma */
107 #define AV_RDMAPORT(base) ((base)+3) /* reset dma */
108 #define AV_NPORT 4 /* 4 i/o ports */
109
110 /* status port bits */
111 #define AV_BUSY 0x40 /* not ready bit define */
112 #define AV_NOEXCEP 0x20 /* no exception bit define */
113 #define AV_RESETMASK 0xf8 /* to check after reset */
114 #define AV_RESETVAL 0x50 /* state after reset */
115
116 /* control port bits */
117 #define AV_RESET 0x80 /* reset command */
118 #define AV_REQUEST 0x40 /* request command */
119 #define AV_IEN 0x20 /* enable interrupts */
120
121 enum wttype {
122 UNKNOWN = 0, /* unknown type, driver disabled */
123 ARCHIVE, /* Archive Viper SC499, SC402 etc */
124 WANGTEK, /* Wangtek */
125 };
126
127 struct wt_softc {
128 struct device sc_dev;
129 void *sc_ih;
130
131 enum wttype type; /* type of controller */
132 int sc_iobase; /* base i/o port */
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, B_READ or B_WRITE */
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 int DATAPORT, CMDPORT, STATPORT, CTLPORT, SDMAPORT, RDMAPORT;
149 u_char BUSY, NOEXCEP, RESETMASK, RESETVAL, ONLINE, RESET, REQUEST, IEN;
150 };
151
152 int wtwait __P((struct wt_softc *sc, int catch, char *msg));
153 int wtcmd __P((struct wt_softc *sc, int cmd));
154 int wtstart __P((struct wt_softc *sc, int flag, void *vaddr, size_t len));
155 void wtdma __P((struct wt_softc *sc));
156 void wttimer __P((void *arg));
157 void wtclock __P((struct wt_softc *sc));
158 int wtreset __P((struct wt_softc *sc));
159 int wtsense __P((struct wt_softc *sc, int verbose, int ignore));
160 int wtstatus __P((struct wt_softc *sc));
161 void wtrewind __P((struct wt_softc *sc));
162 int wtreadfm __P((struct wt_softc *sc));
163 int wtwritefm __P((struct wt_softc *sc));
164 u_char wtpoll __P((struct wt_softc *sc, int mask, int bits));
165
166 int wtprobe __P((struct device *, void *, void *));
167 void wtattach __P((struct device *, struct device *, void *));
168 int wtintr __P((void *sc));
169
170 struct cfdriver wtcd = {
171 NULL, "wt", wtprobe, wtattach, DV_TAPE, sizeof(struct wt_softc)
172 };
173
174 /*
175 * Probe for the presence of the device.
176 */
177 int
178 wtprobe(parent, match, aux)
179 struct device *parent;
180 void *match, *aux;
181 {
182 struct wt_softc *sc = match;
183 struct isa_attach_args *ia = aux;
184 int iobase;
185
186 sc->chan = ia->ia_drq;
187 sc->sc_iobase = iobase = ia->ia_iobase;
188 if (sc->chan < 1 || sc->chan > 3) {
189 printf("%s: Bad drq=%d, should be 1..3\n", sc->sc_dev.dv_xname,
190 sc->chan);
191 return 0;
192 }
193
194 /* Try Wangtek. */
195 sc->type = WANGTEK;
196 sc->CTLPORT = WT_CTLPORT(iobase);
197 sc->STATPORT = WT_STATPORT(iobase);
198 sc->CMDPORT = WT_CMDPORT(iobase);
199 sc->DATAPORT = WT_DATAPORT(iobase);
200 sc->SDMAPORT = sc->RDMAPORT = 0;
201 sc->BUSY = WT_BUSY; sc->NOEXCEP = WT_NOEXCEP;
202 sc->RESETMASK = WT_RESETMASK; sc->RESETVAL = WT_RESETVAL;
203 sc->ONLINE = WT_ONLINE; sc->RESET = WT_RESET;
204 sc->REQUEST = WT_REQUEST; sc->IEN = WT_IEN;
205 if (wtreset(sc)) {
206 ia->ia_iosize = WT_NPORT;
207 return 1;
208 }
209
210 /* Try Archive. */
211 sc->type = ARCHIVE;
212 sc->CTLPORT = AV_CTLPORT(iobase);
213 sc->STATPORT = AV_STATPORT(iobase);
214 sc->CMDPORT = AV_CMDPORT(iobase);
215 sc->DATAPORT = AV_DATAPORT(iobase);
216 sc->SDMAPORT = AV_SDMAPORT(iobase);
217 sc->RDMAPORT = AV_RDMAPORT(iobase);
218 sc->BUSY = AV_BUSY; sc->NOEXCEP = AV_NOEXCEP;
219 sc->RESETMASK = AV_RESETMASK; sc->RESETVAL = AV_RESETVAL;
220 sc->ONLINE = 0; sc->RESET = AV_RESET;
221 sc->REQUEST = AV_REQUEST; sc->IEN = AV_IEN;
222 if (wtreset(sc)) {
223 ia->ia_iosize = AV_NPORT;
224 return 1;
225 }
226
227 /* Tape controller not found. */
228 sc->type = UNKNOWN;
229 return 0;
230 }
231
232 /*
233 * Device is found, configure it.
234 */
235 void
236 wtattach(parent, self, aux)
237 struct device *parent, *self;
238 void *aux;
239 {
240 struct wt_softc *sc = (void *)self;
241 struct isa_attach_args *ia = aux;
242
243 if (sc->type == ARCHIVE) {
244 printf(": type <Archive>\n");
245 /* Reset DMA. */
246 outb(sc->RDMAPORT, 0);
247 } else
248 printf(": type <Wangtek>\n");
249 sc->flags = TPSTART; /* tape is rewound */
250 sc->dens = -1; /* unknown density */
251
252 sc->sc_ih = isa_intr_establish(ia->ia_irq, ISA_IST_EDGE, ISA_IPL_BIO,
253 wtintr, sc);
254 }
255
256 int
257 wtdump(dev, blkno, va, size)
258 dev_t dev;
259 daddr_t blkno;
260 caddr_t va;
261 size_t size;
262 {
263
264 /* Not implemented. */
265 return ENXIO;
266 }
267
268 int
269 wtsize(dev)
270 dev_t dev;
271 {
272
273 /* Not implemented. */
274 return -1;
275 }
276
277 /*
278 * Open routine, called on every device open.
279 */
280 int
281 wtopen(dev, flag)
282 dev_t dev;
283 int flag;
284 {
285 int unit = minor(dev) & T_UNIT;
286 struct wt_softc *sc;
287 int error;
288
289 if (unit >= wtcd.cd_ndevs)
290 return ENXIO;
291 sc = wtcd.cd_devs[unit];
292 if (!sc)
293 return ENXIO;
294
295 /* Check that device is not in use */
296 if (sc->flags & TPINUSE)
297 return EBUSY;
298
299 /* If the tape is in rewound state, check the status and set density. */
300 if (sc->flags & TPSTART) {
301 /* If rewind is going on, wait */
302 if (error = wtwait(sc, PCATCH, "wtrew"))
303 return error;
304
305 /* Check the controller status */
306 if (!wtsense(sc, 0, (flag & FWRITE) ? 0 : TP_WRP)) {
307 /* Bad status, reset the controller. */
308 if (!wtreset(sc))
309 return EIO;
310 if (!wtsense(sc, 1, (flag & FWRITE) ? 0 : TP_WRP))
311 return EIO;
312 }
313
314 /* Set up tape density. */
315 if (sc->dens != (minor(dev) & WT_DENSEL)) {
316 int d = 0;
317
318 switch (minor(dev) & WT_DENSEL) {
319 case WT_DENSDFLT:
320 default:
321 break; /* default density */
322 case WT_QIC11:
323 d = QIC_FMT11; break; /* minor 010 */
324 case WT_QIC24:
325 d = QIC_FMT24; break; /* minor 020 */
326 case WT_QIC120:
327 d = QIC_FMT120; break; /* minor 030 */
328 case WT_QIC150:
329 d = QIC_FMT150; break; /* minor 040 */
330 case WT_QIC300:
331 d = QIC_FMT300; break; /* minor 050 */
332 case WT_QIC600:
333 d = QIC_FMT600; break; /* minor 060 */
334 }
335 if (d) {
336 /* Change tape density. */
337 if (!wtcmd(sc, d))
338 return EIO;
339 if (!wtsense(sc, 1, TP_WRP | TP_ILL))
340 return EIO;
341
342 /* Check the status of the controller. */
343 if (sc->error & TP_ILL) {
344 printf("%s: invalid tape density\n",
345 sc->sc_dev.dv_xname);
346 return ENODEV;
347 }
348 }
349 sc->dens = minor(dev) & WT_DENSEL;
350 }
351 sc->flags &= ~TPSTART;
352 } else if (sc->dens != (minor(dev) & WT_DENSEL))
353 return ENXIO;
354
355 sc->bsize = (minor(dev) & WT_BSIZE) ? 1024 : 512;
356 sc->buf = malloc(sc->bsize, M_TEMP, M_WAITOK);
357
358 sc->flags = TPINUSE;
359 if (flag & FREAD)
360 sc->flags |= TPREAD;
361 if (flag & FWRITE)
362 sc->flags |= TPWRITE;
363 return 0;
364 }
365
366 /*
367 * Close routine, called on last device close.
368 */
369 int
370 wtclose(dev)
371 dev_t dev;
372 {
373 int unit = minor(dev) & T_UNIT;
374 struct wt_softc *sc = wtcd.cd_devs[unit];
375
376 /* If rewind is pending, do nothing */
377 if (sc->flags & TPREW)
378 goto done;
379
380 /* If seek forward is pending and no rewind on close, do nothing */
381 if (sc->flags & TPRMARK) {
382 if (minor(dev) & T_NOREWIND)
383 goto done;
384
385 /* If read file mark is going on, wait */
386 wtwait(sc, 0, "wtrfm");
387 }
388
389 if (sc->flags & TPWANY) {
390 /* Tape was written. Write file mark. */
391 wtwritefm(sc);
392 }
393
394 if ((minor(dev) & T_NOREWIND) == 0) {
395 /* Rewind to beginning of tape. */
396 /* Don't wait until rewind, though. */
397 wtrewind(sc);
398 goto done;
399 }
400 if ((sc->flags & TPRANY) && (sc->flags & (TPVOL | TPWANY)) == 0) {
401 /* Space forward to after next file mark if no writing done. */
402 /* Don't wait for completion. */
403 wtreadfm(sc);
404 }
405
406 done:
407 sc->flags &= TPREW | TPRMARK | TPSTART | TPTIMER;
408 free(sc->buf, M_TEMP);
409 return 0;
410 }
411
412 /*
413 * Ioctl routine. Compatible with BSD ioctls.
414 * Direct QIC-02 commands ERASE and RETENSION added.
415 * There are three possible ioctls:
416 * ioctl(int fd, MTIOCGET, struct mtget *buf) -- get status
417 * ioctl(int fd, MTIOCTOP, struct mtop *buf) -- do BSD-like op
418 * ioctl(int fd, WTQICMD, int qicop) -- do QIC op
419 */
420 int
421 wtioctl(dev, cmd, addr, flag)
422 dev_t dev;
423 u_long cmd;
424 void *addr;
425 int flag;
426 {
427 int unit = minor(dev) & T_UNIT;
428 struct wt_softc *sc = wtcd.cd_devs[unit];
429 int error, count, op;
430
431 switch (cmd) {
432 default:
433 return EINVAL;
434 case WTQICMD: /* direct QIC command */
435 op = *(int *)addr;
436 switch (op) {
437 default:
438 return EINVAL;
439 case QIC_ERASE: /* erase the whole tape */
440 if ((sc->flags & TPWRITE) == 0 || (sc->flags & TPWP))
441 return EACCES;
442 if (error = wtwait(sc, PCATCH, "wterase"))
443 return error;
444 break;
445 case QIC_RETENS: /* retension the tape */
446 if (error = wtwait(sc, PCATCH, "wtretens"))
447 return error;
448 break;
449 }
450 /* Both ERASE and RETENS operations work like REWIND. */
451 /* Simulate the rewind operation here. */
452 sc->flags &= ~(TPRO | TPWO | TPVOL);
453 if (!wtcmd(sc, op))
454 return EIO;
455 sc->flags |= TPSTART | TPREW;
456 if (op == QIC_ERASE)
457 sc->flags |= TPWANY;
458 wtclock(sc);
459 return 0;
460 case MTIOCIEOT: /* ignore EOT errors */
461 case MTIOCEEOT: /* enable EOT errors */
462 return 0;
463 case MTIOCGET:
464 ((struct mtget*)addr)->mt_type =
465 sc->type == ARCHIVE ? MT_ISVIPER1 : 0x11;
466 ((struct mtget*)addr)->mt_dsreg = sc->flags; /* status */
467 ((struct mtget*)addr)->mt_erreg = sc->error; /* errors */
468 ((struct mtget*)addr)->mt_resid = 0;
469 ((struct mtget*)addr)->mt_fileno = 0; /* file */
470 ((struct mtget*)addr)->mt_blkno = 0; /* block */
471 return 0;
472 case MTIOCTOP:
473 break;
474 }
475
476 switch ((short)((struct mtop*)addr)->mt_op) {
477 default:
478 #if 0
479 case MTFSR: /* forward space record */
480 case MTBSR: /* backward space record */
481 case MTBSF: /* backward space file */
482 #endif
483 return EINVAL;
484 case MTNOP: /* no operation, sets status only */
485 case MTCACHE: /* enable controller cache */
486 case MTNOCACHE: /* disable controller cache */
487 return 0;
488 case MTREW: /* rewind */
489 case MTOFFL: /* rewind and put the drive offline */
490 if (sc->flags & TPREW) /* rewind is running */
491 return 0;
492 if (error = wtwait(sc, PCATCH, "wtorew"))
493 return error;
494 wtrewind(sc);
495 return 0;
496 case MTFSF: /* forward space file */
497 for (count = ((struct mtop*)addr)->mt_count; count > 0;
498 --count) {
499 if (error = wtwait(sc, PCATCH, "wtorfm"))
500 return error;
501 if (error = wtreadfm(sc))
502 return error;
503 }
504 return 0;
505 case MTWEOF: /* write an end-of-file record */
506 if ((sc->flags & TPWRITE) == 0 || (sc->flags & TPWP))
507 return EACCES;
508 if (error = wtwait(sc, PCATCH, "wtowfm"))
509 return error;
510 if (error = wtwritefm(sc))
511 return error;
512 return 0;
513 }
514
515 #ifdef DIAGNOSTIC
516 panic("wtioctl: impossible");
517 #endif
518 }
519
520 /*
521 * Strategy routine.
522 */
523 void
524 wtstrategy(bp)
525 struct buf *bp;
526 {
527 int unit = minor(bp->b_dev) & T_UNIT;
528 struct wt_softc *sc = wtcd.cd_devs[unit];
529 int s;
530
531 bp->b_resid = bp->b_bcount;
532
533 /* at file marks and end of tape, we just return '0 bytes available' */
534 if (sc->flags & TPVOL)
535 goto xit;
536
537 if (bp->b_flags & B_READ) {
538 /* Check read access and no previous write to this tape. */
539 if ((sc->flags & TPREAD) == 0 || (sc->flags & TPWANY))
540 goto errxit;
541
542 /* For now, we assume that all data will be copied out */
543 /* If read command outstanding, just skip down */
544 if ((sc->flags & TPRO) == 0) {
545 if (!wtsense(sc, 1, TP_WRP)) {
546 /* Clear status. */
547 goto errxit;
548 }
549 if (!wtcmd(sc, QIC_RDDATA)) {
550 /* Set read mode. */
551 wtsense(sc, 1, TP_WRP);
552 goto errxit;
553 }
554 sc->flags |= TPRO | TPRANY;
555 }
556 } else {
557 /* Check write access and write protection. */
558 /* No previous read from this tape allowed. */
559 if ((sc->flags & TPWRITE) == 0 || (sc->flags & (TPWP | TPRANY)))
560 goto errxit;
561
562 /* If write command outstanding, just skip down */
563 if ((sc->flags & TPWO) == 0) {
564 if (!wtsense(sc, 1, 0)) {
565 /* Clear status. */
566 goto errxit;
567 }
568 if (!wtcmd(sc, QIC_WRTDATA)) {
569 /* Set write mode. */
570 wtsense(sc, 1, 0);
571 goto errxit;
572 }
573 sc->flags |= TPWO | TPWANY;
574 }
575 }
576
577 if (bp->b_bcount == 0)
578 goto xit;
579
580 sc->flags &= ~TPEXCEP;
581 s = splbio();
582 if (wtstart(sc, bp->b_flags, bp->b_data, bp->b_bcount)) {
583 wtwait(sc, 0, (bp->b_flags & B_READ) ? "wtread" : "wtwrite");
584 bp->b_resid -= sc->dmacount;
585 }
586 splx(s);
587
588 if (sc->flags & TPEXCEP) {
589 errxit:
590 bp->b_flags |= B_ERROR;
591 bp->b_error = EIO;
592 }
593 xit:
594 biodone(bp);
595 return;
596 }
597
598 /*
599 * Interrupt routine.
600 */
601 int
602 wtintr(arg)
603 void *arg;
604 {
605 struct wt_softc *sc = arg;
606 u_char x;
607
608 x = inb(sc->STATPORT); /* get status */
609 DEBUG(("wtintr() status=0x%x -- ", x));
610 if ((x & (sc->BUSY | sc->NOEXCEP)) == (sc->BUSY | sc->NOEXCEP)) {
611 DEBUG(("busy\n"));
612 return 0; /* device is busy */
613 }
614
615 /*
616 * Check if rewind finished.
617 */
618 if (sc->flags & TPREW) {
619 DEBUG(((x & (sc->BUSY | sc->NOEXCEP)) == (sc->BUSY | sc->NOEXCEP) ?
620 "rewind busy?\n" : "rewind finished\n"));
621 sc->flags &= ~TPREW; /* rewind finished */
622 wtsense(sc, 1, TP_WRP);
623 wakeup((caddr_t)sc);
624 return 1;
625 }
626
627 /*
628 * Check if writing/reading of file mark finished.
629 */
630 if (sc->flags & (TPRMARK | TPWMARK)) {
631 DEBUG(((x & (sc->BUSY | sc->NOEXCEP)) == (sc->BUSY | sc->NOEXCEP) ?
632 "marker r/w busy?\n" : "marker r/w finished\n"));
633 if ((x & sc->NOEXCEP) == 0) /* operation failed */
634 wtsense(sc, 1, (sc->flags & TPRMARK) ? TP_WRP : 0);
635 sc->flags &= ~(TPRMARK | TPWMARK); /* operation finished */
636 wakeup((caddr_t)sc);
637 return 1;
638 }
639
640 /*
641 * Do we started any i/o? If no, just return.
642 */
643 if ((sc->flags & TPACTIVE) == 0) {
644 DEBUG(("unexpected interrupt\n"));
645 return 0;
646 }
647 sc->flags &= ~TPACTIVE;
648 sc->dmacount += sc->bsize; /* increment counter */
649
650 /*
651 * Clean up dma.
652 */
653 if ((sc->dmaflags & B_READ) &&
654 (sc->dmatotal - sc->dmacount) < sc->bsize) {
655 /* If reading short block, copy the internal buffer
656 * to the user memory. */
657 isa_dmadone(sc->dmaflags, sc->buf, sc->bsize, sc->chan);
658 bcopy(sc->buf, sc->dmavaddr, sc->dmatotal - sc->dmacount);
659 } else
660 isa_dmadone(sc->dmaflags, sc->dmavaddr, sc->bsize, sc->chan);
661
662 /*
663 * On exception, check for end of file and end of volume.
664 */
665 if ((x & sc->NOEXCEP) == 0) {
666 DEBUG(("i/o exception\n"));
667 wtsense(sc, 1, (sc->dmaflags & B_READ) ? TP_WRP : 0);
668 if (sc->error & (TP_EOM | TP_FIL))
669 sc->flags |= TPVOL; /* end of file */
670 else
671 sc->flags |= TPEXCEP; /* i/o error */
672 wakeup((caddr_t)sc);
673 return 1;
674 }
675
676 if (sc->dmacount < sc->dmatotal) {
677 /* Continue I/O. */
678 sc->dmavaddr += sc->bsize;
679 wtdma(sc);
680 DEBUG(("continue i/o, %d\n", sc->dmacount));
681 return 1;
682 }
683 if (sc->dmacount > sc->dmatotal) /* short last block */
684 sc->dmacount = sc->dmatotal;
685 /* Wake up user level. */
686 wakeup((caddr_t)sc);
687 DEBUG(("i/o finished, %d\n", sc->dmacount));
688 return 1;
689 }
690
691 /* start the rewind operation */
692 void
693 wtrewind(sc)
694 struct wt_softc *sc;
695 {
696 int rwmode = sc->flags & (TPRO | TPWO);
697
698 sc->flags &= ~(TPRO | TPWO | TPVOL);
699 /*
700 * Wangtek strictly follows QIC-02 standard:
701 * clearing ONLINE in read/write modes causes rewind.
702 * REWIND command is not allowed in read/write mode
703 * and gives `illegal command' error.
704 */
705 if (sc->type == WANGTEK && rwmode) {
706 outb(sc->CTLPORT, 0);
707 } else if (!wtcmd(sc, QIC_REWIND))
708 return;
709 sc->flags |= TPSTART | TPREW;
710 wtclock(sc);
711 }
712
713 /*
714 * Start the `read marker' operation.
715 */
716 int
717 wtreadfm(sc)
718 struct wt_softc *sc;
719 {
720
721 sc->flags &= ~(TPRO | TPWO | TPVOL);
722 if (!wtcmd(sc, QIC_READFM)) {
723 wtsense(sc, 1, TP_WRP);
724 return EIO;
725 }
726 sc->flags |= TPRMARK | TPRANY;
727 wtclock(sc);
728 /* Don't wait for completion here. */
729 return 0;
730 }
731
732 /*
733 * Write marker to the tape.
734 */
735 int
736 wtwritefm(sc)
737 struct wt_softc *sc;
738 {
739
740 tsleep((caddr_t)wtwritefm, WTPRI, "wtwfm", hz);
741 sc->flags &= ~(TPRO | TPWO);
742 if (!wtcmd(sc, QIC_WRITEFM)) {
743 wtsense(sc, 1, 0);
744 return EIO;
745 }
746 sc->flags |= TPWMARK | TPWANY;
747 wtclock(sc);
748 return wtwait(sc, 0, "wtwfm");
749 }
750
751 /*
752 * While controller status & mask == bits continue waiting.
753 */
754 u_char
755 wtpoll(sc, mask, bits)
756 struct wt_softc *sc;
757 int mask, bits;
758 {
759 u_char x;
760 int i;
761
762 /* Poll status port, waiting for specified bits. */
763 for (i = 0; i < 1000; ++i) { /* up to 1 msec */
764 x = inb(sc->STATPORT);
765 if ((x & mask) != bits)
766 return x;
767 delay(1);
768 }
769 for (i = 0; i < 100; ++i) { /* up to 10 msec */
770 x = inb(sc->STATPORT);
771 if ((x & mask) != bits)
772 return x;
773 delay(100);
774 }
775 for (;;) { /* forever */
776 x = inb(sc->STATPORT);
777 if ((x & mask) != bits)
778 return x;
779 tsleep((caddr_t)wtpoll, WTPRI, "wtpoll", 1);
780 }
781 }
782
783 /*
784 * Execute QIC command.
785 */
786 int
787 wtcmd(sc, cmd)
788 struct wt_softc *sc;
789 int cmd;
790 {
791 u_char x;
792 int s;
793
794 DEBUG(("wtcmd() cmd=0x%x\n", cmd));
795 s = splbio();
796 x = wtpoll(sc, sc->BUSY | sc->NOEXCEP, sc->BUSY | sc->NOEXCEP); /* ready? */
797 if ((x & sc->NOEXCEP) == 0) { /* error */
798 splx(s);
799 return 0;
800 }
801
802 outb(sc->CMDPORT, cmd); /* output the command */
803
804 outb(sc->CTLPORT, sc->REQUEST | sc->ONLINE); /* set request */
805 wtpoll(sc, sc->BUSY, sc->BUSY); /* wait for ready */
806 outb(sc->CTLPORT, sc->IEN | sc->ONLINE); /* reset request */
807 wtpoll(sc, sc->BUSY, 0); /* wait for not ready */
808 splx(s);
809 return 1;
810 }
811
812 /* wait for the end of i/o, seeking marker or rewind operation */
813 int
814 wtwait(sc, catch, msg)
815 struct wt_softc *sc;
816 int catch;
817 char *msg;
818 {
819 int error;
820
821 DEBUG(("wtwait() `%s'\n", msg));
822 while (sc->flags & (TPACTIVE | TPREW | TPRMARK | TPWMARK))
823 if (error = tsleep((caddr_t)sc, WTPRI | catch, msg, 0))
824 return error;
825 return 0;
826 }
827
828 /* initialize dma for the i/o operation */
829 void
830 wtdma(sc)
831 struct wt_softc *sc;
832 {
833
834 sc->flags |= TPACTIVE;
835 wtclock(sc);
836
837 if (sc->type == ARCHIVE) {
838 /* Set DMA. */
839 outb(sc->SDMAPORT, 0);
840 }
841
842 if ((sc->dmaflags & B_READ) &&
843 (sc->dmatotal - sc->dmacount) < sc->bsize) {
844 /* Reading short block; do it through the internal buffer. */
845 isa_dmastart(sc->dmaflags, sc->buf, sc->bsize, sc->chan);
846 } else
847 isa_dmastart(sc->dmaflags, sc->dmavaddr, sc->bsize, sc->chan);
848 }
849
850 /* start i/o operation */
851 int
852 wtstart(sc, flag, vaddr, len)
853 struct wt_softc *sc;
854 int flag;
855 void *vaddr;
856 size_t len;
857 {
858 u_char x;
859
860 DEBUG(("wtstart()\n"));
861 x = wtpoll(sc, sc->BUSY | sc->NOEXCEP, sc->BUSY | sc->NOEXCEP); /* ready? */
862 if ((x & sc->NOEXCEP) == 0) {
863 sc->flags |= TPEXCEP; /* error */
864 return 0;
865 }
866 sc->flags &= ~TPEXCEP; /* clear exception flag */
867 sc->dmavaddr = vaddr;
868 sc->dmatotal = len;
869 sc->dmacount = 0;
870 sc->dmaflags = flag;
871 wtdma(sc);
872 return 1;
873 }
874
875 /*
876 * Start timer.
877 */
878 void
879 wtclock(sc)
880 struct wt_softc *sc;
881 {
882
883 if (sc->flags & TPTIMER)
884 return;
885 sc->flags |= TPTIMER;
886 /*
887 * Some controllers seem to lose dma interrupts too often. To make the
888 * tape stream we need 1 tick timeout.
889 */
890 timeout(wttimer, sc, (sc->flags & TPACTIVE) ? 1 : hz);
891 }
892
893 /*
894 * Simulate an interrupt periodically while i/o is going.
895 * This is necessary in case interrupts get eaten due to
896 * multiple devices on a single IRQ line.
897 */
898 void
899 wttimer(arg)
900 void *arg;
901 {
902 struct wt_softc *sc = (struct wt_softc *)arg;
903 int s;
904
905 sc->flags &= ~TPTIMER;
906 if ((sc->flags & (TPACTIVE | TPREW | TPRMARK | TPWMARK)) == 0)
907 return;
908
909 /* If i/o going, simulate interrupt. */
910 s = splbio();
911 if ((inb(sc->STATPORT) & (sc->BUSY | sc->NOEXCEP)) != (sc->BUSY | sc->NOEXCEP)) {
912 DEBUG(("wttimer() -- "));
913 wtintr(sc);
914 }
915 splx(s);
916
917 /* Restart timer if i/o pending. */
918 if (sc->flags & (TPACTIVE | TPREW | TPRMARK | TPWMARK))
919 wtclock(sc);
920 }
921
922 /*
923 * Perform QIC-02 and QIC-36 compatible reset sequence.
924 */
925 int
926 wtreset(sc)
927 struct wt_softc *sc;
928 {
929 u_char x;
930 int i;
931
932 outb(sc->CTLPORT, sc->RESET | sc->ONLINE); /* send reset */
933 delay(30);
934 outb(sc->CTLPORT, sc->ONLINE); /* turn off reset */
935 delay(30);
936
937 /* Read the controller status. */
938 x = inb(sc->STATPORT);
939 if (x == 0xff) /* no port at this address? */
940 return 0;
941
942 /* Wait 3 sec for reset to complete. Needed for QIC-36 boards? */
943 for (i = 0; i < 3000; ++i) {
944 if ((x & sc->BUSY) == 0 || (x & sc->NOEXCEP) == 0)
945 break;
946 delay(1000);
947 x = inb(sc->STATPORT);
948 }
949 return (x & sc->RESETMASK) == sc->RESETVAL;
950 }
951
952 /*
953 * Get controller status information. Return 0 if user i/o request should
954 * receive an i/o error code.
955 */
956 int
957 wtsense(sc, verbose, ignore)
958 struct wt_softc *sc;
959 int verbose, ignore;
960 {
961 char *msg = 0;
962 int error;
963
964 DEBUG(("wtsense() ignore=0x%x\n", ignore));
965 sc->flags &= ~(TPRO | TPWO);
966 if (!wtstatus(sc))
967 return 0;
968 if ((sc->error & TP_ST0) == 0)
969 sc->error &= ~TP_ST0MASK;
970 if ((sc->error & TP_ST1) == 0)
971 sc->error &= ~TP_ST1MASK;
972 sc->error &= ~ignore; /* ignore certain errors */
973 error = sc->error & (TP_FIL | TP_BNL | TP_UDA | TP_EOM | TP_WRP |
974 TP_USL | TP_CNI | TP_MBD | TP_NDT | TP_ILL);
975 if (!error)
976 return 1;
977 if (!verbose)
978 return 0;
979
980 /* lifted from tdriver.c from Wangtek */
981 if (error & TP_USL)
982 msg = "Drive not online";
983 else if (error & TP_CNI)
984 msg = "No cartridge";
985 else if ((error & TP_WRP) && (sc->flags & TPWP) == 0) {
986 msg = "Tape is write protected";
987 sc->flags |= TPWP;
988 } else if (error & TP_FIL)
989 msg = 0 /*"Filemark detected"*/;
990 else if (error & TP_EOM)
991 msg = 0 /*"End of tape"*/;
992 else if (error & TP_BNL)
993 msg = "Block not located";
994 else if (error & TP_UDA)
995 msg = "Unrecoverable data error";
996 else if (error & TP_NDT)
997 msg = "No data detected";
998 else if (error & TP_ILL)
999 msg = "Illegal command";
1000 if (msg)
1001 printf("%s: %s\n", sc->sc_dev.dv_xname, msg);
1002 return 0;
1003 }
1004
1005 /*
1006 * Get controller status information.
1007 */
1008 int
1009 wtstatus(sc)
1010 struct wt_softc *sc;
1011 {
1012 char *p;
1013 int s;
1014
1015 s = splbio();
1016 wtpoll(sc, sc->BUSY | sc->NOEXCEP, sc->BUSY | sc->NOEXCEP); /* ready? */
1017 outb(sc->CMDPORT, QIC_RDSTAT); /* send `read status' command */
1018
1019 outb(sc->CTLPORT, sc->REQUEST | sc->ONLINE); /* set request */
1020 wtpoll(sc, sc->BUSY, sc->BUSY); /* wait for ready */
1021 outb(sc->CTLPORT, sc->ONLINE); /* reset request */
1022 wtpoll(sc, sc->BUSY, 0); /* wait for not ready */
1023
1024 p = (char *)&sc->error;
1025 while (p < (char *)&sc->error + 6) {
1026 u_char x = wtpoll(sc, sc->BUSY | sc->NOEXCEP, sc->BUSY | sc->NOEXCEP);
1027 if ((x & sc->NOEXCEP) == 0) { /* error */
1028 splx(s);
1029 return 0;
1030 }
1031
1032 *p++ = inb(sc->DATAPORT); /* read status byte */
1033
1034 outb(sc->CTLPORT, sc->REQUEST | sc->ONLINE); /* set request */
1035 wtpoll(sc, sc->BUSY, 0); /* wait for not ready */
1036 outb(sc->CTLPORT, sc->ONLINE); /* unset request */
1037 }
1038 splx(s);
1039 return 1;
1040 }
1041