1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#ifndef __BFA_CS_H__
22#define __BFA_CS_H__
23
24#include "cna.h"
25
26
27
28typedef void (*bfa_sm_t)(void *sm, int event);
29
30
31
32
33
34
35#define bfa_sm_state_decl(oc, st, otype, etype) \
36 static void oc ## _sm_ ## st(otype * fsm, etype event)
37
38#define bfa_sm_set_state(_sm, _state) ((_sm)->sm = (bfa_sm_t)(_state))
39#define bfa_sm_send_event(_sm, _event) ((_sm)->sm((_sm), (_event)))
40#define bfa_sm_get_state(_sm) ((_sm)->sm)
41#define bfa_sm_cmp_state(_sm, _state) ((_sm)->sm == (bfa_sm_t)(_state))
42
43
44struct bfa_sm_table {
45 bfa_sm_t sm;
46 int state;
47 char *name;
48};
49#define BFA_SM(_sm) ((bfa_sm_t)(_sm))
50
51
52typedef void (*bfa_fsm_t)(void *fsm, int event);
53
54
55
56
57
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
63#define bfa_fsm_set_state(_fsm, _state) do { \
64 (_fsm)->fsm = (bfa_fsm_t)(_state); \
65 _state ## _entry(_fsm); \
66} while (0)
67
68#define bfa_fsm_send_event(_fsm, _event) ((_fsm)->fsm((_fsm), (_event)))
69#define bfa_fsm_get_state(_fsm) ((_fsm)->fsm)
70#define bfa_fsm_cmp_state(_fsm, _state) \
71 ((_fsm)->fsm == (bfa_fsm_t)(_state))
72
73static inline int
74bfa_sm_to_state(const struct bfa_sm_table *smt, bfa_sm_t sm)
75{
76 int i = 0;
77
78 while (smt[i].sm && smt[i].sm != sm)
79 i++;
80 return smt[i].state;
81}
82
83
84
85typedef void (*bfa_wc_resume_t) (void *cbarg);
86
87struct bfa_wc {
88 bfa_wc_resume_t wc_resume;
89 void *wc_cbarg;
90 int wc_count;
91};
92
93static inline void
94bfa_wc_up(struct bfa_wc *wc)
95{
96 wc->wc_count++;
97}
98
99static inline void
100bfa_wc_down(struct bfa_wc *wc)
101{
102 wc->wc_count--;
103 if (wc->wc_count == 0)
104 wc->wc_resume(wc->wc_cbarg);
105}
106
107
108static inline void
109bfa_wc_init(struct bfa_wc *wc, bfa_wc_resume_t wc_resume, void *wc_cbarg)
110{
111 wc->wc_resume = wc_resume;
112 wc->wc_cbarg = wc_cbarg;
113 wc->wc_count = 0;
114 bfa_wc_up(wc);
115}
116
117
118static inline void
119bfa_wc_wait(struct bfa_wc *wc)
120{
121 bfa_wc_down(wc);
122}
123
124#endif
125