Remove the priority part of the priority|flags argument to tsleep(). Only
[dragonfly.git] / sys / dev / disk / wt / wt.c
1
2 /*
3  * Streamer tape driver for 386bsd and FreeBSD.
4  * Supports Archive and Wangtek compatible QIC-02/QIC-36 boards.
5  *
6  * Copyright (C) 1993 by:
7  *      Sergey Ryzhkov       <sir@kiae.su>
8  *      Serge Vakulenko      <vak@zebub.msk.su>
9  *
10  * This software is distributed with NO WARRANTIES, not even the implied
11  * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  *
13  * Authors grant any other persons or organisations permission to use
14  * or modify this software as long as this message is kept with the software,
15  * all derivative works or modified versions.
16  *
17  * This driver is derived from the old 386bsd Wangtek streamer tape driver,
18  * made by Robert Baron at CMU, based on Intel sources.
19  * Authors thank Robert Baron, CMU and Intel and retain here
20  * the original CMU copyright notice.
21  *
22  * Version 1.3, Thu Nov 11 12:09:13 MSK 1993
23  * $FreeBSD: src/sys/i386/isa/wt.c,v 1.57.2.1 2000/08/08 19:49:53 peter Exp $
24  * $DragonFly: src/sys/dev/disk/wt/wt.c,v 1.3 2003/07/19 21:14:34 dillon Exp $
25  *
26  */
27
28 /*
29  * Code for MTERASE added by John Lind (john@starfire.mn.org) 95/09/02.
30  * This was very easy due to the excellent structure and clear coding
31  * of the original driver.
32  */
33
34 /*
35  * Copyright (c) 1989 Carnegie-Mellon University.
36  * All rights reserved.
37  *
38  * Authors: Robert Baron
39  *
40  * Permission to use, copy, modify and distribute this software and
41  * its documentation is hereby granted, provided that both the copyright
42  * notice and this permission notice appear in all copies of the
43  * software, derivative works or modified versions, and any portions
44  * thereof, and that both notices appear in supporting documentation.
45  *
46  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
47  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
48  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
49  *
50  * Carnegie Mellon requests users of this software to return to
51  *
52  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
53  *  School of Computer Science
54  *  Carnegie Mellon University
55  *  Pittsburgh PA 15213-3890
56  *
57  * any improvements or extensions that they make and grant Carnegie the
58  * rights to redistribute these changes.
59  */
60
61 #include "wt.h"
62
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/kernel.h>
66 #include <sys/buf.h>
67 #include <sys/fcntl.h>
68 #include <sys/malloc.h>
69 #include <sys/mtio.h>
70 #include <sys/conf.h>
71
72 #include <machine/clock.h>
73
74 #include <i386/isa/isa_device.h>
75 #include <i386/isa/wtreg.h>
76
77
78 /*
79  * Uncomment this to enable internal device tracing.
80  */
81 #define TRACE(s)                /* printf s */
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 typedef struct {
133         unsigned short err;             /* code for error encountered */
134         unsigned short ercnt;           /* number of error blocks */
135         unsigned short urcnt;           /* number of underruns */
136 } wtstatus_t;
137
138 typedef struct {
139         enum wttype type;               /* type of controller */
140         unsigned unit;                  /* unit number */
141         unsigned port;                  /* base i/o port */
142         unsigned chan;                  /* dma channel number, 1..3 */
143         unsigned flags;                 /* state of tape drive */
144         unsigned dens;                  /* tape density */
145         int bsize;                      /* tape block size */
146         void *buf;                      /* internal i/o buffer */
147
148         void *dmavaddr;                 /* virtual address of dma i/o buffer */
149         unsigned dmatotal;              /* size of i/o buffer */
150         unsigned dmaflags;              /* i/o direction, ISADMA_READ or ISADMA_WRITE */
151         unsigned dmacount;              /* resulting length of dma i/o */
152
153         wtstatus_t error;               /* status of controller */
154
155         unsigned short DATAPORT, CMDPORT, STATPORT, CTLPORT, SDMAPORT, RDMAPORT;
156         unsigned char BUSY, NOEXCEP, RESETMASK, RESETVAL;
157         unsigned char ONLINE, RESET, REQUEST, IEN;
158 } wtinfo_t;
159
160 static wtinfo_t wttab[NWT];                    /* tape info by unit number */
161
162 static int wtwait (wtinfo_t *t, int catch, char *msg);
163 static int wtcmd (wtinfo_t *t, int cmd);
164 static int wtstart (wtinfo_t *t, unsigned mode, void *vaddr, unsigned len);
165 static void wtdma (wtinfo_t *t);
166 static timeout_t wtimer;
167 static void wtclock (wtinfo_t *t);
168 static int wtreset (wtinfo_t *t);
169 static int wtsense (wtinfo_t *t, int verb, int ignor);
170 static int wtstatus (wtinfo_t *t);
171 static ointhand2_t wtintr;
172 static void wtrewind (wtinfo_t *t);
173 static int wtreadfm (wtinfo_t *t);
174 static int wtwritefm (wtinfo_t *t);
175 static int wtpoll (wtinfo_t *t, int mask, int bits);
176
177 static  d_open_t        wtopen;
178 static  d_close_t       wtclose;
179 static  d_ioctl_t       wtioctl;
180 static  d_strategy_t    wtstrategy;
181
182 #define CDEV_MAJOR 10
183
184 static struct cdevsw wt_cdevsw = {
185         /* open */      wtopen,
186         /* close */     wtclose,
187         /* read */      physread,
188         /* write */     physwrite,
189         /* ioctl */     wtioctl,
190         /* poll */      nopoll,
191         /* mmap */      nommap,
192         /* strategy */  wtstrategy,
193         /* name */      "wt",
194         /* maj */       CDEV_MAJOR,
195         /* dump */      nodump,
196         /* psize */     nopsize,
197         /* flags */     0,
198         /* bmaj */      -1
199 };
200
201
202 /*
203  * Probe for the presence of the device.
204  */
205 static int 
206 wtprobe (struct isa_device *id)
207 {
208         wtinfo_t *t = wttab + id->id_unit;
209         static int once;
210
211         if (!once++)
212                 cdevsw_add(&wt_cdevsw);
213
214         t->unit = id->id_unit;
215         t->chan = id->id_drq;
216         t->port = id->id_iobase;
217         if (t->chan<1 || t->chan>3) {
218                 printf ("wt%d: Bad drq=%d, should be 1..3\n", t->unit, t->chan);
219                 return (0);
220         }
221
222         /* Try Wangtek. */
223         t->type = WANGTEK;
224         t->CTLPORT = WT_CTLPORT (t->port);  t->STATPORT = WT_STATPORT (t->port);
225         t->CMDPORT = WT_CMDPORT (t->port);  t->DATAPORT = WT_DATAPORT (t->port);
226         t->SDMAPORT = 0;                    t->RDMAPORT = 0;
227         t->BUSY = WT_BUSY;                  t->NOEXCEP = WT_NOEXCEP;
228         t->RESETMASK = WT_RESETMASK;        t->RESETVAL = WT_RESETVAL;
229         t->ONLINE = WT_ONLINE;              t->RESET = WT_RESET;
230         t->REQUEST = WT_REQUEST;            t->IEN = WT_IEN;
231         if (wtreset (t))
232                 return (WT_NPORT);
233
234         /* Try Archive. */
235         t->type = ARCHIVE;
236         t->CTLPORT = AV_CTLPORT (t->port);  t->STATPORT = AV_STATPORT (t->port);
237         t->CMDPORT = AV_CMDPORT (t->port);  t->DATAPORT = AV_DATAPORT (t->port);
238         t->SDMAPORT = AV_SDMAPORT (t->port); t->RDMAPORT = AV_RDMAPORT (t->port);
239         t->BUSY = AV_BUSY;                  t->NOEXCEP = AV_NOEXCEP;
240         t->RESETMASK = AV_RESETMASK;        t->RESETVAL = AV_RESETVAL;
241         t->ONLINE = 0;                      t->RESET = AV_RESET;
242         t->REQUEST = AV_REQUEST;            t->IEN = AV_IEN;
243         if (wtreset (t))
244                 return (AV_NPORT);
245
246         /* Tape controller not found. */
247         t->type = UNKNOWN;
248         return (0);
249 }
250
251 /*
252  * Device is found, configure it.
253  */
254 static int
255 wtattach (struct isa_device *id)
256 {
257         wtinfo_t *t = wttab + id->id_unit;
258
259         id->id_ointr = wtintr;
260         if (t->type == ARCHIVE) {
261                 printf ("wt%d: type <Archive>\n", t->unit);
262                 outb (t->RDMAPORT, 0);          /* reset dma */
263         } else
264                 printf ("wt%d: type <Wangtek>\n", t->unit);
265         t->flags = TPSTART;                     /* tape is rewound */
266         t->dens = -1;                           /* unknown density */
267         isa_dmainit(t->chan, 1024);
268
269         make_dev(&wt_cdevsw, id->id_unit, 0, 0, 0600, "rwt%d", id->id_unit);
270         return (1);
271 }
272
273 struct isa_driver wtdriver = { wtprobe, wtattach, "wt", };
274
275 /*
276  * Open routine, called on every device open.
277  */
278 static int
279 wtopen (dev_t dev, int flag, int fmt, struct proc *p)
280 {
281         int u = minor (dev) & T_UNIT;
282         wtinfo_t *t = wttab + u;
283         int error;
284
285         if (u >= NWT || t->type == UNKNOWN)
286                 return (ENXIO);
287
288         /* Check that device is not in use */
289         if (t->flags & TPINUSE)
290                 return (EBUSY);
291
292         /* If the tape is in rewound state, check the status and set density. */
293         if (t->flags & TPSTART) {
294                 /* If rewind is going on, wait */
295                 error = wtwait (t, PCATCH, "wtrew");
296                 if (error)
297                         return (error);
298
299                 /* Check the controller status */
300                 if (! wtsense (t, 0, (flag & FWRITE) ? 0 : TP_WRP)) {
301                         /* Bad status, reset the controller */
302                         if (! wtreset (t))
303                                 return (EIO);
304                         if (! wtsense (t, 1, (flag & FWRITE) ? 0 : TP_WRP))
305                                 return (EIO);
306                 }
307
308                 /* Set up tape density. */
309                 if (t->dens != (minor (dev) & WT_DENSEL)) {
310                         int d = 0;
311
312                         switch (minor (dev) & WT_DENSEL) {
313                         case WT_DENSDFLT: default: break; /* default density */
314                         case WT_QIC11:  d = QIC_FMT11;  break; /* minor 010 */
315                         case WT_QIC24:  d = QIC_FMT24;  break; /* minor 020 */
316                         case WT_QIC120: d = QIC_FMT120; break; /* minor 030 */
317                         case WT_QIC150: d = QIC_FMT150; break; /* minor 040 */
318                         case WT_QIC300: d = QIC_FMT300; break; /* minor 050 */
319                         case WT_QIC600: d = QIC_FMT600; break; /* minor 060 */
320                         }
321                         if (d) {
322                                 /* Change tape density. */
323                                 if (! wtcmd (t, d))
324                                         return (EIO);
325                                 if (! wtsense (t, 1, TP_WRP | TP_ILL))
326                                         return (EIO);
327
328                                 /* Check the status of the controller. */
329                                 if (t->error.err & TP_ILL) {
330                                         printf ("wt%d: invalid tape density\n", t->unit);
331                                         return (ENODEV);
332                                 }
333                         }
334                         t->dens = minor (dev) & WT_DENSEL;
335                 }
336                 t->flags &= ~TPSTART;
337         } else if (t->dens != (minor (dev) & WT_DENSEL))
338                 return (ENXIO);
339
340         t->bsize = (minor (dev) & WT_BSIZE) ? 1024 : 512;
341         t->buf = malloc (t->bsize, M_TEMP, M_WAITOK);
342         if (! t->buf)
343                 return (EAGAIN);
344
345         if (isa_dma_acquire(t->chan))
346                 return(EBUSY);
347
348         t->flags = TPINUSE;
349
350         if (flag & FREAD)
351                 t->flags |= TPREAD;
352         if (flag & FWRITE)
353                 t->flags |= TPWRITE;
354         return (0);
355 }
356
357 /*
358  * Close routine, called on last device close.
359  */
360 static int
361 wtclose (dev_t dev, int flags, int fmt, struct proc *p)
362 {
363         int u = minor (dev) & T_UNIT;
364         wtinfo_t *t = wttab + u;
365
366         if (u >= NWT || t->type == UNKNOWN)
367                 return (ENXIO);
368
369         /* If rewind is pending, do nothing */
370         if (t->flags & TPREW)
371                 goto done;
372
373         /* If seek forward is pending and no rewind on close, do nothing */
374         if (t->flags & TPRMARK) {
375                 if (minor (dev) & T_NOREWIND)
376                         goto done;
377
378                 /* If read file mark is going on, wait */
379                 wtwait (t, 0, "wtrfm");
380         }
381
382         if (t->flags & TPWANY)
383                 /* Tape was written.  Write file mark. */
384                 wtwritefm (t);
385
386         if (! (minor (dev) & T_NOREWIND)) {
387                 /* Rewind tape to beginning of tape. */
388                 /* Don't wait until rewind, though. */
389                 wtrewind (t);
390                 goto done;
391         }
392         if ((t->flags & TPRANY) && ! (t->flags & (TPVOL | TPWANY)))
393                 /* Space forward to after next file mark if no writing done. */
394                 /* Don't wait for completion. */
395                 wtreadfm (t);
396 done:
397         t->flags &= TPREW | TPRMARK | TPSTART | TPTIMER;
398         free (t->buf, M_TEMP);
399         isa_dma_release(t->chan);
400         return (0);
401 }
402
403 /*
404  * Ioctl routine.  Compatible with BSD ioctls.
405  * There are two possible ioctls:
406  * ioctl (int fd, MTIOCGET, struct mtget *buf)  -- get status
407  * ioctl (int fd, MTIOCTOP, struct mtop *buf)   -- do BSD-like op
408  */
409 static int
410 wtioctl (dev_t dev, u_long cmd, caddr_t arg, int flags, struct proc *p)
411 {
412         int u = minor (dev) & T_UNIT;
413         wtinfo_t *t = wttab + u;
414         int error, count, op;
415
416         if (u >= NWT || t->type == UNKNOWN)
417                 return (ENXIO);
418
419         switch (cmd) {
420         default:
421                 return (EINVAL);
422         case MTIOCIEOT:         /* ignore EOT errors */
423         case MTIOCEEOT:         /* enable EOT errors */
424                 return (0);
425         case MTIOCGET:
426                 ((struct mtget*)arg)->mt_type =
427                         t->type == ARCHIVE ? MT_ISVIPER1 : 0x11;
428                 ((struct mtget*)arg)->mt_dsreg = t->flags;      /* status */
429                 ((struct mtget*)arg)->mt_erreg = t->error.err;  /* errors */
430                 ((struct mtget*)arg)->mt_resid = 0;
431                 ((struct mtget*)arg)->mt_fileno = 0;            /* file */
432                 ((struct mtget*)arg)->mt_blkno = 0;             /* block */
433                 return (0);
434         case MTIOCTOP:
435                 break;
436         }
437         switch ((short) ((struct mtop*)arg)->mt_op) {
438         default:
439         case MTFSR:             /* forward space record */
440         case MTBSR:             /* backward space record */
441         case MTBSF:             /* backward space file */
442                 break;
443         case MTNOP:             /* no operation, sets status only */
444         case MTCACHE:           /* enable controller cache */
445         case MTNOCACHE:         /* disable controller cache */
446                 return (0);
447         case MTREW:             /* rewind */
448         case MTOFFL:            /* rewind and put the drive offline */
449                 if (t->flags & TPREW)   /* rewind is running */
450                         return (0);
451                 error = wtwait (t, PCATCH, "wtorew");
452                 if (error)
453                         return (error);
454                 wtrewind (t);
455                 return (0);
456         case MTFSF:             /* forward space file */
457                 for (count=((struct mtop*)arg)->mt_count; count>0; --count) {
458                         error = wtwait (t, PCATCH, "wtorfm");
459                         if (error)
460                                 return (error);
461                         error = wtreadfm (t);
462                         if (error)
463                                 return (error);
464                 }
465                 return (0);
466         case MTWEOF:            /* write an end-of-file record */
467                 if (! (t->flags & TPWRITE) || (t->flags & TPWP))
468                         return (EACCES);
469                 error = wtwait (t, PCATCH, "wtowfm");
470                 if (error)
471                         return (error);
472                 error = wtwritefm (t);
473                 if (error)
474                         return (error);
475                 return (0);
476         case MTRETENS:          /* re-tension tape */
477                 error = wtwait (t, PCATCH, "wtretens");
478                 if (error)
479                         return (error);
480                 op = QIC_RETENS;
481                 goto erase_retens;
482                 
483         case MTERASE:           /* erase to EOM */
484                 if (! (t->flags & TPWRITE) || (t->flags & TPWP))
485                         return (EACCES);
486                 error = wtwait (t, PCATCH, "wterase");
487                 if (error)
488                         return (error);
489                 op = QIC_ERASE;
490         erase_retens:
491                 /* ERASE and RETENS operations work like REWIND. */
492                 /* Simulate the rewind operation here. */
493                 t->flags &= ~(TPRO | TPWO | TPVOL);
494                 if (! wtcmd (t, op))
495                         return (EIO);
496                 t->flags |= TPSTART | TPREW;
497                 t->flags |= TPWANY;
498                 wtclock (t);
499                 return (0);
500         }
501         return (EINVAL);
502 }
503
504 /*
505  * Strategy routine.
506  */
507 static void
508 wtstrategy (struct buf *bp)
509 {
510         int u = minor (bp->b_dev) & T_UNIT;
511         wtinfo_t *t = wttab + u;
512         int s;
513
514         bp->b_resid = bp->b_bcount;
515         if (u >= NWT || t->type == UNKNOWN) {
516                 bp->b_error = ENXIO;
517                 goto err2xit;
518         }
519
520         /* at file marks and end of tape, we just return '0 bytes available' */
521         if (t->flags & TPVOL)
522                 goto xit;
523
524         if (bp->b_bcount % t->bsize != 0) {
525                 bp->b_error = EINVAL;
526                 goto err2xit;
527         }
528
529         if (bp->b_flags & B_READ) {
530                 /* Check read access and no previous write to this tape. */
531                 if (! (t->flags & TPREAD) || (t->flags & TPWANY))
532                         goto errxit;
533
534                 /* For now, we assume that all data will be copied out */
535                 /* If read command outstanding, just skip down */
536                 if (! (t->flags & TPRO)) {
537                         if (! wtsense (t, 1, TP_WRP))   /* clear status */
538                                 goto errxit;
539                         if (! wtcmd (t, QIC_RDDATA)) {  /* sed read mode */
540                                 wtsense (t, 1, TP_WRP);
541                                 goto errxit;
542                         }
543                         t->flags |= TPRO | TPRANY;
544                 }
545         } else {
546                 /* Check write access and write protection. */
547                 /* No previous read from this tape allowed. */
548                 if (! (t->flags & TPWRITE) || (t->flags & (TPWP | TPRANY)))
549                         goto errxit;
550
551                 /* If write command outstanding, just skip down */
552                 if (! (t->flags & TPWO)) {
553                         if (! wtsense (t, 1, 0))        /* clear status */
554                                 goto errxit;
555                         if (! wtcmd (t, QIC_WRTDATA)) { /* set write mode */
556                                 wtsense (t, 1, 0);
557                                 goto errxit;
558                         }
559                         t->flags |= TPWO | TPWANY;
560                 }
561         }
562
563         if (! bp->b_bcount)
564                 goto xit;
565
566         t->flags &= ~TPEXCEP;
567         s = splbio ();
568         if (wtstart (t, bp->b_flags & B_READ ? ISADMA_READ : ISADMA_WRITE, bp->b_data, bp->b_bcount)) {
569                 wtwait (t, 0, (bp->b_flags & B_READ) ? "wtread" : "wtwrite");
570                 bp->b_resid -= t->dmacount;
571         }
572         splx (s);
573
574         if (t->flags & TPEXCEP) {
575 errxit:         bp->b_error = EIO;
576 err2xit:        bp->b_flags |= B_ERROR;
577         }
578 xit:    biodone (bp);
579         return;
580 }
581
582 /*
583  * Interrupt routine.
584  */
585 static void
586 wtintr (int u)
587 {
588         wtinfo_t *t = wttab + u;
589         unsigned char s;
590
591         if (u >= NWT || t->type == UNKNOWN) {
592                 TRACE (("wtintr() -- device not configured\n"));
593                 return;
594         }
595
596         s = inb (t->STATPORT);                  /* get status */
597         TRACE (("wtintr() status=0x%x -- ", s));
598         if ((s & (t->BUSY | t->NOEXCEP)) == (t->BUSY | t->NOEXCEP)) {
599                 TRACE (("busy\n"));
600                 return;                         /* device is busy */
601         }
602
603         /*
604          * Check if rewind finished.
605          */
606         if (t->flags & TPREW) {
607                 TRACE (((s & (t->BUSY | t->NOEXCEP)) == (t->BUSY | t->NOEXCEP) ?
608                         "rewind busy?\n" : "rewind finished\n"));
609                 t->flags &= ~TPREW;             /* Rewind finished. */
610                 wtsense (t, 1, TP_WRP);
611                 wakeup ((caddr_t)t);
612                 return;
613         }
614
615         /*
616          * Check if writing/reading of file mark finished.
617          */
618         if (t->flags & (TPRMARK | TPWMARK)) {
619                 TRACE (((s & (t->BUSY | t->NOEXCEP)) == (t->BUSY | t->NOEXCEP) ?
620                         "marker r/w busy?\n" : "marker r/w finished\n"));
621                 if (! (s & t->NOEXCEP))         /* operation failed */
622                         wtsense (t, 1, (t->flags & TPRMARK) ? TP_WRP : 0);
623                 t->flags &= ~(TPRMARK | TPWMARK); /* operation finished */
624                 wakeup ((caddr_t)t);
625                 return;
626         }
627
628         /*
629          * Do we started any i/o?  If no, just return.
630          */
631         if (! (t->flags & TPACTIVE)) {
632                 TRACE (("unexpected interrupt\n"));
633                 return;
634         }
635
636         /*
637          * Clean up dma.
638          */
639         if ((t->dmaflags & ISADMA_READ) && (t->dmatotal - t->dmacount) < t->bsize) {
640                 /* If reading short block, copy the internal buffer
641                  * to the user memory. */
642                 isa_dmadone (t->dmaflags, t->buf, t->bsize, t->chan);
643                 bcopy (t->buf, t->dmavaddr, t->dmatotal - t->dmacount);
644         } else
645                 isa_dmadone (t->dmaflags, t->dmavaddr, t->bsize, t->chan);
646
647         t->flags &= ~TPACTIVE;
648         t->dmacount += t->bsize;
649         t->dmavaddr = (char *)t->dmavaddr + t->bsize;
650
651         /*
652          * On exception, check for end of file and end of volume.
653          */
654         if (! (s & t->NOEXCEP)) {
655                 TRACE (("i/o exception\n"));
656                 wtsense (t, 1, (t->dmaflags & ISADMA_READ) ? TP_WRP : 0);
657                 if (t->error.err & (TP_EOM | TP_FIL))
658                         t->flags |= TPVOL;      /* end of file */
659                 else
660                         t->flags |= TPEXCEP;    /* i/o error */
661                 wakeup ((caddr_t)t);
662                 return;
663         }
664
665         if (t->dmacount < t->dmatotal) {        /* continue i/o */
666                 wtdma (t);
667                 TRACE (("continue i/o, %d\n", t->dmacount));
668                 return;
669         }
670         if (t->dmacount > t->dmatotal)          /* short last block */
671                 t->dmacount = t->dmatotal;
672         wakeup ((caddr_t)t);    /* wake up user level */
673         TRACE (("i/o finished, %d\n", t->dmacount));
674 }
675
676 /* start the rewind operation */
677 static void
678 wtrewind (wtinfo_t *t)
679 {
680         int rwmode = (t->flags & (TPRO | TPWO));
681
682         t->flags &= ~(TPRO | TPWO | TPVOL);
683         /*
684          * Wangtek strictly follows QIC-02 standard:
685          * clearing ONLINE in read/write modes causes rewind.
686          * REWIND command is not allowed in read/write mode
687          * and gives `illegal command' error.
688          */
689         if (t->type==WANGTEK && rwmode) {
690                 outb (t->CTLPORT, 0);
691         } else if (! wtcmd (t, QIC_REWIND))
692                 return;
693         t->flags |= TPSTART | TPREW;
694         wtclock (t);
695 }
696
697 /* start the `read marker' operation */
698 static int
699 wtreadfm (wtinfo_t *t)
700 {
701         t->flags &= ~(TPRO | TPWO | TPVOL);
702         if (! wtcmd (t, QIC_READFM)) {
703                 wtsense (t, 1, TP_WRP);
704                 return (EIO);
705         }
706         t->flags |= TPRMARK | TPRANY;
707         wtclock (t);
708         /* Don't wait for completion here. */
709         return (0);
710 }
711
712 /* write marker to the tape */
713 static int
714 wtwritefm (wtinfo_t *t)
715 {
716         tsleep ((caddr_t)wtwritefm, 0, "wtwfm", hz); /* timeout: 1 second */
717         t->flags &= ~(TPRO | TPWO);
718         if (! wtcmd (t, QIC_WRITEFM)) {
719                 wtsense (t, 1, 0);
720                 return (EIO);
721         }
722         t->flags |= TPWMARK | TPWANY;
723         wtclock (t);
724         return (wtwait (t, 0, "wtwfm"));
725 }
726
727 /* while controller status & mask == bits continue waiting */
728 static int
729 wtpoll (wtinfo_t *t, int mask, int bits)
730 {
731         int s, i;
732
733         /* Poll status port, waiting for specified bits. */
734         for (i=0; i<1000; ++i) {                        /* up to 1 msec */
735                 s = inb (t->STATPORT);
736                 if ((s & mask) != bits)
737                         return (s);
738                 DELAY (1);
739         }
740         for (i=0; i<100; ++i) {                         /* up to 10 msec */
741                 s = inb (t->STATPORT);
742                 if ((s & mask) != bits)
743                         return (s);
744                 DELAY (100);
745         }
746         for (;;) {                                      /* forever */
747                 s = inb (t->STATPORT);
748                 if ((s & mask) != bits)
749                         return (s);
750                 tsleep ((caddr_t)wtpoll, 0, "wtpoll", 1); /* timeout: 1 tick */
751         }
752 }
753
754 /* execute QIC command */
755 static int
756 wtcmd (wtinfo_t *t, int cmd)
757 {
758         int s, x;
759
760         TRACE (("wtcmd() cmd=0x%x\n", cmd));
761         x = splbio();
762         s = wtpoll (t, t->BUSY | t->NOEXCEP, t->BUSY | t->NOEXCEP); /* ready? */
763         if (! (s & t->NOEXCEP)) {                       /* error */
764                 splx(x);
765                 return (0);
766         }
767
768         outb (t->CMDPORT, cmd);                         /* output the command */
769
770         outb (t->CTLPORT, t->REQUEST | t->ONLINE);      /* set request */
771         wtpoll (t, t->BUSY, t->BUSY);                   /* wait for ready */
772         outb (t->CTLPORT, t->IEN | t->ONLINE);          /* reset request */
773         wtpoll (t, t->BUSY, 0);                         /* wait for not ready */
774         splx(x);
775         return (1);
776 }
777
778 /* wait for the end of i/o, seeking marker or rewind operation */
779 static int
780 wtwait (wtinfo_t *t, int catch, char *msg)
781 {
782         int error;
783
784         TRACE (("wtwait() `%s'\n", msg));
785         while (t->flags & (TPACTIVE | TPREW | TPRMARK | TPWMARK)) {
786                 error = tsleep ((caddr_t)t, catch, msg, 0);
787                 if (error)
788                         return (error);
789         }
790         return (0);
791 }
792
793 /* initialize dma for the i/o operation */
794 static void
795 wtdma (wtinfo_t *t)
796 {
797         t->flags |= TPACTIVE;
798         wtclock (t);
799
800         if (t->type == ARCHIVE)
801                 outb (t->SDMAPORT, 0);          /* set dma */
802
803         if ((t->dmaflags & ISADMA_READ) && (t->dmatotal - t->dmacount) < t->bsize)
804                 /* Reading short block.  Do it through the internal buffer. */
805                 isa_dmastart (t->dmaflags, t->buf, t->bsize, t->chan);
806         else
807                 isa_dmastart (t->dmaflags, t->dmavaddr, t->bsize, t->chan);
808 }
809
810 /* start i/o operation */
811 static int
812 wtstart (wtinfo_t *t, unsigned flags, void *vaddr, unsigned len)
813 {
814         int s, x;
815
816         TRACE (("wtstart()\n"));
817         x = splbio();
818         s = wtpoll (t, t->BUSY | t->NOEXCEP, t->BUSY | t->NOEXCEP); /* ready? */
819         if (! (s & t->NOEXCEP)) {
820                 t->flags |= TPEXCEP;            /* error */
821                 splx(x);
822                 return (0);
823         }
824         t->flags &= ~TPEXCEP;                   /* clear exception flag */
825         t->dmavaddr = vaddr;
826         t->dmatotal = len;
827         t->dmacount = 0;
828         t->dmaflags = flags;
829         wtdma (t);
830         splx(x);
831         return (1);
832 }
833
834 /* start timer */
835 static void
836 wtclock (wtinfo_t *t)
837 {
838         if (! (t->flags & TPTIMER)) {
839                 t->flags |= TPTIMER;
840                 /* Some controllers seem to lose dma interrupts too often.
841                  * To make the tape stream we need 1 tick timeout. */
842                 timeout (wtimer, (caddr_t)t, (t->flags & TPACTIVE) ? 1 : hz);
843         }
844 }
845
846 /*
847  * Simulate an interrupt periodically while i/o is going.
848  * This is necessary in case interrupts get eaten due to
849  * multiple devices on a single IRQ line.
850  */
851 static void
852 wtimer (void *xt)
853 {
854         wtinfo_t *t = (wtinfo_t *)xt;
855         int s;
856
857         t->flags &= ~TPTIMER;
858         if (! (t->flags & (TPACTIVE | TPREW | TPRMARK | TPWMARK)))
859                 return;
860
861         /* If i/o going, simulate interrupt. */
862         s = splbio ();
863         if ((inb (t->STATPORT) & (t->BUSY | t->NOEXCEP)) != (t->BUSY | t->NOEXCEP)) {
864                 TRACE (("wtimer() -- "));
865                 wtintr (t->unit);
866         }
867         splx (s);
868
869         /* Restart timer if i/o pending. */
870         if (t->flags & (TPACTIVE | TPREW | TPRMARK | TPWMARK))
871                 wtclock (t);
872 }
873
874 /* reset the controller */
875 static int
876 wtreset (wtinfo_t *t)
877 {
878         /* Perform QIC-02 and QIC-36 compatible reset sequence. */
879         /* Thanks to Mikael Hybsch <micke@dynas.se>. */
880         int s, i;
881
882         outb (t->CTLPORT, t->RESET | t->ONLINE); /* send reset */
883         DELAY (30);
884         outb (t->CTLPORT, t->ONLINE);           /* turn off reset */
885         DELAY (30);
886
887         /* Read the controller status. */
888         s = inb (t->STATPORT);
889         if (s == 0xff)                          /* no port at this address? */
890                 return (0);
891
892         /* Wait 3 sec for reset to complete. Needed for QIC-36 boards? */
893         for (i=0; i<3000; ++i) {
894                 if (! (s & t->BUSY) || ! (s & t->NOEXCEP))
895                         break;
896                 DELAY (1000);
897                 s = inb (t->STATPORT);
898         }
899         return ((s & t->RESETMASK) == t->RESETVAL);
900 }
901
902 /* get controller status information */
903 /* return 0 if user i/o request should receive an i/o error code */
904 static int
905 wtsense (wtinfo_t *t, int verb, int ignor)
906 {
907         char *msg = 0;
908         int err;
909
910         TRACE (("wtsense() ignor=0x%x\n", ignor));
911         t->flags &= ~(TPRO | TPWO);
912         if (! wtstatus (t))
913                 return (0);
914         if (! (t->error.err & TP_ST0))
915                 t->error.err &= ~TP_ST0MASK;
916         if (! (t->error.err & TP_ST1))
917                 t->error.err &= ~TP_ST1MASK;
918         t->error.err &= ~ignor;         /* ignore certain errors */
919         err = t->error.err & (TP_FIL | TP_BNL | TP_UDA | TP_EOM | TP_WRP |
920                 TP_USL | TP_CNI | TP_MBD | TP_NDT | TP_ILL);
921         if (! err)
922                 return (1);
923         if (! verb)
924                 return (0);
925
926         /* lifted from tdriver.c from Wangtek */
927         if      (err & TP_USL)  msg = "Drive not online";
928         else if (err & TP_CNI)  msg = "No cartridge";
929         else if ((err & TP_WRP) && !(t->flags & TPWP)) {
930                 msg = "Tape is write protected";
931                 t->flags |= TPWP;
932         }
933         else if (err & TP_FIL)  msg = 0 /*"Filemark detected"*/;
934         else if (err & TP_EOM)  msg = 0 /*"End of tape"*/;
935         else if (err & TP_BNL)  msg = "Block not located";
936         else if (err & TP_UDA)  msg = "Unrecoverable data error";
937         else if (err & TP_NDT)  msg = "No data detected";
938         else if (err & TP_ILL)  msg = "Illegal command";
939         if (msg)
940                 printf ("wt%d: %s\n", t->unit, msg);
941         return (0);
942 }
943
944 /* get controller status information */
945 static int
946 wtstatus (wtinfo_t *t)
947 {
948         char *p;
949         int x;
950
951         x = splbio();
952         wtpoll (t, t->BUSY | t->NOEXCEP, t->BUSY | t->NOEXCEP); /* ready? */
953         outb (t->CMDPORT, QIC_RDSTAT);  /* send `read status' command */
954
955         outb (t->CTLPORT, t->REQUEST | t->ONLINE);      /* set request */
956         wtpoll (t, t->BUSY, t->BUSY);                   /* wait for ready */
957         outb (t->CTLPORT, t->ONLINE);                   /* reset request */
958         wtpoll (t, t->BUSY, 0);                         /* wait for not ready */
959
960         p = (char*) &t->error;
961         while (p < (char*)&t->error + 6) {
962                 int s = wtpoll (t, t->BUSY | t->NOEXCEP, t->BUSY | t->NOEXCEP);
963                 if (! (s & t->NOEXCEP)) {               /* error */
964                         splx(x);
965                         return (0);
966                 }
967
968                 *p++ = inb (t->DATAPORT);               /* read status byte */
969
970                 outb (t->CTLPORT, t->REQUEST | t->ONLINE); /* set request */
971                 wtpoll (t, t->BUSY, 0);                 /* wait for not ready */
972                 DELAY(20);
973                 outb (t->CTLPORT, t->ONLINE);           /* unset request */
974         }
975         splx(x);
976         return (1);
977 }