ahc_eisa.c:
[dragonfly.git] / sys / dev / disk / aic7xxx / aic79xx.h
1 /*
2  * Core definitions and data structures shareable across OS platforms.
3  *
4  * Copyright (c) 1994-2002 Justin T. Gibbs.
5  * Copyright (c) 2000-2002 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.h#101 $
41  *
42  * $FreeBSD: src/sys/dev/aic7xxx/aic79xx.h,v 1.17 2003/12/17 00:02:09 gibbs Exp $
43  * $DragonFly: src/sys/dev/disk/aic7xxx/aic79xx.h,v 1.6 2007/07/06 00:01:16 pavalos Exp $
44  */
45
46 #ifndef _AIC79XX_H_
47 #define _AIC79XX_H_
48
49 /* Register Definitions */
50 #include "aic79xx_reg.h"
51
52 /************************* Forward Declarations *******************************/
53 struct ahd_platform_data;
54 struct scb_platform_data;
55
56 /****************************** Useful Macros *********************************/
57 #ifndef MAX
58 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
59 #endif
60
61 #ifndef MIN
62 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
63 #endif
64
65 #ifndef TRUE
66 #define TRUE 1
67 #endif
68 #ifndef FALSE
69 #define FALSE 0
70 #endif
71
72 #define NUM_ELEMENTS(array) (sizeof(array) / sizeof(*array))
73
74 #define ALL_CHANNELS '\0'
75 #define ALL_TARGETS_MASK 0xFFFF
76 #define INITIATOR_WILDCARD      (~0)
77 #define SCB_LIST_NULL           0xFF00
78 #define SCB_LIST_NULL_LE        (aic_htole16(SCB_LIST_NULL))
79 #define QOUTFIFO_ENTRY_VALID 0x8000
80 #define QOUTFIFO_ENTRY_VALID_LE (aic_htole16(0x8000))
81 #define SCBID_IS_NULL(scbid) (((scbid) & 0xFF00 ) == SCB_LIST_NULL)
82
83 #define SCSIID_TARGET(ahd, scsiid)      \
84         (((scsiid) & TID) >> TID_SHIFT)
85 #define SCSIID_OUR_ID(scsiid)           \
86         ((scsiid) & OID)
87 #define SCSIID_CHANNEL(ahd, scsiid) ('A')
88 #define SCB_IS_SCSIBUS_B(ahd, scb) (0)
89 #define SCB_GET_OUR_ID(scb) \
90         SCSIID_OUR_ID((scb)->hscb->scsiid)
91 #define SCB_GET_TARGET(ahd, scb) \
92         SCSIID_TARGET((ahd), (scb)->hscb->scsiid)
93 #define SCB_GET_CHANNEL(ahd, scb) \
94         SCSIID_CHANNEL(ahd, (scb)->hscb->scsiid)
95 #define SCB_GET_LUN(scb) \
96         ((scb)->hscb->lun)
97 #define SCB_GET_TARGET_OFFSET(ahd, scb) \
98         SCB_GET_TARGET(ahd, scb)
99 #define SCB_GET_TARGET_MASK(ahd, scb) \
100         (0x01 << (SCB_GET_TARGET_OFFSET(ahd, scb)))
101 #ifdef AHD_DEBUG
102 #define SCB_IS_SILENT(scb)                                      \
103         ((ahd_debug & AHD_SHOW_MASKED_ERRORS) == 0              \
104       && (((scb)->flags & SCB_SILENT) != 0))
105 #else
106 #define SCB_IS_SILENT(scb)                                      \
107         (((scb)->flags & SCB_SILENT) != 0)
108 #endif
109 /*
110  * TCLs have the following format: TTTTLLLLLLLL
111  */
112 #define TCL_TARGET_OFFSET(tcl) \
113         ((((tcl) >> 4) & TID) >> 4)
114 #define TCL_LUN(tcl) \
115         (tcl & (AHD_NUM_LUNS - 1))
116 #define BUILD_TCL(scsiid, lun) \
117         ((lun) | (((scsiid) & TID) << 4))
118 #define BUILD_TCL_RAW(target, channel, lun) \
119         ((lun) | ((target) << 8))
120
121 #define SCB_GET_TAG(scb) \
122         aic_le16toh(scb->hscb->tag)
123
124 #ifndef AHD_TARGET_MODE
125 #undef  AHD_TMODE_ENABLE
126 #define AHD_TMODE_ENABLE 0
127 #endif
128
129 #define AHD_BUILD_COL_IDX(target, lun)                          \
130         (((lun) << 4) | target)
131
132 #define AHD_GET_SCB_COL_IDX(ahd, scb)                           \
133         ((SCB_GET_LUN(scb) << 4) | SCB_GET_TARGET(ahd, scb))
134
135 #define AHD_SET_SCB_COL_IDX(scb, col_idx)                               \
136 do {                                                                    \
137         (scb)->hscb->scsiid = ((col_idx) << TID_SHIFT) & TID;           \
138         (scb)->hscb->lun = ((col_idx) >> 4) & (AHD_NUM_LUNS_NONPKT-1);  \
139 } while (0)
140
141 #define AHD_COPY_SCB_COL_IDX(dst, src)                          \
142 do {                                                            \
143         dst->hscb->scsiid = src->hscb->scsiid;                  \
144         dst->hscb->lun = src->hscb->lun;                        \
145 } while (0)
146
147 #define AHD_NEVER_COL_IDX 0xFFFF
148
149 /**************************** Driver Constants ********************************/
150 /*
151  * The maximum number of supported targets.
152  */
153 #define AHD_NUM_TARGETS 16
154
155 /*
156  * The maximum number of supported luns.
157  * The identify message only supports 64 luns in non-packetized transfers.
158  * You can have 2^64 luns when information unit transfers are enabled,
159  * but until we see a need to support that many, we support 256.
160  */
161 #define AHD_NUM_LUNS_NONPKT 64
162 #define AHD_NUM_LUNS 256
163
164 /*
165  * The maximum transfer per S/G segment.
166  */
167 #define AHD_MAXTRANSFER_SIZE     0x00ffffff     /* limited by 24bit counter */
168
169 /*
170  * The maximum amount of SCB storage in hardware on a controller.
171  * This value represents an upper bound.  Due to software design,
172  * we may not be able to use this number.
173  */
174 #define AHD_SCB_MAX     512
175
176 /*
177  * The maximum number of concurrent transactions supported per driver instance.
178  * Sequencer Control Blocks (SCBs) store per-transaction information.
179  */
180 #define AHD_MAX_QUEUE   AHD_SCB_MAX
181
182 /*
183  * Define the size of our QIN and QOUT FIFOs.  They must be a power of 2
184  * in size and accommodate as many transactions as can be queued concurrently.
185  */
186 #define AHD_QIN_SIZE    AHD_MAX_QUEUE
187 #define AHD_QOUT_SIZE   AHD_MAX_QUEUE
188
189 #define AHD_QIN_WRAP(x) ((x) & (AHD_QIN_SIZE-1))
190 /*
191  * The maximum amount of SCB storage we allocate in host memory.
192  */
193 #define AHD_SCB_MAX_ALLOC AHD_MAX_QUEUE
194
195 /*
196  * Ring Buffer of incoming target commands.
197  * We allocate 256 to simplify the logic in the sequencer
198  * by using the natural wrap point of an 8bit counter.
199  */
200 #define AHD_TMODE_CMDS  256
201
202 /* Reset line assertion time in us */
203 #define AHD_BUSRESET_DELAY      25
204
205 /******************* Chip Characteristics/Operating Settings  *****************/
206 /*
207  * Chip Type
208  * The chip order is from least sophisticated to most sophisticated.
209  */
210 typedef enum {
211         AHD_NONE        = 0x0000,
212         AHD_CHIPID_MASK = 0x00FF,
213         AHD_AIC7901     = 0x0001,
214         AHD_AIC7902     = 0x0002,
215         AHD_AIC7901A    = 0x0003,
216         AHD_PCI         = 0x0100,       /* Bus type PCI */
217         AHD_PCIX        = 0x0200,       /* Bus type PCIX */
218         AHD_BUS_MASK    = 0x0F00
219 } ahd_chip;
220
221 /*
222  * Features available in each chip type.
223  */
224 typedef enum {
225         AHD_FENONE              = 0x00000,
226         AHD_WIDE                = 0x00001,/* Wide Channel */
227         AHD_MULTI_FUNC          = 0x00100,/* Multi-Function/Channel Device */
228         AHD_TARGETMODE          = 0x01000,/* Has tested target mode support */
229         AHD_MULTIROLE           = 0x02000,/* Space for two roles at a time */
230         AHD_RTI                 = 0x04000,/* Retained Training Support */
231         AHD_NEW_IOCELL_OPTS     = 0x08000,/* More Signal knobs in the IOCELL */
232         AHD_NEW_DFCNTRL_OPTS    = 0x10000,/* SCSIENWRDIS bit */
233         AHD_FAST_CDB_DELIVERY   = 0x20000,/* CDB acks released to Output Sync */
234         AHD_REMOVABLE           = 0x00000,/* Hot-Swap supported - None so far*/
235         AHD_AIC7901_FE          = AHD_FENONE,
236         AHD_AIC7901A_FE         = AHD_FENONE,
237         AHD_AIC7902_FE          = AHD_MULTI_FUNC
238 } ahd_feature;
239
240 /*
241  * Bugs in the silicon that we work around in software.
242  */
243 typedef enum {
244         AHD_BUGNONE             = 0x0000,
245         /*
246          * Rev A hardware fails to update LAST/CURR/NEXTSCB
247          * correctly in certain packetized selection cases.
248          */
249         AHD_SENT_SCB_UPDATE_BUG = 0x0001,
250         /* The wrong SCB is accessed to check the abort pending bit. */
251         AHD_ABORT_LQI_BUG       = 0x0002,
252         /* Packetized bitbucket crosses packet boundaries. */
253         AHD_PKT_BITBUCKET_BUG   = 0x0004,
254         /* The selection timer runs twice as long as its setting. */
255         AHD_LONG_SETIMO_BUG     = 0x0008,
256         /* The Non-LQ CRC error status is delayed until phase change. */
257         AHD_NLQICRC_DELAYED_BUG = 0x0010,
258         /* The chip must be reset for all outgoing bus resets.  */
259         AHD_SCSIRST_BUG         = 0x0020,
260         /* Some PCIX fields must be saved and restored across chip reset. */
261         AHD_PCIX_CHIPRST_BUG    = 0x0040,
262         /* MMAPIO is not functional in PCI-X mode.  */
263         AHD_PCIX_MMAPIO_BUG     = 0x0080,
264         /* Reads to SCBRAM fail to reset the discard timer. */
265         AHD_PCIX_SCBRAM_RD_BUG  = 0x0100,
266         /* Bug workarounds that can be disabled on non-PCIX busses. */
267         AHD_PCIX_BUG_MASK       = AHD_PCIX_CHIPRST_BUG
268                                 | AHD_PCIX_MMAPIO_BUG
269                                 | AHD_PCIX_SCBRAM_RD_BUG,
270         /*
271          * LQOSTOP0 status set even for forced selections with ATN
272          * to perform non-packetized message delivery.
273          */
274         AHD_LQO_ATNO_BUG        = 0x0200,
275         /* FIFO auto-flush does not always trigger.  */
276         AHD_AUTOFLUSH_BUG       = 0x0400,
277         /* The CLRLQO registers are not self-clearing. */
278         AHD_CLRLQO_AUTOCLR_BUG  = 0x0800,
279         /* The PACKETIZED status bit refers to the previous connection. */
280         AHD_PKTIZED_STATUS_BUG  = 0x1000,
281         /* "Short Luns" are not placed into outgoing LQ packets correctly. */
282         AHD_PKT_LUN_BUG         = 0x2000,
283         /*
284          * Only the FIFO allocated to the non-packetized connection may
285          * be in use during a non-packetzied connection.
286          */
287         AHD_NONPACKFIFO_BUG     = 0x4000,
288         /*
289          * Writing to a DFF SCBPTR register may fail if concurent with
290          * a hardware write to the other DFF SCBPTR register.  This is
291          * not currently a concern in our sequencer since all chips with
292          * this bug have the AHD_NONPACKFIFO_BUG and all writes of concern
293          * occur in non-packetized connections.
294          */
295         AHD_MDFF_WSCBPTR_BUG    = 0x8000,
296         /* SGHADDR updates are slow. */
297         AHD_REG_SLOW_SETTLE_BUG = 0x10000,
298         /*
299          * Changing the MODE_PTR coincident with an interrupt that
300          * switches to a different mode will cause the interrupt to
301          * be in the mode written outside of interrupt context.
302          */
303         AHD_SET_MODE_BUG        = 0x20000,
304         /* Non-packetized busfree revision does not work. */
305         AHD_BUSFREEREV_BUG      = 0x40000,
306         /*
307          * Paced transfers are indicated with a non-standard PPR
308          * option bit in the neg table, 160MHz is indicated by
309          * sync factor 0x7, and the offset if off by a factor of 2.
310          */
311         AHD_PACED_NEGTABLE_BUG  = 0x80000,
312         /* LQOOVERRUN false positives. */
313         AHD_LQOOVERRUN_BUG      = 0x100000,
314         /*
315          * Controller write to INTSTAT will lose to a host
316          * write to CLRINT.
317          */
318         AHD_INTCOLLISION_BUG    = 0x200000,
319         /*
320          * The GEM318 violates the SCSI spec by not waiting
321          * the mandated bus settle delay between phase changes
322          * in some situations.  Some aic79xx chip revs. are more
323          * strict in this regard and will treat REQ assertions
324          * that fall within the bus settle delay window as
325          * glitches.  This flag tells the firmware to tolerate
326          * early REQ assertions.
327          */
328         AHD_EARLY_REQ_BUG       = 0x400000,
329         /*
330          * The LED does not stay on long enough in packetized modes.
331          */
332         AHD_FAINT_LED_BUG       = 0x800000
333 } ahd_bug;
334
335 /*
336  * Configuration specific settings.
337  * The driver determines these settings by probing the
338  * chip/controller's configuration.
339  */
340 typedef enum {
341         AHD_FNONE             = 0x00000,
342         AHD_BOOT_CHANNEL      = 0x00001,/* We were set as the boot channel. */
343         AHD_USEDEFAULTS       = 0x00004,/*
344                                          * For cards without an seeprom
345                                          * or a BIOS to initialize the chip's
346                                          * SRAM, we use the default target
347                                          * settings.
348                                          */
349         AHD_SEQUENCER_DEBUG   = 0x00008,
350         AHD_RESET_BUS_A       = 0x00010,
351         AHD_EXTENDED_TRANS_A  = 0x00020,
352         AHD_TERM_ENB_A        = 0x00040,
353         AHD_SPCHK_ENB_A       = 0x00080,
354         AHD_STPWLEVEL_A       = 0x00100,
355         AHD_INITIATORROLE     = 0x00200,/*
356                                          * Allow initiator operations on
357                                          * this controller.
358                                          */
359         AHD_TARGETROLE        = 0x00400,/*
360                                          * Allow target operations on this
361                                          * controller.
362                                          */
363         AHD_RESOURCE_SHORTAGE = 0x00800,
364         AHD_TQINFIFO_BLOCKED  = 0x01000,/* Blocked waiting for ATIOs */
365         AHD_INT50_SPEEDFLEX   = 0x02000,/*
366                                          * Internal 50pin connector
367                                          * sits behind an aic3860
368                                          */
369         AHD_BIOS_ENABLED      = 0x04000,
370         AHD_ALL_INTERRUPTS    = 0x08000,
371         AHD_39BIT_ADDRESSING  = 0x10000,/* Use 39 bit addressing scheme. */
372         AHD_64BIT_ADDRESSING  = 0x20000,/* Use 64 bit addressing scheme. */
373         AHD_CURRENT_SENSING   = 0x40000,
374         AHD_SCB_CONFIG_USED   = 0x80000,/* No SEEPROM but SCB had info. */
375         AHD_HP_BOARD          = 0x100000,
376         AHD_RESET_POLL_ACTIVE = 0x200000,
377         AHD_UPDATE_PEND_CMDS  = 0x400000,
378         AHD_RUNNING_QOUTFIFO  = 0x800000,
379         AHD_HAD_FIRST_SEL     = 0x1000000,
380         AHD_SHUTDOWN_RECOVERY = 0x2000000 /* Terminate recovery thread. */
381 } ahd_flag;
382
383 /************************* Hardware  SCB Definition ***************************/
384
385 /*
386  * The driver keeps up to MAX_SCB scb structures per card in memory.  The SCB
387  * consists of a "hardware SCB" mirroring the fields available on the card
388  * and additional information the kernel stores for each transaction.
389  *
390  * To minimize space utilization, a portion of the hardware scb stores
391  * different data during different portions of a SCSI transaction.
392  * As initialized by the host driver for the initiator role, this area
393  * contains the SCSI cdb (or a pointer to the  cdb) to be executed.  After
394  * the cdb has been presented to the target, this area serves to store
395  * residual transfer information and the SCSI status byte.
396  * For the target role, the contents of this area do not change, but
397  * still serve a different purpose than for the initiator role.  See
398  * struct target_data for details.
399  */
400
401 /*
402  * Status information embedded in the shared poriton of
403  * an SCB after passing the cdb to the target.  The kernel
404  * driver will only read this data for transactions that
405  * complete abnormally.
406  */
407 struct initiator_status {
408         uint32_t residual_datacnt;      /* Residual in the current S/G seg */
409         uint32_t residual_sgptr;        /* The next S/G for this transfer */
410         uint8_t  scsi_status;           /* Standard SCSI status byte */
411 };
412
413 struct target_status {
414         uint32_t residual_datacnt;      /* Residual in the current S/G seg */
415         uint32_t residual_sgptr;        /* The next S/G for this transfer */
416         uint8_t  scsi_status;           /* SCSI status to give to initiator */
417         uint8_t  target_phases;         /* Bitmap of phases to execute */
418         uint8_t  data_phase;            /* Data-In or Data-Out */
419         uint8_t  initiator_tag;         /* Initiator's transaction tag */
420 };
421
422 /*
423  * Initiator mode SCB shared data area.
424  * If the embedded CDB is 12 bytes or less, we embed
425  * the sense buffer address in the SCB.  This allows
426  * us to retrieve sense information without interrupting
427  * the host in packetized mode.
428  */
429 typedef uint32_t sense_addr_t;
430 #define MAX_CDB_LEN 16
431 #define MAX_CDB_LEN_WITH_SENSE_ADDR (MAX_CDB_LEN - sizeof(sense_addr_t))
432 union initiator_data {
433         struct {
434                 uint64_t cdbptr;
435                 uint8_t  cdblen;
436         } cdb_from_host;
437         uint8_t  cdb[MAX_CDB_LEN];
438         struct {
439                 uint8_t  cdb[MAX_CDB_LEN_WITH_SENSE_ADDR];
440                 sense_addr_t sense_addr;
441         } cdb_plus_saddr;
442 };
443
444 /*
445  * Target mode version of the shared data SCB segment.
446  */
447 struct target_data {
448         uint32_t spare[2];      
449         uint8_t  scsi_status;           /* SCSI status to give to initiator */
450         uint8_t  target_phases;         /* Bitmap of phases to execute */
451         uint8_t  data_phase;            /* Data-In or Data-Out */
452         uint8_t  initiator_tag;         /* Initiator's transaction tag */
453 };
454
455 struct hardware_scb {
456 /*0*/   union {
457                 union   initiator_data idata;
458                 struct  target_data tdata;
459                 struct  initiator_status istatus;
460                 struct  target_status tstatus;
461         } shared_data;
462 /*
463  * A word about residuals.
464  * The scb is presented to the sequencer with the dataptr and datacnt
465  * fields initialized to the contents of the first S/G element to
466  * transfer.  The sgptr field is initialized to the bus address for
467  * the S/G element that follows the first in the in core S/G array
468  * or'ed with the SG_FULL_RESID flag.  Sgptr may point to an invalid
469  * S/G entry for this transfer (single S/G element transfer with the
470  * first elements address and length preloaded in the dataptr/datacnt
471  * fields).  If no transfer is to occur, sgptr is set to SG_LIST_NULL.
472  * The SG_FULL_RESID flag ensures that the residual will be correctly
473  * noted even if no data transfers occur.  Once the data phase is entered,
474  * the residual sgptr and datacnt are loaded from the sgptr and the
475  * datacnt fields.  After each S/G element's dataptr and length are
476  * loaded into the hardware, the residual sgptr is advanced.  After
477  * each S/G element is expired, its datacnt field is checked to see
478  * if the LAST_SEG flag is set.  If so, SG_LIST_NULL is set in the
479  * residual sg ptr and the transfer is considered complete.  If the
480  * sequencer determines that there is a residual in the tranfer, or
481  * there is non-zero status, it will set the SG_STATUS_VALID flag in
482  * sgptr and dma the scb back into host memory.  To sumarize:
483  *
484  * Sequencer:
485  *      o A residual has occurred if SG_FULL_RESID is set in sgptr,
486  *        or residual_sgptr does not have SG_LIST_NULL set.
487  *
488  *      o We are transfering the last segment if residual_datacnt has
489  *        the SG_LAST_SEG flag set.
490  *
491  * Host:
492  *      o A residual can only have occurred if a completed scb has the
493  *        SG_STATUS_VALID flag set.  Inspection of the SCSI status field,
494  *        the residual_datacnt, and the residual_sgptr field will tell
495  *        for sure.
496  *
497  *      o residual_sgptr and sgptr refer to the "next" sg entry
498  *        and so may point beyond the last valid sg entry for the
499  *        transfer.
500  */ 
501 #define SG_PTR_MASK     0xFFFFFFF8
502 /*16*/  uint16_t tag;           /* Reused by Sequencer. */
503 /*18*/  uint8_t  control;       /* See SCB_CONTROL in aic79xx.reg for details */
504 /*19*/  uint8_t  scsiid;        /*
505                                  * Selection out Id
506                                  * Our Id (bits 0-3) Their ID (bits 4-7)
507                                  */
508 /*20*/  uint8_t  lun;
509 /*21*/  uint8_t  task_attribute;
510 /*22*/  uint8_t  cdb_len;
511 /*23*/  uint8_t  task_management;
512 /*24*/  uint64_t dataptr;
513 /*32*/  uint32_t datacnt;       /* Byte 3 is spare. */
514 /*36*/  uint32_t sgptr;
515 /*40*/  uint32_t hscb_busaddr;
516 /*44*/  uint32_t next_hscb_busaddr;
517 /********** Long lun field only downloaded for full 8 byte lun support ********/
518 /*48*/  uint8_t  pkt_long_lun[8];
519 /******* Fields below are not Downloaded (Sequencer may use for scratch) ******/
520 /*56*/  uint8_t  spare[8];
521 };
522
523 /************************ Kernel SCB Definitions ******************************/
524 /*
525  * Some fields of the SCB are OS dependent.  Here we collect the
526  * definitions for elements that all OS platforms need to include
527  * in there SCB definition.
528  */
529
530 /*
531  * Definition of a scatter/gather element as transfered to the controller.
532  * The aic7xxx chips only support a 24bit length.  We use the top byte of
533  * the length to store additional address bits and a flag to indicate
534  * that a given segment terminates the transfer.  This gives us an
535  * addressable range of 512GB on machines with 64bit PCI or with chips
536  * that can support dual address cycles on 32bit PCI busses.
537  */
538 struct ahd_dma_seg {
539         uint32_t        addr;
540         uint32_t        len;
541 #define AHD_DMA_LAST_SEG        0x80000000
542 #define AHD_SG_HIGH_ADDR_MASK   0x7F000000
543 #define AHD_SG_LEN_MASK         0x00FFFFFF
544 };
545
546 struct ahd_dma64_seg {
547         uint64_t        addr;
548         uint32_t        len;
549         uint32_t        pad;
550 };
551
552 struct map_node {
553         bus_dmamap_t             dmamap;
554         bus_addr_t               busaddr;
555         uint8_t                 *vaddr;
556         SLIST_ENTRY(map_node)    links;
557 };
558
559 /*
560  * The current state of this SCB.
561  */
562 typedef enum {
563         SCB_FLAG_NONE           = 0x00000,
564         SCB_TRANSMISSION_ERROR  = 0x00001,/*
565                                            * We detected a parity or CRC
566                                            * error that has effected the
567                                            * payload of the command.  This
568                                            * flag is checked when normal
569                                            * status is returned to catch
570                                            * the case of a target not
571                                            * responding to our attempt
572                                            * to report the error.
573                                            */
574         SCB_OTHERTCL_TIMEOUT    = 0x00002,/*
575                                            * Another device was active
576                                            * during the first timeout for
577                                            * this SCB so we gave ourselves
578                                            * an additional timeout period
579                                            * in case it was hogging the
580                                            * bus.
581                                            */
582         SCB_DEVICE_RESET        = 0x00004,
583         SCB_SENSE               = 0x00008,
584         SCB_CDB32_PTR           = 0x00010,
585         SCB_RECOVERY_SCB        = 0x00020,
586         SCB_AUTO_NEGOTIATE      = 0x00040,/* Negotiate to achieve goal. */
587         SCB_NEGOTIATE           = 0x00080,/* Negotiation forced for command. */
588         SCB_ABORT               = 0x00100,
589         SCB_ACTIVE              = 0x00200,
590         SCB_TARGET_IMMEDIATE    = 0x00400,
591         SCB_PACKETIZED          = 0x00800,
592         SCB_EXPECT_PPR_BUSFREE  = 0x01000,
593         SCB_PKT_SENSE           = 0x02000,
594         SCB_CMDPHASE_ABORT      = 0x04000,
595         SCB_ON_COL_LIST         = 0x08000,
596         SCB_SILENT              = 0x10000,/*
597                                            * Be quiet about transmission type
598                                            * errors.  They are expected and we
599                                            * don't want to upset the user.  This
600                                            * flag is typically used during DV.
601                                            */
602         SCB_TIMEDOUT            = 0x20000/*
603                                           * SCB has timed out and is on the
604                                           * timedout list.
605                                           */
606 } scb_flag;
607
608 struct scb {
609         struct  hardware_scb     *hscb;
610         union {
611                 SLIST_ENTRY(scb)  sle;
612                 LIST_ENTRY(scb)   le;
613                 TAILQ_ENTRY(scb)  tqe;
614         } links;
615         union {
616                 SLIST_ENTRY(scb)  sle;
617                 LIST_ENTRY(scb)   le;
618                 TAILQ_ENTRY(scb)  tqe;
619         } links2;
620 #define pending_links links2.le
621 #define collision_links links2.le
622         LIST_ENTRY(scb)           timedout_links;
623         struct scb               *col_scb;
624         aic_io_ctx_t              io_ctx;
625         struct ahd_softc         *ahd_softc;
626         scb_flag                  flags;
627 #ifndef __linux__
628         bus_dmamap_t              dmamap;
629 #endif
630         struct scb_platform_data *platform_data;
631         struct map_node          *hscb_map;
632         struct map_node          *sg_map;
633         struct map_node          *sense_map;
634         void                     *sg_list;
635         uint8_t                  *sense_data;
636         bus_addr_t                sg_list_busaddr;
637         bus_addr_t                sense_busaddr;
638         u_int                     sg_count;/* How full ahd_dma_seg is */
639 #define AHD_MAX_LQ_CRC_ERRORS 5
640         u_int                     crc_retry_count;
641 };
642
643 TAILQ_HEAD(scb_tailq, scb);
644 LIST_HEAD(scb_list, scb);
645
646 struct scb_data {
647         /*
648          * TAILQ of lists of free SCBs grouped by device
649          * collision domains.
650          */
651         struct scb_tailq free_scbs;
652
653         /*
654          * Per-device lists of SCBs whose tag ID would collide
655          * with an already active tag on the device.
656          */
657         struct scb_list free_scb_lists[AHD_NUM_TARGETS * AHD_NUM_LUNS_NONPKT];
658
659         /*
660          * SCBs that will not collide with any active device.
661          */
662         struct scb_list any_dev_free_scb_list;
663
664         /*
665          * Mapping from tag to SCB.
666          */
667         struct  scb *scbindex[AHD_SCB_MAX];
668
669         /*
670          * "Bus" addresses of our data structures.
671          */
672         bus_dma_tag_t    hscb_dmat;     /* dmat for our hardware SCB array */
673         bus_dma_tag_t    sg_dmat;       /* dmat for our sg segments */
674         bus_dma_tag_t    sense_dmat;    /* dmat for our sense buffers */
675         SLIST_HEAD(, map_node) hscb_maps;
676         SLIST_HEAD(, map_node) sg_maps;
677         SLIST_HEAD(, map_node) sense_maps;
678         int              scbs_left;     /* unallocated scbs in head map_node */
679         int              sgs_left;      /* unallocated sgs in head map_node */
680         int              sense_left;    /* unallocated sense in head map_node */
681         uint16_t         numscbs;
682         uint16_t         maxhscbs;      /* Number of SCBs on the card */
683         uint8_t          init_level;    /*
684                                          * How far we've initialized
685                                          * this structure.
686                                          */
687 };
688
689 /************************ Target Mode Definitions *****************************/
690
691 /*
692  * Connection desciptor for select-in requests in target mode.
693  */
694 struct target_cmd {
695         uint8_t scsiid;         /* Our ID and the initiator's ID */
696         uint8_t identify;       /* Identify message */
697         uint8_t bytes[22];      /* 
698                                  * Bytes contains any additional message
699                                  * bytes terminated by 0xFF.  The remainder
700                                  * is the cdb to execute.
701                                  */
702         uint8_t cmd_valid;      /*
703                                  * When a command is complete, the firmware
704                                  * will set cmd_valid to all bits set.
705                                  * After the host has seen the command,
706                                  * the bits are cleared.  This allows us
707                                  * to just peek at host memory to determine
708                                  * if more work is complete. cmd_valid is on
709                                  * an 8 byte boundary to simplify setting
710                                  * it on aic7880 hardware which only has
711                                  * limited direct access to the DMA FIFO.
712                                  */
713         uint8_t pad[7];
714 };
715
716 /*
717  * Number of events we can buffer up if we run out
718  * of immediate notify ccbs.
719  */
720 #define AHD_TMODE_EVENT_BUFFER_SIZE 8
721 struct ahd_tmode_event {
722         uint8_t initiator_id;
723         uint8_t event_type;     /* MSG type or EVENT_TYPE_BUS_RESET */
724 #define EVENT_TYPE_BUS_RESET 0xFF
725         uint8_t event_arg;
726 };
727
728 /*
729  * Per enabled lun target mode state.
730  * As this state is directly influenced by the host OS'es target mode
731  * environment, we let the OS module define it.  Forward declare the
732  * structure here so we can store arrays of them, etc. in OS neutral
733  * data structures.
734  */
735 #ifdef AHD_TARGET_MODE 
736 struct ahd_tmode_lstate {
737         struct cam_path *path;
738         struct ccb_hdr_slist accept_tios;
739         struct ccb_hdr_slist immed_notifies;
740         struct ahd_tmode_event event_buffer[AHD_TMODE_EVENT_BUFFER_SIZE];
741         uint8_t event_r_idx;
742         uint8_t event_w_idx;
743 };
744 #else
745 struct ahd_tmode_lstate;
746 #endif
747
748 /******************** Transfer Negotiation Datastructures *********************/
749 #define AHD_TRANS_CUR           0x01    /* Modify current neogtiation status */
750 #define AHD_TRANS_ACTIVE        0x03    /* Assume this target is on the bus */
751 #define AHD_TRANS_GOAL          0x04    /* Modify negotiation goal */
752 #define AHD_TRANS_USER          0x08    /* Modify user negotiation settings */
753 #define AHD_PERIOD_10MHz        0x19
754
755 #define AHD_WIDTH_UNKNOWN       0xFF
756 #define AHD_PERIOD_UNKNOWN      0xFF
757 #define AHD_OFFSET_UNKNOWN      0xFF
758 #define AHD_PPR_OPTS_UNKNOWN    0xFF
759
760 /*
761  * Transfer Negotiation Information.
762  */
763 struct ahd_transinfo {
764         uint8_t protocol_version;       /* SCSI Revision level */
765         uint8_t transport_version;      /* SPI Revision level */
766         uint8_t width;                  /* Bus width */
767         uint8_t period;                 /* Sync rate factor */
768         uint8_t offset;                 /* Sync offset */
769         uint8_t ppr_options;            /* Parallel Protocol Request options */
770 };
771
772 /*
773  * Per-initiator current, goal and user transfer negotiation information. */
774 struct ahd_initiator_tinfo {
775         struct ahd_transinfo curr;
776         struct ahd_transinfo goal;
777         struct ahd_transinfo user;
778 };
779
780 /*
781  * Per enabled target ID state.
782  * Pointers to lun target state as well as sync/wide negotiation information
783  * for each initiator<->target mapping.  For the initiator role we pretend
784  * that we are the target and the targets are the initiators since the
785  * negotiation is the same regardless of role.
786  */
787 struct ahd_tmode_tstate {
788         struct ahd_tmode_lstate*        enabled_luns[AHD_NUM_LUNS];
789         struct ahd_initiator_tinfo      transinfo[AHD_NUM_TARGETS];
790
791         /*
792          * Per initiator state bitmasks.
793          */
794         uint16_t         auto_negotiate;/* Auto Negotiation Required */
795         uint16_t         discenable;    /* Disconnection allowed  */
796         uint16_t         tagenable;     /* Tagged Queuing allowed */
797 };
798
799 /*
800  * Points of interest along the negotiated transfer scale.
801  */
802 #define AHD_SYNCRATE_160        0x8
803 #define AHD_SYNCRATE_PACED      0x8
804 #define AHD_SYNCRATE_DT         0x9
805 #define AHD_SYNCRATE_ULTRA2     0xa
806 #define AHD_SYNCRATE_ULTRA      0xc
807 #define AHD_SYNCRATE_FAST       0x19
808 #define AHD_SYNCRATE_MIN_DT     AHD_SYNCRATE_FAST
809 #define AHD_SYNCRATE_SYNC       0x32
810 #define AHD_SYNCRATE_MIN        0x60
811 #define AHD_SYNCRATE_ASYNC      0xFF
812 #define AHD_SYNCRATE_MAX        AHD_SYNCRATE_160
813
814 /* Safe and valid period for async negotiations. */
815 #define AHD_ASYNC_XFER_PERIOD   0x44
816
817 /*
818  * In RevA, the synctable uses a 120MHz rate for the period
819  * factor 8 and 160MHz for the period factor 7.  The 120MHz
820  * rate never made it into the official SCSI spec, so we must
821  * compensate when setting the negotiation table for Rev A
822  * parts.
823  */
824 #define AHD_SYNCRATE_REVA_120   0x8
825 #define AHD_SYNCRATE_REVA_160   0x7
826
827 /***************************** Lookup Tables **********************************/
828 /*
829  * Phase -> name and message out response
830  * to parity errors in each phase table. 
831  */
832 struct ahd_phase_table_entry {
833         uint8_t phase;
834         uint8_t mesg_out; /* Message response to parity errors */
835         char *phasemsg;
836 };
837
838 /************************** Serial EEPROM Format ******************************/
839
840 struct seeprom_config {
841 /*
842  * Per SCSI ID Configuration Flags
843  */
844         uint16_t device_flags[16];      /* words 0-15 */
845 #define         CFXFER          0x003F  /* synchronous transfer rate */
846 #define                 CFXFER_ASYNC    0x3F
847 #define         CFQAS           0x0040  /* Negotiate QAS */
848 #define         CFPACKETIZED    0x0080  /* Negotiate Packetized Transfers */
849 #define         CFSTART         0x0100  /* send start unit SCSI command */
850 #define         CFINCBIOS       0x0200  /* include in BIOS scan */
851 #define         CFDISC          0x0400  /* enable disconnection */
852 #define         CFMULTILUNDEV   0x0800  /* Probe multiple luns in BIOS scan */
853 #define         CFWIDEB         0x1000  /* wide bus device */
854 #define         CFHOSTMANAGED   0x8000  /* Managed by a RAID controller */
855
856 /*
857  * BIOS Control Bits
858  */
859         uint16_t bios_control;          /* word 16 */
860 #define         CFSUPREM        0x0001  /* support all removeable drives */
861 #define         CFSUPREMB       0x0002  /* support removeable boot drives */
862 #define         CFBIOSSTATE     0x000C  /* BIOS Action State */
863 #define             CFBS_DISABLED       0x00
864 #define             CFBS_ENABLED        0x04
865 #define             CFBS_DISABLED_SCAN  0x08
866 #define         CFENABLEDV      0x0010  /* Perform Domain Validation */
867 #define         CFCTRL_A        0x0020  /* BIOS displays Ctrl-A message */      
868 #define         CFSPARITY       0x0040  /* SCSI parity */
869 #define         CFEXTEND        0x0080  /* extended translation enabled */
870 #define         CFBOOTCD        0x0100  /* Support Bootable CD-ROM */
871 #define         CFMSG_LEVEL     0x0600  /* BIOS Message Level */
872 #define                 CFMSG_VERBOSE   0x0000
873 #define                 CFMSG_SILENT    0x0200
874 #define                 CFMSG_DIAG      0x0400
875 #define         CFRESETB        0x0800  /* reset SCSI bus at boot */
876 /*              UNUSED          0xf000  */
877
878 /*
879  * Host Adapter Control Bits
880  */
881         uint16_t adapter_control;       /* word 17 */   
882 #define         CFAUTOTERM      0x0001  /* Perform Auto termination */
883 #define         CFSTERM         0x0002  /* SCSI low byte termination */
884 #define         CFWSTERM        0x0004  /* SCSI high byte termination */
885 #define         CFSEAUTOTERM    0x0008  /* Ultra2 Perform secondary Auto Term*/
886 #define         CFSELOWTERM     0x0010  /* Ultra2 secondary low term */
887 #define         CFSEHIGHTERM    0x0020  /* Ultra2 secondary high term */
888 #define         CFSTPWLEVEL     0x0040  /* Termination level control */
889 #define         CFBIOSAUTOTERM  0x0080  /* Perform Auto termination */
890 #define         CFTERM_MENU     0x0100  /* BIOS displays termination menu */    
891 #define         CFCLUSTERENB    0x8000  /* Cluster Enable */
892
893 /*
894  * Bus Release Time, Host Adapter ID
895  */
896         uint16_t brtime_id;             /* word 18 */
897 #define         CFSCSIID        0x000f  /* host adapter SCSI ID */
898 /*              UNUSED          0x00f0  */
899 #define         CFBRTIME        0xff00  /* bus release time/PCI Latency Time */
900
901 /*
902  * Maximum targets
903  */
904         uint16_t max_targets;           /* word 19 */   
905 #define         CFMAXTARG       0x00ff  /* maximum targets */
906 #define         CFBOOTLUN       0x0f00  /* Lun to boot from */
907 #define         CFBOOTID        0xf000  /* Target to boot from */
908         uint16_t res_1[10];             /* words 20-29 */
909         uint16_t signature;             /* BIOS Signature */
910 #define         CFSIGNATURE     0x400
911         uint16_t checksum;              /* word 31 */
912 };
913
914 /*
915  * Vital Product Data used during POST and by the BIOS.
916  */
917 struct vpd_config {
918         uint8_t  bios_flags;
919 #define         VPDMASTERBIOS   0x0001
920 #define         VPDBOOTHOST     0x0002
921         uint8_t  reserved_1[21];
922         uint8_t  resource_type;
923         uint8_t  resource_len[2];
924         uint8_t  resource_data[8];
925         uint8_t  vpd_tag;
926         uint16_t vpd_len;
927         uint8_t  vpd_keyword[2];
928         uint8_t  length;
929         uint8_t  revision;
930         uint8_t  device_flags;
931         uint8_t  termnation_menus[2];
932         uint8_t  fifo_threshold;
933         uint8_t  end_tag;
934         uint8_t  vpd_checksum;
935         uint16_t default_target_flags;
936         uint16_t default_bios_flags;
937         uint16_t default_ctrl_flags;
938         uint8_t  default_irq;
939         uint8_t  pci_lattime;
940         uint8_t  max_target;
941         uint8_t  boot_lun;
942         uint16_t signature;
943         uint8_t  reserved_2;
944         uint8_t  checksum;
945         uint8_t  reserved_3[4];
946 };
947
948 /****************************** Flexport Logic ********************************/
949 #define FLXADDR_TERMCTL                 0x0
950 #define         FLX_TERMCTL_ENSECHIGH   0x8
951 #define         FLX_TERMCTL_ENSECLOW    0x4
952 #define         FLX_TERMCTL_ENPRIHIGH   0x2
953 #define         FLX_TERMCTL_ENPRILOW    0x1
954 #define FLXADDR_ROMSTAT_CURSENSECTL     0x1
955 #define         FLX_ROMSTAT_SEECFG      0xF0
956 #define         FLX_ROMSTAT_EECFG       0x0F
957 #define         FLX_ROMSTAT_SEE_93C66   0x00
958 #define         FLX_ROMSTAT_SEE_NONE    0xF0
959 #define         FLX_ROMSTAT_EE_512x8    0x0
960 #define         FLX_ROMSTAT_EE_1MBx8    0x1
961 #define         FLX_ROMSTAT_EE_2MBx8    0x2
962 #define         FLX_ROMSTAT_EE_4MBx8    0x3
963 #define         FLX_ROMSTAT_EE_16MBx8   0x4
964 #define                 CURSENSE_ENB    0x1
965 #define FLXADDR_FLEXSTAT                0x2
966 #define         FLX_FSTAT_BUSY          0x1
967 #define FLXADDR_CURRENT_STAT            0x4
968 #define         FLX_CSTAT_SEC_HIGH      0xC0
969 #define         FLX_CSTAT_SEC_LOW       0x30
970 #define         FLX_CSTAT_PRI_HIGH      0x0C
971 #define         FLX_CSTAT_PRI_LOW       0x03
972 #define         FLX_CSTAT_MASK          0x03
973 #define         FLX_CSTAT_SHIFT         2
974 #define         FLX_CSTAT_OKAY          0x0
975 #define         FLX_CSTAT_OVER          0x1
976 #define         FLX_CSTAT_UNDER         0x2
977 #define         FLX_CSTAT_INVALID       0x3
978
979 int             ahd_read_seeprom(struct ahd_softc *ahd, uint16_t *buf,
980                                  u_int start_addr, u_int count, int bstream);
981
982 int             ahd_write_seeprom(struct ahd_softc *ahd, uint16_t *buf,
983                                   u_int start_addr, u_int count);
984 int             ahd_wait_seeprom(struct ahd_softc *ahd);
985 int             ahd_verify_vpd_cksum(struct vpd_config *vpd);
986 int             ahd_verify_cksum(struct seeprom_config *sc);
987 int             ahd_acquire_seeprom(struct ahd_softc *ahd);
988 void            ahd_release_seeprom(struct ahd_softc *ahd);
989
990 /****************************  Message Buffer *********************************/
991 typedef enum {
992         MSG_FLAG_NONE                   = 0x00,
993         MSG_FLAG_EXPECT_PPR_BUSFREE     = 0x01,
994         MSG_FLAG_IU_REQ_CHANGED         = 0x02,
995         MSG_FLAG_EXPECT_IDE_BUSFREE     = 0x04,
996         MSG_FLAG_EXPECT_QASREJ_BUSFREE  = 0x08,
997         MSG_FLAG_PACKETIZED             = 0x10
998 } ahd_msg_flags;
999
1000 typedef enum {
1001         MSG_TYPE_NONE                   = 0x00,
1002         MSG_TYPE_INITIATOR_MSGOUT       = 0x01,
1003         MSG_TYPE_INITIATOR_MSGIN        = 0x02,
1004         MSG_TYPE_TARGET_MSGOUT          = 0x03,
1005         MSG_TYPE_TARGET_MSGIN           = 0x04
1006 } ahd_msg_type;
1007
1008 typedef enum {
1009         MSGLOOP_IN_PROG,
1010         MSGLOOP_MSGCOMPLETE,
1011         MSGLOOP_TERMINATED
1012 } msg_loop_stat;
1013
1014 /*********************** Software Configuration Structure *********************/
1015 struct ahd_suspend_channel_state {
1016         uint8_t scsiseq;
1017         uint8_t sxfrctl0;
1018         uint8_t sxfrctl1;
1019         uint8_t simode0;
1020         uint8_t simode1;
1021         uint8_t seltimer;
1022         uint8_t seqctl;
1023 };
1024
1025 struct ahd_suspend_state {
1026         struct  ahd_suspend_channel_state channel[2];
1027         uint8_t optionmode;
1028         uint8_t dscommand0;
1029         uint8_t dspcistatus;
1030         /* hsmailbox */
1031         uint8_t crccontrol1;
1032         uint8_t scbbaddr;
1033         /* Host and sequencer SCB counts */
1034         uint8_t dff_thrsh;
1035         uint8_t *scratch_ram;
1036         uint8_t *btt;
1037 };
1038
1039 typedef void (*ahd_bus_intr_t)(struct ahd_softc *);
1040
1041 typedef enum {
1042         AHD_MODE_DFF0,
1043         AHD_MODE_DFF1,
1044         AHD_MODE_CCHAN,
1045         AHD_MODE_SCSI,
1046         AHD_MODE_CFG,
1047         AHD_MODE_UNKNOWN
1048 } ahd_mode;
1049
1050 #define AHD_MK_MSK(x) (0x01 << (x))
1051 #define AHD_MODE_DFF0_MSK       AHD_MK_MSK(AHD_MODE_DFF0)
1052 #define AHD_MODE_DFF1_MSK       AHD_MK_MSK(AHD_MODE_DFF1)
1053 #define AHD_MODE_CCHAN_MSK      AHD_MK_MSK(AHD_MODE_CCHAN)
1054 #define AHD_MODE_SCSI_MSK       AHD_MK_MSK(AHD_MODE_SCSI)
1055 #define AHD_MODE_CFG_MSK        AHD_MK_MSK(AHD_MODE_CFG)
1056 #define AHD_MODE_UNKNOWN_MSK    AHD_MK_MSK(AHD_MODE_UNKNOWN)
1057 #define AHD_MODE_ANY_MSK (~0)
1058
1059 typedef uint8_t ahd_mode_state;
1060
1061 typedef void ahd_callback_t (void *);
1062
1063 struct ahd_softc {
1064         bus_space_tag_t           tags[2];
1065         bus_space_handle_t        bshs[2];
1066 #ifndef __linux__
1067         bus_dma_tag_t             buffer_dmat;   /* dmat for buffer I/O */
1068 #endif
1069         struct scb_data           scb_data;
1070
1071         struct hardware_scb      *next_queued_hscb;
1072         struct map_node          *next_queued_hscb_map;
1073
1074         /*
1075          * SCBs that have been sent to the controller
1076          */
1077         LIST_HEAD(, scb)          pending_scbs;
1078
1079         /*
1080          * SCBs whose timeout routine has been called.
1081          */
1082         LIST_HEAD(, scb)          timedout_scbs;
1083
1084         /*
1085          * Current register window mode information.
1086          */
1087         ahd_mode                  dst_mode;
1088         ahd_mode                  src_mode;
1089
1090         /*
1091          * Saved register window mode information
1092          * used for restore on next unpause.
1093          */
1094         ahd_mode                  saved_dst_mode;
1095         ahd_mode                  saved_src_mode;
1096
1097         /*
1098          * Platform specific data.
1099          */
1100         struct ahd_platform_data *platform_data;
1101
1102         /*
1103          * Platform specific device information.
1104          */
1105         aic_dev_softc_t           dev_softc;
1106
1107         /*
1108          * Bus specific device information.
1109          */
1110         ahd_bus_intr_t            bus_intr;
1111
1112         /*
1113          * Target mode related state kept on a per enabled lun basis.
1114          * Targets that are not enabled will have null entries.
1115          * As an initiator, we keep one target entry for our initiator
1116          * ID to store our sync/wide transfer settings.
1117          */
1118         struct ahd_tmode_tstate  *enabled_targets[AHD_NUM_TARGETS];
1119
1120         /*
1121          * The black hole device responsible for handling requests for
1122          * disabled luns on enabled targets.
1123          */
1124         struct ahd_tmode_lstate  *black_hole;
1125
1126         /*
1127          * Device instance currently on the bus awaiting a continue TIO
1128          * for a command that was not given the disconnect priveledge.
1129          */
1130         struct ahd_tmode_lstate  *pending_device;
1131
1132         /*
1133          * Timer handles for timer driven callbacks.
1134          */
1135         aic_timer_t               reset_timer;
1136         aic_timer_t               stat_timer;
1137
1138         /*
1139          * Statistics.
1140          */
1141 #define AHD_STAT_UPDATE_US      250000 /* 250ms */
1142 #define AHD_STAT_BUCKETS        4
1143         u_int                     cmdcmplt_bucket;
1144         uint32_t                  cmdcmplt_counts[AHD_STAT_BUCKETS];
1145         uint32_t                  cmdcmplt_total;
1146
1147         /*
1148          * Card characteristics
1149          */
1150         ahd_chip                  chip;
1151         ahd_feature               features;
1152         ahd_bug                   bugs;
1153         ahd_flag                  flags;
1154         struct seeprom_config    *seep_config;
1155
1156         /* Values to store in the SEQCTL register for pause and unpause */
1157         uint8_t                   unpause;
1158         uint8_t                   pause;
1159
1160         /* Command Queues */
1161         uint16_t                  qoutfifonext;
1162         uint16_t                  qoutfifonext_valid_tag;
1163         uint16_t                  qinfifonext;
1164         uint16_t                  qinfifo[AHD_SCB_MAX];
1165         uint16_t                 *qoutfifo;
1166
1167         /* Critical Section Data */
1168         struct cs                *critical_sections;
1169         u_int                     num_critical_sections;
1170
1171         /* Buffer for handling packetized bitbucket. */
1172         uint8_t                  *overrun_buf;
1173
1174         /* Links for chaining softcs */
1175         TAILQ_ENTRY(ahd_softc)    links;
1176
1177         /* Channel Names ('A', 'B', etc.) */
1178         char                      channel;
1179
1180         /* Initiator Bus ID */
1181         uint8_t                   our_id;
1182
1183         /*
1184          * Target incoming command FIFO.
1185          */
1186         struct target_cmd        *targetcmds;
1187         uint8_t                   tqinfifonext;
1188
1189         /*
1190          * Cached verson of the hs_mailbox so we can avoid
1191          * pausing the sequencer during mailbox updates.
1192          */
1193         uint8_t                   hs_mailbox;
1194
1195         /*
1196          * Incoming and outgoing message handling.
1197          */
1198         uint8_t                   send_msg_perror;
1199         ahd_msg_flags             msg_flags;
1200         ahd_msg_type              msg_type;
1201         uint8_t                   msgout_buf[12];/* Message we are sending */
1202         uint8_t                   msgin_buf[12];/* Message we are receiving */
1203         u_int                     msgout_len;   /* Length of message to send */
1204         u_int                     msgout_index; /* Current index in msgout */
1205         u_int                     msgin_index;  /* Current index in msgin */
1206
1207         /*
1208          * Mapping information for data structures shared
1209          * between the sequencer and kernel.
1210          */
1211         bus_dma_tag_t             parent_dmat;
1212         bus_dma_tag_t             shared_data_dmat;
1213         struct map_node           shared_data_map;
1214
1215         /* Information saved through suspend/resume cycles */
1216         struct ahd_suspend_state  suspend_state;
1217
1218         /* Number of enabled target mode device on this card */
1219         u_int                     enabled_luns;
1220
1221         /* Initialization level of this data structure */
1222         u_int                     init_level;
1223
1224         /* PCI cacheline size. */
1225         u_int                     pci_cachesize;
1226
1227         /* IO Cell Parameters */
1228         uint8_t                   iocell_opts[AHD_NUM_PER_DEV_ANNEXCOLS];
1229
1230         u_int                     stack_size;
1231         uint16_t                 *saved_stack;
1232
1233         /* Per-Unit descriptive information */
1234         const char               *description;
1235         const char               *bus_description;
1236         char                     *name;
1237         int                       unit;
1238
1239         /* Selection Timer settings */
1240         int                       seltime;
1241
1242         /*
1243          * Interrupt coalescing settings.
1244          */
1245 #define AHD_INT_COALESCING_TIMER_DEFAULT                250 /*us*/
1246 #define AHD_INT_COALESCING_MAXCMDS_DEFAULT              10
1247 #define AHD_INT_COALESCING_MAXCMDS_MAX                  127
1248 #define AHD_INT_COALESCING_MINCMDS_DEFAULT              5
1249 #define AHD_INT_COALESCING_MINCMDS_MAX                  127
1250 #define AHD_INT_COALESCING_THRESHOLD_DEFAULT            2000
1251 #define AHD_INT_COALESCING_STOP_THRESHOLD_DEFAULT       1000
1252         u_int                     int_coalescing_timer;
1253         u_int                     int_coalescing_maxcmds;
1254         u_int                     int_coalescing_mincmds;
1255         u_int                     int_coalescing_threshold;
1256         u_int                     int_coalescing_stop_threshold;
1257
1258         uint16_t                  user_discenable;/* Disconnection allowed  */
1259         uint16_t                  user_tagenable;/* Tagged Queuing allowed */
1260 };
1261
1262 TAILQ_HEAD(ahd_softc_tailq, ahd_softc);
1263 extern struct ahd_softc_tailq ahd_tailq;
1264
1265 /*************************** IO Cell Configuration ****************************/
1266 #define AHD_PRECOMP_SLEW_INDEX                                          \
1267     (AHD_ANNEXCOL_PRECOMP_SLEW - AHD_ANNEXCOL_PER_DEV0)
1268
1269 #define AHD_AMPLITUDE_INDEX                                             \
1270     (AHD_ANNEXCOL_AMPLITUDE - AHD_ANNEXCOL_PER_DEV0)
1271
1272 #define AHD_SET_SLEWRATE(ahd, new_slew)                                 \
1273 do {                                                                    \
1274     (ahd)->iocell_opts[AHD_PRECOMP_SLEW_INDEX] &= ~AHD_SLEWRATE_MASK;   \
1275     (ahd)->iocell_opts[AHD_PRECOMP_SLEW_INDEX] |=                       \
1276         (((new_slew) << AHD_SLEWRATE_SHIFT) & AHD_SLEWRATE_MASK);       \
1277 } while (0)
1278
1279 #define AHD_SET_PRECOMP(ahd, new_pcomp)                                 \
1280 do {                                                                    \
1281     (ahd)->iocell_opts[AHD_PRECOMP_SLEW_INDEX] &= ~AHD_PRECOMP_MASK;    \
1282     (ahd)->iocell_opts[AHD_PRECOMP_SLEW_INDEX] |=                       \
1283         (((new_pcomp) << AHD_PRECOMP_SHIFT) & AHD_PRECOMP_MASK);        \
1284 } while (0)
1285
1286 #define AHD_SET_AMPLITUDE(ahd, new_amp)                                 \
1287 do {                                                                    \
1288     (ahd)->iocell_opts[AHD_AMPLITUDE_INDEX] &= ~AHD_AMPLITUDE_MASK;     \
1289     (ahd)->iocell_opts[AHD_AMPLITUDE_INDEX] |=                          \
1290         (((new_amp) << AHD_AMPLITUDE_SHIFT) & AHD_AMPLITUDE_MASK);      \
1291 } while (0)
1292
1293 /************************ Active Device Information ***************************/
1294 typedef enum {
1295         ROLE_UNKNOWN,
1296         ROLE_INITIATOR,
1297         ROLE_TARGET
1298 } role_t;
1299
1300 struct ahd_devinfo {
1301         int      our_scsiid;
1302         int      target_offset;
1303         uint16_t target_mask;
1304         u_int    target;
1305         u_int    lun;
1306         char     channel;
1307         role_t   role;          /*
1308                                  * Only guaranteed to be correct if not
1309                                  * in the busfree state.
1310                                  */
1311 };
1312
1313 /****************************** PCI Structures ********************************/
1314 #define AHD_PCI_IOADDR0 PCIR_BAR(0)     /* I/O BAR*/
1315 #define AHD_PCI_MEMADDR PCIR_BAR(1)     /* Memory BAR */
1316 #define AHD_PCI_IOADDR1 PCIR_BAR(3)     /* Second I/O BAR */
1317
1318 typedef int (ahd_device_setup_t)(struct ahd_softc *);
1319
1320 struct ahd_pci_identity {
1321         uint64_t                 full_id;
1322         uint64_t                 id_mask;
1323         char                    *name;
1324         ahd_device_setup_t      *setup;
1325 };
1326 extern struct ahd_pci_identity ahd_pci_ident_table [];
1327 extern const u_int ahd_num_pci_devs;
1328
1329 /***************************** VL/EISA Declarations ***************************/
1330 struct aic7770_identity {
1331         uint32_t                 full_id;
1332         uint32_t                 id_mask;
1333         char                    *name;
1334         ahd_device_setup_t      *setup;
1335 };
1336 extern struct aic7770_identity aic7770_ident_table [];
1337 extern const int ahd_num_aic7770_devs;
1338
1339 #define AHD_EISA_SLOT_OFFSET    0xc00
1340 #define AHD_EISA_IOSIZE         0x100
1341
1342 /*************************** Function Declarations ****************************/
1343 /******************************************************************************/
1344 void                    ahd_reset_cmds_pending(struct ahd_softc *ahd);
1345 u_int                   ahd_find_busy_tcl(struct ahd_softc *ahd, u_int tcl);
1346 void                    ahd_busy_tcl(struct ahd_softc *ahd,
1347                                      u_int tcl, u_int busyid);
1348 static __inline void    ahd_unbusy_tcl(struct ahd_softc *ahd, u_int tcl);
1349 static __inline void
1350 ahd_unbusy_tcl(struct ahd_softc *ahd, u_int tcl)
1351 {
1352         ahd_busy_tcl(ahd, tcl, SCB_LIST_NULL);
1353 }
1354
1355 /***************************** PCI Front End *********************************/
1356 struct  ahd_pci_identity *ahd_find_pci_device(aic_dev_softc_t);
1357 int                       ahd_pci_config(struct ahd_softc *,
1358                                          struct ahd_pci_identity *);
1359 int     ahd_pci_test_register_access(struct ahd_softc *);
1360
1361 /************************** SCB and SCB queue management **********************/
1362 int             ahd_probe_scbs(struct ahd_softc *);
1363 void            ahd_qinfifo_requeue_tail(struct ahd_softc *ahd,
1364                                          struct scb *scb);
1365 int             ahd_match_scb(struct ahd_softc *ahd, struct scb *scb,
1366                               int target, char channel, int lun,
1367                               u_int tag, role_t role);
1368
1369 /****************************** Initialization ********************************/
1370 struct ahd_softc        *ahd_alloc(void *platform_arg, char *name);
1371 int                      ahd_softc_init(struct ahd_softc *);
1372 void                     ahd_controller_info(struct ahd_softc *ahd, char *buf);
1373 int                      ahd_init(struct ahd_softc *ahd);
1374 int                      ahd_default_config(struct ahd_softc *ahd);
1375 int                      ahd_parse_vpddata(struct ahd_softc *ahd,
1376                                            struct vpd_config *vpd);
1377 int                      ahd_parse_cfgdata(struct ahd_softc *ahd,
1378                                            struct seeprom_config *sc);
1379 void                     ahd_intr_enable(struct ahd_softc *ahd, int enable);
1380 void                     ahd_update_coalescing_values(struct ahd_softc *ahd,
1381                                                       u_int timer,
1382                                                       u_int maxcmds,
1383                                                       u_int mincmds);
1384 void                     ahd_enable_coalescing(struct ahd_softc *ahd,
1385                                                int enable);
1386 void                     ahd_pause_and_flushwork(struct ahd_softc *ahd);
1387 int                      ahd_suspend(struct ahd_softc *ahd); 
1388 int                      ahd_resume(struct ahd_softc *ahd);
1389 void                     ahd_softc_insert(struct ahd_softc *);
1390 struct ahd_softc        *ahd_find_softc(struct ahd_softc *ahd);
1391 void                     ahd_set_unit(struct ahd_softc *, int);
1392 void                     ahd_set_name(struct ahd_softc *, char *);
1393 struct scb              *ahd_get_scb(struct ahd_softc *ahd, u_int col_idx);
1394 void                     ahd_free_scb(struct ahd_softc *ahd, struct scb *scb);
1395 void                     ahd_alloc_scbs(struct ahd_softc *ahd);
1396 void                     ahd_free(struct ahd_softc *ahd);
1397 int                      ahd_reset(struct ahd_softc *ahd, int reinit);
1398 void                     ahd_shutdown(void *arg);
1399 int                      ahd_write_flexport(struct ahd_softc *ahd,
1400                                             u_int addr, u_int value);
1401 int                      ahd_read_flexport(struct ahd_softc *ahd, u_int addr,
1402                                            uint8_t *value);
1403 int                      ahd_wait_flexport(struct ahd_softc *ahd);
1404
1405 /*************************** Interrupt Services *******************************/
1406 void                    ahd_pci_intr(struct ahd_softc *ahd);
1407 void                    ahd_clear_intstat(struct ahd_softc *ahd);
1408 void                    ahd_flush_qoutfifo(struct ahd_softc *ahd);
1409 void                    ahd_run_qoutfifo(struct ahd_softc *ahd);
1410 #ifdef AHD_TARGET_MODE
1411 void                    ahd_run_tqinfifo(struct ahd_softc *ahd, int paused);
1412 #endif
1413 void                    ahd_handle_hwerrint(struct ahd_softc *ahd);
1414 void                    ahd_handle_seqint(struct ahd_softc *ahd, u_int intstat);
1415 void                    ahd_handle_scsiint(struct ahd_softc *ahd,
1416                                            u_int intstat);
1417 void                    ahd_clear_critical_section(struct ahd_softc *ahd);
1418
1419 /***************************** Error Recovery *********************************/
1420 typedef enum {
1421         SEARCH_COMPLETE,
1422         SEARCH_COUNT,
1423         SEARCH_REMOVE,
1424         SEARCH_PRINT
1425 } ahd_search_action;
1426 int                     ahd_search_qinfifo(struct ahd_softc *ahd, int target,
1427                                            char channel, int lun, u_int tag,
1428                                            role_t role, uint32_t status,
1429                                            ahd_search_action action);
1430 int                     ahd_search_disc_list(struct ahd_softc *ahd, int target,
1431                                              char channel, int lun, u_int tag,
1432                                              int stop_on_first, int remove,
1433                                              int save_state);
1434 void                    ahd_freeze_devq(struct ahd_softc *ahd, struct scb *scb);
1435 int                     ahd_reset_channel(struct ahd_softc *ahd, char channel,
1436                                           int initiate_reset);
1437 int                     ahd_abort_scbs(struct ahd_softc *ahd, int target,
1438                                        char channel, int lun, u_int tag,
1439                                        role_t role, uint32_t status);
1440 void                    ahd_restart(struct ahd_softc *ahd);
1441 void                    ahd_clear_fifo(struct ahd_softc *ahd, u_int fifo);
1442 void                    ahd_handle_scb_status(struct ahd_softc *ahd,
1443                                               struct scb *scb);
1444 void                    ahd_handle_scsi_status(struct ahd_softc *ahd,
1445                                                struct scb *scb);
1446 void                    ahd_calc_residual(struct ahd_softc *ahd,
1447                                           struct scb *scb);
1448 void                    ahd_timeout(struct scb *scb);
1449 void                    ahd_recover_commands(struct ahd_softc *ahd);
1450 /*************************** Utility Functions ********************************/
1451 struct ahd_phase_table_entry*
1452                         ahd_lookup_phase_entry(int phase);
1453 void                    ahd_compile_devinfo(struct ahd_devinfo *devinfo,
1454                                             u_int our_id, u_int target,
1455                                             u_int lun, char channel,
1456                                             role_t role);
1457 /************************** Transfer Negotiation ******************************/
1458 void                    ahd_find_syncrate(struct ahd_softc *ahd, u_int *period,
1459                                           u_int *ppr_options, u_int maxsync);
1460 void                    ahd_validate_offset(struct ahd_softc *ahd,
1461                                             struct ahd_initiator_tinfo *tinfo,
1462                                             u_int period, u_int *offset,
1463                                             int wide, role_t role);
1464 void                    ahd_validate_width(struct ahd_softc *ahd,
1465                                            struct ahd_initiator_tinfo *tinfo,
1466                                            u_int *bus_width,
1467                                            role_t role);
1468 /*
1469  * Negotiation types.  These are used to qualify if we should renegotiate
1470  * even if our goal and current transport parameters are identical.
1471  */
1472 typedef enum {
1473         AHD_NEG_TO_GOAL,        /* Renegotiate only if goal and curr differ. */
1474         AHD_NEG_IF_NON_ASYNC,   /* Renegotiate so long as goal is non-async. */
1475         AHD_NEG_ALWAYS          /* Renegotiat even if goal is async. */
1476 } ahd_neg_type;
1477 int                     ahd_update_neg_request(struct ahd_softc*,
1478                                                struct ahd_devinfo*,
1479                                                struct ahd_tmode_tstate*,
1480                                                struct ahd_initiator_tinfo*,
1481                                                ahd_neg_type);
1482 void                    ahd_set_width(struct ahd_softc *ahd,
1483                                       struct ahd_devinfo *devinfo,
1484                                       u_int width, u_int type, int paused);
1485 void                    ahd_set_syncrate(struct ahd_softc *ahd,
1486                                          struct ahd_devinfo *devinfo,
1487                                          u_int period, u_int offset,
1488                                          u_int ppr_options,
1489                                          u_int type, int paused);
1490 typedef enum {
1491         AHD_QUEUE_NONE,
1492         AHD_QUEUE_BASIC,
1493         AHD_QUEUE_TAGGED
1494 } ahd_queue_alg;
1495
1496 void                    ahd_set_tags(struct ahd_softc *ahd,
1497                                      struct ahd_devinfo *devinfo,
1498                                      ahd_queue_alg alg);
1499
1500 /**************************** Target Mode *************************************/
1501 #ifdef AHD_TARGET_MODE
1502 void            ahd_send_lstate_events(struct ahd_softc *,
1503                                        struct ahd_tmode_lstate *);
1504 void            ahd_handle_en_lun(struct ahd_softc *ahd,
1505                                   struct cam_sim *sim, union ccb *ccb);
1506 cam_status      ahd_find_tmode_devs(struct ahd_softc *ahd,
1507                                     struct cam_sim *sim, union ccb *ccb,
1508                                     struct ahd_tmode_tstate **tstate,
1509                                     struct ahd_tmode_lstate **lstate,
1510                                     int notfound_failure);
1511 #ifndef AHD_TMODE_ENABLE
1512 #define AHD_TMODE_ENABLE 0
1513 #endif
1514 #endif
1515 /******************************* Debug ***************************************/
1516 #ifdef AHD_DEBUG
1517 extern uint32_t ahd_debug;
1518 #define AHD_SHOW_MISC           0x00001
1519 #define AHD_SHOW_SENSE          0x00002
1520 #define AHD_SHOW_RECOVERY       0x00004
1521 #define AHD_DUMP_SEEPROM        0x00008
1522 #define AHD_SHOW_TERMCTL        0x00010
1523 #define AHD_SHOW_MEMORY         0x00020
1524 #define AHD_SHOW_MESSAGES       0x00040
1525 #define AHD_SHOW_MODEPTR        0x00080
1526 #define AHD_SHOW_SELTO          0x00100
1527 #define AHD_SHOW_FIFOS          0x00200
1528 #define AHD_SHOW_QFULL          0x00400
1529 #define AHD_SHOW_DV             0x00800
1530 #define AHD_SHOW_MASKED_ERRORS  0x01000
1531 #define AHD_SHOW_QUEUE          0x02000
1532 #define AHD_SHOW_TQIN           0x04000
1533 #define AHD_SHOW_SG             0x08000
1534 #define AHD_SHOW_INT_COALESCING 0x10000
1535 #define AHD_DEBUG_SEQUENCER     0x20000
1536 #endif
1537 void                    ahd_print_scb(struct scb *scb);
1538 void                    ahd_print_devinfo(struct ahd_softc *ahd,
1539                                           struct ahd_devinfo *devinfo);
1540 void                    ahd_dump_sglist(struct scb *scb);
1541 void                    ahd_dump_all_cards_state(void);
1542 void                    ahd_dump_card_state(struct ahd_softc *ahd);
1543 int                     ahd_print_register(ahd_reg_parse_entry_t *table,
1544                                            u_int num_entries,
1545                                            const char *name,
1546                                            u_int address,
1547                                            u_int value,
1548                                            u_int *cur_column,
1549                                            u_int wrap_point);
1550 void                    ahd_dump_scbs(struct ahd_softc *ahd);
1551 #endif /* _AIC79XX_H_ */