2 * Core routines and tables shareable across OS platforms.
4 * Copyright (c) 1994-2002 Justin T. Gibbs.
5 * Copyright (c) 2000-2002 Adaptec Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions, and the following disclaimer,
13 * without modification.
14 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
15 * substantially similar to the "NO WARRANTY" disclaimer below
16 * ("Disclaimer") and any redistribution must be conditioned upon
17 * including a substantially similar Disclaimer requirement for further
18 * binary redistribution.
19 * 3. Neither the names of the above-listed copyright holders nor the names
20 * of any contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
23 * Alternatively, this software may be distributed under the terms of the
24 * GNU General Public License ("GPL") version 2 as published by the Free
25 * Software Foundation.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
37 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGES.
40 * $Id: //depot/aic7xxx/aic7xxx/aic7xxx.c#155 $
44 #include "aic7xxx_osm.h"
45 #include "aic7xxx_inline.h"
46 #include "aicasm/aicasm_insformat.h"
48 #include <dev/aic7xxx/aic7xxx_osm.h>
49 #include <dev/aic7xxx/aic7xxx_inline.h>
50 #include <dev/aic7xxx/aicasm/aicasm_insformat.h>
53 /***************************** Lookup Tables **********************************/
54 char *ahc_chip_names[] =
71 static const u_int num_chip_names = ARRAY_SIZE(ahc_chip_names);
74 * Hardware error codes.
76 struct ahc_hard_error_entry {
81 static struct ahc_hard_error_entry ahc_hard_errors[] = {
82 { ILLHADDR, "Illegal Host Access" },
83 { ILLSADDR, "Illegal Sequencer Address referrenced" },
84 { ILLOPCODE, "Illegal Opcode in sequencer program" },
85 { SQPARERR, "Sequencer Parity Error" },
86 { DPARERR, "Data-path Parity Error" },
87 { MPARERR, "Scratch or SCB Memory Parity Error" },
88 { PCIERRSTAT, "PCI Error detected" },
89 { CIOPARERR, "CIOBUS Parity Error" },
91 static const u_int num_errors = ARRAY_SIZE(ahc_hard_errors);
93 static struct ahc_phase_table_entry ahc_phase_table[] =
95 { P_DATAOUT, MSG_NOOP, "in Data-out phase" },
96 { P_DATAIN, MSG_INITIATOR_DET_ERR, "in Data-in phase" },
97 { P_DATAOUT_DT, MSG_NOOP, "in DT Data-out phase" },
98 { P_DATAIN_DT, MSG_INITIATOR_DET_ERR, "in DT Data-in phase" },
99 { P_COMMAND, MSG_NOOP, "in Command phase" },
100 { P_MESGOUT, MSG_NOOP, "in Message-out phase" },
101 { P_STATUS, MSG_INITIATOR_DET_ERR, "in Status phase" },
102 { P_MESGIN, MSG_PARITY_ERROR, "in Message-in phase" },
103 { P_BUSFREE, MSG_NOOP, "while idle" },
104 { 0, MSG_NOOP, "in unknown phase" }
108 * In most cases we only wish to itterate over real phases, so
109 * exclude the last element from the count.
111 static const u_int num_phases = ARRAY_SIZE(ahc_phase_table) - 1;
114 * Valid SCSIRATE values. (p. 3-17)
115 * Provides a mapping of tranfer periods in ns to the proper value to
116 * stick in the scsixfer reg.
118 static struct ahc_syncrate ahc_syncrates[] =
120 /* ultra2 fast/ultra period rate */
121 { 0x42, 0x000, 9, "80.0" },
122 { 0x03, 0x000, 10, "40.0" },
123 { 0x04, 0x000, 11, "33.0" },
124 { 0x05, 0x100, 12, "20.0" },
125 { 0x06, 0x110, 15, "16.0" },
126 { 0x07, 0x120, 18, "13.4" },
127 { 0x08, 0x000, 25, "10.0" },
128 { 0x19, 0x010, 31, "8.0" },
129 { 0x1a, 0x020, 37, "6.67" },
130 { 0x1b, 0x030, 43, "5.7" },
131 { 0x1c, 0x040, 50, "5.0" },
132 { 0x00, 0x050, 56, "4.4" },
133 { 0x00, 0x060, 62, "4.0" },
134 { 0x00, 0x070, 68, "3.6" },
135 { 0x00, 0x000, 0, NULL }
138 /* Our Sequencer Program */
139 #include "aic7xxx_seq.h"
141 /**************************** Function Declarations ***************************/
142 static void ahc_force_renegotiation(struct ahc_softc *ahc,
143 struct ahc_devinfo *devinfo);
144 static struct ahc_tmode_tstate*
145 ahc_alloc_tstate(struct ahc_softc *ahc,
146 u_int scsi_id, char channel);
147 #ifdef AHC_TARGET_MODE
148 static void ahc_free_tstate(struct ahc_softc *ahc,
149 u_int scsi_id, char channel, int force);
151 static struct ahc_syncrate*
152 ahc_devlimited_syncrate(struct ahc_softc *ahc,
153 struct ahc_initiator_tinfo *,
157 static void ahc_update_pending_scbs(struct ahc_softc *ahc);
158 static void ahc_fetch_devinfo(struct ahc_softc *ahc,
159 struct ahc_devinfo *devinfo);
160 static void ahc_scb_devinfo(struct ahc_softc *ahc,
161 struct ahc_devinfo *devinfo,
163 static void ahc_assert_atn(struct ahc_softc *ahc);
164 static void ahc_setup_initiator_msgout(struct ahc_softc *ahc,
165 struct ahc_devinfo *devinfo,
167 static void ahc_build_transfer_msg(struct ahc_softc *ahc,
168 struct ahc_devinfo *devinfo);
169 static void ahc_construct_sdtr(struct ahc_softc *ahc,
170 struct ahc_devinfo *devinfo,
171 u_int period, u_int offset);
172 static void ahc_construct_wdtr(struct ahc_softc *ahc,
173 struct ahc_devinfo *devinfo,
175 static void ahc_construct_ppr(struct ahc_softc *ahc,
176 struct ahc_devinfo *devinfo,
177 u_int period, u_int offset,
178 u_int bus_width, u_int ppr_options);
179 static void ahc_clear_msg_state(struct ahc_softc *ahc);
180 static void ahc_handle_proto_violation(struct ahc_softc *ahc);
181 static void ahc_handle_message_phase(struct ahc_softc *ahc);
187 static int ahc_sent_msg(struct ahc_softc *ahc, ahc_msgtype type,
188 u_int msgval, int full);
189 static int ahc_parse_msg(struct ahc_softc *ahc,
190 struct ahc_devinfo *devinfo);
191 static int ahc_handle_msg_reject(struct ahc_softc *ahc,
192 struct ahc_devinfo *devinfo);
193 static void ahc_handle_ign_wide_residue(struct ahc_softc *ahc,
194 struct ahc_devinfo *devinfo);
195 static void ahc_reinitialize_dataptrs(struct ahc_softc *ahc);
196 static void ahc_handle_devreset(struct ahc_softc *ahc,
197 struct ahc_devinfo *devinfo,
198 cam_status status, char *message,
200 #ifdef AHC_TARGET_MODE
201 static void ahc_setup_target_msgin(struct ahc_softc *ahc,
202 struct ahc_devinfo *devinfo,
206 static bus_dmamap_callback_t ahc_dmamap_cb;
207 static void ahc_build_free_scb_list(struct ahc_softc *ahc);
208 static int ahc_init_scbdata(struct ahc_softc *ahc);
209 static void ahc_fini_scbdata(struct ahc_softc *ahc);
210 static void ahc_qinfifo_requeue(struct ahc_softc *ahc,
211 struct scb *prev_scb,
213 static int ahc_qinfifo_count(struct ahc_softc *ahc);
214 static u_int ahc_rem_scb_from_disc_list(struct ahc_softc *ahc,
215 u_int prev, u_int scbptr);
216 static void ahc_add_curscb_to_free_list(struct ahc_softc *ahc);
217 static u_int ahc_rem_wscb(struct ahc_softc *ahc,
218 u_int scbpos, u_int prev);
219 static void ahc_reset_current_bus(struct ahc_softc *ahc);
221 static void ahc_dumpseq(struct ahc_softc *ahc);
223 static int ahc_loadseq(struct ahc_softc *ahc);
224 static int ahc_check_patch(struct ahc_softc *ahc,
225 struct patch **start_patch,
226 u_int start_instr, u_int *skip_addr);
227 static void ahc_download_instr(struct ahc_softc *ahc,
228 u_int instrptr, uint8_t *dconsts);
229 #ifdef AHC_TARGET_MODE
230 static void ahc_queue_lstate_event(struct ahc_softc *ahc,
231 struct ahc_tmode_lstate *lstate,
235 static void ahc_update_scsiid(struct ahc_softc *ahc,
237 static int ahc_handle_target_cmd(struct ahc_softc *ahc,
238 struct target_cmd *cmd);
240 /************************* Sequencer Execution Control ************************/
242 * Restart the sequencer program from address zero
245 ahc_restart(struct ahc_softc *ahc)
250 /* No more pending messages. */
251 ahc_clear_msg_state(ahc);
253 ahc_outb(ahc, SCSISIGO, 0); /* De-assert BSY */
254 ahc_outb(ahc, MSG_OUT, MSG_NOOP); /* No message to send */
255 ahc_outb(ahc, SXFRCTL1, ahc_inb(ahc, SXFRCTL1) & ~BITBUCKET);
256 ahc_outb(ahc, LASTPHASE, P_BUSFREE);
257 ahc_outb(ahc, SAVED_SCSIID, 0xFF);
258 ahc_outb(ahc, SAVED_LUN, 0xFF);
261 * Ensure that the sequencer's idea of TQINPOS
262 * matches our own. The sequencer increments TQINPOS
263 * only after it sees a DMA complete and a reset could
264 * occur before the increment leaving the kernel to believe
265 * the command arrived but the sequencer to not.
267 ahc_outb(ahc, TQINPOS, ahc->tqinfifonext);
269 /* Always allow reselection */
270 ahc_outb(ahc, SCSISEQ,
271 ahc_inb(ahc, SCSISEQ_TEMPLATE) & (ENSELI|ENRSELI|ENAUTOATNP));
272 if ((ahc->features & AHC_CMD_CHAN) != 0) {
273 /* Ensure that no DMA operations are in progress */
274 ahc_outb(ahc, CCSCBCNT, 0);
275 ahc_outb(ahc, CCSGCTL, 0);
276 ahc_outb(ahc, CCSCBCTL, 0);
279 * If we were in the process of DMA'ing SCB data into
280 * an SCB, replace that SCB on the free list. This prevents
283 if ((ahc_inb(ahc, SEQ_FLAGS2) & SCB_DMA) != 0) {
284 ahc_add_curscb_to_free_list(ahc);
285 ahc_outb(ahc, SEQ_FLAGS2,
286 ahc_inb(ahc, SEQ_FLAGS2) & ~SCB_DMA);
290 * Clear any pending sequencer interrupt. It is no
291 * longer relevant since we're resetting the Program
294 ahc_outb(ahc, CLRINT, CLRSEQINT);
296 ahc_outb(ahc, MWI_RESIDUAL, 0);
297 ahc_outb(ahc, SEQCTL, ahc->seqctl);
298 ahc_outb(ahc, SEQADDR0, 0);
299 ahc_outb(ahc, SEQADDR1, 0);
304 /************************* Input/Output Queues ********************************/
306 ahc_run_qoutfifo(struct ahc_softc *ahc)
311 ahc_sync_qoutfifo(ahc, BUS_DMASYNC_POSTREAD);
312 while (ahc->qoutfifo[ahc->qoutfifonext] != SCB_LIST_NULL) {
314 scb_index = ahc->qoutfifo[ahc->qoutfifonext];
315 if ((ahc->qoutfifonext & 0x03) == 0x03) {
319 * Clear 32bits of QOUTFIFO at a time
320 * so that we don't clobber an incoming
321 * byte DMA to the array on architectures
322 * that only support 32bit load and store
325 modnext = ahc->qoutfifonext & ~0x3;
326 *((uint32_t *)(&ahc->qoutfifo[modnext])) = 0xFFFFFFFFUL;
327 ahc_dmamap_sync(ahc, ahc->shared_data_dmat,
328 ahc->shared_data_dmamap,
329 /*offset*/modnext, /*len*/4,
330 BUS_DMASYNC_PREREAD);
334 scb = ahc_lookup_scb(ahc, scb_index);
336 printf("%s: WARNING no command for scb %d "
337 "(cmdcmplt)\nQOUTPOS = %d\n",
338 ahc_name(ahc), scb_index,
339 (ahc->qoutfifonext - 1) & 0xFF);
344 * Save off the residual
347 ahc_update_residual(ahc, scb);
353 ahc_run_untagged_queues(struct ahc_softc *ahc)
357 for (i = 0; i < 16; i++)
358 ahc_run_untagged_queue(ahc, &ahc->untagged_queues[i]);
362 ahc_run_untagged_queue(struct ahc_softc *ahc, struct scb_tailq *queue)
366 if (ahc->untagged_queue_lock != 0)
369 if ((scb = TAILQ_FIRST(queue)) != NULL
370 && (scb->flags & SCB_ACTIVE) == 0) {
371 scb->flags |= SCB_ACTIVE;
372 ahc_queue_scb(ahc, scb);
376 /************************* Interrupt Handling *********************************/
378 ahc_handle_brkadrint(struct ahc_softc *ahc)
381 * We upset the sequencer :-(
382 * Lookup the error message
387 error = ahc_inb(ahc, ERROR);
388 for (i = 0; error != 1 && i < num_errors; i++)
390 printf("%s: brkadrint, %s at seqaddr = 0x%x\n",
391 ahc_name(ahc), ahc_hard_errors[i].errmesg,
392 ahc_inb(ahc, SEQADDR0) |
393 (ahc_inb(ahc, SEQADDR1) << 8));
395 ahc_dump_card_state(ahc);
397 /* Tell everyone that this HBA is no longer available */
398 ahc_abort_scbs(ahc, CAM_TARGET_WILDCARD, ALL_CHANNELS,
399 CAM_LUN_WILDCARD, SCB_LIST_NULL, ROLE_UNKNOWN,
402 /* Disable all interrupt sources by resetting the controller */
407 ahc_handle_seqint(struct ahc_softc *ahc, u_int intstat)
410 struct ahc_devinfo devinfo;
412 ahc_fetch_devinfo(ahc, &devinfo);
415 * Clear the upper byte that holds SEQINT status
416 * codes and clear the SEQINT bit. We will unpause
417 * the sequencer, if appropriate, after servicing
420 ahc_outb(ahc, CLRINT, CLRSEQINT);
421 switch (intstat & SEQINT_MASK) {
425 struct hardware_scb *hscb;
428 * Set the default return value to 0 (don't
429 * send sense). The sense code will change
432 ahc_outb(ahc, RETURN_1, 0);
435 * The sequencer will notify us when a command
436 * has an error that would be of interest to
437 * the kernel. This allows us to leave the sequencer
438 * running in the common case of command completes
439 * without error. The sequencer will already have
440 * dma'd the SCB back up to us, so we can reference
441 * the in kernel copy directly.
443 scb_index = ahc_inb(ahc, SCB_TAG);
444 scb = ahc_lookup_scb(ahc, scb_index);
446 ahc_print_devinfo(ahc, &devinfo);
447 printf("ahc_intr - referenced scb "
448 "not valid during seqint 0x%x scb(%d)\n",
450 ahc_dump_card_state(ahc);
457 /* Don't want to clobber the original sense code */
458 if ((scb->flags & SCB_SENSE) != 0) {
460 * Clear the SCB_SENSE Flag and have
461 * the sequencer do a normal command
464 scb->flags &= ~SCB_SENSE;
465 ahc_set_transaction_status(scb, CAM_AUTOSENSE_FAIL);
468 ahc_set_transaction_status(scb, CAM_SCSI_STATUS_ERROR);
469 /* Freeze the queue until the client sees the error. */
470 ahc_freeze_devq(ahc, scb);
472 ahc_set_scsi_status(scb, hscb->shared_data.status.scsi_status);
473 switch (hscb->shared_data.status.scsi_status) {
475 printf("%s: Interrupted for staus of 0???\n",
478 case SCSI_STATUS_CMD_TERMINATED:
479 case SCSI_STATUS_CHECK_COND:
481 struct ahc_dma_seg *sg;
482 struct scsi_sense *sc;
483 struct ahc_initiator_tinfo *targ_info;
484 struct ahc_tmode_tstate *tstate;
485 struct ahc_transinfo *tinfo;
487 if (ahc_debug & AHC_SHOW_SENSE) {
488 ahc_print_path(ahc, scb);
489 printf("SCB %d: requests Check Status\n",
494 if (ahc_perform_autosense(scb) == 0)
497 targ_info = ahc_fetch_transinfo(ahc,
502 tinfo = &targ_info->curr;
504 sc = (struct scsi_sense *)(&hscb->shared_data.cdb);
506 * Save off the residual if there is one.
508 ahc_update_residual(ahc, scb);
510 if (ahc_debug & AHC_SHOW_SENSE) {
511 ahc_print_path(ahc, scb);
512 printf("Sending Sense\n");
515 sg->addr = ahc_get_sense_bufaddr(ahc, scb);
516 sg->len = ahc_get_sense_bufsize(ahc, scb);
517 sg->len |= AHC_DMA_LAST_SEG;
519 /* Fixup byte order */
520 sg->addr = ahc_htole32(sg->addr);
521 sg->len = ahc_htole32(sg->len);
523 sc->opcode = REQUEST_SENSE;
525 if (tinfo->protocol_version <= SCSI_REV_2
526 && SCB_GET_LUN(scb) < 8)
527 sc->byte2 = SCB_GET_LUN(scb) << 5;
530 sc->length = sg->len;
534 * We can't allow the target to disconnect.
535 * This will be an untagged transaction and
536 * having the target disconnect will make this
537 * transaction indestinguishable from outstanding
538 * tagged transactions.
543 * This request sense could be because the
544 * the device lost power or in some other
545 * way has lost our transfer negotiations.
546 * Renegotiate if appropriate. Unit attention
547 * errors will be reported before any data
550 if (ahc_get_residual(scb)
551 == ahc_get_transfer_length(scb)) {
552 ahc_update_neg_request(ahc, &devinfo,
554 AHC_NEG_IF_NON_ASYNC);
556 if (tstate->auto_negotiate & devinfo.target_mask) {
557 hscb->control |= MK_MESSAGE;
558 scb->flags &= ~SCB_NEGOTIATE;
559 scb->flags |= SCB_AUTO_NEGOTIATE;
561 hscb->cdb_len = sizeof(*sc);
562 hscb->dataptr = sg->addr;
563 hscb->datacnt = sg->len;
564 hscb->sgptr = scb->sg_list_phys | SG_FULL_RESID;
565 hscb->sgptr = ahc_htole32(hscb->sgptr);
567 scb->flags |= SCB_SENSE;
568 ahc_qinfifo_requeue_tail(ahc, scb);
569 ahc_outb(ahc, RETURN_1, SEND_SENSE);
571 * Ensure we have enough time to actually
572 * retrieve the sense.
574 ahc_scb_timer_reset(scb, 5 * 1000000);
584 /* Ensure we don't leave the selection hardware on */
585 ahc_outb(ahc, SCSISEQ,
586 ahc_inb(ahc, SCSISEQ) & (ENSELI|ENRSELI|ENAUTOATNP));
588 printf("%s:%c:%d: no active SCB for reconnecting "
589 "target - issuing BUS DEVICE RESET\n",
590 ahc_name(ahc), devinfo.channel, devinfo.target);
591 printf("SAVED_SCSIID == 0x%x, SAVED_LUN == 0x%x, "
592 "ARG_1 == 0x%x ACCUM = 0x%x\n",
593 ahc_inb(ahc, SAVED_SCSIID), ahc_inb(ahc, SAVED_LUN),
594 ahc_inb(ahc, ARG_1), ahc_inb(ahc, ACCUM));
595 printf("SEQ_FLAGS == 0x%x, SCBPTR == 0x%x, BTT == 0x%x, "
597 ahc_inb(ahc, SEQ_FLAGS), ahc_inb(ahc, SCBPTR),
598 ahc_index_busy_tcl(ahc,
599 BUILD_TCL(ahc_inb(ahc, SAVED_SCSIID),
600 ahc_inb(ahc, SAVED_LUN))),
601 ahc_inb(ahc, SINDEX));
602 printf("SCSIID == 0x%x, SCB_SCSIID == 0x%x, SCB_LUN == 0x%x, "
603 "SCB_TAG == 0x%x, SCB_CONTROL == 0x%x\n",
604 ahc_inb(ahc, SCSIID), ahc_inb(ahc, SCB_SCSIID),
605 ahc_inb(ahc, SCB_LUN), ahc_inb(ahc, SCB_TAG),
606 ahc_inb(ahc, SCB_CONTROL));
607 printf("SCSIBUSL == 0x%x, SCSISIGI == 0x%x\n",
608 ahc_inb(ahc, SCSIBUSL), ahc_inb(ahc, SCSISIGI));
609 printf("SXFRCTL0 == 0x%x\n", ahc_inb(ahc, SXFRCTL0));
610 printf("SEQCTL == 0x%x\n", ahc_inb(ahc, SEQCTL));
611 ahc_dump_card_state(ahc);
612 ahc->msgout_buf[0] = MSG_BUS_DEV_RESET;
614 ahc->msgout_index = 0;
615 ahc->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
616 ahc_outb(ahc, MSG_OUT, HOST_MSG);
622 u_int rejbyte = ahc_inb(ahc, ACCUM);
623 printf("%s:%c:%d: Warning - unknown message received from "
624 "target (0x%x). Rejecting\n",
625 ahc_name(ahc), devinfo.channel, devinfo.target, rejbyte);
628 case PROTO_VIOLATION:
630 ahc_handle_proto_violation(ahc);
634 ahc_handle_ign_wide_residue(ahc, &devinfo);
637 ahc_reinitialize_dataptrs(ahc);
643 lastphase = ahc_inb(ahc, LASTPHASE);
644 printf("%s:%c:%d: unknown scsi bus phase %x, "
645 "lastphase = 0x%x. Attempting to continue\n",
646 ahc_name(ahc), devinfo.channel, devinfo.target,
647 lastphase, ahc_inb(ahc, SCSISIGI));
654 lastphase = ahc_inb(ahc, LASTPHASE);
655 printf("%s:%c:%d: Missed busfree. "
656 "Lastphase = 0x%x, Curphase = 0x%x\n",
657 ahc_name(ahc), devinfo.channel, devinfo.target,
658 lastphase, ahc_inb(ahc, SCSISIGI));
665 * The sequencer has encountered a message phase
666 * that requires host assistance for completion.
667 * While handling the message phase(s), we will be
668 * notified by the sequencer after each byte is
669 * transfered so we can track bus phase changes.
671 * If this is the first time we've seen a HOST_MSG_LOOP
672 * interrupt, initialize the state of the host message
675 if (ahc->msg_type == MSG_TYPE_NONE) {
680 bus_phase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK;
681 if (bus_phase != P_MESGIN
682 && bus_phase != P_MESGOUT) {
683 printf("ahc_intr: HOST_MSG_LOOP bad "
687 * Probably transitioned to bus free before
688 * we got here. Just punt the message.
690 ahc_clear_intstat(ahc);
695 scb_index = ahc_inb(ahc, SCB_TAG);
696 scb = ahc_lookup_scb(ahc, scb_index);
697 if (devinfo.role == ROLE_INITIATOR) {
698 if (bus_phase == P_MESGOUT) {
700 panic("HOST_MSG_LOOP with "
704 ahc_setup_initiator_msgout(ahc,
709 MSG_TYPE_INITIATOR_MSGIN;
710 ahc->msgin_index = 0;
713 #ifdef AHC_TARGET_MODE
715 if (bus_phase == P_MESGOUT) {
717 MSG_TYPE_TARGET_MSGOUT;
718 ahc->msgin_index = 0;
721 ahc_setup_target_msgin(ahc,
728 ahc_handle_message_phase(ahc);
734 * If we've cleared the parity error interrupt
735 * but the sequencer still believes that SCSIPERR
736 * is true, it must be that the parity error is
737 * for the currently presented byte on the bus,
738 * and we are not in a phase (data-in) where we will
739 * eventually ack this byte. Ack the byte and
740 * throw it away in the hope that the target will
741 * take us to message out to deliver the appropriate
744 if ((intstat & SCSIINT) == 0
745 && (ahc_inb(ahc, SSTAT1) & SCSIPERR) != 0) {
747 if ((ahc->features & AHC_DT) == 0) {
751 * The hardware will only let you ack bytes
752 * if the expected phase in SCSISIGO matches
753 * the current phase. Make sure this is
754 * currently the case.
756 curphase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK;
757 ahc_outb(ahc, LASTPHASE, curphase);
758 ahc_outb(ahc, SCSISIGO, curphase);
760 if ((ahc_inb(ahc, SCSISIGI) & (CDI|MSGI)) == 0) {
764 * In a data phase. Faster to bitbucket
765 * the data than to individually ack each
766 * byte. This is also the only strategy
767 * that will work with AUTOACK enabled.
769 ahc_outb(ahc, SXFRCTL1,
770 ahc_inb(ahc, SXFRCTL1) | BITBUCKET);
772 while (--wait != 0) {
773 if ((ahc_inb(ahc, SCSISIGI)
778 ahc_outb(ahc, SXFRCTL1,
779 ahc_inb(ahc, SXFRCTL1) & ~BITBUCKET);
784 ahc_print_devinfo(ahc, &devinfo);
785 printf("Unable to clear parity error. "
787 scb_index = ahc_inb(ahc, SCB_TAG);
788 scb = ahc_lookup_scb(ahc, scb_index);
790 ahc_set_transaction_status(scb,
792 ahc_reset_channel(ahc, devinfo.channel,
796 ahc_inb(ahc, SCSIDATL);
804 * When the sequencer detects an overrun, it
805 * places the controller in "BITBUCKET" mode
806 * and allows the target to complete its transfer.
807 * Unfortunately, none of the counters get updated
808 * when the controller is in this mode, so we have
809 * no way of knowing how large the overrun was.
811 u_int scbindex = ahc_inb(ahc, SCB_TAG);
812 u_int lastphase = ahc_inb(ahc, LASTPHASE);
815 scb = ahc_lookup_scb(ahc, scbindex);
816 for (i = 0; i < num_phases; i++) {
817 if (lastphase == ahc_phase_table[i].phase)
820 ahc_print_path(ahc, scb);
821 printf("data overrun detected %s."
823 ahc_phase_table[i].phasemsg,
825 ahc_print_path(ahc, scb);
826 printf("%s seen Data Phase. Length = %ld. NumSGs = %d.\n",
827 ahc_inb(ahc, SEQ_FLAGS) & DPHASE ? "Have" : "Haven't",
828 ahc_get_transfer_length(scb), scb->sg_count);
829 if (scb->sg_count > 0) {
830 for (i = 0; i < scb->sg_count; i++) {
832 printf("sg[%d] - Addr 0x%x%x : Length %d\n",
834 (ahc_le32toh(scb->sg_list[i].len) >> 24
835 & SG_HIGH_ADDR_BITS),
836 ahc_le32toh(scb->sg_list[i].addr),
837 ahc_le32toh(scb->sg_list[i].len)
842 * Set this and it will take effect when the
843 * target does a command complete.
845 ahc_freeze_devq(ahc, scb);
846 if ((scb->flags & SCB_SENSE) == 0) {
847 ahc_set_transaction_status(scb, CAM_DATA_RUN_ERR);
849 scb->flags &= ~SCB_SENSE;
850 ahc_set_transaction_status(scb, CAM_AUTOSENSE_FAIL);
854 if ((ahc->features & AHC_ULTRA2) != 0) {
856 * Clear the channel in case we return
857 * to data phase later.
859 ahc_outb(ahc, SXFRCTL0,
860 ahc_inb(ahc, SXFRCTL0) | CLRSTCNT|CLRCHN);
861 ahc_outb(ahc, SXFRCTL0,
862 ahc_inb(ahc, SXFRCTL0) | CLRSTCNT|CLRCHN);
864 if ((ahc->flags & AHC_39BIT_ADDRESSING) != 0) {
867 /* Ensure HHADDR is 0 for future DMA operations. */
868 dscommand1 = ahc_inb(ahc, DSCOMMAND1);
869 ahc_outb(ahc, DSCOMMAND1, dscommand1 | HADDLDSEL0);
870 ahc_outb(ahc, HADDR, 0);
871 ahc_outb(ahc, DSCOMMAND1, dscommand1);
879 printf("%s:%c:%d:%d: Attempt to issue message failed\n",
880 ahc_name(ahc), devinfo.channel, devinfo.target,
882 scbindex = ahc_inb(ahc, SCB_TAG);
883 scb = ahc_lookup_scb(ahc, scbindex);
885 && (scb->flags & SCB_RECOVERY_SCB) != 0)
887 * Ensure that we didn't put a second instance of this
888 * SCB into the QINFIFO.
890 ahc_search_qinfifo(ahc, SCB_GET_TARGET(ahc, scb),
891 SCB_GET_CHANNEL(ahc, scb),
892 SCB_GET_LUN(scb), scb->hscb->tag,
893 ROLE_INITIATOR, /*status*/0,
899 printf("%s: No free or disconnected SCBs\n", ahc_name(ahc));
900 ahc_dump_card_state(ahc);
908 scbptr = ahc_inb(ahc, SCBPTR);
909 printf("Bogus TAG after DMA. SCBPTR %d, tag %d, our tag %d\n",
910 scbptr, ahc_inb(ahc, ARG_1),
911 ahc->scb_data->hscbs[scbptr].tag);
912 ahc_dump_card_state(ahc);
918 printf("%s: BTT calculation out of range\n", ahc_name(ahc));
919 printf("SAVED_SCSIID == 0x%x, SAVED_LUN == 0x%x, "
920 "ARG_1 == 0x%x ACCUM = 0x%x\n",
921 ahc_inb(ahc, SAVED_SCSIID), ahc_inb(ahc, SAVED_LUN),
922 ahc_inb(ahc, ARG_1), ahc_inb(ahc, ACCUM));
923 printf("SEQ_FLAGS == 0x%x, SCBPTR == 0x%x, BTT == 0x%x, "
924 "SINDEX == 0x%x\n, A == 0x%x\n",
925 ahc_inb(ahc, SEQ_FLAGS), ahc_inb(ahc, SCBPTR),
926 ahc_index_busy_tcl(ahc,
927 BUILD_TCL(ahc_inb(ahc, SAVED_SCSIID),
928 ahc_inb(ahc, SAVED_LUN))),
929 ahc_inb(ahc, SINDEX),
930 ahc_inb(ahc, ACCUM));
931 printf("SCSIID == 0x%x, SCB_SCSIID == 0x%x, SCB_LUN == 0x%x, "
932 "SCB_TAG == 0x%x, SCB_CONTROL == 0x%x\n",
933 ahc_inb(ahc, SCSIID), ahc_inb(ahc, SCB_SCSIID),
934 ahc_inb(ahc, SCB_LUN), ahc_inb(ahc, SCB_TAG),
935 ahc_inb(ahc, SCB_CONTROL));
936 printf("SCSIBUSL == 0x%x, SCSISIGI == 0x%x\n",
937 ahc_inb(ahc, SCSIBUSL), ahc_inb(ahc, SCSISIGI));
938 ahc_dump_card_state(ahc);
943 printf("ahc_intr: seqint, "
944 "intstat == 0x%x, scsisigi = 0x%x\n",
945 intstat, ahc_inb(ahc, SCSISIGI));
950 * The sequencer is paused immediately on
951 * a SEQINT, so we should restart it when
958 ahc_handle_scsiint(struct ahc_softc *ahc, u_int intstat)
967 if ((ahc->features & AHC_TWIN) != 0
968 && ((ahc_inb(ahc, SBLKCTL) & SELBUSB) != 0))
972 intr_channel = cur_channel;
974 if ((ahc->features & AHC_ULTRA2) != 0)
975 status0 = ahc_inb(ahc, SSTAT0) & IOERR;
978 status = ahc_inb(ahc, SSTAT1) & (SELTO|SCSIRSTI|BUSFREE|SCSIPERR);
979 if (status == 0 && status0 == 0) {
980 if ((ahc->features & AHC_TWIN) != 0) {
981 /* Try the other channel */
982 ahc_outb(ahc, SBLKCTL, ahc_inb(ahc, SBLKCTL) ^ SELBUSB);
983 status = ahc_inb(ahc, SSTAT1)
984 & (SELTO|SCSIRSTI|BUSFREE|SCSIPERR);
985 intr_channel = (cur_channel == 'A') ? 'B' : 'A';
988 printf("%s: Spurious SCSI interrupt\n", ahc_name(ahc));
989 ahc_outb(ahc, CLRINT, CLRSCSIINT);
995 /* Make sure the sequencer is in a safe location. */
996 ahc_clear_critical_section(ahc);
998 scb_index = ahc_inb(ahc, SCB_TAG);
999 scb = ahc_lookup_scb(ahc, scb_index);
1001 && (ahc_inb(ahc, SEQ_FLAGS) & NOT_IDENTIFIED) != 0)
1004 if ((ahc->features & AHC_ULTRA2) != 0
1005 && (status0 & IOERR) != 0) {
1008 now_lvd = ahc_inb(ahc, SBLKCTL) & ENAB40;
1009 printf("%s: Transceiver State Has Changed to %s mode\n",
1010 ahc_name(ahc), now_lvd ? "LVD" : "SE");
1011 ahc_outb(ahc, CLRSINT0, CLRIOERR);
1013 * When transitioning to SE mode, the reset line
1014 * glitches, triggering an arbitration bug in some
1015 * Ultra2 controllers. This bug is cleared when we
1016 * assert the reset line. Since a reset glitch has
1017 * already occurred with this transition and a
1018 * transceiver state change is handled just like
1019 * a bus reset anyway, asserting the reset line
1020 * ourselves is safe.
1022 ahc_reset_channel(ahc, intr_channel,
1023 /*Initiate Reset*/now_lvd == 0);
1024 } else if ((status & SCSIRSTI) != 0) {
1025 printf("%s: Someone reset channel %c\n",
1026 ahc_name(ahc), intr_channel);
1027 if (intr_channel != cur_channel)
1028 ahc_outb(ahc, SBLKCTL, ahc_inb(ahc, SBLKCTL) ^ SELBUSB);
1029 ahc_reset_channel(ahc, intr_channel, /*Initiate Reset*/FALSE);
1030 } else if ((status & SCSIPERR) != 0) {
1032 * Determine the bus phase and queue an appropriate message.
1033 * SCSIPERR is latched true as soon as a parity error
1034 * occurs. If the sequencer acked the transfer that
1035 * caused the parity error and the currently presented
1036 * transfer on the bus has correct parity, SCSIPERR will
1037 * be cleared by CLRSCSIPERR. Use this to determine if
1038 * we should look at the last phase the sequencer recorded,
1039 * or the current phase presented on the bus.
1041 struct ahc_devinfo devinfo;
1051 lastphase = ahc_inb(ahc, LASTPHASE);
1052 curphase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK;
1053 sstat2 = ahc_inb(ahc, SSTAT2);
1054 ahc_outb(ahc, CLRSINT1, CLRSCSIPERR);
1056 * For all phases save DATA, the sequencer won't
1057 * automatically ack a byte that has a parity error
1058 * in it. So the only way that the current phase
1059 * could be 'data-in' is if the parity error is for
1060 * an already acked byte in the data phase. During
1061 * synchronous data-in transfers, we may actually
1062 * ack bytes before latching the current phase in
1063 * LASTPHASE, leading to the discrepancy between
1064 * curphase and lastphase.
1066 if ((ahc_inb(ahc, SSTAT1) & SCSIPERR) != 0
1067 || curphase == P_DATAIN || curphase == P_DATAIN_DT)
1068 errorphase = curphase;
1070 errorphase = lastphase;
1072 for (i = 0; i < num_phases; i++) {
1073 if (errorphase == ahc_phase_table[i].phase)
1076 mesg_out = ahc_phase_table[i].mesg_out;
1079 if (SCB_IS_SILENT(scb))
1082 ahc_print_path(ahc, scb);
1083 scb->flags |= SCB_TRANSMISSION_ERROR;
1085 printf("%s:%c:%d: ", ahc_name(ahc), intr_channel,
1086 SCSIID_TARGET(ahc, ahc_inb(ahc, SAVED_SCSIID)));
1087 scsirate = ahc_inb(ahc, SCSIRATE);
1088 if (silent == FALSE) {
1089 printf("parity error detected %s. "
1090 "SEQADDR(0x%x) SCSIRATE(0x%x)\n",
1091 ahc_phase_table[i].phasemsg,
1092 ahc_inw(ahc, SEQADDR0),
1094 if ((ahc->features & AHC_DT) != 0) {
1095 if ((sstat2 & CRCVALERR) != 0)
1096 printf("\tCRC Value Mismatch\n");
1097 if ((sstat2 & CRCENDERR) != 0)
1098 printf("\tNo terminal CRC packet "
1100 if ((sstat2 & CRCREQERR) != 0)
1101 printf("\tIllegal CRC packet "
1103 if ((sstat2 & DUAL_EDGE_ERR) != 0)
1104 printf("\tUnexpected %sDT Data Phase\n",
1105 (scsirate & SINGLE_EDGE)
1110 if ((ahc->features & AHC_DT) != 0
1111 && (sstat2 & DUAL_EDGE_ERR) != 0) {
1113 * This error applies regardless of
1114 * data direction, so ignore the value
1115 * in the phase table.
1117 mesg_out = MSG_INITIATOR_DET_ERR;
1121 * We've set the hardware to assert ATN if we
1122 * get a parity error on "in" phases, so all we
1123 * need to do is stuff the message buffer with
1124 * the appropriate message. "In" phases have set
1125 * mesg_out to something other than MSG_NOP.
1127 if (mesg_out != MSG_NOOP) {
1128 if (ahc->msg_type != MSG_TYPE_NONE)
1129 ahc->send_msg_perror = TRUE;
1131 ahc_outb(ahc, MSG_OUT, mesg_out);
1134 * Force a renegotiation with this target just in
1135 * case we are out of sync for some external reason
1136 * unknown (or unreported) by the target.
1138 ahc_fetch_devinfo(ahc, &devinfo);
1139 ahc_force_renegotiation(ahc, &devinfo);
1141 ahc_outb(ahc, CLRINT, CLRSCSIINT);
1143 } else if ((status & SELTO) != 0) {
1146 /* Stop the selection */
1147 ahc_outb(ahc, SCSISEQ, 0);
1149 /* No more pending messages */
1150 ahc_clear_msg_state(ahc);
1152 /* Clear interrupt state */
1153 ahc_outb(ahc, SIMODE1, ahc_inb(ahc, SIMODE1) & ~ENBUSFREE);
1154 ahc_outb(ahc, CLRSINT1, CLRSELTIMEO|CLRBUSFREE|CLRSCSIPERR);
1157 * Although the driver does not care about the
1158 * 'Selection in Progress' status bit, the busy
1159 * LED does. SELINGO is only cleared by a sucessfull
1160 * selection, so we must manually clear it to insure
1161 * the LED turns off just incase no future successful
1162 * selections occur (e.g. no devices on the bus).
1164 ahc_outb(ahc, CLRSINT0, CLRSELINGO);
1166 scbptr = ahc_inb(ahc, WAITING_SCBH);
1167 ahc_outb(ahc, SCBPTR, scbptr);
1168 scb_index = ahc_inb(ahc, SCB_TAG);
1170 scb = ahc_lookup_scb(ahc, scb_index);
1172 printf("%s: ahc_intr - referenced scb not "
1173 "valid during SELTO scb(%d, %d)\n",
1174 ahc_name(ahc), scbptr, scb_index);
1175 ahc_dump_card_state(ahc);
1177 struct ahc_devinfo devinfo;
1179 if ((ahc_debug & AHC_SHOW_SELTO) != 0) {
1180 ahc_print_path(ahc, scb);
1181 printf("Saw Selection Timeout for SCB 0x%x\n",
1185 ahc_scb_devinfo(ahc, &devinfo, scb);
1186 ahc_set_transaction_status(scb, CAM_SEL_TIMEOUT);
1187 ahc_freeze_devq(ahc, scb);
1190 * Cancel any pending transactions on the device
1191 * now that it seems to be missing. This will
1192 * also revert us to async/narrow transfers until
1193 * we can renegotiate with the device.
1195 ahc_handle_devreset(ahc, &devinfo,
1197 "Selection Timeout",
1198 /*verbose_level*/1);
1200 ahc_outb(ahc, CLRINT, CLRSCSIINT);
1202 } else if ((status & BUSFREE) != 0
1203 && (ahc_inb(ahc, SIMODE1) & ENBUSFREE) != 0) {
1204 struct ahc_devinfo devinfo;
1209 u_int initiator_role_id;
1214 * Clear our selection hardware as soon as possible.
1215 * We may have an entry in the waiting Q for this target,
1216 * that is affected by this busfree and we don't want to
1217 * go about selecting the target while we handle the event.
1219 ahc_outb(ahc, SCSISEQ,
1220 ahc_inb(ahc, SCSISEQ) & (ENSELI|ENRSELI|ENAUTOATNP));
1223 * Disable busfree interrupts and clear the busfree
1224 * interrupt status. We do this here so that several
1225 * bus transactions occur prior to clearing the SCSIINT
1226 * latch. It can take a bit for the clearing to take effect.
1228 ahc_outb(ahc, SIMODE1, ahc_inb(ahc, SIMODE1) & ~ENBUSFREE);
1229 ahc_outb(ahc, CLRSINT1, CLRBUSFREE|CLRSCSIPERR);
1232 * Look at what phase we were last in.
1233 * If its message out, chances are pretty good
1234 * that the busfree was in response to one of
1235 * our abort requests.
1237 lastphase = ahc_inb(ahc, LASTPHASE);
1238 saved_scsiid = ahc_inb(ahc, SAVED_SCSIID);
1239 saved_lun = ahc_inb(ahc, SAVED_LUN);
1240 target = SCSIID_TARGET(ahc, saved_scsiid);
1241 initiator_role_id = SCSIID_OUR_ID(saved_scsiid);
1242 channel = SCSIID_CHANNEL(ahc, saved_scsiid);
1243 ahc_compile_devinfo(&devinfo, initiator_role_id,
1244 target, saved_lun, channel, ROLE_INITIATOR);
1247 if (lastphase == P_MESGOUT) {
1250 tag = SCB_LIST_NULL;
1251 if (ahc_sent_msg(ahc, AHCMSG_1B, MSG_ABORT_TAG, TRUE)
1252 || ahc_sent_msg(ahc, AHCMSG_1B, MSG_ABORT, TRUE)) {
1253 if (ahc->msgout_buf[ahc->msgout_index - 1]
1255 tag = scb->hscb->tag;
1256 ahc_print_path(ahc, scb);
1257 printf("SCB %d - Abort%s Completed.\n",
1258 scb->hscb->tag, tag == SCB_LIST_NULL ?
1260 ahc_abort_scbs(ahc, target, channel,
1265 } else if (ahc_sent_msg(ahc, AHCMSG_1B,
1266 MSG_BUS_DEV_RESET, TRUE)) {
1269 * Don't mark the user's request for this BDR
1270 * as completing with CAM_BDR_SENT. CAM3
1271 * specifies CAM_REQ_CMP.
1274 && scb->io_ctx->ccb_h.func_code== XPT_RESET_DEV
1275 && ahc_match_scb(ahc, scb, target, channel,
1279 ahc_set_transaction_status(scb, CAM_REQ_CMP);
1282 ahc_compile_devinfo(&devinfo,
1288 ahc_handle_devreset(ahc, &devinfo,
1291 /*verbose_level*/0);
1293 } else if (ahc_sent_msg(ahc, AHCMSG_EXT,
1294 MSG_EXT_PPR, FALSE)) {
1295 struct ahc_initiator_tinfo *tinfo;
1296 struct ahc_tmode_tstate *tstate;
1299 * PPR Rejected. Try non-ppr negotiation
1300 * and retry command.
1302 tinfo = ahc_fetch_transinfo(ahc,
1307 tinfo->curr.transport_version = 2;
1308 tinfo->goal.transport_version = 2;
1309 tinfo->goal.ppr_options = 0;
1310 ahc_qinfifo_requeue_tail(ahc, scb);
1312 } else if (ahc_sent_msg(ahc, AHCMSG_EXT,
1313 MSG_EXT_WDTR, FALSE)) {
1315 * Negotiation Rejected. Go-narrow and
1318 ahc_set_width(ahc, &devinfo,
1319 MSG_EXT_WDTR_BUS_8_BIT,
1320 AHC_TRANS_CUR|AHC_TRANS_GOAL,
1322 ahc_qinfifo_requeue_tail(ahc, scb);
1324 } else if (ahc_sent_msg(ahc, AHCMSG_EXT,
1325 MSG_EXT_SDTR, FALSE)) {
1327 * Negotiation Rejected. Go-async and
1330 ahc_set_syncrate(ahc, &devinfo,
1332 /*period*/0, /*offset*/0,
1334 AHC_TRANS_CUR|AHC_TRANS_GOAL,
1336 ahc_qinfifo_requeue_tail(ahc, scb);
1340 if (printerror != 0) {
1346 if ((scb->hscb->control & TAG_ENB) != 0)
1347 tag = scb->hscb->tag;
1349 tag = SCB_LIST_NULL;
1350 ahc_print_path(ahc, scb);
1351 ahc_abort_scbs(ahc, target, channel,
1352 SCB_GET_LUN(scb), tag,
1357 * We had not fully identified this connection,
1358 * so we cannot abort anything.
1360 printf("%s: ", ahc_name(ahc));
1362 for (i = 0; i < num_phases; i++) {
1363 if (lastphase == ahc_phase_table[i].phase)
1366 if (lastphase != P_BUSFREE) {
1368 * Renegotiate with this device at the
1369 * next oportunity just in case this busfree
1370 * is due to a negotiation mismatch with the
1373 ahc_force_renegotiation(ahc, &devinfo);
1375 printf("Unexpected busfree %s\n"
1376 "SEQADDR == 0x%x\n",
1377 ahc_phase_table[i].phasemsg,
1378 ahc_inb(ahc, SEQADDR0)
1379 | (ahc_inb(ahc, SEQADDR1) << 8));
1381 ahc_outb(ahc, CLRINT, CLRSCSIINT);
1384 printf("%s: Missing case in ahc_handle_scsiint. status = %x\n",
1385 ahc_name(ahc), status);
1386 ahc_outb(ahc, CLRINT, CLRSCSIINT);
1391 * Force renegotiation to occur the next time we initiate
1392 * a command to the current device.
1395 ahc_force_renegotiation(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
1397 struct ahc_initiator_tinfo *targ_info;
1398 struct ahc_tmode_tstate *tstate;
1400 targ_info = ahc_fetch_transinfo(ahc,
1402 devinfo->our_scsiid,
1405 ahc_update_neg_request(ahc, devinfo, tstate,
1406 targ_info, AHC_NEG_IF_NON_ASYNC);
1409 #define AHC_MAX_STEPS 2000
1411 ahc_clear_critical_section(struct ahc_softc *ahc)
1418 if (ahc->num_critical_sections == 0)
1430 seqaddr = ahc_inb(ahc, SEQADDR0)
1431 | (ahc_inb(ahc, SEQADDR1) << 8);
1434 * Seqaddr represents the next instruction to execute,
1435 * so we are really executing the instruction just
1440 cs = ahc->critical_sections;
1441 for (i = 0; i < ahc->num_critical_sections; i++, cs++) {
1443 if (cs->begin < seqaddr && cs->end >= seqaddr)
1447 if (i == ahc->num_critical_sections)
1450 if (steps > AHC_MAX_STEPS) {
1451 printf("%s: Infinite loop in critical section\n",
1453 ahc_dump_card_state(ahc);
1454 panic("critical section loop");
1458 if (stepping == FALSE) {
1461 * Disable all interrupt sources so that the
1462 * sequencer will not be stuck by a pausing
1463 * interrupt condition while we attempt to
1464 * leave a critical section.
1466 simode0 = ahc_inb(ahc, SIMODE0);
1467 ahc_outb(ahc, SIMODE0, 0);
1468 simode1 = ahc_inb(ahc, SIMODE1);
1469 if ((ahc->features & AHC_DT) != 0)
1471 * On DT class controllers, we
1472 * use the enhanced busfree logic.
1473 * Unfortunately we cannot re-enable
1474 * busfree detection within the
1475 * current connection, so we must
1476 * leave it on while single stepping.
1478 ahc_outb(ahc, SIMODE1, simode1 & ENBUSFREE);
1480 ahc_outb(ahc, SIMODE1, 0);
1481 ahc_outb(ahc, CLRINT, CLRSCSIINT);
1482 ahc_outb(ahc, SEQCTL, ahc->seqctl | STEP);
1485 if ((ahc->features & AHC_DT) != 0) {
1486 ahc_outb(ahc, CLRSINT1, CLRBUSFREE);
1487 ahc_outb(ahc, CLRINT, CLRSCSIINT);
1489 ahc_outb(ahc, HCNTRL, ahc->unpause);
1490 while (!ahc_is_paused(ahc))
1494 ahc_outb(ahc, SIMODE0, simode0);
1495 ahc_outb(ahc, SIMODE1, simode1);
1496 ahc_outb(ahc, SEQCTL, ahc->seqctl);
1501 * Clear any pending interrupt status.
1504 ahc_clear_intstat(struct ahc_softc *ahc)
1506 /* Clear any interrupt conditions this may have caused */
1507 ahc_outb(ahc, CLRSINT1, CLRSELTIMEO|CLRATNO|CLRSCSIRSTI
1508 |CLRBUSFREE|CLRSCSIPERR|CLRPHASECHG|
1510 ahc_flush_device_writes(ahc);
1511 ahc_outb(ahc, CLRSINT0, CLRSELDO|CLRSELDI|CLRSELINGO);
1512 ahc_flush_device_writes(ahc);
1513 ahc_outb(ahc, CLRINT, CLRSCSIINT);
1514 ahc_flush_device_writes(ahc);
1517 /**************************** Debugging Routines ******************************/
1519 uint32_t ahc_debug = AHC_DEBUG_OPTS;
1523 ahc_print_scb(struct scb *scb)
1527 struct hardware_scb *hscb = scb->hscb;
1529 printf("scb:%p control:0x%x scsiid:0x%x lun:%d cdb_len:%d\n",
1535 printf("Shared Data: ");
1536 for (i = 0; i < sizeof(hscb->shared_data.cdb); i++)
1537 printf("%#02x", hscb->shared_data.cdb[i]);
1538 printf(" dataptr:%#x datacnt:%#x sgptr:%#x tag:%#x\n",
1539 ahc_le32toh(hscb->dataptr),
1540 ahc_le32toh(hscb->datacnt),
1541 ahc_le32toh(hscb->sgptr),
1543 if (scb->sg_count > 0) {
1544 for (i = 0; i < scb->sg_count; i++) {
1545 printf("sg[%d] - Addr 0x%x%x : Length %d\n",
1547 (ahc_le32toh(scb->sg_list[i].len) >> 24
1548 & SG_HIGH_ADDR_BITS),
1549 ahc_le32toh(scb->sg_list[i].addr),
1550 ahc_le32toh(scb->sg_list[i].len));
1555 /************************* Transfer Negotiation *******************************/
1557 * Allocate per target mode instance (ID we respond to as a target)
1558 * transfer negotiation data structures.
1560 static struct ahc_tmode_tstate *
1561 ahc_alloc_tstate(struct ahc_softc *ahc, u_int scsi_id, char channel)
1563 struct ahc_tmode_tstate *master_tstate;
1564 struct ahc_tmode_tstate *tstate;
1567 master_tstate = ahc->enabled_targets[ahc->our_id];
1568 if (channel == 'B') {
1570 master_tstate = ahc->enabled_targets[ahc->our_id_b + 8];
1572 if (ahc->enabled_targets[scsi_id] != NULL
1573 && ahc->enabled_targets[scsi_id] != master_tstate)
1574 panic("%s: ahc_alloc_tstate - Target already allocated",
1576 tstate = (struct ahc_tmode_tstate*)malloc(sizeof(*tstate),
1577 M_DEVBUF, M_NOWAIT);
1582 * If we have allocated a master tstate, copy user settings from
1583 * the master tstate (taken from SRAM or the EEPROM) for this
1584 * channel, but reset our current and goal settings to async/narrow
1585 * until an initiator talks to us.
1587 if (master_tstate != NULL) {
1588 memcpy(tstate, master_tstate, sizeof(*tstate));
1589 memset(tstate->enabled_luns, 0, sizeof(tstate->enabled_luns));
1590 tstate->ultraenb = 0;
1591 for (i = 0; i < AHC_NUM_TARGETS; i++) {
1592 memset(&tstate->transinfo[i].curr, 0,
1593 sizeof(tstate->transinfo[i].curr));
1594 memset(&tstate->transinfo[i].goal, 0,
1595 sizeof(tstate->transinfo[i].goal));
1598 memset(tstate, 0, sizeof(*tstate));
1599 ahc->enabled_targets[scsi_id] = tstate;
1603 #ifdef AHC_TARGET_MODE
1605 * Free per target mode instance (ID we respond to as a target)
1606 * transfer negotiation data structures.
1609 ahc_free_tstate(struct ahc_softc *ahc, u_int scsi_id, char channel, int force)
1611 struct ahc_tmode_tstate *tstate;
1614 * Don't clean up our "master" tstate.
1615 * It has our default user settings.
1617 if (((channel == 'B' && scsi_id == ahc->our_id_b)
1618 || (channel == 'A' && scsi_id == ahc->our_id))
1624 tstate = ahc->enabled_targets[scsi_id];
1626 free(tstate, M_DEVBUF);
1627 ahc->enabled_targets[scsi_id] = NULL;
1632 * Called when we have an active connection to a target on the bus,
1633 * this function finds the nearest syncrate to the input period limited
1634 * by the capabilities of the bus connectivity of and sync settings for
1637 struct ahc_syncrate *
1638 ahc_devlimited_syncrate(struct ahc_softc *ahc,
1639 struct ahc_initiator_tinfo *tinfo,
1640 u_int *period, u_int *ppr_options, role_t role)
1642 struct ahc_transinfo *transinfo;
1645 if ((ahc->features & AHC_ULTRA2) != 0) {
1646 if ((ahc_inb(ahc, SBLKCTL) & ENAB40) != 0
1647 && (ahc_inb(ahc, SSTAT2) & EXP_ACTIVE) == 0) {
1648 maxsync = AHC_SYNCRATE_DT;
1650 maxsync = AHC_SYNCRATE_ULTRA;
1651 /* Can't do DT on an SE bus */
1652 *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
1654 } else if ((ahc->features & AHC_ULTRA) != 0) {
1655 maxsync = AHC_SYNCRATE_ULTRA;
1657 maxsync = AHC_SYNCRATE_FAST;
1660 * Never allow a value higher than our current goal
1661 * period otherwise we may allow a target initiated
1662 * negotiation to go above the limit as set by the
1663 * user. In the case of an initiator initiated
1664 * sync negotiation, we limit based on the user
1665 * setting. This allows the system to still accept
1666 * incoming negotiations even if target initiated
1667 * negotiation is not performed.
1669 if (role == ROLE_TARGET)
1670 transinfo = &tinfo->user;
1672 transinfo = &tinfo->goal;
1673 *ppr_options &= transinfo->ppr_options;
1674 if (transinfo->width == MSG_EXT_WDTR_BUS_8_BIT) {
1675 maxsync = max(maxsync, (u_int)AHC_SYNCRATE_ULTRA2);
1676 *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
1678 if (transinfo->period == 0) {
1683 *period = max(*period, (u_int)transinfo->period);
1684 return (ahc_find_syncrate(ahc, period, ppr_options, maxsync));
1688 * Look up the valid period to SCSIRATE conversion in our table.
1689 * Return the period and offset that should be sent to the target
1690 * if this was the beginning of an SDTR.
1692 struct ahc_syncrate *
1693 ahc_find_syncrate(struct ahc_softc *ahc, u_int *period,
1694 u_int *ppr_options, u_int maxsync)
1696 struct ahc_syncrate *syncrate;
1698 if ((ahc->features & AHC_DT) == 0)
1699 *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
1701 /* Skip all DT only entries if DT is not available */
1702 if ((*ppr_options & MSG_EXT_PPR_DT_REQ) == 0
1703 && maxsync < AHC_SYNCRATE_ULTRA2)
1704 maxsync = AHC_SYNCRATE_ULTRA2;
1706 /* Now set the maxsync based on the card capabilities
1707 * DT is already done above */
1708 if ((ahc->features & (AHC_DT | AHC_ULTRA2)) == 0
1709 && maxsync < AHC_SYNCRATE_ULTRA)
1710 maxsync = AHC_SYNCRATE_ULTRA;
1711 if ((ahc->features & (AHC_DT | AHC_ULTRA2 | AHC_ULTRA)) == 0
1712 && maxsync < AHC_SYNCRATE_FAST)
1713 maxsync = AHC_SYNCRATE_FAST;
1715 for (syncrate = &ahc_syncrates[maxsync];
1716 syncrate->rate != NULL;
1720 * The Ultra2 table doesn't go as low
1721 * as for the Fast/Ultra cards.
1723 if ((ahc->features & AHC_ULTRA2) != 0
1724 && (syncrate->sxfr_u2 == 0))
1727 if (*period <= syncrate->period) {
1729 * When responding to a target that requests
1730 * sync, the requested rate may fall between
1731 * two rates that we can output, but still be
1732 * a rate that we can receive. Because of this,
1733 * we want to respond to the target with
1734 * the same rate that it sent to us even
1735 * if the period we use to send data to it
1736 * is lower. Only lower the response period
1739 if (syncrate == &ahc_syncrates[maxsync])
1740 *period = syncrate->period;
1743 * At some speeds, we only support
1746 if ((syncrate->sxfr_u2 & ST_SXFR) != 0)
1747 *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
1753 || (syncrate->rate == NULL)
1754 || ((ahc->features & AHC_ULTRA2) != 0
1755 && (syncrate->sxfr_u2 == 0))) {
1756 /* Use asynchronous transfers. */
1759 *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
1765 * Convert from an entry in our syncrate table to the SCSI equivalent
1766 * sync "period" factor.
1769 ahc_find_period(struct ahc_softc *ahc, u_int scsirate, u_int maxsync)
1771 struct ahc_syncrate *syncrate;
1773 if ((ahc->features & AHC_ULTRA2) != 0)
1774 scsirate &= SXFR_ULTRA2;
1778 /* now set maxsync based on card capabilities */
1779 if ((ahc->features & AHC_DT) == 0 && maxsync < AHC_SYNCRATE_ULTRA2)
1780 maxsync = AHC_SYNCRATE_ULTRA2;
1781 if ((ahc->features & (AHC_DT | AHC_ULTRA2)) == 0
1782 && maxsync < AHC_SYNCRATE_ULTRA)
1783 maxsync = AHC_SYNCRATE_ULTRA;
1784 if ((ahc->features & (AHC_DT | AHC_ULTRA2 | AHC_ULTRA)) == 0
1785 && maxsync < AHC_SYNCRATE_FAST)
1786 maxsync = AHC_SYNCRATE_FAST;
1789 syncrate = &ahc_syncrates[maxsync];
1790 while (syncrate->rate != NULL) {
1792 if ((ahc->features & AHC_ULTRA2) != 0) {
1793 if (syncrate->sxfr_u2 == 0)
1795 else if (scsirate == (syncrate->sxfr_u2 & SXFR_ULTRA2))
1796 return (syncrate->period);
1797 } else if (scsirate == (syncrate->sxfr & SXFR)) {
1798 return (syncrate->period);
1802 return (0); /* async */
1806 * Truncate the given synchronous offset to a value the
1807 * current adapter type and syncrate are capable of.
1810 ahc_validate_offset(struct ahc_softc *ahc,
1811 struct ahc_initiator_tinfo *tinfo,
1812 struct ahc_syncrate *syncrate,
1813 u_int *offset, int wide, role_t role)
1817 /* Limit offset to what we can do */
1818 if (syncrate == NULL) {
1820 } else if ((ahc->features & AHC_ULTRA2) != 0) {
1821 maxoffset = MAX_OFFSET_ULTRA2;
1824 maxoffset = MAX_OFFSET_16BIT;
1826 maxoffset = MAX_OFFSET_8BIT;
1828 *offset = min(*offset, maxoffset);
1829 if (tinfo != NULL) {
1830 if (role == ROLE_TARGET)
1831 *offset = min(*offset, (u_int)tinfo->user.offset);
1833 *offset = min(*offset, (u_int)tinfo->goal.offset);
1838 * Truncate the given transfer width parameter to a value the
1839 * current adapter type is capable of.
1842 ahc_validate_width(struct ahc_softc *ahc, struct ahc_initiator_tinfo *tinfo,
1843 u_int *bus_width, role_t role)
1845 switch (*bus_width) {
1847 if (ahc->features & AHC_WIDE) {
1849 *bus_width = MSG_EXT_WDTR_BUS_16_BIT;
1853 case MSG_EXT_WDTR_BUS_8_BIT:
1854 *bus_width = MSG_EXT_WDTR_BUS_8_BIT;
1857 if (tinfo != NULL) {
1858 if (role == ROLE_TARGET)
1859 *bus_width = min((u_int)tinfo->user.width, *bus_width);
1861 *bus_width = min((u_int)tinfo->goal.width, *bus_width);
1866 * Update the bitmask of targets for which the controller should
1867 * negotiate with at the next convenient oportunity. This currently
1868 * means the next time we send the initial identify messages for
1869 * a new transaction.
1872 ahc_update_neg_request(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
1873 struct ahc_tmode_tstate *tstate,
1874 struct ahc_initiator_tinfo *tinfo, ahc_neg_type neg_type)
1876 u_int auto_negotiate_orig;
1878 auto_negotiate_orig = tstate->auto_negotiate;
1879 if (neg_type == AHC_NEG_ALWAYS) {
1881 * Force our "current" settings to be
1882 * unknown so that unless a bus reset
1883 * occurs the need to renegotiate is
1884 * recorded persistently.
1886 if ((ahc->features & AHC_WIDE) != 0)
1887 tinfo->curr.width = AHC_WIDTH_UNKNOWN;
1888 tinfo->curr.period = AHC_PERIOD_UNKNOWN;
1889 tinfo->curr.offset = AHC_OFFSET_UNKNOWN;
1891 if (tinfo->curr.period != tinfo->goal.period
1892 || tinfo->curr.width != tinfo->goal.width
1893 || tinfo->curr.offset != tinfo->goal.offset
1894 || tinfo->curr.ppr_options != tinfo->goal.ppr_options
1895 || (neg_type == AHC_NEG_IF_NON_ASYNC
1896 && (tinfo->goal.offset != 0
1897 || tinfo->goal.width != MSG_EXT_WDTR_BUS_8_BIT
1898 || tinfo->goal.ppr_options != 0)))
1899 tstate->auto_negotiate |= devinfo->target_mask;
1901 tstate->auto_negotiate &= ~devinfo->target_mask;
1903 return (auto_negotiate_orig != tstate->auto_negotiate);
1907 * Update the user/goal/curr tables of synchronous negotiation
1908 * parameters as well as, in the case of a current or active update,
1909 * any data structures on the host controller. In the case of an
1910 * active update, the specified target is currently talking to us on
1911 * the bus, so the transfer parameter update must take effect
1915 ahc_set_syncrate(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
1916 struct ahc_syncrate *syncrate, u_int period,
1917 u_int offset, u_int ppr_options, u_int type, int paused)
1919 struct ahc_initiator_tinfo *tinfo;
1920 struct ahc_tmode_tstate *tstate;
1927 active = (type & AHC_TRANS_ACTIVE) == AHC_TRANS_ACTIVE;
1930 if (syncrate == NULL) {
1935 tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid,
1936 devinfo->target, &tstate);
1938 if ((type & AHC_TRANS_USER) != 0) {
1939 tinfo->user.period = period;
1940 tinfo->user.offset = offset;
1941 tinfo->user.ppr_options = ppr_options;
1944 if ((type & AHC_TRANS_GOAL) != 0) {
1945 tinfo->goal.period = period;
1946 tinfo->goal.offset = offset;
1947 tinfo->goal.ppr_options = ppr_options;
1950 old_period = tinfo->curr.period;
1951 old_offset = tinfo->curr.offset;
1952 old_ppr = tinfo->curr.ppr_options;
1954 if ((type & AHC_TRANS_CUR) != 0
1955 && (old_period != period
1956 || old_offset != offset
1957 || old_ppr != ppr_options)) {
1961 scsirate = tinfo->scsirate;
1962 if ((ahc->features & AHC_ULTRA2) != 0) {
1964 scsirate &= ~(SXFR_ULTRA2|SINGLE_EDGE|ENABLE_CRC);
1965 if (syncrate != NULL) {
1966 scsirate |= syncrate->sxfr_u2;
1967 if ((ppr_options & MSG_EXT_PPR_DT_REQ) != 0)
1968 scsirate |= ENABLE_CRC;
1970 scsirate |= SINGLE_EDGE;
1974 scsirate &= ~(SXFR|SOFS);
1976 * Ensure Ultra mode is set properly for
1979 tstate->ultraenb &= ~devinfo->target_mask;
1980 if (syncrate != NULL) {
1981 if (syncrate->sxfr & ULTRA_SXFR) {
1983 devinfo->target_mask;
1985 scsirate |= syncrate->sxfr & SXFR;
1986 scsirate |= offset & SOFS;
1991 sxfrctl0 = ahc_inb(ahc, SXFRCTL0);
1992 sxfrctl0 &= ~FAST20;
1993 if (tstate->ultraenb & devinfo->target_mask)
1995 ahc_outb(ahc, SXFRCTL0, sxfrctl0);
1999 ahc_outb(ahc, SCSIRATE, scsirate);
2000 if ((ahc->features & AHC_ULTRA2) != 0)
2001 ahc_outb(ahc, SCSIOFFSET, offset);
2004 tinfo->scsirate = scsirate;
2005 tinfo->curr.period = period;
2006 tinfo->curr.offset = offset;
2007 tinfo->curr.ppr_options = ppr_options;
2009 ahc_send_async(ahc, devinfo->channel, devinfo->target,
2010 CAM_LUN_WILDCARD, AC_TRANSFER_NEG);
2013 printf("%s: target %d synchronous at %sMHz%s, "
2014 "offset = 0x%x\n", ahc_name(ahc),
2015 devinfo->target, syncrate->rate,
2016 (ppr_options & MSG_EXT_PPR_DT_REQ)
2017 ? " DT" : "", offset);
2019 printf("%s: target %d using "
2020 "asynchronous transfers\n",
2021 ahc_name(ahc), devinfo->target);
2026 update_needed += ahc_update_neg_request(ahc, devinfo, tstate,
2027 tinfo, AHC_NEG_TO_GOAL);
2030 ahc_update_pending_scbs(ahc);
2034 * Update the user/goal/curr tables of wide negotiation
2035 * parameters as well as, in the case of a current or active update,
2036 * any data structures on the host controller. In the case of an
2037 * active update, the specified target is currently talking to us on
2038 * the bus, so the transfer parameter update must take effect
2042 ahc_set_width(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
2043 u_int width, u_int type, int paused)
2045 struct ahc_initiator_tinfo *tinfo;
2046 struct ahc_tmode_tstate *tstate;
2051 active = (type & AHC_TRANS_ACTIVE) == AHC_TRANS_ACTIVE;
2053 tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid,
2054 devinfo->target, &tstate);
2056 if ((type & AHC_TRANS_USER) != 0)
2057 tinfo->user.width = width;
2059 if ((type & AHC_TRANS_GOAL) != 0)
2060 tinfo->goal.width = width;
2062 oldwidth = tinfo->curr.width;
2063 if ((type & AHC_TRANS_CUR) != 0 && oldwidth != width) {
2067 scsirate = tinfo->scsirate;
2068 scsirate &= ~WIDEXFER;
2069 if (width == MSG_EXT_WDTR_BUS_16_BIT)
2070 scsirate |= WIDEXFER;
2072 tinfo->scsirate = scsirate;
2075 ahc_outb(ahc, SCSIRATE, scsirate);
2077 tinfo->curr.width = width;
2079 ahc_send_async(ahc, devinfo->channel, devinfo->target,
2080 CAM_LUN_WILDCARD, AC_TRANSFER_NEG);
2082 printf("%s: target %d using %dbit transfers\n",
2083 ahc_name(ahc), devinfo->target,
2084 8 * (0x01 << width));
2088 update_needed += ahc_update_neg_request(ahc, devinfo, tstate,
2089 tinfo, AHC_NEG_TO_GOAL);
2091 ahc_update_pending_scbs(ahc);
2095 * Update the current state of tagged queuing for a given target.
2098 ahc_set_tags(struct ahc_softc *ahc, struct scsi_cmnd *cmd,
2099 struct ahc_devinfo *devinfo, ahc_queue_alg alg)
2101 struct scsi_device *sdev = cmd->device;
2103 ahc_platform_set_tags(ahc, sdev, devinfo, alg);
2104 ahc_send_async(ahc, devinfo->channel, devinfo->target,
2105 devinfo->lun, AC_TRANSFER_NEG);
2109 * When the transfer settings for a connection change, update any
2110 * in-transit SCBs to contain the new data so the hardware will
2111 * be set correctly during future (re)selections.
2114 ahc_update_pending_scbs(struct ahc_softc *ahc)
2116 struct scb *pending_scb;
2117 int pending_scb_count;
2123 * Traverse the pending SCB list and ensure that all of the
2124 * SCBs there have the proper settings.
2126 pending_scb_count = 0;
2127 LIST_FOREACH(pending_scb, &ahc->pending_scbs, pending_links) {
2128 struct ahc_devinfo devinfo;
2129 struct hardware_scb *pending_hscb;
2130 struct ahc_initiator_tinfo *tinfo;
2131 struct ahc_tmode_tstate *tstate;
2133 ahc_scb_devinfo(ahc, &devinfo, pending_scb);
2134 tinfo = ahc_fetch_transinfo(ahc, devinfo.channel,
2136 devinfo.target, &tstate);
2137 pending_hscb = pending_scb->hscb;
2138 pending_hscb->control &= ~ULTRAENB;
2139 if ((tstate->ultraenb & devinfo.target_mask) != 0)
2140 pending_hscb->control |= ULTRAENB;
2141 pending_hscb->scsirate = tinfo->scsirate;
2142 pending_hscb->scsioffset = tinfo->curr.offset;
2143 if ((tstate->auto_negotiate & devinfo.target_mask) == 0
2144 && (pending_scb->flags & SCB_AUTO_NEGOTIATE) != 0) {
2145 pending_scb->flags &= ~SCB_AUTO_NEGOTIATE;
2146 pending_hscb->control &= ~MK_MESSAGE;
2148 ahc_sync_scb(ahc, pending_scb,
2149 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
2150 pending_scb_count++;
2153 if (pending_scb_count == 0)
2156 if (ahc_is_paused(ahc)) {
2163 saved_scbptr = ahc_inb(ahc, SCBPTR);
2164 /* Ensure that the hscbs down on the card match the new information */
2165 for (i = 0; i < ahc->scb_data->maxhscbs; i++) {
2166 struct hardware_scb *pending_hscb;
2170 ahc_outb(ahc, SCBPTR, i);
2171 scb_tag = ahc_inb(ahc, SCB_TAG);
2172 pending_scb = ahc_lookup_scb(ahc, scb_tag);
2173 if (pending_scb == NULL)
2176 pending_hscb = pending_scb->hscb;
2177 control = ahc_inb(ahc, SCB_CONTROL);
2178 control &= ~(ULTRAENB|MK_MESSAGE);
2179 control |= pending_hscb->control & (ULTRAENB|MK_MESSAGE);
2180 ahc_outb(ahc, SCB_CONTROL, control);
2181 ahc_outb(ahc, SCB_SCSIRATE, pending_hscb->scsirate);
2182 ahc_outb(ahc, SCB_SCSIOFFSET, pending_hscb->scsioffset);
2184 ahc_outb(ahc, SCBPTR, saved_scbptr);
2190 /**************************** Pathing Information *****************************/
2192 ahc_fetch_devinfo(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
2198 if (ahc_inb(ahc, SSTAT0) & TARGET)
2201 role = ROLE_INITIATOR;
2203 if (role == ROLE_TARGET
2204 && (ahc->features & AHC_MULTI_TID) != 0
2205 && (ahc_inb(ahc, SEQ_FLAGS)
2206 & (CMDPHASE_PENDING|TARG_CMD_PENDING|NO_DISCONNECT)) != 0) {
2207 /* We were selected, so pull our id from TARGIDIN */
2208 our_id = ahc_inb(ahc, TARGIDIN) & OID;
2209 } else if ((ahc->features & AHC_ULTRA2) != 0)
2210 our_id = ahc_inb(ahc, SCSIID_ULTRA2) & OID;
2212 our_id = ahc_inb(ahc, SCSIID) & OID;
2214 saved_scsiid = ahc_inb(ahc, SAVED_SCSIID);
2215 ahc_compile_devinfo(devinfo,
2217 SCSIID_TARGET(ahc, saved_scsiid),
2218 ahc_inb(ahc, SAVED_LUN),
2219 SCSIID_CHANNEL(ahc, saved_scsiid),
2223 struct ahc_phase_table_entry*
2224 ahc_lookup_phase_entry(int phase)
2226 struct ahc_phase_table_entry *entry;
2227 struct ahc_phase_table_entry *last_entry;
2230 * num_phases doesn't include the default entry which
2231 * will be returned if the phase doesn't match.
2233 last_entry = &ahc_phase_table[num_phases];
2234 for (entry = ahc_phase_table; entry < last_entry; entry++) {
2235 if (phase == entry->phase)
2242 ahc_compile_devinfo(struct ahc_devinfo *devinfo, u_int our_id, u_int target,
2243 u_int lun, char channel, role_t role)
2245 devinfo->our_scsiid = our_id;
2246 devinfo->target = target;
2248 devinfo->target_offset = target;
2249 devinfo->channel = channel;
2250 devinfo->role = role;
2252 devinfo->target_offset += 8;
2253 devinfo->target_mask = (0x01 << devinfo->target_offset);
2257 ahc_print_devinfo(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
2259 printf("%s:%c:%d:%d: ", ahc_name(ahc), devinfo->channel,
2260 devinfo->target, devinfo->lun);
2264 ahc_scb_devinfo(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
2270 our_id = SCSIID_OUR_ID(scb->hscb->scsiid);
2271 role = ROLE_INITIATOR;
2272 if ((scb->flags & SCB_TARGET_SCB) != 0)
2274 ahc_compile_devinfo(devinfo, our_id, SCB_GET_TARGET(ahc, scb),
2275 SCB_GET_LUN(scb), SCB_GET_CHANNEL(ahc, scb), role);
2279 /************************ Message Phase Processing ****************************/
2281 ahc_assert_atn(struct ahc_softc *ahc)
2286 if ((ahc->features & AHC_DT) == 0)
2287 scsisigo |= ahc_inb(ahc, SCSISIGI);
2288 ahc_outb(ahc, SCSISIGO, scsisigo);
2292 * When an initiator transaction with the MK_MESSAGE flag either reconnects
2293 * or enters the initial message out phase, we are interrupted. Fill our
2294 * outgoing message buffer with the appropriate message and beging handing
2295 * the message phase(s) manually.
2298 ahc_setup_initiator_msgout(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
2302 * To facilitate adding multiple messages together,
2303 * each routine should increment the index and len
2304 * variables instead of setting them explicitly.
2306 ahc->msgout_index = 0;
2307 ahc->msgout_len = 0;
2309 if ((scb->flags & SCB_DEVICE_RESET) == 0
2310 && ahc_inb(ahc, MSG_OUT) == MSG_IDENTIFYFLAG) {
2313 identify_msg = MSG_IDENTIFYFLAG | SCB_GET_LUN(scb);
2314 if ((scb->hscb->control & DISCENB) != 0)
2315 identify_msg |= MSG_IDENTIFY_DISCFLAG;
2316 ahc->msgout_buf[ahc->msgout_index++] = identify_msg;
2319 if ((scb->hscb->control & TAG_ENB) != 0) {
2320 ahc->msgout_buf[ahc->msgout_index++] =
2321 scb->hscb->control & (TAG_ENB|SCB_TAG_TYPE);
2322 ahc->msgout_buf[ahc->msgout_index++] = scb->hscb->tag;
2323 ahc->msgout_len += 2;
2327 if (scb->flags & SCB_DEVICE_RESET) {
2328 ahc->msgout_buf[ahc->msgout_index++] = MSG_BUS_DEV_RESET;
2330 ahc_print_path(ahc, scb);
2331 printf("Bus Device Reset Message Sent\n");
2333 * Clear our selection hardware in advance of
2334 * the busfree. We may have an entry in the waiting
2335 * Q for this target, and we don't want to go about
2336 * selecting while we handle the busfree and blow it
2339 ahc_outb(ahc, SCSISEQ, (ahc_inb(ahc, SCSISEQ) & ~ENSELO));
2340 } else if ((scb->flags & SCB_ABORT) != 0) {
2341 if ((scb->hscb->control & TAG_ENB) != 0)
2342 ahc->msgout_buf[ahc->msgout_index++] = MSG_ABORT_TAG;
2344 ahc->msgout_buf[ahc->msgout_index++] = MSG_ABORT;
2346 ahc_print_path(ahc, scb);
2347 printf("Abort%s Message Sent\n",
2348 (scb->hscb->control & TAG_ENB) != 0 ? " Tag" : "");
2350 * Clear our selection hardware in advance of
2351 * the busfree. We may have an entry in the waiting
2352 * Q for this target, and we don't want to go about
2353 * selecting while we handle the busfree and blow it
2356 ahc_outb(ahc, SCSISEQ, (ahc_inb(ahc, SCSISEQ) & ~ENSELO));
2357 } else if ((scb->flags & (SCB_AUTO_NEGOTIATE|SCB_NEGOTIATE)) != 0) {
2358 ahc_build_transfer_msg(ahc, devinfo);
2360 printf("ahc_intr: AWAITING_MSG for an SCB that "
2361 "does not have a waiting message\n");
2362 printf("SCSIID = %x, target_mask = %x\n", scb->hscb->scsiid,
2363 devinfo->target_mask);
2364 panic("SCB = %d, SCB Control = %x, MSG_OUT = %x "
2365 "SCB flags = %x", scb->hscb->tag, scb->hscb->control,
2366 ahc_inb(ahc, MSG_OUT), scb->flags);
2370 * Clear the MK_MESSAGE flag from the SCB so we aren't
2371 * asked to send this message again.
2373 ahc_outb(ahc, SCB_CONTROL, ahc_inb(ahc, SCB_CONTROL) & ~MK_MESSAGE);
2374 scb->hscb->control &= ~MK_MESSAGE;
2375 ahc->msgout_index = 0;
2376 ahc->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
2380 * Build an appropriate transfer negotiation message for the
2381 * currently active target.
2384 ahc_build_transfer_msg(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
2387 * We need to initiate transfer negotiations.
2388 * If our current and goal settings are identical,
2389 * we want to renegotiate due to a check condition.
2391 struct ahc_initiator_tinfo *tinfo;
2392 struct ahc_tmode_tstate *tstate;
2393 struct ahc_syncrate *rate;
2401 tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid,
2402 devinfo->target, &tstate);
2404 * Filter our period based on the current connection.
2405 * If we can't perform DT transfers on this segment (not in LVD
2406 * mode for instance), then our decision to issue a PPR message
2409 period = tinfo->goal.period;
2410 offset = tinfo->goal.offset;
2411 ppr_options = tinfo->goal.ppr_options;
2412 /* Target initiated PPR is not allowed in the SCSI spec */
2413 if (devinfo->role == ROLE_TARGET)
2415 rate = ahc_devlimited_syncrate(ahc, tinfo, &period,
2416 &ppr_options, devinfo->role);
2417 dowide = tinfo->curr.width != tinfo->goal.width;
2418 dosync = tinfo->curr.offset != offset || tinfo->curr.period != period;
2420 * Only use PPR if we have options that need it, even if the device
2421 * claims to support it. There might be an expander in the way
2424 doppr = ppr_options != 0;
2426 if (!dowide && !dosync && !doppr) {
2427 dowide = tinfo->goal.width != MSG_EXT_WDTR_BUS_8_BIT;
2428 dosync = tinfo->goal.offset != 0;
2431 if (!dowide && !dosync && !doppr) {
2433 * Force async with a WDTR message if we have a wide bus,
2434 * or just issue an SDTR with a 0 offset.
2436 if ((ahc->features & AHC_WIDE) != 0)
2442 ahc_print_devinfo(ahc, devinfo);
2443 printf("Ensuring async\n");
2447 /* Target initiated PPR is not allowed in the SCSI spec */
2448 if (devinfo->role == ROLE_TARGET)
2452 * Both the PPR message and SDTR message require the
2453 * goal syncrate to be limited to what the target device
2454 * is capable of handling (based on whether an LVD->SE
2455 * expander is on the bus), so combine these two cases.
2456 * Regardless, guarantee that if we are using WDTR and SDTR
2457 * messages that WDTR comes first.
2459 if (doppr || (dosync && !dowide)) {
2461 offset = tinfo->goal.offset;
2462 ahc_validate_offset(ahc, tinfo, rate, &offset,
2463 doppr ? tinfo->goal.width
2464 : tinfo->curr.width,
2467 ahc_construct_ppr(ahc, devinfo, period, offset,
2468 tinfo->goal.width, ppr_options);
2470 ahc_construct_sdtr(ahc, devinfo, period, offset);
2473 ahc_construct_wdtr(ahc, devinfo, tinfo->goal.width);
2478 * Build a synchronous negotiation message in our message
2479 * buffer based on the input parameters.
2482 ahc_construct_sdtr(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
2483 u_int period, u_int offset)
2486 period = AHC_ASYNC_XFER_PERIOD;
2487 ahc->msgout_index += spi_populate_sync_msg(
2488 ahc->msgout_buf + ahc->msgout_index, period, offset);
2489 ahc->msgout_len += 5;
2491 printf("(%s:%c:%d:%d): Sending SDTR period %x, offset %x\n",
2492 ahc_name(ahc), devinfo->channel, devinfo->target,
2493 devinfo->lun, period, offset);
2498 * Build a wide negotiation message in our message
2499 * buffer based on the input parameters.
2502 ahc_construct_wdtr(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
2505 ahc->msgout_index += spi_populate_width_msg(
2506 ahc->msgout_buf + ahc->msgout_index, bus_width);
2507 ahc->msgout_len += 4;
2509 printf("(%s:%c:%d:%d): Sending WDTR %x\n",
2510 ahc_name(ahc), devinfo->channel, devinfo->target,
2511 devinfo->lun, bus_width);
2516 * Build a parallel protocol request message in our message
2517 * buffer based on the input parameters.
2520 ahc_construct_ppr(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
2521 u_int period, u_int offset, u_int bus_width,
2525 period = AHC_ASYNC_XFER_PERIOD;
2526 ahc->msgout_index += spi_populate_ppr_msg(
2527 ahc->msgout_buf + ahc->msgout_index, period, offset,
2528 bus_width, ppr_options);
2529 ahc->msgout_len += 8;
2531 printf("(%s:%c:%d:%d): Sending PPR bus_width %x, period %x, "
2532 "offset %x, ppr_options %x\n", ahc_name(ahc),
2533 devinfo->channel, devinfo->target, devinfo->lun,
2534 bus_width, period, offset, ppr_options);
2539 * Clear any active message state.
2542 ahc_clear_msg_state(struct ahc_softc *ahc)
2544 ahc->msgout_len = 0;
2545 ahc->msgin_index = 0;
2546 ahc->msg_type = MSG_TYPE_NONE;
2547 if ((ahc_inb(ahc, SCSISIGI) & ATNI) != 0) {
2549 * The target didn't care to respond to our
2550 * message request, so clear ATN.
2552 ahc_outb(ahc, CLRSINT1, CLRATNO);
2554 ahc_outb(ahc, MSG_OUT, MSG_NOOP);
2555 ahc_outb(ahc, SEQ_FLAGS2,
2556 ahc_inb(ahc, SEQ_FLAGS2) & ~TARGET_MSG_PENDING);
2560 ahc_handle_proto_violation(struct ahc_softc *ahc)
2562 struct ahc_devinfo devinfo;
2570 ahc_fetch_devinfo(ahc, &devinfo);
2571 scbid = ahc_inb(ahc, SCB_TAG);
2572 scb = ahc_lookup_scb(ahc, scbid);
2573 seq_flags = ahc_inb(ahc, SEQ_FLAGS);
2574 curphase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK;
2575 lastphase = ahc_inb(ahc, LASTPHASE);
2576 if ((seq_flags & NOT_IDENTIFIED) != 0) {
2579 * The reconnecting target either did not send an
2580 * identify message, or did, but we didn't find an SCB
2583 ahc_print_devinfo(ahc, &devinfo);
2584 printf("Target did not send an IDENTIFY message. "
2585 "LASTPHASE = 0x%x.\n", lastphase);
2587 } else if (scb == NULL) {
2589 * We don't seem to have an SCB active for this
2590 * transaction. Print an error and reset the bus.
2592 ahc_print_devinfo(ahc, &devinfo);
2593 printf("No SCB found during protocol violation\n");
2594 goto proto_violation_reset;
2596 ahc_set_transaction_status(scb, CAM_SEQUENCE_FAIL);
2597 if ((seq_flags & NO_CDB_SENT) != 0) {
2598 ahc_print_path(ahc, scb);
2599 printf("No or incomplete CDB sent to device.\n");
2600 } else if ((ahc_inb(ahc, SCB_CONTROL) & STATUS_RCVD) == 0) {
2602 * The target never bothered to provide status to
2603 * us prior to completing the command. Since we don't
2604 * know the disposition of this command, we must attempt
2605 * to abort it. Assert ATN and prepare to send an abort
2608 ahc_print_path(ahc, scb);
2609 printf("Completed command without status.\n");
2611 ahc_print_path(ahc, scb);
2612 printf("Unknown protocol violation.\n");
2613 ahc_dump_card_state(ahc);
2616 if ((lastphase & ~P_DATAIN_DT) == 0
2617 || lastphase == P_COMMAND) {
2618 proto_violation_reset:
2620 * Target either went directly to data/command
2621 * phase or didn't respond to our ATN.
2622 * The only safe thing to do is to blow
2623 * it away with a bus reset.
2625 found = ahc_reset_channel(ahc, 'A', TRUE);
2626 printf("%s: Issued Channel %c Bus Reset. "
2627 "%d SCBs aborted\n", ahc_name(ahc), 'A', found);
2630 * Leave the selection hardware off in case
2631 * this abort attempt will affect yet to
2634 ahc_outb(ahc, SCSISEQ,
2635 ahc_inb(ahc, SCSISEQ) & ~ENSELO);
2636 ahc_assert_atn(ahc);
2637 ahc_outb(ahc, MSG_OUT, HOST_MSG);
2639 ahc_print_devinfo(ahc, &devinfo);
2640 ahc->msgout_buf[0] = MSG_ABORT_TASK;
2641 ahc->msgout_len = 1;
2642 ahc->msgout_index = 0;
2643 ahc->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
2645 ahc_print_path(ahc, scb);
2646 scb->flags |= SCB_ABORT;
2648 printf("Protocol violation %s. Attempting to abort.\n",
2649 ahc_lookup_phase_entry(curphase)->phasemsg);
2654 * Manual message loop handler.
2657 ahc_handle_message_phase(struct ahc_softc *ahc)
2659 struct ahc_devinfo devinfo;
2663 ahc_fetch_devinfo(ahc, &devinfo);
2664 end_session = FALSE;
2665 bus_phase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK;
2668 switch (ahc->msg_type) {
2669 case MSG_TYPE_INITIATOR_MSGOUT:
2675 if (ahc->msgout_len == 0)
2676 panic("HOST_MSG_LOOP interrupt with no active message");
2679 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) {
2680 ahc_print_devinfo(ahc, &devinfo);
2681 printf("INITIATOR_MSG_OUT");
2684 phasemis = bus_phase != P_MESGOUT;
2687 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) {
2688 printf(" PHASEMIS %s\n",
2689 ahc_lookup_phase_entry(bus_phase)
2693 if (bus_phase == P_MESGIN) {
2695 * Change gears and see if
2696 * this messages is of interest to
2697 * us or should be passed back to
2700 ahc_outb(ahc, CLRSINT1, CLRATNO);
2701 ahc->send_msg_perror = FALSE;
2702 ahc->msg_type = MSG_TYPE_INITIATOR_MSGIN;
2703 ahc->msgin_index = 0;
2710 if (ahc->send_msg_perror) {
2711 ahc_outb(ahc, CLRSINT1, CLRATNO);
2712 ahc_outb(ahc, CLRSINT1, CLRREQINIT);
2714 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0)
2715 printf(" byte 0x%x\n", ahc->send_msg_perror);
2717 ahc_outb(ahc, SCSIDATL, MSG_PARITY_ERROR);
2721 msgdone = ahc->msgout_index == ahc->msgout_len;
2724 * The target has requested a retry.
2725 * Re-assert ATN, reset our message index to
2728 ahc->msgout_index = 0;
2729 ahc_assert_atn(ahc);
2732 lastbyte = ahc->msgout_index == (ahc->msgout_len - 1);
2734 /* Last byte is signified by dropping ATN */
2735 ahc_outb(ahc, CLRSINT1, CLRATNO);
2739 * Clear our interrupt status and present
2740 * the next byte on the bus.
2742 ahc_outb(ahc, CLRSINT1, CLRREQINIT);
2744 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0)
2745 printf(" byte 0x%x\n",
2746 ahc->msgout_buf[ahc->msgout_index]);
2748 ahc_outb(ahc, SCSIDATL, ahc->msgout_buf[ahc->msgout_index++]);
2751 case MSG_TYPE_INITIATOR_MSGIN:
2757 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) {
2758 ahc_print_devinfo(ahc, &devinfo);
2759 printf("INITIATOR_MSG_IN");
2762 phasemis = bus_phase != P_MESGIN;
2765 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) {
2766 printf(" PHASEMIS %s\n",
2767 ahc_lookup_phase_entry(bus_phase)
2771 ahc->msgin_index = 0;
2772 if (bus_phase == P_MESGOUT
2773 && (ahc->send_msg_perror == TRUE
2774 || (ahc->msgout_len != 0
2775 && ahc->msgout_index == 0))) {
2776 ahc->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
2783 /* Pull the byte in without acking it */
2784 ahc->msgin_buf[ahc->msgin_index] = ahc_inb(ahc, SCSIBUSL);
2786 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0)
2787 printf(" byte 0x%x\n",
2788 ahc->msgin_buf[ahc->msgin_index]);
2791 message_done = ahc_parse_msg(ahc, &devinfo);
2795 * Clear our incoming message buffer in case there
2796 * is another message following this one.
2798 ahc->msgin_index = 0;
2801 * If this message illicited a response,
2802 * assert ATN so the target takes us to the
2803 * message out phase.
2805 if (ahc->msgout_len != 0) {
2807 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) {
2808 ahc_print_devinfo(ahc, &devinfo);
2809 printf("Asserting ATN for response\n");
2812 ahc_assert_atn(ahc);
2817 if (message_done == MSGLOOP_TERMINATED) {
2821 ahc_outb(ahc, CLRSINT1, CLRREQINIT);
2822 ahc_inb(ahc, SCSIDATL);
2826 case MSG_TYPE_TARGET_MSGIN:
2831 if (ahc->msgout_len == 0)
2832 panic("Target MSGIN with no active message");
2835 * If we interrupted a mesgout session, the initiator
2836 * will not know this until our first REQ. So, we
2837 * only honor mesgout requests after we've sent our
2840 if ((ahc_inb(ahc, SCSISIGI) & ATNI) != 0
2841 && ahc->msgout_index > 0)
2842 msgout_request = TRUE;
2844 msgout_request = FALSE;
2846 if (msgout_request) {
2849 * Change gears and see if
2850 * this messages is of interest to
2851 * us or should be passed back to
2854 ahc->msg_type = MSG_TYPE_TARGET_MSGOUT;
2855 ahc_outb(ahc, SCSISIGO, P_MESGOUT | BSYO);
2856 ahc->msgin_index = 0;
2857 /* Dummy read to REQ for first byte */
2858 ahc_inb(ahc, SCSIDATL);
2859 ahc_outb(ahc, SXFRCTL0,
2860 ahc_inb(ahc, SXFRCTL0) | SPIOEN);
2864 msgdone = ahc->msgout_index == ahc->msgout_len;
2866 ahc_outb(ahc, SXFRCTL0,
2867 ahc_inb(ahc, SXFRCTL0) & ~SPIOEN);
2873 * Present the next byte on the bus.
2875 ahc_outb(ahc, SXFRCTL0, ahc_inb(ahc, SXFRCTL0) | SPIOEN);
2876 ahc_outb(ahc, SCSIDATL, ahc->msgout_buf[ahc->msgout_index++]);
2879 case MSG_TYPE_TARGET_MSGOUT:
2885 * The initiator signals that this is
2886 * the last byte by dropping ATN.
2888 lastbyte = (ahc_inb(ahc, SCSISIGI) & ATNI) == 0;
2891 * Read the latched byte, but turn off SPIOEN first
2892 * so that we don't inadvertently cause a REQ for the
2895 ahc_outb(ahc, SXFRCTL0, ahc_inb(ahc, SXFRCTL0) & ~SPIOEN);
2896 ahc->msgin_buf[ahc->msgin_index] = ahc_inb(ahc, SCSIDATL);
2897 msgdone = ahc_parse_msg(ahc, &devinfo);
2898 if (msgdone == MSGLOOP_TERMINATED) {
2900 * The message is *really* done in that it caused
2901 * us to go to bus free. The sequencer has already
2902 * been reset at this point, so pull the ejection
2911 * XXX Read spec about initiator dropping ATN too soon
2912 * and use msgdone to detect it.
2914 if (msgdone == MSGLOOP_MSGCOMPLETE) {
2915 ahc->msgin_index = 0;
2918 * If this message illicited a response, transition
2919 * to the Message in phase and send it.
2921 if (ahc->msgout_len != 0) {
2922 ahc_outb(ahc, SCSISIGO, P_MESGIN | BSYO);
2923 ahc_outb(ahc, SXFRCTL0,
2924 ahc_inb(ahc, SXFRCTL0) | SPIOEN);
2925 ahc->msg_type = MSG_TYPE_TARGET_MSGIN;
2926 ahc->msgin_index = 0;
2934 /* Ask for the next byte. */
2935 ahc_outb(ahc, SXFRCTL0,
2936 ahc_inb(ahc, SXFRCTL0) | SPIOEN);
2942 panic("Unknown REQINIT message type");
2946 ahc_clear_msg_state(ahc);
2947 ahc_outb(ahc, RETURN_1, EXIT_MSG_LOOP);
2949 ahc_outb(ahc, RETURN_1, CONT_MSG_LOOP);
2953 * See if we sent a particular extended message to the target.
2954 * If "full" is true, return true only if the target saw the full
2955 * message. If "full" is false, return true if the target saw at
2956 * least the first byte of the message.
2959 ahc_sent_msg(struct ahc_softc *ahc, ahc_msgtype type, u_int msgval, int full)
2967 while (index < ahc->msgout_len) {
2968 if (ahc->msgout_buf[index] == MSG_EXTENDED) {
2971 end_index = index + 1 + ahc->msgout_buf[index + 1];
2972 if (ahc->msgout_buf[index+2] == msgval
2973 && type == AHCMSG_EXT) {
2976 if (ahc->msgout_index > end_index)
2978 } else if (ahc->msgout_index > index)
2982 } else if (ahc->msgout_buf[index] >= MSG_SIMPLE_TASK
2983 && ahc->msgout_buf[index] <= MSG_IGN_WIDE_RESIDUE) {
2985 /* Skip tag type and tag id or residue param*/
2988 /* Single byte message */
2989 if (type == AHCMSG_1B
2990 && ahc->msgout_buf[index] == msgval
2991 && ahc->msgout_index > index)
3003 * Wait for a complete incoming message, parse it, and respond accordingly.
3006 ahc_parse_msg(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
3008 struct ahc_initiator_tinfo *tinfo;
3009 struct ahc_tmode_tstate *tstate;
3013 u_int targ_scsirate;
3015 done = MSGLOOP_IN_PROG;
3018 tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid,
3019 devinfo->target, &tstate);
3020 targ_scsirate = tinfo->scsirate;
3023 * Parse as much of the message as is available,
3024 * rejecting it if we don't support it. When
3025 * the entire message is available and has been
3026 * handled, return MSGLOOP_MSGCOMPLETE, indicating
3027 * that we have parsed an entire message.
3029 * In the case of extended messages, we accept the length
3030 * byte outright and perform more checking once we know the
3031 * extended message type.
3033 switch (ahc->msgin_buf[0]) {
3034 case MSG_DISCONNECT:
3035 case MSG_SAVEDATAPOINTER:
3036 case MSG_CMDCOMPLETE:
3037 case MSG_RESTOREPOINTERS:
3038 case MSG_IGN_WIDE_RESIDUE:
3040 * End our message loop as these are messages
3041 * the sequencer handles on its own.
3043 done = MSGLOOP_TERMINATED;
3045 case MSG_MESSAGE_REJECT:
3046 response = ahc_handle_msg_reject(ahc, devinfo);
3049 done = MSGLOOP_MSGCOMPLETE;
3053 /* Wait for enough of the message to begin validation */
3054 if (ahc->msgin_index < 2)
3056 switch (ahc->msgin_buf[2]) {
3059 struct ahc_syncrate *syncrate;
3065 if (ahc->msgin_buf[1] != MSG_EXT_SDTR_LEN) {
3071 * Wait until we have both args before validating
3072 * and acting on this message.
3074 * Add one to MSG_EXT_SDTR_LEN to account for
3075 * the extended message preamble.
3077 if (ahc->msgin_index < (MSG_EXT_SDTR_LEN + 1))
3080 period = ahc->msgin_buf[3];
3082 saved_offset = offset = ahc->msgin_buf[4];
3083 syncrate = ahc_devlimited_syncrate(ahc, tinfo, &period,
3086 ahc_validate_offset(ahc, tinfo, syncrate, &offset,
3087 targ_scsirate & WIDEXFER,
3090 printf("(%s:%c:%d:%d): Received "
3091 "SDTR period %x, offset %x\n\t"
3092 "Filtered to period %x, offset %x\n",
3093 ahc_name(ahc), devinfo->channel,
3094 devinfo->target, devinfo->lun,
3095 ahc->msgin_buf[3], saved_offset,
3098 ahc_set_syncrate(ahc, devinfo,
3100 offset, ppr_options,
3101 AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
3105 * See if we initiated Sync Negotiation
3106 * and didn't have to fall down to async
3109 if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_SDTR, TRUE)) {
3111 if (saved_offset != offset) {
3112 /* Went too low - force async */
3117 * Send our own SDTR in reply
3120 && devinfo->role == ROLE_INITIATOR) {
3121 printf("(%s:%c:%d:%d): Target "
3123 ahc_name(ahc), devinfo->channel,
3124 devinfo->target, devinfo->lun);
3126 ahc->msgout_index = 0;
3127 ahc->msgout_len = 0;
3128 ahc_construct_sdtr(ahc, devinfo,
3130 ahc->msgout_index = 0;
3133 done = MSGLOOP_MSGCOMPLETE;
3140 u_int sending_reply;
3142 sending_reply = FALSE;
3143 if (ahc->msgin_buf[1] != MSG_EXT_WDTR_LEN) {
3149 * Wait until we have our arg before validating
3150 * and acting on this message.
3152 * Add one to MSG_EXT_WDTR_LEN to account for
3153 * the extended message preamble.
3155 if (ahc->msgin_index < (MSG_EXT_WDTR_LEN + 1))
3158 bus_width = ahc->msgin_buf[3];
3159 saved_width = bus_width;
3160 ahc_validate_width(ahc, tinfo, &bus_width,
3163 printf("(%s:%c:%d:%d): Received WDTR "
3164 "%x filtered to %x\n",
3165 ahc_name(ahc), devinfo->channel,
3166 devinfo->target, devinfo->lun,
3167 saved_width, bus_width);
3170 if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_WDTR, TRUE)) {
3172 * Don't send a WDTR back to the
3173 * target, since we asked first.
3174 * If the width went higher than our
3175 * request, reject it.
3177 if (saved_width > bus_width) {
3179 printf("(%s:%c:%d:%d): requested %dBit "
3180 "transfers. Rejecting...\n",
3181 ahc_name(ahc), devinfo->channel,
3182 devinfo->target, devinfo->lun,
3183 8 * (0x01 << bus_width));
3188 * Send our own WDTR in reply
3191 && devinfo->role == ROLE_INITIATOR) {
3192 printf("(%s:%c:%d:%d): Target "
3194 ahc_name(ahc), devinfo->channel,
3195 devinfo->target, devinfo->lun);
3197 ahc->msgout_index = 0;
3198 ahc->msgout_len = 0;
3199 ahc_construct_wdtr(ahc, devinfo, bus_width);
3200 ahc->msgout_index = 0;
3202 sending_reply = TRUE;
3205 * After a wide message, we are async, but
3206 * some devices don't seem to honor this portion
3207 * of the spec. Force a renegotiation of the
3208 * sync component of our transfer agreement even
3209 * if our goal is async. By updating our width
3210 * after forcing the negotiation, we avoid
3211 * renegotiating for width.
3213 ahc_update_neg_request(ahc, devinfo, tstate,
3214 tinfo, AHC_NEG_ALWAYS);
3215 ahc_set_width(ahc, devinfo, bus_width,
3216 AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
3218 if (sending_reply == FALSE && reject == FALSE) {
3221 * We will always have an SDTR to send.
3223 ahc->msgout_index = 0;
3224 ahc->msgout_len = 0;
3225 ahc_build_transfer_msg(ahc, devinfo);
3226 ahc->msgout_index = 0;
3229 done = MSGLOOP_MSGCOMPLETE;
3234 struct ahc_syncrate *syncrate;
3241 u_int saved_ppr_options;
3243 if (ahc->msgin_buf[1] != MSG_EXT_PPR_LEN) {
3249 * Wait until we have all args before validating
3250 * and acting on this message.
3252 * Add one to MSG_EXT_PPR_LEN to account for
3253 * the extended message preamble.
3255 if (ahc->msgin_index < (MSG_EXT_PPR_LEN + 1))
3258 period = ahc->msgin_buf[3];
3259 offset = ahc->msgin_buf[5];
3260 bus_width = ahc->msgin_buf[6];
3261 saved_width = bus_width;
3262 ppr_options = ahc->msgin_buf[7];
3264 * According to the spec, a DT only
3265 * period factor with no DT option
3266 * set implies async.
3268 if ((ppr_options & MSG_EXT_PPR_DT_REQ) == 0
3271 saved_ppr_options = ppr_options;
3272 saved_offset = offset;
3275 * Mask out any options we don't support
3276 * on any controller. Transfer options are
3277 * only available if we are negotiating wide.
3279 ppr_options &= MSG_EXT_PPR_DT_REQ;
3283 ahc_validate_width(ahc, tinfo, &bus_width,
3285 syncrate = ahc_devlimited_syncrate(ahc, tinfo, &period,
3288 ahc_validate_offset(ahc, tinfo, syncrate,
3292 if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_PPR, TRUE)) {
3294 * If we are unable to do any of the
3295 * requested options (we went too low),
3296 * then we'll have to reject the message.
3298 if (saved_width > bus_width
3299 || saved_offset != offset
3300 || saved_ppr_options != ppr_options) {
3309 if (devinfo->role != ROLE_TARGET)
3310 printf("(%s:%c:%d:%d): Target "
3312 ahc_name(ahc), devinfo->channel,
3313 devinfo->target, devinfo->lun);
3315 printf("(%s:%c:%d:%d): Initiator "
3317 ahc_name(ahc), devinfo->channel,
3318 devinfo->target, devinfo->lun);
3319 ahc->msgout_index = 0;
3320 ahc->msgout_len = 0;
3321 ahc_construct_ppr(ahc, devinfo, period, offset,
3322 bus_width, ppr_options);
3323 ahc->msgout_index = 0;
3327 printf("(%s:%c:%d:%d): Received PPR width %x, "
3328 "period %x, offset %x,options %x\n"
3329 "\tFiltered to width %x, period %x, "
3330 "offset %x, options %x\n",
3331 ahc_name(ahc), devinfo->channel,
3332 devinfo->target, devinfo->lun,
3333 saved_width, ahc->msgin_buf[3],
3334 saved_offset, saved_ppr_options,
3335 bus_width, period, offset, ppr_options);
3337 ahc_set_width(ahc, devinfo, bus_width,
3338 AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
3340 ahc_set_syncrate(ahc, devinfo,
3342 offset, ppr_options,
3343 AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
3345 done = MSGLOOP_MSGCOMPLETE;
3349 /* Unknown extended message. Reject it. */
3355 #ifdef AHC_TARGET_MODE
3356 case MSG_BUS_DEV_RESET:
3357 ahc_handle_devreset(ahc, devinfo,
3359 "Bus Device Reset Received",
3360 /*verbose_level*/0);
3362 done = MSGLOOP_TERMINATED;
3366 case MSG_CLEAR_QUEUE:
3370 /* Target mode messages */
3371 if (devinfo->role != ROLE_TARGET) {
3375 tag = SCB_LIST_NULL;
3376 if (ahc->msgin_buf[0] == MSG_ABORT_TAG)
3377 tag = ahc_inb(ahc, INITIATOR_TAG);
3378 ahc_abort_scbs(ahc, devinfo->target, devinfo->channel,
3379 devinfo->lun, tag, ROLE_TARGET,
3382 tstate = ahc->enabled_targets[devinfo->our_scsiid];
3383 if (tstate != NULL) {
3384 struct ahc_tmode_lstate* lstate;
3386 lstate = tstate->enabled_luns[devinfo->lun];
3387 if (lstate != NULL) {
3388 ahc_queue_lstate_event(ahc, lstate,
3389 devinfo->our_scsiid,
3392 ahc_send_lstate_events(ahc, lstate);
3396 done = MSGLOOP_TERMINATED;
3400 case MSG_TERM_IO_PROC:
3408 * Setup to reject the message.
3410 ahc->msgout_index = 0;
3411 ahc->msgout_len = 1;
3412 ahc->msgout_buf[0] = MSG_MESSAGE_REJECT;
3413 done = MSGLOOP_MSGCOMPLETE;
3417 if (done != MSGLOOP_IN_PROG && !response)
3418 /* Clear the outgoing message buffer */
3419 ahc->msgout_len = 0;
3425 * Process a message reject message.
3428 ahc_handle_msg_reject(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
3431 * What we care about here is if we had an
3432 * outstanding SDTR or WDTR message for this
3433 * target. If we did, this is a signal that
3434 * the target is refusing negotiation.
3437 struct ahc_initiator_tinfo *tinfo;
3438 struct ahc_tmode_tstate *tstate;
3443 scb_index = ahc_inb(ahc, SCB_TAG);
3444 scb = ahc_lookup_scb(ahc, scb_index);
3445 tinfo = ahc_fetch_transinfo(ahc, devinfo->channel,
3446 devinfo->our_scsiid,
3447 devinfo->target, &tstate);
3448 /* Might be necessary */
3449 last_msg = ahc_inb(ahc, LAST_MSG);
3451 if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_PPR, /*full*/FALSE)) {
3453 * Target does not support the PPR message.
3454 * Attempt to negotiate SPI-2 style.
3457 printf("(%s:%c:%d:%d): PPR Rejected. "
3458 "Trying WDTR/SDTR\n",
3459 ahc_name(ahc), devinfo->channel,
3460 devinfo->target, devinfo->lun);
3462 tinfo->goal.ppr_options = 0;
3463 tinfo->curr.transport_version = 2;
3464 tinfo->goal.transport_version = 2;
3465 ahc->msgout_index = 0;
3466 ahc->msgout_len = 0;
3467 ahc_build_transfer_msg(ahc, devinfo);
3468 ahc->msgout_index = 0;
3470 } else if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_WDTR, /*full*/FALSE)) {
3472 /* note 8bit xfers */
3473 printf("(%s:%c:%d:%d): refuses WIDE negotiation. Using "
3474 "8bit transfers\n", ahc_name(ahc),
3475 devinfo->channel, devinfo->target, devinfo->lun);
3476 ahc_set_width(ahc, devinfo, MSG_EXT_WDTR_BUS_8_BIT,
3477 AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
3480 * No need to clear the sync rate. If the target
3481 * did not accept the command, our syncrate is
3482 * unaffected. If the target started the negotiation,
3483 * but rejected our response, we already cleared the
3484 * sync rate before sending our WDTR.
3486 if (tinfo->goal.offset != tinfo->curr.offset) {
3488 /* Start the sync negotiation */
3489 ahc->msgout_index = 0;
3490 ahc->msgout_len = 0;
3491 ahc_build_transfer_msg(ahc, devinfo);
3492 ahc->msgout_index = 0;
3495 } else if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_SDTR, /*full*/FALSE)) {
3496 /* note asynch xfers and clear flag */
3497 ahc_set_syncrate(ahc, devinfo, /*syncrate*/NULL, /*period*/0,
3498 /*offset*/0, /*ppr_options*/0,
3499 AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
3501 printf("(%s:%c:%d:%d): refuses synchronous negotiation. "
3502 "Using asynchronous transfers\n",
3503 ahc_name(ahc), devinfo->channel,
3504 devinfo->target, devinfo->lun);
3505 } else if ((scb->hscb->control & MSG_SIMPLE_TASK) != 0) {
3509 tag_type = (scb->hscb->control & MSG_SIMPLE_TASK);
3511 if (tag_type == MSG_SIMPLE_TASK) {
3512 printf("(%s:%c:%d:%d): refuses tagged commands. "
3513 "Performing non-tagged I/O\n", ahc_name(ahc),
3514 devinfo->channel, devinfo->target, devinfo->lun);
3515 ahc_set_tags(ahc, scb->io_ctx, devinfo, AHC_QUEUE_NONE);
3518 printf("(%s:%c:%d:%d): refuses %s tagged commands. "
3519 "Performing simple queue tagged I/O only\n",
3520 ahc_name(ahc), devinfo->channel, devinfo->target,
3521 devinfo->lun, tag_type == MSG_ORDERED_TASK
3522 ? "ordered" : "head of queue");
3523 ahc_set_tags(ahc, scb->io_ctx, devinfo, AHC_QUEUE_BASIC);
3528 * Resend the identify for this CCB as the target
3529 * may believe that the selection is invalid otherwise.
3531 ahc_outb(ahc, SCB_CONTROL,
3532 ahc_inb(ahc, SCB_CONTROL) & mask);
3533 scb->hscb->control &= mask;
3534 ahc_set_transaction_tag(scb, /*enabled*/FALSE,
3535 /*type*/MSG_SIMPLE_TASK);
3536 ahc_outb(ahc, MSG_OUT, MSG_IDENTIFYFLAG);
3537 ahc_assert_atn(ahc);
3540 * This transaction is now at the head of
3541 * the untagged queue for this target.
3543 if ((ahc->flags & AHC_SCB_BTT) == 0) {
3544 struct scb_tailq *untagged_q;
3547 &(ahc->untagged_queues[devinfo->target_offset]);
3548 TAILQ_INSERT_HEAD(untagged_q, scb, links.tqe);
3549 scb->flags |= SCB_UNTAGGEDQ;
3551 ahc_busy_tcl(ahc, BUILD_TCL(scb->hscb->scsiid, devinfo->lun),
3555 * Requeue all tagged commands for this target
3556 * currently in our posession so they can be
3557 * converted to untagged commands.
3559 ahc_search_qinfifo(ahc, SCB_GET_TARGET(ahc, scb),
3560 SCB_GET_CHANNEL(ahc, scb),
3561 SCB_GET_LUN(scb), /*tag*/SCB_LIST_NULL,
3562 ROLE_INITIATOR, CAM_REQUEUE_REQ,
3566 * Otherwise, we ignore it.
3568 printf("%s:%c:%d: Message reject for %x -- ignored\n",
3569 ahc_name(ahc), devinfo->channel, devinfo->target,
3576 * Process an ingnore wide residue message.
3579 ahc_handle_ign_wide_residue(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
3584 scb_index = ahc_inb(ahc, SCB_TAG);
3585 scb = ahc_lookup_scb(ahc, scb_index);
3587 * XXX Actually check data direction in the sequencer?
3588 * Perhaps add datadir to some spare bits in the hscb?
3590 if ((ahc_inb(ahc, SEQ_FLAGS) & DPHASE) == 0
3591 || ahc_get_transfer_dir(scb) != CAM_DIR_IN) {
3593 * Ignore the message if we haven't
3594 * seen an appropriate data phase yet.
3598 * If the residual occurred on the last
3599 * transfer and the transfer request was
3600 * expected to end on an odd count, do
3601 * nothing. Otherwise, subtract a byte
3602 * and update the residual count accordingly.
3606 sgptr = ahc_inb(ahc, SCB_RESIDUAL_SGPTR);
3607 if ((sgptr & SG_LIST_NULL) != 0
3608 && (ahc_inb(ahc, SCB_LUN) & SCB_XFERLEN_ODD) != 0) {
3610 * If the residual occurred on the last
3611 * transfer and the transfer request was
3612 * expected to end on an odd count, do
3616 struct ahc_dma_seg *sg;
3621 /* Pull in all of the sgptr */
3622 sgptr = ahc_inl(ahc, SCB_RESIDUAL_SGPTR);
3623 data_cnt = ahc_inl(ahc, SCB_RESIDUAL_DATACNT);
3625 if ((sgptr & SG_LIST_NULL) != 0) {
3627 * The residual data count is not updated
3628 * for the command run to completion case.
3629 * Explicitly zero the count.
3631 data_cnt &= ~AHC_SG_LEN_MASK;
3634 data_addr = ahc_inl(ahc, SHADDR);
3638 sgptr &= SG_PTR_MASK;
3640 sg = ahc_sg_bus_to_virt(scb, sgptr);
3643 * The residual sg ptr points to the next S/G
3644 * to load so we must go back one.
3647 sglen = ahc_le32toh(sg->len) & AHC_SG_LEN_MASK;
3648 if (sg != scb->sg_list
3649 && sglen < (data_cnt & AHC_SG_LEN_MASK)) {
3652 sglen = ahc_le32toh(sg->len);
3654 * Preserve High Address and SG_LIST bits
3655 * while setting the count to 1.
3657 data_cnt = 1 | (sglen & (~AHC_SG_LEN_MASK));
3658 data_addr = ahc_le32toh(sg->addr)
3659 + (sglen & AHC_SG_LEN_MASK) - 1;
3662 * Increment sg so it points to the
3666 sgptr = ahc_sg_virt_to_bus(scb, sg);
3668 ahc_outl(ahc, SCB_RESIDUAL_SGPTR, sgptr);
3669 ahc_outl(ahc, SCB_RESIDUAL_DATACNT, data_cnt);
3671 * Toggle the "oddness" of the transfer length
3672 * to handle this mid-transfer ignore wide
3673 * residue. This ensures that the oddness is
3674 * correct for subsequent data transfers.
3676 ahc_outb(ahc, SCB_LUN,
3677 ahc_inb(ahc, SCB_LUN) ^ SCB_XFERLEN_ODD);
3684 * Reinitialize the data pointers for the active transfer
3685 * based on its current residual.
3688 ahc_reinitialize_dataptrs(struct ahc_softc *ahc)
3691 struct ahc_dma_seg *sg;
3697 scb_index = ahc_inb(ahc, SCB_TAG);
3698 scb = ahc_lookup_scb(ahc, scb_index);
3699 sgptr = (ahc_inb(ahc, SCB_RESIDUAL_SGPTR + 3) << 24)
3700 | (ahc_inb(ahc, SCB_RESIDUAL_SGPTR + 2) << 16)
3701 | (ahc_inb(ahc, SCB_RESIDUAL_SGPTR + 1) << 8)
3702 | ahc_inb(ahc, SCB_RESIDUAL_SGPTR);
3704 sgptr &= SG_PTR_MASK;
3705 sg = ahc_sg_bus_to_virt(scb, sgptr);
3707 /* The residual sg_ptr always points to the next sg */
3710 resid = (ahc_inb(ahc, SCB_RESIDUAL_DATACNT + 2) << 16)
3711 | (ahc_inb(ahc, SCB_RESIDUAL_DATACNT + 1) << 8)
3712 | ahc_inb(ahc, SCB_RESIDUAL_DATACNT);
3714 dataptr = ahc_le32toh(sg->addr)
3715 + (ahc_le32toh(sg->len) & AHC_SG_LEN_MASK)
3717 if ((ahc->flags & AHC_39BIT_ADDRESSING) != 0) {
3720 dscommand1 = ahc_inb(ahc, DSCOMMAND1);
3721 ahc_outb(ahc, DSCOMMAND1, dscommand1 | HADDLDSEL0);
3722 ahc_outb(ahc, HADDR,
3723 (ahc_le32toh(sg->len) >> 24) & SG_HIGH_ADDR_BITS);
3724 ahc_outb(ahc, DSCOMMAND1, dscommand1);
3726 ahc_outb(ahc, HADDR + 3, dataptr >> 24);
3727 ahc_outb(ahc, HADDR + 2, dataptr >> 16);
3728 ahc_outb(ahc, HADDR + 1, dataptr >> 8);
3729 ahc_outb(ahc, HADDR, dataptr);
3730 ahc_outb(ahc, HCNT + 2, resid >> 16);
3731 ahc_outb(ahc, HCNT + 1, resid >> 8);
3732 ahc_outb(ahc, HCNT, resid);
3733 if ((ahc->features & AHC_ULTRA2) == 0) {
3734 ahc_outb(ahc, STCNT + 2, resid >> 16);
3735 ahc_outb(ahc, STCNT + 1, resid >> 8);
3736 ahc_outb(ahc, STCNT, resid);
3741 * Handle the effects of issuing a bus device reset message.
3744 ahc_handle_devreset(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
3745 cam_status status, char *message, int verbose_level)
3747 #ifdef AHC_TARGET_MODE
3748 struct ahc_tmode_tstate* tstate;
3753 found = ahc_abort_scbs(ahc, devinfo->target, devinfo->channel,
3754 CAM_LUN_WILDCARD, SCB_LIST_NULL, devinfo->role,
3757 #ifdef AHC_TARGET_MODE
3759 * Send an immediate notify ccb to all target mord peripheral
3760 * drivers affected by this action.
3762 tstate = ahc->enabled_targets[devinfo->our_scsiid];
3763 if (tstate != NULL) {
3764 for (lun = 0; lun < AHC_NUM_LUNS; lun++) {
3765 struct ahc_tmode_lstate* lstate;
3767 lstate = tstate->enabled_luns[lun];
3771 ahc_queue_lstate_event(ahc, lstate, devinfo->our_scsiid,
3772 MSG_BUS_DEV_RESET, /*arg*/0);
3773 ahc_send_lstate_events(ahc, lstate);
3779 * Go back to async/narrow transfers and renegotiate.
3781 ahc_set_width(ahc, devinfo, MSG_EXT_WDTR_BUS_8_BIT,
3782 AHC_TRANS_CUR, /*paused*/TRUE);
3783 ahc_set_syncrate(ahc, devinfo, /*syncrate*/NULL,
3784 /*period*/0, /*offset*/0, /*ppr_options*/0,
3785 AHC_TRANS_CUR, /*paused*/TRUE);
3787 if (status != CAM_SEL_TIMEOUT)
3788 ahc_send_async(ahc, devinfo->channel, devinfo->target,
3789 CAM_LUN_WILDCARD, AC_SENT_BDR);
3792 && (verbose_level <= bootverbose))
3793 printf("%s: %s on %c:%d. %d SCBs aborted\n", ahc_name(ahc),
3794 message, devinfo->channel, devinfo->target, found);
3797 #ifdef AHC_TARGET_MODE
3799 ahc_setup_target_msgin(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
3804 * To facilitate adding multiple messages together,
3805 * each routine should increment the index and len
3806 * variables instead of setting them explicitly.
3808 ahc->msgout_index = 0;
3809 ahc->msgout_len = 0;
3811 if (scb != NULL && (scb->flags & SCB_AUTO_NEGOTIATE) != 0)
3812 ahc_build_transfer_msg(ahc, devinfo);
3814 panic("ahc_intr: AWAITING target message with no message");
3816 ahc->msgout_index = 0;
3817 ahc->msg_type = MSG_TYPE_TARGET_MSGIN;
3820 /**************************** Initialization **********************************/
3822 * Allocate a controller structure for a new device
3823 * and perform initial initializion.
3826 ahc_alloc(void *platform_arg, char *name)
3828 struct ahc_softc *ahc;
3832 ahc = malloc(sizeof(*ahc), M_DEVBUF, M_NOWAIT);
3834 printf("aic7xxx: cannot malloc softc!\n");
3835 free(name, M_DEVBUF);
3839 ahc = device_get_softc((device_t)platform_arg);
3841 memset(ahc, 0, sizeof(*ahc));
3842 ahc->seep_config = malloc(sizeof(*ahc->seep_config),
3843 M_DEVBUF, M_NOWAIT);
3844 if (ahc->seep_config == NULL) {
3846 free(ahc, M_DEVBUF);
3848 free(name, M_DEVBUF);
3851 LIST_INIT(&ahc->pending_scbs);
3852 /* We don't know our unit number until the OSM sets it */
3855 ahc->description = NULL;
3857 ahc->channel_b = 'B';
3858 ahc->chip = AHC_NONE;
3859 ahc->features = AHC_FENONE;
3860 ahc->bugs = AHC_BUGNONE;
3861 ahc->flags = AHC_FNONE;
3863 * Default to all error reporting enabled with the
3864 * sequencer operating at its fastest speed.
3865 * The bus attach code may modify this.
3867 ahc->seqctl = FASTMODE;
3869 for (i = 0; i < AHC_NUM_TARGETS; i++)
3870 TAILQ_INIT(&ahc->untagged_queues[i]);
3871 if (ahc_platform_alloc(ahc, platform_arg) != 0) {
3879 ahc_softc_init(struct ahc_softc *ahc)
3882 /* The IRQMS bit is only valid on VL and EISA chips */
3883 if ((ahc->chip & AHC_PCI) == 0)
3884 ahc->unpause = ahc_inb(ahc, HCNTRL) & IRQMS;
3887 ahc->pause = ahc->unpause | PAUSE;
3888 /* XXX The shared scb data stuff should be deprecated */
3889 if (ahc->scb_data == NULL) {
3890 ahc->scb_data = malloc(sizeof(*ahc->scb_data),
3891 M_DEVBUF, M_NOWAIT);
3892 if (ahc->scb_data == NULL)
3894 memset(ahc->scb_data, 0, sizeof(*ahc->scb_data));
3901 ahc_set_unit(struct ahc_softc *ahc, int unit)
3907 ahc_set_name(struct ahc_softc *ahc, char *name)
3909 if (ahc->name != NULL)
3910 free(ahc->name, M_DEVBUF);
3915 ahc_free(struct ahc_softc *ahc)
3919 switch (ahc->init_level) {
3925 ahc_dmamap_unload(ahc, ahc->shared_data_dmat,
3926 ahc->shared_data_dmamap);
3929 ahc_dmamem_free(ahc, ahc->shared_data_dmat, ahc->qoutfifo,
3930 ahc->shared_data_dmamap);
3931 ahc_dmamap_destroy(ahc, ahc->shared_data_dmat,
3932 ahc->shared_data_dmamap);
3935 ahc_dma_tag_destroy(ahc, ahc->shared_data_dmat);
3938 ahc_dma_tag_destroy(ahc, ahc->buffer_dmat);
3946 ahc_dma_tag_destroy(ahc, ahc->parent_dmat);
3948 ahc_platform_free(ahc);
3949 ahc_fini_scbdata(ahc);
3950 for (i = 0; i < AHC_NUM_TARGETS; i++) {
3951 struct ahc_tmode_tstate *tstate;
3953 tstate = ahc->enabled_targets[i];
3954 if (tstate != NULL) {
3955 #ifdef AHC_TARGET_MODE
3958 for (j = 0; j < AHC_NUM_LUNS; j++) {
3959 struct ahc_tmode_lstate *lstate;
3961 lstate = tstate->enabled_luns[j];
3962 if (lstate != NULL) {
3963 xpt_free_path(lstate->path);
3964 free(lstate, M_DEVBUF);
3968 free(tstate, M_DEVBUF);
3971 #ifdef AHC_TARGET_MODE
3972 if (ahc->black_hole != NULL) {
3973 xpt_free_path(ahc->black_hole->path);
3974 free(ahc->black_hole, M_DEVBUF);
3977 if (ahc->name != NULL)
3978 free(ahc->name, M_DEVBUF);
3979 if (ahc->seep_config != NULL)
3980 free(ahc->seep_config, M_DEVBUF);
3982 free(ahc, M_DEVBUF);
3988 ahc_shutdown(void *arg)
3990 struct ahc_softc *ahc;
3993 ahc = (struct ahc_softc *)arg;
3995 /* This will reset most registers to 0, but not all */
3996 ahc_reset(ahc, /*reinit*/FALSE);
3997 ahc_outb(ahc, SCSISEQ, 0);
3998 ahc_outb(ahc, SXFRCTL0, 0);
3999 ahc_outb(ahc, DSPCISTATUS, 0);
4001 for (i = TARG_SCSIRATE; i < SCSICONF; i++)
4002 ahc_outb(ahc, i, 0);
4006 * Reset the controller and record some information about it
4007 * that is only available just after a reset. If "reinit" is
4008 * non-zero, this reset occured after initial configuration
4009 * and the caller requests that the chip be fully reinitialized
4010 * to a runable state. Chip interrupts are *not* enabled after
4011 * a reinitialization. The caller must enable interrupts via
4012 * ahc_intr_enable().
4015 ahc_reset(struct ahc_softc *ahc, int reinit)
4018 u_int sxfrctl1_a, sxfrctl1_b;
4023 * Preserve the value of the SXFRCTL1 register for all channels.
4024 * It contains settings that affect termination and we don't want
4025 * to disturb the integrity of the bus.
4029 if ((ahc->chip & AHC_CHIPID_MASK) == AHC_AIC7770) {
4033 * Save channel B's settings in case this chip
4034 * is setup for TWIN channel operation.
4036 sblkctl = ahc_inb(ahc, SBLKCTL);
4037 ahc_outb(ahc, SBLKCTL, sblkctl | SELBUSB);
4038 sxfrctl1_b = ahc_inb(ahc, SXFRCTL1);
4039 ahc_outb(ahc, SBLKCTL, sblkctl & ~SELBUSB);
4041 sxfrctl1_a = ahc_inb(ahc, SXFRCTL1);
4043 ahc_outb(ahc, HCNTRL, CHIPRST | ahc->pause);
4046 * Ensure that the reset has finished. We delay 1000us
4047 * prior to reading the register to make sure the chip
4048 * has sufficiently completed its reset to handle register
4054 } while (--wait && !(ahc_inb(ahc, HCNTRL) & CHIPRSTACK));
4057 printf("%s: WARNING - Failed chip reset! "
4058 "Trying to initialize anyway.\n", ahc_name(ahc));
4060 ahc_outb(ahc, HCNTRL, ahc->pause);
4062 /* Determine channel configuration */
4063 sblkctl = ahc_inb(ahc, SBLKCTL) & (SELBUSB|SELWIDE);
4064 /* No Twin Channel PCI cards */
4065 if ((ahc->chip & AHC_PCI) != 0)
4066 sblkctl &= ~SELBUSB;
4069 /* Single Narrow Channel */
4073 ahc->features |= AHC_WIDE;
4077 ahc->features |= AHC_TWIN;
4080 printf(" Unsupported adapter type. Ignoring\n");
4087 * We must always initialize STPWEN to 1 before we
4088 * restore the saved values. STPWEN is initialized
4089 * to a tri-state condition which can only be cleared
4092 if ((ahc->features & AHC_TWIN) != 0) {
4095 sblkctl = ahc_inb(ahc, SBLKCTL);
4096 ahc_outb(ahc, SBLKCTL, sblkctl | SELBUSB);
4097 ahc_outb(ahc, SXFRCTL1, sxfrctl1_b);
4098 ahc_outb(ahc, SBLKCTL, sblkctl & ~SELBUSB);
4100 ahc_outb(ahc, SXFRCTL1, sxfrctl1_a);
4105 * If a recovery action has forced a chip reset,
4106 * re-initialize the chip to our liking.
4108 error = ahc->bus_chip_init(ahc);
4118 * Determine the number of SCBs available on the controller
4121 ahc_probe_scbs(struct ahc_softc *ahc) {
4124 for (i = 0; i < AHC_SCB_MAX; i++) {
4126 ahc_outb(ahc, SCBPTR, i);
4127 ahc_outb(ahc, SCB_BASE, i);
4128 if (ahc_inb(ahc, SCB_BASE) != i)
4130 ahc_outb(ahc, SCBPTR, 0);
4131 if (ahc_inb(ahc, SCB_BASE) != 0)
4138 ahc_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
4142 baddr = (dma_addr_t *)arg;
4143 *baddr = segs->ds_addr;
4147 ahc_build_free_scb_list(struct ahc_softc *ahc)
4153 if ((ahc->flags & AHC_LSCBS_ENABLED) != 0)
4156 for (i = 0; i < ahc->scb_data->maxhscbs; i++) {
4159 ahc_outb(ahc, SCBPTR, i);
4162 * Touch all SCB bytes to avoid parity errors
4163 * should one of our debugging routines read
4164 * an otherwise uninitiatlized byte.
4166 for (j = 0; j < scbsize; j++)
4167 ahc_outb(ahc, SCB_BASE+j, 0xFF);
4169 /* Clear the control byte. */
4170 ahc_outb(ahc, SCB_CONTROL, 0);
4172 /* Set the next pointer */
4173 if ((ahc->flags & AHC_PAGESCBS) != 0)
4174 ahc_outb(ahc, SCB_NEXT, i+1);
4176 ahc_outb(ahc, SCB_NEXT, SCB_LIST_NULL);
4178 /* Make the tag number, SCSIID, and lun invalid */
4179 ahc_outb(ahc, SCB_TAG, SCB_LIST_NULL);
4180 ahc_outb(ahc, SCB_SCSIID, 0xFF);
4181 ahc_outb(ahc, SCB_LUN, 0xFF);
4184 if ((ahc->flags & AHC_PAGESCBS) != 0) {
4185 /* SCB 0 heads the free list. */
4186 ahc_outb(ahc, FREE_SCBH, 0);
4189 ahc_outb(ahc, FREE_SCBH, SCB_LIST_NULL);
4192 /* Make sure that the last SCB terminates the free list */
4193 ahc_outb(ahc, SCBPTR, i-1);
4194 ahc_outb(ahc, SCB_NEXT, SCB_LIST_NULL);
4198 ahc_init_scbdata(struct ahc_softc *ahc)
4200 struct scb_data *scb_data;
4202 scb_data = ahc->scb_data;
4203 SLIST_INIT(&scb_data->free_scbs);
4204 SLIST_INIT(&scb_data->sg_maps);
4206 /* Allocate SCB resources */
4207 scb_data->scbarray =
4208 (struct scb *)malloc(sizeof(struct scb) * AHC_SCB_MAX_ALLOC,
4209 M_DEVBUF, M_NOWAIT);
4210 if (scb_data->scbarray == NULL)
4212 memset(scb_data->scbarray, 0, sizeof(struct scb) * AHC_SCB_MAX_ALLOC);
4214 /* Determine the number of hardware SCBs and initialize them */
4216 scb_data->maxhscbs = ahc_probe_scbs(ahc);
4217 if (ahc->scb_data->maxhscbs == 0) {
4218 printf("%s: No SCB space found\n", ahc_name(ahc));
4223 * Create our DMA tags. These tags define the kinds of device
4224 * accessible memory allocations and memory mappings we will
4225 * need to perform during normal operation.
4227 * Unless we need to further restrict the allocation, we rely
4228 * on the restrictions of the parent dmat, hence the common
4229 * use of MAXADDR and MAXSIZE.
4232 /* DMA tag for our hardware scb structures */
4233 if (ahc_dma_tag_create(ahc, ahc->parent_dmat, /*alignment*/1,
4234 /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
4235 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
4236 /*highaddr*/BUS_SPACE_MAXADDR,
4237 /*filter*/NULL, /*filterarg*/NULL,
4238 AHC_SCB_MAX_ALLOC * sizeof(struct hardware_scb),
4240 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
4241 /*flags*/0, &scb_data->hscb_dmat) != 0) {
4245 scb_data->init_level++;
4247 /* Allocation for our hscbs */
4248 if (ahc_dmamem_alloc(ahc, scb_data->hscb_dmat,
4249 (void **)&scb_data->hscbs,
4250 BUS_DMA_NOWAIT, &scb_data->hscb_dmamap) != 0) {
4254 scb_data->init_level++;
4256 /* And permanently map them */
4257 ahc_dmamap_load(ahc, scb_data->hscb_dmat, scb_data->hscb_dmamap,
4259 AHC_SCB_MAX_ALLOC * sizeof(struct hardware_scb),
4260 ahc_dmamap_cb, &scb_data->hscb_busaddr, /*flags*/0);
4262 scb_data->init_level++;
4264 /* DMA tag for our sense buffers */
4265 if (ahc_dma_tag_create(ahc, ahc->parent_dmat, /*alignment*/1,
4266 /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
4267 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
4268 /*highaddr*/BUS_SPACE_MAXADDR,
4269 /*filter*/NULL, /*filterarg*/NULL,
4270 AHC_SCB_MAX_ALLOC * sizeof(struct scsi_sense_data),
4272 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
4273 /*flags*/0, &scb_data->sense_dmat) != 0) {
4277 scb_data->init_level++;
4280 if (ahc_dmamem_alloc(ahc, scb_data->sense_dmat,
4281 (void **)&scb_data->sense,
4282 BUS_DMA_NOWAIT, &scb_data->sense_dmamap) != 0) {
4286 scb_data->init_level++;
4288 /* And permanently map them */
4289 ahc_dmamap_load(ahc, scb_data->sense_dmat, scb_data->sense_dmamap,
4291 AHC_SCB_MAX_ALLOC * sizeof(struct scsi_sense_data),
4292 ahc_dmamap_cb, &scb_data->sense_busaddr, /*flags*/0);
4294 scb_data->init_level++;
4296 /* DMA tag for our S/G structures. We allocate in page sized chunks */
4297 if (ahc_dma_tag_create(ahc, ahc->parent_dmat, /*alignment*/8,
4298 /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
4299 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
4300 /*highaddr*/BUS_SPACE_MAXADDR,
4301 /*filter*/NULL, /*filterarg*/NULL,
4302 PAGE_SIZE, /*nsegments*/1,
4303 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
4304 /*flags*/0, &scb_data->sg_dmat) != 0) {
4308 scb_data->init_level++;
4310 /* Perform initial CCB allocation */
4311 memset(scb_data->hscbs, 0,
4312 AHC_SCB_MAX_ALLOC * sizeof(struct hardware_scb));
4313 ahc_alloc_scbs(ahc);
4315 if (scb_data->numscbs == 0) {
4316 printf("%s: ahc_init_scbdata - "
4317 "Unable to allocate initial scbs\n",
4323 * Reserve the next queued SCB.
4325 ahc->next_queued_scb = ahc_get_scb(ahc);
4328 * Note that we were successfull
4338 ahc_fini_scbdata(struct ahc_softc *ahc)
4340 struct scb_data *scb_data;
4342 scb_data = ahc->scb_data;
4343 if (scb_data == NULL)
4346 switch (scb_data->init_level) {
4350 struct sg_map_node *sg_map;
4352 while ((sg_map = SLIST_FIRST(&scb_data->sg_maps))!= NULL) {
4353 SLIST_REMOVE_HEAD(&scb_data->sg_maps, links);
4354 ahc_dmamap_unload(ahc, scb_data->sg_dmat,
4356 ahc_dmamem_free(ahc, scb_data->sg_dmat,
4359 free(sg_map, M_DEVBUF);
4361 ahc_dma_tag_destroy(ahc, scb_data->sg_dmat);
4364 ahc_dmamap_unload(ahc, scb_data->sense_dmat,
4365 scb_data->sense_dmamap);
4367 ahc_dmamem_free(ahc, scb_data->sense_dmat, scb_data->sense,
4368 scb_data->sense_dmamap);
4369 ahc_dmamap_destroy(ahc, scb_data->sense_dmat,
4370 scb_data->sense_dmamap);
4372 ahc_dma_tag_destroy(ahc, scb_data->sense_dmat);
4374 ahc_dmamap_unload(ahc, scb_data->hscb_dmat,
4375 scb_data->hscb_dmamap);
4377 ahc_dmamem_free(ahc, scb_data->hscb_dmat, scb_data->hscbs,
4378 scb_data->hscb_dmamap);
4379 ahc_dmamap_destroy(ahc, scb_data->hscb_dmat,
4380 scb_data->hscb_dmamap);
4382 ahc_dma_tag_destroy(ahc, scb_data->hscb_dmat);
4387 if (scb_data->scbarray != NULL)
4388 free(scb_data->scbarray, M_DEVBUF);
4392 ahc_alloc_scbs(struct ahc_softc *ahc)
4394 struct scb_data *scb_data;
4395 struct scb *next_scb;
4396 struct sg_map_node *sg_map;
4397 dma_addr_t physaddr;
4398 struct ahc_dma_seg *segs;
4402 scb_data = ahc->scb_data;
4403 if (scb_data->numscbs >= AHC_SCB_MAX_ALLOC)
4404 /* Can't allocate any more */
4407 next_scb = &scb_data->scbarray[scb_data->numscbs];
4409 sg_map = malloc(sizeof(*sg_map), M_DEVBUF, M_NOWAIT);
4414 /* Allocate S/G space for the next batch of SCBS */
4415 if (ahc_dmamem_alloc(ahc, scb_data->sg_dmat,
4416 (void **)&sg_map->sg_vaddr,
4417 BUS_DMA_NOWAIT, &sg_map->sg_dmamap) != 0) {
4418 free(sg_map, M_DEVBUF);
4422 SLIST_INSERT_HEAD(&scb_data->sg_maps, sg_map, links);
4424 ahc_dmamap_load(ahc, scb_data->sg_dmat, sg_map->sg_dmamap,
4425 sg_map->sg_vaddr, PAGE_SIZE, ahc_dmamap_cb,
4426 &sg_map->sg_physaddr, /*flags*/0);
4428 segs = sg_map->sg_vaddr;
4429 physaddr = sg_map->sg_physaddr;
4431 newcount = (PAGE_SIZE / (AHC_NSEG * sizeof(struct ahc_dma_seg)));
4432 newcount = min(newcount, (AHC_SCB_MAX_ALLOC - scb_data->numscbs));
4433 for (i = 0; i < newcount; i++) {
4434 struct scb_platform_data *pdata;
4438 pdata = (struct scb_platform_data *)malloc(sizeof(*pdata),
4439 M_DEVBUF, M_NOWAIT);
4442 next_scb->platform_data = pdata;
4443 next_scb->sg_map = sg_map;
4444 next_scb->sg_list = segs;
4446 * The sequencer always starts with the second entry.
4447 * The first entry is embedded in the scb.
4449 next_scb->sg_list_phys = physaddr + sizeof(struct ahc_dma_seg);
4450 next_scb->ahc_softc = ahc;
4451 next_scb->flags = SCB_FREE;
4453 error = ahc_dmamap_create(ahc, ahc->buffer_dmat, /*flags*/0,
4458 next_scb->hscb = &scb_data->hscbs[scb_data->numscbs];
4459 next_scb->hscb->tag = ahc->scb_data->numscbs;
4460 SLIST_INSERT_HEAD(&ahc->scb_data->free_scbs,
4461 next_scb, links.sle);
4463 physaddr += (AHC_NSEG * sizeof(struct ahc_dma_seg));
4465 ahc->scb_data->numscbs++;
4470 ahc_controller_info(struct ahc_softc *ahc, char *buf)
4474 len = sprintf(buf, "%s: ", ahc_chip_names[ahc->chip & AHC_CHIPID_MASK]);
4476 if ((ahc->features & AHC_TWIN) != 0)
4477 len = sprintf(buf, "Twin Channel, A SCSI Id=%d, "
4478 "B SCSI Id=%d, primary %c, ",
4479 ahc->our_id, ahc->our_id_b,
4480 (ahc->flags & AHC_PRIMARY_CHANNEL) + 'A');
4486 if ((ahc->features & AHC_ULTRA) != 0) {
4488 } else if ((ahc->features & AHC_DT) != 0) {
4489 speed = "Ultra160 ";
4490 } else if ((ahc->features & AHC_ULTRA2) != 0) {
4493 if ((ahc->features & AHC_WIDE) != 0) {
4498 len = sprintf(buf, "%s%s Channel %c, SCSI Id=%d, ",
4499 speed, type, ahc->channel, ahc->our_id);
4503 if ((ahc->flags & AHC_PAGESCBS) != 0)
4504 sprintf(buf, "%d/%d SCBs",
4505 ahc->scb_data->maxhscbs, AHC_MAX_QUEUE);
4507 sprintf(buf, "%d SCBs", ahc->scb_data->maxhscbs);
4511 ahc_chip_init(struct ahc_softc *ahc)
4517 u_int scsiseq_template;
4520 ahc_outb(ahc, SEQ_FLAGS, 0);
4521 ahc_outb(ahc, SEQ_FLAGS2, 0);
4523 /* Set the SCSI Id, SXFRCTL0, SXFRCTL1, and SIMODE1, for both channels*/
4524 if (ahc->features & AHC_TWIN) {
4527 * Setup Channel B first.
4529 ahc_outb(ahc, SBLKCTL, ahc_inb(ahc, SBLKCTL) | SELBUSB);
4530 term = (ahc->flags & AHC_TERM_ENB_B) != 0 ? STPWEN : 0;
4531 ahc_outb(ahc, SCSIID, ahc->our_id_b);
4532 scsi_conf = ahc_inb(ahc, SCSICONF + 1);
4533 ahc_outb(ahc, SXFRCTL1, (scsi_conf & (ENSPCHK|STIMESEL))
4534 |term|ahc->seltime_b|ENSTIMER|ACTNEGEN);
4535 if ((ahc->features & AHC_ULTRA2) != 0)
4536 ahc_outb(ahc, SIMODE0, ahc_inb(ahc, SIMODE0)|ENIOERR);
4537 ahc_outb(ahc, SIMODE1, ENSELTIMO|ENSCSIRST|ENSCSIPERR);
4538 ahc_outb(ahc, SXFRCTL0, DFON|SPIOEN);
4540 /* Select Channel A */
4541 ahc_outb(ahc, SBLKCTL, ahc_inb(ahc, SBLKCTL) & ~SELBUSB);
4543 term = (ahc->flags & AHC_TERM_ENB_A) != 0 ? STPWEN : 0;
4544 if ((ahc->features & AHC_ULTRA2) != 0)
4545 ahc_outb(ahc, SCSIID_ULTRA2, ahc->our_id);
4547 ahc_outb(ahc, SCSIID, ahc->our_id);
4548 scsi_conf = ahc_inb(ahc, SCSICONF);
4549 ahc_outb(ahc, SXFRCTL1, (scsi_conf & (ENSPCHK|STIMESEL))
4551 |ENSTIMER|ACTNEGEN);
4552 if ((ahc->features & AHC_ULTRA2) != 0)
4553 ahc_outb(ahc, SIMODE0, ahc_inb(ahc, SIMODE0)|ENIOERR);
4554 ahc_outb(ahc, SIMODE1, ENSELTIMO|ENSCSIRST|ENSCSIPERR);
4555 ahc_outb(ahc, SXFRCTL0, DFON|SPIOEN);
4557 /* There are no untagged SCBs active yet. */
4558 for (i = 0; i < 16; i++) {
4559 ahc_unbusy_tcl(ahc, BUILD_TCL(i << 4, 0));
4560 if ((ahc->flags & AHC_SCB_BTT) != 0) {
4564 * The SCB based BTT allows an entry per
4565 * target and lun pair.
4567 for (lun = 1; lun < AHC_NUM_LUNS; lun++)
4568 ahc_unbusy_tcl(ahc, BUILD_TCL(i << 4, lun));
4572 /* All of our queues are empty */
4573 for (i = 0; i < 256; i++)
4574 ahc->qoutfifo[i] = SCB_LIST_NULL;
4575 ahc_sync_qoutfifo(ahc, BUS_DMASYNC_PREREAD);
4577 for (i = 0; i < 256; i++)
4578 ahc->qinfifo[i] = SCB_LIST_NULL;
4580 if ((ahc->features & AHC_MULTI_TID) != 0) {
4581 ahc_outb(ahc, TARGID, 0);
4582 ahc_outb(ahc, TARGID + 1, 0);
4586 * Tell the sequencer where it can find our arrays in memory.
4588 physaddr = ahc->scb_data->hscb_busaddr;
4589 ahc_outb(ahc, HSCB_ADDR, physaddr & 0xFF);
4590 ahc_outb(ahc, HSCB_ADDR + 1, (physaddr >> 8) & 0xFF);
4591 ahc_outb(ahc, HSCB_ADDR + 2, (physaddr >> 16) & 0xFF);
4592 ahc_outb(ahc, HSCB_ADDR + 3, (physaddr >> 24) & 0xFF);
4594 physaddr = ahc->shared_data_busaddr;
4595 ahc_outb(ahc, SHARED_DATA_ADDR, physaddr & 0xFF);
4596 ahc_outb(ahc, SHARED_DATA_ADDR + 1, (physaddr >> 8) & 0xFF);
4597 ahc_outb(ahc, SHARED_DATA_ADDR + 2, (physaddr >> 16) & 0xFF);
4598 ahc_outb(ahc, SHARED_DATA_ADDR + 3, (physaddr >> 24) & 0xFF);
4601 * Initialize the group code to command length table.
4602 * This overrides the values in TARG_SCSIRATE, so only
4603 * setup the table after we have processed that information.
4605 ahc_outb(ahc, CMDSIZE_TABLE, 5);
4606 ahc_outb(ahc, CMDSIZE_TABLE + 1, 9);
4607 ahc_outb(ahc, CMDSIZE_TABLE + 2, 9);
4608 ahc_outb(ahc, CMDSIZE_TABLE + 3, 0);
4609 ahc_outb(ahc, CMDSIZE_TABLE + 4, 15);
4610 ahc_outb(ahc, CMDSIZE_TABLE + 5, 11);
4611 ahc_outb(ahc, CMDSIZE_TABLE + 6, 0);
4612 ahc_outb(ahc, CMDSIZE_TABLE + 7, 0);
4614 if ((ahc->features & AHC_HS_MAILBOX) != 0)
4615 ahc_outb(ahc, HS_MAILBOX, 0);
4617 /* Tell the sequencer of our initial queue positions */
4618 if ((ahc->features & AHC_TARGETMODE) != 0) {
4619 ahc->tqinfifonext = 1;
4620 ahc_outb(ahc, KERNEL_TQINPOS, ahc->tqinfifonext - 1);
4621 ahc_outb(ahc, TQINPOS, ahc->tqinfifonext);
4623 ahc->qinfifonext = 0;
4624 ahc->qoutfifonext = 0;
4625 if ((ahc->features & AHC_QUEUE_REGS) != 0) {
4626 ahc_outb(ahc, QOFF_CTLSTA, SCB_QSIZE_256);
4627 ahc_outb(ahc, HNSCB_QOFF, ahc->qinfifonext);
4628 ahc_outb(ahc, SNSCB_QOFF, ahc->qinfifonext);
4629 ahc_outb(ahc, SDSCB_QOFF, 0);
4631 ahc_outb(ahc, KERNEL_QINPOS, ahc->qinfifonext);
4632 ahc_outb(ahc, QINPOS, ahc->qinfifonext);
4633 ahc_outb(ahc, QOUTPOS, ahc->qoutfifonext);
4636 /* We don't have any waiting selections */
4637 ahc_outb(ahc, WAITING_SCBH, SCB_LIST_NULL);
4639 /* Our disconnection list is empty too */
4640 ahc_outb(ahc, DISCONNECTED_SCBH, SCB_LIST_NULL);
4642 /* Message out buffer starts empty */
4643 ahc_outb(ahc, MSG_OUT, MSG_NOOP);
4646 * Setup the allowed SCSI Sequences based on operational mode.
4647 * If we are a target, we'll enalbe select in operations once
4648 * we've had a lun enabled.
4650 scsiseq_template = ENSELO|ENAUTOATNO|ENAUTOATNP;
4651 if ((ahc->flags & AHC_INITIATORROLE) != 0)
4652 scsiseq_template |= ENRSELI;
4653 ahc_outb(ahc, SCSISEQ_TEMPLATE, scsiseq_template);
4655 /* Initialize our list of free SCBs. */
4656 ahc_build_free_scb_list(ahc);
4659 * Tell the sequencer which SCB will be the next one it receives.
4661 ahc_outb(ahc, NEXT_QUEUED_SCB, ahc->next_queued_scb->hscb->tag);
4664 * Load the Sequencer program and Enable the adapter
4668 printf("%s: Downloading Sequencer Program...",
4671 error = ahc_loadseq(ahc);
4675 if ((ahc->features & AHC_ULTRA2) != 0) {
4679 * Wait for up to 500ms for our transceivers
4680 * to settle. If the adapter does not have
4681 * a cable attached, the transceivers may
4682 * never settle, so don't complain if we
4686 (ahc_inb(ahc, SBLKCTL) & (ENAB40|ENAB20)) == 0 && wait;
4695 * Start the board, ready for normal operation
4698 ahc_init(struct ahc_softc *ahc)
4706 size_t driver_data_size;
4709 if ((ahc_debug & AHC_DEBUG_SEQUENCER) != 0)
4710 ahc->flags |= AHC_SEQUENCER_DEBUG;
4713 #ifdef AHC_PRINT_SRAM
4714 printf("Scratch Ram:");
4715 for (i = 0x20; i < 0x5f; i++) {
4716 if (((i % 8) == 0) && (i != 0)) {
4719 printf (" 0x%x", ahc_inb(ahc, i));
4721 if ((ahc->features & AHC_MORE_SRAM) != 0) {
4722 for (i = 0x70; i < 0x7f; i++) {
4723 if (((i % 8) == 0) && (i != 0)) {
4726 printf (" 0x%x", ahc_inb(ahc, i));
4731 * Reading uninitialized scratch ram may
4732 * generate parity errors.
4734 ahc_outb(ahc, CLRINT, CLRPARERR);
4735 ahc_outb(ahc, CLRINT, CLRBRKADRINT);
4740 * Assume we have a board at this stage and it has been reset.
4742 if ((ahc->flags & AHC_USEDEFAULTS) != 0)
4743 ahc->our_id = ahc->our_id_b = 7;
4746 * Default to allowing initiator operations.
4748 ahc->flags |= AHC_INITIATORROLE;
4751 * Only allow target mode features if this unit has them enabled.
4753 if ((AHC_TMODE_ENABLE & (0x1 << ahc->unit)) == 0)
4754 ahc->features &= ~AHC_TARGETMODE;
4757 /* DMA tag for mapping buffers into device visible space. */
4758 if (ahc_dma_tag_create(ahc, ahc->parent_dmat, /*alignment*/1,
4759 /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
4760 /*lowaddr*/ahc->flags & AHC_39BIT_ADDRESSING
4761 ? (dma_addr_t)0x7FFFFFFFFFULL
4762 : BUS_SPACE_MAXADDR_32BIT,
4763 /*highaddr*/BUS_SPACE_MAXADDR,
4764 /*filter*/NULL, /*filterarg*/NULL,
4765 /*maxsize*/(AHC_NSEG - 1) * PAGE_SIZE,
4766 /*nsegments*/AHC_NSEG,
4767 /*maxsegsz*/AHC_MAXTRANSFER_SIZE,
4768 /*flags*/BUS_DMA_ALLOCNOW,
4769 &ahc->buffer_dmat) != 0) {
4777 * DMA tag for our command fifos and other data in system memory
4778 * the card's sequencer must be able to access. For initiator
4779 * roles, we need to allocate space for the qinfifo and qoutfifo.
4780 * The qinfifo and qoutfifo are composed of 256 1 byte elements.
4781 * When providing for the target mode role, we must additionally
4782 * provide space for the incoming target command fifo and an extra
4783 * byte to deal with a dma bug in some chip versions.
4785 driver_data_size = 2 * 256 * sizeof(uint8_t);
4786 if ((ahc->features & AHC_TARGETMODE) != 0)
4787 driver_data_size += AHC_TMODE_CMDS * sizeof(struct target_cmd)
4788 + /*DMA WideOdd Bug Buffer*/1;
4789 if (ahc_dma_tag_create(ahc, ahc->parent_dmat, /*alignment*/1,
4790 /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
4791 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
4792 /*highaddr*/BUS_SPACE_MAXADDR,
4793 /*filter*/NULL, /*filterarg*/NULL,
4796 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
4797 /*flags*/0, &ahc->shared_data_dmat) != 0) {
4803 /* Allocation of driver data */
4804 if (ahc_dmamem_alloc(ahc, ahc->shared_data_dmat,
4805 (void **)&ahc->qoutfifo,
4806 BUS_DMA_NOWAIT, &ahc->shared_data_dmamap) != 0) {
4812 /* And permanently map it in */
4813 ahc_dmamap_load(ahc, ahc->shared_data_dmat, ahc->shared_data_dmamap,
4814 ahc->qoutfifo, driver_data_size, ahc_dmamap_cb,
4815 &ahc->shared_data_busaddr, /*flags*/0);
4817 if ((ahc->features & AHC_TARGETMODE) != 0) {
4818 ahc->targetcmds = (struct target_cmd *)ahc->qoutfifo;
4819 ahc->qoutfifo = (uint8_t *)&ahc->targetcmds[AHC_TMODE_CMDS];
4820 ahc->dma_bug_buf = ahc->shared_data_busaddr
4821 + driver_data_size - 1;
4822 /* All target command blocks start out invalid. */
4823 for (i = 0; i < AHC_TMODE_CMDS; i++)
4824 ahc->targetcmds[i].cmd_valid = 0;
4825 ahc_sync_tqinfifo(ahc, BUS_DMASYNC_PREREAD);
4826 ahc->qoutfifo = (uint8_t *)&ahc->targetcmds[256];
4828 ahc->qinfifo = &ahc->qoutfifo[256];
4832 /* Allocate SCB data now that buffer_dmat is initialized */
4833 if (ahc->scb_data->maxhscbs == 0)
4834 if (ahc_init_scbdata(ahc) != 0)
4838 * Allocate a tstate to house information for our
4839 * initiator presence on the bus as well as the user
4840 * data for any target mode initiator.
4842 if (ahc_alloc_tstate(ahc, ahc->our_id, 'A') == NULL) {
4843 printf("%s: unable to allocate ahc_tmode_tstate. "
4844 "Failing attach\n", ahc_name(ahc));
4848 if ((ahc->features & AHC_TWIN) != 0) {
4849 if (ahc_alloc_tstate(ahc, ahc->our_id_b, 'B') == NULL) {
4850 printf("%s: unable to allocate ahc_tmode_tstate. "
4851 "Failing attach\n", ahc_name(ahc));
4856 if (ahc->scb_data->maxhscbs < AHC_SCB_MAX_ALLOC) {
4857 ahc->flags |= AHC_PAGESCBS;
4859 ahc->flags &= ~AHC_PAGESCBS;
4863 if (ahc_debug & AHC_SHOW_MISC) {
4864 printf("%s: hardware scb %u bytes; kernel scb %u bytes; "
4865 "ahc_dma %u bytes\n",
4867 (u_int)sizeof(struct hardware_scb),
4868 (u_int)sizeof(struct scb),
4869 (u_int)sizeof(struct ahc_dma_seg));
4871 #endif /* AHC_DEBUG */
4874 * Look at the information that board initialization or
4875 * the board bios has left us.
4877 if (ahc->features & AHC_TWIN) {
4878 scsi_conf = ahc_inb(ahc, SCSICONF + 1);
4879 if ((scsi_conf & RESET_SCSI) != 0
4880 && (ahc->flags & AHC_INITIATORROLE) != 0)
4881 ahc->flags |= AHC_RESET_BUS_B;
4884 scsi_conf = ahc_inb(ahc, SCSICONF);
4885 if ((scsi_conf & RESET_SCSI) != 0
4886 && (ahc->flags & AHC_INITIATORROLE) != 0)
4887 ahc->flags |= AHC_RESET_BUS_A;
4890 tagenable = ALL_TARGETS_MASK;
4892 /* Grab the disconnection disable table and invert it for our needs */
4893 if ((ahc->flags & AHC_USEDEFAULTS) != 0) {
4894 printf("%s: Host Adapter Bios disabled. Using default SCSI "
4895 "device parameters\n", ahc_name(ahc));
4896 ahc->flags |= AHC_EXTENDED_TRANS_A|AHC_EXTENDED_TRANS_B|
4897 AHC_TERM_ENB_A|AHC_TERM_ENB_B;
4898 discenable = ALL_TARGETS_MASK;
4899 if ((ahc->features & AHC_ULTRA) != 0)
4900 ultraenb = ALL_TARGETS_MASK;
4902 discenable = ~((ahc_inb(ahc, DISC_DSB + 1) << 8)
4903 | ahc_inb(ahc, DISC_DSB));
4904 if ((ahc->features & (AHC_ULTRA|AHC_ULTRA2)) != 0)
4905 ultraenb = (ahc_inb(ahc, ULTRA_ENB + 1) << 8)
4906 | ahc_inb(ahc, ULTRA_ENB);
4909 if ((ahc->features & (AHC_WIDE|AHC_TWIN)) == 0)
4912 for (i = 0; i <= max_targ; i++) {
4913 struct ahc_initiator_tinfo *tinfo;
4914 struct ahc_tmode_tstate *tstate;
4920 our_id = ahc->our_id;
4922 if (i > 7 && (ahc->features & AHC_TWIN) != 0) {
4924 our_id = ahc->our_id_b;
4927 tinfo = ahc_fetch_transinfo(ahc, channel, our_id,
4928 target_id, &tstate);
4929 /* Default to async narrow across the board */
4930 memset(tinfo, 0, sizeof(*tinfo));
4931 if (ahc->flags & AHC_USEDEFAULTS) {
4932 if ((ahc->features & AHC_WIDE) != 0)
4933 tinfo->user.width = MSG_EXT_WDTR_BUS_16_BIT;
4936 * These will be truncated when we determine the
4937 * connection type we have with the target.
4939 tinfo->user.period = ahc_syncrates->period;
4940 tinfo->user.offset = MAX_OFFSET;
4945 /* Take the settings leftover in scratch RAM. */
4946 scsirate = ahc_inb(ahc, TARG_SCSIRATE + i);
4948 if ((ahc->features & AHC_ULTRA2) != 0) {
4952 if ((scsirate & SOFS) == 0x0F) {
4954 * Haven't negotiated yet,
4955 * so the format is different.
4957 scsirate = (scsirate & SXFR) >> 4
4960 | (scsirate & WIDEXFER);
4961 offset = MAX_OFFSET_ULTRA2;
4963 offset = ahc_inb(ahc, TARG_OFFSET + i);
4964 if ((scsirate & ~WIDEXFER) == 0 && offset != 0)
4965 /* Set to the lowest sync rate, 5MHz */
4967 maxsync = AHC_SYNCRATE_ULTRA2;
4968 if ((ahc->features & AHC_DT) != 0)
4969 maxsync = AHC_SYNCRATE_DT;
4970 tinfo->user.period =
4971 ahc_find_period(ahc, scsirate, maxsync);
4973 tinfo->user.period = 0;
4975 tinfo->user.offset = MAX_OFFSET;
4976 if ((scsirate & SXFR_ULTRA2) <= 8/*10MHz*/
4977 && (ahc->features & AHC_DT) != 0)
4978 tinfo->user.ppr_options =
4980 } else if ((scsirate & SOFS) != 0) {
4981 if ((scsirate & SXFR) == 0x40
4982 && (ultraenb & mask) != 0) {
4983 /* Treat 10MHz as a non-ultra speed */
4987 tinfo->user.period =
4988 ahc_find_period(ahc, scsirate,
4990 ? AHC_SYNCRATE_ULTRA
4991 : AHC_SYNCRATE_FAST);
4992 if (tinfo->user.period != 0)
4993 tinfo->user.offset = MAX_OFFSET;
4995 if (tinfo->user.period == 0)
4996 tinfo->user.offset = 0;
4997 if ((scsirate & WIDEXFER) != 0
4998 && (ahc->features & AHC_WIDE) != 0)
4999 tinfo->user.width = MSG_EXT_WDTR_BUS_16_BIT;
5000 tinfo->user.protocol_version = 4;
5001 if ((ahc->features & AHC_DT) != 0)
5002 tinfo->user.transport_version = 3;
5004 tinfo->user.transport_version = 2;
5005 tinfo->goal.protocol_version = 2;
5006 tinfo->goal.transport_version = 2;
5007 tinfo->curr.protocol_version = 2;
5008 tinfo->curr.transport_version = 2;
5010 tstate->ultraenb = 0;
5012 ahc->user_discenable = discenable;
5013 ahc->user_tagenable = tagenable;
5015 return (ahc->bus_chip_init(ahc));
5019 ahc_intr_enable(struct ahc_softc *ahc, int enable)
5023 hcntrl = ahc_inb(ahc, HCNTRL);
5025 ahc->pause &= ~INTEN;
5026 ahc->unpause &= ~INTEN;
5029 ahc->pause |= INTEN;
5030 ahc->unpause |= INTEN;
5032 ahc_outb(ahc, HCNTRL, hcntrl);
5036 * Ensure that the card is paused in a location
5037 * outside of all critical sections and that all
5038 * pending work is completed prior to returning.
5039 * This routine should only be called from outside
5040 * an interrupt context.
5043 ahc_pause_and_flushwork(struct ahc_softc *ahc)
5050 ahc->flags |= AHC_ALL_INTERRUPTS;
5056 * Give the sequencer some time to service
5057 * any active selections.
5064 ahc_outb(ahc, SCSISEQ, ahc_inb(ahc, SCSISEQ) & ~ENSELO);
5065 intstat = ahc_inb(ahc, INTSTAT);
5066 if ((intstat & INT_PEND) == 0) {
5067 ahc_clear_critical_section(ahc);
5068 intstat = ahc_inb(ahc, INTSTAT);
5071 && (intstat != 0xFF || (ahc->features & AHC_REMOVABLE) == 0)
5072 && ((intstat & INT_PEND) != 0
5073 || (ahc_inb(ahc, SSTAT0) & (SELDO|SELINGO)) != 0));
5074 if (maxloops == 0) {
5075 printf("Infinite interrupt loop, INTSTAT = %x",
5076 ahc_inb(ahc, INTSTAT));
5078 ahc_platform_flushwork(ahc);
5079 ahc->flags &= ~AHC_ALL_INTERRUPTS;
5084 ahc_suspend(struct ahc_softc *ahc)
5087 ahc_pause_and_flushwork(ahc);
5089 if (LIST_FIRST(&ahc->pending_scbs) != NULL) {
5094 #ifdef AHC_TARGET_MODE
5096 * XXX What about ATIOs that have not yet been serviced?
5097 * Perhaps we should just refuse to be suspended if we
5098 * are acting in a target role.
5100 if (ahc->pending_device != NULL) {
5110 ahc_resume(struct ahc_softc *ahc)
5113 ahc_reset(ahc, /*reinit*/TRUE);
5114 ahc_intr_enable(ahc, TRUE);
5119 /************************** Busy Target Table *********************************/
5121 * Return the untagged transaction id for a given target/channel lun.
5122 * Optionally, clear the entry.
5125 ahc_index_busy_tcl(struct ahc_softc *ahc, u_int tcl)
5128 u_int target_offset;
5130 if ((ahc->flags & AHC_SCB_BTT) != 0) {
5133 saved_scbptr = ahc_inb(ahc, SCBPTR);
5134 ahc_outb(ahc, SCBPTR, TCL_LUN(tcl));
5135 scbid = ahc_inb(ahc, SCB_64_BTT + TCL_TARGET_OFFSET(tcl));
5136 ahc_outb(ahc, SCBPTR, saved_scbptr);
5138 target_offset = TCL_TARGET_OFFSET(tcl);
5139 scbid = ahc_inb(ahc, BUSY_TARGETS + target_offset);
5146 ahc_unbusy_tcl(struct ahc_softc *ahc, u_int tcl)
5148 u_int target_offset;
5150 if ((ahc->flags & AHC_SCB_BTT) != 0) {
5153 saved_scbptr = ahc_inb(ahc, SCBPTR);
5154 ahc_outb(ahc, SCBPTR, TCL_LUN(tcl));
5155 ahc_outb(ahc, SCB_64_BTT+TCL_TARGET_OFFSET(tcl), SCB_LIST_NULL);
5156 ahc_outb(ahc, SCBPTR, saved_scbptr);
5158 target_offset = TCL_TARGET_OFFSET(tcl);
5159 ahc_outb(ahc, BUSY_TARGETS + target_offset, SCB_LIST_NULL);
5164 ahc_busy_tcl(struct ahc_softc *ahc, u_int tcl, u_int scbid)
5166 u_int target_offset;
5168 if ((ahc->flags & AHC_SCB_BTT) != 0) {
5171 saved_scbptr = ahc_inb(ahc, SCBPTR);
5172 ahc_outb(ahc, SCBPTR, TCL_LUN(tcl));
5173 ahc_outb(ahc, SCB_64_BTT + TCL_TARGET_OFFSET(tcl), scbid);
5174 ahc_outb(ahc, SCBPTR, saved_scbptr);
5176 target_offset = TCL_TARGET_OFFSET(tcl);
5177 ahc_outb(ahc, BUSY_TARGETS + target_offset, scbid);
5181 /************************** SCB and SCB queue management **********************/
5183 ahc_match_scb(struct ahc_softc *ahc, struct scb *scb, int target,
5184 char channel, int lun, u_int tag, role_t role)
5186 int targ = SCB_GET_TARGET(ahc, scb);
5187 char chan = SCB_GET_CHANNEL(ahc, scb);
5188 int slun = SCB_GET_LUN(scb);
5191 match = ((chan == channel) || (channel == ALL_CHANNELS));
5193 match = ((targ == target) || (target == CAM_TARGET_WILDCARD));
5195 match = ((lun == slun) || (lun == CAM_LUN_WILDCARD));
5197 #ifdef AHC_TARGET_MODE
5200 group = XPT_FC_GROUP(scb->io_ctx->ccb_h.func_code);
5201 if (role == ROLE_INITIATOR) {
5202 match = (group != XPT_FC_GROUP_TMODE)
5203 && ((tag == scb->hscb->tag)
5204 || (tag == SCB_LIST_NULL));
5205 } else if (role == ROLE_TARGET) {
5206 match = (group == XPT_FC_GROUP_TMODE)
5207 && ((tag == scb->io_ctx->csio.tag_id)
5208 || (tag == SCB_LIST_NULL));
5210 #else /* !AHC_TARGET_MODE */
5211 match = ((tag == scb->hscb->tag) || (tag == SCB_LIST_NULL));
5212 #endif /* AHC_TARGET_MODE */
5219 ahc_freeze_devq(struct ahc_softc *ahc, struct scb *scb)
5225 target = SCB_GET_TARGET(ahc, scb);
5226 lun = SCB_GET_LUN(scb);
5227 channel = SCB_GET_CHANNEL(ahc, scb);
5229 ahc_search_qinfifo(ahc, target, channel, lun,
5230 /*tag*/SCB_LIST_NULL, ROLE_UNKNOWN,
5231 CAM_REQUEUE_REQ, SEARCH_COMPLETE);
5233 ahc_platform_freeze_devq(ahc, scb);
5237 ahc_qinfifo_requeue_tail(struct ahc_softc *ahc, struct scb *scb)
5239 struct scb *prev_scb;
5242 if (ahc_qinfifo_count(ahc) != 0) {
5246 prev_pos = ahc->qinfifonext - 1;
5247 prev_tag = ahc->qinfifo[prev_pos];
5248 prev_scb = ahc_lookup_scb(ahc, prev_tag);
5250 ahc_qinfifo_requeue(ahc, prev_scb, scb);
5251 if ((ahc->features & AHC_QUEUE_REGS) != 0) {
5252 ahc_outb(ahc, HNSCB_QOFF, ahc->qinfifonext);
5254 ahc_outb(ahc, KERNEL_QINPOS, ahc->qinfifonext);
5259 ahc_qinfifo_requeue(struct ahc_softc *ahc, struct scb *prev_scb,
5262 if (prev_scb == NULL) {
5263 ahc_outb(ahc, NEXT_QUEUED_SCB, scb->hscb->tag);
5265 prev_scb->hscb->next = scb->hscb->tag;
5266 ahc_sync_scb(ahc, prev_scb,
5267 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
5269 ahc->qinfifo[ahc->qinfifonext++] = scb->hscb->tag;
5270 scb->hscb->next = ahc->next_queued_scb->hscb->tag;
5271 ahc_sync_scb(ahc, scb, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
5275 ahc_qinfifo_count(struct ahc_softc *ahc)
5280 if ((ahc->features & AHC_QUEUE_REGS) != 0) {
5281 qinpos = ahc_inb(ahc, SNSCB_QOFF);
5282 ahc_outb(ahc, SNSCB_QOFF, qinpos);
5284 qinpos = ahc_inb(ahc, QINPOS);
5285 diff = ahc->qinfifonext - qinpos;
5290 ahc_search_qinfifo(struct ahc_softc *ahc, int target, char channel,
5291 int lun, u_int tag, role_t role, uint32_t status,
5292 ahc_search_action action)
5295 struct scb *prev_scb;
5305 qintail = ahc->qinfifonext;
5306 have_qregs = (ahc->features & AHC_QUEUE_REGS) != 0;
5308 qinstart = ahc_inb(ahc, SNSCB_QOFF);
5309 ahc_outb(ahc, SNSCB_QOFF, qinstart);
5311 qinstart = ahc_inb(ahc, QINPOS);
5316 if (action == SEARCH_COMPLETE) {
5318 * Don't attempt to run any queued untagged transactions
5319 * until we are done with the abort process.
5321 ahc_freeze_untagged_queues(ahc);
5325 * Start with an empty queue. Entries that are not chosen
5326 * for removal will be re-added to the queue as we go.
5328 ahc->qinfifonext = qinpos;
5329 ahc_outb(ahc, NEXT_QUEUED_SCB, ahc->next_queued_scb->hscb->tag);
5331 while (qinpos != qintail) {
5332 scb = ahc_lookup_scb(ahc, ahc->qinfifo[qinpos]);
5334 printf("qinpos = %d, SCB index = %d\n",
5335 qinpos, ahc->qinfifo[qinpos]);
5339 if (ahc_match_scb(ahc, scb, target, channel, lun, tag, role)) {
5341 * We found an scb that needs to be acted on.
5345 case SEARCH_COMPLETE:
5350 ostat = ahc_get_transaction_status(scb);
5351 if (ostat == CAM_REQ_INPROG)
5352 ahc_set_transaction_status(scb, status);
5353 cstat = ahc_get_transaction_status(scb);
5354 if (cstat != CAM_REQ_CMP)
5355 ahc_freeze_scb(scb);
5356 if ((scb->flags & SCB_ACTIVE) == 0)
5357 printf("Inactive SCB in qinfifo\n");
5365 ahc_qinfifo_requeue(ahc, prev_scb, scb);
5370 ahc_qinfifo_requeue(ahc, prev_scb, scb);
5376 if ((ahc->features & AHC_QUEUE_REGS) != 0) {
5377 ahc_outb(ahc, HNSCB_QOFF, ahc->qinfifonext);
5379 ahc_outb(ahc, KERNEL_QINPOS, ahc->qinfifonext);
5382 if (action != SEARCH_COUNT
5384 && (qinstart != ahc->qinfifonext)) {
5386 * The sequencer may be in the process of dmaing
5387 * down the SCB at the beginning of the queue.
5388 * This could be problematic if either the first,
5389 * or the second SCB is removed from the queue
5390 * (the first SCB includes a pointer to the "next"
5391 * SCB to dma). If we have removed any entries, swap
5392 * the first element in the queue with the next HSCB
5393 * so the sequencer will notice that NEXT_QUEUED_SCB
5394 * has changed during its dma attempt and will retry
5397 scb = ahc_lookup_scb(ahc, ahc->qinfifo[qinstart]);
5400 printf("found = %d, qinstart = %d, qinfifionext = %d\n",
5401 found, qinstart, ahc->qinfifonext);
5402 panic("First/Second Qinfifo fixup\n");
5405 * ahc_swap_with_next_hscb forces our next pointer to
5406 * point to the reserved SCB for future commands. Save
5407 * and restore our original next pointer to maintain
5410 next = scb->hscb->next;
5411 ahc->scb_data->scbindex[scb->hscb->tag] = NULL;
5412 ahc_swap_with_next_hscb(ahc, scb);
5413 scb->hscb->next = next;
5414 ahc->qinfifo[qinstart] = scb->hscb->tag;
5416 /* Tell the card about the new head of the qinfifo. */
5417 ahc_outb(ahc, NEXT_QUEUED_SCB, scb->hscb->tag);
5419 /* Fixup the tail "next" pointer. */
5420 qintail = ahc->qinfifonext - 1;
5421 scb = ahc_lookup_scb(ahc, ahc->qinfifo[qintail]);
5422 scb->hscb->next = ahc->next_queued_scb->hscb->tag;
5426 * Search waiting for selection list.
5428 curscbptr = ahc_inb(ahc, SCBPTR);
5429 next = ahc_inb(ahc, WAITING_SCBH); /* Start at head of list. */
5430 prev = SCB_LIST_NULL;
5432 while (next != SCB_LIST_NULL) {
5435 ahc_outb(ahc, SCBPTR, next);
5436 scb_index = ahc_inb(ahc, SCB_TAG);
5437 if (scb_index >= ahc->scb_data->numscbs) {
5438 printf("Waiting List inconsistency. "
5439 "SCB index == %d, yet numscbs == %d.",
5440 scb_index, ahc->scb_data->numscbs);
5441 ahc_dump_card_state(ahc);
5442 panic("for safety");
5444 scb = ahc_lookup_scb(ahc, scb_index);
5446 printf("scb_index = %d, next = %d\n",
5448 panic("Waiting List traversal\n");
5450 if (ahc_match_scb(ahc, scb, target, channel,
5451 lun, SCB_LIST_NULL, role)) {
5453 * We found an scb that needs to be acted on.
5457 case SEARCH_COMPLETE:
5462 ostat = ahc_get_transaction_status(scb);
5463 if (ostat == CAM_REQ_INPROG)
5464 ahc_set_transaction_status(scb,
5466 cstat = ahc_get_transaction_status(scb);
5467 if (cstat != CAM_REQ_CMP)
5468 ahc_freeze_scb(scb);
5469 if ((scb->flags & SCB_ACTIVE) == 0)
5470 printf("Inactive SCB in Waiting List\n");
5475 next = ahc_rem_wscb(ahc, next, prev);
5479 next = ahc_inb(ahc, SCB_NEXT);
5485 next = ahc_inb(ahc, SCB_NEXT);
5488 ahc_outb(ahc, SCBPTR, curscbptr);
5490 found += ahc_search_untagged_queues(ahc, /*ahc_io_ctx_t*/NULL, target,
5491 channel, lun, status, action);
5493 if (action == SEARCH_COMPLETE)
5494 ahc_release_untagged_queues(ahc);
5499 ahc_search_untagged_queues(struct ahc_softc *ahc, ahc_io_ctx_t ctx,
5500 int target, char channel, int lun, uint32_t status,
5501 ahc_search_action action)
5508 if (action == SEARCH_COMPLETE) {
5510 * Don't attempt to run any queued untagged transactions
5511 * until we are done with the abort process.
5513 ahc_freeze_untagged_queues(ahc);
5518 if ((ahc->flags & AHC_SCB_BTT) == 0) {
5521 if (target != CAM_TARGET_WILDCARD) {
5532 for (; i < maxtarget; i++) {
5533 struct scb_tailq *untagged_q;
5534 struct scb *next_scb;
5536 untagged_q = &(ahc->untagged_queues[i]);
5537 next_scb = TAILQ_FIRST(untagged_q);
5538 while (next_scb != NULL) {
5541 next_scb = TAILQ_NEXT(scb, links.tqe);
5544 * The head of the list may be the currently
5545 * active untagged command for a device.
5546 * We're only searching for commands that
5547 * have not been started. A transaction
5548 * marked active but still in the qinfifo
5549 * is removed by the qinfifo scanning code
5552 if ((scb->flags & SCB_ACTIVE) != 0)
5555 if (ahc_match_scb(ahc, scb, target, channel, lun,
5556 SCB_LIST_NULL, ROLE_INITIATOR) == 0
5557 || (ctx != NULL && ctx != scb->io_ctx))
5561 * We found an scb that needs to be acted on.
5565 case SEARCH_COMPLETE:
5570 ostat = ahc_get_transaction_status(scb);
5571 if (ostat == CAM_REQ_INPROG)
5572 ahc_set_transaction_status(scb, status);
5573 cstat = ahc_get_transaction_status(scb);
5574 if (cstat != CAM_REQ_CMP)
5575 ahc_freeze_scb(scb);
5576 if ((scb->flags & SCB_ACTIVE) == 0)
5577 printf("Inactive SCB in untaggedQ\n");
5582 scb->flags &= ~SCB_UNTAGGEDQ;
5583 TAILQ_REMOVE(untagged_q, scb, links.tqe);
5591 if (action == SEARCH_COMPLETE)
5592 ahc_release_untagged_queues(ahc);
5597 ahc_search_disc_list(struct ahc_softc *ahc, int target, char channel,
5598 int lun, u_int tag, int stop_on_first, int remove,
5608 next = ahc_inb(ahc, DISCONNECTED_SCBH);
5609 prev = SCB_LIST_NULL;
5612 /* restore this when we're done */
5613 active_scb = ahc_inb(ahc, SCBPTR);
5615 /* Silence compiler */
5616 active_scb = SCB_LIST_NULL;
5618 while (next != SCB_LIST_NULL) {
5621 ahc_outb(ahc, SCBPTR, next);
5622 scb_index = ahc_inb(ahc, SCB_TAG);
5623 if (scb_index >= ahc->scb_data->numscbs) {
5624 printf("Disconnected List inconsistency. "
5625 "SCB index == %d, yet numscbs == %d.",
5626 scb_index, ahc->scb_data->numscbs);
5627 ahc_dump_card_state(ahc);
5628 panic("for safety");
5632 panic("Disconnected List Loop. "
5633 "cur SCBPTR == %x, prev SCBPTR == %x.",
5636 scbp = ahc_lookup_scb(ahc, scb_index);
5637 if (ahc_match_scb(ahc, scbp, target, channel, lun,
5638 tag, ROLE_INITIATOR)) {
5642 ahc_rem_scb_from_disc_list(ahc, prev, next);
5645 next = ahc_inb(ahc, SCB_NEXT);
5651 next = ahc_inb(ahc, SCB_NEXT);
5655 ahc_outb(ahc, SCBPTR, active_scb);
5660 * Remove an SCB from the on chip list of disconnected transactions.
5661 * This is empty/unused if we are not performing SCB paging.
5664 ahc_rem_scb_from_disc_list(struct ahc_softc *ahc, u_int prev, u_int scbptr)
5668 ahc_outb(ahc, SCBPTR, scbptr);
5669 next = ahc_inb(ahc, SCB_NEXT);
5671 ahc_outb(ahc, SCB_CONTROL, 0);
5673 ahc_add_curscb_to_free_list(ahc);
5675 if (prev != SCB_LIST_NULL) {
5676 ahc_outb(ahc, SCBPTR, prev);
5677 ahc_outb(ahc, SCB_NEXT, next);
5679 ahc_outb(ahc, DISCONNECTED_SCBH, next);
5685 * Add the SCB as selected by SCBPTR onto the on chip list of
5686 * free hardware SCBs. This list is empty/unused if we are not
5687 * performing SCB paging.
5690 ahc_add_curscb_to_free_list(struct ahc_softc *ahc)
5693 * Invalidate the tag so that our abort
5694 * routines don't think it's active.
5696 ahc_outb(ahc, SCB_TAG, SCB_LIST_NULL);
5698 if ((ahc->flags & AHC_PAGESCBS) != 0) {
5699 ahc_outb(ahc, SCB_NEXT, ahc_inb(ahc, FREE_SCBH));
5700 ahc_outb(ahc, FREE_SCBH, ahc_inb(ahc, SCBPTR));
5705 * Manipulate the waiting for selection list and return the
5706 * scb that follows the one that we remove.
5709 ahc_rem_wscb(struct ahc_softc *ahc, u_int scbpos, u_int prev)
5714 * Select the SCB we want to abort and
5715 * pull the next pointer out of it.
5717 curscb = ahc_inb(ahc, SCBPTR);
5718 ahc_outb(ahc, SCBPTR, scbpos);
5719 next = ahc_inb(ahc, SCB_NEXT);
5721 /* Clear the necessary fields */
5722 ahc_outb(ahc, SCB_CONTROL, 0);
5724 ahc_add_curscb_to_free_list(ahc);
5726 /* update the waiting list */
5727 if (prev == SCB_LIST_NULL) {
5728 /* First in the list */
5729 ahc_outb(ahc, WAITING_SCBH, next);
5732 * Ensure we aren't attempting to perform
5733 * selection for this entry.
5735 ahc_outb(ahc, SCSISEQ, (ahc_inb(ahc, SCSISEQ) & ~ENSELO));
5738 * Select the scb that pointed to us
5739 * and update its next pointer.
5741 ahc_outb(ahc, SCBPTR, prev);
5742 ahc_outb(ahc, SCB_NEXT, next);
5746 * Point us back at the original scb position.
5748 ahc_outb(ahc, SCBPTR, curscb);
5752 /******************************** Error Handling ******************************/
5754 * Abort all SCBs that match the given description (target/channel/lun/tag),
5755 * setting their status to the passed in status if the status has not already
5756 * been modified from CAM_REQ_INPROG. This routine assumes that the sequencer
5757 * is paused before it is called.
5760 ahc_abort_scbs(struct ahc_softc *ahc, int target, char channel,
5761 int lun, u_int tag, role_t role, uint32_t status)
5764 struct scb *scbp_next;
5774 * Don't attempt to run any queued untagged transactions
5775 * until we are done with the abort process.
5777 ahc_freeze_untagged_queues(ahc);
5779 /* restore this when we're done */
5780 active_scb = ahc_inb(ahc, SCBPTR);
5782 found = ahc_search_qinfifo(ahc, target, channel, lun, SCB_LIST_NULL,
5783 role, CAM_REQUEUE_REQ, SEARCH_COMPLETE);
5786 * Clean out the busy target table for any untagged commands.
5790 if (target != CAM_TARGET_WILDCARD) {
5797 if (lun == CAM_LUN_WILDCARD) {
5800 * Unless we are using an SCB based
5801 * busy targets table, there is only
5802 * one table entry for all luns of
5807 if ((ahc->flags & AHC_SCB_BTT) != 0)
5808 maxlun = AHC_NUM_LUNS;
5814 if (role != ROLE_TARGET) {
5815 for (;i < maxtarget; i++) {
5816 for (j = minlun;j < maxlun; j++) {
5820 tcl = BUILD_TCL(i << 4, j);
5821 scbid = ahc_index_busy_tcl(ahc, tcl);
5822 scbp = ahc_lookup_scb(ahc, scbid);
5824 || ahc_match_scb(ahc, scbp, target, channel,
5825 lun, tag, role) == 0)
5827 ahc_unbusy_tcl(ahc, BUILD_TCL(i << 4, j));
5832 * Go through the disconnected list and remove any entries we
5833 * have queued for completion, 0'ing their control byte too.
5834 * We save the active SCB and restore it ourselves, so there
5835 * is no reason for this search to restore it too.
5837 ahc_search_disc_list(ahc, target, channel, lun, tag,
5838 /*stop_on_first*/FALSE, /*remove*/TRUE,
5839 /*save_state*/FALSE);
5843 * Go through the hardware SCB array looking for commands that
5844 * were active but not on any list. In some cases, these remnants
5845 * might not still have mappings in the scbindex array (e.g. unexpected
5846 * bus free with the same scb queued for an abort). Don't hold this
5849 for (i = 0; i < ahc->scb_data->maxhscbs; i++) {
5852 ahc_outb(ahc, SCBPTR, i);
5853 scbid = ahc_inb(ahc, SCB_TAG);
5854 scbp = ahc_lookup_scb(ahc, scbid);
5855 if ((scbp == NULL && scbid != SCB_LIST_NULL)
5857 && ahc_match_scb(ahc, scbp, target, channel, lun, tag, role)))
5858 ahc_add_curscb_to_free_list(ahc);
5862 * Go through the pending CCB list and look for
5863 * commands for this target that are still active.
5864 * These are other tagged commands that were
5865 * disconnected when the reset occurred.
5867 scbp_next = LIST_FIRST(&ahc->pending_scbs);
5868 while (scbp_next != NULL) {
5870 scbp_next = LIST_NEXT(scbp, pending_links);
5871 if (ahc_match_scb(ahc, scbp, target, channel, lun, tag, role)) {
5874 ostat = ahc_get_transaction_status(scbp);
5875 if (ostat == CAM_REQ_INPROG)
5876 ahc_set_transaction_status(scbp, status);
5877 if (ahc_get_transaction_status(scbp) != CAM_REQ_CMP)
5878 ahc_freeze_scb(scbp);
5879 if ((scbp->flags & SCB_ACTIVE) == 0)
5880 printf("Inactive SCB on pending list\n");
5881 ahc_done(ahc, scbp);
5885 ahc_outb(ahc, SCBPTR, active_scb);
5886 ahc_platform_abort_scbs(ahc, target, channel, lun, tag, role, status);
5887 ahc_release_untagged_queues(ahc);
5892 ahc_reset_current_bus(struct ahc_softc *ahc)
5896 ahc_outb(ahc, SIMODE1, ahc_inb(ahc, SIMODE1) & ~ENSCSIRST);
5897 scsiseq = ahc_inb(ahc, SCSISEQ);
5898 ahc_outb(ahc, SCSISEQ, scsiseq | SCSIRSTO);
5899 ahc_flush_device_writes(ahc);
5900 ahc_delay(AHC_BUSRESET_DELAY);
5901 /* Turn off the bus reset */
5902 ahc_outb(ahc, SCSISEQ, scsiseq & ~SCSIRSTO);
5904 ahc_clear_intstat(ahc);
5906 /* Re-enable reset interrupts */
5907 ahc_outb(ahc, SIMODE1, ahc_inb(ahc, SIMODE1) | ENSCSIRST);
5911 ahc_reset_channel(struct ahc_softc *ahc, char channel, int initiate_reset)
5913 struct ahc_devinfo devinfo;
5914 u_int initiator, target, max_scsiid;
5922 ahc->pending_device = NULL;
5924 ahc_compile_devinfo(&devinfo,
5925 CAM_TARGET_WILDCARD,
5926 CAM_TARGET_WILDCARD,
5928 channel, ROLE_UNKNOWN);
5931 /* Make sure the sequencer is in a safe location. */
5932 ahc_clear_critical_section(ahc);
5935 * Run our command complete fifos to ensure that we perform
5936 * completion processing on any commands that 'completed'
5937 * before the reset occurred.
5939 ahc_run_qoutfifo(ahc);
5940 #ifdef AHC_TARGET_MODE
5942 * XXX - In Twin mode, the tqinfifo may have commands
5943 * for an unaffected channel in it. However, if
5944 * we have run out of ATIO resources to drain that
5945 * queue, we may not get them all out here. Further,
5946 * the blocked transactions for the reset channel
5947 * should just be killed off, irrespecitve of whether
5948 * we are blocked on ATIO resources. Write a routine
5949 * to compact the tqinfifo appropriately.
5951 if ((ahc->flags & AHC_TARGETROLE) != 0) {
5952 ahc_run_tqinfifo(ahc, /*paused*/TRUE);
5957 * Reset the bus if we are initiating this reset
5959 sblkctl = ahc_inb(ahc, SBLKCTL);
5961 if ((ahc->features & AHC_TWIN) != 0
5962 && ((sblkctl & SELBUSB) != 0))
5964 scsiseq = ahc_inb(ahc, SCSISEQ_TEMPLATE);
5965 if (cur_channel != channel) {
5966 /* Case 1: Command for another bus is active
5967 * Stealthily reset the other bus without
5968 * upsetting the current bus.
5970 ahc_outb(ahc, SBLKCTL, sblkctl ^ SELBUSB);
5971 simode1 = ahc_inb(ahc, SIMODE1) & ~(ENBUSFREE|ENSCSIRST);
5972 #ifdef AHC_TARGET_MODE
5974 * Bus resets clear ENSELI, so we cannot
5975 * defer re-enabling bus reset interrupts
5976 * if we are in target mode.
5978 if ((ahc->flags & AHC_TARGETROLE) != 0)
5979 simode1 |= ENSCSIRST;
5981 ahc_outb(ahc, SIMODE1, simode1);
5983 ahc_reset_current_bus(ahc);
5984 ahc_clear_intstat(ahc);
5985 ahc_outb(ahc, SCSISEQ, scsiseq & (ENSELI|ENRSELI|ENAUTOATNP));
5986 ahc_outb(ahc, SBLKCTL, sblkctl);
5987 restart_needed = FALSE;
5989 /* Case 2: A command from this bus is active or we're idle */
5990 simode1 = ahc_inb(ahc, SIMODE1) & ~(ENBUSFREE|ENSCSIRST);
5991 #ifdef AHC_TARGET_MODE
5993 * Bus resets clear ENSELI, so we cannot
5994 * defer re-enabling bus reset interrupts
5995 * if we are in target mode.
5997 if ((ahc->flags & AHC_TARGETROLE) != 0)
5998 simode1 |= ENSCSIRST;
6000 ahc_outb(ahc, SIMODE1, simode1);
6002 ahc_reset_current_bus(ahc);
6003 ahc_clear_intstat(ahc);
6004 ahc_outb(ahc, SCSISEQ, scsiseq & (ENSELI|ENRSELI|ENAUTOATNP));
6005 restart_needed = TRUE;
6009 * Clean up all the state information for the
6010 * pending transactions on this bus.
6012 found = ahc_abort_scbs(ahc, CAM_TARGET_WILDCARD, channel,
6013 CAM_LUN_WILDCARD, SCB_LIST_NULL,
6014 ROLE_UNKNOWN, CAM_SCSI_BUS_RESET);
6016 max_scsiid = (ahc->features & AHC_WIDE) ? 15 : 7;
6018 #ifdef AHC_TARGET_MODE
6020 * Send an immediate notify ccb to all target more peripheral
6021 * drivers affected by this action.
6023 for (target = 0; target <= max_scsiid; target++) {
6024 struct ahc_tmode_tstate* tstate;
6027 tstate = ahc->enabled_targets[target];
6030 for (lun = 0; lun < AHC_NUM_LUNS; lun++) {
6031 struct ahc_tmode_lstate* lstate;
6033 lstate = tstate->enabled_luns[lun];
6037 ahc_queue_lstate_event(ahc, lstate, CAM_TARGET_WILDCARD,
6038 EVENT_TYPE_BUS_RESET, /*arg*/0);
6039 ahc_send_lstate_events(ahc, lstate);
6043 /* Notify the XPT that a bus reset occurred */
6044 ahc_send_async(ahc, devinfo.channel, CAM_TARGET_WILDCARD,
6045 CAM_LUN_WILDCARD, AC_BUS_RESET);
6048 * Revert to async/narrow transfers until we renegotiate.
6050 for (target = 0; target <= max_scsiid; target++) {
6052 if (ahc->enabled_targets[target] == NULL)
6054 for (initiator = 0; initiator <= max_scsiid; initiator++) {
6055 struct ahc_devinfo devinfo;
6057 ahc_compile_devinfo(&devinfo, target, initiator,
6059 channel, ROLE_UNKNOWN);
6060 ahc_set_width(ahc, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
6061 AHC_TRANS_CUR, /*paused*/TRUE);
6062 ahc_set_syncrate(ahc, &devinfo, /*syncrate*/NULL,
6063 /*period*/0, /*offset*/0,
6064 /*ppr_options*/0, AHC_TRANS_CUR,
6077 /***************************** Residual Processing ****************************/
6079 * Calculate the residual for a just completed SCB.
6082 ahc_calc_residual(struct ahc_softc *ahc, struct scb *scb)
6084 struct hardware_scb *hscb;
6085 struct status_pkt *spkt;
6087 uint32_t resid_sgptr;
6093 * SG_RESID_VALID clear in sgptr.
6094 * 2) Transferless command
6095 * 3) Never performed any transfers.
6096 * sgptr has SG_FULL_RESID set.
6097 * 4) No residual but target did not
6098 * save data pointers after the
6099 * last transfer, so sgptr was
6101 * 5) We have a partial residual.
6102 * Use residual_sgptr to determine
6107 sgptr = ahc_le32toh(hscb->sgptr);
6108 if ((sgptr & SG_RESID_VALID) == 0)
6111 sgptr &= ~SG_RESID_VALID;
6113 if ((sgptr & SG_LIST_NULL) != 0)
6117 spkt = &hscb->shared_data.status;
6118 resid_sgptr = ahc_le32toh(spkt->residual_sg_ptr);
6119 if ((sgptr & SG_FULL_RESID) != 0) {
6121 resid = ahc_get_transfer_length(scb);
6122 } else if ((resid_sgptr & SG_LIST_NULL) != 0) {
6125 } else if ((resid_sgptr & ~SG_PTR_MASK) != 0) {
6126 panic("Bogus resid sgptr value 0x%x\n", resid_sgptr);
6128 struct ahc_dma_seg *sg;
6131 * Remainder of the SG where the transfer
6134 resid = ahc_le32toh(spkt->residual_datacnt) & AHC_SG_LEN_MASK;
6135 sg = ahc_sg_bus_to_virt(scb, resid_sgptr & SG_PTR_MASK);
6137 /* The residual sg_ptr always points to the next sg */
6141 * Add up the contents of all residual
6142 * SG segments that are after the SG where
6143 * the transfer stopped.
6145 while ((ahc_le32toh(sg->len) & AHC_DMA_LAST_SEG) == 0) {
6147 resid += ahc_le32toh(sg->len) & AHC_SG_LEN_MASK;
6150 if ((scb->flags & SCB_SENSE) == 0)
6151 ahc_set_residual(scb, resid);
6153 ahc_set_sense_residual(scb, resid);
6156 if ((ahc_debug & AHC_SHOW_MISC) != 0) {
6157 ahc_print_path(ahc, scb);
6158 printf("Handled %sResidual of %d bytes\n",
6159 (scb->flags & SCB_SENSE) ? "Sense " : "", resid);
6164 /******************************* Target Mode **********************************/
6165 #ifdef AHC_TARGET_MODE
6167 * Add a target mode event to this lun's queue
6170 ahc_queue_lstate_event(struct ahc_softc *ahc, struct ahc_tmode_lstate *lstate,
6171 u_int initiator_id, u_int event_type, u_int event_arg)
6173 struct ahc_tmode_event *event;
6176 xpt_freeze_devq(lstate->path, /*count*/1);
6177 if (lstate->event_w_idx >= lstate->event_r_idx)
6178 pending = lstate->event_w_idx - lstate->event_r_idx;
6180 pending = AHC_TMODE_EVENT_BUFFER_SIZE + 1
6181 - (lstate->event_r_idx - lstate->event_w_idx);
6183 if (event_type == EVENT_TYPE_BUS_RESET
6184 || event_type == MSG_BUS_DEV_RESET) {
6186 * Any earlier events are irrelevant, so reset our buffer.
6187 * This has the effect of allowing us to deal with reset
6188 * floods (an external device holding down the reset line)
6189 * without losing the event that is really interesting.
6191 lstate->event_r_idx = 0;
6192 lstate->event_w_idx = 0;
6193 xpt_release_devq(lstate->path, pending, /*runqueue*/FALSE);
6196 if (pending == AHC_TMODE_EVENT_BUFFER_SIZE) {
6197 xpt_print_path(lstate->path);
6198 printf("immediate event %x:%x lost\n",
6199 lstate->event_buffer[lstate->event_r_idx].event_type,
6200 lstate->event_buffer[lstate->event_r_idx].event_arg);
6201 lstate->event_r_idx++;
6202 if (lstate->event_r_idx == AHC_TMODE_EVENT_BUFFER_SIZE)
6203 lstate->event_r_idx = 0;
6204 xpt_release_devq(lstate->path, /*count*/1, /*runqueue*/FALSE);
6207 event = &lstate->event_buffer[lstate->event_w_idx];
6208 event->initiator_id = initiator_id;
6209 event->event_type = event_type;
6210 event->event_arg = event_arg;
6211 lstate->event_w_idx++;
6212 if (lstate->event_w_idx == AHC_TMODE_EVENT_BUFFER_SIZE)
6213 lstate->event_w_idx = 0;
6217 * Send any target mode events queued up waiting
6218 * for immediate notify resources.
6221 ahc_send_lstate_events(struct ahc_softc *ahc, struct ahc_tmode_lstate *lstate)
6223 struct ccb_hdr *ccbh;
6224 struct ccb_immed_notify *inot;
6226 while (lstate->event_r_idx != lstate->event_w_idx
6227 && (ccbh = SLIST_FIRST(&lstate->immed_notifies)) != NULL) {
6228 struct ahc_tmode_event *event;
6230 event = &lstate->event_buffer[lstate->event_r_idx];
6231 SLIST_REMOVE_HEAD(&lstate->immed_notifies, sim_links.sle);
6232 inot = (struct ccb_immed_notify *)ccbh;
6233 switch (event->event_type) {
6234 case EVENT_TYPE_BUS_RESET:
6235 ccbh->status = CAM_SCSI_BUS_RESET|CAM_DEV_QFRZN;
6238 ccbh->status = CAM_MESSAGE_RECV|CAM_DEV_QFRZN;
6239 inot->message_args[0] = event->event_type;
6240 inot->message_args[1] = event->event_arg;
6243 inot->initiator_id = event->initiator_id;
6244 inot->sense_len = 0;
6245 xpt_done((union ccb *)inot);
6246 lstate->event_r_idx++;
6247 if (lstate->event_r_idx == AHC_TMODE_EVENT_BUFFER_SIZE)
6248 lstate->event_r_idx = 0;
6253 /******************** Sequencer Program Patching/Download *********************/
6257 ahc_dumpseq(struct ahc_softc* ahc)
6261 ahc_outb(ahc, SEQCTL, PERRORDIS|FAILDIS|FASTMODE|LOADRAM);
6262 ahc_outb(ahc, SEQADDR0, 0);
6263 ahc_outb(ahc, SEQADDR1, 0);
6264 for (i = 0; i < ahc->instruction_ram_size; i++) {
6265 uint8_t ins_bytes[4];
6267 ahc_insb(ahc, SEQRAM, ins_bytes, 4);
6268 printf("0x%08x\n", ins_bytes[0] << 24
6269 | ins_bytes[1] << 16
6277 ahc_loadseq(struct ahc_softc *ahc)
6279 struct cs cs_table[num_critical_sections];
6280 u_int begin_set[num_critical_sections];
6281 u_int end_set[num_critical_sections];
6282 struct patch *cur_patch;
6287 u_int sg_prefetch_cnt;
6289 uint8_t download_consts[7];
6292 * Start out with 0 critical sections
6293 * that apply to this firmware load.
6297 memset(begin_set, 0, sizeof(begin_set));
6298 memset(end_set, 0, sizeof(end_set));
6300 /* Setup downloadable constant table */
6301 download_consts[QOUTFIFO_OFFSET] = 0;
6302 if (ahc->targetcmds != NULL)
6303 download_consts[QOUTFIFO_OFFSET] += 32;
6304 download_consts[QINFIFO_OFFSET] = download_consts[QOUTFIFO_OFFSET] + 1;
6305 download_consts[CACHESIZE_MASK] = ahc->pci_cachesize - 1;
6306 download_consts[INVERTED_CACHESIZE_MASK] = ~(ahc->pci_cachesize - 1);
6307 sg_prefetch_cnt = ahc->pci_cachesize;
6308 if (sg_prefetch_cnt < (2 * sizeof(struct ahc_dma_seg)))
6309 sg_prefetch_cnt = 2 * sizeof(struct ahc_dma_seg);
6310 download_consts[SG_PREFETCH_CNT] = sg_prefetch_cnt;
6311 download_consts[SG_PREFETCH_ALIGN_MASK] = ~(sg_prefetch_cnt - 1);
6312 download_consts[SG_PREFETCH_ADDR_MASK] = (sg_prefetch_cnt - 1);
6314 cur_patch = patches;
6317 ahc_outb(ahc, SEQCTL, PERRORDIS|FAILDIS|FASTMODE|LOADRAM);
6318 ahc_outb(ahc, SEQADDR0, 0);
6319 ahc_outb(ahc, SEQADDR1, 0);
6321 for (i = 0; i < sizeof(seqprog)/4; i++) {
6322 if (ahc_check_patch(ahc, &cur_patch, i, &skip_addr) == 0) {
6324 * Don't download this instruction as it
6325 * is in a patch that was removed.
6330 if (downloaded == ahc->instruction_ram_size) {
6332 * We're about to exceed the instruction
6333 * storage capacity for this chip. Fail
6336 printf("\n%s: Program too large for instruction memory "
6337 "size of %d!\n", ahc_name(ahc),
6338 ahc->instruction_ram_size);
6343 * Move through the CS table until we find a CS
6344 * that might apply to this instruction.
6346 for (; cur_cs < num_critical_sections; cur_cs++) {
6347 if (critical_sections[cur_cs].end <= i) {
6348 if (begin_set[cs_count] == TRUE
6349 && end_set[cs_count] == FALSE) {
6350 cs_table[cs_count].end = downloaded;
6351 end_set[cs_count] = TRUE;
6356 if (critical_sections[cur_cs].begin <= i
6357 && begin_set[cs_count] == FALSE) {
6358 cs_table[cs_count].begin = downloaded;
6359 begin_set[cs_count] = TRUE;
6363 ahc_download_instr(ahc, i, download_consts);
6367 ahc->num_critical_sections = cs_count;
6368 if (cs_count != 0) {
6370 cs_count *= sizeof(struct cs);
6371 ahc->critical_sections = malloc(cs_count, M_DEVBUF, M_NOWAIT);
6372 if (ahc->critical_sections == NULL)
6373 panic("ahc_loadseq: Could not malloc");
6374 memcpy(ahc->critical_sections, cs_table, cs_count);
6376 ahc_outb(ahc, SEQCTL, PERRORDIS|FAILDIS|FASTMODE);
6379 printf(" %d instructions downloaded\n", downloaded);
6380 printf("%s: Features 0x%x, Bugs 0x%x, Flags 0x%x\n",
6381 ahc_name(ahc), ahc->features, ahc->bugs, ahc->flags);
6387 ahc_check_patch(struct ahc_softc *ahc, struct patch **start_patch,
6388 u_int start_instr, u_int *skip_addr)
6390 struct patch *cur_patch;
6391 struct patch *last_patch;
6394 num_patches = ARRAY_SIZE(patches);
6395 last_patch = &patches[num_patches];
6396 cur_patch = *start_patch;
6398 while (cur_patch < last_patch && start_instr == cur_patch->begin) {
6400 if (cur_patch->patch_func(ahc) == 0) {
6402 /* Start rejecting code */
6403 *skip_addr = start_instr + cur_patch->skip_instr;
6404 cur_patch += cur_patch->skip_patch;
6406 /* Accepted this patch. Advance to the next
6407 * one and wait for our intruction pointer to
6414 *start_patch = cur_patch;
6415 if (start_instr < *skip_addr)
6416 /* Still skipping */
6423 ahc_download_instr(struct ahc_softc *ahc, u_int instrptr, uint8_t *dconsts)
6425 union ins_formats instr;
6426 struct ins_format1 *fmt1_ins;
6427 struct ins_format3 *fmt3_ins;
6431 * The firmware is always compiled into a little endian format.
6433 instr.integer = ahc_le32toh(*(uint32_t*)&seqprog[instrptr * 4]);
6435 fmt1_ins = &instr.format1;
6438 /* Pull the opcode */
6439 opcode = instr.format1.opcode;
6450 struct patch *cur_patch;
6456 fmt3_ins = &instr.format3;
6458 address = fmt3_ins->address;
6459 cur_patch = patches;
6462 for (i = 0; i < address;) {
6464 ahc_check_patch(ahc, &cur_patch, i, &skip_addr);
6466 if (skip_addr > i) {
6469 end_addr = min(address, skip_addr);
6470 address_offset += end_addr - i;
6476 address -= address_offset;
6477 fmt3_ins->address = address;
6486 if (fmt1_ins->parity != 0) {
6487 fmt1_ins->immediate = dconsts[fmt1_ins->immediate];
6489 fmt1_ins->parity = 0;
6490 if ((ahc->features & AHC_CMD_CHAN) == 0
6491 && opcode == AIC_OP_BMOV) {
6493 * Block move was added at the same time
6494 * as the command channel. Verify that
6495 * this is only a move of a single element
6496 * and convert the BMOV to a MOV
6497 * (AND with an immediate of FF).
6499 if (fmt1_ins->immediate != 1)
6500 panic("%s: BMOV not supported\n",
6502 fmt1_ins->opcode = AIC_OP_AND;
6503 fmt1_ins->immediate = 0xff;
6507 if ((ahc->features & AHC_ULTRA2) != 0) {
6510 /* Calculate odd parity for the instruction */
6511 for (i = 0, count = 0; i < 31; i++) {
6515 if ((instr.integer & mask) != 0)
6518 if ((count & 0x01) == 0)
6519 instr.format1.parity = 1;
6521 /* Compress the instruction for older sequencers */
6522 if (fmt3_ins != NULL) {
6525 | (fmt3_ins->source << 8)
6526 | (fmt3_ins->address << 16)
6527 | (fmt3_ins->opcode << 25);
6531 | (fmt1_ins->source << 8)
6532 | (fmt1_ins->destination << 16)
6533 | (fmt1_ins->ret << 24)
6534 | (fmt1_ins->opcode << 25);
6537 /* The sequencer is a little endian cpu */
6538 instr.integer = ahc_htole32(instr.integer);
6539 ahc_outsb(ahc, SEQRAM, instr.bytes, 4);
6542 panic("Unknown opcode encountered in seq program");
6548 ahc_print_register(ahc_reg_parse_entry_t *table, u_int num_entries,
6549 const char *name, u_int address, u_int value,
6550 u_int *cur_column, u_int wrap_point)
6555 if (cur_column != NULL && *cur_column >= wrap_point) {
6559 printed = printf("%s[0x%x]", name, value);
6560 if (table == NULL) {
6561 printed += printf(" ");
6562 *cur_column += printed;
6566 while (printed_mask != 0xFF) {
6569 for (entry = 0; entry < num_entries; entry++) {
6570 if (((value & table[entry].mask)
6571 != table[entry].value)
6572 || ((printed_mask & table[entry].mask)
6573 == table[entry].mask))
6576 printed += printf("%s%s",
6577 printed_mask == 0 ? ":(" : "|",
6579 printed_mask |= table[entry].mask;
6583 if (entry >= num_entries)
6586 if (printed_mask != 0)
6587 printed += printf(") ");
6589 printed += printf(" ");
6590 if (cur_column != NULL)
6591 *cur_column += printed;
6596 ahc_dump_card_state(struct ahc_softc *ahc)
6599 struct scb_tailq *untagged_q;
6610 uint8_t saved_scbptr;
6612 if (ahc_is_paused(ahc)) {
6619 saved_scbptr = ahc_inb(ahc, SCBPTR);
6620 last_phase = ahc_inb(ahc, LASTPHASE);
6621 printf(">>>>>>>>>>>>>>>>>> Dump Card State Begins <<<<<<<<<<<<<<<<<\n"
6622 "%s: Dumping Card State %s, at SEQADDR 0x%x\n",
6623 ahc_name(ahc), ahc_lookup_phase_entry(last_phase)->phasemsg,
6624 ahc_inb(ahc, SEQADDR0) | (ahc_inb(ahc, SEQADDR1) << 8));
6626 printf("Card was paused\n");
6627 printf("ACCUM = 0x%x, SINDEX = 0x%x, DINDEX = 0x%x, ARG_2 = 0x%x\n",
6628 ahc_inb(ahc, ACCUM), ahc_inb(ahc, SINDEX), ahc_inb(ahc, DINDEX),
6629 ahc_inb(ahc, ARG_2));
6630 printf("HCNT = 0x%x SCBPTR = 0x%x\n", ahc_inb(ahc, HCNT),
6631 ahc_inb(ahc, SCBPTR));
6633 if ((ahc->features & AHC_DT) != 0)
6634 ahc_scsiphase_print(ahc_inb(ahc, SCSIPHASE), &cur_col, 50);
6635 ahc_scsisigi_print(ahc_inb(ahc, SCSISIGI), &cur_col, 50);
6636 ahc_error_print(ahc_inb(ahc, ERROR), &cur_col, 50);
6637 ahc_scsibusl_print(ahc_inb(ahc, SCSIBUSL), &cur_col, 50);
6638 ahc_lastphase_print(ahc_inb(ahc, LASTPHASE), &cur_col, 50);
6639 ahc_scsiseq_print(ahc_inb(ahc, SCSISEQ), &cur_col, 50);
6640 ahc_sblkctl_print(ahc_inb(ahc, SBLKCTL), &cur_col, 50);
6641 ahc_scsirate_print(ahc_inb(ahc, SCSIRATE), &cur_col, 50);
6642 ahc_seqctl_print(ahc_inb(ahc, SEQCTL), &cur_col, 50);
6643 ahc_seq_flags_print(ahc_inb(ahc, SEQ_FLAGS), &cur_col, 50);
6644 ahc_sstat0_print(ahc_inb(ahc, SSTAT0), &cur_col, 50);
6645 ahc_sstat1_print(ahc_inb(ahc, SSTAT1), &cur_col, 50);
6646 ahc_sstat2_print(ahc_inb(ahc, SSTAT2), &cur_col, 50);
6647 ahc_sstat3_print(ahc_inb(ahc, SSTAT3), &cur_col, 50);
6648 ahc_simode0_print(ahc_inb(ahc, SIMODE0), &cur_col, 50);
6649 ahc_simode1_print(ahc_inb(ahc, SIMODE1), &cur_col, 50);
6650 ahc_sxfrctl0_print(ahc_inb(ahc, SXFRCTL0), &cur_col, 50);
6651 ahc_dfcntrl_print(ahc_inb(ahc, DFCNTRL), &cur_col, 50);
6652 ahc_dfstatus_print(ahc_inb(ahc, DFSTATUS), &cur_col, 50);
6656 for (i = 0; i < STACK_SIZE; i++)
6657 printf(" 0x%x", ahc_inb(ahc, STACK)|(ahc_inb(ahc, STACK) << 8));
6658 printf("\nSCB count = %d\n", ahc->scb_data->numscbs);
6659 printf("Kernel NEXTQSCB = %d\n", ahc->next_queued_scb->hscb->tag);
6660 printf("Card NEXTQSCB = %d\n", ahc_inb(ahc, NEXT_QUEUED_SCB));
6662 printf("QINFIFO entries: ");
6663 if ((ahc->features & AHC_QUEUE_REGS) != 0) {
6664 qinpos = ahc_inb(ahc, SNSCB_QOFF);
6665 ahc_outb(ahc, SNSCB_QOFF, qinpos);
6667 qinpos = ahc_inb(ahc, QINPOS);
6668 qintail = ahc->qinfifonext;
6669 while (qinpos != qintail) {
6670 printf("%d ", ahc->qinfifo[qinpos]);
6675 printf("Waiting Queue entries: ");
6676 scb_index = ahc_inb(ahc, WAITING_SCBH);
6678 while (scb_index != SCB_LIST_NULL && i++ < 256) {
6679 ahc_outb(ahc, SCBPTR, scb_index);
6680 printf("%d:%d ", scb_index, ahc_inb(ahc, SCB_TAG));
6681 scb_index = ahc_inb(ahc, SCB_NEXT);
6685 printf("Disconnected Queue entries: ");
6686 scb_index = ahc_inb(ahc, DISCONNECTED_SCBH);
6688 while (scb_index != SCB_LIST_NULL && i++ < 256) {
6689 ahc_outb(ahc, SCBPTR, scb_index);
6690 printf("%d:%d ", scb_index, ahc_inb(ahc, SCB_TAG));
6691 scb_index = ahc_inb(ahc, SCB_NEXT);
6695 ahc_sync_qoutfifo(ahc, BUS_DMASYNC_POSTREAD);
6696 printf("QOUTFIFO entries: ");
6697 qoutpos = ahc->qoutfifonext;
6699 while (ahc->qoutfifo[qoutpos] != SCB_LIST_NULL && i++ < 256) {
6700 printf("%d ", ahc->qoutfifo[qoutpos]);
6705 printf("Sequencer Free SCB List: ");
6706 scb_index = ahc_inb(ahc, FREE_SCBH);
6708 while (scb_index != SCB_LIST_NULL && i++ < 256) {
6709 ahc_outb(ahc, SCBPTR, scb_index);
6710 printf("%d ", scb_index);
6711 scb_index = ahc_inb(ahc, SCB_NEXT);
6715 printf("Sequencer SCB Info: ");
6716 for (i = 0; i < ahc->scb_data->maxhscbs; i++) {
6717 ahc_outb(ahc, SCBPTR, i);
6718 cur_col = printf("\n%3d ", i);
6720 ahc_scb_control_print(ahc_inb(ahc, SCB_CONTROL), &cur_col, 60);
6721 ahc_scb_scsiid_print(ahc_inb(ahc, SCB_SCSIID), &cur_col, 60);
6722 ahc_scb_lun_print(ahc_inb(ahc, SCB_LUN), &cur_col, 60);
6723 ahc_scb_tag_print(ahc_inb(ahc, SCB_TAG), &cur_col, 60);
6727 printf("Pending list: ");
6729 LIST_FOREACH(scb, &ahc->pending_scbs, pending_links) {
6732 cur_col = printf("\n%3d ", scb->hscb->tag);
6733 ahc_scb_control_print(scb->hscb->control, &cur_col, 60);
6734 ahc_scb_scsiid_print(scb->hscb->scsiid, &cur_col, 60);
6735 ahc_scb_lun_print(scb->hscb->lun, &cur_col, 60);
6736 if ((ahc->flags & AHC_PAGESCBS) == 0) {
6737 ahc_outb(ahc, SCBPTR, scb->hscb->tag);
6739 ahc_scb_control_print(ahc_inb(ahc, SCB_CONTROL),
6741 ahc_scb_tag_print(ahc_inb(ahc, SCB_TAG), &cur_col, 60);
6747 printf("Kernel Free SCB list: ");
6749 SLIST_FOREACH(scb, &ahc->scb_data->free_scbs, links.sle) {
6752 printf("%d ", scb->hscb->tag);
6756 maxtarget = (ahc->features & (AHC_WIDE|AHC_TWIN)) ? 15 : 7;
6757 for (target = 0; target <= maxtarget; target++) {
6758 untagged_q = &ahc->untagged_queues[target];
6759 if (TAILQ_FIRST(untagged_q) == NULL)
6761 printf("Untagged Q(%d): ", target);
6763 TAILQ_FOREACH(scb, untagged_q, links.tqe) {
6766 printf("%d ", scb->hscb->tag);
6771 ahc_platform_dump_card_state(ahc);
6772 printf("\n<<<<<<<<<<<<<<<<< Dump Card State Ends >>>>>>>>>>>>>>>>>>\n");
6773 ahc_outb(ahc, SCBPTR, saved_scbptr);
6778 /************************* Target Mode ****************************************/
6779 #ifdef AHC_TARGET_MODE
6781 ahc_find_tmode_devs(struct ahc_softc *ahc, struct cam_sim *sim, union ccb *ccb,
6782 struct ahc_tmode_tstate **tstate,
6783 struct ahc_tmode_lstate **lstate,
6784 int notfound_failure)
6787 if ((ahc->features & AHC_TARGETMODE) == 0)
6788 return (CAM_REQ_INVALID);
6791 * Handle the 'black hole' device that sucks up
6792 * requests to unattached luns on enabled targets.
6794 if (ccb->ccb_h.target_id == CAM_TARGET_WILDCARD
6795 && ccb->ccb_h.target_lun == CAM_LUN_WILDCARD) {
6797 *lstate = ahc->black_hole;
6801 max_id = (ahc->features & AHC_WIDE) ? 16 : 8;
6802 if (ccb->ccb_h.target_id >= max_id)
6803 return (CAM_TID_INVALID);
6805 if (ccb->ccb_h.target_lun >= AHC_NUM_LUNS)
6806 return (CAM_LUN_INVALID);
6808 *tstate = ahc->enabled_targets[ccb->ccb_h.target_id];
6810 if (*tstate != NULL)
6812 (*tstate)->enabled_luns[ccb->ccb_h.target_lun];
6815 if (notfound_failure != 0 && *lstate == NULL)
6816 return (CAM_PATH_INVALID);
6818 return (CAM_REQ_CMP);
6822 ahc_handle_en_lun(struct ahc_softc *ahc, struct cam_sim *sim, union ccb *ccb)
6824 struct ahc_tmode_tstate *tstate;
6825 struct ahc_tmode_lstate *lstate;
6826 struct ccb_en_lun *cel;
6836 status = ahc_find_tmode_devs(ahc, sim, ccb, &tstate, &lstate,
6837 /*notfound_failure*/FALSE);
6839 if (status != CAM_REQ_CMP) {
6840 ccb->ccb_h.status = status;
6844 if (cam_sim_bus(sim) == 0)
6845 our_id = ahc->our_id;
6847 our_id = ahc->our_id_b;
6849 if (ccb->ccb_h.target_id != our_id) {
6851 * our_id represents our initiator ID, or
6852 * the ID of the first target to have an
6853 * enabled lun in target mode. There are
6854 * two cases that may preclude enabling a
6855 * target id other than our_id.
6857 * o our_id is for an active initiator role.
6858 * Since the hardware does not support
6859 * reselections to the initiator role at
6860 * anything other than our_id, and our_id
6861 * is used by the hardware to indicate the
6862 * ID to use for both select-out and
6863 * reselect-out operations, the only target
6864 * ID we can support in this mode is our_id.
6866 * o The MULTARGID feature is not available and
6867 * a previous target mode ID has been enabled.
6869 if ((ahc->features & AHC_MULTIROLE) != 0) {
6871 if ((ahc->features & AHC_MULTI_TID) != 0
6872 && (ahc->flags & AHC_INITIATORROLE) != 0) {
6874 * Only allow additional targets if
6875 * the initiator role is disabled.
6876 * The hardware cannot handle a re-select-in
6877 * on the initiator id during a re-select-out
6878 * on a different target id.
6880 status = CAM_TID_INVALID;
6881 } else if ((ahc->flags & AHC_INITIATORROLE) != 0
6882 || ahc->enabled_luns > 0) {
6884 * Only allow our target id to change
6885 * if the initiator role is not configured
6886 * and there are no enabled luns which
6887 * are attached to the currently registered
6890 status = CAM_TID_INVALID;
6892 } else if ((ahc->features & AHC_MULTI_TID) == 0
6893 && ahc->enabled_luns > 0) {
6895 status = CAM_TID_INVALID;
6899 if (status != CAM_REQ_CMP) {
6900 ccb->ccb_h.status = status;
6905 * We now have an id that is valid.
6906 * If we aren't in target mode, switch modes.
6908 if ((ahc->flags & AHC_TARGETROLE) == 0
6909 && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
6911 ahc_flag saved_flags;
6913 printf("Configuring Target Mode\n");
6915 if (LIST_FIRST(&ahc->pending_scbs) != NULL) {
6916 ccb->ccb_h.status = CAM_BUSY;
6917 ahc_unlock(ahc, &s);
6920 saved_flags = ahc->flags;
6921 ahc->flags |= AHC_TARGETROLE;
6922 if ((ahc->features & AHC_MULTIROLE) == 0)
6923 ahc->flags &= ~AHC_INITIATORROLE;
6925 error = ahc_loadseq(ahc);
6928 * Restore original configuration and notify
6929 * the caller that we cannot support target mode.
6930 * Since the adapter started out in this
6931 * configuration, the firmware load will succeed,
6932 * so there is no point in checking ahc_loadseq's
6935 ahc->flags = saved_flags;
6936 (void)ahc_loadseq(ahc);
6938 ahc_unlock(ahc, &s);
6939 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
6943 ahc_unlock(ahc, &s);
6946 target = ccb->ccb_h.target_id;
6947 lun = ccb->ccb_h.target_lun;
6948 channel = SIM_CHANNEL(ahc, sim);
6949 target_mask = 0x01 << target;
6953 if (cel->enable != 0) {
6956 /* Are we already enabled?? */
6957 if (lstate != NULL) {
6958 xpt_print_path(ccb->ccb_h.path);
6959 printf("Lun already enabled\n");
6960 ccb->ccb_h.status = CAM_LUN_ALRDY_ENA;
6964 if (cel->grp6_len != 0
6965 || cel->grp7_len != 0) {
6967 * Don't (yet?) support vendor
6968 * specific commands.
6970 ccb->ccb_h.status = CAM_REQ_INVALID;
6971 printf("Non-zero Group Codes\n");
6977 * Setup our data structures.
6979 if (target != CAM_TARGET_WILDCARD && tstate == NULL) {
6980 tstate = ahc_alloc_tstate(ahc, target, channel);
6981 if (tstate == NULL) {
6982 xpt_print_path(ccb->ccb_h.path);
6983 printf("Couldn't allocate tstate\n");
6984 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
6988 lstate = malloc(sizeof(*lstate), M_DEVBUF, M_NOWAIT);
6989 if (lstate == NULL) {
6990 xpt_print_path(ccb->ccb_h.path);
6991 printf("Couldn't allocate lstate\n");
6992 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
6995 memset(lstate, 0, sizeof(*lstate));
6996 status = xpt_create_path(&lstate->path, /*periph*/NULL,
6997 xpt_path_path_id(ccb->ccb_h.path),
6998 xpt_path_target_id(ccb->ccb_h.path),
6999 xpt_path_lun_id(ccb->ccb_h.path));
7000 if (status != CAM_REQ_CMP) {
7001 free(lstate, M_DEVBUF);
7002 xpt_print_path(ccb->ccb_h.path);
7003 printf("Couldn't allocate path\n");
7004 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
7007 SLIST_INIT(&lstate->accept_tios);
7008 SLIST_INIT(&lstate->immed_notifies);
7011 if (target != CAM_TARGET_WILDCARD) {
7012 tstate->enabled_luns[lun] = lstate;
7013 ahc->enabled_luns++;
7015 if ((ahc->features & AHC_MULTI_TID) != 0) {
7018 targid_mask = ahc_inb(ahc, TARGID)
7019 | (ahc_inb(ahc, TARGID + 1) << 8);
7021 targid_mask |= target_mask;
7022 ahc_outb(ahc, TARGID, targid_mask);
7023 ahc_outb(ahc, TARGID+1, (targid_mask >> 8));
7025 ahc_update_scsiid(ahc, targid_mask);
7030 channel = SIM_CHANNEL(ahc, sim);
7031 our_id = SIM_SCSI_ID(ahc, sim);
7034 * This can only happen if selections
7037 if (target != our_id) {
7042 sblkctl = ahc_inb(ahc, SBLKCTL);
7043 cur_channel = (sblkctl & SELBUSB)
7045 if ((ahc->features & AHC_TWIN) == 0)
7047 swap = cur_channel != channel;
7049 ahc->our_id = target;
7051 ahc->our_id_b = target;
7054 ahc_outb(ahc, SBLKCTL,
7057 ahc_outb(ahc, SCSIID, target);
7060 ahc_outb(ahc, SBLKCTL, sblkctl);
7064 ahc->black_hole = lstate;
7065 /* Allow select-in operations */
7066 if (ahc->black_hole != NULL && ahc->enabled_luns > 0) {
7067 scsiseq = ahc_inb(ahc, SCSISEQ_TEMPLATE);
7069 ahc_outb(ahc, SCSISEQ_TEMPLATE, scsiseq);
7070 scsiseq = ahc_inb(ahc, SCSISEQ);
7072 ahc_outb(ahc, SCSISEQ, scsiseq);
7075 ahc_unlock(ahc, &s);
7076 ccb->ccb_h.status = CAM_REQ_CMP;
7077 xpt_print_path(ccb->ccb_h.path);
7078 printf("Lun now enabled for target mode\n");
7083 if (lstate == NULL) {
7084 ccb->ccb_h.status = CAM_LUN_INVALID;
7090 ccb->ccb_h.status = CAM_REQ_CMP;
7091 LIST_FOREACH(scb, &ahc->pending_scbs, pending_links) {
7092 struct ccb_hdr *ccbh;
7094 ccbh = &scb->io_ctx->ccb_h;
7095 if (ccbh->func_code == XPT_CONT_TARGET_IO
7096 && !xpt_path_comp(ccbh->path, ccb->ccb_h.path)){
7097 printf("CTIO pending\n");
7098 ccb->ccb_h.status = CAM_REQ_INVALID;
7099 ahc_unlock(ahc, &s);
7104 if (SLIST_FIRST(&lstate->accept_tios) != NULL) {
7105 printf("ATIOs pending\n");
7106 ccb->ccb_h.status = CAM_REQ_INVALID;
7109 if (SLIST_FIRST(&lstate->immed_notifies) != NULL) {
7110 printf("INOTs pending\n");
7111 ccb->ccb_h.status = CAM_REQ_INVALID;
7114 if (ccb->ccb_h.status != CAM_REQ_CMP) {
7115 ahc_unlock(ahc, &s);
7119 xpt_print_path(ccb->ccb_h.path);
7120 printf("Target mode disabled\n");
7121 xpt_free_path(lstate->path);
7122 free(lstate, M_DEVBUF);
7125 /* Can we clean up the target too? */
7126 if (target != CAM_TARGET_WILDCARD) {
7127 tstate->enabled_luns[lun] = NULL;
7128 ahc->enabled_luns--;
7129 for (empty = 1, i = 0; i < 8; i++)
7130 if (tstate->enabled_luns[i] != NULL) {
7136 ahc_free_tstate(ahc, target, channel,
7138 if (ahc->features & AHC_MULTI_TID) {
7141 targid_mask = ahc_inb(ahc, TARGID)
7142 | (ahc_inb(ahc, TARGID + 1)
7145 targid_mask &= ~target_mask;
7146 ahc_outb(ahc, TARGID, targid_mask);
7147 ahc_outb(ahc, TARGID+1,
7148 (targid_mask >> 8));
7149 ahc_update_scsiid(ahc, targid_mask);
7154 ahc->black_hole = NULL;
7157 * We can't allow selections without
7158 * our black hole device.
7162 if (ahc->enabled_luns == 0) {
7163 /* Disallow select-in */
7166 scsiseq = ahc_inb(ahc, SCSISEQ_TEMPLATE);
7168 ahc_outb(ahc, SCSISEQ_TEMPLATE, scsiseq);
7169 scsiseq = ahc_inb(ahc, SCSISEQ);
7171 ahc_outb(ahc, SCSISEQ, scsiseq);
7173 if ((ahc->features & AHC_MULTIROLE) == 0) {
7174 printf("Configuring Initiator Mode\n");
7175 ahc->flags &= ~AHC_TARGETROLE;
7176 ahc->flags |= AHC_INITIATORROLE;
7178 * Returning to a configuration that
7179 * fit previously will always succeed.
7181 (void)ahc_loadseq(ahc);
7184 * Unpaused. The extra unpause
7185 * that follows is harmless.
7190 ahc_unlock(ahc, &s);
7195 ahc_update_scsiid(struct ahc_softc *ahc, u_int targid_mask)
7200 if ((ahc->features & AHC_MULTI_TID) == 0)
7201 panic("ahc_update_scsiid called on non-multitid unit\n");
7204 * Since we will rely on the TARGID mask
7205 * for selection enables, ensure that OID
7206 * in SCSIID is not set to some other ID
7207 * that we don't want to allow selections on.
7209 if ((ahc->features & AHC_ULTRA2) != 0)
7210 scsiid = ahc_inb(ahc, SCSIID_ULTRA2);
7212 scsiid = ahc_inb(ahc, SCSIID);
7213 scsiid_mask = 0x1 << (scsiid & OID);
7214 if ((targid_mask & scsiid_mask) == 0) {
7217 /* ffs counts from 1 */
7218 our_id = ffs(targid_mask);
7220 our_id = ahc->our_id;
7226 if ((ahc->features & AHC_ULTRA2) != 0)
7227 ahc_outb(ahc, SCSIID_ULTRA2, scsiid);
7229 ahc_outb(ahc, SCSIID, scsiid);
7233 ahc_run_tqinfifo(struct ahc_softc *ahc, int paused)
7235 struct target_cmd *cmd;
7238 * If the card supports auto-access pause,
7239 * we can access the card directly regardless
7240 * of whether it is paused or not.
7242 if ((ahc->features & AHC_AUTOPAUSE) != 0)
7245 ahc_sync_tqinfifo(ahc, BUS_DMASYNC_POSTREAD);
7246 while ((cmd = &ahc->targetcmds[ahc->tqinfifonext])->cmd_valid != 0) {
7249 * Only advance through the queue if we
7250 * have the resources to process the command.
7252 if (ahc_handle_target_cmd(ahc, cmd) != 0)
7256 ahc_dmamap_sync(ahc, ahc->shared_data_dmat,
7257 ahc->shared_data_dmamap,
7258 ahc_targetcmd_offset(ahc, ahc->tqinfifonext),
7259 sizeof(struct target_cmd),
7260 BUS_DMASYNC_PREREAD);
7261 ahc->tqinfifonext++;
7264 * Lazily update our position in the target mode incoming
7265 * command queue as seen by the sequencer.
7267 if ((ahc->tqinfifonext & (HOST_TQINPOS - 1)) == 1) {
7268 if ((ahc->features & AHC_HS_MAILBOX) != 0) {
7271 hs_mailbox = ahc_inb(ahc, HS_MAILBOX);
7272 hs_mailbox &= ~HOST_TQINPOS;
7273 hs_mailbox |= ahc->tqinfifonext & HOST_TQINPOS;
7274 ahc_outb(ahc, HS_MAILBOX, hs_mailbox);
7278 ahc_outb(ahc, KERNEL_TQINPOS,
7279 ahc->tqinfifonext & HOST_TQINPOS);
7288 ahc_handle_target_cmd(struct ahc_softc *ahc, struct target_cmd *cmd)
7290 struct ahc_tmode_tstate *tstate;
7291 struct ahc_tmode_lstate *lstate;
7292 struct ccb_accept_tio *atio;
7298 initiator = SCSIID_TARGET(ahc, cmd->scsiid);
7299 target = SCSIID_OUR_ID(cmd->scsiid);
7300 lun = (cmd->identify & MSG_IDENTIFY_LUNMASK);
7303 tstate = ahc->enabled_targets[target];
7306 lstate = tstate->enabled_luns[lun];
7309 * Commands for disabled luns go to the black hole driver.
7312 lstate = ahc->black_hole;
7314 atio = (struct ccb_accept_tio*)SLIST_FIRST(&lstate->accept_tios);
7316 ahc->flags |= AHC_TQINFIFO_BLOCKED;
7318 * Wait for more ATIOs from the peripheral driver for this lun.
7321 printf("%s: ATIOs exhausted\n", ahc_name(ahc));
7324 ahc->flags &= ~AHC_TQINFIFO_BLOCKED;
7326 printf("Incoming command from %d for %d:%d%s\n",
7327 initiator, target, lun,
7328 lstate == ahc->black_hole ? "(Black Holed)" : "");
7330 SLIST_REMOVE_HEAD(&lstate->accept_tios, sim_links.sle);
7332 if (lstate == ahc->black_hole) {
7333 /* Fill in the wildcards */
7334 atio->ccb_h.target_id = target;
7335 atio->ccb_h.target_lun = lun;
7339 * Package it up and send it off to
7340 * whomever has this lun enabled.
7342 atio->sense_len = 0;
7343 atio->init_id = initiator;
7344 if (byte[0] != 0xFF) {
7345 /* Tag was included */
7346 atio->tag_action = *byte++;
7347 atio->tag_id = *byte++;
7348 atio->ccb_h.flags = CAM_TAG_ACTION_VALID;
7350 atio->ccb_h.flags = 0;
7354 /* Okay. Now determine the cdb size based on the command code */
7355 switch (*byte >> CMD_GROUP_CODE_SHIFT) {
7371 /* Only copy the opcode. */
7373 printf("Reserved or VU command code type encountered\n");
7377 memcpy(atio->cdb_io.cdb_bytes, byte, atio->cdb_len);
7379 atio->ccb_h.status |= CAM_CDB_RECVD;
7381 if ((cmd->identify & MSG_IDENTIFY_DISCFLAG) == 0) {
7383 * We weren't allowed to disconnect.
7384 * We're hanging on the bus until a
7385 * continue target I/O comes in response
7386 * to this accept tio.
7389 printf("Received Immediate Command %d:%d:%d - %p\n",
7390 initiator, target, lun, ahc->pending_device);
7392 ahc->pending_device = lstate;
7393 ahc_freeze_ccb((union ccb *)atio);
7394 atio->ccb_h.flags |= CAM_DIS_DISCONNECT;
7396 xpt_done((union ccb*)atio);