bfa_cs.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. * bfa_cs.h BFA common services
  19. */
  20. #ifndef __BFA_CS_H__
  21. #define __BFA_CS_H__
  22. #include "bfa_os_inc.h"
  23. /*
  24. * BFA TRC
  25. */
  26. #ifndef BFA_TRC_MAX
  27. #define BFA_TRC_MAX (4 * 1024)
  28. #endif
  29. #ifndef BFA_TRC_TS
  30. #define BFA_TRC_TS(_trcm) ((_trcm)->ticks++)
  31. #endif
  32. struct bfa_trc_s {
  33. #ifdef __BIGENDIAN
  34. u16 fileno;
  35. u16 line;
  36. #else
  37. u16 line;
  38. u16 fileno;
  39. #endif
  40. u32 timestamp;
  41. union {
  42. struct {
  43. u32 rsvd;
  44. u32 u32;
  45. } u32;
  46. u64 u64;
  47. } data;
  48. };
  49. struct bfa_trc_mod_s {
  50. u32 head;
  51. u32 tail;
  52. u32 ntrc;
  53. u32 stopped;
  54. u32 ticks;
  55. u32 rsvd[3];
  56. struct bfa_trc_s trc[BFA_TRC_MAX];
  57. };
  58. enum {
  59. BFA_TRC_HAL = 1, /* BFA modules */
  60. BFA_TRC_FCS = 2, /* BFA FCS modules */
  61. BFA_TRC_LDRV = 3, /* Linux driver modules */
  62. BFA_TRC_CNA = 4, /* Common modules */
  63. };
  64. #define BFA_TRC_MOD_SH 10
  65. #define BFA_TRC_MOD(__mod) ((BFA_TRC_ ## __mod) << BFA_TRC_MOD_SH)
  66. /*
  67. * Define a new tracing file (module). Module should match one defined above.
  68. */
  69. #define BFA_TRC_FILE(__mod, __submod) \
  70. static int __trc_fileno = ((BFA_TRC_ ## __mod ## _ ## __submod) | \
  71. BFA_TRC_MOD(__mod))
  72. #define bfa_trc32(_trcp, _data) \
  73. __bfa_trc((_trcp)->trcmod, __trc_fileno, __LINE__, (u32)_data)
  74. #define bfa_trc(_trcp, _data) \
  75. __bfa_trc((_trcp)->trcmod, __trc_fileno, __LINE__, (u64)_data)
  76. static inline void
  77. bfa_trc_init(struct bfa_trc_mod_s *trcm)
  78. {
  79. trcm->head = trcm->tail = trcm->stopped = 0;
  80. trcm->ntrc = BFA_TRC_MAX;
  81. }
  82. static inline void
  83. bfa_trc_stop(struct bfa_trc_mod_s *trcm)
  84. {
  85. trcm->stopped = 1;
  86. }
  87. #ifdef FWTRC
  88. extern void dc_flush(void *data);
  89. #else
  90. #define dc_flush(data)
  91. #endif
  92. static inline void
  93. __bfa_trc(struct bfa_trc_mod_s *trcm, int fileno, int line, u64 data)
  94. {
  95. int tail = trcm->tail;
  96. struct bfa_trc_s *trc = &trcm->trc[tail];
  97. if (trcm->stopped)
  98. return;
  99. trc->fileno = (u16) fileno;
  100. trc->line = (u16) line;
  101. trc->data.u64 = data;
  102. trc->timestamp = BFA_TRC_TS(trcm);
  103. dc_flush(trc);
  104. trcm->tail = (trcm->tail + 1) & (BFA_TRC_MAX - 1);
  105. if (trcm->tail == trcm->head)
  106. trcm->head = (trcm->head + 1) & (BFA_TRC_MAX - 1);
  107. dc_flush(trcm);
  108. }
  109. static inline void
  110. __bfa_trc32(struct bfa_trc_mod_s *trcm, int fileno, int line, u32 data)
  111. {
  112. int tail = trcm->tail;
  113. struct bfa_trc_s *trc = &trcm->trc[tail];
  114. if (trcm->stopped)
  115. return;
  116. trc->fileno = (u16) fileno;
  117. trc->line = (u16) line;
  118. trc->data.u32.u32 = data;
  119. trc->timestamp = BFA_TRC_TS(trcm);
  120. dc_flush(trc);
  121. trcm->tail = (trcm->tail + 1) & (BFA_TRC_MAX - 1);
  122. if (trcm->tail == trcm->head)
  123. trcm->head = (trcm->head + 1) & (BFA_TRC_MAX - 1);
  124. dc_flush(trcm);
  125. }
  126. #ifndef BFA_PERF_BUILD
  127. #define bfa_trc_fp(_trcp, _data) bfa_trc(_trcp, _data)
  128. #else
  129. #define bfa_trc_fp(_trcp, _data)
  130. #endif
  131. /*
  132. * @ BFA LOG interfaces
  133. */
  134. #define bfa_assert(__cond) do { \
  135. if (!(__cond)) { \
  136. printk(KERN_ERR "assert(%s) failed at %s:%d\\n", \
  137. #__cond, __FILE__, __LINE__); \
  138. } \
  139. } while (0)
  140. #define bfa_sm_fault(__mod, __event) do { \
  141. bfa_trc(__mod, (((u32)0xDEAD << 16) | __event)); \
  142. printk(KERN_ERR "Assertion failure: %s:%d: %d", \
  143. __FILE__, __LINE__, (__event)); \
  144. } while (0)
  145. #ifndef BFA_PERF_BUILD
  146. #define bfa_assert_fp(__cond) bfa_assert(__cond)
  147. #else
  148. #define bfa_assert_fp(__cond)
  149. #endif
  150. /* BFA queue definitions */
  151. #define bfa_q_first(_q) ((void *)(((struct list_head *) (_q))->next))
  152. #define bfa_q_next(_qe) (((struct list_head *) (_qe))->next)
  153. #define bfa_q_prev(_qe) (((struct list_head *) (_qe))->prev)
  154. /*
  155. * bfa_q_qe_init - to initialize a queue element
  156. */
  157. #define bfa_q_qe_init(_qe) { \
  158. bfa_q_next(_qe) = (struct list_head *) NULL; \
  159. bfa_q_prev(_qe) = (struct list_head *) NULL; \
  160. }
  161. /*
  162. * bfa_q_deq - dequeue an element from head of the queue
  163. */
  164. #define bfa_q_deq(_q, _qe) { \
  165. if (!list_empty(_q)) { \
  166. (*((struct list_head **) (_qe))) = bfa_q_next(_q); \
  167. bfa_q_prev(bfa_q_next(*((struct list_head **) _qe))) = \
  168. (struct list_head *) (_q); \
  169. bfa_q_next(_q) = bfa_q_next(*((struct list_head **) _qe));\
  170. BFA_Q_DBG_INIT(*((struct list_head **) _qe)); \
  171. } else { \
  172. *((struct list_head **) (_qe)) = (struct list_head *) NULL;\
  173. } \
  174. }
  175. /*
  176. * bfa_q_deq_tail - dequeue an element from tail of the queue
  177. */
  178. #define bfa_q_deq_tail(_q, _qe) { \
  179. if (!list_empty(_q)) { \
  180. *((struct list_head **) (_qe)) = bfa_q_prev(_q); \
  181. bfa_q_next(bfa_q_prev(*((struct list_head **) _qe))) = \
  182. (struct list_head *) (_q); \
  183. bfa_q_prev(_q) = bfa_q_prev(*(struct list_head **) _qe);\
  184. BFA_Q_DBG_INIT(*((struct list_head **) _qe)); \
  185. } else { \
  186. *((struct list_head **) (_qe)) = (struct list_head *) NULL;\
  187. } \
  188. }
  189. static inline int
  190. bfa_q_is_on_q_func(struct list_head *q, struct list_head *qe)
  191. {
  192. struct list_head *tqe;
  193. tqe = bfa_q_next(q);
  194. while (tqe != q) {
  195. if (tqe == qe)
  196. return 1;
  197. tqe = bfa_q_next(tqe);
  198. if (tqe == NULL)
  199. break;
  200. }
  201. return 0;
  202. }
  203. /*
  204. * #ifdef BFA_DEBUG (Using bfa_assert to check for debug_build is not
  205. * consistent across modules)
  206. */
  207. #ifndef BFA_PERF_BUILD
  208. #define BFA_Q_DBG_INIT(_qe) bfa_q_qe_init(_qe)
  209. #else
  210. #define BFA_Q_DBG_INIT(_qe)
  211. #endif
  212. #define bfa_q_is_on_q(_q, _qe) \
  213. bfa_q_is_on_q_func(_q, (struct list_head *)(_qe))
  214. /*
  215. * @ BFA state machine interfaces
  216. */
  217. typedef void (*bfa_sm_t)(void *sm, int event);
  218. /*
  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
  223. */
  224. #define bfa_sm_state_decl(oc, st, otype, etype) \
  225. static void oc ## _sm_ ## st(otype * fsm, etype event)
  226. #define bfa_sm_set_state(_sm, _state) ((_sm)->sm = (bfa_sm_t)(_state))
  227. #define bfa_sm_send_event(_sm, _event) ((_sm)->sm((_sm), (_event)))
  228. #define bfa_sm_get_state(_sm) ((_sm)->sm)
  229. #define bfa_sm_cmp_state(_sm, _state) ((_sm)->sm == (bfa_sm_t)(_state))
  230. /*
  231. * For converting from state machine function to state encoding.
  232. */
  233. struct bfa_sm_table_s {
  234. bfa_sm_t sm; /* state machine function */
  235. int state; /* state machine encoding */
  236. char *name; /* state name for display */
  237. };
  238. #define BFA_SM(_sm) ((bfa_sm_t)(_sm))
  239. /*
  240. * State machine with entry actions.
  241. */
  242. typedef void (*bfa_fsm_t)(void *fsm, int event);
  243. /*
  244. * oc - object class eg. bfa_ioc
  245. * st - state, eg. reset
  246. * otype - object type, eg. struct bfa_ioc_s
  247. * etype - object type, eg. enum ioc_event
  248. */
  249. #define bfa_fsm_state_decl(oc, st, otype, etype) \
  250. static void oc ## _sm_ ## st(otype * fsm, etype event); \
  251. static void oc ## _sm_ ## st ## _entry(otype * fsm)
  252. #define bfa_fsm_set_state(_fsm, _state) do { \
  253. (_fsm)->fsm = (bfa_fsm_t)(_state); \
  254. _state ## _entry(_fsm); \
  255. } while (0)
  256. #define bfa_fsm_send_event(_fsm, _event) ((_fsm)->fsm((_fsm), (_event)))
  257. #define bfa_fsm_get_state(_fsm) ((_fsm)->fsm)
  258. #define bfa_fsm_cmp_state(_fsm, _state) \
  259. ((_fsm)->fsm == (bfa_fsm_t)(_state))
  260. static inline int
  261. bfa_sm_to_state(struct bfa_sm_table_s *smt, bfa_sm_t sm)
  262. {
  263. int i = 0;
  264. while (smt[i].sm && smt[i].sm != sm)
  265. i++;
  266. return smt[i].state;
  267. }
  268. /*
  269. * @ Generic wait counter.
  270. */
  271. typedef void (*bfa_wc_resume_t) (void *cbarg);
  272. struct bfa_wc_s {
  273. bfa_wc_resume_t wc_resume;
  274. void *wc_cbarg;
  275. int wc_count;
  276. };
  277. static inline void
  278. bfa_wc_up(struct bfa_wc_s *wc)
  279. {
  280. wc->wc_count++;
  281. }
  282. static inline void
  283. bfa_wc_down(struct bfa_wc_s *wc)
  284. {
  285. wc->wc_count--;
  286. if (wc->wc_count == 0)
  287. wc->wc_resume(wc->wc_cbarg);
  288. }
  289. /*
  290. * Initialize a waiting counter.
  291. */
  292. static inline void
  293. bfa_wc_init(struct bfa_wc_s *wc, bfa_wc_resume_t wc_resume, void *wc_cbarg)
  294. {
  295. wc->wc_resume = wc_resume;
  296. wc->wc_cbarg = wc_cbarg;
  297. wc->wc_count = 0;
  298. bfa_wc_up(wc);
  299. }
  300. /*
  301. * Wait for counter to reach zero
  302. */
  303. static inline void
  304. bfa_wc_wait(struct bfa_wc_s *wc)
  305. {
  306. bfa_wc_down(wc);
  307. }
  308. #endif /* __BFA_CS_H__ */