bfa_cs.h 7.5 KB

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