1 /*- 2 * Core routines and tables shareable across OS platforms. 3 * 4 * Copyright (c) 1994-2002, 2004 Justin T. Gibbs. 5 * Copyright (c) 2000-2003 Adaptec Inc. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions, and the following disclaimer, 13 * without modification. 14 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 15 * substantially similar to the "NO WARRANTY" disclaimer below 16 * ("Disclaimer") and any redistribution must be conditioned upon 17 * including a substantially similar Disclaimer requirement for further 18 * binary redistribution. 19 * 3. Neither the names of the above-listed copyright holders nor the names 20 * of any contributors may be used to endorse or promote products derived 21 * from this software without specific prior written permission. 22 * 23 * Alternatively, this software may be distributed under the terms of the 24 * GNU General Public License ("GPL") version 2 as published by the Free 25 * Software Foundation. 26 * 27 * NO WARRANTY 28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 32 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 36 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 37 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 38 * POSSIBILITY OF SUCH DAMAGES. 39 * 40 * $Id: //depot/aic7xxx/aic7xxx/aic79xx.c#246 $ 41 */ 42 43 #ifdef __linux__ 44 #include "aic79xx_osm.h" 45 #include "aic79xx_inline.h" 46 #include "aicasm/aicasm_insformat.h" 47 #else 48 #include <sys/cdefs.h> 49 __FBSDID("$FreeBSD$"); 50 #include <dev/aic7xxx/aic79xx_osm.h> 51 #include <dev/aic7xxx/aic79xx_inline.h> 52 #include <dev/aic7xxx/aicasm/aicasm_insformat.h> 53 #endif 54 55 /******************************** Globals *************************************/ 56 struct ahd_softc_tailq ahd_tailq = TAILQ_HEAD_INITIALIZER(ahd_tailq); 57 uint32_t ahd_attach_to_HostRAID_controllers = 1; 58 59 /***************************** Lookup Tables **********************************/ 60 char *ahd_chip_names[] = 61 { 62 "NONE", 63 "aic7901", 64 "aic7902", 65 "aic7901A" 66 }; 67 68 /* 69 * Hardware error codes. 70 */ 71 struct ahd_hard_error_entry { 72 uint8_t errno; 73 char *errmesg; 74 }; 75 76 static struct ahd_hard_error_entry ahd_hard_errors[] = { 77 { DSCTMOUT, "Discard Timer has timed out" }, 78 { ILLOPCODE, "Illegal Opcode in sequencer program" }, 79 { SQPARERR, "Sequencer Parity Error" }, 80 { DPARERR, "Data-path Parity Error" }, 81 { MPARERR, "Scratch or SCB Memory Parity Error" }, 82 { CIOPARERR, "CIOBUS Parity Error" }, 83 }; 84 static const u_int num_errors = NUM_ELEMENTS(ahd_hard_errors); 85 86 static struct ahd_phase_table_entry ahd_phase_table[] = 87 { 88 { P_DATAOUT, MSG_NOOP, "in Data-out phase" }, 89 { P_DATAIN, MSG_INITIATOR_DET_ERR, "in Data-in phase" }, 90 { P_DATAOUT_DT, MSG_NOOP, "in DT Data-out phase" }, 91 { P_DATAIN_DT, MSG_INITIATOR_DET_ERR, "in DT Data-in phase" }, 92 { P_COMMAND, MSG_NOOP, "in Command phase" }, 93 { P_MESGOUT, MSG_NOOP, "in Message-out phase" }, 94 { P_STATUS, MSG_INITIATOR_DET_ERR, "in Status phase" }, 95 { P_MESGIN, MSG_PARITY_ERROR, "in Message-in phase" }, 96 { P_BUSFREE, MSG_NOOP, "while idle" }, 97 { 0, MSG_NOOP, "in unknown phase" } 98 }; 99 100 /* 101 * In most cases we only wish to itterate over real phases, so 102 * exclude the last element from the count. 103 */ 104 static const u_int num_phases = NUM_ELEMENTS(ahd_phase_table) - 1; 105 106 /* Our Sequencer Program */ 107 #include "aic79xx_seq.h" 108 109 /**************************** Function Declarations ***************************/ 110 static void ahd_handle_transmission_error(struct ahd_softc *ahd); 111 static void ahd_handle_lqiphase_error(struct ahd_softc *ahd, 112 u_int lqistat1); 113 static int ahd_handle_pkt_busfree(struct ahd_softc *ahd, 114 u_int busfreetime); 115 static int ahd_handle_nonpkt_busfree(struct ahd_softc *ahd); 116 static void ahd_handle_proto_violation(struct ahd_softc *ahd); 117 static void ahd_force_renegotiation(struct ahd_softc *ahd, 118 struct ahd_devinfo *devinfo); 119 120 static struct ahd_tmode_tstate* 121 ahd_alloc_tstate(struct ahd_softc *ahd, 122 u_int scsi_id, char channel); 123 #ifdef AHD_TARGET_MODE 124 static void ahd_free_tstate(struct ahd_softc *ahd, 125 u_int scsi_id, char channel, int force); 126 #endif 127 static void ahd_devlimited_syncrate(struct ahd_softc *ahd, 128 struct ahd_initiator_tinfo *, 129 u_int *period, 130 u_int *ppr_options, 131 role_t role); 132 static void ahd_update_neg_table(struct ahd_softc *ahd, 133 struct ahd_devinfo *devinfo, 134 struct ahd_transinfo *tinfo); 135 static void ahd_update_pending_scbs(struct ahd_softc *ahd); 136 static void ahd_fetch_devinfo(struct ahd_softc *ahd, 137 struct ahd_devinfo *devinfo); 138 static void ahd_scb_devinfo(struct ahd_softc *ahd, 139 struct ahd_devinfo *devinfo, 140 struct scb *scb); 141 static void ahd_setup_initiator_msgout(struct ahd_softc *ahd, 142 struct ahd_devinfo *devinfo, 143 struct scb *scb); 144 static void ahd_build_transfer_msg(struct ahd_softc *ahd, 145 struct ahd_devinfo *devinfo); 146 static void ahd_construct_sdtr(struct ahd_softc *ahd, 147 struct ahd_devinfo *devinfo, 148 u_int period, u_int offset); 149 static void ahd_construct_wdtr(struct ahd_softc *ahd, 150 struct ahd_devinfo *devinfo, 151 u_int bus_width); 152 static void ahd_construct_ppr(struct ahd_softc *ahd, 153 struct ahd_devinfo *devinfo, 154 u_int period, u_int offset, 155 u_int bus_width, u_int ppr_options); 156 static void ahd_clear_msg_state(struct ahd_softc *ahd); 157 static void ahd_handle_message_phase(struct ahd_softc *ahd); 158 typedef enum { 159 AHDMSG_1B, 160 AHDMSG_2B, 161 AHDMSG_EXT 162 } ahd_msgtype; 163 static int ahd_sent_msg(struct ahd_softc *ahd, ahd_msgtype type, 164 u_int msgval, int full); 165 static int ahd_parse_msg(struct ahd_softc *ahd, 166 struct ahd_devinfo *devinfo); 167 static int ahd_handle_msg_reject(struct ahd_softc *ahd, 168 struct ahd_devinfo *devinfo); 169 static void ahd_handle_ign_wide_residue(struct ahd_softc *ahd, 170 struct ahd_devinfo *devinfo); 171 static void ahd_reinitialize_dataptrs(struct ahd_softc *ahd); 172 static void ahd_handle_devreset(struct ahd_softc *ahd, 173 struct ahd_devinfo *devinfo, 174 u_int lun, cam_status status, 175 char *message, int verbose_level); 176 #ifdef AHD_TARGET_MODE 177 static void ahd_setup_target_msgin(struct ahd_softc *ahd, 178 struct ahd_devinfo *devinfo, 179 struct scb *scb); 180 #endif 181 182 static u_int ahd_sglist_size(struct ahd_softc *ahd); 183 static u_int ahd_sglist_allocsize(struct ahd_softc *ahd); 184 static bus_dmamap_callback_t 185 ahd_dmamap_cb; 186 static void ahd_initialize_hscbs(struct ahd_softc *ahd); 187 static int ahd_init_scbdata(struct ahd_softc *ahd); 188 static void ahd_fini_scbdata(struct ahd_softc *ahd); 189 static void ahd_setup_iocell_workaround(struct ahd_softc *ahd); 190 static void ahd_iocell_first_selection(struct ahd_softc *ahd); 191 static void ahd_add_col_list(struct ahd_softc *ahd, 192 struct scb *scb, u_int col_idx); 193 static void ahd_rem_col_list(struct ahd_softc *ahd, 194 struct scb *scb); 195 static void ahd_chip_init(struct ahd_softc *ahd); 196 static void ahd_qinfifo_requeue(struct ahd_softc *ahd, 197 struct scb *prev_scb, 198 struct scb *scb); 199 static int ahd_qinfifo_count(struct ahd_softc *ahd); 200 static int ahd_search_scb_list(struct ahd_softc *ahd, int target, 201 char channel, int lun, u_int tag, 202 role_t role, uint32_t status, 203 ahd_search_action action, 204 u_int *list_head, u_int *list_tail, 205 u_int tid); 206 static void ahd_stitch_tid_list(struct ahd_softc *ahd, 207 u_int tid_prev, u_int tid_cur, 208 u_int tid_next); 209 static void ahd_add_scb_to_free_list(struct ahd_softc *ahd, 210 u_int scbid); 211 static u_int ahd_rem_wscb(struct ahd_softc *ahd, u_int scbid, 212 u_int prev, u_int next, u_int tid); 213 static void ahd_reset_current_bus(struct ahd_softc *ahd); 214 static ahd_callback_t ahd_reset_poll; 215 static ahd_callback_t ahd_stat_timer; 216 #ifdef AHD_DUMP_SEQ 217 static void ahd_dumpseq(struct ahd_softc *ahd); 218 #endif 219 static void ahd_loadseq(struct ahd_softc *ahd); 220 static int ahd_check_patch(struct ahd_softc *ahd, 221 struct patch **start_patch, 222 u_int start_instr, u_int *skip_addr); 223 static u_int ahd_resolve_seqaddr(struct ahd_softc *ahd, 224 u_int address); 225 static void ahd_download_instr(struct ahd_softc *ahd, 226 u_int instrptr, uint8_t *dconsts); 227 static int ahd_probe_stack_size(struct ahd_softc *ahd); 228 static int ahd_other_scb_timeout(struct ahd_softc *ahd, 229 struct scb *scb, 230 struct scb *other_scb); 231 static int ahd_scb_active_in_fifo(struct ahd_softc *ahd, 232 struct scb *scb); 233 static void ahd_run_data_fifo(struct ahd_softc *ahd, 234 struct scb *scb); 235 236 #ifdef AHD_TARGET_MODE 237 static void ahd_queue_lstate_event(struct ahd_softc *ahd, 238 struct ahd_tmode_lstate *lstate, 239 u_int initiator_id, 240 u_int event_type, 241 u_int event_arg); 242 static void ahd_update_scsiid(struct ahd_softc *ahd, 243 u_int targid_mask); 244 static int ahd_handle_target_cmd(struct ahd_softc *ahd, 245 struct target_cmd *cmd); 246 #endif 247 248 /******************************** Private Inlines *****************************/ 249 static __inline void ahd_assert_atn(struct ahd_softc *ahd); 250 static __inline int ahd_currently_packetized(struct ahd_softc *ahd); 251 static __inline int ahd_set_active_fifo(struct ahd_softc *ahd); 252 253 static __inline void 254 ahd_assert_atn(struct ahd_softc *ahd) 255 { 256 ahd_outb(ahd, SCSISIGO, ATNO); 257 } 258 259 /* 260 * Determine if the current connection has a packetized 261 * agreement. This does not necessarily mean that we 262 * are currently in a packetized transfer. We could 263 * just as easily be sending or receiving a message. 264 */ 265 static __inline int 266 ahd_currently_packetized(struct ahd_softc *ahd) 267 { 268 ahd_mode_state saved_modes; 269 int packetized; 270 271 saved_modes = ahd_save_modes(ahd); 272 if ((ahd->bugs & AHD_PKTIZED_STATUS_BUG) != 0) { 273 /* 274 * The packetized bit refers to the last 275 * connection, not the current one. Check 276 * for non-zero LQISTATE instead. 277 */ 278 ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG); 279 packetized = ahd_inb(ahd, LQISTATE) != 0; 280 } else { 281 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI); 282 packetized = ahd_inb(ahd, LQISTAT2) & PACKETIZED; 283 } 284 ahd_restore_modes(ahd, saved_modes); 285 return (packetized); 286 } 287 288 static __inline int 289 ahd_set_active_fifo(struct ahd_softc *ahd) 290 { 291 u_int active_fifo; 292 293 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK); 294 active_fifo = ahd_inb(ahd, DFFSTAT) & CURRFIFO; 295 switch (active_fifo) { 296 case 0: 297 case 1: 298 ahd_set_modes(ahd, active_fifo, active_fifo); 299 return (1); 300 default: 301 return (0); 302 } 303 } 304 305 /************************* Sequencer Execution Control ************************/ 306 /* 307 * Restart the sequencer program from address zero 308 */ 309 void 310 ahd_restart(struct ahd_softc *ahd) 311 { 312 313 ahd_pause(ahd); 314 315 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI); 316 317 /* No more pending messages */ 318 ahd_clear_msg_state(ahd); 319 ahd_outb(ahd, SCSISIGO, 0); /* De-assert BSY */ 320 ahd_outb(ahd, MSG_OUT, MSG_NOOP); /* No message to send */ 321 ahd_outb(ahd, SXFRCTL1, ahd_inb(ahd, SXFRCTL1) & ~BITBUCKET); 322 ahd_outb(ahd, SEQINTCTL, 0); 323 ahd_outb(ahd, LASTPHASE, P_BUSFREE); 324 ahd_outb(ahd, SEQ_FLAGS, 0); 325 ahd_outb(ahd, SAVED_SCSIID, 0xFF); 326 ahd_outb(ahd, SAVED_LUN, 0xFF); 327 328 /* 329 * Ensure that the sequencer's idea of TQINPOS 330 * matches our own. The sequencer increments TQINPOS 331 * only after it sees a DMA complete and a reset could 332 * occur before the increment leaving the kernel to believe 333 * the command arrived but the sequencer to not. 334 */ 335 ahd_outb(ahd, TQINPOS, ahd->tqinfifonext); 336 337 /* Always allow reselection */ 338 ahd_outb(ahd, SCSISEQ1, 339 ahd_inb(ahd, SCSISEQ_TEMPLATE) & (ENSELI|ENRSELI|ENAUTOATNP)); 340 ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN); 341 342 /* 343 * Clear any pending sequencer interrupt. It is no 344 * longer relevant since we're resetting the Program 345 * Counter. 346 */ 347 ahd_outb(ahd, CLRINT, CLRSEQINT); 348 349 ahd_outb(ahd, SEQCTL0, FASTMODE|SEQRESET); 350 ahd_unpause(ahd); 351 } 352 353 void 354 ahd_clear_fifo(struct ahd_softc *ahd, u_int fifo) 355 { 356 ahd_mode_state saved_modes; 357 358 #ifdef AHD_DEBUG 359 if ((ahd_debug & AHD_SHOW_FIFOS) != 0) 360 printf("%s: Clearing FIFO %d\n", ahd_name(ahd), fifo); 361 #endif 362 saved_modes = ahd_save_modes(ahd); 363 ahd_set_modes(ahd, fifo, fifo); 364 ahd_outb(ahd, DFFSXFRCTL, RSTCHN|CLRSHCNT); 365 if ((ahd_inb(ahd, SG_STATE) & FETCH_INPROG) != 0) 366 ahd_outb(ahd, CCSGCTL, CCSGRESET); 367 ahd_outb(ahd, LONGJMP_ADDR + 1, INVALID_ADDR); 368 ahd_outb(ahd, SG_STATE, 0); 369 ahd_restore_modes(ahd, saved_modes); 370 } 371 372 /************************* Input/Output Queues ********************************/ 373 /* 374 * Flush and completed commands that are sitting in the command 375 * complete queues down on the chip but have yet to be dma'ed back up. 376 */ 377 void 378 ahd_flush_qoutfifo(struct ahd_softc *ahd) 379 { 380 struct scb *scb; 381 ahd_mode_state saved_modes; 382 u_int saved_scbptr; 383 u_int ccscbctl; 384 u_int scbid; 385 u_int next_scbid; 386 387 saved_modes = ahd_save_modes(ahd); 388 389 /* 390 * Flush the good status FIFO for completed packetized commands. 391 */ 392 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI); 393 saved_scbptr = ahd_get_scbptr(ahd); 394 while ((ahd_inb(ahd, LQISTAT2) & LQIGSAVAIL) != 0) { 395 u_int fifo_mode; 396 u_int i; 397 398 scbid = ahd_inw(ahd, GSFIFO); 399 scb = ahd_lookup_scb(ahd, scbid); 400 if (scb == NULL) { 401 printf("%s: Warning - GSFIFO SCB %d invalid\n", 402 ahd_name(ahd), scbid); 403 AHD_CORRECTABLE_ERROR(ahd); 404 continue; 405 } 406 /* 407 * Determine if this transaction is still active in 408 * any FIFO. If it is, we must flush that FIFO to 409 * the host before completing the command. 410 */ 411 fifo_mode = 0; 412 rescan_fifos: 413 for (i = 0; i < 2; i++) { 414 /* Toggle to the other mode. */ 415 fifo_mode ^= 1; 416 ahd_set_modes(ahd, fifo_mode, fifo_mode); 417 418 if (ahd_scb_active_in_fifo(ahd, scb) == 0) 419 continue; 420 421 ahd_run_data_fifo(ahd, scb); 422 423 /* 424 * Running this FIFO may cause a CFG4DATA for 425 * this same transaction to assert in the other 426 * FIFO or a new snapshot SAVEPTRS interrupt 427 * in this FIFO. Even running a FIFO may not 428 * clear the transaction if we are still waiting 429 * for data to drain to the host. We must loop 430 * until the transaction is not active in either 431 * FIFO just to be sure. Reset our loop counter 432 * so we will visit both FIFOs again before 433 * declaring this transaction finished. We 434 * also delay a bit so that status has a chance 435 * to change before we look at this FIFO again. 436 */ 437 aic_delay(200); 438 goto rescan_fifos; 439 } 440 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI); 441 ahd_set_scbptr(ahd, scbid); 442 if ((ahd_inb_scbram(ahd, SCB_SGPTR) & SG_LIST_NULL) == 0 443 && ((ahd_inb_scbram(ahd, SCB_SGPTR) & SG_FULL_RESID) != 0 444 || (ahd_inb_scbram(ahd, SCB_RESIDUAL_SGPTR) 445 & SG_LIST_NULL) != 0)) { 446 u_int comp_head; 447 448 /* 449 * The transfer completed with a residual. 450 * Place this SCB on the complete DMA list 451 * so that we update our in-core copy of the 452 * SCB before completing the command. 453 */ 454 ahd_outb(ahd, SCB_SCSI_STATUS, 0); 455 ahd_outb(ahd, SCB_SGPTR, 456 ahd_inb_scbram(ahd, SCB_SGPTR) 457 | SG_STATUS_VALID); 458 ahd_outw(ahd, SCB_TAG, scbid); 459 ahd_outw(ahd, SCB_NEXT_COMPLETE, SCB_LIST_NULL); 460 comp_head = ahd_inw(ahd, COMPLETE_DMA_SCB_HEAD); 461 if (SCBID_IS_NULL(comp_head)) { 462 ahd_outw(ahd, COMPLETE_DMA_SCB_HEAD, scbid); 463 ahd_outw(ahd, COMPLETE_DMA_SCB_TAIL, scbid); 464 } else { 465 u_int tail; 466 467 tail = ahd_inw(ahd, COMPLETE_DMA_SCB_TAIL); 468 ahd_set_scbptr(ahd, tail); 469 ahd_outw(ahd, SCB_NEXT_COMPLETE, scbid); 470 ahd_outw(ahd, COMPLETE_DMA_SCB_TAIL, scbid); 471 ahd_set_scbptr(ahd, scbid); 472 } 473 } else 474 ahd_complete_scb(ahd, scb); 475 } 476 ahd_set_scbptr(ahd, saved_scbptr); 477 478 /* 479 * Setup for command channel portion of flush. 480 */ 481 ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN); 482 483 /* 484 * Wait for any inprogress DMA to complete and clear DMA state 485 * if this if for an SCB in the qinfifo. 486 */ 487 while (((ccscbctl = ahd_inb(ahd, CCSCBCTL)) & (CCARREN|CCSCBEN)) != 0) { 488 489 if ((ccscbctl & (CCSCBDIR|CCARREN)) == (CCSCBDIR|CCARREN)) { 490 if ((ccscbctl & ARRDONE) != 0) 491 break; 492 } else if ((ccscbctl & CCSCBDONE) != 0) 493 break; 494 aic_delay(200); 495 } 496 /* 497 * We leave the sequencer to cleanup in the case of DMA's to 498 * update the qoutfifo. In all other cases (DMA's to the 499 * chip or a push of an SCB from the COMPLETE_DMA_SCB list), 500 * we disable the DMA engine so that the sequencer will not 501 * attempt to handle the DMA completion. 502 */ 503 if ((ccscbctl & CCSCBDIR) != 0 || (ccscbctl & ARRDONE) != 0) 504 ahd_outb(ahd, CCSCBCTL, ccscbctl & ~(CCARREN|CCSCBEN)); 505 506 /* 507 * Complete any SCBs that just finished 508 * being DMA'ed into the qoutfifo. 509 */ 510 ahd_run_qoutfifo(ahd); 511 512 saved_scbptr = ahd_get_scbptr(ahd); 513 /* 514 * Manually update/complete any completed SCBs that are waiting to be 515 * DMA'ed back up to the host. 516 */ 517 scbid = ahd_inw(ahd, COMPLETE_DMA_SCB_HEAD); 518 while (!SCBID_IS_NULL(scbid)) { 519 uint8_t *hscb_ptr; 520 u_int i; 521 522 ahd_set_scbptr(ahd, scbid); 523 next_scbid = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE); 524 scb = ahd_lookup_scb(ahd, scbid); 525 if (scb == NULL) { 526 printf("%s: Warning - DMA-up and complete " 527 "SCB %d invalid\n", ahd_name(ahd), scbid); 528 AHD_CORRECTABLE_ERROR(ahd); 529 continue; 530 } 531 hscb_ptr = (uint8_t *)scb->hscb; 532 for (i = 0; i < sizeof(struct hardware_scb); i++) 533 *hscb_ptr++ = ahd_inb_scbram(ahd, SCB_BASE + i); 534 535 ahd_complete_scb(ahd, scb); 536 scbid = next_scbid; 537 } 538 ahd_outw(ahd, COMPLETE_DMA_SCB_HEAD, SCB_LIST_NULL); 539 ahd_outw(ahd, COMPLETE_DMA_SCB_TAIL, SCB_LIST_NULL); 540 541 scbid = ahd_inw(ahd, COMPLETE_ON_QFREEZE_HEAD); 542 while (!SCBID_IS_NULL(scbid)) { 543 544 ahd_set_scbptr(ahd, scbid); 545 next_scbid = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE); 546 scb = ahd_lookup_scb(ahd, scbid); 547 if (scb == NULL) { 548 printf("%s: Warning - Complete Qfrz SCB %d invalid\n", 549 ahd_name(ahd), scbid); 550 AHD_CORRECTABLE_ERROR(ahd); 551 continue; 552 } 553 554 ahd_complete_scb(ahd, scb); 555 scbid = next_scbid; 556 } 557 ahd_outw(ahd, COMPLETE_ON_QFREEZE_HEAD, SCB_LIST_NULL); 558 559 scbid = ahd_inw(ahd, COMPLETE_SCB_HEAD); 560 while (!SCBID_IS_NULL(scbid)) { 561 562 ahd_set_scbptr(ahd, scbid); 563 next_scbid = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE); 564 scb = ahd_lookup_scb(ahd, scbid); 565 if (scb == NULL) { 566 printf("%s: Warning - Complete SCB %d invalid\n", 567 ahd_name(ahd), scbid); 568 AHD_CORRECTABLE_ERROR(ahd); 569 continue; 570 } 571 572 ahd_complete_scb(ahd, scb); 573 scbid = next_scbid; 574 } 575 ahd_outw(ahd, COMPLETE_SCB_HEAD, SCB_LIST_NULL); 576 577 /* 578 * Restore state. 579 */ 580 ahd_set_scbptr(ahd, saved_scbptr); 581 ahd_restore_modes(ahd, saved_modes); 582 ahd->flags |= AHD_UPDATE_PEND_CMDS; 583 } 584 585 /* 586 * Determine if an SCB for a packetized transaction 587 * is active in a FIFO. 588 */ 589 static int 590 ahd_scb_active_in_fifo(struct ahd_softc *ahd, struct scb *scb) 591 { 592 593 /* 594 * The FIFO is only active for our transaction if 595 * the SCBPTR matches the SCB's ID and the firmware 596 * has installed a handler for the FIFO or we have 597 * a pending SAVEPTRS or CFG4DATA interrupt. 598 */ 599 if (ahd_get_scbptr(ahd) != SCB_GET_TAG(scb) 600 || ((ahd_inb(ahd, LONGJMP_ADDR+1) & INVALID_ADDR) != 0 601 && (ahd_inb(ahd, SEQINTSRC) & (CFG4DATA|SAVEPTRS)) == 0)) 602 return (0); 603 604 return (1); 605 } 606 607 /* 608 * Run a data fifo to completion for a transaction we know 609 * has completed across the SCSI bus (good status has been 610 * received). We are already set to the correct FIFO mode 611 * on entry to this routine. 612 * 613 * This function attempts to operate exactly as the firmware 614 * would when running this FIFO. Care must be taken to update 615 * this routine any time the firmware's FIFO algorithm is 616 * changed. 617 */ 618 static void 619 ahd_run_data_fifo(struct ahd_softc *ahd, struct scb *scb) 620 { 621 u_int seqintsrc; 622 623 seqintsrc = ahd_inb(ahd, SEQINTSRC); 624 if ((seqintsrc & CFG4DATA) != 0) { 625 uint32_t datacnt; 626 uint32_t sgptr; 627 628 /* 629 * Clear full residual flag. 630 */ 631 sgptr = ahd_inl_scbram(ahd, SCB_SGPTR) & ~SG_FULL_RESID; 632 ahd_outb(ahd, SCB_SGPTR, sgptr); 633 634 /* 635 * Load datacnt and address. 636 */ 637 datacnt = ahd_inl_scbram(ahd, SCB_DATACNT); 638 if ((datacnt & AHD_DMA_LAST_SEG) != 0) { 639 sgptr |= LAST_SEG; 640 ahd_outb(ahd, SG_STATE, 0); 641 } else 642 ahd_outb(ahd, SG_STATE, LOADING_NEEDED); 643 ahd_outq(ahd, HADDR, ahd_inq_scbram(ahd, SCB_DATAPTR)); 644 ahd_outl(ahd, HCNT, datacnt & AHD_SG_LEN_MASK); 645 ahd_outb(ahd, SG_CACHE_PRE, sgptr); 646 ahd_outb(ahd, DFCNTRL, PRELOADEN|SCSIEN|HDMAEN); 647 648 /* 649 * Initialize Residual Fields. 650 */ 651 ahd_outb(ahd, SCB_RESIDUAL_DATACNT+3, datacnt >> 24); 652 ahd_outl(ahd, SCB_RESIDUAL_SGPTR, sgptr & SG_PTR_MASK); 653 654 /* 655 * Mark the SCB as having a FIFO in use. 656 */ 657 ahd_outb(ahd, SCB_FIFO_USE_COUNT, 658 ahd_inb_scbram(ahd, SCB_FIFO_USE_COUNT) + 1); 659 660 /* 661 * Install a "fake" handler for this FIFO. 662 */ 663 ahd_outw(ahd, LONGJMP_ADDR, 0); 664 665 /* 666 * Notify the hardware that we have satisfied 667 * this sequencer interrupt. 668 */ 669 ahd_outb(ahd, CLRSEQINTSRC, CLRCFG4DATA); 670 } else if ((seqintsrc & SAVEPTRS) != 0) { 671 uint32_t sgptr; 672 uint32_t resid; 673 674 if ((ahd_inb(ahd, LONGJMP_ADDR+1)&INVALID_ADDR) != 0) { 675 /* 676 * Snapshot Save Pointers. All that 677 * is necessary to clear the snapshot 678 * is a CLRCHN. 679 */ 680 goto clrchn; 681 } 682 683 /* 684 * Disable S/G fetch so the DMA engine 685 * is available to future users. 686 */ 687 if ((ahd_inb(ahd, SG_STATE) & FETCH_INPROG) != 0) 688 ahd_outb(ahd, CCSGCTL, 0); 689 ahd_outb(ahd, SG_STATE, 0); 690 691 /* 692 * Flush the data FIFO. Strickly only 693 * necessary for Rev A parts. 694 */ 695 ahd_outb(ahd, DFCNTRL, ahd_inb(ahd, DFCNTRL) | FIFOFLUSH); 696 697 /* 698 * Calculate residual. 699 */ 700 sgptr = ahd_inl_scbram(ahd, SCB_RESIDUAL_SGPTR); 701 resid = ahd_inl(ahd, SHCNT); 702 resid |= ahd_inb_scbram(ahd, SCB_RESIDUAL_DATACNT+3) << 24; 703 ahd_outl(ahd, SCB_RESIDUAL_DATACNT, resid); 704 if ((ahd_inb(ahd, SG_CACHE_SHADOW) & LAST_SEG) == 0) { 705 /* 706 * Must back up to the correct S/G element. 707 * Typically this just means resetting our 708 * low byte to the offset in the SG_CACHE, 709 * but if we wrapped, we have to correct 710 * the other bytes of the sgptr too. 711 */ 712 if ((ahd_inb(ahd, SG_CACHE_SHADOW) & 0x80) != 0 713 && (sgptr & 0x80) == 0) 714 sgptr -= 0x100; 715 sgptr &= ~0xFF; 716 sgptr |= ahd_inb(ahd, SG_CACHE_SHADOW) 717 & SG_ADDR_MASK; 718 ahd_outl(ahd, SCB_RESIDUAL_SGPTR, sgptr); 719 ahd_outb(ahd, SCB_RESIDUAL_DATACNT + 3, 0); 720 } else if ((resid & AHD_SG_LEN_MASK) == 0) { 721 ahd_outb(ahd, SCB_RESIDUAL_SGPTR, 722 sgptr | SG_LIST_NULL); 723 } 724 /* 725 * Save Pointers. 726 */ 727 ahd_outq(ahd, SCB_DATAPTR, ahd_inq(ahd, SHADDR)); 728 ahd_outl(ahd, SCB_DATACNT, resid); 729 ahd_outl(ahd, SCB_SGPTR, sgptr); 730 ahd_outb(ahd, CLRSEQINTSRC, CLRSAVEPTRS); 731 ahd_outb(ahd, SEQIMODE, 732 ahd_inb(ahd, SEQIMODE) | ENSAVEPTRS); 733 /* 734 * If the data is to the SCSI bus, we are 735 * done, otherwise wait for FIFOEMP. 736 */ 737 if ((ahd_inb(ahd, DFCNTRL) & DIRECTION) != 0) 738 goto clrchn; 739 } else if ((ahd_inb(ahd, SG_STATE) & LOADING_NEEDED) != 0) { 740 uint32_t sgptr; 741 uint64_t data_addr; 742 uint32_t data_len; 743 u_int dfcntrl; 744 745 /* 746 * Disable S/G fetch so the DMA engine 747 * is available to future users. We won't 748 * be using the DMA engine to load segments. 749 */ 750 if ((ahd_inb(ahd, SG_STATE) & FETCH_INPROG) != 0) { 751 ahd_outb(ahd, CCSGCTL, 0); 752 ahd_outb(ahd, SG_STATE, LOADING_NEEDED); 753 } 754 755 /* 756 * Wait for the DMA engine to notice that the 757 * host transfer is enabled and that there is 758 * space in the S/G FIFO for new segments before 759 * loading more segments. 760 */ 761 if ((ahd_inb(ahd, DFSTATUS) & PRELOAD_AVAIL) != 0 762 && (ahd_inb(ahd, DFCNTRL) & HDMAENACK) != 0) { 763 764 /* 765 * Determine the offset of the next S/G 766 * element to load. 767 */ 768 sgptr = ahd_inl_scbram(ahd, SCB_RESIDUAL_SGPTR); 769 sgptr &= SG_PTR_MASK; 770 if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0) { 771 struct ahd_dma64_seg *sg; 772 773 sg = ahd_sg_bus_to_virt(ahd, scb, sgptr); 774 data_addr = sg->addr; 775 data_len = sg->len; 776 sgptr += sizeof(*sg); 777 } else { 778 struct ahd_dma_seg *sg; 779 780 sg = ahd_sg_bus_to_virt(ahd, scb, sgptr); 781 data_addr = sg->len & AHD_SG_HIGH_ADDR_MASK; 782 data_addr <<= 8; 783 data_addr |= sg->addr; 784 data_len = sg->len; 785 sgptr += sizeof(*sg); 786 } 787 788 /* 789 * Update residual information. 790 */ 791 ahd_outb(ahd, SCB_RESIDUAL_DATACNT+3, data_len >> 24); 792 ahd_outl(ahd, SCB_RESIDUAL_SGPTR, sgptr); 793 794 /* 795 * Load the S/G. 796 */ 797 if (data_len & AHD_DMA_LAST_SEG) { 798 sgptr |= LAST_SEG; 799 ahd_outb(ahd, SG_STATE, 0); 800 } 801 ahd_outq(ahd, HADDR, data_addr); 802 ahd_outl(ahd, HCNT, data_len & AHD_SG_LEN_MASK); 803 ahd_outb(ahd, SG_CACHE_PRE, sgptr & 0xFF); 804 805 /* 806 * Advertise the segment to the hardware. 807 */ 808 dfcntrl = ahd_inb(ahd, DFCNTRL)|PRELOADEN|HDMAEN; 809 if ((ahd->features & AHD_NEW_DFCNTRL_OPTS) != 0) { 810 /* 811 * Use SCSIENWRDIS so that SCSIEN 812 * is never modified by this 813 * operation. 814 */ 815 dfcntrl |= SCSIENWRDIS; 816 } 817 ahd_outb(ahd, DFCNTRL, dfcntrl); 818 } 819 } else if ((ahd_inb(ahd, SG_CACHE_SHADOW) & LAST_SEG_DONE) != 0) { 820 821 /* 822 * Transfer completed to the end of SG list 823 * and has flushed to the host. 824 */ 825 ahd_outb(ahd, SCB_SGPTR, 826 ahd_inb_scbram(ahd, SCB_SGPTR) | SG_LIST_NULL); 827 goto clrchn; 828 } else if ((ahd_inb(ahd, DFSTATUS) & FIFOEMP) != 0) { 829 clrchn: 830 /* 831 * Clear any handler for this FIFO, decrement 832 * the FIFO use count for the SCB, and release 833 * the FIFO. 834 */ 835 ahd_outb(ahd, LONGJMP_ADDR + 1, INVALID_ADDR); 836 ahd_outb(ahd, SCB_FIFO_USE_COUNT, 837 ahd_inb_scbram(ahd, SCB_FIFO_USE_COUNT) - 1); 838 ahd_outb(ahd, DFFSXFRCTL, CLRCHN); 839 } 840 } 841 842 /* 843 * Look for entries in the QoutFIFO that have completed. 844 * The valid_tag completion field indicates the validity 845 * of the entry - the valid value toggles each time through 846 * the queue. We use the sg_status field in the completion 847 * entry to avoid referencing the hscb if the completion 848 * occurred with no errors and no residual. sg_status is 849 * a copy of the first byte (little endian) of the sgptr 850 * hscb field. 851 */ 852 void 853 ahd_run_qoutfifo(struct ahd_softc *ahd) 854 { 855 struct ahd_completion *completion; 856 struct scb *scb; 857 u_int scb_index; 858 859 if ((ahd->flags & AHD_RUNNING_QOUTFIFO) != 0) 860 panic("ahd_run_qoutfifo recursion"); 861 ahd->flags |= AHD_RUNNING_QOUTFIFO; 862 ahd_sync_qoutfifo(ahd, BUS_DMASYNC_POSTREAD); 863 for (;;) { 864 completion = &ahd->qoutfifo[ahd->qoutfifonext]; 865 866 if (completion->valid_tag != ahd->qoutfifonext_valid_tag) 867 break; 868 869 scb_index = aic_le16toh(completion->tag); 870 scb = ahd_lookup_scb(ahd, scb_index); 871 if (scb == NULL) { 872 printf("%s: WARNING no command for scb %d " 873 "(cmdcmplt)\nQOUTPOS = %d\n", 874 ahd_name(ahd), scb_index, 875 ahd->qoutfifonext); 876 AHD_CORRECTABLE_ERROR(ahd); 877 ahd_dump_card_state(ahd); 878 } else if ((completion->sg_status & SG_STATUS_VALID) != 0) { 879 ahd_handle_scb_status(ahd, scb); 880 } else { 881 ahd_done(ahd, scb); 882 } 883 884 ahd->qoutfifonext = (ahd->qoutfifonext+1) & (AHD_QOUT_SIZE-1); 885 if (ahd->qoutfifonext == 0) 886 ahd->qoutfifonext_valid_tag ^= QOUTFIFO_ENTRY_VALID; 887 } 888 ahd->flags &= ~AHD_RUNNING_QOUTFIFO; 889 } 890 891 /************************* Interrupt Handling *********************************/ 892 void 893 ahd_handle_hwerrint(struct ahd_softc *ahd) 894 { 895 /* 896 * Some catastrophic hardware error has occurred. 897 * Print it for the user and disable the controller. 898 */ 899 int i; 900 int error; 901 902 error = ahd_inb(ahd, ERROR); 903 for (i = 0; i < num_errors; i++) { 904 if ((error & ahd_hard_errors[i].errno) != 0) { 905 printf("%s: hwerrint, %s\n", 906 ahd_name(ahd), ahd_hard_errors[i].errmesg); 907 AHD_UNCORRECTABLE_ERROR(ahd); 908 } 909 } 910 911 ahd_dump_card_state(ahd); 912 panic("BRKADRINT"); 913 914 /* Tell everyone that this HBA is no longer available */ 915 ahd_abort_scbs(ahd, CAM_TARGET_WILDCARD, ALL_CHANNELS, 916 CAM_LUN_WILDCARD, SCB_LIST_NULL, ROLE_UNKNOWN, 917 CAM_NO_HBA); 918 919 /* Tell the system that this controller has gone away. */ 920 ahd_free(ahd); 921 } 922 923 void 924 ahd_handle_seqint(struct ahd_softc *ahd, u_int intstat) 925 { 926 u_int seqintcode; 927 928 /* 929 * Save the sequencer interrupt code and clear the SEQINT 930 * bit. We will unpause the sequencer, if appropriate, 931 * after servicing the request. 932 */ 933 seqintcode = ahd_inb(ahd, SEQINTCODE); 934 ahd_outb(ahd, CLRINT, CLRSEQINT); 935 if ((ahd->bugs & AHD_INTCOLLISION_BUG) != 0) { 936 /* 937 * Unpause the sequencer and let it clear 938 * SEQINT by writing NO_SEQINT to it. This 939 * will cause the sequencer to be paused again, 940 * which is the expected state of this routine. 941 */ 942 ahd_unpause(ahd); 943 while (!ahd_is_paused(ahd)) 944 ; 945 ahd_outb(ahd, CLRINT, CLRSEQINT); 946 } 947 ahd_update_modes(ahd); 948 #ifdef AHD_DEBUG 949 if ((ahd_debug & AHD_SHOW_MISC) != 0) 950 printf("%s: Handle Seqint Called for code %d\n", 951 ahd_name(ahd), seqintcode); 952 #endif 953 switch (seqintcode) { 954 case ENTERING_NONPACK: 955 { 956 struct scb *scb; 957 u_int scbid; 958 959 AHD_ASSERT_MODES(ahd, ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK), 960 ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK)); 961 scbid = ahd_get_scbptr(ahd); 962 scb = ahd_lookup_scb(ahd, scbid); 963 if (scb == NULL) { 964 /* 965 * Somehow need to know if this 966 * is from a selection or reselection. 967 * From that, we can determine target 968 * ID so we at least have an I_T nexus. 969 */ 970 } else { 971 ahd_outb(ahd, SAVED_SCSIID, scb->hscb->scsiid); 972 ahd_outb(ahd, SAVED_LUN, scb->hscb->lun); 973 ahd_outb(ahd, SEQ_FLAGS, 0x0); 974 } 975 if ((ahd_inb(ahd, LQISTAT2) & LQIPHASE_OUTPKT) != 0 976 && (ahd_inb(ahd, SCSISIGO) & ATNO) != 0) { 977 /* 978 * Phase change after read stream with 979 * CRC error with P0 asserted on last 980 * packet. 981 */ 982 #ifdef AHD_DEBUG 983 if ((ahd_debug & AHD_SHOW_RECOVERY) != 0) 984 printf("%s: Assuming LQIPHASE_NLQ with " 985 "P0 assertion\n", ahd_name(ahd)); 986 #endif 987 } 988 #ifdef AHD_DEBUG 989 if ((ahd_debug & AHD_SHOW_RECOVERY) != 0) 990 printf("%s: Entering NONPACK\n", ahd_name(ahd)); 991 #endif 992 break; 993 } 994 case INVALID_SEQINT: 995 printf("%s: Invalid Sequencer interrupt occurred.\n", 996 ahd_name(ahd)); 997 ahd_dump_card_state(ahd); 998 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE); 999 AHD_UNCORRECTABLE_ERROR(ahd); 1000 break; 1001 case STATUS_OVERRUN: 1002 { 1003 struct scb *scb; 1004 u_int scbid; 1005 1006 scbid = ahd_get_scbptr(ahd); 1007 scb = ahd_lookup_scb(ahd, scbid); 1008 if (scb != NULL) 1009 ahd_print_path(ahd, scb); 1010 else 1011 printf("%s: ", ahd_name(ahd)); 1012 printf("SCB %d Packetized Status Overrun", scbid); 1013 ahd_dump_card_state(ahd); 1014 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE); 1015 AHD_UNCORRECTABLE_ERROR(ahd); 1016 break; 1017 } 1018 case CFG4ISTAT_INTR: 1019 { 1020 struct scb *scb; 1021 u_int scbid; 1022 1023 scbid = ahd_get_scbptr(ahd); 1024 scb = ahd_lookup_scb(ahd, scbid); 1025 if (scb == NULL) { 1026 ahd_dump_card_state(ahd); 1027 printf("CFG4ISTAT: Free SCB %d referenced", scbid); 1028 AHD_FATAL_ERROR(ahd); 1029 panic("For safety"); 1030 } 1031 ahd_outq(ahd, HADDR, scb->sense_busaddr); 1032 ahd_outw(ahd, HCNT, AHD_SENSE_BUFSIZE); 1033 ahd_outb(ahd, HCNT + 2, 0); 1034 ahd_outb(ahd, SG_CACHE_PRE, SG_LAST_SEG); 1035 ahd_outb(ahd, DFCNTRL, PRELOADEN|SCSIEN|HDMAEN); 1036 break; 1037 } 1038 case ILLEGAL_PHASE: 1039 { 1040 u_int bus_phase; 1041 1042 bus_phase = ahd_inb(ahd, SCSISIGI) & PHASE_MASK; 1043 printf("%s: ILLEGAL_PHASE 0x%x\n", 1044 ahd_name(ahd), bus_phase); 1045 1046 switch (bus_phase) { 1047 case P_DATAOUT: 1048 case P_DATAIN: 1049 case P_DATAOUT_DT: 1050 case P_DATAIN_DT: 1051 case P_MESGOUT: 1052 case P_STATUS: 1053 case P_MESGIN: 1054 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE); 1055 printf("%s: Issued Bus Reset.\n", ahd_name(ahd)); 1056 AHD_UNCORRECTABLE_ERROR(ahd); 1057 break; 1058 case P_COMMAND: 1059 { 1060 struct ahd_devinfo devinfo; 1061 struct scb *scb; 1062 struct ahd_tmode_tstate *tstate; 1063 u_int scbid; 1064 1065 /* 1066 * If a target takes us into the command phase 1067 * assume that it has been externally reset and 1068 * has thus lost our previous packetized negotiation 1069 * agreement. Since we have not sent an identify 1070 * message and may not have fully qualified the 1071 * connection, we change our command to TUR, assert 1072 * ATN and ABORT the task when we go to message in 1073 * phase. The OSM will see the REQUEUE_REQUEST 1074 * status and retry the command. 1075 */ 1076 scbid = ahd_get_scbptr(ahd); 1077 scb = ahd_lookup_scb(ahd, scbid); 1078 if (scb == NULL) { 1079 AHD_CORRECTABLE_ERROR(ahd); 1080 printf("Invalid phase with no valid SCB. " 1081 "Resetting bus.\n"); 1082 ahd_reset_channel(ahd, 'A', 1083 /*Initiate Reset*/TRUE); 1084 break; 1085 } 1086 ahd_compile_devinfo(&devinfo, SCB_GET_OUR_ID(scb), 1087 SCB_GET_TARGET(ahd, scb), 1088 SCB_GET_LUN(scb), 1089 SCB_GET_CHANNEL(ahd, scb), 1090 ROLE_INITIATOR); 1091 ahd_fetch_transinfo(ahd, 1092 devinfo.channel, 1093 devinfo.our_scsiid, 1094 devinfo.target, 1095 &tstate); 1096 ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT, 1097 AHD_TRANS_ACTIVE, /*paused*/TRUE); 1098 ahd_set_syncrate(ahd, &devinfo, /*period*/0, 1099 /*offset*/0, /*ppr_options*/0, 1100 AHD_TRANS_ACTIVE, /*paused*/TRUE); 1101 ahd_outb(ahd, SCB_CDB_STORE, 0); 1102 ahd_outb(ahd, SCB_CDB_STORE+1, 0); 1103 ahd_outb(ahd, SCB_CDB_STORE+2, 0); 1104 ahd_outb(ahd, SCB_CDB_STORE+3, 0); 1105 ahd_outb(ahd, SCB_CDB_STORE+4, 0); 1106 ahd_outb(ahd, SCB_CDB_STORE+5, 0); 1107 ahd_outb(ahd, SCB_CDB_LEN, 6); 1108 scb->hscb->control &= ~(TAG_ENB|SCB_TAG_TYPE); 1109 scb->hscb->control |= MK_MESSAGE; 1110 ahd_outb(ahd, SCB_CONTROL, scb->hscb->control); 1111 ahd_outb(ahd, MSG_OUT, HOST_MSG); 1112 ahd_outb(ahd, SAVED_SCSIID, scb->hscb->scsiid); 1113 /* 1114 * The lun is 0, regardless of the SCB's lun 1115 * as we have not sent an identify message. 1116 */ 1117 ahd_outb(ahd, SAVED_LUN, 0); 1118 ahd_outb(ahd, SEQ_FLAGS, 0); 1119 ahd_assert_atn(ahd); 1120 scb->flags &= ~SCB_PACKETIZED; 1121 scb->flags |= SCB_ABORT|SCB_CMDPHASE_ABORT; 1122 ahd_freeze_devq(ahd, scb); 1123 aic_set_transaction_status(scb, CAM_REQUEUE_REQ); 1124 aic_freeze_scb(scb); 1125 1126 /* 1127 * Allow the sequencer to continue with 1128 * non-pack processing. 1129 */ 1130 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI); 1131 ahd_outb(ahd, CLRLQOINT1, CLRLQOPHACHGINPKT); 1132 if ((ahd->bugs & AHD_CLRLQO_AUTOCLR_BUG) != 0) { 1133 ahd_outb(ahd, CLRLQOINT1, 0); 1134 } 1135 #ifdef AHD_DEBUG 1136 if ((ahd_debug & AHD_SHOW_RECOVERY) != 0) { 1137 ahd_print_path(ahd, scb); 1138 AHD_CORRECTABLE_ERROR(ahd); 1139 printf("Unexpected command phase from " 1140 "packetized target\n"); 1141 } 1142 #endif 1143 break; 1144 } 1145 } 1146 break; 1147 } 1148 case CFG4OVERRUN: 1149 { 1150 struct scb *scb; 1151 u_int scb_index; 1152 1153 #ifdef AHD_DEBUG 1154 if ((ahd_debug & AHD_SHOW_RECOVERY) != 0) { 1155 printf("%s: CFG4OVERRUN mode = %x\n", ahd_name(ahd), 1156 ahd_inb(ahd, MODE_PTR)); 1157 } 1158 #endif 1159 scb_index = ahd_get_scbptr(ahd); 1160 scb = ahd_lookup_scb(ahd, scb_index); 1161 if (scb == NULL) { 1162 /* 1163 * Attempt to transfer to an SCB that is 1164 * not outstanding. 1165 */ 1166 ahd_assert_atn(ahd); 1167 ahd_outb(ahd, MSG_OUT, HOST_MSG); 1168 ahd->msgout_buf[0] = MSG_ABORT_TASK; 1169 ahd->msgout_len = 1; 1170 ahd->msgout_index = 0; 1171 ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT; 1172 /* 1173 * Clear status received flag to prevent any 1174 * attempt to complete this bogus SCB. 1175 */ 1176 ahd_outb(ahd, SCB_CONTROL, 1177 ahd_inb_scbram(ahd, SCB_CONTROL) 1178 & ~STATUS_RCVD); 1179 } 1180 break; 1181 } 1182 case DUMP_CARD_STATE: 1183 { 1184 ahd_dump_card_state(ahd); 1185 break; 1186 } 1187 case PDATA_REINIT: 1188 { 1189 #ifdef AHD_DEBUG 1190 if ((ahd_debug & AHD_SHOW_RECOVERY) != 0) { 1191 printf("%s: PDATA_REINIT - DFCNTRL = 0x%x " 1192 "SG_CACHE_SHADOW = 0x%x\n", 1193 ahd_name(ahd), ahd_inb(ahd, DFCNTRL), 1194 ahd_inb(ahd, SG_CACHE_SHADOW)); 1195 } 1196 #endif 1197 ahd_reinitialize_dataptrs(ahd); 1198 break; 1199 } 1200 case HOST_MSG_LOOP: 1201 { 1202 struct ahd_devinfo devinfo; 1203 1204 /* 1205 * The sequencer has encountered a message phase 1206 * that requires host assistance for completion. 1207 * While handling the message phase(s), we will be 1208 * notified by the sequencer after each byte is 1209 * transfered so we can track bus phase changes. 1210 * 1211 * If this is the first time we've seen a HOST_MSG_LOOP 1212 * interrupt, initialize the state of the host message 1213 * loop. 1214 */ 1215 ahd_fetch_devinfo(ahd, &devinfo); 1216 if (ahd->msg_type == MSG_TYPE_NONE) { 1217 struct scb *scb; 1218 u_int scb_index; 1219 u_int bus_phase; 1220 1221 bus_phase = ahd_inb(ahd, SCSISIGI) & PHASE_MASK; 1222 if (bus_phase != P_MESGIN 1223 && bus_phase != P_MESGOUT) { 1224 printf("ahd_intr: HOST_MSG_LOOP bad " 1225 "phase 0x%x\n", bus_phase); 1226 AHD_CORRECTABLE_ERROR(ahd); 1227 /* 1228 * Probably transitioned to bus free before 1229 * we got here. Just punt the message. 1230 */ 1231 ahd_dump_card_state(ahd); 1232 ahd_clear_intstat(ahd); 1233 ahd_restart(ahd); 1234 return; 1235 } 1236 1237 scb_index = ahd_get_scbptr(ahd); 1238 scb = ahd_lookup_scb(ahd, scb_index); 1239 if (devinfo.role == ROLE_INITIATOR) { 1240 if (bus_phase == P_MESGOUT) 1241 ahd_setup_initiator_msgout(ahd, 1242 &devinfo, 1243 scb); 1244 else { 1245 ahd->msg_type = 1246 MSG_TYPE_INITIATOR_MSGIN; 1247 ahd->msgin_index = 0; 1248 } 1249 } 1250 #ifdef AHD_TARGET_MODE 1251 else { 1252 if (bus_phase == P_MESGOUT) { 1253 ahd->msg_type = 1254 MSG_TYPE_TARGET_MSGOUT; 1255 ahd->msgin_index = 0; 1256 } 1257 else 1258 ahd_setup_target_msgin(ahd, 1259 &devinfo, 1260 scb); 1261 } 1262 #endif 1263 } 1264 1265 ahd_handle_message_phase(ahd); 1266 break; 1267 } 1268 case NO_MATCH: 1269 { 1270 /* Ensure we don't leave the selection hardware on */ 1271 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK); 1272 ahd_outb(ahd, SCSISEQ0, ahd_inb(ahd, SCSISEQ0) & ~ENSELO); 1273 1274 printf("%s:%c:%d: no active SCB for reconnecting " 1275 "target - issuing BUS DEVICE RESET\n", 1276 ahd_name(ahd), 'A', ahd_inb(ahd, SELID) >> 4); 1277 printf("SAVED_SCSIID == 0x%x, SAVED_LUN == 0x%x, " 1278 "REG0 == 0x%x ACCUM = 0x%x\n", 1279 ahd_inb(ahd, SAVED_SCSIID), ahd_inb(ahd, SAVED_LUN), 1280 ahd_inw(ahd, REG0), ahd_inb(ahd, ACCUM)); 1281 printf("SEQ_FLAGS == 0x%x, SCBPTR == 0x%x, BTT == 0x%x, " 1282 "SINDEX == 0x%x\n", 1283 ahd_inb(ahd, SEQ_FLAGS), ahd_get_scbptr(ahd), 1284 ahd_find_busy_tcl(ahd, 1285 BUILD_TCL(ahd_inb(ahd, SAVED_SCSIID), 1286 ahd_inb(ahd, SAVED_LUN))), 1287 ahd_inw(ahd, SINDEX)); 1288 printf("SELID == 0x%x, SCB_SCSIID == 0x%x, SCB_LUN == 0x%x, " 1289 "SCB_CONTROL == 0x%x\n", 1290 ahd_inb(ahd, SELID), ahd_inb_scbram(ahd, SCB_SCSIID), 1291 ahd_inb_scbram(ahd, SCB_LUN), 1292 ahd_inb_scbram(ahd, SCB_CONTROL)); 1293 printf("SCSIBUS[0] == 0x%x, SCSISIGI == 0x%x\n", 1294 ahd_inb(ahd, SCSIBUS), ahd_inb(ahd, SCSISIGI)); 1295 printf("SXFRCTL0 == 0x%x\n", ahd_inb(ahd, SXFRCTL0)); 1296 printf("SEQCTL0 == 0x%x\n", ahd_inb(ahd, SEQCTL0)); 1297 ahd_dump_card_state(ahd); 1298 ahd->msgout_buf[0] = MSG_BUS_DEV_RESET; 1299 ahd->msgout_len = 1; 1300 ahd->msgout_index = 0; 1301 ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT; 1302 ahd_outb(ahd, MSG_OUT, HOST_MSG); 1303 ahd_assert_atn(ahd); 1304 break; 1305 } 1306 case PROTO_VIOLATION: 1307 { 1308 ahd_handle_proto_violation(ahd); 1309 break; 1310 } 1311 case IGN_WIDE_RES: 1312 { 1313 struct ahd_devinfo devinfo; 1314 1315 ahd_fetch_devinfo(ahd, &devinfo); 1316 ahd_handle_ign_wide_residue(ahd, &devinfo); 1317 break; 1318 } 1319 case BAD_PHASE: 1320 { 1321 u_int lastphase; 1322 1323 lastphase = ahd_inb(ahd, LASTPHASE); 1324 printf("%s:%c:%d: unknown scsi bus phase %x, " 1325 "lastphase = 0x%x. Attempting to continue\n", 1326 ahd_name(ahd), 'A', 1327 SCSIID_TARGET(ahd, ahd_inb(ahd, SAVED_SCSIID)), 1328 lastphase, ahd_inb(ahd, SCSISIGI)); 1329 AHD_CORRECTABLE_ERROR(ahd); 1330 break; 1331 } 1332 case MISSED_BUSFREE: 1333 { 1334 u_int lastphase; 1335 1336 lastphase = ahd_inb(ahd, LASTPHASE); 1337 printf("%s:%c:%d: Missed busfree. " 1338 "Lastphase = 0x%x, Curphase = 0x%x\n", 1339 ahd_name(ahd), 'A', 1340 SCSIID_TARGET(ahd, ahd_inb(ahd, SAVED_SCSIID)), 1341 lastphase, ahd_inb(ahd, SCSISIGI)); 1342 AHD_CORRECTABLE_ERROR(ahd); 1343 ahd_restart(ahd); 1344 return; 1345 } 1346 case DATA_OVERRUN: 1347 { 1348 /* 1349 * When the sequencer detects an overrun, it 1350 * places the controller in "BITBUCKET" mode 1351 * and allows the target to complete its transfer. 1352 * Unfortunately, none of the counters get updated 1353 * when the controller is in this mode, so we have 1354 * no way of knowing how large the overrun was. 1355 */ 1356 struct scb *scb; 1357 u_int scbindex; 1358 #ifdef AHD_DEBUG 1359 u_int lastphase; 1360 #endif 1361 1362 scbindex = ahd_get_scbptr(ahd); 1363 scb = ahd_lookup_scb(ahd, scbindex); 1364 #ifdef AHD_DEBUG 1365 lastphase = ahd_inb(ahd, LASTPHASE); 1366 if ((ahd_debug & AHD_SHOW_RECOVERY) != 0) { 1367 ahd_print_path(ahd, scb); 1368 printf("data overrun detected %s. Tag == 0x%x.\n", 1369 ahd_lookup_phase_entry(lastphase)->phasemsg, 1370 SCB_GET_TAG(scb)); 1371 ahd_print_path(ahd, scb); 1372 printf("%s seen Data Phase. Length = %ld. " 1373 "NumSGs = %d.\n", 1374 ahd_inb(ahd, SEQ_FLAGS) & DPHASE 1375 ? "Have" : "Haven't", 1376 aic_get_transfer_length(scb), scb->sg_count); 1377 ahd_dump_sglist(scb); 1378 } 1379 #endif 1380 1381 /* 1382 * Set this and it will take effect when the 1383 * target does a command complete. 1384 */ 1385 ahd_freeze_devq(ahd, scb); 1386 aic_set_transaction_status(scb, CAM_DATA_RUN_ERR); 1387 aic_freeze_scb(scb); 1388 break; 1389 } 1390 case MKMSG_FAILED: 1391 { 1392 struct ahd_devinfo devinfo; 1393 struct scb *scb; 1394 u_int scbid; 1395 1396 ahd_fetch_devinfo(ahd, &devinfo); 1397 printf("%s:%c:%d:%d: Attempt to issue message failed\n", 1398 ahd_name(ahd), devinfo.channel, devinfo.target, 1399 devinfo.lun); 1400 scbid = ahd_get_scbptr(ahd); 1401 scb = ahd_lookup_scb(ahd, scbid); 1402 AHD_CORRECTABLE_ERROR(ahd); 1403 if (scb != NULL 1404 && (scb->flags & SCB_RECOVERY_SCB) != 0) 1405 /* 1406 * Ensure that we didn't put a second instance of this 1407 * SCB into the QINFIFO. 1408 */ 1409 ahd_search_qinfifo(ahd, SCB_GET_TARGET(ahd, scb), 1410 SCB_GET_CHANNEL(ahd, scb), 1411 SCB_GET_LUN(scb), SCB_GET_TAG(scb), 1412 ROLE_INITIATOR, /*status*/0, 1413 SEARCH_REMOVE); 1414 ahd_outb(ahd, SCB_CONTROL, 1415 ahd_inb_scbram(ahd, SCB_CONTROL) & ~MK_MESSAGE); 1416 break; 1417 } 1418 case TASKMGMT_FUNC_COMPLETE: 1419 { 1420 u_int scbid; 1421 struct scb *scb; 1422 1423 scbid = ahd_get_scbptr(ahd); 1424 scb = ahd_lookup_scb(ahd, scbid); 1425 if (scb != NULL) { 1426 u_int lun; 1427 u_int tag; 1428 cam_status error; 1429 1430 ahd_print_path(ahd, scb); 1431 printf("Task Management Func 0x%x Complete\n", 1432 scb->hscb->task_management); 1433 lun = CAM_LUN_WILDCARD; 1434 tag = SCB_LIST_NULL; 1435 1436 switch (scb->hscb->task_management) { 1437 case SIU_TASKMGMT_ABORT_TASK: 1438 tag = SCB_GET_TAG(scb); 1439 case SIU_TASKMGMT_ABORT_TASK_SET: 1440 case SIU_TASKMGMT_CLEAR_TASK_SET: 1441 lun = scb->hscb->lun; 1442 error = CAM_REQ_ABORTED; 1443 ahd_abort_scbs(ahd, SCB_GET_TARGET(ahd, scb), 1444 'A', lun, tag, ROLE_INITIATOR, 1445 error); 1446 break; 1447 case SIU_TASKMGMT_LUN_RESET: 1448 lun = scb->hscb->lun; 1449 case SIU_TASKMGMT_TARGET_RESET: 1450 { 1451 struct ahd_devinfo devinfo; 1452 1453 ahd_scb_devinfo(ahd, &devinfo, scb); 1454 error = CAM_BDR_SENT; 1455 ahd_handle_devreset(ahd, &devinfo, lun, 1456 CAM_BDR_SENT, 1457 lun != CAM_LUN_WILDCARD 1458 ? "Lun Reset" 1459 : "Target Reset", 1460 /*verbose_level*/0); 1461 break; 1462 } 1463 default: 1464 panic("Unexpected TaskMgmt Func\n"); 1465 break; 1466 } 1467 } 1468 break; 1469 } 1470 case TASKMGMT_CMD_CMPLT_OKAY: 1471 { 1472 u_int scbid; 1473 struct scb *scb; 1474 1475 /* 1476 * An ABORT TASK TMF failed to be delivered before 1477 * the targeted command completed normally. 1478 */ 1479 scbid = ahd_get_scbptr(ahd); 1480 scb = ahd_lookup_scb(ahd, scbid); 1481 if (scb != NULL) { 1482 /* 1483 * Remove the second instance of this SCB from 1484 * the QINFIFO if it is still there. 1485 */ 1486 ahd_print_path(ahd, scb); 1487 printf("SCB completes before TMF\n"); 1488 /* 1489 * Handle losing the race. Wait until any 1490 * current selection completes. We will then 1491 * set the TMF back to zero in this SCB so that 1492 * the sequencer doesn't bother to issue another 1493 * sequencer interrupt for its completion. 1494 */ 1495 while ((ahd_inb(ahd, SCSISEQ0) & ENSELO) != 0 1496 && (ahd_inb(ahd, SSTAT0) & SELDO) == 0 1497 && (ahd_inb(ahd, SSTAT1) & SELTO) == 0) 1498 ; 1499 ahd_outb(ahd, SCB_TASK_MANAGEMENT, 0); 1500 ahd_search_qinfifo(ahd, SCB_GET_TARGET(ahd, scb), 1501 SCB_GET_CHANNEL(ahd, scb), 1502 SCB_GET_LUN(scb), SCB_GET_TAG(scb), 1503 ROLE_INITIATOR, /*status*/0, 1504 SEARCH_REMOVE); 1505 } 1506 break; 1507 } 1508 case TRACEPOINT0: 1509 case TRACEPOINT1: 1510 case TRACEPOINT2: 1511 case TRACEPOINT3: 1512 printf("%s: Tracepoint %d\n", ahd_name(ahd), 1513 seqintcode - TRACEPOINT0); 1514 break; 1515 case NO_SEQINT: 1516 break; 1517 case SAW_HWERR: 1518 ahd_handle_hwerrint(ahd); 1519 break; 1520 default: 1521 printf("%s: Unexpected SEQINTCODE %d\n", ahd_name(ahd), 1522 seqintcode); 1523 break; 1524 } 1525 /* 1526 * The sequencer is paused immediately on 1527 * a SEQINT, so we should restart it when 1528 * we're done. 1529 */ 1530 ahd_unpause(ahd); 1531 } 1532 1533 void 1534 ahd_handle_scsiint(struct ahd_softc *ahd, u_int intstat) 1535 { 1536 struct scb *scb; 1537 u_int status0; 1538 u_int status3; 1539 u_int status; 1540 u_int lqistat1; 1541 u_int lqostat0; 1542 u_int scbid; 1543 u_int busfreetime; 1544 1545 ahd_update_modes(ahd); 1546 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI); 1547 1548 status3 = ahd_inb(ahd, SSTAT3) & (NTRAMPERR|OSRAMPERR); 1549 status0 = ahd_inb(ahd, SSTAT0) & (IOERR|OVERRUN|SELDI|SELDO); 1550 status = ahd_inb(ahd, SSTAT1) & (SELTO|SCSIRSTI|BUSFREE|SCSIPERR); 1551 lqistat1 = ahd_inb(ahd, LQISTAT1); 1552 lqostat0 = ahd_inb(ahd, LQOSTAT0); 1553 busfreetime = ahd_inb(ahd, SSTAT2) & BUSFREETIME; 1554 if ((status0 & (SELDI|SELDO)) != 0) { 1555 u_int simode0; 1556 1557 ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG); 1558 simode0 = ahd_inb(ahd, SIMODE0); 1559 status0 &= simode0 & (IOERR|OVERRUN|SELDI|SELDO); 1560 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI); 1561 } 1562 scbid = ahd_get_scbptr(ahd); 1563 scb = ahd_lookup_scb(ahd, scbid); 1564 if (scb != NULL 1565 && (ahd_inb(ahd, SEQ_FLAGS) & NOT_IDENTIFIED) != 0) 1566 scb = NULL; 1567 1568 if ((status0 & IOERR) != 0) { 1569 u_int now_lvd; 1570 1571 now_lvd = ahd_inb(ahd, SBLKCTL) & ENAB40; 1572 printf("%s: Transceiver State Has Changed to %s mode\n", 1573 ahd_name(ahd), now_lvd ? "LVD" : "SE"); 1574 ahd_outb(ahd, CLRSINT0, CLRIOERR); 1575 /* 1576 * A change in I/O mode is equivalent to a bus reset. 1577 */ 1578 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE); 1579 ahd_pause(ahd); 1580 ahd_setup_iocell_workaround(ahd); 1581 ahd_unpause(ahd); 1582 } else if ((status0 & OVERRUN) != 0) { 1583 1584 printf("%s: SCSI offset overrun detected. Resetting bus.\n", 1585 ahd_name(ahd)); 1586 AHD_CORRECTABLE_ERROR(ahd); 1587 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE); 1588 } else if ((status & SCSIRSTI) != 0) { 1589 1590 printf("%s: Someone reset channel A\n", ahd_name(ahd)); 1591 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/FALSE); 1592 AHD_UNCORRECTABLE_ERROR(ahd); 1593 } else if ((status & SCSIPERR) != 0) { 1594 1595 /* Make sure the sequencer is in a safe location. */ 1596 ahd_clear_critical_section(ahd); 1597 1598 ahd_handle_transmission_error(ahd); 1599 } else if (lqostat0 != 0) { 1600 1601 printf("%s: lqostat0 == 0x%x!\n", ahd_name(ahd), lqostat0); 1602 ahd_outb(ahd, CLRLQOINT0, lqostat0); 1603 if ((ahd->bugs & AHD_CLRLQO_AUTOCLR_BUG) != 0) 1604 ahd_outb(ahd, CLRLQOINT1, 0); 1605 } else if ((status & SELTO) != 0) { 1606 u_int scbid; 1607 1608 /* Stop the selection */ 1609 ahd_outb(ahd, SCSISEQ0, 0); 1610 1611 /* Make sure the sequencer is in a safe location. */ 1612 ahd_clear_critical_section(ahd); 1613 1614 /* No more pending messages */ 1615 ahd_clear_msg_state(ahd); 1616 1617 /* Clear interrupt state */ 1618 ahd_outb(ahd, CLRSINT1, CLRSELTIMEO|CLRBUSFREE|CLRSCSIPERR); 1619 1620 /* 1621 * Although the driver does not care about the 1622 * 'Selection in Progress' status bit, the busy 1623 * LED does. SELINGO is only cleared by a sucessfull 1624 * selection, so we must manually clear it to insure 1625 * the LED turns off just incase no future successful 1626 * selections occur (e.g. no devices on the bus). 1627 */ 1628 ahd_outb(ahd, CLRSINT0, CLRSELINGO); 1629 1630 scbid = ahd_inw(ahd, WAITING_TID_HEAD); 1631 scb = ahd_lookup_scb(ahd, scbid); 1632 if (scb == NULL) { 1633 printf("%s: ahd_intr - referenced scb not " 1634 "valid during SELTO scb(0x%x)\n", 1635 ahd_name(ahd), scbid); 1636 ahd_dump_card_state(ahd); 1637 AHD_UNCORRECTABLE_ERROR(ahd); 1638 } else { 1639 struct ahd_devinfo devinfo; 1640 #ifdef AHD_DEBUG 1641 if ((ahd_debug & AHD_SHOW_SELTO) != 0) { 1642 ahd_print_path(ahd, scb); 1643 printf("Saw Selection Timeout for SCB 0x%x\n", 1644 scbid); 1645 } 1646 #endif 1647 ahd_scb_devinfo(ahd, &devinfo, scb); 1648 aic_set_transaction_status(scb, CAM_SEL_TIMEOUT); 1649 ahd_freeze_devq(ahd, scb); 1650 1651 /* 1652 * Cancel any pending transactions on the device 1653 * now that it seems to be missing. This will 1654 * also revert us to async/narrow transfers until 1655 * we can renegotiate with the device. 1656 */ 1657 ahd_handle_devreset(ahd, &devinfo, 1658 CAM_LUN_WILDCARD, 1659 CAM_SEL_TIMEOUT, 1660 "Selection Timeout", 1661 /*verbose_level*/1); 1662 } 1663 ahd_outb(ahd, CLRINT, CLRSCSIINT); 1664 ahd_iocell_first_selection(ahd); 1665 ahd_unpause(ahd); 1666 } else if ((status0 & (SELDI|SELDO)) != 0) { 1667 1668 ahd_iocell_first_selection(ahd); 1669 ahd_unpause(ahd); 1670 } else if (status3 != 0) { 1671 printf("%s: SCSI Cell parity error SSTAT3 == 0x%x\n", 1672 ahd_name(ahd), status3); 1673 AHD_CORRECTABLE_ERROR(ahd); 1674 ahd_outb(ahd, CLRSINT3, status3); 1675 } else if ((lqistat1 & (LQIPHASE_LQ|LQIPHASE_NLQ)) != 0) { 1676 1677 /* Make sure the sequencer is in a safe location. */ 1678 ahd_clear_critical_section(ahd); 1679 1680 ahd_handle_lqiphase_error(ahd, lqistat1); 1681 } else if ((lqistat1 & LQICRCI_NLQ) != 0) { 1682 /* 1683 * This status can be delayed during some 1684 * streaming operations. The SCSIPHASE 1685 * handler has already dealt with this case 1686 * so just clear the error. 1687 */ 1688 ahd_outb(ahd, CLRLQIINT1, CLRLQICRCI_NLQ); 1689 } else if ((status & BUSFREE) != 0 1690 || (lqistat1 & LQOBUSFREE) != 0) { 1691 u_int lqostat1; 1692 int restart; 1693 int clear_fifo; 1694 int packetized; 1695 u_int mode; 1696 1697 /* 1698 * Clear our selection hardware as soon as possible. 1699 * We may have an entry in the waiting Q for this target, 1700 * that is affected by this busfree and we don't want to 1701 * go about selecting the target while we handle the event. 1702 */ 1703 ahd_outb(ahd, SCSISEQ0, 0); 1704 1705 /* Make sure the sequencer is in a safe location. */ 1706 ahd_clear_critical_section(ahd); 1707 1708 /* 1709 * Determine what we were up to at the time of 1710 * the busfree. 1711 */ 1712 mode = AHD_MODE_SCSI; 1713 busfreetime = ahd_inb(ahd, SSTAT2) & BUSFREETIME; 1714 lqostat1 = ahd_inb(ahd, LQOSTAT1); 1715 switch (busfreetime) { 1716 case BUSFREE_DFF0: 1717 case BUSFREE_DFF1: 1718 { 1719 u_int scbid; 1720 struct scb *scb; 1721 1722 mode = busfreetime == BUSFREE_DFF0 1723 ? AHD_MODE_DFF0 : AHD_MODE_DFF1; 1724 ahd_set_modes(ahd, mode, mode); 1725 scbid = ahd_get_scbptr(ahd); 1726 scb = ahd_lookup_scb(ahd, scbid); 1727 if (scb == NULL) { 1728 printf("%s: Invalid SCB %d in DFF%d " 1729 "during unexpected busfree\n", 1730 ahd_name(ahd), scbid, mode); 1731 packetized = 0; 1732 AHD_CORRECTABLE_ERROR(ahd); 1733 } else 1734 packetized = (scb->flags & SCB_PACKETIZED) != 0; 1735 clear_fifo = 1; 1736 break; 1737 } 1738 case BUSFREE_LQO: 1739 clear_fifo = 0; 1740 packetized = 1; 1741 break; 1742 default: 1743 clear_fifo = 0; 1744 packetized = (lqostat1 & LQOBUSFREE) != 0; 1745 if (!packetized 1746 && ahd_inb(ahd, LASTPHASE) == P_BUSFREE 1747 && (ahd_inb(ahd, SSTAT0) & SELDI) == 0 1748 && ((ahd_inb(ahd, SSTAT0) & SELDO) == 0 1749 || (ahd_inb(ahd, SCSISEQ0) & ENSELO) == 0)) 1750 /* 1751 * Assume packetized if we are not 1752 * on the bus in a non-packetized 1753 * capacity and any pending selection 1754 * was a packetized selection. 1755 */ 1756 packetized = 1; 1757 break; 1758 } 1759 1760 #ifdef AHD_DEBUG 1761 if ((ahd_debug & AHD_SHOW_MISC) != 0) 1762 printf("Saw Busfree. Busfreetime = 0x%x.\n", 1763 busfreetime); 1764 #endif 1765 /* 1766 * Busfrees that occur in non-packetized phases are 1767 * handled by the nonpkt_busfree handler. 1768 */ 1769 if (packetized && ahd_inb(ahd, LASTPHASE) == P_BUSFREE) { 1770 restart = ahd_handle_pkt_busfree(ahd, busfreetime); 1771 } else { 1772 packetized = 0; 1773 restart = ahd_handle_nonpkt_busfree(ahd); 1774 } 1775 /* 1776 * Clear the busfree interrupt status. The setting of 1777 * the interrupt is a pulse, so in a perfect world, we 1778 * would not need to muck with the ENBUSFREE logic. This 1779 * would ensure that if the bus moves on to another 1780 * connection, busfree protection is still in force. If 1781 * BUSFREEREV is broken, however, we must manually clear 1782 * the ENBUSFREE if the busfree occurred during a non-pack 1783 * connection so that we don't get false positives during 1784 * future, packetized, connections. 1785 */ 1786 ahd_outb(ahd, CLRSINT1, CLRBUSFREE); 1787 if (packetized == 0 1788 && (ahd->bugs & AHD_BUSFREEREV_BUG) != 0) 1789 ahd_outb(ahd, SIMODE1, 1790 ahd_inb(ahd, SIMODE1) & ~ENBUSFREE); 1791 1792 if (clear_fifo) 1793 ahd_clear_fifo(ahd, mode); 1794 1795 ahd_clear_msg_state(ahd); 1796 ahd_outb(ahd, CLRINT, CLRSCSIINT); 1797 if (restart) { 1798 ahd_restart(ahd); 1799 } else { 1800 ahd_unpause(ahd); 1801 } 1802 } else { 1803 printf("%s: Missing case in ahd_handle_scsiint. status = %x\n", 1804 ahd_name(ahd), status); 1805 ahd_dump_card_state(ahd); 1806 ahd_clear_intstat(ahd); 1807 ahd_unpause(ahd); 1808 } 1809 } 1810 1811 static void 1812 ahd_handle_transmission_error(struct ahd_softc *ahd) 1813 { 1814 struct scb *scb; 1815 u_int scbid; 1816 u_int lqistat1; 1817 u_int msg_out; 1818 u_int curphase; 1819 u_int lastphase; 1820 u_int perrdiag; 1821 u_int cur_col; 1822 int silent; 1823 1824 scb = NULL; 1825 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI); 1826 lqistat1 = ahd_inb(ahd, LQISTAT1) & ~(LQIPHASE_LQ|LQIPHASE_NLQ); 1827 ahd_inb(ahd, LQISTAT2); 1828 if ((lqistat1 & (LQICRCI_NLQ|LQICRCI_LQ)) == 0 1829 && (ahd->bugs & AHD_NLQICRC_DELAYED_BUG) != 0) { 1830 u_int lqistate; 1831 1832 ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG); 1833 lqistate = ahd_inb(ahd, LQISTATE); 1834 if ((lqistate >= 0x1E && lqistate <= 0x24) 1835 || (lqistate == 0x29)) { 1836 #ifdef AHD_DEBUG 1837 if ((ahd_debug & AHD_SHOW_RECOVERY) != 0) { 1838 printf("%s: NLQCRC found via LQISTATE\n", 1839 ahd_name(ahd)); 1840 } 1841 #endif 1842 lqistat1 |= LQICRCI_NLQ; 1843 } 1844 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI); 1845 } 1846 1847 ahd_outb(ahd, CLRLQIINT1, lqistat1); 1848 lastphase = ahd_inb(ahd, LASTPHASE); 1849 curphase = ahd_inb(ahd, SCSISIGI) & PHASE_MASK; 1850 perrdiag = ahd_inb(ahd, PERRDIAG); 1851 msg_out = MSG_INITIATOR_DET_ERR; 1852 ahd_outb(ahd, CLRSINT1, CLRSCSIPERR); 1853 1854 /* 1855 * Try to find the SCB associated with this error. 1856 */ 1857 silent = FALSE; 1858 if (lqistat1 == 0 1859 || (lqistat1 & LQICRCI_NLQ) != 0) { 1860 if ((lqistat1 & (LQICRCI_NLQ|LQIOVERI_NLQ)) != 0) 1861 ahd_set_active_fifo(ahd); 1862 scbid = ahd_get_scbptr(ahd); 1863 scb = ahd_lookup_scb(ahd, scbid); 1864 if (scb != NULL && SCB_IS_SILENT(scb)) 1865 silent = TRUE; 1866 } 1867 1868 cur_col = 0; 1869 if (silent == FALSE) { 1870 printf("%s: Transmission error detected\n", ahd_name(ahd)); 1871 ahd_lqistat1_print(lqistat1, &cur_col, 50); 1872 ahd_lastphase_print(lastphase, &cur_col, 50); 1873 ahd_scsisigi_print(curphase, &cur_col, 50); 1874 ahd_perrdiag_print(perrdiag, &cur_col, 50); 1875 printf("\n"); 1876 AHD_CORRECTABLE_ERROR(ahd); 1877 ahd_dump_card_state(ahd); 1878 } 1879 1880 if ((lqistat1 & (LQIOVERI_LQ|LQIOVERI_NLQ)) != 0) { 1881 if (silent == FALSE) { 1882 printf("%s: Gross protocol error during incoming " 1883 "packet. lqistat1 == 0x%x. Resetting bus.\n", 1884 ahd_name(ahd), lqistat1); 1885 AHD_UNCORRECTABLE_ERROR(ahd); 1886 } 1887 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE); 1888 return; 1889 } else if ((lqistat1 & LQICRCI_LQ) != 0) { 1890 /* 1891 * A CRC error has been detected on an incoming LQ. 1892 * The bus is currently hung on the last ACK. 1893 * Hit LQIRETRY to release the last ack, and 1894 * wait for the sequencer to determine that ATNO 1895 * is asserted while in message out to take us 1896 * to our host message loop. No NONPACKREQ or 1897 * LQIPHASE type errors will occur in this 1898 * scenario. After this first LQIRETRY, the LQI 1899 * manager will be in ISELO where it will 1900 * happily sit until another packet phase begins. 1901 * Unexpected bus free detection is enabled 1902 * through any phases that occur after we release 1903 * this last ack until the LQI manager sees a 1904 * packet phase. This implies we may have to 1905 * ignore a perfectly valid "unexected busfree" 1906 * after our "initiator detected error" message is 1907 * sent. A busfree is the expected response after 1908 * we tell the target that it's L_Q was corrupted. 1909 * (SPI4R09 10.7.3.3.3) 1910 */ 1911 ahd_outb(ahd, LQCTL2, LQIRETRY); 1912 printf("LQIRetry for LQICRCI_LQ to release ACK\n"); 1913 AHD_CORRECTABLE_ERROR(ahd); 1914 } else if ((lqistat1 & LQICRCI_NLQ) != 0) { 1915 /* 1916 * We detected a CRC error in a NON-LQ packet. 1917 * The hardware has varying behavior in this situation 1918 * depending on whether this packet was part of a 1919 * stream or not. 1920 * 1921 * PKT by PKT mode: 1922 * The hardware has already acked the complete packet. 1923 * If the target honors our outstanding ATN condition, 1924 * we should be (or soon will be) in MSGOUT phase. 1925 * This will trigger the LQIPHASE_LQ status bit as the 1926 * hardware was expecting another LQ. Unexpected 1927 * busfree detection is enabled. Once LQIPHASE_LQ is 1928 * true (first entry into host message loop is much 1929 * the same), we must clear LQIPHASE_LQ and hit 1930 * LQIRETRY so the hardware is ready to handle 1931 * a future LQ. NONPACKREQ will not be asserted again 1932 * once we hit LQIRETRY until another packet is 1933 * processed. The target may either go busfree 1934 * or start another packet in response to our message. 1935 * 1936 * Read Streaming P0 asserted: 1937 * If we raise ATN and the target completes the entire 1938 * stream (P0 asserted during the last packet), the 1939 * hardware will ack all data and return to the ISTART 1940 * state. When the target reponds to our ATN condition, 1941 * LQIPHASE_LQ will be asserted. We should respond to 1942 * this with an LQIRETRY to prepare for any future 1943 * packets. NONPACKREQ will not be asserted again 1944 * once we hit LQIRETRY until another packet is 1945 * processed. The target may either go busfree or 1946 * start another packet in response to our message. 1947 * Busfree detection is enabled. 1948 * 1949 * Read Streaming P0 not asserted: 1950 * If we raise ATN and the target transitions to 1951 * MSGOUT in or after a packet where P0 is not 1952 * asserted, the hardware will assert LQIPHASE_NLQ. 1953 * We should respond to the LQIPHASE_NLQ with an 1954 * LQIRETRY. Should the target stay in a non-pkt 1955 * phase after we send our message, the hardware 1956 * will assert LQIPHASE_LQ. Recovery is then just as 1957 * listed above for the read streaming with P0 asserted. 1958 * Busfree detection is enabled. 1959 */ 1960 if (silent == FALSE) 1961 printf("LQICRC_NLQ\n"); 1962 if (scb == NULL) { 1963 printf("%s: No SCB valid for LQICRC_NLQ. " 1964 "Resetting bus\n", ahd_name(ahd)); 1965 AHD_UNCORRECTABLE_ERROR(ahd); 1966 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE); 1967 return; 1968 } 1969 } else if ((lqistat1 & LQIBADLQI) != 0) { 1970 printf("Need to handle BADLQI!\n"); 1971 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE); 1972 return; 1973 } else if ((perrdiag & (PARITYERR|PREVPHASE)) == PARITYERR) { 1974 if ((curphase & ~P_DATAIN_DT) != 0) { 1975 /* Ack the byte. So we can continue. */ 1976 if (silent == FALSE) 1977 printf("Acking %s to clear perror\n", 1978 ahd_lookup_phase_entry(curphase)->phasemsg); 1979 ahd_inb(ahd, SCSIDAT); 1980 } 1981 1982 if (curphase == P_MESGIN) 1983 msg_out = MSG_PARITY_ERROR; 1984 } 1985 1986 /* 1987 * We've set the hardware to assert ATN if we 1988 * get a parity error on "in" phases, so all we 1989 * need to do is stuff the message buffer with 1990 * the appropriate message. "In" phases have set 1991 * mesg_out to something other than MSG_NOP. 1992 */ 1993 ahd->send_msg_perror = msg_out; 1994 if (scb != NULL && msg_out == MSG_INITIATOR_DET_ERR) 1995 scb->flags |= SCB_TRANSMISSION_ERROR; 1996 ahd_outb(ahd, MSG_OUT, HOST_MSG); 1997 ahd_outb(ahd, CLRINT, CLRSCSIINT); 1998 ahd_unpause(ahd); 1999 } 2000 2001 static void 2002 ahd_handle_lqiphase_error(struct ahd_softc *ahd, u_int lqistat1) 2003 { 2004 /* 2005 * Clear the sources of the interrupts. 2006 */ 2007 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI); 2008 ahd_outb(ahd, CLRLQIINT1, lqistat1); 2009 2010 /* 2011 * If the "illegal" phase changes were in response 2012 * to our ATN to flag a CRC error, AND we ended up 2013 * on packet boundaries, clear the error, restart the 2014 * LQI manager as appropriate, and go on our merry 2015 * way toward sending the message. Otherwise, reset 2016 * the bus to clear the error. 2017 */ 2018 ahd_set_active_fifo(ahd); 2019 if ((ahd_inb(ahd, SCSISIGO) & ATNO) != 0 2020 && (ahd_inb(ahd, MDFFSTAT) & DLZERO) != 0) { 2021 if ((lqistat1 & LQIPHASE_LQ) != 0) { 2022 printf("LQIRETRY for LQIPHASE_LQ\n"); 2023 AHD_CORRECTABLE_ERROR(ahd); 2024 ahd_outb(ahd, LQCTL2, LQIRETRY); 2025 } else if ((lqistat1 & LQIPHASE_NLQ) != 0) { 2026 printf("LQIRETRY for LQIPHASE_NLQ\n"); 2027 AHD_CORRECTABLE_ERROR(ahd); 2028 ahd_outb(ahd, LQCTL2, LQIRETRY); 2029 } else 2030 panic("ahd_handle_lqiphase_error: No phase errors\n"); 2031 ahd_dump_card_state(ahd); 2032 ahd_outb(ahd, CLRINT, CLRSCSIINT); 2033 ahd_unpause(ahd); 2034 } else { 2035 printf("Reseting Channel for LQI Phase error\n"); 2036 AHD_CORRECTABLE_ERROR(ahd); 2037 ahd_dump_card_state(ahd); 2038 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE); 2039 } 2040 } 2041 2042 /* 2043 * Packetized unexpected or expected busfree. 2044 * Entered in mode based on busfreetime. 2045 */ 2046 static int 2047 ahd_handle_pkt_busfree(struct ahd_softc *ahd, u_int busfreetime) 2048 { 2049 u_int lqostat1; 2050 2051 AHD_ASSERT_MODES(ahd, ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK), 2052 ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK)); 2053 lqostat1 = ahd_inb(ahd, LQOSTAT1); 2054 if ((lqostat1 & LQOBUSFREE) != 0) { 2055 struct scb *scb; 2056 u_int scbid; 2057 u_int saved_scbptr; 2058 u_int waiting_h; 2059 u_int waiting_t; 2060 u_int next; 2061 2062 /* 2063 * The LQO manager detected an unexpected busfree 2064 * either: 2065 * 2066 * 1) During an outgoing LQ. 2067 * 2) After an outgoing LQ but before the first 2068 * REQ of the command packet. 2069 * 3) During an outgoing command packet. 2070 * 2071 * In all cases, CURRSCB is pointing to the 2072 * SCB that encountered the failure. Clean 2073 * up the queue, clear SELDO and LQOBUSFREE, 2074 * and allow the sequencer to restart the select 2075 * out at its lesure. 2076 */ 2077 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI); 2078 scbid = ahd_inw(ahd, CURRSCB); 2079 scb = ahd_lookup_scb(ahd, scbid); 2080 if (scb == NULL) 2081 panic("SCB not valid during LQOBUSFREE"); 2082 /* 2083 * Clear the status. 2084 */ 2085 ahd_outb(ahd, CLRLQOINT1, CLRLQOBUSFREE); 2086 if ((ahd->bugs & AHD_CLRLQO_AUTOCLR_BUG) != 0) 2087 ahd_outb(ahd, CLRLQOINT1, 0); 2088 ahd_outb(ahd, SCSISEQ0, ahd_inb(ahd, SCSISEQ0) & ~ENSELO); 2089 ahd_flush_device_writes(ahd); 2090 ahd_outb(ahd, CLRSINT0, CLRSELDO); 2091 2092 /* 2093 * Return the LQO manager to its idle loop. It will 2094 * not do this automatically if the busfree occurs 2095 * after the first REQ of either the LQ or command 2096 * packet or between the LQ and command packet. 2097 */ 2098 ahd_outb(ahd, LQCTL2, ahd_inb(ahd, LQCTL2) | LQOTOIDLE); 2099 2100 /* 2101 * Update the waiting for selection queue so 2102 * we restart on the correct SCB. 2103 */ 2104 waiting_h = ahd_inw(ahd, WAITING_TID_HEAD); 2105 saved_scbptr = ahd_get_scbptr(ahd); 2106 if (waiting_h != scbid) { 2107 2108 ahd_outw(ahd, WAITING_TID_HEAD, scbid); 2109 waiting_t = ahd_inw(ahd, WAITING_TID_TAIL); 2110 if (waiting_t == waiting_h) { 2111 ahd_outw(ahd, WAITING_TID_TAIL, scbid); 2112 next = SCB_LIST_NULL; 2113 } else { 2114 ahd_set_scbptr(ahd, waiting_h); 2115 next = ahd_inw_scbram(ahd, SCB_NEXT2); 2116 } 2117 ahd_set_scbptr(ahd, scbid); 2118 ahd_outw(ahd, SCB_NEXT2, next); 2119 } 2120 ahd_set_scbptr(ahd, saved_scbptr); 2121 if (scb->crc_retry_count < AHD_MAX_LQ_CRC_ERRORS) { 2122 if (SCB_IS_SILENT(scb) == FALSE) { 2123 ahd_print_path(ahd, scb); 2124 printf("Probable outgoing LQ CRC error. " 2125 "Retrying command\n"); 2126 AHD_CORRECTABLE_ERROR(ahd); 2127 } 2128 scb->crc_retry_count++; 2129 } else { 2130 aic_set_transaction_status(scb, CAM_UNCOR_PARITY); 2131 aic_freeze_scb(scb); 2132 ahd_freeze_devq(ahd, scb); 2133 } 2134 /* Return unpausing the sequencer. */ 2135 return (0); 2136 } else if ((ahd_inb(ahd, PERRDIAG) & PARITYERR) != 0) { 2137 /* 2138 * Ignore what are really parity errors that 2139 * occur on the last REQ of a free running 2140 * clock prior to going busfree. Some drives 2141 * do not properly active negate just before 2142 * going busfree resulting in a parity glitch. 2143 */ 2144 ahd_outb(ahd, CLRSINT1, CLRSCSIPERR|CLRBUSFREE); 2145 #ifdef AHD_DEBUG 2146 if ((ahd_debug & AHD_SHOW_MASKED_ERRORS) != 0) 2147 printf("%s: Parity on last REQ detected " 2148 "during busfree phase.\n", 2149 ahd_name(ahd)); 2150 #endif 2151 /* Return unpausing the sequencer. */ 2152 return (0); 2153 } 2154 if (ahd->src_mode != AHD_MODE_SCSI) { 2155 u_int scbid; 2156 struct scb *scb; 2157 2158 scbid = ahd_get_scbptr(ahd); 2159 scb = ahd_lookup_scb(ahd, scbid); 2160 ahd_print_path(ahd, scb); 2161 printf("Unexpected PKT busfree condition\n"); 2162 AHD_UNCORRECTABLE_ERROR(ahd); 2163 ahd_dump_card_state(ahd); 2164 ahd_abort_scbs(ahd, SCB_GET_TARGET(ahd, scb), 'A', 2165 SCB_GET_LUN(scb), SCB_GET_TAG(scb), 2166 ROLE_INITIATOR, CAM_UNEXP_BUSFREE); 2167 2168 /* Return restarting the sequencer. */ 2169 return (1); 2170 } 2171 printf("%s: Unexpected PKT busfree condition\n", ahd_name(ahd)); 2172 AHD_UNCORRECTABLE_ERROR(ahd); 2173 ahd_dump_card_state(ahd); 2174 /* Restart the sequencer. */ 2175 return (1); 2176 } 2177 2178 /* 2179 * Non-packetized unexpected or expected busfree. 2180 */ 2181 static int 2182 ahd_handle_nonpkt_busfree(struct ahd_softc *ahd) 2183 { 2184 struct ahd_devinfo devinfo; 2185 struct scb *scb; 2186 u_int lastphase; 2187 u_int saved_scsiid; 2188 u_int saved_lun; 2189 u_int target; 2190 u_int initiator_role_id; 2191 u_int scbid; 2192 u_int ppr_busfree; 2193 int printerror; 2194 2195 /* 2196 * Look at what phase we were last in. If its message out, 2197 * chances are pretty good that the busfree was in response 2198 * to one of our abort requests. 2199 */ 2200 lastphase = ahd_inb(ahd, LASTPHASE); 2201 saved_scsiid = ahd_inb(ahd, SAVED_SCSIID); 2202 saved_lun = ahd_inb(ahd, SAVED_LUN); 2203 target = SCSIID_TARGET(ahd, saved_scsiid); 2204 initiator_role_id = SCSIID_OUR_ID(saved_scsiid); 2205 ahd_compile_devinfo(&devinfo, initiator_role_id, 2206 target, saved_lun, 'A', ROLE_INITIATOR); 2207 printerror = 1; 2208 2209 scbid = ahd_get_scbptr(ahd); 2210 scb = ahd_lookup_scb(ahd, scbid); 2211 if (scb != NULL 2212 && (ahd_inb(ahd, SEQ_FLAGS) & NOT_IDENTIFIED) != 0) 2213 scb = NULL; 2214 2215 ppr_busfree = (ahd->msg_flags & MSG_FLAG_EXPECT_PPR_BUSFREE) != 0; 2216 if (lastphase == P_MESGOUT) { 2217 u_int tag; 2218 2219 tag = SCB_LIST_NULL; 2220 if (ahd_sent_msg(ahd, AHDMSG_1B, MSG_ABORT_TAG, TRUE) 2221 || ahd_sent_msg(ahd, AHDMSG_1B, MSG_ABORT, TRUE)) { 2222 int found; 2223 int sent_msg; 2224 2225 if (scb == NULL) { 2226 ahd_print_devinfo(ahd, &devinfo); 2227 printf("Abort for unidentified " 2228 "connection completed.\n"); 2229 /* restart the sequencer. */ 2230 return (1); 2231 } 2232 sent_msg = ahd->msgout_buf[ahd->msgout_index - 1]; 2233 ahd_print_path(ahd, scb); 2234 printf("SCB %d - Abort%s Completed.\n", 2235 SCB_GET_TAG(scb), 2236 sent_msg == MSG_ABORT_TAG ? "" : " Tag"); 2237 2238 if (sent_msg == MSG_ABORT_TAG) 2239 tag = SCB_GET_TAG(scb); 2240 2241 if ((scb->flags & SCB_CMDPHASE_ABORT) != 0) { 2242 /* 2243 * This abort is in response to an 2244 * unexpected switch to command phase 2245 * for a packetized connection. Since 2246 * the identify message was never sent, 2247 * "saved lun" is 0. We really want to 2248 * abort only the SCB that encountered 2249 * this error, which could have a different 2250 * lun. The SCB will be retried so the OS 2251 * will see the UA after renegotiating to 2252 * packetized. 2253 */ 2254 tag = SCB_GET_TAG(scb); 2255 saved_lun = scb->hscb->lun; 2256 } 2257 found = ahd_abort_scbs(ahd, target, 'A', saved_lun, 2258 tag, ROLE_INITIATOR, 2259 CAM_REQ_ABORTED); 2260 printf("found == 0x%x\n", found); 2261 printerror = 0; 2262 } else if (ahd_sent_msg(ahd, AHDMSG_1B, 2263 MSG_BUS_DEV_RESET, TRUE)) { 2264 #ifdef __FreeBSD__ 2265 /* 2266 * Don't mark the user's request for this BDR 2267 * as completing with CAM_BDR_SENT. CAM3 2268 * specifies CAM_REQ_CMP. 2269 */ 2270 if (scb != NULL 2271 && scb->io_ctx->ccb_h.func_code== XPT_RESET_DEV 2272 && ahd_match_scb(ahd, scb, target, 'A', 2273 CAM_LUN_WILDCARD, SCB_LIST_NULL, 2274 ROLE_INITIATOR)) 2275 aic_set_transaction_status(scb, CAM_REQ_CMP); 2276 #endif 2277 ahd_handle_devreset(ahd, &devinfo, CAM_LUN_WILDCARD, 2278 CAM_BDR_SENT, "Bus Device Reset", 2279 /*verbose_level*/0); 2280 printerror = 0; 2281 } else if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_PPR, FALSE) 2282 && ppr_busfree == 0) { 2283 struct ahd_initiator_tinfo *tinfo; 2284 struct ahd_tmode_tstate *tstate; 2285 2286 /* 2287 * PPR Rejected. 2288 * 2289 * If the previous negotiation was packetized, 2290 * this could be because the device has been 2291 * reset without our knowledge. Force our 2292 * current negotiation to async and retry the 2293 * negotiation. Otherwise retry the command 2294 * with non-ppr negotiation. 2295 */ 2296 #ifdef AHD_DEBUG 2297 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) 2298 printf("PPR negotiation rejected busfree.\n"); 2299 #endif 2300 tinfo = ahd_fetch_transinfo(ahd, devinfo.channel, 2301 devinfo.our_scsiid, 2302 devinfo.target, &tstate); 2303 if ((tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ)!=0) { 2304 ahd_set_width(ahd, &devinfo, 2305 MSG_EXT_WDTR_BUS_8_BIT, 2306 AHD_TRANS_CUR, 2307 /*paused*/TRUE); 2308 ahd_set_syncrate(ahd, &devinfo, 2309 /*period*/0, /*offset*/0, 2310 /*ppr_options*/0, 2311 AHD_TRANS_CUR, 2312 /*paused*/TRUE); 2313 /* 2314 * The expect PPR busfree handler below 2315 * will effect the retry and necessary 2316 * abort. 2317 */ 2318 } else { 2319 tinfo->curr.transport_version = 2; 2320 tinfo->goal.transport_version = 2; 2321 tinfo->goal.ppr_options = 0; 2322 /* 2323 * Remove any SCBs in the waiting for selection 2324 * queue that may also be for this target so 2325 * that command ordering is preserved. 2326 */ 2327 ahd_freeze_devq(ahd, scb); 2328 ahd_qinfifo_requeue_tail(ahd, scb); 2329 printerror = 0; 2330 } 2331 } else if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_WDTR, FALSE) 2332 && ppr_busfree == 0) { 2333 /* 2334 * Negotiation Rejected. Go-narrow and 2335 * retry command. 2336 */ 2337 #ifdef AHD_DEBUG 2338 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) 2339 printf("WDTR negotiation rejected busfree.\n"); 2340 #endif 2341 ahd_set_width(ahd, &devinfo, 2342 MSG_EXT_WDTR_BUS_8_BIT, 2343 AHD_TRANS_CUR|AHD_TRANS_GOAL, 2344 /*paused*/TRUE); 2345 /* 2346 * Remove any SCBs in the waiting for selection 2347 * queue that may also be for this target so that 2348 * command ordering is preserved. 2349 */ 2350 ahd_freeze_devq(ahd, scb); 2351 ahd_qinfifo_requeue_tail(ahd, scb); 2352 printerror = 0; 2353 } else if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_SDTR, FALSE) 2354 && ppr_busfree == 0) { 2355 /* 2356 * Negotiation Rejected. Go-async and 2357 * retry command. 2358 */ 2359 #ifdef AHD_DEBUG 2360 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) 2361 printf("SDTR negotiation rejected busfree.\n"); 2362 #endif 2363 ahd_set_syncrate(ahd, &devinfo, 2364 /*period*/0, /*offset*/0, 2365 /*ppr_options*/0, 2366 AHD_TRANS_CUR|AHD_TRANS_GOAL, 2367 /*paused*/TRUE); 2368 /* 2369 * Remove any SCBs in the waiting for selection 2370 * queue that may also be for this target so that 2371 * command ordering is preserved. 2372 */ 2373 ahd_freeze_devq(ahd, scb); 2374 ahd_qinfifo_requeue_tail(ahd, scb); 2375 printerror = 0; 2376 } else if ((ahd->msg_flags & MSG_FLAG_EXPECT_IDE_BUSFREE) != 0 2377 && ahd_sent_msg(ahd, AHDMSG_1B, 2378 MSG_INITIATOR_DET_ERR, TRUE)) { 2379 2380 #ifdef AHD_DEBUG 2381 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) 2382 printf("Expected IDE Busfree\n"); 2383 #endif 2384 printerror = 0; 2385 } else if ((ahd->msg_flags & MSG_FLAG_EXPECT_QASREJ_BUSFREE) 2386 && ahd_sent_msg(ahd, AHDMSG_1B, 2387 MSG_MESSAGE_REJECT, TRUE)) { 2388 2389 #ifdef AHD_DEBUG 2390 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) 2391 printf("Expected QAS Reject Busfree\n"); 2392 #endif 2393 printerror = 0; 2394 } 2395 } 2396 2397 /* 2398 * The busfree required flag is honored at the end of 2399 * the message phases. We check it last in case we 2400 * had to send some other message that caused a busfree. 2401 */ 2402 if (printerror != 0 2403 && (lastphase == P_MESGIN || lastphase == P_MESGOUT) 2404 && ((ahd->msg_flags & MSG_FLAG_EXPECT_PPR_BUSFREE) != 0)) { 2405 2406 ahd_freeze_devq(ahd, scb); 2407 aic_set_transaction_status(scb, CAM_REQUEUE_REQ); 2408 aic_freeze_scb(scb); 2409 if ((ahd->msg_flags & MSG_FLAG_IU_REQ_CHANGED) != 0) { 2410 ahd_abort_scbs(ahd, SCB_GET_TARGET(ahd, scb), 2411 SCB_GET_CHANNEL(ahd, scb), 2412 SCB_GET_LUN(scb), SCB_LIST_NULL, 2413 ROLE_INITIATOR, CAM_REQ_ABORTED); 2414 } else { 2415 #ifdef AHD_DEBUG 2416 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) 2417 printf("PPR Negotiation Busfree.\n"); 2418 #endif 2419 ahd_done(ahd, scb); 2420 } 2421 printerror = 0; 2422 } 2423 if (printerror != 0) { 2424 int aborted; 2425 2426 aborted = 0; 2427 if (scb != NULL) { 2428 u_int tag; 2429 2430 if ((scb->hscb->control & TAG_ENB) != 0) 2431 tag = SCB_GET_TAG(scb); 2432 else 2433 tag = SCB_LIST_NULL; 2434 ahd_print_path(ahd, scb); 2435 aborted = ahd_abort_scbs(ahd, target, 'A', 2436 SCB_GET_LUN(scb), tag, 2437 ROLE_INITIATOR, 2438 CAM_UNEXP_BUSFREE); 2439 } else { 2440 /* 2441 * We had not fully identified this connection, 2442 * so we cannot abort anything. 2443 */ 2444 printf("%s: ", ahd_name(ahd)); 2445 } 2446 printf("Unexpected busfree %s, %d SCBs aborted, " 2447 "PRGMCNT == 0x%x\n", 2448 ahd_lookup_phase_entry(lastphase)->phasemsg, 2449 aborted, 2450 ahd_inw(ahd, PRGMCNT)); 2451 AHD_UNCORRECTABLE_ERROR(ahd); 2452 ahd_dump_card_state(ahd); 2453 if (lastphase != P_BUSFREE) 2454 ahd_force_renegotiation(ahd, &devinfo); 2455 } 2456 /* Always restart the sequencer. */ 2457 return (1); 2458 } 2459 2460 static void 2461 ahd_handle_proto_violation(struct ahd_softc *ahd) 2462 { 2463 struct ahd_devinfo devinfo; 2464 struct scb *scb; 2465 u_int scbid; 2466 u_int seq_flags; 2467 u_int curphase; 2468 u_int lastphase; 2469 int found; 2470 2471 ahd_fetch_devinfo(ahd, &devinfo); 2472 scbid = ahd_get_scbptr(ahd); 2473 scb = ahd_lookup_scb(ahd, scbid); 2474 seq_flags = ahd_inb(ahd, SEQ_FLAGS); 2475 curphase = ahd_inb(ahd, SCSISIGI) & PHASE_MASK; 2476 lastphase = ahd_inb(ahd, LASTPHASE); 2477 if ((seq_flags & NOT_IDENTIFIED) != 0) { 2478 2479 /* 2480 * The reconnecting target either did not send an 2481 * identify message, or did, but we didn't find an SCB 2482 * to match. 2483 */ 2484 ahd_print_devinfo(ahd, &devinfo); 2485 printf("Target did not send an IDENTIFY message. " 2486 "LASTPHASE = 0x%x.\n", lastphase); 2487 AHD_UNCORRECTABLE_ERROR(ahd); 2488 scb = NULL; 2489 } else if (scb == NULL) { 2490 /* 2491 * We don't seem to have an SCB active for this 2492 * transaction. Print an error and reset the bus. 2493 */ 2494 ahd_print_devinfo(ahd, &devinfo); 2495 printf("No SCB found during protocol violation\n"); 2496 AHD_UNCORRECTABLE_ERROR(ahd); 2497 goto proto_violation_reset; 2498 } else { 2499 aic_set_transaction_status(scb, CAM_SEQUENCE_FAIL); 2500 if ((seq_flags & NO_CDB_SENT) != 0) { 2501 ahd_print_path(ahd, scb); 2502 printf("No or incomplete CDB sent to device.\n"); 2503 AHD_UNCORRECTABLE_ERROR(ahd); 2504 } else if ((ahd_inb_scbram(ahd, SCB_CONTROL) 2505 & STATUS_RCVD) == 0) { 2506 /* 2507 * The target never bothered to provide status to 2508 * us prior to completing the command. Since we don't 2509 * know the disposition of this command, we must attempt 2510 * to abort it. Assert ATN and prepare to send an abort 2511 * message. 2512 */ 2513 ahd_print_path(ahd, scb); 2514 printf("Completed command without status.\n"); 2515 } else { 2516 ahd_print_path(ahd, scb); 2517 printf("Unknown protocol violation.\n"); 2518 AHD_UNCORRECTABLE_ERROR(ahd); 2519 ahd_dump_card_state(ahd); 2520 } 2521 } 2522 if ((lastphase & ~P_DATAIN_DT) == 0 2523 || lastphase == P_COMMAND) { 2524 proto_violation_reset: 2525 /* 2526 * Target either went directly to data 2527 * phase or didn't respond to our ATN. 2528 * The only safe thing to do is to blow 2529 * it away with a bus reset. 2530 */ 2531 found = ahd_reset_channel(ahd, 'A', TRUE); 2532 printf("%s: Issued Channel %c Bus Reset. " 2533 "%d SCBs aborted\n", ahd_name(ahd), 'A', found); 2534 AHD_UNCORRECTABLE_ERROR(ahd); 2535 } else { 2536 /* 2537 * Leave the selection hardware off in case 2538 * this abort attempt will affect yet to 2539 * be sent commands. 2540 */ 2541 ahd_outb(ahd, SCSISEQ0, 2542 ahd_inb(ahd, SCSISEQ0) & ~ENSELO); 2543 ahd_assert_atn(ahd); 2544 ahd_outb(ahd, MSG_OUT, HOST_MSG); 2545 if (scb == NULL) { 2546 ahd_print_devinfo(ahd, &devinfo); 2547 ahd->msgout_buf[0] = MSG_ABORT_TASK; 2548 ahd->msgout_len = 1; 2549 ahd->msgout_index = 0; 2550 ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT; 2551 } else { 2552 ahd_print_path(ahd, scb); 2553 scb->flags |= SCB_ABORT; 2554 } 2555 printf("Protocol violation %s. Attempting to abort.\n", 2556 ahd_lookup_phase_entry(curphase)->phasemsg); 2557 AHD_UNCORRECTABLE_ERROR(ahd); 2558 } 2559 } 2560 2561 /* 2562 * Force renegotiation to occur the next time we initiate 2563 * a command to the current device. 2564 */ 2565 static void 2566 ahd_force_renegotiation(struct ahd_softc *ahd, struct ahd_devinfo *devinfo) 2567 { 2568 struct ahd_initiator_tinfo *targ_info; 2569 struct ahd_tmode_tstate *tstate; 2570 2571 #ifdef AHD_DEBUG 2572 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) { 2573 ahd_print_devinfo(ahd, devinfo); 2574 printf("Forcing renegotiation\n"); 2575 } 2576 #endif 2577 targ_info = ahd_fetch_transinfo(ahd, 2578 devinfo->channel, 2579 devinfo->our_scsiid, 2580 devinfo->target, 2581 &tstate); 2582 ahd_update_neg_request(ahd, devinfo, tstate, 2583 targ_info, AHD_NEG_IF_NON_ASYNC); 2584 } 2585 2586 #define AHD_MAX_STEPS 2000 2587 void 2588 ahd_clear_critical_section(struct ahd_softc *ahd) 2589 { 2590 ahd_mode_state saved_modes; 2591 int stepping; 2592 int steps; 2593 int first_instr; 2594 u_int simode0; 2595 u_int simode1; 2596 u_int simode3; 2597 u_int lqimode0; 2598 u_int lqimode1; 2599 u_int lqomode0; 2600 u_int lqomode1; 2601 2602 if (ahd->num_critical_sections == 0) 2603 return; 2604 2605 stepping = FALSE; 2606 steps = 0; 2607 first_instr = 0; 2608 simode0 = 0; 2609 simode1 = 0; 2610 simode3 = 0; 2611 lqimode0 = 0; 2612 lqimode1 = 0; 2613 lqomode0 = 0; 2614 lqomode1 = 0; 2615 saved_modes = ahd_save_modes(ahd); 2616 for (;;) { 2617 struct cs *cs; 2618 u_int seqaddr; 2619 u_int i; 2620 2621 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI); 2622 seqaddr = ahd_inw(ahd, CURADDR); 2623 2624 cs = ahd->critical_sections; 2625 for (i = 0; i < ahd->num_critical_sections; i++, cs++) { 2626 2627 if (cs->begin < seqaddr && cs->end >= seqaddr) 2628 break; 2629 } 2630 2631 if (i == ahd->num_critical_sections) 2632 break; 2633 2634 if (steps > AHD_MAX_STEPS) { 2635 printf("%s: Infinite loop in critical section\n" 2636 "%s: First Instruction 0x%x now 0x%x\n", 2637 ahd_name(ahd), ahd_name(ahd), first_instr, 2638 seqaddr); 2639 AHD_FATAL_ERROR(ahd); 2640 ahd_dump_card_state(ahd); 2641 panic("critical section loop"); 2642 } 2643 2644 steps++; 2645 #ifdef AHD_DEBUG 2646 if ((ahd_debug & AHD_SHOW_MISC) != 0) 2647 printf("%s: Single stepping at 0x%x\n", ahd_name(ahd), 2648 seqaddr); 2649 #endif 2650 if (stepping == FALSE) { 2651 2652 first_instr = seqaddr; 2653 ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG); 2654 simode0 = ahd_inb(ahd, SIMODE0); 2655 simode3 = ahd_inb(ahd, SIMODE3); 2656 lqimode0 = ahd_inb(ahd, LQIMODE0); 2657 lqimode1 = ahd_inb(ahd, LQIMODE1); 2658 lqomode0 = ahd_inb(ahd, LQOMODE0); 2659 lqomode1 = ahd_inb(ahd, LQOMODE1); 2660 ahd_outb(ahd, SIMODE0, 0); 2661 ahd_outb(ahd, SIMODE3, 0); 2662 ahd_outb(ahd, LQIMODE0, 0); 2663 ahd_outb(ahd, LQIMODE1, 0); 2664 ahd_outb(ahd, LQOMODE0, 0); 2665 ahd_outb(ahd, LQOMODE1, 0); 2666 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI); 2667 simode1 = ahd_inb(ahd, SIMODE1); 2668 /* 2669 * We don't clear ENBUSFREE. Unfortunately 2670 * we cannot re-enable busfree detection within 2671 * the current connection, so we must leave it 2672 * on while single stepping. 2673 */ 2674 ahd_outb(ahd, SIMODE1, simode1 & ENBUSFREE); 2675 ahd_outb(ahd, SEQCTL0, ahd_inb(ahd, SEQCTL0) | STEP); 2676 stepping = TRUE; 2677 } 2678 ahd_outb(ahd, CLRSINT1, CLRBUSFREE); 2679 ahd_outb(ahd, CLRINT, CLRSCSIINT); 2680 ahd_set_modes(ahd, ahd->saved_src_mode, ahd->saved_dst_mode); 2681 ahd_outb(ahd, HCNTRL, ahd->unpause); 2682 while (!ahd_is_paused(ahd)) 2683 aic_delay(200); 2684 ahd_update_modes(ahd); 2685 } 2686 if (stepping) { 2687 ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG); 2688 ahd_outb(ahd, SIMODE0, simode0); 2689 ahd_outb(ahd, SIMODE3, simode3); 2690 ahd_outb(ahd, LQIMODE0, lqimode0); 2691 ahd_outb(ahd, LQIMODE1, lqimode1); 2692 ahd_outb(ahd, LQOMODE0, lqomode0); 2693 ahd_outb(ahd, LQOMODE1, lqomode1); 2694 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI); 2695 ahd_outb(ahd, SEQCTL0, ahd_inb(ahd, SEQCTL0) & ~STEP); 2696 ahd_outb(ahd, SIMODE1, simode1); 2697 /* 2698 * SCSIINT seems to glitch occassionally when 2699 * the interrupt masks are restored. Clear SCSIINT 2700 * one more time so that only persistent errors 2701 * are seen as a real interrupt. 2702 */ 2703 ahd_outb(ahd, CLRINT, CLRSCSIINT); 2704 } 2705 ahd_restore_modes(ahd, saved_modes); 2706 } 2707 2708 /* 2709 * Clear any pending interrupt status. 2710 */ 2711 void 2712 ahd_clear_intstat(struct ahd_softc *ahd) 2713 { 2714 AHD_ASSERT_MODES(ahd, ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK), 2715 ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK)); 2716 /* Clear any interrupt conditions this may have caused */ 2717 ahd_outb(ahd, CLRLQIINT0, CLRLQIATNQAS|CLRLQICRCT1|CLRLQICRCT2 2718 |CLRLQIBADLQT|CLRLQIATNLQ|CLRLQIATNCMD); 2719 ahd_outb(ahd, CLRLQIINT1, CLRLQIPHASE_LQ|CLRLQIPHASE_NLQ|CLRLIQABORT 2720 |CLRLQICRCI_LQ|CLRLQICRCI_NLQ|CLRLQIBADLQI 2721 |CLRLQIOVERI_LQ|CLRLQIOVERI_NLQ|CLRNONPACKREQ); 2722 ahd_outb(ahd, CLRLQOINT0, CLRLQOTARGSCBPERR|CLRLQOSTOPT2|CLRLQOATNLQ 2723 |CLRLQOATNPKT|CLRLQOTCRC); 2724 ahd_outb(ahd, CLRLQOINT1, CLRLQOINITSCBPERR|CLRLQOSTOPI2|CLRLQOBADQAS 2725 |CLRLQOBUSFREE|CLRLQOPHACHGINPKT); 2726 if ((ahd->bugs & AHD_CLRLQO_AUTOCLR_BUG) != 0) { 2727 ahd_outb(ahd, CLRLQOINT0, 0); 2728 ahd_outb(ahd, CLRLQOINT1, 0); 2729 } 2730 ahd_outb(ahd, CLRSINT3, CLRNTRAMPERR|CLROSRAMPERR); 2731 ahd_outb(ahd, CLRSINT1, CLRSELTIMEO|CLRATNO|CLRSCSIRSTI 2732 |CLRBUSFREE|CLRSCSIPERR|CLRREQINIT); 2733 ahd_outb(ahd, CLRSINT0, CLRSELDO|CLRSELDI|CLRSELINGO 2734 |CLRIOERR|CLROVERRUN); 2735 ahd_outb(ahd, CLRINT, CLRSCSIINT); 2736 } 2737 2738 /**************************** Debugging Routines ******************************/ 2739 #ifdef AHD_DEBUG 2740 uint32_t ahd_debug = AHD_DEBUG_OPTS; 2741 #endif 2742 void 2743 ahd_print_scb(struct scb *scb) 2744 { 2745 struct hardware_scb *hscb; 2746 int i; 2747 2748 hscb = scb->hscb; 2749 printf("scb:%p control:0x%x scsiid:0x%x lun:%d cdb_len:%d\n", 2750 (void *)scb, 2751 hscb->control, 2752 hscb->scsiid, 2753 hscb->lun, 2754 hscb->cdb_len); 2755 printf("Shared Data: "); 2756 for (i = 0; i < sizeof(hscb->shared_data.idata.cdb); i++) 2757 printf("%#02x", hscb->shared_data.idata.cdb[i]); 2758 printf(" dataptr:%#x%x datacnt:%#x sgptr:%#x tag:%#x\n", 2759 (uint32_t)((aic_le64toh(hscb->dataptr) >> 32) & 0xFFFFFFFF), 2760 (uint32_t)(aic_le64toh(hscb->dataptr) & 0xFFFFFFFF), 2761 aic_le32toh(hscb->datacnt), 2762 aic_le32toh(hscb->sgptr), 2763 SCB_GET_TAG(scb)); 2764 ahd_dump_sglist(scb); 2765 } 2766 2767 void 2768 ahd_dump_sglist(struct scb *scb) 2769 { 2770 int i; 2771 2772 if (scb->sg_count > 0) { 2773 if ((scb->ahd_softc->flags & AHD_64BIT_ADDRESSING) != 0) { 2774 struct ahd_dma64_seg *sg_list; 2775 2776 sg_list = (struct ahd_dma64_seg*)scb->sg_list; 2777 for (i = 0; i < scb->sg_count; i++) { 2778 uint64_t addr; 2779 2780 addr = aic_le64toh(sg_list[i].addr); 2781 printf("sg[%d] - Addr 0x%x%x : Length %d%s\n", 2782 i, 2783 (uint32_t)((addr >> 32) & 0xFFFFFFFF), 2784 (uint32_t)(addr & 0xFFFFFFFF), 2785 sg_list[i].len & AHD_SG_LEN_MASK, 2786 (sg_list[i].len & AHD_DMA_LAST_SEG) 2787 ? " Last" : ""); 2788 } 2789 } else { 2790 struct ahd_dma_seg *sg_list; 2791 2792 sg_list = (struct ahd_dma_seg*)scb->sg_list; 2793 for (i = 0; i < scb->sg_count; i++) { 2794 uint32_t len; 2795 2796 len = aic_le32toh(sg_list[i].len); 2797 printf("sg[%d] - Addr 0x%x%x : Length %d%s\n", 2798 i, 2799 (len & AHD_SG_HIGH_ADDR_MASK) >> 24, 2800 aic_le32toh(sg_list[i].addr), 2801 len & AHD_SG_LEN_MASK, 2802 len & AHD_DMA_LAST_SEG ? " Last" : ""); 2803 } 2804 } 2805 } 2806 } 2807 2808 /************************* Transfer Negotiation *******************************/ 2809 /* 2810 * Allocate per target mode instance (ID we respond to as a target) 2811 * transfer negotiation data structures. 2812 */ 2813 static struct ahd_tmode_tstate * 2814 ahd_alloc_tstate(struct ahd_softc *ahd, u_int scsi_id, char channel) 2815 { 2816 struct ahd_tmode_tstate *master_tstate; 2817 struct ahd_tmode_tstate *tstate; 2818 int i; 2819 2820 master_tstate = ahd->enabled_targets[ahd->our_id]; 2821 if (ahd->enabled_targets[scsi_id] != NULL 2822 && ahd->enabled_targets[scsi_id] != master_tstate) 2823 panic("%s: ahd_alloc_tstate - Target already allocated", 2824 ahd_name(ahd)); 2825 tstate = malloc(sizeof(*tstate), M_DEVBUF, M_NOWAIT); 2826 if (tstate == NULL) 2827 return (NULL); 2828 2829 /* 2830 * If we have allocated a master tstate, copy user settings from 2831 * the master tstate (taken from SRAM or the EEPROM) for this 2832 * channel, but reset our current and goal settings to async/narrow 2833 * until an initiator talks to us. 2834 */ 2835 if (master_tstate != NULL) { 2836 memcpy(tstate, master_tstate, sizeof(*tstate)); 2837 memset(tstate->enabled_luns, 0, sizeof(tstate->enabled_luns)); 2838 for (i = 0; i < 16; i++) { 2839 memset(&tstate->transinfo[i].curr, 0, 2840 sizeof(tstate->transinfo[i].curr)); 2841 memset(&tstate->transinfo[i].goal, 0, 2842 sizeof(tstate->transinfo[i].goal)); 2843 } 2844 } else 2845 memset(tstate, 0, sizeof(*tstate)); 2846 ahd->enabled_targets[scsi_id] = tstate; 2847 return (tstate); 2848 } 2849 2850 #ifdef AHD_TARGET_MODE 2851 /* 2852 * Free per target mode instance (ID we respond to as a target) 2853 * transfer negotiation data structures. 2854 */ 2855 static void 2856 ahd_free_tstate(struct ahd_softc *ahd, u_int scsi_id, char channel, int force) 2857 { 2858 struct ahd_tmode_tstate *tstate; 2859 2860 /* 2861 * Don't clean up our "master" tstate. 2862 * It has our default user settings. 2863 */ 2864 if (scsi_id == ahd->our_id 2865 && force == FALSE) 2866 return; 2867 2868 tstate = ahd->enabled_targets[scsi_id]; 2869 if (tstate != NULL) 2870 free(tstate, M_DEVBUF); 2871 ahd->enabled_targets[scsi_id] = NULL; 2872 } 2873 #endif 2874 2875 /* 2876 * Called when we have an active connection to a target on the bus, 2877 * this function finds the nearest period to the input period limited 2878 * by the capabilities of the bus connectivity of and sync settings for 2879 * the target. 2880 */ 2881 void 2882 ahd_devlimited_syncrate(struct ahd_softc *ahd, 2883 struct ahd_initiator_tinfo *tinfo, 2884 u_int *period, u_int *ppr_options, role_t role) 2885 { 2886 struct ahd_transinfo *transinfo; 2887 u_int maxsync; 2888 2889 if ((ahd_inb(ahd, SBLKCTL) & ENAB40) != 0 2890 && (ahd_inb(ahd, SSTAT2) & EXP_ACTIVE) == 0) { 2891 maxsync = AHD_SYNCRATE_PACED; 2892 } else { 2893 maxsync = AHD_SYNCRATE_ULTRA; 2894 /* Can't do DT related options on an SE bus */ 2895 *ppr_options &= MSG_EXT_PPR_QAS_REQ; 2896 } 2897 /* 2898 * Never allow a value higher than our current goal 2899 * period otherwise we may allow a target initiated 2900 * negotiation to go above the limit as set by the 2901 * user. In the case of an initiator initiated 2902 * sync negotiation, we limit based on the user 2903 * setting. This allows the system to still accept 2904 * incoming negotiations even if target initiated 2905 * negotiation is not performed. 2906 */ 2907 if (role == ROLE_TARGET) 2908 transinfo = &tinfo->user; 2909 else 2910 transinfo = &tinfo->goal; 2911 *ppr_options &= (transinfo->ppr_options|MSG_EXT_PPR_PCOMP_EN); 2912 if (transinfo->width == MSG_EXT_WDTR_BUS_8_BIT) { 2913 maxsync = MAX(maxsync, AHD_SYNCRATE_ULTRA2); 2914 *ppr_options &= ~MSG_EXT_PPR_DT_REQ; 2915 } 2916 if (transinfo->period == 0) { 2917 *period = 0; 2918 *ppr_options = 0; 2919 } else { 2920 *period = MAX(*period, transinfo->period); 2921 ahd_find_syncrate(ahd, period, ppr_options, maxsync); 2922 } 2923 } 2924 2925 /* 2926 * Look up the valid period to SCSIRATE conversion in our table. 2927 * Return the period and offset that should be sent to the target 2928 * if this was the beginning of an SDTR. 2929 */ 2930 void 2931 ahd_find_syncrate(struct ahd_softc *ahd, u_int *period, 2932 u_int *ppr_options, u_int maxsync) 2933 { 2934 if (*period < maxsync) 2935 *period = maxsync; 2936 2937 if ((*ppr_options & MSG_EXT_PPR_DT_REQ) != 0 2938 && *period > AHD_SYNCRATE_MIN_DT) 2939 *ppr_options &= ~MSG_EXT_PPR_DT_REQ; 2940 2941 if (*period > AHD_SYNCRATE_MIN) 2942 *period = 0; 2943 2944 /* Honor PPR option conformance rules. */ 2945 if (*period > AHD_SYNCRATE_PACED) 2946 *ppr_options &= ~MSG_EXT_PPR_RTI; 2947 2948 if ((*ppr_options & MSG_EXT_PPR_IU_REQ) == 0) 2949 *ppr_options &= (MSG_EXT_PPR_DT_REQ|MSG_EXT_PPR_QAS_REQ); 2950 2951 if ((*ppr_options & MSG_EXT_PPR_DT_REQ) == 0) 2952 *ppr_options &= MSG_EXT_PPR_QAS_REQ; 2953 2954 /* Skip all PACED only entries if IU is not available */ 2955 if ((*ppr_options & MSG_EXT_PPR_IU_REQ) == 0 2956 && *period < AHD_SYNCRATE_DT) 2957 *period = AHD_SYNCRATE_DT; 2958 2959 /* Skip all DT only entries if DT is not available */ 2960 if ((*ppr_options & MSG_EXT_PPR_DT_REQ) == 0 2961 && *period < AHD_SYNCRATE_ULTRA2) 2962 *period = AHD_SYNCRATE_ULTRA2; 2963 } 2964 2965 /* 2966 * Truncate the given synchronous offset to a value the 2967 * current adapter type and syncrate are capable of. 2968 */ 2969 void 2970 ahd_validate_offset(struct ahd_softc *ahd, 2971 struct ahd_initiator_tinfo *tinfo, 2972 u_int period, u_int *offset, int wide, 2973 role_t role) 2974 { 2975 u_int maxoffset; 2976 2977 /* Limit offset to what we can do */ 2978 if (period == 0) 2979 maxoffset = 0; 2980 else if (period <= AHD_SYNCRATE_PACED) { 2981 if ((ahd->bugs & AHD_PACED_NEGTABLE_BUG) != 0) 2982 maxoffset = MAX_OFFSET_PACED_BUG; 2983 else 2984 maxoffset = MAX_OFFSET_PACED; 2985 } else 2986 maxoffset = MAX_OFFSET_NON_PACED; 2987 *offset = MIN(*offset, maxoffset); 2988 if (tinfo != NULL) { 2989 if (role == ROLE_TARGET) 2990 *offset = MIN(*offset, tinfo->user.offset); 2991 else 2992 *offset = MIN(*offset, tinfo->goal.offset); 2993 } 2994 } 2995 2996 /* 2997 * Truncate the given transfer width parameter to a value the 2998 * current adapter type is capable of. 2999 */ 3000 void 3001 ahd_validate_width(struct ahd_softc *ahd, struct ahd_initiator_tinfo *tinfo, 3002 u_int *bus_width, role_t role) 3003 { 3004 switch (*bus_width) { 3005 default: 3006 if (ahd->features & AHD_WIDE) { 3007 /* Respond Wide */ 3008 *bus_width = MSG_EXT_WDTR_BUS_16_BIT; 3009 break; 3010 } 3011 /* FALLTHROUGH */ 3012 case MSG_EXT_WDTR_BUS_8_BIT: 3013 *bus_width = MSG_EXT_WDTR_BUS_8_BIT; 3014 break; 3015 } 3016 if (tinfo != NULL) { 3017 if (role == ROLE_TARGET) 3018 *bus_width = MIN(tinfo->user.width, *bus_width); 3019 else 3020 *bus_width = MIN(tinfo->goal.width, *bus_width); 3021 } 3022 } 3023 3024 /* 3025 * Update the bitmask of targets for which the controller should 3026 * negotiate with at the next convenient oportunity. This currently 3027 * means the next time we send the initial identify messages for 3028 * a new transaction. 3029 */ 3030 int 3031 ahd_update_neg_request(struct ahd_softc *ahd, struct ahd_devinfo *devinfo, 3032 struct ahd_tmode_tstate *tstate, 3033 struct ahd_initiator_tinfo *tinfo, ahd_neg_type neg_type) 3034 { 3035 u_int auto_negotiate_orig; 3036 3037 auto_negotiate_orig = tstate->auto_negotiate; 3038 if (neg_type == AHD_NEG_ALWAYS) { 3039 /* 3040 * Force our "current" settings to be 3041 * unknown so that unless a bus reset 3042 * occurs the need to renegotiate is 3043 * recorded persistently. 3044 */ 3045 if ((ahd->features & AHD_WIDE) != 0) 3046 tinfo->curr.width = AHD_WIDTH_UNKNOWN; 3047 tinfo->curr.period = AHD_PERIOD_UNKNOWN; 3048 tinfo->curr.offset = AHD_OFFSET_UNKNOWN; 3049 } 3050 if (tinfo->curr.period != tinfo->goal.period 3051 || tinfo->curr.width != tinfo->goal.width 3052 || tinfo->curr.offset != tinfo->goal.offset 3053 || tinfo->curr.ppr_options != tinfo->goal.ppr_options 3054 || (neg_type == AHD_NEG_IF_NON_ASYNC 3055 && (tinfo->goal.offset != 0 3056 || tinfo->goal.width != MSG_EXT_WDTR_BUS_8_BIT 3057 || tinfo->goal.ppr_options != 0))) 3058 tstate->auto_negotiate |= devinfo->target_mask; 3059 else 3060 tstate->auto_negotiate &= ~devinfo->target_mask; 3061 3062 return (auto_negotiate_orig != tstate->auto_negotiate); 3063 } 3064 3065 /* 3066 * Update the user/goal/curr tables of synchronous negotiation 3067 * parameters as well as, in the case of a current or active update, 3068 * any data structures on the host controller. In the case of an 3069 * active update, the specified target is currently talking to us on 3070 * the bus, so the transfer parameter update must take effect 3071 * immediately. 3072 */ 3073 void 3074 ahd_set_syncrate(struct ahd_softc *ahd, struct ahd_devinfo *devinfo, 3075 u_int period, u_int offset, u_int ppr_options, 3076 u_int type, int paused) 3077 { 3078 struct ahd_initiator_tinfo *tinfo; 3079 struct ahd_tmode_tstate *tstate; 3080 u_int old_period; 3081 u_int old_offset; 3082 u_int old_ppr; 3083 int active; 3084 int update_needed; 3085 3086 active = (type & AHD_TRANS_ACTIVE) == AHD_TRANS_ACTIVE; 3087 update_needed = 0; 3088 3089 if (period == 0 || offset == 0) { 3090 period = 0; 3091 offset = 0; 3092 } 3093 3094 tinfo = ahd_fetch_transinfo(ahd, devinfo->channel, devinfo->our_scsiid, 3095 devinfo->target, &tstate); 3096 3097 if ((type & AHD_TRANS_USER) != 0) { 3098 tinfo->user.period = period; 3099 tinfo->user.offset = offset; 3100 tinfo->user.ppr_options = ppr_options; 3101 } 3102 3103 if ((type & AHD_TRANS_GOAL) != 0) { 3104 tinfo->goal.period = period; 3105 tinfo->goal.offset = offset; 3106 tinfo->goal.ppr_options = ppr_options; 3107 } 3108 3109 old_period = tinfo->curr.period; 3110 old_offset = tinfo->curr.offset; 3111 old_ppr = tinfo->curr.ppr_options; 3112 3113 if ((type & AHD_TRANS_CUR) != 0 3114 && (old_period != period 3115 || old_offset != offset 3116 || old_ppr != ppr_options)) { 3117 3118 update_needed++; 3119 3120 tinfo->curr.period = period; 3121 tinfo->curr.offset = offset; 3122 tinfo->curr.ppr_options = ppr_options; 3123 3124 ahd_send_async(ahd, devinfo->channel, devinfo->target, 3125 CAM_LUN_WILDCARD, AC_TRANSFER_NEG, NULL); 3126 if (bootverbose) { 3127 if (offset != 0) { 3128 int options; 3129 3130 printf("%s: target %d synchronous with " 3131 "period = 0x%x, offset = 0x%x", 3132 ahd_name(ahd), devinfo->target, 3133 period, offset); 3134 options = 0; 3135 if ((ppr_options & MSG_EXT_PPR_RD_STRM) != 0) { 3136 printf("(RDSTRM"); 3137 options++; 3138 } 3139 if ((ppr_options & MSG_EXT_PPR_DT_REQ) != 0) { 3140 printf("%s", options ? "|DT" : "(DT"); 3141 options++; 3142 } 3143 if ((ppr_options & MSG_EXT_PPR_IU_REQ) != 0) { 3144 printf("%s", options ? "|IU" : "(IU"); 3145 options++; 3146 } 3147 if ((ppr_options & MSG_EXT_PPR_RTI) != 0) { 3148 printf("%s", options ? "|RTI" : "(RTI"); 3149 options++; 3150 } 3151 if ((ppr_options & MSG_EXT_PPR_QAS_REQ) != 0) { 3152 printf("%s", options ? "|QAS" : "(QAS"); 3153 options++; 3154 } 3155 if (options != 0) 3156 printf(")\n"); 3157 else 3158 printf("\n"); 3159 } else { 3160 printf("%s: target %d using " 3161 "asynchronous transfers%s\n", 3162 ahd_name(ahd), devinfo->target, 3163 (ppr_options & MSG_EXT_PPR_QAS_REQ) != 0 3164 ? "(QAS)" : ""); 3165 } 3166 } 3167 } 3168 /* 3169 * Always refresh the neg-table to handle the case of the 3170 * sequencer setting the ENATNO bit for a MK_MESSAGE request. 3171 * We will always renegotiate in that case if this is a 3172 * packetized request. Also manage the busfree expected flag 3173 * from this common routine so that we catch changes due to 3174 * WDTR or SDTR messages. 3175 */ 3176 if ((type & AHD_TRANS_CUR) != 0) { 3177 if (!paused) 3178 ahd_pause(ahd); 3179 ahd_update_neg_table(ahd, devinfo, &tinfo->curr); 3180 if (!paused) 3181 ahd_unpause(ahd); 3182 if (ahd->msg_type != MSG_TYPE_NONE) { 3183 if ((old_ppr & MSG_EXT_PPR_IU_REQ) 3184 != (ppr_options & MSG_EXT_PPR_IU_REQ)) { 3185 #ifdef AHD_DEBUG 3186 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) { 3187 ahd_print_devinfo(ahd, devinfo); 3188 printf("Expecting IU Change busfree\n"); 3189 } 3190 #endif 3191 ahd->msg_flags |= MSG_FLAG_EXPECT_PPR_BUSFREE 3192 | MSG_FLAG_IU_REQ_CHANGED; 3193 } 3194 if ((old_ppr & MSG_EXT_PPR_IU_REQ) != 0) { 3195 #ifdef AHD_DEBUG 3196 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) 3197 printf("PPR with IU_REQ outstanding\n"); 3198 #endif 3199 ahd->msg_flags |= MSG_FLAG_EXPECT_PPR_BUSFREE; 3200 } 3201 } 3202 } 3203 3204 update_needed += ahd_update_neg_request(ahd, devinfo, tstate, 3205 tinfo, AHD_NEG_TO_GOAL); 3206 3207 if (update_needed && active) 3208 ahd_update_pending_scbs(ahd); 3209 } 3210 3211 /* 3212 * Update the user/goal/curr tables of wide negotiation 3213 * parameters as well as, in the case of a current or active update, 3214 * any data structures on the host controller. In the case of an 3215 * active update, the specified target is currently talking to us on 3216 * the bus, so the transfer parameter update must take effect 3217 * immediately. 3218 */ 3219 void 3220 ahd_set_width(struct ahd_softc *ahd, struct ahd_devinfo *devinfo, 3221 u_int width, u_int type, int paused) 3222 { 3223 struct ahd_initiator_tinfo *tinfo; 3224 struct ahd_tmode_tstate *tstate; 3225 u_int oldwidth; 3226 int active; 3227 int update_needed; 3228 3229 active = (type & AHD_TRANS_ACTIVE) == AHD_TRANS_ACTIVE; 3230 update_needed = 0; 3231 tinfo = ahd_fetch_transinfo(ahd, devinfo->channel, devinfo->our_scsiid, 3232 devinfo->target, &tstate); 3233 3234 if ((type & AHD_TRANS_USER) != 0) 3235 tinfo->user.width = width; 3236 3237 if ((type & AHD_TRANS_GOAL) != 0) 3238 tinfo->goal.width = width; 3239 3240 oldwidth = tinfo->curr.width; 3241 if ((type & AHD_TRANS_CUR) != 0 && oldwidth != width) { 3242 3243 update_needed++; 3244 3245 tinfo->curr.width = width; 3246 ahd_send_async(ahd, devinfo->channel, devinfo->target, 3247 CAM_LUN_WILDCARD, AC_TRANSFER_NEG, NULL); 3248 if (bootverbose) { 3249 printf("%s: target %d using %dbit transfers\n", 3250 ahd_name(ahd), devinfo->target, 3251 8 * (0x01 << width)); 3252 } 3253 } 3254 3255 if ((type & AHD_TRANS_CUR) != 0) { 3256 if (!paused) 3257 ahd_pause(ahd); 3258 ahd_update_neg_table(ahd, devinfo, &tinfo->curr); 3259 if (!paused) 3260 ahd_unpause(ahd); 3261 } 3262 3263 update_needed += ahd_update_neg_request(ahd, devinfo, tstate, 3264 tinfo, AHD_NEG_TO_GOAL); 3265 if (update_needed && active) 3266 ahd_update_pending_scbs(ahd); 3267 3268 } 3269 3270 /* 3271 * Update the current state of tagged queuing for a given target. 3272 */ 3273 void 3274 ahd_set_tags(struct ahd_softc *ahd, struct ahd_devinfo *devinfo, 3275 ahd_queue_alg alg) 3276 { 3277 ahd_platform_set_tags(ahd, devinfo, alg); 3278 ahd_send_async(ahd, devinfo->channel, devinfo->target, 3279 devinfo->lun, AC_TRANSFER_NEG, &alg); 3280 } 3281 3282 static void 3283 ahd_update_neg_table(struct ahd_softc *ahd, struct ahd_devinfo *devinfo, 3284 struct ahd_transinfo *tinfo) 3285 { 3286 ahd_mode_state saved_modes; 3287 u_int period; 3288 u_int ppr_opts; 3289 u_int con_opts; 3290 u_int offset; 3291 u_int saved_negoaddr; 3292 uint8_t iocell_opts[sizeof(ahd->iocell_opts)]; 3293 3294 saved_modes = ahd_save_modes(ahd); 3295 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI); 3296 3297 saved_negoaddr = ahd_inb(ahd, NEGOADDR); 3298 ahd_outb(ahd, NEGOADDR, devinfo->target); 3299 period = tinfo->period; 3300 offset = tinfo->offset; 3301 memcpy(iocell_opts, ahd->iocell_opts, sizeof(ahd->iocell_opts)); 3302 ppr_opts = tinfo->ppr_options & (MSG_EXT_PPR_QAS_REQ|MSG_EXT_PPR_DT_REQ 3303 |MSG_EXT_PPR_IU_REQ|MSG_EXT_PPR_RTI); 3304 con_opts = 0; 3305 if (period == 0) 3306 period = AHD_SYNCRATE_ASYNC; 3307 if (period == AHD_SYNCRATE_160) { 3308 3309 if ((ahd->bugs & AHD_PACED_NEGTABLE_BUG) != 0) { 3310 /* 3311 * When the SPI4 spec was finalized, PACE transfers 3312 * was not made a configurable option in the PPR 3313 * message. Instead it is assumed to be enabled for 3314 * any syncrate faster than 80MHz. Nevertheless, 3315 * Harpoon2A4 allows this to be configurable. 3316 * 3317 * Harpoon2A4 also assumes at most 2 data bytes per 3318 * negotiated REQ/ACK offset. Paced transfers take 3319 * 4, so we must adjust our offset. 3320 */ 3321 ppr_opts |= PPROPT_PACE; 3322 offset *= 2; 3323 3324 /* 3325 * Harpoon2A assumed that there would be a 3326 * fallback rate between 160MHz and 80Mhz, 3327 * so 7 is used as the period factor rather 3328 * than 8 for 160MHz. 3329 */ 3330 period = AHD_SYNCRATE_REVA_160; 3331 } 3332 if ((tinfo->ppr_options & MSG_EXT_PPR_PCOMP_EN) == 0) 3333 iocell_opts[AHD_PRECOMP_SLEW_INDEX] &= 3334 ~AHD_PRECOMP_MASK; 3335 } else { 3336 /* 3337 * Precomp should be disabled for non-paced transfers. 3338 */ 3339 iocell_opts[AHD_PRECOMP_SLEW_INDEX] &= ~AHD_PRECOMP_MASK; 3340 3341 if ((ahd->features & AHD_NEW_IOCELL_OPTS) != 0 3342 && (ppr_opts & MSG_EXT_PPR_DT_REQ) != 0 3343 && (ppr_opts & MSG_EXT_PPR_IU_REQ) == 0) { 3344 /* 3345 * Slow down our CRC interval to be 3346 * compatible with non-packetized 3347 * U160 devices that can't handle a 3348 * CRC at full speed. 3349 */ 3350 con_opts |= ENSLOWCRC; 3351 } 3352 3353 if ((ahd->bugs & AHD_PACED_NEGTABLE_BUG) != 0) { 3354 /* 3355 * On H2A4, revert to a slower slewrate 3356 * on non-paced transfers. 3357 */ 3358 iocell_opts[AHD_PRECOMP_SLEW_INDEX] &= 3359 ~AHD_SLEWRATE_MASK; 3360 } 3361 } 3362 3363 ahd_outb(ahd, ANNEXCOL, AHD_ANNEXCOL_PRECOMP_SLEW); 3364 ahd_outb(ahd, ANNEXDAT, iocell_opts[AHD_PRECOMP_SLEW_INDEX]); 3365 ahd_outb(ahd, ANNEXCOL, AHD_ANNEXCOL_AMPLITUDE); 3366 ahd_outb(ahd, ANNEXDAT, iocell_opts[AHD_AMPLITUDE_INDEX]); 3367 3368 ahd_outb(ahd, NEGPERIOD, period); 3369 ahd_outb(ahd, NEGPPROPTS, ppr_opts); 3370 ahd_outb(ahd, NEGOFFSET, offset); 3371 3372 if (tinfo->width == MSG_EXT_WDTR_BUS_16_BIT) 3373 con_opts |= WIDEXFER; 3374 3375 /* 3376 * During packetized transfers, the target will 3377 * give us the oportunity to send command packets 3378 * without us asserting attention. 3379 */ 3380 if ((tinfo->ppr_options & MSG_EXT_PPR_IU_REQ) == 0) 3381 con_opts |= ENAUTOATNO; 3382 ahd_outb(ahd, NEGCONOPTS, con_opts); 3383 ahd_outb(ahd, NEGOADDR, saved_negoaddr); 3384 ahd_restore_modes(ahd, saved_modes); 3385 } 3386 3387 /* 3388 * When the transfer settings for a connection change, setup for 3389 * negotiation in pending SCBs to effect the change as quickly as 3390 * possible. We also cancel any negotiations that are scheduled 3391 * for inflight SCBs that have not been started yet. 3392 */ 3393 static void 3394 ahd_update_pending_scbs(struct ahd_softc *ahd) 3395 { 3396 struct scb *pending_scb; 3397 int pending_scb_count; 3398 int paused; 3399 u_int saved_scbptr; 3400 ahd_mode_state saved_modes; 3401 3402 /* 3403 * Traverse the pending SCB list and ensure that all of the 3404 * SCBs there have the proper settings. We can only safely 3405 * clear the negotiation required flag (setting requires the 3406 * execution queue to be modified) and this is only possible 3407 * if we are not already attempting to select out for this 3408 * SCB. For this reason, all callers only call this routine 3409 * if we are changing the negotiation settings for the currently 3410 * active transaction on the bus. 3411 */ 3412 pending_scb_count = 0; 3413 LIST_FOREACH(pending_scb, &ahd->pending_scbs, pending_links) { 3414 struct ahd_devinfo devinfo; 3415 struct ahd_tmode_tstate *tstate; 3416 3417 ahd_scb_devinfo(ahd, &devinfo, pending_scb); 3418 ahd_fetch_transinfo(ahd, devinfo.channel, 3419 devinfo.our_scsiid, 3420 devinfo.target, &tstate); 3421 if ((tstate->auto_negotiate & devinfo.target_mask) == 0 3422 && (pending_scb->flags & SCB_AUTO_NEGOTIATE) != 0) { 3423 pending_scb->flags &= ~SCB_AUTO_NEGOTIATE; 3424 pending_scb->hscb->control &= ~MK_MESSAGE; 3425 } 3426 ahd_sync_scb(ahd, pending_scb, 3427 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); 3428 pending_scb_count++; 3429 } 3430 3431 if (pending_scb_count == 0) 3432 return; 3433 3434 if (ahd_is_paused(ahd)) { 3435 paused = 1; 3436 } else { 3437 paused = 0; 3438 ahd_pause(ahd); 3439 } 3440 3441 /* 3442 * Force the sequencer to reinitialize the selection for 3443 * the command at the head of the execution queue if it 3444 * has already been setup. The negotiation changes may 3445 * effect whether we select-out with ATN. It is only 3446 * safe to clear ENSELO when the bus is not free and no 3447 * selection is in progres or completed. 3448 */ 3449 saved_modes = ahd_save_modes(ahd); 3450 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI); 3451 if ((ahd_inb(ahd, SCSISIGI) & BSYI) != 0 3452 && (ahd_inb(ahd, SSTAT0) & (SELDO|SELINGO)) == 0) 3453 ahd_outb(ahd, SCSISEQ0, ahd_inb(ahd, SCSISEQ0) & ~ENSELO); 3454 saved_scbptr = ahd_get_scbptr(ahd); 3455 /* Ensure that the hscbs down on the card match the new information */ 3456 LIST_FOREACH(pending_scb, &ahd->pending_scbs, pending_links) { 3457 u_int scb_tag; 3458 u_int control; 3459 3460 scb_tag = SCB_GET_TAG(pending_scb); 3461 ahd_set_scbptr(ahd, scb_tag); 3462 control = ahd_inb_scbram(ahd, SCB_CONTROL); 3463 control &= ~MK_MESSAGE; 3464 control |= pending_scb->hscb->control & MK_MESSAGE; 3465 ahd_outb(ahd, SCB_CONTROL, control); 3466 } 3467 ahd_set_scbptr(ahd, saved_scbptr); 3468 ahd_restore_modes(ahd, saved_modes); 3469 3470 if (paused == 0) 3471 ahd_unpause(ahd); 3472 } 3473 3474 /**************************** Pathing Information *****************************/ 3475 static void 3476 ahd_fetch_devinfo(struct ahd_softc *ahd, struct ahd_devinfo *devinfo) 3477 { 3478 ahd_mode_state saved_modes; 3479 u_int saved_scsiid; 3480 role_t role; 3481 int our_id; 3482 3483 saved_modes = ahd_save_modes(ahd); 3484 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI); 3485 3486 if (ahd_inb(ahd, SSTAT0) & TARGET) 3487 role = ROLE_TARGET; 3488 else 3489 role = ROLE_INITIATOR; 3490 3491 if (role == ROLE_TARGET 3492 && (ahd_inb(ahd, SEQ_FLAGS) & CMDPHASE_PENDING) != 0) { 3493 /* We were selected, so pull our id from TARGIDIN */ 3494 our_id = ahd_inb(ahd, TARGIDIN) & OID; 3495 } else if (role == ROLE_TARGET) 3496 our_id = ahd_inb(ahd, TOWNID); 3497 else 3498 our_id = ahd_inb(ahd, IOWNID); 3499 3500 saved_scsiid = ahd_inb(ahd, SAVED_SCSIID); 3501 ahd_compile_devinfo(devinfo, 3502 our_id, 3503 SCSIID_TARGET(ahd, saved_scsiid), 3504 ahd_inb(ahd, SAVED_LUN), 3505 SCSIID_CHANNEL(ahd, saved_scsiid), 3506 role); 3507 ahd_restore_modes(ahd, saved_modes); 3508 } 3509 3510 void 3511 ahd_print_devinfo(struct ahd_softc *ahd, struct ahd_devinfo *devinfo) 3512 { 3513 printf("%s:%c:%d:%d: ", ahd_name(ahd), 'A', 3514 devinfo->target, devinfo->lun); 3515 } 3516 3517 struct ahd_phase_table_entry* 3518 ahd_lookup_phase_entry(int phase) 3519 { 3520 struct ahd_phase_table_entry *entry; 3521 struct ahd_phase_table_entry *last_entry; 3522 3523 /* 3524 * num_phases doesn't include the default entry which 3525 * will be returned if the phase doesn't match. 3526 */ 3527 last_entry = &ahd_phase_table[num_phases]; 3528 for (entry = ahd_phase_table; entry < last_entry; entry++) { 3529 if (phase == entry->phase) 3530 break; 3531 } 3532 return (entry); 3533 } 3534 3535 void 3536 ahd_compile_devinfo(struct ahd_devinfo *devinfo, u_int our_id, u_int target, 3537 u_int lun, char channel, role_t role) 3538 { 3539 devinfo->our_scsiid = our_id; 3540 devinfo->target = target; 3541 devinfo->lun = lun; 3542 devinfo->target_offset = target; 3543 devinfo->channel = channel; 3544 devinfo->role = role; 3545 if (channel == 'B') 3546 devinfo->target_offset += 8; 3547 devinfo->target_mask = (0x01 << devinfo->target_offset); 3548 } 3549 3550 static void 3551 ahd_scb_devinfo(struct ahd_softc *ahd, struct ahd_devinfo *devinfo, 3552 struct scb *scb) 3553 { 3554 role_t role; 3555 int our_id; 3556 3557 our_id = SCSIID_OUR_ID(scb->hscb->scsiid); 3558 role = ROLE_INITIATOR; 3559 if ((scb->hscb->control & TARGET_SCB) != 0) 3560 role = ROLE_TARGET; 3561 ahd_compile_devinfo(devinfo, our_id, SCB_GET_TARGET(ahd, scb), 3562 SCB_GET_LUN(scb), SCB_GET_CHANNEL(ahd, scb), role); 3563 } 3564 3565 3566 /************************ Message Phase Processing ****************************/ 3567 /* 3568 * When an initiator transaction with the MK_MESSAGE flag either reconnects 3569 * or enters the initial message out phase, we are interrupted. Fill our 3570 * outgoing message buffer with the appropriate message and beging handing 3571 * the message phase(s) manually. 3572 */ 3573 static void 3574 ahd_setup_initiator_msgout(struct ahd_softc *ahd, struct ahd_devinfo *devinfo, 3575 struct scb *scb) 3576 { 3577 /* 3578 * To facilitate adding multiple messages together, 3579 * each routine should increment the index and len 3580 * variables instead of setting them explicitly. 3581 */ 3582 ahd->msgout_index = 0; 3583 ahd->msgout_len = 0; 3584 3585 if (ahd_currently_packetized(ahd)) 3586 ahd->msg_flags |= MSG_FLAG_PACKETIZED; 3587 3588 if (ahd->send_msg_perror 3589 && ahd_inb(ahd, MSG_OUT) == HOST_MSG) { 3590 ahd->msgout_buf[ahd->msgout_index++] = ahd->send_msg_perror; 3591 ahd->msgout_len++; 3592 ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT; 3593 #ifdef AHD_DEBUG 3594 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) 3595 printf("Setting up for Parity Error delivery\n"); 3596 #endif 3597 return; 3598 } else if (scb == NULL) { 3599 printf("%s: WARNING. No pending message for " 3600 "I_T msgin. Issuing NO-OP\n", ahd_name(ahd)); 3601 AHD_CORRECTABLE_ERROR(ahd); 3602 ahd->msgout_buf[ahd->msgout_index++] = MSG_NOOP; 3603 ahd->msgout_len++; 3604 ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT; 3605 return; 3606 } 3607 3608 if ((scb->flags & SCB_DEVICE_RESET) == 0 3609 && (scb->flags & SCB_PACKETIZED) == 0 3610 && ahd_inb(ahd, MSG_OUT) == MSG_IDENTIFYFLAG) { 3611 u_int identify_msg; 3612 3613 identify_msg = MSG_IDENTIFYFLAG | SCB_GET_LUN(scb); 3614 if ((scb->hscb->control & DISCENB) != 0) 3615 identify_msg |= MSG_IDENTIFY_DISCFLAG; 3616 ahd->msgout_buf[ahd->msgout_index++] = identify_msg; 3617 ahd->msgout_len++; 3618 3619 if ((scb->hscb->control & TAG_ENB) != 0) { 3620 ahd->msgout_buf[ahd->msgout_index++] = 3621 scb->hscb->control & (TAG_ENB|SCB_TAG_TYPE); 3622 ahd->msgout_buf[ahd->msgout_index++] = SCB_GET_TAG(scb); 3623 ahd->msgout_len += 2; 3624 } 3625 } 3626 3627 if (scb->flags & SCB_DEVICE_RESET) { 3628 ahd->msgout_buf[ahd->msgout_index++] = MSG_BUS_DEV_RESET; 3629 ahd->msgout_len++; 3630 ahd_print_path(ahd, scb); 3631 printf("Bus Device Reset Message Sent\n"); 3632 AHD_CORRECTABLE_ERROR(ahd); 3633 /* 3634 * Clear our selection hardware in advance of 3635 * the busfree. We may have an entry in the waiting 3636 * Q for this target, and we don't want to go about 3637 * selecting while we handle the busfree and blow it 3638 * away. 3639 */ 3640 ahd_outb(ahd, SCSISEQ0, 0); 3641 } else if ((scb->flags & SCB_ABORT) != 0) { 3642 3643 if ((scb->hscb->control & TAG_ENB) != 0) { 3644 ahd->msgout_buf[ahd->msgout_index++] = MSG_ABORT_TAG; 3645 } else { 3646 ahd->msgout_buf[ahd->msgout_index++] = MSG_ABORT; 3647 } 3648 ahd->msgout_len++; 3649 ahd_print_path(ahd, scb); 3650 printf("Abort%s Message Sent\n", 3651 (scb->hscb->control & TAG_ENB) != 0 ? " Tag" : ""); 3652 AHD_CORRECTABLE_ERROR(ahd); 3653 /* 3654 * Clear our selection hardware in advance of 3655 * the busfree. We may have an entry in the waiting 3656 * Q for this target, and we don't want to go about 3657 * selecting while we handle the busfree and blow it 3658 * away. 3659 */ 3660 ahd_outb(ahd, SCSISEQ0, 0); 3661 } else if ((scb->flags & (SCB_AUTO_NEGOTIATE|SCB_NEGOTIATE)) != 0) { 3662 ahd_build_transfer_msg(ahd, devinfo); 3663 /* 3664 * Clear our selection hardware in advance of potential 3665 * PPR IU status change busfree. We may have an entry in 3666 * the waiting Q for this target, and we don't want to go 3667 * about selecting while we handle the busfree and blow 3668 * it away. 3669 */ 3670 ahd_outb(ahd, SCSISEQ0, 0); 3671 } else { 3672 printf("ahd_intr: AWAITING_MSG for an SCB that " 3673 "does not have a waiting message\n"); 3674 printf("SCSIID = %x, target_mask = %x\n", scb->hscb->scsiid, 3675 devinfo->target_mask); 3676 AHD_FATAL_ERROR(ahd); 3677 panic("SCB = %d, SCB Control = %x:%x, MSG_OUT = %x " 3678 "SCB flags = %x", SCB_GET_TAG(scb), scb->hscb->control, 3679 ahd_inb_scbram(ahd, SCB_CONTROL), ahd_inb(ahd, MSG_OUT), 3680 scb->flags); 3681 } 3682 3683 /* 3684 * Clear the MK_MESSAGE flag from the SCB so we aren't 3685 * asked to send this message again. 3686 */ 3687 ahd_outb(ahd, SCB_CONTROL, 3688 ahd_inb_scbram(ahd, SCB_CONTROL) & ~MK_MESSAGE); 3689 scb->hscb->control &= ~MK_MESSAGE; 3690 ahd->msgout_index = 0; 3691 ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT; 3692 } 3693 3694 /* 3695 * Build an appropriate transfer negotiation message for the 3696 * currently active target. 3697 */ 3698 static void 3699 ahd_build_transfer_msg(struct ahd_softc *ahd, struct ahd_devinfo *devinfo) 3700 { 3701 /* 3702 * We need to initiate transfer negotiations. 3703 * If our current and goal settings are identical, 3704 * we want to renegotiate due to a check condition. 3705 */ 3706 struct ahd_initiator_tinfo *tinfo; 3707 struct ahd_tmode_tstate *tstate; 3708 int dowide; 3709 int dosync; 3710 int doppr; 3711 u_int period; 3712 u_int ppr_options; 3713 u_int offset; 3714 3715 tinfo = ahd_fetch_transinfo(ahd, devinfo->channel, devinfo->our_scsiid, 3716 devinfo->target, &tstate); 3717 /* 3718 * Filter our period based on the current connection. 3719 * If we can't perform DT transfers on this segment (not in LVD 3720 * mode for instance), then our decision to issue a PPR message 3721 * may change. 3722 */ 3723 period = tinfo->goal.period; 3724 offset = tinfo->goal.offset; 3725 ppr_options = tinfo->goal.ppr_options; 3726 /* Target initiated PPR is not allowed in the SCSI spec */ 3727 if (devinfo->role == ROLE_TARGET) 3728 ppr_options = 0; 3729 ahd_devlimited_syncrate(ahd, tinfo, &period, 3730 &ppr_options, devinfo->role); 3731 dowide = tinfo->curr.width != tinfo->goal.width; 3732 dosync = tinfo->curr.offset != offset || tinfo->curr.period != period; 3733 /* 3734 * Only use PPR if we have options that need it, even if the device 3735 * claims to support it. There might be an expander in the way 3736 * that doesn't. 3737 */ 3738 doppr = ppr_options != 0; 3739 3740 if (!dowide && !dosync && !doppr) { 3741 dowide = tinfo->goal.width != MSG_EXT_WDTR_BUS_8_BIT; 3742 dosync = tinfo->goal.offset != 0; 3743 } 3744 3745 if (!dowide && !dosync && !doppr) { 3746 /* 3747 * Force async with a WDTR message if we have a wide bus, 3748 * or just issue an SDTR with a 0 offset. 3749 */ 3750 if ((ahd->features & AHD_WIDE) != 0) 3751 dowide = 1; 3752 else 3753 dosync = 1; 3754 3755 if (bootverbose) { 3756 ahd_print_devinfo(ahd, devinfo); 3757 printf("Ensuring async\n"); 3758 } 3759 } 3760 /* Target initiated PPR is not allowed in the SCSI spec */ 3761 if (devinfo->role == ROLE_TARGET) 3762 doppr = 0; 3763 3764 /* 3765 * Both the PPR message and SDTR message require the 3766 * goal syncrate to be limited to what the target device 3767 * is capable of handling (based on whether an LVD->SE 3768 * expander is on the bus), so combine these two cases. 3769 * Regardless, guarantee that if we are using WDTR and SDTR 3770 * messages that WDTR comes first. 3771 */ 3772 if (doppr || (dosync && !dowide)) { 3773 3774 offset = tinfo->goal.offset; 3775 ahd_validate_offset(ahd, tinfo, period, &offset, 3776 doppr ? tinfo->goal.width 3777 : tinfo->curr.width, 3778 devinfo->role); 3779 if (doppr) { 3780 ahd_construct_ppr(ahd, devinfo, period, offset, 3781 tinfo->goal.width, ppr_options); 3782 } else { 3783 ahd_construct_sdtr(ahd, devinfo, period, offset); 3784 } 3785 } else { 3786 ahd_construct_wdtr(ahd, devinfo, tinfo->goal.width); 3787 } 3788 } 3789 3790 /* 3791 * Build a synchronous negotiation message in our message 3792 * buffer based on the input parameters. 3793 */ 3794 static void 3795 ahd_construct_sdtr(struct ahd_softc *ahd, struct ahd_devinfo *devinfo, 3796 u_int period, u_int offset) 3797 { 3798 if (offset == 0) 3799 period = AHD_ASYNC_XFER_PERIOD; 3800 ahd->msgout_buf[ahd->msgout_index++] = MSG_EXTENDED; 3801 ahd->msgout_buf[ahd->msgout_index++] = MSG_EXT_SDTR_LEN; 3802 ahd->msgout_buf[ahd->msgout_index++] = MSG_EXT_SDTR; 3803 ahd->msgout_buf[ahd->msgout_index++] = period; 3804 ahd->msgout_buf[ahd->msgout_index++] = offset; 3805 ahd->msgout_len += 5; 3806 if (bootverbose) { 3807 printf("(%s:%c:%d:%d): Sending SDTR period %x, offset %x\n", 3808 ahd_name(ahd), devinfo->channel, devinfo->target, 3809 devinfo->lun, period, offset); 3810 } 3811 } 3812 3813 /* 3814 * Build a wide negotiateion message in our message 3815 * buffer based on the input parameters. 3816 */ 3817 static void 3818 ahd_construct_wdtr(struct ahd_softc *ahd, struct ahd_devinfo *devinfo, 3819 u_int bus_width) 3820 { 3821 ahd->msgout_buf[ahd->msgout_index++] = MSG_EXTENDED; 3822 ahd->msgout_buf[ahd->msgout_index++] = MSG_EXT_WDTR_LEN; 3823 ahd->msgout_buf[ahd->msgout_index++] = MSG_EXT_WDTR; 3824 ahd->msgout_buf[ahd->msgout_index++] = bus_width; 3825 ahd->msgout_len += 4; 3826 if (bootverbose) { 3827 printf("(%s:%c:%d:%d): Sending WDTR %x\n", 3828 ahd_name(ahd), devinfo->channel, devinfo->target, 3829 devinfo->lun, bus_width); 3830 } 3831 } 3832 3833 /* 3834 * Build a parallel protocol request message in our message 3835 * buffer based on the input parameters. 3836 */ 3837 static void 3838 ahd_construct_ppr(struct ahd_softc *ahd, struct ahd_devinfo *devinfo, 3839 u_int period, u_int offset, u_int bus_width, 3840 u_int ppr_options) 3841 { 3842 /* 3843 * Always request precompensation from 3844 * the other target if we are running 3845 * at paced syncrates. 3846 */ 3847 if (period <= AHD_SYNCRATE_PACED) 3848 ppr_options |= MSG_EXT_PPR_PCOMP_EN; 3849 if (offset == 0) 3850 period = AHD_ASYNC_XFER_PERIOD; 3851 ahd->msgout_buf[ahd->msgout_index++] = MSG_EXTENDED; 3852 ahd->msgout_buf[ahd->msgout_index++] = MSG_EXT_PPR_LEN; 3853 ahd->msgout_buf[ahd->msgout_index++] = MSG_EXT_PPR; 3854 ahd->msgout_buf[ahd->msgout_index++] = period; 3855 ahd->msgout_buf[ahd->msgout_index++] = 0; 3856 ahd->msgout_buf[ahd->msgout_index++] = offset; 3857 ahd->msgout_buf[ahd->msgout_index++] = bus_width; 3858 ahd->msgout_buf[ahd->msgout_index++] = ppr_options; 3859 ahd->msgout_len += 8; 3860 if (bootverbose) { 3861 printf("(%s:%c:%d:%d): Sending PPR bus_width %x, period %x, " 3862 "offset %x, ppr_options %x\n", ahd_name(ahd), 3863 devinfo->channel, devinfo->target, devinfo->lun, 3864 bus_width, period, offset, ppr_options); 3865 } 3866 } 3867 3868 /* 3869 * Clear any active message state. 3870 */ 3871 static void 3872 ahd_clear_msg_state(struct ahd_softc *ahd) 3873 { 3874 ahd_mode_state saved_modes; 3875 3876 saved_modes = ahd_save_modes(ahd); 3877 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI); 3878 ahd->send_msg_perror = 0; 3879 ahd->msg_flags = MSG_FLAG_NONE; 3880 ahd->msgout_len = 0; 3881 ahd->msgin_index = 0; 3882 ahd->msg_type = MSG_TYPE_NONE; 3883 if ((ahd_inb(ahd, SCSISIGO) & ATNO) != 0) { 3884 /* 3885 * The target didn't care to respond to our 3886 * message request, so clear ATN. 3887 */ 3888 ahd_outb(ahd, CLRSINT1, CLRATNO); 3889 } 3890 ahd_outb(ahd, MSG_OUT, MSG_NOOP); 3891 ahd_outb(ahd, SEQ_FLAGS2, 3892 ahd_inb(ahd, SEQ_FLAGS2) & ~TARGET_MSG_PENDING); 3893 ahd_restore_modes(ahd, saved_modes); 3894 } 3895 3896 /* 3897 * Manual message loop handler. 3898 */ 3899 static void 3900 ahd_handle_message_phase(struct ahd_softc *ahd) 3901 { 3902 struct ahd_devinfo devinfo; 3903 u_int bus_phase; 3904 int end_session; 3905 3906 ahd_fetch_devinfo(ahd, &devinfo); 3907 end_session = FALSE; 3908 bus_phase = ahd_inb(ahd, LASTPHASE); 3909 3910 if ((ahd_inb(ahd, LQISTAT2) & LQIPHASE_OUTPKT) != 0) { 3911 printf("LQIRETRY for LQIPHASE_OUTPKT\n"); 3912 ahd_outb(ahd, LQCTL2, LQIRETRY); 3913 } 3914 reswitch: 3915 switch (ahd->msg_type) { 3916 case MSG_TYPE_INITIATOR_MSGOUT: 3917 { 3918 int lastbyte; 3919 int phasemis; 3920 int msgdone; 3921 3922 if (ahd->msgout_len == 0 && ahd->send_msg_perror == 0) 3923 panic("HOST_MSG_LOOP interrupt with no active message"); 3924 3925 #ifdef AHD_DEBUG 3926 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) { 3927 ahd_print_devinfo(ahd, &devinfo); 3928 printf("INITIATOR_MSG_OUT"); 3929 } 3930 #endif 3931 phasemis = bus_phase != P_MESGOUT; 3932 if (phasemis) { 3933 #ifdef AHD_DEBUG 3934 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) { 3935 printf(" PHASEMIS %s\n", 3936 ahd_lookup_phase_entry(bus_phase) 3937 ->phasemsg); 3938 } 3939 #endif 3940 if (bus_phase == P_MESGIN) { 3941 /* 3942 * Change gears and see if 3943 * this messages is of interest to 3944 * us or should be passed back to 3945 * the sequencer. 3946 */ 3947 ahd_outb(ahd, CLRSINT1, CLRATNO); 3948 ahd->send_msg_perror = 0; 3949 ahd->msg_type = MSG_TYPE_INITIATOR_MSGIN; 3950 ahd->msgin_index = 0; 3951 goto reswitch; 3952 } 3953 end_session = TRUE; 3954 break; 3955 } 3956 3957 if (ahd->send_msg_perror) { 3958 ahd_outb(ahd, CLRSINT1, CLRATNO); 3959 ahd_outb(ahd, CLRSINT1, CLRREQINIT); 3960 #ifdef AHD_DEBUG 3961 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) 3962 printf(" byte 0x%x\n", ahd->send_msg_perror); 3963 #endif 3964 /* 3965 * If we are notifying the target of a CRC error 3966 * during packetized operations, the target is 3967 * within its rights to acknowledge our message 3968 * with a busfree. 3969 */ 3970 if ((ahd->msg_flags & MSG_FLAG_PACKETIZED) != 0 3971 && ahd->send_msg_perror == MSG_INITIATOR_DET_ERR) 3972 ahd->msg_flags |= MSG_FLAG_EXPECT_IDE_BUSFREE; 3973 3974 ahd_outb(ahd, RETURN_2, ahd->send_msg_perror); 3975 ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_WRITE); 3976 break; 3977 } 3978 3979 msgdone = ahd->msgout_index == ahd->msgout_len; 3980 if (msgdone) { 3981 /* 3982 * The target has requested a retry. 3983 * Re-assert ATN, reset our message index to 3984 * 0, and try again. 3985 */ 3986 ahd->msgout_index = 0; 3987 ahd_assert_atn(ahd); 3988 } 3989 3990 lastbyte = ahd->msgout_index == (ahd->msgout_len - 1); 3991 if (lastbyte) { 3992 /* Last byte is signified by dropping ATN */ 3993 ahd_outb(ahd, CLRSINT1, CLRATNO); 3994 } 3995 3996 /* 3997 * Clear our interrupt status and present 3998 * the next byte on the bus. 3999 */ 4000 ahd_outb(ahd, CLRSINT1, CLRREQINIT); 4001 #ifdef AHD_DEBUG 4002 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) 4003 printf(" byte 0x%x\n", 4004 ahd->msgout_buf[ahd->msgout_index]); 4005 #endif 4006 ahd_outb(ahd, RETURN_2, ahd->msgout_buf[ahd->msgout_index++]); 4007 ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_WRITE); 4008 break; 4009 } 4010 case MSG_TYPE_INITIATOR_MSGIN: 4011 { 4012 int phasemis; 4013 int message_done; 4014 4015 #ifdef AHD_DEBUG 4016 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) { 4017 ahd_print_devinfo(ahd, &devinfo); 4018 printf("INITIATOR_MSG_IN"); 4019 } 4020 #endif 4021 phasemis = bus_phase != P_MESGIN; 4022 if (phasemis) { 4023 #ifdef AHD_DEBUG 4024 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) { 4025 printf(" PHASEMIS %s\n", 4026 ahd_lookup_phase_entry(bus_phase) 4027 ->phasemsg); 4028 } 4029 #endif 4030 ahd->msgin_index = 0; 4031 if (bus_phase == P_MESGOUT 4032 && (ahd->send_msg_perror != 0 4033 || (ahd->msgout_len != 0 4034 && ahd->msgout_index == 0))) { 4035 ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT; 4036 goto reswitch; 4037 } 4038 end_session = TRUE; 4039 break; 4040 } 4041 4042 /* Pull the byte in without acking it */ 4043 ahd->msgin_buf[ahd->msgin_index] = ahd_inb(ahd, SCSIBUS); 4044 #ifdef AHD_DEBUG 4045 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) 4046 printf(" byte 0x%x\n", 4047 ahd->msgin_buf[ahd->msgin_index]); 4048 #endif 4049 4050 message_done = ahd_parse_msg(ahd, &devinfo); 4051 4052 if (message_done) { 4053 /* 4054 * Clear our incoming message buffer in case there 4055 * is another message following this one. 4056 */ 4057 ahd->msgin_index = 0; 4058 4059 /* 4060 * If this message illicited a response, 4061 * assert ATN so the target takes us to the 4062 * message out phase. 4063 */ 4064 if (ahd->msgout_len != 0) { 4065 #ifdef AHD_DEBUG 4066 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) { 4067 ahd_print_devinfo(ahd, &devinfo); 4068 printf("Asserting ATN for response\n"); 4069 } 4070 #endif 4071 ahd_assert_atn(ahd); 4072 } 4073 } else 4074 ahd->msgin_index++; 4075 4076 if (message_done == MSGLOOP_TERMINATED) { 4077 end_session = TRUE; 4078 } else { 4079 /* Ack the byte */ 4080 ahd_outb(ahd, CLRSINT1, CLRREQINIT); 4081 ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_READ); 4082 } 4083 break; 4084 } 4085 case MSG_TYPE_TARGET_MSGIN: 4086 { 4087 int msgdone; 4088 int msgout_request; 4089 4090 /* 4091 * By default, the message loop will continue. 4092 */ 4093 ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_TARG); 4094 4095 if (ahd->msgout_len == 0) 4096 panic("Target MSGIN with no active message"); 4097 4098 /* 4099 * If we interrupted a mesgout session, the initiator 4100 * will not know this until our first REQ. So, we 4101 * only honor mesgout requests after we've sent our 4102 * first byte. 4103 */ 4104 if ((ahd_inb(ahd, SCSISIGI) & ATNI) != 0 4105 && ahd->msgout_index > 0) 4106 msgout_request = TRUE; 4107 else 4108 msgout_request = FALSE; 4109 4110 if (msgout_request) { 4111 4112 /* 4113 * Change gears and see if 4114 * this messages is of interest to 4115 * us or should be passed back to 4116 * the sequencer. 4117 */ 4118 ahd->msg_type = MSG_TYPE_TARGET_MSGOUT; 4119 ahd_outb(ahd, SCSISIGO, P_MESGOUT | BSYO); 4120 ahd->msgin_index = 0; 4121 /* Dummy read to REQ for first byte */ 4122 ahd_inb(ahd, SCSIDAT); 4123 ahd_outb(ahd, SXFRCTL0, 4124 ahd_inb(ahd, SXFRCTL0) | SPIOEN); 4125 break; 4126 } 4127 4128 msgdone = ahd->msgout_index == ahd->msgout_len; 4129 if (msgdone) { 4130 ahd_outb(ahd, SXFRCTL0, 4131 ahd_inb(ahd, SXFRCTL0) & ~SPIOEN); 4132 end_session = TRUE; 4133 break; 4134 } 4135 4136 /* 4137 * Present the next byte on the bus. 4138 */ 4139 ahd_outb(ahd, SXFRCTL0, ahd_inb(ahd, SXFRCTL0) | SPIOEN); 4140 ahd_outb(ahd, SCSIDAT, ahd->msgout_buf[ahd->msgout_index++]); 4141 break; 4142 } 4143 case MSG_TYPE_TARGET_MSGOUT: 4144 { 4145 int lastbyte; 4146 int msgdone; 4147 4148 /* 4149 * By default, the message loop will continue. 4150 */ 4151 ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_TARG); 4152 4153 /* 4154 * The initiator signals that this is 4155 * the last byte by dropping ATN. 4156 */ 4157 lastbyte = (ahd_inb(ahd, SCSISIGI) & ATNI) == 0; 4158 4159 /* 4160 * Read the latched byte, but turn off SPIOEN first 4161 * so that we don't inadvertently cause a REQ for the 4162 * next byte. 4163 */ 4164 ahd_outb(ahd, SXFRCTL0, ahd_inb(ahd, SXFRCTL0) & ~SPIOEN); 4165 ahd->msgin_buf[ahd->msgin_index] = ahd_inb(ahd, SCSIDAT); 4166 msgdone = ahd_parse_msg(ahd, &devinfo); 4167 if (msgdone == MSGLOOP_TERMINATED) { 4168 /* 4169 * The message is *really* done in that it caused 4170 * us to go to bus free. The sequencer has already 4171 * been reset at this point, so pull the ejection 4172 * handle. 4173 */ 4174 return; 4175 } 4176 4177 ahd->msgin_index++; 4178 4179 /* 4180 * XXX Read spec about initiator dropping ATN too soon 4181 * and use msgdone to detect it. 4182 */ 4183 if (msgdone == MSGLOOP_MSGCOMPLETE) { 4184 ahd->msgin_index = 0; 4185 4186 /* 4187 * If this message illicited a response, transition 4188 * to the Message in phase and send it. 4189 */ 4190 if (ahd->msgout_len != 0) { 4191 ahd_outb(ahd, SCSISIGO, P_MESGIN | BSYO); 4192 ahd_outb(ahd, SXFRCTL0, 4193 ahd_inb(ahd, SXFRCTL0) | SPIOEN); 4194 ahd->msg_type = MSG_TYPE_TARGET_MSGIN; 4195 ahd->msgin_index = 0; 4196 break; 4197 } 4198 } 4199 4200 if (lastbyte) 4201 end_session = TRUE; 4202 else { 4203 /* Ask for the next byte. */ 4204 ahd_outb(ahd, SXFRCTL0, 4205 ahd_inb(ahd, SXFRCTL0) | SPIOEN); 4206 } 4207 4208 break; 4209 } 4210 default: 4211 panic("Unknown REQINIT message type"); 4212 } 4213 4214 if (end_session) { 4215 if ((ahd->msg_flags & MSG_FLAG_PACKETIZED) != 0) { 4216 printf("%s: Returning to Idle Loop\n", 4217 ahd_name(ahd)); 4218 ahd_clear_msg_state(ahd); 4219 4220 /* 4221 * Perform the equivalent of a clear_target_state. 4222 */ 4223 ahd_outb(ahd, LASTPHASE, P_BUSFREE); 4224 ahd_outb(ahd, SEQ_FLAGS, NOT_IDENTIFIED|NO_CDB_SENT); 4225 ahd_outb(ahd, SEQCTL0, FASTMODE|SEQRESET); 4226 } else { 4227 ahd_clear_msg_state(ahd); 4228 ahd_outb(ahd, RETURN_1, EXIT_MSG_LOOP); 4229 } 4230 } 4231 } 4232 4233 /* 4234 * See if we sent a particular extended message to the target. 4235 * If "full" is true, return true only if the target saw the full 4236 * message. If "full" is false, return true if the target saw at 4237 * least the first byte of the message. 4238 */ 4239 static int 4240 ahd_sent_msg(struct ahd_softc *ahd, ahd_msgtype type, u_int msgval, int full) 4241 { 4242 int found; 4243 u_int index; 4244 4245 found = FALSE; 4246 index = 0; 4247 4248 while (index < ahd->msgout_len) { 4249 if (ahd->msgout_buf[index] == MSG_EXTENDED) { 4250 u_int end_index; 4251 4252 end_index = index + 1 + ahd->msgout_buf[index + 1]; 4253 if (ahd->msgout_buf[index+2] == msgval 4254 && type == AHDMSG_EXT) { 4255 4256 if (full) { 4257 if (ahd->msgout_index > end_index) 4258 found = TRUE; 4259 } else if (ahd->msgout_index > index) 4260 found = TRUE; 4261 } 4262 index = end_index; 4263 } else if (ahd->msgout_buf[index] >= MSG_SIMPLE_TASK 4264 && ahd->msgout_buf[index] <= MSG_IGN_WIDE_RESIDUE) { 4265 4266 /* Skip tag type and tag id or residue param*/ 4267 index += 2; 4268 } else { 4269 /* Single byte message */ 4270 if (type == AHDMSG_1B 4271 && ahd->msgout_index > index 4272 && (ahd->msgout_buf[index] == msgval 4273 || ((ahd->msgout_buf[index] & MSG_IDENTIFYFLAG) != 0 4274 && msgval == MSG_IDENTIFYFLAG))) 4275 found = TRUE; 4276 index++; 4277 } 4278 4279 if (found) 4280 break; 4281 } 4282 return (found); 4283 } 4284 4285 /* 4286 * Wait for a complete incoming message, parse it, and respond accordingly. 4287 */ 4288 static int 4289 ahd_parse_msg(struct ahd_softc *ahd, struct ahd_devinfo *devinfo) 4290 { 4291 struct ahd_initiator_tinfo *tinfo; 4292 struct ahd_tmode_tstate *tstate; 4293 int reject; 4294 int done; 4295 int response; 4296 4297 done = MSGLOOP_IN_PROG; 4298 response = FALSE; 4299 reject = FALSE; 4300 tinfo = ahd_fetch_transinfo(ahd, devinfo->channel, devinfo->our_scsiid, 4301 devinfo->target, &tstate); 4302 4303 /* 4304 * Parse as much of the message as is available, 4305 * rejecting it if we don't support it. When 4306 * the entire message is available and has been 4307 * handled, return MSGLOOP_MSGCOMPLETE, indicating 4308 * that we have parsed an entire message. 4309 * 4310 * In the case of extended messages, we accept the length 4311 * byte outright and perform more checking once we know the 4312 * extended message type. 4313 */ 4314 switch (ahd->msgin_buf[0]) { 4315 case MSG_DISCONNECT: 4316 case MSG_SAVEDATAPOINTER: 4317 case MSG_CMDCOMPLETE: 4318 case MSG_RESTOREPOINTERS: 4319 case MSG_IGN_WIDE_RESIDUE: 4320 /* 4321 * End our message loop as these are messages 4322 * the sequencer handles on its own. 4323 */ 4324 done = MSGLOOP_TERMINATED; 4325 break; 4326 case MSG_MESSAGE_REJECT: 4327 response = ahd_handle_msg_reject(ahd, devinfo); 4328 /* FALLTHROUGH */ 4329 case MSG_NOOP: 4330 done = MSGLOOP_MSGCOMPLETE; 4331 break; 4332 case MSG_EXTENDED: 4333 { 4334 /* Wait for enough of the message to begin validation */ 4335 if (ahd->msgin_index < 2) 4336 break; 4337 switch (ahd->msgin_buf[2]) { 4338 case MSG_EXT_SDTR: 4339 { 4340 u_int period; 4341 u_int ppr_options; 4342 u_int offset; 4343 u_int saved_offset; 4344 4345 if (ahd->msgin_buf[1] != MSG_EXT_SDTR_LEN) { 4346 reject = TRUE; 4347 break; 4348 } 4349 4350 /* 4351 * Wait until we have both args before validating 4352 * and acting on this message. 4353 * 4354 * Add one to MSG_EXT_SDTR_LEN to account for 4355 * the extended message preamble. 4356 */ 4357 if (ahd->msgin_index < (MSG_EXT_SDTR_LEN + 1)) 4358 break; 4359 4360 period = ahd->msgin_buf[3]; 4361 ppr_options = 0; 4362 saved_offset = offset = ahd->msgin_buf[4]; 4363 ahd_devlimited_syncrate(ahd, tinfo, &period, 4364 &ppr_options, devinfo->role); 4365 ahd_validate_offset(ahd, tinfo, period, &offset, 4366 tinfo->curr.width, devinfo->role); 4367 if (bootverbose) { 4368 printf("(%s:%c:%d:%d): Received " 4369 "SDTR period %x, offset %x\n\t" 4370 "Filtered to period %x, offset %x\n", 4371 ahd_name(ahd), devinfo->channel, 4372 devinfo->target, devinfo->lun, 4373 ahd->msgin_buf[3], saved_offset, 4374 period, offset); 4375 } 4376 ahd_set_syncrate(ahd, devinfo, period, 4377 offset, ppr_options, 4378 AHD_TRANS_ACTIVE|AHD_TRANS_GOAL, 4379 /*paused*/TRUE); 4380 4381 /* 4382 * See if we initiated Sync Negotiation 4383 * and didn't have to fall down to async 4384 * transfers. 4385 */ 4386 if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_SDTR, TRUE)) { 4387 /* We started it */ 4388 if (saved_offset != offset) { 4389 /* Went too low - force async */ 4390 reject = TRUE; 4391 } 4392 } else { 4393 /* 4394 * Send our own SDTR in reply 4395 */ 4396 if (bootverbose 4397 && devinfo->role == ROLE_INITIATOR) { 4398 printf("(%s:%c:%d:%d): Target " 4399 "Initiated SDTR\n", 4400 ahd_name(ahd), devinfo->channel, 4401 devinfo->target, devinfo->lun); 4402 } 4403 ahd->msgout_index = 0; 4404 ahd->msgout_len = 0; 4405 ahd_construct_sdtr(ahd, devinfo, 4406 period, offset); 4407 ahd->msgout_index = 0; 4408 response = TRUE; 4409 } 4410 done = MSGLOOP_MSGCOMPLETE; 4411 break; 4412 } 4413 case MSG_EXT_WDTR: 4414 { 4415 u_int bus_width; 4416 u_int saved_width; 4417 u_int sending_reply; 4418 4419 sending_reply = FALSE; 4420 if (ahd->msgin_buf[1] != MSG_EXT_WDTR_LEN) { 4421 reject = TRUE; 4422 break; 4423 } 4424 4425 /* 4426 * Wait until we have our arg before validating 4427 * and acting on this message. 4428 * 4429 * Add one to MSG_EXT_WDTR_LEN to account for 4430 * the extended message preamble. 4431 */ 4432 if (ahd->msgin_index < (MSG_EXT_WDTR_LEN + 1)) 4433 break; 4434 4435 bus_width = ahd->msgin_buf[3]; 4436 saved_width = bus_width; 4437 ahd_validate_width(ahd, tinfo, &bus_width, 4438 devinfo->role); 4439 if (bootverbose) { 4440 printf("(%s:%c:%d:%d): Received WDTR " 4441 "%x filtered to %x\n", 4442 ahd_name(ahd), devinfo->channel, 4443 devinfo->target, devinfo->lun, 4444 saved_width, bus_width); 4445 } 4446 4447 if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_WDTR, TRUE)) { 4448 /* 4449 * Don't send a WDTR back to the 4450 * target, since we asked first. 4451 * If the width went higher than our 4452 * request, reject it. 4453 */ 4454 if (saved_width > bus_width) { 4455 reject = TRUE; 4456 printf("(%s:%c:%d:%d): requested %dBit " 4457 "transfers. Rejecting...\n", 4458 ahd_name(ahd), devinfo->channel, 4459 devinfo->target, devinfo->lun, 4460 8 * (0x01 << bus_width)); 4461 bus_width = 0; 4462 } 4463 } else { 4464 /* 4465 * Send our own WDTR in reply 4466 */ 4467 if (bootverbose 4468 && devinfo->role == ROLE_INITIATOR) { 4469 printf("(%s:%c:%d:%d): Target " 4470 "Initiated WDTR\n", 4471 ahd_name(ahd), devinfo->channel, 4472 devinfo->target, devinfo->lun); 4473 } 4474 ahd->msgout_index = 0; 4475 ahd->msgout_len = 0; 4476 ahd_construct_wdtr(ahd, devinfo, bus_width); 4477 ahd->msgout_index = 0; 4478 response = TRUE; 4479 sending_reply = TRUE; 4480 } 4481 /* 4482 * After a wide message, we are async, but 4483 * some devices don't seem to honor this portion 4484 * of the spec. Force a renegotiation of the 4485 * sync component of our transfer agreement even 4486 * if our goal is async. By updating our width 4487 * after forcing the negotiation, we avoid 4488 * renegotiating for width. 4489 */ 4490 ahd_update_neg_request(ahd, devinfo, tstate, 4491 tinfo, AHD_NEG_ALWAYS); 4492 ahd_set_width(ahd, devinfo, bus_width, 4493 AHD_TRANS_ACTIVE|AHD_TRANS_GOAL, 4494 /*paused*/TRUE); 4495 if (sending_reply == FALSE && reject == FALSE) { 4496 4497 /* 4498 * We will always have an SDTR to send. 4499 */ 4500 ahd->msgout_index = 0; 4501 ahd->msgout_len = 0; 4502 ahd_build_transfer_msg(ahd, devinfo); 4503 ahd->msgout_index = 0; 4504 response = TRUE; 4505 } 4506 done = MSGLOOP_MSGCOMPLETE; 4507 break; 4508 } 4509 case MSG_EXT_PPR: 4510 { 4511 u_int period; 4512 u_int offset; 4513 u_int bus_width; 4514 u_int ppr_options; 4515 u_int saved_width; 4516 u_int saved_offset; 4517 u_int saved_ppr_options; 4518 4519 if (ahd->msgin_buf[1] != MSG_EXT_PPR_LEN) { 4520 reject = TRUE; 4521 break; 4522 } 4523 4524 /* 4525 * Wait until we have all args before validating 4526 * and acting on this message. 4527 * 4528 * Add one to MSG_EXT_PPR_LEN to account for 4529 * the extended message preamble. 4530 */ 4531 if (ahd->msgin_index < (MSG_EXT_PPR_LEN + 1)) 4532 break; 4533 4534 period = ahd->msgin_buf[3]; 4535 offset = ahd->msgin_buf[5]; 4536 bus_width = ahd->msgin_buf[6]; 4537 saved_width = bus_width; 4538 ppr_options = ahd->msgin_buf[7]; 4539 /* 4540 * According to the spec, a DT only 4541 * period factor with no DT option 4542 * set implies async. 4543 */ 4544 if ((ppr_options & MSG_EXT_PPR_DT_REQ) == 0 4545 && period <= 9) 4546 offset = 0; 4547 saved_ppr_options = ppr_options; 4548 saved_offset = offset; 4549 4550 /* 4551 * Transfer options are only available if we 4552 * are negotiating wide. 4553 */ 4554 if (bus_width == 0) 4555 ppr_options &= MSG_EXT_PPR_QAS_REQ; 4556 4557 ahd_validate_width(ahd, tinfo, &bus_width, 4558 devinfo->role); 4559 ahd_devlimited_syncrate(ahd, tinfo, &period, 4560 &ppr_options, devinfo->role); 4561 ahd_validate_offset(ahd, tinfo, period, &offset, 4562 bus_width, devinfo->role); 4563 4564 if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_PPR, TRUE)) { 4565 /* 4566 * If we are unable to do any of the 4567 * requested options (we went too low), 4568 * then we'll have to reject the message. 4569 */ 4570 if (saved_width > bus_width 4571 || saved_offset != offset 4572 || saved_ppr_options != ppr_options) { 4573 reject = TRUE; 4574 period = 0; 4575 offset = 0; 4576 bus_width = 0; 4577 ppr_options = 0; 4578 } 4579 } else { 4580 if (devinfo->role != ROLE_TARGET) 4581 printf("(%s:%c:%d:%d): Target " 4582 "Initiated PPR\n", 4583 ahd_name(ahd), devinfo->channel, 4584 devinfo->target, devinfo->lun); 4585 else 4586 printf("(%s:%c:%d:%d): Initiator " 4587 "Initiated PPR\n", 4588 ahd_name(ahd), devinfo->channel, 4589 devinfo->target, devinfo->lun); 4590 ahd->msgout_index = 0; 4591 ahd->msgout_len = 0; 4592 ahd_construct_ppr(ahd, devinfo, period, offset, 4593 bus_width, ppr_options); 4594 ahd->msgout_index = 0; 4595 response = TRUE; 4596 } 4597 if (bootverbose) { 4598 printf("(%s:%c:%d:%d): Received PPR width %x, " 4599 "period %x, offset %x,options %x\n" 4600 "\tFiltered to width %x, period %x, " 4601 "offset %x, options %x\n", 4602 ahd_name(ahd), devinfo->channel, 4603 devinfo->target, devinfo->lun, 4604 saved_width, ahd->msgin_buf[3], 4605 saved_offset, saved_ppr_options, 4606 bus_width, period, offset, ppr_options); 4607 } 4608 ahd_set_width(ahd, devinfo, bus_width, 4609 AHD_TRANS_ACTIVE|AHD_TRANS_GOAL, 4610 /*paused*/TRUE); 4611 ahd_set_syncrate(ahd, devinfo, period, 4612 offset, ppr_options, 4613 AHD_TRANS_ACTIVE|AHD_TRANS_GOAL, 4614 /*paused*/TRUE); 4615 4616 done = MSGLOOP_MSGCOMPLETE; 4617 break; 4618 } 4619 default: 4620 /* Unknown extended message. Reject it. */ 4621 reject = TRUE; 4622 break; 4623 } 4624 break; 4625 } 4626 #ifdef AHD_TARGET_MODE 4627 case MSG_BUS_DEV_RESET: 4628 ahd_handle_devreset(ahd, devinfo, CAM_LUN_WILDCARD, 4629 CAM_BDR_SENT, 4630 "Bus Device Reset Received", 4631 /*verbose_level*/0); 4632 ahd_restart(ahd); 4633 done = MSGLOOP_TERMINATED; 4634 break; 4635 case MSG_ABORT_TAG: 4636 case MSG_ABORT: 4637 case MSG_CLEAR_QUEUE: 4638 { 4639 int tag; 4640 4641 /* Target mode messages */ 4642 if (devinfo->role != ROLE_TARGET) { 4643 reject = TRUE; 4644 break; 4645 } 4646 tag = SCB_LIST_NULL; 4647 if (ahd->msgin_buf[0] == MSG_ABORT_TAG) 4648 tag = ahd_inb(ahd, INITIATOR_TAG); 4649 ahd_abort_scbs(ahd, devinfo->target, devinfo->channel, 4650 devinfo->lun, tag, ROLE_TARGET, 4651 CAM_REQ_ABORTED); 4652 4653 tstate = ahd->enabled_targets[devinfo->our_scsiid]; 4654 if (tstate != NULL) { 4655 struct ahd_tmode_lstate* lstate; 4656 4657 lstate = tstate->enabled_luns[devinfo->lun]; 4658 if (lstate != NULL) { 4659 ahd_queue_lstate_event(ahd, lstate, 4660 devinfo->our_scsiid, 4661 ahd->msgin_buf[0], 4662 /*arg*/tag); 4663 ahd_send_lstate_events(ahd, lstate); 4664 } 4665 } 4666 ahd_restart(ahd); 4667 done = MSGLOOP_TERMINATED; 4668 break; 4669 } 4670 #endif 4671 case MSG_QAS_REQUEST: 4672 #ifdef AHD_DEBUG 4673 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) 4674 printf("%s: QAS request. SCSISIGI == 0x%x\n", 4675 ahd_name(ahd), ahd_inb(ahd, SCSISIGI)); 4676 #endif 4677 ahd->msg_flags |= MSG_FLAG_EXPECT_QASREJ_BUSFREE; 4678 /* FALLTHROUGH */ 4679 case MSG_TERM_IO_PROC: 4680 default: 4681 reject = TRUE; 4682 break; 4683 } 4684 4685 if (reject) { 4686 /* 4687 * Setup to reject the message. 4688 */ 4689 ahd->msgout_index = 0; 4690 ahd->msgout_len = 1; 4691 ahd->msgout_buf[0] = MSG_MESSAGE_REJECT; 4692 done = MSGLOOP_MSGCOMPLETE; 4693 response = TRUE; 4694 } 4695 4696 if (done != MSGLOOP_IN_PROG && !response) 4697 /* Clear the outgoing message buffer */ 4698 ahd->msgout_len = 0; 4699 4700 return (done); 4701 } 4702 4703 /* 4704 * Process a message reject message. 4705 */ 4706 static int 4707 ahd_handle_msg_reject(struct ahd_softc *ahd, struct ahd_devinfo *devinfo) 4708 { 4709 /* 4710 * What we care about here is if we had an 4711 * outstanding SDTR or WDTR message for this 4712 * target. If we did, this is a signal that 4713 * the target is refusing negotiation. 4714 */ 4715 struct scb *scb; 4716 struct ahd_initiator_tinfo *tinfo; 4717 struct ahd_tmode_tstate *tstate; 4718 u_int scb_index; 4719 u_int last_msg; 4720 int response = 0; 4721 4722 scb_index = ahd_get_scbptr(ahd); 4723 scb = ahd_lookup_scb(ahd, scb_index); 4724 tinfo = ahd_fetch_transinfo(ahd, devinfo->channel, 4725 devinfo->our_scsiid, 4726 devinfo->target, &tstate); 4727 /* Might be necessary */ 4728 last_msg = ahd_inb(ahd, LAST_MSG); 4729 4730 if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_PPR, /*full*/FALSE)) { 4731 if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_PPR, /*full*/TRUE) 4732 && tinfo->goal.period <= AHD_SYNCRATE_PACED) { 4733 /* 4734 * Target may not like our SPI-4 PPR Options. 4735 * Attempt to negotiate 80MHz which will turn 4736 * off these options. 4737 */ 4738 if (bootverbose) { 4739 printf("(%s:%c:%d:%d): PPR Rejected. " 4740 "Trying simple U160 PPR\n", 4741 ahd_name(ahd), devinfo->channel, 4742 devinfo->target, devinfo->lun); 4743 } 4744 tinfo->goal.period = AHD_SYNCRATE_DT; 4745 tinfo->goal.ppr_options &= MSG_EXT_PPR_IU_REQ 4746 | MSG_EXT_PPR_QAS_REQ 4747 | MSG_EXT_PPR_DT_REQ; 4748 } else { 4749 /* 4750 * Target does not support the PPR message. 4751 * Attempt to negotiate SPI-2 style. 4752 */ 4753 if (bootverbose) { 4754 printf("(%s:%c:%d:%d): PPR Rejected. " 4755 "Trying WDTR/SDTR\n", 4756 ahd_name(ahd), devinfo->channel, 4757 devinfo->target, devinfo->lun); 4758 } 4759 tinfo->goal.ppr_options = 0; 4760 tinfo->curr.transport_version = 2; 4761 tinfo->goal.transport_version = 2; 4762 } 4763 ahd->msgout_index = 0; 4764 ahd->msgout_len = 0; 4765 ahd_build_transfer_msg(ahd, devinfo); 4766 ahd->msgout_index = 0; 4767 response = 1; 4768 } else if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_WDTR, /*full*/FALSE)) { 4769 4770 /* note 8bit xfers */ 4771 printf("(%s:%c:%d:%d): refuses WIDE negotiation. Using " 4772 "8bit transfers\n", ahd_name(ahd), 4773 devinfo->channel, devinfo->target, devinfo->lun); 4774 ahd_set_width(ahd, devinfo, MSG_EXT_WDTR_BUS_8_BIT, 4775 AHD_TRANS_ACTIVE|AHD_TRANS_GOAL, 4776 /*paused*/TRUE); 4777 /* 4778 * No need to clear the sync rate. If the target 4779 * did not accept the command, our syncrate is 4780 * unaffected. If the target started the negotiation, 4781 * but rejected our response, we already cleared the 4782 * sync rate before sending our WDTR. 4783 */ 4784 if (tinfo->goal.offset != tinfo->curr.offset) { 4785 4786 /* Start the sync negotiation */ 4787 ahd->msgout_index = 0; 4788 ahd->msgout_len = 0; 4789 ahd_build_transfer_msg(ahd, devinfo); 4790 ahd->msgout_index = 0; 4791 response = 1; 4792 } 4793 } else if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_SDTR, /*full*/FALSE)) { 4794 /* note asynch xfers and clear flag */ 4795 ahd_set_syncrate(ahd, devinfo, /*period*/0, 4796 /*offset*/0, /*ppr_options*/0, 4797 AHD_TRANS_ACTIVE|AHD_TRANS_GOAL, 4798 /*paused*/TRUE); 4799 printf("(%s:%c:%d:%d): refuses synchronous negotiation. " 4800 "Using asynchronous transfers\n", 4801 ahd_name(ahd), devinfo->channel, 4802 devinfo->target, devinfo->lun); 4803 } else if ((scb->hscb->control & MSG_SIMPLE_TASK) != 0) { 4804 int tag_type; 4805 int mask; 4806 4807 tag_type = (scb->hscb->control & MSG_SIMPLE_TASK); 4808 4809 if (tag_type == MSG_SIMPLE_TASK) { 4810 printf("(%s:%c:%d:%d): refuses tagged commands. " 4811 "Performing non-tagged I/O\n", ahd_name(ahd), 4812 devinfo->channel, devinfo->target, devinfo->lun); 4813 ahd_set_tags(ahd, devinfo, AHD_QUEUE_NONE); 4814 mask = ~0x23; 4815 } else { 4816 printf("(%s:%c:%d:%d): refuses %s tagged commands. " 4817 "Performing simple queue tagged I/O only\n", 4818 ahd_name(ahd), devinfo->channel, devinfo->target, 4819 devinfo->lun, tag_type == MSG_ORDERED_TASK 4820 ? "ordered" : "head of queue"); 4821 ahd_set_tags(ahd, devinfo, AHD_QUEUE_BASIC); 4822 mask = ~0x03; 4823 } 4824 4825 /* 4826 * Resend the identify for this CCB as the target 4827 * may believe that the selection is invalid otherwise. 4828 */ 4829 ahd_outb(ahd, SCB_CONTROL, 4830 ahd_inb_scbram(ahd, SCB_CONTROL) & mask); 4831 scb->hscb->control &= mask; 4832 aic_set_transaction_tag(scb, /*enabled*/FALSE, 4833 /*type*/MSG_SIMPLE_TASK); 4834 ahd_outb(ahd, MSG_OUT, MSG_IDENTIFYFLAG); 4835 ahd_assert_atn(ahd); 4836 ahd_busy_tcl(ahd, BUILD_TCL(scb->hscb->scsiid, devinfo->lun), 4837 SCB_GET_TAG(scb)); 4838 4839 /* 4840 * Requeue all tagged commands for this target 4841 * currently in our posession so they can be 4842 * converted to untagged commands. 4843 */ 4844 ahd_search_qinfifo(ahd, SCB_GET_TARGET(ahd, scb), 4845 SCB_GET_CHANNEL(ahd, scb), 4846 SCB_GET_LUN(scb), /*tag*/SCB_LIST_NULL, 4847 ROLE_INITIATOR, CAM_REQUEUE_REQ, 4848 SEARCH_COMPLETE); 4849 } else if (ahd_sent_msg(ahd, AHDMSG_1B, MSG_IDENTIFYFLAG, TRUE)) { 4850 /* 4851 * Most likely the device believes that we had 4852 * previously negotiated packetized. 4853 */ 4854 ahd->msg_flags |= MSG_FLAG_EXPECT_PPR_BUSFREE 4855 | MSG_FLAG_IU_REQ_CHANGED; 4856 4857 ahd_force_renegotiation(ahd, devinfo); 4858 ahd->msgout_index = 0; 4859 ahd->msgout_len = 0; 4860 ahd_build_transfer_msg(ahd, devinfo); 4861 ahd->msgout_index = 0; 4862 response = 1; 4863 } else { 4864 /* 4865 * Otherwise, we ignore it. 4866 */ 4867 printf("%s:%c:%d: Message reject for %x -- ignored\n", 4868 ahd_name(ahd), devinfo->channel, devinfo->target, 4869 last_msg); 4870 } 4871 return (response); 4872 } 4873 4874 /* 4875 * Process an ingnore wide residue message. 4876 */ 4877 static void 4878 ahd_handle_ign_wide_residue(struct ahd_softc *ahd, struct ahd_devinfo *devinfo) 4879 { 4880 u_int scb_index; 4881 struct scb *scb; 4882 4883 scb_index = ahd_get_scbptr(ahd); 4884 scb = ahd_lookup_scb(ahd, scb_index); 4885 /* 4886 * XXX Actually check data direction in the sequencer? 4887 * Perhaps add datadir to some spare bits in the hscb? 4888 */ 4889 if ((ahd_inb(ahd, SEQ_FLAGS) & DPHASE) == 0 4890 || aic_get_transfer_dir(scb) != CAM_DIR_IN) { 4891 /* 4892 * Ignore the message if we haven't 4893 * seen an appropriate data phase yet. 4894 */ 4895 } else { 4896 /* 4897 * If the residual occurred on the last 4898 * transfer and the transfer request was 4899 * expected to end on an odd count, do 4900 * nothing. Otherwise, subtract a byte 4901 * and update the residual count accordingly. 4902 */ 4903 uint32_t sgptr; 4904 4905 sgptr = ahd_inb_scbram(ahd, SCB_RESIDUAL_SGPTR); 4906 if ((sgptr & SG_LIST_NULL) != 0 4907 && (ahd_inb_scbram(ahd, SCB_TASK_ATTRIBUTE) 4908 & SCB_XFERLEN_ODD) != 0) { 4909 /* 4910 * If the residual occurred on the last 4911 * transfer and the transfer request was 4912 * expected to end on an odd count, do 4913 * nothing. 4914 */ 4915 } else { 4916 uint32_t data_cnt; 4917 uint64_t data_addr; 4918 uint32_t sglen; 4919 4920 /* Pull in the rest of the sgptr */ 4921 sgptr = ahd_inl_scbram(ahd, SCB_RESIDUAL_SGPTR); 4922 data_cnt = ahd_inl_scbram(ahd, SCB_RESIDUAL_DATACNT); 4923 if ((sgptr & SG_LIST_NULL) != 0) { 4924 /* 4925 * The residual data count is not updated 4926 * for the command run to completion case. 4927 * Explicitly zero the count. 4928 */ 4929 data_cnt &= ~AHD_SG_LEN_MASK; 4930 } 4931 data_addr = ahd_inq(ahd, SHADDR); 4932 data_cnt += 1; 4933 data_addr -= 1; 4934 sgptr &= SG_PTR_MASK; 4935 if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0) { 4936 struct ahd_dma64_seg *sg; 4937 4938 sg = ahd_sg_bus_to_virt(ahd, scb, sgptr); 4939 4940 /* 4941 * The residual sg ptr points to the next S/G 4942 * to load so we must go back one. 4943 */ 4944 sg--; 4945 sglen = aic_le32toh(sg->len) & AHD_SG_LEN_MASK; 4946 if (sg != scb->sg_list 4947 && sglen < (data_cnt & AHD_SG_LEN_MASK)) { 4948 4949 sg--; 4950 sglen = aic_le32toh(sg->len); 4951 /* 4952 * Preserve High Address and SG_LIST 4953 * bits while setting the count to 1. 4954 */ 4955 data_cnt = 1|(sglen&(~AHD_SG_LEN_MASK)); 4956 data_addr = aic_le64toh(sg->addr) 4957 + (sglen & AHD_SG_LEN_MASK) 4958 - 1; 4959 4960 /* 4961 * Increment sg so it points to the 4962 * "next" sg. 4963 */ 4964 sg++; 4965 sgptr = ahd_sg_virt_to_bus(ahd, scb, 4966 sg); 4967 } 4968 } else { 4969 struct ahd_dma_seg *sg; 4970 4971 sg = ahd_sg_bus_to_virt(ahd, scb, sgptr); 4972 4973 /* 4974 * The residual sg ptr points to the next S/G 4975 * to load so we must go back one. 4976 */ 4977 sg--; 4978 sglen = aic_le32toh(sg->len) & AHD_SG_LEN_MASK; 4979 if (sg != scb->sg_list 4980 && sglen < (data_cnt & AHD_SG_LEN_MASK)) { 4981 4982 sg--; 4983 sglen = aic_le32toh(sg->len); 4984 /* 4985 * Preserve High Address and SG_LIST 4986 * bits while setting the count to 1. 4987 */ 4988 data_cnt = 1|(sglen&(~AHD_SG_LEN_MASK)); 4989 data_addr = aic_le32toh(sg->addr) 4990 + (sglen & AHD_SG_LEN_MASK) 4991 - 1; 4992 4993 /* 4994 * Increment sg so it points to the 4995 * "next" sg. 4996 */ 4997 sg++; 4998 sgptr = ahd_sg_virt_to_bus(ahd, scb, 4999 sg); 5000 } 5001 } 5002 /* 5003 * Toggle the "oddness" of the transfer length 5004 * to handle this mid-transfer ignore wide 5005 * residue. This ensures that the oddness is 5006 * correct for subsequent data transfers. 5007 */ 5008 ahd_outb(ahd, SCB_TASK_ATTRIBUTE, 5009 ahd_inb_scbram(ahd, SCB_TASK_ATTRIBUTE) 5010 ^ SCB_XFERLEN_ODD); 5011 5012 ahd_outl(ahd, SCB_RESIDUAL_SGPTR, sgptr); 5013 ahd_outl(ahd, SCB_RESIDUAL_DATACNT, data_cnt); 5014 /* 5015 * The FIFO's pointers will be updated if/when the 5016 * sequencer re-enters a data phase. 5017 */ 5018 } 5019 } 5020 } 5021 5022 5023 /* 5024 * Reinitialize the data pointers for the active transfer 5025 * based on its current residual. 5026 */ 5027 static void 5028 ahd_reinitialize_dataptrs(struct ahd_softc *ahd) 5029 { 5030 struct scb *scb; 5031 ahd_mode_state saved_modes; 5032 u_int scb_index; 5033 u_int wait; 5034 uint32_t sgptr; 5035 uint32_t resid; 5036 uint64_t dataptr; 5037 5038 AHD_ASSERT_MODES(ahd, AHD_MODE_DFF0_MSK|AHD_MODE_DFF1_MSK, 5039 AHD_MODE_DFF0_MSK|AHD_MODE_DFF1_MSK); 5040 5041 scb_index = ahd_get_scbptr(ahd); 5042 scb = ahd_lookup_scb(ahd, scb_index); 5043 5044 /* 5045 * Release and reacquire the FIFO so we 5046 * have a clean slate. 5047 */ 5048 ahd_outb(ahd, DFFSXFRCTL, CLRCHN); 5049 wait = 1000; 5050 while (--wait && !(ahd_inb(ahd, MDFFSTAT) & FIFOFREE)) 5051 aic_delay(100); 5052 if (wait == 0) { 5053 ahd_print_path(ahd, scb); 5054 printf("ahd_reinitialize_dataptrs: Forcing FIFO free.\n"); 5055 ahd_outb(ahd, DFFSXFRCTL, RSTCHN|CLRSHCNT); 5056 } 5057 saved_modes = ahd_save_modes(ahd); 5058 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI); 5059 ahd_outb(ahd, DFFSTAT, 5060 ahd_inb(ahd, DFFSTAT) 5061 | (saved_modes == 0x11 ? CURRFIFO_1 : CURRFIFO_0)); 5062 5063 /* 5064 * Determine initial values for data_addr and data_cnt 5065 * for resuming the data phase. 5066 */ 5067 sgptr = ahd_inl_scbram(ahd, SCB_RESIDUAL_SGPTR); 5068 sgptr &= SG_PTR_MASK; 5069 5070 resid = (ahd_inb_scbram(ahd, SCB_RESIDUAL_DATACNT + 2) << 16) 5071 | (ahd_inb_scbram(ahd, SCB_RESIDUAL_DATACNT + 1) << 8) 5072 | ahd_inb_scbram(ahd, SCB_RESIDUAL_DATACNT); 5073 5074 if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0) { 5075 struct ahd_dma64_seg *sg; 5076 5077 sg = ahd_sg_bus_to_virt(ahd, scb, sgptr); 5078 5079 /* The residual sg_ptr always points to the next sg */ 5080 sg--; 5081 5082 dataptr = aic_le64toh(sg->addr) 5083 + (aic_le32toh(sg->len) & AHD_SG_LEN_MASK) 5084 - resid; 5085 ahd_outl(ahd, HADDR + 4, dataptr >> 32); 5086 } else { 5087 struct ahd_dma_seg *sg; 5088 5089 sg = ahd_sg_bus_to_virt(ahd, scb, sgptr); 5090 5091 /* The residual sg_ptr always points to the next sg */ 5092 sg--; 5093 5094 dataptr = aic_le32toh(sg->addr) 5095 + (aic_le32toh(sg->len) & AHD_SG_LEN_MASK) 5096 - resid; 5097 ahd_outb(ahd, HADDR + 4, 5098 (aic_le32toh(sg->len) & ~AHD_SG_LEN_MASK) >> 24); 5099 } 5100 ahd_outl(ahd, HADDR, dataptr); 5101 ahd_outb(ahd, HCNT + 2, resid >> 16); 5102 ahd_outb(ahd, HCNT + 1, resid >> 8); 5103 ahd_outb(ahd, HCNT, resid); 5104 } 5105 5106 /* 5107 * Handle the effects of issuing a bus device reset message. 5108 */ 5109 static void 5110 ahd_handle_devreset(struct ahd_softc *ahd, struct ahd_devinfo *devinfo, 5111 u_int lun, cam_status status, char *message, 5112 int verbose_level) 5113 { 5114 #ifdef AHD_TARGET_MODE 5115 struct ahd_tmode_tstate* tstate; 5116 #endif 5117 int found; 5118 5119 found = ahd_abort_scbs(ahd, devinfo->target, devinfo->channel, 5120 lun, SCB_LIST_NULL, devinfo->role, 5121 status); 5122 5123 #ifdef AHD_TARGET_MODE 5124 /* 5125 * Send an immediate notify ccb to all target mord peripheral 5126 * drivers affected by this action. 5127 */ 5128 tstate = ahd->enabled_targets[devinfo->our_scsiid]; 5129 if (tstate != NULL) { 5130 u_int cur_lun; 5131 u_int max_lun; 5132 5133 if (lun != CAM_LUN_WILDCARD) { 5134 cur_lun = 0; 5135 max_lun = AHD_NUM_LUNS - 1; 5136 } else { 5137 cur_lun = lun; 5138 max_lun = lun; 5139 } 5140 for (cur_lun <= max_lun; cur_lun++) { 5141 struct ahd_tmode_lstate* lstate; 5142 5143 lstate = tstate->enabled_luns[cur_lun]; 5144 if (lstate == NULL) 5145 continue; 5146 5147 ahd_queue_lstate_event(ahd, lstate, devinfo->our_scsiid, 5148 MSG_BUS_DEV_RESET, /*arg*/0); 5149 ahd_send_lstate_events(ahd, lstate); 5150 } 5151 } 5152 #endif 5153 5154 /* 5155 * Go back to async/narrow transfers and renegotiate. 5156 */ 5157 ahd_set_width(ahd, devinfo, MSG_EXT_WDTR_BUS_8_BIT, 5158 AHD_TRANS_CUR, /*paused*/TRUE); 5159 ahd_set_syncrate(ahd, devinfo, /*period*/0, /*offset*/0, 5160 /*ppr_options*/0, AHD_TRANS_CUR, 5161 /*paused*/TRUE); 5162 5163 if (status != CAM_SEL_TIMEOUT) 5164 ahd_send_async(ahd, devinfo->channel, devinfo->target, 5165 lun, AC_SENT_BDR, NULL); 5166 5167 if (message != NULL 5168 && (verbose_level <= bootverbose)) { 5169 AHD_CORRECTABLE_ERROR(ahd); 5170 printf("%s: %s on %c:%d. %d SCBs aborted\n", ahd_name(ahd), 5171 message, devinfo->channel, devinfo->target, found); 5172 } 5173 } 5174 5175 #ifdef AHD_TARGET_MODE 5176 static void 5177 ahd_setup_target_msgin(struct ahd_softc *ahd, struct ahd_devinfo *devinfo, 5178 struct scb *scb) 5179 { 5180 5181 /* 5182 * To facilitate adding multiple messages together, 5183 * each routine should increment the index and len 5184 * variables instead of setting them explicitly. 5185 */ 5186 ahd->msgout_index = 0; 5187 ahd->msgout_len = 0; 5188 5189 if (scb != NULL && (scb->flags & SCB_AUTO_NEGOTIATE) != 0) 5190 ahd_build_transfer_msg(ahd, devinfo); 5191 else 5192 panic("ahd_intr: AWAITING target message with no message"); 5193 5194 ahd->msgout_index = 0; 5195 ahd->msg_type = MSG_TYPE_TARGET_MSGIN; 5196 } 5197 #endif 5198 /**************************** Initialization **********************************/ 5199 static u_int 5200 ahd_sglist_size(struct ahd_softc *ahd) 5201 { 5202 bus_size_t list_size; 5203 5204 list_size = sizeof(struct ahd_dma_seg) * AHD_NSEG; 5205 if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0) 5206 list_size = sizeof(struct ahd_dma64_seg) * AHD_NSEG; 5207 return (list_size); 5208 } 5209 5210 /* 5211 * Calculate the optimum S/G List allocation size. S/G elements used 5212 * for a given transaction must be physically contiguous. Assume the 5213 * OS will allocate full pages to us, so it doesn't make sense to request 5214 * less than a page. 5215 */ 5216 static u_int 5217 ahd_sglist_allocsize(struct ahd_softc *ahd) 5218 { 5219 bus_size_t sg_list_increment; 5220 bus_size_t sg_list_size; 5221 bus_size_t max_list_size; 5222 bus_size_t best_list_size; 5223 5224 /* Start out with the minimum required for AHD_NSEG. */ 5225 sg_list_increment = ahd_sglist_size(ahd); 5226 sg_list_size = sg_list_increment; 5227 5228 /* Get us as close as possible to a page in size. */ 5229 while ((sg_list_size + sg_list_increment) <= PAGE_SIZE) 5230 sg_list_size += sg_list_increment; 5231 5232 /* 5233 * Try to reduce the amount of wastage by allocating 5234 * multiple pages. 5235 */ 5236 best_list_size = sg_list_size; 5237 max_list_size = roundup(sg_list_increment, PAGE_SIZE); 5238 if (max_list_size < 4 * PAGE_SIZE) 5239 max_list_size = 4 * PAGE_SIZE; 5240 if (max_list_size > (AHD_SCB_MAX_ALLOC * sg_list_increment)) 5241 max_list_size = (AHD_SCB_MAX_ALLOC * sg_list_increment); 5242 while ((sg_list_size + sg_list_increment) <= max_list_size 5243 && (sg_list_size % PAGE_SIZE) != 0) { 5244 bus_size_t new_mod; 5245 bus_size_t best_mod; 5246 5247 sg_list_size += sg_list_increment; 5248 new_mod = sg_list_size % PAGE_SIZE; 5249 best_mod = best_list_size % PAGE_SIZE; 5250 if (new_mod > best_mod || new_mod == 0) { 5251 best_list_size = sg_list_size; 5252 } 5253 } 5254 return (best_list_size); 5255 } 5256 5257 /* 5258 * Allocate a controller structure for a new device 5259 * and perform initial initializion. 5260 */ 5261 struct ahd_softc * 5262 ahd_alloc(void *platform_arg, char *name) 5263 { 5264 struct ahd_softc *ahd; 5265 5266 #ifndef __FreeBSD__ 5267 ahd = malloc(sizeof(*ahd), M_DEVBUF, M_NOWAIT); 5268 if (!ahd) { 5269 printf("aic7xxx: cannot malloc softc!\n"); 5270 free(name, M_DEVBUF); 5271 return NULL; 5272 } 5273 #else 5274 ahd = device_get_softc((device_t)platform_arg); 5275 #endif 5276 memset(ahd, 0, sizeof(*ahd)); 5277 ahd->seep_config = malloc(sizeof(*ahd->seep_config), 5278 M_DEVBUF, M_NOWAIT); 5279 if (ahd->seep_config == NULL) { 5280 #ifndef __FreeBSD__ 5281 free(ahd, M_DEVBUF); 5282 #endif 5283 free(name, M_DEVBUF); 5284 return (NULL); 5285 } 5286 LIST_INIT(&ahd->pending_scbs); 5287 LIST_INIT(&ahd->timedout_scbs); 5288 /* We don't know our unit number until the OSM sets it */ 5289 ahd->name = name; 5290 ahd->unit = -1; 5291 ahd->description = NULL; 5292 ahd->bus_description = NULL; 5293 ahd->channel = 'A'; 5294 ahd->chip = AHD_NONE; 5295 ahd->features = AHD_FENONE; 5296 ahd->bugs = AHD_BUGNONE; 5297 ahd->flags = AHD_SPCHK_ENB_A|AHD_RESET_BUS_A|AHD_TERM_ENB_A 5298 | AHD_EXTENDED_TRANS_A|AHD_STPWLEVEL_A; 5299 aic_timer_init(&ahd->reset_timer); 5300 aic_timer_init(&ahd->stat_timer); 5301 ahd->int_coalescing_timer = AHD_INT_COALESCING_TIMER_DEFAULT; 5302 ahd->int_coalescing_maxcmds = AHD_INT_COALESCING_MAXCMDS_DEFAULT; 5303 ahd->int_coalescing_mincmds = AHD_INT_COALESCING_MINCMDS_DEFAULT; 5304 ahd->int_coalescing_threshold = AHD_INT_COALESCING_THRESHOLD_DEFAULT; 5305 ahd->int_coalescing_stop_threshold = 5306 AHD_INT_COALESCING_STOP_THRESHOLD_DEFAULT; 5307 5308 if (ahd_platform_alloc(ahd, platform_arg) != 0) { 5309 ahd_free(ahd); 5310 ahd = NULL; 5311 } 5312 ahd_lockinit(ahd); 5313 #ifdef AHD_DEBUG 5314 if ((ahd_debug & AHD_SHOW_MEMORY) != 0) { 5315 printf("%s: scb size = 0x%x, hscb size = 0x%x\n", 5316 ahd_name(ahd), (u_int)sizeof(struct scb), 5317 (u_int)sizeof(struct hardware_scb)); 5318 } 5319 #endif 5320 return (ahd); 5321 } 5322 5323 int 5324 ahd_softc_init(struct ahd_softc *ahd) 5325 { 5326 5327 ahd->unpause = 0; 5328 ahd->pause = PAUSE; 5329 return (0); 5330 } 5331 5332 void 5333 ahd_softc_insert(struct ahd_softc *ahd) 5334 { 5335 struct ahd_softc *list_ahd; 5336 5337 #if AIC_PCI_CONFIG > 0 5338 /* 5339 * Second Function PCI devices need to inherit some 5340 * settings from function 0. 5341 */ 5342 if ((ahd->features & AHD_MULTI_FUNC) != 0) { 5343 TAILQ_FOREACH(list_ahd, &ahd_tailq, links) { 5344 aic_dev_softc_t list_pci; 5345 aic_dev_softc_t pci; 5346 5347 list_pci = list_ahd->dev_softc; 5348 pci = ahd->dev_softc; 5349 if (aic_get_pci_slot(list_pci) == aic_get_pci_slot(pci) 5350 && aic_get_pci_bus(list_pci) == aic_get_pci_bus(pci)) { 5351 struct ahd_softc *master; 5352 struct ahd_softc *slave; 5353 5354 if (aic_get_pci_function(list_pci) == 0) { 5355 master = list_ahd; 5356 slave = ahd; 5357 } else { 5358 master = ahd; 5359 slave = list_ahd; 5360 } 5361 slave->flags &= ~AHD_BIOS_ENABLED; 5362 slave->flags |= 5363 master->flags & AHD_BIOS_ENABLED; 5364 break; 5365 } 5366 } 5367 } 5368 #endif 5369 5370 /* 5371 * Insertion sort into our list of softcs. 5372 */ 5373 list_ahd = TAILQ_FIRST(&ahd_tailq); 5374 while (list_ahd != NULL 5375 && ahd_softc_comp(ahd, list_ahd) <= 0) 5376 list_ahd = TAILQ_NEXT(list_ahd, links); 5377 if (list_ahd != NULL) 5378 TAILQ_INSERT_BEFORE(list_ahd, ahd, links); 5379 else 5380 TAILQ_INSERT_TAIL(&ahd_tailq, ahd, links); 5381 ahd->init_level++; 5382 } 5383 5384 void 5385 ahd_set_unit(struct ahd_softc *ahd, int unit) 5386 { 5387 ahd->unit = unit; 5388 } 5389 5390 void 5391 ahd_set_name(struct ahd_softc *ahd, char *name) 5392 { 5393 if (ahd->name != NULL) 5394 free(ahd->name, M_DEVBUF); 5395 ahd->name = name; 5396 } 5397 5398 void 5399 ahd_free(struct ahd_softc *ahd) 5400 { 5401 int i; 5402 5403 ahd_terminate_recovery_thread(ahd); 5404 switch (ahd->init_level) { 5405 default: 5406 case 5: 5407 ahd_shutdown(ahd); 5408 /* FALLTHROUGH */ 5409 case 4: 5410 aic_dmamap_unload(ahd, ahd->shared_data_dmat, 5411 ahd->shared_data_map.dmamap); 5412 /* FALLTHROUGH */ 5413 case 3: 5414 aic_dmamem_free(ahd, ahd->shared_data_dmat, ahd->qoutfifo, 5415 ahd->shared_data_map.dmamap); 5416 aic_dmamap_destroy(ahd, ahd->shared_data_dmat, 5417 ahd->shared_data_map.dmamap); 5418 /* FALLTHROUGH */ 5419 case 2: 5420 aic_dma_tag_destroy(ahd, ahd->shared_data_dmat); 5421 case 1: 5422 #ifndef __linux__ 5423 aic_dma_tag_destroy(ahd, ahd->buffer_dmat); 5424 #endif 5425 break; 5426 case 0: 5427 break; 5428 } 5429 5430 #ifndef __linux__ 5431 aic_dma_tag_destroy(ahd, ahd->parent_dmat); 5432 #endif 5433 ahd_platform_free(ahd); 5434 ahd_fini_scbdata(ahd); 5435 for (i = 0; i < AHD_NUM_TARGETS; i++) { 5436 struct ahd_tmode_tstate *tstate; 5437 5438 tstate = ahd->enabled_targets[i]; 5439 if (tstate != NULL) { 5440 #ifdef AHD_TARGET_MODE 5441 int j; 5442 5443 for (j = 0; j < AHD_NUM_LUNS; j++) { 5444 struct ahd_tmode_lstate *lstate; 5445 5446 lstate = tstate->enabled_luns[j]; 5447 if (lstate != NULL) { 5448 xpt_free_path(lstate->path); 5449 free(lstate, M_DEVBUF); 5450 } 5451 } 5452 #endif 5453 free(tstate, M_DEVBUF); 5454 } 5455 } 5456 #ifdef AHD_TARGET_MODE 5457 if (ahd->black_hole != NULL) { 5458 xpt_free_path(ahd->black_hole->path); 5459 free(ahd->black_hole, M_DEVBUF); 5460 } 5461 #endif 5462 if (ahd->name != NULL) 5463 free(ahd->name, M_DEVBUF); 5464 if (ahd->seep_config != NULL) 5465 free(ahd->seep_config, M_DEVBUF); 5466 if (ahd->saved_stack != NULL) 5467 free(ahd->saved_stack, M_DEVBUF); 5468 #ifndef __FreeBSD__ 5469 free(ahd, M_DEVBUF); 5470 #endif 5471 return; 5472 } 5473 5474 void 5475 ahd_shutdown(void *arg) 5476 { 5477 struct ahd_softc *ahd; 5478 5479 ahd = (struct ahd_softc *)arg; 5480 5481 /* 5482 * Stop periodic timer callbacks. 5483 */ 5484 aic_timer_stop(&ahd->reset_timer); 5485 aic_timer_stop(&ahd->stat_timer); 5486 5487 /* This will reset most registers to 0, but not all */ 5488 ahd_reset(ahd, /*reinit*/FALSE); 5489 } 5490 5491 /* 5492 * Reset the controller and record some information about it 5493 * that is only available just after a reset. If "reinit" is 5494 * non-zero, this reset occured after initial configuration 5495 * and the caller requests that the chip be fully reinitialized 5496 * to a runable state. Chip interrupts are *not* enabled after 5497 * a reinitialization. The caller must enable interrupts via 5498 * ahd_intr_enable(). 5499 */ 5500 int 5501 ahd_reset(struct ahd_softc *ahd, int reinit) 5502 { 5503 u_int sxfrctl1; 5504 int wait; 5505 uint32_t cmd; 5506 5507 /* 5508 * Preserve the value of the SXFRCTL1 register for all channels. 5509 * It contains settings that affect termination and we don't want 5510 * to disturb the integrity of the bus. 5511 */ 5512 ahd_pause(ahd); 5513 ahd_update_modes(ahd); 5514 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI); 5515 sxfrctl1 = ahd_inb(ahd, SXFRCTL1); 5516 5517 cmd = aic_pci_read_config(ahd->dev_softc, PCIR_COMMAND, /*bytes*/2); 5518 if ((ahd->bugs & AHD_PCIX_CHIPRST_BUG) != 0) { 5519 uint32_t mod_cmd; 5520 5521 /* 5522 * A4 Razor #632 5523 * During the assertion of CHIPRST, the chip 5524 * does not disable its parity logic prior to 5525 * the start of the reset. This may cause a 5526 * parity error to be detected and thus a 5527 * spurious SERR or PERR assertion. Disble 5528 * PERR and SERR responses during the CHIPRST. 5529 */ 5530 mod_cmd = cmd & ~(PCIM_CMD_PERRESPEN|PCIM_CMD_SERRESPEN); 5531 aic_pci_write_config(ahd->dev_softc, PCIR_COMMAND, 5532 mod_cmd, /*bytes*/2); 5533 } 5534 ahd_outb(ahd, HCNTRL, CHIPRST | ahd->pause); 5535 5536 /* 5537 * Ensure that the reset has finished. We delay 1000us 5538 * prior to reading the register to make sure the chip 5539 * has sufficiently completed its reset to handle register 5540 * accesses. 5541 */ 5542 wait = 1000; 5543 do { 5544 aic_delay(1000); 5545 } while (--wait && !(ahd_inb(ahd, HCNTRL) & CHIPRSTACK)); 5546 5547 if (wait == 0) { 5548 printf("%s: WARNING - Failed chip reset! " 5549 "Trying to initialize anyway.\n", ahd_name(ahd)); 5550 AHD_FATAL_ERROR(ahd); 5551 } 5552 ahd_outb(ahd, HCNTRL, ahd->pause); 5553 5554 if ((ahd->bugs & AHD_PCIX_CHIPRST_BUG) != 0) { 5555 /* 5556 * Clear any latched PCI error status and restore 5557 * previous SERR and PERR response enables. 5558 */ 5559 aic_pci_write_config(ahd->dev_softc, PCIR_STATUS + 1, 5560 0xFF, /*bytes*/1); 5561 aic_pci_write_config(ahd->dev_softc, PCIR_COMMAND, 5562 cmd, /*bytes*/2); 5563 } 5564 5565 /* 5566 * Mode should be SCSI after a chip reset, but lets 5567 * set it just to be safe. We touch the MODE_PTR 5568 * register directly so as to bypass the lazy update 5569 * code in ahd_set_modes(). 5570 */ 5571 ahd_known_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI); 5572 ahd_outb(ahd, MODE_PTR, 5573 ahd_build_mode_state(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI)); 5574 5575 /* 5576 * Restore SXFRCTL1. 5577 * 5578 * We must always initialize STPWEN to 1 before we 5579 * restore the saved values. STPWEN is initialized 5580 * to a tri-state condition which can only be cleared 5581 * by turning it on. 5582 */ 5583 ahd_outb(ahd, SXFRCTL1, sxfrctl1|STPWEN); 5584 ahd_outb(ahd, SXFRCTL1, sxfrctl1); 5585 5586 /* Determine chip configuration */ 5587 ahd->features &= ~AHD_WIDE; 5588 if ((ahd_inb(ahd, SBLKCTL) & SELWIDE) != 0) 5589 ahd->features |= AHD_WIDE; 5590 5591 /* 5592 * If a recovery action has forced a chip reset, 5593 * re-initialize the chip to our liking. 5594 */ 5595 if (reinit != 0) 5596 ahd_chip_init(ahd); 5597 5598 return (0); 5599 } 5600 5601 /* 5602 * Determine the number of SCBs available on the controller 5603 */ 5604 int 5605 ahd_probe_scbs(struct ahd_softc *ahd) { 5606 int i; 5607 5608 AHD_ASSERT_MODES(ahd, ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK), 5609 ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK)); 5610 for (i = 0; i < AHD_SCB_MAX; i++) { 5611 int j; 5612 5613 ahd_set_scbptr(ahd, i); 5614 ahd_outw(ahd, SCB_BASE, i); 5615 for (j = 2; j < 64; j++) 5616 ahd_outb(ahd, SCB_BASE+j, 0); 5617 /* Start out life as unallocated (needing an abort) */ 5618 ahd_outb(ahd, SCB_CONTROL, MK_MESSAGE); 5619 if (ahd_inw_scbram(ahd, SCB_BASE) != i) 5620 break; 5621 ahd_set_scbptr(ahd, 0); 5622 if (ahd_inw_scbram(ahd, SCB_BASE) != 0) 5623 break; 5624 } 5625 return (i); 5626 } 5627 5628 static void 5629 ahd_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error) 5630 { 5631 bus_addr_t *baddr; 5632 5633 baddr = (bus_addr_t *)arg; 5634 *baddr = segs->ds_addr; 5635 } 5636 5637 static void 5638 ahd_initialize_hscbs(struct ahd_softc *ahd) 5639 { 5640 int i; 5641 5642 for (i = 0; i < ahd->scb_data.maxhscbs; i++) { 5643 ahd_set_scbptr(ahd, i); 5644 5645 /* Clear the control byte. */ 5646 ahd_outb(ahd, SCB_CONTROL, 0); 5647 5648 /* Set the next pointer */ 5649 ahd_outw(ahd, SCB_NEXT, SCB_LIST_NULL); 5650 } 5651 } 5652 5653 static int 5654 ahd_init_scbdata(struct ahd_softc *ahd) 5655 { 5656 struct scb_data *scb_data; 5657 int i; 5658 5659 scb_data = &ahd->scb_data; 5660 TAILQ_INIT(&scb_data->free_scbs); 5661 for (i = 0; i < AHD_NUM_TARGETS * AHD_NUM_LUNS_NONPKT; i++) 5662 LIST_INIT(&scb_data->free_scb_lists[i]); 5663 LIST_INIT(&scb_data->any_dev_free_scb_list); 5664 SLIST_INIT(&scb_data->hscb_maps); 5665 SLIST_INIT(&scb_data->sg_maps); 5666 SLIST_INIT(&scb_data->sense_maps); 5667 5668 /* Determine the number of hardware SCBs and initialize them */ 5669 scb_data->maxhscbs = ahd_probe_scbs(ahd); 5670 if (scb_data->maxhscbs == 0) { 5671 printf("%s: No SCB space found\n", ahd_name(ahd)); 5672 AHD_FATAL_ERROR(ahd); 5673 return (ENXIO); 5674 } 5675 5676 ahd_initialize_hscbs(ahd); 5677 5678 /* 5679 * Create our DMA tags. These tags define the kinds of device 5680 * accessible memory allocations and memory mappings we will 5681 * need to perform during normal operation. 5682 * 5683 * Unless we need to further restrict the allocation, we rely 5684 * on the restrictions of the parent dmat, hence the common 5685 * use of MAXADDR and MAXSIZE. 5686 */ 5687 5688 /* DMA tag for our hardware scb structures */ 5689 if (aic_dma_tag_create(ahd, ahd->parent_dmat, /*alignment*/1, 5690 /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1, 5691 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT, 5692 /*highaddr*/BUS_SPACE_MAXADDR, 5693 /*filter*/NULL, /*filterarg*/NULL, 5694 PAGE_SIZE, /*nsegments*/1, 5695 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT, 5696 /*flags*/0, &scb_data->hscb_dmat) != 0) { 5697 goto error_exit; 5698 } 5699 5700 scb_data->init_level++; 5701 5702 /* DMA tag for our S/G structures. */ 5703 if (aic_dma_tag_create(ahd, ahd->parent_dmat, /*alignment*/8, 5704 /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1, 5705 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT, 5706 /*highaddr*/BUS_SPACE_MAXADDR, 5707 /*filter*/NULL, /*filterarg*/NULL, 5708 ahd_sglist_allocsize(ahd), /*nsegments*/1, 5709 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT, 5710 /*flags*/0, &scb_data->sg_dmat) != 0) { 5711 goto error_exit; 5712 } 5713 #ifdef AHD_DEBUG 5714 if ((ahd_debug & AHD_SHOW_MEMORY) != 0) 5715 printf("%s: ahd_sglist_allocsize = 0x%x\n", ahd_name(ahd), 5716 ahd_sglist_allocsize(ahd)); 5717 #endif 5718 5719 scb_data->init_level++; 5720 5721 /* DMA tag for our sense buffers. We allocate in page sized chunks */ 5722 if (aic_dma_tag_create(ahd, ahd->parent_dmat, /*alignment*/1, 5723 /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1, 5724 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT, 5725 /*highaddr*/BUS_SPACE_MAXADDR, 5726 /*filter*/NULL, /*filterarg*/NULL, 5727 PAGE_SIZE, /*nsegments*/1, 5728 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT, 5729 /*flags*/0, &scb_data->sense_dmat) != 0) { 5730 goto error_exit; 5731 } 5732 5733 scb_data->init_level++; 5734 5735 /* Perform initial CCB allocation */ 5736 while (ahd_alloc_scbs(ahd) != 0) 5737 ; 5738 5739 if (scb_data->numscbs == 0) { 5740 printf("%s: ahd_init_scbdata - " 5741 "Unable to allocate initial scbs\n", 5742 ahd_name(ahd)); 5743 goto error_exit; 5744 } 5745 5746 /* 5747 * Note that we were successfull 5748 */ 5749 return (0); 5750 5751 error_exit: 5752 5753 return (ENOMEM); 5754 } 5755 5756 static struct scb * 5757 ahd_find_scb_by_tag(struct ahd_softc *ahd, u_int tag) 5758 { 5759 struct scb *scb; 5760 5761 /* 5762 * Look on the pending list. 5763 */ 5764 LIST_FOREACH(scb, &ahd->pending_scbs, pending_links) { 5765 if (SCB_GET_TAG(scb) == tag) 5766 return (scb); 5767 } 5768 5769 /* 5770 * Then on all of the collision free lists. 5771 */ 5772 TAILQ_FOREACH(scb, &ahd->scb_data.free_scbs, links.tqe) { 5773 struct scb *list_scb; 5774 5775 list_scb = scb; 5776 do { 5777 if (SCB_GET_TAG(list_scb) == tag) 5778 return (list_scb); 5779 list_scb = LIST_NEXT(list_scb, collision_links); 5780 } while (list_scb); 5781 } 5782 5783 /* 5784 * And finally on the generic free list. 5785 */ 5786 LIST_FOREACH(scb, &ahd->scb_data.any_dev_free_scb_list, links.le) { 5787 if (SCB_GET_TAG(scb) == tag) 5788 return (scb); 5789 } 5790 5791 return (NULL); 5792 } 5793 5794 static void 5795 ahd_fini_scbdata(struct ahd_softc *ahd) 5796 { 5797 struct scb_data *scb_data; 5798 5799 scb_data = &ahd->scb_data; 5800 if (scb_data == NULL) 5801 return; 5802 5803 switch (scb_data->init_level) { 5804 default: 5805 case 7: 5806 { 5807 struct map_node *sns_map; 5808 5809 while ((sns_map = SLIST_FIRST(&scb_data->sense_maps)) != NULL) { 5810 SLIST_REMOVE_HEAD(&scb_data->sense_maps, links); 5811 aic_dmamap_unload(ahd, scb_data->sense_dmat, 5812 sns_map->dmamap); 5813 aic_dmamem_free(ahd, scb_data->sense_dmat, 5814 sns_map->vaddr, sns_map->dmamap); 5815 free(sns_map, M_DEVBUF); 5816 } 5817 aic_dma_tag_destroy(ahd, scb_data->sense_dmat); 5818 /* FALLTHROUGH */ 5819 } 5820 case 6: 5821 { 5822 struct map_node *sg_map; 5823 5824 while ((sg_map = SLIST_FIRST(&scb_data->sg_maps)) != NULL) { 5825 SLIST_REMOVE_HEAD(&scb_data->sg_maps, links); 5826 aic_dmamap_unload(ahd, scb_data->sg_dmat, 5827 sg_map->dmamap); 5828 aic_dmamem_free(ahd, scb_data->sg_dmat, 5829 sg_map->vaddr, sg_map->dmamap); 5830 free(sg_map, M_DEVBUF); 5831 } 5832 aic_dma_tag_destroy(ahd, scb_data->sg_dmat); 5833 /* FALLTHROUGH */ 5834 } 5835 case 5: 5836 { 5837 struct map_node *hscb_map; 5838 5839 while ((hscb_map = SLIST_FIRST(&scb_data->hscb_maps)) != NULL) { 5840 SLIST_REMOVE_HEAD(&scb_data->hscb_maps, links); 5841 aic_dmamap_unload(ahd, scb_data->hscb_dmat, 5842 hscb_map->dmamap); 5843 aic_dmamem_free(ahd, scb_data->hscb_dmat, 5844 hscb_map->vaddr, hscb_map->dmamap); 5845 free(hscb_map, M_DEVBUF); 5846 } 5847 aic_dma_tag_destroy(ahd, scb_data->hscb_dmat); 5848 /* FALLTHROUGH */ 5849 } 5850 case 4: 5851 case 3: 5852 case 2: 5853 case 1: 5854 case 0: 5855 break; 5856 } 5857 } 5858 5859 /* 5860 * DSP filter Bypass must be enabled until the first selection 5861 * after a change in bus mode (Razor #491 and #493). 5862 */ 5863 static void 5864 ahd_setup_iocell_workaround(struct ahd_softc *ahd) 5865 { 5866 ahd_mode_state saved_modes; 5867 5868 saved_modes = ahd_save_modes(ahd); 5869 ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG); 5870 ahd_outb(ahd, DSPDATACTL, ahd_inb(ahd, DSPDATACTL) 5871 | BYPASSENAB | RCVROFFSTDIS | XMITOFFSTDIS); 5872 ahd_outb(ahd, SIMODE0, ahd_inb(ahd, SIMODE0) | (ENSELDO|ENSELDI)); 5873 #ifdef AHD_DEBUG 5874 if ((ahd_debug & AHD_SHOW_MISC) != 0) 5875 printf("%s: Setting up iocell workaround\n", ahd_name(ahd)); 5876 #endif 5877 ahd_restore_modes(ahd, saved_modes); 5878 ahd->flags &= ~AHD_HAD_FIRST_SEL; 5879 } 5880 5881 static void 5882 ahd_iocell_first_selection(struct ahd_softc *ahd) 5883 { 5884 ahd_mode_state saved_modes; 5885 u_int sblkctl; 5886 5887 if ((ahd->flags & AHD_HAD_FIRST_SEL) != 0) 5888 return; 5889 saved_modes = ahd_save_modes(ahd); 5890 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI); 5891 sblkctl = ahd_inb(ahd, SBLKCTL); 5892 ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG); 5893 #ifdef AHD_DEBUG 5894 if ((ahd_debug & AHD_SHOW_MISC) != 0) 5895 printf("%s: iocell first selection\n", ahd_name(ahd)); 5896 #endif 5897 if ((sblkctl & ENAB40) != 0) { 5898 ahd_outb(ahd, DSPDATACTL, 5899 ahd_inb(ahd, DSPDATACTL) & ~BYPASSENAB); 5900 #ifdef AHD_DEBUG 5901 if ((ahd_debug & AHD_SHOW_MISC) != 0) 5902 printf("%s: BYPASS now disabled\n", ahd_name(ahd)); 5903 #endif 5904 } 5905 ahd_outb(ahd, SIMODE0, ahd_inb(ahd, SIMODE0) & ~(ENSELDO|ENSELDI)); 5906 ahd_outb(ahd, CLRINT, CLRSCSIINT); 5907 ahd_restore_modes(ahd, saved_modes); 5908 ahd->flags |= AHD_HAD_FIRST_SEL; 5909 } 5910 5911 /*************************** SCB Management ***********************************/ 5912 static void 5913 ahd_add_col_list(struct ahd_softc *ahd, struct scb *scb, u_int col_idx) 5914 { 5915 struct scb_list *free_list; 5916 struct scb_tailq *free_tailq; 5917 struct scb *first_scb; 5918 5919 scb->flags |= SCB_ON_COL_LIST; 5920 AHD_SET_SCB_COL_IDX(scb, col_idx); 5921 free_list = &ahd->scb_data.free_scb_lists[col_idx]; 5922 free_tailq = &ahd->scb_data.free_scbs; 5923 first_scb = LIST_FIRST(free_list); 5924 if (first_scb != NULL) { 5925 LIST_INSERT_AFTER(first_scb, scb, collision_links); 5926 } else { 5927 LIST_INSERT_HEAD(free_list, scb, collision_links); 5928 TAILQ_INSERT_TAIL(free_tailq, scb, links.tqe); 5929 } 5930 } 5931 5932 static void 5933 ahd_rem_col_list(struct ahd_softc *ahd, struct scb *scb) 5934 { 5935 struct scb_list *free_list; 5936 struct scb_tailq *free_tailq; 5937 struct scb *first_scb; 5938 u_int col_idx; 5939 5940 scb->flags &= ~SCB_ON_COL_LIST; 5941 col_idx = AHD_GET_SCB_COL_IDX(ahd, scb); 5942 free_list = &ahd->scb_data.free_scb_lists[col_idx]; 5943 free_tailq = &ahd->scb_data.free_scbs; 5944 first_scb = LIST_FIRST(free_list); 5945 if (first_scb == scb) { 5946 struct scb *next_scb; 5947 5948 /* 5949 * Maintain order in the collision free 5950 * lists for fairness if this device has 5951 * other colliding tags active. 5952 */ 5953 next_scb = LIST_NEXT(scb, collision_links); 5954 if (next_scb != NULL) { 5955 TAILQ_INSERT_AFTER(free_tailq, scb, 5956 next_scb, links.tqe); 5957 } 5958 TAILQ_REMOVE(free_tailq, scb, links.tqe); 5959 } 5960 LIST_REMOVE(scb, collision_links); 5961 } 5962 5963 /* 5964 * Get a free scb. If there are none, see if we can allocate a new SCB. 5965 */ 5966 struct scb * 5967 ahd_get_scb(struct ahd_softc *ahd, u_int col_idx) 5968 { 5969 struct scb *scb; 5970 int tries; 5971 5972 tries = 0; 5973 look_again: 5974 TAILQ_FOREACH(scb, &ahd->scb_data.free_scbs, links.tqe) { 5975 if (AHD_GET_SCB_COL_IDX(ahd, scb) != col_idx) { 5976 ahd_rem_col_list(ahd, scb); 5977 goto found; 5978 } 5979 } 5980 if ((scb = LIST_FIRST(&ahd->scb_data.any_dev_free_scb_list)) == NULL) { 5981 5982 if (tries++ != 0) 5983 return (NULL); 5984 if (ahd_alloc_scbs(ahd) == 0) 5985 return (NULL); 5986 goto look_again; 5987 } 5988 LIST_REMOVE(scb, links.le); 5989 if (col_idx != AHD_NEVER_COL_IDX 5990 && (scb->col_scb != NULL) 5991 && (scb->col_scb->flags & SCB_ACTIVE) == 0) { 5992 LIST_REMOVE(scb->col_scb, links.le); 5993 ahd_add_col_list(ahd, scb->col_scb, col_idx); 5994 } 5995 found: 5996 scb->flags |= SCB_ACTIVE; 5997 return (scb); 5998 } 5999 6000 /* 6001 * Return an SCB resource to the free list. 6002 */ 6003 void 6004 ahd_free_scb(struct ahd_softc *ahd, struct scb *scb) 6005 { 6006 6007 /* Clean up for the next user */ 6008 scb->flags = SCB_FLAG_NONE; 6009 scb->hscb->control = 0; 6010 ahd->scb_data.scbindex[SCB_GET_TAG(scb)] = NULL; 6011 6012 if (scb->col_scb == NULL) { 6013 6014 /* 6015 * No collision possible. Just free normally. 6016 */ 6017 LIST_INSERT_HEAD(&ahd->scb_data.any_dev_free_scb_list, 6018 scb, links.le); 6019 } else if ((scb->col_scb->flags & SCB_ON_COL_LIST) != 0) { 6020 6021 /* 6022 * The SCB we might have collided with is on 6023 * a free collision list. Put both SCBs on 6024 * the generic list. 6025 */ 6026 ahd_rem_col_list(ahd, scb->col_scb); 6027 LIST_INSERT_HEAD(&ahd->scb_data.any_dev_free_scb_list, 6028 scb, links.le); 6029 LIST_INSERT_HEAD(&ahd->scb_data.any_dev_free_scb_list, 6030 scb->col_scb, links.le); 6031 } else if ((scb->col_scb->flags 6032 & (SCB_PACKETIZED|SCB_ACTIVE)) == SCB_ACTIVE 6033 && (scb->col_scb->hscb->control & TAG_ENB) != 0) { 6034 6035 /* 6036 * The SCB we might collide with on the next allocation 6037 * is still active in a non-packetized, tagged, context. 6038 * Put us on the SCB collision list. 6039 */ 6040 ahd_add_col_list(ahd, scb, 6041 AHD_GET_SCB_COL_IDX(ahd, scb->col_scb)); 6042 } else { 6043 /* 6044 * The SCB we might collide with on the next allocation 6045 * is either active in a packetized context, or free. 6046 * Since we can't collide, put this SCB on the generic 6047 * free list. 6048 */ 6049 LIST_INSERT_HEAD(&ahd->scb_data.any_dev_free_scb_list, 6050 scb, links.le); 6051 } 6052 6053 aic_platform_scb_free(ahd, scb); 6054 } 6055 6056 int 6057 ahd_alloc_scbs(struct ahd_softc *ahd) 6058 { 6059 struct scb_data *scb_data; 6060 struct scb *next_scb; 6061 struct hardware_scb *hscb; 6062 struct map_node *hscb_map; 6063 struct map_node *sg_map; 6064 struct map_node *sense_map; 6065 uint8_t *segs; 6066 uint8_t *sense_data; 6067 bus_addr_t hscb_busaddr; 6068 bus_addr_t sg_busaddr; 6069 bus_addr_t sense_busaddr; 6070 int newcount; 6071 int i; 6072 6073 scb_data = &ahd->scb_data; 6074 if (scb_data->numscbs >= AHD_SCB_MAX_ALLOC) 6075 /* Can't allocate any more */ 6076 return (0); 6077 6078 if (scb_data->scbs_left != 0) { 6079 int offset; 6080 6081 offset = (PAGE_SIZE / sizeof(*hscb)) - scb_data->scbs_left; 6082 hscb_map = SLIST_FIRST(&scb_data->hscb_maps); 6083 hscb = &((struct hardware_scb *)hscb_map->vaddr)[offset]; 6084 hscb_busaddr = hscb_map->busaddr + (offset * sizeof(*hscb)); 6085 } else { 6086 hscb_map = malloc(sizeof(*hscb_map), M_DEVBUF, M_NOWAIT); 6087 6088 if (hscb_map == NULL) 6089 return (0); 6090 6091 /* Allocate the next batch of hardware SCBs */ 6092 if (aic_dmamem_alloc(ahd, scb_data->hscb_dmat, 6093 (void **)&hscb_map->vaddr, 6094 BUS_DMA_NOWAIT | BUS_DMA_COHERENT, 6095 &hscb_map->dmamap) != 0) { 6096 free(hscb_map, M_DEVBUF); 6097 return (0); 6098 } 6099 6100 SLIST_INSERT_HEAD(&scb_data->hscb_maps, hscb_map, links); 6101 6102 aic_dmamap_load(ahd, scb_data->hscb_dmat, hscb_map->dmamap, 6103 hscb_map->vaddr, PAGE_SIZE, ahd_dmamap_cb, 6104 &hscb_map->busaddr, /*flags*/0); 6105 6106 hscb = (struct hardware_scb *)hscb_map->vaddr; 6107 hscb_busaddr = hscb_map->busaddr; 6108 scb_data->scbs_left = PAGE_SIZE / sizeof(*hscb); 6109 } 6110 6111 if (scb_data->sgs_left != 0) { 6112 int offset; 6113 6114 offset = ((ahd_sglist_allocsize(ahd) / ahd_sglist_size(ahd)) 6115 - scb_data->sgs_left) * ahd_sglist_size(ahd); 6116 sg_map = SLIST_FIRST(&scb_data->sg_maps); 6117 segs = sg_map->vaddr + offset; 6118 sg_busaddr = sg_map->busaddr + offset; 6119 } else { 6120 sg_map = malloc(sizeof(*sg_map), M_DEVBUF, M_NOWAIT); 6121 6122 if (sg_map == NULL) 6123 return (0); 6124 6125 /* Allocate the next batch of S/G lists */ 6126 if (aic_dmamem_alloc(ahd, scb_data->sg_dmat, 6127 (void **)&sg_map->vaddr, 6128 BUS_DMA_NOWAIT | BUS_DMA_COHERENT, 6129 &sg_map->dmamap) != 0) { 6130 free(sg_map, M_DEVBUF); 6131 return (0); 6132 } 6133 6134 SLIST_INSERT_HEAD(&scb_data->sg_maps, sg_map, links); 6135 6136 aic_dmamap_load(ahd, scb_data->sg_dmat, sg_map->dmamap, 6137 sg_map->vaddr, ahd_sglist_allocsize(ahd), 6138 ahd_dmamap_cb, &sg_map->busaddr, /*flags*/0); 6139 6140 segs = sg_map->vaddr; 6141 sg_busaddr = sg_map->busaddr; 6142 scb_data->sgs_left = 6143 ahd_sglist_allocsize(ahd) / ahd_sglist_size(ahd); 6144 #ifdef AHD_DEBUG 6145 if (ahd_debug & AHD_SHOW_MEMORY) 6146 printf("Mapped SG data\n"); 6147 #endif 6148 } 6149 6150 if (scb_data->sense_left != 0) { 6151 int offset; 6152 6153 offset = PAGE_SIZE - (AHD_SENSE_BUFSIZE * scb_data->sense_left); 6154 sense_map = SLIST_FIRST(&scb_data->sense_maps); 6155 sense_data = sense_map->vaddr + offset; 6156 sense_busaddr = sense_map->busaddr + offset; 6157 } else { 6158 sense_map = malloc(sizeof(*sense_map), M_DEVBUF, M_NOWAIT); 6159 6160 if (sense_map == NULL) 6161 return (0); 6162 6163 /* Allocate the next batch of sense buffers */ 6164 if (aic_dmamem_alloc(ahd, scb_data->sense_dmat, 6165 (void **)&sense_map->vaddr, 6166 BUS_DMA_NOWAIT, &sense_map->dmamap) != 0) { 6167 free(sense_map, M_DEVBUF); 6168 return (0); 6169 } 6170 6171 SLIST_INSERT_HEAD(&scb_data->sense_maps, sense_map, links); 6172 6173 aic_dmamap_load(ahd, scb_data->sense_dmat, sense_map->dmamap, 6174 sense_map->vaddr, PAGE_SIZE, ahd_dmamap_cb, 6175 &sense_map->busaddr, /*flags*/0); 6176 6177 sense_data = sense_map->vaddr; 6178 sense_busaddr = sense_map->busaddr; 6179 scb_data->sense_left = PAGE_SIZE / AHD_SENSE_BUFSIZE; 6180 #ifdef AHD_DEBUG 6181 if (ahd_debug & AHD_SHOW_MEMORY) 6182 printf("Mapped sense data\n"); 6183 #endif 6184 } 6185 6186 newcount = MIN(scb_data->sense_left, scb_data->scbs_left); 6187 newcount = MIN(newcount, scb_data->sgs_left); 6188 newcount = MIN(newcount, (AHD_SCB_MAX_ALLOC - scb_data->numscbs)); 6189 scb_data->sense_left -= newcount; 6190 scb_data->scbs_left -= newcount; 6191 scb_data->sgs_left -= newcount; 6192 for (i = 0; i < newcount; i++) { 6193 struct scb_platform_data *pdata; 6194 u_int col_tag; 6195 #ifndef __linux__ 6196 int error; 6197 #endif 6198 6199 next_scb = (struct scb *)malloc(sizeof(*next_scb), 6200 M_DEVBUF, M_NOWAIT); 6201 if (next_scb == NULL) 6202 break; 6203 6204 pdata = (struct scb_platform_data *)malloc(sizeof(*pdata), 6205 M_DEVBUF, M_NOWAIT); 6206 if (pdata == NULL) { 6207 free(next_scb, M_DEVBUF); 6208 break; 6209 } 6210 next_scb->platform_data = pdata; 6211 next_scb->hscb_map = hscb_map; 6212 next_scb->sg_map = sg_map; 6213 next_scb->sense_map = sense_map; 6214 next_scb->sg_list = segs; 6215 next_scb->sense_data = sense_data; 6216 next_scb->sense_busaddr = sense_busaddr; 6217 memset(hscb, 0, sizeof(*hscb)); 6218 next_scb->hscb = hscb; 6219 hscb->hscb_busaddr = aic_htole32(hscb_busaddr); 6220 6221 /* 6222 * The sequencer always starts with the second entry. 6223 * The first entry is embedded in the scb. 6224 */ 6225 next_scb->sg_list_busaddr = sg_busaddr; 6226 if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0) 6227 next_scb->sg_list_busaddr 6228 += sizeof(struct ahd_dma64_seg); 6229 else 6230 next_scb->sg_list_busaddr += sizeof(struct ahd_dma_seg); 6231 next_scb->ahd_softc = ahd; 6232 next_scb->flags = SCB_FLAG_NONE; 6233 #ifndef __linux__ 6234 error = aic_dmamap_create(ahd, ahd->buffer_dmat, /*flags*/0, 6235 &next_scb->dmamap); 6236 if (error != 0) { 6237 free(next_scb, M_DEVBUF); 6238 free(pdata, M_DEVBUF); 6239 break; 6240 } 6241 #endif 6242 next_scb->hscb->tag = aic_htole16(scb_data->numscbs); 6243 col_tag = scb_data->numscbs ^ 0x100; 6244 next_scb->col_scb = ahd_find_scb_by_tag(ahd, col_tag); 6245 if (next_scb->col_scb != NULL) 6246 next_scb->col_scb->col_scb = next_scb; 6247 aic_timer_init(&next_scb->io_timer); 6248 ahd_free_scb(ahd, next_scb); 6249 hscb++; 6250 hscb_busaddr += sizeof(*hscb); 6251 segs += ahd_sglist_size(ahd); 6252 sg_busaddr += ahd_sglist_size(ahd); 6253 sense_data += AHD_SENSE_BUFSIZE; 6254 sense_busaddr += AHD_SENSE_BUFSIZE; 6255 scb_data->numscbs++; 6256 } 6257 return (i); 6258 } 6259 6260 void 6261 ahd_controller_info(struct ahd_softc *ahd, char *buf) 6262 { 6263 const char *speed; 6264 const char *type; 6265 int len; 6266 6267 len = sprintf(buf, "%s: ", ahd_chip_names[ahd->chip & AHD_CHIPID_MASK]); 6268 buf += len; 6269 6270 speed = "Ultra320 "; 6271 if ((ahd->features & AHD_WIDE) != 0) { 6272 type = "Wide "; 6273 } else { 6274 type = "Single "; 6275 } 6276 len = sprintf(buf, "%s%sChannel %c, SCSI Id=%d, ", 6277 speed, type, ahd->channel, ahd->our_id); 6278 buf += len; 6279 6280 sprintf(buf, "%s, %d SCBs", ahd->bus_description, 6281 ahd->scb_data.maxhscbs); 6282 } 6283 6284 static const char *channel_strings[] = { 6285 "Primary Low", 6286 "Primary High", 6287 "Secondary Low", 6288 "Secondary High" 6289 }; 6290 6291 static const char *termstat_strings[] = { 6292 "Terminated Correctly", 6293 "Over Terminated", 6294 "Under Terminated", 6295 "Not Configured" 6296 }; 6297 6298 /* 6299 * Start the board, ready for normal operation 6300 */ 6301 int 6302 ahd_init(struct ahd_softc *ahd) 6303 { 6304 uint8_t *next_vaddr; 6305 bus_addr_t next_baddr; 6306 size_t driver_data_size; 6307 int i; 6308 int error; 6309 u_int warn_user; 6310 uint8_t current_sensing; 6311 uint8_t fstat; 6312 6313 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK); 6314 6315 ahd->stack_size = ahd_probe_stack_size(ahd); 6316 ahd->saved_stack = malloc(ahd->stack_size * sizeof(uint16_t), 6317 M_DEVBUF, M_NOWAIT); 6318 if (ahd->saved_stack == NULL) 6319 return (ENOMEM); 6320 6321 /* 6322 * Verify that the compiler hasn't over-agressively 6323 * padded important structures. 6324 */ 6325 if (sizeof(struct hardware_scb) != 64) 6326 panic("Hardware SCB size is incorrect"); 6327 6328 #ifdef AHD_DEBUG 6329 if ((ahd_debug & AHD_DEBUG_SEQUENCER) != 0) 6330 ahd->flags |= AHD_SEQUENCER_DEBUG; 6331 #endif 6332 6333 /* 6334 * Default to allowing initiator operations. 6335 */ 6336 ahd->flags |= AHD_INITIATORROLE; 6337 6338 /* 6339 * Only allow target mode features if this unit has them enabled. 6340 */ 6341 if ((AHD_TMODE_ENABLE & (0x1 << ahd->unit)) == 0) 6342 ahd->features &= ~AHD_TARGETMODE; 6343 6344 #ifndef __linux__ 6345 /* DMA tag for mapping buffers into device visible space. */ 6346 if (aic_dma_tag_create(ahd, ahd->parent_dmat, /*alignment*/1, 6347 /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1, 6348 /*lowaddr*/ahd->flags & AHD_39BIT_ADDRESSING 6349 ? (bus_addr_t)0x7FFFFFFFFFULL 6350 : BUS_SPACE_MAXADDR_32BIT, 6351 /*highaddr*/BUS_SPACE_MAXADDR, 6352 /*filter*/NULL, /*filterarg*/NULL, 6353 /*maxsize*/(AHD_NSEG - 1) * PAGE_SIZE, 6354 /*nsegments*/AHD_NSEG, 6355 /*maxsegsz*/AHD_MAXTRANSFER_SIZE, 6356 /*flags*/BUS_DMA_ALLOCNOW, 6357 &ahd->buffer_dmat) != 0) { 6358 return (ENOMEM); 6359 } 6360 #endif 6361 6362 ahd->init_level++; 6363 6364 /* 6365 * DMA tag for our command fifos and other data in system memory 6366 * the card's sequencer must be able to access. For initiator 6367 * roles, we need to allocate space for the qoutfifo. When providing 6368 * for the target mode role, we must additionally provide space for 6369 * the incoming target command fifo. 6370 */ 6371 driver_data_size = AHD_SCB_MAX * sizeof(*ahd->qoutfifo) 6372 + sizeof(struct hardware_scb); 6373 if ((ahd->features & AHD_TARGETMODE) != 0) 6374 driver_data_size += AHD_TMODE_CMDS * sizeof(struct target_cmd); 6375 if ((ahd->bugs & AHD_PKT_BITBUCKET_BUG) != 0) 6376 driver_data_size += PKT_OVERRUN_BUFSIZE; 6377 if (aic_dma_tag_create(ahd, ahd->parent_dmat, /*alignment*/1, 6378 /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1, 6379 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT, 6380 /*highaddr*/BUS_SPACE_MAXADDR, 6381 /*filter*/NULL, /*filterarg*/NULL, 6382 driver_data_size, 6383 /*nsegments*/1, 6384 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT, 6385 /*flags*/0, &ahd->shared_data_dmat) != 0) { 6386 return (ENOMEM); 6387 } 6388 6389 ahd->init_level++; 6390 6391 /* Allocation of driver data */ 6392 if (aic_dmamem_alloc(ahd, ahd->shared_data_dmat, 6393 (void **)&ahd->shared_data_map.vaddr, 6394 BUS_DMA_NOWAIT | BUS_DMA_COHERENT, 6395 &ahd->shared_data_map.dmamap) != 0) { 6396 return (ENOMEM); 6397 } 6398 6399 ahd->init_level++; 6400 6401 /* And permanently map it in */ 6402 aic_dmamap_load(ahd, ahd->shared_data_dmat, ahd->shared_data_map.dmamap, 6403 ahd->shared_data_map.vaddr, driver_data_size, 6404 ahd_dmamap_cb, &ahd->shared_data_map.busaddr, 6405 /*flags*/0); 6406 ahd->qoutfifo = (struct ahd_completion *)ahd->shared_data_map.vaddr; 6407 next_vaddr = (uint8_t *)&ahd->qoutfifo[AHD_QOUT_SIZE]; 6408 next_baddr = ahd->shared_data_map.busaddr 6409 + AHD_QOUT_SIZE*sizeof(struct ahd_completion); 6410 if ((ahd->features & AHD_TARGETMODE) != 0) { 6411 ahd->targetcmds = (struct target_cmd *)next_vaddr; 6412 next_vaddr += AHD_TMODE_CMDS * sizeof(struct target_cmd); 6413 next_baddr += AHD_TMODE_CMDS * sizeof(struct target_cmd); 6414 } 6415 6416 if ((ahd->bugs & AHD_PKT_BITBUCKET_BUG) != 0) { 6417 ahd->overrun_buf = next_vaddr; 6418 next_vaddr += PKT_OVERRUN_BUFSIZE; 6419 next_baddr += PKT_OVERRUN_BUFSIZE; 6420 } 6421 6422 /* 6423 * We need one SCB to serve as the "next SCB". Since the 6424 * tag identifier in this SCB will never be used, there is 6425 * no point in using a valid HSCB tag from an SCB pulled from 6426 * the standard free pool. So, we allocate this "sentinel" 6427 * specially from the DMA safe memory chunk used for the QOUTFIFO. 6428 */ 6429 ahd->next_queued_hscb = (struct hardware_scb *)next_vaddr; 6430 ahd->next_queued_hscb_map = &ahd->shared_data_map; 6431 ahd->next_queued_hscb->hscb_busaddr = aic_htole32(next_baddr); 6432 6433 ahd->init_level++; 6434 6435 /* Allocate SCB data now that buffer_dmat is initialized */ 6436 if (ahd_init_scbdata(ahd) != 0) 6437 return (ENOMEM); 6438 6439 if ((ahd->flags & AHD_INITIATORROLE) == 0) 6440 ahd->flags &= ~AHD_RESET_BUS_A; 6441 6442 /* 6443 * Before committing these settings to the chip, give 6444 * the OSM one last chance to modify our configuration. 6445 */ 6446 ahd_platform_init(ahd); 6447 6448 /* Bring up the chip. */ 6449 ahd_chip_init(ahd); 6450 6451 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK); 6452 6453 if ((ahd->flags & AHD_CURRENT_SENSING) == 0) 6454 goto init_done; 6455 6456 /* 6457 * Verify termination based on current draw and 6458 * warn user if the bus is over/under terminated. 6459 */ 6460 error = ahd_write_flexport(ahd, FLXADDR_ROMSTAT_CURSENSECTL, 6461 CURSENSE_ENB); 6462 if (error != 0) { 6463 printf("%s: current sensing timeout 1\n", ahd_name(ahd)); 6464 goto init_done; 6465 } 6466 for (i = 20, fstat = FLX_FSTAT_BUSY; 6467 (fstat & FLX_FSTAT_BUSY) != 0 && i; i--) { 6468 error = ahd_read_flexport(ahd, FLXADDR_FLEXSTAT, &fstat); 6469 if (error != 0) { 6470 printf("%s: current sensing timeout 2\n", 6471 ahd_name(ahd)); 6472 goto init_done; 6473 } 6474 } 6475 if (i == 0) { 6476 printf("%s: Timedout during current-sensing test\n", 6477 ahd_name(ahd)); 6478 goto init_done; 6479 } 6480 6481 /* Latch Current Sensing status. */ 6482 error = ahd_read_flexport(ahd, FLXADDR_CURRENT_STAT, ¤t_sensing); 6483 if (error != 0) { 6484 printf("%s: current sensing timeout 3\n", ahd_name(ahd)); 6485 goto init_done; 6486 } 6487 6488 /* Diable current sensing. */ 6489 ahd_write_flexport(ahd, FLXADDR_ROMSTAT_CURSENSECTL, 0); 6490 6491 #ifdef AHD_DEBUG 6492 if ((ahd_debug & AHD_SHOW_TERMCTL) != 0) { 6493 printf("%s: current_sensing == 0x%x\n", 6494 ahd_name(ahd), current_sensing); 6495 } 6496 #endif 6497 warn_user = 0; 6498 for (i = 0; i < 4; i++, current_sensing >>= FLX_CSTAT_SHIFT) { 6499 u_int term_stat; 6500 6501 term_stat = (current_sensing & FLX_CSTAT_MASK); 6502 switch (term_stat) { 6503 case FLX_CSTAT_OVER: 6504 case FLX_CSTAT_UNDER: 6505 warn_user++; 6506 case FLX_CSTAT_INVALID: 6507 case FLX_CSTAT_OKAY: 6508 if (warn_user == 0 && bootverbose == 0) 6509 break; 6510 printf("%s: %s Channel %s\n", ahd_name(ahd), 6511 channel_strings[i], termstat_strings[term_stat]); 6512 break; 6513 } 6514 } 6515 if (warn_user) { 6516 printf("%s: WARNING. Termination is not configured correctly.\n" 6517 "%s: WARNING. SCSI bus operations may FAIL.\n", 6518 ahd_name(ahd), ahd_name(ahd)); 6519 AHD_CORRECTABLE_ERROR(ahd); 6520 } 6521 init_done: 6522 ahd_restart(ahd); 6523 aic_timer_reset(&ahd->stat_timer, AHD_STAT_UPDATE_MS, 6524 ahd_stat_timer, ahd); 6525 return (0); 6526 } 6527 6528 /* 6529 * (Re)initialize chip state after a chip reset. 6530 */ 6531 static void 6532 ahd_chip_init(struct ahd_softc *ahd) 6533 { 6534 uint32_t busaddr; 6535 u_int sxfrctl1; 6536 u_int scsiseq_template; 6537 u_int wait; 6538 u_int i; 6539 u_int target; 6540 6541 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI); 6542 /* 6543 * Take the LED out of diagnostic mode 6544 */ 6545 ahd_outb(ahd, SBLKCTL, ahd_inb(ahd, SBLKCTL) & ~(DIAGLEDEN|DIAGLEDON)); 6546 6547 /* 6548 * Return HS_MAILBOX to its default value. 6549 */ 6550 ahd->hs_mailbox = 0; 6551 ahd_outb(ahd, HS_MAILBOX, 0); 6552 6553 /* Set the SCSI Id, SXFRCTL0, SXFRCTL1, and SIMODE1. */ 6554 ahd_outb(ahd, IOWNID, ahd->our_id); 6555 ahd_outb(ahd, TOWNID, ahd->our_id); 6556 sxfrctl1 = (ahd->flags & AHD_TERM_ENB_A) != 0 ? STPWEN : 0; 6557 sxfrctl1 |= (ahd->flags & AHD_SPCHK_ENB_A) != 0 ? ENSPCHK : 0; 6558 if ((ahd->bugs & AHD_LONG_SETIMO_BUG) 6559 && (ahd->seltime != STIMESEL_MIN)) { 6560 /* 6561 * The selection timer duration is twice as long 6562 * as it should be. Halve it by adding "1" to 6563 * the user specified setting. 6564 */ 6565 sxfrctl1 |= ahd->seltime + STIMESEL_BUG_ADJ; 6566 } else { 6567 sxfrctl1 |= ahd->seltime; 6568 } 6569 6570 ahd_outb(ahd, SXFRCTL0, DFON); 6571 ahd_outb(ahd, SXFRCTL1, sxfrctl1|ahd->seltime|ENSTIMER|ACTNEGEN); 6572 ahd_outb(ahd, SIMODE1, ENSELTIMO|ENSCSIRST|ENSCSIPERR); 6573 6574 /* 6575 * Now that termination is set, wait for up 6576 * to 500ms for our transceivers to settle. If 6577 * the adapter does not have a cable attached, 6578 * the transceivers may never settle, so don't 6579 * complain if we fail here. 6580 */ 6581 for (wait = 10000; 6582 (ahd_inb(ahd, SBLKCTL) & (ENAB40|ENAB20)) == 0 && wait; 6583 wait--) 6584 aic_delay(100); 6585 6586 /* Clear any false bus resets due to the transceivers settling */ 6587 ahd_outb(ahd, CLRSINT1, CLRSCSIRSTI); 6588 ahd_outb(ahd, CLRINT, CLRSCSIINT); 6589 6590 /* Initialize mode specific S/G state. */ 6591 for (i = 0; i < 2; i++) { 6592 ahd_set_modes(ahd, AHD_MODE_DFF0 + i, AHD_MODE_DFF0 + i); 6593 ahd_outb(ahd, LONGJMP_ADDR + 1, INVALID_ADDR); 6594 ahd_outb(ahd, SG_STATE, 0); 6595 ahd_outb(ahd, CLRSEQINTSRC, 0xFF); 6596 ahd_outb(ahd, SEQIMODE, 6597 ENSAVEPTRS|ENCFG4DATA|ENCFG4ISTAT 6598 |ENCFG4TSTAT|ENCFG4ICMD|ENCFG4TCMD); 6599 } 6600 6601 ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG); 6602 ahd_outb(ahd, DSCOMMAND0, ahd_inb(ahd, DSCOMMAND0)|MPARCKEN|CACHETHEN); 6603 ahd_outb(ahd, DFF_THRSH, RD_DFTHRSH_75|WR_DFTHRSH_75); 6604 ahd_outb(ahd, SIMODE0, ENIOERR|ENOVERRUN); 6605 ahd_outb(ahd, SIMODE3, ENNTRAMPERR|ENOSRAMPERR); 6606 if ((ahd->bugs & AHD_BUSFREEREV_BUG) != 0) { 6607 ahd_outb(ahd, OPTIONMODE, AUTOACKEN|AUTO_MSGOUT_DE); 6608 } else { 6609 ahd_outb(ahd, OPTIONMODE, AUTOACKEN|BUSFREEREV|AUTO_MSGOUT_DE); 6610 } 6611 ahd_outb(ahd, SCSCHKN, CURRFIFODEF|WIDERESEN|SHVALIDSTDIS); 6612 if ((ahd->chip & AHD_BUS_MASK) == AHD_PCIX) 6613 /* 6614 * Do not issue a target abort when a split completion 6615 * error occurs. Let our PCIX interrupt handler deal 6616 * with it instead. H2A4 Razor #625 6617 */ 6618 ahd_outb(ahd, PCIXCTL, ahd_inb(ahd, PCIXCTL) | SPLTSTADIS); 6619 6620 if ((ahd->bugs & AHD_LQOOVERRUN_BUG) != 0) 6621 ahd_outb(ahd, LQOSCSCTL, LQONOCHKOVER); 6622 6623 /* 6624 * Tweak IOCELL settings. 6625 */ 6626 if ((ahd->flags & AHD_HP_BOARD) != 0) { 6627 for (i = 0; i < NUMDSPS; i++) { 6628 ahd_outb(ahd, DSPSELECT, i); 6629 ahd_outb(ahd, WRTBIASCTL, WRTBIASCTL_HP_DEFAULT); 6630 } 6631 #ifdef AHD_DEBUG 6632 if ((ahd_debug & AHD_SHOW_MISC) != 0) 6633 printf("%s: WRTBIASCTL now 0x%x\n", ahd_name(ahd), 6634 WRTBIASCTL_HP_DEFAULT); 6635 #endif 6636 } 6637 ahd_setup_iocell_workaround(ahd); 6638 6639 /* 6640 * Enable LQI Manager interrupts. 6641 */ 6642 ahd_outb(ahd, LQIMODE1, ENLQIPHASE_LQ|ENLQIPHASE_NLQ|ENLIQABORT 6643 | ENLQICRCI_LQ|ENLQICRCI_NLQ|ENLQIBADLQI 6644 | ENLQIOVERI_LQ|ENLQIOVERI_NLQ); 6645 ahd_outb(ahd, LQOMODE0, ENLQOATNLQ|ENLQOATNPKT|ENLQOTCRC); 6646 /* 6647 * We choose to have the sequencer catch LQOPHCHGINPKT errors 6648 * manually for the command phase at the start of a packetized 6649 * selection case. ENLQOBUSFREE should be made redundant by 6650 * the BUSFREE interrupt, but it seems that some LQOBUSFREE 6651 * events fail to assert the BUSFREE interrupt so we must 6652 * also enable LQOBUSFREE interrupts. 6653 */ 6654 ahd_outb(ahd, LQOMODE1, ENLQOBUSFREE); 6655 6656 /* 6657 * Setup sequencer interrupt handlers. 6658 */ 6659 ahd_outw(ahd, INTVEC1_ADDR, ahd_resolve_seqaddr(ahd, LABEL_seq_isr)); 6660 ahd_outw(ahd, INTVEC2_ADDR, ahd_resolve_seqaddr(ahd, LABEL_timer_isr)); 6661 6662 /* 6663 * Setup SCB Offset registers. 6664 */ 6665 if ((ahd->bugs & AHD_PKT_LUN_BUG) != 0) { 6666 ahd_outb(ahd, LUNPTR, offsetof(struct hardware_scb, 6667 pkt_long_lun)); 6668 } else { 6669 ahd_outb(ahd, LUNPTR, offsetof(struct hardware_scb, lun)); 6670 } 6671 ahd_outb(ahd, CMDLENPTR, offsetof(struct hardware_scb, cdb_len)); 6672 ahd_outb(ahd, ATTRPTR, offsetof(struct hardware_scb, task_attribute)); 6673 ahd_outb(ahd, FLAGPTR, offsetof(struct hardware_scb, task_management)); 6674 ahd_outb(ahd, CMDPTR, offsetof(struct hardware_scb, 6675 shared_data.idata.cdb)); 6676 ahd_outb(ahd, QNEXTPTR, 6677 offsetof(struct hardware_scb, next_hscb_busaddr)); 6678 ahd_outb(ahd, ABRTBITPTR, MK_MESSAGE_BIT_OFFSET); 6679 ahd_outb(ahd, ABRTBYTEPTR, offsetof(struct hardware_scb, control)); 6680 if ((ahd->bugs & AHD_PKT_LUN_BUG) != 0) { 6681 ahd_outb(ahd, LUNLEN, 6682 sizeof(ahd->next_queued_hscb->pkt_long_lun) - 1); 6683 } else { 6684 ahd_outb(ahd, LUNLEN, LUNLEN_SINGLE_LEVEL_LUN); 6685 } 6686 ahd_outb(ahd, CDBLIMIT, SCB_CDB_LEN_PTR - 1); 6687 ahd_outb(ahd, MAXCMD, 0xFF); 6688 ahd_outb(ahd, SCBAUTOPTR, 6689 AUSCBPTR_EN | offsetof(struct hardware_scb, tag)); 6690 6691 /* We haven't been enabled for target mode yet. */ 6692 ahd_outb(ahd, MULTARGID, 0); 6693 ahd_outb(ahd, MULTARGID + 1, 0); 6694 6695 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI); 6696 /* Initialize the negotiation table. */ 6697 if ((ahd->features & AHD_NEW_IOCELL_OPTS) == 0) { 6698 /* 6699 * Clear the spare bytes in the neg table to avoid 6700 * spurious parity errors. 6701 */ 6702 for (target = 0; target < AHD_NUM_TARGETS; target++) { 6703 ahd_outb(ahd, NEGOADDR, target); 6704 ahd_outb(ahd, ANNEXCOL, AHD_ANNEXCOL_PER_DEV0); 6705 for (i = 0; i < AHD_NUM_PER_DEV_ANNEXCOLS; i++) 6706 ahd_outb(ahd, ANNEXDAT, 0); 6707 } 6708 } 6709 for (target = 0; target < AHD_NUM_TARGETS; target++) { 6710 struct ahd_devinfo devinfo; 6711 struct ahd_initiator_tinfo *tinfo; 6712 struct ahd_tmode_tstate *tstate; 6713 6714 tinfo = ahd_fetch_transinfo(ahd, 'A', ahd->our_id, 6715 target, &tstate); 6716 ahd_compile_devinfo(&devinfo, ahd->our_id, 6717 target, CAM_LUN_WILDCARD, 6718 'A', ROLE_INITIATOR); 6719 ahd_update_neg_table(ahd, &devinfo, &tinfo->curr); 6720 } 6721 6722 ahd_outb(ahd, CLRSINT3, NTRAMPERR|OSRAMPERR); 6723 ahd_outb(ahd, CLRINT, CLRSCSIINT); 6724 6725 #ifdef NEEDS_MORE_TESTING 6726 /* 6727 * Always enable abort on incoming L_Qs if this feature is 6728 * supported. We use this to catch invalid SCB references. 6729 */ 6730 if ((ahd->bugs & AHD_ABORT_LQI_BUG) == 0) 6731 ahd_outb(ahd, LQCTL1, ABORTPENDING); 6732 else 6733 #endif 6734 ahd_outb(ahd, LQCTL1, 0); 6735 6736 /* All of our queues are empty */ 6737 ahd->qoutfifonext = 0; 6738 ahd->qoutfifonext_valid_tag = QOUTFIFO_ENTRY_VALID; 6739 ahd_outb(ahd, QOUTFIFO_ENTRY_VALID_TAG, QOUTFIFO_ENTRY_VALID); 6740 for (i = 0; i < AHD_QOUT_SIZE; i++) 6741 ahd->qoutfifo[i].valid_tag = 0; 6742 ahd_sync_qoutfifo(ahd, BUS_DMASYNC_PREREAD); 6743 6744 ahd->qinfifonext = 0; 6745 for (i = 0; i < AHD_QIN_SIZE; i++) 6746 ahd->qinfifo[i] = SCB_LIST_NULL; 6747 6748 if ((ahd->features & AHD_TARGETMODE) != 0) { 6749 /* All target command blocks start out invalid. */ 6750 for (i = 0; i < AHD_TMODE_CMDS; i++) 6751 ahd->targetcmds[i].cmd_valid = 0; 6752 ahd_sync_tqinfifo(ahd, BUS_DMASYNC_PREREAD); 6753 ahd->tqinfifonext = 1; 6754 ahd_outb(ahd, KERNEL_TQINPOS, ahd->tqinfifonext - 1); 6755 ahd_outb(ahd, TQINPOS, ahd->tqinfifonext); 6756 } 6757 6758 /* Initialize Scratch Ram. */ 6759 ahd_outb(ahd, SEQ_FLAGS, 0); 6760 ahd_outb(ahd, SEQ_FLAGS2, 0); 6761 6762 /* We don't have any waiting selections */ 6763 ahd_outw(ahd, WAITING_TID_HEAD, SCB_LIST_NULL); 6764 ahd_outw(ahd, WAITING_TID_TAIL, SCB_LIST_NULL); 6765 ahd_outw(ahd, MK_MESSAGE_SCB, SCB_LIST_NULL); 6766 ahd_outw(ahd, MK_MESSAGE_SCSIID, 0xFF); 6767 for (i = 0; i < AHD_NUM_TARGETS; i++) 6768 ahd_outw(ahd, WAITING_SCB_TAILS + (2 * i), SCB_LIST_NULL); 6769 6770 /* 6771 * Nobody is waiting to be DMAed into the QOUTFIFO. 6772 */ 6773 ahd_outw(ahd, COMPLETE_SCB_HEAD, SCB_LIST_NULL); 6774 ahd_outw(ahd, COMPLETE_SCB_DMAINPROG_HEAD, SCB_LIST_NULL); 6775 ahd_outw(ahd, COMPLETE_DMA_SCB_HEAD, SCB_LIST_NULL); 6776 ahd_outw(ahd, COMPLETE_DMA_SCB_TAIL, SCB_LIST_NULL); 6777 ahd_outw(ahd, COMPLETE_ON_QFREEZE_HEAD, SCB_LIST_NULL); 6778 6779 /* 6780 * The Freeze Count is 0. 6781 */ 6782 ahd->qfreeze_cnt = 0; 6783 ahd_outw(ahd, QFREEZE_COUNT, 0); 6784 ahd_outw(ahd, KERNEL_QFREEZE_COUNT, 0); 6785 6786 /* 6787 * Tell the sequencer where it can find our arrays in memory. 6788 */ 6789 busaddr = ahd->shared_data_map.busaddr; 6790 ahd_outl(ahd, SHARED_DATA_ADDR, busaddr); 6791 ahd_outl(ahd, QOUTFIFO_NEXT_ADDR, busaddr); 6792 6793 /* 6794 * Setup the allowed SCSI Sequences based on operational mode. 6795 * If we are a target, we'll enable select in operations once 6796 * we've had a lun enabled. 6797 */ 6798 scsiseq_template = ENAUTOATNP; 6799 if ((ahd->flags & AHD_INITIATORROLE) != 0) 6800 scsiseq_template |= ENRSELI; 6801 ahd_outb(ahd, SCSISEQ_TEMPLATE, scsiseq_template); 6802 6803 /* There are no busy SCBs yet. */ 6804 for (target = 0; target < AHD_NUM_TARGETS; target++) { 6805 int lun; 6806 6807 for (lun = 0; lun < AHD_NUM_LUNS_NONPKT; lun++) 6808 ahd_unbusy_tcl(ahd, BUILD_TCL_RAW(target, 'A', lun)); 6809 } 6810 6811 /* 6812 * Initialize the group code to command length table. 6813 * Vendor Unique codes are set to 0 so we only capture 6814 * the first byte of the cdb. These can be overridden 6815 * when target mode is enabled. 6816 */ 6817 ahd_outb(ahd, CMDSIZE_TABLE, 5); 6818 ahd_outb(ahd, CMDSIZE_TABLE + 1, 9); 6819 ahd_outb(ahd, CMDSIZE_TABLE + 2, 9); 6820 ahd_outb(ahd, CMDSIZE_TABLE + 3, 0); 6821 ahd_outb(ahd, CMDSIZE_TABLE + 4, 15); 6822 ahd_outb(ahd, CMDSIZE_TABLE + 5, 11); 6823 ahd_outb(ahd, CMDSIZE_TABLE + 6, 0); 6824 ahd_outb(ahd, CMDSIZE_TABLE + 7, 0); 6825 6826 /* Tell the sequencer of our initial queue positions */ 6827 ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN); 6828 ahd_outb(ahd, QOFF_CTLSTA, SCB_QSIZE_512); 6829 ahd->qinfifonext = 0; 6830 ahd_set_hnscb_qoff(ahd, ahd->qinfifonext); 6831 ahd_set_hescb_qoff(ahd, 0); 6832 ahd_set_snscb_qoff(ahd, 0); 6833 ahd_set_sescb_qoff(ahd, 0); 6834 ahd_set_sdscb_qoff(ahd, 0); 6835 6836 /* 6837 * Tell the sequencer which SCB will be the next one it receives. 6838 */ 6839 busaddr = aic_le32toh(ahd->next_queued_hscb->hscb_busaddr); 6840 ahd_outl(ahd, NEXT_QUEUED_SCB_ADDR, busaddr); 6841 6842 /* 6843 * Default to coalescing disabled. 6844 */ 6845 ahd_outw(ahd, INT_COALESCING_CMDCOUNT, 0); 6846 ahd_outw(ahd, CMDS_PENDING, 0); 6847 ahd_update_coalescing_values(ahd, ahd->int_coalescing_timer, 6848 ahd->int_coalescing_maxcmds, 6849 ahd->int_coalescing_mincmds); 6850 ahd_enable_coalescing(ahd, FALSE); 6851 6852 ahd_loadseq(ahd); 6853 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI); 6854 } 6855 6856 /* 6857 * Setup default device and controller settings. 6858 * This should only be called if our probe has 6859 * determined that no configuration data is available. 6860 */ 6861 int 6862 ahd_default_config(struct ahd_softc *ahd) 6863 { 6864 int targ; 6865 6866 ahd->our_id = 7; 6867 6868 /* 6869 * Allocate a tstate to house information for our 6870 * initiator presence on the bus as well as the user 6871 * data for any target mode initiator. 6872 */ 6873 if (ahd_alloc_tstate(ahd, ahd->our_id, 'A') == NULL) { 6874 printf("%s: unable to allocate ahd_tmode_tstate. " 6875 "Failing attach\n", ahd_name(ahd)); 6876 AHD_FATAL_ERROR(ahd); 6877 return (ENOMEM); 6878 } 6879 6880 for (targ = 0; targ < AHD_NUM_TARGETS; targ++) { 6881 struct ahd_devinfo devinfo; 6882 struct ahd_initiator_tinfo *tinfo; 6883 struct ahd_tmode_tstate *tstate; 6884 uint16_t target_mask; 6885 6886 tinfo = ahd_fetch_transinfo(ahd, 'A', ahd->our_id, 6887 targ, &tstate); 6888 /* 6889 * We support SPC2 and SPI4. 6890 */ 6891 tinfo->user.protocol_version = 4; 6892 tinfo->user.transport_version = 4; 6893 6894 target_mask = 0x01 << targ; 6895 ahd->user_discenable |= target_mask; 6896 tstate->discenable |= target_mask; 6897 ahd->user_tagenable |= target_mask; 6898 #ifdef AHD_FORCE_160 6899 tinfo->user.period = AHD_SYNCRATE_DT; 6900 #else 6901 tinfo->user.period = AHD_SYNCRATE_160; 6902 #endif 6903 tinfo->user.offset = MAX_OFFSET; 6904 tinfo->user.ppr_options = MSG_EXT_PPR_RD_STRM 6905 | MSG_EXT_PPR_WR_FLOW 6906 | MSG_EXT_PPR_HOLD_MCS 6907 | MSG_EXT_PPR_IU_REQ 6908 | MSG_EXT_PPR_QAS_REQ 6909 | MSG_EXT_PPR_DT_REQ; 6910 if ((ahd->features & AHD_RTI) != 0) 6911 tinfo->user.ppr_options |= MSG_EXT_PPR_RTI; 6912 6913 tinfo->user.width = MSG_EXT_WDTR_BUS_16_BIT; 6914 6915 /* 6916 * Start out Async/Narrow/Untagged and with 6917 * conservative protocol support. 6918 */ 6919 tinfo->goal.protocol_version = 2; 6920 tinfo->goal.transport_version = 2; 6921 tinfo->curr.protocol_version = 2; 6922 tinfo->curr.transport_version = 2; 6923 ahd_compile_devinfo(&devinfo, ahd->our_id, 6924 targ, CAM_LUN_WILDCARD, 6925 'A', ROLE_INITIATOR); 6926 tstate->tagenable &= ~target_mask; 6927 ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT, 6928 AHD_TRANS_CUR|AHD_TRANS_GOAL, /*paused*/TRUE); 6929 ahd_set_syncrate(ahd, &devinfo, /*period*/0, /*offset*/0, 6930 /*ppr_options*/0, AHD_TRANS_CUR|AHD_TRANS_GOAL, 6931 /*paused*/TRUE); 6932 } 6933 return (0); 6934 } 6935 6936 /* 6937 * Parse device configuration information. 6938 */ 6939 int 6940 ahd_parse_cfgdata(struct ahd_softc *ahd, struct seeprom_config *sc) 6941 { 6942 int targ; 6943 int max_targ; 6944 6945 max_targ = sc->max_targets & CFMAXTARG; 6946 ahd->our_id = sc->brtime_id & CFSCSIID; 6947 6948 /* 6949 * Allocate a tstate to house information for our 6950 * initiator presence on the bus as well as the user 6951 * data for any target mode initiator. 6952 */ 6953 if (ahd_alloc_tstate(ahd, ahd->our_id, 'A') == NULL) { 6954 printf("%s: unable to allocate ahd_tmode_tstate. " 6955 "Failing attach\n", ahd_name(ahd)); 6956 AHD_FATAL_ERROR(ahd); 6957 return (ENOMEM); 6958 } 6959 6960 for (targ = 0; targ < max_targ; targ++) { 6961 struct ahd_devinfo devinfo; 6962 struct ahd_initiator_tinfo *tinfo; 6963 struct ahd_transinfo *user_tinfo; 6964 struct ahd_tmode_tstate *tstate; 6965 uint16_t target_mask; 6966 6967 tinfo = ahd_fetch_transinfo(ahd, 'A', ahd->our_id, 6968 targ, &tstate); 6969 user_tinfo = &tinfo->user; 6970 6971 /* 6972 * We support SPC2 and SPI4. 6973 */ 6974 tinfo->user.protocol_version = 4; 6975 tinfo->user.transport_version = 4; 6976 6977 target_mask = 0x01 << targ; 6978 ahd->user_discenable &= ~target_mask; 6979 tstate->discenable &= ~target_mask; 6980 ahd->user_tagenable &= ~target_mask; 6981 if (sc->device_flags[targ] & CFDISC) { 6982 tstate->discenable |= target_mask; 6983 ahd->user_discenable |= target_mask; 6984 ahd->user_tagenable |= target_mask; 6985 } else { 6986 /* 6987 * Cannot be packetized without disconnection. 6988 */ 6989 sc->device_flags[targ] &= ~CFPACKETIZED; 6990 } 6991 6992 user_tinfo->ppr_options = 0; 6993 user_tinfo->period = (sc->device_flags[targ] & CFXFER); 6994 if (user_tinfo->period < CFXFER_ASYNC) { 6995 if (user_tinfo->period <= AHD_PERIOD_10MHz) 6996 user_tinfo->ppr_options |= MSG_EXT_PPR_DT_REQ; 6997 user_tinfo->offset = MAX_OFFSET; 6998 } else { 6999 user_tinfo->offset = 0; 7000 user_tinfo->period = AHD_ASYNC_XFER_PERIOD; 7001 } 7002 #ifdef AHD_FORCE_160 7003 if (user_tinfo->period <= AHD_SYNCRATE_160) 7004 user_tinfo->period = AHD_SYNCRATE_DT; 7005 #endif 7006 7007 if ((sc->device_flags[targ] & CFPACKETIZED) != 0) { 7008 user_tinfo->ppr_options |= MSG_EXT_PPR_RD_STRM 7009 | MSG_EXT_PPR_WR_FLOW 7010 | MSG_EXT_PPR_HOLD_MCS 7011 | MSG_EXT_PPR_IU_REQ; 7012 if ((ahd->features & AHD_RTI) != 0) 7013 user_tinfo->ppr_options |= MSG_EXT_PPR_RTI; 7014 } 7015 7016 if ((sc->device_flags[targ] & CFQAS) != 0) 7017 user_tinfo->ppr_options |= MSG_EXT_PPR_QAS_REQ; 7018 7019 if ((sc->device_flags[targ] & CFWIDEB) != 0) 7020 user_tinfo->width = MSG_EXT_WDTR_BUS_16_BIT; 7021 else 7022 user_tinfo->width = MSG_EXT_WDTR_BUS_8_BIT; 7023 #ifdef AHD_DEBUG 7024 if ((ahd_debug & AHD_SHOW_MISC) != 0) 7025 printf("(%d): %x:%x:%x:%x\n", targ, user_tinfo->width, 7026 user_tinfo->period, user_tinfo->offset, 7027 user_tinfo->ppr_options); 7028 #endif 7029 /* 7030 * Start out Async/Narrow/Untagged and with 7031 * conservative protocol support. 7032 */ 7033 tstate->tagenable &= ~target_mask; 7034 tinfo->goal.protocol_version = 2; 7035 tinfo->goal.transport_version = 2; 7036 tinfo->curr.protocol_version = 2; 7037 tinfo->curr.transport_version = 2; 7038 ahd_compile_devinfo(&devinfo, ahd->our_id, 7039 targ, CAM_LUN_WILDCARD, 7040 'A', ROLE_INITIATOR); 7041 ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT, 7042 AHD_TRANS_CUR|AHD_TRANS_GOAL, /*paused*/TRUE); 7043 ahd_set_syncrate(ahd, &devinfo, /*period*/0, /*offset*/0, 7044 /*ppr_options*/0, AHD_TRANS_CUR|AHD_TRANS_GOAL, 7045 /*paused*/TRUE); 7046 } 7047 7048 ahd->flags &= ~AHD_SPCHK_ENB_A; 7049 if (sc->bios_control & CFSPARITY) 7050 ahd->flags |= AHD_SPCHK_ENB_A; 7051 7052 ahd->flags &= ~AHD_RESET_BUS_A; 7053 if (sc->bios_control & CFRESETB) 7054 ahd->flags |= AHD_RESET_BUS_A; 7055 7056 ahd->flags &= ~AHD_EXTENDED_TRANS_A; 7057 if (sc->bios_control & CFEXTEND) 7058 ahd->flags |= AHD_EXTENDED_TRANS_A; 7059 7060 ahd->flags &= ~AHD_BIOS_ENABLED; 7061 if ((sc->bios_control & CFBIOSSTATE) == CFBS_ENABLED) 7062 ahd->flags |= AHD_BIOS_ENABLED; 7063 7064 ahd->flags &= ~AHD_STPWLEVEL_A; 7065 if ((sc->adapter_control & CFSTPWLEVEL) != 0) 7066 ahd->flags |= AHD_STPWLEVEL_A; 7067 7068 return (0); 7069 } 7070 7071 /* 7072 * Parse device configuration information. 7073 */ 7074 int 7075 ahd_parse_vpddata(struct ahd_softc *ahd, struct vpd_config *vpd) 7076 { 7077 int error; 7078 7079 error = ahd_verify_vpd_cksum(vpd); 7080 if (error == 0) 7081 return (EINVAL); 7082 if ((vpd->bios_flags & VPDBOOTHOST) != 0) 7083 ahd->flags |= AHD_BOOT_CHANNEL; 7084 return (0); 7085 } 7086 7087 void 7088 ahd_intr_enable(struct ahd_softc *ahd, int enable) 7089 { 7090 u_int hcntrl; 7091 7092 hcntrl = ahd_inb(ahd, HCNTRL); 7093 hcntrl &= ~INTEN; 7094 ahd->pause &= ~INTEN; 7095 ahd->unpause &= ~INTEN; 7096 if (enable) { 7097 hcntrl |= INTEN; 7098 ahd->pause |= INTEN; 7099 ahd->unpause |= INTEN; 7100 } 7101 ahd_outb(ahd, HCNTRL, hcntrl); 7102 } 7103 7104 void 7105 ahd_update_coalescing_values(struct ahd_softc *ahd, u_int timer, u_int maxcmds, 7106 u_int mincmds) 7107 { 7108 if (timer > AHD_TIMER_MAX_US) 7109 timer = AHD_TIMER_MAX_US; 7110 ahd->int_coalescing_timer = timer; 7111 7112 if (maxcmds > AHD_INT_COALESCING_MAXCMDS_MAX) 7113 maxcmds = AHD_INT_COALESCING_MAXCMDS_MAX; 7114 if (mincmds > AHD_INT_COALESCING_MINCMDS_MAX) 7115 mincmds = AHD_INT_COALESCING_MINCMDS_MAX; 7116 ahd->int_coalescing_maxcmds = maxcmds; 7117 ahd_outw(ahd, INT_COALESCING_TIMER, timer / AHD_TIMER_US_PER_TICK); 7118 ahd_outb(ahd, INT_COALESCING_MAXCMDS, -maxcmds); 7119 ahd_outb(ahd, INT_COALESCING_MINCMDS, -mincmds); 7120 } 7121 7122 void 7123 ahd_enable_coalescing(struct ahd_softc *ahd, int enable) 7124 { 7125 7126 ahd->hs_mailbox &= ~ENINT_COALESCE; 7127 if (enable) 7128 ahd->hs_mailbox |= ENINT_COALESCE; 7129 ahd_outb(ahd, HS_MAILBOX, ahd->hs_mailbox); 7130 ahd_flush_device_writes(ahd); 7131 ahd_run_qoutfifo(ahd); 7132 } 7133 7134 /* 7135 * Ensure that the card is paused in a location 7136 * outside of all critical sections and that all 7137 * pending work is completed prior to returning. 7138 * This routine should only be called from outside 7139 * an interrupt context. 7140 */ 7141 void 7142 ahd_pause_and_flushwork(struct ahd_softc *ahd) 7143 { 7144 u_int intstat; 7145 u_int maxloops; 7146 7147 maxloops = 1000; 7148 ahd->flags |= AHD_ALL_INTERRUPTS; 7149 ahd_pause(ahd); 7150 /* 7151 * Freeze the outgoing selections. We do this only 7152 * until we are safely paused without further selections 7153 * pending. 7154 */ 7155 ahd->qfreeze_cnt--; 7156 ahd_outw(ahd, KERNEL_QFREEZE_COUNT, ahd->qfreeze_cnt); 7157 ahd_outb(ahd, SEQ_FLAGS2, ahd_inb(ahd, SEQ_FLAGS2) | SELECTOUT_QFROZEN); 7158 do { 7159 7160 ahd_unpause(ahd); 7161 /* 7162 * Give the sequencer some time to service 7163 * any active selections. 7164 */ 7165 aic_delay(500); 7166 7167 ahd_intr(ahd); 7168 ahd_pause(ahd); 7169 intstat = ahd_inb(ahd, INTSTAT); 7170 if ((intstat & INT_PEND) == 0) { 7171 ahd_clear_critical_section(ahd); 7172 intstat = ahd_inb(ahd, INTSTAT); 7173 } 7174 } while (--maxloops 7175 && (intstat != 0xFF || (ahd->features & AHD_REMOVABLE) == 0) 7176 && ((intstat & INT_PEND) != 0 7177 || (ahd_inb(ahd, SCSISEQ0) & ENSELO) != 0 7178 || (ahd_inb(ahd, SSTAT0) & (SELDO|SELINGO)) != 0)); 7179 7180 if (maxloops == 0) { 7181 printf("Infinite interrupt loop, INTSTAT = %x", 7182 ahd_inb(ahd, INTSTAT)); 7183 AHD_FATAL_ERROR(ahd); 7184 } 7185 ahd->qfreeze_cnt++; 7186 ahd_outw(ahd, KERNEL_QFREEZE_COUNT, ahd->qfreeze_cnt); 7187 7188 ahd_flush_qoutfifo(ahd); 7189 7190 ahd_platform_flushwork(ahd); 7191 ahd->flags &= ~AHD_ALL_INTERRUPTS; 7192 } 7193 7194 int 7195 ahd_suspend(struct ahd_softc *ahd) 7196 { 7197 7198 ahd_pause_and_flushwork(ahd); 7199 7200 if (LIST_FIRST(&ahd->pending_scbs) != NULL) { 7201 ahd_unpause(ahd); 7202 return (EBUSY); 7203 } 7204 ahd_shutdown(ahd); 7205 return (0); 7206 } 7207 7208 int 7209 ahd_resume(struct ahd_softc *ahd) 7210 { 7211 7212 ahd_reset(ahd, /*reinit*/TRUE); 7213 ahd_intr_enable(ahd, TRUE); 7214 ahd_restart(ahd); 7215 return (0); 7216 } 7217 7218 /************************** Busy Target Table *********************************/ 7219 /* 7220 * Set SCBPTR to the SCB that contains the busy 7221 * table entry for TCL. Return the offset into 7222 * the SCB that contains the entry for TCL. 7223 * saved_scbid is dereferenced and set to the 7224 * scbid that should be restored once manipualtion 7225 * of the TCL entry is complete. 7226 */ 7227 static __inline u_int 7228 ahd_index_busy_tcl(struct ahd_softc *ahd, u_int *saved_scbid, u_int tcl) 7229 { 7230 /* 7231 * Index to the SCB that contains the busy entry. 7232 */ 7233 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK); 7234 *saved_scbid = ahd_get_scbptr(ahd); 7235 ahd_set_scbptr(ahd, TCL_LUN(tcl) 7236 | ((TCL_TARGET_OFFSET(tcl) & 0xC) << 4)); 7237 7238 /* 7239 * And now calculate the SCB offset to the entry. 7240 * Each entry is 2 bytes wide, hence the 7241 * multiplication by 2. 7242 */ 7243 return (((TCL_TARGET_OFFSET(tcl) & 0x3) << 1) + SCB_DISCONNECTED_LISTS); 7244 } 7245 7246 /* 7247 * Return the untagged transaction id for a given target/channel lun. 7248 */ 7249 u_int 7250 ahd_find_busy_tcl(struct ahd_softc *ahd, u_int tcl) 7251 { 7252 u_int scbid; 7253 u_int scb_offset; 7254 u_int saved_scbptr; 7255 7256 scb_offset = ahd_index_busy_tcl(ahd, &saved_scbptr, tcl); 7257 scbid = ahd_inw_scbram(ahd, scb_offset); 7258 ahd_set_scbptr(ahd, saved_scbptr); 7259 return (scbid); 7260 } 7261 7262 void 7263 ahd_busy_tcl(struct ahd_softc *ahd, u_int tcl, u_int scbid) 7264 { 7265 u_int scb_offset; 7266 u_int saved_scbptr; 7267 7268 scb_offset = ahd_index_busy_tcl(ahd, &saved_scbptr, tcl); 7269 ahd_outw(ahd, scb_offset, scbid); 7270 ahd_set_scbptr(ahd, saved_scbptr); 7271 } 7272 7273 /************************** SCB and SCB queue management **********************/ 7274 int 7275 ahd_match_scb(struct ahd_softc *ahd, struct scb *scb, int target, 7276 char channel, int lun, u_int tag, role_t role) 7277 { 7278 int targ = SCB_GET_TARGET(ahd, scb); 7279 char chan = SCB_GET_CHANNEL(ahd, scb); 7280 int slun = SCB_GET_LUN(scb); 7281 int match; 7282 7283 match = ((chan == channel) || (channel == ALL_CHANNELS)); 7284 if (match != 0) 7285 match = ((targ == target) || (target == CAM_TARGET_WILDCARD)); 7286 if (match != 0) 7287 match = ((lun == slun) || (lun == CAM_LUN_WILDCARD)); 7288 if (match != 0) { 7289 #ifdef AHD_TARGET_MODE 7290 int group; 7291 7292 group = XPT_FC_GROUP(scb->io_ctx->ccb_h.func_code); 7293 if (role == ROLE_INITIATOR) { 7294 match = (group != XPT_FC_GROUP_TMODE) 7295 && ((tag == SCB_GET_TAG(scb)) 7296 || (tag == SCB_LIST_NULL)); 7297 } else if (role == ROLE_TARGET) { 7298 match = (group == XPT_FC_GROUP_TMODE) 7299 && ((tag == scb->io_ctx->csio.tag_id) 7300 || (tag == SCB_LIST_NULL)); 7301 } 7302 #else /* !AHD_TARGET_MODE */ 7303 match = ((tag == SCB_GET_TAG(scb)) || (tag == SCB_LIST_NULL)); 7304 #endif /* AHD_TARGET_MODE */ 7305 } 7306 7307 return match; 7308 } 7309 7310 void 7311 ahd_freeze_devq(struct ahd_softc *ahd, struct scb *scb) 7312 { 7313 int target; 7314 char channel; 7315 int lun; 7316 7317 target = SCB_GET_TARGET(ahd, scb); 7318 lun = SCB_GET_LUN(scb); 7319 channel = SCB_GET_CHANNEL(ahd, scb); 7320 7321 ahd_search_qinfifo(ahd, target, channel, lun, 7322 /*tag*/SCB_LIST_NULL, ROLE_UNKNOWN, 7323 CAM_REQUEUE_REQ, SEARCH_COMPLETE); 7324 7325 ahd_platform_freeze_devq(ahd, scb); 7326 } 7327 7328 void 7329 ahd_qinfifo_requeue_tail(struct ahd_softc *ahd, struct scb *scb) 7330 { 7331 struct scb *prev_scb; 7332 ahd_mode_state saved_modes; 7333 7334 saved_modes = ahd_save_modes(ahd); 7335 ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN); 7336 prev_scb = NULL; 7337 if (ahd_qinfifo_count(ahd) != 0) { 7338 u_int prev_tag; 7339 u_int prev_pos; 7340 7341 prev_pos = AHD_QIN_WRAP(ahd->qinfifonext - 1); 7342 prev_tag = ahd->qinfifo[prev_pos]; 7343 prev_scb = ahd_lookup_scb(ahd, prev_tag); 7344 } 7345 ahd_qinfifo_requeue(ahd, prev_scb, scb); 7346 ahd_set_hnscb_qoff(ahd, ahd->qinfifonext); 7347 ahd_restore_modes(ahd, saved_modes); 7348 } 7349 7350 static void 7351 ahd_qinfifo_requeue(struct ahd_softc *ahd, struct scb *prev_scb, 7352 struct scb *scb) 7353 { 7354 if (prev_scb == NULL) { 7355 uint32_t busaddr; 7356 7357 busaddr = aic_le32toh(scb->hscb->hscb_busaddr); 7358 ahd_outl(ahd, NEXT_QUEUED_SCB_ADDR, busaddr); 7359 } else { 7360 prev_scb->hscb->next_hscb_busaddr = scb->hscb->hscb_busaddr; 7361 ahd_sync_scb(ahd, prev_scb, 7362 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); 7363 } 7364 ahd->qinfifo[AHD_QIN_WRAP(ahd->qinfifonext)] = SCB_GET_TAG(scb); 7365 ahd->qinfifonext++; 7366 scb->hscb->next_hscb_busaddr = ahd->next_queued_hscb->hscb_busaddr; 7367 ahd_sync_scb(ahd, scb, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); 7368 } 7369 7370 static int 7371 ahd_qinfifo_count(struct ahd_softc *ahd) 7372 { 7373 u_int qinpos; 7374 u_int wrap_qinpos; 7375 u_int wrap_qinfifonext; 7376 7377 AHD_ASSERT_MODES(ahd, AHD_MODE_CCHAN_MSK, AHD_MODE_CCHAN_MSK); 7378 qinpos = ahd_get_snscb_qoff(ahd); 7379 wrap_qinpos = AHD_QIN_WRAP(qinpos); 7380 wrap_qinfifonext = AHD_QIN_WRAP(ahd->qinfifonext); 7381 if (wrap_qinfifonext >= wrap_qinpos) 7382 return (wrap_qinfifonext - wrap_qinpos); 7383 else 7384 return (wrap_qinfifonext 7385 + NUM_ELEMENTS(ahd->qinfifo) - wrap_qinpos); 7386 } 7387 7388 void 7389 ahd_reset_cmds_pending(struct ahd_softc *ahd) 7390 { 7391 struct scb *scb; 7392 ahd_mode_state saved_modes; 7393 u_int pending_cmds; 7394 7395 saved_modes = ahd_save_modes(ahd); 7396 ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN); 7397 7398 /* 7399 * Don't count any commands as outstanding that the 7400 * sequencer has already marked for completion. 7401 */ 7402 ahd_flush_qoutfifo(ahd); 7403 7404 pending_cmds = 0; 7405 LIST_FOREACH(scb, &ahd->pending_scbs, pending_links) { 7406 pending_cmds++; 7407 } 7408 ahd_outw(ahd, CMDS_PENDING, pending_cmds - ahd_qinfifo_count(ahd)); 7409 ahd_restore_modes(ahd, saved_modes); 7410 ahd->flags &= ~AHD_UPDATE_PEND_CMDS; 7411 } 7412 7413 void 7414 ahd_done_with_status(struct ahd_softc *ahd, struct scb *scb, uint32_t status) 7415 { 7416 cam_status ostat; 7417 cam_status cstat; 7418 7419 ostat = aic_get_transaction_status(scb); 7420 if (ostat == CAM_REQ_INPROG) 7421 aic_set_transaction_status(scb, status); 7422 cstat = aic_get_transaction_status(scb); 7423 if (cstat != CAM_REQ_CMP) 7424 aic_freeze_scb(scb); 7425 ahd_done(ahd, scb); 7426 } 7427 7428 int 7429 ahd_search_qinfifo(struct ahd_softc *ahd, int target, char channel, 7430 int lun, u_int tag, role_t role, uint32_t status, 7431 ahd_search_action action) 7432 { 7433 struct scb *scb; 7434 struct scb *mk_msg_scb; 7435 struct scb *prev_scb; 7436 ahd_mode_state saved_modes; 7437 u_int qinstart; 7438 u_int qinpos; 7439 u_int qintail; 7440 u_int tid_next; 7441 u_int tid_prev; 7442 u_int scbid; 7443 u_int seq_flags2; 7444 u_int savedscbptr; 7445 uint32_t busaddr; 7446 int found; 7447 int targets; 7448 7449 /* Must be in CCHAN mode */ 7450 saved_modes = ahd_save_modes(ahd); 7451 ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN); 7452 7453 /* 7454 * Halt any pending SCB DMA. The sequencer will reinitiate 7455 * this dma if the qinfifo is not empty once we unpause. 7456 */ 7457 if ((ahd_inb(ahd, CCSCBCTL) & (CCARREN|CCSCBEN|CCSCBDIR)) 7458 == (CCARREN|CCSCBEN|CCSCBDIR)) { 7459 ahd_outb(ahd, CCSCBCTL, 7460 ahd_inb(ahd, CCSCBCTL) & ~(CCARREN|CCSCBEN)); 7461 while ((ahd_inb(ahd, CCSCBCTL) & (CCARREN|CCSCBEN)) != 0) 7462 ; 7463 } 7464 /* Determine sequencer's position in the qinfifo. */ 7465 qintail = AHD_QIN_WRAP(ahd->qinfifonext); 7466 qinstart = ahd_get_snscb_qoff(ahd); 7467 qinpos = AHD_QIN_WRAP(qinstart); 7468 found = 0; 7469 prev_scb = NULL; 7470 7471 if (action == SEARCH_PRINT) { 7472 printf("qinstart = %d qinfifonext = %d\nQINFIFO:", 7473 qinstart, ahd->qinfifonext); 7474 } 7475 7476 /* 7477 * Start with an empty queue. Entries that are not chosen 7478 * for removal will be re-added to the queue as we go. 7479 */ 7480 ahd->qinfifonext = qinstart; 7481 busaddr = aic_le32toh(ahd->next_queued_hscb->hscb_busaddr); 7482 ahd_outl(ahd, NEXT_QUEUED_SCB_ADDR, busaddr); 7483 7484 while (qinpos != qintail) { 7485 scb = ahd_lookup_scb(ahd, ahd->qinfifo[qinpos]); 7486 if (scb == NULL) { 7487 printf("qinpos = %d, SCB index = %d\n", 7488 qinpos, ahd->qinfifo[qinpos]); 7489 AHD_FATAL_ERROR(ahd); 7490 panic("Loop 1\n"); 7491 } 7492 7493 if (ahd_match_scb(ahd, scb, target, channel, lun, tag, role)) { 7494 /* 7495 * We found an scb that needs to be acted on. 7496 */ 7497 found++; 7498 switch (action) { 7499 case SEARCH_COMPLETE: 7500 if ((scb->flags & SCB_ACTIVE) == 0) 7501 printf("Inactive SCB in qinfifo\n"); 7502 ahd_done_with_status(ahd, scb, status); 7503 /* FALLTHROUGH */ 7504 case SEARCH_REMOVE: 7505 break; 7506 case SEARCH_PRINT: 7507 printf(" 0x%x", ahd->qinfifo[qinpos]); 7508 /* FALLTHROUGH */ 7509 case SEARCH_COUNT: 7510 ahd_qinfifo_requeue(ahd, prev_scb, scb); 7511 prev_scb = scb; 7512 break; 7513 } 7514 } else { 7515 ahd_qinfifo_requeue(ahd, prev_scb, scb); 7516 prev_scb = scb; 7517 } 7518 qinpos = AHD_QIN_WRAP(qinpos+1); 7519 } 7520 7521 ahd_set_hnscb_qoff(ahd, ahd->qinfifonext); 7522 7523 if (action == SEARCH_PRINT) 7524 printf("\nWAITING_TID_QUEUES:\n"); 7525 7526 /* 7527 * Search waiting for selection lists. We traverse the 7528 * list of "their ids" waiting for selection and, if 7529 * appropriate, traverse the SCBs of each "their id" 7530 * looking for matches. 7531 */ 7532 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI); 7533 seq_flags2 = ahd_inb(ahd, SEQ_FLAGS2); 7534 if ((seq_flags2 & PENDING_MK_MESSAGE) != 0) { 7535 scbid = ahd_inw(ahd, MK_MESSAGE_SCB); 7536 mk_msg_scb = ahd_lookup_scb(ahd, scbid); 7537 } else 7538 mk_msg_scb = NULL; 7539 savedscbptr = ahd_get_scbptr(ahd); 7540 tid_next = ahd_inw(ahd, WAITING_TID_HEAD); 7541 tid_prev = SCB_LIST_NULL; 7542 targets = 0; 7543 for (scbid = tid_next; !SCBID_IS_NULL(scbid); scbid = tid_next) { 7544 u_int tid_head; 7545 u_int tid_tail; 7546 7547 targets++; 7548 if (targets > AHD_NUM_TARGETS) 7549 panic("TID LIST LOOP"); 7550 7551 if (scbid >= ahd->scb_data.numscbs) { 7552 printf("%s: Waiting TID List inconsistency. " 7553 "SCB index == 0x%x, yet numscbs == 0x%x.", 7554 ahd_name(ahd), scbid, ahd->scb_data.numscbs); 7555 ahd_dump_card_state(ahd); 7556 panic("for safety"); 7557 } 7558 scb = ahd_lookup_scb(ahd, scbid); 7559 if (scb == NULL) { 7560 printf("%s: SCB = 0x%x Not Active!\n", 7561 ahd_name(ahd), scbid); 7562 panic("Waiting TID List traversal\n"); 7563 } 7564 ahd_set_scbptr(ahd, scbid); 7565 tid_next = ahd_inw_scbram(ahd, SCB_NEXT2); 7566 if (ahd_match_scb(ahd, scb, target, channel, CAM_LUN_WILDCARD, 7567 SCB_LIST_NULL, ROLE_UNKNOWN) == 0) { 7568 tid_prev = scbid; 7569 continue; 7570 } 7571 7572 /* 7573 * We found a list of scbs that needs to be searched. 7574 */ 7575 if (action == SEARCH_PRINT) 7576 printf(" %d ( ", SCB_GET_TARGET(ahd, scb)); 7577 tid_head = scbid; 7578 found += ahd_search_scb_list(ahd, target, channel, 7579 lun, tag, role, status, 7580 action, &tid_head, &tid_tail, 7581 SCB_GET_TARGET(ahd, scb)); 7582 /* 7583 * Check any MK_MESSAGE SCB that is still waiting to 7584 * enter this target's waiting for selection queue. 7585 */ 7586 if (mk_msg_scb != NULL 7587 && ahd_match_scb(ahd, mk_msg_scb, target, channel, 7588 lun, tag, role)) { 7589 7590 /* 7591 * We found an scb that needs to be acted on. 7592 */ 7593 found++; 7594 switch (action) { 7595 case SEARCH_COMPLETE: 7596 if ((mk_msg_scb->flags & SCB_ACTIVE) == 0) 7597 printf("Inactive SCB pending MK_MSG\n"); 7598 ahd_done_with_status(ahd, mk_msg_scb, status); 7599 /* FALLTHROUGH */ 7600 case SEARCH_REMOVE: 7601 { 7602 u_int tail_offset; 7603 7604 printf("Removing MK_MSG scb\n"); 7605 7606 /* 7607 * Reset our tail to the tail of the 7608 * main per-target list. 7609 */ 7610 tail_offset = WAITING_SCB_TAILS 7611 + (2 * SCB_GET_TARGET(ahd, mk_msg_scb)); 7612 ahd_outw(ahd, tail_offset, tid_tail); 7613 7614 seq_flags2 &= ~PENDING_MK_MESSAGE; 7615 ahd_outb(ahd, SEQ_FLAGS2, seq_flags2); 7616 ahd_outw(ahd, CMDS_PENDING, 7617 ahd_inw(ahd, CMDS_PENDING)-1); 7618 mk_msg_scb = NULL; 7619 break; 7620 } 7621 case SEARCH_PRINT: 7622 printf(" 0x%x", SCB_GET_TAG(scb)); 7623 /* FALLTHROUGH */ 7624 case SEARCH_COUNT: 7625 break; 7626 } 7627 } 7628 7629 if (mk_msg_scb != NULL 7630 && SCBID_IS_NULL(tid_head) 7631 && ahd_match_scb(ahd, scb, target, channel, CAM_LUN_WILDCARD, 7632 SCB_LIST_NULL, ROLE_UNKNOWN)) { 7633 7634 /* 7635 * When removing the last SCB for a target 7636 * queue with a pending MK_MESSAGE scb, we 7637 * must queue the MK_MESSAGE scb. 7638 */ 7639 printf("Queueing mk_msg_scb\n"); 7640 tid_head = ahd_inw(ahd, MK_MESSAGE_SCB); 7641 seq_flags2 &= ~PENDING_MK_MESSAGE; 7642 ahd_outb(ahd, SEQ_FLAGS2, seq_flags2); 7643 mk_msg_scb = NULL; 7644 } 7645 if (tid_head != scbid) 7646 ahd_stitch_tid_list(ahd, tid_prev, tid_head, tid_next); 7647 if (!SCBID_IS_NULL(tid_head)) 7648 tid_prev = tid_head; 7649 if (action == SEARCH_PRINT) 7650 printf(")\n"); 7651 } 7652 7653 /* Restore saved state. */ 7654 ahd_set_scbptr(ahd, savedscbptr); 7655 ahd_restore_modes(ahd, saved_modes); 7656 return (found); 7657 } 7658 7659 static int 7660 ahd_search_scb_list(struct ahd_softc *ahd, int target, char channel, 7661 int lun, u_int tag, role_t role, uint32_t status, 7662 ahd_search_action action, u_int *list_head, 7663 u_int *list_tail, u_int tid) 7664 { 7665 struct scb *scb; 7666 u_int scbid; 7667 u_int next; 7668 u_int prev; 7669 int found; 7670 7671 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK); 7672 found = 0; 7673 prev = SCB_LIST_NULL; 7674 next = *list_head; 7675 *list_tail = SCB_LIST_NULL; 7676 for (scbid = next; !SCBID_IS_NULL(scbid); scbid = next) { 7677 if (scbid >= ahd->scb_data.numscbs) { 7678 printf("%s:SCB List inconsistency. " 7679 "SCB == 0x%x, yet numscbs == 0x%x.", 7680 ahd_name(ahd), scbid, ahd->scb_data.numscbs); 7681 ahd_dump_card_state(ahd); 7682 panic("for safety"); 7683 } 7684 scb = ahd_lookup_scb(ahd, scbid); 7685 if (scb == NULL) { 7686 printf("%s: SCB = %d Not Active!\n", 7687 ahd_name(ahd), scbid); 7688 panic("Waiting List traversal\n"); 7689 } 7690 ahd_set_scbptr(ahd, scbid); 7691 *list_tail = scbid; 7692 next = ahd_inw_scbram(ahd, SCB_NEXT); 7693 if (ahd_match_scb(ahd, scb, target, channel, 7694 lun, SCB_LIST_NULL, role) == 0) { 7695 prev = scbid; 7696 continue; 7697 } 7698 found++; 7699 switch (action) { 7700 case SEARCH_COMPLETE: 7701 if ((scb->flags & SCB_ACTIVE) == 0) 7702 printf("Inactive SCB in Waiting List\n"); 7703 ahd_done_with_status(ahd, scb, status); 7704 /* FALLTHROUGH */ 7705 case SEARCH_REMOVE: 7706 ahd_rem_wscb(ahd, scbid, prev, next, tid); 7707 *list_tail = prev; 7708 if (SCBID_IS_NULL(prev)) 7709 *list_head = next; 7710 break; 7711 case SEARCH_PRINT: 7712 printf("0x%x ", scbid); 7713 case SEARCH_COUNT: 7714 prev = scbid; 7715 break; 7716 } 7717 if (found > AHD_SCB_MAX) 7718 panic("SCB LIST LOOP"); 7719 } 7720 if (action == SEARCH_COMPLETE 7721 || action == SEARCH_REMOVE) 7722 ahd_outw(ahd, CMDS_PENDING, ahd_inw(ahd, CMDS_PENDING) - found); 7723 return (found); 7724 } 7725 7726 static void 7727 ahd_stitch_tid_list(struct ahd_softc *ahd, u_int tid_prev, 7728 u_int tid_cur, u_int tid_next) 7729 { 7730 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK); 7731 7732 if (SCBID_IS_NULL(tid_cur)) { 7733 7734 /* Bypass current TID list */ 7735 if (SCBID_IS_NULL(tid_prev)) { 7736 ahd_outw(ahd, WAITING_TID_HEAD, tid_next); 7737 } else { 7738 ahd_set_scbptr(ahd, tid_prev); 7739 ahd_outw(ahd, SCB_NEXT2, tid_next); 7740 } 7741 if (SCBID_IS_NULL(tid_next)) 7742 ahd_outw(ahd, WAITING_TID_TAIL, tid_prev); 7743 } else { 7744 7745 /* Stitch through tid_cur */ 7746 if (SCBID_IS_NULL(tid_prev)) { 7747 ahd_outw(ahd, WAITING_TID_HEAD, tid_cur); 7748 } else { 7749 ahd_set_scbptr(ahd, tid_prev); 7750 ahd_outw(ahd, SCB_NEXT2, tid_cur); 7751 } 7752 ahd_set_scbptr(ahd, tid_cur); 7753 ahd_outw(ahd, SCB_NEXT2, tid_next); 7754 7755 if (SCBID_IS_NULL(tid_next)) 7756 ahd_outw(ahd, WAITING_TID_TAIL, tid_cur); 7757 } 7758 } 7759 7760 /* 7761 * Manipulate the waiting for selection list and return the 7762 * scb that follows the one that we remove. 7763 */ 7764 static u_int 7765 ahd_rem_wscb(struct ahd_softc *ahd, u_int scbid, 7766 u_int prev, u_int next, u_int tid) 7767 { 7768 u_int tail_offset; 7769 7770 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK); 7771 if (!SCBID_IS_NULL(prev)) { 7772 ahd_set_scbptr(ahd, prev); 7773 ahd_outw(ahd, SCB_NEXT, next); 7774 } 7775 7776 /* 7777 * SCBs that have MK_MESSAGE set in them may 7778 * cause the tail pointer to be updated without 7779 * setting the next pointer of the previous tail. 7780 * Only clear the tail if the removed SCB was 7781 * the tail. 7782 */ 7783 tail_offset = WAITING_SCB_TAILS + (2 * tid); 7784 if (SCBID_IS_NULL(next) 7785 && ahd_inw(ahd, tail_offset) == scbid) 7786 ahd_outw(ahd, tail_offset, prev); 7787 7788 ahd_add_scb_to_free_list(ahd, scbid); 7789 return (next); 7790 } 7791 7792 /* 7793 * Add the SCB as selected by SCBPTR onto the on chip list of 7794 * free hardware SCBs. This list is empty/unused if we are not 7795 * performing SCB paging. 7796 */ 7797 static void 7798 ahd_add_scb_to_free_list(struct ahd_softc *ahd, u_int scbid) 7799 { 7800 /* XXX Need some other mechanism to designate "free". */ 7801 /* 7802 * Invalidate the tag so that our abort 7803 * routines don't think it's active. 7804 ahd_outb(ahd, SCB_TAG, SCB_LIST_NULL); 7805 */ 7806 } 7807 7808 /******************************** Error Handling ******************************/ 7809 /* 7810 * Abort all SCBs that match the given description (target/channel/lun/tag), 7811 * setting their status to the passed in status if the status has not already 7812 * been modified from CAM_REQ_INPROG. This routine assumes that the sequencer 7813 * is paused before it is called. 7814 */ 7815 int 7816 ahd_abort_scbs(struct ahd_softc *ahd, int target, char channel, 7817 int lun, u_int tag, role_t role, uint32_t status) 7818 { 7819 struct scb *scbp; 7820 struct scb *scbp_next; 7821 u_int i, j; 7822 u_int maxtarget; 7823 u_int minlun; 7824 u_int maxlun; 7825 int found; 7826 ahd_mode_state saved_modes; 7827 7828 /* restore this when we're done */ 7829 saved_modes = ahd_save_modes(ahd); 7830 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI); 7831 7832 found = ahd_search_qinfifo(ahd, target, channel, lun, SCB_LIST_NULL, 7833 role, CAM_REQUEUE_REQ, SEARCH_COMPLETE); 7834 7835 /* 7836 * Clean out the busy target table for any untagged commands. 7837 */ 7838 i = 0; 7839 maxtarget = 16; 7840 if (target != CAM_TARGET_WILDCARD) { 7841 i = target; 7842 if (channel == 'B') 7843 i += 8; 7844 maxtarget = i + 1; 7845 } 7846 7847 if (lun == CAM_LUN_WILDCARD) { 7848 minlun = 0; 7849 maxlun = AHD_NUM_LUNS_NONPKT; 7850 } else if (lun >= AHD_NUM_LUNS_NONPKT) { 7851 minlun = maxlun = 0; 7852 } else { 7853 minlun = lun; 7854 maxlun = lun + 1; 7855 } 7856 7857 if (role != ROLE_TARGET) { 7858 for (;i < maxtarget; i++) { 7859 for (j = minlun;j < maxlun; j++) { 7860 u_int scbid; 7861 u_int tcl; 7862 7863 tcl = BUILD_TCL_RAW(i, 'A', j); 7864 scbid = ahd_find_busy_tcl(ahd, tcl); 7865 scbp = ahd_lookup_scb(ahd, scbid); 7866 if (scbp == NULL 7867 || ahd_match_scb(ahd, scbp, target, channel, 7868 lun, tag, role) == 0) 7869 continue; 7870 ahd_unbusy_tcl(ahd, BUILD_TCL_RAW(i, 'A', j)); 7871 } 7872 } 7873 } 7874 7875 /* 7876 * Don't abort commands that have already completed, 7877 * but haven't quite made it up to the host yet. 7878 */ 7879 ahd_flush_qoutfifo(ahd); 7880 7881 /* 7882 * Go through the pending CCB list and look for 7883 * commands for this target that are still active. 7884 * These are other tagged commands that were 7885 * disconnected when the reset occurred. 7886 */ 7887 scbp_next = LIST_FIRST(&ahd->pending_scbs); 7888 while (scbp_next != NULL) { 7889 scbp = scbp_next; 7890 scbp_next = LIST_NEXT(scbp, pending_links); 7891 if (ahd_match_scb(ahd, scbp, target, channel, lun, tag, role)) { 7892 cam_status ostat; 7893 7894 ostat = aic_get_transaction_status(scbp); 7895 if (ostat == CAM_REQ_INPROG) 7896 aic_set_transaction_status(scbp, status); 7897 if (aic_get_transaction_status(scbp) != CAM_REQ_CMP) 7898 aic_freeze_scb(scbp); 7899 if ((scbp->flags & SCB_ACTIVE) == 0) 7900 printf("Inactive SCB on pending list\n"); 7901 ahd_done(ahd, scbp); 7902 found++; 7903 } 7904 } 7905 ahd_restore_modes(ahd, saved_modes); 7906 ahd_platform_abort_scbs(ahd, target, channel, lun, tag, role, status); 7907 ahd->flags |= AHD_UPDATE_PEND_CMDS; 7908 return found; 7909 } 7910 7911 static void 7912 ahd_reset_current_bus(struct ahd_softc *ahd) 7913 { 7914 uint8_t scsiseq; 7915 7916 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK); 7917 ahd_outb(ahd, SIMODE1, ahd_inb(ahd, SIMODE1) & ~ENSCSIRST); 7918 scsiseq = ahd_inb(ahd, SCSISEQ0) & ~(ENSELO|ENARBO|SCSIRSTO); 7919 ahd_outb(ahd, SCSISEQ0, scsiseq | SCSIRSTO); 7920 ahd_flush_device_writes(ahd); 7921 aic_delay(AHD_BUSRESET_DELAY); 7922 /* Turn off the bus reset */ 7923 ahd_outb(ahd, SCSISEQ0, scsiseq); 7924 ahd_flush_device_writes(ahd); 7925 aic_delay(AHD_BUSRESET_DELAY); 7926 if ((ahd->bugs & AHD_SCSIRST_BUG) != 0) { 7927 /* 7928 * 2A Razor #474 7929 * Certain chip state is not cleared for 7930 * SCSI bus resets that we initiate, so 7931 * we must reset the chip. 7932 */ 7933 ahd_reset(ahd, /*reinit*/TRUE); 7934 ahd_intr_enable(ahd, /*enable*/TRUE); 7935 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK); 7936 } 7937 7938 ahd_clear_intstat(ahd); 7939 } 7940 7941 int 7942 ahd_reset_channel(struct ahd_softc *ahd, char channel, int initiate_reset) 7943 { 7944 struct ahd_devinfo devinfo; 7945 u_int initiator; 7946 u_int target; 7947 u_int max_scsiid; 7948 int found; 7949 u_int fifo; 7950 u_int next_fifo; 7951 7952 ahd->pending_device = NULL; 7953 7954 ahd_compile_devinfo(&devinfo, 7955 CAM_TARGET_WILDCARD, 7956 CAM_TARGET_WILDCARD, 7957 CAM_LUN_WILDCARD, 7958 channel, ROLE_UNKNOWN); 7959 ahd_pause(ahd); 7960 7961 /* Make sure the sequencer is in a safe location. */ 7962 ahd_clear_critical_section(ahd); 7963 7964 #ifdef AHD_TARGET_MODE 7965 if ((ahd->flags & AHD_TARGETROLE) != 0) { 7966 ahd_run_tqinfifo(ahd, /*paused*/TRUE); 7967 } 7968 #endif 7969 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI); 7970 7971 /* 7972 * Disable selections so no automatic hardware 7973 * functions will modify chip state. 7974 */ 7975 ahd_outb(ahd, SCSISEQ0, 0); 7976 ahd_outb(ahd, SCSISEQ1, 0); 7977 7978 /* 7979 * Safely shut down our DMA engines. Always start with 7980 * the FIFO that is not currently active (if any are 7981 * actively connected). 7982 */ 7983 next_fifo = fifo = ahd_inb(ahd, DFFSTAT) & CURRFIFO; 7984 if (next_fifo > CURRFIFO_1) 7985 /* If disconneced, arbitrarily start with FIFO1. */ 7986 next_fifo = fifo = 0; 7987 do { 7988 next_fifo ^= CURRFIFO_1; 7989 ahd_set_modes(ahd, next_fifo, next_fifo); 7990 ahd_outb(ahd, DFCNTRL, 7991 ahd_inb(ahd, DFCNTRL) & ~(SCSIEN|HDMAEN)); 7992 while ((ahd_inb(ahd, DFCNTRL) & HDMAENACK) != 0) 7993 aic_delay(10); 7994 /* 7995 * Set CURRFIFO to the now inactive channel. 7996 */ 7997 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI); 7998 ahd_outb(ahd, DFFSTAT, next_fifo); 7999 } while (next_fifo != fifo); 8000 8001 /* 8002 * Reset the bus if we are initiating this reset 8003 */ 8004 ahd_clear_msg_state(ahd); 8005 ahd_outb(ahd, SIMODE1, 8006 ahd_inb(ahd, SIMODE1) & ~(ENBUSFREE|ENSCSIRST)); 8007 8008 if (initiate_reset) 8009 ahd_reset_current_bus(ahd); 8010 8011 ahd_clear_intstat(ahd); 8012 8013 /* 8014 * Clean up all the state information for the 8015 * pending transactions on this bus. 8016 */ 8017 found = ahd_abort_scbs(ahd, CAM_TARGET_WILDCARD, channel, 8018 CAM_LUN_WILDCARD, SCB_LIST_NULL, 8019 ROLE_UNKNOWN, CAM_SCSI_BUS_RESET); 8020 8021 /* 8022 * Cleanup anything left in the FIFOs. 8023 */ 8024 ahd_clear_fifo(ahd, 0); 8025 ahd_clear_fifo(ahd, 1); 8026 8027 /* 8028 * Revert to async/narrow transfers until we renegotiate. 8029 */ 8030 max_scsiid = (ahd->features & AHD_WIDE) ? 15 : 7; 8031 for (target = 0; target <= max_scsiid; target++) { 8032 8033 if (ahd->enabled_targets[target] == NULL) 8034 continue; 8035 for (initiator = 0; initiator <= max_scsiid; initiator++) { 8036 struct ahd_devinfo devinfo; 8037 8038 ahd_compile_devinfo(&devinfo, target, initiator, 8039 CAM_LUN_WILDCARD, 8040 'A', ROLE_UNKNOWN); 8041 ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT, 8042 AHD_TRANS_CUR, /*paused*/TRUE); 8043 ahd_set_syncrate(ahd, &devinfo, /*period*/0, 8044 /*offset*/0, /*ppr_options*/0, 8045 AHD_TRANS_CUR, /*paused*/TRUE); 8046 } 8047 } 8048 8049 #ifdef AHD_TARGET_MODE 8050 max_scsiid = (ahd->features & AHD_WIDE) ? 15 : 7; 8051 8052 /* 8053 * Send an immediate notify ccb to all target more peripheral 8054 * drivers affected by this action. 8055 */ 8056 for (target = 0; target <= max_scsiid; target++) { 8057 struct ahd_tmode_tstate* tstate; 8058 u_int lun; 8059 8060 tstate = ahd->enabled_targets[target]; 8061 if (tstate == NULL) 8062 continue; 8063 for (lun = 0; lun < AHD_NUM_LUNS; lun++) { 8064 struct ahd_tmode_lstate* lstate; 8065 8066 lstate = tstate->enabled_luns[lun]; 8067 if (lstate == NULL) 8068 continue; 8069 8070 ahd_queue_lstate_event(ahd, lstate, CAM_TARGET_WILDCARD, 8071 EVENT_TYPE_BUS_RESET, /*arg*/0); 8072 ahd_send_lstate_events(ahd, lstate); 8073 } 8074 } 8075 #endif 8076 /* Notify the XPT that a bus reset occurred */ 8077 ahd_send_async(ahd, devinfo.channel, CAM_TARGET_WILDCARD, 8078 CAM_LUN_WILDCARD, AC_BUS_RESET, NULL); 8079 ahd_restart(ahd); 8080 /* 8081 * Freeze the SIMQ until our poller can determine that 8082 * the bus reset has really gone away. We set the initial 8083 * timer to 0 to have the check performed as soon as possible 8084 * from the timer context. 8085 */ 8086 if ((ahd->flags & AHD_RESET_POLL_ACTIVE) == 0) { 8087 ahd->flags |= AHD_RESET_POLL_ACTIVE; 8088 aic_freeze_simq(ahd); 8089 aic_timer_reset(&ahd->reset_timer, 0, ahd_reset_poll, ahd); 8090 } 8091 return (found); 8092 } 8093 8094 8095 #define AHD_RESET_POLL_MS 1 8096 static void 8097 ahd_reset_poll(void *arg) 8098 { 8099 struct ahd_softc *ahd = (struct ahd_softc *)arg; 8100 u_int scsiseq1; 8101 8102 ahd_lock(ahd); 8103 ahd_pause(ahd); 8104 ahd_update_modes(ahd); 8105 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI); 8106 ahd_outb(ahd, CLRSINT1, CLRSCSIRSTI); 8107 if ((ahd_inb(ahd, SSTAT1) & SCSIRSTI) != 0) { 8108 aic_timer_reset(&ahd->reset_timer, AHD_RESET_POLL_MS, 8109 ahd_reset_poll, ahd); 8110 ahd_unpause(ahd); 8111 ahd_unlock(ahd); 8112 return; 8113 } 8114 8115 /* Reset is now low. Complete chip reinitialization. */ 8116 ahd_outb(ahd, SIMODE1, ahd_inb(ahd, SIMODE1) | ENSCSIRST); 8117 scsiseq1 = ahd_inb(ahd, SCSISEQ_TEMPLATE); 8118 ahd_outb(ahd, SCSISEQ1, scsiseq1 & (ENSELI|ENRSELI|ENAUTOATNP)); 8119 ahd_unpause(ahd); 8120 ahd->flags &= ~AHD_RESET_POLL_ACTIVE; 8121 aic_release_simq(ahd); 8122 ahd_unlock(ahd); 8123 } 8124 8125 /**************************** Statistics Processing ***************************/ 8126 static void 8127 ahd_stat_timer(void *arg) 8128 { 8129 struct ahd_softc *ahd = (struct ahd_softc *)arg; 8130 int enint_coal; 8131 8132 ahd_lock(ahd); 8133 enint_coal = ahd->hs_mailbox & ENINT_COALESCE; 8134 if (ahd->cmdcmplt_total > ahd->int_coalescing_threshold) 8135 enint_coal |= ENINT_COALESCE; 8136 else if (ahd->cmdcmplt_total < ahd->int_coalescing_stop_threshold) 8137 enint_coal &= ~ENINT_COALESCE; 8138 8139 if (enint_coal != (ahd->hs_mailbox & ENINT_COALESCE)) { 8140 ahd_enable_coalescing(ahd, enint_coal); 8141 #ifdef AHD_DEBUG 8142 if ((ahd_debug & AHD_SHOW_INT_COALESCING) != 0) 8143 printf("%s: Interrupt coalescing " 8144 "now %sabled. Cmds %d\n", 8145 ahd_name(ahd), 8146 (enint_coal & ENINT_COALESCE) ? "en" : "dis", 8147 ahd->cmdcmplt_total); 8148 #endif 8149 } 8150 8151 ahd->cmdcmplt_bucket = (ahd->cmdcmplt_bucket+1) & (AHD_STAT_BUCKETS-1); 8152 ahd->cmdcmplt_total -= ahd->cmdcmplt_counts[ahd->cmdcmplt_bucket]; 8153 ahd->cmdcmplt_counts[ahd->cmdcmplt_bucket] = 0; 8154 aic_timer_reset(&ahd->stat_timer, AHD_STAT_UPDATE_MS, 8155 ahd_stat_timer, ahd); 8156 ahd_unlock(ahd); 8157 } 8158 8159 /****************************** Status Processing *****************************/ 8160 void 8161 ahd_handle_scb_status(struct ahd_softc *ahd, struct scb *scb) 8162 { 8163 if (scb->hscb->shared_data.istatus.scsi_status != 0) { 8164 ahd_handle_scsi_status(ahd, scb); 8165 } else { 8166 ahd_calc_residual(ahd, scb); 8167 ahd_done(ahd, scb); 8168 } 8169 } 8170 8171 void 8172 ahd_handle_scsi_status(struct ahd_softc *ahd, struct scb *scb) 8173 { 8174 struct hardware_scb *hscb; 8175 int paused; 8176 8177 /* 8178 * The sequencer freezes its select-out queue 8179 * anytime a SCSI status error occurs. We must 8180 * handle the error and increment our qfreeze count 8181 * to allow the sequencer to continue. We don't 8182 * bother clearing critical sections here since all 8183 * operations are on data structures that the sequencer 8184 * is not touching once the queue is frozen. 8185 */ 8186 hscb = scb->hscb; 8187 8188 if (ahd_is_paused(ahd)) { 8189 paused = 1; 8190 } else { 8191 paused = 0; 8192 ahd_pause(ahd); 8193 } 8194 8195 /* Freeze the queue until the client sees the error. */ 8196 ahd_freeze_devq(ahd, scb); 8197 aic_freeze_scb(scb); 8198 ahd->qfreeze_cnt++; 8199 ahd_outw(ahd, KERNEL_QFREEZE_COUNT, ahd->qfreeze_cnt); 8200 8201 if (paused == 0) 8202 ahd_unpause(ahd); 8203 8204 /* Don't want to clobber the original sense code */ 8205 if ((scb->flags & SCB_SENSE) != 0) { 8206 /* 8207 * Clear the SCB_SENSE Flag and perform 8208 * a normal command completion. 8209 */ 8210 scb->flags &= ~SCB_SENSE; 8211 aic_set_transaction_status(scb, CAM_AUTOSENSE_FAIL); 8212 ahd_done(ahd, scb); 8213 return; 8214 } 8215 aic_set_transaction_status(scb, CAM_SCSI_STATUS_ERROR); 8216 aic_set_scsi_status(scb, hscb->shared_data.istatus.scsi_status); 8217 switch (hscb->shared_data.istatus.scsi_status) { 8218 case STATUS_PKT_SENSE: 8219 { 8220 struct scsi_status_iu_header *siu; 8221 8222 ahd_sync_sense(ahd, scb, BUS_DMASYNC_POSTREAD); 8223 siu = (struct scsi_status_iu_header *)scb->sense_data; 8224 aic_set_scsi_status(scb, siu->status); 8225 #ifdef AHD_DEBUG 8226 if ((ahd_debug & AHD_SHOW_SENSE) != 0) { 8227 ahd_print_path(ahd, scb); 8228 printf("SCB 0x%x Received PKT Status of 0x%x\n", 8229 SCB_GET_TAG(scb), siu->status); 8230 printf("\tflags = 0x%x, sense len = 0x%x, " 8231 "pktfail = 0x%x\n", 8232 siu->flags, scsi_4btoul(siu->sense_length), 8233 scsi_4btoul(siu->pkt_failures_length)); 8234 } 8235 #endif 8236 if ((siu->flags & SIU_RSPVALID) != 0) { 8237 ahd_print_path(ahd, scb); 8238 if (scsi_4btoul(siu->pkt_failures_length) < 4) { 8239 printf("Unable to parse pkt_failures\n"); 8240 } else { 8241 8242 switch (SIU_PKTFAIL_CODE(siu)) { 8243 case SIU_PFC_NONE: 8244 printf("No packet failure found\n"); 8245 AHD_UNCORRECTABLE_ERROR(ahd); 8246 break; 8247 case SIU_PFC_CIU_FIELDS_INVALID: 8248 printf("Invalid Command IU Field\n"); 8249 AHD_UNCORRECTABLE_ERROR(ahd); 8250 break; 8251 case SIU_PFC_TMF_NOT_SUPPORTED: 8252 printf("TMF not supportd\n"); 8253 AHD_UNCORRECTABLE_ERROR(ahd); 8254 break; 8255 case SIU_PFC_TMF_FAILED: 8256 printf("TMF failed\n"); 8257 AHD_UNCORRECTABLE_ERROR(ahd); 8258 break; 8259 case SIU_PFC_INVALID_TYPE_CODE: 8260 printf("Invalid L_Q Type code\n"); 8261 AHD_UNCORRECTABLE_ERROR(ahd); 8262 break; 8263 case SIU_PFC_ILLEGAL_REQUEST: 8264 AHD_UNCORRECTABLE_ERROR(ahd); 8265 printf("Illegal request\n"); 8266 default: 8267 break; 8268 } 8269 } 8270 if (siu->status == SCSI_STATUS_OK) 8271 aic_set_transaction_status(scb, 8272 CAM_REQ_CMP_ERR); 8273 } 8274 if ((siu->flags & SIU_SNSVALID) != 0) { 8275 scb->flags |= SCB_PKT_SENSE; 8276 #ifdef AHD_DEBUG 8277 if ((ahd_debug & AHD_SHOW_SENSE) != 0) 8278 printf("Sense data available\n"); 8279 #endif 8280 } 8281 ahd_done(ahd, scb); 8282 break; 8283 } 8284 case SCSI_STATUS_CMD_TERMINATED: 8285 case SCSI_STATUS_CHECK_COND: 8286 { 8287 struct ahd_devinfo devinfo; 8288 struct ahd_dma_seg *sg; 8289 struct scsi_sense *sc; 8290 struct ahd_initiator_tinfo *targ_info; 8291 struct ahd_tmode_tstate *tstate; 8292 struct ahd_transinfo *tinfo; 8293 #ifdef AHD_DEBUG 8294 if (ahd_debug & AHD_SHOW_SENSE) { 8295 ahd_print_path(ahd, scb); 8296 printf("SCB %d: requests Check Status\n", 8297 SCB_GET_TAG(scb)); 8298 } 8299 #endif 8300 8301 if (aic_perform_autosense(scb) == 0) 8302 break; 8303 8304 ahd_compile_devinfo(&devinfo, SCB_GET_OUR_ID(scb), 8305 SCB_GET_TARGET(ahd, scb), 8306 SCB_GET_LUN(scb), 8307 SCB_GET_CHANNEL(ahd, scb), 8308 ROLE_INITIATOR); 8309 targ_info = ahd_fetch_transinfo(ahd, 8310 devinfo.channel, 8311 devinfo.our_scsiid, 8312 devinfo.target, 8313 &tstate); 8314 tinfo = &targ_info->curr; 8315 sg = scb->sg_list; 8316 sc = (struct scsi_sense *)hscb->shared_data.idata.cdb; 8317 /* 8318 * Save off the residual if there is one. 8319 */ 8320 ahd_update_residual(ahd, scb); 8321 #ifdef AHD_DEBUG 8322 if (ahd_debug & AHD_SHOW_SENSE) { 8323 ahd_print_path(ahd, scb); 8324 printf("Sending Sense\n"); 8325 } 8326 #endif 8327 scb->sg_count = 0; 8328 sg = ahd_sg_setup(ahd, scb, sg, ahd_get_sense_bufaddr(ahd, scb), 8329 aic_get_sense_bufsize(ahd, scb), 8330 /*last*/TRUE); 8331 sc->opcode = REQUEST_SENSE; 8332 sc->byte2 = 0; 8333 if (tinfo->protocol_version <= SCSI_REV_2 8334 && SCB_GET_LUN(scb) < 8) 8335 sc->byte2 = SCB_GET_LUN(scb) << 5; 8336 sc->unused[0] = 0; 8337 sc->unused[1] = 0; 8338 sc->length = aic_get_sense_bufsize(ahd, scb); 8339 sc->control = 0; 8340 8341 /* 8342 * We can't allow the target to disconnect. 8343 * This will be an untagged transaction and 8344 * having the target disconnect will make this 8345 * transaction indestinguishable from outstanding 8346 * tagged transactions. 8347 */ 8348 hscb->control = 0; 8349 8350 /* 8351 * This request sense could be because the 8352 * the device lost power or in some other 8353 * way has lost our transfer negotiations. 8354 * Renegotiate if appropriate. Unit attention 8355 * errors will be reported before any data 8356 * phases occur. 8357 */ 8358 if (aic_get_residual(scb) == aic_get_transfer_length(scb)) { 8359 ahd_update_neg_request(ahd, &devinfo, 8360 tstate, targ_info, 8361 AHD_NEG_IF_NON_ASYNC); 8362 } 8363 if (tstate->auto_negotiate & devinfo.target_mask) { 8364 hscb->control |= MK_MESSAGE; 8365 scb->flags &= 8366 ~(SCB_NEGOTIATE|SCB_ABORT|SCB_DEVICE_RESET); 8367 scb->flags |= SCB_AUTO_NEGOTIATE; 8368 } 8369 hscb->cdb_len = sizeof(*sc); 8370 ahd_setup_data_scb(ahd, scb); 8371 scb->flags |= SCB_SENSE; 8372 ahd_queue_scb(ahd, scb); 8373 /* 8374 * Ensure we have enough time to actually 8375 * retrieve the sense, but only schedule 8376 * the timer if we are not in recovery or 8377 * this is a recovery SCB that is allowed 8378 * to have an active timer. 8379 */ 8380 if (ahd->scb_data.recovery_scbs == 0 8381 || (scb->flags & SCB_RECOVERY_SCB) != 0) 8382 aic_scb_timer_reset(scb, 5 * 1000); 8383 break; 8384 } 8385 case SCSI_STATUS_OK: 8386 printf("%s: Interrupted for staus of 0???\n", 8387 ahd_name(ahd)); 8388 /* FALLTHROUGH */ 8389 default: 8390 ahd_done(ahd, scb); 8391 break; 8392 } 8393 } 8394 8395 /* 8396 * Calculate the residual for a just completed SCB. 8397 */ 8398 void 8399 ahd_calc_residual(struct ahd_softc *ahd, struct scb *scb) 8400 { 8401 struct hardware_scb *hscb; 8402 struct initiator_status *spkt; 8403 uint32_t sgptr; 8404 uint32_t resid_sgptr; 8405 uint32_t resid; 8406 8407 /* 8408 * 5 cases. 8409 * 1) No residual. 8410 * SG_STATUS_VALID clear in sgptr. 8411 * 2) Transferless command 8412 * 3) Never performed any transfers. 8413 * sgptr has SG_FULL_RESID set. 8414 * 4) No residual but target did not 8415 * save data pointers after the 8416 * last transfer, so sgptr was 8417 * never updated. 8418 * 5) We have a partial residual. 8419 * Use residual_sgptr to determine 8420 * where we are. 8421 */ 8422 8423 hscb = scb->hscb; 8424 sgptr = aic_le32toh(hscb->sgptr); 8425 if ((sgptr & SG_STATUS_VALID) == 0) 8426 /* Case 1 */ 8427 return; 8428 sgptr &= ~SG_STATUS_VALID; 8429 8430 if ((sgptr & SG_LIST_NULL) != 0) 8431 /* Case 2 */ 8432 return; 8433 8434 /* 8435 * Residual fields are the same in both 8436 * target and initiator status packets, 8437 * so we can always use the initiator fields 8438 * regardless of the role for this SCB. 8439 */ 8440 spkt = &hscb->shared_data.istatus; 8441 resid_sgptr = aic_le32toh(spkt->residual_sgptr); 8442 if ((sgptr & SG_FULL_RESID) != 0) { 8443 /* Case 3 */ 8444 resid = aic_get_transfer_length(scb); 8445 } else if ((resid_sgptr & SG_LIST_NULL) != 0) { 8446 /* Case 4 */ 8447 return; 8448 } else if ((resid_sgptr & SG_OVERRUN_RESID) != 0) { 8449 ahd_print_path(ahd, scb); 8450 printf("data overrun detected Tag == 0x%x.\n", 8451 SCB_GET_TAG(scb)); 8452 ahd_freeze_devq(ahd, scb); 8453 aic_set_transaction_status(scb, CAM_DATA_RUN_ERR); 8454 aic_freeze_scb(scb); 8455 return; 8456 } else if ((resid_sgptr & ~SG_PTR_MASK) != 0) { 8457 panic("Bogus resid sgptr value 0x%x\n", resid_sgptr); 8458 /* NOTREACHED */ 8459 } else { 8460 struct ahd_dma_seg *sg; 8461 8462 /* 8463 * Remainder of the SG where the transfer 8464 * stopped. 8465 */ 8466 resid = aic_le32toh(spkt->residual_datacnt) & AHD_SG_LEN_MASK; 8467 sg = ahd_sg_bus_to_virt(ahd, scb, resid_sgptr & SG_PTR_MASK); 8468 8469 /* The residual sg_ptr always points to the next sg */ 8470 sg--; 8471 8472 /* 8473 * Add up the contents of all residual 8474 * SG segments that are after the SG where 8475 * the transfer stopped. 8476 */ 8477 while ((aic_le32toh(sg->len) & AHD_DMA_LAST_SEG) == 0) { 8478 sg++; 8479 resid += aic_le32toh(sg->len) & AHD_SG_LEN_MASK; 8480 } 8481 } 8482 if ((scb->flags & SCB_SENSE) == 0) 8483 aic_set_residual(scb, resid); 8484 else 8485 aic_set_sense_residual(scb, resid); 8486 8487 #ifdef AHD_DEBUG 8488 if ((ahd_debug & AHD_SHOW_MISC) != 0) { 8489 ahd_print_path(ahd, scb); 8490 printf("Handled %sResidual of %d bytes\n", 8491 (scb->flags & SCB_SENSE) ? "Sense " : "", resid); 8492 } 8493 #endif 8494 } 8495 8496 /******************************* Target Mode **********************************/ 8497 #ifdef AHD_TARGET_MODE 8498 /* 8499 * Add a target mode event to this lun's queue 8500 */ 8501 static void 8502 ahd_queue_lstate_event(struct ahd_softc *ahd, struct ahd_tmode_lstate *lstate, 8503 u_int initiator_id, u_int event_type, u_int event_arg) 8504 { 8505 struct ahd_tmode_event *event; 8506 int pending; 8507 8508 xpt_freeze_devq(lstate->path, /*count*/1); 8509 if (lstate->event_w_idx >= lstate->event_r_idx) 8510 pending = lstate->event_w_idx - lstate->event_r_idx; 8511 else 8512 pending = AHD_TMODE_EVENT_BUFFER_SIZE + 1 8513 - (lstate->event_r_idx - lstate->event_w_idx); 8514 8515 if (event_type == EVENT_TYPE_BUS_RESET 8516 || event_type == MSG_BUS_DEV_RESET) { 8517 /* 8518 * Any earlier events are irrelevant, so reset our buffer. 8519 * This has the effect of allowing us to deal with reset 8520 * floods (an external device holding down the reset line) 8521 * without losing the event that is really interesting. 8522 */ 8523 lstate->event_r_idx = 0; 8524 lstate->event_w_idx = 0; 8525 xpt_release_devq(lstate->path, pending, /*runqueue*/FALSE); 8526 } 8527 8528 if (pending == AHD_TMODE_EVENT_BUFFER_SIZE) { 8529 xpt_print_path(lstate->path); 8530 printf("immediate event %x:%x lost\n", 8531 lstate->event_buffer[lstate->event_r_idx].event_type, 8532 lstate->event_buffer[lstate->event_r_idx].event_arg); 8533 lstate->event_r_idx++; 8534 if (lstate->event_r_idx == AHD_TMODE_EVENT_BUFFER_SIZE) 8535 lstate->event_r_idx = 0; 8536 xpt_release_devq(lstate->path, /*count*/1, /*runqueue*/FALSE); 8537 } 8538 8539 event = &lstate->event_buffer[lstate->event_w_idx]; 8540 event->initiator_id = initiator_id; 8541 event->event_type = event_type; 8542 event->event_arg = event_arg; 8543 lstate->event_w_idx++; 8544 if (lstate->event_w_idx == AHD_TMODE_EVENT_BUFFER_SIZE) 8545 lstate->event_w_idx = 0; 8546 } 8547 8548 /* 8549 * Send any target mode events queued up waiting 8550 * for immediate notify resources. 8551 */ 8552 void 8553 ahd_send_lstate_events(struct ahd_softc *ahd, struct ahd_tmode_lstate *lstate) 8554 { 8555 struct ccb_hdr *ccbh; 8556 struct ccb_immediate_notify *inot; 8557 8558 while (lstate->event_r_idx != lstate->event_w_idx 8559 && (ccbh = SLIST_FIRST(&lstate->immed_notifies)) != NULL) { 8560 struct ahd_tmode_event *event; 8561 8562 event = &lstate->event_buffer[lstate->event_r_idx]; 8563 SLIST_REMOVE_HEAD(&lstate->immed_notifies, sim_links.sle); 8564 inot = (struct ccb_immediate_notify *)ccbh; 8565 switch (event->event_type) { 8566 case EVENT_TYPE_BUS_RESET: 8567 ccbh->status = CAM_SCSI_BUS_RESET|CAM_DEV_QFRZN; 8568 break; 8569 default: 8570 ccbh->status = CAM_MESSAGE_RECV|CAM_DEV_QFRZN; 8571 inot->arg = event->event_type; 8572 inot->seq_id = event->event_arg; 8573 break; 8574 } 8575 inot->initiator_id = event->initiator_id; 8576 xpt_done((union ccb *)inot); 8577 lstate->event_r_idx++; 8578 if (lstate->event_r_idx == AHD_TMODE_EVENT_BUFFER_SIZE) 8579 lstate->event_r_idx = 0; 8580 } 8581 } 8582 #endif 8583 8584 /******************** Sequencer Program Patching/Download *********************/ 8585 8586 #ifdef AHD_DUMP_SEQ 8587 void 8588 ahd_dumpseq(struct ahd_softc* ahd) 8589 { 8590 int i; 8591 int max_prog; 8592 8593 max_prog = 2048; 8594 8595 ahd_outb(ahd, SEQCTL0, PERRORDIS|FAILDIS|FASTMODE|LOADRAM); 8596 ahd_outw(ahd, PRGMCNT, 0); 8597 for (i = 0; i < max_prog; i++) { 8598 uint8_t ins_bytes[4]; 8599 8600 ahd_insb(ahd, SEQRAM, ins_bytes, 4); 8601 printf("0x%08x\n", ins_bytes[0] << 24 8602 | ins_bytes[1] << 16 8603 | ins_bytes[2] << 8 8604 | ins_bytes[3]); 8605 } 8606 } 8607 #endif 8608 8609 static void 8610 ahd_loadseq(struct ahd_softc *ahd) 8611 { 8612 struct cs cs_table[num_critical_sections]; 8613 u_int begin_set[num_critical_sections]; 8614 u_int end_set[num_critical_sections]; 8615 struct patch *cur_patch; 8616 u_int cs_count; 8617 u_int cur_cs; 8618 u_int i; 8619 int downloaded; 8620 u_int skip_addr; 8621 u_int sg_prefetch_cnt; 8622 u_int sg_prefetch_cnt_limit; 8623 u_int sg_prefetch_align; 8624 u_int sg_size; 8625 u_int cacheline_mask; 8626 uint8_t download_consts[DOWNLOAD_CONST_COUNT]; 8627 8628 if (bootverbose) 8629 printf("%s: Downloading Sequencer Program...", 8630 ahd_name(ahd)); 8631 8632 #if DOWNLOAD_CONST_COUNT != 8 8633 #error "Download Const Mismatch" 8634 #endif 8635 /* 8636 * Start out with 0 critical sections 8637 * that apply to this firmware load. 8638 */ 8639 cs_count = 0; 8640 cur_cs = 0; 8641 memset(begin_set, 0, sizeof(begin_set)); 8642 memset(end_set, 0, sizeof(end_set)); 8643 8644 /* 8645 * Setup downloadable constant table. 8646 * 8647 * The computation for the S/G prefetch variables is 8648 * a bit complicated. We would like to always fetch 8649 * in terms of cachelined sized increments. However, 8650 * if the cacheline is not an even multiple of the 8651 * SG element size or is larger than our SG RAM, using 8652 * just the cache size might leave us with only a portion 8653 * of an SG element at the tail of a prefetch. If the 8654 * cacheline is larger than our S/G prefetch buffer less 8655 * the size of an SG element, we may round down to a cacheline 8656 * that doesn't contain any or all of the S/G of interest 8657 * within the bounds of our S/G ram. Provide variables to 8658 * the sequencer that will allow it to handle these edge 8659 * cases. 8660 */ 8661 /* Start by aligning to the nearest cacheline. */ 8662 sg_prefetch_align = ahd->pci_cachesize; 8663 if (sg_prefetch_align == 0) 8664 sg_prefetch_align = 8; 8665 /* Round down to the nearest power of 2. */ 8666 while (powerof2(sg_prefetch_align) == 0) 8667 sg_prefetch_align--; 8668 8669 cacheline_mask = sg_prefetch_align - 1; 8670 8671 /* 8672 * If the cacheline boundary is greater than half our prefetch RAM 8673 * we risk not being able to fetch even a single complete S/G 8674 * segment if we align to that boundary. 8675 */ 8676 if (sg_prefetch_align > CCSGADDR_MAX/2) 8677 sg_prefetch_align = CCSGADDR_MAX/2; 8678 /* Start by fetching a single cacheline. */ 8679 sg_prefetch_cnt = sg_prefetch_align; 8680 /* 8681 * Increment the prefetch count by cachelines until 8682 * at least one S/G element will fit. 8683 */ 8684 sg_size = sizeof(struct ahd_dma_seg); 8685 if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0) 8686 sg_size = sizeof(struct ahd_dma64_seg); 8687 while (sg_prefetch_cnt < sg_size) 8688 sg_prefetch_cnt += sg_prefetch_align; 8689 /* 8690 * If the cacheline is not an even multiple of 8691 * the S/G size, we may only get a partial S/G when 8692 * we align. Add a cacheline if this is the case. 8693 */ 8694 if ((sg_prefetch_align % sg_size) != 0 8695 && (sg_prefetch_cnt < CCSGADDR_MAX)) 8696 sg_prefetch_cnt += sg_prefetch_align; 8697 /* 8698 * Lastly, compute a value that the sequencer can use 8699 * to determine if the remainder of the CCSGRAM buffer 8700 * has a full S/G element in it. 8701 */ 8702 sg_prefetch_cnt_limit = -(sg_prefetch_cnt - sg_size + 1); 8703 download_consts[SG_PREFETCH_CNT] = sg_prefetch_cnt; 8704 download_consts[SG_PREFETCH_CNT_LIMIT] = sg_prefetch_cnt_limit; 8705 download_consts[SG_PREFETCH_ALIGN_MASK] = ~(sg_prefetch_align - 1); 8706 download_consts[SG_PREFETCH_ADDR_MASK] = (sg_prefetch_align - 1); 8707 download_consts[SG_SIZEOF] = sg_size; 8708 download_consts[PKT_OVERRUN_BUFOFFSET] = 8709 (ahd->overrun_buf - (uint8_t *)ahd->qoutfifo) / 256; 8710 download_consts[SCB_TRANSFER_SIZE] = SCB_TRANSFER_SIZE_1BYTE_LUN; 8711 download_consts[CACHELINE_MASK] = cacheline_mask; 8712 cur_patch = patches; 8713 downloaded = 0; 8714 skip_addr = 0; 8715 ahd_outb(ahd, SEQCTL0, PERRORDIS|FAILDIS|FASTMODE|LOADRAM); 8716 ahd_outw(ahd, PRGMCNT, 0); 8717 8718 for (i = 0; i < sizeof(seqprog)/4; i++) { 8719 if (ahd_check_patch(ahd, &cur_patch, i, &skip_addr) == 0) { 8720 /* 8721 * Don't download this instruction as it 8722 * is in a patch that was removed. 8723 */ 8724 continue; 8725 } 8726 /* 8727 * Move through the CS table until we find a CS 8728 * that might apply to this instruction. 8729 */ 8730 for (; cur_cs < num_critical_sections; cur_cs++) { 8731 if (critical_sections[cur_cs].end <= i) { 8732 if (begin_set[cs_count] == TRUE 8733 && end_set[cs_count] == FALSE) { 8734 cs_table[cs_count].end = downloaded; 8735 end_set[cs_count] = TRUE; 8736 cs_count++; 8737 } 8738 continue; 8739 } 8740 if (critical_sections[cur_cs].begin <= i 8741 && begin_set[cs_count] == FALSE) { 8742 cs_table[cs_count].begin = downloaded; 8743 begin_set[cs_count] = TRUE; 8744 } 8745 break; 8746 } 8747 ahd_download_instr(ahd, i, download_consts); 8748 downloaded++; 8749 } 8750 8751 ahd->num_critical_sections = cs_count; 8752 if (cs_count != 0) { 8753 8754 cs_count *= sizeof(struct cs); 8755 ahd->critical_sections = malloc(cs_count, M_DEVBUF, M_NOWAIT); 8756 if (ahd->critical_sections == NULL) 8757 panic("ahd_loadseq: Could not malloc"); 8758 memcpy(ahd->critical_sections, cs_table, cs_count); 8759 } 8760 ahd_outb(ahd, SEQCTL0, PERRORDIS|FAILDIS|FASTMODE); 8761 8762 if (bootverbose) { 8763 printf(" %d instructions downloaded\n", downloaded); 8764 printf("%s: Features 0x%x, Bugs 0x%x, Flags 0x%x\n", 8765 ahd_name(ahd), ahd->features, ahd->bugs, ahd->flags); 8766 } 8767 } 8768 8769 static int 8770 ahd_check_patch(struct ahd_softc *ahd, struct patch **start_patch, 8771 u_int start_instr, u_int *skip_addr) 8772 { 8773 struct patch *cur_patch; 8774 struct patch *last_patch; 8775 u_int num_patches; 8776 8777 num_patches = sizeof(patches)/sizeof(struct patch); 8778 last_patch = &patches[num_patches]; 8779 cur_patch = *start_patch; 8780 8781 while (cur_patch < last_patch && start_instr == cur_patch->begin) { 8782 8783 if (cur_patch->patch_func(ahd) == 0) { 8784 8785 /* Start rejecting code */ 8786 *skip_addr = start_instr + cur_patch->skip_instr; 8787 cur_patch += cur_patch->skip_patch; 8788 } else { 8789 /* Accepted this patch. Advance to the next 8790 * one and wait for our intruction pointer to 8791 * hit this point. 8792 */ 8793 cur_patch++; 8794 } 8795 } 8796 8797 *start_patch = cur_patch; 8798 if (start_instr < *skip_addr) 8799 /* Still skipping */ 8800 return (0); 8801 8802 return (1); 8803 } 8804 8805 static u_int 8806 ahd_resolve_seqaddr(struct ahd_softc *ahd, u_int address) 8807 { 8808 struct patch *cur_patch; 8809 int address_offset; 8810 u_int skip_addr; 8811 u_int i; 8812 8813 address_offset = 0; 8814 cur_patch = patches; 8815 skip_addr = 0; 8816 8817 for (i = 0; i < address;) { 8818 8819 ahd_check_patch(ahd, &cur_patch, i, &skip_addr); 8820 8821 if (skip_addr > i) { 8822 int end_addr; 8823 8824 end_addr = MIN(address, skip_addr); 8825 address_offset += end_addr - i; 8826 i = skip_addr; 8827 } else { 8828 i++; 8829 } 8830 } 8831 return (address - address_offset); 8832 } 8833 8834 static void 8835 ahd_download_instr(struct ahd_softc *ahd, u_int instrptr, uint8_t *dconsts) 8836 { 8837 union ins_formats instr; 8838 struct ins_format1 *fmt1_ins; 8839 struct ins_format3 *fmt3_ins; 8840 u_int opcode; 8841 8842 /* 8843 * The firmware is always compiled into a little endian format. 8844 */ 8845 instr.integer = aic_le32toh(*(uint32_t*)&seqprog[instrptr * 4]); 8846 8847 fmt1_ins = &instr.format1; 8848 fmt3_ins = NULL; 8849 8850 /* Pull the opcode */ 8851 opcode = instr.format1.opcode; 8852 switch (opcode) { 8853 case AIC_OP_JMP: 8854 case AIC_OP_JC: 8855 case AIC_OP_JNC: 8856 case AIC_OP_CALL: 8857 case AIC_OP_JNE: 8858 case AIC_OP_JNZ: 8859 case AIC_OP_JE: 8860 case AIC_OP_JZ: 8861 { 8862 fmt3_ins = &instr.format3; 8863 fmt3_ins->address = ahd_resolve_seqaddr(ahd, fmt3_ins->address); 8864 /* FALLTHROUGH */ 8865 } 8866 case AIC_OP_OR: 8867 case AIC_OP_AND: 8868 case AIC_OP_XOR: 8869 case AIC_OP_ADD: 8870 case AIC_OP_ADC: 8871 case AIC_OP_BMOV: 8872 if (fmt1_ins->parity != 0) { 8873 fmt1_ins->immediate = dconsts[fmt1_ins->immediate]; 8874 } 8875 fmt1_ins->parity = 0; 8876 /* FALLTHROUGH */ 8877 case AIC_OP_ROL: 8878 { 8879 int i, count; 8880 8881 /* Calculate odd parity for the instruction */ 8882 for (i = 0, count = 0; i < 31; i++) { 8883 uint32_t mask; 8884 8885 mask = 0x01 << i; 8886 if ((instr.integer & mask) != 0) 8887 count++; 8888 } 8889 if ((count & 0x01) == 0) 8890 instr.format1.parity = 1; 8891 8892 /* The sequencer is a little endian cpu */ 8893 instr.integer = aic_htole32(instr.integer); 8894 ahd_outsb(ahd, SEQRAM, instr.bytes, 4); 8895 break; 8896 } 8897 default: 8898 panic("Unknown opcode encountered in seq program"); 8899 break; 8900 } 8901 } 8902 8903 static int 8904 ahd_probe_stack_size(struct ahd_softc *ahd) 8905 { 8906 int last_probe; 8907 8908 last_probe = 0; 8909 while (1) { 8910 int i; 8911 8912 /* 8913 * We avoid using 0 as a pattern to avoid 8914 * confusion if the stack implementation 8915 * "back-fills" with zeros when "poping' 8916 * entries. 8917 */ 8918 for (i = 1; i <= last_probe+1; i++) { 8919 ahd_outb(ahd, STACK, i & 0xFF); 8920 ahd_outb(ahd, STACK, (i >> 8) & 0xFF); 8921 } 8922 8923 /* Verify */ 8924 for (i = last_probe+1; i > 0; i--) { 8925 u_int stack_entry; 8926 8927 stack_entry = ahd_inb(ahd, STACK) 8928 |(ahd_inb(ahd, STACK) << 8); 8929 if (stack_entry != i) 8930 goto sized; 8931 } 8932 last_probe++; 8933 } 8934 sized: 8935 return (last_probe); 8936 } 8937 8938 void 8939 ahd_dump_all_cards_state(void) 8940 { 8941 struct ahd_softc *list_ahd; 8942 8943 TAILQ_FOREACH(list_ahd, &ahd_tailq, links) { 8944 ahd_dump_card_state(list_ahd); 8945 } 8946 } 8947 8948 int 8949 ahd_print_register(ahd_reg_parse_entry_t *table, u_int num_entries, 8950 const char *name, u_int address, u_int value, 8951 u_int *cur_column, u_int wrap_point) 8952 { 8953 int printed; 8954 u_int printed_mask; 8955 u_int dummy_column; 8956 8957 if (cur_column == NULL) { 8958 dummy_column = 0; 8959 cur_column = &dummy_column; 8960 } 8961 8962 if (cur_column != NULL && *cur_column >= wrap_point) { 8963 printf("\n"); 8964 *cur_column = 0; 8965 } 8966 printed = printf("%s[0x%x]", name, value); 8967 if (table == NULL) { 8968 printed += printf(" "); 8969 *cur_column += printed; 8970 return (printed); 8971 } 8972 printed_mask = 0; 8973 while (printed_mask != 0xFF) { 8974 int entry; 8975 8976 for (entry = 0; entry < num_entries; entry++) { 8977 if (((value & table[entry].mask) 8978 != table[entry].value) 8979 || ((printed_mask & table[entry].mask) 8980 == table[entry].mask)) 8981 continue; 8982 8983 printed += printf("%s%s", 8984 printed_mask == 0 ? ":(" : "|", 8985 table[entry].name); 8986 printed_mask |= table[entry].mask; 8987 8988 break; 8989 } 8990 if (entry >= num_entries) 8991 break; 8992 } 8993 if (printed_mask != 0) 8994 printed += printf(") "); 8995 else 8996 printed += printf(" "); 8997 *cur_column += printed; 8998 return (printed); 8999 } 9000 9001 void 9002 ahd_dump_card_state(struct ahd_softc *ahd) 9003 { 9004 struct scb *scb; 9005 ahd_mode_state saved_modes; 9006 u_int dffstat; 9007 int paused; 9008 u_int scb_index; 9009 u_int saved_scb_index; 9010 u_int cur_col; 9011 int i; 9012 9013 if (ahd_is_paused(ahd)) { 9014 paused = 1; 9015 } else { 9016 paused = 0; 9017 ahd_pause(ahd); 9018 } 9019 saved_modes = ahd_save_modes(ahd); 9020 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI); 9021 printf(">>>>>>>>>>>>>>>>>> Dump Card State Begins <<<<<<<<<<<<<<<<<\n" 9022 "%s: Dumping Card State at program address 0x%x Mode 0x%x\n", 9023 ahd_name(ahd), 9024 ahd_inw(ahd, CURADDR), 9025 ahd_build_mode_state(ahd, ahd->saved_src_mode, 9026 ahd->saved_dst_mode)); 9027 if (paused) 9028 printf("Card was paused\n"); 9029 9030 if (ahd_check_cmdcmpltqueues(ahd)) 9031 printf("Completions are pending\n"); 9032 9033 /* 9034 * Mode independent registers. 9035 */ 9036 cur_col = 0; 9037 ahd_intstat_print(ahd_inb(ahd, INTSTAT), &cur_col, 50); 9038 ahd_seloid_print(ahd_inb(ahd, SELOID), &cur_col, 50); 9039 ahd_selid_print(ahd_inb(ahd, SELID), &cur_col, 50); 9040 ahd_hs_mailbox_print(ahd_inb(ahd, LOCAL_HS_MAILBOX), &cur_col, 50); 9041 ahd_intctl_print(ahd_inb(ahd, INTCTL), &cur_col, 50); 9042 ahd_seqintstat_print(ahd_inb(ahd, SEQINTSTAT), &cur_col, 50); 9043 ahd_saved_mode_print(ahd_inb(ahd, SAVED_MODE), &cur_col, 50); 9044 ahd_dffstat_print(ahd_inb(ahd, DFFSTAT), &cur_col, 50); 9045 ahd_scsisigi_print(ahd_inb(ahd, SCSISIGI), &cur_col, 50); 9046 ahd_scsiphase_print(ahd_inb(ahd, SCSIPHASE), &cur_col, 50); 9047 ahd_scsibus_print(ahd_inb(ahd, SCSIBUS), &cur_col, 50); 9048 ahd_lastphase_print(ahd_inb(ahd, LASTPHASE), &cur_col, 50); 9049 ahd_scsiseq0_print(ahd_inb(ahd, SCSISEQ0), &cur_col, 50); 9050 ahd_scsiseq1_print(ahd_inb(ahd, SCSISEQ1), &cur_col, 50); 9051 ahd_seqctl0_print(ahd_inb(ahd, SEQCTL0), &cur_col, 50); 9052 ahd_seqintctl_print(ahd_inb(ahd, SEQINTCTL), &cur_col, 50); 9053 ahd_seq_flags_print(ahd_inb(ahd, SEQ_FLAGS), &cur_col, 50); 9054 ahd_seq_flags2_print(ahd_inb(ahd, SEQ_FLAGS2), &cur_col, 50); 9055 ahd_qfreeze_count_print(ahd_inw(ahd, QFREEZE_COUNT), &cur_col, 50); 9056 ahd_kernel_qfreeze_count_print(ahd_inw(ahd, KERNEL_QFREEZE_COUNT), 9057 &cur_col, 50); 9058 ahd_mk_message_scb_print(ahd_inw(ahd, MK_MESSAGE_SCB), &cur_col, 50); 9059 ahd_mk_message_scsiid_print(ahd_inb(ahd, MK_MESSAGE_SCSIID), 9060 &cur_col, 50); 9061 ahd_sstat0_print(ahd_inb(ahd, SSTAT0), &cur_col, 50); 9062 ahd_sstat1_print(ahd_inb(ahd, SSTAT1), &cur_col, 50); 9063 ahd_sstat2_print(ahd_inb(ahd, SSTAT2), &cur_col, 50); 9064 ahd_sstat3_print(ahd_inb(ahd, SSTAT3), &cur_col, 50); 9065 ahd_perrdiag_print(ahd_inb(ahd, PERRDIAG), &cur_col, 50); 9066 ahd_simode1_print(ahd_inb(ahd, SIMODE1), &cur_col, 50); 9067 ahd_lqistat0_print(ahd_inb(ahd, LQISTAT0), &cur_col, 50); 9068 ahd_lqistat1_print(ahd_inb(ahd, LQISTAT1), &cur_col, 50); 9069 ahd_lqistat2_print(ahd_inb(ahd, LQISTAT2), &cur_col, 50); 9070 ahd_lqostat0_print(ahd_inb(ahd, LQOSTAT0), &cur_col, 50); 9071 ahd_lqostat1_print(ahd_inb(ahd, LQOSTAT1), &cur_col, 50); 9072 ahd_lqostat2_print(ahd_inb(ahd, LQOSTAT2), &cur_col, 50); 9073 printf("\n"); 9074 printf("\nSCB Count = %d CMDS_PENDING = %d LASTSCB 0x%x " 9075 "CURRSCB 0x%x NEXTSCB 0x%x\n", 9076 ahd->scb_data.numscbs, ahd_inw(ahd, CMDS_PENDING), 9077 ahd_inw(ahd, LASTSCB), ahd_inw(ahd, CURRSCB), 9078 ahd_inw(ahd, NEXTSCB)); 9079 cur_col = 0; 9080 /* QINFIFO */ 9081 ahd_search_qinfifo(ahd, CAM_TARGET_WILDCARD, ALL_CHANNELS, 9082 CAM_LUN_WILDCARD, SCB_LIST_NULL, 9083 ROLE_UNKNOWN, /*status*/0, SEARCH_PRINT); 9084 saved_scb_index = ahd_get_scbptr(ahd); 9085 printf("Pending list:"); 9086 i = 0; 9087 LIST_FOREACH(scb, &ahd->pending_scbs, pending_links) { 9088 if (i++ > AHD_SCB_MAX) 9089 break; 9090 cur_col = printf("\n%3d FIFO_USE[0x%x] ", SCB_GET_TAG(scb), 9091 ahd_inb_scbram(ahd, SCB_FIFO_USE_COUNT)); 9092 ahd_set_scbptr(ahd, SCB_GET_TAG(scb)); 9093 ahd_scb_control_print(ahd_inb_scbram(ahd, SCB_CONTROL), 9094 &cur_col, 60); 9095 ahd_scb_scsiid_print(ahd_inb_scbram(ahd, SCB_SCSIID), 9096 &cur_col, 60); 9097 } 9098 printf("\nTotal %d\n", i); 9099 9100 printf("Kernel Free SCB lists: "); 9101 i = 0; 9102 TAILQ_FOREACH(scb, &ahd->scb_data.free_scbs, links.tqe) { 9103 struct scb *list_scb; 9104 9105 printf("\n COLIDX[%d]: ", AHD_GET_SCB_COL_IDX(ahd, scb)); 9106 list_scb = scb; 9107 do { 9108 printf("%d ", SCB_GET_TAG(list_scb)); 9109 list_scb = LIST_NEXT(list_scb, collision_links); 9110 } while (list_scb && i++ < AHD_SCB_MAX); 9111 } 9112 9113 printf("\n Any Device: "); 9114 LIST_FOREACH(scb, &ahd->scb_data.any_dev_free_scb_list, links.le) { 9115 if (i++ > AHD_SCB_MAX) 9116 break; 9117 printf("%d ", SCB_GET_TAG(scb)); 9118 } 9119 printf("\n"); 9120 9121 printf("Sequencer Complete DMA-inprog list: "); 9122 scb_index = ahd_inw(ahd, COMPLETE_SCB_DMAINPROG_HEAD); 9123 i = 0; 9124 while (!SCBID_IS_NULL(scb_index) && i++ < AHD_SCB_MAX) { 9125 ahd_set_scbptr(ahd, scb_index); 9126 printf("%d ", scb_index); 9127 scb_index = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE); 9128 } 9129 printf("\n"); 9130 9131 printf("Sequencer Complete list: "); 9132 scb_index = ahd_inw(ahd, COMPLETE_SCB_HEAD); 9133 i = 0; 9134 while (!SCBID_IS_NULL(scb_index) && i++ < AHD_SCB_MAX) { 9135 ahd_set_scbptr(ahd, scb_index); 9136 printf("%d ", scb_index); 9137 scb_index = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE); 9138 } 9139 printf("\n"); 9140 9141 9142 printf("Sequencer DMA-Up and Complete list: "); 9143 scb_index = ahd_inw(ahd, COMPLETE_DMA_SCB_HEAD); 9144 i = 0; 9145 while (!SCBID_IS_NULL(scb_index) && i++ < AHD_SCB_MAX) { 9146 ahd_set_scbptr(ahd, scb_index); 9147 printf("%d ", scb_index); 9148 scb_index = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE); 9149 } 9150 printf("\n"); 9151 printf("Sequencer On QFreeze and Complete list: "); 9152 scb_index = ahd_inw(ahd, COMPLETE_ON_QFREEZE_HEAD); 9153 i = 0; 9154 while (!SCBID_IS_NULL(scb_index) && i++ < AHD_SCB_MAX) { 9155 ahd_set_scbptr(ahd, scb_index); 9156 printf("%d ", scb_index); 9157 scb_index = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE); 9158 } 9159 printf("\n"); 9160 ahd_set_scbptr(ahd, saved_scb_index); 9161 dffstat = ahd_inb(ahd, DFFSTAT); 9162 for (i = 0; i < 2; i++) { 9163 #ifdef AHD_DEBUG 9164 struct scb *fifo_scb; 9165 #endif 9166 u_int fifo_scbptr; 9167 9168 ahd_set_modes(ahd, AHD_MODE_DFF0 + i, AHD_MODE_DFF0 + i); 9169 fifo_scbptr = ahd_get_scbptr(ahd); 9170 printf("\n\n%s: FIFO%d %s, LONGJMP == 0x%x, SCB 0x%x\n", 9171 ahd_name(ahd), i, 9172 (dffstat & (FIFO0FREE << i)) ? "Free" : "Active", 9173 ahd_inw(ahd, LONGJMP_ADDR), fifo_scbptr); 9174 cur_col = 0; 9175 ahd_seqimode_print(ahd_inb(ahd, SEQIMODE), &cur_col, 50); 9176 ahd_seqintsrc_print(ahd_inb(ahd, SEQINTSRC), &cur_col, 50); 9177 ahd_dfcntrl_print(ahd_inb(ahd, DFCNTRL), &cur_col, 50); 9178 ahd_dfstatus_print(ahd_inb(ahd, DFSTATUS), &cur_col, 50); 9179 ahd_sg_cache_shadow_print(ahd_inb(ahd, SG_CACHE_SHADOW), 9180 &cur_col, 50); 9181 ahd_sg_state_print(ahd_inb(ahd, SG_STATE), &cur_col, 50); 9182 ahd_dffsxfrctl_print(ahd_inb(ahd, DFFSXFRCTL), &cur_col, 50); 9183 ahd_soffcnt_print(ahd_inb(ahd, SOFFCNT), &cur_col, 50); 9184 ahd_mdffstat_print(ahd_inb(ahd, MDFFSTAT), &cur_col, 50); 9185 if (cur_col > 50) { 9186 printf("\n"); 9187 cur_col = 0; 9188 } 9189 cur_col += printf("SHADDR = 0x%x%x, SHCNT = 0x%x ", 9190 ahd_inl(ahd, SHADDR+4), 9191 ahd_inl(ahd, SHADDR), 9192 (ahd_inb(ahd, SHCNT) 9193 | (ahd_inb(ahd, SHCNT + 1) << 8) 9194 | (ahd_inb(ahd, SHCNT + 2) << 16))); 9195 if (cur_col > 50) { 9196 printf("\n"); 9197 cur_col = 0; 9198 } 9199 cur_col += printf("HADDR = 0x%x%x, HCNT = 0x%x ", 9200 ahd_inl(ahd, HADDR+4), 9201 ahd_inl(ahd, HADDR), 9202 (ahd_inb(ahd, HCNT) 9203 | (ahd_inb(ahd, HCNT + 1) << 8) 9204 | (ahd_inb(ahd, HCNT + 2) << 16))); 9205 ahd_ccsgctl_print(ahd_inb(ahd, CCSGCTL), &cur_col, 50); 9206 #ifdef AHD_DEBUG 9207 if ((ahd_debug & AHD_SHOW_SG) != 0) { 9208 fifo_scb = ahd_lookup_scb(ahd, fifo_scbptr); 9209 if (fifo_scb != NULL) 9210 ahd_dump_sglist(fifo_scb); 9211 } 9212 #endif 9213 } 9214 printf("\nLQIN: "); 9215 for (i = 0; i < 20; i++) 9216 printf("0x%x ", ahd_inb(ahd, LQIN + i)); 9217 printf("\n"); 9218 ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG); 9219 printf("%s: LQISTATE = 0x%x, LQOSTATE = 0x%x, OPTIONMODE = 0x%x\n", 9220 ahd_name(ahd), ahd_inb(ahd, LQISTATE), ahd_inb(ahd, LQOSTATE), 9221 ahd_inb(ahd, OPTIONMODE)); 9222 printf("%s: OS_SPACE_CNT = 0x%x MAXCMDCNT = 0x%x\n", 9223 ahd_name(ahd), ahd_inb(ahd, OS_SPACE_CNT), 9224 ahd_inb(ahd, MAXCMDCNT)); 9225 printf("%s: SAVED_SCSIID = 0x%x SAVED_LUN = 0x%x\n", 9226 ahd_name(ahd), ahd_inb(ahd, SAVED_SCSIID), 9227 ahd_inb(ahd, SAVED_LUN)); 9228 ahd_simode0_print(ahd_inb(ahd, SIMODE0), &cur_col, 50); 9229 printf("\n"); 9230 ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN); 9231 cur_col = 0; 9232 ahd_ccscbctl_print(ahd_inb(ahd, CCSCBCTL), &cur_col, 50); 9233 printf("\n"); 9234 ahd_set_modes(ahd, ahd->saved_src_mode, ahd->saved_dst_mode); 9235 printf("%s: REG0 == 0x%x, SINDEX = 0x%x, DINDEX = 0x%x\n", 9236 ahd_name(ahd), ahd_inw(ahd, REG0), ahd_inw(ahd, SINDEX), 9237 ahd_inw(ahd, DINDEX)); 9238 printf("%s: SCBPTR == 0x%x, SCB_NEXT == 0x%x, SCB_NEXT2 == 0x%x\n", 9239 ahd_name(ahd), ahd_get_scbptr(ahd), 9240 ahd_inw_scbram(ahd, SCB_NEXT), 9241 ahd_inw_scbram(ahd, SCB_NEXT2)); 9242 printf("CDB %x %x %x %x %x %x\n", 9243 ahd_inb_scbram(ahd, SCB_CDB_STORE), 9244 ahd_inb_scbram(ahd, SCB_CDB_STORE+1), 9245 ahd_inb_scbram(ahd, SCB_CDB_STORE+2), 9246 ahd_inb_scbram(ahd, SCB_CDB_STORE+3), 9247 ahd_inb_scbram(ahd, SCB_CDB_STORE+4), 9248 ahd_inb_scbram(ahd, SCB_CDB_STORE+5)); 9249 printf("STACK:"); 9250 for (i = 0; i < ahd->stack_size; i++) { 9251 ahd->saved_stack[i] = 9252 ahd_inb(ahd, STACK)|(ahd_inb(ahd, STACK) << 8); 9253 printf(" 0x%x", ahd->saved_stack[i]); 9254 } 9255 for (i = ahd->stack_size-1; i >= 0; i--) { 9256 ahd_outb(ahd, STACK, ahd->saved_stack[i] & 0xFF); 9257 ahd_outb(ahd, STACK, (ahd->saved_stack[i] >> 8) & 0xFF); 9258 } 9259 printf("\n<<<<<<<<<<<<<<<<< Dump Card State Ends >>>>>>>>>>>>>>>>>>\n"); 9260 ahd_platform_dump_card_state(ahd); 9261 ahd_restore_modes(ahd, saved_modes); 9262 if (paused == 0) 9263 ahd_unpause(ahd); 9264 } 9265 9266 void 9267 ahd_dump_scbs(struct ahd_softc *ahd) 9268 { 9269 ahd_mode_state saved_modes; 9270 u_int saved_scb_index; 9271 int i; 9272 9273 saved_modes = ahd_save_modes(ahd); 9274 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI); 9275 saved_scb_index = ahd_get_scbptr(ahd); 9276 for (i = 0; i < AHD_SCB_MAX; i++) { 9277 ahd_set_scbptr(ahd, i); 9278 printf("%3d", i); 9279 printf("(CTRL 0x%x ID 0x%x N 0x%x N2 0x%x SG 0x%x, RSG 0x%x)\n", 9280 ahd_inb_scbram(ahd, SCB_CONTROL), 9281 ahd_inb_scbram(ahd, SCB_SCSIID), 9282 ahd_inw_scbram(ahd, SCB_NEXT), 9283 ahd_inw_scbram(ahd, SCB_NEXT2), 9284 ahd_inl_scbram(ahd, SCB_SGPTR), 9285 ahd_inl_scbram(ahd, SCB_RESIDUAL_SGPTR)); 9286 } 9287 printf("\n"); 9288 ahd_set_scbptr(ahd, saved_scb_index); 9289 ahd_restore_modes(ahd, saved_modes); 9290 } 9291 9292 9293 /*************************** Timeout Handling *********************************/ 9294 void 9295 ahd_timeout(struct scb *scb) 9296 { 9297 struct ahd_softc *ahd; 9298 9299 ahd = scb->ahd_softc; 9300 if ((scb->flags & SCB_ACTIVE) != 0) { 9301 if ((scb->flags & SCB_TIMEDOUT) == 0) { 9302 LIST_INSERT_HEAD(&ahd->timedout_scbs, scb, 9303 timedout_links); 9304 scb->flags |= SCB_TIMEDOUT; 9305 } 9306 ahd_wakeup_recovery_thread(ahd); 9307 } 9308 } 9309 9310 /* 9311 * ahd_recover_commands determines if any of the commands that have currently 9312 * timedout are the root cause for this timeout. Innocent commands are given 9313 * a new timeout while we wait for the command executing on the bus to timeout. 9314 * This routine is invoked from a thread context so we are allowed to sleep. 9315 * Our lock is not held on entry. 9316 */ 9317 void 9318 ahd_recover_commands(struct ahd_softc *ahd) 9319 { 9320 struct scb *scb; 9321 struct scb *active_scb; 9322 int found; 9323 int was_paused; 9324 u_int active_scbptr; 9325 u_int last_phase; 9326 9327 /* 9328 * Pause the controller and manually flush any 9329 * commands that have just completed but that our 9330 * interrupt handler has yet to see. 9331 */ 9332 was_paused = ahd_is_paused(ahd); 9333 9334 printf("%s: Recovery Initiated - Card was %spaused\n", ahd_name(ahd), 9335 was_paused ? "" : "not "); 9336 AHD_CORRECTABLE_ERROR(ahd); 9337 ahd_dump_card_state(ahd); 9338 9339 ahd_pause_and_flushwork(ahd); 9340 9341 if (LIST_EMPTY(&ahd->timedout_scbs) != 0) { 9342 /* 9343 * The timedout commands have already 9344 * completed. This typically means 9345 * that either the timeout value was on 9346 * the hairy edge of what the device 9347 * requires or - more likely - interrupts 9348 * are not happening. 9349 */ 9350 printf("%s: Timedout SCBs already complete. " 9351 "Interrupts may not be functioning.\n", ahd_name(ahd)); 9352 ahd_unpause(ahd); 9353 return; 9354 } 9355 9356 /* 9357 * Determine identity of SCB acting on the bus. 9358 * This test only catches non-packetized transactions. 9359 * Due to the fleeting nature of packetized operations, 9360 * we can't easily determine that a packetized operation 9361 * is on the bus. 9362 */ 9363 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI); 9364 last_phase = ahd_inb(ahd, LASTPHASE); 9365 active_scbptr = ahd_get_scbptr(ahd); 9366 active_scb = NULL; 9367 if (last_phase != P_BUSFREE 9368 || (ahd_inb(ahd, SEQ_FLAGS) & NOT_IDENTIFIED) == 0) 9369 active_scb = ahd_lookup_scb(ahd, active_scbptr); 9370 9371 while ((scb = LIST_FIRST(&ahd->timedout_scbs)) != NULL) { 9372 int target; 9373 int lun; 9374 char channel; 9375 9376 target = SCB_GET_TARGET(ahd, scb); 9377 channel = SCB_GET_CHANNEL(ahd, scb); 9378 lun = SCB_GET_LUN(scb); 9379 9380 ahd_print_path(ahd, scb); 9381 printf("SCB %d - timed out\n", SCB_GET_TAG(scb)); 9382 9383 if (scb->flags & (SCB_DEVICE_RESET|SCB_ABORT)) { 9384 /* 9385 * Been down this road before. 9386 * Do a full bus reset. 9387 */ 9388 aic_set_transaction_status(scb, CAM_CMD_TIMEOUT); 9389 bus_reset: 9390 found = ahd_reset_channel(ahd, channel, 9391 /*Initiate Reset*/TRUE); 9392 printf("%s: Issued Channel %c Bus Reset. " 9393 "%d SCBs aborted\n", ahd_name(ahd), channel, 9394 found); 9395 continue; 9396 } 9397 9398 /* 9399 * Remove the command from the timedout list in 9400 * preparation for requeing it. 9401 */ 9402 LIST_REMOVE(scb, timedout_links); 9403 scb->flags &= ~SCB_TIMEDOUT; 9404 9405 if (active_scb != NULL) { 9406 9407 if (active_scb != scb) { 9408 9409 /* 9410 * If the active SCB is not us, assume that 9411 * the active SCB has a longer timeout than 9412 * the timedout SCB, and wait for the active 9413 * SCB to timeout. As a safeguard, only 9414 * allow this deferral to continue if some 9415 * untimed-out command is outstanding. 9416 */ 9417 if (ahd_other_scb_timeout(ahd, scb, 9418 active_scb) == 0) 9419 goto bus_reset; 9420 continue; 9421 } 9422 9423 /* 9424 * We're active on the bus, so assert ATN 9425 * and hope that the target responds. 9426 */ 9427 ahd_set_recoveryscb(ahd, active_scb); 9428 active_scb->flags |= SCB_RECOVERY_SCB|SCB_DEVICE_RESET; 9429 ahd_outb(ahd, MSG_OUT, HOST_MSG); 9430 ahd_outb(ahd, SCSISIGO, last_phase|ATNO); 9431 ahd_print_path(ahd, active_scb); 9432 printf("BDR message in message buffer\n"); 9433 aic_scb_timer_reset(scb, 2 * 1000); 9434 break; 9435 } else if (last_phase != P_BUSFREE 9436 && ahd_inb(ahd, SCSIPHASE) == 0) { 9437 /* 9438 * SCB is not identified, there 9439 * is no pending REQ, and the sequencer 9440 * has not seen a busfree. Looks like 9441 * a stuck connection waiting to 9442 * go busfree. Reset the bus. 9443 */ 9444 printf("%s: Connection stuck awaiting busfree or " 9445 "Identify Msg.\n", ahd_name(ahd)); 9446 goto bus_reset; 9447 } else if (ahd_search_qinfifo(ahd, target, channel, lun, 9448 SCB_GET_TAG(scb), 9449 ROLE_INITIATOR, /*status*/0, 9450 SEARCH_COUNT) > 0) { 9451 9452 /* 9453 * We haven't even gone out on the bus 9454 * yet, so the timeout must be due to 9455 * some other command. Reset the timer 9456 * and go on. 9457 */ 9458 if (ahd_other_scb_timeout(ahd, scb, NULL) == 0) 9459 goto bus_reset; 9460 } else { 9461 /* 9462 * This SCB is for a disconnected transaction 9463 * and we haven't found a better candidate on 9464 * the bus to explain this timeout. 9465 */ 9466 ahd_set_recoveryscb(ahd, scb); 9467 9468 /* 9469 * Actually re-queue this SCB in an attempt 9470 * to select the device before it reconnects. 9471 * In either case (selection or reselection), 9472 * we will now issue a target reset to the 9473 * timed-out device. 9474 */ 9475 scb->flags |= SCB_DEVICE_RESET; 9476 scb->hscb->cdb_len = 0; 9477 scb->hscb->task_attribute = 0; 9478 scb->hscb->task_management = SIU_TASKMGMT_ABORT_TASK; 9479 9480 ahd_set_scbptr(ahd, SCB_GET_TAG(scb)); 9481 if ((scb->flags & SCB_PACKETIZED) != 0) { 9482 /* 9483 * Mark the SCB has having an outstanding 9484 * task management function. Should the command 9485 * complete normally before the task management 9486 * function can be sent, the host will be 9487 * notified to abort our requeued SCB. 9488 */ 9489 ahd_outb(ahd, SCB_TASK_MANAGEMENT, 9490 scb->hscb->task_management); 9491 } else { 9492 /* 9493 * If non-packetized, set the MK_MESSAGE control 9494 * bit indicating that we desire to send a 9495 * message. We also set the disconnected flag 9496 * since there is no guarantee that our SCB 9497 * control byte matches the version on the 9498 * card. We don't want the sequencer to abort 9499 * the command thinking an unsolicited 9500 * reselection occurred. 9501 */ 9502 scb->hscb->control |= MK_MESSAGE|DISCONNECTED; 9503 9504 /* 9505 * The sequencer will never re-reference the 9506 * in-core SCB. To make sure we are notified 9507 * during reslection, set the MK_MESSAGE flag in 9508 * the card's copy of the SCB. 9509 */ 9510 ahd_outb(ahd, SCB_CONTROL, 9511 ahd_inb(ahd, SCB_CONTROL)|MK_MESSAGE); 9512 } 9513 9514 /* 9515 * Clear out any entries in the QINFIFO first 9516 * so we are the next SCB for this target 9517 * to run. 9518 */ 9519 ahd_search_qinfifo(ahd, target, channel, lun, 9520 SCB_LIST_NULL, ROLE_INITIATOR, 9521 CAM_REQUEUE_REQ, SEARCH_COMPLETE); 9522 ahd_qinfifo_requeue_tail(ahd, scb); 9523 ahd_set_scbptr(ahd, active_scbptr); 9524 ahd_print_path(ahd, scb); 9525 printf("Queuing a BDR SCB\n"); 9526 aic_scb_timer_reset(scb, 2 * 1000); 9527 break; 9528 } 9529 } 9530 9531 /* 9532 * Any remaining SCBs were not the "culprit", so remove 9533 * them from the timeout list. The timer for these commands 9534 * will be reset once the recovery SCB completes. 9535 */ 9536 while ((scb = LIST_FIRST(&ahd->timedout_scbs)) != NULL) { 9537 9538 LIST_REMOVE(scb, timedout_links); 9539 scb->flags &= ~SCB_TIMEDOUT; 9540 } 9541 9542 ahd_unpause(ahd); 9543 } 9544 9545 /* 9546 * Re-schedule a timeout for the passed in SCB if we determine that some 9547 * other SCB is in the process of recovery or an SCB with a longer 9548 * timeout is still pending. Limit our search to just "other_scb" 9549 * if it is non-NULL. 9550 */ 9551 static int 9552 ahd_other_scb_timeout(struct ahd_softc *ahd, struct scb *scb, 9553 struct scb *other_scb) 9554 { 9555 u_int newtimeout; 9556 int found; 9557 9558 ahd_print_path(ahd, scb); 9559 printf("Other SCB Timeout%s", 9560 (scb->flags & SCB_OTHERTCL_TIMEOUT) != 0 9561 ? " again\n" : "\n"); 9562 9563 AHD_UNCORRECTABLE_ERROR(ahd); 9564 newtimeout = aic_get_timeout(scb); 9565 scb->flags |= SCB_OTHERTCL_TIMEOUT; 9566 found = 0; 9567 if (other_scb != NULL) { 9568 if ((other_scb->flags 9569 & (SCB_OTHERTCL_TIMEOUT|SCB_TIMEDOUT)) == 0 9570 || (other_scb->flags & SCB_RECOVERY_SCB) != 0) { 9571 found++; 9572 newtimeout = MAX(aic_get_timeout(other_scb), 9573 newtimeout); 9574 } 9575 } else { 9576 LIST_FOREACH(other_scb, &ahd->pending_scbs, pending_links) { 9577 if ((other_scb->flags 9578 & (SCB_OTHERTCL_TIMEOUT|SCB_TIMEDOUT)) == 0 9579 || (other_scb->flags & SCB_RECOVERY_SCB) != 0) { 9580 found++; 9581 newtimeout = MAX(aic_get_timeout(other_scb), 9582 newtimeout); 9583 } 9584 } 9585 } 9586 9587 if (found != 0) 9588 aic_scb_timer_reset(scb, newtimeout); 9589 else { 9590 ahd_print_path(ahd, scb); 9591 printf("No other SCB worth waiting for...\n"); 9592 } 9593 9594 return (found != 0); 9595 } 9596 9597 /**************************** Flexport Logic **********************************/ 9598 /* 9599 * Read count 16bit words from 16bit word address start_addr from the 9600 * SEEPROM attached to the controller, into buf, using the controller's 9601 * SEEPROM reading state machine. Optionally treat the data as a byte 9602 * stream in terms of byte order. 9603 */ 9604 int 9605 ahd_read_seeprom(struct ahd_softc *ahd, uint16_t *buf, 9606 u_int start_addr, u_int count, int bytestream) 9607 { 9608 u_int cur_addr; 9609 u_int end_addr; 9610 int error; 9611 9612 /* 9613 * If we never make it through the loop even once, 9614 * we were passed invalid arguments. 9615 */ 9616 error = EINVAL; 9617 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK); 9618 end_addr = start_addr + count; 9619 for (cur_addr = start_addr; cur_addr < end_addr; cur_addr++) { 9620 9621 ahd_outb(ahd, SEEADR, cur_addr); 9622 ahd_outb(ahd, SEECTL, SEEOP_READ | SEESTART); 9623 9624 error = ahd_wait_seeprom(ahd); 9625 if (error) 9626 break; 9627 if (bytestream != 0) { 9628 uint8_t *bytestream_ptr; 9629 9630 bytestream_ptr = (uint8_t *)buf; 9631 *bytestream_ptr++ = ahd_inb(ahd, SEEDAT); 9632 *bytestream_ptr = ahd_inb(ahd, SEEDAT+1); 9633 } else { 9634 /* 9635 * ahd_inw() already handles machine byte order. 9636 */ 9637 *buf = ahd_inw(ahd, SEEDAT); 9638 } 9639 buf++; 9640 } 9641 return (error); 9642 } 9643 9644 /* 9645 * Write count 16bit words from buf, into SEEPROM attache to the 9646 * controller starting at 16bit word address start_addr, using the 9647 * controller's SEEPROM writing state machine. 9648 */ 9649 int 9650 ahd_write_seeprom(struct ahd_softc *ahd, uint16_t *buf, 9651 u_int start_addr, u_int count) 9652 { 9653 u_int cur_addr; 9654 u_int end_addr; 9655 int error; 9656 int retval; 9657 9658 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK); 9659 error = ENOENT; 9660 9661 /* Place the chip into write-enable mode */ 9662 ahd_outb(ahd, SEEADR, SEEOP_EWEN_ADDR); 9663 ahd_outb(ahd, SEECTL, SEEOP_EWEN | SEESTART); 9664 error = ahd_wait_seeprom(ahd); 9665 if (error) 9666 return (error); 9667 9668 /* 9669 * Write the data. If we don't get throught the loop at 9670 * least once, the arguments were invalid. 9671 */ 9672 retval = EINVAL; 9673 end_addr = start_addr + count; 9674 for (cur_addr = start_addr; cur_addr < end_addr; cur_addr++) { 9675 ahd_outw(ahd, SEEDAT, *buf++); 9676 ahd_outb(ahd, SEEADR, cur_addr); 9677 ahd_outb(ahd, SEECTL, SEEOP_WRITE | SEESTART); 9678 9679 retval = ahd_wait_seeprom(ahd); 9680 if (retval) 9681 break; 9682 } 9683 9684 /* 9685 * Disable writes. 9686 */ 9687 ahd_outb(ahd, SEEADR, SEEOP_EWDS_ADDR); 9688 ahd_outb(ahd, SEECTL, SEEOP_EWDS | SEESTART); 9689 error = ahd_wait_seeprom(ahd); 9690 if (error) 9691 return (error); 9692 return (retval); 9693 } 9694 9695 /* 9696 * Wait ~100us for the serial eeprom to satisfy our request. 9697 */ 9698 int 9699 ahd_wait_seeprom(struct ahd_softc *ahd) 9700 { 9701 int cnt; 9702 9703 cnt = 5000; 9704 while ((ahd_inb(ahd, SEESTAT) & (SEEARBACK|SEEBUSY)) != 0 && --cnt) 9705 aic_delay(5); 9706 9707 if (cnt == 0) 9708 return (ETIMEDOUT); 9709 return (0); 9710 } 9711 9712 /* 9713 * Validate the two checksums in the per_channel 9714 * vital product data struct. 9715 */ 9716 int 9717 ahd_verify_vpd_cksum(struct vpd_config *vpd) 9718 { 9719 int i; 9720 int maxaddr; 9721 uint32_t checksum; 9722 uint8_t *vpdarray; 9723 9724 vpdarray = (uint8_t *)vpd; 9725 maxaddr = offsetof(struct vpd_config, vpd_checksum); 9726 checksum = 0; 9727 for (i = offsetof(struct vpd_config, resource_type); i < maxaddr; i++) 9728 checksum = checksum + vpdarray[i]; 9729 if (checksum == 0 9730 || (-checksum & 0xFF) != vpd->vpd_checksum) 9731 return (0); 9732 9733 checksum = 0; 9734 maxaddr = offsetof(struct vpd_config, checksum); 9735 for (i = offsetof(struct vpd_config, default_target_flags); 9736 i < maxaddr; i++) 9737 checksum = checksum + vpdarray[i]; 9738 if (checksum == 0 9739 || (-checksum & 0xFF) != vpd->checksum) 9740 return (0); 9741 return (1); 9742 } 9743 9744 int 9745 ahd_verify_cksum(struct seeprom_config *sc) 9746 { 9747 int i; 9748 int maxaddr; 9749 uint32_t checksum; 9750 uint16_t *scarray; 9751 9752 maxaddr = (sizeof(*sc)/2) - 1; 9753 checksum = 0; 9754 scarray = (uint16_t *)sc; 9755 9756 for (i = 0; i < maxaddr; i++) 9757 checksum = checksum + scarray[i]; 9758 if (checksum == 0 9759 || (checksum & 0xFFFF) != sc->checksum) { 9760 return (0); 9761 } else { 9762 return (1); 9763 } 9764 } 9765 9766 int 9767 ahd_acquire_seeprom(struct ahd_softc *ahd) 9768 { 9769 /* 9770 * We should be able to determine the SEEPROM type 9771 * from the flexport logic, but unfortunately not 9772 * all implementations have this logic and there is 9773 * no programatic method for determining if the logic 9774 * is present. 9775 */ 9776 return (1); 9777 #if 0 9778 uint8_t seetype; 9779 int error; 9780 9781 error = ahd_read_flexport(ahd, FLXADDR_ROMSTAT_CURSENSECTL, &seetype); 9782 if (error != 0 9783 || ((seetype & FLX_ROMSTAT_SEECFG) == FLX_ROMSTAT_SEE_NONE)) 9784 return (0); 9785 return (1); 9786 #endif 9787 } 9788 9789 void 9790 ahd_release_seeprom(struct ahd_softc *ahd) 9791 { 9792 /* Currently a no-op */ 9793 } 9794 9795 int 9796 ahd_write_flexport(struct ahd_softc *ahd, u_int addr, u_int value) 9797 { 9798 int error; 9799 9800 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK); 9801 if (addr > 7) 9802 panic("ahd_write_flexport: address out of range"); 9803 ahd_outb(ahd, BRDCTL, BRDEN|(addr << 3)); 9804 error = ahd_wait_flexport(ahd); 9805 if (error != 0) 9806 return (error); 9807 ahd_outb(ahd, BRDDAT, value); 9808 ahd_flush_device_writes(ahd); 9809 ahd_outb(ahd, BRDCTL, BRDSTB|BRDEN|(addr << 3)); 9810 ahd_flush_device_writes(ahd); 9811 ahd_outb(ahd, BRDCTL, BRDEN|(addr << 3)); 9812 ahd_flush_device_writes(ahd); 9813 ahd_outb(ahd, BRDCTL, 0); 9814 ahd_flush_device_writes(ahd); 9815 return (0); 9816 } 9817 9818 int 9819 ahd_read_flexport(struct ahd_softc *ahd, u_int addr, uint8_t *value) 9820 { 9821 int error; 9822 9823 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK); 9824 if (addr > 7) 9825 panic("ahd_read_flexport: address out of range"); 9826 ahd_outb(ahd, BRDCTL, BRDRW|BRDEN|(addr << 3)); 9827 error = ahd_wait_flexport(ahd); 9828 if (error != 0) 9829 return (error); 9830 *value = ahd_inb(ahd, BRDDAT); 9831 ahd_outb(ahd, BRDCTL, 0); 9832 ahd_flush_device_writes(ahd); 9833 return (0); 9834 } 9835 9836 /* 9837 * Wait at most 2 seconds for flexport arbitration to succeed. 9838 */ 9839 int 9840 ahd_wait_flexport(struct ahd_softc *ahd) 9841 { 9842 int cnt; 9843 9844 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK); 9845 cnt = 1000000 * 2 / 5; 9846 while ((ahd_inb(ahd, BRDCTL) & FLXARBACK) == 0 && --cnt) 9847 aic_delay(5); 9848 9849 if (cnt == 0) 9850 return (ETIMEDOUT); 9851 return (0); 9852 } 9853 9854 /************************* Target Mode ****************************************/ 9855 #ifdef AHD_TARGET_MODE 9856 cam_status 9857 ahd_find_tmode_devs(struct ahd_softc *ahd, struct cam_sim *sim, union ccb *ccb, 9858 struct ahd_tmode_tstate **tstate, 9859 struct ahd_tmode_lstate **lstate, 9860 int notfound_failure) 9861 { 9862 9863 if ((ahd->features & AHD_TARGETMODE) == 0) 9864 return (CAM_REQ_INVALID); 9865 9866 /* 9867 * Handle the 'black hole' device that sucks up 9868 * requests to unattached luns on enabled targets. 9869 */ 9870 if (ccb->ccb_h.target_id == CAM_TARGET_WILDCARD 9871 && ccb->ccb_h.target_lun == CAM_LUN_WILDCARD) { 9872 *tstate = NULL; 9873 *lstate = ahd->black_hole; 9874 } else { 9875 u_int max_id; 9876 9877 max_id = (ahd->features & AHD_WIDE) ? 15 : 7; 9878 if (ccb->ccb_h.target_id > max_id) 9879 return (CAM_TID_INVALID); 9880 9881 if (ccb->ccb_h.target_lun >= AHD_NUM_LUNS) 9882 return (CAM_LUN_INVALID); 9883 9884 *tstate = ahd->enabled_targets[ccb->ccb_h.target_id]; 9885 *lstate = NULL; 9886 if (*tstate != NULL) 9887 *lstate = 9888 (*tstate)->enabled_luns[ccb->ccb_h.target_lun]; 9889 } 9890 9891 if (notfound_failure != 0 && *lstate == NULL) 9892 return (CAM_PATH_INVALID); 9893 9894 return (CAM_REQ_CMP); 9895 } 9896 9897 void 9898 ahd_handle_en_lun(struct ahd_softc *ahd, struct cam_sim *sim, union ccb *ccb) 9899 { 9900 #if NOT_YET 9901 struct ahd_tmode_tstate *tstate; 9902 struct ahd_tmode_lstate *lstate; 9903 struct ccb_en_lun *cel; 9904 cam_status status; 9905 u_int target; 9906 u_int lun; 9907 u_int target_mask; 9908 u_long s; 9909 char channel; 9910 9911 status = ahd_find_tmode_devs(ahd, sim, ccb, &tstate, &lstate, 9912 /*notfound_failure*/FALSE); 9913 9914 if (status != CAM_REQ_CMP) { 9915 ccb->ccb_h.status = status; 9916 return; 9917 } 9918 9919 if ((ahd->features & AHD_MULTIROLE) != 0) { 9920 u_int our_id; 9921 9922 our_id = ahd->our_id; 9923 if (ccb->ccb_h.target_id != our_id) { 9924 if ((ahd->features & AHD_MULTI_TID) != 0 9925 && (ahd->flags & AHD_INITIATORROLE) != 0) { 9926 /* 9927 * Only allow additional targets if 9928 * the initiator role is disabled. 9929 * The hardware cannot handle a re-select-in 9930 * on the initiator id during a re-select-out 9931 * on a different target id. 9932 */ 9933 status = CAM_TID_INVALID; 9934 } else if ((ahd->flags & AHD_INITIATORROLE) != 0 9935 || ahd->enabled_luns > 0) { 9936 /* 9937 * Only allow our target id to change 9938 * if the initiator role is not configured 9939 * and there are no enabled luns which 9940 * are attached to the currently registered 9941 * scsi id. 9942 */ 9943 status = CAM_TID_INVALID; 9944 } 9945 } 9946 } 9947 9948 if (status != CAM_REQ_CMP) { 9949 ccb->ccb_h.status = status; 9950 return; 9951 } 9952 9953 /* 9954 * We now have an id that is valid. 9955 * If we aren't in target mode, switch modes. 9956 */ 9957 if ((ahd->flags & AHD_TARGETROLE) == 0 9958 && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) { 9959 printf("Configuring Target Mode\n"); 9960 if (LIST_FIRST(&ahd->pending_scbs) != NULL) { 9961 ccb->ccb_h.status = CAM_BUSY; 9962 return; 9963 } 9964 ahd->flags |= AHD_TARGETROLE; 9965 if ((ahd->features & AHD_MULTIROLE) == 0) 9966 ahd->flags &= ~AHD_INITIATORROLE; 9967 ahd_pause(ahd); 9968 ahd_loadseq(ahd); 9969 ahd_restart(ahd); 9970 } 9971 cel = &ccb->cel; 9972 target = ccb->ccb_h.target_id; 9973 lun = ccb->ccb_h.target_lun; 9974 channel = SIM_CHANNEL(ahd, sim); 9975 target_mask = 0x01 << target; 9976 if (channel == 'B') 9977 target_mask <<= 8; 9978 9979 if (cel->enable != 0) { 9980 u_int scsiseq1; 9981 9982 /* Are we already enabled?? */ 9983 if (lstate != NULL) { 9984 xpt_print_path(ccb->ccb_h.path); 9985 printf("Lun already enabled\n"); 9986 AHD_CORRECTABLE_ERROR(ahd); 9987 ccb->ccb_h.status = CAM_LUN_ALRDY_ENA; 9988 return; 9989 } 9990 9991 if (cel->grp6_len != 0 9992 || cel->grp7_len != 0) { 9993 /* 9994 * Don't (yet?) support vendor 9995 * specific commands. 9996 */ 9997 ccb->ccb_h.status = CAM_REQ_INVALID; 9998 printf("Non-zero Group Codes\n"); 9999 return; 10000 } 10001 10002 /* 10003 * Seems to be okay. 10004 * Setup our data structures. 10005 */ 10006 if (target != CAM_TARGET_WILDCARD && tstate == NULL) { 10007 tstate = ahd_alloc_tstate(ahd, target, channel); 10008 if (tstate == NULL) { 10009 xpt_print_path(ccb->ccb_h.path); 10010 printf("Couldn't allocate tstate\n"); 10011 ccb->ccb_h.status = CAM_RESRC_UNAVAIL; 10012 return; 10013 } 10014 } 10015 lstate = malloc(sizeof(*lstate), M_DEVBUF, M_NOWAIT); 10016 if (lstate == NULL) { 10017 xpt_print_path(ccb->ccb_h.path); 10018 printf("Couldn't allocate lstate\n"); 10019 ccb->ccb_h.status = CAM_RESRC_UNAVAIL; 10020 return; 10021 } 10022 memset(lstate, 0, sizeof(*lstate)); 10023 status = xpt_create_path(&lstate->path, /*periph*/NULL, 10024 xpt_path_path_id(ccb->ccb_h.path), 10025 xpt_path_target_id(ccb->ccb_h.path), 10026 xpt_path_lun_id(ccb->ccb_h.path)); 10027 if (status != CAM_REQ_CMP) { 10028 free(lstate, M_DEVBUF); 10029 xpt_print_path(ccb->ccb_h.path); 10030 printf("Couldn't allocate path\n"); 10031 ccb->ccb_h.status = CAM_RESRC_UNAVAIL; 10032 return; 10033 } 10034 SLIST_INIT(&lstate->accept_tios); 10035 SLIST_INIT(&lstate->immed_notifies); 10036 ahd_pause(ahd); 10037 if (target != CAM_TARGET_WILDCARD) { 10038 tstate->enabled_luns[lun] = lstate; 10039 ahd->enabled_luns++; 10040 10041 if ((ahd->features & AHD_MULTI_TID) != 0) { 10042 u_int targid_mask; 10043 10044 targid_mask = ahd_inw(ahd, TARGID); 10045 targid_mask |= target_mask; 10046 ahd_outw(ahd, TARGID, targid_mask); 10047 ahd_update_scsiid(ahd, targid_mask); 10048 } else { 10049 u_int our_id; 10050 char channel; 10051 10052 channel = SIM_CHANNEL(ahd, sim); 10053 our_id = SIM_SCSI_ID(ahd, sim); 10054 10055 /* 10056 * This can only happen if selections 10057 * are not enabled 10058 */ 10059 if (target != our_id) { 10060 u_int sblkctl; 10061 char cur_channel; 10062 int swap; 10063 10064 sblkctl = ahd_inb(ahd, SBLKCTL); 10065 cur_channel = (sblkctl & SELBUSB) 10066 ? 'B' : 'A'; 10067 if ((ahd->features & AHD_TWIN) == 0) 10068 cur_channel = 'A'; 10069 swap = cur_channel != channel; 10070 ahd->our_id = target; 10071 10072 if (swap) 10073 ahd_outb(ahd, SBLKCTL, 10074 sblkctl ^ SELBUSB); 10075 10076 ahd_outb(ahd, SCSIID, target); 10077 10078 if (swap) 10079 ahd_outb(ahd, SBLKCTL, sblkctl); 10080 } 10081 } 10082 } else 10083 ahd->black_hole = lstate; 10084 /* Allow select-in operations */ 10085 if (ahd->black_hole != NULL && ahd->enabled_luns > 0) { 10086 scsiseq1 = ahd_inb(ahd, SCSISEQ_TEMPLATE); 10087 scsiseq1 |= ENSELI; 10088 ahd_outb(ahd, SCSISEQ_TEMPLATE, scsiseq1); 10089 scsiseq1 = ahd_inb(ahd, SCSISEQ1); 10090 scsiseq1 |= ENSELI; 10091 ahd_outb(ahd, SCSISEQ1, scsiseq1); 10092 } 10093 ahd_unpause(ahd); 10094 ccb->ccb_h.status = CAM_REQ_CMP; 10095 xpt_print_path(ccb->ccb_h.path); 10096 printf("Lun now enabled for target mode\n"); 10097 } else { 10098 struct scb *scb; 10099 int i, empty; 10100 10101 if (lstate == NULL) { 10102 ccb->ccb_h.status = CAM_LUN_INVALID; 10103 return; 10104 } 10105 10106 ccb->ccb_h.status = CAM_REQ_CMP; 10107 LIST_FOREACH(scb, &ahd->pending_scbs, pending_links) { 10108 struct ccb_hdr *ccbh; 10109 10110 ccbh = &scb->io_ctx->ccb_h; 10111 if (ccbh->func_code == XPT_CONT_TARGET_IO 10112 && !xpt_path_comp(ccbh->path, ccb->ccb_h.path)){ 10113 printf("CTIO pending\n"); 10114 ccb->ccb_h.status = CAM_REQ_INVALID; 10115 return; 10116 } 10117 } 10118 10119 if (SLIST_FIRST(&lstate->accept_tios) != NULL) { 10120 printf("ATIOs pending\n"); 10121 ccb->ccb_h.status = CAM_REQ_INVALID; 10122 } 10123 10124 if (SLIST_FIRST(&lstate->immed_notifies) != NULL) { 10125 printf("INOTs pending\n"); 10126 ccb->ccb_h.status = CAM_REQ_INVALID; 10127 } 10128 10129 if (ccb->ccb_h.status != CAM_REQ_CMP) { 10130 return; 10131 } 10132 10133 xpt_print_path(ccb->ccb_h.path); 10134 printf("Target mode disabled\n"); 10135 xpt_free_path(lstate->path); 10136 free(lstate, M_DEVBUF); 10137 10138 ahd_pause(ahd); 10139 /* Can we clean up the target too? */ 10140 if (target != CAM_TARGET_WILDCARD) { 10141 tstate->enabled_luns[lun] = NULL; 10142 ahd->enabled_luns--; 10143 for (empty = 1, i = 0; i < 8; i++) 10144 if (tstate->enabled_luns[i] != NULL) { 10145 empty = 0; 10146 break; 10147 } 10148 10149 if (empty) { 10150 ahd_free_tstate(ahd, target, channel, 10151 /*force*/FALSE); 10152 if (ahd->features & AHD_MULTI_TID) { 10153 u_int targid_mask; 10154 10155 targid_mask = ahd_inw(ahd, TARGID); 10156 targid_mask &= ~target_mask; 10157 ahd_outw(ahd, TARGID, targid_mask); 10158 ahd_update_scsiid(ahd, targid_mask); 10159 } 10160 } 10161 } else { 10162 10163 ahd->black_hole = NULL; 10164 10165 /* 10166 * We can't allow selections without 10167 * our black hole device. 10168 */ 10169 empty = TRUE; 10170 } 10171 if (ahd->enabled_luns == 0) { 10172 /* Disallow select-in */ 10173 u_int scsiseq1; 10174 10175 scsiseq1 = ahd_inb(ahd, SCSISEQ_TEMPLATE); 10176 scsiseq1 &= ~ENSELI; 10177 ahd_outb(ahd, SCSISEQ_TEMPLATE, scsiseq1); 10178 scsiseq1 = ahd_inb(ahd, SCSISEQ1); 10179 scsiseq1 &= ~ENSELI; 10180 ahd_outb(ahd, SCSISEQ1, scsiseq1); 10181 10182 if ((ahd->features & AHD_MULTIROLE) == 0) { 10183 printf("Configuring Initiator Mode\n"); 10184 ahd->flags &= ~AHD_TARGETROLE; 10185 ahd->flags |= AHD_INITIATORROLE; 10186 ahd_pause(ahd); 10187 ahd_loadseq(ahd); 10188 ahd_restart(ahd); 10189 /* 10190 * Unpaused. The extra unpause 10191 * that follows is harmless. 10192 */ 10193 } 10194 } 10195 ahd_unpause(ahd); 10196 } 10197 #endif 10198 } 10199 10200 static void 10201 ahd_update_scsiid(struct ahd_softc *ahd, u_int targid_mask) 10202 { 10203 #if NOT_YET 10204 u_int scsiid_mask; 10205 u_int scsiid; 10206 10207 if ((ahd->features & AHD_MULTI_TID) == 0) 10208 panic("ahd_update_scsiid called on non-multitid unit\n"); 10209 10210 /* 10211 * Since we will rely on the TARGID mask 10212 * for selection enables, ensure that OID 10213 * in SCSIID is not set to some other ID 10214 * that we don't want to allow selections on. 10215 */ 10216 if ((ahd->features & AHD_ULTRA2) != 0) 10217 scsiid = ahd_inb(ahd, SCSIID_ULTRA2); 10218 else 10219 scsiid = ahd_inb(ahd, SCSIID); 10220 scsiid_mask = 0x1 << (scsiid & OID); 10221 if ((targid_mask & scsiid_mask) == 0) { 10222 u_int our_id; 10223 10224 /* ffs counts from 1 */ 10225 our_id = ffs(targid_mask); 10226 if (our_id == 0) 10227 our_id = ahd->our_id; 10228 else 10229 our_id--; 10230 scsiid &= TID; 10231 scsiid |= our_id; 10232 } 10233 if ((ahd->features & AHD_ULTRA2) != 0) 10234 ahd_outb(ahd, SCSIID_ULTRA2, scsiid); 10235 else 10236 ahd_outb(ahd, SCSIID, scsiid); 10237 #endif 10238 } 10239 10240 void 10241 ahd_run_tqinfifo(struct ahd_softc *ahd, int paused) 10242 { 10243 struct target_cmd *cmd; 10244 10245 ahd_sync_tqinfifo(ahd, BUS_DMASYNC_POSTREAD); 10246 while ((cmd = &ahd->targetcmds[ahd->tqinfifonext])->cmd_valid != 0) { 10247 10248 /* 10249 * Only advance through the queue if we 10250 * have the resources to process the command. 10251 */ 10252 if (ahd_handle_target_cmd(ahd, cmd) != 0) 10253 break; 10254 10255 cmd->cmd_valid = 0; 10256 ahd_dmamap_sync(ahd, ahd->shared_data_dmat, 10257 ahd->shared_data_dmamap, 10258 ahd_targetcmd_offset(ahd, ahd->tqinfifonext), 10259 sizeof(struct target_cmd), 10260 BUS_DMASYNC_PREREAD); 10261 ahd->tqinfifonext++; 10262 10263 /* 10264 * Lazily update our position in the target mode incoming 10265 * command queue as seen by the sequencer. 10266 */ 10267 if ((ahd->tqinfifonext & (HOST_TQINPOS - 1)) == 1) { 10268 u_int hs_mailbox; 10269 10270 hs_mailbox = ahd_inb(ahd, HS_MAILBOX); 10271 hs_mailbox &= ~HOST_TQINPOS; 10272 hs_mailbox |= ahd->tqinfifonext & HOST_TQINPOS; 10273 ahd_outb(ahd, HS_MAILBOX, hs_mailbox); 10274 } 10275 } 10276 } 10277 10278 static int 10279 ahd_handle_target_cmd(struct ahd_softc *ahd, struct target_cmd *cmd) 10280 { 10281 struct ahd_tmode_tstate *tstate; 10282 struct ahd_tmode_lstate *lstate; 10283 struct ccb_accept_tio *atio; 10284 uint8_t *byte; 10285 int initiator; 10286 int target; 10287 int lun; 10288 10289 initiator = SCSIID_TARGET(ahd, cmd->scsiid); 10290 target = SCSIID_OUR_ID(cmd->scsiid); 10291 lun = (cmd->identify & MSG_IDENTIFY_LUNMASK); 10292 10293 byte = cmd->bytes; 10294 tstate = ahd->enabled_targets[target]; 10295 lstate = NULL; 10296 if (tstate != NULL) 10297 lstate = tstate->enabled_luns[lun]; 10298 10299 /* 10300 * Commands for disabled luns go to the black hole driver. 10301 */ 10302 if (lstate == NULL) 10303 lstate = ahd->black_hole; 10304 10305 atio = (struct ccb_accept_tio*)SLIST_FIRST(&lstate->accept_tios); 10306 if (atio == NULL) { 10307 ahd->flags |= AHD_TQINFIFO_BLOCKED; 10308 /* 10309 * Wait for more ATIOs from the peripheral driver for this lun. 10310 */ 10311 return (1); 10312 } else 10313 ahd->flags &= ~AHD_TQINFIFO_BLOCKED; 10314 #ifdef AHD_DEBUG 10315 if ((ahd_debug & AHD_SHOW_TQIN) != 0) 10316 printf("Incoming command from %d for %d:%d%s\n", 10317 initiator, target, lun, 10318 lstate == ahd->black_hole ? "(Black Holed)" : ""); 10319 #endif 10320 SLIST_REMOVE_HEAD(&lstate->accept_tios, sim_links.sle); 10321 10322 if (lstate == ahd->black_hole) { 10323 /* Fill in the wildcards */ 10324 atio->ccb_h.target_id = target; 10325 atio->ccb_h.target_lun = lun; 10326 } 10327 10328 /* 10329 * Package it up and send it off to 10330 * whomever has this lun enabled. 10331 */ 10332 atio->sense_len = 0; 10333 atio->init_id = initiator; 10334 if (byte[0] != 0xFF) { 10335 /* Tag was included */ 10336 atio->tag_action = *byte++; 10337 atio->tag_id = *byte++; 10338 atio->ccb_h.flags |= CAM_TAG_ACTION_VALID; 10339 } else { 10340 atio->ccb_h.flags &= ~CAM_TAG_ACTION_VALID; 10341 } 10342 byte++; 10343 10344 /* Okay. Now determine the cdb size based on the command code */ 10345 switch (*byte >> CMD_GROUP_CODE_SHIFT) { 10346 case 0: 10347 atio->cdb_len = 6; 10348 break; 10349 case 1: 10350 case 2: 10351 atio->cdb_len = 10; 10352 break; 10353 case 4: 10354 atio->cdb_len = 16; 10355 break; 10356 case 5: 10357 atio->cdb_len = 12; 10358 break; 10359 case 3: 10360 default: 10361 /* Only copy the opcode. */ 10362 atio->cdb_len = 1; 10363 printf("Reserved or VU command code type encountered\n"); 10364 break; 10365 } 10366 10367 memcpy(atio->cdb_io.cdb_bytes, byte, atio->cdb_len); 10368 10369 atio->ccb_h.status |= CAM_CDB_RECVD; 10370 10371 if ((cmd->identify & MSG_IDENTIFY_DISCFLAG) == 0) { 10372 /* 10373 * We weren't allowed to disconnect. 10374 * We're hanging on the bus until a 10375 * continue target I/O comes in response 10376 * to this accept tio. 10377 */ 10378 #ifdef AHD_DEBUG 10379 if ((ahd_debug & AHD_SHOW_TQIN) != 0) 10380 printf("Received Immediate Command %d:%d:%d - %p\n", 10381 initiator, target, lun, ahd->pending_device); 10382 #endif 10383 ahd->pending_device = lstate; 10384 ahd_freeze_ccb((union ccb *)atio); 10385 atio->ccb_h.flags |= CAM_DIS_DISCONNECT; 10386 } 10387 xpt_done((union ccb*)atio); 10388 return (0); 10389 } 10390 10391 #endif 10392