2 * Core routines and tables shareable across OS platforms.
4 * Copyright (c) 1994-2002 Justin T. Gibbs.
5 * Copyright (c) 2000-2003 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/aic79xx.c#238 $
42 * $FreeBSD: src/sys/dev/aic7xxx/aic79xx.c,v 1.28 2004/02/04 16:38:38 gibbs Exp $
43 * $DragonFly: src/sys/dev/disk/aic7xxx/aic79xx.c,v 1.19 2007/07/06 00:56:38 pavalos Exp $
46 #include "aic79xx_osm.h"
47 #include "aic79xx_inline.h"
48 #include "aicasm/aicasm_insformat.h"
50 /******************************** Globals *************************************/
51 struct ahd_softc_tailq ahd_tailq = TAILQ_HEAD_INITIALIZER(ahd_tailq);
52 uint32_t ahd_attach_to_HostRAID_controllers = 1;
54 /***************************** Lookup Tables **********************************/
55 char *ahd_chip_names[] =
62 static const u_int num_chip_names = NUM_ELEMENTS(ahd_chip_names);
65 * Hardware error codes.
67 struct ahd_hard_error_entry {
72 static struct ahd_hard_error_entry ahd_hard_errors[] = {
73 { DSCTMOUT, "Discard Timer has timed out" },
74 { ILLOPCODE, "Illegal Opcode in sequencer program" },
75 { SQPARERR, "Sequencer Parity Error" },
76 { DPARERR, "Data-path Parity Error" },
77 { MPARERR, "Scratch or SCB Memory Parity Error" },
78 { CIOPARERR, "CIOBUS Parity Error" },
80 static const u_int num_errors = NUM_ELEMENTS(ahd_hard_errors);
82 static struct ahd_phase_table_entry ahd_phase_table[] =
84 { P_DATAOUT, MSG_NOOP, "in Data-out phase" },
85 { P_DATAIN, MSG_INITIATOR_DET_ERR, "in Data-in phase" },
86 { P_DATAOUT_DT, MSG_NOOP, "in DT Data-out phase" },
87 { P_DATAIN_DT, MSG_INITIATOR_DET_ERR, "in DT Data-in phase" },
88 { P_COMMAND, MSG_NOOP, "in Command phase" },
89 { P_MESGOUT, MSG_NOOP, "in Message-out phase" },
90 { P_STATUS, MSG_INITIATOR_DET_ERR, "in Status phase" },
91 { P_MESGIN, MSG_PARITY_ERROR, "in Message-in phase" },
92 { P_BUSFREE, MSG_NOOP, "while idle" },
93 { 0, MSG_NOOP, "in unknown phase" }
97 * In most cases we only wish to itterate over real phases, so
98 * exclude the last element from the count.
100 static const u_int num_phases = NUM_ELEMENTS(ahd_phase_table) - 1;
102 /* Our Sequencer Program */
103 #include "aic79xx_seq.h"
105 /**************************** Function Declarations ***************************/
106 static void ahd_handle_transmission_error(struct ahd_softc *ahd);
107 static void ahd_handle_lqiphase_error(struct ahd_softc *ahd,
109 static int ahd_handle_pkt_busfree(struct ahd_softc *ahd,
111 static int ahd_handle_nonpkt_busfree(struct ahd_softc *ahd);
112 static void ahd_handle_proto_violation(struct ahd_softc *ahd);
113 static void ahd_force_renegotiation(struct ahd_softc *ahd,
114 struct ahd_devinfo *devinfo);
116 static struct ahd_tmode_tstate*
117 ahd_alloc_tstate(struct ahd_softc *ahd,
118 u_int scsi_id, char channel);
119 #ifdef AHD_TARGET_MODE
120 static void ahd_free_tstate(struct ahd_softc *ahd,
121 u_int scsi_id, char channel, int force);
123 static void ahd_devlimited_syncrate(struct ahd_softc *ahd,
124 struct ahd_initiator_tinfo *,
128 static void ahd_update_neg_table(struct ahd_softc *ahd,
129 struct ahd_devinfo *devinfo,
130 struct ahd_transinfo *tinfo);
131 static void ahd_update_pending_scbs(struct ahd_softc *ahd);
132 static void ahd_fetch_devinfo(struct ahd_softc *ahd,
133 struct ahd_devinfo *devinfo);
134 static void ahd_scb_devinfo(struct ahd_softc *ahd,
135 struct ahd_devinfo *devinfo,
137 static void ahd_setup_initiator_msgout(struct ahd_softc *ahd,
138 struct ahd_devinfo *devinfo,
140 static void ahd_build_transfer_msg(struct ahd_softc *ahd,
141 struct ahd_devinfo *devinfo);
142 static void ahd_construct_sdtr(struct ahd_softc *ahd,
143 struct ahd_devinfo *devinfo,
144 u_int period, u_int offset);
145 static void ahd_construct_wdtr(struct ahd_softc *ahd,
146 struct ahd_devinfo *devinfo,
148 static void ahd_construct_ppr(struct ahd_softc *ahd,
149 struct ahd_devinfo *devinfo,
150 u_int period, u_int offset,
151 u_int bus_width, u_int ppr_options);
152 static void ahd_clear_msg_state(struct ahd_softc *ahd);
153 static void ahd_handle_message_phase(struct ahd_softc *ahd);
159 static int ahd_sent_msg(struct ahd_softc *ahd, ahd_msgtype type,
160 u_int msgval, int full);
161 static int ahd_parse_msg(struct ahd_softc *ahd,
162 struct ahd_devinfo *devinfo);
163 static int ahd_handle_msg_reject(struct ahd_softc *ahd,
164 struct ahd_devinfo *devinfo);
165 static void ahd_handle_ign_wide_residue(struct ahd_softc *ahd,
166 struct ahd_devinfo *devinfo);
167 static void ahd_reinitialize_dataptrs(struct ahd_softc *ahd);
168 static void ahd_handle_devreset(struct ahd_softc *ahd,
169 struct ahd_devinfo *devinfo,
170 u_int lun, cam_status status,
171 char *message, int verbose_level);
173 static void ahd_setup_target_msgin(struct ahd_softc *ahd,
174 struct ahd_devinfo *devinfo,
178 static u_int ahd_sglist_size(struct ahd_softc *ahd);
179 static u_int ahd_sglist_allocsize(struct ahd_softc *ahd);
180 static bus_dmamap_callback_t
182 static void ahd_initialize_hscbs(struct ahd_softc *ahd);
183 static int ahd_init_scbdata(struct ahd_softc *ahd);
184 static void ahd_fini_scbdata(struct ahd_softc *ahd);
185 static void ahd_setup_iocell_workaround(struct ahd_softc *ahd);
186 static void ahd_iocell_first_selection(struct ahd_softc *ahd);
187 static void ahd_add_col_list(struct ahd_softc *ahd,
188 struct scb *scb, u_int col_idx);
189 static void ahd_rem_col_list(struct ahd_softc *ahd,
191 static void ahd_chip_init(struct ahd_softc *ahd);
192 static void ahd_qinfifo_requeue(struct ahd_softc *ahd,
193 struct scb *prev_scb,
195 static int ahd_qinfifo_count(struct ahd_softc *ahd);
196 static int ahd_search_scb_list(struct ahd_softc *ahd, int target,
197 char channel, int lun, u_int tag,
198 role_t role, uint32_t status,
199 ahd_search_action action,
200 u_int *list_head, u_int tid);
201 static void ahd_stitch_tid_list(struct ahd_softc *ahd,
202 u_int tid_prev, u_int tid_cur,
204 static void ahd_add_scb_to_free_list(struct ahd_softc *ahd,
206 static u_int ahd_rem_wscb(struct ahd_softc *ahd, u_int scbid,
207 u_int prev, u_int next, u_int tid);
208 static void ahd_reset_current_bus(struct ahd_softc *ahd);
209 static ahd_callback_t ahd_reset_poll;
210 static ahd_callback_t ahd_stat_timer;
212 static void ahd_dumpseq(struct ahd_softc *ahd);
214 static void ahd_loadseq(struct ahd_softc *ahd);
215 static int ahd_check_patch(struct ahd_softc *ahd,
216 struct patch **start_patch,
217 u_int start_instr, u_int *skip_addr);
218 static u_int ahd_resolve_seqaddr(struct ahd_softc *ahd,
220 static void ahd_download_instr(struct ahd_softc *ahd,
221 u_int instrptr, uint8_t *dconsts);
222 static int ahd_probe_stack_size(struct ahd_softc *ahd);
223 static void ahd_other_scb_timeout(struct ahd_softc *ahd,
225 struct scb *other_scb);
226 static int ahd_scb_active_in_fifo(struct ahd_softc *ahd,
228 static void ahd_run_data_fifo(struct ahd_softc *ahd,
231 #ifdef AHD_TARGET_MODE
232 static void ahd_queue_lstate_event(struct ahd_softc *ahd,
233 struct ahd_tmode_lstate *lstate,
237 static void ahd_update_scsiid(struct ahd_softc *ahd,
239 static int ahd_handle_target_cmd(struct ahd_softc *ahd,
240 struct target_cmd *cmd);
243 /******************************** Private Inlines *****************************/
244 static __inline void ahd_assert_atn(struct ahd_softc *ahd);
245 static __inline int ahd_currently_packetized(struct ahd_softc *ahd);
246 static __inline int ahd_set_active_fifo(struct ahd_softc *ahd);
249 ahd_assert_atn(struct ahd_softc *ahd)
251 ahd_outb(ahd, SCSISIGO, ATNO);
255 * Determine if the current connection has a packetized
256 * agreement. This does not necessarily mean that we
257 * are currently in a packetized transfer. We could
258 * just as easily be sending or receiving a message.
261 ahd_currently_packetized(struct ahd_softc *ahd)
263 ahd_mode_state saved_modes;
266 saved_modes = ahd_save_modes(ahd);
267 if ((ahd->bugs & AHD_PKTIZED_STATUS_BUG) != 0) {
269 * The packetized bit refers to the last
270 * connection, not the current one. Check
271 * for non-zero LQISTATE instead.
273 ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
274 packetized = ahd_inb(ahd, LQISTATE) != 0;
276 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
277 packetized = ahd_inb(ahd, LQISTAT2) & PACKETIZED;
279 ahd_restore_modes(ahd, saved_modes);
284 ahd_set_active_fifo(struct ahd_softc *ahd)
288 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
289 active_fifo = ahd_inb(ahd, DFFSTAT) & CURRFIFO;
290 switch (active_fifo) {
293 ahd_set_modes(ahd, active_fifo, active_fifo);
300 /************************* Sequencer Execution Control ************************/
302 * Restart the sequencer program from address zero
305 ahd_restart(struct ahd_softc *ahd)
310 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
312 /* No more pending messages */
313 ahd_clear_msg_state(ahd);
314 ahd_outb(ahd, SCSISIGO, 0); /* De-assert BSY */
315 ahd_outb(ahd, MSG_OUT, MSG_NOOP); /* No message to send */
316 ahd_outb(ahd, SXFRCTL1, ahd_inb(ahd, SXFRCTL1) & ~BITBUCKET);
317 ahd_outb(ahd, SEQINTCTL, 0);
318 ahd_outb(ahd, LASTPHASE, P_BUSFREE);
319 ahd_outb(ahd, SEQ_FLAGS, 0);
320 ahd_outb(ahd, SAVED_SCSIID, 0xFF);
321 ahd_outb(ahd, SAVED_LUN, 0xFF);
324 * Ensure that the sequencer's idea of TQINPOS
325 * matches our own. The sequencer increments TQINPOS
326 * only after it sees a DMA complete and a reset could
327 * occur before the increment leaving the kernel to believe
328 * the command arrived but the sequencer to not.
330 ahd_outb(ahd, TQINPOS, ahd->tqinfifonext);
332 /* Always allow reselection */
333 ahd_outb(ahd, SCSISEQ1,
334 ahd_inb(ahd, SCSISEQ_TEMPLATE) & (ENSELI|ENRSELI|ENAUTOATNP));
335 ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
336 ahd_outb(ahd, SEQCTL0, FASTMODE|SEQRESET);
341 ahd_clear_fifo(struct ahd_softc *ahd, u_int fifo)
343 ahd_mode_state saved_modes;
346 if ((ahd_debug & AHD_SHOW_FIFOS) != 0)
347 kprintf("%s: Clearing FIFO %d\n", ahd_name(ahd), fifo);
349 saved_modes = ahd_save_modes(ahd);
350 ahd_set_modes(ahd, fifo, fifo);
351 ahd_outb(ahd, DFFSXFRCTL, RSTCHN|CLRSHCNT);
352 if ((ahd_inb(ahd, SG_STATE) & FETCH_INPROG) != 0)
353 ahd_outb(ahd, CCSGCTL, CCSGRESET);
354 ahd_outb(ahd, LONGJMP_ADDR + 1, INVALID_ADDR);
355 ahd_outb(ahd, SG_STATE, 0);
356 ahd_restore_modes(ahd, saved_modes);
359 /************************* Input/Output Queues ********************************/
361 * Flush and completed commands that are sitting in the command
362 * complete queues down on the chip but have yet to be dma'ed back up.
365 ahd_flush_qoutfifo(struct ahd_softc *ahd)
368 ahd_mode_state saved_modes;
374 saved_modes = ahd_save_modes(ahd);
377 * Flush the good status FIFO for completed packetized commands.
379 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
380 saved_scbptr = ahd_get_scbptr(ahd);
381 while ((ahd_inb(ahd, LQISTAT2) & LQIGSAVAIL) != 0) {
385 scbid = ahd_inw(ahd, GSFIFO);
386 scb = ahd_lookup_scb(ahd, scbid);
388 kprintf("%s: Warning - GSFIFO SCB %d invalid\n",
389 ahd_name(ahd), scbid);
393 * Determine if this transaction is still active in
394 * any FIFO. If it is, we must flush that FIFO to
395 * the host before completing the command.
399 for (i = 0; i < 2; i++) {
400 /* Toggle to the other mode. */
402 ahd_set_modes(ahd, fifo_mode, fifo_mode);
404 if (ahd_scb_active_in_fifo(ahd, scb) == 0)
407 ahd_run_data_fifo(ahd, scb);
410 * Running this FIFO may cause a CFG4DATA for
411 * this same transaction to assert in the other
412 * FIFO or a new snapshot SAVEPTRS interrupt
413 * in this FIFO. Even running a FIFO may not
414 * clear the transaction if we are still waiting
415 * for data to drain to the host. We must loop
416 * until the transaction is not active in either
417 * FIFO just to be sure. Reset our loop counter
418 * so we will visit both FIFOs again before
419 * declaring this transaction finished. We
420 * also delay a bit so that status has a chance
421 * to change before we look at this FIFO again.
426 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
427 ahd_set_scbptr(ahd, scbid);
428 if ((ahd_inb_scbram(ahd, SCB_SGPTR) & SG_LIST_NULL) == 0
429 && ((ahd_inb_scbram(ahd, SCB_SGPTR) & SG_FULL_RESID) != 0
430 || (ahd_inb_scbram(ahd, SCB_RESIDUAL_SGPTR)
431 & SG_LIST_NULL) != 0)) {
435 * The transfer completed with a residual.
436 * Place this SCB on the complete DMA list
437 * so that we update our in-core copy of the
438 * SCB before completing the command.
440 ahd_outb(ahd, SCB_SCSI_STATUS, 0);
441 ahd_outb(ahd, SCB_SGPTR,
442 ahd_inb_scbram(ahd, SCB_SGPTR)
444 ahd_outw(ahd, SCB_TAG, scbid);
445 ahd_outw(ahd, SCB_NEXT_COMPLETE, SCB_LIST_NULL);
446 comp_head = ahd_inw(ahd, COMPLETE_DMA_SCB_HEAD);
447 if (SCBID_IS_NULL(comp_head)) {
448 ahd_outw(ahd, COMPLETE_DMA_SCB_HEAD, scbid);
449 ahd_outw(ahd, COMPLETE_DMA_SCB_TAIL, scbid);
453 tail = ahd_inw(ahd, COMPLETE_DMA_SCB_TAIL);
454 ahd_set_scbptr(ahd, tail);
455 ahd_outw(ahd, SCB_NEXT_COMPLETE, scbid);
456 ahd_outw(ahd, COMPLETE_DMA_SCB_TAIL, scbid);
457 ahd_set_scbptr(ahd, scbid);
460 ahd_complete_scb(ahd, scb);
462 ahd_set_scbptr(ahd, saved_scbptr);
465 * Setup for command channel portion of flush.
467 ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
470 * Wait for any inprogress DMA to complete and clear DMA state
471 * if this if for an SCB in the qinfifo.
473 while (((ccscbctl = ahd_inb(ahd, CCSCBCTL)) & (CCARREN|CCSCBEN)) != 0) {
475 if ((ccscbctl & (CCSCBDIR|CCARREN)) == (CCSCBDIR|CCARREN)) {
476 if ((ccscbctl & ARRDONE) != 0)
478 } else if ((ccscbctl & CCSCBDONE) != 0)
483 * We leave the sequencer to cleanup in the case of DMA's to
484 * update the qoutfifo. In all other cases (DMA's to the
485 * chip or a push of an SCB from the COMPLETE_DMA_SCB list),
486 * we disable the DMA engine so that the sequencer will not
487 * attempt to handle the DMA completion.
489 if ((ccscbctl & CCSCBDIR) != 0 || (ccscbctl & ARRDONE) != 0)
490 ahd_outb(ahd, CCSCBCTL, ccscbctl & ~(CCARREN|CCSCBEN));
493 * Complete any SCBs that just finished
494 * being DMA'ed into the qoutfifo.
496 ahd_run_qoutfifo(ahd);
498 saved_scbptr = ahd_get_scbptr(ahd);
500 * Manually update/complete any completed SCBs that are waiting to be
501 * DMA'ed back up to the host.
503 scbid = ahd_inw(ahd, COMPLETE_DMA_SCB_HEAD);
504 while (!SCBID_IS_NULL(scbid)) {
508 ahd_set_scbptr(ahd, scbid);
509 next_scbid = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE);
510 scb = ahd_lookup_scb(ahd, scbid);
512 kprintf("%s: Warning - DMA-up and complete "
513 "SCB %d invalid\n", ahd_name(ahd), scbid);
516 hscb_ptr = (uint8_t *)scb->hscb;
517 for (i = 0; i < sizeof(struct hardware_scb); i++)
518 *hscb_ptr++ = ahd_inb_scbram(ahd, SCB_BASE + i);
520 ahd_complete_scb(ahd, scb);
523 ahd_outw(ahd, COMPLETE_DMA_SCB_HEAD, SCB_LIST_NULL);
524 ahd_outw(ahd, COMPLETE_DMA_SCB_TAIL, SCB_LIST_NULL);
526 scbid = ahd_inw(ahd, COMPLETE_ON_QFREEZE_HEAD);
527 while (!SCBID_IS_NULL(scbid)) {
529 ahd_set_scbptr(ahd, scbid);
530 next_scbid = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE);
531 scb = ahd_lookup_scb(ahd, scbid);
533 kprintf("%s: Warning - Complete Qfrz SCB %d invalid\n",
534 ahd_name(ahd), scbid);
538 ahd_complete_scb(ahd, scb);
541 ahd_outw(ahd, COMPLETE_ON_QFREEZE_HEAD, SCB_LIST_NULL);
543 scbid = ahd_inw(ahd, COMPLETE_SCB_HEAD);
544 while (!SCBID_IS_NULL(scbid)) {
546 ahd_set_scbptr(ahd, scbid);
547 next_scbid = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE);
548 scb = ahd_lookup_scb(ahd, scbid);
550 kprintf("%s: Warning - Complete SCB %d invalid\n",
551 ahd_name(ahd), scbid);
555 ahd_complete_scb(ahd, scb);
558 ahd_outw(ahd, COMPLETE_SCB_HEAD, SCB_LIST_NULL);
563 ahd_set_scbptr(ahd, saved_scbptr);
564 ahd_restore_modes(ahd, saved_modes);
565 ahd->flags |= AHD_UPDATE_PEND_CMDS;
569 * Determine if an SCB for a packetized transaction
570 * is active in a FIFO.
573 ahd_scb_active_in_fifo(struct ahd_softc *ahd, struct scb *scb)
577 * The FIFO is only active for our transaction if
578 * the SCBPTR matches the SCB's ID and the firmware
579 * has installed a handler for the FIFO or we have
580 * a pending SAVEPTRS or CFG4DATA interrupt.
582 if (ahd_get_scbptr(ahd) != SCB_GET_TAG(scb)
583 || ((ahd_inb(ahd, LONGJMP_ADDR+1) & INVALID_ADDR) != 0
584 && (ahd_inb(ahd, SEQINTSRC) & (CFG4DATA|SAVEPTRS)) == 0))
591 * Run a data fifo to completion for a transaction we know
592 * has completed across the SCSI bus (good status has been
593 * received). We are already set to the correct FIFO mode
594 * on entry to this routine.
596 * This function attempts to operate exactly as the firmware
597 * would when running this FIFO. Care must be taken to update
598 * this routine any time the firmware's FIFO algorithm is
602 ahd_run_data_fifo(struct ahd_softc *ahd, struct scb *scb)
606 seqintsrc = ahd_inb(ahd, SEQINTSRC);
607 if ((seqintsrc & CFG4DATA) != 0) {
612 * Clear full residual flag.
614 sgptr = ahd_inl_scbram(ahd, SCB_SGPTR) & ~SG_FULL_RESID;
615 ahd_outb(ahd, SCB_SGPTR, sgptr);
618 * Load datacnt and address.
620 datacnt = ahd_inl_scbram(ahd, SCB_DATACNT);
621 if ((datacnt & AHD_DMA_LAST_SEG) != 0) {
623 ahd_outb(ahd, SG_STATE, 0);
625 ahd_outb(ahd, SG_STATE, LOADING_NEEDED);
626 ahd_outq(ahd, HADDR, ahd_inq_scbram(ahd, SCB_DATAPTR));
627 ahd_outl(ahd, HCNT, datacnt & AHD_SG_LEN_MASK);
628 ahd_outb(ahd, SG_CACHE_PRE, sgptr);
629 ahd_outb(ahd, DFCNTRL, PRELOADEN|SCSIEN|HDMAEN);
632 * Initialize Residual Fields.
634 ahd_outb(ahd, SCB_RESIDUAL_DATACNT+3, datacnt >> 24);
635 ahd_outl(ahd, SCB_RESIDUAL_SGPTR, sgptr & SG_PTR_MASK);
638 * Mark the SCB as having a FIFO in use.
640 ahd_outb(ahd, SCB_FIFO_USE_COUNT,
641 ahd_inb_scbram(ahd, SCB_FIFO_USE_COUNT) + 1);
644 * Install a "fake" handler for this FIFO.
646 ahd_outw(ahd, LONGJMP_ADDR, 0);
649 * Notify the hardware that we have satisfied
650 * this sequencer interrupt.
652 ahd_outb(ahd, CLRSEQINTSRC, CLRCFG4DATA);
653 } else if ((seqintsrc & SAVEPTRS) != 0) {
657 if ((ahd_inb(ahd, LONGJMP_ADDR+1)&INVALID_ADDR) != 0) {
659 * Snapshot Save Pointers. All that
660 * is necessary to clear the snapshot
667 * Disable S/G fetch so the DMA engine
668 * is available to future users.
670 if ((ahd_inb(ahd, SG_STATE) & FETCH_INPROG) != 0)
671 ahd_outb(ahd, CCSGCTL, 0);
672 ahd_outb(ahd, SG_STATE, 0);
675 * Flush the data FIFO. Strickly only
676 * necessary for Rev A parts.
678 ahd_outb(ahd, DFCNTRL, ahd_inb(ahd, DFCNTRL) | FIFOFLUSH);
681 * Calculate residual.
683 sgptr = ahd_inl_scbram(ahd, SCB_RESIDUAL_SGPTR);
684 resid = ahd_inl(ahd, SHCNT);
685 resid |= ahd_inb_scbram(ahd, SCB_RESIDUAL_DATACNT+3) << 24;
686 ahd_outl(ahd, SCB_RESIDUAL_DATACNT, resid);
687 if ((ahd_inb(ahd, SG_CACHE_SHADOW) & LAST_SEG) == 0) {
689 * Must back up to the correct S/G element.
690 * Typically this just means resetting our
691 * low byte to the offset in the SG_CACHE,
692 * but if we wrapped, we have to correct
693 * the other bytes of the sgptr too.
695 if ((ahd_inb(ahd, SG_CACHE_SHADOW) & 0x80) != 0
696 && (sgptr & 0x80) == 0)
699 sgptr |= ahd_inb(ahd, SG_CACHE_SHADOW)
701 ahd_outl(ahd, SCB_RESIDUAL_SGPTR, sgptr);
702 ahd_outb(ahd, SCB_RESIDUAL_DATACNT + 3, 0);
703 } else if ((resid & AHD_SG_LEN_MASK) == 0) {
704 ahd_outb(ahd, SCB_RESIDUAL_SGPTR,
705 sgptr | SG_LIST_NULL);
710 ahd_outq(ahd, SCB_DATAPTR, ahd_inq(ahd, SHADDR));
711 ahd_outl(ahd, SCB_DATACNT, resid);
712 ahd_outl(ahd, SCB_SGPTR, sgptr);
713 ahd_outb(ahd, CLRSEQINTSRC, CLRSAVEPTRS);
714 ahd_outb(ahd, SEQIMODE,
715 ahd_inb(ahd, SEQIMODE) | ENSAVEPTRS);
717 * If the data is to the SCSI bus, we are
718 * done, otherwise wait for FIFOEMP.
720 if ((ahd_inb(ahd, DFCNTRL) & DIRECTION) != 0)
722 } else if ((ahd_inb(ahd, SG_STATE) & LOADING_NEEDED) != 0) {
729 * Disable S/G fetch so the DMA engine
730 * is available to future users. We won't
731 * be using the DMA engine to load segments.
733 if ((ahd_inb(ahd, SG_STATE) & FETCH_INPROG) != 0) {
734 ahd_outb(ahd, CCSGCTL, 0);
735 ahd_outb(ahd, SG_STATE, LOADING_NEEDED);
739 * Wait for the DMA engine to notice that the
740 * host transfer is enabled and that there is
741 * space in the S/G FIFO for new segments before
742 * loading more segments.
744 if ((ahd_inb(ahd, DFSTATUS) & PRELOAD_AVAIL) != 0
745 && (ahd_inb(ahd, DFCNTRL) & HDMAENACK) != 0) {
748 * Determine the offset of the next S/G
751 sgptr = ahd_inl_scbram(ahd, SCB_RESIDUAL_SGPTR);
752 sgptr &= SG_PTR_MASK;
753 if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0) {
754 struct ahd_dma64_seg *sg;
756 sg = ahd_sg_bus_to_virt(ahd, scb, sgptr);
757 data_addr = sg->addr;
759 sgptr += sizeof(*sg);
761 struct ahd_dma_seg *sg;
763 sg = ahd_sg_bus_to_virt(ahd, scb, sgptr);
764 data_addr = sg->len & AHD_SG_HIGH_ADDR_MASK;
766 data_addr |= sg->addr;
768 sgptr += sizeof(*sg);
772 * Update residual information.
774 ahd_outb(ahd, SCB_RESIDUAL_DATACNT+3, data_len >> 24);
775 ahd_outl(ahd, SCB_RESIDUAL_SGPTR, sgptr);
780 if (data_len & AHD_DMA_LAST_SEG) {
782 ahd_outb(ahd, SG_STATE, 0);
784 ahd_outq(ahd, HADDR, data_addr);
785 ahd_outl(ahd, HCNT, data_len & AHD_SG_LEN_MASK);
786 ahd_outb(ahd, SG_CACHE_PRE, sgptr & 0xFF);
789 * Advertise the segment to the hardware.
791 dfcntrl = ahd_inb(ahd, DFCNTRL)|PRELOADEN|HDMAEN;
792 if ((ahd->features & AHD_NEW_DFCNTRL_OPTS) != 0) {
794 * Use SCSIENWRDIS so that SCSIEN
795 * is never modified by this
798 dfcntrl |= SCSIENWRDIS;
800 ahd_outb(ahd, DFCNTRL, dfcntrl);
802 } else if ((ahd_inb(ahd, SG_CACHE_SHADOW) & LAST_SEG_DONE) != 0) {
805 * Transfer completed to the end of SG list
806 * and has flushed to the host.
808 ahd_outb(ahd, SCB_SGPTR,
809 ahd_inb_scbram(ahd, SCB_SGPTR) | SG_LIST_NULL);
811 } else if ((ahd_inb(ahd, DFSTATUS) & FIFOEMP) != 0) {
814 * Clear any handler for this FIFO, decrement
815 * the FIFO use count for the SCB, and release
818 ahd_outb(ahd, LONGJMP_ADDR + 1, INVALID_ADDR);
819 ahd_outb(ahd, SCB_FIFO_USE_COUNT,
820 ahd_inb_scbram(ahd, SCB_FIFO_USE_COUNT) - 1);
821 ahd_outb(ahd, DFFSXFRCTL, CLRCHN);
826 * Look for entries in the QoutFIFO that have completed.
827 * The valid_tag completion field indicates the validity
828 * of the entry - the valid value toggles each time through
829 * the queue. We use the sg_status field in the completion
830 * entry to avoid referencing the hscb if the completion
831 * occurred with no errors and no residual. sg_status is
832 * a copy of the first byte (little endian) of the sgptr
836 ahd_run_qoutfifo(struct ahd_softc *ahd)
838 struct ahd_completion *completion;
842 if ((ahd->flags & AHD_RUNNING_QOUTFIFO) != 0)
843 panic("ahd_run_qoutfifo recursion");
844 ahd->flags |= AHD_RUNNING_QOUTFIFO;
845 ahd_sync_qoutfifo(ahd, BUS_DMASYNC_POSTREAD);
847 completion = &ahd->qoutfifo[ahd->qoutfifonext];
849 if (completion->valid_tag != ahd->qoutfifonext_valid_tag)
852 scb_index = aic_le16toh(completion->tag);
853 scb = ahd_lookup_scb(ahd, scb_index);
855 kprintf("%s: WARNING no command for scb %d "
856 "(cmdcmplt)\nQOUTPOS = %d\n",
857 ahd_name(ahd), scb_index,
859 ahd_dump_card_state(ahd);
860 } else if ((completion->sg_status & SG_STATUS_VALID) != 0) {
861 ahd_handle_scb_status(ahd, scb);
866 ahd->qoutfifonext = (ahd->qoutfifonext+1) & (AHD_QOUT_SIZE-1);
867 if (ahd->qoutfifonext == 0)
868 ahd->qoutfifonext_valid_tag ^= QOUTFIFO_ENTRY_VALID;
870 ahd->flags &= ~AHD_RUNNING_QOUTFIFO;
873 /************************* Interrupt Handling *********************************/
875 ahd_handle_hwerrint(struct ahd_softc *ahd)
878 * Some catastrophic hardware error has occurred.
879 * Print it for the user and disable the controller.
884 error = ahd_inb(ahd, ERROR);
885 for (i = 0; i < num_errors; i++) {
886 if ((error & ahd_hard_errors[i].error) != 0)
887 kprintf("%s: hwerrint, %s\n",
888 ahd_name(ahd), ahd_hard_errors[i].errmesg);
891 ahd_dump_card_state(ahd);
894 /* Tell everyone that this HBA is no longer available */
895 ahd_abort_scbs(ahd, CAM_TARGET_WILDCARD, ALL_CHANNELS,
896 CAM_LUN_WILDCARD, SCB_LIST_NULL, ROLE_UNKNOWN,
899 /* Tell the system that this controller has gone away. */
904 ahd_handle_seqint(struct ahd_softc *ahd, u_int intstat)
909 * Save the sequencer interrupt code and clear the SEQINT
910 * bit. We will unpause the sequencer, if appropriate,
911 * after servicing the request.
913 seqintcode = ahd_inb(ahd, SEQINTCODE);
914 ahd_outb(ahd, CLRINT, CLRSEQINT);
915 if ((ahd->bugs & AHD_INTCOLLISION_BUG) != 0) {
917 * Unpause the sequencer and let it clear
918 * SEQINT by writing NO_SEQINT to it. This
919 * will cause the sequencer to be paused again,
920 * which is the expected state of this routine.
923 while (!ahd_is_paused(ahd))
925 ahd_outb(ahd, CLRINT, CLRSEQINT);
927 ahd_update_modes(ahd);
929 if ((ahd_debug & AHD_SHOW_MISC) != 0)
930 kprintf("%s: Handle Seqint Called for code %d\n",
931 ahd_name(ahd), seqintcode);
933 switch (seqintcode) {
934 case ENTERING_NONPACK:
939 AHD_ASSERT_MODES(ahd, ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK),
940 ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK));
941 scbid = ahd_get_scbptr(ahd);
942 scb = ahd_lookup_scb(ahd, scbid);
945 * Somehow need to know if this
946 * is from a selection or reselection.
947 * From that, we can determine target
948 * ID so we at least have an I_T nexus.
951 ahd_outb(ahd, SAVED_SCSIID, scb->hscb->scsiid);
952 ahd_outb(ahd, SAVED_LUN, scb->hscb->lun);
953 ahd_outb(ahd, SEQ_FLAGS, 0x0);
955 if ((ahd_inb(ahd, LQISTAT2) & LQIPHASE_OUTPKT) != 0
956 && (ahd_inb(ahd, SCSISIGO) & ATNO) != 0) {
958 * Phase change after read stream with
959 * CRC error with P0 asserted on last
963 if ((ahd_debug & AHD_SHOW_RECOVERY) != 0)
964 kprintf("%s: Assuming LQIPHASE_NLQ with "
965 "P0 assertion\n", ahd_name(ahd));
969 if ((ahd_debug & AHD_SHOW_RECOVERY) != 0)
970 kprintf("%s: Entering NONPACK\n", ahd_name(ahd));
975 kprintf("%s: Invalid Sequencer interrupt occurred.\n",
977 ahd_dump_card_state(ahd);
978 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
985 scbid = ahd_get_scbptr(ahd);
986 scb = ahd_lookup_scb(ahd, scbid);
988 ahd_print_path(ahd, scb);
990 kprintf("%s: ", ahd_name(ahd));
991 kprintf("SCB %d Packetized Status Overrun", scbid);
992 ahd_dump_card_state(ahd);
993 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
1001 scbid = ahd_get_scbptr(ahd);
1002 scb = ahd_lookup_scb(ahd, scbid);
1004 ahd_dump_card_state(ahd);
1005 kprintf("CFG4ISTAT: Free SCB %d referenced", scbid);
1006 panic("For safety");
1008 ahd_outq(ahd, HADDR, scb->sense_busaddr);
1009 ahd_outw(ahd, HCNT, AHD_SENSE_BUFSIZE);
1010 ahd_outb(ahd, HCNT + 2, 0);
1011 ahd_outb(ahd, SG_CACHE_PRE, SG_LAST_SEG);
1012 ahd_outb(ahd, DFCNTRL, PRELOADEN|SCSIEN|HDMAEN);
1019 bus_phase = ahd_inb(ahd, SCSISIGI) & PHASE_MASK;
1020 kprintf("%s: ILLEGAL_PHASE 0x%x\n",
1021 ahd_name(ahd), bus_phase);
1023 switch (bus_phase) {
1031 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
1032 kprintf("%s: Issued Bus Reset.\n", ahd_name(ahd));
1036 struct ahd_devinfo devinfo;
1038 struct ahd_initiator_tinfo *targ_info;
1039 struct ahd_tmode_tstate *tstate;
1040 struct ahd_transinfo *tinfo;
1044 * If a target takes us into the command phase
1045 * assume that it has been externally reset and
1046 * has thus lost our previous packetized negotiation
1047 * agreement. Since we have not sent an identify
1048 * message and may not have fully qualified the
1049 * connection, we change our command to TUR, assert
1050 * ATN and ABORT the task when we go to message in
1051 * phase. The OSM will see the REQUEUE_REQUEST
1052 * status and retry the command.
1054 scbid = ahd_get_scbptr(ahd);
1055 scb = ahd_lookup_scb(ahd, scbid);
1057 kprintf("Invalid phase with no valid SCB. "
1058 "Resetting bus.\n");
1059 ahd_reset_channel(ahd, 'A',
1060 /*Initiate Reset*/TRUE);
1063 ahd_compile_devinfo(&devinfo, SCB_GET_OUR_ID(scb),
1064 SCB_GET_TARGET(ahd, scb),
1066 SCB_GET_CHANNEL(ahd, scb),
1068 targ_info = ahd_fetch_transinfo(ahd,
1073 tinfo = &targ_info->curr;
1074 ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
1075 AHD_TRANS_ACTIVE, /*paused*/TRUE);
1076 ahd_set_syncrate(ahd, &devinfo, /*period*/0,
1077 /*offset*/0, /*ppr_options*/0,
1078 AHD_TRANS_ACTIVE, /*paused*/TRUE);
1079 ahd_outb(ahd, SCB_CDB_STORE, 0);
1080 ahd_outb(ahd, SCB_CDB_STORE+1, 0);
1081 ahd_outb(ahd, SCB_CDB_STORE+2, 0);
1082 ahd_outb(ahd, SCB_CDB_STORE+3, 0);
1083 ahd_outb(ahd, SCB_CDB_STORE+4, 0);
1084 ahd_outb(ahd, SCB_CDB_STORE+5, 0);
1085 ahd_outb(ahd, SCB_CDB_LEN, 6);
1086 scb->hscb->control &= ~(TAG_ENB|SCB_TAG_TYPE);
1087 scb->hscb->control |= MK_MESSAGE;
1088 ahd_outb(ahd, SCB_CONTROL, scb->hscb->control);
1089 ahd_outb(ahd, MSG_OUT, HOST_MSG);
1090 ahd_outb(ahd, SAVED_SCSIID, scb->hscb->scsiid);
1092 * The lun is 0, regardless of the SCB's lun
1093 * as we have not sent an identify message.
1095 ahd_outb(ahd, SAVED_LUN, 0);
1096 ahd_outb(ahd, SEQ_FLAGS, 0);
1097 ahd_assert_atn(ahd);
1098 scb->flags &= ~SCB_PACKETIZED;
1099 scb->flags |= SCB_ABORT|SCB_CMDPHASE_ABORT;
1100 ahd_freeze_devq(ahd, scb);
1101 aic_set_transaction_status(scb, CAM_REQUEUE_REQ);
1102 aic_freeze_scb(scb);
1105 * Allow the sequencer to continue with
1106 * non-pack processing.
1108 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
1109 ahd_outb(ahd, CLRLQOINT1, CLRLQOPHACHGINPKT);
1110 if ((ahd->bugs & AHD_CLRLQO_AUTOCLR_BUG) != 0) {
1111 ahd_outb(ahd, CLRLQOINT1, 0);
1114 if ((ahd_debug & AHD_SHOW_RECOVERY) != 0) {
1115 ahd_print_path(ahd, scb);
1116 kprintf("Unexpected command phase from "
1117 "packetized target\n");
1131 if ((ahd_debug & AHD_SHOW_RECOVERY) != 0) {
1132 kprintf("%s: CFG4OVERRUN mode = %x\n", ahd_name(ahd),
1133 ahd_inb(ahd, MODE_PTR));
1136 scb_index = ahd_get_scbptr(ahd);
1137 scb = ahd_lookup_scb(ahd, scb_index);
1140 * Attempt to transfer to an SCB that is
1143 ahd_assert_atn(ahd);
1144 ahd_outb(ahd, MSG_OUT, HOST_MSG);
1145 ahd->msgout_buf[0] = MSG_ABORT_TASK;
1146 ahd->msgout_len = 1;
1147 ahd->msgout_index = 0;
1148 ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
1150 * Clear status received flag to prevent any
1151 * attempt to complete this bogus SCB.
1153 ahd_outb(ahd, SCB_CONTROL,
1154 ahd_inb_scbram(ahd, SCB_CONTROL)
1159 case DUMP_CARD_STATE:
1161 ahd_dump_card_state(ahd);
1167 if ((ahd_debug & AHD_SHOW_RECOVERY) != 0) {
1168 kprintf("%s: PDATA_REINIT - DFCNTRL = 0x%x "
1169 "SG_CACHE_SHADOW = 0x%x\n",
1170 ahd_name(ahd), ahd_inb(ahd, DFCNTRL),
1171 ahd_inb(ahd, SG_CACHE_SHADOW));
1174 ahd_reinitialize_dataptrs(ahd);
1179 struct ahd_devinfo devinfo;
1182 * The sequencer has encountered a message phase
1183 * that requires host assistance for completion.
1184 * While handling the message phase(s), we will be
1185 * notified by the sequencer after each byte is
1186 * transfered so we can track bus phase changes.
1188 * If this is the first time we've seen a HOST_MSG_LOOP
1189 * interrupt, initialize the state of the host message
1192 ahd_fetch_devinfo(ahd, &devinfo);
1193 if (ahd->msg_type == MSG_TYPE_NONE) {
1198 bus_phase = ahd_inb(ahd, SCSISIGI) & PHASE_MASK;
1199 if (bus_phase != P_MESGIN
1200 && bus_phase != P_MESGOUT) {
1201 kprintf("ahd_intr: HOST_MSG_LOOP bad "
1202 "phase 0x%x\n", bus_phase);
1204 * Probably transitioned to bus free before
1205 * we got here. Just punt the message.
1207 ahd_dump_card_state(ahd);
1208 ahd_clear_intstat(ahd);
1213 scb_index = ahd_get_scbptr(ahd);
1214 scb = ahd_lookup_scb(ahd, scb_index);
1215 if (devinfo.role == ROLE_INITIATOR) {
1216 if (bus_phase == P_MESGOUT)
1217 ahd_setup_initiator_msgout(ahd,
1222 MSG_TYPE_INITIATOR_MSGIN;
1223 ahd->msgin_index = 0;
1228 if (bus_phase == P_MESGOUT) {
1230 MSG_TYPE_TARGET_MSGOUT;
1231 ahd->msgin_index = 0;
1234 ahd_setup_target_msgin(ahd,
1241 ahd_handle_message_phase(ahd);
1246 /* Ensure we don't leave the selection hardware on */
1247 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
1248 ahd_outb(ahd, SCSISEQ0, ahd_inb(ahd, SCSISEQ0) & ~ENSELO);
1250 kprintf("%s:%c:%d: no active SCB for reconnecting "
1251 "target - issuing BUS DEVICE RESET\n",
1252 ahd_name(ahd), 'A', ahd_inb(ahd, SELID) >> 4);
1253 kprintf("SAVED_SCSIID == 0x%x, SAVED_LUN == 0x%x, "
1254 "REG0 == 0x%x ACCUM = 0x%x\n",
1255 ahd_inb(ahd, SAVED_SCSIID), ahd_inb(ahd, SAVED_LUN),
1256 ahd_inw(ahd, REG0), ahd_inb(ahd, ACCUM));
1257 kprintf("SEQ_FLAGS == 0x%x, SCBPTR == 0x%x, BTT == 0x%x, "
1259 ahd_inb(ahd, SEQ_FLAGS), ahd_get_scbptr(ahd),
1260 ahd_find_busy_tcl(ahd,
1261 BUILD_TCL(ahd_inb(ahd, SAVED_SCSIID),
1262 ahd_inb(ahd, SAVED_LUN))),
1263 ahd_inw(ahd, SINDEX));
1264 kprintf("SELID == 0x%x, SCB_SCSIID == 0x%x, SCB_LUN == 0x%x, "
1265 "SCB_CONTROL == 0x%x\n",
1266 ahd_inb(ahd, SELID), ahd_inb_scbram(ahd, SCB_SCSIID),
1267 ahd_inb_scbram(ahd, SCB_LUN),
1268 ahd_inb_scbram(ahd, SCB_CONTROL));
1269 kprintf("SCSIBUS[0] == 0x%x, SCSISIGI == 0x%x\n",
1270 ahd_inb(ahd, SCSIBUS), ahd_inb(ahd, SCSISIGI));
1271 kprintf("SXFRCTL0 == 0x%x\n", ahd_inb(ahd, SXFRCTL0));
1272 kprintf("SEQCTL0 == 0x%x\n", ahd_inb(ahd, SEQCTL0));
1273 ahd_dump_card_state(ahd);
1274 ahd->msgout_buf[0] = MSG_BUS_DEV_RESET;
1275 ahd->msgout_len = 1;
1276 ahd->msgout_index = 0;
1277 ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
1278 ahd_outb(ahd, MSG_OUT, HOST_MSG);
1279 ahd_assert_atn(ahd);
1282 case PROTO_VIOLATION:
1284 ahd_handle_proto_violation(ahd);
1289 struct ahd_devinfo devinfo;
1291 ahd_fetch_devinfo(ahd, &devinfo);
1292 ahd_handle_ign_wide_residue(ahd, &devinfo);
1299 lastphase = ahd_inb(ahd, LASTPHASE);
1300 kprintf("%s:%c:%d: unknown scsi bus phase %x, "
1301 "lastphase = 0x%x. Attempting to continue\n",
1303 SCSIID_TARGET(ahd, ahd_inb(ahd, SAVED_SCSIID)),
1304 lastphase, ahd_inb(ahd, SCSISIGI));
1307 case MISSED_BUSFREE:
1311 lastphase = ahd_inb(ahd, LASTPHASE);
1312 kprintf("%s:%c:%d: Missed busfree. "
1313 "Lastphase = 0x%x, Curphase = 0x%x\n",
1315 SCSIID_TARGET(ahd, ahd_inb(ahd, SAVED_SCSIID)),
1316 lastphase, ahd_inb(ahd, SCSISIGI));
1323 * When the sequencer detects an overrun, it
1324 * places the controller in "BITBUCKET" mode
1325 * and allows the target to complete its transfer.
1326 * Unfortunately, none of the counters get updated
1327 * when the controller is in this mode, so we have
1328 * no way of knowing how large the overrun was.
1336 scbindex = ahd_get_scbptr(ahd);
1337 scb = ahd_lookup_scb(ahd, scbindex);
1339 lastphase = ahd_inb(ahd, LASTPHASE);
1340 if ((ahd_debug & AHD_SHOW_RECOVERY) != 0) {
1341 ahd_print_path(ahd, scb);
1342 kprintf("data overrun detected %s. Tag == 0x%x.\n",
1343 ahd_lookup_phase_entry(lastphase)->phasemsg,
1345 ahd_print_path(ahd, scb);
1346 kprintf("%s seen Data Phase. Length = %ld. "
1348 ahd_inb(ahd, SEQ_FLAGS) & DPHASE
1349 ? "Have" : "Haven't",
1350 aic_get_transfer_length(scb), scb->sg_count);
1351 ahd_dump_sglist(scb);
1356 * Set this and it will take effect when the
1357 * target does a command complete.
1359 ahd_freeze_devq(ahd, scb);
1360 aic_set_transaction_status(scb, CAM_DATA_RUN_ERR);
1361 aic_freeze_scb(scb);
1366 struct ahd_devinfo devinfo;
1370 ahd_fetch_devinfo(ahd, &devinfo);
1371 kprintf("%s:%c:%d:%d: Attempt to issue message failed\n",
1372 ahd_name(ahd), devinfo.channel, devinfo.target,
1374 scbid = ahd_get_scbptr(ahd);
1375 scb = ahd_lookup_scb(ahd, scbid);
1377 && (scb->flags & SCB_RECOVERY_SCB) != 0)
1379 * Ensure that we didn't put a second instance of this
1380 * SCB into the QINFIFO.
1382 ahd_search_qinfifo(ahd, SCB_GET_TARGET(ahd, scb),
1383 SCB_GET_CHANNEL(ahd, scb),
1384 SCB_GET_LUN(scb), SCB_GET_TAG(scb),
1385 ROLE_INITIATOR, /*status*/0,
1387 ahd_outb(ahd, SCB_CONTROL,
1388 ahd_inb_scbram(ahd, SCB_CONTROL) & ~MK_MESSAGE);
1391 case TASKMGMT_FUNC_COMPLETE:
1396 scbid = ahd_get_scbptr(ahd);
1397 scb = ahd_lookup_scb(ahd, scbid);
1403 ahd_print_path(ahd, scb);
1404 kprintf("Task Management Func 0x%x Complete\n",
1405 scb->hscb->task_management);
1406 lun = CAM_LUN_WILDCARD;
1407 tag = SCB_LIST_NULL;
1409 switch (scb->hscb->task_management) {
1410 case SIU_TASKMGMT_ABORT_TASK:
1411 tag = SCB_GET_TAG(scb);
1412 case SIU_TASKMGMT_ABORT_TASK_SET:
1413 case SIU_TASKMGMT_CLEAR_TASK_SET:
1414 lun = scb->hscb->lun;
1415 error = CAM_REQ_ABORTED;
1416 ahd_abort_scbs(ahd, SCB_GET_TARGET(ahd, scb),
1417 'A', lun, tag, ROLE_INITIATOR,
1420 case SIU_TASKMGMT_LUN_RESET:
1421 lun = scb->hscb->lun;
1422 case SIU_TASKMGMT_TARGET_RESET:
1424 struct ahd_devinfo devinfo;
1426 ahd_scb_devinfo(ahd, &devinfo, scb);
1427 error = CAM_BDR_SENT;
1428 ahd_handle_devreset(ahd, &devinfo, lun,
1430 lun != CAM_LUN_WILDCARD
1433 /*verbose_level*/0);
1437 panic("Unexpected TaskMgmt Func\n");
1443 case TASKMGMT_CMD_CMPLT_OKAY:
1449 * An ABORT TASK TMF failed to be delivered before
1450 * the targeted command completed normally.
1452 scbid = ahd_get_scbptr(ahd);
1453 scb = ahd_lookup_scb(ahd, scbid);
1456 * Remove the second instance of this SCB from
1457 * the QINFIFO if it is still there.
1459 ahd_print_path(ahd, scb);
1460 kprintf("SCB completes before TMF\n");
1462 * Handle losing the race. Wait until any
1463 * current selection completes. We will then
1464 * set the TMF back to zero in this SCB so that
1465 * the sequencer doesn't bother to issue another
1466 * sequencer interrupt for its completion.
1468 while ((ahd_inb(ahd, SCSISEQ0) & ENSELO) != 0
1469 && (ahd_inb(ahd, SSTAT0) & SELDO) == 0
1470 && (ahd_inb(ahd, SSTAT1) & SELTO) == 0)
1472 ahd_outb(ahd, SCB_TASK_MANAGEMENT, 0);
1473 ahd_search_qinfifo(ahd, SCB_GET_TARGET(ahd, scb),
1474 SCB_GET_CHANNEL(ahd, scb),
1475 SCB_GET_LUN(scb), SCB_GET_TAG(scb),
1476 ROLE_INITIATOR, /*status*/0,
1485 kprintf("%s: Tracepoint %d\n", ahd_name(ahd),
1486 seqintcode - TRACEPOINT0);
1491 ahd_handle_hwerrint(ahd);
1494 kprintf("%s: Unexpected SEQINTCODE %d\n", ahd_name(ahd),
1499 * The sequencer is paused immediately on
1500 * a SEQINT, so we should restart it when
1507 ahd_handle_scsiint(struct ahd_softc *ahd, u_int intstat)
1518 ahd_update_modes(ahd);
1519 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
1521 status3 = ahd_inb(ahd, SSTAT3) & (NTRAMPERR|OSRAMPERR);
1522 status0 = ahd_inb(ahd, SSTAT0) & (IOERR|OVERRUN|SELDI|SELDO);
1523 status = ahd_inb(ahd, SSTAT1) & (SELTO|SCSIRSTI|BUSFREE|SCSIPERR);
1524 lqistat1 = ahd_inb(ahd, LQISTAT1);
1525 lqostat0 = ahd_inb(ahd, LQOSTAT0);
1526 busfreetime = ahd_inb(ahd, SSTAT2) & BUSFREETIME;
1527 if ((status0 & (SELDI|SELDO)) != 0) {
1530 ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
1531 simode0 = ahd_inb(ahd, SIMODE0);
1532 status0 &= simode0 & (IOERR|OVERRUN|SELDI|SELDO);
1533 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
1535 scbid = ahd_get_scbptr(ahd);
1536 scb = ahd_lookup_scb(ahd, scbid);
1538 && (ahd_inb(ahd, SEQ_FLAGS) & NOT_IDENTIFIED) != 0)
1541 /* Make sure the sequencer is in a safe location. */
1542 ahd_clear_critical_section(ahd);
1544 if ((status0 & IOERR) != 0) {
1547 now_lvd = ahd_inb(ahd, SBLKCTL) & ENAB40;
1548 kprintf("%s: Transceiver State Has Changed to %s mode\n",
1549 ahd_name(ahd), now_lvd ? "LVD" : "SE");
1550 ahd_outb(ahd, CLRSINT0, CLRIOERR);
1552 * A change in I/O mode is equivalent to a bus reset.
1554 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
1556 ahd_setup_iocell_workaround(ahd);
1558 } else if ((status0 & OVERRUN) != 0) {
1559 kprintf("%s: SCSI offset overrun detected. Resetting bus.\n",
1561 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
1562 } else if ((status & SCSIRSTI) != 0) {
1563 kprintf("%s: Someone reset channel A\n", ahd_name(ahd));
1564 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/FALSE);
1565 } else if ((status & SCSIPERR) != 0) {
1566 ahd_handle_transmission_error(ahd);
1567 } else if (lqostat0 != 0) {
1568 kprintf("%s: lqostat0 == 0x%x!\n", ahd_name(ahd), lqostat0);
1569 ahd_outb(ahd, CLRLQOINT0, lqostat0);
1570 if ((ahd->bugs & AHD_CLRLQO_AUTOCLR_BUG) != 0) {
1571 ahd_outb(ahd, CLRLQOINT1, 0);
1573 } else if ((status & SELTO) != 0) {
1576 /* Stop the selection */
1577 ahd_outb(ahd, SCSISEQ0, 0);
1579 /* No more pending messages */
1580 ahd_clear_msg_state(ahd);
1582 /* Clear interrupt state */
1583 ahd_outb(ahd, CLRSINT1, CLRSELTIMEO|CLRBUSFREE|CLRSCSIPERR);
1586 * Although the driver does not care about the
1587 * 'Selection in Progress' status bit, the busy
1588 * LED does. SELINGO is only cleared by a successful
1589 * selection, so we must manually clear it to insure
1590 * the LED turns off just in case no future successful
1591 * selections occur (e.g. no devices on the bus).
1593 ahd_outb(ahd, CLRSINT0, CLRSELINGO);
1595 scbid = ahd_inw(ahd, WAITING_TID_HEAD);
1596 scb = ahd_lookup_scb(ahd, scbid);
1598 kprintf("%s: ahd_intr - referenced scb not "
1599 "valid during SELTO scb(0x%x)\n",
1600 ahd_name(ahd), scbid);
1601 ahd_dump_card_state(ahd);
1603 struct ahd_devinfo devinfo;
1605 if ((ahd_debug & AHD_SHOW_SELTO) != 0) {
1606 ahd_print_path(ahd, scb);
1607 kprintf("Saw Selection Timeout for SCB 0x%x\n",
1612 * Force a renegotiation with this target just in
1613 * case the cable was pulled and will later be
1614 * re-attached. The target may forget its negotiation
1615 * settings with us should it attempt to reselect
1616 * during the interruption. The target will not issue
1617 * a unit attention in this case, so we must always
1620 ahd_scb_devinfo(ahd, &devinfo, scb);
1621 ahd_force_renegotiation(ahd, &devinfo);
1622 aic_set_transaction_status(scb, CAM_SEL_TIMEOUT);
1623 ahd_freeze_devq(ahd, scb);
1625 ahd_outb(ahd, CLRINT, CLRSCSIINT);
1626 ahd_iocell_first_selection(ahd);
1628 } else if ((status0 & (SELDI|SELDO)) != 0) {
1629 ahd_iocell_first_selection(ahd);
1631 } else if (status3 != 0) {
1632 kprintf("%s: SCSI Cell parity error SSTAT3 == 0x%x\n",
1633 ahd_name(ahd), status3);
1634 ahd_outb(ahd, CLRSINT3, status3);
1635 } else if ((lqistat1 & (LQIPHASE_LQ|LQIPHASE_NLQ)) != 0) {
1636 ahd_handle_lqiphase_error(ahd, lqistat1);
1637 } else if ((lqistat1 & LQICRCI_NLQ) != 0) {
1639 * This status can be delayed during some
1640 * streaming operations. The SCSIPHASE
1641 * handler has already dealt with this case
1642 * so just clear the error.
1644 ahd_outb(ahd, CLRLQIINT1, CLRLQICRCI_NLQ);
1645 } else if ((status & BUSFREE) != 0) {
1653 * Clear our selection hardware as soon as possible.
1654 * We may have an entry in the waiting Q for this target,
1655 * that is affected by this busfree and we don't want to
1656 * go about selecting the target while we handle the event.
1658 ahd_outb(ahd, SCSISEQ0, 0);
1661 * Determine what we were up to at the time of
1664 mode = AHD_MODE_SCSI;
1665 busfreetime = ahd_inb(ahd, SSTAT2) & BUSFREETIME;
1666 lqostat1 = ahd_inb(ahd, LQOSTAT1);
1667 switch (busfreetime) {
1674 mode = busfreetime == BUSFREE_DFF0
1675 ? AHD_MODE_DFF0 : AHD_MODE_DFF1;
1676 ahd_set_modes(ahd, mode, mode);
1677 scbid = ahd_get_scbptr(ahd);
1678 scb = ahd_lookup_scb(ahd, scbid);
1680 kprintf("%s: Invalid SCB %d in DFF%d "
1681 "during unexpected busfree\n",
1682 ahd_name(ahd), scbid, mode);
1685 packetized = (scb->flags & SCB_PACKETIZED) != 0;
1695 packetized = (lqostat1 & LQOBUSFREE) != 0;
1697 && ahd_inb(ahd, LASTPHASE) == P_BUSFREE
1698 && ((ahd_inb(ahd, SSTAT0) & SELDO) == 0
1699 || (ahd_inb(ahd, SCSISEQ0) & ENSELO) == 0))
1701 * Assume packetized if we are not
1702 * on the bus in a non-packetized
1703 * capacity and any pending selection
1704 * was a packetized selection.
1711 if ((ahd_debug & AHD_SHOW_MISC) != 0)
1712 kprintf("Saw Busfree. Busfreetime = 0x%x.\n",
1716 * Busfrees that occur in non-packetized phases are
1717 * handled by the nonpkt_busfree handler.
1719 if (packetized && ahd_inb(ahd, LASTPHASE) == P_BUSFREE) {
1720 restart = ahd_handle_pkt_busfree(ahd, busfreetime);
1723 restart = ahd_handle_nonpkt_busfree(ahd);
1726 * Clear the busfree interrupt status. The setting of
1727 * the interrupt is a pulse, so in a perfect world, we
1728 * would not need to muck with the ENBUSFREE logic. This
1729 * would ensure that if the bus moves on to another
1730 * connection, busfree protection is still in force. If
1731 * BUSFREEREV is broken, however, we must manually clear
1732 * the ENBUSFREE if the busfree occurred during a non-pack
1733 * connection so that we don't get false positives during
1734 * future, packetized, connections.
1736 ahd_outb(ahd, CLRSINT1, CLRBUSFREE);
1738 && (ahd->bugs & AHD_BUSFREEREV_BUG) != 0)
1739 ahd_outb(ahd, SIMODE1,
1740 ahd_inb(ahd, SIMODE1) & ~ENBUSFREE);
1743 ahd_clear_fifo(ahd, mode);
1745 ahd_clear_msg_state(ahd);
1746 ahd_outb(ahd, CLRINT, CLRSCSIINT);
1753 kprintf("%s: Missing case in ahd_handle_scsiint. status = %x\n",
1754 ahd_name(ahd), status);
1755 ahd_dump_card_state(ahd);
1756 ahd_clear_intstat(ahd);
1762 ahd_handle_transmission_error(struct ahd_softc *ahd)
1776 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
1777 lqistat1 = ahd_inb(ahd, LQISTAT1) & ~(LQIPHASE_LQ|LQIPHASE_NLQ);
1778 lqistat2 = ahd_inb(ahd, LQISTAT2);
1779 if ((lqistat1 & (LQICRCI_NLQ|LQICRCI_LQ)) == 0
1780 && (ahd->bugs & AHD_NLQICRC_DELAYED_BUG) != 0) {
1783 ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
1784 lqistate = ahd_inb(ahd, LQISTATE);
1785 if ((lqistate >= 0x1E && lqistate <= 0x24)
1786 || (lqistate == 0x29)) {
1788 if ((ahd_debug & AHD_SHOW_RECOVERY) != 0) {
1789 kprintf("%s: NLQCRC found via LQISTATE\n",
1793 lqistat1 |= LQICRCI_NLQ;
1795 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
1798 ahd_outb(ahd, CLRLQIINT1, lqistat1);
1799 lastphase = ahd_inb(ahd, LASTPHASE);
1800 curphase = ahd_inb(ahd, SCSISIGI) & PHASE_MASK;
1801 perrdiag = ahd_inb(ahd, PERRDIAG);
1802 msg_out = MSG_INITIATOR_DET_ERR;
1803 ahd_outb(ahd, CLRSINT1, CLRSCSIPERR);
1806 * Try to find the SCB associated with this error.
1810 || (lqistat1 & LQICRCI_NLQ) != 0) {
1811 if ((lqistat1 & (LQICRCI_NLQ|LQIOVERI_NLQ)) != 0)
1812 ahd_set_active_fifo(ahd);
1813 scbid = ahd_get_scbptr(ahd);
1814 scb = ahd_lookup_scb(ahd, scbid);
1815 if (scb != NULL && SCB_IS_SILENT(scb))
1820 if (silent == FALSE) {
1821 kprintf("%s: Transmission error detected\n", ahd_name(ahd));
1822 ahd_lqistat1_print(lqistat1, &cur_col, 50);
1823 ahd_lastphase_print(lastphase, &cur_col, 50);
1824 ahd_scsisigi_print(curphase, &cur_col, 50);
1825 ahd_perrdiag_print(perrdiag, &cur_col, 50);
1827 ahd_dump_card_state(ahd);
1830 if ((lqistat1 & (LQIOVERI_LQ|LQIOVERI_NLQ)) != 0) {
1831 if (silent == FALSE) {
1832 kprintf("%s: Gross protocol error during incoming "
1833 "packet. lqistat1 == 0x%x. Resetting bus.\n",
1834 ahd_name(ahd), lqistat1);
1836 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
1838 } else if ((lqistat1 & LQICRCI_LQ) != 0) {
1840 * A CRC error has been detected on an incoming LQ.
1841 * The bus is currently hung on the last ACK.
1842 * Hit LQIRETRY to release the last ack, and
1843 * wait for the sequencer to determine that ATNO
1844 * is asserted while in message out to take us
1845 * to our host message loop. No NONPACKREQ or
1846 * LQIPHASE type errors will occur in this
1847 * scenario. After this first LQIRETRY, the LQI
1848 * manager will be in ISELO where it will
1849 * happily sit until another packet phase begins.
1850 * Unexpected bus free detection is enabled
1851 * through any phases that occur after we release
1852 * this last ack until the LQI manager sees a
1853 * packet phase. This implies we may have to
1854 * ignore a perfectly valid "unexected busfree"
1855 * after our "initiator detected error" message is
1856 * sent. A busfree is the expected response after
1857 * we tell the target that it's L_Q was corrupted.
1858 * (SPI4R09 10.7.3.3.3)
1860 ahd_outb(ahd, LQCTL2, LQIRETRY);
1861 kprintf("LQIRetry for LQICRCI_LQ to release ACK\n");
1862 } else if ((lqistat1 & LQICRCI_NLQ) != 0) {
1864 * We detected a CRC error in a NON-LQ packet.
1865 * The hardware has varying behavior in this situation
1866 * depending on whether this packet was part of a
1870 * The hardware has already acked the complete packet.
1871 * If the target honors our outstanding ATN condition,
1872 * we should be (or soon will be) in MSGOUT phase.
1873 * This will trigger the LQIPHASE_LQ status bit as the
1874 * hardware was expecting another LQ. Unexpected
1875 * busfree detection is enabled. Once LQIPHASE_LQ is
1876 * true (first entry into host message loop is much
1877 * the same), we must clear LQIPHASE_LQ and hit
1878 * LQIRETRY so the hardware is ready to handle
1879 * a future LQ. NONPACKREQ will not be asserted again
1880 * once we hit LQIRETRY until another packet is
1881 * processed. The target may either go busfree
1882 * or start another packet in response to our message.
1884 * Read Streaming P0 asserted:
1885 * If we raise ATN and the target completes the entire
1886 * stream (P0 asserted during the last packet), the
1887 * hardware will ack all data and return to the ISTART
1888 * state. When the target reponds to our ATN condition,
1889 * LQIPHASE_LQ will be asserted. We should respond to
1890 * this with an LQIRETRY to prepare for any future
1891 * packets. NONPACKREQ will not be asserted again
1892 * once we hit LQIRETRY until another packet is
1893 * processed. The target may either go busfree or
1894 * start another packet in response to our message.
1895 * Busfree detection is enabled.
1897 * Read Streaming P0 not asserted:
1898 * If we raise ATN and the target transitions to
1899 * MSGOUT in or after a packet where P0 is not
1900 * asserted, the hardware will assert LQIPHASE_NLQ.
1901 * We should respond to the LQIPHASE_NLQ with an
1902 * LQIRETRY. Should the target stay in a non-pkt
1903 * phase after we send our message, the hardware
1904 * will assert LQIPHASE_LQ. Recovery is then just as
1905 * listed above for the read streaming with P0 asserted.
1906 * Busfree detection is enabled.
1908 if (silent == FALSE)
1909 kprintf("LQICRC_NLQ\n");
1911 kprintf("%s: No SCB valid for LQICRC_NLQ. "
1912 "Resetting bus\n", ahd_name(ahd));
1913 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
1916 } else if ((lqistat1 & LQIBADLQI) != 0) {
1917 kprintf("Need to handle BADLQI!\n");
1918 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
1920 } else if ((perrdiag & (PARITYERR|PREVPHASE)) == PARITYERR) {
1921 if ((curphase & ~P_DATAIN_DT) != 0) {
1922 /* Ack the byte. So we can continue. */
1923 if (silent == FALSE)
1924 kprintf("Acking %s to clear perror\n",
1925 ahd_lookup_phase_entry(curphase)->phasemsg);
1926 ahd_inb(ahd, SCSIDAT);
1929 if (curphase == P_MESGIN)
1930 msg_out = MSG_PARITY_ERROR;
1934 * We've set the hardware to assert ATN if we
1935 * get a parity error on "in" phases, so all we
1936 * need to do is stuff the message buffer with
1937 * the appropriate message. "In" phases have set
1938 * mesg_out to something other than MSG_NOP.
1940 ahd->send_msg_perror = msg_out;
1941 if (scb != NULL && msg_out == MSG_INITIATOR_DET_ERR)
1942 scb->flags |= SCB_TRANSMISSION_ERROR;
1943 ahd_outb(ahd, MSG_OUT, HOST_MSG);
1944 ahd_outb(ahd, CLRINT, CLRSCSIINT);
1949 ahd_handle_lqiphase_error(struct ahd_softc *ahd, u_int lqistat1)
1952 * Clear the sources of the interrupts.
1954 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
1955 ahd_outb(ahd, CLRLQIINT1, lqistat1);
1958 * If the "illegal" phase changes were in response
1959 * to our ATN to flag a CRC error, AND we ended up
1960 * on packet boundaries, clear the error, restart the
1961 * LQI manager as appropriate, and go on our merry
1962 * way toward sending the message. Otherwise, reset
1963 * the bus to clear the error.
1965 ahd_set_active_fifo(ahd);
1966 if ((ahd_inb(ahd, SCSISIGO) & ATNO) != 0
1967 && (ahd_inb(ahd, MDFFSTAT) & DLZERO) != 0) {
1968 if ((lqistat1 & LQIPHASE_LQ) != 0) {
1969 kprintf("LQIRETRY for LQIPHASE_LQ\n");
1970 ahd_outb(ahd, LQCTL2, LQIRETRY);
1971 } else if ((lqistat1 & LQIPHASE_NLQ) != 0) {
1972 kprintf("LQIRETRY for LQIPHASE_NLQ\n");
1973 ahd_outb(ahd, LQCTL2, LQIRETRY);
1975 panic("ahd_handle_lqiphase_error: No phase errors\n");
1976 ahd_dump_card_state(ahd);
1977 ahd_outb(ahd, CLRINT, CLRSCSIINT);
1980 kprintf("Reseting Channel for LQI Phase error\n");
1981 ahd_dump_card_state(ahd);
1982 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
1987 * Packetized unexpected or expected busfree.
1988 * Entered in mode based on busfreetime.
1991 ahd_handle_pkt_busfree(struct ahd_softc *ahd, u_int busfreetime)
1995 AHD_ASSERT_MODES(ahd, ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK),
1996 ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK));
1997 lqostat1 = ahd_inb(ahd, LQOSTAT1);
1998 if ((lqostat1 & LQOBUSFREE) != 0) {
2006 if ((busfreetime & BUSFREE_LQO) == 0)
2007 kprintf("%s: Warning, BUSFREE time is 0x%x. "
2008 "Expected BUSFREE_LQO.\n",
2009 ahd_name(ahd), busfreetime);
2011 * The LQO manager detected an unexpected busfree
2014 * 1) During an outgoing LQ.
2015 * 2) After an outgoing LQ but before the first
2016 * REQ of the command packet.
2017 * 3) During an outgoing command packet.
2019 * In all cases, CURRSCB is pointing to the
2020 * SCB that encountered the failure. Clean
2021 * up the queue, clear SELDO and LQOBUSFREE,
2022 * and allow the sequencer to restart the select
2023 * out at its lesure.
2025 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
2026 scbid = ahd_inw(ahd, CURRSCB);
2027 scb = ahd_lookup_scb(ahd, scbid);
2029 panic("SCB not valid during LQOBUSFREE");
2033 ahd_outb(ahd, CLRLQOINT1, CLRLQOBUSFREE);
2034 if ((ahd->bugs & AHD_CLRLQO_AUTOCLR_BUG) != 0)
2035 ahd_outb(ahd, CLRLQOINT1, 0);
2036 ahd_outb(ahd, SCSISEQ0, ahd_inb(ahd, SCSISEQ0) & ~ENSELO);
2037 ahd_flush_device_writes(ahd);
2038 ahd_outb(ahd, CLRSINT0, CLRSELDO);
2041 * Return the LQO manager to its idle loop. It will
2042 * not do this automatically if the busfree occurs
2043 * after the first REQ of either the LQ or command
2044 * packet or between the LQ and command packet.
2046 ahd_outb(ahd, LQCTL2, ahd_inb(ahd, LQCTL2) | LQOTOIDLE);
2049 * Update the waiting for selection queue so
2050 * we restart on the correct SCB.
2052 waiting_h = ahd_inw(ahd, WAITING_TID_HEAD);
2053 saved_scbptr = ahd_get_scbptr(ahd);
2054 if (waiting_h != scbid) {
2056 ahd_outw(ahd, WAITING_TID_HEAD, scbid);
2057 waiting_t = ahd_inw(ahd, WAITING_TID_TAIL);
2058 if (waiting_t == waiting_h) {
2059 ahd_outw(ahd, WAITING_TID_TAIL, scbid);
2060 next = SCB_LIST_NULL;
2062 ahd_set_scbptr(ahd, waiting_h);
2063 next = ahd_inw_scbram(ahd, SCB_NEXT2);
2065 ahd_set_scbptr(ahd, scbid);
2066 ahd_outw(ahd, SCB_NEXT2, next);
2068 ahd_set_scbptr(ahd, saved_scbptr);
2069 if (scb->crc_retry_count < AHD_MAX_LQ_CRC_ERRORS) {
2070 if (SCB_IS_SILENT(scb) == FALSE) {
2071 ahd_print_path(ahd, scb);
2072 kprintf("Probable outgoing LQ CRC error. "
2073 "Retrying command\n");
2075 scb->crc_retry_count++;
2077 aic_set_transaction_status(scb, CAM_UNCOR_PARITY);
2078 aic_freeze_scb(scb);
2079 ahd_freeze_devq(ahd, scb);
2081 /* Return unpausing the sequencer. */
2083 } else if ((ahd_inb(ahd, PERRDIAG) & PARITYERR) != 0) {
2085 * Ignore what are really parity errors that
2086 * occur on the last REQ of a free running
2087 * clock prior to going busfree. Some drives
2088 * do not properly active negate just before
2089 * going busfree resulting in a parity glitch.
2091 ahd_outb(ahd, CLRSINT1, CLRSCSIPERR|CLRBUSFREE);
2093 if ((ahd_debug & AHD_SHOW_MASKED_ERRORS) != 0)
2094 kprintf("%s: Parity on last REQ detected "
2095 "during busfree phase.\n",
2098 /* Return unpausing the sequencer. */
2101 if (ahd->src_mode != AHD_MODE_SCSI) {
2105 scbid = ahd_get_scbptr(ahd);
2106 scb = ahd_lookup_scb(ahd, scbid);
2107 ahd_print_path(ahd, scb);
2108 kprintf("Unexpected PKT busfree condition\n");
2109 ahd_dump_card_state(ahd);
2110 ahd_abort_scbs(ahd, SCB_GET_TARGET(ahd, scb), 'A',
2111 SCB_GET_LUN(scb), SCB_GET_TAG(scb),
2112 ROLE_INITIATOR, CAM_UNEXP_BUSFREE);
2114 /* Return restarting the sequencer. */
2117 kprintf("%s: Unexpected PKT busfree condition\n", ahd_name(ahd));
2118 ahd_dump_card_state(ahd);
2119 /* Restart the sequencer. */
2124 * Non-packetized unexpected or expected busfree.
2127 ahd_handle_nonpkt_busfree(struct ahd_softc *ahd)
2129 struct ahd_devinfo devinfo;
2135 u_int initiator_role_id;
2141 * Look at what phase we were last in. If its message out,
2142 * chances are pretty good that the busfree was in response
2143 * to one of our abort requests.
2145 lastphase = ahd_inb(ahd, LASTPHASE);
2146 saved_scsiid = ahd_inb(ahd, SAVED_SCSIID);
2147 saved_lun = ahd_inb(ahd, SAVED_LUN);
2148 target = SCSIID_TARGET(ahd, saved_scsiid);
2149 initiator_role_id = SCSIID_OUR_ID(saved_scsiid);
2150 ahd_compile_devinfo(&devinfo, initiator_role_id,
2151 target, saved_lun, 'A', ROLE_INITIATOR);
2154 scbid = ahd_get_scbptr(ahd);
2155 scb = ahd_lookup_scb(ahd, scbid);
2157 && (ahd_inb(ahd, SEQ_FLAGS) & NOT_IDENTIFIED) != 0)
2160 ppr_busfree = (ahd->msg_flags & MSG_FLAG_EXPECT_PPR_BUSFREE) != 0;
2161 if (lastphase == P_MESGOUT) {
2164 tag = SCB_LIST_NULL;
2165 if (ahd_sent_msg(ahd, AHDMSG_1B, MSG_ABORT_TAG, TRUE)
2166 || ahd_sent_msg(ahd, AHDMSG_1B, MSG_ABORT, TRUE)) {
2171 ahd_print_devinfo(ahd, &devinfo);
2172 kprintf("Abort for unidentified "
2173 "connection completed.\n");
2174 /* restart the sequencer. */
2177 sent_msg = ahd->msgout_buf[ahd->msgout_index - 1];
2178 ahd_print_path(ahd, scb);
2179 kprintf("SCB %d - Abort%s Completed.\n",
2181 sent_msg == MSG_ABORT_TAG ? "" : " Tag");
2183 if (sent_msg == MSG_ABORT_TAG)
2184 tag = SCB_GET_TAG(scb);
2186 if ((scb->flags & SCB_CMDPHASE_ABORT) != 0) {
2188 * This abort is in response to an
2189 * unexpected switch to command phase
2190 * for a packetized connection. Since
2191 * the identify message was never sent,
2192 * "saved lun" is 0. We really want to
2193 * abort only the SCB that encountered
2194 * this error, which could have a different
2195 * lun. The SCB will be retried so the OS
2196 * will see the UA after renegotiating to
2199 tag = SCB_GET_TAG(scb);
2200 saved_lun = scb->hscb->lun;
2202 found = ahd_abort_scbs(ahd, target, 'A', saved_lun,
2203 tag, ROLE_INITIATOR,
2205 kprintf("found == 0x%x\n", found);
2207 } else if (ahd_sent_msg(ahd, AHDMSG_1B,
2208 MSG_BUS_DEV_RESET, TRUE)) {
2209 #if defined(__DragonFly__) || defined(__FreeBSD__)
2211 * Don't mark the user's request for this BDR
2212 * as completing with CAM_BDR_SENT. CAM3
2213 * specifies CAM_REQ_CMP.
2216 && scb->io_ctx->ccb_h.func_code== XPT_RESET_DEV
2217 && ahd_match_scb(ahd, scb, target, 'A',
2218 CAM_LUN_WILDCARD, SCB_LIST_NULL,
2220 aic_set_transaction_status(scb, CAM_REQ_CMP);
2222 ahd_handle_devreset(ahd, &devinfo, CAM_LUN_WILDCARD,
2223 CAM_BDR_SENT, "Bus Device Reset",
2224 /*verbose_level*/0);
2226 } else if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_PPR, FALSE)
2227 && ppr_busfree == 0) {
2228 struct ahd_initiator_tinfo *tinfo;
2229 struct ahd_tmode_tstate *tstate;
2232 * PPR Rejected. Try non-ppr negotiation
2233 * and retry command.
2236 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
2237 kprintf("PPR negotiation rejected busfree.\n");
2239 tinfo = ahd_fetch_transinfo(ahd, devinfo.channel,
2241 devinfo.target, &tstate);
2242 tinfo->curr.transport_version = 2;
2243 tinfo->goal.transport_version = 2;
2244 tinfo->goal.ppr_options = 0;
2245 ahd_qinfifo_requeue_tail(ahd, scb);
2247 } else if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_WDTR, FALSE)
2248 && ppr_busfree == 0) {
2250 * Negotiation Rejected. Go-narrow and
2254 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
2255 kprintf("WDTR negotiation rejected busfree.\n");
2257 ahd_set_width(ahd, &devinfo,
2258 MSG_EXT_WDTR_BUS_8_BIT,
2259 AHD_TRANS_CUR|AHD_TRANS_GOAL,
2261 ahd_qinfifo_requeue_tail(ahd, scb);
2263 } else if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_SDTR, FALSE)
2264 && ppr_busfree == 0) {
2266 * Negotiation Rejected. Go-async and
2270 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
2271 kprintf("SDTR negotiation rejected busfree.\n");
2273 ahd_set_syncrate(ahd, &devinfo,
2274 /*period*/0, /*offset*/0,
2276 AHD_TRANS_CUR|AHD_TRANS_GOAL,
2278 ahd_qinfifo_requeue_tail(ahd, scb);
2280 } else if ((ahd->msg_flags & MSG_FLAG_EXPECT_IDE_BUSFREE) != 0
2281 && ahd_sent_msg(ahd, AHDMSG_1B,
2282 MSG_INITIATOR_DET_ERR, TRUE)) {
2285 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
2286 kprintf("Expected IDE Busfree\n");
2289 } else if ((ahd->msg_flags & MSG_FLAG_EXPECT_QASREJ_BUSFREE)
2290 && ahd_sent_msg(ahd, AHDMSG_1B,
2291 MSG_MESSAGE_REJECT, TRUE)) {
2294 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
2295 kprintf("Expected QAS Reject Busfree\n");
2302 * The busfree required flag is honored at the end of
2303 * the message phases. We check it last in case we
2304 * had to send some other message that caused a busfree.
2307 && (lastphase == P_MESGIN || lastphase == P_MESGOUT)
2308 && ((ahd->msg_flags & MSG_FLAG_EXPECT_PPR_BUSFREE) != 0)) {
2310 ahd_freeze_devq(ahd, scb);
2311 aic_set_transaction_status(scb, CAM_REQUEUE_REQ);
2312 aic_freeze_scb(scb);
2313 if ((ahd->msg_flags & MSG_FLAG_IU_REQ_CHANGED) != 0) {
2314 ahd_abort_scbs(ahd, SCB_GET_TARGET(ahd, scb),
2315 SCB_GET_CHANNEL(ahd, scb),
2316 SCB_GET_LUN(scb), SCB_LIST_NULL,
2317 ROLE_INITIATOR, CAM_REQ_ABORTED);
2320 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
2321 kprintf("PPR Negotiation Busfree.\n");
2327 if (printerror != 0) {
2334 if ((scb->hscb->control & TAG_ENB) != 0)
2335 tag = SCB_GET_TAG(scb);
2337 tag = SCB_LIST_NULL;
2338 ahd_print_path(ahd, scb);
2339 aborted = ahd_abort_scbs(ahd, target, 'A',
2340 SCB_GET_LUN(scb), tag,
2345 * We had not fully identified this connection,
2346 * so we cannot abort anything.
2348 kprintf("%s: ", ahd_name(ahd));
2350 if (lastphase != P_BUSFREE)
2351 ahd_force_renegotiation(ahd, &devinfo);
2352 kprintf("Unexpected busfree %s, %d SCBs aborted, "
2353 "PRGMCNT == 0x%x\n",
2354 ahd_lookup_phase_entry(lastphase)->phasemsg,
2356 ahd_inw(ahd, PRGMCNT));
2357 ahd_dump_card_state(ahd);
2359 /* Always restart the sequencer. */
2364 ahd_handle_proto_violation(struct ahd_softc *ahd)
2366 struct ahd_devinfo devinfo;
2374 ahd_fetch_devinfo(ahd, &devinfo);
2375 scbid = ahd_get_scbptr(ahd);
2376 scb = ahd_lookup_scb(ahd, scbid);
2377 seq_flags = ahd_inb(ahd, SEQ_FLAGS);
2378 curphase = ahd_inb(ahd, SCSISIGI) & PHASE_MASK;
2379 lastphase = ahd_inb(ahd, LASTPHASE);
2380 if ((seq_flags & NOT_IDENTIFIED) != 0) {
2383 * The reconnecting target either did not send an
2384 * identify message, or did, but we didn't find an SCB
2387 ahd_print_devinfo(ahd, &devinfo);
2388 kprintf("Target did not send an IDENTIFY message. "
2389 "LASTPHASE = 0x%x.\n", lastphase);
2391 } else if (scb == NULL) {
2393 * We don't seem to have an SCB active for this
2394 * transaction. Print an error and reset the bus.
2396 ahd_print_devinfo(ahd, &devinfo);
2397 kprintf("No SCB found during protocol violation\n");
2398 goto proto_violation_reset;
2400 aic_set_transaction_status(scb, CAM_SEQUENCE_FAIL);
2401 if ((seq_flags & NO_CDB_SENT) != 0) {
2402 ahd_print_path(ahd, scb);
2403 kprintf("No or incomplete CDB sent to device.\n");
2404 } else if ((ahd_inb_scbram(ahd, SCB_CONTROL)
2405 & STATUS_RCVD) == 0) {
2407 * The target never bothered to provide status to
2408 * us prior to completing the command. Since we don't
2409 * know the disposition of this command, we must attempt
2410 * to abort it. Assert ATN and prepare to send an abort
2413 ahd_print_path(ahd, scb);
2414 kprintf("Completed command without status.\n");
2416 ahd_print_path(ahd, scb);
2417 kprintf("Unknown protocol violation.\n");
2418 ahd_dump_card_state(ahd);
2421 if ((lastphase & ~P_DATAIN_DT) == 0
2422 || lastphase == P_COMMAND) {
2423 proto_violation_reset:
2425 * Target either went directly to data
2426 * phase or didn't respond to our ATN.
2427 * The only safe thing to do is to blow
2428 * it away with a bus reset.
2430 found = ahd_reset_channel(ahd, 'A', TRUE);
2431 kprintf("%s: Issued Channel %c Bus Reset. "
2432 "%d SCBs aborted\n", ahd_name(ahd), 'A', found);
2435 * Leave the selection hardware off in case
2436 * this abort attempt will affect yet to
2439 ahd_outb(ahd, SCSISEQ0,
2440 ahd_inb(ahd, SCSISEQ0) & ~ENSELO);
2441 ahd_assert_atn(ahd);
2442 ahd_outb(ahd, MSG_OUT, HOST_MSG);
2444 ahd_print_devinfo(ahd, &devinfo);
2445 ahd->msgout_buf[0] = MSG_ABORT_TASK;
2446 ahd->msgout_len = 1;
2447 ahd->msgout_index = 0;
2448 ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
2450 ahd_print_path(ahd, scb);
2451 scb->flags |= SCB_ABORT;
2453 kprintf("Protocol violation %s. Attempting to abort.\n",
2454 ahd_lookup_phase_entry(curphase)->phasemsg);
2459 * Force renegotiation to occur the next time we initiate
2460 * a command to the current device.
2463 ahd_force_renegotiation(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
2465 struct ahd_initiator_tinfo *targ_info;
2466 struct ahd_tmode_tstate *tstate;
2469 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) {
2470 ahd_print_devinfo(ahd, devinfo);
2471 kprintf("Forcing renegotiation\n");
2474 targ_info = ahd_fetch_transinfo(ahd,
2476 devinfo->our_scsiid,
2479 ahd_update_neg_request(ahd, devinfo, tstate,
2480 targ_info, AHD_NEG_IF_NON_ASYNC);
2483 #define AHD_MAX_STEPS 2000
2485 ahd_clear_critical_section(struct ahd_softc *ahd)
2487 ahd_mode_state saved_modes;
2499 if (ahd->num_critical_sections == 0)
2512 saved_modes = ahd_save_modes(ahd);
2518 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
2519 seqaddr = ahd_inw(ahd, CURADDR);
2521 cs = ahd->critical_sections;
2522 for (i = 0; i < ahd->num_critical_sections; i++, cs++) {
2524 if (cs->begin < seqaddr && cs->end >= seqaddr)
2528 if (i == ahd->num_critical_sections)
2531 if (steps > AHD_MAX_STEPS) {
2532 kprintf("%s: Infinite loop in critical section\n"
2533 "%s: First Instruction 0x%x now 0x%x\n",
2534 ahd_name(ahd), ahd_name(ahd), first_instr,
2536 ahd_dump_card_state(ahd);
2537 panic("critical section loop");
2542 if ((ahd_debug & AHD_SHOW_MISC) != 0)
2543 kprintf("%s: Single stepping at 0x%x\n", ahd_name(ahd),
2546 if (stepping == FALSE) {
2548 first_instr = seqaddr;
2549 ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
2550 simode0 = ahd_inb(ahd, SIMODE0);
2551 simode3 = ahd_inb(ahd, SIMODE3);
2552 lqimode0 = ahd_inb(ahd, LQIMODE0);
2553 lqimode1 = ahd_inb(ahd, LQIMODE1);
2554 lqomode0 = ahd_inb(ahd, LQOMODE0);
2555 lqomode1 = ahd_inb(ahd, LQOMODE1);
2556 ahd_outb(ahd, SIMODE0, 0);
2557 ahd_outb(ahd, SIMODE3, 0);
2558 ahd_outb(ahd, LQIMODE0, 0);
2559 ahd_outb(ahd, LQIMODE1, 0);
2560 ahd_outb(ahd, LQOMODE0, 0);
2561 ahd_outb(ahd, LQOMODE1, 0);
2562 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
2563 simode1 = ahd_inb(ahd, SIMODE1);
2565 * We don't clear ENBUSFREE. Unfortunately
2566 * we cannot re-enable busfree detection within
2567 * the current connection, so we must leave it
2568 * on while single stepping.
2570 ahd_outb(ahd, SIMODE1, simode1 & ENBUSFREE);
2571 ahd_outb(ahd, SEQCTL0, ahd_inb(ahd, SEQCTL0) | STEP);
2574 ahd_outb(ahd, CLRSINT1, CLRBUSFREE);
2575 ahd_outb(ahd, CLRINT, CLRSCSIINT);
2576 ahd_set_modes(ahd, ahd->saved_src_mode, ahd->saved_dst_mode);
2577 ahd_outb(ahd, HCNTRL, ahd->unpause);
2578 while (!ahd_is_paused(ahd))
2580 ahd_update_modes(ahd);
2583 ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
2584 ahd_outb(ahd, SIMODE0, simode0);
2585 ahd_outb(ahd, SIMODE3, simode3);
2586 ahd_outb(ahd, LQIMODE0, lqimode0);
2587 ahd_outb(ahd, LQIMODE1, lqimode1);
2588 ahd_outb(ahd, LQOMODE0, lqomode0);
2589 ahd_outb(ahd, LQOMODE1, lqomode1);
2590 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
2591 ahd_outb(ahd, SEQCTL0, ahd_inb(ahd, SEQCTL0) & ~STEP);
2592 ahd_outb(ahd, SIMODE1, simode1);
2594 * SCSIINT seems to glitch occassionally when
2595 * the interrupt masks are restored. Clear SCSIINT
2596 * one more time so that only persistent errors
2597 * are seen as a real interrupt.
2599 ahd_outb(ahd, CLRINT, CLRSCSIINT);
2601 ahd_restore_modes(ahd, saved_modes);
2605 * Clear any pending interrupt status.
2608 ahd_clear_intstat(struct ahd_softc *ahd)
2610 AHD_ASSERT_MODES(ahd, ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK),
2611 ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK));
2612 /* Clear any interrupt conditions this may have caused */
2613 ahd_outb(ahd, CLRLQIINT0, CLRLQIATNQAS|CLRLQICRCT1|CLRLQICRCT2
2614 |CLRLQIBADLQT|CLRLQIATNLQ|CLRLQIATNCMD);
2615 ahd_outb(ahd, CLRLQIINT1, CLRLQIPHASE_LQ|CLRLQIPHASE_NLQ|CLRLIQABORT
2616 |CLRLQICRCI_LQ|CLRLQICRCI_NLQ|CLRLQIBADLQI
2617 |CLRLQIOVERI_LQ|CLRLQIOVERI_NLQ|CLRNONPACKREQ);
2618 ahd_outb(ahd, CLRLQOINT0, CLRLQOTARGSCBPERR|CLRLQOSTOPT2|CLRLQOATNLQ
2619 |CLRLQOATNPKT|CLRLQOTCRC);
2620 ahd_outb(ahd, CLRLQOINT1, CLRLQOINITSCBPERR|CLRLQOSTOPI2|CLRLQOBADQAS
2621 |CLRLQOBUSFREE|CLRLQOPHACHGINPKT);
2622 if ((ahd->bugs & AHD_CLRLQO_AUTOCLR_BUG) != 0) {
2623 ahd_outb(ahd, CLRLQOINT0, 0);
2624 ahd_outb(ahd, CLRLQOINT1, 0);
2626 ahd_outb(ahd, CLRSINT3, CLRNTRAMPERR|CLROSRAMPERR);
2627 ahd_outb(ahd, CLRSINT1, CLRSELTIMEO|CLRATNO|CLRSCSIRSTI
2628 |CLRBUSFREE|CLRSCSIPERR|CLRREQINIT);
2629 ahd_outb(ahd, CLRSINT0, CLRSELDO|CLRSELDI|CLRSELINGO
2630 |CLRIOERR|CLROVERRUN);
2631 ahd_outb(ahd, CLRINT, CLRSCSIINT);
2634 /**************************** Debugging Routines ******************************/
2636 uint32_t ahd_debug = AHD_DEBUG_OPTS;
2639 ahd_print_scb(struct scb *scb)
2641 struct hardware_scb *hscb;
2645 kprintf("scb:%p control:0x%x scsiid:0x%x lun:%d cdb_len:%d\n",
2651 kprintf("Shared Data: ");
2652 for (i = 0; i < sizeof(hscb->shared_data.idata.cdb); i++)
2653 kprintf("%#02x", hscb->shared_data.idata.cdb[i]);
2654 kprintf(" dataptr:%#x%x datacnt:%#x sgptr:%#x tag:%#x\n",
2655 (uint32_t)((aic_le64toh(hscb->dataptr) >> 32) & 0xFFFFFFFF),
2656 (uint32_t)(aic_le64toh(hscb->dataptr) & 0xFFFFFFFF),
2657 aic_le32toh(hscb->datacnt),
2658 aic_le32toh(hscb->sgptr),
2660 ahd_dump_sglist(scb);
2664 ahd_dump_sglist(struct scb *scb)
2668 if (scb->sg_count > 0) {
2669 if ((scb->ahd_softc->flags & AHD_64BIT_ADDRESSING) != 0) {
2670 struct ahd_dma64_seg *sg_list;
2672 sg_list = (struct ahd_dma64_seg*)scb->sg_list;
2673 for (i = 0; i < scb->sg_count; i++) {
2677 addr = aic_le64toh(sg_list[i].addr);
2678 len = aic_le32toh(sg_list[i].len);
2679 kprintf("sg[%d] - Addr 0x%x%x : Length %d%s\n",
2681 (uint32_t)((addr >> 32) & 0xFFFFFFFF),
2682 (uint32_t)(addr & 0xFFFFFFFF),
2683 sg_list[i].len & AHD_SG_LEN_MASK,
2684 (sg_list[i].len & AHD_DMA_LAST_SEG)
2688 struct ahd_dma_seg *sg_list;
2690 sg_list = (struct ahd_dma_seg*)scb->sg_list;
2691 for (i = 0; i < scb->sg_count; i++) {
2694 len = aic_le32toh(sg_list[i].len);
2695 kprintf("sg[%d] - Addr 0x%x%x : Length %d%s\n",
2697 (len & AHD_SG_HIGH_ADDR_MASK) >> 24,
2698 aic_le32toh(sg_list[i].addr),
2699 len & AHD_SG_LEN_MASK,
2700 len & AHD_DMA_LAST_SEG ? " Last" : "");
2706 /************************* Transfer Negotiation *******************************/
2708 * Allocate per target mode instance (ID we respond to as a target)
2709 * transfer negotiation data structures.
2711 static struct ahd_tmode_tstate *
2712 ahd_alloc_tstate(struct ahd_softc *ahd, u_int scsi_id, char channel)
2714 struct ahd_tmode_tstate *master_tstate;
2715 struct ahd_tmode_tstate *tstate;
2718 master_tstate = ahd->enabled_targets[ahd->our_id];
2719 if (ahd->enabled_targets[scsi_id] != NULL
2720 && ahd->enabled_targets[scsi_id] != master_tstate)
2721 panic("%s: ahd_alloc_tstate - Target already allocated",
2723 tstate = kmalloc(sizeof(*tstate), M_DEVBUF, M_INTWAIT);
2726 * If we have allocated a master tstate, copy user settings from
2727 * the master tstate (taken from SRAM or the EEPROM) for this
2728 * channel, but reset our current and goal settings to async/narrow
2729 * until an initiator talks to us.
2731 if (master_tstate != NULL) {
2732 memcpy(tstate, master_tstate, sizeof(*tstate));
2733 memset(tstate->enabled_luns, 0, sizeof(tstate->enabled_luns));
2734 for (i = 0; i < 16; i++) {
2735 memset(&tstate->transinfo[i].curr, 0,
2736 sizeof(tstate->transinfo[i].curr));
2737 memset(&tstate->transinfo[i].goal, 0,
2738 sizeof(tstate->transinfo[i].goal));
2741 memset(tstate, 0, sizeof(*tstate));
2742 ahd->enabled_targets[scsi_id] = tstate;
2746 #ifdef AHD_TARGET_MODE
2748 * Free per target mode instance (ID we respond to as a target)
2749 * transfer negotiation data structures.
2752 ahd_free_tstate(struct ahd_softc *ahd, u_int scsi_id, char channel, int force)
2754 struct ahd_tmode_tstate *tstate;
2757 * Don't clean up our "master" tstate.
2758 * It has our default user settings.
2760 if (scsi_id == ahd->our_id
2764 tstate = ahd->enabled_targets[scsi_id];
2766 kfree(tstate, M_DEVBUF);
2767 ahd->enabled_targets[scsi_id] = NULL;
2772 * Called when we have an active connection to a target on the bus,
2773 * this function finds the nearest period to the input period limited
2774 * by the capabilities of the bus connectivity of and sync settings for
2778 ahd_devlimited_syncrate(struct ahd_softc *ahd,
2779 struct ahd_initiator_tinfo *tinfo,
2780 u_int *period, u_int *ppr_options, role_t role)
2782 struct ahd_transinfo *transinfo;
2785 if ((ahd_inb(ahd, SBLKCTL) & ENAB40) != 0
2786 && (ahd_inb(ahd, SSTAT2) & EXP_ACTIVE) == 0) {
2787 maxsync = AHD_SYNCRATE_PACED;
2789 maxsync = AHD_SYNCRATE_ULTRA;
2790 /* Can't do DT related options on an SE bus */
2791 *ppr_options &= MSG_EXT_PPR_QAS_REQ;
2794 * Never allow a value higher than our current goal
2795 * period otherwise we may allow a target initiated
2796 * negotiation to go above the limit as set by the
2797 * user. In the case of an initiator initiated
2798 * sync negotiation, we limit based on the user
2799 * setting. This allows the system to still accept
2800 * incoming negotiations even if target initiated
2801 * negotiation is not performed.
2803 if (role == ROLE_TARGET)
2804 transinfo = &tinfo->user;
2806 transinfo = &tinfo->goal;
2807 *ppr_options &= (transinfo->ppr_options|MSG_EXT_PPR_PCOMP_EN);
2808 if (transinfo->width == MSG_EXT_WDTR_BUS_8_BIT) {
2809 maxsync = MAX(maxsync, AHD_SYNCRATE_ULTRA2);
2810 *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
2812 if (transinfo->period == 0) {
2816 *period = MAX(*period, transinfo->period);
2817 ahd_find_syncrate(ahd, period, ppr_options, maxsync);
2822 * Look up the valid period to SCSIRATE conversion in our table.
2823 * Return the period and offset that should be sent to the target
2824 * if this was the beginning of an SDTR.
2827 ahd_find_syncrate(struct ahd_softc *ahd, u_int *period,
2828 u_int *ppr_options, u_int maxsync)
2830 if (*period < maxsync)
2833 if ((*ppr_options & MSG_EXT_PPR_DT_REQ) != 0
2834 && *period > AHD_SYNCRATE_MIN_DT)
2835 *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
2837 if (*period > AHD_SYNCRATE_MIN)
2840 /* Honor PPR option conformance rules. */
2841 if (*period > AHD_SYNCRATE_PACED)
2842 *ppr_options &= ~MSG_EXT_PPR_RTI;
2844 if ((*ppr_options & MSG_EXT_PPR_IU_REQ) == 0)
2845 *ppr_options &= (MSG_EXT_PPR_DT_REQ|MSG_EXT_PPR_QAS_REQ);
2847 if ((*ppr_options & MSG_EXT_PPR_DT_REQ) == 0)
2848 *ppr_options &= MSG_EXT_PPR_QAS_REQ;
2850 /* Skip all PACED only entries if IU is not available */
2851 if ((*ppr_options & MSG_EXT_PPR_IU_REQ) == 0
2852 && *period < AHD_SYNCRATE_DT)
2853 *period = AHD_SYNCRATE_DT;
2855 /* Skip all DT only entries if DT is not available */
2856 if ((*ppr_options & MSG_EXT_PPR_DT_REQ) == 0
2857 && *period < AHD_SYNCRATE_ULTRA2)
2858 *period = AHD_SYNCRATE_ULTRA2;
2862 * Truncate the given synchronous offset to a value the
2863 * current adapter type and syncrate are capable of.
2866 ahd_validate_offset(struct ahd_softc *ahd,
2867 struct ahd_initiator_tinfo *tinfo,
2868 u_int period, u_int *offset, int wide,
2873 /* Limit offset to what we can do */
2876 else if (period <= AHD_SYNCRATE_PACED) {
2877 if ((ahd->bugs & AHD_PACED_NEGTABLE_BUG) != 0)
2878 maxoffset = MAX_OFFSET_PACED_BUG;
2880 maxoffset = MAX_OFFSET_PACED;
2882 maxoffset = MAX_OFFSET_NON_PACED;
2883 *offset = MIN(*offset, maxoffset);
2884 if (tinfo != NULL) {
2885 if (role == ROLE_TARGET)
2886 *offset = MIN(*offset, tinfo->user.offset);
2888 *offset = MIN(*offset, tinfo->goal.offset);
2893 * Truncate the given transfer width parameter to a value the
2894 * current adapter type is capable of.
2897 ahd_validate_width(struct ahd_softc *ahd, struct ahd_initiator_tinfo *tinfo,
2898 u_int *bus_width, role_t role)
2900 switch (*bus_width) {
2902 if (ahd->features & AHD_WIDE) {
2904 *bus_width = MSG_EXT_WDTR_BUS_16_BIT;
2908 case MSG_EXT_WDTR_BUS_8_BIT:
2909 *bus_width = MSG_EXT_WDTR_BUS_8_BIT;
2912 if (tinfo != NULL) {
2913 if (role == ROLE_TARGET)
2914 *bus_width = MIN(tinfo->user.width, *bus_width);
2916 *bus_width = MIN(tinfo->goal.width, *bus_width);
2921 * Update the bitmask of targets for which the controller should
2922 * negotiate with at the next convenient oportunity. This currently
2923 * means the next time we send the initial identify messages for
2924 * a new transaction.
2927 ahd_update_neg_request(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
2928 struct ahd_tmode_tstate *tstate,
2929 struct ahd_initiator_tinfo *tinfo, ahd_neg_type neg_type)
2931 u_int auto_negotiate_orig;
2933 auto_negotiate_orig = tstate->auto_negotiate;
2934 if (neg_type == AHD_NEG_ALWAYS) {
2936 * Force our "current" settings to be
2937 * unknown so that unless a bus reset
2938 * occurs the need to renegotiate is
2939 * recorded persistently.
2941 if ((ahd->features & AHD_WIDE) != 0)
2942 tinfo->curr.width = AHD_WIDTH_UNKNOWN;
2943 tinfo->curr.period = AHD_PERIOD_UNKNOWN;
2944 tinfo->curr.offset = AHD_OFFSET_UNKNOWN;
2946 if (tinfo->curr.period != tinfo->goal.period
2947 || tinfo->curr.width != tinfo->goal.width
2948 || tinfo->curr.offset != tinfo->goal.offset
2949 || tinfo->curr.ppr_options != tinfo->goal.ppr_options
2950 || (neg_type == AHD_NEG_IF_NON_ASYNC
2951 && (tinfo->goal.offset != 0
2952 || tinfo->goal.width != MSG_EXT_WDTR_BUS_8_BIT
2953 || tinfo->goal.ppr_options != 0)))
2954 tstate->auto_negotiate |= devinfo->target_mask;
2956 tstate->auto_negotiate &= ~devinfo->target_mask;
2958 return (auto_negotiate_orig != tstate->auto_negotiate);
2962 * Update the user/goal/curr tables of synchronous negotiation
2963 * parameters as well as, in the case of a current or active update,
2964 * any data structures on the host controller. In the case of an
2965 * active update, the specified target is currently talking to us on
2966 * the bus, so the transfer parameter update must take effect
2970 ahd_set_syncrate(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
2971 u_int period, u_int offset, u_int ppr_options,
2972 u_int type, int paused)
2974 struct ahd_initiator_tinfo *tinfo;
2975 struct ahd_tmode_tstate *tstate;
2982 active = (type & AHD_TRANS_ACTIVE) == AHD_TRANS_ACTIVE;
2985 if (period == 0 || offset == 0) {
2990 tinfo = ahd_fetch_transinfo(ahd, devinfo->channel, devinfo->our_scsiid,
2991 devinfo->target, &tstate);
2993 if ((type & AHD_TRANS_USER) != 0) {
2994 tinfo->user.period = period;
2995 tinfo->user.offset = offset;
2996 tinfo->user.ppr_options = ppr_options;
2999 if ((type & AHD_TRANS_GOAL) != 0) {
3000 tinfo->goal.period = period;
3001 tinfo->goal.offset = offset;
3002 tinfo->goal.ppr_options = ppr_options;
3005 old_period = tinfo->curr.period;
3006 old_offset = tinfo->curr.offset;
3007 old_ppr = tinfo->curr.ppr_options;
3009 if ((type & AHD_TRANS_CUR) != 0
3010 && (old_period != period
3011 || old_offset != offset
3012 || old_ppr != ppr_options)) {
3016 tinfo->curr.period = period;
3017 tinfo->curr.offset = offset;
3018 tinfo->curr.ppr_options = ppr_options;
3020 ahd_send_async(ahd, devinfo->channel, devinfo->target,
3021 CAM_LUN_WILDCARD, AC_TRANSFER_NEG, NULL);
3026 kprintf("%s: target %d synchronous with "
3027 "period = 0x%x, offset = 0x%x",
3028 ahd_name(ahd), devinfo->target,
3031 if ((ppr_options & MSG_EXT_PPR_RD_STRM) != 0) {
3035 if ((ppr_options & MSG_EXT_PPR_DT_REQ) != 0) {
3036 kprintf("%s", options ? "|DT" : "(DT");
3039 if ((ppr_options & MSG_EXT_PPR_IU_REQ) != 0) {
3040 kprintf("%s", options ? "|IU" : "(IU");
3043 if ((ppr_options & MSG_EXT_PPR_RTI) != 0) {
3044 kprintf("%s", options ? "|RTI" : "(RTI");
3047 if ((ppr_options & MSG_EXT_PPR_QAS_REQ) != 0) {
3048 kprintf("%s", options ? "|QAS" : "(QAS");
3056 kprintf("%s: target %d using "
3057 "asynchronous transfers%s\n",
3058 ahd_name(ahd), devinfo->target,
3059 (ppr_options & MSG_EXT_PPR_QAS_REQ) != 0
3065 * Always refresh the neg-table to handle the case of the
3066 * sequencer setting the ENATNO bit for a MK_MESSAGE request.
3067 * We will always renegotiate in that case if this is a
3068 * packetized request. Also manage the busfree expected flag
3069 * from this common routine so that we catch changes due to
3070 * WDTR or SDTR messages.
3072 if ((type & AHD_TRANS_CUR) != 0) {
3075 ahd_update_neg_table(ahd, devinfo, &tinfo->curr);
3078 if (ahd->msg_type != MSG_TYPE_NONE) {
3079 if ((old_ppr & MSG_EXT_PPR_IU_REQ)
3080 != (ppr_options & MSG_EXT_PPR_IU_REQ)) {
3082 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) {
3083 ahd_print_devinfo(ahd, devinfo);
3084 kprintf("Expecting IU Change busfree\n");
3087 ahd->msg_flags |= MSG_FLAG_EXPECT_PPR_BUSFREE
3088 | MSG_FLAG_IU_REQ_CHANGED;
3090 if ((old_ppr & MSG_EXT_PPR_IU_REQ) != 0) {
3092 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
3093 kprintf("PPR with IU_REQ outstanding\n");
3095 ahd->msg_flags |= MSG_FLAG_EXPECT_PPR_BUSFREE;
3100 update_needed += ahd_update_neg_request(ahd, devinfo, tstate,
3101 tinfo, AHD_NEG_TO_GOAL);
3103 if (update_needed && active)
3104 ahd_update_pending_scbs(ahd);
3108 * Update the user/goal/curr tables of wide negotiation
3109 * parameters as well as, in the case of a current or active update,
3110 * any data structures on the host controller. In the case of an
3111 * active update, the specified target is currently talking to us on
3112 * the bus, so the transfer parameter update must take effect
3116 ahd_set_width(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
3117 u_int width, u_int type, int paused)
3119 struct ahd_initiator_tinfo *tinfo;
3120 struct ahd_tmode_tstate *tstate;
3125 active = (type & AHD_TRANS_ACTIVE) == AHD_TRANS_ACTIVE;
3127 tinfo = ahd_fetch_transinfo(ahd, devinfo->channel, devinfo->our_scsiid,
3128 devinfo->target, &tstate);
3130 if ((type & AHD_TRANS_USER) != 0)
3131 tinfo->user.width = width;
3133 if ((type & AHD_TRANS_GOAL) != 0)
3134 tinfo->goal.width = width;
3136 oldwidth = tinfo->curr.width;
3137 if ((type & AHD_TRANS_CUR) != 0 && oldwidth != width) {
3141 tinfo->curr.width = width;
3142 ahd_send_async(ahd, devinfo->channel, devinfo->target,
3143 CAM_LUN_WILDCARD, AC_TRANSFER_NEG, NULL);
3145 kprintf("%s: target %d using %dbit transfers\n",
3146 ahd_name(ahd), devinfo->target,
3147 8 * (0x01 << width));
3151 if ((type & AHD_TRANS_CUR) != 0) {
3154 ahd_update_neg_table(ahd, devinfo, &tinfo->curr);
3159 update_needed += ahd_update_neg_request(ahd, devinfo, tstate,
3160 tinfo, AHD_NEG_TO_GOAL);
3161 if (update_needed && active)
3162 ahd_update_pending_scbs(ahd);
3167 * Update the current state of tagged queuing for a given target.
3170 ahd_set_tags(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
3173 ahd_platform_set_tags(ahd, devinfo, alg);
3174 ahd_send_async(ahd, devinfo->channel, devinfo->target,
3175 devinfo->lun, AC_TRANSFER_NEG, &alg);
3179 ahd_update_neg_table(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
3180 struct ahd_transinfo *tinfo)
3182 ahd_mode_state saved_modes;
3187 u_int saved_negoaddr;
3188 uint8_t iocell_opts[sizeof(ahd->iocell_opts)];
3190 saved_modes = ahd_save_modes(ahd);
3191 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
3193 saved_negoaddr = ahd_inb(ahd, NEGOADDR);
3194 ahd_outb(ahd, NEGOADDR, devinfo->target);
3195 period = tinfo->period;
3196 offset = tinfo->offset;
3197 memcpy(iocell_opts, ahd->iocell_opts, sizeof(ahd->iocell_opts));
3198 ppr_opts = tinfo->ppr_options & (MSG_EXT_PPR_QAS_REQ|MSG_EXT_PPR_DT_REQ
3199 |MSG_EXT_PPR_IU_REQ|MSG_EXT_PPR_RTI);
3202 period = AHD_SYNCRATE_ASYNC;
3203 if (period == AHD_SYNCRATE_160) {
3205 if ((ahd->bugs & AHD_PACED_NEGTABLE_BUG) != 0) {
3207 * When the SPI4 spec was finalized, PACE transfers
3208 * was not made a configurable option in the PPR
3209 * message. Instead it is assumed to be enabled for
3210 * any syncrate faster than 80MHz. Nevertheless,
3211 * Harpoon2A4 allows this to be configurable.
3213 * Harpoon2A4 also assumes at most 2 data bytes per
3214 * negotiated REQ/ACK offset. Paced transfers take
3215 * 4, so we must adjust our offset.
3217 ppr_opts |= PPROPT_PACE;
3221 * Harpoon2A assumed that there would be a
3222 * fallback rate between 160MHz and 80Mhz,
3223 * so 7 is used as the period factor rather
3224 * than 8 for 160MHz.
3226 period = AHD_SYNCRATE_REVA_160;
3228 if ((tinfo->ppr_options & MSG_EXT_PPR_PCOMP_EN) == 0)
3229 iocell_opts[AHD_PRECOMP_SLEW_INDEX] &=
3233 * Precomp should be disabled for non-paced transfers.
3235 iocell_opts[AHD_PRECOMP_SLEW_INDEX] &= ~AHD_PRECOMP_MASK;
3237 if ((ahd->features & AHD_NEW_IOCELL_OPTS) != 0
3238 && (ppr_opts & MSG_EXT_PPR_DT_REQ) != 0
3239 && (ppr_opts & MSG_EXT_PPR_IU_REQ) == 0) {
3241 * Slow down our CRC interval to be
3242 * compatible with non-packetized
3243 * U160 devices that can't handle a
3244 * CRC at full speed.
3246 con_opts |= ENSLOWCRC;
3249 if ((ahd->bugs & AHD_PACED_NEGTABLE_BUG) != 0) {
3251 * On H2A4, revert to a slower slewrate
3252 * on non-paced transfers.
3254 iocell_opts[AHD_PRECOMP_SLEW_INDEX] &=
3259 ahd_outb(ahd, ANNEXCOL, AHD_ANNEXCOL_PRECOMP_SLEW);
3260 ahd_outb(ahd, ANNEXDAT, iocell_opts[AHD_PRECOMP_SLEW_INDEX]);
3261 ahd_outb(ahd, ANNEXCOL, AHD_ANNEXCOL_AMPLITUDE);
3262 ahd_outb(ahd, ANNEXDAT, iocell_opts[AHD_AMPLITUDE_INDEX]);
3264 ahd_outb(ahd, NEGPERIOD, period);
3265 ahd_outb(ahd, NEGPPROPTS, ppr_opts);
3266 ahd_outb(ahd, NEGOFFSET, offset);
3268 if (tinfo->width == MSG_EXT_WDTR_BUS_16_BIT)
3269 con_opts |= WIDEXFER;
3272 * During packetized transfers, the target will
3273 * give us the oportunity to send command packets
3274 * without us asserting attention.
3276 if ((tinfo->ppr_options & MSG_EXT_PPR_IU_REQ) == 0)
3277 con_opts |= ENAUTOATNO;
3278 ahd_outb(ahd, NEGCONOPTS, con_opts);
3279 ahd_outb(ahd, NEGOADDR, saved_negoaddr);
3280 ahd_restore_modes(ahd, saved_modes);
3284 * When the transfer settings for a connection change, setup for
3285 * negotiation in pending SCBs to effect the change as quickly as
3286 * possible. We also cancel any negotiations that are scheduled
3287 * for inflight SCBs that have not been started yet.
3290 ahd_update_pending_scbs(struct ahd_softc *ahd)
3292 struct scb *pending_scb;
3293 int pending_scb_count;
3297 ahd_mode_state saved_modes;
3300 * Traverse the pending SCB list and ensure that all of the
3301 * SCBs there have the proper settings. We can only safely
3302 * clear the negotiation required flag (setting requires the
3303 * execution queue to be modified) and this is only possible
3304 * if we are not already attempting to select out for this
3305 * SCB. For this reason, all callers only call this routine
3306 * if we are changing the negotiation settings for the currently
3307 * active transaction on the bus.
3309 pending_scb_count = 0;
3310 LIST_FOREACH(pending_scb, &ahd->pending_scbs, pending_links) {
3311 struct ahd_devinfo devinfo;
3312 struct hardware_scb *pending_hscb;
3313 struct ahd_initiator_tinfo *tinfo;
3314 struct ahd_tmode_tstate *tstate;
3316 ahd_scb_devinfo(ahd, &devinfo, pending_scb);
3317 tinfo = ahd_fetch_transinfo(ahd, devinfo.channel,
3319 devinfo.target, &tstate);
3320 pending_hscb = pending_scb->hscb;
3321 if ((tstate->auto_negotiate & devinfo.target_mask) == 0
3322 && (pending_scb->flags & SCB_AUTO_NEGOTIATE) != 0) {
3323 pending_scb->flags &= ~SCB_AUTO_NEGOTIATE;
3324 pending_hscb->control &= ~MK_MESSAGE;
3326 ahd_sync_scb(ahd, pending_scb,
3327 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
3328 pending_scb_count++;
3331 if (pending_scb_count == 0)
3334 if (ahd_is_paused(ahd)) {
3342 * Force the sequencer to reinitialize the selection for
3343 * the command at the head of the execution queue if it
3344 * has already been setup. The negotiation changes may
3345 * effect whether we select-out with ATN.
3347 saved_modes = ahd_save_modes(ahd);
3348 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
3349 ahd_outb(ahd, SCSISEQ0, ahd_inb(ahd, SCSISEQ0) & ~ENSELO);
3350 saved_scbptr = ahd_get_scbptr(ahd);
3351 /* Ensure that the hscbs down on the card match the new information */
3352 for (scb_tag = 0; scb_tag < ahd->scb_data.maxhscbs; scb_tag++) {
3353 struct hardware_scb *pending_hscb;
3356 pending_scb = ahd_lookup_scb(ahd, scb_tag);
3357 if (pending_scb == NULL)
3359 ahd_set_scbptr(ahd, scb_tag);
3360 pending_hscb = pending_scb->hscb;
3361 control = ahd_inb_scbram(ahd, SCB_CONTROL);
3362 control &= ~MK_MESSAGE;
3363 control |= pending_hscb->control & MK_MESSAGE;
3364 ahd_outb(ahd, SCB_CONTROL, control);
3366 ahd_set_scbptr(ahd, saved_scbptr);
3367 ahd_restore_modes(ahd, saved_modes);
3373 /**************************** Pathing Information *****************************/
3375 ahd_fetch_devinfo(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
3377 ahd_mode_state saved_modes;
3382 saved_modes = ahd_save_modes(ahd);
3383 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
3385 if (ahd_inb(ahd, SSTAT0) & TARGET)
3388 role = ROLE_INITIATOR;
3390 if (role == ROLE_TARGET
3391 && (ahd_inb(ahd, SEQ_FLAGS) & CMDPHASE_PENDING) != 0) {
3392 /* We were selected, so pull our id from TARGIDIN */
3393 our_id = ahd_inb(ahd, TARGIDIN) & OID;
3394 } else if (role == ROLE_TARGET)
3395 our_id = ahd_inb(ahd, TOWNID);
3397 our_id = ahd_inb(ahd, IOWNID);
3399 saved_scsiid = ahd_inb(ahd, SAVED_SCSIID);
3400 ahd_compile_devinfo(devinfo,
3402 SCSIID_TARGET(ahd, saved_scsiid),
3403 ahd_inb(ahd, SAVED_LUN),
3404 SCSIID_CHANNEL(ahd, saved_scsiid),
3406 ahd_restore_modes(ahd, saved_modes);
3410 ahd_print_devinfo(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
3412 kprintf("%s:%c:%d:%d: ", ahd_name(ahd), 'A',
3413 devinfo->target, devinfo->lun);
3416 struct ahd_phase_table_entry*
3417 ahd_lookup_phase_entry(int phase)
3419 struct ahd_phase_table_entry *entry;
3420 struct ahd_phase_table_entry *last_entry;
3423 * num_phases doesn't include the default entry which
3424 * will be returned if the phase doesn't match.
3426 last_entry = &ahd_phase_table[num_phases];
3427 for (entry = ahd_phase_table; entry < last_entry; entry++) {
3428 if (phase == entry->phase)
3435 ahd_compile_devinfo(struct ahd_devinfo *devinfo, u_int our_id, u_int target,
3436 u_int lun, char channel, role_t role)
3438 devinfo->our_scsiid = our_id;
3439 devinfo->target = target;
3441 devinfo->target_offset = target;
3442 devinfo->channel = channel;
3443 devinfo->role = role;
3445 devinfo->target_offset += 8;
3446 devinfo->target_mask = (0x01 << devinfo->target_offset);
3450 ahd_scb_devinfo(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
3456 our_id = SCSIID_OUR_ID(scb->hscb->scsiid);
3457 role = ROLE_INITIATOR;
3458 if ((scb->hscb->control & TARGET_SCB) != 0)
3460 ahd_compile_devinfo(devinfo, our_id, SCB_GET_TARGET(ahd, scb),
3461 SCB_GET_LUN(scb), SCB_GET_CHANNEL(ahd, scb), role);
3465 /************************ Message Phase Processing ****************************/
3467 * When an initiator transaction with the MK_MESSAGE flag either reconnects
3468 * or enters the initial message out phase, we are interrupted. Fill our
3469 * outgoing message buffer with the appropriate message and beging handing
3470 * the message phase(s) manually.
3473 ahd_setup_initiator_msgout(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
3477 * To facilitate adding multiple messages together,
3478 * each routine should increment the index and len
3479 * variables instead of setting them explicitly.
3481 ahd->msgout_index = 0;
3482 ahd->msgout_len = 0;
3484 if (ahd_currently_packetized(ahd))
3485 ahd->msg_flags |= MSG_FLAG_PACKETIZED;
3487 if (ahd->send_msg_perror
3488 && ahd_inb(ahd, MSG_OUT) == HOST_MSG) {
3489 ahd->msgout_buf[ahd->msgout_index++] = ahd->send_msg_perror;
3491 ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
3493 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
3494 kprintf("Setting up for Parity Error delivery\n");
3497 } else if (scb == NULL) {
3498 kprintf("%s: WARNING. No pending message for "
3499 "I_T msgin. Issuing NO-OP\n", ahd_name(ahd));
3500 ahd->msgout_buf[ahd->msgout_index++] = MSG_NOOP;
3502 ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
3506 if ((scb->flags & SCB_DEVICE_RESET) == 0
3507 && (scb->flags & SCB_PACKETIZED) == 0
3508 && ahd_inb(ahd, MSG_OUT) == MSG_IDENTIFYFLAG) {
3511 identify_msg = MSG_IDENTIFYFLAG | SCB_GET_LUN(scb);
3512 if ((scb->hscb->control & DISCENB) != 0)
3513 identify_msg |= MSG_IDENTIFY_DISCFLAG;
3514 ahd->msgout_buf[ahd->msgout_index++] = identify_msg;
3517 if ((scb->hscb->control & TAG_ENB) != 0) {
3518 ahd->msgout_buf[ahd->msgout_index++] =
3519 scb->hscb->control & (TAG_ENB|SCB_TAG_TYPE);
3520 ahd->msgout_buf[ahd->msgout_index++] = SCB_GET_TAG(scb);
3521 ahd->msgout_len += 2;
3525 if (scb->flags & SCB_DEVICE_RESET) {
3526 ahd->msgout_buf[ahd->msgout_index++] = MSG_BUS_DEV_RESET;
3528 ahd_print_path(ahd, scb);
3529 kprintf("Bus Device Reset Message Sent\n");
3531 * Clear our selection hardware in advance of
3532 * the busfree. We may have an entry in the waiting
3533 * Q for this target, and we don't want to go about
3534 * selecting while we handle the busfree and blow it
3537 ahd_outb(ahd, SCSISEQ0, 0);
3538 } else if ((scb->flags & SCB_ABORT) != 0) {
3540 if ((scb->hscb->control & TAG_ENB) != 0) {
3541 ahd->msgout_buf[ahd->msgout_index++] = MSG_ABORT_TAG;
3543 ahd->msgout_buf[ahd->msgout_index++] = MSG_ABORT;
3546 ahd_print_path(ahd, scb);
3547 kprintf("Abort%s Message Sent\n",
3548 (scb->hscb->control & TAG_ENB) != 0 ? " Tag" : "");
3550 * Clear our selection hardware in advance of
3551 * the busfree. We may have an entry in the waiting
3552 * Q for this target, and we don't want to go about
3553 * selecting while we handle the busfree and blow it
3556 ahd_outb(ahd, SCSISEQ0, 0);
3557 } else if ((scb->flags & (SCB_AUTO_NEGOTIATE|SCB_NEGOTIATE)) != 0) {
3558 ahd_build_transfer_msg(ahd, devinfo);
3560 * Clear our selection hardware in advance of potential
3561 * PPR IU status change busfree. We may have an entry in
3562 * the waiting Q for this target, and we don't want to go
3563 * about selecting while we handle the busfree and blow
3566 ahd_outb(ahd, SCSISEQ0, 0);
3568 kprintf("ahd_intr: AWAITING_MSG for an SCB that "
3569 "does not have a waiting message\n");
3570 kprintf("SCSIID = %x, target_mask = %x\n", scb->hscb->scsiid,
3571 devinfo->target_mask);
3572 panic("SCB = %d, SCB Control = %x:%x, MSG_OUT = %x "
3573 "SCB flags = %x", SCB_GET_TAG(scb), scb->hscb->control,
3574 ahd_inb_scbram(ahd, SCB_CONTROL), ahd_inb(ahd, MSG_OUT),
3579 * Clear the MK_MESSAGE flag from the SCB so we aren't
3580 * asked to send this message again.
3582 ahd_outb(ahd, SCB_CONTROL,
3583 ahd_inb_scbram(ahd, SCB_CONTROL) & ~MK_MESSAGE);
3584 scb->hscb->control &= ~MK_MESSAGE;
3585 ahd->msgout_index = 0;
3586 ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
3590 * Build an appropriate transfer negotiation message for the
3591 * currently active target.
3594 ahd_build_transfer_msg(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
3597 * We need to initiate transfer negotiations.
3598 * If our current and goal settings are identical,
3599 * we want to renegotiate due to a check condition.
3601 struct ahd_initiator_tinfo *tinfo;
3602 struct ahd_tmode_tstate *tstate;
3610 tinfo = ahd_fetch_transinfo(ahd, devinfo->channel, devinfo->our_scsiid,
3611 devinfo->target, &tstate);
3613 * Filter our period based on the current connection.
3614 * If we can't perform DT transfers on this segment (not in LVD
3615 * mode for instance), then our decision to issue a PPR message
3618 period = tinfo->goal.period;
3619 offset = tinfo->goal.offset;
3620 ppr_options = tinfo->goal.ppr_options;
3621 /* Target initiated PPR is not allowed in the SCSI spec */
3622 if (devinfo->role == ROLE_TARGET)
3624 ahd_devlimited_syncrate(ahd, tinfo, &period,
3625 &ppr_options, devinfo->role);
3626 dowide = tinfo->curr.width != tinfo->goal.width;
3627 dosync = tinfo->curr.offset != offset || tinfo->curr.period != period;
3629 * Only use PPR if we have options that need it, even if the device
3630 * claims to support it. There might be an expander in the way
3633 doppr = ppr_options != 0;
3635 if (!dowide && !dosync && !doppr) {
3636 dowide = tinfo->goal.width != MSG_EXT_WDTR_BUS_8_BIT;
3637 dosync = tinfo->goal.offset != 0;
3640 if (!dowide && !dosync && !doppr) {
3642 * Force async with a WDTR message if we have a wide bus,
3643 * or just issue an SDTR with a 0 offset.
3645 if ((ahd->features & AHD_WIDE) != 0)
3651 ahd_print_devinfo(ahd, devinfo);
3652 kprintf("Ensuring async\n");
3655 /* Target initiated PPR is not allowed in the SCSI spec */
3656 if (devinfo->role == ROLE_TARGET)
3660 * Both the PPR message and SDTR message require the
3661 * goal syncrate to be limited to what the target device
3662 * is capable of handling (based on whether an LVD->SE
3663 * expander is on the bus), so combine these two cases.
3664 * Regardless, guarantee that if we are using WDTR and SDTR
3665 * messages that WDTR comes first.
3667 if (doppr || (dosync && !dowide)) {
3669 offset = tinfo->goal.offset;
3670 ahd_validate_offset(ahd, tinfo, period, &offset,
3671 doppr ? tinfo->goal.width
3672 : tinfo->curr.width,
3675 ahd_construct_ppr(ahd, devinfo, period, offset,
3676 tinfo->goal.width, ppr_options);
3678 ahd_construct_sdtr(ahd, devinfo, period, offset);
3681 ahd_construct_wdtr(ahd, devinfo, tinfo->goal.width);
3686 * Build a synchronous negotiation message in our message
3687 * buffer based on the input parameters.
3690 ahd_construct_sdtr(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
3691 u_int period, u_int offset)
3694 period = AHD_ASYNC_XFER_PERIOD;
3695 ahd->msgout_buf[ahd->msgout_index++] = MSG_EXTENDED;
3696 ahd->msgout_buf[ahd->msgout_index++] = MSG_EXT_SDTR_LEN;
3697 ahd->msgout_buf[ahd->msgout_index++] = MSG_EXT_SDTR;
3698 ahd->msgout_buf[ahd->msgout_index++] = period;
3699 ahd->msgout_buf[ahd->msgout_index++] = offset;
3700 ahd->msgout_len += 5;
3702 kprintf("(%s:%c:%d:%d): Sending SDTR period %x, offset %x\n",
3703 ahd_name(ahd), devinfo->channel, devinfo->target,
3704 devinfo->lun, period, offset);
3709 * Build a wide negotiateion message in our message
3710 * buffer based on the input parameters.
3713 ahd_construct_wdtr(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
3716 ahd->msgout_buf[ahd->msgout_index++] = MSG_EXTENDED;
3717 ahd->msgout_buf[ahd->msgout_index++] = MSG_EXT_WDTR_LEN;
3718 ahd->msgout_buf[ahd->msgout_index++] = MSG_EXT_WDTR;
3719 ahd->msgout_buf[ahd->msgout_index++] = bus_width;
3720 ahd->msgout_len += 4;
3722 kprintf("(%s:%c:%d:%d): Sending WDTR %x\n",
3723 ahd_name(ahd), devinfo->channel, devinfo->target,
3724 devinfo->lun, bus_width);
3729 * Build a parallel protocol request message in our message
3730 * buffer based on the input parameters.
3733 ahd_construct_ppr(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
3734 u_int period, u_int offset, u_int bus_width,
3738 * Always request precompensation from
3739 * the other target if we are running
3740 * at paced syncrates.
3742 if (period <= AHD_SYNCRATE_PACED)
3743 ppr_options |= MSG_EXT_PPR_PCOMP_EN;
3745 period = AHD_ASYNC_XFER_PERIOD;
3746 ahd->msgout_buf[ahd->msgout_index++] = MSG_EXTENDED;
3747 ahd->msgout_buf[ahd->msgout_index++] = MSG_EXT_PPR_LEN;
3748 ahd->msgout_buf[ahd->msgout_index++] = MSG_EXT_PPR;
3749 ahd->msgout_buf[ahd->msgout_index++] = period;
3750 ahd->msgout_buf[ahd->msgout_index++] = 0;
3751 ahd->msgout_buf[ahd->msgout_index++] = offset;
3752 ahd->msgout_buf[ahd->msgout_index++] = bus_width;
3753 ahd->msgout_buf[ahd->msgout_index++] = ppr_options;
3754 ahd->msgout_len += 8;
3756 kprintf("(%s:%c:%d:%d): Sending PPR bus_width %x, period %x, "
3757 "offset %x, ppr_options %x\n", ahd_name(ahd),
3758 devinfo->channel, devinfo->target, devinfo->lun,
3759 bus_width, period, offset, ppr_options);
3764 * Clear any active message state.
3767 ahd_clear_msg_state(struct ahd_softc *ahd)
3769 ahd_mode_state saved_modes;
3771 saved_modes = ahd_save_modes(ahd);
3772 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
3773 ahd->send_msg_perror = 0;
3774 ahd->msg_flags = MSG_FLAG_NONE;
3775 ahd->msgout_len = 0;
3776 ahd->msgin_index = 0;
3777 ahd->msg_type = MSG_TYPE_NONE;
3778 if ((ahd_inb(ahd, SCSISIGO) & ATNO) != 0) {
3780 * The target didn't care to respond to our
3781 * message request, so clear ATN.
3783 ahd_outb(ahd, CLRSINT1, CLRATNO);
3785 ahd_outb(ahd, MSG_OUT, MSG_NOOP);
3786 ahd_outb(ahd, SEQ_FLAGS2,
3787 ahd_inb(ahd, SEQ_FLAGS2) & ~TARGET_MSG_PENDING);
3788 ahd_restore_modes(ahd, saved_modes);
3792 * Manual message loop handler.
3795 ahd_handle_message_phase(struct ahd_softc *ahd)
3797 struct ahd_devinfo devinfo;
3801 ahd_fetch_devinfo(ahd, &devinfo);
3802 end_session = FALSE;
3803 bus_phase = ahd_inb(ahd, LASTPHASE);
3805 if ((ahd_inb(ahd, LQISTAT2) & LQIPHASE_OUTPKT) != 0) {
3806 kprintf("LQIRETRY for LQIPHASE_OUTPKT\n");
3807 ahd_outb(ahd, LQCTL2, LQIRETRY);
3810 switch (ahd->msg_type) {
3811 case MSG_TYPE_INITIATOR_MSGOUT:
3817 if (ahd->msgout_len == 0 && ahd->send_msg_perror == 0)
3818 panic("HOST_MSG_LOOP interrupt with no active message");
3821 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) {
3822 ahd_print_devinfo(ahd, &devinfo);
3823 kprintf("INITIATOR_MSG_OUT");
3826 phasemis = bus_phase != P_MESGOUT;
3829 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) {
3830 kprintf(" PHASEMIS %s\n",
3831 ahd_lookup_phase_entry(bus_phase)
3835 if (bus_phase == P_MESGIN) {
3837 * Change gears and see if
3838 * this messages is of interest to
3839 * us or should be passed back to
3842 ahd_outb(ahd, CLRSINT1, CLRATNO);
3843 ahd->send_msg_perror = 0;
3844 ahd->msg_type = MSG_TYPE_INITIATOR_MSGIN;
3845 ahd->msgin_index = 0;
3852 if (ahd->send_msg_perror) {
3853 ahd_outb(ahd, CLRSINT1, CLRATNO);
3854 ahd_outb(ahd, CLRSINT1, CLRREQINIT);
3856 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
3857 kprintf(" byte 0x%x\n", ahd->send_msg_perror);
3860 * If we are notifying the target of a CRC error
3861 * during packetized operations, the target is
3862 * within its rights to acknowledge our message
3865 if ((ahd->msg_flags & MSG_FLAG_PACKETIZED) != 0
3866 && ahd->send_msg_perror == MSG_INITIATOR_DET_ERR)
3867 ahd->msg_flags |= MSG_FLAG_EXPECT_IDE_BUSFREE;
3869 ahd_outb(ahd, RETURN_2, ahd->send_msg_perror);
3870 ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_WRITE);
3874 msgdone = ahd->msgout_index == ahd->msgout_len;
3877 * The target has requested a retry.
3878 * Re-assert ATN, reset our message index to
3881 ahd->msgout_index = 0;
3882 ahd_assert_atn(ahd);
3885 lastbyte = ahd->msgout_index == (ahd->msgout_len - 1);
3887 /* Last byte is signified by dropping ATN */
3888 ahd_outb(ahd, CLRSINT1, CLRATNO);
3892 * Clear our interrupt status and present
3893 * the next byte on the bus.
3895 ahd_outb(ahd, CLRSINT1, CLRREQINIT);
3897 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
3898 kprintf(" byte 0x%x\n",
3899 ahd->msgout_buf[ahd->msgout_index]);
3901 ahd_outb(ahd, RETURN_2, ahd->msgout_buf[ahd->msgout_index++]);
3902 ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_WRITE);
3905 case MSG_TYPE_INITIATOR_MSGIN:
3911 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) {
3912 ahd_print_devinfo(ahd, &devinfo);
3913 kprintf("INITIATOR_MSG_IN");
3916 phasemis = bus_phase != P_MESGIN;
3919 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) {
3920 kprintf(" PHASEMIS %s\n",
3921 ahd_lookup_phase_entry(bus_phase)
3925 ahd->msgin_index = 0;
3926 if (bus_phase == P_MESGOUT
3927 && (ahd->send_msg_perror != 0
3928 || (ahd->msgout_len != 0
3929 && ahd->msgout_index == 0))) {
3930 ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
3937 /* Pull the byte in without acking it */
3938 ahd->msgin_buf[ahd->msgin_index] = ahd_inb(ahd, SCSIBUS);
3940 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
3941 kprintf(" byte 0x%x\n",
3942 ahd->msgin_buf[ahd->msgin_index]);
3945 message_done = ahd_parse_msg(ahd, &devinfo);
3949 * Clear our incoming message buffer in case there
3950 * is another message following this one.
3952 ahd->msgin_index = 0;
3955 * If this message illicited a response,
3956 * assert ATN so the target takes us to the
3957 * message out phase.
3959 if (ahd->msgout_len != 0) {
3961 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) {
3962 ahd_print_devinfo(ahd, &devinfo);
3963 kprintf("Asserting ATN for response\n");
3966 ahd_assert_atn(ahd);
3971 if (message_done == MSGLOOP_TERMINATED) {
3975 ahd_outb(ahd, CLRSINT1, CLRREQINIT);
3976 ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_READ);
3980 case MSG_TYPE_TARGET_MSGIN:
3986 * By default, the message loop will continue.
3988 ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_TARG);
3990 if (ahd->msgout_len == 0)
3991 panic("Target MSGIN with no active message");
3994 * If we interrupted a mesgout session, the initiator
3995 * will not know this until our first REQ. So, we
3996 * only honor mesgout requests after we've sent our
3999 if ((ahd_inb(ahd, SCSISIGI) & ATNI) != 0
4000 && ahd->msgout_index > 0)
4001 msgout_request = TRUE;
4003 msgout_request = FALSE;
4005 if (msgout_request) {
4008 * Change gears and see if
4009 * this messages is of interest to
4010 * us or should be passed back to
4013 ahd->msg_type = MSG_TYPE_TARGET_MSGOUT;
4014 ahd_outb(ahd, SCSISIGO, P_MESGOUT | BSYO);
4015 ahd->msgin_index = 0;
4016 /* Dummy read to REQ for first byte */
4017 ahd_inb(ahd, SCSIDAT);
4018 ahd_outb(ahd, SXFRCTL0,
4019 ahd_inb(ahd, SXFRCTL0) | SPIOEN);
4023 msgdone = ahd->msgout_index == ahd->msgout_len;
4025 ahd_outb(ahd, SXFRCTL0,
4026 ahd_inb(ahd, SXFRCTL0) & ~SPIOEN);
4032 * Present the next byte on the bus.
4034 ahd_outb(ahd, SXFRCTL0, ahd_inb(ahd, SXFRCTL0) | SPIOEN);
4035 ahd_outb(ahd, SCSIDAT, ahd->msgout_buf[ahd->msgout_index++]);
4038 case MSG_TYPE_TARGET_MSGOUT:
4044 * By default, the message loop will continue.
4046 ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_TARG);
4049 * The initiator signals that this is
4050 * the last byte by dropping ATN.
4052 lastbyte = (ahd_inb(ahd, SCSISIGI) & ATNI) == 0;
4055 * Read the latched byte, but turn off SPIOEN first
4056 * so that we don't inadvertently cause a REQ for the
4059 ahd_outb(ahd, SXFRCTL0, ahd_inb(ahd, SXFRCTL0) & ~SPIOEN);
4060 ahd->msgin_buf[ahd->msgin_index] = ahd_inb(ahd, SCSIDAT);
4061 msgdone = ahd_parse_msg(ahd, &devinfo);
4062 if (msgdone == MSGLOOP_TERMINATED) {
4064 * The message is *really* done in that it caused
4065 * us to go to bus free. The sequencer has already
4066 * been reset at this point, so pull the ejection
4075 * XXX Read spec about initiator dropping ATN too soon
4076 * and use msgdone to detect it.
4078 if (msgdone == MSGLOOP_MSGCOMPLETE) {
4079 ahd->msgin_index = 0;
4082 * If this message illicited a response, transition
4083 * to the Message in phase and send it.
4085 if (ahd->msgout_len != 0) {
4086 ahd_outb(ahd, SCSISIGO, P_MESGIN | BSYO);
4087 ahd_outb(ahd, SXFRCTL0,
4088 ahd_inb(ahd, SXFRCTL0) | SPIOEN);
4089 ahd->msg_type = MSG_TYPE_TARGET_MSGIN;
4090 ahd->msgin_index = 0;
4098 /* Ask for the next byte. */
4099 ahd_outb(ahd, SXFRCTL0,
4100 ahd_inb(ahd, SXFRCTL0) | SPIOEN);
4106 panic("Unknown REQINIT message type");
4110 if ((ahd->msg_flags & MSG_FLAG_PACKETIZED) != 0) {
4111 kprintf("%s: Returning to Idle Loop\n",
4113 ahd_clear_msg_state(ahd);
4116 * Perform the equivalent of a clear_target_state.
4118 ahd_outb(ahd, LASTPHASE, P_BUSFREE);
4119 ahd_outb(ahd, SEQ_FLAGS, NOT_IDENTIFIED|NO_CDB_SENT);
4120 ahd_outb(ahd, SEQCTL0, FASTMODE|SEQRESET);
4122 ahd_clear_msg_state(ahd);
4123 ahd_outb(ahd, RETURN_1, EXIT_MSG_LOOP);
4129 * See if we sent a particular extended message to the target.
4130 * If "full" is true, return true only if the target saw the full
4131 * message. If "full" is false, return true if the target saw at
4132 * least the first byte of the message.
4135 ahd_sent_msg(struct ahd_softc *ahd, ahd_msgtype type, u_int msgval, int full)
4143 while (index < ahd->msgout_len) {
4144 if (ahd->msgout_buf[index] == MSG_EXTENDED) {
4147 end_index = index + 1 + ahd->msgout_buf[index + 1];
4148 if (ahd->msgout_buf[index+2] == msgval
4149 && type == AHDMSG_EXT) {
4152 if (ahd->msgout_index > end_index)
4154 } else if (ahd->msgout_index > index)
4158 } else if (ahd->msgout_buf[index] >= MSG_SIMPLE_TASK
4159 && ahd->msgout_buf[index] <= MSG_IGN_WIDE_RESIDUE) {
4161 /* Skip tag type and tag id or residue param*/
4164 /* Single byte message */
4165 if (type == AHDMSG_1B
4166 && ahd->msgout_index > index
4167 && (ahd->msgout_buf[index] == msgval
4168 || ((ahd->msgout_buf[index] & MSG_IDENTIFYFLAG) != 0
4169 && msgval == MSG_IDENTIFYFLAG)))
4181 * Wait for a complete incoming message, parse it, and respond accordingly.
4184 ahd_parse_msg(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
4186 struct ahd_initiator_tinfo *tinfo;
4187 struct ahd_tmode_tstate *tstate;
4192 done = MSGLOOP_IN_PROG;
4195 tinfo = ahd_fetch_transinfo(ahd, devinfo->channel, devinfo->our_scsiid,
4196 devinfo->target, &tstate);
4199 * Parse as much of the message as is available,
4200 * rejecting it if we don't support it. When
4201 * the entire message is available and has been
4202 * handled, return MSGLOOP_MSGCOMPLETE, indicating
4203 * that we have parsed an entire message.
4205 * In the case of extended messages, we accept the length
4206 * byte outright and perform more checking once we know the
4207 * extended message type.
4209 switch (ahd->msgin_buf[0]) {
4210 case MSG_DISCONNECT:
4211 case MSG_SAVEDATAPOINTER:
4212 case MSG_CMDCOMPLETE:
4213 case MSG_RESTOREPOINTERS:
4214 case MSG_IGN_WIDE_RESIDUE:
4216 * End our message loop as these are messages
4217 * the sequencer handles on its own.
4219 done = MSGLOOP_TERMINATED;
4221 case MSG_MESSAGE_REJECT:
4222 response = ahd_handle_msg_reject(ahd, devinfo);
4225 done = MSGLOOP_MSGCOMPLETE;
4229 /* Wait for enough of the message to begin validation */
4230 if (ahd->msgin_index < 2)
4232 switch (ahd->msgin_buf[2]) {
4240 if (ahd->msgin_buf[1] != MSG_EXT_SDTR_LEN) {
4246 * Wait until we have both args before validating
4247 * and acting on this message.
4249 * Add one to MSG_EXT_SDTR_LEN to account for
4250 * the extended message preamble.
4252 if (ahd->msgin_index < (MSG_EXT_SDTR_LEN + 1))
4255 period = ahd->msgin_buf[3];
4257 saved_offset = offset = ahd->msgin_buf[4];
4258 ahd_devlimited_syncrate(ahd, tinfo, &period,
4259 &ppr_options, devinfo->role);
4260 ahd_validate_offset(ahd, tinfo, period, &offset,
4261 tinfo->curr.width, devinfo->role);
4263 kprintf("(%s:%c:%d:%d): Received "
4264 "SDTR period %x, offset %x\n\t"
4265 "Filtered to period %x, offset %x\n",
4266 ahd_name(ahd), devinfo->channel,
4267 devinfo->target, devinfo->lun,
4268 ahd->msgin_buf[3], saved_offset,
4271 ahd_set_syncrate(ahd, devinfo, period,
4272 offset, ppr_options,
4273 AHD_TRANS_ACTIVE|AHD_TRANS_GOAL,
4277 * See if we initiated Sync Negotiation
4278 * and didn't have to fall down to async
4281 if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_SDTR, TRUE)) {
4283 if (saved_offset != offset) {
4284 /* Went too low - force async */
4289 * Send our own SDTR in reply
4292 && devinfo->role == ROLE_INITIATOR) {
4293 kprintf("(%s:%c:%d:%d): Target "
4295 ahd_name(ahd), devinfo->channel,
4296 devinfo->target, devinfo->lun);
4298 ahd->msgout_index = 0;
4299 ahd->msgout_len = 0;
4300 ahd_construct_sdtr(ahd, devinfo,
4302 ahd->msgout_index = 0;
4305 done = MSGLOOP_MSGCOMPLETE;
4312 u_int sending_reply;
4314 sending_reply = FALSE;
4315 if (ahd->msgin_buf[1] != MSG_EXT_WDTR_LEN) {
4321 * Wait until we have our arg before validating
4322 * and acting on this message.
4324 * Add one to MSG_EXT_WDTR_LEN to account for
4325 * the extended message preamble.
4327 if (ahd->msgin_index < (MSG_EXT_WDTR_LEN + 1))
4330 bus_width = ahd->msgin_buf[3];
4331 saved_width = bus_width;
4332 ahd_validate_width(ahd, tinfo, &bus_width,
4335 kprintf("(%s:%c:%d:%d): Received WDTR "
4336 "%x filtered to %x\n",
4337 ahd_name(ahd), devinfo->channel,
4338 devinfo->target, devinfo->lun,
4339 saved_width, bus_width);
4342 if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_WDTR, TRUE)) {
4344 * Don't send a WDTR back to the
4345 * target, since we asked first.
4346 * If the width went higher than our
4347 * request, reject it.
4349 if (saved_width > bus_width) {
4351 kprintf("(%s:%c:%d:%d): requested %dBit "
4352 "transfers. Rejecting...\n",
4353 ahd_name(ahd), devinfo->channel,
4354 devinfo->target, devinfo->lun,
4355 8 * (0x01 << bus_width));
4360 * Send our own WDTR in reply
4363 && devinfo->role == ROLE_INITIATOR) {
4364 kprintf("(%s:%c:%d:%d): Target "
4366 ahd_name(ahd), devinfo->channel,
4367 devinfo->target, devinfo->lun);
4369 ahd->msgout_index = 0;
4370 ahd->msgout_len = 0;
4371 ahd_construct_wdtr(ahd, devinfo, bus_width);
4372 ahd->msgout_index = 0;
4374 sending_reply = TRUE;
4377 * After a wide message, we are async, but
4378 * some devices don't seem to honor this portion
4379 * of the spec. Force a renegotiation of the
4380 * sync component of our transfer agreement even
4381 * if our goal is async. By updating our width
4382 * after forcing the negotiation, we avoid
4383 * renegotiating for width.
4385 ahd_update_neg_request(ahd, devinfo, tstate,
4386 tinfo, AHD_NEG_ALWAYS);
4387 ahd_set_width(ahd, devinfo, bus_width,
4388 AHD_TRANS_ACTIVE|AHD_TRANS_GOAL,
4390 if (sending_reply == FALSE && reject == FALSE) {
4393 * We will always have an SDTR to send.
4395 ahd->msgout_index = 0;
4396 ahd->msgout_len = 0;
4397 ahd_build_transfer_msg(ahd, devinfo);
4398 ahd->msgout_index = 0;
4401 done = MSGLOOP_MSGCOMPLETE;
4412 u_int saved_ppr_options;
4414 if (ahd->msgin_buf[1] != MSG_EXT_PPR_LEN) {
4420 * Wait until we have all args before validating
4421 * and acting on this message.
4423 * Add one to MSG_EXT_PPR_LEN to account for
4424 * the extended message preamble.
4426 if (ahd->msgin_index < (MSG_EXT_PPR_LEN + 1))
4429 period = ahd->msgin_buf[3];
4430 offset = ahd->msgin_buf[5];
4431 bus_width = ahd->msgin_buf[6];
4432 saved_width = bus_width;
4433 ppr_options = ahd->msgin_buf[7];
4435 * According to the spec, a DT only
4436 * period factor with no DT option
4437 * set implies async.
4439 if ((ppr_options & MSG_EXT_PPR_DT_REQ) == 0
4442 saved_ppr_options = ppr_options;
4443 saved_offset = offset;
4446 * Transfer options are only available if we
4447 * are negotiating wide.
4450 ppr_options &= MSG_EXT_PPR_QAS_REQ;
4452 ahd_validate_width(ahd, tinfo, &bus_width,
4454 ahd_devlimited_syncrate(ahd, tinfo, &period,
4455 &ppr_options, devinfo->role);
4456 ahd_validate_offset(ahd, tinfo, period, &offset,
4457 bus_width, devinfo->role);
4459 if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_PPR, TRUE)) {
4461 * If we are unable to do any of the
4462 * requested options (we went too low),
4463 * then we'll have to reject the message.
4465 if (saved_width > bus_width
4466 || saved_offset != offset
4467 || saved_ppr_options != ppr_options) {
4475 if (devinfo->role != ROLE_TARGET)
4476 kprintf("(%s:%c:%d:%d): Target "
4478 ahd_name(ahd), devinfo->channel,
4479 devinfo->target, devinfo->lun);
4481 kprintf("(%s:%c:%d:%d): Initiator "
4483 ahd_name(ahd), devinfo->channel,
4484 devinfo->target, devinfo->lun);
4485 ahd->msgout_index = 0;
4486 ahd->msgout_len = 0;
4487 ahd_construct_ppr(ahd, devinfo, period, offset,
4488 bus_width, ppr_options);
4489 ahd->msgout_index = 0;
4493 kprintf("(%s:%c:%d:%d): Received PPR width %x, "
4494 "period %x, offset %x,options %x\n"
4495 "\tFiltered to width %x, period %x, "
4496 "offset %x, options %x\n",
4497 ahd_name(ahd), devinfo->channel,
4498 devinfo->target, devinfo->lun,
4499 saved_width, ahd->msgin_buf[3],
4500 saved_offset, saved_ppr_options,
4501 bus_width, period, offset, ppr_options);
4503 ahd_set_width(ahd, devinfo, bus_width,
4504 AHD_TRANS_ACTIVE|AHD_TRANS_GOAL,
4506 ahd_set_syncrate(ahd, devinfo, period,
4507 offset, ppr_options,
4508 AHD_TRANS_ACTIVE|AHD_TRANS_GOAL,
4511 done = MSGLOOP_MSGCOMPLETE;
4515 /* Unknown extended message. Reject it. */
4521 #ifdef AHD_TARGET_MODE
4522 case MSG_BUS_DEV_RESET:
4523 ahd_handle_devreset(ahd, devinfo, CAM_LUN_WILDCARD,
4525 "Bus Device Reset Received",
4526 /*verbose_level*/0);
4528 done = MSGLOOP_TERMINATED;
4532 case MSG_CLEAR_QUEUE:
4536 /* Target mode messages */
4537 if (devinfo->role != ROLE_TARGET) {
4541 tag = SCB_LIST_NULL;
4542 if (ahd->msgin_buf[0] == MSG_ABORT_TAG)
4543 tag = ahd_inb(ahd, INITIATOR_TAG);
4544 ahd_abort_scbs(ahd, devinfo->target, devinfo->channel,
4545 devinfo->lun, tag, ROLE_TARGET,
4548 tstate = ahd->enabled_targets[devinfo->our_scsiid];
4549 if (tstate != NULL) {
4550 struct ahd_tmode_lstate* lstate;
4552 lstate = tstate->enabled_luns[devinfo->lun];
4553 if (lstate != NULL) {
4554 ahd_queue_lstate_event(ahd, lstate,
4555 devinfo->our_scsiid,
4558 ahd_send_lstate_events(ahd, lstate);
4562 done = MSGLOOP_TERMINATED;
4566 case MSG_QAS_REQUEST:
4568 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
4569 kprintf("%s: QAS request. SCSISIGI == 0x%x\n",
4570 ahd_name(ahd), ahd_inb(ahd, SCSISIGI));
4572 ahd->msg_flags |= MSG_FLAG_EXPECT_QASREJ_BUSFREE;
4574 case MSG_TERM_IO_PROC:
4582 * Setup to reject the message.
4584 ahd->msgout_index = 0;
4585 ahd->msgout_len = 1;
4586 ahd->msgout_buf[0] = MSG_MESSAGE_REJECT;
4587 done = MSGLOOP_MSGCOMPLETE;
4591 if (done != MSGLOOP_IN_PROG && !response)
4592 /* Clear the outgoing message buffer */
4593 ahd->msgout_len = 0;
4599 * Process a message reject message.
4602 ahd_handle_msg_reject(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
4605 * What we care about here is if we had an
4606 * outstanding SDTR or WDTR message for this
4607 * target. If we did, this is a signal that
4608 * the target is refusing negotiation.
4611 struct ahd_initiator_tinfo *tinfo;
4612 struct ahd_tmode_tstate *tstate;
4617 scb_index = ahd_get_scbptr(ahd);
4618 scb = ahd_lookup_scb(ahd, scb_index);
4619 tinfo = ahd_fetch_transinfo(ahd, devinfo->channel,
4620 devinfo->our_scsiid,
4621 devinfo->target, &tstate);
4622 /* Might be necessary */
4623 last_msg = ahd_inb(ahd, LAST_MSG);
4625 if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_PPR, /*full*/FALSE)) {
4626 if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_PPR, /*full*/TRUE)
4627 && tinfo->goal.period <= AHD_SYNCRATE_PACED) {
4629 * Target may not like our SPI-4 PPR Options.
4630 * Attempt to negotiate 80MHz which will turn
4631 * off these options.
4634 kprintf("(%s:%c:%d:%d): PPR Rejected. "
4635 "Trying simple U160 PPR\n",
4636 ahd_name(ahd), devinfo->channel,
4637 devinfo->target, devinfo->lun);
4639 tinfo->goal.period = AHD_SYNCRATE_DT;
4640 tinfo->goal.ppr_options &= MSG_EXT_PPR_IU_REQ
4641 | MSG_EXT_PPR_QAS_REQ
4642 | MSG_EXT_PPR_DT_REQ;
4645 * Target does not support the PPR message.
4646 * Attempt to negotiate SPI-2 style.
4649 kprintf("(%s:%c:%d:%d): PPR Rejected. "
4650 "Trying WDTR/SDTR\n",
4651 ahd_name(ahd), devinfo->channel,
4652 devinfo->target, devinfo->lun);
4654 tinfo->goal.ppr_options = 0;
4655 tinfo->curr.transport_version = 2;
4656 tinfo->goal.transport_version = 2;
4658 ahd->msgout_index = 0;
4659 ahd->msgout_len = 0;
4660 ahd_build_transfer_msg(ahd, devinfo);
4661 ahd->msgout_index = 0;
4663 } else if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_WDTR, /*full*/FALSE)) {
4665 /* note 8bit xfers */
4666 kprintf("(%s:%c:%d:%d): refuses WIDE negotiation. Using "
4667 "8bit transfers\n", ahd_name(ahd),
4668 devinfo->channel, devinfo->target, devinfo->lun);
4669 ahd_set_width(ahd, devinfo, MSG_EXT_WDTR_BUS_8_BIT,
4670 AHD_TRANS_ACTIVE|AHD_TRANS_GOAL,
4673 * No need to clear the sync rate. If the target
4674 * did not accept the command, our syncrate is
4675 * unaffected. If the target started the negotiation,
4676 * but rejected our response, we already cleared the
4677 * sync rate before sending our WDTR.
4679 if (tinfo->goal.offset != tinfo->curr.offset) {
4681 /* Start the sync negotiation */
4682 ahd->msgout_index = 0;
4683 ahd->msgout_len = 0;
4684 ahd_build_transfer_msg(ahd, devinfo);
4685 ahd->msgout_index = 0;
4688 } else if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_SDTR, /*full*/FALSE)) {
4689 /* note asynch xfers and clear flag */
4690 ahd_set_syncrate(ahd, devinfo, /*period*/0,
4691 /*offset*/0, /*ppr_options*/0,
4692 AHD_TRANS_ACTIVE|AHD_TRANS_GOAL,
4694 kprintf("(%s:%c:%d:%d): refuses synchronous negotiation. "
4695 "Using asynchronous transfers\n",
4696 ahd_name(ahd), devinfo->channel,
4697 devinfo->target, devinfo->lun);
4698 } else if ((scb->hscb->control & MSG_SIMPLE_TASK) != 0) {
4702 tag_type = (scb->hscb->control & MSG_SIMPLE_TASK);
4704 if (tag_type == MSG_SIMPLE_TASK) {
4705 kprintf("(%s:%c:%d:%d): refuses tagged commands. "
4706 "Performing non-tagged I/O\n", ahd_name(ahd),
4707 devinfo->channel, devinfo->target, devinfo->lun);
4708 ahd_set_tags(ahd, devinfo, AHD_QUEUE_NONE);
4711 kprintf("(%s:%c:%d:%d): refuses %s tagged commands. "
4712 "Performing simple queue tagged I/O only\n",
4713 ahd_name(ahd), devinfo->channel, devinfo->target,
4714 devinfo->lun, tag_type == MSG_ORDERED_TASK
4715 ? "ordered" : "head of queue");
4716 ahd_set_tags(ahd, devinfo, AHD_QUEUE_BASIC);
4721 * Resend the identify for this CCB as the target
4722 * may believe that the selection is invalid otherwise.
4724 ahd_outb(ahd, SCB_CONTROL,
4725 ahd_inb_scbram(ahd, SCB_CONTROL) & mask);
4726 scb->hscb->control &= mask;
4727 aic_set_transaction_tag(scb, /*enabled*/FALSE,
4728 /*type*/MSG_SIMPLE_TASK);
4729 ahd_outb(ahd, MSG_OUT, MSG_IDENTIFYFLAG);
4730 ahd_assert_atn(ahd);
4731 ahd_busy_tcl(ahd, BUILD_TCL(scb->hscb->scsiid, devinfo->lun),
4735 * Requeue all tagged commands for this target
4736 * currently in our posession so they can be
4737 * converted to untagged commands.
4739 ahd_search_qinfifo(ahd, SCB_GET_TARGET(ahd, scb),
4740 SCB_GET_CHANNEL(ahd, scb),
4741 SCB_GET_LUN(scb), /*tag*/SCB_LIST_NULL,
4742 ROLE_INITIATOR, CAM_REQUEUE_REQ,
4744 } else if (ahd_sent_msg(ahd, AHDMSG_1B, MSG_IDENTIFYFLAG, TRUE)) {
4746 * Most likely the device believes that we had
4747 * previously negotiated packetized.
4749 ahd->msg_flags |= MSG_FLAG_EXPECT_PPR_BUSFREE
4750 | MSG_FLAG_IU_REQ_CHANGED;
4752 ahd_force_renegotiation(ahd, devinfo);
4753 ahd->msgout_index = 0;
4754 ahd->msgout_len = 0;
4755 ahd_build_transfer_msg(ahd, devinfo);
4756 ahd->msgout_index = 0;
4760 * Otherwise, we ignore it.
4762 kprintf("%s:%c:%d: Message reject for %x -- ignored\n",
4763 ahd_name(ahd), devinfo->channel, devinfo->target,
4770 * Process an ingnore wide residue message.
4773 ahd_handle_ign_wide_residue(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
4778 scb_index = ahd_get_scbptr(ahd);
4779 scb = ahd_lookup_scb(ahd, scb_index);
4781 * XXX Actually check data direction in the sequencer?
4782 * Perhaps add datadir to some spare bits in the hscb?
4784 if ((ahd_inb(ahd, SEQ_FLAGS) & DPHASE) == 0
4785 || aic_get_transfer_dir(scb) != CAM_DIR_IN) {
4787 * Ignore the message if we haven't
4788 * seen an appropriate data phase yet.
4792 * If the residual occurred on the last
4793 * transfer and the transfer request was
4794 * expected to end on an odd count, do
4795 * nothing. Otherwise, subtract a byte
4796 * and update the residual count accordingly.
4800 sgptr = ahd_inb_scbram(ahd, SCB_RESIDUAL_SGPTR);
4801 if ((sgptr & SG_LIST_NULL) != 0
4802 && (ahd_inb_scbram(ahd, SCB_TASK_ATTRIBUTE)
4803 & SCB_XFERLEN_ODD) != 0) {
4805 * If the residual occurred on the last
4806 * transfer and the transfer request was
4807 * expected to end on an odd count, do
4815 /* Pull in the rest of the sgptr */
4816 sgptr = ahd_inl_scbram(ahd, SCB_RESIDUAL_SGPTR);
4817 data_cnt = ahd_inl_scbram(ahd, SCB_RESIDUAL_DATACNT);
4818 if ((sgptr & SG_LIST_NULL) != 0) {
4820 * The residual data count is not updated
4821 * for the command run to completion case.
4822 * Explicitly zero the count.
4824 data_cnt &= ~AHD_SG_LEN_MASK;
4826 data_addr = ahd_inq(ahd, SHADDR);
4829 sgptr &= SG_PTR_MASK;
4830 if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0) {
4831 struct ahd_dma64_seg *sg;
4833 sg = ahd_sg_bus_to_virt(ahd, scb, sgptr);
4836 * The residual sg ptr points to the next S/G
4837 * to load so we must go back one.
4840 sglen = aic_le32toh(sg->len) & AHD_SG_LEN_MASK;
4841 if (sg != scb->sg_list
4842 && sglen < (data_cnt & AHD_SG_LEN_MASK)) {
4845 sglen = aic_le32toh(sg->len);
4847 * Preserve High Address and SG_LIST
4848 * bits while setting the count to 1.
4850 data_cnt = 1|(sglen&(~AHD_SG_LEN_MASK));
4851 data_addr = aic_le64toh(sg->addr)
4852 + (sglen & AHD_SG_LEN_MASK)
4856 * Increment sg so it points to the
4860 sgptr = ahd_sg_virt_to_bus(ahd, scb,
4864 struct ahd_dma_seg *sg;
4866 sg = ahd_sg_bus_to_virt(ahd, scb, sgptr);
4869 * The residual sg ptr points to the next S/G
4870 * to load so we must go back one.
4873 sglen = aic_le32toh(sg->len) & AHD_SG_LEN_MASK;
4874 if (sg != scb->sg_list
4875 && sglen < (data_cnt & AHD_SG_LEN_MASK)) {
4878 sglen = aic_le32toh(sg->len);
4880 * Preserve High Address and SG_LIST
4881 * bits while setting the count to 1.
4883 data_cnt = 1|(sglen&(~AHD_SG_LEN_MASK));
4884 data_addr = aic_le32toh(sg->addr)
4885 + (sglen & AHD_SG_LEN_MASK)
4889 * Increment sg so it points to the
4893 sgptr = ahd_sg_virt_to_bus(ahd, scb,
4898 * Toggle the "oddness" of the transfer length
4899 * to handle this mid-transfer ignore wide
4900 * residue. This ensures that the oddness is
4901 * correct for subsequent data transfers.
4903 ahd_outb(ahd, SCB_TASK_ATTRIBUTE,
4904 ahd_inb_scbram(ahd, SCB_TASK_ATTRIBUTE)
4907 ahd_outl(ahd, SCB_RESIDUAL_SGPTR, sgptr);
4908 ahd_outl(ahd, SCB_RESIDUAL_DATACNT, data_cnt);
4910 * The FIFO's pointers will be updated if/when the
4911 * sequencer re-enters a data phase.
4919 * Reinitialize the data pointers for the active transfer
4920 * based on its current residual.
4923 ahd_reinitialize_dataptrs(struct ahd_softc *ahd)
4926 ahd_mode_state saved_modes;
4933 AHD_ASSERT_MODES(ahd, AHD_MODE_DFF0_MSK|AHD_MODE_DFF1_MSK,
4934 AHD_MODE_DFF0_MSK|AHD_MODE_DFF1_MSK);
4936 scb_index = ahd_get_scbptr(ahd);
4937 scb = ahd_lookup_scb(ahd, scb_index);
4940 * Release and reacquire the FIFO so we
4941 * have a clean slate.
4943 ahd_outb(ahd, DFFSXFRCTL, CLRCHN);
4945 while (--wait && !(ahd_inb(ahd, MDFFSTAT) & FIFOFREE))
4948 ahd_print_path(ahd, scb);
4949 kprintf("ahd_reinitialize_dataptrs: Forcing FIFO free.\n");
4950 ahd_outb(ahd, DFFSXFRCTL, RSTCHN|CLRSHCNT);
4952 saved_modes = ahd_save_modes(ahd);
4953 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
4954 ahd_outb(ahd, DFFSTAT,
4955 ahd_inb(ahd, DFFSTAT)
4956 | (saved_modes == 0x11 ? CURRFIFO_1 : CURRFIFO_0));
4959 * Determine initial values for data_addr and data_cnt
4960 * for resuming the data phase.
4962 sgptr = ahd_inl_scbram(ahd, SCB_RESIDUAL_SGPTR);
4963 sgptr &= SG_PTR_MASK;
4965 resid = (ahd_inb_scbram(ahd, SCB_RESIDUAL_DATACNT + 2) << 16)
4966 | (ahd_inb_scbram(ahd, SCB_RESIDUAL_DATACNT + 1) << 8)
4967 | ahd_inb_scbram(ahd, SCB_RESIDUAL_DATACNT);
4969 if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0) {
4970 struct ahd_dma64_seg *sg;
4972 sg = ahd_sg_bus_to_virt(ahd, scb, sgptr);
4974 /* The residual sg_ptr always points to the next sg */
4977 dataptr = aic_le64toh(sg->addr)
4978 + (aic_le32toh(sg->len) & AHD_SG_LEN_MASK)
4980 ahd_outl(ahd, HADDR + 4, dataptr >> 32);
4982 struct ahd_dma_seg *sg;
4984 sg = ahd_sg_bus_to_virt(ahd, scb, sgptr);
4986 /* The residual sg_ptr always points to the next sg */
4989 dataptr = aic_le32toh(sg->addr)
4990 + (aic_le32toh(sg->len) & AHD_SG_LEN_MASK)
4992 ahd_outb(ahd, HADDR + 4,
4993 (aic_le32toh(sg->len) & ~AHD_SG_LEN_MASK) >> 24);
4995 ahd_outl(ahd, HADDR, dataptr);
4996 ahd_outb(ahd, HCNT + 2, resid >> 16);
4997 ahd_outb(ahd, HCNT + 1, resid >> 8);
4998 ahd_outb(ahd, HCNT, resid);
5002 * Handle the effects of issuing a bus device reset message.
5005 ahd_handle_devreset(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
5006 u_int lun, cam_status status, char *message,
5009 #ifdef AHD_TARGET_MODE
5010 struct ahd_tmode_tstate* tstate;
5014 found = ahd_abort_scbs(ahd, devinfo->target, devinfo->channel,
5015 lun, SCB_LIST_NULL, devinfo->role,
5018 #ifdef AHD_TARGET_MODE
5020 * Send an immediate notify ccb to all target mord peripheral
5021 * drivers affected by this action.
5023 tstate = ahd->enabled_targets[devinfo->our_scsiid];
5024 if (tstate != NULL) {
5028 if (lun != CAM_LUN_WILDCARD) {
5030 max_lun = AHD_NUM_LUNS - 1;
5035 for (cur_lun <= max_lun; cur_lun++) {
5036 struct ahd_tmode_lstate* lstate;
5038 lstate = tstate->enabled_luns[cur_lun];
5042 ahd_queue_lstate_event(ahd, lstate, devinfo->our_scsiid,
5043 MSG_BUS_DEV_RESET, /*arg*/0);
5044 ahd_send_lstate_events(ahd, lstate);
5050 * Go back to async/narrow transfers and renegotiate.
5052 ahd_set_width(ahd, devinfo, MSG_EXT_WDTR_BUS_8_BIT,
5053 AHD_TRANS_CUR, /*paused*/TRUE);
5054 ahd_set_syncrate(ahd, devinfo, /*period*/0, /*offset*/0,
5055 /*ppr_options*/0, AHD_TRANS_CUR, /*paused*/TRUE);
5057 ahd_send_async(ahd, devinfo->channel, devinfo->target,
5058 lun, AC_SENT_BDR, NULL);
5061 && (verbose_level <= bootverbose))
5062 kprintf("%s: %s on %c:%d. %d SCBs aborted\n", ahd_name(ahd),
5063 message, devinfo->channel, devinfo->target, found);
5066 #ifdef AHD_TARGET_MODE
5068 ahd_setup_target_msgin(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
5073 * To facilitate adding multiple messages together,
5074 * each routine should increment the index and len
5075 * variables instead of setting them explicitly.
5077 ahd->msgout_index = 0;
5078 ahd->msgout_len = 0;
5080 if (scb != NULL && (scb->flags & SCB_AUTO_NEGOTIATE) != 0)
5081 ahd_build_transfer_msg(ahd, devinfo);
5083 panic("ahd_intr: AWAITING target message with no message");
5085 ahd->msgout_index = 0;
5086 ahd->msg_type = MSG_TYPE_TARGET_MSGIN;
5089 /**************************** Initialization **********************************/
5091 ahd_sglist_size(struct ahd_softc *ahd)
5093 bus_size_t list_size;
5095 list_size = sizeof(struct ahd_dma_seg) * AHD_NSEG;
5096 if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0)
5097 list_size = sizeof(struct ahd_dma64_seg) * AHD_NSEG;
5102 * Calculate the optimum S/G List allocation size. S/G elements used
5103 * for a given transaction must be physically contiguous. Assume the
5104 * OS will allocate full pages to us, so it doesn't make sense to request
5108 ahd_sglist_allocsize(struct ahd_softc *ahd)
5110 bus_size_t sg_list_increment;
5111 bus_size_t sg_list_size;
5112 bus_size_t max_list_size;
5113 bus_size_t best_list_size;
5115 /* Start out with the minimum required for AHD_NSEG. */
5116 sg_list_increment = ahd_sglist_size(ahd);
5117 sg_list_size = sg_list_increment;
5119 /* Get us as close as possible to a page in size. */
5120 while ((sg_list_size + sg_list_increment) <= PAGE_SIZE)
5121 sg_list_size += sg_list_increment;
5124 * Try to reduce the amount of wastage by allocating
5127 best_list_size = sg_list_size;
5128 max_list_size = roundup(sg_list_increment, PAGE_SIZE);
5129 if (max_list_size < 4 * PAGE_SIZE)
5130 max_list_size = 4 * PAGE_SIZE;
5131 if (max_list_size > (AHD_SCB_MAX_ALLOC * sg_list_increment))
5132 max_list_size = (AHD_SCB_MAX_ALLOC * sg_list_increment);
5133 while ((sg_list_size + sg_list_increment) <= max_list_size
5134 && (sg_list_size % PAGE_SIZE) != 0) {
5136 bus_size_t best_mod;
5138 sg_list_size += sg_list_increment;
5139 new_mod = sg_list_size % PAGE_SIZE;
5140 best_mod = best_list_size % PAGE_SIZE;
5141 if (new_mod > best_mod || new_mod == 0) {
5142 best_list_size = sg_list_size;
5145 return (best_list_size);
5149 * Allocate a controller structure for a new device
5150 * and perform initial initializion.
5153 ahd_alloc(void *platform_arg, char *name)
5155 struct ahd_softc *ahd;
5157 #if !defined(__DragonFly__) && !defined(__FreeBSD__)
5158 ahd = kmalloc(sizeof(*ahd), M_DEVBUF, M_INTWAIT);
5160 ahd = device_get_softc((device_t)platform_arg);
5162 memset(ahd, 0, sizeof(*ahd));
5163 ahd->seep_config = kmalloc(sizeof(*ahd->seep_config),M_DEVBUF,M_INTWAIT);
5164 LIST_INIT(&ahd->pending_scbs);
5165 /* We don't know our unit number until the OSM sets it */
5168 ahd->description = NULL;
5169 ahd->bus_description = NULL;
5171 ahd->chip = AHD_NONE;
5172 ahd->features = AHD_FENONE;
5173 ahd->bugs = AHD_BUGNONE;
5174 ahd->flags = AHD_SPCHK_ENB_A|AHD_RESET_BUS_A|AHD_TERM_ENB_A
5175 | AHD_EXTENDED_TRANS_A|AHD_STPWLEVEL_A;
5176 aic_timer_init(&ahd->reset_timer);
5177 aic_timer_init(&ahd->stat_timer);
5178 ahd->int_coalescing_timer = AHD_INT_COALESCING_TIMER_DEFAULT;
5179 ahd->int_coalescing_maxcmds = AHD_INT_COALESCING_MAXCMDS_DEFAULT;
5180 ahd->int_coalescing_mincmds = AHD_INT_COALESCING_MINCMDS_DEFAULT;
5181 ahd->int_coalescing_threshold = AHD_INT_COALESCING_THRESHOLD_DEFAULT;
5182 ahd->int_coalescing_stop_threshold =
5183 AHD_INT_COALESCING_STOP_THRESHOLD_DEFAULT;
5185 if (ahd_platform_alloc(ahd, platform_arg) != 0) {
5190 if ((ahd_debug & AHD_SHOW_MEMORY) != 0) {
5191 kprintf("%s: scb size = 0x%x, hscb size = 0x%x\n",
5192 ahd_name(ahd), (u_int)sizeof(struct scb),
5193 (u_int)sizeof(struct hardware_scb));
5200 ahd_softc_init(struct ahd_softc *ahd)
5209 ahd_softc_insert(struct ahd_softc *ahd)
5211 struct ahd_softc *list_ahd;
5213 #if AIC_PCI_CONFIG > 0
5215 * Second Function PCI devices need to inherit some
5216 * settings from function 0.
5218 if ((ahd->features & AHD_MULTI_FUNC) != 0) {
5219 TAILQ_FOREACH(list_ahd, &ahd_tailq, links) {
5220 aic_dev_softc_t list_pci;
5221 aic_dev_softc_t pci;
5223 list_pci = list_ahd->dev_softc;
5224 pci = ahd->dev_softc;
5225 if (aic_get_pci_slot(list_pci) == aic_get_pci_slot(pci)
5226 && aic_get_pci_bus(list_pci) == aic_get_pci_bus(pci)) {
5227 struct ahd_softc *master;
5228 struct ahd_softc *slave;
5230 if (aic_get_pci_function(list_pci) == 0) {
5237 slave->flags &= ~AHD_BIOS_ENABLED;
5239 master->flags & AHD_BIOS_ENABLED;
5247 * Insertion sort into our list of softcs.
5249 list_ahd = TAILQ_FIRST(&ahd_tailq);
5250 while (list_ahd != NULL
5251 && ahd_softc_comp(ahd, list_ahd) <= 0)
5252 list_ahd = TAILQ_NEXT(list_ahd, links);
5253 if (list_ahd != NULL)
5254 TAILQ_INSERT_BEFORE(list_ahd, ahd, links);
5256 TAILQ_INSERT_TAIL(&ahd_tailq, ahd, links);
5261 * Verify that the passed in softc pointer is for a
5262 * controller that is still configured.
5265 ahd_find_softc(struct ahd_softc *ahd)
5267 struct ahd_softc *list_ahd;
5269 TAILQ_FOREACH(list_ahd, &ahd_tailq, links) {
5270 if (list_ahd == ahd)
5277 ahd_set_unit(struct ahd_softc *ahd, int unit)
5283 ahd_set_name(struct ahd_softc *ahd, char *name)
5285 if (ahd->name != NULL)
5286 kfree(ahd->name, M_DEVBUF);
5291 ahd_free(struct ahd_softc *ahd)
5295 ahd_terminate_recovery_thread(ahd);
5296 switch (ahd->init_level) {
5302 aic_dmamap_unload(ahd, ahd->shared_data_dmat,
5303 ahd->shared_data_map.dmamap);
5306 aic_dmamem_free(ahd, ahd->shared_data_dmat, ahd->qoutfifo,
5307 ahd->shared_data_map.dmamap);
5308 aic_dmamap_destroy(ahd, ahd->shared_data_dmat,
5309 ahd->shared_data_map.dmamap);
5312 aic_dma_tag_destroy(ahd, ahd->shared_data_dmat);
5315 aic_dma_tag_destroy(ahd, ahd->buffer_dmat);
5323 aic_dma_tag_destroy(ahd, ahd->parent_dmat);
5325 ahd_platform_free(ahd);
5326 ahd_fini_scbdata(ahd);
5327 for (i = 0; i < AHD_NUM_TARGETS; i++) {
5328 struct ahd_tmode_tstate *tstate;
5330 tstate = ahd->enabled_targets[i];
5331 if (tstate != NULL) {
5335 for (j = 0; j < AHD_NUM_LUNS; j++) {
5336 struct ahd_tmode_lstate *lstate;
5338 lstate = tstate->enabled_luns[j];
5339 if (lstate != NULL) {
5340 xpt_free_path(lstate->path);
5341 kfree(lstate, M_DEVBUF);
5345 kfree(tstate, M_DEVBUF);
5349 if (ahd->black_hole != NULL) {
5350 xpt_free_path(ahd->black_hole->path);
5351 kfree(ahd->black_hole, M_DEVBUF);
5354 if (ahd->name != NULL)
5355 kfree(ahd->name, M_DEVBUF);
5356 if (ahd->seep_config != NULL)
5357 kfree(ahd->seep_config, M_DEVBUF);
5358 if (ahd->saved_stack != NULL)
5359 kfree(ahd->saved_stack, M_DEVBUF);
5360 #if !defined(__DragonFly__) && !defined(__FreeBSD__)
5361 kfree(ahd, M_DEVBUF);
5367 ahd_shutdown(void *arg)
5369 struct ahd_softc *ahd;
5371 ahd = (struct ahd_softc *)arg;
5374 * Stop periodic timer callbacks.
5376 aic_timer_stop(&ahd->reset_timer);
5377 aic_timer_stop(&ahd->stat_timer);
5379 /* This will reset most registers to 0, but not all */
5380 ahd_reset(ahd, /*reinit*/FALSE);
5384 * Reset the controller and record some information about it
5385 * that is only available just after a reset. If "reinit" is
5386 * non-zero, this reset occured after initial configuration
5387 * and the caller requests that the chip be fully reinitialized
5388 * to a runable state. Chip interrupts are *not* enabled after
5389 * a reinitialization. The caller must enable interrupts via
5390 * ahd_intr_enable().
5393 ahd_reset(struct ahd_softc *ahd, int reinit)
5400 * Preserve the value of the SXFRCTL1 register for all channels.
5401 * It contains settings that affect termination and we don't want
5402 * to disturb the integrity of the bus.
5405 ahd_update_modes(ahd);
5406 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
5407 sxfrctl1 = ahd_inb(ahd, SXFRCTL1);
5409 cmd = aic_pci_read_config(ahd->dev_softc, PCIR_COMMAND, /*bytes*/2);
5410 if ((ahd->bugs & AHD_PCIX_CHIPRST_BUG) != 0) {
5415 * During the assertion of CHIPRST, the chip
5416 * does not disable its parity logic prior to
5417 * the start of the reset. This may cause a
5418 * parity error to be detected and thus a
5419 * spurious SERR or PERR assertion. Disble
5420 * PERR and SERR responses during the CHIPRST.
5422 mod_cmd = cmd & ~(PCIM_CMD_PERRESPEN|PCIM_CMD_SERRESPEN);
5423 aic_pci_write_config(ahd->dev_softc, PCIR_COMMAND,
5424 mod_cmd, /*bytes*/2);
5426 ahd_outb(ahd, HCNTRL, CHIPRST | ahd->pause);
5429 * Ensure that the reset has finished. We delay 1000us
5430 * prior to reading the register to make sure the chip
5431 * has sufficiently completed its reset to handle register
5437 } while (--wait && !(ahd_inb(ahd, HCNTRL) & CHIPRSTACK));
5440 kprintf("%s: WARNING - Failed chip reset! "
5441 "Trying to initialize anyway.\n", ahd_name(ahd));
5443 ahd_outb(ahd, HCNTRL, ahd->pause);
5445 if ((ahd->bugs & AHD_PCIX_CHIPRST_BUG) != 0) {
5447 * Clear any latched PCI error status and restore
5448 * previous SERR and PERR response enables.
5450 aic_pci_write_config(ahd->dev_softc, PCIR_STATUS + 1,
5452 aic_pci_write_config(ahd->dev_softc, PCIR_COMMAND,
5457 * Mode should be SCSI after a chip reset, but lets
5458 * set it just to be safe. We touch the MODE_PTR
5459 * register directly so as to bypass the lazy update
5460 * code in ahd_set_modes().
5462 ahd_known_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
5463 ahd_outb(ahd, MODE_PTR,
5464 ahd_build_mode_state(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI));
5469 * We must always initialize STPWEN to 1 before we
5470 * restore the saved values. STPWEN is initialized
5471 * to a tri-state condition which can only be cleared
5474 ahd_outb(ahd, SXFRCTL1, sxfrctl1|STPWEN);
5475 ahd_outb(ahd, SXFRCTL1, sxfrctl1);
5477 /* Determine chip configuration */
5478 ahd->features &= ~AHD_WIDE;
5479 if ((ahd_inb(ahd, SBLKCTL) & SELWIDE) != 0)
5480 ahd->features |= AHD_WIDE;
5483 * If a recovery action has forced a chip reset,
5484 * re-initialize the chip to our liking.
5493 * Determine the number of SCBs available on the controller
5496 ahd_probe_scbs(struct ahd_softc *ahd) {
5499 AHD_ASSERT_MODES(ahd, ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK),
5500 ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK));
5501 for (i = 0; i < AHD_SCB_MAX; i++) {
5504 ahd_set_scbptr(ahd, i);
5505 ahd_outw(ahd, SCB_BASE, i);
5506 for (j = 2; j < 64; j++)
5507 ahd_outb(ahd, SCB_BASE+j, 0);
5508 /* Start out life as unallocated (needing an abort) */
5509 ahd_outb(ahd, SCB_CONTROL, MK_MESSAGE);
5510 if (ahd_inw_scbram(ahd, SCB_BASE) != i)
5512 ahd_set_scbptr(ahd, 0);
5513 if (ahd_inw_scbram(ahd, SCB_BASE) != 0)
5520 ahd_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
5524 baddr = (bus_addr_t *)arg;
5525 *baddr = segs->ds_addr;
5529 ahd_initialize_hscbs(struct ahd_softc *ahd)
5533 for (i = 0; i < ahd->scb_data.maxhscbs; i++) {
5534 ahd_set_scbptr(ahd, i);
5536 /* Clear the control byte. */
5537 ahd_outb(ahd, SCB_CONTROL, 0);
5539 /* Set the next pointer */
5540 ahd_outw(ahd, SCB_NEXT, SCB_LIST_NULL);
5545 ahd_init_scbdata(struct ahd_softc *ahd)
5547 struct scb_data *scb_data;
5550 scb_data = &ahd->scb_data;
5551 TAILQ_INIT(&scb_data->free_scbs);
5552 for (i = 0; i < AHD_NUM_TARGETS * AHD_NUM_LUNS_NONPKT; i++)
5553 LIST_INIT(&scb_data->free_scb_lists[i]);
5554 LIST_INIT(&scb_data->any_dev_free_scb_list);
5555 SLIST_INIT(&scb_data->hscb_maps);
5556 SLIST_INIT(&scb_data->sg_maps);
5557 SLIST_INIT(&scb_data->sense_maps);
5559 /* Determine the number of hardware SCBs and initialize them */
5560 scb_data->maxhscbs = ahd_probe_scbs(ahd);
5561 if (scb_data->maxhscbs == 0) {
5562 kprintf("%s: No SCB space found\n", ahd_name(ahd));
5566 ahd_initialize_hscbs(ahd);
5569 * Create our DMA tags. These tags define the kinds of device
5570 * accessible memory allocations and memory mappings we will
5571 * need to perform during normal operation.
5573 * Unless we need to further restrict the allocation, we rely
5574 * on the restrictions of the parent dmat, hence the common
5575 * use of MAXADDR and MAXSIZE.
5578 /* DMA tag for our hardware scb structures */
5579 if (aic_dma_tag_create(ahd, ahd->parent_dmat, /*alignment*/1,
5580 /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
5581 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
5582 /*highaddr*/BUS_SPACE_MAXADDR,
5583 /*filter*/NULL, /*filterarg*/NULL,
5584 PAGE_SIZE, /*nsegments*/1,
5585 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
5586 /*flags*/0, &scb_data->hscb_dmat) != 0) {
5590 scb_data->init_level++;
5592 /* DMA tag for our S/G structures. */
5593 if (aic_dma_tag_create(ahd, ahd->parent_dmat, /*alignment*/8,
5594 /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
5595 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
5596 /*highaddr*/BUS_SPACE_MAXADDR,
5597 /*filter*/NULL, /*filterarg*/NULL,
5598 ahd_sglist_allocsize(ahd), /*nsegments*/1,
5599 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
5600 /*flags*/0, &scb_data->sg_dmat) != 0) {
5604 if ((ahd_debug & AHD_SHOW_MEMORY) != 0)
5605 kprintf("%s: ahd_sglist_allocsize = 0x%x\n", ahd_name(ahd),
5606 ahd_sglist_allocsize(ahd));
5609 scb_data->init_level++;
5611 /* DMA tag for our sense buffers. We allocate in page sized chunks */
5612 if (aic_dma_tag_create(ahd, ahd->parent_dmat, /*alignment*/1,
5613 /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
5614 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
5615 /*highaddr*/BUS_SPACE_MAXADDR,
5616 /*filter*/NULL, /*filterarg*/NULL,
5617 PAGE_SIZE, /*nsegments*/1,
5618 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
5619 /*flags*/0, &scb_data->sense_dmat) != 0) {
5623 scb_data->init_level++;
5625 /* Perform initial CCB allocation */
5626 ahd_alloc_scbs(ahd);
5628 if (scb_data->numscbs == 0) {
5629 kprintf("%s: ahd_init_scbdata - "
5630 "Unable to allocate initial scbs\n",
5636 * Note that we were successful
5646 ahd_find_scb_by_tag(struct ahd_softc *ahd, u_int tag)
5651 * Look on the pending list.
5653 LIST_FOREACH(scb, &ahd->pending_scbs, pending_links) {
5654 if (SCB_GET_TAG(scb) == tag)
5659 * Then on all of the collision free lists.
5661 TAILQ_FOREACH(scb, &ahd->scb_data.free_scbs, links.tqe) {
5662 struct scb *list_scb;
5666 if (SCB_GET_TAG(list_scb) == tag)
5668 list_scb = LIST_NEXT(list_scb, collision_links);
5673 * And finally on the generic free list.
5675 LIST_FOREACH(scb, &ahd->scb_data.any_dev_free_scb_list, links.le) {
5676 if (SCB_GET_TAG(scb) == tag)
5684 ahd_fini_scbdata(struct ahd_softc *ahd)
5686 struct scb_data *scb_data;
5688 scb_data = &ahd->scb_data;
5689 if (scb_data == NULL)
5692 switch (scb_data->init_level) {
5696 struct map_node *sns_map;
5698 while ((sns_map = SLIST_FIRST(&scb_data->sense_maps)) != NULL) {
5699 SLIST_REMOVE_HEAD(&scb_data->sense_maps, links);
5700 aic_dmamap_unload(ahd, scb_data->sense_dmat,
5702 aic_dmamem_free(ahd, scb_data->sense_dmat,
5703 sns_map->vaddr, sns_map->dmamap);
5704 kfree(sns_map, M_DEVBUF);
5706 aic_dma_tag_destroy(ahd, scb_data->sense_dmat);
5711 struct map_node *sg_map;
5713 while ((sg_map = SLIST_FIRST(&scb_data->sg_maps)) != NULL) {
5714 SLIST_REMOVE_HEAD(&scb_data->sg_maps, links);
5715 aic_dmamap_unload(ahd, scb_data->sg_dmat,
5717 aic_dmamem_free(ahd, scb_data->sg_dmat,
5718 sg_map->vaddr, sg_map->dmamap);
5719 kfree(sg_map, M_DEVBUF);
5721 aic_dma_tag_destroy(ahd, scb_data->sg_dmat);
5726 struct map_node *hscb_map;
5728 while ((hscb_map = SLIST_FIRST(&scb_data->hscb_maps)) != NULL) {
5729 SLIST_REMOVE_HEAD(&scb_data->hscb_maps, links);
5730 aic_dmamap_unload(ahd, scb_data->hscb_dmat,
5732 aic_dmamem_free(ahd, scb_data->hscb_dmat,
5733 hscb_map->vaddr, hscb_map->dmamap);
5734 kfree(hscb_map, M_DEVBUF);
5736 aic_dma_tag_destroy(ahd, scb_data->hscb_dmat);
5749 * DSP filter Bypass must be enabled until the first selection
5750 * after a change in bus mode (Razor #491 and #493).
5753 ahd_setup_iocell_workaround(struct ahd_softc *ahd)
5755 ahd_mode_state saved_modes;
5757 saved_modes = ahd_save_modes(ahd);
5758 ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
5759 ahd_outb(ahd, DSPDATACTL, ahd_inb(ahd, DSPDATACTL)
5760 | BYPASSENAB | RCVROFFSTDIS | XMITOFFSTDIS);
5761 ahd_outb(ahd, SIMODE0, ahd_inb(ahd, SIMODE0) | (ENSELDO|ENSELDI));
5763 if ((ahd_debug & AHD_SHOW_MISC) != 0)
5764 kprintf("%s: Setting up iocell workaround\n", ahd_name(ahd));
5766 ahd_restore_modes(ahd, saved_modes);
5767 ahd->flags &= ~AHD_HAD_FIRST_SEL;
5771 ahd_iocell_first_selection(struct ahd_softc *ahd)
5773 ahd_mode_state saved_modes;
5776 if ((ahd->flags & AHD_HAD_FIRST_SEL) != 0)
5778 saved_modes = ahd_save_modes(ahd);
5779 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
5780 sblkctl = ahd_inb(ahd, SBLKCTL);
5781 ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
5783 if ((ahd_debug & AHD_SHOW_MISC) != 0)
5784 kprintf("%s: iocell first selection\n", ahd_name(ahd));
5786 if ((sblkctl & ENAB40) != 0) {
5787 ahd_outb(ahd, DSPDATACTL,
5788 ahd_inb(ahd, DSPDATACTL) & ~BYPASSENAB);
5790 if ((ahd_debug & AHD_SHOW_MISC) != 0)
5791 kprintf("%s: BYPASS now disabled\n", ahd_name(ahd));
5794 ahd_outb(ahd, SIMODE0, ahd_inb(ahd, SIMODE0) & ~(ENSELDO|ENSELDI));
5795 ahd_outb(ahd, CLRINT, CLRSCSIINT);
5796 ahd_restore_modes(ahd, saved_modes);
5797 ahd->flags |= AHD_HAD_FIRST_SEL;
5800 /*************************** SCB Management ***********************************/
5802 ahd_add_col_list(struct ahd_softc *ahd, struct scb *scb, u_int col_idx)
5804 struct scb_list *free_list;
5805 struct scb_tailq *free_tailq;
5806 struct scb *first_scb;
5808 scb->flags |= SCB_ON_COL_LIST;
5809 AHD_SET_SCB_COL_IDX(scb, col_idx);
5810 free_list = &ahd->scb_data.free_scb_lists[col_idx];
5811 free_tailq = &ahd->scb_data.free_scbs;
5812 first_scb = LIST_FIRST(free_list);
5813 if (first_scb != NULL) {
5814 LIST_INSERT_AFTER(first_scb, scb, collision_links);
5816 LIST_INSERT_HEAD(free_list, scb, collision_links);
5817 TAILQ_INSERT_TAIL(free_tailq, scb, links.tqe);
5822 ahd_rem_col_list(struct ahd_softc *ahd, struct scb *scb)
5824 struct scb_list *free_list;
5825 struct scb_tailq *free_tailq;
5826 struct scb *first_scb;
5829 scb->flags &= ~SCB_ON_COL_LIST;
5830 col_idx = AHD_GET_SCB_COL_IDX(ahd, scb);
5831 free_list = &ahd->scb_data.free_scb_lists[col_idx];
5832 free_tailq = &ahd->scb_data.free_scbs;
5833 first_scb = LIST_FIRST(free_list);
5834 if (first_scb == scb) {
5835 struct scb *next_scb;
5838 * Maintain order in the collision free
5839 * lists for fairness if this device has
5840 * other colliding tags active.
5842 next_scb = LIST_NEXT(scb, collision_links);
5843 if (next_scb != NULL) {
5844 TAILQ_INSERT_AFTER(free_tailq, scb,
5845 next_scb, links.tqe);
5847 TAILQ_REMOVE(free_tailq, scb, links.tqe);
5849 LIST_REMOVE(scb, collision_links);
5853 * Get a free scb. If there are none, see if we can allocate a new SCB.
5856 ahd_get_scb(struct ahd_softc *ahd, u_int col_idx)
5863 TAILQ_FOREACH(scb, &ahd->scb_data.free_scbs, links.tqe) {
5864 if (AHD_GET_SCB_COL_IDX(ahd, scb) != col_idx) {
5865 ahd_rem_col_list(ahd, scb);
5869 if ((scb = LIST_FIRST(&ahd->scb_data.any_dev_free_scb_list)) == NULL) {
5873 ahd_alloc_scbs(ahd);
5876 LIST_REMOVE(scb, links.le);
5877 if (col_idx != AHD_NEVER_COL_IDX
5878 && (scb->col_scb != NULL)
5879 && (scb->col_scb->flags & SCB_ACTIVE) == 0) {
5880 LIST_REMOVE(scb->col_scb, links.le);
5881 ahd_add_col_list(ahd, scb->col_scb, col_idx);
5884 scb->flags |= SCB_ACTIVE;
5889 * Return an SCB resource to the free list.
5892 ahd_free_scb(struct ahd_softc *ahd, struct scb *scb)
5895 /* Clean up for the next user */
5896 scb->flags = SCB_FLAG_NONE;
5897 scb->hscb->control = 0;
5898 ahd->scb_data.scbindex[SCB_GET_TAG(scb)] = NULL;
5900 if (scb->col_scb == NULL) {
5903 * No collision possible. Just free normally.
5905 LIST_INSERT_HEAD(&ahd->scb_data.any_dev_free_scb_list,
5907 } else if ((scb->col_scb->flags & SCB_ON_COL_LIST) != 0) {
5910 * The SCB we might have collided with is on
5911 * a free collision list. Put both SCBs on
5914 ahd_rem_col_list(ahd, scb->col_scb);
5915 LIST_INSERT_HEAD(&ahd->scb_data.any_dev_free_scb_list,
5917 LIST_INSERT_HEAD(&ahd->scb_data.any_dev_free_scb_list,
5918 scb->col_scb, links.le);
5919 } else if ((scb->col_scb->flags
5920 & (SCB_PACKETIZED|SCB_ACTIVE)) == SCB_ACTIVE
5921 && (scb->col_scb->hscb->control & TAG_ENB) != 0) {
5924 * The SCB we might collide with on the next allocation
5925 * is still active in a non-packetized, tagged, context.
5926 * Put us on the SCB collision list.
5928 ahd_add_col_list(ahd, scb,
5929 AHD_GET_SCB_COL_IDX(ahd, scb->col_scb));
5932 * The SCB we might collide with on the next allocation
5933 * is either active in a packetized context, or free.
5934 * Since we can't collide, put this SCB on the generic
5937 LIST_INSERT_HEAD(&ahd->scb_data.any_dev_free_scb_list,
5941 aic_platform_scb_free(ahd, scb);
5945 ahd_alloc_scbs(struct ahd_softc *ahd)
5947 struct scb_data *scb_data;
5948 struct scb *next_scb;
5949 struct hardware_scb *hscb;
5950 struct map_node *hscb_map;
5951 struct map_node *sg_map;
5952 struct map_node *sense_map;
5954 uint8_t *sense_data;
5955 bus_addr_t hscb_busaddr;
5956 bus_addr_t sg_busaddr;
5957 bus_addr_t sense_busaddr;
5961 scb_data = &ahd->scb_data;
5962 if (scb_data->numscbs >= AHD_SCB_MAX_ALLOC)
5963 /* Can't allocate any more */
5966 if (scb_data->scbs_left != 0) {
5969 offset = (PAGE_SIZE / sizeof(*hscb)) - scb_data->scbs_left;
5970 hscb_map = SLIST_FIRST(&scb_data->hscb_maps);
5971 hscb = &((struct hardware_scb *)hscb_map->vaddr)[offset];
5972 hscb_busaddr = hscb_map->busaddr + (offset * sizeof(*hscb));
5974 hscb_map = kmalloc(sizeof(*hscb_map), M_DEVBUF, M_INTWAIT);
5976 /* Allocate the next batch of hardware SCBs */
5977 if (aic_dmamem_alloc(ahd, scb_data->hscb_dmat,
5978 (void **)&hscb_map->vaddr,
5979 BUS_DMA_NOWAIT, &hscb_map->dmamap) != 0) {
5980 kfree(hscb_map, M_DEVBUF);
5984 SLIST_INSERT_HEAD(&scb_data->hscb_maps, hscb_map, links);
5986 aic_dmamap_load(ahd, scb_data->hscb_dmat, hscb_map->dmamap,
5987 hscb_map->vaddr, PAGE_SIZE, ahd_dmamap_cb,
5988 &hscb_map->busaddr, /*flags*/0);
5990 hscb = (struct hardware_scb *)hscb_map->vaddr;
5991 hscb_busaddr = hscb_map->busaddr;
5992 scb_data->scbs_left = PAGE_SIZE / sizeof(*hscb);
5993 if (ahd->next_queued_hscb == NULL) {
5995 * We need one HSCB to serve as the "next HSCB". Since
5996 * the tag identifier in this HSCB will never be used,
5997 * there is no point in using a valid SCB from the
5998 * free pool for it. So, we allocate this "sentinel"
6001 ahd->next_queued_hscb = hscb;
6002 ahd->next_queued_hscb_map = hscb_map;
6003 memset(hscb, 0, sizeof(*hscb));
6004 hscb->hscb_busaddr = aic_htole32(hscb_busaddr);
6006 hscb_busaddr += sizeof(*hscb);
6007 scb_data->scbs_left--;
6011 if (scb_data->sgs_left != 0) {
6014 offset = ((ahd_sglist_allocsize(ahd) / ahd_sglist_size(ahd))
6015 - scb_data->sgs_left) * ahd_sglist_size(ahd);
6016 sg_map = SLIST_FIRST(&scb_data->sg_maps);
6017 segs = sg_map->vaddr + offset;
6018 sg_busaddr = sg_map->busaddr + offset;
6020 sg_map = kmalloc(sizeof(*sg_map), M_DEVBUF, M_INTWAIT);
6022 /* Allocate the next batch of S/G lists */
6023 if (aic_dmamem_alloc(ahd, scb_data->sg_dmat,
6024 (void **)&sg_map->vaddr,
6025 BUS_DMA_NOWAIT, &sg_map->dmamap) != 0) {
6026 kfree(sg_map, M_DEVBUF);
6030 SLIST_INSERT_HEAD(&scb_data->sg_maps, sg_map, links);
6032 aic_dmamap_load(ahd, scb_data->sg_dmat, sg_map->dmamap,
6033 sg_map->vaddr, ahd_sglist_allocsize(ahd),
6034 ahd_dmamap_cb, &sg_map->busaddr, /*flags*/0);
6036 segs = sg_map->vaddr;
6037 sg_busaddr = sg_map->busaddr;
6038 scb_data->sgs_left =
6039 ahd_sglist_allocsize(ahd) / ahd_sglist_size(ahd);
6041 if (ahd_debug & AHD_SHOW_MEMORY)
6042 kprintf("Mapped SG data\n");
6046 if (scb_data->sense_left != 0) {
6049 offset = PAGE_SIZE - (AHD_SENSE_BUFSIZE * scb_data->sense_left);
6050 sense_map = SLIST_FIRST(&scb_data->sense_maps);
6051 sense_data = sense_map->vaddr + offset;
6052 sense_busaddr = sense_map->busaddr + offset;
6054 sense_map = kmalloc(sizeof(*sense_map), M_DEVBUF, M_INTWAIT);
6056 /* Allocate the next batch of sense buffers */
6057 if (aic_dmamem_alloc(ahd, scb_data->sense_dmat,
6058 (void **)&sense_map->vaddr,
6059 BUS_DMA_NOWAIT, &sense_map->dmamap) != 0) {
6060 kfree(sense_map, M_DEVBUF);
6064 SLIST_INSERT_HEAD(&scb_data->sense_maps, sense_map, links);
6066 aic_dmamap_load(ahd, scb_data->sense_dmat, sense_map->dmamap,
6067 sense_map->vaddr, PAGE_SIZE, ahd_dmamap_cb,
6068 &sense_map->busaddr, /*flags*/0);
6070 sense_data = sense_map->vaddr;
6071 sense_busaddr = sense_map->busaddr;
6072 scb_data->sense_left = PAGE_SIZE / AHD_SENSE_BUFSIZE;
6074 if (ahd_debug & AHD_SHOW_MEMORY)
6075 kprintf("Mapped sense data\n");
6079 newcount = MIN(scb_data->sense_left, scb_data->scbs_left);
6080 newcount = MIN(newcount, scb_data->sgs_left);
6081 newcount = MIN(newcount, (AHD_SCB_MAX_ALLOC - scb_data->numscbs));
6082 scb_data->sense_left -= newcount;
6083 scb_data->scbs_left -= newcount;
6084 scb_data->sgs_left -= newcount;
6085 for (i = 0; i < newcount; i++) {
6086 struct scb_platform_data *pdata;
6092 next_scb = kmalloc(sizeof(*next_scb), M_DEVBUF, M_INTWAIT);
6093 pdata = kmalloc(sizeof(*pdata), M_DEVBUF, M_INTWAIT);
6094 next_scb->platform_data = pdata;
6095 next_scb->hscb_map = hscb_map;
6096 next_scb->sg_map = sg_map;
6097 next_scb->sense_map = sense_map;
6098 next_scb->sg_list = segs;
6099 next_scb->sense_data = sense_data;
6100 next_scb->sense_busaddr = sense_busaddr;
6101 memset(hscb, 0, sizeof(*hscb));
6102 next_scb->hscb = hscb;
6103 hscb->hscb_busaddr = aic_htole32(hscb_busaddr);
6106 * The sequencer always starts with the second entry.
6107 * The first entry is embedded in the scb.
6109 next_scb->sg_list_busaddr = sg_busaddr;
6110 if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0)
6111 next_scb->sg_list_busaddr
6112 += sizeof(struct ahd_dma64_seg);
6114 next_scb->sg_list_busaddr += sizeof(struct ahd_dma_seg);
6115 next_scb->ahd_softc = ahd;
6116 next_scb->flags = SCB_FLAG_NONE;
6118 error = aic_dmamap_create(ahd, ahd->buffer_dmat, /*flags*/0,
6121 kfree(next_scb, M_DEVBUF);
6122 kfree(pdata, M_DEVBUF);
6126 next_scb->hscb->tag = aic_htole16(scb_data->numscbs);
6127 col_tag = scb_data->numscbs ^ 0x100;
6128 next_scb->col_scb = ahd_find_scb_by_tag(ahd, col_tag);
6129 if (next_scb->col_scb != NULL)
6130 next_scb->col_scb->col_scb = next_scb;
6131 ahd_free_scb(ahd, next_scb);
6133 hscb_busaddr += sizeof(*hscb);
6134 segs += ahd_sglist_size(ahd);
6135 sg_busaddr += ahd_sglist_size(ahd);
6136 sense_data += AHD_SENSE_BUFSIZE;
6137 sense_busaddr += AHD_SENSE_BUFSIZE;
6138 scb_data->numscbs++;
6143 ahd_controller_info(struct ahd_softc *ahd, char *buf)
6149 len = ksprintf(buf, "%s: ",
6150 ahd_chip_names[ahd->chip & AHD_CHIPID_MASK]);
6153 speed = "Ultra320 ";
6154 if ((ahd->features & AHD_WIDE) != 0) {
6159 len = ksprintf(buf, "%s%sChannel %c, SCSI Id=%d, ",
6160 speed, type, ahd->channel, ahd->our_id);
6163 ksprintf(buf, "%s, %d SCBs", ahd->bus_description,
6164 ahd->scb_data.maxhscbs);
6167 static const char *channel_strings[] = {
6174 static const char *termstat_strings[] = {
6175 "Terminated Correctly",
6182 * Start the board, ready for normal operation
6185 ahd_init(struct ahd_softc *ahd)
6187 uint8_t *next_vaddr;
6188 bus_addr_t next_baddr;
6189 size_t driver_data_size;
6193 uint8_t current_sensing;
6196 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
6198 ahd->stack_size = ahd_probe_stack_size(ahd);
6199 ahd->saved_stack = kmalloc(ahd->stack_size * sizeof(uint16_t),
6200 M_DEVBUF, M_WAITOK);
6203 * Verify that the compiler hasn't over-agressively
6204 * padded important structures.
6206 if (sizeof(struct hardware_scb) != 64)
6207 panic("Hardware SCB size is incorrect");
6210 if ((ahd_debug & AHD_DEBUG_SEQUENCER) != 0)
6211 ahd->flags |= AHD_SEQUENCER_DEBUG;
6215 * Default to allowing initiator operations.
6217 ahd->flags |= AHD_INITIATORROLE;
6220 * Only allow target mode features if this unit has them enabled.
6222 if ((AHD_TMODE_ENABLE & (0x1 << ahd->unit)) == 0)
6223 ahd->features &= ~AHD_TARGETMODE;
6226 /* DMA tag for mapping buffers into device visible space. */
6227 if (aic_dma_tag_create(ahd, ahd->parent_dmat, /*alignment*/1,
6228 /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
6229 /*lowaddr*/ahd->flags & AHD_39BIT_ADDRESSING
6230 ? (bus_addr_t)0x7FFFFFFFFFULL
6231 : BUS_SPACE_MAXADDR_32BIT,
6232 /*highaddr*/BUS_SPACE_MAXADDR,
6233 /*filter*/NULL, /*filterarg*/NULL,
6234 /*maxsize*/(AHD_NSEG - 1) * PAGE_SIZE,
6235 /*nsegments*/AHD_NSEG,
6236 /*maxsegsz*/AHD_MAXTRANSFER_SIZE,
6237 /*flags*/BUS_DMA_ALLOCNOW,
6238 &ahd->buffer_dmat) != 0) {
6246 * DMA tag for our command fifos and other data in system memory
6247 * the card's sequencer must be able to access. For initiator
6248 * roles, we need to allocate space for the qoutfifo. When providing
6249 * for the target mode role, we must additionally provide space for
6250 * the incoming target command fifo.
6252 driver_data_size = AHD_SCB_MAX * sizeof(*ahd->qoutfifo);
6253 if ((ahd->features & AHD_TARGETMODE) != 0)
6254 driver_data_size += AHD_TMODE_CMDS * sizeof(struct target_cmd);
6255 if ((ahd->bugs & AHD_PKT_BITBUCKET_BUG) != 0)
6256 driver_data_size += PKT_OVERRUN_BUFSIZE;
6257 if (aic_dma_tag_create(ahd, ahd->parent_dmat, /*alignment*/1,
6258 /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
6259 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
6260 /*highaddr*/BUS_SPACE_MAXADDR,
6261 /*filter*/NULL, /*filterarg*/NULL,
6264 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
6265 /*flags*/0, &ahd->shared_data_dmat) != 0) {
6271 /* Allocation of driver data */
6272 if (aic_dmamem_alloc(ahd, ahd->shared_data_dmat,
6273 (void **)&ahd->shared_data_map.vaddr,
6275 &ahd->shared_data_map.dmamap) != 0) {
6281 /* And permanently map it in */
6282 aic_dmamap_load(ahd, ahd->shared_data_dmat, ahd->shared_data_map.dmamap,
6283 ahd->shared_data_map.vaddr, driver_data_size,
6284 ahd_dmamap_cb, &ahd->shared_data_map.busaddr,
6286 ahd->qoutfifo = (struct ahd_completion *)ahd->shared_data_map.vaddr;
6287 next_vaddr = (uint8_t *)&ahd->qoutfifo[AHD_QOUT_SIZE];
6288 next_baddr = ahd->shared_data_map.busaddr
6289 + AHD_QOUT_SIZE*sizeof(struct ahd_completion);
6290 if ((ahd->features & AHD_TARGETMODE) != 0) {
6291 ahd->targetcmds = (struct target_cmd *)next_vaddr;
6292 next_vaddr += AHD_TMODE_CMDS * sizeof(struct target_cmd);
6293 next_baddr += AHD_TMODE_CMDS * sizeof(struct target_cmd);
6296 if ((ahd->bugs & AHD_PKT_BITBUCKET_BUG) != 0) {
6297 ahd->overrun_buf = next_vaddr;
6298 next_vaddr += PKT_OVERRUN_BUFSIZE;
6299 next_baddr += PKT_OVERRUN_BUFSIZE;
6304 /* Allocate SCB data now that buffer_dmat is initialized */
6305 if (ahd_init_scbdata(ahd) != 0)
6308 if ((ahd->flags & AHD_INITIATORROLE) == 0)
6309 ahd->flags &= ~AHD_RESET_BUS_A;
6312 * Before committing these settings to the chip, give
6313 * the OSM one last chance to modify our configuration.
6315 ahd_platform_init(ahd);
6317 /* Bring up the chip. */
6320 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
6322 if ((ahd->flags & AHD_CURRENT_SENSING) == 0)
6326 * Verify termination based on current draw and
6327 * warn user if the bus is over/under terminated.
6329 error = ahd_write_flexport(ahd, FLXADDR_ROMSTAT_CURSENSECTL,
6332 kprintf("%s: current sensing timeout 1\n", ahd_name(ahd));
6335 for (i = 20, fstat = FLX_FSTAT_BUSY;
6336 (fstat & FLX_FSTAT_BUSY) != 0 && i; i--) {
6337 error = ahd_read_flexport(ahd, FLXADDR_FLEXSTAT, &fstat);
6339 kprintf("%s: current sensing timeout 2\n",
6345 kprintf("%s: Timedout during current-sensing test\n",
6350 /* Latch Current Sensing status. */
6351 error = ahd_read_flexport(ahd, FLXADDR_CURRENT_STAT, ¤t_sensing);
6353 kprintf("%s: current sensing timeout 3\n", ahd_name(ahd));
6357 /* Diable current sensing. */
6358 ahd_write_flexport(ahd, FLXADDR_ROMSTAT_CURSENSECTL, 0);
6361 if ((ahd_debug & AHD_SHOW_TERMCTL) != 0) {
6362 kprintf("%s: current_sensing == 0x%x\n",
6363 ahd_name(ahd), current_sensing);
6367 for (i = 0; i < 4; i++, current_sensing >>= FLX_CSTAT_SHIFT) {
6370 term_stat = (current_sensing & FLX_CSTAT_MASK);
6371 switch (term_stat) {
6372 case FLX_CSTAT_OVER:
6373 case FLX_CSTAT_UNDER:
6375 case FLX_CSTAT_INVALID:
6376 case FLX_CSTAT_OKAY:
6377 if (warn_user == 0 && bootverbose == 0)
6379 kprintf("%s: %s Channel %s\n", ahd_name(ahd),
6380 channel_strings[i], termstat_strings[term_stat]);
6385 kprintf("%s: WARNING. Termination is not configured correctly.\n"
6386 "%s: WARNING. SCSI bus operations may FAIL.\n",
6387 ahd_name(ahd), ahd_name(ahd));
6391 aic_timer_reset(&ahd->stat_timer, AHD_STAT_UPDATE_US,
6392 ahd_stat_timer, ahd);
6397 * (Re)initialize chip state after a chip reset.
6400 ahd_chip_init(struct ahd_softc *ahd)
6404 u_int scsiseq_template;
6409 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
6411 * Take the LED out of diagnostic mode
6413 ahd_outb(ahd, SBLKCTL, ahd_inb(ahd, SBLKCTL) & ~(DIAGLEDEN|DIAGLEDON));
6416 * Return HS_MAILBOX to its default value.
6418 ahd->hs_mailbox = 0;
6419 ahd_outb(ahd, HS_MAILBOX, 0);
6421 /* Set the SCSI Id, SXFRCTL0, SXFRCTL1, and SIMODE1. */
6422 ahd_outb(ahd, IOWNID, ahd->our_id);
6423 ahd_outb(ahd, TOWNID, ahd->our_id);
6424 sxfrctl1 = (ahd->flags & AHD_TERM_ENB_A) != 0 ? STPWEN : 0;
6425 sxfrctl1 |= (ahd->flags & AHD_SPCHK_ENB_A) != 0 ? ENSPCHK : 0;
6426 if ((ahd->bugs & AHD_LONG_SETIMO_BUG)
6427 && (ahd->seltime != STIMESEL_MIN)) {
6429 * The selection timer duration is twice as long
6430 * as it should be. Halve it by adding "1" to
6431 * the user specified setting.
6433 sxfrctl1 |= ahd->seltime + STIMESEL_BUG_ADJ;
6435 sxfrctl1 |= ahd->seltime;
6438 ahd_outb(ahd, SXFRCTL0, DFON);
6439 ahd_outb(ahd, SXFRCTL1, sxfrctl1|ahd->seltime|ENSTIMER|ACTNEGEN);
6440 ahd_outb(ahd, SIMODE1, ENSELTIMO|ENSCSIRST|ENSCSIPERR);
6443 * Now that termination is set, wait for up
6444 * to 500ms for our transceivers to settle. If
6445 * the adapter does not have a cable attached,
6446 * the transceivers may never settle, so don't
6447 * complain if we fail here.
6450 (ahd_inb(ahd, SBLKCTL) & (ENAB40|ENAB20)) == 0 && wait;
6454 /* Clear any false bus resets due to the transceivers settling */
6455 ahd_outb(ahd, CLRSINT1, CLRSCSIRSTI);
6456 ahd_outb(ahd, CLRINT, CLRSCSIINT);
6458 /* Initialize mode specific S/G state. */
6459 for (i = 0; i < 2; i++) {
6460 ahd_set_modes(ahd, AHD_MODE_DFF0 + i, AHD_MODE_DFF0 + i);
6461 ahd_outb(ahd, LONGJMP_ADDR + 1, INVALID_ADDR);
6462 ahd_outb(ahd, SG_STATE, 0);
6463 ahd_outb(ahd, CLRSEQINTSRC, 0xFF);
6464 ahd_outb(ahd, SEQIMODE,
6465 ENSAVEPTRS|ENCFG4DATA|ENCFG4ISTAT
6466 |ENCFG4TSTAT|ENCFG4ICMD|ENCFG4TCMD);
6469 ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
6470 ahd_outb(ahd, DSCOMMAND0, ahd_inb(ahd, DSCOMMAND0)|MPARCKEN|CACHETHEN);
6471 ahd_outb(ahd, DFF_THRSH, RD_DFTHRSH_75|WR_DFTHRSH_75);
6472 ahd_outb(ahd, SIMODE0, ENIOERR|ENOVERRUN);
6473 ahd_outb(ahd, SIMODE3, ENNTRAMPERR|ENOSRAMPERR);
6474 if ((ahd->bugs & AHD_BUSFREEREV_BUG) != 0) {
6475 ahd_outb(ahd, OPTIONMODE, AUTOACKEN|AUTO_MSGOUT_DE);
6477 ahd_outb(ahd, OPTIONMODE, AUTOACKEN|BUSFREEREV|AUTO_MSGOUT_DE);
6479 ahd_outb(ahd, SCSCHKN, CURRFIFODEF|WIDERESEN|SHVALIDSTDIS);
6480 if ((ahd->chip & AHD_BUS_MASK) == AHD_PCIX)
6482 * Do not issue a target abort when a split completion
6483 * error occurs. Let our PCIX interrupt handler deal
6484 * with it instead. H2A4 Razor #625
6486 ahd_outb(ahd, PCIXCTL, ahd_inb(ahd, PCIXCTL) | SPLTSTADIS);
6488 if ((ahd->bugs & AHD_LQOOVERRUN_BUG) != 0)
6489 ahd_outb(ahd, LQOSCSCTL, LQONOCHKOVER);
6492 * Tweak IOCELL settings.
6494 if ((ahd->flags & AHD_HP_BOARD) != 0) {
6495 for (i = 0; i < NUMDSPS; i++) {
6496 ahd_outb(ahd, DSPSELECT, i);
6497 ahd_outb(ahd, WRTBIASCTL, WRTBIASCTL_HP_DEFAULT);
6500 if ((ahd_debug & AHD_SHOW_MISC) != 0)
6501 kprintf("%s: WRTBIASCTL now 0x%x\n", ahd_name(ahd),
6502 WRTBIASCTL_HP_DEFAULT);
6505 ahd_setup_iocell_workaround(ahd);
6508 * Enable LQI Manager interrupts.
6510 ahd_outb(ahd, LQIMODE1, ENLQIPHASE_LQ|ENLQIPHASE_NLQ|ENLIQABORT
6511 | ENLQICRCI_LQ|ENLQICRCI_NLQ|ENLQIBADLQI
6512 | ENLQIOVERI_LQ|ENLQIOVERI_NLQ);
6513 ahd_outb(ahd, LQOMODE0, ENLQOATNLQ|ENLQOATNPKT|ENLQOTCRC);
6515 * An interrupt from LQOBUSFREE is made redundant by the
6516 * BUSFREE interrupt. We choose to have the sequencer catch
6517 * LQOPHCHGINPKT errors manually for the command phase at the
6518 * start of a packetized selection case.
6519 ahd_outb(ahd, LQOMODE1, ENLQOBUSFREE|ENLQOPHACHGINPKT);
6521 ahd_outb(ahd, LQOMODE1, 0);
6524 * Setup sequencer interrupt handlers.
6526 ahd_outw(ahd, INTVEC1_ADDR, ahd_resolve_seqaddr(ahd, LABEL_seq_isr));
6527 ahd_outw(ahd, INTVEC2_ADDR, ahd_resolve_seqaddr(ahd, LABEL_timer_isr));
6530 * Setup SCB Offset registers.
6532 if ((ahd->bugs & AHD_PKT_LUN_BUG) != 0) {
6533 ahd_outb(ahd, LUNPTR, offsetof(struct hardware_scb,
6536 ahd_outb(ahd, LUNPTR, offsetof(struct hardware_scb, lun));
6538 ahd_outb(ahd, CMDLENPTR, offsetof(struct hardware_scb, cdb_len));
6539 ahd_outb(ahd, ATTRPTR, offsetof(struct hardware_scb, task_attribute));
6540 ahd_outb(ahd, FLAGPTR, offsetof(struct hardware_scb, task_management));
6541 ahd_outb(ahd, CMDPTR, offsetof(struct hardware_scb,
6542 shared_data.idata.cdb));
6543 ahd_outb(ahd, QNEXTPTR,
6544 offsetof(struct hardware_scb, next_hscb_busaddr));
6545 ahd_outb(ahd, ABRTBITPTR, MK_MESSAGE_BIT_OFFSET);
6546 ahd_outb(ahd, ABRTBYTEPTR, offsetof(struct hardware_scb, control));
6547 if ((ahd->bugs & AHD_PKT_LUN_BUG) != 0) {
6548 ahd_outb(ahd, LUNLEN,
6549 sizeof(ahd->next_queued_hscb->pkt_long_lun) - 1);
6551 ahd_outb(ahd, LUNLEN, LUNLEN_SINGLE_LEVEL_LUN);
6553 ahd_outb(ahd, CDBLIMIT, SCB_CDB_LEN_PTR - 1);
6554 ahd_outb(ahd, MAXCMD, 0xFF);
6555 ahd_outb(ahd, SCBAUTOPTR,
6556 AUSCBPTR_EN | offsetof(struct hardware_scb, tag));
6558 /* We haven't been enabled for target mode yet. */
6559 ahd_outb(ahd, MULTARGID, 0);
6560 ahd_outb(ahd, MULTARGID + 1, 0);
6562 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
6563 /* Initialize the negotiation table. */
6564 if ((ahd->features & AHD_NEW_IOCELL_OPTS) == 0) {
6566 * Clear the spare bytes in the neg table to avoid
6567 * spurious parity errors.
6569 for (target = 0; target < AHD_NUM_TARGETS; target++) {
6570 ahd_outb(ahd, NEGOADDR, target);
6571 ahd_outb(ahd, ANNEXCOL, AHD_ANNEXCOL_PER_DEV0);
6572 for (i = 0; i < AHD_NUM_PER_DEV_ANNEXCOLS; i++)
6573 ahd_outb(ahd, ANNEXDAT, 0);
6576 for (target = 0; target < AHD_NUM_TARGETS; target++) {
6577 struct ahd_devinfo devinfo;
6578 struct ahd_initiator_tinfo *tinfo;
6579 struct ahd_tmode_tstate *tstate;
6581 tinfo = ahd_fetch_transinfo(ahd, 'A', ahd->our_id,
6583 ahd_compile_devinfo(&devinfo, ahd->our_id,
6584 target, CAM_LUN_WILDCARD,
6585 'A', ROLE_INITIATOR);
6586 ahd_update_neg_table(ahd, &devinfo, &tinfo->curr);
6589 ahd_outb(ahd, CLRSINT3, NTRAMPERR|OSRAMPERR);
6590 ahd_outb(ahd, CLRINT, CLRSCSIINT);
6592 #if NEEDS_MORE_TESTING
6594 * Always enable abort on incoming L_Qs if this feature is
6595 * supported. We use this to catch invalid SCB references.
6597 if ((ahd->bugs & AHD_ABORT_LQI_BUG) == 0)
6598 ahd_outb(ahd, LQCTL1, ABORTPENDING);
6601 ahd_outb(ahd, LQCTL1, 0);
6603 /* All of our queues are empty */
6604 ahd->qoutfifonext = 0;
6605 ahd->qoutfifonext_valid_tag = QOUTFIFO_ENTRY_VALID;
6606 ahd_outb(ahd, QOUTFIFO_ENTRY_VALID_TAG, QOUTFIFO_ENTRY_VALID);
6607 for (i = 0; i < AHD_QOUT_SIZE; i++)
6608 ahd->qoutfifo[i].valid_tag = 0;
6609 ahd_sync_qoutfifo(ahd, BUS_DMASYNC_PREREAD);
6611 ahd->qinfifonext = 0;
6612 for (i = 0; i < AHD_QIN_SIZE; i++)
6613 ahd->qinfifo[i] = SCB_LIST_NULL;
6615 if ((ahd->features & AHD_TARGETMODE) != 0) {
6616 /* All target command blocks start out invalid. */
6617 for (i = 0; i < AHD_TMODE_CMDS; i++)
6618 ahd->targetcmds[i].cmd_valid = 0;
6619 ahd_sync_tqinfifo(ahd, BUS_DMASYNC_PREREAD);
6620 ahd->tqinfifonext = 1;
6621 ahd_outb(ahd, KERNEL_TQINPOS, ahd->tqinfifonext - 1);
6622 ahd_outb(ahd, TQINPOS, ahd->tqinfifonext);
6625 /* Initialize Scratch Ram. */
6626 ahd_outb(ahd, SEQ_FLAGS, 0);
6627 ahd_outb(ahd, SEQ_FLAGS2, 0);
6629 /* We don't have any waiting selections */
6630 ahd_outw(ahd, WAITING_TID_HEAD, SCB_LIST_NULL);
6631 ahd_outw(ahd, WAITING_TID_TAIL, SCB_LIST_NULL);
6632 for (i = 0; i < AHD_NUM_TARGETS; i++)
6633 ahd_outw(ahd, WAITING_SCB_TAILS + (2 * i), SCB_LIST_NULL);
6636 * Nobody is waiting to be DMAed into the QOUTFIFO.
6638 ahd_outw(ahd, COMPLETE_SCB_HEAD, SCB_LIST_NULL);
6639 ahd_outw(ahd, COMPLETE_SCB_DMAINPROG_HEAD, SCB_LIST_NULL);
6640 ahd_outw(ahd, COMPLETE_DMA_SCB_HEAD, SCB_LIST_NULL);
6641 ahd_outw(ahd, COMPLETE_DMA_SCB_TAIL, SCB_LIST_NULL);
6642 ahd_outw(ahd, COMPLETE_ON_QFREEZE_HEAD, SCB_LIST_NULL);
6645 * The Freeze Count is 0.
6647 ahd->qfreeze_cnt = 0;
6648 ahd_outw(ahd, QFREEZE_COUNT, 0);
6649 ahd_outw(ahd, KERNEL_QFREEZE_COUNT, 0);
6652 * Tell the sequencer where it can find our arrays in memory.
6654 busaddr = ahd->shared_data_map.busaddr;
6655 ahd_outl(ahd, SHARED_DATA_ADDR, busaddr);
6656 ahd_outl(ahd, QOUTFIFO_NEXT_ADDR, busaddr);
6659 * Setup the allowed SCSI Sequences based on operational mode.
6660 * If we are a target, we'll enable select in operations once
6661 * we've had a lun enabled.
6663 scsiseq_template = ENAUTOATNP;
6664 if ((ahd->flags & AHD_INITIATORROLE) != 0)
6665 scsiseq_template |= ENRSELI;
6666 ahd_outb(ahd, SCSISEQ_TEMPLATE, scsiseq_template);
6668 /* There are no busy SCBs yet. */
6669 for (target = 0; target < AHD_NUM_TARGETS; target++) {
6672 for (lun = 0; lun < AHD_NUM_LUNS_NONPKT; lun++)
6673 ahd_unbusy_tcl(ahd, BUILD_TCL_RAW(target, 'A', lun));
6677 * Initialize the group code to command length table.
6678 * Vendor Unique codes are set to 0 so we only capture
6679 * the first byte of the cdb. These can be overridden
6680 * when target mode is enabled.
6682 ahd_outb(ahd, CMDSIZE_TABLE, 5);
6683 ahd_outb(ahd, CMDSIZE_TABLE + 1, 9);
6684 ahd_outb(ahd, CMDSIZE_TABLE + 2, 9);
6685 ahd_outb(ahd, CMDSIZE_TABLE + 3, 0);
6686 ahd_outb(ahd, CMDSIZE_TABLE + 4, 15);
6687 ahd_outb(ahd, CMDSIZE_TABLE + 5, 11);
6688 ahd_outb(ahd, CMDSIZE_TABLE + 6, 0);
6689 ahd_outb(ahd, CMDSIZE_TABLE + 7, 0);
6691 /* Tell the sequencer of our initial queue positions */
6692 ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
6693 ahd_outb(ahd, QOFF_CTLSTA, SCB_QSIZE_512);
6694 ahd->qinfifonext = 0;
6695 ahd_set_hnscb_qoff(ahd, ahd->qinfifonext);
6696 ahd_set_hescb_qoff(ahd, 0);
6697 ahd_set_snscb_qoff(ahd, 0);
6698 ahd_set_sescb_qoff(ahd, 0);
6699 ahd_set_sdscb_qoff(ahd, 0);
6702 * Tell the sequencer which SCB will be the next one it receives.
6704 busaddr = aic_le32toh(ahd->next_queued_hscb->hscb_busaddr);
6705 ahd_outl(ahd, NEXT_QUEUED_SCB_ADDR, busaddr);
6708 * Default to coalescing disabled.
6710 ahd_outw(ahd, INT_COALESCING_CMDCOUNT, 0);
6711 ahd_outw(ahd, CMDS_PENDING, 0);
6712 ahd_update_coalescing_values(ahd, ahd->int_coalescing_timer,
6713 ahd->int_coalescing_maxcmds,
6714 ahd->int_coalescing_mincmds);
6715 ahd_enable_coalescing(ahd, FALSE);
6718 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
6722 * Setup default device and controller settings.
6723 * This should only be called if our probe has
6724 * determined that no configuration data is available.
6727 ahd_default_config(struct ahd_softc *ahd)
6734 * Allocate a tstate to house information for our
6735 * initiator presence on the bus as well as the user
6736 * data for any target mode initiator.
6738 if (ahd_alloc_tstate(ahd, ahd->our_id, 'A') == NULL) {
6739 kprintf("%s: unable to allocate ahd_tmode_tstate. "
6740 "Failing attach\n", ahd_name(ahd));
6744 for (targ = 0; targ < AHD_NUM_TARGETS; targ++) {
6745 struct ahd_devinfo devinfo;
6746 struct ahd_initiator_tinfo *tinfo;
6747 struct ahd_tmode_tstate *tstate;
6748 uint16_t target_mask;
6750 tinfo = ahd_fetch_transinfo(ahd, 'A', ahd->our_id,
6753 * We support SPC2 and SPI4.
6755 tinfo->user.protocol_version = 4;
6756 tinfo->user.transport_version = 4;
6758 target_mask = 0x01 << targ;
6759 ahd->user_discenable |= target_mask;
6760 tstate->discenable |= target_mask;
6761 ahd->user_tagenable |= target_mask;
6762 #ifdef AHD_FORCE_160
6763 tinfo->user.period = AHD_SYNCRATE_DT;
6765 tinfo->user.period = AHD_SYNCRATE_160;
6767 tinfo->user.offset = MAX_OFFSET;
6768 tinfo->user.ppr_options = MSG_EXT_PPR_RD_STRM
6769 | MSG_EXT_PPR_WR_FLOW
6770 | MSG_EXT_PPR_HOLD_MCS
6771 | MSG_EXT_PPR_IU_REQ
6772 | MSG_EXT_PPR_QAS_REQ
6773 | MSG_EXT_PPR_DT_REQ;
6774 if ((ahd->features & AHD_RTI) != 0)
6775 tinfo->user.ppr_options |= MSG_EXT_PPR_RTI;
6777 tinfo->user.width = MSG_EXT_WDTR_BUS_16_BIT;
6780 * Start out Async/Narrow/Untagged and with
6781 * conservative protocol support.
6783 tinfo->goal.protocol_version = 2;
6784 tinfo->goal.transport_version = 2;
6785 tinfo->curr.protocol_version = 2;
6786 tinfo->curr.transport_version = 2;
6787 ahd_compile_devinfo(&devinfo, ahd->our_id,
6788 targ, CAM_LUN_WILDCARD,
6789 'A', ROLE_INITIATOR);
6790 tstate->tagenable &= ~target_mask;
6791 ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
6792 AHD_TRANS_CUR|AHD_TRANS_GOAL, /*paused*/TRUE);
6793 ahd_set_syncrate(ahd, &devinfo, /*period*/0, /*offset*/0,
6794 /*ppr_options*/0, AHD_TRANS_CUR|AHD_TRANS_GOAL,
6801 * Parse device configuration information.
6804 ahd_parse_cfgdata(struct ahd_softc *ahd, struct seeprom_config *sc)
6809 max_targ = sc->max_targets & CFMAXTARG;
6810 ahd->our_id = sc->brtime_id & CFSCSIID;
6813 * Allocate a tstate to house information for our
6814 * initiator presence on the bus as well as the user
6815 * data for any target mode initiator.
6817 if (ahd_alloc_tstate(ahd, ahd->our_id, 'A') == NULL) {
6818 kprintf("%s: unable to allocate ahd_tmode_tstate. "
6819 "Failing attach\n", ahd_name(ahd));
6823 for (targ = 0; targ < max_targ; targ++) {
6824 struct ahd_devinfo devinfo;
6825 struct ahd_initiator_tinfo *tinfo;
6826 struct ahd_transinfo *user_tinfo;
6827 struct ahd_tmode_tstate *tstate;
6828 uint16_t target_mask;
6830 tinfo = ahd_fetch_transinfo(ahd, 'A', ahd->our_id,
6832 user_tinfo = &tinfo->user;
6835 * We support SPC2 and SPI4.
6837 tinfo->user.protocol_version = 4;
6838 tinfo->user.transport_version = 4;
6840 target_mask = 0x01 << targ;
6841 ahd->user_discenable &= ~target_mask;
6842 tstate->discenable &= ~target_mask;
6843 ahd->user_tagenable &= ~target_mask;
6844 if (sc->device_flags[targ] & CFDISC) {
6845 tstate->discenable |= target_mask;
6846 ahd->user_discenable |= target_mask;
6847 ahd->user_tagenable |= target_mask;
6850 * Cannot be packetized without disconnection.
6852 sc->device_flags[targ] &= ~CFPACKETIZED;
6855 user_tinfo->ppr_options = 0;
6856 user_tinfo->period = (sc->device_flags[targ] & CFXFER);
6857 if (user_tinfo->period < CFXFER_ASYNC) {
6858 if (user_tinfo->period <= AHD_PERIOD_10MHz)
6859 user_tinfo->ppr_options |= MSG_EXT_PPR_DT_REQ;
6860 user_tinfo->offset = MAX_OFFSET;
6862 user_tinfo->offset = 0;
6863 user_tinfo->period = AHD_ASYNC_XFER_PERIOD;
6865 #ifdef AHD_FORCE_160
6866 if (user_tinfo->period <= AHD_SYNCRATE_160)
6867 user_tinfo->period = AHD_SYNCRATE_DT;
6870 if ((sc->device_flags[targ] & CFPACKETIZED) != 0) {
6871 user_tinfo->ppr_options |= MSG_EXT_PPR_RD_STRM
6872 | MSG_EXT_PPR_WR_FLOW
6873 | MSG_EXT_PPR_HOLD_MCS
6874 | MSG_EXT_PPR_IU_REQ;
6875 if ((ahd->features & AHD_RTI) != 0)
6876 user_tinfo->ppr_options |= MSG_EXT_PPR_RTI;
6879 if ((sc->device_flags[targ] & CFQAS) != 0)
6880 user_tinfo->ppr_options |= MSG_EXT_PPR_QAS_REQ;
6882 if ((sc->device_flags[targ] & CFWIDEB) != 0)
6883 user_tinfo->width = MSG_EXT_WDTR_BUS_16_BIT;
6885 user_tinfo->width = MSG_EXT_WDTR_BUS_8_BIT;
6887 if ((ahd_debug & AHD_SHOW_MISC) != 0)
6888 kprintf("(%d): %x:%x:%x:%x\n", targ, user_tinfo->width,
6889 user_tinfo->period, user_tinfo->offset,
6890 user_tinfo->ppr_options);
6893 * Start out Async/Narrow/Untagged and with
6894 * conservative protocol support.
6896 tstate->tagenable &= ~target_mask;
6897 tinfo->goal.protocol_version = 2;
6898 tinfo->goal.transport_version = 2;
6899 tinfo->curr.protocol_version = 2;
6900 tinfo->curr.transport_version = 2;
6901 ahd_compile_devinfo(&devinfo, ahd->our_id,
6902 targ, CAM_LUN_WILDCARD,
6903 'A', ROLE_INITIATOR);
6904 ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
6905 AHD_TRANS_CUR|AHD_TRANS_GOAL, /*paused*/TRUE);
6906 ahd_set_syncrate(ahd, &devinfo, /*period*/0, /*offset*/0,
6907 /*ppr_options*/0, AHD_TRANS_CUR|AHD_TRANS_GOAL,
6911 ahd->flags &= ~AHD_SPCHK_ENB_A;
6912 if (sc->bios_control & CFSPARITY)
6913 ahd->flags |= AHD_SPCHK_ENB_A;
6915 ahd->flags &= ~AHD_RESET_BUS_A;
6916 if (sc->bios_control & CFRESETB)
6917 ahd->flags |= AHD_RESET_BUS_A;
6919 ahd->flags &= ~AHD_EXTENDED_TRANS_A;
6920 if (sc->bios_control & CFEXTEND)
6921 ahd->flags |= AHD_EXTENDED_TRANS_A;
6923 ahd->flags &= ~AHD_BIOS_ENABLED;
6924 if ((sc->bios_control & CFBIOSSTATE) == CFBS_ENABLED)
6925 ahd->flags |= AHD_BIOS_ENABLED;
6927 ahd->flags &= ~AHD_STPWLEVEL_A;
6928 if ((sc->adapter_control & CFSTPWLEVEL) != 0)
6929 ahd->flags |= AHD_STPWLEVEL_A;
6935 * Parse device configuration information.
6938 ahd_parse_vpddata(struct ahd_softc *ahd, struct vpd_config *vpd)
6942 error = ahd_verify_vpd_cksum(vpd);
6945 if ((vpd->bios_flags & VPDBOOTHOST) != 0)
6946 ahd->flags |= AHD_BOOT_CHANNEL;
6951 ahd_intr_enable(struct ahd_softc *ahd, int enable)
6955 hcntrl = ahd_inb(ahd, HCNTRL);
6957 ahd->pause &= ~INTEN;
6958 ahd->unpause &= ~INTEN;
6961 ahd->pause |= INTEN;
6962 ahd->unpause |= INTEN;
6964 ahd_outb(ahd, HCNTRL, hcntrl);
6968 ahd_update_coalescing_values(struct ahd_softc *ahd, u_int timer, u_int maxcmds,
6971 if (timer > AHD_TIMER_MAX_US)
6972 timer = AHD_TIMER_MAX_US;
6973 ahd->int_coalescing_timer = timer;
6975 if (maxcmds > AHD_INT_COALESCING_MAXCMDS_MAX)
6976 maxcmds = AHD_INT_COALESCING_MAXCMDS_MAX;
6977 if (mincmds > AHD_INT_COALESCING_MINCMDS_MAX)
6978 mincmds = AHD_INT_COALESCING_MINCMDS_MAX;
6979 ahd->int_coalescing_maxcmds = maxcmds;
6980 ahd_outw(ahd, INT_COALESCING_TIMER, timer / AHD_TIMER_US_PER_TICK);
6981 ahd_outb(ahd, INT_COALESCING_MAXCMDS, -maxcmds);
6982 ahd_outb(ahd, INT_COALESCING_MINCMDS, -mincmds);
6986 ahd_enable_coalescing(struct ahd_softc *ahd, int enable)
6989 ahd->hs_mailbox &= ~ENINT_COALESCE;
6991 ahd->hs_mailbox |= ENINT_COALESCE;
6992 ahd_outb(ahd, HS_MAILBOX, ahd->hs_mailbox);
6993 ahd_flush_device_writes(ahd);
6994 ahd_run_qoutfifo(ahd);
6998 * Ensure that the card is paused in a location
6999 * outside of all critical sections and that all
7000 * pending work is completed prior to returning.
7001 * This routine should only be called from outside
7002 * an interrupt context.
7005 ahd_pause_and_flushwork(struct ahd_softc *ahd)
7011 ahd->flags |= AHD_ALL_INTERRUPTS;
7014 * Freeze the outgoing selections. We do this only
7015 * until we are safely paused without further selections
7019 ahd_outw(ahd, KERNEL_QFREEZE_COUNT, ahd->qfreeze_cnt);
7020 ahd_outb(ahd, SEQ_FLAGS2, ahd_inb(ahd, SEQ_FLAGS2) | SELECTOUT_QFROZEN);
7022 struct scb *waiting_scb;
7026 * Give the sequencer some time to service
7027 * any active selections.
7033 ahd_clear_critical_section(ahd);
7034 intstat = ahd_inb(ahd, INTSTAT);
7035 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
7036 if ((ahd_inb(ahd, SSTAT0) & (SELDO|SELINGO)) == 0)
7037 ahd_outb(ahd, SCSISEQ0,
7038 ahd_inb(ahd, SCSISEQ0) & ~ENSELO);
7040 * In the non-packetized case, the sequencer (for Rev A),
7041 * relies on ENSELO remaining set after SELDO. The hardware
7042 * auto-clears ENSELO in the packetized case.
7044 waiting_scb = ahd_lookup_scb(ahd,
7045 ahd_inw(ahd, WAITING_TID_HEAD));
7046 if (waiting_scb != NULL
7047 && (waiting_scb->flags & SCB_PACKETIZED) == 0
7048 && (ahd_inb(ahd, SSTAT0) & (SELDO|SELINGO)) != 0)
7049 ahd_outb(ahd, SCSISEQ0,
7050 ahd_inb(ahd, SCSISEQ0) | ENSELO);
7052 && (intstat != 0xFF || (ahd->features & AHD_REMOVABLE) == 0)
7053 && ((intstat & INT_PEND) != 0
7054 || (ahd_inb(ahd, SCSISEQ0) & ENSELO) != 0
7055 || (ahd_inb(ahd, SSTAT0) & (SELDO|SELINGO)) != 0));
7057 if (maxloops == 0) {
7058 kprintf("Infinite interrupt loop, INTSTAT = %x",
7059 ahd_inb(ahd, INTSTAT));
7062 ahd_outw(ahd, KERNEL_QFREEZE_COUNT, ahd->qfreeze_cnt);
7064 ahd_flush_qoutfifo(ahd);
7066 ahd_platform_flushwork(ahd);
7067 ahd->flags &= ~AHD_ALL_INTERRUPTS;
7071 ahd_suspend(struct ahd_softc *ahd)
7074 ahd_pause_and_flushwork(ahd);
7076 if (LIST_FIRST(&ahd->pending_scbs) != NULL) {
7085 ahd_resume(struct ahd_softc *ahd)
7088 ahd_reset(ahd, /*reinit*/TRUE);
7089 ahd_intr_enable(ahd, TRUE);
7094 /************************** Busy Target Table *********************************/
7096 * Set SCBPTR to the SCB that contains the busy
7097 * table entry for TCL. Return the offset into
7098 * the SCB that contains the entry for TCL.
7099 * saved_scbid is dereferenced and set to the
7100 * scbid that should be restored once manipualtion
7101 * of the TCL entry is complete.
7103 static __inline u_int
7104 ahd_index_busy_tcl(struct ahd_softc *ahd, u_int *saved_scbid, u_int tcl)
7107 * Index to the SCB that contains the busy entry.
7109 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
7110 *saved_scbid = ahd_get_scbptr(ahd);
7111 ahd_set_scbptr(ahd, TCL_LUN(tcl)
7112 | ((TCL_TARGET_OFFSET(tcl) & 0xC) << 4));
7115 * And now calculate the SCB offset to the entry.
7116 * Each entry is 2 bytes wide, hence the
7117 * multiplication by 2.
7119 return (((TCL_TARGET_OFFSET(tcl) & 0x3) << 1) + SCB_DISCONNECTED_LISTS);
7123 * Return the untagged transaction id for a given target/channel lun.
7126 ahd_find_busy_tcl(struct ahd_softc *ahd, u_int tcl)
7132 scb_offset = ahd_index_busy_tcl(ahd, &saved_scbptr, tcl);
7133 scbid = ahd_inw_scbram(ahd, scb_offset);
7134 ahd_set_scbptr(ahd, saved_scbptr);
7139 ahd_busy_tcl(struct ahd_softc *ahd, u_int tcl, u_int scbid)
7144 scb_offset = ahd_index_busy_tcl(ahd, &saved_scbptr, tcl);
7145 ahd_outw(ahd, scb_offset, scbid);
7146 ahd_set_scbptr(ahd, saved_scbptr);
7149 /************************** SCB and SCB queue management **********************/
7151 ahd_match_scb(struct ahd_softc *ahd, struct scb *scb, int target,
7152 char channel, int lun, u_int tag, role_t role)
7154 int targ = SCB_GET_TARGET(ahd, scb);
7155 char chan = SCB_GET_CHANNEL(ahd, scb);
7156 int slun = SCB_GET_LUN(scb);
7159 match = ((chan == channel) || (channel == ALL_CHANNELS));
7161 match = ((targ == target) || (target == CAM_TARGET_WILDCARD));
7163 match = ((lun == slun) || (lun == CAM_LUN_WILDCARD));
7168 group = XPT_FC_GROUP(scb->io_ctx->ccb_h.func_code);
7169 if (role == ROLE_INITIATOR) {
7170 match = (group != XPT_FC_GROUP_TMODE)
7171 && ((tag == SCB_GET_TAG(scb))
7172 || (tag == SCB_LIST_NULL));
7173 } else if (role == ROLE_TARGET) {
7174 match = (group == XPT_FC_GROUP_TMODE)
7175 && ((tag == scb->io_ctx->csio.tag_id)
7176 || (tag == SCB_LIST_NULL));
7178 #else /* !AHD_TARGET_MODE */
7179 match = ((tag == SCB_GET_TAG(scb)) || (tag == SCB_LIST_NULL));
7180 #endif /* AHD_TARGET_MODE */
7187 ahd_freeze_devq(struct ahd_softc *ahd, struct scb *scb)
7193 target = SCB_GET_TARGET(ahd, scb);
7194 lun = SCB_GET_LUN(scb);
7195 channel = SCB_GET_CHANNEL(ahd, scb);
7197 ahd_search_qinfifo(ahd, target, channel, lun,
7198 /*tag*/SCB_LIST_NULL, ROLE_UNKNOWN,
7199 CAM_REQUEUE_REQ, SEARCH_COMPLETE);
7201 ahd_platform_freeze_devq(ahd, scb);
7205 ahd_qinfifo_requeue_tail(struct ahd_softc *ahd, struct scb *scb)
7207 struct scb *prev_scb;
7208 ahd_mode_state saved_modes;
7210 saved_modes = ahd_save_modes(ahd);
7211 ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
7213 if (ahd_qinfifo_count(ahd) != 0) {
7217 prev_pos = AHD_QIN_WRAP(ahd->qinfifonext - 1);
7218 prev_tag = ahd->qinfifo[prev_pos];
7219 prev_scb = ahd_lookup_scb(ahd, prev_tag);
7221 ahd_qinfifo_requeue(ahd, prev_scb, scb);
7222 ahd_set_hnscb_qoff(ahd, ahd->qinfifonext);
7223 ahd_restore_modes(ahd, saved_modes);
7227 ahd_qinfifo_requeue(struct ahd_softc *ahd, struct scb *prev_scb,
7230 if (prev_scb == NULL) {
7233 busaddr = aic_le32toh(scb->hscb->hscb_busaddr);
7234 ahd_outl(ahd, NEXT_QUEUED_SCB_ADDR, busaddr);
7236 prev_scb->hscb->next_hscb_busaddr = scb->hscb->hscb_busaddr;
7237 ahd_sync_scb(ahd, prev_scb,
7238 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
7240 ahd->qinfifo[AHD_QIN_WRAP(ahd->qinfifonext)] = SCB_GET_TAG(scb);
7242 scb->hscb->next_hscb_busaddr = ahd->next_queued_hscb->hscb_busaddr;
7243 ahd_sync_scb(ahd, scb, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
7247 ahd_qinfifo_count(struct ahd_softc *ahd)
7251 u_int wrap_qinfifonext;
7253 AHD_ASSERT_MODES(ahd, AHD_MODE_CCHAN_MSK, AHD_MODE_CCHAN_MSK);
7254 qinpos = ahd_get_snscb_qoff(ahd);
7255 wrap_qinpos = AHD_QIN_WRAP(qinpos);
7256 wrap_qinfifonext = AHD_QIN_WRAP(ahd->qinfifonext);
7257 if (wrap_qinfifonext >= wrap_qinpos)
7258 return (wrap_qinfifonext - wrap_qinpos);
7260 return (wrap_qinfifonext
7261 + NUM_ELEMENTS(ahd->qinfifo) - wrap_qinpos);
7265 ahd_reset_cmds_pending(struct ahd_softc *ahd)
7268 ahd_mode_state saved_modes;
7271 saved_modes = ahd_save_modes(ahd);
7272 ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
7275 * Don't count any commands as outstanding that the
7276 * sequencer has already marked for completion.
7278 ahd_flush_qoutfifo(ahd);
7281 LIST_FOREACH(scb, &ahd->pending_scbs, pending_links) {
7284 ahd_outw(ahd, CMDS_PENDING, pending_cmds - ahd_qinfifo_count(ahd));
7285 ahd_restore_modes(ahd, saved_modes);
7286 ahd->flags &= ~AHD_UPDATE_PEND_CMDS;
7290 ahd_search_qinfifo(struct ahd_softc *ahd, int target, char channel,
7291 int lun, u_int tag, role_t role, uint32_t status,
7292 ahd_search_action action)
7295 struct scb *prev_scb;
7296 ahd_mode_state saved_modes;
7308 /* Must be in CCHAN mode */
7309 saved_modes = ahd_save_modes(ahd);
7310 ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
7313 * Halt any pending SCB DMA. The sequencer will reinitiate
7314 * this dma if the qinfifo is not empty once we unpause.
7316 if ((ahd_inb(ahd, CCSCBCTL) & (CCARREN|CCSCBEN|CCSCBDIR))
7317 == (CCARREN|CCSCBEN|CCSCBDIR)) {
7318 ahd_outb(ahd, CCSCBCTL,
7319 ahd_inb(ahd, CCSCBCTL) & ~(CCARREN|CCSCBEN));
7320 while ((ahd_inb(ahd, CCSCBCTL) & (CCARREN|CCSCBEN)) != 0)
7323 /* Determine sequencer's position in the qinfifo. */
7324 qintail = AHD_QIN_WRAP(ahd->qinfifonext);
7325 qinstart = ahd_get_snscb_qoff(ahd);
7326 qinpos = AHD_QIN_WRAP(qinstart);
7330 if (action == SEARCH_PRINT) {
7331 kprintf("qinstart = %d qinfifonext = %d\nQINFIFO:",
7332 qinstart, ahd->qinfifonext);
7336 * Start with an empty queue. Entries that are not chosen
7337 * for removal will be re-added to the queue as we go.
7339 ahd->qinfifonext = qinstart;
7340 busaddr = aic_le32toh(ahd->next_queued_hscb->hscb_busaddr);
7341 ahd_outl(ahd, NEXT_QUEUED_SCB_ADDR, busaddr);
7343 while (qinpos != qintail) {
7344 scb = ahd_lookup_scb(ahd, ahd->qinfifo[qinpos]);
7346 kprintf("qinpos = %d, SCB index = %d\n",
7347 qinpos, ahd->qinfifo[qinpos]);
7351 if (ahd_match_scb(ahd, scb, target, channel, lun, tag, role)) {
7353 * We found an scb that needs to be acted on.
7357 case SEARCH_COMPLETE:
7362 ostat = aic_get_transaction_status(scb);
7363 if (ostat == CAM_REQ_INPROG)
7364 aic_set_transaction_status(scb,
7366 cstat = aic_get_transaction_status(scb);
7367 if (cstat != CAM_REQ_CMP)
7368 aic_freeze_scb(scb);
7369 if ((scb->flags & SCB_ACTIVE) == 0)
7370 kprintf("Inactive SCB in qinfifo\n");
7378 kprintf(" 0x%x", ahd->qinfifo[qinpos]);
7381 ahd_qinfifo_requeue(ahd, prev_scb, scb);
7386 ahd_qinfifo_requeue(ahd, prev_scb, scb);
7389 qinpos = AHD_QIN_WRAP(qinpos+1);
7392 ahd_set_hnscb_qoff(ahd, ahd->qinfifonext);
7394 if (action == SEARCH_PRINT)
7395 kprintf("\nWAITING_TID_QUEUES:\n");
7398 * Search waiting for selection lists. We traverse the
7399 * list of "their ids" waiting for selection and, if
7400 * appropriate, traverse the SCBs of each "their id"
7401 * looking for matches.
7403 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
7404 savedscbptr = ahd_get_scbptr(ahd);
7405 tid_next = ahd_inw(ahd, WAITING_TID_HEAD);
7406 tid_prev = SCB_LIST_NULL;
7408 for (scbid = tid_next; !SCBID_IS_NULL(scbid); scbid = tid_next) {
7412 * We limit based on the number of SCBs since
7413 * MK_MESSAGE SCBs are not in the per-tid lists.
7416 if (targets > AHD_SCB_MAX) {
7417 panic("TID LIST LOOP");
7419 if (scbid >= ahd->scb_data.numscbs) {
7420 kprintf("%s: Waiting TID List inconsistency. "
7421 "SCB index == 0x%x, yet numscbs == 0x%x.",
7422 ahd_name(ahd), scbid, ahd->scb_data.numscbs);
7423 ahd_dump_card_state(ahd);
7424 panic("for safety");
7426 scb = ahd_lookup_scb(ahd, scbid);
7428 kprintf("%s: SCB = 0x%x Not Active!\n",
7429 ahd_name(ahd), scbid);
7430 panic("Waiting TID List traversal\n");
7432 ahd_set_scbptr(ahd, scbid);
7433 tid_next = ahd_inw_scbram(ahd, SCB_NEXT2);
7434 if (ahd_match_scb(ahd, scb, target, channel, CAM_LUN_WILDCARD,
7435 SCB_LIST_NULL, ROLE_UNKNOWN) == 0) {
7441 * We found a list of scbs that needs to be searched.
7443 if (action == SEARCH_PRINT)
7444 kprintf(" %d ( ", SCB_GET_TARGET(ahd, scb));
7446 found += ahd_search_scb_list(ahd, target, channel,
7447 lun, tag, role, status,
7449 SCB_GET_TARGET(ahd, scb));
7450 if (tid_head != scbid)
7451 ahd_stitch_tid_list(ahd, tid_prev, tid_head, tid_next);
7452 if (!SCBID_IS_NULL(tid_head))
7453 tid_prev = tid_head;
7454 if (action == SEARCH_PRINT)
7457 ahd_set_scbptr(ahd, savedscbptr);
7458 ahd_restore_modes(ahd, saved_modes);
7463 ahd_search_scb_list(struct ahd_softc *ahd, int target, char channel,
7464 int lun, u_int tag, role_t role, uint32_t status,
7465 ahd_search_action action, u_int *list_head, u_int tid)
7473 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
7475 prev = SCB_LIST_NULL;
7477 for (scbid = next; !SCBID_IS_NULL(scbid); scbid = next) {
7478 if (scbid >= ahd->scb_data.numscbs) {
7479 kprintf("%s:SCB List inconsistency. "
7480 "SCB == 0x%x, yet numscbs == 0x%x.",
7481 ahd_name(ahd), scbid, ahd->scb_data.numscbs);
7482 ahd_dump_card_state(ahd);
7483 panic("for safety");
7485 scb = ahd_lookup_scb(ahd, scbid);
7487 kprintf("%s: SCB = %d Not Active!\n",
7488 ahd_name(ahd), scbid);
7489 panic("Waiting List traversal\n");
7491 ahd_set_scbptr(ahd, scbid);
7492 next = ahd_inw_scbram(ahd, SCB_NEXT);
7493 if (ahd_match_scb(ahd, scb, target, channel,
7494 lun, SCB_LIST_NULL, role) == 0) {
7500 case SEARCH_COMPLETE:
7505 ostat = aic_get_transaction_status(scb);
7506 if (ostat == CAM_REQ_INPROG)
7507 aic_set_transaction_status(scb, status);
7508 cstat = aic_get_transaction_status(scb);
7509 if (cstat != CAM_REQ_CMP)
7510 aic_freeze_scb(scb);
7511 if ((scb->flags & SCB_ACTIVE) == 0)
7512 kprintf("Inactive SCB in Waiting List\n");
7517 ahd_rem_wscb(ahd, scbid, prev, next, tid);
7518 if (prev == SCB_LIST_NULL)
7522 kprintf("0x%x ", scbid);
7527 if (found > AHD_SCB_MAX)
7528 panic("SCB LIST LOOP");
7530 if (action == SEARCH_COMPLETE
7531 || action == SEARCH_REMOVE)
7532 ahd_outw(ahd, CMDS_PENDING, ahd_inw(ahd, CMDS_PENDING) - found);
7537 ahd_stitch_tid_list(struct ahd_softc *ahd, u_int tid_prev,
7538 u_int tid_cur, u_int tid_next)
7540 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
7542 if (SCBID_IS_NULL(tid_cur)) {
7544 /* Bypass current TID list */
7545 if (SCBID_IS_NULL(tid_prev)) {
7546 ahd_outw(ahd, WAITING_TID_HEAD, tid_next);
7548 ahd_set_scbptr(ahd, tid_prev);
7549 ahd_outw(ahd, SCB_NEXT2, tid_next);
7551 if (SCBID_IS_NULL(tid_next))
7552 ahd_outw(ahd, WAITING_TID_TAIL, tid_prev);
7555 /* Stitch through tid_cur */
7556 if (SCBID_IS_NULL(tid_prev)) {
7557 ahd_outw(ahd, WAITING_TID_HEAD, tid_cur);
7559 ahd_set_scbptr(ahd, tid_prev);
7560 ahd_outw(ahd, SCB_NEXT2, tid_cur);
7562 ahd_set_scbptr(ahd, tid_cur);
7563 ahd_outw(ahd, SCB_NEXT2, tid_next);
7565 if (SCBID_IS_NULL(tid_next))
7566 ahd_outw(ahd, WAITING_TID_TAIL, tid_cur);
7571 * Manipulate the waiting for selection list and return the
7572 * scb that follows the one that we remove.
7575 ahd_rem_wscb(struct ahd_softc *ahd, u_int scbid,
7576 u_int prev, u_int next, u_int tid)
7580 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
7581 if (!SCBID_IS_NULL(prev)) {
7582 ahd_set_scbptr(ahd, prev);
7583 ahd_outw(ahd, SCB_NEXT, next);
7587 * SCBs that had MK_MESSAGE set in them will not
7588 * be queued to the per-target lists, so don't
7589 * blindly clear the tail pointer.
7591 tail_offset = WAITING_SCB_TAILS + (2 * tid);
7592 if (SCBID_IS_NULL(next)
7593 && ahd_inw(ahd, tail_offset) == scbid)
7594 ahd_outw(ahd, tail_offset, prev);
7595 ahd_add_scb_to_free_list(ahd, scbid);
7600 * Add the SCB as selected by SCBPTR onto the on chip list of
7601 * free hardware SCBs. This list is empty/unused if we are not
7602 * performing SCB paging.
7605 ahd_add_scb_to_free_list(struct ahd_softc *ahd, u_int scbid)
7607 /* XXX Need some other mechanism to designate "free". */
7609 * Invalidate the tag so that our abort
7610 * routines don't think it's active.
7611 ahd_outb(ahd, SCB_TAG, SCB_LIST_NULL);
7615 /******************************** Error Handling ******************************/
7617 * Abort all SCBs that match the given description (target/channel/lun/tag),
7618 * setting their status to the passed in status if the status has not already
7619 * been modified from CAM_REQ_INPROG. This routine assumes that the sequencer
7620 * is paused before it is called.
7623 ahd_abort_scbs(struct ahd_softc *ahd, int target, char channel,
7624 int lun, u_int tag, role_t role, uint32_t status)
7627 struct scb *scbp_next;
7633 ahd_mode_state saved_modes;
7635 /* restore this when we're done */
7636 saved_modes = ahd_save_modes(ahd);
7637 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
7639 found = ahd_search_qinfifo(ahd, target, channel, lun, SCB_LIST_NULL,
7640 role, CAM_REQUEUE_REQ, SEARCH_COMPLETE);
7643 * Clean out the busy target table for any untagged commands.
7647 if (target != CAM_TARGET_WILDCARD) {
7654 if (lun == CAM_LUN_WILDCARD) {
7656 maxlun = AHD_NUM_LUNS_NONPKT;
7657 } else if (lun >= AHD_NUM_LUNS_NONPKT) {
7658 minlun = maxlun = 0;
7664 if (role != ROLE_TARGET) {
7665 for (;i < maxtarget; i++) {
7666 for (j = minlun;j < maxlun; j++) {
7670 tcl = BUILD_TCL_RAW(i, 'A', j);
7671 scbid = ahd_find_busy_tcl(ahd, tcl);
7672 scbp = ahd_lookup_scb(ahd, scbid);
7674 || ahd_match_scb(ahd, scbp, target, channel,
7675 lun, tag, role) == 0)
7677 ahd_unbusy_tcl(ahd, BUILD_TCL_RAW(i, 'A', j));
7683 * Don't abort commands that have already completed,
7684 * but haven't quite made it up to the host yet.
7686 ahd_flush_qoutfifo(ahd);
7689 * Go through the pending CCB list and look for
7690 * commands for this target that are still active.
7691 * These are other tagged commands that were
7692 * disconnected when the reset occurred.
7694 scbp_next = LIST_FIRST(&ahd->pending_scbs);
7695 while (scbp_next != NULL) {
7697 scbp_next = LIST_NEXT(scbp, pending_links);
7698 if (ahd_match_scb(ahd, scbp, target, channel, lun, tag, role)) {
7701 ostat = aic_get_transaction_status(scbp);
7702 if (ostat == CAM_REQ_INPROG)
7703 aic_set_transaction_status(scbp, status);
7704 if (aic_get_transaction_status(scbp) != CAM_REQ_CMP)
7705 aic_freeze_scb(scbp);
7706 if ((scbp->flags & SCB_ACTIVE) == 0)
7707 kprintf("Inactive SCB on pending list\n");
7708 ahd_done(ahd, scbp);
7712 ahd_restore_modes(ahd, saved_modes);
7713 ahd_platform_abort_scbs(ahd, target, channel, lun, tag, role, status);
7714 ahd->flags |= AHD_UPDATE_PEND_CMDS;
7719 ahd_reset_current_bus(struct ahd_softc *ahd)
7723 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
7724 ahd_outb(ahd, SIMODE1, ahd_inb(ahd, SIMODE1) & ~ENSCSIRST);
7725 scsiseq = ahd_inb(ahd, SCSISEQ0) & ~(ENSELO|ENARBO|SCSIRSTO);
7726 ahd_outb(ahd, SCSISEQ0, scsiseq | SCSIRSTO);
7727 ahd_flush_device_writes(ahd);
7728 aic_delay(AHD_BUSRESET_DELAY);
7729 /* Turn off the bus reset */
7730 ahd_outb(ahd, SCSISEQ0, scsiseq);
7731 ahd_flush_device_writes(ahd);
7732 aic_delay(AHD_BUSRESET_DELAY);
7733 if ((ahd->bugs & AHD_SCSIRST_BUG) != 0) {
7736 * Certain chip state is not cleared for
7737 * SCSI bus resets that we initiate, so
7738 * we must reset the chip.
7740 ahd_reset(ahd, /*reinit*/TRUE);
7741 ahd_intr_enable(ahd, /*enable*/TRUE);
7742 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
7745 ahd_clear_intstat(ahd);
7749 ahd_reset_channel(struct ahd_softc *ahd, char channel, int initiate_reset)
7751 struct ahd_devinfo devinfo;
7759 ahd->pending_device = NULL;
7761 ahd_compile_devinfo(&devinfo,
7762 CAM_TARGET_WILDCARD,
7763 CAM_TARGET_WILDCARD,
7765 channel, ROLE_UNKNOWN);
7768 /* Make sure the sequencer is in a safe location. */
7769 ahd_clear_critical_section(ahd);
7772 if ((ahd->flags & AHD_TARGETROLE) != 0) {
7773 ahd_run_tqinfifo(ahd, /*paused*/TRUE);
7776 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
7779 * Disable selections so no automatic hardware
7780 * functions will modify chip state.
7782 ahd_outb(ahd, SCSISEQ0, 0);
7783 ahd_outb(ahd, SCSISEQ1, 0);
7786 * Safely shut down our DMA engines. Always start with
7787 * the FIFO that is not currently active (if any are
7788 * actively connected).
7790 next_fifo = fifo = ahd_inb(ahd, DFFSTAT) & CURRFIFO;
7791 if (next_fifo > CURRFIFO_1)
7792 /* If disconneced, arbitrarily start with FIFO1. */
7793 next_fifo = fifo = 0;
7795 next_fifo ^= CURRFIFO_1;
7796 ahd_set_modes(ahd, next_fifo, next_fifo);
7797 ahd_outb(ahd, DFCNTRL,
7798 ahd_inb(ahd, DFCNTRL) & ~(SCSIEN|HDMAEN));
7799 while ((ahd_inb(ahd, DFCNTRL) & HDMAENACK) != 0)
7802 * Set CURRFIFO to the now inactive channel.
7804 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
7805 ahd_outb(ahd, DFFSTAT, next_fifo);
7806 } while (next_fifo != fifo);
7809 * Reset the bus if we are initiating this reset
7811 ahd_clear_msg_state(ahd);
7812 ahd_outb(ahd, SIMODE1,
7813 ahd_inb(ahd, SIMODE1) & ~(ENBUSFREE|ENSCSIRST));
7816 ahd_reset_current_bus(ahd);
7818 ahd_clear_intstat(ahd);
7821 * Clean up all the state information for the
7822 * pending transactions on this bus.
7824 found = ahd_abort_scbs(ahd, CAM_TARGET_WILDCARD, channel,
7825 CAM_LUN_WILDCARD, SCB_LIST_NULL,
7826 ROLE_UNKNOWN, CAM_SCSI_BUS_RESET);
7829 * Cleanup anything left in the FIFOs.
7831 ahd_clear_fifo(ahd, 0);
7832 ahd_clear_fifo(ahd, 1);
7835 * Revert to async/narrow transfers until we renegotiate.
7837 max_scsiid = (ahd->features & AHD_WIDE) ? 15 : 7;
7838 for (target = 0; target <= max_scsiid; target++) {
7840 if (ahd->enabled_targets[target] == NULL)
7842 for (initiator = 0; initiator <= max_scsiid; initiator++) {
7843 struct ahd_devinfo devinfo;
7845 ahd_compile_devinfo(&devinfo, target, initiator,
7848 ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
7849 AHD_TRANS_CUR, /*paused*/TRUE);
7850 ahd_set_syncrate(ahd, &devinfo, /*period*/0,
7851 /*offset*/0, /*ppr_options*/0,
7852 AHD_TRANS_CUR, /*paused*/TRUE);
7856 #ifdef AHD_TARGET_MODE
7857 max_scsiid = (ahd->features & AHD_WIDE) ? 15 : 7;
7860 * Send an immediate notify ccb to all target more peripheral
7861 * drivers affected by this action.
7863 for (target = 0; target <= max_scsiid; target++) {
7864 struct ahd_tmode_tstate* tstate;
7867 tstate = ahd->enabled_targets[target];
7870 for (lun = 0; lun < AHD_NUM_LUNS; lun++) {
7871 struct ahd_tmode_lstate* lstate;
7873 lstate = tstate->enabled_luns[lun];
7877 ahd_queue_lstate_event(ahd, lstate, CAM_TARGET_WILDCARD,
7878 EVENT_TYPE_BUS_RESET, /*arg*/0);
7879 ahd_send_lstate_events(ahd, lstate);
7883 /* Notify the XPT that a bus reset occurred */
7884 ahd_send_async(ahd, devinfo.channel, CAM_TARGET_WILDCARD,
7885 CAM_LUN_WILDCARD, AC_BUS_RESET, NULL);
7888 * Freeze the SIMQ until our poller can determine that
7889 * the bus reset has really gone away. We set the initial
7890 * timer to 0 to have the check performed as soon as possible
7891 * from the timer context.
7893 if ((ahd->flags & AHD_RESET_POLL_ACTIVE) == 0) {
7894 ahd->flags |= AHD_RESET_POLL_ACTIVE;
7895 aic_freeze_simq(ahd);
7896 aic_timer_reset(&ahd->reset_timer, 0, ahd_reset_poll, ahd);
7902 #define AHD_RESET_POLL_US 1000
7904 ahd_reset_poll(void *arg)
7906 struct ahd_softc *ahd;
7909 ahd = ahd_find_softc((struct ahd_softc *)arg);
7911 kprintf("ahd_reset_poll: Instance %p no longer exists\n", arg);
7916 ahd_update_modes(ahd);
7917 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
7918 ahd_outb(ahd, CLRSINT1, CLRSCSIRSTI);
7919 if ((ahd_inb(ahd, SSTAT1) & SCSIRSTI) != 0) {
7920 aic_timer_reset(&ahd->reset_timer, AHD_RESET_POLL_US,
7921 ahd_reset_poll, ahd);
7927 /* Reset is now low. Complete chip reinitialization. */
7928 ahd_outb(ahd, SIMODE1, ahd_inb(ahd, SIMODE1) | ENSCSIRST);
7929 scsiseq1 = ahd_inb(ahd, SCSISEQ_TEMPLATE);
7930 ahd_outb(ahd, SCSISEQ1, scsiseq1 & (ENSELI|ENRSELI|ENAUTOATNP));
7932 ahd->flags &= ~AHD_RESET_POLL_ACTIVE;
7934 aic_release_simq(ahd);
7937 /**************************** Statistics Processing ***************************/
7939 ahd_stat_timer(void *arg)
7941 struct ahd_softc *ahd;
7944 ahd = ahd_find_softc((struct ahd_softc *)arg);
7946 kprintf("ahd_stat_timer: Instance %p no longer exists\n", arg);
7951 enint_coal = ahd->hs_mailbox & ENINT_COALESCE;
7952 if (ahd->cmdcmplt_total > ahd->int_coalescing_threshold)
7953 enint_coal |= ENINT_COALESCE;
7954 else if (ahd->cmdcmplt_total < ahd->int_coalescing_stop_threshold)
7955 enint_coal &= ~ENINT_COALESCE;
7957 if (enint_coal != (ahd->hs_mailbox & ENINT_COALESCE)) {
7958 ahd_enable_coalescing(ahd, enint_coal);
7960 if ((ahd_debug & AHD_SHOW_INT_COALESCING) != 0)
7961 kprintf("%s: Interrupt coalescing "
7962 "now %sabled. Cmds %d\n",
7964 (enint_coal & ENINT_COALESCE) ? "en" : "dis",
7965 ahd->cmdcmplt_total);
7969 ahd->cmdcmplt_bucket = (ahd->cmdcmplt_bucket+1) & (AHD_STAT_BUCKETS-1);
7970 ahd->cmdcmplt_total -= ahd->cmdcmplt_counts[ahd->cmdcmplt_bucket];
7971 ahd->cmdcmplt_counts[ahd->cmdcmplt_bucket] = 0;
7972 aic_timer_reset(&ahd->stat_timer, AHD_STAT_UPDATE_US,
7973 ahd_stat_timer, ahd);
7977 /****************************** Status Processing *****************************/
7979 ahd_handle_scb_status(struct ahd_softc *ahd, struct scb *scb)
7981 if (scb->hscb->shared_data.istatus.scsi_status != 0) {
7982 ahd_handle_scsi_status(ahd, scb);
7984 ahd_calc_residual(ahd, scb);
7990 ahd_handle_scsi_status(struct ahd_softc *ahd, struct scb *scb)
7992 struct hardware_scb *hscb;
7996 * The sequencer freezes its select-out queue
7997 * anytime a SCSI status error occurs. We must
7998 * handle the error and increment our qfreeze count
7999 * to allow the sequencer to continue. We don't
8000 * bother clearing critical sections here since all
8001 * operations are on data structures that the sequencer
8002 * is not touching once the queue is frozen.
8006 if (ahd_is_paused(ahd)) {
8013 /* Freeze the queue until the client sees the error. */
8014 ahd_freeze_devq(ahd, scb);
8015 aic_freeze_scb(scb);
8017 ahd_outw(ahd, KERNEL_QFREEZE_COUNT, ahd->qfreeze_cnt);
8022 /* Don't want to clobber the original sense code */
8023 if ((scb->flags & SCB_SENSE) != 0) {
8025 * Clear the SCB_SENSE Flag and perform
8026 * a normal command completion.
8028 scb->flags &= ~SCB_SENSE;
8029 aic_set_transaction_status(scb, CAM_AUTOSENSE_FAIL);
8033 aic_set_transaction_status(scb, CAM_SCSI_STATUS_ERROR);
8034 aic_set_scsi_status(scb, hscb->shared_data.istatus.scsi_status);
8035 switch (hscb->shared_data.istatus.scsi_status) {
8036 case STATUS_PKT_SENSE:
8038 struct scsi_status_iu_header *siu;
8040 ahd_sync_sense(ahd, scb, BUS_DMASYNC_POSTREAD);
8041 siu = (struct scsi_status_iu_header *)scb->sense_data;
8042 aic_set_scsi_status(scb, siu->status);
8044 if ((ahd_debug & AHD_SHOW_SENSE) != 0) {
8045 ahd_print_path(ahd, scb);
8046 kprintf("SCB 0x%x Received PKT Status of 0x%x\n",
8047 SCB_GET_TAG(scb), siu->status);
8048 kprintf("\tflags = 0x%x, sense len = 0x%x, "
8050 siu->flags, scsi_4btoul(siu->sense_length),
8051 scsi_4btoul(siu->pkt_failures_length));
8054 if ((siu->flags & SIU_RSPVALID) != 0) {
8055 ahd_print_path(ahd, scb);
8056 if (scsi_4btoul(siu->pkt_failures_length) < 4) {
8057 kprintf("Unable to parse pkt_failures\n");
8060 switch (SIU_PKTFAIL_CODE(siu)) {
8062 kprintf("No packet failure found\n");
8064 case SIU_PFC_CIU_FIELDS_INVALID:
8065 kprintf("Invalid Command IU Field\n");
8067 case SIU_PFC_TMF_NOT_SUPPORTED:
8068 kprintf("TMF not supportd\n");
8070 case SIU_PFC_TMF_FAILED:
8071 kprintf("TMF failed\n");
8073 case SIU_PFC_INVALID_TYPE_CODE:
8074 kprintf("Invalid L_Q Type code\n");
8076 case SIU_PFC_ILLEGAL_REQUEST:
8077 kprintf("Illegal request\n");
8082 if (siu->status == SCSI_STATUS_OK)
8083 aic_set_transaction_status(scb,
8086 if ((siu->flags & SIU_SNSVALID) != 0) {
8087 scb->flags |= SCB_PKT_SENSE;
8089 if ((ahd_debug & AHD_SHOW_SENSE) != 0)
8090 kprintf("Sense data available\n");
8096 case SCSI_STATUS_CMD_TERMINATED:
8097 case SCSI_STATUS_CHECK_COND:
8099 struct ahd_devinfo devinfo;
8100 struct ahd_dma_seg *sg;
8101 struct scsi_sense *sc;
8102 struct ahd_initiator_tinfo *targ_info;
8103 struct ahd_tmode_tstate *tstate;
8104 struct ahd_transinfo *tinfo;
8106 if (ahd_debug & AHD_SHOW_SENSE) {
8107 ahd_print_path(ahd, scb);
8108 kprintf("SCB %d: requests Check Status\n",
8113 if (aic_perform_autosense(scb) == 0)
8116 ahd_compile_devinfo(&devinfo, SCB_GET_OUR_ID(scb),
8117 SCB_GET_TARGET(ahd, scb),
8119 SCB_GET_CHANNEL(ahd, scb),
8121 targ_info = ahd_fetch_transinfo(ahd,
8126 tinfo = &targ_info->curr;
8128 sc = (struct scsi_sense *)hscb->shared_data.idata.cdb;
8130 * Save off the residual if there is one.
8132 ahd_update_residual(ahd, scb);
8134 if (ahd_debug & AHD_SHOW_SENSE) {
8135 ahd_print_path(ahd, scb);
8136 kprintf("Sending Sense\n");
8140 sg = ahd_sg_setup(ahd, scb, sg, ahd_get_sense_bufaddr(ahd, scb),
8141 aic_get_sense_bufsize(ahd, scb),
8143 sc->opcode = REQUEST_SENSE;
8145 if (tinfo->protocol_version <= SCSI_REV_2
8146 && SCB_GET_LUN(scb) < 8)
8147 sc->byte2 = SCB_GET_LUN(scb) << 5;
8150 sc->length = aic_get_sense_bufsize(ahd, scb);
8154 * We can't allow the target to disconnect.
8155 * This will be an untagged transaction and
8156 * having the target disconnect will make this
8157 * transaction indestinguishable from outstanding
8158 * tagged transactions.
8163 * This request sense could be because the
8164 * the device lost power or in some other
8165 * way has lost our transfer negotiations.
8166 * Renegotiate if appropriate. Unit attention
8167 * errors will be reported before any data
8170 if (aic_get_residual(scb) == aic_get_transfer_length(scb)) {
8171 ahd_update_neg_request(ahd, &devinfo,
8173 AHD_NEG_IF_NON_ASYNC);
8175 if (tstate->auto_negotiate & devinfo.target_mask) {
8176 hscb->control |= MK_MESSAGE;
8178 ~(SCB_NEGOTIATE|SCB_ABORT|SCB_DEVICE_RESET);
8179 scb->flags |= SCB_AUTO_NEGOTIATE;
8181 hscb->cdb_len = sizeof(*sc);
8182 ahd_setup_data_scb(ahd, scb);
8183 scb->flags |= SCB_SENSE;
8184 ahd_queue_scb(ahd, scb);
8186 * Ensure we have enough time to actually
8187 * retrieve the sense.
8189 aic_scb_timer_reset(scb, 5 * 1000000);
8192 case SCSI_STATUS_OK:
8193 kprintf("%s: Interrupted for staus of 0???\n",
8203 * Calculate the residual for a just completed SCB.
8206 ahd_calc_residual(struct ahd_softc *ahd, struct scb *scb)
8208 struct hardware_scb *hscb;
8209 struct initiator_status *spkt;
8211 uint32_t resid_sgptr;
8217 * SG_STATUS_VALID clear in sgptr.
8218 * 2) Transferless command
8219 * 3) Never performed any transfers.
8220 * sgptr has SG_FULL_RESID set.
8221 * 4) No residual but target did not
8222 * save data pointers after the
8223 * last transfer, so sgptr was
8225 * 5) We have a partial residual.
8226 * Use residual_sgptr to determine
8231 sgptr = aic_le32toh(hscb->sgptr);
8232 if ((sgptr & SG_STATUS_VALID) == 0)
8235 sgptr &= ~SG_STATUS_VALID;
8237 if ((sgptr & SG_LIST_NULL) != 0)
8242 * Residual fields are the same in both
8243 * target and initiator status packets,
8244 * so we can always use the initiator fields
8245 * regardless of the role for this SCB.
8247 spkt = &hscb->shared_data.istatus;
8248 resid_sgptr = aic_le32toh(spkt->residual_sgptr);
8249 if ((sgptr & SG_FULL_RESID) != 0) {
8251 resid = aic_get_transfer_length(scb);
8252 } else if ((resid_sgptr & SG_LIST_NULL) != 0) {
8255 } else if ((resid_sgptr & SG_OVERRUN_RESID) != 0) {
8256 ahd_print_path(ahd, scb);
8257 kprintf("data overrun detected Tag == 0x%x.\n",
8259 ahd_freeze_devq(ahd, scb);
8260 aic_set_transaction_status(scb, CAM_DATA_RUN_ERR);
8261 aic_freeze_scb(scb);
8263 } else if ((resid_sgptr & ~SG_PTR_MASK) != 0) {
8264 panic("Bogus resid sgptr value 0x%x\n", resid_sgptr);
8267 struct ahd_dma_seg *sg;
8270 * Remainder of the SG where the transfer
8273 resid = aic_le32toh(spkt->residual_datacnt) & AHD_SG_LEN_MASK;
8274 sg = ahd_sg_bus_to_virt(ahd, scb, resid_sgptr & SG_PTR_MASK);
8276 /* The residual sg_ptr always points to the next sg */
8280 * Add up the contents of all residual
8281 * SG segments that are after the SG where
8282 * the transfer stopped.
8284 while ((aic_le32toh(sg->len) & AHD_DMA_LAST_SEG) == 0) {
8286 resid += aic_le32toh(sg->len) & AHD_SG_LEN_MASK;
8289 if ((scb->flags & SCB_SENSE) == 0)
8290 aic_set_residual(scb, resid);
8292 aic_set_sense_residual(scb, resid);
8295 if ((ahd_debug & AHD_SHOW_MISC) != 0) {
8296 ahd_print_path(ahd, scb);
8297 kprintf("Handled %sResidual of %d bytes\n",
8298 (scb->flags & SCB_SENSE) ? "Sense " : "", resid);
8303 /******************************* Target Mode **********************************/
8304 #ifdef AHD_TARGET_MODE
8306 * Add a target mode event to this lun's queue
8309 ahd_queue_lstate_event(struct ahd_softc *ahd, struct ahd_tmode_lstate *lstate,
8310 u_int initiator_id, u_int event_type, u_int event_arg)
8312 struct ahd_tmode_event *event;
8315 xpt_freeze_devq(lstate->path, /*count*/1);
8316 if (lstate->event_w_idx >= lstate->event_r_idx)
8317 pending = lstate->event_w_idx - lstate->event_r_idx;
8319 pending = AHD_TMODE_EVENT_BUFFER_SIZE + 1
8320 - (lstate->event_r_idx - lstate->event_w_idx);
8322 if (event_type == EVENT_TYPE_BUS_RESET
8323 || event_type == MSG_BUS_DEV_RESET) {
8325 * Any earlier events are irrelevant, so reset our buffer.
8326 * This has the effect of allowing us to deal with reset
8327 * floods (an external device holding down the reset line)
8328 * without losing the event that is really interesting.
8330 lstate->event_r_idx = 0;
8331 lstate->event_w_idx = 0;
8332 xpt_release_devq(lstate->path, pending, /*runqueue*/FALSE);
8335 if (pending == AHD_TMODE_EVENT_BUFFER_SIZE) {
8336 xpt_print_path(lstate->path);
8337 kprintf("immediate event %x:%x lost\n",
8338 lstate->event_buffer[lstate->event_r_idx].event_type,
8339 lstate->event_buffer[lstate->event_r_idx].event_arg);
8340 lstate->event_r_idx++;
8341 if (lstate->event_r_idx == AHD_TMODE_EVENT_BUFFER_SIZE)
8342 lstate->event_r_idx = 0;
8343 xpt_release_devq(lstate->path, /*count*/1, /*runqueue*/FALSE);
8346 event = &lstate->event_buffer[lstate->event_w_idx];
8347 event->initiator_id = initiator_id;
8348 event->event_type = event_type;
8349 event->event_arg = event_arg;
8350 lstate->event_w_idx++;
8351 if (lstate->event_w_idx == AHD_TMODE_EVENT_BUFFER_SIZE)
8352 lstate->event_w_idx = 0;
8356 * Send any target mode events queued up waiting
8357 * for immediate notify resources.
8360 ahd_send_lstate_events(struct ahd_softc *ahd, struct ahd_tmode_lstate *lstate)
8362 struct ccb_hdr *ccbh;
8363 struct ccb_immed_notify *inot;
8365 while (lstate->event_r_idx != lstate->event_w_idx
8366 && (ccbh = SLIST_FIRST(&lstate->immed_notifies)) != NULL) {
8367 struct ahd_tmode_event *event;
8369 event = &lstate->event_buffer[lstate->event_r_idx];
8370 SLIST_REMOVE_HEAD(&lstate->immed_notifies, sim_links.sle);
8371 inot = (struct ccb_immed_notify *)ccbh;
8372 switch (event->event_type) {
8373 case EVENT_TYPE_BUS_RESET:
8374 ccbh->status = CAM_SCSI_BUS_RESET|CAM_DEV_QFRZN;
8377 ccbh->status = CAM_MESSAGE_RECV|CAM_DEV_QFRZN;
8378 inot->message_args[0] = event->event_type;
8379 inot->message_args[1] = event->event_arg;
8382 inot->initiator_id = event->initiator_id;
8383 inot->sense_len = 0;
8384 xpt_done((union ccb *)inot);
8385 lstate->event_r_idx++;
8386 if (lstate->event_r_idx == AHD_TMODE_EVENT_BUFFER_SIZE)
8387 lstate->event_r_idx = 0;
8392 /******************** Sequencer Program Patching/Download *********************/
8396 ahd_dumpseq(struct ahd_softc* ahd)
8403 ahd_outb(ahd, SEQCTL0, PERRORDIS|FAILDIS|FASTMODE|LOADRAM);
8404 ahd_outw(ahd, PRGMCNT, 0);
8405 for (i = 0; i < max_prog; i++) {
8406 uint8_t ins_bytes[4];
8408 ahd_insb(ahd, SEQRAM, ins_bytes, 4);
8409 kprintf("0x%08x\n", ins_bytes[0] << 24
8410 | ins_bytes[1] << 16
8418 ahd_loadseq(struct ahd_softc *ahd)
8420 struct cs cs_table[num_critical_sections];
8421 u_int begin_set[num_critical_sections];
8422 u_int end_set[num_critical_sections];
8423 struct patch *cur_patch;
8429 u_int sg_prefetch_cnt;
8430 u_int sg_prefetch_cnt_limit;
8431 u_int sg_prefetch_align;
8433 uint8_t download_consts[DOWNLOAD_CONST_COUNT];
8436 kprintf("%s: Downloading Sequencer Program...",
8439 #if DOWNLOAD_CONST_COUNT != 7
8440 #error "Download Const Mismatch"
8443 * Start out with 0 critical sections
8444 * that apply to this firmware load.
8448 memset(begin_set, 0, sizeof(begin_set));
8449 memset(end_set, 0, sizeof(end_set));
8452 * Setup downloadable constant table.
8454 * The computation for the S/G prefetch variables is
8455 * a bit complicated. We would like to always fetch
8456 * in terms of cachelined sized increments. However,
8457 * if the cacheline is not an even multiple of the
8458 * SG element size or is larger than our SG RAM, using
8459 * just the cache size might leave us with only a portion
8460 * of an SG element at the tail of a prefetch. If the
8461 * cacheline is larger than our S/G prefetch buffer less
8462 * the size of an SG element, we may round down to a cacheline
8463 * that doesn't contain any or all of the S/G of interest
8464 * within the bounds of our S/G ram. Provide variables to
8465 * the sequencer that will allow it to handle these edge
8468 /* Start by aligning to the nearest cacheline. */
8469 sg_prefetch_align = ahd->pci_cachesize;
8470 if (sg_prefetch_align == 0)
8471 sg_prefetch_align = 8;
8472 /* Round down to the nearest power of 2. */
8473 while (powerof2(sg_prefetch_align) == 0)
8474 sg_prefetch_align--;
8476 * If the cacheline boundary is greater than half our prefetch RAM
8477 * we risk not being able to fetch even a single complete S/G
8478 * segment if we align to that boundary.
8480 if (sg_prefetch_align > CCSGADDR_MAX/2)
8481 sg_prefetch_align = CCSGADDR_MAX/2;
8482 /* Start by fetching a single cacheline. */
8483 sg_prefetch_cnt = sg_prefetch_align;
8485 * Increment the prefetch count by cachelines until
8486 * at least one S/G element will fit.
8488 sg_size = sizeof(struct ahd_dma_seg);
8489 if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0)
8490 sg_size = sizeof(struct ahd_dma64_seg);
8491 while (sg_prefetch_cnt < sg_size)
8492 sg_prefetch_cnt += sg_prefetch_align;
8494 * If the cacheline is not an even multiple of
8495 * the S/G size, we may only get a partial S/G when
8496 * we align. Add a cacheline if this is the case.
8498 if ((sg_prefetch_align % sg_size) != 0
8499 && (sg_prefetch_cnt < CCSGADDR_MAX))
8500 sg_prefetch_cnt += sg_prefetch_align;
8502 * Lastly, compute a value that the sequencer can use
8503 * to determine if the remainder of the CCSGRAM buffer
8504 * has a full S/G element in it.
8506 sg_prefetch_cnt_limit = -(sg_prefetch_cnt - sg_size + 1);
8507 download_consts[SG_PREFETCH_CNT] = sg_prefetch_cnt;
8508 download_consts[SG_PREFETCH_CNT_LIMIT] = sg_prefetch_cnt_limit;
8509 download_consts[SG_PREFETCH_ALIGN_MASK] = ~(sg_prefetch_align - 1);
8510 download_consts[SG_PREFETCH_ADDR_MASK] = (sg_prefetch_align - 1);
8511 download_consts[SG_SIZEOF] = sg_size;
8512 download_consts[PKT_OVERRUN_BUFOFFSET] =
8513 (ahd->overrun_buf - (uint8_t *)ahd->qoutfifo) / 256;
8514 download_consts[SCB_TRANSFER_SIZE] = SCB_TRANSFER_SIZE_1BYTE_LUN;
8515 cur_patch = patches;
8518 ahd_outb(ahd, SEQCTL0, PERRORDIS|FAILDIS|FASTMODE|LOADRAM);
8519 ahd_outw(ahd, PRGMCNT, 0);
8521 for (i = 0; i < sizeof(seqprog)/4; i++) {
8522 if (ahd_check_patch(ahd, &cur_patch, i, &skip_addr) == 0) {
8524 * Don't download this instruction as it
8525 * is in a patch that was removed.
8530 * Move through the CS table until we find a CS
8531 * that might apply to this instruction.
8533 for (; cur_cs < num_critical_sections; cur_cs++) {
8534 if (critical_sections[cur_cs].end <= i) {
8535 if (begin_set[cs_count] == TRUE
8536 && end_set[cs_count] == FALSE) {
8537 cs_table[cs_count].end = downloaded;
8538 end_set[cs_count] = TRUE;
8543 if (critical_sections[cur_cs].begin <= i
8544 && begin_set[cs_count] == FALSE) {
8545 cs_table[cs_count].begin = downloaded;
8546 begin_set[cs_count] = TRUE;
8550 ahd_download_instr(ahd, i, download_consts);
8554 ahd->num_critical_sections = cs_count;
8555 if (cs_count != 0) {
8556 cs_count *= sizeof(struct cs);
8557 ahd->critical_sections = kmalloc(cs_count, M_DEVBUF, M_INTWAIT);
8558 memcpy(ahd->critical_sections, cs_table, cs_count);
8560 ahd_outb(ahd, SEQCTL0, PERRORDIS|FAILDIS|FASTMODE);
8563 kprintf(" %d instructions downloaded\n", downloaded);
8564 kprintf("%s: Features 0x%x, Bugs 0x%x, Flags 0x%x\n",
8565 ahd_name(ahd), ahd->features, ahd->bugs, ahd->flags);
8570 ahd_check_patch(struct ahd_softc *ahd, struct patch **start_patch,
8571 u_int start_instr, u_int *skip_addr)
8573 struct patch *cur_patch;
8574 struct patch *last_patch;
8577 num_patches = sizeof(patches)/sizeof(struct patch);
8578 last_patch = &patches[num_patches];
8579 cur_patch = *start_patch;
8581 while (cur_patch < last_patch && start_instr == cur_patch->begin) {
8583 if (cur_patch->patch_func(ahd) == 0) {
8585 /* Start rejecting code */
8586 *skip_addr = start_instr + cur_patch->skip_instr;
8587 cur_patch += cur_patch->skip_patch;
8589 /* Accepted this patch. Advance to the next
8590 * one and wait for our intruction pointer to
8597 *start_patch = cur_patch;
8598 if (start_instr < *skip_addr)
8599 /* Still skipping */
8606 ahd_resolve_seqaddr(struct ahd_softc *ahd, u_int address)
8608 struct patch *cur_patch;
8614 cur_patch = patches;
8617 for (i = 0; i < address;) {
8619 ahd_check_patch(ahd, &cur_patch, i, &skip_addr);
8621 if (skip_addr > i) {
8624 end_addr = MIN(address, skip_addr);
8625 address_offset += end_addr - i;
8631 return (address - address_offset);
8635 ahd_download_instr(struct ahd_softc *ahd, u_int instrptr, uint8_t *dconsts)
8637 union ins_formats instr;
8638 struct ins_format1 *fmt1_ins;
8639 struct ins_format3 *fmt3_ins;
8643 * The firmware is always compiled into a little endian format.
8645 instr.integer = aic_le32toh(*(uint32_t*)&seqprog[instrptr * 4]);
8647 fmt1_ins = &instr.format1;
8650 /* Pull the opcode */
8651 opcode = instr.format1.opcode;
8662 fmt3_ins = &instr.format3;
8663 fmt3_ins->address = ahd_resolve_seqaddr(ahd, fmt3_ins->address);
8672 if (fmt1_ins->parity != 0) {
8673 fmt1_ins->immediate = dconsts[fmt1_ins->immediate];
8675 fmt1_ins->parity = 0;
8681 /* Calculate odd parity for the instruction */
8682 for (i = 0, count = 0; i < 31; i++) {
8686 if ((instr.integer & mask) != 0)
8689 if ((count & 0x01) == 0)
8690 instr.format1.parity = 1;
8692 /* The sequencer is a little endian cpu */
8693 instr.integer = aic_htole32(instr.integer);
8694 ahd_outsb(ahd, SEQRAM, instr.bytes, 4);
8698 panic("Unknown opcode encountered in seq program");
8704 ahd_probe_stack_size(struct ahd_softc *ahd)
8713 * We avoid using 0 as a pattern to avoid
8714 * confusion if the stack implementation
8715 * "back-fills" with zeros when "poping'
8718 for (i = 1; i <= last_probe+1; i++) {
8719 ahd_outb(ahd, STACK, i & 0xFF);
8720 ahd_outb(ahd, STACK, (i >> 8) & 0xFF);
8724 for (i = last_probe+1; i > 0; i--) {
8727 stack_entry = ahd_inb(ahd, STACK)
8728 |(ahd_inb(ahd, STACK) << 8);
8729 if (stack_entry != i)
8735 return (last_probe);
8739 ahd_dump_all_cards_state(void)
8741 struct ahd_softc *list_ahd;
8743 TAILQ_FOREACH(list_ahd, &ahd_tailq, links) {
8744 ahd_dump_card_state(list_ahd);
8749 ahd_print_register(ahd_reg_parse_entry_t *table, u_int num_entries,
8750 const char *name, u_int address, u_int value,
8751 u_int *cur_column, u_int wrap_point)
8756 if (cur_column != NULL && *cur_column >= wrap_point) {
8760 printed = kprintf("%s[0x%x]", name, value);
8761 if (table == NULL) {
8762 printed += kprintf(" ");
8763 *cur_column += printed;
8767 while (printed_mask != 0xFF) {
8770 for (entry = 0; entry < num_entries; entry++) {
8771 if (((value & table[entry].mask)
8772 != table[entry].value)
8773 || ((printed_mask & table[entry].mask)
8774 == table[entry].mask))
8777 printed += kprintf("%s%s",
8778 printed_mask == 0 ? ":(" : "|",
8780 printed_mask |= table[entry].mask;
8784 if (entry >= num_entries)
8787 if (printed_mask != 0)
8788 printed += kprintf(") ");
8790 printed += kprintf(" ");
8791 if (cur_column != NULL)
8792 *cur_column += printed;
8797 ahd_dump_card_state(struct ahd_softc *ahd)
8800 ahd_mode_state saved_modes;
8804 u_int saved_scb_index;
8808 if (ahd_is_paused(ahd)) {
8814 saved_modes = ahd_save_modes(ahd);
8815 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
8816 kprintf(">>>>>>>>>>>>>>>>>> Dump Card State Begins <<<<<<<<<<<<<<<<<\n"
8817 "%s: Dumping Card State at program address 0x%x Mode 0x%x\n",
8819 ahd_inw(ahd, CURADDR),
8820 ahd_build_mode_state(ahd, ahd->saved_src_mode,
8821 ahd->saved_dst_mode));
8823 kprintf("Card was paused\n");
8825 if (ahd_check_cmdcmpltqueues(ahd))
8826 kprintf("Completions are pending\n");
8829 * Mode independent registers.
8832 ahd_hs_mailbox_print(ahd_inb(ahd, LOCAL_HS_MAILBOX), &cur_col, 50);
8833 ahd_intctl_print(ahd_inb(ahd, INTCTL), &cur_col, 50);
8834 ahd_seqintstat_print(ahd_inb(ahd, SEQINTSTAT), &cur_col, 50);
8835 ahd_saved_mode_print(ahd_inb(ahd, SAVED_MODE), &cur_col, 50);
8836 ahd_dffstat_print(ahd_inb(ahd, DFFSTAT), &cur_col, 50);
8837 ahd_scsisigi_print(ahd_inb(ahd, SCSISIGI), &cur_col, 50);
8838 ahd_scsiphase_print(ahd_inb(ahd, SCSIPHASE), &cur_col, 50);
8839 ahd_scsibus_print(ahd_inb(ahd, SCSIBUS), &cur_col, 50);
8840 ahd_lastphase_print(ahd_inb(ahd, LASTPHASE), &cur_col, 50);
8841 ahd_scsiseq0_print(ahd_inb(ahd, SCSISEQ0), &cur_col, 50);
8842 ahd_scsiseq1_print(ahd_inb(ahd, SCSISEQ1), &cur_col, 50);
8843 ahd_seqctl0_print(ahd_inb(ahd, SEQCTL0), &cur_col, 50);
8844 ahd_seqintctl_print(ahd_inb(ahd, SEQINTCTL), &cur_col, 50);
8845 ahd_seq_flags_print(ahd_inb(ahd, SEQ_FLAGS), &cur_col, 50);
8846 ahd_seq_flags2_print(ahd_inb(ahd, SEQ_FLAGS2), &cur_col, 50);
8847 ahd_sstat0_print(ahd_inb(ahd, SSTAT0), &cur_col, 50);
8848 ahd_sstat1_print(ahd_inb(ahd, SSTAT1), &cur_col, 50);
8849 ahd_sstat2_print(ahd_inb(ahd, SSTAT2), &cur_col, 50);
8850 ahd_sstat3_print(ahd_inb(ahd, SSTAT3), &cur_col, 50);
8851 ahd_perrdiag_print(ahd_inb(ahd, PERRDIAG), &cur_col, 50);
8852 ahd_simode1_print(ahd_inb(ahd, SIMODE1), &cur_col, 50);
8853 ahd_lqistat0_print(ahd_inb(ahd, LQISTAT0), &cur_col, 50);
8854 ahd_lqistat1_print(ahd_inb(ahd, LQISTAT1), &cur_col, 50);
8855 ahd_lqistat2_print(ahd_inb(ahd, LQISTAT2), &cur_col, 50);
8856 ahd_lqostat0_print(ahd_inb(ahd, LQOSTAT0), &cur_col, 50);
8857 ahd_lqostat1_print(ahd_inb(ahd, LQOSTAT1), &cur_col, 50);
8858 ahd_lqostat2_print(ahd_inb(ahd, LQOSTAT2), &cur_col, 50);
8860 kprintf("\nSCB Count = %d CMDS_PENDING = %d LASTSCB 0x%x "
8861 "CURRSCB 0x%x NEXTSCB 0x%x\n",
8862 ahd->scb_data.numscbs, ahd_inw(ahd, CMDS_PENDING),
8863 ahd_inw(ahd, LASTSCB), ahd_inw(ahd, CURRSCB),
8864 ahd_inw(ahd, NEXTSCB));
8867 ahd_search_qinfifo(ahd, CAM_TARGET_WILDCARD, ALL_CHANNELS,
8868 CAM_LUN_WILDCARD, SCB_LIST_NULL,
8869 ROLE_UNKNOWN, /*status*/0, SEARCH_PRINT);
8870 saved_scb_index = ahd_get_scbptr(ahd);
8871 kprintf("Pending list:");
8873 LIST_FOREACH(scb, &ahd->pending_scbs, pending_links) {
8874 if (i++ > AHD_SCB_MAX)
8876 cur_col = kprintf("\n%3d FIFO_USE[0x%x] ", SCB_GET_TAG(scb),
8877 ahd_inb_scbram(ahd, SCB_FIFO_USE_COUNT));
8878 ahd_set_scbptr(ahd, SCB_GET_TAG(scb));
8879 ahd_scb_control_print(ahd_inb_scbram(ahd, SCB_CONTROL),
8881 ahd_scb_scsiid_print(ahd_inb_scbram(ahd, SCB_SCSIID),
8884 kprintf("\nTotal %d\n", i);
8886 kprintf("Kernel Free SCB list: ");
8888 TAILQ_FOREACH(scb, &ahd->scb_data.free_scbs, links.tqe) {
8889 struct scb *list_scb;
8893 kprintf("%d ", SCB_GET_TAG(list_scb));
8894 list_scb = LIST_NEXT(list_scb, collision_links);
8895 } while (list_scb && i++ < AHD_SCB_MAX);
8898 LIST_FOREACH(scb, &ahd->scb_data.any_dev_free_scb_list, links.le) {
8899 if (i++ > AHD_SCB_MAX)
8901 kprintf("%d ", SCB_GET_TAG(scb));
8905 kprintf("Sequencer Complete DMA-inprog list: ");
8906 scb_index = ahd_inw(ahd, COMPLETE_SCB_DMAINPROG_HEAD);
8908 while (!SCBID_IS_NULL(scb_index) && i++ < AHD_SCB_MAX) {
8909 ahd_set_scbptr(ahd, scb_index);
8910 kprintf("%d ", scb_index);
8911 scb_index = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE);
8915 kprintf("Sequencer Complete list: ");
8916 scb_index = ahd_inw(ahd, COMPLETE_SCB_HEAD);
8918 while (!SCBID_IS_NULL(scb_index) && i++ < AHD_SCB_MAX) {
8919 ahd_set_scbptr(ahd, scb_index);
8920 kprintf("%d ", scb_index);
8921 scb_index = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE);
8926 kprintf("Sequencer DMA-Up and Complete list: ");
8927 scb_index = ahd_inw(ahd, COMPLETE_DMA_SCB_HEAD);
8929 while (!SCBID_IS_NULL(scb_index) && i++ < AHD_SCB_MAX) {
8930 ahd_set_scbptr(ahd, scb_index);
8931 kprintf("%d ", scb_index);
8932 scb_index = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE);
8935 kprintf("Sequencer On QFreeze and Complete list: ");
8936 scb_index = ahd_inw(ahd, COMPLETE_ON_QFREEZE_HEAD);
8938 while (!SCBID_IS_NULL(scb_index) && i++ < AHD_SCB_MAX) {
8939 ahd_set_scbptr(ahd, scb_index);
8940 kprintf("%d ", scb_index);
8941 scb_index = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE);
8944 ahd_set_scbptr(ahd, saved_scb_index);
8945 dffstat = ahd_inb(ahd, DFFSTAT);
8946 for (i = 0; i < 2; i++) {
8948 struct scb *fifo_scb;
8952 ahd_set_modes(ahd, AHD_MODE_DFF0 + i, AHD_MODE_DFF0 + i);
8953 fifo_scbptr = ahd_get_scbptr(ahd);
8954 kprintf("\n%s: FIFO%d %s, LONGJMP == 0x%x, SCB 0x%x\n",
8956 (dffstat & (FIFO0FREE << i)) ? "Free" : "Active",
8957 ahd_inw(ahd, LONGJMP_ADDR), fifo_scbptr);
8959 ahd_seqimode_print(ahd_inb(ahd, SEQIMODE), &cur_col, 50);
8960 ahd_seqintsrc_print(ahd_inb(ahd, SEQINTSRC), &cur_col, 50);
8961 ahd_dfcntrl_print(ahd_inb(ahd, DFCNTRL), &cur_col, 50);
8962 ahd_dfstatus_print(ahd_inb(ahd, DFSTATUS), &cur_col, 50);
8963 ahd_sg_cache_shadow_print(ahd_inb(ahd, SG_CACHE_SHADOW),
8965 ahd_sg_state_print(ahd_inb(ahd, SG_STATE), &cur_col, 50);
8966 ahd_dffsxfrctl_print(ahd_inb(ahd, DFFSXFRCTL), &cur_col, 50);
8967 ahd_soffcnt_print(ahd_inb(ahd, SOFFCNT), &cur_col, 50);
8968 ahd_mdffstat_print(ahd_inb(ahd, MDFFSTAT), &cur_col, 50);
8973 cur_col += kprintf("SHADDR = 0x%x%x, SHCNT = 0x%x ",
8974 ahd_inl(ahd, SHADDR+4),
8975 ahd_inl(ahd, SHADDR),
8976 (ahd_inb(ahd, SHCNT)
8977 | (ahd_inb(ahd, SHCNT + 1) << 8)
8978 | (ahd_inb(ahd, SHCNT + 2) << 16)));
8983 cur_col += kprintf("HADDR = 0x%x%x, HCNT = 0x%x ",
8984 ahd_inl(ahd, HADDR+4),
8985 ahd_inl(ahd, HADDR),
8987 | (ahd_inb(ahd, HCNT + 1) << 8)
8988 | (ahd_inb(ahd, HCNT + 2) << 16)));
8989 ahd_ccsgctl_print(ahd_inb(ahd, CCSGCTL), &cur_col, 50);
8991 if ((ahd_debug & AHD_SHOW_SG) != 0) {
8992 fifo_scb = ahd_lookup_scb(ahd, fifo_scbptr);
8993 if (fifo_scb != NULL)
8994 ahd_dump_sglist(fifo_scb);
8998 kprintf("\nLQIN: ");
8999 for (i = 0; i < 20; i++)
9000 kprintf("0x%x ", ahd_inb(ahd, LQIN + i));
9002 ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
9003 kprintf("%s: LQISTATE = 0x%x, LQOSTATE = 0x%x, OPTIONMODE = 0x%x\n",
9004 ahd_name(ahd), ahd_inb(ahd, LQISTATE), ahd_inb(ahd, LQOSTATE),
9005 ahd_inb(ahd, OPTIONMODE));
9006 kprintf("%s: OS_SPACE_CNT = 0x%x MAXCMDCNT = 0x%x\n",
9007 ahd_name(ahd), ahd_inb(ahd, OS_SPACE_CNT),
9008 ahd_inb(ahd, MAXCMDCNT));
9009 ahd_simode0_print(ahd_inb(ahd, SIMODE0), &cur_col, 50);
9011 ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
9013 ahd_ccscbctl_print(ahd_inb(ahd, CCSCBCTL), &cur_col, 50);
9015 ahd_set_modes(ahd, ahd->saved_src_mode, ahd->saved_dst_mode);
9016 kprintf("%s: REG0 == 0x%x, SINDEX = 0x%x, DINDEX = 0x%x\n",
9017 ahd_name(ahd), ahd_inw(ahd, REG0), ahd_inw(ahd, SINDEX),
9018 ahd_inw(ahd, DINDEX));
9019 kprintf("%s: SCBPTR == 0x%x, SCB_NEXT == 0x%x, SCB_NEXT2 == 0x%x\n",
9020 ahd_name(ahd), ahd_get_scbptr(ahd),
9021 ahd_inw_scbram(ahd, SCB_NEXT),
9022 ahd_inw_scbram(ahd, SCB_NEXT2));
9023 kprintf("CDB %x %x %x %x %x %x\n",
9024 ahd_inb_scbram(ahd, SCB_CDB_STORE),
9025 ahd_inb_scbram(ahd, SCB_CDB_STORE+1),
9026 ahd_inb_scbram(ahd, SCB_CDB_STORE+2),
9027 ahd_inb_scbram(ahd, SCB_CDB_STORE+3),
9028 ahd_inb_scbram(ahd, SCB_CDB_STORE+4),
9029 ahd_inb_scbram(ahd, SCB_CDB_STORE+5));
9031 for (i = 0; i < ahd->stack_size; i++) {
9032 ahd->saved_stack[i] =
9033 ahd_inb(ahd, STACK)|(ahd_inb(ahd, STACK) << 8);
9034 kprintf(" 0x%x", ahd->saved_stack[i]);
9036 for (i = ahd->stack_size-1; i >= 0; i--) {
9037 ahd_outb(ahd, STACK, ahd->saved_stack[i] & 0xFF);
9038 ahd_outb(ahd, STACK, (ahd->saved_stack[i] >> 8) & 0xFF);
9040 kprintf("\n<<<<<<<<<<<<<<<<< Dump Card State Ends >>>>>>>>>>>>>>>>>>\n");
9041 ahd_platform_dump_card_state(ahd);
9042 ahd_restore_modes(ahd, saved_modes);
9048 ahd_dump_scbs(struct ahd_softc *ahd)
9050 ahd_mode_state saved_modes;
9051 u_int saved_scb_index;
9054 saved_modes = ahd_save_modes(ahd);
9055 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
9056 saved_scb_index = ahd_get_scbptr(ahd);
9057 for (i = 0; i < AHD_SCB_MAX; i++) {
9058 ahd_set_scbptr(ahd, i);
9060 kprintf("(CTRL 0x%x ID 0x%x N 0x%x N2 0x%x SG 0x%x, RSG 0x%x)\n",
9061 ahd_inb_scbram(ahd, SCB_CONTROL),
9062 ahd_inb_scbram(ahd, SCB_SCSIID),
9063 ahd_inw_scbram(ahd, SCB_NEXT),
9064 ahd_inw_scbram(ahd, SCB_NEXT2),
9065 ahd_inl_scbram(ahd, SCB_SGPTR),
9066 ahd_inl_scbram(ahd, SCB_RESIDUAL_SGPTR));
9069 ahd_set_scbptr(ahd, saved_scb_index);
9070 ahd_restore_modes(ahd, saved_modes);
9074 /*************************** Timeout Handling *********************************/
9076 ahd_timeout(struct scb *scb)
9078 struct ahd_softc *ahd;
9080 ahd = scb->ahd_softc;
9081 if ((scb->flags & SCB_ACTIVE) != 0) {
9082 if ((scb->flags & SCB_TIMEDOUT) == 0) {
9083 LIST_INSERT_HEAD(&ahd->timedout_scbs, scb,
9085 scb->flags |= SCB_TIMEDOUT;
9087 ahd_wakeup_recovery_thread(ahd);
9092 * ahd_recover_commands determines if any of the commands that have currently
9093 * timedout are the root cause for this timeout. Innocent commands are given
9094 * a new timeout while we wait for the command executing on the bus to timeout.
9095 * This routine is invoked from a thread context so we are allowed to sleep.
9096 * Our lock is not held on entry.
9099 ahd_recover_commands(struct ahd_softc *ahd)
9102 struct scb *active_scb;
9105 u_int active_scbptr;
9111 * Pause the controller and manually flush any
9112 * commands that have just completed but that our
9113 * interrupt handler has yet to see.
9115 was_paused = ahd_is_paused(ahd);
9116 ahd_pause_and_flushwork(ahd);
9118 if (LIST_EMPTY(&ahd->timedout_scbs) != 0) {
9120 * The timedout commands have already
9121 * completed. This typically means
9122 * that either the timeout value was on
9123 * the hairy edge of what the device
9124 * requires or - more likely - interrupts
9125 * are not happening.
9127 kprintf("%s: Timedout SCBs already complete. "
9128 "Interrupts may not be functioning.\n", ahd_name(ahd));
9134 kprintf("%s: Recovery Initiated - Card was %spaused\n", ahd_name(ahd),
9135 was_paused ? "" : "not ");
9136 ahd_dump_card_state(ahd);
9139 * Determine identity of SCB acting on the bus.
9140 * This test only catches non-packetized transactions.
9141 * Due to the fleeting nature of packetized operations,
9142 * we can't easily determine that a packetized operation
9145 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
9146 last_phase = ahd_inb(ahd, LASTPHASE);
9147 active_scbptr = ahd_get_scbptr(ahd);
9149 if (last_phase != P_BUSFREE
9150 || (ahd_inb(ahd, SEQ_FLAGS) & NOT_IDENTIFIED) == 0)
9151 active_scb = ahd_lookup_scb(ahd, active_scbptr);
9153 while ((scb = LIST_FIRST(&ahd->timedout_scbs)) != NULL) {
9158 target = SCB_GET_TARGET(ahd, scb);
9159 channel = SCB_GET_CHANNEL(ahd, scb);
9160 lun = SCB_GET_LUN(scb);
9162 ahd_print_path(ahd, scb);
9163 kprintf("SCB 0x%x - timed out\n", scb->hscb->tag);
9165 if (scb->flags & (SCB_DEVICE_RESET|SCB_ABORT)) {
9167 * Been down this road before.
9168 * Do a full bus reset.
9170 aic_set_transaction_status(scb, CAM_CMD_TIMEOUT);
9172 found = ahd_reset_channel(ahd, channel,
9173 /*Initiate Reset*/TRUE);
9174 kprintf("%s: Issued Channel %c Bus Reset. "
9175 "%d SCBs aborted\n", ahd_name(ahd), channel,
9181 * Remove the command from the timedout list in
9182 * preparation for requeing it.
9184 LIST_REMOVE(scb, timedout_links);
9185 scb->flags &= ~SCB_TIMEDOUT;
9187 if (active_scb != NULL) {
9189 if (active_scb != scb) {
9191 * If the active SCB is not us, assume that
9192 * the active SCB has a longer timeout than
9193 * the timedout SCB, and wait for the active
9196 ahd_other_scb_timeout(ahd, scb, active_scb);
9201 * We're active on the bus, so assert ATN
9202 * and hope that the target responds.
9204 ahd_set_recoveryscb(ahd, active_scb);
9205 active_scb->flags |= SCB_RECOVERY_SCB|SCB_DEVICE_RESET;
9206 ahd_outb(ahd, MSG_OUT, HOST_MSG);
9207 ahd_outb(ahd, SCSISIGO, last_phase|ATNO);
9208 ahd_print_path(ahd, active_scb);
9209 kprintf("BDR message in message buffer\n");
9210 aic_scb_timer_reset(scb, 2 * 1000000);
9212 } else if (last_phase != P_BUSFREE
9213 && ahd_inb(ahd, SCSIPHASE) == 0) {
9215 * SCB is not identified, there
9216 * is no pending REQ, and the sequencer
9217 * has not seen a busfree. Looks like
9218 * a stuck connection waiting to
9219 * go busfree. Reset the bus.
9221 kprintf("%s: Connection stuck awaiting busfree or "
9222 "Identify Msg.\n", ahd_name(ahd));
9224 } else if (ahd_search_qinfifo(ahd, target, channel, lun,
9225 scb->hscb->tag, ROLE_INITIATOR,
9226 /*status*/0, SEARCH_COUNT) > 0) {
9229 * We haven't even gone out on the bus
9230 * yet, so the timeout must be due to
9231 * some other command. Reset the timer
9234 ahd_other_scb_timeout(ahd, scb, scb);
9237 * This SCB is for a disconnected transaction
9238 * and we haven't found a better candidate on
9239 * the bus to explain this timeout.
9241 ahd_set_recoveryscb(ahd, scb);
9244 * Actually re-queue this SCB in an attempt
9245 * to select the device before it reconnects.
9246 * In either case (selection or reselection),
9247 * we will now issue a target reset to the
9250 * Set the MK_MESSAGE control bit indicating
9251 * that we desire to send a message. We
9252 * also set the disconnected flag since
9253 * in the paging case there is no guarantee
9254 * that our SCB control byte matches the
9255 * version on the card. We don't want the
9256 * sequencer to abort the command thinking
9257 * an unsolicited reselection occurred.
9259 scb->flags |= SCB_DEVICE_RESET;
9260 scb->hscb->cdb_len = 0;
9261 scb->hscb->task_attribute = 0;
9262 scb->hscb->task_management = SIU_TASKMGMT_ABORT_TASK;
9264 ahd_set_scbptr(ahd, SCB_GET_TAG(scb));
9265 if ((scb->flags & SCB_PACKETIZED) != 0) {
9267 * Mark the SCB has having an outstanding
9268 * task management function. Should the command
9269 * complete normally before the task management
9270 * function can be sent, the host will be
9271 * notified to abort our requeued SCB.
9273 ahd_outb(ahd, SCB_TASK_MANAGEMENT,
9274 scb->hscb->task_management);
9277 * If non-packetized, set the MK_MESSAGE control
9278 * bit indicating that we desire to send a
9279 * message. We also set the disconnected flag
9280 * since there is no guarantee that our SCB
9281 * control byte matches the version on the
9282 * card. We don't want the sequencer to abort
9283 * the command thinking an unsolicited
9284 * reselection occurred.
9286 scb->hscb->control |= MK_MESSAGE|DISCONNECTED;
9289 * The sequencer will never re-reference the
9290 * in-core SCB. To make sure we are notified
9291 * during reslection, set the MK_MESSAGE flag in
9292 * the card's copy of the SCB.
9294 ahd_outb(ahd, SCB_CONTROL,
9295 ahd_inb(ahd, SCB_CONTROL)|MK_MESSAGE);
9299 * Clear out any entries in the QINFIFO first
9300 * so we are the next SCB for this target
9303 ahd_search_qinfifo(ahd, target, channel, lun,
9304 SCB_LIST_NULL, ROLE_INITIATOR,
9305 CAM_REQUEUE_REQ, SEARCH_COMPLETE);
9306 ahd_qinfifo_requeue_tail(ahd, scb);
9307 ahd_set_scbptr(ahd, active_scbptr);
9308 ahd_print_path(ahd, scb);
9309 kprintf("Queuing a BDR SCB\n");
9310 aic_scb_timer_reset(scb, 2 * 1000000);
9316 * Any remaining SCBs were not the "culprit", so remove
9317 * them from the timeout list. The timer for these commands
9318 * will be reset once the recovery SCB completes.
9320 while ((scb = LIST_FIRST(&ahd->timedout_scbs)) != NULL) {
9322 LIST_REMOVE(scb, timedout_links);
9323 scb->flags &= ~SCB_TIMEDOUT;
9331 ahd_other_scb_timeout(struct ahd_softc *ahd, struct scb *scb,
9332 struct scb *other_scb)
9336 ahd_print_path(ahd, scb);
9337 kprintf("Other SCB Timeout%s",
9338 (scb->flags & SCB_OTHERTCL_TIMEOUT) != 0
9339 ? " again\n" : "\n");
9340 scb->flags |= SCB_OTHERTCL_TIMEOUT;
9341 newtimeout = MAX(aic_get_timeout(other_scb),
9342 aic_get_timeout(scb));
9343 aic_scb_timer_reset(scb, newtimeout);
9346 /**************************** Flexport Logic **********************************/
9348 * Read count 16bit words from 16bit word address start_addr from the
9349 * SEEPROM attached to the controller, into buf, using the controller's
9350 * SEEPROM reading state machine. Optionally treat the data as a byte
9351 * stream in terms of byte order.
9354 ahd_read_seeprom(struct ahd_softc *ahd, uint16_t *buf,
9355 u_int start_addr, u_int count, int bytestream)
9362 * If we never make it through the loop even once,
9363 * we were passed invalid arguments.
9366 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
9367 end_addr = start_addr + count;
9368 for (cur_addr = start_addr; cur_addr < end_addr; cur_addr++) {
9370 ahd_outb(ahd, SEEADR, cur_addr);
9371 ahd_outb(ahd, SEECTL, SEEOP_READ | SEESTART);
9373 error = ahd_wait_seeprom(ahd);
9376 if (bytestream != 0) {
9377 uint8_t *bytestream_ptr;
9379 bytestream_ptr = (uint8_t *)buf;
9380 *bytestream_ptr++ = ahd_inb(ahd, SEEDAT);
9381 *bytestream_ptr = ahd_inb(ahd, SEEDAT+1);
9384 * ahd_inw() already handles machine byte order.
9386 *buf = ahd_inw(ahd, SEEDAT);
9394 * Write count 16bit words from buf, into SEEPROM attache to the
9395 * controller starting at 16bit word address start_addr, using the
9396 * controller's SEEPROM writing state machine.
9399 ahd_write_seeprom(struct ahd_softc *ahd, uint16_t *buf,
9400 u_int start_addr, u_int count)
9407 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
9410 /* Place the chip into write-enable mode */
9411 ahd_outb(ahd, SEEADR, SEEOP_EWEN_ADDR);
9412 ahd_outb(ahd, SEECTL, SEEOP_EWEN | SEESTART);
9413 error = ahd_wait_seeprom(ahd);
9418 * Write the data. If we don't get throught the loop at
9419 * least once, the arguments were invalid.
9422 end_addr = start_addr + count;
9423 for (cur_addr = start_addr; cur_addr < end_addr; cur_addr++) {
9424 ahd_outw(ahd, SEEDAT, *buf++);
9425 ahd_outb(ahd, SEEADR, cur_addr);
9426 ahd_outb(ahd, SEECTL, SEEOP_WRITE | SEESTART);
9428 retval = ahd_wait_seeprom(ahd);
9436 ahd_outb(ahd, SEEADR, SEEOP_EWDS_ADDR);
9437 ahd_outb(ahd, SEECTL, SEEOP_EWDS | SEESTART);
9438 error = ahd_wait_seeprom(ahd);
9445 * Wait ~100us for the serial eeprom to satisfy our request.
9448 ahd_wait_seeprom(struct ahd_softc *ahd)
9453 while ((ahd_inb(ahd, SEESTAT) & (SEEARBACK|SEEBUSY)) != 0 && --cnt)
9462 * Validate the two checksums in the per_channel
9463 * vital product data struct.
9466 ahd_verify_vpd_cksum(struct vpd_config *vpd)
9473 vpdarray = (uint8_t *)vpd;
9474 maxaddr = offsetof(struct vpd_config, vpd_checksum);
9476 for (i = offsetof(struct vpd_config, resource_type); i < maxaddr; i++)
9477 checksum = checksum + vpdarray[i];
9479 || (-checksum & 0xFF) != vpd->vpd_checksum)
9483 maxaddr = offsetof(struct vpd_config, checksum);
9484 for (i = offsetof(struct vpd_config, default_target_flags);
9486 checksum = checksum + vpdarray[i];
9488 || (-checksum & 0xFF) != vpd->checksum)
9494 ahd_verify_cksum(struct seeprom_config *sc)
9501 maxaddr = (sizeof(*sc)/2) - 1;
9503 scarray = (uint16_t *)sc;
9505 for (i = 0; i < maxaddr; i++)
9506 checksum = checksum + scarray[i];
9508 || (checksum & 0xFFFF) != sc->checksum) {
9516 ahd_acquire_seeprom(struct ahd_softc *ahd)
9519 * We should be able to determine the SEEPROM type
9520 * from the flexport logic, but unfortunately not
9521 * all implementations have this logic and there is
9522 * no programatic method for determining if the logic
9530 error = ahd_read_flexport(ahd, FLXADDR_ROMSTAT_CURSENSECTL, &seetype);
9532 || ((seetype & FLX_ROMSTAT_SEECFG) == FLX_ROMSTAT_SEE_NONE))
9539 ahd_release_seeprom(struct ahd_softc *ahd)
9541 /* Currently a no-op */
9545 ahd_write_flexport(struct ahd_softc *ahd, u_int addr, u_int value)
9549 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
9551 panic("ahd_write_flexport: address out of range");
9552 ahd_outb(ahd, BRDCTL, BRDEN|(addr << 3));
9553 error = ahd_wait_flexport(ahd);
9556 ahd_outb(ahd, BRDDAT, value);
9557 ahd_flush_device_writes(ahd);
9558 ahd_outb(ahd, BRDCTL, BRDSTB|BRDEN|(addr << 3));
9559 ahd_flush_device_writes(ahd);
9560 ahd_outb(ahd, BRDCTL, BRDEN|(addr << 3));
9561 ahd_flush_device_writes(ahd);
9562 ahd_outb(ahd, BRDCTL, 0);
9563 ahd_flush_device_writes(ahd);
9568 ahd_read_flexport(struct ahd_softc *ahd, u_int addr, uint8_t *value)
9572 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
9574 panic("ahd_read_flexport: address out of range");
9575 ahd_outb(ahd, BRDCTL, BRDRW|BRDEN|(addr << 3));
9576 error = ahd_wait_flexport(ahd);
9579 *value = ahd_inb(ahd, BRDDAT);
9580 ahd_outb(ahd, BRDCTL, 0);
9581 ahd_flush_device_writes(ahd);
9586 * Wait at most 2 seconds for flexport arbitration to succeed.
9589 ahd_wait_flexport(struct ahd_softc *ahd)
9593 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
9594 cnt = 1000000 * 2 / 5;
9595 while ((ahd_inb(ahd, BRDCTL) & FLXARBACK) == 0 && --cnt)
9603 /************************* Target Mode ****************************************/
9604 #ifdef AHD_TARGET_MODE
9606 ahd_find_tmode_devs(struct ahd_softc *ahd, struct cam_sim *sim, union ccb *ccb,
9607 struct ahd_tmode_tstate **tstate,
9608 struct ahd_tmode_lstate **lstate,
9609 int notfound_failure)
9612 if ((ahd->features & AHD_TARGETMODE) == 0)
9613 return (CAM_REQ_INVALID);
9616 * Handle the 'black hole' device that sucks up
9617 * requests to unattached luns on enabled targets.
9619 if (ccb->ccb_h.target_id == CAM_TARGET_WILDCARD
9620 && ccb->ccb_h.target_lun == CAM_LUN_WILDCARD) {
9622 *lstate = ahd->black_hole;
9626 max_id = (ahd->features & AHD_WIDE) ? 15 : 7;
9627 if (ccb->ccb_h.target_id > max_id)
9628 return (CAM_TID_INVALID);
9630 if (ccb->ccb_h.target_lun >= AHD_NUM_LUNS)
9631 return (CAM_LUN_INVALID);
9633 *tstate = ahd->enabled_targets[ccb->ccb_h.target_id];
9635 if (*tstate != NULL)
9637 (*tstate)->enabled_luns[ccb->ccb_h.target_lun];
9640 if (notfound_failure != 0 && *lstate == NULL)
9641 return (CAM_PATH_INVALID);
9643 return (CAM_REQ_CMP);
9647 ahd_handle_en_lun(struct ahd_softc *ahd, struct cam_sim *sim, union ccb *ccb)
9650 struct ahd_tmode_tstate *tstate;
9651 struct ahd_tmode_lstate *lstate;
9652 struct ccb_en_lun *cel;
9660 status = ahd_find_tmode_devs(ahd, sim, ccb, &tstate, &lstate,
9661 /*notfound_failure*/FALSE);
9663 if (status != CAM_REQ_CMP) {
9664 ccb->ccb_h.status = status;
9668 if ((ahd->features & AHD_MULTIROLE) != 0) {
9671 our_id = ahd->our_id;
9672 if (ccb->ccb_h.target_id != our_id) {
9673 if ((ahd->features & AHD_MULTI_TID) != 0
9674 && (ahd->flags & AHD_INITIATORROLE) != 0) {
9676 * Only allow additional targets if
9677 * the initiator role is disabled.
9678 * The hardware cannot handle a re-select-in
9679 * on the initiator id during a re-select-out
9680 * on a different target id.
9682 status = CAM_TID_INVALID;
9683 } else if ((ahd->flags & AHD_INITIATORROLE) != 0
9684 || ahd->enabled_luns > 0) {
9686 * Only allow our target id to change
9687 * if the initiator role is not configured
9688 * and there are no enabled luns which
9689 * are attached to the currently registered
9692 status = CAM_TID_INVALID;
9697 if (status != CAM_REQ_CMP) {
9698 ccb->ccb_h.status = status;
9703 * We now have an id that is valid.
9704 * If we aren't in target mode, switch modes.
9706 if ((ahd->flags & AHD_TARGETROLE) == 0
9707 && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
9710 kprintf("Configuring Target Mode\n");
9712 if (LIST_FIRST(&ahd->pending_scbs) != NULL) {
9713 ccb->ccb_h.status = CAM_BUSY;
9717 ahd->flags |= AHD_TARGETROLE;
9718 if ((ahd->features & AHD_MULTIROLE) == 0)
9719 ahd->flags &= ~AHD_INITIATORROLE;
9726 target = ccb->ccb_h.target_id;
9727 lun = ccb->ccb_h.target_lun;
9728 channel = SIM_CHANNEL(ahd, sim);
9729 target_mask = 0x01 << target;
9733 if (cel->enable != 0) {
9736 /* Are we already enabled?? */
9737 if (lstate != NULL) {
9738 xpt_print_path(ccb->ccb_h.path);
9739 kprintf("Lun already enabled\n");
9740 ccb->ccb_h.status = CAM_LUN_ALRDY_ENA;
9744 if (cel->grp6_len != 0
9745 || cel->grp7_len != 0) {
9747 * Don't (yet?) support vendor
9748 * specific commands.
9750 ccb->ccb_h.status = CAM_REQ_INVALID;
9751 kprintf("Non-zero Group Codes\n");
9757 * Setup our data structures.
9759 if (target != CAM_TARGET_WILDCARD && tstate == NULL) {
9760 tstate = ahd_alloc_tstate(ahd, target, channel);
9761 if (tstate == NULL) {
9762 xpt_print_path(ccb->ccb_h.path);
9763 kprintf("Couldn't allocate tstate\n");
9764 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
9768 lstate = kmalloc(sizeof(*lstate), M_DEVBUF, M_INTWAIT | M_ZERO);
9769 status = xpt_create_path(&lstate->path, /*periph*/NULL,
9770 xpt_path_path_id(ccb->ccb_h.path),
9771 xpt_path_target_id(ccb->ccb_h.path),
9772 xpt_path_lun_id(ccb->ccb_h.path));
9773 if (status != CAM_REQ_CMP) {
9774 kfree(lstate, M_DEVBUF);
9775 xpt_print_path(ccb->ccb_h.path);
9776 kprintf("Couldn't allocate path\n");
9777 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
9780 SLIST_INIT(&lstate->accept_tios);
9781 SLIST_INIT(&lstate->immed_notifies);
9784 if (target != CAM_TARGET_WILDCARD) {
9785 tstate->enabled_luns[lun] = lstate;
9786 ahd->enabled_luns++;
9788 if ((ahd->features & AHD_MULTI_TID) != 0) {
9791 targid_mask = ahd_inw(ahd, TARGID);
9792 targid_mask |= target_mask;
9793 ahd_outw(ahd, TARGID, targid_mask);
9794 ahd_update_scsiid(ahd, targid_mask);
9799 channel = SIM_CHANNEL(ahd, sim);
9800 our_id = SIM_SCSI_ID(ahd, sim);
9803 * This can only happen if selections
9806 if (target != our_id) {
9811 sblkctl = ahd_inb(ahd, SBLKCTL);
9812 cur_channel = (sblkctl & SELBUSB)
9814 if ((ahd->features & AHD_TWIN) == 0)
9816 swap = cur_channel != channel;
9817 ahd->our_id = target;
9820 ahd_outb(ahd, SBLKCTL,
9823 ahd_outb(ahd, SCSIID, target);
9826 ahd_outb(ahd, SBLKCTL, sblkctl);
9830 ahd->black_hole = lstate;
9831 /* Allow select-in operations */
9832 if (ahd->black_hole != NULL && ahd->enabled_luns > 0) {
9833 scsiseq1 = ahd_inb(ahd, SCSISEQ_TEMPLATE);
9835 ahd_outb(ahd, SCSISEQ_TEMPLATE, scsiseq1);
9836 scsiseq1 = ahd_inb(ahd, SCSISEQ1);
9838 ahd_outb(ahd, SCSISEQ1, scsiseq1);
9842 ccb->ccb_h.status = CAM_REQ_CMP;
9843 xpt_print_path(ccb->ccb_h.path);
9844 kprintf("Lun now enabled for target mode\n");
9849 if (lstate == NULL) {
9850 ccb->ccb_h.status = CAM_LUN_INVALID;
9856 ccb->ccb_h.status = CAM_REQ_CMP;
9857 LIST_FOREACH(scb, &ahd->pending_scbs, pending_links) {
9858 struct ccb_hdr *ccbh;
9860 ccbh = &scb->io_ctx->ccb_h;
9861 if (ccbh->func_code == XPT_CONT_TARGET_IO
9862 && !xpt_path_comp(ccbh->path, ccb->ccb_h.path)){
9863 kprintf("CTIO pending\n");
9864 ccb->ccb_h.status = CAM_REQ_INVALID;
9870 if (SLIST_FIRST(&lstate->accept_tios) != NULL) {
9871 kprintf("ATIOs pending\n");
9872 ccb->ccb_h.status = CAM_REQ_INVALID;
9875 if (SLIST_FIRST(&lstate->immed_notifies) != NULL) {
9876 kprintf("INOTs pending\n");
9877 ccb->ccb_h.status = CAM_REQ_INVALID;
9880 if (ccb->ccb_h.status != CAM_REQ_CMP) {
9885 xpt_print_path(ccb->ccb_h.path);
9886 kprintf("Target mode disabled\n");
9887 xpt_free_path(lstate->path);
9888 kfree(lstate, M_DEVBUF);
9891 /* Can we clean up the target too? */
9892 if (target != CAM_TARGET_WILDCARD) {
9893 tstate->enabled_luns[lun] = NULL;
9894 ahd->enabled_luns--;
9895 for (empty = 1, i = 0; i < 8; i++)
9896 if (tstate->enabled_luns[i] != NULL) {
9902 ahd_free_tstate(ahd, target, channel,
9904 if (ahd->features & AHD_MULTI_TID) {
9907 targid_mask = ahd_inw(ahd, TARGID);
9908 targid_mask &= ~target_mask;
9909 ahd_outw(ahd, TARGID, targid_mask);
9910 ahd_update_scsiid(ahd, targid_mask);
9915 ahd->black_hole = NULL;
9918 * We can't allow selections without
9919 * our black hole device.
9923 if (ahd->enabled_luns == 0) {
9924 /* Disallow select-in */
9927 scsiseq1 = ahd_inb(ahd, SCSISEQ_TEMPLATE);
9928 scsiseq1 &= ~ENSELI;
9929 ahd_outb(ahd, SCSISEQ_TEMPLATE, scsiseq1);
9930 scsiseq1 = ahd_inb(ahd, SCSISEQ1);
9931 scsiseq1 &= ~ENSELI;
9932 ahd_outb(ahd, SCSISEQ1, scsiseq1);
9934 if ((ahd->features & AHD_MULTIROLE) == 0) {
9935 kprintf("Configuring Initiator Mode\n");
9936 ahd->flags &= ~AHD_TARGETROLE;
9937 ahd->flags |= AHD_INITIATORROLE;
9942 * Unpaused. The extra unpause
9943 * that follows is harmless.
9954 ahd_update_scsiid(struct ahd_softc *ahd, u_int targid_mask)
9960 if ((ahd->features & AHD_MULTI_TID) == 0)
9961 panic("ahd_update_scsiid called on non-multitid unit\n");
9964 * Since we will rely on the TARGID mask
9965 * for selection enables, ensure that OID
9966 * in SCSIID is not set to some other ID
9967 * that we don't want to allow selections on.
9969 if ((ahd->features & AHD_ULTRA2) != 0)
9970 scsiid = ahd_inb(ahd, SCSIID_ULTRA2);
9972 scsiid = ahd_inb(ahd, SCSIID);
9973 scsiid_mask = 0x1 << (scsiid & OID);
9974 if ((targid_mask & scsiid_mask) == 0) {
9977 /* ffs counts from 1 */
9978 our_id = ffs(targid_mask);
9980 our_id = ahd->our_id;
9986 if ((ahd->features & AHD_ULTRA2) != 0)
9987 ahd_outb(ahd, SCSIID_ULTRA2, scsiid);
9989 ahd_outb(ahd, SCSIID, scsiid);
9994 ahd_run_tqinfifo(struct ahd_softc *ahd, int paused)
9996 struct target_cmd *cmd;
9998 ahd_sync_tqinfifo(ahd, BUS_DMASYNC_POSTREAD);
9999 while ((cmd = &ahd->targetcmds[ahd->tqinfifonext])->cmd_valid != 0) {
10002 * Only advance through the queue if we
10003 * have the resources to process the command.
10005 if (ahd_handle_target_cmd(ahd, cmd) != 0)
10008 cmd->cmd_valid = 0;
10009 ahd_dmamap_sync(ahd, ahd->shared_data_dmat,
10010 ahd->shared_data_dmamap,
10011 ahd_targetcmd_offset(ahd, ahd->tqinfifonext),
10012 sizeof(struct target_cmd),
10013 BUS_DMASYNC_PREREAD);
10014 ahd->tqinfifonext++;
10017 * Lazily update our position in the target mode incoming
10018 * command queue as seen by the sequencer.
10020 if ((ahd->tqinfifonext & (HOST_TQINPOS - 1)) == 1) {
10023 hs_mailbox = ahd_inb(ahd, HS_MAILBOX);
10024 hs_mailbox &= ~HOST_TQINPOS;
10025 hs_mailbox |= ahd->tqinfifonext & HOST_TQINPOS;
10026 ahd_outb(ahd, HS_MAILBOX, hs_mailbox);
10032 ahd_handle_target_cmd(struct ahd_softc *ahd, struct target_cmd *cmd)
10034 struct ahd_tmode_tstate *tstate;
10035 struct ahd_tmode_lstate *lstate;
10036 struct ccb_accept_tio *atio;
10042 initiator = SCSIID_TARGET(ahd, cmd->scsiid);
10043 target = SCSIID_OUR_ID(cmd->scsiid);
10044 lun = (cmd->identify & MSG_IDENTIFY_LUNMASK);
10047 tstate = ahd->enabled_targets[target];
10049 if (tstate != NULL)
10050 lstate = tstate->enabled_luns[lun];
10053 * Commands for disabled luns go to the black hole driver.
10055 if (lstate == NULL)
10056 lstate = ahd->black_hole;
10058 atio = (struct ccb_accept_tio*)SLIST_FIRST(&lstate->accept_tios);
10059 if (atio == NULL) {
10060 ahd->flags |= AHD_TQINFIFO_BLOCKED;
10062 * Wait for more ATIOs from the peripheral driver for this lun.
10066 ahd->flags &= ~AHD_TQINFIFO_BLOCKED;
10068 if ((ahd_debug & AHD_SHOW_TQIN) != 0)
10069 kprintf("Incoming command from %d for %d:%d%s\n",
10070 initiator, target, lun,
10071 lstate == ahd->black_hole ? "(Black Holed)" : "");
10073 SLIST_REMOVE_HEAD(&lstate->accept_tios, sim_links.sle);
10075 if (lstate == ahd->black_hole) {
10076 /* Fill in the wildcards */
10077 atio->ccb_h.target_id = target;
10078 atio->ccb_h.target_lun = lun;
10082 * Package it up and send it off to
10083 * whomever has this lun enabled.
10085 atio->sense_len = 0;
10086 atio->init_id = initiator;
10087 if (byte[0] != 0xFF) {
10088 /* Tag was included */
10089 atio->tag_action = *byte++;
10090 atio->tag_id = *byte++;
10091 atio->ccb_h.flags = CAM_TAG_ACTION_VALID;
10093 atio->ccb_h.flags = 0;
10097 /* Okay. Now determine the cdb size based on the command code */
10098 switch (*byte >> CMD_GROUP_CODE_SHIFT) {
10104 atio->cdb_len = 10;
10107 atio->cdb_len = 16;
10110 atio->cdb_len = 12;
10114 /* Only copy the opcode. */
10116 kprintf("Reserved or VU command code type encountered\n");
10120 memcpy(atio->cdb_io.cdb_bytes, byte, atio->cdb_len);
10122 atio->ccb_h.status |= CAM_CDB_RECVD;
10124 if ((cmd->identify & MSG_IDENTIFY_DISCFLAG) == 0) {
10126 * We weren't allowed to disconnect.
10127 * We're hanging on the bus until a
10128 * continue target I/O comes in response
10129 * to this accept tio.
10132 if ((ahd_debug & AHD_SHOW_TQIN) != 0)
10133 kprintf("Received Immediate Command %d:%d:%d - %p\n",
10134 initiator, target, lun, ahd->pending_device);
10136 ahd->pending_device = lstate;
10137 ahd_freeze_ccb((union ccb *)atio);
10138 atio->ccb_h.flags |= CAM_DIS_DISCONNECT;
10140 xpt_done((union ccb*)atio);