wt.c revision 1.41 1 /* $NetBSD: wt.c,v 1.41 1997/10/20 18:43:22 thorpej Exp $ */
2
3 /*
4 * Streamer tape driver.
5 * Supports Archive and Wangtek compatible QIC-02/QIC-36 boards.
6 *
7 * Copyright (C) 1993 by:
8 * Sergey Ryzhkov <sir (at) kiae.su>
9 * Serge Vakulenko <vak (at) zebub.msk.su>
10 *
11 * This software is distributed with NO WARRANTIES, not even the implied
12 * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 *
14 * Authors grant any other persons or organisations permission to use
15 * or modify this software as long as this message is kept with the software,
16 * all derivative works or modified versions.
17 *
18 * This driver is derived from the old 386bsd Wangtek streamer tape driver,
19 * made by Robert Baron at CMU, based on Intel sources.
20 */
21
22 /*
23 * Copyright (c) 1989 Carnegie-Mellon University.
24 * All rights reserved.
25 *
26 * Authors: Robert Baron
27 *
28 * Permission to use, copy, modify and distribute this software and
29 * its documentation is hereby granted, provided that both the copyright
30 * notice and this permission notice appear in all copies of the
31 * software, derivative works or modified versions, and any portions
32 * thereof, and that both notices appear in supporting documentation.
33 *
34 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
35 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
36 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
37 *
38 * Carnegie Mellon requests users of this software to return to
39 *
40 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
41 * School of Computer Science
42 * Carnegie Mellon University
43 * Pittsburgh PA 15213-3890
44 *
45 * any improvements or extensions that they make and grant Carnegie the
46 * rights to redistribute these changes.
47 */
48
49 /*
50 * Copyright 1988, 1989 by Intel Corporation
51 */
52
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/kernel.h>
56 #include <sys/buf.h>
57 #include <sys/fcntl.h>
58 #include <sys/malloc.h>
59 #include <sys/ioctl.h>
60 #include <sys/mtio.h>
61 #include <sys/device.h>
62 #include <sys/proc.h>
63 #include <sys/conf.h>
64
65 #include <vm/vm_param.h>
66
67 #include <machine/intr.h>
68 #include <machine/bus.h>
69 #include <machine/pio.h>
70
71 #include <dev/isa/isavar.h>
72 #include <dev/isa/isadmavar.h>
73 #include <dev/isa/wtreg.h>
74
75 /*
76 * Uncomment this to enable internal device tracing.
77 */
78 #define WTDBPRINT(x) /* printf x */
79
80 #define WTPRI (PZERO+10) /* sleep priority */
81
82 /*
83 * Wangtek controller ports
84 */
85 #define WT_CTLPORT (ia->ia_iobase + 0 ) /* control, write only */
86 #define WT_STATPORT (ia->ia_iobase + 0 ) /* status, read only */
87 #define WT_CMDPORT (ia->ia_iobase + 1 ) /* command, write only */
88 #define WT_DATAPORT (ia->ia_iobase + 1 ) /* data, read only */
89 #define WT_NPORT 2 /* 2 i/o ports */
90
91 /* status port bits */
92 #define WT_BUSY 0x01 /* not ready bit define */
93 #define WT_NOEXCEP 0x02 /* no exception bit define */
94 #define WT_RESETMASK 0x07 /* to check after reset */
95 #define WT_RESETVAL 0x05 /* state after reset */
96
97 /* control port bits */
98 #define WT_ONLINE 0x01 /* device selected */
99 #define WT_RESET 0x02 /* reset command */
100 #define WT_REQUEST 0x04 /* request command */
101 #define WT_IEN 0x08 /* enable dma */
102
103 /*
104 * Archive controller ports
105 */
106 #define AV_DATAPORT (ia->ia_iobase + 0) /* data, read only */
107 #define AV_CMDPORT (ia->ia_iobase + 0) /* command, write only */
108 #define AV_STATPORT (ia->ia_iobase + 1) /* status, read only */
109 #define AV_CTLPORT (ia->ia_iobase + 1) /* control, write only */
110 #define AV_SDMAPORT (ia->ia_iobase + 2) /* start dma */
111 #define AV_RDMAPORT (ia->ia_iobase + 3) /* reset dma */
112 #define AV_NPORT 4 /* 4 i/o ports */
113
114 /* status port bits */
115 #define AV_BUSY 0x40 /* not ready bit define */
116 #define AV_NOEXCEP 0x20 /* no exception bit define */
117 #define AV_RESETMASK 0xf8 /* to check after reset */
118 #define AV_RESETVAL 0x50 /* state after reset */
119
120 /* control port bits */
121 #define AV_RESET 0x80 /* reset command */
122 #define AV_REQUEST 0x40 /* request command */
123 #define AV_IEN 0x20 /* enable interrupts */
124
125 enum wttype {
126 UNKNOWN = 0, /* unknown type, driver disabled */
127 ARCHIVE, /* Archive Viper SC499, SC402 etc */
128 WANGTEK, /* Wangtek */
129 };
130
131 struct wt_softc {
132 struct device sc_dev;
133 void *sc_ih;
134
135 bus_space_tag_t sc_iot;
136 bus_space_handle_t sc_ioh;
137
138 enum wttype type; /* type of controller */
139 int chan; /* dma channel number, 1..3 */
140 int flags; /* state of tape drive */
141 unsigned dens; /* tape density */
142 int bsize; /* tape block size */
143 void *buf; /* internal i/o buffer */
144
145 void *dmavaddr; /* virtual address of dma i/o buffer */
146 size_t dmatotal; /* size of i/o buffer */
147 int dmaflags; /* i/o direction */
148 size_t dmacount; /* resulting length of dma i/o */
149
150 u_short error; /* code for error encountered */
151 u_short ercnt; /* number of error blocks */
152 u_short urcnt; /* number of underruns */
153
154 int DATAPORT, CMDPORT, STATPORT, CTLPORT, SDMAPORT, RDMAPORT;
155 u_char BUSY, NOEXCEP, RESETMASK, RESETVAL, ONLINE, RESET, REQUEST, IEN;
156 };
157
158 /* XXX: These don't belong here really */
159 cdev_decl(wt);
160 bdev_decl(wt);
161
162 int wtwait __P((struct wt_softc *sc, int catch, char *msg));
163 int wtcmd __P((struct wt_softc *sc, int cmd));
164 int wtstart __P((struct wt_softc *sc, int flag, void *vaddr, size_t len));
165 void wtdma __P((struct wt_softc *sc));
166 void wttimer __P((void *arg));
167 void wtclock __P((struct wt_softc *sc));
168 int wtreset __P((struct wt_softc *sc));
169 int wtsense __P((struct wt_softc *sc, int verbose, int ignore));
170 int wtstatus __P((struct wt_softc *sc));
171 void wtrewind __P((struct wt_softc *sc));
172 int wtreadfm __P((struct wt_softc *sc));
173 int wtwritefm __P((struct wt_softc *sc));
174 u_char wtsoft __P((struct wt_softc *sc, int mask, int bits));
175
176 int wtprobe __P((struct device *, void *, void *));
177 void wtattach __P((struct device *, struct device *, void *));
178 int wtintr __P((void *sc));
179
180 struct cfattach wt_ca = {
181 sizeof(struct wt_softc), wtprobe, wtattach
182 };
183
184 struct cfdriver wt_cd = {
185 NULL, "wt", DV_TAPE
186 };
187
188 /*
189 * Probe for the presence of the device.
190 */
191 int
192 wtprobe(parent, match, aux)
193 struct device *parent;
194 void *match, *aux;
195 {
196 struct wt_softc *sc = match;
197 struct isa_attach_args *ia = aux;
198 bus_space_tag_t iot = ia->ia_iot;
199 bus_space_handle_t ioh;
200 int rv = 0;
201
202
203 /* Disallow wildcarded i/o address. */
204 if (ia->ia_iobase == ISACF_PORT_DEFAULT)
205 return (0);
206
207 /* Map i/o space */
208 if (bus_space_map(iot, ia->ia_iobase, AV_NPORT, 0, &ioh))
209 return 0;
210
211 /* XXX broken_indirect_config */
212 sc->chan = ia->ia_drq;
213 if (sc->chan < 1 || sc->chan > 3) {
214 printf("%s: Bad drq=%d, should be 1..3\n", sc->sc_dev.dv_xname,
215 sc->chan);
216 rv = 0;
217 goto done;
218 }
219
220
221 /* Try Wangtek. */
222 sc->type = WANGTEK;
223 sc->CTLPORT = WT_CTLPORT;
224 sc->STATPORT = WT_STATPORT;
225 sc->CMDPORT = WT_CMDPORT;
226 sc->DATAPORT = WT_DATAPORT;
227 sc->SDMAPORT = sc->RDMAPORT = 0;
228 sc->BUSY = WT_BUSY; sc->NOEXCEP = WT_NOEXCEP;
229 sc->RESETMASK = WT_RESETMASK; sc->RESETVAL = WT_RESETVAL;
230 sc->ONLINE = WT_ONLINE; sc->RESET = WT_RESET;
231 sc->REQUEST = WT_REQUEST; sc->IEN = WT_IEN;
232 if (wtreset(sc)) {
233 ia->ia_iosize = WT_NPORT;
234 rv = 1;
235 goto done;
236 }
237
238 /* Try Archive. */
239 sc->type = ARCHIVE;
240 sc->CTLPORT = AV_CTLPORT;
241 sc->STATPORT = AV_STATPORT;
242 sc->CMDPORT = AV_CMDPORT;
243 sc->DATAPORT = AV_DATAPORT;
244 sc->SDMAPORT = AV_SDMAPORT;
245 sc->RDMAPORT = AV_RDMAPORT;
246 sc->BUSY = AV_BUSY; sc->NOEXCEP = AV_NOEXCEP;
247 sc->RESETMASK = AV_RESETMASK; sc->RESETVAL = AV_RESETVAL;
248 sc->ONLINE = 0; sc->RESET = AV_RESET;
249 sc->REQUEST = AV_REQUEST; sc->IEN = AV_IEN;
250 if (wtreset(sc)) {
251 ia->ia_iosize = AV_NPORT;
252 bus_space_unmap(iot, ioh, AV_NPORT);
253 rv = 1;
254 goto done;
255 }
256
257 /* Tape controller not found. */
258 sc->type = UNKNOWN;
259
260 done:
261 bus_space_unmap(iot, ioh, AV_NPORT);
262 return rv;
263 }
264
265 /*
266 * Device is found, configure it.
267 */
268 void
269 wtattach(parent, self, aux)
270 struct device *parent, *self;
271 void *aux;
272 {
273 struct wt_softc *sc = (void *)self;
274 struct isa_attach_args *ia = aux;
275 bus_space_tag_t iot = ia->ia_iot;
276 bus_space_handle_t ioh;
277
278 /* Map i/o space */
279 if (bus_space_map(iot, ia->ia_iobase, AV_NPORT, 0, &ioh)) {
280 printf(": can't map i/o space\n");
281 return;
282 }
283
284 sc->sc_iot = iot;
285 sc->sc_ioh = ioh;
286
287 /* XXX broken_indirect_config */
288 if (sc->type == ARCHIVE) {
289 printf(": type <Archive>\n");
290 /* Reset DMA. */
291 bus_space_write_1(iot, ioh, sc->RDMAPORT, 0);
292 } else
293 printf(": type <Wangtek>\n");
294 sc->flags = TPSTART; /* tape is rewound */
295 sc->dens = -1; /* unknown density */
296
297 if (isa_dmamap_create(parent, sc->chan, MAXPHYS,
298 BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW)) {
299 printf("%s: can't set up ISA DMA map\n",
300 sc->sc_dev.dv_xname);
301 return;
302 }
303
304 sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
305 IPL_BIO, wtintr, sc);
306 }
307
308 int
309 wtdump(dev, blkno, va, size)
310 dev_t dev;
311 daddr_t blkno;
312 caddr_t va;
313 size_t size;
314 {
315
316 /* Not implemented. */
317 return ENXIO;
318 }
319
320 int
321 wtsize(dev)
322 dev_t dev;
323 {
324
325 /* Not implemented. */
326 return -1;
327 }
328
329 /*
330 * Open routine, called on every device open.
331 */
332 int
333 wtopen(dev, flag, mode, p)
334 dev_t dev;
335 int flag;
336 int mode;
337 struct proc *p;
338 {
339 int unit = minor(dev) & T_UNIT;
340 struct wt_softc *sc;
341 int error;
342
343 if (unit >= wt_cd.cd_ndevs)
344 return ENXIO;
345 sc = wt_cd.cd_devs[unit];
346 if (!sc)
347 return ENXIO;
348
349 /* Check that device is not in use */
350 if (sc->flags & TPINUSE)
351 return EBUSY;
352
353 /* If the tape is in rewound state, check the status and set density. */
354 if (sc->flags & TPSTART) {
355 /* If rewind is going on, wait */
356 if ((error = wtwait(sc, PCATCH, "wtrew")) != 0)
357 return error;
358
359 /* Check the controller status */
360 if (!wtsense(sc, 0, (flag & FWRITE) ? 0 : TP_WRP)) {
361 /* Bad status, reset the controller. */
362 if (!wtreset(sc))
363 return EIO;
364 if (!wtsense(sc, 1, (flag & FWRITE) ? 0 : TP_WRP))
365 return EIO;
366 }
367
368 /* Set up tape density. */
369 if (sc->dens != (minor(dev) & WT_DENSEL)) {
370 int d = 0;
371
372 switch (minor(dev) & WT_DENSEL) {
373 case WT_DENSDFLT:
374 default:
375 break; /* default density */
376 case WT_QIC11:
377 d = QIC_FMT11; break; /* minor 010 */
378 case WT_QIC24:
379 d = QIC_FMT24; break; /* minor 020 */
380 case WT_QIC120:
381 d = QIC_FMT120; break; /* minor 030 */
382 case WT_QIC150:
383 d = QIC_FMT150; break; /* minor 040 */
384 case WT_QIC300:
385 d = QIC_FMT300; break; /* minor 050 */
386 case WT_QIC600:
387 d = QIC_FMT600; break; /* minor 060 */
388 }
389 if (d) {
390 /* Change tape density. */
391 if (!wtcmd(sc, d))
392 return EIO;
393 if (!wtsense(sc, 1, TP_WRP | TP_ILL))
394 return EIO;
395
396 /* Check the status of the controller. */
397 if (sc->error & TP_ILL) {
398 printf("%s: invalid tape density\n",
399 sc->sc_dev.dv_xname);
400 return ENODEV;
401 }
402 }
403 sc->dens = minor(dev) & WT_DENSEL;
404 }
405 sc->flags &= ~TPSTART;
406 } else if (sc->dens != (minor(dev) & WT_DENSEL))
407 return ENXIO;
408
409 sc->bsize = (minor(dev) & WT_BSIZE) ? 1024 : 512;
410 sc->buf = malloc(sc->bsize, M_TEMP, M_WAITOK);
411
412 sc->flags = TPINUSE;
413 if (flag & FREAD)
414 sc->flags |= TPREAD;
415 if (flag & FWRITE)
416 sc->flags |= TPWRITE;
417 return 0;
418 }
419
420 /*
421 * Close routine, called on last device close.
422 */
423 int
424 wtclose(dev, flags, mode, p)
425 dev_t dev;
426 int flags;
427 int mode;
428 struct proc *p;
429 {
430 int unit = minor(dev) & T_UNIT;
431 struct wt_softc *sc = wt_cd.cd_devs[unit];
432
433 /* If rewind is pending, do nothing */
434 if (sc->flags & TPREW)
435 goto done;
436
437 /* If seek forward is pending and no rewind on close, do nothing */
438 if (sc->flags & TPRMARK) {
439 if (minor(dev) & T_NOREWIND)
440 goto done;
441
442 /* If read file mark is going on, wait */
443 wtwait(sc, 0, "wtrfm");
444 }
445
446 if (sc->flags & TPWANY) {
447 /* Tape was written. Write file mark. */
448 wtwritefm(sc);
449 }
450
451 if ((minor(dev) & T_NOREWIND) == 0) {
452 /* Rewind to beginning of tape. */
453 /* Don't wait until rewind, though. */
454 wtrewind(sc);
455 goto done;
456 }
457 if ((sc->flags & TPRANY) && (sc->flags & (TPVOL | TPWANY)) == 0) {
458 /* Space forward to after next file mark if no writing done. */
459 /* Don't wait for completion. */
460 wtreadfm(sc);
461 }
462
463 done:
464 sc->flags &= TPREW | TPRMARK | TPSTART | TPTIMER;
465 free(sc->buf, M_TEMP);
466 return 0;
467 }
468
469 /*
470 * Ioctl routine. Compatible with BSD ioctls.
471 * Direct QIC-02 commands ERASE and RETENSION added.
472 * There are three possible ioctls:
473 * ioctl(int fd, MTIOCGET, struct mtget *buf) -- get status
474 * ioctl(int fd, MTIOCTOP, struct mtop *buf) -- do BSD-like op
475 * ioctl(int fd, WTQICMD, int qicop) -- do QIC op
476 */
477 int
478 wtioctl(dev, cmd, addr, flag, p)
479 dev_t dev;
480 u_long cmd;
481 caddr_t addr;
482 int flag;
483 struct proc *p;
484 {
485 int unit = minor(dev) & T_UNIT;
486 struct wt_softc *sc = wt_cd.cd_devs[unit];
487 int error, count, op;
488
489 switch (cmd) {
490 default:
491 return EINVAL;
492 case WTQICMD: /* direct QIC command */
493 op = *(int *)addr;
494 switch (op) {
495 default:
496 return EINVAL;
497 case QIC_ERASE: /* erase the whole tape */
498 if ((sc->flags & TPWRITE) == 0 || (sc->flags & TPWP))
499 return EACCES;
500 if ((error = wtwait(sc, PCATCH, "wterase")) != 0)
501 return error;
502 break;
503 case QIC_RETENS: /* retension the tape */
504 if ((error = wtwait(sc, PCATCH, "wtretens")) != 0)
505 return error;
506 break;
507 }
508 /* Both ERASE and RETENS operations work like REWIND. */
509 /* Simulate the rewind operation here. */
510 sc->flags &= ~(TPRO | TPWO | TPVOL);
511 if (!wtcmd(sc, op))
512 return EIO;
513 sc->flags |= TPSTART | TPREW;
514 if (op == QIC_ERASE)
515 sc->flags |= TPWANY;
516 wtclock(sc);
517 return 0;
518 case MTIOCIEOT: /* ignore EOT errors */
519 case MTIOCEEOT: /* enable EOT errors */
520 return 0;
521 case MTIOCGET:
522 ((struct mtget*)addr)->mt_type =
523 sc->type == ARCHIVE ? MT_ISVIPER1 : 0x11;
524 ((struct mtget*)addr)->mt_dsreg = sc->flags; /* status */
525 ((struct mtget*)addr)->mt_erreg = sc->error; /* errors */
526 ((struct mtget*)addr)->mt_resid = 0;
527 ((struct mtget*)addr)->mt_fileno = 0; /* file */
528 ((struct mtget*)addr)->mt_blkno = 0; /* block */
529 return 0;
530 case MTIOCTOP:
531 break;
532 }
533
534 switch ((short)((struct mtop*)addr)->mt_op) {
535 default:
536 #if 0
537 case MTFSR: /* forward space record */
538 case MTBSR: /* backward space record */
539 case MTBSF: /* backward space file */
540 #endif
541 return EINVAL;
542 case MTNOP: /* no operation, sets status only */
543 case MTCACHE: /* enable controller cache */
544 case MTNOCACHE: /* disable controller cache */
545 return 0;
546 case MTREW: /* rewind */
547 case MTOFFL: /* rewind and put the drive offline */
548 if (sc->flags & TPREW) /* rewind is running */
549 return 0;
550 if ((error = wtwait(sc, PCATCH, "wtorew")) != 0)
551 return error;
552 wtrewind(sc);
553 return 0;
554 case MTFSF: /* forward space file */
555 for (count = ((struct mtop*)addr)->mt_count; count > 0;
556 --count) {
557 if ((error = wtwait(sc, PCATCH, "wtorfm")) != 0)
558 return error;
559 if ((error = wtreadfm(sc)) != 0)
560 return error;
561 }
562 return 0;
563 case MTWEOF: /* write an end-of-file record */
564 if ((sc->flags & TPWRITE) == 0 || (sc->flags & TPWP))
565 return EACCES;
566 if ((error = wtwait(sc, PCATCH, "wtowfm")) != 0)
567 return error;
568 if ((error = wtwritefm(sc)) != 0)
569 return error;
570 return 0;
571 }
572
573 #ifdef DIAGNOSTIC
574 panic("wtioctl: impossible");
575 #endif
576 }
577
578 /*
579 * Strategy routine.
580 */
581 void
582 wtstrategy(bp)
583 struct buf *bp;
584 {
585 int unit = minor(bp->b_dev) & T_UNIT;
586 struct wt_softc *sc = wt_cd.cd_devs[unit];
587 int s;
588
589 bp->b_resid = bp->b_bcount;
590
591 /* at file marks and end of tape, we just return '0 bytes available' */
592 if (sc->flags & TPVOL)
593 goto xit;
594
595 if (bp->b_flags & B_READ) {
596 /* Check read access and no previous write to this tape. */
597 if ((sc->flags & TPREAD) == 0 || (sc->flags & TPWANY))
598 goto errxit;
599
600 /* For now, we assume that all data will be copied out */
601 /* If read command outstanding, just skip down */
602 if ((sc->flags & TPRO) == 0) {
603 if (!wtsense(sc, 1, TP_WRP)) {
604 /* Clear status. */
605 goto errxit;
606 }
607 if (!wtcmd(sc, QIC_RDDATA)) {
608 /* Set read mode. */
609 wtsense(sc, 1, TP_WRP);
610 goto errxit;
611 }
612 sc->flags |= TPRO | TPRANY;
613 }
614 } else {
615 /* Check write access and write protection. */
616 /* No previous read from this tape allowed. */
617 if ((sc->flags & TPWRITE) == 0 || (sc->flags & (TPWP | TPRANY)))
618 goto errxit;
619
620 /* If write command outstanding, just skip down */
621 if ((sc->flags & TPWO) == 0) {
622 if (!wtsense(sc, 1, 0)) {
623 /* Clear status. */
624 goto errxit;
625 }
626 if (!wtcmd(sc, QIC_WRTDATA)) {
627 /* Set write mode. */
628 wtsense(sc, 1, 0);
629 goto errxit;
630 }
631 sc->flags |= TPWO | TPWANY;
632 }
633 }
634
635 if (bp->b_bcount == 0)
636 goto xit;
637
638 sc->flags &= ~TPEXCEP;
639 s = splbio();
640 if (wtstart(sc, bp->b_flags, bp->b_data, bp->b_bcount)) {
641 wtwait(sc, 0, (bp->b_flags & B_READ) ? "wtread" : "wtwrite");
642 bp->b_resid -= sc->dmacount;
643 }
644 splx(s);
645
646 if (sc->flags & TPEXCEP) {
647 errxit:
648 bp->b_flags |= B_ERROR;
649 bp->b_error = EIO;
650 }
651 xit:
652 biodone(bp);
653 return;
654 }
655
656 int
657 wtread(dev, uio, flags)
658 dev_t dev;
659 struct uio *uio;
660 int flags;
661 {
662
663 return (physio(wtstrategy, NULL, dev, B_READ, minphys, uio));
664 }
665
666 int
667 wtwrite(dev, uio, flags)
668 dev_t dev;
669 struct uio *uio;
670 int flags;
671 {
672
673 return (physio(wtstrategy, NULL, dev, B_WRITE, minphys, uio));
674 }
675
676 /*
677 * Interrupt routine.
678 */
679 int
680 wtintr(arg)
681 void *arg;
682 {
683 struct wt_softc *sc = arg;
684 u_char x;
685
686 /* get status */
687 x = bus_space_read_1(sc->sc_iot, sc->sc_ioh, sc->STATPORT);
688 WTDBPRINT(("wtintr() status=0x%x -- ", x));
689 if ((x & (sc->BUSY | sc->NOEXCEP)) == (sc->BUSY | sc->NOEXCEP)) {
690 WTDBPRINT(("busy\n"));
691 return 0; /* device is busy */
692 }
693
694 /*
695 * Check if rewind finished.
696 */
697 if (sc->flags & TPREW) {
698 WTDBPRINT(((x & (sc->BUSY | sc->NOEXCEP)) == (sc->BUSY | sc->NOEXCEP) ?
699 "rewind busy?\n" : "rewind finished\n"));
700 sc->flags &= ~TPREW; /* rewind finished */
701 wtsense(sc, 1, TP_WRP);
702 wakeup((caddr_t)sc);
703 return 1;
704 }
705
706 /*
707 * Check if writing/reading of file mark finished.
708 */
709 if (sc->flags & (TPRMARK | TPWMARK)) {
710 WTDBPRINT(((x & (sc->BUSY | sc->NOEXCEP)) == (sc->BUSY | sc->NOEXCEP) ?
711 "marker r/w busy?\n" : "marker r/w finished\n"));
712 if ((x & sc->NOEXCEP) == 0) /* operation failed */
713 wtsense(sc, 1, (sc->flags & TPRMARK) ? TP_WRP : 0);
714 sc->flags &= ~(TPRMARK | TPWMARK); /* operation finished */
715 wakeup((caddr_t)sc);
716 return 1;
717 }
718
719 /*
720 * Do we started any i/o? If no, just return.
721 */
722 if ((sc->flags & TPACTIVE) == 0) {
723 WTDBPRINT(("unexpected interrupt\n"));
724 return 0;
725 }
726 sc->flags &= ~TPACTIVE;
727 sc->dmacount += sc->bsize; /* increment counter */
728
729 /*
730 * Clean up dma.
731 */
732 if ((sc->dmaflags & DMAMODE_READ) &&
733 (sc->dmatotal - sc->dmacount) < sc->bsize) {
734 /* If reading short block, copy the internal buffer
735 * to the user memory. */
736 isa_dmadone(sc->sc_dev.dv_parent, sc->chan);
737 bcopy(sc->buf, sc->dmavaddr, sc->dmatotal - sc->dmacount);
738 } else
739 isa_dmadone(sc->sc_dev.dv_parent, sc->chan);
740
741 /*
742 * On exception, check for end of file and end of volume.
743 */
744 if ((x & sc->NOEXCEP) == 0) {
745 WTDBPRINT(("i/o exception\n"));
746 wtsense(sc, 1, (sc->dmaflags & DMAMODE_READ) ? TP_WRP : 0);
747 if (sc->error & (TP_EOM | TP_FIL))
748 sc->flags |= TPVOL; /* end of file */
749 else
750 sc->flags |= TPEXCEP; /* i/o error */
751 wakeup((caddr_t)sc);
752 return 1;
753 }
754
755 if (sc->dmacount < sc->dmatotal) {
756 /* Continue I/O. */
757 sc->dmavaddr += sc->bsize;
758 wtdma(sc);
759 WTDBPRINT(("continue i/o, %d\n", sc->dmacount));
760 return 1;
761 }
762 if (sc->dmacount > sc->dmatotal) /* short last block */
763 sc->dmacount = sc->dmatotal;
764 /* Wake up user level. */
765 wakeup((caddr_t)sc);
766 WTDBPRINT(("i/o finished, %d\n", sc->dmacount));
767 return 1;
768 }
769
770 /* start the rewind operation */
771 void
772 wtrewind(sc)
773 struct wt_softc *sc;
774 {
775 int rwmode = sc->flags & (TPRO | TPWO);
776
777 sc->flags &= ~(TPRO | TPWO | TPVOL);
778 /*
779 * Wangtek strictly follows QIC-02 standard:
780 * clearing ONLINE in read/write modes causes rewind.
781 * REWIND command is not allowed in read/write mode
782 * and gives `illegal command' error.
783 */
784 if (sc->type == WANGTEK && rwmode) {
785 bus_space_write_1(sc->sc_iot, sc->sc_ioh, sc->CTLPORT, 0);
786 } else if (!wtcmd(sc, QIC_REWIND))
787 return;
788 sc->flags |= TPSTART | TPREW;
789 wtclock(sc);
790 }
791
792 /*
793 * Start the `read marker' operation.
794 */
795 int
796 wtreadfm(sc)
797 struct wt_softc *sc;
798 {
799
800 sc->flags &= ~(TPRO | TPWO | TPVOL);
801 if (!wtcmd(sc, QIC_READFM)) {
802 wtsense(sc, 1, TP_WRP);
803 return EIO;
804 }
805 sc->flags |= TPRMARK | TPRANY;
806 wtclock(sc);
807 /* Don't wait for completion here. */
808 return 0;
809 }
810
811 /*
812 * Write marker to the tape.
813 */
814 int
815 wtwritefm(sc)
816 struct wt_softc *sc;
817 {
818
819 tsleep((caddr_t)wtwritefm, WTPRI, "wtwfm", hz);
820 sc->flags &= ~(TPRO | TPWO);
821 if (!wtcmd(sc, QIC_WRITEFM)) {
822 wtsense(sc, 1, 0);
823 return EIO;
824 }
825 sc->flags |= TPWMARK | TPWANY;
826 wtclock(sc);
827 return wtwait(sc, 0, "wtwfm");
828 }
829
830 /*
831 * While controller status & mask == bits continue waiting.
832 */
833 u_char
834 wtsoft(sc, mask, bits)
835 struct wt_softc *sc;
836 int mask, bits;
837 {
838 bus_space_tag_t iot = sc->sc_iot;
839 bus_space_handle_t ioh = sc->sc_ioh;
840 u_char x;
841 int i;
842
843
844 /* Poll status port, waiting for specified bits. */
845 for (i = 0; i < 1000; ++i) { /* up to 1 msec */
846 x = bus_space_read_1(iot, ioh, sc->STATPORT);
847 if ((x & mask) != bits)
848 return x;
849 delay(1);
850 }
851 for (i = 0; i < 100; ++i) { /* up to 10 msec */
852 x = bus_space_read_1(iot, ioh, sc->STATPORT);
853 if ((x & mask) != bits)
854 return x;
855 delay(100);
856 }
857 for (;;) { /* forever */
858 x = bus_space_read_1(iot, ioh, sc->STATPORT);
859 if ((x & mask) != bits)
860 return x;
861 tsleep((caddr_t)wtsoft, WTPRI, "wtsoft", 1);
862 }
863 }
864
865 /*
866 * Execute QIC command.
867 */
868 int
869 wtcmd(sc, cmd)
870 struct wt_softc *sc;
871 int cmd;
872 {
873 bus_space_tag_t iot = sc->sc_iot;
874 bus_space_handle_t ioh = sc->sc_ioh;
875 u_char x;
876 int s;
877
878 WTDBPRINT(("wtcmd() cmd=0x%x\n", cmd));
879 s = splbio();
880 x = wtsoft(sc, sc->BUSY | sc->NOEXCEP, sc->BUSY | sc->NOEXCEP); /* ready? */
881 if ((x & sc->NOEXCEP) == 0) { /* error */
882 splx(s);
883 return 0;
884 }
885
886 /* output the command */
887 bus_space_write_1(iot, ioh, sc->CMDPORT, cmd);
888
889 /* set request */
890 bus_space_write_1(iot, ioh, sc->CTLPORT, sc->REQUEST | sc->ONLINE);
891
892 /* wait for ready */
893 wtsoft(sc, sc->BUSY, sc->BUSY);
894
895 /* reset request */
896 bus_space_write_1(iot, ioh, sc->CTLPORT, sc->IEN | sc->ONLINE);
897
898 /* wait for not ready */
899 wtsoft(sc, sc->BUSY, 0);
900 splx(s);
901 return 1;
902 }
903
904 /* wait for the end of i/o, seeking marker or rewind operation */
905 int
906 wtwait(sc, catch, msg)
907 struct wt_softc *sc;
908 int catch;
909 char *msg;
910 {
911 int error;
912
913 WTDBPRINT(("wtwait() `%s'\n", msg));
914 while (sc->flags & (TPACTIVE | TPREW | TPRMARK | TPWMARK))
915 if ((error = tsleep((caddr_t)sc, WTPRI | catch, msg, 0)) != 0)
916 return error;
917 return 0;
918 }
919
920 /* initialize dma for the i/o operation */
921 void
922 wtdma(sc)
923 struct wt_softc *sc;
924 {
925 bus_space_tag_t iot = sc->sc_iot;
926 bus_space_handle_t ioh = sc->sc_ioh;
927
928 sc->flags |= TPACTIVE;
929 wtclock(sc);
930
931 if (sc->type == ARCHIVE) {
932 /* Set DMA. */
933 bus_space_write_1(iot, ioh, sc->SDMAPORT, 0);
934 }
935
936 if ((sc->dmaflags & DMAMODE_READ) &&
937 (sc->dmatotal - sc->dmacount) < sc->bsize) {
938 /* Reading short block; do it through the internal buffer. */
939 isa_dmastart(sc->sc_dev.dv_parent, sc->chan, sc->buf,
940 sc->bsize, NULL, sc->dmaflags, BUS_DMA_NOWAIT);
941 } else
942 isa_dmastart(sc->sc_dev.dv_parent, sc->chan, sc->dmavaddr,
943 sc->bsize, NULL, sc->dmaflags, BUS_DMA_NOWAIT);
944 }
945
946 /* start i/o operation */
947 int
948 wtstart(sc, flag, vaddr, len)
949 struct wt_softc *sc;
950 int flag;
951 void *vaddr;
952 size_t len;
953 {
954 u_char x;
955
956 WTDBPRINT(("wtstart()\n"));
957 x = wtsoft(sc, sc->BUSY | sc->NOEXCEP, sc->BUSY | sc->NOEXCEP); /* ready? */
958 if ((x & sc->NOEXCEP) == 0) {
959 sc->flags |= TPEXCEP; /* error */
960 return 0;
961 }
962 sc->flags &= ~TPEXCEP; /* clear exception flag */
963 sc->dmavaddr = vaddr;
964 sc->dmatotal = len;
965 sc->dmacount = 0;
966 sc->dmaflags = flag & B_READ ? DMAMODE_READ : DMAMODE_WRITE;
967 wtdma(sc);
968 return 1;
969 }
970
971 /*
972 * Start timer.
973 */
974 void
975 wtclock(sc)
976 struct wt_softc *sc;
977 {
978
979 if (sc->flags & TPTIMER)
980 return;
981 sc->flags |= TPTIMER;
982 /*
983 * Some controllers seem to lose dma interrupts too often. To make the
984 * tape stream we need 1 tick timeout.
985 */
986 timeout(wttimer, sc, (sc->flags & TPACTIVE) ? 1 : hz);
987 }
988
989 /*
990 * Simulate an interrupt periodically while i/o is going.
991 * This is necessary in case interrupts get eaten due to
992 * multiple devices on a single IRQ line.
993 */
994 void
995 wttimer(arg)
996 void *arg;
997 {
998 register struct wt_softc *sc = (struct wt_softc *)arg;
999 int s;
1000 u_char status;
1001
1002 sc->flags &= ~TPTIMER;
1003 if ((sc->flags & (TPACTIVE | TPREW | TPRMARK | TPWMARK)) == 0)
1004 return;
1005
1006 /* If i/o going, simulate interrupt. */
1007 s = splbio();
1008 status = bus_space_read_1(sc->sc_iot, sc->sc_ioh, sc->STATPORT);
1009 if ((status & (sc->BUSY | sc->NOEXCEP)) != (sc->BUSY | sc->NOEXCEP)) {
1010 WTDBPRINT(("wttimer() -- "));
1011 wtintr(sc);
1012 }
1013 splx(s);
1014
1015 /* Restart timer if i/o pending. */
1016 if (sc->flags & (TPACTIVE | TPREW | TPRMARK | TPWMARK))
1017 wtclock(sc);
1018 }
1019
1020 /*
1021 * Perform QIC-02 and QIC-36 compatible reset sequence.
1022 */
1023 int
1024 wtreset(sc)
1025 struct wt_softc *sc;
1026 {
1027 bus_space_tag_t iot = sc->sc_iot;
1028 bus_space_handle_t ioh = sc->sc_ioh;
1029 u_char x;
1030 int i;
1031
1032 /* send reset */
1033 bus_space_write_1(iot, ioh, sc->CTLPORT, sc->RESET | sc->ONLINE);
1034 delay(30);
1035 /* turn off reset */
1036 bus_space_write_1(iot, ioh, sc->CTLPORT, sc->ONLINE);
1037 delay(30);
1038
1039 /* Read the controller status. */
1040 x = bus_space_read_1(iot, ioh, sc->STATPORT);
1041 if (x == 0xff) /* no port at this address? */
1042 return 0;
1043
1044 /* Wait 3 sec for reset to complete. Needed for QIC-36 boards? */
1045 for (i = 0; i < 3000; ++i) {
1046 if ((x & sc->BUSY) == 0 || (x & sc->NOEXCEP) == 0)
1047 break;
1048 delay(1000);
1049 x = bus_space_read_1(iot, ioh, sc->STATPORT);
1050 }
1051 return (x & sc->RESETMASK) == sc->RESETVAL;
1052 }
1053
1054 /*
1055 * Get controller status information. Return 0 if user i/o request should
1056 * receive an i/o error code.
1057 */
1058 int
1059 wtsense(sc, verbose, ignore)
1060 struct wt_softc *sc;
1061 int verbose, ignore;
1062 {
1063 char *msg = 0;
1064 int error;
1065
1066 WTDBPRINT(("wtsense() ignore=0x%x\n", ignore));
1067 sc->flags &= ~(TPRO | TPWO);
1068 if (!wtstatus(sc))
1069 return 0;
1070 if ((sc->error & TP_ST0) == 0)
1071 sc->error &= ~TP_ST0MASK;
1072 if ((sc->error & TP_ST1) == 0)
1073 sc->error &= ~TP_ST1MASK;
1074 sc->error &= ~ignore; /* ignore certain errors */
1075 error = sc->error & (TP_FIL | TP_BNL | TP_UDA | TP_EOM | TP_WRP |
1076 TP_USL | TP_CNI | TP_MBD | TP_NDT | TP_ILL);
1077 if (!error)
1078 return 1;
1079 if (!verbose)
1080 return 0;
1081
1082 /* lifted from tdriver.c from Wangtek */
1083 if (error & TP_USL)
1084 msg = "Drive not online";
1085 else if (error & TP_CNI)
1086 msg = "No cartridge";
1087 else if ((error & TP_WRP) && (sc->flags & TPWP) == 0) {
1088 msg = "Tape is write protected";
1089 sc->flags |= TPWP;
1090 } else if (error & TP_FIL)
1091 msg = 0 /*"Filemark detected"*/;
1092 else if (error & TP_EOM)
1093 msg = 0 /*"End of tape"*/;
1094 else if (error & TP_BNL)
1095 msg = "Block not located";
1096 else if (error & TP_UDA)
1097 msg = "Unrecoverable data error";
1098 else if (error & TP_NDT)
1099 msg = "No data detected";
1100 else if (error & TP_ILL)
1101 msg = "Illegal command";
1102 if (msg)
1103 printf("%s: %s\n", sc->sc_dev.dv_xname, msg);
1104 return 0;
1105 }
1106
1107 /*
1108 * Get controller status information.
1109 */
1110 int
1111 wtstatus(sc)
1112 struct wt_softc *sc;
1113 {
1114 bus_space_tag_t iot = sc->sc_iot;
1115 bus_space_handle_t ioh = sc->sc_ioh;
1116 char *p;
1117 int s;
1118
1119 s = splbio();
1120 wtsoft(sc, sc->BUSY | sc->NOEXCEP, sc->BUSY | sc->NOEXCEP); /* ready? */
1121 /* send `read status' command */
1122 bus_space_write_1(iot, ioh, sc->CMDPORT, QIC_RDSTAT);
1123
1124 /* set request */
1125 bus_space_write_1(iot, ioh, sc->CTLPORT, sc->REQUEST | sc->ONLINE);
1126
1127 /* wait for ready */
1128 wtsoft(sc, sc->BUSY, sc->BUSY);
1129 /* reset request */
1130 bus_space_write_1(iot, ioh, sc->CTLPORT, sc->ONLINE);
1131
1132 /* wait for not ready */
1133 wtsoft(sc, sc->BUSY, 0);
1134
1135 p = (char *)&sc->error;
1136 while (p < (char *)&sc->error + 6) {
1137 u_char x = wtsoft(sc, sc->BUSY | sc->NOEXCEP,
1138 sc->BUSY | sc->NOEXCEP);
1139
1140 if ((x & sc->NOEXCEP) == 0) { /* error */
1141 splx(s);
1142 return 0;
1143 }
1144
1145 /* read status byte */
1146 *p++ = bus_space_read_1(iot, ioh, sc->DATAPORT);
1147
1148 /* set request */
1149 bus_space_write_1(iot, ioh, sc->CTLPORT,
1150 sc->REQUEST | sc->ONLINE);
1151
1152 /* wait for not ready */
1153 wtsoft(sc, sc->BUSY, 0);
1154
1155 /* unset request */
1156 bus_space_write_1(iot, ioh, sc->CTLPORT, sc->ONLINE);
1157 }
1158 splx(s);
1159 return 1;
1160 }
1161