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