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 $
43 #include "aic7xxx_osm.h"
44 #include "aic7xxx_inline.h"
45 #include "aicasm/aicasm_insformat.h"
47 /***************************** Lookup Tables **********************************/
48 static const char *const ahc_chip_names
[] = {
66 * Hardware error codes.
68 struct ahc_hard_error_entry
{
73 static const struct ahc_hard_error_entry ahc_hard_errors
[] = {
74 { ILLHADDR
, "Illegal Host Access" },
75 { ILLSADDR
, "Illegal Sequencer Address referenced" },
76 { ILLOPCODE
, "Illegal Opcode in sequencer program" },
77 { SQPARERR
, "Sequencer Parity Error" },
78 { DPARERR
, "Data-path Parity Error" },
79 { MPARERR
, "Scratch or SCB Memory Parity Error" },
80 { PCIERRSTAT
, "PCI Error detected" },
81 { CIOPARERR
, "CIOBUS Parity Error" },
83 static const u_int num_errors
= ARRAY_SIZE(ahc_hard_errors
);
85 static const struct ahc_phase_table_entry ahc_phase_table
[] =
87 { P_DATAOUT
, MSG_NOOP
, "in Data-out phase" },
88 { P_DATAIN
, MSG_INITIATOR_DET_ERR
, "in Data-in phase" },
89 { P_DATAOUT_DT
, MSG_NOOP
, "in DT Data-out phase" },
90 { P_DATAIN_DT
, MSG_INITIATOR_DET_ERR
, "in DT Data-in phase" },
91 { P_COMMAND
, MSG_NOOP
, "in Command phase" },
92 { P_MESGOUT
, MSG_NOOP
, "in Message-out phase" },
93 { P_STATUS
, MSG_INITIATOR_DET_ERR
, "in Status phase" },
94 { P_MESGIN
, MSG_PARITY_ERROR
, "in Message-in phase" },
95 { P_BUSFREE
, MSG_NOOP
, "while idle" },
96 { 0, MSG_NOOP
, "in unknown phase" }
100 * In most cases we only wish to itterate over real phases, so
101 * exclude the last element from the count.
103 static const u_int num_phases
= ARRAY_SIZE(ahc_phase_table
) - 1;
106 * Valid SCSIRATE values. (p. 3-17)
107 * Provides a mapping of tranfer periods in ns to the proper value to
108 * stick in the scsixfer reg.
110 static const struct ahc_syncrate ahc_syncrates
[] =
112 /* ultra2 fast/ultra period rate */
113 { 0x42, 0x000, 9, "80.0" },
114 { 0x03, 0x000, 10, "40.0" },
115 { 0x04, 0x000, 11, "33.0" },
116 { 0x05, 0x100, 12, "20.0" },
117 { 0x06, 0x110, 15, "16.0" },
118 { 0x07, 0x120, 18, "13.4" },
119 { 0x08, 0x000, 25, "10.0" },
120 { 0x19, 0x010, 31, "8.0" },
121 { 0x1a, 0x020, 37, "6.67" },
122 { 0x1b, 0x030, 43, "5.7" },
123 { 0x1c, 0x040, 50, "5.0" },
124 { 0x00, 0x050, 56, "4.4" },
125 { 0x00, 0x060, 62, "4.0" },
126 { 0x00, 0x070, 68, "3.6" },
127 { 0x00, 0x000, 0, NULL
}
130 /* Our Sequencer Program */
131 #include "aic7xxx_seq.h"
133 /**************************** Function Declarations ***************************/
134 static void ahc_force_renegotiation(struct ahc_softc
*ahc
,
135 struct ahc_devinfo
*devinfo
);
136 static struct ahc_tmode_tstate
*
137 ahc_alloc_tstate(struct ahc_softc
*ahc
,
138 u_int scsi_id
, char channel
);
139 #ifdef AHC_TARGET_MODE
140 static void ahc_free_tstate(struct ahc_softc
*ahc
,
141 u_int scsi_id
, char channel
, int force
);
143 static const struct ahc_syncrate
*
144 ahc_devlimited_syncrate(struct ahc_softc
*ahc
,
145 struct ahc_initiator_tinfo
*,
149 static void ahc_update_pending_scbs(struct ahc_softc
*ahc
);
150 static void ahc_fetch_devinfo(struct ahc_softc
*ahc
,
151 struct ahc_devinfo
*devinfo
);
152 static void ahc_scb_devinfo(struct ahc_softc
*ahc
,
153 struct ahc_devinfo
*devinfo
,
155 static void ahc_assert_atn(struct ahc_softc
*ahc
);
156 static void ahc_setup_initiator_msgout(struct ahc_softc
*ahc
,
157 struct ahc_devinfo
*devinfo
,
159 static void ahc_build_transfer_msg(struct ahc_softc
*ahc
,
160 struct ahc_devinfo
*devinfo
);
161 static void ahc_construct_sdtr(struct ahc_softc
*ahc
,
162 struct ahc_devinfo
*devinfo
,
163 u_int period
, u_int offset
);
164 static void ahc_construct_wdtr(struct ahc_softc
*ahc
,
165 struct ahc_devinfo
*devinfo
,
167 static void ahc_construct_ppr(struct ahc_softc
*ahc
,
168 struct ahc_devinfo
*devinfo
,
169 u_int period
, u_int offset
,
170 u_int bus_width
, u_int ppr_options
);
171 static void ahc_clear_msg_state(struct ahc_softc
*ahc
);
172 static void ahc_handle_proto_violation(struct ahc_softc
*ahc
);
173 static void ahc_handle_message_phase(struct ahc_softc
*ahc
);
179 static int ahc_sent_msg(struct ahc_softc
*ahc
, ahc_msgtype type
,
180 u_int msgval
, int full
);
181 static int ahc_parse_msg(struct ahc_softc
*ahc
,
182 struct ahc_devinfo
*devinfo
);
183 static int ahc_handle_msg_reject(struct ahc_softc
*ahc
,
184 struct ahc_devinfo
*devinfo
);
185 static void ahc_handle_ign_wide_residue(struct ahc_softc
*ahc
,
186 struct ahc_devinfo
*devinfo
);
187 static void ahc_reinitialize_dataptrs(struct ahc_softc
*ahc
);
188 static void ahc_handle_devreset(struct ahc_softc
*ahc
,
189 struct ahc_devinfo
*devinfo
,
190 cam_status status
, char *message
,
192 #ifdef AHC_TARGET_MODE
193 static void ahc_setup_target_msgin(struct ahc_softc
*ahc
,
194 struct ahc_devinfo
*devinfo
,
198 static bus_dmamap_callback_t ahc_dmamap_cb
;
199 static void ahc_build_free_scb_list(struct ahc_softc
*ahc
);
200 static int ahc_init_scbdata(struct ahc_softc
*ahc
);
201 static void ahc_fini_scbdata(struct ahc_softc
*ahc
);
202 static void ahc_qinfifo_requeue(struct ahc_softc
*ahc
,
203 struct scb
*prev_scb
,
205 static int ahc_qinfifo_count(struct ahc_softc
*ahc
);
206 static u_int
ahc_rem_scb_from_disc_list(struct ahc_softc
*ahc
,
207 u_int prev
, u_int scbptr
);
208 static void ahc_add_curscb_to_free_list(struct ahc_softc
*ahc
);
209 static u_int
ahc_rem_wscb(struct ahc_softc
*ahc
,
210 u_int scbpos
, u_int prev
);
211 static void ahc_reset_current_bus(struct ahc_softc
*ahc
);
213 static void ahc_dumpseq(struct ahc_softc
*ahc
);
215 static int ahc_loadseq(struct ahc_softc
*ahc
);
216 static int ahc_check_patch(struct ahc_softc
*ahc
,
217 const struct patch
**start_patch
,
218 u_int start_instr
, u_int
*skip_addr
);
219 static void ahc_download_instr(struct ahc_softc
*ahc
,
220 u_int instrptr
, uint8_t *dconsts
);
221 #ifdef AHC_TARGET_MODE
222 static void ahc_queue_lstate_event(struct ahc_softc
*ahc
,
223 struct ahc_tmode_lstate
*lstate
,
227 static void ahc_update_scsiid(struct ahc_softc
*ahc
,
229 static int ahc_handle_target_cmd(struct ahc_softc
*ahc
,
230 struct target_cmd
*cmd
);
233 static u_int
ahc_index_busy_tcl(struct ahc_softc
*ahc
, u_int tcl
);
234 static void ahc_unbusy_tcl(struct ahc_softc
*ahc
, u_int tcl
);
235 static void ahc_busy_tcl(struct ahc_softc
*ahc
,
236 u_int tcl
, u_int busyid
);
238 /************************** SCB and SCB queue management **********************/
239 static void ahc_run_untagged_queues(struct ahc_softc
*ahc
);
240 static void ahc_run_untagged_queue(struct ahc_softc
*ahc
,
241 struct scb_tailq
*queue
);
243 /****************************** Initialization ********************************/
244 static void ahc_alloc_scbs(struct ahc_softc
*ahc
);
245 static void ahc_shutdown(void *arg
);
247 /*************************** Interrupt Services *******************************/
248 static void ahc_clear_intstat(struct ahc_softc
*ahc
);
249 static void ahc_run_qoutfifo(struct ahc_softc
*ahc
);
250 #ifdef AHC_TARGET_MODE
251 static void ahc_run_tqinfifo(struct ahc_softc
*ahc
, int paused
);
253 static void ahc_handle_brkadrint(struct ahc_softc
*ahc
);
254 static void ahc_handle_seqint(struct ahc_softc
*ahc
, u_int intstat
);
255 static void ahc_handle_scsiint(struct ahc_softc
*ahc
,
257 static void ahc_clear_critical_section(struct ahc_softc
*ahc
);
259 /***************************** Error Recovery *********************************/
260 static void ahc_freeze_devq(struct ahc_softc
*ahc
, struct scb
*scb
);
261 static int ahc_abort_scbs(struct ahc_softc
*ahc
, int target
,
262 char channel
, int lun
, u_int tag
,
263 role_t role
, uint32_t status
);
264 static void ahc_calc_residual(struct ahc_softc
*ahc
,
267 /*********************** Untagged Transaction Routines ************************/
268 static inline void ahc_freeze_untagged_queues(struct ahc_softc
*ahc
);
269 static inline void ahc_release_untagged_queues(struct ahc_softc
*ahc
);
272 * Block our completion routine from starting the next untagged
273 * transaction for this target or target lun.
276 ahc_freeze_untagged_queues(struct ahc_softc
*ahc
)
278 if ((ahc
->flags
& AHC_SCB_BTT
) == 0)
279 ahc
->untagged_queue_lock
++;
283 * Allow the next untagged transaction for this target or target lun
284 * to be executed. We use a counting semaphore to allow the lock
285 * to be acquired recursively. Once the count drops to zero, the
286 * transaction queues will be run.
289 ahc_release_untagged_queues(struct ahc_softc
*ahc
)
291 if ((ahc
->flags
& AHC_SCB_BTT
) == 0) {
292 ahc
->untagged_queue_lock
--;
293 if (ahc
->untagged_queue_lock
== 0)
294 ahc_run_untagged_queues(ahc
);
298 /************************* Sequencer Execution Control ************************/
300 * Work around any chip bugs related to halting sequencer execution.
301 * On Ultra2 controllers, we must clear the CIOBUS stretch signal by
302 * reading a register that will set this signal and deassert it.
303 * Without this workaround, if the chip is paused, by an interrupt or
304 * manual pause while accessing scb ram, accesses to certain registers
305 * will hang the system (infinite pci retries).
308 ahc_pause_bug_fix(struct ahc_softc
*ahc
)
310 if ((ahc
->features
& AHC_ULTRA2
) != 0)
311 (void)ahc_inb(ahc
, CCSCBCTL
);
315 * Determine whether the sequencer has halted code execution.
316 * Returns non-zero status if the sequencer is stopped.
319 ahc_is_paused(struct ahc_softc
*ahc
)
321 return ((ahc_inb(ahc
, HCNTRL
) & PAUSE
) != 0);
325 * Request that the sequencer stop and wait, indefinitely, for it
326 * to stop. The sequencer will only acknowledge that it is paused
327 * once it has reached an instruction boundary and PAUSEDIS is
328 * cleared in the SEQCTL register. The sequencer may use PAUSEDIS
329 * for critical sections.
332 ahc_pause(struct ahc_softc
*ahc
)
334 ahc_outb(ahc
, HCNTRL
, ahc
->pause
);
337 * Since the sequencer can disable pausing in a critical section, we
338 * must loop until it actually stops.
340 while (ahc_is_paused(ahc
) == 0)
343 ahc_pause_bug_fix(ahc
);
347 * Allow the sequencer to continue program execution.
348 * We check here to ensure that no additional interrupt
349 * sources that would cause the sequencer to halt have been
350 * asserted. If, for example, a SCSI bus reset is detected
351 * while we are fielding a different, pausing, interrupt type,
352 * we don't want to release the sequencer before going back
353 * into our interrupt handler and dealing with this new
357 ahc_unpause(struct ahc_softc
*ahc
)
359 if ((ahc_inb(ahc
, INTSTAT
) & (SCSIINT
| SEQINT
| BRKADRINT
)) == 0)
360 ahc_outb(ahc
, HCNTRL
, ahc
->unpause
);
363 /************************** Memory mapping routines ***************************/
364 static struct ahc_dma_seg
*
365 ahc_sg_bus_to_virt(struct scb
*scb
, uint32_t sg_busaddr
)
369 sg_index
= (sg_busaddr
- scb
->sg_list_phys
)/sizeof(struct ahc_dma_seg
);
370 /* sg_list_phys points to entry 1, not 0 */
373 return (&scb
->sg_list
[sg_index
]);
377 ahc_sg_virt_to_bus(struct scb
*scb
, struct ahc_dma_seg
*sg
)
381 /* sg_list_phys points to entry 1, not 0 */
382 sg_index
= sg
- &scb
->sg_list
[1];
384 return (scb
->sg_list_phys
+ (sg_index
* sizeof(*scb
->sg_list
)));
388 ahc_hscb_busaddr(struct ahc_softc
*ahc
, u_int index
)
390 return (ahc
->scb_data
->hscb_busaddr
391 + (sizeof(struct hardware_scb
) * index
));
395 ahc_sync_scb(struct ahc_softc
*ahc
, struct scb
*scb
, int op
)
397 ahc_dmamap_sync(ahc
, ahc
->scb_data
->hscb_dmat
,
398 ahc
->scb_data
->hscb_dmamap
,
399 /*offset*/(scb
->hscb
- ahc
->hscbs
) * sizeof(*scb
->hscb
),
400 /*len*/sizeof(*scb
->hscb
), op
);
404 ahc_sync_sglist(struct ahc_softc
*ahc
, struct scb
*scb
, int op
)
406 if (scb
->sg_count
== 0)
409 ahc_dmamap_sync(ahc
, ahc
->scb_data
->sg_dmat
, scb
->sg_map
->sg_dmamap
,
410 /*offset*/(scb
->sg_list
- scb
->sg_map
->sg_vaddr
)
411 * sizeof(struct ahc_dma_seg
),
412 /*len*/sizeof(struct ahc_dma_seg
) * scb
->sg_count
, op
);
415 #ifdef AHC_TARGET_MODE
417 ahc_targetcmd_offset(struct ahc_softc
*ahc
, u_int index
)
419 return (((uint8_t *)&ahc
->targetcmds
[index
]) - ahc
->qoutfifo
);
423 /*********************** Miscellaneous Support Functions ***********************/
425 * Determine whether the sequencer reported a residual
426 * for this SCB/transaction.
429 ahc_update_residual(struct ahc_softc
*ahc
, struct scb
*scb
)
433 sgptr
= ahc_le32toh(scb
->hscb
->sgptr
);
434 if ((sgptr
& SG_RESID_VALID
) != 0)
435 ahc_calc_residual(ahc
, scb
);
439 * Return pointers to the transfer negotiation information
440 * for the specified our_id/remote_id pair.
442 struct ahc_initiator_tinfo
*
443 ahc_fetch_transinfo(struct ahc_softc
*ahc
, char channel
, u_int our_id
,
444 u_int remote_id
, struct ahc_tmode_tstate
**tstate
)
447 * Transfer data structures are stored from the perspective
448 * of the target role. Since the parameters for a connection
449 * in the initiator role to a given target are the same as
450 * when the roles are reversed, we pretend we are the target.
454 *tstate
= ahc
->enabled_targets
[our_id
];
455 return (&(*tstate
)->transinfo
[remote_id
]);
459 ahc_inw(struct ahc_softc
*ahc
, u_int port
)
461 uint16_t r
= ahc_inb(ahc
, port
+1) << 8;
462 return r
| ahc_inb(ahc
, port
);
466 ahc_outw(struct ahc_softc
*ahc
, u_int port
, u_int value
)
468 ahc_outb(ahc
, port
, value
& 0xFF);
469 ahc_outb(ahc
, port
+1, (value
>> 8) & 0xFF);
473 ahc_inl(struct ahc_softc
*ahc
, u_int port
)
475 return ((ahc_inb(ahc
, port
))
476 | (ahc_inb(ahc
, port
+1) << 8)
477 | (ahc_inb(ahc
, port
+2) << 16)
478 | (ahc_inb(ahc
, port
+3) << 24));
482 ahc_outl(struct ahc_softc
*ahc
, u_int port
, uint32_t value
)
484 ahc_outb(ahc
, port
, (value
) & 0xFF);
485 ahc_outb(ahc
, port
+1, ((value
) >> 8) & 0xFF);
486 ahc_outb(ahc
, port
+2, ((value
) >> 16) & 0xFF);
487 ahc_outb(ahc
, port
+3, ((value
) >> 24) & 0xFF);
491 ahc_inq(struct ahc_softc
*ahc
, u_int port
)
493 return ((ahc_inb(ahc
, port
))
494 | (ahc_inb(ahc
, port
+1) << 8)
495 | (ahc_inb(ahc
, port
+2) << 16)
496 | (ahc_inb(ahc
, port
+3) << 24)
497 | (((uint64_t)ahc_inb(ahc
, port
+4)) << 32)
498 | (((uint64_t)ahc_inb(ahc
, port
+5)) << 40)
499 | (((uint64_t)ahc_inb(ahc
, port
+6)) << 48)
500 | (((uint64_t)ahc_inb(ahc
, port
+7)) << 56));
504 ahc_outq(struct ahc_softc
*ahc
, u_int port
, uint64_t value
)
506 ahc_outb(ahc
, port
, value
& 0xFF);
507 ahc_outb(ahc
, port
+1, (value
>> 8) & 0xFF);
508 ahc_outb(ahc
, port
+2, (value
>> 16) & 0xFF);
509 ahc_outb(ahc
, port
+3, (value
>> 24) & 0xFF);
510 ahc_outb(ahc
, port
+4, (value
>> 32) & 0xFF);
511 ahc_outb(ahc
, port
+5, (value
>> 40) & 0xFF);
512 ahc_outb(ahc
, port
+6, (value
>> 48) & 0xFF);
513 ahc_outb(ahc
, port
+7, (value
>> 56) & 0xFF);
517 * Get a free scb. If there are none, see if we can allocate a new SCB.
520 ahc_get_scb(struct ahc_softc
*ahc
)
524 if ((scb
= SLIST_FIRST(&ahc
->scb_data
->free_scbs
)) == NULL
) {
526 scb
= SLIST_FIRST(&ahc
->scb_data
->free_scbs
);
530 SLIST_REMOVE_HEAD(&ahc
->scb_data
->free_scbs
, links
.sle
);
535 * Return an SCB resource to the free list.
538 ahc_free_scb(struct ahc_softc
*ahc
, struct scb
*scb
)
540 struct hardware_scb
*hscb
;
543 /* Clean up for the next user */
544 ahc
->scb_data
->scbindex
[hscb
->tag
] = NULL
;
545 scb
->flags
= SCB_FREE
;
548 SLIST_INSERT_HEAD(&ahc
->scb_data
->free_scbs
, scb
, links
.sle
);
550 /* Notify the OSM that a resource is now available. */
551 ahc_platform_scb_free(ahc
, scb
);
555 ahc_lookup_scb(struct ahc_softc
*ahc
, u_int tag
)
559 scb
= ahc
->scb_data
->scbindex
[tag
];
561 ahc_sync_scb(ahc
, scb
,
562 BUS_DMASYNC_POSTREAD
|BUS_DMASYNC_POSTWRITE
);
567 ahc_swap_with_next_hscb(struct ahc_softc
*ahc
, struct scb
*scb
)
569 struct hardware_scb
*q_hscb
;
573 * Our queuing method is a bit tricky. The card
574 * knows in advance which HSCB to download, and we
575 * can't disappoint it. To achieve this, the next
576 * SCB to download is saved off in ahc->next_queued_scb.
577 * When we are called to queue "an arbitrary scb",
578 * we copy the contents of the incoming HSCB to the one
579 * the sequencer knows about, swap HSCB pointers and
580 * finally assign the SCB to the tag indexed location
581 * in the scb_array. This makes sure that we can still
582 * locate the correct SCB by SCB_TAG.
584 q_hscb
= ahc
->next_queued_scb
->hscb
;
585 saved_tag
= q_hscb
->tag
;
586 memcpy(q_hscb
, scb
->hscb
, sizeof(*scb
->hscb
));
587 if ((scb
->flags
& SCB_CDB32_PTR
) != 0) {
588 q_hscb
->shared_data
.cdb_ptr
=
589 ahc_htole32(ahc_hscb_busaddr(ahc
, q_hscb
->tag
)
590 + offsetof(struct hardware_scb
, cdb32
));
592 q_hscb
->tag
= saved_tag
;
593 q_hscb
->next
= scb
->hscb
->tag
;
595 /* Now swap HSCB pointers. */
596 ahc
->next_queued_scb
->hscb
= scb
->hscb
;
599 /* Now define the mapping from tag to SCB in the scbindex */
600 ahc
->scb_data
->scbindex
[scb
->hscb
->tag
] = scb
;
604 * Tell the sequencer about a new transaction to execute.
607 ahc_queue_scb(struct ahc_softc
*ahc
, struct scb
*scb
)
609 ahc_swap_with_next_hscb(ahc
, scb
);
611 if (scb
->hscb
->tag
== SCB_LIST_NULL
612 || scb
->hscb
->next
== SCB_LIST_NULL
)
613 panic("Attempt to queue invalid SCB tag %x:%x\n",
614 scb
->hscb
->tag
, scb
->hscb
->next
);
617 * Setup data "oddness".
619 scb
->hscb
->lun
&= LID
;
620 if (ahc_get_transfer_length(scb
) & 0x1)
621 scb
->hscb
->lun
|= SCB_XFERLEN_ODD
;
624 * Keep a history of SCBs we've downloaded in the qinfifo.
626 ahc
->qinfifo
[ahc
->qinfifonext
++] = scb
->hscb
->tag
;
629 * Make sure our data is consistent from the
630 * perspective of the adapter.
632 ahc_sync_scb(ahc
, scb
, BUS_DMASYNC_PREREAD
|BUS_DMASYNC_PREWRITE
);
634 /* Tell the adapter about the newly queued SCB */
635 if ((ahc
->features
& AHC_QUEUE_REGS
) != 0) {
636 ahc_outb(ahc
, HNSCB_QOFF
, ahc
->qinfifonext
);
638 if ((ahc
->features
& AHC_AUTOPAUSE
) == 0)
640 ahc_outb(ahc
, KERNEL_QINPOS
, ahc
->qinfifonext
);
641 if ((ahc
->features
& AHC_AUTOPAUSE
) == 0)
646 struct scsi_sense_data
*
647 ahc_get_sense_buf(struct ahc_softc
*ahc
, struct scb
*scb
)
651 offset
= scb
- ahc
->scb_data
->scbarray
;
652 return (&ahc
->scb_data
->sense
[offset
]);
656 ahc_get_sense_bufaddr(struct ahc_softc
*ahc
, struct scb
*scb
)
660 offset
= scb
- ahc
->scb_data
->scbarray
;
661 return (ahc
->scb_data
->sense_busaddr
662 + (offset
* sizeof(struct scsi_sense_data
)));
665 /************************** Interrupt Processing ******************************/
667 ahc_sync_qoutfifo(struct ahc_softc
*ahc
, int op
)
669 ahc_dmamap_sync(ahc
, ahc
->shared_data_dmat
, ahc
->shared_data_dmamap
,
670 /*offset*/0, /*len*/256, op
);
674 ahc_sync_tqinfifo(struct ahc_softc
*ahc
, int op
)
676 #ifdef AHC_TARGET_MODE
677 if ((ahc
->flags
& AHC_TARGETROLE
) != 0) {
678 ahc_dmamap_sync(ahc
, ahc
->shared_data_dmat
,
679 ahc
->shared_data_dmamap
,
680 ahc_targetcmd_offset(ahc
, 0),
681 sizeof(struct target_cmd
) * AHC_TMODE_CMDS
,
688 * See if the firmware has posted any completed commands
689 * into our in-core command complete fifos.
691 #define AHC_RUN_QOUTFIFO 0x1
692 #define AHC_RUN_TQINFIFO 0x2
694 ahc_check_cmdcmpltqueues(struct ahc_softc
*ahc
)
699 ahc_dmamap_sync(ahc
, ahc
->shared_data_dmat
, ahc
->shared_data_dmamap
,
700 /*offset*/ahc
->qoutfifonext
, /*len*/1,
701 BUS_DMASYNC_POSTREAD
);
702 if (ahc
->qoutfifo
[ahc
->qoutfifonext
] != SCB_LIST_NULL
)
703 retval
|= AHC_RUN_QOUTFIFO
;
704 #ifdef AHC_TARGET_MODE
705 if ((ahc
->flags
& AHC_TARGETROLE
) != 0
706 && (ahc
->flags
& AHC_TQINFIFO_BLOCKED
) == 0) {
707 ahc_dmamap_sync(ahc
, ahc
->shared_data_dmat
,
708 ahc
->shared_data_dmamap
,
709 ahc_targetcmd_offset(ahc
, ahc
->tqinfifofnext
),
710 /*len*/sizeof(struct target_cmd
),
711 BUS_DMASYNC_POSTREAD
);
712 if (ahc
->targetcmds
[ahc
->tqinfifonext
].cmd_valid
!= 0)
713 retval
|= AHC_RUN_TQINFIFO
;
720 * Catch an interrupt from the adapter
723 ahc_intr(struct ahc_softc
*ahc
)
727 if ((ahc
->pause
& INTEN
) == 0) {
729 * Our interrupt is not enabled on the chip
730 * and may be disabled for re-entrancy reasons,
731 * so just return. This is likely just a shared
737 * Instead of directly reading the interrupt status register,
738 * infer the cause of the interrupt by checking our in-core
739 * completion queues. This avoids a costly PCI bus read in
742 if ((ahc
->flags
& (AHC_ALL_INTERRUPTS
|AHC_EDGE_INTERRUPT
)) == 0
743 && (ahc_check_cmdcmpltqueues(ahc
) != 0))
746 intstat
= ahc_inb(ahc
, INTSTAT
);
749 if ((intstat
& INT_PEND
) == 0) {
750 #if AHC_PCI_CONFIG > 0
751 if (ahc
->unsolicited_ints
> 500) {
752 ahc
->unsolicited_ints
= 0;
753 if ((ahc
->chip
& AHC_PCI
) != 0
754 && (ahc_inb(ahc
, ERROR
) & PCIERRSTAT
) != 0)
758 ahc
->unsolicited_ints
++;
761 ahc
->unsolicited_ints
= 0;
763 if (intstat
& CMDCMPLT
) {
764 ahc_outb(ahc
, CLRINT
, CLRCMDINT
);
767 * Ensure that the chip sees that we've cleared
768 * this interrupt before we walk the output fifo.
769 * Otherwise, we may, due to posted bus writes,
770 * clear the interrupt after we finish the scan,
771 * and after the sequencer has added new entries
772 * and asserted the interrupt again.
774 ahc_flush_device_writes(ahc
);
775 ahc_run_qoutfifo(ahc
);
776 #ifdef AHC_TARGET_MODE
777 if ((ahc
->flags
& AHC_TARGETROLE
) != 0)
778 ahc_run_tqinfifo(ahc
, /*paused*/FALSE
);
783 * Handle statuses that may invalidate our cached
784 * copy of INTSTAT separately.
786 if (intstat
== 0xFF && (ahc
->features
& AHC_REMOVABLE
) != 0) {
787 /* Hot eject. Do nothing */
788 } else if (intstat
& BRKADRINT
) {
789 ahc_handle_brkadrint(ahc
);
790 } else if ((intstat
& (SEQINT
|SCSIINT
)) != 0) {
792 ahc_pause_bug_fix(ahc
);
794 if ((intstat
& SEQINT
) != 0)
795 ahc_handle_seqint(ahc
, intstat
);
797 if ((intstat
& SCSIINT
) != 0)
798 ahc_handle_scsiint(ahc
, intstat
);
803 /************************* Sequencer Execution Control ************************/
805 * Restart the sequencer program from address zero
808 ahc_restart(struct ahc_softc
*ahc
)
814 /* No more pending messages. */
815 ahc_clear_msg_state(ahc
);
817 ahc_outb(ahc
, SCSISIGO
, 0); /* De-assert BSY */
818 ahc_outb(ahc
, MSG_OUT
, MSG_NOOP
); /* No message to send */
819 ahc_outb(ahc
, SXFRCTL1
, ahc_inb(ahc
, SXFRCTL1
) & ~BITBUCKET
);
820 ahc_outb(ahc
, LASTPHASE
, P_BUSFREE
);
821 ahc_outb(ahc
, SAVED_SCSIID
, 0xFF);
822 ahc_outb(ahc
, SAVED_LUN
, 0xFF);
825 * Ensure that the sequencer's idea of TQINPOS
826 * matches our own. The sequencer increments TQINPOS
827 * only after it sees a DMA complete and a reset could
828 * occur before the increment leaving the kernel to believe
829 * the command arrived but the sequencer to not.
831 ahc_outb(ahc
, TQINPOS
, ahc
->tqinfifonext
);
833 /* Always allow reselection */
834 ahc_outb(ahc
, SCSISEQ
,
835 ahc_inb(ahc
, SCSISEQ_TEMPLATE
) & (ENSELI
|ENRSELI
|ENAUTOATNP
));
836 if ((ahc
->features
& AHC_CMD_CHAN
) != 0) {
837 /* Ensure that no DMA operations are in progress */
838 ahc_outb(ahc
, CCSCBCNT
, 0);
839 ahc_outb(ahc
, CCSGCTL
, 0);
840 ahc_outb(ahc
, CCSCBCTL
, 0);
843 * If we were in the process of DMA'ing SCB data into
844 * an SCB, replace that SCB on the free list. This prevents
847 if ((ahc_inb(ahc
, SEQ_FLAGS2
) & SCB_DMA
) != 0) {
848 ahc_add_curscb_to_free_list(ahc
);
849 ahc_outb(ahc
, SEQ_FLAGS2
,
850 ahc_inb(ahc
, SEQ_FLAGS2
) & ~SCB_DMA
);
854 * Clear any pending sequencer interrupt. It is no
855 * longer relevant since we're resetting the Program
858 ahc_outb(ahc
, CLRINT
, CLRSEQINT
);
860 ahc_outb(ahc
, MWI_RESIDUAL
, 0);
861 ahc_outb(ahc
, SEQCTL
, ahc
->seqctl
);
862 ahc_outb(ahc
, SEQADDR0
, 0);
863 ahc_outb(ahc
, SEQADDR1
, 0);
866 * Take the LED out of diagnostic mode on PM resume, too
868 sblkctl
= ahc_inb(ahc
, SBLKCTL
);
869 ahc_outb(ahc
, SBLKCTL
, (sblkctl
& ~(DIAGLEDEN
|DIAGLEDON
)));
874 /************************* Input/Output Queues ********************************/
876 ahc_run_qoutfifo(struct ahc_softc
*ahc
)
881 ahc_sync_qoutfifo(ahc
, BUS_DMASYNC_POSTREAD
);
882 while (ahc
->qoutfifo
[ahc
->qoutfifonext
] != SCB_LIST_NULL
) {
884 scb_index
= ahc
->qoutfifo
[ahc
->qoutfifonext
];
885 if ((ahc
->qoutfifonext
& 0x03) == 0x03) {
889 * Clear 32bits of QOUTFIFO at a time
890 * so that we don't clobber an incoming
891 * byte DMA to the array on architectures
892 * that only support 32bit load and store
895 modnext
= ahc
->qoutfifonext
& ~0x3;
896 *((uint32_t *)(&ahc
->qoutfifo
[modnext
])) = 0xFFFFFFFFUL
;
897 ahc_dmamap_sync(ahc
, ahc
->shared_data_dmat
,
898 ahc
->shared_data_dmamap
,
899 /*offset*/modnext
, /*len*/4,
900 BUS_DMASYNC_PREREAD
);
904 scb
= ahc_lookup_scb(ahc
, scb_index
);
906 printk("%s: WARNING no command for scb %d "
907 "(cmdcmplt)\nQOUTPOS = %d\n",
908 ahc_name(ahc
), scb_index
,
909 (ahc
->qoutfifonext
- 1) & 0xFF);
914 * Save off the residual
917 ahc_update_residual(ahc
, scb
);
923 ahc_run_untagged_queues(struct ahc_softc
*ahc
)
927 for (i
= 0; i
< 16; i
++)
928 ahc_run_untagged_queue(ahc
, &ahc
->untagged_queues
[i
]);
932 ahc_run_untagged_queue(struct ahc_softc
*ahc
, struct scb_tailq
*queue
)
936 if (ahc
->untagged_queue_lock
!= 0)
939 if ((scb
= TAILQ_FIRST(queue
)) != NULL
940 && (scb
->flags
& SCB_ACTIVE
) == 0) {
941 scb
->flags
|= SCB_ACTIVE
;
942 ahc_queue_scb(ahc
, scb
);
946 /************************* Interrupt Handling *********************************/
948 ahc_handle_brkadrint(struct ahc_softc
*ahc
)
951 * We upset the sequencer :-(
952 * Lookup the error message
957 error
= ahc_inb(ahc
, ERROR
);
958 for (i
= 0; error
!= 1 && i
< num_errors
; i
++)
960 printk("%s: brkadrint, %s at seqaddr = 0x%x\n",
961 ahc_name(ahc
), ahc_hard_errors
[i
].errmesg
,
962 ahc_inb(ahc
, SEQADDR0
) |
963 (ahc_inb(ahc
, SEQADDR1
) << 8));
965 ahc_dump_card_state(ahc
);
967 /* Tell everyone that this HBA is no longer available */
968 ahc_abort_scbs(ahc
, CAM_TARGET_WILDCARD
, ALL_CHANNELS
,
969 CAM_LUN_WILDCARD
, SCB_LIST_NULL
, ROLE_UNKNOWN
,
972 /* Disable all interrupt sources by resetting the controller */
977 ahc_handle_seqint(struct ahc_softc
*ahc
, u_int intstat
)
980 struct ahc_devinfo devinfo
;
982 ahc_fetch_devinfo(ahc
, &devinfo
);
985 * Clear the upper byte that holds SEQINT status
986 * codes and clear the SEQINT bit. We will unpause
987 * the sequencer, if appropriate, after servicing
990 ahc_outb(ahc
, CLRINT
, CLRSEQINT
);
991 switch (intstat
& SEQINT_MASK
) {
995 struct hardware_scb
*hscb
;
998 * Set the default return value to 0 (don't
999 * send sense). The sense code will change
1002 ahc_outb(ahc
, RETURN_1
, 0);
1005 * The sequencer will notify us when a command
1006 * has an error that would be of interest to
1007 * the kernel. This allows us to leave the sequencer
1008 * running in the common case of command completes
1009 * without error. The sequencer will already have
1010 * dma'd the SCB back up to us, so we can reference
1011 * the in kernel copy directly.
1013 scb_index
= ahc_inb(ahc
, SCB_TAG
);
1014 scb
= ahc_lookup_scb(ahc
, scb_index
);
1016 ahc_print_devinfo(ahc
, &devinfo
);
1017 printk("ahc_intr - referenced scb "
1018 "not valid during seqint 0x%x scb(%d)\n",
1019 intstat
, scb_index
);
1020 ahc_dump_card_state(ahc
);
1021 panic("for safety");
1027 /* Don't want to clobber the original sense code */
1028 if ((scb
->flags
& SCB_SENSE
) != 0) {
1030 * Clear the SCB_SENSE Flag and have
1031 * the sequencer do a normal command
1034 scb
->flags
&= ~SCB_SENSE
;
1035 ahc_set_transaction_status(scb
, CAM_AUTOSENSE_FAIL
);
1038 ahc_set_transaction_status(scb
, CAM_SCSI_STATUS_ERROR
);
1039 /* Freeze the queue until the client sees the error. */
1040 ahc_freeze_devq(ahc
, scb
);
1041 ahc_freeze_scb(scb
);
1042 ahc_set_scsi_status(scb
, hscb
->shared_data
.status
.scsi_status
);
1043 switch (hscb
->shared_data
.status
.scsi_status
) {
1044 case SCSI_STATUS_OK
:
1045 printk("%s: Interrupted for status of 0???\n",
1048 case SCSI_STATUS_CMD_TERMINATED
:
1049 case SCSI_STATUS_CHECK_COND
:
1051 struct ahc_dma_seg
*sg
;
1052 struct scsi_sense
*sc
;
1053 struct ahc_initiator_tinfo
*targ_info
;
1054 struct ahc_tmode_tstate
*tstate
;
1055 struct ahc_transinfo
*tinfo
;
1057 if (ahc_debug
& AHC_SHOW_SENSE
) {
1058 ahc_print_path(ahc
, scb
);
1059 printk("SCB %d: requests Check Status\n",
1064 if (ahc_perform_autosense(scb
) == 0)
1067 targ_info
= ahc_fetch_transinfo(ahc
,
1072 tinfo
= &targ_info
->curr
;
1074 sc
= (struct scsi_sense
*)(&hscb
->shared_data
.cdb
);
1076 * Save off the residual if there is one.
1078 ahc_update_residual(ahc
, scb
);
1080 if (ahc_debug
& AHC_SHOW_SENSE
) {
1081 ahc_print_path(ahc
, scb
);
1082 printk("Sending Sense\n");
1085 sg
->addr
= ahc_get_sense_bufaddr(ahc
, scb
);
1086 sg
->len
= ahc_get_sense_bufsize(ahc
, scb
);
1087 sg
->len
|= AHC_DMA_LAST_SEG
;
1089 /* Fixup byte order */
1090 sg
->addr
= ahc_htole32(sg
->addr
);
1091 sg
->len
= ahc_htole32(sg
->len
);
1093 sc
->opcode
= REQUEST_SENSE
;
1095 if (tinfo
->protocol_version
<= SCSI_REV_2
1096 && SCB_GET_LUN(scb
) < 8)
1097 sc
->byte2
= SCB_GET_LUN(scb
) << 5;
1100 sc
->length
= sg
->len
;
1104 * We can't allow the target to disconnect.
1105 * This will be an untagged transaction and
1106 * having the target disconnect will make this
1107 * transaction indestinguishable from outstanding
1108 * tagged transactions.
1113 * This request sense could be because the
1114 * the device lost power or in some other
1115 * way has lost our transfer negotiations.
1116 * Renegotiate if appropriate. Unit attention
1117 * errors will be reported before any data
1120 if (ahc_get_residual(scb
)
1121 == ahc_get_transfer_length(scb
)) {
1122 ahc_update_neg_request(ahc
, &devinfo
,
1124 AHC_NEG_IF_NON_ASYNC
);
1126 if (tstate
->auto_negotiate
& devinfo
.target_mask
) {
1127 hscb
->control
|= MK_MESSAGE
;
1128 scb
->flags
&= ~SCB_NEGOTIATE
;
1129 scb
->flags
|= SCB_AUTO_NEGOTIATE
;
1131 hscb
->cdb_len
= sizeof(*sc
);
1132 hscb
->dataptr
= sg
->addr
;
1133 hscb
->datacnt
= sg
->len
;
1134 hscb
->sgptr
= scb
->sg_list_phys
| SG_FULL_RESID
;
1135 hscb
->sgptr
= ahc_htole32(hscb
->sgptr
);
1137 scb
->flags
|= SCB_SENSE
;
1138 ahc_qinfifo_requeue_tail(ahc
, scb
);
1139 ahc_outb(ahc
, RETURN_1
, SEND_SENSE
);
1141 * Ensure we have enough time to actually
1142 * retrieve the sense.
1144 ahc_scb_timer_reset(scb
, 5 * 1000000);
1154 /* Ensure we don't leave the selection hardware on */
1155 ahc_outb(ahc
, SCSISEQ
,
1156 ahc_inb(ahc
, SCSISEQ
) & (ENSELI
|ENRSELI
|ENAUTOATNP
));
1158 printk("%s:%c:%d: no active SCB for reconnecting "
1159 "target - issuing BUS DEVICE RESET\n",
1160 ahc_name(ahc
), devinfo
.channel
, devinfo
.target
);
1161 printk("SAVED_SCSIID == 0x%x, SAVED_LUN == 0x%x, "
1162 "ARG_1 == 0x%x ACCUM = 0x%x\n",
1163 ahc_inb(ahc
, SAVED_SCSIID
), ahc_inb(ahc
, SAVED_LUN
),
1164 ahc_inb(ahc
, ARG_1
), ahc_inb(ahc
, ACCUM
));
1165 printk("SEQ_FLAGS == 0x%x, SCBPTR == 0x%x, BTT == 0x%x, "
1167 ahc_inb(ahc
, SEQ_FLAGS
), ahc_inb(ahc
, SCBPTR
),
1168 ahc_index_busy_tcl(ahc
,
1169 BUILD_TCL(ahc_inb(ahc
, SAVED_SCSIID
),
1170 ahc_inb(ahc
, SAVED_LUN
))),
1171 ahc_inb(ahc
, SINDEX
));
1172 printk("SCSIID == 0x%x, SCB_SCSIID == 0x%x, SCB_LUN == 0x%x, "
1173 "SCB_TAG == 0x%x, SCB_CONTROL == 0x%x\n",
1174 ahc_inb(ahc
, SCSIID
), ahc_inb(ahc
, SCB_SCSIID
),
1175 ahc_inb(ahc
, SCB_LUN
), ahc_inb(ahc
, SCB_TAG
),
1176 ahc_inb(ahc
, SCB_CONTROL
));
1177 printk("SCSIBUSL == 0x%x, SCSISIGI == 0x%x\n",
1178 ahc_inb(ahc
, SCSIBUSL
), ahc_inb(ahc
, SCSISIGI
));
1179 printk("SXFRCTL0 == 0x%x\n", ahc_inb(ahc
, SXFRCTL0
));
1180 printk("SEQCTL == 0x%x\n", ahc_inb(ahc
, SEQCTL
));
1181 ahc_dump_card_state(ahc
);
1182 ahc
->msgout_buf
[0] = MSG_BUS_DEV_RESET
;
1183 ahc
->msgout_len
= 1;
1184 ahc
->msgout_index
= 0;
1185 ahc
->msg_type
= MSG_TYPE_INITIATOR_MSGOUT
;
1186 ahc_outb(ahc
, MSG_OUT
, HOST_MSG
);
1187 ahc_assert_atn(ahc
);
1192 u_int rejbyte
= ahc_inb(ahc
, ACCUM
);
1193 printk("%s:%c:%d: Warning - unknown message received from "
1194 "target (0x%x). Rejecting\n",
1195 ahc_name(ahc
), devinfo
.channel
, devinfo
.target
, rejbyte
);
1198 case PROTO_VIOLATION
:
1200 ahc_handle_proto_violation(ahc
);
1204 ahc_handle_ign_wide_residue(ahc
, &devinfo
);
1207 ahc_reinitialize_dataptrs(ahc
);
1213 lastphase
= ahc_inb(ahc
, LASTPHASE
);
1214 printk("%s:%c:%d: unknown scsi bus phase %x, "
1215 "lastphase = 0x%x. Attempting to continue\n",
1216 ahc_name(ahc
), devinfo
.channel
, devinfo
.target
,
1217 lastphase
, ahc_inb(ahc
, SCSISIGI
));
1220 case MISSED_BUSFREE
:
1224 lastphase
= ahc_inb(ahc
, LASTPHASE
);
1225 printk("%s:%c:%d: Missed busfree. "
1226 "Lastphase = 0x%x, Curphase = 0x%x\n",
1227 ahc_name(ahc
), devinfo
.channel
, devinfo
.target
,
1228 lastphase
, ahc_inb(ahc
, SCSISIGI
));
1235 * The sequencer has encountered a message phase
1236 * that requires host assistance for completion.
1237 * While handling the message phase(s), we will be
1238 * notified by the sequencer after each byte is
1239 * transferred so we can track bus phase changes.
1241 * If this is the first time we've seen a HOST_MSG_LOOP
1242 * interrupt, initialize the state of the host message
1245 if (ahc
->msg_type
== MSG_TYPE_NONE
) {
1250 bus_phase
= ahc_inb(ahc
, SCSISIGI
) & PHASE_MASK
;
1251 if (bus_phase
!= P_MESGIN
1252 && bus_phase
!= P_MESGOUT
) {
1253 printk("ahc_intr: HOST_MSG_LOOP bad "
1257 * Probably transitioned to bus free before
1258 * we got here. Just punt the message.
1260 ahc_clear_intstat(ahc
);
1265 scb_index
= ahc_inb(ahc
, SCB_TAG
);
1266 scb
= ahc_lookup_scb(ahc
, scb_index
);
1267 if (devinfo
.role
== ROLE_INITIATOR
) {
1268 if (bus_phase
== P_MESGOUT
) {
1270 panic("HOST_MSG_LOOP with "
1274 ahc_setup_initiator_msgout(ahc
,
1279 MSG_TYPE_INITIATOR_MSGIN
;
1280 ahc
->msgin_index
= 0;
1283 #ifdef AHC_TARGET_MODE
1285 if (bus_phase
== P_MESGOUT
) {
1287 MSG_TYPE_TARGET_MSGOUT
;
1288 ahc
->msgin_index
= 0;
1291 ahc_setup_target_msgin(ahc
,
1298 ahc_handle_message_phase(ahc
);
1304 * If we've cleared the parity error interrupt
1305 * but the sequencer still believes that SCSIPERR
1306 * is true, it must be that the parity error is
1307 * for the currently presented byte on the bus,
1308 * and we are not in a phase (data-in) where we will
1309 * eventually ack this byte. Ack the byte and
1310 * throw it away in the hope that the target will
1311 * take us to message out to deliver the appropriate
1314 if ((intstat
& SCSIINT
) == 0
1315 && (ahc_inb(ahc
, SSTAT1
) & SCSIPERR
) != 0) {
1317 if ((ahc
->features
& AHC_DT
) == 0) {
1321 * The hardware will only let you ack bytes
1322 * if the expected phase in SCSISIGO matches
1323 * the current phase. Make sure this is
1324 * currently the case.
1326 curphase
= ahc_inb(ahc
, SCSISIGI
) & PHASE_MASK
;
1327 ahc_outb(ahc
, LASTPHASE
, curphase
);
1328 ahc_outb(ahc
, SCSISIGO
, curphase
);
1330 if ((ahc_inb(ahc
, SCSISIGI
) & (CDI
|MSGI
)) == 0) {
1334 * In a data phase. Faster to bitbucket
1335 * the data than to individually ack each
1336 * byte. This is also the only strategy
1337 * that will work with AUTOACK enabled.
1339 ahc_outb(ahc
, SXFRCTL1
,
1340 ahc_inb(ahc
, SXFRCTL1
) | BITBUCKET
);
1342 while (--wait
!= 0) {
1343 if ((ahc_inb(ahc
, SCSISIGI
)
1348 ahc_outb(ahc
, SXFRCTL1
,
1349 ahc_inb(ahc
, SXFRCTL1
) & ~BITBUCKET
);
1354 ahc_print_devinfo(ahc
, &devinfo
);
1355 printk("Unable to clear parity error. "
1356 "Resetting bus.\n");
1357 scb_index
= ahc_inb(ahc
, SCB_TAG
);
1358 scb
= ahc_lookup_scb(ahc
, scb_index
);
1360 ahc_set_transaction_status(scb
,
1362 ahc_reset_channel(ahc
, devinfo
.channel
,
1363 /*init reset*/TRUE
);
1366 ahc_inb(ahc
, SCSIDATL
);
1374 * When the sequencer detects an overrun, it
1375 * places the controller in "BITBUCKET" mode
1376 * and allows the target to complete its transfer.
1377 * Unfortunately, none of the counters get updated
1378 * when the controller is in this mode, so we have
1379 * no way of knowing how large the overrun was.
1381 u_int scbindex
= ahc_inb(ahc
, SCB_TAG
);
1382 u_int lastphase
= ahc_inb(ahc
, LASTPHASE
);
1385 scb
= ahc_lookup_scb(ahc
, scbindex
);
1386 for (i
= 0; i
< num_phases
; i
++) {
1387 if (lastphase
== ahc_phase_table
[i
].phase
)
1390 ahc_print_path(ahc
, scb
);
1391 printk("data overrun detected %s."
1393 ahc_phase_table
[i
].phasemsg
,
1395 ahc_print_path(ahc
, scb
);
1396 printk("%s seen Data Phase. Length = %ld. NumSGs = %d.\n",
1397 ahc_inb(ahc
, SEQ_FLAGS
) & DPHASE
? "Have" : "Haven't",
1398 ahc_get_transfer_length(scb
), scb
->sg_count
);
1399 if (scb
->sg_count
> 0) {
1400 for (i
= 0; i
< scb
->sg_count
; i
++) {
1402 printk("sg[%d] - Addr 0x%x%x : Length %d\n",
1404 (ahc_le32toh(scb
->sg_list
[i
].len
) >> 24
1405 & SG_HIGH_ADDR_BITS
),
1406 ahc_le32toh(scb
->sg_list
[i
].addr
),
1407 ahc_le32toh(scb
->sg_list
[i
].len
)
1412 * Set this and it will take effect when the
1413 * target does a command complete.
1415 ahc_freeze_devq(ahc
, scb
);
1416 if ((scb
->flags
& SCB_SENSE
) == 0) {
1417 ahc_set_transaction_status(scb
, CAM_DATA_RUN_ERR
);
1419 scb
->flags
&= ~SCB_SENSE
;
1420 ahc_set_transaction_status(scb
, CAM_AUTOSENSE_FAIL
);
1422 ahc_freeze_scb(scb
);
1424 if ((ahc
->features
& AHC_ULTRA2
) != 0) {
1426 * Clear the channel in case we return
1427 * to data phase later.
1429 ahc_outb(ahc
, SXFRCTL0
,
1430 ahc_inb(ahc
, SXFRCTL0
) | CLRSTCNT
|CLRCHN
);
1431 ahc_outb(ahc
, SXFRCTL0
,
1432 ahc_inb(ahc
, SXFRCTL0
) | CLRSTCNT
|CLRCHN
);
1434 if ((ahc
->flags
& AHC_39BIT_ADDRESSING
) != 0) {
1437 /* Ensure HHADDR is 0 for future DMA operations. */
1438 dscommand1
= ahc_inb(ahc
, DSCOMMAND1
);
1439 ahc_outb(ahc
, DSCOMMAND1
, dscommand1
| HADDLDSEL0
);
1440 ahc_outb(ahc
, HADDR
, 0);
1441 ahc_outb(ahc
, DSCOMMAND1
, dscommand1
);
1449 printk("%s:%c:%d:%d: Attempt to issue message failed\n",
1450 ahc_name(ahc
), devinfo
.channel
, devinfo
.target
,
1452 scbindex
= ahc_inb(ahc
, SCB_TAG
);
1453 scb
= ahc_lookup_scb(ahc
, scbindex
);
1455 && (scb
->flags
& SCB_RECOVERY_SCB
) != 0)
1457 * Ensure that we didn't put a second instance of this
1458 * SCB into the QINFIFO.
1460 ahc_search_qinfifo(ahc
, SCB_GET_TARGET(ahc
, scb
),
1461 SCB_GET_CHANNEL(ahc
, scb
),
1462 SCB_GET_LUN(scb
), scb
->hscb
->tag
,
1463 ROLE_INITIATOR
, /*status*/0,
1469 printk("%s: No free or disconnected SCBs\n", ahc_name(ahc
));
1470 ahc_dump_card_state(ahc
);
1471 panic("for safety");
1478 scbptr
= ahc_inb(ahc
, SCBPTR
);
1479 printk("Bogus TAG after DMA. SCBPTR %d, tag %d, our tag %d\n",
1480 scbptr
, ahc_inb(ahc
, ARG_1
),
1481 ahc
->scb_data
->hscbs
[scbptr
].tag
);
1482 ahc_dump_card_state(ahc
);
1483 panic("for safety");
1488 printk("%s: BTT calculation out of range\n", ahc_name(ahc
));
1489 printk("SAVED_SCSIID == 0x%x, SAVED_LUN == 0x%x, "
1490 "ARG_1 == 0x%x ACCUM = 0x%x\n",
1491 ahc_inb(ahc
, SAVED_SCSIID
), ahc_inb(ahc
, SAVED_LUN
),
1492 ahc_inb(ahc
, ARG_1
), ahc_inb(ahc
, ACCUM
));
1493 printk("SEQ_FLAGS == 0x%x, SCBPTR == 0x%x, BTT == 0x%x, "
1494 "SINDEX == 0x%x\n, A == 0x%x\n",
1495 ahc_inb(ahc
, SEQ_FLAGS
), ahc_inb(ahc
, SCBPTR
),
1496 ahc_index_busy_tcl(ahc
,
1497 BUILD_TCL(ahc_inb(ahc
, SAVED_SCSIID
),
1498 ahc_inb(ahc
, SAVED_LUN
))),
1499 ahc_inb(ahc
, SINDEX
),
1500 ahc_inb(ahc
, ACCUM
));
1501 printk("SCSIID == 0x%x, SCB_SCSIID == 0x%x, SCB_LUN == 0x%x, "
1502 "SCB_TAG == 0x%x, SCB_CONTROL == 0x%x\n",
1503 ahc_inb(ahc
, SCSIID
), ahc_inb(ahc
, SCB_SCSIID
),
1504 ahc_inb(ahc
, SCB_LUN
), ahc_inb(ahc
, SCB_TAG
),
1505 ahc_inb(ahc
, SCB_CONTROL
));
1506 printk("SCSIBUSL == 0x%x, SCSISIGI == 0x%x\n",
1507 ahc_inb(ahc
, SCSIBUSL
), ahc_inb(ahc
, SCSISIGI
));
1508 ahc_dump_card_state(ahc
);
1509 panic("for safety");
1513 printk("ahc_intr: seqint, "
1514 "intstat == 0x%x, scsisigi = 0x%x\n",
1515 intstat
, ahc_inb(ahc
, SCSISIGI
));
1520 * The sequencer is paused immediately on
1521 * a SEQINT, so we should restart it when
1528 ahc_handle_scsiint(struct ahc_softc
*ahc
, u_int intstat
)
1537 if ((ahc
->features
& AHC_TWIN
) != 0
1538 && ((ahc_inb(ahc
, SBLKCTL
) & SELBUSB
) != 0))
1542 intr_channel
= cur_channel
;
1544 if ((ahc
->features
& AHC_ULTRA2
) != 0)
1545 status0
= ahc_inb(ahc
, SSTAT0
) & IOERR
;
1548 status
= ahc_inb(ahc
, SSTAT1
) & (SELTO
|SCSIRSTI
|BUSFREE
|SCSIPERR
);
1549 if (status
== 0 && status0
== 0) {
1550 if ((ahc
->features
& AHC_TWIN
) != 0) {
1551 /* Try the other channel */
1552 ahc_outb(ahc
, SBLKCTL
, ahc_inb(ahc
, SBLKCTL
) ^ SELBUSB
);
1553 status
= ahc_inb(ahc
, SSTAT1
)
1554 & (SELTO
|SCSIRSTI
|BUSFREE
|SCSIPERR
);
1555 intr_channel
= (cur_channel
== 'A') ? 'B' : 'A';
1558 printk("%s: Spurious SCSI interrupt\n", ahc_name(ahc
));
1559 ahc_outb(ahc
, CLRINT
, CLRSCSIINT
);
1565 /* Make sure the sequencer is in a safe location. */
1566 ahc_clear_critical_section(ahc
);
1568 scb_index
= ahc_inb(ahc
, SCB_TAG
);
1569 scb
= ahc_lookup_scb(ahc
, scb_index
);
1571 && (ahc_inb(ahc
, SEQ_FLAGS
) & NOT_IDENTIFIED
) != 0)
1574 if ((ahc
->features
& AHC_ULTRA2
) != 0
1575 && (status0
& IOERR
) != 0) {
1578 now_lvd
= ahc_inb(ahc
, SBLKCTL
) & ENAB40
;
1579 printk("%s: Transceiver State Has Changed to %s mode\n",
1580 ahc_name(ahc
), now_lvd
? "LVD" : "SE");
1581 ahc_outb(ahc
, CLRSINT0
, CLRIOERR
);
1583 * When transitioning to SE mode, the reset line
1584 * glitches, triggering an arbitration bug in some
1585 * Ultra2 controllers. This bug is cleared when we
1586 * assert the reset line. Since a reset glitch has
1587 * already occurred with this transition and a
1588 * transceiver state change is handled just like
1589 * a bus reset anyway, asserting the reset line
1590 * ourselves is safe.
1592 ahc_reset_channel(ahc
, intr_channel
,
1593 /*Initiate Reset*/now_lvd
== 0);
1594 } else if ((status
& SCSIRSTI
) != 0) {
1595 printk("%s: Someone reset channel %c\n",
1596 ahc_name(ahc
), intr_channel
);
1597 if (intr_channel
!= cur_channel
)
1598 ahc_outb(ahc
, SBLKCTL
, ahc_inb(ahc
, SBLKCTL
) ^ SELBUSB
);
1599 ahc_reset_channel(ahc
, intr_channel
, /*Initiate Reset*/FALSE
);
1600 } else if ((status
& SCSIPERR
) != 0) {
1602 * Determine the bus phase and queue an appropriate message.
1603 * SCSIPERR is latched true as soon as a parity error
1604 * occurs. If the sequencer acked the transfer that
1605 * caused the parity error and the currently presented
1606 * transfer on the bus has correct parity, SCSIPERR will
1607 * be cleared by CLRSCSIPERR. Use this to determine if
1608 * we should look at the last phase the sequencer recorded,
1609 * or the current phase presented on the bus.
1611 struct ahc_devinfo devinfo
;
1621 lastphase
= ahc_inb(ahc
, LASTPHASE
);
1622 curphase
= ahc_inb(ahc
, SCSISIGI
) & PHASE_MASK
;
1623 sstat2
= ahc_inb(ahc
, SSTAT2
);
1624 ahc_outb(ahc
, CLRSINT1
, CLRSCSIPERR
);
1626 * For all phases save DATA, the sequencer won't
1627 * automatically ack a byte that has a parity error
1628 * in it. So the only way that the current phase
1629 * could be 'data-in' is if the parity error is for
1630 * an already acked byte in the data phase. During
1631 * synchronous data-in transfers, we may actually
1632 * ack bytes before latching the current phase in
1633 * LASTPHASE, leading to the discrepancy between
1634 * curphase and lastphase.
1636 if ((ahc_inb(ahc
, SSTAT1
) & SCSIPERR
) != 0
1637 || curphase
== P_DATAIN
|| curphase
== P_DATAIN_DT
)
1638 errorphase
= curphase
;
1640 errorphase
= lastphase
;
1642 for (i
= 0; i
< num_phases
; i
++) {
1643 if (errorphase
== ahc_phase_table
[i
].phase
)
1646 mesg_out
= ahc_phase_table
[i
].mesg_out
;
1649 if (SCB_IS_SILENT(scb
))
1652 ahc_print_path(ahc
, scb
);
1653 scb
->flags
|= SCB_TRANSMISSION_ERROR
;
1655 printk("%s:%c:%d: ", ahc_name(ahc
), intr_channel
,
1656 SCSIID_TARGET(ahc
, ahc_inb(ahc
, SAVED_SCSIID
)));
1657 scsirate
= ahc_inb(ahc
, SCSIRATE
);
1658 if (silent
== FALSE
) {
1659 printk("parity error detected %s. "
1660 "SEQADDR(0x%x) SCSIRATE(0x%x)\n",
1661 ahc_phase_table
[i
].phasemsg
,
1662 ahc_inw(ahc
, SEQADDR0
),
1664 if ((ahc
->features
& AHC_DT
) != 0) {
1665 if ((sstat2
& CRCVALERR
) != 0)
1666 printk("\tCRC Value Mismatch\n");
1667 if ((sstat2
& CRCENDERR
) != 0)
1668 printk("\tNo terminal CRC packet "
1670 if ((sstat2
& CRCREQERR
) != 0)
1671 printk("\tIllegal CRC packet "
1673 if ((sstat2
& DUAL_EDGE_ERR
) != 0)
1674 printk("\tUnexpected %sDT Data Phase\n",
1675 (scsirate
& SINGLE_EDGE
)
1680 if ((ahc
->features
& AHC_DT
) != 0
1681 && (sstat2
& DUAL_EDGE_ERR
) != 0) {
1683 * This error applies regardless of
1684 * data direction, so ignore the value
1685 * in the phase table.
1687 mesg_out
= MSG_INITIATOR_DET_ERR
;
1691 * We've set the hardware to assert ATN if we
1692 * get a parity error on "in" phases, so all we
1693 * need to do is stuff the message buffer with
1694 * the appropriate message. "In" phases have set
1695 * mesg_out to something other than MSG_NOP.
1697 if (mesg_out
!= MSG_NOOP
) {
1698 if (ahc
->msg_type
!= MSG_TYPE_NONE
)
1699 ahc
->send_msg_perror
= TRUE
;
1701 ahc_outb(ahc
, MSG_OUT
, mesg_out
);
1704 * Force a renegotiation with this target just in
1705 * case we are out of sync for some external reason
1706 * unknown (or unreported) by the target.
1708 ahc_fetch_devinfo(ahc
, &devinfo
);
1709 ahc_force_renegotiation(ahc
, &devinfo
);
1711 ahc_outb(ahc
, CLRINT
, CLRSCSIINT
);
1713 } else if ((status
& SELTO
) != 0) {
1716 /* Stop the selection */
1717 ahc_outb(ahc
, SCSISEQ
, 0);
1719 /* No more pending messages */
1720 ahc_clear_msg_state(ahc
);
1722 /* Clear interrupt state */
1723 ahc_outb(ahc
, SIMODE1
, ahc_inb(ahc
, SIMODE1
) & ~ENBUSFREE
);
1724 ahc_outb(ahc
, CLRSINT1
, CLRSELTIMEO
|CLRBUSFREE
|CLRSCSIPERR
);
1727 * Although the driver does not care about the
1728 * 'Selection in Progress' status bit, the busy
1729 * LED does. SELINGO is only cleared by a successful
1730 * selection, so we must manually clear it to insure
1731 * the LED turns off just incase no future successful
1732 * selections occur (e.g. no devices on the bus).
1734 ahc_outb(ahc
, CLRSINT0
, CLRSELINGO
);
1736 scbptr
= ahc_inb(ahc
, WAITING_SCBH
);
1737 ahc_outb(ahc
, SCBPTR
, scbptr
);
1738 scb_index
= ahc_inb(ahc
, SCB_TAG
);
1740 scb
= ahc_lookup_scb(ahc
, scb_index
);
1742 printk("%s: ahc_intr - referenced scb not "
1743 "valid during SELTO scb(%d, %d)\n",
1744 ahc_name(ahc
), scbptr
, scb_index
);
1745 ahc_dump_card_state(ahc
);
1747 struct ahc_devinfo devinfo
;
1749 if ((ahc_debug
& AHC_SHOW_SELTO
) != 0) {
1750 ahc_print_path(ahc
, scb
);
1751 printk("Saw Selection Timeout for SCB 0x%x\n",
1755 ahc_scb_devinfo(ahc
, &devinfo
, scb
);
1756 ahc_set_transaction_status(scb
, CAM_SEL_TIMEOUT
);
1757 ahc_freeze_devq(ahc
, scb
);
1760 * Cancel any pending transactions on the device
1761 * now that it seems to be missing. This will
1762 * also revert us to async/narrow transfers until
1763 * we can renegotiate with the device.
1765 ahc_handle_devreset(ahc
, &devinfo
,
1767 "Selection Timeout",
1768 /*verbose_level*/1);
1770 ahc_outb(ahc
, CLRINT
, CLRSCSIINT
);
1772 } else if ((status
& BUSFREE
) != 0
1773 && (ahc_inb(ahc
, SIMODE1
) & ENBUSFREE
) != 0) {
1774 struct ahc_devinfo devinfo
;
1779 u_int initiator_role_id
;
1784 * Clear our selection hardware as soon as possible.
1785 * We may have an entry in the waiting Q for this target,
1786 * that is affected by this busfree and we don't want to
1787 * go about selecting the target while we handle the event.
1789 ahc_outb(ahc
, SCSISEQ
,
1790 ahc_inb(ahc
, SCSISEQ
) & (ENSELI
|ENRSELI
|ENAUTOATNP
));
1793 * Disable busfree interrupts and clear the busfree
1794 * interrupt status. We do this here so that several
1795 * bus transactions occur prior to clearing the SCSIINT
1796 * latch. It can take a bit for the clearing to take effect.
1798 ahc_outb(ahc
, SIMODE1
, ahc_inb(ahc
, SIMODE1
) & ~ENBUSFREE
);
1799 ahc_outb(ahc
, CLRSINT1
, CLRBUSFREE
|CLRSCSIPERR
);
1802 * Look at what phase we were last in.
1803 * If its message out, chances are pretty good
1804 * that the busfree was in response to one of
1805 * our abort requests.
1807 lastphase
= ahc_inb(ahc
, LASTPHASE
);
1808 saved_scsiid
= ahc_inb(ahc
, SAVED_SCSIID
);
1809 saved_lun
= ahc_inb(ahc
, SAVED_LUN
);
1810 target
= SCSIID_TARGET(ahc
, saved_scsiid
);
1811 initiator_role_id
= SCSIID_OUR_ID(saved_scsiid
);
1812 channel
= SCSIID_CHANNEL(ahc
, saved_scsiid
);
1813 ahc_compile_devinfo(&devinfo
, initiator_role_id
,
1814 target
, saved_lun
, channel
, ROLE_INITIATOR
);
1817 if (lastphase
== P_MESGOUT
) {
1820 tag
= SCB_LIST_NULL
;
1821 if (ahc_sent_msg(ahc
, AHCMSG_1B
, MSG_ABORT_TAG
, TRUE
)
1822 || ahc_sent_msg(ahc
, AHCMSG_1B
, MSG_ABORT
, TRUE
)) {
1823 if (ahc
->msgout_buf
[ahc
->msgout_index
- 1]
1825 tag
= scb
->hscb
->tag
;
1826 ahc_print_path(ahc
, scb
);
1827 printk("SCB %d - Abort%s Completed.\n",
1828 scb
->hscb
->tag
, tag
== SCB_LIST_NULL
?
1830 ahc_abort_scbs(ahc
, target
, channel
,
1835 } else if (ahc_sent_msg(ahc
, AHCMSG_1B
,
1836 MSG_BUS_DEV_RESET
, TRUE
)) {
1837 ahc_compile_devinfo(&devinfo
,
1843 ahc_handle_devreset(ahc
, &devinfo
,
1846 /*verbose_level*/0);
1848 } else if (ahc_sent_msg(ahc
, AHCMSG_EXT
,
1849 MSG_EXT_PPR
, FALSE
)) {
1850 struct ahc_initiator_tinfo
*tinfo
;
1851 struct ahc_tmode_tstate
*tstate
;
1854 * PPR Rejected. Try non-ppr negotiation
1855 * and retry command.
1857 tinfo
= ahc_fetch_transinfo(ahc
,
1862 tinfo
->curr
.transport_version
= 2;
1863 tinfo
->goal
.transport_version
= 2;
1864 tinfo
->goal
.ppr_options
= 0;
1865 ahc_qinfifo_requeue_tail(ahc
, scb
);
1867 } else if (ahc_sent_msg(ahc
, AHCMSG_EXT
,
1868 MSG_EXT_WDTR
, FALSE
)) {
1870 * Negotiation Rejected. Go-narrow and
1873 ahc_set_width(ahc
, &devinfo
,
1874 MSG_EXT_WDTR_BUS_8_BIT
,
1875 AHC_TRANS_CUR
|AHC_TRANS_GOAL
,
1877 ahc_qinfifo_requeue_tail(ahc
, scb
);
1879 } else if (ahc_sent_msg(ahc
, AHCMSG_EXT
,
1880 MSG_EXT_SDTR
, FALSE
)) {
1882 * Negotiation Rejected. Go-async and
1885 ahc_set_syncrate(ahc
, &devinfo
,
1887 /*period*/0, /*offset*/0,
1889 AHC_TRANS_CUR
|AHC_TRANS_GOAL
,
1891 ahc_qinfifo_requeue_tail(ahc
, scb
);
1895 if (printerror
!= 0) {
1901 if ((scb
->hscb
->control
& TAG_ENB
) != 0)
1902 tag
= scb
->hscb
->tag
;
1904 tag
= SCB_LIST_NULL
;
1905 ahc_print_path(ahc
, scb
);
1906 ahc_abort_scbs(ahc
, target
, channel
,
1907 SCB_GET_LUN(scb
), tag
,
1912 * We had not fully identified this connection,
1913 * so we cannot abort anything.
1915 printk("%s: ", ahc_name(ahc
));
1917 for (i
= 0; i
< num_phases
; i
++) {
1918 if (lastphase
== ahc_phase_table
[i
].phase
)
1921 if (lastphase
!= P_BUSFREE
) {
1923 * Renegotiate with this device at the
1924 * next opportunity just in case this busfree
1925 * is due to a negotiation mismatch with the
1928 ahc_force_renegotiation(ahc
, &devinfo
);
1930 printk("Unexpected busfree %s\n"
1931 "SEQADDR == 0x%x\n",
1932 ahc_phase_table
[i
].phasemsg
,
1933 ahc_inb(ahc
, SEQADDR0
)
1934 | (ahc_inb(ahc
, SEQADDR1
) << 8));
1936 ahc_outb(ahc
, CLRINT
, CLRSCSIINT
);
1939 printk("%s: Missing case in ahc_handle_scsiint. status = %x\n",
1940 ahc_name(ahc
), status
);
1941 ahc_outb(ahc
, CLRINT
, CLRSCSIINT
);
1946 * Force renegotiation to occur the next time we initiate
1947 * a command to the current device.
1950 ahc_force_renegotiation(struct ahc_softc
*ahc
, struct ahc_devinfo
*devinfo
)
1952 struct ahc_initiator_tinfo
*targ_info
;
1953 struct ahc_tmode_tstate
*tstate
;
1955 targ_info
= ahc_fetch_transinfo(ahc
,
1957 devinfo
->our_scsiid
,
1960 ahc_update_neg_request(ahc
, devinfo
, tstate
,
1961 targ_info
, AHC_NEG_IF_NON_ASYNC
);
1964 #define AHC_MAX_STEPS 2000
1966 ahc_clear_critical_section(struct ahc_softc
*ahc
)
1973 if (ahc
->num_critical_sections
== 0)
1985 seqaddr
= ahc_inb(ahc
, SEQADDR0
)
1986 | (ahc_inb(ahc
, SEQADDR1
) << 8);
1989 * Seqaddr represents the next instruction to execute,
1990 * so we are really executing the instruction just
1995 cs
= ahc
->critical_sections
;
1996 for (i
= 0; i
< ahc
->num_critical_sections
; i
++, cs
++) {
1998 if (cs
->begin
< seqaddr
&& cs
->end
>= seqaddr
)
2002 if (i
== ahc
->num_critical_sections
)
2005 if (steps
> AHC_MAX_STEPS
) {
2006 printk("%s: Infinite loop in critical section\n",
2008 ahc_dump_card_state(ahc
);
2009 panic("critical section loop");
2013 if (stepping
== FALSE
) {
2016 * Disable all interrupt sources so that the
2017 * sequencer will not be stuck by a pausing
2018 * interrupt condition while we attempt to
2019 * leave a critical section.
2021 simode0
= ahc_inb(ahc
, SIMODE0
);
2022 ahc_outb(ahc
, SIMODE0
, 0);
2023 simode1
= ahc_inb(ahc
, SIMODE1
);
2024 if ((ahc
->features
& AHC_DT
) != 0)
2026 * On DT class controllers, we
2027 * use the enhanced busfree logic.
2028 * Unfortunately we cannot re-enable
2029 * busfree detection within the
2030 * current connection, so we must
2031 * leave it on while single stepping.
2033 ahc_outb(ahc
, SIMODE1
, simode1
& ENBUSFREE
);
2035 ahc_outb(ahc
, SIMODE1
, 0);
2036 ahc_outb(ahc
, CLRINT
, CLRSCSIINT
);
2037 ahc_outb(ahc
, SEQCTL
, ahc
->seqctl
| STEP
);
2040 if ((ahc
->features
& AHC_DT
) != 0) {
2041 ahc_outb(ahc
, CLRSINT1
, CLRBUSFREE
);
2042 ahc_outb(ahc
, CLRINT
, CLRSCSIINT
);
2044 ahc_outb(ahc
, HCNTRL
, ahc
->unpause
);
2045 while (!ahc_is_paused(ahc
))
2049 ahc_outb(ahc
, SIMODE0
, simode0
);
2050 ahc_outb(ahc
, SIMODE1
, simode1
);
2051 ahc_outb(ahc
, SEQCTL
, ahc
->seqctl
);
2056 * Clear any pending interrupt status.
2059 ahc_clear_intstat(struct ahc_softc
*ahc
)
2061 /* Clear any interrupt conditions this may have caused */
2062 ahc_outb(ahc
, CLRSINT1
, CLRSELTIMEO
|CLRATNO
|CLRSCSIRSTI
2063 |CLRBUSFREE
|CLRSCSIPERR
|CLRPHASECHG
|
2065 ahc_flush_device_writes(ahc
);
2066 ahc_outb(ahc
, CLRSINT0
, CLRSELDO
|CLRSELDI
|CLRSELINGO
);
2067 ahc_flush_device_writes(ahc
);
2068 ahc_outb(ahc
, CLRINT
, CLRSCSIINT
);
2069 ahc_flush_device_writes(ahc
);
2072 /**************************** Debugging Routines ******************************/
2074 uint32_t ahc_debug
= AHC_DEBUG_OPTS
;
2079 ahc_print_scb(struct scb
*scb
)
2083 struct hardware_scb
*hscb
= scb
->hscb
;
2085 printk("scb:%p control:0x%x scsiid:0x%x lun:%d cdb_len:%d\n",
2091 printk("Shared Data: ");
2092 for (i
= 0; i
< sizeof(hscb
->shared_data
.cdb
); i
++)
2093 printk("%#02x", hscb
->shared_data
.cdb
[i
]);
2094 printk(" dataptr:%#x datacnt:%#x sgptr:%#x tag:%#x\n",
2095 ahc_le32toh(hscb
->dataptr
),
2096 ahc_le32toh(hscb
->datacnt
),
2097 ahc_le32toh(hscb
->sgptr
),
2099 if (scb
->sg_count
> 0) {
2100 for (i
= 0; i
< scb
->sg_count
; i
++) {
2101 printk("sg[%d] - Addr 0x%x%x : Length %d\n",
2103 (ahc_le32toh(scb
->sg_list
[i
].len
) >> 24
2104 & SG_HIGH_ADDR_BITS
),
2105 ahc_le32toh(scb
->sg_list
[i
].addr
),
2106 ahc_le32toh(scb
->sg_list
[i
].len
));
2112 /************************* Transfer Negotiation *******************************/
2114 * Allocate per target mode instance (ID we respond to as a target)
2115 * transfer negotiation data structures.
2117 static struct ahc_tmode_tstate
*
2118 ahc_alloc_tstate(struct ahc_softc
*ahc
, u_int scsi_id
, char channel
)
2120 struct ahc_tmode_tstate
*master_tstate
;
2121 struct ahc_tmode_tstate
*tstate
;
2124 master_tstate
= ahc
->enabled_targets
[ahc
->our_id
];
2125 if (channel
== 'B') {
2127 master_tstate
= ahc
->enabled_targets
[ahc
->our_id_b
+ 8];
2129 if (ahc
->enabled_targets
[scsi_id
] != NULL
2130 && ahc
->enabled_targets
[scsi_id
] != master_tstate
)
2131 panic("%s: ahc_alloc_tstate - Target already allocated",
2133 tstate
= kmalloc(sizeof(*tstate
), GFP_ATOMIC
);
2138 * If we have allocated a master tstate, copy user settings from
2139 * the master tstate (taken from SRAM or the EEPROM) for this
2140 * channel, but reset our current and goal settings to async/narrow
2141 * until an initiator talks to us.
2143 if (master_tstate
!= NULL
) {
2144 memcpy(tstate
, master_tstate
, sizeof(*tstate
));
2145 memset(tstate
->enabled_luns
, 0, sizeof(tstate
->enabled_luns
));
2146 tstate
->ultraenb
= 0;
2147 for (i
= 0; i
< AHC_NUM_TARGETS
; i
++) {
2148 memset(&tstate
->transinfo
[i
].curr
, 0,
2149 sizeof(tstate
->transinfo
[i
].curr
));
2150 memset(&tstate
->transinfo
[i
].goal
, 0,
2151 sizeof(tstate
->transinfo
[i
].goal
));
2154 memset(tstate
, 0, sizeof(*tstate
));
2155 ahc
->enabled_targets
[scsi_id
] = tstate
;
2159 #ifdef AHC_TARGET_MODE
2161 * Free per target mode instance (ID we respond to as a target)
2162 * transfer negotiation data structures.
2165 ahc_free_tstate(struct ahc_softc
*ahc
, u_int scsi_id
, char channel
, int force
)
2167 struct ahc_tmode_tstate
*tstate
;
2170 * Don't clean up our "master" tstate.
2171 * It has our default user settings.
2173 if (((channel
== 'B' && scsi_id
== ahc
->our_id_b
)
2174 || (channel
== 'A' && scsi_id
== ahc
->our_id
))
2180 tstate
= ahc
->enabled_targets
[scsi_id
];
2183 ahc
->enabled_targets
[scsi_id
] = NULL
;
2188 * Called when we have an active connection to a target on the bus,
2189 * this function finds the nearest syncrate to the input period limited
2190 * by the capabilities of the bus connectivity of and sync settings for
2193 static const struct ahc_syncrate
*
2194 ahc_devlimited_syncrate(struct ahc_softc
*ahc
,
2195 struct ahc_initiator_tinfo
*tinfo
,
2196 u_int
*period
, u_int
*ppr_options
, role_t role
)
2198 struct ahc_transinfo
*transinfo
;
2201 if ((ahc
->features
& AHC_ULTRA2
) != 0) {
2202 if ((ahc_inb(ahc
, SBLKCTL
) & ENAB40
) != 0
2203 && (ahc_inb(ahc
, SSTAT2
) & EXP_ACTIVE
) == 0) {
2204 maxsync
= AHC_SYNCRATE_DT
;
2206 maxsync
= AHC_SYNCRATE_ULTRA
;
2207 /* Can't do DT on an SE bus */
2208 *ppr_options
&= ~MSG_EXT_PPR_DT_REQ
;
2210 } else if ((ahc
->features
& AHC_ULTRA
) != 0) {
2211 maxsync
= AHC_SYNCRATE_ULTRA
;
2213 maxsync
= AHC_SYNCRATE_FAST
;
2216 * Never allow a value higher than our current goal
2217 * period otherwise we may allow a target initiated
2218 * negotiation to go above the limit as set by the
2219 * user. In the case of an initiator initiated
2220 * sync negotiation, we limit based on the user
2221 * setting. This allows the system to still accept
2222 * incoming negotiations even if target initiated
2223 * negotiation is not performed.
2225 if (role
== ROLE_TARGET
)
2226 transinfo
= &tinfo
->user
;
2228 transinfo
= &tinfo
->goal
;
2229 *ppr_options
&= transinfo
->ppr_options
;
2230 if (transinfo
->width
== MSG_EXT_WDTR_BUS_8_BIT
) {
2231 maxsync
= max(maxsync
, (u_int
)AHC_SYNCRATE_ULTRA2
);
2232 *ppr_options
&= ~MSG_EXT_PPR_DT_REQ
;
2234 if (transinfo
->period
== 0) {
2239 *period
= max(*period
, (u_int
)transinfo
->period
);
2240 return (ahc_find_syncrate(ahc
, period
, ppr_options
, maxsync
));
2244 * Look up the valid period to SCSIRATE conversion in our table.
2245 * Return the period and offset that should be sent to the target
2246 * if this was the beginning of an SDTR.
2248 const struct ahc_syncrate
*
2249 ahc_find_syncrate(struct ahc_softc
*ahc
, u_int
*period
,
2250 u_int
*ppr_options
, u_int maxsync
)
2252 const struct ahc_syncrate
*syncrate
;
2254 if ((ahc
->features
& AHC_DT
) == 0)
2255 *ppr_options
&= ~MSG_EXT_PPR_DT_REQ
;
2257 /* Skip all DT only entries if DT is not available */
2258 if ((*ppr_options
& MSG_EXT_PPR_DT_REQ
) == 0
2259 && maxsync
< AHC_SYNCRATE_ULTRA2
)
2260 maxsync
= AHC_SYNCRATE_ULTRA2
;
2262 /* Now set the maxsync based on the card capabilities
2263 * DT is already done above */
2264 if ((ahc
->features
& (AHC_DT
| AHC_ULTRA2
)) == 0
2265 && maxsync
< AHC_SYNCRATE_ULTRA
)
2266 maxsync
= AHC_SYNCRATE_ULTRA
;
2267 if ((ahc
->features
& (AHC_DT
| AHC_ULTRA2
| AHC_ULTRA
)) == 0
2268 && maxsync
< AHC_SYNCRATE_FAST
)
2269 maxsync
= AHC_SYNCRATE_FAST
;
2271 for (syncrate
= &ahc_syncrates
[maxsync
];
2272 syncrate
->rate
!= NULL
;
2276 * The Ultra2 table doesn't go as low
2277 * as for the Fast/Ultra cards.
2279 if ((ahc
->features
& AHC_ULTRA2
) != 0
2280 && (syncrate
->sxfr_u2
== 0))
2283 if (*period
<= syncrate
->period
) {
2285 * When responding to a target that requests
2286 * sync, the requested rate may fall between
2287 * two rates that we can output, but still be
2288 * a rate that we can receive. Because of this,
2289 * we want to respond to the target with
2290 * the same rate that it sent to us even
2291 * if the period we use to send data to it
2292 * is lower. Only lower the response period
2295 if (syncrate
== &ahc_syncrates
[maxsync
])
2296 *period
= syncrate
->period
;
2299 * At some speeds, we only support
2302 if ((syncrate
->sxfr_u2
& ST_SXFR
) != 0)
2303 *ppr_options
&= ~MSG_EXT_PPR_DT_REQ
;
2309 || (syncrate
->rate
== NULL
)
2310 || ((ahc
->features
& AHC_ULTRA2
) != 0
2311 && (syncrate
->sxfr_u2
== 0))) {
2312 /* Use asynchronous transfers. */
2315 *ppr_options
&= ~MSG_EXT_PPR_DT_REQ
;
2321 * Convert from an entry in our syncrate table to the SCSI equivalent
2322 * sync "period" factor.
2325 ahc_find_period(struct ahc_softc
*ahc
, u_int scsirate
, u_int maxsync
)
2327 const struct ahc_syncrate
*syncrate
;
2329 if ((ahc
->features
& AHC_ULTRA2
) != 0)
2330 scsirate
&= SXFR_ULTRA2
;
2334 /* now set maxsync based on card capabilities */
2335 if ((ahc
->features
& AHC_DT
) == 0 && maxsync
< AHC_SYNCRATE_ULTRA2
)
2336 maxsync
= AHC_SYNCRATE_ULTRA2
;
2337 if ((ahc
->features
& (AHC_DT
| AHC_ULTRA2
)) == 0
2338 && maxsync
< AHC_SYNCRATE_ULTRA
)
2339 maxsync
= AHC_SYNCRATE_ULTRA
;
2340 if ((ahc
->features
& (AHC_DT
| AHC_ULTRA2
| AHC_ULTRA
)) == 0
2341 && maxsync
< AHC_SYNCRATE_FAST
)
2342 maxsync
= AHC_SYNCRATE_FAST
;
2345 syncrate
= &ahc_syncrates
[maxsync
];
2346 while (syncrate
->rate
!= NULL
) {
2348 if ((ahc
->features
& AHC_ULTRA2
) != 0) {
2349 if (syncrate
->sxfr_u2
== 0)
2351 else if (scsirate
== (syncrate
->sxfr_u2
& SXFR_ULTRA2
))
2352 return (syncrate
->period
);
2353 } else if (scsirate
== (syncrate
->sxfr
& SXFR
)) {
2354 return (syncrate
->period
);
2358 return (0); /* async */
2362 * Truncate the given synchronous offset to a value the
2363 * current adapter type and syncrate are capable of.
2366 ahc_validate_offset(struct ahc_softc
*ahc
,
2367 struct ahc_initiator_tinfo
*tinfo
,
2368 const struct ahc_syncrate
*syncrate
,
2369 u_int
*offset
, int wide
, role_t role
)
2373 /* Limit offset to what we can do */
2374 if (syncrate
== NULL
) {
2376 } else if ((ahc
->features
& AHC_ULTRA2
) != 0) {
2377 maxoffset
= MAX_OFFSET_ULTRA2
;
2380 maxoffset
= MAX_OFFSET_16BIT
;
2382 maxoffset
= MAX_OFFSET_8BIT
;
2384 *offset
= min(*offset
, maxoffset
);
2385 if (tinfo
!= NULL
) {
2386 if (role
== ROLE_TARGET
)
2387 *offset
= min(*offset
, (u_int
)tinfo
->user
.offset
);
2389 *offset
= min(*offset
, (u_int
)tinfo
->goal
.offset
);
2394 * Truncate the given transfer width parameter to a value the
2395 * current adapter type is capable of.
2398 ahc_validate_width(struct ahc_softc
*ahc
, struct ahc_initiator_tinfo
*tinfo
,
2399 u_int
*bus_width
, role_t role
)
2401 switch (*bus_width
) {
2403 if (ahc
->features
& AHC_WIDE
) {
2405 *bus_width
= MSG_EXT_WDTR_BUS_16_BIT
;
2409 case MSG_EXT_WDTR_BUS_8_BIT
:
2410 *bus_width
= MSG_EXT_WDTR_BUS_8_BIT
;
2413 if (tinfo
!= NULL
) {
2414 if (role
== ROLE_TARGET
)
2415 *bus_width
= min((u_int
)tinfo
->user
.width
, *bus_width
);
2417 *bus_width
= min((u_int
)tinfo
->goal
.width
, *bus_width
);
2422 * Update the bitmask of targets for which the controller should
2423 * negotiate with at the next convenient opportunity. This currently
2424 * means the next time we send the initial identify messages for
2425 * a new transaction.
2428 ahc_update_neg_request(struct ahc_softc
*ahc
, struct ahc_devinfo
*devinfo
,
2429 struct ahc_tmode_tstate
*tstate
,
2430 struct ahc_initiator_tinfo
*tinfo
, ahc_neg_type neg_type
)
2432 u_int auto_negotiate_orig
;
2434 auto_negotiate_orig
= tstate
->auto_negotiate
;
2435 if (neg_type
== AHC_NEG_ALWAYS
) {
2437 * Force our "current" settings to be
2438 * unknown so that unless a bus reset
2439 * occurs the need to renegotiate is
2440 * recorded persistently.
2442 if ((ahc
->features
& AHC_WIDE
) != 0)
2443 tinfo
->curr
.width
= AHC_WIDTH_UNKNOWN
;
2444 tinfo
->curr
.period
= AHC_PERIOD_UNKNOWN
;
2445 tinfo
->curr
.offset
= AHC_OFFSET_UNKNOWN
;
2447 if (tinfo
->curr
.period
!= tinfo
->goal
.period
2448 || tinfo
->curr
.width
!= tinfo
->goal
.width
2449 || tinfo
->curr
.offset
!= tinfo
->goal
.offset
2450 || tinfo
->curr
.ppr_options
!= tinfo
->goal
.ppr_options
2451 || (neg_type
== AHC_NEG_IF_NON_ASYNC
2452 && (tinfo
->goal
.offset
!= 0
2453 || tinfo
->goal
.width
!= MSG_EXT_WDTR_BUS_8_BIT
2454 || tinfo
->goal
.ppr_options
!= 0)))
2455 tstate
->auto_negotiate
|= devinfo
->target_mask
;
2457 tstate
->auto_negotiate
&= ~devinfo
->target_mask
;
2459 return (auto_negotiate_orig
!= tstate
->auto_negotiate
);
2463 * Update the user/goal/curr tables of synchronous negotiation
2464 * parameters as well as, in the case of a current or active update,
2465 * any data structures on the host controller. In the case of an
2466 * active update, the specified target is currently talking to us on
2467 * the bus, so the transfer parameter update must take effect
2471 ahc_set_syncrate(struct ahc_softc
*ahc
, struct ahc_devinfo
*devinfo
,
2472 const struct ahc_syncrate
*syncrate
, u_int period
,
2473 u_int offset
, u_int ppr_options
, u_int type
, int paused
)
2475 struct ahc_initiator_tinfo
*tinfo
;
2476 struct ahc_tmode_tstate
*tstate
;
2483 active
= (type
& AHC_TRANS_ACTIVE
) == AHC_TRANS_ACTIVE
;
2486 if (syncrate
== NULL
) {
2491 tinfo
= ahc_fetch_transinfo(ahc
, devinfo
->channel
, devinfo
->our_scsiid
,
2492 devinfo
->target
, &tstate
);
2494 if ((type
& AHC_TRANS_USER
) != 0) {
2495 tinfo
->user
.period
= period
;
2496 tinfo
->user
.offset
= offset
;
2497 tinfo
->user
.ppr_options
= ppr_options
;
2500 if ((type
& AHC_TRANS_GOAL
) != 0) {
2501 tinfo
->goal
.period
= period
;
2502 tinfo
->goal
.offset
= offset
;
2503 tinfo
->goal
.ppr_options
= ppr_options
;
2506 old_period
= tinfo
->curr
.period
;
2507 old_offset
= tinfo
->curr
.offset
;
2508 old_ppr
= tinfo
->curr
.ppr_options
;
2510 if ((type
& AHC_TRANS_CUR
) != 0
2511 && (old_period
!= period
2512 || old_offset
!= offset
2513 || old_ppr
!= ppr_options
)) {
2517 scsirate
= tinfo
->scsirate
;
2518 if ((ahc
->features
& AHC_ULTRA2
) != 0) {
2520 scsirate
&= ~(SXFR_ULTRA2
|SINGLE_EDGE
|ENABLE_CRC
);
2521 if (syncrate
!= NULL
) {
2522 scsirate
|= syncrate
->sxfr_u2
;
2523 if ((ppr_options
& MSG_EXT_PPR_DT_REQ
) != 0)
2524 scsirate
|= ENABLE_CRC
;
2526 scsirate
|= SINGLE_EDGE
;
2530 scsirate
&= ~(SXFR
|SOFS
);
2532 * Ensure Ultra mode is set properly for
2535 tstate
->ultraenb
&= ~devinfo
->target_mask
;
2536 if (syncrate
!= NULL
) {
2537 if (syncrate
->sxfr
& ULTRA_SXFR
) {
2539 devinfo
->target_mask
;
2541 scsirate
|= syncrate
->sxfr
& SXFR
;
2542 scsirate
|= offset
& SOFS
;
2547 sxfrctl0
= ahc_inb(ahc
, SXFRCTL0
);
2548 sxfrctl0
&= ~FAST20
;
2549 if (tstate
->ultraenb
& devinfo
->target_mask
)
2551 ahc_outb(ahc
, SXFRCTL0
, sxfrctl0
);
2555 ahc_outb(ahc
, SCSIRATE
, scsirate
);
2556 if ((ahc
->features
& AHC_ULTRA2
) != 0)
2557 ahc_outb(ahc
, SCSIOFFSET
, offset
);
2560 tinfo
->scsirate
= scsirate
;
2561 tinfo
->curr
.period
= period
;
2562 tinfo
->curr
.offset
= offset
;
2563 tinfo
->curr
.ppr_options
= ppr_options
;
2565 ahc_send_async(ahc
, devinfo
->channel
, devinfo
->target
,
2566 CAM_LUN_WILDCARD
, AC_TRANSFER_NEG
);
2569 printk("%s: target %d synchronous at %sMHz%s, "
2570 "offset = 0x%x\n", ahc_name(ahc
),
2571 devinfo
->target
, syncrate
->rate
,
2572 (ppr_options
& MSG_EXT_PPR_DT_REQ
)
2573 ? " DT" : "", offset
);
2575 printk("%s: target %d using "
2576 "asynchronous transfers\n",
2577 ahc_name(ahc
), devinfo
->target
);
2582 update_needed
+= ahc_update_neg_request(ahc
, devinfo
, tstate
,
2583 tinfo
, AHC_NEG_TO_GOAL
);
2586 ahc_update_pending_scbs(ahc
);
2590 * Update the user/goal/curr tables of wide negotiation
2591 * parameters as well as, in the case of a current or active update,
2592 * any data structures on the host controller. In the case of an
2593 * active update, the specified target is currently talking to us on
2594 * the bus, so the transfer parameter update must take effect
2598 ahc_set_width(struct ahc_softc
*ahc
, struct ahc_devinfo
*devinfo
,
2599 u_int width
, u_int type
, int paused
)
2601 struct ahc_initiator_tinfo
*tinfo
;
2602 struct ahc_tmode_tstate
*tstate
;
2607 active
= (type
& AHC_TRANS_ACTIVE
) == AHC_TRANS_ACTIVE
;
2609 tinfo
= ahc_fetch_transinfo(ahc
, devinfo
->channel
, devinfo
->our_scsiid
,
2610 devinfo
->target
, &tstate
);
2612 if ((type
& AHC_TRANS_USER
) != 0)
2613 tinfo
->user
.width
= width
;
2615 if ((type
& AHC_TRANS_GOAL
) != 0)
2616 tinfo
->goal
.width
= width
;
2618 oldwidth
= tinfo
->curr
.width
;
2619 if ((type
& AHC_TRANS_CUR
) != 0 && oldwidth
!= width
) {
2623 scsirate
= tinfo
->scsirate
;
2624 scsirate
&= ~WIDEXFER
;
2625 if (width
== MSG_EXT_WDTR_BUS_16_BIT
)
2626 scsirate
|= WIDEXFER
;
2628 tinfo
->scsirate
= scsirate
;
2631 ahc_outb(ahc
, SCSIRATE
, scsirate
);
2633 tinfo
->curr
.width
= width
;
2635 ahc_send_async(ahc
, devinfo
->channel
, devinfo
->target
,
2636 CAM_LUN_WILDCARD
, AC_TRANSFER_NEG
);
2638 printk("%s: target %d using %dbit transfers\n",
2639 ahc_name(ahc
), devinfo
->target
,
2640 8 * (0x01 << width
));
2644 update_needed
+= ahc_update_neg_request(ahc
, devinfo
, tstate
,
2645 tinfo
, AHC_NEG_TO_GOAL
);
2647 ahc_update_pending_scbs(ahc
);
2651 * Update the current state of tagged queuing for a given target.
2654 ahc_set_tags(struct ahc_softc
*ahc
, struct scsi_cmnd
*cmd
,
2655 struct ahc_devinfo
*devinfo
, ahc_queue_alg alg
)
2657 struct scsi_device
*sdev
= cmd
->device
;
2659 ahc_platform_set_tags(ahc
, sdev
, devinfo
, alg
);
2660 ahc_send_async(ahc
, devinfo
->channel
, devinfo
->target
,
2661 devinfo
->lun
, AC_TRANSFER_NEG
);
2665 * When the transfer settings for a connection change, update any
2666 * in-transit SCBs to contain the new data so the hardware will
2667 * be set correctly during future (re)selections.
2670 ahc_update_pending_scbs(struct ahc_softc
*ahc
)
2672 struct scb
*pending_scb
;
2673 int pending_scb_count
;
2679 * Traverse the pending SCB list and ensure that all of the
2680 * SCBs there have the proper settings.
2682 pending_scb_count
= 0;
2683 LIST_FOREACH(pending_scb
, &ahc
->pending_scbs
, pending_links
) {
2684 struct ahc_devinfo devinfo
;
2685 struct hardware_scb
*pending_hscb
;
2686 struct ahc_initiator_tinfo
*tinfo
;
2687 struct ahc_tmode_tstate
*tstate
;
2689 ahc_scb_devinfo(ahc
, &devinfo
, pending_scb
);
2690 tinfo
= ahc_fetch_transinfo(ahc
, devinfo
.channel
,
2692 devinfo
.target
, &tstate
);
2693 pending_hscb
= pending_scb
->hscb
;
2694 pending_hscb
->control
&= ~ULTRAENB
;
2695 if ((tstate
->ultraenb
& devinfo
.target_mask
) != 0)
2696 pending_hscb
->control
|= ULTRAENB
;
2697 pending_hscb
->scsirate
= tinfo
->scsirate
;
2698 pending_hscb
->scsioffset
= tinfo
->curr
.offset
;
2699 if ((tstate
->auto_negotiate
& devinfo
.target_mask
) == 0
2700 && (pending_scb
->flags
& SCB_AUTO_NEGOTIATE
) != 0) {
2701 pending_scb
->flags
&= ~SCB_AUTO_NEGOTIATE
;
2702 pending_hscb
->control
&= ~MK_MESSAGE
;
2704 ahc_sync_scb(ahc
, pending_scb
,
2705 BUS_DMASYNC_PREREAD
|BUS_DMASYNC_PREWRITE
);
2706 pending_scb_count
++;
2709 if (pending_scb_count
== 0)
2712 if (ahc_is_paused(ahc
)) {
2719 saved_scbptr
= ahc_inb(ahc
, SCBPTR
);
2720 /* Ensure that the hscbs down on the card match the new information */
2721 for (i
= 0; i
< ahc
->scb_data
->maxhscbs
; i
++) {
2722 struct hardware_scb
*pending_hscb
;
2726 ahc_outb(ahc
, SCBPTR
, i
);
2727 scb_tag
= ahc_inb(ahc
, SCB_TAG
);
2728 pending_scb
= ahc_lookup_scb(ahc
, scb_tag
);
2729 if (pending_scb
== NULL
)
2732 pending_hscb
= pending_scb
->hscb
;
2733 control
= ahc_inb(ahc
, SCB_CONTROL
);
2734 control
&= ~(ULTRAENB
|MK_MESSAGE
);
2735 control
|= pending_hscb
->control
& (ULTRAENB
|MK_MESSAGE
);
2736 ahc_outb(ahc
, SCB_CONTROL
, control
);
2737 ahc_outb(ahc
, SCB_SCSIRATE
, pending_hscb
->scsirate
);
2738 ahc_outb(ahc
, SCB_SCSIOFFSET
, pending_hscb
->scsioffset
);
2740 ahc_outb(ahc
, SCBPTR
, saved_scbptr
);
2746 /**************************** Pathing Information *****************************/
2748 ahc_fetch_devinfo(struct ahc_softc
*ahc
, struct ahc_devinfo
*devinfo
)
2754 if (ahc_inb(ahc
, SSTAT0
) & TARGET
)
2757 role
= ROLE_INITIATOR
;
2759 if (role
== ROLE_TARGET
2760 && (ahc
->features
& AHC_MULTI_TID
) != 0
2761 && (ahc_inb(ahc
, SEQ_FLAGS
)
2762 & (CMDPHASE_PENDING
|TARG_CMD_PENDING
|NO_DISCONNECT
)) != 0) {
2763 /* We were selected, so pull our id from TARGIDIN */
2764 our_id
= ahc_inb(ahc
, TARGIDIN
) & OID
;
2765 } else if ((ahc
->features
& AHC_ULTRA2
) != 0)
2766 our_id
= ahc_inb(ahc
, SCSIID_ULTRA2
) & OID
;
2768 our_id
= ahc_inb(ahc
, SCSIID
) & OID
;
2770 saved_scsiid
= ahc_inb(ahc
, SAVED_SCSIID
);
2771 ahc_compile_devinfo(devinfo
,
2773 SCSIID_TARGET(ahc
, saved_scsiid
),
2774 ahc_inb(ahc
, SAVED_LUN
),
2775 SCSIID_CHANNEL(ahc
, saved_scsiid
),
2779 static const struct ahc_phase_table_entry
*
2780 ahc_lookup_phase_entry(int phase
)
2782 const struct ahc_phase_table_entry
*entry
;
2783 const struct ahc_phase_table_entry
*last_entry
;
2786 * num_phases doesn't include the default entry which
2787 * will be returned if the phase doesn't match.
2789 last_entry
= &ahc_phase_table
[num_phases
];
2790 for (entry
= ahc_phase_table
; entry
< last_entry
; entry
++) {
2791 if (phase
== entry
->phase
)
2798 ahc_compile_devinfo(struct ahc_devinfo
*devinfo
, u_int our_id
, u_int target
,
2799 u_int lun
, char channel
, role_t role
)
2801 devinfo
->our_scsiid
= our_id
;
2802 devinfo
->target
= target
;
2804 devinfo
->target_offset
= target
;
2805 devinfo
->channel
= channel
;
2806 devinfo
->role
= role
;
2808 devinfo
->target_offset
+= 8;
2809 devinfo
->target_mask
= (0x01 << devinfo
->target_offset
);
2813 ahc_print_devinfo(struct ahc_softc
*ahc
, struct ahc_devinfo
*devinfo
)
2815 printk("%s:%c:%d:%d: ", ahc_name(ahc
), devinfo
->channel
,
2816 devinfo
->target
, devinfo
->lun
);
2820 ahc_scb_devinfo(struct ahc_softc
*ahc
, struct ahc_devinfo
*devinfo
,
2826 our_id
= SCSIID_OUR_ID(scb
->hscb
->scsiid
);
2827 role
= ROLE_INITIATOR
;
2828 if ((scb
->flags
& SCB_TARGET_SCB
) != 0)
2830 ahc_compile_devinfo(devinfo
, our_id
, SCB_GET_TARGET(ahc
, scb
),
2831 SCB_GET_LUN(scb
), SCB_GET_CHANNEL(ahc
, scb
), role
);
2835 /************************ Message Phase Processing ****************************/
2837 ahc_assert_atn(struct ahc_softc
*ahc
)
2842 if ((ahc
->features
& AHC_DT
) == 0)
2843 scsisigo
|= ahc_inb(ahc
, SCSISIGI
);
2844 ahc_outb(ahc
, SCSISIGO
, scsisigo
);
2848 * When an initiator transaction with the MK_MESSAGE flag either reconnects
2849 * or enters the initial message out phase, we are interrupted. Fill our
2850 * outgoing message buffer with the appropriate message and beging handing
2851 * the message phase(s) manually.
2854 ahc_setup_initiator_msgout(struct ahc_softc
*ahc
, struct ahc_devinfo
*devinfo
,
2858 * To facilitate adding multiple messages together,
2859 * each routine should increment the index and len
2860 * variables instead of setting them explicitly.
2862 ahc
->msgout_index
= 0;
2863 ahc
->msgout_len
= 0;
2865 if ((scb
->flags
& SCB_DEVICE_RESET
) == 0
2866 && ahc_inb(ahc
, MSG_OUT
) == MSG_IDENTIFYFLAG
) {
2869 identify_msg
= MSG_IDENTIFYFLAG
| SCB_GET_LUN(scb
);
2870 if ((scb
->hscb
->control
& DISCENB
) != 0)
2871 identify_msg
|= MSG_IDENTIFY_DISCFLAG
;
2872 ahc
->msgout_buf
[ahc
->msgout_index
++] = identify_msg
;
2875 if ((scb
->hscb
->control
& TAG_ENB
) != 0) {
2876 ahc
->msgout_buf
[ahc
->msgout_index
++] =
2877 scb
->hscb
->control
& (TAG_ENB
|SCB_TAG_TYPE
);
2878 ahc
->msgout_buf
[ahc
->msgout_index
++] = scb
->hscb
->tag
;
2879 ahc
->msgout_len
+= 2;
2883 if (scb
->flags
& SCB_DEVICE_RESET
) {
2884 ahc
->msgout_buf
[ahc
->msgout_index
++] = MSG_BUS_DEV_RESET
;
2886 ahc_print_path(ahc
, scb
);
2887 printk("Bus Device Reset Message Sent\n");
2889 * Clear our selection hardware in advance of
2890 * the busfree. We may have an entry in the waiting
2891 * Q for this target, and we don't want to go about
2892 * selecting while we handle the busfree and blow it
2895 ahc_outb(ahc
, SCSISEQ
, (ahc_inb(ahc
, SCSISEQ
) & ~ENSELO
));
2896 } else if ((scb
->flags
& SCB_ABORT
) != 0) {
2897 if ((scb
->hscb
->control
& TAG_ENB
) != 0)
2898 ahc
->msgout_buf
[ahc
->msgout_index
++] = MSG_ABORT_TAG
;
2900 ahc
->msgout_buf
[ahc
->msgout_index
++] = MSG_ABORT
;
2902 ahc_print_path(ahc
, scb
);
2903 printk("Abort%s Message Sent\n",
2904 (scb
->hscb
->control
& TAG_ENB
) != 0 ? " Tag" : "");
2906 * Clear our selection hardware in advance of
2907 * the busfree. We may have an entry in the waiting
2908 * Q for this target, and we don't want to go about
2909 * selecting while we handle the busfree and blow it
2912 ahc_outb(ahc
, SCSISEQ
, (ahc_inb(ahc
, SCSISEQ
) & ~ENSELO
));
2913 } else if ((scb
->flags
& (SCB_AUTO_NEGOTIATE
|SCB_NEGOTIATE
)) != 0) {
2914 ahc_build_transfer_msg(ahc
, devinfo
);
2916 printk("ahc_intr: AWAITING_MSG for an SCB that "
2917 "does not have a waiting message\n");
2918 printk("SCSIID = %x, target_mask = %x\n", scb
->hscb
->scsiid
,
2919 devinfo
->target_mask
);
2920 panic("SCB = %d, SCB Control = %x, MSG_OUT = %x "
2921 "SCB flags = %x", scb
->hscb
->tag
, scb
->hscb
->control
,
2922 ahc_inb(ahc
, MSG_OUT
), scb
->flags
);
2926 * Clear the MK_MESSAGE flag from the SCB so we aren't
2927 * asked to send this message again.
2929 ahc_outb(ahc
, SCB_CONTROL
, ahc_inb(ahc
, SCB_CONTROL
) & ~MK_MESSAGE
);
2930 scb
->hscb
->control
&= ~MK_MESSAGE
;
2931 ahc
->msgout_index
= 0;
2932 ahc
->msg_type
= MSG_TYPE_INITIATOR_MSGOUT
;
2936 * Build an appropriate transfer negotiation message for the
2937 * currently active target.
2940 ahc_build_transfer_msg(struct ahc_softc
*ahc
, struct ahc_devinfo
*devinfo
)
2943 * We need to initiate transfer negotiations.
2944 * If our current and goal settings are identical,
2945 * we want to renegotiate due to a check condition.
2947 struct ahc_initiator_tinfo
*tinfo
;
2948 struct ahc_tmode_tstate
*tstate
;
2949 const struct ahc_syncrate
*rate
;
2957 tinfo
= ahc_fetch_transinfo(ahc
, devinfo
->channel
, devinfo
->our_scsiid
,
2958 devinfo
->target
, &tstate
);
2960 * Filter our period based on the current connection.
2961 * If we can't perform DT transfers on this segment (not in LVD
2962 * mode for instance), then our decision to issue a PPR message
2965 period
= tinfo
->goal
.period
;
2966 offset
= tinfo
->goal
.offset
;
2967 ppr_options
= tinfo
->goal
.ppr_options
;
2968 /* Target initiated PPR is not allowed in the SCSI spec */
2969 if (devinfo
->role
== ROLE_TARGET
)
2971 rate
= ahc_devlimited_syncrate(ahc
, tinfo
, &period
,
2972 &ppr_options
, devinfo
->role
);
2973 dowide
= tinfo
->curr
.width
!= tinfo
->goal
.width
;
2974 dosync
= tinfo
->curr
.offset
!= offset
|| tinfo
->curr
.period
!= period
;
2976 * Only use PPR if we have options that need it, even if the device
2977 * claims to support it. There might be an expander in the way
2980 doppr
= ppr_options
!= 0;
2982 if (!dowide
&& !dosync
&& !doppr
) {
2983 dowide
= tinfo
->goal
.width
!= MSG_EXT_WDTR_BUS_8_BIT
;
2984 dosync
= tinfo
->goal
.offset
!= 0;
2987 if (!dowide
&& !dosync
&& !doppr
) {
2989 * Force async with a WDTR message if we have a wide bus,
2990 * or just issue an SDTR with a 0 offset.
2992 if ((ahc
->features
& AHC_WIDE
) != 0)
2998 ahc_print_devinfo(ahc
, devinfo
);
2999 printk("Ensuring async\n");
3003 /* Target initiated PPR is not allowed in the SCSI spec */
3004 if (devinfo
->role
== ROLE_TARGET
)
3008 * Both the PPR message and SDTR message require the
3009 * goal syncrate to be limited to what the target device
3010 * is capable of handling (based on whether an LVD->SE
3011 * expander is on the bus), so combine these two cases.
3012 * Regardless, guarantee that if we are using WDTR and SDTR
3013 * messages that WDTR comes first.
3015 if (doppr
|| (dosync
&& !dowide
)) {
3017 offset
= tinfo
->goal
.offset
;
3018 ahc_validate_offset(ahc
, tinfo
, rate
, &offset
,
3019 doppr
? tinfo
->goal
.width
3020 : tinfo
->curr
.width
,
3023 ahc_construct_ppr(ahc
, devinfo
, period
, offset
,
3024 tinfo
->goal
.width
, ppr_options
);
3026 ahc_construct_sdtr(ahc
, devinfo
, period
, offset
);
3029 ahc_construct_wdtr(ahc
, devinfo
, tinfo
->goal
.width
);
3034 * Build a synchronous negotiation message in our message
3035 * buffer based on the input parameters.
3038 ahc_construct_sdtr(struct ahc_softc
*ahc
, struct ahc_devinfo
*devinfo
,
3039 u_int period
, u_int offset
)
3042 period
= AHC_ASYNC_XFER_PERIOD
;
3043 ahc
->msgout_index
+= spi_populate_sync_msg(
3044 ahc
->msgout_buf
+ ahc
->msgout_index
, period
, offset
);
3045 ahc
->msgout_len
+= 5;
3047 printk("(%s:%c:%d:%d): Sending SDTR period %x, offset %x\n",
3048 ahc_name(ahc
), devinfo
->channel
, devinfo
->target
,
3049 devinfo
->lun
, period
, offset
);
3054 * Build a wide negotiation message in our message
3055 * buffer based on the input parameters.
3058 ahc_construct_wdtr(struct ahc_softc
*ahc
, struct ahc_devinfo
*devinfo
,
3061 ahc
->msgout_index
+= spi_populate_width_msg(
3062 ahc
->msgout_buf
+ ahc
->msgout_index
, bus_width
);
3063 ahc
->msgout_len
+= 4;
3065 printk("(%s:%c:%d:%d): Sending WDTR %x\n",
3066 ahc_name(ahc
), devinfo
->channel
, devinfo
->target
,
3067 devinfo
->lun
, bus_width
);
3072 * Build a parallel protocol request message in our message
3073 * buffer based on the input parameters.
3076 ahc_construct_ppr(struct ahc_softc
*ahc
, struct ahc_devinfo
*devinfo
,
3077 u_int period
, u_int offset
, u_int bus_width
,
3081 period
= AHC_ASYNC_XFER_PERIOD
;
3082 ahc
->msgout_index
+= spi_populate_ppr_msg(
3083 ahc
->msgout_buf
+ ahc
->msgout_index
, period
, offset
,
3084 bus_width
, ppr_options
);
3085 ahc
->msgout_len
+= 8;
3087 printk("(%s:%c:%d:%d): Sending PPR bus_width %x, period %x, "
3088 "offset %x, ppr_options %x\n", ahc_name(ahc
),
3089 devinfo
->channel
, devinfo
->target
, devinfo
->lun
,
3090 bus_width
, period
, offset
, ppr_options
);
3095 * Clear any active message state.
3098 ahc_clear_msg_state(struct ahc_softc
*ahc
)
3100 ahc
->msgout_len
= 0;
3101 ahc
->msgin_index
= 0;
3102 ahc
->msg_type
= MSG_TYPE_NONE
;
3103 if ((ahc_inb(ahc
, SCSISIGI
) & ATNI
) != 0) {
3105 * The target didn't care to respond to our
3106 * message request, so clear ATN.
3108 ahc_outb(ahc
, CLRSINT1
, CLRATNO
);
3110 ahc_outb(ahc
, MSG_OUT
, MSG_NOOP
);
3111 ahc_outb(ahc
, SEQ_FLAGS2
,
3112 ahc_inb(ahc
, SEQ_FLAGS2
) & ~TARGET_MSG_PENDING
);
3116 ahc_handle_proto_violation(struct ahc_softc
*ahc
)
3118 struct ahc_devinfo devinfo
;
3126 ahc_fetch_devinfo(ahc
, &devinfo
);
3127 scbid
= ahc_inb(ahc
, SCB_TAG
);
3128 scb
= ahc_lookup_scb(ahc
, scbid
);
3129 seq_flags
= ahc_inb(ahc
, SEQ_FLAGS
);
3130 curphase
= ahc_inb(ahc
, SCSISIGI
) & PHASE_MASK
;
3131 lastphase
= ahc_inb(ahc
, LASTPHASE
);
3132 if ((seq_flags
& NOT_IDENTIFIED
) != 0) {
3135 * The reconnecting target either did not send an
3136 * identify message, or did, but we didn't find an SCB
3139 ahc_print_devinfo(ahc
, &devinfo
);
3140 printk("Target did not send an IDENTIFY message. "
3141 "LASTPHASE = 0x%x.\n", lastphase
);
3143 } else if (scb
== NULL
) {
3145 * We don't seem to have an SCB active for this
3146 * transaction. Print an error and reset the bus.
3148 ahc_print_devinfo(ahc
, &devinfo
);
3149 printk("No SCB found during protocol violation\n");
3150 goto proto_violation_reset
;
3152 ahc_set_transaction_status(scb
, CAM_SEQUENCE_FAIL
);
3153 if ((seq_flags
& NO_CDB_SENT
) != 0) {
3154 ahc_print_path(ahc
, scb
);
3155 printk("No or incomplete CDB sent to device.\n");
3156 } else if ((ahc_inb(ahc
, SCB_CONTROL
) & STATUS_RCVD
) == 0) {
3158 * The target never bothered to provide status to
3159 * us prior to completing the command. Since we don't
3160 * know the disposition of this command, we must attempt
3161 * to abort it. Assert ATN and prepare to send an abort
3164 ahc_print_path(ahc
, scb
);
3165 printk("Completed command without status.\n");
3167 ahc_print_path(ahc
, scb
);
3168 printk("Unknown protocol violation.\n");
3169 ahc_dump_card_state(ahc
);
3172 if ((lastphase
& ~P_DATAIN_DT
) == 0
3173 || lastphase
== P_COMMAND
) {
3174 proto_violation_reset
:
3176 * Target either went directly to data/command
3177 * phase or didn't respond to our ATN.
3178 * The only safe thing to do is to blow
3179 * it away with a bus reset.
3181 found
= ahc_reset_channel(ahc
, 'A', TRUE
);
3182 printk("%s: Issued Channel %c Bus Reset. "
3183 "%d SCBs aborted\n", ahc_name(ahc
), 'A', found
);
3186 * Leave the selection hardware off in case
3187 * this abort attempt will affect yet to
3190 ahc_outb(ahc
, SCSISEQ
,
3191 ahc_inb(ahc
, SCSISEQ
) & ~ENSELO
);
3192 ahc_assert_atn(ahc
);
3193 ahc_outb(ahc
, MSG_OUT
, HOST_MSG
);
3195 ahc_print_devinfo(ahc
, &devinfo
);
3196 ahc
->msgout_buf
[0] = MSG_ABORT_TASK
;
3197 ahc
->msgout_len
= 1;
3198 ahc
->msgout_index
= 0;
3199 ahc
->msg_type
= MSG_TYPE_INITIATOR_MSGOUT
;
3201 ahc_print_path(ahc
, scb
);
3202 scb
->flags
|= SCB_ABORT
;
3204 printk("Protocol violation %s. Attempting to abort.\n",
3205 ahc_lookup_phase_entry(curphase
)->phasemsg
);
3210 * Manual message loop handler.
3213 ahc_handle_message_phase(struct ahc_softc
*ahc
)
3215 struct ahc_devinfo devinfo
;
3219 ahc_fetch_devinfo(ahc
, &devinfo
);
3220 end_session
= FALSE
;
3221 bus_phase
= ahc_inb(ahc
, SCSISIGI
) & PHASE_MASK
;
3224 switch (ahc
->msg_type
) {
3225 case MSG_TYPE_INITIATOR_MSGOUT
:
3231 if (ahc
->msgout_len
== 0)
3232 panic("HOST_MSG_LOOP interrupt with no active message");
3235 if ((ahc_debug
& AHC_SHOW_MESSAGES
) != 0) {
3236 ahc_print_devinfo(ahc
, &devinfo
);
3237 printk("INITIATOR_MSG_OUT");
3240 phasemis
= bus_phase
!= P_MESGOUT
;
3243 if ((ahc_debug
& AHC_SHOW_MESSAGES
) != 0) {
3244 printk(" PHASEMIS %s\n",
3245 ahc_lookup_phase_entry(bus_phase
)
3249 if (bus_phase
== P_MESGIN
) {
3251 * Change gears and see if
3252 * this messages is of interest to
3253 * us or should be passed back to
3256 ahc_outb(ahc
, CLRSINT1
, CLRATNO
);
3257 ahc
->send_msg_perror
= FALSE
;
3258 ahc
->msg_type
= MSG_TYPE_INITIATOR_MSGIN
;
3259 ahc
->msgin_index
= 0;
3266 if (ahc
->send_msg_perror
) {
3267 ahc_outb(ahc
, CLRSINT1
, CLRATNO
);
3268 ahc_outb(ahc
, CLRSINT1
, CLRREQINIT
);
3270 if ((ahc_debug
& AHC_SHOW_MESSAGES
) != 0)
3271 printk(" byte 0x%x\n", ahc
->send_msg_perror
);
3273 ahc_outb(ahc
, SCSIDATL
, MSG_PARITY_ERROR
);
3277 msgdone
= ahc
->msgout_index
== ahc
->msgout_len
;
3280 * The target has requested a retry.
3281 * Re-assert ATN, reset our message index to
3284 ahc
->msgout_index
= 0;
3285 ahc_assert_atn(ahc
);
3288 lastbyte
= ahc
->msgout_index
== (ahc
->msgout_len
- 1);
3290 /* Last byte is signified by dropping ATN */
3291 ahc_outb(ahc
, CLRSINT1
, CLRATNO
);
3295 * Clear our interrupt status and present
3296 * the next byte on the bus.
3298 ahc_outb(ahc
, CLRSINT1
, CLRREQINIT
);
3300 if ((ahc_debug
& AHC_SHOW_MESSAGES
) != 0)
3301 printk(" byte 0x%x\n",
3302 ahc
->msgout_buf
[ahc
->msgout_index
]);
3304 ahc_outb(ahc
, SCSIDATL
, ahc
->msgout_buf
[ahc
->msgout_index
++]);
3307 case MSG_TYPE_INITIATOR_MSGIN
:
3313 if ((ahc_debug
& AHC_SHOW_MESSAGES
) != 0) {
3314 ahc_print_devinfo(ahc
, &devinfo
);
3315 printk("INITIATOR_MSG_IN");
3318 phasemis
= bus_phase
!= P_MESGIN
;
3321 if ((ahc_debug
& AHC_SHOW_MESSAGES
) != 0) {
3322 printk(" PHASEMIS %s\n",
3323 ahc_lookup_phase_entry(bus_phase
)
3327 ahc
->msgin_index
= 0;
3328 if (bus_phase
== P_MESGOUT
3329 && (ahc
->send_msg_perror
== TRUE
3330 || (ahc
->msgout_len
!= 0
3331 && ahc
->msgout_index
== 0))) {
3332 ahc
->msg_type
= MSG_TYPE_INITIATOR_MSGOUT
;
3339 /* Pull the byte in without acking it */
3340 ahc
->msgin_buf
[ahc
->msgin_index
] = ahc_inb(ahc
, SCSIBUSL
);
3342 if ((ahc_debug
& AHC_SHOW_MESSAGES
) != 0)
3343 printk(" byte 0x%x\n",
3344 ahc
->msgin_buf
[ahc
->msgin_index
]);
3347 message_done
= ahc_parse_msg(ahc
, &devinfo
);
3351 * Clear our incoming message buffer in case there
3352 * is another message following this one.
3354 ahc
->msgin_index
= 0;
3357 * If this message illicited a response,
3358 * assert ATN so the target takes us to the
3359 * message out phase.
3361 if (ahc
->msgout_len
!= 0) {
3363 if ((ahc_debug
& AHC_SHOW_MESSAGES
) != 0) {
3364 ahc_print_devinfo(ahc
, &devinfo
);
3365 printk("Asserting ATN for response\n");
3368 ahc_assert_atn(ahc
);
3373 if (message_done
== MSGLOOP_TERMINATED
) {
3377 ahc_outb(ahc
, CLRSINT1
, CLRREQINIT
);
3378 ahc_inb(ahc
, SCSIDATL
);
3382 case MSG_TYPE_TARGET_MSGIN
:
3387 if (ahc
->msgout_len
== 0)
3388 panic("Target MSGIN with no active message");
3391 * If we interrupted a mesgout session, the initiator
3392 * will not know this until our first REQ. So, we
3393 * only honor mesgout requests after we've sent our
3396 if ((ahc_inb(ahc
, SCSISIGI
) & ATNI
) != 0
3397 && ahc
->msgout_index
> 0)
3398 msgout_request
= TRUE
;
3400 msgout_request
= FALSE
;
3402 if (msgout_request
) {
3405 * Change gears and see if
3406 * this messages is of interest to
3407 * us or should be passed back to
3410 ahc
->msg_type
= MSG_TYPE_TARGET_MSGOUT
;
3411 ahc_outb(ahc
, SCSISIGO
, P_MESGOUT
| BSYO
);
3412 ahc
->msgin_index
= 0;
3413 /* Dummy read to REQ for first byte */
3414 ahc_inb(ahc
, SCSIDATL
);
3415 ahc_outb(ahc
, SXFRCTL0
,
3416 ahc_inb(ahc
, SXFRCTL0
) | SPIOEN
);
3420 msgdone
= ahc
->msgout_index
== ahc
->msgout_len
;
3422 ahc_outb(ahc
, SXFRCTL0
,
3423 ahc_inb(ahc
, SXFRCTL0
) & ~SPIOEN
);
3429 * Present the next byte on the bus.
3431 ahc_outb(ahc
, SXFRCTL0
, ahc_inb(ahc
, SXFRCTL0
) | SPIOEN
);
3432 ahc_outb(ahc
, SCSIDATL
, ahc
->msgout_buf
[ahc
->msgout_index
++]);
3435 case MSG_TYPE_TARGET_MSGOUT
:
3441 * The initiator signals that this is
3442 * the last byte by dropping ATN.
3444 lastbyte
= (ahc_inb(ahc
, SCSISIGI
) & ATNI
) == 0;
3447 * Read the latched byte, but turn off SPIOEN first
3448 * so that we don't inadvertently cause a REQ for the
3451 ahc_outb(ahc
, SXFRCTL0
, ahc_inb(ahc
, SXFRCTL0
) & ~SPIOEN
);
3452 ahc
->msgin_buf
[ahc
->msgin_index
] = ahc_inb(ahc
, SCSIDATL
);
3453 msgdone
= ahc_parse_msg(ahc
, &devinfo
);
3454 if (msgdone
== MSGLOOP_TERMINATED
) {
3456 * The message is *really* done in that it caused
3457 * us to go to bus free. The sequencer has already
3458 * been reset at this point, so pull the ejection
3467 * XXX Read spec about initiator dropping ATN too soon
3468 * and use msgdone to detect it.
3470 if (msgdone
== MSGLOOP_MSGCOMPLETE
) {
3471 ahc
->msgin_index
= 0;
3474 * If this message illicited a response, transition
3475 * to the Message in phase and send it.
3477 if (ahc
->msgout_len
!= 0) {
3478 ahc_outb(ahc
, SCSISIGO
, P_MESGIN
| BSYO
);
3479 ahc_outb(ahc
, SXFRCTL0
,
3480 ahc_inb(ahc
, SXFRCTL0
) | SPIOEN
);
3481 ahc
->msg_type
= MSG_TYPE_TARGET_MSGIN
;
3482 ahc
->msgin_index
= 0;
3490 /* Ask for the next byte. */
3491 ahc_outb(ahc
, SXFRCTL0
,
3492 ahc_inb(ahc
, SXFRCTL0
) | SPIOEN
);
3498 panic("Unknown REQINIT message type");
3502 ahc_clear_msg_state(ahc
);
3503 ahc_outb(ahc
, RETURN_1
, EXIT_MSG_LOOP
);
3505 ahc_outb(ahc
, RETURN_1
, CONT_MSG_LOOP
);
3509 * See if we sent a particular extended message to the target.
3510 * If "full" is true, return true only if the target saw the full
3511 * message. If "full" is false, return true if the target saw at
3512 * least the first byte of the message.
3515 ahc_sent_msg(struct ahc_softc
*ahc
, ahc_msgtype type
, u_int msgval
, int full
)
3523 while (index
< ahc
->msgout_len
) {
3524 if (ahc
->msgout_buf
[index
] == MSG_EXTENDED
) {
3527 end_index
= index
+ 1 + ahc
->msgout_buf
[index
+ 1];
3528 if (ahc
->msgout_buf
[index
+2] == msgval
3529 && type
== AHCMSG_EXT
) {
3532 if (ahc
->msgout_index
> end_index
)
3534 } else if (ahc
->msgout_index
> index
)
3538 } else if (ahc
->msgout_buf
[index
] >= MSG_SIMPLE_TASK
3539 && ahc
->msgout_buf
[index
] <= MSG_IGN_WIDE_RESIDUE
) {
3541 /* Skip tag type and tag id or residue param*/
3544 /* Single byte message */
3545 if (type
== AHCMSG_1B
3546 && ahc
->msgout_buf
[index
] == msgval
3547 && ahc
->msgout_index
> index
)
3559 * Wait for a complete incoming message, parse it, and respond accordingly.
3562 ahc_parse_msg(struct ahc_softc
*ahc
, struct ahc_devinfo
*devinfo
)
3564 struct ahc_initiator_tinfo
*tinfo
;
3565 struct ahc_tmode_tstate
*tstate
;
3569 u_int targ_scsirate
;
3571 done
= MSGLOOP_IN_PROG
;
3574 tinfo
= ahc_fetch_transinfo(ahc
, devinfo
->channel
, devinfo
->our_scsiid
,
3575 devinfo
->target
, &tstate
);
3576 targ_scsirate
= tinfo
->scsirate
;
3579 * Parse as much of the message as is available,
3580 * rejecting it if we don't support it. When
3581 * the entire message is available and has been
3582 * handled, return MSGLOOP_MSGCOMPLETE, indicating
3583 * that we have parsed an entire message.
3585 * In the case of extended messages, we accept the length
3586 * byte outright and perform more checking once we know the
3587 * extended message type.
3589 switch (ahc
->msgin_buf
[0]) {
3590 case MSG_DISCONNECT
:
3591 case MSG_SAVEDATAPOINTER
:
3592 case MSG_CMDCOMPLETE
:
3593 case MSG_RESTOREPOINTERS
:
3594 case MSG_IGN_WIDE_RESIDUE
:
3596 * End our message loop as these are messages
3597 * the sequencer handles on its own.
3599 done
= MSGLOOP_TERMINATED
;
3601 case MSG_MESSAGE_REJECT
:
3602 response
= ahc_handle_msg_reject(ahc
, devinfo
);
3605 done
= MSGLOOP_MSGCOMPLETE
;
3609 /* Wait for enough of the message to begin validation */
3610 if (ahc
->msgin_index
< 2)
3612 switch (ahc
->msgin_buf
[2]) {
3615 const struct ahc_syncrate
*syncrate
;
3621 if (ahc
->msgin_buf
[1] != MSG_EXT_SDTR_LEN
) {
3627 * Wait until we have both args before validating
3628 * and acting on this message.
3630 * Add one to MSG_EXT_SDTR_LEN to account for
3631 * the extended message preamble.
3633 if (ahc
->msgin_index
< (MSG_EXT_SDTR_LEN
+ 1))
3636 period
= ahc
->msgin_buf
[3];
3638 saved_offset
= offset
= ahc
->msgin_buf
[4];
3639 syncrate
= ahc_devlimited_syncrate(ahc
, tinfo
, &period
,
3642 ahc_validate_offset(ahc
, tinfo
, syncrate
, &offset
,
3643 targ_scsirate
& WIDEXFER
,
3646 printk("(%s:%c:%d:%d): Received "
3647 "SDTR period %x, offset %x\n\t"
3648 "Filtered to period %x, offset %x\n",
3649 ahc_name(ahc
), devinfo
->channel
,
3650 devinfo
->target
, devinfo
->lun
,
3651 ahc
->msgin_buf
[3], saved_offset
,
3654 ahc_set_syncrate(ahc
, devinfo
,
3656 offset
, ppr_options
,
3657 AHC_TRANS_ACTIVE
|AHC_TRANS_GOAL
,
3661 * See if we initiated Sync Negotiation
3662 * and didn't have to fall down to async
3665 if (ahc_sent_msg(ahc
, AHCMSG_EXT
, MSG_EXT_SDTR
, TRUE
)) {
3667 if (saved_offset
!= offset
) {
3668 /* Went too low - force async */
3673 * Send our own SDTR in reply
3676 && devinfo
->role
== ROLE_INITIATOR
) {
3677 printk("(%s:%c:%d:%d): Target "
3679 ahc_name(ahc
), devinfo
->channel
,
3680 devinfo
->target
, devinfo
->lun
);
3682 ahc
->msgout_index
= 0;
3683 ahc
->msgout_len
= 0;
3684 ahc_construct_sdtr(ahc
, devinfo
,
3686 ahc
->msgout_index
= 0;
3689 done
= MSGLOOP_MSGCOMPLETE
;
3696 u_int sending_reply
;
3698 sending_reply
= FALSE
;
3699 if (ahc
->msgin_buf
[1] != MSG_EXT_WDTR_LEN
) {
3705 * Wait until we have our arg before validating
3706 * and acting on this message.
3708 * Add one to MSG_EXT_WDTR_LEN to account for
3709 * the extended message preamble.
3711 if (ahc
->msgin_index
< (MSG_EXT_WDTR_LEN
+ 1))
3714 bus_width
= ahc
->msgin_buf
[3];
3715 saved_width
= bus_width
;
3716 ahc_validate_width(ahc
, tinfo
, &bus_width
,
3719 printk("(%s:%c:%d:%d): Received WDTR "
3720 "%x filtered to %x\n",
3721 ahc_name(ahc
), devinfo
->channel
,
3722 devinfo
->target
, devinfo
->lun
,
3723 saved_width
, bus_width
);
3726 if (ahc_sent_msg(ahc
, AHCMSG_EXT
, MSG_EXT_WDTR
, TRUE
)) {
3728 * Don't send a WDTR back to the
3729 * target, since we asked first.
3730 * If the width went higher than our
3731 * request, reject it.
3733 if (saved_width
> bus_width
) {
3735 printk("(%s:%c:%d:%d): requested %dBit "
3736 "transfers. Rejecting...\n",
3737 ahc_name(ahc
), devinfo
->channel
,
3738 devinfo
->target
, devinfo
->lun
,
3739 8 * (0x01 << bus_width
));
3744 * Send our own WDTR in reply
3747 && devinfo
->role
== ROLE_INITIATOR
) {
3748 printk("(%s:%c:%d:%d): Target "
3750 ahc_name(ahc
), devinfo
->channel
,
3751 devinfo
->target
, devinfo
->lun
);
3753 ahc
->msgout_index
= 0;
3754 ahc
->msgout_len
= 0;
3755 ahc_construct_wdtr(ahc
, devinfo
, bus_width
);
3756 ahc
->msgout_index
= 0;
3758 sending_reply
= TRUE
;
3761 * After a wide message, we are async, but
3762 * some devices don't seem to honor this portion
3763 * of the spec. Force a renegotiation of the
3764 * sync component of our transfer agreement even
3765 * if our goal is async. By updating our width
3766 * after forcing the negotiation, we avoid
3767 * renegotiating for width.
3769 ahc_update_neg_request(ahc
, devinfo
, tstate
,
3770 tinfo
, AHC_NEG_ALWAYS
);
3771 ahc_set_width(ahc
, devinfo
, bus_width
,
3772 AHC_TRANS_ACTIVE
|AHC_TRANS_GOAL
,
3774 if (sending_reply
== FALSE
&& reject
== FALSE
) {
3777 * We will always have an SDTR to send.
3779 ahc
->msgout_index
= 0;
3780 ahc
->msgout_len
= 0;
3781 ahc_build_transfer_msg(ahc
, devinfo
);
3782 ahc
->msgout_index
= 0;
3785 done
= MSGLOOP_MSGCOMPLETE
;
3790 const struct ahc_syncrate
*syncrate
;
3797 u_int saved_ppr_options
;
3799 if (ahc
->msgin_buf
[1] != MSG_EXT_PPR_LEN
) {
3805 * Wait until we have all args before validating
3806 * and acting on this message.
3808 * Add one to MSG_EXT_PPR_LEN to account for
3809 * the extended message preamble.
3811 if (ahc
->msgin_index
< (MSG_EXT_PPR_LEN
+ 1))
3814 period
= ahc
->msgin_buf
[3];
3815 offset
= ahc
->msgin_buf
[5];
3816 bus_width
= ahc
->msgin_buf
[6];
3817 saved_width
= bus_width
;
3818 ppr_options
= ahc
->msgin_buf
[7];
3820 * According to the spec, a DT only
3821 * period factor with no DT option
3822 * set implies async.
3824 if ((ppr_options
& MSG_EXT_PPR_DT_REQ
) == 0
3827 saved_ppr_options
= ppr_options
;
3828 saved_offset
= offset
;
3831 * Mask out any options we don't support
3832 * on any controller. Transfer options are
3833 * only available if we are negotiating wide.
3835 ppr_options
&= MSG_EXT_PPR_DT_REQ
;
3839 ahc_validate_width(ahc
, tinfo
, &bus_width
,
3841 syncrate
= ahc_devlimited_syncrate(ahc
, tinfo
, &period
,
3844 ahc_validate_offset(ahc
, tinfo
, syncrate
,
3848 if (ahc_sent_msg(ahc
, AHCMSG_EXT
, MSG_EXT_PPR
, TRUE
)) {
3850 * If we are unable to do any of the
3851 * requested options (we went too low),
3852 * then we'll have to reject the message.
3854 if (saved_width
> bus_width
3855 || saved_offset
!= offset
3856 || saved_ppr_options
!= ppr_options
) {
3865 if (devinfo
->role
!= ROLE_TARGET
)
3866 printk("(%s:%c:%d:%d): Target "
3868 ahc_name(ahc
), devinfo
->channel
,
3869 devinfo
->target
, devinfo
->lun
);
3871 printk("(%s:%c:%d:%d): Initiator "
3873 ahc_name(ahc
), devinfo
->channel
,
3874 devinfo
->target
, devinfo
->lun
);
3875 ahc
->msgout_index
= 0;
3876 ahc
->msgout_len
= 0;
3877 ahc_construct_ppr(ahc
, devinfo
, period
, offset
,
3878 bus_width
, ppr_options
);
3879 ahc
->msgout_index
= 0;
3883 printk("(%s:%c:%d:%d): Received PPR width %x, "
3884 "period %x, offset %x,options %x\n"
3885 "\tFiltered to width %x, period %x, "
3886 "offset %x, options %x\n",
3887 ahc_name(ahc
), devinfo
->channel
,
3888 devinfo
->target
, devinfo
->lun
,
3889 saved_width
, ahc
->msgin_buf
[3],
3890 saved_offset
, saved_ppr_options
,
3891 bus_width
, period
, offset
, ppr_options
);
3893 ahc_set_width(ahc
, devinfo
, bus_width
,
3894 AHC_TRANS_ACTIVE
|AHC_TRANS_GOAL
,
3896 ahc_set_syncrate(ahc
, devinfo
,
3898 offset
, ppr_options
,
3899 AHC_TRANS_ACTIVE
|AHC_TRANS_GOAL
,
3901 done
= MSGLOOP_MSGCOMPLETE
;
3905 /* Unknown extended message. Reject it. */
3911 #ifdef AHC_TARGET_MODE
3912 case MSG_BUS_DEV_RESET
:
3913 ahc_handle_devreset(ahc
, devinfo
,
3915 "Bus Device Reset Received",
3916 /*verbose_level*/0);
3918 done
= MSGLOOP_TERMINATED
;
3922 case MSG_CLEAR_QUEUE
:
3926 /* Target mode messages */
3927 if (devinfo
->role
!= ROLE_TARGET
) {
3931 tag
= SCB_LIST_NULL
;
3932 if (ahc
->msgin_buf
[0] == MSG_ABORT_TAG
)
3933 tag
= ahc_inb(ahc
, INITIATOR_TAG
);
3934 ahc_abort_scbs(ahc
, devinfo
->target
, devinfo
->channel
,
3935 devinfo
->lun
, tag
, ROLE_TARGET
,
3938 tstate
= ahc
->enabled_targets
[devinfo
->our_scsiid
];
3939 if (tstate
!= NULL
) {
3940 struct ahc_tmode_lstate
* lstate
;
3942 lstate
= tstate
->enabled_luns
[devinfo
->lun
];
3943 if (lstate
!= NULL
) {
3944 ahc_queue_lstate_event(ahc
, lstate
,
3945 devinfo
->our_scsiid
,
3948 ahc_send_lstate_events(ahc
, lstate
);
3952 done
= MSGLOOP_TERMINATED
;
3956 case MSG_TERM_IO_PROC
:
3964 * Setup to reject the message.
3966 ahc
->msgout_index
= 0;
3967 ahc
->msgout_len
= 1;
3968 ahc
->msgout_buf
[0] = MSG_MESSAGE_REJECT
;
3969 done
= MSGLOOP_MSGCOMPLETE
;
3973 if (done
!= MSGLOOP_IN_PROG
&& !response
)
3974 /* Clear the outgoing message buffer */
3975 ahc
->msgout_len
= 0;
3981 * Process a message reject message.
3984 ahc_handle_msg_reject(struct ahc_softc
*ahc
, struct ahc_devinfo
*devinfo
)
3987 * What we care about here is if we had an
3988 * outstanding SDTR or WDTR message for this
3989 * target. If we did, this is a signal that
3990 * the target is refusing negotiation.
3993 struct ahc_initiator_tinfo
*tinfo
;
3994 struct ahc_tmode_tstate
*tstate
;
3999 scb_index
= ahc_inb(ahc
, SCB_TAG
);
4000 scb
= ahc_lookup_scb(ahc
, scb_index
);
4001 tinfo
= ahc_fetch_transinfo(ahc
, devinfo
->channel
,
4002 devinfo
->our_scsiid
,
4003 devinfo
->target
, &tstate
);
4004 /* Might be necessary */
4005 last_msg
= ahc_inb(ahc
, LAST_MSG
);
4007 if (ahc_sent_msg(ahc
, AHCMSG_EXT
, MSG_EXT_PPR
, /*full*/FALSE
)) {
4009 * Target does not support the PPR message.
4010 * Attempt to negotiate SPI-2 style.
4013 printk("(%s:%c:%d:%d): PPR Rejected. "
4014 "Trying WDTR/SDTR\n",
4015 ahc_name(ahc
), devinfo
->channel
,
4016 devinfo
->target
, devinfo
->lun
);
4018 tinfo
->goal
.ppr_options
= 0;
4019 tinfo
->curr
.transport_version
= 2;
4020 tinfo
->goal
.transport_version
= 2;
4021 ahc
->msgout_index
= 0;
4022 ahc
->msgout_len
= 0;
4023 ahc_build_transfer_msg(ahc
, devinfo
);
4024 ahc
->msgout_index
= 0;
4026 } else if (ahc_sent_msg(ahc
, AHCMSG_EXT
, MSG_EXT_WDTR
, /*full*/FALSE
)) {
4028 /* note 8bit xfers */
4029 printk("(%s:%c:%d:%d): refuses WIDE negotiation. Using "
4030 "8bit transfers\n", ahc_name(ahc
),
4031 devinfo
->channel
, devinfo
->target
, devinfo
->lun
);
4032 ahc_set_width(ahc
, devinfo
, MSG_EXT_WDTR_BUS_8_BIT
,
4033 AHC_TRANS_ACTIVE
|AHC_TRANS_GOAL
,
4036 * No need to clear the sync rate. If the target
4037 * did not accept the command, our syncrate is
4038 * unaffected. If the target started the negotiation,
4039 * but rejected our response, we already cleared the
4040 * sync rate before sending our WDTR.
4042 if (tinfo
->goal
.offset
!= tinfo
->curr
.offset
) {
4044 /* Start the sync negotiation */
4045 ahc
->msgout_index
= 0;
4046 ahc
->msgout_len
= 0;
4047 ahc_build_transfer_msg(ahc
, devinfo
);
4048 ahc
->msgout_index
= 0;
4051 } else if (ahc_sent_msg(ahc
, AHCMSG_EXT
, MSG_EXT_SDTR
, /*full*/FALSE
)) {
4052 /* note asynch xfers and clear flag */
4053 ahc_set_syncrate(ahc
, devinfo
, /*syncrate*/NULL
, /*period*/0,
4054 /*offset*/0, /*ppr_options*/0,
4055 AHC_TRANS_ACTIVE
|AHC_TRANS_GOAL
,
4057 printk("(%s:%c:%d:%d): refuses synchronous negotiation. "
4058 "Using asynchronous transfers\n",
4059 ahc_name(ahc
), devinfo
->channel
,
4060 devinfo
->target
, devinfo
->lun
);
4061 } else if ((scb
->hscb
->control
& MSG_SIMPLE_TASK
) != 0) {
4065 tag_type
= (scb
->hscb
->control
& MSG_SIMPLE_TASK
);
4067 if (tag_type
== MSG_SIMPLE_TASK
) {
4068 printk("(%s:%c:%d:%d): refuses tagged commands. "
4069 "Performing non-tagged I/O\n", ahc_name(ahc
),
4070 devinfo
->channel
, devinfo
->target
, devinfo
->lun
);
4071 ahc_set_tags(ahc
, scb
->io_ctx
, devinfo
, AHC_QUEUE_NONE
);
4074 printk("(%s:%c:%d:%d): refuses %s tagged commands. "
4075 "Performing simple queue tagged I/O only\n",
4076 ahc_name(ahc
), devinfo
->channel
, devinfo
->target
,
4077 devinfo
->lun
, tag_type
== MSG_ORDERED_TASK
4078 ? "ordered" : "head of queue");
4079 ahc_set_tags(ahc
, scb
->io_ctx
, devinfo
, AHC_QUEUE_BASIC
);
4084 * Resend the identify for this CCB as the target
4085 * may believe that the selection is invalid otherwise.
4087 ahc_outb(ahc
, SCB_CONTROL
,
4088 ahc_inb(ahc
, SCB_CONTROL
) & mask
);
4089 scb
->hscb
->control
&= mask
;
4090 ahc_set_transaction_tag(scb
, /*enabled*/FALSE
,
4091 /*type*/MSG_SIMPLE_TASK
);
4092 ahc_outb(ahc
, MSG_OUT
, MSG_IDENTIFYFLAG
);
4093 ahc_assert_atn(ahc
);
4096 * This transaction is now at the head of
4097 * the untagged queue for this target.
4099 if ((ahc
->flags
& AHC_SCB_BTT
) == 0) {
4100 struct scb_tailq
*untagged_q
;
4103 &(ahc
->untagged_queues
[devinfo
->target_offset
]);
4104 TAILQ_INSERT_HEAD(untagged_q
, scb
, links
.tqe
);
4105 scb
->flags
|= SCB_UNTAGGEDQ
;
4107 ahc_busy_tcl(ahc
, BUILD_TCL(scb
->hscb
->scsiid
, devinfo
->lun
),
4111 * Requeue all tagged commands for this target
4112 * currently in our possession so they can be
4113 * converted to untagged commands.
4115 ahc_search_qinfifo(ahc
, SCB_GET_TARGET(ahc
, scb
),
4116 SCB_GET_CHANNEL(ahc
, scb
),
4117 SCB_GET_LUN(scb
), /*tag*/SCB_LIST_NULL
,
4118 ROLE_INITIATOR
, CAM_REQUEUE_REQ
,
4122 * Otherwise, we ignore it.
4124 printk("%s:%c:%d: Message reject for %x -- ignored\n",
4125 ahc_name(ahc
), devinfo
->channel
, devinfo
->target
,
4132 * Process an ingnore wide residue message.
4135 ahc_handle_ign_wide_residue(struct ahc_softc
*ahc
, struct ahc_devinfo
*devinfo
)
4140 scb_index
= ahc_inb(ahc
, SCB_TAG
);
4141 scb
= ahc_lookup_scb(ahc
, scb_index
);
4143 * XXX Actually check data direction in the sequencer?
4144 * Perhaps add datadir to some spare bits in the hscb?
4146 if ((ahc_inb(ahc
, SEQ_FLAGS
) & DPHASE
) == 0
4147 || ahc_get_transfer_dir(scb
) != CAM_DIR_IN
) {
4149 * Ignore the message if we haven't
4150 * seen an appropriate data phase yet.
4154 * If the residual occurred on the last
4155 * transfer and the transfer request was
4156 * expected to end on an odd count, do
4157 * nothing. Otherwise, subtract a byte
4158 * and update the residual count accordingly.
4162 sgptr
= ahc_inb(ahc
, SCB_RESIDUAL_SGPTR
);
4163 if ((sgptr
& SG_LIST_NULL
) != 0
4164 && (ahc_inb(ahc
, SCB_LUN
) & SCB_XFERLEN_ODD
) != 0) {
4166 * If the residual occurred on the last
4167 * transfer and the transfer request was
4168 * expected to end on an odd count, do
4172 struct ahc_dma_seg
*sg
;
4177 /* Pull in all of the sgptr */
4178 sgptr
= ahc_inl(ahc
, SCB_RESIDUAL_SGPTR
);
4179 data_cnt
= ahc_inl(ahc
, SCB_RESIDUAL_DATACNT
);
4181 if ((sgptr
& SG_LIST_NULL
) != 0) {
4183 * The residual data count is not updated
4184 * for the command run to completion case.
4185 * Explicitly zero the count.
4187 data_cnt
&= ~AHC_SG_LEN_MASK
;
4190 data_addr
= ahc_inl(ahc
, SHADDR
);
4194 sgptr
&= SG_PTR_MASK
;
4196 sg
= ahc_sg_bus_to_virt(scb
, sgptr
);
4199 * The residual sg ptr points to the next S/G
4200 * to load so we must go back one.
4203 sglen
= ahc_le32toh(sg
->len
) & AHC_SG_LEN_MASK
;
4204 if (sg
!= scb
->sg_list
4205 && sglen
< (data_cnt
& AHC_SG_LEN_MASK
)) {
4208 sglen
= ahc_le32toh(sg
->len
);
4210 * Preserve High Address and SG_LIST bits
4211 * while setting the count to 1.
4213 data_cnt
= 1 | (sglen
& (~AHC_SG_LEN_MASK
));
4214 data_addr
= ahc_le32toh(sg
->addr
)
4215 + (sglen
& AHC_SG_LEN_MASK
) - 1;
4218 * Increment sg so it points to the
4222 sgptr
= ahc_sg_virt_to_bus(scb
, sg
);
4224 ahc_outl(ahc
, SCB_RESIDUAL_SGPTR
, sgptr
);
4225 ahc_outl(ahc
, SCB_RESIDUAL_DATACNT
, data_cnt
);
4227 * Toggle the "oddness" of the transfer length
4228 * to handle this mid-transfer ignore wide
4229 * residue. This ensures that the oddness is
4230 * correct for subsequent data transfers.
4232 ahc_outb(ahc
, SCB_LUN
,
4233 ahc_inb(ahc
, SCB_LUN
) ^ SCB_XFERLEN_ODD
);
4240 * Reinitialize the data pointers for the active transfer
4241 * based on its current residual.
4244 ahc_reinitialize_dataptrs(struct ahc_softc
*ahc
)
4247 struct ahc_dma_seg
*sg
;
4253 scb_index
= ahc_inb(ahc
, SCB_TAG
);
4254 scb
= ahc_lookup_scb(ahc
, scb_index
);
4255 sgptr
= (ahc_inb(ahc
, SCB_RESIDUAL_SGPTR
+ 3) << 24)
4256 | (ahc_inb(ahc
, SCB_RESIDUAL_SGPTR
+ 2) << 16)
4257 | (ahc_inb(ahc
, SCB_RESIDUAL_SGPTR
+ 1) << 8)
4258 | ahc_inb(ahc
, SCB_RESIDUAL_SGPTR
);
4260 sgptr
&= SG_PTR_MASK
;
4261 sg
= ahc_sg_bus_to_virt(scb
, sgptr
);
4263 /* The residual sg_ptr always points to the next sg */
4266 resid
= (ahc_inb(ahc
, SCB_RESIDUAL_DATACNT
+ 2) << 16)
4267 | (ahc_inb(ahc
, SCB_RESIDUAL_DATACNT
+ 1) << 8)
4268 | ahc_inb(ahc
, SCB_RESIDUAL_DATACNT
);
4270 dataptr
= ahc_le32toh(sg
->addr
)
4271 + (ahc_le32toh(sg
->len
) & AHC_SG_LEN_MASK
)
4273 if ((ahc
->flags
& AHC_39BIT_ADDRESSING
) != 0) {
4276 dscommand1
= ahc_inb(ahc
, DSCOMMAND1
);
4277 ahc_outb(ahc
, DSCOMMAND1
, dscommand1
| HADDLDSEL0
);
4278 ahc_outb(ahc
, HADDR
,
4279 (ahc_le32toh(sg
->len
) >> 24) & SG_HIGH_ADDR_BITS
);
4280 ahc_outb(ahc
, DSCOMMAND1
, dscommand1
);
4282 ahc_outb(ahc
, HADDR
+ 3, dataptr
>> 24);
4283 ahc_outb(ahc
, HADDR
+ 2, dataptr
>> 16);
4284 ahc_outb(ahc
, HADDR
+ 1, dataptr
>> 8);
4285 ahc_outb(ahc
, HADDR
, dataptr
);
4286 ahc_outb(ahc
, HCNT
+ 2, resid
>> 16);
4287 ahc_outb(ahc
, HCNT
+ 1, resid
>> 8);
4288 ahc_outb(ahc
, HCNT
, resid
);
4289 if ((ahc
->features
& AHC_ULTRA2
) == 0) {
4290 ahc_outb(ahc
, STCNT
+ 2, resid
>> 16);
4291 ahc_outb(ahc
, STCNT
+ 1, resid
>> 8);
4292 ahc_outb(ahc
, STCNT
, resid
);
4297 * Handle the effects of issuing a bus device reset message.
4300 ahc_handle_devreset(struct ahc_softc
*ahc
, struct ahc_devinfo
*devinfo
,
4301 cam_status status
, char *message
, int verbose_level
)
4303 #ifdef AHC_TARGET_MODE
4304 struct ahc_tmode_tstate
* tstate
;
4309 found
= ahc_abort_scbs(ahc
, devinfo
->target
, devinfo
->channel
,
4310 CAM_LUN_WILDCARD
, SCB_LIST_NULL
, devinfo
->role
,
4313 #ifdef AHC_TARGET_MODE
4315 * Send an immediate notify ccb to all target mord peripheral
4316 * drivers affected by this action.
4318 tstate
= ahc
->enabled_targets
[devinfo
->our_scsiid
];
4319 if (tstate
!= NULL
) {
4320 for (lun
= 0; lun
< AHC_NUM_LUNS
; lun
++) {
4321 struct ahc_tmode_lstate
* lstate
;
4323 lstate
= tstate
->enabled_luns
[lun
];
4327 ahc_queue_lstate_event(ahc
, lstate
, devinfo
->our_scsiid
,
4328 MSG_BUS_DEV_RESET
, /*arg*/0);
4329 ahc_send_lstate_events(ahc
, lstate
);
4335 * Go back to async/narrow transfers and renegotiate.
4337 ahc_set_width(ahc
, devinfo
, MSG_EXT_WDTR_BUS_8_BIT
,
4338 AHC_TRANS_CUR
, /*paused*/TRUE
);
4339 ahc_set_syncrate(ahc
, devinfo
, /*syncrate*/NULL
,
4340 /*period*/0, /*offset*/0, /*ppr_options*/0,
4341 AHC_TRANS_CUR
, /*paused*/TRUE
);
4343 if (status
!= CAM_SEL_TIMEOUT
)
4344 ahc_send_async(ahc
, devinfo
->channel
, devinfo
->target
,
4345 CAM_LUN_WILDCARD
, AC_SENT_BDR
);
4348 && (verbose_level
<= bootverbose
))
4349 printk("%s: %s on %c:%d. %d SCBs aborted\n", ahc_name(ahc
),
4350 message
, devinfo
->channel
, devinfo
->target
, found
);
4353 #ifdef AHC_TARGET_MODE
4355 ahc_setup_target_msgin(struct ahc_softc
*ahc
, struct ahc_devinfo
*devinfo
,
4360 * To facilitate adding multiple messages together,
4361 * each routine should increment the index and len
4362 * variables instead of setting them explicitly.
4364 ahc
->msgout_index
= 0;
4365 ahc
->msgout_len
= 0;
4367 if (scb
!= NULL
&& (scb
->flags
& SCB_AUTO_NEGOTIATE
) != 0)
4368 ahc_build_transfer_msg(ahc
, devinfo
);
4370 panic("ahc_intr: AWAITING target message with no message");
4372 ahc
->msgout_index
= 0;
4373 ahc
->msg_type
= MSG_TYPE_TARGET_MSGIN
;
4376 /**************************** Initialization **********************************/
4378 * Allocate a controller structure for a new device
4379 * and perform initial initializion.
4382 ahc_alloc(void *platform_arg
, char *name
)
4384 struct ahc_softc
*ahc
;
4387 ahc
= kmalloc(sizeof(*ahc
), GFP_ATOMIC
);
4389 printk("aic7xxx: cannot malloc softc!\n");
4393 memset(ahc
, 0, sizeof(*ahc
));
4394 ahc
->seep_config
= kmalloc(sizeof(*ahc
->seep_config
), GFP_ATOMIC
);
4395 if (ahc
->seep_config
== NULL
) {
4400 LIST_INIT(&ahc
->pending_scbs
);
4401 /* We don't know our unit number until the OSM sets it */
4404 ahc
->description
= NULL
;
4406 ahc
->channel_b
= 'B';
4407 ahc
->chip
= AHC_NONE
;
4408 ahc
->features
= AHC_FENONE
;
4409 ahc
->bugs
= AHC_BUGNONE
;
4410 ahc
->flags
= AHC_FNONE
;
4412 * Default to all error reporting enabled with the
4413 * sequencer operating at its fastest speed.
4414 * The bus attach code may modify this.
4416 ahc
->seqctl
= FASTMODE
;
4418 for (i
= 0; i
< AHC_NUM_TARGETS
; i
++)
4419 TAILQ_INIT(&ahc
->untagged_queues
[i
]);
4420 if (ahc_platform_alloc(ahc
, platform_arg
) != 0) {
4428 ahc_softc_init(struct ahc_softc
*ahc
)
4431 /* The IRQMS bit is only valid on VL and EISA chips */
4432 if ((ahc
->chip
& AHC_PCI
) == 0)
4433 ahc
->unpause
= ahc_inb(ahc
, HCNTRL
) & IRQMS
;
4436 ahc
->pause
= ahc
->unpause
| PAUSE
;
4437 /* XXX The shared scb data stuff should be deprecated */
4438 if (ahc
->scb_data
== NULL
) {
4439 ahc
->scb_data
= kzalloc(sizeof(*ahc
->scb_data
), GFP_ATOMIC
);
4440 if (ahc
->scb_data
== NULL
)
4448 ahc_set_unit(struct ahc_softc
*ahc
, int unit
)
4454 ahc_set_name(struct ahc_softc
*ahc
, char *name
)
4456 if (ahc
->name
!= NULL
)
4462 ahc_free(struct ahc_softc
*ahc
)
4466 switch (ahc
->init_level
) {
4472 ahc_dmamap_unload(ahc
, ahc
->shared_data_dmat
,
4473 ahc
->shared_data_dmamap
);
4476 ahc_dmamem_free(ahc
, ahc
->shared_data_dmat
, ahc
->qoutfifo
,
4477 ahc
->shared_data_dmamap
);
4478 ahc_dmamap_destroy(ahc
, ahc
->shared_data_dmat
,
4479 ahc
->shared_data_dmamap
);
4482 ahc_dma_tag_destroy(ahc
, ahc
->shared_data_dmat
);
4489 ahc_platform_free(ahc
);
4490 ahc_fini_scbdata(ahc
);
4491 for (i
= 0; i
< AHC_NUM_TARGETS
; i
++) {
4492 struct ahc_tmode_tstate
*tstate
;
4494 tstate
= ahc
->enabled_targets
[i
];
4495 if (tstate
!= NULL
) {
4496 #ifdef AHC_TARGET_MODE
4499 for (j
= 0; j
< AHC_NUM_LUNS
; j
++) {
4500 struct ahc_tmode_lstate
*lstate
;
4502 lstate
= tstate
->enabled_luns
[j
];
4503 if (lstate
!= NULL
) {
4504 xpt_free_path(lstate
->path
);
4512 #ifdef AHC_TARGET_MODE
4513 if (ahc
->black_hole
!= NULL
) {
4514 xpt_free_path(ahc
->black_hole
->path
);
4515 kfree(ahc
->black_hole
);
4518 if (ahc
->name
!= NULL
)
4520 if (ahc
->seep_config
!= NULL
)
4521 kfree(ahc
->seep_config
);
4527 ahc_shutdown(void *arg
)
4529 struct ahc_softc
*ahc
;
4532 ahc
= (struct ahc_softc
*)arg
;
4534 /* This will reset most registers to 0, but not all */
4535 ahc_reset(ahc
, /*reinit*/FALSE
);
4536 ahc_outb(ahc
, SCSISEQ
, 0);
4537 ahc_outb(ahc
, SXFRCTL0
, 0);
4538 ahc_outb(ahc
, DSPCISTATUS
, 0);
4540 for (i
= TARG_SCSIRATE
; i
< SCSICONF
; i
++)
4541 ahc_outb(ahc
, i
, 0);
4545 * Reset the controller and record some information about it
4546 * that is only available just after a reset. If "reinit" is
4547 * non-zero, this reset occurred after initial configuration
4548 * and the caller requests that the chip be fully reinitialized
4549 * to a runable state. Chip interrupts are *not* enabled after
4550 * a reinitialization. The caller must enable interrupts via
4551 * ahc_intr_enable().
4554 ahc_reset(struct ahc_softc
*ahc
, int reinit
)
4557 u_int sxfrctl1_a
, sxfrctl1_b
;
4562 * Preserve the value of the SXFRCTL1 register for all channels.
4563 * It contains settings that affect termination and we don't want
4564 * to disturb the integrity of the bus.
4568 if ((ahc
->chip
& AHC_CHIPID_MASK
) == AHC_AIC7770
) {
4572 * Save channel B's settings in case this chip
4573 * is setup for TWIN channel operation.
4575 sblkctl
= ahc_inb(ahc
, SBLKCTL
);
4576 ahc_outb(ahc
, SBLKCTL
, sblkctl
| SELBUSB
);
4577 sxfrctl1_b
= ahc_inb(ahc
, SXFRCTL1
);
4578 ahc_outb(ahc
, SBLKCTL
, sblkctl
& ~SELBUSB
);
4580 sxfrctl1_a
= ahc_inb(ahc
, SXFRCTL1
);
4582 ahc_outb(ahc
, HCNTRL
, CHIPRST
| ahc
->pause
);
4585 * Ensure that the reset has finished. We delay 1000us
4586 * prior to reading the register to make sure the chip
4587 * has sufficiently completed its reset to handle register
4593 } while (--wait
&& !(ahc_inb(ahc
, HCNTRL
) & CHIPRSTACK
));
4596 printk("%s: WARNING - Failed chip reset! "
4597 "Trying to initialize anyway.\n", ahc_name(ahc
));
4599 ahc_outb(ahc
, HCNTRL
, ahc
->pause
);
4601 /* Determine channel configuration */
4602 sblkctl
= ahc_inb(ahc
, SBLKCTL
) & (SELBUSB
|SELWIDE
);
4603 /* No Twin Channel PCI cards */
4604 if ((ahc
->chip
& AHC_PCI
) != 0)
4605 sblkctl
&= ~SELBUSB
;
4608 /* Single Narrow Channel */
4612 ahc
->features
|= AHC_WIDE
;
4616 ahc
->features
|= AHC_TWIN
;
4619 printk(" Unsupported adapter type. Ignoring\n");
4626 * We must always initialize STPWEN to 1 before we
4627 * restore the saved values. STPWEN is initialized
4628 * to a tri-state condition which can only be cleared
4631 if ((ahc
->features
& AHC_TWIN
) != 0) {
4634 sblkctl
= ahc_inb(ahc
, SBLKCTL
);
4635 ahc_outb(ahc
, SBLKCTL
, sblkctl
| SELBUSB
);
4636 ahc_outb(ahc
, SXFRCTL1
, sxfrctl1_b
);
4637 ahc_outb(ahc
, SBLKCTL
, sblkctl
& ~SELBUSB
);
4639 ahc_outb(ahc
, SXFRCTL1
, sxfrctl1_a
);
4644 * If a recovery action has forced a chip reset,
4645 * re-initialize the chip to our liking.
4647 error
= ahc
->bus_chip_init(ahc
);
4657 * Determine the number of SCBs available on the controller
4660 ahc_probe_scbs(struct ahc_softc
*ahc
) {
4663 for (i
= 0; i
< AHC_SCB_MAX
; i
++) {
4665 ahc_outb(ahc
, SCBPTR
, i
);
4666 ahc_outb(ahc
, SCB_BASE
, i
);
4667 if (ahc_inb(ahc
, SCB_BASE
) != i
)
4669 ahc_outb(ahc
, SCBPTR
, 0);
4670 if (ahc_inb(ahc
, SCB_BASE
) != 0)
4677 ahc_dmamap_cb(void *arg
, bus_dma_segment_t
*segs
, int nseg
, int error
)
4681 baddr
= (dma_addr_t
*)arg
;
4682 *baddr
= segs
->ds_addr
;
4686 ahc_build_free_scb_list(struct ahc_softc
*ahc
)
4692 if ((ahc
->flags
& AHC_LSCBS_ENABLED
) != 0)
4695 for (i
= 0; i
< ahc
->scb_data
->maxhscbs
; i
++) {
4698 ahc_outb(ahc
, SCBPTR
, i
);
4701 * Touch all SCB bytes to avoid parity errors
4702 * should one of our debugging routines read
4703 * an otherwise uninitiatlized byte.
4705 for (j
= 0; j
< scbsize
; j
++)
4706 ahc_outb(ahc
, SCB_BASE
+j
, 0xFF);
4708 /* Clear the control byte. */
4709 ahc_outb(ahc
, SCB_CONTROL
, 0);
4711 /* Set the next pointer */
4712 if ((ahc
->flags
& AHC_PAGESCBS
) != 0)
4713 ahc_outb(ahc
, SCB_NEXT
, i
+1);
4715 ahc_outb(ahc
, SCB_NEXT
, SCB_LIST_NULL
);
4717 /* Make the tag number, SCSIID, and lun invalid */
4718 ahc_outb(ahc
, SCB_TAG
, SCB_LIST_NULL
);
4719 ahc_outb(ahc
, SCB_SCSIID
, 0xFF);
4720 ahc_outb(ahc
, SCB_LUN
, 0xFF);
4723 if ((ahc
->flags
& AHC_PAGESCBS
) != 0) {
4724 /* SCB 0 heads the free list. */
4725 ahc_outb(ahc
, FREE_SCBH
, 0);
4728 ahc_outb(ahc
, FREE_SCBH
, SCB_LIST_NULL
);
4731 /* Make sure that the last SCB terminates the free list */
4732 ahc_outb(ahc
, SCBPTR
, i
-1);
4733 ahc_outb(ahc
, SCB_NEXT
, SCB_LIST_NULL
);
4737 ahc_init_scbdata(struct ahc_softc
*ahc
)
4739 struct scb_data
*scb_data
;
4741 scb_data
= ahc
->scb_data
;
4742 SLIST_INIT(&scb_data
->free_scbs
);
4743 SLIST_INIT(&scb_data
->sg_maps
);
4745 /* Allocate SCB resources */
4746 scb_data
->scbarray
= kcalloc(AHC_SCB_MAX_ALLOC
, sizeof(struct scb
),
4748 if (scb_data
->scbarray
== NULL
)
4751 /* Determine the number of hardware SCBs and initialize them */
4753 scb_data
->maxhscbs
= ahc_probe_scbs(ahc
);
4754 if (ahc
->scb_data
->maxhscbs
== 0) {
4755 printk("%s: No SCB space found\n", ahc_name(ahc
));
4760 * Create our DMA tags. These tags define the kinds of device
4761 * accessible memory allocations and memory mappings we will
4762 * need to perform during normal operation.
4764 * Unless we need to further restrict the allocation, we rely
4765 * on the restrictions of the parent dmat, hence the common
4766 * use of MAXADDR and MAXSIZE.
4769 /* DMA tag for our hardware scb structures */
4770 if (ahc_dma_tag_create(ahc
, ahc
->parent_dmat
, /*alignment*/1,
4771 /*boundary*/BUS_SPACE_MAXADDR_32BIT
+ 1,
4772 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT
,
4773 /*highaddr*/BUS_SPACE_MAXADDR
,
4774 /*filter*/NULL
, /*filterarg*/NULL
,
4775 AHC_SCB_MAX_ALLOC
* sizeof(struct hardware_scb
),
4777 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT
,
4778 /*flags*/0, &scb_data
->hscb_dmat
) != 0) {
4782 scb_data
->init_level
++;
4784 /* Allocation for our hscbs */
4785 if (ahc_dmamem_alloc(ahc
, scb_data
->hscb_dmat
,
4786 (void **)&scb_data
->hscbs
,
4787 BUS_DMA_NOWAIT
, &scb_data
->hscb_dmamap
) != 0) {
4791 scb_data
->init_level
++;
4793 /* And permanently map them */
4794 ahc_dmamap_load(ahc
, scb_data
->hscb_dmat
, scb_data
->hscb_dmamap
,
4796 AHC_SCB_MAX_ALLOC
* sizeof(struct hardware_scb
),
4797 ahc_dmamap_cb
, &scb_data
->hscb_busaddr
, /*flags*/0);
4799 scb_data
->init_level
++;
4801 /* DMA tag for our sense buffers */
4802 if (ahc_dma_tag_create(ahc
, ahc
->parent_dmat
, /*alignment*/1,
4803 /*boundary*/BUS_SPACE_MAXADDR_32BIT
+ 1,
4804 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT
,
4805 /*highaddr*/BUS_SPACE_MAXADDR
,
4806 /*filter*/NULL
, /*filterarg*/NULL
,
4807 AHC_SCB_MAX_ALLOC
* sizeof(struct scsi_sense_data
),
4809 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT
,
4810 /*flags*/0, &scb_data
->sense_dmat
) != 0) {
4814 scb_data
->init_level
++;
4817 if (ahc_dmamem_alloc(ahc
, scb_data
->sense_dmat
,
4818 (void **)&scb_data
->sense
,
4819 BUS_DMA_NOWAIT
, &scb_data
->sense_dmamap
) != 0) {
4823 scb_data
->init_level
++;
4825 /* And permanently map them */
4826 ahc_dmamap_load(ahc
, scb_data
->sense_dmat
, scb_data
->sense_dmamap
,
4828 AHC_SCB_MAX_ALLOC
* sizeof(struct scsi_sense_data
),
4829 ahc_dmamap_cb
, &scb_data
->sense_busaddr
, /*flags*/0);
4831 scb_data
->init_level
++;
4833 /* DMA tag for our S/G structures. We allocate in page sized chunks */
4834 if (ahc_dma_tag_create(ahc
, ahc
->parent_dmat
, /*alignment*/8,
4835 /*boundary*/BUS_SPACE_MAXADDR_32BIT
+ 1,
4836 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT
,
4837 /*highaddr*/BUS_SPACE_MAXADDR
,
4838 /*filter*/NULL
, /*filterarg*/NULL
,
4839 PAGE_SIZE
, /*nsegments*/1,
4840 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT
,
4841 /*flags*/0, &scb_data
->sg_dmat
) != 0) {
4845 scb_data
->init_level
++;
4847 /* Perform initial CCB allocation */
4848 memset(scb_data
->hscbs
, 0,
4849 AHC_SCB_MAX_ALLOC
* sizeof(struct hardware_scb
));
4850 ahc_alloc_scbs(ahc
);
4852 if (scb_data
->numscbs
== 0) {
4853 printk("%s: ahc_init_scbdata - "
4854 "Unable to allocate initial scbs\n",
4860 * Reserve the next queued SCB.
4862 ahc
->next_queued_scb
= ahc_get_scb(ahc
);
4865 * Note that we were successful
4875 ahc_fini_scbdata(struct ahc_softc
*ahc
)
4877 struct scb_data
*scb_data
;
4879 scb_data
= ahc
->scb_data
;
4880 if (scb_data
== NULL
)
4883 switch (scb_data
->init_level
) {
4887 struct sg_map_node
*sg_map
;
4889 while ((sg_map
= SLIST_FIRST(&scb_data
->sg_maps
))!= NULL
) {
4890 SLIST_REMOVE_HEAD(&scb_data
->sg_maps
, links
);
4891 ahc_dmamap_unload(ahc
, scb_data
->sg_dmat
,
4893 ahc_dmamem_free(ahc
, scb_data
->sg_dmat
,
4898 ahc_dma_tag_destroy(ahc
, scb_data
->sg_dmat
);
4902 ahc_dmamap_unload(ahc
, scb_data
->sense_dmat
,
4903 scb_data
->sense_dmamap
);
4906 ahc_dmamem_free(ahc
, scb_data
->sense_dmat
, scb_data
->sense
,
4907 scb_data
->sense_dmamap
);
4908 ahc_dmamap_destroy(ahc
, scb_data
->sense_dmat
,
4909 scb_data
->sense_dmamap
);
4912 ahc_dma_tag_destroy(ahc
, scb_data
->sense_dmat
);
4915 ahc_dmamap_unload(ahc
, scb_data
->hscb_dmat
,
4916 scb_data
->hscb_dmamap
);
4919 ahc_dmamem_free(ahc
, scb_data
->hscb_dmat
, scb_data
->hscbs
,
4920 scb_data
->hscb_dmamap
);
4921 ahc_dmamap_destroy(ahc
, scb_data
->hscb_dmat
,
4922 scb_data
->hscb_dmamap
);
4925 ahc_dma_tag_destroy(ahc
, scb_data
->hscb_dmat
);
4930 if (scb_data
->scbarray
!= NULL
)
4931 kfree(scb_data
->scbarray
);
4935 ahc_alloc_scbs(struct ahc_softc
*ahc
)
4937 struct scb_data
*scb_data
;
4938 struct scb
*next_scb
;
4939 struct sg_map_node
*sg_map
;
4940 dma_addr_t physaddr
;
4941 struct ahc_dma_seg
*segs
;
4945 scb_data
= ahc
->scb_data
;
4946 if (scb_data
->numscbs
>= AHC_SCB_MAX_ALLOC
)
4947 /* Can't allocate any more */
4950 next_scb
= &scb_data
->scbarray
[scb_data
->numscbs
];
4952 sg_map
= kmalloc(sizeof(*sg_map
), GFP_ATOMIC
);
4957 /* Allocate S/G space for the next batch of SCBS */
4958 if (ahc_dmamem_alloc(ahc
, scb_data
->sg_dmat
,
4959 (void **)&sg_map
->sg_vaddr
,
4960 BUS_DMA_NOWAIT
, &sg_map
->sg_dmamap
) != 0) {
4965 SLIST_INSERT_HEAD(&scb_data
->sg_maps
, sg_map
, links
);
4967 ahc_dmamap_load(ahc
, scb_data
->sg_dmat
, sg_map
->sg_dmamap
,
4968 sg_map
->sg_vaddr
, PAGE_SIZE
, ahc_dmamap_cb
,
4969 &sg_map
->sg_physaddr
, /*flags*/0);
4971 segs
= sg_map
->sg_vaddr
;
4972 physaddr
= sg_map
->sg_physaddr
;
4974 newcount
= (PAGE_SIZE
/ (AHC_NSEG
* sizeof(struct ahc_dma_seg
)));
4975 newcount
= min(newcount
, (AHC_SCB_MAX_ALLOC
- scb_data
->numscbs
));
4976 for (i
= 0; i
< newcount
; i
++) {
4977 struct scb_platform_data
*pdata
;
4979 pdata
= kmalloc(sizeof(*pdata
), GFP_ATOMIC
);
4982 next_scb
->platform_data
= pdata
;
4983 next_scb
->sg_map
= sg_map
;
4984 next_scb
->sg_list
= segs
;
4986 * The sequencer always starts with the second entry.
4987 * The first entry is embedded in the scb.
4989 next_scb
->sg_list_phys
= physaddr
+ sizeof(struct ahc_dma_seg
);
4990 next_scb
->ahc_softc
= ahc
;
4991 next_scb
->flags
= SCB_FREE
;
4992 next_scb
->hscb
= &scb_data
->hscbs
[scb_data
->numscbs
];
4993 next_scb
->hscb
->tag
= ahc
->scb_data
->numscbs
;
4994 SLIST_INSERT_HEAD(&ahc
->scb_data
->free_scbs
,
4995 next_scb
, links
.sle
);
4997 physaddr
+= (AHC_NSEG
* sizeof(struct ahc_dma_seg
));
4999 ahc
->scb_data
->numscbs
++;
5004 ahc_controller_info(struct ahc_softc
*ahc
, char *buf
)
5008 len
= sprintf(buf
, "%s: ", ahc_chip_names
[ahc
->chip
& AHC_CHIPID_MASK
]);
5010 if ((ahc
->features
& AHC_TWIN
) != 0)
5011 len
= sprintf(buf
, "Twin Channel, A SCSI Id=%d, "
5012 "B SCSI Id=%d, primary %c, ",
5013 ahc
->our_id
, ahc
->our_id_b
,
5014 (ahc
->flags
& AHC_PRIMARY_CHANNEL
) + 'A');
5020 if ((ahc
->features
& AHC_ULTRA
) != 0) {
5022 } else if ((ahc
->features
& AHC_DT
) != 0) {
5023 speed
= "Ultra160 ";
5024 } else if ((ahc
->features
& AHC_ULTRA2
) != 0) {
5027 if ((ahc
->features
& AHC_WIDE
) != 0) {
5032 len
= sprintf(buf
, "%s%s Channel %c, SCSI Id=%d, ",
5033 speed
, type
, ahc
->channel
, ahc
->our_id
);
5037 if ((ahc
->flags
& AHC_PAGESCBS
) != 0)
5038 sprintf(buf
, "%d/%d SCBs",
5039 ahc
->scb_data
->maxhscbs
, AHC_MAX_QUEUE
);
5041 sprintf(buf
, "%d SCBs", ahc
->scb_data
->maxhscbs
);
5045 ahc_chip_init(struct ahc_softc
*ahc
)
5051 u_int scsiseq_template
;
5054 ahc_outb(ahc
, SEQ_FLAGS
, 0);
5055 ahc_outb(ahc
, SEQ_FLAGS2
, 0);
5057 /* Set the SCSI Id, SXFRCTL0, SXFRCTL1, and SIMODE1, for both channels*/
5058 if (ahc
->features
& AHC_TWIN
) {
5061 * Setup Channel B first.
5063 ahc_outb(ahc
, SBLKCTL
, ahc_inb(ahc
, SBLKCTL
) | SELBUSB
);
5064 term
= (ahc
->flags
& AHC_TERM_ENB_B
) != 0 ? STPWEN
: 0;
5065 ahc_outb(ahc
, SCSIID
, ahc
->our_id_b
);
5066 scsi_conf
= ahc_inb(ahc
, SCSICONF
+ 1);
5067 ahc_outb(ahc
, SXFRCTL1
, (scsi_conf
& (ENSPCHK
|STIMESEL
))
5068 |term
|ahc
->seltime_b
|ENSTIMER
|ACTNEGEN
);
5069 if ((ahc
->features
& AHC_ULTRA2
) != 0)
5070 ahc_outb(ahc
, SIMODE0
, ahc_inb(ahc
, SIMODE0
)|ENIOERR
);
5071 ahc_outb(ahc
, SIMODE1
, ENSELTIMO
|ENSCSIRST
|ENSCSIPERR
);
5072 ahc_outb(ahc
, SXFRCTL0
, DFON
|SPIOEN
);
5074 /* Select Channel A */
5075 ahc_outb(ahc
, SBLKCTL
, ahc_inb(ahc
, SBLKCTL
) & ~SELBUSB
);
5077 term
= (ahc
->flags
& AHC_TERM_ENB_A
) != 0 ? STPWEN
: 0;
5078 if ((ahc
->features
& AHC_ULTRA2
) != 0)
5079 ahc_outb(ahc
, SCSIID_ULTRA2
, ahc
->our_id
);
5081 ahc_outb(ahc
, SCSIID
, ahc
->our_id
);
5082 scsi_conf
= ahc_inb(ahc
, SCSICONF
);
5083 ahc_outb(ahc
, SXFRCTL1
, (scsi_conf
& (ENSPCHK
|STIMESEL
))
5085 |ENSTIMER
|ACTNEGEN
);
5086 if ((ahc
->features
& AHC_ULTRA2
) != 0)
5087 ahc_outb(ahc
, SIMODE0
, ahc_inb(ahc
, SIMODE0
)|ENIOERR
);
5088 ahc_outb(ahc
, SIMODE1
, ENSELTIMO
|ENSCSIRST
|ENSCSIPERR
);
5089 ahc_outb(ahc
, SXFRCTL0
, DFON
|SPIOEN
);
5091 /* There are no untagged SCBs active yet. */
5092 for (i
= 0; i
< 16; i
++) {
5093 ahc_unbusy_tcl(ahc
, BUILD_TCL(i
<< 4, 0));
5094 if ((ahc
->flags
& AHC_SCB_BTT
) != 0) {
5098 * The SCB based BTT allows an entry per
5099 * target and lun pair.
5101 for (lun
= 1; lun
< AHC_NUM_LUNS
; lun
++)
5102 ahc_unbusy_tcl(ahc
, BUILD_TCL(i
<< 4, lun
));
5106 /* All of our queues are empty */
5107 for (i
= 0; i
< 256; i
++)
5108 ahc
->qoutfifo
[i
] = SCB_LIST_NULL
;
5109 ahc_sync_qoutfifo(ahc
, BUS_DMASYNC_PREREAD
);
5111 for (i
= 0; i
< 256; i
++)
5112 ahc
->qinfifo
[i
] = SCB_LIST_NULL
;
5114 if ((ahc
->features
& AHC_MULTI_TID
) != 0) {
5115 ahc_outb(ahc
, TARGID
, 0);
5116 ahc_outb(ahc
, TARGID
+ 1, 0);
5120 * Tell the sequencer where it can find our arrays in memory.
5122 physaddr
= ahc
->scb_data
->hscb_busaddr
;
5123 ahc_outb(ahc
, HSCB_ADDR
, physaddr
& 0xFF);
5124 ahc_outb(ahc
, HSCB_ADDR
+ 1, (physaddr
>> 8) & 0xFF);
5125 ahc_outb(ahc
, HSCB_ADDR
+ 2, (physaddr
>> 16) & 0xFF);
5126 ahc_outb(ahc
, HSCB_ADDR
+ 3, (physaddr
>> 24) & 0xFF);
5128 physaddr
= ahc
->shared_data_busaddr
;
5129 ahc_outb(ahc
, SHARED_DATA_ADDR
, physaddr
& 0xFF);
5130 ahc_outb(ahc
, SHARED_DATA_ADDR
+ 1, (physaddr
>> 8) & 0xFF);
5131 ahc_outb(ahc
, SHARED_DATA_ADDR
+ 2, (physaddr
>> 16) & 0xFF);
5132 ahc_outb(ahc
, SHARED_DATA_ADDR
+ 3, (physaddr
>> 24) & 0xFF);
5135 * Initialize the group code to command length table.
5136 * This overrides the values in TARG_SCSIRATE, so only
5137 * setup the table after we have processed that information.
5139 ahc_outb(ahc
, CMDSIZE_TABLE
, 5);
5140 ahc_outb(ahc
, CMDSIZE_TABLE
+ 1, 9);
5141 ahc_outb(ahc
, CMDSIZE_TABLE
+ 2, 9);
5142 ahc_outb(ahc
, CMDSIZE_TABLE
+ 3, 0);
5143 ahc_outb(ahc
, CMDSIZE_TABLE
+ 4, 15);
5144 ahc_outb(ahc
, CMDSIZE_TABLE
+ 5, 11);
5145 ahc_outb(ahc
, CMDSIZE_TABLE
+ 6, 0);
5146 ahc_outb(ahc
, CMDSIZE_TABLE
+ 7, 0);
5148 if ((ahc
->features
& AHC_HS_MAILBOX
) != 0)
5149 ahc_outb(ahc
, HS_MAILBOX
, 0);
5151 /* Tell the sequencer of our initial queue positions */
5152 if ((ahc
->features
& AHC_TARGETMODE
) != 0) {
5153 ahc
->tqinfifonext
= 1;
5154 ahc_outb(ahc
, KERNEL_TQINPOS
, ahc
->tqinfifonext
- 1);
5155 ahc_outb(ahc
, TQINPOS
, ahc
->tqinfifonext
);
5157 ahc
->qinfifonext
= 0;
5158 ahc
->qoutfifonext
= 0;
5159 if ((ahc
->features
& AHC_QUEUE_REGS
) != 0) {
5160 ahc_outb(ahc
, QOFF_CTLSTA
, SCB_QSIZE_256
);
5161 ahc_outb(ahc
, HNSCB_QOFF
, ahc
->qinfifonext
);
5162 ahc_outb(ahc
, SNSCB_QOFF
, ahc
->qinfifonext
);
5163 ahc_outb(ahc
, SDSCB_QOFF
, 0);
5165 ahc_outb(ahc
, KERNEL_QINPOS
, ahc
->qinfifonext
);
5166 ahc_outb(ahc
, QINPOS
, ahc
->qinfifonext
);
5167 ahc_outb(ahc
, QOUTPOS
, ahc
->qoutfifonext
);
5170 /* We don't have any waiting selections */
5171 ahc_outb(ahc
, WAITING_SCBH
, SCB_LIST_NULL
);
5173 /* Our disconnection list is empty too */
5174 ahc_outb(ahc
, DISCONNECTED_SCBH
, SCB_LIST_NULL
);
5176 /* Message out buffer starts empty */
5177 ahc_outb(ahc
, MSG_OUT
, MSG_NOOP
);
5180 * Setup the allowed SCSI Sequences based on operational mode.
5181 * If we are a target, we'll enable select in operations once
5182 * we've had a lun enabled.
5184 scsiseq_template
= ENSELO
|ENAUTOATNO
|ENAUTOATNP
;
5185 if ((ahc
->flags
& AHC_INITIATORROLE
) != 0)
5186 scsiseq_template
|= ENRSELI
;
5187 ahc_outb(ahc
, SCSISEQ_TEMPLATE
, scsiseq_template
);
5189 /* Initialize our list of free SCBs. */
5190 ahc_build_free_scb_list(ahc
);
5193 * Tell the sequencer which SCB will be the next one it receives.
5195 ahc_outb(ahc
, NEXT_QUEUED_SCB
, ahc
->next_queued_scb
->hscb
->tag
);
5198 * Load the Sequencer program and Enable the adapter
5202 printk("%s: Downloading Sequencer Program...",
5205 error
= ahc_loadseq(ahc
);
5209 if ((ahc
->features
& AHC_ULTRA2
) != 0) {
5213 * Wait for up to 500ms for our transceivers
5214 * to settle. If the adapter does not have
5215 * a cable attached, the transceivers may
5216 * never settle, so don't complain if we
5220 (ahc_inb(ahc
, SBLKCTL
) & (ENAB40
|ENAB20
)) == 0 && wait
;
5229 * Start the board, ready for normal operation
5232 ahc_init(struct ahc_softc
*ahc
)
5240 size_t driver_data_size
;
5243 if ((ahc_debug
& AHC_DEBUG_SEQUENCER
) != 0)
5244 ahc
->flags
|= AHC_SEQUENCER_DEBUG
;
5247 #ifdef AHC_PRINT_SRAM
5248 printk("Scratch Ram:");
5249 for (i
= 0x20; i
< 0x5f; i
++) {
5250 if (((i
% 8) == 0) && (i
!= 0)) {
5253 printk (" 0x%x", ahc_inb(ahc
, i
));
5255 if ((ahc
->features
& AHC_MORE_SRAM
) != 0) {
5256 for (i
= 0x70; i
< 0x7f; i
++) {
5257 if (((i
% 8) == 0) && (i
!= 0)) {
5260 printk (" 0x%x", ahc_inb(ahc
, i
));
5265 * Reading uninitialized scratch ram may
5266 * generate parity errors.
5268 ahc_outb(ahc
, CLRINT
, CLRPARERR
);
5269 ahc_outb(ahc
, CLRINT
, CLRBRKADRINT
);
5274 * Assume we have a board at this stage and it has been reset.
5276 if ((ahc
->flags
& AHC_USEDEFAULTS
) != 0)
5277 ahc
->our_id
= ahc
->our_id_b
= 7;
5280 * Default to allowing initiator operations.
5282 ahc
->flags
|= AHC_INITIATORROLE
;
5285 * Only allow target mode features if this unit has them enabled.
5287 if ((AHC_TMODE_ENABLE
& (0x1 << ahc
->unit
)) == 0)
5288 ahc
->features
&= ~AHC_TARGETMODE
;
5293 * DMA tag for our command fifos and other data in system memory
5294 * the card's sequencer must be able to access. For initiator
5295 * roles, we need to allocate space for the qinfifo and qoutfifo.
5296 * The qinfifo and qoutfifo are composed of 256 1 byte elements.
5297 * When providing for the target mode role, we must additionally
5298 * provide space for the incoming target command fifo and an extra
5299 * byte to deal with a dma bug in some chip versions.
5301 driver_data_size
= 2 * 256 * sizeof(uint8_t);
5302 if ((ahc
->features
& AHC_TARGETMODE
) != 0)
5303 driver_data_size
+= AHC_TMODE_CMDS
* sizeof(struct target_cmd
)
5304 + /*DMA WideOdd Bug Buffer*/1;
5305 if (ahc_dma_tag_create(ahc
, ahc
->parent_dmat
, /*alignment*/1,
5306 /*boundary*/BUS_SPACE_MAXADDR_32BIT
+ 1,
5307 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT
,
5308 /*highaddr*/BUS_SPACE_MAXADDR
,
5309 /*filter*/NULL
, /*filterarg*/NULL
,
5312 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT
,
5313 /*flags*/0, &ahc
->shared_data_dmat
) != 0) {
5319 /* Allocation of driver data */
5320 if (ahc_dmamem_alloc(ahc
, ahc
->shared_data_dmat
,
5321 (void **)&ahc
->qoutfifo
,
5322 BUS_DMA_NOWAIT
, &ahc
->shared_data_dmamap
) != 0) {
5328 /* And permanently map it in */
5329 ahc_dmamap_load(ahc
, ahc
->shared_data_dmat
, ahc
->shared_data_dmamap
,
5330 ahc
->qoutfifo
, driver_data_size
, ahc_dmamap_cb
,
5331 &ahc
->shared_data_busaddr
, /*flags*/0);
5333 if ((ahc
->features
& AHC_TARGETMODE
) != 0) {
5334 ahc
->targetcmds
= (struct target_cmd
*)ahc
->qoutfifo
;
5335 ahc
->qoutfifo
= (uint8_t *)&ahc
->targetcmds
[AHC_TMODE_CMDS
];
5336 ahc
->dma_bug_buf
= ahc
->shared_data_busaddr
5337 + driver_data_size
- 1;
5338 /* All target command blocks start out invalid. */
5339 for (i
= 0; i
< AHC_TMODE_CMDS
; i
++)
5340 ahc
->targetcmds
[i
].cmd_valid
= 0;
5341 ahc_sync_tqinfifo(ahc
, BUS_DMASYNC_PREREAD
);
5342 ahc
->qoutfifo
= (uint8_t *)&ahc
->targetcmds
[256];
5344 ahc
->qinfifo
= &ahc
->qoutfifo
[256];
5348 /* Allocate SCB data now that buffer_dmat is initialized */
5349 if (ahc
->scb_data
->maxhscbs
== 0)
5350 if (ahc_init_scbdata(ahc
) != 0)
5354 * Allocate a tstate to house information for our
5355 * initiator presence on the bus as well as the user
5356 * data for any target mode initiator.
5358 if (ahc_alloc_tstate(ahc
, ahc
->our_id
, 'A') == NULL
) {
5359 printk("%s: unable to allocate ahc_tmode_tstate. "
5360 "Failing attach\n", ahc_name(ahc
));
5364 if ((ahc
->features
& AHC_TWIN
) != 0) {
5365 if (ahc_alloc_tstate(ahc
, ahc
->our_id_b
, 'B') == NULL
) {
5366 printk("%s: unable to allocate ahc_tmode_tstate. "
5367 "Failing attach\n", ahc_name(ahc
));
5372 if (ahc
->scb_data
->maxhscbs
< AHC_SCB_MAX_ALLOC
) {
5373 ahc
->flags
|= AHC_PAGESCBS
;
5375 ahc
->flags
&= ~AHC_PAGESCBS
;
5379 if (ahc_debug
& AHC_SHOW_MISC
) {
5380 printk("%s: hardware scb %u bytes; kernel scb %u bytes; "
5381 "ahc_dma %u bytes\n",
5383 (u_int
)sizeof(struct hardware_scb
),
5384 (u_int
)sizeof(struct scb
),
5385 (u_int
)sizeof(struct ahc_dma_seg
));
5387 #endif /* AHC_DEBUG */
5390 * Look at the information that board initialization or
5391 * the board bios has left us.
5393 if (ahc
->features
& AHC_TWIN
) {
5394 scsi_conf
= ahc_inb(ahc
, SCSICONF
+ 1);
5395 if ((scsi_conf
& RESET_SCSI
) != 0
5396 && (ahc
->flags
& AHC_INITIATORROLE
) != 0)
5397 ahc
->flags
|= AHC_RESET_BUS_B
;
5400 scsi_conf
= ahc_inb(ahc
, SCSICONF
);
5401 if ((scsi_conf
& RESET_SCSI
) != 0
5402 && (ahc
->flags
& AHC_INITIATORROLE
) != 0)
5403 ahc
->flags
|= AHC_RESET_BUS_A
;
5406 tagenable
= ALL_TARGETS_MASK
;
5408 /* Grab the disconnection disable table and invert it for our needs */
5409 if ((ahc
->flags
& AHC_USEDEFAULTS
) != 0) {
5410 printk("%s: Host Adapter Bios disabled. Using default SCSI "
5411 "device parameters\n", ahc_name(ahc
));
5412 ahc
->flags
|= AHC_EXTENDED_TRANS_A
|AHC_EXTENDED_TRANS_B
|
5413 AHC_TERM_ENB_A
|AHC_TERM_ENB_B
;
5414 discenable
= ALL_TARGETS_MASK
;
5415 if ((ahc
->features
& AHC_ULTRA
) != 0)
5416 ultraenb
= ALL_TARGETS_MASK
;
5418 discenable
= ~((ahc_inb(ahc
, DISC_DSB
+ 1) << 8)
5419 | ahc_inb(ahc
, DISC_DSB
));
5420 if ((ahc
->features
& (AHC_ULTRA
|AHC_ULTRA2
)) != 0)
5421 ultraenb
= (ahc_inb(ahc
, ULTRA_ENB
+ 1) << 8)
5422 | ahc_inb(ahc
, ULTRA_ENB
);
5425 if ((ahc
->features
& (AHC_WIDE
|AHC_TWIN
)) == 0)
5428 for (i
= 0; i
<= max_targ
; i
++) {
5429 struct ahc_initiator_tinfo
*tinfo
;
5430 struct ahc_tmode_tstate
*tstate
;
5436 our_id
= ahc
->our_id
;
5438 if (i
> 7 && (ahc
->features
& AHC_TWIN
) != 0) {
5440 our_id
= ahc
->our_id_b
;
5443 tinfo
= ahc_fetch_transinfo(ahc
, channel
, our_id
,
5444 target_id
, &tstate
);
5445 /* Default to async narrow across the board */
5446 memset(tinfo
, 0, sizeof(*tinfo
));
5447 if (ahc
->flags
& AHC_USEDEFAULTS
) {
5448 if ((ahc
->features
& AHC_WIDE
) != 0)
5449 tinfo
->user
.width
= MSG_EXT_WDTR_BUS_16_BIT
;
5452 * These will be truncated when we determine the
5453 * connection type we have with the target.
5455 tinfo
->user
.period
= ahc_syncrates
->period
;
5456 tinfo
->user
.offset
= MAX_OFFSET
;
5461 /* Take the settings leftover in scratch RAM. */
5462 scsirate
= ahc_inb(ahc
, TARG_SCSIRATE
+ i
);
5464 if ((ahc
->features
& AHC_ULTRA2
) != 0) {
5468 if ((scsirate
& SOFS
) == 0x0F) {
5470 * Haven't negotiated yet,
5471 * so the format is different.
5473 scsirate
= (scsirate
& SXFR
) >> 4
5476 | (scsirate
& WIDEXFER
);
5477 offset
= MAX_OFFSET_ULTRA2
;
5479 offset
= ahc_inb(ahc
, TARG_OFFSET
+ i
);
5480 if ((scsirate
& ~WIDEXFER
) == 0 && offset
!= 0)
5481 /* Set to the lowest sync rate, 5MHz */
5483 maxsync
= AHC_SYNCRATE_ULTRA2
;
5484 if ((ahc
->features
& AHC_DT
) != 0)
5485 maxsync
= AHC_SYNCRATE_DT
;
5486 tinfo
->user
.period
=
5487 ahc_find_period(ahc
, scsirate
, maxsync
);
5489 tinfo
->user
.period
= 0;
5491 tinfo
->user
.offset
= MAX_OFFSET
;
5492 if ((scsirate
& SXFR_ULTRA2
) <= 8/*10MHz*/
5493 && (ahc
->features
& AHC_DT
) != 0)
5494 tinfo
->user
.ppr_options
=
5496 } else if ((scsirate
& SOFS
) != 0) {
5497 if ((scsirate
& SXFR
) == 0x40
5498 && (ultraenb
& mask
) != 0) {
5499 /* Treat 10MHz as a non-ultra speed */
5503 tinfo
->user
.period
=
5504 ahc_find_period(ahc
, scsirate
,
5506 ? AHC_SYNCRATE_ULTRA
5507 : AHC_SYNCRATE_FAST
);
5508 if (tinfo
->user
.period
!= 0)
5509 tinfo
->user
.offset
= MAX_OFFSET
;
5511 if (tinfo
->user
.period
== 0)
5512 tinfo
->user
.offset
= 0;
5513 if ((scsirate
& WIDEXFER
) != 0
5514 && (ahc
->features
& AHC_WIDE
) != 0)
5515 tinfo
->user
.width
= MSG_EXT_WDTR_BUS_16_BIT
;
5516 tinfo
->user
.protocol_version
= 4;
5517 if ((ahc
->features
& AHC_DT
) != 0)
5518 tinfo
->user
.transport_version
= 3;
5520 tinfo
->user
.transport_version
= 2;
5521 tinfo
->goal
.protocol_version
= 2;
5522 tinfo
->goal
.transport_version
= 2;
5523 tinfo
->curr
.protocol_version
= 2;
5524 tinfo
->curr
.transport_version
= 2;
5526 tstate
->ultraenb
= 0;
5528 ahc
->user_discenable
= discenable
;
5529 ahc
->user_tagenable
= tagenable
;
5531 return (ahc
->bus_chip_init(ahc
));
5535 ahc_intr_enable(struct ahc_softc
*ahc
, int enable
)
5539 hcntrl
= ahc_inb(ahc
, HCNTRL
);
5541 ahc
->pause
&= ~INTEN
;
5542 ahc
->unpause
&= ~INTEN
;
5545 ahc
->pause
|= INTEN
;
5546 ahc
->unpause
|= INTEN
;
5548 ahc_outb(ahc
, HCNTRL
, hcntrl
);
5552 * Ensure that the card is paused in a location
5553 * outside of all critical sections and that all
5554 * pending work is completed prior to returning.
5555 * This routine should only be called from outside
5556 * an interrupt context.
5559 ahc_pause_and_flushwork(struct ahc_softc
*ahc
)
5566 ahc
->flags
|= AHC_ALL_INTERRUPTS
;
5572 * Give the sequencer some time to service
5573 * any active selections.
5580 ahc_outb(ahc
, SCSISEQ
, ahc_inb(ahc
, SCSISEQ
) & ~ENSELO
);
5581 intstat
= ahc_inb(ahc
, INTSTAT
);
5582 if ((intstat
& INT_PEND
) == 0) {
5583 ahc_clear_critical_section(ahc
);
5584 intstat
= ahc_inb(ahc
, INTSTAT
);
5587 && (intstat
!= 0xFF || (ahc
->features
& AHC_REMOVABLE
) == 0)
5588 && ((intstat
& INT_PEND
) != 0
5589 || (ahc_inb(ahc
, SSTAT0
) & (SELDO
|SELINGO
)) != 0));
5590 if (maxloops
== 0) {
5591 printk("Infinite interrupt loop, INTSTAT = %x",
5592 ahc_inb(ahc
, INTSTAT
));
5594 ahc_platform_flushwork(ahc
);
5595 ahc
->flags
&= ~AHC_ALL_INTERRUPTS
;
5600 ahc_suspend(struct ahc_softc
*ahc
)
5603 ahc_pause_and_flushwork(ahc
);
5605 if (LIST_FIRST(&ahc
->pending_scbs
) != NULL
) {
5610 #ifdef AHC_TARGET_MODE
5612 * XXX What about ATIOs that have not yet been serviced?
5613 * Perhaps we should just refuse to be suspended if we
5614 * are acting in a target role.
5616 if (ahc
->pending_device
!= NULL
) {
5626 ahc_resume(struct ahc_softc
*ahc
)
5629 ahc_reset(ahc
, /*reinit*/TRUE
);
5630 ahc_intr_enable(ahc
, TRUE
);
5635 /************************** Busy Target Table *********************************/
5637 * Return the untagged transaction id for a given target/channel lun.
5638 * Optionally, clear the entry.
5641 ahc_index_busy_tcl(struct ahc_softc
*ahc
, u_int tcl
)
5644 u_int target_offset
;
5646 if ((ahc
->flags
& AHC_SCB_BTT
) != 0) {
5649 saved_scbptr
= ahc_inb(ahc
, SCBPTR
);
5650 ahc_outb(ahc
, SCBPTR
, TCL_LUN(tcl
));
5651 scbid
= ahc_inb(ahc
, SCB_64_BTT
+ TCL_TARGET_OFFSET(tcl
));
5652 ahc_outb(ahc
, SCBPTR
, saved_scbptr
);
5654 target_offset
= TCL_TARGET_OFFSET(tcl
);
5655 scbid
= ahc_inb(ahc
, BUSY_TARGETS
+ target_offset
);
5662 ahc_unbusy_tcl(struct ahc_softc
*ahc
, u_int tcl
)
5664 u_int target_offset
;
5666 if ((ahc
->flags
& AHC_SCB_BTT
) != 0) {
5669 saved_scbptr
= ahc_inb(ahc
, SCBPTR
);
5670 ahc_outb(ahc
, SCBPTR
, TCL_LUN(tcl
));
5671 ahc_outb(ahc
, SCB_64_BTT
+TCL_TARGET_OFFSET(tcl
), SCB_LIST_NULL
);
5672 ahc_outb(ahc
, SCBPTR
, saved_scbptr
);
5674 target_offset
= TCL_TARGET_OFFSET(tcl
);
5675 ahc_outb(ahc
, BUSY_TARGETS
+ target_offset
, SCB_LIST_NULL
);
5680 ahc_busy_tcl(struct ahc_softc
*ahc
, u_int tcl
, u_int scbid
)
5682 u_int target_offset
;
5684 if ((ahc
->flags
& AHC_SCB_BTT
) != 0) {
5687 saved_scbptr
= ahc_inb(ahc
, SCBPTR
);
5688 ahc_outb(ahc
, SCBPTR
, TCL_LUN(tcl
));
5689 ahc_outb(ahc
, SCB_64_BTT
+ TCL_TARGET_OFFSET(tcl
), scbid
);
5690 ahc_outb(ahc
, SCBPTR
, saved_scbptr
);
5692 target_offset
= TCL_TARGET_OFFSET(tcl
);
5693 ahc_outb(ahc
, BUSY_TARGETS
+ target_offset
, scbid
);
5697 /************************** SCB and SCB queue management **********************/
5699 ahc_match_scb(struct ahc_softc
*ahc
, struct scb
*scb
, int target
,
5700 char channel
, int lun
, u_int tag
, role_t role
)
5702 int targ
= SCB_GET_TARGET(ahc
, scb
);
5703 char chan
= SCB_GET_CHANNEL(ahc
, scb
);
5704 int slun
= SCB_GET_LUN(scb
);
5707 match
= ((chan
== channel
) || (channel
== ALL_CHANNELS
));
5709 match
= ((targ
== target
) || (target
== CAM_TARGET_WILDCARD
));
5711 match
= ((lun
== slun
) || (lun
== CAM_LUN_WILDCARD
));
5713 #ifdef AHC_TARGET_MODE
5716 group
= XPT_FC_GROUP(scb
->io_ctx
->ccb_h
.func_code
);
5717 if (role
== ROLE_INITIATOR
) {
5718 match
= (group
!= XPT_FC_GROUP_TMODE
)
5719 && ((tag
== scb
->hscb
->tag
)
5720 || (tag
== SCB_LIST_NULL
));
5721 } else if (role
== ROLE_TARGET
) {
5722 match
= (group
== XPT_FC_GROUP_TMODE
)
5723 && ((tag
== scb
->io_ctx
->csio
.tag_id
)
5724 || (tag
== SCB_LIST_NULL
));
5726 #else /* !AHC_TARGET_MODE */
5727 match
= ((tag
== scb
->hscb
->tag
) || (tag
== SCB_LIST_NULL
));
5728 #endif /* AHC_TARGET_MODE */
5735 ahc_freeze_devq(struct ahc_softc
*ahc
, struct scb
*scb
)
5741 target
= SCB_GET_TARGET(ahc
, scb
);
5742 lun
= SCB_GET_LUN(scb
);
5743 channel
= SCB_GET_CHANNEL(ahc
, scb
);
5745 ahc_search_qinfifo(ahc
, target
, channel
, lun
,
5746 /*tag*/SCB_LIST_NULL
, ROLE_UNKNOWN
,
5747 CAM_REQUEUE_REQ
, SEARCH_COMPLETE
);
5749 ahc_platform_freeze_devq(ahc
, scb
);
5753 ahc_qinfifo_requeue_tail(struct ahc_softc
*ahc
, struct scb
*scb
)
5755 struct scb
*prev_scb
;
5758 if (ahc_qinfifo_count(ahc
) != 0) {
5762 prev_pos
= ahc
->qinfifonext
- 1;
5763 prev_tag
= ahc
->qinfifo
[prev_pos
];
5764 prev_scb
= ahc_lookup_scb(ahc
, prev_tag
);
5766 ahc_qinfifo_requeue(ahc
, prev_scb
, scb
);
5767 if ((ahc
->features
& AHC_QUEUE_REGS
) != 0) {
5768 ahc_outb(ahc
, HNSCB_QOFF
, ahc
->qinfifonext
);
5770 ahc_outb(ahc
, KERNEL_QINPOS
, ahc
->qinfifonext
);
5775 ahc_qinfifo_requeue(struct ahc_softc
*ahc
, struct scb
*prev_scb
,
5778 if (prev_scb
== NULL
) {
5779 ahc_outb(ahc
, NEXT_QUEUED_SCB
, scb
->hscb
->tag
);
5781 prev_scb
->hscb
->next
= scb
->hscb
->tag
;
5782 ahc_sync_scb(ahc
, prev_scb
,
5783 BUS_DMASYNC_PREREAD
|BUS_DMASYNC_PREWRITE
);
5785 ahc
->qinfifo
[ahc
->qinfifonext
++] = scb
->hscb
->tag
;
5786 scb
->hscb
->next
= ahc
->next_queued_scb
->hscb
->tag
;
5787 ahc_sync_scb(ahc
, scb
, BUS_DMASYNC_PREREAD
|BUS_DMASYNC_PREWRITE
);
5791 ahc_qinfifo_count(struct ahc_softc
*ahc
)
5796 if ((ahc
->features
& AHC_QUEUE_REGS
) != 0) {
5797 qinpos
= ahc_inb(ahc
, SNSCB_QOFF
);
5798 ahc_outb(ahc
, SNSCB_QOFF
, qinpos
);
5800 qinpos
= ahc_inb(ahc
, QINPOS
);
5801 diff
= ahc
->qinfifonext
- qinpos
;
5806 ahc_search_qinfifo(struct ahc_softc
*ahc
, int target
, char channel
,
5807 int lun
, u_int tag
, role_t role
, uint32_t status
,
5808 ahc_search_action action
)
5811 struct scb
*prev_scb
;
5821 qintail
= ahc
->qinfifonext
;
5822 have_qregs
= (ahc
->features
& AHC_QUEUE_REGS
) != 0;
5824 qinstart
= ahc_inb(ahc
, SNSCB_QOFF
);
5825 ahc_outb(ahc
, SNSCB_QOFF
, qinstart
);
5827 qinstart
= ahc_inb(ahc
, QINPOS
);
5832 if (action
== SEARCH_COMPLETE
) {
5834 * Don't attempt to run any queued untagged transactions
5835 * until we are done with the abort process.
5837 ahc_freeze_untagged_queues(ahc
);
5841 * Start with an empty queue. Entries that are not chosen
5842 * for removal will be re-added to the queue as we go.
5844 ahc
->qinfifonext
= qinpos
;
5845 ahc_outb(ahc
, NEXT_QUEUED_SCB
, ahc
->next_queued_scb
->hscb
->tag
);
5847 while (qinpos
!= qintail
) {
5848 scb
= ahc_lookup_scb(ahc
, ahc
->qinfifo
[qinpos
]);
5850 printk("qinpos = %d, SCB index = %d\n",
5851 qinpos
, ahc
->qinfifo
[qinpos
]);
5855 if (ahc_match_scb(ahc
, scb
, target
, channel
, lun
, tag
, role
)) {
5857 * We found an scb that needs to be acted on.
5861 case SEARCH_COMPLETE
:
5866 ostat
= ahc_get_transaction_status(scb
);
5867 if (ostat
== CAM_REQ_INPROG
)
5868 ahc_set_transaction_status(scb
, status
);
5869 cstat
= ahc_get_transaction_status(scb
);
5870 if (cstat
!= CAM_REQ_CMP
)
5871 ahc_freeze_scb(scb
);
5872 if ((scb
->flags
& SCB_ACTIVE
) == 0)
5873 printk("Inactive SCB in qinfifo\n");
5881 ahc_qinfifo_requeue(ahc
, prev_scb
, scb
);
5886 ahc_qinfifo_requeue(ahc
, prev_scb
, scb
);
5892 if ((ahc
->features
& AHC_QUEUE_REGS
) != 0) {
5893 ahc_outb(ahc
, HNSCB_QOFF
, ahc
->qinfifonext
);
5895 ahc_outb(ahc
, KERNEL_QINPOS
, ahc
->qinfifonext
);
5898 if (action
!= SEARCH_COUNT
5900 && (qinstart
!= ahc
->qinfifonext
)) {
5902 * The sequencer may be in the process of dmaing
5903 * down the SCB at the beginning of the queue.
5904 * This could be problematic if either the first,
5905 * or the second SCB is removed from the queue
5906 * (the first SCB includes a pointer to the "next"
5907 * SCB to dma). If we have removed any entries, swap
5908 * the first element in the queue with the next HSCB
5909 * so the sequencer will notice that NEXT_QUEUED_SCB
5910 * has changed during its dma attempt and will retry
5913 scb
= ahc_lookup_scb(ahc
, ahc
->qinfifo
[qinstart
]);
5916 printk("found = %d, qinstart = %d, qinfifionext = %d\n",
5917 found
, qinstart
, ahc
->qinfifonext
);
5918 panic("First/Second Qinfifo fixup\n");
5921 * ahc_swap_with_next_hscb forces our next pointer to
5922 * point to the reserved SCB for future commands. Save
5923 * and restore our original next pointer to maintain
5926 next
= scb
->hscb
->next
;
5927 ahc
->scb_data
->scbindex
[scb
->hscb
->tag
] = NULL
;
5928 ahc_swap_with_next_hscb(ahc
, scb
);
5929 scb
->hscb
->next
= next
;
5930 ahc
->qinfifo
[qinstart
] = scb
->hscb
->tag
;
5932 /* Tell the card about the new head of the qinfifo. */
5933 ahc_outb(ahc
, NEXT_QUEUED_SCB
, scb
->hscb
->tag
);
5935 /* Fixup the tail "next" pointer. */
5936 qintail
= ahc
->qinfifonext
- 1;
5937 scb
= ahc_lookup_scb(ahc
, ahc
->qinfifo
[qintail
]);
5938 scb
->hscb
->next
= ahc
->next_queued_scb
->hscb
->tag
;
5942 * Search waiting for selection list.
5944 curscbptr
= ahc_inb(ahc
, SCBPTR
);
5945 next
= ahc_inb(ahc
, WAITING_SCBH
); /* Start at head of list. */
5946 prev
= SCB_LIST_NULL
;
5948 while (next
!= SCB_LIST_NULL
) {
5951 ahc_outb(ahc
, SCBPTR
, next
);
5952 scb_index
= ahc_inb(ahc
, SCB_TAG
);
5953 if (scb_index
>= ahc
->scb_data
->numscbs
) {
5954 printk("Waiting List inconsistency. "
5955 "SCB index == %d, yet numscbs == %d.",
5956 scb_index
, ahc
->scb_data
->numscbs
);
5957 ahc_dump_card_state(ahc
);
5958 panic("for safety");
5960 scb
= ahc_lookup_scb(ahc
, scb_index
);
5962 printk("scb_index = %d, next = %d\n",
5964 panic("Waiting List traversal\n");
5966 if (ahc_match_scb(ahc
, scb
, target
, channel
,
5967 lun
, SCB_LIST_NULL
, role
)) {
5969 * We found an scb that needs to be acted on.
5973 case SEARCH_COMPLETE
:
5978 ostat
= ahc_get_transaction_status(scb
);
5979 if (ostat
== CAM_REQ_INPROG
)
5980 ahc_set_transaction_status(scb
,
5982 cstat
= ahc_get_transaction_status(scb
);
5983 if (cstat
!= CAM_REQ_CMP
)
5984 ahc_freeze_scb(scb
);
5985 if ((scb
->flags
& SCB_ACTIVE
) == 0)
5986 printk("Inactive SCB in Waiting List\n");
5991 next
= ahc_rem_wscb(ahc
, next
, prev
);
5995 next
= ahc_inb(ahc
, SCB_NEXT
);
6001 next
= ahc_inb(ahc
, SCB_NEXT
);
6004 ahc_outb(ahc
, SCBPTR
, curscbptr
);
6006 found
+= ahc_search_untagged_queues(ahc
, /*ahc_io_ctx_t*/NULL
, target
,
6007 channel
, lun
, status
, action
);
6009 if (action
== SEARCH_COMPLETE
)
6010 ahc_release_untagged_queues(ahc
);
6015 ahc_search_untagged_queues(struct ahc_softc
*ahc
, ahc_io_ctx_t ctx
,
6016 int target
, char channel
, int lun
, uint32_t status
,
6017 ahc_search_action action
)
6024 if (action
== SEARCH_COMPLETE
) {
6026 * Don't attempt to run any queued untagged transactions
6027 * until we are done with the abort process.
6029 ahc_freeze_untagged_queues(ahc
);
6034 if ((ahc
->flags
& AHC_SCB_BTT
) == 0) {
6037 if (target
!= CAM_TARGET_WILDCARD
) {
6048 for (; i
< maxtarget
; i
++) {
6049 struct scb_tailq
*untagged_q
;
6050 struct scb
*next_scb
;
6052 untagged_q
= &(ahc
->untagged_queues
[i
]);
6053 next_scb
= TAILQ_FIRST(untagged_q
);
6054 while (next_scb
!= NULL
) {
6057 next_scb
= TAILQ_NEXT(scb
, links
.tqe
);
6060 * The head of the list may be the currently
6061 * active untagged command for a device.
6062 * We're only searching for commands that
6063 * have not been started. A transaction
6064 * marked active but still in the qinfifo
6065 * is removed by the qinfifo scanning code
6068 if ((scb
->flags
& SCB_ACTIVE
) != 0)
6071 if (ahc_match_scb(ahc
, scb
, target
, channel
, lun
,
6072 SCB_LIST_NULL
, ROLE_INITIATOR
) == 0
6073 || (ctx
!= NULL
&& ctx
!= scb
->io_ctx
))
6077 * We found an scb that needs to be acted on.
6081 case SEARCH_COMPLETE
:
6086 ostat
= ahc_get_transaction_status(scb
);
6087 if (ostat
== CAM_REQ_INPROG
)
6088 ahc_set_transaction_status(scb
, status
);
6089 cstat
= ahc_get_transaction_status(scb
);
6090 if (cstat
!= CAM_REQ_CMP
)
6091 ahc_freeze_scb(scb
);
6092 if ((scb
->flags
& SCB_ACTIVE
) == 0)
6093 printk("Inactive SCB in untaggedQ\n");
6098 scb
->flags
&= ~SCB_UNTAGGEDQ
;
6099 TAILQ_REMOVE(untagged_q
, scb
, links
.tqe
);
6107 if (action
== SEARCH_COMPLETE
)
6108 ahc_release_untagged_queues(ahc
);
6113 ahc_search_disc_list(struct ahc_softc
*ahc
, int target
, char channel
,
6114 int lun
, u_int tag
, int stop_on_first
, int remove
,
6124 next
= ahc_inb(ahc
, DISCONNECTED_SCBH
);
6125 prev
= SCB_LIST_NULL
;
6128 /* restore this when we're done */
6129 active_scb
= ahc_inb(ahc
, SCBPTR
);
6131 /* Silence compiler */
6132 active_scb
= SCB_LIST_NULL
;
6134 while (next
!= SCB_LIST_NULL
) {
6137 ahc_outb(ahc
, SCBPTR
, next
);
6138 scb_index
= ahc_inb(ahc
, SCB_TAG
);
6139 if (scb_index
>= ahc
->scb_data
->numscbs
) {
6140 printk("Disconnected List inconsistency. "
6141 "SCB index == %d, yet numscbs == %d.",
6142 scb_index
, ahc
->scb_data
->numscbs
);
6143 ahc_dump_card_state(ahc
);
6144 panic("for safety");
6148 panic("Disconnected List Loop. "
6149 "cur SCBPTR == %x, prev SCBPTR == %x.",
6152 scbp
= ahc_lookup_scb(ahc
, scb_index
);
6153 if (ahc_match_scb(ahc
, scbp
, target
, channel
, lun
,
6154 tag
, ROLE_INITIATOR
)) {
6158 ahc_rem_scb_from_disc_list(ahc
, prev
, next
);
6161 next
= ahc_inb(ahc
, SCB_NEXT
);
6167 next
= ahc_inb(ahc
, SCB_NEXT
);
6171 ahc_outb(ahc
, SCBPTR
, active_scb
);
6176 * Remove an SCB from the on chip list of disconnected transactions.
6177 * This is empty/unused if we are not performing SCB paging.
6180 ahc_rem_scb_from_disc_list(struct ahc_softc
*ahc
, u_int prev
, u_int scbptr
)
6184 ahc_outb(ahc
, SCBPTR
, scbptr
);
6185 next
= ahc_inb(ahc
, SCB_NEXT
);
6187 ahc_outb(ahc
, SCB_CONTROL
, 0);
6189 ahc_add_curscb_to_free_list(ahc
);
6191 if (prev
!= SCB_LIST_NULL
) {
6192 ahc_outb(ahc
, SCBPTR
, prev
);
6193 ahc_outb(ahc
, SCB_NEXT
, next
);
6195 ahc_outb(ahc
, DISCONNECTED_SCBH
, next
);
6201 * Add the SCB as selected by SCBPTR onto the on chip list of
6202 * free hardware SCBs. This list is empty/unused if we are not
6203 * performing SCB paging.
6206 ahc_add_curscb_to_free_list(struct ahc_softc
*ahc
)
6209 * Invalidate the tag so that our abort
6210 * routines don't think it's active.
6212 ahc_outb(ahc
, SCB_TAG
, SCB_LIST_NULL
);
6214 if ((ahc
->flags
& AHC_PAGESCBS
) != 0) {
6215 ahc_outb(ahc
, SCB_NEXT
, ahc_inb(ahc
, FREE_SCBH
));
6216 ahc_outb(ahc
, FREE_SCBH
, ahc_inb(ahc
, SCBPTR
));
6221 * Manipulate the waiting for selection list and return the
6222 * scb that follows the one that we remove.
6225 ahc_rem_wscb(struct ahc_softc
*ahc
, u_int scbpos
, u_int prev
)
6230 * Select the SCB we want to abort and
6231 * pull the next pointer out of it.
6233 curscb
= ahc_inb(ahc
, SCBPTR
);
6234 ahc_outb(ahc
, SCBPTR
, scbpos
);
6235 next
= ahc_inb(ahc
, SCB_NEXT
);
6237 /* Clear the necessary fields */
6238 ahc_outb(ahc
, SCB_CONTROL
, 0);
6240 ahc_add_curscb_to_free_list(ahc
);
6242 /* update the waiting list */
6243 if (prev
== SCB_LIST_NULL
) {
6244 /* First in the list */
6245 ahc_outb(ahc
, WAITING_SCBH
, next
);
6248 * Ensure we aren't attempting to perform
6249 * selection for this entry.
6251 ahc_outb(ahc
, SCSISEQ
, (ahc_inb(ahc
, SCSISEQ
) & ~ENSELO
));
6254 * Select the scb that pointed to us
6255 * and update its next pointer.
6257 ahc_outb(ahc
, SCBPTR
, prev
);
6258 ahc_outb(ahc
, SCB_NEXT
, next
);
6262 * Point us back at the original scb position.
6264 ahc_outb(ahc
, SCBPTR
, curscb
);
6268 /******************************** Error Handling ******************************/
6270 * Abort all SCBs that match the given description (target/channel/lun/tag),
6271 * setting their status to the passed in status if the status has not already
6272 * been modified from CAM_REQ_INPROG. This routine assumes that the sequencer
6273 * is paused before it is called.
6276 ahc_abort_scbs(struct ahc_softc
*ahc
, int target
, char channel
,
6277 int lun
, u_int tag
, role_t role
, uint32_t status
)
6280 struct scb
*scbp_next
;
6290 * Don't attempt to run any queued untagged transactions
6291 * until we are done with the abort process.
6293 ahc_freeze_untagged_queues(ahc
);
6295 /* restore this when we're done */
6296 active_scb
= ahc_inb(ahc
, SCBPTR
);
6298 found
= ahc_search_qinfifo(ahc
, target
, channel
, lun
, SCB_LIST_NULL
,
6299 role
, CAM_REQUEUE_REQ
, SEARCH_COMPLETE
);
6302 * Clean out the busy target table for any untagged commands.
6306 if (target
!= CAM_TARGET_WILDCARD
) {
6313 if (lun
== CAM_LUN_WILDCARD
) {
6316 * Unless we are using an SCB based
6317 * busy targets table, there is only
6318 * one table entry for all luns of
6323 if ((ahc
->flags
& AHC_SCB_BTT
) != 0)
6324 maxlun
= AHC_NUM_LUNS
;
6330 if (role
!= ROLE_TARGET
) {
6331 for (;i
< maxtarget
; i
++) {
6332 for (j
= minlun
;j
< maxlun
; j
++) {
6336 tcl
= BUILD_TCL(i
<< 4, j
);
6337 scbid
= ahc_index_busy_tcl(ahc
, tcl
);
6338 scbp
= ahc_lookup_scb(ahc
, scbid
);
6340 || ahc_match_scb(ahc
, scbp
, target
, channel
,
6341 lun
, tag
, role
) == 0)
6343 ahc_unbusy_tcl(ahc
, BUILD_TCL(i
<< 4, j
));
6348 * Go through the disconnected list and remove any entries we
6349 * have queued for completion, 0'ing their control byte too.
6350 * We save the active SCB and restore it ourselves, so there
6351 * is no reason for this search to restore it too.
6353 ahc_search_disc_list(ahc
, target
, channel
, lun
, tag
,
6354 /*stop_on_first*/FALSE
, /*remove*/TRUE
,
6355 /*save_state*/FALSE
);
6359 * Go through the hardware SCB array looking for commands that
6360 * were active but not on any list. In some cases, these remnants
6361 * might not still have mappings in the scbindex array (e.g. unexpected
6362 * bus free with the same scb queued for an abort). Don't hold this
6365 for (i
= 0; i
< ahc
->scb_data
->maxhscbs
; i
++) {
6368 ahc_outb(ahc
, SCBPTR
, i
);
6369 scbid
= ahc_inb(ahc
, SCB_TAG
);
6370 scbp
= ahc_lookup_scb(ahc
, scbid
);
6371 if ((scbp
== NULL
&& scbid
!= SCB_LIST_NULL
)
6373 && ahc_match_scb(ahc
, scbp
, target
, channel
, lun
, tag
, role
)))
6374 ahc_add_curscb_to_free_list(ahc
);
6378 * Go through the pending CCB list and look for
6379 * commands for this target that are still active.
6380 * These are other tagged commands that were
6381 * disconnected when the reset occurred.
6383 scbp_next
= LIST_FIRST(&ahc
->pending_scbs
);
6384 while (scbp_next
!= NULL
) {
6386 scbp_next
= LIST_NEXT(scbp
, pending_links
);
6387 if (ahc_match_scb(ahc
, scbp
, target
, channel
, lun
, tag
, role
)) {
6390 ostat
= ahc_get_transaction_status(scbp
);
6391 if (ostat
== CAM_REQ_INPROG
)
6392 ahc_set_transaction_status(scbp
, status
);
6393 if (ahc_get_transaction_status(scbp
) != CAM_REQ_CMP
)
6394 ahc_freeze_scb(scbp
);
6395 if ((scbp
->flags
& SCB_ACTIVE
) == 0)
6396 printk("Inactive SCB on pending list\n");
6397 ahc_done(ahc
, scbp
);
6401 ahc_outb(ahc
, SCBPTR
, active_scb
);
6402 ahc_platform_abort_scbs(ahc
, target
, channel
, lun
, tag
, role
, status
);
6403 ahc_release_untagged_queues(ahc
);
6408 ahc_reset_current_bus(struct ahc_softc
*ahc
)
6412 ahc_outb(ahc
, SIMODE1
, ahc_inb(ahc
, SIMODE1
) & ~ENSCSIRST
);
6413 scsiseq
= ahc_inb(ahc
, SCSISEQ
);
6414 ahc_outb(ahc
, SCSISEQ
, scsiseq
| SCSIRSTO
);
6415 ahc_flush_device_writes(ahc
);
6416 ahc_delay(AHC_BUSRESET_DELAY
);
6417 /* Turn off the bus reset */
6418 ahc_outb(ahc
, SCSISEQ
, scsiseq
& ~SCSIRSTO
);
6420 ahc_clear_intstat(ahc
);
6422 /* Re-enable reset interrupts */
6423 ahc_outb(ahc
, SIMODE1
, ahc_inb(ahc
, SIMODE1
) | ENSCSIRST
);
6427 ahc_reset_channel(struct ahc_softc
*ahc
, char channel
, int initiate_reset
)
6429 struct ahc_devinfo devinfo
;
6430 u_int initiator
, target
, max_scsiid
;
6438 ahc
->pending_device
= NULL
;
6440 ahc_compile_devinfo(&devinfo
,
6441 CAM_TARGET_WILDCARD
,
6442 CAM_TARGET_WILDCARD
,
6444 channel
, ROLE_UNKNOWN
);
6447 /* Make sure the sequencer is in a safe location. */
6448 ahc_clear_critical_section(ahc
);
6451 * Run our command complete fifos to ensure that we perform
6452 * completion processing on any commands that 'completed'
6453 * before the reset occurred.
6455 ahc_run_qoutfifo(ahc
);
6456 #ifdef AHC_TARGET_MODE
6458 * XXX - In Twin mode, the tqinfifo may have commands
6459 * for an unaffected channel in it. However, if
6460 * we have run out of ATIO resources to drain that
6461 * queue, we may not get them all out here. Further,
6462 * the blocked transactions for the reset channel
6463 * should just be killed off, irrespecitve of whether
6464 * we are blocked on ATIO resources. Write a routine
6465 * to compact the tqinfifo appropriately.
6467 if ((ahc
->flags
& AHC_TARGETROLE
) != 0) {
6468 ahc_run_tqinfifo(ahc
, /*paused*/TRUE
);
6473 * Reset the bus if we are initiating this reset
6475 sblkctl
= ahc_inb(ahc
, SBLKCTL
);
6477 if ((ahc
->features
& AHC_TWIN
) != 0
6478 && ((sblkctl
& SELBUSB
) != 0))
6480 scsiseq
= ahc_inb(ahc
, SCSISEQ_TEMPLATE
);
6481 if (cur_channel
!= channel
) {
6482 /* Case 1: Command for another bus is active
6483 * Stealthily reset the other bus without
6484 * upsetting the current bus.
6486 ahc_outb(ahc
, SBLKCTL
, sblkctl
^ SELBUSB
);
6487 simode1
= ahc_inb(ahc
, SIMODE1
) & ~(ENBUSFREE
|ENSCSIRST
);
6488 #ifdef AHC_TARGET_MODE
6490 * Bus resets clear ENSELI, so we cannot
6491 * defer re-enabling bus reset interrupts
6492 * if we are in target mode.
6494 if ((ahc
->flags
& AHC_TARGETROLE
) != 0)
6495 simode1
|= ENSCSIRST
;
6497 ahc_outb(ahc
, SIMODE1
, simode1
);
6499 ahc_reset_current_bus(ahc
);
6500 ahc_clear_intstat(ahc
);
6501 ahc_outb(ahc
, SCSISEQ
, scsiseq
& (ENSELI
|ENRSELI
|ENAUTOATNP
));
6502 ahc_outb(ahc
, SBLKCTL
, sblkctl
);
6503 restart_needed
= FALSE
;
6505 /* Case 2: A command from this bus is active or we're idle */
6506 simode1
= ahc_inb(ahc
, SIMODE1
) & ~(ENBUSFREE
|ENSCSIRST
);
6507 #ifdef AHC_TARGET_MODE
6509 * Bus resets clear ENSELI, so we cannot
6510 * defer re-enabling bus reset interrupts
6511 * if we are in target mode.
6513 if ((ahc
->flags
& AHC_TARGETROLE
) != 0)
6514 simode1
|= ENSCSIRST
;
6516 ahc_outb(ahc
, SIMODE1
, simode1
);
6518 ahc_reset_current_bus(ahc
);
6519 ahc_clear_intstat(ahc
);
6520 ahc_outb(ahc
, SCSISEQ
, scsiseq
& (ENSELI
|ENRSELI
|ENAUTOATNP
));
6521 restart_needed
= TRUE
;
6525 * Clean up all the state information for the
6526 * pending transactions on this bus.
6528 found
= ahc_abort_scbs(ahc
, CAM_TARGET_WILDCARD
, channel
,
6529 CAM_LUN_WILDCARD
, SCB_LIST_NULL
,
6530 ROLE_UNKNOWN
, CAM_SCSI_BUS_RESET
);
6532 max_scsiid
= (ahc
->features
& AHC_WIDE
) ? 15 : 7;
6534 #ifdef AHC_TARGET_MODE
6536 * Send an immediate notify ccb to all target more peripheral
6537 * drivers affected by this action.
6539 for (target
= 0; target
<= max_scsiid
; target
++) {
6540 struct ahc_tmode_tstate
* tstate
;
6543 tstate
= ahc
->enabled_targets
[target
];
6546 for (lun
= 0; lun
< AHC_NUM_LUNS
; lun
++) {
6547 struct ahc_tmode_lstate
* lstate
;
6549 lstate
= tstate
->enabled_luns
[lun
];
6553 ahc_queue_lstate_event(ahc
, lstate
, CAM_TARGET_WILDCARD
,
6554 EVENT_TYPE_BUS_RESET
, /*arg*/0);
6555 ahc_send_lstate_events(ahc
, lstate
);
6559 /* Notify the XPT that a bus reset occurred */
6560 ahc_send_async(ahc
, devinfo
.channel
, CAM_TARGET_WILDCARD
,
6561 CAM_LUN_WILDCARD
, AC_BUS_RESET
);
6564 * Revert to async/narrow transfers until we renegotiate.
6566 for (target
= 0; target
<= max_scsiid
; target
++) {
6568 if (ahc
->enabled_targets
[target
] == NULL
)
6570 for (initiator
= 0; initiator
<= max_scsiid
; initiator
++) {
6571 struct ahc_devinfo devinfo
;
6573 ahc_compile_devinfo(&devinfo
, target
, initiator
,
6575 channel
, ROLE_UNKNOWN
);
6576 ahc_set_width(ahc
, &devinfo
, MSG_EXT_WDTR_BUS_8_BIT
,
6577 AHC_TRANS_CUR
, /*paused*/TRUE
);
6578 ahc_set_syncrate(ahc
, &devinfo
, /*syncrate*/NULL
,
6579 /*period*/0, /*offset*/0,
6580 /*ppr_options*/0, AHC_TRANS_CUR
,
6593 /***************************** Residual Processing ****************************/
6595 * Calculate the residual for a just completed SCB.
6598 ahc_calc_residual(struct ahc_softc
*ahc
, struct scb
*scb
)
6600 struct hardware_scb
*hscb
;
6601 struct status_pkt
*spkt
;
6603 uint32_t resid_sgptr
;
6609 * SG_RESID_VALID clear in sgptr.
6610 * 2) Transferless command
6611 * 3) Never performed any transfers.
6612 * sgptr has SG_FULL_RESID set.
6613 * 4) No residual but target did not
6614 * save data pointers after the
6615 * last transfer, so sgptr was
6617 * 5) We have a partial residual.
6618 * Use residual_sgptr to determine
6623 sgptr
= ahc_le32toh(hscb
->sgptr
);
6624 if ((sgptr
& SG_RESID_VALID
) == 0)
6627 sgptr
&= ~SG_RESID_VALID
;
6629 if ((sgptr
& SG_LIST_NULL
) != 0)
6633 spkt
= &hscb
->shared_data
.status
;
6634 resid_sgptr
= ahc_le32toh(spkt
->residual_sg_ptr
);
6635 if ((sgptr
& SG_FULL_RESID
) != 0) {
6637 resid
= ahc_get_transfer_length(scb
);
6638 } else if ((resid_sgptr
& SG_LIST_NULL
) != 0) {
6641 } else if ((resid_sgptr
& ~SG_PTR_MASK
) != 0) {
6642 panic("Bogus resid sgptr value 0x%x\n", resid_sgptr
);
6644 struct ahc_dma_seg
*sg
;
6647 * Remainder of the SG where the transfer
6650 resid
= ahc_le32toh(spkt
->residual_datacnt
) & AHC_SG_LEN_MASK
;
6651 sg
= ahc_sg_bus_to_virt(scb
, resid_sgptr
& SG_PTR_MASK
);
6653 /* The residual sg_ptr always points to the next sg */
6657 * Add up the contents of all residual
6658 * SG segments that are after the SG where
6659 * the transfer stopped.
6661 while ((ahc_le32toh(sg
->len
) & AHC_DMA_LAST_SEG
) == 0) {
6663 resid
+= ahc_le32toh(sg
->len
) & AHC_SG_LEN_MASK
;
6666 if ((scb
->flags
& SCB_SENSE
) == 0)
6667 ahc_set_residual(scb
, resid
);
6669 ahc_set_sense_residual(scb
, resid
);
6672 if ((ahc_debug
& AHC_SHOW_MISC
) != 0) {
6673 ahc_print_path(ahc
, scb
);
6674 printk("Handled %sResidual of %d bytes\n",
6675 (scb
->flags
& SCB_SENSE
) ? "Sense " : "", resid
);
6680 /******************************* Target Mode **********************************/
6681 #ifdef AHC_TARGET_MODE
6683 * Add a target mode event to this lun's queue
6686 ahc_queue_lstate_event(struct ahc_softc
*ahc
, struct ahc_tmode_lstate
*lstate
,
6687 u_int initiator_id
, u_int event_type
, u_int event_arg
)
6689 struct ahc_tmode_event
*event
;
6692 xpt_freeze_devq(lstate
->path
, /*count*/1);
6693 if (lstate
->event_w_idx
>= lstate
->event_r_idx
)
6694 pending
= lstate
->event_w_idx
- lstate
->event_r_idx
;
6696 pending
= AHC_TMODE_EVENT_BUFFER_SIZE
+ 1
6697 - (lstate
->event_r_idx
- lstate
->event_w_idx
);
6699 if (event_type
== EVENT_TYPE_BUS_RESET
6700 || event_type
== MSG_BUS_DEV_RESET
) {
6702 * Any earlier events are irrelevant, so reset our buffer.
6703 * This has the effect of allowing us to deal with reset
6704 * floods (an external device holding down the reset line)
6705 * without losing the event that is really interesting.
6707 lstate
->event_r_idx
= 0;
6708 lstate
->event_w_idx
= 0;
6709 xpt_release_devq(lstate
->path
, pending
, /*runqueue*/FALSE
);
6712 if (pending
== AHC_TMODE_EVENT_BUFFER_SIZE
) {
6713 xpt_print_path(lstate
->path
);
6714 printk("immediate event %x:%x lost\n",
6715 lstate
->event_buffer
[lstate
->event_r_idx
].event_type
,
6716 lstate
->event_buffer
[lstate
->event_r_idx
].event_arg
);
6717 lstate
->event_r_idx
++;
6718 if (lstate
->event_r_idx
== AHC_TMODE_EVENT_BUFFER_SIZE
)
6719 lstate
->event_r_idx
= 0;
6720 xpt_release_devq(lstate
->path
, /*count*/1, /*runqueue*/FALSE
);
6723 event
= &lstate
->event_buffer
[lstate
->event_w_idx
];
6724 event
->initiator_id
= initiator_id
;
6725 event
->event_type
= event_type
;
6726 event
->event_arg
= event_arg
;
6727 lstate
->event_w_idx
++;
6728 if (lstate
->event_w_idx
== AHC_TMODE_EVENT_BUFFER_SIZE
)
6729 lstate
->event_w_idx
= 0;
6733 * Send any target mode events queued up waiting
6734 * for immediate notify resources.
6737 ahc_send_lstate_events(struct ahc_softc
*ahc
, struct ahc_tmode_lstate
*lstate
)
6739 struct ccb_hdr
*ccbh
;
6740 struct ccb_immed_notify
*inot
;
6742 while (lstate
->event_r_idx
!= lstate
->event_w_idx
6743 && (ccbh
= SLIST_FIRST(&lstate
->immed_notifies
)) != NULL
) {
6744 struct ahc_tmode_event
*event
;
6746 event
= &lstate
->event_buffer
[lstate
->event_r_idx
];
6747 SLIST_REMOVE_HEAD(&lstate
->immed_notifies
, sim_links
.sle
);
6748 inot
= (struct ccb_immed_notify
*)ccbh
;
6749 switch (event
->event_type
) {
6750 case EVENT_TYPE_BUS_RESET
:
6751 ccbh
->status
= CAM_SCSI_BUS_RESET
|CAM_DEV_QFRZN
;
6754 ccbh
->status
= CAM_MESSAGE_RECV
|CAM_DEV_QFRZN
;
6755 inot
->message_args
[0] = event
->event_type
;
6756 inot
->message_args
[1] = event
->event_arg
;
6759 inot
->initiator_id
= event
->initiator_id
;
6760 inot
->sense_len
= 0;
6761 xpt_done((union ccb
*)inot
);
6762 lstate
->event_r_idx
++;
6763 if (lstate
->event_r_idx
== AHC_TMODE_EVENT_BUFFER_SIZE
)
6764 lstate
->event_r_idx
= 0;
6769 /******************** Sequencer Program Patching/Download *********************/
6773 ahc_dumpseq(struct ahc_softc
* ahc
)
6777 ahc_outb(ahc
, SEQCTL
, PERRORDIS
|FAILDIS
|FASTMODE
|LOADRAM
);
6778 ahc_outb(ahc
, SEQADDR0
, 0);
6779 ahc_outb(ahc
, SEQADDR1
, 0);
6780 for (i
= 0; i
< ahc
->instruction_ram_size
; i
++) {
6781 uint8_t ins_bytes
[4];
6783 ahc_insb(ahc
, SEQRAM
, ins_bytes
, 4);
6784 printk("0x%08x\n", ins_bytes
[0] << 24
6785 | ins_bytes
[1] << 16
6793 ahc_loadseq(struct ahc_softc
*ahc
)
6795 struct cs cs_table
[NUM_CRITICAL_SECTIONS
];
6796 u_int begin_set
[NUM_CRITICAL_SECTIONS
];
6797 u_int end_set
[NUM_CRITICAL_SECTIONS
];
6798 const struct patch
*cur_patch
;
6803 u_int sg_prefetch_cnt
;
6805 uint8_t download_consts
[7];
6808 * Start out with 0 critical sections
6809 * that apply to this firmware load.
6813 memset(begin_set
, 0, sizeof(begin_set
));
6814 memset(end_set
, 0, sizeof(end_set
));
6816 /* Setup downloadable constant table */
6817 download_consts
[QOUTFIFO_OFFSET
] = 0;
6818 if (ahc
->targetcmds
!= NULL
)
6819 download_consts
[QOUTFIFO_OFFSET
] += 32;
6820 download_consts
[QINFIFO_OFFSET
] = download_consts
[QOUTFIFO_OFFSET
] + 1;
6821 download_consts
[CACHESIZE_MASK
] = ahc
->pci_cachesize
- 1;
6822 download_consts
[INVERTED_CACHESIZE_MASK
] = ~(ahc
->pci_cachesize
- 1);
6823 sg_prefetch_cnt
= ahc
->pci_cachesize
;
6824 if (sg_prefetch_cnt
< (2 * sizeof(struct ahc_dma_seg
)))
6825 sg_prefetch_cnt
= 2 * sizeof(struct ahc_dma_seg
);
6826 download_consts
[SG_PREFETCH_CNT
] = sg_prefetch_cnt
;
6827 download_consts
[SG_PREFETCH_ALIGN_MASK
] = ~(sg_prefetch_cnt
- 1);
6828 download_consts
[SG_PREFETCH_ADDR_MASK
] = (sg_prefetch_cnt
- 1);
6830 cur_patch
= patches
;
6833 ahc_outb(ahc
, SEQCTL
, PERRORDIS
|FAILDIS
|FASTMODE
|LOADRAM
);
6834 ahc_outb(ahc
, SEQADDR0
, 0);
6835 ahc_outb(ahc
, SEQADDR1
, 0);
6837 for (i
= 0; i
< sizeof(seqprog
)/4; i
++) {
6838 if (ahc_check_patch(ahc
, &cur_patch
, i
, &skip_addr
) == 0) {
6840 * Don't download this instruction as it
6841 * is in a patch that was removed.
6846 if (downloaded
== ahc
->instruction_ram_size
) {
6848 * We're about to exceed the instruction
6849 * storage capacity for this chip. Fail
6852 printk("\n%s: Program too large for instruction memory "
6853 "size of %d!\n", ahc_name(ahc
),
6854 ahc
->instruction_ram_size
);
6859 * Move through the CS table until we find a CS
6860 * that might apply to this instruction.
6862 for (; cur_cs
< NUM_CRITICAL_SECTIONS
; cur_cs
++) {
6863 if (critical_sections
[cur_cs
].end
<= i
) {
6864 if (begin_set
[cs_count
] == TRUE
6865 && end_set
[cs_count
] == FALSE
) {
6866 cs_table
[cs_count
].end
= downloaded
;
6867 end_set
[cs_count
] = TRUE
;
6872 if (critical_sections
[cur_cs
].begin
<= i
6873 && begin_set
[cs_count
] == FALSE
) {
6874 cs_table
[cs_count
].begin
= downloaded
;
6875 begin_set
[cs_count
] = TRUE
;
6879 ahc_download_instr(ahc
, i
, download_consts
);
6883 ahc
->num_critical_sections
= cs_count
;
6884 if (cs_count
!= 0) {
6886 cs_count
*= sizeof(struct cs
);
6887 ahc
->critical_sections
= kmalloc(cs_count
, GFP_ATOMIC
);
6888 if (ahc
->critical_sections
== NULL
)
6889 panic("ahc_loadseq: Could not malloc");
6890 memcpy(ahc
->critical_sections
, cs_table
, cs_count
);
6892 ahc_outb(ahc
, SEQCTL
, PERRORDIS
|FAILDIS
|FASTMODE
);
6895 printk(" %d instructions downloaded\n", downloaded
);
6896 printk("%s: Features 0x%x, Bugs 0x%x, Flags 0x%x\n",
6897 ahc_name(ahc
), ahc
->features
, ahc
->bugs
, ahc
->flags
);
6903 ahc_check_patch(struct ahc_softc
*ahc
, const struct patch
**start_patch
,
6904 u_int start_instr
, u_int
*skip_addr
)
6906 const struct patch
*cur_patch
;
6907 const struct patch
*last_patch
;
6910 num_patches
= ARRAY_SIZE(patches
);
6911 last_patch
= &patches
[num_patches
];
6912 cur_patch
= *start_patch
;
6914 while (cur_patch
< last_patch
&& start_instr
== cur_patch
->begin
) {
6916 if (cur_patch
->patch_func(ahc
) == 0) {
6918 /* Start rejecting code */
6919 *skip_addr
= start_instr
+ cur_patch
->skip_instr
;
6920 cur_patch
+= cur_patch
->skip_patch
;
6922 /* Accepted this patch. Advance to the next
6923 * one and wait for our intruction pointer to
6930 *start_patch
= cur_patch
;
6931 if (start_instr
< *skip_addr
)
6932 /* Still skipping */
6939 ahc_download_instr(struct ahc_softc
*ahc
, u_int instrptr
, uint8_t *dconsts
)
6941 union ins_formats instr
;
6942 struct ins_format1
*fmt1_ins
;
6943 struct ins_format3
*fmt3_ins
;
6947 * The firmware is always compiled into a little endian format.
6949 instr
.integer
= ahc_le32toh(*(uint32_t*)&seqprog
[instrptr
* 4]);
6951 fmt1_ins
= &instr
.format1
;
6954 /* Pull the opcode */
6955 opcode
= instr
.format1
.opcode
;
6966 const struct patch
*cur_patch
;
6972 fmt3_ins
= &instr
.format3
;
6974 address
= fmt3_ins
->address
;
6975 cur_patch
= patches
;
6978 for (i
= 0; i
< address
;) {
6980 ahc_check_patch(ahc
, &cur_patch
, i
, &skip_addr
);
6982 if (skip_addr
> i
) {
6985 end_addr
= min(address
, skip_addr
);
6986 address_offset
+= end_addr
- i
;
6992 address
-= address_offset
;
6993 fmt3_ins
->address
= address
;
7002 if (fmt1_ins
->parity
!= 0) {
7003 fmt1_ins
->immediate
= dconsts
[fmt1_ins
->immediate
];
7005 fmt1_ins
->parity
= 0;
7006 if ((ahc
->features
& AHC_CMD_CHAN
) == 0
7007 && opcode
== AIC_OP_BMOV
) {
7009 * Block move was added at the same time
7010 * as the command channel. Verify that
7011 * this is only a move of a single element
7012 * and convert the BMOV to a MOV
7013 * (AND with an immediate of FF).
7015 if (fmt1_ins
->immediate
!= 1)
7016 panic("%s: BMOV not supported\n",
7018 fmt1_ins
->opcode
= AIC_OP_AND
;
7019 fmt1_ins
->immediate
= 0xff;
7023 if ((ahc
->features
& AHC_ULTRA2
) != 0) {
7026 /* Calculate odd parity for the instruction */
7027 for (i
= 0, count
= 0; i
< 31; i
++) {
7031 if ((instr
.integer
& mask
) != 0)
7034 if ((count
& 0x01) == 0)
7035 instr
.format1
.parity
= 1;
7037 /* Compress the instruction for older sequencers */
7038 if (fmt3_ins
!= NULL
) {
7041 | (fmt3_ins
->source
<< 8)
7042 | (fmt3_ins
->address
<< 16)
7043 | (fmt3_ins
->opcode
<< 25);
7047 | (fmt1_ins
->source
<< 8)
7048 | (fmt1_ins
->destination
<< 16)
7049 | (fmt1_ins
->ret
<< 24)
7050 | (fmt1_ins
->opcode
<< 25);
7053 /* The sequencer is a little endian cpu */
7054 instr
.integer
= ahc_htole32(instr
.integer
);
7055 ahc_outsb(ahc
, SEQRAM
, instr
.bytes
, 4);
7058 panic("Unknown opcode encountered in seq program");
7064 ahc_print_register(const ahc_reg_parse_entry_t
*table
, u_int num_entries
,
7065 const char *name
, u_int address
, u_int value
,
7066 u_int
*cur_column
, u_int wrap_point
)
7071 if (cur_column
!= NULL
&& *cur_column
>= wrap_point
) {
7075 printed
= printk("%s[0x%x]", name
, value
);
7076 if (table
== NULL
) {
7077 printed
+= printk(" ");
7078 *cur_column
+= printed
;
7082 while (printed_mask
!= 0xFF) {
7085 for (entry
= 0; entry
< num_entries
; entry
++) {
7086 if (((value
& table
[entry
].mask
)
7087 != table
[entry
].value
)
7088 || ((printed_mask
& table
[entry
].mask
)
7089 == table
[entry
].mask
))
7092 printed
+= printk("%s%s",
7093 printed_mask
== 0 ? ":(" : "|",
7095 printed_mask
|= table
[entry
].mask
;
7099 if (entry
>= num_entries
)
7102 if (printed_mask
!= 0)
7103 printed
+= printk(") ");
7105 printed
+= printk(" ");
7106 if (cur_column
!= NULL
)
7107 *cur_column
+= printed
;
7112 ahc_dump_card_state(struct ahc_softc
*ahc
)
7115 struct scb_tailq
*untagged_q
;
7126 uint8_t saved_scbptr
;
7128 if (ahc_is_paused(ahc
)) {
7135 saved_scbptr
= ahc_inb(ahc
, SCBPTR
);
7136 last_phase
= ahc_inb(ahc
, LASTPHASE
);
7137 printk(">>>>>>>>>>>>>>>>>> Dump Card State Begins <<<<<<<<<<<<<<<<<\n"
7138 "%s: Dumping Card State %s, at SEQADDR 0x%x\n",
7139 ahc_name(ahc
), ahc_lookup_phase_entry(last_phase
)->phasemsg
,
7140 ahc_inb(ahc
, SEQADDR0
) | (ahc_inb(ahc
, SEQADDR1
) << 8));
7142 printk("Card was paused\n");
7143 printk("ACCUM = 0x%x, SINDEX = 0x%x, DINDEX = 0x%x, ARG_2 = 0x%x\n",
7144 ahc_inb(ahc
, ACCUM
), ahc_inb(ahc
, SINDEX
), ahc_inb(ahc
, DINDEX
),
7145 ahc_inb(ahc
, ARG_2
));
7146 printk("HCNT = 0x%x SCBPTR = 0x%x\n", ahc_inb(ahc
, HCNT
),
7147 ahc_inb(ahc
, SCBPTR
));
7149 if ((ahc
->features
& AHC_DT
) != 0)
7150 ahc_scsiphase_print(ahc_inb(ahc
, SCSIPHASE
), &cur_col
, 50);
7151 ahc_scsisigi_print(ahc_inb(ahc
, SCSISIGI
), &cur_col
, 50);
7152 ahc_error_print(ahc_inb(ahc
, ERROR
), &cur_col
, 50);
7153 ahc_scsibusl_print(ahc_inb(ahc
, SCSIBUSL
), &cur_col
, 50);
7154 ahc_lastphase_print(ahc_inb(ahc
, LASTPHASE
), &cur_col
, 50);
7155 ahc_scsiseq_print(ahc_inb(ahc
, SCSISEQ
), &cur_col
, 50);
7156 ahc_sblkctl_print(ahc_inb(ahc
, SBLKCTL
), &cur_col
, 50);
7157 ahc_scsirate_print(ahc_inb(ahc
, SCSIRATE
), &cur_col
, 50);
7158 ahc_seqctl_print(ahc_inb(ahc
, SEQCTL
), &cur_col
, 50);
7159 ahc_seq_flags_print(ahc_inb(ahc
, SEQ_FLAGS
), &cur_col
, 50);
7160 ahc_sstat0_print(ahc_inb(ahc
, SSTAT0
), &cur_col
, 50);
7161 ahc_sstat1_print(ahc_inb(ahc
, SSTAT1
), &cur_col
, 50);
7162 ahc_sstat2_print(ahc_inb(ahc
, SSTAT2
), &cur_col
, 50);
7163 ahc_sstat3_print(ahc_inb(ahc
, SSTAT3
), &cur_col
, 50);
7164 ahc_simode0_print(ahc_inb(ahc
, SIMODE0
), &cur_col
, 50);
7165 ahc_simode1_print(ahc_inb(ahc
, SIMODE1
), &cur_col
, 50);
7166 ahc_sxfrctl0_print(ahc_inb(ahc
, SXFRCTL0
), &cur_col
, 50);
7167 ahc_dfcntrl_print(ahc_inb(ahc
, DFCNTRL
), &cur_col
, 50);
7168 ahc_dfstatus_print(ahc_inb(ahc
, DFSTATUS
), &cur_col
, 50);
7172 for (i
= 0; i
< STACK_SIZE
; i
++)
7173 printk(" 0x%x", ahc_inb(ahc
, STACK
)|(ahc_inb(ahc
, STACK
) << 8));
7174 printk("\nSCB count = %d\n", ahc
->scb_data
->numscbs
);
7175 printk("Kernel NEXTQSCB = %d\n", ahc
->next_queued_scb
->hscb
->tag
);
7176 printk("Card NEXTQSCB = %d\n", ahc_inb(ahc
, NEXT_QUEUED_SCB
));
7178 printk("QINFIFO entries: ");
7179 if ((ahc
->features
& AHC_QUEUE_REGS
) != 0) {
7180 qinpos
= ahc_inb(ahc
, SNSCB_QOFF
);
7181 ahc_outb(ahc
, SNSCB_QOFF
, qinpos
);
7183 qinpos
= ahc_inb(ahc
, QINPOS
);
7184 qintail
= ahc
->qinfifonext
;
7185 while (qinpos
!= qintail
) {
7186 printk("%d ", ahc
->qinfifo
[qinpos
]);
7191 printk("Waiting Queue entries: ");
7192 scb_index
= ahc_inb(ahc
, WAITING_SCBH
);
7194 while (scb_index
!= SCB_LIST_NULL
&& i
++ < 256) {
7195 ahc_outb(ahc
, SCBPTR
, scb_index
);
7196 printk("%d:%d ", scb_index
, ahc_inb(ahc
, SCB_TAG
));
7197 scb_index
= ahc_inb(ahc
, SCB_NEXT
);
7201 printk("Disconnected Queue entries: ");
7202 scb_index
= ahc_inb(ahc
, DISCONNECTED_SCBH
);
7204 while (scb_index
!= SCB_LIST_NULL
&& i
++ < 256) {
7205 ahc_outb(ahc
, SCBPTR
, scb_index
);
7206 printk("%d:%d ", scb_index
, ahc_inb(ahc
, SCB_TAG
));
7207 scb_index
= ahc_inb(ahc
, SCB_NEXT
);
7211 ahc_sync_qoutfifo(ahc
, BUS_DMASYNC_POSTREAD
);
7212 printk("QOUTFIFO entries: ");
7213 qoutpos
= ahc
->qoutfifonext
;
7215 while (ahc
->qoutfifo
[qoutpos
] != SCB_LIST_NULL
&& i
++ < 256) {
7216 printk("%d ", ahc
->qoutfifo
[qoutpos
]);
7221 printk("Sequencer Free SCB List: ");
7222 scb_index
= ahc_inb(ahc
, FREE_SCBH
);
7224 while (scb_index
!= SCB_LIST_NULL
&& i
++ < 256) {
7225 ahc_outb(ahc
, SCBPTR
, scb_index
);
7226 printk("%d ", scb_index
);
7227 scb_index
= ahc_inb(ahc
, SCB_NEXT
);
7231 printk("Sequencer SCB Info: ");
7232 for (i
= 0; i
< ahc
->scb_data
->maxhscbs
; i
++) {
7233 ahc_outb(ahc
, SCBPTR
, i
);
7234 cur_col
= printk("\n%3d ", i
);
7236 ahc_scb_control_print(ahc_inb(ahc
, SCB_CONTROL
), &cur_col
, 60);
7237 ahc_scb_scsiid_print(ahc_inb(ahc
, SCB_SCSIID
), &cur_col
, 60);
7238 ahc_scb_lun_print(ahc_inb(ahc
, SCB_LUN
), &cur_col
, 60);
7239 ahc_scb_tag_print(ahc_inb(ahc
, SCB_TAG
), &cur_col
, 60);
7243 printk("Pending list: ");
7245 LIST_FOREACH(scb
, &ahc
->pending_scbs
, pending_links
) {
7248 cur_col
= printk("\n%3d ", scb
->hscb
->tag
);
7249 ahc_scb_control_print(scb
->hscb
->control
, &cur_col
, 60);
7250 ahc_scb_scsiid_print(scb
->hscb
->scsiid
, &cur_col
, 60);
7251 ahc_scb_lun_print(scb
->hscb
->lun
, &cur_col
, 60);
7252 if ((ahc
->flags
& AHC_PAGESCBS
) == 0) {
7253 ahc_outb(ahc
, SCBPTR
, scb
->hscb
->tag
);
7255 ahc_scb_control_print(ahc_inb(ahc
, SCB_CONTROL
),
7257 ahc_scb_tag_print(ahc_inb(ahc
, SCB_TAG
), &cur_col
, 60);
7263 printk("Kernel Free SCB list: ");
7265 SLIST_FOREACH(scb
, &ahc
->scb_data
->free_scbs
, links
.sle
) {
7268 printk("%d ", scb
->hscb
->tag
);
7272 maxtarget
= (ahc
->features
& (AHC_WIDE
|AHC_TWIN
)) ? 15 : 7;
7273 for (target
= 0; target
<= maxtarget
; target
++) {
7274 untagged_q
= &ahc
->untagged_queues
[target
];
7275 if (TAILQ_FIRST(untagged_q
) == NULL
)
7277 printk("Untagged Q(%d): ", target
);
7279 TAILQ_FOREACH(scb
, untagged_q
, links
.tqe
) {
7282 printk("%d ", scb
->hscb
->tag
);
7287 printk("\n<<<<<<<<<<<<<<<<< Dump Card State Ends >>>>>>>>>>>>>>>>>>\n");
7288 ahc_outb(ahc
, SCBPTR
, saved_scbptr
);
7293 /************************* Target Mode ****************************************/
7294 #ifdef AHC_TARGET_MODE
7296 ahc_find_tmode_devs(struct ahc_softc
*ahc
, struct cam_sim
*sim
, union ccb
*ccb
,
7297 struct ahc_tmode_tstate
**tstate
,
7298 struct ahc_tmode_lstate
**lstate
,
7299 int notfound_failure
)
7302 if ((ahc
->features
& AHC_TARGETMODE
) == 0)
7303 return (CAM_REQ_INVALID
);
7306 * Handle the 'black hole' device that sucks up
7307 * requests to unattached luns on enabled targets.
7309 if (ccb
->ccb_h
.target_id
== CAM_TARGET_WILDCARD
7310 && ccb
->ccb_h
.target_lun
== CAM_LUN_WILDCARD
) {
7312 *lstate
= ahc
->black_hole
;
7316 max_id
= (ahc
->features
& AHC_WIDE
) ? 16 : 8;
7317 if (ccb
->ccb_h
.target_id
>= max_id
)
7318 return (CAM_TID_INVALID
);
7320 if (ccb
->ccb_h
.target_lun
>= AHC_NUM_LUNS
)
7321 return (CAM_LUN_INVALID
);
7323 *tstate
= ahc
->enabled_targets
[ccb
->ccb_h
.target_id
];
7325 if (*tstate
!= NULL
)
7327 (*tstate
)->enabled_luns
[ccb
->ccb_h
.target_lun
];
7330 if (notfound_failure
!= 0 && *lstate
== NULL
)
7331 return (CAM_PATH_INVALID
);
7333 return (CAM_REQ_CMP
);
7337 ahc_handle_en_lun(struct ahc_softc
*ahc
, struct cam_sim
*sim
, union ccb
*ccb
)
7339 struct ahc_tmode_tstate
*tstate
;
7340 struct ahc_tmode_lstate
*lstate
;
7341 struct ccb_en_lun
*cel
;
7351 status
= ahc_find_tmode_devs(ahc
, sim
, ccb
, &tstate
, &lstate
,
7352 /*notfound_failure*/FALSE
);
7354 if (status
!= CAM_REQ_CMP
) {
7355 ccb
->ccb_h
.status
= status
;
7359 if (cam_sim_bus(sim
) == 0)
7360 our_id
= ahc
->our_id
;
7362 our_id
= ahc
->our_id_b
;
7364 if (ccb
->ccb_h
.target_id
!= our_id
) {
7366 * our_id represents our initiator ID, or
7367 * the ID of the first target to have an
7368 * enabled lun in target mode. There are
7369 * two cases that may preclude enabling a
7370 * target id other than our_id.
7372 * o our_id is for an active initiator role.
7373 * Since the hardware does not support
7374 * reselections to the initiator role at
7375 * anything other than our_id, and our_id
7376 * is used by the hardware to indicate the
7377 * ID to use for both select-out and
7378 * reselect-out operations, the only target
7379 * ID we can support in this mode is our_id.
7381 * o The MULTARGID feature is not available and
7382 * a previous target mode ID has been enabled.
7384 if ((ahc
->features
& AHC_MULTIROLE
) != 0) {
7386 if ((ahc
->features
& AHC_MULTI_TID
) != 0
7387 && (ahc
->flags
& AHC_INITIATORROLE
) != 0) {
7389 * Only allow additional targets if
7390 * the initiator role is disabled.
7391 * The hardware cannot handle a re-select-in
7392 * on the initiator id during a re-select-out
7393 * on a different target id.
7395 status
= CAM_TID_INVALID
;
7396 } else if ((ahc
->flags
& AHC_INITIATORROLE
) != 0
7397 || ahc
->enabled_luns
> 0) {
7399 * Only allow our target id to change
7400 * if the initiator role is not configured
7401 * and there are no enabled luns which
7402 * are attached to the currently registered
7405 status
= CAM_TID_INVALID
;
7407 } else if ((ahc
->features
& AHC_MULTI_TID
) == 0
7408 && ahc
->enabled_luns
> 0) {
7410 status
= CAM_TID_INVALID
;
7414 if (status
!= CAM_REQ_CMP
) {
7415 ccb
->ccb_h
.status
= status
;
7420 * We now have an id that is valid.
7421 * If we aren't in target mode, switch modes.
7423 if ((ahc
->flags
& AHC_TARGETROLE
) == 0
7424 && ccb
->ccb_h
.target_id
!= CAM_TARGET_WILDCARD
) {
7426 ahc_flag saved_flags
;
7428 printk("Configuring Target Mode\n");
7430 if (LIST_FIRST(&ahc
->pending_scbs
) != NULL
) {
7431 ccb
->ccb_h
.status
= CAM_BUSY
;
7432 ahc_unlock(ahc
, &s
);
7435 saved_flags
= ahc
->flags
;
7436 ahc
->flags
|= AHC_TARGETROLE
;
7437 if ((ahc
->features
& AHC_MULTIROLE
) == 0)
7438 ahc
->flags
&= ~AHC_INITIATORROLE
;
7440 error
= ahc_loadseq(ahc
);
7443 * Restore original configuration and notify
7444 * the caller that we cannot support target mode.
7445 * Since the adapter started out in this
7446 * configuration, the firmware load will succeed,
7447 * so there is no point in checking ahc_loadseq's
7450 ahc
->flags
= saved_flags
;
7451 (void)ahc_loadseq(ahc
);
7453 ahc_unlock(ahc
, &s
);
7454 ccb
->ccb_h
.status
= CAM_FUNC_NOTAVAIL
;
7458 ahc_unlock(ahc
, &s
);
7461 target
= ccb
->ccb_h
.target_id
;
7462 lun
= ccb
->ccb_h
.target_lun
;
7463 channel
= SIM_CHANNEL(ahc
, sim
);
7464 target_mask
= 0x01 << target
;
7468 if (cel
->enable
!= 0) {
7471 /* Are we already enabled?? */
7472 if (lstate
!= NULL
) {
7473 xpt_print_path(ccb
->ccb_h
.path
);
7474 printk("Lun already enabled\n");
7475 ccb
->ccb_h
.status
= CAM_LUN_ALRDY_ENA
;
7479 if (cel
->grp6_len
!= 0
7480 || cel
->grp7_len
!= 0) {
7482 * Don't (yet?) support vendor
7483 * specific commands.
7485 ccb
->ccb_h
.status
= CAM_REQ_INVALID
;
7486 printk("Non-zero Group Codes\n");
7492 * Setup our data structures.
7494 if (target
!= CAM_TARGET_WILDCARD
&& tstate
== NULL
) {
7495 tstate
= ahc_alloc_tstate(ahc
, target
, channel
);
7496 if (tstate
== NULL
) {
7497 xpt_print_path(ccb
->ccb_h
.path
);
7498 printk("Couldn't allocate tstate\n");
7499 ccb
->ccb_h
.status
= CAM_RESRC_UNAVAIL
;
7503 lstate
= kzalloc(sizeof(*lstate
), GFP_ATOMIC
);
7504 if (lstate
== NULL
) {
7505 xpt_print_path(ccb
->ccb_h
.path
);
7506 printk("Couldn't allocate lstate\n");
7507 ccb
->ccb_h
.status
= CAM_RESRC_UNAVAIL
;
7510 status
= xpt_create_path(&lstate
->path
, /*periph*/NULL
,
7511 xpt_path_path_id(ccb
->ccb_h
.path
),
7512 xpt_path_target_id(ccb
->ccb_h
.path
),
7513 xpt_path_lun_id(ccb
->ccb_h
.path
));
7514 if (status
!= CAM_REQ_CMP
) {
7516 xpt_print_path(ccb
->ccb_h
.path
);
7517 printk("Couldn't allocate path\n");
7518 ccb
->ccb_h
.status
= CAM_RESRC_UNAVAIL
;
7521 SLIST_INIT(&lstate
->accept_tios
);
7522 SLIST_INIT(&lstate
->immed_notifies
);
7525 if (target
!= CAM_TARGET_WILDCARD
) {
7526 tstate
->enabled_luns
[lun
] = lstate
;
7527 ahc
->enabled_luns
++;
7529 if ((ahc
->features
& AHC_MULTI_TID
) != 0) {
7532 targid_mask
= ahc_inb(ahc
, TARGID
)
7533 | (ahc_inb(ahc
, TARGID
+ 1) << 8);
7535 targid_mask
|= target_mask
;
7536 ahc_outb(ahc
, TARGID
, targid_mask
);
7537 ahc_outb(ahc
, TARGID
+1, (targid_mask
>> 8));
7539 ahc_update_scsiid(ahc
, targid_mask
);
7544 channel
= SIM_CHANNEL(ahc
, sim
);
7545 our_id
= SIM_SCSI_ID(ahc
, sim
);
7548 * This can only happen if selections
7551 if (target
!= our_id
) {
7556 sblkctl
= ahc_inb(ahc
, SBLKCTL
);
7557 cur_channel
= (sblkctl
& SELBUSB
)
7559 if ((ahc
->features
& AHC_TWIN
) == 0)
7561 swap
= cur_channel
!= channel
;
7563 ahc
->our_id
= target
;
7565 ahc
->our_id_b
= target
;
7568 ahc_outb(ahc
, SBLKCTL
,
7571 ahc_outb(ahc
, SCSIID
, target
);
7574 ahc_outb(ahc
, SBLKCTL
, sblkctl
);
7578 ahc
->black_hole
= lstate
;
7579 /* Allow select-in operations */
7580 if (ahc
->black_hole
!= NULL
&& ahc
->enabled_luns
> 0) {
7581 scsiseq
= ahc_inb(ahc
, SCSISEQ_TEMPLATE
);
7583 ahc_outb(ahc
, SCSISEQ_TEMPLATE
, scsiseq
);
7584 scsiseq
= ahc_inb(ahc
, SCSISEQ
);
7586 ahc_outb(ahc
, SCSISEQ
, scsiseq
);
7589 ahc_unlock(ahc
, &s
);
7590 ccb
->ccb_h
.status
= CAM_REQ_CMP
;
7591 xpt_print_path(ccb
->ccb_h
.path
);
7592 printk("Lun now enabled for target mode\n");
7597 if (lstate
== NULL
) {
7598 ccb
->ccb_h
.status
= CAM_LUN_INVALID
;
7604 ccb
->ccb_h
.status
= CAM_REQ_CMP
;
7605 LIST_FOREACH(scb
, &ahc
->pending_scbs
, pending_links
) {
7606 struct ccb_hdr
*ccbh
;
7608 ccbh
= &scb
->io_ctx
->ccb_h
;
7609 if (ccbh
->func_code
== XPT_CONT_TARGET_IO
7610 && !xpt_path_comp(ccbh
->path
, ccb
->ccb_h
.path
)){
7611 printk("CTIO pending\n");
7612 ccb
->ccb_h
.status
= CAM_REQ_INVALID
;
7613 ahc_unlock(ahc
, &s
);
7618 if (SLIST_FIRST(&lstate
->accept_tios
) != NULL
) {
7619 printk("ATIOs pending\n");
7620 ccb
->ccb_h
.status
= CAM_REQ_INVALID
;
7623 if (SLIST_FIRST(&lstate
->immed_notifies
) != NULL
) {
7624 printk("INOTs pending\n");
7625 ccb
->ccb_h
.status
= CAM_REQ_INVALID
;
7628 if (ccb
->ccb_h
.status
!= CAM_REQ_CMP
) {
7629 ahc_unlock(ahc
, &s
);
7633 xpt_print_path(ccb
->ccb_h
.path
);
7634 printk("Target mode disabled\n");
7635 xpt_free_path(lstate
->path
);
7639 /* Can we clean up the target too? */
7640 if (target
!= CAM_TARGET_WILDCARD
) {
7641 tstate
->enabled_luns
[lun
] = NULL
;
7642 ahc
->enabled_luns
--;
7643 for (empty
= 1, i
= 0; i
< 8; i
++)
7644 if (tstate
->enabled_luns
[i
] != NULL
) {
7650 ahc_free_tstate(ahc
, target
, channel
,
7652 if (ahc
->features
& AHC_MULTI_TID
) {
7655 targid_mask
= ahc_inb(ahc
, TARGID
)
7656 | (ahc_inb(ahc
, TARGID
+ 1)
7659 targid_mask
&= ~target_mask
;
7660 ahc_outb(ahc
, TARGID
, targid_mask
);
7661 ahc_outb(ahc
, TARGID
+1,
7662 (targid_mask
>> 8));
7663 ahc_update_scsiid(ahc
, targid_mask
);
7668 ahc
->black_hole
= NULL
;
7671 * We can't allow selections without
7672 * our black hole device.
7676 if (ahc
->enabled_luns
== 0) {
7677 /* Disallow select-in */
7680 scsiseq
= ahc_inb(ahc
, SCSISEQ_TEMPLATE
);
7682 ahc_outb(ahc
, SCSISEQ_TEMPLATE
, scsiseq
);
7683 scsiseq
= ahc_inb(ahc
, SCSISEQ
);
7685 ahc_outb(ahc
, SCSISEQ
, scsiseq
);
7687 if ((ahc
->features
& AHC_MULTIROLE
) == 0) {
7688 printk("Configuring Initiator Mode\n");
7689 ahc
->flags
&= ~AHC_TARGETROLE
;
7690 ahc
->flags
|= AHC_INITIATORROLE
;
7692 * Returning to a configuration that
7693 * fit previously will always succeed.
7695 (void)ahc_loadseq(ahc
);
7698 * Unpaused. The extra unpause
7699 * that follows is harmless.
7704 ahc_unlock(ahc
, &s
);
7709 ahc_update_scsiid(struct ahc_softc
*ahc
, u_int targid_mask
)
7714 if ((ahc
->features
& AHC_MULTI_TID
) == 0)
7715 panic("ahc_update_scsiid called on non-multitid unit\n");
7718 * Since we will rely on the TARGID mask
7719 * for selection enables, ensure that OID
7720 * in SCSIID is not set to some other ID
7721 * that we don't want to allow selections on.
7723 if ((ahc
->features
& AHC_ULTRA2
) != 0)
7724 scsiid
= ahc_inb(ahc
, SCSIID_ULTRA2
);
7726 scsiid
= ahc_inb(ahc
, SCSIID
);
7727 scsiid_mask
= 0x1 << (scsiid
& OID
);
7728 if ((targid_mask
& scsiid_mask
) == 0) {
7731 /* ffs counts from 1 */
7732 our_id
= ffs(targid_mask
);
7734 our_id
= ahc
->our_id
;
7740 if ((ahc
->features
& AHC_ULTRA2
) != 0)
7741 ahc_outb(ahc
, SCSIID_ULTRA2
, scsiid
);
7743 ahc_outb(ahc
, SCSIID
, scsiid
);
7747 ahc_run_tqinfifo(struct ahc_softc
*ahc
, int paused
)
7749 struct target_cmd
*cmd
;
7752 * If the card supports auto-access pause,
7753 * we can access the card directly regardless
7754 * of whether it is paused or not.
7756 if ((ahc
->features
& AHC_AUTOPAUSE
) != 0)
7759 ahc_sync_tqinfifo(ahc
, BUS_DMASYNC_POSTREAD
);
7760 while ((cmd
= &ahc
->targetcmds
[ahc
->tqinfifonext
])->cmd_valid
!= 0) {
7763 * Only advance through the queue if we
7764 * have the resources to process the command.
7766 if (ahc_handle_target_cmd(ahc
, cmd
) != 0)
7770 ahc_dmamap_sync(ahc
, ahc
->shared_data_dmat
,
7771 ahc
->shared_data_dmamap
,
7772 ahc_targetcmd_offset(ahc
, ahc
->tqinfifonext
),
7773 sizeof(struct target_cmd
),
7774 BUS_DMASYNC_PREREAD
);
7775 ahc
->tqinfifonext
++;
7778 * Lazily update our position in the target mode incoming
7779 * command queue as seen by the sequencer.
7781 if ((ahc
->tqinfifonext
& (HOST_TQINPOS
- 1)) == 1) {
7782 if ((ahc
->features
& AHC_HS_MAILBOX
) != 0) {
7785 hs_mailbox
= ahc_inb(ahc
, HS_MAILBOX
);
7786 hs_mailbox
&= ~HOST_TQINPOS
;
7787 hs_mailbox
|= ahc
->tqinfifonext
& HOST_TQINPOS
;
7788 ahc_outb(ahc
, HS_MAILBOX
, hs_mailbox
);
7792 ahc_outb(ahc
, KERNEL_TQINPOS
,
7793 ahc
->tqinfifonext
& HOST_TQINPOS
);
7802 ahc_handle_target_cmd(struct ahc_softc
*ahc
, struct target_cmd
*cmd
)
7804 struct ahc_tmode_tstate
*tstate
;
7805 struct ahc_tmode_lstate
*lstate
;
7806 struct ccb_accept_tio
*atio
;
7812 initiator
= SCSIID_TARGET(ahc
, cmd
->scsiid
);
7813 target
= SCSIID_OUR_ID(cmd
->scsiid
);
7814 lun
= (cmd
->identify
& MSG_IDENTIFY_LUNMASK
);
7817 tstate
= ahc
->enabled_targets
[target
];
7820 lstate
= tstate
->enabled_luns
[lun
];
7823 * Commands for disabled luns go to the black hole driver.
7826 lstate
= ahc
->black_hole
;
7828 atio
= (struct ccb_accept_tio
*)SLIST_FIRST(&lstate
->accept_tios
);
7830 ahc
->flags
|= AHC_TQINFIFO_BLOCKED
;
7832 * Wait for more ATIOs from the peripheral driver for this lun.
7835 printk("%s: ATIOs exhausted\n", ahc_name(ahc
));
7838 ahc
->flags
&= ~AHC_TQINFIFO_BLOCKED
;
7840 printk("Incoming command from %d for %d:%d%s\n",
7841 initiator
, target
, lun
,
7842 lstate
== ahc
->black_hole
? "(Black Holed)" : "");
7844 SLIST_REMOVE_HEAD(&lstate
->accept_tios
, sim_links
.sle
);
7846 if (lstate
== ahc
->black_hole
) {
7847 /* Fill in the wildcards */
7848 atio
->ccb_h
.target_id
= target
;
7849 atio
->ccb_h
.target_lun
= lun
;
7853 * Package it up and send it off to
7854 * whomever has this lun enabled.
7856 atio
->sense_len
= 0;
7857 atio
->init_id
= initiator
;
7858 if (byte
[0] != 0xFF) {
7859 /* Tag was included */
7860 atio
->tag_action
= *byte
++;
7861 atio
->tag_id
= *byte
++;
7862 atio
->ccb_h
.flags
= CAM_TAG_ACTION_VALID
;
7864 atio
->ccb_h
.flags
= 0;
7868 /* Okay. Now determine the cdb size based on the command code */
7869 switch (*byte
>> CMD_GROUP_CODE_SHIFT
) {
7885 /* Only copy the opcode. */
7887 printk("Reserved or VU command code type encountered\n");
7891 memcpy(atio
->cdb_io
.cdb_bytes
, byte
, atio
->cdb_len
);
7893 atio
->ccb_h
.status
|= CAM_CDB_RECVD
;
7895 if ((cmd
->identify
& MSG_IDENTIFY_DISCFLAG
) == 0) {
7897 * We weren't allowed to disconnect.
7898 * We're hanging on the bus until a
7899 * continue target I/O comes in response
7900 * to this accept tio.
7903 printk("Received Immediate Command %d:%d:%d - %p\n",
7904 initiator
, target
, lun
, ahc
->pending_device
);
7906 ahc
->pending_device
= lstate
;
7907 ahc_freeze_ccb((union ccb
*)atio
);
7908 atio
->ccb_h
.flags
|= CAM_DIS_DISCONNECT
;
7910 xpt_done((union ccb
*)atio
);