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