bfa_cs.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. /* BFA common services */
  19. #ifndef __BFA_CS_H__
  20. #define __BFA_CS_H__
  21. #include "cna.h"
  22. /* BFA state machine interfaces */
  23. typedef void (*bfa_sm_t)(void *sm, int event);
  24. /* oc - object class eg. bfa_ioc
  25. * st - state, eg. reset
  26. * otype - object type, eg. struct bfa_ioc
  27. * etype - object type, eg. enum ioc_event
  28. */
  29. #define bfa_sm_state_decl(oc, st, otype, etype) \
  30. static void oc ## _sm_ ## st(otype * fsm, etype event)
  31. #define bfa_sm_set_state(_sm, _state) ((_sm)->sm = (bfa_sm_t)(_state))
  32. #define bfa_sm_send_event(_sm, _event) ((_sm)->sm((_sm), (_event)))
  33. #define bfa_sm_get_state(_sm) ((_sm)->sm)
  34. #define bfa_sm_cmp_state(_sm, _state) ((_sm)->sm == (bfa_sm_t)(_state))
  35. /* For converting from state machine function to state encoding. */
  36. struct bfa_sm_table {
  37. bfa_sm_t sm; /*!< state machine function */
  38. int state; /*!< state machine encoding */
  39. char *name; /*!< state name for display */
  40. };
  41. #define BFA_SM(_sm) ((bfa_sm_t)(_sm))
  42. /* State machine with entry actions. */
  43. typedef void (*bfa_fsm_t)(void *fsm, int event);
  44. /* oc - object class eg. bfa_ioc
  45. * st - state, eg. reset
  46. * otype - object type, eg. struct bfa_ioc
  47. * etype - object type, eg. enum ioc_event
  48. */
  49. #define bfa_fsm_state_decl(oc, st, otype, etype) \
  50. static void oc ## _sm_ ## st(otype * fsm, etype event); \
  51. static void oc ## _sm_ ## st ## _entry(otype * fsm)
  52. #define bfa_fsm_set_state(_fsm, _state) do { \
  53. (_fsm)->fsm = (bfa_fsm_t)(_state); \
  54. _state ## _entry(_fsm); \
  55. } while (0)
  56. #define bfa_fsm_send_event(_fsm, _event) ((_fsm)->fsm((_fsm), (_event)))
  57. #define bfa_fsm_get_state(_fsm) ((_fsm)->fsm)
  58. #define bfa_fsm_cmp_state(_fsm, _state) \
  59. ((_fsm)->fsm == (bfa_fsm_t)(_state))
  60. static inline int
  61. bfa_sm_to_state(const struct bfa_sm_table *smt, bfa_sm_t sm)
  62. {
  63. int i = 0;
  64. while (smt[i].sm && smt[i].sm != sm)
  65. i++;
  66. return smt[i].state;
  67. }
  68. /* Generic wait counter. */
  69. typedef void (*bfa_wc_resume_t) (void *cbarg);
  70. struct bfa_wc {
  71. bfa_wc_resume_t wc_resume;
  72. void *wc_cbarg;
  73. int wc_count;
  74. };
  75. static inline void
  76. bfa_wc_up(struct bfa_wc *wc)
  77. {
  78. wc->wc_count++;
  79. }
  80. static inline void
  81. bfa_wc_down(struct bfa_wc *wc)
  82. {
  83. wc->wc_count--;
  84. if (wc->wc_count == 0)
  85. wc->wc_resume(wc->wc_cbarg);
  86. }
  87. /* Initialize a waiting counter. */
  88. static inline void
  89. bfa_wc_init(struct bfa_wc *wc, bfa_wc_resume_t wc_resume, void *wc_cbarg)
  90. {
  91. wc->wc_resume = wc_resume;
  92. wc->wc_cbarg = wc_cbarg;
  93. wc->wc_count = 0;
  94. bfa_wc_up(wc);
  95. }
  96. /* Wait for counter to reach zero */
  97. static inline void
  98. bfa_wc_wait(struct bfa_wc *wc)
  99. {
  100. bfa_wc_down(wc);
  101. }
  102. #endif /* __BFA_CS_H__ */