blob: faeae9554ee39bc4f10a9d88176703abfc898b5a [file] [log] [blame]
Dan Williams6f231dd2011-07-02 22:56:22 -07001/*
2 * This file is provided under a dual BSD/GPLv2 license. When using or
3 * redistributing this file, you may do so under either license.
4 *
5 * GPL LICENSE SUMMARY
6 *
7 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21 * The full GNU General Public License is included in this distribution
22 * in the file called LICENSE.GPL.
23 *
24 * BSD LICENSE
25 *
26 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27 * All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
32 *
33 * * Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * * Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in
37 * the documentation and/or other materials provided with the
38 * distribution.
39 * * Neither the name of Intel Corporation nor the names of its
40 * contributors may be used to endorse or promote products derived
41 * from this software without specific prior written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
46 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
47 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 */
Jeff Skirvinc94fc1a2012-03-08 22:41:58 -080055#include <scsi/sas_ata.h>
Dan Williamscc9203b2011-05-08 17:34:44 -070056#include "host.h"
Edmund Nadolski12ef6542011-06-02 00:10:50 +000057#include "isci.h"
Dan Williams88f3b622011-04-22 19:18:03 -070058#include "remote_device.h"
59#include "remote_node_context.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070060#include "scu_event_codes.h"
61#include "scu_task_context.h"
62
Dan Williamsd7a0ccd2012-02-10 01:18:44 -080063#undef C
64#define C(a) (#a)
65const char *rnc_state_name(enum scis_sds_remote_node_context_states state)
66{
67 static const char * const strings[] = RNC_STATES;
Dan Williams6f231dd2011-07-02 22:56:22 -070068
Dan Williamsd7a0ccd2012-02-10 01:18:44 -080069 return strings[state];
70}
71#undef C
Dan Williams6f231dd2011-07-02 22:56:22 -070072
73/**
74 *
Dave Jiange2023b82011-04-21 05:34:49 +000075 * @sci_rnc: The state of the remote node context object to check.
Dan Williams6f231dd2011-07-02 22:56:22 -070076 *
77 * This method will return true if the remote node context is in a READY state
78 * otherwise it will return false bool true if the remote node context is in
79 * the ready state. false if the remote node context is not in the ready state.
80 */
Dan Williams89a73012011-06-30 19:14:33 -070081bool sci_remote_node_context_is_ready(
82 struct sci_remote_node_context *sci_rnc)
Dan Williams6f231dd2011-07-02 22:56:22 -070083{
Edmund Nadolskie3013702011-06-02 00:10:43 +000084 u32 current_state = sci_rnc->sm.current_state_id;
Dan Williams6f231dd2011-07-02 22:56:22 -070085
Edmund Nadolskie3013702011-06-02 00:10:43 +000086 if (current_state == SCI_RNC_READY) {
Dan Williams6f231dd2011-07-02 22:56:22 -070087 return true;
88 }
89
90 return false;
91}
92
Jeff Skirvin31a38ef2012-03-08 22:42:01 -080093bool sci_remote_node_context_is_suspended(struct sci_remote_node_context *sci_rnc)
94{
95 u32 current_state = sci_rnc->sm.current_state_id;
96
97 if (current_state == SCI_RNC_TX_RX_SUSPENDED)
98 return true;
99 return false;
100}
101
Dan Williams89a73012011-06-30 19:14:33 -0700102static union scu_remote_node_context *sci_rnc_by_id(struct isci_host *ihost, u16 id)
103{
104 if (id < ihost->remote_node_entries &&
105 ihost->device_table[id])
106 return &ihost->remote_node_context_table[id];
107
108 return NULL;
109}
110
111static void sci_remote_node_context_construct_buffer(struct sci_remote_node_context *sci_rnc)
Dan Williams6f231dd2011-07-02 22:56:22 -0700112{
Dan Williams78a6f062011-06-30 16:31:37 -0700113 struct isci_remote_device *idev = rnc_to_dev(sci_rnc);
114 struct domain_device *dev = idev->domain_dev;
Dan Williams1f4fa1f2011-04-26 11:44:06 -0700115 int rni = sci_rnc->remote_node_index;
Dan Williamsa1a113b2011-04-21 18:44:45 -0700116 union scu_remote_node_context *rnc;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700117 struct isci_host *ihost;
Dan Williamsa3d568f02011-04-26 09:41:52 -0700118 __le64 sas_addr;
Dan Williams6f231dd2011-07-02 22:56:22 -0700119
Dan Williams34a99152011-07-01 02:25:15 -0700120 ihost = idev->owning_port->owning_controller;
Dan Williams89a73012011-06-30 19:14:33 -0700121 rnc = sci_rnc_by_id(ihost, rni);
Dan Williams6f231dd2011-07-02 22:56:22 -0700122
Dan Williams96143952011-04-19 18:35:58 -0700123 memset(rnc, 0, sizeof(union scu_remote_node_context)
Dan Williams89a73012011-06-30 19:14:33 -0700124 * sci_remote_device_node_count(idev));
Dan Williams6f231dd2011-07-02 22:56:22 -0700125
Dan Williams1f4fa1f2011-04-26 11:44:06 -0700126 rnc->ssp.remote_node_index = rni;
Dan Williams78a6f062011-06-30 16:31:37 -0700127 rnc->ssp.remote_node_port_width = idev->device_port_width;
128 rnc->ssp.logical_port_index = idev->owning_port->physical_port_index;
Dan Williams6f231dd2011-07-02 22:56:22 -0700129
Dan Williamsa3d568f02011-04-26 09:41:52 -0700130 /* sas address is __be64, context ram format is __le64 */
131 sas_addr = cpu_to_le64(SAS_ADDR(dev->sas_addr));
132 rnc->ssp.remote_sas_address_hi = upper_32_bits(sas_addr);
133 rnc->ssp.remote_sas_address_lo = lower_32_bits(sas_addr);
Dan Williams6f231dd2011-07-02 22:56:22 -0700134
135 rnc->ssp.nexus_loss_timer_enable = true;
136 rnc->ssp.check_bit = false;
137 rnc->ssp.is_valid = false;
138 rnc->ssp.is_remote_node_context = true;
139 rnc->ssp.function_number = 0;
140
141 rnc->ssp.arbitration_wait_time = 0;
142
Dan Williams11cc51832012-02-01 00:23:10 -0800143 if (dev_is_sata(dev)) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700144 rnc->ssp.connection_occupancy_timeout =
Dan Williams89a73012011-06-30 19:14:33 -0700145 ihost->user_parameters.stp_max_occupancy_timeout;
Dan Williams6f231dd2011-07-02 22:56:22 -0700146 rnc->ssp.connection_inactivity_timeout =
Dan Williams89a73012011-06-30 19:14:33 -0700147 ihost->user_parameters.stp_inactivity_timeout;
Dan Williams6f231dd2011-07-02 22:56:22 -0700148 } else {
149 rnc->ssp.connection_occupancy_timeout =
Dan Williams89a73012011-06-30 19:14:33 -0700150 ihost->user_parameters.ssp_max_occupancy_timeout;
Dan Williams6f231dd2011-07-02 22:56:22 -0700151 rnc->ssp.connection_inactivity_timeout =
Dan Williams89a73012011-06-30 19:14:33 -0700152 ihost->user_parameters.ssp_inactivity_timeout;
Dan Williams6f231dd2011-07-02 22:56:22 -0700153 }
154
155 rnc->ssp.initial_arbitration_wait_time = 0;
156
157 /* Open Address Frame Parameters */
Dan Williams78a6f062011-06-30 16:31:37 -0700158 rnc->ssp.oaf_connection_rate = idev->connection_rate;
Dan Williams6f231dd2011-07-02 22:56:22 -0700159 rnc->ssp.oaf_features = 0;
160 rnc->ssp.oaf_source_zone_group = 0;
161 rnc->ssp.oaf_more_compatibility_features = 0;
162}
163
Jeff Skirvin0c3ce382012-03-08 22:42:02 -0800164static void sci_remote_node_context_save_cbparams(
165 struct sci_remote_node_context *sci_rnc,
166 scics_sds_remote_node_context_callback callback,
167 void *callback_parameter)
168{
169 sci_rnc->user_callback = callback;
170 sci_rnc->user_cookie = callback_parameter;
171}
Dan Williams6f231dd2011-07-02 22:56:22 -0700172/**
173 *
Dave Jiange2023b82011-04-21 05:34:49 +0000174 * @sci_rnc:
175 * @callback:
Dan Williams6f231dd2011-07-02 22:56:22 -0700176 * @callback_parameter:
177 *
178 * This method will setup the remote node context object so it will transition
179 * to its ready state. If the remote node context is already setup to
180 * transition to its final state then this function does nothing. none
181 */
Dan Williams89a73012011-06-30 19:14:33 -0700182static void sci_remote_node_context_setup_to_resume(
183 struct sci_remote_node_context *sci_rnc,
Dave Jiange2023b82011-04-21 05:34:49 +0000184 scics_sds_remote_node_context_callback callback,
Jeff Skirvin59e35392012-03-08 22:41:54 -0800185 void *callback_parameter,
186 enum sci_remote_node_context_destination_state dest_param)
Dan Williams6f231dd2011-07-02 22:56:22 -0700187{
Jeff Skirvin59e35392012-03-08 22:41:54 -0800188 if (sci_rnc->destination_state != RNC_DEST_FINAL) {
189 sci_rnc->destination_state = dest_param;
Jeff Skirvin0c3ce382012-03-08 22:42:02 -0800190 if (callback != NULL)
191 sci_remote_node_context_save_cbparams(
192 sci_rnc, callback, callback_parameter);
Dan Williams6f231dd2011-07-02 22:56:22 -0700193 }
194}
195
Jeff Skirvin59e35392012-03-08 22:41:54 -0800196static void sci_remote_node_context_setup_to_destroy(
Dan Williams89a73012011-06-30 19:14:33 -0700197 struct sci_remote_node_context *sci_rnc,
Dave Jiange2023b82011-04-21 05:34:49 +0000198 scics_sds_remote_node_context_callback callback,
Dan Williams6f231dd2011-07-02 22:56:22 -0700199 void *callback_parameter)
200{
Jeff Skirvin59e35392012-03-08 22:41:54 -0800201 sci_rnc->destination_state = RNC_DEST_FINAL;
Dave Jiange2023b82011-04-21 05:34:49 +0000202 sci_rnc->user_callback = callback;
203 sci_rnc->user_cookie = callback_parameter;
Dan Williams6f231dd2011-07-02 22:56:22 -0700204}
205
Dan Williams6f231dd2011-07-02 22:56:22 -0700206/**
207 *
208 *
209 * This method just calls the user callback function and then resets the
210 * callback.
211 */
Dan Williams89a73012011-06-30 19:14:33 -0700212static void sci_remote_node_context_notify_user(
213 struct sci_remote_node_context *rnc)
Dan Williams6f231dd2011-07-02 22:56:22 -0700214{
215 if (rnc->user_callback != NULL) {
216 (*rnc->user_callback)(rnc->user_cookie);
217
218 rnc->user_callback = NULL;
219 rnc->user_cookie = NULL;
220 }
221}
222
Dan Williams89a73012011-06-30 19:14:33 -0700223static void sci_remote_node_context_continue_state_transitions(struct sci_remote_node_context *rnc)
Dan Williams6f231dd2011-07-02 22:56:22 -0700224{
Jeff Skirvin59e35392012-03-08 22:41:54 -0800225 if ((rnc->destination_state == RNC_DEST_READY) ||
226 (rnc->destination_state == RNC_DEST_SUSPENDED_RESUME)) {
227 rnc->destination_state = RNC_DEST_READY;
Dan Williams89a73012011-06-30 19:14:33 -0700228 sci_remote_node_context_resume(rnc, rnc->user_callback,
Dan Williamsed3efb72011-05-12 08:50:23 -0700229 rnc->user_cookie);
Jeff Skirvin59e35392012-03-08 22:41:54 -0800230 } else
231 rnc->destination_state = RNC_DEST_UNSPECIFIED;
Dan Williams6f231dd2011-07-02 22:56:22 -0700232}
233
Dan Williams89a73012011-06-30 19:14:33 -0700234static void sci_remote_node_context_validate_context_buffer(struct sci_remote_node_context *sci_rnc)
Dan Williams6f231dd2011-07-02 22:56:22 -0700235{
Dan Williams89a73012011-06-30 19:14:33 -0700236 union scu_remote_node_context *rnc_buffer;
Dan Williams78a6f062011-06-30 16:31:37 -0700237 struct isci_remote_device *idev = rnc_to_dev(sci_rnc);
238 struct domain_device *dev = idev->domain_dev;
Dan Williams89a73012011-06-30 19:14:33 -0700239 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williams6f231dd2011-07-02 22:56:22 -0700240
Dan Williams89a73012011-06-30 19:14:33 -0700241 rnc_buffer = sci_rnc_by_id(ihost, sci_rnc->remote_node_index);
Dan Williams6f231dd2011-07-02 22:56:22 -0700242
243 rnc_buffer->ssp.is_valid = true;
244
Dan Williams11cc51832012-02-01 00:23:10 -0800245 if (dev_is_sata(dev) && dev->parent) {
Dan Williams89a73012011-06-30 19:14:33 -0700246 sci_remote_device_post_request(idev, SCU_CONTEXT_COMMAND_POST_RNC_96);
Dan Williams6f231dd2011-07-02 22:56:22 -0700247 } else {
Dan Williams89a73012011-06-30 19:14:33 -0700248 sci_remote_device_post_request(idev, SCU_CONTEXT_COMMAND_POST_RNC_32);
Dan Williams6f231dd2011-07-02 22:56:22 -0700249
Dan Williams11cc51832012-02-01 00:23:10 -0800250 if (!dev->parent)
Dan Williams89a73012011-06-30 19:14:33 -0700251 sci_port_setup_transports(idev->owning_port,
252 sci_rnc->remote_node_index);
Dan Williams6f231dd2011-07-02 22:56:22 -0700253 }
254}
255
Dan Williams89a73012011-06-30 19:14:33 -0700256static void sci_remote_node_context_invalidate_context_buffer(struct sci_remote_node_context *sci_rnc)
Dan Williams6f231dd2011-07-02 22:56:22 -0700257{
258 union scu_remote_node_context *rnc_buffer;
Dan Williams89a73012011-06-30 19:14:33 -0700259 struct isci_remote_device *idev = rnc_to_dev(sci_rnc);
260 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williams6f231dd2011-07-02 22:56:22 -0700261
Dan Williams89a73012011-06-30 19:14:33 -0700262 rnc_buffer = sci_rnc_by_id(ihost, sci_rnc->remote_node_index);
Dan Williams6f231dd2011-07-02 22:56:22 -0700263
264 rnc_buffer->ssp.is_valid = false;
265
Dan Williams89a73012011-06-30 19:14:33 -0700266 sci_remote_device_post_request(rnc_to_dev(sci_rnc),
267 SCU_CONTEXT_COMMAND_POST_RNC_INVALIDATE);
Dan Williams6f231dd2011-07-02 22:56:22 -0700268}
269
Dan Williams89a73012011-06-30 19:14:33 -0700270static void sci_remote_node_context_initial_state_enter(struct sci_base_state_machine *sm)
Dan Williams6f231dd2011-07-02 22:56:22 -0700271{
Dan Williams89a73012011-06-30 19:14:33 -0700272 struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
Dan Williams6f231dd2011-07-02 22:56:22 -0700273
Dan Williamsf34d9e52011-05-12 09:27:52 -0700274 /* Check to see if we have gotten back to the initial state because
275 * someone requested to destroy the remote node context object.
276 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000277 if (sm->previous_state_id == SCI_RNC_INVALIDATING) {
Jeff Skirvin59e35392012-03-08 22:41:54 -0800278 rnc->destination_state = RNC_DEST_UNSPECIFIED;
Dan Williams89a73012011-06-30 19:14:33 -0700279 sci_remote_node_context_notify_user(rnc);
Dan Williams6f231dd2011-07-02 22:56:22 -0700280 }
281}
282
Dan Williams89a73012011-06-30 19:14:33 -0700283static void sci_remote_node_context_posting_state_enter(struct sci_base_state_machine *sm)
Dan Williams6f231dd2011-07-02 22:56:22 -0700284{
Dan Williams89a73012011-06-30 19:14:33 -0700285 struct sci_remote_node_context *sci_rnc = container_of(sm, typeof(*sci_rnc), sm);
Dan Williams6f231dd2011-07-02 22:56:22 -0700286
Dan Williams89a73012011-06-30 19:14:33 -0700287 sci_remote_node_context_validate_context_buffer(sci_rnc);
Dan Williams6f231dd2011-07-02 22:56:22 -0700288}
289
Dan Williams89a73012011-06-30 19:14:33 -0700290static void sci_remote_node_context_invalidating_state_enter(struct sci_base_state_machine *sm)
Dan Williams6f231dd2011-07-02 22:56:22 -0700291{
Dan Williams89a73012011-06-30 19:14:33 -0700292 struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
Dan Williams6f231dd2011-07-02 22:56:22 -0700293
Jeff Skirvinaa20d932012-03-08 22:41:56 -0800294 /* Terminate all outstanding requests. */
295 sci_remote_device_terminate_requests(rnc_to_dev(rnc));
Dan Williams89a73012011-06-30 19:14:33 -0700296 sci_remote_node_context_invalidate_context_buffer(rnc);
Dan Williams6f231dd2011-07-02 22:56:22 -0700297}
298
Dan Williams89a73012011-06-30 19:14:33 -0700299static void sci_remote_node_context_resuming_state_enter(struct sci_base_state_machine *sm)
Dan Williams6f231dd2011-07-02 22:56:22 -0700300{
Dan Williams89a73012011-06-30 19:14:33 -0700301 struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
Dan Williams78a6f062011-06-30 16:31:37 -0700302 struct isci_remote_device *idev;
Dan Williamsa1a113b2011-04-21 18:44:45 -0700303 struct domain_device *dev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700304
Dan Williams78a6f062011-06-30 16:31:37 -0700305 idev = rnc_to_dev(rnc);
306 dev = idev->domain_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700307
Henryk Dembkowski24621462011-02-23 00:08:52 -0800308 /*
309 * For direct attached SATA devices we need to clear the TLCR
310 * NCQ to TCi tag mapping on the phy and in cases where we
311 * resume because of a target reset we also need to update
312 * the STPTLDARNI register with the RNi of the device
313 */
Dan Williams11cc51832012-02-01 00:23:10 -0800314 if (dev_is_sata(dev) && !dev->parent)
315 sci_port_setup_transports(idev->owning_port, rnc->remote_node_index);
Henryk Dembkowski24621462011-02-23 00:08:52 -0800316
Dan Williams89a73012011-06-30 19:14:33 -0700317 sci_remote_device_post_request(idev, SCU_CONTEXT_COMMAND_POST_RNC_RESUME);
Dan Williams6f231dd2011-07-02 22:56:22 -0700318}
319
Dan Williams89a73012011-06-30 19:14:33 -0700320static void sci_remote_node_context_ready_state_enter(struct sci_base_state_machine *sm)
Dan Williams6f231dd2011-07-02 22:56:22 -0700321{
Dan Williams89a73012011-06-30 19:14:33 -0700322 struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
Jeff Skirvin59e35392012-03-08 22:41:54 -0800323 enum sci_remote_node_context_destination_state dest_select;
324 scics_sds_remote_node_context_callback usr_cb = rnc->user_callback;
325 void *usr_param = rnc->user_cookie;
326 int tell_user = 1;
Dan Williams6f231dd2011-07-02 22:56:22 -0700327
Jeff Skirvin59e35392012-03-08 22:41:54 -0800328 dest_select = rnc->destination_state;
329 rnc->destination_state = RNC_DEST_UNSPECIFIED;
Dan Williams6f231dd2011-07-02 22:56:22 -0700330
Jeff Skirvin59e35392012-03-08 22:41:54 -0800331 if ((dest_select == RNC_DEST_SUSPENDED) ||
332 (dest_select == RNC_DEST_SUSPENDED_RESUME)) {
333 sci_remote_node_context_suspend(
Jeff Skirvin447bfbc2012-03-08 22:41:59 -0800334 rnc, rnc->suspend_reason,
335 SCI_SOFTWARE_SUSPEND_EXPECTED_EVENT);
Jeff Skirvin59e35392012-03-08 22:41:54 -0800336
337 if (dest_select == RNC_DEST_SUSPENDED_RESUME) {
338 sci_remote_node_context_resume(rnc, usr_cb, usr_param);
339 tell_user = 0; /* Wait until ready again. */
340 }
341 }
342 if (tell_user && rnc->user_callback)
Dan Williams89a73012011-06-30 19:14:33 -0700343 sci_remote_node_context_notify_user(rnc);
Dan Williams6f231dd2011-07-02 22:56:22 -0700344}
345
Dan Williams89a73012011-06-30 19:14:33 -0700346static void sci_remote_node_context_tx_suspended_state_enter(struct sci_base_state_machine *sm)
Dan Williams6f231dd2011-07-02 22:56:22 -0700347{
Dan Williams89a73012011-06-30 19:14:33 -0700348 struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
Dan Williams6f231dd2011-07-02 22:56:22 -0700349
Dan Williams89a73012011-06-30 19:14:33 -0700350 sci_remote_node_context_continue_state_transitions(rnc);
Dan Williams6f231dd2011-07-02 22:56:22 -0700351}
352
Dan Williams89a73012011-06-30 19:14:33 -0700353static void sci_remote_node_context_tx_rx_suspended_state_enter(struct sci_base_state_machine *sm)
Dan Williams6f231dd2011-07-02 22:56:22 -0700354{
Dan Williams89a73012011-06-30 19:14:33 -0700355 struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
Jeff Skirvin726980d2012-03-08 22:41:50 -0800356 struct isci_remote_device *idev = rnc_to_dev(rnc);
357 struct isci_host *ihost = idev->owning_port->owning_controller;
Jeff Skirvin31a38ef2012-03-08 22:42:01 -0800358 u32 new_count = rnc->suspend_count + 1;
359
360 if (new_count == 0)
361 rnc->suspend_count = 1;
362 else
363 rnc->suspend_count = new_count;
364 smp_wmb();
Dan Williams6f231dd2011-07-02 22:56:22 -0700365
Jeff Skirvin726980d2012-03-08 22:41:50 -0800366 /* Terminate outstanding requests pending abort. */
367 sci_remote_device_abort_requests_pending_abort(idev);
368
369 wake_up(&ihost->eventq);
Dan Williams89a73012011-06-30 19:14:33 -0700370 sci_remote_node_context_continue_state_transitions(rnc);
Dan Williams6f231dd2011-07-02 22:56:22 -0700371}
372
Jeff Skirvin6f488442012-03-08 22:41:48 -0800373static void sci_remote_node_context_await_suspend_state_exit(
374 struct sci_base_state_machine *sm)
375{
376 struct sci_remote_node_context *rnc
377 = container_of(sm, typeof(*rnc), sm);
Jeff Skirvinc94fc1a2012-03-08 22:41:58 -0800378 struct isci_remote_device *idev = rnc_to_dev(rnc);
Jeff Skirvin6f488442012-03-08 22:41:48 -0800379
Jeff Skirvinc94fc1a2012-03-08 22:41:58 -0800380 if (dev_is_sata(idev->domain_dev))
381 isci_dev_set_hang_detection_timeout(idev, 0);
Jeff Skirvin6f488442012-03-08 22:41:48 -0800382}
383
Dan Williams89a73012011-06-30 19:14:33 -0700384static const struct sci_base_state sci_remote_node_context_state_table[] = {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000385 [SCI_RNC_INITIAL] = {
Dan Williams89a73012011-06-30 19:14:33 -0700386 .enter_state = sci_remote_node_context_initial_state_enter,
Dan Williams6f231dd2011-07-02 22:56:22 -0700387 },
Edmund Nadolskie3013702011-06-02 00:10:43 +0000388 [SCI_RNC_POSTING] = {
Dan Williams89a73012011-06-30 19:14:33 -0700389 .enter_state = sci_remote_node_context_posting_state_enter,
Dan Williams6f231dd2011-07-02 22:56:22 -0700390 },
Edmund Nadolskie3013702011-06-02 00:10:43 +0000391 [SCI_RNC_INVALIDATING] = {
Dan Williams89a73012011-06-30 19:14:33 -0700392 .enter_state = sci_remote_node_context_invalidating_state_enter,
Dan Williams6f231dd2011-07-02 22:56:22 -0700393 },
Edmund Nadolskie3013702011-06-02 00:10:43 +0000394 [SCI_RNC_RESUMING] = {
Dan Williams89a73012011-06-30 19:14:33 -0700395 .enter_state = sci_remote_node_context_resuming_state_enter,
Dan Williams6f231dd2011-07-02 22:56:22 -0700396 },
Edmund Nadolskie3013702011-06-02 00:10:43 +0000397 [SCI_RNC_READY] = {
Dan Williams89a73012011-06-30 19:14:33 -0700398 .enter_state = sci_remote_node_context_ready_state_enter,
Dan Williams6f231dd2011-07-02 22:56:22 -0700399 },
Edmund Nadolskie3013702011-06-02 00:10:43 +0000400 [SCI_RNC_TX_SUSPENDED] = {
Dan Williams89a73012011-06-30 19:14:33 -0700401 .enter_state = sci_remote_node_context_tx_suspended_state_enter,
Dan Williams6f231dd2011-07-02 22:56:22 -0700402 },
Edmund Nadolskie3013702011-06-02 00:10:43 +0000403 [SCI_RNC_TX_RX_SUSPENDED] = {
Dan Williams89a73012011-06-30 19:14:33 -0700404 .enter_state = sci_remote_node_context_tx_rx_suspended_state_enter,
Dan Williams6f231dd2011-07-02 22:56:22 -0700405 },
Jeff Skirvin6f488442012-03-08 22:41:48 -0800406 [SCI_RNC_AWAIT_SUSPENSION] = {
407 .exit_state = sci_remote_node_context_await_suspend_state_exit,
408 },
Dan Williams6f231dd2011-07-02 22:56:22 -0700409};
410
Dan Williams89a73012011-06-30 19:14:33 -0700411void sci_remote_node_context_construct(struct sci_remote_node_context *rnc,
Dan Williams96143952011-04-19 18:35:58 -0700412 u16 remote_node_index)
Dan Williams35173d52011-03-26 16:43:01 -0700413{
Dan Williams89a73012011-06-30 19:14:33 -0700414 memset(rnc, 0, sizeof(struct sci_remote_node_context));
Dan Williams35173d52011-03-26 16:43:01 -0700415
416 rnc->remote_node_index = remote_node_index;
Jeff Skirvin59e35392012-03-08 22:41:54 -0800417 rnc->destination_state = RNC_DEST_UNSPECIFIED;
Dan Williams35173d52011-03-26 16:43:01 -0700418
Dan Williams89a73012011-06-30 19:14:33 -0700419 sci_init_sm(&rnc->sm, sci_remote_node_context_state_table, SCI_RNC_INITIAL);
Dan Williams35173d52011-03-26 16:43:01 -0700420}
Dan Williams338e3862011-05-12 07:46:59 -0700421
Dan Williams89a73012011-06-30 19:14:33 -0700422enum sci_status sci_remote_node_context_event_handler(struct sci_remote_node_context *sci_rnc,
Dan Williams338e3862011-05-12 07:46:59 -0700423 u32 event_code)
424{
425 enum scis_sds_remote_node_context_states state;
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800426 u32 next_state;
Dan Williams338e3862011-05-12 07:46:59 -0700427
Edmund Nadolskie3013702011-06-02 00:10:43 +0000428 state = sci_rnc->sm.current_state_id;
Dan Williams338e3862011-05-12 07:46:59 -0700429 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000430 case SCI_RNC_POSTING:
Dan Williams338e3862011-05-12 07:46:59 -0700431 switch (scu_get_event_code(event_code)) {
432 case SCU_EVENT_POST_RNC_COMPLETE:
Edmund Nadolskie3013702011-06-02 00:10:43 +0000433 sci_change_state(&sci_rnc->sm, SCI_RNC_READY);
Dan Williams338e3862011-05-12 07:46:59 -0700434 break;
435 default:
436 goto out;
437 }
438 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000439 case SCI_RNC_INVALIDATING:
Dan Williams338e3862011-05-12 07:46:59 -0700440 if (scu_get_event_code(event_code) == SCU_EVENT_POST_RNC_INVALIDATE_COMPLETE) {
Jeff Skirvin59e35392012-03-08 22:41:54 -0800441 if (sci_rnc->destination_state == RNC_DEST_FINAL)
442 next_state = SCI_RNC_INITIAL;
Dan Williams338e3862011-05-12 07:46:59 -0700443 else
Jeff Skirvin59e35392012-03-08 22:41:54 -0800444 next_state = SCI_RNC_POSTING;
445 sci_change_state(&sci_rnc->sm, next_state);
Dan Williams338e3862011-05-12 07:46:59 -0700446 } else {
447 switch (scu_get_event_type(event_code)) {
448 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
449 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
450 /* We really dont care if the hardware is going to suspend
451 * the device since it's being invalidated anyway */
452 dev_dbg(scirdev_to_dev(rnc_to_dev(sci_rnc)),
453 "%s: SCIC Remote Node Context 0x%p was "
454 "suspeneded by hardware while being "
455 "invalidated.\n", __func__, sci_rnc);
456 break;
457 default:
458 goto out;
459 }
460 }
461 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000462 case SCI_RNC_RESUMING:
Dan Williams338e3862011-05-12 07:46:59 -0700463 if (scu_get_event_code(event_code) == SCU_EVENT_POST_RCN_RELEASE) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000464 sci_change_state(&sci_rnc->sm, SCI_RNC_READY);
Dan Williams338e3862011-05-12 07:46:59 -0700465 } else {
466 switch (scu_get_event_type(event_code)) {
467 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
468 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
469 /* We really dont care if the hardware is going to suspend
470 * the device since it's being resumed anyway */
471 dev_dbg(scirdev_to_dev(rnc_to_dev(sci_rnc)),
472 "%s: SCIC Remote Node Context 0x%p was "
473 "suspeneded by hardware while being resumed.\n",
474 __func__, sci_rnc);
475 break;
476 default:
477 goto out;
478 }
479 }
480 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000481 case SCI_RNC_READY:
Dan Williams338e3862011-05-12 07:46:59 -0700482 switch (scu_get_event_type(event_code)) {
483 case SCU_EVENT_TL_RNC_SUSPEND_TX:
Edmund Nadolskie3013702011-06-02 00:10:43 +0000484 sci_change_state(&sci_rnc->sm, SCI_RNC_TX_SUSPENDED);
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800485 sci_rnc->suspend_type = scu_get_event_type(event_code);
Dan Williams338e3862011-05-12 07:46:59 -0700486 break;
487 case SCU_EVENT_TL_RNC_SUSPEND_TX_RX:
Edmund Nadolskie3013702011-06-02 00:10:43 +0000488 sci_change_state(&sci_rnc->sm, SCI_RNC_TX_RX_SUSPENDED);
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800489 sci_rnc->suspend_type = scu_get_event_type(event_code);
Dan Williams338e3862011-05-12 07:46:59 -0700490 break;
491 default:
492 goto out;
493 }
494 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000495 case SCI_RNC_AWAIT_SUSPENSION:
Dan Williams338e3862011-05-12 07:46:59 -0700496 switch (scu_get_event_type(event_code)) {
497 case SCU_EVENT_TL_RNC_SUSPEND_TX:
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800498 next_state = SCI_RNC_TX_SUSPENDED;
Dan Williams338e3862011-05-12 07:46:59 -0700499 break;
500 case SCU_EVENT_TL_RNC_SUSPEND_TX_RX:
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800501 next_state = SCI_RNC_TX_RX_SUSPENDED;
Dan Williams338e3862011-05-12 07:46:59 -0700502 break;
503 default:
504 goto out;
505 }
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800506 if (sci_rnc->suspend_type == scu_get_event_type(event_code))
507 sci_change_state(&sci_rnc->sm, next_state);
Dan Williams338e3862011-05-12 07:46:59 -0700508 break;
509 default:
510 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
Dan Williams14e99b42012-02-10 01:05:43 -0800511 "%s: invalid state: %s\n", __func__,
512 rnc_state_name(state));
Dan Williams338e3862011-05-12 07:46:59 -0700513 return SCI_FAILURE_INVALID_STATE;
514 }
515 return SCI_SUCCESS;
516
517 out:
518 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
Dan Williams14e99b42012-02-10 01:05:43 -0800519 "%s: code: %#x state: %s\n", __func__, event_code,
520 rnc_state_name(state));
Dan Williams338e3862011-05-12 07:46:59 -0700521 return SCI_FAILURE;
522
523}
Dan Williamsc845ae92011-05-12 08:26:56 -0700524
Dan Williams89a73012011-06-30 19:14:33 -0700525enum sci_status sci_remote_node_context_destruct(struct sci_remote_node_context *sci_rnc,
Dan Williamsc845ae92011-05-12 08:26:56 -0700526 scics_sds_remote_node_context_callback cb_fn,
527 void *cb_p)
528{
529 enum scis_sds_remote_node_context_states state;
530
Edmund Nadolskie3013702011-06-02 00:10:43 +0000531 state = sci_rnc->sm.current_state_id;
Dan Williamsc845ae92011-05-12 08:26:56 -0700532 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000533 case SCI_RNC_INVALIDATING:
Jeff Skirvin59e35392012-03-08 22:41:54 -0800534 sci_remote_node_context_setup_to_destroy(sci_rnc, cb_fn, cb_p);
Dan Williamsc845ae92011-05-12 08:26:56 -0700535 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000536 case SCI_RNC_POSTING:
537 case SCI_RNC_RESUMING:
538 case SCI_RNC_READY:
539 case SCI_RNC_TX_SUSPENDED:
540 case SCI_RNC_TX_RX_SUSPENDED:
541 case SCI_RNC_AWAIT_SUSPENSION:
Jeff Skirvin59e35392012-03-08 22:41:54 -0800542 sci_remote_node_context_setup_to_destroy(sci_rnc, cb_fn, cb_p);
Edmund Nadolskie3013702011-06-02 00:10:43 +0000543 sci_change_state(&sci_rnc->sm, SCI_RNC_INVALIDATING);
Dan Williamsc845ae92011-05-12 08:26:56 -0700544 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000545 case SCI_RNC_INITIAL:
Dan Williamsc845ae92011-05-12 08:26:56 -0700546 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
Dan Williams14e99b42012-02-10 01:05:43 -0800547 "%s: invalid state: %s\n", __func__,
548 rnc_state_name(state));
Dan Williamsc845ae92011-05-12 08:26:56 -0700549 /* We have decided that the destruct request on the remote node context
550 * can not fail since it is either in the initial/destroyed state or is
551 * can be destroyed.
552 */
553 return SCI_SUCCESS;
554 default:
555 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
Dan Williams14e99b42012-02-10 01:05:43 -0800556 "%s: invalid state %s\n", __func__,
557 rnc_state_name(state));
Dan Williamsc845ae92011-05-12 08:26:56 -0700558 return SCI_FAILURE_INVALID_STATE;
559 }
560}
Dan Williamsed3efb72011-05-12 08:50:23 -0700561
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800562enum sci_status sci_remote_node_context_suspend(
563 struct sci_remote_node_context *sci_rnc,
564 enum sci_remote_node_suspension_reasons suspend_reason,
Jeff Skirvin447bfbc2012-03-08 22:41:59 -0800565 u32 suspend_type)
Dan Williamsed3efb72011-05-12 08:50:23 -0700566{
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800567 enum scis_sds_remote_node_context_states state
568 = sci_rnc->sm.current_state_id;
569 struct isci_remote_device *idev = rnc_to_dev(sci_rnc);
570 enum sci_status status = SCI_FAILURE_INVALID_STATE;
Jeff Skirvin59e35392012-03-08 22:41:54 -0800571 enum sci_remote_node_context_destination_state dest_param =
572 RNC_DEST_UNSPECIFIED;
Dan Williamsed3efb72011-05-12 08:50:23 -0700573
Jeff Skirvin726980d2012-03-08 22:41:50 -0800574 dev_dbg(scirdev_to_dev(idev),
575 "%s: current state %d, current suspend_type %x dest state %d,"
576 " arg suspend_reason %d, arg suspend_type %x",
577 __func__, state, sci_rnc->suspend_type,
578 sci_rnc->destination_state, suspend_reason,
579 suspend_type);
580
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800581 /* Disable automatic state continuations if explicitly suspending. */
Jeff Skirvinc94fc1a2012-03-08 22:41:58 -0800582 if ((suspend_reason == SCI_HW_SUSPEND) ||
Jeff Skirvin59e35392012-03-08 22:41:54 -0800583 (sci_rnc->destination_state == RNC_DEST_FINAL))
584 dest_param = sci_rnc->destination_state;
585
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800586 switch (state) {
Jeff Skirvin59e35392012-03-08 22:41:54 -0800587 case SCI_RNC_RESUMING:
588 break; /* The RNC has been posted, so start the suspend. */
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800589 case SCI_RNC_READY:
590 break;
Jeff Skirvin59e35392012-03-08 22:41:54 -0800591 case SCI_RNC_INVALIDATING:
592 if (sci_rnc->destination_state == RNC_DEST_FINAL) {
593 dev_warn(scirdev_to_dev(idev),
594 "%s: already destroying %p\n",
595 __func__, sci_rnc);
596 return SCI_FAILURE_INVALID_STATE;
597 }
598 /* Fall through and handle like SCI_RNC_POSTING */
599 case SCI_RNC_POSTING:
600 /* Set the destination state to AWAIT - this signals the
601 * entry into the SCI_RNC_READY state that a suspension
602 * needs to be done immediately.
603 */
604 sci_rnc->destination_state = RNC_DEST_SUSPENDED;
Jeff Skirvin447bfbc2012-03-08 22:41:59 -0800605 sci_rnc->suspend_type = suspend_type;
606 sci_rnc->suspend_reason = suspend_reason;
Jeff Skirvin59e35392012-03-08 22:41:54 -0800607 return SCI_SUCCESS;
608
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800609 case SCI_RNC_TX_SUSPENDED:
610 if (suspend_type == SCU_EVENT_TL_RNC_SUSPEND_TX)
611 status = SCI_SUCCESS;
612 break;
613 case SCI_RNC_TX_RX_SUSPENDED:
614 if (suspend_type == SCU_EVENT_TL_RNC_SUSPEND_TX_RX)
615 status = SCI_SUCCESS;
616 break;
617 case SCI_RNC_AWAIT_SUSPENSION:
618 if ((sci_rnc->suspend_type == SCU_EVENT_TL_RNC_SUSPEND_TX_RX)
619 || (suspend_type == sci_rnc->suspend_type))
620 return SCI_SUCCESS;
621 break;
622 default:
Dan Williamsed3efb72011-05-12 08:50:23 -0700623 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
Dan Williams14e99b42012-02-10 01:05:43 -0800624 "%s: invalid state %s\n", __func__,
625 rnc_state_name(state));
Dan Williamsed3efb72011-05-12 08:50:23 -0700626 return SCI_FAILURE_INVALID_STATE;
627 }
Jeff Skirvin59e35392012-03-08 22:41:54 -0800628 sci_rnc->destination_state = dest_param;
Jeff Skirvin447bfbc2012-03-08 22:41:59 -0800629 sci_rnc->suspend_type = suspend_type;
630 sci_rnc->suspend_reason = suspend_reason;
Dan Williamsed3efb72011-05-12 08:50:23 -0700631
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800632 if (status == SCI_SUCCESS) { /* Already in the destination state? */
Jeff Skirvin726980d2012-03-08 22:41:50 -0800633 struct isci_host *ihost = idev->owning_port->owning_controller;
634
Jeff Skirvin726980d2012-03-08 22:41:50 -0800635 wake_up_all(&ihost->eventq); /* Let observers look. */
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800636 return SCI_SUCCESS;
Dan Williamsed3efb72011-05-12 08:50:23 -0700637 }
Jeff Skirvinc94fc1a2012-03-08 22:41:58 -0800638 if ((suspend_reason == SCI_SW_SUSPEND_NORMAL) ||
639 (suspend_reason == SCI_SW_SUSPEND_LINKHANG_DETECT)) {
640
Jeff Skirvin9608b642012-03-08 22:42:00 -0800641 if (suspend_reason == SCI_SW_SUSPEND_LINKHANG_DETECT)
Jeff Skirvinc94fc1a2012-03-08 22:41:58 -0800642 isci_dev_set_hang_detection_timeout(idev, 0x00000001);
643
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800644 sci_remote_device_post_request(
645 idev, SCI_SOFTWARE_SUSPEND_CMD);
646 }
647 if (state != SCI_RNC_AWAIT_SUSPENSION)
648 sci_change_state(&sci_rnc->sm, SCI_RNC_AWAIT_SUSPENSION);
Dan Williamsed3efb72011-05-12 08:50:23 -0700649
Dan Williamsed3efb72011-05-12 08:50:23 -0700650 return SCI_SUCCESS;
651}
652
Dan Williams89a73012011-06-30 19:14:33 -0700653enum sci_status sci_remote_node_context_resume(struct sci_remote_node_context *sci_rnc,
Dan Williamsed3efb72011-05-12 08:50:23 -0700654 scics_sds_remote_node_context_callback cb_fn,
655 void *cb_p)
656{
657 enum scis_sds_remote_node_context_states state;
Jeff Skirvin0c3ce382012-03-08 22:42:02 -0800658 struct isci_remote_device *idev = rnc_to_dev(sci_rnc);
Dan Williamsed3efb72011-05-12 08:50:23 -0700659
Edmund Nadolskie3013702011-06-02 00:10:43 +0000660 state = sci_rnc->sm.current_state_id;
Jeff Skirvin0c3ce382012-03-08 22:42:02 -0800661 dev_dbg(scirdev_to_dev(idev),
662 "%s: state %s, cb_fn = %p, cb_p = %p; dest_state = %d; "
663 "dev resume path %s\n",
664 __func__, rnc_state_name(state), cb_fn, cb_p,
665 sci_rnc->destination_state,
666 test_bit(IDEV_ABORT_PATH_ACTIVE, &idev->flags)
667 ? "<abort active>" : "<normal>");
Jeff Skirvin31a38ef2012-03-08 22:42:01 -0800668
Dan Williamsed3efb72011-05-12 08:50:23 -0700669 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000670 case SCI_RNC_INITIAL:
Dan Williamsed3efb72011-05-12 08:50:23 -0700671 if (sci_rnc->remote_node_index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX)
672 return SCI_FAILURE_INVALID_STATE;
673
Jeff Skirvin0c3ce382012-03-08 22:42:02 -0800674 if (test_bit(IDEV_ABORT_PATH_ACTIVE, &idev->flags))
675 sci_remote_node_context_save_cbparams(sci_rnc, cb_fn,
676 cb_p);
677 else {
678 sci_remote_node_context_setup_to_resume(sci_rnc, cb_fn,
679 cb_p, RNC_DEST_READY);
680 sci_remote_node_context_construct_buffer(sci_rnc);
681 sci_change_state(&sci_rnc->sm, SCI_RNC_POSTING);
682 }
Dan Williamsed3efb72011-05-12 08:50:23 -0700683 return SCI_SUCCESS;
Jeff Skirvin0c3ce382012-03-08 22:42:02 -0800684
Edmund Nadolskie3013702011-06-02 00:10:43 +0000685 case SCI_RNC_POSTING:
686 case SCI_RNC_INVALIDATING:
687 case SCI_RNC_RESUMING:
Jeff Skirvin0c3ce382012-03-08 22:42:02 -0800688 if (test_bit(IDEV_ABORT_PATH_ACTIVE, &idev->flags))
689 sci_remote_node_context_save_cbparams(sci_rnc, cb_fn,
690 cb_p);
691 else {
692 /* We are still waiting to post when a resume was
693 * requested.
Jeff Skirvin59e35392012-03-08 22:41:54 -0800694 */
Jeff Skirvin0c3ce382012-03-08 22:42:02 -0800695 switch (sci_rnc->destination_state) {
696 case RNC_DEST_SUSPENDED:
697 case RNC_DEST_SUSPENDED_RESUME:
698 /* Previously waiting to suspend after posting.
699 * Now continue onto resumption.
700 */
701 sci_remote_node_context_setup_to_resume(
702 sci_rnc, cb_fn, cb_p,
703 RNC_DEST_SUSPENDED_RESUME);
704 break;
705 default:
706 sci_remote_node_context_setup_to_resume(
707 sci_rnc, cb_fn, cb_p,
708 RNC_DEST_READY);
709 break;
710 }
Jeff Skirvin59e35392012-03-08 22:41:54 -0800711 }
Dan Williamsed3efb72011-05-12 08:50:23 -0700712 return SCI_SUCCESS;
Jeff Skirvin0c3ce382012-03-08 22:42:02 -0800713
Jeff Skirvin56d7c012012-03-08 22:41:49 -0800714 case SCI_RNC_TX_SUSPENDED:
Jeff Skirvin0c3ce382012-03-08 22:42:02 -0800715 case SCI_RNC_TX_RX_SUSPENDED:
716 if (test_bit(IDEV_ABORT_PATH_ACTIVE, &idev->flags))
717 sci_remote_node_context_save_cbparams(sci_rnc, cb_fn,
718 cb_p);
719 else {
720 struct domain_device *dev = idev->domain_dev;
721 /* If this is an expander attached SATA device we must
722 * invalidate and repost the RNC since this is the only
723 * way to clear the TCi to NCQ tag mapping table for
724 * the RNi. All other device types we can just resume.
725 */
726 sci_remote_node_context_setup_to_resume(
727 sci_rnc, cb_fn, cb_p, RNC_DEST_READY);
Dan Williamsed3efb72011-05-12 08:50:23 -0700728
Jeff Skirvin0c3ce382012-03-08 22:42:02 -0800729 if (dev_is_sata(dev) && dev->parent)
730 sci_change_state(&sci_rnc->sm,
731 SCI_RNC_INVALIDATING);
732 else
733 sci_change_state(&sci_rnc->sm,
734 SCI_RNC_RESUMING);
735 }
Dan Williamsed3efb72011-05-12 08:50:23 -0700736 return SCI_SUCCESS;
Jeff Skirvin0c3ce382012-03-08 22:42:02 -0800737
Edmund Nadolskie3013702011-06-02 00:10:43 +0000738 case SCI_RNC_AWAIT_SUSPENSION:
Jeff Skirvin0c3ce382012-03-08 22:42:02 -0800739 if (test_bit(IDEV_ABORT_PATH_ACTIVE, &idev->flags))
740 sci_remote_node_context_save_cbparams(sci_rnc, cb_fn,
741 cb_p);
742 else
743 sci_remote_node_context_setup_to_resume(
744 sci_rnc, cb_fn, cb_p,
745 RNC_DEST_SUSPENDED_RESUME);
Dan Williamsed3efb72011-05-12 08:50:23 -0700746 return SCI_SUCCESS;
747 default:
748 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
Dan Williams14e99b42012-02-10 01:05:43 -0800749 "%s: invalid state %s\n", __func__,
750 rnc_state_name(state));
Dan Williamsed3efb72011-05-12 08:50:23 -0700751 return SCI_FAILURE_INVALID_STATE;
752 }
753}
Dan Williamsf34d9e52011-05-12 09:27:52 -0700754
Dan Williams89a73012011-06-30 19:14:33 -0700755enum sci_status sci_remote_node_context_start_io(struct sci_remote_node_context *sci_rnc,
Dan Williams5076a1a2011-06-27 14:57:03 -0700756 struct isci_request *ireq)
Dan Williamsf34d9e52011-05-12 09:27:52 -0700757{
758 enum scis_sds_remote_node_context_states state;
759
Edmund Nadolskie3013702011-06-02 00:10:43 +0000760 state = sci_rnc->sm.current_state_id;
Jeff Skirvinfd536602011-06-20 14:09:22 -0700761
762 switch (state) {
763 case SCI_RNC_READY:
764 return SCI_SUCCESS;
765 case SCI_RNC_TX_SUSPENDED:
766 case SCI_RNC_TX_RX_SUSPENDED:
767 case SCI_RNC_AWAIT_SUSPENSION:
Dan Williamsf34d9e52011-05-12 09:27:52 -0700768 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
Dan Williams14e99b42012-02-10 01:05:43 -0800769 "%s: invalid state %s\n", __func__,
770 rnc_state_name(state));
Dan Williamsf34d9e52011-05-12 09:27:52 -0700771 return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
Jeff Skirvinfd536602011-06-20 14:09:22 -0700772 default:
Dan Williams14e99b42012-02-10 01:05:43 -0800773 dev_dbg(scirdev_to_dev(rnc_to_dev(sci_rnc)),
774 "%s: invalid state %s\n", __func__,
775 rnc_state_name(state));
776 return SCI_FAILURE_INVALID_STATE;
Dan Williamsf34d9e52011-05-12 09:27:52 -0700777 }
Dan Williamsf34d9e52011-05-12 09:27:52 -0700778}
779
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -0800780enum sci_status sci_remote_node_context_start_task(
781 struct sci_remote_node_context *sci_rnc,
782 struct isci_request *ireq,
783 scics_sds_remote_node_context_callback cb_fn,
784 void *cb_p)
Dan Williamsf34d9e52011-05-12 09:27:52 -0700785{
Jeff Skirvin59e35392012-03-08 22:41:54 -0800786 enum sci_status status = sci_remote_node_context_resume(sci_rnc,
787 cb_fn, cb_p);
788 if (status != SCI_SUCCESS)
Dan Williamsf34d9e52011-05-12 09:27:52 -0700789 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
Jeff Skirvin59e35392012-03-08 22:41:54 -0800790 "%s: resume failed: %d\n", __func__, status);
791 return status;
Dan Williamsf34d9e52011-05-12 09:27:52 -0700792}
Jeff Skirvin726980d2012-03-08 22:41:50 -0800793
794int sci_remote_node_context_is_safe_to_abort(
795 struct sci_remote_node_context *sci_rnc)
796{
797 enum scis_sds_remote_node_context_states state;
798
799 state = sci_rnc->sm.current_state_id;
800 switch (state) {
801 case SCI_RNC_INVALIDATING:
802 case SCI_RNC_TX_RX_SUSPENDED:
803 return 1;
804 case SCI_RNC_POSTING:
805 case SCI_RNC_RESUMING:
806 case SCI_RNC_READY:
807 case SCI_RNC_TX_SUSPENDED:
808 case SCI_RNC_AWAIT_SUSPENSION:
809 case SCI_RNC_INITIAL:
810 return 0;
811 default:
812 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
813 "%s: invalid state %d\n", __func__, state);
814 return 0;
815 }
816}