1 /*- 2 * Core routines and tables shareable across OS platforms. 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 * 6 * Copyright (c) 1994-2002 Justin T. Gibbs. 7 * Copyright (c) 2000-2002 Adaptec Inc. 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions, and the following disclaimer, 15 * without modification. 16 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 17 * substantially similar to the "NO WARRANTY" disclaimer below 18 * ("Disclaimer") and any redistribution must be conditioned upon 19 * including a substantially similar Disclaimer requirement for further 20 * binary redistribution. 21 * 3. Neither the names of the above-listed copyright holders nor the names 22 * of any contributors may be used to endorse or promote products derived 23 * from this software without specific prior written permission. 24 * 25 * Alternatively, this software may be distributed under the terms of the 26 * GNU General Public License ("GPL") version 2 as published by the Free 27 * Software Foundation. 28 * 29 * NO WARRANTY 30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 31 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 32 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 33 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 34 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 38 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 39 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 40 * POSSIBILITY OF SUCH DAMAGES. 41 * 42 * $Id: //depot/aic7xxx/aic7xxx/aic7xxx.c#155 $ 43 */ 44 45 #ifdef __linux__ 46 #include "aic7xxx_osm.h" 47 #include "aic7xxx_inline.h" 48 #include "aicasm/aicasm_insformat.h" 49 #else 50 #include <sys/cdefs.h> 51 __FBSDID("$FreeBSD$"); 52 #include <dev/aic7xxx/aic7xxx_osm.h> 53 #include <dev/aic7xxx/aic7xxx_inline.h> 54 #include <dev/aic7xxx/aicasm/aicasm_insformat.h> 55 #endif 56 57 /****************************** Softc Data ************************************/ 58 struct ahc_softc_tailq ahc_tailq = TAILQ_HEAD_INITIALIZER(ahc_tailq); 59 60 /***************************** Lookup Tables **********************************/ 61 char *ahc_chip_names[] = 62 { 63 "NONE", 64 "aic7770", 65 "aic7850", 66 "aic7855", 67 "aic7859", 68 "aic7860", 69 "aic7870", 70 "aic7880", 71 "aic7895", 72 "aic7895C", 73 "aic7890/91", 74 "aic7896/97", 75 "aic7892", 76 "aic7899" 77 }; 78 79 /* 80 * Hardware error codes. 81 */ 82 struct ahc_hard_error_entry { 83 uint8_t errno; 84 char *errmesg; 85 }; 86 87 static struct ahc_hard_error_entry ahc_hard_errors[] = { 88 { ILLHADDR, "Illegal Host Access" }, 89 { ILLSADDR, "Illegal Sequencer Address referrenced" }, 90 { ILLOPCODE, "Illegal Opcode in sequencer program" }, 91 { SQPARERR, "Sequencer Parity Error" }, 92 { DPARERR, "Data-path Parity Error" }, 93 { MPARERR, "Scratch or SCB Memory Parity Error" }, 94 { PCIERRSTAT, "PCI Error detected" }, 95 { CIOPARERR, "CIOBUS Parity Error" }, 96 }; 97 static const u_int num_errors = NUM_ELEMENTS(ahc_hard_errors); 98 99 static struct ahc_phase_table_entry ahc_phase_table[] = 100 { 101 { P_DATAOUT, MSG_NOOP, "in Data-out phase" }, 102 { P_DATAIN, MSG_INITIATOR_DET_ERR, "in Data-in phase" }, 103 { P_DATAOUT_DT, MSG_NOOP, "in DT Data-out phase" }, 104 { P_DATAIN_DT, MSG_INITIATOR_DET_ERR, "in DT Data-in phase" }, 105 { P_COMMAND, MSG_NOOP, "in Command phase" }, 106 { P_MESGOUT, MSG_NOOP, "in Message-out phase" }, 107 { P_STATUS, MSG_INITIATOR_DET_ERR, "in Status phase" }, 108 { P_MESGIN, MSG_PARITY_ERROR, "in Message-in phase" }, 109 { P_BUSFREE, MSG_NOOP, "while idle" }, 110 { 0, MSG_NOOP, "in unknown phase" } 111 }; 112 113 /* 114 * In most cases we only wish to itterate over real phases, so 115 * exclude the last element from the count. 116 */ 117 static const u_int num_phases = NUM_ELEMENTS(ahc_phase_table) - 1; 118 119 /* 120 * Valid SCSIRATE values. (p. 3-17) 121 * Provides a mapping of transfer periods in ns to the proper value to 122 * stick in the scsixfer reg. 123 */ 124 static struct ahc_syncrate ahc_syncrates[] = 125 { 126 /* ultra2 fast/ultra period rate */ 127 { 0x42, 0x000, 9, "80.0" }, 128 { 0x03, 0x000, 10, "40.0" }, 129 { 0x04, 0x000, 11, "33.0" }, 130 { 0x05, 0x100, 12, "20.0" }, 131 { 0x06, 0x110, 15, "16.0" }, 132 { 0x07, 0x120, 18, "13.4" }, 133 { 0x08, 0x000, 25, "10.0" }, 134 { 0x19, 0x010, 31, "8.0" }, 135 { 0x1a, 0x020, 37, "6.67" }, 136 { 0x1b, 0x030, 43, "5.7" }, 137 { 0x1c, 0x040, 50, "5.0" }, 138 { 0x00, 0x050, 56, "4.4" }, 139 { 0x00, 0x060, 62, "4.0" }, 140 { 0x00, 0x070, 68, "3.6" }, 141 { 0x00, 0x000, 0, NULL } 142 }; 143 144 /* Our Sequencer Program */ 145 #include "aic7xxx_seq.h" 146 147 /**************************** Function Declarations ***************************/ 148 static void ahc_force_renegotiation(struct ahc_softc *ahc, 149 struct ahc_devinfo *devinfo); 150 static struct ahc_tmode_tstate* 151 ahc_alloc_tstate(struct ahc_softc *ahc, 152 u_int scsi_id, char channel); 153 #ifdef AHC_TARGET_MODE 154 static void ahc_free_tstate(struct ahc_softc *ahc, 155 u_int scsi_id, char channel, int force); 156 #endif 157 static struct ahc_syncrate* 158 ahc_devlimited_syncrate(struct ahc_softc *ahc, 159 struct ahc_initiator_tinfo *, 160 u_int *period, 161 u_int *ppr_options, 162 role_t role); 163 static void ahc_update_pending_scbs(struct ahc_softc *ahc); 164 static void ahc_fetch_devinfo(struct ahc_softc *ahc, 165 struct ahc_devinfo *devinfo); 166 static void ahc_scb_devinfo(struct ahc_softc *ahc, 167 struct ahc_devinfo *devinfo, 168 struct scb *scb); 169 static void ahc_assert_atn(struct ahc_softc *ahc); 170 static void ahc_setup_initiator_msgout(struct ahc_softc *ahc, 171 struct ahc_devinfo *devinfo, 172 struct scb *scb); 173 static void ahc_build_transfer_msg(struct ahc_softc *ahc, 174 struct ahc_devinfo *devinfo); 175 static void ahc_construct_sdtr(struct ahc_softc *ahc, 176 struct ahc_devinfo *devinfo, 177 u_int period, u_int offset); 178 static void ahc_construct_wdtr(struct ahc_softc *ahc, 179 struct ahc_devinfo *devinfo, 180 u_int bus_width); 181 static void ahc_construct_ppr(struct ahc_softc *ahc, 182 struct ahc_devinfo *devinfo, 183 u_int period, u_int offset, 184 u_int bus_width, u_int ppr_options); 185 static void ahc_clear_msg_state(struct ahc_softc *ahc); 186 static void ahc_handle_proto_violation(struct ahc_softc *ahc); 187 static void ahc_handle_message_phase(struct ahc_softc *ahc); 188 typedef enum { 189 AHCMSG_1B, 190 AHCMSG_2B, 191 AHCMSG_EXT 192 } ahc_msgtype; 193 static int ahc_sent_msg(struct ahc_softc *ahc, ahc_msgtype type, 194 u_int msgval, int full); 195 static int ahc_parse_msg(struct ahc_softc *ahc, 196 struct ahc_devinfo *devinfo); 197 static int ahc_handle_msg_reject(struct ahc_softc *ahc, 198 struct ahc_devinfo *devinfo); 199 static void ahc_handle_ign_wide_residue(struct ahc_softc *ahc, 200 struct ahc_devinfo *devinfo); 201 static void ahc_reinitialize_dataptrs(struct ahc_softc *ahc); 202 static void ahc_handle_devreset(struct ahc_softc *ahc, 203 struct ahc_devinfo *devinfo, 204 cam_status status, char *message, 205 int verbose_level); 206 #ifdef AHC_TARGET_MODE 207 static void ahc_setup_target_msgin(struct ahc_softc *ahc, 208 struct ahc_devinfo *devinfo, 209 struct scb *scb); 210 #endif 211 212 static bus_dmamap_callback_t ahc_dmamap_cb; 213 static void ahc_build_free_scb_list(struct ahc_softc *ahc); 214 static int ahc_init_scbdata(struct ahc_softc *ahc); 215 static void ahc_fini_scbdata(struct ahc_softc *ahc); 216 static void ahc_qinfifo_requeue(struct ahc_softc *ahc, 217 struct scb *prev_scb, 218 struct scb *scb); 219 static int ahc_qinfifo_count(struct ahc_softc *ahc); 220 static u_int ahc_rem_scb_from_disc_list(struct ahc_softc *ahc, 221 u_int prev, u_int scbptr); 222 static void ahc_add_curscb_to_free_list(struct ahc_softc *ahc); 223 static u_int ahc_rem_wscb(struct ahc_softc *ahc, 224 u_int scbpos, u_int prev); 225 static void ahc_reset_current_bus(struct ahc_softc *ahc); 226 #ifdef AHC_DUMP_SEQ 227 static void ahc_dumpseq(struct ahc_softc *ahc); 228 #endif 229 static int ahc_loadseq(struct ahc_softc *ahc); 230 static int ahc_check_patch(struct ahc_softc *ahc, 231 struct patch **start_patch, 232 u_int start_instr, u_int *skip_addr); 233 static void ahc_download_instr(struct ahc_softc *ahc, 234 u_int instrptr, uint8_t *dconsts); 235 static int ahc_other_scb_timeout(struct ahc_softc *ahc, 236 struct scb *scb, 237 struct scb *other_scb); 238 #ifdef AHC_TARGET_MODE 239 static void ahc_queue_lstate_event(struct ahc_softc *ahc, 240 struct ahc_tmode_lstate *lstate, 241 u_int initiator_id, 242 u_int event_type, 243 u_int event_arg); 244 static void ahc_update_scsiid(struct ahc_softc *ahc, 245 u_int targid_mask); 246 static int ahc_handle_target_cmd(struct ahc_softc *ahc, 247 struct target_cmd *cmd); 248 #endif 249 /************************* Sequencer Execution Control ************************/ 250 /* 251 * Restart the sequencer program from address zero 252 */ 253 void 254 ahc_restart(struct ahc_softc *ahc) 255 { 256 257 ahc_pause(ahc); 258 259 /* No more pending messages. */ 260 ahc_clear_msg_state(ahc); 261 262 ahc_outb(ahc, SCSISIGO, 0); /* De-assert BSY */ 263 ahc_outb(ahc, MSG_OUT, MSG_NOOP); /* No message to send */ 264 ahc_outb(ahc, SXFRCTL1, ahc_inb(ahc, SXFRCTL1) & ~BITBUCKET); 265 ahc_outb(ahc, LASTPHASE, P_BUSFREE); 266 ahc_outb(ahc, SAVED_SCSIID, 0xFF); 267 ahc_outb(ahc, SAVED_LUN, 0xFF); 268 269 /* 270 * Ensure that the sequencer's idea of TQINPOS 271 * matches our own. The sequencer increments TQINPOS 272 * only after it sees a DMA complete and a reset could 273 * occur before the increment leaving the kernel to believe 274 * the command arrived but the sequencer to not. 275 */ 276 ahc_outb(ahc, TQINPOS, ahc->tqinfifonext); 277 278 /* Always allow reselection */ 279 ahc_outb(ahc, SCSISEQ, 280 ahc_inb(ahc, SCSISEQ_TEMPLATE) & (ENSELI|ENRSELI|ENAUTOATNP)); 281 if ((ahc->features & AHC_CMD_CHAN) != 0) { 282 /* Ensure that no DMA operations are in progress */ 283 ahc_outb(ahc, CCSCBCNT, 0); 284 ahc_outb(ahc, CCSGCTL, 0); 285 ahc_outb(ahc, CCSCBCTL, 0); 286 } 287 /* 288 * If we were in the process of DMA'ing SCB data into 289 * an SCB, replace that SCB on the free list. This prevents 290 * an SCB leak. 291 */ 292 if ((ahc_inb(ahc, SEQ_FLAGS2) & SCB_DMA) != 0) { 293 ahc_add_curscb_to_free_list(ahc); 294 ahc_outb(ahc, SEQ_FLAGS2, 295 ahc_inb(ahc, SEQ_FLAGS2) & ~SCB_DMA); 296 } 297 298 /* 299 * Clear any pending sequencer interrupt. It is no 300 * longer relevant since we're resetting the Program 301 * Counter. 302 */ 303 ahc_outb(ahc, CLRINT, CLRSEQINT); 304 305 ahc_outb(ahc, MWI_RESIDUAL, 0); 306 ahc_outb(ahc, SEQCTL, ahc->seqctl); 307 ahc_outb(ahc, SEQADDR0, 0); 308 ahc_outb(ahc, SEQADDR1, 0); 309 310 ahc_unpause(ahc); 311 } 312 313 /************************* Input/Output Queues ********************************/ 314 void 315 ahc_run_qoutfifo(struct ahc_softc *ahc) 316 { 317 struct scb *scb; 318 u_int scb_index; 319 320 ahc_sync_qoutfifo(ahc, BUS_DMASYNC_POSTREAD); 321 while (ahc->qoutfifo[ahc->qoutfifonext] != SCB_LIST_NULL) { 322 scb_index = ahc->qoutfifo[ahc->qoutfifonext]; 323 if ((ahc->qoutfifonext & 0x03) == 0x03) { 324 u_int modnext; 325 326 /* 327 * Clear 32bits of QOUTFIFO at a time 328 * so that we don't clobber an incoming 329 * byte DMA to the array on architectures 330 * that only support 32bit load and store 331 * operations. 332 */ 333 modnext = ahc->qoutfifonext & ~0x3; 334 *((uint32_t *)(&ahc->qoutfifo[modnext])) = 0xFFFFFFFFUL; 335 aic_dmamap_sync(ahc, ahc->shared_data_dmat, 336 ahc->shared_data_dmamap, 337 /*offset*/modnext, /*len*/4, 338 BUS_DMASYNC_PREREAD); 339 } 340 ahc->qoutfifonext++; 341 342 scb = ahc_lookup_scb(ahc, scb_index); 343 if (scb == NULL) { 344 printf("%s: WARNING no command for scb %d " 345 "(cmdcmplt)\nQOUTPOS = %d\n", 346 ahc_name(ahc), scb_index, 347 (ahc->qoutfifonext - 1) & 0xFF); 348 continue; 349 } 350 351 /* 352 * Save off the residual 353 * if there is one. 354 */ 355 ahc_update_residual(ahc, scb); 356 ahc_done(ahc, scb); 357 } 358 } 359 360 void 361 ahc_run_untagged_queues(struct ahc_softc *ahc) 362 { 363 int i; 364 365 for (i = 0; i < 16; i++) 366 ahc_run_untagged_queue(ahc, &ahc->untagged_queues[i]); 367 } 368 369 void 370 ahc_run_untagged_queue(struct ahc_softc *ahc, struct scb_tailq *queue) 371 { 372 struct scb *scb; 373 374 if (ahc->untagged_queue_lock != 0) 375 return; 376 377 if ((scb = TAILQ_FIRST(queue)) != NULL 378 && (scb->flags & SCB_ACTIVE) == 0) { 379 scb->flags |= SCB_ACTIVE; 380 /* 381 * Timers are disabled while recovery is in progress. 382 */ 383 aic_scb_timer_start(scb); 384 ahc_queue_scb(ahc, scb); 385 } 386 } 387 388 /************************* Interrupt Handling *********************************/ 389 void 390 ahc_handle_brkadrint(struct ahc_softc *ahc) 391 { 392 /* 393 * We upset the sequencer :-( 394 * Lookup the error message 395 */ 396 int i; 397 int error; 398 399 error = ahc_inb(ahc, ERROR); 400 for (i = 0; error != 1 && i < num_errors; i++) 401 error >>= 1; 402 printf("%s: brkadrint, %s at seqaddr = 0x%x\n", 403 ahc_name(ahc), ahc_hard_errors[i].errmesg, 404 ahc_inb(ahc, SEQADDR0) | 405 (ahc_inb(ahc, SEQADDR1) << 8)); 406 407 ahc_dump_card_state(ahc); 408 409 /* Tell everyone that this HBA is no longer available */ 410 ahc_abort_scbs(ahc, CAM_TARGET_WILDCARD, ALL_CHANNELS, 411 CAM_LUN_WILDCARD, SCB_LIST_NULL, ROLE_UNKNOWN, 412 CAM_NO_HBA); 413 414 /* Disable all interrupt sources by resetting the controller */ 415 ahc_shutdown(ahc); 416 } 417 418 void 419 ahc_handle_seqint(struct ahc_softc *ahc, u_int intstat) 420 { 421 struct scb *scb; 422 struct ahc_devinfo devinfo; 423 424 ahc_fetch_devinfo(ahc, &devinfo); 425 426 /* 427 * Clear the upper byte that holds SEQINT status 428 * codes and clear the SEQINT bit. We will unpause 429 * the sequencer, if appropriate, after servicing 430 * the request. 431 */ 432 ahc_outb(ahc, CLRINT, CLRSEQINT); 433 switch (intstat & SEQINT_MASK) { 434 case BAD_STATUS: 435 { 436 u_int scb_index; 437 struct hardware_scb *hscb; 438 439 /* 440 * Set the default return value to 0 (don't 441 * send sense). The sense code will change 442 * this if needed. 443 */ 444 ahc_outb(ahc, RETURN_1, 0); 445 446 /* 447 * The sequencer will notify us when a command 448 * has an error that would be of interest to 449 * the kernel. This allows us to leave the sequencer 450 * running in the common case of command completes 451 * without error. The sequencer will already have 452 * dma'd the SCB back up to us, so we can reference 453 * the in kernel copy directly. 454 */ 455 scb_index = ahc_inb(ahc, SCB_TAG); 456 scb = ahc_lookup_scb(ahc, scb_index); 457 if (scb == NULL) { 458 ahc_print_devinfo(ahc, &devinfo); 459 printf("ahc_intr - referenced scb " 460 "not valid during seqint 0x%x scb(%d)\n", 461 intstat, scb_index); 462 ahc_dump_card_state(ahc); 463 panic("for safety"); 464 goto unpause; 465 } 466 467 hscb = scb->hscb; 468 469 /* Don't want to clobber the original sense code */ 470 if ((scb->flags & SCB_SENSE) != 0) { 471 /* 472 * Clear the SCB_SENSE Flag and have 473 * the sequencer do a normal command 474 * complete. 475 */ 476 scb->flags &= ~SCB_SENSE; 477 aic_set_transaction_status(scb, CAM_AUTOSENSE_FAIL); 478 break; 479 } 480 aic_set_transaction_status(scb, CAM_SCSI_STATUS_ERROR); 481 /* Freeze the queue until the client sees the error. */ 482 ahc_freeze_devq(ahc, scb); 483 aic_freeze_scb(scb); 484 aic_set_scsi_status(scb, hscb->shared_data.status.scsi_status); 485 switch (hscb->shared_data.status.scsi_status) { 486 case SCSI_STATUS_OK: 487 printf("%s: Interrupted for staus of 0???\n", 488 ahc_name(ahc)); 489 break; 490 case SCSI_STATUS_CMD_TERMINATED: 491 case SCSI_STATUS_CHECK_COND: 492 { 493 struct ahc_dma_seg *sg; 494 struct scsi_sense *sc; 495 struct ahc_initiator_tinfo *targ_info; 496 struct ahc_tmode_tstate *tstate; 497 struct ahc_transinfo *tinfo; 498 #ifdef AHC_DEBUG 499 if (ahc_debug & AHC_SHOW_SENSE) { 500 ahc_print_path(ahc, scb); 501 printf("SCB %d: requests Check Status\n", 502 scb->hscb->tag); 503 } 504 #endif 505 506 if (aic_perform_autosense(scb) == 0) 507 break; 508 509 targ_info = ahc_fetch_transinfo(ahc, 510 devinfo.channel, 511 devinfo.our_scsiid, 512 devinfo.target, 513 &tstate); 514 tinfo = &targ_info->curr; 515 sg = scb->sg_list; 516 sc = (struct scsi_sense *)(&hscb->shared_data.cdb); 517 /* 518 * Save off the residual if there is one. 519 */ 520 ahc_update_residual(ahc, scb); 521 #ifdef AHC_DEBUG 522 if (ahc_debug & AHC_SHOW_SENSE) { 523 ahc_print_path(ahc, scb); 524 printf("Sending Sense\n"); 525 } 526 #endif 527 sg->addr = ahc_get_sense_bufaddr(ahc, scb); 528 sg->len = aic_get_sense_bufsize(ahc, scb); 529 sg->len |= AHC_DMA_LAST_SEG; 530 531 /* Fixup byte order */ 532 sg->addr = aic_htole32(sg->addr); 533 sg->len = aic_htole32(sg->len); 534 535 sc->opcode = REQUEST_SENSE; 536 sc->byte2 = 0; 537 if (tinfo->protocol_version <= SCSI_REV_2 538 && SCB_GET_LUN(scb) < 8) 539 sc->byte2 = SCB_GET_LUN(scb) << 5; 540 sc->unused[0] = 0; 541 sc->unused[1] = 0; 542 sc->length = sg->len; 543 sc->control = 0; 544 545 /* 546 * We can't allow the target to disconnect. 547 * This will be an untagged transaction and 548 * having the target disconnect will make this 549 * transaction indestinguishable from outstanding 550 * tagged transactions. 551 */ 552 hscb->control = 0; 553 554 /* 555 * This request sense could be because the 556 * the device lost power or in some other 557 * way has lost our transfer negotiations. 558 * Renegotiate if appropriate. Unit attention 559 * errors will be reported before any data 560 * phases occur. 561 */ 562 if (aic_get_residual(scb) 563 == aic_get_transfer_length(scb)) { 564 ahc_update_neg_request(ahc, &devinfo, 565 tstate, targ_info, 566 AHC_NEG_IF_NON_ASYNC); 567 } 568 if (tstate->auto_negotiate & devinfo.target_mask) { 569 hscb->control |= MK_MESSAGE; 570 scb->flags &= ~SCB_NEGOTIATE; 571 scb->flags |= SCB_AUTO_NEGOTIATE; 572 } 573 hscb->cdb_len = sizeof(*sc); 574 hscb->dataptr = sg->addr; 575 hscb->datacnt = sg->len; 576 hscb->sgptr = scb->sg_list_phys | SG_FULL_RESID; 577 hscb->sgptr = aic_htole32(hscb->sgptr); 578 scb->sg_count = 1; 579 scb->flags |= SCB_SENSE; 580 ahc_qinfifo_requeue_tail(ahc, scb); 581 ahc_outb(ahc, RETURN_1, SEND_SENSE); 582 /* 583 * Ensure we have enough time to actually 584 * retrieve the sense, but only schedule 585 * the timer if we are not in recovery or 586 * this is a recovery SCB that is allowed 587 * to have an active timer. 588 */ 589 if (ahc->scb_data->recovery_scbs == 0 590 || (scb->flags & SCB_RECOVERY_SCB) != 0) 591 aic_scb_timer_reset(scb, 5 * 1000); 592 break; 593 } 594 default: 595 break; 596 } 597 break; 598 } 599 case NO_MATCH: 600 { 601 /* Ensure we don't leave the selection hardware on */ 602 ahc_outb(ahc, SCSISEQ, 603 ahc_inb(ahc, SCSISEQ) & (ENSELI|ENRSELI|ENAUTOATNP)); 604 605 printf("%s:%c:%d: no active SCB for reconnecting " 606 "target - issuing BUS DEVICE RESET\n", 607 ahc_name(ahc), devinfo.channel, devinfo.target); 608 printf("SAVED_SCSIID == 0x%x, SAVED_LUN == 0x%x, " 609 "ARG_1 == 0x%x ACCUM = 0x%x\n", 610 ahc_inb(ahc, SAVED_SCSIID), ahc_inb(ahc, SAVED_LUN), 611 ahc_inb(ahc, ARG_1), ahc_inb(ahc, ACCUM)); 612 printf("SEQ_FLAGS == 0x%x, SCBPTR == 0x%x, BTT == 0x%x, " 613 "SINDEX == 0x%x\n", 614 ahc_inb(ahc, SEQ_FLAGS), ahc_inb(ahc, SCBPTR), 615 ahc_index_busy_tcl(ahc, 616 BUILD_TCL(ahc_inb(ahc, SAVED_SCSIID), 617 ahc_inb(ahc, SAVED_LUN))), 618 ahc_inb(ahc, SINDEX)); 619 printf("SCSIID == 0x%x, SCB_SCSIID == 0x%x, SCB_LUN == 0x%x, " 620 "SCB_TAG == 0x%x, SCB_CONTROL == 0x%x\n", 621 ahc_inb(ahc, SCSIID), ahc_inb(ahc, SCB_SCSIID), 622 ahc_inb(ahc, SCB_LUN), ahc_inb(ahc, SCB_TAG), 623 ahc_inb(ahc, SCB_CONTROL)); 624 printf("SCSIBUSL == 0x%x, SCSISIGI == 0x%x\n", 625 ahc_inb(ahc, SCSIBUSL), ahc_inb(ahc, SCSISIGI)); 626 printf("SXFRCTL0 == 0x%x\n", ahc_inb(ahc, SXFRCTL0)); 627 printf("SEQCTL == 0x%x\n", ahc_inb(ahc, SEQCTL)); 628 ahc_dump_card_state(ahc); 629 ahc->msgout_buf[0] = MSG_BUS_DEV_RESET; 630 ahc->msgout_len = 1; 631 ahc->msgout_index = 0; 632 ahc->msg_type = MSG_TYPE_INITIATOR_MSGOUT; 633 ahc_outb(ahc, MSG_OUT, HOST_MSG); 634 ahc_assert_atn(ahc); 635 break; 636 } 637 case SEND_REJECT: 638 { 639 u_int rejbyte = ahc_inb(ahc, ACCUM); 640 printf("%s:%c:%d: Warning - unknown message received from " 641 "target (0x%x). Rejecting\n", 642 ahc_name(ahc), devinfo.channel, devinfo.target, rejbyte); 643 break; 644 } 645 case PROTO_VIOLATION: 646 { 647 ahc_handle_proto_violation(ahc); 648 break; 649 } 650 case IGN_WIDE_RES: 651 ahc_handle_ign_wide_residue(ahc, &devinfo); 652 break; 653 case PDATA_REINIT: 654 ahc_reinitialize_dataptrs(ahc); 655 break; 656 case BAD_PHASE: 657 { 658 u_int lastphase; 659 660 lastphase = ahc_inb(ahc, LASTPHASE); 661 printf("%s:%c:%d: unknown scsi bus phase %x, " 662 "lastphase = 0x%x. Attempting to continue\n", 663 ahc_name(ahc), devinfo.channel, devinfo.target, 664 lastphase, ahc_inb(ahc, SCSISIGI)); 665 break; 666 } 667 case MISSED_BUSFREE: 668 { 669 u_int lastphase; 670 671 lastphase = ahc_inb(ahc, LASTPHASE); 672 printf("%s:%c:%d: Missed busfree. " 673 "Lastphase = 0x%x, Curphase = 0x%x\n", 674 ahc_name(ahc), devinfo.channel, devinfo.target, 675 lastphase, ahc_inb(ahc, SCSISIGI)); 676 ahc_restart(ahc); 677 return; 678 } 679 case HOST_MSG_LOOP: 680 { 681 /* 682 * The sequencer has encountered a message phase 683 * that requires host assistance for completion. 684 * While handling the message phase(s), we will be 685 * notified by the sequencer after each byte is 686 * transferred so we can track bus phase changes. 687 * 688 * If this is the first time we've seen a HOST_MSG_LOOP 689 * interrupt, initialize the state of the host message 690 * loop. 691 */ 692 if (ahc->msg_type == MSG_TYPE_NONE) { 693 struct scb *scb; 694 u_int scb_index; 695 u_int bus_phase; 696 697 bus_phase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK; 698 if (bus_phase != P_MESGIN 699 && bus_phase != P_MESGOUT) { 700 printf("ahc_intr: HOST_MSG_LOOP bad " 701 "phase 0x%x\n", 702 bus_phase); 703 /* 704 * Probably transitioned to bus free before 705 * we got here. Just punt the message. 706 */ 707 ahc_clear_intstat(ahc); 708 ahc_restart(ahc); 709 return; 710 } 711 712 scb_index = ahc_inb(ahc, SCB_TAG); 713 scb = ahc_lookup_scb(ahc, scb_index); 714 if (devinfo.role == ROLE_INITIATOR) { 715 if (scb == NULL) 716 panic("HOST_MSG_LOOP with " 717 "invalid SCB %x\n", scb_index); 718 719 if (bus_phase == P_MESGOUT) 720 ahc_setup_initiator_msgout(ahc, 721 &devinfo, 722 scb); 723 else { 724 ahc->msg_type = 725 MSG_TYPE_INITIATOR_MSGIN; 726 ahc->msgin_index = 0; 727 } 728 } 729 #ifdef AHC_TARGET_MODE 730 else { 731 if (bus_phase == P_MESGOUT) { 732 ahc->msg_type = 733 MSG_TYPE_TARGET_MSGOUT; 734 ahc->msgin_index = 0; 735 } 736 else 737 ahc_setup_target_msgin(ahc, 738 &devinfo, 739 scb); 740 } 741 #endif 742 } 743 744 ahc_handle_message_phase(ahc); 745 break; 746 } 747 case PERR_DETECTED: 748 { 749 /* 750 * If we've cleared the parity error interrupt 751 * but the sequencer still believes that SCSIPERR 752 * is true, it must be that the parity error is 753 * for the currently presented byte on the bus, 754 * and we are not in a phase (data-in) where we will 755 * eventually ack this byte. Ack the byte and 756 * throw it away in the hope that the target will 757 * take us to message out to deliver the appropriate 758 * error message. 759 */ 760 if ((intstat & SCSIINT) == 0 761 && (ahc_inb(ahc, SSTAT1) & SCSIPERR) != 0) { 762 if ((ahc->features & AHC_DT) == 0) { 763 u_int curphase; 764 765 /* 766 * The hardware will only let you ack bytes 767 * if the expected phase in SCSISIGO matches 768 * the current phase. Make sure this is 769 * currently the case. 770 */ 771 curphase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK; 772 ahc_outb(ahc, LASTPHASE, curphase); 773 ahc_outb(ahc, SCSISIGO, curphase); 774 } 775 if ((ahc_inb(ahc, SCSISIGI) & (CDI|MSGI)) == 0) { 776 int wait; 777 778 /* 779 * In a data phase. Faster to bitbucket 780 * the data than to individually ack each 781 * byte. This is also the only strategy 782 * that will work with AUTOACK enabled. 783 */ 784 ahc_outb(ahc, SXFRCTL1, 785 ahc_inb(ahc, SXFRCTL1) | BITBUCKET); 786 wait = 5000; 787 while (--wait != 0) { 788 if ((ahc_inb(ahc, SCSISIGI) 789 & (CDI|MSGI)) != 0) 790 break; 791 aic_delay(100); 792 } 793 ahc_outb(ahc, SXFRCTL1, 794 ahc_inb(ahc, SXFRCTL1) & ~BITBUCKET); 795 if (wait == 0) { 796 struct scb *scb; 797 u_int scb_index; 798 799 ahc_print_devinfo(ahc, &devinfo); 800 printf("Unable to clear parity error. " 801 "Resetting bus.\n"); 802 scb_index = ahc_inb(ahc, SCB_TAG); 803 scb = ahc_lookup_scb(ahc, scb_index); 804 if (scb != NULL) 805 aic_set_transaction_status(scb, 806 CAM_UNCOR_PARITY); 807 ahc_reset_channel(ahc, devinfo.channel, 808 /*init reset*/TRUE); 809 } 810 } else { 811 ahc_inb(ahc, SCSIDATL); 812 } 813 } 814 break; 815 } 816 case DATA_OVERRUN: 817 { 818 /* 819 * When the sequencer detects an overrun, it 820 * places the controller in "BITBUCKET" mode 821 * and allows the target to complete its transfer. 822 * Unfortunately, none of the counters get updated 823 * when the controller is in this mode, so we have 824 * no way of knowing how large the overrun was. 825 */ 826 u_int scbindex = ahc_inb(ahc, SCB_TAG); 827 u_int lastphase = ahc_inb(ahc, LASTPHASE); 828 u_int i; 829 830 scb = ahc_lookup_scb(ahc, scbindex); 831 for (i = 0; i < num_phases; i++) { 832 if (lastphase == ahc_phase_table[i].phase) 833 break; 834 } 835 ahc_print_path(ahc, scb); 836 printf("data overrun detected %s." 837 " Tag == 0x%x.\n", 838 ahc_phase_table[i].phasemsg, 839 scb->hscb->tag); 840 ahc_print_path(ahc, scb); 841 printf("%s seen Data Phase. Length = %ld. NumSGs = %d.\n", 842 ahc_inb(ahc, SEQ_FLAGS) & DPHASE ? "Have" : "Haven't", 843 aic_get_transfer_length(scb), scb->sg_count); 844 if (scb->sg_count > 0) { 845 for (i = 0; i < scb->sg_count; i++) { 846 printf("sg[%d] - Addr 0x%x%x : Length %d\n", 847 i, 848 (aic_le32toh(scb->sg_list[i].len) >> 24 849 & SG_HIGH_ADDR_BITS), 850 aic_le32toh(scb->sg_list[i].addr), 851 aic_le32toh(scb->sg_list[i].len) 852 & AHC_SG_LEN_MASK); 853 } 854 } 855 /* 856 * Set this and it will take effect when the 857 * target does a command complete. 858 */ 859 ahc_freeze_devq(ahc, scb); 860 if ((scb->flags & SCB_SENSE) == 0) { 861 aic_set_transaction_status(scb, CAM_DATA_RUN_ERR); 862 } else { 863 scb->flags &= ~SCB_SENSE; 864 aic_set_transaction_status(scb, CAM_AUTOSENSE_FAIL); 865 } 866 aic_freeze_scb(scb); 867 868 if ((ahc->features & AHC_ULTRA2) != 0) { 869 /* 870 * Clear the channel in case we return 871 * to data phase later. 872 */ 873 ahc_outb(ahc, SXFRCTL0, 874 ahc_inb(ahc, SXFRCTL0) | CLRSTCNT|CLRCHN); 875 ahc_outb(ahc, SXFRCTL0, 876 ahc_inb(ahc, SXFRCTL0) | CLRSTCNT|CLRCHN); 877 } 878 if ((ahc->flags & AHC_39BIT_ADDRESSING) != 0) { 879 u_int dscommand1; 880 881 /* Ensure HHADDR is 0 for future DMA operations. */ 882 dscommand1 = ahc_inb(ahc, DSCOMMAND1); 883 ahc_outb(ahc, DSCOMMAND1, dscommand1 | HADDLDSEL0); 884 ahc_outb(ahc, HADDR, 0); 885 ahc_outb(ahc, DSCOMMAND1, dscommand1); 886 } 887 break; 888 } 889 case MKMSG_FAILED: 890 { 891 u_int scbindex; 892 893 printf("%s:%c:%d:%d: Attempt to issue message failed\n", 894 ahc_name(ahc), devinfo.channel, devinfo.target, 895 devinfo.lun); 896 scbindex = ahc_inb(ahc, SCB_TAG); 897 scb = ahc_lookup_scb(ahc, scbindex); 898 if (scb != NULL 899 && (scb->flags & SCB_RECOVERY_SCB) != 0) 900 /* 901 * Ensure that we didn't put a second instance of this 902 * SCB into the QINFIFO. 903 */ 904 ahc_search_qinfifo(ahc, SCB_GET_TARGET(ahc, scb), 905 SCB_GET_CHANNEL(ahc, scb), 906 SCB_GET_LUN(scb), scb->hscb->tag, 907 ROLE_INITIATOR, /*status*/0, 908 SEARCH_REMOVE); 909 break; 910 } 911 case NO_FREE_SCB: 912 { 913 printf("%s: No free or disconnected SCBs\n", ahc_name(ahc)); 914 ahc_dump_card_state(ahc); 915 panic("for safety"); 916 break; 917 } 918 case SCB_MISMATCH: 919 { 920 u_int scbptr; 921 922 scbptr = ahc_inb(ahc, SCBPTR); 923 printf("Bogus TAG after DMA. SCBPTR %d, tag %d, our tag %d\n", 924 scbptr, ahc_inb(ahc, ARG_1), 925 ahc->scb_data->hscbs[scbptr].tag); 926 ahc_dump_card_state(ahc); 927 panic("for safety"); 928 break; 929 } 930 case OUT_OF_RANGE: 931 { 932 printf("%s: BTT calculation out of range\n", ahc_name(ahc)); 933 printf("SAVED_SCSIID == 0x%x, SAVED_LUN == 0x%x, " 934 "ARG_1 == 0x%x ACCUM = 0x%x\n", 935 ahc_inb(ahc, SAVED_SCSIID), ahc_inb(ahc, SAVED_LUN), 936 ahc_inb(ahc, ARG_1), ahc_inb(ahc, ACCUM)); 937 printf("SEQ_FLAGS == 0x%x, SCBPTR == 0x%x, BTT == 0x%x, " 938 "SINDEX == 0x%x\n, A == 0x%x\n", 939 ahc_inb(ahc, SEQ_FLAGS), ahc_inb(ahc, SCBPTR), 940 ahc_index_busy_tcl(ahc, 941 BUILD_TCL(ahc_inb(ahc, SAVED_SCSIID), 942 ahc_inb(ahc, SAVED_LUN))), 943 ahc_inb(ahc, SINDEX), 944 ahc_inb(ahc, ACCUM)); 945 printf("SCSIID == 0x%x, SCB_SCSIID == 0x%x, SCB_LUN == 0x%x, " 946 "SCB_TAG == 0x%x, SCB_CONTROL == 0x%x\n", 947 ahc_inb(ahc, SCSIID), ahc_inb(ahc, SCB_SCSIID), 948 ahc_inb(ahc, SCB_LUN), ahc_inb(ahc, SCB_TAG), 949 ahc_inb(ahc, SCB_CONTROL)); 950 printf("SCSIBUSL == 0x%x, SCSISIGI == 0x%x\n", 951 ahc_inb(ahc, SCSIBUSL), ahc_inb(ahc, SCSISIGI)); 952 ahc_dump_card_state(ahc); 953 panic("for safety"); 954 break; 955 } 956 default: 957 printf("ahc_intr: seqint, " 958 "intstat == 0x%x, scsisigi = 0x%x\n", 959 intstat, ahc_inb(ahc, SCSISIGI)); 960 break; 961 } 962 unpause: 963 /* 964 * The sequencer is paused immediately on 965 * a SEQINT, so we should restart it when 966 * we're done. 967 */ 968 ahc_unpause(ahc); 969 } 970 971 void 972 ahc_handle_scsiint(struct ahc_softc *ahc, u_int intstat) 973 { 974 u_int scb_index; 975 u_int status0; 976 u_int status; 977 struct scb *scb; 978 char cur_channel; 979 char intr_channel; 980 981 if ((ahc->features & AHC_TWIN) != 0 982 && ((ahc_inb(ahc, SBLKCTL) & SELBUSB) != 0)) 983 cur_channel = 'B'; 984 else 985 cur_channel = 'A'; 986 intr_channel = cur_channel; 987 988 if ((ahc->features & AHC_ULTRA2) != 0) 989 status0 = ahc_inb(ahc, SSTAT0) & IOERR; 990 else 991 status0 = 0; 992 status = ahc_inb(ahc, SSTAT1) & (SELTO|SCSIRSTI|BUSFREE|SCSIPERR); 993 if (status == 0 && status0 == 0) { 994 if ((ahc->features & AHC_TWIN) != 0) { 995 /* Try the other channel */ 996 ahc_outb(ahc, SBLKCTL, ahc_inb(ahc, SBLKCTL) ^ SELBUSB); 997 status = ahc_inb(ahc, SSTAT1) 998 & (SELTO|SCSIRSTI|BUSFREE|SCSIPERR); 999 intr_channel = (cur_channel == 'A') ? 'B' : 'A'; 1000 } 1001 if (status == 0) { 1002 printf("%s: Spurious SCSI interrupt\n", ahc_name(ahc)); 1003 ahc_outb(ahc, CLRINT, CLRSCSIINT); 1004 ahc_unpause(ahc); 1005 return; 1006 } 1007 } 1008 1009 /* Make sure the sequencer is in a safe location. */ 1010 ahc_clear_critical_section(ahc); 1011 1012 scb_index = ahc_inb(ahc, SCB_TAG); 1013 scb = ahc_lookup_scb(ahc, scb_index); 1014 if (scb != NULL 1015 && (ahc_inb(ahc, SEQ_FLAGS) & NOT_IDENTIFIED) != 0) 1016 scb = NULL; 1017 1018 if ((ahc->features & AHC_ULTRA2) != 0 1019 && (status0 & IOERR) != 0) { 1020 int now_lvd; 1021 1022 now_lvd = ahc_inb(ahc, SBLKCTL) & ENAB40; 1023 printf("%s: Transceiver State Has Changed to %s mode\n", 1024 ahc_name(ahc), now_lvd ? "LVD" : "SE"); 1025 ahc_outb(ahc, CLRSINT0, CLRIOERR); 1026 /* 1027 * When transitioning to SE mode, the reset line 1028 * glitches, triggering an arbitration bug in some 1029 * Ultra2 controllers. This bug is cleared when we 1030 * assert the reset line. Since a reset glitch has 1031 * already occurred with this transition and a 1032 * transceiver state change is handled just like 1033 * a bus reset anyway, asserting the reset line 1034 * ourselves is safe. 1035 */ 1036 ahc_reset_channel(ahc, intr_channel, 1037 /*Initiate Reset*/now_lvd == 0); 1038 } else if ((status & SCSIRSTI) != 0) { 1039 printf("%s: Someone reset channel %c\n", 1040 ahc_name(ahc), intr_channel); 1041 if (intr_channel != cur_channel) 1042 ahc_outb(ahc, SBLKCTL, ahc_inb(ahc, SBLKCTL) ^ SELBUSB); 1043 ahc_reset_channel(ahc, intr_channel, /*Initiate Reset*/FALSE); 1044 } else if ((status & SCSIPERR) != 0) { 1045 /* 1046 * Determine the bus phase and queue an appropriate message. 1047 * SCSIPERR is latched true as soon as a parity error 1048 * occurs. If the sequencer acked the transfer that 1049 * caused the parity error and the currently presented 1050 * transfer on the bus has correct parity, SCSIPERR will 1051 * be cleared by CLRSCSIPERR. Use this to determine if 1052 * we should look at the last phase the sequencer recorded, 1053 * or the current phase presented on the bus. 1054 */ 1055 struct ahc_devinfo devinfo; 1056 u_int mesg_out; 1057 u_int curphase; 1058 u_int errorphase; 1059 u_int lastphase; 1060 u_int scsirate; 1061 u_int i; 1062 u_int sstat2; 1063 int silent; 1064 1065 lastphase = ahc_inb(ahc, LASTPHASE); 1066 curphase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK; 1067 sstat2 = ahc_inb(ahc, SSTAT2); 1068 ahc_outb(ahc, CLRSINT1, CLRSCSIPERR); 1069 /* 1070 * For all phases save DATA, the sequencer won't 1071 * automatically ack a byte that has a parity error 1072 * in it. So the only way that the current phase 1073 * could be 'data-in' is if the parity error is for 1074 * an already acked byte in the data phase. During 1075 * synchronous data-in transfers, we may actually 1076 * ack bytes before latching the current phase in 1077 * LASTPHASE, leading to the discrepancy between 1078 * curphase and lastphase. 1079 */ 1080 if ((ahc_inb(ahc, SSTAT1) & SCSIPERR) != 0 1081 || curphase == P_DATAIN || curphase == P_DATAIN_DT) 1082 errorphase = curphase; 1083 else 1084 errorphase = lastphase; 1085 1086 for (i = 0; i < num_phases; i++) { 1087 if (errorphase == ahc_phase_table[i].phase) 1088 break; 1089 } 1090 mesg_out = ahc_phase_table[i].mesg_out; 1091 silent = FALSE; 1092 if (scb != NULL) { 1093 if (SCB_IS_SILENT(scb)) 1094 silent = TRUE; 1095 else 1096 ahc_print_path(ahc, scb); 1097 scb->flags |= SCB_TRANSMISSION_ERROR; 1098 } else 1099 printf("%s:%c:%d: ", ahc_name(ahc), intr_channel, 1100 SCSIID_TARGET(ahc, ahc_inb(ahc, SAVED_SCSIID))); 1101 scsirate = ahc_inb(ahc, SCSIRATE); 1102 if (silent == FALSE) { 1103 printf("parity error detected %s. " 1104 "SEQADDR(0x%x) SCSIRATE(0x%x)\n", 1105 ahc_phase_table[i].phasemsg, 1106 ahc_inw(ahc, SEQADDR0), 1107 scsirate); 1108 if ((ahc->features & AHC_DT) != 0) { 1109 if ((sstat2 & CRCVALERR) != 0) 1110 printf("\tCRC Value Mismatch\n"); 1111 if ((sstat2 & CRCENDERR) != 0) 1112 printf("\tNo terminal CRC packet " 1113 "recevied\n"); 1114 if ((sstat2 & CRCREQERR) != 0) 1115 printf("\tIllegal CRC packet " 1116 "request\n"); 1117 if ((sstat2 & DUAL_EDGE_ERR) != 0) 1118 printf("\tUnexpected %sDT Data Phase\n", 1119 (scsirate & SINGLE_EDGE) 1120 ? "" : "non-"); 1121 } 1122 } 1123 1124 if ((ahc->features & AHC_DT) != 0 1125 && (sstat2 & DUAL_EDGE_ERR) != 0) { 1126 /* 1127 * This error applies regardless of 1128 * data direction, so ignore the value 1129 * in the phase table. 1130 */ 1131 mesg_out = MSG_INITIATOR_DET_ERR; 1132 } 1133 1134 /* 1135 * We've set the hardware to assert ATN if we 1136 * get a parity error on "in" phases, so all we 1137 * need to do is stuff the message buffer with 1138 * the appropriate message. "In" phases have set 1139 * mesg_out to something other than MSG_NOP. 1140 */ 1141 if (mesg_out != MSG_NOOP) { 1142 if (ahc->msg_type != MSG_TYPE_NONE) 1143 ahc->send_msg_perror = TRUE; 1144 else 1145 ahc_outb(ahc, MSG_OUT, mesg_out); 1146 } 1147 /* 1148 * Force a renegotiation with this target just in 1149 * case we are out of sync for some external reason 1150 * unknown (or unreported) by the target. 1151 */ 1152 ahc_fetch_devinfo(ahc, &devinfo); 1153 ahc_force_renegotiation(ahc, &devinfo); 1154 1155 ahc_outb(ahc, CLRINT, CLRSCSIINT); 1156 ahc_unpause(ahc); 1157 } else if ((status & SELTO) != 0) { 1158 u_int scbptr; 1159 1160 /* Stop the selection */ 1161 ahc_outb(ahc, SCSISEQ, 0); 1162 1163 /* No more pending messages */ 1164 ahc_clear_msg_state(ahc); 1165 1166 /* Clear interrupt state */ 1167 ahc_outb(ahc, SIMODE1, ahc_inb(ahc, SIMODE1) & ~ENBUSFREE); 1168 ahc_outb(ahc, CLRSINT1, CLRSELTIMEO|CLRBUSFREE|CLRSCSIPERR); 1169 1170 /* 1171 * Although the driver does not care about the 1172 * 'Selection in Progress' status bit, the busy 1173 * LED does. SELINGO is only cleared by a successful 1174 * selection, so we must manually clear it to insure 1175 * the LED turns off just incase no future successful 1176 * selections occur (e.g. no devices on the bus). 1177 */ 1178 ahc_outb(ahc, CLRSINT0, CLRSELINGO); 1179 1180 scbptr = ahc_inb(ahc, WAITING_SCBH); 1181 ahc_outb(ahc, SCBPTR, scbptr); 1182 scb_index = ahc_inb(ahc, SCB_TAG); 1183 1184 scb = ahc_lookup_scb(ahc, scb_index); 1185 if (scb == NULL) { 1186 printf("%s: ahc_intr - referenced scb not " 1187 "valid during SELTO scb(%d, %d)\n", 1188 ahc_name(ahc), scbptr, scb_index); 1189 ahc_dump_card_state(ahc); 1190 } else { 1191 struct ahc_devinfo devinfo; 1192 #ifdef AHC_DEBUG 1193 if ((ahc_debug & AHC_SHOW_SELTO) != 0) { 1194 ahc_print_path(ahc, scb); 1195 printf("Saw Selection Timeout for SCB 0x%x\n", 1196 scb_index); 1197 } 1198 #endif 1199 ahc_scb_devinfo(ahc, &devinfo, scb); 1200 aic_set_transaction_status(scb, CAM_SEL_TIMEOUT); 1201 ahc_freeze_devq(ahc, scb); 1202 1203 /* 1204 * Cancel any pending transactions on the device 1205 * now that it seems to be missing. This will 1206 * also revert us to async/narrow transfers until 1207 * we can renegotiate with the device. 1208 */ 1209 ahc_handle_devreset(ahc, &devinfo, 1210 CAM_SEL_TIMEOUT, 1211 "Selection Timeout", 1212 /*verbose_level*/1); 1213 } 1214 ahc_outb(ahc, CLRINT, CLRSCSIINT); 1215 ahc_restart(ahc); 1216 } else if ((status & BUSFREE) != 0 1217 && (ahc_inb(ahc, SIMODE1) & ENBUSFREE) != 0) { 1218 struct ahc_devinfo devinfo; 1219 u_int lastphase; 1220 u_int saved_scsiid; 1221 u_int saved_lun; 1222 u_int target; 1223 u_int initiator_role_id; 1224 char channel; 1225 int printerror; 1226 1227 /* 1228 * Clear our selection hardware as soon as possible. 1229 * We may have an entry in the waiting Q for this target, 1230 * that is affected by this busfree and we don't want to 1231 * go about selecting the target while we handle the event. 1232 */ 1233 ahc_outb(ahc, SCSISEQ, 1234 ahc_inb(ahc, SCSISEQ) & (ENSELI|ENRSELI|ENAUTOATNP)); 1235 1236 /* 1237 * Disable busfree interrupts and clear the busfree 1238 * interrupt status. We do this here so that several 1239 * bus transactions occur prior to clearing the SCSIINT 1240 * latch. It can take a bit for the clearing to take effect. 1241 */ 1242 ahc_outb(ahc, SIMODE1, ahc_inb(ahc, SIMODE1) & ~ENBUSFREE); 1243 ahc_outb(ahc, CLRSINT1, CLRBUSFREE|CLRSCSIPERR); 1244 1245 /* 1246 * Look at what phase we were last in. 1247 * If its message out, chances are pretty good 1248 * that the busfree was in response to one of 1249 * our abort requests. 1250 */ 1251 lastphase = ahc_inb(ahc, LASTPHASE); 1252 saved_scsiid = ahc_inb(ahc, SAVED_SCSIID); 1253 saved_lun = ahc_inb(ahc, SAVED_LUN); 1254 target = SCSIID_TARGET(ahc, saved_scsiid); 1255 initiator_role_id = SCSIID_OUR_ID(saved_scsiid); 1256 channel = SCSIID_CHANNEL(ahc, saved_scsiid); 1257 ahc_compile_devinfo(&devinfo, initiator_role_id, 1258 target, saved_lun, channel, ROLE_INITIATOR); 1259 printerror = 1; 1260 1261 if (lastphase == P_MESGOUT) { 1262 u_int tag; 1263 1264 tag = SCB_LIST_NULL; 1265 if (ahc_sent_msg(ahc, AHCMSG_1B, MSG_ABORT_TAG, TRUE) 1266 || ahc_sent_msg(ahc, AHCMSG_1B, MSG_ABORT, TRUE)) { 1267 if (ahc->msgout_buf[ahc->msgout_index - 1] 1268 == MSG_ABORT_TAG) 1269 tag = scb->hscb->tag; 1270 ahc_print_path(ahc, scb); 1271 printf("SCB %d - Abort%s Completed.\n", 1272 scb->hscb->tag, tag == SCB_LIST_NULL ? 1273 "" : " Tag"); 1274 ahc_abort_scbs(ahc, target, channel, 1275 saved_lun, tag, 1276 ROLE_INITIATOR, 1277 CAM_REQ_ABORTED); 1278 printerror = 0; 1279 } else if (ahc_sent_msg(ahc, AHCMSG_1B, 1280 MSG_BUS_DEV_RESET, TRUE)) { 1281 #ifdef __FreeBSD__ 1282 /* 1283 * Don't mark the user's request for this BDR 1284 * as completing with CAM_BDR_SENT. CAM3 1285 * specifies CAM_REQ_CMP. 1286 */ 1287 if (scb != NULL 1288 && scb->io_ctx->ccb_h.func_code== XPT_RESET_DEV 1289 && ahc_match_scb(ahc, scb, target, channel, 1290 CAM_LUN_WILDCARD, 1291 SCB_LIST_NULL, 1292 ROLE_INITIATOR)) { 1293 aic_set_transaction_status(scb, CAM_REQ_CMP); 1294 } 1295 #endif 1296 ahc_compile_devinfo(&devinfo, 1297 initiator_role_id, 1298 target, 1299 CAM_LUN_WILDCARD, 1300 channel, 1301 ROLE_INITIATOR); 1302 ahc_handle_devreset(ahc, &devinfo, 1303 CAM_BDR_SENT, 1304 "Bus Device Reset", 1305 /*verbose_level*/0); 1306 printerror = 0; 1307 } else if (ahc_sent_msg(ahc, AHCMSG_EXT, 1308 MSG_EXT_PPR, FALSE)) { 1309 struct ahc_initiator_tinfo *tinfo; 1310 struct ahc_tmode_tstate *tstate; 1311 1312 /* 1313 * PPR Rejected. Try non-ppr negotiation 1314 * and retry command. 1315 */ 1316 tinfo = ahc_fetch_transinfo(ahc, 1317 devinfo.channel, 1318 devinfo.our_scsiid, 1319 devinfo.target, 1320 &tstate); 1321 tinfo->curr.transport_version = 2; 1322 tinfo->goal.transport_version = 2; 1323 tinfo->goal.ppr_options = 0; 1324 ahc_qinfifo_requeue_tail(ahc, scb); 1325 printerror = 0; 1326 } else if (ahc_sent_msg(ahc, AHCMSG_EXT, 1327 MSG_EXT_WDTR, FALSE)) { 1328 /* 1329 * Negotiation Rejected. Go-narrow and 1330 * retry command. 1331 */ 1332 ahc_set_width(ahc, &devinfo, 1333 MSG_EXT_WDTR_BUS_8_BIT, 1334 AHC_TRANS_CUR|AHC_TRANS_GOAL, 1335 /*paused*/TRUE); 1336 ahc_qinfifo_requeue_tail(ahc, scb); 1337 printerror = 0; 1338 } else if (ahc_sent_msg(ahc, AHCMSG_EXT, 1339 MSG_EXT_SDTR, FALSE)) { 1340 /* 1341 * Negotiation Rejected. Go-async and 1342 * retry command. 1343 */ 1344 ahc_set_syncrate(ahc, &devinfo, 1345 /*syncrate*/NULL, 1346 /*period*/0, /*offset*/0, 1347 /*ppr_options*/0, 1348 AHC_TRANS_CUR|AHC_TRANS_GOAL, 1349 /*paused*/TRUE); 1350 ahc_qinfifo_requeue_tail(ahc, scb); 1351 printerror = 0; 1352 } 1353 } 1354 if (printerror != 0) { 1355 u_int i; 1356 1357 if (scb != NULL) { 1358 u_int tag; 1359 1360 if ((scb->hscb->control & TAG_ENB) != 0) 1361 tag = scb->hscb->tag; 1362 else 1363 tag = SCB_LIST_NULL; 1364 ahc_print_path(ahc, scb); 1365 ahc_abort_scbs(ahc, target, channel, 1366 SCB_GET_LUN(scb), tag, 1367 ROLE_INITIATOR, 1368 CAM_UNEXP_BUSFREE); 1369 } else { 1370 /* 1371 * We had not fully identified this connection, 1372 * so we cannot abort anything. 1373 */ 1374 printf("%s: ", ahc_name(ahc)); 1375 } 1376 for (i = 0; i < num_phases; i++) { 1377 if (lastphase == ahc_phase_table[i].phase) 1378 break; 1379 } 1380 if (lastphase != P_BUSFREE) { 1381 /* 1382 * Renegotiate with this device at the 1383 * next opportunity just in case this busfree 1384 * is due to a negotiation mismatch with the 1385 * device. 1386 */ 1387 ahc_force_renegotiation(ahc, &devinfo); 1388 } 1389 printf("Unexpected busfree %s\n" 1390 "SEQADDR == 0x%x\n", 1391 ahc_phase_table[i].phasemsg, 1392 ahc_inb(ahc, SEQADDR0) 1393 | (ahc_inb(ahc, SEQADDR1) << 8)); 1394 } 1395 ahc_outb(ahc, CLRINT, CLRSCSIINT); 1396 ahc_restart(ahc); 1397 } else { 1398 printf("%s: Missing case in ahc_handle_scsiint. status = %x\n", 1399 ahc_name(ahc), status); 1400 ahc_outb(ahc, CLRINT, CLRSCSIINT); 1401 } 1402 } 1403 1404 /* 1405 * Force renegotiation to occur the next time we initiate 1406 * a command to the current device. 1407 */ 1408 static void 1409 ahc_force_renegotiation(struct ahc_softc *ahc, struct ahc_devinfo *devinfo) 1410 { 1411 struct ahc_initiator_tinfo *targ_info; 1412 struct ahc_tmode_tstate *tstate; 1413 1414 targ_info = ahc_fetch_transinfo(ahc, 1415 devinfo->channel, 1416 devinfo->our_scsiid, 1417 devinfo->target, 1418 &tstate); 1419 ahc_update_neg_request(ahc, devinfo, tstate, 1420 targ_info, AHC_NEG_IF_NON_ASYNC); 1421 } 1422 1423 #define AHC_MAX_STEPS 2000 1424 void 1425 ahc_clear_critical_section(struct ahc_softc *ahc) 1426 { 1427 int stepping; 1428 int steps; 1429 u_int simode0; 1430 u_int simode1; 1431 1432 if (ahc->num_critical_sections == 0) 1433 return; 1434 1435 stepping = FALSE; 1436 steps = 0; 1437 simode0 = 0; 1438 simode1 = 0; 1439 for (;;) { 1440 struct cs *cs; 1441 u_int seqaddr; 1442 u_int i; 1443 1444 seqaddr = ahc_inb(ahc, SEQADDR0) 1445 | (ahc_inb(ahc, SEQADDR1) << 8); 1446 1447 /* 1448 * Seqaddr represents the next instruction to execute, 1449 * so we are really executing the instruction just 1450 * before it. 1451 */ 1452 cs = ahc->critical_sections; 1453 for (i = 0; i < ahc->num_critical_sections; i++, cs++) { 1454 1455 if (cs->begin < seqaddr && cs->end >= seqaddr) 1456 break; 1457 } 1458 1459 if (i == ahc->num_critical_sections) 1460 break; 1461 1462 if (steps > AHC_MAX_STEPS) { 1463 printf("%s: Infinite loop in critical section\n", 1464 ahc_name(ahc)); 1465 ahc_dump_card_state(ahc); 1466 panic("critical section loop"); 1467 } 1468 1469 steps++; 1470 if (stepping == FALSE) { 1471 /* 1472 * Disable all interrupt sources so that the 1473 * sequencer will not be stuck by a pausing 1474 * interrupt condition while we attempt to 1475 * leave a critical section. 1476 */ 1477 simode0 = ahc_inb(ahc, SIMODE0); 1478 ahc_outb(ahc, SIMODE0, 0); 1479 simode1 = ahc_inb(ahc, SIMODE1); 1480 if ((ahc->features & AHC_DT) != 0) 1481 /* 1482 * On DT class controllers, we 1483 * use the enhanced busfree logic. 1484 * Unfortunately we cannot re-enable 1485 * busfree detection within the 1486 * current connection, so we must 1487 * leave it on while single stepping. 1488 */ 1489 ahc_outb(ahc, SIMODE1, simode1 & ENBUSFREE); 1490 else 1491 ahc_outb(ahc, SIMODE1, 0); 1492 ahc_outb(ahc, CLRINT, CLRSCSIINT); 1493 ahc_outb(ahc, SEQCTL, ahc->seqctl | STEP); 1494 stepping = TRUE; 1495 } 1496 if ((ahc->features & AHC_DT) != 0) { 1497 ahc_outb(ahc, CLRSINT1, CLRBUSFREE); 1498 ahc_outb(ahc, CLRINT, CLRSCSIINT); 1499 } 1500 ahc_outb(ahc, HCNTRL, ahc->unpause); 1501 while (!ahc_is_paused(ahc)) 1502 aic_delay(200); 1503 } 1504 if (stepping) { 1505 ahc_outb(ahc, SIMODE0, simode0); 1506 ahc_outb(ahc, SIMODE1, simode1); 1507 ahc_outb(ahc, SEQCTL, ahc->seqctl); 1508 } 1509 } 1510 1511 /* 1512 * Clear any pending interrupt status. 1513 */ 1514 void 1515 ahc_clear_intstat(struct ahc_softc *ahc) 1516 { 1517 /* Clear any interrupt conditions this may have caused */ 1518 ahc_outb(ahc, CLRSINT1, CLRSELTIMEO|CLRATNO|CLRSCSIRSTI 1519 |CLRBUSFREE|CLRSCSIPERR|CLRPHASECHG| 1520 CLRREQINIT); 1521 ahc_flush_device_writes(ahc); 1522 ahc_outb(ahc, CLRSINT0, CLRSELDO|CLRSELDI|CLRSELINGO); 1523 ahc_flush_device_writes(ahc); 1524 ahc_outb(ahc, CLRINT, CLRSCSIINT); 1525 ahc_flush_device_writes(ahc); 1526 } 1527 1528 /**************************** Debugging Routines ******************************/ 1529 #ifdef AHC_DEBUG 1530 uint32_t ahc_debug = AHC_DEBUG_OPTS; 1531 #endif 1532 1533 void 1534 ahc_print_scb(struct scb *scb) 1535 { 1536 int i; 1537 1538 struct hardware_scb *hscb = scb->hscb; 1539 1540 printf("scb:%p control:0x%x scsiid:0x%x lun:%d cdb_len:%d\n", 1541 (void *)scb, 1542 hscb->control, 1543 hscb->scsiid, 1544 hscb->lun, 1545 hscb->cdb_len); 1546 printf("Shared Data: "); 1547 for (i = 0; i < sizeof(hscb->shared_data.cdb); i++) 1548 printf("%#02x", hscb->shared_data.cdb[i]); 1549 printf(" dataptr:%#x datacnt:%#x sgptr:%#x tag:%#x\n", 1550 aic_le32toh(hscb->dataptr), 1551 aic_le32toh(hscb->datacnt), 1552 aic_le32toh(hscb->sgptr), 1553 hscb->tag); 1554 if (scb->sg_count > 0) { 1555 for (i = 0; i < scb->sg_count; i++) { 1556 printf("sg[%d] - Addr 0x%x%x : Length %d\n", 1557 i, 1558 (aic_le32toh(scb->sg_list[i].len) >> 24 1559 & SG_HIGH_ADDR_BITS), 1560 aic_le32toh(scb->sg_list[i].addr), 1561 aic_le32toh(scb->sg_list[i].len)); 1562 } 1563 } 1564 } 1565 1566 /************************* Transfer Negotiation *******************************/ 1567 /* 1568 * Allocate per target mode instance (ID we respond to as a target) 1569 * transfer negotiation data structures. 1570 */ 1571 static struct ahc_tmode_tstate * 1572 ahc_alloc_tstate(struct ahc_softc *ahc, u_int scsi_id, char channel) 1573 { 1574 struct ahc_tmode_tstate *master_tstate; 1575 struct ahc_tmode_tstate *tstate; 1576 int i; 1577 1578 master_tstate = ahc->enabled_targets[ahc->our_id]; 1579 if (channel == 'B') { 1580 scsi_id += 8; 1581 master_tstate = ahc->enabled_targets[ahc->our_id_b + 8]; 1582 } 1583 if (ahc->enabled_targets[scsi_id] != NULL 1584 && ahc->enabled_targets[scsi_id] != master_tstate) 1585 panic("%s: ahc_alloc_tstate - Target already allocated", 1586 ahc_name(ahc)); 1587 tstate = (struct ahc_tmode_tstate*)malloc(sizeof(*tstate), 1588 M_DEVBUF, M_NOWAIT); 1589 if (tstate == NULL) 1590 return (NULL); 1591 1592 /* 1593 * If we have allocated a master tstate, copy user settings from 1594 * the master tstate (taken from SRAM or the EEPROM) for this 1595 * channel, but reset our current and goal settings to async/narrow 1596 * until an initiator talks to us. 1597 */ 1598 if (master_tstate != NULL) { 1599 memcpy(tstate, master_tstate, sizeof(*tstate)); 1600 memset(tstate->enabled_luns, 0, sizeof(tstate->enabled_luns)); 1601 tstate->ultraenb = 0; 1602 for (i = 0; i < AHC_NUM_TARGETS; i++) { 1603 memset(&tstate->transinfo[i].curr, 0, 1604 sizeof(tstate->transinfo[i].curr)); 1605 memset(&tstate->transinfo[i].goal, 0, 1606 sizeof(tstate->transinfo[i].goal)); 1607 } 1608 } else 1609 memset(tstate, 0, sizeof(*tstate)); 1610 ahc->enabled_targets[scsi_id] = tstate; 1611 return (tstate); 1612 } 1613 1614 #ifdef AHC_TARGET_MODE 1615 /* 1616 * Free per target mode instance (ID we respond to as a target) 1617 * transfer negotiation data structures. 1618 */ 1619 static void 1620 ahc_free_tstate(struct ahc_softc *ahc, u_int scsi_id, char channel, int force) 1621 { 1622 struct ahc_tmode_tstate *tstate; 1623 1624 /* 1625 * Don't clean up our "master" tstate. 1626 * It has our default user settings. 1627 */ 1628 if (((channel == 'B' && scsi_id == ahc->our_id_b) 1629 || (channel == 'A' && scsi_id == ahc->our_id)) 1630 && force == FALSE) 1631 return; 1632 1633 if (channel == 'B') 1634 scsi_id += 8; 1635 tstate = ahc->enabled_targets[scsi_id]; 1636 if (tstate != NULL) 1637 free(tstate, M_DEVBUF); 1638 ahc->enabled_targets[scsi_id] = NULL; 1639 } 1640 #endif 1641 1642 /* 1643 * Called when we have an active connection to a target on the bus, 1644 * this function finds the nearest syncrate to the input period limited 1645 * by the capabilities of the bus connectivity of and sync settings for 1646 * the target. 1647 */ 1648 struct ahc_syncrate * 1649 ahc_devlimited_syncrate(struct ahc_softc *ahc, 1650 struct ahc_initiator_tinfo *tinfo, 1651 u_int *period, u_int *ppr_options, role_t role) 1652 { 1653 struct ahc_transinfo *transinfo; 1654 u_int maxsync; 1655 1656 if ((ahc->features & AHC_ULTRA2) != 0) { 1657 if ((ahc_inb(ahc, SBLKCTL) & ENAB40) != 0 1658 && (ahc_inb(ahc, SSTAT2) & EXP_ACTIVE) == 0) { 1659 maxsync = AHC_SYNCRATE_DT; 1660 } else { 1661 maxsync = AHC_SYNCRATE_ULTRA; 1662 /* Can't do DT on an SE bus */ 1663 *ppr_options &= ~MSG_EXT_PPR_DT_REQ; 1664 } 1665 } else if ((ahc->features & AHC_ULTRA) != 0) { 1666 maxsync = AHC_SYNCRATE_ULTRA; 1667 } else { 1668 maxsync = AHC_SYNCRATE_FAST; 1669 } 1670 /* 1671 * Never allow a value higher than our current goal 1672 * period otherwise we may allow a target initiated 1673 * negotiation to go above the limit as set by the 1674 * user. In the case of an initiator initiated 1675 * sync negotiation, we limit based on the user 1676 * setting. This allows the system to still accept 1677 * incoming negotiations even if target initiated 1678 * negotiation is not performed. 1679 */ 1680 if (role == ROLE_TARGET) 1681 transinfo = &tinfo->user; 1682 else 1683 transinfo = &tinfo->goal; 1684 *ppr_options &= transinfo->ppr_options; 1685 if (transinfo->width == MSG_EXT_WDTR_BUS_8_BIT) { 1686 maxsync = MAX(maxsync, AHC_SYNCRATE_ULTRA2); 1687 *ppr_options &= ~MSG_EXT_PPR_DT_REQ; 1688 } 1689 if (transinfo->period == 0) { 1690 *period = 0; 1691 *ppr_options = 0; 1692 return (NULL); 1693 } 1694 *period = MAX(*period, transinfo->period); 1695 return (ahc_find_syncrate(ahc, period, ppr_options, maxsync)); 1696 } 1697 1698 /* 1699 * Look up the valid period to SCSIRATE conversion in our table. 1700 * Return the period and offset that should be sent to the target 1701 * if this was the beginning of an SDTR. 1702 */ 1703 struct ahc_syncrate * 1704 ahc_find_syncrate(struct ahc_softc *ahc, u_int *period, 1705 u_int *ppr_options, u_int maxsync) 1706 { 1707 struct ahc_syncrate *syncrate; 1708 1709 if ((ahc->features & AHC_DT) == 0) 1710 *ppr_options &= ~MSG_EXT_PPR_DT_REQ; 1711 1712 /* Skip all DT only entries if DT is not available */ 1713 if ((*ppr_options & MSG_EXT_PPR_DT_REQ) == 0 1714 && maxsync < AHC_SYNCRATE_ULTRA2) 1715 maxsync = AHC_SYNCRATE_ULTRA2; 1716 1717 for (syncrate = &ahc_syncrates[maxsync]; 1718 syncrate->rate != NULL; 1719 syncrate++) { 1720 /* 1721 * The Ultra2 table doesn't go as low 1722 * as for the Fast/Ultra cards. 1723 */ 1724 if ((ahc->features & AHC_ULTRA2) != 0 1725 && (syncrate->sxfr_u2 == 0)) 1726 break; 1727 1728 if (*period <= syncrate->period) { 1729 /* 1730 * When responding to a target that requests 1731 * sync, the requested rate may fall between 1732 * two rates that we can output, but still be 1733 * a rate that we can receive. Because of this, 1734 * we want to respond to the target with 1735 * the same rate that it sent to us even 1736 * if the period we use to send data to it 1737 * is lower. Only lower the response period 1738 * if we must. 1739 */ 1740 if (syncrate == &ahc_syncrates[maxsync]) 1741 *period = syncrate->period; 1742 1743 /* 1744 * At some speeds, we only support 1745 * ST transfers. 1746 */ 1747 if ((syncrate->sxfr_u2 & ST_SXFR) != 0) 1748 *ppr_options &= ~MSG_EXT_PPR_DT_REQ; 1749 break; 1750 } 1751 } 1752 1753 if ((*period == 0) 1754 || (syncrate->rate == NULL) 1755 || ((ahc->features & AHC_ULTRA2) != 0 1756 && (syncrate->sxfr_u2 == 0))) { 1757 /* Use asynchronous transfers. */ 1758 *period = 0; 1759 syncrate = NULL; 1760 *ppr_options &= ~MSG_EXT_PPR_DT_REQ; 1761 } 1762 return (syncrate); 1763 } 1764 1765 /* 1766 * Convert from an entry in our syncrate table to the SCSI equivalent 1767 * sync "period" factor. 1768 */ 1769 u_int 1770 ahc_find_period(struct ahc_softc *ahc, u_int scsirate, u_int maxsync) 1771 { 1772 struct ahc_syncrate *syncrate; 1773 1774 if ((ahc->features & AHC_ULTRA2) != 0) 1775 scsirate &= SXFR_ULTRA2; 1776 else 1777 scsirate &= SXFR; 1778 1779 syncrate = &ahc_syncrates[maxsync]; 1780 while (syncrate->rate != NULL) { 1781 if ((ahc->features & AHC_ULTRA2) != 0) { 1782 if (syncrate->sxfr_u2 == 0) 1783 break; 1784 else if (scsirate == (syncrate->sxfr_u2 & SXFR_ULTRA2)) 1785 return (syncrate->period); 1786 } else if (scsirate == (syncrate->sxfr & SXFR)) { 1787 return (syncrate->period); 1788 } 1789 syncrate++; 1790 } 1791 return (0); /* async */ 1792 } 1793 1794 /* 1795 * Truncate the given synchronous offset to a value the 1796 * current adapter type and syncrate are capable of. 1797 */ 1798 void 1799 ahc_validate_offset(struct ahc_softc *ahc, 1800 struct ahc_initiator_tinfo *tinfo, 1801 struct ahc_syncrate *syncrate, 1802 u_int *offset, int wide, role_t role) 1803 { 1804 u_int maxoffset; 1805 1806 /* Limit offset to what we can do */ 1807 if (syncrate == NULL) { 1808 maxoffset = 0; 1809 } else if ((ahc->features & AHC_ULTRA2) != 0) { 1810 maxoffset = MAX_OFFSET_ULTRA2; 1811 } else { 1812 if (wide) 1813 maxoffset = MAX_OFFSET_16BIT; 1814 else 1815 maxoffset = MAX_OFFSET_8BIT; 1816 } 1817 *offset = MIN(*offset, maxoffset); 1818 if (tinfo != NULL) { 1819 if (role == ROLE_TARGET) 1820 *offset = MIN(*offset, tinfo->user.offset); 1821 else 1822 *offset = MIN(*offset, tinfo->goal.offset); 1823 } 1824 } 1825 1826 /* 1827 * Truncate the given transfer width parameter to a value the 1828 * current adapter type is capable of. 1829 */ 1830 void 1831 ahc_validate_width(struct ahc_softc *ahc, struct ahc_initiator_tinfo *tinfo, 1832 u_int *bus_width, role_t role) 1833 { 1834 switch (*bus_width) { 1835 default: 1836 if (ahc->features & AHC_WIDE) { 1837 /* Respond Wide */ 1838 *bus_width = MSG_EXT_WDTR_BUS_16_BIT; 1839 break; 1840 } 1841 /* FALLTHROUGH */ 1842 case MSG_EXT_WDTR_BUS_8_BIT: 1843 *bus_width = MSG_EXT_WDTR_BUS_8_BIT; 1844 break; 1845 } 1846 if (tinfo != NULL) { 1847 if (role == ROLE_TARGET) 1848 *bus_width = MIN(tinfo->user.width, *bus_width); 1849 else 1850 *bus_width = MIN(tinfo->goal.width, *bus_width); 1851 } 1852 } 1853 1854 /* 1855 * Update the bitmask of targets for which the controller should 1856 * negotiate with at the next convenient opportunity. This currently 1857 * means the next time we send the initial identify messages for 1858 * a new transaction. 1859 */ 1860 int 1861 ahc_update_neg_request(struct ahc_softc *ahc, struct ahc_devinfo *devinfo, 1862 struct ahc_tmode_tstate *tstate, 1863 struct ahc_initiator_tinfo *tinfo, ahc_neg_type neg_type) 1864 { 1865 u_int auto_negotiate_orig; 1866 1867 auto_negotiate_orig = tstate->auto_negotiate; 1868 if (neg_type == AHC_NEG_ALWAYS) { 1869 /* 1870 * Force our "current" settings to be 1871 * unknown so that unless a bus reset 1872 * occurs the need to renegotiate is 1873 * recorded persistently. 1874 */ 1875 if ((ahc->features & AHC_WIDE) != 0) 1876 tinfo->curr.width = AHC_WIDTH_UNKNOWN; 1877 tinfo->curr.period = AHC_PERIOD_UNKNOWN; 1878 tinfo->curr.offset = AHC_OFFSET_UNKNOWN; 1879 } 1880 if (tinfo->curr.period != tinfo->goal.period 1881 || tinfo->curr.width != tinfo->goal.width 1882 || tinfo->curr.offset != tinfo->goal.offset 1883 || tinfo->curr.ppr_options != tinfo->goal.ppr_options 1884 || (neg_type == AHC_NEG_IF_NON_ASYNC 1885 && (tinfo->goal.offset != 0 1886 || tinfo->goal.width != MSG_EXT_WDTR_BUS_8_BIT 1887 || tinfo->goal.ppr_options != 0))) 1888 tstate->auto_negotiate |= devinfo->target_mask; 1889 else 1890 tstate->auto_negotiate &= ~devinfo->target_mask; 1891 1892 return (auto_negotiate_orig != tstate->auto_negotiate); 1893 } 1894 1895 /* 1896 * Update the user/goal/curr tables of synchronous negotiation 1897 * parameters as well as, in the case of a current or active update, 1898 * any data structures on the host controller. In the case of an 1899 * active update, the specified target is currently talking to us on 1900 * the bus, so the transfer parameter update must take effect 1901 * immediately. 1902 */ 1903 void 1904 ahc_set_syncrate(struct ahc_softc *ahc, struct ahc_devinfo *devinfo, 1905 struct ahc_syncrate *syncrate, u_int period, 1906 u_int offset, u_int ppr_options, u_int type, int paused) 1907 { 1908 struct ahc_initiator_tinfo *tinfo; 1909 struct ahc_tmode_tstate *tstate; 1910 u_int old_period; 1911 u_int old_offset; 1912 u_int old_ppr; 1913 int active; 1914 int update_needed; 1915 1916 active = (type & AHC_TRANS_ACTIVE) == AHC_TRANS_ACTIVE; 1917 update_needed = 0; 1918 1919 if (syncrate == NULL) { 1920 period = 0; 1921 offset = 0; 1922 } 1923 1924 tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid, 1925 devinfo->target, &tstate); 1926 1927 if ((type & AHC_TRANS_USER) != 0) { 1928 tinfo->user.period = period; 1929 tinfo->user.offset = offset; 1930 tinfo->user.ppr_options = ppr_options; 1931 } 1932 1933 if ((type & AHC_TRANS_GOAL) != 0) { 1934 tinfo->goal.period = period; 1935 tinfo->goal.offset = offset; 1936 tinfo->goal.ppr_options = ppr_options; 1937 } 1938 1939 old_period = tinfo->curr.period; 1940 old_offset = tinfo->curr.offset; 1941 old_ppr = tinfo->curr.ppr_options; 1942 1943 if ((type & AHC_TRANS_CUR) != 0 1944 && (old_period != period 1945 || old_offset != offset 1946 || old_ppr != ppr_options)) { 1947 u_int scsirate; 1948 1949 update_needed++; 1950 scsirate = tinfo->scsirate; 1951 if ((ahc->features & AHC_ULTRA2) != 0) { 1952 scsirate &= ~(SXFR_ULTRA2|SINGLE_EDGE|ENABLE_CRC); 1953 if (syncrate != NULL) { 1954 scsirate |= syncrate->sxfr_u2; 1955 if ((ppr_options & MSG_EXT_PPR_DT_REQ) != 0) 1956 scsirate |= ENABLE_CRC; 1957 else 1958 scsirate |= SINGLE_EDGE; 1959 } 1960 } else { 1961 scsirate &= ~(SXFR|SOFS); 1962 /* 1963 * Ensure Ultra mode is set properly for 1964 * this target. 1965 */ 1966 tstate->ultraenb &= ~devinfo->target_mask; 1967 if (syncrate != NULL) { 1968 if (syncrate->sxfr & ULTRA_SXFR) { 1969 tstate->ultraenb |= 1970 devinfo->target_mask; 1971 } 1972 scsirate |= syncrate->sxfr & SXFR; 1973 scsirate |= offset & SOFS; 1974 } 1975 if (active) { 1976 u_int sxfrctl0; 1977 1978 sxfrctl0 = ahc_inb(ahc, SXFRCTL0); 1979 sxfrctl0 &= ~FAST20; 1980 if (tstate->ultraenb & devinfo->target_mask) 1981 sxfrctl0 |= FAST20; 1982 ahc_outb(ahc, SXFRCTL0, sxfrctl0); 1983 } 1984 } 1985 if (active) { 1986 ahc_outb(ahc, SCSIRATE, scsirate); 1987 if ((ahc->features & AHC_ULTRA2) != 0) 1988 ahc_outb(ahc, SCSIOFFSET, offset); 1989 } 1990 1991 tinfo->scsirate = scsirate; 1992 tinfo->curr.period = period; 1993 tinfo->curr.offset = offset; 1994 tinfo->curr.ppr_options = ppr_options; 1995 1996 ahc_send_async(ahc, devinfo->channel, devinfo->target, 1997 CAM_LUN_WILDCARD, AC_TRANSFER_NEG, NULL); 1998 if (bootverbose) { 1999 if (offset != 0) { 2000 printf("%s: target %d synchronous at %sMHz%s, " 2001 "offset = 0x%x\n", ahc_name(ahc), 2002 devinfo->target, syncrate->rate, 2003 (ppr_options & MSG_EXT_PPR_DT_REQ) 2004 ? " DT" : "", offset); 2005 } else { 2006 printf("%s: target %d using " 2007 "asynchronous transfers\n", 2008 ahc_name(ahc), devinfo->target); 2009 } 2010 } 2011 } 2012 2013 update_needed += ahc_update_neg_request(ahc, devinfo, tstate, 2014 tinfo, AHC_NEG_TO_GOAL); 2015 2016 if (update_needed) 2017 ahc_update_pending_scbs(ahc); 2018 } 2019 2020 /* 2021 * Update the user/goal/curr tables of wide negotiation 2022 * parameters as well as, in the case of a current or active update, 2023 * any data structures on the host controller. In the case of an 2024 * active update, the specified target is currently talking to us on 2025 * the bus, so the transfer parameter update must take effect 2026 * immediately. 2027 */ 2028 void 2029 ahc_set_width(struct ahc_softc *ahc, struct ahc_devinfo *devinfo, 2030 u_int width, u_int type, int paused) 2031 { 2032 struct ahc_initiator_tinfo *tinfo; 2033 struct ahc_tmode_tstate *tstate; 2034 u_int oldwidth; 2035 int active; 2036 int update_needed; 2037 2038 active = (type & AHC_TRANS_ACTIVE) == AHC_TRANS_ACTIVE; 2039 update_needed = 0; 2040 tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid, 2041 devinfo->target, &tstate); 2042 2043 if ((type & AHC_TRANS_USER) != 0) 2044 tinfo->user.width = width; 2045 2046 if ((type & AHC_TRANS_GOAL) != 0) 2047 tinfo->goal.width = width; 2048 2049 oldwidth = tinfo->curr.width; 2050 if ((type & AHC_TRANS_CUR) != 0 && oldwidth != width) { 2051 u_int scsirate; 2052 2053 update_needed++; 2054 scsirate = tinfo->scsirate; 2055 scsirate &= ~WIDEXFER; 2056 if (width == MSG_EXT_WDTR_BUS_16_BIT) 2057 scsirate |= WIDEXFER; 2058 2059 tinfo->scsirate = scsirate; 2060 2061 if (active) 2062 ahc_outb(ahc, SCSIRATE, scsirate); 2063 2064 tinfo->curr.width = width; 2065 2066 ahc_send_async(ahc, devinfo->channel, devinfo->target, 2067 CAM_LUN_WILDCARD, AC_TRANSFER_NEG, NULL); 2068 if (bootverbose) { 2069 printf("%s: target %d using %dbit transfers\n", 2070 ahc_name(ahc), devinfo->target, 2071 8 * (0x01 << width)); 2072 } 2073 } 2074 2075 update_needed += ahc_update_neg_request(ahc, devinfo, tstate, 2076 tinfo, AHC_NEG_TO_GOAL); 2077 if (update_needed) 2078 ahc_update_pending_scbs(ahc); 2079 } 2080 2081 /* 2082 * Update the current state of tagged queuing for a given target. 2083 */ 2084 void 2085 ahc_set_tags(struct ahc_softc *ahc, struct ahc_devinfo *devinfo, 2086 ahc_queue_alg alg) 2087 { 2088 ahc_platform_set_tags(ahc, devinfo, alg); 2089 ahc_send_async(ahc, devinfo->channel, devinfo->target, 2090 devinfo->lun, AC_TRANSFER_NEG, &alg); 2091 } 2092 2093 /* 2094 * When the transfer settings for a connection change, update any 2095 * in-transit SCBs to contain the new data so the hardware will 2096 * be set correctly during future (re)selections. 2097 */ 2098 static void 2099 ahc_update_pending_scbs(struct ahc_softc *ahc) 2100 { 2101 struct scb *pending_scb; 2102 int pending_scb_count; 2103 int i; 2104 int paused; 2105 u_int saved_scbptr; 2106 2107 /* 2108 * Traverse the pending SCB list and ensure that all of the 2109 * SCBs there have the proper settings. 2110 */ 2111 pending_scb_count = 0; 2112 LIST_FOREACH(pending_scb, &ahc->pending_scbs, pending_links) { 2113 struct ahc_devinfo devinfo; 2114 struct hardware_scb *pending_hscb; 2115 struct ahc_initiator_tinfo *tinfo; 2116 struct ahc_tmode_tstate *tstate; 2117 2118 ahc_scb_devinfo(ahc, &devinfo, pending_scb); 2119 tinfo = ahc_fetch_transinfo(ahc, devinfo.channel, 2120 devinfo.our_scsiid, 2121 devinfo.target, &tstate); 2122 pending_hscb = pending_scb->hscb; 2123 pending_hscb->control &= ~ULTRAENB; 2124 if ((tstate->ultraenb & devinfo.target_mask) != 0) 2125 pending_hscb->control |= ULTRAENB; 2126 pending_hscb->scsirate = tinfo->scsirate; 2127 pending_hscb->scsioffset = tinfo->curr.offset; 2128 if ((tstate->auto_negotiate & devinfo.target_mask) == 0 2129 && (pending_scb->flags & SCB_AUTO_NEGOTIATE) != 0) { 2130 pending_scb->flags &= ~SCB_AUTO_NEGOTIATE; 2131 pending_hscb->control &= ~MK_MESSAGE; 2132 } 2133 ahc_sync_scb(ahc, pending_scb, 2134 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); 2135 pending_scb_count++; 2136 } 2137 2138 if (pending_scb_count == 0) 2139 return; 2140 2141 if (ahc_is_paused(ahc)) { 2142 paused = 1; 2143 } else { 2144 paused = 0; 2145 ahc_pause(ahc); 2146 } 2147 2148 saved_scbptr = ahc_inb(ahc, SCBPTR); 2149 /* Ensure that the hscbs down on the card match the new information */ 2150 for (i = 0; i < ahc->scb_data->maxhscbs; i++) { 2151 struct hardware_scb *pending_hscb; 2152 u_int control; 2153 u_int scb_tag; 2154 2155 ahc_outb(ahc, SCBPTR, i); 2156 scb_tag = ahc_inb(ahc, SCB_TAG); 2157 pending_scb = ahc_lookup_scb(ahc, scb_tag); 2158 if (pending_scb == NULL) 2159 continue; 2160 2161 pending_hscb = pending_scb->hscb; 2162 control = ahc_inb(ahc, SCB_CONTROL); 2163 control &= ~(ULTRAENB|MK_MESSAGE); 2164 control |= pending_hscb->control & (ULTRAENB|MK_MESSAGE); 2165 ahc_outb(ahc, SCB_CONTROL, control); 2166 ahc_outb(ahc, SCB_SCSIRATE, pending_hscb->scsirate); 2167 ahc_outb(ahc, SCB_SCSIOFFSET, pending_hscb->scsioffset); 2168 } 2169 ahc_outb(ahc, SCBPTR, saved_scbptr); 2170 2171 if (paused == 0) 2172 ahc_unpause(ahc); 2173 } 2174 2175 /**************************** Pathing Information *****************************/ 2176 static void 2177 ahc_fetch_devinfo(struct ahc_softc *ahc, struct ahc_devinfo *devinfo) 2178 { 2179 u_int saved_scsiid; 2180 role_t role; 2181 int our_id; 2182 2183 if (ahc_inb(ahc, SSTAT0) & TARGET) 2184 role = ROLE_TARGET; 2185 else 2186 role = ROLE_INITIATOR; 2187 2188 if (role == ROLE_TARGET 2189 && (ahc->features & AHC_MULTI_TID) != 0 2190 && (ahc_inb(ahc, SEQ_FLAGS) 2191 & (CMDPHASE_PENDING|TARG_CMD_PENDING|NO_DISCONNECT)) != 0) { 2192 /* We were selected, so pull our id from TARGIDIN */ 2193 our_id = ahc_inb(ahc, TARGIDIN) & OID; 2194 } else if ((ahc->features & AHC_ULTRA2) != 0) 2195 our_id = ahc_inb(ahc, SCSIID_ULTRA2) & OID; 2196 else 2197 our_id = ahc_inb(ahc, SCSIID) & OID; 2198 2199 saved_scsiid = ahc_inb(ahc, SAVED_SCSIID); 2200 ahc_compile_devinfo(devinfo, 2201 our_id, 2202 SCSIID_TARGET(ahc, saved_scsiid), 2203 ahc_inb(ahc, SAVED_LUN), 2204 SCSIID_CHANNEL(ahc, saved_scsiid), 2205 role); 2206 } 2207 2208 struct ahc_phase_table_entry* 2209 ahc_lookup_phase_entry(int phase) 2210 { 2211 struct ahc_phase_table_entry *entry; 2212 struct ahc_phase_table_entry *last_entry; 2213 2214 /* 2215 * num_phases doesn't include the default entry which 2216 * will be returned if the phase doesn't match. 2217 */ 2218 last_entry = &ahc_phase_table[num_phases]; 2219 for (entry = ahc_phase_table; entry < last_entry; entry++) { 2220 if (phase == entry->phase) 2221 break; 2222 } 2223 return (entry); 2224 } 2225 2226 void 2227 ahc_compile_devinfo(struct ahc_devinfo *devinfo, u_int our_id, u_int target, 2228 u_int lun, char channel, role_t role) 2229 { 2230 devinfo->our_scsiid = our_id; 2231 devinfo->target = target; 2232 devinfo->lun = lun; 2233 devinfo->target_offset = target; 2234 devinfo->channel = channel; 2235 devinfo->role = role; 2236 if (channel == 'B') 2237 devinfo->target_offset += 8; 2238 devinfo->target_mask = (0x01 << devinfo->target_offset); 2239 } 2240 2241 void 2242 ahc_print_devinfo(struct ahc_softc *ahc, struct ahc_devinfo *devinfo) 2243 { 2244 printf("%s:%c:%d:%d: ", ahc_name(ahc), devinfo->channel, 2245 devinfo->target, devinfo->lun); 2246 } 2247 2248 static void 2249 ahc_scb_devinfo(struct ahc_softc *ahc, struct ahc_devinfo *devinfo, 2250 struct scb *scb) 2251 { 2252 role_t role; 2253 int our_id; 2254 2255 our_id = SCSIID_OUR_ID(scb->hscb->scsiid); 2256 role = ROLE_INITIATOR; 2257 if ((scb->flags & SCB_TARGET_SCB) != 0) 2258 role = ROLE_TARGET; 2259 ahc_compile_devinfo(devinfo, our_id, SCB_GET_TARGET(ahc, scb), 2260 SCB_GET_LUN(scb), SCB_GET_CHANNEL(ahc, scb), role); 2261 } 2262 2263 /************************ Message Phase Processing ****************************/ 2264 static void 2265 ahc_assert_atn(struct ahc_softc *ahc) 2266 { 2267 u_int scsisigo; 2268 2269 scsisigo = ATNO; 2270 if ((ahc->features & AHC_DT) == 0) 2271 scsisigo |= ahc_inb(ahc, SCSISIGI); 2272 ahc_outb(ahc, SCSISIGO, scsisigo); 2273 } 2274 2275 /* 2276 * When an initiator transaction with the MK_MESSAGE flag either reconnects 2277 * or enters the initial message out phase, we are interrupted. Fill our 2278 * outgoing message buffer with the appropriate message and beging handing 2279 * the message phase(s) manually. 2280 */ 2281 static void 2282 ahc_setup_initiator_msgout(struct ahc_softc *ahc, struct ahc_devinfo *devinfo, 2283 struct scb *scb) 2284 { 2285 /* 2286 * To facilitate adding multiple messages together, 2287 * each routine should increment the index and len 2288 * variables instead of setting them explicitly. 2289 */ 2290 ahc->msgout_index = 0; 2291 ahc->msgout_len = 0; 2292 2293 if ((scb->flags & SCB_DEVICE_RESET) == 0 2294 && ahc_inb(ahc, MSG_OUT) == MSG_IDENTIFYFLAG) { 2295 u_int identify_msg; 2296 2297 identify_msg = MSG_IDENTIFYFLAG | SCB_GET_LUN(scb); 2298 if ((scb->hscb->control & DISCENB) != 0) 2299 identify_msg |= MSG_IDENTIFY_DISCFLAG; 2300 ahc->msgout_buf[ahc->msgout_index++] = identify_msg; 2301 ahc->msgout_len++; 2302 2303 if ((scb->hscb->control & TAG_ENB) != 0) { 2304 ahc->msgout_buf[ahc->msgout_index++] = 2305 scb->hscb->control & (TAG_ENB|SCB_TAG_TYPE); 2306 ahc->msgout_buf[ahc->msgout_index++] = scb->hscb->tag; 2307 ahc->msgout_len += 2; 2308 } 2309 } 2310 2311 if (scb->flags & SCB_DEVICE_RESET) { 2312 ahc->msgout_buf[ahc->msgout_index++] = MSG_BUS_DEV_RESET; 2313 ahc->msgout_len++; 2314 ahc_print_path(ahc, scb); 2315 printf("Bus Device Reset Message Sent\n"); 2316 /* 2317 * Clear our selection hardware in advance of 2318 * the busfree. We may have an entry in the waiting 2319 * Q for this target, and we don't want to go about 2320 * selecting while we handle the busfree and blow it 2321 * away. 2322 */ 2323 ahc_outb(ahc, SCSISEQ, (ahc_inb(ahc, SCSISEQ) & ~ENSELO)); 2324 } else if ((scb->flags & SCB_ABORT) != 0) { 2325 if ((scb->hscb->control & TAG_ENB) != 0) 2326 ahc->msgout_buf[ahc->msgout_index++] = MSG_ABORT_TAG; 2327 else 2328 ahc->msgout_buf[ahc->msgout_index++] = MSG_ABORT; 2329 ahc->msgout_len++; 2330 ahc_print_path(ahc, scb); 2331 printf("Abort%s Message Sent\n", 2332 (scb->hscb->control & TAG_ENB) != 0 ? " Tag" : ""); 2333 /* 2334 * Clear our selection hardware in advance of 2335 * the busfree. We may have an entry in the waiting 2336 * Q for this target, and we don't want to go about 2337 * selecting while we handle the busfree and blow it 2338 * away. 2339 */ 2340 ahc_outb(ahc, SCSISEQ, (ahc_inb(ahc, SCSISEQ) & ~ENSELO)); 2341 } else if ((scb->flags & (SCB_AUTO_NEGOTIATE|SCB_NEGOTIATE)) != 0) { 2342 ahc_build_transfer_msg(ahc, devinfo); 2343 } else { 2344 printf("ahc_intr: AWAITING_MSG for an SCB that " 2345 "does not have a waiting message\n"); 2346 printf("SCSIID = %x, target_mask = %x\n", scb->hscb->scsiid, 2347 devinfo->target_mask); 2348 panic("SCB = %d, SCB Control = %x, MSG_OUT = %x " 2349 "SCB flags = %x", scb->hscb->tag, scb->hscb->control, 2350 ahc_inb(ahc, MSG_OUT), scb->flags); 2351 } 2352 2353 /* 2354 * Clear the MK_MESSAGE flag from the SCB so we aren't 2355 * asked to send this message again. 2356 */ 2357 ahc_outb(ahc, SCB_CONTROL, ahc_inb(ahc, SCB_CONTROL) & ~MK_MESSAGE); 2358 scb->hscb->control &= ~MK_MESSAGE; 2359 ahc->msgout_index = 0; 2360 ahc->msg_type = MSG_TYPE_INITIATOR_MSGOUT; 2361 } 2362 2363 /* 2364 * Build an appropriate transfer negotiation message for the 2365 * currently active target. 2366 */ 2367 static void 2368 ahc_build_transfer_msg(struct ahc_softc *ahc, struct ahc_devinfo *devinfo) 2369 { 2370 /* 2371 * We need to initiate transfer negotiations. 2372 * If our current and goal settings are identical, 2373 * we want to renegotiate due to a check condition. 2374 */ 2375 struct ahc_initiator_tinfo *tinfo; 2376 struct ahc_tmode_tstate *tstate; 2377 struct ahc_syncrate *rate; 2378 int dowide; 2379 int dosync; 2380 int doppr; 2381 u_int period; 2382 u_int ppr_options; 2383 u_int offset; 2384 2385 tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid, 2386 devinfo->target, &tstate); 2387 /* 2388 * Filter our period based on the current connection. 2389 * If we can't perform DT transfers on this segment (not in LVD 2390 * mode for instance), then our decision to issue a PPR message 2391 * may change. 2392 */ 2393 period = tinfo->goal.period; 2394 offset = tinfo->goal.offset; 2395 ppr_options = tinfo->goal.ppr_options; 2396 /* Target initiated PPR is not allowed in the SCSI spec */ 2397 if (devinfo->role == ROLE_TARGET) 2398 ppr_options = 0; 2399 rate = ahc_devlimited_syncrate(ahc, tinfo, &period, 2400 &ppr_options, devinfo->role); 2401 dowide = tinfo->curr.width != tinfo->goal.width; 2402 dosync = tinfo->curr.offset != offset || tinfo->curr.period != period; 2403 /* 2404 * Only use PPR if we have options that need it, even if the device 2405 * claims to support it. There might be an expander in the way 2406 * that doesn't. 2407 */ 2408 doppr = ppr_options != 0; 2409 2410 if (!dowide && !dosync && !doppr) { 2411 dowide = tinfo->goal.width != MSG_EXT_WDTR_BUS_8_BIT; 2412 dosync = tinfo->goal.offset != 0; 2413 } 2414 2415 if (!dowide && !dosync && !doppr) { 2416 /* 2417 * Force async with a WDTR message if we have a wide bus, 2418 * or just issue an SDTR with a 0 offset. 2419 */ 2420 if ((ahc->features & AHC_WIDE) != 0) 2421 dowide = 1; 2422 else 2423 dosync = 1; 2424 2425 if (bootverbose) { 2426 ahc_print_devinfo(ahc, devinfo); 2427 printf("Ensuring async\n"); 2428 } 2429 } 2430 2431 /* Target initiated PPR is not allowed in the SCSI spec */ 2432 if (devinfo->role == ROLE_TARGET) 2433 doppr = 0; 2434 2435 /* 2436 * Both the PPR message and SDTR message require the 2437 * goal syncrate to be limited to what the target device 2438 * is capable of handling (based on whether an LVD->SE 2439 * expander is on the bus), so combine these two cases. 2440 * Regardless, guarantee that if we are using WDTR and SDTR 2441 * messages that WDTR comes first. 2442 */ 2443 if (doppr || (dosync && !dowide)) { 2444 offset = tinfo->goal.offset; 2445 ahc_validate_offset(ahc, tinfo, rate, &offset, 2446 doppr ? tinfo->goal.width 2447 : tinfo->curr.width, 2448 devinfo->role); 2449 if (doppr) { 2450 ahc_construct_ppr(ahc, devinfo, period, offset, 2451 tinfo->goal.width, ppr_options); 2452 } else { 2453 ahc_construct_sdtr(ahc, devinfo, period, offset); 2454 } 2455 } else { 2456 ahc_construct_wdtr(ahc, devinfo, tinfo->goal.width); 2457 } 2458 } 2459 2460 /* 2461 * Build a synchronous negotiation message in our message 2462 * buffer based on the input parameters. 2463 */ 2464 static void 2465 ahc_construct_sdtr(struct ahc_softc *ahc, struct ahc_devinfo *devinfo, 2466 u_int period, u_int offset) 2467 { 2468 if (offset == 0) 2469 period = AHC_ASYNC_XFER_PERIOD; 2470 ahc->msgout_buf[ahc->msgout_index++] = MSG_EXTENDED; 2471 ahc->msgout_buf[ahc->msgout_index++] = MSG_EXT_SDTR_LEN; 2472 ahc->msgout_buf[ahc->msgout_index++] = MSG_EXT_SDTR; 2473 ahc->msgout_buf[ahc->msgout_index++] = period; 2474 ahc->msgout_buf[ahc->msgout_index++] = offset; 2475 ahc->msgout_len += 5; 2476 if (bootverbose) { 2477 printf("(%s:%c:%d:%d): Sending SDTR period %x, offset %x\n", 2478 ahc_name(ahc), devinfo->channel, devinfo->target, 2479 devinfo->lun, period, offset); 2480 } 2481 } 2482 2483 /* 2484 * Build a wide negotiation message in our message 2485 * buffer based on the input parameters. 2486 */ 2487 static void 2488 ahc_construct_wdtr(struct ahc_softc *ahc, struct ahc_devinfo *devinfo, 2489 u_int bus_width) 2490 { 2491 ahc->msgout_buf[ahc->msgout_index++] = MSG_EXTENDED; 2492 ahc->msgout_buf[ahc->msgout_index++] = MSG_EXT_WDTR_LEN; 2493 ahc->msgout_buf[ahc->msgout_index++] = MSG_EXT_WDTR; 2494 ahc->msgout_buf[ahc->msgout_index++] = bus_width; 2495 ahc->msgout_len += 4; 2496 if (bootverbose) { 2497 printf("(%s:%c:%d:%d): Sending WDTR %x\n", 2498 ahc_name(ahc), devinfo->channel, devinfo->target, 2499 devinfo->lun, bus_width); 2500 } 2501 } 2502 2503 /* 2504 * Build a parallel protocol request message in our message 2505 * buffer based on the input parameters. 2506 */ 2507 static void 2508 ahc_construct_ppr(struct ahc_softc *ahc, struct ahc_devinfo *devinfo, 2509 u_int period, u_int offset, u_int bus_width, 2510 u_int ppr_options) 2511 { 2512 if (offset == 0) 2513 period = AHC_ASYNC_XFER_PERIOD; 2514 ahc->msgout_buf[ahc->msgout_index++] = MSG_EXTENDED; 2515 ahc->msgout_buf[ahc->msgout_index++] = MSG_EXT_PPR_LEN; 2516 ahc->msgout_buf[ahc->msgout_index++] = MSG_EXT_PPR; 2517 ahc->msgout_buf[ahc->msgout_index++] = period; 2518 ahc->msgout_buf[ahc->msgout_index++] = 0; 2519 ahc->msgout_buf[ahc->msgout_index++] = offset; 2520 ahc->msgout_buf[ahc->msgout_index++] = bus_width; 2521 ahc->msgout_buf[ahc->msgout_index++] = ppr_options; 2522 ahc->msgout_len += 8; 2523 if (bootverbose) { 2524 printf("(%s:%c:%d:%d): Sending PPR bus_width %x, period %x, " 2525 "offset %x, ppr_options %x\n", ahc_name(ahc), 2526 devinfo->channel, devinfo->target, devinfo->lun, 2527 bus_width, period, offset, ppr_options); 2528 } 2529 } 2530 2531 /* 2532 * Clear any active message state. 2533 */ 2534 static void 2535 ahc_clear_msg_state(struct ahc_softc *ahc) 2536 { 2537 ahc->msgout_len = 0; 2538 ahc->msgin_index = 0; 2539 ahc->msg_type = MSG_TYPE_NONE; 2540 if ((ahc_inb(ahc, SCSISIGI) & ATNI) != 0) { 2541 /* 2542 * The target didn't care to respond to our 2543 * message request, so clear ATN. 2544 */ 2545 ahc_outb(ahc, CLRSINT1, CLRATNO); 2546 } 2547 ahc_outb(ahc, MSG_OUT, MSG_NOOP); 2548 ahc_outb(ahc, SEQ_FLAGS2, 2549 ahc_inb(ahc, SEQ_FLAGS2) & ~TARGET_MSG_PENDING); 2550 } 2551 2552 static void 2553 ahc_handle_proto_violation(struct ahc_softc *ahc) 2554 { 2555 struct ahc_devinfo devinfo; 2556 struct scb *scb; 2557 u_int scbid; 2558 u_int seq_flags; 2559 u_int curphase; 2560 u_int lastphase; 2561 int found; 2562 2563 ahc_fetch_devinfo(ahc, &devinfo); 2564 scbid = ahc_inb(ahc, SCB_TAG); 2565 scb = ahc_lookup_scb(ahc, scbid); 2566 seq_flags = ahc_inb(ahc, SEQ_FLAGS); 2567 curphase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK; 2568 lastphase = ahc_inb(ahc, LASTPHASE); 2569 if ((seq_flags & NOT_IDENTIFIED) != 0) { 2570 /* 2571 * The reconnecting target either did not send an 2572 * identify message, or did, but we didn't find an SCB 2573 * to match. 2574 */ 2575 ahc_print_devinfo(ahc, &devinfo); 2576 printf("Target did not send an IDENTIFY message. " 2577 "LASTPHASE = 0x%x.\n", lastphase); 2578 scb = NULL; 2579 } else if (scb == NULL) { 2580 /* 2581 * We don't seem to have an SCB active for this 2582 * transaction. Print an error and reset the bus. 2583 */ 2584 ahc_print_devinfo(ahc, &devinfo); 2585 printf("No SCB found during protocol violation\n"); 2586 goto proto_violation_reset; 2587 } else { 2588 aic_set_transaction_status(scb, CAM_SEQUENCE_FAIL); 2589 if ((seq_flags & NO_CDB_SENT) != 0) { 2590 ahc_print_path(ahc, scb); 2591 printf("No or incomplete CDB sent to device.\n"); 2592 } else if ((ahc_inb(ahc, SCB_CONTROL) & STATUS_RCVD) == 0) { 2593 /* 2594 * The target never bothered to provide status to 2595 * us prior to completing the command. Since we don't 2596 * know the disposition of this command, we must attempt 2597 * to abort it. Assert ATN and prepare to send an abort 2598 * message. 2599 */ 2600 ahc_print_path(ahc, scb); 2601 printf("Completed command without status.\n"); 2602 } else { 2603 ahc_print_path(ahc, scb); 2604 printf("Unknown protocol violation.\n"); 2605 ahc_dump_card_state(ahc); 2606 } 2607 } 2608 if ((lastphase & ~P_DATAIN_DT) == 0 2609 || lastphase == P_COMMAND) { 2610 proto_violation_reset: 2611 /* 2612 * Target either went directly to data/command 2613 * phase or didn't respond to our ATN. 2614 * The only safe thing to do is to blow 2615 * it away with a bus reset. 2616 */ 2617 found = ahc_reset_channel(ahc, 'A', TRUE); 2618 printf("%s: Issued Channel %c Bus Reset. " 2619 "%d SCBs aborted\n", ahc_name(ahc), 'A', found); 2620 } else { 2621 /* 2622 * Leave the selection hardware off in case 2623 * this abort attempt will affect yet to 2624 * be sent commands. 2625 */ 2626 ahc_outb(ahc, SCSISEQ, 2627 ahc_inb(ahc, SCSISEQ) & ~ENSELO); 2628 ahc_assert_atn(ahc); 2629 ahc_outb(ahc, MSG_OUT, HOST_MSG); 2630 if (scb == NULL) { 2631 ahc_print_devinfo(ahc, &devinfo); 2632 ahc->msgout_buf[0] = MSG_ABORT_TASK; 2633 ahc->msgout_len = 1; 2634 ahc->msgout_index = 0; 2635 ahc->msg_type = MSG_TYPE_INITIATOR_MSGOUT; 2636 } else { 2637 ahc_print_path(ahc, scb); 2638 scb->flags |= SCB_ABORT; 2639 } 2640 printf("Protocol violation %s. Attempting to abort.\n", 2641 ahc_lookup_phase_entry(curphase)->phasemsg); 2642 } 2643 } 2644 2645 /* 2646 * Manual message loop handler. 2647 */ 2648 static void 2649 ahc_handle_message_phase(struct ahc_softc *ahc) 2650 { 2651 struct ahc_devinfo devinfo; 2652 u_int bus_phase; 2653 int end_session; 2654 2655 ahc_fetch_devinfo(ahc, &devinfo); 2656 end_session = FALSE; 2657 bus_phase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK; 2658 2659 reswitch: 2660 switch (ahc->msg_type) { 2661 case MSG_TYPE_INITIATOR_MSGOUT: 2662 { 2663 int lastbyte; 2664 int phasemis; 2665 int msgdone; 2666 2667 if (ahc->msgout_len == 0) 2668 panic("HOST_MSG_LOOP interrupt with no active message"); 2669 2670 #ifdef AHC_DEBUG 2671 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) { 2672 ahc_print_devinfo(ahc, &devinfo); 2673 printf("INITIATOR_MSG_OUT"); 2674 } 2675 #endif 2676 phasemis = bus_phase != P_MESGOUT; 2677 if (phasemis) { 2678 #ifdef AHC_DEBUG 2679 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) { 2680 printf(" PHASEMIS %s\n", 2681 ahc_lookup_phase_entry(bus_phase) 2682 ->phasemsg); 2683 } 2684 #endif 2685 if (bus_phase == P_MESGIN) { 2686 /* 2687 * Change gears and see if 2688 * this messages is of interest to 2689 * us or should be passed back to 2690 * the sequencer. 2691 */ 2692 ahc_outb(ahc, CLRSINT1, CLRATNO); 2693 ahc->send_msg_perror = FALSE; 2694 ahc->msg_type = MSG_TYPE_INITIATOR_MSGIN; 2695 ahc->msgin_index = 0; 2696 goto reswitch; 2697 } 2698 end_session = TRUE; 2699 break; 2700 } 2701 2702 if (ahc->send_msg_perror) { 2703 ahc_outb(ahc, CLRSINT1, CLRATNO); 2704 ahc_outb(ahc, CLRSINT1, CLRREQINIT); 2705 #ifdef AHC_DEBUG 2706 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) 2707 printf(" byte 0x%x\n", ahc->send_msg_perror); 2708 #endif 2709 ahc_outb(ahc, SCSIDATL, MSG_PARITY_ERROR); 2710 break; 2711 } 2712 2713 msgdone = ahc->msgout_index == ahc->msgout_len; 2714 if (msgdone) { 2715 /* 2716 * The target has requested a retry. 2717 * Re-assert ATN, reset our message index to 2718 * 0, and try again. 2719 */ 2720 ahc->msgout_index = 0; 2721 ahc_assert_atn(ahc); 2722 } 2723 2724 lastbyte = ahc->msgout_index == (ahc->msgout_len - 1); 2725 if (lastbyte) { 2726 /* Last byte is signified by dropping ATN */ 2727 ahc_outb(ahc, CLRSINT1, CLRATNO); 2728 } 2729 2730 /* 2731 * Clear our interrupt status and present 2732 * the next byte on the bus. 2733 */ 2734 ahc_outb(ahc, CLRSINT1, CLRREQINIT); 2735 #ifdef AHC_DEBUG 2736 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) 2737 printf(" byte 0x%x\n", 2738 ahc->msgout_buf[ahc->msgout_index]); 2739 #endif 2740 ahc_outb(ahc, SCSIDATL, ahc->msgout_buf[ahc->msgout_index++]); 2741 break; 2742 } 2743 case MSG_TYPE_INITIATOR_MSGIN: 2744 { 2745 int phasemis; 2746 int message_done; 2747 2748 #ifdef AHC_DEBUG 2749 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) { 2750 ahc_print_devinfo(ahc, &devinfo); 2751 printf("INITIATOR_MSG_IN"); 2752 } 2753 #endif 2754 phasemis = bus_phase != P_MESGIN; 2755 if (phasemis) { 2756 #ifdef AHC_DEBUG 2757 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) { 2758 printf(" PHASEMIS %s\n", 2759 ahc_lookup_phase_entry(bus_phase) 2760 ->phasemsg); 2761 } 2762 #endif 2763 ahc->msgin_index = 0; 2764 if (bus_phase == P_MESGOUT 2765 && (ahc->send_msg_perror == TRUE 2766 || (ahc->msgout_len != 0 2767 && ahc->msgout_index == 0))) { 2768 ahc->msg_type = MSG_TYPE_INITIATOR_MSGOUT; 2769 goto reswitch; 2770 } 2771 end_session = TRUE; 2772 break; 2773 } 2774 2775 /* Pull the byte in without acking it */ 2776 ahc->msgin_buf[ahc->msgin_index] = ahc_inb(ahc, SCSIBUSL); 2777 #ifdef AHC_DEBUG 2778 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) 2779 printf(" byte 0x%x\n", 2780 ahc->msgin_buf[ahc->msgin_index]); 2781 #endif 2782 2783 message_done = ahc_parse_msg(ahc, &devinfo); 2784 2785 if (message_done) { 2786 /* 2787 * Clear our incoming message buffer in case there 2788 * is another message following this one. 2789 */ 2790 ahc->msgin_index = 0; 2791 2792 /* 2793 * If this message illicited a response, 2794 * assert ATN so the target takes us to the 2795 * message out phase. 2796 */ 2797 if (ahc->msgout_len != 0) { 2798 #ifdef AHC_DEBUG 2799 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) { 2800 ahc_print_devinfo(ahc, &devinfo); 2801 printf("Asserting ATN for response\n"); 2802 } 2803 #endif 2804 ahc_assert_atn(ahc); 2805 } 2806 } else 2807 ahc->msgin_index++; 2808 2809 if (message_done == MSGLOOP_TERMINATED) { 2810 end_session = TRUE; 2811 } else { 2812 /* Ack the byte */ 2813 ahc_outb(ahc, CLRSINT1, CLRREQINIT); 2814 ahc_inb(ahc, SCSIDATL); 2815 } 2816 break; 2817 } 2818 case MSG_TYPE_TARGET_MSGIN: 2819 { 2820 int msgdone; 2821 2822 if (ahc->msgout_len == 0) 2823 panic("Target MSGIN with no active message"); 2824 2825 #ifdef AHC_DEBUG 2826 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) { 2827 ahc_print_devinfo(ahc, &devinfo); 2828 printf("TARGET_MSG_IN"); 2829 } 2830 #endif 2831 2832 /* 2833 * If we interrupted a mesgout session, the initiator 2834 * will not know this until our first REQ. So, we 2835 * only honor mesgout requests after we've sent our 2836 * first byte. 2837 */ 2838 if ((ahc_inb(ahc, SCSISIGI) & ATNI) != 0 2839 && ahc->msgout_index > 0) { 2840 /* 2841 * Change gears and see if this messages is 2842 * of interest to us or should be passed back 2843 * to the sequencer. 2844 */ 2845 #ifdef AHC_DEBUG 2846 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) 2847 printf(" Honoring ATN Request.\n"); 2848 #endif 2849 ahc->msg_type = MSG_TYPE_TARGET_MSGOUT; 2850 2851 /* 2852 * Disable SCSI Programmed I/O during the 2853 * phase change so as to avoid phantom REQs. 2854 */ 2855 ahc_outb(ahc, SXFRCTL0, 2856 ahc_inb(ahc, SXFRCTL0) & ~SPIOEN); 2857 2858 /* 2859 * Since SPIORDY asserts when ACK is asserted 2860 * for P_MSGOUT, and SPIORDY's assertion triggered 2861 * our entry into this routine, wait for ACK to 2862 * *de-assert* before changing phases. 2863 */ 2864 while ((ahc_inb(ahc, SCSISIGI) & ACKI) != 0) 2865 ; 2866 2867 ahc_outb(ahc, SCSISIGO, P_MESGOUT | BSYO); 2868 2869 /* 2870 * All phase line changes require a bus 2871 * settle delay before REQ is asserted. 2872 * [SCSI SPI4 10.7.1] 2873 */ 2874 ahc_flush_device_writes(ahc); 2875 aic_delay(AHC_BUSSETTLE_DELAY); 2876 2877 ahc->msgin_index = 0; 2878 /* Enable SCSI Programmed I/O to REQ for first byte */ 2879 ahc_outb(ahc, SXFRCTL0, 2880 ahc_inb(ahc, SXFRCTL0) | SPIOEN); 2881 break; 2882 } 2883 2884 msgdone = ahc->msgout_index == ahc->msgout_len; 2885 if (msgdone) { 2886 ahc_outb(ahc, SXFRCTL0, 2887 ahc_inb(ahc, SXFRCTL0) & ~SPIOEN); 2888 end_session = TRUE; 2889 break; 2890 } 2891 2892 /* 2893 * Present the next byte on the bus. 2894 */ 2895 #ifdef AHC_DEBUG 2896 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) 2897 printf(" byte 0x%x\n", 2898 ahc->msgout_buf[ahc->msgout_index]); 2899 #endif 2900 ahc_outb(ahc, SXFRCTL0, ahc_inb(ahc, SXFRCTL0) | SPIOEN); 2901 ahc_outb(ahc, SCSIDATL, ahc->msgout_buf[ahc->msgout_index++]); 2902 break; 2903 } 2904 case MSG_TYPE_TARGET_MSGOUT: 2905 { 2906 int lastbyte; 2907 int msgdone; 2908 2909 #ifdef AHC_DEBUG 2910 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) { 2911 ahc_print_devinfo(ahc, &devinfo); 2912 printf("TARGET_MSG_OUT"); 2913 } 2914 #endif 2915 /* 2916 * The initiator signals that this is 2917 * the last byte by dropping ATN. 2918 */ 2919 lastbyte = (ahc_inb(ahc, SCSISIGI) & ATNI) == 0; 2920 2921 /* 2922 * Read the latched byte, but turn off SPIOEN first 2923 * so that we don't inadvertently cause a REQ for the 2924 * next byte. 2925 */ 2926 ahc_outb(ahc, SXFRCTL0, ahc_inb(ahc, SXFRCTL0) & ~SPIOEN); 2927 ahc->msgin_buf[ahc->msgin_index] = ahc_inb(ahc, SCSIDATL); 2928 2929 #ifdef AHC_DEBUG 2930 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) 2931 printf(" byte 0x%x\n", 2932 ahc->msgin_buf[ahc->msgin_index]); 2933 #endif 2934 2935 msgdone = ahc_parse_msg(ahc, &devinfo); 2936 if (msgdone == MSGLOOP_TERMINATED) { 2937 /* 2938 * The message is *really* done in that it caused 2939 * us to go to bus free. The sequencer has already 2940 * been reset at this point, so pull the ejection 2941 * handle. 2942 */ 2943 return; 2944 } 2945 2946 ahc->msgin_index++; 2947 2948 /* 2949 * XXX Read spec about initiator dropping ATN too soon 2950 * and use msgdone to detect it. 2951 */ 2952 if (msgdone == MSGLOOP_MSGCOMPLETE) { 2953 ahc->msgin_index = 0; 2954 2955 /* 2956 * If this message illicited a response, transition 2957 * to the Message in phase and send it. 2958 */ 2959 if (ahc->msgout_len != 0) { 2960 #ifdef AHC_DEBUG 2961 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) { 2962 ahc_print_devinfo(ahc, &devinfo); 2963 printf(" preparing response.\n"); 2964 } 2965 #endif 2966 ahc_outb(ahc, SCSISIGO, P_MESGIN | BSYO); 2967 2968 /* 2969 * All phase line changes require a bus 2970 * settle delay before REQ is asserted. 2971 * [SCSI SPI4 10.7.1] When transitioning 2972 * from an OUT to an IN phase, we must 2973 * also wait a data release delay to allow 2974 * the initiator time to release the data 2975 * lines. [SCSI SPI4 10.12] 2976 */ 2977 ahc_flush_device_writes(ahc); 2978 aic_delay(AHC_BUSSETTLE_DELAY 2979 + AHC_DATARELEASE_DELAY); 2980 2981 /* 2982 * Enable SCSI Programmed I/O. This will 2983 * immediately cause SPIORDY to assert, 2984 * and the sequencer will call our message 2985 * loop again. 2986 */ 2987 ahc_outb(ahc, SXFRCTL0, 2988 ahc_inb(ahc, SXFRCTL0) | SPIOEN); 2989 ahc->msg_type = MSG_TYPE_TARGET_MSGIN; 2990 ahc->msgin_index = 0; 2991 break; 2992 } 2993 } 2994 2995 if (lastbyte) 2996 end_session = TRUE; 2997 else { 2998 /* Ask for the next byte. */ 2999 ahc_outb(ahc, SXFRCTL0, 3000 ahc_inb(ahc, SXFRCTL0) | SPIOEN); 3001 } 3002 3003 break; 3004 } 3005 default: 3006 panic("Unknown REQINIT message type"); 3007 } 3008 3009 if (end_session) { 3010 ahc_clear_msg_state(ahc); 3011 ahc_outb(ahc, RETURN_1, EXIT_MSG_LOOP); 3012 } else 3013 ahc_outb(ahc, RETURN_1, CONT_MSG_LOOP); 3014 } 3015 3016 /* 3017 * See if we sent a particular extended message to the target. 3018 * If "full" is true, return true only if the target saw the full 3019 * message. If "full" is false, return true if the target saw at 3020 * least the first byte of the message. 3021 */ 3022 static int 3023 ahc_sent_msg(struct ahc_softc *ahc, ahc_msgtype type, u_int msgval, int full) 3024 { 3025 int found; 3026 u_int index; 3027 3028 found = FALSE; 3029 index = 0; 3030 3031 while (index < ahc->msgout_len) { 3032 if (ahc->msgout_buf[index] == MSG_EXTENDED) { 3033 u_int end_index; 3034 3035 end_index = index + 1 + ahc->msgout_buf[index + 1]; 3036 if (ahc->msgout_buf[index+2] == msgval 3037 && type == AHCMSG_EXT) { 3038 if (full) { 3039 if (ahc->msgout_index > end_index) 3040 found = TRUE; 3041 } else if (ahc->msgout_index > index) 3042 found = TRUE; 3043 } 3044 index = end_index; 3045 } else if (ahc->msgout_buf[index] >= MSG_SIMPLE_TASK 3046 && ahc->msgout_buf[index] <= MSG_IGN_WIDE_RESIDUE) { 3047 /* Skip tag type and tag id or residue param*/ 3048 index += 2; 3049 } else { 3050 /* Single byte message */ 3051 if (type == AHCMSG_1B 3052 && ahc->msgout_buf[index] == msgval 3053 && ahc->msgout_index > index) 3054 found = TRUE; 3055 index++; 3056 } 3057 3058 if (found) 3059 break; 3060 } 3061 return (found); 3062 } 3063 3064 /* 3065 * Wait for a complete incoming message, parse it, and respond accordingly. 3066 */ 3067 static int 3068 ahc_parse_msg(struct ahc_softc *ahc, struct ahc_devinfo *devinfo) 3069 { 3070 struct ahc_initiator_tinfo *tinfo; 3071 struct ahc_tmode_tstate *tstate; 3072 int reject; 3073 int done; 3074 int response; 3075 u_int targ_scsirate; 3076 3077 done = MSGLOOP_IN_PROG; 3078 response = FALSE; 3079 reject = FALSE; 3080 tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid, 3081 devinfo->target, &tstate); 3082 targ_scsirate = tinfo->scsirate; 3083 3084 /* 3085 * Parse as much of the message as is available, 3086 * rejecting it if we don't support it. When 3087 * the entire message is available and has been 3088 * handled, return MSGLOOP_MSGCOMPLETE, indicating 3089 * that we have parsed an entire message. 3090 * 3091 * In the case of extended messages, we accept the length 3092 * byte outright and perform more checking once we know the 3093 * extended message type. 3094 */ 3095 switch (ahc->msgin_buf[0]) { 3096 case MSG_DISCONNECT: 3097 case MSG_SAVEDATAPOINTER: 3098 case MSG_CMDCOMPLETE: 3099 case MSG_RESTOREPOINTERS: 3100 case MSG_IGN_WIDE_RESIDUE: 3101 /* 3102 * End our message loop as these are messages 3103 * the sequencer handles on its own. 3104 */ 3105 done = MSGLOOP_TERMINATED; 3106 break; 3107 case MSG_MESSAGE_REJECT: 3108 response = ahc_handle_msg_reject(ahc, devinfo); 3109 /* FALLTHROUGH */ 3110 case MSG_NOOP: 3111 done = MSGLOOP_MSGCOMPLETE; 3112 break; 3113 case MSG_EXTENDED: 3114 { 3115 /* Wait for enough of the message to begin validation */ 3116 if (ahc->msgin_index < 2) 3117 break; 3118 switch (ahc->msgin_buf[2]) { 3119 case MSG_EXT_SDTR: 3120 { 3121 struct ahc_syncrate *syncrate; 3122 u_int period; 3123 u_int ppr_options; 3124 u_int offset; 3125 u_int saved_offset; 3126 3127 if (ahc->msgin_buf[1] != MSG_EXT_SDTR_LEN) { 3128 reject = TRUE; 3129 break; 3130 } 3131 3132 /* 3133 * Wait until we have both args before validating 3134 * and acting on this message. 3135 * 3136 * Add one to MSG_EXT_SDTR_LEN to account for 3137 * the extended message preamble. 3138 */ 3139 if (ahc->msgin_index < (MSG_EXT_SDTR_LEN + 1)) 3140 break; 3141 3142 period = ahc->msgin_buf[3]; 3143 ppr_options = 0; 3144 saved_offset = offset = ahc->msgin_buf[4]; 3145 syncrate = ahc_devlimited_syncrate(ahc, tinfo, &period, 3146 &ppr_options, 3147 devinfo->role); 3148 ahc_validate_offset(ahc, tinfo, syncrate, &offset, 3149 targ_scsirate & WIDEXFER, 3150 devinfo->role); 3151 if (bootverbose) { 3152 printf("(%s:%c:%d:%d): Received " 3153 "SDTR period %x, offset %x\n\t" 3154 "Filtered to period %x, offset %x\n", 3155 ahc_name(ahc), devinfo->channel, 3156 devinfo->target, devinfo->lun, 3157 ahc->msgin_buf[3], saved_offset, 3158 period, offset); 3159 } 3160 ahc_set_syncrate(ahc, devinfo, 3161 syncrate, period, 3162 offset, ppr_options, 3163 AHC_TRANS_ACTIVE|AHC_TRANS_GOAL, 3164 /*paused*/TRUE); 3165 3166 /* 3167 * See if we initiated Sync Negotiation 3168 * and didn't have to fall down to async 3169 * transfers. 3170 */ 3171 if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_SDTR, TRUE)) { 3172 /* We started it */ 3173 if (saved_offset != offset) { 3174 /* Went too low - force async */ 3175 reject = TRUE; 3176 } 3177 } else { 3178 /* 3179 * Send our own SDTR in reply 3180 */ 3181 if (bootverbose 3182 && devinfo->role == ROLE_INITIATOR) { 3183 printf("(%s:%c:%d:%d): Target " 3184 "Initiated SDTR\n", 3185 ahc_name(ahc), devinfo->channel, 3186 devinfo->target, devinfo->lun); 3187 } 3188 ahc->msgout_index = 0; 3189 ahc->msgout_len = 0; 3190 ahc_construct_sdtr(ahc, devinfo, 3191 period, offset); 3192 ahc->msgout_index = 0; 3193 response = TRUE; 3194 } 3195 done = MSGLOOP_MSGCOMPLETE; 3196 break; 3197 } 3198 case MSG_EXT_WDTR: 3199 { 3200 u_int bus_width; 3201 u_int saved_width; 3202 u_int sending_reply; 3203 3204 sending_reply = FALSE; 3205 if (ahc->msgin_buf[1] != MSG_EXT_WDTR_LEN) { 3206 reject = TRUE; 3207 break; 3208 } 3209 3210 /* 3211 * Wait until we have our arg before validating 3212 * and acting on this message. 3213 * 3214 * Add one to MSG_EXT_WDTR_LEN to account for 3215 * the extended message preamble. 3216 */ 3217 if (ahc->msgin_index < (MSG_EXT_WDTR_LEN + 1)) 3218 break; 3219 3220 bus_width = ahc->msgin_buf[3]; 3221 saved_width = bus_width; 3222 ahc_validate_width(ahc, tinfo, &bus_width, 3223 devinfo->role); 3224 if (bootverbose) { 3225 printf("(%s:%c:%d:%d): Received WDTR " 3226 "%x filtered to %x\n", 3227 ahc_name(ahc), devinfo->channel, 3228 devinfo->target, devinfo->lun, 3229 saved_width, bus_width); 3230 } 3231 3232 if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_WDTR, TRUE)) { 3233 /* 3234 * Don't send a WDTR back to the 3235 * target, since we asked first. 3236 * If the width went higher than our 3237 * request, reject it. 3238 */ 3239 if (saved_width > bus_width) { 3240 reject = TRUE; 3241 printf("(%s:%c:%d:%d): requested %dBit " 3242 "transfers. Rejecting...\n", 3243 ahc_name(ahc), devinfo->channel, 3244 devinfo->target, devinfo->lun, 3245 8 * (0x01 << bus_width)); 3246 bus_width = 0; 3247 } 3248 } else { 3249 /* 3250 * Send our own WDTR in reply 3251 */ 3252 if (bootverbose 3253 && devinfo->role == ROLE_INITIATOR) { 3254 printf("(%s:%c:%d:%d): Target " 3255 "Initiated WDTR\n", 3256 ahc_name(ahc), devinfo->channel, 3257 devinfo->target, devinfo->lun); 3258 } 3259 ahc->msgout_index = 0; 3260 ahc->msgout_len = 0; 3261 ahc_construct_wdtr(ahc, devinfo, bus_width); 3262 ahc->msgout_index = 0; 3263 response = TRUE; 3264 sending_reply = TRUE; 3265 } 3266 /* 3267 * After a wide message, we are async, but 3268 * some devices don't seem to honor this portion 3269 * of the spec. Force a renegotiation of the 3270 * sync component of our transfer agreement even 3271 * if our goal is async. By updating our width 3272 * after forcing the negotiation, we avoid 3273 * renegotiating for width. 3274 */ 3275 ahc_update_neg_request(ahc, devinfo, tstate, 3276 tinfo, AHC_NEG_ALWAYS); 3277 ahc_set_width(ahc, devinfo, bus_width, 3278 AHC_TRANS_ACTIVE|AHC_TRANS_GOAL, 3279 /*paused*/TRUE); 3280 if (sending_reply == FALSE && reject == FALSE) { 3281 /* 3282 * We will always have an SDTR to send. 3283 */ 3284 ahc->msgout_index = 0; 3285 ahc->msgout_len = 0; 3286 ahc_build_transfer_msg(ahc, devinfo); 3287 ahc->msgout_index = 0; 3288 response = TRUE; 3289 } 3290 done = MSGLOOP_MSGCOMPLETE; 3291 break; 3292 } 3293 case MSG_EXT_PPR: 3294 { 3295 struct ahc_syncrate *syncrate; 3296 u_int period; 3297 u_int offset; 3298 u_int bus_width; 3299 u_int ppr_options; 3300 u_int saved_width; 3301 u_int saved_offset; 3302 u_int saved_ppr_options; 3303 3304 if (ahc->msgin_buf[1] != MSG_EXT_PPR_LEN) { 3305 reject = TRUE; 3306 break; 3307 } 3308 3309 /* 3310 * Wait until we have all args before validating 3311 * and acting on this message. 3312 * 3313 * Add one to MSG_EXT_PPR_LEN to account for 3314 * the extended message preamble. 3315 */ 3316 if (ahc->msgin_index < (MSG_EXT_PPR_LEN + 1)) 3317 break; 3318 3319 period = ahc->msgin_buf[3]; 3320 offset = ahc->msgin_buf[5]; 3321 bus_width = ahc->msgin_buf[6]; 3322 saved_width = bus_width; 3323 ppr_options = ahc->msgin_buf[7]; 3324 /* 3325 * According to the spec, a DT only 3326 * period factor with no DT option 3327 * set implies async. 3328 */ 3329 if ((ppr_options & MSG_EXT_PPR_DT_REQ) == 0 3330 && period == 9) 3331 offset = 0; 3332 saved_ppr_options = ppr_options; 3333 saved_offset = offset; 3334 3335 /* 3336 * Mask out any options we don't support 3337 * on any controller. Transfer options are 3338 * only available if we are negotiating wide. 3339 */ 3340 ppr_options &= MSG_EXT_PPR_DT_REQ; 3341 if (bus_width == 0) 3342 ppr_options = 0; 3343 3344 ahc_validate_width(ahc, tinfo, &bus_width, 3345 devinfo->role); 3346 syncrate = ahc_devlimited_syncrate(ahc, tinfo, &period, 3347 &ppr_options, 3348 devinfo->role); 3349 ahc_validate_offset(ahc, tinfo, syncrate, 3350 &offset, bus_width, 3351 devinfo->role); 3352 3353 if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_PPR, TRUE)) { 3354 /* 3355 * If we are unable to do any of the 3356 * requested options (we went too low), 3357 * then we'll have to reject the message. 3358 */ 3359 if (saved_width > bus_width 3360 || saved_offset != offset 3361 || saved_ppr_options != ppr_options) { 3362 reject = TRUE; 3363 period = 0; 3364 offset = 0; 3365 bus_width = 0; 3366 ppr_options = 0; 3367 syncrate = NULL; 3368 } 3369 } else { 3370 if (devinfo->role != ROLE_TARGET) 3371 printf("(%s:%c:%d:%d): Target " 3372 "Initiated PPR\n", 3373 ahc_name(ahc), devinfo->channel, 3374 devinfo->target, devinfo->lun); 3375 else 3376 printf("(%s:%c:%d:%d): Initiator " 3377 "Initiated PPR\n", 3378 ahc_name(ahc), devinfo->channel, 3379 devinfo->target, devinfo->lun); 3380 ahc->msgout_index = 0; 3381 ahc->msgout_len = 0; 3382 ahc_construct_ppr(ahc, devinfo, period, offset, 3383 bus_width, ppr_options); 3384 ahc->msgout_index = 0; 3385 response = TRUE; 3386 } 3387 if (bootverbose) { 3388 printf("(%s:%c:%d:%d): Received PPR width %x, " 3389 "period %x, offset %x,options %x\n" 3390 "\tFiltered to width %x, period %x, " 3391 "offset %x, options %x\n", 3392 ahc_name(ahc), devinfo->channel, 3393 devinfo->target, devinfo->lun, 3394 saved_width, ahc->msgin_buf[3], 3395 saved_offset, saved_ppr_options, 3396 bus_width, period, offset, ppr_options); 3397 } 3398 ahc_set_width(ahc, devinfo, bus_width, 3399 AHC_TRANS_ACTIVE|AHC_TRANS_GOAL, 3400 /*paused*/TRUE); 3401 ahc_set_syncrate(ahc, devinfo, 3402 syncrate, period, 3403 offset, ppr_options, 3404 AHC_TRANS_ACTIVE|AHC_TRANS_GOAL, 3405 /*paused*/TRUE); 3406 done = MSGLOOP_MSGCOMPLETE; 3407 break; 3408 } 3409 default: 3410 /* Unknown extended message. Reject it. */ 3411 reject = TRUE; 3412 break; 3413 } 3414 break; 3415 } 3416 #ifdef AHC_TARGET_MODE 3417 case MSG_BUS_DEV_RESET: 3418 ahc_handle_devreset(ahc, devinfo, 3419 CAM_BDR_SENT, 3420 "Bus Device Reset Received", 3421 /*verbose_level*/0); 3422 ahc_restart(ahc); 3423 done = MSGLOOP_TERMINATED; 3424 break; 3425 case MSG_ABORT_TAG: 3426 case MSG_ABORT: 3427 case MSG_CLEAR_QUEUE: 3428 { 3429 int tag; 3430 3431 /* Target mode messages */ 3432 if (devinfo->role != ROLE_TARGET) { 3433 reject = TRUE; 3434 break; 3435 } 3436 tag = SCB_LIST_NULL; 3437 if (ahc->msgin_buf[0] == MSG_ABORT_TAG) 3438 tag = ahc_inb(ahc, INITIATOR_TAG); 3439 ahc_abort_scbs(ahc, devinfo->target, devinfo->channel, 3440 devinfo->lun, tag, ROLE_TARGET, 3441 CAM_REQ_ABORTED); 3442 3443 tstate = ahc->enabled_targets[devinfo->our_scsiid]; 3444 if (tstate != NULL) { 3445 struct ahc_tmode_lstate* lstate; 3446 3447 lstate = tstate->enabled_luns[devinfo->lun]; 3448 if (lstate != NULL) { 3449 ahc_queue_lstate_event(ahc, lstate, 3450 devinfo->our_scsiid, 3451 ahc->msgin_buf[0], 3452 /*arg*/tag); 3453 ahc_send_lstate_events(ahc, lstate); 3454 } 3455 } 3456 ahc_restart(ahc); 3457 done = MSGLOOP_TERMINATED; 3458 break; 3459 } 3460 #endif 3461 case MSG_TERM_IO_PROC: 3462 default: 3463 reject = TRUE; 3464 break; 3465 } 3466 3467 if (reject) { 3468 /* 3469 * Setup to reject the message. 3470 */ 3471 ahc->msgout_index = 0; 3472 ahc->msgout_len = 1; 3473 ahc->msgout_buf[0] = MSG_MESSAGE_REJECT; 3474 done = MSGLOOP_MSGCOMPLETE; 3475 response = TRUE; 3476 } 3477 3478 if (done != MSGLOOP_IN_PROG && !response) 3479 /* Clear the outgoing message buffer */ 3480 ahc->msgout_len = 0; 3481 3482 return (done); 3483 } 3484 3485 /* 3486 * Process a message reject message. 3487 */ 3488 static int 3489 ahc_handle_msg_reject(struct ahc_softc *ahc, struct ahc_devinfo *devinfo) 3490 { 3491 /* 3492 * What we care about here is if we had an 3493 * outstanding SDTR or WDTR message for this 3494 * target. If we did, this is a signal that 3495 * the target is refusing negotiation. 3496 */ 3497 struct scb *scb; 3498 struct ahc_initiator_tinfo *tinfo; 3499 struct ahc_tmode_tstate *tstate; 3500 u_int scb_index; 3501 u_int last_msg; 3502 int response = 0; 3503 3504 scb_index = ahc_inb(ahc, SCB_TAG); 3505 scb = ahc_lookup_scb(ahc, scb_index); 3506 tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, 3507 devinfo->our_scsiid, 3508 devinfo->target, &tstate); 3509 /* Might be necessary */ 3510 last_msg = ahc_inb(ahc, LAST_MSG); 3511 3512 if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_PPR, /*full*/FALSE)) { 3513 /* 3514 * Target does not support the PPR message. 3515 * Attempt to negotiate SPI-2 style. 3516 */ 3517 if (bootverbose) { 3518 printf("(%s:%c:%d:%d): PPR Rejected. " 3519 "Trying WDTR/SDTR\n", 3520 ahc_name(ahc), devinfo->channel, 3521 devinfo->target, devinfo->lun); 3522 } 3523 tinfo->goal.ppr_options = 0; 3524 tinfo->curr.transport_version = 2; 3525 tinfo->goal.transport_version = 2; 3526 ahc->msgout_index = 0; 3527 ahc->msgout_len = 0; 3528 ahc_build_transfer_msg(ahc, devinfo); 3529 ahc->msgout_index = 0; 3530 response = 1; 3531 } else if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_WDTR, /*full*/FALSE)) { 3532 /* note 8bit xfers */ 3533 printf("(%s:%c:%d:%d): refuses WIDE negotiation. Using " 3534 "8bit transfers\n", ahc_name(ahc), 3535 devinfo->channel, devinfo->target, devinfo->lun); 3536 ahc_set_width(ahc, devinfo, MSG_EXT_WDTR_BUS_8_BIT, 3537 AHC_TRANS_ACTIVE|AHC_TRANS_GOAL, 3538 /*paused*/TRUE); 3539 /* 3540 * No need to clear the sync rate. If the target 3541 * did not accept the command, our syncrate is 3542 * unaffected. If the target started the negotiation, 3543 * but rejected our response, we already cleared the 3544 * sync rate before sending our WDTR. 3545 */ 3546 if (tinfo->goal.offset != tinfo->curr.offset) { 3547 /* Start the sync negotiation */ 3548 ahc->msgout_index = 0; 3549 ahc->msgout_len = 0; 3550 ahc_build_transfer_msg(ahc, devinfo); 3551 ahc->msgout_index = 0; 3552 response = 1; 3553 } 3554 } else if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_SDTR, /*full*/FALSE)) { 3555 /* note asynch xfers and clear flag */ 3556 ahc_set_syncrate(ahc, devinfo, /*syncrate*/NULL, /*period*/0, 3557 /*offset*/0, /*ppr_options*/0, 3558 AHC_TRANS_ACTIVE|AHC_TRANS_GOAL, 3559 /*paused*/TRUE); 3560 printf("(%s:%c:%d:%d): refuses synchronous negotiation. " 3561 "Using asynchronous transfers\n", 3562 ahc_name(ahc), devinfo->channel, 3563 devinfo->target, devinfo->lun); 3564 } else if ((scb->hscb->control & MSG_SIMPLE_TASK) != 0) { 3565 int tag_type; 3566 int mask; 3567 3568 tag_type = (scb->hscb->control & MSG_SIMPLE_TASK); 3569 3570 if (tag_type == MSG_SIMPLE_TASK) { 3571 printf("(%s:%c:%d:%d): refuses tagged commands. " 3572 "Performing non-tagged I/O\n", ahc_name(ahc), 3573 devinfo->channel, devinfo->target, devinfo->lun); 3574 ahc_set_tags(ahc, devinfo, AHC_QUEUE_NONE); 3575 mask = ~0x23; 3576 } else { 3577 printf("(%s:%c:%d:%d): refuses %s tagged commands. " 3578 "Performing simple queue tagged I/O only\n", 3579 ahc_name(ahc), devinfo->channel, devinfo->target, 3580 devinfo->lun, tag_type == MSG_ORDERED_TASK 3581 ? "ordered" : "head of queue"); 3582 ahc_set_tags(ahc, devinfo, AHC_QUEUE_BASIC); 3583 mask = ~0x03; 3584 } 3585 3586 /* 3587 * Resend the identify for this CCB as the target 3588 * may believe that the selection is invalid otherwise. 3589 */ 3590 ahc_outb(ahc, SCB_CONTROL, 3591 ahc_inb(ahc, SCB_CONTROL) & mask); 3592 scb->hscb->control &= mask; 3593 aic_set_transaction_tag(scb, /*enabled*/FALSE, 3594 /*type*/MSG_SIMPLE_TASK); 3595 ahc_outb(ahc, MSG_OUT, MSG_IDENTIFYFLAG); 3596 ahc_assert_atn(ahc); 3597 3598 /* 3599 * This transaction is now at the head of 3600 * the untagged queue for this target. 3601 */ 3602 if ((ahc->flags & AHC_SCB_BTT) == 0) { 3603 struct scb_tailq *untagged_q; 3604 3605 untagged_q = 3606 &(ahc->untagged_queues[devinfo->target_offset]); 3607 TAILQ_INSERT_HEAD(untagged_q, scb, links.tqe); 3608 scb->flags |= SCB_UNTAGGEDQ; 3609 } 3610 ahc_busy_tcl(ahc, BUILD_TCL(scb->hscb->scsiid, devinfo->lun), 3611 scb->hscb->tag); 3612 3613 /* 3614 * Requeue all tagged commands for this target 3615 * currently in our possession so they can be 3616 * converted to untagged commands. 3617 */ 3618 ahc_search_qinfifo(ahc, SCB_GET_TARGET(ahc, scb), 3619 SCB_GET_CHANNEL(ahc, scb), 3620 SCB_GET_LUN(scb), /*tag*/SCB_LIST_NULL, 3621 ROLE_INITIATOR, CAM_REQUEUE_REQ, 3622 SEARCH_COMPLETE); 3623 } else { 3624 /* 3625 * Otherwise, we ignore it. 3626 */ 3627 printf("%s:%c:%d: Message reject for %x -- ignored\n", 3628 ahc_name(ahc), devinfo->channel, devinfo->target, 3629 last_msg); 3630 } 3631 return (response); 3632 } 3633 3634 /* 3635 * Process an ingnore wide residue message. 3636 */ 3637 static void 3638 ahc_handle_ign_wide_residue(struct ahc_softc *ahc, struct ahc_devinfo *devinfo) 3639 { 3640 u_int scb_index; 3641 struct scb *scb; 3642 3643 scb_index = ahc_inb(ahc, SCB_TAG); 3644 scb = ahc_lookup_scb(ahc, scb_index); 3645 /* 3646 * XXX Actually check data direction in the sequencer? 3647 * Perhaps add datadir to some spare bits in the hscb? 3648 */ 3649 if ((ahc_inb(ahc, SEQ_FLAGS) & DPHASE) == 0 3650 || aic_get_transfer_dir(scb) != CAM_DIR_IN) { 3651 /* 3652 * Ignore the message if we haven't 3653 * seen an appropriate data phase yet. 3654 */ 3655 } else { 3656 /* 3657 * If the residual occurred on the last 3658 * transfer and the transfer request was 3659 * expected to end on an odd count, do 3660 * nothing. Otherwise, subtract a byte 3661 * and update the residual count accordingly. 3662 */ 3663 uint32_t sgptr; 3664 3665 sgptr = ahc_inb(ahc, SCB_RESIDUAL_SGPTR); 3666 if ((sgptr & SG_LIST_NULL) != 0 3667 && (ahc_inb(ahc, SCB_LUN) & SCB_XFERLEN_ODD) != 0) { 3668 /* 3669 * If the residual occurred on the last 3670 * transfer and the transfer request was 3671 * expected to end on an odd count, do 3672 * nothing. 3673 */ 3674 } else { 3675 struct ahc_dma_seg *sg; 3676 uint32_t data_cnt; 3677 uint32_t data_addr; 3678 uint32_t sglen; 3679 3680 /* Pull in all of the sgptr */ 3681 sgptr = ahc_inl(ahc, SCB_RESIDUAL_SGPTR); 3682 data_cnt = ahc_inl(ahc, SCB_RESIDUAL_DATACNT); 3683 3684 if ((sgptr & SG_LIST_NULL) != 0) { 3685 /* 3686 * The residual data count is not updated 3687 * for the command run to completion case. 3688 * Explicitly zero the count. 3689 */ 3690 data_cnt &= ~AHC_SG_LEN_MASK; 3691 } 3692 3693 data_addr = ahc_inl(ahc, SHADDR); 3694 3695 data_cnt += 1; 3696 data_addr -= 1; 3697 sgptr &= SG_PTR_MASK; 3698 3699 sg = ahc_sg_bus_to_virt(scb, sgptr); 3700 3701 /* 3702 * The residual sg ptr points to the next S/G 3703 * to load so we must go back one. 3704 */ 3705 sg--; 3706 sglen = aic_le32toh(sg->len) & AHC_SG_LEN_MASK; 3707 if (sg != scb->sg_list 3708 && sglen < (data_cnt & AHC_SG_LEN_MASK)) { 3709 sg--; 3710 sglen = aic_le32toh(sg->len); 3711 /* 3712 * Preserve High Address and SG_LIST bits 3713 * while setting the count to 1. 3714 */ 3715 data_cnt = 1 | (sglen & (~AHC_SG_LEN_MASK)); 3716 data_addr = aic_le32toh(sg->addr) 3717 + (sglen & AHC_SG_LEN_MASK) - 1; 3718 3719 /* 3720 * Increment sg so it points to the 3721 * "next" sg. 3722 */ 3723 sg++; 3724 sgptr = ahc_sg_virt_to_bus(scb, sg); 3725 } 3726 ahc_outl(ahc, SCB_RESIDUAL_SGPTR, sgptr); 3727 ahc_outl(ahc, SCB_RESIDUAL_DATACNT, data_cnt); 3728 /* 3729 * Toggle the "oddness" of the transfer length 3730 * to handle this mid-transfer ignore wide 3731 * residue. This ensures that the oddness is 3732 * correct for subsequent data transfers. 3733 */ 3734 ahc_outb(ahc, SCB_LUN, 3735 ahc_inb(ahc, SCB_LUN) ^ SCB_XFERLEN_ODD); 3736 } 3737 } 3738 } 3739 3740 /* 3741 * Reinitialize the data pointers for the active transfer 3742 * based on its current residual. 3743 */ 3744 static void 3745 ahc_reinitialize_dataptrs(struct ahc_softc *ahc) 3746 { 3747 struct scb *scb; 3748 struct ahc_dma_seg *sg; 3749 u_int scb_index; 3750 uint32_t sgptr; 3751 uint32_t resid; 3752 uint32_t dataptr; 3753 3754 scb_index = ahc_inb(ahc, SCB_TAG); 3755 scb = ahc_lookup_scb(ahc, scb_index); 3756 sgptr = (ahc_inb(ahc, SCB_RESIDUAL_SGPTR + 3) << 24) 3757 | (ahc_inb(ahc, SCB_RESIDUAL_SGPTR + 2) << 16) 3758 | (ahc_inb(ahc, SCB_RESIDUAL_SGPTR + 1) << 8) 3759 | ahc_inb(ahc, SCB_RESIDUAL_SGPTR); 3760 3761 sgptr &= SG_PTR_MASK; 3762 sg = ahc_sg_bus_to_virt(scb, sgptr); 3763 3764 /* The residual sg_ptr always points to the next sg */ 3765 sg--; 3766 3767 resid = (ahc_inb(ahc, SCB_RESIDUAL_DATACNT + 2) << 16) 3768 | (ahc_inb(ahc, SCB_RESIDUAL_DATACNT + 1) << 8) 3769 | ahc_inb(ahc, SCB_RESIDUAL_DATACNT); 3770 3771 dataptr = aic_le32toh(sg->addr) 3772 + (aic_le32toh(sg->len) & AHC_SG_LEN_MASK) 3773 - resid; 3774 if ((ahc->flags & AHC_39BIT_ADDRESSING) != 0) { 3775 u_int dscommand1; 3776 3777 dscommand1 = ahc_inb(ahc, DSCOMMAND1); 3778 ahc_outb(ahc, DSCOMMAND1, dscommand1 | HADDLDSEL0); 3779 ahc_outb(ahc, HADDR, 3780 (aic_le32toh(sg->len) >> 24) & SG_HIGH_ADDR_BITS); 3781 ahc_outb(ahc, DSCOMMAND1, dscommand1); 3782 } 3783 ahc_outb(ahc, HADDR + 3, dataptr >> 24); 3784 ahc_outb(ahc, HADDR + 2, dataptr >> 16); 3785 ahc_outb(ahc, HADDR + 1, dataptr >> 8); 3786 ahc_outb(ahc, HADDR, dataptr); 3787 ahc_outb(ahc, HCNT + 2, resid >> 16); 3788 ahc_outb(ahc, HCNT + 1, resid >> 8); 3789 ahc_outb(ahc, HCNT, resid); 3790 if ((ahc->features & AHC_ULTRA2) == 0) { 3791 ahc_outb(ahc, STCNT + 2, resid >> 16); 3792 ahc_outb(ahc, STCNT + 1, resid >> 8); 3793 ahc_outb(ahc, STCNT, resid); 3794 } 3795 } 3796 3797 /* 3798 * Handle the effects of issuing a bus device reset message. 3799 */ 3800 static void 3801 ahc_handle_devreset(struct ahc_softc *ahc, struct ahc_devinfo *devinfo, 3802 cam_status status, char *message, int verbose_level) 3803 { 3804 #ifdef AHC_TARGET_MODE 3805 struct ahc_tmode_tstate* tstate; 3806 u_int lun; 3807 #endif 3808 int found; 3809 3810 found = ahc_abort_scbs(ahc, devinfo->target, devinfo->channel, 3811 CAM_LUN_WILDCARD, SCB_LIST_NULL, devinfo->role, 3812 status); 3813 3814 #ifdef AHC_TARGET_MODE 3815 /* 3816 * Send an immediate notify ccb to all target mord peripheral 3817 * drivers affected by this action. 3818 */ 3819 tstate = ahc->enabled_targets[devinfo->our_scsiid]; 3820 if (tstate != NULL) { 3821 for (lun = 0; lun < AHC_NUM_LUNS; lun++) { 3822 struct ahc_tmode_lstate* lstate; 3823 3824 lstate = tstate->enabled_luns[lun]; 3825 if (lstate == NULL) 3826 continue; 3827 3828 ahc_queue_lstate_event(ahc, lstate, devinfo->our_scsiid, 3829 MSG_BUS_DEV_RESET, /*arg*/0); 3830 ahc_send_lstate_events(ahc, lstate); 3831 } 3832 } 3833 #endif 3834 3835 /* 3836 * Go back to async/narrow transfers and renegotiate. 3837 */ 3838 ahc_set_width(ahc, devinfo, MSG_EXT_WDTR_BUS_8_BIT, 3839 AHC_TRANS_CUR, /*paused*/TRUE); 3840 ahc_set_syncrate(ahc, devinfo, /*syncrate*/NULL, 3841 /*period*/0, /*offset*/0, /*ppr_options*/0, 3842 AHC_TRANS_CUR, /*paused*/TRUE); 3843 3844 if (status != CAM_SEL_TIMEOUT) 3845 ahc_send_async(ahc, devinfo->channel, devinfo->target, 3846 CAM_LUN_WILDCARD, AC_SENT_BDR, NULL); 3847 3848 if (message != NULL 3849 && (verbose_level <= bootverbose)) 3850 printf("%s: %s on %c:%d. %d SCBs aborted\n", ahc_name(ahc), 3851 message, devinfo->channel, devinfo->target, found); 3852 } 3853 3854 #ifdef AHC_TARGET_MODE 3855 static void 3856 ahc_setup_target_msgin(struct ahc_softc *ahc, struct ahc_devinfo *devinfo, 3857 struct scb *scb) 3858 { 3859 3860 /* 3861 * To facilitate adding multiple messages together, 3862 * each routine should increment the index and len 3863 * variables instead of setting them explicitly. 3864 */ 3865 ahc->msgout_index = 0; 3866 ahc->msgout_len = 0; 3867 3868 if (scb != NULL && (scb->flags & SCB_AUTO_NEGOTIATE) != 0) 3869 ahc_build_transfer_msg(ahc, devinfo); 3870 else 3871 panic("ahc_intr: AWAITING target message with no message"); 3872 3873 ahc->msgout_index = 0; 3874 ahc->msg_type = MSG_TYPE_TARGET_MSGIN; 3875 } 3876 #endif 3877 /**************************** Initialization **********************************/ 3878 /* 3879 * Allocate a controller structure for a new device 3880 * and perform initial initializion. 3881 */ 3882 struct ahc_softc * 3883 ahc_alloc(void *platform_arg, char *name) 3884 { 3885 struct ahc_softc *ahc; 3886 int i; 3887 3888 #ifndef __FreeBSD__ 3889 ahc = malloc(sizeof(*ahc), M_DEVBUF, M_NOWAIT); 3890 if (!ahc) { 3891 printf("aic7xxx: cannot malloc softc!\n"); 3892 free(name, M_DEVBUF); 3893 return NULL; 3894 } 3895 #else 3896 ahc = device_get_softc((device_t)platform_arg); 3897 #endif 3898 memset(ahc, 0, sizeof(*ahc)); 3899 ahc->seep_config = malloc(sizeof(*ahc->seep_config), 3900 M_DEVBUF, M_NOWAIT); 3901 if (ahc->seep_config == NULL) { 3902 #ifndef __FreeBSD__ 3903 free(ahc, M_DEVBUF); 3904 #endif 3905 free(name, M_DEVBUF); 3906 return (NULL); 3907 } 3908 LIST_INIT(&ahc->pending_scbs); 3909 LIST_INIT(&ahc->timedout_scbs); 3910 /* We don't know our unit number until the OSM sets it */ 3911 ahc->name = name; 3912 ahc->unit = -1; 3913 ahc->description = NULL; 3914 ahc->channel = 'A'; 3915 ahc->channel_b = 'B'; 3916 ahc->chip = AHC_NONE; 3917 ahc->features = AHC_FENONE; 3918 ahc->bugs = AHC_BUGNONE; 3919 ahc->flags = AHC_FNONE; 3920 /* 3921 * Default to all error reporting enabled with the 3922 * sequencer operating at its fastest speed. 3923 * The bus attach code may modify this. 3924 */ 3925 ahc->seqctl = FASTMODE; 3926 3927 for (i = 0; i < AHC_NUM_TARGETS; i++) 3928 TAILQ_INIT(&ahc->untagged_queues[i]); 3929 if (ahc_platform_alloc(ahc, platform_arg) != 0) { 3930 ahc_free(ahc); 3931 ahc = NULL; 3932 } 3933 ahc_lockinit(ahc); 3934 return (ahc); 3935 } 3936 3937 int 3938 ahc_softc_init(struct ahc_softc *ahc) 3939 { 3940 3941 /* The IRQMS bit is only valid on VL and EISA chips */ 3942 if ((ahc->chip & AHC_PCI) == 0) 3943 ahc->unpause = ahc_inb(ahc, HCNTRL) & IRQMS; 3944 else 3945 ahc->unpause = 0; 3946 ahc->pause = ahc->unpause | PAUSE; 3947 /* XXX The shared scb data stuff should be deprecated */ 3948 if (ahc->scb_data == NULL) { 3949 ahc->scb_data = malloc(sizeof(*ahc->scb_data), 3950 M_DEVBUF, M_NOWAIT); 3951 if (ahc->scb_data == NULL) 3952 return (ENOMEM); 3953 memset(ahc->scb_data, 0, sizeof(*ahc->scb_data)); 3954 } 3955 3956 return (0); 3957 } 3958 3959 void 3960 ahc_softc_insert(struct ahc_softc *ahc) 3961 { 3962 struct ahc_softc *list_ahc; 3963 3964 #if AIC_PCI_CONFIG > 0 3965 /* 3966 * Second Function PCI devices need to inherit some 3967 * settings from function 0. 3968 */ 3969 if ((ahc->chip & AHC_BUS_MASK) == AHC_PCI 3970 && (ahc->features & AHC_MULTI_FUNC) != 0) { 3971 TAILQ_FOREACH(list_ahc, &ahc_tailq, links) { 3972 aic_dev_softc_t list_pci; 3973 aic_dev_softc_t pci; 3974 3975 list_pci = list_ahc->dev_softc; 3976 pci = ahc->dev_softc; 3977 if (aic_get_pci_slot(list_pci) == aic_get_pci_slot(pci) 3978 && aic_get_pci_bus(list_pci) == aic_get_pci_bus(pci)) { 3979 struct ahc_softc *master; 3980 struct ahc_softc *slave; 3981 3982 if (aic_get_pci_function(list_pci) == 0) { 3983 master = list_ahc; 3984 slave = ahc; 3985 } else { 3986 master = ahc; 3987 slave = list_ahc; 3988 } 3989 slave->flags &= ~AHC_BIOS_ENABLED; 3990 slave->flags |= 3991 master->flags & AHC_BIOS_ENABLED; 3992 slave->flags &= ~AHC_PRIMARY_CHANNEL; 3993 slave->flags |= 3994 master->flags & AHC_PRIMARY_CHANNEL; 3995 break; 3996 } 3997 } 3998 } 3999 #endif 4000 4001 /* 4002 * Insertion sort into our list of softcs. 4003 */ 4004 list_ahc = TAILQ_FIRST(&ahc_tailq); 4005 while (list_ahc != NULL 4006 && ahc_softc_comp(ahc, list_ahc) <= 0) 4007 list_ahc = TAILQ_NEXT(list_ahc, links); 4008 if (list_ahc != NULL) 4009 TAILQ_INSERT_BEFORE(list_ahc, ahc, links); 4010 else 4011 TAILQ_INSERT_TAIL(&ahc_tailq, ahc, links); 4012 ahc->init_level++; 4013 } 4014 4015 void 4016 ahc_set_unit(struct ahc_softc *ahc, int unit) 4017 { 4018 ahc->unit = unit; 4019 } 4020 4021 void 4022 ahc_set_name(struct ahc_softc *ahc, char *name) 4023 { 4024 if (ahc->name != NULL) 4025 free(ahc->name, M_DEVBUF); 4026 ahc->name = name; 4027 } 4028 4029 void 4030 ahc_free(struct ahc_softc *ahc) 4031 { 4032 int i; 4033 4034 ahc_terminate_recovery_thread(ahc); 4035 switch (ahc->init_level) { 4036 default: 4037 case 5: 4038 ahc_shutdown(ahc); 4039 /* FALLTHROUGH */ 4040 case 4: 4041 aic_dmamap_unload(ahc, ahc->shared_data_dmat, 4042 ahc->shared_data_dmamap); 4043 /* FALLTHROUGH */ 4044 case 3: 4045 aic_dmamem_free(ahc, ahc->shared_data_dmat, ahc->qoutfifo, 4046 ahc->shared_data_dmamap); 4047 /* FALLTHROUGH */ 4048 case 2: 4049 aic_dma_tag_destroy(ahc, ahc->shared_data_dmat); 4050 case 1: 4051 #ifndef __linux__ 4052 aic_dma_tag_destroy(ahc, ahc->buffer_dmat); 4053 #endif 4054 break; 4055 case 0: 4056 break; 4057 } 4058 4059 #ifndef __linux__ 4060 aic_dma_tag_destroy(ahc, ahc->parent_dmat); 4061 #endif 4062 ahc_platform_free(ahc); 4063 ahc_fini_scbdata(ahc); 4064 for (i = 0; i < AHC_NUM_TARGETS; i++) { 4065 struct ahc_tmode_tstate *tstate; 4066 4067 tstate = ahc->enabled_targets[i]; 4068 if (tstate != NULL) { 4069 #ifdef AHC_TARGET_MODE 4070 int j; 4071 4072 for (j = 0; j < AHC_NUM_LUNS; j++) { 4073 struct ahc_tmode_lstate *lstate; 4074 4075 lstate = tstate->enabled_luns[j]; 4076 if (lstate != NULL) { 4077 xpt_free_path(lstate->path); 4078 free(lstate, M_DEVBUF); 4079 } 4080 } 4081 #endif 4082 free(tstate, M_DEVBUF); 4083 } 4084 } 4085 #ifdef AHC_TARGET_MODE 4086 if (ahc->black_hole != NULL) { 4087 xpt_free_path(ahc->black_hole->path); 4088 free(ahc->black_hole, M_DEVBUF); 4089 } 4090 #endif 4091 if (ahc->name != NULL) 4092 free(ahc->name, M_DEVBUF); 4093 if (ahc->seep_config != NULL) 4094 free(ahc->seep_config, M_DEVBUF); 4095 #ifndef __FreeBSD__ 4096 free(ahc, M_DEVBUF); 4097 #endif 4098 return; 4099 } 4100 4101 void 4102 ahc_shutdown(void *arg) 4103 { 4104 struct ahc_softc *ahc; 4105 int i; 4106 4107 ahc = (struct ahc_softc *)arg; 4108 4109 /* This will reset most registers to 0, but not all */ 4110 ahc_reset(ahc, /*reinit*/FALSE); 4111 ahc_outb(ahc, SCSISEQ, 0); 4112 ahc_outb(ahc, SXFRCTL0, 0); 4113 ahc_outb(ahc, DSPCISTATUS, 0); 4114 4115 for (i = TARG_SCSIRATE; i < SCSICONF; i++) 4116 ahc_outb(ahc, i, 0); 4117 } 4118 4119 /* 4120 * Reset the controller and record some information about it 4121 * that is only available just after a reset. If "reinit" is 4122 * non-zero, this reset occurred after initial configuration 4123 * and the caller requests that the chip be fully reinitialized 4124 * to a runable state. Chip interrupts are *not* enabled after 4125 * a reinitialization. The caller must enable interrupts via 4126 * ahc_intr_enable(). 4127 */ 4128 int 4129 ahc_reset(struct ahc_softc *ahc, int reinit) 4130 { 4131 u_int sblkctl; 4132 u_int sxfrctl1_a, sxfrctl1_b; 4133 int error; 4134 int wait; 4135 4136 /* 4137 * Preserve the value of the SXFRCTL1 register for all channels. 4138 * It contains settings that affect termination and we don't want 4139 * to disturb the integrity of the bus. 4140 */ 4141 ahc_pause(ahc); 4142 sxfrctl1_b = 0; 4143 if ((ahc->chip & AHC_CHIPID_MASK) == AHC_AIC7770) { 4144 u_int sblkctl; 4145 4146 /* 4147 * Save channel B's settings in case this chip 4148 * is setup for TWIN channel operation. 4149 */ 4150 sblkctl = ahc_inb(ahc, SBLKCTL); 4151 ahc_outb(ahc, SBLKCTL, sblkctl | SELBUSB); 4152 sxfrctl1_b = ahc_inb(ahc, SXFRCTL1); 4153 ahc_outb(ahc, SBLKCTL, sblkctl & ~SELBUSB); 4154 } 4155 sxfrctl1_a = ahc_inb(ahc, SXFRCTL1); 4156 4157 ahc_outb(ahc, HCNTRL, CHIPRST | ahc->pause); 4158 4159 /* 4160 * Ensure that the reset has finished. We delay 1000us 4161 * prior to reading the register to make sure the chip 4162 * has sufficiently completed its reset to handle register 4163 * accesses. 4164 */ 4165 wait = 1000; 4166 do { 4167 aic_delay(1000); 4168 } while (--wait && !(ahc_inb(ahc, HCNTRL) & CHIPRSTACK)); 4169 4170 if (wait == 0) { 4171 printf("%s: WARNING - Failed chip reset! " 4172 "Trying to initialize anyway.\n", ahc_name(ahc)); 4173 } 4174 ahc_outb(ahc, HCNTRL, ahc->pause); 4175 4176 /* Determine channel configuration */ 4177 sblkctl = ahc_inb(ahc, SBLKCTL) & (SELBUSB|SELWIDE); 4178 /* No Twin Channel PCI cards */ 4179 if ((ahc->chip & AHC_PCI) != 0) 4180 sblkctl &= ~SELBUSB; 4181 switch (sblkctl) { 4182 case 0: 4183 /* Single Narrow Channel */ 4184 break; 4185 case 2: 4186 /* Wide Channel */ 4187 ahc->features |= AHC_WIDE; 4188 break; 4189 case 8: 4190 /* Twin Channel */ 4191 ahc->features |= AHC_TWIN; 4192 break; 4193 default: 4194 printf(" Unsupported adapter type. Ignoring\n"); 4195 return(-1); 4196 } 4197 4198 /* 4199 * Reload sxfrctl1. 4200 * 4201 * We must always initialize STPWEN to 1 before we 4202 * restore the saved values. STPWEN is initialized 4203 * to a tri-state condition which can only be cleared 4204 * by turning it on. 4205 */ 4206 if ((ahc->features & AHC_TWIN) != 0) { 4207 u_int sblkctl; 4208 4209 sblkctl = ahc_inb(ahc, SBLKCTL); 4210 ahc_outb(ahc, SBLKCTL, sblkctl | SELBUSB); 4211 ahc_outb(ahc, SXFRCTL1, sxfrctl1_b); 4212 ahc_outb(ahc, SBLKCTL, sblkctl & ~SELBUSB); 4213 } 4214 ahc_outb(ahc, SXFRCTL1, sxfrctl1_a); 4215 4216 error = 0; 4217 if (reinit != 0) 4218 /* 4219 * If a recovery action has forced a chip reset, 4220 * re-initialize the chip to our liking. 4221 */ 4222 error = ahc->bus_chip_init(ahc); 4223 #ifdef AHC_DUMP_SEQ 4224 else 4225 ahc_dumpseq(ahc); 4226 #endif 4227 4228 return (error); 4229 } 4230 4231 /* 4232 * Determine the number of SCBs available on the controller 4233 */ 4234 int 4235 ahc_probe_scbs(struct ahc_softc *ahc) { 4236 int i; 4237 4238 for (i = 0; i < AHC_SCB_MAX; i++) { 4239 ahc_outb(ahc, SCBPTR, i); 4240 ahc_outb(ahc, SCB_BASE, i); 4241 if (ahc_inb(ahc, SCB_BASE) != i) 4242 break; 4243 ahc_outb(ahc, SCBPTR, 0); 4244 if (ahc_inb(ahc, SCB_BASE) != 0) 4245 break; 4246 } 4247 return (i); 4248 } 4249 4250 static void 4251 ahc_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error) 4252 { 4253 bus_addr_t *baddr; 4254 4255 baddr = (bus_addr_t *)arg; 4256 *baddr = segs->ds_addr; 4257 } 4258 4259 static void 4260 ahc_build_free_scb_list(struct ahc_softc *ahc) 4261 { 4262 int scbsize; 4263 int i; 4264 4265 scbsize = 32; 4266 if ((ahc->flags & AHC_LSCBS_ENABLED) != 0) 4267 scbsize = 64; 4268 4269 for (i = 0; i < ahc->scb_data->maxhscbs; i++) { 4270 int j; 4271 4272 ahc_outb(ahc, SCBPTR, i); 4273 4274 /* 4275 * Touch all SCB bytes to avoid parity errors 4276 * should one of our debugging routines read 4277 * an otherwise uninitiatlized byte. 4278 */ 4279 for (j = 0; j < scbsize; j++) 4280 ahc_outb(ahc, SCB_BASE+j, 0xFF); 4281 4282 /* Clear the control byte. */ 4283 ahc_outb(ahc, SCB_CONTROL, 0); 4284 4285 /* Set the next pointer */ 4286 if ((ahc->flags & AHC_PAGESCBS) != 0) 4287 ahc_outb(ahc, SCB_NEXT, i+1); 4288 else 4289 ahc_outb(ahc, SCB_NEXT, SCB_LIST_NULL); 4290 4291 /* Make the tag number, SCSIID, and lun invalid */ 4292 ahc_outb(ahc, SCB_TAG, SCB_LIST_NULL); 4293 ahc_outb(ahc, SCB_SCSIID, 0xFF); 4294 ahc_outb(ahc, SCB_LUN, 0xFF); 4295 } 4296 4297 if ((ahc->flags & AHC_PAGESCBS) != 0) { 4298 /* SCB 0 heads the free list. */ 4299 ahc_outb(ahc, FREE_SCBH, 0); 4300 } else { 4301 /* No free list. */ 4302 ahc_outb(ahc, FREE_SCBH, SCB_LIST_NULL); 4303 } 4304 4305 /* Make sure that the last SCB terminates the free list */ 4306 ahc_outb(ahc, SCBPTR, i-1); 4307 ahc_outb(ahc, SCB_NEXT, SCB_LIST_NULL); 4308 } 4309 4310 static int 4311 ahc_init_scbdata(struct ahc_softc *ahc) 4312 { 4313 struct scb_data *scb_data; 4314 4315 scb_data = ahc->scb_data; 4316 SLIST_INIT(&scb_data->free_scbs); 4317 SLIST_INIT(&scb_data->sg_maps); 4318 4319 /* Allocate SCB resources */ 4320 scb_data->scbarray = 4321 (struct scb *)malloc(sizeof(struct scb) * AHC_SCB_MAX_ALLOC, 4322 M_DEVBUF, M_NOWAIT); 4323 if (scb_data->scbarray == NULL) 4324 return (ENOMEM); 4325 memset(scb_data->scbarray, 0, sizeof(struct scb) * AHC_SCB_MAX_ALLOC); 4326 4327 /* Determine the number of hardware SCBs and initialize them */ 4328 4329 scb_data->maxhscbs = ahc_probe_scbs(ahc); 4330 if (ahc->scb_data->maxhscbs == 0) { 4331 printf("%s: No SCB space found\n", ahc_name(ahc)); 4332 return (ENXIO); 4333 } 4334 4335 /* 4336 * Create our DMA tags. These tags define the kinds of device 4337 * accessible memory allocations and memory mappings we will 4338 * need to perform during normal operation. 4339 * 4340 * Unless we need to further restrict the allocation, we rely 4341 * on the restrictions of the parent dmat, hence the common 4342 * use of MAXADDR and MAXSIZE. 4343 */ 4344 4345 /* DMA tag for our hardware scb structures */ 4346 if (aic_dma_tag_create(ahc, ahc->parent_dmat, /*alignment*/1, 4347 /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1, 4348 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT, 4349 /*highaddr*/BUS_SPACE_MAXADDR, 4350 /*filter*/NULL, /*filterarg*/NULL, 4351 AHC_SCB_MAX_ALLOC * sizeof(struct hardware_scb), 4352 /*nsegments*/1, 4353 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT, 4354 /*flags*/0, &scb_data->hscb_dmat) != 0) { 4355 goto error_exit; 4356 } 4357 4358 scb_data->init_level++; 4359 4360 /* Allocation for our hscbs */ 4361 if (aic_dmamem_alloc(ahc, scb_data->hscb_dmat, 4362 (void **)&scb_data->hscbs, 4363 BUS_DMA_NOWAIT | BUS_DMA_COHERENT, 4364 &scb_data->hscb_dmamap) != 0) { 4365 goto error_exit; 4366 } 4367 4368 scb_data->init_level++; 4369 4370 /* And permanently map them */ 4371 aic_dmamap_load(ahc, scb_data->hscb_dmat, scb_data->hscb_dmamap, 4372 scb_data->hscbs, 4373 AHC_SCB_MAX_ALLOC * sizeof(struct hardware_scb), 4374 ahc_dmamap_cb, &scb_data->hscb_busaddr, /*flags*/0); 4375 4376 scb_data->init_level++; 4377 4378 /* DMA tag for our sense buffers */ 4379 if (aic_dma_tag_create(ahc, ahc->parent_dmat, /*alignment*/1, 4380 /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1, 4381 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT, 4382 /*highaddr*/BUS_SPACE_MAXADDR, 4383 /*filter*/NULL, /*filterarg*/NULL, 4384 AHC_SCB_MAX_ALLOC * sizeof(struct scsi_sense_data), 4385 /*nsegments*/1, 4386 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT, 4387 /*flags*/0, &scb_data->sense_dmat) != 0) { 4388 goto error_exit; 4389 } 4390 4391 scb_data->init_level++; 4392 4393 /* Allocate them */ 4394 if (aic_dmamem_alloc(ahc, scb_data->sense_dmat, 4395 (void **)&scb_data->sense, 4396 BUS_DMA_NOWAIT, &scb_data->sense_dmamap) != 0) { 4397 goto error_exit; 4398 } 4399 4400 scb_data->init_level++; 4401 4402 /* And permanently map them */ 4403 aic_dmamap_load(ahc, scb_data->sense_dmat, scb_data->sense_dmamap, 4404 scb_data->sense, 4405 AHC_SCB_MAX_ALLOC * sizeof(struct scsi_sense_data), 4406 ahc_dmamap_cb, &scb_data->sense_busaddr, /*flags*/0); 4407 4408 scb_data->init_level++; 4409 4410 /* DMA tag for our S/G structures. We allocate in page sized chunks */ 4411 if (aic_dma_tag_create(ahc, ahc->parent_dmat, /*alignment*/8, 4412 /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1, 4413 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT, 4414 /*highaddr*/BUS_SPACE_MAXADDR, 4415 /*filter*/NULL, /*filterarg*/NULL, 4416 PAGE_SIZE, /*nsegments*/1, 4417 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT, 4418 /*flags*/0, &scb_data->sg_dmat) != 0) { 4419 goto error_exit; 4420 } 4421 4422 scb_data->init_level++; 4423 4424 /* Perform initial CCB allocation */ 4425 memset(scb_data->hscbs, 0, 4426 AHC_SCB_MAX_ALLOC * sizeof(struct hardware_scb)); 4427 while (ahc_alloc_scbs(ahc) != 0) 4428 ; 4429 4430 if (scb_data->numscbs == 0) { 4431 printf("%s: ahc_init_scbdata - " 4432 "Unable to allocate initial scbs\n", 4433 ahc_name(ahc)); 4434 goto error_exit; 4435 } 4436 4437 /* 4438 * Reserve the next queued SCB. 4439 */ 4440 ahc->next_queued_scb = ahc_get_scb(ahc); 4441 4442 /* 4443 * Note that we were successful 4444 */ 4445 return (0); 4446 4447 error_exit: 4448 4449 return (ENOMEM); 4450 } 4451 4452 static void 4453 ahc_fini_scbdata(struct ahc_softc *ahc) 4454 { 4455 struct scb_data *scb_data; 4456 4457 scb_data = ahc->scb_data; 4458 if (scb_data == NULL) 4459 return; 4460 4461 switch (scb_data->init_level) { 4462 default: 4463 case 7: 4464 { 4465 struct sg_map_node *sg_map; 4466 4467 while ((sg_map = SLIST_FIRST(&scb_data->sg_maps))!= NULL) { 4468 SLIST_REMOVE_HEAD(&scb_data->sg_maps, links); 4469 aic_dmamap_unload(ahc, scb_data->sg_dmat, 4470 sg_map->sg_dmamap); 4471 aic_dmamem_free(ahc, scb_data->sg_dmat, 4472 sg_map->sg_vaddr, 4473 sg_map->sg_dmamap); 4474 free(sg_map, M_DEVBUF); 4475 } 4476 aic_dma_tag_destroy(ahc, scb_data->sg_dmat); 4477 } 4478 case 6: 4479 aic_dmamap_unload(ahc, scb_data->sense_dmat, 4480 scb_data->sense_dmamap); 4481 case 5: 4482 aic_dmamem_free(ahc, scb_data->sense_dmat, scb_data->sense, 4483 scb_data->sense_dmamap); 4484 case 4: 4485 aic_dma_tag_destroy(ahc, scb_data->sense_dmat); 4486 case 3: 4487 aic_dmamap_unload(ahc, scb_data->hscb_dmat, 4488 scb_data->hscb_dmamap); 4489 case 2: 4490 aic_dmamem_free(ahc, scb_data->hscb_dmat, scb_data->hscbs, 4491 scb_data->hscb_dmamap); 4492 case 1: 4493 aic_dma_tag_destroy(ahc, scb_data->hscb_dmat); 4494 break; 4495 case 0: 4496 break; 4497 } 4498 if (scb_data->scbarray != NULL) 4499 free(scb_data->scbarray, M_DEVBUF); 4500 } 4501 4502 int 4503 ahc_alloc_scbs(struct ahc_softc *ahc) 4504 { 4505 struct scb_data *scb_data; 4506 struct scb *next_scb; 4507 struct sg_map_node *sg_map; 4508 bus_addr_t physaddr; 4509 struct ahc_dma_seg *segs; 4510 int newcount; 4511 int i; 4512 4513 scb_data = ahc->scb_data; 4514 if (scb_data->numscbs >= AHC_SCB_MAX_ALLOC) 4515 /* Can't allocate any more */ 4516 return (0); 4517 4518 next_scb = &scb_data->scbarray[scb_data->numscbs]; 4519 4520 sg_map = malloc(sizeof(*sg_map), M_DEVBUF, M_NOWAIT); 4521 4522 if (sg_map == NULL) 4523 return (0); 4524 4525 /* Allocate S/G space for the next batch of SCBS */ 4526 if (aic_dmamem_alloc(ahc, scb_data->sg_dmat, 4527 (void **)&sg_map->sg_vaddr, 4528 BUS_DMA_NOWAIT | BUS_DMA_COHERENT, 4529 &sg_map->sg_dmamap) != 0) { 4530 free(sg_map, M_DEVBUF); 4531 return (0); 4532 } 4533 4534 SLIST_INSERT_HEAD(&scb_data->sg_maps, sg_map, links); 4535 4536 aic_dmamap_load(ahc, scb_data->sg_dmat, sg_map->sg_dmamap, 4537 sg_map->sg_vaddr, PAGE_SIZE, ahc_dmamap_cb, 4538 &sg_map->sg_physaddr, /*flags*/0); 4539 4540 segs = sg_map->sg_vaddr; 4541 physaddr = sg_map->sg_physaddr; 4542 4543 newcount = (PAGE_SIZE / (AHC_NSEG * sizeof(struct ahc_dma_seg))); 4544 newcount = MIN(newcount, (AHC_SCB_MAX_ALLOC - scb_data->numscbs)); 4545 for (i = 0; i < newcount; i++) { 4546 struct scb_platform_data *pdata; 4547 #ifndef __linux__ 4548 int error; 4549 #endif 4550 pdata = (struct scb_platform_data *)malloc(sizeof(*pdata), 4551 M_DEVBUF, M_NOWAIT); 4552 if (pdata == NULL) 4553 break; 4554 next_scb->platform_data = pdata; 4555 next_scb->sg_map = sg_map; 4556 next_scb->sg_list = segs; 4557 /* 4558 * The sequencer always starts with the second entry. 4559 * The first entry is embedded in the scb. 4560 */ 4561 next_scb->sg_list_phys = physaddr + sizeof(struct ahc_dma_seg); 4562 next_scb->ahc_softc = ahc; 4563 next_scb->flags = SCB_FLAG_NONE; 4564 #ifndef __linux__ 4565 error = aic_dmamap_create(ahc, ahc->buffer_dmat, /*flags*/0, 4566 &next_scb->dmamap); 4567 if (error != 0) 4568 break; 4569 #endif 4570 next_scb->hscb = &scb_data->hscbs[scb_data->numscbs]; 4571 next_scb->hscb->tag = ahc->scb_data->numscbs; 4572 aic_timer_init(&next_scb->io_timer); 4573 SLIST_INSERT_HEAD(&ahc->scb_data->free_scbs, 4574 next_scb, links.sle); 4575 segs += AHC_NSEG; 4576 physaddr += (AHC_NSEG * sizeof(struct ahc_dma_seg)); 4577 next_scb++; 4578 ahc->scb_data->numscbs++; 4579 } 4580 return (i); 4581 } 4582 4583 void 4584 ahc_controller_info(struct ahc_softc *ahc, char *buf) 4585 { 4586 int len; 4587 4588 len = sprintf(buf, "%s: ", ahc_chip_names[ahc->chip & AHC_CHIPID_MASK]); 4589 buf += len; 4590 if ((ahc->features & AHC_TWIN) != 0) 4591 len = sprintf(buf, "Twin Channel, A SCSI Id=%d, " 4592 "B SCSI Id=%d, primary %c, ", 4593 ahc->our_id, ahc->our_id_b, 4594 (ahc->flags & AHC_PRIMARY_CHANNEL) + 'A'); 4595 else { 4596 const char *speed; 4597 const char *type; 4598 4599 speed = ""; 4600 if ((ahc->features & AHC_ULTRA) != 0) { 4601 speed = "Ultra "; 4602 } else if ((ahc->features & AHC_DT) != 0) { 4603 speed = "Ultra160 "; 4604 } else if ((ahc->features & AHC_ULTRA2) != 0) { 4605 speed = "Ultra2 "; 4606 } 4607 if ((ahc->features & AHC_WIDE) != 0) { 4608 type = "Wide"; 4609 } else { 4610 type = "Single"; 4611 } 4612 len = sprintf(buf, "%s%s Channel %c, SCSI Id=%d, ", 4613 speed, type, ahc->channel, ahc->our_id); 4614 } 4615 buf += len; 4616 4617 if ((ahc->flags & AHC_PAGESCBS) != 0) 4618 sprintf(buf, "%d/%d SCBs", 4619 ahc->scb_data->maxhscbs, AHC_MAX_QUEUE); 4620 else 4621 sprintf(buf, "%d SCBs", ahc->scb_data->maxhscbs); 4622 } 4623 4624 int 4625 ahc_chip_init(struct ahc_softc *ahc) 4626 { 4627 int term; 4628 int error; 4629 u_int i; 4630 u_int scsi_conf; 4631 u_int scsiseq_template; 4632 uint32_t physaddr; 4633 4634 ahc_outb(ahc, SEQ_FLAGS, 0); 4635 ahc_outb(ahc, SEQ_FLAGS2, 0); 4636 4637 /* Set the SCSI Id, SXFRCTL0, SXFRCTL1, and SIMODE1, for both channels*/ 4638 if (ahc->features & AHC_TWIN) { 4639 /* 4640 * Setup Channel B first. 4641 */ 4642 ahc_outb(ahc, SBLKCTL, ahc_inb(ahc, SBLKCTL) | SELBUSB); 4643 term = (ahc->flags & AHC_TERM_ENB_B) != 0 ? STPWEN : 0; 4644 ahc_outb(ahc, SCSIID, ahc->our_id_b); 4645 scsi_conf = ahc_inb(ahc, SCSICONF + 1); 4646 ahc_outb(ahc, SXFRCTL1, (scsi_conf & (ENSPCHK|STIMESEL)) 4647 |term|ahc->seltime_b|ENSTIMER|ACTNEGEN); 4648 if ((ahc->features & AHC_ULTRA2) != 0) 4649 ahc_outb(ahc, SIMODE0, ahc_inb(ahc, SIMODE0)|ENIOERR); 4650 ahc_outb(ahc, SIMODE1, ENSELTIMO|ENSCSIRST|ENSCSIPERR); 4651 ahc_outb(ahc, SXFRCTL0, DFON|SPIOEN); 4652 4653 /* Select Channel A */ 4654 ahc_outb(ahc, SBLKCTL, ahc_inb(ahc, SBLKCTL) & ~SELBUSB); 4655 } 4656 term = (ahc->flags & AHC_TERM_ENB_A) != 0 ? STPWEN : 0; 4657 if ((ahc->features & AHC_ULTRA2) != 0) 4658 ahc_outb(ahc, SCSIID_ULTRA2, ahc->our_id); 4659 else 4660 ahc_outb(ahc, SCSIID, ahc->our_id); 4661 scsi_conf = ahc_inb(ahc, SCSICONF); 4662 ahc_outb(ahc, SXFRCTL1, (scsi_conf & (ENSPCHK|STIMESEL)) 4663 |term|ahc->seltime 4664 |ENSTIMER|ACTNEGEN); 4665 if ((ahc->features & AHC_ULTRA2) != 0) 4666 ahc_outb(ahc, SIMODE0, ahc_inb(ahc, SIMODE0)|ENIOERR); 4667 ahc_outb(ahc, SIMODE1, ENSELTIMO|ENSCSIRST|ENSCSIPERR); 4668 ahc_outb(ahc, SXFRCTL0, DFON|SPIOEN); 4669 4670 /* There are no untagged SCBs active yet. */ 4671 for (i = 0; i < 16; i++) { 4672 ahc_unbusy_tcl(ahc, BUILD_TCL(i << 4, 0)); 4673 if ((ahc->flags & AHC_SCB_BTT) != 0) { 4674 int lun; 4675 4676 /* 4677 * The SCB based BTT allows an entry per 4678 * target and lun pair. 4679 */ 4680 for (lun = 1; lun < AHC_NUM_LUNS; lun++) 4681 ahc_unbusy_tcl(ahc, BUILD_TCL(i << 4, lun)); 4682 } 4683 } 4684 4685 /* All of our queues are empty */ 4686 for (i = 0; i < 256; i++) 4687 ahc->qoutfifo[i] = SCB_LIST_NULL; 4688 ahc_sync_qoutfifo(ahc, BUS_DMASYNC_PREREAD); 4689 4690 for (i = 0; i < 256; i++) 4691 ahc->qinfifo[i] = SCB_LIST_NULL; 4692 4693 if ((ahc->features & AHC_MULTI_TID) != 0) { 4694 ahc_outb(ahc, TARGID, 0); 4695 ahc_outb(ahc, TARGID + 1, 0); 4696 } 4697 4698 /* 4699 * Tell the sequencer where it can find our arrays in memory. 4700 */ 4701 physaddr = ahc->scb_data->hscb_busaddr; 4702 ahc_outb(ahc, HSCB_ADDR, physaddr & 0xFF); 4703 ahc_outb(ahc, HSCB_ADDR + 1, (physaddr >> 8) & 0xFF); 4704 ahc_outb(ahc, HSCB_ADDR + 2, (physaddr >> 16) & 0xFF); 4705 ahc_outb(ahc, HSCB_ADDR + 3, (physaddr >> 24) & 0xFF); 4706 4707 physaddr = ahc->shared_data_busaddr; 4708 ahc_outb(ahc, SHARED_DATA_ADDR, physaddr & 0xFF); 4709 ahc_outb(ahc, SHARED_DATA_ADDR + 1, (physaddr >> 8) & 0xFF); 4710 ahc_outb(ahc, SHARED_DATA_ADDR + 2, (physaddr >> 16) & 0xFF); 4711 ahc_outb(ahc, SHARED_DATA_ADDR + 3, (physaddr >> 24) & 0xFF); 4712 4713 /* 4714 * Initialize the group code to command length table. 4715 * This overrides the values in TARG_SCSIRATE, so only 4716 * setup the table after we have processed that information. 4717 */ 4718 ahc_outb(ahc, CMDSIZE_TABLE, 5); 4719 ahc_outb(ahc, CMDSIZE_TABLE + 1, 9); 4720 ahc_outb(ahc, CMDSIZE_TABLE + 2, 9); 4721 ahc_outb(ahc, CMDSIZE_TABLE + 3, 0); 4722 ahc_outb(ahc, CMDSIZE_TABLE + 4, 15); 4723 ahc_outb(ahc, CMDSIZE_TABLE + 5, 11); 4724 ahc_outb(ahc, CMDSIZE_TABLE + 6, 0); 4725 ahc_outb(ahc, CMDSIZE_TABLE + 7, 0); 4726 4727 if ((ahc->features & AHC_HS_MAILBOX) != 0) 4728 ahc_outb(ahc, HS_MAILBOX, 0); 4729 4730 /* Tell the sequencer of our initial queue positions */ 4731 if ((ahc->features & AHC_TARGETMODE) != 0) { 4732 ahc->tqinfifonext = 1; 4733 ahc_outb(ahc, KERNEL_TQINPOS, ahc->tqinfifonext - 1); 4734 ahc_outb(ahc, TQINPOS, ahc->tqinfifonext); 4735 } 4736 ahc->qinfifonext = 0; 4737 ahc->qoutfifonext = 0; 4738 if ((ahc->features & AHC_QUEUE_REGS) != 0) { 4739 ahc_outb(ahc, QOFF_CTLSTA, SCB_QSIZE_256); 4740 ahc_outb(ahc, HNSCB_QOFF, ahc->qinfifonext); 4741 ahc_outb(ahc, SNSCB_QOFF, ahc->qinfifonext); 4742 ahc_outb(ahc, SDSCB_QOFF, 0); 4743 } else { 4744 ahc_outb(ahc, KERNEL_QINPOS, ahc->qinfifonext); 4745 ahc_outb(ahc, QINPOS, ahc->qinfifonext); 4746 ahc_outb(ahc, QOUTPOS, ahc->qoutfifonext); 4747 } 4748 4749 /* We don't have any waiting selections */ 4750 ahc_outb(ahc, WAITING_SCBH, SCB_LIST_NULL); 4751 4752 /* Our disconnection list is empty too */ 4753 ahc_outb(ahc, DISCONNECTED_SCBH, SCB_LIST_NULL); 4754 4755 /* Message out buffer starts empty */ 4756 ahc_outb(ahc, MSG_OUT, MSG_NOOP); 4757 4758 /* 4759 * Setup the allowed SCSI Sequences based on operational mode. 4760 * If we are a target, we'll enalbe select in operations once 4761 * we've had a lun enabled. 4762 */ 4763 scsiseq_template = ENSELO|ENAUTOATNO|ENAUTOATNP; 4764 if ((ahc->flags & AHC_INITIATORROLE) != 0) 4765 scsiseq_template |= ENRSELI; 4766 ahc_outb(ahc, SCSISEQ_TEMPLATE, scsiseq_template); 4767 4768 /* Initialize our list of free SCBs. */ 4769 ahc_build_free_scb_list(ahc); 4770 4771 /* 4772 * Tell the sequencer which SCB will be the next one it receives. 4773 */ 4774 ahc_outb(ahc, NEXT_QUEUED_SCB, ahc->next_queued_scb->hscb->tag); 4775 4776 /* 4777 * Load the Sequencer program and Enable the adapter 4778 * in "fast" mode. 4779 */ 4780 if (bootverbose) 4781 printf("%s: Downloading Sequencer Program...", 4782 ahc_name(ahc)); 4783 4784 error = ahc_loadseq(ahc); 4785 if (error != 0) 4786 return (error); 4787 4788 if ((ahc->features & AHC_ULTRA2) != 0) { 4789 int wait; 4790 4791 /* 4792 * Wait for up to 500ms for our transceivers 4793 * to settle. If the adapter does not have 4794 * a cable attached, the transceivers may 4795 * never settle, so don't complain if we 4796 * fail here. 4797 */ 4798 for (wait = 5000; 4799 (ahc_inb(ahc, SBLKCTL) & (ENAB40|ENAB20)) == 0 && wait; 4800 wait--) 4801 aic_delay(100); 4802 } 4803 ahc_restart(ahc); 4804 return (0); 4805 } 4806 4807 /* 4808 * Start the board, ready for normal operation 4809 */ 4810 int 4811 ahc_init(struct ahc_softc *ahc) 4812 { 4813 int max_targ; 4814 int error; 4815 u_int i; 4816 u_int scsi_conf; 4817 u_int ultraenb; 4818 u_int discenable; 4819 u_int tagenable; 4820 size_t driver_data_size; 4821 4822 #ifdef AHC_DEBUG 4823 if ((ahc_debug & AHC_DEBUG_SEQUENCER) != 0) 4824 ahc->flags |= AHC_SEQUENCER_DEBUG; 4825 #endif 4826 4827 #ifdef AHC_PRINT_SRAM 4828 printf("Scratch Ram:"); 4829 for (i = 0x20; i < 0x5f; i++) { 4830 if (((i % 8) == 0) && (i != 0)) { 4831 printf ("\n "); 4832 } 4833 printf (" 0x%x", ahc_inb(ahc, i)); 4834 } 4835 if ((ahc->features & AHC_MORE_SRAM) != 0) { 4836 for (i = 0x70; i < 0x7f; i++) { 4837 if (((i % 8) == 0) && (i != 0)) { 4838 printf ("\n "); 4839 } 4840 printf (" 0x%x", ahc_inb(ahc, i)); 4841 } 4842 } 4843 printf ("\n"); 4844 /* 4845 * Reading uninitialized scratch ram may 4846 * generate parity errors. 4847 */ 4848 ahc_outb(ahc, CLRINT, CLRPARERR); 4849 ahc_outb(ahc, CLRINT, CLRBRKADRINT); 4850 #endif 4851 max_targ = 15; 4852 4853 /* 4854 * Assume we have a board at this stage and it has been reset. 4855 */ 4856 if ((ahc->flags & AHC_USEDEFAULTS) != 0) 4857 ahc->our_id = ahc->our_id_b = 7; 4858 4859 /* 4860 * Default to allowing initiator operations. 4861 */ 4862 ahc->flags |= AHC_INITIATORROLE; 4863 4864 /* 4865 * Only allow target mode features if this unit has them enabled. 4866 */ 4867 if ((AHC_TMODE_ENABLE & (0x1 << ahc->unit)) == 0) 4868 ahc->features &= ~AHC_TARGETMODE; 4869 4870 #ifndef __linux__ 4871 /* DMA tag for mapping buffers into device visible space. */ 4872 if (aic_dma_tag_create(ahc, ahc->parent_dmat, /*alignment*/1, 4873 /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1, 4874 /*lowaddr*/ahc->flags & AHC_39BIT_ADDRESSING 4875 ? (bus_addr_t)0x7FFFFFFFFFULL 4876 : BUS_SPACE_MAXADDR_32BIT, 4877 /*highaddr*/BUS_SPACE_MAXADDR, 4878 /*filter*/NULL, /*filterarg*/NULL, 4879 /*maxsize*/(AHC_NSEG - 1) * PAGE_SIZE, 4880 /*nsegments*/AHC_NSEG, 4881 /*maxsegsz*/AHC_MAXTRANSFER_SIZE, 4882 /*flags*/BUS_DMA_ALLOCNOW, 4883 &ahc->buffer_dmat) != 0) { 4884 return (ENOMEM); 4885 } 4886 #endif 4887 4888 ahc->init_level++; 4889 4890 /* 4891 * DMA tag for our command fifos and other data in system memory 4892 * the card's sequencer must be able to access. For initiator 4893 * roles, we need to allocate space for the qinfifo and qoutfifo. 4894 * The qinfifo and qoutfifo are composed of 256 1 byte elements. 4895 * When providing for the target mode role, we must additionally 4896 * provide space for the incoming target command fifo and an extra 4897 * byte to deal with a dma bug in some chip versions. 4898 */ 4899 driver_data_size = 2 * 256 * sizeof(uint8_t); 4900 if ((ahc->features & AHC_TARGETMODE) != 0) 4901 driver_data_size += AHC_TMODE_CMDS * sizeof(struct target_cmd) 4902 + /*DMA WideOdd Bug Buffer*/1; 4903 if (aic_dma_tag_create(ahc, ahc->parent_dmat, /*alignment*/1, 4904 /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1, 4905 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT, 4906 /*highaddr*/BUS_SPACE_MAXADDR, 4907 /*filter*/NULL, /*filterarg*/NULL, 4908 driver_data_size, 4909 /*nsegments*/1, 4910 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT, 4911 /*flags*/0, &ahc->shared_data_dmat) != 0) { 4912 return (ENOMEM); 4913 } 4914 4915 ahc->init_level++; 4916 4917 /* Allocation of driver data */ 4918 if (aic_dmamem_alloc(ahc, ahc->shared_data_dmat, 4919 (void **)&ahc->qoutfifo, 4920 BUS_DMA_NOWAIT | BUS_DMA_COHERENT, 4921 &ahc->shared_data_dmamap) != 0) { 4922 return (ENOMEM); 4923 } 4924 4925 ahc->init_level++; 4926 4927 /* And permanently map it in */ 4928 aic_dmamap_load(ahc, ahc->shared_data_dmat, ahc->shared_data_dmamap, 4929 ahc->qoutfifo, driver_data_size, ahc_dmamap_cb, 4930 &ahc->shared_data_busaddr, /*flags*/0); 4931 4932 if ((ahc->features & AHC_TARGETMODE) != 0) { 4933 ahc->targetcmds = (struct target_cmd *)ahc->qoutfifo; 4934 ahc->qoutfifo = (uint8_t *)&ahc->targetcmds[AHC_TMODE_CMDS]; 4935 ahc->dma_bug_buf = ahc->shared_data_busaddr 4936 + driver_data_size - 1; 4937 /* All target command blocks start out invalid. */ 4938 for (i = 0; i < AHC_TMODE_CMDS; i++) 4939 ahc->targetcmds[i].cmd_valid = 0; 4940 ahc_sync_tqinfifo(ahc, BUS_DMASYNC_PREREAD); 4941 ahc->qoutfifo = (uint8_t *)&ahc->targetcmds[256]; 4942 } 4943 ahc->qinfifo = &ahc->qoutfifo[256]; 4944 4945 ahc->init_level++; 4946 4947 /* Allocate SCB data now that buffer_dmat is initialized */ 4948 if (ahc->scb_data->maxhscbs == 0) 4949 if (ahc_init_scbdata(ahc) != 0) 4950 return (ENOMEM); 4951 4952 /* 4953 * Allocate a tstate to house information for our 4954 * initiator presence on the bus as well as the user 4955 * data for any target mode initiator. 4956 */ 4957 if (ahc_alloc_tstate(ahc, ahc->our_id, 'A') == NULL) { 4958 printf("%s: unable to allocate ahc_tmode_tstate. " 4959 "Failing attach\n", ahc_name(ahc)); 4960 return (ENOMEM); 4961 } 4962 4963 if ((ahc->features & AHC_TWIN) != 0) { 4964 if (ahc_alloc_tstate(ahc, ahc->our_id_b, 'B') == NULL) { 4965 printf("%s: unable to allocate ahc_tmode_tstate. " 4966 "Failing attach\n", ahc_name(ahc)); 4967 return (ENOMEM); 4968 } 4969 } 4970 4971 /* 4972 * Fire up a recovery thread for this controller. 4973 */ 4974 error = ahc_spawn_recovery_thread(ahc); 4975 if (error != 0) 4976 return (error); 4977 4978 if (ahc->scb_data->maxhscbs < AHC_SCB_MAX_ALLOC) { 4979 ahc->flags |= AHC_PAGESCBS; 4980 } else { 4981 ahc->flags &= ~AHC_PAGESCBS; 4982 } 4983 4984 #ifdef AHC_DEBUG 4985 if (ahc_debug & AHC_SHOW_MISC) { 4986 printf("%s: hardware scb %u bytes; kernel scb %u bytes; " 4987 "ahc_dma %u bytes\n", 4988 ahc_name(ahc), 4989 (u_int)sizeof(struct hardware_scb), 4990 (u_int)sizeof(struct scb), 4991 (u_int)sizeof(struct ahc_dma_seg)); 4992 } 4993 #endif /* AHC_DEBUG */ 4994 4995 /* 4996 * Look at the information that board initialization or 4997 * the board bios has left us. 4998 */ 4999 if (ahc->features & AHC_TWIN) { 5000 scsi_conf = ahc_inb(ahc, SCSICONF + 1); 5001 if ((scsi_conf & RESET_SCSI) != 0 5002 && (ahc->flags & AHC_INITIATORROLE) != 0) 5003 ahc->flags |= AHC_RESET_BUS_B; 5004 } 5005 5006 scsi_conf = ahc_inb(ahc, SCSICONF); 5007 if ((scsi_conf & RESET_SCSI) != 0 5008 && (ahc->flags & AHC_INITIATORROLE) != 0) 5009 ahc->flags |= AHC_RESET_BUS_A; 5010 5011 ultraenb = 0; 5012 tagenable = ALL_TARGETS_MASK; 5013 5014 /* Grab the disconnection disable table and invert it for our needs */ 5015 if ((ahc->flags & AHC_USEDEFAULTS) != 0) { 5016 printf("%s: Host Adapter Bios disabled. Using default SCSI " 5017 "device parameters\n", ahc_name(ahc)); 5018 ahc->flags |= AHC_EXTENDED_TRANS_A|AHC_EXTENDED_TRANS_B| 5019 AHC_TERM_ENB_A|AHC_TERM_ENB_B; 5020 discenable = ALL_TARGETS_MASK; 5021 if ((ahc->features & AHC_ULTRA) != 0) 5022 ultraenb = ALL_TARGETS_MASK; 5023 } else { 5024 discenable = ~((ahc_inb(ahc, DISC_DSB + 1) << 8) 5025 | ahc_inb(ahc, DISC_DSB)); 5026 if ((ahc->features & (AHC_ULTRA|AHC_ULTRA2)) != 0) 5027 ultraenb = (ahc_inb(ahc, ULTRA_ENB + 1) << 8) 5028 | ahc_inb(ahc, ULTRA_ENB); 5029 } 5030 5031 if ((ahc->features & (AHC_WIDE|AHC_TWIN)) == 0) 5032 max_targ = 7; 5033 5034 for (i = 0; i <= max_targ; i++) { 5035 struct ahc_initiator_tinfo *tinfo; 5036 struct ahc_tmode_tstate *tstate; 5037 u_int our_id; 5038 u_int target_id; 5039 char channel; 5040 5041 channel = 'A'; 5042 our_id = ahc->our_id; 5043 target_id = i; 5044 if (i > 7 && (ahc->features & AHC_TWIN) != 0) { 5045 channel = 'B'; 5046 our_id = ahc->our_id_b; 5047 target_id = i % 8; 5048 } 5049 tinfo = ahc_fetch_transinfo(ahc, channel, our_id, 5050 target_id, &tstate); 5051 /* Default to async narrow across the board */ 5052 memset(tinfo, 0, sizeof(*tinfo)); 5053 if (ahc->flags & AHC_USEDEFAULTS) { 5054 if ((ahc->features & AHC_WIDE) != 0) 5055 tinfo->user.width = MSG_EXT_WDTR_BUS_16_BIT; 5056 5057 /* 5058 * These will be truncated when we determine the 5059 * connection type we have with the target. 5060 */ 5061 tinfo->user.period = ahc_syncrates->period; 5062 tinfo->user.offset = MAX_OFFSET; 5063 } else { 5064 u_int scsirate; 5065 uint16_t mask; 5066 5067 /* Take the settings leftover in scratch RAM. */ 5068 scsirate = ahc_inb(ahc, TARG_SCSIRATE + i); 5069 mask = (0x01 << i); 5070 if ((ahc->features & AHC_ULTRA2) != 0) { 5071 u_int offset; 5072 u_int maxsync; 5073 5074 if ((scsirate & SOFS) == 0x0F) { 5075 /* 5076 * Haven't negotiated yet, 5077 * so the format is different. 5078 */ 5079 scsirate = (scsirate & SXFR) >> 4 5080 | (ultraenb & mask) 5081 ? 0x08 : 0x0 5082 | (scsirate & WIDEXFER); 5083 offset = MAX_OFFSET_ULTRA2; 5084 } else 5085 offset = ahc_inb(ahc, TARG_OFFSET + i); 5086 if ((scsirate & ~WIDEXFER) == 0 && offset != 0) 5087 /* Set to the lowest sync rate, 5MHz */ 5088 scsirate |= 0x1c; 5089 maxsync = AHC_SYNCRATE_ULTRA2; 5090 if ((ahc->features & AHC_DT) != 0) 5091 maxsync = AHC_SYNCRATE_DT; 5092 tinfo->user.period = 5093 ahc_find_period(ahc, scsirate, maxsync); 5094 if (offset == 0) 5095 tinfo->user.period = 0; 5096 else 5097 tinfo->user.offset = MAX_OFFSET; 5098 if ((scsirate & SXFR_ULTRA2) <= 8/*10MHz*/ 5099 && (ahc->features & AHC_DT) != 0) 5100 tinfo->user.ppr_options = 5101 MSG_EXT_PPR_DT_REQ; 5102 } else if ((scsirate & SOFS) != 0) { 5103 if ((scsirate & SXFR) == 0x40 5104 && (ultraenb & mask) != 0) { 5105 /* Treat 10MHz as a non-ultra speed */ 5106 scsirate &= ~SXFR; 5107 ultraenb &= ~mask; 5108 } 5109 tinfo->user.period = 5110 ahc_find_period(ahc, scsirate, 5111 (ultraenb & mask) 5112 ? AHC_SYNCRATE_ULTRA 5113 : AHC_SYNCRATE_FAST); 5114 if (tinfo->user.period != 0) 5115 tinfo->user.offset = MAX_OFFSET; 5116 } 5117 if (tinfo->user.period == 0) 5118 tinfo->user.offset = 0; 5119 if ((scsirate & WIDEXFER) != 0 5120 && (ahc->features & AHC_WIDE) != 0) 5121 tinfo->user.width = MSG_EXT_WDTR_BUS_16_BIT; 5122 tinfo->user.protocol_version = 4; 5123 if ((ahc->features & AHC_DT) != 0) 5124 tinfo->user.transport_version = 3; 5125 else 5126 tinfo->user.transport_version = 2; 5127 tinfo->goal.protocol_version = 2; 5128 tinfo->goal.transport_version = 2; 5129 tinfo->curr.protocol_version = 2; 5130 tinfo->curr.transport_version = 2; 5131 } 5132 tstate->ultraenb = 0; 5133 } 5134 ahc->user_discenable = discenable; 5135 ahc->user_tagenable = tagenable; 5136 5137 return (ahc->bus_chip_init(ahc)); 5138 } 5139 5140 void 5141 ahc_intr_enable(struct ahc_softc *ahc, int enable) 5142 { 5143 u_int hcntrl; 5144 5145 hcntrl = ahc_inb(ahc, HCNTRL); 5146 hcntrl &= ~INTEN; 5147 ahc->pause &= ~INTEN; 5148 ahc->unpause &= ~INTEN; 5149 if (enable) { 5150 hcntrl |= INTEN; 5151 ahc->pause |= INTEN; 5152 ahc->unpause |= INTEN; 5153 } 5154 ahc_outb(ahc, HCNTRL, hcntrl); 5155 } 5156 5157 /* 5158 * Ensure that the card is paused in a location 5159 * outside of all critical sections and that all 5160 * pending work is completed prior to returning. 5161 * This routine should only be called from outside 5162 * an interrupt context. 5163 */ 5164 void 5165 ahc_pause_and_flushwork(struct ahc_softc *ahc) 5166 { 5167 int intstat; 5168 int maxloops; 5169 int paused; 5170 5171 maxloops = 1000; 5172 ahc->flags |= AHC_ALL_INTERRUPTS; 5173 paused = FALSE; 5174 do { 5175 if (paused) { 5176 ahc_unpause(ahc); 5177 /* 5178 * Give the sequencer some time to service 5179 * any active selections. 5180 */ 5181 aic_delay(500); 5182 } 5183 ahc_intr(ahc); 5184 ahc_pause(ahc); 5185 paused = TRUE; 5186 ahc_outb(ahc, SCSISEQ, ahc_inb(ahc, SCSISEQ) & ~ENSELO); 5187 intstat = ahc_inb(ahc, INTSTAT); 5188 if ((intstat & INT_PEND) == 0) { 5189 ahc_clear_critical_section(ahc); 5190 intstat = ahc_inb(ahc, INTSTAT); 5191 } 5192 } while (--maxloops 5193 && (intstat != 0xFF || (ahc->features & AHC_REMOVABLE) == 0) 5194 && ((intstat & INT_PEND) != 0 5195 || (ahc_inb(ahc, SSTAT0) & (SELDO|SELINGO)) != 0)); 5196 if (maxloops == 0) { 5197 printf("Infinite interrupt loop, INTSTAT = %x", 5198 ahc_inb(ahc, INTSTAT)); 5199 } 5200 ahc_platform_flushwork(ahc); 5201 ahc->flags &= ~AHC_ALL_INTERRUPTS; 5202 } 5203 5204 int 5205 ahc_suspend(struct ahc_softc *ahc) 5206 { 5207 5208 ahc_pause_and_flushwork(ahc); 5209 5210 if (LIST_FIRST(&ahc->pending_scbs) != NULL) { 5211 ahc_unpause(ahc); 5212 return (EBUSY); 5213 } 5214 5215 #ifdef AHC_TARGET_MODE 5216 /* 5217 * XXX What about ATIOs that have not yet been serviced? 5218 * Perhaps we should just refuse to be suspended if we 5219 * are acting in a target role. 5220 */ 5221 if (ahc->pending_device != NULL) { 5222 ahc_unpause(ahc); 5223 return (EBUSY); 5224 } 5225 #endif 5226 ahc_shutdown(ahc); 5227 return (0); 5228 } 5229 5230 int 5231 ahc_resume(struct ahc_softc *ahc) 5232 { 5233 5234 ahc_reset(ahc, /*reinit*/TRUE); 5235 ahc_intr_enable(ahc, TRUE); 5236 ahc_restart(ahc); 5237 return (0); 5238 } 5239 5240 /************************** Busy Target Table *********************************/ 5241 /* 5242 * Return the untagged transaction id for a given target/channel lun. 5243 * Optionally, clear the entry. 5244 */ 5245 u_int 5246 ahc_index_busy_tcl(struct ahc_softc *ahc, u_int tcl) 5247 { 5248 u_int scbid; 5249 u_int target_offset; 5250 5251 if ((ahc->flags & AHC_SCB_BTT) != 0) { 5252 u_int saved_scbptr; 5253 5254 saved_scbptr = ahc_inb(ahc, SCBPTR); 5255 ahc_outb(ahc, SCBPTR, TCL_LUN(tcl)); 5256 scbid = ahc_inb(ahc, SCB_64_BTT + TCL_TARGET_OFFSET(tcl)); 5257 ahc_outb(ahc, SCBPTR, saved_scbptr); 5258 } else { 5259 target_offset = TCL_TARGET_OFFSET(tcl); 5260 scbid = ahc_inb(ahc, BUSY_TARGETS + target_offset); 5261 } 5262 5263 return (scbid); 5264 } 5265 5266 void 5267 ahc_unbusy_tcl(struct ahc_softc *ahc, u_int tcl) 5268 { 5269 u_int target_offset; 5270 5271 if ((ahc->flags & AHC_SCB_BTT) != 0) { 5272 u_int saved_scbptr; 5273 5274 saved_scbptr = ahc_inb(ahc, SCBPTR); 5275 ahc_outb(ahc, SCBPTR, TCL_LUN(tcl)); 5276 ahc_outb(ahc, SCB_64_BTT+TCL_TARGET_OFFSET(tcl), SCB_LIST_NULL); 5277 ahc_outb(ahc, SCBPTR, saved_scbptr); 5278 } else { 5279 target_offset = TCL_TARGET_OFFSET(tcl); 5280 ahc_outb(ahc, BUSY_TARGETS + target_offset, SCB_LIST_NULL); 5281 } 5282 } 5283 5284 void 5285 ahc_busy_tcl(struct ahc_softc *ahc, u_int tcl, u_int scbid) 5286 { 5287 u_int target_offset; 5288 5289 if ((ahc->flags & AHC_SCB_BTT) != 0) { 5290 u_int saved_scbptr; 5291 5292 saved_scbptr = ahc_inb(ahc, SCBPTR); 5293 ahc_outb(ahc, SCBPTR, TCL_LUN(tcl)); 5294 ahc_outb(ahc, SCB_64_BTT + TCL_TARGET_OFFSET(tcl), scbid); 5295 ahc_outb(ahc, SCBPTR, saved_scbptr); 5296 } else { 5297 target_offset = TCL_TARGET_OFFSET(tcl); 5298 ahc_outb(ahc, BUSY_TARGETS + target_offset, scbid); 5299 } 5300 } 5301 5302 /************************** SCB and SCB queue management **********************/ 5303 int 5304 ahc_match_scb(struct ahc_softc *ahc, struct scb *scb, int target, 5305 char channel, int lun, u_int tag, role_t role) 5306 { 5307 int targ = SCB_GET_TARGET(ahc, scb); 5308 char chan = SCB_GET_CHANNEL(ahc, scb); 5309 int slun = SCB_GET_LUN(scb); 5310 int match; 5311 5312 match = ((chan == channel) || (channel == ALL_CHANNELS)); 5313 if (match != 0) 5314 match = ((targ == target) || (target == CAM_TARGET_WILDCARD)); 5315 if (match != 0) 5316 match = ((lun == slun) || (lun == CAM_LUN_WILDCARD)); 5317 if (match != 0) { 5318 #ifdef AHC_TARGET_MODE 5319 int group; 5320 5321 group = XPT_FC_GROUP(scb->io_ctx->ccb_h.func_code); 5322 if (role == ROLE_INITIATOR) { 5323 match = (group != XPT_FC_GROUP_TMODE) 5324 && ((tag == scb->hscb->tag) 5325 || (tag == SCB_LIST_NULL)); 5326 } else if (role == ROLE_TARGET) { 5327 match = (group == XPT_FC_GROUP_TMODE) 5328 && ((tag == scb->io_ctx->csio.tag_id) 5329 || (tag == SCB_LIST_NULL)); 5330 } 5331 #else /* !AHC_TARGET_MODE */ 5332 match = ((tag == scb->hscb->tag) || (tag == SCB_LIST_NULL)); 5333 #endif /* AHC_TARGET_MODE */ 5334 } 5335 5336 return match; 5337 } 5338 5339 void 5340 ahc_freeze_devq(struct ahc_softc *ahc, struct scb *scb) 5341 { 5342 int target; 5343 char channel; 5344 int lun; 5345 5346 target = SCB_GET_TARGET(ahc, scb); 5347 lun = SCB_GET_LUN(scb); 5348 channel = SCB_GET_CHANNEL(ahc, scb); 5349 5350 ahc_search_qinfifo(ahc, target, channel, lun, 5351 /*tag*/SCB_LIST_NULL, ROLE_UNKNOWN, 5352 CAM_REQUEUE_REQ, SEARCH_COMPLETE); 5353 5354 ahc_platform_freeze_devq(ahc, scb); 5355 } 5356 5357 void 5358 ahc_qinfifo_requeue_tail(struct ahc_softc *ahc, struct scb *scb) 5359 { 5360 struct scb *prev_scb; 5361 5362 prev_scb = NULL; 5363 if (ahc_qinfifo_count(ahc) != 0) { 5364 u_int prev_tag; 5365 uint8_t prev_pos; 5366 5367 prev_pos = ahc->qinfifonext - 1; 5368 prev_tag = ahc->qinfifo[prev_pos]; 5369 prev_scb = ahc_lookup_scb(ahc, prev_tag); 5370 } 5371 ahc_qinfifo_requeue(ahc, prev_scb, scb); 5372 if ((ahc->features & AHC_QUEUE_REGS) != 0) { 5373 ahc_outb(ahc, HNSCB_QOFF, ahc->qinfifonext); 5374 } else { 5375 ahc_outb(ahc, KERNEL_QINPOS, ahc->qinfifonext); 5376 } 5377 } 5378 5379 static void 5380 ahc_qinfifo_requeue(struct ahc_softc *ahc, struct scb *prev_scb, 5381 struct scb *scb) 5382 { 5383 if (prev_scb == NULL) { 5384 ahc_outb(ahc, NEXT_QUEUED_SCB, scb->hscb->tag); 5385 } else { 5386 prev_scb->hscb->next = scb->hscb->tag; 5387 ahc_sync_scb(ahc, prev_scb, 5388 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); 5389 } 5390 ahc->qinfifo[ahc->qinfifonext++] = scb->hscb->tag; 5391 scb->hscb->next = ahc->next_queued_scb->hscb->tag; 5392 ahc_sync_scb(ahc, scb, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); 5393 } 5394 5395 static int 5396 ahc_qinfifo_count(struct ahc_softc *ahc) 5397 { 5398 uint8_t qinpos; 5399 uint8_t diff; 5400 5401 if ((ahc->features & AHC_QUEUE_REGS) != 0) { 5402 qinpos = ahc_inb(ahc, SNSCB_QOFF); 5403 ahc_outb(ahc, SNSCB_QOFF, qinpos); 5404 } else 5405 qinpos = ahc_inb(ahc, QINPOS); 5406 diff = ahc->qinfifonext - qinpos; 5407 return (diff); 5408 } 5409 5410 int 5411 ahc_search_qinfifo(struct ahc_softc *ahc, int target, char channel, 5412 int lun, u_int tag, role_t role, uint32_t status, 5413 ahc_search_action action) 5414 { 5415 struct scb *scb; 5416 struct scb *prev_scb; 5417 uint8_t qinstart; 5418 uint8_t qinpos; 5419 uint8_t qintail; 5420 uint8_t next; 5421 uint8_t prev; 5422 uint8_t curscbptr; 5423 int found; 5424 int have_qregs; 5425 5426 qintail = ahc->qinfifonext; 5427 have_qregs = (ahc->features & AHC_QUEUE_REGS) != 0; 5428 if (have_qregs) { 5429 qinstart = ahc_inb(ahc, SNSCB_QOFF); 5430 ahc_outb(ahc, SNSCB_QOFF, qinstart); 5431 } else 5432 qinstart = ahc_inb(ahc, QINPOS); 5433 qinpos = qinstart; 5434 found = 0; 5435 prev_scb = NULL; 5436 5437 if (action == SEARCH_COMPLETE) { 5438 /* 5439 * Don't attempt to run any queued untagged transactions 5440 * until we are done with the abort process. 5441 */ 5442 ahc_freeze_untagged_queues(ahc); 5443 } 5444 5445 /* 5446 * Start with an empty queue. Entries that are not chosen 5447 * for removal will be re-added to the queue as we go. 5448 */ 5449 ahc->qinfifonext = qinpos; 5450 ahc_outb(ahc, NEXT_QUEUED_SCB, ahc->next_queued_scb->hscb->tag); 5451 5452 while (qinpos != qintail) { 5453 scb = ahc_lookup_scb(ahc, ahc->qinfifo[qinpos]); 5454 if (scb == NULL) { 5455 printf("qinpos = %d, SCB index = %d\n", 5456 qinpos, ahc->qinfifo[qinpos]); 5457 panic("Loop 1\n"); 5458 } 5459 5460 if (ahc_match_scb(ahc, scb, target, channel, lun, tag, role)) { 5461 /* 5462 * We found an scb that needs to be acted on. 5463 */ 5464 found++; 5465 switch (action) { 5466 case SEARCH_COMPLETE: 5467 { 5468 cam_status ostat; 5469 cam_status cstat; 5470 5471 ostat = aic_get_transaction_status(scb); 5472 if (ostat == CAM_REQ_INPROG) 5473 aic_set_transaction_status(scb, status); 5474 cstat = aic_get_transaction_status(scb); 5475 if (cstat != CAM_REQ_CMP) 5476 aic_freeze_scb(scb); 5477 if ((scb->flags & SCB_ACTIVE) == 0) 5478 printf("Inactive SCB in qinfifo\n"); 5479 ahc_done(ahc, scb); 5480 5481 /* FALLTHROUGH */ 5482 } 5483 case SEARCH_REMOVE: 5484 break; 5485 case SEARCH_COUNT: 5486 ahc_qinfifo_requeue(ahc, prev_scb, scb); 5487 prev_scb = scb; 5488 break; 5489 } 5490 } else { 5491 ahc_qinfifo_requeue(ahc, prev_scb, scb); 5492 prev_scb = scb; 5493 } 5494 qinpos++; 5495 } 5496 5497 if ((ahc->features & AHC_QUEUE_REGS) != 0) { 5498 ahc_outb(ahc, HNSCB_QOFF, ahc->qinfifonext); 5499 } else { 5500 ahc_outb(ahc, KERNEL_QINPOS, ahc->qinfifonext); 5501 } 5502 5503 if (action != SEARCH_COUNT 5504 && (found != 0) 5505 && (qinstart != ahc->qinfifonext)) { 5506 /* 5507 * The sequencer may be in the process of dmaing 5508 * down the SCB at the beginning of the queue. 5509 * This could be problematic if either the first, 5510 * or the second SCB is removed from the queue 5511 * (the first SCB includes a pointer to the "next" 5512 * SCB to dma). If we have removed any entries, swap 5513 * the first element in the queue with the next HSCB 5514 * so the sequencer will notice that NEXT_QUEUED_SCB 5515 * has changed during its dma attempt and will retry 5516 * the DMA. 5517 */ 5518 scb = ahc_lookup_scb(ahc, ahc->qinfifo[qinstart]); 5519 5520 if (scb == NULL) { 5521 printf("found = %d, qinstart = %d, qinfifionext = %d\n", 5522 found, qinstart, ahc->qinfifonext); 5523 panic("First/Second Qinfifo fixup\n"); 5524 } 5525 /* 5526 * ahc_swap_with_next_hscb forces our next pointer to 5527 * point to the reserved SCB for future commands. Save 5528 * and restore our original next pointer to maintain 5529 * queue integrity. 5530 */ 5531 next = scb->hscb->next; 5532 ahc->scb_data->scbindex[scb->hscb->tag] = NULL; 5533 ahc_swap_with_next_hscb(ahc, scb); 5534 scb->hscb->next = next; 5535 ahc->qinfifo[qinstart] = scb->hscb->tag; 5536 5537 /* Tell the card about the new head of the qinfifo. */ 5538 ahc_outb(ahc, NEXT_QUEUED_SCB, scb->hscb->tag); 5539 5540 /* Fixup the tail "next" pointer. */ 5541 qintail = ahc->qinfifonext - 1; 5542 scb = ahc_lookup_scb(ahc, ahc->qinfifo[qintail]); 5543 scb->hscb->next = ahc->next_queued_scb->hscb->tag; 5544 } 5545 5546 /* 5547 * Search waiting for selection list. 5548 */ 5549 curscbptr = ahc_inb(ahc, SCBPTR); 5550 next = ahc_inb(ahc, WAITING_SCBH); /* Start at head of list. */ 5551 prev = SCB_LIST_NULL; 5552 5553 while (next != SCB_LIST_NULL) { 5554 uint8_t scb_index; 5555 5556 ahc_outb(ahc, SCBPTR, next); 5557 scb_index = ahc_inb(ahc, SCB_TAG); 5558 if (scb_index >= ahc->scb_data->numscbs) { 5559 printf("Waiting List inconsistency. " 5560 "SCB index == %d, yet numscbs == %d.", 5561 scb_index, ahc->scb_data->numscbs); 5562 ahc_dump_card_state(ahc); 5563 panic("for safety"); 5564 } 5565 scb = ahc_lookup_scb(ahc, scb_index); 5566 if (scb == NULL) { 5567 printf("scb_index = %d, next = %d\n", 5568 scb_index, next); 5569 panic("Waiting List traversal\n"); 5570 } 5571 if (ahc_match_scb(ahc, scb, target, channel, 5572 lun, SCB_LIST_NULL, role)) { 5573 /* 5574 * We found an scb that needs to be acted on. 5575 */ 5576 found++; 5577 switch (action) { 5578 case SEARCH_COMPLETE: 5579 { 5580 cam_status ostat; 5581 cam_status cstat; 5582 5583 ostat = aic_get_transaction_status(scb); 5584 if (ostat == CAM_REQ_INPROG) 5585 aic_set_transaction_status(scb, 5586 status); 5587 cstat = aic_get_transaction_status(scb); 5588 if (cstat != CAM_REQ_CMP) 5589 aic_freeze_scb(scb); 5590 if ((scb->flags & SCB_ACTIVE) == 0) 5591 printf("Inactive SCB in Wait List\n"); 5592 ahc_done(ahc, scb); 5593 /* FALLTHROUGH */ 5594 } 5595 case SEARCH_REMOVE: 5596 next = ahc_rem_wscb(ahc, next, prev); 5597 break; 5598 case SEARCH_COUNT: 5599 prev = next; 5600 next = ahc_inb(ahc, SCB_NEXT); 5601 break; 5602 } 5603 } else { 5604 5605 prev = next; 5606 next = ahc_inb(ahc, SCB_NEXT); 5607 } 5608 } 5609 ahc_outb(ahc, SCBPTR, curscbptr); 5610 5611 found += ahc_search_untagged_queues(ahc, /*aic_io_ctx_t*/NULL, target, 5612 channel, lun, status, action); 5613 5614 if (action == SEARCH_COMPLETE) 5615 ahc_release_untagged_queues(ahc); 5616 return (found); 5617 } 5618 5619 int 5620 ahc_search_untagged_queues(struct ahc_softc *ahc, aic_io_ctx_t ctx, 5621 int target, char channel, int lun, uint32_t status, 5622 ahc_search_action action) 5623 { 5624 struct scb *scb; 5625 int maxtarget; 5626 int found; 5627 int i; 5628 5629 if (action == SEARCH_COMPLETE) { 5630 /* 5631 * Don't attempt to run any queued untagged transactions 5632 * until we are done with the abort process. 5633 */ 5634 ahc_freeze_untagged_queues(ahc); 5635 } 5636 5637 found = 0; 5638 i = 0; 5639 if ((ahc->flags & AHC_SCB_BTT) == 0) { 5640 maxtarget = 16; 5641 if (target != CAM_TARGET_WILDCARD) { 5642 i = target; 5643 if (channel == 'B') 5644 i += 8; 5645 maxtarget = i + 1; 5646 } 5647 } else { 5648 maxtarget = 0; 5649 } 5650 5651 for (; i < maxtarget; i++) { 5652 struct scb_tailq *untagged_q; 5653 struct scb *next_scb; 5654 5655 untagged_q = &(ahc->untagged_queues[i]); 5656 next_scb = TAILQ_FIRST(untagged_q); 5657 while (next_scb != NULL) { 5658 scb = next_scb; 5659 next_scb = TAILQ_NEXT(scb, links.tqe); 5660 5661 /* 5662 * The head of the list may be the currently 5663 * active untagged command for a device. 5664 * We're only searching for commands that 5665 * have not been started. A transaction 5666 * marked active but still in the qinfifo 5667 * is removed by the qinfifo scanning code 5668 * above. 5669 */ 5670 if ((scb->flags & SCB_ACTIVE) != 0) 5671 continue; 5672 5673 if (ahc_match_scb(ahc, scb, target, channel, lun, 5674 SCB_LIST_NULL, ROLE_INITIATOR) == 0 5675 || (ctx != NULL && ctx != scb->io_ctx)) 5676 continue; 5677 5678 /* 5679 * We found an scb that needs to be acted on. 5680 */ 5681 found++; 5682 switch (action) { 5683 case SEARCH_COMPLETE: 5684 { 5685 cam_status ostat; 5686 cam_status cstat; 5687 5688 ostat = aic_get_transaction_status(scb); 5689 if (ostat == CAM_REQ_INPROG) 5690 aic_set_transaction_status(scb, status); 5691 cstat = aic_get_transaction_status(scb); 5692 if (cstat != CAM_REQ_CMP) 5693 aic_freeze_scb(scb); 5694 ahc_done(ahc, scb); 5695 break; 5696 } 5697 case SEARCH_REMOVE: 5698 scb->flags &= ~SCB_UNTAGGEDQ; 5699 TAILQ_REMOVE(untagged_q, scb, links.tqe); 5700 break; 5701 case SEARCH_COUNT: 5702 break; 5703 } 5704 } 5705 } 5706 5707 if (action == SEARCH_COMPLETE) 5708 ahc_release_untagged_queues(ahc); 5709 return (found); 5710 } 5711 5712 int 5713 ahc_search_disc_list(struct ahc_softc *ahc, int target, char channel, 5714 int lun, u_int tag, int stop_on_first, int remove, 5715 int save_state) 5716 { 5717 struct scb *scbp; 5718 u_int next; 5719 u_int prev; 5720 u_int count; 5721 u_int active_scb; 5722 5723 count = 0; 5724 next = ahc_inb(ahc, DISCONNECTED_SCBH); 5725 prev = SCB_LIST_NULL; 5726 5727 if (save_state) { 5728 /* restore this when we're done */ 5729 active_scb = ahc_inb(ahc, SCBPTR); 5730 } else 5731 /* Silence compiler */ 5732 active_scb = SCB_LIST_NULL; 5733 5734 while (next != SCB_LIST_NULL) { 5735 u_int scb_index; 5736 5737 ahc_outb(ahc, SCBPTR, next); 5738 scb_index = ahc_inb(ahc, SCB_TAG); 5739 if (scb_index >= ahc->scb_data->numscbs) { 5740 printf("Disconnected List inconsistency. " 5741 "SCB index == %d, yet numscbs == %d.", 5742 scb_index, ahc->scb_data->numscbs); 5743 ahc_dump_card_state(ahc); 5744 panic("for safety"); 5745 } 5746 5747 if (next == prev) { 5748 panic("Disconnected List Loop. " 5749 "cur SCBPTR == %x, prev SCBPTR == %x.", 5750 next, prev); 5751 } 5752 scbp = ahc_lookup_scb(ahc, scb_index); 5753 if (ahc_match_scb(ahc, scbp, target, channel, lun, 5754 tag, ROLE_INITIATOR)) { 5755 count++; 5756 if (remove) { 5757 next = 5758 ahc_rem_scb_from_disc_list(ahc, prev, next); 5759 } else { 5760 prev = next; 5761 next = ahc_inb(ahc, SCB_NEXT); 5762 } 5763 if (stop_on_first) 5764 break; 5765 } else { 5766 prev = next; 5767 next = ahc_inb(ahc, SCB_NEXT); 5768 } 5769 } 5770 if (save_state) 5771 ahc_outb(ahc, SCBPTR, active_scb); 5772 return (count); 5773 } 5774 5775 /* 5776 * Remove an SCB from the on chip list of disconnected transactions. 5777 * This is empty/unused if we are not performing SCB paging. 5778 */ 5779 static u_int 5780 ahc_rem_scb_from_disc_list(struct ahc_softc *ahc, u_int prev, u_int scbptr) 5781 { 5782 u_int next; 5783 5784 ahc_outb(ahc, SCBPTR, scbptr); 5785 next = ahc_inb(ahc, SCB_NEXT); 5786 5787 ahc_outb(ahc, SCB_CONTROL, 0); 5788 5789 ahc_add_curscb_to_free_list(ahc); 5790 5791 if (prev != SCB_LIST_NULL) { 5792 ahc_outb(ahc, SCBPTR, prev); 5793 ahc_outb(ahc, SCB_NEXT, next); 5794 } else 5795 ahc_outb(ahc, DISCONNECTED_SCBH, next); 5796 5797 return (next); 5798 } 5799 5800 /* 5801 * Add the SCB as selected by SCBPTR onto the on chip list of 5802 * free hardware SCBs. This list is empty/unused if we are not 5803 * performing SCB paging. 5804 */ 5805 static void 5806 ahc_add_curscb_to_free_list(struct ahc_softc *ahc) 5807 { 5808 /* 5809 * Invalidate the tag so that our abort 5810 * routines don't think it's active. 5811 */ 5812 ahc_outb(ahc, SCB_TAG, SCB_LIST_NULL); 5813 5814 if ((ahc->flags & AHC_PAGESCBS) != 0) { 5815 ahc_outb(ahc, SCB_NEXT, ahc_inb(ahc, FREE_SCBH)); 5816 ahc_outb(ahc, FREE_SCBH, ahc_inb(ahc, SCBPTR)); 5817 } 5818 } 5819 5820 /* 5821 * Manipulate the waiting for selection list and return the 5822 * scb that follows the one that we remove. 5823 */ 5824 static u_int 5825 ahc_rem_wscb(struct ahc_softc *ahc, u_int scbpos, u_int prev) 5826 { 5827 u_int curscb, next; 5828 5829 /* 5830 * Select the SCB we want to abort and 5831 * pull the next pointer out of it. 5832 */ 5833 curscb = ahc_inb(ahc, SCBPTR); 5834 ahc_outb(ahc, SCBPTR, scbpos); 5835 next = ahc_inb(ahc, SCB_NEXT); 5836 5837 /* Clear the necessary fields */ 5838 ahc_outb(ahc, SCB_CONTROL, 0); 5839 5840 ahc_add_curscb_to_free_list(ahc); 5841 5842 /* update the waiting list */ 5843 if (prev == SCB_LIST_NULL) { 5844 /* First in the list */ 5845 ahc_outb(ahc, WAITING_SCBH, next); 5846 5847 /* 5848 * Ensure we aren't attempting to perform 5849 * selection for this entry. 5850 */ 5851 ahc_outb(ahc, SCSISEQ, (ahc_inb(ahc, SCSISEQ) & ~ENSELO)); 5852 } else { 5853 /* 5854 * Select the scb that pointed to us 5855 * and update its next pointer. 5856 */ 5857 ahc_outb(ahc, SCBPTR, prev); 5858 ahc_outb(ahc, SCB_NEXT, next); 5859 } 5860 5861 /* 5862 * Point us back at the original scb position. 5863 */ 5864 ahc_outb(ahc, SCBPTR, curscb); 5865 return next; 5866 } 5867 5868 /******************************** Error Handling ******************************/ 5869 /* 5870 * Abort all SCBs that match the given description (target/channel/lun/tag), 5871 * setting their status to the passed in status if the status has not already 5872 * been modified from CAM_REQ_INPROG. This routine assumes that the sequencer 5873 * is paused before it is called. 5874 */ 5875 int 5876 ahc_abort_scbs(struct ahc_softc *ahc, int target, char channel, 5877 int lun, u_int tag, role_t role, uint32_t status) 5878 { 5879 struct scb *scbp; 5880 struct scb *scbp_next; 5881 u_int active_scb; 5882 int i, j; 5883 int maxtarget; 5884 int minlun; 5885 int maxlun; 5886 5887 int found; 5888 5889 /* 5890 * Don't attempt to run any queued untagged transactions 5891 * until we are done with the abort process. 5892 */ 5893 ahc_freeze_untagged_queues(ahc); 5894 5895 /* restore this when we're done */ 5896 active_scb = ahc_inb(ahc, SCBPTR); 5897 5898 found = ahc_search_qinfifo(ahc, target, channel, lun, SCB_LIST_NULL, 5899 role, CAM_REQUEUE_REQ, SEARCH_COMPLETE); 5900 5901 /* 5902 * Clean out the busy target table for any untagged commands. 5903 */ 5904 i = 0; 5905 maxtarget = 16; 5906 if (target != CAM_TARGET_WILDCARD) { 5907 i = target; 5908 if (channel == 'B') 5909 i += 8; 5910 maxtarget = i + 1; 5911 } 5912 5913 if (lun == CAM_LUN_WILDCARD) { 5914 /* 5915 * Unless we are using an SCB based 5916 * busy targets table, there is only 5917 * one table entry for all luns of 5918 * a target. 5919 */ 5920 minlun = 0; 5921 maxlun = 1; 5922 if ((ahc->flags & AHC_SCB_BTT) != 0) 5923 maxlun = AHC_NUM_LUNS; 5924 } else { 5925 minlun = lun; 5926 maxlun = lun + 1; 5927 } 5928 5929 if (role != ROLE_TARGET) { 5930 for (;i < maxtarget; i++) { 5931 for (j = minlun;j < maxlun; j++) { 5932 u_int scbid; 5933 u_int tcl; 5934 5935 tcl = BUILD_TCL(i << 4, j); 5936 scbid = ahc_index_busy_tcl(ahc, tcl); 5937 scbp = ahc_lookup_scb(ahc, scbid); 5938 if (scbp == NULL 5939 || ahc_match_scb(ahc, scbp, target, channel, 5940 lun, tag, role) == 0) 5941 continue; 5942 ahc_unbusy_tcl(ahc, BUILD_TCL(i << 4, j)); 5943 } 5944 } 5945 5946 /* 5947 * Go through the disconnected list and remove any entries we 5948 * have queued for completion, 0'ing their control byte too. 5949 * We save the active SCB and restore it ourselves, so there 5950 * is no reason for this search to restore it too. 5951 */ 5952 ahc_search_disc_list(ahc, target, channel, lun, tag, 5953 /*stop_on_first*/FALSE, /*remove*/TRUE, 5954 /*save_state*/FALSE); 5955 } 5956 5957 /* 5958 * Go through the hardware SCB array looking for commands that 5959 * were active but not on any list. In some cases, these remnants 5960 * might not still have mappings in the scbindex array (e.g. unexpected 5961 * bus free with the same scb queued for an abort). Don't hold this 5962 * against them. 5963 */ 5964 for (i = 0; i < ahc->scb_data->maxhscbs; i++) { 5965 u_int scbid; 5966 5967 ahc_outb(ahc, SCBPTR, i); 5968 scbid = ahc_inb(ahc, SCB_TAG); 5969 scbp = ahc_lookup_scb(ahc, scbid); 5970 if ((scbp == NULL && scbid != SCB_LIST_NULL) 5971 || (scbp != NULL 5972 && ahc_match_scb(ahc, scbp, target, channel, lun, tag, role))) 5973 ahc_add_curscb_to_free_list(ahc); 5974 } 5975 5976 /* 5977 * Go through the pending CCB list and look for 5978 * commands for this target that are still active. 5979 * These are other tagged commands that were 5980 * disconnected when the reset occurred. 5981 */ 5982 scbp_next = LIST_FIRST(&ahc->pending_scbs); 5983 while (scbp_next != NULL) { 5984 scbp = scbp_next; 5985 scbp_next = LIST_NEXT(scbp, pending_links); 5986 if (ahc_match_scb(ahc, scbp, target, channel, lun, tag, role)) { 5987 cam_status ostat; 5988 5989 ostat = aic_get_transaction_status(scbp); 5990 if (ostat == CAM_REQ_INPROG) 5991 aic_set_transaction_status(scbp, status); 5992 if (aic_get_transaction_status(scbp) != CAM_REQ_CMP) 5993 aic_freeze_scb(scbp); 5994 if ((scbp->flags & SCB_ACTIVE) == 0) 5995 printf("Inactive SCB on pending list\n"); 5996 ahc_done(ahc, scbp); 5997 found++; 5998 } 5999 } 6000 ahc_outb(ahc, SCBPTR, active_scb); 6001 ahc_platform_abort_scbs(ahc, target, channel, lun, tag, role, status); 6002 ahc_release_untagged_queues(ahc); 6003 return found; 6004 } 6005 6006 static void 6007 ahc_reset_current_bus(struct ahc_softc *ahc) 6008 { 6009 uint8_t scsiseq; 6010 6011 ahc_outb(ahc, SIMODE1, ahc_inb(ahc, SIMODE1) & ~ENSCSIRST); 6012 scsiseq = ahc_inb(ahc, SCSISEQ); 6013 ahc_outb(ahc, SCSISEQ, scsiseq | SCSIRSTO); 6014 ahc_flush_device_writes(ahc); 6015 aic_delay(AHC_BUSRESET_DELAY); 6016 /* Turn off the bus reset */ 6017 ahc_outb(ahc, SCSISEQ, scsiseq & ~SCSIRSTO); 6018 6019 ahc_clear_intstat(ahc); 6020 6021 /* Re-enable reset interrupts */ 6022 ahc_outb(ahc, SIMODE1, ahc_inb(ahc, SIMODE1) | ENSCSIRST); 6023 } 6024 6025 int 6026 ahc_reset_channel(struct ahc_softc *ahc, char channel, int initiate_reset) 6027 { 6028 struct ahc_devinfo devinfo; 6029 u_int initiator, target, max_scsiid; 6030 u_int sblkctl; 6031 u_int scsiseq; 6032 u_int simode1; 6033 int found; 6034 int restart_needed; 6035 char cur_channel; 6036 6037 ahc->pending_device = NULL; 6038 6039 ahc_compile_devinfo(&devinfo, 6040 CAM_TARGET_WILDCARD, 6041 CAM_TARGET_WILDCARD, 6042 CAM_LUN_WILDCARD, 6043 channel, ROLE_UNKNOWN); 6044 ahc_pause(ahc); 6045 6046 /* Make sure the sequencer is in a safe location. */ 6047 ahc_clear_critical_section(ahc); 6048 6049 /* 6050 * Run our command complete fifos to ensure that we perform 6051 * completion processing on any commands that 'completed' 6052 * before the reset occurred. 6053 */ 6054 ahc_run_qoutfifo(ahc); 6055 #ifdef AHC_TARGET_MODE 6056 /* 6057 * XXX - In Twin mode, the tqinfifo may have commands 6058 * for an unaffected channel in it. However, if 6059 * we have run out of ATIO resources to drain that 6060 * queue, we may not get them all out here. Further, 6061 * the blocked transactions for the reset channel 6062 * should just be killed off, irrespecitve of whether 6063 * we are blocked on ATIO resources. Write a routine 6064 * to compact the tqinfifo appropriately. 6065 */ 6066 if ((ahc->flags & AHC_TARGETROLE) != 0) { 6067 ahc_run_tqinfifo(ahc, /*paused*/TRUE); 6068 } 6069 #endif 6070 6071 /* 6072 * Reset the bus if we are initiating this reset 6073 */ 6074 sblkctl = ahc_inb(ahc, SBLKCTL); 6075 cur_channel = 'A'; 6076 if ((ahc->features & AHC_TWIN) != 0 6077 && ((sblkctl & SELBUSB) != 0)) 6078 cur_channel = 'B'; 6079 scsiseq = ahc_inb(ahc, SCSISEQ_TEMPLATE); 6080 if (cur_channel != channel) { 6081 /* Case 1: Command for another bus is active 6082 * Stealthily reset the other bus without 6083 * upsetting the current bus. 6084 */ 6085 ahc_outb(ahc, SBLKCTL, sblkctl ^ SELBUSB); 6086 simode1 = ahc_inb(ahc, SIMODE1) & ~(ENBUSFREE|ENSCSIRST); 6087 #ifdef AHC_TARGET_MODE 6088 /* 6089 * Bus resets clear ENSELI, so we cannot 6090 * defer re-enabling bus reset interrupts 6091 * if we are in target mode. 6092 */ 6093 if ((ahc->flags & AHC_TARGETROLE) != 0) 6094 simode1 |= ENSCSIRST; 6095 #endif 6096 ahc_outb(ahc, SIMODE1, simode1); 6097 if (initiate_reset) 6098 ahc_reset_current_bus(ahc); 6099 ahc_clear_intstat(ahc); 6100 ahc_outb(ahc, SCSISEQ, scsiseq & (ENSELI|ENRSELI|ENAUTOATNP)); 6101 ahc_outb(ahc, SBLKCTL, sblkctl); 6102 restart_needed = FALSE; 6103 } else { 6104 /* Case 2: A command from this bus is active or we're idle */ 6105 simode1 = ahc_inb(ahc, SIMODE1) & ~(ENBUSFREE|ENSCSIRST); 6106 #ifdef AHC_TARGET_MODE 6107 /* 6108 * Bus resets clear ENSELI, so we cannot 6109 * defer re-enabling bus reset interrupts 6110 * if we are in target mode. 6111 */ 6112 if ((ahc->flags & AHC_TARGETROLE) != 0) 6113 simode1 |= ENSCSIRST; 6114 #endif 6115 ahc_outb(ahc, SIMODE1, simode1); 6116 if (initiate_reset) 6117 ahc_reset_current_bus(ahc); 6118 ahc_clear_intstat(ahc); 6119 ahc_outb(ahc, SCSISEQ, scsiseq & (ENSELI|ENRSELI|ENAUTOATNP)); 6120 restart_needed = TRUE; 6121 } 6122 6123 /* 6124 * Clean up all the state information for the 6125 * pending transactions on this bus. 6126 */ 6127 found = ahc_abort_scbs(ahc, CAM_TARGET_WILDCARD, channel, 6128 CAM_LUN_WILDCARD, SCB_LIST_NULL, 6129 ROLE_UNKNOWN, CAM_SCSI_BUS_RESET); 6130 6131 max_scsiid = (ahc->features & AHC_WIDE) ? 15 : 7; 6132 6133 #ifdef AHC_TARGET_MODE 6134 /* 6135 * Send an immediate notify ccb to all target more peripheral 6136 * drivers affected by this action. 6137 */ 6138 for (target = 0; target <= max_scsiid; target++) { 6139 struct ahc_tmode_tstate* tstate; 6140 u_int lun; 6141 6142 tstate = ahc->enabled_targets[target]; 6143 if (tstate == NULL) 6144 continue; 6145 for (lun = 0; lun < AHC_NUM_LUNS; lun++) { 6146 struct ahc_tmode_lstate* lstate; 6147 6148 lstate = tstate->enabled_luns[lun]; 6149 if (lstate == NULL) 6150 continue; 6151 6152 ahc_queue_lstate_event(ahc, lstate, CAM_TARGET_WILDCARD, 6153 EVENT_TYPE_BUS_RESET, /*arg*/0); 6154 ahc_send_lstate_events(ahc, lstate); 6155 } 6156 } 6157 #endif 6158 /* Notify the XPT that a bus reset occurred */ 6159 ahc_send_async(ahc, devinfo.channel, CAM_TARGET_WILDCARD, 6160 CAM_LUN_WILDCARD, AC_BUS_RESET, NULL); 6161 6162 /* 6163 * Revert to async/narrow transfers until we renegotiate. 6164 */ 6165 for (target = 0; target <= max_scsiid; target++) { 6166 if (ahc->enabled_targets[target] == NULL) 6167 continue; 6168 for (initiator = 0; initiator <= max_scsiid; initiator++) { 6169 struct ahc_devinfo devinfo; 6170 6171 ahc_compile_devinfo(&devinfo, target, initiator, 6172 CAM_LUN_WILDCARD, 6173 channel, ROLE_UNKNOWN); 6174 ahc_set_width(ahc, &devinfo, MSG_EXT_WDTR_BUS_8_BIT, 6175 AHC_TRANS_CUR, /*paused*/TRUE); 6176 ahc_set_syncrate(ahc, &devinfo, /*syncrate*/NULL, 6177 /*period*/0, /*offset*/0, 6178 /*ppr_options*/0, AHC_TRANS_CUR, 6179 /*paused*/TRUE); 6180 } 6181 } 6182 6183 if (restart_needed) 6184 ahc_restart(ahc); 6185 else 6186 ahc_unpause(ahc); 6187 return found; 6188 } 6189 6190 /***************************** Residual Processing ****************************/ 6191 /* 6192 * Calculate the residual for a just completed SCB. 6193 */ 6194 void 6195 ahc_calc_residual(struct ahc_softc *ahc, struct scb *scb) 6196 { 6197 struct hardware_scb *hscb; 6198 struct status_pkt *spkt; 6199 uint32_t sgptr; 6200 uint32_t resid_sgptr; 6201 uint32_t resid; 6202 6203 /* 6204 * 5 cases. 6205 * 1) No residual. 6206 * SG_RESID_VALID clear in sgptr. 6207 * 2) Transferless command 6208 * 3) Never performed any transfers. 6209 * sgptr has SG_FULL_RESID set. 6210 * 4) No residual but target did not 6211 * save data pointers after the 6212 * last transfer, so sgptr was 6213 * never updated. 6214 * 5) We have a partial residual. 6215 * Use residual_sgptr to determine 6216 * where we are. 6217 */ 6218 6219 hscb = scb->hscb; 6220 sgptr = aic_le32toh(hscb->sgptr); 6221 if ((sgptr & SG_RESID_VALID) == 0) 6222 /* Case 1 */ 6223 return; 6224 sgptr &= ~SG_RESID_VALID; 6225 6226 if ((sgptr & SG_LIST_NULL) != 0) 6227 /* Case 2 */ 6228 return; 6229 6230 spkt = &hscb->shared_data.status; 6231 resid_sgptr = aic_le32toh(spkt->residual_sg_ptr); 6232 if ((sgptr & SG_FULL_RESID) != 0) { 6233 /* Case 3 */ 6234 resid = aic_get_transfer_length(scb); 6235 } else if ((resid_sgptr & SG_LIST_NULL) != 0) { 6236 /* Case 4 */ 6237 return; 6238 } else if ((resid_sgptr & ~SG_PTR_MASK) != 0) { 6239 panic("Bogus resid sgptr value 0x%x\n", resid_sgptr); 6240 /* NOTREACHED */ 6241 return; 6242 } else { 6243 struct ahc_dma_seg *sg; 6244 6245 /* 6246 * Remainder of the SG where the transfer 6247 * stopped. 6248 */ 6249 resid = aic_le32toh(spkt->residual_datacnt) & AHC_SG_LEN_MASK; 6250 sg = ahc_sg_bus_to_virt(scb, resid_sgptr & SG_PTR_MASK); 6251 6252 /* The residual sg_ptr always points to the next sg */ 6253 sg--; 6254 6255 /* 6256 * Add up the contents of all residual 6257 * SG segments that are after the SG where 6258 * the transfer stopped. 6259 */ 6260 while ((aic_le32toh(sg->len) & AHC_DMA_LAST_SEG) == 0) { 6261 sg++; 6262 resid += aic_le32toh(sg->len) & AHC_SG_LEN_MASK; 6263 } 6264 } 6265 if ((scb->flags & SCB_SENSE) == 0) 6266 aic_set_residual(scb, resid); 6267 else 6268 aic_set_sense_residual(scb, resid); 6269 6270 #ifdef AHC_DEBUG 6271 if ((ahc_debug & AHC_SHOW_MISC) != 0) { 6272 ahc_print_path(ahc, scb); 6273 printf("Handled %sResidual of %d bytes\n", 6274 (scb->flags & SCB_SENSE) ? "Sense " : "", resid); 6275 } 6276 #endif 6277 } 6278 6279 /******************************* Target Mode **********************************/ 6280 #ifdef AHC_TARGET_MODE 6281 /* 6282 * Add a target mode event to this lun's queue 6283 */ 6284 static void 6285 ahc_queue_lstate_event(struct ahc_softc *ahc, struct ahc_tmode_lstate *lstate, 6286 u_int initiator_id, u_int event_type, u_int event_arg) 6287 { 6288 struct ahc_tmode_event *event; 6289 int pending; 6290 6291 xpt_freeze_devq(lstate->path, /*count*/1); 6292 if (lstate->event_w_idx >= lstate->event_r_idx) 6293 pending = lstate->event_w_idx - lstate->event_r_idx; 6294 else 6295 pending = AHC_TMODE_EVENT_BUFFER_SIZE + 1 6296 - (lstate->event_r_idx - lstate->event_w_idx); 6297 6298 if (event_type == EVENT_TYPE_BUS_RESET 6299 || event_type == MSG_BUS_DEV_RESET) { 6300 /* 6301 * Any earlier events are irrelevant, so reset our buffer. 6302 * This has the effect of allowing us to deal with reset 6303 * floods (an external device holding down the reset line) 6304 * without losing the event that is really interesting. 6305 */ 6306 lstate->event_r_idx = 0; 6307 lstate->event_w_idx = 0; 6308 xpt_release_devq(lstate->path, pending, /*runqueue*/FALSE); 6309 } 6310 6311 if (pending == AHC_TMODE_EVENT_BUFFER_SIZE) { 6312 xpt_print_path(lstate->path); 6313 printf("immediate event %x:%x lost\n", 6314 lstate->event_buffer[lstate->event_r_idx].event_type, 6315 lstate->event_buffer[lstate->event_r_idx].event_arg); 6316 lstate->event_r_idx++; 6317 if (lstate->event_r_idx == AHC_TMODE_EVENT_BUFFER_SIZE) 6318 lstate->event_r_idx = 0; 6319 xpt_release_devq(lstate->path, /*count*/1, /*runqueue*/FALSE); 6320 } 6321 6322 event = &lstate->event_buffer[lstate->event_w_idx]; 6323 event->initiator_id = initiator_id; 6324 event->event_type = event_type; 6325 event->event_arg = event_arg; 6326 lstate->event_w_idx++; 6327 if (lstate->event_w_idx == AHC_TMODE_EVENT_BUFFER_SIZE) 6328 lstate->event_w_idx = 0; 6329 } 6330 6331 /* 6332 * Send any target mode events queued up waiting 6333 * for immediate notify resources. 6334 */ 6335 void 6336 ahc_send_lstate_events(struct ahc_softc *ahc, struct ahc_tmode_lstate *lstate) 6337 { 6338 struct ccb_hdr *ccbh; 6339 struct ccb_immediate_notify *inot; 6340 6341 while (lstate->event_r_idx != lstate->event_w_idx 6342 && (ccbh = SLIST_FIRST(&lstate->immed_notifies)) != NULL) { 6343 struct ahc_tmode_event *event; 6344 6345 event = &lstate->event_buffer[lstate->event_r_idx]; 6346 SLIST_REMOVE_HEAD(&lstate->immed_notifies, sim_links.sle); 6347 inot = (struct ccb_immediate_notify *)ccbh; 6348 switch (event->event_type) { 6349 case EVENT_TYPE_BUS_RESET: 6350 ccbh->status = CAM_SCSI_BUS_RESET|CAM_DEV_QFRZN; 6351 break; 6352 default: 6353 ccbh->status = CAM_MESSAGE_RECV|CAM_DEV_QFRZN; 6354 inot->arg = event->event_type; 6355 inot->seq_id = event->event_arg; 6356 break; 6357 } 6358 inot->initiator_id = event->initiator_id; 6359 xpt_done((union ccb *)inot); 6360 lstate->event_r_idx++; 6361 if (lstate->event_r_idx == AHC_TMODE_EVENT_BUFFER_SIZE) 6362 lstate->event_r_idx = 0; 6363 } 6364 } 6365 #endif 6366 6367 /******************** Sequencer Program Patching/Download *********************/ 6368 6369 #ifdef AHC_DUMP_SEQ 6370 void 6371 ahc_dumpseq(struct ahc_softc* ahc) 6372 { 6373 int i; 6374 6375 ahc_outb(ahc, SEQCTL, PERRORDIS|FAILDIS|FASTMODE|LOADRAM); 6376 ahc_outb(ahc, SEQADDR0, 0); 6377 ahc_outb(ahc, SEQADDR1, 0); 6378 for (i = 0; i < ahc->instruction_ram_size; i++) { 6379 uint8_t ins_bytes[4]; 6380 6381 ahc_insb(ahc, SEQRAM, ins_bytes, 4); 6382 printf("0x%08x\n", ins_bytes[0] << 24 6383 | ins_bytes[1] << 16 6384 | ins_bytes[2] << 8 6385 | ins_bytes[3]); 6386 } 6387 } 6388 #endif 6389 6390 static int 6391 ahc_loadseq(struct ahc_softc *ahc) 6392 { 6393 struct cs cs_table[num_critical_sections]; 6394 u_int begin_set[num_critical_sections]; 6395 u_int end_set[num_critical_sections]; 6396 struct patch *cur_patch; 6397 u_int cs_count; 6398 u_int cur_cs; 6399 u_int i; 6400 u_int skip_addr; 6401 u_int sg_prefetch_cnt; 6402 int downloaded; 6403 uint8_t download_consts[7]; 6404 6405 /* 6406 * Start out with 0 critical sections 6407 * that apply to this firmware load. 6408 */ 6409 cs_count = 0; 6410 cur_cs = 0; 6411 memset(begin_set, 0, sizeof(begin_set)); 6412 memset(end_set, 0, sizeof(end_set)); 6413 6414 /* Setup downloadable constant table */ 6415 download_consts[QOUTFIFO_OFFSET] = 0; 6416 if (ahc->targetcmds != NULL) 6417 download_consts[QOUTFIFO_OFFSET] += 32; 6418 download_consts[QINFIFO_OFFSET] = download_consts[QOUTFIFO_OFFSET] + 1; 6419 download_consts[CACHESIZE_MASK] = ahc->pci_cachesize - 1; 6420 download_consts[INVERTED_CACHESIZE_MASK] = ~(ahc->pci_cachesize - 1); 6421 sg_prefetch_cnt = ahc->pci_cachesize; 6422 if (sg_prefetch_cnt < (2 * sizeof(struct ahc_dma_seg))) 6423 sg_prefetch_cnt = 2 * sizeof(struct ahc_dma_seg); 6424 download_consts[SG_PREFETCH_CNT] = sg_prefetch_cnt; 6425 download_consts[SG_PREFETCH_ALIGN_MASK] = ~(sg_prefetch_cnt - 1); 6426 download_consts[SG_PREFETCH_ADDR_MASK] = (sg_prefetch_cnt - 1); 6427 6428 cur_patch = patches; 6429 downloaded = 0; 6430 skip_addr = 0; 6431 ahc_outb(ahc, SEQCTL, PERRORDIS|FAILDIS|FASTMODE|LOADRAM); 6432 ahc_outb(ahc, SEQADDR0, 0); 6433 ahc_outb(ahc, SEQADDR1, 0); 6434 6435 for (i = 0; i < sizeof(seqprog)/4; i++) { 6436 if (ahc_check_patch(ahc, &cur_patch, i, &skip_addr) == 0) { 6437 /* 6438 * Don't download this instruction as it 6439 * is in a patch that was removed. 6440 */ 6441 continue; 6442 } 6443 6444 if (downloaded == ahc->instruction_ram_size) { 6445 /* 6446 * We're about to exceed the instruction 6447 * storage capacity for this chip. Fail 6448 * the load. 6449 */ 6450 printf("\n%s: Program too large for instruction memory " 6451 "size of %d!\n", ahc_name(ahc), 6452 ahc->instruction_ram_size); 6453 return (ENOMEM); 6454 } 6455 6456 /* 6457 * Move through the CS table until we find a CS 6458 * that might apply to this instruction. 6459 */ 6460 for (; cur_cs < num_critical_sections; cur_cs++) { 6461 if (critical_sections[cur_cs].end <= i) { 6462 if (begin_set[cs_count] == TRUE 6463 && end_set[cs_count] == FALSE) { 6464 cs_table[cs_count].end = downloaded; 6465 end_set[cs_count] = TRUE; 6466 cs_count++; 6467 } 6468 continue; 6469 } 6470 if (critical_sections[cur_cs].begin <= i 6471 && begin_set[cs_count] == FALSE) { 6472 cs_table[cs_count].begin = downloaded; 6473 begin_set[cs_count] = TRUE; 6474 } 6475 break; 6476 } 6477 ahc_download_instr(ahc, i, download_consts); 6478 downloaded++; 6479 } 6480 6481 ahc->num_critical_sections = cs_count; 6482 if (cs_count != 0) { 6483 cs_count *= sizeof(struct cs); 6484 ahc->critical_sections = malloc(cs_count, M_DEVBUF, M_NOWAIT); 6485 if (ahc->critical_sections == NULL) 6486 panic("ahc_loadseq: Could not malloc"); 6487 memcpy(ahc->critical_sections, cs_table, cs_count); 6488 } 6489 ahc_outb(ahc, SEQCTL, PERRORDIS|FAILDIS|FASTMODE); 6490 6491 if (bootverbose) { 6492 printf(" %d instructions downloaded\n", downloaded); 6493 printf("%s: Features 0x%x, Bugs 0x%x, Flags 0x%x\n", 6494 ahc_name(ahc), ahc->features, ahc->bugs, ahc->flags); 6495 } 6496 return (0); 6497 } 6498 6499 static int 6500 ahc_check_patch(struct ahc_softc *ahc, struct patch **start_patch, 6501 u_int start_instr, u_int *skip_addr) 6502 { 6503 struct patch *cur_patch; 6504 struct patch *last_patch; 6505 u_int num_patches; 6506 6507 num_patches = sizeof(patches)/sizeof(struct patch); 6508 last_patch = &patches[num_patches]; 6509 cur_patch = *start_patch; 6510 6511 while (cur_patch < last_patch && start_instr == cur_patch->begin) { 6512 if (cur_patch->patch_func(ahc) == 0) { 6513 /* Start rejecting code */ 6514 *skip_addr = start_instr + cur_patch->skip_instr; 6515 cur_patch += cur_patch->skip_patch; 6516 } else { 6517 /* Accepted this patch. Advance to the next 6518 * one and wait for our instruction pointer to 6519 * hit this point. 6520 */ 6521 cur_patch++; 6522 } 6523 } 6524 6525 *start_patch = cur_patch; 6526 if (start_instr < *skip_addr) 6527 /* Still skipping */ 6528 return (0); 6529 6530 return (1); 6531 } 6532 6533 static void 6534 ahc_download_instr(struct ahc_softc *ahc, u_int instrptr, uint8_t *dconsts) 6535 { 6536 union ins_formats instr; 6537 struct ins_format1 *fmt1_ins; 6538 struct ins_format3 *fmt3_ins; 6539 u_int opcode; 6540 6541 /* 6542 * The firmware is always compiled into a little endian format. 6543 */ 6544 instr.integer = aic_le32toh(*(uint32_t*)&seqprog[instrptr * 4]); 6545 6546 fmt1_ins = &instr.format1; 6547 fmt3_ins = NULL; 6548 6549 /* Pull the opcode */ 6550 opcode = instr.format1.opcode; 6551 switch (opcode) { 6552 case AIC_OP_JMP: 6553 case AIC_OP_JC: 6554 case AIC_OP_JNC: 6555 case AIC_OP_CALL: 6556 case AIC_OP_JNE: 6557 case AIC_OP_JNZ: 6558 case AIC_OP_JE: 6559 case AIC_OP_JZ: 6560 { 6561 struct patch *cur_patch; 6562 int address_offset; 6563 u_int address; 6564 u_int skip_addr; 6565 u_int i; 6566 6567 fmt3_ins = &instr.format3; 6568 address_offset = 0; 6569 address = fmt3_ins->address; 6570 cur_patch = patches; 6571 skip_addr = 0; 6572 6573 for (i = 0; i < address;) { 6574 ahc_check_patch(ahc, &cur_patch, i, &skip_addr); 6575 6576 if (skip_addr > i) { 6577 int end_addr; 6578 6579 end_addr = MIN(address, skip_addr); 6580 address_offset += end_addr - i; 6581 i = skip_addr; 6582 } else { 6583 i++; 6584 } 6585 } 6586 address -= address_offset; 6587 fmt3_ins->address = address; 6588 /* FALLTHROUGH */ 6589 } 6590 case AIC_OP_OR: 6591 case AIC_OP_AND: 6592 case AIC_OP_XOR: 6593 case AIC_OP_ADD: 6594 case AIC_OP_ADC: 6595 case AIC_OP_BMOV: 6596 if (fmt1_ins->parity != 0) { 6597 fmt1_ins->immediate = dconsts[fmt1_ins->immediate]; 6598 } 6599 fmt1_ins->parity = 0; 6600 if ((ahc->features & AHC_CMD_CHAN) == 0 6601 && opcode == AIC_OP_BMOV) { 6602 /* 6603 * Block move was added at the same time 6604 * as the command channel. Verify that 6605 * this is only a move of a single element 6606 * and convert the BMOV to a MOV 6607 * (AND with an immediate of FF). 6608 */ 6609 if (fmt1_ins->immediate != 1) 6610 panic("%s: BMOV not supported\n", 6611 ahc_name(ahc)); 6612 fmt1_ins->opcode = AIC_OP_AND; 6613 fmt1_ins->immediate = 0xff; 6614 } 6615 /* FALLTHROUGH */ 6616 case AIC_OP_ROL: 6617 if ((ahc->features & AHC_ULTRA2) != 0) { 6618 int i, count; 6619 6620 /* Calculate odd parity for the instruction */ 6621 for (i = 0, count = 0; i < 31; i++) { 6622 uint32_t mask; 6623 6624 mask = 0x01 << i; 6625 if ((instr.integer & mask) != 0) 6626 count++; 6627 } 6628 if ((count & 0x01) == 0) 6629 instr.format1.parity = 1; 6630 } else { 6631 /* Compress the instruction for older sequencers */ 6632 if (fmt3_ins != NULL) { 6633 instr.integer = 6634 fmt3_ins->immediate 6635 | (fmt3_ins->source << 8) 6636 | (fmt3_ins->address << 16) 6637 | (fmt3_ins->opcode << 25); 6638 } else { 6639 instr.integer = 6640 fmt1_ins->immediate 6641 | (fmt1_ins->source << 8) 6642 | (fmt1_ins->destination << 16) 6643 | (fmt1_ins->ret << 24) 6644 | (fmt1_ins->opcode << 25); 6645 } 6646 } 6647 /* The sequencer is a little endian cpu */ 6648 instr.integer = aic_htole32(instr.integer); 6649 ahc_outsb(ahc, SEQRAM, instr.bytes, 4); 6650 break; 6651 default: 6652 panic("Unknown opcode encountered in seq program"); 6653 break; 6654 } 6655 } 6656 6657 int 6658 ahc_print_register(ahc_reg_parse_entry_t *table, u_int num_entries, 6659 const char *name, u_int address, u_int value, 6660 u_int *cur_column, u_int wrap_point) 6661 { 6662 int printed; 6663 u_int printed_mask; 6664 u_int dummy_column; 6665 6666 if (cur_column == NULL) { 6667 dummy_column = 0; 6668 cur_column = &dummy_column; 6669 } 6670 6671 if (*cur_column >= wrap_point) { 6672 printf("\n"); 6673 *cur_column = 0; 6674 } 6675 printed = printf("%s[0x%x]", name, value); 6676 if (table == NULL) { 6677 printed += printf(" "); 6678 *cur_column += printed; 6679 return (printed); 6680 } 6681 printed_mask = 0; 6682 while (printed_mask != 0xFF) { 6683 int entry; 6684 6685 for (entry = 0; entry < num_entries; entry++) { 6686 if (((value & table[entry].mask) 6687 != table[entry].value) 6688 || ((printed_mask & table[entry].mask) 6689 == table[entry].mask)) 6690 continue; 6691 6692 printed += printf("%s%s", 6693 printed_mask == 0 ? ":(" : "|", 6694 table[entry].name); 6695 printed_mask |= table[entry].mask; 6696 6697 break; 6698 } 6699 if (entry >= num_entries) 6700 break; 6701 } 6702 if (printed_mask != 0) 6703 printed += printf(") "); 6704 else 6705 printed += printf(" "); 6706 if (cur_column != NULL) 6707 *cur_column += printed; 6708 return (printed); 6709 } 6710 6711 void 6712 ahc_dump_card_state(struct ahc_softc *ahc) 6713 { 6714 struct scb *scb; 6715 struct scb_tailq *untagged_q; 6716 u_int cur_col; 6717 int paused; 6718 int target; 6719 int maxtarget; 6720 int i; 6721 uint8_t last_phase; 6722 uint8_t qinpos; 6723 uint8_t qintail; 6724 uint8_t qoutpos; 6725 uint8_t scb_index; 6726 uint8_t saved_scbptr; 6727 6728 if (ahc_is_paused(ahc)) { 6729 paused = 1; 6730 } else { 6731 paused = 0; 6732 ahc_pause(ahc); 6733 } 6734 6735 saved_scbptr = ahc_inb(ahc, SCBPTR); 6736 last_phase = ahc_inb(ahc, LASTPHASE); 6737 printf(">>>>>>>>>>>>>>>>>> Dump Card State Begins <<<<<<<<<<<<<<<<<\n" 6738 "%s: Dumping Card State %s, at SEQADDR 0x%x\n", 6739 ahc_name(ahc), ahc_lookup_phase_entry(last_phase)->phasemsg, 6740 ahc_inb(ahc, SEQADDR0) | (ahc_inb(ahc, SEQADDR1) << 8)); 6741 if (paused) 6742 printf("Card was paused\n"); 6743 printf("ACCUM = 0x%x, SINDEX = 0x%x, DINDEX = 0x%x, ARG_2 = 0x%x\n", 6744 ahc_inb(ahc, ACCUM), ahc_inb(ahc, SINDEX), ahc_inb(ahc, DINDEX), 6745 ahc_inb(ahc, ARG_2)); 6746 printf("HCNT = 0x%x SCBPTR = 0x%x\n", ahc_inb(ahc, HCNT), 6747 ahc_inb(ahc, SCBPTR)); 6748 cur_col = 0; 6749 if ((ahc->features & AHC_DT) != 0) 6750 ahc_scsiphase_print(ahc_inb(ahc, SCSIPHASE), &cur_col, 50); 6751 ahc_scsisigi_print(ahc_inb(ahc, SCSISIGI), &cur_col, 50); 6752 ahc_error_print(ahc_inb(ahc, ERROR), &cur_col, 50); 6753 ahc_scsibusl_print(ahc_inb(ahc, SCSIBUSL), &cur_col, 50); 6754 ahc_lastphase_print(ahc_inb(ahc, LASTPHASE), &cur_col, 50); 6755 ahc_scsiseq_print(ahc_inb(ahc, SCSISEQ), &cur_col, 50); 6756 ahc_sblkctl_print(ahc_inb(ahc, SBLKCTL), &cur_col, 50); 6757 ahc_scsirate_print(ahc_inb(ahc, SCSIRATE), &cur_col, 50); 6758 ahc_seqctl_print(ahc_inb(ahc, SEQCTL), &cur_col, 50); 6759 ahc_seq_flags_print(ahc_inb(ahc, SEQ_FLAGS), &cur_col, 50); 6760 ahc_sstat0_print(ahc_inb(ahc, SSTAT0), &cur_col, 50); 6761 ahc_sstat1_print(ahc_inb(ahc, SSTAT1), &cur_col, 50); 6762 ahc_sstat2_print(ahc_inb(ahc, SSTAT2), &cur_col, 50); 6763 ahc_sstat3_print(ahc_inb(ahc, SSTAT3), &cur_col, 50); 6764 ahc_simode0_print(ahc_inb(ahc, SIMODE0), &cur_col, 50); 6765 ahc_simode1_print(ahc_inb(ahc, SIMODE1), &cur_col, 50); 6766 ahc_sxfrctl0_print(ahc_inb(ahc, SXFRCTL0), &cur_col, 50); 6767 ahc_dfcntrl_print(ahc_inb(ahc, DFCNTRL), &cur_col, 50); 6768 ahc_dfstatus_print(ahc_inb(ahc, DFSTATUS), &cur_col, 50); 6769 if (cur_col != 0) 6770 printf("\n"); 6771 printf("STACK:"); 6772 for (i = 0; i < STACK_SIZE; i++) 6773 printf(" 0x%x", ahc_inb(ahc, STACK)|(ahc_inb(ahc, STACK) << 8)); 6774 printf("\nSCB count = %d\n", ahc->scb_data->numscbs); 6775 printf("Kernel NEXTQSCB = %d\n", ahc->next_queued_scb->hscb->tag); 6776 printf("Card NEXTQSCB = %d\n", ahc_inb(ahc, NEXT_QUEUED_SCB)); 6777 /* QINFIFO */ 6778 printf("QINFIFO entries: "); 6779 if ((ahc->features & AHC_QUEUE_REGS) != 0) { 6780 qinpos = ahc_inb(ahc, SNSCB_QOFF); 6781 ahc_outb(ahc, SNSCB_QOFF, qinpos); 6782 } else 6783 qinpos = ahc_inb(ahc, QINPOS); 6784 qintail = ahc->qinfifonext; 6785 while (qinpos != qintail) { 6786 printf("%d ", ahc->qinfifo[qinpos]); 6787 qinpos++; 6788 } 6789 printf("\n"); 6790 6791 printf("Waiting Queue entries: "); 6792 scb_index = ahc_inb(ahc, WAITING_SCBH); 6793 i = 0; 6794 while (scb_index != SCB_LIST_NULL && i++ < 256) { 6795 ahc_outb(ahc, SCBPTR, scb_index); 6796 printf("%d:%d ", scb_index, ahc_inb(ahc, SCB_TAG)); 6797 scb_index = ahc_inb(ahc, SCB_NEXT); 6798 } 6799 printf("\n"); 6800 6801 printf("Disconnected Queue entries: "); 6802 scb_index = ahc_inb(ahc, DISCONNECTED_SCBH); 6803 i = 0; 6804 while (scb_index != SCB_LIST_NULL && i++ < 256) { 6805 ahc_outb(ahc, SCBPTR, scb_index); 6806 printf("%d:%d ", scb_index, ahc_inb(ahc, SCB_TAG)); 6807 scb_index = ahc_inb(ahc, SCB_NEXT); 6808 } 6809 printf("\n"); 6810 6811 ahc_sync_qoutfifo(ahc, BUS_DMASYNC_POSTREAD); 6812 printf("QOUTFIFO entries: "); 6813 qoutpos = ahc->qoutfifonext; 6814 i = 0; 6815 while (ahc->qoutfifo[qoutpos] != SCB_LIST_NULL && i++ < 256) { 6816 printf("%d ", ahc->qoutfifo[qoutpos]); 6817 qoutpos++; 6818 } 6819 printf("\n"); 6820 6821 printf("Sequencer Free SCB List: "); 6822 scb_index = ahc_inb(ahc, FREE_SCBH); 6823 i = 0; 6824 while (scb_index != SCB_LIST_NULL && i++ < 256) { 6825 ahc_outb(ahc, SCBPTR, scb_index); 6826 printf("%d ", scb_index); 6827 scb_index = ahc_inb(ahc, SCB_NEXT); 6828 } 6829 printf("\n"); 6830 6831 printf("Sequencer SCB Info: "); 6832 for (i = 0; i < ahc->scb_data->maxhscbs; i++) { 6833 ahc_outb(ahc, SCBPTR, i); 6834 cur_col = printf("\n%3d ", i); 6835 6836 ahc_scb_control_print(ahc_inb(ahc, SCB_CONTROL), &cur_col, 60); 6837 ahc_scb_scsiid_print(ahc_inb(ahc, SCB_SCSIID), &cur_col, 60); 6838 ahc_scb_lun_print(ahc_inb(ahc, SCB_LUN), &cur_col, 60); 6839 ahc_scb_tag_print(ahc_inb(ahc, SCB_TAG), &cur_col, 60); 6840 } 6841 printf("\n"); 6842 6843 printf("Pending list: "); 6844 i = 0; 6845 LIST_FOREACH(scb, &ahc->pending_scbs, pending_links) { 6846 if (i++ > 256) 6847 break; 6848 cur_col = printf("\n%3d ", scb->hscb->tag); 6849 ahc_scb_control_print(scb->hscb->control, &cur_col, 60); 6850 ahc_scb_scsiid_print(scb->hscb->scsiid, &cur_col, 60); 6851 ahc_scb_lun_print(scb->hscb->lun, &cur_col, 60); 6852 if ((ahc->flags & AHC_PAGESCBS) == 0) { 6853 ahc_outb(ahc, SCBPTR, scb->hscb->tag); 6854 printf("("); 6855 ahc_scb_control_print(ahc_inb(ahc, SCB_CONTROL), 6856 &cur_col, 60); 6857 ahc_scb_tag_print(ahc_inb(ahc, SCB_TAG), &cur_col, 60); 6858 printf(")"); 6859 } 6860 } 6861 printf("\n"); 6862 6863 printf("Kernel Free SCB list: "); 6864 i = 0; 6865 SLIST_FOREACH(scb, &ahc->scb_data->free_scbs, links.sle) { 6866 if (i++ > 256) 6867 break; 6868 printf("%d ", scb->hscb->tag); 6869 } 6870 printf("\n"); 6871 6872 maxtarget = (ahc->features & (AHC_WIDE|AHC_TWIN)) ? 15 : 7; 6873 for (target = 0; target <= maxtarget; target++) { 6874 untagged_q = &ahc->untagged_queues[target]; 6875 if (TAILQ_FIRST(untagged_q) == NULL) 6876 continue; 6877 printf("Untagged Q(%d): ", target); 6878 i = 0; 6879 TAILQ_FOREACH(scb, untagged_q, links.tqe) { 6880 if (i++ > 256) 6881 break; 6882 printf("%d ", scb->hscb->tag); 6883 } 6884 printf("\n"); 6885 } 6886 6887 ahc_platform_dump_card_state(ahc); 6888 printf("\n<<<<<<<<<<<<<<<<< Dump Card State Ends >>>>>>>>>>>>>>>>>>\n"); 6889 ahc_outb(ahc, SCBPTR, saved_scbptr); 6890 if (paused == 0) 6891 ahc_unpause(ahc); 6892 } 6893 6894 /*************************** Timeout Handling *********************************/ 6895 void 6896 ahc_timeout(struct scb *scb) 6897 { 6898 struct ahc_softc *ahc; 6899 6900 ahc = scb->ahc_softc; 6901 if ((scb->flags & SCB_ACTIVE) != 0) { 6902 if ((scb->flags & SCB_TIMEDOUT) == 0) { 6903 LIST_INSERT_HEAD(&ahc->timedout_scbs, scb, 6904 timedout_links); 6905 scb->flags |= SCB_TIMEDOUT; 6906 } 6907 ahc_wakeup_recovery_thread(ahc); 6908 } 6909 } 6910 6911 /* 6912 * Re-schedule a timeout for the passed in SCB if we determine that some 6913 * other SCB is in the process of recovery or an SCB with a longer 6914 * timeout is still pending. Limit our search to just "other_scb" 6915 * if it is non-NULL. 6916 */ 6917 static int 6918 ahc_other_scb_timeout(struct ahc_softc *ahc, struct scb *scb, 6919 struct scb *other_scb) 6920 { 6921 u_int newtimeout; 6922 int found; 6923 6924 ahc_print_path(ahc, scb); 6925 printf("Other SCB Timeout%s", 6926 (scb->flags & SCB_OTHERTCL_TIMEOUT) != 0 6927 ? " again\n" : "\n"); 6928 6929 newtimeout = aic_get_timeout(scb); 6930 scb->flags |= SCB_OTHERTCL_TIMEOUT; 6931 found = 0; 6932 if (other_scb != NULL) { 6933 if ((other_scb->flags 6934 & (SCB_OTHERTCL_TIMEOUT|SCB_TIMEDOUT)) == 0 6935 || (other_scb->flags & SCB_RECOVERY_SCB) != 0) { 6936 found++; 6937 newtimeout = MAX(aic_get_timeout(other_scb), 6938 newtimeout); 6939 } 6940 } else { 6941 LIST_FOREACH(other_scb, &ahc->pending_scbs, pending_links) { 6942 if ((other_scb->flags 6943 & (SCB_OTHERTCL_TIMEOUT|SCB_TIMEDOUT)) == 0 6944 || (other_scb->flags & SCB_RECOVERY_SCB) != 0) { 6945 found++; 6946 newtimeout = 6947 MAX(aic_get_timeout(other_scb), 6948 newtimeout); 6949 } 6950 } 6951 } 6952 6953 if (found != 0) 6954 aic_scb_timer_reset(scb, newtimeout); 6955 else { 6956 ahc_print_path(ahc, scb); 6957 printf("No other SCB worth waiting for...\n"); 6958 } 6959 6960 return (found != 0); 6961 } 6962 6963 /* 6964 * ahc_recover_commands determines if any of the commands that have currently 6965 * timedout are the root cause for this timeout. Innocent commands are given 6966 * a new timeout while we wait for the command executing on the bus to timeout. 6967 * This routine is invoked from a thread context so we are allowed to sleep. 6968 * Our lock is not held on entry. 6969 */ 6970 void 6971 ahc_recover_commands(struct ahc_softc *ahc) 6972 { 6973 struct scb *scb; 6974 int found; 6975 int restart_needed; 6976 u_int last_phase; 6977 6978 /* 6979 * Pause the controller and manually flush any 6980 * commands that have just completed but that our 6981 * interrupt handler has yet to see. 6982 */ 6983 ahc_pause_and_flushwork(ahc); 6984 6985 if (LIST_EMPTY(&ahc->timedout_scbs) != 0) { 6986 /* 6987 * The timedout commands have already 6988 * completed. This typically means 6989 * that either the timeout value was on 6990 * the hairy edge of what the device 6991 * requires or - more likely - interrupts 6992 * are not happening. 6993 */ 6994 printf("%s: Timedout SCBs already complete. " 6995 "Interrupts may not be functioning.\n", ahc_name(ahc)); 6996 ahc_unpause(ahc); 6997 return; 6998 } 6999 7000 restart_needed = 0; 7001 printf("%s: Recovery Initiated\n", ahc_name(ahc)); 7002 ahc_dump_card_state(ahc); 7003 7004 last_phase = ahc_inb(ahc, LASTPHASE); 7005 while ((scb = LIST_FIRST(&ahc->timedout_scbs)) != NULL) { 7006 u_int active_scb_index; 7007 u_int saved_scbptr; 7008 int target; 7009 int lun; 7010 int i; 7011 char channel; 7012 7013 target = SCB_GET_TARGET(ahc, scb); 7014 channel = SCB_GET_CHANNEL(ahc, scb); 7015 lun = SCB_GET_LUN(scb); 7016 7017 ahc_print_path(ahc, scb); 7018 printf("SCB 0x%x - timed out\n", scb->hscb->tag); 7019 if (scb->sg_count > 0) { 7020 for (i = 0; i < scb->sg_count; i++) { 7021 printf("sg[%d] - Addr 0x%x : Length %d\n", 7022 i, 7023 scb->sg_list[i].addr, 7024 scb->sg_list[i].len & AHC_SG_LEN_MASK); 7025 } 7026 } 7027 if (scb->flags & (SCB_DEVICE_RESET|SCB_ABORT)) { 7028 /* 7029 * Been down this road before. 7030 * Do a full bus reset. 7031 */ 7032 aic_set_transaction_status(scb, CAM_CMD_TIMEOUT); 7033 bus_reset: 7034 found = ahc_reset_channel(ahc, channel, 7035 /*Initiate Reset*/TRUE); 7036 printf("%s: Issued Channel %c Bus Reset. " 7037 "%d SCBs aborted\n", ahc_name(ahc), channel, 7038 found); 7039 continue; 7040 } 7041 7042 /* 7043 * Remove the command from the timedout list in 7044 * preparation for requeing it. 7045 */ 7046 LIST_REMOVE(scb, timedout_links); 7047 scb->flags &= ~SCB_TIMEDOUT; 7048 7049 /* 7050 * If we are a target, transition to bus free and report 7051 * the timeout. 7052 * 7053 * The target/initiator that is holding up the bus may not 7054 * be the same as the one that triggered this timeout 7055 * (different commands have different timeout lengths). 7056 * If the bus is idle and we are actiing as the initiator 7057 * for this request, queue a BDR message to the timed out 7058 * target. Otherwise, if the timed out transaction is 7059 * active: 7060 * Initiator transaction: 7061 * Stuff the message buffer with a BDR message and assert 7062 * ATN in the hopes that the target will let go of the bus 7063 * and go to the mesgout phase. If this fails, we'll 7064 * get another timeout 2 seconds later which will attempt 7065 * a bus reset. 7066 * 7067 * Target transaction: 7068 * Transition to BUS FREE and report the error. 7069 * It's good to be the target! 7070 */ 7071 saved_scbptr = ahc_inb(ahc, SCBPTR); 7072 active_scb_index = ahc_inb(ahc, SCB_TAG); 7073 7074 if ((ahc_inb(ahc, SEQ_FLAGS) & NOT_IDENTIFIED) == 0 7075 && (active_scb_index < ahc->scb_data->numscbs)) { 7076 struct scb *active_scb; 7077 7078 /* 7079 * If the active SCB is not us, assume that 7080 * the active SCB has a longer timeout than 7081 * the timedout SCB, and wait for the active 7082 * SCB to timeout. 7083 */ 7084 active_scb = ahc_lookup_scb(ahc, active_scb_index); 7085 if (active_scb != scb) { 7086 if (ahc_other_scb_timeout(ahc, scb, 7087 active_scb) == 0) 7088 goto bus_reset; 7089 continue; 7090 } 7091 7092 /* It's us */ 7093 if ((scb->flags & SCB_TARGET_SCB) != 0) { 7094 /* 7095 * Send back any queued up transactions 7096 * and properly record the error condition. 7097 */ 7098 ahc_abort_scbs(ahc, SCB_GET_TARGET(ahc, scb), 7099 SCB_GET_CHANNEL(ahc, scb), 7100 SCB_GET_LUN(scb), 7101 scb->hscb->tag, 7102 ROLE_TARGET, 7103 CAM_CMD_TIMEOUT); 7104 7105 /* Will clear us from the bus */ 7106 restart_needed = 1; 7107 break; 7108 } 7109 7110 ahc_set_recoveryscb(ahc, active_scb); 7111 ahc_outb(ahc, MSG_OUT, HOST_MSG); 7112 ahc_outb(ahc, SCSISIGO, last_phase|ATNO); 7113 ahc_print_path(ahc, active_scb); 7114 printf("BDR message in message buffer\n"); 7115 active_scb->flags |= SCB_DEVICE_RESET; 7116 aic_scb_timer_reset(scb, 2 * 1000); 7117 } else if (last_phase != P_BUSFREE 7118 && (ahc_inb(ahc, SSTAT1) & REQINIT) == 0) { 7119 /* 7120 * SCB is not identified, there 7121 * is no pending REQ, and the sequencer 7122 * has not seen a busfree. Looks like 7123 * a stuck connection waiting to 7124 * go busfree. Reset the bus. 7125 */ 7126 printf("%s: Connection stuck awaiting busfree or " 7127 "Identify Msg.\n", ahc_name(ahc)); 7128 goto bus_reset; 7129 } else { 7130 int disconnected; 7131 7132 if (last_phase != P_BUSFREE 7133 && (ahc_inb(ahc, SSTAT0) & TARGET) != 0) { 7134 /* Hung target selection. Goto busfree */ 7135 printf("%s: Hung target selection\n", 7136 ahc_name(ahc)); 7137 restart_needed = 1; 7138 break; 7139 } 7140 7141 /* XXX Shouldn't panic. Just punt instead? */ 7142 if ((scb->flags & SCB_TARGET_SCB) != 0) 7143 panic("Timed-out target SCB but bus idle"); 7144 7145 if (ahc_search_qinfifo(ahc, target, channel, lun, 7146 scb->hscb->tag, ROLE_INITIATOR, 7147 /*status*/0, SEARCH_COUNT) > 0) { 7148 disconnected = FALSE; 7149 } else { 7150 disconnected = TRUE; 7151 } 7152 7153 if (disconnected) { 7154 ahc_set_recoveryscb(ahc, scb); 7155 /* 7156 * Actually re-queue this SCB in an attempt 7157 * to select the device before it reconnects. 7158 * In either case (selection or reselection), 7159 * we will now issue a target reset to the 7160 * timed-out device. 7161 * 7162 * Set the MK_MESSAGE control bit indicating 7163 * that we desire to send a message. We 7164 * also set the disconnected flag since 7165 * in the paging case there is no guarantee 7166 * that our SCB control byte matches the 7167 * version on the card. We don't want the 7168 * sequencer to abort the command thinking 7169 * an unsolicited reselection occurred. 7170 */ 7171 scb->hscb->control |= MK_MESSAGE|DISCONNECTED; 7172 scb->flags |= SCB_DEVICE_RESET; 7173 7174 /* 7175 * Remove any cached copy of this SCB in the 7176 * disconnected list in preparation for the 7177 * queuing of our abort SCB. We use the 7178 * same element in the SCB, SCB_NEXT, for 7179 * both the qinfifo and the disconnected list. 7180 */ 7181 ahc_search_disc_list(ahc, target, channel, 7182 lun, scb->hscb->tag, 7183 /*stop_on_first*/TRUE, 7184 /*remove*/TRUE, 7185 /*save_state*/FALSE); 7186 7187 /* 7188 * In the non-paging case, the sequencer will 7189 * never re-reference the in-core SCB. 7190 * To make sure we are notified during 7191 * reslection, set the MK_MESSAGE flag in 7192 * the card's copy of the SCB. 7193 */ 7194 if ((ahc->flags & AHC_PAGESCBS) == 0) { 7195 ahc_outb(ahc, SCBPTR, scb->hscb->tag); 7196 ahc_outb(ahc, SCB_CONTROL, 7197 ahc_inb(ahc, SCB_CONTROL) 7198 | MK_MESSAGE); 7199 } 7200 7201 /* 7202 * Clear out any entries in the QINFIFO first 7203 * so we are the next SCB for this target 7204 * to run. 7205 */ 7206 ahc_search_qinfifo(ahc, 7207 SCB_GET_TARGET(ahc, scb), 7208 channel, SCB_GET_LUN(scb), 7209 SCB_LIST_NULL, 7210 ROLE_INITIATOR, 7211 CAM_REQUEUE_REQ, 7212 SEARCH_COMPLETE); 7213 ahc_print_path(ahc, scb); 7214 printf("Queuing a BDR SCB\n"); 7215 ahc_qinfifo_requeue_tail(ahc, scb); 7216 ahc_outb(ahc, SCBPTR, saved_scbptr); 7217 aic_scb_timer_reset(scb, 2 * 1000); 7218 } else { 7219 /* Go "immediately" to the bus reset */ 7220 /* This shouldn't happen */ 7221 ahc_set_recoveryscb(ahc, scb); 7222 ahc_print_path(ahc, scb); 7223 printf("SCB %d: Immediate reset. " 7224 "Flags = 0x%x\n", scb->hscb->tag, 7225 scb->flags); 7226 goto bus_reset; 7227 } 7228 } 7229 break; 7230 } 7231 7232 /* 7233 * Any remaining SCBs were not the "culprit", so remove 7234 * them from the timeout list. The timer for these commands 7235 * will be reset once the recovery SCB completes. 7236 */ 7237 while ((scb = LIST_FIRST(&ahc->timedout_scbs)) != NULL) { 7238 LIST_REMOVE(scb, timedout_links); 7239 scb->flags &= ~SCB_TIMEDOUT; 7240 } 7241 7242 if (restart_needed) 7243 ahc_restart(ahc); 7244 else 7245 ahc_unpause(ahc); 7246 } 7247 7248 /************************* Target Mode ****************************************/ 7249 #ifdef AHC_TARGET_MODE 7250 cam_status 7251 ahc_find_tmode_devs(struct ahc_softc *ahc, struct cam_sim *sim, union ccb *ccb, 7252 struct ahc_tmode_tstate **tstate, 7253 struct ahc_tmode_lstate **lstate, 7254 int notfound_failure) 7255 { 7256 7257 if ((ahc->features & AHC_TARGETMODE) == 0) 7258 return (CAM_REQ_INVALID); 7259 7260 /* 7261 * Handle the 'black hole' device that sucks up 7262 * requests to unattached luns on enabled targets. 7263 */ 7264 if (ccb->ccb_h.target_id == CAM_TARGET_WILDCARD 7265 && ccb->ccb_h.target_lun == CAM_LUN_WILDCARD) { 7266 *tstate = NULL; 7267 *lstate = ahc->black_hole; 7268 } else { 7269 u_int max_id; 7270 7271 max_id = (ahc->features & AHC_WIDE) ? 15 : 7; 7272 if (ccb->ccb_h.target_id > max_id) 7273 return (CAM_TID_INVALID); 7274 7275 if (ccb->ccb_h.target_lun >= AHC_NUM_LUNS) 7276 return (CAM_LUN_INVALID); 7277 7278 *tstate = ahc->enabled_targets[ccb->ccb_h.target_id]; 7279 *lstate = NULL; 7280 if (*tstate != NULL) 7281 *lstate = 7282 (*tstate)->enabled_luns[ccb->ccb_h.target_lun]; 7283 } 7284 7285 if (notfound_failure != 0 && *lstate == NULL) 7286 return (CAM_PATH_INVALID); 7287 7288 return (CAM_REQ_CMP); 7289 } 7290 7291 void 7292 ahc_handle_en_lun(struct ahc_softc *ahc, struct cam_sim *sim, union ccb *ccb) 7293 { 7294 struct ahc_tmode_tstate *tstate; 7295 struct ahc_tmode_lstate *lstate; 7296 struct ccb_en_lun *cel; 7297 cam_status status; 7298 u_int target; 7299 u_int lun; 7300 u_int target_mask; 7301 u_int our_id; 7302 int error; 7303 char channel; 7304 7305 status = ahc_find_tmode_devs(ahc, sim, ccb, &tstate, &lstate, 7306 /*notfound_failure*/FALSE); 7307 7308 if (status != CAM_REQ_CMP) { 7309 ccb->ccb_h.status = status; 7310 return; 7311 } 7312 7313 if (cam_sim_bus(sim) == 0) 7314 our_id = ahc->our_id; 7315 else 7316 our_id = ahc->our_id_b; 7317 7318 if (ccb->ccb_h.target_id != our_id 7319 && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) { 7320 /* 7321 * our_id represents our initiator ID, or 7322 * the ID of the first target to have an 7323 * enabled lun in target mode. There are 7324 * two cases that may preclude enabling a 7325 * target id other than our_id. 7326 * 7327 * o our_id is for an active initiator role. 7328 * Since the hardware does not support 7329 * reselections to the initiator role at 7330 * anything other than our_id, and our_id 7331 * is used by the hardware to indicate the 7332 * ID to use for both select-out and 7333 * reselect-out operations, the only target 7334 * ID we can support in this mode is our_id. 7335 * 7336 * o The MULTARGID feature is not available and 7337 * a previous target mode ID has been enabled. 7338 */ 7339 if ((ahc->features & AHC_MULTIROLE) != 0) { 7340 if ((ahc->features & AHC_MULTI_TID) != 0 7341 && (ahc->flags & AHC_INITIATORROLE) != 0) { 7342 /* 7343 * Only allow additional targets if 7344 * the initiator role is disabled. 7345 * The hardware cannot handle a re-select-in 7346 * on the initiator id during a re-select-out 7347 * on a different target id. 7348 */ 7349 status = CAM_TID_INVALID; 7350 } else if ((ahc->flags & AHC_INITIATORROLE) != 0 7351 || ahc->enabled_luns > 0) { 7352 /* 7353 * Only allow our target id to change 7354 * if the initiator role is not configured 7355 * and there are no enabled luns which 7356 * are attached to the currently registered 7357 * scsi id. 7358 */ 7359 status = CAM_TID_INVALID; 7360 } 7361 } else if ((ahc->features & AHC_MULTI_TID) == 0 7362 && ahc->enabled_luns > 0) { 7363 status = CAM_TID_INVALID; 7364 } 7365 } 7366 7367 if (status != CAM_REQ_CMP) { 7368 ccb->ccb_h.status = status; 7369 return; 7370 } 7371 7372 /* 7373 * We now have an id that is valid. 7374 * If we aren't in target mode, switch modes. 7375 */ 7376 if ((ahc->flags & AHC_TARGETROLE) == 0 7377 && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) { 7378 ahc_flag saved_flags; 7379 7380 printf("Configuring Target Mode\n"); 7381 if (LIST_FIRST(&ahc->pending_scbs) != NULL) { 7382 ccb->ccb_h.status = CAM_BUSY; 7383 return; 7384 } 7385 saved_flags = ahc->flags; 7386 ahc->flags |= AHC_TARGETROLE; 7387 if ((ahc->features & AHC_MULTIROLE) == 0) 7388 ahc->flags &= ~AHC_INITIATORROLE; 7389 ahc_pause(ahc); 7390 error = ahc_loadseq(ahc); 7391 if (error != 0) { 7392 /* 7393 * Restore original configuration and notify 7394 * the caller that we cannot support target mode. 7395 * Since the adapter started out in this 7396 * configuration, the firmware load will succeed, 7397 * so there is no point in checking ahc_loadseq's 7398 * return value. 7399 */ 7400 ahc->flags = saved_flags; 7401 (void)ahc_loadseq(ahc); 7402 ahc_restart(ahc); 7403 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL; 7404 return; 7405 } 7406 ahc_restart(ahc); 7407 } 7408 cel = &ccb->cel; 7409 target = ccb->ccb_h.target_id; 7410 lun = ccb->ccb_h.target_lun; 7411 channel = SIM_CHANNEL(ahc, sim); 7412 target_mask = 0x01 << target; 7413 if (channel == 'B') 7414 target_mask <<= 8; 7415 7416 if (cel->enable != 0) { 7417 u_int scsiseq; 7418 7419 /* Are we already enabled?? */ 7420 if (lstate != NULL) { 7421 xpt_print_path(ccb->ccb_h.path); 7422 printf("Lun already enabled\n"); 7423 ccb->ccb_h.status = CAM_LUN_ALRDY_ENA; 7424 return; 7425 } 7426 7427 if (cel->grp6_len != 0 7428 || cel->grp7_len != 0) { 7429 /* 7430 * Don't (yet?) support vendor 7431 * specific commands. 7432 */ 7433 ccb->ccb_h.status = CAM_REQ_INVALID; 7434 printf("Non-zero Group Codes\n"); 7435 return; 7436 } 7437 7438 /* 7439 * Seems to be okay. 7440 * Setup our data structures. 7441 */ 7442 if (target != CAM_TARGET_WILDCARD && tstate == NULL) { 7443 tstate = ahc_alloc_tstate(ahc, target, channel); 7444 if (tstate == NULL) { 7445 xpt_print_path(ccb->ccb_h.path); 7446 printf("Couldn't allocate tstate\n"); 7447 ccb->ccb_h.status = CAM_RESRC_UNAVAIL; 7448 return; 7449 } 7450 } 7451 lstate = malloc(sizeof(*lstate), M_DEVBUF, M_NOWAIT); 7452 if (lstate == NULL) { 7453 xpt_print_path(ccb->ccb_h.path); 7454 printf("Couldn't allocate lstate\n"); 7455 ccb->ccb_h.status = CAM_RESRC_UNAVAIL; 7456 return; 7457 } 7458 memset(lstate, 0, sizeof(*lstate)); 7459 status = xpt_create_path(&lstate->path, /*periph*/NULL, 7460 xpt_path_path_id(ccb->ccb_h.path), 7461 xpt_path_target_id(ccb->ccb_h.path), 7462 xpt_path_lun_id(ccb->ccb_h.path)); 7463 if (status != CAM_REQ_CMP) { 7464 free(lstate, M_DEVBUF); 7465 xpt_print_path(ccb->ccb_h.path); 7466 printf("Couldn't allocate path\n"); 7467 ccb->ccb_h.status = CAM_RESRC_UNAVAIL; 7468 return; 7469 } 7470 SLIST_INIT(&lstate->accept_tios); 7471 SLIST_INIT(&lstate->immed_notifies); 7472 ahc_pause(ahc); 7473 if (target != CAM_TARGET_WILDCARD) { 7474 tstate->enabled_luns[lun] = lstate; 7475 ahc->enabled_luns++; 7476 7477 if ((ahc->features & AHC_MULTI_TID) != 0) { 7478 u_int targid_mask; 7479 7480 targid_mask = ahc_inb(ahc, TARGID) 7481 | (ahc_inb(ahc, TARGID + 1) << 8); 7482 7483 targid_mask |= target_mask; 7484 ahc_outb(ahc, TARGID, targid_mask); 7485 ahc_outb(ahc, TARGID+1, (targid_mask >> 8)); 7486 7487 ahc_update_scsiid(ahc, targid_mask); 7488 } else { 7489 u_int our_id; 7490 char channel; 7491 7492 channel = SIM_CHANNEL(ahc, sim); 7493 our_id = SIM_SCSI_ID(ahc, sim); 7494 7495 /* 7496 * This can only happen if selections 7497 * are not enabled 7498 */ 7499 if (target != our_id) { 7500 u_int sblkctl; 7501 char cur_channel; 7502 int swap; 7503 7504 sblkctl = ahc_inb(ahc, SBLKCTL); 7505 cur_channel = (sblkctl & SELBUSB) 7506 ? 'B' : 'A'; 7507 if ((ahc->features & AHC_TWIN) == 0) 7508 cur_channel = 'A'; 7509 swap = cur_channel != channel; 7510 if (channel == 'A') 7511 ahc->our_id = target; 7512 else 7513 ahc->our_id_b = target; 7514 7515 if (swap) 7516 ahc_outb(ahc, SBLKCTL, 7517 sblkctl ^ SELBUSB); 7518 7519 ahc_outb(ahc, SCSIID, target); 7520 7521 if (swap) 7522 ahc_outb(ahc, SBLKCTL, sblkctl); 7523 } 7524 } 7525 } else 7526 ahc->black_hole = lstate; 7527 /* Allow select-in operations */ 7528 if (ahc->black_hole != NULL && ahc->enabled_luns > 0) { 7529 scsiseq = ahc_inb(ahc, SCSISEQ_TEMPLATE); 7530 scsiseq |= ENSELI; 7531 ahc_outb(ahc, SCSISEQ_TEMPLATE, scsiseq); 7532 scsiseq = ahc_inb(ahc, SCSISEQ); 7533 scsiseq |= ENSELI; 7534 ahc_outb(ahc, SCSISEQ, scsiseq); 7535 } 7536 ahc_unpause(ahc); 7537 ccb->ccb_h.status = CAM_REQ_CMP; 7538 xpt_print_path(ccb->ccb_h.path); 7539 printf("Lun now enabled for target mode\n"); 7540 } else { 7541 struct scb *scb; 7542 int i, empty; 7543 7544 if (lstate == NULL) { 7545 ccb->ccb_h.status = CAM_LUN_INVALID; 7546 return; 7547 } 7548 7549 ccb->ccb_h.status = CAM_REQ_CMP; 7550 LIST_FOREACH(scb, &ahc->pending_scbs, pending_links) { 7551 struct ccb_hdr *ccbh; 7552 7553 ccbh = &scb->io_ctx->ccb_h; 7554 if (ccbh->func_code == XPT_CONT_TARGET_IO 7555 && !xpt_path_comp(ccbh->path, ccb->ccb_h.path)){ 7556 printf("CTIO pending\n"); 7557 ccb->ccb_h.status = CAM_REQ_INVALID; 7558 return; 7559 } 7560 } 7561 7562 if (SLIST_FIRST(&lstate->accept_tios) != NULL) { 7563 printf("ATIOs pending\n"); 7564 ccb->ccb_h.status = CAM_REQ_INVALID; 7565 } 7566 7567 if (SLIST_FIRST(&lstate->immed_notifies) != NULL) { 7568 printf("INOTs pending\n"); 7569 ccb->ccb_h.status = CAM_REQ_INVALID; 7570 } 7571 7572 if (ccb->ccb_h.status != CAM_REQ_CMP) { 7573 return; 7574 } 7575 7576 xpt_print_path(ccb->ccb_h.path); 7577 printf("Target mode disabled\n"); 7578 xpt_free_path(lstate->path); 7579 free(lstate, M_DEVBUF); 7580 7581 ahc_pause(ahc); 7582 /* Can we clean up the target too? */ 7583 if (target != CAM_TARGET_WILDCARD) { 7584 tstate->enabled_luns[lun] = NULL; 7585 ahc->enabled_luns--; 7586 for (empty = 1, i = 0; i < 8; i++) 7587 if (tstate->enabled_luns[i] != NULL) { 7588 empty = 0; 7589 break; 7590 } 7591 7592 if (empty) { 7593 ahc_free_tstate(ahc, target, channel, 7594 /*force*/FALSE); 7595 if (ahc->features & AHC_MULTI_TID) { 7596 u_int targid_mask; 7597 7598 targid_mask = ahc_inb(ahc, TARGID) 7599 | (ahc_inb(ahc, TARGID + 1) 7600 << 8); 7601 7602 targid_mask &= ~target_mask; 7603 ahc_outb(ahc, TARGID, targid_mask); 7604 ahc_outb(ahc, TARGID+1, 7605 (targid_mask >> 8)); 7606 ahc_update_scsiid(ahc, targid_mask); 7607 } 7608 } 7609 } else { 7610 ahc->black_hole = NULL; 7611 7612 /* 7613 * We can't allow selections without 7614 * our black hole device. 7615 */ 7616 empty = TRUE; 7617 } 7618 if (ahc->enabled_luns == 0) { 7619 /* Disallow select-in */ 7620 u_int scsiseq; 7621 7622 scsiseq = ahc_inb(ahc, SCSISEQ_TEMPLATE); 7623 scsiseq &= ~ENSELI; 7624 ahc_outb(ahc, SCSISEQ_TEMPLATE, scsiseq); 7625 scsiseq = ahc_inb(ahc, SCSISEQ); 7626 scsiseq &= ~ENSELI; 7627 ahc_outb(ahc, SCSISEQ, scsiseq); 7628 7629 if ((ahc->features & AHC_MULTIROLE) == 0) { 7630 printf("Configuring Initiator Mode\n"); 7631 ahc->flags &= ~AHC_TARGETROLE; 7632 ahc->flags |= AHC_INITIATORROLE; 7633 /* 7634 * Returning to a configuration that 7635 * fit previously will always succeed. 7636 */ 7637 (void)ahc_loadseq(ahc); 7638 ahc_restart(ahc); 7639 /* 7640 * Unpaused. The extra unpause 7641 * that follows is harmless. 7642 */ 7643 } 7644 } 7645 ahc_unpause(ahc); 7646 } 7647 } 7648 7649 static void 7650 ahc_update_scsiid(struct ahc_softc *ahc, u_int targid_mask) 7651 { 7652 u_int scsiid_mask; 7653 u_int scsiid; 7654 7655 if ((ahc->features & AHC_MULTI_TID) == 0) 7656 panic("ahc_update_scsiid called on non-multitid unit\n"); 7657 7658 /* 7659 * Since we will rely on the TARGID mask 7660 * for selection enables, ensure that OID 7661 * in SCSIID is not set to some other ID 7662 * that we don't want to allow selections on. 7663 */ 7664 if ((ahc->features & AHC_ULTRA2) != 0) 7665 scsiid = ahc_inb(ahc, SCSIID_ULTRA2); 7666 else 7667 scsiid = ahc_inb(ahc, SCSIID); 7668 scsiid_mask = 0x1 << (scsiid & OID); 7669 if ((targid_mask & scsiid_mask) == 0) { 7670 u_int our_id; 7671 7672 /* ffs counts from 1 */ 7673 our_id = ffs(targid_mask); 7674 if (our_id == 0) 7675 our_id = ahc->our_id; 7676 else 7677 our_id--; 7678 scsiid &= TID; 7679 scsiid |= our_id; 7680 } 7681 if ((ahc->features & AHC_ULTRA2) != 0) 7682 ahc_outb(ahc, SCSIID_ULTRA2, scsiid); 7683 else 7684 ahc_outb(ahc, SCSIID, scsiid); 7685 } 7686 7687 void 7688 ahc_run_tqinfifo(struct ahc_softc *ahc, int paused) 7689 { 7690 struct target_cmd *cmd; 7691 7692 /* 7693 * If the card supports auto-access pause, 7694 * we can access the card directly regardless 7695 * of whether it is paused or not. 7696 */ 7697 if ((ahc->features & AHC_AUTOPAUSE) != 0) 7698 paused = TRUE; 7699 7700 ahc_sync_tqinfifo(ahc, BUS_DMASYNC_POSTREAD); 7701 while ((cmd = &ahc->targetcmds[ahc->tqinfifonext])->cmd_valid != 0) { 7702 /* 7703 * Only advance through the queue if we 7704 * have the resources to process the command. 7705 */ 7706 if (ahc_handle_target_cmd(ahc, cmd) != 0) 7707 break; 7708 7709 cmd->cmd_valid = 0; 7710 aic_dmamap_sync(ahc, ahc->shared_data_dmat, 7711 ahc->shared_data_dmamap, 7712 ahc_targetcmd_offset(ahc, ahc->tqinfifonext), 7713 sizeof(struct target_cmd), 7714 BUS_DMASYNC_PREREAD); 7715 ahc->tqinfifonext++; 7716 7717 /* 7718 * Lazily update our position in the target mode incoming 7719 * command queue as seen by the sequencer. 7720 */ 7721 if ((ahc->tqinfifonext & (HOST_TQINPOS - 1)) == 1) { 7722 if ((ahc->features & AHC_HS_MAILBOX) != 0) { 7723 u_int hs_mailbox; 7724 7725 hs_mailbox = ahc_inb(ahc, HS_MAILBOX); 7726 hs_mailbox &= ~HOST_TQINPOS; 7727 hs_mailbox |= ahc->tqinfifonext & HOST_TQINPOS; 7728 ahc_outb(ahc, HS_MAILBOX, hs_mailbox); 7729 } else { 7730 if (!paused) 7731 ahc_pause(ahc); 7732 ahc_outb(ahc, KERNEL_TQINPOS, 7733 ahc->tqinfifonext & HOST_TQINPOS); 7734 if (!paused) 7735 ahc_unpause(ahc); 7736 } 7737 } 7738 } 7739 } 7740 7741 static int 7742 ahc_handle_target_cmd(struct ahc_softc *ahc, struct target_cmd *cmd) 7743 { 7744 struct ahc_tmode_tstate *tstate; 7745 struct ahc_tmode_lstate *lstate; 7746 struct ccb_accept_tio *atio; 7747 uint8_t *byte; 7748 int initiator; 7749 int target; 7750 int lun; 7751 7752 initiator = SCSIID_TARGET(ahc, cmd->scsiid); 7753 target = SCSIID_OUR_ID(cmd->scsiid); 7754 lun = (cmd->identify & MSG_IDENTIFY_LUNMASK); 7755 7756 byte = cmd->bytes; 7757 tstate = ahc->enabled_targets[target]; 7758 lstate = NULL; 7759 if (tstate != NULL) 7760 lstate = tstate->enabled_luns[lun]; 7761 7762 /* 7763 * Commands for disabled luns go to the black hole driver. 7764 */ 7765 if (lstate == NULL) 7766 lstate = ahc->black_hole; 7767 7768 atio = (struct ccb_accept_tio*)SLIST_FIRST(&lstate->accept_tios); 7769 if (atio == NULL) { 7770 ahc->flags |= AHC_TQINFIFO_BLOCKED; 7771 /* 7772 * Wait for more ATIOs from the peripheral driver for this lun. 7773 */ 7774 if (bootverbose) 7775 printf("%s: ATIOs exhausted\n", ahc_name(ahc)); 7776 return (1); 7777 } else 7778 ahc->flags &= ~AHC_TQINFIFO_BLOCKED; 7779 #ifdef AHC_DEBUG 7780 if (ahc_debug & AHC_SHOW_TQIN) { 7781 printf("Incoming command from %d for %d:%d%s\n", 7782 initiator, target, lun, 7783 lstate == ahc->black_hole ? "(Black Holed)" : ""); 7784 } 7785 #endif 7786 SLIST_REMOVE_HEAD(&lstate->accept_tios, sim_links.sle); 7787 7788 if (lstate == ahc->black_hole) { 7789 /* Fill in the wildcards */ 7790 atio->ccb_h.target_id = target; 7791 atio->ccb_h.target_lun = lun; 7792 } 7793 7794 /* 7795 * Package it up and send it off to 7796 * whomever has this lun enabled. 7797 */ 7798 atio->sense_len = 0; 7799 atio->init_id = initiator; 7800 if (byte[0] != 0xFF) { 7801 /* Tag was included */ 7802 atio->tag_action = *byte++; 7803 atio->tag_id = *byte++; 7804 atio->ccb_h.flags |= CAM_TAG_ACTION_VALID; 7805 } else { 7806 atio->ccb_h.flags &= ~CAM_TAG_ACTION_VALID; 7807 } 7808 byte++; 7809 7810 /* Okay. Now determine the cdb size based on the command code */ 7811 switch (*byte >> CMD_GROUP_CODE_SHIFT) { 7812 case 0: 7813 atio->cdb_len = 6; 7814 break; 7815 case 1: 7816 case 2: 7817 atio->cdb_len = 10; 7818 break; 7819 case 4: 7820 atio->cdb_len = 16; 7821 break; 7822 case 5: 7823 atio->cdb_len = 12; 7824 break; 7825 case 3: 7826 default: 7827 /* Only copy the opcode. */ 7828 atio->cdb_len = 1; 7829 printf("Reserved or VU command code type encountered\n"); 7830 break; 7831 } 7832 7833 memcpy(atio->cdb_io.cdb_bytes, byte, atio->cdb_len); 7834 7835 atio->ccb_h.status |= CAM_CDB_RECVD; 7836 7837 if ((cmd->identify & MSG_IDENTIFY_DISCFLAG) == 0) { 7838 /* 7839 * We weren't allowed to disconnect. 7840 * We're hanging on the bus until a 7841 * continue target I/O comes in response 7842 * to this accept tio. 7843 */ 7844 #ifdef AHC_DEBUG 7845 if (ahc_debug & AHC_SHOW_TQIN) { 7846 printf("Received Immediate Command %d:%d:%d - %p\n", 7847 initiator, target, lun, ahc->pending_device); 7848 } 7849 #endif 7850 ahc->pending_device = lstate; 7851 aic_freeze_ccb((union ccb *)atio); 7852 atio->ccb_h.flags |= CAM_DIS_DISCONNECT; 7853 } 7854 xpt_done((union ccb*)atio); 7855 return (0); 7856 } 7857 7858 #endif 7859