2 * Copyright (c) 2005-2010 Brocade Communications Systems, Inc.
6 * Linux driver for Brocade Fibre Channel Host Bus Adapter.
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
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.
19 * bfa_cs.h BFA common services
25 #include "bfa_os_inc.h"
32 #define BFA_TRC_MAX (4 * 1024)
36 #define BFA_TRC_TS(_trcm) ((_trcm)->ticks++)
57 struct bfa_trc_mod_s {
64 struct bfa_trc_s trc[BFA_TRC_MAX];
68 BFA_TRC_HAL = 1, /* BFA modules */
69 BFA_TRC_FCS = 2, /* BFA FCS modules */
70 BFA_TRC_LDRV = 3, /* Linux driver modules */
71 BFA_TRC_CNA = 4, /* Common modules */
73 #define BFA_TRC_MOD_SH 10
74 #define BFA_TRC_MOD(__mod) ((BFA_TRC_ ## __mod) << BFA_TRC_MOD_SH)
77 * Define a new tracing file (module). Module should match one defined above.
79 #define BFA_TRC_FILE(__mod, __submod) \
80 static int __trc_fileno = ((BFA_TRC_ ## __mod ## _ ## __submod) | \
84 #define bfa_trc32(_trcp, _data) \
85 __bfa_trc((_trcp)->trcmod, __trc_fileno, __LINE__, (u32)_data)
86 #define bfa_trc(_trcp, _data) \
87 __bfa_trc((_trcp)->trcmod, __trc_fileno, __LINE__, (u64)_data)
90 bfa_trc_init(struct bfa_trc_mod_s *trcm)
92 trcm->head = trcm->tail = trcm->stopped = 0;
93 trcm->ntrc = BFA_TRC_MAX;
97 bfa_trc_stop(struct bfa_trc_mod_s *trcm)
103 extern void dc_flush(void *data);
105 #define dc_flush(data)
110 __bfa_trc(struct bfa_trc_mod_s *trcm, int fileno, int line, u64 data)
112 int tail = trcm->tail;
113 struct bfa_trc_s *trc = &trcm->trc[tail];
118 trc->fileno = (u16) fileno;
119 trc->line = (u16) line;
120 trc->data.u64 = data;
121 trc->timestamp = BFA_TRC_TS(trcm);
124 trcm->tail = (trcm->tail + 1) & (BFA_TRC_MAX - 1);
125 if (trcm->tail == trcm->head)
126 trcm->head = (trcm->head + 1) & (BFA_TRC_MAX - 1);
132 __bfa_trc32(struct bfa_trc_mod_s *trcm, int fileno, int line, u32 data)
134 int tail = trcm->tail;
135 struct bfa_trc_s *trc = &trcm->trc[tail];
140 trc->fileno = (u16) fileno;
141 trc->line = (u16) line;
142 trc->data.u32.u32 = data;
143 trc->timestamp = BFA_TRC_TS(trcm);
146 trcm->tail = (trcm->tail + 1) & (BFA_TRC_MAX - 1);
147 if (trcm->tail == trcm->head)
148 trcm->head = (trcm->head + 1) & (BFA_TRC_MAX - 1);
152 #ifndef BFA_PERF_BUILD
153 #define bfa_trc_fp(_trcp, _data) bfa_trc(_trcp, _data)
155 #define bfa_trc_fp(_trcp, _data)
159 * @ BFA LOG interfaces
161 #define bfa_assert(__cond) do { \
163 printk(KERN_ERR "assert(%s) failed at %s:%d\\n", \
164 #__cond, __FILE__, __LINE__); \
168 #define bfa_sm_fault(__mod, __event) do { \
169 bfa_trc(__mod, (((u32)0xDEAD << 16) | __event)); \
170 printk(KERN_ERR "Assertion failure: %s:%d: %d", \
171 __FILE__, __LINE__, (__event)); \
174 #ifndef BFA_PERF_BUILD
175 #define bfa_assert_fp(__cond) bfa_assert(__cond)
177 #define bfa_assert_fp(__cond)
180 /* BFA queue definitions */
181 #define bfa_q_first(_q) ((void *)(((struct list_head *) (_q))->next))
182 #define bfa_q_next(_qe) (((struct list_head *) (_qe))->next)
183 #define bfa_q_prev(_qe) (((struct list_head *) (_qe))->prev)
186 * bfa_q_qe_init - to initialize a queue element
188 #define bfa_q_qe_init(_qe) { \
189 bfa_q_next(_qe) = (struct list_head *) NULL; \
190 bfa_q_prev(_qe) = (struct list_head *) NULL; \
194 * bfa_q_deq - dequeue an element from head of the queue
196 #define bfa_q_deq(_q, _qe) { \
197 if (!list_empty(_q)) { \
198 (*((struct list_head **) (_qe))) = bfa_q_next(_q); \
199 bfa_q_prev(bfa_q_next(*((struct list_head **) _qe))) = \
200 (struct list_head *) (_q); \
201 bfa_q_next(_q) = bfa_q_next(*((struct list_head **) _qe));\
202 BFA_Q_DBG_INIT(*((struct list_head **) _qe)); \
204 *((struct list_head **) (_qe)) = (struct list_head *) NULL;\
209 * bfa_q_deq_tail - dequeue an element from tail of the queue
211 #define bfa_q_deq_tail(_q, _qe) { \
212 if (!list_empty(_q)) { \
213 *((struct list_head **) (_qe)) = bfa_q_prev(_q); \
214 bfa_q_next(bfa_q_prev(*((struct list_head **) _qe))) = \
215 (struct list_head *) (_q); \
216 bfa_q_prev(_q) = bfa_q_prev(*(struct list_head **) _qe);\
217 BFA_Q_DBG_INIT(*((struct list_head **) _qe)); \
219 *((struct list_head **) (_qe)) = (struct list_head *) NULL;\
224 bfa_q_is_on_q_func(struct list_head *q, struct list_head *qe)
226 struct list_head *tqe;
232 tqe = bfa_q_next(tqe);
240 * #ifdef BFA_DEBUG (Using bfa_assert to check for debug_build is not
241 * consistent across modules)
243 #ifndef BFA_PERF_BUILD
244 #define BFA_Q_DBG_INIT(_qe) bfa_q_qe_init(_qe)
246 #define BFA_Q_DBG_INIT(_qe)
249 #define bfa_q_is_on_q(_q, _qe) \
250 bfa_q_is_on_q_func(_q, (struct list_head *)(_qe))
253 * @ BFA state machine interfaces
256 typedef void (*bfa_sm_t)(void *sm, int event);
259 * oc - object class eg. bfa_ioc
260 * st - state, eg. reset
261 * otype - object type, eg. struct bfa_ioc_s
262 * etype - object type, eg. enum ioc_event
264 #define bfa_sm_state_decl(oc, st, otype, etype) \
265 static void oc ## _sm_ ## st(otype * fsm, etype event)
267 #define bfa_sm_set_state(_sm, _state) ((_sm)->sm = (bfa_sm_t)(_state))
268 #define bfa_sm_send_event(_sm, _event) ((_sm)->sm((_sm), (_event)))
269 #define bfa_sm_get_state(_sm) ((_sm)->sm)
270 #define bfa_sm_cmp_state(_sm, _state) ((_sm)->sm == (bfa_sm_t)(_state))
273 * For converting from state machine function to state encoding.
275 struct bfa_sm_table_s {
276 bfa_sm_t sm; /* state machine function */
277 int state; /* state machine encoding */
278 char *name; /* state name for display */
280 #define BFA_SM(_sm) ((bfa_sm_t)(_sm))
283 * State machine with entry actions.
285 typedef void (*bfa_fsm_t)(void *fsm, int event);
288 * oc - object class eg. bfa_ioc
289 * st - state, eg. reset
290 * otype - object type, eg. struct bfa_ioc_s
291 * etype - object type, eg. enum ioc_event
293 #define bfa_fsm_state_decl(oc, st, otype, etype) \
294 static void oc ## _sm_ ## st(otype * fsm, etype event); \
295 static void oc ## _sm_ ## st ## _entry(otype * fsm)
297 #define bfa_fsm_set_state(_fsm, _state) do { \
298 (_fsm)->fsm = (bfa_fsm_t)(_state); \
299 _state ## _entry(_fsm); \
302 #define bfa_fsm_send_event(_fsm, _event) ((_fsm)->fsm((_fsm), (_event)))
303 #define bfa_fsm_get_state(_fsm) ((_fsm)->fsm)
304 #define bfa_fsm_cmp_state(_fsm, _state) \
305 ((_fsm)->fsm == (bfa_fsm_t)(_state))
308 bfa_sm_to_state(struct bfa_sm_table_s *smt, bfa_sm_t sm)
312 while (smt[i].sm && smt[i].sm != sm)
318 * @ Generic wait counter.
321 typedef void (*bfa_wc_resume_t) (void *cbarg);
324 bfa_wc_resume_t wc_resume;
330 bfa_wc_up(struct bfa_wc_s *wc)
336 bfa_wc_down(struct bfa_wc_s *wc)
339 if (wc->wc_count == 0)
340 wc->wc_resume(wc->wc_cbarg);
344 * Initialize a waiting counter.
347 bfa_wc_init(struct bfa_wc_s *wc, bfa_wc_resume_t wc_resume, void *wc_cbarg)
349 wc->wc_resume = wc_resume;
350 wc->wc_cbarg = wc_cbarg;
356 * Wait for counter to reach zero
359 bfa_wc_wait(struct bfa_wc_s *wc)
364 #endif /* __BFA_CS_H__ */