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