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