make oldconfig will rebuild these...
[linux-2.4.21-pre4.git] / drivers / scsi / sym53c8xx_2 / sym_hipd.h
1 /*
2  * Device driver for the SYMBIOS/LSILOGIC 53C8XX and 53C1010 family 
3  * of PCI-SCSI IO processors.
4  *
5  * Copyright (C) 1999-2001  Gerard Roudier <groudier@free.fr>
6  *
7  * This driver is derived from the Linux sym53c8xx driver.
8  * Copyright (C) 1998-2000  Gerard Roudier
9  *
10  * The sym53c8xx driver is derived from the ncr53c8xx driver that had been 
11  * a port of the FreeBSD ncr driver to Linux-1.2.13.
12  *
13  * The original ncr driver has been written for 386bsd and FreeBSD by
14  *         Wolfgang Stanglmeier        <wolf@cologne.de>
15  *         Stefan Esser                <se@mi.Uni-Koeln.de>
16  * Copyright (C) 1994  Wolfgang Stanglmeier
17  *
18  * Other major contributions:
19  *
20  * NVRAM detection and reading.
21  * Copyright (C) 1997 Richard Waltham <dormouse@farsrobt.demon.co.uk>
22  *
23  *-----------------------------------------------------------------------------
24  *
25  * Redistribution and use in source and binary forms, with or without
26  * modification, are permitted provided that the following conditions
27  * are met:
28  * 1. Redistributions of source code must retain the above copyright
29  *    notice, this list of conditions and the following disclaimer.
30  * 2. The name of the author may not be used to endorse or promote products
31  *    derived from this software without specific prior written permission.
32  *
33  * Where this Software is combined with software released under the terms of 
34  * the GNU Public License ("GPL") and the terms of the GPL would require the 
35  * combined work to also be released under the terms of the GPL, the terms
36  * and conditions of this License will apply in addition to those of the
37  * GPL with the exception of any terms or conditions of this License that
38  * conflict with, or are expressly prohibited by, the GPL.
39  *
40  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
44  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  */
52
53 #ifndef SYM_HIPD_H
54 #define SYM_HIPD_H
55
56 /*
57  *  Generic driver options.
58  *
59  *  They may be defined in platform specific headers, if they 
60  *  are useful.
61  *
62  *    SYM_OPT_NO_BUS_MEMORY_MAPPING
63  *        When this option is set, the driver will not load the 
64  *        on-chip RAM using MMIO, but let the SCRIPTS processor 
65  *        do the work using MOVE MEMORY instructions.
66  *        (set for Linux/PPC)
67  *
68  *    SYM_OPT_HANDLE_DIR_UNKNOWN
69  *        When this option is set, the SCRIPTS used by the driver 
70  *        are able to handle SCSI transfers with direction not 
71  *        supplied by user.
72  *        (set for Linux-2.0.X)
73  *
74  *    SYM_OPT_HANDLE_DEVICE_QUEUEING
75  *        When this option is set, the driver will use a queue per 
76  *        device and handle QUEUE FULL status requeuing internally.
77  *
78  *    SYM_OPT_BUS_DMA_ABSTRACTION
79  *        When this option is set, the driver allocator is responsible 
80  *        of maintaining bus physical addresses and so provides virtual 
81  *        to bus physical address translation of driver data structures.
82  *        (set for FreeBSD-4 and Linux 2.3)
83  *
84  *    SYM_OPT_SNIFF_INQUIRY
85  *        When this option is set, the driver sniff out successful 
86  *        INQUIRY response and performs negotiations accordingly.
87  *        (set for Linux)
88  *
89  *    SYM_OPT_LIMIT_COMMAND_REORDERING
90  *        When this option is set, the driver tries to limit tagged 
91  *        command reordering to some reasonnable value.
92  *        (set for Linux)
93  */
94 #if 0
95 #define SYM_OPT_NO_BUS_MEMORY_MAPPING
96 #define SYM_OPT_HANDLE_DIR_UNKNOWN
97 #define SYM_OPT_HANDLE_DEVICE_QUEUEING
98 #define SYM_OPT_BUS_DMA_ABSTRACTION
99 #define SYM_OPT_SNIFF_INQUIRY
100 #define SYM_OPT_LIMIT_COMMAND_REORDERING
101 #endif
102
103 /*
104  *  Active debugging tags and verbosity.
105  *  Both DEBUG_FLAGS and sym_verbose can be redefined 
106  *  by the platform specific code to something else.
107  */
108 #define DEBUG_ALLOC     (0x0001)
109 #define DEBUG_PHASE     (0x0002)
110 #define DEBUG_POLL      (0x0004)
111 #define DEBUG_QUEUE     (0x0008)
112 #define DEBUG_RESULT    (0x0010)
113 #define DEBUG_SCATTER   (0x0020)
114 #define DEBUG_SCRIPT    (0x0040)
115 #define DEBUG_TINY      (0x0080)
116 #define DEBUG_TIMING    (0x0100)
117 #define DEBUG_NEGO      (0x0200)
118 #define DEBUG_TAGS      (0x0400)
119 #define DEBUG_POINTER   (0x0800)
120
121 #ifndef DEBUG_FLAGS
122 #define DEBUG_FLAGS     (0x0000)
123 #endif
124
125 #ifndef sym_verbose
126 #define sym_verbose     (np->verbose)
127 #endif
128
129 /*
130  *  These ones should have been already defined.
131  */
132 #ifndef offsetof
133 #define offsetof(t, m)  ((size_t) (&((t *)0)->m))
134 #endif
135 #ifndef MIN
136 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
137 #endif
138 #ifndef assert
139 #define assert(expression) { \
140         if (!(expression)) { \
141                 (void)panic( \
142                         "assertion \"%s\" failed: file \"%s\", line %d\n", \
143                         #expression, \
144                         __FILE__, __LINE__); \
145         } \
146 }
147 #endif
148
149 /*
150  *  Number of tasks per device we want to handle.
151  */
152 #if     SYM_CONF_MAX_TAG_ORDER > 8
153 #error  "more than 256 tags per logical unit not allowed."
154 #endif
155 #define SYM_CONF_MAX_TASK       (1<<SYM_CONF_MAX_TAG_ORDER)
156
157 /*
158  *  Donnot use more tasks that we can handle.
159  */
160 #ifndef SYM_CONF_MAX_TAG
161 #define SYM_CONF_MAX_TAG        SYM_CONF_MAX_TASK
162 #endif
163 #if     SYM_CONF_MAX_TAG > SYM_CONF_MAX_TASK
164 #undef  SYM_CONF_MAX_TAG
165 #define SYM_CONF_MAX_TAG        SYM_CONF_MAX_TASK
166 #endif
167
168 /*
169  *    This one means 'NO TAG for this job'
170  */
171 #define NO_TAG  (256)
172
173 /*
174  *  Number of SCSI targets.
175  */
176 #if     SYM_CONF_MAX_TARGET > 16
177 #error  "more than 16 targets not allowed."
178 #endif
179
180 /*
181  *  Number of logical units per target.
182  */
183 #if     SYM_CONF_MAX_LUN > 64
184 #error  "more than 64 logical units per target not allowed."
185 #endif
186
187 /*
188  *    Asynchronous pre-scaler (ns). Shall be 40 for 
189  *    the SCSI timings to be compliant.
190  */
191 #define SYM_CONF_MIN_ASYNC (40)
192
193 /*
194  *  Number of entries in the START and DONE queues.
195  *
196  *  We limit to 1 PAGE in order to succeed allocation of 
197  *  these queues. Each entry is 8 bytes long (2 DWORDS).
198  */
199 #ifdef  SYM_CONF_MAX_START
200 #define SYM_CONF_MAX_QUEUE (SYM_CONF_MAX_START+2)
201 #else
202 #define SYM_CONF_MAX_QUEUE (7*SYM_CONF_MAX_TASK+2)
203 #define SYM_CONF_MAX_START (SYM_CONF_MAX_QUEUE-2)
204 #endif
205
206 #if     SYM_CONF_MAX_QUEUE > SYM_MEM_CLUSTER_SIZE/8
207 #undef  SYM_CONF_MAX_QUEUE
208 #define SYM_CONF_MAX_QUEUE (SYM_MEM_CLUSTER_SIZE/8)
209 #undef  SYM_CONF_MAX_START
210 #define SYM_CONF_MAX_START (SYM_CONF_MAX_QUEUE-2)
211 #endif
212
213 /*
214  *  For this one, we want a short name :-)
215  */
216 #define MAX_QUEUE       SYM_CONF_MAX_QUEUE
217
218 /*
219  *  Union of supported NVRAM formats.
220  */
221 struct sym_nvram {
222         int type;
223 #define SYM_SYMBIOS_NVRAM       (1)
224 #define SYM_TEKRAM_NVRAM        (2)
225 #if SYM_CONF_NVRAM_SUPPORT
226         union {
227                 Symbios_nvram Symbios;
228                 Tekram_nvram Tekram;
229         } data;
230 #endif
231 };
232
233 /*
234  *  Common definitions for both bus space based and legacy IO methods.
235  */
236 #define INB(r)          INB_OFF(offsetof(struct sym_reg,r))
237 #define INW(r)          INW_OFF(offsetof(struct sym_reg,r))
238 #define INL(r)          INL_OFF(offsetof(struct sym_reg,r))
239
240 #define OUTB(r, v)      OUTB_OFF(offsetof(struct sym_reg,r), (v))
241 #define OUTW(r, v)      OUTW_OFF(offsetof(struct sym_reg,r), (v))
242 #define OUTL(r, v)      OUTL_OFF(offsetof(struct sym_reg,r), (v))
243
244 #define OUTONB(r, m)    OUTB(r, INB(r) | (m))
245 #define OUTOFFB(r, m)   OUTB(r, INB(r) & ~(m))
246 #define OUTONW(r, m)    OUTW(r, INW(r) | (m))
247 #define OUTOFFW(r, m)   OUTW(r, INW(r) & ~(m))
248 #define OUTONL(r, m)    OUTL(r, INL(r) | (m))
249 #define OUTOFFL(r, m)   OUTL(r, INL(r) & ~(m))
250
251 /*
252  *  We normally want the chip to have a consistent view
253  *  of driver internal data structures when we restart it.
254  *  Thus these macros.
255  */
256 #define OUTL_DSP(v)                             \
257         do {                                    \
258                 MEMORY_WRITE_BARRIER();         \
259                 OUTL (nc_dsp, (v));             \
260         } while (0)
261
262 #define OUTONB_STD()                            \
263         do {                                    \
264                 MEMORY_WRITE_BARRIER();         \
265                 OUTONB (nc_dcntl, (STD|NOCOM)); \
266         } while (0)
267
268 /*
269  *  Command control block states.
270  */
271 #define HS_IDLE         (0)
272 #define HS_BUSY         (1)
273 #define HS_NEGOTIATE    (2)     /* sync/wide data transfer*/
274 #define HS_DISCONNECT   (3)     /* Disconnected by target */
275 #define HS_WAIT         (4)     /* waiting for resource   */
276
277 #define HS_DONEMASK     (0x80)
278 #define HS_COMPLETE     (4|HS_DONEMASK)
279 #define HS_SEL_TIMEOUT  (5|HS_DONEMASK) /* Selection timeout      */
280 #define HS_UNEXPECTED   (6|HS_DONEMASK) /* Unexpected disconnect  */
281 #define HS_COMP_ERR     (7|HS_DONEMASK) /* Completed with error   */
282
283 /*
284  *  Software Interrupt Codes
285  */
286 #define SIR_BAD_SCSI_STATUS     (1)
287 #define SIR_SEL_ATN_NO_MSG_OUT  (2)
288 #define SIR_MSG_RECEIVED        (3)
289 #define SIR_MSG_WEIRD           (4)
290 #define SIR_NEGO_FAILED         (5)
291 #define SIR_NEGO_PROTO          (6)
292 #define SIR_SCRIPT_STOPPED      (7)
293 #define SIR_REJECT_TO_SEND      (8)
294 #define SIR_SWIDE_OVERRUN       (9)
295 #define SIR_SODL_UNDERRUN       (10)
296 #define SIR_RESEL_NO_MSG_IN     (11)
297 #define SIR_RESEL_NO_IDENTIFY   (12)
298 #define SIR_RESEL_BAD_LUN       (13)
299 #define SIR_TARGET_SELECTED     (14)
300 #define SIR_RESEL_BAD_I_T_L     (15)
301 #define SIR_RESEL_BAD_I_T_L_Q   (16)
302 #define SIR_ABORT_SENT          (17)
303 #define SIR_RESEL_ABORTED       (18)
304 #define SIR_MSG_OUT_DONE        (19)
305 #define SIR_COMPLETE_ERROR      (20)
306 #define SIR_DATA_OVERRUN        (21)
307 #define SIR_BAD_PHASE           (22)
308 #if     SYM_CONF_DMA_ADDRESSING_MODE == 2
309 #define SIR_DMAP_DIRTY          (23)
310 #define SIR_MAX                 (23)
311 #else
312 #define SIR_MAX                 (22)
313 #endif
314
315 /*
316  *  Extended error bit codes.
317  *  xerr_status field of struct sym_ccb.
318  */
319 #define XE_EXTRA_DATA   (1)     /* unexpected data phase         */
320 #define XE_BAD_PHASE    (1<<1)  /* illegal phase (4/5)           */
321 #define XE_PARITY_ERR   (1<<2)  /* unrecovered SCSI parity error */
322 #define XE_SODL_UNRUN   (1<<3)  /* ODD transfer in DATA OUT phase */
323 #define XE_SWIDE_OVRUN  (1<<4)  /* ODD transfer in DATA IN phase */
324
325 /*
326  *  Negotiation status.
327  *  nego_status field of struct sym_ccb.
328  */
329 #define NS_SYNC         (1)
330 #define NS_WIDE         (2)
331 #define NS_PPR          (3)
332
333 /*
334  *  A CCB hashed table is used to retrieve CCB address 
335  *  from DSA value.
336  */
337 #define CCB_HASH_SHIFT          8
338 #define CCB_HASH_SIZE           (1UL << CCB_HASH_SHIFT)
339 #define CCB_HASH_MASK           (CCB_HASH_SIZE-1)
340 #if 1
341 #define CCB_HASH_CODE(dsa)      \
342         (((dsa) >> (_LGRU16_(sizeof(struct sym_ccb)))) & CCB_HASH_MASK)
343 #else
344 #define CCB_HASH_CODE(dsa)      (((dsa) >> 9) & CCB_HASH_MASK)
345 #endif
346
347 #if     SYM_CONF_DMA_ADDRESSING_MODE == 2
348 /*
349  *  We may want to use segment registers for 64 bit DMA.
350  *  16 segments registers -> up to 64 GB addressable.
351  */
352 #define SYM_DMAP_SHIFT  (4)
353 #define SYM_DMAP_SIZE   (1u<<SYM_DMAP_SHIFT)
354 #define SYM_DMAP_MASK   (SYM_DMAP_SIZE-1)
355 #endif
356
357 /*
358  *  Device flags.
359  */
360 #define SYM_DISC_ENABLED        (1)
361 #define SYM_TAGS_ENABLED        (1<<1)
362 #define SYM_SCAN_BOOT_DISABLED  (1<<2)
363 #define SYM_SCAN_LUNS_DISABLED  (1<<3)
364
365 /*
366  *  Host adapter miscellaneous flags.
367  */
368 #define SYM_AVOID_BUS_RESET     (1)
369 #define SYM_SCAN_TARGETS_HILO   (1<<1)
370
371 /*
372  *  Misc.
373  */
374 #define SYM_SNOOP_TIMEOUT (10000000)
375 #define BUS_8_BIT       0
376 #define BUS_16_BIT      1
377
378 /*
379  *  Gather negotiable parameters value
380  */
381 struct sym_trans {
382         u8 scsi_version;
383         u8 spi_version;
384         u8 period;
385         u8 offset;
386         u8 width;
387         u8 options;     /* PPR options */
388 };
389
390 struct sym_tinfo {
391         struct sym_trans curr;
392         struct sym_trans goal;
393         struct sym_trans user;
394 #ifdef  SYM_OPT_ANNOUNCE_TRANSFER_RATE
395         struct sym_trans prev;
396 #endif
397 };
398
399 /*
400  *  Global TCB HEADER.
401  *
402  *  Due to lack of indirect addressing on earlier NCR chips,
403  *  this substructure is copied from the TCB to a global 
404  *  address after selection.
405  *  For SYMBIOS chips that support LOAD/STORE this copy is 
406  *  not needed and thus not performed.
407  */
408 struct sym_tcbh {
409         /*
410          *  Scripts bus addresses of LUN table accessed from scripts.
411          *  LUN #0 is a special case, since multi-lun devices are rare, 
412          *  and we we want to speed-up the general case and not waste 
413          *  resources.
414          */
415         u32     luntbl_sa;      /* bus address of this table    */
416         u32     lun0_sa;        /* bus address of LCB #0        */
417         /*
418          *  Actual SYNC/WIDE IO registers value for this target.
419          *  'sval', 'wval' and 'uval' are read from SCRIPTS and 
420          *  so have alignment constraints.
421          */
422 /*0*/   u_char  uval;           /* -> SCNTL4 register           */
423 /*1*/   u_char  sval;           /* -> SXFER  io register        */
424 /*2*/   u_char  filler1;
425 /*3*/   u_char  wval;           /* -> SCNTL3 io register        */
426 };
427
428 /*
429  *  Target Control Block
430  */
431 struct sym_tcb {
432         /*
433          *  TCB header.
434          *  Assumed at offset 0.
435          */
436 /*0*/   struct sym_tcbh head;
437
438         /*
439          *  LUN table used by the SCRIPTS processor.
440          *  An array of bus addresses is used on reselection.
441          */
442         u32     *luntbl;        /* LCBs bus address table       */
443
444         /*
445          *  LUN table used by the C code.
446          */
447         lcb_p   lun0p;          /* LCB of LUN #0 (usual case)   */
448 #if SYM_CONF_MAX_LUN > 1
449         lcb_p   *lunmp;         /* Other LCBs [1..MAX_LUN]      */
450 #endif
451
452         /*
453          *  Bitmap that tells about LUNs that succeeded at least 
454          *  1 IO and therefore assumed to be a real device.
455          *  Avoid useless allocation of the LCB structure.
456          */
457         u32     lun_map[(SYM_CONF_MAX_LUN+31)/32];
458
459         /*
460          *  Bitmap that tells about LUNs that haven't yet an LCB 
461          *  allocated (not discovered or LCB allocation failed).
462          */
463         u32     busy0_map[(SYM_CONF_MAX_LUN+31)/32];
464
465 #ifdef  SYM_HAVE_STCB
466         /*
467          *  O/S specific data structure.
468          */
469         struct sym_stcb s;
470 #endif
471
472         /*
473          *  Transfer capabilities (SIP)
474          */
475         struct sym_tinfo tinfo;
476
477         /*
478          * Keep track of the CCB used for the negotiation in order
479          * to ensure that only 1 negotiation is queued at a time.
480          */
481         ccb_p   nego_cp;        /* CCB used for the nego                */
482
483         /*
484          *  Set when we want to reset the device.
485          */
486         u_char  to_reset;
487
488         /*
489          *  Other user settable limits and options.
490          *  These limits are read from the NVRAM if present.
491          */
492         u_char  usrflags;
493         u_short usrtags;
494
495 #ifdef  SYM_OPT_SNIFF_INQUIRY
496         /*
497          *  Some minimal information from INQUIRY response.
498          */
499         u32     cmdq_map[(SYM_CONF_MAX_LUN+31)/32];
500         u_char  inq_version;
501         u_char  inq_byte7;
502         u_char  inq_byte56;
503         u_char  inq_byte7_valid;
504 #endif
505
506 };
507
508 /*
509  *  Global LCB HEADER.
510  *
511  *  Due to lack of indirect addressing on earlier NCR chips,
512  *  this substructure is copied from the LCB to a global 
513  *  address after selection.
514  *  For SYMBIOS chips that support LOAD/STORE this copy is 
515  *  not needed and thus not performed.
516  */
517 struct sym_lcbh {
518         /*
519          *  SCRIPTS address jumped by SCRIPTS on reselection.
520          *  For not probed logical units, this address points to 
521          *  SCRIPTS that deal with bad LU handling (must be at 
522          *  offset zero of the LCB for that reason).
523          */
524 /*0*/   u32     resel_sa;
525
526         /*
527          *  Task (bus address of a CCB) read from SCRIPTS that points 
528          *  to the unique ITL nexus allowed to be disconnected.
529          */
530         u32     itl_task_sa;
531
532         /*
533          *  Task table bus address (read from SCRIPTS).
534          */
535         u32     itlq_tbl_sa;
536 };
537
538 /*
539  *  Logical Unit Control Block
540  */
541 struct sym_lcb {
542         /*
543          *  TCB header.
544          *  Assumed at offset 0.
545          */
546 /*0*/   struct sym_lcbh head;
547
548         /*
549          *  Task table read from SCRIPTS that contains pointers to 
550          *  ITLQ nexuses. The bus address read from SCRIPTS is 
551          *  inside the header.
552          */
553         u32     *itlq_tbl;      /* Kernel virtual address       */
554
555         /*
556          *  Busy CCBs management.
557          */
558         u_short busy_itlq;      /* Number of busy tagged CCBs   */
559         u_short busy_itl;       /* Number of busy untagged CCBs */
560
561         /*
562          *  Circular tag allocation buffer.
563          */
564         u_short ia_tag;         /* Tag allocation index         */
565         u_short if_tag;         /* Tag release index            */
566         u_char  *cb_tags;       /* Circular tags buffer         */
567
568         /*
569          *  O/S specific data structure.
570          */
571 #ifdef  SYM_HAVE_SLCB
572         struct sym_slcb s;
573 #endif
574
575 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
576         /*
577          *  Optionnaly the driver can handle device queueing, 
578          *  and requeues internally command to redo.
579          */
580         SYM_QUEHEAD
581                 waiting_ccbq;
582         SYM_QUEHEAD
583                 started_ccbq;
584         int     num_sgood;
585         u_short started_tags;
586         u_short started_no_tag;
587         u_short started_max;
588         u_short started_limit;
589 #endif
590
591 #ifdef SYM_OPT_LIMIT_COMMAND_REORDERING
592         /*
593          *  Optionnaly the driver can try to prevent SCSI 
594          *  IOs from being too much reordering.
595          */
596         u_char          tags_si;        /* Current index to tags sum    */
597         u_short         tags_sum[2];    /* Tags sum counters            */
598         u_short         tags_since;     /* # of tags since last switch  */
599 #endif
600
601         /*
602          *  Set when we want to clear all tasks.
603          */
604         u_char to_clear;
605
606         /*
607          *  Capabilities.
608          */
609         u_char  user_flags;
610         u_char  curr_flags;
611 };
612
613 /*
614  *  Action from SCRIPTS on a task.
615  *  Is part of the CCB, but is also used separately to plug 
616  *  error handling action to perform from SCRIPTS.
617  */
618 struct sym_actscr {
619         u32     start;          /* Jumped by SCRIPTS after selection    */
620         u32     restart;        /* Jumped by SCRIPTS on relection       */
621 };
622
623 /*
624  *  Phase mismatch context.
625  *
626  *  It is part of the CCB and is used as parameters for the 
627  *  DATA pointer. We need two contexts to handle correctly the 
628  *  SAVED DATA POINTER.
629  */
630 struct sym_pmc {
631         struct  sym_tblmove sg; /* Updated interrupted SG block */
632         u32     ret;            /* SCRIPT return address        */
633 };
634
635 /*
636  *  LUN control block lookup.
637  *  We use a direct pointer for LUN #0, and a table of 
638  *  pointers which is only allocated for devices that support 
639  *  LUN(s) > 0.
640  */
641 #if SYM_CONF_MAX_LUN <= 1
642 #define sym_lp(np, tp, lun) (!lun) ? (tp)->lun0p : 0
643 #else
644 #define sym_lp(np, tp, lun) \
645         (!lun) ? (tp)->lun0p : (tp)->lunmp ? (tp)->lunmp[(lun)] : 0
646 #endif
647
648 /*
649  *  Status are used by the host and the script processor.
650  *
651  *  The last four bytes (status[4]) are copied to the 
652  *  scratchb register (declared as scr0..scr3) just after the 
653  *  select/reselect, and copied back just after disconnecting.
654  *  Inside the script the XX_REG are used.
655  */
656
657 /*
658  *  Last four bytes (script)
659  */
660 #define  HX_REG scr0
661 #define  HX_PRT nc_scr0
662 #define  HS_REG scr1
663 #define  HS_PRT nc_scr1
664 #define  SS_REG scr2
665 #define  SS_PRT nc_scr2
666 #define  HF_REG scr3
667 #define  HF_PRT nc_scr3
668
669 /*
670  *  Last four bytes (host)
671  */
672 #define  host_xflags   phys.head.status[0]
673 #define  host_status   phys.head.status[1]
674 #define  ssss_status   phys.head.status[2]
675 #define  host_flags    phys.head.status[3]
676
677 /*
678  *  Host flags
679  */
680 #define HF_IN_PM0       1u
681 #define HF_IN_PM1       (1u<<1)
682 #define HF_ACT_PM       (1u<<2)
683 #define HF_DP_SAVED     (1u<<3)
684 #define HF_SENSE        (1u<<4)
685 #define HF_EXT_ERR      (1u<<5)
686 #define HF_DATA_IN      (1u<<6)
687 #ifdef SYM_CONF_IARB_SUPPORT
688 #define HF_HINT_IARB    (1u<<7)
689 #endif
690
691 /*
692  *  More host flags
693  */
694 #if     SYM_CONF_DMA_ADDRESSING_MODE == 2
695 #define HX_DMAP_DIRTY   (1u<<7)
696 #endif
697
698 /*
699  *  Global CCB HEADER.
700  *
701  *  Due to lack of indirect addressing on earlier NCR chips,
702  *  this substructure is copied from the ccb to a global 
703  *  address after selection (or reselection) and copied back 
704  *  before disconnect.
705  *  For SYMBIOS chips that support LOAD/STORE this copy is 
706  *  not needed and thus not performed.
707  */
708
709 struct sym_ccbh {
710         /*
711          *  Start and restart SCRIPTS addresses (must be at 0).
712          */
713 /*0*/   struct sym_actscr go;
714
715         /*
716          *  SCRIPTS jump address that deal with data pointers.
717          *  'savep' points to the position in the script responsible 
718          *  for the actual transfer of data.
719          *  It's written on reception of a SAVE_DATA_POINTER message.
720          */
721         u32     savep;          /* Jump address to saved data pointer   */
722         u32     lastp;          /* SCRIPTS address at end of data       */
723 #ifdef  SYM_OPT_HANDLE_DIR_UNKNOWN
724         u32     wlastp;
725 #endif
726
727         /*
728          *  Status fields.
729          */
730         u8      status[4];
731 };
732
733 /*
734  *  GET/SET the value of the data pointer used by SCRIPTS.
735  *
736  *  We must distinguish between the LOAD/STORE-based SCRIPTS 
737  *  that use directly the header in the CCB, and the NCR-GENERIC 
738  *  SCRIPTS that use the copy of the header in the HCB.
739  */
740 #if     SYM_CONF_GENERIC_SUPPORT
741 #define sym_set_script_dp(np, cp, dp)                           \
742         do {                                                    \
743                 if (np->features & FE_LDSTR)                    \
744                         cp->phys.head.lastp = cpu_to_scr(dp);   \
745                 else                                            \
746                         np->ccb_head.lastp = cpu_to_scr(dp);    \
747         } while (0)
748 #define sym_get_script_dp(np, cp)                               \
749         scr_to_cpu((np->features & FE_LDSTR) ?                  \
750                 cp->phys.head.lastp : np->ccb_head.lastp)
751 #else
752 #define sym_set_script_dp(np, cp, dp)                           \
753         do {                                                    \
754                 cp->phys.head.lastp = cpu_to_scr(dp);           \
755         } while (0)
756
757 #define sym_get_script_dp(np, cp) (cp->phys.head.lastp)
758 #endif
759
760 /*
761  *  Data Structure Block
762  *
763  *  During execution of a ccb by the script processor, the 
764  *  DSA (data structure address) register points to this 
765  *  substructure of the ccb.
766  */
767 struct sym_dsb {
768         /*
769          *  CCB header.
770          *  Also assumed at offset 0 of the sym_ccb structure.
771          */
772 /*0*/   struct sym_ccbh head;
773
774         /*
775          *  Phase mismatch contexts.
776          *  We need two to handle correctly the SAVED DATA POINTER.
777          *  MUST BOTH BE AT OFFSET < 256, due to using 8 bit arithmetic 
778          *  for address calculation from SCRIPTS.
779          */
780         struct sym_pmc pm0;
781         struct sym_pmc pm1;
782
783         /*
784          *  Table data for Script
785          */
786         struct sym_tblsel  select;
787         struct sym_tblmove smsg;
788         struct sym_tblmove smsg_ext;
789         struct sym_tblmove cmd;
790         struct sym_tblmove sense;
791         struct sym_tblmove wresid;
792         struct sym_tblmove data [SYM_CONF_MAX_SG];
793 };
794
795 /*
796  *  Our Command Control Block
797  */
798 struct sym_ccb {
799         /*
800          *  This is the data structure which is pointed by the DSA 
801          *  register when it is executed by the script processor.
802          *  It must be the first entry.
803          */
804         struct sym_dsb phys;
805
806         /*
807          *  Pointer to CAM ccb and related stuff.
808          */
809         cam_ccb_p cam_ccb;      /* CAM scsiio ccb               */
810         u8      cdb_buf[16];    /* Copy of CDB                  */
811         u8      *sns_bbuf;      /* Bounce buffer for sense data */
812 #ifndef SYM_SNS_BBUF_LEN
813 #define SYM_SNS_BBUF_LEN (32)
814 #endif  
815         int     data_len;       /* Total data length            */
816         int     segments;       /* Number of SG segments        */
817
818         u8      order;          /* Tag type (if tagged command) */
819
820         /*
821          *  Miscellaneous status'.
822          */
823         u_char  nego_status;    /* Negotiation status           */
824         u_char  xerr_status;    /* Extended error flags         */
825         u32     extra_bytes;    /* Extraneous bytes transferred */
826
827         /*
828          *  Message areas.
829          *  We prepare a message to be sent after selection.
830          *  We may use a second one if the command is rescheduled 
831          *  due to CHECK_CONDITION or COMMAND TERMINATED.
832          *  Contents are IDENTIFY and SIMPLE_TAG.
833          *  While negotiating sync or wide transfer,
834          *  a SDTR or WDTR message is appended.
835          */
836         u_char  scsi_smsg [12];
837         u_char  scsi_smsg2[12];
838
839         /*
840          *  Auto request sense related fields.
841          */
842         u_char  sensecmd[6];    /* Request Sense command        */
843         u_char  sv_scsi_status; /* Saved SCSI status            */
844         u_char  sv_xerr_status; /* Saved extended status        */
845         int     sv_resid;       /* Saved residual               */
846
847         /*
848          *  O/S specific data structure.
849          */
850 #ifdef  SYM_HAVE_SCCB
851         struct sym_sccb s;
852 #endif
853         /*
854          *  Other fields.
855          */
856 #ifdef  SYM_OPT_HANDLE_IO_TIMEOUT
857         SYM_QUEHEAD tmo_linkq;  /* Optional timeout handling    */
858         u_int   tmo_clock;      /* (link and dealine value)     */
859 #endif
860         u32     ccb_ba;         /* BUS address of this CCB      */
861         u_short tag;            /* Tag for this transfer        */
862                                 /*  NO_TAG means no tag         */
863         u_char  target;
864         u_char  lun;
865         ccb_p   link_ccbh;      /* Host adapter CCB hash chain  */
866         SYM_QUEHEAD
867                 link_ccbq;      /* Link to free/busy CCB queue  */
868         u32     startp;         /* Initial data pointer         */
869         u32     goalp;          /* Expected last data pointer   */
870 #ifdef  SYM_OPT_HANDLE_DIR_UNKNOWN
871         u32     wgoalp;
872 #endif
873         int     ext_sg;         /* Extreme data pointer, used   */
874         int     ext_ofs;        /*  to calculate the residual.  */
875 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
876         SYM_QUEHEAD
877                 link2_ccbq;     /* Link for device queueing     */
878         u_char  started;        /* CCB queued to the squeue     */
879 #endif
880         u_char  to_abort;       /* Want this IO to be aborted   */
881 #ifdef SYM_OPT_LIMIT_COMMAND_REORDERING
882         u_char  tags_si;        /* Lun tags sum index (0,1)     */
883 #endif
884 };
885
886 #define CCB_BA(cp,lbl)  (cp->ccb_ba + offsetof(struct sym_ccb, lbl))
887
888 #ifdef  SYM_OPT_HANDLE_DIR_UNKNOWN
889 #define sym_goalp(cp) ((cp->host_flags & HF_DATA_IN) ? cp->goalp : cp->wgoalp)
890 #else
891 #define sym_goalp(cp) (cp->goalp)
892 #endif
893
894 /*
895  *  Host Control Block
896  */
897 struct sym_hcb {
898         /*
899          *  Global headers.
900          *  Due to poorness of addressing capabilities, earlier 
901          *  chips (810, 815, 825) copy part of the data structures 
902          *  (CCB, TCB and LCB) in fixed areas.
903          */
904 #if     SYM_CONF_GENERIC_SUPPORT
905         struct sym_ccbh ccb_head;
906         struct sym_tcbh tcb_head;
907         struct sym_lcbh lcb_head;
908 #endif
909         /*
910          *  Idle task and invalid task actions and 
911          *  their bus addresses.
912          */
913         struct sym_actscr idletask, notask, bad_itl, bad_itlq;
914         u32 idletask_ba, notask_ba, bad_itl_ba, bad_itlq_ba;
915
916         /*
917          *  Dummy lun table to protect us against target 
918          *  returning bad lun number on reselection.
919          */
920         u32     *badluntbl;     /* Table physical address       */
921         u32     badlun_sa;      /* SCRIPT handler BUS address   */
922
923         /*
924          *  Bus address of this host control block.
925          */
926         u32     hcb_ba;
927
928         /*
929          *  Bit 32-63 of the on-chip RAM bus address in LE format.
930          *  The START_RAM64 script loads the MMRS and MMWS from this 
931          *  field.
932          */
933         u32     scr_ram_seg;
934
935         /*
936          *  Initial value of some IO register bits.
937          *  These values are assumed to have been set by BIOS, and may 
938          *  be used to probe adapter implementation differences.
939          */
940         u_char  sv_scntl0, sv_scntl3, sv_dmode, sv_dcntl, sv_ctest3, sv_ctest4,
941                 sv_ctest5, sv_gpcntl, sv_stest2, sv_stest4, sv_scntl4,
942                 sv_stest1;
943
944         /*
945          *  Actual initial value of IO register bits used by the 
946          *  driver. They are loaded at initialisation according to  
947          *  features that are to be enabled/disabled.
948          */
949         u_char  rv_scntl0, rv_scntl3, rv_dmode, rv_dcntl, rv_ctest3, rv_ctest4, 
950                 rv_ctest5, rv_stest2, rv_ccntl0, rv_ccntl1, rv_scntl4;
951
952         /*
953          *  Target data.
954          */
955         struct sym_tcb  target[SYM_CONF_MAX_TARGET];
956
957         /*
958          *  Target control block bus address array used by the SCRIPT 
959          *  on reselection.
960          */
961         u32             *targtbl;
962         u32             targtbl_ba;
963
964         /*
965          *  DMA pool handle for this HBA.
966          */
967 #ifdef  SYM_OPT_BUS_DMA_ABSTRACTION
968         m_pool_ident_t  bus_dmat;
969 #endif
970
971         /*
972          *  O/S specific data structure
973          */
974         struct sym_shcb s;
975
976         /*
977          *  Physical bus addresses of the chip.
978          */
979         u32             mmio_ba;        /* MMIO 32 bit BUS address      */
980         int             mmio_ws;        /* MMIO Window size             */
981
982         u32             ram_ba;         /* RAM 32 bit BUS address       */
983         int             ram_ws;         /* RAM window size              */
984
985         /*
986          *  SCRIPTS virtual and physical bus addresses.
987          *  'script'  is loaded in the on-chip RAM if present.
988          *  'scripth' stays in main memory for all chips except the 
989          *  53C895A, 53C896 and 53C1010 that provide 8K on-chip RAM.
990          */
991         u_char          *scripta0;      /* Copy of scripts A, B, Z      */
992         u_char          *scriptb0;
993         u_char          *scriptz0;
994         u32             scripta_ba;     /* Actual scripts A, B, Z       */
995         u32             scriptb_ba;     /* 32 bit bus addresses.        */
996         u32             scriptz_ba;
997         u_short         scripta_sz;     /* Actual size of script A, B, Z*/
998         u_short         scriptb_sz;
999         u_short         scriptz_sz;
1000
1001         /*
1002          *  Bus addresses, setup and patch methods for 
1003          *  the selected firmware.
1004          */
1005         struct sym_fwa_ba fwa_bas;      /* Useful SCRIPTA bus addresses */
1006         struct sym_fwb_ba fwb_bas;      /* Useful SCRIPTB bus addresses */
1007         struct sym_fwz_ba fwz_bas;      /* Useful SCRIPTZ bus addresses */
1008         void            (*fw_setup)(hcb_p np, struct sym_fw *fw);
1009         void            (*fw_patch)(hcb_p np);
1010         char            *fw_name;
1011
1012         /*
1013          *  General controller parameters and configuration.
1014          */
1015         u_short device_id;      /* PCI device id                */
1016         u_char  revision_id;    /* PCI device revision id       */
1017         u_int   features;       /* Chip features map            */
1018         u_char  myaddr;         /* SCSI id of the adapter       */
1019         u_char  maxburst;       /* log base 2 of dwords burst   */
1020         u_char  maxwide;        /* Maximum transfer width       */
1021         u_char  minsync;        /* Min sync period factor (ST)  */
1022         u_char  maxsync;        /* Max sync period factor (ST)  */
1023         u_char  maxoffs;        /* Max scsi offset        (ST)  */
1024         u_char  minsync_dt;     /* Min sync period factor (DT)  */
1025         u_char  maxsync_dt;     /* Max sync period factor (DT)  */
1026         u_char  maxoffs_dt;     /* Max scsi offset        (DT)  */
1027         u_char  multiplier;     /* Clock multiplier (1,2,4)     */
1028         u_char  clock_divn;     /* Number of clock divisors     */
1029         u32     clock_khz;      /* SCSI clock frequency in KHz  */
1030         u32     pciclk_khz;     /* Estimated PCI clock  in KHz  */
1031         /*
1032          *  Start queue management.
1033          *  It is filled up by the host processor and accessed by the 
1034          *  SCRIPTS processor in order to start SCSI commands.
1035          */
1036         volatile                /* Prevent code optimizations   */
1037         u32     *squeue;        /* Start queue virtual address  */
1038         u32     squeue_ba;      /* Start queue BUS address      */
1039         u_short squeueput;      /* Next free slot of the queue  */
1040         u_short actccbs;        /* Number of allocated CCBs     */
1041
1042         /*
1043          *  Command completion queue.
1044          *  It is the same size as the start queue to avoid overflow.
1045          */
1046         u_short dqueueget;      /* Next position to scan        */
1047         volatile                /* Prevent code optimizations   */
1048         u32     *dqueue;        /* Completion (done) queue      */
1049         u32     dqueue_ba;      /* Done queue BUS address       */
1050
1051         /*
1052          *  Miscellaneous buffers accessed by the scripts-processor.
1053          *  They shall be DWORD aligned, because they may be read or 
1054          *  written with a script command.
1055          */
1056         u_char          msgout[8];      /* Buffer for MESSAGE OUT       */
1057         u_char          msgin [8];      /* Buffer for MESSAGE IN        */
1058         u32             lastmsg;        /* Last SCSI message sent       */
1059         u32             scratch;        /* Scratch for SCSI receive     */
1060                                         /* Also used for cache test     */
1061         /*
1062          *  Miscellaneous configuration and status parameters.
1063          */
1064         u_char          usrflags;       /* Miscellaneous user flags     */
1065         u_char          scsi_mode;      /* Current SCSI BUS mode        */
1066         u_char          verbose;        /* Verbosity for this controller*/
1067
1068         /*
1069          *  CCB lists and queue.
1070          */
1071         ccb_p *ccbh;                    /* CCBs hashed by DSA value     */
1072                                         /* CCB_HASH_SIZE lists of CCBs  */
1073         SYM_QUEHEAD     free_ccbq;      /* Queue of available CCBs      */
1074         SYM_QUEHEAD     busy_ccbq;      /* Queue of busy CCBs           */
1075
1076         /*
1077          *  During error handling and/or recovery,
1078          *  active CCBs that are to be completed with 
1079          *  error or requeued are moved from the busy_ccbq
1080          *  to the comp_ccbq prior to completion.
1081          */
1082         SYM_QUEHEAD     comp_ccbq;
1083
1084 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
1085         SYM_QUEHEAD     dummy_ccbq;
1086 #endif
1087         /*
1088          *  Optional handling of IO timeouts.
1089          */
1090 #ifdef  SYM_OPT_HANDLE_IO_TIMEOUT
1091         SYM_QUEHEAD tmo0_ccbq;
1092         SYM_QUEHEAD *tmo_ccbq;  /* [2*SYM_TIMEOUT_ORDER_MAX] */
1093         u_int   tmo_clock;
1094         u_int   tmo_actq;
1095 #endif
1096
1097         /*
1098          *  IMMEDIATE ARBITRATION (IARB) control.
1099          *
1100          *  We keep track in 'last_cp' of the last CCB that has been 
1101          *  queued to the SCRIPTS processor and clear 'last_cp' when 
1102          *  this CCB completes. If last_cp is not zero at the moment 
1103          *  we queue a new CCB, we set a flag in 'last_cp' that is 
1104          *  used by the SCRIPTS as a hint for setting IARB.
1105          *  We donnot set more than 'iarb_max' consecutive hints for 
1106          *  IARB in order to leave devices a chance to reselect.
1107          *  By the way, any non zero value of 'iarb_max' is unfair. :)
1108          */
1109 #ifdef SYM_CONF_IARB_SUPPORT
1110         u_short         iarb_max;       /* Max. # consecutive IARB hints*/
1111         u_short         iarb_count;     /* Actual # of these hints      */
1112         ccb_p           last_cp;
1113 #endif
1114
1115         /*
1116          *  Command abort handling.
1117          *  We need to synchronize tightly with the SCRIPTS 
1118          *  processor in order to handle things correctly.
1119          */
1120         u_char          abrt_msg[4];    /* Message to send buffer       */
1121         struct sym_tblmove abrt_tbl;    /* Table for the MOV of it      */
1122         struct sym_tblsel  abrt_sel;    /* Sync params for selection    */
1123         u_char          istat_sem;      /* Tells the chip to stop (SEM) */
1124
1125         /*
1126          *  64 bit DMA handling.
1127          */
1128 #if     SYM_CONF_DMA_ADDRESSING_MODE != 0
1129         u_char  use_dac;                /* Use PCI DAC cycles           */
1130 #if     SYM_CONF_DMA_ADDRESSING_MODE == 2
1131         u_char  dmap_dirty;             /* Dma segments registers dirty */
1132         u32     dmap_bah[SYM_DMAP_SIZE];/* Segment registers map        */
1133 #endif
1134 #endif
1135 };
1136
1137 #define HCB_BA(np, lbl) (np->hcb_ba + offsetof(struct sym_hcb, lbl))
1138
1139 /*
1140  *  NVRAM reading (sym_nvram.c).
1141  */
1142 void sym_nvram_setup_host (hcb_p np, struct sym_nvram *nvram);
1143 void sym_nvram_setup_target (hcb_p np, int target, struct sym_nvram *nvp);
1144 int sym_read_nvram (sdev_p np, struct sym_nvram *nvp);
1145
1146 /*
1147  *  FIRMWARES (sym_fw.c)
1148  */
1149 struct sym_fw * sym_find_firmware(struct sym_pci_chip *chip);
1150 void sym_fw_bind_script (hcb_p np, u32 *start, int len);
1151
1152 /*
1153  *  Driver methods called from O/S specific code.
1154  */
1155 char *sym_driver_name(void);
1156 void sym_print_xerr(ccb_p cp, int x_status);
1157 int sym_reset_scsi_bus(hcb_p np, int enab_int);
1158 struct sym_pci_chip *
1159 sym_lookup_pci_chip_table (u_short device_id, u_char revision);
1160 void sym_put_start_queue(hcb_p np, ccb_p cp);
1161 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
1162 void sym_start_next_ccbs(hcb_p np, lcb_p lp, int maxn);
1163 #endif
1164 void sym_start_up (hcb_p np, int reason);
1165 void sym_interrupt (hcb_p np);
1166 void sym_flush_comp_queue(hcb_p np, int cam_status);
1167 int sym_clear_tasks(hcb_p np, int cam_status, int target, int lun, int task);
1168 ccb_p sym_get_ccb (hcb_p np, u_char tn, u_char ln, u_char tag_order);
1169 void sym_free_ccb (hcb_p np, ccb_p cp);
1170 lcb_p sym_alloc_lcb (hcb_p np, u_char tn, u_char ln);
1171 int sym_queue_scsiio(hcb_p np, cam_scsiio_p csio, ccb_p cp);
1172 int sym_abort_scsiio(hcb_p np, cam_ccb_p ccb, int timed_out);
1173 int sym_abort_ccb(hcb_p np, ccb_p cp, int timed_out);
1174 int sym_reset_scsi_target(hcb_p np, int target);
1175 void sym_hcb_free(hcb_p np);
1176
1177 #ifdef SYM_OPT_NVRAM_PRE_READ
1178 int sym_hcb_attach(hcb_p np, struct sym_fw *fw, struct sym_nvram *nvram);
1179 #else
1180 int sym_hcb_attach(hcb_p np, struct sym_fw *fw);
1181 #endif
1182
1183 /*
1184  *  Optionnaly, the driver may handle IO timeouts.
1185  */
1186 #ifdef  SYM_OPT_HANDLE_IO_TIMEOUT
1187 int sym_abort_ccb(hcb_p np, ccb_p cp, int timed_out);
1188 void sym_timeout_ccb(hcb_p np, ccb_p cp, u_int ticks);
1189 static void __inline sym_untimeout_ccb(hcb_p np, ccb_p cp)
1190 {
1191         sym_remque(&cp->tmo_linkq);
1192         sym_insque_head(&cp->tmo_linkq, &np->tmo0_ccbq);
1193 }
1194 void sym_clock(hcb_p np);
1195 #endif  /* SYM_OPT_HANDLE_IO_TIMEOUT */
1196
1197 /*
1198  *  Optionnaly, the driver may provide a function
1199  *  to announce transfer rate changes.
1200  */
1201 #ifdef  SYM_OPT_ANNOUNCE_TRANSFER_RATE
1202 void sym_announce_transfer_rate(hcb_p np, int target);
1203 #endif
1204
1205 /*
1206  *  Optionnaly, the driver may sniff inquiry data.
1207  */
1208 #ifdef  SYM_OPT_SNIFF_INQUIRY
1209 #define INQ7_CMDQ       (0x02)
1210 #define INQ7_SYNC       (0x10)
1211 #define INQ7_WIDE16     (0x20)
1212
1213 #define INQ56_CLOCKING  (3<<2)
1214 #define INQ56_ST_ONLY   (0<<2)
1215 #define INQ56_DT_ONLY   (1<<2)
1216 #define INQ56_ST_DT     (3<<2)
1217
1218 void sym_update_trans_settings(hcb_p np, tcb_p tp);
1219 int  
1220 __sym_sniff_inquiry(hcb_p np, u_char tn, u_char ln,
1221                     u_char *inq_data, int inq_len);
1222 #endif
1223
1224
1225 /*
1226  *  Build a scatter/gather entry.
1227  *
1228  *  For 64 bit systems, we use the 8 upper bits of the size field 
1229  *  to provide bus address bits 32-39 to the SCRIPTS processor.
1230  *  This allows the 895A, 896, 1010 to address up to 1 TB of memory.
1231  */
1232
1233 #if   SYM_CONF_DMA_ADDRESSING_MODE == 0
1234 #define sym_build_sge(np, data, badd, len)      \
1235 do {                                            \
1236         (data)->addr = cpu_to_scr(badd);        \
1237         (data)->size = cpu_to_scr(len);         \
1238 } while (0)
1239 #elif SYM_CONF_DMA_ADDRESSING_MODE == 1
1240 #define sym_build_sge(np, data, badd, len)                              \
1241 do {                                                                    \
1242         (data)->addr = cpu_to_scr(badd);                                \
1243         (data)->size = cpu_to_scr((((badd) >> 8) & 0xff000000) + len);  \
1244 } while (0)
1245 #elif SYM_CONF_DMA_ADDRESSING_MODE == 2
1246 int sym_lookup_dmap(hcb_p np, u32 h, int s);
1247 static __inline void 
1248 sym_build_sge(hcb_p np, struct sym_tblmove *data, u64 badd, int len)
1249 {
1250         u32 h = (badd>>32);
1251         int s = (h&SYM_DMAP_MASK);
1252
1253         if (h != np->dmap_bah[s])
1254                 goto bad;
1255 good:
1256         (data)->addr = cpu_to_scr(badd);
1257         (data)->size = cpu_to_scr((s<<24) + len);
1258         return;
1259 bad:
1260         s = sym_lookup_dmap(np, h, s);
1261         goto good;
1262 }
1263 #else
1264 #error "Unsupported DMA addressing mode"
1265 #endif
1266
1267 /*
1268  *  Set up data pointers used by SCRIPTS.
1269  *  Called from O/S specific code.
1270  */
1271 static void __inline 
1272 sym_setup_data_pointers(hcb_p np, ccb_p cp, int dir)
1273 {
1274         u32 lastp, goalp;
1275
1276         /*
1277          *  No segments means no data.
1278          */
1279         if (!cp->segments)
1280                 dir = CAM_DIR_NONE;
1281
1282         /*
1283          *  Set the data pointer.
1284          */
1285         switch(dir) {
1286 #ifdef  SYM_OPT_HANDLE_DIR_UNKNOWN
1287         case CAM_DIR_UNKNOWN:
1288 #endif
1289         case CAM_DIR_OUT:
1290                 goalp = SCRIPTA_BA (np, data_out2) + 8;
1291                 lastp = goalp - 8 - (cp->segments * (2*4));
1292 #ifdef  SYM_OPT_HANDLE_DIR_UNKNOWN
1293                 cp->wgoalp = cpu_to_scr(goalp);
1294                 if (dir != CAM_DIR_UNKNOWN)
1295                         break;
1296                 cp->phys.head.wlastp = cpu_to_scr(lastp);
1297                 /* fall through */
1298 #else
1299                 break;
1300 #endif
1301         case CAM_DIR_IN:
1302                 cp->host_flags |= HF_DATA_IN;
1303                 goalp = SCRIPTA_BA (np, data_in2) + 8;
1304                 lastp = goalp - 8 - (cp->segments * (2*4));
1305                 break;
1306         case CAM_DIR_NONE:
1307         default:
1308 #ifdef  SYM_OPT_HANDLE_DIR_UNKNOWN
1309                 cp->host_flags |= HF_DATA_IN;
1310 #endif
1311                 lastp = goalp = SCRIPTB_BA (np, no_data);
1312                 break;
1313         }
1314
1315         /*
1316          *  Set all pointers values needed by SCRIPTS.
1317          */
1318         cp->phys.head.lastp = cpu_to_scr(lastp);
1319         cp->phys.head.savep = cpu_to_scr(lastp);
1320         cp->startp          = cp->phys.head.savep;
1321         cp->goalp           = cpu_to_scr(goalp);
1322
1323 #ifdef  SYM_OPT_HANDLE_DIR_UNKNOWN
1324         /*
1325          *  If direction is unknown, start at data_io.
1326          */
1327         if (dir == CAM_DIR_UNKNOWN)
1328                 cp->phys.head.savep = cpu_to_scr(SCRIPTB_BA (np, data_io));
1329 #endif
1330 }
1331
1332 /*
1333  *  MEMORY ALLOCATOR.
1334  */
1335
1336 /*
1337  *  Shortest memory chunk is (1<<SYM_MEM_SHIFT), currently 16.
1338  *  Actual allocations happen as SYM_MEM_CLUSTER_SIZE sized.
1339  *  (1 PAGE at a time is just fine).
1340  */
1341 #define SYM_MEM_SHIFT   4
1342 #define SYM_MEM_CLUSTER_SIZE    (1UL << SYM_MEM_CLUSTER_SHIFT)
1343 #define SYM_MEM_CLUSTER_MASK    (SYM_MEM_CLUSTER_SIZE-1)
1344
1345 /*
1346  *  Link between free memory chunks of a given size.
1347  */
1348 typedef struct sym_m_link {
1349         struct sym_m_link *next;
1350 } *m_link_p;
1351
1352 /*
1353  *  Virtual to bus physical translation for a given cluster.
1354  *  Such a structure is only useful with DMA abstraction.
1355  */
1356 #ifdef  SYM_OPT_BUS_DMA_ABSTRACTION
1357 typedef struct sym_m_vtob {     /* Virtual to Bus address translation */
1358         struct sym_m_vtob *next;
1359 #ifdef  SYM_HAVE_M_SVTOB
1360         struct sym_m_svtob s;   /* OS specific data structure */
1361 #endif
1362         m_addr_t        vaddr;  /* Virtual address */
1363         m_addr_t        baddr;  /* Bus physical address */
1364 } *m_vtob_p;
1365
1366 /* Hash this stuff a bit to speed up translations */
1367 #define VTOB_HASH_SHIFT         5
1368 #define VTOB_HASH_SIZE          (1UL << VTOB_HASH_SHIFT)
1369 #define VTOB_HASH_MASK          (VTOB_HASH_SIZE-1)
1370 #define VTOB_HASH_CODE(m)       \
1371         ((((m_addr_t) (m)) >> SYM_MEM_CLUSTER_SHIFT) & VTOB_HASH_MASK)
1372 #endif  /* SYM_OPT_BUS_DMA_ABSTRACTION */
1373
1374 /*
1375  *  Memory pool of a given kind.
1376  *  Ideally, we want to use:
1377  *  1) 1 pool for memory we donnot need to involve in DMA.
1378  *  2) The same pool for controllers that require same DMA 
1379  *     constraints and features.
1380  *     The OS specific m_pool_id_t thing and the sym_m_pool_match() 
1381  *     method are expected to tell the driver about.
1382  */
1383 typedef struct sym_m_pool {
1384 #ifdef  SYM_OPT_BUS_DMA_ABSTRACTION
1385         m_pool_ident_t  dev_dmat;       /* Identifies the pool (see above) */
1386         m_addr_t (*get_mem_cluster)(struct sym_m_pool *);
1387 #ifdef  SYM_MEM_FREE_UNUSED
1388         void (*free_mem_cluster)(struct sym_m_pool *, m_addr_t);
1389 #endif
1390 #define M_GET_MEM_CLUSTER()             mp->get_mem_cluster(mp)
1391 #define M_FREE_MEM_CLUSTER(p)           mp->free_mem_cluster(mp, p)
1392 #ifdef  SYM_HAVE_M_SPOOL
1393         struct sym_m_spool      s;      /* OS specific data structure */
1394 #endif
1395         int nump;
1396         m_vtob_p vtob[VTOB_HASH_SIZE];
1397         struct sym_m_pool *next;
1398 #else
1399 #define M_GET_MEM_CLUSTER()             sym_get_mem_cluster()
1400 #define M_FREE_MEM_CLUSTER(p)           sym_free_mem_cluster(p)
1401 #endif  /* SYM_OPT_BUS_DMA_ABSTRACTION */
1402         struct sym_m_link h[SYM_MEM_CLUSTER_SHIFT - SYM_MEM_SHIFT + 1];
1403 } *m_pool_p;
1404
1405 /*
1406  *  Alloc and free non DMAable memory.
1407  */
1408 void sym_mfree_unlocked(void *ptr, int size, char *name);
1409 void *sym_calloc_unlocked(int size, char *name);
1410
1411 /*
1412  *  Alloc, free and translate addresses to bus physical 
1413  *  for DMAable memory.
1414  */
1415 #ifdef  SYM_OPT_BUS_DMA_ABSTRACTION
1416 void *__sym_calloc_dma_unlocked(m_pool_ident_t dev_dmat, int size, char *name);
1417 void 
1418 __sym_mfree_dma_unlocked(m_pool_ident_t dev_dmat, void *m,int size, char *name);
1419 u32 __vtobus_unlocked(m_pool_ident_t dev_dmat, void *m);
1420 #endif
1421
1422 /*
1423  * Verbs used by the driver code for DMAable memory handling.
1424  * The _uvptv_ macro avoids a nasty warning about pointer to volatile 
1425  * being discarded.
1426  */
1427 #define _uvptv_(p) ((void *)((u_long)(p)))
1428
1429 #define _sym_calloc_dma(np, l, n)       __sym_calloc_dma(np->bus_dmat, l, n)
1430 #define _sym_mfree_dma(np, p, l, n)     \
1431                         __sym_mfree_dma(np->bus_dmat, _uvptv_(p), l, n)
1432 #define sym_calloc_dma(l, n)            _sym_calloc_dma(np, l, n)
1433 #define sym_mfree_dma(p, l, n)          _sym_mfree_dma(np, p, l, n)
1434 #define _vtobus(np, p)                  __vtobus(np->bus_dmat, _uvptv_(p))
1435 #define vtobus(p)                       _vtobus(np, p)
1436
1437 /*
1438  *  Override some function names.
1439  */
1440 #define PRINT_ADDR      sym_print_addr
1441 #define PRINT_TARGET    sym_print_target
1442 #define PRINT_LUN       sym_print_lun
1443 #define MDELAY          sym_mdelay
1444 #define UDELAY          sym_udelay
1445
1446 #endif /* SYM_HIPD_H */