1 /* $NetBSD: mt.c,v 1.25 2004/08/28 17:37:02 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 /* 40 * Copyright (c) 1992, The University of Utah and 41 * the Computer Systems Laboratory at the University of Utah (CSL). 42 * All rights reserved. 43 * 44 * Permission to use, copy, modify and distribute this software is hereby 45 * granted provided that (1) source code retains these copyright, permission, 46 * and disclaimer notices, and (2) redistributions including binaries 47 * reproduce the notices in supporting documentation, and (3) all advertising 48 * materials mentioning features or use of this software display the following 49 * acknowledgement: ``This product includes software developed by the 50 * Computer Systems Laboratory at the University of Utah.'' 51 * 52 * THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS 53 * IS" CONDITION. THE UNIVERSITY OF UTAH AND CSL DISCLAIM ANY LIABILITY OF 54 * ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 55 * 56 * CSL requests users of this software to return to csl-dist@cs.utah.edu any 57 * improvements that they make and grant CSL redistribution rights. 58 * 59 * Utah $Hdr: mt.c 1.8 95/09/12$ 60 */ 61 /* @(#)mt.c 3.9 90/07/10 mt Xinu 62 * 63 * Magnetic tape driver (7974a, 7978a/b, 7979a, 7980a, 7980xc) 64 * Original version contributed by Mt. Xinu. 65 * Modified for 4.4BSD by Mark Davies and Andrew Vignaux, Department of 66 * Computer Science, Victoria University of Wellington 67 */ 68 69 #include <sys/cdefs.h> 70 __KERNEL_RCSID(0, "$NetBSD: mt.c,v 1.25 2004/08/28 17:37:02 thorpej Exp $"); 71 72 #include <sys/param.h> 73 #include <sys/systm.h> 74 #include <sys/callout.h> 75 #include <sys/buf.h> 76 #include <sys/ioctl.h> 77 #include <sys/mtio.h> 78 #include <sys/file.h> 79 #include <sys/proc.h> 80 #include <sys/errno.h> 81 #include <sys/syslog.h> 82 #include <sys/tty.h> 83 #include <sys/kernel.h> 84 #include <sys/tprintf.h> 85 #include <sys/device.h> 86 #include <sys/conf.h> 87 88 #include <hp300/dev/hpibvar.h> 89 90 #include <hp300/dev/mtreg.h> 91 92 static const struct mtinfo { 93 u_short hwid; 94 char *desc; 95 } mtinfo[] = { 96 { MT7978ID, "7978" }, 97 { MT7979AID, "7979A" }, 98 { MT7980ID, "7980" }, 99 { MT7974AID, "7974A" }, 100 }; 101 static const int nmtinfo = sizeof(mtinfo) / sizeof(mtinfo[0]); 102 103 struct mt_softc { 104 struct device sc_dev; 105 struct callout sc_start_ch; 106 struct callout sc_intr_ch; 107 int sc_hpibno; /* logical HPIB this slave it attached to */ 108 int sc_slave; /* HPIB slave address (0-6) */ 109 short sc_flags; /* see below */ 110 u_char sc_lastdsj; /* place for DSJ in mtreaddsj() */ 111 u_char sc_lastecmd; /* place for End Command in mtreaddsj() */ 112 short sc_recvtimeo; /* count of hpibsend timeouts to prevent hang */ 113 short sc_statindex; /* index for next sc_stat when MTF_STATTIMEO */ 114 struct mt_stat sc_stat;/* status bytes last read from device */ 115 short sc_density; /* current density of tape (mtio.h format) */ 116 short sc_type; /* tape drive model (hardware IDs) */ 117 struct hpibqueue sc_hq; /* HPIB device queue member */ 118 tpr_t sc_ttyp; 119 struct bufq_state sc_tab;/* buf queue */ 120 int sc_active; 121 struct buf sc_bufstore; /* XXX buffer storage */ 122 }; 123 124 #ifdef DEBUG 125 int mtdebug = 0; 126 #define dlog if (mtdebug) log 127 #else 128 #define dlog if (0) log 129 #endif 130 131 #define UNIT(x) (minor(x) & 3) 132 133 #define B_CMD B_XXX /* command buf instead of data */ 134 #define b_cmd b_blkno /* blkno holds cmd when B_CMD */ 135 136 static int mtmatch(struct device *, struct cfdata *, void *); 137 static void mtattach(struct device *, struct device *, void *); 138 139 CFATTACH_DECL(mt, sizeof(struct mt_softc), 140 mtmatch, mtattach, NULL, NULL); 141 142 extern struct cfdriver mt_cd; 143 144 static dev_type_open(mtopen); 145 static dev_type_close(mtclose); 146 static dev_type_read(mtread); 147 static dev_type_write(mtwrite); 148 static dev_type_ioctl(mtioctl); 149 static dev_type_strategy(mtstrategy); 150 151 const struct bdevsw mt_bdevsw = { 152 mtopen, mtclose, mtstrategy, mtioctl, nodump, nosize, D_TAPE 153 }; 154 155 const struct cdevsw mt_cdevsw = { 156 mtopen, mtclose, mtread, mtwrite, mtioctl, 157 nostop, notty, nopoll, nommap, nokqfilter, D_TAPE 158 }; 159 160 static int mtident(struct mt_softc *, struct hpibbus_attach_args *); 161 static void mtustart(struct mt_softc *); 162 static int mtreaddsj(struct mt_softc *, int); 163 static int mtcommand(dev_t, int, int); 164 static void spl_mtintr(void *); 165 static void spl_mtstart(void *); 166 167 static void mtstart(void *); 168 static void mtgo(void *); 169 static void mtintr(void *); 170 171 static int 172 mtmatch(struct device *parent, struct cfdata *match, void *aux) 173 { 174 struct hpibbus_attach_args *ha = aux; 175 176 return (mtident(NULL, ha)); 177 } 178 179 static void 180 mtattach(struct device *parent, struct device *self, void *aux) 181 { 182 struct mt_softc *sc = (struct mt_softc *)self; 183 struct hpibbus_attach_args *ha = aux; 184 int unit, hpibno, slave; 185 186 if (mtident(sc, ha) == 0) { 187 printf("\n%s: impossible!\n", sc->sc_dev.dv_xname); 188 return; 189 } 190 191 unit = self->dv_unit; 192 hpibno = parent->dv_unit; 193 slave = ha->ha_slave; 194 195 bufq_alloc(&sc->sc_tab, BUFQ_FCFS); 196 callout_init(&sc->sc_start_ch); 197 callout_init(&sc->sc_intr_ch); 198 199 sc->sc_hpibno = hpibno; 200 sc->sc_slave = slave; 201 sc->sc_flags = MTF_EXISTS; 202 203 /* Initialize hpib job queue entry. */ 204 sc->sc_hq.hq_softc = sc; 205 sc->sc_hq.hq_slave = sc->sc_slave; 206 sc->sc_hq.hq_start = mtstart; 207 sc->sc_hq.hq_go = mtgo; 208 sc->sc_hq.hq_intr = mtintr; 209 } 210 211 static int 212 mtident(struct mt_softc *sc, struct hpibbus_attach_args *ha) 213 { 214 int i; 215 216 for (i = 0; i < nmtinfo; i++) { 217 if (ha->ha_id == mtinfo[i].hwid) { 218 if (sc != NULL) { 219 sc->sc_type = mtinfo[i].hwid; 220 printf(": %s tape\n", mtinfo[i].desc); 221 } 222 return (1); 223 } 224 } 225 return (0); 226 } 227 228 /* 229 * Perform a read of "Device Status Jump" register and update the 230 * status if necessary. If status is read, the given "ecmd" is also 231 * performed, unless "ecmd" is zero. Returns DSJ value, -1 on failure 232 * and -2 on "temporary" failure. 233 */ 234 static int 235 mtreaddsj(struct mt_softc *sc, int ecmd) 236 { 237 int retval; 238 239 if (sc->sc_flags & MTF_STATTIMEO) 240 goto getstats; 241 retval = hpibrecv(sc->sc_hpibno, 242 (sc->sc_flags & MTF_DSJTIMEO) ? -1 : sc->sc_slave, 243 MTT_DSJ, &(sc->sc_lastdsj), 1); 244 sc->sc_flags &= ~MTF_DSJTIMEO; 245 if (retval != 1) { 246 dlog(LOG_DEBUG, "%s can't hpibrecv DSJ", 247 sc->sc_dev.dv_xname); 248 if (sc->sc_recvtimeo == 0) 249 sc->sc_recvtimeo = hz; 250 if (--sc->sc_recvtimeo == 0) 251 return (-1); 252 if (retval == 0) 253 sc->sc_flags |= MTF_DSJTIMEO; 254 return (-2); 255 } 256 sc->sc_recvtimeo = 0; 257 sc->sc_statindex = 0; 258 dlog(LOG_DEBUG, "%s readdsj: 0x%x", sc->sc_dev.dv_xname, 259 sc->sc_lastdsj); 260 sc->sc_lastecmd = ecmd; 261 switch (sc->sc_lastdsj) { 262 case 0: 263 if (ecmd & MTE_DSJ_FORCE) 264 break; 265 return (0); 266 267 case 2: 268 sc->sc_lastecmd = MTE_COMPLETE; 269 case 1: 270 break; 271 272 default: 273 log(LOG_ERR, "%s readdsj: DSJ 0x%x\n", sc->sc_dev.dv_xname, 274 sc->sc_lastdsj); 275 return (-1); 276 } 277 getstats: 278 retval = hpibrecv(sc->sc_hpibno, 279 (sc->sc_flags & MTF_STATCONT) ? -1 : sc->sc_slave, 280 MTT_STAT, ((char *)&(sc->sc_stat)) + sc->sc_statindex, 281 sizeof(sc->sc_stat) - sc->sc_statindex); 282 sc->sc_flags &= ~(MTF_STATTIMEO | MTF_STATCONT); 283 if (retval != sizeof(sc->sc_stat) - sc->sc_statindex) { 284 if (sc->sc_recvtimeo == 0) 285 sc->sc_recvtimeo = hz; 286 if (--sc->sc_recvtimeo != 0) { 287 if (retval >= 0) { 288 sc->sc_statindex += retval; 289 sc->sc_flags |= MTF_STATCONT; 290 } 291 sc->sc_flags |= MTF_STATTIMEO; 292 return (-2); 293 } 294 log(LOG_ERR, "%s readdsj: can't read status", 295 sc->sc_dev.dv_xname); 296 return (-1); 297 } 298 sc->sc_recvtimeo = 0; 299 sc->sc_statindex = 0; 300 dlog(LOG_DEBUG, "%s readdsj: status is %x %x %x %x %x %x", 301 sc->sc_dev.dv_xname, 302 sc->sc_stat1, sc->sc_stat2, sc->sc_stat3, 303 sc->sc_stat4, sc->sc_stat5, sc->sc_stat6); 304 if (sc->sc_lastecmd) 305 (void) hpibsend(sc->sc_hpibno, sc->sc_slave, 306 MTL_ECMD, &(sc->sc_lastecmd), 1); 307 return ((int) sc->sc_lastdsj); 308 } 309 310 static int 311 mtopen(dev_t dev, int flag, int mode, struct proc *p) 312 { 313 int unit = UNIT(dev); 314 struct mt_softc *sc; 315 int req_den; 316 int error; 317 318 if (unit >= mt_cd.cd_ndevs || 319 (sc = mt_cd.cd_devs[unit]) == NULL || 320 (sc->sc_flags & MTF_EXISTS) == 0) 321 return (ENXIO); 322 323 dlog(LOG_DEBUG, "%s open: flags 0x%x", sc->sc_dev.dv_xname, 324 sc->sc_flags); 325 if (sc->sc_flags & MTF_OPEN) 326 return (EBUSY); 327 sc->sc_flags |= MTF_OPEN; 328 sc->sc_ttyp = tprintf_open(p); 329 if ((sc->sc_flags & MTF_ALIVE) == 0) { 330 error = mtcommand(dev, MTRESET, 0); 331 if (error != 0 || (sc->sc_flags & MTF_ALIVE) == 0) 332 goto errout; 333 if ((sc->sc_stat1 & (SR1_BOT | SR1_ONLINE)) == SR1_ONLINE) 334 (void) mtcommand(dev, MTREW, 0); 335 } 336 for (;;) { 337 if ((error = mtcommand(dev, MTNOP, 0)) != 0) 338 goto errout; 339 if (!(sc->sc_flags & MTF_REW)) 340 break; 341 if (tsleep((caddr_t) &lbolt, PCATCH | (PZERO + 1), 342 "mt", 0) != 0) { 343 error = EINTR; 344 goto errout; 345 } 346 } 347 if ((flag & FWRITE) && (sc->sc_stat1 & SR1_RO)) { 348 error = EROFS; 349 goto errout; 350 } 351 if (!(sc->sc_stat1 & SR1_ONLINE)) { 352 uprintf("%s: not online\n", sc->sc_dev.dv_xname); 353 error = EIO; 354 goto errout; 355 } 356 /* 357 * Select density: 358 * - find out what density the drive is set to 359 * (i.e. the density of the current tape) 360 * - if we are going to write 361 * - if we're not at the beginning of the tape 362 * - complain if we want to change densities 363 * - otherwise, select the mtcommand to set the density 364 * 365 * If the drive doesn't support it then don't change the recorded 366 * density. 367 * 368 * The original MOREbsd code had these additional conditions 369 * for the mid-tape change 370 * 371 * req_den != T_BADBPI && 372 * sc->sc_density != T_6250BPI 373 * 374 * which suggests that it would be possible to write multiple 375 * densities if req_den == T_BAD_BPI or the current tape 376 * density was 6250. Testing of our 7980 suggests that the 377 * device cannot change densities mid-tape. 378 * 379 * ajv@comp.vuw.ac.nz 380 */ 381 sc->sc_density = (sc->sc_stat2 & SR2_6250) ? T_6250BPI : ( 382 (sc->sc_stat3 & SR3_1600) ? T_1600BPI : ( 383 (sc->sc_stat3 & SR3_800) ? T_800BPI : -1)); 384 req_den = (dev & T_DENSEL); 385 386 if (flag & FWRITE) { 387 if (!(sc->sc_stat1 & SR1_BOT)) { 388 if (sc->sc_density != req_den) { 389 uprintf("%s: can't change density mid-tape\n", 390 sc->sc_dev.dv_xname); 391 error = EIO; 392 goto errout; 393 } 394 } 395 else { 396 int mtset_density = 397 (req_den == T_800BPI ? MTSET800BPI : ( 398 req_den == T_1600BPI ? MTSET1600BPI : ( 399 req_den == T_6250BPI ? MTSET6250BPI : ( 400 sc->sc_type == MT7980ID 401 ? MTSET6250DC 402 : MTSET6250BPI)))); 403 if (mtcommand(dev, mtset_density, 0) == 0) 404 sc->sc_density = req_den; 405 } 406 } 407 return (0); 408 errout: 409 sc->sc_flags &= ~MTF_OPEN; 410 return (error); 411 } 412 413 static int 414 mtclose(dev_t dev, int flag, int fmt, struct proc *p) 415 { 416 struct mt_softc *sc = mt_cd.cd_devs[UNIT(dev)]; 417 418 if (sc->sc_flags & MTF_WRT) { 419 (void) mtcommand(dev, MTWEOF, 2); 420 (void) mtcommand(dev, MTBSF, 0); 421 } 422 if ((minor(dev) & T_NOREWIND) == 0) 423 (void) mtcommand(dev, MTREW, 0); 424 sc->sc_flags &= ~MTF_OPEN; 425 tprintf_close(sc->sc_ttyp); 426 return (0); 427 } 428 429 static int 430 mtcommand(dev_t dev, int cmd, int cnt) 431 { 432 struct mt_softc *sc = mt_cd.cd_devs[UNIT(dev)]; 433 struct buf *bp = &sc->sc_bufstore; 434 int error = 0; 435 436 #if 1 437 if (bp->b_flags & B_BUSY) 438 return (EBUSY); 439 #endif 440 bp->b_cmd = cmd; 441 bp->b_dev = dev; 442 do { 443 bp->b_flags = B_BUSY | B_CMD; 444 mtstrategy(bp); 445 biowait(bp); 446 if (bp->b_flags & B_ERROR) { 447 error = (int) (unsigned) bp->b_error; 448 break; 449 } 450 } while (--cnt > 0); 451 #if 0 452 bp->b_flags = 0 /*&= ~B_BUSY*/; 453 #else 454 bp->b_flags &= ~B_BUSY; 455 #endif 456 return (error); 457 } 458 459 /* 460 * Only thing to check here is for legal record lengths (writes only). 461 */ 462 static void 463 mtstrategy(struct buf *bp) 464 { 465 struct mt_softc *sc; 466 int unit; 467 int s; 468 469 unit = UNIT(bp->b_dev); 470 sc = mt_cd.cd_devs[unit]; 471 dlog(LOG_DEBUG, "%s strategy", sc->sc_dev.dv_xname); 472 if ((bp->b_flags & (B_CMD | B_READ)) == 0) { 473 #define WRITE_BITS_IGNORED 8 474 #if 0 475 if (bp->b_bcount & ((1 << WRITE_BITS_IGNORED) - 1)) { 476 tprintf(sc->sc_ttyp, 477 "%s: write record must be multiple of %d\n", 478 sc->sc_dev.dv_xname, 1 << WRITE_BITS_IGNORED); 479 goto error; 480 } 481 #endif 482 s = 16 * 1024; 483 if (sc->sc_stat2 & SR2_LONGREC) { 484 switch (sc->sc_density) { 485 case T_1600BPI: 486 s = 32 * 1024; 487 break; 488 489 case T_6250BPI: 490 case T_BADBPI: 491 s = 60 * 1024; 492 break; 493 } 494 } 495 if (bp->b_bcount > s) { 496 tprintf(sc->sc_ttyp, 497 "%s: write record (%ld) too big: limit (%d)\n", 498 sc->sc_dev.dv_xname, bp->b_bcount, s); 499 #if 0 /* XXX see above */ 500 error: 501 #endif 502 bp->b_flags |= B_ERROR; 503 bp->b_error = EIO; 504 biodone(bp); 505 return; 506 } 507 } 508 s = splbio(); 509 BUFQ_PUT(&sc->sc_tab, bp); 510 if (sc->sc_active == 0) { 511 sc->sc_active = 1; 512 mtustart(sc); 513 } 514 splx(s); 515 } 516 517 static void 518 mtustart(struct mt_softc *sc) 519 { 520 521 dlog(LOG_DEBUG, "%s ustart", sc->sc_dev.dv_xname); 522 if (hpibreq(sc->sc_dev.dv_parent, &sc->sc_hq)) 523 mtstart(sc); 524 } 525 526 static void 527 spl_mtintr(void *arg) 528 { 529 struct mt_softc *sc = arg; 530 int s = splbio(); 531 532 hpibppclear(sc->sc_hpibno); 533 mtintr(sc); 534 splx(s); 535 } 536 537 static void 538 spl_mtstart(void *arg) 539 { 540 int s = splbio(); 541 542 mtstart(arg); 543 splx(s); 544 } 545 546 static void 547 mtstart(void *arg) 548 { 549 struct mt_softc *sc = arg; 550 struct buf *bp; 551 short cmdcount = 1; 552 u_char cmdbuf[2]; 553 554 dlog(LOG_DEBUG, "%s start", sc->sc_dev.dv_xname); 555 sc->sc_flags &= ~MTF_WRT; 556 bp = BUFQ_PEEK(&sc->sc_tab); 557 if ((sc->sc_flags & MTF_ALIVE) == 0 && 558 ((bp->b_flags & B_CMD) == 0 || bp->b_cmd != MTRESET)) 559 goto fatalerror; 560 561 if (sc->sc_flags & MTF_REW) { 562 if (!hpibpptest(sc->sc_hpibno, sc->sc_slave)) 563 goto stillrew; 564 switch (mtreaddsj(sc, MTE_DSJ_FORCE|MTE_COMPLETE|MTE_IDLE)) { 565 case 0: 566 case 1: 567 stillrew: 568 if ((sc->sc_stat1 & SR1_BOT) || 569 !(sc->sc_stat1 & SR1_ONLINE)) { 570 sc->sc_flags &= ~MTF_REW; 571 break; 572 } 573 case -2: 574 /* 575 * -2 means "timeout" reading DSJ, which is probably 576 * temporary. This is considered OK when doing a NOP, 577 * but not otherwise. 578 */ 579 if (sc->sc_flags & (MTF_DSJTIMEO | MTF_STATTIMEO)) { 580 callout_reset(&sc->sc_start_ch, hz >> 5, 581 spl_mtstart, sc); 582 return; 583 } 584 case 2: 585 if (bp->b_cmd != MTNOP || !(bp->b_flags & B_CMD)) { 586 bp->b_error = EBUSY; 587 goto errdone; 588 } 589 goto done; 590 591 default: 592 goto fatalerror; 593 } 594 } 595 if (bp->b_flags & B_CMD) { 596 if (sc->sc_flags & MTF_PASTEOT) { 597 switch(bp->b_cmd) { 598 case MTFSF: 599 case MTWEOF: 600 case MTFSR: 601 bp->b_error = ENOSPC; 602 goto errdone; 603 604 case MTBSF: 605 case MTOFFL: 606 case MTBSR: 607 case MTREW: 608 sc->sc_flags &= ~(MTF_PASTEOT | MTF_ATEOT); 609 break; 610 } 611 } 612 switch(bp->b_cmd) { 613 case MTFSF: 614 if (sc->sc_flags & MTF_HITEOF) 615 goto done; 616 cmdbuf[0] = MTTC_FSF; 617 break; 618 619 case MTBSF: 620 if (sc->sc_flags & MTF_HITBOF) 621 goto done; 622 cmdbuf[0] = MTTC_BSF; 623 break; 624 625 case MTOFFL: 626 sc->sc_flags |= MTF_REW; 627 cmdbuf[0] = MTTC_REWOFF; 628 break; 629 630 case MTWEOF: 631 cmdbuf[0] = MTTC_WFM; 632 break; 633 634 case MTBSR: 635 cmdbuf[0] = MTTC_BSR; 636 break; 637 638 case MTFSR: 639 cmdbuf[0] = MTTC_FSR; 640 break; 641 642 case MTREW: 643 sc->sc_flags |= MTF_REW; 644 cmdbuf[0] = MTTC_REW; 645 break; 646 647 case MTNOP: 648 /* 649 * NOP is supposed to set status bits. 650 * Force readdsj to do it. 651 */ 652 switch (mtreaddsj(sc, 653 MTE_DSJ_FORCE | MTE_COMPLETE | MTE_IDLE)) { 654 default: 655 goto done; 656 657 case -1: 658 /* 659 * If this fails, perform a device clear 660 * to fix any protocol problems and (most 661 * likely) get the status. 662 */ 663 bp->b_cmd = MTRESET; 664 break; 665 666 case -2: 667 callout_reset(&sc->sc_start_ch, hz >> 5, 668 spl_mtstart, sc); 669 return; 670 } 671 672 case MTRESET: 673 /* 674 * 1) selected device clear (send with "-2" secondary) 675 * 2) set timeout, then wait for "service request" 676 * 3) interrupt will read DSJ (and END COMPLETE-IDLE) 677 */ 678 if (hpibsend(sc->sc_hpibno, sc->sc_slave, -2, NULL, 0)){ 679 log(LOG_ERR, "%s can't reset", 680 sc->sc_dev.dv_xname); 681 goto fatalerror; 682 } 683 callout_reset(&sc->sc_intr_ch, 4 * hz, spl_mtintr, sc); 684 hpibawait(sc->sc_hpibno); 685 return; 686 687 case MTSET800BPI: 688 cmdbuf[0] = MTTC_800; 689 break; 690 691 case MTSET1600BPI: 692 cmdbuf[0] = MTTC_1600; 693 break; 694 695 case MTSET6250BPI: 696 cmdbuf[0] = MTTC_6250; 697 break; 698 699 case MTSET6250DC: 700 cmdbuf[0] = MTTC_DC6250; 701 break; 702 } 703 } else { 704 if (sc->sc_flags & MTF_PASTEOT) { 705 bp->b_error = ENOSPC; 706 goto errdone; 707 } 708 if (bp->b_flags & B_READ) { 709 sc->sc_flags |= MTF_IO; 710 cmdbuf[0] = MTTC_READ; 711 } else { 712 sc->sc_flags |= MTF_WRT | MTF_IO; 713 cmdbuf[0] = MTTC_WRITE; 714 cmdbuf[1] = (bp->b_bcount + ((1 << WRITE_BITS_IGNORED) - 1)) >> WRITE_BITS_IGNORED; 715 cmdcount = 2; 716 } 717 } 718 if (hpibsend(sc->sc_hpibno, sc->sc_slave, MTL_TCMD, cmdbuf, cmdcount) 719 == cmdcount) { 720 if (sc->sc_flags & MTF_REW) 721 goto done; 722 hpibawait(sc->sc_hpibno); 723 return; 724 } 725 fatalerror: 726 /* 727 * If anything fails, the drive is probably hosed, so mark it not 728 * "ALIVE" (but it EXISTS and is OPEN or we wouldn't be here, and 729 * if, last we heard, it was REWinding, remember that). 730 */ 731 sc->sc_flags &= MTF_EXISTS | MTF_OPEN | MTF_REW; 732 bp->b_error = EIO; 733 errdone: 734 bp->b_flags |= B_ERROR; 735 done: 736 sc->sc_flags &= ~(MTF_HITEOF | MTF_HITBOF); 737 (void)BUFQ_GET(&sc->sc_tab); 738 biodone(bp); 739 hpibfree(sc->sc_dev.dv_parent, &sc->sc_hq); 740 if ((bp = BUFQ_PEEK(&sc->sc_tab)) == NULL) 741 sc->sc_active = 0; 742 else 743 mtustart(sc); 744 } 745 746 /* 747 * The Utah code had a bug which meant that the driver was unable to read. 748 * "rw" was initialized to bp->b_flags & B_READ before "bp" was initialized. 749 * -- ajv@comp.vuw.ac.nz 750 */ 751 static void 752 mtgo(void *arg) 753 { 754 struct mt_softc *sc = arg; 755 struct buf *bp; 756 int rw; 757 758 dlog(LOG_DEBUG, "%s go", sc->sc_dev.dv_xname); 759 bp = BUFQ_PEEK(&sc->sc_tab); 760 rw = bp->b_flags & B_READ; 761 hpibgo(sc->sc_hpibno, sc->sc_slave, rw ? MTT_READ : MTL_WRITE, 762 bp->b_data, bp->b_bcount, rw, rw != 0); 763 } 764 765 static void 766 mtintr(void *arg) 767 { 768 struct mt_softc *sc = arg; 769 struct buf *bp; 770 int i; 771 u_char cmdbuf[4]; 772 773 bp = BUFQ_PEEK(&sc->sc_tab); 774 if (bp == NULL) { 775 log(LOG_ERR, "%s intr: bp == NULL", sc->sc_dev.dv_xname); 776 return; 777 } 778 779 dlog(LOG_DEBUG, "%s intr", sc->sc_dev.dv_xname); 780 781 /* 782 * Some operation completed. Read status bytes and report errors. 783 * Clear EOF flags here `cause they're set once on specific conditions 784 * below when a command succeeds. 785 * A DSJ of 2 always means keep waiting. If the command was READ 786 * (and we're in data DMA phase) stop data transfer first. 787 */ 788 sc->sc_flags &= ~(MTF_HITEOF | MTF_HITBOF); 789 if ((bp->b_flags & (B_CMD|B_READ)) == B_READ && 790 !(sc->sc_flags & (MTF_IO | MTF_STATTIMEO | MTF_DSJTIMEO))){ 791 cmdbuf[0] = MTE_STOP; 792 (void) hpibsend(sc->sc_hpibno, sc->sc_slave, MTL_ECMD,cmdbuf,1); 793 } 794 switch (mtreaddsj(sc, 0)) { 795 case 0: 796 break; 797 798 case 1: 799 /* 800 * If we're in the middle of a READ/WRITE and have yet to 801 * start the data transfer, a DSJ of one should terminate it. 802 */ 803 sc->sc_flags &= ~MTF_IO; 804 break; 805 806 case 2: 807 (void) hpibawait(sc->sc_hpibno); 808 return; 809 810 case -2: 811 /* 812 * -2 means that the drive failed to respond quickly enough 813 * to the request for DSJ. It's probably just "busy" figuring 814 * it out and will know in a little bit... 815 */ 816 callout_reset(&sc->sc_intr_ch, hz >> 5, spl_mtintr, sc); 817 return; 818 819 default: 820 log(LOG_ERR, "%s intr: can't get drive stat", 821 sc->sc_dev.dv_xname); 822 goto error; 823 } 824 if (sc->sc_stat1 & (SR1_ERR | SR1_REJECT)) { 825 i = sc->sc_stat4 & SR4_ERCLMASK; 826 log(LOG_ERR, "%s: %s error, retry %d, SR2/3 %x/%x, code %d", 827 sc->sc_dev.dv_xname, i == SR4_DEVICE ? "device" : 828 (i == SR4_PROTOCOL ? "protocol" : 829 (i == SR4_SELFTEST ? "selftest" : "unknown")), 830 sc->sc_stat4 & SR4_RETRYMASK, sc->sc_stat2, 831 sc->sc_stat3, sc->sc_stat5); 832 833 if ((bp->b_flags & B_CMD) && bp->b_cmd == MTRESET) 834 callout_stop(&sc->sc_intr_ch); 835 if (sc->sc_stat3 & SR3_POWERUP) 836 sc->sc_flags &= MTF_OPEN | MTF_EXISTS; 837 goto error; 838 } 839 /* 840 * Report and clear any soft errors. 841 */ 842 if (sc->sc_stat1 & SR1_SOFTERR) { 843 log(LOG_WARNING, "%s: soft error, retry %d\n", 844 sc->sc_dev.dv_xname, sc->sc_stat4 & SR4_RETRYMASK); 845 sc->sc_stat1 &= ~SR1_SOFTERR; 846 } 847 /* 848 * We've initiated a read or write, but haven't actually started to 849 * DMA the data yet. At this point, the drive's ready. 850 */ 851 if (sc->sc_flags & MTF_IO) { 852 sc->sc_flags &= ~MTF_IO; 853 if (hpibustart(sc->sc_hpibno)) 854 mtgo(sc); 855 return; 856 } 857 /* 858 * Check for End Of Tape - we're allowed to hit EOT and then write (or 859 * read) one more record. If we get here and have not already hit EOT, 860 * return ENOSPC to inform the process that it's hit it. If we get 861 * here and HAVE already hit EOT, don't allow any more operations that 862 * move the tape forward. 863 */ 864 if (sc->sc_stat1 & SR1_EOT) { 865 if (sc->sc_flags & MTF_ATEOT) 866 sc->sc_flags |= MTF_PASTEOT; 867 else { 868 bp->b_flags |= B_ERROR; 869 bp->b_error = ENOSPC; 870 sc->sc_flags |= MTF_ATEOT; 871 } 872 } 873 /* 874 * If a motion command was being executed, check for Tape Marks. 875 * If we were doing data, make sure we got the right amount, and 876 * check for hitting tape marks on reads. 877 */ 878 if (bp->b_flags & B_CMD) { 879 if (sc->sc_stat1 & SR1_EOF) { 880 if (bp->b_cmd == MTFSR) 881 sc->sc_flags |= MTF_HITEOF; 882 if (bp->b_cmd == MTBSR) 883 sc->sc_flags |= MTF_HITBOF; 884 } 885 if (bp->b_cmd == MTRESET) { 886 callout_stop(&sc->sc_intr_ch); 887 sc->sc_flags |= MTF_ALIVE; 888 } 889 } else { 890 i = hpibrecv(sc->sc_hpibno, sc->sc_slave, MTT_BCNT, cmdbuf, 2); 891 if (i != 2) { 892 log(LOG_ERR, "%s intr: can't get xfer length\n", 893 sc->sc_dev.dv_xname); 894 goto error; 895 } 896 i = (int) *((u_short *) cmdbuf); 897 if (i <= bp->b_bcount) { 898 if (i == 0) 899 sc->sc_flags |= MTF_HITEOF; 900 bp->b_resid = bp->b_bcount - i; 901 dlog(LOG_DEBUG, "%s intr: bcount %ld, resid %ld", 902 sc->sc_dev.dv_xname, bp->b_bcount, bp->b_resid); 903 } else { 904 tprintf(sc->sc_ttyp, 905 "%s: record (%d) larger than wanted (%ld)\n", 906 sc->sc_dev.dv_xname, i, bp->b_bcount); 907 error: 908 sc->sc_flags &= ~MTF_IO; 909 bp->b_error = EIO; 910 bp->b_flags |= B_ERROR; 911 } 912 } 913 /* 914 * The operation is completely done. 915 * Let the drive know with an END command. 916 */ 917 cmdbuf[0] = MTE_COMPLETE | MTE_IDLE; 918 (void) hpibsend(sc->sc_hpibno, sc->sc_slave, MTL_ECMD, cmdbuf, 1); 919 bp->b_flags &= ~B_CMD; 920 (void)BUFQ_GET(&sc->sc_tab); 921 biodone(bp); 922 hpibfree(sc->sc_dev.dv_parent, &sc->sc_hq); 923 if (BUFQ_PEEK(&sc->sc_tab) == NULL) 924 sc->sc_active = 0; 925 else 926 mtustart(sc); 927 } 928 929 static int 930 mtread(dev_t dev, struct uio *uio, int flags) 931 { 932 struct mt_softc *sc = mt_cd.cd_devs[UNIT(dev)]; 933 934 return(physio(mtstrategy, &sc->sc_bufstore, 935 dev, B_READ, minphys, uio)); 936 } 937 938 static int 939 mtwrite(dev_t dev, struct uio *uio, int flags) 940 { 941 struct mt_softc *sc = mt_cd.cd_devs[UNIT(dev)]; 942 943 return(physio(mtstrategy, &sc->sc_bufstore, 944 dev, B_WRITE, minphys, uio)); 945 } 946 947 static int 948 mtioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p) 949 { 950 struct mtop *op; 951 int cnt; 952 953 switch (cmd) { 954 case MTIOCTOP: 955 op = (struct mtop *)data; 956 switch(op->mt_op) { 957 case MTWEOF: 958 case MTFSF: 959 case MTBSR: 960 case MTBSF: 961 case MTFSR: 962 cnt = op->mt_count; 963 break; 964 965 case MTOFFL: 966 case MTREW: 967 case MTNOP: 968 cnt = 0; 969 break; 970 971 default: 972 return (EINVAL); 973 } 974 return (mtcommand(dev, op->mt_op, cnt)); 975 976 case MTIOCGET: 977 break; 978 979 default: 980 return (EINVAL); 981 } 982 return (0); 983 } 984