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