xref: /freebsd/sys/dev/ocs_fc/ocs_sm.h (revision 580744621f33383027108364dcadad718df46ffe)
1 /*-
2  * Copyright (c) 2017 Broadcom. All rights reserved.
3  * The term "Broadcom" refers to Broadcom Limited and/or its subsidiaries.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  *    this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  *    this list of conditions and the following disclaimer in the documentation
13  *    and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  *
31  * $FreeBSD$
32  */
33 
34 /**
35  * @file
36  * Generic state machine framework declarations.
37  */
38 
39 #ifndef _OCS_SM_H
40 #define _OCS_SM_H
41 
42 /**
43  * State Machine (SM) IDs.
44  */
45 enum {
46 	OCS_SM_COMMON = 0,
47 	OCS_SM_DOMAIN,
48 	OCS_SM_PORT,
49 	OCS_SM_LOGIN,
50 	OCS_SM_LAST
51 };
52 
53 #define OCS_SM_EVENT_SHIFT		24
54 #define OCS_SM_EVENT_START(id)		((id) << OCS_SM_EVENT_SHIFT)
55 
56 extern const char *ocs_sm_id[];		/**< String format of the above enums. */
57 
58 /**
59  * State Machine events.
60  */
61 typedef enum {
62 	/* Common Events */
63 	OCS_EVT_ENTER = OCS_SM_EVENT_START(OCS_SM_COMMON),
64 	OCS_EVT_REENTER,
65 	OCS_EVT_EXIT,
66 	OCS_EVT_SHUTDOWN,
67 	OCS_EVT_ALL_CHILD_NODES_FREE,
68 	OCS_EVT_RESUME,
69 	OCS_EVT_TIMER_EXPIRED,
70 
71 	/* Domain Events */
72 	OCS_EVT_RESPONSE = OCS_SM_EVENT_START(OCS_SM_DOMAIN),
73 	OCS_EVT_ERROR,
74 
75 	OCS_EVT_DOMAIN_FOUND,
76 	OCS_EVT_DOMAIN_ALLOC_OK,
77 	OCS_EVT_DOMAIN_ALLOC_FAIL,
78 	OCS_EVT_DOMAIN_REQ_ATTACH,
79 	OCS_EVT_DOMAIN_ATTACH_OK,
80 	OCS_EVT_DOMAIN_ATTACH_FAIL,
81 	OCS_EVT_DOMAIN_LOST,
82 	OCS_EVT_DOMAIN_FREE_OK,
83 	OCS_EVT_DOMAIN_FREE_FAIL,
84 	OCS_EVT_HW_DOMAIN_REQ_ATTACH,
85 	OCS_EVT_HW_DOMAIN_REQ_FREE,
86 
87 	/* Sport Events */
88 	OCS_EVT_SPORT_ALLOC_OK = OCS_SM_EVENT_START(OCS_SM_PORT),
89 	OCS_EVT_SPORT_ALLOC_FAIL,
90 	OCS_EVT_SPORT_ATTACH_OK,
91 	OCS_EVT_SPORT_ATTACH_FAIL,
92 	OCS_EVT_SPORT_FREE_OK,
93 	OCS_EVT_SPORT_FREE_FAIL,
94 	OCS_EVT_SPORT_TOPOLOGY_NOTIFY,
95 	OCS_EVT_HW_PORT_ALLOC_OK,
96 	OCS_EVT_HW_PORT_ALLOC_FAIL,
97 	OCS_EVT_HW_PORT_ATTACH_OK,
98 	OCS_EVT_HW_PORT_REQ_ATTACH,
99 	OCS_EVT_HW_PORT_REQ_FREE,
100 	OCS_EVT_HW_PORT_FREE_OK,
101 
102 	/* Login Events */
103 	OCS_EVT_SRRS_ELS_REQ_OK = OCS_SM_EVENT_START(OCS_SM_LOGIN),
104 	OCS_EVT_SRRS_ELS_CMPL_OK,
105 	OCS_EVT_SRRS_ELS_REQ_FAIL,
106 	OCS_EVT_SRRS_ELS_CMPL_FAIL,
107 	OCS_EVT_SRRS_ELS_REQ_RJT,
108 	OCS_EVT_NODE_ATTACH_OK,
109 	OCS_EVT_NODE_ATTACH_FAIL,
110 	OCS_EVT_NODE_FREE_OK,
111 	OCS_EVT_NODE_FREE_FAIL,
112 	OCS_EVT_ELS_FRAME,
113 	OCS_EVT_ELS_REQ_TIMEOUT,
114 	OCS_EVT_ELS_REQ_ABORTED,
115 	OCS_EVT_ABORT_ELS,		/**< request an ELS IO be aborted */
116 	OCS_EVT_ELS_ABORT_CMPL,	        /**< ELS abort process complete */
117 
118 	OCS_EVT_ABTS_RCVD,
119 
120 	OCS_EVT_NODE_MISSING,		/**< node is not in the GID_PT payload */
121 	OCS_EVT_NODE_REFOUND,		/**< node is allocated and in the GID_PT payload */
122 	OCS_EVT_SHUTDOWN_IMPLICIT_LOGO,	/**< node shutting down due to PLOGI recvd (implicit logo) */
123 	OCS_EVT_SHUTDOWN_EXPLICIT_LOGO,	/**< node shutting down due to LOGO recvd/sent (explicit logo) */
124 
125 	OCS_EVT_PLOGI_RCVD,
126 	OCS_EVT_FLOGI_RCVD,
127 	OCS_EVT_LOGO_RCVD,
128 	OCS_EVT_RRQ_RCVD,
129 	OCS_EVT_PRLI_RCVD,
130 	OCS_EVT_PRLO_RCVD,
131 	OCS_EVT_PDISC_RCVD,
132 	OCS_EVT_FDISC_RCVD,
133 	OCS_EVT_ADISC_RCVD,
134 	OCS_EVT_RSCN_RCVD,
135 	OCS_EVT_SCR_RCVD,
136 	OCS_EVT_ELS_RCVD,
137 
138 	OCS_EVT_FCP_CMD_RCVD,
139 
140 	/* Used by fabric emulation */
141 	OCS_EVT_RFT_ID_RCVD,
142 	OCS_EVT_RFF_ID_RCVD,
143 	OCS_EVT_GNN_ID_RCVD,
144 	OCS_EVT_GPN_ID_RCVD,
145 	OCS_EVT_GFPN_ID_RCVD,
146 	OCS_EVT_GFF_ID_RCVD,
147 	OCS_EVT_GID_FT_RCVD,
148 	OCS_EVT_GID_PT_RCVD,
149 	OCS_EVT_RPN_ID_RCVD,
150 	OCS_EVT_RNN_ID_RCVD,
151 	OCS_EVT_RCS_ID_RCVD,
152 	OCS_EVT_RSNN_NN_RCVD,
153 	OCS_EVT_RSPN_ID_RCVD,
154 	OCS_EVT_RHBA_RCVD,
155 	OCS_EVT_RPA_RCVD,
156 
157 	OCS_EVT_GIDPT_DELAY_EXPIRED,
158 
159 	/* SCSI Target Server events */
160 	OCS_EVT_ABORT_IO,
161 	OCS_EVT_ABORT_IO_NO_RESP,
162 	OCS_EVT_IO_CMPL,
163 	OCS_EVT_IO_CMPL_ERRORS,
164 	OCS_EVT_RESP_CMPL,
165 	OCS_EVT_ABORT_CMPL,
166 	OCS_EVT_NODE_ACTIVE_IO_LIST_EMPTY,
167 	OCS_EVT_NODE_DEL_INI_COMPLETE,
168 	OCS_EVT_NODE_DEL_TGT_COMPLETE,
169 	OCS_EVT_IO_ABORTED_BY_TMF,
170 	OCS_EVT_IO_ABORT_IGNORED,
171 	OCS_EVT_IO_FIRST_BURST,
172 	OCS_EVT_IO_FIRST_BURST_ERR,
173 	OCS_EVT_IO_FIRST_BURST_ABORTED,
174 
175 	/* Must be last */
176 	OCS_EVT_LAST
177 } ocs_sm_event_t;
178 
179 /* Declare ocs_sm_ctx_s */
180 typedef struct ocs_sm_ctx_s ocs_sm_ctx_t;
181 
182 /* State machine state function */
183 typedef void *(*ocs_sm_function_t)(ocs_sm_ctx_t *, ocs_sm_event_t, void *);
184 
185 /* State machine context header  */
186 struct ocs_sm_ctx_s {
187 	ocs_sm_function_t current_state;
188 	const char *description;
189 	void	*app;			/** Application-specific handle. */
190 };
191 
192 extern int ocs_sm_post_event(ocs_sm_ctx_t *, ocs_sm_event_t, void *);
193 extern void ocs_sm_transition(ocs_sm_ctx_t *, ocs_sm_function_t, void *);
194 extern void ocs_sm_disable(ocs_sm_ctx_t *ctx);
195 extern const char *ocs_sm_event_name(ocs_sm_event_t evt);
196 
197 #if 0
198 #define smtrace(sm)	ocs_log_debug(NULL, "%s: %-20s -->   %s\n", sm, ocs_sm_event_name(evt), __func__)
199 #else
200 #define smtrace(...)
201 #endif
202 
203 #endif /* ! _OCS_SM_H */
204