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