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: releng/5.0/sys/i386/isa/wt.c 96561 2002-05-14 06:57:02Z phk $
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
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/kernel.h>
65 #include <sys/bio.h>
66 #include <sys/fcntl.h>
67 #include <sys/malloc.h>
68 #include <sys/mtio.h>
69 #include <sys/conf.h>
70 #include <sys/bus.h>
71
72
73 #include <i386/isa/isa_device.h>
74 #include <i386/isa/wtreg.h>
75
76 #ifndef COMPAT_OLDISA
77 #error "The wt device requires the old isa compatibility shims"
78 #endif
79
80 /*
81 * Uncomment this to enable internal device tracing.
82 */
83 #define TRACE(s) /* printf s */
84
85 #define WTPRI (PZERO+10) /* sleep priority */
86
87 /*
88 * Wangtek controller ports
89 */
90 #define WT_CTLPORT(base) ((base)+0) /* control, write only */
91 #define WT_STATPORT(base) ((base)+0) /* status, read only */
92 #define WT_CMDPORT(base) ((base)+1) /* command, write only */
93 #define WT_DATAPORT(base) ((base)+1) /* data, read only */
94 #define WT_NPORT 2 /* 2 i/o ports */
95
96 /* status port bits */
97 #define WT_BUSY 0x01 /* not ready bit define */
98 #define WT_NOEXCEP 0x02 /* no exception bit define */
99 #define WT_RESETMASK 0x07 /* to check after reset */
100 #define WT_RESETVAL 0x05 /* state after reset */
101
102 /* control port bits */
103 #define WT_ONLINE 0x01 /* device selected */
104 #define WT_RESET 0x02 /* reset command */
105 #define WT_REQUEST 0x04 /* request command */
106 #define WT_IEN 0x08 /* enable dma */
107
108 /*
109 * Archive controller ports
110 */
111 #define AV_DATAPORT(base) ((base)+0) /* data, read only */
112 #define AV_CMDPORT(base) ((base)+0) /* command, write only */
113 #define AV_STATPORT(base) ((base)+1) /* status, read only */
114 #define AV_CTLPORT(base) ((base)+1) /* control, write only */
115 #define AV_SDMAPORT(base) ((base)+2) /* start dma */
116 #define AV_RDMAPORT(base) ((base)+3) /* reset dma */
117 #define AV_NPORT 4 /* 4 i/o ports */
118
119 /* status port bits */
120 #define AV_BUSY 0x40 /* not ready bit define */
121 #define AV_NOEXCEP 0x20 /* no exception bit define */
122 #define AV_RESETMASK 0xf8 /* to check after reset */
123 #define AV_RESETVAL 0x50 /* state after reset */
124
125 /* control port bits */
126 #define AV_RESET 0x80 /* reset command */
127 #define AV_REQUEST 0x40 /* request command */
128 #define AV_IEN 0x20 /* enable interrupts */
129
130 enum wttype {
131 UNKNOWN = 0, /* unknown type, driver disabled */
132 ARCHIVE, /* Archive Viper SC499, SC402 etc */
133 WANGTEK /* Wangtek */
134 };
135
136 typedef struct {
137 unsigned short err; /* code for error encountered */
138 unsigned short ercnt; /* number of error blocks */
139 unsigned short urcnt; /* number of underruns */
140 } wtstatus_t;
141
142 typedef struct {
143 enum wttype type; /* type of controller */
144 unsigned unit; /* unit number */
145 unsigned port; /* base i/o port */
146 unsigned chan; /* dma channel number, 1..3 */
147 unsigned flags; /* state of tape drive */
148 unsigned dens; /* tape density */
149 int bsize; /* tape block size */
150 void *buf; /* internal i/o buffer */
151
152 void *dmavaddr; /* virtual address of dma i/o buffer */
153 unsigned dmatotal; /* size of i/o buffer */
154 unsigned dmaflags; /* i/o direction, ISADMA_READ or ISADMA_WRITE */
155 unsigned dmacount; /* resulting length of dma i/o */
156
157 wtstatus_t error; /* status of controller */
158
159 unsigned short DATAPORT, CMDPORT, STATPORT, CTLPORT, SDMAPORT, RDMAPORT;
160 unsigned char BUSY, NOEXCEP, RESETMASK, RESETVAL;
161 unsigned char ONLINE, RESET, REQUEST, IEN;
162 } wtinfo_t;
163
164 static wtinfo_t wttab[NWT]; /* tape info by unit number */
165
166 static int wtwait (wtinfo_t *t, int catch, char *msg);
167 static int wtcmd (wtinfo_t *t, int cmd);
168 static int wtstart (wtinfo_t *t, unsigned mode, void *vaddr, unsigned len);
169 static void wtdma (wtinfo_t *t);
170 static timeout_t wtimer;
171 static void wtclock (wtinfo_t *t);
172 static int wtreset (wtinfo_t *t);
173 static int wtsense (wtinfo_t *t, int verb, int ignor);
174 static int wtstatus (wtinfo_t *t);
175 static ointhand2_t wtintr;
176 static void wtrewind (wtinfo_t *t);
177 static int wtreadfm (wtinfo_t *t);
178 static int wtwritefm (wtinfo_t *t);
179 static int wtpoll (wtinfo_t *t, int mask, int bits);
180
181 static d_open_t wtopen;
182 static d_close_t wtclose;
183 static d_ioctl_t wtioctl;
184 static d_strategy_t wtstrategy;
185
186 #define CDEV_MAJOR 10
187
188 static struct cdevsw wt_cdevsw = {
189 /* open */ wtopen,
190 /* close */ wtclose,
191 /* read */ physread,
192 /* write */ physwrite,
193 /* ioctl */ wtioctl,
194 /* poll */ nopoll,
195 /* mmap */ nommap,
196 /* strategy */ wtstrategy,
197 /* name */ "wt",
198 /* maj */ CDEV_MAJOR,
199 /* dump */ nodump,
200 /* psize */ nopsize,
201 /* flags */ 0,
202 };
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 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 = {
273 INTR_TYPE_BIO,
274 wtprobe,
275 wtattach,
276 "wt",
277 };
278 COMPAT_ISA_DRIVER(wt, wtdriver);
279
280 /*
281 * Open routine, called on every device open.
282 */
283 static int
284 wtopen (dev_t dev, int flag, int fmt, struct thread *td)
285 {
286 int u = minor (dev) & WT_UNIT;
287 wtinfo_t *t = wttab + u;
288 int error;
289
290 if (u >= NWT || t->type == UNKNOWN)
291 return (ENXIO);
292
293 /* Check that device is not in use */
294 if (t->flags & TPINUSE)
295 return (EBUSY);
296
297 /* If the tape is in rewound state, check the status and set density. */
298 if (t->flags & TPSTART) {
299 /* If rewind is going on, wait */
300 error = wtwait (t, PCATCH, "wtrew");
301 if (error)
302 return (error);
303
304 /* Check the controller status */
305 if (! wtsense (t, 0, (flag & FWRITE) ? 0 : TP_WRP)) {
306 /* Bad status, reset the controller */
307 if (! wtreset (t))
308 return (EIO);
309 if (! wtsense (t, 1, (flag & FWRITE) ? 0 : TP_WRP))
310 return (EIO);
311 }
312
313 /* Set up tape density. */
314 if (t->dens != (minor (dev) & WT_DENSEL)) {
315 int d = 0;
316
317 switch (minor (dev) & WT_DENSEL) {
318 case WT_DENSDFLT: default: break; /* default density */
319 case WT_QIC11: d = QIC_FMT11; break; /* minor 010 */
320 case WT_QIC24: d = QIC_FMT24; break; /* minor 020 */
321 case WT_QIC120: d = QIC_FMT120; break; /* minor 030 */
322 case WT_QIC150: d = QIC_FMT150; break; /* minor 040 */
323 case WT_QIC300: d = QIC_FMT300; break; /* minor 050 */
324 case WT_QIC600: d = QIC_FMT600; break; /* minor 060 */
325 }
326 if (d) {
327 /* Change tape density. */
328 if (! wtcmd (t, d))
329 return (EIO);
330 if (! wtsense (t, 1, TP_WRP | TP_ILL))
331 return (EIO);
332
333 /* Check the status of the controller. */
334 if (t->error.err & TP_ILL) {
335 printf ("wt%d: invalid tape density\n", t->unit);
336 return (ENODEV);
337 }
338 }
339 t->dens = minor (dev) & WT_DENSEL;
340 }
341 t->flags &= ~TPSTART;
342 } else if (t->dens != (minor (dev) & WT_DENSEL))
343 return (ENXIO);
344
345 t->bsize = (minor (dev) & WT_BSIZE) ? 1024 : 512;
346 t->buf = malloc (t->bsize, M_TEMP, M_WAITOK);
347 if (! t->buf)
348 return (EAGAIN);
349
350 if (isa_dma_acquire(t->chan))
351 return(EBUSY);
352
353 t->flags = TPINUSE;
354
355 if (flag & FREAD)
356 t->flags |= TPREAD;
357 if (flag & FWRITE)
358 t->flags |= TPWRITE;
359 return (0);
360 }
361
362 /*
363 * Close routine, called on last device close.
364 */
365 static int
366 wtclose (dev_t dev, int flags, int fmt, struct thread *td)
367 {
368 int u = minor (dev) & WT_UNIT;
369 wtinfo_t *t = wttab + u;
370
371 if (u >= NWT || t->type == UNKNOWN)
372 return (ENXIO);
373
374 /* If rewind is pending, do nothing */
375 if (t->flags & TPREW)
376 goto done;
377
378 /* If seek forward is pending and no rewind on close, do nothing */
379 if (t->flags & TPRMARK) {
380 if (minor (dev) & WT_NOREWIND)
381 goto done;
382
383 /* If read file mark is going on, wait */
384 wtwait (t, 0, "wtrfm");
385 }
386
387 if (t->flags & TPWANY)
388 /* Tape was written. Write file mark. */
389 wtwritefm (t);
390
391 if (! (minor (dev) & WT_NOREWIND)) {
392 /* Rewind tape to beginning of tape. */
393 /* Don't wait until rewind, though. */
394 wtrewind (t);
395 goto done;
396 }
397 if ((t->flags & TPRANY) && ! (t->flags & (TPVOL | TPWANY)))
398 /* Space forward to after next file mark if no writing done. */
399 /* Don't wait for completion. */
400 wtreadfm (t);
401 done:
402 t->flags &= TPREW | TPRMARK | TPSTART | TPTIMER;
403 free (t->buf, M_TEMP);
404 isa_dma_release(t->chan);
405 return (0);
406 }
407
408 /*
409 * Ioctl routine. Compatible with BSD ioctls.
410 * There are two possible ioctls:
411 * ioctl (int fd, MTIOCGET, struct mtget *buf) -- get status
412 * ioctl (int fd, MTIOCTOP, struct mtop *buf) -- do BSD-like op
413 */
414 static int
415 wtioctl (dev_t dev, u_long cmd, caddr_t arg, int flags, struct thread *td)
416 {
417 int u = minor (dev) & WT_UNIT;
418 wtinfo_t *t = wttab + u;
419 int error, count, op;
420
421 if (u >= NWT || t->type == UNKNOWN)
422 return (ENXIO);
423
424 switch (cmd) {
425 default:
426 return (EINVAL);
427 case MTIOCIEOT: /* ignore EOT errors */
428 case MTIOCEEOT: /* enable EOT errors */
429 return (0);
430 case MTIOCGET:
431 ((struct mtget*)arg)->mt_type =
432 t->type == ARCHIVE ? MT_ISVIPER1 : 0x11;
433 ((struct mtget*)arg)->mt_dsreg = t->flags; /* status */
434 ((struct mtget*)arg)->mt_erreg = t->error.err; /* errors */
435 ((struct mtget*)arg)->mt_resid = 0;
436 ((struct mtget*)arg)->mt_fileno = 0; /* file */
437 ((struct mtget*)arg)->mt_blkno = 0; /* block */
438 return (0);
439 case MTIOCTOP:
440 break;
441 }
442 switch ((short) ((struct mtop*)arg)->mt_op) {
443 default:
444 case MTFSR: /* forward space record */
445 case MTBSR: /* backward space record */
446 case MTBSF: /* backward space file */
447 break;
448 case MTNOP: /* no operation, sets status only */
449 case MTCACHE: /* enable controller cache */
450 case MTNOCACHE: /* disable controller cache */
451 return (0);
452 case MTREW: /* rewind */
453 case MTOFFL: /* rewind and put the drive offline */
454 if (t->flags & TPREW) /* rewind is running */
455 return (0);
456 error = wtwait (t, PCATCH, "wtorew");
457 if (error)
458 return (error);
459 wtrewind (t);
460 return (0);
461 case MTFSF: /* forward space file */
462 for (count=((struct mtop*)arg)->mt_count; count>0; --count) {
463 error = wtwait (t, PCATCH, "wtorfm");
464 if (error)
465 return (error);
466 error = wtreadfm (t);
467 if (error)
468 return (error);
469 }
470 return (0);
471 case MTWEOF: /* write an end-of-file record */
472 if (! (t->flags & TPWRITE) || (t->flags & TPWP))
473 return (EACCES);
474 error = wtwait (t, PCATCH, "wtowfm");
475 if (error)
476 return (error);
477 error = wtwritefm (t);
478 if (error)
479 return (error);
480 return (0);
481 case MTRETENS: /* re-tension tape */
482 error = wtwait (t, PCATCH, "wtretens");
483 if (error)
484 return (error);
485 op = QIC_RETENS;
486 goto erase_retens;
487
488 case MTERASE: /* erase to EOM */
489 if (! (t->flags & TPWRITE) || (t->flags & TPWP))
490 return (EACCES);
491 error = wtwait (t, PCATCH, "wterase");
492 if (error)
493 return (error);
494 op = QIC_ERASE;
495 erase_retens:
496 /* ERASE and RETENS operations work like REWIND. */
497 /* Simulate the rewind operation here. */
498 t->flags &= ~(TPRO | TPWO | TPVOL);
499 if (! wtcmd (t, op))
500 return (EIO);
501 t->flags |= TPSTART | TPREW;
502 t->flags |= TPWANY;
503 wtclock (t);
504 return (0);
505 }
506 return (EINVAL);
507 }
508
509 /*
510 * Strategy routine.
511 */
512 static void
513 wtstrategy (struct bio *bp)
514 {
515 int u = minor (bp->bio_dev) & WT_UNIT;
516 wtinfo_t *t = wttab + u;
517 int s;
518
519 bp->bio_resid = bp->bio_bcount;
520 if (u >= NWT || t->type == UNKNOWN) {
521 bp->bio_error = ENXIO;
522 goto err2xit;
523 }
524
525 /* at file marks and end of tape, we just return '0 bytes available' */
526 if (t->flags & TPVOL)
527 goto xit;
528
529 if (bp->bio_bcount % t->bsize != 0) {
530 bp->bio_error = EINVAL;
531 goto err2xit;
532 }
533
534 if (bp->bio_cmd == BIO_READ) {
535 /* Check read access and no previous write to this tape. */
536 if (! (t->flags & TPREAD) || (t->flags & TPWANY))
537 goto errxit;
538
539 /* For now, we assume that all data will be copied out */
540 /* If read command outstanding, just skip down */
541 if (! (t->flags & TPRO)) {
542 if (! wtsense (t, 1, TP_WRP)) /* clear status */
543 goto errxit;
544 if (! wtcmd (t, QIC_RDDATA)) { /* sed read mode */
545 wtsense (t, 1, TP_WRP);
546 goto errxit;
547 }
548 t->flags |= TPRO | TPRANY;
549 }
550 } else {
551 /* Check write access and write protection. */
552 /* No previous read from this tape allowed. */
553 if (! (t->flags & TPWRITE) || (t->flags & (TPWP | TPRANY)))
554 goto errxit;
555
556 /* If write command outstanding, just skip down */
557 if (! (t->flags & TPWO)) {
558 if (! wtsense (t, 1, 0)) /* clear status */
559 goto errxit;
560 if (! wtcmd (t, QIC_WRTDATA)) { /* set write mode */
561 wtsense (t, 1, 0);
562 goto errxit;
563 }
564 t->flags |= TPWO | TPWANY;
565 }
566 }
567
568 if (! bp->bio_bcount)
569 goto xit;
570
571 t->flags &= ~TPEXCEP;
572 s = splbio ();
573 if (wtstart (t, bp->bio_cmd == BIO_READ ? ISADMA_READ : ISADMA_WRITE,
574 bp->bio_data, bp->bio_bcount)) {
575 wtwait (t, 0, (bp->bio_cmd == BIO_READ) ? "wtread" : "wtwrite");
576 bp->bio_resid -= t->dmacount;
577 }
578 splx (s);
579
580 if (t->flags & TPEXCEP) {
581 errxit: bp->bio_error = EIO;
582 err2xit: bp->bio_flags |= BIO_ERROR;
583 }
584 xit: biodone (bp);
585 return;
586 }
587
588 /*
589 * Interrupt routine.
590 */
591 static void
592 wtintr (int u)
593 {
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, WTPRI, "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, WTPRI, "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, x;
765
766 TRACE (("wtcmd() cmd=0x%x\n", cmd));
767 x = splbio();
768 s = wtpoll (t, t->BUSY | t->NOEXCEP, t->BUSY | t->NOEXCEP); /* ready? */
769 if (! (s & t->NOEXCEP)) { /* error */
770 splx(x);
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 splx(x);
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, WTPRI | 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, x;
821
822 TRACE (("wtstart()\n"));
823 x = splbio();
824 s = wtpoll (t, t->BUSY | t->NOEXCEP, t->BUSY | t->NOEXCEP); /* ready? */
825 if (! (s & t->NOEXCEP)) {
826 t->flags |= TPEXCEP; /* error */
827 splx(x);
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 splx(x);
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 timeout (wtimer, (caddr_t)t, (t->flags & TPACTIVE) ? 1 : hz);
849 }
850 }
851
852 /*
853 * Simulate an interrupt periodically while i/o is going.
854 * This is necessary in case interrupts get eaten due to
855 * multiple devices on a single IRQ line.
856 */
857 static void
858 wtimer (void *xt)
859 {
860 wtinfo_t *t = (wtinfo_t *)xt;
861 int s;
862
863 t->flags &= ~TPTIMER;
864 if (! (t->flags & (TPACTIVE | TPREW | TPRMARK | TPWMARK)))
865 return;
866
867 /* If i/o going, simulate interrupt. */
868 s = splbio ();
869 if ((inb (t->STATPORT) & (t->BUSY | t->NOEXCEP)) != (t->BUSY | t->NOEXCEP)) {
870 TRACE (("wtimer() -- "));
871 wtintr (t->unit);
872 }
873 splx (s);
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 int x;
956
957 x = splbio();
958 wtpoll (t, t->BUSY | t->NOEXCEP, t->BUSY | t->NOEXCEP); /* ready? */
959 outb (t->CMDPORT, QIC_RDSTAT); /* send `read status' command */
960
961 outb (t->CTLPORT, t->REQUEST | t->ONLINE); /* set request */
962 wtpoll (t, t->BUSY, t->BUSY); /* wait for ready */
963 outb (t->CTLPORT, t->ONLINE); /* reset request */
964 wtpoll (t, t->BUSY, 0); /* wait for not ready */
965
966 p = (char*) &t->error;
967 while (p < (char*)&t->error + 6) {
968 int s = wtpoll (t, t->BUSY | t->NOEXCEP, t->BUSY | t->NOEXCEP);
969 if (! (s & t->NOEXCEP)) { /* error */
970 splx(x);
971 return (0);
972 }
973
974 *p++ = inb (t->DATAPORT); /* read status byte */
975
976 outb (t->CTLPORT, t->REQUEST | t->ONLINE); /* set request */
977 wtpoll (t, t->BUSY, 0); /* wait for not ready */
978 DELAY(20);
979 outb (t->CTLPORT, t->ONLINE); /* unset request */
980 }
981 splx(x);
982 return (1);
983 }
Cache object: 004faf888dfd2e178ff05ed6c0b9a88a
|