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