2 * Core routines and tables shareable across OS platforms.
4 * Copyright (c) 1994-2002 Justin T. Gibbs.
5 * Copyright (c) 2000-2002 Adaptec Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions, and the following disclaimer,
13 * without modification.
14 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
15 * substantially similar to the "NO WARRANTY" disclaimer below
16 * ("Disclaimer") and any redistribution must be conditioned upon
17 * including a substantially similar Disclaimer requirement for further
18 * binary redistribution.
19 * 3. Neither the names of the above-listed copyright holders nor the names
20 * of any contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
23 * Alternatively, this software may be distributed under the terms of the
24 * GNU General Public License ("GPL") version 2 as published by the Free
25 * Software Foundation.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
37 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGES.
40 * $Id: //depot/aic7xxx/aic7xxx/aic7xxx.c#155 $
44 #include "aic7xxx_osm.h"
45 #include "aic7xxx_inline.h"
46 #include "aicasm/aicasm_insformat.h"
48 #include <dev/aic7xxx/aic7xxx_osm.h>
49 #include <dev/aic7xxx/aic7xxx_inline.h>
50 #include <dev/aic7xxx/aicasm/aicasm_insformat.h>
53 /***************************** Lookup Tables **********************************/
54 static const char *const ahc_chip_names
[] = {
70 static const u_int num_chip_names
= ARRAY_SIZE(ahc_chip_names
);
73 * Hardware error codes.
75 struct ahc_hard_error_entry
{
80 static const struct ahc_hard_error_entry ahc_hard_errors
[] = {
81 { ILLHADDR
, "Illegal Host Access" },
82 { ILLSADDR
, "Illegal Sequencer Address referrenced" },
83 { ILLOPCODE
, "Illegal Opcode in sequencer program" },
84 { SQPARERR
, "Sequencer Parity Error" },
85 { DPARERR
, "Data-path Parity Error" },
86 { MPARERR
, "Scratch or SCB Memory Parity Error" },
87 { PCIERRSTAT
, "PCI Error detected" },
88 { CIOPARERR
, "CIOBUS Parity Error" },
90 static const u_int num_errors
= ARRAY_SIZE(ahc_hard_errors
);
92 static const struct ahc_phase_table_entry ahc_phase_table
[] =
94 { P_DATAOUT
, MSG_NOOP
, "in Data-out phase" },
95 { P_DATAIN
, MSG_INITIATOR_DET_ERR
, "in Data-in phase" },
96 { P_DATAOUT_DT
, MSG_NOOP
, "in DT Data-out phase" },
97 { P_DATAIN_DT
, MSG_INITIATOR_DET_ERR
, "in DT Data-in phase" },
98 { P_COMMAND
, MSG_NOOP
, "in Command phase" },
99 { P_MESGOUT
, MSG_NOOP
, "in Message-out phase" },
100 { P_STATUS
, MSG_INITIATOR_DET_ERR
, "in Status phase" },
101 { P_MESGIN
, MSG_PARITY_ERROR
, "in Message-in phase" },
102 { P_BUSFREE
, MSG_NOOP
, "while idle" },
103 { 0, MSG_NOOP
, "in unknown phase" }
107 * In most cases we only wish to itterate over real phases, so
108 * exclude the last element from the count.
110 static const u_int num_phases
= ARRAY_SIZE(ahc_phase_table
) - 1;
113 * Valid SCSIRATE values. (p. 3-17)
114 * Provides a mapping of tranfer periods in ns to the proper value to
115 * stick in the scsixfer reg.
117 static const struct ahc_syncrate ahc_syncrates
[] =
119 /* ultra2 fast/ultra period rate */
120 { 0x42, 0x000, 9, "80.0" },
121 { 0x03, 0x000, 10, "40.0" },
122 { 0x04, 0x000, 11, "33.0" },
123 { 0x05, 0x100, 12, "20.0" },
124 { 0x06, 0x110, 15, "16.0" },
125 { 0x07, 0x120, 18, "13.4" },
126 { 0x08, 0x000, 25, "10.0" },
127 { 0x19, 0x010, 31, "8.0" },
128 { 0x1a, 0x020, 37, "6.67" },
129 { 0x1b, 0x030, 43, "5.7" },
130 { 0x1c, 0x040, 50, "5.0" },
131 { 0x00, 0x050, 56, "4.4" },
132 { 0x00, 0x060, 62, "4.0" },
133 { 0x00, 0x070, 68, "3.6" },
134 { 0x00, 0x000, 0, NULL
}
137 /* Our Sequencer Program */
138 #include "aic7xxx_seq.h"
140 /**************************** Function Declarations ***************************/
141 static void ahc_force_renegotiation(struct ahc_softc
*ahc
,
142 struct ahc_devinfo
*devinfo
);
143 static struct ahc_tmode_tstate
*
144 ahc_alloc_tstate(struct ahc_softc
*ahc
,
145 u_int scsi_id
, char channel
);
146 #ifdef AHC_TARGET_MODE
147 static void ahc_free_tstate(struct ahc_softc
*ahc
,
148 u_int scsi_id
, char channel
, int force
);
150 static const struct ahc_syncrate
*
151 ahc_devlimited_syncrate(struct ahc_softc
*ahc
,
152 struct ahc_initiator_tinfo
*,
156 static void ahc_update_pending_scbs(struct ahc_softc
*ahc
);
157 static void ahc_fetch_devinfo(struct ahc_softc
*ahc
,
158 struct ahc_devinfo
*devinfo
);
159 static void ahc_scb_devinfo(struct ahc_softc
*ahc
,
160 struct ahc_devinfo
*devinfo
,
162 static void ahc_assert_atn(struct ahc_softc
*ahc
);
163 static void ahc_setup_initiator_msgout(struct ahc_softc
*ahc
,
164 struct ahc_devinfo
*devinfo
,
166 static void ahc_build_transfer_msg(struct ahc_softc
*ahc
,
167 struct ahc_devinfo
*devinfo
);
168 static void ahc_construct_sdtr(struct ahc_softc
*ahc
,
169 struct ahc_devinfo
*devinfo
,
170 u_int period
, u_int offset
);
171 static void ahc_construct_wdtr(struct ahc_softc
*ahc
,
172 struct ahc_devinfo
*devinfo
,
174 static void ahc_construct_ppr(struct ahc_softc
*ahc
,
175 struct ahc_devinfo
*devinfo
,
176 u_int period
, u_int offset
,
177 u_int bus_width
, u_int ppr_options
);
178 static void ahc_clear_msg_state(struct ahc_softc
*ahc
);
179 static void ahc_handle_proto_violation(struct ahc_softc
*ahc
);
180 static void ahc_handle_message_phase(struct ahc_softc
*ahc
);
186 static int ahc_sent_msg(struct ahc_softc
*ahc
, ahc_msgtype type
,
187 u_int msgval
, int full
);
188 static int ahc_parse_msg(struct ahc_softc
*ahc
,
189 struct ahc_devinfo
*devinfo
);
190 static int ahc_handle_msg_reject(struct ahc_softc
*ahc
,
191 struct ahc_devinfo
*devinfo
);
192 static void ahc_handle_ign_wide_residue(struct ahc_softc
*ahc
,
193 struct ahc_devinfo
*devinfo
);
194 static void ahc_reinitialize_dataptrs(struct ahc_softc
*ahc
);
195 static void ahc_handle_devreset(struct ahc_softc
*ahc
,
196 struct ahc_devinfo
*devinfo
,
197 cam_status status
, char *message
,
199 #ifdef AHC_TARGET_MODE
200 static void ahc_setup_target_msgin(struct ahc_softc
*ahc
,
201 struct ahc_devinfo
*devinfo
,
205 static bus_dmamap_callback_t ahc_dmamap_cb
;
206 static void ahc_build_free_scb_list(struct ahc_softc
*ahc
);
207 static int ahc_init_scbdata(struct ahc_softc
*ahc
);
208 static void ahc_fini_scbdata(struct ahc_softc
*ahc
);
209 static void ahc_qinfifo_requeue(struct ahc_softc
*ahc
,
210 struct scb
*prev_scb
,
212 static int ahc_qinfifo_count(struct ahc_softc
*ahc
);
213 static u_int
ahc_rem_scb_from_disc_list(struct ahc_softc
*ahc
,
214 u_int prev
, u_int scbptr
);
215 static void ahc_add_curscb_to_free_list(struct ahc_softc
*ahc
);
216 static u_int
ahc_rem_wscb(struct ahc_softc
*ahc
,
217 u_int scbpos
, u_int prev
);
218 static void ahc_reset_current_bus(struct ahc_softc
*ahc
);
220 static void ahc_dumpseq(struct ahc_softc
*ahc
);
222 static int ahc_loadseq(struct ahc_softc
*ahc
);
223 static int ahc_check_patch(struct ahc_softc
*ahc
,
224 const struct patch
**start_patch
,
225 u_int start_instr
, u_int
*skip_addr
);
226 static void ahc_download_instr(struct ahc_softc
*ahc
,
227 u_int instrptr
, uint8_t *dconsts
);
228 #ifdef AHC_TARGET_MODE
229 static void ahc_queue_lstate_event(struct ahc_softc
*ahc
,
230 struct ahc_tmode_lstate
*lstate
,
234 static void ahc_update_scsiid(struct ahc_softc
*ahc
,
236 static int ahc_handle_target_cmd(struct ahc_softc
*ahc
,
237 struct target_cmd
*cmd
);
240 static u_int
ahc_index_busy_tcl(struct ahc_softc
*ahc
, u_int tcl
);
241 static void ahc_unbusy_tcl(struct ahc_softc
*ahc
, u_int tcl
);
242 static void ahc_busy_tcl(struct ahc_softc
*ahc
,
243 u_int tcl
, u_int busyid
);
245 /************************** SCB and SCB queue management **********************/
246 static void ahc_run_untagged_queues(struct ahc_softc
*ahc
);
247 static void ahc_run_untagged_queue(struct ahc_softc
*ahc
,
248 struct scb_tailq
*queue
);
250 /****************************** Initialization ********************************/
251 static void ahc_alloc_scbs(struct ahc_softc
*ahc
);
252 static void ahc_shutdown(void *arg
);
254 /*************************** Interrupt Services *******************************/
255 static void ahc_clear_intstat(struct ahc_softc
*ahc
);
256 static void ahc_run_qoutfifo(struct ahc_softc
*ahc
);
257 #ifdef AHC_TARGET_MODE
258 static void ahc_run_tqinfifo(struct ahc_softc
*ahc
, int paused
);
260 static void ahc_handle_brkadrint(struct ahc_softc
*ahc
);
261 static void ahc_handle_seqint(struct ahc_softc
*ahc
, u_int intstat
);
262 static void ahc_handle_scsiint(struct ahc_softc
*ahc
,
264 static void ahc_clear_critical_section(struct ahc_softc
*ahc
);
266 /***************************** Error Recovery *********************************/
267 static void ahc_freeze_devq(struct ahc_softc
*ahc
, struct scb
*scb
);
268 static int ahc_abort_scbs(struct ahc_softc
*ahc
, int target
,
269 char channel
, int lun
, u_int tag
,
270 role_t role
, uint32_t status
);
271 static void ahc_calc_residual(struct ahc_softc
*ahc
,
274 /*********************** Untagged Transaction Routines ************************/
275 static inline void ahc_freeze_untagged_queues(struct ahc_softc
*ahc
);
276 static inline void ahc_release_untagged_queues(struct ahc_softc
*ahc
);
279 * Block our completion routine from starting the next untagged
280 * transaction for this target or target lun.
283 ahc_freeze_untagged_queues(struct ahc_softc
*ahc
)
285 if ((ahc
->flags
& AHC_SCB_BTT
) == 0)
286 ahc
->untagged_queue_lock
++;
290 * Allow the next untagged transaction for this target or target lun
291 * to be executed. We use a counting semaphore to allow the lock
292 * to be acquired recursively. Once the count drops to zero, the
293 * transaction queues will be run.
296 ahc_release_untagged_queues(struct ahc_softc
*ahc
)
298 if ((ahc
->flags
& AHC_SCB_BTT
) == 0) {
299 ahc
->untagged_queue_lock
--;
300 if (ahc
->untagged_queue_lock
== 0)
301 ahc_run_untagged_queues(ahc
);
305 /************************* Sequencer Execution Control ************************/
307 ahc_pause_bug_fix(struct ahc_softc
*ahc
)
309 if ((ahc
->features
& AHC_ULTRA2
) != 0)
310 (void)ahc_inb(ahc
, CCSCBCTL
);
314 * Determine whether the sequencer has halted code execution.
315 * Returns non-zero status if the sequencer is stopped.
318 ahc_is_paused(struct ahc_softc
*ahc
)
320 return ((ahc_inb(ahc
, HCNTRL
) & PAUSE
) != 0);
324 * Request that the sequencer stop and wait, indefinitely, for it
325 * to stop. The sequencer will only acknowledge that it is paused
326 * once it has reached an instruction boundary and PAUSEDIS is
327 * cleared in the SEQCTL register. The sequencer may use PAUSEDIS
328 * for critical sections.
331 ahc_pause(struct ahc_softc
*ahc
)
333 ahc_outb(ahc
, HCNTRL
, ahc
->pause
);
336 * Since the sequencer can disable pausing in a critical section, we
337 * must loop until it actually stops.
339 while (ahc_is_paused(ahc
) == 0)
342 ahc_pause_bug_fix(ahc
);
346 * Allow the sequencer to continue program execution.
347 * We check here to ensure that no additional interrupt
348 * sources that would cause the sequencer to halt have been
349 * asserted. If, for example, a SCSI bus reset is detected
350 * while we are fielding a different, pausing, interrupt type,
351 * we don't want to release the sequencer before going back
352 * into our interrupt handler and dealing with this new
356 ahc_unpause(struct ahc_softc
*ahc
)
358 if ((ahc_inb(ahc
, INTSTAT
) & (SCSIINT
| SEQINT
| BRKADRINT
)) == 0)
359 ahc_outb(ahc
, HCNTRL
, ahc
->unpause
);
362 /************************** Memory mapping routines ***************************/
363 static struct ahc_dma_seg
*
364 ahc_sg_bus_to_virt(struct scb
*scb
, uint32_t sg_busaddr
)
368 sg_index
= (sg_busaddr
- scb
->sg_list_phys
)/sizeof(struct ahc_dma_seg
);
369 /* sg_list_phys points to entry 1, not 0 */
372 return (&scb
->sg_list
[sg_index
]);
376 ahc_sg_virt_to_bus(struct scb
*scb
, struct ahc_dma_seg
*sg
)
380 /* sg_list_phys points to entry 1, not 0 */
381 sg_index
= sg
- &scb
->sg_list
[1];
383 return (scb
->sg_list_phys
+ (sg_index
* sizeof(*scb
->sg_list
)));
387 ahc_hscb_busaddr(struct ahc_softc
*ahc
, u_int index
)
389 return (ahc
->scb_data
->hscb_busaddr
390 + (sizeof(struct hardware_scb
) * index
));
394 ahc_sync_scb(struct ahc_softc
*ahc
, struct scb
*scb
, int op
)
396 ahc_dmamap_sync(ahc
, ahc
->scb_data
->hscb_dmat
,
397 ahc
->scb_data
->hscb_dmamap
,
398 /*offset*/(scb
->hscb
- ahc
->hscbs
) * sizeof(*scb
->hscb
),
399 /*len*/sizeof(*scb
->hscb
), op
);
403 ahc_sync_sglist(struct ahc_softc
*ahc
, struct scb
*scb
, int op
)
405 if (scb
->sg_count
== 0)
408 ahc_dmamap_sync(ahc
, ahc
->scb_data
->sg_dmat
, scb
->sg_map
->sg_dmamap
,
409 /*offset*/(scb
->sg_list
- scb
->sg_map
->sg_vaddr
)
410 * sizeof(struct ahc_dma_seg
),
411 /*len*/sizeof(struct ahc_dma_seg
) * scb
->sg_count
, op
);
414 #ifdef AHC_TARGET_MODE
416 ahc_targetcmd_offset(struct ahc_softc
*ahc
, u_int index
)
418 return (((uint8_t *)&ahc
->targetcmds
[index
]) - ahc
->qoutfifo
);
422 /*********************** Miscelaneous Support Functions ***********************/
424 * Determine whether the sequencer reported a residual
425 * for this SCB/transaction.
428 ahc_update_residual(struct ahc_softc
*ahc
, struct scb
*scb
)
432 sgptr
= ahc_le32toh(scb
->hscb
->sgptr
);
433 if ((sgptr
& SG_RESID_VALID
) != 0)
434 ahc_calc_residual(ahc
, scb
);
438 * Return pointers to the transfer negotiation information
439 * for the specified our_id/remote_id pair.
441 struct ahc_initiator_tinfo
*
442 ahc_fetch_transinfo(struct ahc_softc
*ahc
, char channel
, u_int our_id
,
443 u_int remote_id
, struct ahc_tmode_tstate
**tstate
)
446 * Transfer data structures are stored from the perspective
447 * of the target role. Since the parameters for a connection
448 * in the initiator role to a given target are the same as
449 * when the roles are reversed, we pretend we are the target.
453 *tstate
= ahc
->enabled_targets
[our_id
];
454 return (&(*tstate
)->transinfo
[remote_id
]);
458 ahc_inw(struct ahc_softc
*ahc
, u_int port
)
460 uint16_t r
= ahc_inb(ahc
, port
+1) << 8;
461 return r
| ahc_inb(ahc
, port
);
465 ahc_outw(struct ahc_softc
*ahc
, u_int port
, u_int value
)
467 ahc_outb(ahc
, port
, value
& 0xFF);
468 ahc_outb(ahc
, port
+1, (value
>> 8) & 0xFF);
472 ahc_inl(struct ahc_softc
*ahc
, u_int port
)
474 return ((ahc_inb(ahc
, port
))
475 | (ahc_inb(ahc
, port
+1) << 8)
476 | (ahc_inb(ahc
, port
+2) << 16)
477 | (ahc_inb(ahc
, port
+3) << 24));
481 ahc_outl(struct ahc_softc
*ahc
, u_int port
, uint32_t value
)
483 ahc_outb(ahc
, port
, (value
) & 0xFF);
484 ahc_outb(ahc
, port
+1, ((value
) >> 8) & 0xFF);
485 ahc_outb(ahc
, port
+2, ((value
) >> 16) & 0xFF);
486 ahc_outb(ahc
, port
+3, ((value
) >> 24) & 0xFF);
490 ahc_inq(struct ahc_softc
*ahc
, u_int port
)
492 return ((ahc_inb(ahc
, port
))
493 | (ahc_inb(ahc
, port
+1) << 8)
494 | (ahc_inb(ahc
, port
+2) << 16)
495 | (ahc_inb(ahc
, port
+3) << 24)
496 | (((uint64_t)ahc_inb(ahc
, port
+4)) << 32)
497 | (((uint64_t)ahc_inb(ahc
, port
+5)) << 40)
498 | (((uint64_t)ahc_inb(ahc
, port
+6)) << 48)
499 | (((uint64_t)ahc_inb(ahc
, port
+7)) << 56));
503 ahc_outq(struct ahc_softc
*ahc
, u_int port
, uint64_t value
)
505 ahc_outb(ahc
, port
, value
& 0xFF);
506 ahc_outb(ahc
, port
+1, (value
>> 8) & 0xFF);
507 ahc_outb(ahc
, port
+2, (value
>> 16) & 0xFF);
508 ahc_outb(ahc
, port
+3, (value
>> 24) & 0xFF);
509 ahc_outb(ahc
, port
+4, (value
>> 32) & 0xFF);
510 ahc_outb(ahc
, port
+5, (value
>> 40) & 0xFF);
511 ahc_outb(ahc
, port
+6, (value
>> 48) & 0xFF);
512 ahc_outb(ahc
, port
+7, (value
>> 56) & 0xFF);
516 * Get a free scb. If there are none, see if we can allocate a new SCB.
519 ahc_get_scb(struct ahc_softc
*ahc
)
523 if ((scb
= SLIST_FIRST(&ahc
->scb_data
->free_scbs
)) == NULL
) {
525 scb
= SLIST_FIRST(&ahc
->scb_data
->free_scbs
);
529 SLIST_REMOVE_HEAD(&ahc
->scb_data
->free_scbs
, links
.sle
);
534 * Return an SCB resource to the free list.
537 ahc_free_scb(struct ahc_softc
*ahc
, struct scb
*scb
)
539 struct hardware_scb
*hscb
;
542 /* Clean up for the next user */
543 ahc
->scb_data
->scbindex
[hscb
->tag
] = NULL
;
544 scb
->flags
= SCB_FREE
;
547 SLIST_INSERT_HEAD(&ahc
->scb_data
->free_scbs
, scb
, links
.sle
);
549 /* Notify the OSM that a resource is now available. */
550 ahc_platform_scb_free(ahc
, scb
);
554 ahc_lookup_scb(struct ahc_softc
*ahc
, u_int tag
)
558 scb
= ahc
->scb_data
->scbindex
[tag
];
560 ahc_sync_scb(ahc
, scb
,
561 BUS_DMASYNC_POSTREAD
|BUS_DMASYNC_POSTWRITE
);
566 ahc_swap_with_next_hscb(struct ahc_softc
*ahc
, struct scb
*scb
)
568 struct hardware_scb
*q_hscb
;
572 * Our queuing method is a bit tricky. The card
573 * knows in advance which HSCB to download, and we
574 * can't disappoint it. To achieve this, the next
575 * SCB to download is saved off in ahc->next_queued_scb.
576 * When we are called to queue "an arbitrary scb",
577 * we copy the contents of the incoming HSCB to the one
578 * the sequencer knows about, swap HSCB pointers and
579 * finally assign the SCB to the tag indexed location
580 * in the scb_array. This makes sure that we can still
581 * locate the correct SCB by SCB_TAG.
583 q_hscb
= ahc
->next_queued_scb
->hscb
;
584 saved_tag
= q_hscb
->tag
;
585 memcpy(q_hscb
, scb
->hscb
, sizeof(*scb
->hscb
));
586 if ((scb
->flags
& SCB_CDB32_PTR
) != 0) {
587 q_hscb
->shared_data
.cdb_ptr
=
588 ahc_htole32(ahc_hscb_busaddr(ahc
, q_hscb
->tag
)
589 + offsetof(struct hardware_scb
, cdb32
));
591 q_hscb
->tag
= saved_tag
;
592 q_hscb
->next
= scb
->hscb
->tag
;
594 /* Now swap HSCB pointers. */
595 ahc
->next_queued_scb
->hscb
= scb
->hscb
;
598 /* Now define the mapping from tag to SCB in the scbindex */
599 ahc
->scb_data
->scbindex
[scb
->hscb
->tag
] = scb
;
603 * Tell the sequencer about a new transaction to execute.
606 ahc_queue_scb(struct ahc_softc
*ahc
, struct scb
*scb
)
608 ahc_swap_with_next_hscb(ahc
, scb
);
610 if (scb
->hscb
->tag
== SCB_LIST_NULL
611 || scb
->hscb
->next
== SCB_LIST_NULL
)
612 panic("Attempt to queue invalid SCB tag %x:%x\n",
613 scb
->hscb
->tag
, scb
->hscb
->next
);
616 * Setup data "oddness".
618 scb
->hscb
->lun
&= LID
;
619 if (ahc_get_transfer_length(scb
) & 0x1)
620 scb
->hscb
->lun
|= SCB_XFERLEN_ODD
;
623 * Keep a history of SCBs we've downloaded in the qinfifo.
625 ahc
->qinfifo
[ahc
->qinfifonext
++] = scb
->hscb
->tag
;
628 * Make sure our data is consistent from the
629 * perspective of the adapter.
631 ahc_sync_scb(ahc
, scb
, BUS_DMASYNC_PREREAD
|BUS_DMASYNC_PREWRITE
);
633 /* Tell the adapter about the newly queued SCB */
634 if ((ahc
->features
& AHC_QUEUE_REGS
) != 0) {
635 ahc_outb(ahc
, HNSCB_QOFF
, ahc
->qinfifonext
);
637 if ((ahc
->features
& AHC_AUTOPAUSE
) == 0)
639 ahc_outb(ahc
, KERNEL_QINPOS
, ahc
->qinfifonext
);
640 if ((ahc
->features
& AHC_AUTOPAUSE
) == 0)
645 struct scsi_sense_data
*
646 ahc_get_sense_buf(struct ahc_softc
*ahc
, struct scb
*scb
)
650 offset
= scb
- ahc
->scb_data
->scbarray
;
651 return (&ahc
->scb_data
->sense
[offset
]);
655 ahc_get_sense_bufaddr(struct ahc_softc
*ahc
, struct scb
*scb
)
659 offset
= scb
- ahc
->scb_data
->scbarray
;
660 return (ahc
->scb_data
->sense_busaddr
661 + (offset
* sizeof(struct scsi_sense_data
)));
664 /************************** Interrupt Processing ******************************/
666 ahc_sync_qoutfifo(struct ahc_softc
*ahc
, int op
)
668 ahc_dmamap_sync(ahc
, ahc
->shared_data_dmat
, ahc
->shared_data_dmamap
,
669 /*offset*/0, /*len*/256, op
);
673 ahc_sync_tqinfifo(struct ahc_softc
*ahc
, int op
)
675 #ifdef AHC_TARGET_MODE
676 if ((ahc
->flags
& AHC_TARGETROLE
) != 0) {
677 ahc_dmamap_sync(ahc
, ahc
->shared_data_dmat
,
678 ahc
->shared_data_dmamap
,
679 ahc_targetcmd_offset(ahc
, 0),
680 sizeof(struct target_cmd
) * AHC_TMODE_CMDS
,
687 * See if the firmware has posted any completed commands
688 * into our in-core command complete fifos.
690 #define AHC_RUN_QOUTFIFO 0x1
691 #define AHC_RUN_TQINFIFO 0x2
693 ahc_check_cmdcmpltqueues(struct ahc_softc
*ahc
)
698 ahc_dmamap_sync(ahc
, ahc
->shared_data_dmat
, ahc
->shared_data_dmamap
,
699 /*offset*/ahc
->qoutfifonext
, /*len*/1,
700 BUS_DMASYNC_POSTREAD
);
701 if (ahc
->qoutfifo
[ahc
->qoutfifonext
] != SCB_LIST_NULL
)
702 retval
|= AHC_RUN_QOUTFIFO
;
703 #ifdef AHC_TARGET_MODE
704 if ((ahc
->flags
& AHC_TARGETROLE
) != 0
705 && (ahc
->flags
& AHC_TQINFIFO_BLOCKED
) == 0) {
706 ahc_dmamap_sync(ahc
, ahc
->shared_data_dmat
,
707 ahc
->shared_data_dmamap
,
708 ahc_targetcmd_offset(ahc
, ahc
->tqinfifofnext
),
709 /*len*/sizeof(struct target_cmd
),
710 BUS_DMASYNC_POSTREAD
);
711 if (ahc
->targetcmds
[ahc
->tqinfifonext
].cmd_valid
!= 0)
712 retval
|= AHC_RUN_TQINFIFO
;
719 * Catch an interrupt from the adapter
722 ahc_intr(struct ahc_softc
*ahc
)
726 if ((ahc
->pause
& INTEN
) == 0) {
728 * Our interrupt is not enabled on the chip
729 * and may be disabled for re-entrancy reasons,
730 * so just return. This is likely just a shared
736 * Instead of directly reading the interrupt status register,
737 * infer the cause of the interrupt by checking our in-core
738 * completion queues. This avoids a costly PCI bus read in
741 if ((ahc
->flags
& (AHC_ALL_INTERRUPTS
|AHC_EDGE_INTERRUPT
)) == 0
742 && (ahc_check_cmdcmpltqueues(ahc
) != 0))
745 intstat
= ahc_inb(ahc
, INTSTAT
);
748 if ((intstat
& INT_PEND
) == 0) {
749 #if AHC_PCI_CONFIG > 0
750 if (ahc
->unsolicited_ints
> 500) {
751 ahc
->unsolicited_ints
= 0;
752 if ((ahc
->chip
& AHC_PCI
) != 0
753 && (ahc_inb(ahc
, ERROR
) & PCIERRSTAT
) != 0)
757 ahc
->unsolicited_ints
++;
760 ahc
->unsolicited_ints
= 0;
762 if (intstat
& CMDCMPLT
) {
763 ahc_outb(ahc
, CLRINT
, CLRCMDINT
);
766 * Ensure that the chip sees that we've cleared
767 * this interrupt before we walk the output fifo.
768 * Otherwise, we may, due to posted bus writes,
769 * clear the interrupt after we finish the scan,
770 * and after the sequencer has added new entries
771 * and asserted the interrupt again.
773 ahc_flush_device_writes(ahc
);
774 ahc_run_qoutfifo(ahc
);
775 #ifdef AHC_TARGET_MODE
776 if ((ahc
->flags
& AHC_TARGETROLE
) != 0)
777 ahc_run_tqinfifo(ahc
, /*paused*/FALSE
);
782 * Handle statuses that may invalidate our cached
783 * copy of INTSTAT separately.
785 if (intstat
== 0xFF && (ahc
->features
& AHC_REMOVABLE
) != 0) {
786 /* Hot eject. Do nothing */
787 } else if (intstat
& BRKADRINT
) {
788 ahc_handle_brkadrint(ahc
);
789 } else if ((intstat
& (SEQINT
|SCSIINT
)) != 0) {
791 ahc_pause_bug_fix(ahc
);
793 if ((intstat
& SEQINT
) != 0)
794 ahc_handle_seqint(ahc
, intstat
);
796 if ((intstat
& SCSIINT
) != 0)
797 ahc_handle_scsiint(ahc
, intstat
);
802 /************************* Sequencer Execution Control ************************/
804 * Restart the sequencer program from address zero
807 ahc_restart(struct ahc_softc
*ahc
)
813 /* No more pending messages. */
814 ahc_clear_msg_state(ahc
);
816 ahc_outb(ahc
, SCSISIGO
, 0); /* De-assert BSY */
817 ahc_outb(ahc
, MSG_OUT
, MSG_NOOP
); /* No message to send */
818 ahc_outb(ahc
, SXFRCTL1
, ahc_inb(ahc
, SXFRCTL1
) & ~BITBUCKET
);
819 ahc_outb(ahc
, LASTPHASE
, P_BUSFREE
);
820 ahc_outb(ahc
, SAVED_SCSIID
, 0xFF);
821 ahc_outb(ahc
, SAVED_LUN
, 0xFF);
824 * Ensure that the sequencer's idea of TQINPOS
825 * matches our own. The sequencer increments TQINPOS
826 * only after it sees a DMA complete and a reset could
827 * occur before the increment leaving the kernel to believe
828 * the command arrived but the sequencer to not.
830 ahc_outb(ahc
, TQINPOS
, ahc
->tqinfifonext
);
832 /* Always allow reselection */
833 ahc_outb(ahc
, SCSISEQ
,
834 ahc_inb(ahc
, SCSISEQ_TEMPLATE
) & (ENSELI
|ENRSELI
|ENAUTOATNP
));
835 if ((ahc
->features
& AHC_CMD_CHAN
) != 0) {
836 /* Ensure that no DMA operations are in progress */
837 ahc_outb(ahc
, CCSCBCNT
, 0);
838 ahc_outb(ahc
, CCSGCTL
, 0);
839 ahc_outb(ahc
, CCSCBCTL
, 0);
842 * If we were in the process of DMA'ing SCB data into
843 * an SCB, replace that SCB on the free list. This prevents
846 if ((ahc_inb(ahc
, SEQ_FLAGS2
) & SCB_DMA
) != 0) {
847 ahc_add_curscb_to_free_list(ahc
);
848 ahc_outb(ahc
, SEQ_FLAGS2
,
849 ahc_inb(ahc
, SEQ_FLAGS2
) & ~SCB_DMA
);
853 * Clear any pending sequencer interrupt. It is no
854 * longer relevant since we're resetting the Program
857 ahc_outb(ahc
, CLRINT
, CLRSEQINT
);
859 ahc_outb(ahc
, MWI_RESIDUAL
, 0);
860 ahc_outb(ahc
, SEQCTL
, ahc
->seqctl
);
861 ahc_outb(ahc
, SEQADDR0
, 0);
862 ahc_outb(ahc
, SEQADDR1
, 0);
865 * Take the LED out of diagnostic mode on PM resume, too
867 sblkctl
= ahc_inb(ahc
, SBLKCTL
);
868 ahc_outb(ahc
, SBLKCTL
, (sblkctl
& ~(DIAGLEDEN
|DIAGLEDON
)));
873 /************************* Input/Output Queues ********************************/
875 ahc_run_qoutfifo(struct ahc_softc
*ahc
)
880 ahc_sync_qoutfifo(ahc
, BUS_DMASYNC_POSTREAD
);
881 while (ahc
->qoutfifo
[ahc
->qoutfifonext
] != SCB_LIST_NULL
) {
883 scb_index
= ahc
->qoutfifo
[ahc
->qoutfifonext
];
884 if ((ahc
->qoutfifonext
& 0x03) == 0x03) {
888 * Clear 32bits of QOUTFIFO at a time
889 * so that we don't clobber an incoming
890 * byte DMA to the array on architectures
891 * that only support 32bit load and store
894 modnext
= ahc
->qoutfifonext
& ~0x3;
895 *((uint32_t *)(&ahc
->qoutfifo
[modnext
])) = 0xFFFFFFFFUL
;
896 ahc_dmamap_sync(ahc
, ahc
->shared_data_dmat
,
897 ahc
->shared_data_dmamap
,
898 /*offset*/modnext
, /*len*/4,
899 BUS_DMASYNC_PREREAD
);
903 scb
= ahc_lookup_scb(ahc
, scb_index
);
905 printk("%s: WARNING no command for scb %d "
906 "(cmdcmplt)\nQOUTPOS = %d\n",
907 ahc_name(ahc
), scb_index
,
908 (ahc
->qoutfifonext
- 1) & 0xFF);
913 * Save off the residual
916 ahc_update_residual(ahc
, scb
);
922 ahc_run_untagged_queues(struct ahc_softc
*ahc
)
926 for (i
= 0; i
< 16; i
++)
927 ahc_run_untagged_queue(ahc
, &ahc
->untagged_queues
[i
]);
931 ahc_run_untagged_queue(struct ahc_softc
*ahc
, struct scb_tailq
*queue
)
935 if (ahc
->untagged_queue_lock
!= 0)
938 if ((scb
= TAILQ_FIRST(queue
)) != NULL
939 && (scb
->flags
& SCB_ACTIVE
) == 0) {
940 scb
->flags
|= SCB_ACTIVE
;
941 ahc_queue_scb(ahc
, scb
);
945 /************************* Interrupt Handling *********************************/
947 ahc_handle_brkadrint(struct ahc_softc
*ahc
)
950 * We upset the sequencer :-(
951 * Lookup the error message
956 error
= ahc_inb(ahc
, ERROR
);
957 for (i
= 0; error
!= 1 && i
< num_errors
; i
++)
959 printk("%s: brkadrint, %s at seqaddr = 0x%x\n",
960 ahc_name(ahc
), ahc_hard_errors
[i
].errmesg
,
961 ahc_inb(ahc
, SEQADDR0
) |
962 (ahc_inb(ahc
, SEQADDR1
) << 8));
964 ahc_dump_card_state(ahc
);
966 /* Tell everyone that this HBA is no longer available */
967 ahc_abort_scbs(ahc
, CAM_TARGET_WILDCARD
, ALL_CHANNELS
,
968 CAM_LUN_WILDCARD
, SCB_LIST_NULL
, ROLE_UNKNOWN
,
971 /* Disable all interrupt sources by resetting the controller */
976 ahc_handle_seqint(struct ahc_softc
*ahc
, u_int intstat
)
979 struct ahc_devinfo devinfo
;
981 ahc_fetch_devinfo(ahc
, &devinfo
);
984 * Clear the upper byte that holds SEQINT status
985 * codes and clear the SEQINT bit. We will unpause
986 * the sequencer, if appropriate, after servicing
989 ahc_outb(ahc
, CLRINT
, CLRSEQINT
);
990 switch (intstat
& SEQINT_MASK
) {
994 struct hardware_scb
*hscb
;
997 * Set the default return value to 0 (don't
998 * send sense). The sense code will change
1001 ahc_outb(ahc
, RETURN_1
, 0);
1004 * The sequencer will notify us when a command
1005 * has an error that would be of interest to
1006 * the kernel. This allows us to leave the sequencer
1007 * running in the common case of command completes
1008 * without error. The sequencer will already have
1009 * dma'd the SCB back up to us, so we can reference
1010 * the in kernel copy directly.
1012 scb_index
= ahc_inb(ahc
, SCB_TAG
);
1013 scb
= ahc_lookup_scb(ahc
, scb_index
);
1015 ahc_print_devinfo(ahc
, &devinfo
);
1016 printk("ahc_intr - referenced scb "
1017 "not valid during seqint 0x%x scb(%d)\n",
1018 intstat
, scb_index
);
1019 ahc_dump_card_state(ahc
);
1020 panic("for safety");
1026 /* Don't want to clobber the original sense code */
1027 if ((scb
->flags
& SCB_SENSE
) != 0) {
1029 * Clear the SCB_SENSE Flag and have
1030 * the sequencer do a normal command
1033 scb
->flags
&= ~SCB_SENSE
;
1034 ahc_set_transaction_status(scb
, CAM_AUTOSENSE_FAIL
);
1037 ahc_set_transaction_status(scb
, CAM_SCSI_STATUS_ERROR
);
1038 /* Freeze the queue until the client sees the error. */
1039 ahc_freeze_devq(ahc
, scb
);
1040 ahc_freeze_scb(scb
);
1041 ahc_set_scsi_status(scb
, hscb
->shared_data
.status
.scsi_status
);
1042 switch (hscb
->shared_data
.status
.scsi_status
) {
1043 case SCSI_STATUS_OK
:
1044 printk("%s: Interrupted for staus of 0???\n",
1047 case SCSI_STATUS_CMD_TERMINATED
:
1048 case SCSI_STATUS_CHECK_COND
:
1050 struct ahc_dma_seg
*sg
;
1051 struct scsi_sense
*sc
;
1052 struct ahc_initiator_tinfo
*targ_info
;
1053 struct ahc_tmode_tstate
*tstate
;
1054 struct ahc_transinfo
*tinfo
;
1056 if (ahc_debug
& AHC_SHOW_SENSE
) {
1057 ahc_print_path(ahc
, scb
);
1058 printk("SCB %d: requests Check Status\n",
1063 if (ahc_perform_autosense(scb
) == 0)
1066 targ_info
= ahc_fetch_transinfo(ahc
,
1071 tinfo
= &targ_info
->curr
;
1073 sc
= (struct scsi_sense
*)(&hscb
->shared_data
.cdb
);
1075 * Save off the residual if there is one.
1077 ahc_update_residual(ahc
, scb
);
1079 if (ahc_debug
& AHC_SHOW_SENSE
) {
1080 ahc_print_path(ahc
, scb
);
1081 printk("Sending Sense\n");
1084 sg
->addr
= ahc_get_sense_bufaddr(ahc
, scb
);
1085 sg
->len
= ahc_get_sense_bufsize(ahc
, scb
);
1086 sg
->len
|= AHC_DMA_LAST_SEG
;
1088 /* Fixup byte order */
1089 sg
->addr
= ahc_htole32(sg
->addr
);
1090 sg
->len
= ahc_htole32(sg
->len
);
1092 sc
->opcode
= REQUEST_SENSE
;
1094 if (tinfo
->protocol_version
<= SCSI_REV_2
1095 && SCB_GET_LUN(scb
) < 8)
1096 sc
->byte2
= SCB_GET_LUN(scb
) << 5;
1099 sc
->length
= sg
->len
;
1103 * We can't allow the target to disconnect.
1104 * This will be an untagged transaction and
1105 * having the target disconnect will make this
1106 * transaction indestinguishable from outstanding
1107 * tagged transactions.
1112 * This request sense could be because the
1113 * the device lost power or in some other
1114 * way has lost our transfer negotiations.
1115 * Renegotiate if appropriate. Unit attention
1116 * errors will be reported before any data
1119 if (ahc_get_residual(scb
)
1120 == ahc_get_transfer_length(scb
)) {
1121 ahc_update_neg_request(ahc
, &devinfo
,
1123 AHC_NEG_IF_NON_ASYNC
);
1125 if (tstate
->auto_negotiate
& devinfo
.target_mask
) {
1126 hscb
->control
|= MK_MESSAGE
;
1127 scb
->flags
&= ~SCB_NEGOTIATE
;
1128 scb
->flags
|= SCB_AUTO_NEGOTIATE
;
1130 hscb
->cdb_len
= sizeof(*sc
);
1131 hscb
->dataptr
= sg
->addr
;
1132 hscb
->datacnt
= sg
->len
;
1133 hscb
->sgptr
= scb
->sg_list_phys
| SG_FULL_RESID
;
1134 hscb
->sgptr
= ahc_htole32(hscb
->sgptr
);
1136 scb
->flags
|= SCB_SENSE
;
1137 ahc_qinfifo_requeue_tail(ahc
, scb
);
1138 ahc_outb(ahc
, RETURN_1
, SEND_SENSE
);
1140 * Ensure we have enough time to actually
1141 * retrieve the sense.
1143 ahc_scb_timer_reset(scb
, 5 * 1000000);
1153 /* Ensure we don't leave the selection hardware on */
1154 ahc_outb(ahc
, SCSISEQ
,
1155 ahc_inb(ahc
, SCSISEQ
) & (ENSELI
|ENRSELI
|ENAUTOATNP
));
1157 printk("%s:%c:%d: no active SCB for reconnecting "
1158 "target - issuing BUS DEVICE RESET\n",
1159 ahc_name(ahc
), devinfo
.channel
, devinfo
.target
);
1160 printk("SAVED_SCSIID == 0x%x, SAVED_LUN == 0x%x, "
1161 "ARG_1 == 0x%x ACCUM = 0x%x\n",
1162 ahc_inb(ahc
, SAVED_SCSIID
), ahc_inb(ahc
, SAVED_LUN
),
1163 ahc_inb(ahc
, ARG_1
), ahc_inb(ahc
, ACCUM
));
1164 printk("SEQ_FLAGS == 0x%x, SCBPTR == 0x%x, BTT == 0x%x, "
1166 ahc_inb(ahc
, SEQ_FLAGS
), ahc_inb(ahc
, SCBPTR
),
1167 ahc_index_busy_tcl(ahc
,
1168 BUILD_TCL(ahc_inb(ahc
, SAVED_SCSIID
),
1169 ahc_inb(ahc
, SAVED_LUN
))),
1170 ahc_inb(ahc
, SINDEX
));
1171 printk("SCSIID == 0x%x, SCB_SCSIID == 0x%x, SCB_LUN == 0x%x, "
1172 "SCB_TAG == 0x%x, SCB_CONTROL == 0x%x\n",
1173 ahc_inb(ahc
, SCSIID
), ahc_inb(ahc
, SCB_SCSIID
),
1174 ahc_inb(ahc
, SCB_LUN
), ahc_inb(ahc
, SCB_TAG
),
1175 ahc_inb(ahc
, SCB_CONTROL
));
1176 printk("SCSIBUSL == 0x%x, SCSISIGI == 0x%x\n",
1177 ahc_inb(ahc
, SCSIBUSL
), ahc_inb(ahc
, SCSISIGI
));
1178 printk("SXFRCTL0 == 0x%x\n", ahc_inb(ahc
, SXFRCTL0
));
1179 printk("SEQCTL == 0x%x\n", ahc_inb(ahc
, SEQCTL
));
1180 ahc_dump_card_state(ahc
);
1181 ahc
->msgout_buf
[0] = MSG_BUS_DEV_RESET
;
1182 ahc
->msgout_len
= 1;
1183 ahc
->msgout_index
= 0;
1184 ahc
->msg_type
= MSG_TYPE_INITIATOR_MSGOUT
;
1185 ahc_outb(ahc
, MSG_OUT
, HOST_MSG
);
1186 ahc_assert_atn(ahc
);
1191 u_int rejbyte
= ahc_inb(ahc
, ACCUM
);
1192 printk("%s:%c:%d: Warning - unknown message received from "
1193 "target (0x%x). Rejecting\n",
1194 ahc_name(ahc
), devinfo
.channel
, devinfo
.target
, rejbyte
);
1197 case PROTO_VIOLATION
:
1199 ahc_handle_proto_violation(ahc
);
1203 ahc_handle_ign_wide_residue(ahc
, &devinfo
);
1206 ahc_reinitialize_dataptrs(ahc
);
1212 lastphase
= ahc_inb(ahc
, LASTPHASE
);
1213 printk("%s:%c:%d: unknown scsi bus phase %x, "
1214 "lastphase = 0x%x. Attempting to continue\n",
1215 ahc_name(ahc
), devinfo
.channel
, devinfo
.target
,
1216 lastphase
, ahc_inb(ahc
, SCSISIGI
));
1219 case MISSED_BUSFREE
:
1223 lastphase
= ahc_inb(ahc
, LASTPHASE
);
1224 printk("%s:%c:%d: Missed busfree. "
1225 "Lastphase = 0x%x, Curphase = 0x%x\n",
1226 ahc_name(ahc
), devinfo
.channel
, devinfo
.target
,
1227 lastphase
, ahc_inb(ahc
, SCSISIGI
));
1234 * The sequencer has encountered a message phase
1235 * that requires host assistance for completion.
1236 * While handling the message phase(s), we will be
1237 * notified by the sequencer after each byte is
1238 * transfered so we can track bus phase changes.
1240 * If this is the first time we've seen a HOST_MSG_LOOP
1241 * interrupt, initialize the state of the host message
1244 if (ahc
->msg_type
== MSG_TYPE_NONE
) {
1249 bus_phase
= ahc_inb(ahc
, SCSISIGI
) & PHASE_MASK
;
1250 if (bus_phase
!= P_MESGIN
1251 && bus_phase
!= P_MESGOUT
) {
1252 printk("ahc_intr: HOST_MSG_LOOP bad "
1256 * Probably transitioned to bus free before
1257 * we got here. Just punt the message.
1259 ahc_clear_intstat(ahc
);
1264 scb_index
= ahc_inb(ahc
, SCB_TAG
);
1265 scb
= ahc_lookup_scb(ahc
, scb_index
);
1266 if (devinfo
.role
== ROLE_INITIATOR
) {
1267 if (bus_phase
== P_MESGOUT
) {
1269 panic("HOST_MSG_LOOP with "
1273 ahc_setup_initiator_msgout(ahc
,
1278 MSG_TYPE_INITIATOR_MSGIN
;
1279 ahc
->msgin_index
= 0;
1282 #ifdef AHC_TARGET_MODE
1284 if (bus_phase
== P_MESGOUT
) {
1286 MSG_TYPE_TARGET_MSGOUT
;
1287 ahc
->msgin_index
= 0;
1290 ahc_setup_target_msgin(ahc
,
1297 ahc_handle_message_phase(ahc
);
1303 * If we've cleared the parity error interrupt
1304 * but the sequencer still believes that SCSIPERR
1305 * is true, it must be that the parity error is
1306 * for the currently presented byte on the bus,
1307 * and we are not in a phase (data-in) where we will
1308 * eventually ack this byte. Ack the byte and
1309 * throw it away in the hope that the target will
1310 * take us to message out to deliver the appropriate
1313 if ((intstat
& SCSIINT
) == 0
1314 && (ahc_inb(ahc
, SSTAT1
) & SCSIPERR
) != 0) {
1316 if ((ahc
->features
& AHC_DT
) == 0) {
1320 * The hardware will only let you ack bytes
1321 * if the expected phase in SCSISIGO matches
1322 * the current phase. Make sure this is
1323 * currently the case.
1325 curphase
= ahc_inb(ahc
, SCSISIGI
) & PHASE_MASK
;
1326 ahc_outb(ahc
, LASTPHASE
, curphase
);
1327 ahc_outb(ahc
, SCSISIGO
, curphase
);
1329 if ((ahc_inb(ahc
, SCSISIGI
) & (CDI
|MSGI
)) == 0) {
1333 * In a data phase. Faster to bitbucket
1334 * the data than to individually ack each
1335 * byte. This is also the only strategy
1336 * that will work with AUTOACK enabled.
1338 ahc_outb(ahc
, SXFRCTL1
,
1339 ahc_inb(ahc
, SXFRCTL1
) | BITBUCKET
);
1341 while (--wait
!= 0) {
1342 if ((ahc_inb(ahc
, SCSISIGI
)
1347 ahc_outb(ahc
, SXFRCTL1
,
1348 ahc_inb(ahc
, SXFRCTL1
) & ~BITBUCKET
);
1353 ahc_print_devinfo(ahc
, &devinfo
);
1354 printk("Unable to clear parity error. "
1355 "Resetting bus.\n");
1356 scb_index
= ahc_inb(ahc
, SCB_TAG
);
1357 scb
= ahc_lookup_scb(ahc
, scb_index
);
1359 ahc_set_transaction_status(scb
,
1361 ahc_reset_channel(ahc
, devinfo
.channel
,
1362 /*init reset*/TRUE
);
1365 ahc_inb(ahc
, SCSIDATL
);
1373 * When the sequencer detects an overrun, it
1374 * places the controller in "BITBUCKET" mode
1375 * and allows the target to complete its transfer.
1376 * Unfortunately, none of the counters get updated
1377 * when the controller is in this mode, so we have
1378 * no way of knowing how large the overrun was.
1380 u_int scbindex
= ahc_inb(ahc
, SCB_TAG
);
1381 u_int lastphase
= ahc_inb(ahc
, LASTPHASE
);
1384 scb
= ahc_lookup_scb(ahc
, scbindex
);
1385 for (i
= 0; i
< num_phases
; i
++) {
1386 if (lastphase
== ahc_phase_table
[i
].phase
)
1389 ahc_print_path(ahc
, scb
);
1390 printk("data overrun detected %s."
1392 ahc_phase_table
[i
].phasemsg
,
1394 ahc_print_path(ahc
, scb
);
1395 printk("%s seen Data Phase. Length = %ld. NumSGs = %d.\n",
1396 ahc_inb(ahc
, SEQ_FLAGS
) & DPHASE
? "Have" : "Haven't",
1397 ahc_get_transfer_length(scb
), scb
->sg_count
);
1398 if (scb
->sg_count
> 0) {
1399 for (i
= 0; i
< scb
->sg_count
; i
++) {
1401 printk("sg[%d] - Addr 0x%x%x : Length %d\n",
1403 (ahc_le32toh(scb
->sg_list
[i
].len
) >> 24
1404 & SG_HIGH_ADDR_BITS
),
1405 ahc_le32toh(scb
->sg_list
[i
].addr
),
1406 ahc_le32toh(scb
->sg_list
[i
].len
)
1411 * Set this and it will take effect when the
1412 * target does a command complete.
1414 ahc_freeze_devq(ahc
, scb
);
1415 if ((scb
->flags
& SCB_SENSE
) == 0) {
1416 ahc_set_transaction_status(scb
, CAM_DATA_RUN_ERR
);
1418 scb
->flags
&= ~SCB_SENSE
;
1419 ahc_set_transaction_status(scb
, CAM_AUTOSENSE_FAIL
);
1421 ahc_freeze_scb(scb
);
1423 if ((ahc
->features
& AHC_ULTRA2
) != 0) {
1425 * Clear the channel in case we return
1426 * to data phase later.
1428 ahc_outb(ahc
, SXFRCTL0
,
1429 ahc_inb(ahc
, SXFRCTL0
) | CLRSTCNT
|CLRCHN
);
1430 ahc_outb(ahc
, SXFRCTL0
,
1431 ahc_inb(ahc
, SXFRCTL0
) | CLRSTCNT
|CLRCHN
);
1433 if ((ahc
->flags
& AHC_39BIT_ADDRESSING
) != 0) {
1436 /* Ensure HHADDR is 0 for future DMA operations. */
1437 dscommand1
= ahc_inb(ahc
, DSCOMMAND1
);
1438 ahc_outb(ahc
, DSCOMMAND1
, dscommand1
| HADDLDSEL0
);
1439 ahc_outb(ahc
, HADDR
, 0);
1440 ahc_outb(ahc
, DSCOMMAND1
, dscommand1
);
1448 printk("%s:%c:%d:%d: Attempt to issue message failed\n",
1449 ahc_name(ahc
), devinfo
.channel
, devinfo
.target
,
1451 scbindex
= ahc_inb(ahc
, SCB_TAG
);
1452 scb
= ahc_lookup_scb(ahc
, scbindex
);
1454 && (scb
->flags
& SCB_RECOVERY_SCB
) != 0)
1456 * Ensure that we didn't put a second instance of this
1457 * SCB into the QINFIFO.
1459 ahc_search_qinfifo(ahc
, SCB_GET_TARGET(ahc
, scb
),
1460 SCB_GET_CHANNEL(ahc
, scb
),
1461 SCB_GET_LUN(scb
), scb
->hscb
->tag
,
1462 ROLE_INITIATOR
, /*status*/0,
1468 printk("%s: No free or disconnected SCBs\n", ahc_name(ahc
));
1469 ahc_dump_card_state(ahc
);
1470 panic("for safety");
1477 scbptr
= ahc_inb(ahc
, SCBPTR
);
1478 printk("Bogus TAG after DMA. SCBPTR %d, tag %d, our tag %d\n",
1479 scbptr
, ahc_inb(ahc
, ARG_1
),
1480 ahc
->scb_data
->hscbs
[scbptr
].tag
);
1481 ahc_dump_card_state(ahc
);
1482 panic("for saftey");
1487 printk("%s: BTT calculation out of range\n", ahc_name(ahc
));
1488 printk("SAVED_SCSIID == 0x%x, SAVED_LUN == 0x%x, "
1489 "ARG_1 == 0x%x ACCUM = 0x%x\n",
1490 ahc_inb(ahc
, SAVED_SCSIID
), ahc_inb(ahc
, SAVED_LUN
),
1491 ahc_inb(ahc
, ARG_1
), ahc_inb(ahc
, ACCUM
));
1492 printk("SEQ_FLAGS == 0x%x, SCBPTR == 0x%x, BTT == 0x%x, "
1493 "SINDEX == 0x%x\n, A == 0x%x\n",
1494 ahc_inb(ahc
, SEQ_FLAGS
), ahc_inb(ahc
, SCBPTR
),
1495 ahc_index_busy_tcl(ahc
,
1496 BUILD_TCL(ahc_inb(ahc
, SAVED_SCSIID
),
1497 ahc_inb(ahc
, SAVED_LUN
))),
1498 ahc_inb(ahc
, SINDEX
),
1499 ahc_inb(ahc
, ACCUM
));
1500 printk("SCSIID == 0x%x, SCB_SCSIID == 0x%x, SCB_LUN == 0x%x, "
1501 "SCB_TAG == 0x%x, SCB_CONTROL == 0x%x\n",
1502 ahc_inb(ahc
, SCSIID
), ahc_inb(ahc
, SCB_SCSIID
),
1503 ahc_inb(ahc
, SCB_LUN
), ahc_inb(ahc
, SCB_TAG
),
1504 ahc_inb(ahc
, SCB_CONTROL
));
1505 printk("SCSIBUSL == 0x%x, SCSISIGI == 0x%x\n",
1506 ahc_inb(ahc
, SCSIBUSL
), ahc_inb(ahc
, SCSISIGI
));
1507 ahc_dump_card_state(ahc
);
1508 panic("for safety");
1512 printk("ahc_intr: seqint, "
1513 "intstat == 0x%x, scsisigi = 0x%x\n",
1514 intstat
, ahc_inb(ahc
, SCSISIGI
));
1519 * The sequencer is paused immediately on
1520 * a SEQINT, so we should restart it when
1527 ahc_handle_scsiint(struct ahc_softc
*ahc
, u_int intstat
)
1536 if ((ahc
->features
& AHC_TWIN
) != 0
1537 && ((ahc_inb(ahc
, SBLKCTL
) & SELBUSB
) != 0))
1541 intr_channel
= cur_channel
;
1543 if ((ahc
->features
& AHC_ULTRA2
) != 0)
1544 status0
= ahc_inb(ahc
, SSTAT0
) & IOERR
;
1547 status
= ahc_inb(ahc
, SSTAT1
) & (SELTO
|SCSIRSTI
|BUSFREE
|SCSIPERR
);
1548 if (status
== 0 && status0
== 0) {
1549 if ((ahc
->features
& AHC_TWIN
) != 0) {
1550 /* Try the other channel */
1551 ahc_outb(ahc
, SBLKCTL
, ahc_inb(ahc
, SBLKCTL
) ^ SELBUSB
);
1552 status
= ahc_inb(ahc
, SSTAT1
)
1553 & (SELTO
|SCSIRSTI
|BUSFREE
|SCSIPERR
);
1554 intr_channel
= (cur_channel
== 'A') ? 'B' : 'A';
1557 printk("%s: Spurious SCSI interrupt\n", ahc_name(ahc
));
1558 ahc_outb(ahc
, CLRINT
, CLRSCSIINT
);
1564 /* Make sure the sequencer is in a safe location. */
1565 ahc_clear_critical_section(ahc
);
1567 scb_index
= ahc_inb(ahc
, SCB_TAG
);
1568 scb
= ahc_lookup_scb(ahc
, scb_index
);
1570 && (ahc_inb(ahc
, SEQ_FLAGS
) & NOT_IDENTIFIED
) != 0)
1573 if ((ahc
->features
& AHC_ULTRA2
) != 0
1574 && (status0
& IOERR
) != 0) {
1577 now_lvd
= ahc_inb(ahc
, SBLKCTL
) & ENAB40
;
1578 printk("%s: Transceiver State Has Changed to %s mode\n",
1579 ahc_name(ahc
), now_lvd
? "LVD" : "SE");
1580 ahc_outb(ahc
, CLRSINT0
, CLRIOERR
);
1582 * When transitioning to SE mode, the reset line
1583 * glitches, triggering an arbitration bug in some
1584 * Ultra2 controllers. This bug is cleared when we
1585 * assert the reset line. Since a reset glitch has
1586 * already occurred with this transition and a
1587 * transceiver state change is handled just like
1588 * a bus reset anyway, asserting the reset line
1589 * ourselves is safe.
1591 ahc_reset_channel(ahc
, intr_channel
,
1592 /*Initiate Reset*/now_lvd
== 0);
1593 } else if ((status
& SCSIRSTI
) != 0) {
1594 printk("%s: Someone reset channel %c\n",
1595 ahc_name(ahc
), intr_channel
);
1596 if (intr_channel
!= cur_channel
)
1597 ahc_outb(ahc
, SBLKCTL
, ahc_inb(ahc
, SBLKCTL
) ^ SELBUSB
);
1598 ahc_reset_channel(ahc
, intr_channel
, /*Initiate Reset*/FALSE
);
1599 } else if ((status
& SCSIPERR
) != 0) {
1601 * Determine the bus phase and queue an appropriate message.
1602 * SCSIPERR is latched true as soon as a parity error
1603 * occurs. If the sequencer acked the transfer that
1604 * caused the parity error and the currently presented
1605 * transfer on the bus has correct parity, SCSIPERR will
1606 * be cleared by CLRSCSIPERR. Use this to determine if
1607 * we should look at the last phase the sequencer recorded,
1608 * or the current phase presented on the bus.
1610 struct ahc_devinfo devinfo
;
1620 lastphase
= ahc_inb(ahc
, LASTPHASE
);
1621 curphase
= ahc_inb(ahc
, SCSISIGI
) & PHASE_MASK
;
1622 sstat2
= ahc_inb(ahc
, SSTAT2
);
1623 ahc_outb(ahc
, CLRSINT1
, CLRSCSIPERR
);
1625 * For all phases save DATA, the sequencer won't
1626 * automatically ack a byte that has a parity error
1627 * in it. So the only way that the current phase
1628 * could be 'data-in' is if the parity error is for
1629 * an already acked byte in the data phase. During
1630 * synchronous data-in transfers, we may actually
1631 * ack bytes before latching the current phase in
1632 * LASTPHASE, leading to the discrepancy between
1633 * curphase and lastphase.
1635 if ((ahc_inb(ahc
, SSTAT1
) & SCSIPERR
) != 0
1636 || curphase
== P_DATAIN
|| curphase
== P_DATAIN_DT
)
1637 errorphase
= curphase
;
1639 errorphase
= lastphase
;
1641 for (i
= 0; i
< num_phases
; i
++) {
1642 if (errorphase
== ahc_phase_table
[i
].phase
)
1645 mesg_out
= ahc_phase_table
[i
].mesg_out
;
1648 if (SCB_IS_SILENT(scb
))
1651 ahc_print_path(ahc
, scb
);
1652 scb
->flags
|= SCB_TRANSMISSION_ERROR
;
1654 printk("%s:%c:%d: ", ahc_name(ahc
), intr_channel
,
1655 SCSIID_TARGET(ahc
, ahc_inb(ahc
, SAVED_SCSIID
)));
1656 scsirate
= ahc_inb(ahc
, SCSIRATE
);
1657 if (silent
== FALSE
) {
1658 printk("parity error detected %s. "
1659 "SEQADDR(0x%x) SCSIRATE(0x%x)\n",
1660 ahc_phase_table
[i
].phasemsg
,
1661 ahc_inw(ahc
, SEQADDR0
),
1663 if ((ahc
->features
& AHC_DT
) != 0) {
1664 if ((sstat2
& CRCVALERR
) != 0)
1665 printk("\tCRC Value Mismatch\n");
1666 if ((sstat2
& CRCENDERR
) != 0)
1667 printk("\tNo terminal CRC packet "
1669 if ((sstat2
& CRCREQERR
) != 0)
1670 printk("\tIllegal CRC packet "
1672 if ((sstat2
& DUAL_EDGE_ERR
) != 0)
1673 printk("\tUnexpected %sDT Data Phase\n",
1674 (scsirate
& SINGLE_EDGE
)
1679 if ((ahc
->features
& AHC_DT
) != 0
1680 && (sstat2
& DUAL_EDGE_ERR
) != 0) {
1682 * This error applies regardless of
1683 * data direction, so ignore the value
1684 * in the phase table.
1686 mesg_out
= MSG_INITIATOR_DET_ERR
;
1690 * We've set the hardware to assert ATN if we
1691 * get a parity error on "in" phases, so all we
1692 * need to do is stuff the message buffer with
1693 * the appropriate message. "In" phases have set
1694 * mesg_out to something other than MSG_NOP.
1696 if (mesg_out
!= MSG_NOOP
) {
1697 if (ahc
->msg_type
!= MSG_TYPE_NONE
)
1698 ahc
->send_msg_perror
= TRUE
;
1700 ahc_outb(ahc
, MSG_OUT
, mesg_out
);
1703 * Force a renegotiation with this target just in
1704 * case we are out of sync for some external reason
1705 * unknown (or unreported) by the target.
1707 ahc_fetch_devinfo(ahc
, &devinfo
);
1708 ahc_force_renegotiation(ahc
, &devinfo
);
1710 ahc_outb(ahc
, CLRINT
, CLRSCSIINT
);
1712 } else if ((status
& SELTO
) != 0) {
1715 /* Stop the selection */
1716 ahc_outb(ahc
, SCSISEQ
, 0);
1718 /* No more pending messages */
1719 ahc_clear_msg_state(ahc
);
1721 /* Clear interrupt state */
1722 ahc_outb(ahc
, SIMODE1
, ahc_inb(ahc
, SIMODE1
) & ~ENBUSFREE
);
1723 ahc_outb(ahc
, CLRSINT1
, CLRSELTIMEO
|CLRBUSFREE
|CLRSCSIPERR
);
1726 * Although the driver does not care about the
1727 * 'Selection in Progress' status bit, the busy
1728 * LED does. SELINGO is only cleared by a successfull
1729 * selection, so we must manually clear it to insure
1730 * the LED turns off just incase no future successful
1731 * selections occur (e.g. no devices on the bus).
1733 ahc_outb(ahc
, CLRSINT0
, CLRSELINGO
);
1735 scbptr
= ahc_inb(ahc
, WAITING_SCBH
);
1736 ahc_outb(ahc
, SCBPTR
, scbptr
);
1737 scb_index
= ahc_inb(ahc
, SCB_TAG
);
1739 scb
= ahc_lookup_scb(ahc
, scb_index
);
1741 printk("%s: ahc_intr - referenced scb not "
1742 "valid during SELTO scb(%d, %d)\n",
1743 ahc_name(ahc
), scbptr
, scb_index
);
1744 ahc_dump_card_state(ahc
);
1746 struct ahc_devinfo devinfo
;
1748 if ((ahc_debug
& AHC_SHOW_SELTO
) != 0) {
1749 ahc_print_path(ahc
, scb
);
1750 printk("Saw Selection Timeout for SCB 0x%x\n",
1754 ahc_scb_devinfo(ahc
, &devinfo
, scb
);
1755 ahc_set_transaction_status(scb
, CAM_SEL_TIMEOUT
);
1756 ahc_freeze_devq(ahc
, scb
);
1759 * Cancel any pending transactions on the device
1760 * now that it seems to be missing. This will
1761 * also revert us to async/narrow transfers until
1762 * we can renegotiate with the device.
1764 ahc_handle_devreset(ahc
, &devinfo
,
1766 "Selection Timeout",
1767 /*verbose_level*/1);
1769 ahc_outb(ahc
, CLRINT
, CLRSCSIINT
);
1771 } else if ((status
& BUSFREE
) != 0
1772 && (ahc_inb(ahc
, SIMODE1
) & ENBUSFREE
) != 0) {
1773 struct ahc_devinfo devinfo
;
1778 u_int initiator_role_id
;
1783 * Clear our selection hardware as soon as possible.
1784 * We may have an entry in the waiting Q for this target,
1785 * that is affected by this busfree and we don't want to
1786 * go about selecting the target while we handle the event.
1788 ahc_outb(ahc
, SCSISEQ
,
1789 ahc_inb(ahc
, SCSISEQ
) & (ENSELI
|ENRSELI
|ENAUTOATNP
));
1792 * Disable busfree interrupts and clear the busfree
1793 * interrupt status. We do this here so that several
1794 * bus transactions occur prior to clearing the SCSIINT
1795 * latch. It can take a bit for the clearing to take effect.
1797 ahc_outb(ahc
, SIMODE1
, ahc_inb(ahc
, SIMODE1
) & ~ENBUSFREE
);
1798 ahc_outb(ahc
, CLRSINT1
, CLRBUSFREE
|CLRSCSIPERR
);
1801 * Look at what phase we were last in.
1802 * If its message out, chances are pretty good
1803 * that the busfree was in response to one of
1804 * our abort requests.
1806 lastphase
= ahc_inb(ahc
, LASTPHASE
);
1807 saved_scsiid
= ahc_inb(ahc
, SAVED_SCSIID
);
1808 saved_lun
= ahc_inb(ahc
, SAVED_LUN
);
1809 target
= SCSIID_TARGET(ahc
, saved_scsiid
);
1810 initiator_role_id
= SCSIID_OUR_ID(saved_scsiid
);
1811 channel
= SCSIID_CHANNEL(ahc
, saved_scsiid
);
1812 ahc_compile_devinfo(&devinfo
, initiator_role_id
,
1813 target
, saved_lun
, channel
, ROLE_INITIATOR
);
1816 if (lastphase
== P_MESGOUT
) {
1819 tag
= SCB_LIST_NULL
;
1820 if (ahc_sent_msg(ahc
, AHCMSG_1B
, MSG_ABORT_TAG
, TRUE
)
1821 || ahc_sent_msg(ahc
, AHCMSG_1B
, MSG_ABORT
, TRUE
)) {
1822 if (ahc
->msgout_buf
[ahc
->msgout_index
- 1]
1824 tag
= scb
->hscb
->tag
;
1825 ahc_print_path(ahc
, scb
);
1826 printk("SCB %d - Abort%s Completed.\n",
1827 scb
->hscb
->tag
, tag
== SCB_LIST_NULL
?
1829 ahc_abort_scbs(ahc
, target
, channel
,
1834 } else if (ahc_sent_msg(ahc
, AHCMSG_1B
,
1835 MSG_BUS_DEV_RESET
, TRUE
)) {
1838 * Don't mark the user's request for this BDR
1839 * as completing with CAM_BDR_SENT. CAM3
1840 * specifies CAM_REQ_CMP.
1843 && scb
->io_ctx
->ccb_h
.func_code
== XPT_RESET_DEV
1844 && ahc_match_scb(ahc
, scb
, target
, channel
,
1848 ahc_set_transaction_status(scb
, CAM_REQ_CMP
);
1851 ahc_compile_devinfo(&devinfo
,
1857 ahc_handle_devreset(ahc
, &devinfo
,
1860 /*verbose_level*/0);
1862 } else if (ahc_sent_msg(ahc
, AHCMSG_EXT
,
1863 MSG_EXT_PPR
, FALSE
)) {
1864 struct ahc_initiator_tinfo
*tinfo
;
1865 struct ahc_tmode_tstate
*tstate
;
1868 * PPR Rejected. Try non-ppr negotiation
1869 * and retry command.
1871 tinfo
= ahc_fetch_transinfo(ahc
,
1876 tinfo
->curr
.transport_version
= 2;
1877 tinfo
->goal
.transport_version
= 2;
1878 tinfo
->goal
.ppr_options
= 0;
1879 ahc_qinfifo_requeue_tail(ahc
, scb
);
1881 } else if (ahc_sent_msg(ahc
, AHCMSG_EXT
,
1882 MSG_EXT_WDTR
, FALSE
)) {
1884 * Negotiation Rejected. Go-narrow and
1887 ahc_set_width(ahc
, &devinfo
,
1888 MSG_EXT_WDTR_BUS_8_BIT
,
1889 AHC_TRANS_CUR
|AHC_TRANS_GOAL
,
1891 ahc_qinfifo_requeue_tail(ahc
, scb
);
1893 } else if (ahc_sent_msg(ahc
, AHCMSG_EXT
,
1894 MSG_EXT_SDTR
, FALSE
)) {
1896 * Negotiation Rejected. Go-async and
1899 ahc_set_syncrate(ahc
, &devinfo
,
1901 /*period*/0, /*offset*/0,
1903 AHC_TRANS_CUR
|AHC_TRANS_GOAL
,
1905 ahc_qinfifo_requeue_tail(ahc
, scb
);
1909 if (printerror
!= 0) {
1915 if ((scb
->hscb
->control
& TAG_ENB
) != 0)
1916 tag
= scb
->hscb
->tag
;
1918 tag
= SCB_LIST_NULL
;
1919 ahc_print_path(ahc
, scb
);
1920 ahc_abort_scbs(ahc
, target
, channel
,
1921 SCB_GET_LUN(scb
), tag
,
1926 * We had not fully identified this connection,
1927 * so we cannot abort anything.
1929 printk("%s: ", ahc_name(ahc
));
1931 for (i
= 0; i
< num_phases
; i
++) {
1932 if (lastphase
== ahc_phase_table
[i
].phase
)
1935 if (lastphase
!= P_BUSFREE
) {
1937 * Renegotiate with this device at the
1938 * next oportunity just in case this busfree
1939 * is due to a negotiation mismatch with the
1942 ahc_force_renegotiation(ahc
, &devinfo
);
1944 printk("Unexpected busfree %s\n"
1945 "SEQADDR == 0x%x\n",
1946 ahc_phase_table
[i
].phasemsg
,
1947 ahc_inb(ahc
, SEQADDR0
)
1948 | (ahc_inb(ahc
, SEQADDR1
) << 8));
1950 ahc_outb(ahc
, CLRINT
, CLRSCSIINT
);
1953 printk("%s: Missing case in ahc_handle_scsiint. status = %x\n",
1954 ahc_name(ahc
), status
);
1955 ahc_outb(ahc
, CLRINT
, CLRSCSIINT
);
1960 * Force renegotiation to occur the next time we initiate
1961 * a command to the current device.
1964 ahc_force_renegotiation(struct ahc_softc
*ahc
, struct ahc_devinfo
*devinfo
)
1966 struct ahc_initiator_tinfo
*targ_info
;
1967 struct ahc_tmode_tstate
*tstate
;
1969 targ_info
= ahc_fetch_transinfo(ahc
,
1971 devinfo
->our_scsiid
,
1974 ahc_update_neg_request(ahc
, devinfo
, tstate
,
1975 targ_info
, AHC_NEG_IF_NON_ASYNC
);
1978 #define AHC_MAX_STEPS 2000
1980 ahc_clear_critical_section(struct ahc_softc
*ahc
)
1987 if (ahc
->num_critical_sections
== 0)
1999 seqaddr
= ahc_inb(ahc
, SEQADDR0
)
2000 | (ahc_inb(ahc
, SEQADDR1
) << 8);
2003 * Seqaddr represents the next instruction to execute,
2004 * so we are really executing the instruction just
2009 cs
= ahc
->critical_sections
;
2010 for (i
= 0; i
< ahc
->num_critical_sections
; i
++, cs
++) {
2012 if (cs
->begin
< seqaddr
&& cs
->end
>= seqaddr
)
2016 if (i
== ahc
->num_critical_sections
)
2019 if (steps
> AHC_MAX_STEPS
) {
2020 printk("%s: Infinite loop in critical section\n",
2022 ahc_dump_card_state(ahc
);
2023 panic("critical section loop");
2027 if (stepping
== FALSE
) {
2030 * Disable all interrupt sources so that the
2031 * sequencer will not be stuck by a pausing
2032 * interrupt condition while we attempt to
2033 * leave a critical section.
2035 simode0
= ahc_inb(ahc
, SIMODE0
);
2036 ahc_outb(ahc
, SIMODE0
, 0);
2037 simode1
= ahc_inb(ahc
, SIMODE1
);
2038 if ((ahc
->features
& AHC_DT
) != 0)
2040 * On DT class controllers, we
2041 * use the enhanced busfree logic.
2042 * Unfortunately we cannot re-enable
2043 * busfree detection within the
2044 * current connection, so we must
2045 * leave it on while single stepping.
2047 ahc_outb(ahc
, SIMODE1
, simode1
& ENBUSFREE
);
2049 ahc_outb(ahc
, SIMODE1
, 0);
2050 ahc_outb(ahc
, CLRINT
, CLRSCSIINT
);
2051 ahc_outb(ahc
, SEQCTL
, ahc
->seqctl
| STEP
);
2054 if ((ahc
->features
& AHC_DT
) != 0) {
2055 ahc_outb(ahc
, CLRSINT1
, CLRBUSFREE
);
2056 ahc_outb(ahc
, CLRINT
, CLRSCSIINT
);
2058 ahc_outb(ahc
, HCNTRL
, ahc
->unpause
);
2059 while (!ahc_is_paused(ahc
))
2063 ahc_outb(ahc
, SIMODE0
, simode0
);
2064 ahc_outb(ahc
, SIMODE1
, simode1
);
2065 ahc_outb(ahc
, SEQCTL
, ahc
->seqctl
);
2070 * Clear any pending interrupt status.
2073 ahc_clear_intstat(struct ahc_softc
*ahc
)
2075 /* Clear any interrupt conditions this may have caused */
2076 ahc_outb(ahc
, CLRSINT1
, CLRSELTIMEO
|CLRATNO
|CLRSCSIRSTI
2077 |CLRBUSFREE
|CLRSCSIPERR
|CLRPHASECHG
|
2079 ahc_flush_device_writes(ahc
);
2080 ahc_outb(ahc
, CLRSINT0
, CLRSELDO
|CLRSELDI
|CLRSELINGO
);
2081 ahc_flush_device_writes(ahc
);
2082 ahc_outb(ahc
, CLRINT
, CLRSCSIINT
);
2083 ahc_flush_device_writes(ahc
);
2086 /**************************** Debugging Routines ******************************/
2088 uint32_t ahc_debug
= AHC_DEBUG_OPTS
;
2092 /************************* Transfer Negotiation *******************************/
2094 * Allocate per target mode instance (ID we respond to as a target)
2095 * transfer negotiation data structures.
2097 static struct ahc_tmode_tstate
*
2098 ahc_alloc_tstate(struct ahc_softc
*ahc
, u_int scsi_id
, char channel
)
2100 struct ahc_tmode_tstate
*master_tstate
;
2101 struct ahc_tmode_tstate
*tstate
;
2104 master_tstate
= ahc
->enabled_targets
[ahc
->our_id
];
2105 if (channel
== 'B') {
2107 master_tstate
= ahc
->enabled_targets
[ahc
->our_id_b
+ 8];
2109 if (ahc
->enabled_targets
[scsi_id
] != NULL
2110 && ahc
->enabled_targets
[scsi_id
] != master_tstate
)
2111 panic("%s: ahc_alloc_tstate - Target already allocated",
2113 tstate
= kmalloc(sizeof(*tstate
), GFP_ATOMIC
);
2118 * If we have allocated a master tstate, copy user settings from
2119 * the master tstate (taken from SRAM or the EEPROM) for this
2120 * channel, but reset our current and goal settings to async/narrow
2121 * until an initiator talks to us.
2123 if (master_tstate
!= NULL
) {
2124 memcpy(tstate
, master_tstate
, sizeof(*tstate
));
2125 memset(tstate
->enabled_luns
, 0, sizeof(tstate
->enabled_luns
));
2126 tstate
->ultraenb
= 0;
2127 for (i
= 0; i
< AHC_NUM_TARGETS
; i
++) {
2128 memset(&tstate
->transinfo
[i
].curr
, 0,
2129 sizeof(tstate
->transinfo
[i
].curr
));
2130 memset(&tstate
->transinfo
[i
].goal
, 0,
2131 sizeof(tstate
->transinfo
[i
].goal
));
2134 memset(tstate
, 0, sizeof(*tstate
));
2135 ahc
->enabled_targets
[scsi_id
] = tstate
;
2139 #ifdef AHC_TARGET_MODE
2141 * Free per target mode instance (ID we respond to as a target)
2142 * transfer negotiation data structures.
2145 ahc_free_tstate(struct ahc_softc
*ahc
, u_int scsi_id
, char channel
, int force
)
2147 struct ahc_tmode_tstate
*tstate
;
2150 * Don't clean up our "master" tstate.
2151 * It has our default user settings.
2153 if (((channel
== 'B' && scsi_id
== ahc
->our_id_b
)
2154 || (channel
== 'A' && scsi_id
== ahc
->our_id
))
2160 tstate
= ahc
->enabled_targets
[scsi_id
];
2163 ahc
->enabled_targets
[scsi_id
] = NULL
;
2168 * Called when we have an active connection to a target on the bus,
2169 * this function finds the nearest syncrate to the input period limited
2170 * by the capabilities of the bus connectivity of and sync settings for
2173 const struct ahc_syncrate
*
2174 ahc_devlimited_syncrate(struct ahc_softc
*ahc
,
2175 struct ahc_initiator_tinfo
*tinfo
,
2176 u_int
*period
, u_int
*ppr_options
, role_t role
)
2178 struct ahc_transinfo
*transinfo
;
2181 if ((ahc
->features
& AHC_ULTRA2
) != 0) {
2182 if ((ahc_inb(ahc
, SBLKCTL
) & ENAB40
) != 0
2183 && (ahc_inb(ahc
, SSTAT2
) & EXP_ACTIVE
) == 0) {
2184 maxsync
= AHC_SYNCRATE_DT
;
2186 maxsync
= AHC_SYNCRATE_ULTRA
;
2187 /* Can't do DT on an SE bus */
2188 *ppr_options
&= ~MSG_EXT_PPR_DT_REQ
;
2190 } else if ((ahc
->features
& AHC_ULTRA
) != 0) {
2191 maxsync
= AHC_SYNCRATE_ULTRA
;
2193 maxsync
= AHC_SYNCRATE_FAST
;
2196 * Never allow a value higher than our current goal
2197 * period otherwise we may allow a target initiated
2198 * negotiation to go above the limit as set by the
2199 * user. In the case of an initiator initiated
2200 * sync negotiation, we limit based on the user
2201 * setting. This allows the system to still accept
2202 * incoming negotiations even if target initiated
2203 * negotiation is not performed.
2205 if (role
== ROLE_TARGET
)
2206 transinfo
= &tinfo
->user
;
2208 transinfo
= &tinfo
->goal
;
2209 *ppr_options
&= transinfo
->ppr_options
;
2210 if (transinfo
->width
== MSG_EXT_WDTR_BUS_8_BIT
) {
2211 maxsync
= max(maxsync
, (u_int
)AHC_SYNCRATE_ULTRA2
);
2212 *ppr_options
&= ~MSG_EXT_PPR_DT_REQ
;
2214 if (transinfo
->period
== 0) {
2219 *period
= max(*period
, (u_int
)transinfo
->period
);
2220 return (ahc_find_syncrate(ahc
, period
, ppr_options
, maxsync
));
2224 * Look up the valid period to SCSIRATE conversion in our table.
2225 * Return the period and offset that should be sent to the target
2226 * if this was the beginning of an SDTR.
2228 const struct ahc_syncrate
*
2229 ahc_find_syncrate(struct ahc_softc
*ahc
, u_int
*period
,
2230 u_int
*ppr_options
, u_int maxsync
)
2232 const struct ahc_syncrate
*syncrate
;
2234 if ((ahc
->features
& AHC_DT
) == 0)
2235 *ppr_options
&= ~MSG_EXT_PPR_DT_REQ
;
2237 /* Skip all DT only entries if DT is not available */
2238 if ((*ppr_options
& MSG_EXT_PPR_DT_REQ
) == 0
2239 && maxsync
< AHC_SYNCRATE_ULTRA2
)
2240 maxsync
= AHC_SYNCRATE_ULTRA2
;
2242 /* Now set the maxsync based on the card capabilities
2243 * DT is already done above */
2244 if ((ahc
->features
& (AHC_DT
| AHC_ULTRA2
)) == 0
2245 && maxsync
< AHC_SYNCRATE_ULTRA
)
2246 maxsync
= AHC_SYNCRATE_ULTRA
;
2247 if ((ahc
->features
& (AHC_DT
| AHC_ULTRA2
| AHC_ULTRA
)) == 0
2248 && maxsync
< AHC_SYNCRATE_FAST
)
2249 maxsync
= AHC_SYNCRATE_FAST
;
2251 for (syncrate
= &ahc_syncrates
[maxsync
];
2252 syncrate
->rate
!= NULL
;
2256 * The Ultra2 table doesn't go as low
2257 * as for the Fast/Ultra cards.
2259 if ((ahc
->features
& AHC_ULTRA2
) != 0
2260 && (syncrate
->sxfr_u2
== 0))
2263 if (*period
<= syncrate
->period
) {
2265 * When responding to a target that requests
2266 * sync, the requested rate may fall between
2267 * two rates that we can output, but still be
2268 * a rate that we can receive. Because of this,
2269 * we want to respond to the target with
2270 * the same rate that it sent to us even
2271 * if the period we use to send data to it
2272 * is lower. Only lower the response period
2275 if (syncrate
== &ahc_syncrates
[maxsync
])
2276 *period
= syncrate
->period
;
2279 * At some speeds, we only support
2282 if ((syncrate
->sxfr_u2
& ST_SXFR
) != 0)
2283 *ppr_options
&= ~MSG_EXT_PPR_DT_REQ
;
2289 || (syncrate
->rate
== NULL
)
2290 || ((ahc
->features
& AHC_ULTRA2
) != 0
2291 && (syncrate
->sxfr_u2
== 0))) {
2292 /* Use asynchronous transfers. */
2295 *ppr_options
&= ~MSG_EXT_PPR_DT_REQ
;
2301 * Convert from an entry in our syncrate table to the SCSI equivalent
2302 * sync "period" factor.
2305 ahc_find_period(struct ahc_softc
*ahc
, u_int scsirate
, u_int maxsync
)
2307 const struct ahc_syncrate
*syncrate
;
2309 if ((ahc
->features
& AHC_ULTRA2
) != 0)
2310 scsirate
&= SXFR_ULTRA2
;
2314 /* now set maxsync based on card capabilities */
2315 if ((ahc
->features
& AHC_DT
) == 0 && maxsync
< AHC_SYNCRATE_ULTRA2
)
2316 maxsync
= AHC_SYNCRATE_ULTRA2
;
2317 if ((ahc
->features
& (AHC_DT
| AHC_ULTRA2
)) == 0
2318 && maxsync
< AHC_SYNCRATE_ULTRA
)
2319 maxsync
= AHC_SYNCRATE_ULTRA
;
2320 if ((ahc
->features
& (AHC_DT
| AHC_ULTRA2
| AHC_ULTRA
)) == 0
2321 && maxsync
< AHC_SYNCRATE_FAST
)
2322 maxsync
= AHC_SYNCRATE_FAST
;
2325 syncrate
= &ahc_syncrates
[maxsync
];
2326 while (syncrate
->rate
!= NULL
) {
2328 if ((ahc
->features
& AHC_ULTRA2
) != 0) {
2329 if (syncrate
->sxfr_u2
== 0)
2331 else if (scsirate
== (syncrate
->sxfr_u2
& SXFR_ULTRA2
))
2332 return (syncrate
->period
);
2333 } else if (scsirate
== (syncrate
->sxfr
& SXFR
)) {
2334 return (syncrate
->period
);
2338 return (0); /* async */
2342 * Truncate the given synchronous offset to a value the
2343 * current adapter type and syncrate are capable of.
2346 ahc_validate_offset(struct ahc_softc
*ahc
,
2347 struct ahc_initiator_tinfo
*tinfo
,
2348 const struct ahc_syncrate
*syncrate
,
2349 u_int
*offset
, int wide
, role_t role
)
2353 /* Limit offset to what we can do */
2354 if (syncrate
== NULL
) {
2356 } else if ((ahc
->features
& AHC_ULTRA2
) != 0) {
2357 maxoffset
= MAX_OFFSET_ULTRA2
;
2360 maxoffset
= MAX_OFFSET_16BIT
;
2362 maxoffset
= MAX_OFFSET_8BIT
;
2364 *offset
= min(*offset
, maxoffset
);
2365 if (tinfo
!= NULL
) {
2366 if (role
== ROLE_TARGET
)
2367 *offset
= min(*offset
, (u_int
)tinfo
->user
.offset
);
2369 *offset
= min(*offset
, (u_int
)tinfo
->goal
.offset
);
2374 * Truncate the given transfer width parameter to a value the
2375 * current adapter type is capable of.
2378 ahc_validate_width(struct ahc_softc
*ahc
, struct ahc_initiator_tinfo
*tinfo
,
2379 u_int
*bus_width
, role_t role
)
2381 switch (*bus_width
) {
2383 if (ahc
->features
& AHC_WIDE
) {
2385 *bus_width
= MSG_EXT_WDTR_BUS_16_BIT
;
2389 case MSG_EXT_WDTR_BUS_8_BIT
:
2390 *bus_width
= MSG_EXT_WDTR_BUS_8_BIT
;
2393 if (tinfo
!= NULL
) {
2394 if (role
== ROLE_TARGET
)
2395 *bus_width
= min((u_int
)tinfo
->user
.width
, *bus_width
);
2397 *bus_width
= min((u_int
)tinfo
->goal
.width
, *bus_width
);
2402 * Update the bitmask of targets for which the controller should
2403 * negotiate with at the next convenient oportunity. This currently
2404 * means the next time we send the initial identify messages for
2405 * a new transaction.
2408 ahc_update_neg_request(struct ahc_softc
*ahc
, struct ahc_devinfo
*devinfo
,
2409 struct ahc_tmode_tstate
*tstate
,
2410 struct ahc_initiator_tinfo
*tinfo
, ahc_neg_type neg_type
)
2412 u_int auto_negotiate_orig
;
2414 auto_negotiate_orig
= tstate
->auto_negotiate
;
2415 if (neg_type
== AHC_NEG_ALWAYS
) {
2417 * Force our "current" settings to be
2418 * unknown so that unless a bus reset
2419 * occurs the need to renegotiate is
2420 * recorded persistently.
2422 if ((ahc
->features
& AHC_WIDE
) != 0)
2423 tinfo
->curr
.width
= AHC_WIDTH_UNKNOWN
;
2424 tinfo
->curr
.period
= AHC_PERIOD_UNKNOWN
;
2425 tinfo
->curr
.offset
= AHC_OFFSET_UNKNOWN
;
2427 if (tinfo
->curr
.period
!= tinfo
->goal
.period
2428 || tinfo
->curr
.width
!= tinfo
->goal
.width
2429 || tinfo
->curr
.offset
!= tinfo
->goal
.offset
2430 || tinfo
->curr
.ppr_options
!= tinfo
->goal
.ppr_options
2431 || (neg_type
== AHC_NEG_IF_NON_ASYNC
2432 && (tinfo
->goal
.offset
!= 0
2433 || tinfo
->goal
.width
!= MSG_EXT_WDTR_BUS_8_BIT
2434 || tinfo
->goal
.ppr_options
!= 0)))
2435 tstate
->auto_negotiate
|= devinfo
->target_mask
;
2437 tstate
->auto_negotiate
&= ~devinfo
->target_mask
;
2439 return (auto_negotiate_orig
!= tstate
->auto_negotiate
);
2443 * Update the user/goal/curr tables of synchronous negotiation
2444 * parameters as well as, in the case of a current or active update,
2445 * any data structures on the host controller. In the case of an
2446 * active update, the specified target is currently talking to us on
2447 * the bus, so the transfer parameter update must take effect
2451 ahc_set_syncrate(struct ahc_softc
*ahc
, struct ahc_devinfo
*devinfo
,
2452 const struct ahc_syncrate
*syncrate
, u_int period
,
2453 u_int offset
, u_int ppr_options
, u_int type
, int paused
)
2455 struct ahc_initiator_tinfo
*tinfo
;
2456 struct ahc_tmode_tstate
*tstate
;
2463 active
= (type
& AHC_TRANS_ACTIVE
) == AHC_TRANS_ACTIVE
;
2466 if (syncrate
== NULL
) {
2471 tinfo
= ahc_fetch_transinfo(ahc
, devinfo
->channel
, devinfo
->our_scsiid
,
2472 devinfo
->target
, &tstate
);
2474 if ((type
& AHC_TRANS_USER
) != 0) {
2475 tinfo
->user
.period
= period
;
2476 tinfo
->user
.offset
= offset
;
2477 tinfo
->user
.ppr_options
= ppr_options
;
2480 if ((type
& AHC_TRANS_GOAL
) != 0) {
2481 tinfo
->goal
.period
= period
;
2482 tinfo
->goal
.offset
= offset
;
2483 tinfo
->goal
.ppr_options
= ppr_options
;
2486 old_period
= tinfo
->curr
.period
;
2487 old_offset
= tinfo
->curr
.offset
;
2488 old_ppr
= tinfo
->curr
.ppr_options
;
2490 if ((type
& AHC_TRANS_CUR
) != 0
2491 && (old_period
!= period
2492 || old_offset
!= offset
2493 || old_ppr
!= ppr_options
)) {
2497 scsirate
= tinfo
->scsirate
;
2498 if ((ahc
->features
& AHC_ULTRA2
) != 0) {
2500 scsirate
&= ~(SXFR_ULTRA2
|SINGLE_EDGE
|ENABLE_CRC
);
2501 if (syncrate
!= NULL
) {
2502 scsirate
|= syncrate
->sxfr_u2
;
2503 if ((ppr_options
& MSG_EXT_PPR_DT_REQ
) != 0)
2504 scsirate
|= ENABLE_CRC
;
2506 scsirate
|= SINGLE_EDGE
;
2510 scsirate
&= ~(SXFR
|SOFS
);
2512 * Ensure Ultra mode is set properly for
2515 tstate
->ultraenb
&= ~devinfo
->target_mask
;
2516 if (syncrate
!= NULL
) {
2517 if (syncrate
->sxfr
& ULTRA_SXFR
) {
2519 devinfo
->target_mask
;
2521 scsirate
|= syncrate
->sxfr
& SXFR
;
2522 scsirate
|= offset
& SOFS
;
2527 sxfrctl0
= ahc_inb(ahc
, SXFRCTL0
);
2528 sxfrctl0
&= ~FAST20
;
2529 if (tstate
->ultraenb
& devinfo
->target_mask
)
2531 ahc_outb(ahc
, SXFRCTL0
, sxfrctl0
);
2535 ahc_outb(ahc
, SCSIRATE
, scsirate
);
2536 if ((ahc
->features
& AHC_ULTRA2
) != 0)
2537 ahc_outb(ahc
, SCSIOFFSET
, offset
);
2540 tinfo
->scsirate
= scsirate
;
2541 tinfo
->curr
.period
= period
;
2542 tinfo
->curr
.offset
= offset
;
2543 tinfo
->curr
.ppr_options
= ppr_options
;
2545 ahc_send_async(ahc
, devinfo
->channel
, devinfo
->target
,
2546 CAM_LUN_WILDCARD
, AC_TRANSFER_NEG
);
2549 printk("%s: target %d synchronous at %sMHz%s, "
2550 "offset = 0x%x\n", ahc_name(ahc
),
2551 devinfo
->target
, syncrate
->rate
,
2552 (ppr_options
& MSG_EXT_PPR_DT_REQ
)
2553 ? " DT" : "", offset
);
2555 printk("%s: target %d using "
2556 "asynchronous transfers\n",
2557 ahc_name(ahc
), devinfo
->target
);
2562 update_needed
+= ahc_update_neg_request(ahc
, devinfo
, tstate
,
2563 tinfo
, AHC_NEG_TO_GOAL
);
2566 ahc_update_pending_scbs(ahc
);
2570 * Update the user/goal/curr tables of wide negotiation
2571 * parameters as well as, in the case of a current or active update,
2572 * any data structures on the host controller. In the case of an
2573 * active update, the specified target is currently talking to us on
2574 * the bus, so the transfer parameter update must take effect
2578 ahc_set_width(struct ahc_softc
*ahc
, struct ahc_devinfo
*devinfo
,
2579 u_int width
, u_int type
, int paused
)
2581 struct ahc_initiator_tinfo
*tinfo
;
2582 struct ahc_tmode_tstate
*tstate
;
2587 active
= (type
& AHC_TRANS_ACTIVE
) == AHC_TRANS_ACTIVE
;
2589 tinfo
= ahc_fetch_transinfo(ahc
, devinfo
->channel
, devinfo
->our_scsiid
,
2590 devinfo
->target
, &tstate
);
2592 if ((type
& AHC_TRANS_USER
) != 0)
2593 tinfo
->user
.width
= width
;
2595 if ((type
& AHC_TRANS_GOAL
) != 0)
2596 tinfo
->goal
.width
= width
;
2598 oldwidth
= tinfo
->curr
.width
;
2599 if ((type
& AHC_TRANS_CUR
) != 0 && oldwidth
!= width
) {
2603 scsirate
= tinfo
->scsirate
;
2604 scsirate
&= ~WIDEXFER
;
2605 if (width
== MSG_EXT_WDTR_BUS_16_BIT
)
2606 scsirate
|= WIDEXFER
;
2608 tinfo
->scsirate
= scsirate
;
2611 ahc_outb(ahc
, SCSIRATE
, scsirate
);
2613 tinfo
->curr
.width
= width
;
2615 ahc_send_async(ahc
, devinfo
->channel
, devinfo
->target
,
2616 CAM_LUN_WILDCARD
, AC_TRANSFER_NEG
);
2618 printk("%s: target %d using %dbit transfers\n",
2619 ahc_name(ahc
), devinfo
->target
,
2620 8 * (0x01 << width
));
2624 update_needed
+= ahc_update_neg_request(ahc
, devinfo
, tstate
,
2625 tinfo
, AHC_NEG_TO_GOAL
);
2627 ahc_update_pending_scbs(ahc
);
2631 * Update the current state of tagged queuing for a given target.
2634 ahc_set_tags(struct ahc_softc
*ahc
, struct scsi_cmnd
*cmd
,
2635 struct ahc_devinfo
*devinfo
, ahc_queue_alg alg
)
2637 struct scsi_device
*sdev
= cmd
->device
;
2639 ahc_platform_set_tags(ahc
, sdev
, devinfo
, alg
);
2640 ahc_send_async(ahc
, devinfo
->channel
, devinfo
->target
,
2641 devinfo
->lun
, AC_TRANSFER_NEG
);
2645 * When the transfer settings for a connection change, update any
2646 * in-transit SCBs to contain the new data so the hardware will
2647 * be set correctly during future (re)selections.
2650 ahc_update_pending_scbs(struct ahc_softc
*ahc
)
2652 struct scb
*pending_scb
;
2653 int pending_scb_count
;
2659 * Traverse the pending SCB list and ensure that all of the
2660 * SCBs there have the proper settings.
2662 pending_scb_count
= 0;
2663 LIST_FOREACH(pending_scb
, &ahc
->pending_scbs
, pending_links
) {
2664 struct ahc_devinfo devinfo
;
2665 struct hardware_scb
*pending_hscb
;
2666 struct ahc_initiator_tinfo
*tinfo
;
2667 struct ahc_tmode_tstate
*tstate
;
2669 ahc_scb_devinfo(ahc
, &devinfo
, pending_scb
);
2670 tinfo
= ahc_fetch_transinfo(ahc
, devinfo
.channel
,
2672 devinfo
.target
, &tstate
);
2673 pending_hscb
= pending_scb
->hscb
;
2674 pending_hscb
->control
&= ~ULTRAENB
;
2675 if ((tstate
->ultraenb
& devinfo
.target_mask
) != 0)
2676 pending_hscb
->control
|= ULTRAENB
;
2677 pending_hscb
->scsirate
= tinfo
->scsirate
;
2678 pending_hscb
->scsioffset
= tinfo
->curr
.offset
;
2679 if ((tstate
->auto_negotiate
& devinfo
.target_mask
) == 0
2680 && (pending_scb
->flags
& SCB_AUTO_NEGOTIATE
) != 0) {
2681 pending_scb
->flags
&= ~SCB_AUTO_NEGOTIATE
;
2682 pending_hscb
->control
&= ~MK_MESSAGE
;
2684 ahc_sync_scb(ahc
, pending_scb
,
2685 BUS_DMASYNC_PREREAD
|BUS_DMASYNC_PREWRITE
);
2686 pending_scb_count
++;
2689 if (pending_scb_count
== 0)
2692 if (ahc_is_paused(ahc
)) {
2699 saved_scbptr
= ahc_inb(ahc
, SCBPTR
);
2700 /* Ensure that the hscbs down on the card match the new information */
2701 for (i
= 0; i
< ahc
->scb_data
->maxhscbs
; i
++) {
2702 struct hardware_scb
*pending_hscb
;
2706 ahc_outb(ahc
, SCBPTR
, i
);
2707 scb_tag
= ahc_inb(ahc
, SCB_TAG
);
2708 pending_scb
= ahc_lookup_scb(ahc
, scb_tag
);
2709 if (pending_scb
== NULL
)
2712 pending_hscb
= pending_scb
->hscb
;
2713 control
= ahc_inb(ahc
, SCB_CONTROL
);
2714 control
&= ~(ULTRAENB
|MK_MESSAGE
);
2715 control
|= pending_hscb
->control
& (ULTRAENB
|MK_MESSAGE
);
2716 ahc_outb(ahc
, SCB_CONTROL
, control
);
2717 ahc_outb(ahc
, SCB_SCSIRATE
, pending_hscb
->scsirate
);
2718 ahc_outb(ahc
, SCB_SCSIOFFSET
, pending_hscb
->scsioffset
);
2720 ahc_outb(ahc
, SCBPTR
, saved_scbptr
);
2726 /**************************** Pathing Information *****************************/
2728 ahc_fetch_devinfo(struct ahc_softc
*ahc
, struct ahc_devinfo
*devinfo
)
2734 if (ahc_inb(ahc
, SSTAT0
) & TARGET
)
2737 role
= ROLE_INITIATOR
;
2739 if (role
== ROLE_TARGET
2740 && (ahc
->features
& AHC_MULTI_TID
) != 0
2741 && (ahc_inb(ahc
, SEQ_FLAGS
)
2742 & (CMDPHASE_PENDING
|TARG_CMD_PENDING
|NO_DISCONNECT
)) != 0) {
2743 /* We were selected, so pull our id from TARGIDIN */
2744 our_id
= ahc_inb(ahc
, TARGIDIN
) & OID
;
2745 } else if ((ahc
->features
& AHC_ULTRA2
) != 0)
2746 our_id
= ahc_inb(ahc
, SCSIID_ULTRA2
) & OID
;
2748 our_id
= ahc_inb(ahc
, SCSIID
) & OID
;
2750 saved_scsiid
= ahc_inb(ahc
, SAVED_SCSIID
);
2751 ahc_compile_devinfo(devinfo
,
2753 SCSIID_TARGET(ahc
, saved_scsiid
),
2754 ahc_inb(ahc
, SAVED_LUN
),
2755 SCSIID_CHANNEL(ahc
, saved_scsiid
),
2759 static const struct ahc_phase_table_entry
*
2760 ahc_lookup_phase_entry(int phase
)
2762 const struct ahc_phase_table_entry
*entry
;
2763 const struct ahc_phase_table_entry
*last_entry
;
2766 * num_phases doesn't include the default entry which
2767 * will be returned if the phase doesn't match.
2769 last_entry
= &ahc_phase_table
[num_phases
];
2770 for (entry
= ahc_phase_table
; entry
< last_entry
; entry
++) {
2771 if (phase
== entry
->phase
)
2778 ahc_compile_devinfo(struct ahc_devinfo
*devinfo
, u_int our_id
, u_int target
,
2779 u_int lun
, char channel
, role_t role
)
2781 devinfo
->our_scsiid
= our_id
;
2782 devinfo
->target
= target
;
2784 devinfo
->target_offset
= target
;
2785 devinfo
->channel
= channel
;
2786 devinfo
->role
= role
;
2788 devinfo
->target_offset
+= 8;
2789 devinfo
->target_mask
= (0x01 << devinfo
->target_offset
);
2793 ahc_print_devinfo(struct ahc_softc
*ahc
, struct ahc_devinfo
*devinfo
)
2795 printk("%s:%c:%d:%d: ", ahc_name(ahc
), devinfo
->channel
,
2796 devinfo
->target
, devinfo
->lun
);
2800 ahc_scb_devinfo(struct ahc_softc
*ahc
, struct ahc_devinfo
*devinfo
,
2806 our_id
= SCSIID_OUR_ID(scb
->hscb
->scsiid
);
2807 role
= ROLE_INITIATOR
;
2808 if ((scb
->flags
& SCB_TARGET_SCB
) != 0)
2810 ahc_compile_devinfo(devinfo
, our_id
, SCB_GET_TARGET(ahc
, scb
),
2811 SCB_GET_LUN(scb
), SCB_GET_CHANNEL(ahc
, scb
), role
);
2815 /************************ Message Phase Processing ****************************/
2817 ahc_assert_atn(struct ahc_softc
*ahc
)
2822 if ((ahc
->features
& AHC_DT
) == 0)
2823 scsisigo
|= ahc_inb(ahc
, SCSISIGI
);
2824 ahc_outb(ahc
, SCSISIGO
, scsisigo
);
2828 * When an initiator transaction with the MK_MESSAGE flag either reconnects
2829 * or enters the initial message out phase, we are interrupted. Fill our
2830 * outgoing message buffer with the appropriate message and beging handing
2831 * the message phase(s) manually.
2834 ahc_setup_initiator_msgout(struct ahc_softc
*ahc
, struct ahc_devinfo
*devinfo
,
2838 * To facilitate adding multiple messages together,
2839 * each routine should increment the index and len
2840 * variables instead of setting them explicitly.
2842 ahc
->msgout_index
= 0;
2843 ahc
->msgout_len
= 0;
2845 if ((scb
->flags
& SCB_DEVICE_RESET
) == 0
2846 && ahc_inb(ahc
, MSG_OUT
) == MSG_IDENTIFYFLAG
) {
2849 identify_msg
= MSG_IDENTIFYFLAG
| SCB_GET_LUN(scb
);
2850 if ((scb
->hscb
->control
& DISCENB
) != 0)
2851 identify_msg
|= MSG_IDENTIFY_DISCFLAG
;
2852 ahc
->msgout_buf
[ahc
->msgout_index
++] = identify_msg
;
2855 if ((scb
->hscb
->control
& TAG_ENB
) != 0) {
2856 ahc
->msgout_buf
[ahc
->msgout_index
++] =
2857 scb
->hscb
->control
& (TAG_ENB
|SCB_TAG_TYPE
);
2858 ahc
->msgout_buf
[ahc
->msgout_index
++] = scb
->hscb
->tag
;
2859 ahc
->msgout_len
+= 2;
2863 if (scb
->flags
& SCB_DEVICE_RESET
) {
2864 ahc
->msgout_buf
[ahc
->msgout_index
++] = MSG_BUS_DEV_RESET
;
2866 ahc_print_path(ahc
, scb
);
2867 printk("Bus Device Reset Message Sent\n");
2869 * Clear our selection hardware in advance of
2870 * the busfree. We may have an entry in the waiting
2871 * Q for this target, and we don't want to go about
2872 * selecting while we handle the busfree and blow it
2875 ahc_outb(ahc
, SCSISEQ
, (ahc_inb(ahc
, SCSISEQ
) & ~ENSELO
));
2876 } else if ((scb
->flags
& SCB_ABORT
) != 0) {
2877 if ((scb
->hscb
->control
& TAG_ENB
) != 0)
2878 ahc
->msgout_buf
[ahc
->msgout_index
++] = MSG_ABORT_TAG
;
2880 ahc
->msgout_buf
[ahc
->msgout_index
++] = MSG_ABORT
;
2882 ahc_print_path(ahc
, scb
);
2883 printk("Abort%s Message Sent\n",
2884 (scb
->hscb
->control
& TAG_ENB
) != 0 ? " Tag" : "");
2886 * Clear our selection hardware in advance of
2887 * the busfree. We may have an entry in the waiting
2888 * Q for this target, and we don't want to go about
2889 * selecting while we handle the busfree and blow it
2892 ahc_outb(ahc
, SCSISEQ
, (ahc_inb(ahc
, SCSISEQ
) & ~ENSELO
));
2893 } else if ((scb
->flags
& (SCB_AUTO_NEGOTIATE
|SCB_NEGOTIATE
)) != 0) {
2894 ahc_build_transfer_msg(ahc
, devinfo
);
2896 printk("ahc_intr: AWAITING_MSG for an SCB that "
2897 "does not have a waiting message\n");
2898 printk("SCSIID = %x, target_mask = %x\n", scb
->hscb
->scsiid
,
2899 devinfo
->target_mask
);
2900 panic("SCB = %d, SCB Control = %x, MSG_OUT = %x "
2901 "SCB flags = %x", scb
->hscb
->tag
, scb
->hscb
->control
,
2902 ahc_inb(ahc
, MSG_OUT
), scb
->flags
);
2906 * Clear the MK_MESSAGE flag from the SCB so we aren't
2907 * asked to send this message again.
2909 ahc_outb(ahc
, SCB_CONTROL
, ahc_inb(ahc
, SCB_CONTROL
) & ~MK_MESSAGE
);
2910 scb
->hscb
->control
&= ~MK_MESSAGE
;
2911 ahc
->msgout_index
= 0;
2912 ahc
->msg_type
= MSG_TYPE_INITIATOR_MSGOUT
;
2916 * Build an appropriate transfer negotiation message for the
2917 * currently active target.
2920 ahc_build_transfer_msg(struct ahc_softc
*ahc
, struct ahc_devinfo
*devinfo
)
2923 * We need to initiate transfer negotiations.
2924 * If our current and goal settings are identical,
2925 * we want to renegotiate due to a check condition.
2927 struct ahc_initiator_tinfo
*tinfo
;
2928 struct ahc_tmode_tstate
*tstate
;
2929 const struct ahc_syncrate
*rate
;
2937 tinfo
= ahc_fetch_transinfo(ahc
, devinfo
->channel
, devinfo
->our_scsiid
,
2938 devinfo
->target
, &tstate
);
2940 * Filter our period based on the current connection.
2941 * If we can't perform DT transfers on this segment (not in LVD
2942 * mode for instance), then our decision to issue a PPR message
2945 period
= tinfo
->goal
.period
;
2946 offset
= tinfo
->goal
.offset
;
2947 ppr_options
= tinfo
->goal
.ppr_options
;
2948 /* Target initiated PPR is not allowed in the SCSI spec */
2949 if (devinfo
->role
== ROLE_TARGET
)
2951 rate
= ahc_devlimited_syncrate(ahc
, tinfo
, &period
,
2952 &ppr_options
, devinfo
->role
);
2953 dowide
= tinfo
->curr
.width
!= tinfo
->goal
.width
;
2954 dosync
= tinfo
->curr
.offset
!= offset
|| tinfo
->curr
.period
!= period
;
2956 * Only use PPR if we have options that need it, even if the device
2957 * claims to support it. There might be an expander in the way
2960 doppr
= ppr_options
!= 0;
2962 if (!dowide
&& !dosync
&& !doppr
) {
2963 dowide
= tinfo
->goal
.width
!= MSG_EXT_WDTR_BUS_8_BIT
;
2964 dosync
= tinfo
->goal
.offset
!= 0;
2967 if (!dowide
&& !dosync
&& !doppr
) {
2969 * Force async with a WDTR message if we have a wide bus,
2970 * or just issue an SDTR with a 0 offset.
2972 if ((ahc
->features
& AHC_WIDE
) != 0)
2978 ahc_print_devinfo(ahc
, devinfo
);
2979 printk("Ensuring async\n");
2983 /* Target initiated PPR is not allowed in the SCSI spec */
2984 if (devinfo
->role
== ROLE_TARGET
)
2988 * Both the PPR message and SDTR message require the
2989 * goal syncrate to be limited to what the target device
2990 * is capable of handling (based on whether an LVD->SE
2991 * expander is on the bus), so combine these two cases.
2992 * Regardless, guarantee that if we are using WDTR and SDTR
2993 * messages that WDTR comes first.
2995 if (doppr
|| (dosync
&& !dowide
)) {
2997 offset
= tinfo
->goal
.offset
;
2998 ahc_validate_offset(ahc
, tinfo
, rate
, &offset
,
2999 doppr
? tinfo
->goal
.width
3000 : tinfo
->curr
.width
,
3003 ahc_construct_ppr(ahc
, devinfo
, period
, offset
,
3004 tinfo
->goal
.width
, ppr_options
);
3006 ahc_construct_sdtr(ahc
, devinfo
, period
, offset
);
3009 ahc_construct_wdtr(ahc
, devinfo
, tinfo
->goal
.width
);
3014 * Build a synchronous negotiation message in our message
3015 * buffer based on the input parameters.
3018 ahc_construct_sdtr(struct ahc_softc
*ahc
, struct ahc_devinfo
*devinfo
,
3019 u_int period
, u_int offset
)
3022 period
= AHC_ASYNC_XFER_PERIOD
;
3023 ahc
->msgout_index
+= spi_populate_sync_msg(
3024 ahc
->msgout_buf
+ ahc
->msgout_index
, period
, offset
);
3025 ahc
->msgout_len
+= 5;
3027 printk("(%s:%c:%d:%d): Sending SDTR period %x, offset %x\n",
3028 ahc_name(ahc
), devinfo
->channel
, devinfo
->target
,
3029 devinfo
->lun
, period
, offset
);
3034 * Build a wide negotiation message in our message
3035 * buffer based on the input parameters.
3038 ahc_construct_wdtr(struct ahc_softc
*ahc
, struct ahc_devinfo
*devinfo
,
3041 ahc
->msgout_index
+= spi_populate_width_msg(
3042 ahc
->msgout_buf
+ ahc
->msgout_index
, bus_width
);
3043 ahc
->msgout_len
+= 4;
3045 printk("(%s:%c:%d:%d): Sending WDTR %x\n",
3046 ahc_name(ahc
), devinfo
->channel
, devinfo
->target
,
3047 devinfo
->lun
, bus_width
);
3052 * Build a parallel protocol request message in our message
3053 * buffer based on the input parameters.
3056 ahc_construct_ppr(struct ahc_softc
*ahc
, struct ahc_devinfo
*devinfo
,
3057 u_int period
, u_int offset
, u_int bus_width
,
3061 period
= AHC_ASYNC_XFER_PERIOD
;
3062 ahc
->msgout_index
+= spi_populate_ppr_msg(
3063 ahc
->msgout_buf
+ ahc
->msgout_index
, period
, offset
,
3064 bus_width
, ppr_options
);
3065 ahc
->msgout_len
+= 8;
3067 printk("(%s:%c:%d:%d): Sending PPR bus_width %x, period %x, "
3068 "offset %x, ppr_options %x\n", ahc_name(ahc
),
3069 devinfo
->channel
, devinfo
->target
, devinfo
->lun
,
3070 bus_width
, period
, offset
, ppr_options
);
3075 * Clear any active message state.
3078 ahc_clear_msg_state(struct ahc_softc
*ahc
)
3080 ahc
->msgout_len
= 0;
3081 ahc
->msgin_index
= 0;
3082 ahc
->msg_type
= MSG_TYPE_NONE
;
3083 if ((ahc_inb(ahc
, SCSISIGI
) & ATNI
) != 0) {
3085 * The target didn't care to respond to our
3086 * message request, so clear ATN.
3088 ahc_outb(ahc
, CLRSINT1
, CLRATNO
);
3090 ahc_outb(ahc
, MSG_OUT
, MSG_NOOP
);
3091 ahc_outb(ahc
, SEQ_FLAGS2
,
3092 ahc_inb(ahc
, SEQ_FLAGS2
) & ~TARGET_MSG_PENDING
);
3096 ahc_handle_proto_violation(struct ahc_softc
*ahc
)
3098 struct ahc_devinfo devinfo
;
3106 ahc_fetch_devinfo(ahc
, &devinfo
);
3107 scbid
= ahc_inb(ahc
, SCB_TAG
);
3108 scb
= ahc_lookup_scb(ahc
, scbid
);
3109 seq_flags
= ahc_inb(ahc
, SEQ_FLAGS
);
3110 curphase
= ahc_inb(ahc
, SCSISIGI
) & PHASE_MASK
;
3111 lastphase
= ahc_inb(ahc
, LASTPHASE
);
3112 if ((seq_flags
& NOT_IDENTIFIED
) != 0) {
3115 * The reconnecting target either did not send an
3116 * identify message, or did, but we didn't find an SCB
3119 ahc_print_devinfo(ahc
, &devinfo
);
3120 printk("Target did not send an IDENTIFY message. "
3121 "LASTPHASE = 0x%x.\n", lastphase
);
3123 } else if (scb
== NULL
) {
3125 * We don't seem to have an SCB active for this
3126 * transaction. Print an error and reset the bus.
3128 ahc_print_devinfo(ahc
, &devinfo
);
3129 printk("No SCB found during protocol violation\n");
3130 goto proto_violation_reset
;
3132 ahc_set_transaction_status(scb
, CAM_SEQUENCE_FAIL
);
3133 if ((seq_flags
& NO_CDB_SENT
) != 0) {
3134 ahc_print_path(ahc
, scb
);
3135 printk("No or incomplete CDB sent to device.\n");
3136 } else if ((ahc_inb(ahc
, SCB_CONTROL
) & STATUS_RCVD
) == 0) {
3138 * The target never bothered to provide status to
3139 * us prior to completing the command. Since we don't
3140 * know the disposition of this command, we must attempt
3141 * to abort it. Assert ATN and prepare to send an abort
3144 ahc_print_path(ahc
, scb
);
3145 printk("Completed command without status.\n");
3147 ahc_print_path(ahc
, scb
);
3148 printk("Unknown protocol violation.\n");
3149 ahc_dump_card_state(ahc
);
3152 if ((lastphase
& ~P_DATAIN_DT
) == 0
3153 || lastphase
== P_COMMAND
) {
3154 proto_violation_reset
:
3156 * Target either went directly to data/command
3157 * phase or didn't respond to our ATN.
3158 * The only safe thing to do is to blow
3159 * it away with a bus reset.
3161 found
= ahc_reset_channel(ahc
, 'A', TRUE
);
3162 printk("%s: Issued Channel %c Bus Reset. "
3163 "%d SCBs aborted\n", ahc_name(ahc
), 'A', found
);
3166 * Leave the selection hardware off in case
3167 * this abort attempt will affect yet to
3170 ahc_outb(ahc
, SCSISEQ
,
3171 ahc_inb(ahc
, SCSISEQ
) & ~ENSELO
);
3172 ahc_assert_atn(ahc
);
3173 ahc_outb(ahc
, MSG_OUT
, HOST_MSG
);
3175 ahc_print_devinfo(ahc
, &devinfo
);
3176 ahc
->msgout_buf
[0] = MSG_ABORT_TASK
;
3177 ahc
->msgout_len
= 1;
3178 ahc
->msgout_index
= 0;
3179 ahc
->msg_type
= MSG_TYPE_INITIATOR_MSGOUT
;
3181 ahc_print_path(ahc
, scb
);
3182 scb
->flags
|= SCB_ABORT
;
3184 printk("Protocol violation %s. Attempting to abort.\n",
3185 ahc_lookup_phase_entry(curphase
)->phasemsg
);
3190 * Manual message loop handler.
3193 ahc_handle_message_phase(struct ahc_softc
*ahc
)
3195 struct ahc_devinfo devinfo
;
3199 ahc_fetch_devinfo(ahc
, &devinfo
);
3200 end_session
= FALSE
;
3201 bus_phase
= ahc_inb(ahc
, SCSISIGI
) & PHASE_MASK
;
3204 switch (ahc
->msg_type
) {
3205 case MSG_TYPE_INITIATOR_MSGOUT
:
3211 if (ahc
->msgout_len
== 0)
3212 panic("HOST_MSG_LOOP interrupt with no active message");
3215 if ((ahc_debug
& AHC_SHOW_MESSAGES
) != 0) {
3216 ahc_print_devinfo(ahc
, &devinfo
);
3217 printk("INITIATOR_MSG_OUT");
3220 phasemis
= bus_phase
!= P_MESGOUT
;
3223 if ((ahc_debug
& AHC_SHOW_MESSAGES
) != 0) {
3224 printk(" PHASEMIS %s\n",
3225 ahc_lookup_phase_entry(bus_phase
)
3229 if (bus_phase
== P_MESGIN
) {
3231 * Change gears and see if
3232 * this messages is of interest to
3233 * us or should be passed back to
3236 ahc_outb(ahc
, CLRSINT1
, CLRATNO
);
3237 ahc
->send_msg_perror
= FALSE
;
3238 ahc
->msg_type
= MSG_TYPE_INITIATOR_MSGIN
;
3239 ahc
->msgin_index
= 0;
3246 if (ahc
->send_msg_perror
) {
3247 ahc_outb(ahc
, CLRSINT1
, CLRATNO
);
3248 ahc_outb(ahc
, CLRSINT1
, CLRREQINIT
);
3250 if ((ahc_debug
& AHC_SHOW_MESSAGES
) != 0)
3251 printk(" byte 0x%x\n", ahc
->send_msg_perror
);
3253 ahc_outb(ahc
, SCSIDATL
, MSG_PARITY_ERROR
);
3257 msgdone
= ahc
->msgout_index
== ahc
->msgout_len
;
3260 * The target has requested a retry.
3261 * Re-assert ATN, reset our message index to
3264 ahc
->msgout_index
= 0;
3265 ahc_assert_atn(ahc
);
3268 lastbyte
= ahc
->msgout_index
== (ahc
->msgout_len
- 1);
3270 /* Last byte is signified by dropping ATN */
3271 ahc_outb(ahc
, CLRSINT1
, CLRATNO
);
3275 * Clear our interrupt status and present
3276 * the next byte on the bus.
3278 ahc_outb(ahc
, CLRSINT1
, CLRREQINIT
);
3280 if ((ahc_debug
& AHC_SHOW_MESSAGES
) != 0)
3281 printk(" byte 0x%x\n",
3282 ahc
->msgout_buf
[ahc
->msgout_index
]);
3284 ahc_outb(ahc
, SCSIDATL
, ahc
->msgout_buf
[ahc
->msgout_index
++]);
3287 case MSG_TYPE_INITIATOR_MSGIN
:
3293 if ((ahc_debug
& AHC_SHOW_MESSAGES
) != 0) {
3294 ahc_print_devinfo(ahc
, &devinfo
);
3295 printk("INITIATOR_MSG_IN");
3298 phasemis
= bus_phase
!= P_MESGIN
;
3301 if ((ahc_debug
& AHC_SHOW_MESSAGES
) != 0) {
3302 printk(" PHASEMIS %s\n",
3303 ahc_lookup_phase_entry(bus_phase
)
3307 ahc
->msgin_index
= 0;
3308 if (bus_phase
== P_MESGOUT
3309 && (ahc
->send_msg_perror
== TRUE
3310 || (ahc
->msgout_len
!= 0
3311 && ahc
->msgout_index
== 0))) {
3312 ahc
->msg_type
= MSG_TYPE_INITIATOR_MSGOUT
;
3319 /* Pull the byte in without acking it */
3320 ahc
->msgin_buf
[ahc
->msgin_index
] = ahc_inb(ahc
, SCSIBUSL
);
3322 if ((ahc_debug
& AHC_SHOW_MESSAGES
) != 0)
3323 printk(" byte 0x%x\n",
3324 ahc
->msgin_buf
[ahc
->msgin_index
]);
3327 message_done
= ahc_parse_msg(ahc
, &devinfo
);
3331 * Clear our incoming message buffer in case there
3332 * is another message following this one.
3334 ahc
->msgin_index
= 0;
3337 * If this message illicited a response,
3338 * assert ATN so the target takes us to the
3339 * message out phase.
3341 if (ahc
->msgout_len
!= 0) {
3343 if ((ahc_debug
& AHC_SHOW_MESSAGES
) != 0) {
3344 ahc_print_devinfo(ahc
, &devinfo
);
3345 printk("Asserting ATN for response\n");
3348 ahc_assert_atn(ahc
);
3353 if (message_done
== MSGLOOP_TERMINATED
) {
3357 ahc_outb(ahc
, CLRSINT1
, CLRREQINIT
);
3358 ahc_inb(ahc
, SCSIDATL
);
3362 case MSG_TYPE_TARGET_MSGIN
:
3367 if (ahc
->msgout_len
== 0)
3368 panic("Target MSGIN with no active message");
3371 * If we interrupted a mesgout session, the initiator
3372 * will not know this until our first REQ. So, we
3373 * only honor mesgout requests after we've sent our
3376 if ((ahc_inb(ahc
, SCSISIGI
) & ATNI
) != 0
3377 && ahc
->msgout_index
> 0)
3378 msgout_request
= TRUE
;
3380 msgout_request
= FALSE
;
3382 if (msgout_request
) {
3385 * Change gears and see if
3386 * this messages is of interest to
3387 * us or should be passed back to
3390 ahc
->msg_type
= MSG_TYPE_TARGET_MSGOUT
;
3391 ahc_outb(ahc
, SCSISIGO
, P_MESGOUT
| BSYO
);
3392 ahc
->msgin_index
= 0;
3393 /* Dummy read to REQ for first byte */
3394 ahc_inb(ahc
, SCSIDATL
);
3395 ahc_outb(ahc
, SXFRCTL0
,
3396 ahc_inb(ahc
, SXFRCTL0
) | SPIOEN
);
3400 msgdone
= ahc
->msgout_index
== ahc
->msgout_len
;
3402 ahc_outb(ahc
, SXFRCTL0
,
3403 ahc_inb(ahc
, SXFRCTL0
) & ~SPIOEN
);
3409 * Present the next byte on the bus.
3411 ahc_outb(ahc
, SXFRCTL0
, ahc_inb(ahc
, SXFRCTL0
) | SPIOEN
);
3412 ahc_outb(ahc
, SCSIDATL
, ahc
->msgout_buf
[ahc
->msgout_index
++]);
3415 case MSG_TYPE_TARGET_MSGOUT
:
3421 * The initiator signals that this is
3422 * the last byte by dropping ATN.
3424 lastbyte
= (ahc_inb(ahc
, SCSISIGI
) & ATNI
) == 0;
3427 * Read the latched byte, but turn off SPIOEN first
3428 * so that we don't inadvertently cause a REQ for the
3431 ahc_outb(ahc
, SXFRCTL0
, ahc_inb(ahc
, SXFRCTL0
) & ~SPIOEN
);
3432 ahc
->msgin_buf
[ahc
->msgin_index
] = ahc_inb(ahc
, SCSIDATL
);
3433 msgdone
= ahc_parse_msg(ahc
, &devinfo
);
3434 if (msgdone
== MSGLOOP_TERMINATED
) {
3436 * The message is *really* done in that it caused
3437 * us to go to bus free. The sequencer has already
3438 * been reset at this point, so pull the ejection
3446 if (msgdone
== MSGLOOP_MSGCOMPLETE
) {
3447 ahc
->msgin_index
= 0;
3450 * If this message illicited a response, transition
3451 * to the Message in phase and send it.
3453 if (ahc
->msgout_len
!= 0) {
3454 ahc_outb(ahc
, SCSISIGO
, P_MESGIN
| BSYO
);
3455 ahc_outb(ahc
, SXFRCTL0
,
3456 ahc_inb(ahc
, SXFRCTL0
) | SPIOEN
);
3457 ahc
->msg_type
= MSG_TYPE_TARGET_MSGIN
;
3458 ahc
->msgin_index
= 0;
3466 /* Ask for the next byte. */
3467 ahc_outb(ahc
, SXFRCTL0
,
3468 ahc_inb(ahc
, SXFRCTL0
) | SPIOEN
);
3474 panic("Unknown REQINIT message type");
3478 ahc_clear_msg_state(ahc
);
3479 ahc_outb(ahc
, RETURN_1
, EXIT_MSG_LOOP
);
3481 ahc_outb(ahc
, RETURN_1
, CONT_MSG_LOOP
);
3485 * See if we sent a particular extended message to the target.
3486 * If "full" is true, return true only if the target saw the full
3487 * message. If "full" is false, return true if the target saw at
3488 * least the first byte of the message.
3491 ahc_sent_msg(struct ahc_softc
*ahc
, ahc_msgtype type
, u_int msgval
, int full
)
3499 while (index
< ahc
->msgout_len
) {
3500 if (ahc
->msgout_buf
[index
] == MSG_EXTENDED
) {
3503 end_index
= index
+ 1 + ahc
->msgout_buf
[index
+ 1];
3504 if (ahc
->msgout_buf
[index
+2] == msgval
3505 && type
== AHCMSG_EXT
) {
3508 if (ahc
->msgout_index
> end_index
)
3510 } else if (ahc
->msgout_index
> index
)
3514 } else if (ahc
->msgout_buf
[index
] >= MSG_SIMPLE_TASK
3515 && ahc
->msgout_buf
[index
] <= MSG_IGN_WIDE_RESIDUE
) {
3517 /* Skip tag type and tag id or residue param*/
3520 /* Single byte message */
3521 if (type
== AHCMSG_1B
3522 && ahc
->msgout_buf
[index
] == msgval
3523 && ahc
->msgout_index
> index
)
3535 * Wait for a complete incoming message, parse it, and respond accordingly.
3538 ahc_parse_msg(struct ahc_softc
*ahc
, struct ahc_devinfo
*devinfo
)
3540 struct ahc_initiator_tinfo
*tinfo
;
3541 struct ahc_tmode_tstate
*tstate
;
3545 u_int targ_scsirate
;
3547 done
= MSGLOOP_IN_PROG
;
3550 tinfo
= ahc_fetch_transinfo(ahc
, devinfo
->channel
, devinfo
->our_scsiid
,
3551 devinfo
->target
, &tstate
);
3552 targ_scsirate
= tinfo
->scsirate
;
3555 * Parse as much of the message as is available,
3556 * rejecting it if we don't support it. When
3557 * the entire message is available and has been
3558 * handled, return MSGLOOP_MSGCOMPLETE, indicating
3559 * that we have parsed an entire message.
3561 * In the case of extended messages, we accept the length
3562 * byte outright and perform more checking once we know the
3563 * extended message type.
3565 switch (ahc
->msgin_buf
[0]) {
3566 case MSG_DISCONNECT
:
3567 case MSG_SAVEDATAPOINTER
:
3568 case MSG_CMDCOMPLETE
:
3569 case MSG_RESTOREPOINTERS
:
3570 case MSG_IGN_WIDE_RESIDUE
:
3572 * End our message loop as these are messages
3573 * the sequencer handles on its own.
3575 done
= MSGLOOP_TERMINATED
;
3577 case MSG_MESSAGE_REJECT
:
3578 response
= ahc_handle_msg_reject(ahc
, devinfo
);
3581 done
= MSGLOOP_MSGCOMPLETE
;
3585 /* Wait for enough of the message to begin validation */
3586 if (ahc
->msgin_index
< 2)
3588 switch (ahc
->msgin_buf
[2]) {
3591 const struct ahc_syncrate
*syncrate
;
3597 if (ahc
->msgin_buf
[1] != MSG_EXT_SDTR_LEN
) {
3603 * Wait until we have both args before validating
3604 * and acting on this message.
3606 * Add one to MSG_EXT_SDTR_LEN to account for
3607 * the extended message preamble.
3609 if (ahc
->msgin_index
< (MSG_EXT_SDTR_LEN
+ 1))
3612 period
= ahc
->msgin_buf
[3];
3614 saved_offset
= offset
= ahc
->msgin_buf
[4];
3615 syncrate
= ahc_devlimited_syncrate(ahc
, tinfo
, &period
,
3618 ahc_validate_offset(ahc
, tinfo
, syncrate
, &offset
,
3619 targ_scsirate
& WIDEXFER
,
3622 printk("(%s:%c:%d:%d): Received "
3623 "SDTR period %x, offset %x\n\t"
3624 "Filtered to period %x, offset %x\n",
3625 ahc_name(ahc
), devinfo
->channel
,
3626 devinfo
->target
, devinfo
->lun
,
3627 ahc
->msgin_buf
[3], saved_offset
,
3630 ahc_set_syncrate(ahc
, devinfo
,
3632 offset
, ppr_options
,
3633 AHC_TRANS_ACTIVE
|AHC_TRANS_GOAL
,
3637 * See if we initiated Sync Negotiation
3638 * and didn't have to fall down to async
3641 if (ahc_sent_msg(ahc
, AHCMSG_EXT
, MSG_EXT_SDTR
, TRUE
)) {
3643 if (saved_offset
!= offset
) {
3644 /* Went too low - force async */
3649 * Send our own SDTR in reply
3652 && devinfo
->role
== ROLE_INITIATOR
) {
3653 printk("(%s:%c:%d:%d): Target "
3655 ahc_name(ahc
), devinfo
->channel
,
3656 devinfo
->target
, devinfo
->lun
);
3658 ahc
->msgout_index
= 0;
3659 ahc
->msgout_len
= 0;
3660 ahc_construct_sdtr(ahc
, devinfo
,
3662 ahc
->msgout_index
= 0;
3665 done
= MSGLOOP_MSGCOMPLETE
;
3672 u_int sending_reply
;
3674 sending_reply
= FALSE
;
3675 if (ahc
->msgin_buf
[1] != MSG_EXT_WDTR_LEN
) {
3681 * Wait until we have our arg before validating
3682 * and acting on this message.
3684 * Add one to MSG_EXT_WDTR_LEN to account for
3685 * the extended message preamble.
3687 if (ahc
->msgin_index
< (MSG_EXT_WDTR_LEN
+ 1))
3690 bus_width
= ahc
->msgin_buf
[3];
3691 saved_width
= bus_width
;
3692 ahc_validate_width(ahc
, tinfo
, &bus_width
,
3695 printk("(%s:%c:%d:%d): Received WDTR "
3696 "%x filtered to %x\n",
3697 ahc_name(ahc
), devinfo
->channel
,
3698 devinfo
->target
, devinfo
->lun
,
3699 saved_width
, bus_width
);
3702 if (ahc_sent_msg(ahc
, AHCMSG_EXT
, MSG_EXT_WDTR
, TRUE
)) {
3704 * Don't send a WDTR back to the
3705 * target, since we asked first.
3706 * If the width went higher than our
3707 * request, reject it.
3709 if (saved_width
> bus_width
) {
3711 printk("(%s:%c:%d:%d): requested %dBit "
3712 "transfers. Rejecting...\n",
3713 ahc_name(ahc
), devinfo
->channel
,
3714 devinfo
->target
, devinfo
->lun
,
3715 8 * (0x01 << bus_width
));
3720 * Send our own WDTR in reply
3723 && devinfo
->role
== ROLE_INITIATOR
) {
3724 printk("(%s:%c:%d:%d): Target "
3726 ahc_name(ahc
), devinfo
->channel
,
3727 devinfo
->target
, devinfo
->lun
);
3729 ahc
->msgout_index
= 0;
3730 ahc
->msgout_len
= 0;
3731 ahc_construct_wdtr(ahc
, devinfo
, bus_width
);
3732 ahc
->msgout_index
= 0;
3734 sending_reply
= TRUE
;
3737 * After a wide message, we are async, but
3738 * some devices don't seem to honor this portion
3739 * of the spec. Force a renegotiation of the
3740 * sync component of our transfer agreement even
3741 * if our goal is async. By updating our width
3742 * after forcing the negotiation, we avoid
3743 * renegotiating for width.
3745 ahc_update_neg_request(ahc
, devinfo
, tstate
,
3746 tinfo
, AHC_NEG_ALWAYS
);
3747 ahc_set_width(ahc
, devinfo
, bus_width
,
3748 AHC_TRANS_ACTIVE
|AHC_TRANS_GOAL
,
3750 if (sending_reply
== FALSE
&& reject
== FALSE
) {
3753 * We will always have an SDTR to send.
3755 ahc
->msgout_index
= 0;
3756 ahc
->msgout_len
= 0;
3757 ahc_build_transfer_msg(ahc
, devinfo
);
3758 ahc
->msgout_index
= 0;
3761 done
= MSGLOOP_MSGCOMPLETE
;
3766 const struct ahc_syncrate
*syncrate
;
3773 u_int saved_ppr_options
;
3775 if (ahc
->msgin_buf
[1] != MSG_EXT_PPR_LEN
) {
3781 * Wait until we have all args before validating
3782 * and acting on this message.
3784 * Add one to MSG_EXT_PPR_LEN to account for
3785 * the extended message preamble.
3787 if (ahc
->msgin_index
< (MSG_EXT_PPR_LEN
+ 1))
3790 period
= ahc
->msgin_buf
[3];
3791 offset
= ahc
->msgin_buf
[5];
3792 bus_width
= ahc
->msgin_buf
[6];
3793 saved_width
= bus_width
;
3794 ppr_options
= ahc
->msgin_buf
[7];
3796 * According to the spec, a DT only
3797 * period factor with no DT option
3798 * set implies async.
3800 if ((ppr_options
& MSG_EXT_PPR_DT_REQ
) == 0
3803 saved_ppr_options
= ppr_options
;
3804 saved_offset
= offset
;
3807 * Mask out any options we don't support
3808 * on any controller. Transfer options are
3809 * only available if we are negotiating wide.
3811 ppr_options
&= MSG_EXT_PPR_DT_REQ
;
3815 ahc_validate_width(ahc
, tinfo
, &bus_width
,
3817 syncrate
= ahc_devlimited_syncrate(ahc
, tinfo
, &period
,
3820 ahc_validate_offset(ahc
, tinfo
, syncrate
,
3824 if (ahc_sent_msg(ahc
, AHCMSG_EXT
, MSG_EXT_PPR
, TRUE
)) {
3826 * If we are unable to do any of the
3827 * requested options (we went too low),
3828 * then we'll have to reject the message.
3830 if (saved_width
> bus_width
3831 || saved_offset
!= offset
3832 || saved_ppr_options
!= ppr_options
) {
3841 if (devinfo
->role
!= ROLE_TARGET
)
3842 printk("(%s:%c:%d:%d): Target "
3844 ahc_name(ahc
), devinfo
->channel
,
3845 devinfo
->target
, devinfo
->lun
);
3847 printk("(%s:%c:%d:%d): Initiator "
3849 ahc_name(ahc
), devinfo
->channel
,
3850 devinfo
->target
, devinfo
->lun
);
3851 ahc
->msgout_index
= 0;
3852 ahc
->msgout_len
= 0;
3853 ahc_construct_ppr(ahc
, devinfo
, period
, offset
,
3854 bus_width
, ppr_options
);
3855 ahc
->msgout_index
= 0;
3859 printk("(%s:%c:%d:%d): Received PPR width %x, "
3860 "period %x, offset %x,options %x\n"
3861 "\tFiltered to width %x, period %x, "
3862 "offset %x, options %x\n",
3863 ahc_name(ahc
), devinfo
->channel
,
3864 devinfo
->target
, devinfo
->lun
,
3865 saved_width
, ahc
->msgin_buf
[3],
3866 saved_offset
, saved_ppr_options
,
3867 bus_width
, period
, offset
, ppr_options
);
3869 ahc_set_width(ahc
, devinfo
, bus_width
,
3870 AHC_TRANS_ACTIVE
|AHC_TRANS_GOAL
,
3872 ahc_set_syncrate(ahc
, devinfo
,
3874 offset
, ppr_options
,
3875 AHC_TRANS_ACTIVE
|AHC_TRANS_GOAL
,
3877 done
= MSGLOOP_MSGCOMPLETE
;
3881 /* Unknown extended message. Reject it. */
3887 #ifdef AHC_TARGET_MODE
3888 case MSG_BUS_DEV_RESET
:
3889 ahc_handle_devreset(ahc
, devinfo
,
3891 "Bus Device Reset Received",
3892 /*verbose_level*/0);
3894 done
= MSGLOOP_TERMINATED
;
3898 case MSG_CLEAR_QUEUE
:
3902 /* Target mode messages */
3903 if (devinfo
->role
!= ROLE_TARGET
) {
3907 tag
= SCB_LIST_NULL
;
3908 if (ahc
->msgin_buf
[0] == MSG_ABORT_TAG
)
3909 tag
= ahc_inb(ahc
, INITIATOR_TAG
);
3910 ahc_abort_scbs(ahc
, devinfo
->target
, devinfo
->channel
,
3911 devinfo
->lun
, tag
, ROLE_TARGET
,
3914 tstate
= ahc
->enabled_targets
[devinfo
->our_scsiid
];
3915 if (tstate
!= NULL
) {
3916 struct ahc_tmode_lstate
* lstate
;
3918 lstate
= tstate
->enabled_luns
[devinfo
->lun
];
3919 if (lstate
!= NULL
) {
3920 ahc_queue_lstate_event(ahc
, lstate
,
3921 devinfo
->our_scsiid
,
3924 ahc_send_lstate_events(ahc
, lstate
);
3928 done
= MSGLOOP_TERMINATED
;
3932 case MSG_TERM_IO_PROC
:
3940 * Setup to reject the message.
3942 ahc
->msgout_index
= 0;
3943 ahc
->msgout_len
= 1;
3944 ahc
->msgout_buf
[0] = MSG_MESSAGE_REJECT
;
3945 done
= MSGLOOP_MSGCOMPLETE
;
3949 if (done
!= MSGLOOP_IN_PROG
&& !response
)
3950 /* Clear the outgoing message buffer */
3951 ahc
->msgout_len
= 0;
3957 * Process a message reject message.
3960 ahc_handle_msg_reject(struct ahc_softc
*ahc
, struct ahc_devinfo
*devinfo
)
3963 * What we care about here is if we had an
3964 * outstanding SDTR or WDTR message for this
3965 * target. If we did, this is a signal that
3966 * the target is refusing negotiation.
3969 struct ahc_initiator_tinfo
*tinfo
;
3970 struct ahc_tmode_tstate
*tstate
;
3975 scb_index
= ahc_inb(ahc
, SCB_TAG
);
3976 scb
= ahc_lookup_scb(ahc
, scb_index
);
3977 tinfo
= ahc_fetch_transinfo(ahc
, devinfo
->channel
,
3978 devinfo
->our_scsiid
,
3979 devinfo
->target
, &tstate
);
3980 /* Might be necessary */
3981 last_msg
= ahc_inb(ahc
, LAST_MSG
);
3983 if (ahc_sent_msg(ahc
, AHCMSG_EXT
, MSG_EXT_PPR
, /*full*/FALSE
)) {
3985 * Target does not support the PPR message.
3986 * Attempt to negotiate SPI-2 style.
3989 printk("(%s:%c:%d:%d): PPR Rejected. "
3990 "Trying WDTR/SDTR\n",
3991 ahc_name(ahc
), devinfo
->channel
,
3992 devinfo
->target
, devinfo
->lun
);
3994 tinfo
->goal
.ppr_options
= 0;
3995 tinfo
->curr
.transport_version
= 2;
3996 tinfo
->goal
.transport_version
= 2;
3997 ahc
->msgout_index
= 0;
3998 ahc
->msgout_len
= 0;
3999 ahc_build_transfer_msg(ahc
, devinfo
);
4000 ahc
->msgout_index
= 0;
4002 } else if (ahc_sent_msg(ahc
, AHCMSG_EXT
, MSG_EXT_WDTR
, /*full*/FALSE
)) {
4004 /* note 8bit xfers */
4005 printk("(%s:%c:%d:%d): refuses WIDE negotiation. Using "
4006 "8bit transfers\n", ahc_name(ahc
),
4007 devinfo
->channel
, devinfo
->target
, devinfo
->lun
);
4008 ahc_set_width(ahc
, devinfo
, MSG_EXT_WDTR_BUS_8_BIT
,
4009 AHC_TRANS_ACTIVE
|AHC_TRANS_GOAL
,
4012 * No need to clear the sync rate. If the target
4013 * did not accept the command, our syncrate is
4014 * unaffected. If the target started the negotiation,
4015 * but rejected our response, we already cleared the
4016 * sync rate before sending our WDTR.
4018 if (tinfo
->goal
.offset
!= tinfo
->curr
.offset
) {
4020 /* Start the sync negotiation */
4021 ahc
->msgout_index
= 0;
4022 ahc
->msgout_len
= 0;
4023 ahc_build_transfer_msg(ahc
, devinfo
);
4024 ahc
->msgout_index
= 0;
4027 } else if (ahc_sent_msg(ahc
, AHCMSG_EXT
, MSG_EXT_SDTR
, /*full*/FALSE
)) {
4028 /* note asynch xfers and clear flag */
4029 ahc_set_syncrate(ahc
, devinfo
, /*syncrate*/NULL
, /*period*/0,
4030 /*offset*/0, /*ppr_options*/0,
4031 AHC_TRANS_ACTIVE
|AHC_TRANS_GOAL
,
4033 printk("(%s:%c:%d:%d): refuses synchronous negotiation. "
4034 "Using asynchronous transfers\n",
4035 ahc_name(ahc
), devinfo
->channel
,
4036 devinfo
->target
, devinfo
->lun
);
4037 } else if ((scb
->hscb
->control
& MSG_SIMPLE_TASK
) != 0) {
4041 tag_type
= (scb
->hscb
->control
& MSG_SIMPLE_TASK
);
4043 if (tag_type
== MSG_SIMPLE_TASK
) {
4044 printk("(%s:%c:%d:%d): refuses tagged commands. "
4045 "Performing non-tagged I/O\n", ahc_name(ahc
),
4046 devinfo
->channel
, devinfo
->target
, devinfo
->lun
);
4047 ahc_set_tags(ahc
, scb
->io_ctx
, devinfo
, AHC_QUEUE_NONE
);
4050 printk("(%s:%c:%d:%d): refuses %s tagged commands. "
4051 "Performing simple queue tagged I/O only\n",
4052 ahc_name(ahc
), devinfo
->channel
, devinfo
->target
,
4053 devinfo
->lun
, tag_type
== MSG_ORDERED_TASK
4054 ? "ordered" : "head of queue");
4055 ahc_set_tags(ahc
, scb
->io_ctx
, devinfo
, AHC_QUEUE_BASIC
);
4060 * Resend the identify for this CCB as the target
4061 * may believe that the selection is invalid otherwise.
4063 ahc_outb(ahc
, SCB_CONTROL
,
4064 ahc_inb(ahc
, SCB_CONTROL
) & mask
);
4065 scb
->hscb
->control
&= mask
;
4066 ahc_set_transaction_tag(scb
, /*enabled*/FALSE
,
4067 /*type*/MSG_SIMPLE_TASK
);
4068 ahc_outb(ahc
, MSG_OUT
, MSG_IDENTIFYFLAG
);
4069 ahc_assert_atn(ahc
);
4072 * This transaction is now at the head of
4073 * the untagged queue for this target.
4075 if ((ahc
->flags
& AHC_SCB_BTT
) == 0) {
4076 struct scb_tailq
*untagged_q
;
4079 &(ahc
->untagged_queues
[devinfo
->target_offset
]);
4080 TAILQ_INSERT_HEAD(untagged_q
, scb
, links
.tqe
);
4081 scb
->flags
|= SCB_UNTAGGEDQ
;
4083 ahc_busy_tcl(ahc
, BUILD_TCL(scb
->hscb
->scsiid
, devinfo
->lun
),
4087 * Requeue all tagged commands for this target
4088 * currently in our posession so they can be
4089 * converted to untagged commands.
4091 ahc_search_qinfifo(ahc
, SCB_GET_TARGET(ahc
, scb
),
4092 SCB_GET_CHANNEL(ahc
, scb
),
4093 SCB_GET_LUN(scb
), /*tag*/SCB_LIST_NULL
,
4094 ROLE_INITIATOR
, CAM_REQUEUE_REQ
,
4098 * Otherwise, we ignore it.
4100 printk("%s:%c:%d: Message reject for %x -- ignored\n",
4101 ahc_name(ahc
), devinfo
->channel
, devinfo
->target
,
4108 * Process an ingnore wide residue message.
4111 ahc_handle_ign_wide_residue(struct ahc_softc
*ahc
, struct ahc_devinfo
*devinfo
)
4116 scb_index
= ahc_inb(ahc
, SCB_TAG
);
4117 scb
= ahc_lookup_scb(ahc
, scb_index
);
4118 if ((ahc_inb(ahc
, SEQ_FLAGS
) & DPHASE
) == 0
4119 || ahc_get_transfer_dir(scb
) != CAM_DIR_IN
) {
4121 * Ignore the message if we haven't
4122 * seen an appropriate data phase yet.
4126 * If the residual occurred on the last
4127 * transfer and the transfer request was
4128 * expected to end on an odd count, do
4129 * nothing. Otherwise, subtract a byte
4130 * and update the residual count accordingly.
4134 sgptr
= ahc_inb(ahc
, SCB_RESIDUAL_SGPTR
);
4135 if ((sgptr
& SG_LIST_NULL
) != 0
4136 && (ahc_inb(ahc
, SCB_LUN
) & SCB_XFERLEN_ODD
) != 0) {
4138 * If the residual occurred on the last
4139 * transfer and the transfer request was
4140 * expected to end on an odd count, do
4144 struct ahc_dma_seg
*sg
;
4149 /* Pull in all of the sgptr */
4150 sgptr
= ahc_inl(ahc
, SCB_RESIDUAL_SGPTR
);
4151 data_cnt
= ahc_inl(ahc
, SCB_RESIDUAL_DATACNT
);
4153 if ((sgptr
& SG_LIST_NULL
) != 0) {
4155 * The residual data count is not updated
4156 * for the command run to completion case.
4157 * Explicitly zero the count.
4159 data_cnt
&= ~AHC_SG_LEN_MASK
;
4162 data_addr
= ahc_inl(ahc
, SHADDR
);
4166 sgptr
&= SG_PTR_MASK
;
4168 sg
= ahc_sg_bus_to_virt(scb
, sgptr
);
4171 * The residual sg ptr points to the next S/G
4172 * to load so we must go back one.
4175 sglen
= ahc_le32toh(sg
->len
) & AHC_SG_LEN_MASK
;
4176 if (sg
!= scb
->sg_list
4177 && sglen
< (data_cnt
& AHC_SG_LEN_MASK
)) {
4180 sglen
= ahc_le32toh(sg
->len
);
4182 * Preserve High Address and SG_LIST bits
4183 * while setting the count to 1.
4185 data_cnt
= 1 | (sglen
& (~AHC_SG_LEN_MASK
));
4186 data_addr
= ahc_le32toh(sg
->addr
)
4187 + (sglen
& AHC_SG_LEN_MASK
) - 1;
4190 * Increment sg so it points to the
4194 sgptr
= ahc_sg_virt_to_bus(scb
, sg
);
4196 ahc_outl(ahc
, SCB_RESIDUAL_SGPTR
, sgptr
);
4197 ahc_outl(ahc
, SCB_RESIDUAL_DATACNT
, data_cnt
);
4199 * Toggle the "oddness" of the transfer length
4200 * to handle this mid-transfer ignore wide
4201 * residue. This ensures that the oddness is
4202 * correct for subsequent data transfers.
4204 ahc_outb(ahc
, SCB_LUN
,
4205 ahc_inb(ahc
, SCB_LUN
) ^ SCB_XFERLEN_ODD
);
4212 * Reinitialize the data pointers for the active transfer
4213 * based on its current residual.
4216 ahc_reinitialize_dataptrs(struct ahc_softc
*ahc
)
4219 struct ahc_dma_seg
*sg
;
4225 scb_index
= ahc_inb(ahc
, SCB_TAG
);
4226 scb
= ahc_lookup_scb(ahc
, scb_index
);
4227 sgptr
= (ahc_inb(ahc
, SCB_RESIDUAL_SGPTR
+ 3) << 24)
4228 | (ahc_inb(ahc
, SCB_RESIDUAL_SGPTR
+ 2) << 16)
4229 | (ahc_inb(ahc
, SCB_RESIDUAL_SGPTR
+ 1) << 8)
4230 | ahc_inb(ahc
, SCB_RESIDUAL_SGPTR
);
4232 sgptr
&= SG_PTR_MASK
;
4233 sg
= ahc_sg_bus_to_virt(scb
, sgptr
);
4235 /* The residual sg_ptr always points to the next sg */
4238 resid
= (ahc_inb(ahc
, SCB_RESIDUAL_DATACNT
+ 2) << 16)
4239 | (ahc_inb(ahc
, SCB_RESIDUAL_DATACNT
+ 1) << 8)
4240 | ahc_inb(ahc
, SCB_RESIDUAL_DATACNT
);
4242 dataptr
= ahc_le32toh(sg
->addr
)
4243 + (ahc_le32toh(sg
->len
) & AHC_SG_LEN_MASK
)
4245 if ((ahc
->flags
& AHC_39BIT_ADDRESSING
) != 0) {
4248 dscommand1
= ahc_inb(ahc
, DSCOMMAND1
);
4249 ahc_outb(ahc
, DSCOMMAND1
, dscommand1
| HADDLDSEL0
);
4250 ahc_outb(ahc
, HADDR
,
4251 (ahc_le32toh(sg
->len
) >> 24) & SG_HIGH_ADDR_BITS
);
4252 ahc_outb(ahc
, DSCOMMAND1
, dscommand1
);
4254 ahc_outb(ahc
, HADDR
+ 3, dataptr
>> 24);
4255 ahc_outb(ahc
, HADDR
+ 2, dataptr
>> 16);
4256 ahc_outb(ahc
, HADDR
+ 1, dataptr
>> 8);
4257 ahc_outb(ahc
, HADDR
, dataptr
);
4258 ahc_outb(ahc
, HCNT
+ 2, resid
>> 16);
4259 ahc_outb(ahc
, HCNT
+ 1, resid
>> 8);
4260 ahc_outb(ahc
, HCNT
, resid
);
4261 if ((ahc
->features
& AHC_ULTRA2
) == 0) {
4262 ahc_outb(ahc
, STCNT
+ 2, resid
>> 16);
4263 ahc_outb(ahc
, STCNT
+ 1, resid
>> 8);
4264 ahc_outb(ahc
, STCNT
, resid
);
4269 * Handle the effects of issuing a bus device reset message.
4272 ahc_handle_devreset(struct ahc_softc
*ahc
, struct ahc_devinfo
*devinfo
,
4273 cam_status status
, char *message
, int verbose_level
)
4275 #ifdef AHC_TARGET_MODE
4276 struct ahc_tmode_tstate
* tstate
;
4281 found
= ahc_abort_scbs(ahc
, devinfo
->target
, devinfo
->channel
,
4282 CAM_LUN_WILDCARD
, SCB_LIST_NULL
, devinfo
->role
,
4285 #ifdef AHC_TARGET_MODE
4287 * Send an immediate notify ccb to all target mord peripheral
4288 * drivers affected by this action.
4290 tstate
= ahc
->enabled_targets
[devinfo
->our_scsiid
];
4291 if (tstate
!= NULL
) {
4292 for (lun
= 0; lun
< AHC_NUM_LUNS
; lun
++) {
4293 struct ahc_tmode_lstate
* lstate
;
4295 lstate
= tstate
->enabled_luns
[lun
];
4299 ahc_queue_lstate_event(ahc
, lstate
, devinfo
->our_scsiid
,
4300 MSG_BUS_DEV_RESET
, /*arg*/0);
4301 ahc_send_lstate_events(ahc
, lstate
);
4307 * Go back to async/narrow transfers and renegotiate.
4309 ahc_set_width(ahc
, devinfo
, MSG_EXT_WDTR_BUS_8_BIT
,
4310 AHC_TRANS_CUR
, /*paused*/TRUE
);
4311 ahc_set_syncrate(ahc
, devinfo
, /*syncrate*/NULL
,
4312 /*period*/0, /*offset*/0, /*ppr_options*/0,
4313 AHC_TRANS_CUR
, /*paused*/TRUE
);
4315 if (status
!= CAM_SEL_TIMEOUT
)
4316 ahc_send_async(ahc
, devinfo
->channel
, devinfo
->target
,
4317 CAM_LUN_WILDCARD
, AC_SENT_BDR
);
4320 && (verbose_level
<= bootverbose
))
4321 printk("%s: %s on %c:%d. %d SCBs aborted\n", ahc_name(ahc
),
4322 message
, devinfo
->channel
, devinfo
->target
, found
);
4325 #ifdef AHC_TARGET_MODE
4327 ahc_setup_target_msgin(struct ahc_softc
*ahc
, struct ahc_devinfo
*devinfo
,
4332 * To facilitate adding multiple messages together,
4333 * each routine should increment the index and len
4334 * variables instead of setting them explicitly.
4336 ahc
->msgout_index
= 0;
4337 ahc
->msgout_len
= 0;
4339 if (scb
!= NULL
&& (scb
->flags
& SCB_AUTO_NEGOTIATE
) != 0)
4340 ahc_build_transfer_msg(ahc
, devinfo
);
4342 panic("ahc_intr: AWAITING target message with no message");
4344 ahc
->msgout_index
= 0;
4345 ahc
->msg_type
= MSG_TYPE_TARGET_MSGIN
;
4348 /**************************** Initialization **********************************/
4350 * Allocate a controller structure for a new device
4351 * and perform initial initializion.
4354 ahc_alloc(void *platform_arg
, char *name
)
4356 struct ahc_softc
*ahc
;
4360 ahc
= kmalloc(sizeof(*ahc
), GFP_ATOMIC
);
4362 printk("aic7xxx: cannot malloc softc!\n");
4367 ahc
= device_get_softc((device_t
)platform_arg
);
4369 memset(ahc
, 0, sizeof(*ahc
));
4370 ahc
->seep_config
= kmalloc(sizeof(*ahc
->seep_config
), GFP_ATOMIC
);
4371 if (ahc
->seep_config
== NULL
) {
4378 LIST_INIT(&ahc
->pending_scbs
);
4379 /* We don't know our unit number until the OSM sets it */
4382 ahc
->description
= NULL
;
4384 ahc
->channel_b
= 'B';
4385 ahc
->chip
= AHC_NONE
;
4386 ahc
->features
= AHC_FENONE
;
4387 ahc
->bugs
= AHC_BUGNONE
;
4388 ahc
->flags
= AHC_FNONE
;
4390 * Default to all error reporting enabled with the
4391 * sequencer operating at its fastest speed.
4392 * The bus attach code may modify this.
4394 ahc
->seqctl
= FASTMODE
;
4396 for (i
= 0; i
< AHC_NUM_TARGETS
; i
++)
4397 TAILQ_INIT(&ahc
->untagged_queues
[i
]);
4398 if (ahc_platform_alloc(ahc
, platform_arg
) != 0) {
4406 ahc_softc_init(struct ahc_softc
*ahc
)
4409 /* The IRQMS bit is only valid on VL and EISA chips */
4410 if ((ahc
->chip
& AHC_PCI
) == 0)
4411 ahc
->unpause
= ahc_inb(ahc
, HCNTRL
) & IRQMS
;
4414 ahc
->pause
= ahc
->unpause
| PAUSE
;
4415 if (ahc
->scb_data
== NULL
) {
4416 ahc
->scb_data
= kmalloc(sizeof(*ahc
->scb_data
), GFP_ATOMIC
);
4417 if (ahc
->scb_data
== NULL
)
4419 memset(ahc
->scb_data
, 0, sizeof(*ahc
->scb_data
));
4426 ahc_set_unit(struct ahc_softc
*ahc
, int unit
)
4432 ahc_set_name(struct ahc_softc
*ahc
, char *name
)
4434 if (ahc
->name
!= NULL
)
4440 ahc_free(struct ahc_softc
*ahc
)
4444 switch (ahc
->init_level
) {
4450 ahc_dmamap_unload(ahc
, ahc
->shared_data_dmat
,
4451 ahc
->shared_data_dmamap
);
4454 ahc_dmamem_free(ahc
, ahc
->shared_data_dmat
, ahc
->qoutfifo
,
4455 ahc
->shared_data_dmamap
);
4456 ahc_dmamap_destroy(ahc
, ahc
->shared_data_dmat
,
4457 ahc
->shared_data_dmamap
);
4460 ahc_dma_tag_destroy(ahc
, ahc
->shared_data_dmat
);
4463 ahc_dma_tag_destroy(ahc
, ahc
->buffer_dmat
);
4471 ahc_dma_tag_destroy(ahc
, ahc
->parent_dmat
);
4473 ahc_platform_free(ahc
);
4474 ahc_fini_scbdata(ahc
);
4475 for (i
= 0; i
< AHC_NUM_TARGETS
; i
++) {
4476 struct ahc_tmode_tstate
*tstate
;
4478 tstate
= ahc
->enabled_targets
[i
];
4479 if (tstate
!= NULL
) {
4480 #ifdef AHC_TARGET_MODE
4483 for (j
= 0; j
< AHC_NUM_LUNS
; j
++) {
4484 struct ahc_tmode_lstate
*lstate
;
4486 lstate
= tstate
->enabled_luns
[j
];
4487 if (lstate
!= NULL
) {
4488 xpt_free_path(lstate
->path
);
4496 #ifdef AHC_TARGET_MODE
4497 if (ahc
->black_hole
!= NULL
) {
4498 xpt_free_path(ahc
->black_hole
->path
);
4499 kfree(ahc
->black_hole
);
4502 if (ahc
->name
!= NULL
)
4504 if (ahc
->seep_config
!= NULL
)
4505 kfree(ahc
->seep_config
);
4513 ahc_shutdown(void *arg
)
4515 struct ahc_softc
*ahc
;
4518 ahc
= (struct ahc_softc
*)arg
;
4520 /* This will reset most registers to 0, but not all */
4521 ahc_reset(ahc
, /*reinit*/FALSE
);
4522 ahc_outb(ahc
, SCSISEQ
, 0);
4523 ahc_outb(ahc
, SXFRCTL0
, 0);
4524 ahc_outb(ahc
, DSPCISTATUS
, 0);
4526 for (i
= TARG_SCSIRATE
; i
< SCSICONF
; i
++)
4527 ahc_outb(ahc
, i
, 0);
4531 * Reset the controller and record some information about it
4532 * that is only available just after a reset. If "reinit" is
4533 * non-zero, this reset occured after initial configuration
4534 * and the caller requests that the chip be fully reinitialized
4535 * to a runable state. Chip interrupts are *not* enabled after
4536 * a reinitialization. The caller must enable interrupts via
4537 * ahc_intr_enable().
4540 ahc_reset(struct ahc_softc
*ahc
, int reinit
)
4543 u_int sxfrctl1_a
, sxfrctl1_b
;
4548 * Preserve the value of the SXFRCTL1 register for all channels.
4549 * It contains settings that affect termination and we don't want
4550 * to disturb the integrity of the bus.
4554 if ((ahc
->chip
& AHC_CHIPID_MASK
) == AHC_AIC7770
) {
4558 * Save channel B's settings in case this chip
4559 * is setup for TWIN channel operation.
4561 sblkctl
= ahc_inb(ahc
, SBLKCTL
);
4562 ahc_outb(ahc
, SBLKCTL
, sblkctl
| SELBUSB
);
4563 sxfrctl1_b
= ahc_inb(ahc
, SXFRCTL1
);
4564 ahc_outb(ahc
, SBLKCTL
, sblkctl
& ~SELBUSB
);
4566 sxfrctl1_a
= ahc_inb(ahc
, SXFRCTL1
);
4568 ahc_outb(ahc
, HCNTRL
, CHIPRST
| ahc
->pause
);
4571 * Ensure that the reset has finished. We delay 1000us
4572 * prior to reading the register to make sure the chip
4573 * has sufficiently completed its reset to handle register
4579 } while (--wait
&& !(ahc_inb(ahc
, HCNTRL
) & CHIPRSTACK
));
4582 printk("%s: WARNING - Failed chip reset! "
4583 "Trying to initialize anyway.\n", ahc_name(ahc
));
4585 ahc_outb(ahc
, HCNTRL
, ahc
->pause
);
4587 /* Determine channel configuration */
4588 sblkctl
= ahc_inb(ahc
, SBLKCTL
) & (SELBUSB
|SELWIDE
);
4589 /* No Twin Channel PCI cards */
4590 if ((ahc
->chip
& AHC_PCI
) != 0)
4591 sblkctl
&= ~SELBUSB
;
4594 /* Single Narrow Channel */
4598 ahc
->features
|= AHC_WIDE
;
4602 ahc
->features
|= AHC_TWIN
;
4605 printk(" Unsupported adapter type. Ignoring\n");
4612 * We must always initialize STPWEN to 1 before we
4613 * restore the saved values. STPWEN is initialized
4614 * to a tri-state condition which can only be cleared
4617 if ((ahc
->features
& AHC_TWIN
) != 0) {
4620 sblkctl
= ahc_inb(ahc
, SBLKCTL
);
4621 ahc_outb(ahc
, SBLKCTL
, sblkctl
| SELBUSB
);
4622 ahc_outb(ahc
, SXFRCTL1
, sxfrctl1_b
);
4623 ahc_outb(ahc
, SBLKCTL
, sblkctl
& ~SELBUSB
);
4625 ahc_outb(ahc
, SXFRCTL1
, sxfrctl1_a
);
4630 * If a recovery action has forced a chip reset,
4631 * re-initialize the chip to our liking.
4633 error
= ahc
->bus_chip_init(ahc
);
4643 * Determine the number of SCBs available on the controller
4646 ahc_probe_scbs(struct ahc_softc
*ahc
) {
4649 for (i
= 0; i
< AHC_SCB_MAX
; i
++) {
4651 ahc_outb(ahc
, SCBPTR
, i
);
4652 ahc_outb(ahc
, SCB_BASE
, i
);
4653 if (ahc_inb(ahc
, SCB_BASE
) != i
)
4655 ahc_outb(ahc
, SCBPTR
, 0);
4656 if (ahc_inb(ahc
, SCB_BASE
) != 0)
4663 ahc_dmamap_cb(void *arg
, bus_dma_segment_t
*segs
, int nseg
, int error
)
4667 baddr
= (dma_addr_t
*)arg
;
4668 *baddr
= segs
->ds_addr
;
4672 ahc_build_free_scb_list(struct ahc_softc
*ahc
)
4678 if ((ahc
->flags
& AHC_LSCBS_ENABLED
) != 0)
4681 for (i
= 0; i
< ahc
->scb_data
->maxhscbs
; i
++) {
4684 ahc_outb(ahc
, SCBPTR
, i
);
4687 * Touch all SCB bytes to avoid parity errors
4688 * should one of our debugging routines read
4689 * an otherwise uninitiatlized byte.
4691 for (j
= 0; j
< scbsize
; j
++)
4692 ahc_outb(ahc
, SCB_BASE
+j
, 0xFF);
4694 /* Clear the control byte. */
4695 ahc_outb(ahc
, SCB_CONTROL
, 0);
4697 /* Set the next pointer */
4698 if ((ahc
->flags
& AHC_PAGESCBS
) != 0)
4699 ahc_outb(ahc
, SCB_NEXT
, i
+1);
4701 ahc_outb(ahc
, SCB_NEXT
, SCB_LIST_NULL
);
4703 /* Make the tag number, SCSIID, and lun invalid */
4704 ahc_outb(ahc
, SCB_TAG
, SCB_LIST_NULL
);
4705 ahc_outb(ahc
, SCB_SCSIID
, 0xFF);
4706 ahc_outb(ahc
, SCB_LUN
, 0xFF);
4709 if ((ahc
->flags
& AHC_PAGESCBS
) != 0) {
4710 /* SCB 0 heads the free list. */
4711 ahc_outb(ahc
, FREE_SCBH
, 0);
4714 ahc_outb(ahc
, FREE_SCBH
, SCB_LIST_NULL
);
4717 /* Make sure that the last SCB terminates the free list */
4718 ahc_outb(ahc
, SCBPTR
, i
-1);
4719 ahc_outb(ahc
, SCB_NEXT
, SCB_LIST_NULL
);
4723 ahc_init_scbdata(struct ahc_softc
*ahc
)
4725 struct scb_data
*scb_data
;
4727 scb_data
= ahc
->scb_data
;
4728 SLIST_INIT(&scb_data
->free_scbs
);
4729 SLIST_INIT(&scb_data
->sg_maps
);
4731 /* Allocate SCB resources */
4732 scb_data
->scbarray
= (struct scb
*)kmalloc(sizeof(struct scb
) * AHC_SCB_MAX_ALLOC
, GFP_ATOMIC
);
4733 if (scb_data
->scbarray
== NULL
)
4735 memset(scb_data
->scbarray
, 0, sizeof(struct scb
) * AHC_SCB_MAX_ALLOC
);
4737 /* Determine the number of hardware SCBs and initialize them */
4739 scb_data
->maxhscbs
= ahc_probe_scbs(ahc
);
4740 if (ahc
->scb_data
->maxhscbs
== 0) {
4741 printk("%s: No SCB space found\n", ahc_name(ahc
));
4746 * Create our DMA tags. These tags define the kinds of device
4747 * accessible memory allocations and memory mappings we will
4748 * need to perform during normal operation.
4750 * Unless we need to further restrict the allocation, we rely
4751 * on the restrictions of the parent dmat, hence the common
4752 * use of MAXADDR and MAXSIZE.
4755 /* DMA tag for our hardware scb structures */
4756 if (ahc_dma_tag_create(ahc
, ahc
->parent_dmat
, /*alignment*/1,
4757 /*boundary*/BUS_SPACE_MAXADDR_32BIT
+ 1,
4758 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT
,
4759 /*highaddr*/BUS_SPACE_MAXADDR
,
4760 /*filter*/NULL
, /*filterarg*/NULL
,
4761 AHC_SCB_MAX_ALLOC
* sizeof(struct hardware_scb
),
4763 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT
,
4764 /*flags*/0, &scb_data
->hscb_dmat
) != 0) {
4768 scb_data
->init_level
++;
4770 /* Allocation for our hscbs */
4771 if (ahc_dmamem_alloc(ahc
, scb_data
->hscb_dmat
,
4772 (void **)&scb_data
->hscbs
,
4773 BUS_DMA_NOWAIT
, &scb_data
->hscb_dmamap
) != 0) {
4777 scb_data
->init_level
++;
4779 /* And permanently map them */
4780 ahc_dmamap_load(ahc
, scb_data
->hscb_dmat
, scb_data
->hscb_dmamap
,
4782 AHC_SCB_MAX_ALLOC
* sizeof(struct hardware_scb
),
4783 ahc_dmamap_cb
, &scb_data
->hscb_busaddr
, /*flags*/0);
4785 scb_data
->init_level
++;
4787 /* DMA tag for our sense buffers */
4788 if (ahc_dma_tag_create(ahc
, ahc
->parent_dmat
, /*alignment*/1,
4789 /*boundary*/BUS_SPACE_MAXADDR_32BIT
+ 1,
4790 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT
,
4791 /*highaddr*/BUS_SPACE_MAXADDR
,
4792 /*filter*/NULL
, /*filterarg*/NULL
,
4793 AHC_SCB_MAX_ALLOC
* sizeof(struct scsi_sense_data
),
4795 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT
,
4796 /*flags*/0, &scb_data
->sense_dmat
) != 0) {
4800 scb_data
->init_level
++;
4803 if (ahc_dmamem_alloc(ahc
, scb_data
->sense_dmat
,
4804 (void **)&scb_data
->sense
,
4805 BUS_DMA_NOWAIT
, &scb_data
->sense_dmamap
) != 0) {
4809 scb_data
->init_level
++;
4811 /* And permanently map them */
4812 ahc_dmamap_load(ahc
, scb_data
->sense_dmat
, scb_data
->sense_dmamap
,
4814 AHC_SCB_MAX_ALLOC
* sizeof(struct scsi_sense_data
),
4815 ahc_dmamap_cb
, &scb_data
->sense_busaddr
, /*flags*/0);
4817 scb_data
->init_level
++;
4819 /* DMA tag for our S/G structures. We allocate in page sized chunks */
4820 if (ahc_dma_tag_create(ahc
, ahc
->parent_dmat
, /*alignment*/8,
4821 /*boundary*/BUS_SPACE_MAXADDR_32BIT
+ 1,
4822 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT
,
4823 /*highaddr*/BUS_SPACE_MAXADDR
,
4824 /*filter*/NULL
, /*filterarg*/NULL
,
4825 PAGE_SIZE
, /*nsegments*/1,
4826 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT
,
4827 /*flags*/0, &scb_data
->sg_dmat
) != 0) {
4831 scb_data
->init_level
++;
4833 /* Perform initial CCB allocation */
4834 memset(scb_data
->hscbs
, 0,
4835 AHC_SCB_MAX_ALLOC
* sizeof(struct hardware_scb
));
4836 ahc_alloc_scbs(ahc
);
4838 if (scb_data
->numscbs
== 0) {
4839 printk("%s: ahc_init_scbdata - "
4840 "Unable to allocate initial scbs\n",
4846 * Reserve the next queued SCB.
4848 ahc
->next_queued_scb
= ahc_get_scb(ahc
);
4851 * Note that we were successfull
4861 ahc_fini_scbdata(struct ahc_softc
*ahc
)
4863 struct scb_data
*scb_data
;
4865 scb_data
= ahc
->scb_data
;
4866 if (scb_data
== NULL
)
4869 switch (scb_data
->init_level
) {
4873 struct sg_map_node
*sg_map
;
4875 while ((sg_map
= SLIST_FIRST(&scb_data
->sg_maps
))!= NULL
) {
4876 SLIST_REMOVE_HEAD(&scb_data
->sg_maps
, links
);
4877 ahc_dmamap_unload(ahc
, scb_data
->sg_dmat
,
4879 ahc_dmamem_free(ahc
, scb_data
->sg_dmat
,
4884 ahc_dma_tag_destroy(ahc
, scb_data
->sg_dmat
);
4887 ahc_dmamap_unload(ahc
, scb_data
->sense_dmat
,
4888 scb_data
->sense_dmamap
);
4890 ahc_dmamem_free(ahc
, scb_data
->sense_dmat
, scb_data
->sense
,
4891 scb_data
->sense_dmamap
);
4892 ahc_dmamap_destroy(ahc
, scb_data
->sense_dmat
,
4893 scb_data
->sense_dmamap
);
4895 ahc_dma_tag_destroy(ahc
, scb_data
->sense_dmat
);
4897 ahc_dmamap_unload(ahc
, scb_data
->hscb_dmat
,
4898 scb_data
->hscb_dmamap
);
4900 ahc_dmamem_free(ahc
, scb_data
->hscb_dmat
, scb_data
->hscbs
,
4901 scb_data
->hscb_dmamap
);
4902 ahc_dmamap_destroy(ahc
, scb_data
->hscb_dmat
,
4903 scb_data
->hscb_dmamap
);
4905 ahc_dma_tag_destroy(ahc
, scb_data
->hscb_dmat
);
4910 if (scb_data
->scbarray
!= NULL
)
4911 kfree(scb_data
->scbarray
);
4915 ahc_alloc_scbs(struct ahc_softc
*ahc
)
4917 struct scb_data
*scb_data
;
4918 struct scb
*next_scb
;
4919 struct sg_map_node
*sg_map
;
4920 dma_addr_t physaddr
;
4921 struct ahc_dma_seg
*segs
;
4925 scb_data
= ahc
->scb_data
;
4926 if (scb_data
->numscbs
>= AHC_SCB_MAX_ALLOC
)
4927 /* Can't allocate any more */
4930 next_scb
= &scb_data
->scbarray
[scb_data
->numscbs
];
4932 sg_map
= kmalloc(sizeof(*sg_map
), GFP_ATOMIC
);
4937 /* Allocate S/G space for the next batch of SCBS */
4938 if (ahc_dmamem_alloc(ahc
, scb_data
->sg_dmat
,
4939 (void **)&sg_map
->sg_vaddr
,
4940 BUS_DMA_NOWAIT
, &sg_map
->sg_dmamap
) != 0) {
4945 SLIST_INSERT_HEAD(&scb_data
->sg_maps
, sg_map
, links
);
4947 ahc_dmamap_load(ahc
, scb_data
->sg_dmat
, sg_map
->sg_dmamap
,
4948 sg_map
->sg_vaddr
, PAGE_SIZE
, ahc_dmamap_cb
,
4949 &sg_map
->sg_physaddr
, /*flags*/0);
4951 segs
= sg_map
->sg_vaddr
;
4952 physaddr
= sg_map
->sg_physaddr
;
4954 newcount
= (PAGE_SIZE
/ (AHC_NSEG
* sizeof(struct ahc_dma_seg
)));
4955 newcount
= min(newcount
, (AHC_SCB_MAX_ALLOC
- scb_data
->numscbs
));
4956 for (i
= 0; i
< newcount
; i
++) {
4957 struct scb_platform_data
*pdata
;
4961 pdata
= kmalloc(sizeof(*pdata
), GFP_ATOMIC
);
4964 next_scb
->platform_data
= pdata
;
4965 next_scb
->sg_map
= sg_map
;
4966 next_scb
->sg_list
= segs
;
4968 * The sequencer always starts with the second entry.
4969 * The first entry is embedded in the scb.
4971 next_scb
->sg_list_phys
= physaddr
+ sizeof(struct ahc_dma_seg
);
4972 next_scb
->ahc_softc
= ahc
;
4973 next_scb
->flags
= SCB_FREE
;
4975 error
= ahc_dmamap_create(ahc
, ahc
->buffer_dmat
, /*flags*/0,
4980 next_scb
->hscb
= &scb_data
->hscbs
[scb_data
->numscbs
];
4981 next_scb
->hscb
->tag
= ahc
->scb_data
->numscbs
;
4982 SLIST_INSERT_HEAD(&ahc
->scb_data
->free_scbs
,
4983 next_scb
, links
.sle
);
4985 physaddr
+= (AHC_NSEG
* sizeof(struct ahc_dma_seg
));
4987 ahc
->scb_data
->numscbs
++;
4992 ahc_controller_info(struct ahc_softc
*ahc
, char *buf
)
4996 len
= sprintf(buf
, "%s: ", ahc_chip_names
[ahc
->chip
& AHC_CHIPID_MASK
]);
4998 if ((ahc
->features
& AHC_TWIN
) != 0)
4999 len
= sprintf(buf
, "Twin Channel, A SCSI Id=%d, "
5000 "B SCSI Id=%d, primary %c, ",
5001 ahc
->our_id
, ahc
->our_id_b
,
5002 (ahc
->flags
& AHC_PRIMARY_CHANNEL
) + 'A');
5008 if ((ahc
->features
& AHC_ULTRA
) != 0) {
5010 } else if ((ahc
->features
& AHC_DT
) != 0) {
5011 speed
= "Ultra160 ";
5012 } else if ((ahc
->features
& AHC_ULTRA2
) != 0) {
5015 if ((ahc
->features
& AHC_WIDE
) != 0) {
5020 len
= sprintf(buf
, "%s%s Channel %c, SCSI Id=%d, ",
5021 speed
, type
, ahc
->channel
, ahc
->our_id
);
5025 if ((ahc
->flags
& AHC_PAGESCBS
) != 0)
5026 sprintf(buf
, "%d/%d SCBs",
5027 ahc
->scb_data
->maxhscbs
, AHC_MAX_QUEUE
);
5029 sprintf(buf
, "%d SCBs", ahc
->scb_data
->maxhscbs
);
5033 ahc_chip_init(struct ahc_softc
*ahc
)
5039 u_int scsiseq_template
;
5042 ahc_outb(ahc
, SEQ_FLAGS
, 0);
5043 ahc_outb(ahc
, SEQ_FLAGS2
, 0);
5045 /* Set the SCSI Id, SXFRCTL0, SXFRCTL1, and SIMODE1, for both channels*/
5046 if (ahc
->features
& AHC_TWIN
) {
5049 * Setup Channel B first.
5051 ahc_outb(ahc
, SBLKCTL
, ahc_inb(ahc
, SBLKCTL
) | SELBUSB
);
5052 term
= (ahc
->flags
& AHC_TERM_ENB_B
) != 0 ? STPWEN
: 0;
5053 ahc_outb(ahc
, SCSIID
, ahc
->our_id_b
);
5054 scsi_conf
= ahc_inb(ahc
, SCSICONF
+ 1);
5055 ahc_outb(ahc
, SXFRCTL1
, (scsi_conf
& (ENSPCHK
|STIMESEL
))
5056 |term
|ahc
->seltime_b
|ENSTIMER
|ACTNEGEN
);
5057 if ((ahc
->features
& AHC_ULTRA2
) != 0)
5058 ahc_outb(ahc
, SIMODE0
, ahc_inb(ahc
, SIMODE0
)|ENIOERR
);
5059 ahc_outb(ahc
, SIMODE1
, ENSELTIMO
|ENSCSIRST
|ENSCSIPERR
);
5060 ahc_outb(ahc
, SXFRCTL0
, DFON
|SPIOEN
);
5062 /* Select Channel A */
5063 ahc_outb(ahc
, SBLKCTL
, ahc_inb(ahc
, SBLKCTL
) & ~SELBUSB
);
5065 term
= (ahc
->flags
& AHC_TERM_ENB_A
) != 0 ? STPWEN
: 0;
5066 if ((ahc
->features
& AHC_ULTRA2
) != 0)
5067 ahc_outb(ahc
, SCSIID_ULTRA2
, ahc
->our_id
);
5069 ahc_outb(ahc
, SCSIID
, ahc
->our_id
);
5070 scsi_conf
= ahc_inb(ahc
, SCSICONF
);
5071 ahc_outb(ahc
, SXFRCTL1
, (scsi_conf
& (ENSPCHK
|STIMESEL
))
5073 |ENSTIMER
|ACTNEGEN
);
5074 if ((ahc
->features
& AHC_ULTRA2
) != 0)
5075 ahc_outb(ahc
, SIMODE0
, ahc_inb(ahc
, SIMODE0
)|ENIOERR
);
5076 ahc_outb(ahc
, SIMODE1
, ENSELTIMO
|ENSCSIRST
|ENSCSIPERR
);
5077 ahc_outb(ahc
, SXFRCTL0
, DFON
|SPIOEN
);
5079 /* There are no untagged SCBs active yet. */
5080 for (i
= 0; i
< 16; i
++) {
5081 ahc_unbusy_tcl(ahc
, BUILD_TCL(i
<< 4, 0));
5082 if ((ahc
->flags
& AHC_SCB_BTT
) != 0) {
5086 * The SCB based BTT allows an entry per
5087 * target and lun pair.
5089 for (lun
= 1; lun
< AHC_NUM_LUNS
; lun
++)
5090 ahc_unbusy_tcl(ahc
, BUILD_TCL(i
<< 4, lun
));
5094 /* All of our queues are empty */
5095 for (i
= 0; i
< 256; i
++)
5096 ahc
->qoutfifo
[i
] = SCB_LIST_NULL
;
5097 ahc_sync_qoutfifo(ahc
, BUS_DMASYNC_PREREAD
);
5099 for (i
= 0; i
< 256; i
++)
5100 ahc
->qinfifo
[i
] = SCB_LIST_NULL
;
5102 if ((ahc
->features
& AHC_MULTI_TID
) != 0) {
5103 ahc_outb(ahc
, TARGID
, 0);
5104 ahc_outb(ahc
, TARGID
+ 1, 0);
5108 * Tell the sequencer where it can find our arrays in memory.
5110 physaddr
= ahc
->scb_data
->hscb_busaddr
;
5111 ahc_outb(ahc
, HSCB_ADDR
, physaddr
& 0xFF);
5112 ahc_outb(ahc
, HSCB_ADDR
+ 1, (physaddr
>> 8) & 0xFF);
5113 ahc_outb(ahc
, HSCB_ADDR
+ 2, (physaddr
>> 16) & 0xFF);
5114 ahc_outb(ahc
, HSCB_ADDR
+ 3, (physaddr
>> 24) & 0xFF);
5116 physaddr
= ahc
->shared_data_busaddr
;
5117 ahc_outb(ahc
, SHARED_DATA_ADDR
, physaddr
& 0xFF);
5118 ahc_outb(ahc
, SHARED_DATA_ADDR
+ 1, (physaddr
>> 8) & 0xFF);
5119 ahc_outb(ahc
, SHARED_DATA_ADDR
+ 2, (physaddr
>> 16) & 0xFF);
5120 ahc_outb(ahc
, SHARED_DATA_ADDR
+ 3, (physaddr
>> 24) & 0xFF);
5123 * Initialize the group code to command length table.
5124 * This overrides the values in TARG_SCSIRATE, so only
5125 * setup the table after we have processed that information.
5127 ahc_outb(ahc
, CMDSIZE_TABLE
, 5);
5128 ahc_outb(ahc
, CMDSIZE_TABLE
+ 1, 9);
5129 ahc_outb(ahc
, CMDSIZE_TABLE
+ 2, 9);
5130 ahc_outb(ahc
, CMDSIZE_TABLE
+ 3, 0);
5131 ahc_outb(ahc
, CMDSIZE_TABLE
+ 4, 15);
5132 ahc_outb(ahc
, CMDSIZE_TABLE
+ 5, 11);
5133 ahc_outb(ahc
, CMDSIZE_TABLE
+ 6, 0);
5134 ahc_outb(ahc
, CMDSIZE_TABLE
+ 7, 0);
5136 if ((ahc
->features
& AHC_HS_MAILBOX
) != 0)
5137 ahc_outb(ahc
, HS_MAILBOX
, 0);
5139 /* Tell the sequencer of our initial queue positions */
5140 if ((ahc
->features
& AHC_TARGETMODE
) != 0) {
5141 ahc
->tqinfifonext
= 1;
5142 ahc_outb(ahc
, KERNEL_TQINPOS
, ahc
->tqinfifonext
- 1);
5143 ahc_outb(ahc
, TQINPOS
, ahc
->tqinfifonext
);
5145 ahc
->qinfifonext
= 0;
5146 ahc
->qoutfifonext
= 0;
5147 if ((ahc
->features
& AHC_QUEUE_REGS
) != 0) {
5148 ahc_outb(ahc
, QOFF_CTLSTA
, SCB_QSIZE_256
);
5149 ahc_outb(ahc
, HNSCB_QOFF
, ahc
->qinfifonext
);
5150 ahc_outb(ahc
, SNSCB_QOFF
, ahc
->qinfifonext
);
5151 ahc_outb(ahc
, SDSCB_QOFF
, 0);
5153 ahc_outb(ahc
, KERNEL_QINPOS
, ahc
->qinfifonext
);
5154 ahc_outb(ahc
, QINPOS
, ahc
->qinfifonext
);
5155 ahc_outb(ahc
, QOUTPOS
, ahc
->qoutfifonext
);
5158 /* We don't have any waiting selections */
5159 ahc_outb(ahc
, WAITING_SCBH
, SCB_LIST_NULL
);
5161 /* Our disconnection list is empty too */
5162 ahc_outb(ahc
, DISCONNECTED_SCBH
, SCB_LIST_NULL
);
5164 /* Message out buffer starts empty */
5165 ahc_outb(ahc
, MSG_OUT
, MSG_NOOP
);
5168 * Setup the allowed SCSI Sequences based on operational mode.
5169 * If we are a target, we'll enable select in operations once
5170 * we've had a lun enabled.
5172 scsiseq_template
= ENSELO
|ENAUTOATNO
|ENAUTOATNP
;
5173 if ((ahc
->flags
& AHC_INITIATORROLE
) != 0)
5174 scsiseq_template
|= ENRSELI
;
5175 ahc_outb(ahc
, SCSISEQ_TEMPLATE
, scsiseq_template
);
5177 /* Initialize our list of free SCBs. */
5178 ahc_build_free_scb_list(ahc
);
5181 * Tell the sequencer which SCB will be the next one it receives.
5183 ahc_outb(ahc
, NEXT_QUEUED_SCB
, ahc
->next_queued_scb
->hscb
->tag
);
5186 * Load the Sequencer program and Enable the adapter
5190 printk("%s: Downloading Sequencer Program...",
5193 error
= ahc_loadseq(ahc
);
5197 if ((ahc
->features
& AHC_ULTRA2
) != 0) {
5201 * Wait for up to 500ms for our transceivers
5202 * to settle. If the adapter does not have
5203 * a cable attached, the transceivers may
5204 * never settle, so don't complain if we
5208 (ahc_inb(ahc
, SBLKCTL
) & (ENAB40
|ENAB20
)) == 0 && wait
;
5217 * Start the board, ready for normal operation
5220 ahc_init(struct ahc_softc
*ahc
)
5228 size_t driver_data_size
;
5231 if ((ahc_debug
& AHC_DEBUG_SEQUENCER
) != 0)
5232 ahc
->flags
|= AHC_SEQUENCER_DEBUG
;
5235 #ifdef AHC_PRINT_SRAM
5236 printk("Scratch Ram:");
5237 for (i
= 0x20; i
< 0x5f; i
++) {
5238 if (((i
% 8) == 0) && (i
!= 0)) {
5241 printk (" 0x%x", ahc_inb(ahc
, i
));
5243 if ((ahc
->features
& AHC_MORE_SRAM
) != 0) {
5244 for (i
= 0x70; i
< 0x7f; i
++) {
5245 if (((i
% 8) == 0) && (i
!= 0)) {
5248 printk (" 0x%x", ahc_inb(ahc
, i
));
5253 * Reading uninitialized scratch ram may
5254 * generate parity errors.
5256 ahc_outb(ahc
, CLRINT
, CLRPARERR
);
5257 ahc_outb(ahc
, CLRINT
, CLRBRKADRINT
);
5262 * Assume we have a board at this stage and it has been reset.
5264 if ((ahc
->flags
& AHC_USEDEFAULTS
) != 0)
5265 ahc
->our_id
= ahc
->our_id_b
= 7;
5268 * Default to allowing initiator operations.
5270 ahc
->flags
|= AHC_INITIATORROLE
;
5273 * Only allow target mode features if this unit has them enabled.
5275 if ((AHC_TMODE_ENABLE
& (0x1 << ahc
->unit
)) == 0)
5276 ahc
->features
&= ~AHC_TARGETMODE
;
5279 /* DMA tag for mapping buffers into device visible space. */
5280 if (ahc_dma_tag_create(ahc
, ahc
->parent_dmat
, /*alignment*/1,
5281 /*boundary*/BUS_SPACE_MAXADDR_32BIT
+ 1,
5282 /*lowaddr*/ahc
->flags
& AHC_39BIT_ADDRESSING
5283 ? (dma_addr_t
)0x7FFFFFFFFFULL
5284 : BUS_SPACE_MAXADDR_32BIT
,
5285 /*highaddr*/BUS_SPACE_MAXADDR
,
5286 /*filter*/NULL
, /*filterarg*/NULL
,
5287 /*maxsize*/(AHC_NSEG
- 1) * PAGE_SIZE
,
5288 /*nsegments*/AHC_NSEG
,
5289 /*maxsegsz*/AHC_MAXTRANSFER_SIZE
,
5290 /*flags*/BUS_DMA_ALLOCNOW
,
5291 &ahc
->buffer_dmat
) != 0) {
5299 * DMA tag for our command fifos and other data in system memory
5300 * the card's sequencer must be able to access. For initiator
5301 * roles, we need to allocate space for the qinfifo and qoutfifo.
5302 * The qinfifo and qoutfifo are composed of 256 1 byte elements.
5303 * When providing for the target mode role, we must additionally
5304 * provide space for the incoming target command fifo and an extra
5305 * byte to deal with a dma bug in some chip versions.
5307 driver_data_size
= 2 * 256 * sizeof(uint8_t);
5308 if ((ahc
->features
& AHC_TARGETMODE
) != 0)
5309 driver_data_size
+= AHC_TMODE_CMDS
* sizeof(struct target_cmd
)
5310 + /*DMA WideOdd Bug Buffer*/1;
5311 if (ahc_dma_tag_create(ahc
, ahc
->parent_dmat
, /*alignment*/1,
5312 /*boundary*/BUS_SPACE_MAXADDR_32BIT
+ 1,
5313 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT
,
5314 /*highaddr*/BUS_SPACE_MAXADDR
,
5315 /*filter*/NULL
, /*filterarg*/NULL
,
5318 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT
,
5319 /*flags*/0, &ahc
->shared_data_dmat
) != 0) {
5325 /* Allocation of driver data */
5326 if (ahc_dmamem_alloc(ahc
, ahc
->shared_data_dmat
,
5327 (void **)&ahc
->qoutfifo
,
5328 BUS_DMA_NOWAIT
, &ahc
->shared_data_dmamap
) != 0) {
5334 /* And permanently map it in */
5335 ahc_dmamap_load(ahc
, ahc
->shared_data_dmat
, ahc
->shared_data_dmamap
,
5336 ahc
->qoutfifo
, driver_data_size
, ahc_dmamap_cb
,
5337 &ahc
->shared_data_busaddr
, /*flags*/0);
5339 if ((ahc
->features
& AHC_TARGETMODE
) != 0) {
5340 ahc
->targetcmds
= (struct target_cmd
*)ahc
->qoutfifo
;
5341 ahc
->qoutfifo
= (uint8_t *)&ahc
->targetcmds
[AHC_TMODE_CMDS
];
5342 ahc
->dma_bug_buf
= ahc
->shared_data_busaddr
5343 + driver_data_size
- 1;
5344 /* All target command blocks start out invalid. */
5345 for (i
= 0; i
< AHC_TMODE_CMDS
; i
++)
5346 ahc
->targetcmds
[i
].cmd_valid
= 0;
5347 ahc_sync_tqinfifo(ahc
, BUS_DMASYNC_PREREAD
);
5348 ahc
->qoutfifo
= (uint8_t *)&ahc
->targetcmds
[256];
5350 ahc
->qinfifo
= &ahc
->qoutfifo
[256];
5354 /* Allocate SCB data now that buffer_dmat is initialized */
5355 if (ahc
->scb_data
->maxhscbs
== 0)
5356 if (ahc_init_scbdata(ahc
) != 0)
5360 * Allocate a tstate to house information for our
5361 * initiator presence on the bus as well as the user
5362 * data for any target mode initiator.
5364 if (ahc_alloc_tstate(ahc
, ahc
->our_id
, 'A') == NULL
) {
5365 printk("%s: unable to allocate ahc_tmode_tstate. "
5366 "Failing attach\n", ahc_name(ahc
));
5370 if ((ahc
->features
& AHC_TWIN
) != 0) {
5371 if (ahc_alloc_tstate(ahc
, ahc
->our_id_b
, 'B') == NULL
) {
5372 printk("%s: unable to allocate ahc_tmode_tstate. "
5373 "Failing attach\n", ahc_name(ahc
));
5378 if (ahc
->scb_data
->maxhscbs
< AHC_SCB_MAX_ALLOC
) {
5379 ahc
->flags
|= AHC_PAGESCBS
;
5381 ahc
->flags
&= ~AHC_PAGESCBS
;
5385 if (ahc_debug
& AHC_SHOW_MISC
) {
5386 printk("%s: hardware scb %u bytes; kernel scb %u bytes; "
5387 "ahc_dma %u bytes\n",
5389 (u_int
)sizeof(struct hardware_scb
),
5390 (u_int
)sizeof(struct scb
),
5391 (u_int
)sizeof(struct ahc_dma_seg
));
5393 #endif /* AHC_DEBUG */
5396 * Look at the information that board initialization or
5397 * the board bios has left us.
5399 if (ahc
->features
& AHC_TWIN
) {
5400 scsi_conf
= ahc_inb(ahc
, SCSICONF
+ 1);
5401 if ((scsi_conf
& RESET_SCSI
) != 0
5402 && (ahc
->flags
& AHC_INITIATORROLE
) != 0)
5403 ahc
->flags
|= AHC_RESET_BUS_B
;
5406 scsi_conf
= ahc_inb(ahc
, SCSICONF
);
5407 if ((scsi_conf
& RESET_SCSI
) != 0
5408 && (ahc
->flags
& AHC_INITIATORROLE
) != 0)
5409 ahc
->flags
|= AHC_RESET_BUS_A
;
5412 tagenable
= ALL_TARGETS_MASK
;
5414 /* Grab the disconnection disable table and invert it for our needs */
5415 if ((ahc
->flags
& AHC_USEDEFAULTS
) != 0) {
5416 printk("%s: Host Adapter Bios disabled. Using default SCSI "
5417 "device parameters\n", ahc_name(ahc
));
5418 ahc
->flags
|= AHC_EXTENDED_TRANS_A
|AHC_EXTENDED_TRANS_B
|
5419 AHC_TERM_ENB_A
|AHC_TERM_ENB_B
;
5420 discenable
= ALL_TARGETS_MASK
;
5421 if ((ahc
->features
& AHC_ULTRA
) != 0)
5422 ultraenb
= ALL_TARGETS_MASK
;
5424 discenable
= ~((ahc_inb(ahc
, DISC_DSB
+ 1) << 8)
5425 | ahc_inb(ahc
, DISC_DSB
));
5426 if ((ahc
->features
& (AHC_ULTRA
|AHC_ULTRA2
)) != 0)
5427 ultraenb
= (ahc_inb(ahc
, ULTRA_ENB
+ 1) << 8)
5428 | ahc_inb(ahc
, ULTRA_ENB
);
5431 if ((ahc
->features
& (AHC_WIDE
|AHC_TWIN
)) == 0)
5434 for (i
= 0; i
<= max_targ
; i
++) {
5435 struct ahc_initiator_tinfo
*tinfo
;
5436 struct ahc_tmode_tstate
*tstate
;
5442 our_id
= ahc
->our_id
;
5444 if (i
> 7 && (ahc
->features
& AHC_TWIN
) != 0) {
5446 our_id
= ahc
->our_id_b
;
5449 tinfo
= ahc_fetch_transinfo(ahc
, channel
, our_id
,
5450 target_id
, &tstate
);
5451 /* Default to async narrow across the board */
5452 memset(tinfo
, 0, sizeof(*tinfo
));
5453 if (ahc
->flags
& AHC_USEDEFAULTS
) {
5454 if ((ahc
->features
& AHC_WIDE
) != 0)
5455 tinfo
->user
.width
= MSG_EXT_WDTR_BUS_16_BIT
;
5458 * These will be truncated when we determine the
5459 * connection type we have with the target.
5461 tinfo
->user
.period
= ahc_syncrates
->period
;
5462 tinfo
->user
.offset
= MAX_OFFSET
;
5467 /* Take the settings leftover in scratch RAM. */
5468 scsirate
= ahc_inb(ahc
, TARG_SCSIRATE
+ i
);
5470 if ((ahc
->features
& AHC_ULTRA2
) != 0) {
5474 if ((scsirate
& SOFS
) == 0x0F) {
5476 * Haven't negotiated yet,
5477 * so the format is different.
5479 scsirate
= (scsirate
& SXFR
) >> 4
5482 | (scsirate
& WIDEXFER
);
5483 offset
= MAX_OFFSET_ULTRA2
;
5485 offset
= ahc_inb(ahc
, TARG_OFFSET
+ i
);
5486 if ((scsirate
& ~WIDEXFER
) == 0 && offset
!= 0)
5487 /* Set to the lowest sync rate, 5MHz */
5489 maxsync
= AHC_SYNCRATE_ULTRA2
;
5490 if ((ahc
->features
& AHC_DT
) != 0)
5491 maxsync
= AHC_SYNCRATE_DT
;
5492 tinfo
->user
.period
=
5493 ahc_find_period(ahc
, scsirate
, maxsync
);
5495 tinfo
->user
.period
= 0;
5497 tinfo
->user
.offset
= MAX_OFFSET
;
5498 if ((scsirate
& SXFR_ULTRA2
) <= 8/*10MHz*/
5499 && (ahc
->features
& AHC_DT
) != 0)
5500 tinfo
->user
.ppr_options
=
5502 } else if ((scsirate
& SOFS
) != 0) {
5503 if ((scsirate
& SXFR
) == 0x40
5504 && (ultraenb
& mask
) != 0) {
5505 /* Treat 10MHz as a non-ultra speed */
5509 tinfo
->user
.period
=
5510 ahc_find_period(ahc
, scsirate
,
5512 ? AHC_SYNCRATE_ULTRA
5513 : AHC_SYNCRATE_FAST
);
5514 if (tinfo
->user
.period
!= 0)
5515 tinfo
->user
.offset
= MAX_OFFSET
;
5517 if (tinfo
->user
.period
== 0)
5518 tinfo
->user
.offset
= 0;
5519 if ((scsirate
& WIDEXFER
) != 0
5520 && (ahc
->features
& AHC_WIDE
) != 0)
5521 tinfo
->user
.width
= MSG_EXT_WDTR_BUS_16_BIT
;
5522 tinfo
->user
.protocol_version
= 4;
5523 if ((ahc
->features
& AHC_DT
) != 0)
5524 tinfo
->user
.transport_version
= 3;
5526 tinfo
->user
.transport_version
= 2;
5527 tinfo
->goal
.protocol_version
= 2;
5528 tinfo
->goal
.transport_version
= 2;
5529 tinfo
->curr
.protocol_version
= 2;
5530 tinfo
->curr
.transport_version
= 2;
5532 tstate
->ultraenb
= 0;
5534 ahc
->user_discenable
= discenable
;
5535 ahc
->user_tagenable
= tagenable
;
5537 return (ahc
->bus_chip_init(ahc
));
5541 ahc_intr_enable(struct ahc_softc
*ahc
, int enable
)
5545 hcntrl
= ahc_inb(ahc
, HCNTRL
);
5547 ahc
->pause
&= ~INTEN
;
5548 ahc
->unpause
&= ~INTEN
;
5551 ahc
->pause
|= INTEN
;
5552 ahc
->unpause
|= INTEN
;
5554 ahc_outb(ahc
, HCNTRL
, hcntrl
);
5558 * Ensure that the card is paused in a location
5559 * outside of all critical sections and that all
5560 * pending work is completed prior to returning.
5561 * This routine should only be called from outside
5562 * an interrupt context.
5565 ahc_pause_and_flushwork(struct ahc_softc
*ahc
)
5572 ahc
->flags
|= AHC_ALL_INTERRUPTS
;
5578 * Give the sequencer some time to service
5579 * any active selections.
5586 ahc_outb(ahc
, SCSISEQ
, ahc_inb(ahc
, SCSISEQ
) & ~ENSELO
);
5587 intstat
= ahc_inb(ahc
, INTSTAT
);
5588 if ((intstat
& INT_PEND
) == 0) {
5589 ahc_clear_critical_section(ahc
);
5590 intstat
= ahc_inb(ahc
, INTSTAT
);
5593 && (intstat
!= 0xFF || (ahc
->features
& AHC_REMOVABLE
) == 0)
5594 && ((intstat
& INT_PEND
) != 0
5595 || (ahc_inb(ahc
, SSTAT0
) & (SELDO
|SELINGO
)) != 0));
5596 if (maxloops
== 0) {
5597 printk("Infinite interrupt loop, INTSTAT = %x",
5598 ahc_inb(ahc
, INTSTAT
));
5600 ahc_platform_flushwork(ahc
);
5601 ahc
->flags
&= ~AHC_ALL_INTERRUPTS
;
5606 ahc_suspend(struct ahc_softc
*ahc
)
5609 ahc_pause_and_flushwork(ahc
);
5611 if (LIST_FIRST(&ahc
->pending_scbs
) != NULL
) {
5616 #ifdef AHC_TARGET_MODE
5617 if (ahc
->pending_device
!= NULL
) {
5627 ahc_resume(struct ahc_softc
*ahc
)
5630 ahc_reset(ahc
, /*reinit*/TRUE
);
5631 ahc_intr_enable(ahc
, TRUE
);
5636 /************************** Busy Target Table *********************************/
5638 * Return the untagged transaction id for a given target/channel lun.
5639 * Optionally, clear the entry.
5642 ahc_index_busy_tcl(struct ahc_softc
*ahc
, u_int tcl
)
5645 u_int target_offset
;
5647 if ((ahc
->flags
& AHC_SCB_BTT
) != 0) {
5650 saved_scbptr
= ahc_inb(ahc
, SCBPTR
);
5651 ahc_outb(ahc
, SCBPTR
, TCL_LUN(tcl
));
5652 scbid
= ahc_inb(ahc
, SCB_64_BTT
+ TCL_TARGET_OFFSET(tcl
));
5653 ahc_outb(ahc
, SCBPTR
, saved_scbptr
);
5655 target_offset
= TCL_TARGET_OFFSET(tcl
);
5656 scbid
= ahc_inb(ahc
, BUSY_TARGETS
+ target_offset
);
5663 ahc_unbusy_tcl(struct ahc_softc
*ahc
, u_int tcl
)
5665 u_int target_offset
;
5667 if ((ahc
->flags
& AHC_SCB_BTT
) != 0) {
5670 saved_scbptr
= ahc_inb(ahc
, SCBPTR
);
5671 ahc_outb(ahc
, SCBPTR
, TCL_LUN(tcl
));
5672 ahc_outb(ahc
, SCB_64_BTT
+TCL_TARGET_OFFSET(tcl
), SCB_LIST_NULL
);
5673 ahc_outb(ahc
, SCBPTR
, saved_scbptr
);
5675 target_offset
= TCL_TARGET_OFFSET(tcl
);
5676 ahc_outb(ahc
, BUSY_TARGETS
+ target_offset
, SCB_LIST_NULL
);
5681 ahc_busy_tcl(struct ahc_softc
*ahc
, u_int tcl
, u_int scbid
)
5683 u_int target_offset
;
5685 if ((ahc
->flags
& AHC_SCB_BTT
) != 0) {
5688 saved_scbptr
= ahc_inb(ahc
, SCBPTR
);
5689 ahc_outb(ahc
, SCBPTR
, TCL_LUN(tcl
));
5690 ahc_outb(ahc
, SCB_64_BTT
+ TCL_TARGET_OFFSET(tcl
), scbid
);
5691 ahc_outb(ahc
, SCBPTR
, saved_scbptr
);
5693 target_offset
= TCL_TARGET_OFFSET(tcl
);
5694 ahc_outb(ahc
, BUSY_TARGETS
+ target_offset
, scbid
);
5698 /************************** SCB and SCB queue management **********************/
5700 ahc_match_scb(struct ahc_softc
*ahc
, struct scb
*scb
, int target
,
5701 char channel
, int lun
, u_int tag
, role_t role
)
5703 int targ
= SCB_GET_TARGET(ahc
, scb
);
5704 char chan
= SCB_GET_CHANNEL(ahc
, scb
);
5705 int slun
= SCB_GET_LUN(scb
);
5708 match
= ((chan
== channel
) || (channel
== ALL_CHANNELS
));
5710 match
= ((targ
== target
) || (target
== CAM_TARGET_WILDCARD
));
5712 match
= ((lun
== slun
) || (lun
== CAM_LUN_WILDCARD
));
5714 #ifdef AHC_TARGET_MODE
5717 group
= XPT_FC_GROUP(scb
->io_ctx
->ccb_h
.func_code
);
5718 if (role
== ROLE_INITIATOR
) {
5719 match
= (group
!= XPT_FC_GROUP_TMODE
)
5720 && ((tag
== scb
->hscb
->tag
)
5721 || (tag
== SCB_LIST_NULL
));
5722 } else if (role
== ROLE_TARGET
) {
5723 match
= (group
== XPT_FC_GROUP_TMODE
)
5724 && ((tag
== scb
->io_ctx
->csio
.tag_id
)
5725 || (tag
== SCB_LIST_NULL
));
5727 #else /* !AHC_TARGET_MODE */
5728 match
= ((tag
== scb
->hscb
->tag
) || (tag
== SCB_LIST_NULL
));
5729 #endif /* AHC_TARGET_MODE */
5736 ahc_freeze_devq(struct ahc_softc
*ahc
, struct scb
*scb
)
5742 target
= SCB_GET_TARGET(ahc
, scb
);
5743 lun
= SCB_GET_LUN(scb
);
5744 channel
= SCB_GET_CHANNEL(ahc
, scb
);
5746 ahc_search_qinfifo(ahc
, target
, channel
, lun
,
5747 /*tag*/SCB_LIST_NULL
, ROLE_UNKNOWN
,
5748 CAM_REQUEUE_REQ
, SEARCH_COMPLETE
);
5750 ahc_platform_freeze_devq(ahc
, scb
);
5754 ahc_qinfifo_requeue_tail(struct ahc_softc
*ahc
, struct scb
*scb
)
5756 struct scb
*prev_scb
;
5759 if (ahc_qinfifo_count(ahc
) != 0) {
5763 prev_pos
= ahc
->qinfifonext
- 1;
5764 prev_tag
= ahc
->qinfifo
[prev_pos
];
5765 prev_scb
= ahc_lookup_scb(ahc
, prev_tag
);
5767 ahc_qinfifo_requeue(ahc
, prev_scb
, scb
);
5768 if ((ahc
->features
& AHC_QUEUE_REGS
) != 0) {
5769 ahc_outb(ahc
, HNSCB_QOFF
, ahc
->qinfifonext
);
5771 ahc_outb(ahc
, KERNEL_QINPOS
, ahc
->qinfifonext
);
5776 ahc_qinfifo_requeue(struct ahc_softc
*ahc
, struct scb
*prev_scb
,
5779 if (prev_scb
== NULL
) {
5780 ahc_outb(ahc
, NEXT_QUEUED_SCB
, scb
->hscb
->tag
);
5782 prev_scb
->hscb
->next
= scb
->hscb
->tag
;
5783 ahc_sync_scb(ahc
, prev_scb
,
5784 BUS_DMASYNC_PREREAD
|BUS_DMASYNC_PREWRITE
);
5786 ahc
->qinfifo
[ahc
->qinfifonext
++] = scb
->hscb
->tag
;
5787 scb
->hscb
->next
= ahc
->next_queued_scb
->hscb
->tag
;
5788 ahc_sync_scb(ahc
, scb
, BUS_DMASYNC_PREREAD
|BUS_DMASYNC_PREWRITE
);
5792 ahc_qinfifo_count(struct ahc_softc
*ahc
)
5797 if ((ahc
->features
& AHC_QUEUE_REGS
) != 0) {
5798 qinpos
= ahc_inb(ahc
, SNSCB_QOFF
);
5799 ahc_outb(ahc
, SNSCB_QOFF
, qinpos
);
5801 qinpos
= ahc_inb(ahc
, QINPOS
);
5802 diff
= ahc
->qinfifonext
- qinpos
;
5807 ahc_search_qinfifo(struct ahc_softc
*ahc
, int target
, char channel
,
5808 int lun
, u_int tag
, role_t role
, uint32_t status
,
5809 ahc_search_action action
)
5812 struct scb
*prev_scb
;
5822 qintail
= ahc
->qinfifonext
;
5823 have_qregs
= (ahc
->features
& AHC_QUEUE_REGS
) != 0;
5825 qinstart
= ahc_inb(ahc
, SNSCB_QOFF
);
5826 ahc_outb(ahc
, SNSCB_QOFF
, qinstart
);
5828 qinstart
= ahc_inb(ahc
, QINPOS
);
5833 if (action
== SEARCH_COMPLETE
) {
5835 * Don't attempt to run any queued untagged transactions
5836 * until we are done with the abort process.
5838 ahc_freeze_untagged_queues(ahc
);
5842 * Start with an empty queue. Entries that are not chosen
5843 * for removal will be re-added to the queue as we go.
5845 ahc
->qinfifonext
= qinpos
;
5846 ahc_outb(ahc
, NEXT_QUEUED_SCB
, ahc
->next_queued_scb
->hscb
->tag
);
5848 while (qinpos
!= qintail
) {
5849 scb
= ahc_lookup_scb(ahc
, ahc
->qinfifo
[qinpos
]);
5851 printk("qinpos = %d, SCB index = %d\n",
5852 qinpos
, ahc
->qinfifo
[qinpos
]);
5856 if (ahc_match_scb(ahc
, scb
, target
, channel
, lun
, tag
, role
)) {
5858 * We found an scb that needs to be acted on.
5862 case SEARCH_COMPLETE
:
5867 ostat
= ahc_get_transaction_status(scb
);
5868 if (ostat
== CAM_REQ_INPROG
)
5869 ahc_set_transaction_status(scb
, status
);
5870 cstat
= ahc_get_transaction_status(scb
);
5871 if (cstat
!= CAM_REQ_CMP
)
5872 ahc_freeze_scb(scb
);
5873 if ((scb
->flags
& SCB_ACTIVE
) == 0)
5874 printk("Inactive SCB in qinfifo\n");
5882 ahc_qinfifo_requeue(ahc
, prev_scb
, scb
);
5887 ahc_qinfifo_requeue(ahc
, prev_scb
, scb
);
5893 if ((ahc
->features
& AHC_QUEUE_REGS
) != 0) {
5894 ahc_outb(ahc
, HNSCB_QOFF
, ahc
->qinfifonext
);
5896 ahc_outb(ahc
, KERNEL_QINPOS
, ahc
->qinfifonext
);
5899 if (action
!= SEARCH_COUNT
5901 && (qinstart
!= ahc
->qinfifonext
)) {
5903 * The sequencer may be in the process of dmaing
5904 * down the SCB at the beginning of the queue.
5905 * This could be problematic if either the first,
5906 * or the second SCB is removed from the queue
5907 * (the first SCB includes a pointer to the "next"
5908 * SCB to dma). If we have removed any entries, swap
5909 * the first element in the queue with the next HSCB
5910 * so the sequencer will notice that NEXT_QUEUED_SCB
5911 * has changed during its dma attempt and will retry
5914 scb
= ahc_lookup_scb(ahc
, ahc
->qinfifo
[qinstart
]);
5917 printk("found = %d, qinstart = %d, qinfifionext = %d\n",
5918 found
, qinstart
, ahc
->qinfifonext
);
5919 panic("First/Second Qinfifo fixup\n");
5922 * ahc_swap_with_next_hscb forces our next pointer to
5923 * point to the reserved SCB for future commands. Save
5924 * and restore our original next pointer to maintain
5927 next
= scb
->hscb
->next
;
5928 ahc
->scb_data
->scbindex
[scb
->hscb
->tag
] = NULL
;
5929 ahc_swap_with_next_hscb(ahc
, scb
);
5930 scb
->hscb
->next
= next
;
5931 ahc
->qinfifo
[qinstart
] = scb
->hscb
->tag
;
5933 /* Tell the card about the new head of the qinfifo. */
5934 ahc_outb(ahc
, NEXT_QUEUED_SCB
, scb
->hscb
->tag
);
5936 /* Fixup the tail "next" pointer. */
5937 qintail
= ahc
->qinfifonext
- 1;
5938 scb
= ahc_lookup_scb(ahc
, ahc
->qinfifo
[qintail
]);
5939 scb
->hscb
->next
= ahc
->next_queued_scb
->hscb
->tag
;
5943 * Search waiting for selection list.
5945 curscbptr
= ahc_inb(ahc
, SCBPTR
);
5946 next
= ahc_inb(ahc
, WAITING_SCBH
); /* Start at head of list. */
5947 prev
= SCB_LIST_NULL
;
5949 while (next
!= SCB_LIST_NULL
) {
5952 ahc_outb(ahc
, SCBPTR
, next
);
5953 scb_index
= ahc_inb(ahc
, SCB_TAG
);
5954 if (scb_index
>= ahc
->scb_data
->numscbs
) {
5955 printk("Waiting List inconsistency. "
5956 "SCB index == %d, yet numscbs == %d.",
5957 scb_index
, ahc
->scb_data
->numscbs
);
5958 ahc_dump_card_state(ahc
);
5959 panic("for safety");
5961 scb
= ahc_lookup_scb(ahc
, scb_index
);
5963 printk("scb_index = %d, next = %d\n",
5965 panic("Waiting List traversal\n");
5967 if (ahc_match_scb(ahc
, scb
, target
, channel
,
5968 lun
, SCB_LIST_NULL
, role
)) {
5970 * We found an scb that needs to be acted on.
5974 case SEARCH_COMPLETE
:
5979 ostat
= ahc_get_transaction_status(scb
);
5980 if (ostat
== CAM_REQ_INPROG
)
5981 ahc_set_transaction_status(scb
,
5983 cstat
= ahc_get_transaction_status(scb
);
5984 if (cstat
!= CAM_REQ_CMP
)
5985 ahc_freeze_scb(scb
);
5986 if ((scb
->flags
& SCB_ACTIVE
) == 0)
5987 printk("Inactive SCB in Waiting List\n");
5992 next
= ahc_rem_wscb(ahc
, next
, prev
);
5996 next
= ahc_inb(ahc
, SCB_NEXT
);
6002 next
= ahc_inb(ahc
, SCB_NEXT
);
6005 ahc_outb(ahc
, SCBPTR
, curscbptr
);
6007 found
+= ahc_search_untagged_queues(ahc
, /*ahc_io_ctx_t*/NULL
, target
,
6008 channel
, lun
, status
, action
);
6010 if (action
== SEARCH_COMPLETE
)
6011 ahc_release_untagged_queues(ahc
);
6016 ahc_search_untagged_queues(struct ahc_softc
*ahc
, ahc_io_ctx_t ctx
,
6017 int target
, char channel
, int lun
, uint32_t status
,
6018 ahc_search_action action
)
6025 if (action
== SEARCH_COMPLETE
) {
6027 * Don't attempt to run any queued untagged transactions
6028 * until we are done with the abort process.
6030 ahc_freeze_untagged_queues(ahc
);
6035 if ((ahc
->flags
& AHC_SCB_BTT
) == 0) {
6038 if (target
!= CAM_TARGET_WILDCARD
) {
6049 for (; i
< maxtarget
; i
++) {
6050 struct scb_tailq
*untagged_q
;
6051 struct scb
*next_scb
;
6053 untagged_q
= &(ahc
->untagged_queues
[i
]);
6054 next_scb
= TAILQ_FIRST(untagged_q
);
6055 while (next_scb
!= NULL
) {
6058 next_scb
= TAILQ_NEXT(scb
, links
.tqe
);
6061 * The head of the list may be the currently
6062 * active untagged command for a device.
6063 * We're only searching for commands that
6064 * have not been started. A transaction
6065 * marked active but still in the qinfifo
6066 * is removed by the qinfifo scanning code
6069 if ((scb
->flags
& SCB_ACTIVE
) != 0)
6072 if (ahc_match_scb(ahc
, scb
, target
, channel
, lun
,
6073 SCB_LIST_NULL
, ROLE_INITIATOR
) == 0
6074 || (ctx
!= NULL
&& ctx
!= scb
->io_ctx
))
6078 * We found an scb that needs to be acted on.
6082 case SEARCH_COMPLETE
:
6087 ostat
= ahc_get_transaction_status(scb
);
6088 if (ostat
== CAM_REQ_INPROG
)
6089 ahc_set_transaction_status(scb
, status
);
6090 cstat
= ahc_get_transaction_status(scb
);
6091 if (cstat
!= CAM_REQ_CMP
)
6092 ahc_freeze_scb(scb
);
6093 if ((scb
->flags
& SCB_ACTIVE
) == 0)
6094 printk("Inactive SCB in untaggedQ\n");
6099 scb
->flags
&= ~SCB_UNTAGGEDQ
;
6100 TAILQ_REMOVE(untagged_q
, scb
, links
.tqe
);
6108 if (action
== SEARCH_COMPLETE
)
6109 ahc_release_untagged_queues(ahc
);
6114 ahc_search_disc_list(struct ahc_softc
*ahc
, int target
, char channel
,
6115 int lun
, u_int tag
, int stop_on_first
, int remove
,
6125 next
= ahc_inb(ahc
, DISCONNECTED_SCBH
);
6126 prev
= SCB_LIST_NULL
;
6129 /* restore this when we're done */
6130 active_scb
= ahc_inb(ahc
, SCBPTR
);
6132 /* Silence compiler */
6133 active_scb
= SCB_LIST_NULL
;
6135 while (next
!= SCB_LIST_NULL
) {
6138 ahc_outb(ahc
, SCBPTR
, next
);
6139 scb_index
= ahc_inb(ahc
, SCB_TAG
);
6140 if (scb_index
>= ahc
->scb_data
->numscbs
) {
6141 printk("Disconnected List inconsistency. "
6142 "SCB index == %d, yet numscbs == %d.",
6143 scb_index
, ahc
->scb_data
->numscbs
);
6144 ahc_dump_card_state(ahc
);
6145 panic("for safety");
6149 panic("Disconnected List Loop. "
6150 "cur SCBPTR == %x, prev SCBPTR == %x.",
6153 scbp
= ahc_lookup_scb(ahc
, scb_index
);
6154 if (ahc_match_scb(ahc
, scbp
, target
, channel
, lun
,
6155 tag
, ROLE_INITIATOR
)) {
6159 ahc_rem_scb_from_disc_list(ahc
, prev
, next
);
6162 next
= ahc_inb(ahc
, SCB_NEXT
);
6168 next
= ahc_inb(ahc
, SCB_NEXT
);
6172 ahc_outb(ahc
, SCBPTR
, active_scb
);
6177 * Remove an SCB from the on chip list of disconnected transactions.
6178 * This is empty/unused if we are not performing SCB paging.
6181 ahc_rem_scb_from_disc_list(struct ahc_softc
*ahc
, u_int prev
, u_int scbptr
)
6185 ahc_outb(ahc
, SCBPTR
, scbptr
);
6186 next
= ahc_inb(ahc
, SCB_NEXT
);
6188 ahc_outb(ahc
, SCB_CONTROL
, 0);
6190 ahc_add_curscb_to_free_list(ahc
);
6192 if (prev
!= SCB_LIST_NULL
) {
6193 ahc_outb(ahc
, SCBPTR
, prev
);
6194 ahc_outb(ahc
, SCB_NEXT
, next
);
6196 ahc_outb(ahc
, DISCONNECTED_SCBH
, next
);
6202 * Add the SCB as selected by SCBPTR onto the on chip list of
6203 * free hardware SCBs. This list is empty/unused if we are not
6204 * performing SCB paging.
6207 ahc_add_curscb_to_free_list(struct ahc_softc
*ahc
)
6210 * Invalidate the tag so that our abort
6211 * routines don't think it's active.
6213 ahc_outb(ahc
, SCB_TAG
, SCB_LIST_NULL
);
6215 if ((ahc
->flags
& AHC_PAGESCBS
) != 0) {
6216 ahc_outb(ahc
, SCB_NEXT
, ahc_inb(ahc
, FREE_SCBH
));
6217 ahc_outb(ahc
, FREE_SCBH
, ahc_inb(ahc
, SCBPTR
));
6222 * Manipulate the waiting for selection list and return the
6223 * scb that follows the one that we remove.
6226 ahc_rem_wscb(struct ahc_softc
*ahc
, u_int scbpos
, u_int prev
)
6231 * Select the SCB we want to abort and
6232 * pull the next pointer out of it.
6234 curscb
= ahc_inb(ahc
, SCBPTR
);
6235 ahc_outb(ahc
, SCBPTR
, scbpos
);
6236 next
= ahc_inb(ahc
, SCB_NEXT
);
6238 /* Clear the necessary fields */
6239 ahc_outb(ahc
, SCB_CONTROL
, 0);
6241 ahc_add_curscb_to_free_list(ahc
);
6243 /* update the waiting list */
6244 if (prev
== SCB_LIST_NULL
) {
6245 /* First in the list */
6246 ahc_outb(ahc
, WAITING_SCBH
, next
);
6249 * Ensure we aren't attempting to perform
6250 * selection for this entry.
6252 ahc_outb(ahc
, SCSISEQ
, (ahc_inb(ahc
, SCSISEQ
) & ~ENSELO
));
6255 * Select the scb that pointed to us
6256 * and update its next pointer.
6258 ahc_outb(ahc
, SCBPTR
, prev
);
6259 ahc_outb(ahc
, SCB_NEXT
, next
);
6263 * Point us back at the original scb position.
6265 ahc_outb(ahc
, SCBPTR
, curscb
);
6269 /******************************** Error Handling ******************************/
6271 * Abort all SCBs that match the given description (target/channel/lun/tag),
6272 * setting their status to the passed in status if the status has not already
6273 * been modified from CAM_REQ_INPROG. This routine assumes that the sequencer
6274 * is paused before it is called.
6277 ahc_abort_scbs(struct ahc_softc
*ahc
, int target
, char channel
,
6278 int lun
, u_int tag
, role_t role
, uint32_t status
)
6281 struct scb
*scbp_next
;
6291 * Don't attempt to run any queued untagged transactions
6292 * until we are done with the abort process.
6294 ahc_freeze_untagged_queues(ahc
);
6296 /* restore this when we're done */
6297 active_scb
= ahc_inb(ahc
, SCBPTR
);
6299 found
= ahc_search_qinfifo(ahc
, target
, channel
, lun
, SCB_LIST_NULL
,
6300 role
, CAM_REQUEUE_REQ
, SEARCH_COMPLETE
);
6303 * Clean out the busy target table for any untagged commands.
6307 if (target
!= CAM_TARGET_WILDCARD
) {
6314 if (lun
== CAM_LUN_WILDCARD
) {
6317 * Unless we are using an SCB based
6318 * busy targets table, there is only
6319 * one table entry for all luns of
6324 if ((ahc
->flags
& AHC_SCB_BTT
) != 0)
6325 maxlun
= AHC_NUM_LUNS
;
6331 if (role
!= ROLE_TARGET
) {
6332 for (;i
< maxtarget
; i
++) {
6333 for (j
= minlun
;j
< maxlun
; j
++) {
6337 tcl
= BUILD_TCL(i
<< 4, j
);
6338 scbid
= ahc_index_busy_tcl(ahc
, tcl
);
6339 scbp
= ahc_lookup_scb(ahc
, scbid
);
6341 || ahc_match_scb(ahc
, scbp
, target
, channel
,
6342 lun
, tag
, role
) == 0)
6344 ahc_unbusy_tcl(ahc
, BUILD_TCL(i
<< 4, j
));
6349 * Go through the disconnected list and remove any entries we
6350 * have queued for completion, 0'ing their control byte too.
6351 * We save the active SCB and restore it ourselves, so there
6352 * is no reason for this search to restore it too.
6354 ahc_search_disc_list(ahc
, target
, channel
, lun
, tag
,
6355 /*stop_on_first*/FALSE
, /*remove*/TRUE
,
6356 /*save_state*/FALSE
);
6360 * Go through the hardware SCB array looking for commands that
6361 * were active but not on any list. In some cases, these remnants
6362 * might not still have mappings in the scbindex array (e.g. unexpected
6363 * bus free with the same scb queued for an abort). Don't hold this
6366 for (i
= 0; i
< ahc
->scb_data
->maxhscbs
; i
++) {
6369 ahc_outb(ahc
, SCBPTR
, i
);
6370 scbid
= ahc_inb(ahc
, SCB_TAG
);
6371 scbp
= ahc_lookup_scb(ahc
, scbid
);
6372 if ((scbp
== NULL
&& scbid
!= SCB_LIST_NULL
)
6374 && ahc_match_scb(ahc
, scbp
, target
, channel
, lun
, tag
, role
)))
6375 ahc_add_curscb_to_free_list(ahc
);
6379 * Go through the pending CCB list and look for
6380 * commands for this target that are still active.
6381 * These are other tagged commands that were
6382 * disconnected when the reset occurred.
6384 scbp_next
= LIST_FIRST(&ahc
->pending_scbs
);
6385 while (scbp_next
!= NULL
) {
6387 scbp_next
= LIST_NEXT(scbp
, pending_links
);
6388 if (ahc_match_scb(ahc
, scbp
, target
, channel
, lun
, tag
, role
)) {
6391 ostat
= ahc_get_transaction_status(scbp
);
6392 if (ostat
== CAM_REQ_INPROG
)
6393 ahc_set_transaction_status(scbp
, status
);
6394 if (ahc_get_transaction_status(scbp
) != CAM_REQ_CMP
)
6395 ahc_freeze_scb(scbp
);
6396 if ((scbp
->flags
& SCB_ACTIVE
) == 0)
6397 printk("Inactive SCB on pending list\n");
6398 ahc_done(ahc
, scbp
);
6402 ahc_outb(ahc
, SCBPTR
, active_scb
);
6403 ahc_platform_abort_scbs(ahc
, target
, channel
, lun
, tag
, role
, status
);
6404 ahc_release_untagged_queues(ahc
);
6409 ahc_reset_current_bus(struct ahc_softc
*ahc
)
6413 ahc_outb(ahc
, SIMODE1
, ahc_inb(ahc
, SIMODE1
) & ~ENSCSIRST
);
6414 scsiseq
= ahc_inb(ahc
, SCSISEQ
);
6415 ahc_outb(ahc
, SCSISEQ
, scsiseq
| SCSIRSTO
);
6416 ahc_flush_device_writes(ahc
);
6417 ahc_delay(AHC_BUSRESET_DELAY
);
6418 /* Turn off the bus reset */
6419 ahc_outb(ahc
, SCSISEQ
, scsiseq
& ~SCSIRSTO
);
6421 ahc_clear_intstat(ahc
);
6423 /* Re-enable reset interrupts */
6424 ahc_outb(ahc
, SIMODE1
, ahc_inb(ahc
, SIMODE1
) | ENSCSIRST
);
6428 ahc_reset_channel(struct ahc_softc
*ahc
, char channel
, int initiate_reset
)
6430 struct ahc_devinfo devinfo
;
6431 u_int initiator
, target
, max_scsiid
;
6439 ahc
->pending_device
= NULL
;
6441 ahc_compile_devinfo(&devinfo
,
6442 CAM_TARGET_WILDCARD
,
6443 CAM_TARGET_WILDCARD
,
6445 channel
, ROLE_UNKNOWN
);
6448 /* Make sure the sequencer is in a safe location. */
6449 ahc_clear_critical_section(ahc
);
6452 * Run our command complete fifos to ensure that we perform
6453 * completion processing on any commands that 'completed'
6454 * before the reset occurred.
6456 ahc_run_qoutfifo(ahc
);
6457 #ifdef AHC_TARGET_MODE
6458 if ((ahc
->flags
& AHC_TARGETROLE
) != 0) {
6459 ahc_run_tqinfifo(ahc
, /*paused*/TRUE
);
6464 * Reset the bus if we are initiating this reset
6466 sblkctl
= ahc_inb(ahc
, SBLKCTL
);
6468 if ((ahc
->features
& AHC_TWIN
) != 0
6469 && ((sblkctl
& SELBUSB
) != 0))
6471 scsiseq
= ahc_inb(ahc
, SCSISEQ_TEMPLATE
);
6472 if (cur_channel
!= channel
) {
6473 /* Case 1: Command for another bus is active
6474 * Stealthily reset the other bus without
6475 * upsetting the current bus.
6477 ahc_outb(ahc
, SBLKCTL
, sblkctl
^ SELBUSB
);
6478 simode1
= ahc_inb(ahc
, SIMODE1
) & ~(ENBUSFREE
|ENSCSIRST
);
6479 #ifdef AHC_TARGET_MODE
6481 * Bus resets clear ENSELI, so we cannot
6482 * defer re-enabling bus reset interrupts
6483 * if we are in target mode.
6485 if ((ahc
->flags
& AHC_TARGETROLE
) != 0)
6486 simode1
|= ENSCSIRST
;
6488 ahc_outb(ahc
, SIMODE1
, simode1
);
6490 ahc_reset_current_bus(ahc
);
6491 ahc_clear_intstat(ahc
);
6492 ahc_outb(ahc
, SCSISEQ
, scsiseq
& (ENSELI
|ENRSELI
|ENAUTOATNP
));
6493 ahc_outb(ahc
, SBLKCTL
, sblkctl
);
6494 restart_needed
= FALSE
;
6496 /* Case 2: A command from this bus is active or we're idle */
6497 simode1
= ahc_inb(ahc
, SIMODE1
) & ~(ENBUSFREE
|ENSCSIRST
);
6498 #ifdef AHC_TARGET_MODE
6500 * Bus resets clear ENSELI, so we cannot
6501 * defer re-enabling bus reset interrupts
6502 * if we are in target mode.
6504 if ((ahc
->flags
& AHC_TARGETROLE
) != 0)
6505 simode1
|= ENSCSIRST
;
6507 ahc_outb(ahc
, SIMODE1
, simode1
);
6509 ahc_reset_current_bus(ahc
);
6510 ahc_clear_intstat(ahc
);
6511 ahc_outb(ahc
, SCSISEQ
, scsiseq
& (ENSELI
|ENRSELI
|ENAUTOATNP
));
6512 restart_needed
= TRUE
;
6516 * Clean up all the state information for the
6517 * pending transactions on this bus.
6519 found
= ahc_abort_scbs(ahc
, CAM_TARGET_WILDCARD
, channel
,
6520 CAM_LUN_WILDCARD
, SCB_LIST_NULL
,
6521 ROLE_UNKNOWN
, CAM_SCSI_BUS_RESET
);
6523 max_scsiid
= (ahc
->features
& AHC_WIDE
) ? 15 : 7;
6525 #ifdef AHC_TARGET_MODE
6527 * Send an immediate notify ccb to all target more peripheral
6528 * drivers affected by this action.
6530 for (target
= 0; target
<= max_scsiid
; target
++) {
6531 struct ahc_tmode_tstate
* tstate
;
6534 tstate
= ahc
->enabled_targets
[target
];
6537 for (lun
= 0; lun
< AHC_NUM_LUNS
; lun
++) {
6538 struct ahc_tmode_lstate
* lstate
;
6540 lstate
= tstate
->enabled_luns
[lun
];
6544 ahc_queue_lstate_event(ahc
, lstate
, CAM_TARGET_WILDCARD
,
6545 EVENT_TYPE_BUS_RESET
, /*arg*/0);
6546 ahc_send_lstate_events(ahc
, lstate
);
6550 /* Notify the XPT that a bus reset occurred */
6551 ahc_send_async(ahc
, devinfo
.channel
, CAM_TARGET_WILDCARD
,
6552 CAM_LUN_WILDCARD
, AC_BUS_RESET
);
6555 * Revert to async/narrow transfers until we renegotiate.
6557 for (target
= 0; target
<= max_scsiid
; target
++) {
6559 if (ahc
->enabled_targets
[target
] == NULL
)
6561 for (initiator
= 0; initiator
<= max_scsiid
; initiator
++) {
6562 struct ahc_devinfo devinfo
;
6564 ahc_compile_devinfo(&devinfo
, target
, initiator
,
6566 channel
, ROLE_UNKNOWN
);
6567 ahc_set_width(ahc
, &devinfo
, MSG_EXT_WDTR_BUS_8_BIT
,
6568 AHC_TRANS_CUR
, /*paused*/TRUE
);
6569 ahc_set_syncrate(ahc
, &devinfo
, /*syncrate*/NULL
,
6570 /*period*/0, /*offset*/0,
6571 /*ppr_options*/0, AHC_TRANS_CUR
,
6584 /***************************** Residual Processing ****************************/
6586 * Calculate the residual for a just completed SCB.
6589 ahc_calc_residual(struct ahc_softc
*ahc
, struct scb
*scb
)
6591 struct hardware_scb
*hscb
;
6592 struct status_pkt
*spkt
;
6594 uint32_t resid_sgptr
;
6600 * SG_RESID_VALID clear in sgptr.
6601 * 2) Transferless command
6602 * 3) Never performed any transfers.
6603 * sgptr has SG_FULL_RESID set.
6604 * 4) No residual but target did not
6605 * save data pointers after the
6606 * last transfer, so sgptr was
6608 * 5) We have a partial residual.
6609 * Use residual_sgptr to determine
6614 sgptr
= ahc_le32toh(hscb
->sgptr
);
6615 if ((sgptr
& SG_RESID_VALID
) == 0)
6618 sgptr
&= ~SG_RESID_VALID
;
6620 if ((sgptr
& SG_LIST_NULL
) != 0)
6624 spkt
= &hscb
->shared_data
.status
;
6625 resid_sgptr
= ahc_le32toh(spkt
->residual_sg_ptr
);
6626 if ((sgptr
& SG_FULL_RESID
) != 0) {
6628 resid
= ahc_get_transfer_length(scb
);
6629 } else if ((resid_sgptr
& SG_LIST_NULL
) != 0) {
6632 } else if ((resid_sgptr
& ~SG_PTR_MASK
) != 0) {
6633 panic("Bogus resid sgptr value 0x%x\n", resid_sgptr
);
6635 struct ahc_dma_seg
*sg
;
6638 * Remainder of the SG where the transfer
6641 resid
= ahc_le32toh(spkt
->residual_datacnt
) & AHC_SG_LEN_MASK
;
6642 sg
= ahc_sg_bus_to_virt(scb
, resid_sgptr
& SG_PTR_MASK
);
6644 /* The residual sg_ptr always points to the next sg */
6648 * Add up the contents of all residual
6649 * SG segments that are after the SG where
6650 * the transfer stopped.
6652 while ((ahc_le32toh(sg
->len
) & AHC_DMA_LAST_SEG
) == 0) {
6654 resid
+= ahc_le32toh(sg
->len
) & AHC_SG_LEN_MASK
;
6657 if ((scb
->flags
& SCB_SENSE
) == 0)
6658 ahc_set_residual(scb
, resid
);
6660 ahc_set_sense_residual(scb
, resid
);
6663 if ((ahc_debug
& AHC_SHOW_MISC
) != 0) {
6664 ahc_print_path(ahc
, scb
);
6665 printk("Handled %sResidual of %d bytes\n",
6666 (scb
->flags
& SCB_SENSE
) ? "Sense " : "", resid
);
6671 /******************************* Target Mode **********************************/
6672 #ifdef AHC_TARGET_MODE
6674 * Add a target mode event to this lun's queue
6677 ahc_queue_lstate_event(struct ahc_softc
*ahc
, struct ahc_tmode_lstate
*lstate
,
6678 u_int initiator_id
, u_int event_type
, u_int event_arg
)
6680 struct ahc_tmode_event
*event
;
6683 xpt_freeze_devq(lstate
->path
, /*count*/1);
6684 if (lstate
->event_w_idx
>= lstate
->event_r_idx
)
6685 pending
= lstate
->event_w_idx
- lstate
->event_r_idx
;
6687 pending
= AHC_TMODE_EVENT_BUFFER_SIZE
+ 1
6688 - (lstate
->event_r_idx
- lstate
->event_w_idx
);
6690 if (event_type
== EVENT_TYPE_BUS_RESET
6691 || event_type
== MSG_BUS_DEV_RESET
) {
6693 * Any earlier events are irrelevant, so reset our buffer.
6694 * This has the effect of allowing us to deal with reset
6695 * floods (an external device holding down the reset line)
6696 * without losing the event that is really interesting.
6698 lstate
->event_r_idx
= 0;
6699 lstate
->event_w_idx
= 0;
6700 xpt_release_devq(lstate
->path
, pending
, /*runqueue*/FALSE
);
6703 if (pending
== AHC_TMODE_EVENT_BUFFER_SIZE
) {
6704 xpt_print_path(lstate
->path
);
6705 printk("immediate event %x:%x lost\n",
6706 lstate
->event_buffer
[lstate
->event_r_idx
].event_type
,
6707 lstate
->event_buffer
[lstate
->event_r_idx
].event_arg
);
6708 lstate
->event_r_idx
++;
6709 if (lstate
->event_r_idx
== AHC_TMODE_EVENT_BUFFER_SIZE
)
6710 lstate
->event_r_idx
= 0;
6711 xpt_release_devq(lstate
->path
, /*count*/1, /*runqueue*/FALSE
);
6714 event
= &lstate
->event_buffer
[lstate
->event_w_idx
];
6715 event
->initiator_id
= initiator_id
;
6716 event
->event_type
= event_type
;
6717 event
->event_arg
= event_arg
;
6718 lstate
->event_w_idx
++;
6719 if (lstate
->event_w_idx
== AHC_TMODE_EVENT_BUFFER_SIZE
)
6720 lstate
->event_w_idx
= 0;
6724 * Send any target mode events queued up waiting
6725 * for immediate notify resources.
6728 ahc_send_lstate_events(struct ahc_softc
*ahc
, struct ahc_tmode_lstate
*lstate
)
6730 struct ccb_hdr
*ccbh
;
6731 struct ccb_immed_notify
*inot
;
6733 while (lstate
->event_r_idx
!= lstate
->event_w_idx
6734 && (ccbh
= SLIST_FIRST(&lstate
->immed_notifies
)) != NULL
) {
6735 struct ahc_tmode_event
*event
;
6737 event
= &lstate
->event_buffer
[lstate
->event_r_idx
];
6738 SLIST_REMOVE_HEAD(&lstate
->immed_notifies
, sim_links
.sle
);
6739 inot
= (struct ccb_immed_notify
*)ccbh
;
6740 switch (event
->event_type
) {
6741 case EVENT_TYPE_BUS_RESET
:
6742 ccbh
->status
= CAM_SCSI_BUS_RESET
|CAM_DEV_QFRZN
;
6745 ccbh
->status
= CAM_MESSAGE_RECV
|CAM_DEV_QFRZN
;
6746 inot
->message_args
[0] = event
->event_type
;
6747 inot
->message_args
[1] = event
->event_arg
;
6750 inot
->initiator_id
= event
->initiator_id
;
6751 inot
->sense_len
= 0;
6752 xpt_done((union ccb
*)inot
);
6753 lstate
->event_r_idx
++;
6754 if (lstate
->event_r_idx
== AHC_TMODE_EVENT_BUFFER_SIZE
)
6755 lstate
->event_r_idx
= 0;
6760 /******************** Sequencer Program Patching/Download *********************/
6764 ahc_dumpseq(struct ahc_softc
* ahc
)
6768 ahc_outb(ahc
, SEQCTL
, PERRORDIS
|FAILDIS
|FASTMODE
|LOADRAM
);
6769 ahc_outb(ahc
, SEQADDR0
, 0);
6770 ahc_outb(ahc
, SEQADDR1
, 0);
6771 for (i
= 0; i
< ahc
->instruction_ram_size
; i
++) {
6772 uint8_t ins_bytes
[4];
6774 ahc_insb(ahc
, SEQRAM
, ins_bytes
, 4);
6775 printk("0x%08x\n", ins_bytes
[0] << 24
6776 | ins_bytes
[1] << 16
6784 ahc_loadseq(struct ahc_softc
*ahc
)
6786 struct cs cs_table
[num_critical_sections
];
6787 u_int begin_set
[num_critical_sections
];
6788 u_int end_set
[num_critical_sections
];
6789 const struct patch
*cur_patch
;
6794 u_int sg_prefetch_cnt
;
6796 uint8_t download_consts
[7];
6799 * Start out with 0 critical sections
6800 * that apply to this firmware load.
6804 memset(begin_set
, 0, sizeof(begin_set
));
6805 memset(end_set
, 0, sizeof(end_set
));
6807 /* Setup downloadable constant table */
6808 download_consts
[QOUTFIFO_OFFSET
] = 0;
6809 if (ahc
->targetcmds
!= NULL
)
6810 download_consts
[QOUTFIFO_OFFSET
] += 32;
6811 download_consts
[QINFIFO_OFFSET
] = download_consts
[QOUTFIFO_OFFSET
] + 1;
6812 download_consts
[CACHESIZE_MASK
] = ahc
->pci_cachesize
- 1;
6813 download_consts
[INVERTED_CACHESIZE_MASK
] = ~(ahc
->pci_cachesize
- 1);
6814 sg_prefetch_cnt
= ahc
->pci_cachesize
;
6815 if (sg_prefetch_cnt
< (2 * sizeof(struct ahc_dma_seg
)))
6816 sg_prefetch_cnt
= 2 * sizeof(struct ahc_dma_seg
);
6817 download_consts
[SG_PREFETCH_CNT
] = sg_prefetch_cnt
;
6818 download_consts
[SG_PREFETCH_ALIGN_MASK
] = ~(sg_prefetch_cnt
- 1);
6819 download_consts
[SG_PREFETCH_ADDR_MASK
] = (sg_prefetch_cnt
- 1);
6821 cur_patch
= patches
;
6824 ahc_outb(ahc
, SEQCTL
, PERRORDIS
|FAILDIS
|FASTMODE
|LOADRAM
);
6825 ahc_outb(ahc
, SEQADDR0
, 0);
6826 ahc_outb(ahc
, SEQADDR1
, 0);
6828 for (i
= 0; i
< sizeof(seqprog
)/4; i
++) {
6829 if (ahc_check_patch(ahc
, &cur_patch
, i
, &skip_addr
) == 0) {
6831 * Don't download this instruction as it
6832 * is in a patch that was removed.
6837 if (downloaded
== ahc
->instruction_ram_size
) {
6839 * We're about to exceed the instruction
6840 * storage capacity for this chip. Fail
6843 printk("\n%s: Program too large for instruction memory "
6844 "size of %d!\n", ahc_name(ahc
),
6845 ahc
->instruction_ram_size
);
6850 * Move through the CS table until we find a CS
6851 * that might apply to this instruction.
6853 for (; cur_cs
< num_critical_sections
; cur_cs
++) {
6854 if (critical_sections
[cur_cs
].end
<= i
) {
6855 if (begin_set
[cs_count
] == TRUE
6856 && end_set
[cs_count
] == FALSE
) {
6857 cs_table
[cs_count
].end
= downloaded
;
6858 end_set
[cs_count
] = TRUE
;
6863 if (critical_sections
[cur_cs
].begin
<= i
6864 && begin_set
[cs_count
] == FALSE
) {
6865 cs_table
[cs_count
].begin
= downloaded
;
6866 begin_set
[cs_count
] = TRUE
;
6870 ahc_download_instr(ahc
, i
, download_consts
);
6874 ahc
->num_critical_sections
= cs_count
;
6875 if (cs_count
!= 0) {
6877 cs_count
*= sizeof(struct cs
);
6878 ahc
->critical_sections
= kmalloc(cs_count
, GFP_ATOMIC
);
6879 if (ahc
->critical_sections
== NULL
)
6880 panic("ahc_loadseq: Could not malloc");
6881 memcpy(ahc
->critical_sections
, cs_table
, cs_count
);
6883 ahc_outb(ahc
, SEQCTL
, PERRORDIS
|FAILDIS
|FASTMODE
);
6886 printk(" %d instructions downloaded\n", downloaded
);
6887 printk("%s: Features 0x%x, Bugs 0x%x, Flags 0x%x\n",
6888 ahc_name(ahc
), ahc
->features
, ahc
->bugs
, ahc
->flags
);
6894 ahc_check_patch(struct ahc_softc
*ahc
, const struct patch
**start_patch
,
6895 u_int start_instr
, u_int
*skip_addr
)
6897 const struct patch
*cur_patch
;
6898 const struct patch
*last_patch
;
6901 num_patches
= ARRAY_SIZE(patches
);
6902 last_patch
= &patches
[num_patches
];
6903 cur_patch
= *start_patch
;
6905 while (cur_patch
< last_patch
&& start_instr
== cur_patch
->begin
) {
6907 if (cur_patch
->patch_func(ahc
) == 0) {
6909 /* Start rejecting code */
6910 *skip_addr
= start_instr
+ cur_patch
->skip_instr
;
6911 cur_patch
+= cur_patch
->skip_patch
;
6913 /* Accepted this patch. Advance to the next
6914 * one and wait for our intruction pointer to
6921 *start_patch
= cur_patch
;
6922 if (start_instr
< *skip_addr
)
6923 /* Still skipping */
6930 ahc_download_instr(struct ahc_softc
*ahc
, u_int instrptr
, uint8_t *dconsts
)
6932 union ins_formats instr
;
6933 struct ins_format1
*fmt1_ins
;
6934 struct ins_format3
*fmt3_ins
;
6938 * The firmware is always compiled into a little endian format.
6940 instr
.integer
= ahc_le32toh(*(uint32_t*)&seqprog
[instrptr
* 4]);
6942 fmt1_ins
= &instr
.format1
;
6945 /* Pull the opcode */
6946 opcode
= instr
.format1
.opcode
;
6957 const struct patch
*cur_patch
;
6963 fmt3_ins
= &instr
.format3
;
6965 address
= fmt3_ins
->address
;
6966 cur_patch
= patches
;
6969 for (i
= 0; i
< address
;) {
6971 ahc_check_patch(ahc
, &cur_patch
, i
, &skip_addr
);
6973 if (skip_addr
> i
) {
6976 end_addr
= min(address
, skip_addr
);
6977 address_offset
+= end_addr
- i
;
6983 address
-= address_offset
;
6984 fmt3_ins
->address
= address
;
6993 if (fmt1_ins
->parity
!= 0) {
6994 fmt1_ins
->immediate
= dconsts
[fmt1_ins
->immediate
];
6996 fmt1_ins
->parity
= 0;
6997 if ((ahc
->features
& AHC_CMD_CHAN
) == 0
6998 && opcode
== AIC_OP_BMOV
) {
7000 * Block move was added at the same time
7001 * as the command channel. Verify that
7002 * this is only a move of a single element
7003 * and convert the BMOV to a MOV
7004 * (AND with an immediate of FF).
7006 if (fmt1_ins
->immediate
!= 1)
7007 panic("%s: BMOV not supported\n",
7009 fmt1_ins
->opcode
= AIC_OP_AND
;
7010 fmt1_ins
->immediate
= 0xff;
7014 if ((ahc
->features
& AHC_ULTRA2
) != 0) {
7017 /* Calculate odd parity for the instruction */
7018 for (i
= 0, count
= 0; i
< 31; i
++) {
7022 if ((instr
.integer
& mask
) != 0)
7025 if ((count
& 0x01) == 0)
7026 instr
.format1
.parity
= 1;
7028 /* Compress the instruction for older sequencers */
7029 if (fmt3_ins
!= NULL
) {
7032 | (fmt3_ins
->source
<< 8)
7033 | (fmt3_ins
->address
<< 16)
7034 | (fmt3_ins
->opcode
<< 25);
7038 | (fmt1_ins
->source
<< 8)
7039 | (fmt1_ins
->destination
<< 16)
7040 | (fmt1_ins
->ret
<< 24)
7041 | (fmt1_ins
->opcode
<< 25);
7044 /* The sequencer is a little endian cpu */
7045 instr
.integer
= ahc_htole32(instr
.integer
);
7046 ahc_outsb(ahc
, SEQRAM
, instr
.bytes
, 4);
7049 panic("Unknown opcode encountered in seq program");
7055 ahc_print_register(const ahc_reg_parse_entry_t
*table
, u_int num_entries
,
7056 const char *name
, u_int address
, u_int value
,
7057 u_int
*cur_column
, u_int wrap_point
)
7062 if (cur_column
!= NULL
&& *cur_column
>= wrap_point
) {
7066 printed
= printk("%s[0x%x]", name
, value
);
7067 if (table
== NULL
) {
7068 printed
+= printk(" ");
7069 *cur_column
+= printed
;
7073 while (printed_mask
!= 0xFF) {
7076 for (entry
= 0; entry
< num_entries
; entry
++) {
7077 if (((value
& table
[entry
].mask
)
7078 != table
[entry
].value
)
7079 || ((printed_mask
& table
[entry
].mask
)
7080 == table
[entry
].mask
))
7083 printed
+= printk("%s%s",
7084 printed_mask
== 0 ? ":(" : "|",
7086 printed_mask
|= table
[entry
].mask
;
7090 if (entry
>= num_entries
)
7093 if (printed_mask
!= 0)
7094 printed
+= printk(") ");
7096 printed
+= printk(" ");
7097 if (cur_column
!= NULL
)
7098 *cur_column
+= printed
;
7103 ahc_dump_card_state(struct ahc_softc
*ahc
)
7106 struct scb_tailq
*untagged_q
;
7117 uint8_t saved_scbptr
;
7119 if (ahc_is_paused(ahc
)) {
7126 saved_scbptr
= ahc_inb(ahc
, SCBPTR
);
7127 last_phase
= ahc_inb(ahc
, LASTPHASE
);
7128 printk(">>>>>>>>>>>>>>>>>> Dump Card State Begins <<<<<<<<<<<<<<<<<\n"
7129 "%s: Dumping Card State %s, at SEQADDR 0x%x\n",
7130 ahc_name(ahc
), ahc_lookup_phase_entry(last_phase
)->phasemsg
,
7131 ahc_inb(ahc
, SEQADDR0
) | (ahc_inb(ahc
, SEQADDR1
) << 8));
7133 printk("Card was paused\n");
7134 printk("ACCUM = 0x%x, SINDEX = 0x%x, DINDEX = 0x%x, ARG_2 = 0x%x\n",
7135 ahc_inb(ahc
, ACCUM
), ahc_inb(ahc
, SINDEX
), ahc_inb(ahc
, DINDEX
),
7136 ahc_inb(ahc
, ARG_2
));
7137 printk("HCNT = 0x%x SCBPTR = 0x%x\n", ahc_inb(ahc
, HCNT
),
7138 ahc_inb(ahc
, SCBPTR
));
7140 if ((ahc
->features
& AHC_DT
) != 0)
7141 ahc_scsiphase_print(ahc_inb(ahc
, SCSIPHASE
), &cur_col
, 50);
7142 ahc_scsisigi_print(ahc_inb(ahc
, SCSISIGI
), &cur_col
, 50);
7143 ahc_error_print(ahc_inb(ahc
, ERROR
), &cur_col
, 50);
7144 ahc_scsibusl_print(ahc_inb(ahc
, SCSIBUSL
), &cur_col
, 50);
7145 ahc_lastphase_print(ahc_inb(ahc
, LASTPHASE
), &cur_col
, 50);
7146 ahc_scsiseq_print(ahc_inb(ahc
, SCSISEQ
), &cur_col
, 50);
7147 ahc_sblkctl_print(ahc_inb(ahc
, SBLKCTL
), &cur_col
, 50);
7148 ahc_scsirate_print(ahc_inb(ahc
, SCSIRATE
), &cur_col
, 50);
7149 ahc_seqctl_print(ahc_inb(ahc
, SEQCTL
), &cur_col
, 50);
7150 ahc_seq_flags_print(ahc_inb(ahc
, SEQ_FLAGS
), &cur_col
, 50);
7151 ahc_sstat0_print(ahc_inb(ahc
, SSTAT0
), &cur_col
, 50);
7152 ahc_sstat1_print(ahc_inb(ahc
, SSTAT1
), &cur_col
, 50);
7153 ahc_sstat2_print(ahc_inb(ahc
, SSTAT2
), &cur_col
, 50);
7154 ahc_sstat3_print(ahc_inb(ahc
, SSTAT3
), &cur_col
, 50);
7155 ahc_simode0_print(ahc_inb(ahc
, SIMODE0
), &cur_col
, 50);
7156 ahc_simode1_print(ahc_inb(ahc
, SIMODE1
), &cur_col
, 50);
7157 ahc_sxfrctl0_print(ahc_inb(ahc
, SXFRCTL0
), &cur_col
, 50);
7158 ahc_dfcntrl_print(ahc_inb(ahc
, DFCNTRL
), &cur_col
, 50);
7159 ahc_dfstatus_print(ahc_inb(ahc
, DFSTATUS
), &cur_col
, 50);
7163 for (i
= 0; i
< STACK_SIZE
; i
++)
7164 printk(" 0x%x", ahc_inb(ahc
, STACK
)|(ahc_inb(ahc
, STACK
) << 8));
7165 printk("\nSCB count = %d\n", ahc
->scb_data
->numscbs
);
7166 printk("Kernel NEXTQSCB = %d\n", ahc
->next_queued_scb
->hscb
->tag
);
7167 printk("Card NEXTQSCB = %d\n", ahc_inb(ahc
, NEXT_QUEUED_SCB
));
7169 printk("QINFIFO entries: ");
7170 if ((ahc
->features
& AHC_QUEUE_REGS
) != 0) {
7171 qinpos
= ahc_inb(ahc
, SNSCB_QOFF
);
7172 ahc_outb(ahc
, SNSCB_QOFF
, qinpos
);
7174 qinpos
= ahc_inb(ahc
, QINPOS
);
7175 qintail
= ahc
->qinfifonext
;
7176 while (qinpos
!= qintail
) {
7177 printk("%d ", ahc
->qinfifo
[qinpos
]);
7182 printk("Waiting Queue entries: ");
7183 scb_index
= ahc_inb(ahc
, WAITING_SCBH
);
7185 while (scb_index
!= SCB_LIST_NULL
&& i
++ < 256) {
7186 ahc_outb(ahc
, SCBPTR
, scb_index
);
7187 printk("%d:%d ", scb_index
, ahc_inb(ahc
, SCB_TAG
));
7188 scb_index
= ahc_inb(ahc
, SCB_NEXT
);
7192 printk("Disconnected Queue entries: ");
7193 scb_index
= ahc_inb(ahc
, DISCONNECTED_SCBH
);
7195 while (scb_index
!= SCB_LIST_NULL
&& i
++ < 256) {
7196 ahc_outb(ahc
, SCBPTR
, scb_index
);
7197 printk("%d:%d ", scb_index
, ahc_inb(ahc
, SCB_TAG
));
7198 scb_index
= ahc_inb(ahc
, SCB_NEXT
);
7202 ahc_sync_qoutfifo(ahc
, BUS_DMASYNC_POSTREAD
);
7203 printk("QOUTFIFO entries: ");
7204 qoutpos
= ahc
->qoutfifonext
;
7206 while (ahc
->qoutfifo
[qoutpos
] != SCB_LIST_NULL
&& i
++ < 256) {
7207 printk("%d ", ahc
->qoutfifo
[qoutpos
]);
7212 printk("Sequencer Free SCB List: ");
7213 scb_index
= ahc_inb(ahc
, FREE_SCBH
);
7215 while (scb_index
!= SCB_LIST_NULL
&& i
++ < 256) {
7216 ahc_outb(ahc
, SCBPTR
, scb_index
);
7217 printk("%d ", scb_index
);
7218 scb_index
= ahc_inb(ahc
, SCB_NEXT
);
7222 printk("Sequencer SCB Info: ");
7223 for (i
= 0; i
< ahc
->scb_data
->maxhscbs
; i
++) {
7224 ahc_outb(ahc
, SCBPTR
, i
);
7225 cur_col
= printk("\n%3d ", i
);
7227 ahc_scb_control_print(ahc_inb(ahc
, SCB_CONTROL
), &cur_col
, 60);
7228 ahc_scb_scsiid_print(ahc_inb(ahc
, SCB_SCSIID
), &cur_col
, 60);
7229 ahc_scb_lun_print(ahc_inb(ahc
, SCB_LUN
), &cur_col
, 60);
7230 ahc_scb_tag_print(ahc_inb(ahc
, SCB_TAG
), &cur_col
, 60);
7234 printk("Pending list: ");
7236 LIST_FOREACH(scb
, &ahc
->pending_scbs
, pending_links
) {
7239 cur_col
= printk("\n%3d ", scb
->hscb
->tag
);
7240 ahc_scb_control_print(scb
->hscb
->control
, &cur_col
, 60);
7241 ahc_scb_scsiid_print(scb
->hscb
->scsiid
, &cur_col
, 60);
7242 ahc_scb_lun_print(scb
->hscb
->lun
, &cur_col
, 60);
7243 if ((ahc
->flags
& AHC_PAGESCBS
) == 0) {
7244 ahc_outb(ahc
, SCBPTR
, scb
->hscb
->tag
);
7246 ahc_scb_control_print(ahc_inb(ahc
, SCB_CONTROL
),
7248 ahc_scb_tag_print(ahc_inb(ahc
, SCB_TAG
), &cur_col
, 60);
7254 printk("Kernel Free SCB list: ");
7256 SLIST_FOREACH(scb
, &ahc
->scb_data
->free_scbs
, links
.sle
) {
7259 printk("%d ", scb
->hscb
->tag
);
7263 maxtarget
= (ahc
->features
& (AHC_WIDE
|AHC_TWIN
)) ? 15 : 7;
7264 for (target
= 0; target
<= maxtarget
; target
++) {
7265 untagged_q
= &ahc
->untagged_queues
[target
];
7266 if (TAILQ_FIRST(untagged_q
) == NULL
)
7268 printk("Untagged Q(%d): ", target
);
7270 TAILQ_FOREACH(scb
, untagged_q
, links
.tqe
) {
7273 printk("%d ", scb
->hscb
->tag
);
7278 ahc_platform_dump_card_state(ahc
);
7279 printk("\n<<<<<<<<<<<<<<<<< Dump Card State Ends >>>>>>>>>>>>>>>>>>\n");
7280 ahc_outb(ahc
, SCBPTR
, saved_scbptr
);
7285 /************************* Target Mode ****************************************/
7286 #ifdef AHC_TARGET_MODE
7288 ahc_find_tmode_devs(struct ahc_softc
*ahc
, struct cam_sim
*sim
, union ccb
*ccb
,
7289 struct ahc_tmode_tstate
**tstate
,
7290 struct ahc_tmode_lstate
**lstate
,
7291 int notfound_failure
)
7294 if ((ahc
->features
& AHC_TARGETMODE
) == 0)
7295 return (CAM_REQ_INVALID
);
7298 * Handle the 'black hole' device that sucks up
7299 * requests to unattached luns on enabled targets.
7301 if (ccb
->ccb_h
.target_id
== CAM_TARGET_WILDCARD
7302 && ccb
->ccb_h
.target_lun
== CAM_LUN_WILDCARD
) {
7304 *lstate
= ahc
->black_hole
;
7308 max_id
= (ahc
->features
& AHC_WIDE
) ? 16 : 8;
7309 if (ccb
->ccb_h
.target_id
>= max_id
)
7310 return (CAM_TID_INVALID
);
7312 if (ccb
->ccb_h
.target_lun
>= AHC_NUM_LUNS
)
7313 return (CAM_LUN_INVALID
);
7315 *tstate
= ahc
->enabled_targets
[ccb
->ccb_h
.target_id
];
7317 if (*tstate
!= NULL
)
7319 (*tstate
)->enabled_luns
[ccb
->ccb_h
.target_lun
];
7322 if (notfound_failure
!= 0 && *lstate
== NULL
)
7323 return (CAM_PATH_INVALID
);
7325 return (CAM_REQ_CMP
);
7329 ahc_handle_en_lun(struct ahc_softc
*ahc
, struct cam_sim
*sim
, union ccb
*ccb
)
7331 struct ahc_tmode_tstate
*tstate
;
7332 struct ahc_tmode_lstate
*lstate
;
7333 struct ccb_en_lun
*cel
;
7343 status
= ahc_find_tmode_devs(ahc
, sim
, ccb
, &tstate
, &lstate
,
7344 /*notfound_failure*/FALSE
);
7346 if (status
!= CAM_REQ_CMP
) {
7347 ccb
->ccb_h
.status
= status
;
7351 if (cam_sim_bus(sim
) == 0)
7352 our_id
= ahc
->our_id
;
7354 our_id
= ahc
->our_id_b
;
7356 if (ccb
->ccb_h
.target_id
!= our_id
) {
7358 * our_id represents our initiator ID, or
7359 * the ID of the first target to have an
7360 * enabled lun in target mode. There are
7361 * two cases that may preclude enabling a
7362 * target id other than our_id.
7364 * o our_id is for an active initiator role.
7365 * Since the hardware does not support
7366 * reselections to the initiator role at
7367 * anything other than our_id, and our_id
7368 * is used by the hardware to indicate the
7369 * ID to use for both select-out and
7370 * reselect-out operations, the only target
7371 * ID we can support in this mode is our_id.
7373 * o The MULTARGID feature is not available and
7374 * a previous target mode ID has been enabled.
7376 if ((ahc
->features
& AHC_MULTIROLE
) != 0) {
7378 if ((ahc
->features
& AHC_MULTI_TID
) != 0
7379 && (ahc
->flags
& AHC_INITIATORROLE
) != 0) {
7381 * Only allow additional targets if
7382 * the initiator role is disabled.
7383 * The hardware cannot handle a re-select-in
7384 * on the initiator id during a re-select-out
7385 * on a different target id.
7387 status
= CAM_TID_INVALID
;
7388 } else if ((ahc
->flags
& AHC_INITIATORROLE
) != 0
7389 || ahc
->enabled_luns
> 0) {
7391 * Only allow our target id to change
7392 * if the initiator role is not configured
7393 * and there are no enabled luns which
7394 * are attached to the currently registered
7397 status
= CAM_TID_INVALID
;
7399 } else if ((ahc
->features
& AHC_MULTI_TID
) == 0
7400 && ahc
->enabled_luns
> 0) {
7402 status
= CAM_TID_INVALID
;
7406 if (status
!= CAM_REQ_CMP
) {
7407 ccb
->ccb_h
.status
= status
;
7412 * We now have an id that is valid.
7413 * If we aren't in target mode, switch modes.
7415 if ((ahc
->flags
& AHC_TARGETROLE
) == 0
7416 && ccb
->ccb_h
.target_id
!= CAM_TARGET_WILDCARD
) {
7418 ahc_flag saved_flags
;
7420 printk("Configuring Target Mode\n");
7422 if (LIST_FIRST(&ahc
->pending_scbs
) != NULL
) {
7423 ccb
->ccb_h
.status
= CAM_BUSY
;
7424 ahc_unlock(ahc
, &s
);
7427 saved_flags
= ahc
->flags
;
7428 ahc
->flags
|= AHC_TARGETROLE
;
7429 if ((ahc
->features
& AHC_MULTIROLE
) == 0)
7430 ahc
->flags
&= ~AHC_INITIATORROLE
;
7432 error
= ahc_loadseq(ahc
);
7435 * Restore original configuration and notify
7436 * the caller that we cannot support target mode.
7437 * Since the adapter started out in this
7438 * configuration, the firmware load will succeed,
7439 * so there is no point in checking ahc_loadseq's
7442 ahc
->flags
= saved_flags
;
7443 (void)ahc_loadseq(ahc
);
7445 ahc_unlock(ahc
, &s
);
7446 ccb
->ccb_h
.status
= CAM_FUNC_NOTAVAIL
;
7450 ahc_unlock(ahc
, &s
);
7453 target
= ccb
->ccb_h
.target_id
;
7454 lun
= ccb
->ccb_h
.target_lun
;
7455 channel
= SIM_CHANNEL(ahc
, sim
);
7456 target_mask
= 0x01 << target
;
7460 if (cel
->enable
!= 0) {
7463 /* Are we already enabled?? */
7464 if (lstate
!= NULL
) {
7465 xpt_print_path(ccb
->ccb_h
.path
);
7466 printk("Lun already enabled\n");
7467 ccb
->ccb_h
.status
= CAM_LUN_ALRDY_ENA
;
7471 if (cel
->grp6_len
!= 0
7472 || cel
->grp7_len
!= 0) {
7474 * Don't (yet?) support vendor
7475 * specific commands.
7477 ccb
->ccb_h
.status
= CAM_REQ_INVALID
;
7478 printk("Non-zero Group Codes\n");
7484 * Setup our data structures.
7486 if (target
!= CAM_TARGET_WILDCARD
&& tstate
== NULL
) {
7487 tstate
= ahc_alloc_tstate(ahc
, target
, channel
);
7488 if (tstate
== NULL
) {
7489 xpt_print_path(ccb
->ccb_h
.path
);
7490 printk("Couldn't allocate tstate\n");
7491 ccb
->ccb_h
.status
= CAM_RESRC_UNAVAIL
;
7495 lstate
= kmalloc(sizeof(*lstate
), GFP_ATOMIC
);
7496 if (lstate
== NULL
) {
7497 xpt_print_path(ccb
->ccb_h
.path
);
7498 printk("Couldn't allocate lstate\n");
7499 ccb
->ccb_h
.status
= CAM_RESRC_UNAVAIL
;
7502 memset(lstate
, 0, sizeof(*lstate
));
7503 status
= xpt_create_path(&lstate
->path
, /*periph*/NULL
,
7504 xpt_path_path_id(ccb
->ccb_h
.path
),
7505 xpt_path_target_id(ccb
->ccb_h
.path
),
7506 xpt_path_lun_id(ccb
->ccb_h
.path
));
7507 if (status
!= CAM_REQ_CMP
) {
7509 xpt_print_path(ccb
->ccb_h
.path
);
7510 printk("Couldn't allocate path\n");
7511 ccb
->ccb_h
.status
= CAM_RESRC_UNAVAIL
;
7514 SLIST_INIT(&lstate
->accept_tios
);
7515 SLIST_INIT(&lstate
->immed_notifies
);
7518 if (target
!= CAM_TARGET_WILDCARD
) {
7519 tstate
->enabled_luns
[lun
] = lstate
;
7520 ahc
->enabled_luns
++;
7522 if ((ahc
->features
& AHC_MULTI_TID
) != 0) {
7525 targid_mask
= ahc_inb(ahc
, TARGID
)
7526 | (ahc_inb(ahc
, TARGID
+ 1) << 8);
7528 targid_mask
|= target_mask
;
7529 ahc_outb(ahc
, TARGID
, targid_mask
);
7530 ahc_outb(ahc
, TARGID
+1, (targid_mask
>> 8));
7532 ahc_update_scsiid(ahc
, targid_mask
);
7537 channel
= SIM_CHANNEL(ahc
, sim
);
7538 our_id
= SIM_SCSI_ID(ahc
, sim
);
7541 * This can only happen if selections
7544 if (target
!= our_id
) {
7549 sblkctl
= ahc_inb(ahc
, SBLKCTL
);
7550 cur_channel
= (sblkctl
& SELBUSB
)
7552 if ((ahc
->features
& AHC_TWIN
) == 0)
7554 swap
= cur_channel
!= channel
;
7556 ahc
->our_id
= target
;
7558 ahc
->our_id_b
= target
;
7561 ahc_outb(ahc
, SBLKCTL
,
7564 ahc_outb(ahc
, SCSIID
, target
);
7567 ahc_outb(ahc
, SBLKCTL
, sblkctl
);
7571 ahc
->black_hole
= lstate
;
7572 /* Allow select-in operations */
7573 if (ahc
->black_hole
!= NULL
&& ahc
->enabled_luns
> 0) {
7574 scsiseq
= ahc_inb(ahc
, SCSISEQ_TEMPLATE
);
7576 ahc_outb(ahc
, SCSISEQ_TEMPLATE
, scsiseq
);
7577 scsiseq
= ahc_inb(ahc
, SCSISEQ
);
7579 ahc_outb(ahc
, SCSISEQ
, scsiseq
);
7582 ahc_unlock(ahc
, &s
);
7583 ccb
->ccb_h
.status
= CAM_REQ_CMP
;
7584 xpt_print_path(ccb
->ccb_h
.path
);
7585 printk("Lun now enabled for target mode\n");
7590 if (lstate
== NULL
) {
7591 ccb
->ccb_h
.status
= CAM_LUN_INVALID
;
7597 ccb
->ccb_h
.status
= CAM_REQ_CMP
;
7598 LIST_FOREACH(scb
, &ahc
->pending_scbs
, pending_links
) {
7599 struct ccb_hdr
*ccbh
;
7601 ccbh
= &scb
->io_ctx
->ccb_h
;
7602 if (ccbh
->func_code
== XPT_CONT_TARGET_IO
7603 && !xpt_path_comp(ccbh
->path
, ccb
->ccb_h
.path
)){
7604 printk("CTIO pending\n");
7605 ccb
->ccb_h
.status
= CAM_REQ_INVALID
;
7606 ahc_unlock(ahc
, &s
);
7611 if (SLIST_FIRST(&lstate
->accept_tios
) != NULL
) {
7612 printk("ATIOs pending\n");
7613 ccb
->ccb_h
.status
= CAM_REQ_INVALID
;
7616 if (SLIST_FIRST(&lstate
->immed_notifies
) != NULL
) {
7617 printk("INOTs pending\n");
7618 ccb
->ccb_h
.status
= CAM_REQ_INVALID
;
7621 if (ccb
->ccb_h
.status
!= CAM_REQ_CMP
) {
7622 ahc_unlock(ahc
, &s
);
7626 xpt_print_path(ccb
->ccb_h
.path
);
7627 printk("Target mode disabled\n");
7628 xpt_free_path(lstate
->path
);
7632 /* Can we clean up the target too? */
7633 if (target
!= CAM_TARGET_WILDCARD
) {
7634 tstate
->enabled_luns
[lun
] = NULL
;
7635 ahc
->enabled_luns
--;
7636 for (empty
= 1, i
= 0; i
< 8; i
++)
7637 if (tstate
->enabled_luns
[i
] != NULL
) {
7643 ahc_free_tstate(ahc
, target
, channel
,
7645 if (ahc
->features
& AHC_MULTI_TID
) {
7648 targid_mask
= ahc_inb(ahc
, TARGID
)
7649 | (ahc_inb(ahc
, TARGID
+ 1)
7652 targid_mask
&= ~target_mask
;
7653 ahc_outb(ahc
, TARGID
, targid_mask
);
7654 ahc_outb(ahc
, TARGID
+1,
7655 (targid_mask
>> 8));
7656 ahc_update_scsiid(ahc
, targid_mask
);
7661 ahc
->black_hole
= NULL
;
7664 * We can't allow selections without
7665 * our black hole device.
7669 if (ahc
->enabled_luns
== 0) {
7670 /* Disallow select-in */
7673 scsiseq
= ahc_inb(ahc
, SCSISEQ_TEMPLATE
);
7675 ahc_outb(ahc
, SCSISEQ_TEMPLATE
, scsiseq
);
7676 scsiseq
= ahc_inb(ahc
, SCSISEQ
);
7678 ahc_outb(ahc
, SCSISEQ
, scsiseq
);
7680 if ((ahc
->features
& AHC_MULTIROLE
) == 0) {
7681 printk("Configuring Initiator Mode\n");
7682 ahc
->flags
&= ~AHC_TARGETROLE
;
7683 ahc
->flags
|= AHC_INITIATORROLE
;
7685 * Returning to a configuration that
7686 * fit previously will always succeed.
7688 (void)ahc_loadseq(ahc
);
7691 * Unpaused. The extra unpause
7692 * that follows is harmless.
7697 ahc_unlock(ahc
, &s
);
7702 ahc_update_scsiid(struct ahc_softc
*ahc
, u_int targid_mask
)
7707 if ((ahc
->features
& AHC_MULTI_TID
) == 0)
7708 panic("ahc_update_scsiid called on non-multitid unit\n");
7711 * Since we will rely on the TARGID mask
7712 * for selection enables, ensure that OID
7713 * in SCSIID is not set to some other ID
7714 * that we don't want to allow selections on.
7716 if ((ahc
->features
& AHC_ULTRA2
) != 0)
7717 scsiid
= ahc_inb(ahc
, SCSIID_ULTRA2
);
7719 scsiid
= ahc_inb(ahc
, SCSIID
);
7720 scsiid_mask
= 0x1 << (scsiid
& OID
);
7721 if ((targid_mask
& scsiid_mask
) == 0) {
7724 /* ffs counts from 1 */
7725 our_id
= ffs(targid_mask
);
7727 our_id
= ahc
->our_id
;
7733 if ((ahc
->features
& AHC_ULTRA2
) != 0)
7734 ahc_outb(ahc
, SCSIID_ULTRA2
, scsiid
);
7736 ahc_outb(ahc
, SCSIID
, scsiid
);
7740 ahc_run_tqinfifo(struct ahc_softc
*ahc
, int paused
)
7742 struct target_cmd
*cmd
;
7745 * If the card supports auto-access pause,
7746 * we can access the card directly regardless
7747 * of whether it is paused or not.
7749 if ((ahc
->features
& AHC_AUTOPAUSE
) != 0)
7752 ahc_sync_tqinfifo(ahc
, BUS_DMASYNC_POSTREAD
);
7753 while ((cmd
= &ahc
->targetcmds
[ahc
->tqinfifonext
])->cmd_valid
!= 0) {
7756 * Only advance through the queue if we
7757 * have the resources to process the command.
7759 if (ahc_handle_target_cmd(ahc
, cmd
) != 0)
7763 ahc_dmamap_sync(ahc
, ahc
->shared_data_dmat
,
7764 ahc
->shared_data_dmamap
,
7765 ahc_targetcmd_offset(ahc
, ahc
->tqinfifonext
),
7766 sizeof(struct target_cmd
),
7767 BUS_DMASYNC_PREREAD
);
7768 ahc
->tqinfifonext
++;
7771 * Lazily update our position in the target mode incoming
7772 * command queue as seen by the sequencer.
7774 if ((ahc
->tqinfifonext
& (HOST_TQINPOS
- 1)) == 1) {
7775 if ((ahc
->features
& AHC_HS_MAILBOX
) != 0) {
7778 hs_mailbox
= ahc_inb(ahc
, HS_MAILBOX
);
7779 hs_mailbox
&= ~HOST_TQINPOS
;
7780 hs_mailbox
|= ahc
->tqinfifonext
& HOST_TQINPOS
;
7781 ahc_outb(ahc
, HS_MAILBOX
, hs_mailbox
);
7785 ahc_outb(ahc
, KERNEL_TQINPOS
,
7786 ahc
->tqinfifonext
& HOST_TQINPOS
);
7795 ahc_handle_target_cmd(struct ahc_softc
*ahc
, struct target_cmd
*cmd
)
7797 struct ahc_tmode_tstate
*tstate
;
7798 struct ahc_tmode_lstate
*lstate
;
7799 struct ccb_accept_tio
*atio
;
7805 initiator
= SCSIID_TARGET(ahc
, cmd
->scsiid
);
7806 target
= SCSIID_OUR_ID(cmd
->scsiid
);
7807 lun
= (cmd
->identify
& MSG_IDENTIFY_LUNMASK
);
7810 tstate
= ahc
->enabled_targets
[target
];
7813 lstate
= tstate
->enabled_luns
[lun
];
7816 * Commands for disabled luns go to the black hole driver.
7819 lstate
= ahc
->black_hole
;
7821 atio
= (struct ccb_accept_tio
*)SLIST_FIRST(&lstate
->accept_tios
);
7823 ahc
->flags
|= AHC_TQINFIFO_BLOCKED
;
7825 * Wait for more ATIOs from the peripheral driver for this lun.
7828 printk("%s: ATIOs exhausted\n", ahc_name(ahc
));
7831 ahc
->flags
&= ~AHC_TQINFIFO_BLOCKED
;
7832 SLIST_REMOVE_HEAD(&lstate
->accept_tios
, sim_links
.sle
);
7834 if (lstate
== ahc
->black_hole
) {
7835 /* Fill in the wildcards */
7836 atio
->ccb_h
.target_id
= target
;
7837 atio
->ccb_h
.target_lun
= lun
;
7841 * Package it up and send it off to
7842 * whomever has this lun enabled.
7844 atio
->sense_len
= 0;
7845 atio
->init_id
= initiator
;
7846 if (byte
[0] != 0xFF) {
7847 /* Tag was included */
7848 atio
->tag_action
= *byte
++;
7849 atio
->tag_id
= *byte
++;
7850 atio
->ccb_h
.flags
= CAM_TAG_ACTION_VALID
;
7852 atio
->ccb_h
.flags
= 0;
7856 /* Okay. Now determine the cdb size based on the command code */
7857 switch (*byte
>> CMD_GROUP_CODE_SHIFT
) {
7873 /* Only copy the opcode. */
7875 printk("Reserved or VU command code type encountered\n");
7879 memcpy(atio
->cdb_io
.cdb_bytes
, byte
, atio
->cdb_len
);
7881 atio
->ccb_h
.status
|= CAM_CDB_RECVD
;
7883 if ((cmd
->identify
& MSG_IDENTIFY_DISCFLAG
) == 0) {
7885 * We weren't allowed to disconnect.
7886 * We're hanging on the bus until a
7887 * continue target I/O comes in response
7888 * to this accept tio.
7890 ahc
->pending_device
= lstate
;
7891 ahc_freeze_ccb((union ccb
*)atio
);
7892 atio
->ccb_h
.flags
|= CAM_DIS_DISCONNECT
;
7894 xpt_done((union ccb
*)atio
);