xref: /freebsd/sys/dev/isci/scil/sci_base_state_machine.c (revision dd41de95a84d979615a2ef11df6850622bf6184e)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
3  *
4  * This file is provided under a dual BSD/GPLv2 license.  When using or
5  * redistributing this file, you may do so under either license.
6  *
7  * GPL LICENSE SUMMARY
8  *
9  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of version 2 of the GNU General Public License as
13  * published by the Free Software Foundation.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
23  * The full GNU General Public License is included in this distribution
24  * in the file called LICENSE.GPL.
25  *
26  * BSD LICENSE
27  *
28  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
29  * All rights reserved.
30  *
31  * Redistribution and use in source and binary forms, with or without
32  * modification, are permitted provided that the following conditions
33  * are met:
34  *
35  *   * Redistributions of source code must retain the above copyright
36  *     notice, this list of conditions and the following disclaimer.
37  *   * Redistributions in binary form must reproduce the above copyright
38  *     notice, this list of conditions and the following disclaimer in
39  *     the documentation and/or other materials provided with the
40  *     distribution.
41  *
42  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
43  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
44  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
45  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
46  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
47  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
48  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
49  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
50  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
51  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
52  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
53  */
54 
55 #include <sys/cdefs.h>
56 __FBSDID("$FreeBSD$");
57 
58 /**
59  * @file
60  *
61  * @brief This file contains all of the functionality common to all state
62  *        machine object implementations.
63  */
64 
65 #include <dev/isci/scil/sci_base_state_machine.h>
66 
67 #define SCI_STATE_MACHINE_EXIT_STATE(state_machine) \
68    if ( \
69        ((state_machine)->state_table[(state_machine)->current_state_id].\
70           exit_state != NULL) \
71       ) \
72    { \
73       ((state_machine)->state_table[(state_machine)->current_state_id].\
74       exit_state((state_machine)->state_machine_owner)); \
75    }
76 
77 #define SCI_STATE_MACHINE_ENTER_STATE(state_machine) \
78       ((state_machine)->state_table[(state_machine)->current_state_id].\
79       enter_state((state_machine)->state_machine_owner))
80 
81 #define SCI_STATE_MACHINE_SET_STATE(state_machine, id) \
82    ((state_machine)->current_state_id = (id))
83 
84 //******************************************************************************
85 //* P R O T E C T E D    M E T H O D S
86 //******************************************************************************
87 
88 /**
89  * @brief This method will set the initial state and state table
90  *        for the state machine. The caller should follow this
91  *        request with the initialize request to cause the state
92  *        machine to start.
93  *
94  * @param[in] this_state_machine This parameter provides the state machine
95  *            object to be constructed.
96  * @param[in] state_machine_owner This parameter indicates the object that
97  *            is owns the state machine being constructed.
98  * @param[in] state_table This parameter specifies the table of state objects
99  *            that is managed by this state machine.
100  * @param[in] initial_state This parameter specifies the value of the initial
101  *            state for this state machine.
102  *
103  * @return none
104  */
105 void sci_base_state_machine_construct(
106    SCI_BASE_STATE_MACHINE_T * this_state_machine,
107    SCI_BASE_OBJECT_T        * my_state_machine_owner,
108    SCI_BASE_STATE_T         * state_table,
109    U32                        initial_state
110 )
111 {
112 #if defined(SCI_LOGGING)
113    sci_base_subject_construct(&this_state_machine->parent);
114 #endif // defined(SCI_LOGGING)
115 
116    this_state_machine->state_machine_owner = my_state_machine_owner;
117    this_state_machine->initial_state_id  = initial_state;
118    this_state_machine->previous_state_id = initial_state;
119    this_state_machine->current_state_id  = initial_state;
120    this_state_machine->state_table       = state_table;
121 }
122 
123 /**
124  * @brief This method will cause the state machine to enter the
125  *        initial state.
126  *
127  * @see sci_base_state_machine_construct() for how to set the initial state
128  *
129  * @param[in] this_state_machine This parameter specifies the state machine
130  *            that is to be started.
131  *
132  * @return none
133  */
134 void sci_base_state_machine_start(
135    SCI_BASE_STATE_MACHINE_T *this_state_machine
136 )
137 {
138    SCI_STATE_MACHINE_SET_STATE(
139       this_state_machine, this_state_machine->initial_state_id
140    );
141 
142 #if defined(SCI_BASE_ENABLE_SUBJECT_NOTIFICATION)
143    sci_base_subject_notify(&this_state_machine->parent);
144 #endif
145 
146    SCI_STATE_MACHINE_ENTER_STATE(this_state_machine);
147 }
148 
149 /**
150  * @brief This method will cause the state machine to exit it's current
151  *        state only.
152  *
153  * @param[in] this_state_machine This parameter specifies the state machine
154  *            that is to be stopped.
155  *
156  * @return none
157  */
158 void sci_base_state_machine_stop(
159    SCI_BASE_STATE_MACHINE_T *this_state_machine
160 )
161 {
162    SCI_STATE_MACHINE_EXIT_STATE(this_state_machine);
163 
164 #if defined(SCI_BASE_ENABLE_SUBJECT_NOTIFICATION)
165    sci_base_subject_notify(&this_state_machine->parent);
166 #endif
167 }
168 
169 /**
170  * @brief This method performs an update to the current state of
171  *        the state machine.
172  *
173  * @param[in] this_state_machine This parameter specifies the state machine
174  *            for which the caller wishes to perform a state change.
175  * @param[in] next_state This parameter specifies the new state for the
176  *            state machine.
177  *
178  * @return none
179  */
180 void sci_base_state_machine_change_state(
181    SCI_BASE_STATE_MACHINE_T *this_state_machine,
182    U32   next_state
183 )
184 {
185    SCI_STATE_MACHINE_EXIT_STATE(this_state_machine);
186 
187    this_state_machine->previous_state_id = this_state_machine->current_state_id;
188    SCI_STATE_MACHINE_SET_STATE(this_state_machine, next_state);
189 
190 #if defined(SCI_BASE_ENABLE_SUBJECT_NOTIFICATION)
191    // Notify of the state change prior to entering the state.
192    sci_base_subject_notify(&this_state_machine->parent);
193 #endif
194 
195    SCI_STATE_MACHINE_ENTER_STATE(this_state_machine);
196 }
197 
198 /**
199  * @brief This method simply returns the current state of the
200  *         state machine to the caller.
201  *
202  * @param[in] this_state_machine  This parameter specifies the state
203  *            machine for which to retrieve the current state.
204  *
205  * @return This method returns a U32 value indicating the current state for
206  *         the supplied state machine.
207  */
208 U32 sci_base_state_machine_get_state(
209    SCI_BASE_STATE_MACHINE_T *this_state_machine
210 )
211 {
212    return this_state_machine->current_state_id;
213 }
214 
215