2 * Copyright (c) 2005-2014 Brocade Communications Systems, Inc.
3 * Copyright (c) 2014- QLogic Corporation.
7 * Linux driver for QLogic BR-series Fibre Channel Host Bus Adapter.
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License (GPL) Version 2 as
11 * published by the Free Software Foundation
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
20 * bfa_cs.h BFA common services
33 #define BFA_TRC_MAX (4 * 1024)
36 #define BFA_TRC_TS(_trcm) \
38 struct timespec64 ts; \
40 ktime_get_ts64(&ts); \
41 (ts.tv_sec*1000000+ts.tv_nsec / 1000); \
45 #define BFA_TRC_TS(_trcm) ((_trcm)->ticks++)
66 struct bfa_trc_mod_s
{
73 struct bfa_trc_s trc
[BFA_TRC_MAX
];
77 BFA_TRC_HAL
= 1, /* BFA modules */
78 BFA_TRC_FCS
= 2, /* BFA FCS modules */
79 BFA_TRC_LDRV
= 3, /* Linux driver modules */
80 BFA_TRC_CNA
= 4, /* Common modules */
82 #define BFA_TRC_MOD_SH 10
83 #define BFA_TRC_MOD(__mod) ((BFA_TRC_ ## __mod) << BFA_TRC_MOD_SH)
86 * Define a new tracing file (module). Module should match one defined above.
88 #define BFA_TRC_FILE(__mod, __submod) \
89 static int __trc_fileno = ((BFA_TRC_ ## __mod ## _ ## __submod) | \
93 #define bfa_trc32(_trcp, _data) \
94 __bfa_trc((_trcp)->trcmod, __trc_fileno, __LINE__, (u32)_data)
95 #define bfa_trc(_trcp, _data) \
96 __bfa_trc((_trcp)->trcmod, __trc_fileno, __LINE__, (u64)_data)
99 bfa_trc_init(struct bfa_trc_mod_s
*trcm
)
101 trcm
->head
= trcm
->tail
= trcm
->stopped
= 0;
102 trcm
->ntrc
= BFA_TRC_MAX
;
106 bfa_trc_stop(struct bfa_trc_mod_s
*trcm
)
112 __bfa_trc(struct bfa_trc_mod_s
*trcm
, int fileno
, int line
, u64 data
);
115 __bfa_trc32(struct bfa_trc_mod_s
*trcm
, int fileno
, int line
, u32 data
);
117 #define bfa_sm_fault(__mod, __event) do { \
118 bfa_trc(__mod, (((u32)0xDEAD << 16) | __event)); \
119 printk(KERN_ERR "Assertion failure: %s:%d: %d", \
120 __FILE__, __LINE__, (__event)); \
123 /* BFA queue definitions */
124 #define bfa_q_first(_q) ((void *)(((struct list_head *) (_q))->next))
125 #define bfa_q_next(_qe) (((struct list_head *) (_qe))->next)
126 #define bfa_q_prev(_qe) (((struct list_head *) (_qe))->prev)
129 * bfa_q_qe_init - to initialize a queue element
131 #define bfa_q_qe_init(_qe) { \
132 bfa_q_next(_qe) = (struct list_head *) NULL; \
133 bfa_q_prev(_qe) = (struct list_head *) NULL; \
137 * bfa_q_deq - dequeue an element from head of the queue
139 #define bfa_q_deq(_q, _qe) do { \
140 if (!list_empty(_q)) { \
141 (*((struct list_head **) (_qe))) = bfa_q_next(_q); \
142 bfa_q_prev(bfa_q_next(*((struct list_head **) _qe))) = \
143 (struct list_head *) (_q); \
144 bfa_q_next(_q) = bfa_q_next(*((struct list_head **) _qe));\
146 *((struct list_head **) (_qe)) = (struct list_head *) NULL;\
151 * bfa_q_deq_tail - dequeue an element from tail of the queue
153 #define bfa_q_deq_tail(_q, _qe) { \
154 if (!list_empty(_q)) { \
155 *((struct list_head **) (_qe)) = bfa_q_prev(_q); \
156 bfa_q_next(bfa_q_prev(*((struct list_head **) _qe))) = \
157 (struct list_head *) (_q); \
158 bfa_q_prev(_q) = bfa_q_prev(*(struct list_head **) _qe);\
160 *((struct list_head **) (_qe)) = (struct list_head *) NULL;\
165 bfa_q_is_on_q_func(struct list_head
*q
, struct list_head
*qe
)
167 struct list_head
*tqe
;
173 tqe
= bfa_q_next(tqe
);
180 #define bfa_q_is_on_q(_q, _qe) \
181 bfa_q_is_on_q_func(_q, (struct list_head *)(_qe))
184 * @ BFA state machine interfaces
187 typedef void (*bfa_sm_t
)(void *sm
, int event
);
190 * oc - object class eg. bfa_ioc
191 * st - state, eg. reset
192 * otype - object type, eg. struct bfa_ioc_s
193 * etype - object type, eg. enum ioc_event
195 #define bfa_sm_state_decl(oc, st, otype, etype) \
196 static void oc ## _sm_ ## st(otype * fsm, etype event)
198 #define bfa_sm_set_state(_sm, _state) ((_sm)->sm = (bfa_sm_t)(_state))
199 #define bfa_sm_send_event(_sm, _event) ((_sm)->sm((_sm), (_event)))
200 #define bfa_sm_get_state(_sm) ((_sm)->sm)
201 #define bfa_sm_cmp_state(_sm, _state) ((_sm)->sm == (bfa_sm_t)(_state))
204 * For converting from state machine function to state encoding.
206 struct bfa_sm_table_s
{
207 bfa_sm_t sm
; /* state machine function */
208 int state
; /* state machine encoding */
209 char *name
; /* state name for display */
211 #define BFA_SM(_sm) ((bfa_sm_t)(_sm))
214 * State machine with entry actions.
216 typedef void (*bfa_fsm_t
)(void *fsm
, int event
);
219 * oc - object class eg. bfa_ioc
220 * st - state, eg. reset
221 * otype - object type, eg. struct bfa_ioc_s
222 * etype - object type, eg. enum ioc_event
224 #define bfa_fsm_state_decl(oc, st, otype, etype) \
225 static void oc ## _sm_ ## st(otype * fsm, etype event); \
226 static void oc ## _sm_ ## st ## _entry(otype * fsm)
228 #define bfa_fsm_set_state(_fsm, _state) do { \
229 (_fsm)->fsm = (bfa_fsm_t)(_state); \
230 _state ## _entry(_fsm); \
233 #define bfa_fsm_send_event(_fsm, _event) ((_fsm)->fsm((_fsm), (_event)))
234 #define bfa_fsm_get_state(_fsm) ((_fsm)->fsm)
235 #define bfa_fsm_cmp_state(_fsm, _state) \
236 ((_fsm)->fsm == (bfa_fsm_t)(_state))
239 bfa_sm_to_state(struct bfa_sm_table_s
*smt
, bfa_sm_t sm
)
243 while (smt
[i
].sm
&& smt
[i
].sm
!= sm
)
249 * @ Generic wait counter.
252 typedef void (*bfa_wc_resume_t
) (void *cbarg
);
255 bfa_wc_resume_t wc_resume
;
261 bfa_wc_up(struct bfa_wc_s
*wc
)
267 bfa_wc_down(struct bfa_wc_s
*wc
)
270 if (wc
->wc_count
== 0)
271 wc
->wc_resume(wc
->wc_cbarg
);
275 * Initialize a waiting counter.
278 bfa_wc_init(struct bfa_wc_s
*wc
, bfa_wc_resume_t wc_resume
, void *wc_cbarg
)
280 wc
->wc_resume
= wc_resume
;
281 wc
->wc_cbarg
= wc_cbarg
;
287 * Wait for counter to reach zero
290 bfa_wc_wait(struct bfa_wc_s
*wc
)
296 wwn2str(char *wwn_str
, u64 wwn
)
304 sprintf(wwn_str
, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x", w
.byte
[0],
305 w
.byte
[1], w
.byte
[2], w
.byte
[3], w
.byte
[4], w
.byte
[5],
306 w
.byte
[6], w
.byte
[7]);
310 fcid2str(char *fcid_str
, u32 fcid
)
318 sprintf(fcid_str
, "%02x:%02x:%02x", f
.byte
[1], f
.byte
[2], f
.byte
[3]);
321 #define bfa_swap_3b(_x) \
322 ((((_x) & 0xff) << 16) | \
323 ((_x) & 0x00ff00) | \
324 (((_x) & 0xff0000) >> 16))
327 #define bfa_hton3b(_x) bfa_swap_3b(_x)
329 #define bfa_hton3b(_x) (_x)
332 #define bfa_ntoh3b(_x) bfa_hton3b(_x)
334 #endif /* __BFA_CS_H__ */