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