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