bfa_cs.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. static inline void
  95. __bfa_trc(struct bfa_trc_mod_s *trcm, int fileno, int line, u64 data)
  96. {
  97. int tail = trcm->tail;
  98. struct bfa_trc_s *trc = &trcm->trc[tail];
  99. if (trcm->stopped)
  100. return;
  101. trc->fileno = (u16) fileno;
  102. trc->line = (u16) line;
  103. trc->data.u64 = data;
  104. trc->timestamp = BFA_TRC_TS(trcm);
  105. trcm->tail = (trcm->tail + 1) & (BFA_TRC_MAX - 1);
  106. if (trcm->tail == trcm->head)
  107. trcm->head = (trcm->head + 1) & (BFA_TRC_MAX - 1);
  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. trcm->tail = (trcm->tail + 1) & (BFA_TRC_MAX - 1);
  121. if (trcm->tail == trcm->head)
  122. trcm->head = (trcm->head + 1) & (BFA_TRC_MAX - 1);
  123. }
  124. #define bfa_sm_fault(__mod, __event) do { \
  125. bfa_trc(__mod, (((u32)0xDEAD << 16) | __event)); \
  126. printk(KERN_ERR "Assertion failure: %s:%d: %d", \
  127. __FILE__, __LINE__, (__event)); \
  128. } while (0)
  129. /* BFA queue definitions */
  130. #define bfa_q_first(_q) ((void *)(((struct list_head *) (_q))->next))
  131. #define bfa_q_next(_qe) (((struct list_head *) (_qe))->next)
  132. #define bfa_q_prev(_qe) (((struct list_head *) (_qe))->prev)
  133. /*
  134. * bfa_q_qe_init - to initialize a queue element
  135. */
  136. #define bfa_q_qe_init(_qe) { \
  137. bfa_q_next(_qe) = (struct list_head *) NULL; \
  138. bfa_q_prev(_qe) = (struct list_head *) NULL; \
  139. }
  140. /*
  141. * bfa_q_deq - dequeue an element from head of the queue
  142. */
  143. #define bfa_q_deq(_q, _qe) do { \
  144. if (!list_empty(_q)) { \
  145. (*((struct list_head **) (_qe))) = bfa_q_next(_q); \
  146. bfa_q_prev(bfa_q_next(*((struct list_head **) _qe))) = \
  147. (struct list_head *) (_q); \
  148. bfa_q_next(_q) = bfa_q_next(*((struct list_head **) _qe));\
  149. } else { \
  150. *((struct list_head **) (_qe)) = (struct list_head *) NULL;\
  151. } \
  152. } while (0)
  153. /*
  154. * bfa_q_deq_tail - dequeue an element from tail of the queue
  155. */
  156. #define bfa_q_deq_tail(_q, _qe) { \
  157. if (!list_empty(_q)) { \
  158. *((struct list_head **) (_qe)) = bfa_q_prev(_q); \
  159. bfa_q_next(bfa_q_prev(*((struct list_head **) _qe))) = \
  160. (struct list_head *) (_q); \
  161. bfa_q_prev(_q) = bfa_q_prev(*(struct list_head **) _qe);\
  162. } else { \
  163. *((struct list_head **) (_qe)) = (struct list_head *) NULL;\
  164. } \
  165. }
  166. static inline int
  167. bfa_q_is_on_q_func(struct list_head *q, struct list_head *qe)
  168. {
  169. struct list_head *tqe;
  170. tqe = bfa_q_next(q);
  171. while (tqe != q) {
  172. if (tqe == qe)
  173. return 1;
  174. tqe = bfa_q_next(tqe);
  175. if (tqe == NULL)
  176. break;
  177. }
  178. return 0;
  179. }
  180. #define bfa_q_is_on_q(_q, _qe) \
  181. bfa_q_is_on_q_func(_q, (struct list_head *)(_qe))
  182. /*
  183. * @ BFA state machine interfaces
  184. */
  185. typedef void (*bfa_sm_t)(void *sm, 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_sm_state_decl(oc, st, otype, etype) \
  193. static void oc ## _sm_ ## st(otype * fsm, etype event)
  194. #define bfa_sm_set_state(_sm, _state) ((_sm)->sm = (bfa_sm_t)(_state))
  195. #define bfa_sm_send_event(_sm, _event) ((_sm)->sm((_sm), (_event)))
  196. #define bfa_sm_get_state(_sm) ((_sm)->sm)
  197. #define bfa_sm_cmp_state(_sm, _state) ((_sm)->sm == (bfa_sm_t)(_state))
  198. /*
  199. * For converting from state machine function to state encoding.
  200. */
  201. struct bfa_sm_table_s {
  202. bfa_sm_t sm; /* state machine function */
  203. int state; /* state machine encoding */
  204. char *name; /* state name for display */
  205. };
  206. #define BFA_SM(_sm) ((bfa_sm_t)(_sm))
  207. /*
  208. * State machine with entry actions.
  209. */
  210. typedef void (*bfa_fsm_t)(void *fsm, int event);
  211. /*
  212. * oc - object class eg. bfa_ioc
  213. * st - state, eg. reset
  214. * otype - object type, eg. struct bfa_ioc_s
  215. * etype - object type, eg. enum ioc_event
  216. */
  217. #define bfa_fsm_state_decl(oc, st, otype, etype) \
  218. static void oc ## _sm_ ## st(otype * fsm, etype event); \
  219. static void oc ## _sm_ ## st ## _entry(otype * fsm)
  220. #define bfa_fsm_set_state(_fsm, _state) do { \
  221. (_fsm)->fsm = (bfa_fsm_t)(_state); \
  222. _state ## _entry(_fsm); \
  223. } while (0)
  224. #define bfa_fsm_send_event(_fsm, _event) ((_fsm)->fsm((_fsm), (_event)))
  225. #define bfa_fsm_get_state(_fsm) ((_fsm)->fsm)
  226. #define bfa_fsm_cmp_state(_fsm, _state) \
  227. ((_fsm)->fsm == (bfa_fsm_t)(_state))
  228. static inline int
  229. bfa_sm_to_state(struct bfa_sm_table_s *smt, bfa_sm_t sm)
  230. {
  231. int i = 0;
  232. while (smt[i].sm && smt[i].sm != sm)
  233. i++;
  234. return smt[i].state;
  235. }
  236. /*
  237. * @ Generic wait counter.
  238. */
  239. typedef void (*bfa_wc_resume_t) (void *cbarg);
  240. struct bfa_wc_s {
  241. bfa_wc_resume_t wc_resume;
  242. void *wc_cbarg;
  243. int wc_count;
  244. };
  245. static inline void
  246. bfa_wc_up(struct bfa_wc_s *wc)
  247. {
  248. wc->wc_count++;
  249. }
  250. static inline void
  251. bfa_wc_down(struct bfa_wc_s *wc)
  252. {
  253. wc->wc_count--;
  254. if (wc->wc_count == 0)
  255. wc->wc_resume(wc->wc_cbarg);
  256. }
  257. /*
  258. * Initialize a waiting counter.
  259. */
  260. static inline void
  261. bfa_wc_init(struct bfa_wc_s *wc, bfa_wc_resume_t wc_resume, void *wc_cbarg)
  262. {
  263. wc->wc_resume = wc_resume;
  264. wc->wc_cbarg = wc_cbarg;
  265. wc->wc_count = 0;
  266. bfa_wc_up(wc);
  267. }
  268. /*
  269. * Wait for counter to reach zero
  270. */
  271. static inline void
  272. bfa_wc_wait(struct bfa_wc_s *wc)
  273. {
  274. bfa_wc_down(wc);
  275. }
  276. static inline void
  277. wwn2str(char *wwn_str, u64 wwn)
  278. {
  279. union {
  280. u64 wwn;
  281. u8 byte[8];
  282. } w;
  283. w.wwn = wwn;
  284. sprintf(wwn_str, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x", w.byte[0],
  285. w.byte[1], w.byte[2], w.byte[3], w.byte[4], w.byte[5],
  286. w.byte[6], w.byte[7]);
  287. }
  288. static inline void
  289. fcid2str(char *fcid_str, u32 fcid)
  290. {
  291. union {
  292. u32 fcid;
  293. u8 byte[4];
  294. } f;
  295. f.fcid = fcid;
  296. sprintf(fcid_str, "%02x:%02x:%02x", f.byte[1], f.byte[2], f.byte[3]);
  297. }
  298. #define bfa_swap_3b(_x) \
  299. ((((_x) & 0xff) << 16) | \
  300. ((_x) & 0x00ff00) | \
  301. (((_x) & 0xff0000) >> 16))
  302. #ifndef __BIG_ENDIAN
  303. #define bfa_hton3b(_x) bfa_swap_3b(_x)
  304. #else
  305. #define bfa_hton3b(_x) (_x)
  306. #endif
  307. #define bfa_ntoh3b(_x) bfa_hton3b(_x)
  308. #endif /* __BFA_CS_H__ */