xref: /freebsd/sys/dev/ocs_fc/ocs_sm.h (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
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 
32 /**
33  * @file
34  * Generic state machine framework declarations.
35  */
36 
37 #ifndef _OCS_SM_H
38 #define _OCS_SM_H
39 
40 /**
41  * State Machine (SM) IDs.
42  */
43 enum {
44 	OCS_SM_COMMON = 0,
45 	OCS_SM_DOMAIN,
46 	OCS_SM_PORT,
47 	OCS_SM_LOGIN,
48 	OCS_SM_LAST
49 };
50 
51 #define OCS_SM_EVENT_SHIFT		24
52 #define OCS_SM_EVENT_START(id)		((id) << OCS_SM_EVENT_SHIFT)
53 
54 extern const char *ocs_sm_id[];		/**< String format of the above enums. */
55 
56 /**
57  * State Machine events.
58  */
59 typedef enum {
60 	/* Common Events */
61 	OCS_EVT_ENTER = OCS_SM_EVENT_START(OCS_SM_COMMON),
62 	OCS_EVT_REENTER,
63 	OCS_EVT_EXIT,
64 	OCS_EVT_SHUTDOWN,
65 	OCS_EVT_ALL_CHILD_NODES_FREE,
66 	OCS_EVT_RESUME,
67 	OCS_EVT_TIMER_EXPIRED,
68 
69 	/* Domain Events */
70 	OCS_EVT_RESPONSE = OCS_SM_EVENT_START(OCS_SM_DOMAIN),
71 	OCS_EVT_ERROR,
72 
73 	OCS_EVT_DOMAIN_FOUND,
74 	OCS_EVT_DOMAIN_ALLOC_OK,
75 	OCS_EVT_DOMAIN_ALLOC_FAIL,
76 	OCS_EVT_DOMAIN_REQ_ATTACH,
77 	OCS_EVT_DOMAIN_ATTACH_OK,
78 	OCS_EVT_DOMAIN_ATTACH_FAIL,
79 	OCS_EVT_DOMAIN_LOST,
80 	OCS_EVT_DOMAIN_FREE_OK,
81 	OCS_EVT_DOMAIN_FREE_FAIL,
82 	OCS_EVT_HW_DOMAIN_REQ_ATTACH,
83 	OCS_EVT_HW_DOMAIN_REQ_FREE,
84 
85 	/* Sport Events */
86 	OCS_EVT_SPORT_ALLOC_OK = OCS_SM_EVENT_START(OCS_SM_PORT),
87 	OCS_EVT_SPORT_ALLOC_FAIL,
88 	OCS_EVT_SPORT_ATTACH_OK,
89 	OCS_EVT_SPORT_ATTACH_FAIL,
90 	OCS_EVT_SPORT_FREE_OK,
91 	OCS_EVT_SPORT_FREE_FAIL,
92 	OCS_EVT_SPORT_TOPOLOGY_NOTIFY,
93 	OCS_EVT_HW_PORT_ALLOC_OK,
94 	OCS_EVT_HW_PORT_ALLOC_FAIL,
95 	OCS_EVT_HW_PORT_ATTACH_OK,
96 	OCS_EVT_HW_PORT_REQ_ATTACH,
97 	OCS_EVT_HW_PORT_REQ_FREE,
98 	OCS_EVT_HW_PORT_FREE_OK,
99 
100 	/* Login Events */
101 	OCS_EVT_SRRS_ELS_REQ_OK = OCS_SM_EVENT_START(OCS_SM_LOGIN),
102 	OCS_EVT_SRRS_ELS_CMPL_OK,
103 	OCS_EVT_SRRS_ELS_REQ_FAIL,
104 	OCS_EVT_SRRS_ELS_CMPL_FAIL,
105 	OCS_EVT_SRRS_ELS_REQ_RJT,
106 	OCS_EVT_NODE_ATTACH_OK,
107 	OCS_EVT_NODE_ATTACH_FAIL,
108 	OCS_EVT_NODE_FREE_OK,
109 	OCS_EVT_NODE_FREE_FAIL,
110 	OCS_EVT_ELS_FRAME,
111 	OCS_EVT_ELS_REQ_TIMEOUT,
112 	OCS_EVT_ELS_REQ_ABORTED,
113 	OCS_EVT_ABORT_ELS,		/**< request an ELS IO be aborted */
114 	OCS_EVT_ELS_ABORT_CMPL,	        /**< ELS abort process complete */
115 
116 	OCS_EVT_ABTS_RCVD,
117 
118 	OCS_EVT_NODE_MISSING,		/**< node is not in the GID_PT payload */
119 	OCS_EVT_NODE_REFOUND,		/**< node is allocated and in the GID_PT payload */
120 	OCS_EVT_SHUTDOWN_IMPLICIT_LOGO,	/**< node shutting down due to PLOGI recvd (implicit logo) */
121 	OCS_EVT_SHUTDOWN_EXPLICIT_LOGO,	/**< node shutting down due to LOGO recvd/sent (explicit logo) */
122 
123 	OCS_EVT_PLOGI_RCVD,
124 	OCS_EVT_FLOGI_RCVD,
125 	OCS_EVT_LOGO_RCVD,
126 	OCS_EVT_RRQ_RCVD,
127 	OCS_EVT_PRLI_RCVD,
128 	OCS_EVT_PRLO_RCVD,
129 	OCS_EVT_PDISC_RCVD,
130 	OCS_EVT_FDISC_RCVD,
131 	OCS_EVT_ADISC_RCVD,
132 	OCS_EVT_RSCN_RCVD,
133 	OCS_EVT_SCR_RCVD,
134 	OCS_EVT_ELS_RCVD,
135 
136 	OCS_EVT_FCP_CMD_RCVD,
137 
138 	/* Used by fabric emulation */
139 	OCS_EVT_RFT_ID_RCVD,
140 	OCS_EVT_RFF_ID_RCVD,
141 	OCS_EVT_GNN_ID_RCVD,
142 	OCS_EVT_GPN_ID_RCVD,
143 	OCS_EVT_GFPN_ID_RCVD,
144 	OCS_EVT_GFF_ID_RCVD,
145 	OCS_EVT_GID_FT_RCVD,
146 	OCS_EVT_GID_PT_RCVD,
147 	OCS_EVT_RPN_ID_RCVD,
148 	OCS_EVT_RNN_ID_RCVD,
149 	OCS_EVT_RCS_ID_RCVD,
150 	OCS_EVT_RSNN_NN_RCVD,
151 	OCS_EVT_RSPN_ID_RCVD,
152 	OCS_EVT_RHBA_RCVD,
153 	OCS_EVT_RPA_RCVD,
154 
155 	OCS_EVT_GIDPT_DELAY_EXPIRED,
156 
157 	/* SCSI Target Server events */
158 	OCS_EVT_ABORT_IO,
159 	OCS_EVT_ABORT_IO_NO_RESP,
160 	OCS_EVT_IO_CMPL,
161 	OCS_EVT_IO_CMPL_ERRORS,
162 	OCS_EVT_RESP_CMPL,
163 	OCS_EVT_ABORT_CMPL,
164 	OCS_EVT_NODE_ACTIVE_IO_LIST_EMPTY,
165 	OCS_EVT_NODE_DEL_INI_COMPLETE,
166 	OCS_EVT_NODE_DEL_TGT_COMPLETE,
167 	OCS_EVT_IO_ABORTED_BY_TMF,
168 	OCS_EVT_IO_ABORT_IGNORED,
169 	OCS_EVT_IO_FIRST_BURST,
170 	OCS_EVT_IO_FIRST_BURST_ERR,
171 	OCS_EVT_IO_FIRST_BURST_ABORTED,
172 
173 	/* Must be last */
174 	OCS_EVT_LAST
175 } ocs_sm_event_t;
176 
177 /* Declare ocs_sm_ctx_s */
178 typedef struct ocs_sm_ctx_s ocs_sm_ctx_t;
179 
180 /* State machine state function */
181 typedef void *(*ocs_sm_function_t)(ocs_sm_ctx_t *, ocs_sm_event_t, void *);
182 
183 /* State machine context header  */
184 struct ocs_sm_ctx_s {
185 	ocs_sm_function_t current_state;
186 	const char *description;
187 	void	*app;			/** Application-specific handle. */
188 };
189 
190 extern int ocs_sm_post_event(ocs_sm_ctx_t *, ocs_sm_event_t, void *);
191 extern void ocs_sm_transition(ocs_sm_ctx_t *, ocs_sm_function_t, void *);
192 extern void ocs_sm_disable(ocs_sm_ctx_t *ctx);
193 extern const char *ocs_sm_event_name(ocs_sm_event_t evt);
194 
195 #if 0
196 #define smtrace(sm)	ocs_log_debug(NULL, "%s: %-20s -->   %s\n", sm, ocs_sm_event_name(evt), __func__)
197 #else
198 #define smtrace(...)
199 #endif
200 
201 #endif /* ! _OCS_SM_H */
202