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