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