1 /* $NetBSD: mt.c,v 1.40 2007/10/17 19:54:23 garbled 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.40 2007/10/17 19:54:23 garbled 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/bufq.h> 77 #include <sys/ioctl.h> 78 #include <sys/mtio.h> 79 #include <sys/file.h> 80 #include <sys/proc.h> 81 #include <sys/errno.h> 82 #include <sys/syslog.h> 83 #include <sys/tty.h> 84 #include <sys/kernel.h> 85 #include <sys/tprintf.h> 86 #include <sys/device.h> 87 #include <sys/conf.h> 88 89 #include <hp300/dev/hpibvar.h> 90 91 #include <hp300/dev/mtreg.h> 92 93 #include "ioconf.h" 94 95 static const struct mtinfo { 96 u_short hwid; 97 const char *desc; 98 } mtinfo[] = { 99 { MT7978ID, "7978" }, 100 { MT7979AID, "7979A" }, 101 { MT7980ID, "7980" }, 102 { MT7974AID, "7974A" }, 103 }; 104 static const int nmtinfo = sizeof(mtinfo) / sizeof(mtinfo[0]); 105 106 struct mt_softc { 107 struct device sc_dev; 108 struct callout sc_start_ch; 109 struct callout sc_intr_ch; 110 int sc_hpibno; /* logical HPIB this slave it attached to */ 111 int sc_slave; /* HPIB slave address (0-6) */ 112 short sc_flags; /* see below */ 113 u_char sc_lastdsj; /* place for DSJ in mtreaddsj() */ 114 u_char sc_lastecmd; /* place for End Command in mtreaddsj() */ 115 short sc_recvtimeo; /* count of hpibsend timeouts to prevent hang */ 116 short sc_statindex; /* index for next sc_stat when MTF_STATTIMEO */ 117 struct mt_stat sc_stat;/* status bytes last read from device */ 118 short sc_density; /* current density of tape (mtio.h format) */ 119 short sc_type; /* tape drive model (hardware IDs) */ 120 struct hpibqueue sc_hq; /* HPIB device queue member */ 121 tpr_t sc_ttyp; 122 struct bufq_state *sc_tab;/* buf queue */ 123 int sc_active; 124 struct buf sc_bufstore; /* XXX buffer storage */ 125 }; 126 127 #ifdef DEBUG 128 int mtdebug = 0; 129 #define dlog if (mtdebug) log 130 #else 131 #define dlog if (0) log 132 #endif 133 134 #define UNIT(x) (minor(x) & 3) 135 136 #define B_CMD B_DEVPRIVATE /* command buf instead of data */ 137 #define b_cmd b_blkno /* blkno holds cmd when B_CMD */ 138 139 static int mtmatch(struct device *, struct cfdata *, void *); 140 static void mtattach(struct device *, struct device *, void *); 141 142 CFATTACH_DECL(mt, sizeof(struct mt_softc), 143 mtmatch, mtattach, NULL, NULL); 144 145 static dev_type_open(mtopen); 146 static dev_type_close(mtclose); 147 static dev_type_read(mtread); 148 static dev_type_write(mtwrite); 149 static dev_type_ioctl(mtioctl); 150 static dev_type_strategy(mtstrategy); 151 152 const struct bdevsw mt_bdevsw = { 153 mtopen, mtclose, mtstrategy, mtioctl, nodump, nosize, D_TAPE 154 }; 155 156 const struct cdevsw mt_cdevsw = { 157 mtopen, mtclose, mtread, mtwrite, mtioctl, 158 nostop, notty, nopoll, nommap, nokqfilter, D_TAPE 159 }; 160 161 static int mtident(struct mt_softc *, struct hpibbus_attach_args *); 162 static void mtustart(struct mt_softc *); 163 static int mtreaddsj(struct mt_softc *, int); 164 static int mtcommand(dev_t, int, int); 165 static void spl_mtintr(void *); 166 static void spl_mtstart(void *); 167 168 static void mtstart(void *); 169 static void mtgo(void *); 170 static void mtintr(void *); 171 172 static int 173 mtmatch(struct device *parent, struct cfdata *match, void *aux) 174 { 175 struct hpibbus_attach_args *ha = aux; 176 177 return mtident(NULL, ha); 178 } 179 180 static void 181 mtattach(struct device *parent, struct device *self, void *aux) 182 { 183 struct mt_softc *sc = (struct mt_softc *)self; 184 struct hpibbus_attach_args *ha = aux; 185 int unit, hpibno, slave; 186 187 if (mtident(sc, ha) == 0) { 188 printf("\n%s: impossible!\n", sc->sc_dev.dv_xname); 189 return; 190 } 191 192 unit = device_unit(self); 193 hpibno = device_unit(parent); 194 slave = ha->ha_slave; 195 196 bufq_alloc(&sc->sc_tab, "fcfs", 0); 197 callout_init(&sc->sc_start_ch, 0); 198 callout_init(&sc->sc_intr_ch, 0); 199 200 sc->sc_hpibno = hpibno; 201 sc->sc_slave = slave; 202 sc->sc_flags = MTF_EXISTS; 203 204 /* Initialize hpib job queue entry. */ 205 sc->sc_hq.hq_softc = sc; 206 sc->sc_hq.hq_slave = sc->sc_slave; 207 sc->sc_hq.hq_start = mtstart; 208 sc->sc_hq.hq_go = mtgo; 209 sc->sc_hq.hq_intr = mtintr; 210 } 211 212 static int 213 mtident(struct mt_softc *sc, struct hpibbus_attach_args *ha) 214 { 215 int i; 216 217 for (i = 0; i < nmtinfo; i++) { 218 if (ha->ha_id == mtinfo[i].hwid) { 219 if (sc != NULL) { 220 sc->sc_type = mtinfo[i].hwid; 221 printf(": %s tape\n", mtinfo[i].desc); 222 } 223 return 1; 224 } 225 } 226 return 0; 227 } 228 229 /* 230 * Perform a read of "Device Status Jump" register and update the 231 * status if necessary. If status is read, the given "ecmd" is also 232 * performed, unless "ecmd" is zero. Returns DSJ value, -1 on failure 233 * and -2 on "temporary" failure. 234 */ 235 static int 236 mtreaddsj(struct mt_softc *sc, int ecmd) 237 { 238 int retval; 239 240 if (sc->sc_flags & MTF_STATTIMEO) 241 goto getstats; 242 retval = hpibrecv(sc->sc_hpibno, 243 (sc->sc_flags & MTF_DSJTIMEO) ? -1 : sc->sc_slave, 244 MTT_DSJ, &(sc->sc_lastdsj), 1); 245 sc->sc_flags &= ~MTF_DSJTIMEO; 246 if (retval != 1) { 247 dlog(LOG_DEBUG, "%s can't hpibrecv DSJ", 248 sc->sc_dev.dv_xname); 249 if (sc->sc_recvtimeo == 0) 250 sc->sc_recvtimeo = hz; 251 if (--sc->sc_recvtimeo == 0) 252 return -1; 253 if (retval == 0) 254 sc->sc_flags |= MTF_DSJTIMEO; 255 return -2; 256 } 257 sc->sc_recvtimeo = 0; 258 sc->sc_statindex = 0; 259 dlog(LOG_DEBUG, "%s readdsj: 0x%x", sc->sc_dev.dv_xname, 260 sc->sc_lastdsj); 261 sc->sc_lastecmd = ecmd; 262 switch (sc->sc_lastdsj) { 263 case 0: 264 if (ecmd & MTE_DSJ_FORCE) 265 break; 266 return 0; 267 268 case 2: 269 sc->sc_lastecmd = MTE_COMPLETE; 270 case 1: 271 break; 272 273 default: 274 log(LOG_ERR, "%s readdsj: DSJ 0x%x\n", sc->sc_dev.dv_xname, 275 sc->sc_lastdsj); 276 return -1; 277 } 278 getstats: 279 retval = hpibrecv(sc->sc_hpibno, 280 (sc->sc_flags & MTF_STATCONT) ? -1 : sc->sc_slave, 281 MTT_STAT, ((char *)&(sc->sc_stat)) + sc->sc_statindex, 282 sizeof(sc->sc_stat) - sc->sc_statindex); 283 sc->sc_flags &= ~(MTF_STATTIMEO | MTF_STATCONT); 284 if (retval != sizeof(sc->sc_stat) - sc->sc_statindex) { 285 if (sc->sc_recvtimeo == 0) 286 sc->sc_recvtimeo = hz; 287 if (--sc->sc_recvtimeo != 0) { 288 if (retval >= 0) { 289 sc->sc_statindex += retval; 290 sc->sc_flags |= MTF_STATCONT; 291 } 292 sc->sc_flags |= MTF_STATTIMEO; 293 return -2; 294 } 295 log(LOG_ERR, "%s readdsj: can't read status", 296 sc->sc_dev.dv_xname); 297 return -1; 298 } 299 sc->sc_recvtimeo = 0; 300 sc->sc_statindex = 0; 301 dlog(LOG_DEBUG, "%s readdsj: status is %x %x %x %x %x %x", 302 sc->sc_dev.dv_xname, 303 sc->sc_stat1, sc->sc_stat2, sc->sc_stat3, 304 sc->sc_stat4, sc->sc_stat5, sc->sc_stat6); 305 if (sc->sc_lastecmd) 306 (void) hpibsend(sc->sc_hpibno, sc->sc_slave, 307 MTL_ECMD, &(sc->sc_lastecmd), 1); 308 return (int)sc->sc_lastdsj; 309 } 310 311 static int 312 mtopen(dev_t dev, int flag, int mode, struct lwp *l) 313 { 314 int unit = UNIT(dev); 315 struct mt_softc *sc; 316 int req_den; 317 int error; 318 319 if (unit >= mt_cd.cd_ndevs || 320 (sc = mt_cd.cd_devs[unit]) == NULL || 321 (sc->sc_flags & MTF_EXISTS) == 0) 322 return ENXIO; 323 324 dlog(LOG_DEBUG, "%s open: flags 0x%x", sc->sc_dev.dv_xname, 325 sc->sc_flags); 326 if (sc->sc_flags & MTF_OPEN) 327 return EBUSY; 328 sc->sc_flags |= MTF_OPEN; 329 sc->sc_ttyp = tprintf_open(l->l_proc); 330 if ((sc->sc_flags & MTF_ALIVE) == 0) { 331 error = mtcommand(dev, MTRESET, 0); 332 if (error != 0 || (sc->sc_flags & MTF_ALIVE) == 0) 333 goto errout; 334 if ((sc->sc_stat1 & (SR1_BOT | SR1_ONLINE)) == SR1_ONLINE) 335 (void) mtcommand(dev, MTREW, 0); 336 } 337 for (;;) { 338 if ((error = mtcommand(dev, MTNOP, 0)) != 0) 339 goto errout; 340 if (!(sc->sc_flags & MTF_REW)) 341 break; 342 if (tsleep((void *) &lbolt, PCATCH | (PZERO + 1), 343 "mt", 0) != 0) { 344 error = EINTR; 345 goto errout; 346 } 347 } 348 if ((flag & FWRITE) && (sc->sc_stat1 & SR1_RO)) { 349 error = EROFS; 350 goto errout; 351 } 352 if (!(sc->sc_stat1 & SR1_ONLINE)) { 353 uprintf("%s: not online\n", sc->sc_dev.dv_xname); 354 error = EIO; 355 goto errout; 356 } 357 /* 358 * Select density: 359 * - find out what density the drive is set to 360 * (i.e. the density of the current tape) 361 * - if we are going to write 362 * - if we're not at the beginning of the tape 363 * - complain if we want to change densities 364 * - otherwise, select the mtcommand to set the density 365 * 366 * If the drive doesn't support it then don't change the recorded 367 * density. 368 * 369 * The original MOREbsd code had these additional conditions 370 * for the mid-tape change 371 * 372 * req_den != T_BADBPI && 373 * sc->sc_density != T_6250BPI 374 * 375 * which suggests that it would be possible to write multiple 376 * densities if req_den == T_BAD_BPI or the current tape 377 * density was 6250. Testing of our 7980 suggests that the 378 * device cannot change densities mid-tape. 379 * 380 * ajv@comp.vuw.ac.nz 381 */ 382 sc->sc_density = (sc->sc_stat2 & SR2_6250) ? T_6250BPI : ( 383 (sc->sc_stat3 & SR3_1600) ? T_1600BPI : ( 384 (sc->sc_stat3 & SR3_800) ? T_800BPI : -1)); 385 req_den = (dev & T_DENSEL); 386 387 if (flag & FWRITE) { 388 if (!(sc->sc_stat1 & SR1_BOT)) { 389 if (sc->sc_density != req_den) { 390 uprintf("%s: can't change density mid-tape\n", 391 sc->sc_dev.dv_xname); 392 error = EIO; 393 goto errout; 394 } 395 } 396 else { 397 int mtset_density = 398 (req_den == T_800BPI ? MTSET800BPI : ( 399 req_den == T_1600BPI ? MTSET1600BPI : ( 400 req_den == T_6250BPI ? MTSET6250BPI : ( 401 sc->sc_type == MT7980ID 402 ? MTSET6250DC 403 : MTSET6250BPI)))); 404 if (mtcommand(dev, mtset_density, 0) == 0) 405 sc->sc_density = req_den; 406 } 407 } 408 return 0; 409 errout: 410 sc->sc_flags &= ~MTF_OPEN; 411 return error; 412 } 413 414 static int 415 mtclose(dev_t dev, int flag, int fmt, struct lwp *l) 416 { 417 struct mt_softc *sc = mt_cd.cd_devs[UNIT(dev)]; 418 419 if (sc->sc_flags & MTF_WRT) { 420 (void) mtcommand(dev, MTWEOF, 2); 421 (void) mtcommand(dev, MTBSF, 0); 422 } 423 if ((minor(dev) & T_NOREWIND) == 0) 424 (void) mtcommand(dev, MTREW, 0); 425 sc->sc_flags &= ~MTF_OPEN; 426 tprintf_close(sc->sc_ttyp); 427 return 0; 428 } 429 430 static int 431 mtcommand(dev_t dev, int cmd, int cnt) 432 { 433 struct mt_softc *sc = mt_cd.cd_devs[UNIT(dev)]; 434 struct buf *bp = &sc->sc_bufstore; 435 int error = 0; 436 437 #if 1 438 if (bp->b_flags & B_BUSY) 439 return EBUSY; 440 #endif 441 bp->b_cmd = cmd; 442 bp->b_dev = dev; 443 do { 444 bp->b_flags = B_BUSY | B_CMD; 445 mtstrategy(bp); 446 biowait(bp); 447 if (bp->b_error != 0) { 448 error = bp->b_error; 449 break; 450 } 451 } while (--cnt > 0); 452 #if 0 453 bp->b_flags = 0 /*&= ~B_BUSY*/; 454 #else 455 bp->b_flags &= ~B_BUSY; 456 #endif 457 return error; 458 } 459 460 /* 461 * Only thing to check here is for legal record lengths (writes only). 462 */ 463 static void 464 mtstrategy(struct buf *bp) 465 { 466 struct mt_softc *sc; 467 int unit; 468 int s; 469 470 unit = UNIT(bp->b_dev); 471 sc = mt_cd.cd_devs[unit]; 472 dlog(LOG_DEBUG, "%s strategy", sc->sc_dev.dv_xname); 473 if ((bp->b_flags & (B_CMD | B_READ)) == 0) { 474 #define WRITE_BITS_IGNORED 8 475 #if 0 476 if (bp->b_bcount & ((1 << WRITE_BITS_IGNORED) - 1)) { 477 tprintf(sc->sc_ttyp, 478 "%s: write record must be multiple of %d\n", 479 sc->sc_dev.dv_xname, 1 << WRITE_BITS_IGNORED); 480 goto error; 481 } 482 #endif 483 s = 16 * 1024; 484 if (sc->sc_stat2 & SR2_LONGREC) { 485 switch (sc->sc_density) { 486 case T_1600BPI: 487 s = 32 * 1024; 488 break; 489 490 case T_6250BPI: 491 case T_BADBPI: 492 s = 60 * 1024; 493 break; 494 } 495 } 496 if (bp->b_bcount > s) { 497 tprintf(sc->sc_ttyp, 498 "%s: write record (%d) too big: limit (%d)\n", 499 sc->sc_dev.dv_xname, bp->b_bcount, s); 500 #if 0 /* XXX see above */ 501 error: 502 #endif 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(device_parent(&sc->sc_dev), &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 done; 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 done; 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 done; 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 done: 734 sc->sc_flags &= ~(MTF_HITEOF | MTF_HITBOF); 735 (void)BUFQ_GET(sc->sc_tab); 736 biodone(bp); 737 hpibfree(device_parent(&sc->sc_dev), &sc->sc_hq); 738 if ((bp = BUFQ_PEEK(sc->sc_tab)) == NULL) 739 sc->sc_active = 0; 740 else 741 mtustart(sc); 742 } 743 744 /* 745 * The Utah code had a bug which meant that the driver was unable to read. 746 * "rw" was initialized to bp->b_flags & B_READ before "bp" was initialized. 747 * -- ajv@comp.vuw.ac.nz 748 */ 749 static void 750 mtgo(void *arg) 751 { 752 struct mt_softc *sc = arg; 753 struct buf *bp; 754 int rw; 755 756 dlog(LOG_DEBUG, "%s go", sc->sc_dev.dv_xname); 757 bp = BUFQ_PEEK(sc->sc_tab); 758 rw = bp->b_flags & B_READ; 759 hpibgo(sc->sc_hpibno, sc->sc_slave, rw ? MTT_READ : MTL_WRITE, 760 bp->b_data, bp->b_bcount, rw, rw != 0); 761 } 762 763 static void 764 mtintr(void *arg) 765 { 766 struct mt_softc *sc = arg; 767 struct buf *bp; 768 int i; 769 u_char cmdbuf[4]; 770 771 bp = BUFQ_PEEK(sc->sc_tab); 772 if (bp == NULL) { 773 log(LOG_ERR, "%s intr: bp == NULL", sc->sc_dev.dv_xname); 774 return; 775 } 776 777 dlog(LOG_DEBUG, "%s intr", sc->sc_dev.dv_xname); 778 779 /* 780 * Some operation completed. Read status bytes and report errors. 781 * Clear EOF flags here `cause they're set once on specific conditions 782 * below when a command succeeds. 783 * A DSJ of 2 always means keep waiting. If the command was READ 784 * (and we're in data DMA phase) stop data transfer first. 785 */ 786 sc->sc_flags &= ~(MTF_HITEOF | MTF_HITBOF); 787 if ((bp->b_flags & (B_CMD|B_READ)) == B_READ && 788 !(sc->sc_flags & (MTF_IO | MTF_STATTIMEO | MTF_DSJTIMEO))){ 789 cmdbuf[0] = MTE_STOP; 790 (void) hpibsend(sc->sc_hpibno, sc->sc_slave, MTL_ECMD,cmdbuf,1); 791 } 792 switch (mtreaddsj(sc, 0)) { 793 case 0: 794 break; 795 796 case 1: 797 /* 798 * If we're in the middle of a READ/WRITE and have yet to 799 * start the data transfer, a DSJ of one should terminate it. 800 */ 801 sc->sc_flags &= ~MTF_IO; 802 break; 803 804 case 2: 805 (void) hpibawait(sc->sc_hpibno); 806 return; 807 808 case -2: 809 /* 810 * -2 means that the drive failed to respond quickly enough 811 * to the request for DSJ. It's probably just "busy" figuring 812 * it out and will know in a little bit... 813 */ 814 callout_reset(&sc->sc_intr_ch, hz >> 5, spl_mtintr, sc); 815 return; 816 817 default: 818 log(LOG_ERR, "%s intr: can't get drive stat", 819 sc->sc_dev.dv_xname); 820 goto error; 821 } 822 if (sc->sc_stat1 & (SR1_ERR | SR1_REJECT)) { 823 i = sc->sc_stat4 & SR4_ERCLMASK; 824 log(LOG_ERR, "%s: %s error, retry %d, SR2/3 %x/%x, code %d", 825 sc->sc_dev.dv_xname, i == SR4_DEVICE ? "device" : 826 (i == SR4_PROTOCOL ? "protocol" : 827 (i == SR4_SELFTEST ? "selftest" : "unknown")), 828 sc->sc_stat4 & SR4_RETRYMASK, sc->sc_stat2, 829 sc->sc_stat3, sc->sc_stat5); 830 831 if ((bp->b_flags & B_CMD) && bp->b_cmd == MTRESET) 832 callout_stop(&sc->sc_intr_ch); 833 if (sc->sc_stat3 & SR3_POWERUP) 834 sc->sc_flags &= MTF_OPEN | MTF_EXISTS; 835 goto error; 836 } 837 /* 838 * Report and clear any soft errors. 839 */ 840 if (sc->sc_stat1 & SR1_SOFTERR) { 841 log(LOG_WARNING, "%s: soft error, retry %d\n", 842 sc->sc_dev.dv_xname, sc->sc_stat4 & SR4_RETRYMASK); 843 sc->sc_stat1 &= ~SR1_SOFTERR; 844 } 845 /* 846 * We've initiated a read or write, but haven't actually started to 847 * DMA the data yet. At this point, the drive's ready. 848 */ 849 if (sc->sc_flags & MTF_IO) { 850 sc->sc_flags &= ~MTF_IO; 851 if (hpibustart(sc->sc_hpibno)) 852 mtgo(sc); 853 return; 854 } 855 /* 856 * Check for End Of Tape - we're allowed to hit EOT and then write (or 857 * read) one more record. If we get here and have not already hit EOT, 858 * return ENOSPC to inform the process that it's hit it. If we get 859 * here and HAVE already hit EOT, don't allow any more operations that 860 * move the tape forward. 861 */ 862 if (sc->sc_stat1 & SR1_EOT) { 863 if (sc->sc_flags & MTF_ATEOT) 864 sc->sc_flags |= MTF_PASTEOT; 865 else { 866 bp->b_error = ENOSPC; 867 sc->sc_flags |= MTF_ATEOT; 868 } 869 } 870 /* 871 * If a motion command was being executed, check for Tape Marks. 872 * If we were doing data, make sure we got the right amount, and 873 * check for hitting tape marks on reads. 874 */ 875 if (bp->b_flags & B_CMD) { 876 if (sc->sc_stat1 & SR1_EOF) { 877 if (bp->b_cmd == MTFSR) 878 sc->sc_flags |= MTF_HITEOF; 879 if (bp->b_cmd == MTBSR) 880 sc->sc_flags |= MTF_HITBOF; 881 } 882 if (bp->b_cmd == MTRESET) { 883 callout_stop(&sc->sc_intr_ch); 884 sc->sc_flags |= MTF_ALIVE; 885 } 886 } else { 887 i = hpibrecv(sc->sc_hpibno, sc->sc_slave, MTT_BCNT, cmdbuf, 2); 888 if (i != 2) { 889 log(LOG_ERR, "%s intr: can't get xfer length\n", 890 sc->sc_dev.dv_xname); 891 goto error; 892 } 893 i = (int) *((u_short *) cmdbuf); 894 if (i <= bp->b_bcount) { 895 if (i == 0) 896 sc->sc_flags |= MTF_HITEOF; 897 bp->b_resid = bp->b_bcount - i; 898 dlog(LOG_DEBUG, "%s intr: bcount %d, resid %d", 899 sc->sc_dev.dv_xname, bp->b_bcount, bp->b_resid); 900 } else { 901 tprintf(sc->sc_ttyp, 902 "%s: record (%d) larger than wanted (%d)\n", 903 sc->sc_dev.dv_xname, i, bp->b_bcount); 904 error: 905 sc->sc_flags &= ~MTF_IO; 906 bp->b_error = EIO; 907 } 908 } 909 /* 910 * The operation is completely done. 911 * Let the drive know with an END command. 912 */ 913 cmdbuf[0] = MTE_COMPLETE | MTE_IDLE; 914 (void) hpibsend(sc->sc_hpibno, sc->sc_slave, MTL_ECMD, cmdbuf, 1); 915 bp->b_flags &= ~B_CMD; 916 (void)BUFQ_GET(sc->sc_tab); 917 biodone(bp); 918 hpibfree(device_parent(&sc->sc_dev), &sc->sc_hq); 919 if (BUFQ_PEEK(sc->sc_tab) == NULL) 920 sc->sc_active = 0; 921 else 922 mtustart(sc); 923 } 924 925 static int 926 mtread(dev_t dev, struct uio *uio, int flags) 927 { 928 struct mt_softc *sc = mt_cd.cd_devs[UNIT(dev)]; 929 930 return physio(mtstrategy, &sc->sc_bufstore, 931 dev, B_READ, minphys, uio); 932 } 933 934 static int 935 mtwrite(dev_t dev, struct uio *uio, int flags) 936 { 937 struct mt_softc *sc = mt_cd.cd_devs[UNIT(dev)]; 938 939 return physio(mtstrategy, &sc->sc_bufstore, 940 dev, B_WRITE, minphys, uio); 941 } 942 943 static int 944 mtioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) 945 { 946 struct mtop *op; 947 int cnt; 948 949 switch (cmd) { 950 case MTIOCTOP: 951 op = (struct mtop *)data; 952 switch(op->mt_op) { 953 case MTWEOF: 954 case MTFSF: 955 case MTBSR: 956 case MTBSF: 957 case MTFSR: 958 cnt = op->mt_count; 959 break; 960 961 case MTOFFL: 962 case MTREW: 963 case MTNOP: 964 cnt = 0; 965 break; 966 967 default: 968 return EINVAL; 969 } 970 return mtcommand(dev, op->mt_op, cnt); 971 972 case MTIOCGET: 973 break; 974 975 default: 976 return EINVAL; 977 } 978 return 0; 979 } 980