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