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