1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Linux network driver for QLogic BR-series Converged Network Adapter.
4  */
5 /*
6  * Copyright (c) 2005-2014 Brocade Communications Systems, Inc.
7  * Copyright (c) 2014-2015 QLogic Corporation
8  * All rights reserved
9  * www.qlogic.com
10  */
11 
12 /* BFA common services */
13 
14 #ifndef __BFA_CS_H__
15 #define __BFA_CS_H__
16 
17 #include "cna.h"
18 
19 /* BFA state machine interfaces */
20 
21 /* For converting from state machine function to state encoding. */
22 #define BFA_SM_TABLE(n, s, e, t)				\
23 struct s;							\
24 enum e;								\
25 typedef void (*t)(struct s *, enum e);				\
26 								\
27 struct n ## _sm_table_s {					\
28 	t		sm;	/* state machine function */	\
29 	int		state;	/* state machine encoding */	\
30 	char		*name;	/* state name for display */	\
31 };								\
32 								\
33 static inline int						\
34 n ## _sm_to_state(struct n ## _sm_table_s *smt, t sm)		\
35 {								\
36 	int	i = 0;						\
37 								\
38 	while (smt[i].sm && smt[i].sm != sm)			\
39 		i++;						\
40 	return smt[i].state;					\
41 }
42 
43 BFA_SM_TABLE(iocpf,	bfa_iocpf,	iocpf_event,	bfa_fsm_iocpf_t)
44 BFA_SM_TABLE(ioc,	bfa_ioc,	ioc_event,	bfa_fsm_ioc_t)
45 BFA_SM_TABLE(cmdq,	bfa_msgq_cmdq,	cmdq_event,	bfa_fsm_msgq_cmdq_t)
46 BFA_SM_TABLE(rspq,	bfa_msgq_rspq,	rspq_event,	bfa_fsm_msgq_rspq_t)
47 
48 BFA_SM_TABLE(ioceth,	bna_ioceth,	bna_ioceth_event, bna_fsm_ioceth_t)
49 BFA_SM_TABLE(enet,	bna_enet,	bna_enet_event, bna_fsm_enet_t)
50 BFA_SM_TABLE(ethport,	bna_ethport,	bna_ethport_event, bna_fsm_ethport_t)
51 BFA_SM_TABLE(tx,	bna_tx,		bna_tx_event,	bna_fsm_tx_t)
52 BFA_SM_TABLE(rxf,	bna_rxf,	bna_rxf_event, bna_fsm_rxf_t)
53 BFA_SM_TABLE(rx,	bna_rx,		bna_rx_event,	bna_fsm_rx_t)
54 
55 #undef BFA_SM_TABLE
56 
57 #define BFA_SM(_sm)	(_sm)
58 
59 /* State machine with entry actions. */
60 typedef void (*bfa_fsm_t)(void *fsm, int event);
61 
62 /* oc - object class eg. bfa_ioc
63  * st - state, eg. reset
64  * otype - object type, eg. struct bfa_ioc
65  * etype - object type, eg. enum ioc_event
66  */
67 #define bfa_fsm_state_decl(oc, st, otype, etype)			\
68 	static void oc ## _sm_ ## st(otype * fsm, etype event);		\
69 	static void oc ## _sm_ ## st ## _entry(otype * fsm)
70 
71 #define bfa_fsm_set_state(_fsm, _state) do {				\
72 	(_fsm)->fsm = (_state);						\
73 	_state ## _entry(_fsm);						\
74 } while (0)
75 
76 #define bfa_fsm_send_event(_fsm, _event)	((_fsm)->fsm((_fsm), (_event)))
77 #define bfa_fsm_cmp_state(_fsm, _state)		((_fsm)->fsm == (_state))
78 /* Generic wait counter. */
79 
80 typedef void (*bfa_wc_resume_t) (void *cbarg);
81 
82 struct bfa_wc {
83 	bfa_wc_resume_t wc_resume;
84 	void		*wc_cbarg;
85 	int		wc_count;
86 };
87 
88 static inline void
89 bfa_wc_up(struct bfa_wc *wc)
90 {
91 	wc->wc_count++;
92 }
93 
94 static inline void
95 bfa_wc_down(struct bfa_wc *wc)
96 {
97 	wc->wc_count--;
98 	if (wc->wc_count == 0)
99 		wc->wc_resume(wc->wc_cbarg);
100 }
101 
102 /* Initialize a waiting counter. */
103 static inline void
104 bfa_wc_init(struct bfa_wc *wc, bfa_wc_resume_t wc_resume, void *wc_cbarg)
105 {
106 	wc->wc_resume = wc_resume;
107 	wc->wc_cbarg = wc_cbarg;
108 	wc->wc_count = 0;
109 	bfa_wc_up(wc);
110 }
111 
112 /* Wait for counter to reach zero */
113 static inline void
114 bfa_wc_wait(struct bfa_wc *wc)
115 {
116 	bfa_wc_down(wc);
117 }
118 
119 #endif /* __BFA_CS_H__ */
120