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