FreeBSD/Linux Kernel Cross Reference
sys/i386/isa/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$
24 *
25 */
26
27 /*
28 * Code for MTERASE added by John Lind (john@starfire.mn.org) 95/09/02.
29 * This was very easy due to the excellent structure and clear coding
30 * of the original driver.
31 */
32
33 /*
34 * Copyright (c) 1989 Carnegie-Mellon University.
35 * All rights reserved.
36 *
37 * Authors: Robert Baron
38 *
39 * Permission to use, copy, modify and distribute this software and
40 * its documentation is hereby granted, provided that both the copyright
41 * notice and this permission notice appear in all copies of the
42 * software, derivative works or modified versions, and any portions
43 * thereof, and that both notices appear in supporting documentation.
44 *
45 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
46 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
47 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
48 *
49 * Carnegie Mellon requests users of this software to return to
50 *
51 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
52 * School of Computer Science
53 * Carnegie Mellon University
54 * Pittsburgh PA 15213-3890
55 *
56 * any improvements or extensions that they make and grant Carnegie the
57 * rights to redistribute these changes.
58 */
59
60 #include "wt.h"
61 #if NWT > 0
62
63 #include "opt_devfs.h"
64
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/kernel.h>
68 #include <sys/buf.h>
69 #include <sys/fcntl.h>
70 #include <sys/malloc.h>
71 #include <sys/mtio.h>
72 #include <sys/conf.h>
73 #ifdef DEVFS
74 #include <sys/devfsext.h>
75 #endif /*DEVFS*/
76
77 #include <machine/clock.h>
78
79 #include <i386/isa/isa_device.h>
80 #include <i386/isa/wtreg.h>
81
82
83 /*
84 * Uncomment this to enable internal device tracing.
85 */
86 #define TRACE(s) /* printf s */
87
88 #define WTPRI (PZERO+10) /* sleep priority */
89
90 /*
91 * Wangtek controller ports
92 */
93 #define WT_CTLPORT(base) ((base)+0) /* control, write only */
94 #define WT_STATPORT(base) ((base)+0) /* status, read only */
95 #define WT_CMDPORT(base) ((base)+1) /* command, write only */
96 #define WT_DATAPORT(base) ((base)+1) /* data, read only */
97 #define WT_NPORT 2 /* 2 i/o ports */
98
99 /* status port bits */
100 #define WT_BUSY 0x01 /* not ready bit define */
101 #define WT_NOEXCEP 0x02 /* no exception bit define */
102 #define WT_RESETMASK 0x07 /* to check after reset */
103 #define WT_RESETVAL 0x05 /* state after reset */
104
105 /* control port bits */
106 #define WT_ONLINE 0x01 /* device selected */
107 #define WT_RESET 0x02 /* reset command */
108 #define WT_REQUEST 0x04 /* request command */
109 #define WT_IEN 0x08 /* enable dma */
110
111 /*
112 * Archive controller ports
113 */
114 #define AV_DATAPORT(base) ((base)+0) /* data, read only */
115 #define AV_CMDPORT(base) ((base)+0) /* command, write only */
116 #define AV_STATPORT(base) ((base)+1) /* status, read only */
117 #define AV_CTLPORT(base) ((base)+1) /* control, write only */
118 #define AV_SDMAPORT(base) ((base)+2) /* start dma */
119 #define AV_RDMAPORT(base) ((base)+3) /* reset dma */
120 #define AV_NPORT 4 /* 4 i/o ports */
121
122 /* status port bits */
123 #define AV_BUSY 0x40 /* not ready bit define */
124 #define AV_NOEXCEP 0x20 /* no exception bit define */
125 #define AV_RESETMASK 0xf8 /* to check after reset */
126 #define AV_RESETVAL 0x50 /* state after reset */
127
128 /* control port bits */
129 #define AV_RESET 0x80 /* reset command */
130 #define AV_REQUEST 0x40 /* request command */
131 #define AV_IEN 0x20 /* enable interrupts */
132
133 enum wttype {
134 UNKNOWN = 0, /* unknown type, driver disabled */
135 ARCHIVE, /* Archive Viper SC499, SC402 etc */
136 WANGTEK /* Wangtek */
137 };
138
139 typedef struct {
140 unsigned short err; /* code for error encountered */
141 unsigned short ercnt; /* number of error blocks */
142 unsigned short urcnt; /* number of underruns */
143 } wtstatus_t;
144
145 typedef struct {
146 enum wttype type; /* type of controller */
147 unsigned unit; /* unit number */
148 unsigned port; /* base i/o port */
149 unsigned chan; /* dma channel number, 1..3 */
150 unsigned flags; /* state of tape drive */
151 unsigned dens; /* tape density */
152 int bsize; /* tape block size */
153 void *buf; /* internal i/o buffer */
154
155 void *dmavaddr; /* virtual address of dma i/o buffer */
156 unsigned dmatotal; /* size of i/o buffer */
157 unsigned dmaflags; /* i/o direction, B_READ or B_WRITE */
158 unsigned dmacount; /* resulting length of dma i/o */
159
160 wtstatus_t error; /* status of controller */
161
162 unsigned short DATAPORT, CMDPORT, STATPORT, CTLPORT, SDMAPORT, RDMAPORT;
163 unsigned char BUSY, NOEXCEP, RESETMASK, RESETVAL;
164 unsigned char ONLINE, RESET, REQUEST, IEN;
165 #ifdef DEVFS
166 void *devfs_token_r;
167 #endif
168 } wtinfo_t;
169
170 static wtinfo_t wttab[NWT]; /* tape info by unit number */
171
172 static int wtwait (wtinfo_t *t, int catch, char *msg);
173 static int wtcmd (wtinfo_t *t, int cmd);
174 static int wtstart (wtinfo_t *t, unsigned mode, void *vaddr, unsigned len);
175 static void wtdma (wtinfo_t *t);
176 static timeout_t wtimer;
177 static void wtclock (wtinfo_t *t);
178 static int wtreset (wtinfo_t *t);
179 static int wtsense (wtinfo_t *t, int verb, int ignor);
180 static int wtstatus (wtinfo_t *t);
181 static ointhand2_t wtintr;
182 static void wtrewind (wtinfo_t *t);
183 static int wtreadfm (wtinfo_t *t);
184 static int wtwritefm (wtinfo_t *t);
185 static int wtpoll (wtinfo_t *t, int mask, int bits);
186
187 static d_open_t wtopen;
188 static d_read_t wtread;
189 static d_write_t wtwrite;
190 static d_close_t wtclose;
191 static d_ioctl_t wtioctl;
192 static d_strategy_t wtstrategy;
193
194 #define CDEV_MAJOR 10
195 #define BDEV_MAJOR 3
196
197
198 static struct cdevsw wt_cdevsw = {
199 wtopen, wtclose, wtread, wtwrite,
200 wtioctl, nostop, nullreset, nodevtotty,
201 seltrue, nommap, wtstrategy, "wt",
202 NULL, -1 };
203
204
205 /*
206 * Probe for the presence of the device.
207 */
208 static int
209 wtprobe (struct isa_device *id)
210 {
211 wtinfo_t *t = wttab + id->id_unit;
212
213 t->unit = id->id_unit;
214 t->chan = id->id_drq;
215 t->port = id->id_iobase;
216 if (t->chan<1 || t->chan>3) {
217 printf ("wt%d: Bad drq=%d, should be 1..3\n", t->unit, t->chan);
218 return (0);
219 }
220
221 /* Try Wangtek. */
222 t->type = WANGTEK;
223 t->CTLPORT = WT_CTLPORT (t->port); t->STATPORT = WT_STATPORT (t->port);
224 t->CMDPORT = WT_CMDPORT (t->port); t->DATAPORT = WT_DATAPORT (t->port);
225 t->SDMAPORT = 0; t->RDMAPORT = 0;
226 t->BUSY = WT_BUSY; t->NOEXCEP = WT_NOEXCEP;
227 t->RESETMASK = WT_RESETMASK; t->RESETVAL = WT_RESETVAL;
228 t->ONLINE = WT_ONLINE; t->RESET = WT_RESET;
229 t->REQUEST = WT_REQUEST; t->IEN = WT_IEN;
230 if (wtreset (t))
231 return (WT_NPORT);
232
233 /* Try Archive. */
234 t->type = ARCHIVE;
235 t->CTLPORT = AV_CTLPORT (t->port); t->STATPORT = AV_STATPORT (t->port);
236 t->CMDPORT = AV_CMDPORT (t->port); t->DATAPORT = AV_DATAPORT (t->port);
237 t->SDMAPORT = AV_SDMAPORT (t->port); t->RDMAPORT = AV_RDMAPORT (t->port);
238 t->BUSY = AV_BUSY; t->NOEXCEP = AV_NOEXCEP;
239 t->RESETMASK = AV_RESETMASK; t->RESETVAL = AV_RESETVAL;
240 t->ONLINE = 0; t->RESET = AV_RESET;
241 t->REQUEST = AV_REQUEST; t->IEN = AV_IEN;
242 if (wtreset (t))
243 return (AV_NPORT);
244
245 /* Tape controller not found. */
246 t->type = UNKNOWN;
247 return (0);
248 }
249
250 /*
251 * Device is found, configure it.
252 */
253 static int
254 wtattach (struct isa_device *id)
255 {
256 wtinfo_t *t = wttab + id->id_unit;
257
258 id->id_ointr = wtintr;
259 if (t->type == ARCHIVE) {
260 printf ("wt%d: type <Archive>\n", t->unit);
261 outb (t->RDMAPORT, 0); /* reset dma */
262 } else
263 printf ("wt%d: type <Wangtek>\n", t->unit);
264 t->flags = TPSTART; /* tape is rewound */
265 t->dens = -1; /* unknown density */
266 isa_dmainit(t->chan, 1024);
267
268 #ifdef DEVFS
269 t->devfs_token_r =
270 devfs_add_devswf(&wt_cdevsw, id->id_unit, DV_CHR, 0, 0,
271 0600, "rwt%d", id->id_unit);
272 #endif
273 return (1);
274 }
275
276 struct isa_driver wtdriver = { wtprobe, wtattach, "wt", };
277
278 /*
279 * Open routine, called on every device open.
280 */
281 static int
282 wtopen (dev_t dev, int flag, int fmt, struct proc *p)
283 {
284 int u = minor (dev) & T_UNIT;
285 wtinfo_t *t = wttab + u;
286 int error;
287
288 if (u >= NWT || t->type == UNKNOWN)
289 return (ENXIO);
290
291 /* Check that device is not in use */
292 if (t->flags & TPINUSE)
293 return (EBUSY);
294
295 /* If the tape is in rewound state, check the status and set density. */
296 if (t->flags & TPSTART) {
297 /* If rewind is going on, wait */
298 if (error = wtwait (t, PCATCH, "wtrew"))
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 proc *p)
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 proc *p)
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 if (error = wtwait (t, PCATCH, "wtorew"))
454 return (error);
455 wtrewind (t);
456 return (0);
457 case MTFSF: /* forward space file */
458 for (count=((struct mtop*)arg)->mt_count; count>0; --count) {
459 if (error = wtwait (t, PCATCH, "wtorfm"))
460 return (error);
461 if (error = wtreadfm (t))
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 if (error = wtwait (t, PCATCH, "wtowfm"))
469 return (error);
470 if (error = wtwritefm (t))
471 return (error);
472 return (0);
473 case MTRETENS: /* re-tension tape */
474 if (error = wtwait (t, PCATCH, "wtretens"))
475 return (error);
476 op = QIC_RETENS;
477 goto erase_retens;
478
479 case MTERASE: /* erase to EOM */
480 if (! (t->flags & TPWRITE) || (t->flags & TPWP))
481 return (EACCES);
482 if (error = wtwait (t, PCATCH, "wterase"))
483 return (error);
484 op = QIC_ERASE;
485 erase_retens:
486 /* ERASE and RETENS operations work like REWIND. */
487 /* Simulate the rewind operation here. */
488 t->flags &= ~(TPRO | TPWO | TPVOL);
489 if (! wtcmd (t, op))
490 return (EIO);
491 t->flags |= TPSTART | TPREW;
492 t->flags |= TPWANY;
493 wtclock (t);
494 return (0);
495 }
496 return (EINVAL);
497 }
498
499 static int
500 wtread(dev_t dev, struct uio *uio, int ioflag)
501 {
502 return (physio(wtstrategy, NULL, dev, 1, minphys, uio));
503 }
504
505 static int
506 wtwrite(dev_t dev, struct uio *uio, int ioflag)
507 {
508 return (physio(wtstrategy, NULL, dev, 0, minphys, uio));
509 }
510
511 /*
512 * Strategy routine.
513 */
514 static void
515 wtstrategy (struct buf *bp)
516 {
517 int u = minor (bp->b_dev) & T_UNIT;
518 wtinfo_t *t = wttab + u;
519 int s;
520
521 bp->b_resid = bp->b_bcount;
522 if (u >= NWT || t->type == UNKNOWN) {
523 bp->b_error = ENXIO;
524 goto err2xit;
525 }
526
527 /* at file marks and end of tape, we just return '0 bytes available' */
528 if (t->flags & TPVOL)
529 goto xit;
530
531 if (bp->b_bcount % t->bsize != 0) {
532 bp->b_error = EINVAL;
533 goto err2xit;
534 }
535
536 if (bp->b_flags & B_READ) {
537 /* Check read access and no previous write to this tape. */
538 if (! (t->flags & TPREAD) || (t->flags & TPWANY))
539 goto errxit;
540
541 /* For now, we assume that all data will be copied out */
542 /* If read command outstanding, just skip down */
543 if (! (t->flags & TPRO)) {
544 if (! wtsense (t, 1, TP_WRP)) /* clear status */
545 goto errxit;
546 if (! wtcmd (t, QIC_RDDATA)) { /* sed read mode */
547 wtsense (t, 1, TP_WRP);
548 goto errxit;
549 }
550 t->flags |= TPRO | TPRANY;
551 }
552 } else {
553 /* Check write access and write protection. */
554 /* No previous read from this tape allowed. */
555 if (! (t->flags & TPWRITE) || (t->flags & (TPWP | TPRANY)))
556 goto errxit;
557
558 /* If write command outstanding, just skip down */
559 if (! (t->flags & TPWO)) {
560 if (! wtsense (t, 1, 0)) /* clear status */
561 goto errxit;
562 if (! wtcmd (t, QIC_WRTDATA)) { /* set write mode */
563 wtsense (t, 1, 0);
564 goto errxit;
565 }
566 t->flags |= TPWO | TPWANY;
567 }
568 }
569
570 if (! bp->b_bcount)
571 goto xit;
572
573 t->flags &= ~TPEXCEP;
574 s = splbio ();
575 if (wtstart (t, bp->b_flags, bp->b_data, bp->b_bcount)) {
576 wtwait (t, 0, (bp->b_flags & B_READ) ? "wtread" : "wtwrite");
577 bp->b_resid -= t->dmacount;
578 }
579 splx (s);
580
581 if (t->flags & TPEXCEP) {
582 errxit: bp->b_error = EIO;
583 err2xit: bp->b_flags |= B_ERROR;
584 }
585 xit: biodone (bp);
586 return;
587 }
588
589 /*
590 * Interrupt routine.
591 */
592 static void
593 wtintr (int u)
594 {
595 wtinfo_t *t = wttab + u;
596 unsigned char s;
597
598 if (u >= NWT || t->type == UNKNOWN) {
599 TRACE (("wtintr() -- device not configured\n"));
600 return;
601 }
602
603 s = inb (t->STATPORT); /* get status */
604 TRACE (("wtintr() status=0x%x -- ", s));
605 if ((s & (t->BUSY | t->NOEXCEP)) == (t->BUSY | t->NOEXCEP)) {
606 TRACE (("busy\n"));
607 return; /* device is busy */
608 }
609
610 /*
611 * Check if rewind finished.
612 */
613 if (t->flags & TPREW) {
614 TRACE (((s & (t->BUSY | t->NOEXCEP)) == (t->BUSY | t->NOEXCEP) ?
615 "rewind busy?\n" : "rewind finished\n"));
616 t->flags &= ~TPREW; /* Rewind finished. */
617 wtsense (t, 1, TP_WRP);
618 wakeup ((caddr_t)t);
619 return;
620 }
621
622 /*
623 * Check if writing/reading of file mark finished.
624 */
625 if (t->flags & (TPRMARK | TPWMARK)) {
626 TRACE (((s & (t->BUSY | t->NOEXCEP)) == (t->BUSY | t->NOEXCEP) ?
627 "marker r/w busy?\n" : "marker r/w finished\n"));
628 if (! (s & t->NOEXCEP)) /* operation failed */
629 wtsense (t, 1, (t->flags & TPRMARK) ? TP_WRP : 0);
630 t->flags &= ~(TPRMARK | TPWMARK); /* operation finished */
631 wakeup ((caddr_t)t);
632 return;
633 }
634
635 /*
636 * Do we started any i/o? If no, just return.
637 */
638 if (! (t->flags & TPACTIVE)) {
639 TRACE (("unexpected interrupt\n"));
640 return;
641 }
642
643 /*
644 * Clean up dma.
645 */
646 if ((t->dmaflags & B_READ) && (t->dmatotal - t->dmacount) < t->bsize) {
647 /* If reading short block, copy the internal buffer
648 * to the user memory. */
649 isa_dmadone (t->dmaflags, t->buf, t->bsize, t->chan);
650 bcopy (t->buf, t->dmavaddr, t->dmatotal - t->dmacount);
651 } else
652 isa_dmadone (t->dmaflags, t->dmavaddr, t->bsize, t->chan);
653
654 t->flags &= ~TPACTIVE;
655 t->dmacount += t->bsize;
656 t->dmavaddr = (char *)t->dmavaddr + t->bsize;
657
658 /*
659 * On exception, check for end of file and end of volume.
660 */
661 if (! (s & t->NOEXCEP)) {
662 TRACE (("i/o exception\n"));
663 wtsense (t, 1, (t->dmaflags & B_READ) ? TP_WRP : 0);
664 if (t->error.err & (TP_EOM | TP_FIL))
665 t->flags |= TPVOL; /* end of file */
666 else
667 t->flags |= TPEXCEP; /* i/o error */
668 wakeup ((caddr_t)t);
669 return;
670 }
671
672 if (t->dmacount < t->dmatotal) { /* continue i/o */
673 wtdma (t);
674 TRACE (("continue i/o, %d\n", t->dmacount));
675 return;
676 }
677 if (t->dmacount > t->dmatotal) /* short last block */
678 t->dmacount = t->dmatotal;
679 wakeup ((caddr_t)t); /* wake up user level */
680 TRACE (("i/o finished, %d\n", t->dmacount));
681 }
682
683 /* start the rewind operation */
684 static void
685 wtrewind (wtinfo_t *t)
686 {
687 int rwmode = (t->flags & (TPRO | TPWO));
688
689 t->flags &= ~(TPRO | TPWO | TPVOL);
690 /*
691 * Wangtek strictly follows QIC-02 standard:
692 * clearing ONLINE in read/write modes causes rewind.
693 * REWIND command is not allowed in read/write mode
694 * and gives `illegal command' error.
695 */
696 if (t->type==WANGTEK && rwmode) {
697 outb (t->CTLPORT, 0);
698 } else if (! wtcmd (t, QIC_REWIND))
699 return;
700 t->flags |= TPSTART | TPREW;
701 wtclock (t);
702 }
703
704 /* start the `read marker' operation */
705 static int
706 wtreadfm (wtinfo_t *t)
707 {
708 t->flags &= ~(TPRO | TPWO | TPVOL);
709 if (! wtcmd (t, QIC_READFM)) {
710 wtsense (t, 1, TP_WRP);
711 return (EIO);
712 }
713 t->flags |= TPRMARK | TPRANY;
714 wtclock (t);
715 /* Don't wait for completion here. */
716 return (0);
717 }
718
719 /* write marker to the tape */
720 static int
721 wtwritefm (wtinfo_t *t)
722 {
723 tsleep ((caddr_t)wtwritefm, WTPRI, "wtwfm", hz); /* timeout: 1 second */
724 t->flags &= ~(TPRO | TPWO);
725 if (! wtcmd (t, QIC_WRITEFM)) {
726 wtsense (t, 1, 0);
727 return (EIO);
728 }
729 t->flags |= TPWMARK | TPWANY;
730 wtclock (t);
731 return (wtwait (t, 0, "wtwfm"));
732 }
733
734 /* while controller status & mask == bits continue waiting */
735 static int
736 wtpoll (wtinfo_t *t, int mask, int bits)
737 {
738 int s, i;
739
740 /* Poll status port, waiting for specified bits. */
741 for (i=0; i<1000; ++i) { /* up to 1 msec */
742 s = inb (t->STATPORT);
743 if ((s & mask) != bits)
744 return (s);
745 DELAY (1);
746 }
747 for (i=0; i<100; ++i) { /* up to 10 msec */
748 s = inb (t->STATPORT);
749 if ((s & mask) != bits)
750 return (s);
751 DELAY (100);
752 }
753 for (;;) { /* forever */
754 s = inb (t->STATPORT);
755 if ((s & mask) != bits)
756 return (s);
757 tsleep ((caddr_t)wtpoll, WTPRI, "wtpoll", 1); /* timeout: 1 tick */
758 }
759 }
760
761 /* execute QIC command */
762 static int
763 wtcmd (wtinfo_t *t, int cmd)
764 {
765 int s, x;
766
767 TRACE (("wtcmd() cmd=0x%x\n", cmd));
768 x = splbio();
769 s = wtpoll (t, t->BUSY | t->NOEXCEP, t->BUSY | t->NOEXCEP); /* ready? */
770 if (! (s & t->NOEXCEP)) { /* error */
771 splx(x);
772 return (0);
773 }
774
775 outb (t->CMDPORT, cmd); /* output the command */
776
777 outb (t->CTLPORT, t->REQUEST | t->ONLINE); /* set request */
778 wtpoll (t, t->BUSY, t->BUSY); /* wait for ready */
779 outb (t->CTLPORT, t->IEN | t->ONLINE); /* reset request */
780 wtpoll (t, t->BUSY, 0); /* wait for not ready */
781 splx(x);
782 return (1);
783 }
784
785 /* wait for the end of i/o, seeking marker or rewind operation */
786 static int
787 wtwait (wtinfo_t *t, int catch, char *msg)
788 {
789 int error;
790
791 TRACE (("wtwait() `%s'\n", msg));
792 while (t->flags & (TPACTIVE | TPREW | TPRMARK | TPWMARK))
793 if (error = tsleep ((caddr_t)t, WTPRI | catch, msg, 0))
794 return (error);
795 return (0);
796 }
797
798 /* initialize dma for the i/o operation */
799 static void
800 wtdma (wtinfo_t *t)
801 {
802 t->flags |= TPACTIVE;
803 wtclock (t);
804
805 if (t->type == ARCHIVE)
806 outb (t->SDMAPORT, 0); /* set dma */
807
808 if ((t->dmaflags & B_READ) && (t->dmatotal - t->dmacount) < t->bsize)
809 /* Reading short block. Do it through the internal buffer. */
810 isa_dmastart (t->dmaflags, t->buf, t->bsize, t->chan);
811 else
812 isa_dmastart (t->dmaflags, t->dmavaddr, t->bsize, t->chan);
813 }
814
815 /* start i/o operation */
816 static int
817 wtstart (wtinfo_t *t, unsigned flags, void *vaddr, unsigned len)
818 {
819 int s, x;
820
821 TRACE (("wtstart()\n"));
822 x = splbio();
823 s = wtpoll (t, t->BUSY | t->NOEXCEP, t->BUSY | t->NOEXCEP); /* ready? */
824 if (! (s & t->NOEXCEP)) {
825 t->flags |= TPEXCEP; /* error */
826 splx(x);
827 return (0);
828 }
829 t->flags &= ~TPEXCEP; /* clear exception flag */
830 t->dmavaddr = vaddr;
831 t->dmatotal = len;
832 t->dmacount = 0;
833 t->dmaflags = flags;
834 wtdma (t);
835 splx(x);
836 return (1);
837 }
838
839 /* start timer */
840 static void
841 wtclock (wtinfo_t *t)
842 {
843 if (! (t->flags & TPTIMER)) {
844 t->flags |= TPTIMER;
845 /* Some controllers seem to lose dma interrupts too often.
846 * To make the tape stream we need 1 tick timeout. */
847 timeout (wtimer, (caddr_t)t, (t->flags & TPACTIVE) ? 1 : hz);
848 }
849 }
850
851 /*
852 * Simulate an interrupt periodically while i/o is going.
853 * This is necessary in case interrupts get eaten due to
854 * multiple devices on a single IRQ line.
855 */
856 static void
857 wtimer (void *xt)
858 {
859 wtinfo_t *t = (wtinfo_t *)xt;
860 int s;
861
862 t->flags &= ~TPTIMER;
863 if (! (t->flags & (TPACTIVE | TPREW | TPRMARK | TPWMARK)))
864 return;
865
866 /* If i/o going, simulate interrupt. */
867 s = splbio ();
868 if ((inb (t->STATPORT) & (t->BUSY | t->NOEXCEP)) != (t->BUSY | t->NOEXCEP)) {
869 TRACE (("wtimer() -- "));
870 wtintr (t->unit);
871 }
872 splx (s);
873
874 /* Restart timer if i/o pending. */
875 if (t->flags & (TPACTIVE | TPREW | TPRMARK | TPWMARK))
876 wtclock (t);
877 }
878
879 /* reset the controller */
880 static int
881 wtreset (wtinfo_t *t)
882 {
883 /* Perform QIC-02 and QIC-36 compatible reset sequence. */
884 /* Thanks to Mikael Hybsch <micke@dynas.se>. */
885 int s, i;
886
887 outb (t->CTLPORT, t->RESET | t->ONLINE); /* send reset */
888 DELAY (30);
889 outb (t->CTLPORT, t->ONLINE); /* turn off reset */
890 DELAY (30);
891
892 /* Read the controller status. */
893 s = inb (t->STATPORT);
894 if (s == 0xff) /* no port at this address? */
895 return (0);
896
897 /* Wait 3 sec for reset to complete. Needed for QIC-36 boards? */
898 for (i=0; i<3000; ++i) {
899 if (! (s & t->BUSY) || ! (s & t->NOEXCEP))
900 break;
901 DELAY (1000);
902 s = inb (t->STATPORT);
903 }
904 return ((s & t->RESETMASK) == t->RESETVAL);
905 }
906
907 /* get controller status information */
908 /* return 0 if user i/o request should receive an i/o error code */
909 static int
910 wtsense (wtinfo_t *t, int verb, int ignor)
911 {
912 char *msg = 0;
913 int err;
914
915 TRACE (("wtsense() ignor=0x%x\n", ignor));
916 t->flags &= ~(TPRO | TPWO);
917 if (! wtstatus (t))
918 return (0);
919 if (! (t->error.err & TP_ST0))
920 t->error.err &= ~TP_ST0MASK;
921 if (! (t->error.err & TP_ST1))
922 t->error.err &= ~TP_ST1MASK;
923 t->error.err &= ~ignor; /* ignore certain errors */
924 err = t->error.err & (TP_FIL | TP_BNL | TP_UDA | TP_EOM | TP_WRP |
925 TP_USL | TP_CNI | TP_MBD | TP_NDT | TP_ILL);
926 if (! err)
927 return (1);
928 if (! verb)
929 return (0);
930
931 /* lifted from tdriver.c from Wangtek */
932 if (err & TP_USL) msg = "Drive not online";
933 else if (err & TP_CNI) msg = "No cartridge";
934 else if ((err & TP_WRP) && !(t->flags & TPWP)) {
935 msg = "Tape is write protected";
936 t->flags |= TPWP;
937 }
938 else if (err & TP_FIL) msg = 0 /*"Filemark detected"*/;
939 else if (err & TP_EOM) msg = 0 /*"End of tape"*/;
940 else if (err & TP_BNL) msg = "Block not located";
941 else if (err & TP_UDA) msg = "Unrecoverable data error";
942 else if (err & TP_NDT) msg = "No data detected";
943 else if (err & TP_ILL) msg = "Illegal command";
944 if (msg)
945 printf ("wt%d: %s\n", t->unit, msg);
946 return (0);
947 }
948
949 /* get controller status information */
950 static int
951 wtstatus (wtinfo_t *t)
952 {
953 char *p;
954 int x;
955
956 x = splbio();
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 splx(x);
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 splx(x);
981 return (1);
982 }
983
984
985 static wt_devsw_installed = 0;
986
987 static void
988 wt_drvinit(void *unused)
989 {
990
991 if( ! wt_devsw_installed ) {
992 dev_t dev;
993
994 dev = makedev(CDEV_MAJOR, 0);
995 cdevsw_add(&dev, &wt_cdevsw, NULL);
996 wt_devsw_installed = 1;
997 }
998 }
999
1000 SYSINIT(wtdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,wt_drvinit,NULL)
1001
1002
1003 #endif /* NWT */
Cache object: d437ffdf8fc132104786a03328a48122
|