xref: /freebsd/sys/dev/ocs_fc/ocs_sm.c (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
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.
35  */
36 
37 #include "ocs_os.h"
38 #include "ocs_sm.h"
39 
40 const char *ocs_sm_id[] = {
41 	"common",
42 	"domain",
43 	"login"
44 };
45 
46 /**
47  * @brief Post an event to a context.
48  *
49  * @param ctx State machine context
50  * @param evt Event to post
51  * @param data Event-specific data (if any)
52  *
53  * @return 0 if successfully posted event; -1 if state machine
54  *         is disabled
55  */
56 int
57 ocs_sm_post_event(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *data)
58 {
59 	if (ctx->current_state) {
60 		ctx->current_state(ctx, evt, data);
61 		return 0;
62 	} else {
63 		return -1;
64 	}
65 }
66 
67 /**
68  * @brief Transition to a new state.
69  */
70 void
71 ocs_sm_transition(ocs_sm_ctx_t *ctx, ocs_sm_function_t state, void *data)
72 {
73 	if (ctx->current_state == state) {
74 		ocs_sm_post_event(ctx, OCS_EVT_REENTER, data);
75 	} else {
76 		ocs_sm_post_event(ctx, OCS_EVT_EXIT, data);
77 		ctx->current_state = state;
78 		ocs_sm_post_event(ctx, OCS_EVT_ENTER, data);
79 	}
80 }
81 
82 /**
83  * @brief Disable further state machine processing.
84  */
85 void
86 ocs_sm_disable(ocs_sm_ctx_t *ctx)
87 {
88 	ctx->current_state = NULL;
89 }
90 
91 const char *ocs_sm_event_name(ocs_sm_event_t evt)
92 {
93 	switch (evt) {
94 	#define RETEVT(x)	case x:		return #x;
95 	RETEVT(OCS_EVT_ENTER)
96 	RETEVT(OCS_EVT_REENTER)
97 	RETEVT(OCS_EVT_EXIT)
98 	RETEVT(OCS_EVT_SHUTDOWN)
99 	RETEVT(OCS_EVT_RESPONSE)
100 	RETEVT(OCS_EVT_RESUME)
101 	RETEVT(OCS_EVT_TIMER_EXPIRED)
102 	RETEVT(OCS_EVT_ERROR)
103 	RETEVT(OCS_EVT_SRRS_ELS_REQ_OK)
104 	RETEVT(OCS_EVT_SRRS_ELS_CMPL_OK)
105 	RETEVT(OCS_EVT_SRRS_ELS_REQ_FAIL)
106 	RETEVT(OCS_EVT_SRRS_ELS_CMPL_FAIL)
107 	RETEVT(OCS_EVT_SRRS_ELS_REQ_RJT)
108 	RETEVT(OCS_EVT_NODE_ATTACH_OK)
109 	RETEVT(OCS_EVT_NODE_ATTACH_FAIL)
110 	RETEVT(OCS_EVT_NODE_FREE_OK)
111 	RETEVT(OCS_EVT_ELS_REQ_TIMEOUT)
112 	RETEVT(OCS_EVT_ELS_REQ_ABORTED)
113 	RETEVT(OCS_EVT_ABORT_ELS)
114 	RETEVT(OCS_EVT_ELS_ABORT_CMPL)
115 
116 	RETEVT(OCS_EVT_DOMAIN_FOUND)
117 	RETEVT(OCS_EVT_DOMAIN_ALLOC_OK)
118 	RETEVT(OCS_EVT_DOMAIN_ALLOC_FAIL)
119 	RETEVT(OCS_EVT_DOMAIN_REQ_ATTACH)
120 	RETEVT(OCS_EVT_DOMAIN_ATTACH_OK)
121 	RETEVT(OCS_EVT_DOMAIN_ATTACH_FAIL)
122 	RETEVT(OCS_EVT_DOMAIN_LOST)
123 	RETEVT(OCS_EVT_DOMAIN_FREE_OK)
124 	RETEVT(OCS_EVT_DOMAIN_FREE_FAIL)
125 	RETEVT(OCS_EVT_HW_DOMAIN_REQ_ATTACH)
126 	RETEVT(OCS_EVT_HW_DOMAIN_REQ_FREE)
127 	RETEVT(OCS_EVT_ALL_CHILD_NODES_FREE)
128 
129 	RETEVT(OCS_EVT_SPORT_ALLOC_OK)
130 	RETEVT(OCS_EVT_SPORT_ALLOC_FAIL)
131 	RETEVT(OCS_EVT_SPORT_ATTACH_OK)
132 	RETEVT(OCS_EVT_SPORT_ATTACH_FAIL)
133 	RETEVT(OCS_EVT_SPORT_FREE_OK)
134 	RETEVT(OCS_EVT_SPORT_FREE_FAIL)
135 	RETEVT(OCS_EVT_SPORT_TOPOLOGY_NOTIFY)
136 	RETEVT(OCS_EVT_HW_PORT_ALLOC_OK)
137 	RETEVT(OCS_EVT_HW_PORT_ALLOC_FAIL)
138 	RETEVT(OCS_EVT_HW_PORT_ATTACH_OK)
139 	RETEVT(OCS_EVT_HW_PORT_REQ_ATTACH)
140 	RETEVT(OCS_EVT_HW_PORT_REQ_FREE)
141 	RETEVT(OCS_EVT_HW_PORT_FREE_OK)
142 
143 	RETEVT(OCS_EVT_NODE_FREE_FAIL)
144 
145 	RETEVT(OCS_EVT_ABTS_RCVD)
146 
147 	RETEVT(OCS_EVT_NODE_MISSING)
148 	RETEVT(OCS_EVT_NODE_REFOUND)
149 	RETEVT(OCS_EVT_SHUTDOWN_IMPLICIT_LOGO)
150 	RETEVT(OCS_EVT_SHUTDOWN_EXPLICIT_LOGO)
151 
152 	RETEVT(OCS_EVT_ELS_FRAME)
153 	RETEVT(OCS_EVT_PLOGI_RCVD)
154 	RETEVT(OCS_EVT_FLOGI_RCVD)
155 	RETEVT(OCS_EVT_LOGO_RCVD)
156 	RETEVT(OCS_EVT_PRLI_RCVD)
157 	RETEVT(OCS_EVT_PRLO_RCVD)
158 	RETEVT(OCS_EVT_PDISC_RCVD)
159 	RETEVT(OCS_EVT_FDISC_RCVD)
160 	RETEVT(OCS_EVT_ADISC_RCVD)
161 	RETEVT(OCS_EVT_RSCN_RCVD)
162 	RETEVT(OCS_EVT_SCR_RCVD)
163 	RETEVT(OCS_EVT_ELS_RCVD)
164 	RETEVT(OCS_EVT_LAST)
165 	RETEVT(OCS_EVT_FCP_CMD_RCVD)
166 
167 	RETEVT(OCS_EVT_RFT_ID_RCVD)
168 	RETEVT(OCS_EVT_RFF_ID_RCVD)
169 	RETEVT(OCS_EVT_GNN_ID_RCVD)
170 	RETEVT(OCS_EVT_GPN_ID_RCVD)
171 	RETEVT(OCS_EVT_GFPN_ID_RCVD)
172 	RETEVT(OCS_EVT_GFF_ID_RCVD)
173 	RETEVT(OCS_EVT_GID_FT_RCVD)
174 	RETEVT(OCS_EVT_GID_PT_RCVD)
175 	RETEVT(OCS_EVT_RPN_ID_RCVD)
176 	RETEVT(OCS_EVT_RNN_ID_RCVD)
177 	RETEVT(OCS_EVT_RCS_ID_RCVD)
178 	RETEVT(OCS_EVT_RSNN_NN_RCVD)
179 	RETEVT(OCS_EVT_RSPN_ID_RCVD)
180 	RETEVT(OCS_EVT_RHBA_RCVD)
181 	RETEVT(OCS_EVT_RPA_RCVD)
182 
183 	RETEVT(OCS_EVT_GIDPT_DELAY_EXPIRED)
184 
185 	RETEVT(OCS_EVT_ABORT_IO)
186 	RETEVT(OCS_EVT_ABORT_IO_NO_RESP)
187 	RETEVT(OCS_EVT_IO_CMPL)
188 	RETEVT(OCS_EVT_IO_CMPL_ERRORS)
189 	RETEVT(OCS_EVT_RESP_CMPL)
190 	RETEVT(OCS_EVT_ABORT_CMPL)
191 	RETEVT(OCS_EVT_NODE_ACTIVE_IO_LIST_EMPTY)
192 	RETEVT(OCS_EVT_NODE_DEL_INI_COMPLETE)
193 	RETEVT(OCS_EVT_NODE_DEL_TGT_COMPLETE)
194 	RETEVT(OCS_EVT_IO_ABORTED_BY_TMF)
195 	RETEVT(OCS_EVT_IO_ABORT_IGNORED)
196 	RETEVT(OCS_EVT_IO_FIRST_BURST)
197 	RETEVT(OCS_EVT_IO_FIRST_BURST_ERR)
198 	RETEVT(OCS_EVT_IO_FIRST_BURST_ABORTED)
199 
200 	default:
201 		break;
202 	#undef RETEVT
203 	}
204 	return "unknown";
205 }
206