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