isci: Handle all suspending TC completions
[deliverable/linux.git] / drivers / scsi / isci / remote_node_context.c
CommitLineData
6f231dda
DW
1/*
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
cc9203bf 56#include "host.h"
12ef6544 57#include "isci.h"
88f3b62a
DW
58#include "remote_device.h"
59#include "remote_node_context.h"
6f231dda
DW
60#include "scu_event_codes.h"
61#include "scu_task_context.h"
62
d7a0ccdd
DW
63#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;
6f231dda 68
d7a0ccdd
DW
69 return strings[state];
70}
71#undef C
6f231dda
DW
72
73/**
74 *
e2023b87 75 * @sci_rnc: The state of the remote node context object to check.
6f231dda
DW
76 *
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 */
89a7301f
DW
81bool sci_remote_node_context_is_ready(
82 struct sci_remote_node_context *sci_rnc)
6f231dda 83{
e301370a 84 u32 current_state = sci_rnc->sm.current_state_id;
6f231dda 85
e301370a 86 if (current_state == SCI_RNC_READY) {
6f231dda
DW
87 return true;
88 }
89
90 return false;
91}
92
89a7301f
DW
93static 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)
6f231dda 103{
78a6f06e
DW
104 struct isci_remote_device *idev = rnc_to_dev(sci_rnc);
105 struct domain_device *dev = idev->domain_dev;
1f4fa1f9 106 int rni = sci_rnc->remote_node_index;
a1a113b0 107 union scu_remote_node_context *rnc;
d9dcb4ba 108 struct isci_host *ihost;
a3d568f0 109 __le64 sas_addr;
6f231dda 110
34a99158 111 ihost = idev->owning_port->owning_controller;
89a7301f 112 rnc = sci_rnc_by_id(ihost, rni);
6f231dda 113
9614395e 114 memset(rnc, 0, sizeof(union scu_remote_node_context)
89a7301f 115 * sci_remote_device_node_count(idev));
6f231dda 116
1f4fa1f9 117 rnc->ssp.remote_node_index = rni;
78a6f06e
DW
118 rnc->ssp.remote_node_port_width = idev->device_port_width;
119 rnc->ssp.logical_port_index = idev->owning_port->physical_port_index;
6f231dda 120
a3d568f0
DW
121 /* 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);
6f231dda
DW
125
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
11cc5183 134 if (dev_is_sata(dev)) {
6f231dda 135 rnc->ssp.connection_occupancy_timeout =
89a7301f 136 ihost->user_parameters.stp_max_occupancy_timeout;
6f231dda 137 rnc->ssp.connection_inactivity_timeout =
89a7301f 138 ihost->user_parameters.stp_inactivity_timeout;
6f231dda
DW
139 } else {
140 rnc->ssp.connection_occupancy_timeout =
89a7301f 141 ihost->user_parameters.ssp_max_occupancy_timeout;
6f231dda 142 rnc->ssp.connection_inactivity_timeout =
89a7301f 143 ihost->user_parameters.ssp_inactivity_timeout;
6f231dda
DW
144 }
145
146 rnc->ssp.initial_arbitration_wait_time = 0;
147
148 /* Open Address Frame Parameters */
78a6f06e 149 rnc->ssp.oaf_connection_rate = idev->connection_rate;
6f231dda
DW
150 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 *
e2023b87
DJ
157 * @sci_rnc:
158 * @callback:
6f231dda
DW
159 * @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 */
89a7301f
DW
165static void sci_remote_node_context_setup_to_resume(
166 struct sci_remote_node_context *sci_rnc,
e2023b87 167 scics_sds_remote_node_context_callback callback,
6f231dda
DW
168 void *callback_parameter)
169{
e2023b87
DJ
170 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;
6f231dda
DW
174 }
175}
176
89a7301f
DW
177static void sci_remote_node_context_setup_to_destory(
178 struct sci_remote_node_context *sci_rnc,
e2023b87 179 scics_sds_remote_node_context_callback callback,
6f231dda
DW
180 void *callback_parameter)
181{
e2023b87
DJ
182 sci_rnc->destination_state = SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_FINAL;
183 sci_rnc->user_callback = callback;
184 sci_rnc->user_cookie = callback_parameter;
6f231dda
DW
185}
186
6f231dda
DW
187/**
188 *
189 *
190 * This method just calls the user callback function and then resets the
191 * callback.
192 */
89a7301f
DW
193static void sci_remote_node_context_notify_user(
194 struct sci_remote_node_context *rnc)
6f231dda
DW
195{
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
89a7301f 204static void sci_remote_node_context_continue_state_transitions(struct sci_remote_node_context *rnc)
6f231dda 205{
ed3efb77 206 if (rnc->destination_state == SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_READY)
89a7301f 207 sci_remote_node_context_resume(rnc, rnc->user_callback,
ed3efb77 208 rnc->user_cookie);
6f231dda
DW
209}
210
89a7301f 211static void sci_remote_node_context_validate_context_buffer(struct sci_remote_node_context *sci_rnc)
6f231dda 212{
89a7301f 213 union scu_remote_node_context *rnc_buffer;
78a6f06e
DW
214 struct isci_remote_device *idev = rnc_to_dev(sci_rnc);
215 struct domain_device *dev = idev->domain_dev;
89a7301f 216 struct isci_host *ihost = idev->owning_port->owning_controller;
6f231dda 217
89a7301f 218 rnc_buffer = sci_rnc_by_id(ihost, sci_rnc->remote_node_index);
6f231dda
DW
219
220 rnc_buffer->ssp.is_valid = true;
221
11cc5183 222 if (dev_is_sata(dev) && dev->parent) {
89a7301f 223 sci_remote_device_post_request(idev, SCU_CONTEXT_COMMAND_POST_RNC_96);
6f231dda 224 } else {
89a7301f 225 sci_remote_device_post_request(idev, SCU_CONTEXT_COMMAND_POST_RNC_32);
6f231dda 226
11cc5183 227 if (!dev->parent)
89a7301f
DW
228 sci_port_setup_transports(idev->owning_port,
229 sci_rnc->remote_node_index);
6f231dda
DW
230 }
231}
232
89a7301f 233static void sci_remote_node_context_invalidate_context_buffer(struct sci_remote_node_context *sci_rnc)
6f231dda
DW
234{
235 union scu_remote_node_context *rnc_buffer;
89a7301f
DW
236 struct isci_remote_device *idev = rnc_to_dev(sci_rnc);
237 struct isci_host *ihost = idev->owning_port->owning_controller;
6f231dda 238
89a7301f 239 rnc_buffer = sci_rnc_by_id(ihost, sci_rnc->remote_node_index);
6f231dda
DW
240
241 rnc_buffer->ssp.is_valid = false;
242
89a7301f
DW
243 sci_remote_device_post_request(rnc_to_dev(sci_rnc),
244 SCU_CONTEXT_COMMAND_POST_RNC_INVALIDATE);
6f231dda
DW
245}
246
89a7301f 247static void sci_remote_node_context_initial_state_enter(struct sci_base_state_machine *sm)
6f231dda 248{
89a7301f 249 struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
6f231dda 250
f34d9e5d
DW
251 /* Check to see if we have gotten back to the initial state because
252 * someone requested to destroy the remote node context object.
253 */
e301370a 254 if (sm->previous_state_id == SCI_RNC_INVALIDATING) {
6f231dda 255 rnc->destination_state = SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_UNSPECIFIED;
89a7301f 256 sci_remote_node_context_notify_user(rnc);
6f231dda
DW
257 }
258}
259
89a7301f 260static void sci_remote_node_context_posting_state_enter(struct sci_base_state_machine *sm)
6f231dda 261{
89a7301f 262 struct sci_remote_node_context *sci_rnc = container_of(sm, typeof(*sci_rnc), sm);
6f231dda 263
89a7301f 264 sci_remote_node_context_validate_context_buffer(sci_rnc);
6f231dda
DW
265}
266
89a7301f 267static void sci_remote_node_context_invalidating_state_enter(struct sci_base_state_machine *sm)
6f231dda 268{
89a7301f 269 struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
6f231dda 270
89a7301f 271 sci_remote_node_context_invalidate_context_buffer(rnc);
6f231dda
DW
272}
273
89a7301f 274static void sci_remote_node_context_resuming_state_enter(struct sci_base_state_machine *sm)
6f231dda 275{
89a7301f 276 struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
78a6f06e 277 struct isci_remote_device *idev;
a1a113b0 278 struct domain_device *dev;
6f231dda 279
78a6f06e
DW
280 idev = rnc_to_dev(rnc);
281 dev = idev->domain_dev;
6f231dda 282
24621466
HD
283 /*
284 * For direct attached SATA devices we need to clear the TLCR
285 * NCQ to TCi tag mapping on the phy and in cases where we
286 * resume because of a target reset we also need to update
287 * the STPTLDARNI register with the RNi of the device
288 */
11cc5183
DW
289 if (dev_is_sata(dev) && !dev->parent)
290 sci_port_setup_transports(idev->owning_port, rnc->remote_node_index);
24621466 291
89a7301f 292 sci_remote_device_post_request(idev, SCU_CONTEXT_COMMAND_POST_RNC_RESUME);
6f231dda
DW
293}
294
89a7301f 295static void sci_remote_node_context_ready_state_enter(struct sci_base_state_machine *sm)
6f231dda 296{
89a7301f 297 struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
6f231dda 298
6f231dda
DW
299 rnc->destination_state = SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_UNSPECIFIED;
300
f34d9e5d 301 if (rnc->user_callback)
89a7301f 302 sci_remote_node_context_notify_user(rnc);
6f231dda
DW
303}
304
89a7301f 305static void sci_remote_node_context_tx_suspended_state_enter(struct sci_base_state_machine *sm)
6f231dda 306{
89a7301f 307 struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
6f231dda 308
89a7301f 309 sci_remote_node_context_continue_state_transitions(rnc);
6f231dda
DW
310}
311
89a7301f 312static void sci_remote_node_context_tx_rx_suspended_state_enter(struct sci_base_state_machine *sm)
6f231dda 313{
89a7301f 314 struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
6f231dda 315
89a7301f 316 sci_remote_node_context_continue_state_transitions(rnc);
6f231dda
DW
317}
318
6f48844e
JS
319static void sci_remote_node_context_await_suspend_state_exit(
320 struct sci_base_state_machine *sm)
321{
322 struct sci_remote_node_context *rnc
323 = container_of(sm, typeof(*rnc), sm);
324
325 isci_dev_set_hang_detection_timeout(rnc_to_dev(rnc), 0);
326}
327
89a7301f 328static const struct sci_base_state sci_remote_node_context_state_table[] = {
e301370a 329 [SCI_RNC_INITIAL] = {
89a7301f 330 .enter_state = sci_remote_node_context_initial_state_enter,
6f231dda 331 },
e301370a 332 [SCI_RNC_POSTING] = {
89a7301f 333 .enter_state = sci_remote_node_context_posting_state_enter,
6f231dda 334 },
e301370a 335 [SCI_RNC_INVALIDATING] = {
89a7301f 336 .enter_state = sci_remote_node_context_invalidating_state_enter,
6f231dda 337 },
e301370a 338 [SCI_RNC_RESUMING] = {
89a7301f 339 .enter_state = sci_remote_node_context_resuming_state_enter,
6f231dda 340 },
e301370a 341 [SCI_RNC_READY] = {
89a7301f 342 .enter_state = sci_remote_node_context_ready_state_enter,
6f231dda 343 },
e301370a 344 [SCI_RNC_TX_SUSPENDED] = {
89a7301f 345 .enter_state = sci_remote_node_context_tx_suspended_state_enter,
6f231dda 346 },
e301370a 347 [SCI_RNC_TX_RX_SUSPENDED] = {
89a7301f 348 .enter_state = sci_remote_node_context_tx_rx_suspended_state_enter,
6f231dda 349 },
6f48844e
JS
350 [SCI_RNC_AWAIT_SUSPENSION] = {
351 .exit_state = sci_remote_node_context_await_suspend_state_exit,
352 },
6f231dda
DW
353};
354
89a7301f 355void sci_remote_node_context_construct(struct sci_remote_node_context *rnc,
9614395e 356 u16 remote_node_index)
35173d57 357{
89a7301f 358 memset(rnc, 0, sizeof(struct sci_remote_node_context));
35173d57
DW
359
360 rnc->remote_node_index = remote_node_index;
35173d57
DW
361 rnc->destination_state = SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_UNSPECIFIED;
362
89a7301f 363 sci_init_sm(&rnc->sm, sci_remote_node_context_state_table, SCI_RNC_INITIAL);
35173d57 364}
338e386d 365
89a7301f 366enum sci_status sci_remote_node_context_event_handler(struct sci_remote_node_context *sci_rnc,
338e386d
DW
367 u32 event_code)
368{
369 enum scis_sds_remote_node_context_states state;
ac78ed0f 370 u32 next_state;
338e386d 371
e301370a 372 state = sci_rnc->sm.current_state_id;
338e386d 373 switch (state) {
e301370a 374 case SCI_RNC_POSTING:
338e386d
DW
375 switch (scu_get_event_code(event_code)) {
376 case SCU_EVENT_POST_RNC_COMPLETE:
e301370a 377 sci_change_state(&sci_rnc->sm, SCI_RNC_READY);
338e386d
DW
378 break;
379 default:
380 goto out;
381 }
382 break;
e301370a 383 case SCI_RNC_INVALIDATING:
338e386d
DW
384 if (scu_get_event_code(event_code) == SCU_EVENT_POST_RNC_INVALIDATE_COMPLETE) {
385 if (sci_rnc->destination_state == SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_FINAL)
e301370a 386 state = SCI_RNC_INITIAL;
338e386d 387 else
e301370a
EN
388 state = SCI_RNC_POSTING;
389 sci_change_state(&sci_rnc->sm, state);
338e386d
DW
390 } else {
391 switch (scu_get_event_type(event_code)) {
392 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
393 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
394 /* We really dont care if the hardware is going to suspend
395 * the device since it's being invalidated anyway */
396 dev_dbg(scirdev_to_dev(rnc_to_dev(sci_rnc)),
397 "%s: SCIC Remote Node Context 0x%p was "
398 "suspeneded by hardware while being "
399 "invalidated.\n", __func__, sci_rnc);
400 break;
401 default:
402 goto out;
403 }
404 }
405 break;
e301370a 406 case SCI_RNC_RESUMING:
338e386d 407 if (scu_get_event_code(event_code) == SCU_EVENT_POST_RCN_RELEASE) {
e301370a 408 sci_change_state(&sci_rnc->sm, SCI_RNC_READY);
338e386d
DW
409 } else {
410 switch (scu_get_event_type(event_code)) {
411 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
412 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
413 /* We really dont care if the hardware is going to suspend
414 * the device since it's being resumed anyway */
415 dev_dbg(scirdev_to_dev(rnc_to_dev(sci_rnc)),
416 "%s: SCIC Remote Node Context 0x%p was "
417 "suspeneded by hardware while being resumed.\n",
418 __func__, sci_rnc);
419 break;
420 default:
421 goto out;
422 }
423 }
424 break;
e301370a 425 case SCI_RNC_READY:
338e386d
DW
426 switch (scu_get_event_type(event_code)) {
427 case SCU_EVENT_TL_RNC_SUSPEND_TX:
e301370a 428 sci_change_state(&sci_rnc->sm, SCI_RNC_TX_SUSPENDED);
ac78ed0f 429 sci_rnc->suspend_type = scu_get_event_type(event_code);
338e386d
DW
430 break;
431 case SCU_EVENT_TL_RNC_SUSPEND_TX_RX:
e301370a 432 sci_change_state(&sci_rnc->sm, SCI_RNC_TX_RX_SUSPENDED);
ac78ed0f 433 sci_rnc->suspend_type = scu_get_event_type(event_code);
338e386d
DW
434 break;
435 default:
436 goto out;
437 }
438 break;
e301370a 439 case SCI_RNC_AWAIT_SUSPENSION:
338e386d
DW
440 switch (scu_get_event_type(event_code)) {
441 case SCU_EVENT_TL_RNC_SUSPEND_TX:
ac78ed0f 442 next_state = SCI_RNC_TX_SUSPENDED;
338e386d
DW
443 break;
444 case SCU_EVENT_TL_RNC_SUSPEND_TX_RX:
ac78ed0f 445 next_state = SCI_RNC_TX_RX_SUSPENDED;
338e386d
DW
446 break;
447 default:
448 goto out;
449 }
ac78ed0f
JS
450 if (sci_rnc->suspend_type == scu_get_event_type(event_code))
451 sci_change_state(&sci_rnc->sm, next_state);
338e386d
DW
452 break;
453 default:
454 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
14e99b4a
DW
455 "%s: invalid state: %s\n", __func__,
456 rnc_state_name(state));
338e386d
DW
457 return SCI_FAILURE_INVALID_STATE;
458 }
459 return SCI_SUCCESS;
460
461 out:
462 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
14e99b4a
DW
463 "%s: code: %#x state: %s\n", __func__, event_code,
464 rnc_state_name(state));
338e386d
DW
465 return SCI_FAILURE;
466
467}
c845ae96 468
89a7301f 469enum sci_status sci_remote_node_context_destruct(struct sci_remote_node_context *sci_rnc,
c845ae96
DW
470 scics_sds_remote_node_context_callback cb_fn,
471 void *cb_p)
472{
473 enum scis_sds_remote_node_context_states state;
474
e301370a 475 state = sci_rnc->sm.current_state_id;
c845ae96 476 switch (state) {
e301370a 477 case SCI_RNC_INVALIDATING:
89a7301f 478 sci_remote_node_context_setup_to_destory(sci_rnc, cb_fn, cb_p);
c845ae96 479 return SCI_SUCCESS;
e301370a
EN
480 case SCI_RNC_POSTING:
481 case SCI_RNC_RESUMING:
482 case SCI_RNC_READY:
483 case SCI_RNC_TX_SUSPENDED:
484 case SCI_RNC_TX_RX_SUSPENDED:
485 case SCI_RNC_AWAIT_SUSPENSION:
89a7301f 486 sci_remote_node_context_setup_to_destory(sci_rnc, cb_fn, cb_p);
e301370a 487 sci_change_state(&sci_rnc->sm, SCI_RNC_INVALIDATING);
c845ae96 488 return SCI_SUCCESS;
e301370a 489 case SCI_RNC_INITIAL:
c845ae96 490 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
14e99b4a
DW
491 "%s: invalid state: %s\n", __func__,
492 rnc_state_name(state));
c845ae96
DW
493 /* We have decided that the destruct request on the remote node context
494 * can not fail since it is either in the initial/destroyed state or is
495 * can be destroyed.
496 */
497 return SCI_SUCCESS;
498 default:
499 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
14e99b4a
DW
500 "%s: invalid state %s\n", __func__,
501 rnc_state_name(state));
c845ae96
DW
502 return SCI_FAILURE_INVALID_STATE;
503 }
504}
ed3efb77 505
ac78ed0f
JS
506enum sci_status sci_remote_node_context_suspend(
507 struct sci_remote_node_context *sci_rnc,
508 enum sci_remote_node_suspension_reasons suspend_reason,
509 u32 suspend_type,
510 scics_sds_remote_node_context_callback cb_fn,
511 void *cb_p)
ed3efb77 512{
ac78ed0f
JS
513 enum scis_sds_remote_node_context_states state
514 = sci_rnc->sm.current_state_id;
515 struct isci_remote_device *idev = rnc_to_dev(sci_rnc);
516 enum sci_status status = SCI_FAILURE_INVALID_STATE;
ed3efb77 517
ac78ed0f
JS
518 /* Disable automatic state continuations if explicitly suspending. */
519 if (suspend_reason == SCI_SOFTWARE_SUSPENSION)
520 sci_rnc->destination_state
521 = SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_UNSPECIFIED;
522 switch (state) {
523 case SCI_RNC_READY:
524 break;
525 case SCI_RNC_TX_SUSPENDED:
526 if (suspend_type == SCU_EVENT_TL_RNC_SUSPEND_TX)
527 status = SCI_SUCCESS;
528 break;
529 case SCI_RNC_TX_RX_SUSPENDED:
530 if (suspend_type == SCU_EVENT_TL_RNC_SUSPEND_TX_RX)
531 status = SCI_SUCCESS;
532 break;
533 case SCI_RNC_AWAIT_SUSPENSION:
534 if ((sci_rnc->suspend_type == SCU_EVENT_TL_RNC_SUSPEND_TX_RX)
535 || (suspend_type == sci_rnc->suspend_type))
536 return SCI_SUCCESS;
537 break;
538 default:
ed3efb77 539 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
14e99b4a
DW
540 "%s: invalid state %s\n", __func__,
541 rnc_state_name(state));
ed3efb77
DW
542 return SCI_FAILURE_INVALID_STATE;
543 }
ac78ed0f
JS
544 sci_rnc->user_callback = cb_fn;
545 sci_rnc->user_cookie = cb_p;
546 sci_rnc->suspend_type = suspend_type;
ed3efb77 547
ac78ed0f
JS
548 if (status == SCI_SUCCESS) { /* Already in the destination state? */
549 sci_remote_node_context_notify_user(sci_rnc);
550 return SCI_SUCCESS;
551 }
552 if (suspend_reason == SCI_SOFTWARE_SUSPENSION) {
553 isci_dev_set_hang_detection_timeout(idev, 0x00000001);
554 sci_remote_device_post_request(
555 idev, SCI_SOFTWARE_SUSPEND_CMD);
ed3efb77 556 }
ac78ed0f
JS
557 if (state != SCI_RNC_AWAIT_SUSPENSION)
558 sci_change_state(&sci_rnc->sm, SCI_RNC_AWAIT_SUSPENSION);
ed3efb77 559
ed3efb77
DW
560 return SCI_SUCCESS;
561}
562
89a7301f 563enum sci_status sci_remote_node_context_resume(struct sci_remote_node_context *sci_rnc,
ed3efb77
DW
564 scics_sds_remote_node_context_callback cb_fn,
565 void *cb_p)
566{
567 enum scis_sds_remote_node_context_states state;
568
e301370a 569 state = sci_rnc->sm.current_state_id;
ed3efb77 570 switch (state) {
e301370a 571 case SCI_RNC_INITIAL:
ed3efb77
DW
572 if (sci_rnc->remote_node_index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX)
573 return SCI_FAILURE_INVALID_STATE;
574
89a7301f
DW
575 sci_remote_node_context_setup_to_resume(sci_rnc, cb_fn, cb_p);
576 sci_remote_node_context_construct_buffer(sci_rnc);
e301370a 577 sci_change_state(&sci_rnc->sm, SCI_RNC_POSTING);
ed3efb77 578 return SCI_SUCCESS;
e301370a
EN
579 case SCI_RNC_POSTING:
580 case SCI_RNC_INVALIDATING:
581 case SCI_RNC_RESUMING:
ed3efb77
DW
582 if (sci_rnc->destination_state != SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_READY)
583 return SCI_FAILURE_INVALID_STATE;
584
585 sci_rnc->user_callback = cb_fn;
586 sci_rnc->user_cookie = cb_p;
587 return SCI_SUCCESS;
56d7c013
JS
588 case SCI_RNC_TX_SUSPENDED:
589 case SCI_RNC_TX_RX_SUSPENDED: {
78a6f06e
DW
590 struct isci_remote_device *idev = rnc_to_dev(sci_rnc);
591 struct domain_device *dev = idev->domain_dev;
ed3efb77 592
56d7c013
JS
593 /* If this is an expander attached SATA device we must
594 * invalidate and repost the RNC since this is the only way
595 * to clear the TCi to NCQ tag mapping table for the RNi.
596 * All other device types we can just resume.
597 */
89a7301f 598 sci_remote_node_context_setup_to_resume(sci_rnc, cb_fn, cb_p);
ed3efb77 599
11cc5183
DW
600 if (dev_is_sata(dev) && dev->parent)
601 sci_change_state(&sci_rnc->sm, SCI_RNC_INVALIDATING);
602 else
e301370a 603 sci_change_state(&sci_rnc->sm, SCI_RNC_RESUMING);
ed3efb77
DW
604 return SCI_SUCCESS;
605 }
e301370a 606 case SCI_RNC_AWAIT_SUSPENSION:
89a7301f 607 sci_remote_node_context_setup_to_resume(sci_rnc, cb_fn, cb_p);
ed3efb77
DW
608 return SCI_SUCCESS;
609 default:
610 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
14e99b4a
DW
611 "%s: invalid state %s\n", __func__,
612 rnc_state_name(state));
ed3efb77
DW
613 return SCI_FAILURE_INVALID_STATE;
614 }
615}
f34d9e5d 616
89a7301f 617enum sci_status sci_remote_node_context_start_io(struct sci_remote_node_context *sci_rnc,
5076a1a9 618 struct isci_request *ireq)
f34d9e5d
DW
619{
620 enum scis_sds_remote_node_context_states state;
621
e301370a 622 state = sci_rnc->sm.current_state_id;
fd536601
JS
623
624 switch (state) {
625 case SCI_RNC_READY:
626 return SCI_SUCCESS;
627 case SCI_RNC_TX_SUSPENDED:
628 case SCI_RNC_TX_RX_SUSPENDED:
629 case SCI_RNC_AWAIT_SUSPENSION:
f34d9e5d 630 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
14e99b4a
DW
631 "%s: invalid state %s\n", __func__,
632 rnc_state_name(state));
f34d9e5d 633 return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
fd536601 634 default:
14e99b4a
DW
635 dev_dbg(scirdev_to_dev(rnc_to_dev(sci_rnc)),
636 "%s: invalid state %s\n", __func__,
637 rnc_state_name(state));
638 return SCI_FAILURE_INVALID_STATE;
f34d9e5d 639 }
f34d9e5d
DW
640}
641
89a7301f 642enum sci_status sci_remote_node_context_start_task(struct sci_remote_node_context *sci_rnc,
5076a1a9 643 struct isci_request *ireq)
f34d9e5d
DW
644{
645 enum scis_sds_remote_node_context_states state;
646
e301370a 647 state = sci_rnc->sm.current_state_id;
f34d9e5d 648 switch (state) {
e301370a
EN
649 case SCI_RNC_RESUMING:
650 case SCI_RNC_READY:
651 case SCI_RNC_AWAIT_SUSPENSION:
f34d9e5d 652 return SCI_SUCCESS;
e301370a
EN
653 case SCI_RNC_TX_SUSPENDED:
654 case SCI_RNC_TX_RX_SUSPENDED:
89a7301f 655 sci_remote_node_context_resume(sci_rnc, NULL, NULL);
f34d9e5d
DW
656 return SCI_SUCCESS;
657 default:
658 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
14e99b4a
DW
659 "%s: invalid state %s\n", __func__,
660 rnc_state_name(state));
f34d9e5d
DW
661 return SCI_FAILURE_INVALID_STATE;
662 }
663}
This page took 0.118703 seconds and 5 git commands to generate.