1 /* $NetBSD: siop.c,v 1.65 2010/01/29 19:56:41 phx Exp $ */ 2 3 /* 4 * Copyright (c) 1990 The Regents of the University of California. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Van Jacobson of Lawrence Berkeley Laboratory. 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. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)siop.c 7.5 (Berkeley) 5/4/91 35 */ 36 37 /* 38 * Copyright (c) 1994 Michael L. Hitch 39 * 40 * This code is derived from software contributed to Berkeley by 41 * Van Jacobson of Lawrence Berkeley Laboratory. 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 * 52 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 53 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 54 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 55 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 56 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 57 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 58 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 59 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 60 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 61 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 62 * 63 * @(#)siop.c 7.5 (Berkeley) 5/4/91 64 */ 65 66 /* 67 * AMIGA 53C710 scsi adaptor driver 68 */ 69 70 #include "opt_ddb.h" 71 72 #include <sys/cdefs.h> 73 __KERNEL_RCSID(0, "$NetBSD: siop.c,v 1.65 2010/01/29 19:56:41 phx Exp $"); 74 75 #include <sys/param.h> 76 #include <sys/systm.h> 77 #include <sys/callout.h> 78 #include <sys/kernel.h> 79 #include <sys/device.h> 80 #include <sys/disklabel.h> 81 #include <sys/buf.h> 82 #include <sys/malloc.h> 83 84 #include <uvm/uvm_extern.h> 85 86 #include <dev/scsipi/scsi_all.h> 87 #include <dev/scsipi/scsipi_all.h> 88 #include <dev/scsipi/scsiconf.h> 89 #include <machine/cpu.h> 90 #ifdef __m68k__ 91 #include <m68k/cacheops.h> 92 #else 93 #define DCIAS(pa) 94 #endif 95 #include <amiga/amiga/custom.h> 96 #include <amiga/amiga/isr.h> 97 #include <amiga/dev/siopreg.h> 98 #include <amiga/dev/siopvar.h> 99 100 /* 101 * SCSI delays 102 * In u-seconds, primarily for state changes on the SPC. 103 */ 104 #define SCSI_CMD_WAIT 500000 /* wait per step of 'immediate' cmds */ 105 #define SCSI_DATA_WAIT 500000 /* wait per data in/out step */ 106 #define SCSI_INIT_WAIT 500000 /* wait per step (both) during init */ 107 108 void siop_select(struct siop_softc *); 109 void siopabort(struct siop_softc *, siop_regmap_p, const char *); 110 void sioperror(struct siop_softc *, siop_regmap_p, u_char); 111 void siopstart(struct siop_softc *); 112 int siop_checkintr(struct siop_softc *, u_char, u_char, u_char, int *); 113 void siopreset(struct siop_softc *); 114 void siopsetdelay(int); 115 void siop_scsidone(struct siop_acb *, int); 116 void siop_timeout(void *); 117 void siop_sched(struct siop_softc *); 118 void siop_poll(struct siop_softc *, struct siop_acb *); 119 void siopintr(struct siop_softc *); 120 void scsi_period_to_siop(struct siop_softc *, int); 121 void siop_start(struct siop_softc *, int, int, u_char *, int, u_char *, int); 122 void siop_dump_acb(struct siop_acb *); 123 124 /* 53C710 script */ 125 const 126 #include <amiga/dev/siop_script.out> 127 128 /* default to not inhibit sync negotiation on any drive */ 129 u_char siop_inhibit_sync[8] = { 0, 0, 0, 0, 0, 0, 0 }; /* initialize, so patchable */ 130 u_char siop_allow_disc[8] = {3, 3, 3, 3, 3, 3, 3, 3}; 131 int siop_no_dma = 0; 132 133 int siop_reset_delay = 250; /* delay after reset, in milleseconds */ 134 135 int siop_cmd_wait = SCSI_CMD_WAIT; 136 int siop_data_wait = SCSI_DATA_WAIT; 137 int siop_init_wait = SCSI_INIT_WAIT; 138 139 #ifdef DEBUG_SYNC 140 /* 141 * sync period transfer lookup - only valid for 66 MHz clock 142 */ 143 static struct { 144 unsigned char p; /* period from sync request message */ 145 unsigned char r; /* siop_period << 4 | sbcl */ 146 } sync_tab[] = { 147 { 60/4, 0<<4 | 1}, 148 { 76/4, 1<<4 | 1}, 149 { 92/4, 2<<4 | 1}, 150 { 92/4, 0<<4 | 2}, 151 {108/4, 3<<4 | 1}, 152 {116/4, 1<<4 | 2}, 153 {120/4, 4<<4 | 1}, 154 {120/4, 0<<4 | 3}, 155 {136/4, 5<<4 | 1}, 156 {140/4, 2<<4 | 2}, 157 {152/4, 6<<4 | 1}, 158 {152/4, 1<<4 | 3}, 159 {164/4, 3<<4 | 2}, 160 {168/4, 7<<4 | 1}, 161 {180/4, 2<<4 | 3}, 162 {184/4, 4<<4 | 2}, 163 {208/4, 5<<4 | 2}, 164 {212/4, 3<<4 | 3}, 165 {232/4, 6<<4 | 2}, 166 {240/4, 4<<4 | 3}, 167 {256/4, 7<<4 | 2}, 168 {272/4, 5<<4 | 3}, 169 {300/4, 6<<4 | 3}, 170 {332/4, 7<<4 | 3} 171 }; 172 #endif 173 174 #ifdef DEBUG 175 /* 176 * 0x01 - full debug 177 * 0x02 - DMA chaining 178 * 0x04 - siopintr 179 * 0x08 - phase mismatch 180 * 0x10 - <not used> 181 * 0x20 - panic on unhandled exceptions 182 * 0x100 - disconnect/reselect 183 */ 184 int siop_debug = 0; 185 int siopsync_debug = 0; 186 int siopdma_hits = 0; 187 int siopdma_misses = 0; 188 int siopchain_ints = 0; 189 int siopstarts = 0; 190 int siopints = 0; 191 int siopphmm = 0; 192 #define SIOP_TRACE_SIZE 128 193 #define SIOP_TRACE(a,b,c,d) \ 194 siop_trbuf[siop_trix] = (a); \ 195 siop_trbuf[siop_trix+1] = (b); \ 196 siop_trbuf[siop_trix+2] = (c); \ 197 siop_trbuf[siop_trix+3] = (d); \ 198 siop_trix = (siop_trix + 4) & (SIOP_TRACE_SIZE - 1); 199 u_char siop_trbuf[SIOP_TRACE_SIZE]; 200 int siop_trix; 201 void siop_dump(struct siop_softc *); 202 void siop_dump_trace(void); 203 #else 204 #define SIOP_TRACE(a,b,c,d) 205 #endif 206 207 208 /* 209 * default minphys routine for siop based controllers 210 */ 211 void 212 siop_minphys(struct buf *bp) 213 { 214 215 /* 216 * No max transfer at this level. 217 */ 218 minphys(bp); 219 } 220 221 /* 222 * used by specific siop controller 223 * 224 */ 225 void 226 siop_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req, 227 void *arg) 228 { 229 struct scsipi_xfer *xs; 230 struct scsipi_periph *periph; 231 struct siop_acb *acb; 232 struct siop_softc *sc = (void *)chan->chan_adapter->adapt_dev; 233 int flags, s; 234 235 switch (req) { 236 case ADAPTER_REQ_RUN_XFER: 237 xs = arg; 238 periph = xs->xs_periph; 239 flags = xs->xs_control; 240 241 /* XXXX ?? */ 242 if (flags & XS_CTL_DATA_UIO) 243 panic("siop: scsi data uio requested"); 244 245 /* XXXX ?? */ 246 if (sc->sc_nexus && flags & XS_CTL_POLL) 247 /* panic("siop_scsicmd: busy");*/ 248 printf("siop_scsicmd: busy\n"); 249 250 s = splbio(); 251 acb = sc->free_list.tqh_first; 252 if (acb) { 253 TAILQ_REMOVE(&sc->free_list, acb, chain); 254 } 255 splx(s); 256 257 #ifdef DIAGNOSTIC 258 /* 259 * This should never happen as we track the resources 260 * in the mid-layer. 261 */ 262 if (acb == NULL) { 263 scsipi_printaddr(periph); 264 printf("unable to allocate acb\n"); 265 panic("siop_scsipi_request"); 266 } 267 #endif 268 269 acb->flags = ACB_ACTIVE; 270 acb->xs = xs; 271 memcpy(&acb->cmd, xs->cmd, xs->cmdlen); 272 acb->clen = xs->cmdlen; 273 acb->daddr = xs->data; 274 acb->dleft = xs->datalen; 275 276 s = splbio(); 277 TAILQ_INSERT_TAIL(&sc->ready_list, acb, chain); 278 279 if (sc->sc_nexus == NULL) 280 siop_sched(sc); 281 282 splx(s); 283 284 if (flags & XS_CTL_POLL || siop_no_dma) 285 siop_poll(sc, acb); 286 return; 287 288 case ADAPTER_REQ_GROW_RESOURCES: 289 return; 290 291 case ADAPTER_REQ_SET_XFER_MODE: 292 return; 293 } 294 } 295 296 void 297 siop_poll(struct siop_softc *sc, struct siop_acb *acb) 298 { 299 siop_regmap_p rp = sc->sc_siopp; 300 struct scsipi_xfer *xs = acb->xs; 301 int i; 302 int status; 303 u_char istat; 304 u_char dstat; 305 u_char sstat0; 306 int s; 307 int to; 308 309 s = splbio(); 310 to = xs->timeout / 1000; 311 if (sc->nexus_list.tqh_first) 312 printf("%s: siop_poll called with disconnected device\n", 313 sc->sc_dev.dv_xname); 314 for (;;) { 315 /* use cmd_wait values? */ 316 i = 50000; 317 /* XXX spl0(); */ 318 while (((istat = rp->siop_istat) & 319 (SIOP_ISTAT_SIP | SIOP_ISTAT_DIP)) == 0) { 320 if (--i <= 0) { 321 #ifdef DEBUG 322 printf ("waiting: tgt %d cmd %02x sbcl %02x dsp %lx (+%lx) dcmd %lx ds %p timeout %d\n", 323 xs->xs_periph->periph_target, acb->cmd.opcode, 324 rp->siop_sbcl, rp->siop_dsp, 325 rp->siop_dsp - sc->sc_scriptspa, 326 *((volatile long *)&rp->siop_dcmd), &acb->ds, acb->xs->timeout); 327 #endif 328 i = 50000; 329 --to; 330 if (to <= 0) { 331 siopreset(sc); 332 return; 333 } 334 } 335 delay(20); 336 } 337 sstat0 = rp->siop_sstat0; 338 dstat = rp->siop_dstat; 339 if (siop_checkintr(sc, istat, dstat, sstat0, &status)) { 340 if (acb != sc->sc_nexus) 341 printf("%s: siop_poll disconnected device completed\n", 342 sc->sc_dev.dv_xname); 343 else if ((sc->sc_flags & SIOP_INTDEFER) == 0) { 344 sc->sc_flags &= ~SIOP_INTSOFF; 345 rp->siop_sien = sc->sc_sien; 346 rp->siop_dien = sc->sc_dien; 347 } 348 siop_scsidone(sc->sc_nexus, status); 349 } 350 351 if (xs->xs_status & XS_STS_DONE) 352 break; 353 } 354 splx(s); 355 } 356 357 /* 358 * start next command that's ready 359 */ 360 void 361 siop_sched(struct siop_softc *sc) 362 { 363 struct scsipi_periph *periph; 364 struct siop_acb *acb; 365 int i; 366 367 #ifdef DEBUG 368 if (sc->sc_nexus) { 369 printf("%s: siop_sched- nexus %p/%d ready %p/%d\n", 370 sc->sc_dev.dv_xname, sc->sc_nexus, 371 sc->sc_nexus->xs->xs_periph->periph_target, 372 sc->ready_list.tqh_first, 373 sc->ready_list.tqh_first->xs->xs_periph->periph_target); 374 return; 375 } 376 #endif 377 for (acb = sc->ready_list.tqh_first; acb; acb = acb->chain.tqe_next) { 378 periph = acb->xs->xs_periph; 379 i = periph->periph_target; 380 if(!(sc->sc_tinfo[i].lubusy & (1 << periph->periph_lun))) { 381 struct siop_tinfo *ti = &sc->sc_tinfo[i]; 382 383 TAILQ_REMOVE(&sc->ready_list, acb, chain); 384 sc->sc_nexus = acb; 385 periph = acb->xs->xs_periph; 386 ti = &sc->sc_tinfo[periph->periph_target]; 387 ti->lubusy |= (1 << periph->periph_lun); 388 break; 389 } 390 } 391 392 if (acb == NULL) { 393 #ifdef DEBUGXXX 394 printf("%s: siop_sched didn't find ready command\n", 395 sc->sc_dev.dv_xname); 396 #endif 397 return; 398 } 399 400 if (acb->xs->xs_control & XS_CTL_RESET) 401 siopreset(sc); 402 403 #if 0 404 acb->cmd.bytes[0] |= slp->scsipi_scsi.lun << 5; /* XXXX */ 405 #endif 406 ++sc->sc_active; 407 siop_select(sc); 408 } 409 410 void 411 siop_scsidone(struct siop_acb *acb, int stat) 412 { 413 struct scsipi_xfer *xs; 414 struct scsipi_periph *periph; 415 struct siop_softc *sc; 416 int dosched = 0; 417 418 if (acb == NULL || (xs = acb->xs) == NULL) { 419 #ifdef DIAGNOSTIC 420 printf("siop_scsidone: NULL acb or scsipi_xfer\n"); 421 #if defined(DEBUG) && defined(DDB) 422 Debugger(); 423 #endif 424 #endif 425 return; 426 } 427 428 callout_stop(&xs->xs_callout); 429 430 periph = xs->xs_periph; 431 sc = (void *)periph->periph_channel->chan_adapter->adapt_dev; 432 433 xs->status = stat; 434 xs->resid = 0; /* XXXX */ 435 436 if (xs->error == XS_NOERROR) { 437 if (stat == SCSI_CHECK || stat == SCSI_BUSY) 438 xs->error = XS_BUSY; 439 } 440 441 /* 442 * Remove the ACB from whatever queue it's on. We have to do a bit of 443 * a hack to figure out which queue it's on. Note that it is *not* 444 * necessary to cdr down the ready queue, but we must cdr down the 445 * nexus queue and see if it's there, so we can mark the unit as no 446 * longer busy. This code is sickening, but it works. 447 */ 448 if (acb == sc->sc_nexus) { 449 sc->sc_nexus = NULL; 450 sc->sc_tinfo[periph->periph_target].lubusy &= 451 ~(1<<periph->periph_lun); 452 if (sc->ready_list.tqh_first) 453 dosched = 1; /* start next command */ 454 --sc->sc_active; 455 SIOP_TRACE('d','a',stat,0) 456 } else if (sc->ready_list.tqh_last == &acb->chain.tqe_next) { 457 TAILQ_REMOVE(&sc->ready_list, acb, chain); 458 SIOP_TRACE('d','r',stat,0) 459 } else { 460 register struct siop_acb *acb2; 461 for (acb2 = sc->nexus_list.tqh_first; acb2; 462 acb2 = acb2->chain.tqe_next) 463 if (acb2 == acb) { 464 TAILQ_REMOVE(&sc->nexus_list, acb, chain); 465 sc->sc_tinfo[periph->periph_target].lubusy 466 &= ~(1<<periph->periph_lun); 467 --sc->sc_active; 468 break; 469 } 470 if (acb2) 471 ; 472 else if (acb->chain.tqe_next) { 473 TAILQ_REMOVE(&sc->ready_list, acb, chain); 474 --sc->sc_active; 475 } else { 476 printf("%s: can't find matching acb\n", 477 sc->sc_dev.dv_xname); 478 #ifdef DDB 479 /* Debugger(); */ 480 #endif 481 } 482 SIOP_TRACE('d','n',stat,0); 483 } 484 /* Put it on the free list. */ 485 acb->flags = ACB_FREE; 486 TAILQ_INSERT_HEAD(&sc->free_list, acb, chain); 487 488 sc->sc_tinfo[periph->periph_target].cmds++; 489 490 scsipi_done(xs); 491 492 if (dosched && sc->sc_nexus == NULL) 493 siop_sched(sc); 494 } 495 496 void 497 siopabort(register struct siop_softc *sc, siop_regmap_p rp, const char *where) 498 { 499 #ifdef fix_this 500 int i; 501 #endif 502 503 printf ("%s: abort %s: dstat %02x, sstat0 %02x sbcl %02x\n", 504 sc->sc_dev.dv_xname, 505 where, rp->siop_dstat, rp->siop_sstat0, rp->siop_sbcl); 506 507 if (sc->sc_active > 0) { 508 #ifdef TODO 509 SET_SBIC_cmd (rp, SBIC_CMD_ABORT); 510 WAIT_CIP (rp); 511 512 GET_SBIC_asr (rp, asr); 513 if (asr & (SBIC_ASR_BSY|SBIC_ASR_LCI)) 514 { 515 /* ok, get more drastic.. */ 516 517 SET_SBIC_cmd (rp, SBIC_CMD_RESET); 518 delay(25); 519 SBIC_WAIT(rp, SBIC_ASR_INT, 0); 520 GET_SBIC_csr (rp, csr); /* clears interrupt also */ 521 522 return; 523 } 524 525 do 526 { 527 SBIC_WAIT (rp, SBIC_ASR_INT, 0); 528 GET_SBIC_csr (rp, csr); 529 } 530 while ((csr != SBIC_CSR_DISC) && (csr != SBIC_CSR_DISC_1) 531 && (csr != SBIC_CSR_CMD_INVALID)); 532 #endif 533 534 /* lets just hope it worked.. */ 535 #ifdef fix_this 536 for (i = 0; i < 2; ++i) { 537 if (sc->sc_iob[i].sc_xs && &sc->sc_iob[i] != 538 sc->sc_cur) { 539 printf ("siopabort: cleanup!\n"); 540 sc->sc_iob[i].sc_xs = NULL; 541 } 542 } 543 #endif /* fix_this */ 544 /* sc->sc_active = 0; */ 545 } 546 } 547 548 void 549 siopinitialize(struct siop_softc *sc) 550 { 551 int i; 552 u_int inhibit_sync; 553 extern u_long scsi_nosync; 554 extern int shift_nosync; 555 556 /* 557 * Need to check that scripts is on a long word boundary 558 * Also should verify that dev doesn't span non-contiguous 559 * physical pages. 560 */ 561 sc->sc_scriptspa = kvtop((void *)__UNCONST(scripts)); 562 563 /* 564 * malloc sc_acb to ensure that DS is on a long word boundary. 565 */ 566 567 sc->sc_acb = malloc(sizeof(struct siop_acb) * SIOP_NACB, 568 M_DEVBUF, M_NOWAIT); 569 if (sc->sc_acb == NULL) 570 panic("siopinitialize: ACB malloc failed!"); 571 572 sc->sc_tcp[1] = 1000 / sc->sc_clock_freq; 573 sc->sc_tcp[2] = 1500 / sc->sc_clock_freq; 574 sc->sc_tcp[3] = 2000 / sc->sc_clock_freq; 575 sc->sc_minsync = sc->sc_tcp[1]; /* in 4ns units */ 576 if (sc->sc_minsync < 25) 577 sc->sc_minsync = 25; 578 if (sc->sc_clock_freq <= 25) { 579 sc->sc_dcntl |= 0x80; /* SCLK/1 */ 580 sc->sc_tcp[0] = sc->sc_tcp[1]; 581 } else if (sc->sc_clock_freq <= 37) { 582 sc->sc_dcntl |= 0x40; /* SCLK/1.5 */ 583 sc->sc_tcp[0] = sc->sc_tcp[2]; 584 } else if (sc->sc_clock_freq <= 50) { 585 sc->sc_dcntl |= 0x00; /* SCLK/2 */ 586 sc->sc_tcp[0] = sc->sc_tcp[3]; 587 } else { 588 sc->sc_dcntl |= 0xc0; /* SCLK/3 */ 589 sc->sc_tcp[0] = 3000 / sc->sc_clock_freq; 590 } 591 592 if (scsi_nosync) { 593 inhibit_sync = (scsi_nosync >> shift_nosync) & 0xff; 594 shift_nosync += 8; 595 #ifdef DEBUG 596 if (inhibit_sync) 597 printf("%s: Inhibiting synchronous transfer %02x\n", 598 sc->sc_dev.dv_xname, inhibit_sync); 599 #endif 600 for (i = 0; i < 8; ++i) 601 if (inhibit_sync & (1 << i)) 602 siop_inhibit_sync[i] = 1; 603 } 604 605 siopreset (sc); 606 } 607 608 void 609 siop_timeout(void *arg) 610 { 611 struct siop_acb *acb; 612 struct scsipi_periph *periph; 613 struct siop_softc *sc; 614 int s; 615 616 acb = arg; 617 periph = acb->xs->xs_periph; 618 sc = device_private(periph->periph_channel->chan_adapter->adapt_dev); 619 scsipi_printaddr(periph); 620 printf("timed out\n"); 621 622 s = splbio(); 623 624 acb->xs->error = XS_TIMEOUT; 625 siopreset(sc); 626 627 splx(s); 628 } 629 630 void 631 siopreset(struct siop_softc *sc) 632 { 633 siop_regmap_p rp; 634 u_int i, s; 635 u_char dummy; 636 struct siop_acb *acb; 637 638 rp = sc->sc_siopp; 639 640 if (sc->sc_flags & SIOP_ALIVE) 641 siopabort(sc, rp, "reset"); 642 643 printf("%s: ", sc->sc_dev.dv_xname); /* XXXX */ 644 645 s = splbio(); 646 647 /* 648 * Reset the chip 649 * XXX - is this really needed? 650 */ 651 rp->siop_istat |= SIOP_ISTAT_ABRT; /* abort current script */ 652 rp->siop_istat |= SIOP_ISTAT_RST; /* reset chip */ 653 rp->siop_istat &= ~SIOP_ISTAT_RST; 654 /* 655 * Reset SCSI bus (do we really want this?) 656 */ 657 rp->siop_sien = 0; 658 rp->siop_scntl1 |= SIOP_SCNTL1_RST; 659 delay(1); 660 rp->siop_scntl1 &= ~SIOP_SCNTL1_RST; 661 662 /* 663 * Set up various chip parameters 664 */ 665 rp->siop_scntl0 = SIOP_ARB_FULL | SIOP_SCNTL0_EPC | SIOP_SCNTL0_EPG; 666 rp->siop_scntl1 = SIOP_SCNTL1_ESR; 667 rp->siop_dcntl = sc->sc_dcntl; 668 rp->siop_dmode = 0x80; /* burst length = 4 */ 669 rp->siop_sien = 0x00; /* don't enable interrupts yet */ 670 rp->siop_dien = 0x00; /* don't enable interrupts yet */ 671 rp->siop_scid = 1 << sc->sc_channel.chan_id; 672 rp->siop_dwt = 0x00; 673 rp->siop_ctest0 |= SIOP_CTEST0_BTD | SIOP_CTEST0_EAN; 674 rp->siop_ctest7 |= sc->sc_ctest7; 675 676 /* will need to re-negotiate sync xfers */ 677 memset(&sc->sc_sync, 0, sizeof (sc->sc_sync)); 678 679 i = rp->siop_istat; 680 if (i & SIOP_ISTAT_SIP) 681 dummy = rp->siop_sstat0; 682 if (i & SIOP_ISTAT_DIP) 683 dummy = rp->siop_dstat; 684 685 splx (s); 686 687 delay (siop_reset_delay * 1000); 688 printf("siop id %d reset V%d\n", sc->sc_channel.chan_id, 689 rp->siop_ctest8 >> 4); 690 691 if ((sc->sc_flags & SIOP_ALIVE) == 0) { 692 TAILQ_INIT(&sc->ready_list); 693 TAILQ_INIT(&sc->nexus_list); 694 TAILQ_INIT(&sc->free_list); 695 sc->sc_nexus = NULL; 696 acb = sc->sc_acb; 697 memset(acb, 0, sizeof(struct siop_acb) * SIOP_NACB); 698 for (i = 0; i < SIOP_NACB; i++) { 699 TAILQ_INSERT_TAIL(&sc->free_list, acb, chain); 700 acb++; 701 } 702 memset(sc->sc_tinfo, 0, sizeof(sc->sc_tinfo)); 703 } else { 704 if (sc->sc_nexus != NULL) { 705 sc->sc_nexus->xs->error = XS_RESET; 706 siop_scsidone(sc->sc_nexus, sc->sc_nexus->stat[0]); 707 } 708 while ((acb = sc->nexus_list.tqh_first) > 0) { 709 acb->xs->error = XS_RESET; 710 siop_scsidone(acb, acb->stat[0]); 711 } 712 } 713 714 sc->sc_flags |= SIOP_ALIVE; 715 sc->sc_flags &= ~(SIOP_INTDEFER|SIOP_INTSOFF); 716 /* enable SCSI and DMA interrupts */ 717 sc->sc_sien = SIOP_SIEN_M_A | SIOP_SIEN_STO | /*SIOP_SIEN_SEL |*/ SIOP_SIEN_SGE | 718 SIOP_SIEN_UDC | SIOP_SIEN_RST | SIOP_SIEN_PAR; 719 sc->sc_dien = SIOP_DIEN_BF | SIOP_DIEN_ABRT | SIOP_DIEN_SIR | 720 /*SIOP_DIEN_WTD |*/ SIOP_DIEN_IID; 721 rp->siop_sien = sc->sc_sien; 722 rp->siop_dien = sc->sc_dien; 723 } 724 725 /* 726 * Setup Data Storage for 53C710 and start SCRIPTS processing 727 */ 728 729 void 730 siop_start(struct siop_softc *sc, int target, int lun, u_char *cbuf, int clen, 731 u_char *buf, int len) 732 { 733 siop_regmap_p rp = sc->sc_siopp; 734 int nchain; 735 int count, tcount; 736 char *addr, *dmaend; 737 struct siop_acb *acb = sc->sc_nexus; 738 #ifdef DEBUG 739 int i; 740 #endif 741 742 #ifdef DEBUG 743 if (siop_debug & 0x100 && rp->siop_sbcl & SIOP_BSY) { 744 printf ("ACK! siop was busy: rp %p script %p dsa %p active %ld\n", 745 rp, &scripts, &acb->ds, sc->sc_active); 746 printf ("istat %02x sfbr %02x lcrc %02x sien %02x dien %02x\n", 747 rp->siop_istat, rp->siop_sfbr, rp->siop_lcrc, 748 rp->siop_sien, rp->siop_dien); 749 #ifdef DDB 750 /*Debugger();*/ 751 #endif 752 } 753 #endif 754 acb->msgout[0] = MSG_IDENTIFY | lun; 755 if (siop_allow_disc[target] & 2 || 756 (siop_allow_disc[target] && len == 0)) 757 acb->msgout[0] = MSG_IDENTIFY_DR | lun; 758 acb->status = 0; 759 acb->stat[0] = -1; 760 acb->msg[0] = -1; 761 acb->ds.scsi_addr = (0x10000 << target) | (sc->sc_sync[target].sxfer << 8); 762 acb->ds.idlen = 1; 763 acb->ds.idbuf = (char *) kvtop(&acb->msgout[0]); 764 acb->ds.cmdlen = clen; 765 acb->ds.cmdbuf = (char *) kvtop(cbuf); 766 acb->ds.stslen = 1; 767 acb->ds.stsbuf = (char *) kvtop(&acb->stat[0]); 768 acb->ds.msglen = 1; 769 acb->ds.msgbuf = (char *) kvtop(&acb->msg[0]); 770 acb->msg[1] = -1; 771 acb->ds.msginlen = 1; 772 acb->ds.extmsglen = 1; 773 acb->ds.synmsglen = 3; 774 acb->ds.msginbuf = (char *) kvtop(&acb->msg[1]); 775 acb->ds.extmsgbuf = (char *) kvtop(&acb->msg[2]); 776 acb->ds.synmsgbuf = (char *) kvtop(&acb->msg[3]); 777 memset(&acb->ds.chain, 0, sizeof (acb->ds.chain)); 778 779 /* 780 * Negotiate wide is the initial negotiation state; since the 53c710 781 * doesn't do wide transfers, just begin the synchronous transfer 782 * negotation here. 783 */ 784 if (sc->sc_sync[target].state == NEG_WIDE) { 785 if (siop_inhibit_sync[target]) { 786 sc->sc_sync[target].state = NEG_DONE; 787 sc->sc_sync[target].sbcl = 0; 788 sc->sc_sync[target].sxfer = 0; 789 #ifdef DEBUG 790 if (siopsync_debug) 791 printf ("Forcing target %d asynchronous\n", target); 792 #endif 793 } 794 else { 795 acb->msg[2] = -1; 796 acb->msgout[1] = MSG_EXT_MESSAGE; 797 acb->msgout[2] = 3; 798 acb->msgout[3] = MSG_SYNC_REQ; 799 #ifdef MAXTOR_SYNC_KLUDGE 800 acb->msgout[4] = 50 / 4; /* ask for ridiculous period */ 801 #else 802 acb->msgout[4] = sc->sc_minsync; 803 #endif 804 acb->msgout[5] = SIOP_MAX_OFFSET; 805 acb->ds.idlen = 6; 806 sc->sc_sync[target].state = NEG_WAITS; 807 #ifdef DEBUG 808 if (siopsync_debug) 809 printf ("Sending sync request to target %d\n", target); 810 #endif 811 } 812 } 813 814 /* 815 * Build physical DMA addresses for scatter/gather I/O 816 */ 817 acb->iob_buf = buf; 818 acb->iob_len = len; 819 acb->iob_curbuf = acb->iob_curlen = 0; 820 nchain = 0; 821 count = len; 822 addr = buf; 823 dmaend = NULL; 824 while (count > 0) { 825 acb->ds.chain[nchain].databuf = (char *) kvtop (addr); 826 if (count < (tcount = PAGE_SIZE - ((int) addr & PGOFSET))) 827 tcount = count; 828 acb->ds.chain[nchain].datalen = tcount; 829 addr += tcount; 830 count -= tcount; 831 if (acb->ds.chain[nchain].databuf == dmaend) { 832 dmaend += acb->ds.chain[nchain].datalen; 833 acb->ds.chain[nchain].datalen = 0; 834 acb->ds.chain[--nchain].datalen += tcount; 835 #ifdef DEBUG 836 ++siopdma_hits; 837 #endif 838 } 839 else { 840 dmaend = acb->ds.chain[nchain].databuf + 841 acb->ds.chain[nchain].datalen; 842 acb->ds.chain[nchain].datalen = tcount; 843 #ifdef DEBUG 844 if (nchain) /* Don't count miss on first one */ 845 ++siopdma_misses; 846 #endif 847 } 848 ++nchain; 849 } 850 #ifdef DEBUG 851 if (nchain != 1 && len != 0 && siop_debug & 3) { 852 printf ("DMA chaining set: %d\n", nchain); 853 for (i = 0; i < nchain; ++i) { 854 printf (" [%d] %8p %lx\n", i, acb->ds.chain[i].databuf, 855 acb->ds.chain[i].datalen); 856 } 857 } 858 #endif 859 860 /* push data cache for all data the 53c710 needs to access */ 861 dma_cachectl ((void *)acb, sizeof (struct siop_acb)); 862 dma_cachectl (cbuf, clen); 863 if (buf != NULL && len != 0) 864 dma_cachectl (buf, len); 865 #ifdef DEBUG 866 if (siop_debug & 0x100 && rp->siop_sbcl & SIOP_BSY) { 867 printf ("ACK! siop was busy at start: rp %p script %p dsa %p active %ld\n", 868 rp, &scripts, &acb->ds, sc->sc_active); 869 #ifdef DDB 870 /*Debugger();*/ 871 #endif 872 } 873 #endif 874 if (sc->nexus_list.tqh_first == NULL) { 875 callout_reset(&acb->xs->xs_callout, 876 mstohz(acb->xs->timeout) + 1, siop_timeout, acb); 877 if (rp->siop_istat & SIOP_ISTAT_CON) 878 printf("%s: siop_select while connected?\n", 879 sc->sc_dev.dv_xname); 880 rp->siop_temp = 0; 881 rp->siop_sbcl = sc->sc_sync[target].sbcl; 882 rp->siop_dsa = kvtop((void *)&acb->ds); 883 rp->siop_dsp = sc->sc_scriptspa; 884 SIOP_TRACE('s',1,0,0) 885 } else { 886 if ((rp->siop_istat & SIOP_ISTAT_CON) == 0) { 887 rp->siop_istat = SIOP_ISTAT_SIGP; 888 SIOP_TRACE('s',2,0,0); 889 } 890 else { 891 SIOP_TRACE('s',3,rp->siop_istat,0); 892 } 893 } 894 #ifdef DEBUG 895 ++siopstarts; 896 #endif 897 } 898 899 /* 900 * Process a DMA or SCSI interrupt from the 53C710 SIOP 901 */ 902 903 int 904 siop_checkintr(struct siop_softc *sc, u_char istat, u_char dstat, 905 u_char sstat0, int *status) 906 { 907 siop_regmap_p rp = sc->sc_siopp; 908 struct siop_acb *acb = sc->sc_nexus; 909 int target = 0; 910 int dfifo, dbc, sstat1; 911 912 dfifo = rp->siop_dfifo; 913 dbc = rp->siop_dbc0; 914 sstat1 = rp->siop_sstat1; 915 rp->siop_ctest8 |= SIOP_CTEST8_CLF; 916 while ((rp->siop_ctest1 & SIOP_CTEST1_FMT) != SIOP_CTEST1_FMT) 917 ; 918 rp->siop_ctest8 &= ~SIOP_CTEST8_CLF; 919 #ifdef DEBUG 920 ++siopints; 921 #if 0 922 if (siop_debug & 0x100) { 923 DCIAS(&acb->stat[0]); /* XXX */ 924 printf ("siopchkintr: istat %x dstat %x sstat0 %x dsps %x sbcl %x sts %x msg %x\n", 925 istat, dstat, sstat0, rp->siop_dsps, rp->siop_sbcl, acb->stat[0], acb->msg[0]); 926 printf ("sync msg in: %02x %02x %02x %02x %02x %02x\n", 927 acb->msg[0], acb->msg[1], acb->msg[2], 928 acb->msg[3], acb->msg[4], acb->msg[5]); 929 } 930 #endif 931 if (rp->siop_dsp && (rp->siop_dsp < sc->sc_scriptspa || 932 rp->siop_dsp >= sc->sc_scriptspa + sizeof(scripts))) { 933 printf ("%s: dsp not within script dsp %lx scripts %lx:%lx", 934 sc->sc_dev.dv_xname, rp->siop_dsp, sc->sc_scriptspa, 935 sc->sc_scriptspa + sizeof(scripts)); 936 printf(" istat %x dstat %x sstat0 %x\n", 937 istat, dstat, sstat0); 938 #ifdef DDB 939 Debugger(); 940 #endif 941 } 942 #endif 943 SIOP_TRACE('i',dstat,istat,(istat&SIOP_ISTAT_DIP)?rp->siop_dsps&0xff:sstat0); 944 if (dstat & SIOP_DSTAT_SIR && rp->siop_dsps == 0xff00) { 945 /* Normal completion status, or check condition */ 946 #ifdef DEBUG 947 if (rp->siop_dsa != kvtop((void *)&acb->ds)) { 948 printf ("siop: invalid dsa: %lx %x\n", rp->siop_dsa, 949 (unsigned)kvtop((void *)&acb->ds)); 950 panic("*** siop DSA invalid ***"); 951 } 952 #endif 953 target = acb->xs->xs_periph->periph_target; 954 if (sc->sc_sync[target].state == NEG_WAITS) { 955 if (acb->msg[1] == 0xff) 956 printf ("%s: target %d ignored sync request\n", 957 sc->sc_dev.dv_xname, target); 958 else if (acb->msg[1] == MSG_REJECT) 959 printf ("%s: target %d rejected sync request\n", 960 sc->sc_dev.dv_xname, target); 961 else 962 /* XXX - need to set sync transfer parameters */ 963 printf("%s: target %d (sync) %02x %02x %02x\n", 964 sc->sc_dev.dv_xname, target, acb->msg[1], 965 acb->msg[2], acb->msg[3]); 966 sc->sc_sync[target].state = NEG_DONE; 967 } 968 dma_cachectl(&acb->stat[0], 1); 969 *status = acb->stat[0]; 970 #ifdef DEBUG 971 if (rp->siop_sbcl & SIOP_BSY) { 972 /*printf ("ACK! siop was busy at end: rp %x script %x dsa %x\n", 973 rp, &scripts, &acb->ds);*/ 974 #ifdef DDB 975 /*Debugger();*/ 976 #endif 977 } 978 if (acb->msg[0] != 0x00) 979 printf("%s: message was not COMMAND COMPLETE: %x\n", 980 sc->sc_dev.dv_xname, acb->msg[0]); 981 #endif 982 if (sc->nexus_list.tqh_first) 983 rp->siop_dcntl |= SIOP_DCNTL_STD; 984 return 1; 985 } 986 if (dstat & SIOP_DSTAT_SIR && rp->siop_dsps == 0xff0b) { 987 target = acb->xs->xs_periph->periph_target; 988 if (acb->msg[1] == MSG_EXT_MESSAGE && acb->msg[2] == 3 && 989 acb->msg[3] == MSG_SYNC_REQ) { 990 #ifdef DEBUG 991 if (siopsync_debug) 992 printf ("sync msg in: %02x %02x %02x %02x %02x %02x\n", 993 acb->msg[0], acb->msg[1], acb->msg[2], 994 acb->msg[3], acb->msg[4], acb->msg[5]); 995 #endif 996 sc->sc_sync[target].sxfer = 0; 997 sc->sc_sync[target].sbcl = 0; 998 if (acb->msg[2] == 3 && 999 acb->msg[3] == MSG_SYNC_REQ && 1000 acb->msg[5] != 0) { 1001 #ifdef MAXTOR_KLUDGE 1002 /* 1003 * Kludge for my Maxtor XT8580S 1004 * It accepts whatever we request, even 1005 * though it won't work. So we ask for 1006 * a short period than we can handle. If 1007 * the device says it can do it, use 208ns. 1008 * If the device says it can do less than 1009 * 100ns, then we limit it to 100ns. 1010 */ 1011 if (acb->msg[4] && acb->msg[4] < 100 / 4) { 1012 #ifdef DEBUG 1013 printf ("%d: target %d wanted %dns period\n", 1014 sc->sc_dev.dv_xname, target, 1015 acb->msg[4] * 4); 1016 #endif 1017 if (acb->msg[4] == 50 / 4) 1018 acb->msg[4] = 208 / 4; 1019 else 1020 acb->msg[4] = 100 / 4; 1021 } 1022 #endif /* MAXTOR_KLUDGE */ 1023 printf ("%s: target %d now synchronous, period=%dns, offset=%d\n", 1024 sc->sc_dev.dv_xname, target, 1025 acb->msg[4] * 4, acb->msg[5]); 1026 scsi_period_to_siop (sc, target); 1027 } 1028 rp->siop_sxfer = sc->sc_sync[target].sxfer; 1029 rp->siop_sbcl = sc->sc_sync[target].sbcl; 1030 if (sc->sc_sync[target].state == NEG_WAITS) { 1031 sc->sc_sync[target].state = NEG_DONE; 1032 rp->siop_dsp = sc->sc_scriptspa + Ent_clear_ack; 1033 return(0); 1034 } 1035 rp->siop_dcntl |= SIOP_DCNTL_STD; 1036 sc->sc_sync[target].state = NEG_DONE; 1037 return (0); 1038 } 1039 /* XXX - not SDTR message */ 1040 } 1041 if (sstat0 & SIOP_SSTAT0_M_A) { /* Phase mismatch */ 1042 #ifdef DEBUG 1043 ++siopphmm; 1044 if (acb == NULL) 1045 printf("%s: Phase mismatch with no active command?\n", 1046 sc->sc_dev.dv_xname); 1047 #endif 1048 if (acb->iob_len) { 1049 int adjust; 1050 adjust = ((dfifo - (dbc & 0x7f)) & 0x7f); 1051 if (sstat1 & SIOP_SSTAT1_ORF) 1052 ++adjust; 1053 if (sstat1 & SIOP_SSTAT1_OLF) 1054 ++adjust; 1055 acb->iob_curlen = 1056 *((long *)__UNVOLATILE(&rp->siop_dcmd)) & 0xffffff; 1057 acb->iob_curlen += adjust; 1058 acb->iob_curbuf = 1059 *((long *)__UNVOLATILE(&rp->siop_dnad)) - adjust; 1060 #ifdef DEBUG 1061 if (siop_debug & 0x100) { 1062 int i; 1063 printf ("Phase mismatch: curbuf %lx curlen %lx dfifo %x dbc %x sstat1 %x adjust %x sbcl %x starts %d acb %p\n", 1064 acb->iob_curbuf, acb->iob_curlen, dfifo, 1065 dbc, sstat1, adjust, rp->siop_sbcl, 1066 siopstarts, acb); 1067 if (acb->ds.chain[1].datalen) { 1068 for (i = 0; acb->ds.chain[i].datalen; ++i) 1069 printf("chain[%d] addr %p len %lx\n", 1070 i, acb->ds.chain[i].databuf, 1071 acb->ds.chain[i].datalen); 1072 } 1073 } 1074 #endif 1075 dma_cachectl ((void *)acb, sizeof(*acb)); 1076 } 1077 #ifdef DEBUG 1078 SIOP_TRACE('m',rp->siop_sbcl,(rp->siop_dsp>>8),rp->siop_dsp); 1079 if (siop_debug & 9) 1080 printf ("Phase mismatch: %x dsp +%lx dcmd %lx\n", 1081 rp->siop_sbcl, 1082 rp->siop_dsp - sc->sc_scriptspa, 1083 *((volatile long *)&rp->siop_dcmd)); 1084 #endif 1085 if ((rp->siop_sbcl & SIOP_REQ) == 0) { 1086 printf ("Phase mismatch: REQ not asserted! %02x dsp %lx\n", 1087 rp->siop_sbcl, rp->siop_dsp); 1088 #if defined(DEBUG) && defined(DDB) 1089 /*Debugger(); XXX is*/ 1090 #endif 1091 } 1092 switch (rp->siop_sbcl & 7) { 1093 case 0: /* data out */ 1094 case 1: /* data in */ 1095 case 2: /* status */ 1096 case 3: /* command */ 1097 case 6: /* message in */ 1098 case 7: /* message out */ 1099 rp->siop_dsp = sc->sc_scriptspa + Ent_switch; 1100 break; 1101 default: 1102 goto bad_phase; 1103 } 1104 return 0; 1105 } 1106 if (sstat0 & SIOP_SSTAT0_STO) { /* Select timed out */ 1107 #ifdef DEBUG 1108 if (acb == NULL) 1109 printf("%s: Select timeout with no active command?\n", 1110 sc->sc_dev.dv_xname); 1111 if (rp->siop_sbcl & SIOP_BSY) { 1112 printf ("ACK! siop was busy at timeout: rp %p script %p dsa %p\n", 1113 rp, &scripts, &acb->ds); 1114 printf(" sbcl %x sdid %x istat %x dstat %x sstat0 %x\n", 1115 rp->siop_sbcl, rp->siop_sdid, istat, dstat, sstat0); 1116 if (!(rp->siop_sbcl & SIOP_BSY)) { 1117 printf ("Yikes, it's not busy now!\n"); 1118 #if 0 1119 *status = -1; 1120 if (sc->nexus_list.tqh_first) 1121 rp->siop_dsp = sc->sc_scriptspa + Ent_wait_reselect; 1122 return 1; 1123 #endif 1124 } 1125 /* rp->siop_dcntl |= SIOP_DCNTL_STD;*/ 1126 return (0); 1127 #ifdef DDB 1128 Debugger(); 1129 #endif 1130 } 1131 #endif 1132 *status = -1; 1133 acb->xs->error = XS_SELTIMEOUT; 1134 if (sc->nexus_list.tqh_first) 1135 rp->siop_dsp = sc->sc_scriptspa + Ent_wait_reselect; 1136 return 1; 1137 } 1138 if (acb) 1139 target = acb->xs->xs_periph->periph_target; 1140 else 1141 target = 7; 1142 if (sstat0 & SIOP_SSTAT0_UDC) { 1143 #ifdef DEBUG 1144 if (acb == NULL) 1145 printf("%s: Unexpected disconnect with no active command?\n", 1146 sc->sc_dev.dv_xname); 1147 printf ("%s: target %d disconnected unexpectedly\n", 1148 sc->sc_dev.dv_xname, target); 1149 #endif 1150 #if 0 1151 siopabort (sc, rp, "siopchkintr"); 1152 #endif 1153 *status = STS_BUSY; 1154 if (sc->nexus_list.tqh_first) 1155 rp->siop_dsp = sc->sc_scriptspa + Ent_wait_reselect; 1156 return (acb != NULL); 1157 } 1158 if (dstat & SIOP_DSTAT_SIR && (rp->siop_dsps == 0xff01 || 1159 rp->siop_dsps == 0xff02)) { 1160 #ifdef DEBUG 1161 if (siop_debug & 0x100) 1162 printf ("%s: ID %02x disconnected TEMP %lx (+%lx) curbuf %lx curlen %lx buf %p len %lx dfifo %x dbc %x sstat1 %x starts %d acb %p\n", 1163 sc->sc_dev.dv_xname, 1 << target, rp->siop_temp, 1164 rp->siop_temp ? rp->siop_temp - sc->sc_scriptspa : 0, 1165 acb->iob_curbuf, acb->iob_curlen, 1166 acb->ds.chain[0].databuf, acb->ds.chain[0].datalen, dfifo, dbc, sstat1, siopstarts, acb); 1167 #endif 1168 if (acb == NULL) { 1169 printf("%s: Disconnect with no active command?\n", 1170 sc->sc_dev.dv_xname); 1171 return (0); 1172 } 1173 /* 1174 * XXXX need to update iob_curbuf/iob_curlen to reflect 1175 * current data transferred. If device disconnected in 1176 * the middle of a DMA block, they should already be set 1177 * by the phase change interrupt. If the disconnect 1178 * occurs on a DMA block boundary, we have to figure out 1179 * which DMA block it was. 1180 */ 1181 if (acb->iob_len && rp->siop_temp) { 1182 int n = rp->siop_temp - sc->sc_scriptspa; 1183 1184 if (acb->iob_curlen && acb->iob_curlen != acb->ds.chain[0].datalen) 1185 printf("%s: iob_curbuf/len already set? n %x iob %lx/%lx chain[0] %p/%lx\n", 1186 sc->sc_dev.dv_xname, n, acb->iob_curbuf, acb->iob_curlen, 1187 acb->ds.chain[0].databuf, acb->ds.chain[0].datalen); 1188 if (n < Ent_datain) 1189 n = (n - Ent_dataout) / 16; 1190 else 1191 n = (n - Ent_datain) / 16; 1192 if (n <= 0 && n > DMAMAXIO) 1193 printf("TEMP invalid %d\n", n); 1194 else { 1195 acb->iob_curbuf = (u_long)acb->ds.chain[n].databuf; 1196 acb->iob_curlen = acb->ds.chain[n].datalen; 1197 } 1198 #ifdef DEBUG 1199 if (siop_debug & 0x100) { 1200 printf("%s: TEMP offset %d", sc->sc_dev.dv_xname, n); 1201 printf(" curbuf %lx curlen %lx\n", acb->iob_curbuf, 1202 acb->iob_curlen); 1203 } 1204 #endif 1205 } 1206 /* 1207 * If data transfer was interrupted by disconnect, iob_curbuf 1208 * and iob_curlen should reflect the point of interruption. 1209 * Adjust the DMA chain so that the data transfer begins 1210 * at the appropriate place upon reselection. 1211 * XXX This should only be done on save data pointer message? 1212 */ 1213 if (acb->iob_curlen) { 1214 int i, j; 1215 1216 #ifdef DEBUG 1217 if (siop_debug & 0x100) 1218 printf ("%s: adjusting DMA chain\n", 1219 sc->sc_dev.dv_xname); 1220 if (rp->siop_dsps == 0xff02) 1221 printf ("%s: ID %02x disconnected without Save Data Pointers\n", 1222 sc->sc_dev.dv_xname, 1 << target); 1223 #endif 1224 /* XXX is: if (rp->siop_dsps != 0xff02) { */ 1225 /* not disconnected without save data ptr */ 1226 for (i = 0; i < DMAMAXIO; ++i) { 1227 if (acb->ds.chain[i].datalen == 0) 1228 break; 1229 if (acb->iob_curbuf >= (long)acb->ds.chain[i].databuf && 1230 acb->iob_curbuf < (long)(acb->ds.chain[i].databuf + 1231 acb->ds.chain[i].datalen)) 1232 break; 1233 } 1234 if (i >= DMAMAXIO || acb->ds.chain[i].datalen == 0) { 1235 printf("couldn't find saved data pointer: "); 1236 printf("curbuf %lx curlen %lx i %d\n", 1237 acb->iob_curbuf, acb->iob_curlen, i); 1238 #ifdef DDB 1239 Debugger(); 1240 #endif 1241 } 1242 /* XXX is: } */ 1243 #ifdef DEBUG 1244 if (siop_debug & 0x100) 1245 printf(" chain[0]: %p/%lx -> %lx/%lx\n", 1246 acb->ds.chain[0].databuf, 1247 acb->ds.chain[0].datalen, 1248 acb->iob_curbuf, 1249 acb->iob_curlen); 1250 #endif 1251 acb->ds.chain[0].databuf = (char *)acb->iob_curbuf; 1252 acb->ds.chain[0].datalen = acb->iob_curlen; 1253 for (j = 1, ++i; i < DMAMAXIO && acb->ds.chain[i].datalen; ++i, ++j) { 1254 #ifdef DEBUG 1255 if (siop_debug & 0x100) 1256 printf(" chain[%d]: %p/%lx -> %p/%lx\n", j, 1257 acb->ds.chain[j].databuf, 1258 acb->ds.chain[j].datalen, 1259 acb->ds.chain[i].databuf, 1260 acb->ds.chain[i].datalen); 1261 #endif 1262 acb->ds.chain[j].databuf = acb->ds.chain[i].databuf; 1263 acb->ds.chain[j].datalen = acb->ds.chain[i].datalen; 1264 } 1265 if (j < DMAMAXIO) 1266 acb->ds.chain[j].datalen = 0; 1267 DCIAS(kvtop((void *)&acb->ds.chain)); 1268 } 1269 ++sc->sc_tinfo[target].dconns; 1270 /* 1271 * add nexus to waiting list 1272 * clear nexus 1273 * try to start another command for another target/lun 1274 */ 1275 acb->status = sc->sc_flags & SIOP_INTSOFF; 1276 TAILQ_INSERT_HEAD(&sc->nexus_list, acb, chain); 1277 sc->sc_nexus = NULL; /* no current device */ 1278 /* start script to wait for reselect */ 1279 if (sc->sc_nexus == NULL) 1280 rp->siop_dsp = sc->sc_scriptspa + Ent_wait_reselect; 1281 /* XXXX start another command ? */ 1282 if (sc->ready_list.tqh_first) 1283 siop_sched(sc); 1284 return (0); 1285 } 1286 if (dstat & SIOP_DSTAT_SIR && rp->siop_dsps == 0xff03) { 1287 int reselid = rp->siop_scratch & 0x7f; 1288 int reselun = rp->siop_sfbr & 0x07; 1289 1290 sc->sc_sstat1 = rp->siop_sbcl; /* XXXX save current SBCL */ 1291 #ifdef DEBUG 1292 if (siop_debug & 0x100) 1293 printf ("%s: target ID %02x reselected dsps %lx\n", 1294 sc->sc_dev.dv_xname, reselid, 1295 rp->siop_dsps); 1296 if ((rp->siop_sfbr & 0x80) == 0) 1297 printf("%s: Reselect message in was not identify: %x\n", 1298 sc->sc_dev.dv_xname, rp->siop_sfbr); 1299 #endif 1300 if (sc->sc_nexus) { 1301 #ifdef DEBUG 1302 if (siop_debug & 0x100) 1303 printf ("%s: reselect ID %02x w/active\n", 1304 sc->sc_dev.dv_xname, reselid); 1305 #endif 1306 TAILQ_INSERT_HEAD(&sc->ready_list, sc->sc_nexus, chain); 1307 sc->sc_tinfo[sc->sc_nexus->xs->xs_periph->periph_target].lubusy 1308 &= ~(1 << sc->sc_nexus->xs->xs_periph->periph_lun); 1309 --sc->sc_active; 1310 } 1311 /* 1312 * locate acb of reselecting device 1313 * set sc->sc_nexus to acb 1314 */ 1315 for (acb = sc->nexus_list.tqh_first; acb; 1316 acb = acb->chain.tqe_next) { 1317 if (reselid != (acb->ds.scsi_addr >> 16) || 1318 reselun != (acb->msgout[0] & 0x07)) 1319 continue; 1320 TAILQ_REMOVE(&sc->nexus_list, acb, chain); 1321 sc->sc_nexus = acb; 1322 sc->sc_flags |= acb->status; 1323 acb->status = 0; 1324 DCIAS(kvtop(&acb->stat[0])); 1325 rp->siop_dsa = kvtop((void *)&acb->ds); 1326 rp->siop_sxfer = 1327 sc->sc_sync[acb->xs->xs_periph->periph_target].sxfer; 1328 rp->siop_sbcl = 1329 sc->sc_sync[acb->xs->xs_periph->periph_target].sbcl; 1330 break; 1331 } 1332 if (acb == NULL) { 1333 printf("%s: target ID %02x reselect nexus_list %p\n", 1334 sc->sc_dev.dv_xname, reselid, 1335 sc->nexus_list.tqh_first); 1336 panic("unable to find reselecting device"); 1337 } 1338 dma_cachectl ((void *)acb, sizeof(*acb)); 1339 rp->siop_temp = 0; 1340 rp->siop_dcntl |= SIOP_DCNTL_STD; 1341 return (0); 1342 } 1343 if (dstat & SIOP_DSTAT_SIR && rp->siop_dsps == 0xff04) { 1344 #ifdef DEBUG 1345 u_short ctest2 = rp->siop_ctest2; 1346 1347 /* reselect was interrupted (by Sig_P or select) */ 1348 if (siop_debug & 0x100 || 1349 (ctest2 & SIOP_CTEST2_SIGP) == 0) 1350 printf ("%s: reselect interrupted (Sig_P?) scntl1 %x ctest2 %x sfbr %x istat %x/%x\n", 1351 sc->sc_dev.dv_xname, rp->siop_scntl1, 1352 ctest2, rp->siop_sfbr, istat, rp->siop_istat); 1353 #endif 1354 /* XXX assumes it was not select */ 1355 if (sc->sc_nexus == NULL) { 1356 #ifdef DEBUG 1357 printf("%s: reselect interrupted, sc_nexus == NULL\n", 1358 sc->sc_dev.dv_xname); 1359 #if 0 1360 siop_dump(sc); 1361 #ifdef DDB 1362 Debugger(); 1363 #endif 1364 #endif 1365 #endif 1366 rp->siop_dcntl |= SIOP_DCNTL_STD; 1367 return(0); 1368 } 1369 target = sc->sc_nexus->xs->xs_periph->periph_target; 1370 rp->siop_temp = 0; 1371 rp->siop_dsa = kvtop((void *)&sc->sc_nexus->ds); 1372 rp->siop_sxfer = sc->sc_sync[target].sxfer; 1373 rp->siop_sbcl = sc->sc_sync[target].sbcl; 1374 rp->siop_dsp = sc->sc_scriptspa; 1375 return (0); 1376 } 1377 if (dstat & SIOP_DSTAT_SIR && rp->siop_dsps == 0xff06) { 1378 if (acb == NULL) 1379 printf("%s: Bad message-in with no active command?\n", 1380 sc->sc_dev.dv_xname); 1381 /* Unrecognized message in byte */ 1382 dma_cachectl (&acb->msg[1],1); 1383 printf ("%s: Unrecognized message in data sfbr %x msg %x sbcl %x\n", 1384 sc->sc_dev.dv_xname, rp->siop_sfbr, acb->msg[1], rp->siop_sbcl); 1385 /* what should be done here? */ 1386 DCIAS(kvtop(&acb->msg[1])); 1387 rp->siop_dsp = sc->sc_scriptspa + Ent_clear_ack; 1388 return (0); 1389 } 1390 if (dstat & SIOP_DSTAT_SIR && rp->siop_dsps == 0xff0a) { 1391 /* Status phase wasn't followed by message in phase? */ 1392 printf ("%s: Status phase not followed by message in phase? sbcl %x sbdl %x\n", 1393 sc->sc_dev.dv_xname, rp->siop_sbcl, rp->siop_sbdl); 1394 if (rp->siop_sbcl == 0xa7) { 1395 /* It is now, just continue the script? */ 1396 rp->siop_dcntl |= SIOP_DCNTL_STD; 1397 return (0); 1398 } 1399 } 1400 if (sstat0 == 0 && dstat & SIOP_DSTAT_SIR) { 1401 dma_cachectl (&acb->stat[0], 1); 1402 dma_cachectl (&acb->msg[0], 1); 1403 printf ("SIOP interrupt: %lx sts %x msg %x %x sbcl %x\n", 1404 rp->siop_dsps, acb->stat[0], acb->msg[0], acb->msg[1], 1405 rp->siop_sbcl); 1406 siopreset (sc); 1407 *status = -1; 1408 return 0; /* siopreset has cleaned up */ 1409 } 1410 if (sstat0 & SIOP_SSTAT0_SGE) 1411 printf ("SIOP: SCSI Gross Error\n"); 1412 if (sstat0 & SIOP_SSTAT0_PAR) 1413 printf ("SIOP: Parity Error\n"); 1414 if (dstat & SIOP_DSTAT_IID) 1415 printf ("SIOP: Invalid instruction detected\n"); 1416 bad_phase: 1417 /* 1418 * temporary panic for unhandled conditions 1419 * displays various things about the 53C710 status and registers 1420 * then panics. 1421 * XXXX need to clean this up to print out the info, reset, and continue 1422 */ 1423 printf ("siopchkintr: target %x ds %p\n", target, &acb->ds); 1424 printf ("scripts %lx ds %x rp %x dsp %lx dcmd %lx\n", 1425 sc->sc_scriptspa, (unsigned)kvtop((void *)&acb->ds), 1426 (unsigned)kvtop((void *)__UNVOLATILE(rp)), rp->siop_dsp, 1427 *((volatile long *)&rp->siop_dcmd)); 1428 printf ("siopchkintr: istat %x dstat %x sstat0 %x dsps %lx dsa %lx sbcl %x sts %x msg %x %x sfbr %x\n", 1429 istat, dstat, sstat0, rp->siop_dsps, rp->siop_dsa, 1430 rp->siop_sbcl, acb->stat[0], acb->msg[0], acb->msg[1], rp->siop_sfbr); 1431 #ifdef DEBUG 1432 if (siop_debug & 0x20) 1433 panic("siopchkintr: **** temp ****"); 1434 #endif 1435 #ifdef DDB 1436 Debugger (); 1437 #endif 1438 siopreset (sc); /* hard reset */ 1439 *status = -1; 1440 return 0; /* siopreset cleaned up */ 1441 } 1442 1443 void 1444 siop_select(struct siop_softc *sc) 1445 { 1446 siop_regmap_p rp; 1447 struct siop_acb *acb = sc->sc_nexus; 1448 1449 #ifdef DEBUG 1450 if (siop_debug & 1) 1451 printf ("%s: select ", sc->sc_dev.dv_xname); 1452 #endif 1453 1454 rp = sc->sc_siopp; 1455 if (acb->xs->xs_control & XS_CTL_POLL || siop_no_dma) { 1456 sc->sc_flags |= SIOP_INTSOFF; 1457 sc->sc_flags &= ~SIOP_INTDEFER; 1458 if ((rp->siop_istat & 0x08) == 0) { 1459 rp->siop_sien = 0; 1460 rp->siop_dien = 0; 1461 } 1462 #if 0 1463 } else if ((sc->sc_flags & SIOP_INTDEFER) == 0) { 1464 sc->sc_flags &= ~SIOP_INTSOFF; 1465 if ((rp->siop_istat & 0x08) == 0) { 1466 rp->siop_sien = sc->sc_sien; 1467 rp->siop_dien = sc->sc_dien; 1468 } 1469 #endif 1470 } 1471 #ifdef DEBUG 1472 if (siop_debug & 1) 1473 printf ("siop_select: target %x cmd %02x ds %p\n", 1474 acb->xs->xs_periph->periph_target, acb->cmd.opcode, 1475 &sc->sc_nexus->ds); 1476 #endif 1477 1478 siop_start(sc, acb->xs->xs_periph->periph_target, 1479 acb->xs->xs_periph->periph_lun, 1480 (u_char *)&acb->cmd, acb->clen, acb->daddr, acb->dleft); 1481 1482 return; 1483 } 1484 1485 /* 1486 * 53C710 interrupt handler 1487 */ 1488 1489 void 1490 siopintr(register struct siop_softc *sc) 1491 { 1492 siop_regmap_p rp; 1493 register u_char istat, dstat, sstat0; 1494 int status; 1495 int s = splbio(); 1496 1497 istat = sc->sc_istat; 1498 if ((istat & (SIOP_ISTAT_SIP | SIOP_ISTAT_DIP)) == 0) { 1499 splx(s); 1500 return; 1501 } 1502 1503 /* Got a valid interrupt on this device */ 1504 rp = sc->sc_siopp; 1505 dstat = sc->sc_dstat; 1506 sstat0 = sc->sc_sstat0; 1507 if (dstat & SIOP_DSTAT_SIR) 1508 sc->sc_intcode = rp->siop_dsps; 1509 sc->sc_istat = 0; 1510 #ifdef DEBUG 1511 if (siop_debug & 1) 1512 printf ("%s: intr istat %x dstat %x sstat0 %x\n", 1513 sc->sc_dev.dv_xname, istat, dstat, sstat0); 1514 if (!sc->sc_active) { 1515 printf ("%s: spurious interrupt? istat %x dstat %x sstat0 %x nexus %p status %x\n", 1516 sc->sc_dev.dv_xname, istat, dstat, sstat0, 1517 sc->sc_nexus, sc->sc_nexus ? sc->sc_nexus->stat[0] : 0); 1518 } 1519 #endif 1520 1521 #ifdef DEBUG 1522 if (siop_debug & 5) { 1523 DCIAS(kvtop(&sc->sc_nexus->stat[0])); 1524 printf ("%s: intr istat %x dstat %x sstat0 %x dsps %lx sbcl %x sts %x msg %x\n", 1525 sc->sc_dev.dv_xname, istat, dstat, sstat0, 1526 rp->siop_dsps, rp->siop_sbcl, 1527 sc->sc_nexus->stat[0], sc->sc_nexus->msg[0]); 1528 } 1529 #endif 1530 if (sc->sc_flags & SIOP_INTDEFER) { 1531 sc->sc_flags &= ~(SIOP_INTDEFER | SIOP_INTSOFF); 1532 rp->siop_sien = sc->sc_sien; 1533 rp->siop_dien = sc->sc_dien; 1534 } 1535 if (siop_checkintr (sc, istat, dstat, sstat0, &status)) { 1536 #if 1 1537 if (status == 0xff) 1538 printf ("siopintr: status == 0xff\n"); 1539 #endif 1540 if ((sc->sc_flags & (SIOP_INTSOFF | SIOP_INTDEFER)) != SIOP_INTSOFF) { 1541 #if 0 1542 if (rp->siop_sbcl & SIOP_BSY) { 1543 printf ("%s: SCSI bus busy at completion", 1544 sc->sc_dev.dv_xname); 1545 printf(" targ %d sbcl %02x sfbr %x lcrc %02x dsp +%x\n", 1546 sc->sc_nexus->xs->xs_periph->periph_target, 1547 rp->siop_sbcl, rp->siop_sfbr, rp->siop_lcrc, 1548 rp->siop_dsp - sc->sc_scriptspa); 1549 } 1550 #endif 1551 siop_scsidone(sc->sc_nexus, sc->sc_nexus ? 1552 sc->sc_nexus->stat[0] : -1); 1553 } 1554 } 1555 splx(s); 1556 } 1557 1558 /* 1559 * This is based on the Progressive Peripherals 33Mhz Zeus driver and will 1560 * not be correct for other 53c710 boards. 1561 * 1562 */ 1563 void 1564 scsi_period_to_siop(struct siop_softc *sc, int target) 1565 { 1566 int period, offset, sxfer, sbcl = 0; 1567 #ifdef DEBUG_SYNC 1568 int i; 1569 #endif 1570 1571 period = sc->sc_nexus->msg[4]; 1572 offset = sc->sc_nexus->msg[5]; 1573 #ifdef DEBUG_SYNC 1574 sxfer = 0; 1575 if (offset <= SIOP_MAX_OFFSET) 1576 sxfer = offset; 1577 for (i = 0; i < sizeof (sync_tab) / 2; ++i) { 1578 if (period <= sync_tab[i].p) { 1579 sxfer |= sync_tab[i].r & 0x70; 1580 sbcl = sync_tab[i].r & 0x03; 1581 break; 1582 } 1583 } 1584 printf ("siop sync old: siop_sxfr %02x, siop_sbcl %02x\n", sxfer, sbcl); 1585 #endif 1586 for (sbcl = 1; sbcl < 4; ++sbcl) { 1587 sxfer = (period * 4 - 1) / sc->sc_tcp[sbcl] - 3; 1588 if (sxfer >= 0 && sxfer <= 7) 1589 break; 1590 } 1591 if (sbcl > 3) { 1592 printf("siop sync: unable to compute sync params for period %dns\n", 1593 period * 4); 1594 /* 1595 * XXX need to pick a value we can do and renegotiate 1596 */ 1597 sxfer = sbcl = 0; 1598 } else { 1599 sxfer = (sxfer << 4) | ((offset <= SIOP_MAX_OFFSET) ? 1600 offset : SIOP_MAX_OFFSET); 1601 #ifdef DEBUG_SYNC 1602 printf("siop sync: params for period %dns: sxfer %x sbcl %x", 1603 period * 4, sxfer, sbcl); 1604 printf(" actual period %dns\n", 1605 sc->sc_tcp[sbcl] * ((sxfer >> 4) + 4)); 1606 #endif 1607 } 1608 sc->sc_sync[target].sxfer = sxfer; 1609 sc->sc_sync[target].sbcl = sbcl; 1610 #ifdef DEBUG_SYNC 1611 printf ("siop sync: siop_sxfr %02x, siop_sbcl %02x\n", sxfer, sbcl); 1612 #endif 1613 } 1614 1615 #ifdef DEBUG 1616 1617 #if SIOP_TRACE_SIZE 1618 void 1619 siop_dump_trace(void) 1620 { 1621 int i; 1622 1623 printf("siop trace: next index %d\n", siop_trix); 1624 i = siop_trix; 1625 do { 1626 printf("%3d: '%c' %02x %02x %02x\n", i, siop_trbuf[i], 1627 siop_trbuf[i + 1], siop_trbuf[i + 2], siop_trbuf[i + 3]); 1628 i = (i + 4) & (SIOP_TRACE_SIZE - 1); 1629 } while (i != siop_trix); 1630 } 1631 #endif 1632 1633 void 1634 siop_dump_acb(struct siop_acb *acb) 1635 { 1636 u_char *b = (u_char *) &acb->cmd; 1637 int i; 1638 1639 printf("acb@%p ", acb); 1640 if (acb->xs == NULL) { 1641 printf("<unused>\n"); 1642 return; 1643 } 1644 printf("(%d:%d) flags %2x clen %2d cmd ", 1645 acb->xs->xs_periph->periph_target, 1646 acb->xs->xs_periph->periph_lun, acb->flags, acb->clen); 1647 for (i = acb->clen; i; --i) 1648 printf(" %02x", *b++); 1649 printf("\n"); 1650 printf(" xs: %p data %p:%04x ", acb->xs, acb->xs->data, 1651 acb->xs->datalen); 1652 printf("va %p:%lx ", acb->iob_buf, acb->iob_len); 1653 printf("cur %lx:%lx\n", acb->iob_curbuf, acb->iob_curlen); 1654 } 1655 1656 void 1657 siop_dump(struct siop_softc *sc) 1658 { 1659 struct siop_acb *acb; 1660 siop_regmap_p rp = sc->sc_siopp; 1661 int s; 1662 int i; 1663 1664 s = splbio(); 1665 #if SIOP_TRACE_SIZE 1666 siop_dump_trace(); 1667 #endif 1668 printf("%s@%p regs %p istat %x\n", 1669 sc->sc_dev.dv_xname, sc, rp, rp->siop_istat); 1670 if ((acb = sc->free_list.tqh_first) > 0) { 1671 printf("Free list:\n"); 1672 while (acb) { 1673 siop_dump_acb(acb); 1674 acb = acb->chain.tqe_next; 1675 } 1676 } 1677 if ((acb = sc->ready_list.tqh_first) > 0) { 1678 printf("Ready list:\n"); 1679 while (acb) { 1680 siop_dump_acb(acb); 1681 acb = acb->chain.tqe_next; 1682 } 1683 } 1684 if ((acb = sc->nexus_list.tqh_first) > 0) { 1685 printf("Nexus list:\n"); 1686 while (acb) { 1687 siop_dump_acb(acb); 1688 acb = acb->chain.tqe_next; 1689 } 1690 } 1691 if (sc->sc_nexus) { 1692 printf("Nexus:\n"); 1693 siop_dump_acb(sc->sc_nexus); 1694 } 1695 for (i = 0; i < 8; ++i) { 1696 if (sc->sc_tinfo[i].cmds > 2) { 1697 printf("tgt %d: cmds %d disc %d lubusy %x\n", 1698 i, sc->sc_tinfo[i].cmds, 1699 sc->sc_tinfo[i].dconns, 1700 sc->sc_tinfo[i].lubusy); 1701 } 1702 } 1703 splx(s); 1704 } 1705 #endif 1706