[SCSI] bfa: remove all OS wrappers
[pandora-kernel.git] / drivers / scsi / bfa / bfa_cs.h
1 /*
2  * Copyright (c) 2005-2010 Brocade Communications Systems, Inc.
3  * All rights reserved
4  * www.brocade.com
5  *
6  * Linux driver for Brocade Fibre Channel Host Bus Adapter.
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License (GPL) Version 2 as
10  * published by the Free Software Foundation
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  */
17
18 /*
19  *  bfa_cs.h BFA common services
20  */
21
22 #ifndef __BFA_CS_H__
23 #define __BFA_CS_H__
24
25 #include "bfad_drv.h"
26
27 /*
28  * BFA TRC
29  */
30
31 #ifndef BFA_TRC_MAX
32 #define BFA_TRC_MAX     (4 * 1024)
33 #endif
34
35 #define BFA_TRC_TS(_trcm)                               \
36         ({                                              \
37                 struct timeval tv;                      \
38                                                         \
39                 do_gettimeofday(&tv);                   \
40                 (tv.tv_sec*1000000+tv.tv_usec);         \
41         })
42
43 #ifndef BFA_TRC_TS
44 #define BFA_TRC_TS(_trcm)       ((_trcm)->ticks++)
45 #endif
46
47 struct bfa_trc_s {
48 #ifdef __BIG_ENDIAN
49         u16     fileno;
50         u16     line;
51 #else
52         u16     line;
53         u16     fileno;
54 #endif
55         u32     timestamp;
56         union {
57                 struct {
58                         u32     rsvd;
59                         u32     u32;
60                 } u32;
61                 u64     u64;
62         } data;
63 };
64
65 struct bfa_trc_mod_s {
66         u32     head;
67         u32     tail;
68         u32     ntrc;
69         u32     stopped;
70         u32     ticks;
71         u32     rsvd[3];
72         struct bfa_trc_s trc[BFA_TRC_MAX];
73 };
74
75 enum {
76         BFA_TRC_HAL  = 1,       /*  BFA modules */
77         BFA_TRC_FCS  = 2,       /*  BFA FCS modules */
78         BFA_TRC_LDRV = 3,       /*  Linux driver modules */
79         BFA_TRC_CNA  = 4,       /*  Common modules */
80 };
81 #define BFA_TRC_MOD_SH  10
82 #define BFA_TRC_MOD(__mod)      ((BFA_TRC_ ## __mod) << BFA_TRC_MOD_SH)
83
84 /*
85  * Define a new tracing file (module). Module should match one defined above.
86  */
87 #define BFA_TRC_FILE(__mod, __submod)                                   \
88         static int __trc_fileno = ((BFA_TRC_ ## __mod ## _ ## __submod) | \
89                                                  BFA_TRC_MOD(__mod))
90
91
92 #define bfa_trc32(_trcp, _data) \
93         __bfa_trc((_trcp)->trcmod, __trc_fileno, __LINE__, (u32)_data)
94 #define bfa_trc(_trcp, _data)   \
95         __bfa_trc((_trcp)->trcmod, __trc_fileno, __LINE__, (u64)_data)
96
97 static inline void
98 bfa_trc_init(struct bfa_trc_mod_s *trcm)
99 {
100         trcm->head = trcm->tail = trcm->stopped = 0;
101         trcm->ntrc = BFA_TRC_MAX;
102 }
103
104 static inline void
105 bfa_trc_stop(struct bfa_trc_mod_s *trcm)
106 {
107         trcm->stopped = 1;
108 }
109
110 #ifdef FWTRC
111 extern void dc_flush(void *data);
112 #else
113 #define dc_flush(data)
114 #endif
115
116
117 static inline void
118 __bfa_trc(struct bfa_trc_mod_s *trcm, int fileno, int line, u64 data)
119 {
120         int             tail = trcm->tail;
121         struct bfa_trc_s        *trc = &trcm->trc[tail];
122
123         if (trcm->stopped)
124                 return;
125
126         trc->fileno = (u16) fileno;
127         trc->line = (u16) line;
128         trc->data.u64 = data;
129         trc->timestamp = BFA_TRC_TS(trcm);
130         dc_flush(trc);
131
132         trcm->tail = (trcm->tail + 1) & (BFA_TRC_MAX - 1);
133         if (trcm->tail == trcm->head)
134                 trcm->head = (trcm->head + 1) & (BFA_TRC_MAX - 1);
135         dc_flush(trcm);
136 }
137
138
139 static inline void
140 __bfa_trc32(struct bfa_trc_mod_s *trcm, int fileno, int line, u32 data)
141 {
142         int             tail = trcm->tail;
143         struct bfa_trc_s *trc = &trcm->trc[tail];
144
145         if (trcm->stopped)
146                 return;
147
148         trc->fileno = (u16) fileno;
149         trc->line = (u16) line;
150         trc->data.u32.u32 = data;
151         trc->timestamp = BFA_TRC_TS(trcm);
152         dc_flush(trc);
153
154         trcm->tail = (trcm->tail + 1) & (BFA_TRC_MAX - 1);
155         if (trcm->tail == trcm->head)
156                 trcm->head = (trcm->head + 1) & (BFA_TRC_MAX - 1);
157         dc_flush(trcm);
158 }
159
160 #ifndef BFA_PERF_BUILD
161 #define bfa_trc_fp(_trcp, _data)        bfa_trc(_trcp, _data)
162 #else
163 #define bfa_trc_fp(_trcp, _data)
164 #endif
165
166 /*
167  * @ BFA LOG interfaces
168  */
169 #define bfa_assert(__cond)      do {                                    \
170         if (!(__cond)) {                                                \
171                 printk(KERN_ERR "assert(%s) failed at %s:%d\\n",         \
172                 #__cond, __FILE__, __LINE__);                           \
173         }                                                               \
174 } while (0)
175
176 #define bfa_sm_fault(__mod, __event)    do {                            \
177         bfa_trc(__mod, (((u32)0xDEAD << 16) | __event));                \
178         printk(KERN_ERR "Assertion failure: %s:%d: %d",                 \
179                 __FILE__, __LINE__, (__event));                         \
180 } while (0)
181
182 #ifndef BFA_PERF_BUILD
183 #define bfa_assert_fp(__cond)   bfa_assert(__cond)
184 #else
185 #define bfa_assert_fp(__cond)
186 #endif
187
188 /* BFA queue definitions */
189 #define bfa_q_first(_q) ((void *)(((struct list_head *) (_q))->next))
190 #define bfa_q_next(_qe) (((struct list_head *) (_qe))->next)
191 #define bfa_q_prev(_qe) (((struct list_head *) (_qe))->prev)
192
193 /*
194  * bfa_q_qe_init - to initialize a queue element
195  */
196 #define bfa_q_qe_init(_qe) {                            \
197         bfa_q_next(_qe) = (struct list_head *) NULL;    \
198         bfa_q_prev(_qe) = (struct list_head *) NULL;    \
199 }
200
201 /*
202  * bfa_q_deq - dequeue an element from head of the queue
203  */
204 #define bfa_q_deq(_q, _qe) {                                            \
205         if (!list_empty(_q)) {                                          \
206                 (*((struct list_head **) (_qe))) = bfa_q_next(_q);      \
207                 bfa_q_prev(bfa_q_next(*((struct list_head **) _qe))) =  \
208                                 (struct list_head *) (_q);              \
209                 bfa_q_next(_q) = bfa_q_next(*((struct list_head **) _qe));\
210                 BFA_Q_DBG_INIT(*((struct list_head **) _qe));           \
211         } else {                                                        \
212                 *((struct list_head **) (_qe)) = (struct list_head *) NULL;\
213         }                                                               \
214 }
215
216 /*
217  * bfa_q_deq_tail - dequeue an element from tail of the queue
218  */
219 #define bfa_q_deq_tail(_q, _qe) {                                       \
220         if (!list_empty(_q)) {                                          \
221                 *((struct list_head **) (_qe)) = bfa_q_prev(_q);        \
222                 bfa_q_next(bfa_q_prev(*((struct list_head **) _qe))) =  \
223                         (struct list_head *) (_q);                      \
224                 bfa_q_prev(_q) = bfa_q_prev(*(struct list_head **) _qe);\
225                 BFA_Q_DBG_INIT(*((struct list_head **) _qe));           \
226         } else {                                                        \
227                 *((struct list_head **) (_qe)) = (struct list_head *) NULL;\
228         }                                                               \
229 }
230
231 static inline int
232 bfa_q_is_on_q_func(struct list_head *q, struct list_head *qe)
233 {
234         struct list_head        *tqe;
235
236         tqe = bfa_q_next(q);
237         while (tqe != q) {
238                 if (tqe == qe)
239                         return 1;
240                 tqe = bfa_q_next(tqe);
241                 if (tqe == NULL)
242                         break;
243         }
244         return 0;
245 }
246
247 /*
248  * #ifdef BFA_DEBUG (Using bfa_assert to check for debug_build is not
249  * consistent across modules)
250  */
251 #ifndef BFA_PERF_BUILD
252 #define BFA_Q_DBG_INIT(_qe) bfa_q_qe_init(_qe)
253 #else
254 #define BFA_Q_DBG_INIT(_qe)
255 #endif
256
257 #define bfa_q_is_on_q(_q, _qe)      \
258         bfa_q_is_on_q_func(_q, (struct list_head *)(_qe))
259
260 /*
261  * @ BFA state machine interfaces
262  */
263
264 typedef void (*bfa_sm_t)(void *sm, int event);
265
266 /*
267  * oc - object class eg. bfa_ioc
268  * st - state, eg. reset
269  * otype - object type, eg. struct bfa_ioc_s
270  * etype - object type, eg. enum ioc_event
271  */
272 #define bfa_sm_state_decl(oc, st, otype, etype)         \
273         static void oc ## _sm_ ## st(otype * fsm, etype event)
274
275 #define bfa_sm_set_state(_sm, _state)   ((_sm)->sm = (bfa_sm_t)(_state))
276 #define bfa_sm_send_event(_sm, _event)  ((_sm)->sm((_sm), (_event)))
277 #define bfa_sm_get_state(_sm)           ((_sm)->sm)
278 #define bfa_sm_cmp_state(_sm, _state)   ((_sm)->sm == (bfa_sm_t)(_state))
279
280 /*
281  * For converting from state machine function to state encoding.
282  */
283 struct bfa_sm_table_s {
284         bfa_sm_t        sm;     /*  state machine function      */
285         int             state;  /*  state machine encoding      */
286         char            *name;  /*  state name for display      */
287 };
288 #define BFA_SM(_sm)     ((bfa_sm_t)(_sm))
289
290 /*
291  * State machine with entry actions.
292  */
293 typedef void (*bfa_fsm_t)(void *fsm, int event);
294
295 /*
296  * oc - object class eg. bfa_ioc
297  * st - state, eg. reset
298  * otype - object type, eg. struct bfa_ioc_s
299  * etype - object type, eg. enum ioc_event
300  */
301 #define bfa_fsm_state_decl(oc, st, otype, etype)                \
302         static void oc ## _sm_ ## st(otype * fsm, etype event);      \
303         static void oc ## _sm_ ## st ## _entry(otype * fsm)
304
305 #define bfa_fsm_set_state(_fsm, _state) do {    \
306         (_fsm)->fsm = (bfa_fsm_t)(_state);      \
307         _state ## _entry(_fsm);      \
308 } while (0)
309
310 #define bfa_fsm_send_event(_fsm, _event)        ((_fsm)->fsm((_fsm), (_event)))
311 #define bfa_fsm_get_state(_fsm)                 ((_fsm)->fsm)
312 #define bfa_fsm_cmp_state(_fsm, _state)         \
313         ((_fsm)->fsm == (bfa_fsm_t)(_state))
314
315 static inline int
316 bfa_sm_to_state(struct bfa_sm_table_s *smt, bfa_sm_t sm)
317 {
318         int     i = 0;
319
320         while (smt[i].sm && smt[i].sm != sm)
321                 i++;
322         return smt[i].state;
323 }
324
325 /*
326  * @ Generic wait counter.
327  */
328
329 typedef void (*bfa_wc_resume_t) (void *cbarg);
330
331 struct bfa_wc_s {
332         bfa_wc_resume_t wc_resume;
333         void            *wc_cbarg;
334         int             wc_count;
335 };
336
337 static inline void
338 bfa_wc_up(struct bfa_wc_s *wc)
339 {
340         wc->wc_count++;
341 }
342
343 static inline void
344 bfa_wc_down(struct bfa_wc_s *wc)
345 {
346         wc->wc_count--;
347         if (wc->wc_count == 0)
348                 wc->wc_resume(wc->wc_cbarg);
349 }
350
351 /*
352  * Initialize a waiting counter.
353  */
354 static inline void
355 bfa_wc_init(struct bfa_wc_s *wc, bfa_wc_resume_t wc_resume, void *wc_cbarg)
356 {
357         wc->wc_resume = wc_resume;
358         wc->wc_cbarg = wc_cbarg;
359         wc->wc_count = 0;
360         bfa_wc_up(wc);
361 }
362
363 /*
364  * Wait for counter to reach zero
365  */
366 static inline void
367 bfa_wc_wait(struct bfa_wc_s *wc)
368 {
369         bfa_wc_down(wc);
370 }
371
372 static inline void
373 wwn2str(char *wwn_str, u64 wwn)
374 {
375         union {
376                 u64 wwn;
377                 u8 byte[8];
378         } w;
379
380         w.wwn = wwn;
381         sprintf(wwn_str, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x", w.byte[0],
382                 w.byte[1], w.byte[2], w.byte[3], w.byte[4], w.byte[5],
383                 w.byte[6], w.byte[7]);
384 }
385
386 static inline void
387 fcid2str(char *fcid_str, u32 fcid)
388 {
389         union {
390                 u32 fcid;
391                 u8 byte[4];
392         } f;
393
394         f.fcid = fcid;
395         sprintf(fcid_str, "%02x:%02x:%02x", f.byte[1], f.byte[2], f.byte[3]);
396 }
397
398 #define bfa_swap_3b(_x)                         \
399         ((((_x) & 0xff) << 16) |                \
400         ((_x) & 0x00ff00) |                     \
401         (((_x) & 0xff0000) >> 16))
402
403 #ifndef __BIG_ENDIAN
404 #define bfa_hton3b(_x)  bfa_swap_3b(_x)
405 #else
406 #define bfa_hton3b(_x)  (_x)
407 #endif
408
409 #define bfa_ntoh3b(_x)  bfa_hton3b(_x)
410
411 #endif /* __BFA_CS_H__ */