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