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