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