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