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