ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / scsi / sym53c8xx_2 / sym_misc.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_MISC_H
54 #define SYM_MISC_H
55
56 /*
57  *  A la VMS/CAM-3 queue management.
58  */
59 typedef struct sym_quehead {
60         struct sym_quehead *flink;      /* Forward  pointer */
61         struct sym_quehead *blink;      /* Backward pointer */
62 } SYM_QUEHEAD;
63
64 #define sym_que_init(ptr) do { \
65         (ptr)->flink = (ptr); (ptr)->blink = (ptr); \
66 } while (0)
67
68 static __inline struct sym_quehead *sym_que_first(struct sym_quehead *head)
69 {
70         return (head->flink == head) ? 0 : head->flink;
71 }
72
73 static __inline struct sym_quehead *sym_que_last(struct sym_quehead *head)
74 {
75         return (head->blink == head) ? 0 : head->blink;
76 }
77
78 static __inline void __sym_que_add(struct sym_quehead * new,
79         struct sym_quehead * blink,
80         struct sym_quehead * flink)
81 {
82         flink->blink    = new;
83         new->flink      = flink;
84         new->blink      = blink;
85         blink->flink    = new;
86 }
87
88 static __inline void __sym_que_del(struct sym_quehead * blink,
89         struct sym_quehead * flink)
90 {
91         flink->blink = blink;
92         blink->flink = flink;
93 }
94
95 static __inline int sym_que_empty(struct sym_quehead *head)
96 {
97         return head->flink == head;
98 }
99
100 static __inline void sym_que_splice(struct sym_quehead *list,
101         struct sym_quehead *head)
102 {
103         struct sym_quehead *first = list->flink;
104
105         if (first != list) {
106                 struct sym_quehead *last = list->blink;
107                 struct sym_quehead *at   = head->flink;
108
109                 first->blink = head;
110                 head->flink  = first;
111
112                 last->flink = at;
113                 at->blink   = last;
114         }
115 }
116
117 static __inline void sym_que_move(struct sym_quehead *orig,
118         struct sym_quehead *dest)
119 {
120         struct sym_quehead *first, *last;
121
122         first = orig->flink;
123         if (first != orig) {
124                 first->blink = dest;
125                 dest->flink  = first;
126                 last = orig->blink;
127                 last->flink  = dest;
128                 dest->blink  = last;
129                 orig->flink  = orig;
130                 orig->blink  = orig;
131         } else {
132                 dest->flink  = dest;
133                 dest->blink  = dest;
134         }
135 }
136
137 #define sym_que_entry(ptr, type, member) \
138         ((type *)((char *)(ptr)-(unsigned int)(&((type *)0)->member)))
139
140
141 #define sym_insque(new, pos)            __sym_que_add(new, pos, (pos)->flink)
142
143 #define sym_remque(el)                  __sym_que_del((el)->blink, (el)->flink)
144
145 #define sym_insque_head(new, head)      __sym_que_add(new, head, (head)->flink)
146
147 static __inline struct sym_quehead *sym_remque_head(struct sym_quehead *head)
148 {
149         struct sym_quehead *elem = head->flink;
150
151         if (elem != head)
152                 __sym_que_del(head, elem->flink);
153         else
154                 elem = 0;
155         return elem;
156 }
157
158 #define sym_insque_tail(new, head)      __sym_que_add(new, (head)->blink, head)
159
160 static __inline struct sym_quehead *sym_remque_tail(struct sym_quehead *head)
161 {
162         struct sym_quehead *elem = head->blink;
163
164         if (elem != head)
165                 __sym_que_del(elem->blink, head);
166         else
167                 elem = 0;
168         return elem;
169 }
170
171 /*
172  *  This one may be useful.
173  */
174 #define FOR_EACH_QUEUED_ELEMENT(head, qp) \
175         for (qp = (head)->flink; qp != (head); qp = qp->flink)
176 /*
177  *  FreeBSD does not offer our kind of queue in the CAM CCB.
178  *  So, we have to cast.
179  */
180 #define sym_qptr(p)     ((struct sym_quehead *) (p))
181
182 /*
183  *  Simple bitmap operations.
184  */ 
185 #define sym_set_bit(p, n)       (((u32 *)(p))[(n)>>5] |=  (1<<((n)&0x1f)))
186 #define sym_clr_bit(p, n)       (((u32 *)(p))[(n)>>5] &= ~(1<<((n)&0x1f)))
187 #define sym_is_bit(p, n)        (((u32 *)(p))[(n)>>5] &   (1<<((n)&0x1f)))
188
189 /*
190  * The below round up/down macros are to be used with a constant 
191  * as argument (sizeof(...) for example), for the compiler to 
192  * optimize the whole thing.
193  */
194 #define _U_(a,m)        (a)<=(1<<m)?m:
195
196 /*
197  * Round up logarithm to base 2 of a 16 bit constant.
198  */
199 #define _LGRU16_(a) \
200 ( \
201  _U_(a, 0)_U_(a, 1)_U_(a, 2)_U_(a, 3)_U_(a, 4)_U_(a, 5)_U_(a, 6)_U_(a, 7) \
202  _U_(a, 8)_U_(a, 9)_U_(a,10)_U_(a,11)_U_(a,12)_U_(a,13)_U_(a,14)_U_(a,15) \
203  16)
204
205 #endif /* SYM_MISC_H */