blob: 77c8b5138b7ef69e65ed84f9eb0f808bf265c25a [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}
163
164/**
165 *
Dave Jiange2023b82011-04-21 05:34:49 +0000166 * @sci_rnc:
167 * @callback:
Dan Williams6f231dd2011-07-02 22:56:22 -0700168 * @callback_parameter:
169 *
170 * This method will setup the remote node context object so it will transition
171 * to its ready state. If the remote node context is already setup to
172 * transition to its final state then this function does nothing. none
173 */
Dan Williams89a73012011-06-30 19:14:33 -0700174static void sci_remote_node_context_setup_to_resume(
175 struct sci_remote_node_context *sci_rnc,
Dave Jiange2023b82011-04-21 05:34:49 +0000176 scics_sds_remote_node_context_callback callback,
Jeff Skirvin59e35392012-03-08 22:41:54 -0800177 void *callback_parameter,
178 enum sci_remote_node_context_destination_state dest_param)
Dan Williams6f231dd2011-07-02 22:56:22 -0700179{
Jeff Skirvin59e35392012-03-08 22:41:54 -0800180 if (sci_rnc->destination_state != RNC_DEST_FINAL) {
181 sci_rnc->destination_state = dest_param;
182 if (callback != NULL) {
183 sci_rnc->user_callback = callback;
184 sci_rnc->user_cookie = callback_parameter;
185 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700186 }
187}
188
Jeff Skirvin59e35392012-03-08 22:41:54 -0800189static void sci_remote_node_context_setup_to_destroy(
Dan Williams89a73012011-06-30 19:14:33 -0700190 struct sci_remote_node_context *sci_rnc,
Dave Jiange2023b82011-04-21 05:34:49 +0000191 scics_sds_remote_node_context_callback callback,
Dan Williams6f231dd2011-07-02 22:56:22 -0700192 void *callback_parameter)
193{
Jeff Skirvin59e35392012-03-08 22:41:54 -0800194 sci_rnc->destination_state = RNC_DEST_FINAL;
Dave Jiange2023b82011-04-21 05:34:49 +0000195 sci_rnc->user_callback = callback;
196 sci_rnc->user_cookie = callback_parameter;
Dan Williams6f231dd2011-07-02 22:56:22 -0700197}
198
Dan Williams6f231dd2011-07-02 22:56:22 -0700199/**
200 *
201 *
202 * This method just calls the user callback function and then resets the
203 * callback.
204 */
Dan Williams89a73012011-06-30 19:14:33 -0700205static void sci_remote_node_context_notify_user(
206 struct sci_remote_node_context *rnc)
Dan Williams6f231dd2011-07-02 22:56:22 -0700207{
208 if (rnc->user_callback != NULL) {
209 (*rnc->user_callback)(rnc->user_cookie);
210
211 rnc->user_callback = NULL;
212 rnc->user_cookie = NULL;
213 }
214}
215
Dan Williams89a73012011-06-30 19:14:33 -0700216static void sci_remote_node_context_continue_state_transitions(struct sci_remote_node_context *rnc)
Dan Williams6f231dd2011-07-02 22:56:22 -0700217{
Jeff Skirvin59e35392012-03-08 22:41:54 -0800218 if ((rnc->destination_state == RNC_DEST_READY) ||
219 (rnc->destination_state == RNC_DEST_SUSPENDED_RESUME)) {
220 rnc->destination_state = RNC_DEST_READY;
Dan Williams89a73012011-06-30 19:14:33 -0700221 sci_remote_node_context_resume(rnc, rnc->user_callback,
Dan Williamsed3efb72011-05-12 08:50:23 -0700222 rnc->user_cookie);
Jeff Skirvin59e35392012-03-08 22:41:54 -0800223 } else
224 rnc->destination_state = RNC_DEST_UNSPECIFIED;
Dan Williams6f231dd2011-07-02 22:56:22 -0700225}
226
Dan Williams89a73012011-06-30 19:14:33 -0700227static void sci_remote_node_context_validate_context_buffer(struct sci_remote_node_context *sci_rnc)
Dan Williams6f231dd2011-07-02 22:56:22 -0700228{
Dan Williams89a73012011-06-30 19:14:33 -0700229 union scu_remote_node_context *rnc_buffer;
Dan Williams78a6f062011-06-30 16:31:37 -0700230 struct isci_remote_device *idev = rnc_to_dev(sci_rnc);
231 struct domain_device *dev = idev->domain_dev;
Dan Williams89a73012011-06-30 19:14:33 -0700232 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williams6f231dd2011-07-02 22:56:22 -0700233
Dan Williams89a73012011-06-30 19:14:33 -0700234 rnc_buffer = sci_rnc_by_id(ihost, sci_rnc->remote_node_index);
Dan Williams6f231dd2011-07-02 22:56:22 -0700235
236 rnc_buffer->ssp.is_valid = true;
237
Dan Williams11cc5182012-02-01 00:23:10 -0800238 if (dev_is_sata(dev) && dev->parent) {
Dan Williams89a73012011-06-30 19:14:33 -0700239 sci_remote_device_post_request(idev, SCU_CONTEXT_COMMAND_POST_RNC_96);
Dan Williams6f231dd2011-07-02 22:56:22 -0700240 } else {
Dan Williams89a73012011-06-30 19:14:33 -0700241 sci_remote_device_post_request(idev, SCU_CONTEXT_COMMAND_POST_RNC_32);
Dan Williams6f231dd2011-07-02 22:56:22 -0700242
Dan Williams11cc5182012-02-01 00:23:10 -0800243 if (!dev->parent)
Dan Williams89a73012011-06-30 19:14:33 -0700244 sci_port_setup_transports(idev->owning_port,
245 sci_rnc->remote_node_index);
Dan Williams6f231dd2011-07-02 22:56:22 -0700246 }
247}
248
Dan Williams89a73012011-06-30 19:14:33 -0700249static void sci_remote_node_context_invalidate_context_buffer(struct sci_remote_node_context *sci_rnc)
Dan Williams6f231dd2011-07-02 22:56:22 -0700250{
251 union scu_remote_node_context *rnc_buffer;
Dan Williams89a73012011-06-30 19:14:33 -0700252 struct isci_remote_device *idev = rnc_to_dev(sci_rnc);
253 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williams6f231dd2011-07-02 22:56:22 -0700254
Dan Williams89a73012011-06-30 19:14:33 -0700255 rnc_buffer = sci_rnc_by_id(ihost, sci_rnc->remote_node_index);
Dan Williams6f231dd2011-07-02 22:56:22 -0700256
257 rnc_buffer->ssp.is_valid = false;
258
Dan Williams89a73012011-06-30 19:14:33 -0700259 sci_remote_device_post_request(rnc_to_dev(sci_rnc),
260 SCU_CONTEXT_COMMAND_POST_RNC_INVALIDATE);
Dan Williams6f231dd2011-07-02 22:56:22 -0700261}
262
Dan Williams89a73012011-06-30 19:14:33 -0700263static void sci_remote_node_context_initial_state_enter(struct sci_base_state_machine *sm)
Dan Williams6f231dd2011-07-02 22:56:22 -0700264{
Dan Williams89a73012011-06-30 19:14:33 -0700265 struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
Dan Williams6f231dd2011-07-02 22:56:22 -0700266
Dan Williamsf34d9e52011-05-12 09:27:52 -0700267 /* Check to see if we have gotten back to the initial state because
268 * someone requested to destroy the remote node context object.
269 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000270 if (sm->previous_state_id == SCI_RNC_INVALIDATING) {
Jeff Skirvin59e35392012-03-08 22:41:54 -0800271 rnc->destination_state = RNC_DEST_UNSPECIFIED;
Dan Williams89a73012011-06-30 19:14:33 -0700272 sci_remote_node_context_notify_user(rnc);
Dan Williams6f231dd2011-07-02 22:56:22 -0700273 }
274}
275
Dan Williams89a73012011-06-30 19:14:33 -0700276static void sci_remote_node_context_posting_state_enter(struct sci_base_state_machine *sm)
Dan Williams6f231dd2011-07-02 22:56:22 -0700277{
Dan Williams89a73012011-06-30 19:14:33 -0700278 struct sci_remote_node_context *sci_rnc = container_of(sm, typeof(*sci_rnc), sm);
Dan Williams6f231dd2011-07-02 22:56:22 -0700279
Dan Williams89a73012011-06-30 19:14:33 -0700280 sci_remote_node_context_validate_context_buffer(sci_rnc);
Dan Williams6f231dd2011-07-02 22:56:22 -0700281}
282
Dan Williams89a73012011-06-30 19:14:33 -0700283static void sci_remote_node_context_invalidating_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 *rnc = container_of(sm, typeof(*rnc), sm);
Dan Williams6f231dd2011-07-02 22:56:22 -0700286
Jeff Skirvinaa20d932012-03-08 22:41:56 -0800287 /* Terminate all outstanding requests. */
288 sci_remote_device_terminate_requests(rnc_to_dev(rnc));
Dan Williams89a73012011-06-30 19:14:33 -0700289 sci_remote_node_context_invalidate_context_buffer(rnc);
Dan Williams6f231dd2011-07-02 22:56:22 -0700290}
291
Dan Williams89a73012011-06-30 19:14:33 -0700292static void sci_remote_node_context_resuming_state_enter(struct sci_base_state_machine *sm)
Dan Williams6f231dd2011-07-02 22:56:22 -0700293{
Dan Williams89a73012011-06-30 19:14:33 -0700294 struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
Dan Williams78a6f062011-06-30 16:31:37 -0700295 struct isci_remote_device *idev;
Dan Williamsa1a113b2011-04-21 18:44:45 -0700296 struct domain_device *dev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700297
Dan Williams78a6f062011-06-30 16:31:37 -0700298 idev = rnc_to_dev(rnc);
299 dev = idev->domain_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700300
Henryk Dembkowski24621462011-02-23 00:08:52 -0800301 /*
302 * For direct attached SATA devices we need to clear the TLCR
303 * NCQ to TCi tag mapping on the phy and in cases where we
304 * resume because of a target reset we also need to update
305 * the STPTLDARNI register with the RNi of the device
306 */
Dan Williams11cc5182012-02-01 00:23:10 -0800307 if (dev_is_sata(dev) && !dev->parent)
308 sci_port_setup_transports(idev->owning_port, rnc->remote_node_index);
Henryk Dembkowski24621462011-02-23 00:08:52 -0800309
Dan Williams89a73012011-06-30 19:14:33 -0700310 sci_remote_device_post_request(idev, SCU_CONTEXT_COMMAND_POST_RNC_RESUME);
Dan Williams6f231dd2011-07-02 22:56:22 -0700311}
312
Dan Williams89a73012011-06-30 19:14:33 -0700313static void sci_remote_node_context_ready_state_enter(struct sci_base_state_machine *sm)
Dan Williams6f231dd2011-07-02 22:56:22 -0700314{
Dan Williams89a73012011-06-30 19:14:33 -0700315 struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
Jeff Skirvin59e35392012-03-08 22:41:54 -0800316 enum sci_remote_node_context_destination_state dest_select;
317 scics_sds_remote_node_context_callback usr_cb = rnc->user_callback;
318 void *usr_param = rnc->user_cookie;
319 int tell_user = 1;
Dan Williams6f231dd2011-07-02 22:56:22 -0700320
Jeff Skirvin59e35392012-03-08 22:41:54 -0800321 dest_select = rnc->destination_state;
322 rnc->destination_state = RNC_DEST_UNSPECIFIED;
Dan Williams6f231dd2011-07-02 22:56:22 -0700323
Jeff Skirvin59e35392012-03-08 22:41:54 -0800324 if ((dest_select == RNC_DEST_SUSPENDED) ||
325 (dest_select == RNC_DEST_SUSPENDED_RESUME)) {
326 sci_remote_node_context_suspend(
Jeff Skirvin447bfbc2012-03-08 22:41:59 -0800327 rnc, rnc->suspend_reason,
328 SCI_SOFTWARE_SUSPEND_EXPECTED_EVENT);
Jeff Skirvin59e35392012-03-08 22:41:54 -0800329
330 if (dest_select == RNC_DEST_SUSPENDED_RESUME) {
331 sci_remote_node_context_resume(rnc, usr_cb, usr_param);
332 tell_user = 0; /* Wait until ready again. */
333 }
334 }
335 if (tell_user && rnc->user_callback)
Dan Williams89a73012011-06-30 19:14:33 -0700336 sci_remote_node_context_notify_user(rnc);
Dan Williams6f231dd2011-07-02 22:56:22 -0700337}
338
Dan Williams89a73012011-06-30 19:14:33 -0700339static void sci_remote_node_context_tx_suspended_state_enter(struct sci_base_state_machine *sm)
Dan Williams6f231dd2011-07-02 22:56:22 -0700340{
Dan Williams89a73012011-06-30 19:14:33 -0700341 struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
Dan Williams6f231dd2011-07-02 22:56:22 -0700342
Dan Williams89a73012011-06-30 19:14:33 -0700343 sci_remote_node_context_continue_state_transitions(rnc);
Dan Williams6f231dd2011-07-02 22:56:22 -0700344}
345
Dan Williams89a73012011-06-30 19:14:33 -0700346static void sci_remote_node_context_tx_rx_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);
Jeff Skirvin726980d2012-03-08 22:41:50 -0800349 struct isci_remote_device *idev = rnc_to_dev(rnc);
350 struct isci_host *ihost = idev->owning_port->owning_controller;
Jeff Skirvin31a38ef2012-03-08 22:42:01 -0800351 u32 new_count = rnc->suspend_count + 1;
352
353 if (new_count == 0)
354 rnc->suspend_count = 1;
355 else
356 rnc->suspend_count = new_count;
357 smp_wmb();
Dan Williams6f231dd2011-07-02 22:56:22 -0700358
Jeff Skirvin726980d2012-03-08 22:41:50 -0800359 /* Terminate outstanding requests pending abort. */
360 sci_remote_device_abort_requests_pending_abort(idev);
361
362 wake_up(&ihost->eventq);
Dan Williams89a73012011-06-30 19:14:33 -0700363 sci_remote_node_context_continue_state_transitions(rnc);
Dan Williams6f231dd2011-07-02 22:56:22 -0700364}
365
Jeff Skirvin6f488442012-03-08 22:41:48 -0800366static void sci_remote_node_context_await_suspend_state_exit(
367 struct sci_base_state_machine *sm)
368{
369 struct sci_remote_node_context *rnc
370 = container_of(sm, typeof(*rnc), sm);
Jeff Skirvinc94fc1a2012-03-08 22:41:58 -0800371 struct isci_remote_device *idev = rnc_to_dev(rnc);
Jeff Skirvin6f488442012-03-08 22:41:48 -0800372
Jeff Skirvinc94fc1a2012-03-08 22:41:58 -0800373 if (dev_is_sata(idev->domain_dev))
374 isci_dev_set_hang_detection_timeout(idev, 0);
Jeff Skirvin6f488442012-03-08 22:41:48 -0800375}
376
Dan Williams89a73012011-06-30 19:14:33 -0700377static const struct sci_base_state sci_remote_node_context_state_table[] = {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000378 [SCI_RNC_INITIAL] = {
Dan Williams89a73012011-06-30 19:14:33 -0700379 .enter_state = sci_remote_node_context_initial_state_enter,
Dan Williams6f231dd2011-07-02 22:56:22 -0700380 },
Edmund Nadolskie3013702011-06-02 00:10:43 +0000381 [SCI_RNC_POSTING] = {
Dan Williams89a73012011-06-30 19:14:33 -0700382 .enter_state = sci_remote_node_context_posting_state_enter,
Dan Williams6f231dd2011-07-02 22:56:22 -0700383 },
Edmund Nadolskie3013702011-06-02 00:10:43 +0000384 [SCI_RNC_INVALIDATING] = {
Dan Williams89a73012011-06-30 19:14:33 -0700385 .enter_state = sci_remote_node_context_invalidating_state_enter,
Dan Williams6f231dd2011-07-02 22:56:22 -0700386 },
Edmund Nadolskie3013702011-06-02 00:10:43 +0000387 [SCI_RNC_RESUMING] = {
Dan Williams89a73012011-06-30 19:14:33 -0700388 .enter_state = sci_remote_node_context_resuming_state_enter,
Dan Williams6f231dd2011-07-02 22:56:22 -0700389 },
Edmund Nadolskie3013702011-06-02 00:10:43 +0000390 [SCI_RNC_READY] = {
Dan Williams89a73012011-06-30 19:14:33 -0700391 .enter_state = sci_remote_node_context_ready_state_enter,
Dan Williams6f231dd2011-07-02 22:56:22 -0700392 },
Edmund Nadolskie3013702011-06-02 00:10:43 +0000393 [SCI_RNC_TX_SUSPENDED] = {
Dan Williams89a73012011-06-30 19:14:33 -0700394 .enter_state = sci_remote_node_context_tx_suspended_state_enter,
Dan Williams6f231dd2011-07-02 22:56:22 -0700395 },
Edmund Nadolskie3013702011-06-02 00:10:43 +0000396 [SCI_RNC_TX_RX_SUSPENDED] = {
Dan Williams89a73012011-06-30 19:14:33 -0700397 .enter_state = sci_remote_node_context_tx_rx_suspended_state_enter,
Dan Williams6f231dd2011-07-02 22:56:22 -0700398 },
Jeff Skirvin6f488442012-03-08 22:41:48 -0800399 [SCI_RNC_AWAIT_SUSPENSION] = {
400 .exit_state = sci_remote_node_context_await_suspend_state_exit,
401 },
Dan Williams6f231dd2011-07-02 22:56:22 -0700402};
403
Dan Williams89a73012011-06-30 19:14:33 -0700404void sci_remote_node_context_construct(struct sci_remote_node_context *rnc,
Dan Williams96143952011-04-19 18:35:58 -0700405 u16 remote_node_index)
Dan Williams35173d52011-03-26 16:43:01 -0700406{
Dan Williams89a73012011-06-30 19:14:33 -0700407 memset(rnc, 0, sizeof(struct sci_remote_node_context));
Dan Williams35173d52011-03-26 16:43:01 -0700408
409 rnc->remote_node_index = remote_node_index;
Jeff Skirvin59e35392012-03-08 22:41:54 -0800410 rnc->destination_state = RNC_DEST_UNSPECIFIED;
Dan Williams35173d52011-03-26 16:43:01 -0700411
Dan Williams89a73012011-06-30 19:14:33 -0700412 sci_init_sm(&rnc->sm, sci_remote_node_context_state_table, SCI_RNC_INITIAL);
Dan Williams35173d52011-03-26 16:43:01 -0700413}
Dan Williams338e3862011-05-12 07:46:59 -0700414
Dan Williams89a73012011-06-30 19:14:33 -0700415enum sci_status sci_remote_node_context_event_handler(struct sci_remote_node_context *sci_rnc,
Dan Williams338e3862011-05-12 07:46:59 -0700416 u32 event_code)
417{
418 enum scis_sds_remote_node_context_states state;
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800419 u32 next_state;
Dan Williams338e3862011-05-12 07:46:59 -0700420
Edmund Nadolskie3013702011-06-02 00:10:43 +0000421 state = sci_rnc->sm.current_state_id;
Dan Williams338e3862011-05-12 07:46:59 -0700422 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000423 case SCI_RNC_POSTING:
Dan Williams338e3862011-05-12 07:46:59 -0700424 switch (scu_get_event_code(event_code)) {
425 case SCU_EVENT_POST_RNC_COMPLETE:
Edmund Nadolskie3013702011-06-02 00:10:43 +0000426 sci_change_state(&sci_rnc->sm, SCI_RNC_READY);
Dan Williams338e3862011-05-12 07:46:59 -0700427 break;
428 default:
429 goto out;
430 }
431 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000432 case SCI_RNC_INVALIDATING:
Dan Williams338e3862011-05-12 07:46:59 -0700433 if (scu_get_event_code(event_code) == SCU_EVENT_POST_RNC_INVALIDATE_COMPLETE) {
Jeff Skirvin59e35392012-03-08 22:41:54 -0800434 if (sci_rnc->destination_state == RNC_DEST_FINAL)
435 next_state = SCI_RNC_INITIAL;
Dan Williams338e3862011-05-12 07:46:59 -0700436 else
Jeff Skirvin59e35392012-03-08 22:41:54 -0800437 next_state = SCI_RNC_POSTING;
438 sci_change_state(&sci_rnc->sm, next_state);
Dan Williams338e3862011-05-12 07:46:59 -0700439 } else {
440 switch (scu_get_event_type(event_code)) {
441 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
442 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
443 /* We really dont care if the hardware is going to suspend
444 * the device since it's being invalidated anyway */
445 dev_dbg(scirdev_to_dev(rnc_to_dev(sci_rnc)),
446 "%s: SCIC Remote Node Context 0x%p was "
447 "suspeneded by hardware while being "
448 "invalidated.\n", __func__, sci_rnc);
449 break;
450 default:
451 goto out;
452 }
453 }
454 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000455 case SCI_RNC_RESUMING:
Dan Williams338e3862011-05-12 07:46:59 -0700456 if (scu_get_event_code(event_code) == SCU_EVENT_POST_RCN_RELEASE) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000457 sci_change_state(&sci_rnc->sm, SCI_RNC_READY);
Dan Williams338e3862011-05-12 07:46:59 -0700458 } else {
459 switch (scu_get_event_type(event_code)) {
460 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
461 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
462 /* We really dont care if the hardware is going to suspend
463 * the device since it's being resumed anyway */
464 dev_dbg(scirdev_to_dev(rnc_to_dev(sci_rnc)),
465 "%s: SCIC Remote Node Context 0x%p was "
466 "suspeneded by hardware while being resumed.\n",
467 __func__, sci_rnc);
468 break;
469 default:
470 goto out;
471 }
472 }
473 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000474 case SCI_RNC_READY:
Dan Williams338e3862011-05-12 07:46:59 -0700475 switch (scu_get_event_type(event_code)) {
476 case SCU_EVENT_TL_RNC_SUSPEND_TX:
Edmund Nadolskie3013702011-06-02 00:10:43 +0000477 sci_change_state(&sci_rnc->sm, SCI_RNC_TX_SUSPENDED);
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800478 sci_rnc->suspend_type = scu_get_event_type(event_code);
Dan Williams338e3862011-05-12 07:46:59 -0700479 break;
480 case SCU_EVENT_TL_RNC_SUSPEND_TX_RX:
Edmund Nadolskie3013702011-06-02 00:10:43 +0000481 sci_change_state(&sci_rnc->sm, SCI_RNC_TX_RX_SUSPENDED);
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800482 sci_rnc->suspend_type = scu_get_event_type(event_code);
Dan Williams338e3862011-05-12 07:46:59 -0700483 break;
484 default:
485 goto out;
486 }
487 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000488 case SCI_RNC_AWAIT_SUSPENSION:
Dan Williams338e3862011-05-12 07:46:59 -0700489 switch (scu_get_event_type(event_code)) {
490 case SCU_EVENT_TL_RNC_SUSPEND_TX:
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800491 next_state = SCI_RNC_TX_SUSPENDED;
Dan Williams338e3862011-05-12 07:46:59 -0700492 break;
493 case SCU_EVENT_TL_RNC_SUSPEND_TX_RX:
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800494 next_state = SCI_RNC_TX_RX_SUSPENDED;
Dan Williams338e3862011-05-12 07:46:59 -0700495 break;
496 default:
497 goto out;
498 }
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800499 if (sci_rnc->suspend_type == scu_get_event_type(event_code))
500 sci_change_state(&sci_rnc->sm, next_state);
Dan Williams338e3862011-05-12 07:46:59 -0700501 break;
502 default:
503 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
Dan Williams14e99b42012-02-10 01:05:43 -0800504 "%s: invalid state: %s\n", __func__,
505 rnc_state_name(state));
Dan Williams338e3862011-05-12 07:46:59 -0700506 return SCI_FAILURE_INVALID_STATE;
507 }
508 return SCI_SUCCESS;
509
510 out:
511 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
Dan Williams14e99b42012-02-10 01:05:43 -0800512 "%s: code: %#x state: %s\n", __func__, event_code,
513 rnc_state_name(state));
Dan Williams338e3862011-05-12 07:46:59 -0700514 return SCI_FAILURE;
515
516}
Dan Williamsc845ae92011-05-12 08:26:56 -0700517
Dan Williams89a73012011-06-30 19:14:33 -0700518enum sci_status sci_remote_node_context_destruct(struct sci_remote_node_context *sci_rnc,
Dan Williamsc845ae92011-05-12 08:26:56 -0700519 scics_sds_remote_node_context_callback cb_fn,
520 void *cb_p)
521{
522 enum scis_sds_remote_node_context_states state;
523
Edmund Nadolskie3013702011-06-02 00:10:43 +0000524 state = sci_rnc->sm.current_state_id;
Dan Williamsc845ae92011-05-12 08:26:56 -0700525 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000526 case SCI_RNC_INVALIDATING:
Jeff Skirvin59e35392012-03-08 22:41:54 -0800527 sci_remote_node_context_setup_to_destroy(sci_rnc, cb_fn, cb_p);
Dan Williamsc845ae92011-05-12 08:26:56 -0700528 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000529 case SCI_RNC_POSTING:
530 case SCI_RNC_RESUMING:
531 case SCI_RNC_READY:
532 case SCI_RNC_TX_SUSPENDED:
533 case SCI_RNC_TX_RX_SUSPENDED:
534 case SCI_RNC_AWAIT_SUSPENSION:
Jeff Skirvin59e35392012-03-08 22:41:54 -0800535 sci_remote_node_context_setup_to_destroy(sci_rnc, cb_fn, cb_p);
Edmund Nadolskie3013702011-06-02 00:10:43 +0000536 sci_change_state(&sci_rnc->sm, SCI_RNC_INVALIDATING);
Dan Williamsc845ae92011-05-12 08:26:56 -0700537 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000538 case SCI_RNC_INITIAL:
Dan Williamsc845ae92011-05-12 08:26:56 -0700539 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
Dan Williams14e99b42012-02-10 01:05:43 -0800540 "%s: invalid state: %s\n", __func__,
541 rnc_state_name(state));
Dan Williamsc845ae92011-05-12 08:26:56 -0700542 /* We have decided that the destruct request on the remote node context
543 * can not fail since it is either in the initial/destroyed state or is
544 * can be destroyed.
545 */
546 return SCI_SUCCESS;
547 default:
548 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
Dan Williams14e99b42012-02-10 01:05:43 -0800549 "%s: invalid state %s\n", __func__,
550 rnc_state_name(state));
Dan Williamsc845ae92011-05-12 08:26:56 -0700551 return SCI_FAILURE_INVALID_STATE;
552 }
553}
Dan Williamsed3efb72011-05-12 08:50:23 -0700554
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800555enum sci_status sci_remote_node_context_suspend(
556 struct sci_remote_node_context *sci_rnc,
557 enum sci_remote_node_suspension_reasons suspend_reason,
Jeff Skirvin447bfbc2012-03-08 22:41:59 -0800558 u32 suspend_type)
Dan Williamsed3efb72011-05-12 08:50:23 -0700559{
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800560 enum scis_sds_remote_node_context_states state
561 = sci_rnc->sm.current_state_id;
562 struct isci_remote_device *idev = rnc_to_dev(sci_rnc);
563 enum sci_status status = SCI_FAILURE_INVALID_STATE;
Jeff Skirvin59e35392012-03-08 22:41:54 -0800564 enum sci_remote_node_context_destination_state dest_param =
565 RNC_DEST_UNSPECIFIED;
Dan Williamsed3efb72011-05-12 08:50:23 -0700566
Jeff Skirvin726980d2012-03-08 22:41:50 -0800567 dev_dbg(scirdev_to_dev(idev),
568 "%s: current state %d, current suspend_type %x dest state %d,"
569 " arg suspend_reason %d, arg suspend_type %x",
570 __func__, state, sci_rnc->suspend_type,
571 sci_rnc->destination_state, suspend_reason,
572 suspend_type);
573
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800574 /* Disable automatic state continuations if explicitly suspending. */
Jeff Skirvinc94fc1a2012-03-08 22:41:58 -0800575 if ((suspend_reason == SCI_HW_SUSPEND) ||
Jeff Skirvin59e35392012-03-08 22:41:54 -0800576 (sci_rnc->destination_state == RNC_DEST_FINAL))
577 dest_param = sci_rnc->destination_state;
578
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800579 switch (state) {
Jeff Skirvin59e35392012-03-08 22:41:54 -0800580 case SCI_RNC_RESUMING:
581 break; /* The RNC has been posted, so start the suspend. */
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800582 case SCI_RNC_READY:
583 break;
Jeff Skirvin59e35392012-03-08 22:41:54 -0800584 case SCI_RNC_INVALIDATING:
585 if (sci_rnc->destination_state == RNC_DEST_FINAL) {
586 dev_warn(scirdev_to_dev(idev),
587 "%s: already destroying %p\n",
588 __func__, sci_rnc);
589 return SCI_FAILURE_INVALID_STATE;
590 }
591 /* Fall through and handle like SCI_RNC_POSTING */
592 case SCI_RNC_POSTING:
593 /* Set the destination state to AWAIT - this signals the
594 * entry into the SCI_RNC_READY state that a suspension
595 * needs to be done immediately.
596 */
597 sci_rnc->destination_state = RNC_DEST_SUSPENDED;
Jeff Skirvin447bfbc2012-03-08 22:41:59 -0800598 sci_rnc->suspend_type = suspend_type;
599 sci_rnc->suspend_reason = suspend_reason;
Jeff Skirvin59e35392012-03-08 22:41:54 -0800600 return SCI_SUCCESS;
601
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800602 case SCI_RNC_TX_SUSPENDED:
603 if (suspend_type == SCU_EVENT_TL_RNC_SUSPEND_TX)
604 status = SCI_SUCCESS;
605 break;
606 case SCI_RNC_TX_RX_SUSPENDED:
607 if (suspend_type == SCU_EVENT_TL_RNC_SUSPEND_TX_RX)
608 status = SCI_SUCCESS;
609 break;
610 case SCI_RNC_AWAIT_SUSPENSION:
611 if ((sci_rnc->suspend_type == SCU_EVENT_TL_RNC_SUSPEND_TX_RX)
612 || (suspend_type == sci_rnc->suspend_type))
613 return SCI_SUCCESS;
614 break;
615 default:
Dan Williamsed3efb72011-05-12 08:50:23 -0700616 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
Dan Williams14e99b42012-02-10 01:05:43 -0800617 "%s: invalid state %s\n", __func__,
618 rnc_state_name(state));
Dan Williamsed3efb72011-05-12 08:50:23 -0700619 return SCI_FAILURE_INVALID_STATE;
620 }
Jeff Skirvin59e35392012-03-08 22:41:54 -0800621 sci_rnc->destination_state = dest_param;
Jeff Skirvin447bfbc2012-03-08 22:41:59 -0800622 sci_rnc->suspend_type = suspend_type;
623 sci_rnc->suspend_reason = suspend_reason;
Dan Williamsed3efb72011-05-12 08:50:23 -0700624
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800625 if (status == SCI_SUCCESS) { /* Already in the destination state? */
Jeff Skirvin726980d2012-03-08 22:41:50 -0800626 struct isci_host *ihost = idev->owning_port->owning_controller;
627
Jeff Skirvin726980d2012-03-08 22:41:50 -0800628 wake_up_all(&ihost->eventq); /* Let observers look. */
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800629 return SCI_SUCCESS;
Dan Williamsed3efb72011-05-12 08:50:23 -0700630 }
Jeff Skirvinc94fc1a2012-03-08 22:41:58 -0800631 if ((suspend_reason == SCI_SW_SUSPEND_NORMAL) ||
632 (suspend_reason == SCI_SW_SUSPEND_LINKHANG_DETECT)) {
633
Jeff Skirvin9608b642012-03-08 22:42:00 -0800634 if (suspend_reason == SCI_SW_SUSPEND_LINKHANG_DETECT)
Jeff Skirvinc94fc1a2012-03-08 22:41:58 -0800635 isci_dev_set_hang_detection_timeout(idev, 0x00000001);
636
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800637 sci_remote_device_post_request(
638 idev, SCI_SOFTWARE_SUSPEND_CMD);
639 }
640 if (state != SCI_RNC_AWAIT_SUSPENSION)
641 sci_change_state(&sci_rnc->sm, SCI_RNC_AWAIT_SUSPENSION);
Dan Williamsed3efb72011-05-12 08:50:23 -0700642
Dan Williamsed3efb72011-05-12 08:50:23 -0700643 return SCI_SUCCESS;
644}
645
Dan Williams89a73012011-06-30 19:14:33 -0700646enum sci_status sci_remote_node_context_resume(struct sci_remote_node_context *sci_rnc,
Dan Williamsed3efb72011-05-12 08:50:23 -0700647 scics_sds_remote_node_context_callback cb_fn,
648 void *cb_p)
649{
650 enum scis_sds_remote_node_context_states state;
651
Edmund Nadolskie3013702011-06-02 00:10:43 +0000652 state = sci_rnc->sm.current_state_id;
Jeff Skirvin31a38ef2012-03-08 22:42:01 -0800653 dev_dbg(scirdev_to_dev(rnc_to_dev(sci_rnc)),
654 "%s: state %s, cb_fn = %p, cb_p = %p; dest_state = %d\n",
655 __func__, rnc_state_name(state), cb_fn, cb_p,
656 sci_rnc->destination_state);
657
Dan Williamsed3efb72011-05-12 08:50:23 -0700658 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000659 case SCI_RNC_INITIAL:
Dan Williamsed3efb72011-05-12 08:50:23 -0700660 if (sci_rnc->remote_node_index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX)
661 return SCI_FAILURE_INVALID_STATE;
662
Jeff Skirvin59e35392012-03-08 22:41:54 -0800663 sci_remote_node_context_setup_to_resume(sci_rnc, cb_fn, cb_p,
664 RNC_DEST_READY);
Dan Williams89a73012011-06-30 19:14:33 -0700665 sci_remote_node_context_construct_buffer(sci_rnc);
Edmund Nadolskie3013702011-06-02 00:10:43 +0000666 sci_change_state(&sci_rnc->sm, SCI_RNC_POSTING);
Dan Williamsed3efb72011-05-12 08:50:23 -0700667 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000668 case SCI_RNC_POSTING:
669 case SCI_RNC_INVALIDATING:
670 case SCI_RNC_RESUMING:
Jeff Skirvin59e35392012-03-08 22:41:54 -0800671 /* We are still waiting to post when a resume was requested. */
672 switch (sci_rnc->destination_state) {
673 case RNC_DEST_SUSPENDED:
674 case RNC_DEST_SUSPENDED_RESUME:
675 /* Previously waiting to suspend after posting. Now
676 * continue onto resumption.
677 */
678 sci_remote_node_context_setup_to_resume(
679 sci_rnc, cb_fn, cb_p,
680 RNC_DEST_SUSPENDED_RESUME);
681 break;
682 default:
683 sci_remote_node_context_setup_to_resume(
684 sci_rnc, cb_fn, cb_p,
685 RNC_DEST_READY);
686 break;
687 }
Dan Williamsed3efb72011-05-12 08:50:23 -0700688 return SCI_SUCCESS;
Jeff Skirvin56d7c012012-03-08 22:41:49 -0800689 case SCI_RNC_TX_SUSPENDED:
690 case SCI_RNC_TX_RX_SUSPENDED: {
Dan Williams78a6f062011-06-30 16:31:37 -0700691 struct isci_remote_device *idev = rnc_to_dev(sci_rnc);
692 struct domain_device *dev = idev->domain_dev;
Dan Williamsed3efb72011-05-12 08:50:23 -0700693
Jeff Skirvin56d7c012012-03-08 22:41:49 -0800694 /* If this is an expander attached SATA device we must
695 * invalidate and repost the RNC since this is the only way
696 * to clear the TCi to NCQ tag mapping table for the RNi.
697 * All other device types we can just resume.
698 */
Jeff Skirvin59e35392012-03-08 22:41:54 -0800699 sci_remote_node_context_setup_to_resume(sci_rnc, cb_fn, cb_p,
700 RNC_DEST_READY);
Dan Williamsed3efb72011-05-12 08:50:23 -0700701
Dan Williams11cc5182012-02-01 00:23:10 -0800702 if (dev_is_sata(dev) && dev->parent)
703 sci_change_state(&sci_rnc->sm, SCI_RNC_INVALIDATING);
704 else
Edmund Nadolskie3013702011-06-02 00:10:43 +0000705 sci_change_state(&sci_rnc->sm, SCI_RNC_RESUMING);
Dan Williamsed3efb72011-05-12 08:50:23 -0700706 return SCI_SUCCESS;
707 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000708 case SCI_RNC_AWAIT_SUSPENSION:
Jeff Skirvin59e35392012-03-08 22:41:54 -0800709 sci_remote_node_context_setup_to_resume(
710 sci_rnc, cb_fn, cb_p,
711 RNC_DEST_SUSPENDED_RESUME);
Dan Williamsed3efb72011-05-12 08:50:23 -0700712 return SCI_SUCCESS;
713 default:
714 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
Dan Williams14e99b42012-02-10 01:05:43 -0800715 "%s: invalid state %s\n", __func__,
716 rnc_state_name(state));
Dan Williamsed3efb72011-05-12 08:50:23 -0700717 return SCI_FAILURE_INVALID_STATE;
718 }
719}
Dan Williamsf34d9e52011-05-12 09:27:52 -0700720
Dan Williams89a73012011-06-30 19:14:33 -0700721enum sci_status sci_remote_node_context_start_io(struct sci_remote_node_context *sci_rnc,
Dan Williams5076a1a2011-06-27 14:57:03 -0700722 struct isci_request *ireq)
Dan Williamsf34d9e52011-05-12 09:27:52 -0700723{
724 enum scis_sds_remote_node_context_states state;
725
Edmund Nadolskie3013702011-06-02 00:10:43 +0000726 state = sci_rnc->sm.current_state_id;
Jeff Skirvinfd536602011-06-20 14:09:22 -0700727
728 switch (state) {
729 case SCI_RNC_READY:
730 return SCI_SUCCESS;
731 case SCI_RNC_TX_SUSPENDED:
732 case SCI_RNC_TX_RX_SUSPENDED:
733 case SCI_RNC_AWAIT_SUSPENSION:
Dan Williamsf34d9e52011-05-12 09:27:52 -0700734 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
Dan Williams14e99b42012-02-10 01:05:43 -0800735 "%s: invalid state %s\n", __func__,
736 rnc_state_name(state));
Dan Williamsf34d9e52011-05-12 09:27:52 -0700737 return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
Jeff Skirvinfd536602011-06-20 14:09:22 -0700738 default:
Dan Williams14e99b42012-02-10 01:05:43 -0800739 dev_dbg(scirdev_to_dev(rnc_to_dev(sci_rnc)),
740 "%s: invalid state %s\n", __func__,
741 rnc_state_name(state));
742 return SCI_FAILURE_INVALID_STATE;
Dan Williamsf34d9e52011-05-12 09:27:52 -0700743 }
Dan Williamsf34d9e52011-05-12 09:27:52 -0700744}
745
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -0800746enum sci_status sci_remote_node_context_start_task(
747 struct sci_remote_node_context *sci_rnc,
748 struct isci_request *ireq,
749 scics_sds_remote_node_context_callback cb_fn,
750 void *cb_p)
Dan Williamsf34d9e52011-05-12 09:27:52 -0700751{
Jeff Skirvin59e35392012-03-08 22:41:54 -0800752 enum sci_status status = sci_remote_node_context_resume(sci_rnc,
753 cb_fn, cb_p);
754 if (status != SCI_SUCCESS)
Dan Williamsf34d9e52011-05-12 09:27:52 -0700755 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
Jeff Skirvin59e35392012-03-08 22:41:54 -0800756 "%s: resume failed: %d\n", __func__, status);
757 return status;
Dan Williamsf34d9e52011-05-12 09:27:52 -0700758}
Jeff Skirvin726980d2012-03-08 22:41:50 -0800759
760int sci_remote_node_context_is_safe_to_abort(
761 struct sci_remote_node_context *sci_rnc)
762{
763 enum scis_sds_remote_node_context_states state;
764
765 state = sci_rnc->sm.current_state_id;
766 switch (state) {
767 case SCI_RNC_INVALIDATING:
768 case SCI_RNC_TX_RX_SUSPENDED:
769 return 1;
770 case SCI_RNC_POSTING:
771 case SCI_RNC_RESUMING:
772 case SCI_RNC_READY:
773 case SCI_RNC_TX_SUSPENDED:
774 case SCI_RNC_AWAIT_SUSPENSION:
775 case SCI_RNC_INITIAL:
776 return 0;
777 default:
778 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
779 "%s: invalid state %d\n", __func__, state);
780 return 0;
781 }
782}