2 * Core routines and tables shareable across OS platforms.
4 * Copyright (c) 1994-2002 Justin T. Gibbs.
5 * Copyright (c) 2000-2003 Adaptec Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions, and the following disclaimer,
13 * without modification.
14 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
15 * substantially similar to the "NO WARRANTY" disclaimer below
16 * ("Disclaimer") and any redistribution must be conditioned upon
17 * including a substantially similar Disclaimer requirement for further
18 * binary redistribution.
19 * 3. Neither the names of the above-listed copyright holders nor the names
20 * of any contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
23 * Alternatively, this software may be distributed under the terms of the
24 * GNU General Public License ("GPL") version 2 as published by the Free
25 * Software Foundation.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
37 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGES.
40 * $Id: //depot/aic7xxx/aic7xxx/aic79xx.c#250 $
44 #include "aic79xx_osm.h"
45 #include "aic79xx_inline.h"
46 #include "aicasm/aicasm_insformat.h"
48 #include <dev/aic7xxx/aic79xx_osm.h>
49 #include <dev/aic7xxx/aic79xx_inline.h>
50 #include <dev/aic7xxx/aicasm/aicasm_insformat.h>
54 /***************************** Lookup Tables **********************************/
55 static char *ahd_chip_names[] =
62 static const u_int num_chip_names = ARRAY_SIZE(ahd_chip_names);
65 * Hardware error codes.
67 struct ahd_hard_error_entry {
72 static struct ahd_hard_error_entry ahd_hard_errors[] = {
73 { DSCTMOUT, "Discard Timer has timed out" },
74 { ILLOPCODE, "Illegal Opcode in sequencer program" },
75 { SQPARERR, "Sequencer Parity Error" },
76 { DPARERR, "Data-path Parity Error" },
77 { MPARERR, "Scratch or SCB Memory Parity Error" },
78 { CIOPARERR, "CIOBUS Parity Error" },
80 static const u_int num_errors = ARRAY_SIZE(ahd_hard_errors);
82 static struct ahd_phase_table_entry ahd_phase_table[] =
84 { P_DATAOUT, MSG_NOOP, "in Data-out phase" },
85 { P_DATAIN, MSG_INITIATOR_DET_ERR, "in Data-in phase" },
86 { P_DATAOUT_DT, MSG_NOOP, "in DT Data-out phase" },
87 { P_DATAIN_DT, MSG_INITIATOR_DET_ERR, "in DT Data-in phase" },
88 { P_COMMAND, MSG_NOOP, "in Command phase" },
89 { P_MESGOUT, MSG_NOOP, "in Message-out phase" },
90 { P_STATUS, MSG_INITIATOR_DET_ERR, "in Status phase" },
91 { P_MESGIN, MSG_PARITY_ERROR, "in Message-in phase" },
92 { P_BUSFREE, MSG_NOOP, "while idle" },
93 { 0, MSG_NOOP, "in unknown phase" }
97 * In most cases we only wish to itterate over real phases, so
98 * exclude the last element from the count.
100 static const u_int num_phases = ARRAY_SIZE(ahd_phase_table) - 1;
102 /* Our Sequencer Program */
103 #include "aic79xx_seq.h"
105 /**************************** Function Declarations ***************************/
106 static void ahd_handle_transmission_error(struct ahd_softc *ahd);
107 static void ahd_handle_lqiphase_error(struct ahd_softc *ahd,
109 static int ahd_handle_pkt_busfree(struct ahd_softc *ahd,
111 static int ahd_handle_nonpkt_busfree(struct ahd_softc *ahd);
112 static void ahd_handle_proto_violation(struct ahd_softc *ahd);
113 static void ahd_force_renegotiation(struct ahd_softc *ahd,
114 struct ahd_devinfo *devinfo);
116 static struct ahd_tmode_tstate*
117 ahd_alloc_tstate(struct ahd_softc *ahd,
118 u_int scsi_id, char channel);
119 #ifdef AHD_TARGET_MODE
120 static void ahd_free_tstate(struct ahd_softc *ahd,
121 u_int scsi_id, char channel, int force);
123 static void ahd_devlimited_syncrate(struct ahd_softc *ahd,
124 struct ahd_initiator_tinfo *,
128 static void ahd_update_neg_table(struct ahd_softc *ahd,
129 struct ahd_devinfo *devinfo,
130 struct ahd_transinfo *tinfo);
131 static void ahd_update_pending_scbs(struct ahd_softc *ahd);
132 static void ahd_fetch_devinfo(struct ahd_softc *ahd,
133 struct ahd_devinfo *devinfo);
134 static void ahd_scb_devinfo(struct ahd_softc *ahd,
135 struct ahd_devinfo *devinfo,
137 static void ahd_setup_initiator_msgout(struct ahd_softc *ahd,
138 struct ahd_devinfo *devinfo,
140 static void ahd_build_transfer_msg(struct ahd_softc *ahd,
141 struct ahd_devinfo *devinfo);
142 static void ahd_construct_sdtr(struct ahd_softc *ahd,
143 struct ahd_devinfo *devinfo,
144 u_int period, u_int offset);
145 static void ahd_construct_wdtr(struct ahd_softc *ahd,
146 struct ahd_devinfo *devinfo,
148 static void ahd_construct_ppr(struct ahd_softc *ahd,
149 struct ahd_devinfo *devinfo,
150 u_int period, u_int offset,
151 u_int bus_width, u_int ppr_options);
152 static void ahd_clear_msg_state(struct ahd_softc *ahd);
153 static void ahd_handle_message_phase(struct ahd_softc *ahd);
159 static int ahd_sent_msg(struct ahd_softc *ahd, ahd_msgtype type,
160 u_int msgval, int full);
161 static int ahd_parse_msg(struct ahd_softc *ahd,
162 struct ahd_devinfo *devinfo);
163 static int ahd_handle_msg_reject(struct ahd_softc *ahd,
164 struct ahd_devinfo *devinfo);
165 static void ahd_handle_ign_wide_residue(struct ahd_softc *ahd,
166 struct ahd_devinfo *devinfo);
167 static void ahd_reinitialize_dataptrs(struct ahd_softc *ahd);
168 static void ahd_handle_devreset(struct ahd_softc *ahd,
169 struct ahd_devinfo *devinfo,
170 u_int lun, cam_status status,
171 char *message, int verbose_level);
172 #ifdef AHD_TARGET_MODE
173 static void ahd_setup_target_msgin(struct ahd_softc *ahd,
174 struct ahd_devinfo *devinfo,
178 static u_int ahd_sglist_size(struct ahd_softc *ahd);
179 static u_int ahd_sglist_allocsize(struct ahd_softc *ahd);
180 static bus_dmamap_callback_t
182 static void ahd_initialize_hscbs(struct ahd_softc *ahd);
183 static int ahd_init_scbdata(struct ahd_softc *ahd);
184 static void ahd_fini_scbdata(struct ahd_softc *ahd);
185 static void ahd_setup_iocell_workaround(struct ahd_softc *ahd);
186 static void ahd_iocell_first_selection(struct ahd_softc *ahd);
187 static void ahd_add_col_list(struct ahd_softc *ahd,
188 struct scb *scb, u_int col_idx);
189 static void ahd_rem_col_list(struct ahd_softc *ahd,
191 static void ahd_chip_init(struct ahd_softc *ahd);
192 static void ahd_qinfifo_requeue(struct ahd_softc *ahd,
193 struct scb *prev_scb,
195 static int ahd_qinfifo_count(struct ahd_softc *ahd);
196 static int ahd_search_scb_list(struct ahd_softc *ahd, int target,
197 char channel, int lun, u_int tag,
198 role_t role, uint32_t status,
199 ahd_search_action action,
200 u_int *list_head, u_int *list_tail,
202 static void ahd_stitch_tid_list(struct ahd_softc *ahd,
203 u_int tid_prev, u_int tid_cur,
205 static void ahd_add_scb_to_free_list(struct ahd_softc *ahd,
207 static u_int ahd_rem_wscb(struct ahd_softc *ahd, u_int scbid,
208 u_int prev, u_int next, u_int tid);
209 static void ahd_reset_current_bus(struct ahd_softc *ahd);
210 static ahd_callback_t ahd_stat_timer;
212 static void ahd_dumpseq(struct ahd_softc *ahd);
214 static void ahd_loadseq(struct ahd_softc *ahd);
215 static int ahd_check_patch(struct ahd_softc *ahd,
216 struct patch **start_patch,
217 u_int start_instr, u_int *skip_addr);
218 static u_int ahd_resolve_seqaddr(struct ahd_softc *ahd,
220 static void ahd_download_instr(struct ahd_softc *ahd,
221 u_int instrptr, uint8_t *dconsts);
222 static int ahd_probe_stack_size(struct ahd_softc *ahd);
223 static int ahd_scb_active_in_fifo(struct ahd_softc *ahd,
225 static void ahd_run_data_fifo(struct ahd_softc *ahd,
228 #ifdef AHD_TARGET_MODE
229 static void ahd_queue_lstate_event(struct ahd_softc *ahd,
230 struct ahd_tmode_lstate *lstate,
234 static void ahd_update_scsiid(struct ahd_softc *ahd,
236 static int ahd_handle_target_cmd(struct ahd_softc *ahd,
237 struct target_cmd *cmd);
240 static int ahd_abort_scbs(struct ahd_softc *ahd, int target,
241 char channel, int lun, u_int tag,
242 role_t role, uint32_t status);
243 static void ahd_alloc_scbs(struct ahd_softc *ahd);
244 static void ahd_busy_tcl(struct ahd_softc *ahd, u_int tcl,
246 static void ahd_calc_residual(struct ahd_softc *ahd,
248 static void ahd_clear_critical_section(struct ahd_softc *ahd);
249 static void ahd_clear_intstat(struct ahd_softc *ahd);
250 static void ahd_enable_coalescing(struct ahd_softc *ahd,
252 static u_int ahd_find_busy_tcl(struct ahd_softc *ahd, u_int tcl);
253 static void ahd_freeze_devq(struct ahd_softc *ahd,
255 static void ahd_handle_scb_status(struct ahd_softc *ahd,
257 static struct ahd_phase_table_entry* ahd_lookup_phase_entry(int phase);
258 static void ahd_shutdown(void *arg);
259 static void ahd_update_coalescing_values(struct ahd_softc *ahd,
263 static int ahd_verify_vpd_cksum(struct vpd_config *vpd);
264 static int ahd_wait_seeprom(struct ahd_softc *ahd);
265 static int ahd_match_scb(struct ahd_softc *ahd, struct scb *scb,
266 int target, char channel, int lun,
267 u_int tag, role_t role);
269 /************************ Sequencer Execution Control *************************/
271 ahd_set_modes(struct ahd_softc *ahd, ahd_mode src, ahd_mode dst)
273 if (ahd->src_mode == src && ahd->dst_mode == dst)
276 if (ahd->src_mode == AHD_MODE_UNKNOWN
277 || ahd->dst_mode == AHD_MODE_UNKNOWN)
278 panic("Setting mode prior to saving it.\n");
279 if ((ahd_debug & AHD_SHOW_MODEPTR) != 0)
280 printf("%s: Setting mode 0x%x\n", ahd_name(ahd),
281 ahd_build_mode_state(ahd, src, dst));
283 ahd_outb(ahd, MODE_PTR, ahd_build_mode_state(ahd, src, dst));
289 ahd_update_modes(struct ahd_softc *ahd)
291 ahd_mode_state mode_ptr;
295 mode_ptr = ahd_inb(ahd, MODE_PTR);
297 if ((ahd_debug & AHD_SHOW_MODEPTR) != 0)
298 printf("Reading mode 0x%x\n", mode_ptr);
300 ahd_extract_mode_state(ahd, mode_ptr, &src, &dst);
301 ahd_known_modes(ahd, src, dst);
305 ahd_assert_modes(struct ahd_softc *ahd, ahd_mode srcmode,
306 ahd_mode dstmode, const char *file, int line)
309 if ((srcmode & AHD_MK_MSK(ahd->src_mode)) == 0
310 || (dstmode & AHD_MK_MSK(ahd->dst_mode)) == 0) {
311 panic("%s:%s:%d: Mode assertion failed.\n",
312 ahd_name(ahd), file, line);
317 #define AHD_ASSERT_MODES(ahd, source, dest) \
318 ahd_assert_modes(ahd, source, dest, __FILE__, __LINE__);
321 ahd_save_modes(struct ahd_softc *ahd)
323 if (ahd->src_mode == AHD_MODE_UNKNOWN
324 || ahd->dst_mode == AHD_MODE_UNKNOWN)
325 ahd_update_modes(ahd);
327 return (ahd_build_mode_state(ahd, ahd->src_mode, ahd->dst_mode));
331 ahd_restore_modes(struct ahd_softc *ahd, ahd_mode_state state)
336 ahd_extract_mode_state(ahd, state, &src, &dst);
337 ahd_set_modes(ahd, src, dst);
341 * Determine whether the sequencer has halted code execution.
342 * Returns non-zero status if the sequencer is stopped.
345 ahd_is_paused(struct ahd_softc *ahd)
347 return ((ahd_inb(ahd, HCNTRL) & PAUSE) != 0);
351 * Request that the sequencer stop and wait, indefinitely, for it
352 * to stop. The sequencer will only acknowledge that it is paused
353 * once it has reached an instruction boundary and PAUSEDIS is
354 * cleared in the SEQCTL register. The sequencer may use PAUSEDIS
355 * for critical sections.
358 ahd_pause(struct ahd_softc *ahd)
360 ahd_outb(ahd, HCNTRL, ahd->pause);
363 * Since the sequencer can disable pausing in a critical section, we
364 * must loop until it actually stops.
366 while (ahd_is_paused(ahd) == 0)
371 * Allow the sequencer to continue program execution.
372 * We check here to ensure that no additional interrupt
373 * sources that would cause the sequencer to halt have been
374 * asserted. If, for example, a SCSI bus reset is detected
375 * while we are fielding a different, pausing, interrupt type,
376 * we don't want to release the sequencer before going back
377 * into our interrupt handler and dealing with this new
381 ahd_unpause(struct ahd_softc *ahd)
384 * Automatically restore our modes to those saved
385 * prior to the first change of the mode.
387 if (ahd->saved_src_mode != AHD_MODE_UNKNOWN
388 && ahd->saved_dst_mode != AHD_MODE_UNKNOWN) {
389 if ((ahd->flags & AHD_UPDATE_PEND_CMDS) != 0)
390 ahd_reset_cmds_pending(ahd);
391 ahd_set_modes(ahd, ahd->saved_src_mode, ahd->saved_dst_mode);
394 if ((ahd_inb(ahd, INTSTAT) & ~CMDCMPLT) == 0)
395 ahd_outb(ahd, HCNTRL, ahd->unpause);
397 ahd_known_modes(ahd, AHD_MODE_UNKNOWN, AHD_MODE_UNKNOWN);
400 /*********************** Scatter Gather List Handling *************************/
402 ahd_sg_setup(struct ahd_softc *ahd, struct scb *scb,
403 void *sgptr, dma_addr_t addr, bus_size_t len, int last)
406 if (sizeof(dma_addr_t) > 4
407 && (ahd->flags & AHD_64BIT_ADDRESSING) != 0) {
408 struct ahd_dma64_seg *sg;
410 sg = (struct ahd_dma64_seg *)sgptr;
411 sg->addr = ahd_htole64(addr);
412 sg->len = ahd_htole32(len | (last ? AHD_DMA_LAST_SEG : 0));
415 struct ahd_dma_seg *sg;
417 sg = (struct ahd_dma_seg *)sgptr;
418 sg->addr = ahd_htole32(addr & 0xFFFFFFFF);
419 sg->len = ahd_htole32(len | ((addr >> 8) & 0x7F000000)
420 | (last ? AHD_DMA_LAST_SEG : 0));
426 ahd_setup_scb_common(struct ahd_softc *ahd, struct scb *scb)
428 /* XXX Handle target mode SCBs. */
429 scb->crc_retry_count = 0;
430 if ((scb->flags & SCB_PACKETIZED) != 0) {
431 /* XXX what about ACA?? It is type 4, but TAG_TYPE == 0x3. */
432 scb->hscb->task_attribute = scb->hscb->control & SCB_TAG_TYPE;
434 if (ahd_get_transfer_length(scb) & 0x01)
435 scb->hscb->task_attribute = SCB_XFERLEN_ODD;
437 scb->hscb->task_attribute = 0;
440 if (scb->hscb->cdb_len <= MAX_CDB_LEN_WITH_SENSE_ADDR
441 || (scb->hscb->cdb_len & SCB_CDB_LEN_PTR) != 0)
442 scb->hscb->shared_data.idata.cdb_plus_saddr.sense_addr =
443 ahd_htole32(scb->sense_busaddr);
447 ahd_setup_data_scb(struct ahd_softc *ahd, struct scb *scb)
450 * Copy the first SG into the "current" data ponter area.
452 if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0) {
453 struct ahd_dma64_seg *sg;
455 sg = (struct ahd_dma64_seg *)scb->sg_list;
456 scb->hscb->dataptr = sg->addr;
457 scb->hscb->datacnt = sg->len;
459 struct ahd_dma_seg *sg;
460 uint32_t *dataptr_words;
462 sg = (struct ahd_dma_seg *)scb->sg_list;
463 dataptr_words = (uint32_t*)&scb->hscb->dataptr;
464 dataptr_words[0] = sg->addr;
465 dataptr_words[1] = 0;
466 if ((ahd->flags & AHD_39BIT_ADDRESSING) != 0) {
469 high_addr = ahd_le32toh(sg->len) & 0x7F000000;
470 scb->hscb->dataptr |= ahd_htole64(high_addr << 8);
472 scb->hscb->datacnt = sg->len;
475 * Note where to find the SG entries in bus space.
476 * We also set the full residual flag which the
477 * sequencer will clear as soon as a data transfer
480 scb->hscb->sgptr = ahd_htole32(scb->sg_list_busaddr|SG_FULL_RESID);
484 ahd_setup_noxfer_scb(struct ahd_softc *ahd, struct scb *scb)
486 scb->hscb->sgptr = ahd_htole32(SG_LIST_NULL);
487 scb->hscb->dataptr = 0;
488 scb->hscb->datacnt = 0;
491 /************************** Memory mapping routines ***************************/
493 ahd_sg_bus_to_virt(struct ahd_softc *ahd, struct scb *scb, uint32_t sg_busaddr)
495 dma_addr_t sg_offset;
497 /* sg_list_phys points to entry 1, not 0 */
498 sg_offset = sg_busaddr - (scb->sg_list_busaddr - ahd_sg_size(ahd));
499 return ((uint8_t *)scb->sg_list + sg_offset);
503 ahd_sg_virt_to_bus(struct ahd_softc *ahd, struct scb *scb, void *sg)
505 dma_addr_t sg_offset;
507 /* sg_list_phys points to entry 1, not 0 */
508 sg_offset = ((uint8_t *)sg - (uint8_t *)scb->sg_list)
511 return (scb->sg_list_busaddr + sg_offset);
515 ahd_sync_scb(struct ahd_softc *ahd, struct scb *scb, int op)
517 ahd_dmamap_sync(ahd, ahd->scb_data.hscb_dmat,
518 scb->hscb_map->dmamap,
519 /*offset*/(uint8_t*)scb->hscb - scb->hscb_map->vaddr,
520 /*len*/sizeof(*scb->hscb), op);
524 ahd_sync_sglist(struct ahd_softc *ahd, struct scb *scb, int op)
526 if (scb->sg_count == 0)
529 ahd_dmamap_sync(ahd, ahd->scb_data.sg_dmat,
531 /*offset*/scb->sg_list_busaddr - ahd_sg_size(ahd),
532 /*len*/ahd_sg_size(ahd) * scb->sg_count, op);
536 ahd_sync_sense(struct ahd_softc *ahd, struct scb *scb, int op)
538 ahd_dmamap_sync(ahd, ahd->scb_data.sense_dmat,
539 scb->sense_map->dmamap,
540 /*offset*/scb->sense_busaddr,
541 /*len*/AHD_SENSE_BUFSIZE, op);
545 ahd_targetcmd_offset(struct ahd_softc *ahd, u_int index)
547 return (((uint8_t *)&ahd->targetcmds[index])
548 - (uint8_t *)ahd->qoutfifo);
551 /*********************** Miscelaneous Support Functions ***********************/
553 * Return pointers to the transfer negotiation information
554 * for the specified our_id/remote_id pair.
556 struct ahd_initiator_tinfo *
557 ahd_fetch_transinfo(struct ahd_softc *ahd, char channel, u_int our_id,
558 u_int remote_id, struct ahd_tmode_tstate **tstate)
561 * Transfer data structures are stored from the perspective
562 * of the target role. Since the parameters for a connection
563 * in the initiator role to a given target are the same as
564 * when the roles are reversed, we pretend we are the target.
568 *tstate = ahd->enabled_targets[our_id];
569 return (&(*tstate)->transinfo[remote_id]);
573 ahd_inw(struct ahd_softc *ahd, u_int port)
576 * Read high byte first as some registers increment
577 * or have other side effects when the low byte is
580 uint16_t r = ahd_inb(ahd, port+1) << 8;
581 return r | ahd_inb(ahd, port);
585 ahd_outw(struct ahd_softc *ahd, u_int port, u_int value)
588 * Write low byte first to accomodate registers
589 * such as PRGMCNT where the order maters.
591 ahd_outb(ahd, port, value & 0xFF);
592 ahd_outb(ahd, port+1, (value >> 8) & 0xFF);
596 ahd_inl(struct ahd_softc *ahd, u_int port)
598 return ((ahd_inb(ahd, port))
599 | (ahd_inb(ahd, port+1) << 8)
600 | (ahd_inb(ahd, port+2) << 16)
601 | (ahd_inb(ahd, port+3) << 24));
605 ahd_outl(struct ahd_softc *ahd, u_int port, uint32_t value)
607 ahd_outb(ahd, port, (value) & 0xFF);
608 ahd_outb(ahd, port+1, ((value) >> 8) & 0xFF);
609 ahd_outb(ahd, port+2, ((value) >> 16) & 0xFF);
610 ahd_outb(ahd, port+3, ((value) >> 24) & 0xFF);
614 ahd_inq(struct ahd_softc *ahd, u_int port)
616 return ((ahd_inb(ahd, port))
617 | (ahd_inb(ahd, port+1) << 8)
618 | (ahd_inb(ahd, port+2) << 16)
619 | (ahd_inb(ahd, port+3) << 24)
620 | (((uint64_t)ahd_inb(ahd, port+4)) << 32)
621 | (((uint64_t)ahd_inb(ahd, port+5)) << 40)
622 | (((uint64_t)ahd_inb(ahd, port+6)) << 48)
623 | (((uint64_t)ahd_inb(ahd, port+7)) << 56));
627 ahd_outq(struct ahd_softc *ahd, u_int port, uint64_t value)
629 ahd_outb(ahd, port, value & 0xFF);
630 ahd_outb(ahd, port+1, (value >> 8) & 0xFF);
631 ahd_outb(ahd, port+2, (value >> 16) & 0xFF);
632 ahd_outb(ahd, port+3, (value >> 24) & 0xFF);
633 ahd_outb(ahd, port+4, (value >> 32) & 0xFF);
634 ahd_outb(ahd, port+5, (value >> 40) & 0xFF);
635 ahd_outb(ahd, port+6, (value >> 48) & 0xFF);
636 ahd_outb(ahd, port+7, (value >> 56) & 0xFF);
640 ahd_get_scbptr(struct ahd_softc *ahd)
642 AHD_ASSERT_MODES(ahd, ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK),
643 ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK));
644 return (ahd_inb(ahd, SCBPTR) | (ahd_inb(ahd, SCBPTR + 1) << 8));
648 ahd_set_scbptr(struct ahd_softc *ahd, u_int scbptr)
650 AHD_ASSERT_MODES(ahd, ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK),
651 ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK));
652 ahd_outb(ahd, SCBPTR, scbptr & 0xFF);
653 ahd_outb(ahd, SCBPTR+1, (scbptr >> 8) & 0xFF);
657 ahd_get_hnscb_qoff(struct ahd_softc *ahd)
659 return (ahd_inw_atomic(ahd, HNSCB_QOFF));
663 ahd_set_hnscb_qoff(struct ahd_softc *ahd, u_int value)
665 ahd_outw_atomic(ahd, HNSCB_QOFF, value);
669 ahd_get_hescb_qoff(struct ahd_softc *ahd)
671 return (ahd_inb(ahd, HESCB_QOFF));
675 ahd_set_hescb_qoff(struct ahd_softc *ahd, u_int value)
677 ahd_outb(ahd, HESCB_QOFF, value);
681 ahd_get_snscb_qoff(struct ahd_softc *ahd)
685 AHD_ASSERT_MODES(ahd, AHD_MODE_CCHAN_MSK, AHD_MODE_CCHAN_MSK);
686 oldvalue = ahd_inw(ahd, SNSCB_QOFF);
687 ahd_outw(ahd, SNSCB_QOFF, oldvalue);
692 ahd_set_snscb_qoff(struct ahd_softc *ahd, u_int value)
694 AHD_ASSERT_MODES(ahd, AHD_MODE_CCHAN_MSK, AHD_MODE_CCHAN_MSK);
695 ahd_outw(ahd, SNSCB_QOFF, value);
699 ahd_get_sescb_qoff(struct ahd_softc *ahd)
701 AHD_ASSERT_MODES(ahd, AHD_MODE_CCHAN_MSK, AHD_MODE_CCHAN_MSK);
702 return (ahd_inb(ahd, SESCB_QOFF));
706 ahd_set_sescb_qoff(struct ahd_softc *ahd, u_int value)
708 AHD_ASSERT_MODES(ahd, AHD_MODE_CCHAN_MSK, AHD_MODE_CCHAN_MSK);
709 ahd_outb(ahd, SESCB_QOFF, value);
713 ahd_get_sdscb_qoff(struct ahd_softc *ahd)
715 AHD_ASSERT_MODES(ahd, AHD_MODE_CCHAN_MSK, AHD_MODE_CCHAN_MSK);
716 return (ahd_inb(ahd, SDSCB_QOFF) | (ahd_inb(ahd, SDSCB_QOFF + 1) << 8));
720 ahd_set_sdscb_qoff(struct ahd_softc *ahd, u_int value)
722 AHD_ASSERT_MODES(ahd, AHD_MODE_CCHAN_MSK, AHD_MODE_CCHAN_MSK);
723 ahd_outb(ahd, SDSCB_QOFF, value & 0xFF);
724 ahd_outb(ahd, SDSCB_QOFF+1, (value >> 8) & 0xFF);
728 ahd_inb_scbram(struct ahd_softc *ahd, u_int offset)
733 * Workaround PCI-X Rev A. hardware bug.
734 * After a host read of SCB memory, the chip
735 * may become confused into thinking prefetch
736 * was required. This starts the discard timer
737 * running and can cause an unexpected discard
738 * timer interrupt. The work around is to read
739 * a normal register prior to the exhaustion of
740 * the discard timer. The mode pointer register
741 * has no side effects and so serves well for
746 value = ahd_inb(ahd, offset);
747 if ((ahd->bugs & AHD_PCIX_SCBRAM_RD_BUG) != 0)
748 ahd_inb(ahd, MODE_PTR);
753 ahd_inw_scbram(struct ahd_softc *ahd, u_int offset)
755 return (ahd_inb_scbram(ahd, offset)
756 | (ahd_inb_scbram(ahd, offset+1) << 8));
760 ahd_inl_scbram(struct ahd_softc *ahd, u_int offset)
762 return (ahd_inw_scbram(ahd, offset)
763 | (ahd_inw_scbram(ahd, offset+2) << 16));
767 ahd_inq_scbram(struct ahd_softc *ahd, u_int offset)
769 return (ahd_inl_scbram(ahd, offset)
770 | ((uint64_t)ahd_inl_scbram(ahd, offset+4)) << 32);
774 ahd_lookup_scb(struct ahd_softc *ahd, u_int tag)
778 if (tag >= AHD_SCB_MAX)
780 scb = ahd->scb_data.scbindex[tag];
782 ahd_sync_scb(ahd, scb,
783 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
788 ahd_swap_with_next_hscb(struct ahd_softc *ahd, struct scb *scb)
790 struct hardware_scb *q_hscb;
791 struct map_node *q_hscb_map;
792 uint32_t saved_hscb_busaddr;
795 * Our queuing method is a bit tricky. The card
796 * knows in advance which HSCB (by address) to download,
797 * and we can't disappoint it. To achieve this, the next
798 * HSCB to download is saved off in ahd->next_queued_hscb.
799 * When we are called to queue "an arbitrary scb",
800 * we copy the contents of the incoming HSCB to the one
801 * the sequencer knows about, swap HSCB pointers and
802 * finally assign the SCB to the tag indexed location
803 * in the scb_array. This makes sure that we can still
804 * locate the correct SCB by SCB_TAG.
806 q_hscb = ahd->next_queued_hscb;
807 q_hscb_map = ahd->next_queued_hscb_map;
808 saved_hscb_busaddr = q_hscb->hscb_busaddr;
809 memcpy(q_hscb, scb->hscb, sizeof(*scb->hscb));
810 q_hscb->hscb_busaddr = saved_hscb_busaddr;
811 q_hscb->next_hscb_busaddr = scb->hscb->hscb_busaddr;
813 /* Now swap HSCB pointers. */
814 ahd->next_queued_hscb = scb->hscb;
815 ahd->next_queued_hscb_map = scb->hscb_map;
817 scb->hscb_map = q_hscb_map;
819 /* Now define the mapping from tag to SCB in the scbindex */
820 ahd->scb_data.scbindex[SCB_GET_TAG(scb)] = scb;
824 * Tell the sequencer about a new transaction to execute.
827 ahd_queue_scb(struct ahd_softc *ahd, struct scb *scb)
829 ahd_swap_with_next_hscb(ahd, scb);
831 if (SCBID_IS_NULL(SCB_GET_TAG(scb)))
832 panic("Attempt to queue invalid SCB tag %x\n",
836 * Keep a history of SCBs we've downloaded in the qinfifo.
838 ahd->qinfifo[AHD_QIN_WRAP(ahd->qinfifonext)] = SCB_GET_TAG(scb);
841 if (scb->sg_count != 0)
842 ahd_setup_data_scb(ahd, scb);
844 ahd_setup_noxfer_scb(ahd, scb);
845 ahd_setup_scb_common(ahd, scb);
848 * Make sure our data is consistent from the
849 * perspective of the adapter.
851 ahd_sync_scb(ahd, scb, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
854 if ((ahd_debug & AHD_SHOW_QUEUE) != 0) {
855 uint64_t host_dataptr;
857 host_dataptr = ahd_le64toh(scb->hscb->dataptr);
858 printf("%s: Queueing SCB %d:0x%x bus addr 0x%x - 0x%x%x/0x%x\n",
860 SCB_GET_TAG(scb), scb->hscb->scsiid,
861 ahd_le32toh(scb->hscb->hscb_busaddr),
862 (u_int)((host_dataptr >> 32) & 0xFFFFFFFF),
863 (u_int)(host_dataptr & 0xFFFFFFFF),
864 ahd_le32toh(scb->hscb->datacnt));
867 /* Tell the adapter about the newly queued SCB */
868 ahd_set_hnscb_qoff(ahd, ahd->qinfifonext);
871 /************************** Interrupt Processing ******************************/
873 ahd_sync_qoutfifo(struct ahd_softc *ahd, int op)
875 ahd_dmamap_sync(ahd, ahd->shared_data_dmat, ahd->shared_data_map.dmamap,
877 /*len*/AHD_SCB_MAX * sizeof(struct ahd_completion), op);
881 ahd_sync_tqinfifo(struct ahd_softc *ahd, int op)
883 #ifdef AHD_TARGET_MODE
884 if ((ahd->flags & AHD_TARGETROLE) != 0) {
885 ahd_dmamap_sync(ahd, ahd->shared_data_dmat,
886 ahd->shared_data_map.dmamap,
887 ahd_targetcmd_offset(ahd, 0),
888 sizeof(struct target_cmd) * AHD_TMODE_CMDS,
895 * See if the firmware has posted any completed commands
896 * into our in-core command complete fifos.
898 #define AHD_RUN_QOUTFIFO 0x1
899 #define AHD_RUN_TQINFIFO 0x2
901 ahd_check_cmdcmpltqueues(struct ahd_softc *ahd)
906 ahd_dmamap_sync(ahd, ahd->shared_data_dmat, ahd->shared_data_map.dmamap,
907 /*offset*/ahd->qoutfifonext * sizeof(*ahd->qoutfifo),
908 /*len*/sizeof(*ahd->qoutfifo), BUS_DMASYNC_POSTREAD);
909 if (ahd->qoutfifo[ahd->qoutfifonext].valid_tag
910 == ahd->qoutfifonext_valid_tag)
911 retval |= AHD_RUN_QOUTFIFO;
912 #ifdef AHD_TARGET_MODE
913 if ((ahd->flags & AHD_TARGETROLE) != 0
914 && (ahd->flags & AHD_TQINFIFO_BLOCKED) == 0) {
915 ahd_dmamap_sync(ahd, ahd->shared_data_dmat,
916 ahd->shared_data_map.dmamap,
917 ahd_targetcmd_offset(ahd, ahd->tqinfifofnext),
918 /*len*/sizeof(struct target_cmd),
919 BUS_DMASYNC_POSTREAD);
920 if (ahd->targetcmds[ahd->tqinfifonext].cmd_valid != 0)
921 retval |= AHD_RUN_TQINFIFO;
928 * Catch an interrupt from the adapter
931 ahd_intr(struct ahd_softc *ahd)
935 if ((ahd->pause & INTEN) == 0) {
937 * Our interrupt is not enabled on the chip
938 * and may be disabled for re-entrancy reasons,
939 * so just return. This is likely just a shared
946 * Instead of directly reading the interrupt status register,
947 * infer the cause of the interrupt by checking our in-core
948 * completion queues. This avoids a costly PCI bus read in
951 if ((ahd->flags & AHD_ALL_INTERRUPTS) == 0
952 && (ahd_check_cmdcmpltqueues(ahd) != 0))
955 intstat = ahd_inb(ahd, INTSTAT);
957 if ((intstat & INT_PEND) == 0)
960 if (intstat & CMDCMPLT) {
961 ahd_outb(ahd, CLRINT, CLRCMDINT);
964 * Ensure that the chip sees that we've cleared
965 * this interrupt before we walk the output fifo.
966 * Otherwise, we may, due to posted bus writes,
967 * clear the interrupt after we finish the scan,
968 * and after the sequencer has added new entries
969 * and asserted the interrupt again.
971 if ((ahd->bugs & AHD_INTCOLLISION_BUG) != 0) {
972 if (ahd_is_paused(ahd)) {
974 * Potentially lost SEQINT.
975 * If SEQINTCODE is non-zero,
976 * simulate the SEQINT.
978 if (ahd_inb(ahd, SEQINTCODE) != NO_SEQINT)
982 ahd_flush_device_writes(ahd);
984 ahd_run_qoutfifo(ahd);
985 ahd->cmdcmplt_counts[ahd->cmdcmplt_bucket]++;
986 ahd->cmdcmplt_total++;
987 #ifdef AHD_TARGET_MODE
988 if ((ahd->flags & AHD_TARGETROLE) != 0)
989 ahd_run_tqinfifo(ahd, /*paused*/FALSE);
994 * Handle statuses that may invalidate our cached
995 * copy of INTSTAT separately.
997 if (intstat == 0xFF && (ahd->features & AHD_REMOVABLE) != 0) {
998 /* Hot eject. Do nothing */
999 } else if (intstat & HWERRINT) {
1000 ahd_handle_hwerrint(ahd);
1001 } else if ((intstat & (PCIINT|SPLTINT)) != 0) {
1005 if ((intstat & SEQINT) != 0)
1006 ahd_handle_seqint(ahd, intstat);
1008 if ((intstat & SCSIINT) != 0)
1009 ahd_handle_scsiint(ahd, intstat);
1014 /******************************** Private Inlines *****************************/
1015 static __inline void
1016 ahd_assert_atn(struct ahd_softc *ahd)
1018 ahd_outb(ahd, SCSISIGO, ATNO);
1022 * Determine if the current connection has a packetized
1023 * agreement. This does not necessarily mean that we
1024 * are currently in a packetized transfer. We could
1025 * just as easily be sending or receiving a message.
1028 ahd_currently_packetized(struct ahd_softc *ahd)
1030 ahd_mode_state saved_modes;
1033 saved_modes = ahd_save_modes(ahd);
1034 if ((ahd->bugs & AHD_PKTIZED_STATUS_BUG) != 0) {
1036 * The packetized bit refers to the last
1037 * connection, not the current one. Check
1038 * for non-zero LQISTATE instead.
1040 ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
1041 packetized = ahd_inb(ahd, LQISTATE) != 0;
1043 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
1044 packetized = ahd_inb(ahd, LQISTAT2) & PACKETIZED;
1046 ahd_restore_modes(ahd, saved_modes);
1047 return (packetized);
1051 ahd_set_active_fifo(struct ahd_softc *ahd)
1055 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
1056 active_fifo = ahd_inb(ahd, DFFSTAT) & CURRFIFO;
1057 switch (active_fifo) {
1060 ahd_set_modes(ahd, active_fifo, active_fifo);
1067 static __inline void
1068 ahd_unbusy_tcl(struct ahd_softc *ahd, u_int tcl)
1070 ahd_busy_tcl(ahd, tcl, SCB_LIST_NULL);
1074 * Determine whether the sequencer reported a residual
1075 * for this SCB/transaction.
1077 static __inline void
1078 ahd_update_residual(struct ahd_softc *ahd, struct scb *scb)
1082 sgptr = ahd_le32toh(scb->hscb->sgptr);
1083 if ((sgptr & SG_STATUS_VALID) != 0)
1084 ahd_calc_residual(ahd, scb);
1087 static __inline void
1088 ahd_complete_scb(struct ahd_softc *ahd, struct scb *scb)
1092 sgptr = ahd_le32toh(scb->hscb->sgptr);
1093 if ((sgptr & SG_STATUS_VALID) != 0)
1094 ahd_handle_scb_status(ahd, scb);
1100 /************************* Sequencer Execution Control ************************/
1102 * Restart the sequencer program from address zero
1105 ahd_restart(struct ahd_softc *ahd)
1110 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
1112 /* No more pending messages */
1113 ahd_clear_msg_state(ahd);
1114 ahd_outb(ahd, SCSISIGO, 0); /* De-assert BSY */
1115 ahd_outb(ahd, MSG_OUT, MSG_NOOP); /* No message to send */
1116 ahd_outb(ahd, SXFRCTL1, ahd_inb(ahd, SXFRCTL1) & ~BITBUCKET);
1117 ahd_outb(ahd, SEQINTCTL, 0);
1118 ahd_outb(ahd, LASTPHASE, P_BUSFREE);
1119 ahd_outb(ahd, SEQ_FLAGS, 0);
1120 ahd_outb(ahd, SAVED_SCSIID, 0xFF);
1121 ahd_outb(ahd, SAVED_LUN, 0xFF);
1124 * Ensure that the sequencer's idea of TQINPOS
1125 * matches our own. The sequencer increments TQINPOS
1126 * only after it sees a DMA complete and a reset could
1127 * occur before the increment leaving the kernel to believe
1128 * the command arrived but the sequencer to not.
1130 ahd_outb(ahd, TQINPOS, ahd->tqinfifonext);
1132 /* Always allow reselection */
1133 ahd_outb(ahd, SCSISEQ1,
1134 ahd_inb(ahd, SCSISEQ_TEMPLATE) & (ENSELI|ENRSELI|ENAUTOATNP));
1135 ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
1138 * Clear any pending sequencer interrupt. It is no
1139 * longer relevant since we're resetting the Program
1142 ahd_outb(ahd, CLRINT, CLRSEQINT);
1144 ahd_outb(ahd, SEQCTL0, FASTMODE|SEQRESET);
1149 ahd_clear_fifo(struct ahd_softc *ahd, u_int fifo)
1151 ahd_mode_state saved_modes;
1154 if ((ahd_debug & AHD_SHOW_FIFOS) != 0)
1155 printf("%s: Clearing FIFO %d\n", ahd_name(ahd), fifo);
1157 saved_modes = ahd_save_modes(ahd);
1158 ahd_set_modes(ahd, fifo, fifo);
1159 ahd_outb(ahd, DFFSXFRCTL, RSTCHN|CLRSHCNT);
1160 if ((ahd_inb(ahd, SG_STATE) & FETCH_INPROG) != 0)
1161 ahd_outb(ahd, CCSGCTL, CCSGRESET);
1162 ahd_outb(ahd, LONGJMP_ADDR + 1, INVALID_ADDR);
1163 ahd_outb(ahd, SG_STATE, 0);
1164 ahd_restore_modes(ahd, saved_modes);
1167 /************************* Input/Output Queues ********************************/
1169 * Flush and completed commands that are sitting in the command
1170 * complete queues down on the chip but have yet to be dma'ed back up.
1173 ahd_flush_qoutfifo(struct ahd_softc *ahd)
1176 ahd_mode_state saved_modes;
1182 saved_modes = ahd_save_modes(ahd);
1185 * Flush the good status FIFO for completed packetized commands.
1187 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
1188 saved_scbptr = ahd_get_scbptr(ahd);
1189 while ((ahd_inb(ahd, LQISTAT2) & LQIGSAVAIL) != 0) {
1193 scbid = ahd_inw(ahd, GSFIFO);
1194 scb = ahd_lookup_scb(ahd, scbid);
1196 printf("%s: Warning - GSFIFO SCB %d invalid\n",
1197 ahd_name(ahd), scbid);
1201 * Determine if this transaction is still active in
1202 * any FIFO. If it is, we must flush that FIFO to
1203 * the host before completing the command.
1207 for (i = 0; i < 2; i++) {
1208 /* Toggle to the other mode. */
1210 ahd_set_modes(ahd, fifo_mode, fifo_mode);
1212 if (ahd_scb_active_in_fifo(ahd, scb) == 0)
1215 ahd_run_data_fifo(ahd, scb);
1218 * Running this FIFO may cause a CFG4DATA for
1219 * this same transaction to assert in the other
1220 * FIFO or a new snapshot SAVEPTRS interrupt
1221 * in this FIFO. Even running a FIFO may not
1222 * clear the transaction if we are still waiting
1223 * for data to drain to the host. We must loop
1224 * until the transaction is not active in either
1225 * FIFO just to be sure. Reset our loop counter
1226 * so we will visit both FIFOs again before
1227 * declaring this transaction finished. We
1228 * also delay a bit so that status has a chance
1229 * to change before we look at this FIFO again.
1234 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
1235 ahd_set_scbptr(ahd, scbid);
1236 if ((ahd_inb_scbram(ahd, SCB_SGPTR) & SG_LIST_NULL) == 0
1237 && ((ahd_inb_scbram(ahd, SCB_SGPTR) & SG_FULL_RESID) != 0
1238 || (ahd_inb_scbram(ahd, SCB_RESIDUAL_SGPTR)
1239 & SG_LIST_NULL) != 0)) {
1243 * The transfer completed with a residual.
1244 * Place this SCB on the complete DMA list
1245 * so that we update our in-core copy of the
1246 * SCB before completing the command.
1248 ahd_outb(ahd, SCB_SCSI_STATUS, 0);
1249 ahd_outb(ahd, SCB_SGPTR,
1250 ahd_inb_scbram(ahd, SCB_SGPTR)
1252 ahd_outw(ahd, SCB_TAG, scbid);
1253 ahd_outw(ahd, SCB_NEXT_COMPLETE, SCB_LIST_NULL);
1254 comp_head = ahd_inw(ahd, COMPLETE_DMA_SCB_HEAD);
1255 if (SCBID_IS_NULL(comp_head)) {
1256 ahd_outw(ahd, COMPLETE_DMA_SCB_HEAD, scbid);
1257 ahd_outw(ahd, COMPLETE_DMA_SCB_TAIL, scbid);
1261 tail = ahd_inw(ahd, COMPLETE_DMA_SCB_TAIL);
1262 ahd_set_scbptr(ahd, tail);
1263 ahd_outw(ahd, SCB_NEXT_COMPLETE, scbid);
1264 ahd_outw(ahd, COMPLETE_DMA_SCB_TAIL, scbid);
1265 ahd_set_scbptr(ahd, scbid);
1268 ahd_complete_scb(ahd, scb);
1270 ahd_set_scbptr(ahd, saved_scbptr);
1273 * Setup for command channel portion of flush.
1275 ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
1278 * Wait for any inprogress DMA to complete and clear DMA state
1279 * if this if for an SCB in the qinfifo.
1281 while (((ccscbctl = ahd_inb(ahd, CCSCBCTL)) & (CCARREN|CCSCBEN)) != 0) {
1283 if ((ccscbctl & (CCSCBDIR|CCARREN)) == (CCSCBDIR|CCARREN)) {
1284 if ((ccscbctl & ARRDONE) != 0)
1286 } else if ((ccscbctl & CCSCBDONE) != 0)
1291 * We leave the sequencer to cleanup in the case of DMA's to
1292 * update the qoutfifo. In all other cases (DMA's to the
1293 * chip or a push of an SCB from the COMPLETE_DMA_SCB list),
1294 * we disable the DMA engine so that the sequencer will not
1295 * attempt to handle the DMA completion.
1297 if ((ccscbctl & CCSCBDIR) != 0 || (ccscbctl & ARRDONE) != 0)
1298 ahd_outb(ahd, CCSCBCTL, ccscbctl & ~(CCARREN|CCSCBEN));
1301 * Complete any SCBs that just finished
1302 * being DMA'ed into the qoutfifo.
1304 ahd_run_qoutfifo(ahd);
1306 saved_scbptr = ahd_get_scbptr(ahd);
1308 * Manually update/complete any completed SCBs that are waiting to be
1309 * DMA'ed back up to the host.
1311 scbid = ahd_inw(ahd, COMPLETE_DMA_SCB_HEAD);
1312 while (!SCBID_IS_NULL(scbid)) {
1316 ahd_set_scbptr(ahd, scbid);
1317 next_scbid = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE);
1318 scb = ahd_lookup_scb(ahd, scbid);
1320 printf("%s: Warning - DMA-up and complete "
1321 "SCB %d invalid\n", ahd_name(ahd), scbid);
1324 hscb_ptr = (uint8_t *)scb->hscb;
1325 for (i = 0; i < sizeof(struct hardware_scb); i++)
1326 *hscb_ptr++ = ahd_inb_scbram(ahd, SCB_BASE + i);
1328 ahd_complete_scb(ahd, scb);
1331 ahd_outw(ahd, COMPLETE_DMA_SCB_HEAD, SCB_LIST_NULL);
1332 ahd_outw(ahd, COMPLETE_DMA_SCB_TAIL, SCB_LIST_NULL);
1334 scbid = ahd_inw(ahd, COMPLETE_ON_QFREEZE_HEAD);
1335 while (!SCBID_IS_NULL(scbid)) {
1337 ahd_set_scbptr(ahd, scbid);
1338 next_scbid = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE);
1339 scb = ahd_lookup_scb(ahd, scbid);
1341 printf("%s: Warning - Complete Qfrz SCB %d invalid\n",
1342 ahd_name(ahd), scbid);
1346 ahd_complete_scb(ahd, scb);
1349 ahd_outw(ahd, COMPLETE_ON_QFREEZE_HEAD, SCB_LIST_NULL);
1351 scbid = ahd_inw(ahd, COMPLETE_SCB_HEAD);
1352 while (!SCBID_IS_NULL(scbid)) {
1354 ahd_set_scbptr(ahd, scbid);
1355 next_scbid = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE);
1356 scb = ahd_lookup_scb(ahd, scbid);
1358 printf("%s: Warning - Complete SCB %d invalid\n",
1359 ahd_name(ahd), scbid);
1363 ahd_complete_scb(ahd, scb);
1366 ahd_outw(ahd, COMPLETE_SCB_HEAD, SCB_LIST_NULL);
1371 ahd_set_scbptr(ahd, saved_scbptr);
1372 ahd_restore_modes(ahd, saved_modes);
1373 ahd->flags |= AHD_UPDATE_PEND_CMDS;
1377 * Determine if an SCB for a packetized transaction
1378 * is active in a FIFO.
1381 ahd_scb_active_in_fifo(struct ahd_softc *ahd, struct scb *scb)
1385 * The FIFO is only active for our transaction if
1386 * the SCBPTR matches the SCB's ID and the firmware
1387 * has installed a handler for the FIFO or we have
1388 * a pending SAVEPTRS or CFG4DATA interrupt.
1390 if (ahd_get_scbptr(ahd) != SCB_GET_TAG(scb)
1391 || ((ahd_inb(ahd, LONGJMP_ADDR+1) & INVALID_ADDR) != 0
1392 && (ahd_inb(ahd, SEQINTSRC) & (CFG4DATA|SAVEPTRS)) == 0))
1399 * Run a data fifo to completion for a transaction we know
1400 * has completed across the SCSI bus (good status has been
1401 * received). We are already set to the correct FIFO mode
1402 * on entry to this routine.
1404 * This function attempts to operate exactly as the firmware
1405 * would when running this FIFO. Care must be taken to update
1406 * this routine any time the firmware's FIFO algorithm is
1410 ahd_run_data_fifo(struct ahd_softc *ahd, struct scb *scb)
1414 seqintsrc = ahd_inb(ahd, SEQINTSRC);
1415 if ((seqintsrc & CFG4DATA) != 0) {
1420 * Clear full residual flag.
1422 sgptr = ahd_inl_scbram(ahd, SCB_SGPTR) & ~SG_FULL_RESID;
1423 ahd_outb(ahd, SCB_SGPTR, sgptr);
1426 * Load datacnt and address.
1428 datacnt = ahd_inl_scbram(ahd, SCB_DATACNT);
1429 if ((datacnt & AHD_DMA_LAST_SEG) != 0) {
1431 ahd_outb(ahd, SG_STATE, 0);
1433 ahd_outb(ahd, SG_STATE, LOADING_NEEDED);
1434 ahd_outq(ahd, HADDR, ahd_inq_scbram(ahd, SCB_DATAPTR));
1435 ahd_outl(ahd, HCNT, datacnt & AHD_SG_LEN_MASK);
1436 ahd_outb(ahd, SG_CACHE_PRE, sgptr);
1437 ahd_outb(ahd, DFCNTRL, PRELOADEN|SCSIEN|HDMAEN);
1440 * Initialize Residual Fields.
1442 ahd_outb(ahd, SCB_RESIDUAL_DATACNT+3, datacnt >> 24);
1443 ahd_outl(ahd, SCB_RESIDUAL_SGPTR, sgptr & SG_PTR_MASK);
1446 * Mark the SCB as having a FIFO in use.
1448 ahd_outb(ahd, SCB_FIFO_USE_COUNT,
1449 ahd_inb_scbram(ahd, SCB_FIFO_USE_COUNT) + 1);
1452 * Install a "fake" handler for this FIFO.
1454 ahd_outw(ahd, LONGJMP_ADDR, 0);
1457 * Notify the hardware that we have satisfied
1458 * this sequencer interrupt.
1460 ahd_outb(ahd, CLRSEQINTSRC, CLRCFG4DATA);
1461 } else if ((seqintsrc & SAVEPTRS) != 0) {
1465 if ((ahd_inb(ahd, LONGJMP_ADDR+1)&INVALID_ADDR) != 0) {
1467 * Snapshot Save Pointers. All that
1468 * is necessary to clear the snapshot
1475 * Disable S/G fetch so the DMA engine
1476 * is available to future users.
1478 if ((ahd_inb(ahd, SG_STATE) & FETCH_INPROG) != 0)
1479 ahd_outb(ahd, CCSGCTL, 0);
1480 ahd_outb(ahd, SG_STATE, 0);
1483 * Flush the data FIFO. Strickly only
1484 * necessary for Rev A parts.
1486 ahd_outb(ahd, DFCNTRL, ahd_inb(ahd, DFCNTRL) | FIFOFLUSH);
1489 * Calculate residual.
1491 sgptr = ahd_inl_scbram(ahd, SCB_RESIDUAL_SGPTR);
1492 resid = ahd_inl(ahd, SHCNT);
1493 resid |= ahd_inb_scbram(ahd, SCB_RESIDUAL_DATACNT+3) << 24;
1494 ahd_outl(ahd, SCB_RESIDUAL_DATACNT, resid);
1495 if ((ahd_inb(ahd, SG_CACHE_SHADOW) & LAST_SEG) == 0) {
1497 * Must back up to the correct S/G element.
1498 * Typically this just means resetting our
1499 * low byte to the offset in the SG_CACHE,
1500 * but if we wrapped, we have to correct
1501 * the other bytes of the sgptr too.
1503 if ((ahd_inb(ahd, SG_CACHE_SHADOW) & 0x80) != 0
1504 && (sgptr & 0x80) == 0)
1507 sgptr |= ahd_inb(ahd, SG_CACHE_SHADOW)
1509 ahd_outl(ahd, SCB_RESIDUAL_SGPTR, sgptr);
1510 ahd_outb(ahd, SCB_RESIDUAL_DATACNT + 3, 0);
1511 } else if ((resid & AHD_SG_LEN_MASK) == 0) {
1512 ahd_outb(ahd, SCB_RESIDUAL_SGPTR,
1513 sgptr | SG_LIST_NULL);
1518 ahd_outq(ahd, SCB_DATAPTR, ahd_inq(ahd, SHADDR));
1519 ahd_outl(ahd, SCB_DATACNT, resid);
1520 ahd_outl(ahd, SCB_SGPTR, sgptr);
1521 ahd_outb(ahd, CLRSEQINTSRC, CLRSAVEPTRS);
1522 ahd_outb(ahd, SEQIMODE,
1523 ahd_inb(ahd, SEQIMODE) | ENSAVEPTRS);
1525 * If the data is to the SCSI bus, we are
1526 * done, otherwise wait for FIFOEMP.
1528 if ((ahd_inb(ahd, DFCNTRL) & DIRECTION) != 0)
1530 } else if ((ahd_inb(ahd, SG_STATE) & LOADING_NEEDED) != 0) {
1537 * Disable S/G fetch so the DMA engine
1538 * is available to future users. We won't
1539 * be using the DMA engine to load segments.
1541 if ((ahd_inb(ahd, SG_STATE) & FETCH_INPROG) != 0) {
1542 ahd_outb(ahd, CCSGCTL, 0);
1543 ahd_outb(ahd, SG_STATE, LOADING_NEEDED);
1547 * Wait for the DMA engine to notice that the
1548 * host transfer is enabled and that there is
1549 * space in the S/G FIFO for new segments before
1550 * loading more segments.
1552 if ((ahd_inb(ahd, DFSTATUS) & PRELOAD_AVAIL) != 0
1553 && (ahd_inb(ahd, DFCNTRL) & HDMAENACK) != 0) {
1556 * Determine the offset of the next S/G
1559 sgptr = ahd_inl_scbram(ahd, SCB_RESIDUAL_SGPTR);
1560 sgptr &= SG_PTR_MASK;
1561 if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0) {
1562 struct ahd_dma64_seg *sg;
1564 sg = ahd_sg_bus_to_virt(ahd, scb, sgptr);
1565 data_addr = sg->addr;
1567 sgptr += sizeof(*sg);
1569 struct ahd_dma_seg *sg;
1571 sg = ahd_sg_bus_to_virt(ahd, scb, sgptr);
1572 data_addr = sg->len & AHD_SG_HIGH_ADDR_MASK;
1574 data_addr |= sg->addr;
1576 sgptr += sizeof(*sg);
1580 * Update residual information.
1582 ahd_outb(ahd, SCB_RESIDUAL_DATACNT+3, data_len >> 24);
1583 ahd_outl(ahd, SCB_RESIDUAL_SGPTR, sgptr);
1588 if (data_len & AHD_DMA_LAST_SEG) {
1590 ahd_outb(ahd, SG_STATE, 0);
1592 ahd_outq(ahd, HADDR, data_addr);
1593 ahd_outl(ahd, HCNT, data_len & AHD_SG_LEN_MASK);
1594 ahd_outb(ahd, SG_CACHE_PRE, sgptr & 0xFF);
1597 * Advertise the segment to the hardware.
1599 dfcntrl = ahd_inb(ahd, DFCNTRL)|PRELOADEN|HDMAEN;
1600 if ((ahd->features & AHD_NEW_DFCNTRL_OPTS) != 0) {
1602 * Use SCSIENWRDIS so that SCSIEN
1603 * is never modified by this
1606 dfcntrl |= SCSIENWRDIS;
1608 ahd_outb(ahd, DFCNTRL, dfcntrl);
1610 } else if ((ahd_inb(ahd, SG_CACHE_SHADOW) & LAST_SEG_DONE) != 0) {
1613 * Transfer completed to the end of SG list
1614 * and has flushed to the host.
1616 ahd_outb(ahd, SCB_SGPTR,
1617 ahd_inb_scbram(ahd, SCB_SGPTR) | SG_LIST_NULL);
1619 } else if ((ahd_inb(ahd, DFSTATUS) & FIFOEMP) != 0) {
1622 * Clear any handler for this FIFO, decrement
1623 * the FIFO use count for the SCB, and release
1626 ahd_outb(ahd, LONGJMP_ADDR + 1, INVALID_ADDR);
1627 ahd_outb(ahd, SCB_FIFO_USE_COUNT,
1628 ahd_inb_scbram(ahd, SCB_FIFO_USE_COUNT) - 1);
1629 ahd_outb(ahd, DFFSXFRCTL, CLRCHN);
1634 * Look for entries in the QoutFIFO that have completed.
1635 * The valid_tag completion field indicates the validity
1636 * of the entry - the valid value toggles each time through
1637 * the queue. We use the sg_status field in the completion
1638 * entry to avoid referencing the hscb if the completion
1639 * occurred with no errors and no residual. sg_status is
1640 * a copy of the first byte (little endian) of the sgptr
1644 ahd_run_qoutfifo(struct ahd_softc *ahd)
1646 struct ahd_completion *completion;
1650 if ((ahd->flags & AHD_RUNNING_QOUTFIFO) != 0)
1651 panic("ahd_run_qoutfifo recursion");
1652 ahd->flags |= AHD_RUNNING_QOUTFIFO;
1653 ahd_sync_qoutfifo(ahd, BUS_DMASYNC_POSTREAD);
1655 completion = &ahd->qoutfifo[ahd->qoutfifonext];
1657 if (completion->valid_tag != ahd->qoutfifonext_valid_tag)
1660 scb_index = ahd_le16toh(completion->tag);
1661 scb = ahd_lookup_scb(ahd, scb_index);
1663 printf("%s: WARNING no command for scb %d "
1664 "(cmdcmplt)\nQOUTPOS = %d\n",
1665 ahd_name(ahd), scb_index,
1667 ahd_dump_card_state(ahd);
1668 } else if ((completion->sg_status & SG_STATUS_VALID) != 0) {
1669 ahd_handle_scb_status(ahd, scb);
1674 ahd->qoutfifonext = (ahd->qoutfifonext+1) & (AHD_QOUT_SIZE-1);
1675 if (ahd->qoutfifonext == 0)
1676 ahd->qoutfifonext_valid_tag ^= QOUTFIFO_ENTRY_VALID;
1678 ahd->flags &= ~AHD_RUNNING_QOUTFIFO;
1681 /************************* Interrupt Handling *********************************/
1683 ahd_handle_hwerrint(struct ahd_softc *ahd)
1686 * Some catastrophic hardware error has occurred.
1687 * Print it for the user and disable the controller.
1692 error = ahd_inb(ahd, ERROR);
1693 for (i = 0; i < num_errors; i++) {
1694 if ((error & ahd_hard_errors[i].errno) != 0)
1695 printf("%s: hwerrint, %s\n",
1696 ahd_name(ahd), ahd_hard_errors[i].errmesg);
1699 ahd_dump_card_state(ahd);
1702 /* Tell everyone that this HBA is no longer available */
1703 ahd_abort_scbs(ahd, CAM_TARGET_WILDCARD, ALL_CHANNELS,
1704 CAM_LUN_WILDCARD, SCB_LIST_NULL, ROLE_UNKNOWN,
1707 /* Tell the system that this controller has gone away. */
1713 ahd_dump_sglist(struct scb *scb)
1717 if (scb->sg_count > 0) {
1718 if ((scb->ahd_softc->flags & AHD_64BIT_ADDRESSING) != 0) {
1719 struct ahd_dma64_seg *sg_list;
1721 sg_list = (struct ahd_dma64_seg*)scb->sg_list;
1722 for (i = 0; i < scb->sg_count; i++) {
1726 addr = ahd_le64toh(sg_list[i].addr);
1727 len = ahd_le32toh(sg_list[i].len);
1728 printf("sg[%d] - Addr 0x%x%x : Length %d%s\n",
1730 (uint32_t)((addr >> 32) & 0xFFFFFFFF),
1731 (uint32_t)(addr & 0xFFFFFFFF),
1732 sg_list[i].len & AHD_SG_LEN_MASK,
1733 (sg_list[i].len & AHD_DMA_LAST_SEG)
1737 struct ahd_dma_seg *sg_list;
1739 sg_list = (struct ahd_dma_seg*)scb->sg_list;
1740 for (i = 0; i < scb->sg_count; i++) {
1743 len = ahd_le32toh(sg_list[i].len);
1744 printf("sg[%d] - Addr 0x%x%x : Length %d%s\n",
1746 (len & AHD_SG_HIGH_ADDR_MASK) >> 24,
1747 ahd_le32toh(sg_list[i].addr),
1748 len & AHD_SG_LEN_MASK,
1749 len & AHD_DMA_LAST_SEG ? " Last" : "");
1754 #endif /* AHD_DEBUG */
1757 ahd_handle_seqint(struct ahd_softc *ahd, u_int intstat)
1762 * Save the sequencer interrupt code and clear the SEQINT
1763 * bit. We will unpause the sequencer, if appropriate,
1764 * after servicing the request.
1766 seqintcode = ahd_inb(ahd, SEQINTCODE);
1767 ahd_outb(ahd, CLRINT, CLRSEQINT);
1768 if ((ahd->bugs & AHD_INTCOLLISION_BUG) != 0) {
1770 * Unpause the sequencer and let it clear
1771 * SEQINT by writing NO_SEQINT to it. This
1772 * will cause the sequencer to be paused again,
1773 * which is the expected state of this routine.
1776 while (!ahd_is_paused(ahd))
1778 ahd_outb(ahd, CLRINT, CLRSEQINT);
1780 ahd_update_modes(ahd);
1782 if ((ahd_debug & AHD_SHOW_MISC) != 0)
1783 printf("%s: Handle Seqint Called for code %d\n",
1784 ahd_name(ahd), seqintcode);
1786 switch (seqintcode) {
1787 case ENTERING_NONPACK:
1792 AHD_ASSERT_MODES(ahd, ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK),
1793 ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK));
1794 scbid = ahd_get_scbptr(ahd);
1795 scb = ahd_lookup_scb(ahd, scbid);
1798 * Somehow need to know if this
1799 * is from a selection or reselection.
1800 * From that, we can determine target
1801 * ID so we at least have an I_T nexus.
1804 ahd_outb(ahd, SAVED_SCSIID, scb->hscb->scsiid);
1805 ahd_outb(ahd, SAVED_LUN, scb->hscb->lun);
1806 ahd_outb(ahd, SEQ_FLAGS, 0x0);
1808 if ((ahd_inb(ahd, LQISTAT2) & LQIPHASE_OUTPKT) != 0
1809 && (ahd_inb(ahd, SCSISIGO) & ATNO) != 0) {
1811 * Phase change after read stream with
1812 * CRC error with P0 asserted on last
1816 if ((ahd_debug & AHD_SHOW_RECOVERY) != 0)
1817 printf("%s: Assuming LQIPHASE_NLQ with "
1818 "P0 assertion\n", ahd_name(ahd));
1822 if ((ahd_debug & AHD_SHOW_RECOVERY) != 0)
1823 printf("%s: Entering NONPACK\n", ahd_name(ahd));
1827 case INVALID_SEQINT:
1828 printf("%s: Invalid Sequencer interrupt occurred, "
1829 "resetting channel.\n",
1832 if ((ahd_debug & AHD_SHOW_RECOVERY) != 0)
1833 ahd_dump_card_state(ahd);
1835 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
1837 case STATUS_OVERRUN:
1842 scbid = ahd_get_scbptr(ahd);
1843 scb = ahd_lookup_scb(ahd, scbid);
1845 ahd_print_path(ahd, scb);
1847 printf("%s: ", ahd_name(ahd));
1848 printf("SCB %d Packetized Status Overrun", scbid);
1849 ahd_dump_card_state(ahd);
1850 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
1853 case CFG4ISTAT_INTR:
1858 scbid = ahd_get_scbptr(ahd);
1859 scb = ahd_lookup_scb(ahd, scbid);
1861 ahd_dump_card_state(ahd);
1862 printf("CFG4ISTAT: Free SCB %d referenced", scbid);
1863 panic("For safety");
1865 ahd_outq(ahd, HADDR, scb->sense_busaddr);
1866 ahd_outw(ahd, HCNT, AHD_SENSE_BUFSIZE);
1867 ahd_outb(ahd, HCNT + 2, 0);
1868 ahd_outb(ahd, SG_CACHE_PRE, SG_LAST_SEG);
1869 ahd_outb(ahd, DFCNTRL, PRELOADEN|SCSIEN|HDMAEN);
1876 bus_phase = ahd_inb(ahd, SCSISIGI) & PHASE_MASK;
1877 printf("%s: ILLEGAL_PHASE 0x%x\n",
1878 ahd_name(ahd), bus_phase);
1880 switch (bus_phase) {
1888 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
1889 printf("%s: Issued Bus Reset.\n", ahd_name(ahd));
1893 struct ahd_devinfo devinfo;
1895 struct ahd_initiator_tinfo *targ_info;
1896 struct ahd_tmode_tstate *tstate;
1897 struct ahd_transinfo *tinfo;
1901 * If a target takes us into the command phase
1902 * assume that it has been externally reset and
1903 * has thus lost our previous packetized negotiation
1904 * agreement. Since we have not sent an identify
1905 * message and may not have fully qualified the
1906 * connection, we change our command to TUR, assert
1907 * ATN and ABORT the task when we go to message in
1908 * phase. The OSM will see the REQUEUE_REQUEST
1909 * status and retry the command.
1911 scbid = ahd_get_scbptr(ahd);
1912 scb = ahd_lookup_scb(ahd, scbid);
1914 printf("Invalid phase with no valid SCB. "
1915 "Resetting bus.\n");
1916 ahd_reset_channel(ahd, 'A',
1917 /*Initiate Reset*/TRUE);
1920 ahd_compile_devinfo(&devinfo, SCB_GET_OUR_ID(scb),
1921 SCB_GET_TARGET(ahd, scb),
1923 SCB_GET_CHANNEL(ahd, scb),
1925 targ_info = ahd_fetch_transinfo(ahd,
1930 tinfo = &targ_info->curr;
1931 ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
1932 AHD_TRANS_ACTIVE, /*paused*/TRUE);
1933 ahd_set_syncrate(ahd, &devinfo, /*period*/0,
1934 /*offset*/0, /*ppr_options*/0,
1935 AHD_TRANS_ACTIVE, /*paused*/TRUE);
1936 /* Hand-craft TUR command */
1937 ahd_outb(ahd, SCB_CDB_STORE, 0);
1938 ahd_outb(ahd, SCB_CDB_STORE+1, 0);
1939 ahd_outb(ahd, SCB_CDB_STORE+2, 0);
1940 ahd_outb(ahd, SCB_CDB_STORE+3, 0);
1941 ahd_outb(ahd, SCB_CDB_STORE+4, 0);
1942 ahd_outb(ahd, SCB_CDB_STORE+5, 0);
1943 ahd_outb(ahd, SCB_CDB_LEN, 6);
1944 scb->hscb->control &= ~(TAG_ENB|SCB_TAG_TYPE);
1945 scb->hscb->control |= MK_MESSAGE;
1946 ahd_outb(ahd, SCB_CONTROL, scb->hscb->control);
1947 ahd_outb(ahd, MSG_OUT, HOST_MSG);
1948 ahd_outb(ahd, SAVED_SCSIID, scb->hscb->scsiid);
1950 * The lun is 0, regardless of the SCB's lun
1951 * as we have not sent an identify message.
1953 ahd_outb(ahd, SAVED_LUN, 0);
1954 ahd_outb(ahd, SEQ_FLAGS, 0);
1955 ahd_assert_atn(ahd);
1956 scb->flags &= ~SCB_PACKETIZED;
1957 scb->flags |= SCB_ABORT|SCB_EXTERNAL_RESET;
1958 ahd_freeze_devq(ahd, scb);
1959 ahd_set_transaction_status(scb, CAM_REQUEUE_REQ);
1960 ahd_freeze_scb(scb);
1963 ahd_send_async(ahd, devinfo.channel, devinfo.target,
1964 CAM_LUN_WILDCARD, AC_SENT_BDR);
1967 * Allow the sequencer to continue with
1968 * non-pack processing.
1970 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
1971 ahd_outb(ahd, CLRLQOINT1, CLRLQOPHACHGINPKT);
1972 if ((ahd->bugs & AHD_CLRLQO_AUTOCLR_BUG) != 0) {
1973 ahd_outb(ahd, CLRLQOINT1, 0);
1976 if ((ahd_debug & AHD_SHOW_RECOVERY) != 0) {
1977 ahd_print_path(ahd, scb);
1978 printf("Unexpected command phase from "
1979 "packetized target\n");
1993 if ((ahd_debug & AHD_SHOW_RECOVERY) != 0) {
1994 printf("%s: CFG4OVERRUN mode = %x\n", ahd_name(ahd),
1995 ahd_inb(ahd, MODE_PTR));
1998 scb_index = ahd_get_scbptr(ahd);
1999 scb = ahd_lookup_scb(ahd, scb_index);
2002 * Attempt to transfer to an SCB that is
2005 ahd_assert_atn(ahd);
2006 ahd_outb(ahd, MSG_OUT, HOST_MSG);
2007 ahd->msgout_buf[0] = MSG_ABORT_TASK;
2008 ahd->msgout_len = 1;
2009 ahd->msgout_index = 0;
2010 ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
2012 * Clear status received flag to prevent any
2013 * attempt to complete this bogus SCB.
2015 ahd_outb(ahd, SCB_CONTROL,
2016 ahd_inb_scbram(ahd, SCB_CONTROL)
2021 case DUMP_CARD_STATE:
2023 ahd_dump_card_state(ahd);
2029 if ((ahd_debug & AHD_SHOW_RECOVERY) != 0) {
2030 printf("%s: PDATA_REINIT - DFCNTRL = 0x%x "
2031 "SG_CACHE_SHADOW = 0x%x\n",
2032 ahd_name(ahd), ahd_inb(ahd, DFCNTRL),
2033 ahd_inb(ahd, SG_CACHE_SHADOW));
2036 ahd_reinitialize_dataptrs(ahd);
2041 struct ahd_devinfo devinfo;
2044 * The sequencer has encountered a message phase
2045 * that requires host assistance for completion.
2046 * While handling the message phase(s), we will be
2047 * notified by the sequencer after each byte is
2048 * transfered so we can track bus phase changes.
2050 * If this is the first time we've seen a HOST_MSG_LOOP
2051 * interrupt, initialize the state of the host message
2054 ahd_fetch_devinfo(ahd, &devinfo);
2055 if (ahd->msg_type == MSG_TYPE_NONE) {
2060 bus_phase = ahd_inb(ahd, SCSISIGI) & PHASE_MASK;
2061 if (bus_phase != P_MESGIN
2062 && bus_phase != P_MESGOUT) {
2063 printf("ahd_intr: HOST_MSG_LOOP bad "
2064 "phase 0x%x\n", bus_phase);
2066 * Probably transitioned to bus free before
2067 * we got here. Just punt the message.
2069 ahd_dump_card_state(ahd);
2070 ahd_clear_intstat(ahd);
2075 scb_index = ahd_get_scbptr(ahd);
2076 scb = ahd_lookup_scb(ahd, scb_index);
2077 if (devinfo.role == ROLE_INITIATOR) {
2078 if (bus_phase == P_MESGOUT)
2079 ahd_setup_initiator_msgout(ahd,
2084 MSG_TYPE_INITIATOR_MSGIN;
2085 ahd->msgin_index = 0;
2088 #ifdef AHD_TARGET_MODE
2090 if (bus_phase == P_MESGOUT) {
2092 MSG_TYPE_TARGET_MSGOUT;
2093 ahd->msgin_index = 0;
2096 ahd_setup_target_msgin(ahd,
2103 ahd_handle_message_phase(ahd);
2108 /* Ensure we don't leave the selection hardware on */
2109 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
2110 ahd_outb(ahd, SCSISEQ0, ahd_inb(ahd, SCSISEQ0) & ~ENSELO);
2112 printf("%s:%c:%d: no active SCB for reconnecting "
2113 "target - issuing BUS DEVICE RESET\n",
2114 ahd_name(ahd), 'A', ahd_inb(ahd, SELID) >> 4);
2115 printf("SAVED_SCSIID == 0x%x, SAVED_LUN == 0x%x, "
2116 "REG0 == 0x%x ACCUM = 0x%x\n",
2117 ahd_inb(ahd, SAVED_SCSIID), ahd_inb(ahd, SAVED_LUN),
2118 ahd_inw(ahd, REG0), ahd_inb(ahd, ACCUM));
2119 printf("SEQ_FLAGS == 0x%x, SCBPTR == 0x%x, BTT == 0x%x, "
2121 ahd_inb(ahd, SEQ_FLAGS), ahd_get_scbptr(ahd),
2122 ahd_find_busy_tcl(ahd,
2123 BUILD_TCL(ahd_inb(ahd, SAVED_SCSIID),
2124 ahd_inb(ahd, SAVED_LUN))),
2125 ahd_inw(ahd, SINDEX));
2126 printf("SELID == 0x%x, SCB_SCSIID == 0x%x, SCB_LUN == 0x%x, "
2127 "SCB_CONTROL == 0x%x\n",
2128 ahd_inb(ahd, SELID), ahd_inb_scbram(ahd, SCB_SCSIID),
2129 ahd_inb_scbram(ahd, SCB_LUN),
2130 ahd_inb_scbram(ahd, SCB_CONTROL));
2131 printf("SCSIBUS[0] == 0x%x, SCSISIGI == 0x%x\n",
2132 ahd_inb(ahd, SCSIBUS), ahd_inb(ahd, SCSISIGI));
2133 printf("SXFRCTL0 == 0x%x\n", ahd_inb(ahd, SXFRCTL0));
2134 printf("SEQCTL0 == 0x%x\n", ahd_inb(ahd, SEQCTL0));
2135 ahd_dump_card_state(ahd);
2136 ahd->msgout_buf[0] = MSG_BUS_DEV_RESET;
2137 ahd->msgout_len = 1;
2138 ahd->msgout_index = 0;
2139 ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
2140 ahd_outb(ahd, MSG_OUT, HOST_MSG);
2141 ahd_assert_atn(ahd);
2144 case PROTO_VIOLATION:
2146 ahd_handle_proto_violation(ahd);
2151 struct ahd_devinfo devinfo;
2153 ahd_fetch_devinfo(ahd, &devinfo);
2154 ahd_handle_ign_wide_residue(ahd, &devinfo);
2161 lastphase = ahd_inb(ahd, LASTPHASE);
2162 printf("%s:%c:%d: unknown scsi bus phase %x, "
2163 "lastphase = 0x%x. Attempting to continue\n",
2165 SCSIID_TARGET(ahd, ahd_inb(ahd, SAVED_SCSIID)),
2166 lastphase, ahd_inb(ahd, SCSISIGI));
2169 case MISSED_BUSFREE:
2173 lastphase = ahd_inb(ahd, LASTPHASE);
2174 printf("%s:%c:%d: Missed busfree. "
2175 "Lastphase = 0x%x, Curphase = 0x%x\n",
2177 SCSIID_TARGET(ahd, ahd_inb(ahd, SAVED_SCSIID)),
2178 lastphase, ahd_inb(ahd, SCSISIGI));
2185 * When the sequencer detects an overrun, it
2186 * places the controller in "BITBUCKET" mode
2187 * and allows the target to complete its transfer.
2188 * Unfortunately, none of the counters get updated
2189 * when the controller is in this mode, so we have
2190 * no way of knowing how large the overrun was.
2198 scbindex = ahd_get_scbptr(ahd);
2199 scb = ahd_lookup_scb(ahd, scbindex);
2201 lastphase = ahd_inb(ahd, LASTPHASE);
2202 if ((ahd_debug & AHD_SHOW_RECOVERY) != 0) {
2203 ahd_print_path(ahd, scb);
2204 printf("data overrun detected %s. Tag == 0x%x.\n",
2205 ahd_lookup_phase_entry(lastphase)->phasemsg,
2207 ahd_print_path(ahd, scb);
2208 printf("%s seen Data Phase. Length = %ld. "
2210 ahd_inb(ahd, SEQ_FLAGS) & DPHASE
2211 ? "Have" : "Haven't",
2212 ahd_get_transfer_length(scb), scb->sg_count);
2213 ahd_dump_sglist(scb);
2218 * Set this and it will take effect when the
2219 * target does a command complete.
2221 ahd_freeze_devq(ahd, scb);
2222 ahd_set_transaction_status(scb, CAM_DATA_RUN_ERR);
2223 ahd_freeze_scb(scb);
2228 struct ahd_devinfo devinfo;
2232 ahd_fetch_devinfo(ahd, &devinfo);
2233 printf("%s:%c:%d:%d: Attempt to issue message failed\n",
2234 ahd_name(ahd), devinfo.channel, devinfo.target,
2236 scbid = ahd_get_scbptr(ahd);
2237 scb = ahd_lookup_scb(ahd, scbid);
2239 && (scb->flags & SCB_RECOVERY_SCB) != 0)
2241 * Ensure that we didn't put a second instance of this
2242 * SCB into the QINFIFO.
2244 ahd_search_qinfifo(ahd, SCB_GET_TARGET(ahd, scb),
2245 SCB_GET_CHANNEL(ahd, scb),
2246 SCB_GET_LUN(scb), SCB_GET_TAG(scb),
2247 ROLE_INITIATOR, /*status*/0,
2249 ahd_outb(ahd, SCB_CONTROL,
2250 ahd_inb_scbram(ahd, SCB_CONTROL) & ~MK_MESSAGE);
2253 case TASKMGMT_FUNC_COMPLETE:
2258 scbid = ahd_get_scbptr(ahd);
2259 scb = ahd_lookup_scb(ahd, scbid);
2265 ahd_print_path(ahd, scb);
2266 printf("Task Management Func 0x%x Complete\n",
2267 scb->hscb->task_management);
2268 lun = CAM_LUN_WILDCARD;
2269 tag = SCB_LIST_NULL;
2271 switch (scb->hscb->task_management) {
2272 case SIU_TASKMGMT_ABORT_TASK:
2273 tag = SCB_GET_TAG(scb);
2274 case SIU_TASKMGMT_ABORT_TASK_SET:
2275 case SIU_TASKMGMT_CLEAR_TASK_SET:
2276 lun = scb->hscb->lun;
2277 error = CAM_REQ_ABORTED;
2278 ahd_abort_scbs(ahd, SCB_GET_TARGET(ahd, scb),
2279 'A', lun, tag, ROLE_INITIATOR,
2282 case SIU_TASKMGMT_LUN_RESET:
2283 lun = scb->hscb->lun;
2284 case SIU_TASKMGMT_TARGET_RESET:
2286 struct ahd_devinfo devinfo;
2288 ahd_scb_devinfo(ahd, &devinfo, scb);
2289 error = CAM_BDR_SENT;
2290 ahd_handle_devreset(ahd, &devinfo, lun,
2292 lun != CAM_LUN_WILDCARD
2295 /*verbose_level*/0);
2299 panic("Unexpected TaskMgmt Func\n");
2305 case TASKMGMT_CMD_CMPLT_OKAY:
2311 * An ABORT TASK TMF failed to be delivered before
2312 * the targeted command completed normally.
2314 scbid = ahd_get_scbptr(ahd);
2315 scb = ahd_lookup_scb(ahd, scbid);
2318 * Remove the second instance of this SCB from
2319 * the QINFIFO if it is still there.
2321 ahd_print_path(ahd, scb);
2322 printf("SCB completes before TMF\n");
2324 * Handle losing the race. Wait until any
2325 * current selection completes. We will then
2326 * set the TMF back to zero in this SCB so that
2327 * the sequencer doesn't bother to issue another
2328 * sequencer interrupt for its completion.
2330 while ((ahd_inb(ahd, SCSISEQ0) & ENSELO) != 0
2331 && (ahd_inb(ahd, SSTAT0) & SELDO) == 0
2332 && (ahd_inb(ahd, SSTAT1) & SELTO) == 0)
2334 ahd_outb(ahd, SCB_TASK_MANAGEMENT, 0);
2335 ahd_search_qinfifo(ahd, SCB_GET_TARGET(ahd, scb),
2336 SCB_GET_CHANNEL(ahd, scb),
2337 SCB_GET_LUN(scb), SCB_GET_TAG(scb),
2338 ROLE_INITIATOR, /*status*/0,
2347 printf("%s: Tracepoint %d\n", ahd_name(ahd),
2348 seqintcode - TRACEPOINT0);
2353 ahd_handle_hwerrint(ahd);
2356 printf("%s: Unexpected SEQINTCODE %d\n", ahd_name(ahd),
2361 * The sequencer is paused immediately on
2362 * a SEQINT, so we should restart it when
2369 ahd_handle_scsiint(struct ahd_softc *ahd, u_int intstat)
2380 ahd_update_modes(ahd);
2381 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
2383 status3 = ahd_inb(ahd, SSTAT3) & (NTRAMPERR|OSRAMPERR);
2384 status0 = ahd_inb(ahd, SSTAT0) & (IOERR|OVERRUN|SELDI|SELDO);
2385 status = ahd_inb(ahd, SSTAT1) & (SELTO|SCSIRSTI|BUSFREE|SCSIPERR);
2386 lqistat1 = ahd_inb(ahd, LQISTAT1);
2387 lqostat0 = ahd_inb(ahd, LQOSTAT0);
2388 busfreetime = ahd_inb(ahd, SSTAT2) & BUSFREETIME;
2391 * Ignore external resets after a bus reset.
2393 if (((status & SCSIRSTI) != 0) && (ahd->flags & AHD_BUS_RESET_ACTIVE)) {
2394 ahd_outb(ahd, CLRSINT1, CLRSCSIRSTI);
2399 * Clear bus reset flag
2401 ahd->flags &= ~AHD_BUS_RESET_ACTIVE;
2403 if ((status0 & (SELDI|SELDO)) != 0) {
2406 ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
2407 simode0 = ahd_inb(ahd, SIMODE0);
2408 status0 &= simode0 & (IOERR|OVERRUN|SELDI|SELDO);
2409 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
2411 scbid = ahd_get_scbptr(ahd);
2412 scb = ahd_lookup_scb(ahd, scbid);
2414 && (ahd_inb(ahd, SEQ_FLAGS) & NOT_IDENTIFIED) != 0)
2417 if ((status0 & IOERR) != 0) {
2420 now_lvd = ahd_inb(ahd, SBLKCTL) & ENAB40;
2421 printf("%s: Transceiver State Has Changed to %s mode\n",
2422 ahd_name(ahd), now_lvd ? "LVD" : "SE");
2423 ahd_outb(ahd, CLRSINT0, CLRIOERR);
2425 * A change in I/O mode is equivalent to a bus reset.
2427 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
2429 ahd_setup_iocell_workaround(ahd);
2431 } else if ((status0 & OVERRUN) != 0) {
2433 printf("%s: SCSI offset overrun detected. Resetting bus.\n",
2435 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
2436 } else if ((status & SCSIRSTI) != 0) {
2438 printf("%s: Someone reset channel A\n", ahd_name(ahd));
2439 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/FALSE);
2440 } else if ((status & SCSIPERR) != 0) {
2442 /* Make sure the sequencer is in a safe location. */
2443 ahd_clear_critical_section(ahd);
2445 ahd_handle_transmission_error(ahd);
2446 } else if (lqostat0 != 0) {
2448 printf("%s: lqostat0 == 0x%x!\n", ahd_name(ahd), lqostat0);
2449 ahd_outb(ahd, CLRLQOINT0, lqostat0);
2450 if ((ahd->bugs & AHD_CLRLQO_AUTOCLR_BUG) != 0)
2451 ahd_outb(ahd, CLRLQOINT1, 0);
2452 } else if ((status & SELTO) != 0) {
2455 /* Stop the selection */
2456 ahd_outb(ahd, SCSISEQ0, 0);
2458 /* Make sure the sequencer is in a safe location. */
2459 ahd_clear_critical_section(ahd);
2461 /* No more pending messages */
2462 ahd_clear_msg_state(ahd);
2464 /* Clear interrupt state */
2465 ahd_outb(ahd, CLRSINT1, CLRSELTIMEO|CLRBUSFREE|CLRSCSIPERR);
2468 * Although the driver does not care about the
2469 * 'Selection in Progress' status bit, the busy
2470 * LED does. SELINGO is only cleared by a sucessfull
2471 * selection, so we must manually clear it to insure
2472 * the LED turns off just incase no future successful
2473 * selections occur (e.g. no devices on the bus).
2475 ahd_outb(ahd, CLRSINT0, CLRSELINGO);
2477 scbid = ahd_inw(ahd, WAITING_TID_HEAD);
2478 scb = ahd_lookup_scb(ahd, scbid);
2480 printf("%s: ahd_intr - referenced scb not "
2481 "valid during SELTO scb(0x%x)\n",
2482 ahd_name(ahd), scbid);
2483 ahd_dump_card_state(ahd);
2485 struct ahd_devinfo devinfo;
2487 if ((ahd_debug & AHD_SHOW_SELTO) != 0) {
2488 ahd_print_path(ahd, scb);
2489 printf("Saw Selection Timeout for SCB 0x%x\n",
2493 ahd_scb_devinfo(ahd, &devinfo, scb);
2494 ahd_set_transaction_status(scb, CAM_SEL_TIMEOUT);
2495 ahd_freeze_devq(ahd, scb);
2498 * Cancel any pending transactions on the device
2499 * now that it seems to be missing. This will
2500 * also revert us to async/narrow transfers until
2501 * we can renegotiate with the device.
2503 ahd_handle_devreset(ahd, &devinfo,
2506 "Selection Timeout",
2507 /*verbose_level*/1);
2509 ahd_outb(ahd, CLRINT, CLRSCSIINT);
2510 ahd_iocell_first_selection(ahd);
2512 } else if ((status0 & (SELDI|SELDO)) != 0) {
2514 ahd_iocell_first_selection(ahd);
2516 } else if (status3 != 0) {
2517 printf("%s: SCSI Cell parity error SSTAT3 == 0x%x\n",
2518 ahd_name(ahd), status3);
2519 ahd_outb(ahd, CLRSINT3, status3);
2520 } else if ((lqistat1 & (LQIPHASE_LQ|LQIPHASE_NLQ)) != 0) {
2522 /* Make sure the sequencer is in a safe location. */
2523 ahd_clear_critical_section(ahd);
2525 ahd_handle_lqiphase_error(ahd, lqistat1);
2526 } else if ((lqistat1 & LQICRCI_NLQ) != 0) {
2528 * This status can be delayed during some
2529 * streaming operations. The SCSIPHASE
2530 * handler has already dealt with this case
2531 * so just clear the error.
2533 ahd_outb(ahd, CLRLQIINT1, CLRLQICRCI_NLQ);
2534 } else if ((status & BUSFREE) != 0
2535 || (lqistat1 & LQOBUSFREE) != 0) {
2543 * Clear our selection hardware as soon as possible.
2544 * We may have an entry in the waiting Q for this target,
2545 * that is affected by this busfree and we don't want to
2546 * go about selecting the target while we handle the event.
2548 ahd_outb(ahd, SCSISEQ0, 0);
2550 /* Make sure the sequencer is in a safe location. */
2551 ahd_clear_critical_section(ahd);
2554 * Determine what we were up to at the time of
2557 mode = AHD_MODE_SCSI;
2558 busfreetime = ahd_inb(ahd, SSTAT2) & BUSFREETIME;
2559 lqostat1 = ahd_inb(ahd, LQOSTAT1);
2560 switch (busfreetime) {
2567 mode = busfreetime == BUSFREE_DFF0
2568 ? AHD_MODE_DFF0 : AHD_MODE_DFF1;
2569 ahd_set_modes(ahd, mode, mode);
2570 scbid = ahd_get_scbptr(ahd);
2571 scb = ahd_lookup_scb(ahd, scbid);
2573 printf("%s: Invalid SCB %d in DFF%d "
2574 "during unexpected busfree\n",
2575 ahd_name(ahd), scbid, mode);
2578 packetized = (scb->flags & SCB_PACKETIZED) != 0;
2588 packetized = (lqostat1 & LQOBUSFREE) != 0;
2590 && ahd_inb(ahd, LASTPHASE) == P_BUSFREE
2591 && (ahd_inb(ahd, SSTAT0) & SELDI) == 0
2592 && ((ahd_inb(ahd, SSTAT0) & SELDO) == 0
2593 || (ahd_inb(ahd, SCSISEQ0) & ENSELO) == 0))
2595 * Assume packetized if we are not
2596 * on the bus in a non-packetized
2597 * capacity and any pending selection
2598 * was a packetized selection.
2605 if ((ahd_debug & AHD_SHOW_MISC) != 0)
2606 printf("Saw Busfree. Busfreetime = 0x%x.\n",
2610 * Busfrees that occur in non-packetized phases are
2611 * handled by the nonpkt_busfree handler.
2613 if (packetized && ahd_inb(ahd, LASTPHASE) == P_BUSFREE) {
2614 restart = ahd_handle_pkt_busfree(ahd, busfreetime);
2617 restart = ahd_handle_nonpkt_busfree(ahd);
2620 * Clear the busfree interrupt status. The setting of
2621 * the interrupt is a pulse, so in a perfect world, we
2622 * would not need to muck with the ENBUSFREE logic. This
2623 * would ensure that if the bus moves on to another
2624 * connection, busfree protection is still in force. If
2625 * BUSFREEREV is broken, however, we must manually clear
2626 * the ENBUSFREE if the busfree occurred during a non-pack
2627 * connection so that we don't get false positives during
2628 * future, packetized, connections.
2630 ahd_outb(ahd, CLRSINT1, CLRBUSFREE);
2632 && (ahd->bugs & AHD_BUSFREEREV_BUG) != 0)
2633 ahd_outb(ahd, SIMODE1,
2634 ahd_inb(ahd, SIMODE1) & ~ENBUSFREE);
2637 ahd_clear_fifo(ahd, mode);
2639 ahd_clear_msg_state(ahd);
2640 ahd_outb(ahd, CLRINT, CLRSCSIINT);
2647 printf("%s: Missing case in ahd_handle_scsiint. status = %x\n",
2648 ahd_name(ahd), status);
2649 ahd_dump_card_state(ahd);
2650 ahd_clear_intstat(ahd);
2656 ahd_handle_transmission_error(struct ahd_softc *ahd)
2670 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
2671 lqistat1 = ahd_inb(ahd, LQISTAT1) & ~(LQIPHASE_LQ|LQIPHASE_NLQ);
2672 lqistat2 = ahd_inb(ahd, LQISTAT2);
2673 if ((lqistat1 & (LQICRCI_NLQ|LQICRCI_LQ)) == 0
2674 && (ahd->bugs & AHD_NLQICRC_DELAYED_BUG) != 0) {
2677 ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
2678 lqistate = ahd_inb(ahd, LQISTATE);
2679 if ((lqistate >= 0x1E && lqistate <= 0x24)
2680 || (lqistate == 0x29)) {
2682 if ((ahd_debug & AHD_SHOW_RECOVERY) != 0) {
2683 printf("%s: NLQCRC found via LQISTATE\n",
2687 lqistat1 |= LQICRCI_NLQ;
2689 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
2692 ahd_outb(ahd, CLRLQIINT1, lqistat1);
2693 lastphase = ahd_inb(ahd, LASTPHASE);
2694 curphase = ahd_inb(ahd, SCSISIGI) & PHASE_MASK;
2695 perrdiag = ahd_inb(ahd, PERRDIAG);
2696 msg_out = MSG_INITIATOR_DET_ERR;
2697 ahd_outb(ahd, CLRSINT1, CLRSCSIPERR);
2700 * Try to find the SCB associated with this error.
2704 || (lqistat1 & LQICRCI_NLQ) != 0) {
2705 if ((lqistat1 & (LQICRCI_NLQ|LQIOVERI_NLQ)) != 0)
2706 ahd_set_active_fifo(ahd);
2707 scbid = ahd_get_scbptr(ahd);
2708 scb = ahd_lookup_scb(ahd, scbid);
2709 if (scb != NULL && SCB_IS_SILENT(scb))
2714 if (silent == FALSE) {
2715 printf("%s: Transmission error detected\n", ahd_name(ahd));
2716 ahd_lqistat1_print(lqistat1, &cur_col, 50);
2717 ahd_lastphase_print(lastphase, &cur_col, 50);
2718 ahd_scsisigi_print(curphase, &cur_col, 50);
2719 ahd_perrdiag_print(perrdiag, &cur_col, 50);
2721 ahd_dump_card_state(ahd);
2724 if ((lqistat1 & (LQIOVERI_LQ|LQIOVERI_NLQ)) != 0) {
2725 if (silent == FALSE) {
2726 printf("%s: Gross protocol error during incoming "
2727 "packet. lqistat1 == 0x%x. Resetting bus.\n",
2728 ahd_name(ahd), lqistat1);
2730 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
2732 } else if ((lqistat1 & LQICRCI_LQ) != 0) {
2734 * A CRC error has been detected on an incoming LQ.
2735 * The bus is currently hung on the last ACK.
2736 * Hit LQIRETRY to release the last ack, and
2737 * wait for the sequencer to determine that ATNO
2738 * is asserted while in message out to take us
2739 * to our host message loop. No NONPACKREQ or
2740 * LQIPHASE type errors will occur in this
2741 * scenario. After this first LQIRETRY, the LQI
2742 * manager will be in ISELO where it will
2743 * happily sit until another packet phase begins.
2744 * Unexpected bus free detection is enabled
2745 * through any phases that occur after we release
2746 * this last ack until the LQI manager sees a
2747 * packet phase. This implies we may have to
2748 * ignore a perfectly valid "unexected busfree"
2749 * after our "initiator detected error" message is
2750 * sent. A busfree is the expected response after
2751 * we tell the target that it's L_Q was corrupted.
2752 * (SPI4R09 10.7.3.3.3)
2754 ahd_outb(ahd, LQCTL2, LQIRETRY);
2755 printf("LQIRetry for LQICRCI_LQ to release ACK\n");
2756 } else if ((lqistat1 & LQICRCI_NLQ) != 0) {
2758 * We detected a CRC error in a NON-LQ packet.
2759 * The hardware has varying behavior in this situation
2760 * depending on whether this packet was part of a
2764 * The hardware has already acked the complete packet.
2765 * If the target honors our outstanding ATN condition,
2766 * we should be (or soon will be) in MSGOUT phase.
2767 * This will trigger the LQIPHASE_LQ status bit as the
2768 * hardware was expecting another LQ. Unexpected
2769 * busfree detection is enabled. Once LQIPHASE_LQ is
2770 * true (first entry into host message loop is much
2771 * the same), we must clear LQIPHASE_LQ and hit
2772 * LQIRETRY so the hardware is ready to handle
2773 * a future LQ. NONPACKREQ will not be asserted again
2774 * once we hit LQIRETRY until another packet is
2775 * processed. The target may either go busfree
2776 * or start another packet in response to our message.
2778 * Read Streaming P0 asserted:
2779 * If we raise ATN and the target completes the entire
2780 * stream (P0 asserted during the last packet), the
2781 * hardware will ack all data and return to the ISTART
2782 * state. When the target reponds to our ATN condition,
2783 * LQIPHASE_LQ will be asserted. We should respond to
2784 * this with an LQIRETRY to prepare for any future
2785 * packets. NONPACKREQ will not be asserted again
2786 * once we hit LQIRETRY until another packet is
2787 * processed. The target may either go busfree or
2788 * start another packet in response to our message.
2789 * Busfree detection is enabled.
2791 * Read Streaming P0 not asserted:
2792 * If we raise ATN and the target transitions to
2793 * MSGOUT in or after a packet where P0 is not
2794 * asserted, the hardware will assert LQIPHASE_NLQ.
2795 * We should respond to the LQIPHASE_NLQ with an
2796 * LQIRETRY. Should the target stay in a non-pkt
2797 * phase after we send our message, the hardware
2798 * will assert LQIPHASE_LQ. Recovery is then just as
2799 * listed above for the read streaming with P0 asserted.
2800 * Busfree detection is enabled.
2802 if (silent == FALSE)
2803 printf("LQICRC_NLQ\n");
2805 printf("%s: No SCB valid for LQICRC_NLQ. "
2806 "Resetting bus\n", ahd_name(ahd));
2807 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
2810 } else if ((lqistat1 & LQIBADLQI) != 0) {
2811 printf("Need to handle BADLQI!\n");
2812 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
2814 } else if ((perrdiag & (PARITYERR|PREVPHASE)) == PARITYERR) {
2815 if ((curphase & ~P_DATAIN_DT) != 0) {
2816 /* Ack the byte. So we can continue. */
2817 if (silent == FALSE)
2818 printf("Acking %s to clear perror\n",
2819 ahd_lookup_phase_entry(curphase)->phasemsg);
2820 ahd_inb(ahd, SCSIDAT);
2823 if (curphase == P_MESGIN)
2824 msg_out = MSG_PARITY_ERROR;
2828 * We've set the hardware to assert ATN if we
2829 * get a parity error on "in" phases, so all we
2830 * need to do is stuff the message buffer with
2831 * the appropriate message. "In" phases have set
2832 * mesg_out to something other than MSG_NOP.
2834 ahd->send_msg_perror = msg_out;
2835 if (scb != NULL && msg_out == MSG_INITIATOR_DET_ERR)
2836 scb->flags |= SCB_TRANSMISSION_ERROR;
2837 ahd_outb(ahd, MSG_OUT, HOST_MSG);
2838 ahd_outb(ahd, CLRINT, CLRSCSIINT);
2843 ahd_handle_lqiphase_error(struct ahd_softc *ahd, u_int lqistat1)
2846 * Clear the sources of the interrupts.
2848 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
2849 ahd_outb(ahd, CLRLQIINT1, lqistat1);
2852 * If the "illegal" phase changes were in response
2853 * to our ATN to flag a CRC error, AND we ended up
2854 * on packet boundaries, clear the error, restart the
2855 * LQI manager as appropriate, and go on our merry
2856 * way toward sending the message. Otherwise, reset
2857 * the bus to clear the error.
2859 ahd_set_active_fifo(ahd);
2860 if ((ahd_inb(ahd, SCSISIGO) & ATNO) != 0
2861 && (ahd_inb(ahd, MDFFSTAT) & DLZERO) != 0) {
2862 if ((lqistat1 & LQIPHASE_LQ) != 0) {
2863 printf("LQIRETRY for LQIPHASE_LQ\n");
2864 ahd_outb(ahd, LQCTL2, LQIRETRY);
2865 } else if ((lqistat1 & LQIPHASE_NLQ) != 0) {
2866 printf("LQIRETRY for LQIPHASE_NLQ\n");
2867 ahd_outb(ahd, LQCTL2, LQIRETRY);
2869 panic("ahd_handle_lqiphase_error: No phase errors\n");
2870 ahd_dump_card_state(ahd);
2871 ahd_outb(ahd, CLRINT, CLRSCSIINT);
2874 printf("Reseting Channel for LQI Phase error\n");
2875 ahd_dump_card_state(ahd);
2876 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
2881 * Packetized unexpected or expected busfree.
2882 * Entered in mode based on busfreetime.
2885 ahd_handle_pkt_busfree(struct ahd_softc *ahd, u_int busfreetime)
2889 AHD_ASSERT_MODES(ahd, ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK),
2890 ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK));
2891 lqostat1 = ahd_inb(ahd, LQOSTAT1);
2892 if ((lqostat1 & LQOBUSFREE) != 0) {
2901 * The LQO manager detected an unexpected busfree
2904 * 1) During an outgoing LQ.
2905 * 2) After an outgoing LQ but before the first
2906 * REQ of the command packet.
2907 * 3) During an outgoing command packet.
2909 * In all cases, CURRSCB is pointing to the
2910 * SCB that encountered the failure. Clean
2911 * up the queue, clear SELDO and LQOBUSFREE,
2912 * and allow the sequencer to restart the select
2913 * out at its lesure.
2915 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
2916 scbid = ahd_inw(ahd, CURRSCB);
2917 scb = ahd_lookup_scb(ahd, scbid);
2919 panic("SCB not valid during LQOBUSFREE");
2923 ahd_outb(ahd, CLRLQOINT1, CLRLQOBUSFREE);
2924 if ((ahd->bugs & AHD_CLRLQO_AUTOCLR_BUG) != 0)
2925 ahd_outb(ahd, CLRLQOINT1, 0);
2926 ahd_outb(ahd, SCSISEQ0, ahd_inb(ahd, SCSISEQ0) & ~ENSELO);
2927 ahd_flush_device_writes(ahd);
2928 ahd_outb(ahd, CLRSINT0, CLRSELDO);
2931 * Return the LQO manager to its idle loop. It will
2932 * not do this automatically if the busfree occurs
2933 * after the first REQ of either the LQ or command
2934 * packet or between the LQ and command packet.
2936 ahd_outb(ahd, LQCTL2, ahd_inb(ahd, LQCTL2) | LQOTOIDLE);
2939 * Update the waiting for selection queue so
2940 * we restart on the correct SCB.
2942 waiting_h = ahd_inw(ahd, WAITING_TID_HEAD);
2943 saved_scbptr = ahd_get_scbptr(ahd);
2944 if (waiting_h != scbid) {
2946 ahd_outw(ahd, WAITING_TID_HEAD, scbid);
2947 waiting_t = ahd_inw(ahd, WAITING_TID_TAIL);
2948 if (waiting_t == waiting_h) {
2949 ahd_outw(ahd, WAITING_TID_TAIL, scbid);
2950 next = SCB_LIST_NULL;
2952 ahd_set_scbptr(ahd, waiting_h);
2953 next = ahd_inw_scbram(ahd, SCB_NEXT2);
2955 ahd_set_scbptr(ahd, scbid);
2956 ahd_outw(ahd, SCB_NEXT2, next);
2958 ahd_set_scbptr(ahd, saved_scbptr);
2959 if (scb->crc_retry_count < AHD_MAX_LQ_CRC_ERRORS) {
2960 if (SCB_IS_SILENT(scb) == FALSE) {
2961 ahd_print_path(ahd, scb);
2962 printf("Probable outgoing LQ CRC error. "
2963 "Retrying command\n");
2965 scb->crc_retry_count++;
2967 ahd_set_transaction_status(scb, CAM_UNCOR_PARITY);
2968 ahd_freeze_scb(scb);
2969 ahd_freeze_devq(ahd, scb);
2971 /* Return unpausing the sequencer. */
2973 } else if ((ahd_inb(ahd, PERRDIAG) & PARITYERR) != 0) {
2975 * Ignore what are really parity errors that
2976 * occur on the last REQ of a free running
2977 * clock prior to going busfree. Some drives
2978 * do not properly active negate just before
2979 * going busfree resulting in a parity glitch.
2981 ahd_outb(ahd, CLRSINT1, CLRSCSIPERR|CLRBUSFREE);
2983 if ((ahd_debug & AHD_SHOW_MASKED_ERRORS) != 0)
2984 printf("%s: Parity on last REQ detected "
2985 "during busfree phase.\n",
2988 /* Return unpausing the sequencer. */
2991 if (ahd->src_mode != AHD_MODE_SCSI) {
2995 scbid = ahd_get_scbptr(ahd);
2996 scb = ahd_lookup_scb(ahd, scbid);
2997 ahd_print_path(ahd, scb);
2998 printf("Unexpected PKT busfree condition\n");
2999 ahd_dump_card_state(ahd);
3000 ahd_abort_scbs(ahd, SCB_GET_TARGET(ahd, scb), 'A',
3001 SCB_GET_LUN(scb), SCB_GET_TAG(scb),
3002 ROLE_INITIATOR, CAM_UNEXP_BUSFREE);
3004 /* Return restarting the sequencer. */
3007 printf("%s: Unexpected PKT busfree condition\n", ahd_name(ahd));
3008 ahd_dump_card_state(ahd);
3009 /* Restart the sequencer. */
3014 * Non-packetized unexpected or expected busfree.
3017 ahd_handle_nonpkt_busfree(struct ahd_softc *ahd)
3019 struct ahd_devinfo devinfo;
3025 u_int initiator_role_id;
3031 * Look at what phase we were last in. If its message out,
3032 * chances are pretty good that the busfree was in response
3033 * to one of our abort requests.
3035 lastphase = ahd_inb(ahd, LASTPHASE);
3036 saved_scsiid = ahd_inb(ahd, SAVED_SCSIID);
3037 saved_lun = ahd_inb(ahd, SAVED_LUN);
3038 target = SCSIID_TARGET(ahd, saved_scsiid);
3039 initiator_role_id = SCSIID_OUR_ID(saved_scsiid);
3040 ahd_compile_devinfo(&devinfo, initiator_role_id,
3041 target, saved_lun, 'A', ROLE_INITIATOR);
3044 scbid = ahd_get_scbptr(ahd);
3045 scb = ahd_lookup_scb(ahd, scbid);
3047 && (ahd_inb(ahd, SEQ_FLAGS) & NOT_IDENTIFIED) != 0)
3050 ppr_busfree = (ahd->msg_flags & MSG_FLAG_EXPECT_PPR_BUSFREE) != 0;
3051 if (lastphase == P_MESGOUT) {
3054 tag = SCB_LIST_NULL;
3055 if (ahd_sent_msg(ahd, AHDMSG_1B, MSG_ABORT_TAG, TRUE)
3056 || ahd_sent_msg(ahd, AHDMSG_1B, MSG_ABORT, TRUE)) {
3061 ahd_print_devinfo(ahd, &devinfo);
3062 printf("Abort for unidentified "
3063 "connection completed.\n");
3064 /* restart the sequencer. */
3067 sent_msg = ahd->msgout_buf[ahd->msgout_index - 1];
3068 ahd_print_path(ahd, scb);
3069 printf("SCB %d - Abort%s Completed.\n",
3071 sent_msg == MSG_ABORT_TAG ? "" : " Tag");
3073 if (sent_msg == MSG_ABORT_TAG)
3074 tag = SCB_GET_TAG(scb);
3076 if ((scb->flags & SCB_EXTERNAL_RESET) != 0) {
3078 * This abort is in response to an
3079 * unexpected switch to command phase
3080 * for a packetized connection. Since
3081 * the identify message was never sent,
3082 * "saved lun" is 0. We really want to
3083 * abort only the SCB that encountered
3084 * this error, which could have a different
3085 * lun. The SCB will be retried so the OS
3086 * will see the UA after renegotiating to
3089 tag = SCB_GET_TAG(scb);
3090 saved_lun = scb->hscb->lun;
3092 found = ahd_abort_scbs(ahd, target, 'A', saved_lun,
3093 tag, ROLE_INITIATOR,
3095 printf("found == 0x%x\n", found);
3097 } else if (ahd_sent_msg(ahd, AHDMSG_1B,
3098 MSG_BUS_DEV_RESET, TRUE)) {
3101 * Don't mark the user's request for this BDR
3102 * as completing with CAM_BDR_SENT. CAM3
3103 * specifies CAM_REQ_CMP.
3106 && scb->io_ctx->ccb_h.func_code== XPT_RESET_DEV
3107 && ahd_match_scb(ahd, scb, target, 'A',
3108 CAM_LUN_WILDCARD, SCB_LIST_NULL,
3110 ahd_set_transaction_status(scb, CAM_REQ_CMP);
3112 ahd_handle_devreset(ahd, &devinfo, CAM_LUN_WILDCARD,
3113 CAM_BDR_SENT, "Bus Device Reset",
3114 /*verbose_level*/0);
3116 } else if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_PPR, FALSE)
3117 && ppr_busfree == 0) {
3118 struct ahd_initiator_tinfo *tinfo;
3119 struct ahd_tmode_tstate *tstate;
3124 * If the previous negotiation was packetized,
3125 * this could be because the device has been
3126 * reset without our knowledge. Force our
3127 * current negotiation to async and retry the
3128 * negotiation. Otherwise retry the command
3129 * with non-ppr negotiation.
3132 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
3133 printf("PPR negotiation rejected busfree.\n");
3135 tinfo = ahd_fetch_transinfo(ahd, devinfo.channel,
3137 devinfo.target, &tstate);
3138 if ((tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ)!=0) {
3139 ahd_set_width(ahd, &devinfo,
3140 MSG_EXT_WDTR_BUS_8_BIT,
3143 ahd_set_syncrate(ahd, &devinfo,
3144 /*period*/0, /*offset*/0,
3149 * The expect PPR busfree handler below
3150 * will effect the retry and necessary
3154 tinfo->curr.transport_version = 2;
3155 tinfo->goal.transport_version = 2;
3156 tinfo->goal.ppr_options = 0;
3158 * Remove any SCBs in the waiting for selection
3159 * queue that may also be for this target so
3160 * that command ordering is preserved.
3162 ahd_freeze_devq(ahd, scb);
3163 ahd_qinfifo_requeue_tail(ahd, scb);
3166 } else if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_WDTR, FALSE)
3167 && ppr_busfree == 0) {
3169 * Negotiation Rejected. Go-narrow and
3173 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
3174 printf("WDTR negotiation rejected busfree.\n");
3176 ahd_set_width(ahd, &devinfo,
3177 MSG_EXT_WDTR_BUS_8_BIT,
3178 AHD_TRANS_CUR|AHD_TRANS_GOAL,
3181 * Remove any SCBs in the waiting for selection
3182 * queue that may also be for this target so that
3183 * command ordering is preserved.
3185 ahd_freeze_devq(ahd, scb);
3186 ahd_qinfifo_requeue_tail(ahd, scb);
3188 } else if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_SDTR, FALSE)
3189 && ppr_busfree == 0) {
3191 * Negotiation Rejected. Go-async and
3195 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
3196 printf("SDTR negotiation rejected busfree.\n");
3198 ahd_set_syncrate(ahd, &devinfo,
3199 /*period*/0, /*offset*/0,
3201 AHD_TRANS_CUR|AHD_TRANS_GOAL,
3204 * Remove any SCBs in the waiting for selection
3205 * queue that may also be for this target so that
3206 * command ordering is preserved.
3208 ahd_freeze_devq(ahd, scb);
3209 ahd_qinfifo_requeue_tail(ahd, scb);
3211 } else if ((ahd->msg_flags & MSG_FLAG_EXPECT_IDE_BUSFREE) != 0
3212 && ahd_sent_msg(ahd, AHDMSG_1B,
3213 MSG_INITIATOR_DET_ERR, TRUE)) {
3216 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
3217 printf("Expected IDE Busfree\n");
3220 } else if ((ahd->msg_flags & MSG_FLAG_EXPECT_QASREJ_BUSFREE)
3221 && ahd_sent_msg(ahd, AHDMSG_1B,
3222 MSG_MESSAGE_REJECT, TRUE)) {
3225 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
3226 printf("Expected QAS Reject Busfree\n");
3233 * The busfree required flag is honored at the end of
3234 * the message phases. We check it last in case we
3235 * had to send some other message that caused a busfree.
3238 && (lastphase == P_MESGIN || lastphase == P_MESGOUT)
3239 && ((ahd->msg_flags & MSG_FLAG_EXPECT_PPR_BUSFREE) != 0)) {
3241 ahd_freeze_devq(ahd, scb);
3242 ahd_set_transaction_status(scb, CAM_REQUEUE_REQ);
3243 ahd_freeze_scb(scb);
3244 if ((ahd->msg_flags & MSG_FLAG_IU_REQ_CHANGED) != 0) {
3245 ahd_abort_scbs(ahd, SCB_GET_TARGET(ahd, scb),
3246 SCB_GET_CHANNEL(ahd, scb),
3247 SCB_GET_LUN(scb), SCB_LIST_NULL,
3248 ROLE_INITIATOR, CAM_REQ_ABORTED);
3251 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
3252 printf("PPR Negotiation Busfree.\n");
3258 if (printerror != 0) {
3265 if ((scb->hscb->control & TAG_ENB) != 0)
3266 tag = SCB_GET_TAG(scb);
3268 tag = SCB_LIST_NULL;
3269 ahd_print_path(ahd, scb);
3270 aborted = ahd_abort_scbs(ahd, target, 'A',
3271 SCB_GET_LUN(scb), tag,
3276 * We had not fully identified this connection,
3277 * so we cannot abort anything.
3279 printf("%s: ", ahd_name(ahd));
3281 printf("Unexpected busfree %s, %d SCBs aborted, "
3282 "PRGMCNT == 0x%x\n",
3283 ahd_lookup_phase_entry(lastphase)->phasemsg,
3285 ahd_inw(ahd, PRGMCNT));
3286 ahd_dump_card_state(ahd);
3287 if (lastphase != P_BUSFREE)
3288 ahd_force_renegotiation(ahd, &devinfo);
3290 /* Always restart the sequencer. */
3295 ahd_handle_proto_violation(struct ahd_softc *ahd)
3297 struct ahd_devinfo devinfo;
3305 ahd_fetch_devinfo(ahd, &devinfo);
3306 scbid = ahd_get_scbptr(ahd);
3307 scb = ahd_lookup_scb(ahd, scbid);
3308 seq_flags = ahd_inb(ahd, SEQ_FLAGS);
3309 curphase = ahd_inb(ahd, SCSISIGI) & PHASE_MASK;
3310 lastphase = ahd_inb(ahd, LASTPHASE);
3311 if ((seq_flags & NOT_IDENTIFIED) != 0) {
3314 * The reconnecting target either did not send an
3315 * identify message, or did, but we didn't find an SCB
3318 ahd_print_devinfo(ahd, &devinfo);
3319 printf("Target did not send an IDENTIFY message. "
3320 "LASTPHASE = 0x%x.\n", lastphase);
3322 } else if (scb == NULL) {
3324 * We don't seem to have an SCB active for this
3325 * transaction. Print an error and reset the bus.
3327 ahd_print_devinfo(ahd, &devinfo);
3328 printf("No SCB found during protocol violation\n");
3329 goto proto_violation_reset;
3331 ahd_set_transaction_status(scb, CAM_SEQUENCE_FAIL);
3332 if ((seq_flags & NO_CDB_SENT) != 0) {
3333 ahd_print_path(ahd, scb);
3334 printf("No or incomplete CDB sent to device.\n");
3335 } else if ((ahd_inb_scbram(ahd, SCB_CONTROL)
3336 & STATUS_RCVD) == 0) {
3338 * The target never bothered to provide status to
3339 * us prior to completing the command. Since we don't
3340 * know the disposition of this command, we must attempt
3341 * to abort it. Assert ATN and prepare to send an abort
3344 ahd_print_path(ahd, scb);
3345 printf("Completed command without status.\n");
3347 ahd_print_path(ahd, scb);
3348 printf("Unknown protocol violation.\n");
3349 ahd_dump_card_state(ahd);
3352 if ((lastphase & ~P_DATAIN_DT) == 0
3353 || lastphase == P_COMMAND) {
3354 proto_violation_reset:
3356 * Target either went directly to data
3357 * phase or didn't respond to our ATN.
3358 * The only safe thing to do is to blow
3359 * it away with a bus reset.
3361 found = ahd_reset_channel(ahd, 'A', TRUE);
3362 printf("%s: Issued Channel %c Bus Reset. "
3363 "%d SCBs aborted\n", ahd_name(ahd), 'A', found);
3366 * Leave the selection hardware off in case
3367 * this abort attempt will affect yet to
3370 ahd_outb(ahd, SCSISEQ0,
3371 ahd_inb(ahd, SCSISEQ0) & ~ENSELO);
3372 ahd_assert_atn(ahd);
3373 ahd_outb(ahd, MSG_OUT, HOST_MSG);
3375 ahd_print_devinfo(ahd, &devinfo);
3376 ahd->msgout_buf[0] = MSG_ABORT_TASK;
3377 ahd->msgout_len = 1;
3378 ahd->msgout_index = 0;
3379 ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
3381 ahd_print_path(ahd, scb);
3382 scb->flags |= SCB_ABORT;
3384 printf("Protocol violation %s. Attempting to abort.\n",
3385 ahd_lookup_phase_entry(curphase)->phasemsg);
3390 * Force renegotiation to occur the next time we initiate
3391 * a command to the current device.
3394 ahd_force_renegotiation(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
3396 struct ahd_initiator_tinfo *targ_info;
3397 struct ahd_tmode_tstate *tstate;
3400 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) {
3401 ahd_print_devinfo(ahd, devinfo);
3402 printf("Forcing renegotiation\n");
3405 targ_info = ahd_fetch_transinfo(ahd,
3407 devinfo->our_scsiid,
3410 ahd_update_neg_request(ahd, devinfo, tstate,
3411 targ_info, AHD_NEG_IF_NON_ASYNC);
3414 #define AHD_MAX_STEPS 2000
3416 ahd_clear_critical_section(struct ahd_softc *ahd)
3418 ahd_mode_state saved_modes;
3430 if (ahd->num_critical_sections == 0)
3443 saved_modes = ahd_save_modes(ahd);
3449 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
3450 seqaddr = ahd_inw(ahd, CURADDR);
3452 cs = ahd->critical_sections;
3453 for (i = 0; i < ahd->num_critical_sections; i++, cs++) {
3455 if (cs->begin < seqaddr && cs->end >= seqaddr)
3459 if (i == ahd->num_critical_sections)
3462 if (steps > AHD_MAX_STEPS) {
3463 printf("%s: Infinite loop in critical section\n"
3464 "%s: First Instruction 0x%x now 0x%x\n",
3465 ahd_name(ahd), ahd_name(ahd), first_instr,
3467 ahd_dump_card_state(ahd);
3468 panic("critical section loop");
3473 if ((ahd_debug & AHD_SHOW_MISC) != 0)
3474 printf("%s: Single stepping at 0x%x\n", ahd_name(ahd),
3477 if (stepping == FALSE) {
3479 first_instr = seqaddr;
3480 ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
3481 simode0 = ahd_inb(ahd, SIMODE0);
3482 simode3 = ahd_inb(ahd, SIMODE3);
3483 lqimode0 = ahd_inb(ahd, LQIMODE0);
3484 lqimode1 = ahd_inb(ahd, LQIMODE1);
3485 lqomode0 = ahd_inb(ahd, LQOMODE0);
3486 lqomode1 = ahd_inb(ahd, LQOMODE1);
3487 ahd_outb(ahd, SIMODE0, 0);
3488 ahd_outb(ahd, SIMODE3, 0);
3489 ahd_outb(ahd, LQIMODE0, 0);
3490 ahd_outb(ahd, LQIMODE1, 0);
3491 ahd_outb(ahd, LQOMODE0, 0);
3492 ahd_outb(ahd, LQOMODE1, 0);
3493 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
3494 simode1 = ahd_inb(ahd, SIMODE1);
3496 * We don't clear ENBUSFREE. Unfortunately
3497 * we cannot re-enable busfree detection within
3498 * the current connection, so we must leave it
3499 * on while single stepping.
3501 ahd_outb(ahd, SIMODE1, simode1 & ENBUSFREE);
3502 ahd_outb(ahd, SEQCTL0, ahd_inb(ahd, SEQCTL0) | STEP);
3505 ahd_outb(ahd, CLRSINT1, CLRBUSFREE);
3506 ahd_outb(ahd, CLRINT, CLRSCSIINT);
3507 ahd_set_modes(ahd, ahd->saved_src_mode, ahd->saved_dst_mode);
3508 ahd_outb(ahd, HCNTRL, ahd->unpause);
3509 while (!ahd_is_paused(ahd))
3511 ahd_update_modes(ahd);
3514 ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
3515 ahd_outb(ahd, SIMODE0, simode0);
3516 ahd_outb(ahd, SIMODE3, simode3);
3517 ahd_outb(ahd, LQIMODE0, lqimode0);
3518 ahd_outb(ahd, LQIMODE1, lqimode1);
3519 ahd_outb(ahd, LQOMODE0, lqomode0);
3520 ahd_outb(ahd, LQOMODE1, lqomode1);
3521 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
3522 ahd_outb(ahd, SEQCTL0, ahd_inb(ahd, SEQCTL0) & ~STEP);
3523 ahd_outb(ahd, SIMODE1, simode1);
3525 * SCSIINT seems to glitch occassionally when
3526 * the interrupt masks are restored. Clear SCSIINT
3527 * one more time so that only persistent errors
3528 * are seen as a real interrupt.
3530 ahd_outb(ahd, CLRINT, CLRSCSIINT);
3532 ahd_restore_modes(ahd, saved_modes);
3536 * Clear any pending interrupt status.
3539 ahd_clear_intstat(struct ahd_softc *ahd)
3541 AHD_ASSERT_MODES(ahd, ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK),
3542 ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK));
3543 /* Clear any interrupt conditions this may have caused */
3544 ahd_outb(ahd, CLRLQIINT0, CLRLQIATNQAS|CLRLQICRCT1|CLRLQICRCT2
3545 |CLRLQIBADLQT|CLRLQIATNLQ|CLRLQIATNCMD);
3546 ahd_outb(ahd, CLRLQIINT1, CLRLQIPHASE_LQ|CLRLQIPHASE_NLQ|CLRLIQABORT
3547 |CLRLQICRCI_LQ|CLRLQICRCI_NLQ|CLRLQIBADLQI
3548 |CLRLQIOVERI_LQ|CLRLQIOVERI_NLQ|CLRNONPACKREQ);
3549 ahd_outb(ahd, CLRLQOINT0, CLRLQOTARGSCBPERR|CLRLQOSTOPT2|CLRLQOATNLQ
3550 |CLRLQOATNPKT|CLRLQOTCRC);
3551 ahd_outb(ahd, CLRLQOINT1, CLRLQOINITSCBPERR|CLRLQOSTOPI2|CLRLQOBADQAS
3552 |CLRLQOBUSFREE|CLRLQOPHACHGINPKT);
3553 if ((ahd->bugs & AHD_CLRLQO_AUTOCLR_BUG) != 0) {
3554 ahd_outb(ahd, CLRLQOINT0, 0);
3555 ahd_outb(ahd, CLRLQOINT1, 0);
3557 ahd_outb(ahd, CLRSINT3, CLRNTRAMPERR|CLROSRAMPERR);
3558 ahd_outb(ahd, CLRSINT1, CLRSELTIMEO|CLRATNO|CLRSCSIRSTI
3559 |CLRBUSFREE|CLRSCSIPERR|CLRREQINIT);
3560 ahd_outb(ahd, CLRSINT0, CLRSELDO|CLRSELDI|CLRSELINGO
3561 |CLRIOERR|CLROVERRUN);
3562 ahd_outb(ahd, CLRINT, CLRSCSIINT);
3565 /**************************** Debugging Routines ******************************/
3567 uint32_t ahd_debug = AHD_DEBUG_OPTS;
3572 ahd_print_scb(struct scb *scb)
3574 struct hardware_scb *hscb;
3578 printf("scb:%p control:0x%x scsiid:0x%x lun:%d cdb_len:%d\n",
3584 printf("Shared Data: ");
3585 for (i = 0; i < sizeof(hscb->shared_data.idata.cdb); i++)
3586 printf("%#02x", hscb->shared_data.idata.cdb[i]);
3587 printf(" dataptr:%#x%x datacnt:%#x sgptr:%#x tag:%#x\n",
3588 (uint32_t)((ahd_le64toh(hscb->dataptr) >> 32) & 0xFFFFFFFF),
3589 (uint32_t)(ahd_le64toh(hscb->dataptr) & 0xFFFFFFFF),
3590 ahd_le32toh(hscb->datacnt),
3591 ahd_le32toh(hscb->sgptr),
3593 ahd_dump_sglist(scb);
3597 /************************* Transfer Negotiation *******************************/
3599 * Allocate per target mode instance (ID we respond to as a target)
3600 * transfer negotiation data structures.
3602 static struct ahd_tmode_tstate *
3603 ahd_alloc_tstate(struct ahd_softc *ahd, u_int scsi_id, char channel)
3605 struct ahd_tmode_tstate *master_tstate;
3606 struct ahd_tmode_tstate *tstate;
3609 master_tstate = ahd->enabled_targets[ahd->our_id];
3610 if (ahd->enabled_targets[scsi_id] != NULL
3611 && ahd->enabled_targets[scsi_id] != master_tstate)
3612 panic("%s: ahd_alloc_tstate - Target already allocated",
3614 tstate = malloc(sizeof(*tstate), M_DEVBUF, M_NOWAIT);
3619 * If we have allocated a master tstate, copy user settings from
3620 * the master tstate (taken from SRAM or the EEPROM) for this
3621 * channel, but reset our current and goal settings to async/narrow
3622 * until an initiator talks to us.
3624 if (master_tstate != NULL) {
3625 memcpy(tstate, master_tstate, sizeof(*tstate));
3626 memset(tstate->enabled_luns, 0, sizeof(tstate->enabled_luns));
3627 for (i = 0; i < 16; i++) {
3628 memset(&tstate->transinfo[i].curr, 0,
3629 sizeof(tstate->transinfo[i].curr));
3630 memset(&tstate->transinfo[i].goal, 0,
3631 sizeof(tstate->transinfo[i].goal));
3634 memset(tstate, 0, sizeof(*tstate));
3635 ahd->enabled_targets[scsi_id] = tstate;
3639 #ifdef AHD_TARGET_MODE
3641 * Free per target mode instance (ID we respond to as a target)
3642 * transfer negotiation data structures.
3645 ahd_free_tstate(struct ahd_softc *ahd, u_int scsi_id, char channel, int force)
3647 struct ahd_tmode_tstate *tstate;
3650 * Don't clean up our "master" tstate.
3651 * It has our default user settings.
3653 if (scsi_id == ahd->our_id
3657 tstate = ahd->enabled_targets[scsi_id];
3659 free(tstate, M_DEVBUF);
3660 ahd->enabled_targets[scsi_id] = NULL;
3665 * Called when we have an active connection to a target on the bus,
3666 * this function finds the nearest period to the input period limited
3667 * by the capabilities of the bus connectivity of and sync settings for
3671 ahd_devlimited_syncrate(struct ahd_softc *ahd,
3672 struct ahd_initiator_tinfo *tinfo,
3673 u_int *period, u_int *ppr_options, role_t role)
3675 struct ahd_transinfo *transinfo;
3678 if ((ahd_inb(ahd, SBLKCTL) & ENAB40) != 0
3679 && (ahd_inb(ahd, SSTAT2) & EXP_ACTIVE) == 0) {
3680 maxsync = AHD_SYNCRATE_PACED;
3682 maxsync = AHD_SYNCRATE_ULTRA;
3683 /* Can't do DT related options on an SE bus */
3684 *ppr_options &= MSG_EXT_PPR_QAS_REQ;
3687 * Never allow a value higher than our current goal
3688 * period otherwise we may allow a target initiated
3689 * negotiation to go above the limit as set by the
3690 * user. In the case of an initiator initiated
3691 * sync negotiation, we limit based on the user
3692 * setting. This allows the system to still accept
3693 * incoming negotiations even if target initiated
3694 * negotiation is not performed.
3696 if (role == ROLE_TARGET)
3697 transinfo = &tinfo->user;
3699 transinfo = &tinfo->goal;
3700 *ppr_options &= (transinfo->ppr_options|MSG_EXT_PPR_PCOMP_EN);
3701 if (transinfo->width == MSG_EXT_WDTR_BUS_8_BIT) {
3702 maxsync = max(maxsync, (u_int)AHD_SYNCRATE_ULTRA2);
3703 *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
3705 if (transinfo->period == 0) {
3709 *period = max(*period, (u_int)transinfo->period);
3710 ahd_find_syncrate(ahd, period, ppr_options, maxsync);
3715 * Look up the valid period to SCSIRATE conversion in our table.
3716 * Return the period and offset that should be sent to the target
3717 * if this was the beginning of an SDTR.
3720 ahd_find_syncrate(struct ahd_softc *ahd, u_int *period,
3721 u_int *ppr_options, u_int maxsync)
3723 if (*period < maxsync)
3726 if ((*ppr_options & MSG_EXT_PPR_DT_REQ) != 0
3727 && *period > AHD_SYNCRATE_MIN_DT)
3728 *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
3730 if (*period > AHD_SYNCRATE_MIN)
3733 /* Honor PPR option conformance rules. */
3734 if (*period > AHD_SYNCRATE_PACED)
3735 *ppr_options &= ~MSG_EXT_PPR_RTI;
3737 if ((*ppr_options & MSG_EXT_PPR_IU_REQ) == 0)
3738 *ppr_options &= (MSG_EXT_PPR_DT_REQ|MSG_EXT_PPR_QAS_REQ);
3740 if ((*ppr_options & MSG_EXT_PPR_DT_REQ) == 0)
3741 *ppr_options &= MSG_EXT_PPR_QAS_REQ;
3743 /* Skip all PACED only entries if IU is not available */
3744 if ((*ppr_options & MSG_EXT_PPR_IU_REQ) == 0
3745 && *period < AHD_SYNCRATE_DT)
3746 *period = AHD_SYNCRATE_DT;
3748 /* Skip all DT only entries if DT is not available */
3749 if ((*ppr_options & MSG_EXT_PPR_DT_REQ) == 0
3750 && *period < AHD_SYNCRATE_ULTRA2)
3751 *period = AHD_SYNCRATE_ULTRA2;
3755 * Truncate the given synchronous offset to a value the
3756 * current adapter type and syncrate are capable of.
3759 ahd_validate_offset(struct ahd_softc *ahd,
3760 struct ahd_initiator_tinfo *tinfo,
3761 u_int period, u_int *offset, int wide,
3766 /* Limit offset to what we can do */
3769 else if (period <= AHD_SYNCRATE_PACED) {
3770 if ((ahd->bugs & AHD_PACED_NEGTABLE_BUG) != 0)
3771 maxoffset = MAX_OFFSET_PACED_BUG;
3773 maxoffset = MAX_OFFSET_PACED;
3775 maxoffset = MAX_OFFSET_NON_PACED;
3776 *offset = min(*offset, maxoffset);
3777 if (tinfo != NULL) {
3778 if (role == ROLE_TARGET)
3779 *offset = min(*offset, (u_int)tinfo->user.offset);
3781 *offset = min(*offset, (u_int)tinfo->goal.offset);
3786 * Truncate the given transfer width parameter to a value the
3787 * current adapter type is capable of.
3790 ahd_validate_width(struct ahd_softc *ahd, struct ahd_initiator_tinfo *tinfo,
3791 u_int *bus_width, role_t role)
3793 switch (*bus_width) {
3795 if (ahd->features & AHD_WIDE) {
3797 *bus_width = MSG_EXT_WDTR_BUS_16_BIT;
3801 case MSG_EXT_WDTR_BUS_8_BIT:
3802 *bus_width = MSG_EXT_WDTR_BUS_8_BIT;
3805 if (tinfo != NULL) {
3806 if (role == ROLE_TARGET)
3807 *bus_width = min((u_int)tinfo->user.width, *bus_width);
3809 *bus_width = min((u_int)tinfo->goal.width, *bus_width);
3814 * Update the bitmask of targets for which the controller should
3815 * negotiate with at the next convenient oportunity. This currently
3816 * means the next time we send the initial identify messages for
3817 * a new transaction.
3820 ahd_update_neg_request(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
3821 struct ahd_tmode_tstate *tstate,
3822 struct ahd_initiator_tinfo *tinfo, ahd_neg_type neg_type)
3824 u_int auto_negotiate_orig;
3826 auto_negotiate_orig = tstate->auto_negotiate;
3827 if (neg_type == AHD_NEG_ALWAYS) {
3829 * Force our "current" settings to be
3830 * unknown so that unless a bus reset
3831 * occurs the need to renegotiate is
3832 * recorded persistently.
3834 if ((ahd->features & AHD_WIDE) != 0)
3835 tinfo->curr.width = AHD_WIDTH_UNKNOWN;
3836 tinfo->curr.period = AHD_PERIOD_UNKNOWN;
3837 tinfo->curr.offset = AHD_OFFSET_UNKNOWN;
3839 if (tinfo->curr.period != tinfo->goal.period
3840 || tinfo->curr.width != tinfo->goal.width
3841 || tinfo->curr.offset != tinfo->goal.offset
3842 || tinfo->curr.ppr_options != tinfo->goal.ppr_options
3843 || (neg_type == AHD_NEG_IF_NON_ASYNC
3844 && (tinfo->goal.offset != 0
3845 || tinfo->goal.width != MSG_EXT_WDTR_BUS_8_BIT
3846 || tinfo->goal.ppr_options != 0)))
3847 tstate->auto_negotiate |= devinfo->target_mask;
3849 tstate->auto_negotiate &= ~devinfo->target_mask;
3851 return (auto_negotiate_orig != tstate->auto_negotiate);
3855 * Update the user/goal/curr tables of synchronous negotiation
3856 * parameters as well as, in the case of a current or active update,
3857 * any data structures on the host controller. In the case of an
3858 * active update, the specified target is currently talking to us on
3859 * the bus, so the transfer parameter update must take effect
3863 ahd_set_syncrate(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
3864 u_int period, u_int offset, u_int ppr_options,
3865 u_int type, int paused)
3867 struct ahd_initiator_tinfo *tinfo;
3868 struct ahd_tmode_tstate *tstate;
3875 active = (type & AHD_TRANS_ACTIVE) == AHD_TRANS_ACTIVE;
3878 if (period == 0 || offset == 0) {
3883 tinfo = ahd_fetch_transinfo(ahd, devinfo->channel, devinfo->our_scsiid,
3884 devinfo->target, &tstate);
3886 if ((type & AHD_TRANS_USER) != 0) {
3887 tinfo->user.period = period;
3888 tinfo->user.offset = offset;
3889 tinfo->user.ppr_options = ppr_options;
3892 if ((type & AHD_TRANS_GOAL) != 0) {
3893 tinfo->goal.period = period;
3894 tinfo->goal.offset = offset;
3895 tinfo->goal.ppr_options = ppr_options;
3898 old_period = tinfo->curr.period;
3899 old_offset = tinfo->curr.offset;
3900 old_ppr = tinfo->curr.ppr_options;
3902 if ((type & AHD_TRANS_CUR) != 0
3903 && (old_period != period
3904 || old_offset != offset
3905 || old_ppr != ppr_options)) {
3909 tinfo->curr.period = period;
3910 tinfo->curr.offset = offset;
3911 tinfo->curr.ppr_options = ppr_options;
3913 ahd_send_async(ahd, devinfo->channel, devinfo->target,
3914 CAM_LUN_WILDCARD, AC_TRANSFER_NEG);
3919 printf("%s: target %d synchronous with "
3920 "period = 0x%x, offset = 0x%x",
3921 ahd_name(ahd), devinfo->target,
3924 if ((ppr_options & MSG_EXT_PPR_RD_STRM) != 0) {
3928 if ((ppr_options & MSG_EXT_PPR_DT_REQ) != 0) {
3929 printf("%s", options ? "|DT" : "(DT");
3932 if ((ppr_options & MSG_EXT_PPR_IU_REQ) != 0) {
3933 printf("%s", options ? "|IU" : "(IU");
3936 if ((ppr_options & MSG_EXT_PPR_RTI) != 0) {
3937 printf("%s", options ? "|RTI" : "(RTI");
3940 if ((ppr_options & MSG_EXT_PPR_QAS_REQ) != 0) {
3941 printf("%s", options ? "|QAS" : "(QAS");
3949 printf("%s: target %d using "
3950 "asynchronous transfers%s\n",
3951 ahd_name(ahd), devinfo->target,
3952 (ppr_options & MSG_EXT_PPR_QAS_REQ) != 0
3958 * Always refresh the neg-table to handle the case of the
3959 * sequencer setting the ENATNO bit for a MK_MESSAGE request.
3960 * We will always renegotiate in that case if this is a
3961 * packetized request. Also manage the busfree expected flag
3962 * from this common routine so that we catch changes due to
3963 * WDTR or SDTR messages.
3965 if ((type & AHD_TRANS_CUR) != 0) {
3968 ahd_update_neg_table(ahd, devinfo, &tinfo->curr);
3971 if (ahd->msg_type != MSG_TYPE_NONE) {
3972 if ((old_ppr & MSG_EXT_PPR_IU_REQ)
3973 != (ppr_options & MSG_EXT_PPR_IU_REQ)) {
3975 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) {
3976 ahd_print_devinfo(ahd, devinfo);
3977 printf("Expecting IU Change busfree\n");
3980 ahd->msg_flags |= MSG_FLAG_EXPECT_PPR_BUSFREE
3981 | MSG_FLAG_IU_REQ_CHANGED;
3983 if ((old_ppr & MSG_EXT_PPR_IU_REQ) != 0) {
3985 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
3986 printf("PPR with IU_REQ outstanding\n");
3988 ahd->msg_flags |= MSG_FLAG_EXPECT_PPR_BUSFREE;
3993 update_needed += ahd_update_neg_request(ahd, devinfo, tstate,
3994 tinfo, AHD_NEG_TO_GOAL);
3996 if (update_needed && active)
3997 ahd_update_pending_scbs(ahd);
4001 * Update the user/goal/curr tables of wide negotiation
4002 * parameters as well as, in the case of a current or active update,
4003 * any data structures on the host controller. In the case of an
4004 * active update, the specified target is currently talking to us on
4005 * the bus, so the transfer parameter update must take effect
4009 ahd_set_width(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
4010 u_int width, u_int type, int paused)
4012 struct ahd_initiator_tinfo *tinfo;
4013 struct ahd_tmode_tstate *tstate;
4018 active = (type & AHD_TRANS_ACTIVE) == AHD_TRANS_ACTIVE;
4020 tinfo = ahd_fetch_transinfo(ahd, devinfo->channel, devinfo->our_scsiid,
4021 devinfo->target, &tstate);
4023 if ((type & AHD_TRANS_USER) != 0)
4024 tinfo->user.width = width;
4026 if ((type & AHD_TRANS_GOAL) != 0)
4027 tinfo->goal.width = width;
4029 oldwidth = tinfo->curr.width;
4030 if ((type & AHD_TRANS_CUR) != 0 && oldwidth != width) {
4034 tinfo->curr.width = width;
4035 ahd_send_async(ahd, devinfo->channel, devinfo->target,
4036 CAM_LUN_WILDCARD, AC_TRANSFER_NEG);
4038 printf("%s: target %d using %dbit transfers\n",
4039 ahd_name(ahd), devinfo->target,
4040 8 * (0x01 << width));
4044 if ((type & AHD_TRANS_CUR) != 0) {
4047 ahd_update_neg_table(ahd, devinfo, &tinfo->curr);
4052 update_needed += ahd_update_neg_request(ahd, devinfo, tstate,
4053 tinfo, AHD_NEG_TO_GOAL);
4054 if (update_needed && active)
4055 ahd_update_pending_scbs(ahd);
4060 * Update the current state of tagged queuing for a given target.
4063 ahd_set_tags(struct ahd_softc *ahd, struct scsi_cmnd *cmd,
4064 struct ahd_devinfo *devinfo, ahd_queue_alg alg)
4066 struct scsi_device *sdev = cmd->device;
4068 ahd_platform_set_tags(ahd, sdev, devinfo, alg);
4069 ahd_send_async(ahd, devinfo->channel, devinfo->target,
4070 devinfo->lun, AC_TRANSFER_NEG);
4074 ahd_update_neg_table(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
4075 struct ahd_transinfo *tinfo)
4077 ahd_mode_state saved_modes;
4082 u_int saved_negoaddr;
4083 uint8_t iocell_opts[sizeof(ahd->iocell_opts)];
4085 saved_modes = ahd_save_modes(ahd);
4086 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
4088 saved_negoaddr = ahd_inb(ahd, NEGOADDR);
4089 ahd_outb(ahd, NEGOADDR, devinfo->target);
4090 period = tinfo->period;
4091 offset = tinfo->offset;
4092 memcpy(iocell_opts, ahd->iocell_opts, sizeof(ahd->iocell_opts));
4093 ppr_opts = tinfo->ppr_options & (MSG_EXT_PPR_QAS_REQ|MSG_EXT_PPR_DT_REQ
4094 |MSG_EXT_PPR_IU_REQ|MSG_EXT_PPR_RTI);
4097 period = AHD_SYNCRATE_ASYNC;
4098 if (period == AHD_SYNCRATE_160) {
4100 if ((ahd->bugs & AHD_PACED_NEGTABLE_BUG) != 0) {
4102 * When the SPI4 spec was finalized, PACE transfers
4103 * was not made a configurable option in the PPR
4104 * message. Instead it is assumed to be enabled for
4105 * any syncrate faster than 80MHz. Nevertheless,
4106 * Harpoon2A4 allows this to be configurable.
4108 * Harpoon2A4 also assumes at most 2 data bytes per
4109 * negotiated REQ/ACK offset. Paced transfers take
4110 * 4, so we must adjust our offset.
4112 ppr_opts |= PPROPT_PACE;
4116 * Harpoon2A assumed that there would be a
4117 * fallback rate between 160MHz and 80Mhz,
4118 * so 7 is used as the period factor rather
4119 * than 8 for 160MHz.
4121 period = AHD_SYNCRATE_REVA_160;
4123 if ((tinfo->ppr_options & MSG_EXT_PPR_PCOMP_EN) == 0)
4124 iocell_opts[AHD_PRECOMP_SLEW_INDEX] &=
4128 * Precomp should be disabled for non-paced transfers.
4130 iocell_opts[AHD_PRECOMP_SLEW_INDEX] &= ~AHD_PRECOMP_MASK;
4132 if ((ahd->features & AHD_NEW_IOCELL_OPTS) != 0
4133 && (ppr_opts & MSG_EXT_PPR_DT_REQ) != 0
4134 && (ppr_opts & MSG_EXT_PPR_IU_REQ) == 0) {
4136 * Slow down our CRC interval to be
4137 * compatible with non-packetized
4138 * U160 devices that can't handle a
4139 * CRC at full speed.
4141 con_opts |= ENSLOWCRC;
4144 if ((ahd->bugs & AHD_PACED_NEGTABLE_BUG) != 0) {
4146 * On H2A4, revert to a slower slewrate
4147 * on non-paced transfers.
4149 iocell_opts[AHD_PRECOMP_SLEW_INDEX] &=
4154 ahd_outb(ahd, ANNEXCOL, AHD_ANNEXCOL_PRECOMP_SLEW);
4155 ahd_outb(ahd, ANNEXDAT, iocell_opts[AHD_PRECOMP_SLEW_INDEX]);
4156 ahd_outb(ahd, ANNEXCOL, AHD_ANNEXCOL_AMPLITUDE);
4157 ahd_outb(ahd, ANNEXDAT, iocell_opts[AHD_AMPLITUDE_INDEX]);
4159 ahd_outb(ahd, NEGPERIOD, period);
4160 ahd_outb(ahd, NEGPPROPTS, ppr_opts);
4161 ahd_outb(ahd, NEGOFFSET, offset);
4163 if (tinfo->width == MSG_EXT_WDTR_BUS_16_BIT)
4164 con_opts |= WIDEXFER;
4167 * Slow down our CRC interval to be
4168 * compatible with packetized U320 devices
4169 * that can't handle a CRC at full speed
4171 if (ahd->features & AHD_AIC79XXB_SLOWCRC) {
4172 con_opts |= ENSLOWCRC;
4176 * During packetized transfers, the target will
4177 * give us the oportunity to send command packets
4178 * without us asserting attention.
4180 if ((tinfo->ppr_options & MSG_EXT_PPR_IU_REQ) == 0)
4181 con_opts |= ENAUTOATNO;
4182 ahd_outb(ahd, NEGCONOPTS, con_opts);
4183 ahd_outb(ahd, NEGOADDR, saved_negoaddr);
4184 ahd_restore_modes(ahd, saved_modes);
4188 * When the transfer settings for a connection change, setup for
4189 * negotiation in pending SCBs to effect the change as quickly as
4190 * possible. We also cancel any negotiations that are scheduled
4191 * for inflight SCBs that have not been started yet.
4194 ahd_update_pending_scbs(struct ahd_softc *ahd)
4196 struct scb *pending_scb;
4197 int pending_scb_count;
4200 ahd_mode_state saved_modes;
4203 * Traverse the pending SCB list and ensure that all of the
4204 * SCBs there have the proper settings. We can only safely
4205 * clear the negotiation required flag (setting requires the
4206 * execution queue to be modified) and this is only possible
4207 * if we are not already attempting to select out for this
4208 * SCB. For this reason, all callers only call this routine
4209 * if we are changing the negotiation settings for the currently
4210 * active transaction on the bus.
4212 pending_scb_count = 0;
4213 LIST_FOREACH(pending_scb, &ahd->pending_scbs, pending_links) {
4214 struct ahd_devinfo devinfo;
4215 struct ahd_initiator_tinfo *tinfo;
4216 struct ahd_tmode_tstate *tstate;
4218 ahd_scb_devinfo(ahd, &devinfo, pending_scb);
4219 tinfo = ahd_fetch_transinfo(ahd, devinfo.channel,
4221 devinfo.target, &tstate);
4222 if ((tstate->auto_negotiate & devinfo.target_mask) == 0
4223 && (pending_scb->flags & SCB_AUTO_NEGOTIATE) != 0) {
4224 pending_scb->flags &= ~SCB_AUTO_NEGOTIATE;
4225 pending_scb->hscb->control &= ~MK_MESSAGE;
4227 ahd_sync_scb(ahd, pending_scb,
4228 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
4229 pending_scb_count++;
4232 if (pending_scb_count == 0)
4235 if (ahd_is_paused(ahd)) {
4243 * Force the sequencer to reinitialize the selection for
4244 * the command at the head of the execution queue if it
4245 * has already been setup. The negotiation changes may
4246 * effect whether we select-out with ATN. It is only
4247 * safe to clear ENSELO when the bus is not free and no
4248 * selection is in progres or completed.
4250 saved_modes = ahd_save_modes(ahd);
4251 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
4252 if ((ahd_inb(ahd, SCSISIGI) & BSYI) != 0
4253 && (ahd_inb(ahd, SSTAT0) & (SELDO|SELINGO)) == 0)
4254 ahd_outb(ahd, SCSISEQ0, ahd_inb(ahd, SCSISEQ0) & ~ENSELO);
4255 saved_scbptr = ahd_get_scbptr(ahd);
4256 /* Ensure that the hscbs down on the card match the new information */
4257 LIST_FOREACH(pending_scb, &ahd->pending_scbs, pending_links) {
4261 scb_tag = SCB_GET_TAG(pending_scb);
4262 ahd_set_scbptr(ahd, scb_tag);
4263 control = ahd_inb_scbram(ahd, SCB_CONTROL);
4264 control &= ~MK_MESSAGE;
4265 control |= pending_scb->hscb->control & MK_MESSAGE;
4266 ahd_outb(ahd, SCB_CONTROL, control);
4268 ahd_set_scbptr(ahd, saved_scbptr);
4269 ahd_restore_modes(ahd, saved_modes);
4275 /**************************** Pathing Information *****************************/
4277 ahd_fetch_devinfo(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
4279 ahd_mode_state saved_modes;
4284 saved_modes = ahd_save_modes(ahd);
4285 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
4287 if (ahd_inb(ahd, SSTAT0) & TARGET)
4290 role = ROLE_INITIATOR;
4292 if (role == ROLE_TARGET
4293 && (ahd_inb(ahd, SEQ_FLAGS) & CMDPHASE_PENDING) != 0) {
4294 /* We were selected, so pull our id from TARGIDIN */
4295 our_id = ahd_inb(ahd, TARGIDIN) & OID;
4296 } else if (role == ROLE_TARGET)
4297 our_id = ahd_inb(ahd, TOWNID);
4299 our_id = ahd_inb(ahd, IOWNID);
4301 saved_scsiid = ahd_inb(ahd, SAVED_SCSIID);
4302 ahd_compile_devinfo(devinfo,
4304 SCSIID_TARGET(ahd, saved_scsiid),
4305 ahd_inb(ahd, SAVED_LUN),
4306 SCSIID_CHANNEL(ahd, saved_scsiid),
4308 ahd_restore_modes(ahd, saved_modes);
4312 ahd_print_devinfo(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
4314 printf("%s:%c:%d:%d: ", ahd_name(ahd), 'A',
4315 devinfo->target, devinfo->lun);
4318 static struct ahd_phase_table_entry*
4319 ahd_lookup_phase_entry(int phase)
4321 struct ahd_phase_table_entry *entry;
4322 struct ahd_phase_table_entry *last_entry;
4325 * num_phases doesn't include the default entry which
4326 * will be returned if the phase doesn't match.
4328 last_entry = &ahd_phase_table[num_phases];
4329 for (entry = ahd_phase_table; entry < last_entry; entry++) {
4330 if (phase == entry->phase)
4337 ahd_compile_devinfo(struct ahd_devinfo *devinfo, u_int our_id, u_int target,
4338 u_int lun, char channel, role_t role)
4340 devinfo->our_scsiid = our_id;
4341 devinfo->target = target;
4343 devinfo->target_offset = target;
4344 devinfo->channel = channel;
4345 devinfo->role = role;
4347 devinfo->target_offset += 8;
4348 devinfo->target_mask = (0x01 << devinfo->target_offset);
4352 ahd_scb_devinfo(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
4358 our_id = SCSIID_OUR_ID(scb->hscb->scsiid);
4359 role = ROLE_INITIATOR;
4360 if ((scb->hscb->control & TARGET_SCB) != 0)
4362 ahd_compile_devinfo(devinfo, our_id, SCB_GET_TARGET(ahd, scb),
4363 SCB_GET_LUN(scb), SCB_GET_CHANNEL(ahd, scb), role);
4367 /************************ Message Phase Processing ****************************/
4369 * When an initiator transaction with the MK_MESSAGE flag either reconnects
4370 * or enters the initial message out phase, we are interrupted. Fill our
4371 * outgoing message buffer with the appropriate message and beging handing
4372 * the message phase(s) manually.
4375 ahd_setup_initiator_msgout(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
4379 * To facilitate adding multiple messages together,
4380 * each routine should increment the index and len
4381 * variables instead of setting them explicitly.
4383 ahd->msgout_index = 0;
4384 ahd->msgout_len = 0;
4386 if (ahd_currently_packetized(ahd))
4387 ahd->msg_flags |= MSG_FLAG_PACKETIZED;
4389 if (ahd->send_msg_perror
4390 && ahd_inb(ahd, MSG_OUT) == HOST_MSG) {
4391 ahd->msgout_buf[ahd->msgout_index++] = ahd->send_msg_perror;
4393 ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
4395 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
4396 printf("Setting up for Parity Error delivery\n");
4399 } else if (scb == NULL) {
4400 printf("%s: WARNING. No pending message for "
4401 "I_T msgin. Issuing NO-OP\n", ahd_name(ahd));
4402 ahd->msgout_buf[ahd->msgout_index++] = MSG_NOOP;
4404 ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
4408 if ((scb->flags & SCB_DEVICE_RESET) == 0
4409 && (scb->flags & SCB_PACKETIZED) == 0
4410 && ahd_inb(ahd, MSG_OUT) == MSG_IDENTIFYFLAG) {
4413 identify_msg = MSG_IDENTIFYFLAG | SCB_GET_LUN(scb);
4414 if ((scb->hscb->control & DISCENB) != 0)
4415 identify_msg |= MSG_IDENTIFY_DISCFLAG;
4416 ahd->msgout_buf[ahd->msgout_index++] = identify_msg;
4419 if ((scb->hscb->control & TAG_ENB) != 0) {
4420 ahd->msgout_buf[ahd->msgout_index++] =
4421 scb->hscb->control & (TAG_ENB|SCB_TAG_TYPE);
4422 ahd->msgout_buf[ahd->msgout_index++] = SCB_GET_TAG(scb);
4423 ahd->msgout_len += 2;
4427 if (scb->flags & SCB_DEVICE_RESET) {
4428 ahd->msgout_buf[ahd->msgout_index++] = MSG_BUS_DEV_RESET;
4430 ahd_print_path(ahd, scb);
4431 printf("Bus Device Reset Message Sent\n");
4433 * Clear our selection hardware in advance of
4434 * the busfree. We may have an entry in the waiting
4435 * Q for this target, and we don't want to go about
4436 * selecting while we handle the busfree and blow it
4439 ahd_outb(ahd, SCSISEQ0, 0);
4440 } else if ((scb->flags & SCB_ABORT) != 0) {
4442 if ((scb->hscb->control & TAG_ENB) != 0) {
4443 ahd->msgout_buf[ahd->msgout_index++] = MSG_ABORT_TAG;
4445 ahd->msgout_buf[ahd->msgout_index++] = MSG_ABORT;
4448 ahd_print_path(ahd, scb);
4449 printf("Abort%s Message Sent\n",
4450 (scb->hscb->control & TAG_ENB) != 0 ? " Tag" : "");
4452 * Clear our selection hardware in advance of
4453 * the busfree. We may have an entry in the waiting
4454 * Q for this target, and we don't want to go about
4455 * selecting while we handle the busfree and blow it
4458 ahd_outb(ahd, SCSISEQ0, 0);
4459 } else if ((scb->flags & (SCB_AUTO_NEGOTIATE|SCB_NEGOTIATE)) != 0) {
4460 ahd_build_transfer_msg(ahd, devinfo);
4462 * Clear our selection hardware in advance of potential
4463 * PPR IU status change busfree. We may have an entry in
4464 * the waiting Q for this target, and we don't want to go
4465 * about selecting while we handle the busfree and blow
4468 ahd_outb(ahd, SCSISEQ0, 0);
4470 printf("ahd_intr: AWAITING_MSG for an SCB that "
4471 "does not have a waiting message\n");
4472 printf("SCSIID = %x, target_mask = %x\n", scb->hscb->scsiid,
4473 devinfo->target_mask);
4474 panic("SCB = %d, SCB Control = %x:%x, MSG_OUT = %x "
4475 "SCB flags = %x", SCB_GET_TAG(scb), scb->hscb->control,
4476 ahd_inb_scbram(ahd, SCB_CONTROL), ahd_inb(ahd, MSG_OUT),
4481 * Clear the MK_MESSAGE flag from the SCB so we aren't
4482 * asked to send this message again.
4484 ahd_outb(ahd, SCB_CONTROL,
4485 ahd_inb_scbram(ahd, SCB_CONTROL) & ~MK_MESSAGE);
4486 scb->hscb->control &= ~MK_MESSAGE;
4487 ahd->msgout_index = 0;
4488 ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
4492 * Build an appropriate transfer negotiation message for the
4493 * currently active target.
4496 ahd_build_transfer_msg(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
4499 * We need to initiate transfer negotiations.
4500 * If our current and goal settings are identical,
4501 * we want to renegotiate due to a check condition.
4503 struct ahd_initiator_tinfo *tinfo;
4504 struct ahd_tmode_tstate *tstate;
4512 tinfo = ahd_fetch_transinfo(ahd, devinfo->channel, devinfo->our_scsiid,
4513 devinfo->target, &tstate);
4515 * Filter our period based on the current connection.
4516 * If we can't perform DT transfers on this segment (not in LVD
4517 * mode for instance), then our decision to issue a PPR message
4520 period = tinfo->goal.period;
4521 offset = tinfo->goal.offset;
4522 ppr_options = tinfo->goal.ppr_options;
4523 /* Target initiated PPR is not allowed in the SCSI spec */
4524 if (devinfo->role == ROLE_TARGET)
4526 ahd_devlimited_syncrate(ahd, tinfo, &period,
4527 &ppr_options, devinfo->role);
4528 dowide = tinfo->curr.width != tinfo->goal.width;
4529 dosync = tinfo->curr.offset != offset || tinfo->curr.period != period;
4531 * Only use PPR if we have options that need it, even if the device
4532 * claims to support it. There might be an expander in the way
4535 doppr = ppr_options != 0;
4537 if (!dowide && !dosync && !doppr) {
4538 dowide = tinfo->goal.width != MSG_EXT_WDTR_BUS_8_BIT;
4539 dosync = tinfo->goal.offset != 0;
4542 if (!dowide && !dosync && !doppr) {
4544 * Force async with a WDTR message if we have a wide bus,
4545 * or just issue an SDTR with a 0 offset.
4547 if ((ahd->features & AHD_WIDE) != 0)
4553 ahd_print_devinfo(ahd, devinfo);
4554 printf("Ensuring async\n");
4557 /* Target initiated PPR is not allowed in the SCSI spec */
4558 if (devinfo->role == ROLE_TARGET)
4562 * Both the PPR message and SDTR message require the
4563 * goal syncrate to be limited to what the target device
4564 * is capable of handling (based on whether an LVD->SE
4565 * expander is on the bus), so combine these two cases.
4566 * Regardless, guarantee that if we are using WDTR and SDTR
4567 * messages that WDTR comes first.
4569 if (doppr || (dosync && !dowide)) {
4571 offset = tinfo->goal.offset;
4572 ahd_validate_offset(ahd, tinfo, period, &offset,
4573 doppr ? tinfo->goal.width
4574 : tinfo->curr.width,
4577 ahd_construct_ppr(ahd, devinfo, period, offset,
4578 tinfo->goal.width, ppr_options);
4580 ahd_construct_sdtr(ahd, devinfo, period, offset);
4583 ahd_construct_wdtr(ahd, devinfo, tinfo->goal.width);
4588 * Build a synchronous negotiation message in our message
4589 * buffer based on the input parameters.
4592 ahd_construct_sdtr(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
4593 u_int period, u_int offset)
4596 period = AHD_ASYNC_XFER_PERIOD;
4597 ahd->msgout_index += spi_populate_sync_msg(
4598 ahd->msgout_buf + ahd->msgout_index, period, offset);
4599 ahd->msgout_len += 5;
4601 printf("(%s:%c:%d:%d): Sending SDTR period %x, offset %x\n",
4602 ahd_name(ahd), devinfo->channel, devinfo->target,
4603 devinfo->lun, period, offset);
4608 * Build a wide negotiateion message in our message
4609 * buffer based on the input parameters.
4612 ahd_construct_wdtr(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
4615 ahd->msgout_index += spi_populate_width_msg(
4616 ahd->msgout_buf + ahd->msgout_index, bus_width);
4617 ahd->msgout_len += 4;
4619 printf("(%s:%c:%d:%d): Sending WDTR %x\n",
4620 ahd_name(ahd), devinfo->channel, devinfo->target,
4621 devinfo->lun, bus_width);
4626 * Build a parallel protocol request message in our message
4627 * buffer based on the input parameters.
4630 ahd_construct_ppr(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
4631 u_int period, u_int offset, u_int bus_width,
4635 * Always request precompensation from
4636 * the other target if we are running
4637 * at paced syncrates.
4639 if (period <= AHD_SYNCRATE_PACED)
4640 ppr_options |= MSG_EXT_PPR_PCOMP_EN;
4642 period = AHD_ASYNC_XFER_PERIOD;
4643 ahd->msgout_index += spi_populate_ppr_msg(
4644 ahd->msgout_buf + ahd->msgout_index, period, offset,
4645 bus_width, ppr_options);
4646 ahd->msgout_len += 8;
4648 printf("(%s:%c:%d:%d): Sending PPR bus_width %x, period %x, "
4649 "offset %x, ppr_options %x\n", ahd_name(ahd),
4650 devinfo->channel, devinfo->target, devinfo->lun,
4651 bus_width, period, offset, ppr_options);
4656 * Clear any active message state.
4659 ahd_clear_msg_state(struct ahd_softc *ahd)
4661 ahd_mode_state saved_modes;
4663 saved_modes = ahd_save_modes(ahd);
4664 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
4665 ahd->send_msg_perror = 0;
4666 ahd->msg_flags = MSG_FLAG_NONE;
4667 ahd->msgout_len = 0;
4668 ahd->msgin_index = 0;
4669 ahd->msg_type = MSG_TYPE_NONE;
4670 if ((ahd_inb(ahd, SCSISIGO) & ATNO) != 0) {
4672 * The target didn't care to respond to our
4673 * message request, so clear ATN.
4675 ahd_outb(ahd, CLRSINT1, CLRATNO);
4677 ahd_outb(ahd, MSG_OUT, MSG_NOOP);
4678 ahd_outb(ahd, SEQ_FLAGS2,
4679 ahd_inb(ahd, SEQ_FLAGS2) & ~TARGET_MSG_PENDING);
4680 ahd_restore_modes(ahd, saved_modes);
4684 * Manual message loop handler.
4687 ahd_handle_message_phase(struct ahd_softc *ahd)
4689 struct ahd_devinfo devinfo;
4693 ahd_fetch_devinfo(ahd, &devinfo);
4694 end_session = FALSE;
4695 bus_phase = ahd_inb(ahd, LASTPHASE);
4697 if ((ahd_inb(ahd, LQISTAT2) & LQIPHASE_OUTPKT) != 0) {
4698 printf("LQIRETRY for LQIPHASE_OUTPKT\n");
4699 ahd_outb(ahd, LQCTL2, LQIRETRY);
4702 switch (ahd->msg_type) {
4703 case MSG_TYPE_INITIATOR_MSGOUT:
4709 if (ahd->msgout_len == 0 && ahd->send_msg_perror == 0)
4710 panic("HOST_MSG_LOOP interrupt with no active message");
4713 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) {
4714 ahd_print_devinfo(ahd, &devinfo);
4715 printf("INITIATOR_MSG_OUT");
4718 phasemis = bus_phase != P_MESGOUT;
4721 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) {
4722 printf(" PHASEMIS %s\n",
4723 ahd_lookup_phase_entry(bus_phase)
4727 if (bus_phase == P_MESGIN) {
4729 * Change gears and see if
4730 * this messages is of interest to
4731 * us or should be passed back to
4734 ahd_outb(ahd, CLRSINT1, CLRATNO);
4735 ahd->send_msg_perror = 0;
4736 ahd->msg_type = MSG_TYPE_INITIATOR_MSGIN;
4737 ahd->msgin_index = 0;
4744 if (ahd->send_msg_perror) {
4745 ahd_outb(ahd, CLRSINT1, CLRATNO);
4746 ahd_outb(ahd, CLRSINT1, CLRREQINIT);
4748 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
4749 printf(" byte 0x%x\n", ahd->send_msg_perror);
4752 * If we are notifying the target of a CRC error
4753 * during packetized operations, the target is
4754 * within its rights to acknowledge our message
4757 if ((ahd->msg_flags & MSG_FLAG_PACKETIZED) != 0
4758 && ahd->send_msg_perror == MSG_INITIATOR_DET_ERR)
4759 ahd->msg_flags |= MSG_FLAG_EXPECT_IDE_BUSFREE;
4761 ahd_outb(ahd, RETURN_2, ahd->send_msg_perror);
4762 ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_WRITE);
4766 msgdone = ahd->msgout_index == ahd->msgout_len;
4769 * The target has requested a retry.
4770 * Re-assert ATN, reset our message index to
4773 ahd->msgout_index = 0;
4774 ahd_assert_atn(ahd);
4777 lastbyte = ahd->msgout_index == (ahd->msgout_len - 1);
4779 /* Last byte is signified by dropping ATN */
4780 ahd_outb(ahd, CLRSINT1, CLRATNO);
4784 * Clear our interrupt status and present
4785 * the next byte on the bus.
4787 ahd_outb(ahd, CLRSINT1, CLRREQINIT);
4789 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
4790 printf(" byte 0x%x\n",
4791 ahd->msgout_buf[ahd->msgout_index]);
4793 ahd_outb(ahd, RETURN_2, ahd->msgout_buf[ahd->msgout_index++]);
4794 ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_WRITE);
4797 case MSG_TYPE_INITIATOR_MSGIN:
4803 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) {
4804 ahd_print_devinfo(ahd, &devinfo);
4805 printf("INITIATOR_MSG_IN");
4808 phasemis = bus_phase != P_MESGIN;
4811 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) {
4812 printf(" PHASEMIS %s\n",
4813 ahd_lookup_phase_entry(bus_phase)
4817 ahd->msgin_index = 0;
4818 if (bus_phase == P_MESGOUT
4819 && (ahd->send_msg_perror != 0
4820 || (ahd->msgout_len != 0
4821 && ahd->msgout_index == 0))) {
4822 ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
4829 /* Pull the byte in without acking it */
4830 ahd->msgin_buf[ahd->msgin_index] = ahd_inb(ahd, SCSIBUS);
4832 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
4833 printf(" byte 0x%x\n",
4834 ahd->msgin_buf[ahd->msgin_index]);
4837 message_done = ahd_parse_msg(ahd, &devinfo);
4841 * Clear our incoming message buffer in case there
4842 * is another message following this one.
4844 ahd->msgin_index = 0;
4847 * If this message illicited a response,
4848 * assert ATN so the target takes us to the
4849 * message out phase.
4851 if (ahd->msgout_len != 0) {
4853 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) {
4854 ahd_print_devinfo(ahd, &devinfo);
4855 printf("Asserting ATN for response\n");
4858 ahd_assert_atn(ahd);
4863 if (message_done == MSGLOOP_TERMINATED) {
4867 ahd_outb(ahd, CLRSINT1, CLRREQINIT);
4868 ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_READ);
4872 case MSG_TYPE_TARGET_MSGIN:
4878 * By default, the message loop will continue.
4880 ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_TARG);
4882 if (ahd->msgout_len == 0)
4883 panic("Target MSGIN with no active message");
4886 * If we interrupted a mesgout session, the initiator
4887 * will not know this until our first REQ. So, we
4888 * only honor mesgout requests after we've sent our
4891 if ((ahd_inb(ahd, SCSISIGI) & ATNI) != 0
4892 && ahd->msgout_index > 0)
4893 msgout_request = TRUE;
4895 msgout_request = FALSE;
4897 if (msgout_request) {
4900 * Change gears and see if
4901 * this messages is of interest to
4902 * us or should be passed back to
4905 ahd->msg_type = MSG_TYPE_TARGET_MSGOUT;
4906 ahd_outb(ahd, SCSISIGO, P_MESGOUT | BSYO);
4907 ahd->msgin_index = 0;
4908 /* Dummy read to REQ for first byte */
4909 ahd_inb(ahd, SCSIDAT);
4910 ahd_outb(ahd, SXFRCTL0,
4911 ahd_inb(ahd, SXFRCTL0) | SPIOEN);
4915 msgdone = ahd->msgout_index == ahd->msgout_len;
4917 ahd_outb(ahd, SXFRCTL0,
4918 ahd_inb(ahd, SXFRCTL0) & ~SPIOEN);
4924 * Present the next byte on the bus.
4926 ahd_outb(ahd, SXFRCTL0, ahd_inb(ahd, SXFRCTL0) | SPIOEN);
4927 ahd_outb(ahd, SCSIDAT, ahd->msgout_buf[ahd->msgout_index++]);
4930 case MSG_TYPE_TARGET_MSGOUT:
4936 * By default, the message loop will continue.
4938 ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_TARG);
4941 * The initiator signals that this is
4942 * the last byte by dropping ATN.
4944 lastbyte = (ahd_inb(ahd, SCSISIGI) & ATNI) == 0;
4947 * Read the latched byte, but turn off SPIOEN first
4948 * so that we don't inadvertently cause a REQ for the
4951 ahd_outb(ahd, SXFRCTL0, ahd_inb(ahd, SXFRCTL0) & ~SPIOEN);
4952 ahd->msgin_buf[ahd->msgin_index] = ahd_inb(ahd, SCSIDAT);
4953 msgdone = ahd_parse_msg(ahd, &devinfo);
4954 if (msgdone == MSGLOOP_TERMINATED) {
4956 * The message is *really* done in that it caused
4957 * us to go to bus free. The sequencer has already
4958 * been reset at this point, so pull the ejection
4967 * XXX Read spec about initiator dropping ATN too soon
4968 * and use msgdone to detect it.
4970 if (msgdone == MSGLOOP_MSGCOMPLETE) {
4971 ahd->msgin_index = 0;
4974 * If this message illicited a response, transition
4975 * to the Message in phase and send it.
4977 if (ahd->msgout_len != 0) {
4978 ahd_outb(ahd, SCSISIGO, P_MESGIN | BSYO);
4979 ahd_outb(ahd, SXFRCTL0,
4980 ahd_inb(ahd, SXFRCTL0) | SPIOEN);
4981 ahd->msg_type = MSG_TYPE_TARGET_MSGIN;
4982 ahd->msgin_index = 0;
4990 /* Ask for the next byte. */
4991 ahd_outb(ahd, SXFRCTL0,
4992 ahd_inb(ahd, SXFRCTL0) | SPIOEN);
4998 panic("Unknown REQINIT message type");
5002 if ((ahd->msg_flags & MSG_FLAG_PACKETIZED) != 0) {
5003 printf("%s: Returning to Idle Loop\n",
5005 ahd_clear_msg_state(ahd);
5008 * Perform the equivalent of a clear_target_state.
5010 ahd_outb(ahd, LASTPHASE, P_BUSFREE);
5011 ahd_outb(ahd, SEQ_FLAGS, NOT_IDENTIFIED|NO_CDB_SENT);
5012 ahd_outb(ahd, SEQCTL0, FASTMODE|SEQRESET);
5014 ahd_clear_msg_state(ahd);
5015 ahd_outb(ahd, RETURN_1, EXIT_MSG_LOOP);
5021 * See if we sent a particular extended message to the target.
5022 * If "full" is true, return true only if the target saw the full
5023 * message. If "full" is false, return true if the target saw at
5024 * least the first byte of the message.
5027 ahd_sent_msg(struct ahd_softc *ahd, ahd_msgtype type, u_int msgval, int full)
5035 while (index < ahd->msgout_len) {
5036 if (ahd->msgout_buf[index] == MSG_EXTENDED) {
5039 end_index = index + 1 + ahd->msgout_buf[index + 1];
5040 if (ahd->msgout_buf[index+2] == msgval
5041 && type == AHDMSG_EXT) {
5044 if (ahd->msgout_index > end_index)
5046 } else if (ahd->msgout_index > index)
5050 } else if (ahd->msgout_buf[index] >= MSG_SIMPLE_TASK
5051 && ahd->msgout_buf[index] <= MSG_IGN_WIDE_RESIDUE) {
5053 /* Skip tag type and tag id or residue param*/
5056 /* Single byte message */
5057 if (type == AHDMSG_1B
5058 && ahd->msgout_index > index
5059 && (ahd->msgout_buf[index] == msgval
5060 || ((ahd->msgout_buf[index] & MSG_IDENTIFYFLAG) != 0
5061 && msgval == MSG_IDENTIFYFLAG)))
5073 * Wait for a complete incoming message, parse it, and respond accordingly.
5076 ahd_parse_msg(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
5078 struct ahd_initiator_tinfo *tinfo;
5079 struct ahd_tmode_tstate *tstate;
5084 done = MSGLOOP_IN_PROG;
5087 tinfo = ahd_fetch_transinfo(ahd, devinfo->channel, devinfo->our_scsiid,
5088 devinfo->target, &tstate);
5091 * Parse as much of the message as is available,
5092 * rejecting it if we don't support it. When
5093 * the entire message is available and has been
5094 * handled, return MSGLOOP_MSGCOMPLETE, indicating
5095 * that we have parsed an entire message.
5097 * In the case of extended messages, we accept the length
5098 * byte outright and perform more checking once we know the
5099 * extended message type.
5101 switch (ahd->msgin_buf[0]) {
5102 case MSG_DISCONNECT:
5103 case MSG_SAVEDATAPOINTER:
5104 case MSG_CMDCOMPLETE:
5105 case MSG_RESTOREPOINTERS:
5106 case MSG_IGN_WIDE_RESIDUE:
5108 * End our message loop as these are messages
5109 * the sequencer handles on its own.
5111 done = MSGLOOP_TERMINATED;
5113 case MSG_MESSAGE_REJECT:
5114 response = ahd_handle_msg_reject(ahd, devinfo);
5117 done = MSGLOOP_MSGCOMPLETE;
5121 /* Wait for enough of the message to begin validation */
5122 if (ahd->msgin_index < 2)
5124 switch (ahd->msgin_buf[2]) {
5132 if (ahd->msgin_buf[1] != MSG_EXT_SDTR_LEN) {
5138 * Wait until we have both args before validating
5139 * and acting on this message.
5141 * Add one to MSG_EXT_SDTR_LEN to account for
5142 * the extended message preamble.
5144 if (ahd->msgin_index < (MSG_EXT_SDTR_LEN + 1))
5147 period = ahd->msgin_buf[3];
5149 saved_offset = offset = ahd->msgin_buf[4];
5150 ahd_devlimited_syncrate(ahd, tinfo, &period,
5151 &ppr_options, devinfo->role);
5152 ahd_validate_offset(ahd, tinfo, period, &offset,
5153 tinfo->curr.width, devinfo->role);
5155 printf("(%s:%c:%d:%d): Received "
5156 "SDTR period %x, offset %x\n\t"
5157 "Filtered to period %x, offset %x\n",
5158 ahd_name(ahd), devinfo->channel,
5159 devinfo->target, devinfo->lun,
5160 ahd->msgin_buf[3], saved_offset,
5163 ahd_set_syncrate(ahd, devinfo, period,
5164 offset, ppr_options,
5165 AHD_TRANS_ACTIVE|AHD_TRANS_GOAL,
5169 * See if we initiated Sync Negotiation
5170 * and didn't have to fall down to async
5173 if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_SDTR, TRUE)) {
5175 if (saved_offset != offset) {
5176 /* Went too low - force async */
5181 * Send our own SDTR in reply
5184 && devinfo->role == ROLE_INITIATOR) {
5185 printf("(%s:%c:%d:%d): Target "
5187 ahd_name(ahd), devinfo->channel,
5188 devinfo->target, devinfo->lun);
5190 ahd->msgout_index = 0;
5191 ahd->msgout_len = 0;
5192 ahd_construct_sdtr(ahd, devinfo,
5194 ahd->msgout_index = 0;
5197 done = MSGLOOP_MSGCOMPLETE;
5204 u_int sending_reply;
5206 sending_reply = FALSE;
5207 if (ahd->msgin_buf[1] != MSG_EXT_WDTR_LEN) {
5213 * Wait until we have our arg before validating
5214 * and acting on this message.
5216 * Add one to MSG_EXT_WDTR_LEN to account for
5217 * the extended message preamble.
5219 if (ahd->msgin_index < (MSG_EXT_WDTR_LEN + 1))
5222 bus_width = ahd->msgin_buf[3];
5223 saved_width = bus_width;
5224 ahd_validate_width(ahd, tinfo, &bus_width,
5227 printf("(%s:%c:%d:%d): Received WDTR "
5228 "%x filtered to %x\n",
5229 ahd_name(ahd), devinfo->channel,
5230 devinfo->target, devinfo->lun,
5231 saved_width, bus_width);
5234 if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_WDTR, TRUE)) {
5236 * Don't send a WDTR back to the
5237 * target, since we asked first.
5238 * If the width went higher than our
5239 * request, reject it.
5241 if (saved_width > bus_width) {
5243 printf("(%s:%c:%d:%d): requested %dBit "
5244 "transfers. Rejecting...\n",
5245 ahd_name(ahd), devinfo->channel,
5246 devinfo->target, devinfo->lun,
5247 8 * (0x01 << bus_width));
5252 * Send our own WDTR in reply
5255 && devinfo->role == ROLE_INITIATOR) {
5256 printf("(%s:%c:%d:%d): Target "
5258 ahd_name(ahd), devinfo->channel,
5259 devinfo->target, devinfo->lun);
5261 ahd->msgout_index = 0;
5262 ahd->msgout_len = 0;
5263 ahd_construct_wdtr(ahd, devinfo, bus_width);
5264 ahd->msgout_index = 0;
5266 sending_reply = TRUE;
5269 * After a wide message, we are async, but
5270 * some devices don't seem to honor this portion
5271 * of the spec. Force a renegotiation of the
5272 * sync component of our transfer agreement even
5273 * if our goal is async. By updating our width
5274 * after forcing the negotiation, we avoid
5275 * renegotiating for width.
5277 ahd_update_neg_request(ahd, devinfo, tstate,
5278 tinfo, AHD_NEG_ALWAYS);
5279 ahd_set_width(ahd, devinfo, bus_width,
5280 AHD_TRANS_ACTIVE|AHD_TRANS_GOAL,
5282 if (sending_reply == FALSE && reject == FALSE) {
5285 * We will always have an SDTR to send.
5287 ahd->msgout_index = 0;
5288 ahd->msgout_len = 0;
5289 ahd_build_transfer_msg(ahd, devinfo);
5290 ahd->msgout_index = 0;
5293 done = MSGLOOP_MSGCOMPLETE;
5304 u_int saved_ppr_options;
5306 if (ahd->msgin_buf[1] != MSG_EXT_PPR_LEN) {
5312 * Wait until we have all args before validating
5313 * and acting on this message.
5315 * Add one to MSG_EXT_PPR_LEN to account for
5316 * the extended message preamble.
5318 if (ahd->msgin_index < (MSG_EXT_PPR_LEN + 1))
5321 period = ahd->msgin_buf[3];
5322 offset = ahd->msgin_buf[5];
5323 bus_width = ahd->msgin_buf[6];
5324 saved_width = bus_width;
5325 ppr_options = ahd->msgin_buf[7];
5327 * According to the spec, a DT only
5328 * period factor with no DT option
5329 * set implies async.
5331 if ((ppr_options & MSG_EXT_PPR_DT_REQ) == 0
5334 saved_ppr_options = ppr_options;
5335 saved_offset = offset;
5338 * Transfer options are only available if we
5339 * are negotiating wide.
5342 ppr_options &= MSG_EXT_PPR_QAS_REQ;
5344 ahd_validate_width(ahd, tinfo, &bus_width,
5346 ahd_devlimited_syncrate(ahd, tinfo, &period,
5347 &ppr_options, devinfo->role);
5348 ahd_validate_offset(ahd, tinfo, period, &offset,
5349 bus_width, devinfo->role);
5351 if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_PPR, TRUE)) {
5353 * If we are unable to do any of the
5354 * requested options (we went too low),
5355 * then we'll have to reject the message.
5357 if (saved_width > bus_width
5358 || saved_offset != offset
5359 || saved_ppr_options != ppr_options) {
5367 if (devinfo->role != ROLE_TARGET)
5368 printf("(%s:%c:%d:%d): Target "
5370 ahd_name(ahd), devinfo->channel,
5371 devinfo->target, devinfo->lun);
5373 printf("(%s:%c:%d:%d): Initiator "
5375 ahd_name(ahd), devinfo->channel,
5376 devinfo->target, devinfo->lun);
5377 ahd->msgout_index = 0;
5378 ahd->msgout_len = 0;
5379 ahd_construct_ppr(ahd, devinfo, period, offset,
5380 bus_width, ppr_options);
5381 ahd->msgout_index = 0;
5385 printf("(%s:%c:%d:%d): Received PPR width %x, "
5386 "period %x, offset %x,options %x\n"
5387 "\tFiltered to width %x, period %x, "
5388 "offset %x, options %x\n",
5389 ahd_name(ahd), devinfo->channel,
5390 devinfo->target, devinfo->lun,
5391 saved_width, ahd->msgin_buf[3],
5392 saved_offset, saved_ppr_options,
5393 bus_width, period, offset, ppr_options);
5395 ahd_set_width(ahd, devinfo, bus_width,
5396 AHD_TRANS_ACTIVE|AHD_TRANS_GOAL,
5398 ahd_set_syncrate(ahd, devinfo, period,
5399 offset, ppr_options,
5400 AHD_TRANS_ACTIVE|AHD_TRANS_GOAL,
5403 done = MSGLOOP_MSGCOMPLETE;
5407 /* Unknown extended message. Reject it. */
5413 #ifdef AHD_TARGET_MODE
5414 case MSG_BUS_DEV_RESET:
5415 ahd_handle_devreset(ahd, devinfo, CAM_LUN_WILDCARD,
5417 "Bus Device Reset Received",
5418 /*verbose_level*/0);
5420 done = MSGLOOP_TERMINATED;
5424 case MSG_CLEAR_QUEUE:
5428 /* Target mode messages */
5429 if (devinfo->role != ROLE_TARGET) {
5433 tag = SCB_LIST_NULL;
5434 if (ahd->msgin_buf[0] == MSG_ABORT_TAG)
5435 tag = ahd_inb(ahd, INITIATOR_TAG);
5436 ahd_abort_scbs(ahd, devinfo->target, devinfo->channel,
5437 devinfo->lun, tag, ROLE_TARGET,
5440 tstate = ahd->enabled_targets[devinfo->our_scsiid];
5441 if (tstate != NULL) {
5442 struct ahd_tmode_lstate* lstate;
5444 lstate = tstate->enabled_luns[devinfo->lun];
5445 if (lstate != NULL) {
5446 ahd_queue_lstate_event(ahd, lstate,
5447 devinfo->our_scsiid,
5450 ahd_send_lstate_events(ahd, lstate);
5454 done = MSGLOOP_TERMINATED;
5458 case MSG_QAS_REQUEST:
5460 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
5461 printf("%s: QAS request. SCSISIGI == 0x%x\n",
5462 ahd_name(ahd), ahd_inb(ahd, SCSISIGI));
5464 ahd->msg_flags |= MSG_FLAG_EXPECT_QASREJ_BUSFREE;
5466 case MSG_TERM_IO_PROC:
5474 * Setup to reject the message.
5476 ahd->msgout_index = 0;
5477 ahd->msgout_len = 1;
5478 ahd->msgout_buf[0] = MSG_MESSAGE_REJECT;
5479 done = MSGLOOP_MSGCOMPLETE;
5483 if (done != MSGLOOP_IN_PROG && !response)
5484 /* Clear the outgoing message buffer */
5485 ahd->msgout_len = 0;
5491 * Process a message reject message.
5494 ahd_handle_msg_reject(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
5497 * What we care about here is if we had an
5498 * outstanding SDTR or WDTR message for this
5499 * target. If we did, this is a signal that
5500 * the target is refusing negotiation.
5503 struct ahd_initiator_tinfo *tinfo;
5504 struct ahd_tmode_tstate *tstate;
5509 scb_index = ahd_get_scbptr(ahd);
5510 scb = ahd_lookup_scb(ahd, scb_index);
5511 tinfo = ahd_fetch_transinfo(ahd, devinfo->channel,
5512 devinfo->our_scsiid,
5513 devinfo->target, &tstate);
5514 /* Might be necessary */
5515 last_msg = ahd_inb(ahd, LAST_MSG);
5517 if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_PPR, /*full*/FALSE)) {
5518 if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_PPR, /*full*/TRUE)
5519 && tinfo->goal.period <= AHD_SYNCRATE_PACED) {
5521 * Target may not like our SPI-4 PPR Options.
5522 * Attempt to negotiate 80MHz which will turn
5523 * off these options.
5526 printf("(%s:%c:%d:%d): PPR Rejected. "
5527 "Trying simple U160 PPR\n",
5528 ahd_name(ahd), devinfo->channel,
5529 devinfo->target, devinfo->lun);
5531 tinfo->goal.period = AHD_SYNCRATE_DT;
5532 tinfo->goal.ppr_options &= MSG_EXT_PPR_IU_REQ
5533 | MSG_EXT_PPR_QAS_REQ
5534 | MSG_EXT_PPR_DT_REQ;
5537 * Target does not support the PPR message.
5538 * Attempt to negotiate SPI-2 style.
5541 printf("(%s:%c:%d:%d): PPR Rejected. "
5542 "Trying WDTR/SDTR\n",
5543 ahd_name(ahd), devinfo->channel,
5544 devinfo->target, devinfo->lun);
5546 tinfo->goal.ppr_options = 0;
5547 tinfo->curr.transport_version = 2;
5548 tinfo->goal.transport_version = 2;
5550 ahd->msgout_index = 0;
5551 ahd->msgout_len = 0;
5552 ahd_build_transfer_msg(ahd, devinfo);
5553 ahd->msgout_index = 0;
5555 } else if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_WDTR, /*full*/FALSE)) {
5557 /* note 8bit xfers */
5558 printf("(%s:%c:%d:%d): refuses WIDE negotiation. Using "
5559 "8bit transfers\n", ahd_name(ahd),
5560 devinfo->channel, devinfo->target, devinfo->lun);
5561 ahd_set_width(ahd, devinfo, MSG_EXT_WDTR_BUS_8_BIT,
5562 AHD_TRANS_ACTIVE|AHD_TRANS_GOAL,
5565 * No need to clear the sync rate. If the target
5566 * did not accept the command, our syncrate is
5567 * unaffected. If the target started the negotiation,
5568 * but rejected our response, we already cleared the
5569 * sync rate before sending our WDTR.
5571 if (tinfo->goal.offset != tinfo->curr.offset) {
5573 /* Start the sync negotiation */
5574 ahd->msgout_index = 0;
5575 ahd->msgout_len = 0;
5576 ahd_build_transfer_msg(ahd, devinfo);
5577 ahd->msgout_index = 0;
5580 } else if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_SDTR, /*full*/FALSE)) {
5581 /* note asynch xfers and clear flag */
5582 ahd_set_syncrate(ahd, devinfo, /*period*/0,
5583 /*offset*/0, /*ppr_options*/0,
5584 AHD_TRANS_ACTIVE|AHD_TRANS_GOAL,
5586 printf("(%s:%c:%d:%d): refuses synchronous negotiation. "
5587 "Using asynchronous transfers\n",
5588 ahd_name(ahd), devinfo->channel,
5589 devinfo->target, devinfo->lun);
5590 } else if ((scb->hscb->control & MSG_SIMPLE_TASK) != 0) {
5594 tag_type = (scb->hscb->control & MSG_SIMPLE_TASK);
5596 if (tag_type == MSG_SIMPLE_TASK) {
5597 printf("(%s:%c:%d:%d): refuses tagged commands. "
5598 "Performing non-tagged I/O\n", ahd_name(ahd),
5599 devinfo->channel, devinfo->target, devinfo->lun);
5600 ahd_set_tags(ahd, scb->io_ctx, devinfo, AHD_QUEUE_NONE);
5603 printf("(%s:%c:%d:%d): refuses %s tagged commands. "
5604 "Performing simple queue tagged I/O only\n",
5605 ahd_name(ahd), devinfo->channel, devinfo->target,
5606 devinfo->lun, tag_type == MSG_ORDERED_TASK
5607 ? "ordered" : "head of queue");
5608 ahd_set_tags(ahd, scb->io_ctx, devinfo, AHD_QUEUE_BASIC);
5613 * Resend the identify for this CCB as the target
5614 * may believe that the selection is invalid otherwise.
5616 ahd_outb(ahd, SCB_CONTROL,
5617 ahd_inb_scbram(ahd, SCB_CONTROL) & mask);
5618 scb->hscb->control &= mask;
5619 ahd_set_transaction_tag(scb, /*enabled*/FALSE,
5620 /*type*/MSG_SIMPLE_TASK);
5621 ahd_outb(ahd, MSG_OUT, MSG_IDENTIFYFLAG);
5622 ahd_assert_atn(ahd);
5623 ahd_busy_tcl(ahd, BUILD_TCL(scb->hscb->scsiid, devinfo->lun),
5627 * Requeue all tagged commands for this target
5628 * currently in our posession so they can be
5629 * converted to untagged commands.
5631 ahd_search_qinfifo(ahd, SCB_GET_TARGET(ahd, scb),
5632 SCB_GET_CHANNEL(ahd, scb),
5633 SCB_GET_LUN(scb), /*tag*/SCB_LIST_NULL,
5634 ROLE_INITIATOR, CAM_REQUEUE_REQ,
5636 } else if (ahd_sent_msg(ahd, AHDMSG_1B, MSG_IDENTIFYFLAG, TRUE)) {
5638 * Most likely the device believes that we had
5639 * previously negotiated packetized.
5641 ahd->msg_flags |= MSG_FLAG_EXPECT_PPR_BUSFREE
5642 | MSG_FLAG_IU_REQ_CHANGED;
5644 ahd_force_renegotiation(ahd, devinfo);
5645 ahd->msgout_index = 0;
5646 ahd->msgout_len = 0;
5647 ahd_build_transfer_msg(ahd, devinfo);
5648 ahd->msgout_index = 0;
5652 * Otherwise, we ignore it.
5654 printf("%s:%c:%d: Message reject for %x -- ignored\n",
5655 ahd_name(ahd), devinfo->channel, devinfo->target,
5662 * Process an ingnore wide residue message.
5665 ahd_handle_ign_wide_residue(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
5670 scb_index = ahd_get_scbptr(ahd);
5671 scb = ahd_lookup_scb(ahd, scb_index);
5673 * XXX Actually check data direction in the sequencer?
5674 * Perhaps add datadir to some spare bits in the hscb?
5676 if ((ahd_inb(ahd, SEQ_FLAGS) & DPHASE) == 0
5677 || ahd_get_transfer_dir(scb) != CAM_DIR_IN) {
5679 * Ignore the message if we haven't
5680 * seen an appropriate data phase yet.
5684 * If the residual occurred on the last
5685 * transfer and the transfer request was
5686 * expected to end on an odd count, do
5687 * nothing. Otherwise, subtract a byte
5688 * and update the residual count accordingly.
5692 sgptr = ahd_inb_scbram(ahd, SCB_RESIDUAL_SGPTR);
5693 if ((sgptr & SG_LIST_NULL) != 0
5694 && (ahd_inb_scbram(ahd, SCB_TASK_ATTRIBUTE)
5695 & SCB_XFERLEN_ODD) != 0) {
5697 * If the residual occurred on the last
5698 * transfer and the transfer request was
5699 * expected to end on an odd count, do
5707 /* Pull in the rest of the sgptr */
5708 sgptr = ahd_inl_scbram(ahd, SCB_RESIDUAL_SGPTR);
5709 data_cnt = ahd_inl_scbram(ahd, SCB_RESIDUAL_DATACNT);
5710 if ((sgptr & SG_LIST_NULL) != 0) {
5712 * The residual data count is not updated
5713 * for the command run to completion case.
5714 * Explicitly zero the count.
5716 data_cnt &= ~AHD_SG_LEN_MASK;
5718 data_addr = ahd_inq(ahd, SHADDR);
5721 sgptr &= SG_PTR_MASK;
5722 if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0) {
5723 struct ahd_dma64_seg *sg;
5725 sg = ahd_sg_bus_to_virt(ahd, scb, sgptr);
5728 * The residual sg ptr points to the next S/G
5729 * to load so we must go back one.
5732 sglen = ahd_le32toh(sg->len) & AHD_SG_LEN_MASK;
5733 if (sg != scb->sg_list
5734 && sglen < (data_cnt & AHD_SG_LEN_MASK)) {
5737 sglen = ahd_le32toh(sg->len);
5739 * Preserve High Address and SG_LIST
5740 * bits while setting the count to 1.
5742 data_cnt = 1|(sglen&(~AHD_SG_LEN_MASK));
5743 data_addr = ahd_le64toh(sg->addr)
5744 + (sglen & AHD_SG_LEN_MASK)
5748 * Increment sg so it points to the
5752 sgptr = ahd_sg_virt_to_bus(ahd, scb,
5756 struct ahd_dma_seg *sg;
5758 sg = ahd_sg_bus_to_virt(ahd, scb, sgptr);
5761 * The residual sg ptr points to the next S/G
5762 * to load so we must go back one.
5765 sglen = ahd_le32toh(sg->len) & AHD_SG_LEN_MASK;
5766 if (sg != scb->sg_list
5767 && sglen < (data_cnt & AHD_SG_LEN_MASK)) {
5770 sglen = ahd_le32toh(sg->len);
5772 * Preserve High Address and SG_LIST
5773 * bits while setting the count to 1.
5775 data_cnt = 1|(sglen&(~AHD_SG_LEN_MASK));
5776 data_addr = ahd_le32toh(sg->addr)
5777 + (sglen & AHD_SG_LEN_MASK)
5781 * Increment sg so it points to the
5785 sgptr = ahd_sg_virt_to_bus(ahd, scb,
5790 * Toggle the "oddness" of the transfer length
5791 * to handle this mid-transfer ignore wide
5792 * residue. This ensures that the oddness is
5793 * correct for subsequent data transfers.
5795 ahd_outb(ahd, SCB_TASK_ATTRIBUTE,
5796 ahd_inb_scbram(ahd, SCB_TASK_ATTRIBUTE)
5799 ahd_outl(ahd, SCB_RESIDUAL_SGPTR, sgptr);
5800 ahd_outl(ahd, SCB_RESIDUAL_DATACNT, data_cnt);
5802 * The FIFO's pointers will be updated if/when the
5803 * sequencer re-enters a data phase.
5811 * Reinitialize the data pointers for the active transfer
5812 * based on its current residual.
5815 ahd_reinitialize_dataptrs(struct ahd_softc *ahd)
5818 ahd_mode_state saved_modes;
5825 AHD_ASSERT_MODES(ahd, AHD_MODE_DFF0_MSK|AHD_MODE_DFF1_MSK,
5826 AHD_MODE_DFF0_MSK|AHD_MODE_DFF1_MSK);
5828 scb_index = ahd_get_scbptr(ahd);
5829 scb = ahd_lookup_scb(ahd, scb_index);
5832 * Release and reacquire the FIFO so we
5833 * have a clean slate.
5835 ahd_outb(ahd, DFFSXFRCTL, CLRCHN);
5837 while (--wait && !(ahd_inb(ahd, MDFFSTAT) & FIFOFREE))
5840 ahd_print_path(ahd, scb);
5841 printf("ahd_reinitialize_dataptrs: Forcing FIFO free.\n");
5842 ahd_outb(ahd, DFFSXFRCTL, RSTCHN|CLRSHCNT);
5844 saved_modes = ahd_save_modes(ahd);
5845 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
5846 ahd_outb(ahd, DFFSTAT,
5847 ahd_inb(ahd, DFFSTAT)
5848 | (saved_modes == 0x11 ? CURRFIFO_1 : CURRFIFO_0));
5851 * Determine initial values for data_addr and data_cnt
5852 * for resuming the data phase.
5854 sgptr = ahd_inl_scbram(ahd, SCB_RESIDUAL_SGPTR);
5855 sgptr &= SG_PTR_MASK;
5857 resid = (ahd_inb_scbram(ahd, SCB_RESIDUAL_DATACNT + 2) << 16)
5858 | (ahd_inb_scbram(ahd, SCB_RESIDUAL_DATACNT + 1) << 8)
5859 | ahd_inb_scbram(ahd, SCB_RESIDUAL_DATACNT);
5861 if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0) {
5862 struct ahd_dma64_seg *sg;
5864 sg = ahd_sg_bus_to_virt(ahd, scb, sgptr);
5866 /* The residual sg_ptr always points to the next sg */
5869 dataptr = ahd_le64toh(sg->addr)
5870 + (ahd_le32toh(sg->len) & AHD_SG_LEN_MASK)
5872 ahd_outl(ahd, HADDR + 4, dataptr >> 32);
5874 struct ahd_dma_seg *sg;
5876 sg = ahd_sg_bus_to_virt(ahd, scb, sgptr);
5878 /* The residual sg_ptr always points to the next sg */
5881 dataptr = ahd_le32toh(sg->addr)
5882 + (ahd_le32toh(sg->len) & AHD_SG_LEN_MASK)
5884 ahd_outb(ahd, HADDR + 4,
5885 (ahd_le32toh(sg->len) & ~AHD_SG_LEN_MASK) >> 24);
5887 ahd_outl(ahd, HADDR, dataptr);
5888 ahd_outb(ahd, HCNT + 2, resid >> 16);
5889 ahd_outb(ahd, HCNT + 1, resid >> 8);
5890 ahd_outb(ahd, HCNT, resid);
5894 * Handle the effects of issuing a bus device reset message.
5897 ahd_handle_devreset(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
5898 u_int lun, cam_status status, char *message,
5901 #ifdef AHD_TARGET_MODE
5902 struct ahd_tmode_tstate* tstate;
5906 found = ahd_abort_scbs(ahd, devinfo->target, devinfo->channel,
5907 lun, SCB_LIST_NULL, devinfo->role,
5910 #ifdef AHD_TARGET_MODE
5912 * Send an immediate notify ccb to all target mord peripheral
5913 * drivers affected by this action.
5915 tstate = ahd->enabled_targets[devinfo->our_scsiid];
5916 if (tstate != NULL) {
5920 if (lun != CAM_LUN_WILDCARD) {
5922 max_lun = AHD_NUM_LUNS - 1;
5927 for (;cur_lun <= max_lun; cur_lun++) {
5928 struct ahd_tmode_lstate* lstate;
5930 lstate = tstate->enabled_luns[cur_lun];
5934 ahd_queue_lstate_event(ahd, lstate, devinfo->our_scsiid,
5935 MSG_BUS_DEV_RESET, /*arg*/0);
5936 ahd_send_lstate_events(ahd, lstate);
5942 * Go back to async/narrow transfers and renegotiate.
5944 ahd_set_width(ahd, devinfo, MSG_EXT_WDTR_BUS_8_BIT,
5945 AHD_TRANS_CUR, /*paused*/TRUE);
5946 ahd_set_syncrate(ahd, devinfo, /*period*/0, /*offset*/0,
5947 /*ppr_options*/0, AHD_TRANS_CUR,
5950 if (status != CAM_SEL_TIMEOUT)
5951 ahd_send_async(ahd, devinfo->channel, devinfo->target,
5952 CAM_LUN_WILDCARD, AC_SENT_BDR);
5954 if (message != NULL && bootverbose)
5955 printf("%s: %s on %c:%d. %d SCBs aborted\n", ahd_name(ahd),
5956 message, devinfo->channel, devinfo->target, found);
5959 #ifdef AHD_TARGET_MODE
5961 ahd_setup_target_msgin(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
5966 * To facilitate adding multiple messages together,
5967 * each routine should increment the index and len
5968 * variables instead of setting them explicitly.
5970 ahd->msgout_index = 0;
5971 ahd->msgout_len = 0;
5973 if (scb != NULL && (scb->flags & SCB_AUTO_NEGOTIATE) != 0)
5974 ahd_build_transfer_msg(ahd, devinfo);
5976 panic("ahd_intr: AWAITING target message with no message");
5978 ahd->msgout_index = 0;
5979 ahd->msg_type = MSG_TYPE_TARGET_MSGIN;
5982 /**************************** Initialization **********************************/
5984 ahd_sglist_size(struct ahd_softc *ahd)
5986 bus_size_t list_size;
5988 list_size = sizeof(struct ahd_dma_seg) * AHD_NSEG;
5989 if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0)
5990 list_size = sizeof(struct ahd_dma64_seg) * AHD_NSEG;
5995 * Calculate the optimum S/G List allocation size. S/G elements used
5996 * for a given transaction must be physically contiguous. Assume the
5997 * OS will allocate full pages to us, so it doesn't make sense to request
6001 ahd_sglist_allocsize(struct ahd_softc *ahd)
6003 bus_size_t sg_list_increment;
6004 bus_size_t sg_list_size;
6005 bus_size_t max_list_size;
6006 bus_size_t best_list_size;
6008 /* Start out with the minimum required for AHD_NSEG. */
6009 sg_list_increment = ahd_sglist_size(ahd);
6010 sg_list_size = sg_list_increment;
6012 /* Get us as close as possible to a page in size. */
6013 while ((sg_list_size + sg_list_increment) <= PAGE_SIZE)
6014 sg_list_size += sg_list_increment;
6017 * Try to reduce the amount of wastage by allocating
6020 best_list_size = sg_list_size;
6021 max_list_size = roundup(sg_list_increment, PAGE_SIZE);
6022 if (max_list_size < 4 * PAGE_SIZE)
6023 max_list_size = 4 * PAGE_SIZE;
6024 if (max_list_size > (AHD_SCB_MAX_ALLOC * sg_list_increment))
6025 max_list_size = (AHD_SCB_MAX_ALLOC * sg_list_increment);
6026 while ((sg_list_size + sg_list_increment) <= max_list_size
6027 && (sg_list_size % PAGE_SIZE) != 0) {
6029 bus_size_t best_mod;
6031 sg_list_size += sg_list_increment;
6032 new_mod = sg_list_size % PAGE_SIZE;
6033 best_mod = best_list_size % PAGE_SIZE;
6034 if (new_mod > best_mod || new_mod == 0) {
6035 best_list_size = sg_list_size;
6038 return (best_list_size);
6042 * Allocate a controller structure for a new device
6043 * and perform initial initializion.
6046 ahd_alloc(void *platform_arg, char *name)
6048 struct ahd_softc *ahd;
6051 ahd = malloc(sizeof(*ahd), M_DEVBUF, M_NOWAIT);
6053 printf("aic7xxx: cannot malloc softc!\n");
6054 free(name, M_DEVBUF);
6058 ahd = device_get_softc((device_t)platform_arg);
6060 memset(ahd, 0, sizeof(*ahd));
6061 ahd->seep_config = malloc(sizeof(*ahd->seep_config),
6062 M_DEVBUF, M_NOWAIT);
6063 if (ahd->seep_config == NULL) {
6065 free(ahd, M_DEVBUF);
6067 free(name, M_DEVBUF);
6070 LIST_INIT(&ahd->pending_scbs);
6071 /* We don't know our unit number until the OSM sets it */
6074 ahd->description = NULL;
6075 ahd->bus_description = NULL;
6077 ahd->chip = AHD_NONE;
6078 ahd->features = AHD_FENONE;
6079 ahd->bugs = AHD_BUGNONE;
6080 ahd->flags = AHD_SPCHK_ENB_A|AHD_RESET_BUS_A|AHD_TERM_ENB_A
6081 | AHD_EXTENDED_TRANS_A|AHD_STPWLEVEL_A;
6082 ahd_timer_init(&ahd->reset_timer);
6083 ahd_timer_init(&ahd->stat_timer);
6084 ahd->int_coalescing_timer = AHD_INT_COALESCING_TIMER_DEFAULT;
6085 ahd->int_coalescing_maxcmds = AHD_INT_COALESCING_MAXCMDS_DEFAULT;
6086 ahd->int_coalescing_mincmds = AHD_INT_COALESCING_MINCMDS_DEFAULT;
6087 ahd->int_coalescing_threshold = AHD_INT_COALESCING_THRESHOLD_DEFAULT;
6088 ahd->int_coalescing_stop_threshold =
6089 AHD_INT_COALESCING_STOP_THRESHOLD_DEFAULT;
6091 if (ahd_platform_alloc(ahd, platform_arg) != 0) {
6096 if ((ahd_debug & AHD_SHOW_MEMORY) != 0) {
6097 printf("%s: scb size = 0x%x, hscb size = 0x%x\n",
6098 ahd_name(ahd), (u_int)sizeof(struct scb),
6099 (u_int)sizeof(struct hardware_scb));
6106 ahd_softc_init(struct ahd_softc *ahd)
6115 ahd_set_unit(struct ahd_softc *ahd, int unit)
6121 ahd_set_name(struct ahd_softc *ahd, char *name)
6123 if (ahd->name != NULL)
6124 free(ahd->name, M_DEVBUF);
6129 ahd_free(struct ahd_softc *ahd)
6133 switch (ahd->init_level) {
6139 ahd_dmamap_unload(ahd, ahd->shared_data_dmat,
6140 ahd->shared_data_map.dmamap);
6143 ahd_dmamem_free(ahd, ahd->shared_data_dmat, ahd->qoutfifo,
6144 ahd->shared_data_map.dmamap);
6145 ahd_dmamap_destroy(ahd, ahd->shared_data_dmat,
6146 ahd->shared_data_map.dmamap);
6149 ahd_dma_tag_destroy(ahd, ahd->shared_data_dmat);
6152 ahd_dma_tag_destroy(ahd, ahd->buffer_dmat);
6160 ahd_dma_tag_destroy(ahd, ahd->parent_dmat);
6162 ahd_platform_free(ahd);
6163 ahd_fini_scbdata(ahd);
6164 for (i = 0; i < AHD_NUM_TARGETS; i++) {
6165 struct ahd_tmode_tstate *tstate;
6167 tstate = ahd->enabled_targets[i];
6168 if (tstate != NULL) {
6169 #ifdef AHD_TARGET_MODE
6172 for (j = 0; j < AHD_NUM_LUNS; j++) {
6173 struct ahd_tmode_lstate *lstate;
6175 lstate = tstate->enabled_luns[j];
6176 if (lstate != NULL) {
6177 xpt_free_path(lstate->path);
6178 free(lstate, M_DEVBUF);
6182 free(tstate, M_DEVBUF);
6185 #ifdef AHD_TARGET_MODE
6186 if (ahd->black_hole != NULL) {
6187 xpt_free_path(ahd->black_hole->path);
6188 free(ahd->black_hole, M_DEVBUF);
6191 if (ahd->name != NULL)
6192 free(ahd->name, M_DEVBUF);
6193 if (ahd->seep_config != NULL)
6194 free(ahd->seep_config, M_DEVBUF);
6195 if (ahd->saved_stack != NULL)
6196 free(ahd->saved_stack, M_DEVBUF);
6198 free(ahd, M_DEVBUF);
6204 ahd_shutdown(void *arg)
6206 struct ahd_softc *ahd;
6208 ahd = (struct ahd_softc *)arg;
6211 * Stop periodic timer callbacks.
6213 ahd_timer_stop(&ahd->reset_timer);
6214 ahd_timer_stop(&ahd->stat_timer);
6216 /* This will reset most registers to 0, but not all */
6217 ahd_reset(ahd, /*reinit*/FALSE);
6221 * Reset the controller and record some information about it
6222 * that is only available just after a reset. If "reinit" is
6223 * non-zero, this reset occured after initial configuration
6224 * and the caller requests that the chip be fully reinitialized
6225 * to a runable state. Chip interrupts are *not* enabled after
6226 * a reinitialization. The caller must enable interrupts via
6227 * ahd_intr_enable().
6230 ahd_reset(struct ahd_softc *ahd, int reinit)
6237 * Preserve the value of the SXFRCTL1 register for all channels.
6238 * It contains settings that affect termination and we don't want
6239 * to disturb the integrity of the bus.
6242 ahd_update_modes(ahd);
6243 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
6244 sxfrctl1 = ahd_inb(ahd, SXFRCTL1);
6246 cmd = ahd_pci_read_config(ahd->dev_softc, PCIR_COMMAND, /*bytes*/2);
6247 if ((ahd->bugs & AHD_PCIX_CHIPRST_BUG) != 0) {
6252 * During the assertion of CHIPRST, the chip
6253 * does not disable its parity logic prior to
6254 * the start of the reset. This may cause a
6255 * parity error to be detected and thus a
6256 * spurious SERR or PERR assertion. Disble
6257 * PERR and SERR responses during the CHIPRST.
6259 mod_cmd = cmd & ~(PCIM_CMD_PERRESPEN|PCIM_CMD_SERRESPEN);
6260 ahd_pci_write_config(ahd->dev_softc, PCIR_COMMAND,
6261 mod_cmd, /*bytes*/2);
6263 ahd_outb(ahd, HCNTRL, CHIPRST | ahd->pause);
6266 * Ensure that the reset has finished. We delay 1000us
6267 * prior to reading the register to make sure the chip
6268 * has sufficiently completed its reset to handle register
6274 } while (--wait && !(ahd_inb(ahd, HCNTRL) & CHIPRSTACK));
6277 printf("%s: WARNING - Failed chip reset! "
6278 "Trying to initialize anyway.\n", ahd_name(ahd));
6280 ahd_outb(ahd, HCNTRL, ahd->pause);
6282 if ((ahd->bugs & AHD_PCIX_CHIPRST_BUG) != 0) {
6284 * Clear any latched PCI error status and restore
6285 * previous SERR and PERR response enables.
6287 ahd_pci_write_config(ahd->dev_softc, PCIR_STATUS + 1,
6289 ahd_pci_write_config(ahd->dev_softc, PCIR_COMMAND,
6294 * Mode should be SCSI after a chip reset, but lets
6295 * set it just to be safe. We touch the MODE_PTR
6296 * register directly so as to bypass the lazy update
6297 * code in ahd_set_modes().
6299 ahd_known_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
6300 ahd_outb(ahd, MODE_PTR,
6301 ahd_build_mode_state(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI));
6306 * We must always initialize STPWEN to 1 before we
6307 * restore the saved values. STPWEN is initialized
6308 * to a tri-state condition which can only be cleared
6311 ahd_outb(ahd, SXFRCTL1, sxfrctl1|STPWEN);
6312 ahd_outb(ahd, SXFRCTL1, sxfrctl1);
6314 /* Determine chip configuration */
6315 ahd->features &= ~AHD_WIDE;
6316 if ((ahd_inb(ahd, SBLKCTL) & SELWIDE) != 0)
6317 ahd->features |= AHD_WIDE;
6320 * If a recovery action has forced a chip reset,
6321 * re-initialize the chip to our liking.
6330 * Determine the number of SCBs available on the controller
6333 ahd_probe_scbs(struct ahd_softc *ahd) {
6336 AHD_ASSERT_MODES(ahd, ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK),
6337 ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK));
6338 for (i = 0; i < AHD_SCB_MAX; i++) {
6341 ahd_set_scbptr(ahd, i);
6342 ahd_outw(ahd, SCB_BASE, i);
6343 for (j = 2; j < 64; j++)
6344 ahd_outb(ahd, SCB_BASE+j, 0);
6345 /* Start out life as unallocated (needing an abort) */
6346 ahd_outb(ahd, SCB_CONTROL, MK_MESSAGE);
6347 if (ahd_inw_scbram(ahd, SCB_BASE) != i)
6349 ahd_set_scbptr(ahd, 0);
6350 if (ahd_inw_scbram(ahd, SCB_BASE) != 0)
6357 ahd_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
6361 baddr = (dma_addr_t *)arg;
6362 *baddr = segs->ds_addr;
6366 ahd_initialize_hscbs(struct ahd_softc *ahd)
6370 for (i = 0; i < ahd->scb_data.maxhscbs; i++) {
6371 ahd_set_scbptr(ahd, i);
6373 /* Clear the control byte. */
6374 ahd_outb(ahd, SCB_CONTROL, 0);
6376 /* Set the next pointer */
6377 ahd_outw(ahd, SCB_NEXT, SCB_LIST_NULL);
6382 ahd_init_scbdata(struct ahd_softc *ahd)
6384 struct scb_data *scb_data;
6387 scb_data = &ahd->scb_data;
6388 TAILQ_INIT(&scb_data->free_scbs);
6389 for (i = 0; i < AHD_NUM_TARGETS * AHD_NUM_LUNS_NONPKT; i++)
6390 LIST_INIT(&scb_data->free_scb_lists[i]);
6391 LIST_INIT(&scb_data->any_dev_free_scb_list);
6392 SLIST_INIT(&scb_data->hscb_maps);
6393 SLIST_INIT(&scb_data->sg_maps);
6394 SLIST_INIT(&scb_data->sense_maps);
6396 /* Determine the number of hardware SCBs and initialize them */
6397 scb_data->maxhscbs = ahd_probe_scbs(ahd);
6398 if (scb_data->maxhscbs == 0) {
6399 printf("%s: No SCB space found\n", ahd_name(ahd));
6403 ahd_initialize_hscbs(ahd);
6406 * Create our DMA tags. These tags define the kinds of device
6407 * accessible memory allocations and memory mappings we will
6408 * need to perform during normal operation.
6410 * Unless we need to further restrict the allocation, we rely
6411 * on the restrictions of the parent dmat, hence the common
6412 * use of MAXADDR and MAXSIZE.
6415 /* DMA tag for our hardware scb structures */
6416 if (ahd_dma_tag_create(ahd, ahd->parent_dmat, /*alignment*/1,
6417 /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
6418 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
6419 /*highaddr*/BUS_SPACE_MAXADDR,
6420 /*filter*/NULL, /*filterarg*/NULL,
6421 PAGE_SIZE, /*nsegments*/1,
6422 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
6423 /*flags*/0, &scb_data->hscb_dmat) != 0) {
6427 scb_data->init_level++;
6429 /* DMA tag for our S/G structures. */
6430 if (ahd_dma_tag_create(ahd, ahd->parent_dmat, /*alignment*/8,
6431 /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
6432 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
6433 /*highaddr*/BUS_SPACE_MAXADDR,
6434 /*filter*/NULL, /*filterarg*/NULL,
6435 ahd_sglist_allocsize(ahd), /*nsegments*/1,
6436 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
6437 /*flags*/0, &scb_data->sg_dmat) != 0) {
6441 if ((ahd_debug & AHD_SHOW_MEMORY) != 0)
6442 printf("%s: ahd_sglist_allocsize = 0x%x\n", ahd_name(ahd),
6443 ahd_sglist_allocsize(ahd));
6446 scb_data->init_level++;
6448 /* DMA tag for our sense buffers. We allocate in page sized chunks */
6449 if (ahd_dma_tag_create(ahd, ahd->parent_dmat, /*alignment*/1,
6450 /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
6451 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
6452 /*highaddr*/BUS_SPACE_MAXADDR,
6453 /*filter*/NULL, /*filterarg*/NULL,
6454 PAGE_SIZE, /*nsegments*/1,
6455 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
6456 /*flags*/0, &scb_data->sense_dmat) != 0) {
6460 scb_data->init_level++;
6462 /* Perform initial CCB allocation */
6463 ahd_alloc_scbs(ahd);
6465 if (scb_data->numscbs == 0) {
6466 printf("%s: ahd_init_scbdata - "
6467 "Unable to allocate initial scbs\n",
6473 * Note that we were successfull
6483 ahd_find_scb_by_tag(struct ahd_softc *ahd, u_int tag)
6488 * Look on the pending list.
6490 LIST_FOREACH(scb, &ahd->pending_scbs, pending_links) {
6491 if (SCB_GET_TAG(scb) == tag)
6496 * Then on all of the collision free lists.
6498 TAILQ_FOREACH(scb, &ahd->scb_data.free_scbs, links.tqe) {
6499 struct scb *list_scb;
6503 if (SCB_GET_TAG(list_scb) == tag)
6505 list_scb = LIST_NEXT(list_scb, collision_links);
6510 * And finally on the generic free list.
6512 LIST_FOREACH(scb, &ahd->scb_data.any_dev_free_scb_list, links.le) {
6513 if (SCB_GET_TAG(scb) == tag)
6521 ahd_fini_scbdata(struct ahd_softc *ahd)
6523 struct scb_data *scb_data;
6525 scb_data = &ahd->scb_data;
6526 if (scb_data == NULL)
6529 switch (scb_data->init_level) {
6533 struct map_node *sns_map;
6535 while ((sns_map = SLIST_FIRST(&scb_data->sense_maps)) != NULL) {
6536 SLIST_REMOVE_HEAD(&scb_data->sense_maps, links);
6537 ahd_dmamap_unload(ahd, scb_data->sense_dmat,
6539 ahd_dmamem_free(ahd, scb_data->sense_dmat,
6540 sns_map->vaddr, sns_map->dmamap);
6541 free(sns_map, M_DEVBUF);
6543 ahd_dma_tag_destroy(ahd, scb_data->sense_dmat);
6548 struct map_node *sg_map;
6550 while ((sg_map = SLIST_FIRST(&scb_data->sg_maps)) != NULL) {
6551 SLIST_REMOVE_HEAD(&scb_data->sg_maps, links);
6552 ahd_dmamap_unload(ahd, scb_data->sg_dmat,
6554 ahd_dmamem_free(ahd, scb_data->sg_dmat,
6555 sg_map->vaddr, sg_map->dmamap);
6556 free(sg_map, M_DEVBUF);
6558 ahd_dma_tag_destroy(ahd, scb_data->sg_dmat);
6563 struct map_node *hscb_map;
6565 while ((hscb_map = SLIST_FIRST(&scb_data->hscb_maps)) != NULL) {
6566 SLIST_REMOVE_HEAD(&scb_data->hscb_maps, links);
6567 ahd_dmamap_unload(ahd, scb_data->hscb_dmat,
6569 ahd_dmamem_free(ahd, scb_data->hscb_dmat,
6570 hscb_map->vaddr, hscb_map->dmamap);
6571 free(hscb_map, M_DEVBUF);
6573 ahd_dma_tag_destroy(ahd, scb_data->hscb_dmat);
6586 * DSP filter Bypass must be enabled until the first selection
6587 * after a change in bus mode (Razor #491 and #493).
6590 ahd_setup_iocell_workaround(struct ahd_softc *ahd)
6592 ahd_mode_state saved_modes;
6594 saved_modes = ahd_save_modes(ahd);
6595 ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
6596 ahd_outb(ahd, DSPDATACTL, ahd_inb(ahd, DSPDATACTL)
6597 | BYPASSENAB | RCVROFFSTDIS | XMITOFFSTDIS);
6598 ahd_outb(ahd, SIMODE0, ahd_inb(ahd, SIMODE0) | (ENSELDO|ENSELDI));
6600 if ((ahd_debug & AHD_SHOW_MISC) != 0)
6601 printf("%s: Setting up iocell workaround\n", ahd_name(ahd));
6603 ahd_restore_modes(ahd, saved_modes);
6604 ahd->flags &= ~AHD_HAD_FIRST_SEL;
6608 ahd_iocell_first_selection(struct ahd_softc *ahd)
6610 ahd_mode_state saved_modes;
6613 if ((ahd->flags & AHD_HAD_FIRST_SEL) != 0)
6615 saved_modes = ahd_save_modes(ahd);
6616 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
6617 sblkctl = ahd_inb(ahd, SBLKCTL);
6618 ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
6620 if ((ahd_debug & AHD_SHOW_MISC) != 0)
6621 printf("%s: iocell first selection\n", ahd_name(ahd));
6623 if ((sblkctl & ENAB40) != 0) {
6624 ahd_outb(ahd, DSPDATACTL,
6625 ahd_inb(ahd, DSPDATACTL) & ~BYPASSENAB);
6627 if ((ahd_debug & AHD_SHOW_MISC) != 0)
6628 printf("%s: BYPASS now disabled\n", ahd_name(ahd));
6631 ahd_outb(ahd, SIMODE0, ahd_inb(ahd, SIMODE0) & ~(ENSELDO|ENSELDI));
6632 ahd_outb(ahd, CLRINT, CLRSCSIINT);
6633 ahd_restore_modes(ahd, saved_modes);
6634 ahd->flags |= AHD_HAD_FIRST_SEL;
6637 /*************************** SCB Management ***********************************/
6639 ahd_add_col_list(struct ahd_softc *ahd, struct scb *scb, u_int col_idx)
6641 struct scb_list *free_list;
6642 struct scb_tailq *free_tailq;
6643 struct scb *first_scb;
6645 scb->flags |= SCB_ON_COL_LIST;
6646 AHD_SET_SCB_COL_IDX(scb, col_idx);
6647 free_list = &ahd->scb_data.free_scb_lists[col_idx];
6648 free_tailq = &ahd->scb_data.free_scbs;
6649 first_scb = LIST_FIRST(free_list);
6650 if (first_scb != NULL) {
6651 LIST_INSERT_AFTER(first_scb, scb, collision_links);
6653 LIST_INSERT_HEAD(free_list, scb, collision_links);
6654 TAILQ_INSERT_TAIL(free_tailq, scb, links.tqe);
6659 ahd_rem_col_list(struct ahd_softc *ahd, struct scb *scb)
6661 struct scb_list *free_list;
6662 struct scb_tailq *free_tailq;
6663 struct scb *first_scb;
6666 scb->flags &= ~SCB_ON_COL_LIST;
6667 col_idx = AHD_GET_SCB_COL_IDX(ahd, scb);
6668 free_list = &ahd->scb_data.free_scb_lists[col_idx];
6669 free_tailq = &ahd->scb_data.free_scbs;
6670 first_scb = LIST_FIRST(free_list);
6671 if (first_scb == scb) {
6672 struct scb *next_scb;
6675 * Maintain order in the collision free
6676 * lists for fairness if this device has
6677 * other colliding tags active.
6679 next_scb = LIST_NEXT(scb, collision_links);
6680 if (next_scb != NULL) {
6681 TAILQ_INSERT_AFTER(free_tailq, scb,
6682 next_scb, links.tqe);
6684 TAILQ_REMOVE(free_tailq, scb, links.tqe);
6686 LIST_REMOVE(scb, collision_links);
6690 * Get a free scb. If there are none, see if we can allocate a new SCB.
6693 ahd_get_scb(struct ahd_softc *ahd, u_int col_idx)
6700 TAILQ_FOREACH(scb, &ahd->scb_data.free_scbs, links.tqe) {
6701 if (AHD_GET_SCB_COL_IDX(ahd, scb) != col_idx) {
6702 ahd_rem_col_list(ahd, scb);
6706 if ((scb = LIST_FIRST(&ahd->scb_data.any_dev_free_scb_list)) == NULL) {
6710 ahd_alloc_scbs(ahd);
6713 LIST_REMOVE(scb, links.le);
6714 if (col_idx != AHD_NEVER_COL_IDX
6715 && (scb->col_scb != NULL)
6716 && (scb->col_scb->flags & SCB_ACTIVE) == 0) {
6717 LIST_REMOVE(scb->col_scb, links.le);
6718 ahd_add_col_list(ahd, scb->col_scb, col_idx);
6721 scb->flags |= SCB_ACTIVE;
6726 * Return an SCB resource to the free list.
6729 ahd_free_scb(struct ahd_softc *ahd, struct scb *scb)
6731 /* Clean up for the next user */
6732 scb->flags = SCB_FLAG_NONE;
6733 scb->hscb->control = 0;
6734 ahd->scb_data.scbindex[SCB_GET_TAG(scb)] = NULL;
6736 if (scb->col_scb == NULL) {
6739 * No collision possible. Just free normally.
6741 LIST_INSERT_HEAD(&ahd->scb_data.any_dev_free_scb_list,
6743 } else if ((scb->col_scb->flags & SCB_ON_COL_LIST) != 0) {
6746 * The SCB we might have collided with is on
6747 * a free collision list. Put both SCBs on
6750 ahd_rem_col_list(ahd, scb->col_scb);
6751 LIST_INSERT_HEAD(&ahd->scb_data.any_dev_free_scb_list,
6753 LIST_INSERT_HEAD(&ahd->scb_data.any_dev_free_scb_list,
6754 scb->col_scb, links.le);
6755 } else if ((scb->col_scb->flags
6756 & (SCB_PACKETIZED|SCB_ACTIVE)) == SCB_ACTIVE
6757 && (scb->col_scb->hscb->control & TAG_ENB) != 0) {
6760 * The SCB we might collide with on the next allocation
6761 * is still active in a non-packetized, tagged, context.
6762 * Put us on the SCB collision list.
6764 ahd_add_col_list(ahd, scb,
6765 AHD_GET_SCB_COL_IDX(ahd, scb->col_scb));
6768 * The SCB we might collide with on the next allocation
6769 * is either active in a packetized context, or free.
6770 * Since we can't collide, put this SCB on the generic
6773 LIST_INSERT_HEAD(&ahd->scb_data.any_dev_free_scb_list,
6777 ahd_platform_scb_free(ahd, scb);
6781 ahd_alloc_scbs(struct ahd_softc *ahd)
6783 struct scb_data *scb_data;
6784 struct scb *next_scb;
6785 struct hardware_scb *hscb;
6786 struct map_node *hscb_map;
6787 struct map_node *sg_map;
6788 struct map_node *sense_map;
6790 uint8_t *sense_data;
6791 dma_addr_t hscb_busaddr;
6792 dma_addr_t sg_busaddr;
6793 dma_addr_t sense_busaddr;
6797 scb_data = &ahd->scb_data;
6798 if (scb_data->numscbs >= AHD_SCB_MAX_ALLOC)
6799 /* Can't allocate any more */
6802 if (scb_data->scbs_left != 0) {
6805 offset = (PAGE_SIZE / sizeof(*hscb)) - scb_data->scbs_left;
6806 hscb_map = SLIST_FIRST(&scb_data->hscb_maps);
6807 hscb = &((struct hardware_scb *)hscb_map->vaddr)[offset];
6808 hscb_busaddr = hscb_map->physaddr + (offset * sizeof(*hscb));
6810 hscb_map = malloc(sizeof(*hscb_map), M_DEVBUF, M_NOWAIT);
6812 if (hscb_map == NULL)
6815 /* Allocate the next batch of hardware SCBs */
6816 if (ahd_dmamem_alloc(ahd, scb_data->hscb_dmat,
6817 (void **)&hscb_map->vaddr,
6818 BUS_DMA_NOWAIT, &hscb_map->dmamap) != 0) {
6819 free(hscb_map, M_DEVBUF);
6823 SLIST_INSERT_HEAD(&scb_data->hscb_maps, hscb_map, links);
6825 ahd_dmamap_load(ahd, scb_data->hscb_dmat, hscb_map->dmamap,
6826 hscb_map->vaddr, PAGE_SIZE, ahd_dmamap_cb,
6827 &hscb_map->physaddr, /*flags*/0);
6829 hscb = (struct hardware_scb *)hscb_map->vaddr;
6830 hscb_busaddr = hscb_map->physaddr;
6831 scb_data->scbs_left = PAGE_SIZE / sizeof(*hscb);
6834 if (scb_data->sgs_left != 0) {
6837 offset = ((ahd_sglist_allocsize(ahd) / ahd_sglist_size(ahd))
6838 - scb_data->sgs_left) * ahd_sglist_size(ahd);
6839 sg_map = SLIST_FIRST(&scb_data->sg_maps);
6840 segs = sg_map->vaddr + offset;
6841 sg_busaddr = sg_map->physaddr + offset;
6843 sg_map = malloc(sizeof(*sg_map), M_DEVBUF, M_NOWAIT);
6848 /* Allocate the next batch of S/G lists */
6849 if (ahd_dmamem_alloc(ahd, scb_data->sg_dmat,
6850 (void **)&sg_map->vaddr,
6851 BUS_DMA_NOWAIT, &sg_map->dmamap) != 0) {
6852 free(sg_map, M_DEVBUF);
6856 SLIST_INSERT_HEAD(&scb_data->sg_maps, sg_map, links);
6858 ahd_dmamap_load(ahd, scb_data->sg_dmat, sg_map->dmamap,
6859 sg_map->vaddr, ahd_sglist_allocsize(ahd),
6860 ahd_dmamap_cb, &sg_map->physaddr, /*flags*/0);
6862 segs = sg_map->vaddr;
6863 sg_busaddr = sg_map->physaddr;
6864 scb_data->sgs_left =
6865 ahd_sglist_allocsize(ahd) / ahd_sglist_size(ahd);
6867 if (ahd_debug & AHD_SHOW_MEMORY)
6868 printf("Mapped SG data\n");
6872 if (scb_data->sense_left != 0) {
6875 offset = PAGE_SIZE - (AHD_SENSE_BUFSIZE * scb_data->sense_left);
6876 sense_map = SLIST_FIRST(&scb_data->sense_maps);
6877 sense_data = sense_map->vaddr + offset;
6878 sense_busaddr = sense_map->physaddr + offset;
6880 sense_map = malloc(sizeof(*sense_map), M_DEVBUF, M_NOWAIT);
6882 if (sense_map == NULL)
6885 /* Allocate the next batch of sense buffers */
6886 if (ahd_dmamem_alloc(ahd, scb_data->sense_dmat,
6887 (void **)&sense_map->vaddr,
6888 BUS_DMA_NOWAIT, &sense_map->dmamap) != 0) {
6889 free(sense_map, M_DEVBUF);
6893 SLIST_INSERT_HEAD(&scb_data->sense_maps, sense_map, links);
6895 ahd_dmamap_load(ahd, scb_data->sense_dmat, sense_map->dmamap,
6896 sense_map->vaddr, PAGE_SIZE, ahd_dmamap_cb,
6897 &sense_map->physaddr, /*flags*/0);
6899 sense_data = sense_map->vaddr;
6900 sense_busaddr = sense_map->physaddr;
6901 scb_data->sense_left = PAGE_SIZE / AHD_SENSE_BUFSIZE;
6903 if (ahd_debug & AHD_SHOW_MEMORY)
6904 printf("Mapped sense data\n");
6908 newcount = min(scb_data->sense_left, scb_data->scbs_left);
6909 newcount = min(newcount, scb_data->sgs_left);
6910 newcount = min(newcount, (AHD_SCB_MAX_ALLOC - scb_data->numscbs));
6911 for (i = 0; i < newcount; i++) {
6912 struct scb_platform_data *pdata;
6918 next_scb = (struct scb *)malloc(sizeof(*next_scb),
6919 M_DEVBUF, M_NOWAIT);
6920 if (next_scb == NULL)
6923 pdata = (struct scb_platform_data *)malloc(sizeof(*pdata),
6924 M_DEVBUF, M_NOWAIT);
6925 if (pdata == NULL) {
6926 free(next_scb, M_DEVBUF);
6929 next_scb->platform_data = pdata;
6930 next_scb->hscb_map = hscb_map;
6931 next_scb->sg_map = sg_map;
6932 next_scb->sense_map = sense_map;
6933 next_scb->sg_list = segs;
6934 next_scb->sense_data = sense_data;
6935 next_scb->sense_busaddr = sense_busaddr;
6936 memset(hscb, 0, sizeof(*hscb));
6937 next_scb->hscb = hscb;
6938 hscb->hscb_busaddr = ahd_htole32(hscb_busaddr);
6941 * The sequencer always starts with the second entry.
6942 * The first entry is embedded in the scb.
6944 next_scb->sg_list_busaddr = sg_busaddr;
6945 if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0)
6946 next_scb->sg_list_busaddr
6947 += sizeof(struct ahd_dma64_seg);
6949 next_scb->sg_list_busaddr += sizeof(struct ahd_dma_seg);
6950 next_scb->ahd_softc = ahd;
6951 next_scb->flags = SCB_FLAG_NONE;
6953 error = ahd_dmamap_create(ahd, ahd->buffer_dmat, /*flags*/0,
6956 free(next_scb, M_DEVBUF);
6957 free(pdata, M_DEVBUF);
6961 next_scb->hscb->tag = ahd_htole16(scb_data->numscbs);
6962 col_tag = scb_data->numscbs ^ 0x100;
6963 next_scb->col_scb = ahd_find_scb_by_tag(ahd, col_tag);
6964 if (next_scb->col_scb != NULL)
6965 next_scb->col_scb->col_scb = next_scb;
6966 ahd_free_scb(ahd, next_scb);
6968 hscb_busaddr += sizeof(*hscb);
6969 segs += ahd_sglist_size(ahd);
6970 sg_busaddr += ahd_sglist_size(ahd);
6971 sense_data += AHD_SENSE_BUFSIZE;
6972 sense_busaddr += AHD_SENSE_BUFSIZE;
6973 scb_data->numscbs++;
6974 scb_data->sense_left--;
6975 scb_data->scbs_left--;
6976 scb_data->sgs_left--;
6981 ahd_controller_info(struct ahd_softc *ahd, char *buf)
6987 len = sprintf(buf, "%s: ", ahd_chip_names[ahd->chip & AHD_CHIPID_MASK]);
6990 speed = "Ultra320 ";
6991 if ((ahd->features & AHD_WIDE) != 0) {
6996 len = sprintf(buf, "%s%sChannel %c, SCSI Id=%d, ",
6997 speed, type, ahd->channel, ahd->our_id);
7000 sprintf(buf, "%s, %d SCBs", ahd->bus_description,
7001 ahd->scb_data.maxhscbs);
7004 static const char *channel_strings[] = {
7011 static const char *termstat_strings[] = {
7012 "Terminated Correctly",
7018 /***************************** Timer Facilities *******************************/
7019 #define ahd_timer_init init_timer
7020 #define ahd_timer_stop del_timer_sync
7021 typedef void ahd_linux_callback_t (u_long);
7024 ahd_timer_reset(ahd_timer_t *timer, int usec, ahd_callback_t *func, void *arg)
7026 struct ahd_softc *ahd;
7028 ahd = (struct ahd_softc *)arg;
7030 timer->data = (u_long)arg;
7031 timer->expires = jiffies + (usec * HZ)/1000000;
7032 timer->function = (ahd_linux_callback_t*)func;
7037 * Start the board, ready for normal operation
7040 ahd_init(struct ahd_softc *ahd)
7042 uint8_t *next_vaddr;
7043 dma_addr_t next_baddr;
7044 size_t driver_data_size;
7048 uint8_t current_sensing;
7051 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
7053 ahd->stack_size = ahd_probe_stack_size(ahd);
7054 ahd->saved_stack = malloc(ahd->stack_size * sizeof(uint16_t),
7055 M_DEVBUF, M_NOWAIT);
7056 if (ahd->saved_stack == NULL)
7060 * Verify that the compiler hasn't over-agressively
7061 * padded important structures.
7063 if (sizeof(struct hardware_scb) != 64)
7064 panic("Hardware SCB size is incorrect");
7067 if ((ahd_debug & AHD_DEBUG_SEQUENCER) != 0)
7068 ahd->flags |= AHD_SEQUENCER_DEBUG;
7072 * Default to allowing initiator operations.
7074 ahd->flags |= AHD_INITIATORROLE;
7077 * Only allow target mode features if this unit has them enabled.
7079 if ((AHD_TMODE_ENABLE & (0x1 << ahd->unit)) == 0)
7080 ahd->features &= ~AHD_TARGETMODE;
7083 /* DMA tag for mapping buffers into device visible space. */
7084 if (ahd_dma_tag_create(ahd, ahd->parent_dmat, /*alignment*/1,
7085 /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
7086 /*lowaddr*/ahd->flags & AHD_39BIT_ADDRESSING
7087 ? (dma_addr_t)0x7FFFFFFFFFULL
7088 : BUS_SPACE_MAXADDR_32BIT,
7089 /*highaddr*/BUS_SPACE_MAXADDR,
7090 /*filter*/NULL, /*filterarg*/NULL,
7091 /*maxsize*/(AHD_NSEG - 1) * PAGE_SIZE,
7092 /*nsegments*/AHD_NSEG,
7093 /*maxsegsz*/AHD_MAXTRANSFER_SIZE,
7094 /*flags*/BUS_DMA_ALLOCNOW,
7095 &ahd->buffer_dmat) != 0) {
7103 * DMA tag for our command fifos and other data in system memory
7104 * the card's sequencer must be able to access. For initiator
7105 * roles, we need to allocate space for the qoutfifo. When providing
7106 * for the target mode role, we must additionally provide space for
7107 * the incoming target command fifo.
7109 driver_data_size = AHD_SCB_MAX * sizeof(*ahd->qoutfifo)
7110 + sizeof(struct hardware_scb);
7111 if ((ahd->features & AHD_TARGETMODE) != 0)
7112 driver_data_size += AHD_TMODE_CMDS * sizeof(struct target_cmd);
7113 if ((ahd->bugs & AHD_PKT_BITBUCKET_BUG) != 0)
7114 driver_data_size += PKT_OVERRUN_BUFSIZE;
7115 if (ahd_dma_tag_create(ahd, ahd->parent_dmat, /*alignment*/1,
7116 /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
7117 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
7118 /*highaddr*/BUS_SPACE_MAXADDR,
7119 /*filter*/NULL, /*filterarg*/NULL,
7122 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
7123 /*flags*/0, &ahd->shared_data_dmat) != 0) {
7129 /* Allocation of driver data */
7130 if (ahd_dmamem_alloc(ahd, ahd->shared_data_dmat,
7131 (void **)&ahd->shared_data_map.vaddr,
7133 &ahd->shared_data_map.dmamap) != 0) {
7139 /* And permanently map it in */
7140 ahd_dmamap_load(ahd, ahd->shared_data_dmat, ahd->shared_data_map.dmamap,
7141 ahd->shared_data_map.vaddr, driver_data_size,
7142 ahd_dmamap_cb, &ahd->shared_data_map.physaddr,
7144 ahd->qoutfifo = (struct ahd_completion *)ahd->shared_data_map.vaddr;
7145 next_vaddr = (uint8_t *)&ahd->qoutfifo[AHD_QOUT_SIZE];
7146 next_baddr = ahd->shared_data_map.physaddr
7147 + AHD_QOUT_SIZE*sizeof(struct ahd_completion);
7148 if ((ahd->features & AHD_TARGETMODE) != 0) {
7149 ahd->targetcmds = (struct target_cmd *)next_vaddr;
7150 next_vaddr += AHD_TMODE_CMDS * sizeof(struct target_cmd);
7151 next_baddr += AHD_TMODE_CMDS * sizeof(struct target_cmd);
7154 if ((ahd->bugs & AHD_PKT_BITBUCKET_BUG) != 0) {
7155 ahd->overrun_buf = next_vaddr;
7156 next_vaddr += PKT_OVERRUN_BUFSIZE;
7157 next_baddr += PKT_OVERRUN_BUFSIZE;
7161 * We need one SCB to serve as the "next SCB". Since the
7162 * tag identifier in this SCB will never be used, there is
7163 * no point in using a valid HSCB tag from an SCB pulled from
7164 * the standard free pool. So, we allocate this "sentinel"
7165 * specially from the DMA safe memory chunk used for the QOUTFIFO.
7167 ahd->next_queued_hscb = (struct hardware_scb *)next_vaddr;
7168 ahd->next_queued_hscb_map = &ahd->shared_data_map;
7169 ahd->next_queued_hscb->hscb_busaddr = ahd_htole32(next_baddr);
7173 /* Allocate SCB data now that buffer_dmat is initialized */
7174 if (ahd_init_scbdata(ahd) != 0)
7177 if ((ahd->flags & AHD_INITIATORROLE) == 0)
7178 ahd->flags &= ~AHD_RESET_BUS_A;
7181 * Before committing these settings to the chip, give
7182 * the OSM one last chance to modify our configuration.
7184 ahd_platform_init(ahd);
7186 /* Bring up the chip. */
7189 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
7191 if ((ahd->flags & AHD_CURRENT_SENSING) == 0)
7195 * Verify termination based on current draw and
7196 * warn user if the bus is over/under terminated.
7198 error = ahd_write_flexport(ahd, FLXADDR_ROMSTAT_CURSENSECTL,
7201 printf("%s: current sensing timeout 1\n", ahd_name(ahd));
7204 for (i = 20, fstat = FLX_FSTAT_BUSY;
7205 (fstat & FLX_FSTAT_BUSY) != 0 && i; i--) {
7206 error = ahd_read_flexport(ahd, FLXADDR_FLEXSTAT, &fstat);
7208 printf("%s: current sensing timeout 2\n",
7214 printf("%s: Timedout during current-sensing test\n",
7219 /* Latch Current Sensing status. */
7220 error = ahd_read_flexport(ahd, FLXADDR_CURRENT_STAT, ¤t_sensing);
7222 printf("%s: current sensing timeout 3\n", ahd_name(ahd));
7226 /* Diable current sensing. */
7227 ahd_write_flexport(ahd, FLXADDR_ROMSTAT_CURSENSECTL, 0);
7230 if ((ahd_debug & AHD_SHOW_TERMCTL) != 0) {
7231 printf("%s: current_sensing == 0x%x\n",
7232 ahd_name(ahd), current_sensing);
7236 for (i = 0; i < 4; i++, current_sensing >>= FLX_CSTAT_SHIFT) {
7239 term_stat = (current_sensing & FLX_CSTAT_MASK);
7240 switch (term_stat) {
7241 case FLX_CSTAT_OVER:
7242 case FLX_CSTAT_UNDER:
7244 case FLX_CSTAT_INVALID:
7245 case FLX_CSTAT_OKAY:
7246 if (warn_user == 0 && bootverbose == 0)
7248 printf("%s: %s Channel %s\n", ahd_name(ahd),
7249 channel_strings[i], termstat_strings[term_stat]);
7254 printf("%s: WARNING. Termination is not configured correctly.\n"
7255 "%s: WARNING. SCSI bus operations may FAIL.\n",
7256 ahd_name(ahd), ahd_name(ahd));
7260 ahd_timer_reset(&ahd->stat_timer, AHD_STAT_UPDATE_US,
7261 ahd_stat_timer, ahd);
7266 * (Re)initialize chip state after a chip reset.
7269 ahd_chip_init(struct ahd_softc *ahd)
7273 u_int scsiseq_template;
7278 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
7280 * Take the LED out of diagnostic mode
7282 ahd_outb(ahd, SBLKCTL, ahd_inb(ahd, SBLKCTL) & ~(DIAGLEDEN|DIAGLEDON));
7285 * Return HS_MAILBOX to its default value.
7287 ahd->hs_mailbox = 0;
7288 ahd_outb(ahd, HS_MAILBOX, 0);
7290 /* Set the SCSI Id, SXFRCTL0, SXFRCTL1, and SIMODE1. */
7291 ahd_outb(ahd, IOWNID, ahd->our_id);
7292 ahd_outb(ahd, TOWNID, ahd->our_id);
7293 sxfrctl1 = (ahd->flags & AHD_TERM_ENB_A) != 0 ? STPWEN : 0;
7294 sxfrctl1 |= (ahd->flags & AHD_SPCHK_ENB_A) != 0 ? ENSPCHK : 0;
7295 if ((ahd->bugs & AHD_LONG_SETIMO_BUG)
7296 && (ahd->seltime != STIMESEL_MIN)) {
7298 * The selection timer duration is twice as long
7299 * as it should be. Halve it by adding "1" to
7300 * the user specified setting.
7302 sxfrctl1 |= ahd->seltime + STIMESEL_BUG_ADJ;
7304 sxfrctl1 |= ahd->seltime;
7307 ahd_outb(ahd, SXFRCTL0, DFON);
7308 ahd_outb(ahd, SXFRCTL1, sxfrctl1|ahd->seltime|ENSTIMER|ACTNEGEN);
7309 ahd_outb(ahd, SIMODE1, ENSELTIMO|ENSCSIRST|ENSCSIPERR);
7312 * Now that termination is set, wait for up
7313 * to 500ms for our transceivers to settle. If
7314 * the adapter does not have a cable attached,
7315 * the transceivers may never settle, so don't
7316 * complain if we fail here.
7319 (ahd_inb(ahd, SBLKCTL) & (ENAB40|ENAB20)) == 0 && wait;
7323 /* Clear any false bus resets due to the transceivers settling */
7324 ahd_outb(ahd, CLRSINT1, CLRSCSIRSTI);
7325 ahd_outb(ahd, CLRINT, CLRSCSIINT);
7327 /* Initialize mode specific S/G state. */
7328 for (i = 0; i < 2; i++) {
7329 ahd_set_modes(ahd, AHD_MODE_DFF0 + i, AHD_MODE_DFF0 + i);
7330 ahd_outb(ahd, LONGJMP_ADDR + 1, INVALID_ADDR);
7331 ahd_outb(ahd, SG_STATE, 0);
7332 ahd_outb(ahd, CLRSEQINTSRC, 0xFF);
7333 ahd_outb(ahd, SEQIMODE,
7334 ENSAVEPTRS|ENCFG4DATA|ENCFG4ISTAT
7335 |ENCFG4TSTAT|ENCFG4ICMD|ENCFG4TCMD);
7338 ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
7339 ahd_outb(ahd, DSCOMMAND0, ahd_inb(ahd, DSCOMMAND0)|MPARCKEN|CACHETHEN);
7340 ahd_outb(ahd, DFF_THRSH, RD_DFTHRSH_75|WR_DFTHRSH_75);
7341 ahd_outb(ahd, SIMODE0, ENIOERR|ENOVERRUN);
7342 ahd_outb(ahd, SIMODE3, ENNTRAMPERR|ENOSRAMPERR);
7343 if ((ahd->bugs & AHD_BUSFREEREV_BUG) != 0) {
7344 ahd_outb(ahd, OPTIONMODE, AUTOACKEN|AUTO_MSGOUT_DE);
7346 ahd_outb(ahd, OPTIONMODE, AUTOACKEN|BUSFREEREV|AUTO_MSGOUT_DE);
7348 ahd_outb(ahd, SCSCHKN, CURRFIFODEF|WIDERESEN|SHVALIDSTDIS);
7349 if ((ahd->chip & AHD_BUS_MASK) == AHD_PCIX)
7351 * Do not issue a target abort when a split completion
7352 * error occurs. Let our PCIX interrupt handler deal
7353 * with it instead. H2A4 Razor #625
7355 ahd_outb(ahd, PCIXCTL, ahd_inb(ahd, PCIXCTL) | SPLTSTADIS);
7357 if ((ahd->bugs & AHD_LQOOVERRUN_BUG) != 0)
7358 ahd_outb(ahd, LQOSCSCTL, LQONOCHKOVER);
7361 * Tweak IOCELL settings.
7363 if ((ahd->flags & AHD_HP_BOARD) != 0) {
7364 for (i = 0; i < NUMDSPS; i++) {
7365 ahd_outb(ahd, DSPSELECT, i);
7366 ahd_outb(ahd, WRTBIASCTL, WRTBIASCTL_HP_DEFAULT);
7369 if ((ahd_debug & AHD_SHOW_MISC) != 0)
7370 printf("%s: WRTBIASCTL now 0x%x\n", ahd_name(ahd),
7371 WRTBIASCTL_HP_DEFAULT);
7374 ahd_setup_iocell_workaround(ahd);
7377 * Enable LQI Manager interrupts.
7379 ahd_outb(ahd, LQIMODE1, ENLQIPHASE_LQ|ENLQIPHASE_NLQ|ENLIQABORT
7380 | ENLQICRCI_LQ|ENLQICRCI_NLQ|ENLQIBADLQI
7381 | ENLQIOVERI_LQ|ENLQIOVERI_NLQ);
7382 ahd_outb(ahd, LQOMODE0, ENLQOATNLQ|ENLQOATNPKT|ENLQOTCRC);
7384 * We choose to have the sequencer catch LQOPHCHGINPKT errors
7385 * manually for the command phase at the start of a packetized
7386 * selection case. ENLQOBUSFREE should be made redundant by
7387 * the BUSFREE interrupt, but it seems that some LQOBUSFREE
7388 * events fail to assert the BUSFREE interrupt so we must
7389 * also enable LQOBUSFREE interrupts.
7391 ahd_outb(ahd, LQOMODE1, ENLQOBUSFREE);
7394 * Setup sequencer interrupt handlers.
7396 ahd_outw(ahd, INTVEC1_ADDR, ahd_resolve_seqaddr(ahd, LABEL_seq_isr));
7397 ahd_outw(ahd, INTVEC2_ADDR, ahd_resolve_seqaddr(ahd, LABEL_timer_isr));
7400 * Setup SCB Offset registers.
7402 if ((ahd->bugs & AHD_PKT_LUN_BUG) != 0) {
7403 ahd_outb(ahd, LUNPTR, offsetof(struct hardware_scb,
7406 ahd_outb(ahd, LUNPTR, offsetof(struct hardware_scb, lun));
7408 ahd_outb(ahd, CMDLENPTR, offsetof(struct hardware_scb, cdb_len));
7409 ahd_outb(ahd, ATTRPTR, offsetof(struct hardware_scb, task_attribute));
7410 ahd_outb(ahd, FLAGPTR, offsetof(struct hardware_scb, task_management));
7411 ahd_outb(ahd, CMDPTR, offsetof(struct hardware_scb,
7412 shared_data.idata.cdb));
7413 ahd_outb(ahd, QNEXTPTR,
7414 offsetof(struct hardware_scb, next_hscb_busaddr));
7415 ahd_outb(ahd, ABRTBITPTR, MK_MESSAGE_BIT_OFFSET);
7416 ahd_outb(ahd, ABRTBYTEPTR, offsetof(struct hardware_scb, control));
7417 if ((ahd->bugs & AHD_PKT_LUN_BUG) != 0) {
7418 ahd_outb(ahd, LUNLEN,
7419 sizeof(ahd->next_queued_hscb->pkt_long_lun) - 1);
7421 ahd_outb(ahd, LUNLEN, LUNLEN_SINGLE_LEVEL_LUN);
7423 ahd_outb(ahd, CDBLIMIT, SCB_CDB_LEN_PTR - 1);
7424 ahd_outb(ahd, MAXCMD, 0xFF);
7425 ahd_outb(ahd, SCBAUTOPTR,
7426 AUSCBPTR_EN | offsetof(struct hardware_scb, tag));
7428 /* We haven't been enabled for target mode yet. */
7429 ahd_outb(ahd, MULTARGID, 0);
7430 ahd_outb(ahd, MULTARGID + 1, 0);
7432 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
7433 /* Initialize the negotiation table. */
7434 if ((ahd->features & AHD_NEW_IOCELL_OPTS) == 0) {
7436 * Clear the spare bytes in the neg table to avoid
7437 * spurious parity errors.
7439 for (target = 0; target < AHD_NUM_TARGETS; target++) {
7440 ahd_outb(ahd, NEGOADDR, target);
7441 ahd_outb(ahd, ANNEXCOL, AHD_ANNEXCOL_PER_DEV0);
7442 for (i = 0; i < AHD_NUM_PER_DEV_ANNEXCOLS; i++)
7443 ahd_outb(ahd, ANNEXDAT, 0);
7446 for (target = 0; target < AHD_NUM_TARGETS; target++) {
7447 struct ahd_devinfo devinfo;
7448 struct ahd_initiator_tinfo *tinfo;
7449 struct ahd_tmode_tstate *tstate;
7451 tinfo = ahd_fetch_transinfo(ahd, 'A', ahd->our_id,
7453 ahd_compile_devinfo(&devinfo, ahd->our_id,
7454 target, CAM_LUN_WILDCARD,
7455 'A', ROLE_INITIATOR);
7456 ahd_update_neg_table(ahd, &devinfo, &tinfo->curr);
7459 ahd_outb(ahd, CLRSINT3, NTRAMPERR|OSRAMPERR);
7460 ahd_outb(ahd, CLRINT, CLRSCSIINT);
7462 #ifdef NEEDS_MORE_TESTING
7464 * Always enable abort on incoming L_Qs if this feature is
7465 * supported. We use this to catch invalid SCB references.
7467 if ((ahd->bugs & AHD_ABORT_LQI_BUG) == 0)
7468 ahd_outb(ahd, LQCTL1, ABORTPENDING);
7471 ahd_outb(ahd, LQCTL1, 0);
7473 /* All of our queues are empty */
7474 ahd->qoutfifonext = 0;
7475 ahd->qoutfifonext_valid_tag = QOUTFIFO_ENTRY_VALID;
7476 ahd_outb(ahd, QOUTFIFO_ENTRY_VALID_TAG, QOUTFIFO_ENTRY_VALID);
7477 for (i = 0; i < AHD_QOUT_SIZE; i++)
7478 ahd->qoutfifo[i].valid_tag = 0;
7479 ahd_sync_qoutfifo(ahd, BUS_DMASYNC_PREREAD);
7481 ahd->qinfifonext = 0;
7482 for (i = 0; i < AHD_QIN_SIZE; i++)
7483 ahd->qinfifo[i] = SCB_LIST_NULL;
7485 if ((ahd->features & AHD_TARGETMODE) != 0) {
7486 /* All target command blocks start out invalid. */
7487 for (i = 0; i < AHD_TMODE_CMDS; i++)
7488 ahd->targetcmds[i].cmd_valid = 0;
7489 ahd_sync_tqinfifo(ahd, BUS_DMASYNC_PREREAD);
7490 ahd->tqinfifonext = 1;
7491 ahd_outb(ahd, KERNEL_TQINPOS, ahd->tqinfifonext - 1);
7492 ahd_outb(ahd, TQINPOS, ahd->tqinfifonext);
7495 /* Initialize Scratch Ram. */
7496 ahd_outb(ahd, SEQ_FLAGS, 0);
7497 ahd_outb(ahd, SEQ_FLAGS2, 0);
7499 /* We don't have any waiting selections */
7500 ahd_outw(ahd, WAITING_TID_HEAD, SCB_LIST_NULL);
7501 ahd_outw(ahd, WAITING_TID_TAIL, SCB_LIST_NULL);
7502 ahd_outw(ahd, MK_MESSAGE_SCB, SCB_LIST_NULL);
7503 ahd_outw(ahd, MK_MESSAGE_SCSIID, 0xFF);
7504 for (i = 0; i < AHD_NUM_TARGETS; i++)
7505 ahd_outw(ahd, WAITING_SCB_TAILS + (2 * i), SCB_LIST_NULL);
7508 * Nobody is waiting to be DMAed into the QOUTFIFO.
7510 ahd_outw(ahd, COMPLETE_SCB_HEAD, SCB_LIST_NULL);
7511 ahd_outw(ahd, COMPLETE_SCB_DMAINPROG_HEAD, SCB_LIST_NULL);
7512 ahd_outw(ahd, COMPLETE_DMA_SCB_HEAD, SCB_LIST_NULL);
7513 ahd_outw(ahd, COMPLETE_DMA_SCB_TAIL, SCB_LIST_NULL);
7514 ahd_outw(ahd, COMPLETE_ON_QFREEZE_HEAD, SCB_LIST_NULL);
7517 * The Freeze Count is 0.
7519 ahd->qfreeze_cnt = 0;
7520 ahd_outw(ahd, QFREEZE_COUNT, 0);
7521 ahd_outw(ahd, KERNEL_QFREEZE_COUNT, 0);
7524 * Tell the sequencer where it can find our arrays in memory.
7526 busaddr = ahd->shared_data_map.physaddr;
7527 ahd_outl(ahd, SHARED_DATA_ADDR, busaddr);
7528 ahd_outl(ahd, QOUTFIFO_NEXT_ADDR, busaddr);
7531 * Setup the allowed SCSI Sequences based on operational mode.
7532 * If we are a target, we'll enable select in operations once
7533 * we've had a lun enabled.
7535 scsiseq_template = ENAUTOATNP;
7536 if ((ahd->flags & AHD_INITIATORROLE) != 0)
7537 scsiseq_template |= ENRSELI;
7538 ahd_outb(ahd, SCSISEQ_TEMPLATE, scsiseq_template);
7540 /* There are no busy SCBs yet. */
7541 for (target = 0; target < AHD_NUM_TARGETS; target++) {
7544 for (lun = 0; lun < AHD_NUM_LUNS_NONPKT; lun++)
7545 ahd_unbusy_tcl(ahd, BUILD_TCL_RAW(target, 'A', lun));
7549 * Initialize the group code to command length table.
7550 * Vendor Unique codes are set to 0 so we only capture
7551 * the first byte of the cdb. These can be overridden
7552 * when target mode is enabled.
7554 ahd_outb(ahd, CMDSIZE_TABLE, 5);
7555 ahd_outb(ahd, CMDSIZE_TABLE + 1, 9);
7556 ahd_outb(ahd, CMDSIZE_TABLE + 2, 9);
7557 ahd_outb(ahd, CMDSIZE_TABLE + 3, 0);
7558 ahd_outb(ahd, CMDSIZE_TABLE + 4, 15);
7559 ahd_outb(ahd, CMDSIZE_TABLE + 5, 11);
7560 ahd_outb(ahd, CMDSIZE_TABLE + 6, 0);
7561 ahd_outb(ahd, CMDSIZE_TABLE + 7, 0);
7563 /* Tell the sequencer of our initial queue positions */
7564 ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
7565 ahd_outb(ahd, QOFF_CTLSTA, SCB_QSIZE_512);
7566 ahd->qinfifonext = 0;
7567 ahd_set_hnscb_qoff(ahd, ahd->qinfifonext);
7568 ahd_set_hescb_qoff(ahd, 0);
7569 ahd_set_snscb_qoff(ahd, 0);
7570 ahd_set_sescb_qoff(ahd, 0);
7571 ahd_set_sdscb_qoff(ahd, 0);
7574 * Tell the sequencer which SCB will be the next one it receives.
7576 busaddr = ahd_le32toh(ahd->next_queued_hscb->hscb_busaddr);
7577 ahd_outl(ahd, NEXT_QUEUED_SCB_ADDR, busaddr);
7580 * Default to coalescing disabled.
7582 ahd_outw(ahd, INT_COALESCING_CMDCOUNT, 0);
7583 ahd_outw(ahd, CMDS_PENDING, 0);
7584 ahd_update_coalescing_values(ahd, ahd->int_coalescing_timer,
7585 ahd->int_coalescing_maxcmds,
7586 ahd->int_coalescing_mincmds);
7587 ahd_enable_coalescing(ahd, FALSE);
7590 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
7592 if (ahd->features & AHD_AIC79XXB_SLOWCRC) {
7593 u_int negodat3 = ahd_inb(ahd, NEGCONOPTS);
7595 negodat3 |= ENSLOWCRC;
7596 ahd_outb(ahd, NEGCONOPTS, negodat3);
7597 negodat3 = ahd_inb(ahd, NEGCONOPTS);
7598 if (!(negodat3 & ENSLOWCRC))
7599 printf("aic79xx: failed to set the SLOWCRC bit\n");
7601 printf("aic79xx: SLOWCRC bit set\n");
7606 * Setup default device and controller settings.
7607 * This should only be called if our probe has
7608 * determined that no configuration data is available.
7611 ahd_default_config(struct ahd_softc *ahd)
7618 * Allocate a tstate to house information for our
7619 * initiator presence on the bus as well as the user
7620 * data for any target mode initiator.
7622 if (ahd_alloc_tstate(ahd, ahd->our_id, 'A') == NULL) {
7623 printf("%s: unable to allocate ahd_tmode_tstate. "
7624 "Failing attach\n", ahd_name(ahd));
7628 for (targ = 0; targ < AHD_NUM_TARGETS; targ++) {
7629 struct ahd_devinfo devinfo;
7630 struct ahd_initiator_tinfo *tinfo;
7631 struct ahd_tmode_tstate *tstate;
7632 uint16_t target_mask;
7634 tinfo = ahd_fetch_transinfo(ahd, 'A', ahd->our_id,
7637 * We support SPC2 and SPI4.
7639 tinfo->user.protocol_version = 4;
7640 tinfo->user.transport_version = 4;
7642 target_mask = 0x01 << targ;
7643 ahd->user_discenable |= target_mask;
7644 tstate->discenable |= target_mask;
7645 ahd->user_tagenable |= target_mask;
7646 #ifdef AHD_FORCE_160
7647 tinfo->user.period = AHD_SYNCRATE_DT;
7649 tinfo->user.period = AHD_SYNCRATE_160;
7651 tinfo->user.offset = MAX_OFFSET;
7652 tinfo->user.ppr_options = MSG_EXT_PPR_RD_STRM
7653 | MSG_EXT_PPR_WR_FLOW
7654 | MSG_EXT_PPR_HOLD_MCS
7655 | MSG_EXT_PPR_IU_REQ
7656 | MSG_EXT_PPR_QAS_REQ
7657 | MSG_EXT_PPR_DT_REQ;
7658 if ((ahd->features & AHD_RTI) != 0)
7659 tinfo->user.ppr_options |= MSG_EXT_PPR_RTI;
7661 tinfo->user.width = MSG_EXT_WDTR_BUS_16_BIT;
7664 * Start out Async/Narrow/Untagged and with
7665 * conservative protocol support.
7667 tinfo->goal.protocol_version = 2;
7668 tinfo->goal.transport_version = 2;
7669 tinfo->curr.protocol_version = 2;
7670 tinfo->curr.transport_version = 2;
7671 ahd_compile_devinfo(&devinfo, ahd->our_id,
7672 targ, CAM_LUN_WILDCARD,
7673 'A', ROLE_INITIATOR);
7674 tstate->tagenable &= ~target_mask;
7675 ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
7676 AHD_TRANS_CUR|AHD_TRANS_GOAL, /*paused*/TRUE);
7677 ahd_set_syncrate(ahd, &devinfo, /*period*/0, /*offset*/0,
7678 /*ppr_options*/0, AHD_TRANS_CUR|AHD_TRANS_GOAL,
7685 * Parse device configuration information.
7688 ahd_parse_cfgdata(struct ahd_softc *ahd, struct seeprom_config *sc)
7693 max_targ = sc->max_targets & CFMAXTARG;
7694 ahd->our_id = sc->brtime_id & CFSCSIID;
7697 * Allocate a tstate to house information for our
7698 * initiator presence on the bus as well as the user
7699 * data for any target mode initiator.
7701 if (ahd_alloc_tstate(ahd, ahd->our_id, 'A') == NULL) {
7702 printf("%s: unable to allocate ahd_tmode_tstate. "
7703 "Failing attach\n", ahd_name(ahd));
7707 for (targ = 0; targ < max_targ; targ++) {
7708 struct ahd_devinfo devinfo;
7709 struct ahd_initiator_tinfo *tinfo;
7710 struct ahd_transinfo *user_tinfo;
7711 struct ahd_tmode_tstate *tstate;
7712 uint16_t target_mask;
7714 tinfo = ahd_fetch_transinfo(ahd, 'A', ahd->our_id,
7716 user_tinfo = &tinfo->user;
7719 * We support SPC2 and SPI4.
7721 tinfo->user.protocol_version = 4;
7722 tinfo->user.transport_version = 4;
7724 target_mask = 0x01 << targ;
7725 ahd->user_discenable &= ~target_mask;
7726 tstate->discenable &= ~target_mask;
7727 ahd->user_tagenable &= ~target_mask;
7728 if (sc->device_flags[targ] & CFDISC) {
7729 tstate->discenable |= target_mask;
7730 ahd->user_discenable |= target_mask;
7731 ahd->user_tagenable |= target_mask;
7734 * Cannot be packetized without disconnection.
7736 sc->device_flags[targ] &= ~CFPACKETIZED;
7739 user_tinfo->ppr_options = 0;
7740 user_tinfo->period = (sc->device_flags[targ] & CFXFER);
7741 if (user_tinfo->period < CFXFER_ASYNC) {
7742 if (user_tinfo->period <= AHD_PERIOD_10MHz)
7743 user_tinfo->ppr_options |= MSG_EXT_PPR_DT_REQ;
7744 user_tinfo->offset = MAX_OFFSET;
7746 user_tinfo->offset = 0;
7747 user_tinfo->period = AHD_ASYNC_XFER_PERIOD;
7749 #ifdef AHD_FORCE_160
7750 if (user_tinfo->period <= AHD_SYNCRATE_160)
7751 user_tinfo->period = AHD_SYNCRATE_DT;
7754 if ((sc->device_flags[targ] & CFPACKETIZED) != 0) {
7755 user_tinfo->ppr_options |= MSG_EXT_PPR_RD_STRM
7756 | MSG_EXT_PPR_WR_FLOW
7757 | MSG_EXT_PPR_HOLD_MCS
7758 | MSG_EXT_PPR_IU_REQ;
7759 if ((ahd->features & AHD_RTI) != 0)
7760 user_tinfo->ppr_options |= MSG_EXT_PPR_RTI;
7763 if ((sc->device_flags[targ] & CFQAS) != 0)
7764 user_tinfo->ppr_options |= MSG_EXT_PPR_QAS_REQ;
7766 if ((sc->device_flags[targ] & CFWIDEB) != 0)
7767 user_tinfo->width = MSG_EXT_WDTR_BUS_16_BIT;
7769 user_tinfo->width = MSG_EXT_WDTR_BUS_8_BIT;
7771 if ((ahd_debug & AHD_SHOW_MISC) != 0)
7772 printf("(%d): %x:%x:%x:%x\n", targ, user_tinfo->width,
7773 user_tinfo->period, user_tinfo->offset,
7774 user_tinfo->ppr_options);
7777 * Start out Async/Narrow/Untagged and with
7778 * conservative protocol support.
7780 tstate->tagenable &= ~target_mask;
7781 tinfo->goal.protocol_version = 2;
7782 tinfo->goal.transport_version = 2;
7783 tinfo->curr.protocol_version = 2;
7784 tinfo->curr.transport_version = 2;
7785 ahd_compile_devinfo(&devinfo, ahd->our_id,
7786 targ, CAM_LUN_WILDCARD,
7787 'A', ROLE_INITIATOR);
7788 ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
7789 AHD_TRANS_CUR|AHD_TRANS_GOAL, /*paused*/TRUE);
7790 ahd_set_syncrate(ahd, &devinfo, /*period*/0, /*offset*/0,
7791 /*ppr_options*/0, AHD_TRANS_CUR|AHD_TRANS_GOAL,
7795 ahd->flags &= ~AHD_SPCHK_ENB_A;
7796 if (sc->bios_control & CFSPARITY)
7797 ahd->flags |= AHD_SPCHK_ENB_A;
7799 ahd->flags &= ~AHD_RESET_BUS_A;
7800 if (sc->bios_control & CFRESETB)
7801 ahd->flags |= AHD_RESET_BUS_A;
7803 ahd->flags &= ~AHD_EXTENDED_TRANS_A;
7804 if (sc->bios_control & CFEXTEND)
7805 ahd->flags |= AHD_EXTENDED_TRANS_A;
7807 ahd->flags &= ~AHD_BIOS_ENABLED;
7808 if ((sc->bios_control & CFBIOSSTATE) == CFBS_ENABLED)
7809 ahd->flags |= AHD_BIOS_ENABLED;
7811 ahd->flags &= ~AHD_STPWLEVEL_A;
7812 if ((sc->adapter_control & CFSTPWLEVEL) != 0)
7813 ahd->flags |= AHD_STPWLEVEL_A;
7819 * Parse device configuration information.
7822 ahd_parse_vpddata(struct ahd_softc *ahd, struct vpd_config *vpd)
7826 error = ahd_verify_vpd_cksum(vpd);
7829 if ((vpd->bios_flags & VPDBOOTHOST) != 0)
7830 ahd->flags |= AHD_BOOT_CHANNEL;
7835 ahd_intr_enable(struct ahd_softc *ahd, int enable)
7839 hcntrl = ahd_inb(ahd, HCNTRL);
7841 ahd->pause &= ~INTEN;
7842 ahd->unpause &= ~INTEN;
7845 ahd->pause |= INTEN;
7846 ahd->unpause |= INTEN;
7848 ahd_outb(ahd, HCNTRL, hcntrl);
7852 ahd_update_coalescing_values(struct ahd_softc *ahd, u_int timer, u_int maxcmds,
7855 if (timer > AHD_TIMER_MAX_US)
7856 timer = AHD_TIMER_MAX_US;
7857 ahd->int_coalescing_timer = timer;
7859 if (maxcmds > AHD_INT_COALESCING_MAXCMDS_MAX)
7860 maxcmds = AHD_INT_COALESCING_MAXCMDS_MAX;
7861 if (mincmds > AHD_INT_COALESCING_MINCMDS_MAX)
7862 mincmds = AHD_INT_COALESCING_MINCMDS_MAX;
7863 ahd->int_coalescing_maxcmds = maxcmds;
7864 ahd_outw(ahd, INT_COALESCING_TIMER, timer / AHD_TIMER_US_PER_TICK);
7865 ahd_outb(ahd, INT_COALESCING_MAXCMDS, -maxcmds);
7866 ahd_outb(ahd, INT_COALESCING_MINCMDS, -mincmds);
7870 ahd_enable_coalescing(struct ahd_softc *ahd, int enable)
7873 ahd->hs_mailbox &= ~ENINT_COALESCE;
7875 ahd->hs_mailbox |= ENINT_COALESCE;
7876 ahd_outb(ahd, HS_MAILBOX, ahd->hs_mailbox);
7877 ahd_flush_device_writes(ahd);
7878 ahd_run_qoutfifo(ahd);
7882 * Ensure that the card is paused in a location
7883 * outside of all critical sections and that all
7884 * pending work is completed prior to returning.
7885 * This routine should only be called from outside
7886 * an interrupt context.
7889 ahd_pause_and_flushwork(struct ahd_softc *ahd)
7895 ahd->flags |= AHD_ALL_INTERRUPTS;
7898 * Freeze the outgoing selections. We do this only
7899 * until we are safely paused without further selections
7903 ahd_outw(ahd, KERNEL_QFREEZE_COUNT, ahd->qfreeze_cnt);
7904 ahd_outb(ahd, SEQ_FLAGS2, ahd_inb(ahd, SEQ_FLAGS2) | SELECTOUT_QFROZEN);
7909 * Give the sequencer some time to service
7910 * any active selections.
7916 intstat = ahd_inb(ahd, INTSTAT);
7917 if ((intstat & INT_PEND) == 0) {
7918 ahd_clear_critical_section(ahd);
7919 intstat = ahd_inb(ahd, INTSTAT);
7922 && (intstat != 0xFF || (ahd->features & AHD_REMOVABLE) == 0)
7923 && ((intstat & INT_PEND) != 0
7924 || (ahd_inb(ahd, SCSISEQ0) & ENSELO) != 0
7925 || (ahd_inb(ahd, SSTAT0) & (SELDO|SELINGO)) != 0));
7927 if (maxloops == 0) {
7928 printf("Infinite interrupt loop, INTSTAT = %x",
7929 ahd_inb(ahd, INTSTAT));
7932 ahd_outw(ahd, KERNEL_QFREEZE_COUNT, ahd->qfreeze_cnt);
7934 ahd_flush_qoutfifo(ahd);
7936 ahd->flags &= ~AHD_ALL_INTERRUPTS;
7941 ahd_suspend(struct ahd_softc *ahd)
7944 ahd_pause_and_flushwork(ahd);
7946 if (LIST_FIRST(&ahd->pending_scbs) != NULL) {
7955 ahd_resume(struct ahd_softc *ahd)
7958 ahd_reset(ahd, /*reinit*/TRUE);
7959 ahd_intr_enable(ahd, TRUE);
7964 /************************** Busy Target Table *********************************/
7966 * Set SCBPTR to the SCB that contains the busy
7967 * table entry for TCL. Return the offset into
7968 * the SCB that contains the entry for TCL.
7969 * saved_scbid is dereferenced and set to the
7970 * scbid that should be restored once manipualtion
7971 * of the TCL entry is complete.
7973 static __inline u_int
7974 ahd_index_busy_tcl(struct ahd_softc *ahd, u_int *saved_scbid, u_int tcl)
7977 * Index to the SCB that contains the busy entry.
7979 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
7980 *saved_scbid = ahd_get_scbptr(ahd);
7981 ahd_set_scbptr(ahd, TCL_LUN(tcl)
7982 | ((TCL_TARGET_OFFSET(tcl) & 0xC) << 4));
7985 * And now calculate the SCB offset to the entry.
7986 * Each entry is 2 bytes wide, hence the
7987 * multiplication by 2.
7989 return (((TCL_TARGET_OFFSET(tcl) & 0x3) << 1) + SCB_DISCONNECTED_LISTS);
7993 * Return the untagged transaction id for a given target/channel lun.
7996 ahd_find_busy_tcl(struct ahd_softc *ahd, u_int tcl)
8002 scb_offset = ahd_index_busy_tcl(ahd, &saved_scbptr, tcl);
8003 scbid = ahd_inw_scbram(ahd, scb_offset);
8004 ahd_set_scbptr(ahd, saved_scbptr);
8009 ahd_busy_tcl(struct ahd_softc *ahd, u_int tcl, u_int scbid)
8014 scb_offset = ahd_index_busy_tcl(ahd, &saved_scbptr, tcl);
8015 ahd_outw(ahd, scb_offset, scbid);
8016 ahd_set_scbptr(ahd, saved_scbptr);
8019 /************************** SCB and SCB queue management **********************/
8021 ahd_match_scb(struct ahd_softc *ahd, struct scb *scb, int target,
8022 char channel, int lun, u_int tag, role_t role)
8024 int targ = SCB_GET_TARGET(ahd, scb);
8025 char chan = SCB_GET_CHANNEL(ahd, scb);
8026 int slun = SCB_GET_LUN(scb);
8029 match = ((chan == channel) || (channel == ALL_CHANNELS));
8031 match = ((targ == target) || (target == CAM_TARGET_WILDCARD));
8033 match = ((lun == slun) || (lun == CAM_LUN_WILDCARD));
8035 #ifdef AHD_TARGET_MODE
8038 group = XPT_FC_GROUP(scb->io_ctx->ccb_h.func_code);
8039 if (role == ROLE_INITIATOR) {
8040 match = (group != XPT_FC_GROUP_TMODE)
8041 && ((tag == SCB_GET_TAG(scb))
8042 || (tag == SCB_LIST_NULL));
8043 } else if (role == ROLE_TARGET) {
8044 match = (group == XPT_FC_GROUP_TMODE)
8045 && ((tag == scb->io_ctx->csio.tag_id)
8046 || (tag == SCB_LIST_NULL));
8048 #else /* !AHD_TARGET_MODE */
8049 match = ((tag == SCB_GET_TAG(scb)) || (tag == SCB_LIST_NULL));
8050 #endif /* AHD_TARGET_MODE */
8057 ahd_freeze_devq(struct ahd_softc *ahd, struct scb *scb)
8063 target = SCB_GET_TARGET(ahd, scb);
8064 lun = SCB_GET_LUN(scb);
8065 channel = SCB_GET_CHANNEL(ahd, scb);
8067 ahd_search_qinfifo(ahd, target, channel, lun,
8068 /*tag*/SCB_LIST_NULL, ROLE_UNKNOWN,
8069 CAM_REQUEUE_REQ, SEARCH_COMPLETE);
8071 ahd_platform_freeze_devq(ahd, scb);
8075 ahd_qinfifo_requeue_tail(struct ahd_softc *ahd, struct scb *scb)
8077 struct scb *prev_scb;
8078 ahd_mode_state saved_modes;
8080 saved_modes = ahd_save_modes(ahd);
8081 ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
8083 if (ahd_qinfifo_count(ahd) != 0) {
8087 prev_pos = AHD_QIN_WRAP(ahd->qinfifonext - 1);
8088 prev_tag = ahd->qinfifo[prev_pos];
8089 prev_scb = ahd_lookup_scb(ahd, prev_tag);
8091 ahd_qinfifo_requeue(ahd, prev_scb, scb);
8092 ahd_set_hnscb_qoff(ahd, ahd->qinfifonext);
8093 ahd_restore_modes(ahd, saved_modes);
8097 ahd_qinfifo_requeue(struct ahd_softc *ahd, struct scb *prev_scb,
8100 if (prev_scb == NULL) {
8103 busaddr = ahd_le32toh(scb->hscb->hscb_busaddr);
8104 ahd_outl(ahd, NEXT_QUEUED_SCB_ADDR, busaddr);
8106 prev_scb->hscb->next_hscb_busaddr = scb->hscb->hscb_busaddr;
8107 ahd_sync_scb(ahd, prev_scb,
8108 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
8110 ahd->qinfifo[AHD_QIN_WRAP(ahd->qinfifonext)] = SCB_GET_TAG(scb);
8112 scb->hscb->next_hscb_busaddr = ahd->next_queued_hscb->hscb_busaddr;
8113 ahd_sync_scb(ahd, scb, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
8117 ahd_qinfifo_count(struct ahd_softc *ahd)
8121 u_int wrap_qinfifonext;
8123 AHD_ASSERT_MODES(ahd, AHD_MODE_CCHAN_MSK, AHD_MODE_CCHAN_MSK);
8124 qinpos = ahd_get_snscb_qoff(ahd);
8125 wrap_qinpos = AHD_QIN_WRAP(qinpos);
8126 wrap_qinfifonext = AHD_QIN_WRAP(ahd->qinfifonext);
8127 if (wrap_qinfifonext >= wrap_qinpos)
8128 return (wrap_qinfifonext - wrap_qinpos);
8130 return (wrap_qinfifonext
8131 + ARRAY_SIZE(ahd->qinfifo) - wrap_qinpos);
8135 ahd_reset_cmds_pending(struct ahd_softc *ahd)
8138 ahd_mode_state saved_modes;
8141 saved_modes = ahd_save_modes(ahd);
8142 ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
8145 * Don't count any commands as outstanding that the
8146 * sequencer has already marked for completion.
8148 ahd_flush_qoutfifo(ahd);
8151 LIST_FOREACH(scb, &ahd->pending_scbs, pending_links) {
8154 ahd_outw(ahd, CMDS_PENDING, pending_cmds - ahd_qinfifo_count(ahd));
8155 ahd_restore_modes(ahd, saved_modes);
8156 ahd->flags &= ~AHD_UPDATE_PEND_CMDS;
8160 ahd_done_with_status(struct ahd_softc *ahd, struct scb *scb, uint32_t status)
8165 ostat = ahd_get_transaction_status(scb);
8166 if (ostat == CAM_REQ_INPROG)
8167 ahd_set_transaction_status(scb, status);
8168 cstat = ahd_get_transaction_status(scb);
8169 if (cstat != CAM_REQ_CMP)
8170 ahd_freeze_scb(scb);
8175 ahd_search_qinfifo(struct ahd_softc *ahd, int target, char channel,
8176 int lun, u_int tag, role_t role, uint32_t status,
8177 ahd_search_action action)
8180 struct scb *mk_msg_scb;
8181 struct scb *prev_scb;
8182 ahd_mode_state saved_modes;
8195 /* Must be in CCHAN mode */
8196 saved_modes = ahd_save_modes(ahd);
8197 ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
8200 * Halt any pending SCB DMA. The sequencer will reinitiate
8201 * this dma if the qinfifo is not empty once we unpause.
8203 if ((ahd_inb(ahd, CCSCBCTL) & (CCARREN|CCSCBEN|CCSCBDIR))
8204 == (CCARREN|CCSCBEN|CCSCBDIR)) {
8205 ahd_outb(ahd, CCSCBCTL,
8206 ahd_inb(ahd, CCSCBCTL) & ~(CCARREN|CCSCBEN));
8207 while ((ahd_inb(ahd, CCSCBCTL) & (CCARREN|CCSCBEN)) != 0)
8210 /* Determine sequencer's position in the qinfifo. */
8211 qintail = AHD_QIN_WRAP(ahd->qinfifonext);
8212 qinstart = ahd_get_snscb_qoff(ahd);
8213 qinpos = AHD_QIN_WRAP(qinstart);
8217 if (action == SEARCH_PRINT) {
8218 printf("qinstart = %d qinfifonext = %d\nQINFIFO:",
8219 qinstart, ahd->qinfifonext);
8223 * Start with an empty queue. Entries that are not chosen
8224 * for removal will be re-added to the queue as we go.
8226 ahd->qinfifonext = qinstart;
8227 busaddr = ahd_le32toh(ahd->next_queued_hscb->hscb_busaddr);
8228 ahd_outl(ahd, NEXT_QUEUED_SCB_ADDR, busaddr);
8230 while (qinpos != qintail) {
8231 scb = ahd_lookup_scb(ahd, ahd->qinfifo[qinpos]);
8233 printf("qinpos = %d, SCB index = %d\n",
8234 qinpos, ahd->qinfifo[qinpos]);
8238 if (ahd_match_scb(ahd, scb, target, channel, lun, tag, role)) {
8240 * We found an scb that needs to be acted on.
8244 case SEARCH_COMPLETE:
8245 if ((scb->flags & SCB_ACTIVE) == 0)
8246 printf("Inactive SCB in qinfifo\n");
8247 ahd_done_with_status(ahd, scb, status);
8252 printf(" 0x%x", ahd->qinfifo[qinpos]);
8255 ahd_qinfifo_requeue(ahd, prev_scb, scb);
8260 ahd_qinfifo_requeue(ahd, prev_scb, scb);
8263 qinpos = AHD_QIN_WRAP(qinpos+1);
8266 ahd_set_hnscb_qoff(ahd, ahd->qinfifonext);
8268 if (action == SEARCH_PRINT)
8269 printf("\nWAITING_TID_QUEUES:\n");
8272 * Search waiting for selection lists. We traverse the
8273 * list of "their ids" waiting for selection and, if
8274 * appropriate, traverse the SCBs of each "their id"
8275 * looking for matches.
8277 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
8278 seq_flags2 = ahd_inb(ahd, SEQ_FLAGS2);
8279 if ((seq_flags2 & PENDING_MK_MESSAGE) != 0) {
8280 scbid = ahd_inw(ahd, MK_MESSAGE_SCB);
8281 mk_msg_scb = ahd_lookup_scb(ahd, scbid);
8284 savedscbptr = ahd_get_scbptr(ahd);
8285 tid_next = ahd_inw(ahd, WAITING_TID_HEAD);
8286 tid_prev = SCB_LIST_NULL;
8288 for (scbid = tid_next; !SCBID_IS_NULL(scbid); scbid = tid_next) {
8293 if (targets > AHD_NUM_TARGETS)
8294 panic("TID LIST LOOP");
8296 if (scbid >= ahd->scb_data.numscbs) {
8297 printf("%s: Waiting TID List inconsistency. "
8298 "SCB index == 0x%x, yet numscbs == 0x%x.",
8299 ahd_name(ahd), scbid, ahd->scb_data.numscbs);
8300 ahd_dump_card_state(ahd);
8301 panic("for safety");
8303 scb = ahd_lookup_scb(ahd, scbid);
8305 printf("%s: SCB = 0x%x Not Active!\n",
8306 ahd_name(ahd), scbid);
8307 panic("Waiting TID List traversal\n");
8309 ahd_set_scbptr(ahd, scbid);
8310 tid_next = ahd_inw_scbram(ahd, SCB_NEXT2);
8311 if (ahd_match_scb(ahd, scb, target, channel, CAM_LUN_WILDCARD,
8312 SCB_LIST_NULL, ROLE_UNKNOWN) == 0) {
8318 * We found a list of scbs that needs to be searched.
8320 if (action == SEARCH_PRINT)
8321 printf(" %d ( ", SCB_GET_TARGET(ahd, scb));
8323 found += ahd_search_scb_list(ahd, target, channel,
8324 lun, tag, role, status,
8325 action, &tid_head, &tid_tail,
8326 SCB_GET_TARGET(ahd, scb));
8328 * Check any MK_MESSAGE SCB that is still waiting to
8329 * enter this target's waiting for selection queue.
8331 if (mk_msg_scb != NULL
8332 && ahd_match_scb(ahd, mk_msg_scb, target, channel,
8336 * We found an scb that needs to be acted on.
8340 case SEARCH_COMPLETE:
8341 if ((mk_msg_scb->flags & SCB_ACTIVE) == 0)
8342 printf("Inactive SCB pending MK_MSG\n");
8343 ahd_done_with_status(ahd, mk_msg_scb, status);
8349 printf("Removing MK_MSG scb\n");
8352 * Reset our tail to the tail of the
8353 * main per-target list.
8355 tail_offset = WAITING_SCB_TAILS
8356 + (2 * SCB_GET_TARGET(ahd, mk_msg_scb));
8357 ahd_outw(ahd, tail_offset, tid_tail);
8359 seq_flags2 &= ~PENDING_MK_MESSAGE;
8360 ahd_outb(ahd, SEQ_FLAGS2, seq_flags2);
8361 ahd_outw(ahd, CMDS_PENDING,
8362 ahd_inw(ahd, CMDS_PENDING)-1);
8367 printf(" 0x%x", SCB_GET_TAG(scb));
8374 if (mk_msg_scb != NULL
8375 && SCBID_IS_NULL(tid_head)
8376 && ahd_match_scb(ahd, scb, target, channel, CAM_LUN_WILDCARD,
8377 SCB_LIST_NULL, ROLE_UNKNOWN)) {
8380 * When removing the last SCB for a target
8381 * queue with a pending MK_MESSAGE scb, we
8382 * must queue the MK_MESSAGE scb.
8384 printf("Queueing mk_msg_scb\n");
8385 tid_head = ahd_inw(ahd, MK_MESSAGE_SCB);
8386 seq_flags2 &= ~PENDING_MK_MESSAGE;
8387 ahd_outb(ahd, SEQ_FLAGS2, seq_flags2);
8390 if (tid_head != scbid)
8391 ahd_stitch_tid_list(ahd, tid_prev, tid_head, tid_next);
8392 if (!SCBID_IS_NULL(tid_head))
8393 tid_prev = tid_head;
8394 if (action == SEARCH_PRINT)
8398 /* Restore saved state. */
8399 ahd_set_scbptr(ahd, savedscbptr);
8400 ahd_restore_modes(ahd, saved_modes);
8405 ahd_search_scb_list(struct ahd_softc *ahd, int target, char channel,
8406 int lun, u_int tag, role_t role, uint32_t status,
8407 ahd_search_action action, u_int *list_head,
8408 u_int *list_tail, u_int tid)
8416 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
8418 prev = SCB_LIST_NULL;
8420 *list_tail = SCB_LIST_NULL;
8421 for (scbid = next; !SCBID_IS_NULL(scbid); scbid = next) {
8422 if (scbid >= ahd->scb_data.numscbs) {
8423 printf("%s:SCB List inconsistency. "
8424 "SCB == 0x%x, yet numscbs == 0x%x.",
8425 ahd_name(ahd), scbid, ahd->scb_data.numscbs);
8426 ahd_dump_card_state(ahd);
8427 panic("for safety");
8429 scb = ahd_lookup_scb(ahd, scbid);
8431 printf("%s: SCB = %d Not Active!\n",
8432 ahd_name(ahd), scbid);
8433 panic("Waiting List traversal\n");
8435 ahd_set_scbptr(ahd, scbid);
8437 next = ahd_inw_scbram(ahd, SCB_NEXT);
8438 if (ahd_match_scb(ahd, scb, target, channel,
8439 lun, SCB_LIST_NULL, role) == 0) {
8445 case SEARCH_COMPLETE:
8446 if ((scb->flags & SCB_ACTIVE) == 0)
8447 printf("Inactive SCB in Waiting List\n");
8448 ahd_done_with_status(ahd, scb, status);
8451 ahd_rem_wscb(ahd, scbid, prev, next, tid);
8453 if (SCBID_IS_NULL(prev))
8457 printf("0x%x ", scbid);
8462 if (found > AHD_SCB_MAX)
8463 panic("SCB LIST LOOP");
8465 if (action == SEARCH_COMPLETE
8466 || action == SEARCH_REMOVE)
8467 ahd_outw(ahd, CMDS_PENDING, ahd_inw(ahd, CMDS_PENDING) - found);
8472 ahd_stitch_tid_list(struct ahd_softc *ahd, u_int tid_prev,
8473 u_int tid_cur, u_int tid_next)
8475 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
8477 if (SCBID_IS_NULL(tid_cur)) {
8479 /* Bypass current TID list */
8480 if (SCBID_IS_NULL(tid_prev)) {
8481 ahd_outw(ahd, WAITING_TID_HEAD, tid_next);
8483 ahd_set_scbptr(ahd, tid_prev);
8484 ahd_outw(ahd, SCB_NEXT2, tid_next);
8486 if (SCBID_IS_NULL(tid_next))
8487 ahd_outw(ahd, WAITING_TID_TAIL, tid_prev);
8490 /* Stitch through tid_cur */
8491 if (SCBID_IS_NULL(tid_prev)) {
8492 ahd_outw(ahd, WAITING_TID_HEAD, tid_cur);
8494 ahd_set_scbptr(ahd, tid_prev);
8495 ahd_outw(ahd, SCB_NEXT2, tid_cur);
8497 ahd_set_scbptr(ahd, tid_cur);
8498 ahd_outw(ahd, SCB_NEXT2, tid_next);
8500 if (SCBID_IS_NULL(tid_next))
8501 ahd_outw(ahd, WAITING_TID_TAIL, tid_cur);
8506 * Manipulate the waiting for selection list and return the
8507 * scb that follows the one that we remove.
8510 ahd_rem_wscb(struct ahd_softc *ahd, u_int scbid,
8511 u_int prev, u_int next, u_int tid)
8515 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
8516 if (!SCBID_IS_NULL(prev)) {
8517 ahd_set_scbptr(ahd, prev);
8518 ahd_outw(ahd, SCB_NEXT, next);
8522 * SCBs that have MK_MESSAGE set in them may
8523 * cause the tail pointer to be updated without
8524 * setting the next pointer of the previous tail.
8525 * Only clear the tail if the removed SCB was
8528 tail_offset = WAITING_SCB_TAILS + (2 * tid);
8529 if (SCBID_IS_NULL(next)
8530 && ahd_inw(ahd, tail_offset) == scbid)
8531 ahd_outw(ahd, tail_offset, prev);
8533 ahd_add_scb_to_free_list(ahd, scbid);
8538 * Add the SCB as selected by SCBPTR onto the on chip list of
8539 * free hardware SCBs. This list is empty/unused if we are not
8540 * performing SCB paging.
8543 ahd_add_scb_to_free_list(struct ahd_softc *ahd, u_int scbid)
8545 /* XXX Need some other mechanism to designate "free". */
8547 * Invalidate the tag so that our abort
8548 * routines don't think it's active.
8549 ahd_outb(ahd, SCB_TAG, SCB_LIST_NULL);
8553 /******************************** Error Handling ******************************/
8555 * Abort all SCBs that match the given description (target/channel/lun/tag),
8556 * setting their status to the passed in status if the status has not already
8557 * been modified from CAM_REQ_INPROG. This routine assumes that the sequencer
8558 * is paused before it is called.
8561 ahd_abort_scbs(struct ahd_softc *ahd, int target, char channel,
8562 int lun, u_int tag, role_t role, uint32_t status)
8565 struct scb *scbp_next;
8571 ahd_mode_state saved_modes;
8573 /* restore this when we're done */
8574 saved_modes = ahd_save_modes(ahd);
8575 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
8577 found = ahd_search_qinfifo(ahd, target, channel, lun, SCB_LIST_NULL,
8578 role, CAM_REQUEUE_REQ, SEARCH_COMPLETE);
8581 * Clean out the busy target table for any untagged commands.
8585 if (target != CAM_TARGET_WILDCARD) {
8592 if (lun == CAM_LUN_WILDCARD) {
8594 maxlun = AHD_NUM_LUNS_NONPKT;
8595 } else if (lun >= AHD_NUM_LUNS_NONPKT) {
8596 minlun = maxlun = 0;
8602 if (role != ROLE_TARGET) {
8603 for (;i < maxtarget; i++) {
8604 for (j = minlun;j < maxlun; j++) {
8608 tcl = BUILD_TCL_RAW(i, 'A', j);
8609 scbid = ahd_find_busy_tcl(ahd, tcl);
8610 scbp = ahd_lookup_scb(ahd, scbid);
8612 || ahd_match_scb(ahd, scbp, target, channel,
8613 lun, tag, role) == 0)
8615 ahd_unbusy_tcl(ahd, BUILD_TCL_RAW(i, 'A', j));
8621 * Don't abort commands that have already completed,
8622 * but haven't quite made it up to the host yet.
8624 ahd_flush_qoutfifo(ahd);
8627 * Go through the pending CCB list and look for
8628 * commands for this target that are still active.
8629 * These are other tagged commands that were
8630 * disconnected when the reset occurred.
8632 scbp_next = LIST_FIRST(&ahd->pending_scbs);
8633 while (scbp_next != NULL) {
8635 scbp_next = LIST_NEXT(scbp, pending_links);
8636 if (ahd_match_scb(ahd, scbp, target, channel, lun, tag, role)) {
8639 ostat = ahd_get_transaction_status(scbp);
8640 if (ostat == CAM_REQ_INPROG)
8641 ahd_set_transaction_status(scbp, status);
8642 if (ahd_get_transaction_status(scbp) != CAM_REQ_CMP)
8643 ahd_freeze_scb(scbp);
8644 if ((scbp->flags & SCB_ACTIVE) == 0)
8645 printf("Inactive SCB on pending list\n");
8646 ahd_done(ahd, scbp);
8650 ahd_restore_modes(ahd, saved_modes);
8651 ahd_platform_abort_scbs(ahd, target, channel, lun, tag, role, status);
8652 ahd->flags |= AHD_UPDATE_PEND_CMDS;
8657 ahd_reset_current_bus(struct ahd_softc *ahd)
8661 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
8662 ahd_outb(ahd, SIMODE1, ahd_inb(ahd, SIMODE1) & ~ENSCSIRST);
8663 scsiseq = ahd_inb(ahd, SCSISEQ0) & ~(ENSELO|ENARBO|SCSIRSTO);
8664 ahd_outb(ahd, SCSISEQ0, scsiseq | SCSIRSTO);
8665 ahd_flush_device_writes(ahd);
8666 ahd_delay(AHD_BUSRESET_DELAY);
8667 /* Turn off the bus reset */
8668 ahd_outb(ahd, SCSISEQ0, scsiseq);
8669 ahd_flush_device_writes(ahd);
8670 ahd_delay(AHD_BUSRESET_DELAY);
8671 if ((ahd->bugs & AHD_SCSIRST_BUG) != 0) {
8674 * Certain chip state is not cleared for
8675 * SCSI bus resets that we initiate, so
8676 * we must reset the chip.
8678 ahd_reset(ahd, /*reinit*/TRUE);
8679 ahd_intr_enable(ahd, /*enable*/TRUE);
8680 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
8683 ahd_clear_intstat(ahd);
8687 ahd_reset_channel(struct ahd_softc *ahd, char channel, int initiate_reset)
8689 struct ahd_devinfo devinfo;
8699 * Check if the last bus reset is cleared
8701 if (ahd->flags & AHD_BUS_RESET_ACTIVE) {
8702 printf("%s: bus reset still active\n",
8706 ahd->flags |= AHD_BUS_RESET_ACTIVE;
8708 ahd->pending_device = NULL;
8710 ahd_compile_devinfo(&devinfo,
8711 CAM_TARGET_WILDCARD,
8712 CAM_TARGET_WILDCARD,
8714 channel, ROLE_UNKNOWN);
8717 /* Make sure the sequencer is in a safe location. */
8718 ahd_clear_critical_section(ahd);
8721 * Run our command complete fifos to ensure that we perform
8722 * completion processing on any commands that 'completed'
8723 * before the reset occurred.
8725 ahd_run_qoutfifo(ahd);
8726 #ifdef AHD_TARGET_MODE
8727 if ((ahd->flags & AHD_TARGETROLE) != 0) {
8728 ahd_run_tqinfifo(ahd, /*paused*/TRUE);
8731 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
8734 * Disable selections so no automatic hardware
8735 * functions will modify chip state.
8737 ahd_outb(ahd, SCSISEQ0, 0);
8738 ahd_outb(ahd, SCSISEQ1, 0);
8741 * Safely shut down our DMA engines. Always start with
8742 * the FIFO that is not currently active (if any are
8743 * actively connected).
8745 next_fifo = fifo = ahd_inb(ahd, DFFSTAT) & CURRFIFO;
8746 if (next_fifo > CURRFIFO_1)
8747 /* If disconneced, arbitrarily start with FIFO1. */
8748 next_fifo = fifo = 0;
8750 next_fifo ^= CURRFIFO_1;
8751 ahd_set_modes(ahd, next_fifo, next_fifo);
8752 ahd_outb(ahd, DFCNTRL,
8753 ahd_inb(ahd, DFCNTRL) & ~(SCSIEN|HDMAEN));
8754 while ((ahd_inb(ahd, DFCNTRL) & HDMAENACK) != 0)
8757 * Set CURRFIFO to the now inactive channel.
8759 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
8760 ahd_outb(ahd, DFFSTAT, next_fifo);
8761 } while (next_fifo != fifo);
8764 * Reset the bus if we are initiating this reset
8766 ahd_clear_msg_state(ahd);
8767 ahd_outb(ahd, SIMODE1,
8768 ahd_inb(ahd, SIMODE1) & ~(ENBUSFREE|ENSCSIRST));
8771 ahd_reset_current_bus(ahd);
8773 ahd_clear_intstat(ahd);
8776 * Clean up all the state information for the
8777 * pending transactions on this bus.
8779 found = ahd_abort_scbs(ahd, CAM_TARGET_WILDCARD, channel,
8780 CAM_LUN_WILDCARD, SCB_LIST_NULL,
8781 ROLE_UNKNOWN, CAM_SCSI_BUS_RESET);
8784 * Cleanup anything left in the FIFOs.
8786 ahd_clear_fifo(ahd, 0);
8787 ahd_clear_fifo(ahd, 1);
8790 * Clear SCSI interrupt status
8792 ahd_outb(ahd, CLRSINT1, CLRSCSIRSTI);
8795 * Reenable selections
8797 ahd_outb(ahd, SIMODE1, ahd_inb(ahd, SIMODE1) | ENSCSIRST);
8798 scsiseq = ahd_inb(ahd, SCSISEQ_TEMPLATE);
8799 ahd_outb(ahd, SCSISEQ1, scsiseq & (ENSELI|ENRSELI|ENAUTOATNP));
8801 max_scsiid = (ahd->features & AHD_WIDE) ? 15 : 7;
8802 #ifdef AHD_TARGET_MODE
8804 * Send an immediate notify ccb to all target more peripheral
8805 * drivers affected by this action.
8807 for (target = 0; target <= max_scsiid; target++) {
8808 struct ahd_tmode_tstate* tstate;
8811 tstate = ahd->enabled_targets[target];
8814 for (lun = 0; lun < AHD_NUM_LUNS; lun++) {
8815 struct ahd_tmode_lstate* lstate;
8817 lstate = tstate->enabled_luns[lun];
8821 ahd_queue_lstate_event(ahd, lstate, CAM_TARGET_WILDCARD,
8822 EVENT_TYPE_BUS_RESET, /*arg*/0);
8823 ahd_send_lstate_events(ahd, lstate);
8828 * Revert to async/narrow transfers until we renegotiate.
8830 for (target = 0; target <= max_scsiid; target++) {
8832 if (ahd->enabled_targets[target] == NULL)
8834 for (initiator = 0; initiator <= max_scsiid; initiator++) {
8835 struct ahd_devinfo devinfo;
8837 ahd_compile_devinfo(&devinfo, target, initiator,
8840 ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
8841 AHD_TRANS_CUR, /*paused*/TRUE);
8842 ahd_set_syncrate(ahd, &devinfo, /*period*/0,
8843 /*offset*/0, /*ppr_options*/0,
8844 AHD_TRANS_CUR, /*paused*/TRUE);
8848 /* Notify the XPT that a bus reset occurred */
8849 ahd_send_async(ahd, devinfo.channel, CAM_TARGET_WILDCARD,
8850 CAM_LUN_WILDCARD, AC_BUS_RESET);
8857 /**************************** Statistics Processing ***************************/
8859 ahd_stat_timer(void *arg)
8861 struct ahd_softc *ahd = arg;
8867 enint_coal = ahd->hs_mailbox & ENINT_COALESCE;
8868 if (ahd->cmdcmplt_total > ahd->int_coalescing_threshold)
8869 enint_coal |= ENINT_COALESCE;
8870 else if (ahd->cmdcmplt_total < ahd->int_coalescing_stop_threshold)
8871 enint_coal &= ~ENINT_COALESCE;
8873 if (enint_coal != (ahd->hs_mailbox & ENINT_COALESCE)) {
8874 ahd_enable_coalescing(ahd, enint_coal);
8876 if ((ahd_debug & AHD_SHOW_INT_COALESCING) != 0)
8877 printf("%s: Interrupt coalescing "
8878 "now %sabled. Cmds %d\n",
8880 (enint_coal & ENINT_COALESCE) ? "en" : "dis",
8881 ahd->cmdcmplt_total);
8885 ahd->cmdcmplt_bucket = (ahd->cmdcmplt_bucket+1) & (AHD_STAT_BUCKETS-1);
8886 ahd->cmdcmplt_total -= ahd->cmdcmplt_counts[ahd->cmdcmplt_bucket];
8887 ahd->cmdcmplt_counts[ahd->cmdcmplt_bucket] = 0;
8888 ahd_timer_reset(&ahd->stat_timer, AHD_STAT_UPDATE_US,
8889 ahd_stat_timer, ahd);
8890 ahd_unlock(ahd, &s);
8893 /****************************** Status Processing *****************************/
8896 ahd_handle_scsi_status(struct ahd_softc *ahd, struct scb *scb)
8898 struct hardware_scb *hscb;
8902 * The sequencer freezes its select-out queue
8903 * anytime a SCSI status error occurs. We must
8904 * handle the error and increment our qfreeze count
8905 * to allow the sequencer to continue. We don't
8906 * bother clearing critical sections here since all
8907 * operations are on data structures that the sequencer
8908 * is not touching once the queue is frozen.
8912 if (ahd_is_paused(ahd)) {
8919 /* Freeze the queue until the client sees the error. */
8920 ahd_freeze_devq(ahd, scb);
8921 ahd_freeze_scb(scb);
8923 ahd_outw(ahd, KERNEL_QFREEZE_COUNT, ahd->qfreeze_cnt);
8928 /* Don't want to clobber the original sense code */
8929 if ((scb->flags & SCB_SENSE) != 0) {
8931 * Clear the SCB_SENSE Flag and perform
8932 * a normal command completion.
8934 scb->flags &= ~SCB_SENSE;
8935 ahd_set_transaction_status(scb, CAM_AUTOSENSE_FAIL);
8939 ahd_set_transaction_status(scb, CAM_SCSI_STATUS_ERROR);
8940 ahd_set_scsi_status(scb, hscb->shared_data.istatus.scsi_status);
8941 switch (hscb->shared_data.istatus.scsi_status) {
8942 case STATUS_PKT_SENSE:
8944 struct scsi_status_iu_header *siu;
8946 ahd_sync_sense(ahd, scb, BUS_DMASYNC_POSTREAD);
8947 siu = (struct scsi_status_iu_header *)scb->sense_data;
8948 ahd_set_scsi_status(scb, siu->status);
8950 if ((ahd_debug & AHD_SHOW_SENSE) != 0) {
8951 ahd_print_path(ahd, scb);
8952 printf("SCB 0x%x Received PKT Status of 0x%x\n",
8953 SCB_GET_TAG(scb), siu->status);
8954 printf("\tflags = 0x%x, sense len = 0x%x, "
8956 siu->flags, scsi_4btoul(siu->sense_length),
8957 scsi_4btoul(siu->pkt_failures_length));
8960 if ((siu->flags & SIU_RSPVALID) != 0) {
8961 ahd_print_path(ahd, scb);
8962 if (scsi_4btoul(siu->pkt_failures_length) < 4) {
8963 printf("Unable to parse pkt_failures\n");
8966 switch (SIU_PKTFAIL_CODE(siu)) {
8968 printf("No packet failure found\n");
8970 case SIU_PFC_CIU_FIELDS_INVALID:
8971 printf("Invalid Command IU Field\n");
8973 case SIU_PFC_TMF_NOT_SUPPORTED:
8974 printf("TMF not supportd\n");
8976 case SIU_PFC_TMF_FAILED:
8977 printf("TMF failed\n");
8979 case SIU_PFC_INVALID_TYPE_CODE:
8980 printf("Invalid L_Q Type code\n");
8982 case SIU_PFC_ILLEGAL_REQUEST:
8983 printf("Illegal request\n");
8988 if (siu->status == SCSI_STATUS_OK)
8989 ahd_set_transaction_status(scb,
8992 if ((siu->flags & SIU_SNSVALID) != 0) {
8993 scb->flags |= SCB_PKT_SENSE;
8995 if ((ahd_debug & AHD_SHOW_SENSE) != 0)
8996 printf("Sense data available\n");
9002 case SCSI_STATUS_CMD_TERMINATED:
9003 case SCSI_STATUS_CHECK_COND:
9005 struct ahd_devinfo devinfo;
9006 struct ahd_dma_seg *sg;
9007 struct scsi_sense *sc;
9008 struct ahd_initiator_tinfo *targ_info;
9009 struct ahd_tmode_tstate *tstate;
9010 struct ahd_transinfo *tinfo;
9012 if (ahd_debug & AHD_SHOW_SENSE) {
9013 ahd_print_path(ahd, scb);
9014 printf("SCB %d: requests Check Status\n",
9019 if (ahd_perform_autosense(scb) == 0)
9022 ahd_compile_devinfo(&devinfo, SCB_GET_OUR_ID(scb),
9023 SCB_GET_TARGET(ahd, scb),
9025 SCB_GET_CHANNEL(ahd, scb),
9027 targ_info = ahd_fetch_transinfo(ahd,
9032 tinfo = &targ_info->curr;
9034 sc = (struct scsi_sense *)hscb->shared_data.idata.cdb;
9036 * Save off the residual if there is one.
9038 ahd_update_residual(ahd, scb);
9040 if (ahd_debug & AHD_SHOW_SENSE) {
9041 ahd_print_path(ahd, scb);
9042 printf("Sending Sense\n");
9046 sg = ahd_sg_setup(ahd, scb, sg, ahd_get_sense_bufaddr(ahd, scb),
9047 ahd_get_sense_bufsize(ahd, scb),
9049 sc->opcode = REQUEST_SENSE;
9051 if (tinfo->protocol_version <= SCSI_REV_2
9052 && SCB_GET_LUN(scb) < 8)
9053 sc->byte2 = SCB_GET_LUN(scb) << 5;
9056 sc->length = ahd_get_sense_bufsize(ahd, scb);
9060 * We can't allow the target to disconnect.
9061 * This will be an untagged transaction and
9062 * having the target disconnect will make this
9063 * transaction indestinguishable from outstanding
9064 * tagged transactions.
9069 * This request sense could be because the
9070 * the device lost power or in some other
9071 * way has lost our transfer negotiations.
9072 * Renegotiate if appropriate. Unit attention
9073 * errors will be reported before any data
9076 if (ahd_get_residual(scb) == ahd_get_transfer_length(scb)) {
9077 ahd_update_neg_request(ahd, &devinfo,
9079 AHD_NEG_IF_NON_ASYNC);
9081 if (tstate->auto_negotiate & devinfo.target_mask) {
9082 hscb->control |= MK_MESSAGE;
9084 ~(SCB_NEGOTIATE|SCB_ABORT|SCB_DEVICE_RESET);
9085 scb->flags |= SCB_AUTO_NEGOTIATE;
9087 hscb->cdb_len = sizeof(*sc);
9088 ahd_setup_data_scb(ahd, scb);
9089 scb->flags |= SCB_SENSE;
9090 ahd_queue_scb(ahd, scb);
9093 case SCSI_STATUS_OK:
9094 printf("%s: Interrupted for staus of 0???\n",
9104 ahd_handle_scb_status(struct ahd_softc *ahd, struct scb *scb)
9106 if (scb->hscb->shared_data.istatus.scsi_status != 0) {
9107 ahd_handle_scsi_status(ahd, scb);
9109 ahd_calc_residual(ahd, scb);
9115 * Calculate the residual for a just completed SCB.
9118 ahd_calc_residual(struct ahd_softc *ahd, struct scb *scb)
9120 struct hardware_scb *hscb;
9121 struct initiator_status *spkt;
9123 uint32_t resid_sgptr;
9129 * SG_STATUS_VALID clear in sgptr.
9130 * 2) Transferless command
9131 * 3) Never performed any transfers.
9132 * sgptr has SG_FULL_RESID set.
9133 * 4) No residual but target did not
9134 * save data pointers after the
9135 * last transfer, so sgptr was
9137 * 5) We have a partial residual.
9138 * Use residual_sgptr to determine
9143 sgptr = ahd_le32toh(hscb->sgptr);
9144 if ((sgptr & SG_STATUS_VALID) == 0)
9147 sgptr &= ~SG_STATUS_VALID;
9149 if ((sgptr & SG_LIST_NULL) != 0)
9154 * Residual fields are the same in both
9155 * target and initiator status packets,
9156 * so we can always use the initiator fields
9157 * regardless of the role for this SCB.
9159 spkt = &hscb->shared_data.istatus;
9160 resid_sgptr = ahd_le32toh(spkt->residual_sgptr);
9161 if ((sgptr & SG_FULL_RESID) != 0) {
9163 resid = ahd_get_transfer_length(scb);
9164 } else if ((resid_sgptr & SG_LIST_NULL) != 0) {
9167 } else if ((resid_sgptr & SG_OVERRUN_RESID) != 0) {
9168 ahd_print_path(ahd, scb);
9169 printf("data overrun detected Tag == 0x%x.\n",
9171 ahd_freeze_devq(ahd, scb);
9172 ahd_set_transaction_status(scb, CAM_DATA_RUN_ERR);
9173 ahd_freeze_scb(scb);
9175 } else if ((resid_sgptr & ~SG_PTR_MASK) != 0) {
9176 panic("Bogus resid sgptr value 0x%x\n", resid_sgptr);
9179 struct ahd_dma_seg *sg;
9182 * Remainder of the SG where the transfer
9185 resid = ahd_le32toh(spkt->residual_datacnt) & AHD_SG_LEN_MASK;
9186 sg = ahd_sg_bus_to_virt(ahd, scb, resid_sgptr & SG_PTR_MASK);
9188 /* The residual sg_ptr always points to the next sg */
9192 * Add up the contents of all residual
9193 * SG segments that are after the SG where
9194 * the transfer stopped.
9196 while ((ahd_le32toh(sg->len) & AHD_DMA_LAST_SEG) == 0) {
9198 resid += ahd_le32toh(sg->len) & AHD_SG_LEN_MASK;
9201 if ((scb->flags & SCB_SENSE) == 0)
9202 ahd_set_residual(scb, resid);
9204 ahd_set_sense_residual(scb, resid);
9207 if ((ahd_debug & AHD_SHOW_MISC) != 0) {
9208 ahd_print_path(ahd, scb);
9209 printf("Handled %sResidual of %d bytes\n",
9210 (scb->flags & SCB_SENSE) ? "Sense " : "", resid);
9215 /******************************* Target Mode **********************************/
9216 #ifdef AHD_TARGET_MODE
9218 * Add a target mode event to this lun's queue
9221 ahd_queue_lstate_event(struct ahd_softc *ahd, struct ahd_tmode_lstate *lstate,
9222 u_int initiator_id, u_int event_type, u_int event_arg)
9224 struct ahd_tmode_event *event;
9227 xpt_freeze_devq(lstate->path, /*count*/1);
9228 if (lstate->event_w_idx >= lstate->event_r_idx)
9229 pending = lstate->event_w_idx - lstate->event_r_idx;
9231 pending = AHD_TMODE_EVENT_BUFFER_SIZE + 1
9232 - (lstate->event_r_idx - lstate->event_w_idx);
9234 if (event_type == EVENT_TYPE_BUS_RESET
9235 || event_type == MSG_BUS_DEV_RESET) {
9237 * Any earlier events are irrelevant, so reset our buffer.
9238 * This has the effect of allowing us to deal with reset
9239 * floods (an external device holding down the reset line)
9240 * without losing the event that is really interesting.
9242 lstate->event_r_idx = 0;
9243 lstate->event_w_idx = 0;
9244 xpt_release_devq(lstate->path, pending, /*runqueue*/FALSE);
9247 if (pending == AHD_TMODE_EVENT_BUFFER_SIZE) {
9248 xpt_print_path(lstate->path);
9249 printf("immediate event %x:%x lost\n",
9250 lstate->event_buffer[lstate->event_r_idx].event_type,
9251 lstate->event_buffer[lstate->event_r_idx].event_arg);
9252 lstate->event_r_idx++;
9253 if (lstate->event_r_idx == AHD_TMODE_EVENT_BUFFER_SIZE)
9254 lstate->event_r_idx = 0;
9255 xpt_release_devq(lstate->path, /*count*/1, /*runqueue*/FALSE);
9258 event = &lstate->event_buffer[lstate->event_w_idx];
9259 event->initiator_id = initiator_id;
9260 event->event_type = event_type;
9261 event->event_arg = event_arg;
9262 lstate->event_w_idx++;
9263 if (lstate->event_w_idx == AHD_TMODE_EVENT_BUFFER_SIZE)
9264 lstate->event_w_idx = 0;
9268 * Send any target mode events queued up waiting
9269 * for immediate notify resources.
9272 ahd_send_lstate_events(struct ahd_softc *ahd, struct ahd_tmode_lstate *lstate)
9274 struct ccb_hdr *ccbh;
9275 struct ccb_immed_notify *inot;
9277 while (lstate->event_r_idx != lstate->event_w_idx
9278 && (ccbh = SLIST_FIRST(&lstate->immed_notifies)) != NULL) {
9279 struct ahd_tmode_event *event;
9281 event = &lstate->event_buffer[lstate->event_r_idx];
9282 SLIST_REMOVE_HEAD(&lstate->immed_notifies, sim_links.sle);
9283 inot = (struct ccb_immed_notify *)ccbh;
9284 switch (event->event_type) {
9285 case EVENT_TYPE_BUS_RESET:
9286 ccbh->status = CAM_SCSI_BUS_RESET|CAM_DEV_QFRZN;
9289 ccbh->status = CAM_MESSAGE_RECV|CAM_DEV_QFRZN;
9290 inot->message_args[0] = event->event_type;
9291 inot->message_args[1] = event->event_arg;
9294 inot->initiator_id = event->initiator_id;
9295 inot->sense_len = 0;
9296 xpt_done((union ccb *)inot);
9297 lstate->event_r_idx++;
9298 if (lstate->event_r_idx == AHD_TMODE_EVENT_BUFFER_SIZE)
9299 lstate->event_r_idx = 0;
9304 /******************** Sequencer Program Patching/Download *********************/
9308 ahd_dumpseq(struct ahd_softc* ahd)
9315 ahd_outb(ahd, SEQCTL0, PERRORDIS|FAILDIS|FASTMODE|LOADRAM);
9316 ahd_outw(ahd, PRGMCNT, 0);
9317 for (i = 0; i < max_prog; i++) {
9318 uint8_t ins_bytes[4];
9320 ahd_insb(ahd, SEQRAM, ins_bytes, 4);
9321 printf("0x%08x\n", ins_bytes[0] << 24
9322 | ins_bytes[1] << 16
9330 ahd_loadseq(struct ahd_softc *ahd)
9332 struct cs cs_table[num_critical_sections];
9333 u_int begin_set[num_critical_sections];
9334 u_int end_set[num_critical_sections];
9335 struct patch *cur_patch;
9341 u_int sg_prefetch_cnt;
9342 u_int sg_prefetch_cnt_limit;
9343 u_int sg_prefetch_align;
9345 u_int cacheline_mask;
9346 uint8_t download_consts[DOWNLOAD_CONST_COUNT];
9349 printf("%s: Downloading Sequencer Program...",
9352 #if DOWNLOAD_CONST_COUNT != 8
9353 #error "Download Const Mismatch"
9356 * Start out with 0 critical sections
9357 * that apply to this firmware load.
9361 memset(begin_set, 0, sizeof(begin_set));
9362 memset(end_set, 0, sizeof(end_set));
9365 * Setup downloadable constant table.
9367 * The computation for the S/G prefetch variables is
9368 * a bit complicated. We would like to always fetch
9369 * in terms of cachelined sized increments. However,
9370 * if the cacheline is not an even multiple of the
9371 * SG element size or is larger than our SG RAM, using
9372 * just the cache size might leave us with only a portion
9373 * of an SG element at the tail of a prefetch. If the
9374 * cacheline is larger than our S/G prefetch buffer less
9375 * the size of an SG element, we may round down to a cacheline
9376 * that doesn't contain any or all of the S/G of interest
9377 * within the bounds of our S/G ram. Provide variables to
9378 * the sequencer that will allow it to handle these edge
9381 /* Start by aligning to the nearest cacheline. */
9382 sg_prefetch_align = ahd->pci_cachesize;
9383 if (sg_prefetch_align == 0)
9384 sg_prefetch_align = 8;
9385 /* Round down to the nearest power of 2. */
9386 while (powerof2(sg_prefetch_align) == 0)
9387 sg_prefetch_align--;
9389 cacheline_mask = sg_prefetch_align - 1;
9392 * If the cacheline boundary is greater than half our prefetch RAM
9393 * we risk not being able to fetch even a single complete S/G
9394 * segment if we align to that boundary.
9396 if (sg_prefetch_align > CCSGADDR_MAX/2)
9397 sg_prefetch_align = CCSGADDR_MAX/2;
9398 /* Start by fetching a single cacheline. */
9399 sg_prefetch_cnt = sg_prefetch_align;
9401 * Increment the prefetch count by cachelines until
9402 * at least one S/G element will fit.
9404 sg_size = sizeof(struct ahd_dma_seg);
9405 if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0)
9406 sg_size = sizeof(struct ahd_dma64_seg);
9407 while (sg_prefetch_cnt < sg_size)
9408 sg_prefetch_cnt += sg_prefetch_align;
9410 * If the cacheline is not an even multiple of
9411 * the S/G size, we may only get a partial S/G when
9412 * we align. Add a cacheline if this is the case.
9414 if ((sg_prefetch_align % sg_size) != 0
9415 && (sg_prefetch_cnt < CCSGADDR_MAX))
9416 sg_prefetch_cnt += sg_prefetch_align;
9418 * Lastly, compute a value that the sequencer can use
9419 * to determine if the remainder of the CCSGRAM buffer
9420 * has a full S/G element in it.
9422 sg_prefetch_cnt_limit = -(sg_prefetch_cnt - sg_size + 1);
9423 download_consts[SG_PREFETCH_CNT] = sg_prefetch_cnt;
9424 download_consts[SG_PREFETCH_CNT_LIMIT] = sg_prefetch_cnt_limit;
9425 download_consts[SG_PREFETCH_ALIGN_MASK] = ~(sg_prefetch_align - 1);
9426 download_consts[SG_PREFETCH_ADDR_MASK] = (sg_prefetch_align - 1);
9427 download_consts[SG_SIZEOF] = sg_size;
9428 download_consts[PKT_OVERRUN_BUFOFFSET] =
9429 (ahd->overrun_buf - (uint8_t *)ahd->qoutfifo) / 256;
9430 download_consts[SCB_TRANSFER_SIZE] = SCB_TRANSFER_SIZE_1BYTE_LUN;
9431 download_consts[CACHELINE_MASK] = cacheline_mask;
9432 cur_patch = patches;
9435 ahd_outb(ahd, SEQCTL0, PERRORDIS|FAILDIS|FASTMODE|LOADRAM);
9436 ahd_outw(ahd, PRGMCNT, 0);
9438 for (i = 0; i < sizeof(seqprog)/4; i++) {
9439 if (ahd_check_patch(ahd, &cur_patch, i, &skip_addr) == 0) {
9441 * Don't download this instruction as it
9442 * is in a patch that was removed.
9447 * Move through the CS table until we find a CS
9448 * that might apply to this instruction.
9450 for (; cur_cs < num_critical_sections; cur_cs++) {
9451 if (critical_sections[cur_cs].end <= i) {
9452 if (begin_set[cs_count] == TRUE
9453 && end_set[cs_count] == FALSE) {
9454 cs_table[cs_count].end = downloaded;
9455 end_set[cs_count] = TRUE;
9460 if (critical_sections[cur_cs].begin <= i
9461 && begin_set[cs_count] == FALSE) {
9462 cs_table[cs_count].begin = downloaded;
9463 begin_set[cs_count] = TRUE;
9467 ahd_download_instr(ahd, i, download_consts);
9471 ahd->num_critical_sections = cs_count;
9472 if (cs_count != 0) {
9474 cs_count *= sizeof(struct cs);
9475 ahd->critical_sections = malloc(cs_count, M_DEVBUF, M_NOWAIT);
9476 if (ahd->critical_sections == NULL)
9477 panic("ahd_loadseq: Could not malloc");
9478 memcpy(ahd->critical_sections, cs_table, cs_count);
9480 ahd_outb(ahd, SEQCTL0, PERRORDIS|FAILDIS|FASTMODE);
9483 printf(" %d instructions downloaded\n", downloaded);
9484 printf("%s: Features 0x%x, Bugs 0x%x, Flags 0x%x\n",
9485 ahd_name(ahd), ahd->features, ahd->bugs, ahd->flags);
9490 ahd_check_patch(struct ahd_softc *ahd, struct patch **start_patch,
9491 u_int start_instr, u_int *skip_addr)
9493 struct patch *cur_patch;
9494 struct patch *last_patch;
9497 num_patches = ARRAY_SIZE(patches);
9498 last_patch = &patches[num_patches];
9499 cur_patch = *start_patch;
9501 while (cur_patch < last_patch && start_instr == cur_patch->begin) {
9503 if (cur_patch->patch_func(ahd) == 0) {
9505 /* Start rejecting code */
9506 *skip_addr = start_instr + cur_patch->skip_instr;
9507 cur_patch += cur_patch->skip_patch;
9509 /* Accepted this patch. Advance to the next
9510 * one and wait for our intruction pointer to
9517 *start_patch = cur_patch;
9518 if (start_instr < *skip_addr)
9519 /* Still skipping */
9526 ahd_resolve_seqaddr(struct ahd_softc *ahd, u_int address)
9528 struct patch *cur_patch;
9534 cur_patch = patches;
9537 for (i = 0; i < address;) {
9539 ahd_check_patch(ahd, &cur_patch, i, &skip_addr);
9541 if (skip_addr > i) {
9544 end_addr = min(address, skip_addr);
9545 address_offset += end_addr - i;
9551 return (address - address_offset);
9555 ahd_download_instr(struct ahd_softc *ahd, u_int instrptr, uint8_t *dconsts)
9557 union ins_formats instr;
9558 struct ins_format1 *fmt1_ins;
9559 struct ins_format3 *fmt3_ins;
9563 * The firmware is always compiled into a little endian format.
9565 instr.integer = ahd_le32toh(*(uint32_t*)&seqprog[instrptr * 4]);
9567 fmt1_ins = &instr.format1;
9570 /* Pull the opcode */
9571 opcode = instr.format1.opcode;
9582 fmt3_ins = &instr.format3;
9583 fmt3_ins->address = ahd_resolve_seqaddr(ahd, fmt3_ins->address);
9592 if (fmt1_ins->parity != 0) {
9593 fmt1_ins->immediate = dconsts[fmt1_ins->immediate];
9595 fmt1_ins->parity = 0;
9601 /* Calculate odd parity for the instruction */
9602 for (i = 0, count = 0; i < 31; i++) {
9606 if ((instr.integer & mask) != 0)
9609 if ((count & 0x01) == 0)
9610 instr.format1.parity = 1;
9612 /* The sequencer is a little endian cpu */
9613 instr.integer = ahd_htole32(instr.integer);
9614 ahd_outsb(ahd, SEQRAM, instr.bytes, 4);
9618 panic("Unknown opcode encountered in seq program");
9624 ahd_probe_stack_size(struct ahd_softc *ahd)
9633 * We avoid using 0 as a pattern to avoid
9634 * confusion if the stack implementation
9635 * "back-fills" with zeros when "poping'
9638 for (i = 1; i <= last_probe+1; i++) {
9639 ahd_outb(ahd, STACK, i & 0xFF);
9640 ahd_outb(ahd, STACK, (i >> 8) & 0xFF);
9644 for (i = last_probe+1; i > 0; i--) {
9647 stack_entry = ahd_inb(ahd, STACK)
9648 |(ahd_inb(ahd, STACK) << 8);
9649 if (stack_entry != i)
9655 return (last_probe);
9659 ahd_print_register(ahd_reg_parse_entry_t *table, u_int num_entries,
9660 const char *name, u_int address, u_int value,
9661 u_int *cur_column, u_int wrap_point)
9666 if (cur_column != NULL && *cur_column >= wrap_point) {
9670 printed = printf("%s[0x%x]", name, value);
9671 if (table == NULL) {
9672 printed += printf(" ");
9673 *cur_column += printed;
9677 while (printed_mask != 0xFF) {
9680 for (entry = 0; entry < num_entries; entry++) {
9681 if (((value & table[entry].mask)
9682 != table[entry].value)
9683 || ((printed_mask & table[entry].mask)
9684 == table[entry].mask))
9687 printed += printf("%s%s",
9688 printed_mask == 0 ? ":(" : "|",
9690 printed_mask |= table[entry].mask;
9694 if (entry >= num_entries)
9697 if (printed_mask != 0)
9698 printed += printf(") ");
9700 printed += printf(" ");
9701 if (cur_column != NULL)
9702 *cur_column += printed;
9707 ahd_dump_card_state(struct ahd_softc *ahd)
9710 ahd_mode_state saved_modes;
9714 u_int saved_scb_index;
9718 if (ahd_is_paused(ahd)) {
9724 saved_modes = ahd_save_modes(ahd);
9725 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
9726 printf(">>>>>>>>>>>>>>>>>> Dump Card State Begins <<<<<<<<<<<<<<<<<\n"
9727 "%s: Dumping Card State at program address 0x%x Mode 0x%x\n",
9729 ahd_inw(ahd, CURADDR),
9730 ahd_build_mode_state(ahd, ahd->saved_src_mode,
9731 ahd->saved_dst_mode));
9733 printf("Card was paused\n");
9735 if (ahd_check_cmdcmpltqueues(ahd))
9736 printf("Completions are pending\n");
9739 * Mode independent registers.
9742 ahd_intstat_print(ahd_inb(ahd, INTSTAT), &cur_col, 50);
9743 ahd_seloid_print(ahd_inb(ahd, SELOID), &cur_col, 50);
9744 ahd_selid_print(ahd_inb(ahd, SELID), &cur_col, 50);
9745 ahd_hs_mailbox_print(ahd_inb(ahd, LOCAL_HS_MAILBOX), &cur_col, 50);
9746 ahd_intctl_print(ahd_inb(ahd, INTCTL), &cur_col, 50);
9747 ahd_seqintstat_print(ahd_inb(ahd, SEQINTSTAT), &cur_col, 50);
9748 ahd_saved_mode_print(ahd_inb(ahd, SAVED_MODE), &cur_col, 50);
9749 ahd_dffstat_print(ahd_inb(ahd, DFFSTAT), &cur_col, 50);
9750 ahd_scsisigi_print(ahd_inb(ahd, SCSISIGI), &cur_col, 50);
9751 ahd_scsiphase_print(ahd_inb(ahd, SCSIPHASE), &cur_col, 50);
9752 ahd_scsibus_print(ahd_inb(ahd, SCSIBUS), &cur_col, 50);
9753 ahd_lastphase_print(ahd_inb(ahd, LASTPHASE), &cur_col, 50);
9754 ahd_scsiseq0_print(ahd_inb(ahd, SCSISEQ0), &cur_col, 50);
9755 ahd_scsiseq1_print(ahd_inb(ahd, SCSISEQ1), &cur_col, 50);
9756 ahd_seqctl0_print(ahd_inb(ahd, SEQCTL0), &cur_col, 50);
9757 ahd_seqintctl_print(ahd_inb(ahd, SEQINTCTL), &cur_col, 50);
9758 ahd_seq_flags_print(ahd_inb(ahd, SEQ_FLAGS), &cur_col, 50);
9759 ahd_seq_flags2_print(ahd_inb(ahd, SEQ_FLAGS2), &cur_col, 50);
9760 ahd_qfreeze_count_print(ahd_inw(ahd, QFREEZE_COUNT), &cur_col, 50);
9761 ahd_kernel_qfreeze_count_print(ahd_inw(ahd, KERNEL_QFREEZE_COUNT),
9763 ahd_mk_message_scb_print(ahd_inw(ahd, MK_MESSAGE_SCB), &cur_col, 50);
9764 ahd_mk_message_scsiid_print(ahd_inb(ahd, MK_MESSAGE_SCSIID),
9766 ahd_sstat0_print(ahd_inb(ahd, SSTAT0), &cur_col, 50);
9767 ahd_sstat1_print(ahd_inb(ahd, SSTAT1), &cur_col, 50);
9768 ahd_sstat2_print(ahd_inb(ahd, SSTAT2), &cur_col, 50);
9769 ahd_sstat3_print(ahd_inb(ahd, SSTAT3), &cur_col, 50);
9770 ahd_perrdiag_print(ahd_inb(ahd, PERRDIAG), &cur_col, 50);
9771 ahd_simode1_print(ahd_inb(ahd, SIMODE1), &cur_col, 50);
9772 ahd_lqistat0_print(ahd_inb(ahd, LQISTAT0), &cur_col, 50);
9773 ahd_lqistat1_print(ahd_inb(ahd, LQISTAT1), &cur_col, 50);
9774 ahd_lqistat2_print(ahd_inb(ahd, LQISTAT2), &cur_col, 50);
9775 ahd_lqostat0_print(ahd_inb(ahd, LQOSTAT0), &cur_col, 50);
9776 ahd_lqostat1_print(ahd_inb(ahd, LQOSTAT1), &cur_col, 50);
9777 ahd_lqostat2_print(ahd_inb(ahd, LQOSTAT2), &cur_col, 50);
9779 printf("\nSCB Count = %d CMDS_PENDING = %d LASTSCB 0x%x "
9780 "CURRSCB 0x%x NEXTSCB 0x%x\n",
9781 ahd->scb_data.numscbs, ahd_inw(ahd, CMDS_PENDING),
9782 ahd_inw(ahd, LASTSCB), ahd_inw(ahd, CURRSCB),
9783 ahd_inw(ahd, NEXTSCB));
9786 ahd_search_qinfifo(ahd, CAM_TARGET_WILDCARD, ALL_CHANNELS,
9787 CAM_LUN_WILDCARD, SCB_LIST_NULL,
9788 ROLE_UNKNOWN, /*status*/0, SEARCH_PRINT);
9789 saved_scb_index = ahd_get_scbptr(ahd);
9790 printf("Pending list:");
9792 LIST_FOREACH(scb, &ahd->pending_scbs, pending_links) {
9793 if (i++ > AHD_SCB_MAX)
9795 cur_col = printf("\n%3d FIFO_USE[0x%x] ", SCB_GET_TAG(scb),
9796 ahd_inb_scbram(ahd, SCB_FIFO_USE_COUNT));
9797 ahd_set_scbptr(ahd, SCB_GET_TAG(scb));
9798 ahd_scb_control_print(ahd_inb_scbram(ahd, SCB_CONTROL),
9800 ahd_scb_scsiid_print(ahd_inb_scbram(ahd, SCB_SCSIID),
9803 printf("\nTotal %d\n", i);
9805 printf("Kernel Free SCB list: ");
9807 TAILQ_FOREACH(scb, &ahd->scb_data.free_scbs, links.tqe) {
9808 struct scb *list_scb;
9812 printf("%d ", SCB_GET_TAG(list_scb));
9813 list_scb = LIST_NEXT(list_scb, collision_links);
9814 } while (list_scb && i++ < AHD_SCB_MAX);
9817 LIST_FOREACH(scb, &ahd->scb_data.any_dev_free_scb_list, links.le) {
9818 if (i++ > AHD_SCB_MAX)
9820 printf("%d ", SCB_GET_TAG(scb));
9824 printf("Sequencer Complete DMA-inprog list: ");
9825 scb_index = ahd_inw(ahd, COMPLETE_SCB_DMAINPROG_HEAD);
9827 while (!SCBID_IS_NULL(scb_index) && i++ < AHD_SCB_MAX) {
9828 ahd_set_scbptr(ahd, scb_index);
9829 printf("%d ", scb_index);
9830 scb_index = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE);
9834 printf("Sequencer Complete list: ");
9835 scb_index = ahd_inw(ahd, COMPLETE_SCB_HEAD);
9837 while (!SCBID_IS_NULL(scb_index) && i++ < AHD_SCB_MAX) {
9838 ahd_set_scbptr(ahd, scb_index);
9839 printf("%d ", scb_index);
9840 scb_index = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE);
9845 printf("Sequencer DMA-Up and Complete list: ");
9846 scb_index = ahd_inw(ahd, COMPLETE_DMA_SCB_HEAD);
9848 while (!SCBID_IS_NULL(scb_index) && i++ < AHD_SCB_MAX) {
9849 ahd_set_scbptr(ahd, scb_index);
9850 printf("%d ", scb_index);
9851 scb_index = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE);
9854 printf("Sequencer On QFreeze and Complete list: ");
9855 scb_index = ahd_inw(ahd, COMPLETE_ON_QFREEZE_HEAD);
9857 while (!SCBID_IS_NULL(scb_index) && i++ < AHD_SCB_MAX) {
9858 ahd_set_scbptr(ahd, scb_index);
9859 printf("%d ", scb_index);
9860 scb_index = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE);
9863 ahd_set_scbptr(ahd, saved_scb_index);
9864 dffstat = ahd_inb(ahd, DFFSTAT);
9865 for (i = 0; i < 2; i++) {
9867 struct scb *fifo_scb;
9871 ahd_set_modes(ahd, AHD_MODE_DFF0 + i, AHD_MODE_DFF0 + i);
9872 fifo_scbptr = ahd_get_scbptr(ahd);
9873 printf("\n\n%s: FIFO%d %s, LONGJMP == 0x%x, SCB 0x%x\n",
9875 (dffstat & (FIFO0FREE << i)) ? "Free" : "Active",
9876 ahd_inw(ahd, LONGJMP_ADDR), fifo_scbptr);
9878 ahd_seqimode_print(ahd_inb(ahd, SEQIMODE), &cur_col, 50);
9879 ahd_seqintsrc_print(ahd_inb(ahd, SEQINTSRC), &cur_col, 50);
9880 ahd_dfcntrl_print(ahd_inb(ahd, DFCNTRL), &cur_col, 50);
9881 ahd_dfstatus_print(ahd_inb(ahd, DFSTATUS), &cur_col, 50);
9882 ahd_sg_cache_shadow_print(ahd_inb(ahd, SG_CACHE_SHADOW),
9884 ahd_sg_state_print(ahd_inb(ahd, SG_STATE), &cur_col, 50);
9885 ahd_dffsxfrctl_print(ahd_inb(ahd, DFFSXFRCTL), &cur_col, 50);
9886 ahd_soffcnt_print(ahd_inb(ahd, SOFFCNT), &cur_col, 50);
9887 ahd_mdffstat_print(ahd_inb(ahd, MDFFSTAT), &cur_col, 50);
9892 cur_col += printf("SHADDR = 0x%x%x, SHCNT = 0x%x ",
9893 ahd_inl(ahd, SHADDR+4),
9894 ahd_inl(ahd, SHADDR),
9895 (ahd_inb(ahd, SHCNT)
9896 | (ahd_inb(ahd, SHCNT + 1) << 8)
9897 | (ahd_inb(ahd, SHCNT + 2) << 16)));
9902 cur_col += printf("HADDR = 0x%x%x, HCNT = 0x%x ",
9903 ahd_inl(ahd, HADDR+4),
9904 ahd_inl(ahd, HADDR),
9906 | (ahd_inb(ahd, HCNT + 1) << 8)
9907 | (ahd_inb(ahd, HCNT + 2) << 16)));
9908 ahd_ccsgctl_print(ahd_inb(ahd, CCSGCTL), &cur_col, 50);
9910 if ((ahd_debug & AHD_SHOW_SG) != 0) {
9911 fifo_scb = ahd_lookup_scb(ahd, fifo_scbptr);
9912 if (fifo_scb != NULL)
9913 ahd_dump_sglist(fifo_scb);
9918 for (i = 0; i < 20; i++)
9919 printf("0x%x ", ahd_inb(ahd, LQIN + i));
9921 ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
9922 printf("%s: LQISTATE = 0x%x, LQOSTATE = 0x%x, OPTIONMODE = 0x%x\n",
9923 ahd_name(ahd), ahd_inb(ahd, LQISTATE), ahd_inb(ahd, LQOSTATE),
9924 ahd_inb(ahd, OPTIONMODE));
9925 printf("%s: OS_SPACE_CNT = 0x%x MAXCMDCNT = 0x%x\n",
9926 ahd_name(ahd), ahd_inb(ahd, OS_SPACE_CNT),
9927 ahd_inb(ahd, MAXCMDCNT));
9928 printf("%s: SAVED_SCSIID = 0x%x SAVED_LUN = 0x%x\n",
9929 ahd_name(ahd), ahd_inb(ahd, SAVED_SCSIID),
9930 ahd_inb(ahd, SAVED_LUN));
9931 ahd_simode0_print(ahd_inb(ahd, SIMODE0), &cur_col, 50);
9933 ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
9935 ahd_ccscbctl_print(ahd_inb(ahd, CCSCBCTL), &cur_col, 50);
9937 ahd_set_modes(ahd, ahd->saved_src_mode, ahd->saved_dst_mode);
9938 printf("%s: REG0 == 0x%x, SINDEX = 0x%x, DINDEX = 0x%x\n",
9939 ahd_name(ahd), ahd_inw(ahd, REG0), ahd_inw(ahd, SINDEX),
9940 ahd_inw(ahd, DINDEX));
9941 printf("%s: SCBPTR == 0x%x, SCB_NEXT == 0x%x, SCB_NEXT2 == 0x%x\n",
9942 ahd_name(ahd), ahd_get_scbptr(ahd),
9943 ahd_inw_scbram(ahd, SCB_NEXT),
9944 ahd_inw_scbram(ahd, SCB_NEXT2));
9945 printf("CDB %x %x %x %x %x %x\n",
9946 ahd_inb_scbram(ahd, SCB_CDB_STORE),
9947 ahd_inb_scbram(ahd, SCB_CDB_STORE+1),
9948 ahd_inb_scbram(ahd, SCB_CDB_STORE+2),
9949 ahd_inb_scbram(ahd, SCB_CDB_STORE+3),
9950 ahd_inb_scbram(ahd, SCB_CDB_STORE+4),
9951 ahd_inb_scbram(ahd, SCB_CDB_STORE+5));
9953 for (i = 0; i < ahd->stack_size; i++) {
9954 ahd->saved_stack[i] =
9955 ahd_inb(ahd, STACK)|(ahd_inb(ahd, STACK) << 8);
9956 printf(" 0x%x", ahd->saved_stack[i]);
9958 for (i = ahd->stack_size-1; i >= 0; i--) {
9959 ahd_outb(ahd, STACK, ahd->saved_stack[i] & 0xFF);
9960 ahd_outb(ahd, STACK, (ahd->saved_stack[i] >> 8) & 0xFF);
9962 printf("\n<<<<<<<<<<<<<<<<< Dump Card State Ends >>>>>>>>>>>>>>>>>>\n");
9963 ahd_restore_modes(ahd, saved_modes);
9970 ahd_dump_scbs(struct ahd_softc *ahd)
9972 ahd_mode_state saved_modes;
9973 u_int saved_scb_index;
9976 saved_modes = ahd_save_modes(ahd);
9977 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
9978 saved_scb_index = ahd_get_scbptr(ahd);
9979 for (i = 0; i < AHD_SCB_MAX; i++) {
9980 ahd_set_scbptr(ahd, i);
9982 printf("(CTRL 0x%x ID 0x%x N 0x%x N2 0x%x SG 0x%x, RSG 0x%x)\n",
9983 ahd_inb_scbram(ahd, SCB_CONTROL),
9984 ahd_inb_scbram(ahd, SCB_SCSIID),
9985 ahd_inw_scbram(ahd, SCB_NEXT),
9986 ahd_inw_scbram(ahd, SCB_NEXT2),
9987 ahd_inl_scbram(ahd, SCB_SGPTR),
9988 ahd_inl_scbram(ahd, SCB_RESIDUAL_SGPTR));
9991 ahd_set_scbptr(ahd, saved_scb_index);
9992 ahd_restore_modes(ahd, saved_modes);
9996 /**************************** Flexport Logic **********************************/
9998 * Read count 16bit words from 16bit word address start_addr from the
9999 * SEEPROM attached to the controller, into buf, using the controller's
10000 * SEEPROM reading state machine. Optionally treat the data as a byte
10001 * stream in terms of byte order.
10004 ahd_read_seeprom(struct ahd_softc *ahd, uint16_t *buf,
10005 u_int start_addr, u_int count, int bytestream)
10012 * If we never make it through the loop even once,
10013 * we were passed invalid arguments.
10016 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
10017 end_addr = start_addr + count;
10018 for (cur_addr = start_addr; cur_addr < end_addr; cur_addr++) {
10020 ahd_outb(ahd, SEEADR, cur_addr);
10021 ahd_outb(ahd, SEECTL, SEEOP_READ | SEESTART);
10023 error = ahd_wait_seeprom(ahd);
10026 if (bytestream != 0) {
10027 uint8_t *bytestream_ptr;
10029 bytestream_ptr = (uint8_t *)buf;
10030 *bytestream_ptr++ = ahd_inb(ahd, SEEDAT);
10031 *bytestream_ptr = ahd_inb(ahd, SEEDAT+1);
10034 * ahd_inw() already handles machine byte order.
10036 *buf = ahd_inw(ahd, SEEDAT);
10044 * Write count 16bit words from buf, into SEEPROM attache to the
10045 * controller starting at 16bit word address start_addr, using the
10046 * controller's SEEPROM writing state machine.
10049 ahd_write_seeprom(struct ahd_softc *ahd, uint16_t *buf,
10050 u_int start_addr, u_int count)
10057 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
10060 /* Place the chip into write-enable mode */
10061 ahd_outb(ahd, SEEADR, SEEOP_EWEN_ADDR);
10062 ahd_outb(ahd, SEECTL, SEEOP_EWEN | SEESTART);
10063 error = ahd_wait_seeprom(ahd);
10068 * Write the data. If we don't get throught the loop at
10069 * least once, the arguments were invalid.
10072 end_addr = start_addr + count;
10073 for (cur_addr = start_addr; cur_addr < end_addr; cur_addr++) {
10074 ahd_outw(ahd, SEEDAT, *buf++);
10075 ahd_outb(ahd, SEEADR, cur_addr);
10076 ahd_outb(ahd, SEECTL, SEEOP_WRITE | SEESTART);
10078 retval = ahd_wait_seeprom(ahd);
10086 ahd_outb(ahd, SEEADR, SEEOP_EWDS_ADDR);
10087 ahd_outb(ahd, SEECTL, SEEOP_EWDS | SEESTART);
10088 error = ahd_wait_seeprom(ahd);
10095 * Wait ~100us for the serial eeprom to satisfy our request.
10098 ahd_wait_seeprom(struct ahd_softc *ahd)
10103 while ((ahd_inb(ahd, SEESTAT) & (SEEARBACK|SEEBUSY)) != 0 && --cnt)
10107 return (ETIMEDOUT);
10112 * Validate the two checksums in the per_channel
10113 * vital product data struct.
10116 ahd_verify_vpd_cksum(struct vpd_config *vpd)
10123 vpdarray = (uint8_t *)vpd;
10124 maxaddr = offsetof(struct vpd_config, vpd_checksum);
10126 for (i = offsetof(struct vpd_config, resource_type); i < maxaddr; i++)
10127 checksum = checksum + vpdarray[i];
10129 || (-checksum & 0xFF) != vpd->vpd_checksum)
10133 maxaddr = offsetof(struct vpd_config, checksum);
10134 for (i = offsetof(struct vpd_config, default_target_flags);
10136 checksum = checksum + vpdarray[i];
10138 || (-checksum & 0xFF) != vpd->checksum)
10144 ahd_verify_cksum(struct seeprom_config *sc)
10151 maxaddr = (sizeof(*sc)/2) - 1;
10153 scarray = (uint16_t *)sc;
10155 for (i = 0; i < maxaddr; i++)
10156 checksum = checksum + scarray[i];
10158 || (checksum & 0xFFFF) != sc->checksum) {
10166 ahd_acquire_seeprom(struct ahd_softc *ahd)
10169 * We should be able to determine the SEEPROM type
10170 * from the flexport logic, but unfortunately not
10171 * all implementations have this logic and there is
10172 * no programatic method for determining if the logic
10180 error = ahd_read_flexport(ahd, FLXADDR_ROMSTAT_CURSENSECTL, &seetype);
10182 || ((seetype & FLX_ROMSTAT_SEECFG) == FLX_ROMSTAT_SEE_NONE))
10189 ahd_release_seeprom(struct ahd_softc *ahd)
10191 /* Currently a no-op */
10195 * Wait at most 2 seconds for flexport arbitration to succeed.
10198 ahd_wait_flexport(struct ahd_softc *ahd)
10202 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
10203 cnt = 1000000 * 2 / 5;
10204 while ((ahd_inb(ahd, BRDCTL) & FLXARBACK) == 0 && --cnt)
10208 return (ETIMEDOUT);
10213 ahd_write_flexport(struct ahd_softc *ahd, u_int addr, u_int value)
10217 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
10219 panic("ahd_write_flexport: address out of range");
10220 ahd_outb(ahd, BRDCTL, BRDEN|(addr << 3));
10221 error = ahd_wait_flexport(ahd);
10224 ahd_outb(ahd, BRDDAT, value);
10225 ahd_flush_device_writes(ahd);
10226 ahd_outb(ahd, BRDCTL, BRDSTB|BRDEN|(addr << 3));
10227 ahd_flush_device_writes(ahd);
10228 ahd_outb(ahd, BRDCTL, BRDEN|(addr << 3));
10229 ahd_flush_device_writes(ahd);
10230 ahd_outb(ahd, BRDCTL, 0);
10231 ahd_flush_device_writes(ahd);
10236 ahd_read_flexport(struct ahd_softc *ahd, u_int addr, uint8_t *value)
10240 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
10242 panic("ahd_read_flexport: address out of range");
10243 ahd_outb(ahd, BRDCTL, BRDRW|BRDEN|(addr << 3));
10244 error = ahd_wait_flexport(ahd);
10247 *value = ahd_inb(ahd, BRDDAT);
10248 ahd_outb(ahd, BRDCTL, 0);
10249 ahd_flush_device_writes(ahd);
10253 /************************* Target Mode ****************************************/
10254 #ifdef AHD_TARGET_MODE
10256 ahd_find_tmode_devs(struct ahd_softc *ahd, struct cam_sim *sim, union ccb *ccb,
10257 struct ahd_tmode_tstate **tstate,
10258 struct ahd_tmode_lstate **lstate,
10259 int notfound_failure)
10262 if ((ahd->features & AHD_TARGETMODE) == 0)
10263 return (CAM_REQ_INVALID);
10266 * Handle the 'black hole' device that sucks up
10267 * requests to unattached luns on enabled targets.
10269 if (ccb->ccb_h.target_id == CAM_TARGET_WILDCARD
10270 && ccb->ccb_h.target_lun == CAM_LUN_WILDCARD) {
10272 *lstate = ahd->black_hole;
10276 max_id = (ahd->features & AHD_WIDE) ? 16 : 8;
10277 if (ccb->ccb_h.target_id >= max_id)
10278 return (CAM_TID_INVALID);
10280 if (ccb->ccb_h.target_lun >= AHD_NUM_LUNS)
10281 return (CAM_LUN_INVALID);
10283 *tstate = ahd->enabled_targets[ccb->ccb_h.target_id];
10285 if (*tstate != NULL)
10287 (*tstate)->enabled_luns[ccb->ccb_h.target_lun];
10290 if (notfound_failure != 0 && *lstate == NULL)
10291 return (CAM_PATH_INVALID);
10293 return (CAM_REQ_CMP);
10297 ahd_handle_en_lun(struct ahd_softc *ahd, struct cam_sim *sim, union ccb *ccb)
10300 struct ahd_tmode_tstate *tstate;
10301 struct ahd_tmode_lstate *lstate;
10302 struct ccb_en_lun *cel;
10310 status = ahd_find_tmode_devs(ahd, sim, ccb, &tstate, &lstate,
10311 /*notfound_failure*/FALSE);
10313 if (status != CAM_REQ_CMP) {
10314 ccb->ccb_h.status = status;
10318 if ((ahd->features & AHD_MULTIROLE) != 0) {
10321 our_id = ahd->our_id;
10322 if (ccb->ccb_h.target_id != our_id) {
10323 if ((ahd->features & AHD_MULTI_TID) != 0
10324 && (ahd->flags & AHD_INITIATORROLE) != 0) {
10326 * Only allow additional targets if
10327 * the initiator role is disabled.
10328 * The hardware cannot handle a re-select-in
10329 * on the initiator id during a re-select-out
10330 * on a different target id.
10332 status = CAM_TID_INVALID;
10333 } else if ((ahd->flags & AHD_INITIATORROLE) != 0
10334 || ahd->enabled_luns > 0) {
10336 * Only allow our target id to change
10337 * if the initiator role is not configured
10338 * and there are no enabled luns which
10339 * are attached to the currently registered
10342 status = CAM_TID_INVALID;
10347 if (status != CAM_REQ_CMP) {
10348 ccb->ccb_h.status = status;
10353 * We now have an id that is valid.
10354 * If we aren't in target mode, switch modes.
10356 if ((ahd->flags & AHD_TARGETROLE) == 0
10357 && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
10360 printf("Configuring Target Mode\n");
10362 if (LIST_FIRST(&ahd->pending_scbs) != NULL) {
10363 ccb->ccb_h.status = CAM_BUSY;
10364 ahd_unlock(ahd, &s);
10367 ahd->flags |= AHD_TARGETROLE;
10368 if ((ahd->features & AHD_MULTIROLE) == 0)
10369 ahd->flags &= ~AHD_INITIATORROLE;
10373 ahd_unlock(ahd, &s);
10376 target = ccb->ccb_h.target_id;
10377 lun = ccb->ccb_h.target_lun;
10378 channel = SIM_CHANNEL(ahd, sim);
10379 target_mask = 0x01 << target;
10380 if (channel == 'B')
10383 if (cel->enable != 0) {
10386 /* Are we already enabled?? */
10387 if (lstate != NULL) {
10388 xpt_print_path(ccb->ccb_h.path);
10389 printf("Lun already enabled\n");
10390 ccb->ccb_h.status = CAM_LUN_ALRDY_ENA;
10394 if (cel->grp6_len != 0
10395 || cel->grp7_len != 0) {
10397 * Don't (yet?) support vendor
10398 * specific commands.
10400 ccb->ccb_h.status = CAM_REQ_INVALID;
10401 printf("Non-zero Group Codes\n");
10406 * Seems to be okay.
10407 * Setup our data structures.
10409 if (target != CAM_TARGET_WILDCARD && tstate == NULL) {
10410 tstate = ahd_alloc_tstate(ahd, target, channel);
10411 if (tstate == NULL) {
10412 xpt_print_path(ccb->ccb_h.path);
10413 printf("Couldn't allocate tstate\n");
10414 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
10418 lstate = malloc(sizeof(*lstate), M_DEVBUF, M_NOWAIT);
10419 if (lstate == NULL) {
10420 xpt_print_path(ccb->ccb_h.path);
10421 printf("Couldn't allocate lstate\n");
10422 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
10425 memset(lstate, 0, sizeof(*lstate));
10426 status = xpt_create_path(&lstate->path, /*periph*/NULL,
10427 xpt_path_path_id(ccb->ccb_h.path),
10428 xpt_path_target_id(ccb->ccb_h.path),
10429 xpt_path_lun_id(ccb->ccb_h.path));
10430 if (status != CAM_REQ_CMP) {
10431 free(lstate, M_DEVBUF);
10432 xpt_print_path(ccb->ccb_h.path);
10433 printf("Couldn't allocate path\n");
10434 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
10437 SLIST_INIT(&lstate->accept_tios);
10438 SLIST_INIT(&lstate->immed_notifies);
10441 if (target != CAM_TARGET_WILDCARD) {
10442 tstate->enabled_luns[lun] = lstate;
10443 ahd->enabled_luns++;
10445 if ((ahd->features & AHD_MULTI_TID) != 0) {
10448 targid_mask = ahd_inw(ahd, TARGID);
10449 targid_mask |= target_mask;
10450 ahd_outw(ahd, TARGID, targid_mask);
10451 ahd_update_scsiid(ahd, targid_mask);
10456 channel = SIM_CHANNEL(ahd, sim);
10457 our_id = SIM_SCSI_ID(ahd, sim);
10460 * This can only happen if selections
10463 if (target != our_id) {
10468 sblkctl = ahd_inb(ahd, SBLKCTL);
10469 cur_channel = (sblkctl & SELBUSB)
10471 if ((ahd->features & AHD_TWIN) == 0)
10473 swap = cur_channel != channel;
10474 ahd->our_id = target;
10477 ahd_outb(ahd, SBLKCTL,
10478 sblkctl ^ SELBUSB);
10480 ahd_outb(ahd, SCSIID, target);
10483 ahd_outb(ahd, SBLKCTL, sblkctl);
10487 ahd->black_hole = lstate;
10488 /* Allow select-in operations */
10489 if (ahd->black_hole != NULL && ahd->enabled_luns > 0) {
10490 scsiseq1 = ahd_inb(ahd, SCSISEQ_TEMPLATE);
10491 scsiseq1 |= ENSELI;
10492 ahd_outb(ahd, SCSISEQ_TEMPLATE, scsiseq1);
10493 scsiseq1 = ahd_inb(ahd, SCSISEQ1);
10494 scsiseq1 |= ENSELI;
10495 ahd_outb(ahd, SCSISEQ1, scsiseq1);
10498 ahd_unlock(ahd, &s);
10499 ccb->ccb_h.status = CAM_REQ_CMP;
10500 xpt_print_path(ccb->ccb_h.path);
10501 printf("Lun now enabled for target mode\n");
10506 if (lstate == NULL) {
10507 ccb->ccb_h.status = CAM_LUN_INVALID;
10513 ccb->ccb_h.status = CAM_REQ_CMP;
10514 LIST_FOREACH(scb, &ahd->pending_scbs, pending_links) {
10515 struct ccb_hdr *ccbh;
10517 ccbh = &scb->io_ctx->ccb_h;
10518 if (ccbh->func_code == XPT_CONT_TARGET_IO
10519 && !xpt_path_comp(ccbh->path, ccb->ccb_h.path)){
10520 printf("CTIO pending\n");
10521 ccb->ccb_h.status = CAM_REQ_INVALID;
10522 ahd_unlock(ahd, &s);
10527 if (SLIST_FIRST(&lstate->accept_tios) != NULL) {
10528 printf("ATIOs pending\n");
10529 ccb->ccb_h.status = CAM_REQ_INVALID;
10532 if (SLIST_FIRST(&lstate->immed_notifies) != NULL) {
10533 printf("INOTs pending\n");
10534 ccb->ccb_h.status = CAM_REQ_INVALID;
10537 if (ccb->ccb_h.status != CAM_REQ_CMP) {
10538 ahd_unlock(ahd, &s);
10542 xpt_print_path(ccb->ccb_h.path);
10543 printf("Target mode disabled\n");
10544 xpt_free_path(lstate->path);
10545 free(lstate, M_DEVBUF);
10548 /* Can we clean up the target too? */
10549 if (target != CAM_TARGET_WILDCARD) {
10550 tstate->enabled_luns[lun] = NULL;
10551 ahd->enabled_luns--;
10552 for (empty = 1, i = 0; i < 8; i++)
10553 if (tstate->enabled_luns[i] != NULL) {
10559 ahd_free_tstate(ahd, target, channel,
10561 if (ahd->features & AHD_MULTI_TID) {
10564 targid_mask = ahd_inw(ahd, TARGID);
10565 targid_mask &= ~target_mask;
10566 ahd_outw(ahd, TARGID, targid_mask);
10567 ahd_update_scsiid(ahd, targid_mask);
10572 ahd->black_hole = NULL;
10575 * We can't allow selections without
10576 * our black hole device.
10580 if (ahd->enabled_luns == 0) {
10581 /* Disallow select-in */
10584 scsiseq1 = ahd_inb(ahd, SCSISEQ_TEMPLATE);
10585 scsiseq1 &= ~ENSELI;
10586 ahd_outb(ahd, SCSISEQ_TEMPLATE, scsiseq1);
10587 scsiseq1 = ahd_inb(ahd, SCSISEQ1);
10588 scsiseq1 &= ~ENSELI;
10589 ahd_outb(ahd, SCSISEQ1, scsiseq1);
10591 if ((ahd->features & AHD_MULTIROLE) == 0) {
10592 printf("Configuring Initiator Mode\n");
10593 ahd->flags &= ~AHD_TARGETROLE;
10594 ahd->flags |= AHD_INITIATORROLE;
10599 * Unpaused. The extra unpause
10600 * that follows is harmless.
10605 ahd_unlock(ahd, &s);
10611 ahd_update_scsiid(struct ahd_softc *ahd, u_int targid_mask)
10617 if ((ahd->features & AHD_MULTI_TID) == 0)
10618 panic("ahd_update_scsiid called on non-multitid unit\n");
10621 * Since we will rely on the TARGID mask
10622 * for selection enables, ensure that OID
10623 * in SCSIID is not set to some other ID
10624 * that we don't want to allow selections on.
10626 if ((ahd->features & AHD_ULTRA2) != 0)
10627 scsiid = ahd_inb(ahd, SCSIID_ULTRA2);
10629 scsiid = ahd_inb(ahd, SCSIID);
10630 scsiid_mask = 0x1 << (scsiid & OID);
10631 if ((targid_mask & scsiid_mask) == 0) {
10634 /* ffs counts from 1 */
10635 our_id = ffs(targid_mask);
10637 our_id = ahd->our_id;
10643 if ((ahd->features & AHD_ULTRA2) != 0)
10644 ahd_outb(ahd, SCSIID_ULTRA2, scsiid);
10646 ahd_outb(ahd, SCSIID, scsiid);
10651 ahd_run_tqinfifo(struct ahd_softc *ahd, int paused)
10653 struct target_cmd *cmd;
10655 ahd_sync_tqinfifo(ahd, BUS_DMASYNC_POSTREAD);
10656 while ((cmd = &ahd->targetcmds[ahd->tqinfifonext])->cmd_valid != 0) {
10659 * Only advance through the queue if we
10660 * have the resources to process the command.
10662 if (ahd_handle_target_cmd(ahd, cmd) != 0)
10665 cmd->cmd_valid = 0;
10666 ahd_dmamap_sync(ahd, ahd->shared_data_dmat,
10667 ahd->shared_data_map.dmamap,
10668 ahd_targetcmd_offset(ahd, ahd->tqinfifonext),
10669 sizeof(struct target_cmd),
10670 BUS_DMASYNC_PREREAD);
10671 ahd->tqinfifonext++;
10674 * Lazily update our position in the target mode incoming
10675 * command queue as seen by the sequencer.
10677 if ((ahd->tqinfifonext & (HOST_TQINPOS - 1)) == 1) {
10680 hs_mailbox = ahd_inb(ahd, HS_MAILBOX);
10681 hs_mailbox &= ~HOST_TQINPOS;
10682 hs_mailbox |= ahd->tqinfifonext & HOST_TQINPOS;
10683 ahd_outb(ahd, HS_MAILBOX, hs_mailbox);
10689 ahd_handle_target_cmd(struct ahd_softc *ahd, struct target_cmd *cmd)
10691 struct ahd_tmode_tstate *tstate;
10692 struct ahd_tmode_lstate *lstate;
10693 struct ccb_accept_tio *atio;
10699 initiator = SCSIID_TARGET(ahd, cmd->scsiid);
10700 target = SCSIID_OUR_ID(cmd->scsiid);
10701 lun = (cmd->identify & MSG_IDENTIFY_LUNMASK);
10704 tstate = ahd->enabled_targets[target];
10706 if (tstate != NULL)
10707 lstate = tstate->enabled_luns[lun];
10710 * Commands for disabled luns go to the black hole driver.
10712 if (lstate == NULL)
10713 lstate = ahd->black_hole;
10715 atio = (struct ccb_accept_tio*)SLIST_FIRST(&lstate->accept_tios);
10716 if (atio == NULL) {
10717 ahd->flags |= AHD_TQINFIFO_BLOCKED;
10719 * Wait for more ATIOs from the peripheral driver for this lun.
10723 ahd->flags &= ~AHD_TQINFIFO_BLOCKED;
10725 if ((ahd_debug & AHD_SHOW_TQIN) != 0)
10726 printf("Incoming command from %d for %d:%d%s\n",
10727 initiator, target, lun,
10728 lstate == ahd->black_hole ? "(Black Holed)" : "");
10730 SLIST_REMOVE_HEAD(&lstate->accept_tios, sim_links.sle);
10732 if (lstate == ahd->black_hole) {
10733 /* Fill in the wildcards */
10734 atio->ccb_h.target_id = target;
10735 atio->ccb_h.target_lun = lun;
10739 * Package it up and send it off to
10740 * whomever has this lun enabled.
10742 atio->sense_len = 0;
10743 atio->init_id = initiator;
10744 if (byte[0] != 0xFF) {
10745 /* Tag was included */
10746 atio->tag_action = *byte++;
10747 atio->tag_id = *byte++;
10748 atio->ccb_h.flags = CAM_TAG_ACTION_VALID;
10750 atio->ccb_h.flags = 0;
10754 /* Okay. Now determine the cdb size based on the command code */
10755 switch (*byte >> CMD_GROUP_CODE_SHIFT) {
10761 atio->cdb_len = 10;
10764 atio->cdb_len = 16;
10767 atio->cdb_len = 12;
10771 /* Only copy the opcode. */
10773 printf("Reserved or VU command code type encountered\n");
10777 memcpy(atio->cdb_io.cdb_bytes, byte, atio->cdb_len);
10779 atio->ccb_h.status |= CAM_CDB_RECVD;
10781 if ((cmd->identify & MSG_IDENTIFY_DISCFLAG) == 0) {
10783 * We weren't allowed to disconnect.
10784 * We're hanging on the bus until a
10785 * continue target I/O comes in response
10786 * to this accept tio.
10789 if ((ahd_debug & AHD_SHOW_TQIN) != 0)
10790 printf("Received Immediate Command %d:%d:%d - %p\n",
10791 initiator, target, lun, ahd->pending_device);
10793 ahd->pending_device = lstate;
10794 ahd_freeze_ccb((union ccb *)atio);
10795 atio->ccb_h.flags |= CAM_DIS_DISCONNECT;
10797 xpt_done((union ccb*)atio);