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