1 /* $NetBSD: ct.c,v 1.53 2008/03/29 06:47:07 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 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 /* 40 * Copyright (c) 1982, 1990, 1993 41 * The Regents of the University of California. All rights reserved. 42 * 43 * Redistribution and use in source and binary forms, with or without 44 * modification, are permitted provided that the following conditions 45 * are met: 46 * 1. Redistributions of source code must retain the above copyright 47 * notice, this list of conditions and the following disclaimer. 48 * 2. Redistributions in binary form must reproduce the above copyright 49 * notice, this list of conditions and the following disclaimer in the 50 * documentation and/or other materials provided with the distribution. 51 * 3. Neither the name of the University nor the names of its contributors 52 * may be used to endorse or promote products derived from this software 53 * without specific prior written permission. 54 * 55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 65 * SUCH DAMAGE. 66 * 67 * @(#)ct.c 8.2 (Berkeley) 1/12/94 68 */ 69 70 /* 71 * CS80 cartridge tape driver (9144, 88140, 9145) 72 * 73 * Reminder: 74 * C_CC bit (character count option) when used in the CS/80 command 75 * 'set options' will cause the tape not to stream. 76 * 77 * TODO: 78 * make filesystem compatible 79 * make block mode work according to mtio(4) spec. (if possible) 80 * merge with cs80 disk driver 81 * finish support of 9145 82 */ 83 84 #include <sys/cdefs.h> 85 __KERNEL_RCSID(0, "$NetBSD: ct.c,v 1.53 2008/03/29 06:47:07 tsutsui Exp $"); 86 87 #include <sys/param.h> 88 #include <sys/systm.h> 89 #include <sys/buf.h> 90 #include <sys/bufq.h> 91 #include <sys/conf.h> 92 #include <sys/device.h> 93 #include <sys/ioctl.h> 94 #include <sys/mtio.h> 95 #include <sys/proc.h> 96 #include <sys/tprintf.h> 97 98 #include <hp300/dev/hpibvar.h> 99 100 #include <hp300/dev/ctreg.h> 101 102 #include "ioconf.h" 103 104 /* number of eof marks to remember */ 105 #define EOFS 128 106 107 struct ct_softc { 108 device_t sc_dev; 109 int sc_slave; /* HP-IB slave ID */ 110 int sc_punit; /* physical unit */ 111 struct ct_iocmd sc_ioc; 112 struct ct_rscmd sc_rsc; 113 struct ct_stat sc_stat; 114 struct ct_ssmcmd sc_ssmc; 115 struct ct_srcmd sc_src; 116 struct ct_soptcmd sc_soptc; 117 struct ct_ulcmd sc_ul; 118 struct ct_wfmcmd sc_wfm; 119 struct ct_clearcmd sc_clear; 120 struct bufq_state *sc_tab; 121 int sc_active; 122 struct buf *sc_bp; 123 struct buf sc_bufstore; /* XXX */ 124 int sc_blkno; 125 int sc_cmd; 126 int sc_resid; 127 char *sc_addr; 128 int sc_flags; 129 short sc_type; 130 tpr_t sc_tpr; 131 struct hpibqueue sc_hq; /* entry on hpib job queue */ 132 int sc_eofp; 133 int sc_eofs[EOFS]; 134 }; 135 136 /* flags */ 137 #define CTF_OPEN 0x01 138 #define CTF_ALIVE 0x02 139 #define CTF_WRT 0x04 140 #define CTF_CMD 0x08 141 #define CTF_IO 0x10 142 #define CTF_BEOF 0x20 143 #define CTF_AEOF 0x40 144 #define CTF_EOT 0x80 145 #define CTF_STATWAIT 0x100 146 #define CTF_CANSTREAM 0x200 147 #define CTF_WRTTN 0x400 148 149 static int ctmatch(device_t, cfdata_t, void *); 150 static void ctattach(device_t, device_t, void *); 151 152 CFATTACH_DECL_NEW(ct, sizeof(struct ct_softc), 153 ctmatch, ctattach, NULL, NULL); 154 155 static dev_type_open(ctopen); 156 static dev_type_close(ctclose); 157 static dev_type_read(ctread); 158 static dev_type_write(ctwrite); 159 static dev_type_ioctl(ctioctl); 160 static dev_type_strategy(ctstrategy); 161 162 const struct bdevsw ct_bdevsw = { 163 ctopen, ctclose, ctstrategy, ctioctl, nodump, nosize, D_TAPE 164 }; 165 166 const struct cdevsw ct_cdevsw = { 167 ctopen, ctclose, ctread, ctwrite, ctioctl, 168 nostop, notty, nopoll, nommap, nokqfilter, D_TAPE 169 }; 170 171 static int ctident(device_t, struct ct_softc *, 172 struct hpibbus_attach_args *); 173 174 static void ctreset(struct ct_softc *); 175 static void ctaddeof(struct ct_softc *); 176 static void ctustart(struct ct_softc *); 177 static void cteof(struct ct_softc *, struct buf *); 178 static void ctdone(struct ct_softc *, struct buf *); 179 180 static void ctstart(void *); 181 static void ctgo(void *); 182 static void ctintr(void *); 183 184 static void ctcommand(dev_t, int, int); 185 186 static const struct ctinfo { 187 short hwid; 188 short punit; 189 const char *desc; 190 } ctinfo[] = { 191 { CT7946ID, 1, "7946A" }, 192 { CT7912PID, 1, "7912P" }, 193 { CT7914PID, 1, "7914P" }, 194 { CT9144ID, 0, "9144" }, 195 { CT9145ID, 0, "9145" }, 196 { CT35401ID, 0, "35401A"}, 197 }; 198 static const int nctinfo = __arraycount(ctinfo); 199 200 #define CT_NOREW 4 201 #define CT_STREAM 8 202 #define UNIT(x) (minor(x) & 3) 203 #define ctpunit(x) ((x) & 7) 204 205 #ifdef DEBUG 206 int ctdebug = 0; 207 #define CDB_FILES 0x01 208 #define CT_BSF 0x02 209 #endif 210 211 static int 212 ctmatch(device_t parent, cfdata_t cf, void *aux) 213 { 214 struct hpibbus_attach_args *ha = aux; 215 216 return ctident(parent, NULL, ha); 217 } 218 219 static void 220 ctattach(device_t parent, device_t self, void *aux) 221 { 222 struct ct_softc *sc = device_private(self); 223 struct hpibbus_attach_args *ha = aux; 224 225 sc->sc_dev = self; 226 if (ctident(parent, sc, ha) == 0) { 227 aprint_error(": didn't respond to describe command!\n"); 228 return; 229 } 230 231 sc->sc_slave = ha->ha_slave; 232 sc->sc_punit = ha->ha_punit; 233 234 bufq_alloc(&sc->sc_tab, "fcfs", 0); 235 236 /* Initialize hpib job queue entry. */ 237 sc->sc_hq.hq_softc = sc; 238 sc->sc_hq.hq_slave = sc->sc_slave; 239 sc->sc_hq.hq_start = ctstart; 240 sc->sc_hq.hq_go = ctgo; 241 sc->sc_hq.hq_intr = ctintr; 242 243 ctreset(sc); 244 sc->sc_flags |= CTF_ALIVE; 245 } 246 247 static int 248 ctident(device_t parent, struct ct_softc *sc, struct hpibbus_attach_args *ha) 249 { 250 struct ct_describe desc; 251 u_char stat, cmd[3]; 252 char name[7]; 253 int i, id, n, type, canstream; 254 255 type = canstream = 0; 256 257 /* Verify that we have a CS80 device. */ 258 if ((ha->ha_id & 0x200) == 0) 259 return 0; 260 261 /* Is it one of the tapes we support? */ 262 for (id = 0; id < nctinfo; id++) 263 if (ha->ha_id == ctinfo[id].hwid) 264 break; 265 if (id == nctinfo) 266 return 0; 267 268 ha->ha_punit = ctinfo[id].punit; 269 270 /* 271 * So far, so good. Get drive parameters. Note command 272 * is always issued to unit 0. 273 */ 274 cmd[0] = C_SUNIT(0); 275 cmd[1] = C_SVOL(0); 276 cmd[2] = C_DESC; 277 hpibsend(device_unit(parent), ha->ha_slave, C_CMD, cmd, sizeof(cmd)); 278 hpibrecv(device_unit(parent), ha->ha_slave, C_EXEC, &desc, 37); 279 hpibrecv(device_unit(parent), ha->ha_slave, C_QSTAT, &stat, 280 sizeof(stat)); 281 282 memset(name, 0, sizeof(name)); 283 if (stat == 0) { 284 n = desc.d_name; 285 for (i = 5; i >= 0; i--) { 286 name[i] = (n & 0xf) + '0'; 287 n >>= 4; 288 } 289 } 290 291 switch (ha->ha_id) { 292 case CT7946ID: 293 if (memcmp(name, "079450", 6) == 0) 294 return 0; /* not really a 7946 */ 295 /* fall into... */ 296 case CT9144ID: 297 case CT9145ID: 298 case CT35401ID: 299 type = CT9144; 300 canstream = 1; 301 break; 302 303 case CT7912PID: 304 case CT7914PID: 305 type = CT88140; 306 break; 307 } 308 309 if (sc != NULL) { 310 sc->sc_type = type; 311 sc->sc_flags = canstream ? CTF_CANSTREAM : 0; 312 aprint_normal(": %s %stape\n", ctinfo[id].desc, 313 canstream ? "streaming " : ""); 314 } 315 316 return 1; 317 } 318 319 static void 320 ctreset(struct ct_softc *sc) 321 { 322 int ctlr, slave; 323 uint8_t stat; 324 325 ctlr = device_unit(device_parent(sc->sc_dev)); 326 slave = sc->sc_slave; 327 328 sc->sc_clear.unit = C_SUNIT(sc->sc_punit); 329 sc->sc_clear.cmd = C_CLEAR; 330 hpibsend(ctlr, slave, C_TCMD, &sc->sc_clear, sizeof(sc->sc_clear)); 331 hpibswait(ctlr, slave); 332 hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat)); 333 334 sc->sc_src.unit = C_SUNIT(CTCTLR); 335 sc->sc_src.nop = C_NOP; 336 sc->sc_src.cmd = C_SREL; 337 sc->sc_src.param = C_REL; 338 hpibsend(ctlr, slave, C_CMD, &sc->sc_src, sizeof(sc->sc_src)); 339 hpibswait(ctlr, slave); 340 hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat)); 341 342 sc->sc_ssmc.unit = C_SUNIT(sc->sc_punit); 343 sc->sc_ssmc.cmd = C_SSM; 344 sc->sc_ssmc.refm = REF_MASK; 345 sc->sc_ssmc.fefm = FEF_MASK; 346 sc->sc_ssmc.aefm = AEF_MASK; 347 sc->sc_ssmc.iefm = IEF_MASK; 348 hpibsend(ctlr, slave, C_CMD, &sc->sc_ssmc, sizeof(sc->sc_ssmc)); 349 hpibswait(ctlr, slave); 350 hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat)); 351 352 sc->sc_soptc.unit = C_SUNIT(sc->sc_punit); 353 sc->sc_soptc.nop = C_NOP; 354 sc->sc_soptc.cmd = C_SOPT; 355 sc->sc_soptc.opt = C_SPAR; 356 hpibsend(ctlr, slave, C_CMD, &sc->sc_soptc, sizeof(sc->sc_soptc)); 357 hpibswait(ctlr, slave); 358 hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat)); 359 } 360 361 /*ARGSUSED*/ 362 static int 363 ctopen(dev_t dev, int flag, int type, struct lwp *l) 364 { 365 struct ct_softc *sc; 366 uint8_t stat; 367 int cc, ctlr, slave; 368 369 if (UNIT(dev) >= ct_cd.cd_ndevs || 370 (sc = device_private(ct_cd.cd_devs[UNIT(dev)])) == NULL || 371 (sc->sc_flags & CTF_ALIVE) == 0) 372 return ENXIO; 373 374 if (sc->sc_flags & CTF_OPEN) 375 return EBUSY; 376 377 ctlr = device_unit(device_parent(sc->sc_dev)); 378 slave = sc->sc_slave; 379 380 sc->sc_soptc.unit = C_SUNIT(sc->sc_punit); 381 sc->sc_soptc.nop = C_NOP; 382 sc->sc_soptc.cmd = C_SOPT; 383 if ((dev & CT_STREAM) && (sc->sc_flags & CTF_CANSTREAM)) 384 sc->sc_soptc.opt = C_SPAR | C_IMRPT; 385 else 386 sc->sc_soptc.opt = C_SPAR; 387 388 /* 389 * Check the return of hpibsend() and hpibswait(). 390 * Drive could be loading/unloading a tape. If not checked, 391 * driver hangs. 392 */ 393 cc = hpibsend(ctlr, slave, C_CMD, &sc->sc_soptc, sizeof(sc->sc_soptc)); 394 if (cc != sizeof(sc->sc_soptc)) 395 return EBUSY; 396 397 hpibswait(ctlr, slave); 398 cc = hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat)); 399 if (cc != sizeof(stat)) 400 return EBUSY; 401 402 sc->sc_tpr = tprintf_open(l->l_proc); 403 sc->sc_flags |= CTF_OPEN; 404 return 0; 405 } 406 407 /*ARGSUSED*/ 408 static int 409 ctclose(dev_t dev, int flag, int fmt, struct lwp *l) 410 { 411 struct ct_softc *sc = device_private(ct_cd.cd_devs[UNIT(dev)]); 412 413 if ((sc->sc_flags & (CTF_WRT|CTF_WRTTN)) == (CTF_WRT|CTF_WRTTN) && 414 (sc->sc_flags & CTF_EOT) == 0 ) { /* XXX return error if EOT ?? */ 415 ctcommand(dev, MTWEOF, 2); 416 ctcommand(dev, MTBSR, 1); 417 if (sc->sc_eofp == EOFS - 1) 418 sc->sc_eofs[EOFS - 1]--; 419 else 420 sc->sc_eofp--; 421 #ifdef DEBUG 422 if(ctdebug & CT_BSF) 423 printf("%s: ctclose backup eofs prt %d blk %d\n", 424 device_xname(sc->sc_dev), sc->sc_eofp, 425 sc->sc_eofs[sc->sc_eofp]); 426 #endif 427 } 428 if ((minor(dev) & CT_NOREW) == 0) 429 ctcommand(dev, MTREW, 1); 430 sc->sc_flags &= ~(CTF_OPEN | CTF_WRT | CTF_WRTTN); 431 tprintf_close(sc->sc_tpr); 432 #ifdef DEBUG 433 if (ctdebug & CDB_FILES) 434 printf("ctclose: flags %x\n", sc->sc_flags); 435 #endif 436 return 0; /* XXX */ 437 } 438 439 static void 440 ctcommand(dev_t dev, int cmd, int cnt) 441 { 442 struct ct_softc *sc = device_private(ct_cd.cd_devs[UNIT(dev)]); 443 struct buf *bp = &sc->sc_bufstore; 444 struct buf *nbp = 0; 445 446 if (cmd == MTBSF && sc->sc_eofp == EOFS - 1) { 447 cnt = sc->sc_eofs[EOFS - 1] - cnt; 448 ctcommand(dev, MTREW, 1); 449 ctcommand(dev, MTFSF, cnt); 450 cnt = 2; 451 cmd = MTBSR; 452 } 453 454 if (cmd == MTBSF && sc->sc_eofp - cnt < 0) { 455 cnt = 1; 456 cmd = MTREW; 457 } 458 459 sc->sc_flags |= CTF_CMD; 460 sc->sc_bp = bp; 461 sc->sc_cmd = cmd; 462 bp->b_dev = dev; 463 if (cmd == MTFSF) { 464 nbp = (struct buf *)geteblk(MAXBSIZE); 465 bp->b_data = nbp->b_data; 466 bp->b_bcount = MAXBSIZE; 467 } 468 469 while (cnt-- > 0) { 470 bp->b_cflags = BC_BUSY; 471 if (cmd == MTBSF) { 472 sc->sc_blkno = sc->sc_eofs[sc->sc_eofp]; 473 sc->sc_eofp--; 474 #ifdef DEBUG 475 if (ctdebug & CT_BSF) 476 printf("%s: backup eof pos %d blk %d\n", 477 device_xname(sc->sc_dev), sc->sc_eofp, 478 sc->sc_eofs[sc->sc_eofp]); 479 #endif 480 } 481 ctstrategy(bp); 482 biowait(bp); 483 } 484 bp->b_flags = 0; 485 sc->sc_flags &= ~CTF_CMD; 486 if (nbp) 487 brelse(nbp, 0); 488 } 489 490 static void 491 ctstrategy(struct buf *bp) 492 { 493 int s, unit; 494 struct ct_softc *sc; 495 496 unit = UNIT(bp->b_dev); 497 sc = device_private(ct_cd.cd_devs[unit]); 498 499 s = splbio(); 500 BUFQ_PUT(sc->sc_tab, bp); 501 if (sc->sc_active == 0) { 502 sc->sc_active = 1; 503 ctustart(sc); 504 } 505 splx(s); 506 } 507 508 static void 509 ctustart(struct ct_softc *sc) 510 { 511 struct buf *bp; 512 513 bp = BUFQ_PEEK(sc->sc_tab); 514 sc->sc_addr = bp->b_data; 515 sc->sc_resid = bp->b_bcount; 516 if (hpibreq(device_parent(sc->sc_dev), &sc->sc_hq)) 517 ctstart(sc); 518 } 519 520 static void 521 ctstart(void *arg) 522 { 523 struct ct_softc *sc = arg; 524 struct buf *bp; 525 int i, ctlr, slave; 526 527 ctlr = device_unit(device_parent(sc->sc_dev)); 528 slave = sc->sc_slave; 529 530 bp = BUFQ_PEEK(sc->sc_tab); 531 if ((sc->sc_flags & CTF_CMD) && sc->sc_bp == bp) { 532 switch(sc->sc_cmd) { 533 case MTFSF: 534 bp->b_flags |= B_READ; 535 goto mustio; 536 537 case MTBSF: 538 goto gotaddr; 539 540 case MTOFFL: 541 sc->sc_blkno = 0; 542 sc->sc_ul.unit = C_SUNIT(sc->sc_punit); 543 sc->sc_ul.cmd = C_UNLOAD; 544 hpibsend(ctlr, slave, C_CMD, &sc->sc_ul, 545 sizeof(sc->sc_ul)); 546 break; 547 548 case MTWEOF: 549 sc->sc_blkno++; 550 sc->sc_flags |= CTF_WRT; 551 sc->sc_wfm.unit = C_SUNIT(sc->sc_punit); 552 sc->sc_wfm.cmd = C_WFM; 553 hpibsend(ctlr, slave, C_CMD, &sc->sc_wfm, 554 sizeof(sc->sc_wfm)); 555 ctaddeof(sc); 556 break; 557 558 case MTBSR: 559 sc->sc_blkno--; 560 goto gotaddr; 561 562 case MTFSR: 563 sc->sc_blkno++; 564 goto gotaddr; 565 566 case MTREW: 567 sc->sc_blkno = 0; 568 #ifdef DEBUG 569 if(ctdebug & CT_BSF) 570 printf("%s: clearing eofs\n", 571 device_xname(sc->sc_dev)); 572 #endif 573 for (i=0; i<EOFS; i++) 574 sc->sc_eofs[i] = 0; 575 sc->sc_eofp = 0; 576 577 gotaddr: 578 sc->sc_ioc.saddr = C_SADDR; 579 sc->sc_ioc.addr0 = 0; 580 sc->sc_ioc.addr = sc->sc_blkno; 581 sc->sc_ioc.unit = C_SUNIT(sc->sc_punit); 582 sc->sc_ioc.nop2 = C_NOP; 583 sc->sc_ioc.slen = C_SLEN; 584 sc->sc_ioc.len = 0; 585 sc->sc_ioc.nop3 = C_NOP; 586 sc->sc_ioc.cmd = C_READ; 587 hpibsend(ctlr, slave, C_CMD, &sc->sc_ioc, 588 sizeof(sc->sc_ioc)); 589 break; 590 } 591 } else { 592 mustio: 593 if ((bp->b_flags & B_READ) && 594 sc->sc_flags & (CTF_BEOF|CTF_EOT)) { 595 #ifdef DEBUG 596 if (ctdebug & CDB_FILES) 597 printf("ctstart: before flags %x\n", 598 sc->sc_flags); 599 #endif 600 if (sc->sc_flags & CTF_BEOF) { 601 sc->sc_flags &= ~CTF_BEOF; 602 sc->sc_flags |= CTF_AEOF; 603 #ifdef DEBUG 604 if (ctdebug & CDB_FILES) 605 printf("ctstart: after flags %x\n", 606 sc->sc_flags); 607 #endif 608 } 609 bp->b_resid = bp->b_bcount; 610 ctdone(sc, bp); 611 return; 612 } 613 sc->sc_flags |= CTF_IO; 614 sc->sc_ioc.unit = C_SUNIT(sc->sc_punit); 615 sc->sc_ioc.saddr = C_SADDR; 616 sc->sc_ioc.addr0 = 0; 617 sc->sc_ioc.addr = sc->sc_blkno; 618 sc->sc_ioc.nop2 = C_NOP; 619 sc->sc_ioc.slen = C_SLEN; 620 sc->sc_ioc.len = sc->sc_resid; 621 sc->sc_ioc.nop3 = C_NOP; 622 if (bp->b_flags & B_READ) 623 sc->sc_ioc.cmd = C_READ; 624 else { 625 sc->sc_ioc.cmd = C_WRITE; 626 sc->sc_flags |= (CTF_WRT | CTF_WRTTN); 627 } 628 hpibsend(ctlr, slave, C_CMD, &sc->sc_ioc, sizeof(sc->sc_ioc)); 629 } 630 hpibawait(ctlr); 631 } 632 633 static void 634 ctgo(void *arg) 635 { 636 struct ct_softc *sc = arg; 637 struct buf *bp; 638 int rw; 639 640 bp = BUFQ_PEEK(sc->sc_tab); 641 rw = bp->b_flags & B_READ; 642 hpibgo(device_unit(device_parent(sc->sc_dev)), sc->sc_slave, C_EXEC, 643 sc->sc_addr, sc->sc_resid, rw, rw != 0); 644 } 645 646 /* 647 * Hideous grue to handle EOF/EOT (mostly for reads) 648 */ 649 static void 650 cteof(struct ct_softc *sc, struct buf *bp) 651 { 652 long blks; 653 654 /* 655 * EOT on a write is an error. 656 */ 657 if ((bp->b_flags & B_READ) == 0) { 658 bp->b_resid = bp->b_bcount; 659 bp->b_error = ENOSPC; 660 sc->sc_flags |= CTF_EOT; 661 return; 662 } 663 /* 664 * Use returned block position to determine how many blocks 665 * we really read and update b_resid. 666 */ 667 blks = sc->sc_stat.c_blk - sc->sc_blkno - 1; 668 #ifdef DEBUG 669 if (ctdebug & CDB_FILES) 670 printf("cteof: bc %d oblk %d nblk %ld read %ld, resid %ld\n", 671 bp->b_bcount, sc->sc_blkno, sc->sc_stat.c_blk, 672 blks, bp->b_bcount - CTKTOB(blks)); 673 #endif 674 if (blks == -1) { /* 9145 on EOF does not change sc_stat.c_blk */ 675 blks = 0; 676 sc->sc_blkno++; 677 } 678 else { 679 sc->sc_blkno = sc->sc_stat.c_blk; 680 } 681 bp->b_resid = bp->b_bcount - CTKTOB(blks); 682 /* 683 * If we are at physical EOV or were after an EOF, 684 * we are now at logical EOT. 685 */ 686 if ((sc->sc_stat.c_aef & AEF_EOV) || 687 (sc->sc_flags & CTF_AEOF)) { 688 sc->sc_flags |= CTF_EOT; 689 sc->sc_flags &= ~(CTF_AEOF|CTF_BEOF); 690 } 691 /* 692 * If we were before an EOF or we have just completed a FSF, 693 * we are now after EOF. 694 */ 695 else if ((sc->sc_flags & CTF_BEOF) || 696 ((sc->sc_flags & CTF_CMD) && sc->sc_cmd == MTFSF)) { 697 sc->sc_flags |= CTF_AEOF; 698 sc->sc_flags &= ~CTF_BEOF; 699 } 700 /* 701 * Otherwise if we read something we are now before EOF 702 * (and no longer after EOF). 703 */ 704 else if (blks) { 705 sc->sc_flags |= CTF_BEOF; 706 sc->sc_flags &= ~CTF_AEOF; 707 } 708 /* 709 * Finally, if we didn't read anything we just passed an EOF 710 */ 711 else 712 sc->sc_flags |= CTF_AEOF; 713 #ifdef DEBUG 714 if (ctdebug & CDB_FILES) 715 printf("cteof: leaving flags %x\n", sc->sc_flags); 716 #endif 717 } 718 719 /* ARGSUSED */ 720 static void 721 ctintr(void *arg) 722 { 723 struct ct_softc *sc = arg; 724 struct buf *bp; 725 uint8_t stat; 726 int ctlr, slave, unit; 727 728 ctlr = device_unit(device_parent(sc->sc_dev)); 729 slave = sc->sc_slave; 730 unit = device_unit(sc->sc_dev); 731 732 bp = BUFQ_PEEK(sc->sc_tab); 733 if (bp == NULL) { 734 printf("%s: bp == NULL\n", device_xname(sc->sc_dev)); 735 return; 736 } 737 if (sc->sc_flags & CTF_IO) { 738 sc->sc_flags &= ~CTF_IO; 739 if (hpibustart(ctlr)) 740 ctgo(sc); 741 return; 742 } 743 if ((sc->sc_flags & CTF_STATWAIT) == 0) { 744 if (hpibpptest(ctlr, slave) == 0) { 745 sc->sc_flags |= CTF_STATWAIT; 746 hpibawait(ctlr); 747 return; 748 } 749 } else 750 sc->sc_flags &= ~CTF_STATWAIT; 751 hpibrecv(ctlr, slave, C_QSTAT, &stat, 1); 752 #ifdef DEBUG 753 if (ctdebug & CDB_FILES) 754 printf("ctintr: before flags %x\n", sc->sc_flags); 755 #endif 756 if (stat) { 757 sc->sc_rsc.unit = C_SUNIT(sc->sc_punit); 758 sc->sc_rsc.cmd = C_STATUS; 759 hpibsend(ctlr, slave, C_CMD, &sc->sc_rsc, sizeof(sc->sc_rsc)); 760 hpibrecv(ctlr, slave, C_EXEC, &sc->sc_stat, 761 sizeof(sc->sc_stat)); 762 hpibrecv(ctlr, slave, C_QSTAT, &stat, 1); 763 #ifdef DEBUG 764 if (ctdebug & CDB_FILES) 765 printf("ctintr: return stat 0x%x, A%x F%x blk %ld\n", 766 stat, sc->sc_stat.c_aef, 767 sc->sc_stat.c_fef, sc->sc_stat.c_blk); 768 #endif 769 if (stat == 0) { 770 if (sc->sc_stat.c_aef & (AEF_EOF | AEF_EOV)) { 771 cteof(sc, bp); 772 ctaddeof(sc); 773 goto done; 774 } 775 if (sc->sc_stat.c_fef & FEF_PF) { 776 ctreset(sc); 777 ctstart(sc); 778 return; 779 } 780 if (sc->sc_stat.c_fef & FEF_REXMT) { 781 ctstart(sc); 782 return; 783 } 784 if (sc->sc_stat.c_aef & 0x5800) { 785 if (sc->sc_stat.c_aef & 0x4000) 786 tprintf(sc->sc_tpr, 787 "%s: uninitialized media\n", 788 device_xname(sc->sc_dev)); 789 if (sc->sc_stat.c_aef & 0x1000) 790 tprintf(sc->sc_tpr, 791 "%s: not ready\n", 792 device_xname(sc->sc_dev)); 793 if (sc->sc_stat.c_aef & 0x0800) 794 tprintf(sc->sc_tpr, 795 "%s: write protect\n", 796 device_xname(sc->sc_dev)); 797 } else { 798 printf("%s err: v%d u%d ru%d bn%ld, ", 799 device_xname(sc->sc_dev), 800 (sc->sc_stat.c_vu >> 4) & 0xF, 801 sc->sc_stat.c_vu & 0xF, 802 sc->sc_stat.c_pend, 803 sc->sc_stat.c_blk); 804 printf("R0x%x F0x%x A0x%x I0x%x\n", 805 sc->sc_stat.c_ref, 806 sc->sc_stat.c_fef, 807 sc->sc_stat.c_aef, 808 sc->sc_stat.c_ief); 809 } 810 } else 811 printf("%s: request status failed\n", 812 device_xname(sc->sc_dev)); 813 bp->b_error = EIO; 814 goto done; 815 } else 816 bp->b_resid = 0; 817 if (sc->sc_flags & CTF_CMD) { 818 switch (sc->sc_cmd) { 819 case MTFSF: 820 sc->sc_flags &= ~(CTF_BEOF|CTF_AEOF); 821 sc->sc_blkno += CTBTOK(sc->sc_resid); 822 ctstart(sc); 823 return; 824 case MTBSF: 825 sc->sc_flags &= ~(CTF_AEOF|CTF_BEOF|CTF_EOT); 826 break; 827 case MTBSR: 828 sc->sc_flags &= ~CTF_BEOF; 829 if (sc->sc_flags & CTF_EOT) { 830 sc->sc_flags |= CTF_AEOF; 831 sc->sc_flags &= ~CTF_EOT; 832 } else if (sc->sc_flags & CTF_AEOF) { 833 sc->sc_flags |= CTF_BEOF; 834 sc->sc_flags &= ~CTF_AEOF; 835 } 836 break; 837 case MTWEOF: 838 sc->sc_flags &= ~CTF_BEOF; 839 if (sc->sc_flags & (CTF_AEOF|CTF_EOT)) { 840 sc->sc_flags |= CTF_EOT; 841 sc->sc_flags &= ~CTF_AEOF; 842 } else 843 sc->sc_flags |= CTF_AEOF; 844 break; 845 case MTREW: 846 case MTOFFL: 847 sc->sc_flags &= ~(CTF_BEOF|CTF_AEOF|CTF_EOT); 848 break; 849 } 850 } else { 851 sc->sc_flags &= ~CTF_AEOF; 852 sc->sc_blkno += CTBTOK(sc->sc_resid); 853 } 854 done: 855 #ifdef DEBUG 856 if (ctdebug & CDB_FILES) 857 printf("ctintr: after flags %x\n", sc->sc_flags); 858 #endif 859 ctdone(sc, bp); 860 } 861 862 static void 863 ctdone(struct ct_softc *sc, struct buf *bp) 864 { 865 866 (void)BUFQ_GET(sc->sc_tab); 867 biodone(bp); 868 hpibfree(device_parent(sc->sc_dev), &sc->sc_hq); 869 if (BUFQ_PEEK(sc->sc_tab) == NULL) { 870 sc->sc_active = 0; 871 return; 872 } 873 ctustart(sc); 874 } 875 876 static int 877 ctread(dev_t dev, struct uio *uio, int flags) 878 { 879 880 return physio(ctstrategy, NULL, dev, B_READ, minphys, uio); 881 } 882 883 static int 884 ctwrite(dev_t dev, struct uio *uio, int flags) 885 { 886 887 /* XXX: check for hardware write-protect? */ 888 return physio(ctstrategy, NULL, dev, B_WRITE, minphys, uio); 889 } 890 891 /*ARGSUSED*/ 892 static int 893 ctioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) 894 { 895 struct mtop *op; 896 int cnt; 897 898 switch (cmd) { 899 900 case MTIOCTOP: 901 op = (struct mtop *)data; 902 switch(op->mt_op) { 903 904 case MTWEOF: 905 case MTFSF: 906 case MTBSR: 907 case MTBSF: 908 case MTFSR: 909 cnt = op->mt_count; 910 break; 911 912 case MTREW: 913 case MTOFFL: 914 cnt = 1; 915 break; 916 917 default: 918 return EINVAL; 919 } 920 ctcommand(dev, op->mt_op, cnt); 921 break; 922 923 case MTIOCGET: 924 break; 925 926 default: 927 return EINVAL; 928 } 929 return 0; 930 } 931 932 static void 933 ctaddeof(struct ct_softc *sc) 934 { 935 936 if (sc->sc_eofp == EOFS - 1) 937 sc->sc_eofs[EOFS - 1]++; 938 else { 939 sc->sc_eofp++; 940 if (sc->sc_eofp == EOFS - 1) 941 sc->sc_eofs[EOFS - 1] = EOFS; 942 else 943 /* save blkno */ 944 sc->sc_eofs[sc->sc_eofp] = sc->sc_blkno - 1; 945 } 946 #ifdef DEBUG 947 if (ctdebug & CT_BSF) 948 printf("%s: add eof pos %d blk %d\n", 949 device_xname(sc->sc_dev), sc->sc_eofp, 950 sc->sc_eofs[sc->sc_eofp]); 951 #endif 952 } 953