2 * Inline routines shareable across OS platforms.
4 * Copyright (c) 1994-2001 Justin T. Gibbs.
5 * Copyright (c) 2000-2003 Adaptec Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions, and the following disclaimer,
13 * without modification.
14 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
15 * substantially similar to the "NO WARRANTY" disclaimer below
16 * ("Disclaimer") and any redistribution must be conditioned upon
17 * including a substantially similar Disclaimer requirement for further
18 * binary redistribution.
19 * 3. Neither the names of the above-listed copyright holders nor the names
20 * of any contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
23 * Alternatively, this software may be distributed under the terms of the
24 * GNU General Public License ("GPL") version 2 as published by the Free
25 * Software Foundation.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
37 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGES.
40 * $Id: //depot/aic7xxx/aic7xxx/aic79xx_inline.h#51 $
45 #ifndef _AIC79XX_INLINE_H_
46 #define _AIC79XX_INLINE_H_
48 /******************************** Debugging ***********************************/
49 static __inline
char *ahd_name(struct ahd_softc
*ahd
);
51 static __inline
char *
52 ahd_name(struct ahd_softc
*ahd
)
57 /************************ Sequencer Execution Control *************************/
58 static __inline
void ahd_known_modes(struct ahd_softc
*ahd
,
59 ahd_mode src
, ahd_mode dst
);
60 static __inline ahd_mode_state
ahd_build_mode_state(struct ahd_softc
*ahd
,
63 static __inline
void ahd_extract_mode_state(struct ahd_softc
*ahd
,
65 ahd_mode
*src
, ahd_mode
*dst
);
66 static __inline
void ahd_set_modes(struct ahd_softc
*ahd
, ahd_mode src
,
68 static __inline
void ahd_update_modes(struct ahd_softc
*ahd
);
69 static __inline
void ahd_assert_modes(struct ahd_softc
*ahd
, ahd_mode srcmode
,
70 ahd_mode dstmode
, const char *file
,
72 static __inline ahd_mode_state
ahd_save_modes(struct ahd_softc
*ahd
);
73 static __inline
void ahd_restore_modes(struct ahd_softc
*ahd
,
74 ahd_mode_state state
);
75 static __inline
int ahd_is_paused(struct ahd_softc
*ahd
);
76 static __inline
void ahd_pause(struct ahd_softc
*ahd
);
77 static __inline
void ahd_unpause(struct ahd_softc
*ahd
);
80 ahd_known_modes(struct ahd_softc
*ahd
, ahd_mode src
, ahd_mode dst
)
84 ahd
->saved_src_mode
= src
;
85 ahd
->saved_dst_mode
= dst
;
88 static __inline ahd_mode_state
89 ahd_build_mode_state(struct ahd_softc
*ahd
, ahd_mode src
, ahd_mode dst
)
91 return ((src
<< SRC_MODE_SHIFT
) | (dst
<< DST_MODE_SHIFT
));
95 ahd_extract_mode_state(struct ahd_softc
*ahd
, ahd_mode_state state
,
96 ahd_mode
*src
, ahd_mode
*dst
)
98 *src
= (state
& SRC_MODE
) >> SRC_MODE_SHIFT
;
99 *dst
= (state
& DST_MODE
) >> DST_MODE_SHIFT
;
103 ahd_set_modes(struct ahd_softc
*ahd
, ahd_mode src
, ahd_mode dst
)
105 if (ahd
->src_mode
== src
&& ahd
->dst_mode
== dst
)
108 if (ahd
->src_mode
== AHD_MODE_UNKNOWN
109 || ahd
->dst_mode
== AHD_MODE_UNKNOWN
)
110 panic("Setting mode prior to saving it.\n");
111 if ((ahd_debug
& AHD_SHOW_MODEPTR
) != 0)
112 printf("%s: Setting mode 0x%x\n", ahd_name(ahd
),
113 ahd_build_mode_state(ahd
, src
, dst
));
115 ahd_outb(ahd
, MODE_PTR
, ahd_build_mode_state(ahd
, src
, dst
));
121 ahd_update_modes(struct ahd_softc
*ahd
)
123 ahd_mode_state mode_ptr
;
127 mode_ptr
= ahd_inb(ahd
, MODE_PTR
);
129 if ((ahd_debug
& AHD_SHOW_MODEPTR
) != 0)
130 printf("Reading mode 0x%x\n", mode_ptr
);
132 ahd_extract_mode_state(ahd
, mode_ptr
, &src
, &dst
);
133 ahd_known_modes(ahd
, src
, dst
);
137 ahd_assert_modes(struct ahd_softc
*ahd
, ahd_mode srcmode
,
138 ahd_mode dstmode
, const char *file
, int line
)
141 if ((srcmode
& AHD_MK_MSK(ahd
->src_mode
)) == 0
142 || (dstmode
& AHD_MK_MSK(ahd
->dst_mode
)) == 0) {
143 panic("%s:%s:%d: Mode assertion failed.\n",
144 ahd_name(ahd
), file
, line
);
149 static __inline ahd_mode_state
150 ahd_save_modes(struct ahd_softc
*ahd
)
152 if (ahd
->src_mode
== AHD_MODE_UNKNOWN
153 || ahd
->dst_mode
== AHD_MODE_UNKNOWN
)
154 ahd_update_modes(ahd
);
156 return (ahd_build_mode_state(ahd
, ahd
->src_mode
, ahd
->dst_mode
));
160 ahd_restore_modes(struct ahd_softc
*ahd
, ahd_mode_state state
)
165 ahd_extract_mode_state(ahd
, state
, &src
, &dst
);
166 ahd_set_modes(ahd
, src
, dst
);
169 #define AHD_ASSERT_MODES(ahd, source, dest) \
170 ahd_assert_modes(ahd, source, dest, __FILE__, __LINE__);
173 * Determine whether the sequencer has halted code execution.
174 * Returns non-zero status if the sequencer is stopped.
177 ahd_is_paused(struct ahd_softc
*ahd
)
179 return ((ahd_inb(ahd
, HCNTRL
) & PAUSE
) != 0);
183 * Request that the sequencer stop and wait, indefinitely, for it
184 * to stop. The sequencer will only acknowledge that it is paused
185 * once it has reached an instruction boundary and PAUSEDIS is
186 * cleared in the SEQCTL register. The sequencer may use PAUSEDIS
187 * for critical sections.
190 ahd_pause(struct ahd_softc
*ahd
)
192 ahd_outb(ahd
, HCNTRL
, ahd
->pause
);
195 * Since the sequencer can disable pausing in a critical section, we
196 * must loop until it actually stops.
198 while (ahd_is_paused(ahd
) == 0)
203 * Allow the sequencer to continue program execution.
204 * We check here to ensure that no additional interrupt
205 * sources that would cause the sequencer to halt have been
206 * asserted. If, for example, a SCSI bus reset is detected
207 * while we are fielding a different, pausing, interrupt type,
208 * we don't want to release the sequencer before going back
209 * into our interrupt handler and dealing with this new
213 ahd_unpause(struct ahd_softc
*ahd
)
216 * Automatically restore our modes to those saved
217 * prior to the first change of the mode.
219 if (ahd
->saved_src_mode
!= AHD_MODE_UNKNOWN
220 && ahd
->saved_dst_mode
!= AHD_MODE_UNKNOWN
) {
221 if ((ahd
->flags
& AHD_UPDATE_PEND_CMDS
) != 0)
222 ahd_reset_cmds_pending(ahd
);
223 ahd_set_modes(ahd
, ahd
->saved_src_mode
, ahd
->saved_dst_mode
);
226 if ((ahd_inb(ahd
, INTSTAT
) & ~CMDCMPLT
) == 0)
227 ahd_outb(ahd
, HCNTRL
, ahd
->unpause
);
229 ahd_known_modes(ahd
, AHD_MODE_UNKNOWN
, AHD_MODE_UNKNOWN
);
232 /*********************** Scatter Gather List Handling *************************/
233 static __inline
void *ahd_sg_setup(struct ahd_softc
*ahd
, struct scb
*scb
,
234 void *sgptr
, dma_addr_t addr
,
235 bus_size_t len
, int last
);
236 static __inline
void ahd_setup_scb_common(struct ahd_softc
*ahd
,
238 static __inline
void ahd_setup_data_scb(struct ahd_softc
*ahd
,
240 static __inline
void ahd_setup_noxfer_scb(struct ahd_softc
*ahd
,
243 static __inline
void *
244 ahd_sg_setup(struct ahd_softc
*ahd
, struct scb
*scb
,
245 void *sgptr
, dma_addr_t addr
, bus_size_t len
, int last
)
248 if (sizeof(dma_addr_t
) > 4
249 && (ahd
->flags
& AHD_64BIT_ADDRESSING
) != 0) {
250 struct ahd_dma64_seg
*sg
;
252 sg
= (struct ahd_dma64_seg
*)sgptr
;
253 sg
->addr
= ahd_htole64(addr
);
254 sg
->len
= ahd_htole32(len
| (last
? AHD_DMA_LAST_SEG
: 0));
257 struct ahd_dma_seg
*sg
;
259 sg
= (struct ahd_dma_seg
*)sgptr
;
260 sg
->addr
= ahd_htole32(addr
& 0xFFFFFFFF);
261 sg
->len
= ahd_htole32(len
| ((addr
>> 8) & 0x7F000000)
262 | (last
? AHD_DMA_LAST_SEG
: 0));
268 ahd_setup_scb_common(struct ahd_softc
*ahd
, struct scb
*scb
)
270 /* XXX Handle target mode SCBs. */
271 scb
->crc_retry_count
= 0;
272 if ((scb
->flags
& SCB_PACKETIZED
) != 0) {
273 /* XXX what about ACA?? It is type 4, but TAG_TYPE == 0x3. */
274 scb
->hscb
->task_attribute
= scb
->hscb
->control
& SCB_TAG_TYPE
;
276 if (ahd_get_transfer_length(scb
) & 0x01)
277 scb
->hscb
->task_attribute
= SCB_XFERLEN_ODD
;
279 scb
->hscb
->task_attribute
= 0;
282 if (scb
->hscb
->cdb_len
<= MAX_CDB_LEN_WITH_SENSE_ADDR
283 || (scb
->hscb
->cdb_len
& SCB_CDB_LEN_PTR
) != 0)
284 scb
->hscb
->shared_data
.idata
.cdb_plus_saddr
.sense_addr
=
285 ahd_htole32(scb
->sense_busaddr
);
289 ahd_setup_data_scb(struct ahd_softc
*ahd
, struct scb
*scb
)
292 * Copy the first SG into the "current" data ponter area.
294 if ((ahd
->flags
& AHD_64BIT_ADDRESSING
) != 0) {
295 struct ahd_dma64_seg
*sg
;
297 sg
= (struct ahd_dma64_seg
*)scb
->sg_list
;
298 scb
->hscb
->dataptr
= sg
->addr
;
299 scb
->hscb
->datacnt
= sg
->len
;
301 struct ahd_dma_seg
*sg
;
302 uint32_t *dataptr_words
;
304 sg
= (struct ahd_dma_seg
*)scb
->sg_list
;
305 dataptr_words
= (uint32_t*)&scb
->hscb
->dataptr
;
306 dataptr_words
[0] = sg
->addr
;
307 dataptr_words
[1] = 0;
308 if ((ahd
->flags
& AHD_39BIT_ADDRESSING
) != 0) {
311 high_addr
= ahd_le32toh(sg
->len
) & 0x7F000000;
312 scb
->hscb
->dataptr
|= ahd_htole64(high_addr
<< 8);
314 scb
->hscb
->datacnt
= sg
->len
;
317 * Note where to find the SG entries in bus space.
318 * We also set the full residual flag which the
319 * sequencer will clear as soon as a data transfer
322 scb
->hscb
->sgptr
= ahd_htole32(scb
->sg_list_busaddr
|SG_FULL_RESID
);
326 ahd_setup_noxfer_scb(struct ahd_softc
*ahd
, struct scb
*scb
)
328 scb
->hscb
->sgptr
= ahd_htole32(SG_LIST_NULL
);
329 scb
->hscb
->dataptr
= 0;
330 scb
->hscb
->datacnt
= 0;
333 /************************** Memory mapping routines ***************************/
334 static __inline
size_t ahd_sg_size(struct ahd_softc
*ahd
);
335 static __inline
void *
336 ahd_sg_bus_to_virt(struct ahd_softc
*ahd
,
338 uint32_t sg_busaddr
);
339 static __inline
uint32_t
340 ahd_sg_virt_to_bus(struct ahd_softc
*ahd
,
343 static __inline
void ahd_sync_scb(struct ahd_softc
*ahd
,
344 struct scb
*scb
, int op
);
345 static __inline
void ahd_sync_sglist(struct ahd_softc
*ahd
,
346 struct scb
*scb
, int op
);
347 static __inline
void ahd_sync_sense(struct ahd_softc
*ahd
,
348 struct scb
*scb
, int op
);
349 static __inline
uint32_t
350 ahd_targetcmd_offset(struct ahd_softc
*ahd
,
353 static __inline
size_t
354 ahd_sg_size(struct ahd_softc
*ahd
)
356 if ((ahd
->flags
& AHD_64BIT_ADDRESSING
) != 0)
357 return (sizeof(struct ahd_dma64_seg
));
358 return (sizeof(struct ahd_dma_seg
));
361 static __inline
void *
362 ahd_sg_bus_to_virt(struct ahd_softc
*ahd
, struct scb
*scb
, uint32_t sg_busaddr
)
364 dma_addr_t sg_offset
;
366 /* sg_list_phys points to entry 1, not 0 */
367 sg_offset
= sg_busaddr
- (scb
->sg_list_busaddr
- ahd_sg_size(ahd
));
368 return ((uint8_t *)scb
->sg_list
+ sg_offset
);
371 static __inline
uint32_t
372 ahd_sg_virt_to_bus(struct ahd_softc
*ahd
, struct scb
*scb
, void *sg
)
374 dma_addr_t sg_offset
;
376 /* sg_list_phys points to entry 1, not 0 */
377 sg_offset
= ((uint8_t *)sg
- (uint8_t *)scb
->sg_list
)
380 return (scb
->sg_list_busaddr
+ sg_offset
);
384 ahd_sync_scb(struct ahd_softc
*ahd
, struct scb
*scb
, int op
)
386 ahd_dmamap_sync(ahd
, ahd
->scb_data
.hscb_dmat
,
387 scb
->hscb_map
->dmamap
,
388 /*offset*/(uint8_t*)scb
->hscb
- scb
->hscb_map
->vaddr
,
389 /*len*/sizeof(*scb
->hscb
), op
);
393 ahd_sync_sglist(struct ahd_softc
*ahd
, struct scb
*scb
, int op
)
395 if (scb
->sg_count
== 0)
398 ahd_dmamap_sync(ahd
, ahd
->scb_data
.sg_dmat
,
400 /*offset*/scb
->sg_list_busaddr
- ahd_sg_size(ahd
),
401 /*len*/ahd_sg_size(ahd
) * scb
->sg_count
, op
);
405 ahd_sync_sense(struct ahd_softc
*ahd
, struct scb
*scb
, int op
)
407 ahd_dmamap_sync(ahd
, ahd
->scb_data
.sense_dmat
,
408 scb
->sense_map
->dmamap
,
409 /*offset*/scb
->sense_busaddr
,
410 /*len*/AHD_SENSE_BUFSIZE
, op
);
413 static __inline
uint32_t
414 ahd_targetcmd_offset(struct ahd_softc
*ahd
, u_int index
)
416 return (((uint8_t *)&ahd
->targetcmds
[index
])
417 - (uint8_t *)ahd
->qoutfifo
);
420 /*********************** Miscelaneous Support Functions ***********************/
421 static __inline
void ahd_complete_scb(struct ahd_softc
*ahd
,
423 static __inline
void ahd_update_residual(struct ahd_softc
*ahd
,
425 static __inline
struct ahd_initiator_tinfo
*
426 ahd_fetch_transinfo(struct ahd_softc
*ahd
,
427 char channel
, u_int our_id
,
429 struct ahd_tmode_tstate
**tstate
);
430 static __inline
uint16_t
431 ahd_inw(struct ahd_softc
*ahd
, u_int port
);
432 static __inline
void ahd_outw(struct ahd_softc
*ahd
, u_int port
,
434 static __inline
uint32_t
435 ahd_inl(struct ahd_softc
*ahd
, u_int port
);
436 static __inline
void ahd_outl(struct ahd_softc
*ahd
, u_int port
,
438 static __inline
uint64_t
439 ahd_inq(struct ahd_softc
*ahd
, u_int port
);
440 static __inline
void ahd_outq(struct ahd_softc
*ahd
, u_int port
,
442 static __inline u_int
ahd_get_scbptr(struct ahd_softc
*ahd
);
443 static __inline
void ahd_set_scbptr(struct ahd_softc
*ahd
, u_int scbptr
);
444 static __inline u_int
ahd_get_hnscb_qoff(struct ahd_softc
*ahd
);
445 static __inline
void ahd_set_hnscb_qoff(struct ahd_softc
*ahd
, u_int value
);
446 static __inline u_int
ahd_get_hescb_qoff(struct ahd_softc
*ahd
);
447 static __inline
void ahd_set_hescb_qoff(struct ahd_softc
*ahd
, u_int value
);
448 static __inline u_int
ahd_get_snscb_qoff(struct ahd_softc
*ahd
);
449 static __inline
void ahd_set_snscb_qoff(struct ahd_softc
*ahd
, u_int value
);
450 static __inline u_int
ahd_get_sescb_qoff(struct ahd_softc
*ahd
);
451 static __inline
void ahd_set_sescb_qoff(struct ahd_softc
*ahd
, u_int value
);
452 static __inline u_int
ahd_get_sdscb_qoff(struct ahd_softc
*ahd
);
453 static __inline
void ahd_set_sdscb_qoff(struct ahd_softc
*ahd
, u_int value
);
454 static __inline u_int
ahd_inb_scbram(struct ahd_softc
*ahd
, u_int offset
);
455 static __inline u_int
ahd_inw_scbram(struct ahd_softc
*ahd
, u_int offset
);
456 static __inline
uint32_t
457 ahd_inl_scbram(struct ahd_softc
*ahd
, u_int offset
);
458 static __inline
uint64_t
459 ahd_inq_scbram(struct ahd_softc
*ahd
, u_int offset
);
460 static __inline
void ahd_swap_with_next_hscb(struct ahd_softc
*ahd
,
462 static __inline
void ahd_queue_scb(struct ahd_softc
*ahd
, struct scb
*scb
);
463 static __inline
uint8_t *
464 ahd_get_sense_buf(struct ahd_softc
*ahd
,
466 static __inline
uint32_t
467 ahd_get_sense_bufaddr(struct ahd_softc
*ahd
,
471 ahd_complete_scb(struct ahd_softc
*ahd
, struct scb
*scb
)
475 sgptr
= ahd_le32toh(scb
->hscb
->sgptr
);
476 if ((sgptr
& SG_STATUS_VALID
) != 0)
477 ahd_handle_scb_status(ahd
, scb
);
483 * Determine whether the sequencer reported a residual
484 * for this SCB/transaction.
487 ahd_update_residual(struct ahd_softc
*ahd
, struct scb
*scb
)
491 sgptr
= ahd_le32toh(scb
->hscb
->sgptr
);
492 if ((sgptr
& SG_STATUS_VALID
) != 0)
493 ahd_calc_residual(ahd
, scb
);
497 * Return pointers to the transfer negotiation information
498 * for the specified our_id/remote_id pair.
500 static __inline
struct ahd_initiator_tinfo
*
501 ahd_fetch_transinfo(struct ahd_softc
*ahd
, char channel
, u_int our_id
,
502 u_int remote_id
, struct ahd_tmode_tstate
**tstate
)
505 * Transfer data structures are stored from the perspective
506 * of the target role. Since the parameters for a connection
507 * in the initiator role to a given target are the same as
508 * when the roles are reversed, we pretend we are the target.
512 *tstate
= ahd
->enabled_targets
[our_id
];
513 return (&(*tstate
)->transinfo
[remote_id
]);
516 #define AHD_COPY_COL_IDX(dst, src) \
518 dst->hscb->scsiid = src->hscb->scsiid; \
519 dst->hscb->lun = src->hscb->lun; \
522 static __inline
uint16_t
523 ahd_inw(struct ahd_softc
*ahd
, u_int port
)
525 return ((ahd_inb(ahd
, port
+1) << 8) | ahd_inb(ahd
, port
));
529 ahd_outw(struct ahd_softc
*ahd
, u_int port
, u_int value
)
531 ahd_outb(ahd
, port
, value
& 0xFF);
532 ahd_outb(ahd
, port
+1, (value
>> 8) & 0xFF);
535 static __inline
uint32_t
536 ahd_inl(struct ahd_softc
*ahd
, u_int port
)
538 return ((ahd_inb(ahd
, port
))
539 | (ahd_inb(ahd
, port
+1) << 8)
540 | (ahd_inb(ahd
, port
+2) << 16)
541 | (ahd_inb(ahd
, port
+3) << 24));
545 ahd_outl(struct ahd_softc
*ahd
, u_int port
, uint32_t value
)
547 ahd_outb(ahd
, port
, (value
) & 0xFF);
548 ahd_outb(ahd
, port
+1, ((value
) >> 8) & 0xFF);
549 ahd_outb(ahd
, port
+2, ((value
) >> 16) & 0xFF);
550 ahd_outb(ahd
, port
+3, ((value
) >> 24) & 0xFF);
553 static __inline
uint64_t
554 ahd_inq(struct ahd_softc
*ahd
, u_int port
)
556 return ((ahd_inb(ahd
, port
))
557 | (ahd_inb(ahd
, port
+1) << 8)
558 | (ahd_inb(ahd
, port
+2) << 16)
559 | (ahd_inb(ahd
, port
+3) << 24)
560 | (((uint64_t)ahd_inb(ahd
, port
+4)) << 32)
561 | (((uint64_t)ahd_inb(ahd
, port
+5)) << 40)
562 | (((uint64_t)ahd_inb(ahd
, port
+6)) << 48)
563 | (((uint64_t)ahd_inb(ahd
, port
+7)) << 56));
567 ahd_outq(struct ahd_softc
*ahd
, u_int port
, uint64_t value
)
569 ahd_outb(ahd
, port
, value
& 0xFF);
570 ahd_outb(ahd
, port
+1, (value
>> 8) & 0xFF);
571 ahd_outb(ahd
, port
+2, (value
>> 16) & 0xFF);
572 ahd_outb(ahd
, port
+3, (value
>> 24) & 0xFF);
573 ahd_outb(ahd
, port
+4, (value
>> 32) & 0xFF);
574 ahd_outb(ahd
, port
+5, (value
>> 40) & 0xFF);
575 ahd_outb(ahd
, port
+6, (value
>> 48) & 0xFF);
576 ahd_outb(ahd
, port
+7, (value
>> 56) & 0xFF);
579 static __inline u_int
580 ahd_get_scbptr(struct ahd_softc
*ahd
)
582 AHD_ASSERT_MODES(ahd
, ~(AHD_MODE_UNKNOWN_MSK
|AHD_MODE_CFG_MSK
),
583 ~(AHD_MODE_UNKNOWN_MSK
|AHD_MODE_CFG_MSK
));
584 return (ahd_inb(ahd
, SCBPTR
) | (ahd_inb(ahd
, SCBPTR
+ 1) << 8));
588 ahd_set_scbptr(struct ahd_softc
*ahd
, u_int scbptr
)
590 AHD_ASSERT_MODES(ahd
, ~(AHD_MODE_UNKNOWN_MSK
|AHD_MODE_CFG_MSK
),
591 ~(AHD_MODE_UNKNOWN_MSK
|AHD_MODE_CFG_MSK
));
592 ahd_outb(ahd
, SCBPTR
, scbptr
& 0xFF);
593 ahd_outb(ahd
, SCBPTR
+1, (scbptr
>> 8) & 0xFF);
596 static __inline u_int
597 ahd_get_hnscb_qoff(struct ahd_softc
*ahd
)
599 return (ahd_inw_atomic(ahd
, HNSCB_QOFF
));
603 ahd_set_hnscb_qoff(struct ahd_softc
*ahd
, u_int value
)
605 ahd_outw_atomic(ahd
, HNSCB_QOFF
, value
);
608 static __inline u_int
609 ahd_get_hescb_qoff(struct ahd_softc
*ahd
)
611 return (ahd_inb(ahd
, HESCB_QOFF
));
615 ahd_set_hescb_qoff(struct ahd_softc
*ahd
, u_int value
)
617 ahd_outb(ahd
, HESCB_QOFF
, value
);
620 static __inline u_int
621 ahd_get_snscb_qoff(struct ahd_softc
*ahd
)
625 AHD_ASSERT_MODES(ahd
, AHD_MODE_CCHAN_MSK
, AHD_MODE_CCHAN_MSK
);
626 oldvalue
= ahd_inw(ahd
, SNSCB_QOFF
);
627 ahd_outw(ahd
, SNSCB_QOFF
, oldvalue
);
632 ahd_set_snscb_qoff(struct ahd_softc
*ahd
, u_int value
)
634 AHD_ASSERT_MODES(ahd
, AHD_MODE_CCHAN_MSK
, AHD_MODE_CCHAN_MSK
);
635 ahd_outw(ahd
, SNSCB_QOFF
, value
);
638 static __inline u_int
639 ahd_get_sescb_qoff(struct ahd_softc
*ahd
)
641 AHD_ASSERT_MODES(ahd
, AHD_MODE_CCHAN_MSK
, AHD_MODE_CCHAN_MSK
);
642 return (ahd_inb(ahd
, SESCB_QOFF
));
646 ahd_set_sescb_qoff(struct ahd_softc
*ahd
, u_int value
)
648 AHD_ASSERT_MODES(ahd
, AHD_MODE_CCHAN_MSK
, AHD_MODE_CCHAN_MSK
);
649 ahd_outb(ahd
, SESCB_QOFF
, value
);
652 static __inline u_int
653 ahd_get_sdscb_qoff(struct ahd_softc
*ahd
)
655 AHD_ASSERT_MODES(ahd
, AHD_MODE_CCHAN_MSK
, AHD_MODE_CCHAN_MSK
);
656 return (ahd_inb(ahd
, SDSCB_QOFF
) | (ahd_inb(ahd
, SDSCB_QOFF
+ 1) << 8));
660 ahd_set_sdscb_qoff(struct ahd_softc
*ahd
, u_int value
)
662 AHD_ASSERT_MODES(ahd
, AHD_MODE_CCHAN_MSK
, AHD_MODE_CCHAN_MSK
);
663 ahd_outb(ahd
, SDSCB_QOFF
, value
& 0xFF);
664 ahd_outb(ahd
, SDSCB_QOFF
+1, (value
>> 8) & 0xFF);
667 static __inline u_int
668 ahd_inb_scbram(struct ahd_softc
*ahd
, u_int offset
)
673 * Workaround PCI-X Rev A. hardware bug.
674 * After a host read of SCB memory, the chip
675 * may become confused into thinking prefetch
676 * was required. This starts the discard timer
677 * running and can cause an unexpected discard
678 * timer interrupt. The work around is to read
679 * a normal register prior to the exhaustion of
680 * the discard timer. The mode pointer register
681 * has no side effects and so serves well for
686 value
= ahd_inb(ahd
, offset
);
687 if ((ahd
->flags
& AHD_PCIX_SCBRAM_RD_BUG
) != 0)
688 ahd_inb(ahd
, MODE_PTR
);
692 static __inline u_int
693 ahd_inw_scbram(struct ahd_softc
*ahd
, u_int offset
)
695 return (ahd_inb_scbram(ahd
, offset
)
696 | (ahd_inb_scbram(ahd
, offset
+1) << 8));
699 static __inline
uint32_t
700 ahd_inl_scbram(struct ahd_softc
*ahd
, u_int offset
)
702 return (ahd_inw_scbram(ahd
, offset
)
703 | (ahd_inw_scbram(ahd
, offset
+2) << 16));
706 static __inline
uint64_t
707 ahd_inq_scbram(struct ahd_softc
*ahd
, u_int offset
)
709 return (ahd_inl_scbram(ahd
, offset
)
710 | ((uint64_t)ahd_inl_scbram(ahd
, offset
+4)) << 32);
713 static __inline
struct scb
*
714 ahd_lookup_scb(struct ahd_softc
*ahd
, u_int tag
)
718 if (tag
>= AHD_SCB_MAX
)
720 scb
= ahd
->scb_data
.scbindex
[tag
];
722 ahd_sync_scb(ahd
, scb
,
723 BUS_DMASYNC_POSTREAD
|BUS_DMASYNC_POSTWRITE
);
728 ahd_swap_with_next_hscb(struct ahd_softc
*ahd
, struct scb
*scb
)
730 struct hardware_scb
*q_hscb
;
731 uint32_t saved_hscb_busaddr
;
734 * Our queuing method is a bit tricky. The card
735 * knows in advance which HSCB (by address) to download,
736 * and we can't disappoint it. To achieve this, the next
737 * HSCB to download is saved off in ahd->next_queued_hscb.
738 * When we are called to queue "an arbitrary scb",
739 * we copy the contents of the incoming HSCB to the one
740 * the sequencer knows about, swap HSCB pointers and
741 * finally assign the SCB to the tag indexed location
742 * in the scb_array. This makes sure that we can still
743 * locate the correct SCB by SCB_TAG.
745 q_hscb
= ahd
->next_queued_hscb
;
746 saved_hscb_busaddr
= q_hscb
->hscb_busaddr
;
747 memcpy(q_hscb
, scb
->hscb
, sizeof(*scb
->hscb
));
748 q_hscb
->hscb_busaddr
= saved_hscb_busaddr
;
749 q_hscb
->next_hscb_busaddr
= scb
->hscb
->hscb_busaddr
;
751 /* Now swap HSCB pointers. */
752 ahd
->next_queued_hscb
= scb
->hscb
;
755 /* Now define the mapping from tag to SCB in the scbindex */
756 ahd
->scb_data
.scbindex
[SCB_GET_TAG(scb
)] = scb
;
760 * Tell the sequencer about a new transaction to execute.
763 ahd_queue_scb(struct ahd_softc
*ahd
, struct scb
*scb
)
765 ahd_swap_with_next_hscb(ahd
, scb
);
767 if (SCBID_IS_NULL(SCB_GET_TAG(scb
)))
768 panic("Attempt to queue invalid SCB tag %x\n",
772 * Keep a history of SCBs we've downloaded in the qinfifo.
774 ahd
->qinfifo
[AHD_QIN_WRAP(ahd
->qinfifonext
)] = SCB_GET_TAG(scb
);
777 if (scb
->sg_count
!= 0)
778 ahd_setup_data_scb(ahd
, scb
);
780 ahd_setup_noxfer_scb(ahd
, scb
);
781 ahd_setup_scb_common(ahd
, scb
);
784 * Make sure our data is consistent from the
785 * perspective of the adapter.
787 ahd_sync_scb(ahd
, scb
, BUS_DMASYNC_PREREAD
|BUS_DMASYNC_PREWRITE
);
790 if ((ahd_debug
& AHD_SHOW_QUEUE
) != 0) {
791 uint64_t host_dataptr
;
793 host_dataptr
= ahd_le64toh(scb
->hscb
->dataptr
);
794 printf("%s: Queueing SCB 0x%x bus addr 0x%x - 0x%x%x/0x%x\n",
796 SCB_GET_TAG(scb
), ahd_le32toh(scb
->hscb
->hscb_busaddr
),
797 (u_int
)((host_dataptr
>> 32) & 0xFFFFFFFF),
798 (u_int
)(host_dataptr
& 0xFFFFFFFF),
799 ahd_le32toh(scb
->hscb
->datacnt
));
802 /* Tell the adapter about the newly queued SCB */
803 ahd_set_hnscb_qoff(ahd
, ahd
->qinfifonext
);
806 static __inline
uint8_t *
807 ahd_get_sense_buf(struct ahd_softc
*ahd
, struct scb
*scb
)
809 return (scb
->sense_data
);
812 static __inline
uint32_t
813 ahd_get_sense_bufaddr(struct ahd_softc
*ahd
, struct scb
*scb
)
815 return (scb
->sense_busaddr
);
818 /************************** Interrupt Processing ******************************/
819 static __inline
void ahd_sync_qoutfifo(struct ahd_softc
*ahd
, int op
);
820 static __inline
void ahd_sync_tqinfifo(struct ahd_softc
*ahd
, int op
);
821 static __inline u_int
ahd_check_cmdcmpltqueues(struct ahd_softc
*ahd
);
822 static __inline
int ahd_intr(struct ahd_softc
*ahd
);
825 ahd_sync_qoutfifo(struct ahd_softc
*ahd
, int op
)
827 ahd_dmamap_sync(ahd
, ahd
->shared_data_dmat
, ahd
->shared_data_dmamap
,
828 /*offset*/0, /*len*/AHC_SCB_MAX
* sizeof(uint16_t), op
);
832 ahd_sync_tqinfifo(struct ahd_softc
*ahd
, int op
)
834 #ifdef AHD_TARGET_MODE
835 if ((ahd
->flags
& AHD_TARGETROLE
) != 0) {
836 ahd_dmamap_sync(ahd
, ahd
->shared_data_dmat
,
837 ahd
->shared_data_dmamap
,
838 ahd_targetcmd_offset(ahd
, 0),
839 sizeof(struct target_cmd
) * AHD_TMODE_CMDS
,
846 * See if the firmware has posted any completed commands
847 * into our in-core command complete fifos.
849 #define AHD_RUN_QOUTFIFO 0x1
850 #define AHD_RUN_TQINFIFO 0x2
851 static __inline u_int
852 ahd_check_cmdcmpltqueues(struct ahd_softc
*ahd
)
857 ahd_dmamap_sync(ahd
, ahd
->shared_data_dmat
, ahd
->shared_data_dmamap
,
858 /*offset*/ahd
->qoutfifonext
, /*len*/2,
859 BUS_DMASYNC_POSTREAD
);
860 if ((ahd
->qoutfifo
[ahd
->qoutfifonext
]
861 & QOUTFIFO_ENTRY_VALID_LE
) == ahd
->qoutfifonext_valid_tag
)
862 retval
|= AHD_RUN_QOUTFIFO
;
863 #ifdef AHD_TARGET_MODE
864 if ((ahd
->flags
& AHD_TARGETROLE
) != 0
865 && (ahd
->flags
& AHD_TQINFIFO_BLOCKED
) == 0) {
866 ahd_dmamap_sync(ahd
, ahd
->shared_data_dmat
,
867 ahd
->shared_data_dmamap
,
868 ahd_targetcmd_offset(ahd
, ahd
->tqinfifofnext
),
869 /*len*/sizeof(struct target_cmd
),
870 BUS_DMASYNC_POSTREAD
);
871 if (ahd
->targetcmds
[ahd
->tqinfifonext
].cmd_valid
!= 0)
872 retval
|= AHD_RUN_TQINFIFO
;
879 * Catch an interrupt from the adapter
882 ahd_intr(struct ahd_softc
*ahd
)
886 if ((ahd
->pause
& INTEN
) == 0) {
888 * Our interrupt is not enabled on the chip
889 * and may be disabled for re-entrancy reasons,
890 * so just return. This is likely just a shared
897 * Instead of directly reading the interrupt status register,
898 * infer the cause of the interrupt by checking our in-core
899 * completion queues. This avoids a costly PCI bus read in
902 if ((ahd
->flags
& AHD_ALL_INTERRUPTS
) == 0
903 && (ahd_check_cmdcmpltqueues(ahd
) != 0))
906 intstat
= ahd_inb(ahd
, INTSTAT
);
908 if ((intstat
& INT_PEND
) == 0)
911 if (intstat
& CMDCMPLT
) {
912 ahd_outb(ahd
, CLRINT
, CLRCMDINT
);
915 * Ensure that the chip sees that we've cleared
916 * this interrupt before we walk the output fifo.
917 * Otherwise, we may, due to posted bus writes,
918 * clear the interrupt after we finish the scan,
919 * and after the sequencer has added new entries
920 * and asserted the interrupt again.
922 if ((ahd
->bugs
& AHD_INTCOLLISION_BUG
) != 0) {
923 if (ahd_is_paused(ahd
)) {
925 * Potentially lost SEQINT.
926 * If SEQINTCODE is non-zero,
927 * simulate the SEQINT.
929 if (ahd_inb(ahd
, SEQINTCODE
) != NO_SEQINT
)
933 ahd_flush_device_writes(ahd
);
935 ahd_run_qoutfifo(ahd
);
936 ahd
->cmdcmplt_counts
[ahd
->cmdcmplt_bucket
]++;
937 ahd
->cmdcmplt_total
++;
938 #ifdef AHD_TARGET_MODE
939 if ((ahd
->flags
& AHD_TARGETROLE
) != 0)
940 ahd_run_tqinfifo(ahd
, /*paused*/FALSE
);
945 * Handle statuses that may invalidate our cached
946 * copy of INTSTAT separately.
948 if (intstat
== 0xFF && (ahd
->features
& AHD_REMOVABLE
) != 0) {
949 /* Hot eject. Do nothing */
950 } else if (intstat
& HWERRINT
) {
951 ahd_handle_hwerrint(ahd
);
952 } else if ((intstat
& (PCIINT
|SPLTINT
)) != 0) {
956 if ((intstat
& SEQINT
) != 0)
957 ahd_handle_seqint(ahd
, intstat
);
959 if ((intstat
& SCSIINT
) != 0)
960 ahd_handle_scsiint(ahd
, intstat
);
965 #endif /* _AIC79XX_INLINE_H_ */