blob: 748e8339d1ec70db96475c1714bd4733e7495259 [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 Williams6f231dd2011-07-02 22:56:22 -070063
64/**
65 *
Dave Jiange2023b82011-04-21 05:34:49 +000066 * @sci_rnc: The RNC for which the is posted request is being made.
Dan Williams6f231dd2011-07-02 22:56:22 -070067 *
68 * This method will return true if the RNC is not in the initial state. In all
69 * other states the RNC is considered active and this will return true. The
70 * destroy request of the state machine drives the RNC back to the initial
71 * state. If the state machine changes then this routine will also have to be
72 * changed. bool true if the state machine is not in the initial state false if
73 * the state machine is in the initial state
74 */
75
76/**
77 *
Dave Jiange2023b82011-04-21 05:34:49 +000078 * @sci_rnc: The state of the remote node context object to check.
Dan Williams6f231dd2011-07-02 22:56:22 -070079 *
80 * This method will return true if the remote node context is in a READY state
81 * otherwise it will return false bool true if the remote node context is in
82 * the ready state. false if the remote node context is not in the ready state.
83 */
Dan Williams89a73012011-06-30 19:14:33 -070084bool sci_remote_node_context_is_ready(
85 struct sci_remote_node_context *sci_rnc)
Dan Williams6f231dd2011-07-02 22:56:22 -070086{
Edmund Nadolskie3013702011-06-02 00:10:43 +000087 u32 current_state = sci_rnc->sm.current_state_id;
Dan Williams6f231dd2011-07-02 22:56:22 -070088
Edmund Nadolskie3013702011-06-02 00:10:43 +000089 if (current_state == SCI_RNC_READY) {
Dan Williams6f231dd2011-07-02 22:56:22 -070090 return true;
91 }
92
93 return false;
94}
95
Dan Williams89a73012011-06-30 19:14:33 -070096static union scu_remote_node_context *sci_rnc_by_id(struct isci_host *ihost, u16 id)
97{
98 if (id < ihost->remote_node_entries &&
99 ihost->device_table[id])
100 return &ihost->remote_node_context_table[id];
101
102 return NULL;
103}
104
105static void sci_remote_node_context_construct_buffer(struct sci_remote_node_context *sci_rnc)
Dan Williams6f231dd2011-07-02 22:56:22 -0700106{
Dan Williams78a6f062011-06-30 16:31:37 -0700107 struct isci_remote_device *idev = rnc_to_dev(sci_rnc);
108 struct domain_device *dev = idev->domain_dev;
Dan Williams1f4fa1f2011-04-26 11:44:06 -0700109 int rni = sci_rnc->remote_node_index;
Dan Williamsa1a113b2011-04-21 18:44:45 -0700110 union scu_remote_node_context *rnc;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700111 struct isci_host *ihost;
Dan Williamsa3d568f02011-04-26 09:41:52 -0700112 __le64 sas_addr;
Dan Williams6f231dd2011-07-02 22:56:22 -0700113
Dan Williams34a99152011-07-01 02:25:15 -0700114 ihost = idev->owning_port->owning_controller;
Dan Williams89a73012011-06-30 19:14:33 -0700115 rnc = sci_rnc_by_id(ihost, rni);
Dan Williams6f231dd2011-07-02 22:56:22 -0700116
Dan Williams96143952011-04-19 18:35:58 -0700117 memset(rnc, 0, sizeof(union scu_remote_node_context)
Dan Williams89a73012011-06-30 19:14:33 -0700118 * sci_remote_device_node_count(idev));
Dan Williams6f231dd2011-07-02 22:56:22 -0700119
Dan Williams1f4fa1f2011-04-26 11:44:06 -0700120 rnc->ssp.remote_node_index = rni;
Dan Williams78a6f062011-06-30 16:31:37 -0700121 rnc->ssp.remote_node_port_width = idev->device_port_width;
122 rnc->ssp.logical_port_index = idev->owning_port->physical_port_index;
Dan Williams6f231dd2011-07-02 22:56:22 -0700123
Dan Williamsa3d568f02011-04-26 09:41:52 -0700124 /* sas address is __be64, context ram format is __le64 */
125 sas_addr = cpu_to_le64(SAS_ADDR(dev->sas_addr));
126 rnc->ssp.remote_sas_address_hi = upper_32_bits(sas_addr);
127 rnc->ssp.remote_sas_address_lo = lower_32_bits(sas_addr);
Dan Williams6f231dd2011-07-02 22:56:22 -0700128
129 rnc->ssp.nexus_loss_timer_enable = true;
130 rnc->ssp.check_bit = false;
131 rnc->ssp.is_valid = false;
132 rnc->ssp.is_remote_node_context = true;
133 rnc->ssp.function_number = 0;
134
135 rnc->ssp.arbitration_wait_time = 0;
136
Dan Williamsa1a113b2011-04-21 18:44:45 -0700137 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700138 rnc->ssp.connection_occupancy_timeout =
Dan Williams89a73012011-06-30 19:14:33 -0700139 ihost->user_parameters.stp_max_occupancy_timeout;
Dan Williams6f231dd2011-07-02 22:56:22 -0700140 rnc->ssp.connection_inactivity_timeout =
Dan Williams89a73012011-06-30 19:14:33 -0700141 ihost->user_parameters.stp_inactivity_timeout;
Dan Williams6f231dd2011-07-02 22:56:22 -0700142 } else {
143 rnc->ssp.connection_occupancy_timeout =
Dan Williams89a73012011-06-30 19:14:33 -0700144 ihost->user_parameters.ssp_max_occupancy_timeout;
Dan Williams6f231dd2011-07-02 22:56:22 -0700145 rnc->ssp.connection_inactivity_timeout =
Dan Williams89a73012011-06-30 19:14:33 -0700146 ihost->user_parameters.ssp_inactivity_timeout;
Dan Williams6f231dd2011-07-02 22:56:22 -0700147 }
148
149 rnc->ssp.initial_arbitration_wait_time = 0;
150
151 /* Open Address Frame Parameters */
Dan Williams78a6f062011-06-30 16:31:37 -0700152 rnc->ssp.oaf_connection_rate = idev->connection_rate;
Dan Williams6f231dd2011-07-02 22:56:22 -0700153 rnc->ssp.oaf_features = 0;
154 rnc->ssp.oaf_source_zone_group = 0;
155 rnc->ssp.oaf_more_compatibility_features = 0;
156}
157
158/**
159 *
Dave Jiange2023b82011-04-21 05:34:49 +0000160 * @sci_rnc:
161 * @callback:
Dan Williams6f231dd2011-07-02 22:56:22 -0700162 * @callback_parameter:
163 *
164 * This method will setup the remote node context object so it will transition
165 * to its ready state. If the remote node context is already setup to
166 * transition to its final state then this function does nothing. none
167 */
Dan Williams89a73012011-06-30 19:14:33 -0700168static void sci_remote_node_context_setup_to_resume(
169 struct sci_remote_node_context *sci_rnc,
Dave Jiange2023b82011-04-21 05:34:49 +0000170 scics_sds_remote_node_context_callback callback,
Dan Williams6f231dd2011-07-02 22:56:22 -0700171 void *callback_parameter)
172{
Dave Jiange2023b82011-04-21 05:34:49 +0000173 if (sci_rnc->destination_state != SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_FINAL) {
174 sci_rnc->destination_state = SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_READY;
175 sci_rnc->user_callback = callback;
176 sci_rnc->user_cookie = callback_parameter;
Dan Williams6f231dd2011-07-02 22:56:22 -0700177 }
178}
179
Dan Williams89a73012011-06-30 19:14:33 -0700180static void sci_remote_node_context_setup_to_destory(
181 struct sci_remote_node_context *sci_rnc,
Dave Jiange2023b82011-04-21 05:34:49 +0000182 scics_sds_remote_node_context_callback callback,
Dan Williams6f231dd2011-07-02 22:56:22 -0700183 void *callback_parameter)
184{
Dave Jiange2023b82011-04-21 05:34:49 +0000185 sci_rnc->destination_state = SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_FINAL;
186 sci_rnc->user_callback = callback;
187 sci_rnc->user_cookie = callback_parameter;
Dan Williams6f231dd2011-07-02 22:56:22 -0700188}
189
Dan Williams6f231dd2011-07-02 22:56:22 -0700190/**
191 *
192 *
193 * This method just calls the user callback function and then resets the
194 * callback.
195 */
Dan Williams89a73012011-06-30 19:14:33 -0700196static void sci_remote_node_context_notify_user(
197 struct sci_remote_node_context *rnc)
Dan Williams6f231dd2011-07-02 22:56:22 -0700198{
199 if (rnc->user_callback != NULL) {
200 (*rnc->user_callback)(rnc->user_cookie);
201
202 rnc->user_callback = NULL;
203 rnc->user_cookie = NULL;
204 }
205}
206
Dan Williams89a73012011-06-30 19:14:33 -0700207static void sci_remote_node_context_continue_state_transitions(struct sci_remote_node_context *rnc)
Dan Williams6f231dd2011-07-02 22:56:22 -0700208{
Dan Williamsed3efb72011-05-12 08:50:23 -0700209 if (rnc->destination_state == SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_READY)
Dan Williams89a73012011-06-30 19:14:33 -0700210 sci_remote_node_context_resume(rnc, rnc->user_callback,
Dan Williamsed3efb72011-05-12 08:50:23 -0700211 rnc->user_cookie);
Dan Williams6f231dd2011-07-02 22:56:22 -0700212}
213
Dan Williams89a73012011-06-30 19:14:33 -0700214static void sci_remote_node_context_validate_context_buffer(struct sci_remote_node_context *sci_rnc)
Dan Williams6f231dd2011-07-02 22:56:22 -0700215{
Dan Williams89a73012011-06-30 19:14:33 -0700216 union scu_remote_node_context *rnc_buffer;
Dan Williams78a6f062011-06-30 16:31:37 -0700217 struct isci_remote_device *idev = rnc_to_dev(sci_rnc);
218 struct domain_device *dev = idev->domain_dev;
Dan Williams89a73012011-06-30 19:14:33 -0700219 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williams6f231dd2011-07-02 22:56:22 -0700220
Dan Williams89a73012011-06-30 19:14:33 -0700221 rnc_buffer = sci_rnc_by_id(ihost, sci_rnc->remote_node_index);
Dan Williams6f231dd2011-07-02 22:56:22 -0700222
223 rnc_buffer->ssp.is_valid = true;
224
Dan Williams78a6f062011-06-30 16:31:37 -0700225 if (!idev->is_direct_attached &&
Dan Williamsa1a113b2011-04-21 18:44:45 -0700226 (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))) {
Dan Williams89a73012011-06-30 19:14:33 -0700227 sci_remote_device_post_request(idev, SCU_CONTEXT_COMMAND_POST_RNC_96);
Dan Williams6f231dd2011-07-02 22:56:22 -0700228 } else {
Dan Williams89a73012011-06-30 19:14:33 -0700229 sci_remote_device_post_request(idev, SCU_CONTEXT_COMMAND_POST_RNC_32);
Dan Williams6f231dd2011-07-02 22:56:22 -0700230
Dan Williams89a73012011-06-30 19:14:33 -0700231 if (idev->is_direct_attached)
232 sci_port_setup_transports(idev->owning_port,
233 sci_rnc->remote_node_index);
Dan Williams6f231dd2011-07-02 22:56:22 -0700234 }
235}
236
Dan Williams89a73012011-06-30 19:14:33 -0700237static void sci_remote_node_context_invalidate_context_buffer(struct sci_remote_node_context *sci_rnc)
Dan Williams6f231dd2011-07-02 22:56:22 -0700238{
239 union scu_remote_node_context *rnc_buffer;
Dan Williams89a73012011-06-30 19:14:33 -0700240 struct isci_remote_device *idev = rnc_to_dev(sci_rnc);
241 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williams6f231dd2011-07-02 22:56:22 -0700242
Dan Williams89a73012011-06-30 19:14:33 -0700243 rnc_buffer = sci_rnc_by_id(ihost, sci_rnc->remote_node_index);
Dan Williams6f231dd2011-07-02 22:56:22 -0700244
245 rnc_buffer->ssp.is_valid = false;
246
Dan Williams89a73012011-06-30 19:14:33 -0700247 sci_remote_device_post_request(rnc_to_dev(sci_rnc),
248 SCU_CONTEXT_COMMAND_POST_RNC_INVALIDATE);
Dan Williams6f231dd2011-07-02 22:56:22 -0700249}
250
Dan Williams89a73012011-06-30 19:14:33 -0700251static void sci_remote_node_context_initial_state_enter(struct sci_base_state_machine *sm)
Dan Williams6f231dd2011-07-02 22:56:22 -0700252{
Dan Williams89a73012011-06-30 19:14:33 -0700253 struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
Dan Williams6f231dd2011-07-02 22:56:22 -0700254
Dan Williamsf34d9e52011-05-12 09:27:52 -0700255 /* Check to see if we have gotten back to the initial state because
256 * someone requested to destroy the remote node context object.
257 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000258 if (sm->previous_state_id == SCI_RNC_INVALIDATING) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700259 rnc->destination_state = SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_UNSPECIFIED;
Dan Williams89a73012011-06-30 19:14:33 -0700260 sci_remote_node_context_notify_user(rnc);
Dan Williams6f231dd2011-07-02 22:56:22 -0700261 }
262}
263
Dan Williams89a73012011-06-30 19:14:33 -0700264static void sci_remote_node_context_posting_state_enter(struct sci_base_state_machine *sm)
Dan Williams6f231dd2011-07-02 22:56:22 -0700265{
Dan Williams89a73012011-06-30 19:14:33 -0700266 struct sci_remote_node_context *sci_rnc = container_of(sm, typeof(*sci_rnc), sm);
Dan Williams6f231dd2011-07-02 22:56:22 -0700267
Dan Williams89a73012011-06-30 19:14:33 -0700268 sci_remote_node_context_validate_context_buffer(sci_rnc);
Dan Williams6f231dd2011-07-02 22:56:22 -0700269}
270
Dan Williams89a73012011-06-30 19:14:33 -0700271static void sci_remote_node_context_invalidating_state_enter(struct sci_base_state_machine *sm)
Dan Williams6f231dd2011-07-02 22:56:22 -0700272{
Dan Williams89a73012011-06-30 19:14:33 -0700273 struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
Dan Williams6f231dd2011-07-02 22:56:22 -0700274
Dan Williams89a73012011-06-30 19:14:33 -0700275 sci_remote_node_context_invalidate_context_buffer(rnc);
Dan Williams6f231dd2011-07-02 22:56:22 -0700276}
277
Dan Williams89a73012011-06-30 19:14:33 -0700278static void sci_remote_node_context_resuming_state_enter(struct sci_base_state_machine *sm)
Dan Williams6f231dd2011-07-02 22:56:22 -0700279{
Dan Williams89a73012011-06-30 19:14:33 -0700280 struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
Dan Williams78a6f062011-06-30 16:31:37 -0700281 struct isci_remote_device *idev;
Dan Williamsa1a113b2011-04-21 18:44:45 -0700282 struct domain_device *dev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700283
Dan Williams78a6f062011-06-30 16:31:37 -0700284 idev = rnc_to_dev(rnc);
285 dev = idev->domain_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700286
Henryk Dembkowski24621462011-02-23 00:08:52 -0800287 /*
288 * For direct attached SATA devices we need to clear the TLCR
289 * NCQ to TCi tag mapping on the phy and in cases where we
290 * resume because of a target reset we also need to update
291 * the STPTLDARNI register with the RNi of the device
292 */
Dan Williamsa1a113b2011-04-21 18:44:45 -0700293 if ((dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) &&
Dan Williams78a6f062011-06-30 16:31:37 -0700294 idev->is_direct_attached)
Dan Williams89a73012011-06-30 19:14:33 -0700295 sci_port_setup_transports(idev->owning_port,
Dan Williams96143952011-04-19 18:35:58 -0700296 rnc->remote_node_index);
Henryk Dembkowski24621462011-02-23 00:08:52 -0800297
Dan Williams89a73012011-06-30 19:14:33 -0700298 sci_remote_device_post_request(idev, SCU_CONTEXT_COMMAND_POST_RNC_RESUME);
Dan Williams6f231dd2011-07-02 22:56:22 -0700299}
300
Dan Williams89a73012011-06-30 19:14:33 -0700301static void sci_remote_node_context_ready_state_enter(struct sci_base_state_machine *sm)
Dan Williams6f231dd2011-07-02 22:56:22 -0700302{
Dan Williams89a73012011-06-30 19:14:33 -0700303 struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
Dan Williams6f231dd2011-07-02 22:56:22 -0700304
Dan Williams6f231dd2011-07-02 22:56:22 -0700305 rnc->destination_state = SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_UNSPECIFIED;
306
Dan Williamsf34d9e52011-05-12 09:27:52 -0700307 if (rnc->user_callback)
Dan Williams89a73012011-06-30 19:14:33 -0700308 sci_remote_node_context_notify_user(rnc);
Dan Williams6f231dd2011-07-02 22:56:22 -0700309}
310
Dan Williams89a73012011-06-30 19:14:33 -0700311static void sci_remote_node_context_tx_suspended_state_enter(struct sci_base_state_machine *sm)
Dan Williams6f231dd2011-07-02 22:56:22 -0700312{
Dan Williams89a73012011-06-30 19:14:33 -0700313 struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
Dan Williams6f231dd2011-07-02 22:56:22 -0700314
Dan Williams89a73012011-06-30 19:14:33 -0700315 sci_remote_node_context_continue_state_transitions(rnc);
Dan Williams6f231dd2011-07-02 22:56:22 -0700316}
317
Dan Williams89a73012011-06-30 19:14:33 -0700318static void sci_remote_node_context_tx_rx_suspended_state_enter(struct sci_base_state_machine *sm)
Dan Williams6f231dd2011-07-02 22:56:22 -0700319{
Dan Williams89a73012011-06-30 19:14:33 -0700320 struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
Dan Williams6f231dd2011-07-02 22:56:22 -0700321
Dan Williams89a73012011-06-30 19:14:33 -0700322 sci_remote_node_context_continue_state_transitions(rnc);
Dan Williams6f231dd2011-07-02 22:56:22 -0700323}
324
Dan Williams89a73012011-06-30 19:14:33 -0700325static const struct sci_base_state sci_remote_node_context_state_table[] = {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000326 [SCI_RNC_INITIAL] = {
Dan Williams89a73012011-06-30 19:14:33 -0700327 .enter_state = sci_remote_node_context_initial_state_enter,
Dan Williams6f231dd2011-07-02 22:56:22 -0700328 },
Edmund Nadolskie3013702011-06-02 00:10:43 +0000329 [SCI_RNC_POSTING] = {
Dan Williams89a73012011-06-30 19:14:33 -0700330 .enter_state = sci_remote_node_context_posting_state_enter,
Dan Williams6f231dd2011-07-02 22:56:22 -0700331 },
Edmund Nadolskie3013702011-06-02 00:10:43 +0000332 [SCI_RNC_INVALIDATING] = {
Dan Williams89a73012011-06-30 19:14:33 -0700333 .enter_state = sci_remote_node_context_invalidating_state_enter,
Dan Williams6f231dd2011-07-02 22:56:22 -0700334 },
Edmund Nadolskie3013702011-06-02 00:10:43 +0000335 [SCI_RNC_RESUMING] = {
Dan Williams89a73012011-06-30 19:14:33 -0700336 .enter_state = sci_remote_node_context_resuming_state_enter,
Dan Williams6f231dd2011-07-02 22:56:22 -0700337 },
Edmund Nadolskie3013702011-06-02 00:10:43 +0000338 [SCI_RNC_READY] = {
Dan Williams89a73012011-06-30 19:14:33 -0700339 .enter_state = sci_remote_node_context_ready_state_enter,
Dan Williams6f231dd2011-07-02 22:56:22 -0700340 },
Edmund Nadolskie3013702011-06-02 00:10:43 +0000341 [SCI_RNC_TX_SUSPENDED] = {
Dan Williams89a73012011-06-30 19:14:33 -0700342 .enter_state = sci_remote_node_context_tx_suspended_state_enter,
Dan Williams6f231dd2011-07-02 22:56:22 -0700343 },
Edmund Nadolskie3013702011-06-02 00:10:43 +0000344 [SCI_RNC_TX_RX_SUSPENDED] = {
Dan Williams89a73012011-06-30 19:14:33 -0700345 .enter_state = sci_remote_node_context_tx_rx_suspended_state_enter,
Dan Williams6f231dd2011-07-02 22:56:22 -0700346 },
Edmund Nadolskie3013702011-06-02 00:10:43 +0000347 [SCI_RNC_AWAIT_SUSPENSION] = { },
Dan Williams6f231dd2011-07-02 22:56:22 -0700348};
349
Dan Williams89a73012011-06-30 19:14:33 -0700350void sci_remote_node_context_construct(struct sci_remote_node_context *rnc,
Dan Williams96143952011-04-19 18:35:58 -0700351 u16 remote_node_index)
Dan Williams35173d52011-03-26 16:43:01 -0700352{
Dan Williams89a73012011-06-30 19:14:33 -0700353 memset(rnc, 0, sizeof(struct sci_remote_node_context));
Dan Williams35173d52011-03-26 16:43:01 -0700354
355 rnc->remote_node_index = remote_node_index;
Dan Williams35173d52011-03-26 16:43:01 -0700356 rnc->destination_state = SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_UNSPECIFIED;
357
Dan Williams89a73012011-06-30 19:14:33 -0700358 sci_init_sm(&rnc->sm, sci_remote_node_context_state_table, SCI_RNC_INITIAL);
Dan Williams35173d52011-03-26 16:43:01 -0700359}
Dan Williams338e3862011-05-12 07:46:59 -0700360
Dan Williams89a73012011-06-30 19:14:33 -0700361enum sci_status sci_remote_node_context_event_handler(struct sci_remote_node_context *sci_rnc,
Dan Williams338e3862011-05-12 07:46:59 -0700362 u32 event_code)
363{
364 enum scis_sds_remote_node_context_states state;
365
Edmund Nadolskie3013702011-06-02 00:10:43 +0000366 state = sci_rnc->sm.current_state_id;
Dan Williams338e3862011-05-12 07:46:59 -0700367 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000368 case SCI_RNC_POSTING:
Dan Williams338e3862011-05-12 07:46:59 -0700369 switch (scu_get_event_code(event_code)) {
370 case SCU_EVENT_POST_RNC_COMPLETE:
Edmund Nadolskie3013702011-06-02 00:10:43 +0000371 sci_change_state(&sci_rnc->sm, SCI_RNC_READY);
Dan Williams338e3862011-05-12 07:46:59 -0700372 break;
373 default:
374 goto out;
375 }
376 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000377 case SCI_RNC_INVALIDATING:
Dan Williams338e3862011-05-12 07:46:59 -0700378 if (scu_get_event_code(event_code) == SCU_EVENT_POST_RNC_INVALIDATE_COMPLETE) {
379 if (sci_rnc->destination_state == SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_FINAL)
Edmund Nadolskie3013702011-06-02 00:10:43 +0000380 state = SCI_RNC_INITIAL;
Dan Williams338e3862011-05-12 07:46:59 -0700381 else
Edmund Nadolskie3013702011-06-02 00:10:43 +0000382 state = SCI_RNC_POSTING;
383 sci_change_state(&sci_rnc->sm, state);
Dan Williams338e3862011-05-12 07:46:59 -0700384 } else {
385 switch (scu_get_event_type(event_code)) {
386 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
387 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
388 /* We really dont care if the hardware is going to suspend
389 * the device since it's being invalidated anyway */
390 dev_dbg(scirdev_to_dev(rnc_to_dev(sci_rnc)),
391 "%s: SCIC Remote Node Context 0x%p was "
392 "suspeneded by hardware while being "
393 "invalidated.\n", __func__, sci_rnc);
394 break;
395 default:
396 goto out;
397 }
398 }
399 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000400 case SCI_RNC_RESUMING:
Dan Williams338e3862011-05-12 07:46:59 -0700401 if (scu_get_event_code(event_code) == SCU_EVENT_POST_RCN_RELEASE) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000402 sci_change_state(&sci_rnc->sm, SCI_RNC_READY);
Dan Williams338e3862011-05-12 07:46:59 -0700403 } else {
404 switch (scu_get_event_type(event_code)) {
405 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
406 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
407 /* We really dont care if the hardware is going to suspend
408 * the device since it's being resumed anyway */
409 dev_dbg(scirdev_to_dev(rnc_to_dev(sci_rnc)),
410 "%s: SCIC Remote Node Context 0x%p was "
411 "suspeneded by hardware while being resumed.\n",
412 __func__, sci_rnc);
413 break;
414 default:
415 goto out;
416 }
417 }
418 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000419 case SCI_RNC_READY:
Dan Williams338e3862011-05-12 07:46:59 -0700420 switch (scu_get_event_type(event_code)) {
421 case SCU_EVENT_TL_RNC_SUSPEND_TX:
Edmund Nadolskie3013702011-06-02 00:10:43 +0000422 sci_change_state(&sci_rnc->sm, SCI_RNC_TX_SUSPENDED);
Dan Williams338e3862011-05-12 07:46:59 -0700423 sci_rnc->suspension_code = scu_get_event_specifier(event_code);
424 break;
425 case SCU_EVENT_TL_RNC_SUSPEND_TX_RX:
Edmund Nadolskie3013702011-06-02 00:10:43 +0000426 sci_change_state(&sci_rnc->sm, SCI_RNC_TX_RX_SUSPENDED);
Dan Williams338e3862011-05-12 07:46:59 -0700427 sci_rnc->suspension_code = scu_get_event_specifier(event_code);
428 break;
429 default:
430 goto out;
431 }
432 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000433 case SCI_RNC_AWAIT_SUSPENSION:
Dan Williams338e3862011-05-12 07:46:59 -0700434 switch (scu_get_event_type(event_code)) {
435 case SCU_EVENT_TL_RNC_SUSPEND_TX:
Edmund Nadolskie3013702011-06-02 00:10:43 +0000436 sci_change_state(&sci_rnc->sm, SCI_RNC_TX_SUSPENDED);
Dan Williams338e3862011-05-12 07:46:59 -0700437 sci_rnc->suspension_code = scu_get_event_specifier(event_code);
438 break;
439 case SCU_EVENT_TL_RNC_SUSPEND_TX_RX:
Edmund Nadolskie3013702011-06-02 00:10:43 +0000440 sci_change_state(&sci_rnc->sm, SCI_RNC_TX_RX_SUSPENDED);
Dan Williams338e3862011-05-12 07:46:59 -0700441 sci_rnc->suspension_code = scu_get_event_specifier(event_code);
442 break;
443 default:
444 goto out;
445 }
446 break;
447 default:
448 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
449 "%s: invalid state %d\n", __func__, state);
450 return SCI_FAILURE_INVALID_STATE;
451 }
452 return SCI_SUCCESS;
453
454 out:
455 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
456 "%s: code: %#x state: %d\n", __func__, event_code, state);
457 return SCI_FAILURE;
458
459}
Dan Williamsc845ae92011-05-12 08:26:56 -0700460
Dan Williams89a73012011-06-30 19:14:33 -0700461enum sci_status sci_remote_node_context_destruct(struct sci_remote_node_context *sci_rnc,
Dan Williamsc845ae92011-05-12 08:26:56 -0700462 scics_sds_remote_node_context_callback cb_fn,
463 void *cb_p)
464{
465 enum scis_sds_remote_node_context_states state;
466
Edmund Nadolskie3013702011-06-02 00:10:43 +0000467 state = sci_rnc->sm.current_state_id;
Dan Williamsc845ae92011-05-12 08:26:56 -0700468 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000469 case SCI_RNC_INVALIDATING:
Dan Williams89a73012011-06-30 19:14:33 -0700470 sci_remote_node_context_setup_to_destory(sci_rnc, cb_fn, cb_p);
Dan Williamsc845ae92011-05-12 08:26:56 -0700471 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000472 case SCI_RNC_POSTING:
473 case SCI_RNC_RESUMING:
474 case SCI_RNC_READY:
475 case SCI_RNC_TX_SUSPENDED:
476 case SCI_RNC_TX_RX_SUSPENDED:
477 case SCI_RNC_AWAIT_SUSPENSION:
Dan Williams89a73012011-06-30 19:14:33 -0700478 sci_remote_node_context_setup_to_destory(sci_rnc, cb_fn, cb_p);
Edmund Nadolskie3013702011-06-02 00:10:43 +0000479 sci_change_state(&sci_rnc->sm, SCI_RNC_INVALIDATING);
Dan Williamsc845ae92011-05-12 08:26:56 -0700480 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000481 case SCI_RNC_INITIAL:
Dan Williamsc845ae92011-05-12 08:26:56 -0700482 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
483 "%s: invalid state %d\n", __func__, state);
484 /* 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)),
491 "%s: invalid state %d\n", __func__, state);
492 return SCI_FAILURE_INVALID_STATE;
493 }
494}
Dan Williamsed3efb72011-05-12 08:50:23 -0700495
Dan Williams89a73012011-06-30 19:14:33 -0700496enum sci_status sci_remote_node_context_suspend(struct sci_remote_node_context *sci_rnc,
Dan Williamsed3efb72011-05-12 08:50:23 -0700497 u32 suspend_type,
498 scics_sds_remote_node_context_callback cb_fn,
499 void *cb_p)
500{
501 enum scis_sds_remote_node_context_states state;
502
Edmund Nadolskie3013702011-06-02 00:10:43 +0000503 state = sci_rnc->sm.current_state_id;
504 if (state != SCI_RNC_READY) {
Dan Williamsed3efb72011-05-12 08:50:23 -0700505 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
506 "%s: invalid state %d\n", __func__, state);
507 return SCI_FAILURE_INVALID_STATE;
508 }
509
510 sci_rnc->user_callback = cb_fn;
511 sci_rnc->user_cookie = cb_p;
512 sci_rnc->suspension_code = suspend_type;
513
514 if (suspend_type == SCI_SOFTWARE_SUSPENSION) {
Dan Williams89a73012011-06-30 19:14:33 -0700515 sci_remote_device_post_request(rnc_to_dev(sci_rnc),
Dan Williamsed3efb72011-05-12 08:50:23 -0700516 SCU_CONTEXT_COMMAND_POST_RNC_SUSPEND_TX);
517 }
518
Edmund Nadolskie3013702011-06-02 00:10:43 +0000519 sci_change_state(&sci_rnc->sm, SCI_RNC_AWAIT_SUSPENSION);
Dan Williamsed3efb72011-05-12 08:50:23 -0700520 return SCI_SUCCESS;
521}
522
Dan Williams89a73012011-06-30 19:14:33 -0700523enum sci_status sci_remote_node_context_resume(struct sci_remote_node_context *sci_rnc,
Dan Williamsed3efb72011-05-12 08:50:23 -0700524 scics_sds_remote_node_context_callback cb_fn,
525 void *cb_p)
526{
527 enum scis_sds_remote_node_context_states state;
528
Edmund Nadolskie3013702011-06-02 00:10:43 +0000529 state = sci_rnc->sm.current_state_id;
Dan Williamsed3efb72011-05-12 08:50:23 -0700530 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000531 case SCI_RNC_INITIAL:
Dan Williamsed3efb72011-05-12 08:50:23 -0700532 if (sci_rnc->remote_node_index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX)
533 return SCI_FAILURE_INVALID_STATE;
534
Dan Williams89a73012011-06-30 19:14:33 -0700535 sci_remote_node_context_setup_to_resume(sci_rnc, cb_fn, cb_p);
536 sci_remote_node_context_construct_buffer(sci_rnc);
Edmund Nadolskie3013702011-06-02 00:10:43 +0000537 sci_change_state(&sci_rnc->sm, SCI_RNC_POSTING);
Dan Williamsed3efb72011-05-12 08:50:23 -0700538 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000539 case SCI_RNC_POSTING:
540 case SCI_RNC_INVALIDATING:
541 case SCI_RNC_RESUMING:
Dan Williamsed3efb72011-05-12 08:50:23 -0700542 if (sci_rnc->destination_state != SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_READY)
543 return SCI_FAILURE_INVALID_STATE;
544
545 sci_rnc->user_callback = cb_fn;
546 sci_rnc->user_cookie = cb_p;
547 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000548 case SCI_RNC_TX_SUSPENDED: {
Dan Williams78a6f062011-06-30 16:31:37 -0700549 struct isci_remote_device *idev = rnc_to_dev(sci_rnc);
550 struct domain_device *dev = idev->domain_dev;
Dan Williamsed3efb72011-05-12 08:50:23 -0700551
Dan Williams89a73012011-06-30 19:14:33 -0700552 sci_remote_node_context_setup_to_resume(sci_rnc, cb_fn, cb_p);
Dan Williamsed3efb72011-05-12 08:50:23 -0700553
554 /* TODO: consider adding a resume action of NONE, INVALIDATE, WRITE_TLCR */
555 if (dev->dev_type == SAS_END_DEV || dev_is_expander(dev))
Edmund Nadolskie3013702011-06-02 00:10:43 +0000556 sci_change_state(&sci_rnc->sm, SCI_RNC_RESUMING);
Dan Williamsed3efb72011-05-12 08:50:23 -0700557 else if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
Dan Williams78a6f062011-06-30 16:31:37 -0700558 if (idev->is_direct_attached) {
Dan Williamsed3efb72011-05-12 08:50:23 -0700559 /* @todo Fix this since I am being silly in writing to the STPTLDARNI register. */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000560 sci_change_state(&sci_rnc->sm, SCI_RNC_RESUMING);
Dan Williamsed3efb72011-05-12 08:50:23 -0700561 } else {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000562 sci_change_state(&sci_rnc->sm, SCI_RNC_INVALIDATING);
Dan Williamsed3efb72011-05-12 08:50:23 -0700563 }
564 } else
565 return SCI_FAILURE;
566 return SCI_SUCCESS;
567 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000568 case SCI_RNC_TX_RX_SUSPENDED:
Dan Williams89a73012011-06-30 19:14:33 -0700569 sci_remote_node_context_setup_to_resume(sci_rnc, cb_fn, cb_p);
Edmund Nadolskie3013702011-06-02 00:10:43 +0000570 sci_change_state(&sci_rnc->sm, SCI_RNC_RESUMING);
Dan Williamsed3efb72011-05-12 08:50:23 -0700571 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000572 case SCI_RNC_AWAIT_SUSPENSION:
Dan Williams89a73012011-06-30 19:14:33 -0700573 sci_remote_node_context_setup_to_resume(sci_rnc, cb_fn, cb_p);
Dan Williamsed3efb72011-05-12 08:50:23 -0700574 return SCI_SUCCESS;
575 default:
576 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
577 "%s: invalid state %d\n", __func__, state);
578 return SCI_FAILURE_INVALID_STATE;
579 }
580}
Dan Williamsf34d9e52011-05-12 09:27:52 -0700581
Dan Williams89a73012011-06-30 19:14:33 -0700582enum sci_status sci_remote_node_context_start_io(struct sci_remote_node_context *sci_rnc,
Dan Williams5076a1a2011-06-27 14:57:03 -0700583 struct isci_request *ireq)
Dan Williamsf34d9e52011-05-12 09:27:52 -0700584{
585 enum scis_sds_remote_node_context_states state;
586
Edmund Nadolskie3013702011-06-02 00:10:43 +0000587 state = sci_rnc->sm.current_state_id;
Jeff Skirvinfd536602011-06-20 14:09:22 -0700588
589 switch (state) {
590 case SCI_RNC_READY:
591 return SCI_SUCCESS;
592 case SCI_RNC_TX_SUSPENDED:
593 case SCI_RNC_TX_RX_SUSPENDED:
594 case SCI_RNC_AWAIT_SUSPENSION:
Dan Williamsf34d9e52011-05-12 09:27:52 -0700595 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
596 "%s: invalid state %d\n", __func__, state);
597 return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
Jeff Skirvinfd536602011-06-20 14:09:22 -0700598 default:
599 break;
Dan Williamsf34d9e52011-05-12 09:27:52 -0700600 }
Jeff Skirvinfd536602011-06-20 14:09:22 -0700601 dev_dbg(scirdev_to_dev(rnc_to_dev(sci_rnc)),
602 "%s: requested to start IO while still resuming, %d\n",
603 __func__, state);
604 return SCI_FAILURE_INVALID_STATE;
Dan Williamsf34d9e52011-05-12 09:27:52 -0700605}
606
Dan Williams89a73012011-06-30 19:14:33 -0700607enum sci_status sci_remote_node_context_start_task(struct sci_remote_node_context *sci_rnc,
Dan Williams5076a1a2011-06-27 14:57:03 -0700608 struct isci_request *ireq)
Dan Williamsf34d9e52011-05-12 09:27:52 -0700609{
610 enum scis_sds_remote_node_context_states state;
611
Edmund Nadolskie3013702011-06-02 00:10:43 +0000612 state = sci_rnc->sm.current_state_id;
Dan Williamsf34d9e52011-05-12 09:27:52 -0700613 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000614 case SCI_RNC_RESUMING:
615 case SCI_RNC_READY:
616 case SCI_RNC_AWAIT_SUSPENSION:
Dan Williamsf34d9e52011-05-12 09:27:52 -0700617 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000618 case SCI_RNC_TX_SUSPENDED:
619 case SCI_RNC_TX_RX_SUSPENDED:
Dan Williams89a73012011-06-30 19:14:33 -0700620 sci_remote_node_context_resume(sci_rnc, NULL, NULL);
Dan Williamsf34d9e52011-05-12 09:27:52 -0700621 return SCI_SUCCESS;
622 default:
623 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
624 "%s: invalid state %d\n", __func__, state);
625 return SCI_FAILURE_INVALID_STATE;
626 }
627}