MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / scsi / sym53c8xx_2 / sym_misc.h
blob4568f0c222db58696e654a0ca1284df333b897dd
1 /*
2 * Device driver for the SYMBIOS/LSILOGIC 53C8XX and 53C1010 family
3 * of PCI-SCSI IO processors.
5 * Copyright (C) 1999-2001 Gerard Roudier <groudier@free.fr>
7 * This driver is derived from the Linux sym53c8xx driver.
8 * Copyright (C) 1998-2000 Gerard Roudier
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.
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
18 * Other major contributions:
20 * NVRAM detection and reading.
21 * Copyright (C) 1997 Richard Waltham <dormouse@farsrobt.demon.co.uk>
23 *-----------------------------------------------------------------------------
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.
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.
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.
53 #ifndef SYM_MISC_H
54 #define SYM_MISC_H
57 * A la VMS/CAM-3 queue management.
59 typedef struct sym_quehead {
60 struct sym_quehead *flink; /* Forward pointer */
61 struct sym_quehead *blink; /* Backward pointer */
62 } SYM_QUEHEAD;
64 #define sym_que_init(ptr) do { \
65 (ptr)->flink = (ptr); (ptr)->blink = (ptr); \
66 } while (0)
68 static __inline struct sym_quehead *sym_que_first(struct sym_quehead *head)
70 return (head->flink == head) ? 0 : head->flink;
73 static __inline struct sym_quehead *sym_que_last(struct sym_quehead *head)
75 return (head->blink == head) ? 0 : head->blink;
78 static __inline void __sym_que_add(struct sym_quehead * new,
79 struct sym_quehead * blink,
80 struct sym_quehead * flink)
82 flink->blink = new;
83 new->flink = flink;
84 new->blink = blink;
85 blink->flink = new;
88 static __inline void __sym_que_del(struct sym_quehead * blink,
89 struct sym_quehead * flink)
91 flink->blink = blink;
92 blink->flink = flink;
95 static __inline int sym_que_empty(struct sym_quehead *head)
97 return head->flink == head;
100 static __inline void sym_que_splice(struct sym_quehead *list,
101 struct sym_quehead *head)
103 struct sym_quehead *first = list->flink;
105 if (first != list) {
106 struct sym_quehead *last = list->blink;
107 struct sym_quehead *at = head->flink;
109 first->blink = head;
110 head->flink = first;
112 last->flink = at;
113 at->blink = last;
117 static __inline void sym_que_move(struct sym_quehead *orig,
118 struct sym_quehead *dest)
120 struct sym_quehead *first, *last;
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;
137 #define sym_que_entry(ptr, type, member) \
138 ((type *)((char *)(ptr)-(unsigned int)(&((type *)0)->member)))
141 #define sym_insque(new, pos) __sym_que_add(new, pos, (pos)->flink)
143 #define sym_remque(el) __sym_que_del((el)->blink, (el)->flink)
145 #define sym_insque_head(new, head) __sym_que_add(new, head, (head)->flink)
147 static __inline struct sym_quehead *sym_remque_head(struct sym_quehead *head)
149 struct sym_quehead *elem = head->flink;
151 if (elem != head)
152 __sym_que_del(head, elem->flink);
153 else
154 elem = NULL;
155 return elem;
158 #define sym_insque_tail(new, head) __sym_que_add(new, (head)->blink, head)
160 static __inline struct sym_quehead *sym_remque_tail(struct sym_quehead *head)
162 struct sym_quehead *elem = head->blink;
164 if (elem != head)
165 __sym_que_del(elem->blink, head);
166 else
167 elem = 0;
168 return elem;
172 * This one may be useful.
174 #define FOR_EACH_QUEUED_ELEMENT(head, qp) \
175 for (qp = (head)->flink; qp != (head); qp = qp->flink)
177 * FreeBSD does not offer our kind of queue in the CAM CCB.
178 * So, we have to cast.
180 #define sym_qptr(p) ((struct sym_quehead *) (p))
183 * Simple bitmap operations.
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)))
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.
194 #define _U_(a,m) (a)<=(1<<m)?m:
197 * Round up logarithm to base 2 of a 16 bit constant.
199 #define _LGRU16_(a) \
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) \
205 #endif /* SYM_MISC_H */