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