aic79xx_inline.h revision 1.2 1 1.2 wiz /* $NetBSD: aic79xx_inline.h,v 1.2 2003/05/03 18:11:13 wiz Exp $ */
2 1.1 fvdl
3 1.1 fvdl /*
4 1.1 fvdl * Inline routines shareable across OS platforms.
5 1.1 fvdl *
6 1.1 fvdl * Copyright (c) 1994-2001 Justin T. Gibbs.
7 1.1 fvdl * Copyright (c) 2000-2003 Adaptec Inc.
8 1.1 fvdl * All rights reserved.
9 1.1 fvdl *
10 1.1 fvdl * Redistribution and use in source and binary forms, with or without
11 1.1 fvdl * modification, are permitted provided that the following conditions
12 1.1 fvdl * are met:
13 1.1 fvdl * 1. Redistributions of source code must retain the above copyright
14 1.1 fvdl * notice, this list of conditions, and the following disclaimer,
15 1.1 fvdl * without modification.
16 1.1 fvdl * 2. Redistributions in binary form must reproduce at minimum a disclaimer
17 1.1 fvdl * substantially similar to the "NO WARRANTY" disclaimer below
18 1.1 fvdl * ("Disclaimer") and any redistribution must be conditioned upon
19 1.1 fvdl * including a substantially similar Disclaimer requirement for further
20 1.1 fvdl * binary redistribution.
21 1.1 fvdl * 3. Neither the names of the above-listed copyright holders nor the names
22 1.1 fvdl * of any contributors may be used to endorse or promote products derived
23 1.1 fvdl * from this software without specific prior written permission.
24 1.1 fvdl *
25 1.1 fvdl * Alternatively, this software may be distributed under the terms of the
26 1.1 fvdl * GNU General Public License ("GPL") version 2 as published by the Free
27 1.1 fvdl * Software Foundation.
28 1.1 fvdl *
29 1.1 fvdl * NO WARRANTY
30 1.1 fvdl * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 1.1 fvdl * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 1.1 fvdl * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
33 1.1 fvdl * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34 1.1 fvdl * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 1.1 fvdl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 1.1 fvdl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 1.1 fvdl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
38 1.1 fvdl * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
39 1.1 fvdl * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 1.1 fvdl * POSSIBILITY OF SUCH DAMAGES.
41 1.1 fvdl *
42 1.1 fvdl * //depot/aic7xxx/aic7xxx/aic79xx_inline.h#44 $
43 1.1 fvdl *
44 1.1 fvdl * $FreeBSD: src/sys/dev/aic7xxx/aic79xx_inline.h,v 1.8 2003/03/06 23:58:34 gibbs Exp $
45 1.1 fvdl */
46 1.1 fvdl /*
47 1.1 fvdl * Ported from FreeBSD by Pascal Renauld, Network Storage Solutions, Inc. - April 2003
48 1.1 fvdl */
49 1.1 fvdl
50 1.1 fvdl #ifndef _AIC79XX_INLINE_H_
51 1.1 fvdl #define _AIC79XX_INLINE_H_
52 1.1 fvdl
53 1.1 fvdl /******************************** Debugging ***********************************/
54 1.1 fvdl static __inline char *ahd_name(struct ahd_softc *ahd);
55 1.1 fvdl
56 1.1 fvdl static __inline char *
57 1.1 fvdl ahd_name(struct ahd_softc *ahd)
58 1.1 fvdl {
59 1.1 fvdl return (ahd->name);
60 1.1 fvdl }
61 1.1 fvdl
62 1.1 fvdl /************************ Sequencer Execution Control *************************/
63 1.1 fvdl static __inline void ahd_known_modes(struct ahd_softc *ahd,
64 1.1 fvdl ahd_mode src, ahd_mode dst);
65 1.1 fvdl static __inline ahd_mode_state ahd_build_mode_state(struct ahd_softc *ahd,
66 1.1 fvdl ahd_mode src,
67 1.1 fvdl ahd_mode dst);
68 1.1 fvdl static __inline void ahd_extract_mode_state(struct ahd_softc *ahd,
69 1.1 fvdl ahd_mode_state state,
70 1.1 fvdl ahd_mode *src, ahd_mode *dst);
71 1.1 fvdl static __inline void ahd_set_modes(struct ahd_softc *ahd, ahd_mode src,
72 1.1 fvdl ahd_mode dst);
73 1.1 fvdl static __inline void ahd_update_modes(struct ahd_softc *ahd);
74 1.1 fvdl static __inline void ahd_assert_modes(struct ahd_softc *ahd, ahd_mode srcmode,
75 1.1 fvdl ahd_mode dstmode, const char *file,
76 1.1 fvdl int line);
77 1.1 fvdl static __inline ahd_mode_state ahd_save_modes(struct ahd_softc *ahd);
78 1.1 fvdl static __inline void ahd_restore_modes(struct ahd_softc *ahd,
79 1.1 fvdl ahd_mode_state state);
80 1.1 fvdl static __inline int ahd_is_paused(struct ahd_softc *ahd);
81 1.1 fvdl static __inline void ahd_pause(struct ahd_softc *ahd);
82 1.1 fvdl static __inline void ahd_unpause(struct ahd_softc *ahd);
83 1.1 fvdl
84 1.1 fvdl static __inline void
85 1.1 fvdl ahd_known_modes(struct ahd_softc *ahd, ahd_mode src, ahd_mode dst)
86 1.1 fvdl {
87 1.1 fvdl ahd->src_mode = src;
88 1.1 fvdl ahd->dst_mode = dst;
89 1.1 fvdl ahd->saved_src_mode = src;
90 1.1 fvdl ahd->saved_dst_mode = dst;
91 1.1 fvdl }
92 1.1 fvdl
93 1.1 fvdl static __inline ahd_mode_state
94 1.1 fvdl ahd_build_mode_state(struct ahd_softc *ahd, ahd_mode src, ahd_mode dst)
95 1.1 fvdl {
96 1.1 fvdl return ((src << SRC_MODE_SHIFT) | (dst << DST_MODE_SHIFT));
97 1.1 fvdl }
98 1.1 fvdl
99 1.1 fvdl static __inline void
100 1.1 fvdl ahd_extract_mode_state(struct ahd_softc *ahd, ahd_mode_state state,
101 1.1 fvdl ahd_mode *src, ahd_mode *dst)
102 1.1 fvdl {
103 1.1 fvdl *src = (state & SRC_MODE) >> SRC_MODE_SHIFT;
104 1.1 fvdl *dst = (state & DST_MODE) >> DST_MODE_SHIFT;
105 1.1 fvdl }
106 1.1 fvdl
107 1.1 fvdl static __inline void
108 1.1 fvdl ahd_set_modes(struct ahd_softc *ahd, ahd_mode src, ahd_mode dst)
109 1.1 fvdl {
110 1.1 fvdl if (ahd->src_mode == src && ahd->dst_mode == dst)
111 1.1 fvdl return;
112 1.1 fvdl #ifdef AHD_DEBUG
113 1.1 fvdl if (ahd->src_mode == AHD_MODE_UNKNOWN
114 1.1 fvdl || ahd->dst_mode == AHD_MODE_UNKNOWN)
115 1.1 fvdl panic("Setting mode prior to saving it.\n");
116 1.1 fvdl if ((ahd_debug & AHD_SHOW_MODEPTR) != 0)
117 1.1 fvdl printf("%s: Setting mode 0x%x\n", ahd_name(ahd),
118 1.1 fvdl ahd_build_mode_state(ahd, src, dst));
119 1.1 fvdl #endif
120 1.1 fvdl ahd_outb(ahd, MODE_PTR, ahd_build_mode_state(ahd, src, dst));
121 1.1 fvdl ahd->src_mode = src;
122 1.1 fvdl ahd->dst_mode = dst;
123 1.1 fvdl }
124 1.1 fvdl
125 1.1 fvdl static __inline void
126 1.1 fvdl ahd_update_modes(struct ahd_softc *ahd)
127 1.1 fvdl {
128 1.1 fvdl ahd_mode_state mode_ptr;
129 1.1 fvdl ahd_mode src;
130 1.1 fvdl ahd_mode dst;
131 1.1 fvdl
132 1.1 fvdl mode_ptr = ahd_inb(ahd, MODE_PTR);
133 1.1 fvdl #ifdef AHD_DEBUG
134 1.1 fvdl if ((ahd_debug & AHD_SHOW_MODEPTR) != 0)
135 1.1 fvdl printf("Reading mode 0x%x\n", mode_ptr);
136 1.1 fvdl #endif
137 1.1 fvdl ahd_extract_mode_state(ahd, mode_ptr, &src, &dst);
138 1.1 fvdl ahd_known_modes(ahd, src, dst);
139 1.1 fvdl }
140 1.1 fvdl
141 1.1 fvdl static __inline void
142 1.1 fvdl ahd_assert_modes(struct ahd_softc *ahd, ahd_mode srcmode,
143 1.1 fvdl ahd_mode dstmode, const char *file, int line)
144 1.1 fvdl {
145 1.1 fvdl #ifdef AHD_DEBUG
146 1.1 fvdl if ((srcmode & AHD_MK_MSK(ahd->src_mode)) == 0
147 1.1 fvdl || (dstmode & AHD_MK_MSK(ahd->dst_mode)) == 0) {
148 1.1 fvdl panic("%s:%s:%d: Mode assertion failed.\n",
149 1.1 fvdl ahd_name(ahd), file, line);
150 1.1 fvdl }
151 1.1 fvdl #endif
152 1.1 fvdl }
153 1.1 fvdl
154 1.1 fvdl static __inline ahd_mode_state
155 1.1 fvdl ahd_save_modes(struct ahd_softc *ahd)
156 1.1 fvdl {
157 1.1 fvdl if (ahd->src_mode == AHD_MODE_UNKNOWN
158 1.1 fvdl || ahd->dst_mode == AHD_MODE_UNKNOWN)
159 1.1 fvdl ahd_update_modes(ahd);
160 1.1 fvdl
161 1.1 fvdl return (ahd_build_mode_state(ahd, ahd->src_mode, ahd->dst_mode));
162 1.1 fvdl }
163 1.1 fvdl
164 1.1 fvdl static __inline void
165 1.1 fvdl ahd_restore_modes(struct ahd_softc *ahd, ahd_mode_state state)
166 1.1 fvdl {
167 1.1 fvdl ahd_mode src;
168 1.1 fvdl ahd_mode dst;
169 1.1 fvdl
170 1.1 fvdl ahd_extract_mode_state(ahd, state, &src, &dst);
171 1.1 fvdl ahd_set_modes(ahd, src, dst);
172 1.1 fvdl }
173 1.1 fvdl
174 1.1 fvdl #define AHD_ASSERT_MODES(ahd, source, dest) \
175 1.1 fvdl ahd_assert_modes(ahd, source, dest, __FILE__, __LINE__);
176 1.1 fvdl
177 1.1 fvdl /*
178 1.1 fvdl * Determine whether the sequencer has halted code execution.
179 1.1 fvdl * Returns non-zero status if the sequencer is stopped.
180 1.1 fvdl */
181 1.1 fvdl static __inline int
182 1.1 fvdl ahd_is_paused(struct ahd_softc *ahd)
183 1.1 fvdl {
184 1.1 fvdl return ((ahd_inb(ahd, HCNTRL) & PAUSE) != 0);
185 1.1 fvdl }
186 1.1 fvdl
187 1.1 fvdl /*
188 1.1 fvdl * Request that the sequencer stop and wait, indefinitely, for it
189 1.1 fvdl * to stop. The sequencer will only acknowledge that it is paused
190 1.1 fvdl * once it has reached an instruction boundary and PAUSEDIS is
191 1.1 fvdl * cleared in the SEQCTL register. The sequencer may use PAUSEDIS
192 1.1 fvdl * for critical sections.
193 1.1 fvdl */
194 1.1 fvdl static __inline void
195 1.1 fvdl ahd_pause(struct ahd_softc *ahd)
196 1.1 fvdl {
197 1.1 fvdl ahd_outb(ahd, HCNTRL, ahd->pause);
198 1.1 fvdl
199 1.1 fvdl /*
200 1.1 fvdl * Since the sequencer can disable pausing in a critical section, we
201 1.1 fvdl * must loop until it actually stops.
202 1.1 fvdl */
203 1.1 fvdl while (ahd_is_paused(ahd) == 0)
204 1.1 fvdl ;
205 1.1 fvdl }
206 1.1 fvdl
207 1.1 fvdl /*
208 1.1 fvdl * Allow the sequencer to continue program execution.
209 1.1 fvdl * We check here to ensure that no additional interrupt
210 1.1 fvdl * sources that would cause the sequencer to halt have been
211 1.1 fvdl * asserted. If, for example, a SCSI bus reset is detected
212 1.1 fvdl * while we are fielding a different, pausing, interrupt type,
213 1.1 fvdl * we don't want to release the sequencer before going back
214 1.1 fvdl * into our interrupt handler and dealing with this new
215 1.1 fvdl * condition.
216 1.1 fvdl */
217 1.1 fvdl static __inline void
218 1.1 fvdl ahd_unpause(struct ahd_softc *ahd)
219 1.1 fvdl {
220 1.1 fvdl /*
221 1.1 fvdl * Automatically restore our modes to those saved
222 1.1 fvdl * prior to the first change of the mode.
223 1.1 fvdl */
224 1.1 fvdl if (ahd->saved_src_mode != AHD_MODE_UNKNOWN
225 1.1 fvdl && ahd->saved_dst_mode != AHD_MODE_UNKNOWN) {
226 1.1 fvdl if ((ahd->flags & AHD_UPDATE_PEND_CMDS) != 0)
227 1.1 fvdl ahd_reset_cmds_pending(ahd);
228 1.1 fvdl ahd_set_modes(ahd, ahd->saved_src_mode, ahd->saved_dst_mode);
229 1.1 fvdl }
230 1.1 fvdl
231 1.1 fvdl if ((ahd_inb(ahd, INTSTAT) & ~(SWTMINT | CMDCMPLT)) == 0)
232 1.1 fvdl ahd_outb(ahd, HCNTRL, ahd->unpause);
233 1.1 fvdl
234 1.1 fvdl ahd_known_modes(ahd, AHD_MODE_UNKNOWN, AHD_MODE_UNKNOWN);
235 1.1 fvdl }
236 1.1 fvdl
237 1.1 fvdl /*********************** Scatter Gather List Handling *************************/
238 1.1 fvdl static __inline void *ahd_sg_setup(struct ahd_softc *ahd, struct scb *scb,
239 1.1 fvdl void *sgptr, bus_addr_t addr,
240 1.1 fvdl bus_size_t len, int last);
241 1.1 fvdl static __inline void ahd_setup_scb_common(struct ahd_softc *ahd,
242 1.1 fvdl struct scb *scb);
243 1.1 fvdl static __inline void ahd_setup_data_scb(struct ahd_softc *ahd,
244 1.1 fvdl struct scb *scb);
245 1.1 fvdl static __inline void ahd_setup_noxfer_scb(struct ahd_softc *ahd,
246 1.1 fvdl struct scb *scb);
247 1.1 fvdl
248 1.1 fvdl static __inline void *
249 1.1 fvdl ahd_sg_setup(struct ahd_softc *ahd, struct scb *scb,
250 1.1 fvdl void *sgptr, bus_addr_t addr, bus_size_t len, int last)
251 1.1 fvdl {
252 1.1 fvdl scb->sg_count++;
253 1.1 fvdl if (sizeof(bus_addr_t) > 4
254 1.1 fvdl && (ahd->flags & AHD_64BIT_ADDRESSING) != 0) {
255 1.1 fvdl struct ahd_dma64_seg *sg;
256 1.1 fvdl
257 1.1 fvdl sg = (struct ahd_dma64_seg *)sgptr;
258 1.1 fvdl sg->addr = ahd_htole64(addr);
259 1.1 fvdl sg->len = ahd_htole32(len | (last ? AHD_DMA_LAST_SEG : 0));
260 1.1 fvdl return (sg + 1);
261 1.1 fvdl } else {
262 1.1 fvdl struct ahd_dma_seg *sg;
263 1.1 fvdl
264 1.1 fvdl sg = (struct ahd_dma_seg *)sgptr;
265 1.1 fvdl sg->addr = ahd_htole32(addr & 0xFFFFFFFF);
266 1.1 fvdl sg->len = ahd_htole32(len | ((addr >> 8) & 0x7F000000)
267 1.1 fvdl | (last ? AHD_DMA_LAST_SEG : 0));
268 1.1 fvdl return (sg + 1);
269 1.1 fvdl }
270 1.1 fvdl }
271 1.1 fvdl
272 1.1 fvdl static __inline void
273 1.1 fvdl ahd_setup_scb_common(struct ahd_softc *ahd, struct scb *scb)
274 1.1 fvdl {
275 1.1 fvdl /* XXX Handle target mode SCBs. */
276 1.1 fvdl scb->crc_retry_count = 0;
277 1.1 fvdl if ((scb->flags & SCB_PACKETIZED) != 0) {
278 1.1 fvdl /* XXX what about ACA?? It is type 4, but TAG_TYPE == 0x3. */
279 1.1 fvdl scb->hscb->task_attribute= scb->hscb->control & SCB_TAG_TYPE;
280 1.1 fvdl /*
281 1.1 fvdl * For Rev A short lun workaround.
282 1.1 fvdl */
283 1.1 fvdl memset(scb->hscb->pkt_long_lun, 0, sizeof(scb->hscb->pkt_long_lun));
284 1.1 fvdl scb->hscb->pkt_long_lun[6] = scb->hscb->lun;
285 1.1 fvdl }
286 1.1 fvdl
287 1.1 fvdl if (scb->hscb->cdb_len <= MAX_CDB_LEN_WITH_SENSE_ADDR
288 1.1 fvdl || (scb->hscb->cdb_len & SCB_CDB_LEN_PTR) != 0)
289 1.1 fvdl scb->hscb->shared_data.idata.cdb_plus_saddr.sense_addr =
290 1.1 fvdl ahd_htole32(scb->sense_busaddr);
291 1.1 fvdl }
292 1.1 fvdl
293 1.1 fvdl static __inline void
294 1.1 fvdl ahd_setup_data_scb(struct ahd_softc *ahd, struct scb *scb)
295 1.1 fvdl {
296 1.1 fvdl /*
297 1.1 fvdl * Copy the first SG into the "current" data ponter area.
298 1.1 fvdl */
299 1.1 fvdl if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0) {
300 1.1 fvdl struct ahd_dma64_seg *sg;
301 1.1 fvdl
302 1.1 fvdl sg = (struct ahd_dma64_seg *)scb->sg_list;
303 1.1 fvdl scb->hscb->dataptr = sg->addr;
304 1.1 fvdl scb->hscb->datacnt = sg->len;
305 1.1 fvdl } else {
306 1.1 fvdl struct ahd_dma_seg *sg;
307 1.1 fvdl
308 1.1 fvdl sg = (struct ahd_dma_seg *)scb->sg_list;
309 1.1 fvdl scb->hscb->dataptr = sg->addr;
310 1.1 fvdl if ((ahd->flags & AHD_39BIT_ADDRESSING) != 0) {
311 1.1 fvdl uint64_t high_addr;
312 1.1 fvdl
313 1.1 fvdl high_addr = ahd_le32toh(sg->len) & 0x7F000000;
314 1.1 fvdl scb->hscb->dataptr |= ahd_htole64(high_addr << 8);
315 1.1 fvdl }
316 1.1 fvdl scb->hscb->datacnt = sg->len;
317 1.1 fvdl }
318 1.1 fvdl /*
319 1.1 fvdl * Note where to find the SG entries in bus space.
320 1.1 fvdl * We also set the full residual flag which the
321 1.1 fvdl * sequencer will clear as soon as a data transfer
322 1.1 fvdl * occurs.
323 1.1 fvdl */
324 1.1 fvdl scb->hscb->sgptr = ahd_htole32(scb->sg_list_busaddr|SG_FULL_RESID);
325 1.1 fvdl }
326 1.1 fvdl
327 1.1 fvdl static __inline void
328 1.1 fvdl ahd_setup_noxfer_scb(struct ahd_softc *ahd, struct scb *scb)
329 1.1 fvdl {
330 1.1 fvdl scb->hscb->sgptr = ahd_htole32(SG_LIST_NULL);
331 1.1 fvdl scb->hscb->dataptr = 0;
332 1.1 fvdl scb->hscb->datacnt = 0;
333 1.1 fvdl }
334 1.1 fvdl
335 1.1 fvdl /************************** Memory mapping routines ***************************/
336 1.1 fvdl static __inline size_t ahd_sg_size(struct ahd_softc *ahd);
337 1.1 fvdl static __inline void *
338 1.1 fvdl ahd_sg_bus_to_virt(struct ahd_softc *ahd,
339 1.1 fvdl struct scb *scb,
340 1.1 fvdl uint32_t sg_busaddr);
341 1.1 fvdl static __inline uint32_t
342 1.1 fvdl ahd_sg_virt_to_bus(struct ahd_softc *ahd,
343 1.1 fvdl struct scb *scb,
344 1.1 fvdl void *sg);
345 1.1 fvdl static __inline void ahd_sync_scb(struct ahd_softc *ahd,
346 1.1 fvdl struct scb *scb, int op);
347 1.1 fvdl static __inline void ahd_sync_sglist(struct ahd_softc *ahd,
348 1.1 fvdl struct scb *scb, int op);
349 1.1 fvdl static __inline void ahd_sync_sense(struct ahd_softc *ahd,
350 1.1 fvdl struct scb *scb, int op);
351 1.1 fvdl static __inline uint32_t
352 1.1 fvdl ahd_targetcmd_offset(struct ahd_softc *ahd,
353 1.1 fvdl u_int index);
354 1.1 fvdl
355 1.1 fvdl static __inline size_t
356 1.1 fvdl ahd_sg_size(struct ahd_softc *ahd)
357 1.1 fvdl {
358 1.1 fvdl if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0)
359 1.1 fvdl return (sizeof(struct ahd_dma64_seg));
360 1.1 fvdl return (sizeof(struct ahd_dma_seg));
361 1.1 fvdl }
362 1.1 fvdl
363 1.1 fvdl static __inline void *
364 1.1 fvdl ahd_sg_bus_to_virt(struct ahd_softc *ahd, struct scb *scb, uint32_t sg_busaddr)
365 1.1 fvdl {
366 1.1 fvdl bus_addr_t sg_offset;
367 1.1 fvdl
368 1.1 fvdl /* sg_list_phys points to entry 1, not 0 */
369 1.1 fvdl sg_offset = sg_busaddr - (scb->sg_list_busaddr - ahd_sg_size(ahd));
370 1.1 fvdl return ((uint8_t *)scb->sg_list + sg_offset);
371 1.1 fvdl }
372 1.1 fvdl
373 1.1 fvdl static __inline uint32_t
374 1.1 fvdl ahd_sg_virt_to_bus(struct ahd_softc *ahd, struct scb *scb, void *sg)
375 1.1 fvdl {
376 1.1 fvdl bus_addr_t sg_offset;
377 1.1 fvdl
378 1.1 fvdl /* sg_list_phys points to entry 1, not 0 */
379 1.1 fvdl sg_offset = ((uint8_t *)sg - (uint8_t *)scb->sg_list)
380 1.1 fvdl - ahd_sg_size(ahd);
381 1.1 fvdl
382 1.1 fvdl return (scb->sg_list_busaddr + sg_offset);
383 1.1 fvdl }
384 1.1 fvdl
385 1.1 fvdl static __inline void
386 1.1 fvdl ahd_sync_scb(struct ahd_softc *ahd, struct scb *scb, int op)
387 1.1 fvdl {
388 1.1 fvdl ahd_dmamap_sync(ahd, ahd->parent_dmat, scb->hscb_map->dmamap,
389 1.1 fvdl /*offset*/(uint8_t*)scb->hscb - scb->hscb_map->vaddr,
390 1.1 fvdl /*len*/sizeof(*scb->hscb), op);
391 1.1 fvdl }
392 1.1 fvdl
393 1.1 fvdl static __inline void
394 1.1 fvdl ahd_sync_sglist(struct ahd_softc *ahd, struct scb *scb, int op)
395 1.1 fvdl {
396 1.1 fvdl if (scb->sg_count == 0)
397 1.1 fvdl return;
398 1.1 fvdl
399 1.1 fvdl ahd_dmamap_sync(ahd, ahd->parent_dmat, scb->sg_map->dmamap,
400 1.1 fvdl /*offset*/scb->sg_list_busaddr - ahd_sg_size(ahd),
401 1.1 fvdl /*len*/ahd_sg_size(ahd) * scb->sg_count, op);
402 1.1 fvdl }
403 1.1 fvdl
404 1.1 fvdl static __inline void
405 1.1 fvdl ahd_sync_sense(struct ahd_softc *ahd, struct scb *scb, int op)
406 1.1 fvdl {
407 1.1 fvdl ahd_dmamap_sync(ahd, ahd->parent_dmat,
408 1.1 fvdl scb->sense_map->dmamap,
409 1.1 fvdl /*offset*/scb->sense_busaddr,
410 1.1 fvdl /*len*/AHD_SENSE_BUFSIZE, op);
411 1.1 fvdl }
412 1.1 fvdl
413 1.1 fvdl static __inline uint32_t
414 1.1 fvdl ahd_targetcmd_offset(struct ahd_softc *ahd, u_int index)
415 1.1 fvdl {
416 1.1 fvdl return (((uint8_t *)&ahd->targetcmds[index])
417 1.1 fvdl - (uint8_t *)ahd->qoutfifo);
418 1.1 fvdl }
419 1.1 fvdl
420 1.1 fvdl /*********************** Miscelaneous Support Functions ***********************/
421 1.1 fvdl static __inline void ahd_complete_scb(struct ahd_softc *ahd,
422 1.1 fvdl struct scb *scb);
423 1.1 fvdl static __inline void ahd_update_residual(struct ahd_softc *ahd,
424 1.1 fvdl struct scb *scb);
425 1.1 fvdl static __inline struct ahd_initiator_tinfo *
426 1.1 fvdl ahd_fetch_transinfo(struct ahd_softc *ahd,
427 1.1 fvdl char channel, u_int our_id,
428 1.1 fvdl u_int remote_id,
429 1.1 fvdl struct ahd_tmode_tstate **tstate);
430 1.1 fvdl static __inline uint16_t
431 1.1 fvdl ahd_inw(struct ahd_softc *ahd, u_int port);
432 1.1 fvdl static __inline void ahd_outw(struct ahd_softc *ahd, u_int port,
433 1.1 fvdl u_int value);
434 1.1 fvdl static __inline uint32_t
435 1.1 fvdl ahd_inl(struct ahd_softc *ahd, u_int port);
436 1.1 fvdl static __inline void ahd_outl(struct ahd_softc *ahd, u_int port,
437 1.1 fvdl uint32_t value);
438 1.1 fvdl static __inline uint64_t
439 1.1 fvdl ahd_inq(struct ahd_softc *ahd, u_int port);
440 1.1 fvdl static __inline void ahd_outq(struct ahd_softc *ahd, u_int port,
441 1.1 fvdl uint64_t value);
442 1.1 fvdl static __inline u_int ahd_get_scbptr(struct ahd_softc *ahd);
443 1.1 fvdl static __inline void ahd_set_scbptr(struct ahd_softc *ahd, u_int scbptr);
444 1.1 fvdl static __inline u_int ahd_get_hnscb_qoff(struct ahd_softc *ahd);
445 1.1 fvdl static __inline void ahd_set_hnscb_qoff(struct ahd_softc *ahd, u_int value);
446 1.1 fvdl static __inline u_int ahd_get_hescb_qoff(struct ahd_softc *ahd);
447 1.1 fvdl static __inline void ahd_set_hescb_qoff(struct ahd_softc *ahd, u_int value);
448 1.1 fvdl static __inline u_int ahd_get_snscb_qoff(struct ahd_softc *ahd);
449 1.1 fvdl static __inline void ahd_set_snscb_qoff(struct ahd_softc *ahd, u_int value);
450 1.1 fvdl static __inline u_int ahd_get_sescb_qoff(struct ahd_softc *ahd);
451 1.1 fvdl static __inline void ahd_set_sescb_qoff(struct ahd_softc *ahd, u_int value);
452 1.1 fvdl static __inline u_int ahd_get_sdscb_qoff(struct ahd_softc *ahd);
453 1.1 fvdl static __inline void ahd_set_sdscb_qoff(struct ahd_softc *ahd, u_int value);
454 1.1 fvdl static __inline u_int ahd_inb_scbram(struct ahd_softc *ahd, u_int offset);
455 1.1 fvdl static __inline u_int ahd_inw_scbram(struct ahd_softc *ahd, u_int offset);
456 1.1 fvdl static __inline uint32_t
457 1.1 fvdl ahd_inl_scbram(struct ahd_softc *ahd, u_int offset);
458 1.1 fvdl static __inline void ahd_swap_with_next_hscb(struct ahd_softc *ahd,
459 1.1 fvdl struct scb *scb);
460 1.1 fvdl static __inline void ahd_queue_scb(struct ahd_softc *ahd, struct scb *scb);
461 1.1 fvdl static __inline uint8_t *
462 1.1 fvdl ahd_get_sense_buf(struct ahd_softc *ahd,
463 1.1 fvdl struct scb *scb);
464 1.1 fvdl static __inline uint32_t
465 1.1 fvdl ahd_get_sense_bufaddr(struct ahd_softc *ahd,
466 1.1 fvdl struct scb *scb);
467 1.1 fvdl static __inline void ahd_post_scb(struct ahd_softc *ahd,
468 1.1 fvdl struct scb *scb);
469 1.1 fvdl
470 1.1 fvdl
471 1.1 fvdl static __inline void
472 1.1 fvdl ahd_post_scb(struct ahd_softc *ahd, struct scb *scb)
473 1.1 fvdl {
474 1.1 fvdl uint32_t sgptr;
475 1.1 fvdl
476 1.1 fvdl sgptr = ahd_le32toh(scb->hscb->sgptr);
477 1.1 fvdl if ((sgptr & SG_STATUS_VALID) != 0)
478 1.1 fvdl ahd_handle_scb_status(ahd, scb);
479 1.1 fvdl else
480 1.1 fvdl ahd_done(ahd, scb);
481 1.1 fvdl }
482 1.1 fvdl
483 1.1 fvdl static __inline void
484 1.1 fvdl ahd_complete_scb(struct ahd_softc *ahd, struct scb *scb)
485 1.1 fvdl {
486 1.1 fvdl uint32_t sgptr;
487 1.1 fvdl
488 1.1 fvdl sgptr = ahd_le32toh(scb->hscb->sgptr);
489 1.1 fvdl if ((sgptr & SG_STATUS_VALID) != 0)
490 1.1 fvdl ahd_handle_scb_status(ahd, scb);
491 1.1 fvdl else
492 1.1 fvdl ahd_done(ahd, scb);
493 1.1 fvdl }
494 1.1 fvdl
495 1.1 fvdl /*
496 1.1 fvdl * Determine whether the sequencer reported a residual
497 1.1 fvdl * for this SCB/transaction.
498 1.1 fvdl */
499 1.1 fvdl static __inline void
500 1.1 fvdl ahd_update_residual(struct ahd_softc *ahd, struct scb *scb)
501 1.1 fvdl {
502 1.1 fvdl uint32_t sgptr;
503 1.1 fvdl
504 1.1 fvdl sgptr = ahd_le32toh(scb->hscb->sgptr);
505 1.1 fvdl if ((sgptr & SG_STATUS_VALID) != 0)
506 1.1 fvdl ahd_calc_residual(ahd, scb);
507 1.1 fvdl }
508 1.1 fvdl
509 1.1 fvdl /*
510 1.1 fvdl * Return pointers to the transfer negotiation information
511 1.1 fvdl * for the specified our_id/remote_id pair.
512 1.1 fvdl */
513 1.1 fvdl static __inline struct ahd_initiator_tinfo *
514 1.1 fvdl ahd_fetch_transinfo(struct ahd_softc *ahd, char channel, u_int our_id,
515 1.1 fvdl u_int remote_id, struct ahd_tmode_tstate **tstate)
516 1.1 fvdl {
517 1.1 fvdl /*
518 1.1 fvdl * Transfer data structures are stored from the perspective
519 1.1 fvdl * of the target role. Since the parameters for a connection
520 1.1 fvdl * in the initiator role to a given target are the same as
521 1.1 fvdl * when the roles are reversed, we pretend we are the target.
522 1.1 fvdl */
523 1.1 fvdl if (channel == 'B')
524 1.1 fvdl our_id += 8;
525 1.1 fvdl *tstate = ahd->enabled_targets[our_id];
526 1.1 fvdl return (&(*tstate)->transinfo[remote_id]);
527 1.1 fvdl }
528 1.1 fvdl
529 1.1 fvdl #define AHD_COPY_COL_IDX(dst, src) \
530 1.1 fvdl do { \
531 1.1 fvdl dst->hscb->scsiid = src->hscb->scsiid; \
532 1.1 fvdl dst->hscb->lun = src->hscb->lun; \
533 1.1 fvdl } while (0)
534 1.1 fvdl
535 1.1 fvdl static __inline uint16_t
536 1.1 fvdl ahd_inw(struct ahd_softc *ahd, u_int port)
537 1.1 fvdl {
538 1.1 fvdl return ((ahd_inb(ahd, port+1) << 8) | ahd_inb(ahd, port));
539 1.1 fvdl }
540 1.1 fvdl
541 1.1 fvdl static __inline void
542 1.1 fvdl ahd_outw(struct ahd_softc *ahd, u_int port, u_int value)
543 1.1 fvdl {
544 1.1 fvdl ahd_outb(ahd, port, value & 0xFF);
545 1.1 fvdl ahd_outb(ahd, port+1, (value >> 8) & 0xFF);
546 1.1 fvdl }
547 1.1 fvdl
548 1.1 fvdl static __inline uint32_t
549 1.1 fvdl ahd_inl(struct ahd_softc *ahd, u_int port)
550 1.1 fvdl {
551 1.1 fvdl return ((ahd_inb(ahd, port))
552 1.1 fvdl | (ahd_inb(ahd, port+1) << 8)
553 1.1 fvdl | (ahd_inb(ahd, port+2) << 16)
554 1.1 fvdl | (ahd_inb(ahd, port+3) << 24));
555 1.1 fvdl }
556 1.1 fvdl
557 1.1 fvdl static __inline void
558 1.1 fvdl ahd_outl(struct ahd_softc *ahd, u_int port, uint32_t value)
559 1.1 fvdl {
560 1.1 fvdl ahd_outb(ahd, port, (value) & 0xFF);
561 1.1 fvdl ahd_outb(ahd, port+1, ((value) >> 8) & 0xFF);
562 1.1 fvdl ahd_outb(ahd, port+2, ((value) >> 16) & 0xFF);
563 1.1 fvdl ahd_outb(ahd, port+3, ((value) >> 24) & 0xFF);
564 1.1 fvdl }
565 1.1 fvdl
566 1.1 fvdl static __inline uint64_t
567 1.1 fvdl ahd_inq(struct ahd_softc *ahd, u_int port)
568 1.1 fvdl {
569 1.1 fvdl return ((ahd_inb(ahd, port))
570 1.1 fvdl | (ahd_inb(ahd, port+1) << 8)
571 1.1 fvdl | (ahd_inb(ahd, port+2) << 16)
572 1.1 fvdl | (ahd_inb(ahd, port+3) << 24)
573 1.1 fvdl | (((uint64_t)ahd_inb(ahd, port+4)) << 32)
574 1.1 fvdl | (((uint64_t)ahd_inb(ahd, port+5)) << 40)
575 1.1 fvdl | (((uint64_t)ahd_inb(ahd, port+6)) << 48)
576 1.1 fvdl | (((uint64_t)ahd_inb(ahd, port+7)) << 56));
577 1.1 fvdl }
578 1.1 fvdl
579 1.1 fvdl static __inline void
580 1.1 fvdl ahd_outq(struct ahd_softc *ahd, u_int port, uint64_t value)
581 1.1 fvdl {
582 1.1 fvdl ahd_outb(ahd, port, value & 0xFF);
583 1.1 fvdl ahd_outb(ahd, port+1, (value >> 8) & 0xFF);
584 1.1 fvdl ahd_outb(ahd, port+2, (value >> 16) & 0xFF);
585 1.1 fvdl ahd_outb(ahd, port+3, (value >> 24) & 0xFF);
586 1.1 fvdl ahd_outb(ahd, port+4, (value >> 32) & 0xFF);
587 1.1 fvdl ahd_outb(ahd, port+5, (value >> 40) & 0xFF);
588 1.1 fvdl ahd_outb(ahd, port+6, (value >> 48) & 0xFF);
589 1.1 fvdl ahd_outb(ahd, port+7, (value >> 56) & 0xFF);
590 1.1 fvdl }
591 1.1 fvdl
592 1.1 fvdl static __inline u_int
593 1.1 fvdl ahd_get_scbptr(struct ahd_softc *ahd)
594 1.1 fvdl {
595 1.1 fvdl AHD_ASSERT_MODES(ahd, ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK),
596 1.1 fvdl ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK));
597 1.1 fvdl return (ahd_inb(ahd, SCBPTR) | (ahd_inb(ahd, SCBPTR + 1) << 8));
598 1.1 fvdl }
599 1.1 fvdl
600 1.1 fvdl static __inline void
601 1.1 fvdl ahd_set_scbptr(struct ahd_softc *ahd, u_int scbptr)
602 1.1 fvdl {
603 1.1 fvdl AHD_ASSERT_MODES(ahd, ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK),
604 1.1 fvdl ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK));
605 1.1 fvdl ahd_outb(ahd, SCBPTR, scbptr & 0xFF);
606 1.1 fvdl ahd_outb(ahd, SCBPTR+1, (scbptr >> 8) & 0xFF);
607 1.1 fvdl }
608 1.1 fvdl
609 1.1 fvdl static __inline u_int
610 1.1 fvdl ahd_get_hnscb_qoff(struct ahd_softc *ahd)
611 1.1 fvdl {
612 1.1 fvdl return (ahd_inw_atomic(ahd, HNSCB_QOFF));
613 1.1 fvdl }
614 1.1 fvdl
615 1.1 fvdl static __inline void
616 1.1 fvdl ahd_set_hnscb_qoff(struct ahd_softc *ahd, u_int value)
617 1.1 fvdl {
618 1.1 fvdl ahd_outw_atomic(ahd, HNSCB_QOFF, value);
619 1.1 fvdl }
620 1.1 fvdl
621 1.1 fvdl static __inline u_int
622 1.1 fvdl ahd_get_hescb_qoff(struct ahd_softc *ahd)
623 1.1 fvdl {
624 1.1 fvdl return (ahd_inb(ahd, HESCB_QOFF));
625 1.1 fvdl }
626 1.1 fvdl
627 1.1 fvdl static __inline void
628 1.1 fvdl ahd_set_hescb_qoff(struct ahd_softc *ahd, u_int value)
629 1.1 fvdl {
630 1.1 fvdl ahd_outb(ahd, HESCB_QOFF, value);
631 1.1 fvdl }
632 1.1 fvdl
633 1.1 fvdl static __inline u_int
634 1.1 fvdl ahd_get_snscb_qoff(struct ahd_softc *ahd)
635 1.1 fvdl {
636 1.1 fvdl u_int oldvalue;
637 1.1 fvdl
638 1.1 fvdl AHD_ASSERT_MODES(ahd, AHD_MODE_CCHAN_MSK, AHD_MODE_CCHAN_MSK);
639 1.1 fvdl oldvalue = ahd_inw(ahd, SNSCB_QOFF);
640 1.1 fvdl ahd_outw(ahd, SNSCB_QOFF, oldvalue);
641 1.1 fvdl return (oldvalue);
642 1.1 fvdl }
643 1.1 fvdl
644 1.1 fvdl static __inline void
645 1.1 fvdl ahd_set_snscb_qoff(struct ahd_softc *ahd, u_int value)
646 1.1 fvdl {
647 1.1 fvdl AHD_ASSERT_MODES(ahd, AHD_MODE_CCHAN_MSK, AHD_MODE_CCHAN_MSK);
648 1.1 fvdl ahd_outw(ahd, SNSCB_QOFF, value);
649 1.1 fvdl }
650 1.1 fvdl
651 1.1 fvdl static __inline u_int
652 1.1 fvdl ahd_get_sescb_qoff(struct ahd_softc *ahd)
653 1.1 fvdl {
654 1.1 fvdl AHD_ASSERT_MODES(ahd, AHD_MODE_CCHAN_MSK, AHD_MODE_CCHAN_MSK);
655 1.1 fvdl return (ahd_inb(ahd, SESCB_QOFF));
656 1.1 fvdl }
657 1.1 fvdl
658 1.1 fvdl static __inline void
659 1.1 fvdl ahd_set_sescb_qoff(struct ahd_softc *ahd, u_int value)
660 1.1 fvdl {
661 1.1 fvdl AHD_ASSERT_MODES(ahd, AHD_MODE_CCHAN_MSK, AHD_MODE_CCHAN_MSK);
662 1.1 fvdl ahd_outb(ahd, SESCB_QOFF, value);
663 1.1 fvdl }
664 1.1 fvdl
665 1.1 fvdl static __inline u_int
666 1.1 fvdl ahd_get_sdscb_qoff(struct ahd_softc *ahd)
667 1.1 fvdl {
668 1.1 fvdl AHD_ASSERT_MODES(ahd, AHD_MODE_CCHAN_MSK, AHD_MODE_CCHAN_MSK);
669 1.1 fvdl return (ahd_inb(ahd, SDSCB_QOFF) | (ahd_inb(ahd, SDSCB_QOFF + 1) << 8));
670 1.1 fvdl }
671 1.1 fvdl
672 1.1 fvdl static __inline void
673 1.1 fvdl ahd_set_sdscb_qoff(struct ahd_softc *ahd, u_int value)
674 1.1 fvdl {
675 1.1 fvdl AHD_ASSERT_MODES(ahd, AHD_MODE_CCHAN_MSK, AHD_MODE_CCHAN_MSK);
676 1.1 fvdl ahd_outb(ahd, SDSCB_QOFF, value & 0xFF);
677 1.1 fvdl ahd_outb(ahd, SDSCB_QOFF+1, (value >> 8) & 0xFF);
678 1.1 fvdl }
679 1.1 fvdl
680 1.1 fvdl static __inline u_int
681 1.1 fvdl ahd_inb_scbram(struct ahd_softc *ahd, u_int offset)
682 1.1 fvdl {
683 1.1 fvdl u_int value;
684 1.1 fvdl
685 1.1 fvdl /*
686 1.1 fvdl * Workaround PCI-X Rev A. hardware bug.
687 1.1 fvdl * After a host read of SCB memory, the chip
688 1.1 fvdl * may become confused into thinking prefetch
689 1.1 fvdl * was required. This starts the discard timer
690 1.1 fvdl * running and can cause an unexpected discard
691 1.1 fvdl * timer interrupt. The work around is to read
692 1.1 fvdl * a normal register prior to the exhaustion of
693 1.1 fvdl * the discard timer. The mode pointer register
694 1.1 fvdl * has no side effects and so serves well for
695 1.1 fvdl * this purpose.
696 1.1 fvdl *
697 1.1 fvdl * Razor #528
698 1.1 fvdl */
699 1.1 fvdl value = ahd_inb(ahd, offset);
700 1.1 fvdl if ((ahd->flags & AHD_PCIX_SCBRAM_RD_BUG) != 0)
701 1.1 fvdl ahd_inb(ahd, MODE_PTR);
702 1.1 fvdl return (value);
703 1.1 fvdl }
704 1.1 fvdl
705 1.1 fvdl static __inline u_int
706 1.1 fvdl ahd_inw_scbram(struct ahd_softc *ahd, u_int offset)
707 1.1 fvdl {
708 1.1 fvdl return (ahd_inb_scbram(ahd, offset)
709 1.1 fvdl | (ahd_inb_scbram(ahd, offset+1) << 8));
710 1.1 fvdl }
711 1.1 fvdl
712 1.1 fvdl static __inline uint32_t
713 1.1 fvdl ahd_inl_scbram(struct ahd_softc *ahd, u_int offset)
714 1.1 fvdl {
715 1.1 fvdl return (ahd_inb_scbram(ahd, offset)
716 1.1 fvdl | (ahd_inb_scbram(ahd, offset+1) << 8)
717 1.1 fvdl | (ahd_inb_scbram(ahd, offset+2) << 16)
718 1.1 fvdl | (ahd_inb_scbram(ahd, offset+3) << 24));
719 1.1 fvdl }
720 1.1 fvdl
721 1.1 fvdl static __inline struct scb *
722 1.1 fvdl ahd_lookup_scb(struct ahd_softc *ahd, u_int tag)
723 1.1 fvdl {
724 1.1 fvdl struct scb* scb;
725 1.1 fvdl
726 1.1 fvdl if (tag >= AHD_SCB_MAX)
727 1.1 fvdl return (NULL);
728 1.1 fvdl scb = ahd->scb_data.scbindex[tag];
729 1.1 fvdl if (scb != NULL)
730 1.1 fvdl ahd_sync_scb(ahd, scb,
731 1.1 fvdl BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
732 1.1 fvdl return (scb);
733 1.1 fvdl }
734 1.1 fvdl
735 1.1 fvdl static __inline void
736 1.1 fvdl ahd_swap_with_next_hscb(struct ahd_softc *ahd, struct scb *scb)
737 1.1 fvdl {
738 1.1 fvdl struct hardware_scb *q_hscb;
739 1.1 fvdl uint32_t saved_hscb_busaddr;
740 1.1 fvdl
741 1.1 fvdl /*
742 1.1 fvdl * Our queuing method is a bit tricky. The card
743 1.1 fvdl * knows in advance which HSCB (by address) to download,
744 1.1 fvdl * and we can't disappoint it. To achieve this, the next
745 1.1 fvdl * HSCB to download is saved off in ahd->next_queued_hscb.
746 1.1 fvdl * When we are called to queue "an arbitrary scb",
747 1.1 fvdl * we copy the contents of the incoming HSCB to the one
748 1.1 fvdl * the sequencer knows about, swap HSCB pointers and
749 1.1 fvdl * finally assign the SCB to the tag indexed location
750 1.1 fvdl * in the scb_array. This makes sure that we can still
751 1.1 fvdl * locate the correct SCB by SCB_TAG.
752 1.1 fvdl */
753 1.1 fvdl q_hscb = ahd->next_queued_hscb;
754 1.1 fvdl saved_hscb_busaddr = q_hscb->hscb_busaddr;
755 1.1 fvdl memcpy(q_hscb, scb->hscb, sizeof(*scb->hscb));
756 1.1 fvdl q_hscb->hscb_busaddr = saved_hscb_busaddr;
757 1.1 fvdl q_hscb->next_hscb_busaddr = scb->hscb->hscb_busaddr;
758 1.1 fvdl
759 1.1 fvdl /* Now swap HSCB pointers. */
760 1.1 fvdl ahd->next_queued_hscb = scb->hscb;
761 1.1 fvdl scb->hscb = q_hscb;
762 1.1 fvdl
763 1.1 fvdl /* Now define the mapping from tag to SCB in the scbindex */
764 1.1 fvdl ahd->scb_data.scbindex[SCB_GET_TAG(scb)] = scb;
765 1.1 fvdl }
766 1.1 fvdl
767 1.1 fvdl /*
768 1.1 fvdl * Tell the sequencer about a new transaction to execute.
769 1.1 fvdl */
770 1.1 fvdl static __inline void
771 1.1 fvdl ahd_queue_scb(struct ahd_softc *ahd, struct scb *scb)
772 1.1 fvdl {
773 1.1 fvdl ahd_swap_with_next_hscb(ahd, scb);
774 1.1 fvdl
775 1.1 fvdl if (SCBID_IS_NULL(SCB_GET_TAG(scb)))
776 1.1 fvdl panic("Attempt to queue invalid SCB tag %x\n",
777 1.1 fvdl SCB_GET_TAG(scb));
778 1.1 fvdl
779 1.1 fvdl /*
780 1.1 fvdl * Keep a history of SCBs we've downloaded in the qinfifo.
781 1.1 fvdl */
782 1.1 fvdl ahd->qinfifo[AHD_QIN_WRAP(ahd->qinfifonext)] = SCB_GET_TAG(scb);
783 1.1 fvdl ahd->qinfifonext++;
784 1.1 fvdl
785 1.1 fvdl if (scb->sg_count != 0)
786 1.1 fvdl ahd_setup_data_scb(ahd, scb);
787 1.1 fvdl else
788 1.1 fvdl ahd_setup_noxfer_scb(ahd, scb);
789 1.1 fvdl ahd_setup_scb_common(ahd, scb);
790 1.1 fvdl
791 1.1 fvdl /*
792 1.1 fvdl * Make sure our data is consistent from the
793 1.1 fvdl * perspective of the adapter.
794 1.1 fvdl */
795 1.1 fvdl ahd_sync_scb(ahd, scb, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
796 1.1 fvdl
797 1.1 fvdl #ifdef AHD_DEBUG
798 1.1 fvdl if ((ahd_debug & AHD_SHOW_QUEUE) != 0) {
799 1.1 fvdl printf("%s: Queueing SCB 0x%x bus addr 0x%x - 0x%x%x/0x%x\n",
800 1.1 fvdl ahd_name(ahd),
801 1.1 fvdl SCB_GET_TAG(scb), scb->hscb->hscb_busaddr,
802 1.1 fvdl (u_int)((scb->hscb->dataptr >> 32) & 0xFFFFFFFF),
803 1.1 fvdl (u_int)(scb->hscb->dataptr & 0xFFFFFFFF),
804 1.1 fvdl scb->hscb->datacnt);
805 1.1 fvdl }
806 1.1 fvdl #endif
807 1.1 fvdl /* Tell the adapter about the newly queued SCB */
808 1.1 fvdl ahd_set_hnscb_qoff(ahd, ahd->qinfifonext);
809 1.1 fvdl }
810 1.1 fvdl
811 1.1 fvdl static __inline uint8_t *
812 1.1 fvdl ahd_get_sense_buf(struct ahd_softc *ahd, struct scb *scb)
813 1.1 fvdl {
814 1.1 fvdl return (scb->sense_data);
815 1.1 fvdl }
816 1.1 fvdl
817 1.1 fvdl static __inline uint32_t
818 1.1 fvdl ahd_get_sense_bufaddr(struct ahd_softc *ahd, struct scb *scb)
819 1.1 fvdl {
820 1.1 fvdl return (scb->sense_busaddr);
821 1.1 fvdl }
822 1.1 fvdl
823 1.1 fvdl /************************** Interrupt Processing ******************************/
824 1.1 fvdl static __inline void ahd_sync_qoutfifo(struct ahd_softc *ahd, int op);
825 1.1 fvdl static __inline void ahd_sync_tqinfifo(struct ahd_softc *ahd, int op);
826 1.1 fvdl static __inline u_int ahd_check_cmdcmpltqueues(struct ahd_softc *ahd);
827 1.1 fvdl static __inline int ahd_intr(void *arg);
828 1.1 fvdl static __inline void ahd_minphys(struct buf *bp);
829 1.1 fvdl
830 1.1 fvdl static __inline void
831 1.1 fvdl ahd_sync_qoutfifo(struct ahd_softc *ahd, int op)
832 1.1 fvdl {
833 1.1 fvdl ahd_dmamap_sync(ahd, ahd->parent_dmat, ahd->shared_data_dmamap,
834 1.1 fvdl /*offset*/0, /*len*/AHD_SCB_MAX * sizeof(uint16_t), op);
835 1.1 fvdl }
836 1.1 fvdl
837 1.1 fvdl static __inline void
838 1.1 fvdl ahd_sync_tqinfifo(struct ahd_softc *ahd, int op)
839 1.1 fvdl {
840 1.1 fvdl #ifdef AHD_TARGET_MODE
841 1.1 fvdl if ((ahd->flags & AHD_TARGETROLE) != 0) {
842 1.1 fvdl ahd_dmamap_sync(ahd, ahd->parent_dmat /*shared_data_dmat*/,
843 1.1 fvdl ahd->shared_data_dmamap,
844 1.1 fvdl ahd_targetcmd_offset(ahd, 0),
845 1.1 fvdl sizeof(struct target_cmd) * AHD_TMODE_CMDS,
846 1.1 fvdl op);
847 1.1 fvdl }
848 1.1 fvdl #endif
849 1.1 fvdl }
850 1.1 fvdl
851 1.1 fvdl /*
852 1.1 fvdl * See if the firmware has posted any completed commands
853 1.1 fvdl * into our in-core command complete fifos.
854 1.1 fvdl */
855 1.1 fvdl #define AHD_RUN_QOUTFIFO 0x1
856 1.1 fvdl #define AHD_RUN_TQINFIFO 0x2
857 1.1 fvdl static __inline u_int
858 1.1 fvdl ahd_check_cmdcmpltqueues(struct ahd_softc *ahd)
859 1.1 fvdl {
860 1.1 fvdl u_int retval;
861 1.1 fvdl
862 1.1 fvdl retval = 0;
863 1.1 fvdl ahd_dmamap_sync(ahd, ahd->parent_dmat /*shared_data_dmat*/, ahd->shared_data_dmamap,
864 1.1 fvdl /*offset*/ahd->qoutfifonext, /*len*/2,
865 1.1 fvdl BUS_DMASYNC_POSTREAD);
866 1.1 fvdl if ((ahd->qoutfifo[ahd->qoutfifonext]
867 1.1 fvdl & QOUTFIFO_ENTRY_VALID_LE) == ahd->qoutfifonext_valid_tag)
868 1.1 fvdl retval |= AHD_RUN_QOUTFIFO;
869 1.1 fvdl #ifdef AHD_TARGET_MODE
870 1.1 fvdl if ((ahd->flags & AHD_TARGETROLE) != 0
871 1.1 fvdl && (ahd->flags & AHD_TQINFIFO_BLOCKED) == 0) {
872 1.1 fvdl ahd_dmamap_sync(ahd, ahd->parent_dmat /*shared_data_dmat*/,
873 1.1 fvdl ahd->shared_data_dmamap,
874 1.1 fvdl ahd_targetcmd_offset(ahd, ahd->tqinfifofnext),
875 1.1 fvdl /*len*/sizeof(struct target_cmd),
876 1.1 fvdl BUS_DMASYNC_POSTREAD);
877 1.1 fvdl if (ahd->targetcmds[ahd->tqinfifonext].cmd_valid != 0)
878 1.1 fvdl retval |= AHD_RUN_TQINFIFO;
879 1.1 fvdl }
880 1.1 fvdl #endif
881 1.1 fvdl return (retval);
882 1.1 fvdl }
883 1.1 fvdl
884 1.1 fvdl /*
885 1.1 fvdl * Catch an interrupt from the adapter
886 1.1 fvdl */
887 1.1 fvdl static __inline int
888 1.1 fvdl ahd_intr(void *arg)
889 1.1 fvdl {
890 1.1 fvdl struct ahd_softc *ahd = (struct ahd_softc*)arg;
891 1.1 fvdl u_int intstat;
892 1.1 fvdl
893 1.1 fvdl if ((ahd->pause & INTEN) == 0) {
894 1.1 fvdl /*
895 1.1 fvdl * Our interrupt is not enabled on the chip
896 1.1 fvdl * and may be disabled for re-entrancy reasons,
897 1.1 fvdl * so just return. This is likely just a shared
898 1.1 fvdl * interrupt.
899 1.1 fvdl */
900 1.1 fvdl return 0;
901 1.1 fvdl }
902 1.1 fvdl
903 1.1 fvdl /*
904 1.1 fvdl * Instead of directly reading the interrupt status register,
905 1.1 fvdl * infer the cause of the interrupt by checking our in-core
906 1.1 fvdl * completion queues. This avoids a costly PCI bus read in
907 1.1 fvdl * most cases.
908 1.1 fvdl */
909 1.1 fvdl if ((ahd->flags & AHD_ALL_INTERRUPTS) == 0
910 1.1 fvdl && (ahd_check_cmdcmpltqueues(ahd) != 0))
911 1.1 fvdl intstat = CMDCMPLT;
912 1.1 fvdl else
913 1.1 fvdl intstat = ahd_inb(ahd, INTSTAT);
914 1.1 fvdl
915 1.1 fvdl if (intstat & CMDCMPLT) {
916 1.1 fvdl ahd_outb(ahd, CLRINT, CLRCMDINT);
917 1.1 fvdl
918 1.1 fvdl /*
919 1.1 fvdl * Ensure that the chip sees that we've cleared
920 1.1 fvdl * this interrupt before we walk the output fifo.
921 1.1 fvdl * Otherwise, we may, due to posted bus writes,
922 1.1 fvdl * clear the interrupt after we finish the scan,
923 1.1 fvdl * and after the sequencer has added new entries
924 1.1 fvdl * and asserted the interrupt again.
925 1.1 fvdl */
926 1.1 fvdl if ((ahd->bugs & AHD_INTCOLLISION_BUG) != 0) {
927 1.1 fvdl if (ahd_is_paused(ahd)) {
928 1.1 fvdl /*
929 1.1 fvdl * Potentially lost SEQINT.
930 1.1 fvdl * If SEQINTCODE is non-zero,
931 1.1 fvdl * simulate the SEQINT.
932 1.1 fvdl */
933 1.1 fvdl if (ahd_inb(ahd, SEQINTCODE) != NO_SEQINT)
934 1.1 fvdl intstat |= SEQINT;
935 1.1 fvdl }
936 1.1 fvdl } else {
937 1.1 fvdl ahd_flush_device_writes(ahd);
938 1.1 fvdl }
939 1.1 fvdl scsipi_channel_freeze(&ahd->sc_channel, 1);
940 1.1 fvdl ahd_run_qoutfifo(ahd);
941 1.1 fvdl scsipi_channel_thaw(&ahd->sc_channel, 1);
942 1.1 fvdl ahd->cmdcmplt_counts[ahd->cmdcmplt_bucket]++;
943 1.1 fvdl ahd->cmdcmplt_total++;
944 1.1 fvdl #ifdef AHD_TARGET_MODE
945 1.1 fvdl if ((ahd->flags & AHD_TARGETROLE) != 0)
946 1.1 fvdl ahd_run_tqinfifo(ahd, /*paused*/FALSE);
947 1.1 fvdl #endif
948 1.1 fvdl if (intstat == CMDCMPLT)
949 1.1 fvdl return 1;
950 1.1 fvdl }
951 1.1 fvdl
952 1.1 fvdl if (intstat == 0xFF && (ahd->features & AHD_REMOVABLE) != 0)
953 1.1 fvdl /* Hot eject */
954 1.1 fvdl return 1;
955 1.1 fvdl
956 1.1 fvdl if ((intstat & INT_PEND) == 0)
957 1.1 fvdl return 1;
958 1.1 fvdl
959 1.1 fvdl if (intstat & HWERRINT) {
960 1.1 fvdl ahd_handle_hwerrint(ahd);
961 1.1 fvdl return 1;
962 1.1 fvdl }
963 1.1 fvdl
964 1.1 fvdl if ((intstat & (PCIINT|SPLTINT)) != 0) {
965 1.1 fvdl ahd->bus_intr(ahd);
966 1.1 fvdl return 1;
967 1.1 fvdl }
968 1.1 fvdl
969 1.1 fvdl if ((intstat & (SEQINT)) != 0) {
970 1.1 fvdl ahd_handle_seqint(ahd, intstat);
971 1.1 fvdl return 1;
972 1.1 fvdl }
973 1.1 fvdl
974 1.1 fvdl if ((intstat & SCSIINT) != 0) {
975 1.1 fvdl ahd_handle_scsiint(ahd, intstat);
976 1.1 fvdl return 1;
977 1.1 fvdl }
978 1.1 fvdl
979 1.1 fvdl return 1;
980 1.1 fvdl }
981 1.1 fvdl
982 1.1 fvdl static __inline void
983 1.1 fvdl ahd_minphys(bp)
984 1.1 fvdl struct buf *bp;
985 1.1 fvdl {
986 1.1 fvdl /*
987 1.1 fvdl * Even though the card can transfer up to 16megs per command
988 1.2 wiz * we are limited by the number of segments in the DMA segment
989 1.1 fvdl * list that we can hold. The worst case is that all pages are
990 1.1 fvdl * discontinuous physically, hense the "page per segment" limit
991 1.1 fvdl * enforced here.
992 1.1 fvdl */
993 1.1 fvdl if (bp->b_bcount > AHD_MAXTRANSFER_SIZE) {
994 1.1 fvdl bp->b_bcount = AHD_MAXTRANSFER_SIZE;
995 1.1 fvdl }
996 1.1 fvdl minphys(bp);
997 1.1 fvdl }
998 1.1 fvdl
999 1.1 fvdl static __inline u_int32_t scsi_4btoul(u_int8_t *bytes);
1000 1.1 fvdl
1001 1.1 fvdl static __inline u_int32_t
1002 1.1 fvdl scsi_4btoul(u_int8_t *bytes)
1003 1.1 fvdl {
1004 1.1 fvdl u_int32_t rv;
1005 1.1 fvdl
1006 1.1 fvdl rv = (bytes[0] << 24) |
1007 1.1 fvdl (bytes[1] << 16) |
1008 1.1 fvdl (bytes[2] << 8) |
1009 1.1 fvdl bytes[3];
1010 1.1 fvdl return (rv);
1011 1.1 fvdl }
1012 1.1 fvdl
1013 1.1 fvdl
1014 1.1 fvdl #endif /* _AIC79XX_INLINE_H_ */
1015