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
32 #define BFA_TRC_MAX (4 * 1024)
35 #define BFA_TRC_TS(_trcm) \
39 do_gettimeofday(&tv); \
40 (tv.tv_sec*1000000+tv.tv_usec); \
44 #define BFA_TRC_TS(_trcm) ((_trcm)->ticks++)
65 struct bfa_trc_mod_s
{
72 struct bfa_trc_s trc
[BFA_TRC_MAX
];
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 */
81 #define BFA_TRC_MOD_SH 10
82 #define BFA_TRC_MOD(__mod) ((BFA_TRC_ ## __mod) << BFA_TRC_MOD_SH)
85 * Define a new tracing file (module). Module should match one defined above.
87 #define BFA_TRC_FILE(__mod, __submod) \
88 static int __trc_fileno = ((BFA_TRC_ ## __mod ## _ ## __submod) | \
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)
98 bfa_trc_init(struct bfa_trc_mod_s
*trcm
)
100 trcm
->head
= trcm
->tail
= trcm
->stopped
= 0;
101 trcm
->ntrc
= BFA_TRC_MAX
;
105 bfa_trc_stop(struct bfa_trc_mod_s
*trcm
)
111 __bfa_trc(struct bfa_trc_mod_s
*trcm
, int fileno
, int line
, u64 data
)
113 int tail
= trcm
->tail
;
114 struct bfa_trc_s
*trc
= &trcm
->trc
[tail
];
119 trc
->fileno
= (u16
) fileno
;
120 trc
->line
= (u16
) line
;
121 trc
->data
.u64
= data
;
122 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);
131 __bfa_trc32(struct bfa_trc_mod_s
*trcm
, int fileno
, int line
, u32 data
)
133 int tail
= trcm
->tail
;
134 struct bfa_trc_s
*trc
= &trcm
->trc
[tail
];
139 trc
->fileno
= (u16
) fileno
;
140 trc
->line
= (u16
) line
;
141 trc
->data
.u32
.u32
= data
;
142 trc
->timestamp
= BFA_TRC_TS(trcm
);
144 trcm
->tail
= (trcm
->tail
+ 1) & (BFA_TRC_MAX
- 1);
145 if (trcm
->tail
== trcm
->head
)
146 trcm
->head
= (trcm
->head
+ 1) & (BFA_TRC_MAX
- 1);
149 #define bfa_sm_fault(__mod, __event) do { \
150 bfa_trc(__mod, (((u32)0xDEAD << 16) | __event)); \
151 printk(KERN_ERR "Assertion failure: %s:%d: %d", \
152 __FILE__, __LINE__, (__event)); \
155 /* BFA queue definitions */
156 #define bfa_q_first(_q) ((void *)(((struct list_head *) (_q))->next))
157 #define bfa_q_next(_qe) (((struct list_head *) (_qe))->next)
158 #define bfa_q_prev(_qe) (((struct list_head *) (_qe))->prev)
161 * bfa_q_qe_init - to initialize a queue element
163 #define bfa_q_qe_init(_qe) { \
164 bfa_q_next(_qe) = (struct list_head *) NULL; \
165 bfa_q_prev(_qe) = (struct list_head *) NULL; \
169 * bfa_q_deq - dequeue an element from head of the queue
171 #define bfa_q_deq(_q, _qe) { \
172 if (!list_empty(_q)) { \
173 (*((struct list_head **) (_qe))) = bfa_q_next(_q); \
174 bfa_q_prev(bfa_q_next(*((struct list_head **) _qe))) = \
175 (struct list_head *) (_q); \
176 bfa_q_next(_q) = bfa_q_next(*((struct list_head **) _qe));\
178 *((struct list_head **) (_qe)) = (struct list_head *) NULL;\
183 * bfa_q_deq_tail - dequeue an element from tail of the queue
185 #define bfa_q_deq_tail(_q, _qe) { \
186 if (!list_empty(_q)) { \
187 *((struct list_head **) (_qe)) = bfa_q_prev(_q); \
188 bfa_q_next(bfa_q_prev(*((struct list_head **) _qe))) = \
189 (struct list_head *) (_q); \
190 bfa_q_prev(_q) = bfa_q_prev(*(struct list_head **) _qe);\
192 *((struct list_head **) (_qe)) = (struct list_head *) NULL;\
197 bfa_q_is_on_q_func(struct list_head
*q
, struct list_head
*qe
)
199 struct list_head
*tqe
;
205 tqe
= bfa_q_next(tqe
);
212 #define bfa_q_is_on_q(_q, _qe) \
213 bfa_q_is_on_q_func(_q, (struct list_head *)(_qe))
216 * @ BFA state machine interfaces
219 typedef void (*bfa_sm_t
)(void *sm
, int event
);
222 * oc - object class eg. bfa_ioc
223 * st - state, eg. reset
224 * otype - object type, eg. struct bfa_ioc_s
225 * etype - object type, eg. enum ioc_event
227 #define bfa_sm_state_decl(oc, st, otype, etype) \
228 static void oc ## _sm_ ## st(otype * fsm, etype event)
230 #define bfa_sm_set_state(_sm, _state) ((_sm)->sm = (bfa_sm_t)(_state))
231 #define bfa_sm_send_event(_sm, _event) ((_sm)->sm((_sm), (_event)))
232 #define bfa_sm_get_state(_sm) ((_sm)->sm)
233 #define bfa_sm_cmp_state(_sm, _state) ((_sm)->sm == (bfa_sm_t)(_state))
236 * For converting from state machine function to state encoding.
238 struct bfa_sm_table_s
{
239 bfa_sm_t sm
; /* state machine function */
240 int state
; /* state machine encoding */
241 char *name
; /* state name for display */
243 #define BFA_SM(_sm) ((bfa_sm_t)(_sm))
246 * State machine with entry actions.
248 typedef void (*bfa_fsm_t
)(void *fsm
, int event
);
251 * oc - object class eg. bfa_ioc
252 * st - state, eg. reset
253 * otype - object type, eg. struct bfa_ioc_s
254 * etype - object type, eg. enum ioc_event
256 #define bfa_fsm_state_decl(oc, st, otype, etype) \
257 static void oc ## _sm_ ## st(otype * fsm, etype event); \
258 static void oc ## _sm_ ## st ## _entry(otype * fsm)
260 #define bfa_fsm_set_state(_fsm, _state) do { \
261 (_fsm)->fsm = (bfa_fsm_t)(_state); \
262 _state ## _entry(_fsm); \
265 #define bfa_fsm_send_event(_fsm, _event) ((_fsm)->fsm((_fsm), (_event)))
266 #define bfa_fsm_get_state(_fsm) ((_fsm)->fsm)
267 #define bfa_fsm_cmp_state(_fsm, _state) \
268 ((_fsm)->fsm == (bfa_fsm_t)(_state))
271 bfa_sm_to_state(struct bfa_sm_table_s
*smt
, bfa_sm_t sm
)
275 while (smt
[i
].sm
&& smt
[i
].sm
!= sm
)
281 * @ Generic wait counter.
284 typedef void (*bfa_wc_resume_t
) (void *cbarg
);
287 bfa_wc_resume_t wc_resume
;
293 bfa_wc_up(struct bfa_wc_s
*wc
)
299 bfa_wc_down(struct bfa_wc_s
*wc
)
302 if (wc
->wc_count
== 0)
303 wc
->wc_resume(wc
->wc_cbarg
);
307 * Initialize a waiting counter.
310 bfa_wc_init(struct bfa_wc_s
*wc
, bfa_wc_resume_t wc_resume
, void *wc_cbarg
)
312 wc
->wc_resume
= wc_resume
;
313 wc
->wc_cbarg
= wc_cbarg
;
319 * Wait for counter to reach zero
322 bfa_wc_wait(struct bfa_wc_s
*wc
)
328 wwn2str(char *wwn_str
, u64 wwn
)
336 sprintf(wwn_str
, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x", w
.byte
[0],
337 w
.byte
[1], w
.byte
[2], w
.byte
[3], w
.byte
[4], w
.byte
[5],
338 w
.byte
[6], w
.byte
[7]);
342 fcid2str(char *fcid_str
, u32 fcid
)
350 sprintf(fcid_str
, "%02x:%02x:%02x", f
.byte
[1], f
.byte
[2], f
.byte
[3]);
353 #define bfa_swap_3b(_x) \
354 ((((_x) & 0xff) << 16) | \
355 ((_x) & 0x00ff00) | \
356 (((_x) & 0xff0000) >> 16))
359 #define bfa_hton3b(_x) bfa_swap_3b(_x)
361 #define bfa_hton3b(_x) (_x)
364 #define bfa_ntoh3b(_x) bfa_hton3b(_x)
366 #endif /* __BFA_CS_H__ */