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