bfa_cs.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (c) 2005-2014 Brocade Communications Systems, Inc.
  4. * Copyright (c) 2014- QLogic Corporation.
  5. * All rights reserved
  6. * www.qlogic.com
  7. *
  8. * Linux driver for QLogic BR-series Fibre Channel Host Bus Adapter.
  9. */
  10. /*
  11. * bfa_cs.h BFA common services
  12. */
  13. #ifndef __BFA_CS_H__
  14. #define __BFA_CS_H__
  15. #include "bfad_drv.h"
  16. /*
  17. * BFA TRC
  18. */
  19. #ifndef BFA_TRC_MAX
  20. #define BFA_TRC_MAX (4 * 1024)
  21. #endif
  22. #define BFA_TRC_TS(_trcm) \
  23. ({ \
  24. struct timespec64 ts; \
  25. \
  26. ktime_get_ts64(&ts); \
  27. (ts.tv_sec*1000000+ts.tv_nsec / 1000); \
  28. })
  29. #ifndef BFA_TRC_TS
  30. #define BFA_TRC_TS(_trcm) ((_trcm)->ticks++)
  31. #endif
  32. struct bfa_trc_s {
  33. #ifdef __BIG_ENDIAN
  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. void
  88. __bfa_trc(struct bfa_trc_mod_s *trcm, int fileno, int line, u64 data);
  89. void
  90. __bfa_trc32(struct bfa_trc_mod_s *trcm, int fileno, int line, u32 data);
  91. #define bfa_sm_fault(__mod, __event) do { \
  92. bfa_trc(__mod, (((u32)0xDEAD << 16) | __event)); \
  93. printk(KERN_ERR "Assertion failure: %s:%d: %d", \
  94. __FILE__, __LINE__, (__event)); \
  95. } while (0)
  96. /* BFA queue definitions */
  97. #define bfa_q_first(_q) ((void *)(((struct list_head *) (_q))->next))
  98. #define bfa_q_next(_qe) (((struct list_head *) (_qe))->next)
  99. #define bfa_q_prev(_qe) (((struct list_head *) (_qe))->prev)
  100. /*
  101. * bfa_q_qe_init - to initialize a queue element
  102. */
  103. #define bfa_q_qe_init(_qe) { \
  104. bfa_q_next(_qe) = (struct list_head *) NULL; \
  105. bfa_q_prev(_qe) = (struct list_head *) NULL; \
  106. }
  107. /*
  108. * bfa_q_deq - dequeue an element from head of the queue
  109. */
  110. #define bfa_q_deq(_q, _qe) do { \
  111. if (!list_empty(_q)) { \
  112. (*((struct list_head **) (_qe))) = bfa_q_next(_q); \
  113. bfa_q_prev(bfa_q_next(*((struct list_head **) _qe))) = \
  114. (struct list_head *) (_q); \
  115. bfa_q_next(_q) = bfa_q_next(*((struct list_head **) _qe));\
  116. } else { \
  117. *((struct list_head **) (_qe)) = (struct list_head *) NULL;\
  118. } \
  119. } while (0)
  120. /*
  121. * bfa_q_deq_tail - dequeue an element from tail of the queue
  122. */
  123. #define bfa_q_deq_tail(_q, _qe) { \
  124. if (!list_empty(_q)) { \
  125. *((struct list_head **) (_qe)) = bfa_q_prev(_q); \
  126. bfa_q_next(bfa_q_prev(*((struct list_head **) _qe))) = \
  127. (struct list_head *) (_q); \
  128. bfa_q_prev(_q) = bfa_q_prev(*(struct list_head **) _qe);\
  129. } else { \
  130. *((struct list_head **) (_qe)) = (struct list_head *) NULL;\
  131. } \
  132. }
  133. static inline int
  134. bfa_q_is_on_q_func(struct list_head *q, struct list_head *qe)
  135. {
  136. struct list_head *tqe;
  137. tqe = bfa_q_next(q);
  138. while (tqe != q) {
  139. if (tqe == qe)
  140. return 1;
  141. tqe = bfa_q_next(tqe);
  142. if (tqe == NULL)
  143. break;
  144. }
  145. return 0;
  146. }
  147. #define bfa_q_is_on_q(_q, _qe) \
  148. bfa_q_is_on_q_func(_q, (struct list_head *)(_qe))
  149. /*
  150. * @ BFA state machine interfaces
  151. */
  152. typedef void (*bfa_sm_t)(void *sm, int event);
  153. /*
  154. * oc - object class eg. bfa_ioc
  155. * st - state, eg. reset
  156. * otype - object type, eg. struct bfa_ioc_s
  157. * etype - object type, eg. enum ioc_event
  158. */
  159. #define bfa_sm_state_decl(oc, st, otype, etype) \
  160. static void oc ## _sm_ ## st(otype * fsm, etype event)
  161. #define bfa_sm_set_state(_sm, _state) ((_sm)->sm = (bfa_sm_t)(_state))
  162. #define bfa_sm_send_event(_sm, _event) ((_sm)->sm((_sm), (_event)))
  163. #define bfa_sm_get_state(_sm) ((_sm)->sm)
  164. #define bfa_sm_cmp_state(_sm, _state) ((_sm)->sm == (bfa_sm_t)(_state))
  165. /*
  166. * For converting from state machine function to state encoding.
  167. */
  168. struct bfa_sm_table_s {
  169. bfa_sm_t sm; /* state machine function */
  170. int state; /* state machine encoding */
  171. char *name; /* state name for display */
  172. };
  173. #define BFA_SM(_sm) ((bfa_sm_t)(_sm))
  174. /*
  175. * State machine with entry actions.
  176. */
  177. typedef void (*bfa_fsm_t)(void *fsm, int event);
  178. /*
  179. * oc - object class eg. bfa_ioc
  180. * st - state, eg. reset
  181. * otype - object type, eg. struct bfa_ioc_s
  182. * etype - object type, eg. enum ioc_event
  183. */
  184. #define bfa_fsm_state_decl(oc, st, otype, etype) \
  185. static void oc ## _sm_ ## st(otype * fsm, etype event); \
  186. static void oc ## _sm_ ## st ## _entry(otype * fsm)
  187. #define bfa_fsm_set_state(_fsm, _state) do { \
  188. (_fsm)->fsm = (bfa_fsm_t)(_state); \
  189. _state ## _entry(_fsm); \
  190. } while (0)
  191. #define bfa_fsm_send_event(_fsm, _event) ((_fsm)->fsm((_fsm), (_event)))
  192. #define bfa_fsm_get_state(_fsm) ((_fsm)->fsm)
  193. #define bfa_fsm_cmp_state(_fsm, _state) \
  194. ((_fsm)->fsm == (bfa_fsm_t)(_state))
  195. static inline int
  196. bfa_sm_to_state(struct bfa_sm_table_s *smt, bfa_sm_t sm)
  197. {
  198. int i = 0;
  199. while (smt[i].sm && smt[i].sm != sm)
  200. i++;
  201. return smt[i].state;
  202. }
  203. /*
  204. * @ Generic wait counter.
  205. */
  206. typedef void (*bfa_wc_resume_t) (void *cbarg);
  207. struct bfa_wc_s {
  208. bfa_wc_resume_t wc_resume;
  209. void *wc_cbarg;
  210. int wc_count;
  211. };
  212. static inline void
  213. bfa_wc_up(struct bfa_wc_s *wc)
  214. {
  215. wc->wc_count++;
  216. }
  217. static inline void
  218. bfa_wc_down(struct bfa_wc_s *wc)
  219. {
  220. wc->wc_count--;
  221. if (wc->wc_count == 0)
  222. wc->wc_resume(wc->wc_cbarg);
  223. }
  224. /*
  225. * Initialize a waiting counter.
  226. */
  227. static inline void
  228. bfa_wc_init(struct bfa_wc_s *wc, bfa_wc_resume_t wc_resume, void *wc_cbarg)
  229. {
  230. wc->wc_resume = wc_resume;
  231. wc->wc_cbarg = wc_cbarg;
  232. wc->wc_count = 0;
  233. bfa_wc_up(wc);
  234. }
  235. /*
  236. * Wait for counter to reach zero
  237. */
  238. static inline void
  239. bfa_wc_wait(struct bfa_wc_s *wc)
  240. {
  241. bfa_wc_down(wc);
  242. }
  243. static inline void
  244. wwn2str(char *wwn_str, u64 wwn)
  245. {
  246. union {
  247. u64 wwn;
  248. u8 byte[8];
  249. } w;
  250. w.wwn = wwn;
  251. sprintf(wwn_str, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x", w.byte[0],
  252. w.byte[1], w.byte[2], w.byte[3], w.byte[4], w.byte[5],
  253. w.byte[6], w.byte[7]);
  254. }
  255. static inline void
  256. fcid2str(char *fcid_str, u32 fcid)
  257. {
  258. union {
  259. u32 fcid;
  260. u8 byte[4];
  261. } f;
  262. f.fcid = fcid;
  263. sprintf(fcid_str, "%02x:%02x:%02x", f.byte[1], f.byte[2], f.byte[3]);
  264. }
  265. #define bfa_swap_3b(_x) \
  266. ((((_x) & 0xff) << 16) | \
  267. ((_x) & 0x00ff00) | \
  268. (((_x) & 0xff0000) >> 16))
  269. #ifndef __BIG_ENDIAN
  270. #define bfa_hton3b(_x) bfa_swap_3b(_x)
  271. #else
  272. #define bfa_hton3b(_x) (_x)
  273. #endif
  274. #define bfa_ntoh3b(_x) bfa_hton3b(_x)
  275. #endif /* __BFA_CS_H__ */