1 /* $NetBSD: sunscpal.c,v 1.23 2008/07/06 13:29:50 tsutsui Exp $ */ 2 3 /* 4 * Copyright (c) 2001 Matthew Fredette 5 * Copyright (c) 1995 David Jones, Gordon W. Ross 6 * Copyright (c) 1994 Jarle Greipsland 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. The name of the authors may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 4. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by 22 * David Jones and Gordon Ross 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 27 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, 28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 */ 35 36 /* 37 * This is a machine-independent driver for the Sun "sc" 38 * SCSI Bus Controller (SBC). 39 * 40 * This code should work with any memory-mapped card, 41 * and can be shared by multiple adapters that address 42 * the card with different register offset spacings. 43 * (This can happen on the atari, for example.) 44 * 45 * Credits, history: 46 * 47 * Matthew Fredette completely copied revision 1.38 of 48 * ncr5380sbc.c, and then heavily modified it to match 49 * the Sun sc PAL. The remaining credits are for 50 * ncr5380sbc.c: 51 * 52 * David Jones is the author of most of the code that now 53 * appears in this file, and was the architect of the 54 * current overall structure (MI/MD code separation, etc.) 55 * 56 * Gordon Ross integrated the message phase code, added lots of 57 * comments about what happens when and why (re. SCSI spec.), 58 * debugged some reentrance problems, and added several new 59 * "hooks" needed for the Sun3 "si" adapters. 60 * 61 * The message in/out code was taken nearly verbatim from 62 * the aic6360 driver by Jarle Greipsland. 63 * 64 * Several other NCR5380 drivers were used for reference 65 * while developing this driver, including work by: 66 * The Alice Group (mac68k port) namely: 67 * Allen K. Briggs, Chris P. Caputo, Michael L. Finch, 68 * Bradley A. Grantham, and Lawrence A. Kesteloot 69 * Michael L. Hitch (amiga drivers: sci.c) 70 * Leo Weppelman (atari driver: ncr5380.c) 71 * There are others too. Thanks, everyone. 72 * 73 * Transliteration to bus_space() performed 9/17/98 by 74 * John Ruschmeyer (jruschme@exit109.com) for i386 'nca' driver. 75 * Thank you all. 76 */ 77 78 #include <sys/cdefs.h> 79 __KERNEL_RCSID(0, "$NetBSD: sunscpal.c,v 1.23 2008/07/06 13:29:50 tsutsui Exp $"); 80 81 #include "opt_ddb.h" 82 83 #include <sys/param.h> 84 #include <sys/systm.h> 85 #include <sys/kernel.h> 86 #include <sys/errno.h> 87 #include <sys/malloc.h> 88 #include <sys/device.h> 89 #include <sys/buf.h> 90 #include <sys/proc.h> 91 #include <sys/user.h> 92 93 #include <dev/scsipi/scsi_all.h> 94 #include <dev/scsipi/scsipi_all.h> 95 #include <dev/scsipi/scsipi_debug.h> 96 #include <dev/scsipi/scsi_message.h> 97 #include <dev/scsipi/scsiconf.h> 98 99 #ifdef DDB 100 #include <ddb/db_output.h> 101 #endif 102 103 #include <dev/ic/sunscpalreg.h> 104 #include <dev/ic/sunscpalvar.h> 105 106 static void sunscpal_reset_scsibus(struct sunscpal_softc *); 107 static void sunscpal_sched(struct sunscpal_softc *); 108 static void sunscpal_done(struct sunscpal_softc *); 109 110 static int sunscpal_select(struct sunscpal_softc *, struct sunscpal_req *); 111 static void sunscpal_reselect(struct sunscpal_softc *); 112 113 static int sunscpal_msg_in(struct sunscpal_softc *); 114 static int sunscpal_msg_out(struct sunscpal_softc *); 115 static int sunscpal_data_xfer(struct sunscpal_softc *, int); 116 static int sunscpal_command(struct sunscpal_softc *); 117 static int sunscpal_status(struct sunscpal_softc *); 118 static void sunscpal_machine(struct sunscpal_softc *); 119 120 void sunscpal_abort(struct sunscpal_softc *); 121 void sunscpal_cmd_timeout(void *); 122 /* 123 * Action flags returned by the info_transfer functions: 124 * (These determine what happens next.) 125 */ 126 #define ACT_CONTINUE 0x00 /* No flags: expect another phase */ 127 #define ACT_DISCONNECT 0x01 /* Target is disconnecting */ 128 #define ACT_CMD_DONE 0x02 /* Need to call scsipi_done() */ 129 #define ACT_RESET_BUS 0x04 /* Need bus reset (cmd timeout) */ 130 #define ACT_WAIT_DMA 0x10 /* Wait for DMA to complete */ 131 132 /***************************************************************** 133 * Debugging stuff 134 *****************************************************************/ 135 136 #ifndef DDB 137 /* This is used only in recoverable places. */ 138 #ifndef Debugger 139 #define Debugger() printf("Debug: sunscpal.c:%d\n", __LINE__) 140 #endif 141 #endif 142 143 #ifdef SUNSCPAL_DEBUG 144 145 #define SUNSCPAL_DBG_BREAK 1 146 #define SUNSCPAL_DBG_CMDS 2 147 #define SUNSCPAL_DBG_DMA 4 148 int sunscpal_debug = 0; 149 #define SUNSCPAL_BREAK() \ 150 do { if (sunscpal_debug & SUNSCPAL_DBG_BREAK) Debugger(); } while (0) 151 static void sunscpal_show_scsi_cmd(struct scsipi_xfer *); 152 #ifdef DDB 153 void sunscpal_clear_trace(void); 154 void sunscpal_show_trace(void); 155 void sunscpal_show_req(struct sunscpal_req *); 156 void sunscpal_show_state(void); 157 #endif /* DDB */ 158 #else /* SUNSCPAL_DEBUG */ 159 160 #define SUNSCPAL_BREAK() /* nada */ 161 #define sunscpal_show_scsi_cmd(xs) /* nada */ 162 163 #endif /* SUNSCPAL_DEBUG */ 164 165 static const char * 166 phase_names[8] = { 167 "DATA_OUT", 168 "DATA_IN", 169 "COMMAND", 170 "STATUS", 171 "UNSPEC1", 172 "UNSPEC2", 173 "MSG_OUT", 174 "MSG_IN", 175 }; 176 177 #ifdef SUNSCPAL_USE_BUS_DMA 178 static void sunscpal_dma_alloc(struct sunscpal_softc *); 179 static void sunscpal_dma_free(struct sunscpal_softc *); 180 static void sunscpal_dma_setup(struct sunscpal_softc *); 181 #else 182 #define sunscpal_dma_alloc(sc) (*sc->sc_dma_alloc)(sc) 183 #define sunscpal_dma_free(sc) (*sc->sc_dma_free)(sc) 184 #define sunscpal_dma_setup(sc) (*sc->sc_dma_setup)(sc) 185 #endif 186 static void sunscpal_minphys(struct buf *); 187 188 /***************************************************************** 189 * Actual chip control 190 *****************************************************************/ 191 192 /* 193 * XXX: These timeouts might need to be tuned... 194 */ 195 196 /* This one is used when waiting for a phase change. (X100uS.) */ 197 int sunscpal_wait_phase_timo = 1000 * 10 * 300; /* 5 min. */ 198 199 /* These are used in the following inline functions. */ 200 int sunscpal_wait_req_timo = 1000 * 50; /* X2 = 100 mS. */ 201 int sunscpal_wait_nrq_timo = 1000 * 25; /* X2 = 50 mS. */ 202 203 static inline int sunscpal_wait_req(struct sunscpal_softc *); 204 static inline int sunscpal_wait_not_req(struct sunscpal_softc *); 205 static inline void sunscpal_sched_msgout(struct sunscpal_softc *, int); 206 207 /* Return zero on success. */ 208 static inline int sunscpal_wait_req(struct sunscpal_softc *sc) 209 { 210 int timo = sunscpal_wait_req_timo; 211 212 for (;;) { 213 if (SUNSCPAL_READ_2(sc, sunscpal_icr) & SUNSCPAL_ICR_REQUEST) { 214 timo = 0; /* return 0 */ 215 break; 216 } 217 if (--timo < 0) 218 break; /* return -1 */ 219 delay(2); 220 } 221 return timo; 222 } 223 224 /* Return zero on success. */ 225 static inline int sunscpal_wait_not_req(struct sunscpal_softc *sc) 226 { 227 int timo = sunscpal_wait_nrq_timo; 228 229 for (;;) { 230 if ((SUNSCPAL_READ_2(sc, sunscpal_icr) & 231 SUNSCPAL_ICR_REQUEST) == 0) { 232 timo = 0; /* return 0 */ 233 break; 234 } 235 if (--timo < 0) 236 break; /* return -1 */ 237 delay(2); 238 } 239 return timo; 240 } 241 242 /* 243 * These functions control DMA functions in the chipset independent of 244 * the host DMA implementation. 245 */ 246 static void sunscpal_dma_start(struct sunscpal_softc *); 247 static void sunscpal_dma_poll(struct sunscpal_softc *); 248 static void sunscpal_dma_stop(struct sunscpal_softc *); 249 250 static void 251 sunscpal_dma_start(struct sunscpal_softc *sc) 252 { 253 struct sunscpal_req *sr = sc->sc_current; 254 int xlen; 255 uint16_t icr; 256 257 xlen = sc->sc_reqlen; 258 259 /* Let'er rip! */ 260 icr = SUNSCPAL_READ_2(sc, sunscpal_icr); 261 icr |= SUNSCPAL_ICR_DMA_ENABLE | 262 ((xlen & 1) ? 0 : SUNSCPAL_ICR_WORD_MODE) | 263 ((sr->sr_flags & SR_IMMED) ? 0 : SUNSCPAL_ICR_INTERRUPT_ENABLE); 264 SUNSCPAL_WRITE_2(sc, sunscpal_icr, icr); 265 266 sc->sc_state |= SUNSCPAL_DOINGDMA; 267 268 #ifdef SUNSCPAL_DEBUG 269 if (sunscpal_debug & SUNSCPAL_DBG_DMA) { 270 printf("%s: started, flags=0x%x\n", 271 __func__, sc->sc_state); 272 } 273 #endif 274 } 275 276 #define ICR_MASK (SUNSCPAL_ICR_PARITY_ERROR | SUNSCPAL_ICR_BUS_ERROR | SUNSCPAL_ICR_INTERRUPT_REQUEST) 277 #define POLL_TIMO 50000 /* X100 = 5 sec. */ 278 279 /* 280 * Poll (spin-wait) for DMA completion. 281 * Called right after xx_dma_start(), and 282 * xx_dma_stop() will be called next. 283 */ 284 static void 285 sunscpal_dma_poll(struct sunscpal_softc *sc) 286 { 287 struct sunscpal_req *sr = sc->sc_current; 288 int tmo; 289 290 /* Make sure DMA started successfully. */ 291 if (sc->sc_state & SUNSCPAL_ABORTING) 292 return; 293 294 /* Wait for any "DMA complete" or error bits. */ 295 tmo = POLL_TIMO; 296 for (;;) { 297 if (SUNSCPAL_READ_2(sc, sunscpal_icr) & ICR_MASK) 298 break; 299 if (--tmo <= 0) { 300 printf("sc: DMA timeout (while polling)\n"); 301 /* Indicate timeout as MI code would. */ 302 sr->sr_flags |= SR_OVERDUE; 303 break; 304 } 305 delay(100); 306 } 307 SUNSCPAL_TRACE("sunscpal_dma_poll: waited %d\n", POLL_TIMO - tmo); 308 309 #ifdef SUNSCPAL_DEBUG 310 if (sunscpal_debug & SUNSCPAL_DBG_DMA) { 311 char buffer[64]; 312 bitmask_snprintf(SUNSCPAL_READ_2(sc, sunscpal_icr), 313 SUNSCPAL_ICR_BITS, buffer, sizeof(buffer)); 314 printf("%s: done, icr=%s\n", __func__, buffer); 315 } 316 #endif 317 } 318 319 static void 320 sunscpal_dma_stop(struct sunscpal_softc *sc) 321 { 322 struct sunscpal_req *sr = sc->sc_current; 323 struct scsipi_xfer *xs = sr->sr_xs; 324 int resid, ntrans; 325 uint16_t icr; 326 327 if ((sc->sc_state & SUNSCPAL_DOINGDMA) == 0) { 328 #ifdef DEBUG 329 printf("%s: DMA not running\n", __func__); 330 #endif 331 return; 332 } 333 sc->sc_state &= ~SUNSCPAL_DOINGDMA; 334 335 /* First, halt the DMA engine. */ 336 icr = SUNSCPAL_READ_2(sc, sunscpal_icr); 337 icr &= ~(SUNSCPAL_ICR_DMA_ENABLE | SUNSCPAL_ICR_WORD_MODE | 338 SUNSCPAL_ICR_INTERRUPT_ENABLE); 339 SUNSCPAL_WRITE_2(sc, sunscpal_icr, icr); 340 341 #ifdef SUNSCPAL_USE_BUS_DMA 342 /* 343 * XXX - this function is supposed to be independent of 344 * the host's DMA implementation. 345 */ 346 { 347 sunscpal_dma_handle_t dh = sr->sr_dma_hand; 348 349 /* sync the DMA map: */ 350 bus_dmamap_sync(sc->sunscpal_dmat, dh->dh_dmamap, 0, dh->dh_maplen, 351 ((xs->xs_control & XS_CTL_DATA_OUT) == 0 ? 352 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE)); 353 } 354 #endif /* SUNSCPAL_USE_BUS_DMA */ 355 356 357 if (icr & (SUNSCPAL_ICR_BUS_ERROR)) { 358 char buffer[64]; 359 bitmask_snprintf(icr, SUNSCPAL_ICR_BITS, buffer, 360 sizeof(buffer)); 361 printf("sc: DMA error, icr=%s, reset\n", buffer); 362 sr->sr_xs->error = XS_DRIVER_STUFFUP; 363 sc->sc_state |= SUNSCPAL_ABORTING; 364 goto out; 365 } 366 367 /* Note that timeout may have set the error flag. */ 368 if (sc->sc_state & SUNSCPAL_ABORTING) 369 goto out; 370 371 /* XXX: Wait for DMA to actually finish? */ 372 373 /* 374 * Now try to figure out how much actually transferred 375 */ 376 377 resid = SUNSCPAL_DMA_COUNT_FLIP(SUNSCPAL_READ_2(sc, 378 sunscpal_dma_count)); 379 ntrans = sc->sc_reqlen - resid; 380 381 #ifdef SUNSCPAL_DEBUG 382 if (sunscpal_debug & SUNSCPAL_DBG_DMA) { 383 printf("%s: resid=0x%x ntrans=0x%x\n", 384 __func__, resid, ntrans); 385 } 386 #endif 387 388 if (ntrans < sc->sc_min_dma_len) { 389 printf("sc: DMA count: 0x%x\n", resid); 390 sc->sc_state |= SUNSCPAL_ABORTING; 391 goto out; 392 } 393 if (ntrans > sc->sc_datalen) 394 panic("%s: excess transfer", __func__); 395 396 /* Adjust data pointer */ 397 sc->sc_dataptr += ntrans; 398 sc->sc_datalen -= ntrans; 399 400 /* 401 * After a read, we may need to clean-up 402 * "Left-over bytes" (yuck!) 403 */ 404 if (((xs->xs_control & XS_CTL_DATA_OUT) == 0) && 405 ((icr & SUNSCPAL_ICR_ODD_LENGTH) != 0)) { 406 #ifdef DEBUG 407 printf("sc: Got Left-over bytes!\n"); 408 #endif 409 *(sc->sc_dataptr++) = SUNSCPAL_READ_1(sc, sunscpal_data); 410 sc->sc_datalen--; 411 } 412 413 out: 414 SUNSCPAL_WRITE_2(sc, sunscpal_dma_count, SUNSCPAL_DMA_COUNT_FLIP(0)); 415 416 } 417 418 /* Ask the target for a MSG_OUT phase. */ 419 static inline void 420 sunscpal_sched_msgout(struct sunscpal_softc *sc, int msg_code) 421 { 422 /* 423 * This controller does not allow you to assert ATN, which 424 * will eventually leave us with no option other than to reset 425 * the bus. We keep this function as a placeholder, though, 426 * and this printf will eventually go away or get #ifdef'ed: 427 */ 428 printf("%s: trying to schedule 0x%0x\n", __func__, msg_code); 429 sc->sc_msgpriq |= msg_code; 430 } 431 432 int 433 sunscpal_pio_out(struct sunscpal_softc *sc, int phase, int count, uint8_t *data) 434 { 435 int resid; 436 437 resid = count; 438 while (resid > 0) { 439 if (!SUNSCPAL_BUSY(sc)) { 440 SUNSCPAL_TRACE("pio_out: lost BSY, resid=%d\n", resid); 441 break; 442 } 443 if (sunscpal_wait_req(sc)) { 444 SUNSCPAL_TRACE("pio_out: no REQ, resid=%d\n", resid); 445 break; 446 } 447 if (SUNSCPAL_BUS_PHASE(SUNSCPAL_READ_2(sc, sunscpal_icr)) != 448 phase) 449 break; 450 451 /* Put the data on the bus. */ 452 if (data) { 453 SUNSCPAL_BYTE_WRITE(sc, phase, *data++); 454 } else { 455 SUNSCPAL_BYTE_WRITE(sc, phase, 0); 456 } 457 458 --resid; 459 } 460 461 return count - resid; 462 } 463 464 465 int 466 sunscpal_pio_in(struct sunscpal_softc *sc, int phase, int count, uint8_t *data) 467 { 468 int resid; 469 470 resid = count; 471 while (resid > 0) { 472 if (!SUNSCPAL_BUSY(sc)) { 473 SUNSCPAL_TRACE("pio_in: lost BSY, resid=%d\n", resid); 474 break; 475 } 476 if (sunscpal_wait_req(sc)) { 477 SUNSCPAL_TRACE("pio_in: no REQ, resid=%d\n", resid); 478 break; 479 } 480 /* A phase change is not valid until AFTER REQ rises! */ 481 if (SUNSCPAL_BUS_PHASE(SUNSCPAL_READ_2(sc, sunscpal_icr)) != 482 phase) 483 break; 484 485 /* Read the data bus. */ 486 if (data) 487 *data++ = SUNSCPAL_BYTE_READ(sc, phase); 488 else 489 (void)SUNSCPAL_BYTE_READ(sc, phase); 490 491 --resid; 492 } 493 494 return count - resid; 495 } 496 497 498 void 499 sunscpal_init(struct sunscpal_softc *sc) 500 { 501 int i, j; 502 503 #ifdef SUNSCPAL_DEBUG 504 sunscpal_debug_sc = sc; 505 #endif 506 507 for (i = 0; i < SUNSCPAL_OPENINGS; i++) 508 sc->sc_ring[i].sr_xs = NULL; 509 for (i = 0; i < 8; i++) 510 for (j = 0; j < 8; j++) 511 sc->sc_matrix[i][j] = NULL; 512 513 sc->sc_prevphase = SUNSCPAL_PHASE_INVALID; 514 sc->sc_state = SUNSCPAL_IDLE; 515 516 SUNSCPAL_WRITE_2(sc, sunscpal_icr, 0); 517 SUNSCPAL_WRITE_2(sc, sunscpal_dma_addr_h, 0); 518 SUNSCPAL_WRITE_2(sc, sunscpal_dma_addr_l, 0); 519 SUNSCPAL_WRITE_2(sc, sunscpal_dma_count, SUNSCPAL_DMA_COUNT_FLIP(0)); 520 521 SUNSCPAL_CLR_INTR(sc); 522 523 /* Another hack (Er.. hook!) for anything that needs it: */ 524 if (sc->sc_intr_on) { 525 SUNSCPAL_TRACE("init: intr ON\n", 0); 526 sc->sc_intr_on(sc); 527 } 528 } 529 530 531 static void 532 sunscpal_reset_scsibus(struct sunscpal_softc *sc) 533 { 534 535 SUNSCPAL_TRACE("reset_scsibus, cur=0x%x\n", (long)sc->sc_current); 536 537 SUNSCPAL_WRITE_2(sc, sunscpal_icr, SUNSCPAL_ICR_RESET); 538 delay(500); 539 SUNSCPAL_WRITE_2(sc, sunscpal_icr, 0); 540 541 SUNSCPAL_CLR_INTR(sc); 542 /* XXX - Need long delay here! */ 543 delay(100000); 544 545 /* XXX - Need to cancel disconnected requests. */ 546 } 547 548 549 /* 550 * Interrupt handler for the SCSI Bus Controller (SBC) 551 * This may also called for a DMA timeout (at splbio). 552 */ 553 int 554 sunscpal_intr(void *arg) 555 { 556 struct sunscpal_softc *sc = arg; 557 int claimed = 0; 558 559 /* 560 * Do not touch SBC regs here unless sc_current == NULL 561 * or it will complain about "register conflict" errors. 562 * Instead, just let sunscpal_machine() deal with it. 563 */ 564 SUNSCPAL_TRACE("intr: top, state=%d\n", sc->sc_state); 565 566 if (sc->sc_state == SUNSCPAL_IDLE) { 567 /* 568 * Might be reselect. sunscpal_reselect() will check, 569 * and set up the connection if so. This will verify 570 * that sc_current == NULL at the beginning... 571 */ 572 573 /* Another hack (Er.. hook!) for anything that needs it: */ 574 if (sc->sc_intr_off) { 575 SUNSCPAL_TRACE("intr: for reselect, intr off\n", 0); 576 sc->sc_intr_off(sc); 577 } 578 579 sunscpal_reselect(sc); 580 } 581 582 /* 583 * The remaining documented interrupt causes are a DMA complete 584 * condition. 585 * 586 * The procedure is to let sunscpal_machine() figure out what 587 * to do next. 588 */ 589 if (sc->sc_state & SUNSCPAL_WORKING) { 590 SUNSCPAL_TRACE("intr: call machine, cur=0x%x\n", 591 (long)sc->sc_current); 592 /* This will usually free-up the nexus. */ 593 sunscpal_machine(sc); 594 SUNSCPAL_TRACE("intr: machine done, cur=0x%x\n", 595 (long)sc->sc_current); 596 claimed = 1; 597 } 598 599 /* Maybe we can run some commands now... */ 600 if (sc->sc_state == SUNSCPAL_IDLE) { 601 SUNSCPAL_TRACE("intr: call sched, cur=0x%x\n", 602 (long)sc->sc_current); 603 sunscpal_sched(sc); 604 SUNSCPAL_TRACE("intr: sched done, cur=0x%x\n", 605 (long)sc->sc_current); 606 } 607 608 return claimed; 609 } 610 611 612 /* 613 * Abort the current command (i.e. due to timeout) 614 */ 615 void 616 sunscpal_abort(struct sunscpal_softc *sc) 617 { 618 619 /* 620 * Finish it now. If DMA is in progress, we 621 * can not call sunscpal_sched_msgout() because 622 * that hits the SBC (avoid DMA conflict). 623 */ 624 625 /* Another hack (Er.. hook!) for anything that needs it: */ 626 if (sc->sc_intr_off) { 627 SUNSCPAL_TRACE("abort: intr off\n", 0); 628 sc->sc_intr_off(sc); 629 } 630 631 sc->sc_state |= SUNSCPAL_ABORTING; 632 if ((sc->sc_state & SUNSCPAL_DOINGDMA) == 0) { 633 sunscpal_sched_msgout(sc, SEND_ABORT); 634 } 635 SUNSCPAL_TRACE("abort: call machine, cur=0x%x\n", 636 (long)sc->sc_current); 637 sunscpal_machine(sc); 638 SUNSCPAL_TRACE("abort: machine done, cur=0x%x\n", 639 (long)sc->sc_current); 640 641 /* Another hack (Er.. hook!) for anything that needs it: */ 642 if (sc->sc_intr_on) { 643 SUNSCPAL_TRACE("abort: intr ON\n", 0); 644 sc->sc_intr_on(sc); 645 } 646 } 647 648 /* 649 * Timeout handler, scheduled for each SCSI command. 650 */ 651 void 652 sunscpal_cmd_timeout(void *arg) 653 { 654 struct sunscpal_req *sr = arg; 655 struct scsipi_xfer *xs; 656 struct scsipi_periph *periph; 657 struct sunscpal_softc *sc; 658 int s; 659 660 s = splbio(); 661 662 /* Get all our variables... */ 663 xs = sr->sr_xs; 664 if (xs == NULL) { 665 printf("%s: no scsipi_xfer\n", __func__); 666 goto out; 667 } 668 periph = xs->xs_periph; 669 sc = device_private(periph->periph_channel->chan_adapter->adapt_dev); 670 671 printf("%s: cmd timeout, targ=%d, lun=%d\n", 672 device_xname(sc->sc_dev), 673 sr->sr_target, sr->sr_lun); 674 675 /* 676 * Mark the overdue job as failed, and arrange for 677 * sunscpal_machine to terminate it. If the victim 678 * is the current job, call sunscpal_machine() now. 679 * Otherwise arrange for sunscpal_sched() to do it. 680 */ 681 sr->sr_flags |= SR_OVERDUE; 682 if (sc->sc_current == sr) { 683 SUNSCPAL_TRACE("cmd_tmo: call abort, sr=0x%x\n", (long)sr); 684 sunscpal_abort(sc); 685 } else { 686 /* 687 * The driver may be idle, or busy with another job. 688 * Arrange for sunscpal_sched() to do the deed. 689 */ 690 SUNSCPAL_TRACE("cmd_tmo: clear matrix, t/l=0x%02x\n", 691 (sr->sr_target << 4) | sr->sr_lun); 692 sc->sc_matrix[sr->sr_target][sr->sr_lun] = NULL; 693 } 694 695 /* 696 * We may have aborted the current job, or may have 697 * already been idle. In either case, we should now 698 * be idle, so try to start another job. 699 */ 700 if (sc->sc_state == SUNSCPAL_IDLE) { 701 SUNSCPAL_TRACE("cmd_tmo: call sched, cur=0x%x\n", 702 (long)sc->sc_current); 703 sunscpal_sched(sc); 704 SUNSCPAL_TRACE("cmd_tmo: sched done, cur=0x%x\n", 705 (long)sc->sc_current); 706 } 707 708 out: 709 splx(s); 710 } 711 712 713 /***************************************************************** 714 * Interface to higher level 715 *****************************************************************/ 716 717 718 /* 719 * Enter a new SCSI command into the "issue" queue, and 720 * if there is work to do, start it going. 721 * 722 * WARNING: This can be called recursively! 723 * (see comment in sunscpal_done) 724 */ 725 void 726 sunscpal_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req, 727 void *arg) 728 { 729 struct scsipi_xfer *xs; 730 struct scsipi_periph *periph; 731 struct sunscpal_softc *sc; 732 struct sunscpal_req *sr; 733 int s, i, flags; 734 735 sc = device_private(chan->chan_adapter->adapt_dev); 736 737 switch (req) { 738 case ADAPTER_REQ_RUN_XFER: 739 xs = arg; 740 periph = xs->xs_periph; 741 flags = xs->xs_control; 742 743 if (flags & XS_CTL_DATA_UIO) 744 panic("sunscpal: scsi data uio requested"); 745 746 s = splbio(); 747 748 if (flags & XS_CTL_POLL) { 749 /* Terminate any current command. */ 750 sr = sc->sc_current; 751 if (sr != NULL) { 752 printf("%s: polled request aborting %d/%d\n", 753 device_xname(sc->sc_dev), sr->sr_target, 754 sr->sr_lun); 755 sunscpal_abort(sc); 756 } 757 if (sc->sc_state != SUNSCPAL_IDLE) { 758 panic("%s: polled request, abort failed", 759 __func__); 760 } 761 } 762 763 /* 764 * Find lowest empty slot in ring buffer. 765 * XXX: What about "fairness" and cmd order? 766 */ 767 for (i = 0; i < SUNSCPAL_OPENINGS; i++) 768 if (sc->sc_ring[i].sr_xs == NULL) 769 goto new; 770 771 xs->error = XS_RESOURCE_SHORTAGE; 772 SUNSCPAL_TRACE("scsipi_cmd: no openings, rv=%d\n", rv); 773 goto out; 774 775 new: 776 /* Create queue entry */ 777 sr = &sc->sc_ring[i]; 778 sr->sr_xs = xs; 779 sr->sr_target = xs->xs_periph->periph_target; 780 sr->sr_lun = xs->xs_periph->periph_lun; 781 sr->sr_dma_hand = NULL; 782 sr->sr_dataptr = xs->data; 783 sr->sr_datalen = xs->datalen; 784 sr->sr_flags = (flags & XS_CTL_POLL) ? SR_IMMED : 0; 785 sr->sr_status = -1; /* no value */ 786 sc->sc_ncmds++; 787 788 SUNSCPAL_TRACE("scsipi_cmd: new sr=0x%x\n", (long)sr); 789 790 if (flags & XS_CTL_POLL) { 791 /* Force this new command to be next. */ 792 sc->sc_rr = i; 793 } 794 795 /* 796 * If we were idle, run some commands... 797 */ 798 if (sc->sc_state == SUNSCPAL_IDLE) { 799 SUNSCPAL_TRACE("scsipi_cmd: call sched, cur=0x%x\n", 800 (long)sc->sc_current); 801 sunscpal_sched(sc); 802 SUNSCPAL_TRACE("scsipi_cmd: sched done, cur=0x%x\n", 803 (long)sc->sc_current); 804 } 805 806 if (flags & XS_CTL_POLL) { 807 /* Make sure sunscpal_sched() finished it. */ 808 if ((xs->xs_status & XS_STS_DONE) == 0) 809 panic("%s: poll didn't finish", __func__); 810 } 811 812 out: 813 splx(s); 814 return; 815 816 case ADAPTER_REQ_GROW_RESOURCES: 817 /* XXX Not supported. */ 818 return; 819 820 case ADAPTER_REQ_SET_XFER_MODE: 821 { 822 /* 823 * We don't support Sync, Wide, or Tagged Queueing. 824 * Just callback now, to report this. 825 */ 826 struct scsipi_xfer_mode *xm = arg; 827 828 xm->xm_mode = 0; 829 xm->xm_period = 0; 830 xm->xm_offset = 0; 831 scsipi_async_event(chan, ASYNC_EVENT_XFER_MODE, xm); 832 return; 833 } 834 } 835 } 836 837 838 /* 839 * POST PROCESSING OF SCSI_CMD (usually current) 840 * Called by sunscpal_sched(), sunscpal_machine() 841 */ 842 static void 843 sunscpal_done(struct sunscpal_softc *sc) 844 { 845 struct sunscpal_req *sr; 846 struct scsipi_xfer *xs; 847 848 #ifdef DIAGNOSTIC 849 if (sc->sc_state == SUNSCPAL_IDLE) 850 panic("%s: state=idle", __func__); 851 if (sc->sc_current == NULL) 852 panic("%s: current=0", __func__); 853 #endif 854 855 sr = sc->sc_current; 856 xs = sr->sr_xs; 857 858 SUNSCPAL_TRACE("done: top, cur=0x%x\n", (long)sc->sc_current); 859 860 /* 861 * Clean up DMA resources for this command. 862 */ 863 if (sr->sr_dma_hand) { 864 SUNSCPAL_TRACE("done: dma_free, dh=0x%x\n", 865 (long)sr->sr_dma_hand); 866 sunscpal_dma_free(sc); 867 } 868 #ifdef DIAGNOSTIC 869 if (sr->sr_dma_hand) 870 panic("%s: DMA free did not", __func__); 871 #endif 872 873 if (sc->sc_state & SUNSCPAL_ABORTING) { 874 SUNSCPAL_TRACE("done: aborting, error=%d\n", xs->error); 875 if (xs->error == XS_NOERROR) 876 xs->error = XS_TIMEOUT; 877 } 878 879 SUNSCPAL_TRACE("done: check error=%d\n", (long)xs->error); 880 881 /* If error is already set, ignore sr_status value. */ 882 if (xs->error != XS_NOERROR) 883 goto finish; 884 885 SUNSCPAL_TRACE("done: check status=%d\n", sr->sr_status); 886 887 xs->status = sr->sr_status; 888 switch (sr->sr_status) { 889 case SCSI_OK: /* 0 */ 890 break; 891 892 case SCSI_CHECK: 893 case SCSI_BUSY: 894 xs->error = XS_BUSY; 895 break; 896 897 case -1: 898 /* This is our "impossible" initial value. */ 899 /* fallthrough */ 900 default: 901 printf("%s: target %d, bad status=%d\n", 902 device_xname(sc->sc_dev), sr->sr_target, sr->sr_status); 903 xs->error = XS_DRIVER_STUFFUP; 904 break; 905 } 906 907 finish: 908 909 SUNSCPAL_TRACE("done: finish, error=%d\n", xs->error); 910 911 /* 912 * Dequeue the finished command, but don't clear sc_state until 913 * after the call to scsipi_done(), because that may call back to 914 * sunscpal_scsi_cmd() - unwanted recursion! 915 * 916 * Keeping sc->sc_state != idle terminates the recursion. 917 */ 918 #ifdef DIAGNOSTIC 919 if ((sc->sc_state & SUNSCPAL_WORKING) == 0) 920 panic("%s: bad state", __func__); 921 #endif 922 923 /* Clear our pointers to the request. */ 924 sc->sc_current = NULL; 925 sc->sc_matrix[sr->sr_target][sr->sr_lun] = NULL; 926 callout_stop(&sr->sr_xs->xs_callout); 927 928 /* Make the request free. */ 929 sr->sr_xs = NULL; 930 sc->sc_ncmds--; 931 932 /* Tell common SCSI code it is done. */ 933 scsipi_done(xs); 934 935 sc->sc_state = SUNSCPAL_IDLE; 936 /* Now sunscpal_sched() may be called again. */ 937 } 938 939 940 /* 941 * Schedule a SCSI operation. This routine should return 942 * only after it achieves one of the following conditions: 943 * Busy (sc->sc_state != SUNSCPAL_IDLE) 944 * No more work can be started. 945 */ 946 static void 947 sunscpal_sched(struct sunscpal_softc *sc) 948 { 949 struct sunscpal_req *sr; 950 struct scsipi_xfer *xs; 951 int target = 0, lun = 0; 952 int error, i; 953 954 /* Another hack (Er.. hook!) for anything that needs it: */ 955 if (sc->sc_intr_off) { 956 SUNSCPAL_TRACE("sched: top, intr off\n", 0); 957 sc->sc_intr_off(sc); 958 } 959 960 next_job: 961 /* 962 * Grab the next job from queue. Must be idle. 963 */ 964 #ifdef DIAGNOSTIC 965 if (sc->sc_state != SUNSCPAL_IDLE) 966 panic("%s: not idle", __func__); 967 if (sc->sc_current) 968 panic("%s: current set", __func__); 969 #endif 970 971 /* 972 * Always start the search where we last looked. 973 */ 974 i = sc->sc_rr; 975 sr = NULL; 976 do { 977 if (sc->sc_ring[i].sr_xs) { 978 target = sc->sc_ring[i].sr_target; 979 lun = sc->sc_ring[i].sr_lun; 980 if (sc->sc_matrix[target][lun] == NULL) { 981 /* 982 * Do not mark the target/LUN busy yet, 983 * because reselect may cause some other 984 * job to become the current one, so we 985 * might not actually start this job. 986 * Instead, set sc_matrix later on. 987 */ 988 sc->sc_rr = i; 989 sr = &sc->sc_ring[i]; 990 break; 991 } 992 } 993 i++; 994 if (i == SUNSCPAL_OPENINGS) 995 i = 0; 996 } while (i != sc->sc_rr); 997 998 if (sr == NULL) { 999 SUNSCPAL_TRACE("sched: no work, cur=0x%x\n", 1000 (long)sc->sc_current); 1001 1002 /* Another hack (Er.. hook!) for anything that needs it: */ 1003 if (sc->sc_intr_on) { 1004 SUNSCPAL_TRACE("sched: ret, intr ON\n", 0); 1005 sc->sc_intr_on(sc); 1006 } 1007 1008 return; /* No more work to do. */ 1009 } 1010 1011 SUNSCPAL_TRACE("sched: select for t/l=0x%02x\n", 1012 (sr->sr_target << 4) | sr->sr_lun); 1013 1014 sc->sc_state = SUNSCPAL_WORKING; 1015 error = sunscpal_select(sc, sr); 1016 if (sc->sc_current) { 1017 /* Lost the race! reselected out from under us! */ 1018 /* Work with the reselected job. */ 1019 if (sr->sr_flags & SR_IMMED) { 1020 printf("%s: reselected while polling (abort)\n", 1021 device_xname(sc->sc_dev)); 1022 /* Abort the reselected job. */ 1023 sc->sc_state |= SUNSCPAL_ABORTING; 1024 sc->sc_msgpriq |= SEND_ABORT; 1025 } 1026 sr = sc->sc_current; 1027 xs = sr->sr_xs; 1028 SUNSCPAL_TRACE("sched: reselect, new sr=0x%x\n", (long)sr); 1029 goto have_nexus; 1030 } 1031 1032 /* Normal selection result. Target/LUN is now busy. */ 1033 sc->sc_matrix[target][lun] = sr; 1034 sc->sc_current = sr; /* connected */ 1035 xs = sr->sr_xs; 1036 1037 /* 1038 * Initialize pointers, etc. for this job 1039 */ 1040 sc->sc_dataptr = sr->sr_dataptr; 1041 sc->sc_datalen = sr->sr_datalen; 1042 sc->sc_prevphase = SUNSCPAL_PHASE_INVALID; 1043 sc->sc_msgpriq = SEND_IDENTIFY; 1044 sc->sc_msgoutq = 0; 1045 sc->sc_msgout = 0; 1046 1047 SUNSCPAL_TRACE("sched: select rv=%d\n", error); 1048 1049 switch (error) { 1050 case XS_NOERROR: 1051 break; 1052 1053 case XS_BUSY: 1054 /* XXX - Reset and try again. */ 1055 printf("%s: select found SCSI bus busy, resetting...\n", 1056 device_xname(sc->sc_dev)); 1057 sunscpal_reset_scsibus(sc); 1058 /* fallthrough */ 1059 case XS_SELTIMEOUT: 1060 default: 1061 xs->error = error; /* from select */ 1062 SUNSCPAL_TRACE("sched: call done, sr=0x%x\n", (long)sr); 1063 sunscpal_done(sc); 1064 1065 /* Paranoia: clear everything. */ 1066 sc->sc_dataptr = NULL; 1067 sc->sc_datalen = 0; 1068 sc->sc_prevphase = SUNSCPAL_PHASE_INVALID; 1069 sc->sc_msgpriq = 0; 1070 sc->sc_msgoutq = 0; 1071 sc->sc_msgout = 0; 1072 1073 goto next_job; 1074 } 1075 1076 /* 1077 * Selection was successful. Normally, this means 1078 * we are starting a new command. However, this 1079 * might be the termination of an overdue job. 1080 */ 1081 if (sr->sr_flags & SR_OVERDUE) { 1082 SUNSCPAL_TRACE("sched: overdue, sr=0x%x\n", (long)sr); 1083 sc->sc_state |= SUNSCPAL_ABORTING; 1084 sc->sc_msgpriq |= SEND_ABORT; 1085 goto have_nexus; 1086 } 1087 1088 /* 1089 * OK, we are starting a new command. 1090 * Initialize and allocate resources for the new command. 1091 * Device reset is special (only uses MSG_OUT phase). 1092 * Normal commands start in MSG_OUT phase where we will 1093 * send and IDENDIFY message, and then expect CMD phase. 1094 */ 1095 #ifdef SUNSCPAL_DEBUG 1096 if (sunscpal_debug & SUNSCPAL_DBG_CMDS) { 1097 printf("%s: begin, target=%d, LUN=%d\n", __func__, 1098 xs->xs_periph->periph_target, xs->xs_periph->periph_lun); 1099 sunscpal_show_scsi_cmd(xs); 1100 } 1101 #endif 1102 if (xs->xs_control & XS_CTL_RESET) { 1103 SUNSCPAL_TRACE("sched: cmd=reset, sr=0x%x\n", (long)sr); 1104 /* Not an error, so do not set SUNSCPAL_ABORTING */ 1105 sc->sc_msgpriq |= SEND_DEV_RESET; 1106 goto have_nexus; 1107 } 1108 1109 #ifdef DIAGNOSTIC 1110 if ((xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) == 0) { 1111 if (sc->sc_dataptr) { 1112 printf("%s: ptr but no data in/out flags?\n", 1113 device_xname(sc->sc_dev)); 1114 SUNSCPAL_BREAK(); 1115 sc->sc_dataptr = NULL; 1116 } 1117 } 1118 #endif 1119 1120 /* Allocate DMA space (maybe) */ 1121 if (sc->sc_dataptr && (sc->sc_flags & SUNSCPAL_DISABLE_DMA) == 0 && 1122 (sc->sc_datalen >= sc->sc_min_dma_len)) 1123 { 1124 SUNSCPAL_TRACE("sched: dma_alloc, len=%d\n", sc->sc_datalen); 1125 sunscpal_dma_alloc(sc); 1126 } 1127 1128 /* 1129 * Initialization hook called just after select, 1130 * at the beginning of COMMAND phase. 1131 * (but AFTER the DMA allocation is done) 1132 * 1133 * We need to set up the DMA engine BEFORE the target puts 1134 * the SCSI bus into any DATA phase. 1135 */ 1136 if (sr->sr_dma_hand) { 1137 SUNSCPAL_TRACE("sched: dma_setup, dh=0x%x\n", 1138 (long) sr->sr_dma_hand); 1139 sunscpal_dma_setup(sc); 1140 } 1141 1142 /* 1143 * Schedule a timeout for the job we are starting. 1144 */ 1145 if ((sr->sr_flags & SR_IMMED) == 0) { 1146 i = mstohz(xs->timeout); 1147 SUNSCPAL_TRACE("sched: set timeout=%d\n", i); 1148 callout_reset(&sr->sr_xs->xs_callout, i, 1149 sunscpal_cmd_timeout, sr); 1150 } 1151 1152 have_nexus: 1153 1154 SUNSCPAL_TRACE("sched: call machine, cur=0x%x\n", 1155 (long)sc->sc_current); 1156 sunscpal_machine(sc); 1157 SUNSCPAL_TRACE("sched: machine done, cur=0x%x\n", 1158 (long)sc->sc_current); 1159 1160 /* 1161 * What state did sunscpal_machine() leave us in? 1162 * Hopefully it sometimes completes a job... 1163 */ 1164 if (sc->sc_state == SUNSCPAL_IDLE) 1165 goto next_job; 1166 1167 return; /* Have work in progress. */ 1168 } 1169 1170 1171 /* 1172 * Reselect handler: checks for reselection, and if we are being 1173 * reselected, it sets up sc->sc_current. 1174 * 1175 * We are reselected when: 1176 * SEL is TRUE 1177 * IO is TRUE 1178 * BSY is FALSE 1179 */ 1180 void 1181 sunscpal_reselect(struct sunscpal_softc *sc) 1182 { 1183 1184 /* 1185 * This controller does not implement disconnect/reselect, so 1186 * we really don't have anything to do here. We keep this 1187 * function as a placeholder, though. 1188 */ 1189 } 1190 1191 /* 1192 * Select target: xs is the transfer that we are selecting for. 1193 * sc->sc_current should be NULL. 1194 * 1195 * Returns: 1196 * sc->sc_current != NULL ==> we were reselected (race!) 1197 * XS_NOERROR ==> selection worked 1198 * XS_BUSY ==> lost arbitration 1199 * XS_SELTIMEOUT ==> no response to selection 1200 */ 1201 static int 1202 sunscpal_select(struct sunscpal_softc *sc, struct sunscpal_req *sr) 1203 { 1204 int timo, target_mask; 1205 u_short mode; 1206 1207 /* Check for reselect */ 1208 sunscpal_reselect(sc); 1209 if (sc->sc_current) { 1210 SUNSCPAL_TRACE("select: reselect, cur=0x%x\n", 1211 (long)sc->sc_current); 1212 return XS_BUSY; /* reselected */ 1213 } 1214 1215 /* 1216 * Select the target. 1217 */ 1218 target_mask = (1 << sr->sr_target); 1219 SUNSCPAL_WRITE_1(sc, sunscpal_data, target_mask); 1220 SUNSCPAL_WRITE_2(sc, sunscpal_icr, SUNSCPAL_ICR_SELECT); 1221 1222 /* 1223 * Wait for the target to assert BSY. 1224 * SCSI spec. says wait for 250 mS. 1225 */ 1226 for (timo = 25000;;) { 1227 if (SUNSCPAL_READ_2(sc, sunscpal_icr) & SUNSCPAL_ICR_BUSY) 1228 goto success; 1229 if (--timo <= 0) 1230 break; 1231 delay(10); 1232 } 1233 1234 SUNSCPAL_WRITE_1(sc, sunscpal_data, 0); 1235 SUNSCPAL_WRITE_2(sc, sunscpal_icr, 0); 1236 1237 SUNSCPAL_TRACE("select: device down, rc=%d\n", XS_SELTIMEOUT); 1238 return XS_SELTIMEOUT; 1239 1240 success: 1241 1242 /* 1243 * The target is now driving BSY, so we can stop 1244 * driving SEL and the data bus. We do set up 1245 * whether or not this target needs parity. 1246 */ 1247 mode = 0; 1248 if ((sc->sc_parity_disable & target_mask) == 0) 1249 mode |= SUNSCPAL_ICR_PARITY_ENABLE; 1250 SUNSCPAL_WRITE_2(sc, sunscpal_icr, mode); 1251 1252 return XS_NOERROR; 1253 } 1254 1255 /***************************************************************** 1256 * Functions to handle each info. transfer phase: 1257 *****************************************************************/ 1258 1259 /* 1260 * The message system: 1261 * 1262 * This is a revamped message system that now should easier accommodate 1263 * new messages, if necessary. 1264 * 1265 * Currently we accept these messages: 1266 * IDENTIFY (when reselecting) 1267 * COMMAND COMPLETE # (expect bus free after messages marked #) 1268 * NOOP 1269 * MESSAGE REJECT 1270 * SYNCHRONOUS DATA TRANSFER REQUEST 1271 * SAVE DATA POINTER 1272 * RESTORE POINTERS 1273 * DISCONNECT # 1274 * 1275 * We may send these messages in prioritized order: 1276 * BUS DEVICE RESET # if XS_CTL_RESET & xs->xs_control (or in 1277 * weird sits.) 1278 * MESSAGE PARITY ERROR par. err. during MSGI 1279 * MESSAGE REJECT If we get a message we don't know how to handle 1280 * ABORT # send on errors 1281 * INITIATOR DETECTED ERROR also on errors (SCSI2) (during info xfer) 1282 * IDENTIFY At the start of each transfer 1283 * SYNCHRONOUS DATA TRANSFER REQUEST if appropriate 1284 * NOOP if nothing else fits the bill ... 1285 */ 1286 1287 /* 1288 * Precondition: 1289 * The SCSI bus is already in the MSGI phase and there is a message byte 1290 * on the bus, along with an asserted REQ signal. 1291 * 1292 * Our return value determines whether our caller, sunscpal_machine() 1293 * will expect to see another REQ (and possibly phase change). 1294 */ 1295 static int 1296 sunscpal_msg_in(struct sunscpal_softc *sc) 1297 { 1298 struct sunscpal_req *sr = sc->sc_current; 1299 struct scsipi_xfer *xs = sr->sr_xs; 1300 int n, phase; 1301 int act_flags; 1302 1303 act_flags = ACT_CONTINUE; 1304 1305 if (sc->sc_prevphase == SUNSCPAL_PHASE_MSG_IN) { 1306 /* This is a continuation of the previous message. */ 1307 n = sc->sc_imp - sc->sc_imess; 1308 SUNSCPAL_TRACE("msg_in: continuation, n=%d\n", n); 1309 goto nextbyte; 1310 } 1311 1312 /* This is a new MESSAGE IN phase. Clean up our state. */ 1313 sc->sc_state &= ~SUNSCPAL_DROP_MSGIN; 1314 1315 nextmsg: 1316 n = 0; 1317 sc->sc_imp = &sc->sc_imess[n]; 1318 1319 nextbyte: 1320 /* 1321 * Read a whole message, but don't ack the last byte. If we reject the 1322 * message, we have to assert ATN during the message transfer phase 1323 * itself. 1324 */ 1325 for (;;) { 1326 /* 1327 * Read a message byte. 1328 * First, check BSY, REQ, phase... 1329 */ 1330 if (!SUNSCPAL_BUSY(sc)) { 1331 SUNSCPAL_TRACE("msg_in: lost BSY, n=%d\n", n); 1332 /* XXX - Assume the command completed? */ 1333 act_flags |= (ACT_DISCONNECT | ACT_CMD_DONE); 1334 return act_flags; 1335 } 1336 if (sunscpal_wait_req(sc)) { 1337 SUNSCPAL_TRACE("msg_in: BSY but no REQ, n=%d\n", n); 1338 /* Just let sunscpal_machine() handle it... */ 1339 return act_flags; 1340 } 1341 phase = SUNSCPAL_BUS_PHASE(SUNSCPAL_READ_2(sc, sunscpal_icr)); 1342 if (phase != SUNSCPAL_PHASE_MSG_IN) { 1343 /* 1344 * Target left MESSAGE IN, probably because it 1345 * a) noticed our ATN signal, or 1346 * b) ran out of messages. 1347 */ 1348 return act_flags; 1349 } 1350 /* Still in MESSAGE IN phase, and REQ is asserted. */ 1351 if ((SUNSCPAL_READ_2(sc, sunscpal_icr) & 1352 SUNSCPAL_ICR_PARITY_ERROR) != 0) { 1353 sunscpal_sched_msgout(sc, SEND_PARITY_ERROR); 1354 sc->sc_state |= SUNSCPAL_DROP_MSGIN; 1355 } 1356 1357 /* Gather incoming message bytes if needed. */ 1358 if ((sc->sc_state & SUNSCPAL_DROP_MSGIN) == 0) { 1359 if (n >= SUNSCPAL_MAX_MSG_LEN) { 1360 sunscpal_sched_msgout(sc, SEND_REJECT); 1361 sc->sc_state |= SUNSCPAL_DROP_MSGIN; 1362 } else { 1363 *sc->sc_imp++ = 1364 SUNSCPAL_READ_1(sc, sunscpal_cmd_stat); 1365 n++; 1366 /* 1367 * This testing is suboptimal, but most 1368 * messages will be of the one byte variety, so 1369 * it should not affect performance 1370 * significantly. 1371 */ 1372 if (n == 1 && MSG_IS1BYTE(sc->sc_imess[0])) 1373 goto have_msg; 1374 if (n == 2 && MSG_IS2BYTE(sc->sc_imess[0])) 1375 goto have_msg; 1376 if (n >= 3 && MSG_ISEXTENDED(sc->sc_imess[0]) && 1377 n == sc->sc_imess[1] + 2) 1378 goto have_msg; 1379 } 1380 } 1381 1382 /* 1383 * If we reach this spot we're either: 1384 * a) in the middle of a multi-byte message, or 1385 * b) dropping bytes. 1386 */ 1387 1388 if (act_flags != ACT_CONTINUE) 1389 return act_flags; 1390 1391 /* back to nextbyte */ 1392 } 1393 1394 have_msg: 1395 /* We now have a complete message. Parse it. */ 1396 1397 switch (sc->sc_imess[0]) { 1398 case MSG_CMDCOMPLETE: 1399 SUNSCPAL_TRACE("msg_in: CMDCOMPLETE\n", 0); 1400 /* Target is about to disconnect. */ 1401 act_flags |= (ACT_DISCONNECT | ACT_CMD_DONE); 1402 break; 1403 1404 case MSG_PARITY_ERROR: 1405 SUNSCPAL_TRACE("msg_in: PARITY_ERROR\n", 0); 1406 /* Resend the last message. */ 1407 sunscpal_sched_msgout(sc, sc->sc_msgout); 1408 break; 1409 1410 case MSG_MESSAGE_REJECT: 1411 /* The target rejects the last message we sent. */ 1412 SUNSCPAL_TRACE("msg_in: got reject for 0x%x\n", sc->sc_msgout); 1413 switch (sc->sc_msgout) { 1414 case SEND_IDENTIFY: 1415 /* Really old target controller? */ 1416 /* XXX ... */ 1417 break; 1418 case SEND_INIT_DET_ERR: 1419 goto abort; 1420 } 1421 break; 1422 1423 case MSG_NOOP: 1424 SUNSCPAL_TRACE("msg_in: NOOP\n", 0); 1425 break; 1426 1427 case MSG_DISCONNECT: 1428 SUNSCPAL_TRACE("msg_in: DISCONNECT\n", 0); 1429 /* Target is about to disconnect. */ 1430 act_flags |= ACT_DISCONNECT; 1431 if ((xs->xs_periph->periph_quirks & PQUIRK_AUTOSAVE) == 0) 1432 break; 1433 /*FALLTHROUGH*/ 1434 1435 case MSG_SAVEDATAPOINTER: 1436 SUNSCPAL_TRACE("msg_in: SAVE_PTRS\n", 0); 1437 sr->sr_dataptr = sc->sc_dataptr; 1438 sr->sr_datalen = sc->sc_datalen; 1439 break; 1440 1441 case MSG_RESTOREPOINTERS: 1442 SUNSCPAL_TRACE("msg_in: RESTORE_PTRS\n", 0); 1443 sc->sc_dataptr = sr->sr_dataptr; 1444 sc->sc_datalen = sr->sr_datalen; 1445 break; 1446 1447 case MSG_EXTENDED: 1448 switch (sc->sc_imess[2]) { 1449 case MSG_EXT_SDTR: 1450 case MSG_EXT_WDTR: 1451 /* This controller can not do synchronous mode. */ 1452 goto reject; 1453 default: 1454 printf("%s: unrecognized MESSAGE EXTENDED; " 1455 "sending REJECT\n", 1456 device_xname(sc->sc_dev)); 1457 SUNSCPAL_BREAK(); 1458 goto reject; 1459 } 1460 break; 1461 1462 default: 1463 SUNSCPAL_TRACE("msg_in: eh? imsg=0x%x\n", sc->sc_imess[0]); 1464 printf("%s: unrecognized MESSAGE; sending REJECT\n", 1465 device_xname(sc->sc_dev)); 1466 SUNSCPAL_BREAK(); 1467 /* FALLTHROUGH */ 1468 reject: 1469 sunscpal_sched_msgout(sc, SEND_REJECT); 1470 break; 1471 1472 abort: 1473 sc->sc_state |= SUNSCPAL_ABORTING; 1474 sunscpal_sched_msgout(sc, SEND_ABORT); 1475 break; 1476 } 1477 1478 /* Go get the next message, if any. */ 1479 if (act_flags == ACT_CONTINUE) 1480 goto nextmsg; 1481 1482 return act_flags; 1483 } 1484 1485 1486 /* 1487 * The message out (and in) stuff is a bit complicated: 1488 * If the target requests another message (sequence) without 1489 * having changed phase in between it really asks for a 1490 * retransmit, probably due to parity error(s). 1491 * The following messages can be sent: 1492 * IDENTIFY @ These 4 stem from SCSI command activity 1493 * SDTR @ 1494 * WDTR @ 1495 * DEV_RESET @ 1496 * REJECT if MSGI doesn't make sense 1497 * PARITY_ERROR if parity error while in MSGI 1498 * INIT_DET_ERR if parity error while not in MSGI 1499 * ABORT if INIT_DET_ERR rejected 1500 * NOOP if asked for a message and there's nothing to send 1501 * 1502 * Note that we call this one with (sc_current == NULL) 1503 * when sending ABORT for unwanted reselections. 1504 */ 1505 static int 1506 sunscpal_msg_out(struct sunscpal_softc *sc) 1507 { 1508 /* 1509 * This controller does not allow you to assert ATN, which 1510 * means we will never get the opportunity to send messages to 1511 * the target (the bus will never enter this MSG_OUT phase). 1512 * This will eventually leave us with no option other than to 1513 * reset the bus. We keep this function as a placeholder, 1514 * though, and this printf will eventually go away or get 1515 * #ifdef'ed: 1516 */ 1517 printf("%s: bus is in MSG_OUT phase?\n", __func__); 1518 return ACT_CONTINUE | ACT_RESET_BUS; 1519 } 1520 1521 /* 1522 * Handle command phase. 1523 */ 1524 static int 1525 sunscpal_command(struct sunscpal_softc *sc) 1526 { 1527 struct sunscpal_req *sr = sc->sc_current; 1528 struct scsipi_xfer *xs = sr->sr_xs; 1529 int len; 1530 1531 /* Assume command can be sent in one go. */ 1532 /* XXX: Do this using DMA, and get a phase change intr? */ 1533 len = sunscpal_pio_out(sc, SUNSCPAL_PHASE_COMMAND, xs->cmdlen, 1534 (uint8_t *)xs->cmd); 1535 1536 if (len != xs->cmdlen) { 1537 #ifdef SUNSCPAL_DEBUG 1538 printf("%s: short transfer: wanted %d got %d.\n", 1539 __func__, xs->cmdlen, len); 1540 sunscpal_show_scsi_cmd(xs); 1541 SUNSCPAL_BREAK(); 1542 #endif 1543 if (len < 6) { 1544 xs->error = XS_DRIVER_STUFFUP; 1545 sc->sc_state |= SUNSCPAL_ABORTING; 1546 sunscpal_sched_msgout(sc, SEND_ABORT); 1547 } 1548 1549 } 1550 1551 return ACT_CONTINUE; 1552 } 1553 1554 1555 /* 1556 * Handle either data_in or data_out 1557 */ 1558 static int 1559 sunscpal_data_xfer(struct sunscpal_softc *sc, int phase) 1560 { 1561 struct sunscpal_req *sr = sc->sc_current; 1562 struct scsipi_xfer *xs = sr->sr_xs; 1563 int expected_phase; 1564 int len; 1565 1566 /* 1567 * When aborting a command, disallow any data phase. 1568 */ 1569 if (sc->sc_state & SUNSCPAL_ABORTING) { 1570 printf("%s: aborting, bus phase=%s (reset)\n", 1571 device_xname(sc->sc_dev), phase_names[(phase >> 8) & 7]); 1572 return ACT_RESET_BUS; /* XXX */ 1573 } 1574 1575 /* Validate expected phase (data_in or data_out) */ 1576 expected_phase = (xs->xs_control & XS_CTL_DATA_OUT) ? 1577 SUNSCPAL_PHASE_DATA_OUT : SUNSCPAL_PHASE_DATA_IN; 1578 if (phase != expected_phase) { 1579 printf("%s: data phase error\n", device_xname(sc->sc_dev)); 1580 goto abort; 1581 } 1582 1583 /* Make sure we have some data to move. */ 1584 if (sc->sc_datalen <= 0) { 1585 /* Device needs padding. */ 1586 if (phase == SUNSCPAL_PHASE_DATA_IN) 1587 sunscpal_pio_in(sc, phase, 4096, NULL); 1588 else 1589 sunscpal_pio_out(sc, phase, 4096, NULL); 1590 /* Make sure that caused a phase change. */ 1591 if (SUNSCPAL_BUS_PHASE(SUNSCPAL_READ_2(sc, sunscpal_icr)) == 1592 phase) { 1593 /* More than 4k is just too much! */ 1594 printf("%s: too much data padding\n", 1595 device_xname(sc->sc_dev)); 1596 goto abort; 1597 } 1598 return ACT_CONTINUE; 1599 } 1600 1601 /* 1602 * Attempt DMA only if dma_alloc gave us a DMA handle AND 1603 * there is enough left to transfer so DMA is worth while. 1604 */ 1605 if (sr->sr_dma_hand && (sc->sc_datalen >= sc->sc_min_dma_len)) { 1606 /* 1607 * OK, really start DMA. Note, the MD start function 1608 * is responsible for setting the TCMD register, etc. 1609 * (Acknowledge the phase change there, not here.) 1610 */ 1611 SUNSCPAL_TRACE("data_xfer: dma_start, dh=0x%x\n", 1612 (long)sr->sr_dma_hand); 1613 sunscpal_dma_start(sc); 1614 return ACT_WAIT_DMA; 1615 } 1616 1617 /* 1618 * Doing PIO for data transfer. (Possibly "Pseudo DMA") 1619 * XXX: Do PDMA functions need to set tcmd later? 1620 */ 1621 SUNSCPAL_TRACE("data_xfer: doing PIO, len=%d\n", sc->sc_datalen); 1622 if (phase == SUNSCPAL_PHASE_DATA_OUT) { 1623 len = sunscpal_pio_out(sc, phase, 1624 sc->sc_datalen, sc->sc_dataptr); 1625 } else { 1626 len = sunscpal_pio_in(sc, phase, 1627 sc->sc_datalen, sc->sc_dataptr); 1628 } 1629 sc->sc_dataptr += len; 1630 sc->sc_datalen -= len; 1631 1632 SUNSCPAL_TRACE("data_xfer: did PIO, resid=%d\n", sc->sc_datalen); 1633 return ACT_CONTINUE; 1634 1635 abort: 1636 sc->sc_state |= SUNSCPAL_ABORTING; 1637 sunscpal_sched_msgout(sc, SEND_ABORT); 1638 return ACT_CONTINUE; 1639 } 1640 1641 1642 static int 1643 sunscpal_status(struct sunscpal_softc *sc) 1644 { 1645 int len; 1646 uint8_t status; 1647 struct sunscpal_req *sr = sc->sc_current; 1648 1649 len = sunscpal_pio_in(sc, SUNSCPAL_PHASE_STATUS, 1, &status); 1650 if (len) { 1651 sr->sr_status = status; 1652 } else { 1653 printf("%s: none?\n", __func__); 1654 } 1655 1656 return ACT_CONTINUE; 1657 } 1658 1659 1660 /* 1661 * This is the big state machine that follows SCSI phase changes. 1662 * This is somewhat like a co-routine. It will do a SCSI command, 1663 * and exit if the command is complete, or if it must wait, i.e. 1664 * for DMA to complete or for reselect to resume the job. 1665 * 1666 * The bus must be selected, and we need to know which command is 1667 * being undertaken. 1668 */ 1669 static void 1670 sunscpal_machine(struct sunscpal_softc *sc) 1671 { 1672 struct sunscpal_req *sr; 1673 struct scsipi_xfer *xs; 1674 int act_flags, phase, timo; 1675 1676 #ifdef DIAGNOSTIC 1677 if (sc->sc_state == SUNSCPAL_IDLE) 1678 panic("%s: state=idle", __func__); 1679 if (sc->sc_current == NULL) 1680 panic("%s: no current cmd", __func__); 1681 #endif 1682 1683 sr = sc->sc_current; 1684 xs = sr->sr_xs; 1685 act_flags = ACT_CONTINUE; 1686 1687 /* 1688 * This will be called by sunscpal_intr() when DMA is 1689 * complete. Must stop DMA before touching the PAL or 1690 * there will be "register conflict" errors. 1691 */ 1692 if ((sc->sc_state & SUNSCPAL_DOINGDMA) != 0) { 1693 /* Pick-up where where we left off... */ 1694 goto dma_done; 1695 } 1696 1697 next_phase: 1698 1699 if (!SUNSCPAL_BUSY(sc)) { 1700 /* Unexpected disconnect */ 1701 printf("%s: unexpected disconnect.\n", __func__); 1702 xs->error = XS_DRIVER_STUFFUP; 1703 act_flags |= (ACT_DISCONNECT | ACT_CMD_DONE); 1704 goto do_actions; 1705 } 1706 1707 /* 1708 * Wait for REQ before reading the phase. 1709 * Need to wait longer than usual here, because 1710 * some devices are just plain slow... 1711 */ 1712 timo = sunscpal_wait_phase_timo; 1713 for (;;) { 1714 if (SUNSCPAL_READ_2(sc, sunscpal_icr) & SUNSCPAL_ICR_REQUEST) 1715 break; 1716 if (--timo <= 0) { 1717 if (sc->sc_state & SUNSCPAL_ABORTING) { 1718 printf("%s: no REQ while aborting, reset\n", 1719 device_xname(sc->sc_dev)); 1720 act_flags |= ACT_RESET_BUS; 1721 goto do_actions; 1722 } 1723 printf("%s: no REQ for next phase, abort\n", 1724 device_xname(sc->sc_dev)); 1725 sc->sc_state |= SUNSCPAL_ABORTING; 1726 sunscpal_sched_msgout(sc, SEND_ABORT); 1727 goto next_phase; 1728 } 1729 delay(100); 1730 } 1731 1732 phase = SUNSCPAL_BUS_PHASE(SUNSCPAL_READ_2(sc, sunscpal_icr)); 1733 SUNSCPAL_TRACE("machine: phase=%s\n", 1734 (long)phase_names[(phase >> 8) & 7]); 1735 1736 /* 1737 * We assume that the device knows what it's doing, 1738 * so any phase is good. 1739 */ 1740 1741 switch (phase) { 1742 1743 case SUNSCPAL_PHASE_DATA_OUT: 1744 case SUNSCPAL_PHASE_DATA_IN: 1745 act_flags = sunscpal_data_xfer(sc, phase); 1746 break; 1747 1748 case SUNSCPAL_PHASE_COMMAND: 1749 act_flags = sunscpal_command(sc); 1750 break; 1751 1752 case SUNSCPAL_PHASE_STATUS: 1753 act_flags = sunscpal_status(sc); 1754 break; 1755 1756 case SUNSCPAL_PHASE_MSG_OUT: 1757 act_flags = sunscpal_msg_out(sc); 1758 break; 1759 1760 case SUNSCPAL_PHASE_MSG_IN: 1761 act_flags = sunscpal_msg_in(sc); 1762 break; 1763 1764 default: 1765 printf("%s: Unexpected phase 0x%x\n", __func__, phase); 1766 sc->sc_state |= SUNSCPAL_ABORTING; 1767 sunscpal_sched_msgout(sc, SEND_ABORT); 1768 goto next_phase; 1769 1770 } /* switch */ 1771 sc->sc_prevphase = phase; 1772 1773 do_actions: 1774 1775 if (act_flags & ACT_WAIT_DMA) { 1776 act_flags &= ~ACT_WAIT_DMA; 1777 /* Wait for DMA to complete (polling, or interrupt). */ 1778 if ((sr->sr_flags & SR_IMMED) == 0) { 1779 SUNSCPAL_TRACE("machine: wait for DMA intr.\n", 0); 1780 return; /* will resume at dma_done */ 1781 } 1782 /* Busy-wait for it to finish. */ 1783 SUNSCPAL_TRACE("machine: dma_poll, dh=0x%x\n", 1784 (long)sr->sr_dma_hand); 1785 sunscpal_dma_poll(sc); 1786 dma_done: 1787 /* Return here after interrupt. */ 1788 if (sr->sr_flags & SR_OVERDUE) 1789 sc->sc_state |= SUNSCPAL_ABORTING; 1790 SUNSCPAL_TRACE("machine: dma_stop, dh=0x%x\n", 1791 (long)sr->sr_dma_hand); 1792 sunscpal_dma_stop(sc); 1793 SUNSCPAL_CLR_INTR(sc); /* XXX */ 1794 /* 1795 * While DMA is running we can not touch the SBC, 1796 * so various places just set SUNSCPAL_ABORTING and 1797 * expect us the "kick it" when DMA is done. 1798 */ 1799 if (sc->sc_state & SUNSCPAL_ABORTING) { 1800 sunscpal_sched_msgout(sc, SEND_ABORT); 1801 } 1802 } 1803 1804 /* 1805 * Check for parity error. 1806 * XXX - better place to check? 1807 */ 1808 if (SUNSCPAL_READ_2(sc, sunscpal_icr) & SUNSCPAL_ICR_PARITY_ERROR) { 1809 printf("%s: parity error!\n", device_xname(sc->sc_dev)); 1810 /* XXX: sc->sc_state |= SUNSCPAL_ABORTING; */ 1811 sunscpal_sched_msgout(sc, SEND_PARITY_ERROR); 1812 } 1813 1814 if (act_flags == ACT_CONTINUE) 1815 goto next_phase; 1816 /* All other actions "break" from the loop. */ 1817 1818 SUNSCPAL_TRACE("machine: act_flags=0x%x\n", act_flags); 1819 1820 if (act_flags & ACT_RESET_BUS) { 1821 act_flags |= ACT_CMD_DONE; 1822 /* 1823 * Reset the SCSI bus, usually due to a timeout. 1824 * The error code XS_TIMEOUT allows retries. 1825 */ 1826 sc->sc_state |= SUNSCPAL_ABORTING; 1827 printf("%s: reset SCSI bus for TID=%d LUN=%d\n", 1828 device_xname(sc->sc_dev), sr->sr_target, sr->sr_lun); 1829 sunscpal_reset_scsibus(sc); 1830 } 1831 1832 if (act_flags & ACT_CMD_DONE) { 1833 act_flags |= ACT_DISCONNECT; 1834 /* Need to call scsipi_done() */ 1835 /* XXX: from the aic6360 driver, but why? */ 1836 if (sc->sc_datalen < 0) { 1837 printf("%s: %d extra bytes from %d:%d\n", 1838 device_xname(sc->sc_dev), -sc->sc_datalen, 1839 sr->sr_target, sr->sr_lun); 1840 sc->sc_datalen = 0; 1841 } 1842 xs->resid = sc->sc_datalen; 1843 /* Note: this will clear sc_current */ 1844 SUNSCPAL_TRACE("machine: call done, cur=0x%x\n", (long)sr); 1845 sunscpal_done(sc); 1846 } 1847 1848 if (act_flags & ACT_DISCONNECT) { 1849 /* 1850 * The device has dropped BSY (or will soon). 1851 * We have to wait here for BSY to drop, otherwise 1852 * the next command may decide we need a bus reset. 1853 */ 1854 timo = sunscpal_wait_req_timo; /* XXX */ 1855 for (;;) { 1856 if (!SUNSCPAL_BUSY(sc)) 1857 goto busfree; 1858 if (--timo <= 0) 1859 break; 1860 delay(2); 1861 } 1862 /* Device is sitting on the bus! */ 1863 printf("%s: Target %d LUN %d stuck busy, resetting...\n", 1864 device_xname(sc->sc_dev), sr->sr_target, sr->sr_lun); 1865 sunscpal_reset_scsibus(sc); 1866 busfree: 1867 SUNSCPAL_TRACE("machine: discon, waited %d\n", 1868 sunscpal_wait_req_timo - timo); 1869 1870 SUNSCPAL_WRITE_2(sc, sunscpal_icr, 0); 1871 1872 if ((act_flags & ACT_CMD_DONE) == 0) { 1873 SUNSCPAL_TRACE("machine: discon, cur=0x%x\n", (long)sr); 1874 } 1875 1876 /* 1877 * We may be here due to a disconnect message, 1878 * in which case we did NOT call sunscpal_done, 1879 * and we need to clear sc_current. 1880 */ 1881 sc->sc_state = SUNSCPAL_IDLE; 1882 sc->sc_current = NULL; 1883 1884 /* Paranoia: clear everything. */ 1885 sc->sc_dataptr = NULL; 1886 sc->sc_datalen = 0; 1887 sc->sc_prevphase = SUNSCPAL_PHASE_INVALID; 1888 sc->sc_msgpriq = 0; 1889 sc->sc_msgoutq = 0; 1890 sc->sc_msgout = 0; 1891 1892 /* Our caller will re-enable interrupts. */ 1893 } 1894 } 1895 1896 1897 #ifdef SUNSCPAL_DEBUG 1898 1899 static void 1900 sunscpal_show_scsi_cmd(struct scsipi_xfer *xs) 1901 { 1902 uint8_t *b = (uint8_t *)xs->cmd; 1903 int i = 0; 1904 1905 scsipi_printaddr(xs->xs_periph); 1906 if ((xs->xs_control & XS_CTL_RESET) == 0) { 1907 printf("-"); 1908 while (i < xs->cmdlen) { 1909 if (i != 0) 1910 printf(","); 1911 printf("%x", b[i++]); 1912 } 1913 printf("-\n"); 1914 } else { 1915 printf("-RESET-\n"); 1916 } 1917 } 1918 1919 1920 int sunscpal_traceidx = 0; 1921 1922 #define TRACE_MAX 1024 1923 struct trace_ent { 1924 char *msg; 1925 long val; 1926 } sunscpal_tracebuf[TRACE_MAX]; 1927 1928 void 1929 sunscpal_trace(char *msg, long val) 1930 { 1931 struct trace_ent *tr; 1932 int s; 1933 1934 s = splbio(); 1935 1936 tr = &sunscpal_tracebuf[sunscpal_traceidx]; 1937 1938 sunscpal_traceidx++; 1939 if (sunscpal_traceidx >= TRACE_MAX) 1940 sunscpal_traceidx = 0; 1941 1942 tr->msg = msg; 1943 tr->val = val; 1944 1945 splx(s); 1946 } 1947 1948 #ifdef DDB 1949 void 1950 sunscpal_clear_trace(void) 1951 { 1952 1953 sunscpal_traceidx = 0; 1954 memset((void *)sunscpal_tracebuf, 0, sizeof(sunscpal_tracebuf)); 1955 } 1956 1957 void 1958 sunscpal_show_trace(void) 1959 { 1960 struct trace_ent *tr; 1961 int idx; 1962 1963 idx = sunscpal_traceidx; 1964 do { 1965 tr = &sunscpal_tracebuf[idx]; 1966 idx++; 1967 if (idx >= TRACE_MAX) 1968 idx = 0; 1969 if (tr->msg) 1970 db_printf(tr->msg, tr->val); 1971 } while (idx != sunscpal_traceidx); 1972 } 1973 1974 void 1975 sunscpal_show_req(struct sunscpal_req *sr) 1976 { 1977 struct scsipi_xfer *xs = sr->sr_xs; 1978 1979 db_printf("TID=%d ", sr->sr_target); 1980 db_printf("LUN=%d ", sr->sr_lun); 1981 db_printf("dh=%p ", sr->sr_dma_hand); 1982 db_printf("dptr=%p ", sr->sr_dataptr); 1983 db_printf("dlen=0x%x ", sr->sr_datalen); 1984 db_printf("flags=%d ", sr->sr_flags); 1985 db_printf("stat=%d ", sr->sr_status); 1986 1987 if (xs == NULL) { 1988 db_printf("(xs=NULL)\n"); 1989 return; 1990 } 1991 db_printf("\n"); 1992 #ifdef SCSIDEBUG 1993 show_scsipi_xs(xs); 1994 #else 1995 db_printf("xs=%p\n", xs); 1996 #endif 1997 } 1998 1999 void 2000 sunscpal_show_state(void) 2001 { 2002 struct sunscpal_softc *sc; 2003 struct sunscpal_req *sr; 2004 int i, j, k; 2005 2006 sc = sunscpal_debug_sc; 2007 2008 if (sc == NULL) { 2009 db_printf("sunscpal_debug_sc == NULL\n"); 2010 return; 2011 } 2012 2013 db_printf("sc_ncmds=%d\n", sc->sc_ncmds); 2014 k = -1; /* which is current? */ 2015 for (i = 0; i < SUNSCPAL_OPENINGS; i++) { 2016 sr = &sc->sc_ring[i]; 2017 if (sr->sr_xs) { 2018 if (sr == sc->sc_current) 2019 k = i; 2020 db_printf("req %d: (sr=%p)", i, sr); 2021 sunscpal_show_req(sr); 2022 } 2023 } 2024 db_printf("sc_rr=%d, current=%d\n", sc->sc_rr, k); 2025 2026 db_printf("Active request matrix:\n"); 2027 for(i = 0; i < 8; i++) { /* targets */ 2028 for (j = 0; j < 8; j++) { /* LUN */ 2029 sr = sc->sc_matrix[i][j]; 2030 if (sr) { 2031 db_printf("TID=%d LUN=%d sr=%p\n", i, j, sr); 2032 } 2033 } 2034 } 2035 2036 db_printf("sc_state=0x%x\n", sc->sc_state); 2037 db_printf("sc_current=%p\n", sc->sc_current); 2038 db_printf("sc_dataptr=%p\n", sc->sc_dataptr); 2039 db_printf("sc_datalen=0x%x\n", sc->sc_datalen); 2040 2041 db_printf("sc_prevphase=%d\n", sc->sc_prevphase); 2042 db_printf("sc_msgpriq=0x%x\n", sc->sc_msgpriq); 2043 } 2044 #endif /* DDB */ 2045 #endif /* SUNSCPAL_DEBUG */ 2046 2047 void 2048 sunscpal_attach(struct sunscpal_softc *sc, int options) 2049 { 2050 2051 /* 2052 * Handle our options. 2053 */ 2054 aprint_normal(": options=0x%x\n", options); 2055 sc->sc_parity_disable = (options & SUNSCPAL_OPT_NO_PARITY_CHK); 2056 if (options & SUNSCPAL_OPT_DISABLE_DMA) 2057 sc->sc_flags |= SUNSCPAL_DISABLE_DMA; 2058 2059 /* 2060 * Fill in the adapter. 2061 */ 2062 memset(&sc->sc_adapter, 0, sizeof(sc->sc_adapter)); 2063 sc->sc_adapter.adapt_dev = sc->sc_dev; 2064 sc->sc_adapter.adapt_nchannels = 1; 2065 sc->sc_adapter.adapt_openings = SUNSCPAL_OPENINGS; 2066 sc->sc_adapter.adapt_max_periph = 1; 2067 sc->sc_adapter.adapt_request = sunscpal_scsipi_request; 2068 sc->sc_adapter.adapt_minphys = sunscpal_minphys; 2069 if (options & SUNSCPAL_OPT_FORCE_POLLING) 2070 sc->sc_adapter.adapt_flags |= SCSIPI_ADAPT_POLL_ONLY; 2071 2072 sc->sc_channel.chan_adapter = &sc->sc_adapter; 2073 sc->sc_channel.chan_bustype = &scsi_bustype; 2074 sc->sc_channel.chan_channel = 0; 2075 sc->sc_channel.chan_ntargets = 8; 2076 sc->sc_channel.chan_nluns = 8; 2077 sc->sc_channel.chan_id = 7; 2078 2079 /* 2080 * Add reference to adapter so that we drop the reference after 2081 * config_found() to make sure the adatper is disabled. 2082 */ 2083 if (scsipi_adapter_addref(&sc->sc_adapter) != 0) { 2084 aprint_error_dev(sc->sc_dev, "unable to enable controller\n"); 2085 return; 2086 } 2087 2088 sunscpal_init(sc); /* Init chip and driver */ 2089 sunscpal_reset_scsibus(sc); 2090 2091 /* 2092 * Ask the adapter what subunits are present 2093 */ 2094 (void)config_found(sc->sc_dev, &sc->sc_channel, scsiprint); 2095 scsipi_adapter_delref(&sc->sc_adapter); 2096 } 2097 2098 int 2099 sunscpal_detach(struct sunscpal_softc *sc, int flags) 2100 { 2101 2102 return EOPNOTSUPP; 2103 } 2104 2105 static void 2106 sunscpal_minphys(struct buf *bp) 2107 { 2108 2109 if (bp->b_bcount > SUNSCPAL_MAX_DMA_LEN) { 2110 #ifdef SUNSCPAL_DEBUG 2111 if (sunscpal_debug & SUNSCPAL_DBG_DMA) { 2112 printf("%s: len = 0x%lx.\n", __func__, bp->b_bcount); 2113 Debugger(); 2114 } 2115 #endif 2116 bp->b_bcount = SUNSCPAL_MAX_DMA_LEN; 2117 } 2118 return minphys(bp); 2119 } 2120 2121 #ifdef SUNSCPAL_USE_BUS_DMA 2122 2123 /* 2124 * Allocate a DMA handle and put it in sr->sr_dma_hand. Prepare 2125 * for DMA transfer. 2126 */ 2127 static void 2128 sunscpal_dma_alloc(struct sunscpal_softc *sc) 2129 { 2130 struct sunscpal_req *sr = sc->sc_current; 2131 sunscpal_dma_handle_t dh; 2132 int i, xlen; 2133 u_long addr; 2134 2135 #ifdef DIAGNOSTIC 2136 if (sr->sr_dma_hand != NULL) 2137 panic("%s: already have DMA handle", __func__); 2138 #endif 2139 2140 addr = (u_long)sc->sc_dataptr; 2141 xlen = sc->sc_datalen; 2142 2143 /* If the DMA start addr is misaligned then do PIO */ 2144 if ((addr & 1) || (xlen & 1)) { 2145 printf("%s: misaligned.\n", __func__); 2146 return; 2147 } 2148 2149 /* Make sure our caller checked sc_min_dma_len. */ 2150 if (xlen < sc->sc_min_dma_len) 2151 panic("%s: xlen=0x%x", __func__, xlen); 2152 2153 /* 2154 * Never attempt single transfers of more than 63k, because 2155 * our count register is only 16 bits. 2156 * This should never happen since already bounded by minphys(). 2157 * XXX - Should just segment these... 2158 */ 2159 if (xlen > SUNSCPAL_MAX_DMA_LEN) { 2160 printf("%s: excessive xlen=0x%x\n", __func__, xlen); 2161 Debugger(); 2162 sc->sc_datalen = xlen = SUNSCPAL_MAX_DMA_LEN; 2163 } 2164 2165 /* Find free DMA handle. Guaranteed to find one since we have 2166 as many DMA handles as the driver has processes. */ 2167 for (i = 0; i < SUNSCPAL_OPENINGS; i++) { 2168 if ((sc->sc_dma_handles[i].dh_flags & SUNSCDH_BUSY) == 0) 2169 goto found; 2170 } 2171 panic("%s: no free DMA handles.", device_xname(sc->sc_dev)); 2172 found: 2173 2174 dh = &sc->sc_dma_handles[i]; 2175 dh->dh_flags = SUNSCDH_BUSY; 2176 dh->dh_mapaddr = (uint8_t *)addr; 2177 dh->dh_maplen = xlen; 2178 dh->dh_dvma = 0; 2179 2180 /* Load the DMA map. */ 2181 if (bus_dmamap_load(sc->sunscpal_dmat, dh->dh_dmamap, 2182 dh->dh_mapaddr, dh->dh_maplen, NULL, BUS_DMA_NOWAIT) != 0) { 2183 /* Can't load map */ 2184 printf("%s: can't DMA %p/0x%x\n", __func__, 2185 dh->dh_mapaddr, dh->dh_maplen); 2186 dh->dh_flags = 0; 2187 return; 2188 } 2189 2190 /* success */ 2191 sr->sr_dma_hand = dh; 2192 } 2193 2194 static void 2195 sunscpal_dma_free(struct sunscpal_softc *sc) 2196 { 2197 struct sunscpal_req *sr = sc->sc_current; 2198 sunscpal_dma_handle_t dh = sr->sr_dma_hand; 2199 2200 #ifdef DIAGNOSTIC 2201 if (dh == NULL) 2202 panic("%s: no DMA handle", __func__); 2203 #endif 2204 2205 if (sc->sc_state & SUNSCPAL_DOINGDMA) 2206 panic("%s: free while in progress", __func__); 2207 2208 if (dh->dh_flags & SUNSCDH_BUSY) { 2209 /* XXX - Should separate allocation and mapping. */ 2210 /* Give back the DVMA space. */ 2211 bus_dmamap_unload(sc->sunscpal_dmat, dh->dh_dmamap); 2212 dh->dh_flags = 0; 2213 } 2214 sr->sr_dma_hand = NULL; 2215 } 2216 2217 /* 2218 * This function is called during the SELECT phase that 2219 * precedes a COMMAND phase, in case we need to setup the 2220 * DMA engine before the bus enters a DATA phase. 2221 * 2222 * On the sc version, setup the start address and the count. 2223 */ 2224 static void 2225 sunscpal_dma_setup(struct sunscpal_softc *sc) 2226 { 2227 struct sunscpal_req *sr = sc->sc_current; 2228 struct scsipi_xfer *xs = sr->sr_xs; 2229 sunscpal_dma_handle_t dh = sr->sr_dma_hand; 2230 long data_pa; 2231 int xlen; 2232 2233 /* 2234 * Get the DVMA mapping for this segment. 2235 * XXX - Should separate allocation and mapin. 2236 */ 2237 data_pa = dh->dh_dvma; 2238 data_pa += (sc->sc_dataptr - dh->dh_mapaddr); 2239 if (data_pa & 1) 2240 panic("%s: bad pa=0x%lx", __func__, data_pa); 2241 xlen = sc->sc_datalen; 2242 if (xlen & 1) 2243 panic("%s: bad xlen=0x%x", __func__, xlen); 2244 sc->sc_reqlen = xlen; /* XXX: or less? */ 2245 2246 #ifdef SUNSCPAL_DEBUG 2247 if (sunscpal_debug & SUNSCPAL_DBG_DMA) { 2248 printf("%s: dh=%p, pa=0x%lx, xlen=0x%x\n", 2249 __func__, dh, data_pa, xlen); 2250 } 2251 #endif 2252 2253 /* sync the DMA map: */ 2254 bus_dmamap_sync(sc->sunscpal_dmat, dh->dh_dmamap, 0, dh->dh_maplen, 2255 ((xs->xs_control & XS_CTL_DATA_OUT) == 0 ? 2256 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE)); 2257 2258 /* Load the start address and the count. */ 2259 SUNSCPAL_WRITE_2(sc, sunscpal_dma_addr_h, (data_pa >> 16) & 0xFFFF); 2260 SUNSCPAL_WRITE_2(sc, sunscpal_dma_addr_l, (data_pa >> 0) & 0xFFFF); 2261 SUNSCPAL_WRITE_2(sc, sunscpal_dma_count, SUNSCPAL_DMA_COUNT_FLIP(xlen)); 2262 } 2263 2264 #endif /* SUNSCPAL_USE_BUS_DMA */ 2265