bfa_cs.h 2.6 KB

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