blob: 994ec0c25a744b265add2c30a47ba154b7cef07e [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 */
55
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
Dan Williams89a73012011-06-30 19:14:33 -070093static union scu_remote_node_context *sci_rnc_by_id(struct isci_host *ihost, u16 id)
94{
95 if (id < ihost->remote_node_entries &&
96 ihost->device_table[id])
97 return &ihost->remote_node_context_table[id];
98
99 return NULL;
100}
101
102static void sci_remote_node_context_construct_buffer(struct sci_remote_node_context *sci_rnc)
Dan Williams6f231dd2011-07-02 22:56:22 -0700103{
Dan Williams78a6f062011-06-30 16:31:37 -0700104 struct isci_remote_device *idev = rnc_to_dev(sci_rnc);
105 struct domain_device *dev = idev->domain_dev;
Dan Williams1f4fa1f2011-04-26 11:44:06 -0700106 int rni = sci_rnc->remote_node_index;
Dan Williamsa1a113b2011-04-21 18:44:45 -0700107 union scu_remote_node_context *rnc;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700108 struct isci_host *ihost;
Dan Williamsa3d568f02011-04-26 09:41:52 -0700109 __le64 sas_addr;
Dan Williams6f231dd2011-07-02 22:56:22 -0700110
Dan Williams34a99152011-07-01 02:25:15 -0700111 ihost = idev->owning_port->owning_controller;
Dan Williams89a73012011-06-30 19:14:33 -0700112 rnc = sci_rnc_by_id(ihost, rni);
Dan Williams6f231dd2011-07-02 22:56:22 -0700113
Dan Williams96143952011-04-19 18:35:58 -0700114 memset(rnc, 0, sizeof(union scu_remote_node_context)
Dan Williams89a73012011-06-30 19:14:33 -0700115 * sci_remote_device_node_count(idev));
Dan Williams6f231dd2011-07-02 22:56:22 -0700116
Dan Williams1f4fa1f2011-04-26 11:44:06 -0700117 rnc->ssp.remote_node_index = rni;
Dan Williams78a6f062011-06-30 16:31:37 -0700118 rnc->ssp.remote_node_port_width = idev->device_port_width;
119 rnc->ssp.logical_port_index = idev->owning_port->physical_port_index;
Dan Williams6f231dd2011-07-02 22:56:22 -0700120
Dan Williamsa3d568f02011-04-26 09:41:52 -0700121 /* sas address is __be64, context ram format is __le64 */
122 sas_addr = cpu_to_le64(SAS_ADDR(dev->sas_addr));
123 rnc->ssp.remote_sas_address_hi = upper_32_bits(sas_addr);
124 rnc->ssp.remote_sas_address_lo = lower_32_bits(sas_addr);
Dan Williams6f231dd2011-07-02 22:56:22 -0700125
126 rnc->ssp.nexus_loss_timer_enable = true;
127 rnc->ssp.check_bit = false;
128 rnc->ssp.is_valid = false;
129 rnc->ssp.is_remote_node_context = true;
130 rnc->ssp.function_number = 0;
131
132 rnc->ssp.arbitration_wait_time = 0;
133
Dan Williamsa1a113b2011-04-21 18:44:45 -0700134 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700135 rnc->ssp.connection_occupancy_timeout =
Dan Williams89a73012011-06-30 19:14:33 -0700136 ihost->user_parameters.stp_max_occupancy_timeout;
Dan Williams6f231dd2011-07-02 22:56:22 -0700137 rnc->ssp.connection_inactivity_timeout =
Dan Williams89a73012011-06-30 19:14:33 -0700138 ihost->user_parameters.stp_inactivity_timeout;
Dan Williams6f231dd2011-07-02 22:56:22 -0700139 } else {
140 rnc->ssp.connection_occupancy_timeout =
Dan Williams89a73012011-06-30 19:14:33 -0700141 ihost->user_parameters.ssp_max_occupancy_timeout;
Dan Williams6f231dd2011-07-02 22:56:22 -0700142 rnc->ssp.connection_inactivity_timeout =
Dan Williams89a73012011-06-30 19:14:33 -0700143 ihost->user_parameters.ssp_inactivity_timeout;
Dan Williams6f231dd2011-07-02 22:56:22 -0700144 }
145
146 rnc->ssp.initial_arbitration_wait_time = 0;
147
148 /* Open Address Frame Parameters */
Dan Williams78a6f062011-06-30 16:31:37 -0700149 rnc->ssp.oaf_connection_rate = idev->connection_rate;
Dan Williams6f231dd2011-07-02 22:56:22 -0700150 rnc->ssp.oaf_features = 0;
151 rnc->ssp.oaf_source_zone_group = 0;
152 rnc->ssp.oaf_more_compatibility_features = 0;
153}
154
155/**
156 *
Dave Jiange2023b82011-04-21 05:34:49 +0000157 * @sci_rnc:
158 * @callback:
Dan Williams6f231dd2011-07-02 22:56:22 -0700159 * @callback_parameter:
160 *
161 * This method will setup the remote node context object so it will transition
162 * to its ready state. If the remote node context is already setup to
163 * transition to its final state then this function does nothing. none
164 */
Dan Williams89a73012011-06-30 19:14:33 -0700165static void sci_remote_node_context_setup_to_resume(
166 struct sci_remote_node_context *sci_rnc,
Dave Jiange2023b82011-04-21 05:34:49 +0000167 scics_sds_remote_node_context_callback callback,
Dan Williams6f231dd2011-07-02 22:56:22 -0700168 void *callback_parameter)
169{
Dave Jiange2023b82011-04-21 05:34:49 +0000170 if (sci_rnc->destination_state != SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_FINAL) {
171 sci_rnc->destination_state = SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_READY;
172 sci_rnc->user_callback = callback;
173 sci_rnc->user_cookie = callback_parameter;
Dan Williams6f231dd2011-07-02 22:56:22 -0700174 }
175}
176
Dan Williams89a73012011-06-30 19:14:33 -0700177static void sci_remote_node_context_setup_to_destory(
178 struct sci_remote_node_context *sci_rnc,
Dave Jiange2023b82011-04-21 05:34:49 +0000179 scics_sds_remote_node_context_callback callback,
Dan Williams6f231dd2011-07-02 22:56:22 -0700180 void *callback_parameter)
181{
Dave Jiange2023b82011-04-21 05:34:49 +0000182 sci_rnc->destination_state = SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_FINAL;
183 sci_rnc->user_callback = callback;
184 sci_rnc->user_cookie = callback_parameter;
Dan Williams6f231dd2011-07-02 22:56:22 -0700185}
186
Dan Williams6f231dd2011-07-02 22:56:22 -0700187/**
188 *
189 *
190 * This method just calls the user callback function and then resets the
191 * callback.
192 */
Dan Williams89a73012011-06-30 19:14:33 -0700193static void sci_remote_node_context_notify_user(
194 struct sci_remote_node_context *rnc)
Dan Williams6f231dd2011-07-02 22:56:22 -0700195{
196 if (rnc->user_callback != NULL) {
197 (*rnc->user_callback)(rnc->user_cookie);
198
199 rnc->user_callback = NULL;
200 rnc->user_cookie = NULL;
201 }
202}
203
Dan Williams89a73012011-06-30 19:14:33 -0700204static void sci_remote_node_context_continue_state_transitions(struct sci_remote_node_context *rnc)
Dan Williams6f231dd2011-07-02 22:56:22 -0700205{
Dan Williamsed3efb72011-05-12 08:50:23 -0700206 if (rnc->destination_state == SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_READY)
Dan Williams89a73012011-06-30 19:14:33 -0700207 sci_remote_node_context_resume(rnc, rnc->user_callback,
Dan Williamsed3efb72011-05-12 08:50:23 -0700208 rnc->user_cookie);
Dan Williams6f231dd2011-07-02 22:56:22 -0700209}
210
Dan Williams89a73012011-06-30 19:14:33 -0700211static void sci_remote_node_context_validate_context_buffer(struct sci_remote_node_context *sci_rnc)
Dan Williams6f231dd2011-07-02 22:56:22 -0700212{
Dan Williams89a73012011-06-30 19:14:33 -0700213 union scu_remote_node_context *rnc_buffer;
Dan Williams78a6f062011-06-30 16:31:37 -0700214 struct isci_remote_device *idev = rnc_to_dev(sci_rnc);
215 struct domain_device *dev = idev->domain_dev;
Dan Williams89a73012011-06-30 19:14:33 -0700216 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williams6f231dd2011-07-02 22:56:22 -0700217
Dan Williams89a73012011-06-30 19:14:33 -0700218 rnc_buffer = sci_rnc_by_id(ihost, sci_rnc->remote_node_index);
Dan Williams6f231dd2011-07-02 22:56:22 -0700219
220 rnc_buffer->ssp.is_valid = true;
221
Dan Williams78a6f062011-06-30 16:31:37 -0700222 if (!idev->is_direct_attached &&
Dan Williamsa1a113b2011-04-21 18:44:45 -0700223 (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))) {
Dan Williams89a73012011-06-30 19:14:33 -0700224 sci_remote_device_post_request(idev, SCU_CONTEXT_COMMAND_POST_RNC_96);
Dan Williams6f231dd2011-07-02 22:56:22 -0700225 } else {
Dan Williams89a73012011-06-30 19:14:33 -0700226 sci_remote_device_post_request(idev, SCU_CONTEXT_COMMAND_POST_RNC_32);
Dan Williams6f231dd2011-07-02 22:56:22 -0700227
Dan Williams89a73012011-06-30 19:14:33 -0700228 if (idev->is_direct_attached)
229 sci_port_setup_transports(idev->owning_port,
230 sci_rnc->remote_node_index);
Dan Williams6f231dd2011-07-02 22:56:22 -0700231 }
232}
233
Dan Williams89a73012011-06-30 19:14:33 -0700234static void sci_remote_node_context_invalidate_context_buffer(struct sci_remote_node_context *sci_rnc)
Dan Williams6f231dd2011-07-02 22:56:22 -0700235{
236 union scu_remote_node_context *rnc_buffer;
Dan Williams89a73012011-06-30 19:14:33 -0700237 struct isci_remote_device *idev = rnc_to_dev(sci_rnc);
238 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williams6f231dd2011-07-02 22:56:22 -0700239
Dan Williams89a73012011-06-30 19:14:33 -0700240 rnc_buffer = sci_rnc_by_id(ihost, sci_rnc->remote_node_index);
Dan Williams6f231dd2011-07-02 22:56:22 -0700241
242 rnc_buffer->ssp.is_valid = false;
243
Dan Williams89a73012011-06-30 19:14:33 -0700244 sci_remote_device_post_request(rnc_to_dev(sci_rnc),
245 SCU_CONTEXT_COMMAND_POST_RNC_INVALIDATE);
Dan Williams6f231dd2011-07-02 22:56:22 -0700246}
247
Dan Williams89a73012011-06-30 19:14:33 -0700248static void sci_remote_node_context_initial_state_enter(struct sci_base_state_machine *sm)
Dan Williams6f231dd2011-07-02 22:56:22 -0700249{
Dan Williams89a73012011-06-30 19:14:33 -0700250 struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
Dan Williams6f231dd2011-07-02 22:56:22 -0700251
Dan Williamsf34d9e52011-05-12 09:27:52 -0700252 /* Check to see if we have gotten back to the initial state because
253 * someone requested to destroy the remote node context object.
254 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000255 if (sm->previous_state_id == SCI_RNC_INVALIDATING) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700256 rnc->destination_state = SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_UNSPECIFIED;
Dan Williams89a73012011-06-30 19:14:33 -0700257 sci_remote_node_context_notify_user(rnc);
Dan Williams6f231dd2011-07-02 22:56:22 -0700258 }
259}
260
Dan Williams89a73012011-06-30 19:14:33 -0700261static void sci_remote_node_context_posting_state_enter(struct sci_base_state_machine *sm)
Dan Williams6f231dd2011-07-02 22:56:22 -0700262{
Dan Williams89a73012011-06-30 19:14:33 -0700263 struct sci_remote_node_context *sci_rnc = container_of(sm, typeof(*sci_rnc), sm);
Dan Williams6f231dd2011-07-02 22:56:22 -0700264
Dan Williams89a73012011-06-30 19:14:33 -0700265 sci_remote_node_context_validate_context_buffer(sci_rnc);
Dan Williams6f231dd2011-07-02 22:56:22 -0700266}
267
Dan Williams89a73012011-06-30 19:14:33 -0700268static void sci_remote_node_context_invalidating_state_enter(struct sci_base_state_machine *sm)
Dan Williams6f231dd2011-07-02 22:56:22 -0700269{
Dan Williams89a73012011-06-30 19:14:33 -0700270 struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
Dan Williams6f231dd2011-07-02 22:56:22 -0700271
Dan Williams89a73012011-06-30 19:14:33 -0700272 sci_remote_node_context_invalidate_context_buffer(rnc);
Dan Williams6f231dd2011-07-02 22:56:22 -0700273}
274
Dan Williams89a73012011-06-30 19:14:33 -0700275static void sci_remote_node_context_resuming_state_enter(struct sci_base_state_machine *sm)
Dan Williams6f231dd2011-07-02 22:56:22 -0700276{
Dan Williams89a73012011-06-30 19:14:33 -0700277 struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
Dan Williams78a6f062011-06-30 16:31:37 -0700278 struct isci_remote_device *idev;
Dan Williamsa1a113b2011-04-21 18:44:45 -0700279 struct domain_device *dev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700280
Dan Williams78a6f062011-06-30 16:31:37 -0700281 idev = rnc_to_dev(rnc);
282 dev = idev->domain_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700283
Henryk Dembkowski24621462011-02-23 00:08:52 -0800284 /*
285 * For direct attached SATA devices we need to clear the TLCR
286 * NCQ to TCi tag mapping on the phy and in cases where we
287 * resume because of a target reset we also need to update
288 * the STPTLDARNI register with the RNi of the device
289 */
Dan Williamsa1a113b2011-04-21 18:44:45 -0700290 if ((dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) &&
Dan Williams78a6f062011-06-30 16:31:37 -0700291 idev->is_direct_attached)
Dan Williams89a73012011-06-30 19:14:33 -0700292 sci_port_setup_transports(idev->owning_port,
Dan Williams96143952011-04-19 18:35:58 -0700293 rnc->remote_node_index);
Henryk Dembkowski24621462011-02-23 00:08:52 -0800294
Dan Williams89a73012011-06-30 19:14:33 -0700295 sci_remote_device_post_request(idev, SCU_CONTEXT_COMMAND_POST_RNC_RESUME);
Dan Williams6f231dd2011-07-02 22:56:22 -0700296}
297
Dan Williams89a73012011-06-30 19:14:33 -0700298static void sci_remote_node_context_ready_state_enter(struct sci_base_state_machine *sm)
Dan Williams6f231dd2011-07-02 22:56:22 -0700299{
Dan Williams89a73012011-06-30 19:14:33 -0700300 struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
Dan Williams6f231dd2011-07-02 22:56:22 -0700301
Dan Williams6f231dd2011-07-02 22:56:22 -0700302 rnc->destination_state = SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_UNSPECIFIED;
303
Dan Williamsf34d9e52011-05-12 09:27:52 -0700304 if (rnc->user_callback)
Dan Williams89a73012011-06-30 19:14:33 -0700305 sci_remote_node_context_notify_user(rnc);
Dan Williams6f231dd2011-07-02 22:56:22 -0700306}
307
Dan Williams89a73012011-06-30 19:14:33 -0700308static void sci_remote_node_context_tx_suspended_state_enter(struct sci_base_state_machine *sm)
Dan Williams6f231dd2011-07-02 22:56:22 -0700309{
Dan Williams89a73012011-06-30 19:14:33 -0700310 struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
Dan Williams6f231dd2011-07-02 22:56:22 -0700311
Dan Williams89a73012011-06-30 19:14:33 -0700312 sci_remote_node_context_continue_state_transitions(rnc);
Dan Williams6f231dd2011-07-02 22:56:22 -0700313}
314
Dan Williams89a73012011-06-30 19:14:33 -0700315static void sci_remote_node_context_tx_rx_suspended_state_enter(struct sci_base_state_machine *sm)
Dan Williams6f231dd2011-07-02 22:56:22 -0700316{
Dan Williams89a73012011-06-30 19:14:33 -0700317 struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
Dan Williams6f231dd2011-07-02 22:56:22 -0700318
Dan Williams89a73012011-06-30 19:14:33 -0700319 sci_remote_node_context_continue_state_transitions(rnc);
Dan Williams6f231dd2011-07-02 22:56:22 -0700320}
321
Dan Williams89a73012011-06-30 19:14:33 -0700322static const struct sci_base_state sci_remote_node_context_state_table[] = {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000323 [SCI_RNC_INITIAL] = {
Dan Williams89a73012011-06-30 19:14:33 -0700324 .enter_state = sci_remote_node_context_initial_state_enter,
Dan Williams6f231dd2011-07-02 22:56:22 -0700325 },
Edmund Nadolskie3013702011-06-02 00:10:43 +0000326 [SCI_RNC_POSTING] = {
Dan Williams89a73012011-06-30 19:14:33 -0700327 .enter_state = sci_remote_node_context_posting_state_enter,
Dan Williams6f231dd2011-07-02 22:56:22 -0700328 },
Edmund Nadolskie3013702011-06-02 00:10:43 +0000329 [SCI_RNC_INVALIDATING] = {
Dan Williams89a73012011-06-30 19:14:33 -0700330 .enter_state = sci_remote_node_context_invalidating_state_enter,
Dan Williams6f231dd2011-07-02 22:56:22 -0700331 },
Edmund Nadolskie3013702011-06-02 00:10:43 +0000332 [SCI_RNC_RESUMING] = {
Dan Williams89a73012011-06-30 19:14:33 -0700333 .enter_state = sci_remote_node_context_resuming_state_enter,
Dan Williams6f231dd2011-07-02 22:56:22 -0700334 },
Edmund Nadolskie3013702011-06-02 00:10:43 +0000335 [SCI_RNC_READY] = {
Dan Williams89a73012011-06-30 19:14:33 -0700336 .enter_state = sci_remote_node_context_ready_state_enter,
Dan Williams6f231dd2011-07-02 22:56:22 -0700337 },
Edmund Nadolskie3013702011-06-02 00:10:43 +0000338 [SCI_RNC_TX_SUSPENDED] = {
Dan Williams89a73012011-06-30 19:14:33 -0700339 .enter_state = sci_remote_node_context_tx_suspended_state_enter,
Dan Williams6f231dd2011-07-02 22:56:22 -0700340 },
Edmund Nadolskie3013702011-06-02 00:10:43 +0000341 [SCI_RNC_TX_RX_SUSPENDED] = {
Dan Williams89a73012011-06-30 19:14:33 -0700342 .enter_state = sci_remote_node_context_tx_rx_suspended_state_enter,
Dan Williams6f231dd2011-07-02 22:56:22 -0700343 },
Edmund Nadolskie3013702011-06-02 00:10:43 +0000344 [SCI_RNC_AWAIT_SUSPENSION] = { },
Dan Williams6f231dd2011-07-02 22:56:22 -0700345};
346
Dan Williams89a73012011-06-30 19:14:33 -0700347void sci_remote_node_context_construct(struct sci_remote_node_context *rnc,
Dan Williams96143952011-04-19 18:35:58 -0700348 u16 remote_node_index)
Dan Williams35173d52011-03-26 16:43:01 -0700349{
Dan Williams89a73012011-06-30 19:14:33 -0700350 memset(rnc, 0, sizeof(struct sci_remote_node_context));
Dan Williams35173d52011-03-26 16:43:01 -0700351
352 rnc->remote_node_index = remote_node_index;
Dan Williams35173d52011-03-26 16:43:01 -0700353 rnc->destination_state = SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_UNSPECIFIED;
354
Dan Williams89a73012011-06-30 19:14:33 -0700355 sci_init_sm(&rnc->sm, sci_remote_node_context_state_table, SCI_RNC_INITIAL);
Dan Williams35173d52011-03-26 16:43:01 -0700356}
Dan Williams338e3862011-05-12 07:46:59 -0700357
Dan Williams89a73012011-06-30 19:14:33 -0700358enum sci_status sci_remote_node_context_event_handler(struct sci_remote_node_context *sci_rnc,
Dan Williams338e3862011-05-12 07:46:59 -0700359 u32 event_code)
360{
361 enum scis_sds_remote_node_context_states state;
362
Edmund Nadolskie3013702011-06-02 00:10:43 +0000363 state = sci_rnc->sm.current_state_id;
Dan Williams338e3862011-05-12 07:46:59 -0700364 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000365 case SCI_RNC_POSTING:
Dan Williams338e3862011-05-12 07:46:59 -0700366 switch (scu_get_event_code(event_code)) {
367 case SCU_EVENT_POST_RNC_COMPLETE:
Edmund Nadolskie3013702011-06-02 00:10:43 +0000368 sci_change_state(&sci_rnc->sm, SCI_RNC_READY);
Dan Williams338e3862011-05-12 07:46:59 -0700369 break;
370 default:
371 goto out;
372 }
373 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000374 case SCI_RNC_INVALIDATING:
Dan Williams338e3862011-05-12 07:46:59 -0700375 if (scu_get_event_code(event_code) == SCU_EVENT_POST_RNC_INVALIDATE_COMPLETE) {
376 if (sci_rnc->destination_state == SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_FINAL)
Edmund Nadolskie3013702011-06-02 00:10:43 +0000377 state = SCI_RNC_INITIAL;
Dan Williams338e3862011-05-12 07:46:59 -0700378 else
Edmund Nadolskie3013702011-06-02 00:10:43 +0000379 state = SCI_RNC_POSTING;
380 sci_change_state(&sci_rnc->sm, state);
Dan Williams338e3862011-05-12 07:46:59 -0700381 } else {
382 switch (scu_get_event_type(event_code)) {
383 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
384 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
385 /* We really dont care if the hardware is going to suspend
386 * the device since it's being invalidated anyway */
387 dev_dbg(scirdev_to_dev(rnc_to_dev(sci_rnc)),
388 "%s: SCIC Remote Node Context 0x%p was "
389 "suspeneded by hardware while being "
390 "invalidated.\n", __func__, sci_rnc);
391 break;
392 default:
393 goto out;
394 }
395 }
396 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000397 case SCI_RNC_RESUMING:
Dan Williams338e3862011-05-12 07:46:59 -0700398 if (scu_get_event_code(event_code) == SCU_EVENT_POST_RCN_RELEASE) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000399 sci_change_state(&sci_rnc->sm, SCI_RNC_READY);
Dan Williams338e3862011-05-12 07:46:59 -0700400 } else {
401 switch (scu_get_event_type(event_code)) {
402 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
403 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
404 /* We really dont care if the hardware is going to suspend
405 * the device since it's being resumed anyway */
406 dev_dbg(scirdev_to_dev(rnc_to_dev(sci_rnc)),
407 "%s: SCIC Remote Node Context 0x%p was "
408 "suspeneded by hardware while being resumed.\n",
409 __func__, sci_rnc);
410 break;
411 default:
412 goto out;
413 }
414 }
415 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000416 case SCI_RNC_READY:
Dan Williams338e3862011-05-12 07:46:59 -0700417 switch (scu_get_event_type(event_code)) {
418 case SCU_EVENT_TL_RNC_SUSPEND_TX:
Edmund Nadolskie3013702011-06-02 00:10:43 +0000419 sci_change_state(&sci_rnc->sm, SCI_RNC_TX_SUSPENDED);
Dan Williams338e3862011-05-12 07:46:59 -0700420 sci_rnc->suspension_code = scu_get_event_specifier(event_code);
421 break;
422 case SCU_EVENT_TL_RNC_SUSPEND_TX_RX:
Edmund Nadolskie3013702011-06-02 00:10:43 +0000423 sci_change_state(&sci_rnc->sm, SCI_RNC_TX_RX_SUSPENDED);
Dan Williams338e3862011-05-12 07:46:59 -0700424 sci_rnc->suspension_code = scu_get_event_specifier(event_code);
425 break;
426 default:
427 goto out;
428 }
429 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000430 case SCI_RNC_AWAIT_SUSPENSION:
Dan Williams338e3862011-05-12 07:46:59 -0700431 switch (scu_get_event_type(event_code)) {
432 case SCU_EVENT_TL_RNC_SUSPEND_TX:
Edmund Nadolskie3013702011-06-02 00:10:43 +0000433 sci_change_state(&sci_rnc->sm, SCI_RNC_TX_SUSPENDED);
Dan Williams338e3862011-05-12 07:46:59 -0700434 sci_rnc->suspension_code = scu_get_event_specifier(event_code);
435 break;
436 case SCU_EVENT_TL_RNC_SUSPEND_TX_RX:
Edmund Nadolskie3013702011-06-02 00:10:43 +0000437 sci_change_state(&sci_rnc->sm, SCI_RNC_TX_RX_SUSPENDED);
Dan Williams338e3862011-05-12 07:46:59 -0700438 sci_rnc->suspension_code = scu_get_event_specifier(event_code);
439 break;
440 default:
441 goto out;
442 }
443 break;
444 default:
445 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
Dan Williams14e99b42012-02-10 01:05:43 -0800446 "%s: invalid state: %s\n", __func__,
447 rnc_state_name(state));
Dan Williams338e3862011-05-12 07:46:59 -0700448 return SCI_FAILURE_INVALID_STATE;
449 }
450 return SCI_SUCCESS;
451
452 out:
453 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
Dan Williams14e99b42012-02-10 01:05:43 -0800454 "%s: code: %#x state: %s\n", __func__, event_code,
455 rnc_state_name(state));
Dan Williams338e3862011-05-12 07:46:59 -0700456 return SCI_FAILURE;
457
458}
Dan Williamsc845ae92011-05-12 08:26:56 -0700459
Dan Williams89a73012011-06-30 19:14:33 -0700460enum sci_status sci_remote_node_context_destruct(struct sci_remote_node_context *sci_rnc,
Dan Williamsc845ae92011-05-12 08:26:56 -0700461 scics_sds_remote_node_context_callback cb_fn,
462 void *cb_p)
463{
464 enum scis_sds_remote_node_context_states state;
465
Edmund Nadolskie3013702011-06-02 00:10:43 +0000466 state = sci_rnc->sm.current_state_id;
Dan Williamsc845ae92011-05-12 08:26:56 -0700467 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000468 case SCI_RNC_INVALIDATING:
Dan Williams89a73012011-06-30 19:14:33 -0700469 sci_remote_node_context_setup_to_destory(sci_rnc, cb_fn, cb_p);
Dan Williamsc845ae92011-05-12 08:26:56 -0700470 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000471 case SCI_RNC_POSTING:
472 case SCI_RNC_RESUMING:
473 case SCI_RNC_READY:
474 case SCI_RNC_TX_SUSPENDED:
475 case SCI_RNC_TX_RX_SUSPENDED:
476 case SCI_RNC_AWAIT_SUSPENSION:
Dan Williams89a73012011-06-30 19:14:33 -0700477 sci_remote_node_context_setup_to_destory(sci_rnc, cb_fn, cb_p);
Edmund Nadolskie3013702011-06-02 00:10:43 +0000478 sci_change_state(&sci_rnc->sm, SCI_RNC_INVALIDATING);
Dan Williamsc845ae92011-05-12 08:26:56 -0700479 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000480 case SCI_RNC_INITIAL:
Dan Williamsc845ae92011-05-12 08:26:56 -0700481 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
Dan Williams14e99b42012-02-10 01:05:43 -0800482 "%s: invalid state: %s\n", __func__,
483 rnc_state_name(state));
Dan Williamsc845ae92011-05-12 08:26:56 -0700484 /* We have decided that the destruct request on the remote node context
485 * can not fail since it is either in the initial/destroyed state or is
486 * can be destroyed.
487 */
488 return SCI_SUCCESS;
489 default:
490 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
Dan Williams14e99b42012-02-10 01:05:43 -0800491 "%s: invalid state %s\n", __func__,
492 rnc_state_name(state));
Dan Williamsc845ae92011-05-12 08:26:56 -0700493 return SCI_FAILURE_INVALID_STATE;
494 }
495}
Dan Williamsed3efb72011-05-12 08:50:23 -0700496
Dan Williams89a73012011-06-30 19:14:33 -0700497enum sci_status sci_remote_node_context_suspend(struct sci_remote_node_context *sci_rnc,
Dan Williamsed3efb72011-05-12 08:50:23 -0700498 u32 suspend_type,
499 scics_sds_remote_node_context_callback cb_fn,
500 void *cb_p)
501{
502 enum scis_sds_remote_node_context_states state;
503
Edmund Nadolskie3013702011-06-02 00:10:43 +0000504 state = sci_rnc->sm.current_state_id;
505 if (state != SCI_RNC_READY) {
Dan Williamsed3efb72011-05-12 08:50:23 -0700506 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 Williamsed3efb72011-05-12 08:50:23 -0700509 return SCI_FAILURE_INVALID_STATE;
510 }
511
512 sci_rnc->user_callback = cb_fn;
513 sci_rnc->user_cookie = cb_p;
514 sci_rnc->suspension_code = suspend_type;
515
516 if (suspend_type == SCI_SOFTWARE_SUSPENSION) {
Dan Williams89a73012011-06-30 19:14:33 -0700517 sci_remote_device_post_request(rnc_to_dev(sci_rnc),
Dan Williamsed3efb72011-05-12 08:50:23 -0700518 SCU_CONTEXT_COMMAND_POST_RNC_SUSPEND_TX);
519 }
520
Edmund Nadolskie3013702011-06-02 00:10:43 +0000521 sci_change_state(&sci_rnc->sm, SCI_RNC_AWAIT_SUSPENSION);
Dan Williamsed3efb72011-05-12 08:50:23 -0700522 return SCI_SUCCESS;
523}
524
Dan Williams89a73012011-06-30 19:14:33 -0700525enum sci_status sci_remote_node_context_resume(struct sci_remote_node_context *sci_rnc,
Dan Williamsed3efb72011-05-12 08:50:23 -0700526 scics_sds_remote_node_context_callback cb_fn,
527 void *cb_p)
528{
529 enum scis_sds_remote_node_context_states state;
530
Edmund Nadolskie3013702011-06-02 00:10:43 +0000531 state = sci_rnc->sm.current_state_id;
Dan Williamsed3efb72011-05-12 08:50:23 -0700532 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000533 case SCI_RNC_INITIAL:
Dan Williamsed3efb72011-05-12 08:50:23 -0700534 if (sci_rnc->remote_node_index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX)
535 return SCI_FAILURE_INVALID_STATE;
536
Dan Williams89a73012011-06-30 19:14:33 -0700537 sci_remote_node_context_setup_to_resume(sci_rnc, cb_fn, cb_p);
538 sci_remote_node_context_construct_buffer(sci_rnc);
Edmund Nadolskie3013702011-06-02 00:10:43 +0000539 sci_change_state(&sci_rnc->sm, SCI_RNC_POSTING);
Dan Williamsed3efb72011-05-12 08:50:23 -0700540 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000541 case SCI_RNC_POSTING:
542 case SCI_RNC_INVALIDATING:
543 case SCI_RNC_RESUMING:
Dan Williamsed3efb72011-05-12 08:50:23 -0700544 if (sci_rnc->destination_state != SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_READY)
545 return SCI_FAILURE_INVALID_STATE;
546
547 sci_rnc->user_callback = cb_fn;
548 sci_rnc->user_cookie = cb_p;
549 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000550 case SCI_RNC_TX_SUSPENDED: {
Dan Williams78a6f062011-06-30 16:31:37 -0700551 struct isci_remote_device *idev = rnc_to_dev(sci_rnc);
552 struct domain_device *dev = idev->domain_dev;
Dan Williamsed3efb72011-05-12 08:50:23 -0700553
Dan Williams89a73012011-06-30 19:14:33 -0700554 sci_remote_node_context_setup_to_resume(sci_rnc, cb_fn, cb_p);
Dan Williamsed3efb72011-05-12 08:50:23 -0700555
556 /* TODO: consider adding a resume action of NONE, INVALIDATE, WRITE_TLCR */
557 if (dev->dev_type == SAS_END_DEV || dev_is_expander(dev))
Edmund Nadolskie3013702011-06-02 00:10:43 +0000558 sci_change_state(&sci_rnc->sm, SCI_RNC_RESUMING);
Dan Williamsed3efb72011-05-12 08:50:23 -0700559 else if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
Dan Williams78a6f062011-06-30 16:31:37 -0700560 if (idev->is_direct_attached) {
Dan Williamsed3efb72011-05-12 08:50:23 -0700561 /* @todo Fix this since I am being silly in writing to the STPTLDARNI register. */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000562 sci_change_state(&sci_rnc->sm, SCI_RNC_RESUMING);
Dan Williamsed3efb72011-05-12 08:50:23 -0700563 } else {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000564 sci_change_state(&sci_rnc->sm, SCI_RNC_INVALIDATING);
Dan Williamsed3efb72011-05-12 08:50:23 -0700565 }
566 } else
567 return SCI_FAILURE;
568 return SCI_SUCCESS;
569 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000570 case SCI_RNC_TX_RX_SUSPENDED:
Dan Williams89a73012011-06-30 19:14:33 -0700571 sci_remote_node_context_setup_to_resume(sci_rnc, cb_fn, cb_p);
Edmund Nadolskie3013702011-06-02 00:10:43 +0000572 sci_change_state(&sci_rnc->sm, SCI_RNC_RESUMING);
Dan Williamsed3efb72011-05-12 08:50:23 -0700573 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000574 case SCI_RNC_AWAIT_SUSPENSION:
Dan Williams89a73012011-06-30 19:14:33 -0700575 sci_remote_node_context_setup_to_resume(sci_rnc, cb_fn, cb_p);
Dan Williamsed3efb72011-05-12 08:50:23 -0700576 return SCI_SUCCESS;
577 default:
578 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
Dan Williams14e99b42012-02-10 01:05:43 -0800579 "%s: invalid state %s\n", __func__,
580 rnc_state_name(state));
Dan Williamsed3efb72011-05-12 08:50:23 -0700581 return SCI_FAILURE_INVALID_STATE;
582 }
583}
Dan Williamsf34d9e52011-05-12 09:27:52 -0700584
Dan Williams89a73012011-06-30 19:14:33 -0700585enum sci_status sci_remote_node_context_start_io(struct sci_remote_node_context *sci_rnc,
Dan Williams5076a1a2011-06-27 14:57:03 -0700586 struct isci_request *ireq)
Dan Williamsf34d9e52011-05-12 09:27:52 -0700587{
588 enum scis_sds_remote_node_context_states state;
589
Edmund Nadolskie3013702011-06-02 00:10:43 +0000590 state = sci_rnc->sm.current_state_id;
Jeff Skirvinfd536602011-06-20 14:09:22 -0700591
592 switch (state) {
593 case SCI_RNC_READY:
594 return SCI_SUCCESS;
595 case SCI_RNC_TX_SUSPENDED:
596 case SCI_RNC_TX_RX_SUSPENDED:
597 case SCI_RNC_AWAIT_SUSPENSION:
Dan Williamsf34d9e52011-05-12 09:27:52 -0700598 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
Dan Williams14e99b42012-02-10 01:05:43 -0800599 "%s: invalid state %s\n", __func__,
600 rnc_state_name(state));
Dan Williamsf34d9e52011-05-12 09:27:52 -0700601 return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
Jeff Skirvinfd536602011-06-20 14:09:22 -0700602 default:
Dan Williams14e99b42012-02-10 01:05:43 -0800603 dev_dbg(scirdev_to_dev(rnc_to_dev(sci_rnc)),
604 "%s: invalid state %s\n", __func__,
605 rnc_state_name(state));
606 return SCI_FAILURE_INVALID_STATE;
Dan Williamsf34d9e52011-05-12 09:27:52 -0700607 }
Dan Williamsf34d9e52011-05-12 09:27:52 -0700608}
609
Dan Williams89a73012011-06-30 19:14:33 -0700610enum sci_status sci_remote_node_context_start_task(struct sci_remote_node_context *sci_rnc,
Dan Williams5076a1a2011-06-27 14:57:03 -0700611 struct isci_request *ireq)
Dan Williamsf34d9e52011-05-12 09:27:52 -0700612{
613 enum scis_sds_remote_node_context_states state;
614
Edmund Nadolskie3013702011-06-02 00:10:43 +0000615 state = sci_rnc->sm.current_state_id;
Dan Williamsf34d9e52011-05-12 09:27:52 -0700616 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000617 case SCI_RNC_RESUMING:
618 case SCI_RNC_READY:
619 case SCI_RNC_AWAIT_SUSPENSION:
Dan Williamsf34d9e52011-05-12 09:27:52 -0700620 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000621 case SCI_RNC_TX_SUSPENDED:
622 case SCI_RNC_TX_RX_SUSPENDED:
Dan Williams89a73012011-06-30 19:14:33 -0700623 sci_remote_node_context_resume(sci_rnc, NULL, NULL);
Dan Williamsf34d9e52011-05-12 09:27:52 -0700624 return SCI_SUCCESS;
625 default:
626 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
Dan Williams14e99b42012-02-10 01:05:43 -0800627 "%s: invalid state %s\n", __func__,
628 rnc_state_name(state));
Dan Williamsf34d9e52011-05-12 09:27:52 -0700629 return SCI_FAILURE_INVALID_STATE;
630 }
631}