bfa_cs.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Linux network driver for Brocade Converged Network Adapter.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License (GPL) Version 2 as
  6. * published by the Free Software Foundation
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. */
  13. /*
  14. * Copyright (c) 2005-2011 Brocade Communications Systems, Inc.
  15. * All rights reserved
  16. * www.brocade.com
  17. */
  18. /**
  19. * @file bfa_cs.h BFA common services
  20. */
  21. #ifndef __BFA_CS_H__
  22. #define __BFA_CS_H__
  23. #include "cna.h"
  24. /**
  25. * @ BFA state machine interfaces
  26. */
  27. typedef void (*bfa_sm_t)(void *sm, int event);
  28. /**
  29. * oc - object class eg. bfa_ioc
  30. * st - state, eg. reset
  31. * otype - object type, eg. struct bfa_ioc
  32. * etype - object type, eg. enum ioc_event
  33. */
  34. #define bfa_sm_state_decl(oc, st, otype, etype) \
  35. static void oc ## _sm_ ## st(otype * fsm, etype event)
  36. #define bfa_sm_set_state(_sm, _state) ((_sm)->sm = (bfa_sm_t)(_state))
  37. #define bfa_sm_send_event(_sm, _event) ((_sm)->sm((_sm), (_event)))
  38. #define bfa_sm_get_state(_sm) ((_sm)->sm)
  39. #define bfa_sm_cmp_state(_sm, _state) ((_sm)->sm == (bfa_sm_t)(_state))
  40. /**
  41. * For converting from state machine function to state encoding.
  42. */
  43. struct bfa_sm_table {
  44. bfa_sm_t sm; /*!< state machine function */
  45. int state; /*!< state machine encoding */
  46. char *name; /*!< state name for display */
  47. };
  48. #define BFA_SM(_sm) ((bfa_sm_t)(_sm))
  49. /**
  50. * State machine with entry actions.
  51. */
  52. typedef void (*bfa_fsm_t)(void *fsm, int event);
  53. /**
  54. * oc - object class eg. bfa_ioc
  55. * st - state, eg. reset
  56. * otype - object type, eg. struct bfa_ioc
  57. * etype - object type, eg. enum ioc_event
  58. */
  59. #define bfa_fsm_state_decl(oc, st, otype, etype) \
  60. static void oc ## _sm_ ## st(otype * fsm, etype event); \
  61. static void oc ## _sm_ ## st ## _entry(otype * fsm)
  62. #define bfa_fsm_set_state(_fsm, _state) do { \
  63. (_fsm)->fsm = (bfa_fsm_t)(_state); \
  64. _state ## _entry(_fsm); \
  65. } while (0)
  66. #define bfa_fsm_send_event(_fsm, _event) ((_fsm)->fsm((_fsm), (_event)))
  67. #define bfa_fsm_get_state(_fsm) ((_fsm)->fsm)
  68. #define bfa_fsm_cmp_state(_fsm, _state) \
  69. ((_fsm)->fsm == (bfa_fsm_t)(_state))
  70. static inline int
  71. bfa_sm_to_state(const struct bfa_sm_table *smt, bfa_sm_t sm)
  72. {
  73. int i = 0;
  74. while (smt[i].sm && smt[i].sm != sm)
  75. i++;
  76. return smt[i].state;
  77. }
  78. /**
  79. * @ Generic wait counter.
  80. */
  81. typedef void (*bfa_wc_resume_t) (void *cbarg);
  82. struct bfa_wc {
  83. bfa_wc_resume_t wc_resume;
  84. void *wc_cbarg;
  85. int wc_count;
  86. };
  87. static inline void
  88. bfa_wc_up(struct bfa_wc *wc)
  89. {
  90. wc->wc_count++;
  91. }
  92. static inline void
  93. bfa_wc_down(struct bfa_wc *wc)
  94. {
  95. wc->wc_count--;
  96. if (wc->wc_count == 0)
  97. wc->wc_resume(wc->wc_cbarg);
  98. }
  99. /**
  100. * Initialize a waiting counter.
  101. */
  102. static inline void
  103. bfa_wc_init(struct bfa_wc *wc, bfa_wc_resume_t wc_resume, void *wc_cbarg)
  104. {
  105. wc->wc_resume = wc_resume;
  106. wc->wc_cbarg = wc_cbarg;
  107. wc->wc_count = 0;
  108. bfa_wc_up(wc);
  109. }
  110. /**
  111. * Wait for counter to reach zero
  112. */
  113. static inline void
  114. bfa_wc_wait(struct bfa_wc *wc)
  115. {
  116. bfa_wc_down(wc);
  117. }
  118. #endif /* __BFA_CS_H__ */