- Update Xen patches to 3.3-rc5 and c/s 1157.
[linux-flexiantxendom0-3.2.10.git] / drivers / xen / sfc_netback / ci / compat / utils.h
1 /****************************************************************************
2  * Copyright 2002-2005: Level 5 Networks Inc.
3  * Copyright 2005-2008: Solarflare Communications Inc,
4  *                      9501 Jeronimo Road, Suite 250,
5  *                      Irvine, CA 92618, USA
6  *
7  * Maintained by Solarflare Communications
8  *  <linux-xen-drivers@solarflare.com>
9  *  <onload-dev@solarflare.com>
10  *
11  * This program is free software; you can redistribute it and/or modify it
12  * under the terms of the GNU General Public License version 2 as published
13  * by the Free Software Foundation, incorporated herein by reference.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23  ****************************************************************************
24  */
25
26 /*
27  * \author  djr
28  *  \brief  Handy utility macros.
29  *   \date  2003/01/17
30  */
31
32 /*! \cidoxg_include_ci_compat  */
33
34 #ifndef __CI_COMPAT_UTILS_H__
35 #define __CI_COMPAT_UTILS_H__
36
37
38 /**********************************************************************
39  * Alignment -- [align] must be a power of 2.
40  **********************************************************************/
41
42   /*! Align forward onto next boundary. */
43
44 #define CI_ALIGN_FWD(p, align)               (((p)+(align)-1u) & ~((align)-1u))
45
46
47   /*! Align back onto prev boundary. */
48
49 #define CI_ALIGN_BACK(p, align)              ((p) & ~((align)-1u))
50
51
52   /*! How far to next boundary? */
53
54 #define CI_ALIGN_NEEDED(p, align, signed_t)  (-(signed_t)(p) & ((align)-1u))
55
56
57   /*! How far beyond prev boundary? */
58
59 #define CI_OFFSET(p, align)                  ((p) & ((align)-1u))
60
61
62   /*! Does object fit in gap before next boundary? */
63
64 #define CI_FITS(p, size, align, signed_t)                       \
65   (CI_ALIGN_NEEDED((p) + 1, (align), signed_t) + 1 >= (size))
66
67
68   /*! Align forward onto next boundary. */
69
70 #define CI_PTR_ALIGN_FWD(p, align)                                         \
71   ((char*) CI_ALIGN_FWD(((ci_ptr_arith_t)(p)), ((ci_ptr_arith_t)(align))))
72
73   /*! Align back onto prev boundary. */
74
75 #define CI_PTR_ALIGN_BACK(p, align)                                         \
76   ((char*) CI_ALIGN_BACK(((ci_ptr_arith_t)(p)), ((ci_ptr_arith_t)(align))))
77
78   /*! How far to next boundary? */
79
80 #define CI_PTR_ALIGN_NEEDED(p, align)                                   \
81   CI_ALIGN_NEEDED(((ci_ptr_arith_t)(p)), ((ci_ptr_arith_t)(align)),     \
82                   ci_ptr_arith_t)
83
84   /*! How far to next boundary? NZ = not zero i.e. give align if on boundary  */
85
86 #define CI_PTR_ALIGN_NEEDED_NZ(p, align)                                        \
87   ((align) - (((char*)p) -                                                      \
88   ((char*) CI_ALIGN_BACK(((ci_ptr_arith_t)(p)), ((ci_ptr_arith_t)(align))))))
89
90   /*! How far beyond prev boundary? */
91
92 #define CI_PTR_OFFSET(p, align)                                 \
93   CI_OFFSET(((ci_ptr_arith_t)(p)), ((ci_ptr_arith_t)(align)))
94
95
96   /* Same as CI_ALIGN_FWD and CI_ALIGN_BACK. */
97
98 #define CI_ROUND_UP(i, align)      (((i)+(align)-1u) & ~((align)-1u))
99
100 #define CI_ROUND_DOWN(i, align)    ((i) & ~((align)-1u))
101
102
103 /**********************************************************************
104  * Byte-order
105  **********************************************************************/
106
107 /* These are not flags.  They are enumeration values for use with
108  * CI_MY_BYTE_ORDER. */
109 #define CI_BIG_ENDIAN          1
110 #define CI_LITTLE_ENDIAN       0
111
112 /*
113 ** Note that these byte-swapping primitives may leave junk in bits above
114 ** the range they operate on.
115 **
116 ** The CI_BSWAP_nn() routines require that bits above [nn] are zero.  Use
117 ** CI_BSWAPM_nn(x) if this cannot be guaranteed.
118 */
119
120 /* ?? May be able to improve on some of these with inline assembler on some
121 ** platforms.
122 */
123
124 #define CI_BSWAP_16(v)    ((((v) & 0xff) << 8) | ((v) >> 8))
125 #define CI_BSWAPM_16(v)   ((((v) & 0xff) << 8) | (((v) & 0xff00) >> 8))
126
127 #define CI_BSWAP_32(v)    (((v) >> 24)               |  \
128                            (((v) & 0x00ff0000) >> 8) |  \
129                            (((v) & 0x0000ff00) << 8) |  \
130                            ((v) << 24))
131 #define CI_BSWAPM_32(v)   ((((v) & 0xff000000) >> 24) | \
132                            (((v) & 0x00ff0000) >> 8)  | \
133                            (((v) & 0x0000ff00) << 8)  | \
134                            ((v) << 24))
135
136 #define CI_BSWAP_64(v)    (((v) >> 56)                        | \
137                            (((v) & 0x00ff000000000000) >> 40) | \
138                            (((v) & 0x0000ff0000000000) >> 24) | \
139                            (((v) & 0x000000ff00000000) >> 8)  | \
140                            (((v) & 0x00000000ff000000) << 8)  | \
141                            (((v) & 0x0000000000ff0000) << 24) | \
142                            (((v) & 0x000000000000ff00) << 40) | \
143                            ((v) << 56))
144
145 # define CI_BSWAPPED_16_IF(c,v)  ((c) ? CI_BSWAP_16(v) : (v))
146 # define CI_BSWAPPED_32_IF(c,v)  ((c) ? CI_BSWAP_32(v) : (v))
147 # define CI_BSWAPPED_64_IF(c,v)  ((c) ? CI_BSWAP_64(v) : (v))
148 # define CI_BSWAP_16_IF(c,v)     do{ if((c)) (v) = CI_BSWAP_16(v); }while(0)
149 # define CI_BSWAP_32_IF(c,v)     do{ if((c)) (v) = CI_BSWAP_32(v); }while(0)
150 # define CI_BSWAP_64_IF(c,v)     do{ if((c)) (v) = CI_BSWAP_64(v); }while(0)
151
152 #if (CI_MY_BYTE_ORDER == CI_LITTLE_ENDIAN)
153 # define CI_BSWAP_LE16(v)    (v)
154 # define CI_BSWAP_LE32(v)    (v)
155 # define CI_BSWAP_LE64(v)    (v)
156 # define CI_BSWAP_BE16(v)    CI_BSWAP_16(v)
157 # define CI_BSWAP_BE32(v)    CI_BSWAP_32(v)
158 # define CI_BSWAP_BE64(v)    CI_BSWAP_64(v)
159 # define CI_BSWAPM_LE16(v)   (v)
160 # define CI_BSWAPM_LE32(v)   (v)
161 # define CI_BSWAPM_LE64(v)   (v)
162 # define CI_BSWAPM_BE16(v)   CI_BSWAPM_16(v)
163 # define CI_BSWAPM_BE32(v)   CI_BSWAPM_32(v)
164 #elif (CI_MY_BYTE_ORDER == CI_BIG_ENDIAN)
165 # define CI_BSWAP_BE16(v)    (v)
166 # define CI_BSWAP_BE32(v)    (v)
167 # define CI_BSWAP_BE64(v)    (v)
168 # define CI_BSWAP_LE16(v)    CI_BSWAP_16(v)
169 # define CI_BSWAP_LE32(v)    CI_BSWAP_32(v)
170 # define CI_BSWAP_LE64(v)    CI_BSWAP_64(v)
171 # define CI_BSWAPM_BE16(v)   (v)
172 # define CI_BSWAPM_BE32(v)   (v)
173 # define CI_BSWAPM_BE64(v)   (v)
174 # define CI_BSWAPM_LE16(v)   CI_BSWAPM_16(v)
175 # define CI_BSWAPM_LE32(v)   CI_BSWAPM_32(v)
176 #else
177 # error Bad endian.
178 #endif
179
180
181 /**********************************************************************
182  * Get pointer to struct from pointer to member
183  **********************************************************************/
184
185 #define CI_MEMBER_OFFSET(c_type, mbr_name)  \
186   ((ci_uint32) (ci_uintptr_t)(&((c_type*)0)->mbr_name))
187
188 #define CI_MEMBER_SIZE(c_type, mbr_name)        \
189   sizeof(((c_type*)0)->mbr_name)
190
191 #define __CI_CONTAINER(c_type, mbr_name, p_mbr)  \
192   ( (c_type*) ((char*)(p_mbr) - CI_MEMBER_OFFSET(c_type, mbr_name)) )
193
194 #ifndef CI_CONTAINER
195 # define CI_CONTAINER(t,m,p)  __CI_CONTAINER(t,m,p)
196 #endif
197
198
199 /**********************************************************************
200  * Structure member initialiser.
201  **********************************************************************/
202
203 #ifndef CI_STRUCT_MBR
204 # define CI_STRUCT_MBR(name, val)       .name = val
205 #endif
206
207
208 /**********************************************************************
209  * min / max
210  **********************************************************************/ 
211
212 #define CI_MIN(x,y) (((x) < (y)) ? (x) : (y))
213 #define CI_MAX(x,y) (((x) > (y)) ? (x) : (y))
214
215 /**********************************************************************
216  * abs
217  **********************************************************************/ 
218
219 #define CI_ABS(x) (((x) < 0) ? -(x) : (x))
220
221 /**********************************************************************
222  * Conditional debugging
223  **********************************************************************/ 
224
225 #ifdef NDEBUG
226 # define CI_DEBUG(x)
227 # define CI_NDEBUG(x)      x
228 # define CI_IF_DEBUG(y,n)  (n)
229 # define CI_DEBUG_ARG(x)
230 #else
231 # define CI_DEBUG(x)       x
232 # define CI_NDEBUG(x)
233 # define CI_IF_DEBUG(y,n)  (y)
234 # define CI_DEBUG_ARG(x)   ,x
235 #endif
236
237 #ifdef __KERNEL__
238 #define CI_KERNEL_ARG(x)   ,x
239 #else
240 #define CI_KERNEL_ARG(x)
241 #endif
242
243 #ifdef _WIN32
244 # define CI_KERNEL_ARG_WIN(x) CI_KERNEL_ARG(x)
245 # define CI_ARG_WIN(x) ,x
246 #else
247 # define CI_KERNEL_ARG_WIN(x)
248 # define CI_ARG_WIN(x) 
249 #endif
250
251 #ifdef __unix__
252 # define CI_KERNEL_ARG_UNIX(x) CI_KERNEL_ARG(x)
253 # define CI_ARG_UNIX(x) ,x
254 #else
255 # define CI_KERNEL_ARG_UNIX(x)
256 # define CI_ARG_UNIX(x) 
257 #endif
258
259 #ifdef __linux__
260 # define CI_KERNEL_ARG_LINUX(x) CI_KERNEL_ARG(x)
261 # define CI_ARG_LINUX(x) ,x
262 #else
263 # define CI_KERNEL_ARG_LINUX(x)
264 # define CI_ARG_LINUX(x) 
265 #endif
266
267
268 #endif  /* __CI_COMPAT_UTILS_H__ */
269 /*! \cidoxg_end */