Update to 3.4-final.
[linux-flexiantxendom0-3.2.10.git] / drivers / xen / sfc_netfront / ef_vi_internal.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  Really-and-truely-honestly internal stuff for libef.
29  *   \date  2004/06/13
30  */
31
32 /*! \cidoxg_include_ci_ul */
33 #ifndef __CI_EF_VI_INTERNAL_H__
34 #define __CI_EF_VI_INTERNAL_H__
35
36
37 /* These flags share space with enum ef_vi_flags. */
38 #define EF_VI_BUG5692_WORKAROUND  0x10000
39
40
41 /* ***********************************************************************
42  * COMPILATION CONTROL FLAGS (see ef_vi.h for "workaround" controls)
43  */
44
45 #define EF_VI_DO_MAGIC_CHECKS 1
46
47
48 /**********************************************************************
49  * Headers
50  */
51
52 #include <etherfabric/ef_vi.h>
53 #include "sysdep.h"
54 #include "ef_vi_falcon.h"
55
56
57 /**********************************************************************
58  * Debugging.
59  */
60
61 #ifndef NDEBUG
62
63 # define _ef_assert(exp, file, line) BUG_ON(!(exp));
64
65 # define _ef_assert2(exp, x, y, file, line)  do {       \
66                 if (unlikely(!(exp)))           \
67                         BUG();                          \
68         } while (0)
69
70 #else
71
72 # define _ef_assert(exp, file, line)
73 # define _ef_assert2(e, x, y, file, line)
74
75 #endif
76
77 #define ef_assert(a)          do{ _ef_assert((a),__FILE__,__LINE__); } while(0)
78 #define ef_assert_equal(a,b)  _ef_assert2((a)==(b),(a),(b),__FILE__,__LINE__)
79 #define ef_assert_eq          ef_assert_equal
80 #define ef_assert_lt(a,b)     _ef_assert2((a)<(b),(a),(b),__FILE__,__LINE__)
81 #define ef_assert_le(a,b)     _ef_assert2((a)<=(b),(a),(b),__FILE__,__LINE__)
82 #define ef_assert_nequal(a,b) _ef_assert2((a)!=(b),(a),(b),__FILE__,__LINE__)
83 #define ef_assert_ne          ef_assert_nequal
84 #define ef_assert_ge(a,b)     _ef_assert2((a)>=(b),(a),(b),__FILE__,__LINE__)
85 #define ef_assert_gt(a,b)     _ef_assert2((a)>(b),(a),(b),__FILE__,__LINE__)
86
87 /**********************************************************************
88  * Debug checks. ******************************************************
89  **********************************************************************/
90
91 #ifdef NDEBUG
92 # define EF_VI_MAGIC_SET(p, type)
93 # define EF_VI_CHECK_VI(p)
94 # define EF_VI_CHECK_EVENT_Q(p)
95 # define EF_VI_CHECK_IOBUFSET(p)
96 # define EF_VI_CHECK_FILTER(p)
97 # define EF_VI_CHECK_SHMBUF(p)
98 # define EF_VI_CHECK_PT_EP(p)
99 #else
100 # define EF_VI                    0x3
101 # define EF_EPLOCK                0x6
102 # define EF_IOBUFSET              0x9
103 # define EF_FILTER                0xa
104 # define EF_SHMBUF                0x11
105
106 # define EF_VI_MAGIC(p, type)                           \
107         (((unsigned)(type) << 28) |                     \
108          (((unsigned)(intptr_t)(p)) & 0x0fffffffu))
109
110 # if !EF_VI_DO_MAGIC_CHECKS
111 #  define EF_VI_MAGIC_SET(p, type)
112 #  define EF_VI_MAGIC_CHECK(p, type)
113 # else
114 #  define EF_VI_MAGIC_SET(p, type)                      \
115         do {                                            \
116                 (p)->magic = EF_VI_MAGIC((p), (type));  \
117         } while (0)
118
119 # define EF_VI_MAGIC_OKAY(p, type)                      \
120         ((p)->magic == EF_VI_MAGIC((p), (type)))
121
122 # define EF_VI_MAGIC_CHECK(p, type)                     \
123         ef_assert(EF_VI_MAGIC_OKAY((p), (type)))
124
125 #endif /* EF_VI_DO_MAGIC_CHECKS */
126
127 # define EF_VI_CHECK_VI(p)                      \
128         ef_assert(p);                           \
129         EF_VI_MAGIC_CHECK((p), EF_VI);
130
131 # define EF_VI_CHECK_EVENT_Q(p)                 \
132         ef_assert(p);                           \
133         EF_VI_MAGIC_CHECK((p), EF_VI);          \
134         ef_assert((p)->evq_base);               \
135         ef_assert((p)->evq_mask);
136
137 # define EF_VI_CHECK_PT_EP(p)                   \
138         ef_assert(p);                           \
139         EF_VI_MAGIC_CHECK((p), EF_VI);          \
140         ef_assert((p)->ep_state);
141
142 # define EF_VI_CHECK_IOBUFSET(p)                \
143         ef_assert(p);                           \
144         EF_VI_MAGIC_CHECK((p), EF_IOBUFSET)
145
146 # define EF_VI_CHECK_FILTER(p)                  \
147         ef_assert(p);                           \
148         EF_VI_MAGIC_CHECK((p), EF_FILTER);
149
150 # define EF_VI_CHECK_SHMBUF(p)                  \
151         ef_assert(p);                           \
152         EF_VI_MAGIC_CHECK((p), EF_SHMBUF);
153
154 #endif
155
156 #ifndef NDEBUG
157 # define EF_DRIVER_MAGIC 0x00f00ba4
158 # define EF_ASSERT_THIS_DRIVER_VALID(driver)                            \
159         do{ ef_assert(driver);                                          \
160                 EF_VI_MAGIC_CHECK((driver), EF_DRIVER_MAGIC);           \
161                 ef_assert((driver)->init);               }while(0)
162
163 # define EF_ASSERT_DRIVER_VALID() EF_ASSERT_THIS_DRIVER_VALID(&ci_driver)
164 #else
165 # define EF_ASSERT_THIS_DRIVER_VALID(driver)
166 # define EF_ASSERT_DRIVER_VALID()
167 #endif
168
169
170 /* *************************************
171  * Power of 2 FIFO
172  */
173
174 #define EF_VI_FIFO2_M(f, x)  ((x) & ((f)->fifo_mask))
175 #define ef_vi_fifo2_valid(f) ((f) && (f)->fifo && (f)->fifo_mask > 0 && \
176                               (f)->fifo_rd_i <= (f)->fifo_mask       && \
177                               (f)->fifo_wr_i <= (f)->fifo_mask       && \
178                               EF_VI_IS_POW2((f)->fifo_mask+1u))
179
180 #define ef_vi_fifo2_init(f, cap)                        \
181         do{ ef_assert(EF_VI_IS_POW2((cap) + 1));        \
182                 (f)->fifo_rd_i = (f)->fifo_wr_i = 0u;   \
183                 (f)->fifo_mask = (cap);                 \
184         }while(0)
185
186 #define ef_vi_fifo2_is_empty(f) ((f)->fifo_rd_i == (f)->fifo_wr_i)
187 #define ef_vi_fifo2_capacity(f) ((f)->fifo_mask)
188 #define ef_vi_fifo2_buf_size(f) ((f)->fifo_mask + 1u)
189 #define ef_vi_fifo2_end(f)      ((f)->fifo + ef_vi_fifo2_buf_size(f))
190 #define ef_vi_fifo2_peek(f)     ((f)->fifo[(f)->fifo_rd_i])
191 #define ef_vi_fifo2_poke(f)     ((f)->fifo[(f)->fifo_wr_i])
192 #define ef_vi_fifo2_num(f)   EF_VI_FIFO2_M((f),(f)->fifo_wr_i-(f)->fifo_rd_i)
193
194 #define ef_vi_fifo2_wr_prev(f)                                          \
195         do{ (f)->fifo_wr_i = EF_VI_FIFO2_M((f), (f)->fifo_wr_i - 1u); }while(0)
196 #define ef_vi_fifo2_wr_next(f)                                          \
197         do{ (f)->fifo_wr_i = EF_VI_FIFO2_M((f), (f)->fifo_wr_i + 1u); }while(0)
198 #define ef_vi_fifo2_rd_adv(f, n)                                        \
199         do{ (f)->fifo_rd_i = EF_VI_FIFO2_M((f), (f)->fifo_rd_i + (n)); }while(0)
200 #define ef_vi_fifo2_rd_prev(f)                                          \
201         do{ (f)->fifo_rd_i = EF_VI_FIFO2_M((f), (f)->fifo_rd_i - 1u); }while(0)
202 #define ef_vi_fifo2_rd_next(f)                                          \
203         do{ (f)->fifo_rd_i = EF_VI_FIFO2_M((f), (f)->fifo_rd_i + 1u); }while(0)
204
205 #define ef_vi_fifo2_put(f, v)                                           \
206         do{ ef_vi_fifo2_poke(f) = (v); ef_vi_fifo2_wr_next(f); }while(0)
207 #define ef_vi_fifo2_get(f, pv)                                          \
208         do{ *(pv) = ef_vi_fifo2_peek(f); ef_vi_fifo2_rd_next(f); }while(0)
209
210
211 /* *********************************************************************
212  * Eventq handling
213  */
214
215 typedef union {
216         uint64_t    u64;
217         struct {
218                 uint32_t  a;
219                 uint32_t  b;
220         } opaque;
221 } ef_vi_event;
222
223
224 #define EF_VI_EVENT_OFFSET(q, i)                                        \
225         (((q)->evq_state->evq_ptr - (i) * sizeof(ef_vi_event)) & (q)->evq_mask)
226
227 #define EF_VI_EVENT_PTR(q, i)                                           \
228         ((ef_vi_event*) ((q)->evq_base + EF_VI_EVENT_OFFSET((q), (i))))
229
230 /* *********************************************************************
231  * Miscellaneous goodies
232  */
233 #ifdef NDEBUG
234 # define EF_VI_DEBUG(x)
235 #else
236 # define EF_VI_DEBUG(x)            x
237 #endif
238
239 #define EF_VI_ROUND_UP(i, align)   (((i)+(align)-1u) & ~((align)-1u))
240 #define EF_VI_ALIGN_FWD(p, align)  (((p)+(align)-1u) & ~((align)-1u))
241 #define EF_VI_ALIGN_BACK(p, align) ((p) & ~((align)-1u))
242 #define EF_VI_PTR_ALIGN_BACK(p, align)                                  \
243         ((char*)EF_VI_ALIGN_BACK(((intptr_t)(p)), ((intptr_t)(align))))
244 #define EF_VI_IS_POW2(x)           ((x) && ! ((x) & ((x) - 1)))
245
246
247 /* ******************************************************************** 
248  */
249
250 extern void falcon_vi_init(ef_vi*, void* vvis ) EF_VI_HF;
251 extern void ef_eventq_state_init(ef_vi* evq) EF_VI_HF;
252 extern void __ef_init(void) EF_VI_HF;
253
254
255 #endif  /* __CI_EF_VI_INTERNAL_H__ */
256