2 * Core routines and tables shareable across OS platforms.
4 * Copyright (c) 1994-2002 Justin T. Gibbs.
5 * Copyright (c) 2000-2002 Adaptec Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions, and the following disclaimer,
13 * without modification.
14 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
15 * substantially similar to the "NO WARRANTY" disclaimer below
16 * ("Disclaimer") and any redistribution must be conditioned upon
17 * including a substantially similar Disclaimer requirement for further
18 * binary redistribution.
19 * 3. Neither the names of the above-listed copyright holders nor the names
20 * of any contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
23 * Alternatively, this software may be distributed under the terms of the
24 * GNU General Public License ("GPL") version 2 as published by the Free
25 * Software Foundation.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
37 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGES.
40 * $Id: //depot/aic7xxx/aic7xxx/aic7xxx.c#155 $
44 #include "aic7xxx_osm.h"
45 #include "aic7xxx_inline.h"
46 #include "aicasm/aicasm_insformat.h"
48 #include <dev/aic7xxx/aic7xxx_osm.h>
49 #include <dev/aic7xxx/aic7xxx_inline.h>
50 #include <dev/aic7xxx/aicasm/aicasm_insformat.h>
53 /***************************** Lookup Tables **********************************/
54 static const char *const ahc_chip_names[] = {
70 static const u_int num_chip_names = ARRAY_SIZE(ahc_chip_names);
73 * Hardware error codes.
75 struct ahc_hard_error_entry {
80 static const struct ahc_hard_error_entry ahc_hard_errors[] = {
81 { ILLHADDR, "Illegal Host Access" },
82 { ILLSADDR, "Illegal Sequencer Address referrenced" },
83 { ILLOPCODE, "Illegal Opcode in sequencer program" },
84 { SQPARERR, "Sequencer Parity Error" },
85 { DPARERR, "Data-path Parity Error" },
86 { MPARERR, "Scratch or SCB Memory Parity Error" },
87 { PCIERRSTAT, "PCI Error detected" },
88 { CIOPARERR, "CIOBUS Parity Error" },
90 static const u_int num_errors = ARRAY_SIZE(ahc_hard_errors);
92 static const struct ahc_phase_table_entry ahc_phase_table[] =
94 { P_DATAOUT, MSG_NOOP, "in Data-out phase" },
95 { P_DATAIN, MSG_INITIATOR_DET_ERR, "in Data-in phase" },
96 { P_DATAOUT_DT, MSG_NOOP, "in DT Data-out phase" },
97 { P_DATAIN_DT, MSG_INITIATOR_DET_ERR, "in DT Data-in phase" },
98 { P_COMMAND, MSG_NOOP, "in Command phase" },
99 { P_MESGOUT, MSG_NOOP, "in Message-out phase" },
100 { P_STATUS, MSG_INITIATOR_DET_ERR, "in Status phase" },
101 { P_MESGIN, MSG_PARITY_ERROR, "in Message-in phase" },
102 { P_BUSFREE, MSG_NOOP, "while idle" },
103 { 0, MSG_NOOP, "in unknown phase" }
107 * In most cases we only wish to itterate over real phases, so
108 * exclude the last element from the count.
110 static const u_int num_phases = ARRAY_SIZE(ahc_phase_table) - 1;
113 * Valid SCSIRATE values. (p. 3-17)
114 * Provides a mapping of tranfer periods in ns to the proper value to
115 * stick in the scsixfer reg.
117 static const struct ahc_syncrate ahc_syncrates[] =
119 /* ultra2 fast/ultra period rate */
120 { 0x42, 0x000, 9, "80.0" },
121 { 0x03, 0x000, 10, "40.0" },
122 { 0x04, 0x000, 11, "33.0" },
123 { 0x05, 0x100, 12, "20.0" },
124 { 0x06, 0x110, 15, "16.0" },
125 { 0x07, 0x120, 18, "13.4" },
126 { 0x08, 0x000, 25, "10.0" },
127 { 0x19, 0x010, 31, "8.0" },
128 { 0x1a, 0x020, 37, "6.67" },
129 { 0x1b, 0x030, 43, "5.7" },
130 { 0x1c, 0x040, 50, "5.0" },
131 { 0x00, 0x050, 56, "4.4" },
132 { 0x00, 0x060, 62, "4.0" },
133 { 0x00, 0x070, 68, "3.6" },
134 { 0x00, 0x000, 0, NULL }
137 /* Our Sequencer Program */
138 #include "aic7xxx_seq.h"
140 /**************************** Function Declarations ***************************/
141 static void ahc_force_renegotiation(struct ahc_softc *ahc,
142 struct ahc_devinfo *devinfo);
143 static struct ahc_tmode_tstate*
144 ahc_alloc_tstate(struct ahc_softc *ahc,
145 u_int scsi_id, char channel);
146 #ifdef AHC_TARGET_MODE
147 static void ahc_free_tstate(struct ahc_softc *ahc,
148 u_int scsi_id, char channel, int force);
150 static const struct ahc_syncrate*
151 ahc_devlimited_syncrate(struct ahc_softc *ahc,
152 struct ahc_initiator_tinfo *,
156 static void ahc_update_pending_scbs(struct ahc_softc *ahc);
157 static void ahc_fetch_devinfo(struct ahc_softc *ahc,
158 struct ahc_devinfo *devinfo);
159 static void ahc_scb_devinfo(struct ahc_softc *ahc,
160 struct ahc_devinfo *devinfo,
162 static void ahc_assert_atn(struct ahc_softc *ahc);
163 static void ahc_setup_initiator_msgout(struct ahc_softc *ahc,
164 struct ahc_devinfo *devinfo,
166 static void ahc_build_transfer_msg(struct ahc_softc *ahc,
167 struct ahc_devinfo *devinfo);
168 static void ahc_construct_sdtr(struct ahc_softc *ahc,
169 struct ahc_devinfo *devinfo,
170 u_int period, u_int offset);
171 static void ahc_construct_wdtr(struct ahc_softc *ahc,
172 struct ahc_devinfo *devinfo,
174 static void ahc_construct_ppr(struct ahc_softc *ahc,
175 struct ahc_devinfo *devinfo,
176 u_int period, u_int offset,
177 u_int bus_width, u_int ppr_options);
178 static void ahc_clear_msg_state(struct ahc_softc *ahc);
179 static void ahc_handle_proto_violation(struct ahc_softc *ahc);
180 static void ahc_handle_message_phase(struct ahc_softc *ahc);
186 static int ahc_sent_msg(struct ahc_softc *ahc, ahc_msgtype type,
187 u_int msgval, int full);
188 static int ahc_parse_msg(struct ahc_softc *ahc,
189 struct ahc_devinfo *devinfo);
190 static int ahc_handle_msg_reject(struct ahc_softc *ahc,
191 struct ahc_devinfo *devinfo);
192 static void ahc_handle_ign_wide_residue(struct ahc_softc *ahc,
193 struct ahc_devinfo *devinfo);
194 static void ahc_reinitialize_dataptrs(struct ahc_softc *ahc);
195 static void ahc_handle_devreset(struct ahc_softc *ahc,
196 struct ahc_devinfo *devinfo,
197 cam_status status, char *message,
199 #ifdef AHC_TARGET_MODE
200 static void ahc_setup_target_msgin(struct ahc_softc *ahc,
201 struct ahc_devinfo *devinfo,
205 static bus_dmamap_callback_t ahc_dmamap_cb;
206 static void ahc_build_free_scb_list(struct ahc_softc *ahc);
207 static int ahc_init_scbdata(struct ahc_softc *ahc);
208 static void ahc_fini_scbdata(struct ahc_softc *ahc);
209 static void ahc_qinfifo_requeue(struct ahc_softc *ahc,
210 struct scb *prev_scb,
212 static int ahc_qinfifo_count(struct ahc_softc *ahc);
213 static u_int ahc_rem_scb_from_disc_list(struct ahc_softc *ahc,
214 u_int prev, u_int scbptr);
215 static void ahc_add_curscb_to_free_list(struct ahc_softc *ahc);
216 static u_int ahc_rem_wscb(struct ahc_softc *ahc,
217 u_int scbpos, u_int prev);
218 static void ahc_reset_current_bus(struct ahc_softc *ahc);
220 static void ahc_dumpseq(struct ahc_softc *ahc);
222 static int ahc_loadseq(struct ahc_softc *ahc);
223 static int ahc_check_patch(struct ahc_softc *ahc,
224 const struct patch **start_patch,
225 u_int start_instr, u_int *skip_addr);
226 static void ahc_download_instr(struct ahc_softc *ahc,
227 u_int instrptr, uint8_t *dconsts);
228 #ifdef AHC_TARGET_MODE
229 static void ahc_queue_lstate_event(struct ahc_softc *ahc,
230 struct ahc_tmode_lstate *lstate,
234 static void ahc_update_scsiid(struct ahc_softc *ahc,
236 static int ahc_handle_target_cmd(struct ahc_softc *ahc,
237 struct target_cmd *cmd);
240 static u_int ahc_index_busy_tcl(struct ahc_softc *ahc, u_int tcl);
241 static void ahc_unbusy_tcl(struct ahc_softc *ahc, u_int tcl);
242 static void ahc_busy_tcl(struct ahc_softc *ahc,
243 u_int tcl, u_int busyid);
245 /************************** SCB and SCB queue management **********************/
246 static void ahc_run_untagged_queues(struct ahc_softc *ahc);
247 static void ahc_run_untagged_queue(struct ahc_softc *ahc,
248 struct scb_tailq *queue);
250 /****************************** Initialization ********************************/
251 static void ahc_alloc_scbs(struct ahc_softc *ahc);
252 static void ahc_shutdown(void *arg);
254 /*************************** Interrupt Services *******************************/
255 static void ahc_clear_intstat(struct ahc_softc *ahc);
256 static void ahc_run_qoutfifo(struct ahc_softc *ahc);
257 #ifdef AHC_TARGET_MODE
258 static void ahc_run_tqinfifo(struct ahc_softc *ahc, int paused);
260 static void ahc_handle_brkadrint(struct ahc_softc *ahc);
261 static void ahc_handle_seqint(struct ahc_softc *ahc, u_int intstat);
262 static void ahc_handle_scsiint(struct ahc_softc *ahc,
264 static void ahc_clear_critical_section(struct ahc_softc *ahc);
266 /***************************** Error Recovery *********************************/
267 static void ahc_freeze_devq(struct ahc_softc *ahc, struct scb *scb);
268 static int ahc_abort_scbs(struct ahc_softc *ahc, int target,
269 char channel, int lun, u_int tag,
270 role_t role, uint32_t status);
271 static void ahc_calc_residual(struct ahc_softc *ahc,
274 /*********************** Untagged Transaction Routines ************************/
275 static inline void ahc_freeze_untagged_queues(struct ahc_softc *ahc);
276 static inline void ahc_release_untagged_queues(struct ahc_softc *ahc);
279 * Block our completion routine from starting the next untagged
280 * transaction for this target or target lun.
283 ahc_freeze_untagged_queues(struct ahc_softc *ahc)
285 if ((ahc->flags & AHC_SCB_BTT) == 0)
286 ahc->untagged_queue_lock++;
290 * Allow the next untagged transaction for this target or target lun
291 * to be executed. We use a counting semaphore to allow the lock
292 * to be acquired recursively. Once the count drops to zero, the
293 * transaction queues will be run.
296 ahc_release_untagged_queues(struct ahc_softc *ahc)
298 if ((ahc->flags & AHC_SCB_BTT) == 0) {
299 ahc->untagged_queue_lock--;
300 if (ahc->untagged_queue_lock == 0)
301 ahc_run_untagged_queues(ahc);
305 /************************* Sequencer Execution Control ************************/
307 * Work around any chip bugs related to halting sequencer execution.
308 * On Ultra2 controllers, we must clear the CIOBUS stretch signal by
309 * reading a register that will set this signal and deassert it.
310 * Without this workaround, if the chip is paused, by an interrupt or
311 * manual pause while accessing scb ram, accesses to certain registers
312 * will hang the system (infinite pci retries).
315 ahc_pause_bug_fix(struct ahc_softc *ahc)
317 if ((ahc->features & AHC_ULTRA2) != 0)
318 (void)ahc_inb(ahc, CCSCBCTL);
322 * Determine whether the sequencer has halted code execution.
323 * Returns non-zero status if the sequencer is stopped.
326 ahc_is_paused(struct ahc_softc *ahc)
328 return ((ahc_inb(ahc, HCNTRL) & PAUSE) != 0);
332 * Request that the sequencer stop and wait, indefinitely, for it
333 * to stop. The sequencer will only acknowledge that it is paused
334 * once it has reached an instruction boundary and PAUSEDIS is
335 * cleared in the SEQCTL register. The sequencer may use PAUSEDIS
336 * for critical sections.
339 ahc_pause(struct ahc_softc *ahc)
341 ahc_outb(ahc, HCNTRL, ahc->pause);
344 * Since the sequencer can disable pausing in a critical section, we
345 * must loop until it actually stops.
347 while (ahc_is_paused(ahc) == 0)
350 ahc_pause_bug_fix(ahc);
354 * Allow the sequencer to continue program execution.
355 * We check here to ensure that no additional interrupt
356 * sources that would cause the sequencer to halt have been
357 * asserted. If, for example, a SCSI bus reset is detected
358 * while we are fielding a different, pausing, interrupt type,
359 * we don't want to release the sequencer before going back
360 * into our interrupt handler and dealing with this new
364 ahc_unpause(struct ahc_softc *ahc)
366 if ((ahc_inb(ahc, INTSTAT) & (SCSIINT | SEQINT | BRKADRINT)) == 0)
367 ahc_outb(ahc, HCNTRL, ahc->unpause);
370 /************************** Memory mapping routines ***************************/
371 static struct ahc_dma_seg *
372 ahc_sg_bus_to_virt(struct scb *scb, uint32_t sg_busaddr)
376 sg_index = (sg_busaddr - scb->sg_list_phys)/sizeof(struct ahc_dma_seg);
377 /* sg_list_phys points to entry 1, not 0 */
380 return (&scb->sg_list[sg_index]);
384 ahc_sg_virt_to_bus(struct scb *scb, struct ahc_dma_seg *sg)
388 /* sg_list_phys points to entry 1, not 0 */
389 sg_index = sg - &scb->sg_list[1];
391 return (scb->sg_list_phys + (sg_index * sizeof(*scb->sg_list)));
395 ahc_hscb_busaddr(struct ahc_softc *ahc, u_int index)
397 return (ahc->scb_data->hscb_busaddr
398 + (sizeof(struct hardware_scb) * index));
402 ahc_sync_scb(struct ahc_softc *ahc, struct scb *scb, int op)
404 ahc_dmamap_sync(ahc, ahc->scb_data->hscb_dmat,
405 ahc->scb_data->hscb_dmamap,
406 /*offset*/(scb->hscb - ahc->hscbs) * sizeof(*scb->hscb),
407 /*len*/sizeof(*scb->hscb), op);
411 ahc_sync_sglist(struct ahc_softc *ahc, struct scb *scb, int op)
413 if (scb->sg_count == 0)
416 ahc_dmamap_sync(ahc, ahc->scb_data->sg_dmat, scb->sg_map->sg_dmamap,
417 /*offset*/(scb->sg_list - scb->sg_map->sg_vaddr)
418 * sizeof(struct ahc_dma_seg),
419 /*len*/sizeof(struct ahc_dma_seg) * scb->sg_count, op);
422 #ifdef AHC_TARGET_MODE
424 ahc_targetcmd_offset(struct ahc_softc *ahc, u_int index)
426 return (((uint8_t *)&ahc->targetcmds[index]) - ahc->qoutfifo);
430 /*********************** Miscelaneous Support Functions ***********************/
432 * Determine whether the sequencer reported a residual
433 * for this SCB/transaction.
436 ahc_update_residual(struct ahc_softc *ahc, struct scb *scb)
440 sgptr = ahc_le32toh(scb->hscb->sgptr);
441 if ((sgptr & SG_RESID_VALID) != 0)
442 ahc_calc_residual(ahc, scb);
446 * Return pointers to the transfer negotiation information
447 * for the specified our_id/remote_id pair.
449 struct ahc_initiator_tinfo *
450 ahc_fetch_transinfo(struct ahc_softc *ahc, char channel, u_int our_id,
451 u_int remote_id, struct ahc_tmode_tstate **tstate)
454 * Transfer data structures are stored from the perspective
455 * of the target role. Since the parameters for a connection
456 * in the initiator role to a given target are the same as
457 * when the roles are reversed, we pretend we are the target.
461 *tstate = ahc->enabled_targets[our_id];
462 return (&(*tstate)->transinfo[remote_id]);
466 ahc_inw(struct ahc_softc *ahc, u_int port)
468 uint16_t r = ahc_inb(ahc, port+1) << 8;
469 return r | ahc_inb(ahc, port);
473 ahc_outw(struct ahc_softc *ahc, u_int port, u_int value)
475 ahc_outb(ahc, port, value & 0xFF);
476 ahc_outb(ahc, port+1, (value >> 8) & 0xFF);
480 ahc_inl(struct ahc_softc *ahc, u_int port)
482 return ((ahc_inb(ahc, port))
483 | (ahc_inb(ahc, port+1) << 8)
484 | (ahc_inb(ahc, port+2) << 16)
485 | (ahc_inb(ahc, port+3) << 24));
489 ahc_outl(struct ahc_softc *ahc, u_int port, uint32_t value)
491 ahc_outb(ahc, port, (value) & 0xFF);
492 ahc_outb(ahc, port+1, ((value) >> 8) & 0xFF);
493 ahc_outb(ahc, port+2, ((value) >> 16) & 0xFF);
494 ahc_outb(ahc, port+3, ((value) >> 24) & 0xFF);
498 ahc_inq(struct ahc_softc *ahc, u_int port)
500 return ((ahc_inb(ahc, port))
501 | (ahc_inb(ahc, port+1) << 8)
502 | (ahc_inb(ahc, port+2) << 16)
503 | (ahc_inb(ahc, port+3) << 24)
504 | (((uint64_t)ahc_inb(ahc, port+4)) << 32)
505 | (((uint64_t)ahc_inb(ahc, port+5)) << 40)
506 | (((uint64_t)ahc_inb(ahc, port+6)) << 48)
507 | (((uint64_t)ahc_inb(ahc, port+7)) << 56));
511 ahc_outq(struct ahc_softc *ahc, u_int port, uint64_t value)
513 ahc_outb(ahc, port, value & 0xFF);
514 ahc_outb(ahc, port+1, (value >> 8) & 0xFF);
515 ahc_outb(ahc, port+2, (value >> 16) & 0xFF);
516 ahc_outb(ahc, port+3, (value >> 24) & 0xFF);
517 ahc_outb(ahc, port+4, (value >> 32) & 0xFF);
518 ahc_outb(ahc, port+5, (value >> 40) & 0xFF);
519 ahc_outb(ahc, port+6, (value >> 48) & 0xFF);
520 ahc_outb(ahc, port+7, (value >> 56) & 0xFF);
524 * Get a free scb. If there are none, see if we can allocate a new SCB.
527 ahc_get_scb(struct ahc_softc *ahc)
531 if ((scb = SLIST_FIRST(&ahc->scb_data->free_scbs)) == NULL) {
533 scb = SLIST_FIRST(&ahc->scb_data->free_scbs);
537 SLIST_REMOVE_HEAD(&ahc->scb_data->free_scbs, links.sle);
542 * Return an SCB resource to the free list.
545 ahc_free_scb(struct ahc_softc *ahc, struct scb *scb)
547 struct hardware_scb *hscb;
550 /* Clean up for the next user */
551 ahc->scb_data->scbindex[hscb->tag] = NULL;
552 scb->flags = SCB_FREE;
555 SLIST_INSERT_HEAD(&ahc->scb_data->free_scbs, scb, links.sle);
557 /* Notify the OSM that a resource is now available. */
558 ahc_platform_scb_free(ahc, scb);
562 ahc_lookup_scb(struct ahc_softc *ahc, u_int tag)
566 scb = ahc->scb_data->scbindex[tag];
568 ahc_sync_scb(ahc, scb,
569 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
574 ahc_swap_with_next_hscb(struct ahc_softc *ahc, struct scb *scb)
576 struct hardware_scb *q_hscb;
580 * Our queuing method is a bit tricky. The card
581 * knows in advance which HSCB to download, and we
582 * can't disappoint it. To achieve this, the next
583 * SCB to download is saved off in ahc->next_queued_scb.
584 * When we are called to queue "an arbitrary scb",
585 * we copy the contents of the incoming HSCB to the one
586 * the sequencer knows about, swap HSCB pointers and
587 * finally assign the SCB to the tag indexed location
588 * in the scb_array. This makes sure that we can still
589 * locate the correct SCB by SCB_TAG.
591 q_hscb = ahc->next_queued_scb->hscb;
592 saved_tag = q_hscb->tag;
593 memcpy(q_hscb, scb->hscb, sizeof(*scb->hscb));
594 if ((scb->flags & SCB_CDB32_PTR) != 0) {
595 q_hscb->shared_data.cdb_ptr =
596 ahc_htole32(ahc_hscb_busaddr(ahc, q_hscb->tag)
597 + offsetof(struct hardware_scb, cdb32));
599 q_hscb->tag = saved_tag;
600 q_hscb->next = scb->hscb->tag;
602 /* Now swap HSCB pointers. */
603 ahc->next_queued_scb->hscb = scb->hscb;
606 /* Now define the mapping from tag to SCB in the scbindex */
607 ahc->scb_data->scbindex[scb->hscb->tag] = scb;
611 * Tell the sequencer about a new transaction to execute.
614 ahc_queue_scb(struct ahc_softc *ahc, struct scb *scb)
616 ahc_swap_with_next_hscb(ahc, scb);
618 if (scb->hscb->tag == SCB_LIST_NULL
619 || scb->hscb->next == SCB_LIST_NULL)
620 panic("Attempt to queue invalid SCB tag %x:%x\n",
621 scb->hscb->tag, scb->hscb->next);
624 * Setup data "oddness".
626 scb->hscb->lun &= LID;
627 if (ahc_get_transfer_length(scb) & 0x1)
628 scb->hscb->lun |= SCB_XFERLEN_ODD;
631 * Keep a history of SCBs we've downloaded in the qinfifo.
633 ahc->qinfifo[ahc->qinfifonext++] = scb->hscb->tag;
636 * Make sure our data is consistent from the
637 * perspective of the adapter.
639 ahc_sync_scb(ahc, scb, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
641 /* Tell the adapter about the newly queued SCB */
642 if ((ahc->features & AHC_QUEUE_REGS) != 0) {
643 ahc_outb(ahc, HNSCB_QOFF, ahc->qinfifonext);
645 if ((ahc->features & AHC_AUTOPAUSE) == 0)
647 ahc_outb(ahc, KERNEL_QINPOS, ahc->qinfifonext);
648 if ((ahc->features & AHC_AUTOPAUSE) == 0)
653 struct scsi_sense_data *
654 ahc_get_sense_buf(struct ahc_softc *ahc, struct scb *scb)
658 offset = scb - ahc->scb_data->scbarray;
659 return (&ahc->scb_data->sense[offset]);
663 ahc_get_sense_bufaddr(struct ahc_softc *ahc, struct scb *scb)
667 offset = scb - ahc->scb_data->scbarray;
668 return (ahc->scb_data->sense_busaddr
669 + (offset * sizeof(struct scsi_sense_data)));
672 /************************** Interrupt Processing ******************************/
674 ahc_sync_qoutfifo(struct ahc_softc *ahc, int op)
676 ahc_dmamap_sync(ahc, ahc->shared_data_dmat, ahc->shared_data_dmamap,
677 /*offset*/0, /*len*/256, op);
681 ahc_sync_tqinfifo(struct ahc_softc *ahc, int op)
683 #ifdef AHC_TARGET_MODE
684 if ((ahc->flags & AHC_TARGETROLE) != 0) {
685 ahc_dmamap_sync(ahc, ahc->shared_data_dmat,
686 ahc->shared_data_dmamap,
687 ahc_targetcmd_offset(ahc, 0),
688 sizeof(struct target_cmd) * AHC_TMODE_CMDS,
695 * See if the firmware has posted any completed commands
696 * into our in-core command complete fifos.
698 #define AHC_RUN_QOUTFIFO 0x1
699 #define AHC_RUN_TQINFIFO 0x2
701 ahc_check_cmdcmpltqueues(struct ahc_softc *ahc)
706 ahc_dmamap_sync(ahc, ahc->shared_data_dmat, ahc->shared_data_dmamap,
707 /*offset*/ahc->qoutfifonext, /*len*/1,
708 BUS_DMASYNC_POSTREAD);
709 if (ahc->qoutfifo[ahc->qoutfifonext] != SCB_LIST_NULL)
710 retval |= AHC_RUN_QOUTFIFO;
711 #ifdef AHC_TARGET_MODE
712 if ((ahc->flags & AHC_TARGETROLE) != 0
713 && (ahc->flags & AHC_TQINFIFO_BLOCKED) == 0) {
714 ahc_dmamap_sync(ahc, ahc->shared_data_dmat,
715 ahc->shared_data_dmamap,
716 ahc_targetcmd_offset(ahc, ahc->tqinfifofnext),
717 /*len*/sizeof(struct target_cmd),
718 BUS_DMASYNC_POSTREAD);
719 if (ahc->targetcmds[ahc->tqinfifonext].cmd_valid != 0)
720 retval |= AHC_RUN_TQINFIFO;
727 * Catch an interrupt from the adapter
730 ahc_intr(struct ahc_softc *ahc)
734 if ((ahc->pause & INTEN) == 0) {
736 * Our interrupt is not enabled on the chip
737 * and may be disabled for re-entrancy reasons,
738 * so just return. This is likely just a shared
744 * Instead of directly reading the interrupt status register,
745 * infer the cause of the interrupt by checking our in-core
746 * completion queues. This avoids a costly PCI bus read in
749 if ((ahc->flags & (AHC_ALL_INTERRUPTS|AHC_EDGE_INTERRUPT)) == 0
750 && (ahc_check_cmdcmpltqueues(ahc) != 0))
753 intstat = ahc_inb(ahc, INTSTAT);
756 if ((intstat & INT_PEND) == 0) {
757 #if AHC_PCI_CONFIG > 0
758 if (ahc->unsolicited_ints > 500) {
759 ahc->unsolicited_ints = 0;
760 if ((ahc->chip & AHC_PCI) != 0
761 && (ahc_inb(ahc, ERROR) & PCIERRSTAT) != 0)
765 ahc->unsolicited_ints++;
768 ahc->unsolicited_ints = 0;
770 if (intstat & CMDCMPLT) {
771 ahc_outb(ahc, CLRINT, CLRCMDINT);
774 * Ensure that the chip sees that we've cleared
775 * this interrupt before we walk the output fifo.
776 * Otherwise, we may, due to posted bus writes,
777 * clear the interrupt after we finish the scan,
778 * and after the sequencer has added new entries
779 * and asserted the interrupt again.
781 ahc_flush_device_writes(ahc);
782 ahc_run_qoutfifo(ahc);
783 #ifdef AHC_TARGET_MODE
784 if ((ahc->flags & AHC_TARGETROLE) != 0)
785 ahc_run_tqinfifo(ahc, /*paused*/FALSE);
790 * Handle statuses that may invalidate our cached
791 * copy of INTSTAT separately.
793 if (intstat == 0xFF && (ahc->features & AHC_REMOVABLE) != 0) {
794 /* Hot eject. Do nothing */
795 } else if (intstat & BRKADRINT) {
796 ahc_handle_brkadrint(ahc);
797 } else if ((intstat & (SEQINT|SCSIINT)) != 0) {
799 ahc_pause_bug_fix(ahc);
801 if ((intstat & SEQINT) != 0)
802 ahc_handle_seqint(ahc, intstat);
804 if ((intstat & SCSIINT) != 0)
805 ahc_handle_scsiint(ahc, intstat);
810 /************************* Sequencer Execution Control ************************/
812 * Restart the sequencer program from address zero
815 ahc_restart(struct ahc_softc *ahc)
820 /* No more pending messages. */
821 ahc_clear_msg_state(ahc);
823 ahc_outb(ahc, SCSISIGO, 0); /* De-assert BSY */
824 ahc_outb(ahc, MSG_OUT, MSG_NOOP); /* No message to send */
825 ahc_outb(ahc, SXFRCTL1, ahc_inb(ahc, SXFRCTL1) & ~BITBUCKET);
826 ahc_outb(ahc, LASTPHASE, P_BUSFREE);
827 ahc_outb(ahc, SAVED_SCSIID, 0xFF);
828 ahc_outb(ahc, SAVED_LUN, 0xFF);
831 * Ensure that the sequencer's idea of TQINPOS
832 * matches our own. The sequencer increments TQINPOS
833 * only after it sees a DMA complete and a reset could
834 * occur before the increment leaving the kernel to believe
835 * the command arrived but the sequencer to not.
837 ahc_outb(ahc, TQINPOS, ahc->tqinfifonext);
839 /* Always allow reselection */
840 ahc_outb(ahc, SCSISEQ,
841 ahc_inb(ahc, SCSISEQ_TEMPLATE) & (ENSELI|ENRSELI|ENAUTOATNP));
842 if ((ahc->features & AHC_CMD_CHAN) != 0) {
843 /* Ensure that no DMA operations are in progress */
844 ahc_outb(ahc, CCSCBCNT, 0);
845 ahc_outb(ahc, CCSGCTL, 0);
846 ahc_outb(ahc, CCSCBCTL, 0);
849 * If we were in the process of DMA'ing SCB data into
850 * an SCB, replace that SCB on the free list. This prevents
853 if ((ahc_inb(ahc, SEQ_FLAGS2) & SCB_DMA) != 0) {
854 ahc_add_curscb_to_free_list(ahc);
855 ahc_outb(ahc, SEQ_FLAGS2,
856 ahc_inb(ahc, SEQ_FLAGS2) & ~SCB_DMA);
860 * Clear any pending sequencer interrupt. It is no
861 * longer relevant since we're resetting the Program
864 ahc_outb(ahc, CLRINT, CLRSEQINT);
866 ahc_outb(ahc, MWI_RESIDUAL, 0);
867 ahc_outb(ahc, SEQCTL, ahc->seqctl);
868 ahc_outb(ahc, SEQADDR0, 0);
869 ahc_outb(ahc, SEQADDR1, 0);
874 /************************* Input/Output Queues ********************************/
876 ahc_run_qoutfifo(struct ahc_softc *ahc)
881 ahc_sync_qoutfifo(ahc, BUS_DMASYNC_POSTREAD);
882 while (ahc->qoutfifo[ahc->qoutfifonext] != SCB_LIST_NULL) {
884 scb_index = ahc->qoutfifo[ahc->qoutfifonext];
885 if ((ahc->qoutfifonext & 0x03) == 0x03) {
889 * Clear 32bits of QOUTFIFO at a time
890 * so that we don't clobber an incoming
891 * byte DMA to the array on architectures
892 * that only support 32bit load and store
895 modnext = ahc->qoutfifonext & ~0x3;
896 *((uint32_t *)(&ahc->qoutfifo[modnext])) = 0xFFFFFFFFUL;
897 ahc_dmamap_sync(ahc, ahc->shared_data_dmat,
898 ahc->shared_data_dmamap,
899 /*offset*/modnext, /*len*/4,
900 BUS_DMASYNC_PREREAD);
904 scb = ahc_lookup_scb(ahc, scb_index);
906 printf("%s: WARNING no command for scb %d "
907 "(cmdcmplt)\nQOUTPOS = %d\n",
908 ahc_name(ahc), scb_index,
909 (ahc->qoutfifonext - 1) & 0xFF);
914 * Save off the residual
917 ahc_update_residual(ahc, scb);
923 ahc_run_untagged_queues(struct ahc_softc *ahc)
927 for (i = 0; i < 16; i++)
928 ahc_run_untagged_queue(ahc, &ahc->untagged_queues[i]);
932 ahc_run_untagged_queue(struct ahc_softc *ahc, struct scb_tailq *queue)
936 if (ahc->untagged_queue_lock != 0)
939 if ((scb = TAILQ_FIRST(queue)) != NULL
940 && (scb->flags & SCB_ACTIVE) == 0) {
941 scb->flags |= SCB_ACTIVE;
942 ahc_queue_scb(ahc, scb);
946 /************************* Interrupt Handling *********************************/
948 ahc_handle_brkadrint(struct ahc_softc *ahc)
951 * We upset the sequencer :-(
952 * Lookup the error message
957 error = ahc_inb(ahc, ERROR);
958 for (i = 0; error != 1 && i < num_errors; i++)
960 printf("%s: brkadrint, %s at seqaddr = 0x%x\n",
961 ahc_name(ahc), ahc_hard_errors[i].errmesg,
962 ahc_inb(ahc, SEQADDR0) |
963 (ahc_inb(ahc, SEQADDR1) << 8));
965 ahc_dump_card_state(ahc);
967 /* Tell everyone that this HBA is no longer available */
968 ahc_abort_scbs(ahc, CAM_TARGET_WILDCARD, ALL_CHANNELS,
969 CAM_LUN_WILDCARD, SCB_LIST_NULL, ROLE_UNKNOWN,
972 /* Disable all interrupt sources by resetting the controller */
977 ahc_handle_seqint(struct ahc_softc *ahc, u_int intstat)
980 struct ahc_devinfo devinfo;
982 ahc_fetch_devinfo(ahc, &devinfo);
985 * Clear the upper byte that holds SEQINT status
986 * codes and clear the SEQINT bit. We will unpause
987 * the sequencer, if appropriate, after servicing
990 ahc_outb(ahc, CLRINT, CLRSEQINT);
991 switch (intstat & SEQINT_MASK) {
995 struct hardware_scb *hscb;
998 * Set the default return value to 0 (don't
999 * send sense). The sense code will change
1002 ahc_outb(ahc, RETURN_1, 0);
1005 * The sequencer will notify us when a command
1006 * has an error that would be of interest to
1007 * the kernel. This allows us to leave the sequencer
1008 * running in the common case of command completes
1009 * without error. The sequencer will already have
1010 * dma'd the SCB back up to us, so we can reference
1011 * the in kernel copy directly.
1013 scb_index = ahc_inb(ahc, SCB_TAG);
1014 scb = ahc_lookup_scb(ahc, scb_index);
1016 ahc_print_devinfo(ahc, &devinfo);
1017 printf("ahc_intr - referenced scb "
1018 "not valid during seqint 0x%x scb(%d)\n",
1019 intstat, scb_index);
1020 ahc_dump_card_state(ahc);
1021 panic("for safety");
1027 /* Don't want to clobber the original sense code */
1028 if ((scb->flags & SCB_SENSE) != 0) {
1030 * Clear the SCB_SENSE Flag and have
1031 * the sequencer do a normal command
1034 scb->flags &= ~SCB_SENSE;
1035 ahc_set_transaction_status(scb, CAM_AUTOSENSE_FAIL);
1038 ahc_set_transaction_status(scb, CAM_SCSI_STATUS_ERROR);
1039 /* Freeze the queue until the client sees the error. */
1040 ahc_freeze_devq(ahc, scb);
1041 ahc_freeze_scb(scb);
1042 ahc_set_scsi_status(scb, hscb->shared_data.status.scsi_status);
1043 switch (hscb->shared_data.status.scsi_status) {
1044 case SCSI_STATUS_OK:
1045 printf("%s: Interrupted for staus of 0???\n",
1048 case SCSI_STATUS_CMD_TERMINATED:
1049 case SCSI_STATUS_CHECK_COND:
1051 struct ahc_dma_seg *sg;
1052 struct scsi_sense *sc;
1053 struct ahc_initiator_tinfo *targ_info;
1054 struct ahc_tmode_tstate *tstate;
1055 struct ahc_transinfo *tinfo;
1057 if (ahc_debug & AHC_SHOW_SENSE) {
1058 ahc_print_path(ahc, scb);
1059 printf("SCB %d: requests Check Status\n",
1064 if (ahc_perform_autosense(scb) == 0)
1067 targ_info = ahc_fetch_transinfo(ahc,
1072 tinfo = &targ_info->curr;
1074 sc = (struct scsi_sense *)(&hscb->shared_data.cdb);
1076 * Save off the residual if there is one.
1078 ahc_update_residual(ahc, scb);
1080 if (ahc_debug & AHC_SHOW_SENSE) {
1081 ahc_print_path(ahc, scb);
1082 printf("Sending Sense\n");
1085 sg->addr = ahc_get_sense_bufaddr(ahc, scb);
1086 sg->len = ahc_get_sense_bufsize(ahc, scb);
1087 sg->len |= AHC_DMA_LAST_SEG;
1089 /* Fixup byte order */
1090 sg->addr = ahc_htole32(sg->addr);
1091 sg->len = ahc_htole32(sg->len);
1093 sc->opcode = REQUEST_SENSE;
1095 if (tinfo->protocol_version <= SCSI_REV_2
1096 && SCB_GET_LUN(scb) < 8)
1097 sc->byte2 = SCB_GET_LUN(scb) << 5;
1100 sc->length = sg->len;
1104 * We can't allow the target to disconnect.
1105 * This will be an untagged transaction and
1106 * having the target disconnect will make this
1107 * transaction indestinguishable from outstanding
1108 * tagged transactions.
1113 * This request sense could be because the
1114 * the device lost power or in some other
1115 * way has lost our transfer negotiations.
1116 * Renegotiate if appropriate. Unit attention
1117 * errors will be reported before any data
1120 if (ahc_get_residual(scb)
1121 == ahc_get_transfer_length(scb)) {
1122 ahc_update_neg_request(ahc, &devinfo,
1124 AHC_NEG_IF_NON_ASYNC);
1126 if (tstate->auto_negotiate & devinfo.target_mask) {
1127 hscb->control |= MK_MESSAGE;
1128 scb->flags &= ~SCB_NEGOTIATE;
1129 scb->flags |= SCB_AUTO_NEGOTIATE;
1131 hscb->cdb_len = sizeof(*sc);
1132 hscb->dataptr = sg->addr;
1133 hscb->datacnt = sg->len;
1134 hscb->sgptr = scb->sg_list_phys | SG_FULL_RESID;
1135 hscb->sgptr = ahc_htole32(hscb->sgptr);
1137 scb->flags |= SCB_SENSE;
1138 ahc_qinfifo_requeue_tail(ahc, scb);
1139 ahc_outb(ahc, RETURN_1, SEND_SENSE);
1141 * Ensure we have enough time to actually
1142 * retrieve the sense.
1144 ahc_scb_timer_reset(scb, 5 * 1000000);
1154 /* Ensure we don't leave the selection hardware on */
1155 ahc_outb(ahc, SCSISEQ,
1156 ahc_inb(ahc, SCSISEQ) & (ENSELI|ENRSELI|ENAUTOATNP));
1158 printf("%s:%c:%d: no active SCB for reconnecting "
1159 "target - issuing BUS DEVICE RESET\n",
1160 ahc_name(ahc), devinfo.channel, devinfo.target);
1161 printf("SAVED_SCSIID == 0x%x, SAVED_LUN == 0x%x, "
1162 "ARG_1 == 0x%x ACCUM = 0x%x\n",
1163 ahc_inb(ahc, SAVED_SCSIID), ahc_inb(ahc, SAVED_LUN),
1164 ahc_inb(ahc, ARG_1), ahc_inb(ahc, ACCUM));
1165 printf("SEQ_FLAGS == 0x%x, SCBPTR == 0x%x, BTT == 0x%x, "
1167 ahc_inb(ahc, SEQ_FLAGS), ahc_inb(ahc, SCBPTR),
1168 ahc_index_busy_tcl(ahc,
1169 BUILD_TCL(ahc_inb(ahc, SAVED_SCSIID),
1170 ahc_inb(ahc, SAVED_LUN))),
1171 ahc_inb(ahc, SINDEX));
1172 printf("SCSIID == 0x%x, SCB_SCSIID == 0x%x, SCB_LUN == 0x%x, "
1173 "SCB_TAG == 0x%x, SCB_CONTROL == 0x%x\n",
1174 ahc_inb(ahc, SCSIID), ahc_inb(ahc, SCB_SCSIID),
1175 ahc_inb(ahc, SCB_LUN), ahc_inb(ahc, SCB_TAG),
1176 ahc_inb(ahc, SCB_CONTROL));
1177 printf("SCSIBUSL == 0x%x, SCSISIGI == 0x%x\n",
1178 ahc_inb(ahc, SCSIBUSL), ahc_inb(ahc, SCSISIGI));
1179 printf("SXFRCTL0 == 0x%x\n", ahc_inb(ahc, SXFRCTL0));
1180 printf("SEQCTL == 0x%x\n", ahc_inb(ahc, SEQCTL));
1181 ahc_dump_card_state(ahc);
1182 ahc->msgout_buf[0] = MSG_BUS_DEV_RESET;
1183 ahc->msgout_len = 1;
1184 ahc->msgout_index = 0;
1185 ahc->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
1186 ahc_outb(ahc, MSG_OUT, HOST_MSG);
1187 ahc_assert_atn(ahc);
1192 u_int rejbyte = ahc_inb(ahc, ACCUM);
1193 printf("%s:%c:%d: Warning - unknown message received from "
1194 "target (0x%x). Rejecting\n",
1195 ahc_name(ahc), devinfo.channel, devinfo.target, rejbyte);
1198 case PROTO_VIOLATION:
1200 ahc_handle_proto_violation(ahc);
1204 ahc_handle_ign_wide_residue(ahc, &devinfo);
1207 ahc_reinitialize_dataptrs(ahc);
1213 lastphase = ahc_inb(ahc, LASTPHASE);
1214 printf("%s:%c:%d: unknown scsi bus phase %x, "
1215 "lastphase = 0x%x. Attempting to continue\n",
1216 ahc_name(ahc), devinfo.channel, devinfo.target,
1217 lastphase, ahc_inb(ahc, SCSISIGI));
1220 case MISSED_BUSFREE:
1224 lastphase = ahc_inb(ahc, LASTPHASE);
1225 printf("%s:%c:%d: Missed busfree. "
1226 "Lastphase = 0x%x, Curphase = 0x%x\n",
1227 ahc_name(ahc), devinfo.channel, devinfo.target,
1228 lastphase, ahc_inb(ahc, SCSISIGI));
1235 * The sequencer has encountered a message phase
1236 * that requires host assistance for completion.
1237 * While handling the message phase(s), we will be
1238 * notified by the sequencer after each byte is
1239 * transfered so we can track bus phase changes.
1241 * If this is the first time we've seen a HOST_MSG_LOOP
1242 * interrupt, initialize the state of the host message
1245 if (ahc->msg_type == MSG_TYPE_NONE) {
1250 bus_phase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK;
1251 if (bus_phase != P_MESGIN
1252 && bus_phase != P_MESGOUT) {
1253 printf("ahc_intr: HOST_MSG_LOOP bad "
1257 * Probably transitioned to bus free before
1258 * we got here. Just punt the message.
1260 ahc_clear_intstat(ahc);
1265 scb_index = ahc_inb(ahc, SCB_TAG);
1266 scb = ahc_lookup_scb(ahc, scb_index);
1267 if (devinfo.role == ROLE_INITIATOR) {
1268 if (bus_phase == P_MESGOUT) {
1270 panic("HOST_MSG_LOOP with "
1274 ahc_setup_initiator_msgout(ahc,
1279 MSG_TYPE_INITIATOR_MSGIN;
1280 ahc->msgin_index = 0;
1283 #ifdef AHC_TARGET_MODE
1285 if (bus_phase == P_MESGOUT) {
1287 MSG_TYPE_TARGET_MSGOUT;
1288 ahc->msgin_index = 0;
1291 ahc_setup_target_msgin(ahc,
1298 ahc_handle_message_phase(ahc);
1304 * If we've cleared the parity error interrupt
1305 * but the sequencer still believes that SCSIPERR
1306 * is true, it must be that the parity error is
1307 * for the currently presented byte on the bus,
1308 * and we are not in a phase (data-in) where we will
1309 * eventually ack this byte. Ack the byte and
1310 * throw it away in the hope that the target will
1311 * take us to message out to deliver the appropriate
1314 if ((intstat & SCSIINT) == 0
1315 && (ahc_inb(ahc, SSTAT1) & SCSIPERR) != 0) {
1317 if ((ahc->features & AHC_DT) == 0) {
1321 * The hardware will only let you ack bytes
1322 * if the expected phase in SCSISIGO matches
1323 * the current phase. Make sure this is
1324 * currently the case.
1326 curphase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK;
1327 ahc_outb(ahc, LASTPHASE, curphase);
1328 ahc_outb(ahc, SCSISIGO, curphase);
1330 if ((ahc_inb(ahc, SCSISIGI) & (CDI|MSGI)) == 0) {
1334 * In a data phase. Faster to bitbucket
1335 * the data than to individually ack each
1336 * byte. This is also the only strategy
1337 * that will work with AUTOACK enabled.
1339 ahc_outb(ahc, SXFRCTL1,
1340 ahc_inb(ahc, SXFRCTL1) | BITBUCKET);
1342 while (--wait != 0) {
1343 if ((ahc_inb(ahc, SCSISIGI)
1348 ahc_outb(ahc, SXFRCTL1,
1349 ahc_inb(ahc, SXFRCTL1) & ~BITBUCKET);
1354 ahc_print_devinfo(ahc, &devinfo);
1355 printf("Unable to clear parity error. "
1356 "Resetting bus.\n");
1357 scb_index = ahc_inb(ahc, SCB_TAG);
1358 scb = ahc_lookup_scb(ahc, scb_index);
1360 ahc_set_transaction_status(scb,
1362 ahc_reset_channel(ahc, devinfo.channel,
1363 /*init reset*/TRUE);
1366 ahc_inb(ahc, SCSIDATL);
1374 * When the sequencer detects an overrun, it
1375 * places the controller in "BITBUCKET" mode
1376 * and allows the target to complete its transfer.
1377 * Unfortunately, none of the counters get updated
1378 * when the controller is in this mode, so we have
1379 * no way of knowing how large the overrun was.
1381 u_int scbindex = ahc_inb(ahc, SCB_TAG);
1382 u_int lastphase = ahc_inb(ahc, LASTPHASE);
1385 scb = ahc_lookup_scb(ahc, scbindex);
1386 for (i = 0; i < num_phases; i++) {
1387 if (lastphase == ahc_phase_table[i].phase)
1390 ahc_print_path(ahc, scb);
1391 printf("data overrun detected %s."
1393 ahc_phase_table[i].phasemsg,
1395 ahc_print_path(ahc, scb);
1396 printf("%s seen Data Phase. Length = %ld. NumSGs = %d.\n",
1397 ahc_inb(ahc, SEQ_FLAGS) & DPHASE ? "Have" : "Haven't",
1398 ahc_get_transfer_length(scb), scb->sg_count);
1399 if (scb->sg_count > 0) {
1400 for (i = 0; i < scb->sg_count; i++) {
1402 printf("sg[%d] - Addr 0x%x%x : Length %d\n",
1404 (ahc_le32toh(scb->sg_list[i].len) >> 24
1405 & SG_HIGH_ADDR_BITS),
1406 ahc_le32toh(scb->sg_list[i].addr),
1407 ahc_le32toh(scb->sg_list[i].len)
1412 * Set this and it will take effect when the
1413 * target does a command complete.
1415 ahc_freeze_devq(ahc, scb);
1416 if ((scb->flags & SCB_SENSE) == 0) {
1417 ahc_set_transaction_status(scb, CAM_DATA_RUN_ERR);
1419 scb->flags &= ~SCB_SENSE;
1420 ahc_set_transaction_status(scb, CAM_AUTOSENSE_FAIL);
1422 ahc_freeze_scb(scb);
1424 if ((ahc->features & AHC_ULTRA2) != 0) {
1426 * Clear the channel in case we return
1427 * to data phase later.
1429 ahc_outb(ahc, SXFRCTL0,
1430 ahc_inb(ahc, SXFRCTL0) | CLRSTCNT|CLRCHN);
1431 ahc_outb(ahc, SXFRCTL0,
1432 ahc_inb(ahc, SXFRCTL0) | CLRSTCNT|CLRCHN);
1434 if ((ahc->flags & AHC_39BIT_ADDRESSING) != 0) {
1437 /* Ensure HHADDR is 0 for future DMA operations. */
1438 dscommand1 = ahc_inb(ahc, DSCOMMAND1);
1439 ahc_outb(ahc, DSCOMMAND1, dscommand1 | HADDLDSEL0);
1440 ahc_outb(ahc, HADDR, 0);
1441 ahc_outb(ahc, DSCOMMAND1, dscommand1);
1449 printf("%s:%c:%d:%d: Attempt to issue message failed\n",
1450 ahc_name(ahc), devinfo.channel, devinfo.target,
1452 scbindex = ahc_inb(ahc, SCB_TAG);
1453 scb = ahc_lookup_scb(ahc, scbindex);
1455 && (scb->flags & SCB_RECOVERY_SCB) != 0)
1457 * Ensure that we didn't put a second instance of this
1458 * SCB into the QINFIFO.
1460 ahc_search_qinfifo(ahc, SCB_GET_TARGET(ahc, scb),
1461 SCB_GET_CHANNEL(ahc, scb),
1462 SCB_GET_LUN(scb), scb->hscb->tag,
1463 ROLE_INITIATOR, /*status*/0,
1469 printf("%s: No free or disconnected SCBs\n", ahc_name(ahc));
1470 ahc_dump_card_state(ahc);
1471 panic("for safety");
1478 scbptr = ahc_inb(ahc, SCBPTR);
1479 printf("Bogus TAG after DMA. SCBPTR %d, tag %d, our tag %d\n",
1480 scbptr, ahc_inb(ahc, ARG_1),
1481 ahc->scb_data->hscbs[scbptr].tag);
1482 ahc_dump_card_state(ahc);
1483 panic("for saftey");
1488 printf("%s: BTT calculation out of range\n", ahc_name(ahc));
1489 printf("SAVED_SCSIID == 0x%x, SAVED_LUN == 0x%x, "
1490 "ARG_1 == 0x%x ACCUM = 0x%x\n",
1491 ahc_inb(ahc, SAVED_SCSIID), ahc_inb(ahc, SAVED_LUN),
1492 ahc_inb(ahc, ARG_1), ahc_inb(ahc, ACCUM));
1493 printf("SEQ_FLAGS == 0x%x, SCBPTR == 0x%x, BTT == 0x%x, "
1494 "SINDEX == 0x%x\n, A == 0x%x\n",
1495 ahc_inb(ahc, SEQ_FLAGS), ahc_inb(ahc, SCBPTR),
1496 ahc_index_busy_tcl(ahc,
1497 BUILD_TCL(ahc_inb(ahc, SAVED_SCSIID),
1498 ahc_inb(ahc, SAVED_LUN))),
1499 ahc_inb(ahc, SINDEX),
1500 ahc_inb(ahc, ACCUM));
1501 printf("SCSIID == 0x%x, SCB_SCSIID == 0x%x, SCB_LUN == 0x%x, "
1502 "SCB_TAG == 0x%x, SCB_CONTROL == 0x%x\n",
1503 ahc_inb(ahc, SCSIID), ahc_inb(ahc, SCB_SCSIID),
1504 ahc_inb(ahc, SCB_LUN), ahc_inb(ahc, SCB_TAG),
1505 ahc_inb(ahc, SCB_CONTROL));
1506 printf("SCSIBUSL == 0x%x, SCSISIGI == 0x%x\n",
1507 ahc_inb(ahc, SCSIBUSL), ahc_inb(ahc, SCSISIGI));
1508 ahc_dump_card_state(ahc);
1509 panic("for safety");
1513 printf("ahc_intr: seqint, "
1514 "intstat == 0x%x, scsisigi = 0x%x\n",
1515 intstat, ahc_inb(ahc, SCSISIGI));
1520 * The sequencer is paused immediately on
1521 * a SEQINT, so we should restart it when
1528 ahc_handle_scsiint(struct ahc_softc *ahc, u_int intstat)
1537 if ((ahc->features & AHC_TWIN) != 0
1538 && ((ahc_inb(ahc, SBLKCTL) & SELBUSB) != 0))
1542 intr_channel = cur_channel;
1544 if ((ahc->features & AHC_ULTRA2) != 0)
1545 status0 = ahc_inb(ahc, SSTAT0) & IOERR;
1548 status = ahc_inb(ahc, SSTAT1) & (SELTO|SCSIRSTI|BUSFREE|SCSIPERR);
1549 if (status == 0 && status0 == 0) {
1550 if ((ahc->features & AHC_TWIN) != 0) {
1551 /* Try the other channel */
1552 ahc_outb(ahc, SBLKCTL, ahc_inb(ahc, SBLKCTL) ^ SELBUSB);
1553 status = ahc_inb(ahc, SSTAT1)
1554 & (SELTO|SCSIRSTI|BUSFREE|SCSIPERR);
1555 intr_channel = (cur_channel == 'A') ? 'B' : 'A';
1558 printf("%s: Spurious SCSI interrupt\n", ahc_name(ahc));
1559 ahc_outb(ahc, CLRINT, CLRSCSIINT);
1565 /* Make sure the sequencer is in a safe location. */
1566 ahc_clear_critical_section(ahc);
1568 scb_index = ahc_inb(ahc, SCB_TAG);
1569 scb = ahc_lookup_scb(ahc, scb_index);
1571 && (ahc_inb(ahc, SEQ_FLAGS) & NOT_IDENTIFIED) != 0)
1574 if ((ahc->features & AHC_ULTRA2) != 0
1575 && (status0 & IOERR) != 0) {
1578 now_lvd = ahc_inb(ahc, SBLKCTL) & ENAB40;
1579 printf("%s: Transceiver State Has Changed to %s mode\n",
1580 ahc_name(ahc), now_lvd ? "LVD" : "SE");
1581 ahc_outb(ahc, CLRSINT0, CLRIOERR);
1583 * When transitioning to SE mode, the reset line
1584 * glitches, triggering an arbitration bug in some
1585 * Ultra2 controllers. This bug is cleared when we
1586 * assert the reset line. Since a reset glitch has
1587 * already occurred with this transition and a
1588 * transceiver state change is handled just like
1589 * a bus reset anyway, asserting the reset line
1590 * ourselves is safe.
1592 ahc_reset_channel(ahc, intr_channel,
1593 /*Initiate Reset*/now_lvd == 0);
1594 } else if ((status & SCSIRSTI) != 0) {
1595 printf("%s: Someone reset channel %c\n",
1596 ahc_name(ahc), intr_channel);
1597 if (intr_channel != cur_channel)
1598 ahc_outb(ahc, SBLKCTL, ahc_inb(ahc, SBLKCTL) ^ SELBUSB);
1599 ahc_reset_channel(ahc, intr_channel, /*Initiate Reset*/FALSE);
1600 } else if ((status & SCSIPERR) != 0) {
1602 * Determine the bus phase and queue an appropriate message.
1603 * SCSIPERR is latched true as soon as a parity error
1604 * occurs. If the sequencer acked the transfer that
1605 * caused the parity error and the currently presented
1606 * transfer on the bus has correct parity, SCSIPERR will
1607 * be cleared by CLRSCSIPERR. Use this to determine if
1608 * we should look at the last phase the sequencer recorded,
1609 * or the current phase presented on the bus.
1611 struct ahc_devinfo devinfo;
1621 lastphase = ahc_inb(ahc, LASTPHASE);
1622 curphase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK;
1623 sstat2 = ahc_inb(ahc, SSTAT2);
1624 ahc_outb(ahc, CLRSINT1, CLRSCSIPERR);
1626 * For all phases save DATA, the sequencer won't
1627 * automatically ack a byte that has a parity error
1628 * in it. So the only way that the current phase
1629 * could be 'data-in' is if the parity error is for
1630 * an already acked byte in the data phase. During
1631 * synchronous data-in transfers, we may actually
1632 * ack bytes before latching the current phase in
1633 * LASTPHASE, leading to the discrepancy between
1634 * curphase and lastphase.
1636 if ((ahc_inb(ahc, SSTAT1) & SCSIPERR) != 0
1637 || curphase == P_DATAIN || curphase == P_DATAIN_DT)
1638 errorphase = curphase;
1640 errorphase = lastphase;
1642 for (i = 0; i < num_phases; i++) {
1643 if (errorphase == ahc_phase_table[i].phase)
1646 mesg_out = ahc_phase_table[i].mesg_out;
1649 if (SCB_IS_SILENT(scb))
1652 ahc_print_path(ahc, scb);
1653 scb->flags |= SCB_TRANSMISSION_ERROR;
1655 printf("%s:%c:%d: ", ahc_name(ahc), intr_channel,
1656 SCSIID_TARGET(ahc, ahc_inb(ahc, SAVED_SCSIID)));
1657 scsirate = ahc_inb(ahc, SCSIRATE);
1658 if (silent == FALSE) {
1659 printf("parity error detected %s. "
1660 "SEQADDR(0x%x) SCSIRATE(0x%x)\n",
1661 ahc_phase_table[i].phasemsg,
1662 ahc_inw(ahc, SEQADDR0),
1664 if ((ahc->features & AHC_DT) != 0) {
1665 if ((sstat2 & CRCVALERR) != 0)
1666 printf("\tCRC Value Mismatch\n");
1667 if ((sstat2 & CRCENDERR) != 0)
1668 printf("\tNo terminal CRC packet "
1670 if ((sstat2 & CRCREQERR) != 0)
1671 printf("\tIllegal CRC packet "
1673 if ((sstat2 & DUAL_EDGE_ERR) != 0)
1674 printf("\tUnexpected %sDT Data Phase\n",
1675 (scsirate & SINGLE_EDGE)
1680 if ((ahc->features & AHC_DT) != 0
1681 && (sstat2 & DUAL_EDGE_ERR) != 0) {
1683 * This error applies regardless of
1684 * data direction, so ignore the value
1685 * in the phase table.
1687 mesg_out = MSG_INITIATOR_DET_ERR;
1691 * We've set the hardware to assert ATN if we
1692 * get a parity error on "in" phases, so all we
1693 * need to do is stuff the message buffer with
1694 * the appropriate message. "In" phases have set
1695 * mesg_out to something other than MSG_NOP.
1697 if (mesg_out != MSG_NOOP) {
1698 if (ahc->msg_type != MSG_TYPE_NONE)
1699 ahc->send_msg_perror = TRUE;
1701 ahc_outb(ahc, MSG_OUT, mesg_out);
1704 * Force a renegotiation with this target just in
1705 * case we are out of sync for some external reason
1706 * unknown (or unreported) by the target.
1708 ahc_fetch_devinfo(ahc, &devinfo);
1709 ahc_force_renegotiation(ahc, &devinfo);
1711 ahc_outb(ahc, CLRINT, CLRSCSIINT);
1713 } else if ((status & SELTO) != 0) {
1716 /* Stop the selection */
1717 ahc_outb(ahc, SCSISEQ, 0);
1719 /* No more pending messages */
1720 ahc_clear_msg_state(ahc);
1722 /* Clear interrupt state */
1723 ahc_outb(ahc, SIMODE1, ahc_inb(ahc, SIMODE1) & ~ENBUSFREE);
1724 ahc_outb(ahc, CLRSINT1, CLRSELTIMEO|CLRBUSFREE|CLRSCSIPERR);
1727 * Although the driver does not care about the
1728 * 'Selection in Progress' status bit, the busy
1729 * LED does. SELINGO is only cleared by a sucessfull
1730 * selection, so we must manually clear it to insure
1731 * the LED turns off just incase no future successful
1732 * selections occur (e.g. no devices on the bus).
1734 ahc_outb(ahc, CLRSINT0, CLRSELINGO);
1736 scbptr = ahc_inb(ahc, WAITING_SCBH);
1737 ahc_outb(ahc, SCBPTR, scbptr);
1738 scb_index = ahc_inb(ahc, SCB_TAG);
1740 scb = ahc_lookup_scb(ahc, scb_index);
1742 printf("%s: ahc_intr - referenced scb not "
1743 "valid during SELTO scb(%d, %d)\n",
1744 ahc_name(ahc), scbptr, scb_index);
1745 ahc_dump_card_state(ahc);
1747 struct ahc_devinfo devinfo;
1749 if ((ahc_debug & AHC_SHOW_SELTO) != 0) {
1750 ahc_print_path(ahc, scb);
1751 printf("Saw Selection Timeout for SCB 0x%x\n",
1755 ahc_scb_devinfo(ahc, &devinfo, scb);
1756 ahc_set_transaction_status(scb, CAM_SEL_TIMEOUT);
1757 ahc_freeze_devq(ahc, scb);
1760 * Cancel any pending transactions on the device
1761 * now that it seems to be missing. This will
1762 * also revert us to async/narrow transfers until
1763 * we can renegotiate with the device.
1765 ahc_handle_devreset(ahc, &devinfo,
1767 "Selection Timeout",
1768 /*verbose_level*/1);
1770 ahc_outb(ahc, CLRINT, CLRSCSIINT);
1772 } else if ((status & BUSFREE) != 0
1773 && (ahc_inb(ahc, SIMODE1) & ENBUSFREE) != 0) {
1774 struct ahc_devinfo devinfo;
1779 u_int initiator_role_id;
1784 * Clear our selection hardware as soon as possible.
1785 * We may have an entry in the waiting Q for this target,
1786 * that is affected by this busfree and we don't want to
1787 * go about selecting the target while we handle the event.
1789 ahc_outb(ahc, SCSISEQ,
1790 ahc_inb(ahc, SCSISEQ) & (ENSELI|ENRSELI|ENAUTOATNP));
1793 * Disable busfree interrupts and clear the busfree
1794 * interrupt status. We do this here so that several
1795 * bus transactions occur prior to clearing the SCSIINT
1796 * latch. It can take a bit for the clearing to take effect.
1798 ahc_outb(ahc, SIMODE1, ahc_inb(ahc, SIMODE1) & ~ENBUSFREE);
1799 ahc_outb(ahc, CLRSINT1, CLRBUSFREE|CLRSCSIPERR);
1802 * Look at what phase we were last in.
1803 * If its message out, chances are pretty good
1804 * that the busfree was in response to one of
1805 * our abort requests.
1807 lastphase = ahc_inb(ahc, LASTPHASE);
1808 saved_scsiid = ahc_inb(ahc, SAVED_SCSIID);
1809 saved_lun = ahc_inb(ahc, SAVED_LUN);
1810 target = SCSIID_TARGET(ahc, saved_scsiid);
1811 initiator_role_id = SCSIID_OUR_ID(saved_scsiid);
1812 channel = SCSIID_CHANNEL(ahc, saved_scsiid);
1813 ahc_compile_devinfo(&devinfo, initiator_role_id,
1814 target, saved_lun, channel, ROLE_INITIATOR);
1817 if (lastphase == P_MESGOUT) {
1820 tag = SCB_LIST_NULL;
1821 if (ahc_sent_msg(ahc, AHCMSG_1B, MSG_ABORT_TAG, TRUE)
1822 || ahc_sent_msg(ahc, AHCMSG_1B, MSG_ABORT, TRUE)) {
1823 if (ahc->msgout_buf[ahc->msgout_index - 1]
1825 tag = scb->hscb->tag;
1826 ahc_print_path(ahc, scb);
1827 printf("SCB %d - Abort%s Completed.\n",
1828 scb->hscb->tag, tag == SCB_LIST_NULL ?
1830 ahc_abort_scbs(ahc, target, channel,
1835 } else if (ahc_sent_msg(ahc, AHCMSG_1B,
1836 MSG_BUS_DEV_RESET, TRUE)) {
1839 * Don't mark the user's request for this BDR
1840 * as completing with CAM_BDR_SENT. CAM3
1841 * specifies CAM_REQ_CMP.
1844 && scb->io_ctx->ccb_h.func_code== XPT_RESET_DEV
1845 && ahc_match_scb(ahc, scb, target, channel,
1849 ahc_set_transaction_status(scb, CAM_REQ_CMP);
1852 ahc_compile_devinfo(&devinfo,
1858 ahc_handle_devreset(ahc, &devinfo,
1861 /*verbose_level*/0);
1863 } else if (ahc_sent_msg(ahc, AHCMSG_EXT,
1864 MSG_EXT_PPR, FALSE)) {
1865 struct ahc_initiator_tinfo *tinfo;
1866 struct ahc_tmode_tstate *tstate;
1869 * PPR Rejected. Try non-ppr negotiation
1870 * and retry command.
1872 tinfo = ahc_fetch_transinfo(ahc,
1877 tinfo->curr.transport_version = 2;
1878 tinfo->goal.transport_version = 2;
1879 tinfo->goal.ppr_options = 0;
1880 ahc_qinfifo_requeue_tail(ahc, scb);
1882 } else if (ahc_sent_msg(ahc, AHCMSG_EXT,
1883 MSG_EXT_WDTR, FALSE)) {
1885 * Negotiation Rejected. Go-narrow and
1888 ahc_set_width(ahc, &devinfo,
1889 MSG_EXT_WDTR_BUS_8_BIT,
1890 AHC_TRANS_CUR|AHC_TRANS_GOAL,
1892 ahc_qinfifo_requeue_tail(ahc, scb);
1894 } else if (ahc_sent_msg(ahc, AHCMSG_EXT,
1895 MSG_EXT_SDTR, FALSE)) {
1897 * Negotiation Rejected. Go-async and
1900 ahc_set_syncrate(ahc, &devinfo,
1902 /*period*/0, /*offset*/0,
1904 AHC_TRANS_CUR|AHC_TRANS_GOAL,
1906 ahc_qinfifo_requeue_tail(ahc, scb);
1910 if (printerror != 0) {
1916 if ((scb->hscb->control & TAG_ENB) != 0)
1917 tag = scb->hscb->tag;
1919 tag = SCB_LIST_NULL;
1920 ahc_print_path(ahc, scb);
1921 ahc_abort_scbs(ahc, target, channel,
1922 SCB_GET_LUN(scb), tag,
1927 * We had not fully identified this connection,
1928 * so we cannot abort anything.
1930 printf("%s: ", ahc_name(ahc));
1932 for (i = 0; i < num_phases; i++) {
1933 if (lastphase == ahc_phase_table[i].phase)
1936 if (lastphase != P_BUSFREE) {
1938 * Renegotiate with this device at the
1939 * next oportunity just in case this busfree
1940 * is due to a negotiation mismatch with the
1943 ahc_force_renegotiation(ahc, &devinfo);
1945 printf("Unexpected busfree %s\n"
1946 "SEQADDR == 0x%x\n",
1947 ahc_phase_table[i].phasemsg,
1948 ahc_inb(ahc, SEQADDR0)
1949 | (ahc_inb(ahc, SEQADDR1) << 8));
1951 ahc_outb(ahc, CLRINT, CLRSCSIINT);
1954 printf("%s: Missing case in ahc_handle_scsiint. status = %x\n",
1955 ahc_name(ahc), status);
1956 ahc_outb(ahc, CLRINT, CLRSCSIINT);
1961 * Force renegotiation to occur the next time we initiate
1962 * a command to the current device.
1965 ahc_force_renegotiation(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
1967 struct ahc_initiator_tinfo *targ_info;
1968 struct ahc_tmode_tstate *tstate;
1970 targ_info = ahc_fetch_transinfo(ahc,
1972 devinfo->our_scsiid,
1975 ahc_update_neg_request(ahc, devinfo, tstate,
1976 targ_info, AHC_NEG_IF_NON_ASYNC);
1979 #define AHC_MAX_STEPS 2000
1981 ahc_clear_critical_section(struct ahc_softc *ahc)
1988 if (ahc->num_critical_sections == 0)
2000 seqaddr = ahc_inb(ahc, SEQADDR0)
2001 | (ahc_inb(ahc, SEQADDR1) << 8);
2004 * Seqaddr represents the next instruction to execute,
2005 * so we are really executing the instruction just
2010 cs = ahc->critical_sections;
2011 for (i = 0; i < ahc->num_critical_sections; i++, cs++) {
2013 if (cs->begin < seqaddr && cs->end >= seqaddr)
2017 if (i == ahc->num_critical_sections)
2020 if (steps > AHC_MAX_STEPS) {
2021 printf("%s: Infinite loop in critical section\n",
2023 ahc_dump_card_state(ahc);
2024 panic("critical section loop");
2028 if (stepping == FALSE) {
2031 * Disable all interrupt sources so that the
2032 * sequencer will not be stuck by a pausing
2033 * interrupt condition while we attempt to
2034 * leave a critical section.
2036 simode0 = ahc_inb(ahc, SIMODE0);
2037 ahc_outb(ahc, SIMODE0, 0);
2038 simode1 = ahc_inb(ahc, SIMODE1);
2039 if ((ahc->features & AHC_DT) != 0)
2041 * On DT class controllers, we
2042 * use the enhanced busfree logic.
2043 * Unfortunately we cannot re-enable
2044 * busfree detection within the
2045 * current connection, so we must
2046 * leave it on while single stepping.
2048 ahc_outb(ahc, SIMODE1, simode1 & ENBUSFREE);
2050 ahc_outb(ahc, SIMODE1, 0);
2051 ahc_outb(ahc, CLRINT, CLRSCSIINT);
2052 ahc_outb(ahc, SEQCTL, ahc->seqctl | STEP);
2055 if ((ahc->features & AHC_DT) != 0) {
2056 ahc_outb(ahc, CLRSINT1, CLRBUSFREE);
2057 ahc_outb(ahc, CLRINT, CLRSCSIINT);
2059 ahc_outb(ahc, HCNTRL, ahc->unpause);
2060 while (!ahc_is_paused(ahc))
2064 ahc_outb(ahc, SIMODE0, simode0);
2065 ahc_outb(ahc, SIMODE1, simode1);
2066 ahc_outb(ahc, SEQCTL, ahc->seqctl);
2071 * Clear any pending interrupt status.
2074 ahc_clear_intstat(struct ahc_softc *ahc)
2076 /* Clear any interrupt conditions this may have caused */
2077 ahc_outb(ahc, CLRSINT1, CLRSELTIMEO|CLRATNO|CLRSCSIRSTI
2078 |CLRBUSFREE|CLRSCSIPERR|CLRPHASECHG|
2080 ahc_flush_device_writes(ahc);
2081 ahc_outb(ahc, CLRSINT0, CLRSELDO|CLRSELDI|CLRSELINGO);
2082 ahc_flush_device_writes(ahc);
2083 ahc_outb(ahc, CLRINT, CLRSCSIINT);
2084 ahc_flush_device_writes(ahc);
2087 /**************************** Debugging Routines ******************************/
2089 uint32_t ahc_debug = AHC_DEBUG_OPTS;
2094 ahc_print_scb(struct scb *scb)
2098 struct hardware_scb *hscb = scb->hscb;
2100 printf("scb:%p control:0x%x scsiid:0x%x lun:%d cdb_len:%d\n",
2106 printf("Shared Data: ");
2107 for (i = 0; i < sizeof(hscb->shared_data.cdb); i++)
2108 printf("%#02x", hscb->shared_data.cdb[i]);
2109 printf(" dataptr:%#x datacnt:%#x sgptr:%#x tag:%#x\n",
2110 ahc_le32toh(hscb->dataptr),
2111 ahc_le32toh(hscb->datacnt),
2112 ahc_le32toh(hscb->sgptr),
2114 if (scb->sg_count > 0) {
2115 for (i = 0; i < scb->sg_count; i++) {
2116 printf("sg[%d] - Addr 0x%x%x : Length %d\n",
2118 (ahc_le32toh(scb->sg_list[i].len) >> 24
2119 & SG_HIGH_ADDR_BITS),
2120 ahc_le32toh(scb->sg_list[i].addr),
2121 ahc_le32toh(scb->sg_list[i].len));
2127 /************************* Transfer Negotiation *******************************/
2129 * Allocate per target mode instance (ID we respond to as a target)
2130 * transfer negotiation data structures.
2132 static struct ahc_tmode_tstate *
2133 ahc_alloc_tstate(struct ahc_softc *ahc, u_int scsi_id, char channel)
2135 struct ahc_tmode_tstate *master_tstate;
2136 struct ahc_tmode_tstate *tstate;
2139 master_tstate = ahc->enabled_targets[ahc->our_id];
2140 if (channel == 'B') {
2142 master_tstate = ahc->enabled_targets[ahc->our_id_b + 8];
2144 if (ahc->enabled_targets[scsi_id] != NULL
2145 && ahc->enabled_targets[scsi_id] != master_tstate)
2146 panic("%s: ahc_alloc_tstate - Target already allocated",
2148 tstate = (struct ahc_tmode_tstate*)malloc(sizeof(*tstate),
2149 M_DEVBUF, M_NOWAIT);
2154 * If we have allocated a master tstate, copy user settings from
2155 * the master tstate (taken from SRAM or the EEPROM) for this
2156 * channel, but reset our current and goal settings to async/narrow
2157 * until an initiator talks to us.
2159 if (master_tstate != NULL) {
2160 memcpy(tstate, master_tstate, sizeof(*tstate));
2161 memset(tstate->enabled_luns, 0, sizeof(tstate->enabled_luns));
2162 tstate->ultraenb = 0;
2163 for (i = 0; i < AHC_NUM_TARGETS; i++) {
2164 memset(&tstate->transinfo[i].curr, 0,
2165 sizeof(tstate->transinfo[i].curr));
2166 memset(&tstate->transinfo[i].goal, 0,
2167 sizeof(tstate->transinfo[i].goal));
2170 memset(tstate, 0, sizeof(*tstate));
2171 ahc->enabled_targets[scsi_id] = tstate;
2175 #ifdef AHC_TARGET_MODE
2177 * Free per target mode instance (ID we respond to as a target)
2178 * transfer negotiation data structures.
2181 ahc_free_tstate(struct ahc_softc *ahc, u_int scsi_id, char channel, int force)
2183 struct ahc_tmode_tstate *tstate;
2186 * Don't clean up our "master" tstate.
2187 * It has our default user settings.
2189 if (((channel == 'B' && scsi_id == ahc->our_id_b)
2190 || (channel == 'A' && scsi_id == ahc->our_id))
2196 tstate = ahc->enabled_targets[scsi_id];
2198 free(tstate, M_DEVBUF);
2199 ahc->enabled_targets[scsi_id] = NULL;
2204 * Called when we have an active connection to a target on the bus,
2205 * this function finds the nearest syncrate to the input period limited
2206 * by the capabilities of the bus connectivity of and sync settings for
2209 const struct ahc_syncrate *
2210 ahc_devlimited_syncrate(struct ahc_softc *ahc,
2211 struct ahc_initiator_tinfo *tinfo,
2212 u_int *period, u_int *ppr_options, role_t role)
2214 struct ahc_transinfo *transinfo;
2217 if ((ahc->features & AHC_ULTRA2) != 0) {
2218 if ((ahc_inb(ahc, SBLKCTL) & ENAB40) != 0
2219 && (ahc_inb(ahc, SSTAT2) & EXP_ACTIVE) == 0) {
2220 maxsync = AHC_SYNCRATE_DT;
2222 maxsync = AHC_SYNCRATE_ULTRA;
2223 /* Can't do DT on an SE bus */
2224 *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
2226 } else if ((ahc->features & AHC_ULTRA) != 0) {
2227 maxsync = AHC_SYNCRATE_ULTRA;
2229 maxsync = AHC_SYNCRATE_FAST;
2232 * Never allow a value higher than our current goal
2233 * period otherwise we may allow a target initiated
2234 * negotiation to go above the limit as set by the
2235 * user. In the case of an initiator initiated
2236 * sync negotiation, we limit based on the user
2237 * setting. This allows the system to still accept
2238 * incoming negotiations even if target initiated
2239 * negotiation is not performed.
2241 if (role == ROLE_TARGET)
2242 transinfo = &tinfo->user;
2244 transinfo = &tinfo->goal;
2245 *ppr_options &= transinfo->ppr_options;
2246 if (transinfo->width == MSG_EXT_WDTR_BUS_8_BIT) {
2247 maxsync = max(maxsync, (u_int)AHC_SYNCRATE_ULTRA2);
2248 *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
2250 if (transinfo->period == 0) {
2255 *period = max(*period, (u_int)transinfo->period);
2256 return (ahc_find_syncrate(ahc, period, ppr_options, maxsync));
2260 * Look up the valid period to SCSIRATE conversion in our table.
2261 * Return the period and offset that should be sent to the target
2262 * if this was the beginning of an SDTR.
2264 const struct ahc_syncrate *
2265 ahc_find_syncrate(struct ahc_softc *ahc, u_int *period,
2266 u_int *ppr_options, u_int maxsync)
2268 const struct ahc_syncrate *syncrate;
2270 if ((ahc->features & AHC_DT) == 0)
2271 *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
2273 /* Skip all DT only entries if DT is not available */
2274 if ((*ppr_options & MSG_EXT_PPR_DT_REQ) == 0
2275 && maxsync < AHC_SYNCRATE_ULTRA2)
2276 maxsync = AHC_SYNCRATE_ULTRA2;
2278 /* Now set the maxsync based on the card capabilities
2279 * DT is already done above */
2280 if ((ahc->features & (AHC_DT | AHC_ULTRA2)) == 0
2281 && maxsync < AHC_SYNCRATE_ULTRA)
2282 maxsync = AHC_SYNCRATE_ULTRA;
2283 if ((ahc->features & (AHC_DT | AHC_ULTRA2 | AHC_ULTRA)) == 0
2284 && maxsync < AHC_SYNCRATE_FAST)
2285 maxsync = AHC_SYNCRATE_FAST;
2287 for (syncrate = &ahc_syncrates[maxsync];
2288 syncrate->rate != NULL;
2292 * The Ultra2 table doesn't go as low
2293 * as for the Fast/Ultra cards.
2295 if ((ahc->features & AHC_ULTRA2) != 0
2296 && (syncrate->sxfr_u2 == 0))
2299 if (*period <= syncrate->period) {
2301 * When responding to a target that requests
2302 * sync, the requested rate may fall between
2303 * two rates that we can output, but still be
2304 * a rate that we can receive. Because of this,
2305 * we want to respond to the target with
2306 * the same rate that it sent to us even
2307 * if the period we use to send data to it
2308 * is lower. Only lower the response period
2311 if (syncrate == &ahc_syncrates[maxsync])
2312 *period = syncrate->period;
2315 * At some speeds, we only support
2318 if ((syncrate->sxfr_u2 & ST_SXFR) != 0)
2319 *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
2325 || (syncrate->rate == NULL)
2326 || ((ahc->features & AHC_ULTRA2) != 0
2327 && (syncrate->sxfr_u2 == 0))) {
2328 /* Use asynchronous transfers. */
2331 *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
2337 * Convert from an entry in our syncrate table to the SCSI equivalent
2338 * sync "period" factor.
2341 ahc_find_period(struct ahc_softc *ahc, u_int scsirate, u_int maxsync)
2343 const struct ahc_syncrate *syncrate;
2345 if ((ahc->features & AHC_ULTRA2) != 0)
2346 scsirate &= SXFR_ULTRA2;
2350 /* now set maxsync based on card capabilities */
2351 if ((ahc->features & AHC_DT) == 0 && maxsync < AHC_SYNCRATE_ULTRA2)
2352 maxsync = AHC_SYNCRATE_ULTRA2;
2353 if ((ahc->features & (AHC_DT | AHC_ULTRA2)) == 0
2354 && maxsync < AHC_SYNCRATE_ULTRA)
2355 maxsync = AHC_SYNCRATE_ULTRA;
2356 if ((ahc->features & (AHC_DT | AHC_ULTRA2 | AHC_ULTRA)) == 0
2357 && maxsync < AHC_SYNCRATE_FAST)
2358 maxsync = AHC_SYNCRATE_FAST;
2361 syncrate = &ahc_syncrates[maxsync];
2362 while (syncrate->rate != NULL) {
2364 if ((ahc->features & AHC_ULTRA2) != 0) {
2365 if (syncrate->sxfr_u2 == 0)
2367 else if (scsirate == (syncrate->sxfr_u2 & SXFR_ULTRA2))
2368 return (syncrate->period);
2369 } else if (scsirate == (syncrate->sxfr & SXFR)) {
2370 return (syncrate->period);
2374 return (0); /* async */
2378 * Truncate the given synchronous offset to a value the
2379 * current adapter type and syncrate are capable of.
2382 ahc_validate_offset(struct ahc_softc *ahc,
2383 struct ahc_initiator_tinfo *tinfo,
2384 const struct ahc_syncrate *syncrate,
2385 u_int *offset, int wide, role_t role)
2389 /* Limit offset to what we can do */
2390 if (syncrate == NULL) {
2392 } else if ((ahc->features & AHC_ULTRA2) != 0) {
2393 maxoffset = MAX_OFFSET_ULTRA2;
2396 maxoffset = MAX_OFFSET_16BIT;
2398 maxoffset = MAX_OFFSET_8BIT;
2400 *offset = min(*offset, maxoffset);
2401 if (tinfo != NULL) {
2402 if (role == ROLE_TARGET)
2403 *offset = min(*offset, (u_int)tinfo->user.offset);
2405 *offset = min(*offset, (u_int)tinfo->goal.offset);
2410 * Truncate the given transfer width parameter to a value the
2411 * current adapter type is capable of.
2414 ahc_validate_width(struct ahc_softc *ahc, struct ahc_initiator_tinfo *tinfo,
2415 u_int *bus_width, role_t role)
2417 switch (*bus_width) {
2419 if (ahc->features & AHC_WIDE) {
2421 *bus_width = MSG_EXT_WDTR_BUS_16_BIT;
2425 case MSG_EXT_WDTR_BUS_8_BIT:
2426 *bus_width = MSG_EXT_WDTR_BUS_8_BIT;
2429 if (tinfo != NULL) {
2430 if (role == ROLE_TARGET)
2431 *bus_width = min((u_int)tinfo->user.width, *bus_width);
2433 *bus_width = min((u_int)tinfo->goal.width, *bus_width);
2438 * Update the bitmask of targets for which the controller should
2439 * negotiate with at the next convenient oportunity. This currently
2440 * means the next time we send the initial identify messages for
2441 * a new transaction.
2444 ahc_update_neg_request(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
2445 struct ahc_tmode_tstate *tstate,
2446 struct ahc_initiator_tinfo *tinfo, ahc_neg_type neg_type)
2448 u_int auto_negotiate_orig;
2450 auto_negotiate_orig = tstate->auto_negotiate;
2451 if (neg_type == AHC_NEG_ALWAYS) {
2453 * Force our "current" settings to be
2454 * unknown so that unless a bus reset
2455 * occurs the need to renegotiate is
2456 * recorded persistently.
2458 if ((ahc->features & AHC_WIDE) != 0)
2459 tinfo->curr.width = AHC_WIDTH_UNKNOWN;
2460 tinfo->curr.period = AHC_PERIOD_UNKNOWN;
2461 tinfo->curr.offset = AHC_OFFSET_UNKNOWN;
2463 if (tinfo->curr.period != tinfo->goal.period
2464 || tinfo->curr.width != tinfo->goal.width
2465 || tinfo->curr.offset != tinfo->goal.offset
2466 || tinfo->curr.ppr_options != tinfo->goal.ppr_options
2467 || (neg_type == AHC_NEG_IF_NON_ASYNC
2468 && (tinfo->goal.offset != 0
2469 || tinfo->goal.width != MSG_EXT_WDTR_BUS_8_BIT
2470 || tinfo->goal.ppr_options != 0)))
2471 tstate->auto_negotiate |= devinfo->target_mask;
2473 tstate->auto_negotiate &= ~devinfo->target_mask;
2475 return (auto_negotiate_orig != tstate->auto_negotiate);
2479 * Update the user/goal/curr tables of synchronous negotiation
2480 * parameters as well as, in the case of a current or active update,
2481 * any data structures on the host controller. In the case of an
2482 * active update, the specified target is currently talking to us on
2483 * the bus, so the transfer parameter update must take effect
2487 ahc_set_syncrate(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
2488 const struct ahc_syncrate *syncrate, u_int period,
2489 u_int offset, u_int ppr_options, u_int type, int paused)
2491 struct ahc_initiator_tinfo *tinfo;
2492 struct ahc_tmode_tstate *tstate;
2499 active = (type & AHC_TRANS_ACTIVE) == AHC_TRANS_ACTIVE;
2502 if (syncrate == NULL) {
2507 tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid,
2508 devinfo->target, &tstate);
2510 if ((type & AHC_TRANS_USER) != 0) {
2511 tinfo->user.period = period;
2512 tinfo->user.offset = offset;
2513 tinfo->user.ppr_options = ppr_options;
2516 if ((type & AHC_TRANS_GOAL) != 0) {
2517 tinfo->goal.period = period;
2518 tinfo->goal.offset = offset;
2519 tinfo->goal.ppr_options = ppr_options;
2522 old_period = tinfo->curr.period;
2523 old_offset = tinfo->curr.offset;
2524 old_ppr = tinfo->curr.ppr_options;
2526 if ((type & AHC_TRANS_CUR) != 0
2527 && (old_period != period
2528 || old_offset != offset
2529 || old_ppr != ppr_options)) {
2533 scsirate = tinfo->scsirate;
2534 if ((ahc->features & AHC_ULTRA2) != 0) {
2536 scsirate &= ~(SXFR_ULTRA2|SINGLE_EDGE|ENABLE_CRC);
2537 if (syncrate != NULL) {
2538 scsirate |= syncrate->sxfr_u2;
2539 if ((ppr_options & MSG_EXT_PPR_DT_REQ) != 0)
2540 scsirate |= ENABLE_CRC;
2542 scsirate |= SINGLE_EDGE;
2546 scsirate &= ~(SXFR|SOFS);
2548 * Ensure Ultra mode is set properly for
2551 tstate->ultraenb &= ~devinfo->target_mask;
2552 if (syncrate != NULL) {
2553 if (syncrate->sxfr & ULTRA_SXFR) {
2555 devinfo->target_mask;
2557 scsirate |= syncrate->sxfr & SXFR;
2558 scsirate |= offset & SOFS;
2563 sxfrctl0 = ahc_inb(ahc, SXFRCTL0);
2564 sxfrctl0 &= ~FAST20;
2565 if (tstate->ultraenb & devinfo->target_mask)
2567 ahc_outb(ahc, SXFRCTL0, sxfrctl0);
2571 ahc_outb(ahc, SCSIRATE, scsirate);
2572 if ((ahc->features & AHC_ULTRA2) != 0)
2573 ahc_outb(ahc, SCSIOFFSET, offset);
2576 tinfo->scsirate = scsirate;
2577 tinfo->curr.period = period;
2578 tinfo->curr.offset = offset;
2579 tinfo->curr.ppr_options = ppr_options;
2581 ahc_send_async(ahc, devinfo->channel, devinfo->target,
2582 CAM_LUN_WILDCARD, AC_TRANSFER_NEG);
2585 printf("%s: target %d synchronous at %sMHz%s, "
2586 "offset = 0x%x\n", ahc_name(ahc),
2587 devinfo->target, syncrate->rate,
2588 (ppr_options & MSG_EXT_PPR_DT_REQ)
2589 ? " DT" : "", offset);
2591 printf("%s: target %d using "
2592 "asynchronous transfers\n",
2593 ahc_name(ahc), devinfo->target);
2598 update_needed += ahc_update_neg_request(ahc, devinfo, tstate,
2599 tinfo, AHC_NEG_TO_GOAL);
2602 ahc_update_pending_scbs(ahc);
2606 * Update the user/goal/curr tables of wide negotiation
2607 * parameters as well as, in the case of a current or active update,
2608 * any data structures on the host controller. In the case of an
2609 * active update, the specified target is currently talking to us on
2610 * the bus, so the transfer parameter update must take effect
2614 ahc_set_width(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
2615 u_int width, u_int type, int paused)
2617 struct ahc_initiator_tinfo *tinfo;
2618 struct ahc_tmode_tstate *tstate;
2623 active = (type & AHC_TRANS_ACTIVE) == AHC_TRANS_ACTIVE;
2625 tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid,
2626 devinfo->target, &tstate);
2628 if ((type & AHC_TRANS_USER) != 0)
2629 tinfo->user.width = width;
2631 if ((type & AHC_TRANS_GOAL) != 0)
2632 tinfo->goal.width = width;
2634 oldwidth = tinfo->curr.width;
2635 if ((type & AHC_TRANS_CUR) != 0 && oldwidth != width) {
2639 scsirate = tinfo->scsirate;
2640 scsirate &= ~WIDEXFER;
2641 if (width == MSG_EXT_WDTR_BUS_16_BIT)
2642 scsirate |= WIDEXFER;
2644 tinfo->scsirate = scsirate;
2647 ahc_outb(ahc, SCSIRATE, scsirate);
2649 tinfo->curr.width = width;
2651 ahc_send_async(ahc, devinfo->channel, devinfo->target,
2652 CAM_LUN_WILDCARD, AC_TRANSFER_NEG);
2654 printf("%s: target %d using %dbit transfers\n",
2655 ahc_name(ahc), devinfo->target,
2656 8 * (0x01 << width));
2660 update_needed += ahc_update_neg_request(ahc, devinfo, tstate,
2661 tinfo, AHC_NEG_TO_GOAL);
2663 ahc_update_pending_scbs(ahc);
2667 * Update the current state of tagged queuing for a given target.
2670 ahc_set_tags(struct ahc_softc *ahc, struct scsi_cmnd *cmd,
2671 struct ahc_devinfo *devinfo, ahc_queue_alg alg)
2673 struct scsi_device *sdev = cmd->device;
2675 ahc_platform_set_tags(ahc, sdev, devinfo, alg);
2676 ahc_send_async(ahc, devinfo->channel, devinfo->target,
2677 devinfo->lun, AC_TRANSFER_NEG);
2681 * When the transfer settings for a connection change, update any
2682 * in-transit SCBs to contain the new data so the hardware will
2683 * be set correctly during future (re)selections.
2686 ahc_update_pending_scbs(struct ahc_softc *ahc)
2688 struct scb *pending_scb;
2689 int pending_scb_count;
2695 * Traverse the pending SCB list and ensure that all of the
2696 * SCBs there have the proper settings.
2698 pending_scb_count = 0;
2699 LIST_FOREACH(pending_scb, &ahc->pending_scbs, pending_links) {
2700 struct ahc_devinfo devinfo;
2701 struct hardware_scb *pending_hscb;
2702 struct ahc_initiator_tinfo *tinfo;
2703 struct ahc_tmode_tstate *tstate;
2705 ahc_scb_devinfo(ahc, &devinfo, pending_scb);
2706 tinfo = ahc_fetch_transinfo(ahc, devinfo.channel,
2708 devinfo.target, &tstate);
2709 pending_hscb = pending_scb->hscb;
2710 pending_hscb->control &= ~ULTRAENB;
2711 if ((tstate->ultraenb & devinfo.target_mask) != 0)
2712 pending_hscb->control |= ULTRAENB;
2713 pending_hscb->scsirate = tinfo->scsirate;
2714 pending_hscb->scsioffset = tinfo->curr.offset;
2715 if ((tstate->auto_negotiate & devinfo.target_mask) == 0
2716 && (pending_scb->flags & SCB_AUTO_NEGOTIATE) != 0) {
2717 pending_scb->flags &= ~SCB_AUTO_NEGOTIATE;
2718 pending_hscb->control &= ~MK_MESSAGE;
2720 ahc_sync_scb(ahc, pending_scb,
2721 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
2722 pending_scb_count++;
2725 if (pending_scb_count == 0)
2728 if (ahc_is_paused(ahc)) {
2735 saved_scbptr = ahc_inb(ahc, SCBPTR);
2736 /* Ensure that the hscbs down on the card match the new information */
2737 for (i = 0; i < ahc->scb_data->maxhscbs; i++) {
2738 struct hardware_scb *pending_hscb;
2742 ahc_outb(ahc, SCBPTR, i);
2743 scb_tag = ahc_inb(ahc, SCB_TAG);
2744 pending_scb = ahc_lookup_scb(ahc, scb_tag);
2745 if (pending_scb == NULL)
2748 pending_hscb = pending_scb->hscb;
2749 control = ahc_inb(ahc, SCB_CONTROL);
2750 control &= ~(ULTRAENB|MK_MESSAGE);
2751 control |= pending_hscb->control & (ULTRAENB|MK_MESSAGE);
2752 ahc_outb(ahc, SCB_CONTROL, control);
2753 ahc_outb(ahc, SCB_SCSIRATE, pending_hscb->scsirate);
2754 ahc_outb(ahc, SCB_SCSIOFFSET, pending_hscb->scsioffset);
2756 ahc_outb(ahc, SCBPTR, saved_scbptr);
2762 /**************************** Pathing Information *****************************/
2764 ahc_fetch_devinfo(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
2770 if (ahc_inb(ahc, SSTAT0) & TARGET)
2773 role = ROLE_INITIATOR;
2775 if (role == ROLE_TARGET
2776 && (ahc->features & AHC_MULTI_TID) != 0
2777 && (ahc_inb(ahc, SEQ_FLAGS)
2778 & (CMDPHASE_PENDING|TARG_CMD_PENDING|NO_DISCONNECT)) != 0) {
2779 /* We were selected, so pull our id from TARGIDIN */
2780 our_id = ahc_inb(ahc, TARGIDIN) & OID;
2781 } else if ((ahc->features & AHC_ULTRA2) != 0)
2782 our_id = ahc_inb(ahc, SCSIID_ULTRA2) & OID;
2784 our_id = ahc_inb(ahc, SCSIID) & OID;
2786 saved_scsiid = ahc_inb(ahc, SAVED_SCSIID);
2787 ahc_compile_devinfo(devinfo,
2789 SCSIID_TARGET(ahc, saved_scsiid),
2790 ahc_inb(ahc, SAVED_LUN),
2791 SCSIID_CHANNEL(ahc, saved_scsiid),
2795 static const struct ahc_phase_table_entry*
2796 ahc_lookup_phase_entry(int phase)
2798 const struct ahc_phase_table_entry *entry;
2799 const struct ahc_phase_table_entry *last_entry;
2802 * num_phases doesn't include the default entry which
2803 * will be returned if the phase doesn't match.
2805 last_entry = &ahc_phase_table[num_phases];
2806 for (entry = ahc_phase_table; entry < last_entry; entry++) {
2807 if (phase == entry->phase)
2814 ahc_compile_devinfo(struct ahc_devinfo *devinfo, u_int our_id, u_int target,
2815 u_int lun, char channel, role_t role)
2817 devinfo->our_scsiid = our_id;
2818 devinfo->target = target;
2820 devinfo->target_offset = target;
2821 devinfo->channel = channel;
2822 devinfo->role = role;
2824 devinfo->target_offset += 8;
2825 devinfo->target_mask = (0x01 << devinfo->target_offset);
2829 ahc_print_devinfo(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
2831 printf("%s:%c:%d:%d: ", ahc_name(ahc), devinfo->channel,
2832 devinfo->target, devinfo->lun);
2836 ahc_scb_devinfo(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
2842 our_id = SCSIID_OUR_ID(scb->hscb->scsiid);
2843 role = ROLE_INITIATOR;
2844 if ((scb->flags & SCB_TARGET_SCB) != 0)
2846 ahc_compile_devinfo(devinfo, our_id, SCB_GET_TARGET(ahc, scb),
2847 SCB_GET_LUN(scb), SCB_GET_CHANNEL(ahc, scb), role);
2851 /************************ Message Phase Processing ****************************/
2853 ahc_assert_atn(struct ahc_softc *ahc)
2858 if ((ahc->features & AHC_DT) == 0)
2859 scsisigo |= ahc_inb(ahc, SCSISIGI);
2860 ahc_outb(ahc, SCSISIGO, scsisigo);
2864 * When an initiator transaction with the MK_MESSAGE flag either reconnects
2865 * or enters the initial message out phase, we are interrupted. Fill our
2866 * outgoing message buffer with the appropriate message and beging handing
2867 * the message phase(s) manually.
2870 ahc_setup_initiator_msgout(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
2874 * To facilitate adding multiple messages together,
2875 * each routine should increment the index and len
2876 * variables instead of setting them explicitly.
2878 ahc->msgout_index = 0;
2879 ahc->msgout_len = 0;
2881 if ((scb->flags & SCB_DEVICE_RESET) == 0
2882 && ahc_inb(ahc, MSG_OUT) == MSG_IDENTIFYFLAG) {
2885 identify_msg = MSG_IDENTIFYFLAG | SCB_GET_LUN(scb);
2886 if ((scb->hscb->control & DISCENB) != 0)
2887 identify_msg |= MSG_IDENTIFY_DISCFLAG;
2888 ahc->msgout_buf[ahc->msgout_index++] = identify_msg;
2891 if ((scb->hscb->control & TAG_ENB) != 0) {
2892 ahc->msgout_buf[ahc->msgout_index++] =
2893 scb->hscb->control & (TAG_ENB|SCB_TAG_TYPE);
2894 ahc->msgout_buf[ahc->msgout_index++] = scb->hscb->tag;
2895 ahc->msgout_len += 2;
2899 if (scb->flags & SCB_DEVICE_RESET) {
2900 ahc->msgout_buf[ahc->msgout_index++] = MSG_BUS_DEV_RESET;
2902 ahc_print_path(ahc, scb);
2903 printf("Bus Device Reset Message Sent\n");
2905 * Clear our selection hardware in advance of
2906 * the busfree. We may have an entry in the waiting
2907 * Q for this target, and we don't want to go about
2908 * selecting while we handle the busfree and blow it
2911 ahc_outb(ahc, SCSISEQ, (ahc_inb(ahc, SCSISEQ) & ~ENSELO));
2912 } else if ((scb->flags & SCB_ABORT) != 0) {
2913 if ((scb->hscb->control & TAG_ENB) != 0)
2914 ahc->msgout_buf[ahc->msgout_index++] = MSG_ABORT_TAG;
2916 ahc->msgout_buf[ahc->msgout_index++] = MSG_ABORT;
2918 ahc_print_path(ahc, scb);
2919 printf("Abort%s Message Sent\n",
2920 (scb->hscb->control & TAG_ENB) != 0 ? " Tag" : "");
2922 * Clear our selection hardware in advance of
2923 * the busfree. We may have an entry in the waiting
2924 * Q for this target, and we don't want to go about
2925 * selecting while we handle the busfree and blow it
2928 ahc_outb(ahc, SCSISEQ, (ahc_inb(ahc, SCSISEQ) & ~ENSELO));
2929 } else if ((scb->flags & (SCB_AUTO_NEGOTIATE|SCB_NEGOTIATE)) != 0) {
2930 ahc_build_transfer_msg(ahc, devinfo);
2932 printf("ahc_intr: AWAITING_MSG for an SCB that "
2933 "does not have a waiting message\n");
2934 printf("SCSIID = %x, target_mask = %x\n", scb->hscb->scsiid,
2935 devinfo->target_mask);
2936 panic("SCB = %d, SCB Control = %x, MSG_OUT = %x "
2937 "SCB flags = %x", scb->hscb->tag, scb->hscb->control,
2938 ahc_inb(ahc, MSG_OUT), scb->flags);
2942 * Clear the MK_MESSAGE flag from the SCB so we aren't
2943 * asked to send this message again.
2945 ahc_outb(ahc, SCB_CONTROL, ahc_inb(ahc, SCB_CONTROL) & ~MK_MESSAGE);
2946 scb->hscb->control &= ~MK_MESSAGE;
2947 ahc->msgout_index = 0;
2948 ahc->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
2952 * Build an appropriate transfer negotiation message for the
2953 * currently active target.
2956 ahc_build_transfer_msg(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
2959 * We need to initiate transfer negotiations.
2960 * If our current and goal settings are identical,
2961 * we want to renegotiate due to a check condition.
2963 struct ahc_initiator_tinfo *tinfo;
2964 struct ahc_tmode_tstate *tstate;
2965 const struct ahc_syncrate *rate;
2973 tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid,
2974 devinfo->target, &tstate);
2976 * Filter our period based on the current connection.
2977 * If we can't perform DT transfers on this segment (not in LVD
2978 * mode for instance), then our decision to issue a PPR message
2981 period = tinfo->goal.period;
2982 offset = tinfo->goal.offset;
2983 ppr_options = tinfo->goal.ppr_options;
2984 /* Target initiated PPR is not allowed in the SCSI spec */
2985 if (devinfo->role == ROLE_TARGET)
2987 rate = ahc_devlimited_syncrate(ahc, tinfo, &period,
2988 &ppr_options, devinfo->role);
2989 dowide = tinfo->curr.width != tinfo->goal.width;
2990 dosync = tinfo->curr.offset != offset || tinfo->curr.period != period;
2992 * Only use PPR if we have options that need it, even if the device
2993 * claims to support it. There might be an expander in the way
2996 doppr = ppr_options != 0;
2998 if (!dowide && !dosync && !doppr) {
2999 dowide = tinfo->goal.width != MSG_EXT_WDTR_BUS_8_BIT;
3000 dosync = tinfo->goal.offset != 0;
3003 if (!dowide && !dosync && !doppr) {
3005 * Force async with a WDTR message if we have a wide bus,
3006 * or just issue an SDTR with a 0 offset.
3008 if ((ahc->features & AHC_WIDE) != 0)
3014 ahc_print_devinfo(ahc, devinfo);
3015 printf("Ensuring async\n");
3019 /* Target initiated PPR is not allowed in the SCSI spec */
3020 if (devinfo->role == ROLE_TARGET)
3024 * Both the PPR message and SDTR message require the
3025 * goal syncrate to be limited to what the target device
3026 * is capable of handling (based on whether an LVD->SE
3027 * expander is on the bus), so combine these two cases.
3028 * Regardless, guarantee that if we are using WDTR and SDTR
3029 * messages that WDTR comes first.
3031 if (doppr || (dosync && !dowide)) {
3033 offset = tinfo->goal.offset;
3034 ahc_validate_offset(ahc, tinfo, rate, &offset,
3035 doppr ? tinfo->goal.width
3036 : tinfo->curr.width,
3039 ahc_construct_ppr(ahc, devinfo, period, offset,
3040 tinfo->goal.width, ppr_options);
3042 ahc_construct_sdtr(ahc, devinfo, period, offset);
3045 ahc_construct_wdtr(ahc, devinfo, tinfo->goal.width);
3050 * Build a synchronous negotiation message in our message
3051 * buffer based on the input parameters.
3054 ahc_construct_sdtr(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
3055 u_int period, u_int offset)
3058 period = AHC_ASYNC_XFER_PERIOD;
3059 ahc->msgout_index += spi_populate_sync_msg(
3060 ahc->msgout_buf + ahc->msgout_index, period, offset);
3061 ahc->msgout_len += 5;
3063 printf("(%s:%c:%d:%d): Sending SDTR period %x, offset %x\n",
3064 ahc_name(ahc), devinfo->channel, devinfo->target,
3065 devinfo->lun, period, offset);
3070 * Build a wide negotiation message in our message
3071 * buffer based on the input parameters.
3074 ahc_construct_wdtr(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
3077 ahc->msgout_index += spi_populate_width_msg(
3078 ahc->msgout_buf + ahc->msgout_index, bus_width);
3079 ahc->msgout_len += 4;
3081 printf("(%s:%c:%d:%d): Sending WDTR %x\n",
3082 ahc_name(ahc), devinfo->channel, devinfo->target,
3083 devinfo->lun, bus_width);
3088 * Build a parallel protocol request message in our message
3089 * buffer based on the input parameters.
3092 ahc_construct_ppr(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
3093 u_int period, u_int offset, u_int bus_width,
3097 period = AHC_ASYNC_XFER_PERIOD;
3098 ahc->msgout_index += spi_populate_ppr_msg(
3099 ahc->msgout_buf + ahc->msgout_index, period, offset,
3100 bus_width, ppr_options);
3101 ahc->msgout_len += 8;
3103 printf("(%s:%c:%d:%d): Sending PPR bus_width %x, period %x, "
3104 "offset %x, ppr_options %x\n", ahc_name(ahc),
3105 devinfo->channel, devinfo->target, devinfo->lun,
3106 bus_width, period, offset, ppr_options);
3111 * Clear any active message state.
3114 ahc_clear_msg_state(struct ahc_softc *ahc)
3116 ahc->msgout_len = 0;
3117 ahc->msgin_index = 0;
3118 ahc->msg_type = MSG_TYPE_NONE;
3119 if ((ahc_inb(ahc, SCSISIGI) & ATNI) != 0) {
3121 * The target didn't care to respond to our
3122 * message request, so clear ATN.
3124 ahc_outb(ahc, CLRSINT1, CLRATNO);
3126 ahc_outb(ahc, MSG_OUT, MSG_NOOP);
3127 ahc_outb(ahc, SEQ_FLAGS2,
3128 ahc_inb(ahc, SEQ_FLAGS2) & ~TARGET_MSG_PENDING);
3132 ahc_handle_proto_violation(struct ahc_softc *ahc)
3134 struct ahc_devinfo devinfo;
3142 ahc_fetch_devinfo(ahc, &devinfo);
3143 scbid = ahc_inb(ahc, SCB_TAG);
3144 scb = ahc_lookup_scb(ahc, scbid);
3145 seq_flags = ahc_inb(ahc, SEQ_FLAGS);
3146 curphase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK;
3147 lastphase = ahc_inb(ahc, LASTPHASE);
3148 if ((seq_flags & NOT_IDENTIFIED) != 0) {
3151 * The reconnecting target either did not send an
3152 * identify message, or did, but we didn't find an SCB
3155 ahc_print_devinfo(ahc, &devinfo);
3156 printf("Target did not send an IDENTIFY message. "
3157 "LASTPHASE = 0x%x.\n", lastphase);
3159 } else if (scb == NULL) {
3161 * We don't seem to have an SCB active for this
3162 * transaction. Print an error and reset the bus.
3164 ahc_print_devinfo(ahc, &devinfo);
3165 printf("No SCB found during protocol violation\n");
3166 goto proto_violation_reset;
3168 ahc_set_transaction_status(scb, CAM_SEQUENCE_FAIL);
3169 if ((seq_flags & NO_CDB_SENT) != 0) {
3170 ahc_print_path(ahc, scb);
3171 printf("No or incomplete CDB sent to device.\n");
3172 } else if ((ahc_inb(ahc, SCB_CONTROL) & STATUS_RCVD) == 0) {
3174 * The target never bothered to provide status to
3175 * us prior to completing the command. Since we don't
3176 * know the disposition of this command, we must attempt
3177 * to abort it. Assert ATN and prepare to send an abort
3180 ahc_print_path(ahc, scb);
3181 printf("Completed command without status.\n");
3183 ahc_print_path(ahc, scb);
3184 printf("Unknown protocol violation.\n");
3185 ahc_dump_card_state(ahc);
3188 if ((lastphase & ~P_DATAIN_DT) == 0
3189 || lastphase == P_COMMAND) {
3190 proto_violation_reset:
3192 * Target either went directly to data/command
3193 * phase or didn't respond to our ATN.
3194 * The only safe thing to do is to blow
3195 * it away with a bus reset.
3197 found = ahc_reset_channel(ahc, 'A', TRUE);
3198 printf("%s: Issued Channel %c Bus Reset. "
3199 "%d SCBs aborted\n", ahc_name(ahc), 'A', found);
3202 * Leave the selection hardware off in case
3203 * this abort attempt will affect yet to
3206 ahc_outb(ahc, SCSISEQ,
3207 ahc_inb(ahc, SCSISEQ) & ~ENSELO);
3208 ahc_assert_atn(ahc);
3209 ahc_outb(ahc, MSG_OUT, HOST_MSG);
3211 ahc_print_devinfo(ahc, &devinfo);
3212 ahc->msgout_buf[0] = MSG_ABORT_TASK;
3213 ahc->msgout_len = 1;
3214 ahc->msgout_index = 0;
3215 ahc->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
3217 ahc_print_path(ahc, scb);
3218 scb->flags |= SCB_ABORT;
3220 printf("Protocol violation %s. Attempting to abort.\n",
3221 ahc_lookup_phase_entry(curphase)->phasemsg);
3226 * Manual message loop handler.
3229 ahc_handle_message_phase(struct ahc_softc *ahc)
3231 struct ahc_devinfo devinfo;
3235 ahc_fetch_devinfo(ahc, &devinfo);
3236 end_session = FALSE;
3237 bus_phase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK;
3240 switch (ahc->msg_type) {
3241 case MSG_TYPE_INITIATOR_MSGOUT:
3247 if (ahc->msgout_len == 0)
3248 panic("HOST_MSG_LOOP interrupt with no active message");
3251 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) {
3252 ahc_print_devinfo(ahc, &devinfo);
3253 printf("INITIATOR_MSG_OUT");
3256 phasemis = bus_phase != P_MESGOUT;
3259 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) {
3260 printf(" PHASEMIS %s\n",
3261 ahc_lookup_phase_entry(bus_phase)
3265 if (bus_phase == P_MESGIN) {
3267 * Change gears and see if
3268 * this messages is of interest to
3269 * us or should be passed back to
3272 ahc_outb(ahc, CLRSINT1, CLRATNO);
3273 ahc->send_msg_perror = FALSE;
3274 ahc->msg_type = MSG_TYPE_INITIATOR_MSGIN;
3275 ahc->msgin_index = 0;
3282 if (ahc->send_msg_perror) {
3283 ahc_outb(ahc, CLRSINT1, CLRATNO);
3284 ahc_outb(ahc, CLRSINT1, CLRREQINIT);
3286 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0)
3287 printf(" byte 0x%x\n", ahc->send_msg_perror);
3289 ahc_outb(ahc, SCSIDATL, MSG_PARITY_ERROR);
3293 msgdone = ahc->msgout_index == ahc->msgout_len;
3296 * The target has requested a retry.
3297 * Re-assert ATN, reset our message index to
3300 ahc->msgout_index = 0;
3301 ahc_assert_atn(ahc);
3304 lastbyte = ahc->msgout_index == (ahc->msgout_len - 1);
3306 /* Last byte is signified by dropping ATN */
3307 ahc_outb(ahc, CLRSINT1, CLRATNO);
3311 * Clear our interrupt status and present
3312 * the next byte on the bus.
3314 ahc_outb(ahc, CLRSINT1, CLRREQINIT);
3316 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0)
3317 printf(" byte 0x%x\n",
3318 ahc->msgout_buf[ahc->msgout_index]);
3320 ahc_outb(ahc, SCSIDATL, ahc->msgout_buf[ahc->msgout_index++]);
3323 case MSG_TYPE_INITIATOR_MSGIN:
3329 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) {
3330 ahc_print_devinfo(ahc, &devinfo);
3331 printf("INITIATOR_MSG_IN");
3334 phasemis = bus_phase != P_MESGIN;
3337 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) {
3338 printf(" PHASEMIS %s\n",
3339 ahc_lookup_phase_entry(bus_phase)
3343 ahc->msgin_index = 0;
3344 if (bus_phase == P_MESGOUT
3345 && (ahc->send_msg_perror == TRUE
3346 || (ahc->msgout_len != 0
3347 && ahc->msgout_index == 0))) {
3348 ahc->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
3355 /* Pull the byte in without acking it */
3356 ahc->msgin_buf[ahc->msgin_index] = ahc_inb(ahc, SCSIBUSL);
3358 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0)
3359 printf(" byte 0x%x\n",
3360 ahc->msgin_buf[ahc->msgin_index]);
3363 message_done = ahc_parse_msg(ahc, &devinfo);
3367 * Clear our incoming message buffer in case there
3368 * is another message following this one.
3370 ahc->msgin_index = 0;
3373 * If this message illicited a response,
3374 * assert ATN so the target takes us to the
3375 * message out phase.
3377 if (ahc->msgout_len != 0) {
3379 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) {
3380 ahc_print_devinfo(ahc, &devinfo);
3381 printf("Asserting ATN for response\n");
3384 ahc_assert_atn(ahc);
3389 if (message_done == MSGLOOP_TERMINATED) {
3393 ahc_outb(ahc, CLRSINT1, CLRREQINIT);
3394 ahc_inb(ahc, SCSIDATL);
3398 case MSG_TYPE_TARGET_MSGIN:
3403 if (ahc->msgout_len == 0)
3404 panic("Target MSGIN with no active message");
3407 * If we interrupted a mesgout session, the initiator
3408 * will not know this until our first REQ. So, we
3409 * only honor mesgout requests after we've sent our
3412 if ((ahc_inb(ahc, SCSISIGI) & ATNI) != 0
3413 && ahc->msgout_index > 0)
3414 msgout_request = TRUE;
3416 msgout_request = FALSE;
3418 if (msgout_request) {
3421 * Change gears and see if
3422 * this messages is of interest to
3423 * us or should be passed back to
3426 ahc->msg_type = MSG_TYPE_TARGET_MSGOUT;
3427 ahc_outb(ahc, SCSISIGO, P_MESGOUT | BSYO);
3428 ahc->msgin_index = 0;
3429 /* Dummy read to REQ for first byte */
3430 ahc_inb(ahc, SCSIDATL);
3431 ahc_outb(ahc, SXFRCTL0,
3432 ahc_inb(ahc, SXFRCTL0) | SPIOEN);
3436 msgdone = ahc->msgout_index == ahc->msgout_len;
3438 ahc_outb(ahc, SXFRCTL0,
3439 ahc_inb(ahc, SXFRCTL0) & ~SPIOEN);
3445 * Present the next byte on the bus.
3447 ahc_outb(ahc, SXFRCTL0, ahc_inb(ahc, SXFRCTL0) | SPIOEN);
3448 ahc_outb(ahc, SCSIDATL, ahc->msgout_buf[ahc->msgout_index++]);
3451 case MSG_TYPE_TARGET_MSGOUT:
3457 * The initiator signals that this is
3458 * the last byte by dropping ATN.
3460 lastbyte = (ahc_inb(ahc, SCSISIGI) & ATNI) == 0;
3463 * Read the latched byte, but turn off SPIOEN first
3464 * so that we don't inadvertently cause a REQ for the
3467 ahc_outb(ahc, SXFRCTL0, ahc_inb(ahc, SXFRCTL0) & ~SPIOEN);
3468 ahc->msgin_buf[ahc->msgin_index] = ahc_inb(ahc, SCSIDATL);
3469 msgdone = ahc_parse_msg(ahc, &devinfo);
3470 if (msgdone == MSGLOOP_TERMINATED) {
3472 * The message is *really* done in that it caused
3473 * us to go to bus free. The sequencer has already
3474 * been reset at this point, so pull the ejection
3483 * XXX Read spec about initiator dropping ATN too soon
3484 * and use msgdone to detect it.
3486 if (msgdone == MSGLOOP_MSGCOMPLETE) {
3487 ahc->msgin_index = 0;
3490 * If this message illicited a response, transition
3491 * to the Message in phase and send it.
3493 if (ahc->msgout_len != 0) {
3494 ahc_outb(ahc, SCSISIGO, P_MESGIN | BSYO);
3495 ahc_outb(ahc, SXFRCTL0,
3496 ahc_inb(ahc, SXFRCTL0) | SPIOEN);
3497 ahc->msg_type = MSG_TYPE_TARGET_MSGIN;
3498 ahc->msgin_index = 0;
3506 /* Ask for the next byte. */
3507 ahc_outb(ahc, SXFRCTL0,
3508 ahc_inb(ahc, SXFRCTL0) | SPIOEN);
3514 panic("Unknown REQINIT message type");
3518 ahc_clear_msg_state(ahc);
3519 ahc_outb(ahc, RETURN_1, EXIT_MSG_LOOP);
3521 ahc_outb(ahc, RETURN_1, CONT_MSG_LOOP);
3525 * See if we sent a particular extended message to the target.
3526 * If "full" is true, return true only if the target saw the full
3527 * message. If "full" is false, return true if the target saw at
3528 * least the first byte of the message.
3531 ahc_sent_msg(struct ahc_softc *ahc, ahc_msgtype type, u_int msgval, int full)
3539 while (index < ahc->msgout_len) {
3540 if (ahc->msgout_buf[index] == MSG_EXTENDED) {
3543 end_index = index + 1 + ahc->msgout_buf[index + 1];
3544 if (ahc->msgout_buf[index+2] == msgval
3545 && type == AHCMSG_EXT) {
3548 if (ahc->msgout_index > end_index)
3550 } else if (ahc->msgout_index > index)
3554 } else if (ahc->msgout_buf[index] >= MSG_SIMPLE_TASK
3555 && ahc->msgout_buf[index] <= MSG_IGN_WIDE_RESIDUE) {
3557 /* Skip tag type and tag id or residue param*/
3560 /* Single byte message */
3561 if (type == AHCMSG_1B
3562 && ahc->msgout_buf[index] == msgval
3563 && ahc->msgout_index > index)
3575 * Wait for a complete incoming message, parse it, and respond accordingly.
3578 ahc_parse_msg(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
3580 struct ahc_initiator_tinfo *tinfo;
3581 struct ahc_tmode_tstate *tstate;
3585 u_int targ_scsirate;
3587 done = MSGLOOP_IN_PROG;
3590 tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid,
3591 devinfo->target, &tstate);
3592 targ_scsirate = tinfo->scsirate;
3595 * Parse as much of the message as is available,
3596 * rejecting it if we don't support it. When
3597 * the entire message is available and has been
3598 * handled, return MSGLOOP_MSGCOMPLETE, indicating
3599 * that we have parsed an entire message.
3601 * In the case of extended messages, we accept the length
3602 * byte outright and perform more checking once we know the
3603 * extended message type.
3605 switch (ahc->msgin_buf[0]) {
3606 case MSG_DISCONNECT:
3607 case MSG_SAVEDATAPOINTER:
3608 case MSG_CMDCOMPLETE:
3609 case MSG_RESTOREPOINTERS:
3610 case MSG_IGN_WIDE_RESIDUE:
3612 * End our message loop as these are messages
3613 * the sequencer handles on its own.
3615 done = MSGLOOP_TERMINATED;
3617 case MSG_MESSAGE_REJECT:
3618 response = ahc_handle_msg_reject(ahc, devinfo);
3621 done = MSGLOOP_MSGCOMPLETE;
3625 /* Wait for enough of the message to begin validation */
3626 if (ahc->msgin_index < 2)
3628 switch (ahc->msgin_buf[2]) {
3631 const struct ahc_syncrate *syncrate;
3637 if (ahc->msgin_buf[1] != MSG_EXT_SDTR_LEN) {
3643 * Wait until we have both args before validating
3644 * and acting on this message.
3646 * Add one to MSG_EXT_SDTR_LEN to account for
3647 * the extended message preamble.
3649 if (ahc->msgin_index < (MSG_EXT_SDTR_LEN + 1))
3652 period = ahc->msgin_buf[3];
3654 saved_offset = offset = ahc->msgin_buf[4];
3655 syncrate = ahc_devlimited_syncrate(ahc, tinfo, &period,
3658 ahc_validate_offset(ahc, tinfo, syncrate, &offset,
3659 targ_scsirate & WIDEXFER,
3662 printf("(%s:%c:%d:%d): Received "
3663 "SDTR period %x, offset %x\n\t"
3664 "Filtered to period %x, offset %x\n",
3665 ahc_name(ahc), devinfo->channel,
3666 devinfo->target, devinfo->lun,
3667 ahc->msgin_buf[3], saved_offset,
3670 ahc_set_syncrate(ahc, devinfo,
3672 offset, ppr_options,
3673 AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
3677 * See if we initiated Sync Negotiation
3678 * and didn't have to fall down to async
3681 if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_SDTR, TRUE)) {
3683 if (saved_offset != offset) {
3684 /* Went too low - force async */
3689 * Send our own SDTR in reply
3692 && devinfo->role == ROLE_INITIATOR) {
3693 printf("(%s:%c:%d:%d): Target "
3695 ahc_name(ahc), devinfo->channel,
3696 devinfo->target, devinfo->lun);
3698 ahc->msgout_index = 0;
3699 ahc->msgout_len = 0;
3700 ahc_construct_sdtr(ahc, devinfo,
3702 ahc->msgout_index = 0;
3705 done = MSGLOOP_MSGCOMPLETE;
3712 u_int sending_reply;
3714 sending_reply = FALSE;
3715 if (ahc->msgin_buf[1] != MSG_EXT_WDTR_LEN) {
3721 * Wait until we have our arg before validating
3722 * and acting on this message.
3724 * Add one to MSG_EXT_WDTR_LEN to account for
3725 * the extended message preamble.
3727 if (ahc->msgin_index < (MSG_EXT_WDTR_LEN + 1))
3730 bus_width = ahc->msgin_buf[3];
3731 saved_width = bus_width;
3732 ahc_validate_width(ahc, tinfo, &bus_width,
3735 printf("(%s:%c:%d:%d): Received WDTR "
3736 "%x filtered to %x\n",
3737 ahc_name(ahc), devinfo->channel,
3738 devinfo->target, devinfo->lun,
3739 saved_width, bus_width);
3742 if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_WDTR, TRUE)) {
3744 * Don't send a WDTR back to the
3745 * target, since we asked first.
3746 * If the width went higher than our
3747 * request, reject it.
3749 if (saved_width > bus_width) {
3751 printf("(%s:%c:%d:%d): requested %dBit "
3752 "transfers. Rejecting...\n",
3753 ahc_name(ahc), devinfo->channel,
3754 devinfo->target, devinfo->lun,
3755 8 * (0x01 << bus_width));
3760 * Send our own WDTR in reply
3763 && devinfo->role == ROLE_INITIATOR) {
3764 printf("(%s:%c:%d:%d): Target "
3766 ahc_name(ahc), devinfo->channel,
3767 devinfo->target, devinfo->lun);
3769 ahc->msgout_index = 0;
3770 ahc->msgout_len = 0;
3771 ahc_construct_wdtr(ahc, devinfo, bus_width);
3772 ahc->msgout_index = 0;
3774 sending_reply = TRUE;
3777 * After a wide message, we are async, but
3778 * some devices don't seem to honor this portion
3779 * of the spec. Force a renegotiation of the
3780 * sync component of our transfer agreement even
3781 * if our goal is async. By updating our width
3782 * after forcing the negotiation, we avoid
3783 * renegotiating for width.
3785 ahc_update_neg_request(ahc, devinfo, tstate,
3786 tinfo, AHC_NEG_ALWAYS);
3787 ahc_set_width(ahc, devinfo, bus_width,
3788 AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
3790 if (sending_reply == FALSE && reject == FALSE) {
3793 * We will always have an SDTR to send.
3795 ahc->msgout_index = 0;
3796 ahc->msgout_len = 0;
3797 ahc_build_transfer_msg(ahc, devinfo);
3798 ahc->msgout_index = 0;
3801 done = MSGLOOP_MSGCOMPLETE;
3806 const struct ahc_syncrate *syncrate;
3813 u_int saved_ppr_options;
3815 if (ahc->msgin_buf[1] != MSG_EXT_PPR_LEN) {
3821 * Wait until we have all args before validating
3822 * and acting on this message.
3824 * Add one to MSG_EXT_PPR_LEN to account for
3825 * the extended message preamble.
3827 if (ahc->msgin_index < (MSG_EXT_PPR_LEN + 1))
3830 period = ahc->msgin_buf[3];
3831 offset = ahc->msgin_buf[5];
3832 bus_width = ahc->msgin_buf[6];
3833 saved_width = bus_width;
3834 ppr_options = ahc->msgin_buf[7];
3836 * According to the spec, a DT only
3837 * period factor with no DT option
3838 * set implies async.
3840 if ((ppr_options & MSG_EXT_PPR_DT_REQ) == 0
3843 saved_ppr_options = ppr_options;
3844 saved_offset = offset;
3847 * Mask out any options we don't support
3848 * on any controller. Transfer options are
3849 * only available if we are negotiating wide.
3851 ppr_options &= MSG_EXT_PPR_DT_REQ;
3855 ahc_validate_width(ahc, tinfo, &bus_width,
3857 syncrate = ahc_devlimited_syncrate(ahc, tinfo, &period,
3860 ahc_validate_offset(ahc, tinfo, syncrate,
3864 if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_PPR, TRUE)) {
3866 * If we are unable to do any of the
3867 * requested options (we went too low),
3868 * then we'll have to reject the message.
3870 if (saved_width > bus_width
3871 || saved_offset != offset
3872 || saved_ppr_options != ppr_options) {
3881 if (devinfo->role != ROLE_TARGET)
3882 printf("(%s:%c:%d:%d): Target "
3884 ahc_name(ahc), devinfo->channel,
3885 devinfo->target, devinfo->lun);
3887 printf("(%s:%c:%d:%d): Initiator "
3889 ahc_name(ahc), devinfo->channel,
3890 devinfo->target, devinfo->lun);
3891 ahc->msgout_index = 0;
3892 ahc->msgout_len = 0;
3893 ahc_construct_ppr(ahc, devinfo, period, offset,
3894 bus_width, ppr_options);
3895 ahc->msgout_index = 0;
3899 printf("(%s:%c:%d:%d): Received PPR width %x, "
3900 "period %x, offset %x,options %x\n"
3901 "\tFiltered to width %x, period %x, "
3902 "offset %x, options %x\n",
3903 ahc_name(ahc), devinfo->channel,
3904 devinfo->target, devinfo->lun,
3905 saved_width, ahc->msgin_buf[3],
3906 saved_offset, saved_ppr_options,
3907 bus_width, period, offset, ppr_options);
3909 ahc_set_width(ahc, devinfo, bus_width,
3910 AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
3912 ahc_set_syncrate(ahc, devinfo,
3914 offset, ppr_options,
3915 AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
3917 done = MSGLOOP_MSGCOMPLETE;
3921 /* Unknown extended message. Reject it. */
3927 #ifdef AHC_TARGET_MODE
3928 case MSG_BUS_DEV_RESET:
3929 ahc_handle_devreset(ahc, devinfo,
3931 "Bus Device Reset Received",
3932 /*verbose_level*/0);
3934 done = MSGLOOP_TERMINATED;
3938 case MSG_CLEAR_QUEUE:
3942 /* Target mode messages */
3943 if (devinfo->role != ROLE_TARGET) {
3947 tag = SCB_LIST_NULL;
3948 if (ahc->msgin_buf[0] == MSG_ABORT_TAG)
3949 tag = ahc_inb(ahc, INITIATOR_TAG);
3950 ahc_abort_scbs(ahc, devinfo->target, devinfo->channel,
3951 devinfo->lun, tag, ROLE_TARGET,
3954 tstate = ahc->enabled_targets[devinfo->our_scsiid];
3955 if (tstate != NULL) {
3956 struct ahc_tmode_lstate* lstate;
3958 lstate = tstate->enabled_luns[devinfo->lun];
3959 if (lstate != NULL) {
3960 ahc_queue_lstate_event(ahc, lstate,
3961 devinfo->our_scsiid,
3964 ahc_send_lstate_events(ahc, lstate);
3968 done = MSGLOOP_TERMINATED;
3972 case MSG_TERM_IO_PROC:
3980 * Setup to reject the message.
3982 ahc->msgout_index = 0;
3983 ahc->msgout_len = 1;
3984 ahc->msgout_buf[0] = MSG_MESSAGE_REJECT;
3985 done = MSGLOOP_MSGCOMPLETE;
3989 if (done != MSGLOOP_IN_PROG && !response)
3990 /* Clear the outgoing message buffer */
3991 ahc->msgout_len = 0;
3997 * Process a message reject message.
4000 ahc_handle_msg_reject(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
4003 * What we care about here is if we had an
4004 * outstanding SDTR or WDTR message for this
4005 * target. If we did, this is a signal that
4006 * the target is refusing negotiation.
4009 struct ahc_initiator_tinfo *tinfo;
4010 struct ahc_tmode_tstate *tstate;
4015 scb_index = ahc_inb(ahc, SCB_TAG);
4016 scb = ahc_lookup_scb(ahc, scb_index);
4017 tinfo = ahc_fetch_transinfo(ahc, devinfo->channel,
4018 devinfo->our_scsiid,
4019 devinfo->target, &tstate);
4020 /* Might be necessary */
4021 last_msg = ahc_inb(ahc, LAST_MSG);
4023 if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_PPR, /*full*/FALSE)) {
4025 * Target does not support the PPR message.
4026 * Attempt to negotiate SPI-2 style.
4029 printf("(%s:%c:%d:%d): PPR Rejected. "
4030 "Trying WDTR/SDTR\n",
4031 ahc_name(ahc), devinfo->channel,
4032 devinfo->target, devinfo->lun);
4034 tinfo->goal.ppr_options = 0;
4035 tinfo->curr.transport_version = 2;
4036 tinfo->goal.transport_version = 2;
4037 ahc->msgout_index = 0;
4038 ahc->msgout_len = 0;
4039 ahc_build_transfer_msg(ahc, devinfo);
4040 ahc->msgout_index = 0;
4042 } else if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_WDTR, /*full*/FALSE)) {
4044 /* note 8bit xfers */
4045 printf("(%s:%c:%d:%d): refuses WIDE negotiation. Using "
4046 "8bit transfers\n", ahc_name(ahc),
4047 devinfo->channel, devinfo->target, devinfo->lun);
4048 ahc_set_width(ahc, devinfo, MSG_EXT_WDTR_BUS_8_BIT,
4049 AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
4052 * No need to clear the sync rate. If the target
4053 * did not accept the command, our syncrate is
4054 * unaffected. If the target started the negotiation,
4055 * but rejected our response, we already cleared the
4056 * sync rate before sending our WDTR.
4058 if (tinfo->goal.offset != tinfo->curr.offset) {
4060 /* Start the sync negotiation */
4061 ahc->msgout_index = 0;
4062 ahc->msgout_len = 0;
4063 ahc_build_transfer_msg(ahc, devinfo);
4064 ahc->msgout_index = 0;
4067 } else if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_SDTR, /*full*/FALSE)) {
4068 /* note asynch xfers and clear flag */
4069 ahc_set_syncrate(ahc, devinfo, /*syncrate*/NULL, /*period*/0,
4070 /*offset*/0, /*ppr_options*/0,
4071 AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
4073 printf("(%s:%c:%d:%d): refuses synchronous negotiation. "
4074 "Using asynchronous transfers\n",
4075 ahc_name(ahc), devinfo->channel,
4076 devinfo->target, devinfo->lun);
4077 } else if ((scb->hscb->control & MSG_SIMPLE_TASK) != 0) {
4081 tag_type = (scb->hscb->control & MSG_SIMPLE_TASK);
4083 if (tag_type == MSG_SIMPLE_TASK) {
4084 printf("(%s:%c:%d:%d): refuses tagged commands. "
4085 "Performing non-tagged I/O\n", ahc_name(ahc),
4086 devinfo->channel, devinfo->target, devinfo->lun);
4087 ahc_set_tags(ahc, scb->io_ctx, devinfo, AHC_QUEUE_NONE);
4090 printf("(%s:%c:%d:%d): refuses %s tagged commands. "
4091 "Performing simple queue tagged I/O only\n",
4092 ahc_name(ahc), devinfo->channel, devinfo->target,
4093 devinfo->lun, tag_type == MSG_ORDERED_TASK
4094 ? "ordered" : "head of queue");
4095 ahc_set_tags(ahc, scb->io_ctx, devinfo, AHC_QUEUE_BASIC);
4100 * Resend the identify for this CCB as the target
4101 * may believe that the selection is invalid otherwise.
4103 ahc_outb(ahc, SCB_CONTROL,
4104 ahc_inb(ahc, SCB_CONTROL) & mask);
4105 scb->hscb->control &= mask;
4106 ahc_set_transaction_tag(scb, /*enabled*/FALSE,
4107 /*type*/MSG_SIMPLE_TASK);
4108 ahc_outb(ahc, MSG_OUT, MSG_IDENTIFYFLAG);
4109 ahc_assert_atn(ahc);
4112 * This transaction is now at the head of
4113 * the untagged queue for this target.
4115 if ((ahc->flags & AHC_SCB_BTT) == 0) {
4116 struct scb_tailq *untagged_q;
4119 &(ahc->untagged_queues[devinfo->target_offset]);
4120 TAILQ_INSERT_HEAD(untagged_q, scb, links.tqe);
4121 scb->flags |= SCB_UNTAGGEDQ;
4123 ahc_busy_tcl(ahc, BUILD_TCL(scb->hscb->scsiid, devinfo->lun),
4127 * Requeue all tagged commands for this target
4128 * currently in our posession so they can be
4129 * converted to untagged commands.
4131 ahc_search_qinfifo(ahc, SCB_GET_TARGET(ahc, scb),
4132 SCB_GET_CHANNEL(ahc, scb),
4133 SCB_GET_LUN(scb), /*tag*/SCB_LIST_NULL,
4134 ROLE_INITIATOR, CAM_REQUEUE_REQ,
4138 * Otherwise, we ignore it.
4140 printf("%s:%c:%d: Message reject for %x -- ignored\n",
4141 ahc_name(ahc), devinfo->channel, devinfo->target,
4148 * Process an ingnore wide residue message.
4151 ahc_handle_ign_wide_residue(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
4156 scb_index = ahc_inb(ahc, SCB_TAG);
4157 scb = ahc_lookup_scb(ahc, scb_index);
4159 * XXX Actually check data direction in the sequencer?
4160 * Perhaps add datadir to some spare bits in the hscb?
4162 if ((ahc_inb(ahc, SEQ_FLAGS) & DPHASE) == 0
4163 || ahc_get_transfer_dir(scb) != CAM_DIR_IN) {
4165 * Ignore the message if we haven't
4166 * seen an appropriate data phase yet.
4170 * If the residual occurred on the last
4171 * transfer and the transfer request was
4172 * expected to end on an odd count, do
4173 * nothing. Otherwise, subtract a byte
4174 * and update the residual count accordingly.
4178 sgptr = ahc_inb(ahc, SCB_RESIDUAL_SGPTR);
4179 if ((sgptr & SG_LIST_NULL) != 0
4180 && (ahc_inb(ahc, SCB_LUN) & SCB_XFERLEN_ODD) != 0) {
4182 * If the residual occurred on the last
4183 * transfer and the transfer request was
4184 * expected to end on an odd count, do
4188 struct ahc_dma_seg *sg;
4193 /* Pull in all of the sgptr */
4194 sgptr = ahc_inl(ahc, SCB_RESIDUAL_SGPTR);
4195 data_cnt = ahc_inl(ahc, SCB_RESIDUAL_DATACNT);
4197 if ((sgptr & SG_LIST_NULL) != 0) {
4199 * The residual data count is not updated
4200 * for the command run to completion case.
4201 * Explicitly zero the count.
4203 data_cnt &= ~AHC_SG_LEN_MASK;
4206 data_addr = ahc_inl(ahc, SHADDR);
4210 sgptr &= SG_PTR_MASK;
4212 sg = ahc_sg_bus_to_virt(scb, sgptr);
4215 * The residual sg ptr points to the next S/G
4216 * to load so we must go back one.
4219 sglen = ahc_le32toh(sg->len) & AHC_SG_LEN_MASK;
4220 if (sg != scb->sg_list
4221 && sglen < (data_cnt & AHC_SG_LEN_MASK)) {
4224 sglen = ahc_le32toh(sg->len);
4226 * Preserve High Address and SG_LIST bits
4227 * while setting the count to 1.
4229 data_cnt = 1 | (sglen & (~AHC_SG_LEN_MASK));
4230 data_addr = ahc_le32toh(sg->addr)
4231 + (sglen & AHC_SG_LEN_MASK) - 1;
4234 * Increment sg so it points to the
4238 sgptr = ahc_sg_virt_to_bus(scb, sg);
4240 ahc_outl(ahc, SCB_RESIDUAL_SGPTR, sgptr);
4241 ahc_outl(ahc, SCB_RESIDUAL_DATACNT, data_cnt);
4243 * Toggle the "oddness" of the transfer length
4244 * to handle this mid-transfer ignore wide
4245 * residue. This ensures that the oddness is
4246 * correct for subsequent data transfers.
4248 ahc_outb(ahc, SCB_LUN,
4249 ahc_inb(ahc, SCB_LUN) ^ SCB_XFERLEN_ODD);
4256 * Reinitialize the data pointers for the active transfer
4257 * based on its current residual.
4260 ahc_reinitialize_dataptrs(struct ahc_softc *ahc)
4263 struct ahc_dma_seg *sg;
4269 scb_index = ahc_inb(ahc, SCB_TAG);
4270 scb = ahc_lookup_scb(ahc, scb_index);
4271 sgptr = (ahc_inb(ahc, SCB_RESIDUAL_SGPTR + 3) << 24)
4272 | (ahc_inb(ahc, SCB_RESIDUAL_SGPTR + 2) << 16)
4273 | (ahc_inb(ahc, SCB_RESIDUAL_SGPTR + 1) << 8)
4274 | ahc_inb(ahc, SCB_RESIDUAL_SGPTR);
4276 sgptr &= SG_PTR_MASK;
4277 sg = ahc_sg_bus_to_virt(scb, sgptr);
4279 /* The residual sg_ptr always points to the next sg */
4282 resid = (ahc_inb(ahc, SCB_RESIDUAL_DATACNT + 2) << 16)
4283 | (ahc_inb(ahc, SCB_RESIDUAL_DATACNT + 1) << 8)
4284 | ahc_inb(ahc, SCB_RESIDUAL_DATACNT);
4286 dataptr = ahc_le32toh(sg->addr)
4287 + (ahc_le32toh(sg->len) & AHC_SG_LEN_MASK)
4289 if ((ahc->flags & AHC_39BIT_ADDRESSING) != 0) {
4292 dscommand1 = ahc_inb(ahc, DSCOMMAND1);
4293 ahc_outb(ahc, DSCOMMAND1, dscommand1 | HADDLDSEL0);
4294 ahc_outb(ahc, HADDR,
4295 (ahc_le32toh(sg->len) >> 24) & SG_HIGH_ADDR_BITS);
4296 ahc_outb(ahc, DSCOMMAND1, dscommand1);
4298 ahc_outb(ahc, HADDR + 3, dataptr >> 24);
4299 ahc_outb(ahc, HADDR + 2, dataptr >> 16);
4300 ahc_outb(ahc, HADDR + 1, dataptr >> 8);
4301 ahc_outb(ahc, HADDR, dataptr);
4302 ahc_outb(ahc, HCNT + 2, resid >> 16);
4303 ahc_outb(ahc, HCNT + 1, resid >> 8);
4304 ahc_outb(ahc, HCNT, resid);
4305 if ((ahc->features & AHC_ULTRA2) == 0) {
4306 ahc_outb(ahc, STCNT + 2, resid >> 16);
4307 ahc_outb(ahc, STCNT + 1, resid >> 8);
4308 ahc_outb(ahc, STCNT, resid);
4313 * Handle the effects of issuing a bus device reset message.
4316 ahc_handle_devreset(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
4317 cam_status status, char *message, int verbose_level)
4319 #ifdef AHC_TARGET_MODE
4320 struct ahc_tmode_tstate* tstate;
4325 found = ahc_abort_scbs(ahc, devinfo->target, devinfo->channel,
4326 CAM_LUN_WILDCARD, SCB_LIST_NULL, devinfo->role,
4329 #ifdef AHC_TARGET_MODE
4331 * Send an immediate notify ccb to all target mord peripheral
4332 * drivers affected by this action.
4334 tstate = ahc->enabled_targets[devinfo->our_scsiid];
4335 if (tstate != NULL) {
4336 for (lun = 0; lun < AHC_NUM_LUNS; lun++) {
4337 struct ahc_tmode_lstate* lstate;
4339 lstate = tstate->enabled_luns[lun];
4343 ahc_queue_lstate_event(ahc, lstate, devinfo->our_scsiid,
4344 MSG_BUS_DEV_RESET, /*arg*/0);
4345 ahc_send_lstate_events(ahc, lstate);
4351 * Go back to async/narrow transfers and renegotiate.
4353 ahc_set_width(ahc, devinfo, MSG_EXT_WDTR_BUS_8_BIT,
4354 AHC_TRANS_CUR, /*paused*/TRUE);
4355 ahc_set_syncrate(ahc, devinfo, /*syncrate*/NULL,
4356 /*period*/0, /*offset*/0, /*ppr_options*/0,
4357 AHC_TRANS_CUR, /*paused*/TRUE);
4359 if (status != CAM_SEL_TIMEOUT)
4360 ahc_send_async(ahc, devinfo->channel, devinfo->target,
4361 CAM_LUN_WILDCARD, AC_SENT_BDR);
4364 && (verbose_level <= bootverbose))
4365 printf("%s: %s on %c:%d. %d SCBs aborted\n", ahc_name(ahc),
4366 message, devinfo->channel, devinfo->target, found);
4369 #ifdef AHC_TARGET_MODE
4371 ahc_setup_target_msgin(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
4376 * To facilitate adding multiple messages together,
4377 * each routine should increment the index and len
4378 * variables instead of setting them explicitly.
4380 ahc->msgout_index = 0;
4381 ahc->msgout_len = 0;
4383 if (scb != NULL && (scb->flags & SCB_AUTO_NEGOTIATE) != 0)
4384 ahc_build_transfer_msg(ahc, devinfo);
4386 panic("ahc_intr: AWAITING target message with no message");
4388 ahc->msgout_index = 0;
4389 ahc->msg_type = MSG_TYPE_TARGET_MSGIN;
4392 /**************************** Initialization **********************************/
4394 * Allocate a controller structure for a new device
4395 * and perform initial initializion.
4398 ahc_alloc(void *platform_arg, char *name)
4400 struct ahc_softc *ahc;
4404 ahc = malloc(sizeof(*ahc), M_DEVBUF, M_NOWAIT);
4406 printf("aic7xxx: cannot malloc softc!\n");
4407 free(name, M_DEVBUF);
4411 ahc = device_get_softc((device_t)platform_arg);
4413 memset(ahc, 0, sizeof(*ahc));
4414 ahc->seep_config = malloc(sizeof(*ahc->seep_config),
4415 M_DEVBUF, M_NOWAIT);
4416 if (ahc->seep_config == NULL) {
4418 free(ahc, M_DEVBUF);
4420 free(name, M_DEVBUF);
4423 LIST_INIT(&ahc->pending_scbs);
4424 /* We don't know our unit number until the OSM sets it */
4427 ahc->description = NULL;
4429 ahc->channel_b = 'B';
4430 ahc->chip = AHC_NONE;
4431 ahc->features = AHC_FENONE;
4432 ahc->bugs = AHC_BUGNONE;
4433 ahc->flags = AHC_FNONE;
4435 * Default to all error reporting enabled with the
4436 * sequencer operating at its fastest speed.
4437 * The bus attach code may modify this.
4439 ahc->seqctl = FASTMODE;
4441 for (i = 0; i < AHC_NUM_TARGETS; i++)
4442 TAILQ_INIT(&ahc->untagged_queues[i]);
4443 if (ahc_platform_alloc(ahc, platform_arg) != 0) {
4451 ahc_softc_init(struct ahc_softc *ahc)
4454 /* The IRQMS bit is only valid on VL and EISA chips */
4455 if ((ahc->chip & AHC_PCI) == 0)
4456 ahc->unpause = ahc_inb(ahc, HCNTRL) & IRQMS;
4459 ahc->pause = ahc->unpause | PAUSE;
4460 /* XXX The shared scb data stuff should be deprecated */
4461 if (ahc->scb_data == NULL) {
4462 ahc->scb_data = malloc(sizeof(*ahc->scb_data),
4463 M_DEVBUF, M_NOWAIT);
4464 if (ahc->scb_data == NULL)
4466 memset(ahc->scb_data, 0, sizeof(*ahc->scb_data));
4473 ahc_set_unit(struct ahc_softc *ahc, int unit)
4479 ahc_set_name(struct ahc_softc *ahc, char *name)
4481 if (ahc->name != NULL)
4482 free(ahc->name, M_DEVBUF);
4487 ahc_free(struct ahc_softc *ahc)
4491 switch (ahc->init_level) {
4497 ahc_dmamap_unload(ahc, ahc->shared_data_dmat,
4498 ahc->shared_data_dmamap);
4501 ahc_dmamem_free(ahc, ahc->shared_data_dmat, ahc->qoutfifo,
4502 ahc->shared_data_dmamap);
4503 ahc_dmamap_destroy(ahc, ahc->shared_data_dmat,
4504 ahc->shared_data_dmamap);
4507 ahc_dma_tag_destroy(ahc, ahc->shared_data_dmat);
4510 ahc_dma_tag_destroy(ahc, ahc->buffer_dmat);
4518 ahc_dma_tag_destroy(ahc, ahc->parent_dmat);
4520 ahc_platform_free(ahc);
4521 ahc_fini_scbdata(ahc);
4522 for (i = 0; i < AHC_NUM_TARGETS; i++) {
4523 struct ahc_tmode_tstate *tstate;
4525 tstate = ahc->enabled_targets[i];
4526 if (tstate != NULL) {
4527 #ifdef AHC_TARGET_MODE
4530 for (j = 0; j < AHC_NUM_LUNS; j++) {
4531 struct ahc_tmode_lstate *lstate;
4533 lstate = tstate->enabled_luns[j];
4534 if (lstate != NULL) {
4535 xpt_free_path(lstate->path);
4536 free(lstate, M_DEVBUF);
4540 free(tstate, M_DEVBUF);
4543 #ifdef AHC_TARGET_MODE
4544 if (ahc->black_hole != NULL) {
4545 xpt_free_path(ahc->black_hole->path);
4546 free(ahc->black_hole, M_DEVBUF);
4549 if (ahc->name != NULL)
4550 free(ahc->name, M_DEVBUF);
4551 if (ahc->seep_config != NULL)
4552 free(ahc->seep_config, M_DEVBUF);
4554 free(ahc, M_DEVBUF);
4560 ahc_shutdown(void *arg)
4562 struct ahc_softc *ahc;
4565 ahc = (struct ahc_softc *)arg;
4567 /* This will reset most registers to 0, but not all */
4568 ahc_reset(ahc, /*reinit*/FALSE);
4569 ahc_outb(ahc, SCSISEQ, 0);
4570 ahc_outb(ahc, SXFRCTL0, 0);
4571 ahc_outb(ahc, DSPCISTATUS, 0);
4573 for (i = TARG_SCSIRATE; i < SCSICONF; i++)
4574 ahc_outb(ahc, i, 0);
4578 * Reset the controller and record some information about it
4579 * that is only available just after a reset. If "reinit" is
4580 * non-zero, this reset occured after initial configuration
4581 * and the caller requests that the chip be fully reinitialized
4582 * to a runable state. Chip interrupts are *not* enabled after
4583 * a reinitialization. The caller must enable interrupts via
4584 * ahc_intr_enable().
4587 ahc_reset(struct ahc_softc *ahc, int reinit)
4590 u_int sxfrctl1_a, sxfrctl1_b;
4595 * Preserve the value of the SXFRCTL1 register for all channels.
4596 * It contains settings that affect termination and we don't want
4597 * to disturb the integrity of the bus.
4601 if ((ahc->chip & AHC_CHIPID_MASK) == AHC_AIC7770) {
4605 * Save channel B's settings in case this chip
4606 * is setup for TWIN channel operation.
4608 sblkctl = ahc_inb(ahc, SBLKCTL);
4609 ahc_outb(ahc, SBLKCTL, sblkctl | SELBUSB);
4610 sxfrctl1_b = ahc_inb(ahc, SXFRCTL1);
4611 ahc_outb(ahc, SBLKCTL, sblkctl & ~SELBUSB);
4613 sxfrctl1_a = ahc_inb(ahc, SXFRCTL1);
4615 ahc_outb(ahc, HCNTRL, CHIPRST | ahc->pause);
4618 * Ensure that the reset has finished. We delay 1000us
4619 * prior to reading the register to make sure the chip
4620 * has sufficiently completed its reset to handle register
4626 } while (--wait && !(ahc_inb(ahc, HCNTRL) & CHIPRSTACK));
4629 printf("%s: WARNING - Failed chip reset! "
4630 "Trying to initialize anyway.\n", ahc_name(ahc));
4632 ahc_outb(ahc, HCNTRL, ahc->pause);
4634 /* Determine channel configuration */
4635 sblkctl = ahc_inb(ahc, SBLKCTL) & (SELBUSB|SELWIDE);
4636 /* No Twin Channel PCI cards */
4637 if ((ahc->chip & AHC_PCI) != 0)
4638 sblkctl &= ~SELBUSB;
4641 /* Single Narrow Channel */
4645 ahc->features |= AHC_WIDE;
4649 ahc->features |= AHC_TWIN;
4652 printf(" Unsupported adapter type. Ignoring\n");
4659 * We must always initialize STPWEN to 1 before we
4660 * restore the saved values. STPWEN is initialized
4661 * to a tri-state condition which can only be cleared
4664 if ((ahc->features & AHC_TWIN) != 0) {
4667 sblkctl = ahc_inb(ahc, SBLKCTL);
4668 ahc_outb(ahc, SBLKCTL, sblkctl | SELBUSB);
4669 ahc_outb(ahc, SXFRCTL1, sxfrctl1_b);
4670 ahc_outb(ahc, SBLKCTL, sblkctl & ~SELBUSB);
4672 ahc_outb(ahc, SXFRCTL1, sxfrctl1_a);
4677 * If a recovery action has forced a chip reset,
4678 * re-initialize the chip to our liking.
4680 error = ahc->bus_chip_init(ahc);
4690 * Determine the number of SCBs available on the controller
4693 ahc_probe_scbs(struct ahc_softc *ahc) {
4696 for (i = 0; i < AHC_SCB_MAX; i++) {
4698 ahc_outb(ahc, SCBPTR, i);
4699 ahc_outb(ahc, SCB_BASE, i);
4700 if (ahc_inb(ahc, SCB_BASE) != i)
4702 ahc_outb(ahc, SCBPTR, 0);
4703 if (ahc_inb(ahc, SCB_BASE) != 0)
4710 ahc_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
4714 baddr = (dma_addr_t *)arg;
4715 *baddr = segs->ds_addr;
4719 ahc_build_free_scb_list(struct ahc_softc *ahc)
4725 if ((ahc->flags & AHC_LSCBS_ENABLED) != 0)
4728 for (i = 0; i < ahc->scb_data->maxhscbs; i++) {
4731 ahc_outb(ahc, SCBPTR, i);
4734 * Touch all SCB bytes to avoid parity errors
4735 * should one of our debugging routines read
4736 * an otherwise uninitiatlized byte.
4738 for (j = 0; j < scbsize; j++)
4739 ahc_outb(ahc, SCB_BASE+j, 0xFF);
4741 /* Clear the control byte. */
4742 ahc_outb(ahc, SCB_CONTROL, 0);
4744 /* Set the next pointer */
4745 if ((ahc->flags & AHC_PAGESCBS) != 0)
4746 ahc_outb(ahc, SCB_NEXT, i+1);
4748 ahc_outb(ahc, SCB_NEXT, SCB_LIST_NULL);
4750 /* Make the tag number, SCSIID, and lun invalid */
4751 ahc_outb(ahc, SCB_TAG, SCB_LIST_NULL);
4752 ahc_outb(ahc, SCB_SCSIID, 0xFF);
4753 ahc_outb(ahc, SCB_LUN, 0xFF);
4756 if ((ahc->flags & AHC_PAGESCBS) != 0) {
4757 /* SCB 0 heads the free list. */
4758 ahc_outb(ahc, FREE_SCBH, 0);
4761 ahc_outb(ahc, FREE_SCBH, SCB_LIST_NULL);
4764 /* Make sure that the last SCB terminates the free list */
4765 ahc_outb(ahc, SCBPTR, i-1);
4766 ahc_outb(ahc, SCB_NEXT, SCB_LIST_NULL);
4770 ahc_init_scbdata(struct ahc_softc *ahc)
4772 struct scb_data *scb_data;
4774 scb_data = ahc->scb_data;
4775 SLIST_INIT(&scb_data->free_scbs);
4776 SLIST_INIT(&scb_data->sg_maps);
4778 /* Allocate SCB resources */
4779 scb_data->scbarray =
4780 (struct scb *)malloc(sizeof(struct scb) * AHC_SCB_MAX_ALLOC,
4781 M_DEVBUF, M_NOWAIT);
4782 if (scb_data->scbarray == NULL)
4784 memset(scb_data->scbarray, 0, sizeof(struct scb) * AHC_SCB_MAX_ALLOC);
4786 /* Determine the number of hardware SCBs and initialize them */
4788 scb_data->maxhscbs = ahc_probe_scbs(ahc);
4789 if (ahc->scb_data->maxhscbs == 0) {
4790 printf("%s: No SCB space found\n", ahc_name(ahc));
4795 * Create our DMA tags. These tags define the kinds of device
4796 * accessible memory allocations and memory mappings we will
4797 * need to perform during normal operation.
4799 * Unless we need to further restrict the allocation, we rely
4800 * on the restrictions of the parent dmat, hence the common
4801 * use of MAXADDR and MAXSIZE.
4804 /* DMA tag for our hardware scb structures */
4805 if (ahc_dma_tag_create(ahc, ahc->parent_dmat, /*alignment*/1,
4806 /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
4807 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
4808 /*highaddr*/BUS_SPACE_MAXADDR,
4809 /*filter*/NULL, /*filterarg*/NULL,
4810 AHC_SCB_MAX_ALLOC * sizeof(struct hardware_scb),
4812 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
4813 /*flags*/0, &scb_data->hscb_dmat) != 0) {
4817 scb_data->init_level++;
4819 /* Allocation for our hscbs */
4820 if (ahc_dmamem_alloc(ahc, scb_data->hscb_dmat,
4821 (void **)&scb_data->hscbs,
4822 BUS_DMA_NOWAIT, &scb_data->hscb_dmamap) != 0) {
4826 scb_data->init_level++;
4828 /* And permanently map them */
4829 ahc_dmamap_load(ahc, scb_data->hscb_dmat, scb_data->hscb_dmamap,
4831 AHC_SCB_MAX_ALLOC * sizeof(struct hardware_scb),
4832 ahc_dmamap_cb, &scb_data->hscb_busaddr, /*flags*/0);
4834 scb_data->init_level++;
4836 /* DMA tag for our sense buffers */
4837 if (ahc_dma_tag_create(ahc, ahc->parent_dmat, /*alignment*/1,
4838 /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
4839 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
4840 /*highaddr*/BUS_SPACE_MAXADDR,
4841 /*filter*/NULL, /*filterarg*/NULL,
4842 AHC_SCB_MAX_ALLOC * sizeof(struct scsi_sense_data),
4844 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
4845 /*flags*/0, &scb_data->sense_dmat) != 0) {
4849 scb_data->init_level++;
4852 if (ahc_dmamem_alloc(ahc, scb_data->sense_dmat,
4853 (void **)&scb_data->sense,
4854 BUS_DMA_NOWAIT, &scb_data->sense_dmamap) != 0) {
4858 scb_data->init_level++;
4860 /* And permanently map them */
4861 ahc_dmamap_load(ahc, scb_data->sense_dmat, scb_data->sense_dmamap,
4863 AHC_SCB_MAX_ALLOC * sizeof(struct scsi_sense_data),
4864 ahc_dmamap_cb, &scb_data->sense_busaddr, /*flags*/0);
4866 scb_data->init_level++;
4868 /* DMA tag for our S/G structures. We allocate in page sized chunks */
4869 if (ahc_dma_tag_create(ahc, ahc->parent_dmat, /*alignment*/8,
4870 /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
4871 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
4872 /*highaddr*/BUS_SPACE_MAXADDR,
4873 /*filter*/NULL, /*filterarg*/NULL,
4874 PAGE_SIZE, /*nsegments*/1,
4875 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
4876 /*flags*/0, &scb_data->sg_dmat) != 0) {
4880 scb_data->init_level++;
4882 /* Perform initial CCB allocation */
4883 memset(scb_data->hscbs, 0,
4884 AHC_SCB_MAX_ALLOC * sizeof(struct hardware_scb));
4885 ahc_alloc_scbs(ahc);
4887 if (scb_data->numscbs == 0) {
4888 printf("%s: ahc_init_scbdata - "
4889 "Unable to allocate initial scbs\n",
4895 * Reserve the next queued SCB.
4897 ahc->next_queued_scb = ahc_get_scb(ahc);
4900 * Note that we were successfull
4910 ahc_fini_scbdata(struct ahc_softc *ahc)
4912 struct scb_data *scb_data;
4914 scb_data = ahc->scb_data;
4915 if (scb_data == NULL)
4918 switch (scb_data->init_level) {
4922 struct sg_map_node *sg_map;
4924 while ((sg_map = SLIST_FIRST(&scb_data->sg_maps))!= NULL) {
4925 SLIST_REMOVE_HEAD(&scb_data->sg_maps, links);
4926 ahc_dmamap_unload(ahc, scb_data->sg_dmat,
4928 ahc_dmamem_free(ahc, scb_data->sg_dmat,
4931 free(sg_map, M_DEVBUF);
4933 ahc_dma_tag_destroy(ahc, scb_data->sg_dmat);
4936 ahc_dmamap_unload(ahc, scb_data->sense_dmat,
4937 scb_data->sense_dmamap);
4939 ahc_dmamem_free(ahc, scb_data->sense_dmat, scb_data->sense,
4940 scb_data->sense_dmamap);
4941 ahc_dmamap_destroy(ahc, scb_data->sense_dmat,
4942 scb_data->sense_dmamap);
4944 ahc_dma_tag_destroy(ahc, scb_data->sense_dmat);
4946 ahc_dmamap_unload(ahc, scb_data->hscb_dmat,
4947 scb_data->hscb_dmamap);
4949 ahc_dmamem_free(ahc, scb_data->hscb_dmat, scb_data->hscbs,
4950 scb_data->hscb_dmamap);
4951 ahc_dmamap_destroy(ahc, scb_data->hscb_dmat,
4952 scb_data->hscb_dmamap);
4954 ahc_dma_tag_destroy(ahc, scb_data->hscb_dmat);
4959 if (scb_data->scbarray != NULL)
4960 free(scb_data->scbarray, M_DEVBUF);
4964 ahc_alloc_scbs(struct ahc_softc *ahc)
4966 struct scb_data *scb_data;
4967 struct scb *next_scb;
4968 struct sg_map_node *sg_map;
4969 dma_addr_t physaddr;
4970 struct ahc_dma_seg *segs;
4974 scb_data = ahc->scb_data;
4975 if (scb_data->numscbs >= AHC_SCB_MAX_ALLOC)
4976 /* Can't allocate any more */
4979 next_scb = &scb_data->scbarray[scb_data->numscbs];
4981 sg_map = malloc(sizeof(*sg_map), M_DEVBUF, M_NOWAIT);
4986 /* Allocate S/G space for the next batch of SCBS */
4987 if (ahc_dmamem_alloc(ahc, scb_data->sg_dmat,
4988 (void **)&sg_map->sg_vaddr,
4989 BUS_DMA_NOWAIT, &sg_map->sg_dmamap) != 0) {
4990 free(sg_map, M_DEVBUF);
4994 SLIST_INSERT_HEAD(&scb_data->sg_maps, sg_map, links);
4996 ahc_dmamap_load(ahc, scb_data->sg_dmat, sg_map->sg_dmamap,
4997 sg_map->sg_vaddr, PAGE_SIZE, ahc_dmamap_cb,
4998 &sg_map->sg_physaddr, /*flags*/0);
5000 segs = sg_map->sg_vaddr;
5001 physaddr = sg_map->sg_physaddr;
5003 newcount = (PAGE_SIZE / (AHC_NSEG * sizeof(struct ahc_dma_seg)));
5004 newcount = min(newcount, (AHC_SCB_MAX_ALLOC - scb_data->numscbs));
5005 for (i = 0; i < newcount; i++) {
5006 struct scb_platform_data *pdata;
5010 pdata = (struct scb_platform_data *)malloc(sizeof(*pdata),
5011 M_DEVBUF, M_NOWAIT);
5014 next_scb->platform_data = pdata;
5015 next_scb->sg_map = sg_map;
5016 next_scb->sg_list = segs;
5018 * The sequencer always starts with the second entry.
5019 * The first entry is embedded in the scb.
5021 next_scb->sg_list_phys = physaddr + sizeof(struct ahc_dma_seg);
5022 next_scb->ahc_softc = ahc;
5023 next_scb->flags = SCB_FREE;
5025 error = ahc_dmamap_create(ahc, ahc->buffer_dmat, /*flags*/0,
5030 next_scb->hscb = &scb_data->hscbs[scb_data->numscbs];
5031 next_scb->hscb->tag = ahc->scb_data->numscbs;
5032 SLIST_INSERT_HEAD(&ahc->scb_data->free_scbs,
5033 next_scb, links.sle);
5035 physaddr += (AHC_NSEG * sizeof(struct ahc_dma_seg));
5037 ahc->scb_data->numscbs++;
5042 ahc_controller_info(struct ahc_softc *ahc, char *buf)
5046 len = sprintf(buf, "%s: ", ahc_chip_names[ahc->chip & AHC_CHIPID_MASK]);
5048 if ((ahc->features & AHC_TWIN) != 0)
5049 len = sprintf(buf, "Twin Channel, A SCSI Id=%d, "
5050 "B SCSI Id=%d, primary %c, ",
5051 ahc->our_id, ahc->our_id_b,
5052 (ahc->flags & AHC_PRIMARY_CHANNEL) + 'A');
5058 if ((ahc->features & AHC_ULTRA) != 0) {
5060 } else if ((ahc->features & AHC_DT) != 0) {
5061 speed = "Ultra160 ";
5062 } else if ((ahc->features & AHC_ULTRA2) != 0) {
5065 if ((ahc->features & AHC_WIDE) != 0) {
5070 len = sprintf(buf, "%s%s Channel %c, SCSI Id=%d, ",
5071 speed, type, ahc->channel, ahc->our_id);
5075 if ((ahc->flags & AHC_PAGESCBS) != 0)
5076 sprintf(buf, "%d/%d SCBs",
5077 ahc->scb_data->maxhscbs, AHC_MAX_QUEUE);
5079 sprintf(buf, "%d SCBs", ahc->scb_data->maxhscbs);
5083 ahc_chip_init(struct ahc_softc *ahc)
5089 u_int scsiseq_template;
5092 ahc_outb(ahc, SEQ_FLAGS, 0);
5093 ahc_outb(ahc, SEQ_FLAGS2, 0);
5095 /* Set the SCSI Id, SXFRCTL0, SXFRCTL1, and SIMODE1, for both channels*/
5096 if (ahc->features & AHC_TWIN) {
5099 * Setup Channel B first.
5101 ahc_outb(ahc, SBLKCTL, ahc_inb(ahc, SBLKCTL) | SELBUSB);
5102 term = (ahc->flags & AHC_TERM_ENB_B) != 0 ? STPWEN : 0;
5103 ahc_outb(ahc, SCSIID, ahc->our_id_b);
5104 scsi_conf = ahc_inb(ahc, SCSICONF + 1);
5105 ahc_outb(ahc, SXFRCTL1, (scsi_conf & (ENSPCHK|STIMESEL))
5106 |term|ahc->seltime_b|ENSTIMER|ACTNEGEN);
5107 if ((ahc->features & AHC_ULTRA2) != 0)
5108 ahc_outb(ahc, SIMODE0, ahc_inb(ahc, SIMODE0)|ENIOERR);
5109 ahc_outb(ahc, SIMODE1, ENSELTIMO|ENSCSIRST|ENSCSIPERR);
5110 ahc_outb(ahc, SXFRCTL0, DFON|SPIOEN);
5112 /* Select Channel A */
5113 ahc_outb(ahc, SBLKCTL, ahc_inb(ahc, SBLKCTL) & ~SELBUSB);
5115 term = (ahc->flags & AHC_TERM_ENB_A) != 0 ? STPWEN : 0;
5116 if ((ahc->features & AHC_ULTRA2) != 0)
5117 ahc_outb(ahc, SCSIID_ULTRA2, ahc->our_id);
5119 ahc_outb(ahc, SCSIID, ahc->our_id);
5120 scsi_conf = ahc_inb(ahc, SCSICONF);
5121 ahc_outb(ahc, SXFRCTL1, (scsi_conf & (ENSPCHK|STIMESEL))
5123 |ENSTIMER|ACTNEGEN);
5124 if ((ahc->features & AHC_ULTRA2) != 0)
5125 ahc_outb(ahc, SIMODE0, ahc_inb(ahc, SIMODE0)|ENIOERR);
5126 ahc_outb(ahc, SIMODE1, ENSELTIMO|ENSCSIRST|ENSCSIPERR);
5127 ahc_outb(ahc, SXFRCTL0, DFON|SPIOEN);
5129 /* There are no untagged SCBs active yet. */
5130 for (i = 0; i < 16; i++) {
5131 ahc_unbusy_tcl(ahc, BUILD_TCL(i << 4, 0));
5132 if ((ahc->flags & AHC_SCB_BTT) != 0) {
5136 * The SCB based BTT allows an entry per
5137 * target and lun pair.
5139 for (lun = 1; lun < AHC_NUM_LUNS; lun++)
5140 ahc_unbusy_tcl(ahc, BUILD_TCL(i << 4, lun));
5144 /* All of our queues are empty */
5145 for (i = 0; i < 256; i++)
5146 ahc->qoutfifo[i] = SCB_LIST_NULL;
5147 ahc_sync_qoutfifo(ahc, BUS_DMASYNC_PREREAD);
5149 for (i = 0; i < 256; i++)
5150 ahc->qinfifo[i] = SCB_LIST_NULL;
5152 if ((ahc->features & AHC_MULTI_TID) != 0) {
5153 ahc_outb(ahc, TARGID, 0);
5154 ahc_outb(ahc, TARGID + 1, 0);
5158 * Tell the sequencer where it can find our arrays in memory.
5160 physaddr = ahc->scb_data->hscb_busaddr;
5161 ahc_outb(ahc, HSCB_ADDR, physaddr & 0xFF);
5162 ahc_outb(ahc, HSCB_ADDR + 1, (physaddr >> 8) & 0xFF);
5163 ahc_outb(ahc, HSCB_ADDR + 2, (physaddr >> 16) & 0xFF);
5164 ahc_outb(ahc, HSCB_ADDR + 3, (physaddr >> 24) & 0xFF);
5166 physaddr = ahc->shared_data_busaddr;
5167 ahc_outb(ahc, SHARED_DATA_ADDR, physaddr & 0xFF);
5168 ahc_outb(ahc, SHARED_DATA_ADDR + 1, (physaddr >> 8) & 0xFF);
5169 ahc_outb(ahc, SHARED_DATA_ADDR + 2, (physaddr >> 16) & 0xFF);
5170 ahc_outb(ahc, SHARED_DATA_ADDR + 3, (physaddr >> 24) & 0xFF);
5173 * Initialize the group code to command length table.
5174 * This overrides the values in TARG_SCSIRATE, so only
5175 * setup the table after we have processed that information.
5177 ahc_outb(ahc, CMDSIZE_TABLE, 5);
5178 ahc_outb(ahc, CMDSIZE_TABLE + 1, 9);
5179 ahc_outb(ahc, CMDSIZE_TABLE + 2, 9);
5180 ahc_outb(ahc, CMDSIZE_TABLE + 3, 0);
5181 ahc_outb(ahc, CMDSIZE_TABLE + 4, 15);
5182 ahc_outb(ahc, CMDSIZE_TABLE + 5, 11);
5183 ahc_outb(ahc, CMDSIZE_TABLE + 6, 0);
5184 ahc_outb(ahc, CMDSIZE_TABLE + 7, 0);
5186 if ((ahc->features & AHC_HS_MAILBOX) != 0)
5187 ahc_outb(ahc, HS_MAILBOX, 0);
5189 /* Tell the sequencer of our initial queue positions */
5190 if ((ahc->features & AHC_TARGETMODE) != 0) {
5191 ahc->tqinfifonext = 1;
5192 ahc_outb(ahc, KERNEL_TQINPOS, ahc->tqinfifonext - 1);
5193 ahc_outb(ahc, TQINPOS, ahc->tqinfifonext);
5195 ahc->qinfifonext = 0;
5196 ahc->qoutfifonext = 0;
5197 if ((ahc->features & AHC_QUEUE_REGS) != 0) {
5198 ahc_outb(ahc, QOFF_CTLSTA, SCB_QSIZE_256);
5199 ahc_outb(ahc, HNSCB_QOFF, ahc->qinfifonext);
5200 ahc_outb(ahc, SNSCB_QOFF, ahc->qinfifonext);
5201 ahc_outb(ahc, SDSCB_QOFF, 0);
5203 ahc_outb(ahc, KERNEL_QINPOS, ahc->qinfifonext);
5204 ahc_outb(ahc, QINPOS, ahc->qinfifonext);
5205 ahc_outb(ahc, QOUTPOS, ahc->qoutfifonext);
5208 /* We don't have any waiting selections */
5209 ahc_outb(ahc, WAITING_SCBH, SCB_LIST_NULL);
5211 /* Our disconnection list is empty too */
5212 ahc_outb(ahc, DISCONNECTED_SCBH, SCB_LIST_NULL);
5214 /* Message out buffer starts empty */
5215 ahc_outb(ahc, MSG_OUT, MSG_NOOP);
5218 * Setup the allowed SCSI Sequences based on operational mode.
5219 * If we are a target, we'll enalbe select in operations once
5220 * we've had a lun enabled.
5222 scsiseq_template = ENSELO|ENAUTOATNO|ENAUTOATNP;
5223 if ((ahc->flags & AHC_INITIATORROLE) != 0)
5224 scsiseq_template |= ENRSELI;
5225 ahc_outb(ahc, SCSISEQ_TEMPLATE, scsiseq_template);
5227 /* Initialize our list of free SCBs. */
5228 ahc_build_free_scb_list(ahc);
5231 * Tell the sequencer which SCB will be the next one it receives.
5233 ahc_outb(ahc, NEXT_QUEUED_SCB, ahc->next_queued_scb->hscb->tag);
5236 * Load the Sequencer program and Enable the adapter
5240 printf("%s: Downloading Sequencer Program...",
5243 error = ahc_loadseq(ahc);
5247 if ((ahc->features & AHC_ULTRA2) != 0) {
5251 * Wait for up to 500ms for our transceivers
5252 * to settle. If the adapter does not have
5253 * a cable attached, the transceivers may
5254 * never settle, so don't complain if we
5258 (ahc_inb(ahc, SBLKCTL) & (ENAB40|ENAB20)) == 0 && wait;
5267 * Start the board, ready for normal operation
5270 ahc_init(struct ahc_softc *ahc)
5278 size_t driver_data_size;
5281 if ((ahc_debug & AHC_DEBUG_SEQUENCER) != 0)
5282 ahc->flags |= AHC_SEQUENCER_DEBUG;
5285 #ifdef AHC_PRINT_SRAM
5286 printf("Scratch Ram:");
5287 for (i = 0x20; i < 0x5f; i++) {
5288 if (((i % 8) == 0) && (i != 0)) {
5291 printf (" 0x%x", ahc_inb(ahc, i));
5293 if ((ahc->features & AHC_MORE_SRAM) != 0) {
5294 for (i = 0x70; i < 0x7f; i++) {
5295 if (((i % 8) == 0) && (i != 0)) {
5298 printf (" 0x%x", ahc_inb(ahc, i));
5303 * Reading uninitialized scratch ram may
5304 * generate parity errors.
5306 ahc_outb(ahc, CLRINT, CLRPARERR);
5307 ahc_outb(ahc, CLRINT, CLRBRKADRINT);
5312 * Assume we have a board at this stage and it has been reset.
5314 if ((ahc->flags & AHC_USEDEFAULTS) != 0)
5315 ahc->our_id = ahc->our_id_b = 7;
5318 * Default to allowing initiator operations.
5320 ahc->flags |= AHC_INITIATORROLE;
5323 * Only allow target mode features if this unit has them enabled.
5325 if ((AHC_TMODE_ENABLE & (0x1 << ahc->unit)) == 0)
5326 ahc->features &= ~AHC_TARGETMODE;
5329 /* DMA tag for mapping buffers into device visible space. */
5330 if (ahc_dma_tag_create(ahc, ahc->parent_dmat, /*alignment*/1,
5331 /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
5332 /*lowaddr*/ahc->flags & AHC_39BIT_ADDRESSING
5333 ? (dma_addr_t)0x7FFFFFFFFFULL
5334 : BUS_SPACE_MAXADDR_32BIT,
5335 /*highaddr*/BUS_SPACE_MAXADDR,
5336 /*filter*/NULL, /*filterarg*/NULL,
5337 /*maxsize*/(AHC_NSEG - 1) * PAGE_SIZE,
5338 /*nsegments*/AHC_NSEG,
5339 /*maxsegsz*/AHC_MAXTRANSFER_SIZE,
5340 /*flags*/BUS_DMA_ALLOCNOW,
5341 &ahc->buffer_dmat) != 0) {
5349 * DMA tag for our command fifos and other data in system memory
5350 * the card's sequencer must be able to access. For initiator
5351 * roles, we need to allocate space for the qinfifo and qoutfifo.
5352 * The qinfifo and qoutfifo are composed of 256 1 byte elements.
5353 * When providing for the target mode role, we must additionally
5354 * provide space for the incoming target command fifo and an extra
5355 * byte to deal with a dma bug in some chip versions.
5357 driver_data_size = 2 * 256 * sizeof(uint8_t);
5358 if ((ahc->features & AHC_TARGETMODE) != 0)
5359 driver_data_size += AHC_TMODE_CMDS * sizeof(struct target_cmd)
5360 + /*DMA WideOdd Bug Buffer*/1;
5361 if (ahc_dma_tag_create(ahc, ahc->parent_dmat, /*alignment*/1,
5362 /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
5363 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
5364 /*highaddr*/BUS_SPACE_MAXADDR,
5365 /*filter*/NULL, /*filterarg*/NULL,
5368 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
5369 /*flags*/0, &ahc->shared_data_dmat) != 0) {
5375 /* Allocation of driver data */
5376 if (ahc_dmamem_alloc(ahc, ahc->shared_data_dmat,
5377 (void **)&ahc->qoutfifo,
5378 BUS_DMA_NOWAIT, &ahc->shared_data_dmamap) != 0) {
5384 /* And permanently map it in */
5385 ahc_dmamap_load(ahc, ahc->shared_data_dmat, ahc->shared_data_dmamap,
5386 ahc->qoutfifo, driver_data_size, ahc_dmamap_cb,
5387 &ahc->shared_data_busaddr, /*flags*/0);
5389 if ((ahc->features & AHC_TARGETMODE) != 0) {
5390 ahc->targetcmds = (struct target_cmd *)ahc->qoutfifo;
5391 ahc->qoutfifo = (uint8_t *)&ahc->targetcmds[AHC_TMODE_CMDS];
5392 ahc->dma_bug_buf = ahc->shared_data_busaddr
5393 + driver_data_size - 1;
5394 /* All target command blocks start out invalid. */
5395 for (i = 0; i < AHC_TMODE_CMDS; i++)
5396 ahc->targetcmds[i].cmd_valid = 0;
5397 ahc_sync_tqinfifo(ahc, BUS_DMASYNC_PREREAD);
5398 ahc->qoutfifo = (uint8_t *)&ahc->targetcmds[256];
5400 ahc->qinfifo = &ahc->qoutfifo[256];
5404 /* Allocate SCB data now that buffer_dmat is initialized */
5405 if (ahc->scb_data->maxhscbs == 0)
5406 if (ahc_init_scbdata(ahc) != 0)
5410 * Allocate a tstate to house information for our
5411 * initiator presence on the bus as well as the user
5412 * data for any target mode initiator.
5414 if (ahc_alloc_tstate(ahc, ahc->our_id, 'A') == NULL) {
5415 printf("%s: unable to allocate ahc_tmode_tstate. "
5416 "Failing attach\n", ahc_name(ahc));
5420 if ((ahc->features & AHC_TWIN) != 0) {
5421 if (ahc_alloc_tstate(ahc, ahc->our_id_b, 'B') == NULL) {
5422 printf("%s: unable to allocate ahc_tmode_tstate. "
5423 "Failing attach\n", ahc_name(ahc));
5428 if (ahc->scb_data->maxhscbs < AHC_SCB_MAX_ALLOC) {
5429 ahc->flags |= AHC_PAGESCBS;
5431 ahc->flags &= ~AHC_PAGESCBS;
5435 if (ahc_debug & AHC_SHOW_MISC) {
5436 printf("%s: hardware scb %u bytes; kernel scb %u bytes; "
5437 "ahc_dma %u bytes\n",
5439 (u_int)sizeof(struct hardware_scb),
5440 (u_int)sizeof(struct scb),
5441 (u_int)sizeof(struct ahc_dma_seg));
5443 #endif /* AHC_DEBUG */
5446 * Look at the information that board initialization or
5447 * the board bios has left us.
5449 if (ahc->features & AHC_TWIN) {
5450 scsi_conf = ahc_inb(ahc, SCSICONF + 1);
5451 if ((scsi_conf & RESET_SCSI) != 0
5452 && (ahc->flags & AHC_INITIATORROLE) != 0)
5453 ahc->flags |= AHC_RESET_BUS_B;
5456 scsi_conf = ahc_inb(ahc, SCSICONF);
5457 if ((scsi_conf & RESET_SCSI) != 0
5458 && (ahc->flags & AHC_INITIATORROLE) != 0)
5459 ahc->flags |= AHC_RESET_BUS_A;
5462 tagenable = ALL_TARGETS_MASK;
5464 /* Grab the disconnection disable table and invert it for our needs */
5465 if ((ahc->flags & AHC_USEDEFAULTS) != 0) {
5466 printf("%s: Host Adapter Bios disabled. Using default SCSI "
5467 "device parameters\n", ahc_name(ahc));
5468 ahc->flags |= AHC_EXTENDED_TRANS_A|AHC_EXTENDED_TRANS_B|
5469 AHC_TERM_ENB_A|AHC_TERM_ENB_B;
5470 discenable = ALL_TARGETS_MASK;
5471 if ((ahc->features & AHC_ULTRA) != 0)
5472 ultraenb = ALL_TARGETS_MASK;
5474 discenable = ~((ahc_inb(ahc, DISC_DSB + 1) << 8)
5475 | ahc_inb(ahc, DISC_DSB));
5476 if ((ahc->features & (AHC_ULTRA|AHC_ULTRA2)) != 0)
5477 ultraenb = (ahc_inb(ahc, ULTRA_ENB + 1) << 8)
5478 | ahc_inb(ahc, ULTRA_ENB);
5481 if ((ahc->features & (AHC_WIDE|AHC_TWIN)) == 0)
5484 for (i = 0; i <= max_targ; i++) {
5485 struct ahc_initiator_tinfo *tinfo;
5486 struct ahc_tmode_tstate *tstate;
5492 our_id = ahc->our_id;
5494 if (i > 7 && (ahc->features & AHC_TWIN) != 0) {
5496 our_id = ahc->our_id_b;
5499 tinfo = ahc_fetch_transinfo(ahc, channel, our_id,
5500 target_id, &tstate);
5501 /* Default to async narrow across the board */
5502 memset(tinfo, 0, sizeof(*tinfo));
5503 if (ahc->flags & AHC_USEDEFAULTS) {
5504 if ((ahc->features & AHC_WIDE) != 0)
5505 tinfo->user.width = MSG_EXT_WDTR_BUS_16_BIT;
5508 * These will be truncated when we determine the
5509 * connection type we have with the target.
5511 tinfo->user.period = ahc_syncrates->period;
5512 tinfo->user.offset = MAX_OFFSET;
5517 /* Take the settings leftover in scratch RAM. */
5518 scsirate = ahc_inb(ahc, TARG_SCSIRATE + i);
5520 if ((ahc->features & AHC_ULTRA2) != 0) {
5524 if ((scsirate & SOFS) == 0x0F) {
5526 * Haven't negotiated yet,
5527 * so the format is different.
5529 scsirate = (scsirate & SXFR) >> 4
5532 | (scsirate & WIDEXFER);
5533 offset = MAX_OFFSET_ULTRA2;
5535 offset = ahc_inb(ahc, TARG_OFFSET + i);
5536 if ((scsirate & ~WIDEXFER) == 0 && offset != 0)
5537 /* Set to the lowest sync rate, 5MHz */
5539 maxsync = AHC_SYNCRATE_ULTRA2;
5540 if ((ahc->features & AHC_DT) != 0)
5541 maxsync = AHC_SYNCRATE_DT;
5542 tinfo->user.period =
5543 ahc_find_period(ahc, scsirate, maxsync);
5545 tinfo->user.period = 0;
5547 tinfo->user.offset = MAX_OFFSET;
5548 if ((scsirate & SXFR_ULTRA2) <= 8/*10MHz*/
5549 && (ahc->features & AHC_DT) != 0)
5550 tinfo->user.ppr_options =
5552 } else if ((scsirate & SOFS) != 0) {
5553 if ((scsirate & SXFR) == 0x40
5554 && (ultraenb & mask) != 0) {
5555 /* Treat 10MHz as a non-ultra speed */
5559 tinfo->user.period =
5560 ahc_find_period(ahc, scsirate,
5562 ? AHC_SYNCRATE_ULTRA
5563 : AHC_SYNCRATE_FAST);
5564 if (tinfo->user.period != 0)
5565 tinfo->user.offset = MAX_OFFSET;
5567 if (tinfo->user.period == 0)
5568 tinfo->user.offset = 0;
5569 if ((scsirate & WIDEXFER) != 0
5570 && (ahc->features & AHC_WIDE) != 0)
5571 tinfo->user.width = MSG_EXT_WDTR_BUS_16_BIT;
5572 tinfo->user.protocol_version = 4;
5573 if ((ahc->features & AHC_DT) != 0)
5574 tinfo->user.transport_version = 3;
5576 tinfo->user.transport_version = 2;
5577 tinfo->goal.protocol_version = 2;
5578 tinfo->goal.transport_version = 2;
5579 tinfo->curr.protocol_version = 2;
5580 tinfo->curr.transport_version = 2;
5582 tstate->ultraenb = 0;
5584 ahc->user_discenable = discenable;
5585 ahc->user_tagenable = tagenable;
5587 return (ahc->bus_chip_init(ahc));
5591 ahc_intr_enable(struct ahc_softc *ahc, int enable)
5595 hcntrl = ahc_inb(ahc, HCNTRL);
5597 ahc->pause &= ~INTEN;
5598 ahc->unpause &= ~INTEN;
5601 ahc->pause |= INTEN;
5602 ahc->unpause |= INTEN;
5604 ahc_outb(ahc, HCNTRL, hcntrl);
5608 * Ensure that the card is paused in a location
5609 * outside of all critical sections and that all
5610 * pending work is completed prior to returning.
5611 * This routine should only be called from outside
5612 * an interrupt context.
5615 ahc_pause_and_flushwork(struct ahc_softc *ahc)
5622 ahc->flags |= AHC_ALL_INTERRUPTS;
5628 * Give the sequencer some time to service
5629 * any active selections.
5636 ahc_outb(ahc, SCSISEQ, ahc_inb(ahc, SCSISEQ) & ~ENSELO);
5637 intstat = ahc_inb(ahc, INTSTAT);
5638 if ((intstat & INT_PEND) == 0) {
5639 ahc_clear_critical_section(ahc);
5640 intstat = ahc_inb(ahc, INTSTAT);
5643 && (intstat != 0xFF || (ahc->features & AHC_REMOVABLE) == 0)
5644 && ((intstat & INT_PEND) != 0
5645 || (ahc_inb(ahc, SSTAT0) & (SELDO|SELINGO)) != 0));
5646 if (maxloops == 0) {
5647 printf("Infinite interrupt loop, INTSTAT = %x",
5648 ahc_inb(ahc, INTSTAT));
5650 ahc_platform_flushwork(ahc);
5651 ahc->flags &= ~AHC_ALL_INTERRUPTS;
5656 ahc_suspend(struct ahc_softc *ahc)
5659 ahc_pause_and_flushwork(ahc);
5661 if (LIST_FIRST(&ahc->pending_scbs) != NULL) {
5666 #ifdef AHC_TARGET_MODE
5668 * XXX What about ATIOs that have not yet been serviced?
5669 * Perhaps we should just refuse to be suspended if we
5670 * are acting in a target role.
5672 if (ahc->pending_device != NULL) {
5682 ahc_resume(struct ahc_softc *ahc)
5685 ahc_reset(ahc, /*reinit*/TRUE);
5686 ahc_intr_enable(ahc, TRUE);
5691 /************************** Busy Target Table *********************************/
5693 * Return the untagged transaction id for a given target/channel lun.
5694 * Optionally, clear the entry.
5697 ahc_index_busy_tcl(struct ahc_softc *ahc, u_int tcl)
5700 u_int target_offset;
5702 if ((ahc->flags & AHC_SCB_BTT) != 0) {
5705 saved_scbptr = ahc_inb(ahc, SCBPTR);
5706 ahc_outb(ahc, SCBPTR, TCL_LUN(tcl));
5707 scbid = ahc_inb(ahc, SCB_64_BTT + TCL_TARGET_OFFSET(tcl));
5708 ahc_outb(ahc, SCBPTR, saved_scbptr);
5710 target_offset = TCL_TARGET_OFFSET(tcl);
5711 scbid = ahc_inb(ahc, BUSY_TARGETS + target_offset);
5718 ahc_unbusy_tcl(struct ahc_softc *ahc, u_int tcl)
5720 u_int target_offset;
5722 if ((ahc->flags & AHC_SCB_BTT) != 0) {
5725 saved_scbptr = ahc_inb(ahc, SCBPTR);
5726 ahc_outb(ahc, SCBPTR, TCL_LUN(tcl));
5727 ahc_outb(ahc, SCB_64_BTT+TCL_TARGET_OFFSET(tcl), SCB_LIST_NULL);
5728 ahc_outb(ahc, SCBPTR, saved_scbptr);
5730 target_offset = TCL_TARGET_OFFSET(tcl);
5731 ahc_outb(ahc, BUSY_TARGETS + target_offset, SCB_LIST_NULL);
5736 ahc_busy_tcl(struct ahc_softc *ahc, u_int tcl, u_int scbid)
5738 u_int target_offset;
5740 if ((ahc->flags & AHC_SCB_BTT) != 0) {
5743 saved_scbptr = ahc_inb(ahc, SCBPTR);
5744 ahc_outb(ahc, SCBPTR, TCL_LUN(tcl));
5745 ahc_outb(ahc, SCB_64_BTT + TCL_TARGET_OFFSET(tcl), scbid);
5746 ahc_outb(ahc, SCBPTR, saved_scbptr);
5748 target_offset = TCL_TARGET_OFFSET(tcl);
5749 ahc_outb(ahc, BUSY_TARGETS + target_offset, scbid);
5753 /************************** SCB and SCB queue management **********************/
5755 ahc_match_scb(struct ahc_softc *ahc, struct scb *scb, int target,
5756 char channel, int lun, u_int tag, role_t role)
5758 int targ = SCB_GET_TARGET(ahc, scb);
5759 char chan = SCB_GET_CHANNEL(ahc, scb);
5760 int slun = SCB_GET_LUN(scb);
5763 match = ((chan == channel) || (channel == ALL_CHANNELS));
5765 match = ((targ == target) || (target == CAM_TARGET_WILDCARD));
5767 match = ((lun == slun) || (lun == CAM_LUN_WILDCARD));
5769 #ifdef AHC_TARGET_MODE
5772 group = XPT_FC_GROUP(scb->io_ctx->ccb_h.func_code);
5773 if (role == ROLE_INITIATOR) {
5774 match = (group != XPT_FC_GROUP_TMODE)
5775 && ((tag == scb->hscb->tag)
5776 || (tag == SCB_LIST_NULL));
5777 } else if (role == ROLE_TARGET) {
5778 match = (group == XPT_FC_GROUP_TMODE)
5779 && ((tag == scb->io_ctx->csio.tag_id)
5780 || (tag == SCB_LIST_NULL));
5782 #else /* !AHC_TARGET_MODE */
5783 match = ((tag == scb->hscb->tag) || (tag == SCB_LIST_NULL));
5784 #endif /* AHC_TARGET_MODE */
5791 ahc_freeze_devq(struct ahc_softc *ahc, struct scb *scb)
5797 target = SCB_GET_TARGET(ahc, scb);
5798 lun = SCB_GET_LUN(scb);
5799 channel = SCB_GET_CHANNEL(ahc, scb);
5801 ahc_search_qinfifo(ahc, target, channel, lun,
5802 /*tag*/SCB_LIST_NULL, ROLE_UNKNOWN,
5803 CAM_REQUEUE_REQ, SEARCH_COMPLETE);
5805 ahc_platform_freeze_devq(ahc, scb);
5809 ahc_qinfifo_requeue_tail(struct ahc_softc *ahc, struct scb *scb)
5811 struct scb *prev_scb;
5814 if (ahc_qinfifo_count(ahc) != 0) {
5818 prev_pos = ahc->qinfifonext - 1;
5819 prev_tag = ahc->qinfifo[prev_pos];
5820 prev_scb = ahc_lookup_scb(ahc, prev_tag);
5822 ahc_qinfifo_requeue(ahc, prev_scb, scb);
5823 if ((ahc->features & AHC_QUEUE_REGS) != 0) {
5824 ahc_outb(ahc, HNSCB_QOFF, ahc->qinfifonext);
5826 ahc_outb(ahc, KERNEL_QINPOS, ahc->qinfifonext);
5831 ahc_qinfifo_requeue(struct ahc_softc *ahc, struct scb *prev_scb,
5834 if (prev_scb == NULL) {
5835 ahc_outb(ahc, NEXT_QUEUED_SCB, scb->hscb->tag);
5837 prev_scb->hscb->next = scb->hscb->tag;
5838 ahc_sync_scb(ahc, prev_scb,
5839 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
5841 ahc->qinfifo[ahc->qinfifonext++] = scb->hscb->tag;
5842 scb->hscb->next = ahc->next_queued_scb->hscb->tag;
5843 ahc_sync_scb(ahc, scb, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
5847 ahc_qinfifo_count(struct ahc_softc *ahc)
5852 if ((ahc->features & AHC_QUEUE_REGS) != 0) {
5853 qinpos = ahc_inb(ahc, SNSCB_QOFF);
5854 ahc_outb(ahc, SNSCB_QOFF, qinpos);
5856 qinpos = ahc_inb(ahc, QINPOS);
5857 diff = ahc->qinfifonext - qinpos;
5862 ahc_search_qinfifo(struct ahc_softc *ahc, int target, char channel,
5863 int lun, u_int tag, role_t role, uint32_t status,
5864 ahc_search_action action)
5867 struct scb *prev_scb;
5877 qintail = ahc->qinfifonext;
5878 have_qregs = (ahc->features & AHC_QUEUE_REGS) != 0;
5880 qinstart = ahc_inb(ahc, SNSCB_QOFF);
5881 ahc_outb(ahc, SNSCB_QOFF, qinstart);
5883 qinstart = ahc_inb(ahc, QINPOS);
5888 if (action == SEARCH_COMPLETE) {
5890 * Don't attempt to run any queued untagged transactions
5891 * until we are done with the abort process.
5893 ahc_freeze_untagged_queues(ahc);
5897 * Start with an empty queue. Entries that are not chosen
5898 * for removal will be re-added to the queue as we go.
5900 ahc->qinfifonext = qinpos;
5901 ahc_outb(ahc, NEXT_QUEUED_SCB, ahc->next_queued_scb->hscb->tag);
5903 while (qinpos != qintail) {
5904 scb = ahc_lookup_scb(ahc, ahc->qinfifo[qinpos]);
5906 printf("qinpos = %d, SCB index = %d\n",
5907 qinpos, ahc->qinfifo[qinpos]);
5911 if (ahc_match_scb(ahc, scb, target, channel, lun, tag, role)) {
5913 * We found an scb that needs to be acted on.
5917 case SEARCH_COMPLETE:
5922 ostat = ahc_get_transaction_status(scb);
5923 if (ostat == CAM_REQ_INPROG)
5924 ahc_set_transaction_status(scb, status);
5925 cstat = ahc_get_transaction_status(scb);
5926 if (cstat != CAM_REQ_CMP)
5927 ahc_freeze_scb(scb);
5928 if ((scb->flags & SCB_ACTIVE) == 0)
5929 printf("Inactive SCB in qinfifo\n");
5937 ahc_qinfifo_requeue(ahc, prev_scb, scb);
5942 ahc_qinfifo_requeue(ahc, prev_scb, scb);
5948 if ((ahc->features & AHC_QUEUE_REGS) != 0) {
5949 ahc_outb(ahc, HNSCB_QOFF, ahc->qinfifonext);
5951 ahc_outb(ahc, KERNEL_QINPOS, ahc->qinfifonext);
5954 if (action != SEARCH_COUNT
5956 && (qinstart != ahc->qinfifonext)) {
5958 * The sequencer may be in the process of dmaing
5959 * down the SCB at the beginning of the queue.
5960 * This could be problematic if either the first,
5961 * or the second SCB is removed from the queue
5962 * (the first SCB includes a pointer to the "next"
5963 * SCB to dma). If we have removed any entries, swap
5964 * the first element in the queue with the next HSCB
5965 * so the sequencer will notice that NEXT_QUEUED_SCB
5966 * has changed during its dma attempt and will retry
5969 scb = ahc_lookup_scb(ahc, ahc->qinfifo[qinstart]);
5972 printf("found = %d, qinstart = %d, qinfifionext = %d\n",
5973 found, qinstart, ahc->qinfifonext);
5974 panic("First/Second Qinfifo fixup\n");
5977 * ahc_swap_with_next_hscb forces our next pointer to
5978 * point to the reserved SCB for future commands. Save
5979 * and restore our original next pointer to maintain
5982 next = scb->hscb->next;
5983 ahc->scb_data->scbindex[scb->hscb->tag] = NULL;
5984 ahc_swap_with_next_hscb(ahc, scb);
5985 scb->hscb->next = next;
5986 ahc->qinfifo[qinstart] = scb->hscb->tag;
5988 /* Tell the card about the new head of the qinfifo. */
5989 ahc_outb(ahc, NEXT_QUEUED_SCB, scb->hscb->tag);
5991 /* Fixup the tail "next" pointer. */
5992 qintail = ahc->qinfifonext - 1;
5993 scb = ahc_lookup_scb(ahc, ahc->qinfifo[qintail]);
5994 scb->hscb->next = ahc->next_queued_scb->hscb->tag;
5998 * Search waiting for selection list.
6000 curscbptr = ahc_inb(ahc, SCBPTR);
6001 next = ahc_inb(ahc, WAITING_SCBH); /* Start at head of list. */
6002 prev = SCB_LIST_NULL;
6004 while (next != SCB_LIST_NULL) {
6007 ahc_outb(ahc, SCBPTR, next);
6008 scb_index = ahc_inb(ahc, SCB_TAG);
6009 if (scb_index >= ahc->scb_data->numscbs) {
6010 printf("Waiting List inconsistency. "
6011 "SCB index == %d, yet numscbs == %d.",
6012 scb_index, ahc->scb_data->numscbs);
6013 ahc_dump_card_state(ahc);
6014 panic("for safety");
6016 scb = ahc_lookup_scb(ahc, scb_index);
6018 printf("scb_index = %d, next = %d\n",
6020 panic("Waiting List traversal\n");
6022 if (ahc_match_scb(ahc, scb, target, channel,
6023 lun, SCB_LIST_NULL, role)) {
6025 * We found an scb that needs to be acted on.
6029 case SEARCH_COMPLETE:
6034 ostat = ahc_get_transaction_status(scb);
6035 if (ostat == CAM_REQ_INPROG)
6036 ahc_set_transaction_status(scb,
6038 cstat = ahc_get_transaction_status(scb);
6039 if (cstat != CAM_REQ_CMP)
6040 ahc_freeze_scb(scb);
6041 if ((scb->flags & SCB_ACTIVE) == 0)
6042 printf("Inactive SCB in Waiting List\n");
6047 next = ahc_rem_wscb(ahc, next, prev);
6051 next = ahc_inb(ahc, SCB_NEXT);
6057 next = ahc_inb(ahc, SCB_NEXT);
6060 ahc_outb(ahc, SCBPTR, curscbptr);
6062 found += ahc_search_untagged_queues(ahc, /*ahc_io_ctx_t*/NULL, target,
6063 channel, lun, status, action);
6065 if (action == SEARCH_COMPLETE)
6066 ahc_release_untagged_queues(ahc);
6071 ahc_search_untagged_queues(struct ahc_softc *ahc, ahc_io_ctx_t ctx,
6072 int target, char channel, int lun, uint32_t status,
6073 ahc_search_action action)
6080 if (action == SEARCH_COMPLETE) {
6082 * Don't attempt to run any queued untagged transactions
6083 * until we are done with the abort process.
6085 ahc_freeze_untagged_queues(ahc);
6090 if ((ahc->flags & AHC_SCB_BTT) == 0) {
6093 if (target != CAM_TARGET_WILDCARD) {
6104 for (; i < maxtarget; i++) {
6105 struct scb_tailq *untagged_q;
6106 struct scb *next_scb;
6108 untagged_q = &(ahc->untagged_queues[i]);
6109 next_scb = TAILQ_FIRST(untagged_q);
6110 while (next_scb != NULL) {
6113 next_scb = TAILQ_NEXT(scb, links.tqe);
6116 * The head of the list may be the currently
6117 * active untagged command for a device.
6118 * We're only searching for commands that
6119 * have not been started. A transaction
6120 * marked active but still in the qinfifo
6121 * is removed by the qinfifo scanning code
6124 if ((scb->flags & SCB_ACTIVE) != 0)
6127 if (ahc_match_scb(ahc, scb, target, channel, lun,
6128 SCB_LIST_NULL, ROLE_INITIATOR) == 0
6129 || (ctx != NULL && ctx != scb->io_ctx))
6133 * We found an scb that needs to be acted on.
6137 case SEARCH_COMPLETE:
6142 ostat = ahc_get_transaction_status(scb);
6143 if (ostat == CAM_REQ_INPROG)
6144 ahc_set_transaction_status(scb, status);
6145 cstat = ahc_get_transaction_status(scb);
6146 if (cstat != CAM_REQ_CMP)
6147 ahc_freeze_scb(scb);
6148 if ((scb->flags & SCB_ACTIVE) == 0)
6149 printf("Inactive SCB in untaggedQ\n");
6154 scb->flags &= ~SCB_UNTAGGEDQ;
6155 TAILQ_REMOVE(untagged_q, scb, links.tqe);
6163 if (action == SEARCH_COMPLETE)
6164 ahc_release_untagged_queues(ahc);
6169 ahc_search_disc_list(struct ahc_softc *ahc, int target, char channel,
6170 int lun, u_int tag, int stop_on_first, int remove,
6180 next = ahc_inb(ahc, DISCONNECTED_SCBH);
6181 prev = SCB_LIST_NULL;
6184 /* restore this when we're done */
6185 active_scb = ahc_inb(ahc, SCBPTR);
6187 /* Silence compiler */
6188 active_scb = SCB_LIST_NULL;
6190 while (next != SCB_LIST_NULL) {
6193 ahc_outb(ahc, SCBPTR, next);
6194 scb_index = ahc_inb(ahc, SCB_TAG);
6195 if (scb_index >= ahc->scb_data->numscbs) {
6196 printf("Disconnected List inconsistency. "
6197 "SCB index == %d, yet numscbs == %d.",
6198 scb_index, ahc->scb_data->numscbs);
6199 ahc_dump_card_state(ahc);
6200 panic("for safety");
6204 panic("Disconnected List Loop. "
6205 "cur SCBPTR == %x, prev SCBPTR == %x.",
6208 scbp = ahc_lookup_scb(ahc, scb_index);
6209 if (ahc_match_scb(ahc, scbp, target, channel, lun,
6210 tag, ROLE_INITIATOR)) {
6214 ahc_rem_scb_from_disc_list(ahc, prev, next);
6217 next = ahc_inb(ahc, SCB_NEXT);
6223 next = ahc_inb(ahc, SCB_NEXT);
6227 ahc_outb(ahc, SCBPTR, active_scb);
6232 * Remove an SCB from the on chip list of disconnected transactions.
6233 * This is empty/unused if we are not performing SCB paging.
6236 ahc_rem_scb_from_disc_list(struct ahc_softc *ahc, u_int prev, u_int scbptr)
6240 ahc_outb(ahc, SCBPTR, scbptr);
6241 next = ahc_inb(ahc, SCB_NEXT);
6243 ahc_outb(ahc, SCB_CONTROL, 0);
6245 ahc_add_curscb_to_free_list(ahc);
6247 if (prev != SCB_LIST_NULL) {
6248 ahc_outb(ahc, SCBPTR, prev);
6249 ahc_outb(ahc, SCB_NEXT, next);
6251 ahc_outb(ahc, DISCONNECTED_SCBH, next);
6257 * Add the SCB as selected by SCBPTR onto the on chip list of
6258 * free hardware SCBs. This list is empty/unused if we are not
6259 * performing SCB paging.
6262 ahc_add_curscb_to_free_list(struct ahc_softc *ahc)
6265 * Invalidate the tag so that our abort
6266 * routines don't think it's active.
6268 ahc_outb(ahc, SCB_TAG, SCB_LIST_NULL);
6270 if ((ahc->flags & AHC_PAGESCBS) != 0) {
6271 ahc_outb(ahc, SCB_NEXT, ahc_inb(ahc, FREE_SCBH));
6272 ahc_outb(ahc, FREE_SCBH, ahc_inb(ahc, SCBPTR));
6277 * Manipulate the waiting for selection list and return the
6278 * scb that follows the one that we remove.
6281 ahc_rem_wscb(struct ahc_softc *ahc, u_int scbpos, u_int prev)
6286 * Select the SCB we want to abort and
6287 * pull the next pointer out of it.
6289 curscb = ahc_inb(ahc, SCBPTR);
6290 ahc_outb(ahc, SCBPTR, scbpos);
6291 next = ahc_inb(ahc, SCB_NEXT);
6293 /* Clear the necessary fields */
6294 ahc_outb(ahc, SCB_CONTROL, 0);
6296 ahc_add_curscb_to_free_list(ahc);
6298 /* update the waiting list */
6299 if (prev == SCB_LIST_NULL) {
6300 /* First in the list */
6301 ahc_outb(ahc, WAITING_SCBH, next);
6304 * Ensure we aren't attempting to perform
6305 * selection for this entry.
6307 ahc_outb(ahc, SCSISEQ, (ahc_inb(ahc, SCSISEQ) & ~ENSELO));
6310 * Select the scb that pointed to us
6311 * and update its next pointer.
6313 ahc_outb(ahc, SCBPTR, prev);
6314 ahc_outb(ahc, SCB_NEXT, next);
6318 * Point us back at the original scb position.
6320 ahc_outb(ahc, SCBPTR, curscb);
6324 /******************************** Error Handling ******************************/
6326 * Abort all SCBs that match the given description (target/channel/lun/tag),
6327 * setting their status to the passed in status if the status has not already
6328 * been modified from CAM_REQ_INPROG. This routine assumes that the sequencer
6329 * is paused before it is called.
6332 ahc_abort_scbs(struct ahc_softc *ahc, int target, char channel,
6333 int lun, u_int tag, role_t role, uint32_t status)
6336 struct scb *scbp_next;
6346 * Don't attempt to run any queued untagged transactions
6347 * until we are done with the abort process.
6349 ahc_freeze_untagged_queues(ahc);
6351 /* restore this when we're done */
6352 active_scb = ahc_inb(ahc, SCBPTR);
6354 found = ahc_search_qinfifo(ahc, target, channel, lun, SCB_LIST_NULL,
6355 role, CAM_REQUEUE_REQ, SEARCH_COMPLETE);
6358 * Clean out the busy target table for any untagged commands.
6362 if (target != CAM_TARGET_WILDCARD) {
6369 if (lun == CAM_LUN_WILDCARD) {
6372 * Unless we are using an SCB based
6373 * busy targets table, there is only
6374 * one table entry for all luns of
6379 if ((ahc->flags & AHC_SCB_BTT) != 0)
6380 maxlun = AHC_NUM_LUNS;
6386 if (role != ROLE_TARGET) {
6387 for (;i < maxtarget; i++) {
6388 for (j = minlun;j < maxlun; j++) {
6392 tcl = BUILD_TCL(i << 4, j);
6393 scbid = ahc_index_busy_tcl(ahc, tcl);
6394 scbp = ahc_lookup_scb(ahc, scbid);
6396 || ahc_match_scb(ahc, scbp, target, channel,
6397 lun, tag, role) == 0)
6399 ahc_unbusy_tcl(ahc, BUILD_TCL(i << 4, j));
6404 * Go through the disconnected list and remove any entries we
6405 * have queued for completion, 0'ing their control byte too.
6406 * We save the active SCB and restore it ourselves, so there
6407 * is no reason for this search to restore it too.
6409 ahc_search_disc_list(ahc, target, channel, lun, tag,
6410 /*stop_on_first*/FALSE, /*remove*/TRUE,
6411 /*save_state*/FALSE);
6415 * Go through the hardware SCB array looking for commands that
6416 * were active but not on any list. In some cases, these remnants
6417 * might not still have mappings in the scbindex array (e.g. unexpected
6418 * bus free with the same scb queued for an abort). Don't hold this
6421 for (i = 0; i < ahc->scb_data->maxhscbs; i++) {
6424 ahc_outb(ahc, SCBPTR, i);
6425 scbid = ahc_inb(ahc, SCB_TAG);
6426 scbp = ahc_lookup_scb(ahc, scbid);
6427 if ((scbp == NULL && scbid != SCB_LIST_NULL)
6429 && ahc_match_scb(ahc, scbp, target, channel, lun, tag, role)))
6430 ahc_add_curscb_to_free_list(ahc);
6434 * Go through the pending CCB list and look for
6435 * commands for this target that are still active.
6436 * These are other tagged commands that were
6437 * disconnected when the reset occurred.
6439 scbp_next = LIST_FIRST(&ahc->pending_scbs);
6440 while (scbp_next != NULL) {
6442 scbp_next = LIST_NEXT(scbp, pending_links);
6443 if (ahc_match_scb(ahc, scbp, target, channel, lun, tag, role)) {
6446 ostat = ahc_get_transaction_status(scbp);
6447 if (ostat == CAM_REQ_INPROG)
6448 ahc_set_transaction_status(scbp, status);
6449 if (ahc_get_transaction_status(scbp) != CAM_REQ_CMP)
6450 ahc_freeze_scb(scbp);
6451 if ((scbp->flags & SCB_ACTIVE) == 0)
6452 printf("Inactive SCB on pending list\n");
6453 ahc_done(ahc, scbp);
6457 ahc_outb(ahc, SCBPTR, active_scb);
6458 ahc_platform_abort_scbs(ahc, target, channel, lun, tag, role, status);
6459 ahc_release_untagged_queues(ahc);
6464 ahc_reset_current_bus(struct ahc_softc *ahc)
6468 ahc_outb(ahc, SIMODE1, ahc_inb(ahc, SIMODE1) & ~ENSCSIRST);
6469 scsiseq = ahc_inb(ahc, SCSISEQ);
6470 ahc_outb(ahc, SCSISEQ, scsiseq | SCSIRSTO);
6471 ahc_flush_device_writes(ahc);
6472 ahc_delay(AHC_BUSRESET_DELAY);
6473 /* Turn off the bus reset */
6474 ahc_outb(ahc, SCSISEQ, scsiseq & ~SCSIRSTO);
6476 ahc_clear_intstat(ahc);
6478 /* Re-enable reset interrupts */
6479 ahc_outb(ahc, SIMODE1, ahc_inb(ahc, SIMODE1) | ENSCSIRST);
6483 ahc_reset_channel(struct ahc_softc *ahc, char channel, int initiate_reset)
6485 struct ahc_devinfo devinfo;
6486 u_int initiator, target, max_scsiid;
6494 ahc->pending_device = NULL;
6496 ahc_compile_devinfo(&devinfo,
6497 CAM_TARGET_WILDCARD,
6498 CAM_TARGET_WILDCARD,
6500 channel, ROLE_UNKNOWN);
6503 /* Make sure the sequencer is in a safe location. */
6504 ahc_clear_critical_section(ahc);
6507 * Run our command complete fifos to ensure that we perform
6508 * completion processing on any commands that 'completed'
6509 * before the reset occurred.
6511 ahc_run_qoutfifo(ahc);
6512 #ifdef AHC_TARGET_MODE
6514 * XXX - In Twin mode, the tqinfifo may have commands
6515 * for an unaffected channel in it. However, if
6516 * we have run out of ATIO resources to drain that
6517 * queue, we may not get them all out here. Further,
6518 * the blocked transactions for the reset channel
6519 * should just be killed off, irrespecitve of whether
6520 * we are blocked on ATIO resources. Write a routine
6521 * to compact the tqinfifo appropriately.
6523 if ((ahc->flags & AHC_TARGETROLE) != 0) {
6524 ahc_run_tqinfifo(ahc, /*paused*/TRUE);
6529 * Reset the bus if we are initiating this reset
6531 sblkctl = ahc_inb(ahc, SBLKCTL);
6533 if ((ahc->features & AHC_TWIN) != 0
6534 && ((sblkctl & SELBUSB) != 0))
6536 scsiseq = ahc_inb(ahc, SCSISEQ_TEMPLATE);
6537 if (cur_channel != channel) {
6538 /* Case 1: Command for another bus is active
6539 * Stealthily reset the other bus without
6540 * upsetting the current bus.
6542 ahc_outb(ahc, SBLKCTL, sblkctl ^ SELBUSB);
6543 simode1 = ahc_inb(ahc, SIMODE1) & ~(ENBUSFREE|ENSCSIRST);
6544 #ifdef AHC_TARGET_MODE
6546 * Bus resets clear ENSELI, so we cannot
6547 * defer re-enabling bus reset interrupts
6548 * if we are in target mode.
6550 if ((ahc->flags & AHC_TARGETROLE) != 0)
6551 simode1 |= ENSCSIRST;
6553 ahc_outb(ahc, SIMODE1, simode1);
6555 ahc_reset_current_bus(ahc);
6556 ahc_clear_intstat(ahc);
6557 ahc_outb(ahc, SCSISEQ, scsiseq & (ENSELI|ENRSELI|ENAUTOATNP));
6558 ahc_outb(ahc, SBLKCTL, sblkctl);
6559 restart_needed = FALSE;
6561 /* Case 2: A command from this bus is active or we're idle */
6562 simode1 = ahc_inb(ahc, SIMODE1) & ~(ENBUSFREE|ENSCSIRST);
6563 #ifdef AHC_TARGET_MODE
6565 * Bus resets clear ENSELI, so we cannot
6566 * defer re-enabling bus reset interrupts
6567 * if we are in target mode.
6569 if ((ahc->flags & AHC_TARGETROLE) != 0)
6570 simode1 |= ENSCSIRST;
6572 ahc_outb(ahc, SIMODE1, simode1);
6574 ahc_reset_current_bus(ahc);
6575 ahc_clear_intstat(ahc);
6576 ahc_outb(ahc, SCSISEQ, scsiseq & (ENSELI|ENRSELI|ENAUTOATNP));
6577 restart_needed = TRUE;
6581 * Clean up all the state information for the
6582 * pending transactions on this bus.
6584 found = ahc_abort_scbs(ahc, CAM_TARGET_WILDCARD, channel,
6585 CAM_LUN_WILDCARD, SCB_LIST_NULL,
6586 ROLE_UNKNOWN, CAM_SCSI_BUS_RESET);
6588 max_scsiid = (ahc->features & AHC_WIDE) ? 15 : 7;
6590 #ifdef AHC_TARGET_MODE
6592 * Send an immediate notify ccb to all target more peripheral
6593 * drivers affected by this action.
6595 for (target = 0; target <= max_scsiid; target++) {
6596 struct ahc_tmode_tstate* tstate;
6599 tstate = ahc->enabled_targets[target];
6602 for (lun = 0; lun < AHC_NUM_LUNS; lun++) {
6603 struct ahc_tmode_lstate* lstate;
6605 lstate = tstate->enabled_luns[lun];
6609 ahc_queue_lstate_event(ahc, lstate, CAM_TARGET_WILDCARD,
6610 EVENT_TYPE_BUS_RESET, /*arg*/0);
6611 ahc_send_lstate_events(ahc, lstate);
6615 /* Notify the XPT that a bus reset occurred */
6616 ahc_send_async(ahc, devinfo.channel, CAM_TARGET_WILDCARD,
6617 CAM_LUN_WILDCARD, AC_BUS_RESET);
6620 * Revert to async/narrow transfers until we renegotiate.
6622 for (target = 0; target <= max_scsiid; target++) {
6624 if (ahc->enabled_targets[target] == NULL)
6626 for (initiator = 0; initiator <= max_scsiid; initiator++) {
6627 struct ahc_devinfo devinfo;
6629 ahc_compile_devinfo(&devinfo, target, initiator,
6631 channel, ROLE_UNKNOWN);
6632 ahc_set_width(ahc, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
6633 AHC_TRANS_CUR, /*paused*/TRUE);
6634 ahc_set_syncrate(ahc, &devinfo, /*syncrate*/NULL,
6635 /*period*/0, /*offset*/0,
6636 /*ppr_options*/0, AHC_TRANS_CUR,
6649 /***************************** Residual Processing ****************************/
6651 * Calculate the residual for a just completed SCB.
6654 ahc_calc_residual(struct ahc_softc *ahc, struct scb *scb)
6656 struct hardware_scb *hscb;
6657 struct status_pkt *spkt;
6659 uint32_t resid_sgptr;
6665 * SG_RESID_VALID clear in sgptr.
6666 * 2) Transferless command
6667 * 3) Never performed any transfers.
6668 * sgptr has SG_FULL_RESID set.
6669 * 4) No residual but target did not
6670 * save data pointers after the
6671 * last transfer, so sgptr was
6673 * 5) We have a partial residual.
6674 * Use residual_sgptr to determine
6679 sgptr = ahc_le32toh(hscb->sgptr);
6680 if ((sgptr & SG_RESID_VALID) == 0)
6683 sgptr &= ~SG_RESID_VALID;
6685 if ((sgptr & SG_LIST_NULL) != 0)
6689 spkt = &hscb->shared_data.status;
6690 resid_sgptr = ahc_le32toh(spkt->residual_sg_ptr);
6691 if ((sgptr & SG_FULL_RESID) != 0) {
6693 resid = ahc_get_transfer_length(scb);
6694 } else if ((resid_sgptr & SG_LIST_NULL) != 0) {
6697 } else if ((resid_sgptr & ~SG_PTR_MASK) != 0) {
6698 panic("Bogus resid sgptr value 0x%x\n", resid_sgptr);
6700 struct ahc_dma_seg *sg;
6703 * Remainder of the SG where the transfer
6706 resid = ahc_le32toh(spkt->residual_datacnt) & AHC_SG_LEN_MASK;
6707 sg = ahc_sg_bus_to_virt(scb, resid_sgptr & SG_PTR_MASK);
6709 /* The residual sg_ptr always points to the next sg */
6713 * Add up the contents of all residual
6714 * SG segments that are after the SG where
6715 * the transfer stopped.
6717 while ((ahc_le32toh(sg->len) & AHC_DMA_LAST_SEG) == 0) {
6719 resid += ahc_le32toh(sg->len) & AHC_SG_LEN_MASK;
6722 if ((scb->flags & SCB_SENSE) == 0)
6723 ahc_set_residual(scb, resid);
6725 ahc_set_sense_residual(scb, resid);
6728 if ((ahc_debug & AHC_SHOW_MISC) != 0) {
6729 ahc_print_path(ahc, scb);
6730 printf("Handled %sResidual of %d bytes\n",
6731 (scb->flags & SCB_SENSE) ? "Sense " : "", resid);
6736 /******************************* Target Mode **********************************/
6737 #ifdef AHC_TARGET_MODE
6739 * Add a target mode event to this lun's queue
6742 ahc_queue_lstate_event(struct ahc_softc *ahc, struct ahc_tmode_lstate *lstate,
6743 u_int initiator_id, u_int event_type, u_int event_arg)
6745 struct ahc_tmode_event *event;
6748 xpt_freeze_devq(lstate->path, /*count*/1);
6749 if (lstate->event_w_idx >= lstate->event_r_idx)
6750 pending = lstate->event_w_idx - lstate->event_r_idx;
6752 pending = AHC_TMODE_EVENT_BUFFER_SIZE + 1
6753 - (lstate->event_r_idx - lstate->event_w_idx);
6755 if (event_type == EVENT_TYPE_BUS_RESET
6756 || event_type == MSG_BUS_DEV_RESET) {
6758 * Any earlier events are irrelevant, so reset our buffer.
6759 * This has the effect of allowing us to deal with reset
6760 * floods (an external device holding down the reset line)
6761 * without losing the event that is really interesting.
6763 lstate->event_r_idx = 0;
6764 lstate->event_w_idx = 0;
6765 xpt_release_devq(lstate->path, pending, /*runqueue*/FALSE);
6768 if (pending == AHC_TMODE_EVENT_BUFFER_SIZE) {
6769 xpt_print_path(lstate->path);
6770 printf("immediate event %x:%x lost\n",
6771 lstate->event_buffer[lstate->event_r_idx].event_type,
6772 lstate->event_buffer[lstate->event_r_idx].event_arg);
6773 lstate->event_r_idx++;
6774 if (lstate->event_r_idx == AHC_TMODE_EVENT_BUFFER_SIZE)
6775 lstate->event_r_idx = 0;
6776 xpt_release_devq(lstate->path, /*count*/1, /*runqueue*/FALSE);
6779 event = &lstate->event_buffer[lstate->event_w_idx];
6780 event->initiator_id = initiator_id;
6781 event->event_type = event_type;
6782 event->event_arg = event_arg;
6783 lstate->event_w_idx++;
6784 if (lstate->event_w_idx == AHC_TMODE_EVENT_BUFFER_SIZE)
6785 lstate->event_w_idx = 0;
6789 * Send any target mode events queued up waiting
6790 * for immediate notify resources.
6793 ahc_send_lstate_events(struct ahc_softc *ahc, struct ahc_tmode_lstate *lstate)
6795 struct ccb_hdr *ccbh;
6796 struct ccb_immed_notify *inot;
6798 while (lstate->event_r_idx != lstate->event_w_idx
6799 && (ccbh = SLIST_FIRST(&lstate->immed_notifies)) != NULL) {
6800 struct ahc_tmode_event *event;
6802 event = &lstate->event_buffer[lstate->event_r_idx];
6803 SLIST_REMOVE_HEAD(&lstate->immed_notifies, sim_links.sle);
6804 inot = (struct ccb_immed_notify *)ccbh;
6805 switch (event->event_type) {
6806 case EVENT_TYPE_BUS_RESET:
6807 ccbh->status = CAM_SCSI_BUS_RESET|CAM_DEV_QFRZN;
6810 ccbh->status = CAM_MESSAGE_RECV|CAM_DEV_QFRZN;
6811 inot->message_args[0] = event->event_type;
6812 inot->message_args[1] = event->event_arg;
6815 inot->initiator_id = event->initiator_id;
6816 inot->sense_len = 0;
6817 xpt_done((union ccb *)inot);
6818 lstate->event_r_idx++;
6819 if (lstate->event_r_idx == AHC_TMODE_EVENT_BUFFER_SIZE)
6820 lstate->event_r_idx = 0;
6825 /******************** Sequencer Program Patching/Download *********************/
6829 ahc_dumpseq(struct ahc_softc* ahc)
6833 ahc_outb(ahc, SEQCTL, PERRORDIS|FAILDIS|FASTMODE|LOADRAM);
6834 ahc_outb(ahc, SEQADDR0, 0);
6835 ahc_outb(ahc, SEQADDR1, 0);
6836 for (i = 0; i < ahc->instruction_ram_size; i++) {
6837 uint8_t ins_bytes[4];
6839 ahc_insb(ahc, SEQRAM, ins_bytes, 4);
6840 printf("0x%08x\n", ins_bytes[0] << 24
6841 | ins_bytes[1] << 16
6849 ahc_loadseq(struct ahc_softc *ahc)
6851 struct cs cs_table[num_critical_sections];
6852 u_int begin_set[num_critical_sections];
6853 u_int end_set[num_critical_sections];
6854 const struct patch *cur_patch;
6859 u_int sg_prefetch_cnt;
6861 uint8_t download_consts[7];
6864 * Start out with 0 critical sections
6865 * that apply to this firmware load.
6869 memset(begin_set, 0, sizeof(begin_set));
6870 memset(end_set, 0, sizeof(end_set));
6872 /* Setup downloadable constant table */
6873 download_consts[QOUTFIFO_OFFSET] = 0;
6874 if (ahc->targetcmds != NULL)
6875 download_consts[QOUTFIFO_OFFSET] += 32;
6876 download_consts[QINFIFO_OFFSET] = download_consts[QOUTFIFO_OFFSET] + 1;
6877 download_consts[CACHESIZE_MASK] = ahc->pci_cachesize - 1;
6878 download_consts[INVERTED_CACHESIZE_MASK] = ~(ahc->pci_cachesize - 1);
6879 sg_prefetch_cnt = ahc->pci_cachesize;
6880 if (sg_prefetch_cnt < (2 * sizeof(struct ahc_dma_seg)))
6881 sg_prefetch_cnt = 2 * sizeof(struct ahc_dma_seg);
6882 download_consts[SG_PREFETCH_CNT] = sg_prefetch_cnt;
6883 download_consts[SG_PREFETCH_ALIGN_MASK] = ~(sg_prefetch_cnt - 1);
6884 download_consts[SG_PREFETCH_ADDR_MASK] = (sg_prefetch_cnt - 1);
6886 cur_patch = patches;
6889 ahc_outb(ahc, SEQCTL, PERRORDIS|FAILDIS|FASTMODE|LOADRAM);
6890 ahc_outb(ahc, SEQADDR0, 0);
6891 ahc_outb(ahc, SEQADDR1, 0);
6893 for (i = 0; i < sizeof(seqprog)/4; i++) {
6894 if (ahc_check_patch(ahc, &cur_patch, i, &skip_addr) == 0) {
6896 * Don't download this instruction as it
6897 * is in a patch that was removed.
6902 if (downloaded == ahc->instruction_ram_size) {
6904 * We're about to exceed the instruction
6905 * storage capacity for this chip. Fail
6908 printf("\n%s: Program too large for instruction memory "
6909 "size of %d!\n", ahc_name(ahc),
6910 ahc->instruction_ram_size);
6915 * Move through the CS table until we find a CS
6916 * that might apply to this instruction.
6918 for (; cur_cs < num_critical_sections; cur_cs++) {
6919 if (critical_sections[cur_cs].end <= i) {
6920 if (begin_set[cs_count] == TRUE
6921 && end_set[cs_count] == FALSE) {
6922 cs_table[cs_count].end = downloaded;
6923 end_set[cs_count] = TRUE;
6928 if (critical_sections[cur_cs].begin <= i
6929 && begin_set[cs_count] == FALSE) {
6930 cs_table[cs_count].begin = downloaded;
6931 begin_set[cs_count] = TRUE;
6935 ahc_download_instr(ahc, i, download_consts);
6939 ahc->num_critical_sections = cs_count;
6940 if (cs_count != 0) {
6942 cs_count *= sizeof(struct cs);
6943 ahc->critical_sections = malloc(cs_count, M_DEVBUF, M_NOWAIT);
6944 if (ahc->critical_sections == NULL)
6945 panic("ahc_loadseq: Could not malloc");
6946 memcpy(ahc->critical_sections, cs_table, cs_count);
6948 ahc_outb(ahc, SEQCTL, PERRORDIS|FAILDIS|FASTMODE);
6951 printf(" %d instructions downloaded\n", downloaded);
6952 printf("%s: Features 0x%x, Bugs 0x%x, Flags 0x%x\n",
6953 ahc_name(ahc), ahc->features, ahc->bugs, ahc->flags);
6959 ahc_check_patch(struct ahc_softc *ahc, const struct patch **start_patch,
6960 u_int start_instr, u_int *skip_addr)
6962 const struct patch *cur_patch;
6963 const struct patch *last_patch;
6966 num_patches = ARRAY_SIZE(patches);
6967 last_patch = &patches[num_patches];
6968 cur_patch = *start_patch;
6970 while (cur_patch < last_patch && start_instr == cur_patch->begin) {
6972 if (cur_patch->patch_func(ahc) == 0) {
6974 /* Start rejecting code */
6975 *skip_addr = start_instr + cur_patch->skip_instr;
6976 cur_patch += cur_patch->skip_patch;
6978 /* Accepted this patch. Advance to the next
6979 * one and wait for our intruction pointer to
6986 *start_patch = cur_patch;
6987 if (start_instr < *skip_addr)
6988 /* Still skipping */
6995 ahc_download_instr(struct ahc_softc *ahc, u_int instrptr, uint8_t *dconsts)
6997 union ins_formats instr;
6998 struct ins_format1 *fmt1_ins;
6999 struct ins_format3 *fmt3_ins;
7003 * The firmware is always compiled into a little endian format.
7005 instr.integer = ahc_le32toh(*(uint32_t*)&seqprog[instrptr * 4]);
7007 fmt1_ins = &instr.format1;
7010 /* Pull the opcode */
7011 opcode = instr.format1.opcode;
7022 const struct patch *cur_patch;
7028 fmt3_ins = &instr.format3;
7030 address = fmt3_ins->address;
7031 cur_patch = patches;
7034 for (i = 0; i < address;) {
7036 ahc_check_patch(ahc, &cur_patch, i, &skip_addr);
7038 if (skip_addr > i) {
7041 end_addr = min(address, skip_addr);
7042 address_offset += end_addr - i;
7048 address -= address_offset;
7049 fmt3_ins->address = address;
7058 if (fmt1_ins->parity != 0) {
7059 fmt1_ins->immediate = dconsts[fmt1_ins->immediate];
7061 fmt1_ins->parity = 0;
7062 if ((ahc->features & AHC_CMD_CHAN) == 0
7063 && opcode == AIC_OP_BMOV) {
7065 * Block move was added at the same time
7066 * as the command channel. Verify that
7067 * this is only a move of a single element
7068 * and convert the BMOV to a MOV
7069 * (AND with an immediate of FF).
7071 if (fmt1_ins->immediate != 1)
7072 panic("%s: BMOV not supported\n",
7074 fmt1_ins->opcode = AIC_OP_AND;
7075 fmt1_ins->immediate = 0xff;
7079 if ((ahc->features & AHC_ULTRA2) != 0) {
7082 /* Calculate odd parity for the instruction */
7083 for (i = 0, count = 0; i < 31; i++) {
7087 if ((instr.integer & mask) != 0)
7090 if ((count & 0x01) == 0)
7091 instr.format1.parity = 1;
7093 /* Compress the instruction for older sequencers */
7094 if (fmt3_ins != NULL) {
7097 | (fmt3_ins->source << 8)
7098 | (fmt3_ins->address << 16)
7099 | (fmt3_ins->opcode << 25);
7103 | (fmt1_ins->source << 8)
7104 | (fmt1_ins->destination << 16)
7105 | (fmt1_ins->ret << 24)
7106 | (fmt1_ins->opcode << 25);
7109 /* The sequencer is a little endian cpu */
7110 instr.integer = ahc_htole32(instr.integer);
7111 ahc_outsb(ahc, SEQRAM, instr.bytes, 4);
7114 panic("Unknown opcode encountered in seq program");
7120 ahc_print_register(const ahc_reg_parse_entry_t *table, u_int num_entries,
7121 const char *name, u_int address, u_int value,
7122 u_int *cur_column, u_int wrap_point)
7127 if (cur_column != NULL && *cur_column >= wrap_point) {
7131 printed = printf("%s[0x%x]", name, value);
7132 if (table == NULL) {
7133 printed += printf(" ");
7134 *cur_column += printed;
7138 while (printed_mask != 0xFF) {
7141 for (entry = 0; entry < num_entries; entry++) {
7142 if (((value & table[entry].mask)
7143 != table[entry].value)
7144 || ((printed_mask & table[entry].mask)
7145 == table[entry].mask))
7148 printed += printf("%s%s",
7149 printed_mask == 0 ? ":(" : "|",
7151 printed_mask |= table[entry].mask;
7155 if (entry >= num_entries)
7158 if (printed_mask != 0)
7159 printed += printf(") ");
7161 printed += printf(" ");
7162 if (cur_column != NULL)
7163 *cur_column += printed;
7168 ahc_dump_card_state(struct ahc_softc *ahc)
7171 struct scb_tailq *untagged_q;
7182 uint8_t saved_scbptr;
7184 if (ahc_is_paused(ahc)) {
7191 saved_scbptr = ahc_inb(ahc, SCBPTR);
7192 last_phase = ahc_inb(ahc, LASTPHASE);
7193 printf(">>>>>>>>>>>>>>>>>> Dump Card State Begins <<<<<<<<<<<<<<<<<\n"
7194 "%s: Dumping Card State %s, at SEQADDR 0x%x\n",
7195 ahc_name(ahc), ahc_lookup_phase_entry(last_phase)->phasemsg,
7196 ahc_inb(ahc, SEQADDR0) | (ahc_inb(ahc, SEQADDR1) << 8));
7198 printf("Card was paused\n");
7199 printf("ACCUM = 0x%x, SINDEX = 0x%x, DINDEX = 0x%x, ARG_2 = 0x%x\n",
7200 ahc_inb(ahc, ACCUM), ahc_inb(ahc, SINDEX), ahc_inb(ahc, DINDEX),
7201 ahc_inb(ahc, ARG_2));
7202 printf("HCNT = 0x%x SCBPTR = 0x%x\n", ahc_inb(ahc, HCNT),
7203 ahc_inb(ahc, SCBPTR));
7205 if ((ahc->features & AHC_DT) != 0)
7206 ahc_scsiphase_print(ahc_inb(ahc, SCSIPHASE), &cur_col, 50);
7207 ahc_scsisigi_print(ahc_inb(ahc, SCSISIGI), &cur_col, 50);
7208 ahc_error_print(ahc_inb(ahc, ERROR), &cur_col, 50);
7209 ahc_scsibusl_print(ahc_inb(ahc, SCSIBUSL), &cur_col, 50);
7210 ahc_lastphase_print(ahc_inb(ahc, LASTPHASE), &cur_col, 50);
7211 ahc_scsiseq_print(ahc_inb(ahc, SCSISEQ), &cur_col, 50);
7212 ahc_sblkctl_print(ahc_inb(ahc, SBLKCTL), &cur_col, 50);
7213 ahc_scsirate_print(ahc_inb(ahc, SCSIRATE), &cur_col, 50);
7214 ahc_seqctl_print(ahc_inb(ahc, SEQCTL), &cur_col, 50);
7215 ahc_seq_flags_print(ahc_inb(ahc, SEQ_FLAGS), &cur_col, 50);
7216 ahc_sstat0_print(ahc_inb(ahc, SSTAT0), &cur_col, 50);
7217 ahc_sstat1_print(ahc_inb(ahc, SSTAT1), &cur_col, 50);
7218 ahc_sstat2_print(ahc_inb(ahc, SSTAT2), &cur_col, 50);
7219 ahc_sstat3_print(ahc_inb(ahc, SSTAT3), &cur_col, 50);
7220 ahc_simode0_print(ahc_inb(ahc, SIMODE0), &cur_col, 50);
7221 ahc_simode1_print(ahc_inb(ahc, SIMODE1), &cur_col, 50);
7222 ahc_sxfrctl0_print(ahc_inb(ahc, SXFRCTL0), &cur_col, 50);
7223 ahc_dfcntrl_print(ahc_inb(ahc, DFCNTRL), &cur_col, 50);
7224 ahc_dfstatus_print(ahc_inb(ahc, DFSTATUS), &cur_col, 50);
7228 for (i = 0; i < STACK_SIZE; i++)
7229 printf(" 0x%x", ahc_inb(ahc, STACK)|(ahc_inb(ahc, STACK) << 8));
7230 printf("\nSCB count = %d\n", ahc->scb_data->numscbs);
7231 printf("Kernel NEXTQSCB = %d\n", ahc->next_queued_scb->hscb->tag);
7232 printf("Card NEXTQSCB = %d\n", ahc_inb(ahc, NEXT_QUEUED_SCB));
7234 printf("QINFIFO entries: ");
7235 if ((ahc->features & AHC_QUEUE_REGS) != 0) {
7236 qinpos = ahc_inb(ahc, SNSCB_QOFF);
7237 ahc_outb(ahc, SNSCB_QOFF, qinpos);
7239 qinpos = ahc_inb(ahc, QINPOS);
7240 qintail = ahc->qinfifonext;
7241 while (qinpos != qintail) {
7242 printf("%d ", ahc->qinfifo[qinpos]);
7247 printf("Waiting Queue entries: ");
7248 scb_index = ahc_inb(ahc, WAITING_SCBH);
7250 while (scb_index != SCB_LIST_NULL && i++ < 256) {
7251 ahc_outb(ahc, SCBPTR, scb_index);
7252 printf("%d:%d ", scb_index, ahc_inb(ahc, SCB_TAG));
7253 scb_index = ahc_inb(ahc, SCB_NEXT);
7257 printf("Disconnected Queue entries: ");
7258 scb_index = ahc_inb(ahc, DISCONNECTED_SCBH);
7260 while (scb_index != SCB_LIST_NULL && i++ < 256) {
7261 ahc_outb(ahc, SCBPTR, scb_index);
7262 printf("%d:%d ", scb_index, ahc_inb(ahc, SCB_TAG));
7263 scb_index = ahc_inb(ahc, SCB_NEXT);
7267 ahc_sync_qoutfifo(ahc, BUS_DMASYNC_POSTREAD);
7268 printf("QOUTFIFO entries: ");
7269 qoutpos = ahc->qoutfifonext;
7271 while (ahc->qoutfifo[qoutpos] != SCB_LIST_NULL && i++ < 256) {
7272 printf("%d ", ahc->qoutfifo[qoutpos]);
7277 printf("Sequencer Free SCB List: ");
7278 scb_index = ahc_inb(ahc, FREE_SCBH);
7280 while (scb_index != SCB_LIST_NULL && i++ < 256) {
7281 ahc_outb(ahc, SCBPTR, scb_index);
7282 printf("%d ", scb_index);
7283 scb_index = ahc_inb(ahc, SCB_NEXT);
7287 printf("Sequencer SCB Info: ");
7288 for (i = 0; i < ahc->scb_data->maxhscbs; i++) {
7289 ahc_outb(ahc, SCBPTR, i);
7290 cur_col = printf("\n%3d ", i);
7292 ahc_scb_control_print(ahc_inb(ahc, SCB_CONTROL), &cur_col, 60);
7293 ahc_scb_scsiid_print(ahc_inb(ahc, SCB_SCSIID), &cur_col, 60);
7294 ahc_scb_lun_print(ahc_inb(ahc, SCB_LUN), &cur_col, 60);
7295 ahc_scb_tag_print(ahc_inb(ahc, SCB_TAG), &cur_col, 60);
7299 printf("Pending list: ");
7301 LIST_FOREACH(scb, &ahc->pending_scbs, pending_links) {
7304 cur_col = printf("\n%3d ", scb->hscb->tag);
7305 ahc_scb_control_print(scb->hscb->control, &cur_col, 60);
7306 ahc_scb_scsiid_print(scb->hscb->scsiid, &cur_col, 60);
7307 ahc_scb_lun_print(scb->hscb->lun, &cur_col, 60);
7308 if ((ahc->flags & AHC_PAGESCBS) == 0) {
7309 ahc_outb(ahc, SCBPTR, scb->hscb->tag);
7311 ahc_scb_control_print(ahc_inb(ahc, SCB_CONTROL),
7313 ahc_scb_tag_print(ahc_inb(ahc, SCB_TAG), &cur_col, 60);
7319 printf("Kernel Free SCB list: ");
7321 SLIST_FOREACH(scb, &ahc->scb_data->free_scbs, links.sle) {
7324 printf("%d ", scb->hscb->tag);
7328 maxtarget = (ahc->features & (AHC_WIDE|AHC_TWIN)) ? 15 : 7;
7329 for (target = 0; target <= maxtarget; target++) {
7330 untagged_q = &ahc->untagged_queues[target];
7331 if (TAILQ_FIRST(untagged_q) == NULL)
7333 printf("Untagged Q(%d): ", target);
7335 TAILQ_FOREACH(scb, untagged_q, links.tqe) {
7338 printf("%d ", scb->hscb->tag);
7343 ahc_platform_dump_card_state(ahc);
7344 printf("\n<<<<<<<<<<<<<<<<< Dump Card State Ends >>>>>>>>>>>>>>>>>>\n");
7345 ahc_outb(ahc, SCBPTR, saved_scbptr);
7350 /************************* Target Mode ****************************************/
7351 #ifdef AHC_TARGET_MODE
7353 ahc_find_tmode_devs(struct ahc_softc *ahc, struct cam_sim *sim, union ccb *ccb,
7354 struct ahc_tmode_tstate **tstate,
7355 struct ahc_tmode_lstate **lstate,
7356 int notfound_failure)
7359 if ((ahc->features & AHC_TARGETMODE) == 0)
7360 return (CAM_REQ_INVALID);
7363 * Handle the 'black hole' device that sucks up
7364 * requests to unattached luns on enabled targets.
7366 if (ccb->ccb_h.target_id == CAM_TARGET_WILDCARD
7367 && ccb->ccb_h.target_lun == CAM_LUN_WILDCARD) {
7369 *lstate = ahc->black_hole;
7373 max_id = (ahc->features & AHC_WIDE) ? 16 : 8;
7374 if (ccb->ccb_h.target_id >= max_id)
7375 return (CAM_TID_INVALID);
7377 if (ccb->ccb_h.target_lun >= AHC_NUM_LUNS)
7378 return (CAM_LUN_INVALID);
7380 *tstate = ahc->enabled_targets[ccb->ccb_h.target_id];
7382 if (*tstate != NULL)
7384 (*tstate)->enabled_luns[ccb->ccb_h.target_lun];
7387 if (notfound_failure != 0 && *lstate == NULL)
7388 return (CAM_PATH_INVALID);
7390 return (CAM_REQ_CMP);
7394 ahc_handle_en_lun(struct ahc_softc *ahc, struct cam_sim *sim, union ccb *ccb)
7396 struct ahc_tmode_tstate *tstate;
7397 struct ahc_tmode_lstate *lstate;
7398 struct ccb_en_lun *cel;
7408 status = ahc_find_tmode_devs(ahc, sim, ccb, &tstate, &lstate,
7409 /*notfound_failure*/FALSE);
7411 if (status != CAM_REQ_CMP) {
7412 ccb->ccb_h.status = status;
7416 if (cam_sim_bus(sim) == 0)
7417 our_id = ahc->our_id;
7419 our_id = ahc->our_id_b;
7421 if (ccb->ccb_h.target_id != our_id) {
7423 * our_id represents our initiator ID, or
7424 * the ID of the first target to have an
7425 * enabled lun in target mode. There are
7426 * two cases that may preclude enabling a
7427 * target id other than our_id.
7429 * o our_id is for an active initiator role.
7430 * Since the hardware does not support
7431 * reselections to the initiator role at
7432 * anything other than our_id, and our_id
7433 * is used by the hardware to indicate the
7434 * ID to use for both select-out and
7435 * reselect-out operations, the only target
7436 * ID we can support in this mode is our_id.
7438 * o The MULTARGID feature is not available and
7439 * a previous target mode ID has been enabled.
7441 if ((ahc->features & AHC_MULTIROLE) != 0) {
7443 if ((ahc->features & AHC_MULTI_TID) != 0
7444 && (ahc->flags & AHC_INITIATORROLE) != 0) {
7446 * Only allow additional targets if
7447 * the initiator role is disabled.
7448 * The hardware cannot handle a re-select-in
7449 * on the initiator id during a re-select-out
7450 * on a different target id.
7452 status = CAM_TID_INVALID;
7453 } else if ((ahc->flags & AHC_INITIATORROLE) != 0
7454 || ahc->enabled_luns > 0) {
7456 * Only allow our target id to change
7457 * if the initiator role is not configured
7458 * and there are no enabled luns which
7459 * are attached to the currently registered
7462 status = CAM_TID_INVALID;
7464 } else if ((ahc->features & AHC_MULTI_TID) == 0
7465 && ahc->enabled_luns > 0) {
7467 status = CAM_TID_INVALID;
7471 if (status != CAM_REQ_CMP) {
7472 ccb->ccb_h.status = status;
7477 * We now have an id that is valid.
7478 * If we aren't in target mode, switch modes.
7480 if ((ahc->flags & AHC_TARGETROLE) == 0
7481 && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
7483 ahc_flag saved_flags;
7485 printf("Configuring Target Mode\n");
7487 if (LIST_FIRST(&ahc->pending_scbs) != NULL) {
7488 ccb->ccb_h.status = CAM_BUSY;
7489 ahc_unlock(ahc, &s);
7492 saved_flags = ahc->flags;
7493 ahc->flags |= AHC_TARGETROLE;
7494 if ((ahc->features & AHC_MULTIROLE) == 0)
7495 ahc->flags &= ~AHC_INITIATORROLE;
7497 error = ahc_loadseq(ahc);
7500 * Restore original configuration and notify
7501 * the caller that we cannot support target mode.
7502 * Since the adapter started out in this
7503 * configuration, the firmware load will succeed,
7504 * so there is no point in checking ahc_loadseq's
7507 ahc->flags = saved_flags;
7508 (void)ahc_loadseq(ahc);
7510 ahc_unlock(ahc, &s);
7511 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
7515 ahc_unlock(ahc, &s);
7518 target = ccb->ccb_h.target_id;
7519 lun = ccb->ccb_h.target_lun;
7520 channel = SIM_CHANNEL(ahc, sim);
7521 target_mask = 0x01 << target;
7525 if (cel->enable != 0) {
7528 /* Are we already enabled?? */
7529 if (lstate != NULL) {
7530 xpt_print_path(ccb->ccb_h.path);
7531 printf("Lun already enabled\n");
7532 ccb->ccb_h.status = CAM_LUN_ALRDY_ENA;
7536 if (cel->grp6_len != 0
7537 || cel->grp7_len != 0) {
7539 * Don't (yet?) support vendor
7540 * specific commands.
7542 ccb->ccb_h.status = CAM_REQ_INVALID;
7543 printf("Non-zero Group Codes\n");
7549 * Setup our data structures.
7551 if (target != CAM_TARGET_WILDCARD && tstate == NULL) {
7552 tstate = ahc_alloc_tstate(ahc, target, channel);
7553 if (tstate == NULL) {
7554 xpt_print_path(ccb->ccb_h.path);
7555 printf("Couldn't allocate tstate\n");
7556 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
7560 lstate = malloc(sizeof(*lstate), M_DEVBUF, M_NOWAIT);
7561 if (lstate == NULL) {
7562 xpt_print_path(ccb->ccb_h.path);
7563 printf("Couldn't allocate lstate\n");
7564 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
7567 memset(lstate, 0, sizeof(*lstate));
7568 status = xpt_create_path(&lstate->path, /*periph*/NULL,
7569 xpt_path_path_id(ccb->ccb_h.path),
7570 xpt_path_target_id(ccb->ccb_h.path),
7571 xpt_path_lun_id(ccb->ccb_h.path));
7572 if (status != CAM_REQ_CMP) {
7573 free(lstate, M_DEVBUF);
7574 xpt_print_path(ccb->ccb_h.path);
7575 printf("Couldn't allocate path\n");
7576 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
7579 SLIST_INIT(&lstate->accept_tios);
7580 SLIST_INIT(&lstate->immed_notifies);
7583 if (target != CAM_TARGET_WILDCARD) {
7584 tstate->enabled_luns[lun] = lstate;
7585 ahc->enabled_luns++;
7587 if ((ahc->features & AHC_MULTI_TID) != 0) {
7590 targid_mask = ahc_inb(ahc, TARGID)
7591 | (ahc_inb(ahc, TARGID + 1) << 8);
7593 targid_mask |= target_mask;
7594 ahc_outb(ahc, TARGID, targid_mask);
7595 ahc_outb(ahc, TARGID+1, (targid_mask >> 8));
7597 ahc_update_scsiid(ahc, targid_mask);
7602 channel = SIM_CHANNEL(ahc, sim);
7603 our_id = SIM_SCSI_ID(ahc, sim);
7606 * This can only happen if selections
7609 if (target != our_id) {
7614 sblkctl = ahc_inb(ahc, SBLKCTL);
7615 cur_channel = (sblkctl & SELBUSB)
7617 if ((ahc->features & AHC_TWIN) == 0)
7619 swap = cur_channel != channel;
7621 ahc->our_id = target;
7623 ahc->our_id_b = target;
7626 ahc_outb(ahc, SBLKCTL,
7629 ahc_outb(ahc, SCSIID, target);
7632 ahc_outb(ahc, SBLKCTL, sblkctl);
7636 ahc->black_hole = lstate;
7637 /* Allow select-in operations */
7638 if (ahc->black_hole != NULL && ahc->enabled_luns > 0) {
7639 scsiseq = ahc_inb(ahc, SCSISEQ_TEMPLATE);
7641 ahc_outb(ahc, SCSISEQ_TEMPLATE, scsiseq);
7642 scsiseq = ahc_inb(ahc, SCSISEQ);
7644 ahc_outb(ahc, SCSISEQ, scsiseq);
7647 ahc_unlock(ahc, &s);
7648 ccb->ccb_h.status = CAM_REQ_CMP;
7649 xpt_print_path(ccb->ccb_h.path);
7650 printf("Lun now enabled for target mode\n");
7655 if (lstate == NULL) {
7656 ccb->ccb_h.status = CAM_LUN_INVALID;
7662 ccb->ccb_h.status = CAM_REQ_CMP;
7663 LIST_FOREACH(scb, &ahc->pending_scbs, pending_links) {
7664 struct ccb_hdr *ccbh;
7666 ccbh = &scb->io_ctx->ccb_h;
7667 if (ccbh->func_code == XPT_CONT_TARGET_IO
7668 && !xpt_path_comp(ccbh->path, ccb->ccb_h.path)){
7669 printf("CTIO pending\n");
7670 ccb->ccb_h.status = CAM_REQ_INVALID;
7671 ahc_unlock(ahc, &s);
7676 if (SLIST_FIRST(&lstate->accept_tios) != NULL) {
7677 printf("ATIOs pending\n");
7678 ccb->ccb_h.status = CAM_REQ_INVALID;
7681 if (SLIST_FIRST(&lstate->immed_notifies) != NULL) {
7682 printf("INOTs pending\n");
7683 ccb->ccb_h.status = CAM_REQ_INVALID;
7686 if (ccb->ccb_h.status != CAM_REQ_CMP) {
7687 ahc_unlock(ahc, &s);
7691 xpt_print_path(ccb->ccb_h.path);
7692 printf("Target mode disabled\n");
7693 xpt_free_path(lstate->path);
7694 free(lstate, M_DEVBUF);
7697 /* Can we clean up the target too? */
7698 if (target != CAM_TARGET_WILDCARD) {
7699 tstate->enabled_luns[lun] = NULL;
7700 ahc->enabled_luns--;
7701 for (empty = 1, i = 0; i < 8; i++)
7702 if (tstate->enabled_luns[i] != NULL) {
7708 ahc_free_tstate(ahc, target, channel,
7710 if (ahc->features & AHC_MULTI_TID) {
7713 targid_mask = ahc_inb(ahc, TARGID)
7714 | (ahc_inb(ahc, TARGID + 1)
7717 targid_mask &= ~target_mask;
7718 ahc_outb(ahc, TARGID, targid_mask);
7719 ahc_outb(ahc, TARGID+1,
7720 (targid_mask >> 8));
7721 ahc_update_scsiid(ahc, targid_mask);
7726 ahc->black_hole = NULL;
7729 * We can't allow selections without
7730 * our black hole device.
7734 if (ahc->enabled_luns == 0) {
7735 /* Disallow select-in */
7738 scsiseq = ahc_inb(ahc, SCSISEQ_TEMPLATE);
7740 ahc_outb(ahc, SCSISEQ_TEMPLATE, scsiseq);
7741 scsiseq = ahc_inb(ahc, SCSISEQ);
7743 ahc_outb(ahc, SCSISEQ, scsiseq);
7745 if ((ahc->features & AHC_MULTIROLE) == 0) {
7746 printf("Configuring Initiator Mode\n");
7747 ahc->flags &= ~AHC_TARGETROLE;
7748 ahc->flags |= AHC_INITIATORROLE;
7750 * Returning to a configuration that
7751 * fit previously will always succeed.
7753 (void)ahc_loadseq(ahc);
7756 * Unpaused. The extra unpause
7757 * that follows is harmless.
7762 ahc_unlock(ahc, &s);
7767 ahc_update_scsiid(struct ahc_softc *ahc, u_int targid_mask)
7772 if ((ahc->features & AHC_MULTI_TID) == 0)
7773 panic("ahc_update_scsiid called on non-multitid unit\n");
7776 * Since we will rely on the TARGID mask
7777 * for selection enables, ensure that OID
7778 * in SCSIID is not set to some other ID
7779 * that we don't want to allow selections on.
7781 if ((ahc->features & AHC_ULTRA2) != 0)
7782 scsiid = ahc_inb(ahc, SCSIID_ULTRA2);
7784 scsiid = ahc_inb(ahc, SCSIID);
7785 scsiid_mask = 0x1 << (scsiid & OID);
7786 if ((targid_mask & scsiid_mask) == 0) {
7789 /* ffs counts from 1 */
7790 our_id = ffs(targid_mask);
7792 our_id = ahc->our_id;
7798 if ((ahc->features & AHC_ULTRA2) != 0)
7799 ahc_outb(ahc, SCSIID_ULTRA2, scsiid);
7801 ahc_outb(ahc, SCSIID, scsiid);
7805 ahc_run_tqinfifo(struct ahc_softc *ahc, int paused)
7807 struct target_cmd *cmd;
7810 * If the card supports auto-access pause,
7811 * we can access the card directly regardless
7812 * of whether it is paused or not.
7814 if ((ahc->features & AHC_AUTOPAUSE) != 0)
7817 ahc_sync_tqinfifo(ahc, BUS_DMASYNC_POSTREAD);
7818 while ((cmd = &ahc->targetcmds[ahc->tqinfifonext])->cmd_valid != 0) {
7821 * Only advance through the queue if we
7822 * have the resources to process the command.
7824 if (ahc_handle_target_cmd(ahc, cmd) != 0)
7828 ahc_dmamap_sync(ahc, ahc->shared_data_dmat,
7829 ahc->shared_data_dmamap,
7830 ahc_targetcmd_offset(ahc, ahc->tqinfifonext),
7831 sizeof(struct target_cmd),
7832 BUS_DMASYNC_PREREAD);
7833 ahc->tqinfifonext++;
7836 * Lazily update our position in the target mode incoming
7837 * command queue as seen by the sequencer.
7839 if ((ahc->tqinfifonext & (HOST_TQINPOS - 1)) == 1) {
7840 if ((ahc->features & AHC_HS_MAILBOX) != 0) {
7843 hs_mailbox = ahc_inb(ahc, HS_MAILBOX);
7844 hs_mailbox &= ~HOST_TQINPOS;
7845 hs_mailbox |= ahc->tqinfifonext & HOST_TQINPOS;
7846 ahc_outb(ahc, HS_MAILBOX, hs_mailbox);
7850 ahc_outb(ahc, KERNEL_TQINPOS,
7851 ahc->tqinfifonext & HOST_TQINPOS);
7860 ahc_handle_target_cmd(struct ahc_softc *ahc, struct target_cmd *cmd)
7862 struct ahc_tmode_tstate *tstate;
7863 struct ahc_tmode_lstate *lstate;
7864 struct ccb_accept_tio *atio;
7870 initiator = SCSIID_TARGET(ahc, cmd->scsiid);
7871 target = SCSIID_OUR_ID(cmd->scsiid);
7872 lun = (cmd->identify & MSG_IDENTIFY_LUNMASK);
7875 tstate = ahc->enabled_targets[target];
7878 lstate = tstate->enabled_luns[lun];
7881 * Commands for disabled luns go to the black hole driver.
7884 lstate = ahc->black_hole;
7886 atio = (struct ccb_accept_tio*)SLIST_FIRST(&lstate->accept_tios);
7888 ahc->flags |= AHC_TQINFIFO_BLOCKED;
7890 * Wait for more ATIOs from the peripheral driver for this lun.
7893 printf("%s: ATIOs exhausted\n", ahc_name(ahc));
7896 ahc->flags &= ~AHC_TQINFIFO_BLOCKED;
7898 printf("Incoming command from %d for %d:%d%s\n",
7899 initiator, target, lun,
7900 lstate == ahc->black_hole ? "(Black Holed)" : "");
7902 SLIST_REMOVE_HEAD(&lstate->accept_tios, sim_links.sle);
7904 if (lstate == ahc->black_hole) {
7905 /* Fill in the wildcards */
7906 atio->ccb_h.target_id = target;
7907 atio->ccb_h.target_lun = lun;
7911 * Package it up and send it off to
7912 * whomever has this lun enabled.
7914 atio->sense_len = 0;
7915 atio->init_id = initiator;
7916 if (byte[0] != 0xFF) {
7917 /* Tag was included */
7918 atio->tag_action = *byte++;
7919 atio->tag_id = *byte++;
7920 atio->ccb_h.flags = CAM_TAG_ACTION_VALID;
7922 atio->ccb_h.flags = 0;
7926 /* Okay. Now determine the cdb size based on the command code */
7927 switch (*byte >> CMD_GROUP_CODE_SHIFT) {
7943 /* Only copy the opcode. */
7945 printf("Reserved or VU command code type encountered\n");
7949 memcpy(atio->cdb_io.cdb_bytes, byte, atio->cdb_len);
7951 atio->ccb_h.status |= CAM_CDB_RECVD;
7953 if ((cmd->identify & MSG_IDENTIFY_DISCFLAG) == 0) {
7955 * We weren't allowed to disconnect.
7956 * We're hanging on the bus until a
7957 * continue target I/O comes in response
7958 * to this accept tio.
7961 printf("Received Immediate Command %d:%d:%d - %p\n",
7962 initiator, target, lun, ahc->pending_device);
7964 ahc->pending_device = lstate;
7965 ahc_freeze_ccb((union ccb *)atio);
7966 atio->ccb_h.flags |= CAM_DIS_DISCONNECT;
7968 xpt_done((union ccb*)atio);