isci: Fix RNC suspend call for SCI_RESUMING state.
[firefly-linux-kernel-4.4.55.git] / drivers / scsi / isci / remote_node_context.c
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 #include <scsi/sas_ata.h>
56 #include "host.h"
57 #include "isci.h"
58 #include "remote_device.h"
59 #include "remote_node_context.h"
60 #include "scu_event_codes.h"
61 #include "scu_task_context.h"
62
63 #undef C
64 #define C(a) (#a)
65 const char *rnc_state_name(enum scis_sds_remote_node_context_states state)
66 {
67         static const char * const strings[] = RNC_STATES;
68
69         return strings[state];
70 }
71 #undef C
72
73 /**
74  *
75  * @sci_rnc: The state of the remote node context object to check.
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  */
81 bool sci_remote_node_context_is_ready(
82         struct sci_remote_node_context *sci_rnc)
83 {
84         u32 current_state = sci_rnc->sm.current_state_id;
85
86         if (current_state == SCI_RNC_READY) {
87                 return true;
88         }
89
90         return false;
91 }
92
93 bool sci_remote_node_context_is_suspended(struct sci_remote_node_context *sci_rnc)
94 {
95         u32 current_state = sci_rnc->sm.current_state_id;
96
97         if (current_state == SCI_RNC_TX_RX_SUSPENDED)
98                 return true;
99         return false;
100 }
101
102 static union scu_remote_node_context *sci_rnc_by_id(struct isci_host *ihost, u16 id)
103 {
104         if (id < ihost->remote_node_entries &&
105             ihost->device_table[id])
106                 return &ihost->remote_node_context_table[id];
107
108         return NULL;
109 }
110
111 static void sci_remote_node_context_construct_buffer(struct sci_remote_node_context *sci_rnc)
112 {
113         struct isci_remote_device *idev = rnc_to_dev(sci_rnc);
114         struct domain_device *dev = idev->domain_dev;
115         int rni = sci_rnc->remote_node_index;
116         union scu_remote_node_context *rnc;
117         struct isci_host *ihost;
118         __le64 sas_addr;
119
120         ihost = idev->owning_port->owning_controller;
121         rnc = sci_rnc_by_id(ihost, rni);
122
123         memset(rnc, 0, sizeof(union scu_remote_node_context)
124                 * sci_remote_device_node_count(idev));
125
126         rnc->ssp.remote_node_index = rni;
127         rnc->ssp.remote_node_port_width = idev->device_port_width;
128         rnc->ssp.logical_port_index = idev->owning_port->physical_port_index;
129
130         /* sas address is __be64, context ram format is __le64 */
131         sas_addr = cpu_to_le64(SAS_ADDR(dev->sas_addr));
132         rnc->ssp.remote_sas_address_hi = upper_32_bits(sas_addr);
133         rnc->ssp.remote_sas_address_lo = lower_32_bits(sas_addr);
134
135         rnc->ssp.nexus_loss_timer_enable = true;
136         rnc->ssp.check_bit               = false;
137         rnc->ssp.is_valid                = false;
138         rnc->ssp.is_remote_node_context  = true;
139         rnc->ssp.function_number         = 0;
140
141         rnc->ssp.arbitration_wait_time = 0;
142
143         if (dev_is_sata(dev)) {
144                 rnc->ssp.connection_occupancy_timeout =
145                         ihost->user_parameters.stp_max_occupancy_timeout;
146                 rnc->ssp.connection_inactivity_timeout =
147                         ihost->user_parameters.stp_inactivity_timeout;
148         } else {
149                 rnc->ssp.connection_occupancy_timeout  =
150                         ihost->user_parameters.ssp_max_occupancy_timeout;
151                 rnc->ssp.connection_inactivity_timeout =
152                         ihost->user_parameters.ssp_inactivity_timeout;
153         }
154
155         rnc->ssp.initial_arbitration_wait_time = 0;
156
157         /* Open Address Frame Parameters */
158         rnc->ssp.oaf_connection_rate = idev->connection_rate;
159         rnc->ssp.oaf_features = 0;
160         rnc->ssp.oaf_source_zone_group = 0;
161         rnc->ssp.oaf_more_compatibility_features = 0;
162 }
163
164 static void sci_remote_node_context_save_cbparams(
165         struct sci_remote_node_context *sci_rnc,
166         scics_sds_remote_node_context_callback callback,
167         void *callback_parameter)
168 {
169         sci_rnc->user_callback = callback;
170         sci_rnc->user_cookie   = callback_parameter;
171 }
172 /**
173  *
174  * @sci_rnc:
175  * @callback:
176  * @callback_parameter:
177  *
178  * This method will setup the remote node context object so it will transition
179  * to its ready state.  If the remote node context is already setup to
180  * transition to its final state then this function does nothing. none
181  */
182 static void sci_remote_node_context_setup_to_resume(
183         struct sci_remote_node_context *sci_rnc,
184         scics_sds_remote_node_context_callback callback,
185         void *callback_parameter,
186         enum sci_remote_node_context_destination_state dest_param)
187 {
188         if (sci_rnc->destination_state != RNC_DEST_FINAL) {
189                 sci_rnc->destination_state = dest_param;
190                 if (callback != NULL)
191                         sci_remote_node_context_save_cbparams(
192                                 sci_rnc, callback, callback_parameter);
193         }
194 }
195
196 static void sci_remote_node_context_setup_to_destroy(
197         struct sci_remote_node_context *sci_rnc,
198         scics_sds_remote_node_context_callback callback,
199         void *callback_parameter)
200 {
201         sci_rnc->destination_state = RNC_DEST_FINAL;
202         sci_rnc->user_callback     = callback;
203         sci_rnc->user_cookie       = callback_parameter;
204 }
205
206 /**
207  *
208  *
209  * This method just calls the user callback function and then resets the
210  * callback.
211  */
212 static void sci_remote_node_context_notify_user(
213         struct sci_remote_node_context *rnc)
214 {
215         if (rnc->user_callback != NULL) {
216                 (*rnc->user_callback)(rnc->user_cookie);
217
218                 rnc->user_callback = NULL;
219                 rnc->user_cookie = NULL;
220         }
221 }
222
223 static void sci_remote_node_context_continue_state_transitions(struct sci_remote_node_context *rnc)
224 {
225         if ((rnc->destination_state == RNC_DEST_READY) ||
226             (rnc->destination_state == RNC_DEST_SUSPENDED_RESUME)) {
227                 rnc->destination_state = RNC_DEST_READY;
228                 sci_remote_node_context_resume(rnc, rnc->user_callback,
229                                                     rnc->user_cookie);
230         } else
231                 rnc->destination_state = RNC_DEST_UNSPECIFIED;
232 }
233
234 static void sci_remote_node_context_validate_context_buffer(struct sci_remote_node_context *sci_rnc)
235 {
236         union scu_remote_node_context *rnc_buffer;
237         struct isci_remote_device *idev = rnc_to_dev(sci_rnc);
238         struct domain_device *dev = idev->domain_dev;
239         struct isci_host *ihost = idev->owning_port->owning_controller;
240
241         rnc_buffer = sci_rnc_by_id(ihost, sci_rnc->remote_node_index);
242
243         rnc_buffer->ssp.is_valid = true;
244
245         if (dev_is_sata(dev) && dev->parent) {
246                 sci_remote_device_post_request(idev, SCU_CONTEXT_COMMAND_POST_RNC_96);
247         } else {
248                 sci_remote_device_post_request(idev, SCU_CONTEXT_COMMAND_POST_RNC_32);
249
250                 if (!dev->parent)
251                         sci_port_setup_transports(idev->owning_port,
252                                                   sci_rnc->remote_node_index);
253         }
254 }
255
256 static void sci_remote_node_context_invalidate_context_buffer(struct sci_remote_node_context *sci_rnc)
257 {
258         union scu_remote_node_context *rnc_buffer;
259         struct isci_remote_device *idev = rnc_to_dev(sci_rnc);
260         struct isci_host *ihost = idev->owning_port->owning_controller;
261
262         rnc_buffer = sci_rnc_by_id(ihost, sci_rnc->remote_node_index);
263
264         rnc_buffer->ssp.is_valid = false;
265
266         sci_remote_device_post_request(rnc_to_dev(sci_rnc),
267                                        SCU_CONTEXT_COMMAND_POST_RNC_INVALIDATE);
268 }
269
270 static void sci_remote_node_context_initial_state_enter(struct sci_base_state_machine *sm)
271 {
272         struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
273
274         /* Check to see if we have gotten back to the initial state because
275          * someone requested to destroy the remote node context object.
276          */
277         if (sm->previous_state_id == SCI_RNC_INVALIDATING) {
278                 rnc->destination_state = RNC_DEST_UNSPECIFIED;
279                 sci_remote_node_context_notify_user(rnc);
280         }
281 }
282
283 static void sci_remote_node_context_posting_state_enter(struct sci_base_state_machine *sm)
284 {
285         struct sci_remote_node_context *sci_rnc = container_of(sm, typeof(*sci_rnc), sm);
286
287         sci_remote_node_context_validate_context_buffer(sci_rnc);
288 }
289
290 static void sci_remote_node_context_invalidating_state_enter(struct sci_base_state_machine *sm)
291 {
292         struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
293
294         /* Terminate all outstanding requests. */
295         sci_remote_device_terminate_requests(rnc_to_dev(rnc));
296         sci_remote_node_context_invalidate_context_buffer(rnc);
297 }
298
299 static void sci_remote_node_context_resuming_state_enter(struct sci_base_state_machine *sm)
300 {
301         struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
302         struct isci_remote_device *idev;
303         struct domain_device *dev;
304
305         idev = rnc_to_dev(rnc);
306         dev = idev->domain_dev;
307
308         /*
309          * For direct attached SATA devices we need to clear the TLCR
310          * NCQ to TCi tag mapping on the phy and in cases where we
311          * resume because of a target reset we also need to update
312          * the STPTLDARNI register with the RNi of the device
313          */
314         if (dev_is_sata(dev) && !dev->parent)
315                 sci_port_setup_transports(idev->owning_port, rnc->remote_node_index);
316
317         sci_remote_device_post_request(idev, SCU_CONTEXT_COMMAND_POST_RNC_RESUME);
318 }
319
320 static void sci_remote_node_context_ready_state_enter(struct sci_base_state_machine *sm)
321 {
322         struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
323         enum sci_remote_node_context_destination_state dest_select;
324         int tell_user = 1;
325
326         dest_select = rnc->destination_state;
327         rnc->destination_state = RNC_DEST_UNSPECIFIED;
328
329         if ((dest_select == RNC_DEST_SUSPENDED) ||
330             (dest_select == RNC_DEST_SUSPENDED_RESUME)) {
331                 sci_remote_node_context_suspend(
332                         rnc, rnc->suspend_reason,
333                         SCI_SOFTWARE_SUSPEND_EXPECTED_EVENT);
334
335                 if (dest_select == RNC_DEST_SUSPENDED_RESUME)
336                         tell_user = 0;  /* Wait until ready again. */
337         }
338         if (tell_user)
339                 sci_remote_node_context_notify_user(rnc);
340 }
341
342 static void sci_remote_node_context_tx_suspended_state_enter(struct sci_base_state_machine *sm)
343 {
344         struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
345
346         sci_remote_node_context_continue_state_transitions(rnc);
347 }
348
349 static void sci_remote_node_context_tx_rx_suspended_state_enter(struct sci_base_state_machine *sm)
350 {
351         struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
352         struct isci_remote_device *idev = rnc_to_dev(rnc);
353         struct isci_host *ihost = idev->owning_port->owning_controller;
354         u32 new_count = rnc->suspend_count + 1;
355
356         if (new_count == 0)
357                 rnc->suspend_count = 1;
358         else
359                 rnc->suspend_count = new_count;
360         smp_wmb();
361
362         /* Terminate outstanding requests pending abort. */
363         sci_remote_device_abort_requests_pending_abort(idev);
364
365         wake_up(&ihost->eventq);
366         sci_remote_node_context_continue_state_transitions(rnc);
367 }
368
369 static void sci_remote_node_context_await_suspend_state_exit(
370         struct sci_base_state_machine *sm)
371 {
372         struct sci_remote_node_context *rnc
373                 = container_of(sm, typeof(*rnc), sm);
374         struct isci_remote_device *idev = rnc_to_dev(rnc);
375
376         if (dev_is_sata(idev->domain_dev))
377                 isci_dev_set_hang_detection_timeout(idev, 0);
378 }
379
380 static const struct sci_base_state sci_remote_node_context_state_table[] = {
381         [SCI_RNC_INITIAL] = {
382                 .enter_state = sci_remote_node_context_initial_state_enter,
383         },
384         [SCI_RNC_POSTING] = {
385                 .enter_state = sci_remote_node_context_posting_state_enter,
386         },
387         [SCI_RNC_INVALIDATING] = {
388                 .enter_state = sci_remote_node_context_invalidating_state_enter,
389         },
390         [SCI_RNC_RESUMING] = {
391                 .enter_state = sci_remote_node_context_resuming_state_enter,
392         },
393         [SCI_RNC_READY] = {
394                 .enter_state = sci_remote_node_context_ready_state_enter,
395         },
396         [SCI_RNC_TX_SUSPENDED] = {
397                 .enter_state = sci_remote_node_context_tx_suspended_state_enter,
398         },
399         [SCI_RNC_TX_RX_SUSPENDED] = {
400                 .enter_state = sci_remote_node_context_tx_rx_suspended_state_enter,
401         },
402         [SCI_RNC_AWAIT_SUSPENSION] = {
403                 .exit_state = sci_remote_node_context_await_suspend_state_exit,
404         },
405 };
406
407 void sci_remote_node_context_construct(struct sci_remote_node_context *rnc,
408                                             u16 remote_node_index)
409 {
410         memset(rnc, 0, sizeof(struct sci_remote_node_context));
411
412         rnc->remote_node_index = remote_node_index;
413         rnc->destination_state = RNC_DEST_UNSPECIFIED;
414
415         sci_init_sm(&rnc->sm, sci_remote_node_context_state_table, SCI_RNC_INITIAL);
416 }
417
418 enum sci_status sci_remote_node_context_event_handler(struct sci_remote_node_context *sci_rnc,
419                                                            u32 event_code)
420 {
421         enum scis_sds_remote_node_context_states state;
422         u32 next_state;
423
424         state = sci_rnc->sm.current_state_id;
425         switch (state) {
426         case SCI_RNC_POSTING:
427                 switch (scu_get_event_code(event_code)) {
428                 case SCU_EVENT_POST_RNC_COMPLETE:
429                         sci_change_state(&sci_rnc->sm, SCI_RNC_READY);
430                         break;
431                 default:
432                         goto out;
433                 }
434                 break;
435         case SCI_RNC_INVALIDATING:
436                 if (scu_get_event_code(event_code) == SCU_EVENT_POST_RNC_INVALIDATE_COMPLETE) {
437                         if (sci_rnc->destination_state == RNC_DEST_FINAL)
438                                 next_state = SCI_RNC_INITIAL;
439                         else
440                                 next_state = SCI_RNC_POSTING;
441                         sci_change_state(&sci_rnc->sm, next_state);
442                 } else {
443                         switch (scu_get_event_type(event_code)) {
444                         case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
445                         case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
446                                 /* We really dont care if the hardware is going to suspend
447                                  * the device since it's being invalidated anyway */
448                                 dev_dbg(scirdev_to_dev(rnc_to_dev(sci_rnc)),
449                                         "%s: SCIC Remote Node Context 0x%p was "
450                                         "suspeneded by hardware while being "
451                                         "invalidated.\n", __func__, sci_rnc);
452                                 break;
453                         default:
454                                 goto out;
455                         }
456                 }
457                 break;
458         case SCI_RNC_RESUMING:
459                 if (scu_get_event_code(event_code) == SCU_EVENT_POST_RCN_RELEASE) {
460                         sci_change_state(&sci_rnc->sm, SCI_RNC_READY);
461                 } else {
462                         switch (scu_get_event_type(event_code)) {
463                         case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
464                         case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
465                                 /* We really dont care if the hardware is going to suspend
466                                  * the device since it's being resumed anyway */
467                                 dev_dbg(scirdev_to_dev(rnc_to_dev(sci_rnc)),
468                                         "%s: SCIC Remote Node Context 0x%p was "
469                                         "suspeneded by hardware while being resumed.\n",
470                                         __func__, sci_rnc);
471                                 break;
472                         default:
473                                 goto out;
474                         }
475                 }
476                 break;
477         case SCI_RNC_READY:
478                 switch (scu_get_event_type(event_code)) {
479                 case SCU_EVENT_TL_RNC_SUSPEND_TX:
480                         sci_change_state(&sci_rnc->sm, SCI_RNC_TX_SUSPENDED);
481                         sci_rnc->suspend_type = scu_get_event_type(event_code);
482                         break;
483                 case SCU_EVENT_TL_RNC_SUSPEND_TX_RX:
484                         sci_change_state(&sci_rnc->sm, SCI_RNC_TX_RX_SUSPENDED);
485                         sci_rnc->suspend_type = scu_get_event_type(event_code);
486                         break;
487                 default:
488                         goto out;
489                 }
490                 break;
491         case SCI_RNC_AWAIT_SUSPENSION:
492                 switch (scu_get_event_type(event_code)) {
493                 case SCU_EVENT_TL_RNC_SUSPEND_TX:
494                         next_state = SCI_RNC_TX_SUSPENDED;
495                         break;
496                 case SCU_EVENT_TL_RNC_SUSPEND_TX_RX:
497                         next_state = SCI_RNC_TX_RX_SUSPENDED;
498                         break;
499                 default:
500                         goto out;
501                 }
502                 if (sci_rnc->suspend_type == scu_get_event_type(event_code))
503                         sci_change_state(&sci_rnc->sm, next_state);
504                 break;
505         default:
506                 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
507                          "%s: invalid state: %s\n", __func__,
508                          rnc_state_name(state));
509                 return SCI_FAILURE_INVALID_STATE;
510         }
511         return SCI_SUCCESS;
512
513  out:
514         dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
515                  "%s: code: %#x state: %s\n", __func__, event_code,
516                  rnc_state_name(state));
517         return SCI_FAILURE;
518
519 }
520
521 enum sci_status sci_remote_node_context_destruct(struct sci_remote_node_context *sci_rnc,
522                                                       scics_sds_remote_node_context_callback cb_fn,
523                                                       void *cb_p)
524 {
525         enum scis_sds_remote_node_context_states state;
526
527         state = sci_rnc->sm.current_state_id;
528         switch (state) {
529         case SCI_RNC_INVALIDATING:
530                 sci_remote_node_context_setup_to_destroy(sci_rnc, cb_fn, cb_p);
531                 return SCI_SUCCESS;
532         case SCI_RNC_POSTING:
533         case SCI_RNC_RESUMING:
534         case SCI_RNC_READY:
535         case SCI_RNC_TX_SUSPENDED:
536         case SCI_RNC_TX_RX_SUSPENDED:
537         case SCI_RNC_AWAIT_SUSPENSION:
538                 sci_remote_node_context_setup_to_destroy(sci_rnc, cb_fn, cb_p);
539                 sci_change_state(&sci_rnc->sm, SCI_RNC_INVALIDATING);
540                 return SCI_SUCCESS;
541         case SCI_RNC_INITIAL:
542                 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
543                          "%s: invalid state: %s\n", __func__,
544                          rnc_state_name(state));
545                 /* We have decided that the destruct request on the remote node context
546                  * can not fail since it is either in the initial/destroyed state or is
547                  * can be destroyed.
548                  */
549                 return SCI_SUCCESS;
550         default:
551                 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
552                          "%s: invalid state %s\n", __func__,
553                          rnc_state_name(state));
554                 return SCI_FAILURE_INVALID_STATE;
555         }
556 }
557
558 enum sci_status sci_remote_node_context_suspend(
559                         struct sci_remote_node_context *sci_rnc,
560                         enum sci_remote_node_suspension_reasons suspend_reason,
561                         u32 suspend_type)
562 {
563         enum scis_sds_remote_node_context_states state
564                 = sci_rnc->sm.current_state_id;
565         struct isci_remote_device *idev = rnc_to_dev(sci_rnc);
566         enum sci_status status = SCI_FAILURE_INVALID_STATE;
567         enum sci_remote_node_context_destination_state dest_param =
568                 RNC_DEST_UNSPECIFIED;
569
570         dev_dbg(scirdev_to_dev(idev),
571                 "%s: current state %d, current suspend_type %x dest state %d,"
572                         " arg suspend_reason %d, arg suspend_type %x",
573                 __func__, state, sci_rnc->suspend_type,
574                 sci_rnc->destination_state, suspend_reason,
575                 suspend_type);
576
577         /* Disable automatic state continuations if explicitly suspending. */
578         if ((suspend_reason == SCI_HW_SUSPEND) ||
579             (sci_rnc->destination_state == RNC_DEST_FINAL))
580                 dest_param = sci_rnc->destination_state;
581
582         switch (state) {
583         case SCI_RNC_READY:
584                 break;
585         case SCI_RNC_INVALIDATING:
586                 if (sci_rnc->destination_state == RNC_DEST_FINAL) {
587                         dev_warn(scirdev_to_dev(idev),
588                                  "%s: already destroying %p\n",
589                                  __func__, sci_rnc);
590                         return SCI_FAILURE_INVALID_STATE;
591                 }
592                 /* Fall through and handle like SCI_RNC_POSTING */
593         case SCI_RNC_RESUMING:
594                 /* Fall through and handle like SCI_RNC_POSTING */
595         case SCI_RNC_POSTING:
596                 /* Set the destination state to AWAIT - this signals the
597                  * entry into the SCI_RNC_READY state that a suspension
598                  * needs to be done immediately.
599                  */
600                 sci_rnc->destination_state = RNC_DEST_SUSPENDED;
601                 sci_rnc->suspend_type = suspend_type;
602                 sci_rnc->suspend_reason = suspend_reason;
603                 return SCI_SUCCESS;
604
605         case SCI_RNC_TX_SUSPENDED:
606                 if (suspend_type == SCU_EVENT_TL_RNC_SUSPEND_TX)
607                         status = SCI_SUCCESS;
608                 break;
609         case SCI_RNC_TX_RX_SUSPENDED:
610                 if (suspend_type == SCU_EVENT_TL_RNC_SUSPEND_TX_RX)
611                         status = SCI_SUCCESS;
612                 break;
613         case SCI_RNC_AWAIT_SUSPENSION:
614                 if ((sci_rnc->suspend_type == SCU_EVENT_TL_RNC_SUSPEND_TX_RX)
615                     || (suspend_type == sci_rnc->suspend_type))
616                         return SCI_SUCCESS;
617                 break;
618         default:
619                 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
620                          "%s: invalid state %s\n", __func__,
621                          rnc_state_name(state));
622                 return SCI_FAILURE_INVALID_STATE;
623         }
624         sci_rnc->destination_state = dest_param;
625         sci_rnc->suspend_type = suspend_type;
626         sci_rnc->suspend_reason = suspend_reason;
627
628         if (status == SCI_SUCCESS) { /* Already in the destination state? */
629                 struct isci_host *ihost = idev->owning_port->owning_controller;
630
631                 wake_up_all(&ihost->eventq); /* Let observers look. */
632                 return SCI_SUCCESS;
633         }
634         if ((suspend_reason == SCI_SW_SUSPEND_NORMAL) ||
635             (suspend_reason == SCI_SW_SUSPEND_LINKHANG_DETECT)) {
636
637                 if (suspend_reason == SCI_SW_SUSPEND_LINKHANG_DETECT)
638                         isci_dev_set_hang_detection_timeout(idev, 0x00000001);
639
640                 sci_remote_device_post_request(
641                         idev, SCI_SOFTWARE_SUSPEND_CMD);
642         }
643         if (state != SCI_RNC_AWAIT_SUSPENSION)
644                 sci_change_state(&sci_rnc->sm, SCI_RNC_AWAIT_SUSPENSION);
645
646         return SCI_SUCCESS;
647 }
648
649 enum sci_status sci_remote_node_context_resume(struct sci_remote_node_context *sci_rnc,
650                                                     scics_sds_remote_node_context_callback cb_fn,
651                                                     void *cb_p)
652 {
653         enum scis_sds_remote_node_context_states state;
654         struct isci_remote_device *idev = rnc_to_dev(sci_rnc);
655
656         state = sci_rnc->sm.current_state_id;
657         dev_dbg(scirdev_to_dev(idev),
658                 "%s: state %s, cb_fn = %p, cb_p = %p; dest_state = %d; "
659                         "dev resume path %s\n",
660                 __func__, rnc_state_name(state), cb_fn, cb_p,
661                 sci_rnc->destination_state,
662                 test_bit(IDEV_ABORT_PATH_ACTIVE, &idev->flags)
663                         ? "<abort active>" : "<normal>");
664
665         switch (state) {
666         case SCI_RNC_INITIAL:
667                 if (sci_rnc->remote_node_index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX)
668                         return SCI_FAILURE_INVALID_STATE;
669
670                 if (test_bit(IDEV_ABORT_PATH_ACTIVE, &idev->flags))
671                         sci_remote_node_context_save_cbparams(sci_rnc, cb_fn,
672                                                               cb_p);
673                 else {
674                         sci_remote_node_context_setup_to_resume(sci_rnc, cb_fn,
675                                                         cb_p, RNC_DEST_READY);
676                         sci_remote_node_context_construct_buffer(sci_rnc);
677                         sci_change_state(&sci_rnc->sm, SCI_RNC_POSTING);
678                 }
679                 return SCI_SUCCESS;
680
681         case SCI_RNC_POSTING:
682         case SCI_RNC_INVALIDATING:
683         case SCI_RNC_RESUMING:
684                 if (test_bit(IDEV_ABORT_PATH_ACTIVE, &idev->flags))
685                         sci_remote_node_context_save_cbparams(sci_rnc, cb_fn,
686                                                               cb_p);
687                 else {
688                         /* We are still waiting to post when a resume was
689                          * requested.
690                          */
691                         switch (sci_rnc->destination_state) {
692                         case RNC_DEST_SUSPENDED:
693                         case RNC_DEST_SUSPENDED_RESUME:
694                                 /* Previously waiting to suspend after posting.
695                                  * Now continue onto resumption.
696                                  */
697                                 sci_remote_node_context_setup_to_resume(
698                                         sci_rnc, cb_fn, cb_p,
699                                         RNC_DEST_SUSPENDED_RESUME);
700                                 break;
701                         default:
702                                 sci_remote_node_context_setup_to_resume(
703                                         sci_rnc, cb_fn, cb_p,
704                                         RNC_DEST_READY);
705                                 break;
706                         }
707                 }
708                 return SCI_SUCCESS;
709
710         case SCI_RNC_TX_SUSPENDED:
711         case SCI_RNC_TX_RX_SUSPENDED:
712                 if (test_bit(IDEV_ABORT_PATH_ACTIVE, &idev->flags))
713                         sci_remote_node_context_save_cbparams(sci_rnc, cb_fn,
714                                                               cb_p);
715                 else {
716                         struct domain_device *dev = idev->domain_dev;
717                         /* If this is an expander attached SATA device we must
718                          * invalidate and repost the RNC since this is the only
719                          * way to clear the TCi to NCQ tag mapping table for
720                          * the RNi. All other device types we can just resume.
721                          */
722                         sci_remote_node_context_setup_to_resume(
723                                 sci_rnc, cb_fn, cb_p, RNC_DEST_READY);
724
725                         if (dev_is_sata(dev) && dev->parent)
726                                 sci_change_state(&sci_rnc->sm,
727                                                  SCI_RNC_INVALIDATING);
728                         else
729                                 sci_change_state(&sci_rnc->sm,
730                                                  SCI_RNC_RESUMING);
731                 }
732                 return SCI_SUCCESS;
733
734         case SCI_RNC_AWAIT_SUSPENSION:
735                 if (test_bit(IDEV_ABORT_PATH_ACTIVE, &idev->flags))
736                         sci_remote_node_context_save_cbparams(sci_rnc, cb_fn,
737                                                               cb_p);
738                 else
739                         sci_remote_node_context_setup_to_resume(
740                                 sci_rnc, cb_fn, cb_p,
741                                 RNC_DEST_SUSPENDED_RESUME);
742                 return SCI_SUCCESS;
743         default:
744                 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
745                          "%s: invalid state %s\n", __func__,
746                          rnc_state_name(state));
747                 return SCI_FAILURE_INVALID_STATE;
748         }
749 }
750
751 enum sci_status sci_remote_node_context_start_io(struct sci_remote_node_context *sci_rnc,
752                                                              struct isci_request *ireq)
753 {
754         enum scis_sds_remote_node_context_states state;
755
756         state = sci_rnc->sm.current_state_id;
757
758         switch (state) {
759         case SCI_RNC_READY:
760                 return SCI_SUCCESS;
761         case SCI_RNC_TX_SUSPENDED:
762         case SCI_RNC_TX_RX_SUSPENDED:
763         case SCI_RNC_AWAIT_SUSPENSION:
764                 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
765                          "%s: invalid state %s\n", __func__,
766                          rnc_state_name(state));
767                 return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
768         default:
769                 dev_dbg(scirdev_to_dev(rnc_to_dev(sci_rnc)),
770                         "%s: invalid state %s\n", __func__,
771                         rnc_state_name(state));
772                 return SCI_FAILURE_INVALID_STATE;
773         }
774 }
775
776 enum sci_status sci_remote_node_context_start_task(
777         struct sci_remote_node_context *sci_rnc,
778         struct isci_request *ireq,
779         scics_sds_remote_node_context_callback cb_fn,
780         void *cb_p)
781 {
782         enum sci_status status = sci_remote_node_context_resume(sci_rnc,
783                                                                 cb_fn, cb_p);
784         if (status != SCI_SUCCESS)
785                 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
786                         "%s: resume failed: %d\n", __func__, status);
787         return status;
788 }
789
790 int sci_remote_node_context_is_safe_to_abort(
791         struct sci_remote_node_context *sci_rnc)
792 {
793         enum scis_sds_remote_node_context_states state;
794
795         state = sci_rnc->sm.current_state_id;
796         switch (state) {
797         case SCI_RNC_INVALIDATING:
798         case SCI_RNC_TX_RX_SUSPENDED:
799                 return 1;
800         case SCI_RNC_POSTING:
801         case SCI_RNC_RESUMING:
802         case SCI_RNC_READY:
803         case SCI_RNC_TX_SUSPENDED:
804         case SCI_RNC_AWAIT_SUSPENSION:
805         case SCI_RNC_INITIAL:
806                 return 0;
807         default:
808                 dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
809                          "%s: invalid state %d\n", __func__, state);
810                 return 0;
811         }
812 }