blob: a0a62e3a500d1c1a08d107ad975fbd7bfe23a8c1 [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;
Jeff Skirvin59e35392012-03-08 22:41:54 -0800324 int tell_user = 1;
Dan Williams6f231dd2011-07-02 22:56:22 -0700325
Jeff Skirvin59e35392012-03-08 22:41:54 -0800326 dest_select = rnc->destination_state;
327 rnc->destination_state = RNC_DEST_UNSPECIFIED;
Dan Williams6f231dd2011-07-02 22:56:22 -0700328
Jeff Skirvin59e35392012-03-08 22:41:54 -0800329 if ((dest_select == RNC_DEST_SUSPENDED) ||
330 (dest_select == RNC_DEST_SUSPENDED_RESUME)) {
331 sci_remote_node_context_suspend(
Jeff Skirvin447bfbc2012-03-08 22:41:59 -0800332 rnc, rnc->suspend_reason,
333 SCI_SOFTWARE_SUSPEND_EXPECTED_EVENT);
Jeff Skirvin59e35392012-03-08 22:41:54 -0800334
Jeff Skirvind76689e2012-03-08 22:42:04 -0800335 if (dest_select == RNC_DEST_SUSPENDED_RESUME)
Jeff Skirvin59e35392012-03-08 22:41:54 -0800336 tell_user = 0; /* Wait until ready again. */
Jeff Skirvin59e35392012-03-08 22:41:54 -0800337 }
Jeff Skirvind76689e2012-03-08 22:42:04 -0800338 if (tell_user)
Dan Williams89a73012011-06-30 19:14:33 -0700339 sci_remote_node_context_notify_user(rnc);
Dan Williams6f231dd2011-07-02 22:56:22 -0700340}
341
Dan Williams89a73012011-06-30 19:14:33 -0700342static void sci_remote_node_context_tx_suspended_state_enter(struct sci_base_state_machine *sm)
Dan Williams6f231dd2011-07-02 22:56:22 -0700343{
Dan Williams89a73012011-06-30 19:14:33 -0700344 struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
Dan Williams6f231dd2011-07-02 22:56:22 -0700345
Dan Williams89a73012011-06-30 19:14:33 -0700346 sci_remote_node_context_continue_state_transitions(rnc);
Dan Williams6f231dd2011-07-02 22:56:22 -0700347}
348
Dan Williams89a73012011-06-30 19:14:33 -0700349static void sci_remote_node_context_tx_rx_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);
Jeff Skirvin726980d2012-03-08 22:41:50 -0800352 struct isci_remote_device *idev = rnc_to_dev(rnc);
353 struct isci_host *ihost = idev->owning_port->owning_controller;
Jeff Skirvin31a38ef2012-03-08 22:42:01 -0800354 u32 new_count = rnc->suspend_count + 1;
355
356 if (new_count == 0)
357 rnc->suspend_count = 1;
358 else
359 rnc->suspend_count = new_count;
360 smp_wmb();
Dan Williams6f231dd2011-07-02 22:56:22 -0700361
Jeff Skirvin726980d2012-03-08 22:41:50 -0800362 /* Terminate outstanding requests pending abort. */
363 sci_remote_device_abort_requests_pending_abort(idev);
364
365 wake_up(&ihost->eventq);
Dan Williams89a73012011-06-30 19:14:33 -0700366 sci_remote_node_context_continue_state_transitions(rnc);
Dan Williams6f231dd2011-07-02 22:56:22 -0700367}
368
Jeff Skirvin6f488442012-03-08 22:41:48 -0800369static void sci_remote_node_context_await_suspend_state_exit(
370 struct sci_base_state_machine *sm)
371{
372 struct sci_remote_node_context *rnc
373 = container_of(sm, typeof(*rnc), sm);
Jeff Skirvinc94fc1a2012-03-08 22:41:58 -0800374 struct isci_remote_device *idev = rnc_to_dev(rnc);
Jeff Skirvin6f488442012-03-08 22:41:48 -0800375
Jeff Skirvinc94fc1a2012-03-08 22:41:58 -0800376 if (dev_is_sata(idev->domain_dev))
377 isci_dev_set_hang_detection_timeout(idev, 0);
Jeff Skirvin6f488442012-03-08 22:41:48 -0800378}
379
Dan Williams89a73012011-06-30 19:14:33 -0700380static const struct sci_base_state sci_remote_node_context_state_table[] = {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000381 [SCI_RNC_INITIAL] = {
Dan Williams89a73012011-06-30 19:14:33 -0700382 .enter_state = sci_remote_node_context_initial_state_enter,
Dan Williams6f231dd2011-07-02 22:56:22 -0700383 },
Edmund Nadolskie3013702011-06-02 00:10:43 +0000384 [SCI_RNC_POSTING] = {
Dan Williams89a73012011-06-30 19:14:33 -0700385 .enter_state = sci_remote_node_context_posting_state_enter,
Dan Williams6f231dd2011-07-02 22:56:22 -0700386 },
Edmund Nadolskie3013702011-06-02 00:10:43 +0000387 [SCI_RNC_INVALIDATING] = {
Dan Williams89a73012011-06-30 19:14:33 -0700388 .enter_state = sci_remote_node_context_invalidating_state_enter,
Dan Williams6f231dd2011-07-02 22:56:22 -0700389 },
Edmund Nadolskie3013702011-06-02 00:10:43 +0000390 [SCI_RNC_RESUMING] = {
Dan Williams89a73012011-06-30 19:14:33 -0700391 .enter_state = sci_remote_node_context_resuming_state_enter,
Dan Williams6f231dd2011-07-02 22:56:22 -0700392 },
Edmund Nadolskie3013702011-06-02 00:10:43 +0000393 [SCI_RNC_READY] = {
Dan Williams89a73012011-06-30 19:14:33 -0700394 .enter_state = sci_remote_node_context_ready_state_enter,
Dan Williams6f231dd2011-07-02 22:56:22 -0700395 },
Edmund Nadolskie3013702011-06-02 00:10:43 +0000396 [SCI_RNC_TX_SUSPENDED] = {
Dan Williams89a73012011-06-30 19:14:33 -0700397 .enter_state = sci_remote_node_context_tx_suspended_state_enter,
Dan Williams6f231dd2011-07-02 22:56:22 -0700398 },
Edmund Nadolskie3013702011-06-02 00:10:43 +0000399 [SCI_RNC_TX_RX_SUSPENDED] = {
Dan Williams89a73012011-06-30 19:14:33 -0700400 .enter_state = sci_remote_node_context_tx_rx_suspended_state_enter,
Dan Williams6f231dd2011-07-02 22:56:22 -0700401 },
Jeff Skirvin6f488442012-03-08 22:41:48 -0800402 [SCI_RNC_AWAIT_SUSPENSION] = {
403 .exit_state = sci_remote_node_context_await_suspend_state_exit,
404 },
Dan Williams6f231dd2011-07-02 22:56:22 -0700405};
406
Dan Williams89a73012011-06-30 19:14:33 -0700407void sci_remote_node_context_construct(struct sci_remote_node_context *rnc,
Dan Williams96143952011-04-19 18:35:58 -0700408 u16 remote_node_index)
Dan Williams35173d52011-03-26 16:43:01 -0700409{
Dan Williams89a73012011-06-30 19:14:33 -0700410 memset(rnc, 0, sizeof(struct sci_remote_node_context));
Dan Williams35173d52011-03-26 16:43:01 -0700411
412 rnc->remote_node_index = remote_node_index;
Jeff Skirvin59e35392012-03-08 22:41:54 -0800413 rnc->destination_state = RNC_DEST_UNSPECIFIED;
Dan Williams35173d52011-03-26 16:43:01 -0700414
Dan Williams89a73012011-06-30 19:14:33 -0700415 sci_init_sm(&rnc->sm, sci_remote_node_context_state_table, SCI_RNC_INITIAL);
Dan Williams35173d52011-03-26 16:43:01 -0700416}
Dan Williams338e3862011-05-12 07:46:59 -0700417
Dan Williams89a73012011-06-30 19:14:33 -0700418enum sci_status sci_remote_node_context_event_handler(struct sci_remote_node_context *sci_rnc,
Dan Williams338e3862011-05-12 07:46:59 -0700419 u32 event_code)
420{
421 enum scis_sds_remote_node_context_states state;
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800422 u32 next_state;
Dan Williams338e3862011-05-12 07:46:59 -0700423
Edmund Nadolskie3013702011-06-02 00:10:43 +0000424 state = sci_rnc->sm.current_state_id;
Dan Williams338e3862011-05-12 07:46:59 -0700425 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000426 case SCI_RNC_POSTING:
Dan Williams338e3862011-05-12 07:46:59 -0700427 switch (scu_get_event_code(event_code)) {
428 case SCU_EVENT_POST_RNC_COMPLETE:
Edmund Nadolskie3013702011-06-02 00:10:43 +0000429 sci_change_state(&sci_rnc->sm, SCI_RNC_READY);
Dan Williams338e3862011-05-12 07:46:59 -0700430 break;
431 default:
432 goto out;
433 }
434 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000435 case SCI_RNC_INVALIDATING:
Dan Williams338e3862011-05-12 07:46:59 -0700436 if (scu_get_event_code(event_code) == SCU_EVENT_POST_RNC_INVALIDATE_COMPLETE) {
Jeff Skirvin59e35392012-03-08 22:41:54 -0800437 if (sci_rnc->destination_state == RNC_DEST_FINAL)
438 next_state = SCI_RNC_INITIAL;
Dan Williams338e3862011-05-12 07:46:59 -0700439 else
Jeff Skirvin59e35392012-03-08 22:41:54 -0800440 next_state = SCI_RNC_POSTING;
441 sci_change_state(&sci_rnc->sm, next_state);
Dan Williams338e3862011-05-12 07:46:59 -0700442 } else {
443 switch (scu_get_event_type(event_code)) {
444 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
445 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
446 /* We really dont care if the hardware is going to suspend
447 * the device since it's being invalidated anyway */
Jeff Skirvin8c731882012-03-08 22:42:06 -0800448 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
Dan Williams338e3862011-05-12 07:46:59 -0700449 "%s: SCIC Remote Node Context 0x%p was "
450 "suspeneded by hardware while being "
451 "invalidated.\n", __func__, sci_rnc);
452 break;
453 default:
454 goto out;
455 }
456 }
457 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000458 case SCI_RNC_RESUMING:
Dan Williams338e3862011-05-12 07:46:59 -0700459 if (scu_get_event_code(event_code) == SCU_EVENT_POST_RCN_RELEASE) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000460 sci_change_state(&sci_rnc->sm, SCI_RNC_READY);
Dan Williams338e3862011-05-12 07:46:59 -0700461 } else {
462 switch (scu_get_event_type(event_code)) {
463 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
464 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
465 /* We really dont care if the hardware is going to suspend
466 * the device since it's being resumed anyway */
Jeff Skirvin8c731882012-03-08 22:42:06 -0800467 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
Dan Williams338e3862011-05-12 07:46:59 -0700468 "%s: SCIC Remote Node Context 0x%p was "
469 "suspeneded by hardware while being resumed.\n",
470 __func__, sci_rnc);
471 break;
472 default:
473 goto out;
474 }
475 }
476 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000477 case SCI_RNC_READY:
Dan Williams338e3862011-05-12 07:46:59 -0700478 switch (scu_get_event_type(event_code)) {
479 case SCU_EVENT_TL_RNC_SUSPEND_TX:
Edmund Nadolskie3013702011-06-02 00:10:43 +0000480 sci_change_state(&sci_rnc->sm, SCI_RNC_TX_SUSPENDED);
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800481 sci_rnc->suspend_type = scu_get_event_type(event_code);
Dan Williams338e3862011-05-12 07:46:59 -0700482 break;
483 case SCU_EVENT_TL_RNC_SUSPEND_TX_RX:
Edmund Nadolskie3013702011-06-02 00:10:43 +0000484 sci_change_state(&sci_rnc->sm, SCI_RNC_TX_RX_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 default:
488 goto out;
489 }
490 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000491 case SCI_RNC_AWAIT_SUSPENSION:
Dan Williams338e3862011-05-12 07:46:59 -0700492 switch (scu_get_event_type(event_code)) {
493 case SCU_EVENT_TL_RNC_SUSPEND_TX:
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800494 next_state = SCI_RNC_TX_SUSPENDED;
Dan Williams338e3862011-05-12 07:46:59 -0700495 break;
496 case SCU_EVENT_TL_RNC_SUSPEND_TX_RX:
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800497 next_state = SCI_RNC_TX_RX_SUSPENDED;
Dan Williams338e3862011-05-12 07:46:59 -0700498 break;
499 default:
500 goto out;
501 }
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800502 if (sci_rnc->suspend_type == scu_get_event_type(event_code))
503 sci_change_state(&sci_rnc->sm, next_state);
Dan Williams338e3862011-05-12 07:46:59 -0700504 break;
505 default:
506 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
Dan Williams14e99b42012-02-10 01:05:43 -0800507 "%s: invalid state: %s\n", __func__,
508 rnc_state_name(state));
Dan Williams338e3862011-05-12 07:46:59 -0700509 return SCI_FAILURE_INVALID_STATE;
510 }
511 return SCI_SUCCESS;
512
513 out:
514 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
Dan Williams14e99b42012-02-10 01:05:43 -0800515 "%s: code: %#x state: %s\n", __func__, event_code,
516 rnc_state_name(state));
Dan Williams338e3862011-05-12 07:46:59 -0700517 return SCI_FAILURE;
518
519}
Dan Williamsc845ae92011-05-12 08:26:56 -0700520
Dan Williams89a73012011-06-30 19:14:33 -0700521enum sci_status sci_remote_node_context_destruct(struct sci_remote_node_context *sci_rnc,
Dan Williamsc845ae92011-05-12 08:26:56 -0700522 scics_sds_remote_node_context_callback cb_fn,
523 void *cb_p)
524{
525 enum scis_sds_remote_node_context_states state;
526
Edmund Nadolskie3013702011-06-02 00:10:43 +0000527 state = sci_rnc->sm.current_state_id;
Dan Williamsc845ae92011-05-12 08:26:56 -0700528 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000529 case SCI_RNC_INVALIDATING:
Jeff Skirvin59e35392012-03-08 22:41:54 -0800530 sci_remote_node_context_setup_to_destroy(sci_rnc, cb_fn, cb_p);
Dan Williamsc845ae92011-05-12 08:26:56 -0700531 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000532 case SCI_RNC_POSTING:
533 case SCI_RNC_RESUMING:
534 case SCI_RNC_READY:
535 case SCI_RNC_TX_SUSPENDED:
536 case SCI_RNC_TX_RX_SUSPENDED:
537 case SCI_RNC_AWAIT_SUSPENSION:
Jeff Skirvin59e35392012-03-08 22:41:54 -0800538 sci_remote_node_context_setup_to_destroy(sci_rnc, cb_fn, cb_p);
Edmund Nadolskie3013702011-06-02 00:10:43 +0000539 sci_change_state(&sci_rnc->sm, SCI_RNC_INVALIDATING);
Dan Williamsc845ae92011-05-12 08:26:56 -0700540 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000541 case SCI_RNC_INITIAL:
Dan Williamsc845ae92011-05-12 08:26:56 -0700542 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
Dan Williams14e99b42012-02-10 01:05:43 -0800543 "%s: invalid state: %s\n", __func__,
544 rnc_state_name(state));
Dan Williamsc845ae92011-05-12 08:26:56 -0700545 /* We have decided that the destruct request on the remote node context
546 * can not fail since it is either in the initial/destroyed state or is
547 * can be destroyed.
548 */
549 return SCI_SUCCESS;
550 default:
551 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 return SCI_FAILURE_INVALID_STATE;
555 }
556}
Dan Williamsed3efb72011-05-12 08:50:23 -0700557
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800558enum sci_status sci_remote_node_context_suspend(
559 struct sci_remote_node_context *sci_rnc,
560 enum sci_remote_node_suspension_reasons suspend_reason,
Jeff Skirvin447bfbc2012-03-08 22:41:59 -0800561 u32 suspend_type)
Dan Williamsed3efb72011-05-12 08:50:23 -0700562{
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800563 enum scis_sds_remote_node_context_states state
564 = sci_rnc->sm.current_state_id;
565 struct isci_remote_device *idev = rnc_to_dev(sci_rnc);
566 enum sci_status status = SCI_FAILURE_INVALID_STATE;
Jeff Skirvin59e35392012-03-08 22:41:54 -0800567 enum sci_remote_node_context_destination_state dest_param =
568 RNC_DEST_UNSPECIFIED;
Dan Williamsed3efb72011-05-12 08:50:23 -0700569
Jeff Skirvin726980d2012-03-08 22:41:50 -0800570 dev_dbg(scirdev_to_dev(idev),
Jeff Skirvin8c731882012-03-08 22:42:06 -0800571 "%s: current state %s, current suspend_type %x dest state %d,"
Jeff Skirvin726980d2012-03-08 22:41:50 -0800572 " arg suspend_reason %d, arg suspend_type %x",
Jeff Skirvin8c731882012-03-08 22:42:06 -0800573 __func__, rnc_state_name(state), sci_rnc->suspend_type,
Jeff Skirvin726980d2012-03-08 22:41:50 -0800574 sci_rnc->destination_state, suspend_reason,
575 suspend_type);
576
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800577 /* Disable automatic state continuations if explicitly suspending. */
Jeff Skirvinc94fc1a2012-03-08 22:41:58 -0800578 if ((suspend_reason == SCI_HW_SUSPEND) ||
Jeff Skirvin59e35392012-03-08 22:41:54 -0800579 (sci_rnc->destination_state == RNC_DEST_FINAL))
580 dest_param = sci_rnc->destination_state;
581
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800582 switch (state) {
583 case SCI_RNC_READY:
584 break;
Jeff Skirvin59e35392012-03-08 22:41:54 -0800585 case SCI_RNC_INVALIDATING:
586 if (sci_rnc->destination_state == RNC_DEST_FINAL) {
587 dev_warn(scirdev_to_dev(idev),
588 "%s: already destroying %p\n",
589 __func__, sci_rnc);
590 return SCI_FAILURE_INVALID_STATE;
591 }
592 /* Fall through and handle like SCI_RNC_POSTING */
Jeff Skirvind76689e2012-03-08 22:42:04 -0800593 case SCI_RNC_RESUMING:
594 /* Fall through and handle like SCI_RNC_POSTING */
Jeff Skirvin59e35392012-03-08 22:41:54 -0800595 case SCI_RNC_POSTING:
596 /* Set the destination state to AWAIT - this signals the
597 * entry into the SCI_RNC_READY state that a suspension
598 * needs to be done immediately.
599 */
600 sci_rnc->destination_state = RNC_DEST_SUSPENDED;
Jeff Skirvin447bfbc2012-03-08 22:41:59 -0800601 sci_rnc->suspend_type = suspend_type;
602 sci_rnc->suspend_reason = suspend_reason;
Jeff Skirvin59e35392012-03-08 22:41:54 -0800603 return SCI_SUCCESS;
604
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800605 case SCI_RNC_TX_SUSPENDED:
606 if (suspend_type == SCU_EVENT_TL_RNC_SUSPEND_TX)
607 status = SCI_SUCCESS;
608 break;
609 case SCI_RNC_TX_RX_SUSPENDED:
610 if (suspend_type == SCU_EVENT_TL_RNC_SUSPEND_TX_RX)
611 status = SCI_SUCCESS;
612 break;
613 case SCI_RNC_AWAIT_SUSPENSION:
614 if ((sci_rnc->suspend_type == SCU_EVENT_TL_RNC_SUSPEND_TX_RX)
615 || (suspend_type == sci_rnc->suspend_type))
616 return SCI_SUCCESS;
617 break;
618 default:
Dan Williamsed3efb72011-05-12 08:50:23 -0700619 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
Dan Williams14e99b42012-02-10 01:05:43 -0800620 "%s: invalid state %s\n", __func__,
621 rnc_state_name(state));
Dan Williamsed3efb72011-05-12 08:50:23 -0700622 return SCI_FAILURE_INVALID_STATE;
623 }
Jeff Skirvin59e35392012-03-08 22:41:54 -0800624 sci_rnc->destination_state = dest_param;
Jeff Skirvin447bfbc2012-03-08 22:41:59 -0800625 sci_rnc->suspend_type = suspend_type;
626 sci_rnc->suspend_reason = suspend_reason;
Dan Williamsed3efb72011-05-12 08:50:23 -0700627
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800628 if (status == SCI_SUCCESS) { /* Already in the destination state? */
Jeff Skirvin726980d2012-03-08 22:41:50 -0800629 struct isci_host *ihost = idev->owning_port->owning_controller;
630
Jeff Skirvin726980d2012-03-08 22:41:50 -0800631 wake_up_all(&ihost->eventq); /* Let observers look. */
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800632 return SCI_SUCCESS;
Dan Williamsed3efb72011-05-12 08:50:23 -0700633 }
Jeff Skirvinc94fc1a2012-03-08 22:41:58 -0800634 if ((suspend_reason == SCI_SW_SUSPEND_NORMAL) ||
635 (suspend_reason == SCI_SW_SUSPEND_LINKHANG_DETECT)) {
636
Jeff Skirvin9608b642012-03-08 22:42:00 -0800637 if (suspend_reason == SCI_SW_SUSPEND_LINKHANG_DETECT)
Jeff Skirvinc94fc1a2012-03-08 22:41:58 -0800638 isci_dev_set_hang_detection_timeout(idev, 0x00000001);
639
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800640 sci_remote_device_post_request(
641 idev, SCI_SOFTWARE_SUSPEND_CMD);
642 }
643 if (state != SCI_RNC_AWAIT_SUSPENSION)
644 sci_change_state(&sci_rnc->sm, SCI_RNC_AWAIT_SUSPENSION);
Dan Williamsed3efb72011-05-12 08:50:23 -0700645
Dan Williamsed3efb72011-05-12 08:50:23 -0700646 return SCI_SUCCESS;
647}
648
Dan Williams89a73012011-06-30 19:14:33 -0700649enum sci_status sci_remote_node_context_resume(struct sci_remote_node_context *sci_rnc,
Dan Williamsed3efb72011-05-12 08:50:23 -0700650 scics_sds_remote_node_context_callback cb_fn,
651 void *cb_p)
652{
653 enum scis_sds_remote_node_context_states state;
Jeff Skirvin0c3ce382012-03-08 22:42:02 -0800654 struct isci_remote_device *idev = rnc_to_dev(sci_rnc);
Dan Williamsed3efb72011-05-12 08:50:23 -0700655
Edmund Nadolskie3013702011-06-02 00:10:43 +0000656 state = sci_rnc->sm.current_state_id;
Jeff Skirvin0c3ce382012-03-08 22:42:02 -0800657 dev_dbg(scirdev_to_dev(idev),
658 "%s: state %s, cb_fn = %p, cb_p = %p; dest_state = %d; "
659 "dev resume path %s\n",
660 __func__, rnc_state_name(state), cb_fn, cb_p,
661 sci_rnc->destination_state,
662 test_bit(IDEV_ABORT_PATH_ACTIVE, &idev->flags)
663 ? "<abort active>" : "<normal>");
Jeff Skirvin31a38ef2012-03-08 22:42:01 -0800664
Dan Williamsed3efb72011-05-12 08:50:23 -0700665 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000666 case SCI_RNC_INITIAL:
Dan Williamsed3efb72011-05-12 08:50:23 -0700667 if (sci_rnc->remote_node_index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX)
668 return SCI_FAILURE_INVALID_STATE;
669
Jeff Skirvin0c3ce382012-03-08 22:42:02 -0800670 if (test_bit(IDEV_ABORT_PATH_ACTIVE, &idev->flags))
671 sci_remote_node_context_save_cbparams(sci_rnc, cb_fn,
672 cb_p);
673 else {
674 sci_remote_node_context_setup_to_resume(sci_rnc, cb_fn,
675 cb_p, RNC_DEST_READY);
676 sci_remote_node_context_construct_buffer(sci_rnc);
677 sci_change_state(&sci_rnc->sm, SCI_RNC_POSTING);
678 }
Dan Williamsed3efb72011-05-12 08:50:23 -0700679 return SCI_SUCCESS;
Jeff Skirvin0c3ce382012-03-08 22:42:02 -0800680
Edmund Nadolskie3013702011-06-02 00:10:43 +0000681 case SCI_RNC_POSTING:
682 case SCI_RNC_INVALIDATING:
683 case SCI_RNC_RESUMING:
Jeff Skirvin0c3ce382012-03-08 22:42:02 -0800684 if (test_bit(IDEV_ABORT_PATH_ACTIVE, &idev->flags))
685 sci_remote_node_context_save_cbparams(sci_rnc, cb_fn,
686 cb_p);
687 else {
688 /* We are still waiting to post when a resume was
689 * requested.
Jeff Skirvin59e35392012-03-08 22:41:54 -0800690 */
Jeff Skirvin0c3ce382012-03-08 22:42:02 -0800691 switch (sci_rnc->destination_state) {
692 case RNC_DEST_SUSPENDED:
693 case RNC_DEST_SUSPENDED_RESUME:
694 /* Previously waiting to suspend after posting.
695 * Now continue onto resumption.
696 */
697 sci_remote_node_context_setup_to_resume(
698 sci_rnc, cb_fn, cb_p,
699 RNC_DEST_SUSPENDED_RESUME);
700 break;
701 default:
702 sci_remote_node_context_setup_to_resume(
703 sci_rnc, cb_fn, cb_p,
704 RNC_DEST_READY);
705 break;
706 }
Jeff Skirvin59e35392012-03-08 22:41:54 -0800707 }
Dan Williamsed3efb72011-05-12 08:50:23 -0700708 return SCI_SUCCESS;
Jeff Skirvin0c3ce382012-03-08 22:42:02 -0800709
Jeff Skirvin56d7c012012-03-08 22:41:49 -0800710 case SCI_RNC_TX_SUSPENDED:
Jeff Skirvin0c3ce382012-03-08 22:42:02 -0800711 case SCI_RNC_TX_RX_SUSPENDED:
712 if (test_bit(IDEV_ABORT_PATH_ACTIVE, &idev->flags))
713 sci_remote_node_context_save_cbparams(sci_rnc, cb_fn,
714 cb_p);
715 else {
716 struct domain_device *dev = idev->domain_dev;
717 /* If this is an expander attached SATA device we must
718 * invalidate and repost the RNC since this is the only
719 * way to clear the TCi to NCQ tag mapping table for
720 * the RNi. All other device types we can just resume.
721 */
722 sci_remote_node_context_setup_to_resume(
723 sci_rnc, cb_fn, cb_p, RNC_DEST_READY);
Dan Williamsed3efb72011-05-12 08:50:23 -0700724
Jeff Skirvin0c3ce382012-03-08 22:42:02 -0800725 if (dev_is_sata(dev) && dev->parent)
726 sci_change_state(&sci_rnc->sm,
727 SCI_RNC_INVALIDATING);
728 else
729 sci_change_state(&sci_rnc->sm,
730 SCI_RNC_RESUMING);
731 }
Dan Williamsed3efb72011-05-12 08:50:23 -0700732 return SCI_SUCCESS;
Jeff Skirvin0c3ce382012-03-08 22:42:02 -0800733
Edmund Nadolskie3013702011-06-02 00:10:43 +0000734 case SCI_RNC_AWAIT_SUSPENSION:
Jeff Skirvin0c3ce382012-03-08 22:42:02 -0800735 if (test_bit(IDEV_ABORT_PATH_ACTIVE, &idev->flags))
736 sci_remote_node_context_save_cbparams(sci_rnc, cb_fn,
737 cb_p);
738 else
739 sci_remote_node_context_setup_to_resume(
740 sci_rnc, cb_fn, cb_p,
741 RNC_DEST_SUSPENDED_RESUME);
Dan Williamsed3efb72011-05-12 08:50:23 -0700742 return SCI_SUCCESS;
743 default:
744 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
Dan Williams14e99b42012-02-10 01:05:43 -0800745 "%s: invalid state %s\n", __func__,
746 rnc_state_name(state));
Dan Williamsed3efb72011-05-12 08:50:23 -0700747 return SCI_FAILURE_INVALID_STATE;
748 }
749}
Dan Williamsf34d9e52011-05-12 09:27:52 -0700750
Dan Williams89a73012011-06-30 19:14:33 -0700751enum sci_status sci_remote_node_context_start_io(struct sci_remote_node_context *sci_rnc,
Dan Williams5076a1a2011-06-27 14:57:03 -0700752 struct isci_request *ireq)
Dan Williamsf34d9e52011-05-12 09:27:52 -0700753{
754 enum scis_sds_remote_node_context_states state;
755
Edmund Nadolskie3013702011-06-02 00:10:43 +0000756 state = sci_rnc->sm.current_state_id;
Jeff Skirvinfd536602011-06-20 14:09:22 -0700757
758 switch (state) {
759 case SCI_RNC_READY:
760 return SCI_SUCCESS;
761 case SCI_RNC_TX_SUSPENDED:
762 case SCI_RNC_TX_RX_SUSPENDED:
763 case SCI_RNC_AWAIT_SUSPENSION:
Dan Williamsf34d9e52011-05-12 09:27:52 -0700764 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
Dan Williams14e99b42012-02-10 01:05:43 -0800765 "%s: invalid state %s\n", __func__,
766 rnc_state_name(state));
Dan Williamsf34d9e52011-05-12 09:27:52 -0700767 return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
Jeff Skirvinfd536602011-06-20 14:09:22 -0700768 default:
Dan Williams14e99b42012-02-10 01:05:43 -0800769 dev_dbg(scirdev_to_dev(rnc_to_dev(sci_rnc)),
770 "%s: invalid state %s\n", __func__,
771 rnc_state_name(state));
772 return SCI_FAILURE_INVALID_STATE;
Dan Williamsf34d9e52011-05-12 09:27:52 -0700773 }
Dan Williamsf34d9e52011-05-12 09:27:52 -0700774}
775
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -0800776enum sci_status sci_remote_node_context_start_task(
777 struct sci_remote_node_context *sci_rnc,
778 struct isci_request *ireq,
779 scics_sds_remote_node_context_callback cb_fn,
780 void *cb_p)
Dan Williamsf34d9e52011-05-12 09:27:52 -0700781{
Jeff Skirvin59e35392012-03-08 22:41:54 -0800782 enum sci_status status = sci_remote_node_context_resume(sci_rnc,
783 cb_fn, cb_p);
784 if (status != SCI_SUCCESS)
Dan Williamsf34d9e52011-05-12 09:27:52 -0700785 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
Jeff Skirvin59e35392012-03-08 22:41:54 -0800786 "%s: resume failed: %d\n", __func__, status);
787 return status;
Dan Williamsf34d9e52011-05-12 09:27:52 -0700788}
Jeff Skirvin726980d2012-03-08 22:41:50 -0800789
790int sci_remote_node_context_is_safe_to_abort(
791 struct sci_remote_node_context *sci_rnc)
792{
793 enum scis_sds_remote_node_context_states state;
794
795 state = sci_rnc->sm.current_state_id;
796 switch (state) {
797 case SCI_RNC_INVALIDATING:
798 case SCI_RNC_TX_RX_SUSPENDED:
799 return 1;
800 case SCI_RNC_POSTING:
801 case SCI_RNC_RESUMING:
802 case SCI_RNC_READY:
803 case SCI_RNC_TX_SUSPENDED:
804 case SCI_RNC_AWAIT_SUSPENSION:
805 case SCI_RNC_INITIAL:
806 return 0;
807 default:
808 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
809 "%s: invalid state %d\n", __func__, state);
810 return 0;
811 }
812}