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