blob: 1910100638a280a5c70e1f2118e354d6ecfb1a43 [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 Williams11cc5182012-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}
Dan Williams6f231dd2011-07-02 22:56:22 -0700163/**
164 *
Dave Jiange2023b82011-04-21 05:34:49 +0000165 * @sci_rnc:
166 * @callback:
Dan Williams6f231dd2011-07-02 22:56:22 -0700167 * @callback_parameter:
168 *
169 * This method will setup the remote node context object so it will transition
170 * to its ready state. If the remote node context is already setup to
171 * transition to its final state then this function does nothing. none
172 */
Dan Williams89a73012011-06-30 19:14:33 -0700173static void sci_remote_node_context_setup_to_resume(
174 struct sci_remote_node_context *sci_rnc,
Dave Jiange2023b82011-04-21 05:34:49 +0000175 scics_sds_remote_node_context_callback callback,
Jeff Skirvin59e35392012-03-08 22:41:54 -0800176 void *callback_parameter,
177 enum sci_remote_node_context_destination_state dest_param)
Dan Williams6f231dd2011-07-02 22:56:22 -0700178{
Jeff Skirvin59e35392012-03-08 22:41:54 -0800179 if (sci_rnc->destination_state != RNC_DEST_FINAL) {
180 sci_rnc->destination_state = dest_param;
Jeff Skirvin6c6aacb2012-03-13 17:03:00 -0700181 if (callback != NULL) {
182 sci_rnc->user_callback = callback;
183 sci_rnc->user_cookie = callback_parameter;
184 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700185 }
186}
187
Jeff Skirvin59e35392012-03-08 22:41:54 -0800188static void sci_remote_node_context_setup_to_destroy(
Dan Williams89a73012011-06-30 19:14:33 -0700189 struct sci_remote_node_context *sci_rnc,
Dave Jiange2023b82011-04-21 05:34:49 +0000190 scics_sds_remote_node_context_callback callback,
Dan Williams6f231dd2011-07-02 22:56:22 -0700191 void *callback_parameter)
192{
Jeff Skirvinde2eb4d2012-03-13 17:15:11 -0700193 struct isci_host *ihost = idev_to_ihost(rnc_to_dev(sci_rnc));
194
Jeff Skirvin59e35392012-03-08 22:41:54 -0800195 sci_rnc->destination_state = RNC_DEST_FINAL;
Dave Jiange2023b82011-04-21 05:34:49 +0000196 sci_rnc->user_callback = callback;
197 sci_rnc->user_cookie = callback_parameter;
Jeff Skirvinde2eb4d2012-03-13 17:15:11 -0700198
199 wake_up(&ihost->eventq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700200}
201
Dan Williams6f231dd2011-07-02 22:56:22 -0700202/**
203 *
204 *
205 * This method just calls the user callback function and then resets the
206 * callback.
207 */
Dan Williams89a73012011-06-30 19:14:33 -0700208static void sci_remote_node_context_notify_user(
209 struct sci_remote_node_context *rnc)
Dan Williams6f231dd2011-07-02 22:56:22 -0700210{
211 if (rnc->user_callback != NULL) {
212 (*rnc->user_callback)(rnc->user_cookie);
213
214 rnc->user_callback = NULL;
215 rnc->user_cookie = NULL;
216 }
217}
218
Dan Williams89a73012011-06-30 19:14:33 -0700219static void sci_remote_node_context_continue_state_transitions(struct sci_remote_node_context *rnc)
Dan Williams6f231dd2011-07-02 22:56:22 -0700220{
Jeff Skirvin79cbab82012-03-13 16:36:35 -0700221 switch (rnc->destination_state) {
222 case RNC_DEST_READY:
223 case RNC_DEST_SUSPENDED_RESUME:
Jeff Skirvin59e35392012-03-08 22:41:54 -0800224 rnc->destination_state = RNC_DEST_READY;
Jeff Skirvin79cbab82012-03-13 16:36:35 -0700225 /* Fall through... */
226 case RNC_DEST_FINAL:
Dan Williams89a73012011-06-30 19:14:33 -0700227 sci_remote_node_context_resume(rnc, rnc->user_callback,
Jeff Skirvin79cbab82012-03-13 16:36:35 -0700228 rnc->user_cookie);
229 break;
230 default:
Jeff Skirvin59e35392012-03-08 22:41:54 -0800231 rnc->destination_state = RNC_DEST_UNSPECIFIED;
Jeff Skirvin79cbab82012-03-13 16:36:35 -0700232 break;
233 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700234}
235
Dan Williams89a73012011-06-30 19:14:33 -0700236static void sci_remote_node_context_validate_context_buffer(struct sci_remote_node_context *sci_rnc)
Dan Williams6f231dd2011-07-02 22:56:22 -0700237{
Dan Williams89a73012011-06-30 19:14:33 -0700238 union scu_remote_node_context *rnc_buffer;
Dan Williams78a6f062011-06-30 16:31:37 -0700239 struct isci_remote_device *idev = rnc_to_dev(sci_rnc);
240 struct domain_device *dev = idev->domain_dev;
Dan Williams89a73012011-06-30 19:14:33 -0700241 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williams6f231dd2011-07-02 22:56:22 -0700242
Dan Williams89a73012011-06-30 19:14:33 -0700243 rnc_buffer = sci_rnc_by_id(ihost, sci_rnc->remote_node_index);
Dan Williams6f231dd2011-07-02 22:56:22 -0700244
245 rnc_buffer->ssp.is_valid = true;
246
Dan Williams11cc5182012-02-01 00:23:10 -0800247 if (dev_is_sata(dev) && dev->parent) {
Dan Williams89a73012011-06-30 19:14:33 -0700248 sci_remote_device_post_request(idev, SCU_CONTEXT_COMMAND_POST_RNC_96);
Dan Williams6f231dd2011-07-02 22:56:22 -0700249 } else {
Dan Williams89a73012011-06-30 19:14:33 -0700250 sci_remote_device_post_request(idev, SCU_CONTEXT_COMMAND_POST_RNC_32);
Dan Williams6f231dd2011-07-02 22:56:22 -0700251
Dan Williams11cc5182012-02-01 00:23:10 -0800252 if (!dev->parent)
Dan Williams89a73012011-06-30 19:14:33 -0700253 sci_port_setup_transports(idev->owning_port,
254 sci_rnc->remote_node_index);
Dan Williams6f231dd2011-07-02 22:56:22 -0700255 }
256}
257
Dan Williams89a73012011-06-30 19:14:33 -0700258static void sci_remote_node_context_invalidate_context_buffer(struct sci_remote_node_context *sci_rnc)
Dan Williams6f231dd2011-07-02 22:56:22 -0700259{
260 union scu_remote_node_context *rnc_buffer;
Dan Williams89a73012011-06-30 19:14:33 -0700261 struct isci_remote_device *idev = rnc_to_dev(sci_rnc);
262 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williams6f231dd2011-07-02 22:56:22 -0700263
Dan Williams89a73012011-06-30 19:14:33 -0700264 rnc_buffer = sci_rnc_by_id(ihost, sci_rnc->remote_node_index);
Dan Williams6f231dd2011-07-02 22:56:22 -0700265
266 rnc_buffer->ssp.is_valid = false;
267
Dan Williams89a73012011-06-30 19:14:33 -0700268 sci_remote_device_post_request(rnc_to_dev(sci_rnc),
269 SCU_CONTEXT_COMMAND_POST_RNC_INVALIDATE);
Dan Williams6f231dd2011-07-02 22:56:22 -0700270}
271
Dan Williams89a73012011-06-30 19:14:33 -0700272static void sci_remote_node_context_initial_state_enter(struct sci_base_state_machine *sm)
Dan Williams6f231dd2011-07-02 22:56:22 -0700273{
Dan Williams89a73012011-06-30 19:14:33 -0700274 struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
Jeff Skirvin1f053882012-03-08 22:42:08 -0800275 struct isci_remote_device *idev = rnc_to_dev(rnc);
276 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williams6f231dd2011-07-02 22:56:22 -0700277
Dan Williamsf34d9e52011-05-12 09:27:52 -0700278 /* Check to see if we have gotten back to the initial state because
279 * someone requested to destroy the remote node context object.
280 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000281 if (sm->previous_state_id == SCI_RNC_INVALIDATING) {
Jeff Skirvin59e35392012-03-08 22:41:54 -0800282 rnc->destination_state = RNC_DEST_UNSPECIFIED;
Dan Williams89a73012011-06-30 19:14:33 -0700283 sci_remote_node_context_notify_user(rnc);
Jeff Skirvin1f053882012-03-08 22:42:08 -0800284
285 smp_wmb();
286 wake_up(&ihost->eventq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700287 }
288}
289
Dan Williams89a73012011-06-30 19:14:33 -0700290static void sci_remote_node_context_posting_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 *sci_rnc = container_of(sm, typeof(*sci_rnc), sm);
Dan Williams6f231dd2011-07-02 22:56:22 -0700293
Dan Williams89a73012011-06-30 19:14:33 -0700294 sci_remote_node_context_validate_context_buffer(sci_rnc);
Dan Williams6f231dd2011-07-02 22:56:22 -0700295}
296
Dan Williams89a73012011-06-30 19:14:33 -0700297static void sci_remote_node_context_invalidating_state_enter(struct sci_base_state_machine *sm)
Dan Williams6f231dd2011-07-02 22:56:22 -0700298{
Dan Williams89a73012011-06-30 19:14:33 -0700299 struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
Dan Williams6f231dd2011-07-02 22:56:22 -0700300
Jeff Skirvinaa20d932012-03-08 22:41:56 -0800301 /* Terminate all outstanding requests. */
302 sci_remote_device_terminate_requests(rnc_to_dev(rnc));
Dan Williams89a73012011-06-30 19:14:33 -0700303 sci_remote_node_context_invalidate_context_buffer(rnc);
Dan Williams6f231dd2011-07-02 22:56:22 -0700304}
305
Dan Williams89a73012011-06-30 19:14:33 -0700306static void sci_remote_node_context_resuming_state_enter(struct sci_base_state_machine *sm)
Dan Williams6f231dd2011-07-02 22:56:22 -0700307{
Dan Williams89a73012011-06-30 19:14:33 -0700308 struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
Dan Williams78a6f062011-06-30 16:31:37 -0700309 struct isci_remote_device *idev;
Dan Williamsa1a113b2011-04-21 18:44:45 -0700310 struct domain_device *dev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700311
Dan Williams78a6f062011-06-30 16:31:37 -0700312 idev = rnc_to_dev(rnc);
313 dev = idev->domain_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700314
Henryk Dembkowski24621462011-02-23 00:08:52 -0800315 /*
316 * For direct attached SATA devices we need to clear the TLCR
317 * NCQ to TCi tag mapping on the phy and in cases where we
318 * resume because of a target reset we also need to update
319 * the STPTLDARNI register with the RNi of the device
320 */
Dan Williams11cc5182012-02-01 00:23:10 -0800321 if (dev_is_sata(dev) && !dev->parent)
322 sci_port_setup_transports(idev->owning_port, rnc->remote_node_index);
Henryk Dembkowski24621462011-02-23 00:08:52 -0800323
Dan Williams89a73012011-06-30 19:14:33 -0700324 sci_remote_device_post_request(idev, SCU_CONTEXT_COMMAND_POST_RNC_RESUME);
Dan Williams6f231dd2011-07-02 22:56:22 -0700325}
326
Dan Williams89a73012011-06-30 19:14:33 -0700327static void sci_remote_node_context_ready_state_enter(struct sci_base_state_machine *sm)
Dan Williams6f231dd2011-07-02 22:56:22 -0700328{
Dan Williams89a73012011-06-30 19:14:33 -0700329 struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
Jeff Skirvin59e35392012-03-08 22:41:54 -0800330 enum sci_remote_node_context_destination_state dest_select;
Jeff Skirvin59e35392012-03-08 22:41:54 -0800331 int tell_user = 1;
Dan Williams6f231dd2011-07-02 22:56:22 -0700332
Jeff Skirvin59e35392012-03-08 22:41:54 -0800333 dest_select = rnc->destination_state;
334 rnc->destination_state = RNC_DEST_UNSPECIFIED;
Dan Williams6f231dd2011-07-02 22:56:22 -0700335
Jeff Skirvin59e35392012-03-08 22:41:54 -0800336 if ((dest_select == RNC_DEST_SUSPENDED) ||
337 (dest_select == RNC_DEST_SUSPENDED_RESUME)) {
338 sci_remote_node_context_suspend(
Jeff Skirvin447bfbc2012-03-08 22:41:59 -0800339 rnc, rnc->suspend_reason,
340 SCI_SOFTWARE_SUSPEND_EXPECTED_EVENT);
Jeff Skirvin59e35392012-03-08 22:41:54 -0800341
Jeff Skirvind76689e2012-03-08 22:42:04 -0800342 if (dest_select == RNC_DEST_SUSPENDED_RESUME)
Jeff Skirvin59e35392012-03-08 22:41:54 -0800343 tell_user = 0; /* Wait until ready again. */
Jeff Skirvin59e35392012-03-08 22:41:54 -0800344 }
Jeff Skirvind76689e2012-03-08 22:42:04 -0800345 if (tell_user)
Dan Williams89a73012011-06-30 19:14:33 -0700346 sci_remote_node_context_notify_user(rnc);
Dan Williams6f231dd2011-07-02 22:56:22 -0700347}
348
Dan Williams89a73012011-06-30 19:14:33 -0700349static void sci_remote_node_context_tx_suspended_state_enter(struct sci_base_state_machine *sm)
Dan Williams6f231dd2011-07-02 22:56:22 -0700350{
Dan Williams89a73012011-06-30 19:14:33 -0700351 struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
Dan Williams6f231dd2011-07-02 22:56:22 -0700352
Dan Williams89a73012011-06-30 19:14:33 -0700353 sci_remote_node_context_continue_state_transitions(rnc);
Dan Williams6f231dd2011-07-02 22:56:22 -0700354}
355
Dan Williams89a73012011-06-30 19:14:33 -0700356static void sci_remote_node_context_tx_rx_suspended_state_enter(struct sci_base_state_machine *sm)
Dan Williams6f231dd2011-07-02 22:56:22 -0700357{
Dan Williams89a73012011-06-30 19:14:33 -0700358 struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
Jeff Skirvin726980d2012-03-08 22:41:50 -0800359 struct isci_remote_device *idev = rnc_to_dev(rnc);
360 struct isci_host *ihost = idev->owning_port->owning_controller;
Jeff Skirvin31a38ef2012-03-08 22:42:01 -0800361 u32 new_count = rnc->suspend_count + 1;
362
363 if (new_count == 0)
364 rnc->suspend_count = 1;
365 else
366 rnc->suspend_count = new_count;
367 smp_wmb();
Dan Williams6f231dd2011-07-02 22:56:22 -0700368
Jeff Skirvin726980d2012-03-08 22:41:50 -0800369 /* Terminate outstanding requests pending abort. */
370 sci_remote_device_abort_requests_pending_abort(idev);
371
372 wake_up(&ihost->eventq);
Dan Williams89a73012011-06-30 19:14:33 -0700373 sci_remote_node_context_continue_state_transitions(rnc);
Dan Williams6f231dd2011-07-02 22:56:22 -0700374}
375
Jeff Skirvin6f488442012-03-08 22:41:48 -0800376static void sci_remote_node_context_await_suspend_state_exit(
377 struct sci_base_state_machine *sm)
378{
379 struct sci_remote_node_context *rnc
380 = container_of(sm, typeof(*rnc), sm);
Jeff Skirvinc94fc1a2012-03-08 22:41:58 -0800381 struct isci_remote_device *idev = rnc_to_dev(rnc);
Jeff Skirvin6f488442012-03-08 22:41:48 -0800382
Jeff Skirvinc94fc1a2012-03-08 22:41:58 -0800383 if (dev_is_sata(idev->domain_dev))
384 isci_dev_set_hang_detection_timeout(idev, 0);
Jeff Skirvin6f488442012-03-08 22:41:48 -0800385}
386
Dan Williams89a73012011-06-30 19:14:33 -0700387static const struct sci_base_state sci_remote_node_context_state_table[] = {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000388 [SCI_RNC_INITIAL] = {
Dan Williams89a73012011-06-30 19:14:33 -0700389 .enter_state = sci_remote_node_context_initial_state_enter,
Dan Williams6f231dd2011-07-02 22:56:22 -0700390 },
Edmund Nadolskie3013702011-06-02 00:10:43 +0000391 [SCI_RNC_POSTING] = {
Dan Williams89a73012011-06-30 19:14:33 -0700392 .enter_state = sci_remote_node_context_posting_state_enter,
Dan Williams6f231dd2011-07-02 22:56:22 -0700393 },
Edmund Nadolskie3013702011-06-02 00:10:43 +0000394 [SCI_RNC_INVALIDATING] = {
Dan Williams89a73012011-06-30 19:14:33 -0700395 .enter_state = sci_remote_node_context_invalidating_state_enter,
Dan Williams6f231dd2011-07-02 22:56:22 -0700396 },
Edmund Nadolskie3013702011-06-02 00:10:43 +0000397 [SCI_RNC_RESUMING] = {
Dan Williams89a73012011-06-30 19:14:33 -0700398 .enter_state = sci_remote_node_context_resuming_state_enter,
Dan Williams6f231dd2011-07-02 22:56:22 -0700399 },
Edmund Nadolskie3013702011-06-02 00:10:43 +0000400 [SCI_RNC_READY] = {
Dan Williams89a73012011-06-30 19:14:33 -0700401 .enter_state = sci_remote_node_context_ready_state_enter,
Dan Williams6f231dd2011-07-02 22:56:22 -0700402 },
Edmund Nadolskie3013702011-06-02 00:10:43 +0000403 [SCI_RNC_TX_SUSPENDED] = {
Dan Williams89a73012011-06-30 19:14:33 -0700404 .enter_state = sci_remote_node_context_tx_suspended_state_enter,
Dan Williams6f231dd2011-07-02 22:56:22 -0700405 },
Edmund Nadolskie3013702011-06-02 00:10:43 +0000406 [SCI_RNC_TX_RX_SUSPENDED] = {
Dan Williams89a73012011-06-30 19:14:33 -0700407 .enter_state = sci_remote_node_context_tx_rx_suspended_state_enter,
Dan Williams6f231dd2011-07-02 22:56:22 -0700408 },
Jeff Skirvin6f488442012-03-08 22:41:48 -0800409 [SCI_RNC_AWAIT_SUSPENSION] = {
410 .exit_state = sci_remote_node_context_await_suspend_state_exit,
411 },
Dan Williams6f231dd2011-07-02 22:56:22 -0700412};
413
Dan Williams89a73012011-06-30 19:14:33 -0700414void sci_remote_node_context_construct(struct sci_remote_node_context *rnc,
Dan Williams96143952011-04-19 18:35:58 -0700415 u16 remote_node_index)
Dan Williams35173d52011-03-26 16:43:01 -0700416{
Dan Williams89a73012011-06-30 19:14:33 -0700417 memset(rnc, 0, sizeof(struct sci_remote_node_context));
Dan Williams35173d52011-03-26 16:43:01 -0700418
419 rnc->remote_node_index = remote_node_index;
Jeff Skirvin59e35392012-03-08 22:41:54 -0800420 rnc->destination_state = RNC_DEST_UNSPECIFIED;
Dan Williams35173d52011-03-26 16:43:01 -0700421
Dan Williams89a73012011-06-30 19:14:33 -0700422 sci_init_sm(&rnc->sm, sci_remote_node_context_state_table, SCI_RNC_INITIAL);
Dan Williams35173d52011-03-26 16:43:01 -0700423}
Dan Williams338e3862011-05-12 07:46:59 -0700424
Dan Williams89a73012011-06-30 19:14:33 -0700425enum sci_status sci_remote_node_context_event_handler(struct sci_remote_node_context *sci_rnc,
Dan Williams338e3862011-05-12 07:46:59 -0700426 u32 event_code)
427{
428 enum scis_sds_remote_node_context_states state;
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800429 u32 next_state;
Dan Williams338e3862011-05-12 07:46:59 -0700430
Edmund Nadolskie3013702011-06-02 00:10:43 +0000431 state = sci_rnc->sm.current_state_id;
Dan Williams338e3862011-05-12 07:46:59 -0700432 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000433 case SCI_RNC_POSTING:
Dan Williams338e3862011-05-12 07:46:59 -0700434 switch (scu_get_event_code(event_code)) {
435 case SCU_EVENT_POST_RNC_COMPLETE:
Edmund Nadolskie3013702011-06-02 00:10:43 +0000436 sci_change_state(&sci_rnc->sm, SCI_RNC_READY);
Dan Williams338e3862011-05-12 07:46:59 -0700437 break;
438 default:
439 goto out;
440 }
441 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000442 case SCI_RNC_INVALIDATING:
Dan Williams338e3862011-05-12 07:46:59 -0700443 if (scu_get_event_code(event_code) == SCU_EVENT_POST_RNC_INVALIDATE_COMPLETE) {
Jeff Skirvin59e35392012-03-08 22:41:54 -0800444 if (sci_rnc->destination_state == RNC_DEST_FINAL)
445 next_state = SCI_RNC_INITIAL;
Dan Williams338e3862011-05-12 07:46:59 -0700446 else
Jeff Skirvin59e35392012-03-08 22:41:54 -0800447 next_state = SCI_RNC_POSTING;
448 sci_change_state(&sci_rnc->sm, next_state);
Dan Williams338e3862011-05-12 07:46:59 -0700449 } else {
450 switch (scu_get_event_type(event_code)) {
451 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
452 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
453 /* We really dont care if the hardware is going to suspend
454 * the device since it's being invalidated anyway */
Jeff Skirvin8c731882012-03-08 22:42:06 -0800455 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
Dan Williams338e3862011-05-12 07:46:59 -0700456 "%s: SCIC Remote Node Context 0x%p was "
457 "suspeneded by hardware while being "
458 "invalidated.\n", __func__, sci_rnc);
459 break;
460 default:
461 goto out;
462 }
463 }
464 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000465 case SCI_RNC_RESUMING:
Dan Williams338e3862011-05-12 07:46:59 -0700466 if (scu_get_event_code(event_code) == SCU_EVENT_POST_RCN_RELEASE) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000467 sci_change_state(&sci_rnc->sm, SCI_RNC_READY);
Dan Williams338e3862011-05-12 07:46:59 -0700468 } else {
469 switch (scu_get_event_type(event_code)) {
470 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
471 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
472 /* We really dont care if the hardware is going to suspend
473 * the device since it's being resumed anyway */
Jeff Skirvin8c731882012-03-08 22:42:06 -0800474 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
Dan Williams338e3862011-05-12 07:46:59 -0700475 "%s: SCIC Remote Node Context 0x%p was "
476 "suspeneded by hardware while being resumed.\n",
477 __func__, sci_rnc);
478 break;
479 default:
480 goto out;
481 }
482 }
483 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000484 case SCI_RNC_READY:
Dan Williams338e3862011-05-12 07:46:59 -0700485 switch (scu_get_event_type(event_code)) {
486 case SCU_EVENT_TL_RNC_SUSPEND_TX:
Edmund Nadolskie3013702011-06-02 00:10:43 +0000487 sci_change_state(&sci_rnc->sm, SCI_RNC_TX_SUSPENDED);
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800488 sci_rnc->suspend_type = scu_get_event_type(event_code);
Dan Williams338e3862011-05-12 07:46:59 -0700489 break;
490 case SCU_EVENT_TL_RNC_SUSPEND_TX_RX:
Edmund Nadolskie3013702011-06-02 00:10:43 +0000491 sci_change_state(&sci_rnc->sm, SCI_RNC_TX_RX_SUSPENDED);
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800492 sci_rnc->suspend_type = scu_get_event_type(event_code);
Dan Williams338e3862011-05-12 07:46:59 -0700493 break;
494 default:
495 goto out;
496 }
497 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000498 case SCI_RNC_AWAIT_SUSPENSION:
Dan Williams338e3862011-05-12 07:46:59 -0700499 switch (scu_get_event_type(event_code)) {
500 case SCU_EVENT_TL_RNC_SUSPEND_TX:
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800501 next_state = SCI_RNC_TX_SUSPENDED;
Dan Williams338e3862011-05-12 07:46:59 -0700502 break;
503 case SCU_EVENT_TL_RNC_SUSPEND_TX_RX:
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800504 next_state = SCI_RNC_TX_RX_SUSPENDED;
Dan Williams338e3862011-05-12 07:46:59 -0700505 break;
506 default:
507 goto out;
508 }
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800509 if (sci_rnc->suspend_type == scu_get_event_type(event_code))
510 sci_change_state(&sci_rnc->sm, next_state);
Dan Williams338e3862011-05-12 07:46:59 -0700511 break;
512 default:
513 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
Dan Williams14e99b42012-02-10 01:05:43 -0800514 "%s: invalid state: %s\n", __func__,
515 rnc_state_name(state));
Dan Williams338e3862011-05-12 07:46:59 -0700516 return SCI_FAILURE_INVALID_STATE;
517 }
518 return SCI_SUCCESS;
519
520 out:
521 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
Dan Williams14e99b42012-02-10 01:05:43 -0800522 "%s: code: %#x state: %s\n", __func__, event_code,
523 rnc_state_name(state));
Dan Williams338e3862011-05-12 07:46:59 -0700524 return SCI_FAILURE;
525
526}
Dan Williamsc845ae92011-05-12 08:26:56 -0700527
Dan Williams89a73012011-06-30 19:14:33 -0700528enum sci_status sci_remote_node_context_destruct(struct sci_remote_node_context *sci_rnc,
Dan Williamsc845ae92011-05-12 08:26:56 -0700529 scics_sds_remote_node_context_callback cb_fn,
530 void *cb_p)
531{
532 enum scis_sds_remote_node_context_states state;
533
Edmund Nadolskie3013702011-06-02 00:10:43 +0000534 state = sci_rnc->sm.current_state_id;
Dan Williamsc845ae92011-05-12 08:26:56 -0700535 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000536 case SCI_RNC_INVALIDATING:
Jeff Skirvin59e35392012-03-08 22:41:54 -0800537 sci_remote_node_context_setup_to_destroy(sci_rnc, cb_fn, cb_p);
Dan Williamsc845ae92011-05-12 08:26:56 -0700538 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000539 case SCI_RNC_POSTING:
540 case SCI_RNC_RESUMING:
541 case SCI_RNC_READY:
542 case SCI_RNC_TX_SUSPENDED:
543 case SCI_RNC_TX_RX_SUSPENDED:
Jeff Skirvin59e35392012-03-08 22:41:54 -0800544 sci_remote_node_context_setup_to_destroy(sci_rnc, cb_fn, cb_p);
Edmund Nadolskie3013702011-06-02 00:10:43 +0000545 sci_change_state(&sci_rnc->sm, SCI_RNC_INVALIDATING);
Dan Williamsc845ae92011-05-12 08:26:56 -0700546 return SCI_SUCCESS;
Jeff Skirvin79cbab82012-03-13 16:36:35 -0700547 case SCI_RNC_AWAIT_SUSPENSION:
548 sci_remote_node_context_setup_to_destroy(sci_rnc, cb_fn, cb_p);
549 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000550 case SCI_RNC_INITIAL:
Dan Williamsc845ae92011-05-12 08:26:56 -0700551 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
Dan Williams14e99b42012-02-10 01:05:43 -0800552 "%s: invalid state: %s\n", __func__,
553 rnc_state_name(state));
Dan Williamsc845ae92011-05-12 08:26:56 -0700554 /* We have decided that the destruct request on the remote node context
555 * can not fail since it is either in the initial/destroyed state or is
556 * can be destroyed.
557 */
558 return SCI_SUCCESS;
559 default:
560 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
Dan Williams14e99b42012-02-10 01:05:43 -0800561 "%s: invalid state %s\n", __func__,
562 rnc_state_name(state));
Dan Williamsc845ae92011-05-12 08:26:56 -0700563 return SCI_FAILURE_INVALID_STATE;
564 }
565}
Dan Williamsed3efb72011-05-12 08:50:23 -0700566
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800567enum sci_status sci_remote_node_context_suspend(
568 struct sci_remote_node_context *sci_rnc,
569 enum sci_remote_node_suspension_reasons suspend_reason,
Jeff Skirvin447bfbc2012-03-08 22:41:59 -0800570 u32 suspend_type)
Dan Williamsed3efb72011-05-12 08:50:23 -0700571{
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800572 enum scis_sds_remote_node_context_states state
573 = sci_rnc->sm.current_state_id;
574 struct isci_remote_device *idev = rnc_to_dev(sci_rnc);
575 enum sci_status status = SCI_FAILURE_INVALID_STATE;
Jeff Skirvin59e35392012-03-08 22:41:54 -0800576 enum sci_remote_node_context_destination_state dest_param =
577 RNC_DEST_UNSPECIFIED;
Dan Williamsed3efb72011-05-12 08:50:23 -0700578
Jeff Skirvin726980d2012-03-08 22:41:50 -0800579 dev_dbg(scirdev_to_dev(idev),
Jeff Skirvin8c731882012-03-08 22:42:06 -0800580 "%s: current state %s, current suspend_type %x dest state %d,"
Jeff Skirvin726980d2012-03-08 22:41:50 -0800581 " arg suspend_reason %d, arg suspend_type %x",
Jeff Skirvin8c731882012-03-08 22:42:06 -0800582 __func__, rnc_state_name(state), sci_rnc->suspend_type,
Jeff Skirvin726980d2012-03-08 22:41:50 -0800583 sci_rnc->destination_state, suspend_reason,
584 suspend_type);
585
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800586 /* Disable automatic state continuations if explicitly suspending. */
Jeff Skirvinc94fc1a2012-03-08 22:41:58 -0800587 if ((suspend_reason == SCI_HW_SUSPEND) ||
Jeff Skirvin59e35392012-03-08 22:41:54 -0800588 (sci_rnc->destination_state == RNC_DEST_FINAL))
589 dest_param = sci_rnc->destination_state;
590
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800591 switch (state) {
592 case SCI_RNC_READY:
593 break;
Jeff Skirvin59e35392012-03-08 22:41:54 -0800594 case SCI_RNC_INVALIDATING:
595 if (sci_rnc->destination_state == RNC_DEST_FINAL) {
596 dev_warn(scirdev_to_dev(idev),
597 "%s: already destroying %p\n",
598 __func__, sci_rnc);
599 return SCI_FAILURE_INVALID_STATE;
600 }
601 /* Fall through and handle like SCI_RNC_POSTING */
Jeff Skirvind76689e2012-03-08 22:42:04 -0800602 case SCI_RNC_RESUMING:
603 /* Fall through and handle like SCI_RNC_POSTING */
Jeff Skirvin59e35392012-03-08 22:41:54 -0800604 case SCI_RNC_POSTING:
605 /* Set the destination state to AWAIT - this signals the
606 * entry into the SCI_RNC_READY state that a suspension
607 * needs to be done immediately.
608 */
Jeff Skirvin6c6aacb2012-03-13 17:03:00 -0700609 if (sci_rnc->destination_state != RNC_DEST_FINAL)
610 sci_rnc->destination_state = RNC_DEST_SUSPENDED;
Jeff Skirvin447bfbc2012-03-08 22:41:59 -0800611 sci_rnc->suspend_type = suspend_type;
612 sci_rnc->suspend_reason = suspend_reason;
Jeff Skirvin59e35392012-03-08 22:41:54 -0800613 return SCI_SUCCESS;
614
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800615 case SCI_RNC_TX_SUSPENDED:
616 if (suspend_type == SCU_EVENT_TL_RNC_SUSPEND_TX)
617 status = SCI_SUCCESS;
618 break;
619 case SCI_RNC_TX_RX_SUSPENDED:
620 if (suspend_type == SCU_EVENT_TL_RNC_SUSPEND_TX_RX)
621 status = SCI_SUCCESS;
622 break;
623 case SCI_RNC_AWAIT_SUSPENSION:
624 if ((sci_rnc->suspend_type == SCU_EVENT_TL_RNC_SUSPEND_TX_RX)
625 || (suspend_type == sci_rnc->suspend_type))
626 return SCI_SUCCESS;
627 break;
628 default:
Dan Williamsed3efb72011-05-12 08:50:23 -0700629 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
Dan Williams14e99b42012-02-10 01:05:43 -0800630 "%s: invalid state %s\n", __func__,
631 rnc_state_name(state));
Dan Williamsed3efb72011-05-12 08:50:23 -0700632 return SCI_FAILURE_INVALID_STATE;
633 }
Jeff Skirvin59e35392012-03-08 22:41:54 -0800634 sci_rnc->destination_state = dest_param;
Jeff Skirvin447bfbc2012-03-08 22:41:59 -0800635 sci_rnc->suspend_type = suspend_type;
636 sci_rnc->suspend_reason = suspend_reason;
Dan Williamsed3efb72011-05-12 08:50:23 -0700637
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800638 if (status == SCI_SUCCESS) { /* Already in the destination state? */
Jeff Skirvin726980d2012-03-08 22:41:50 -0800639 struct isci_host *ihost = idev->owning_port->owning_controller;
640
Jeff Skirvin726980d2012-03-08 22:41:50 -0800641 wake_up_all(&ihost->eventq); /* Let observers look. */
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800642 return SCI_SUCCESS;
Dan Williamsed3efb72011-05-12 08:50:23 -0700643 }
Jeff Skirvinc94fc1a2012-03-08 22:41:58 -0800644 if ((suspend_reason == SCI_SW_SUSPEND_NORMAL) ||
645 (suspend_reason == SCI_SW_SUSPEND_LINKHANG_DETECT)) {
646
Jeff Skirvin9608b642012-03-08 22:42:00 -0800647 if (suspend_reason == SCI_SW_SUSPEND_LINKHANG_DETECT)
Jeff Skirvinc94fc1a2012-03-08 22:41:58 -0800648 isci_dev_set_hang_detection_timeout(idev, 0x00000001);
649
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800650 sci_remote_device_post_request(
651 idev, SCI_SOFTWARE_SUSPEND_CMD);
652 }
653 if (state != SCI_RNC_AWAIT_SUSPENSION)
654 sci_change_state(&sci_rnc->sm, SCI_RNC_AWAIT_SUSPENSION);
Dan Williamsed3efb72011-05-12 08:50:23 -0700655
Dan Williamsed3efb72011-05-12 08:50:23 -0700656 return SCI_SUCCESS;
657}
658
Dan Williams89a73012011-06-30 19:14:33 -0700659enum sci_status sci_remote_node_context_resume(struct sci_remote_node_context *sci_rnc,
Dan Williamsed3efb72011-05-12 08:50:23 -0700660 scics_sds_remote_node_context_callback cb_fn,
661 void *cb_p)
662{
663 enum scis_sds_remote_node_context_states state;
Jeff Skirvin0c3ce382012-03-08 22:42:02 -0800664 struct isci_remote_device *idev = rnc_to_dev(sci_rnc);
Dan Williamsed3efb72011-05-12 08:50:23 -0700665
Edmund Nadolskie3013702011-06-02 00:10:43 +0000666 state = sci_rnc->sm.current_state_id;
Jeff Skirvin0c3ce382012-03-08 22:42:02 -0800667 dev_dbg(scirdev_to_dev(idev),
668 "%s: state %s, cb_fn = %p, cb_p = %p; dest_state = %d; "
669 "dev resume path %s\n",
670 __func__, rnc_state_name(state), cb_fn, cb_p,
671 sci_rnc->destination_state,
672 test_bit(IDEV_ABORT_PATH_ACTIVE, &idev->flags)
673 ? "<abort active>" : "<normal>");
Jeff Skirvin31a38ef2012-03-08 22:42:01 -0800674
Dan Williamsed3efb72011-05-12 08:50:23 -0700675 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000676 case SCI_RNC_INITIAL:
Dan Williamsed3efb72011-05-12 08:50:23 -0700677 if (sci_rnc->remote_node_index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX)
678 return SCI_FAILURE_INVALID_STATE;
679
Jeff Skirvin6c6aacb2012-03-13 17:03:00 -0700680 sci_remote_node_context_setup_to_resume(sci_rnc, cb_fn, cb_p,
681 RNC_DEST_READY);
682 if (!test_bit(IDEV_ABORT_PATH_ACTIVE, &idev->flags)) {
Jeff Skirvin0c3ce382012-03-08 22:42:02 -0800683 sci_remote_node_context_construct_buffer(sci_rnc);
684 sci_change_state(&sci_rnc->sm, SCI_RNC_POSTING);
685 }
Dan Williamsed3efb72011-05-12 08:50:23 -0700686 return SCI_SUCCESS;
Jeff Skirvin0c3ce382012-03-08 22:42:02 -0800687
Edmund Nadolskie3013702011-06-02 00:10:43 +0000688 case SCI_RNC_POSTING:
689 case SCI_RNC_INVALIDATING:
690 case SCI_RNC_RESUMING:
Jeff Skirvin6c6aacb2012-03-13 17:03:00 -0700691 /* We are still waiting to post when a resume was
692 * requested.
693 */
694 switch (sci_rnc->destination_state) {
695 case RNC_DEST_SUSPENDED:
696 case RNC_DEST_SUSPENDED_RESUME:
697 /* Previously waiting to suspend after posting.
698 * Now continue onto resumption.
Jeff Skirvin59e35392012-03-08 22:41:54 -0800699 */
Jeff Skirvin6c6aacb2012-03-13 17:03:00 -0700700 sci_remote_node_context_setup_to_resume(
701 sci_rnc, cb_fn, cb_p,
702 RNC_DEST_SUSPENDED_RESUME);
703 break;
704 default:
705 sci_remote_node_context_setup_to_resume(
706 sci_rnc, cb_fn, cb_p,
707 RNC_DEST_READY);
708 break;
Jeff Skirvin59e35392012-03-08 22:41:54 -0800709 }
Dan Williamsed3efb72011-05-12 08:50:23 -0700710 return SCI_SUCCESS;
Jeff Skirvin0c3ce382012-03-08 22:42:02 -0800711
Jeff Skirvin56d7c012012-03-08 22:41:49 -0800712 case SCI_RNC_TX_SUSPENDED:
Jeff Skirvin0c3ce382012-03-08 22:42:02 -0800713 case SCI_RNC_TX_RX_SUSPENDED:
Jeff Skirvin6c6aacb2012-03-13 17:03:00 -0700714 {
Jeff Skirvin0c3ce382012-03-08 22:42:02 -0800715 struct domain_device *dev = idev->domain_dev;
716 /* If this is an expander attached SATA device we must
717 * invalidate and repost the RNC since this is the only
718 * way to clear the TCi to NCQ tag mapping table for
719 * the RNi. All other device types we can just resume.
720 */
721 sci_remote_node_context_setup_to_resume(
722 sci_rnc, cb_fn, cb_p, RNC_DEST_READY);
Dan Williamsed3efb72011-05-12 08:50:23 -0700723
Jeff Skirvin6c6aacb2012-03-13 17:03:00 -0700724 if (!test_bit(IDEV_ABORT_PATH_ACTIVE, &idev->flags)) {
725 if ((dev_is_sata(dev) && dev->parent) ||
726 (sci_rnc->destination_state == RNC_DEST_FINAL))
727 sci_change_state(&sci_rnc->sm,
728 SCI_RNC_INVALIDATING);
729 else
730 sci_change_state(&sci_rnc->sm,
731 SCI_RNC_RESUMING);
732 }
Jeff Skirvin0c3ce382012-03-08 22:42:02 -0800733 }
Dan Williamsed3efb72011-05-12 08:50:23 -0700734 return SCI_SUCCESS;
Jeff Skirvin0c3ce382012-03-08 22:42:02 -0800735
Edmund Nadolskie3013702011-06-02 00:10:43 +0000736 case SCI_RNC_AWAIT_SUSPENSION:
Jeff Skirvin6c6aacb2012-03-13 17:03:00 -0700737 sci_remote_node_context_setup_to_resume(
738 sci_rnc, cb_fn, cb_p, RNC_DEST_SUSPENDED_RESUME);
Dan Williamsed3efb72011-05-12 08:50:23 -0700739 return SCI_SUCCESS;
740 default:
741 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
Dan Williams14e99b42012-02-10 01:05:43 -0800742 "%s: invalid state %s\n", __func__,
743 rnc_state_name(state));
Dan Williamsed3efb72011-05-12 08:50:23 -0700744 return SCI_FAILURE_INVALID_STATE;
745 }
746}
Dan Williamsf34d9e52011-05-12 09:27:52 -0700747
Dan Williams89a73012011-06-30 19:14:33 -0700748enum sci_status sci_remote_node_context_start_io(struct sci_remote_node_context *sci_rnc,
Dan Williams5076a1a2011-06-27 14:57:03 -0700749 struct isci_request *ireq)
Dan Williamsf34d9e52011-05-12 09:27:52 -0700750{
751 enum scis_sds_remote_node_context_states state;
752
Edmund Nadolskie3013702011-06-02 00:10:43 +0000753 state = sci_rnc->sm.current_state_id;
Jeff Skirvinfd536602011-06-20 14:09:22 -0700754
755 switch (state) {
756 case SCI_RNC_READY:
757 return SCI_SUCCESS;
758 case SCI_RNC_TX_SUSPENDED:
759 case SCI_RNC_TX_RX_SUSPENDED:
760 case SCI_RNC_AWAIT_SUSPENSION:
Dan Williamsf34d9e52011-05-12 09:27:52 -0700761 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
Dan Williams14e99b42012-02-10 01:05:43 -0800762 "%s: invalid state %s\n", __func__,
763 rnc_state_name(state));
Dan Williamsf34d9e52011-05-12 09:27:52 -0700764 return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
Jeff Skirvinfd536602011-06-20 14:09:22 -0700765 default:
Dan Williams14e99b42012-02-10 01:05:43 -0800766 dev_dbg(scirdev_to_dev(rnc_to_dev(sci_rnc)),
767 "%s: invalid state %s\n", __func__,
768 rnc_state_name(state));
769 return SCI_FAILURE_INVALID_STATE;
Dan Williamsf34d9e52011-05-12 09:27:52 -0700770 }
Dan Williamsf34d9e52011-05-12 09:27:52 -0700771}
772
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -0800773enum sci_status sci_remote_node_context_start_task(
774 struct sci_remote_node_context *sci_rnc,
775 struct isci_request *ireq,
776 scics_sds_remote_node_context_callback cb_fn,
777 void *cb_p)
Dan Williamsf34d9e52011-05-12 09:27:52 -0700778{
Jeff Skirvin59e35392012-03-08 22:41:54 -0800779 enum sci_status status = sci_remote_node_context_resume(sci_rnc,
780 cb_fn, cb_p);
781 if (status != SCI_SUCCESS)
Dan Williamsf34d9e52011-05-12 09:27:52 -0700782 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
Jeff Skirvin59e35392012-03-08 22:41:54 -0800783 "%s: resume failed: %d\n", __func__, status);
784 return status;
Dan Williamsf34d9e52011-05-12 09:27:52 -0700785}
Jeff Skirvin726980d2012-03-08 22:41:50 -0800786
787int sci_remote_node_context_is_safe_to_abort(
788 struct sci_remote_node_context *sci_rnc)
789{
790 enum scis_sds_remote_node_context_states state;
791
792 state = sci_rnc->sm.current_state_id;
793 switch (state) {
794 case SCI_RNC_INVALIDATING:
795 case SCI_RNC_TX_RX_SUSPENDED:
796 return 1;
797 case SCI_RNC_POSTING:
798 case SCI_RNC_RESUMING:
799 case SCI_RNC_READY:
800 case SCI_RNC_TX_SUSPENDED:
801 case SCI_RNC_AWAIT_SUSPENSION:
802 case SCI_RNC_INITIAL:
803 return 0;
804 default:
805 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
806 "%s: invalid state %d\n", __func__, state);
807 return 0;
808 }
809}