2 * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
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.
9 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
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.
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.
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.
28 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
29 * All rights reserved.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
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
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.
55 #include <sys/cdefs.h>
56 __FBSDID("$FreeBSD$");
61 * @brief This file contains all of the functionality common to all state
62 * machine object implementations.
65 #include <dev/isci/scil/sci_base_state_machine.h>
67 #define SCI_STATE_MACHINE_EXIT_STATE(state_machine) \
69 ((state_machine)->state_table[(state_machine)->current_state_id].\
73 ((state_machine)->state_table[(state_machine)->current_state_id].\
74 exit_state((state_machine)->state_machine_owner)); \
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))
81 #define SCI_STATE_MACHINE_SET_STATE(state_machine, id) \
82 ((state_machine)->current_state_id = (id))
84 //******************************************************************************
85 //* P R O T E C T E D M E T H O D S
86 //******************************************************************************
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
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.
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,
112 #if defined(SCI_LOGGING)
113 sci_base_subject_construct(&this_state_machine->parent);
114 #endif // defined(SCI_LOGGING)
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;
124 * @brief This method will cause the state machine to enter the
127 * @see sci_base_state_machine_construct() for how to set the initial state
129 * @param[in] this_state_machine This parameter specifies the state machine
130 * that is to be started.
134 void sci_base_state_machine_start(
135 SCI_BASE_STATE_MACHINE_T *this_state_machine
138 SCI_STATE_MACHINE_SET_STATE(
139 this_state_machine, this_state_machine->initial_state_id
142 #if defined(SCI_BASE_ENABLE_SUBJECT_NOTIFICATION)
143 sci_base_subject_notify(&this_state_machine->parent);
146 SCI_STATE_MACHINE_ENTER_STATE(this_state_machine);
150 * @brief This method will cause the state machine to exit it's current
153 * @param[in] this_state_machine This parameter specifies the state machine
154 * that is to be stopped.
158 void sci_base_state_machine_stop(
159 SCI_BASE_STATE_MACHINE_T *this_state_machine
162 SCI_STATE_MACHINE_EXIT_STATE(this_state_machine);
164 #if defined(SCI_BASE_ENABLE_SUBJECT_NOTIFICATION)
165 sci_base_subject_notify(&this_state_machine->parent);
170 * @brief This method performs an update to the current state of
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
180 void sci_base_state_machine_change_state(
181 SCI_BASE_STATE_MACHINE_T *this_state_machine,
185 SCI_STATE_MACHINE_EXIT_STATE(this_state_machine);
187 this_state_machine->previous_state_id = this_state_machine->current_state_id;
188 SCI_STATE_MACHINE_SET_STATE(this_state_machine, next_state);
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);
195 SCI_STATE_MACHINE_ENTER_STATE(this_state_machine);
199 * @brief This method simply returns the current state of the
200 * state machine to the caller.
202 * @param[in] this_state_machine This parameter specifies the state
203 * machine for which to retrieve the current state.
205 * @return This method returns a U32 value indicating the current state for
206 * the supplied state machine.
208 U32 sci_base_state_machine_get_state(
209 SCI_BASE_STATE_MACHINE_T *this_state_machine
212 return this_state_machine->current_state_id;