bfa_cs.h 9.1 KB

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